Ocean
Ocean::Media::Android::VideoDecoder Class Reference

This class implements a simple video decoder for Android using encoded media samples from memory as input. More...

Public Member Functions

 VideoDecoder ()
 Default constructor creating an un-initialized decoder. More...
 
 VideoDecoder (VideoDecoder &&videoDecoder) noexcept
 Move constructor. More...
 
 ~VideoDecoder ()
 Destructs the video decoder and releases all associated resources. More...
 
bool initialize (const std::string &mime, const unsigned int width, const unsigned int height)
 
bool start ()
 Starts the video decoder. More...
 
bool stop ()
 Stops the video decoder. More...
 
bool pushSample (const void *data, const size_t size, const uint64_t presentationTime)
 Adds a new media sample which needs to be decoded to the video decoder. More...
 
Frame popFrame (int64_t *presentationTime=nullptr)
 Optional the frame's presentation time will be returned, this is the presentation time which was used when the corresponding sample was provided in decodedSample(). More...
 
bool isInitialized () const
 Returns whether this decode is initialized. More...
 
bool isStarted () const
 Returns whether this decoder is currently running. More...
 
void release ()
 Explicitly releases this video encoder. More...
 
VideoDecoderoperator= (VideoDecoder &&videoDecoder) noexcept
 Move operator. More...
 

Static Public Member Functions

static Frame extractVideoFrameFromCodecOutputBuffer (AMediaCodec *const mediaCodec, int64_t *presentationTime=nullptr)
 Extracts the video frame from an output buffer of a video codec. More...
 

Protected Member Functions

 VideoDecoder (const VideoDecoder &)=delete
 Disabled copy constructor. More...
 
VideoDecoderoperator= (const VideoDecoder &)=delete
 Disabled copy operator. More...
 

Protected Attributes

NativeMediaLibrary::ScopedSubscription nativeMediaLibrarySubscription_
 The subscription for the native media library. More...
 
NativeMediaLibrary::ScopedAMediaCodec decoder_
 The Android media decoder used to decode the video. More...
 
bool isStarted_ = false
 True, if the decoder is currently started. More...
 
Lock lock_
 The decoder's lock. More...
 

Detailed Description

This class implements a simple video decoder for Android using encoded media samples from memory as input.

Usage:

// a function which is e.g., running in a separate thread
void threadRun()
{
VideoDecoder videoDecoder;
// initializing the decoder with the input format of the media samples
if (!videoDecoder.initialize("video/avc", 1920u, 1080u))
{
// handle error
}
if (!videoDecoder.start())
{
// handle error
}
unsigned int frameIndex = 0u;
double frameRate = 30.0;
while (true)
{
void* sampleData = nullptr;
size_t sampleSize = 0;
// external function: function needs to provide the new media samples from an external source - e.g., from an external webcam, a video stream, etc.
if (doesNewInputSampleExist(sampleData, &sampleSize))
{
// presentation time in microseconds
uint64_t presentationTime = uint64_t(1.0e6 * double(frameIndex) / frameRate);
// we forward the media sample to the decoder, eventually it will be decoded and will be available through decodedFrame()
if (!videoDecoder.pushSample(sampleData, sampleSize, presentationTime))
{
// handle error
}
++frameIndex;
}
// we simply check whether another frame has been decoded (there may be a delay between
Frame newFrame = videoDecoder.popFrame();
if (newFrame.isValid())
{
// external function: receiving new frames and processes the frames
sendFrameToReceiver(std::move(newFrame));
}
}
}
VideoDecoder()
Default constructor creating an un-initialized decoder.

Constructor & Destructor Documentation

◆ VideoDecoder() [1/3]

Ocean::Media::Android::VideoDecoder::VideoDecoder ( )

Default constructor creating an un-initialized decoder.

◆ VideoDecoder() [2/3]

Ocean::Media::Android::VideoDecoder::VideoDecoder ( VideoDecoder &&  videoDecoder)
inlinenoexcept

Move constructor.

Parameters
videoDecoderThe decoder to be moved

◆ ~VideoDecoder()

Ocean::Media::Android::VideoDecoder::~VideoDecoder ( )

Destructs the video decoder and releases all associated resources.

◆ VideoDecoder() [3/3]

Ocean::Media::Android::VideoDecoder::VideoDecoder ( const VideoDecoder )
protecteddelete

Disabled copy constructor.

Member Function Documentation

◆ extractVideoFrameFromCodecOutputBuffer()

static Frame Ocean::Media::Android::VideoDecoder::extractVideoFrameFromCodecOutputBuffer ( AMediaCodec *const  mediaCodec,
int64_t *  presentationTime = nullptr 
)
static

Extracts the video frame from an output buffer of a video codec.

Parameters
mediaCodecThe media codec to which the output buffer belongs, must be valid
presentationTimeOptional resulting presentation time in micro seconds, with range (-infinity, infinity)
Returns
The resulting extracted frame, invalid if the frame could not be extracted

◆ initialize()

bool Ocean::Media::Android::VideoDecoder::initialize ( const std::string &  mime,
const unsigned int  width,
const unsigned int  height 
)
Parameters
mimeThe MIME type (Multipurpose Internet Mail Extensions) of the video to be decoded, e.g., "video/avc", "video/hevc", ...
widthThe width of the video to be decoded, in pixel, with range [1, infinity)
heightThe height of the video to be decoded, in pixel, with range [1, infinity)
Returns
True, if succeeded
See also
isInitialized().

◆ isInitialized()

bool Ocean::Media::Android::VideoDecoder::isInitialized ( ) const
inline

Returns whether this decode is initialized.

Returns
True, if so
See also
initialize().

◆ isStarted()

bool Ocean::Media::Android::VideoDecoder::isStarted ( ) const
inline

Returns whether this decoder is currently running.

Returns
True, if so
See also
start().

◆ operator=() [1/2]

VideoDecoder& Ocean::Media::Android::VideoDecoder::operator= ( const VideoDecoder )
protecteddelete

Disabled copy operator.

Returns
Reference to this object

◆ operator=() [2/2]

VideoDecoder & Ocean::Media::Android::VideoDecoder::operator= ( VideoDecoder &&  videoDecoder)
inlinenoexcept

Move operator.

Parameters
videoDecoderThe video decoder to be moved
Returns
Reference to this object

◆ popFrame()

Frame Ocean::Media::Android::VideoDecoder::popFrame ( int64_t *  presentationTime = nullptr)

Optional the frame's presentation time will be returned, this is the presentation time which was used when the corresponding sample was provided in decodedSample().

Parameters
presentationTimeOptional resulting presentation time in micro seconds, with range (-infinity, infinity)
Returns
The resulting frame, invalid if currently no decoded frame is available
See also
pushSample().

◆ pushSample()

bool Ocean::Media::Android::VideoDecoder::pushSample ( const void *  data,
const size_t  size,
const uint64_t  presentationTime 
)

Adds a new media sample which needs to be decoded to the video decoder.

The decoder needs to be initialized and started.
The presentation time is mainly intended to allow associating the provided encoded media sample with the resulting decoded frame when calling popFrame().
However, it's recommended to define a reasonable presentation time for each sample (e.g., let the first sample start at 0 and increment the time by 1^6/fps for each following sample.

Parameters
dataThe data of the encoded media sample, must be valid
sizeThe size of the encoded media sample, in bytes, with range [1, infinity)
presentationTimeThe presentation time of the sample, in microseconds, with range [0, infinity)
Returns
True, if succeeded
See also
start(), isInitialized(), isStarted().

◆ release()

void Ocean::Media::Android::VideoDecoder::release ( )

Explicitly releases this video encoder.

If the encoder is still running, the encoder will be stopped as well.

◆ start()

bool Ocean::Media::Android::VideoDecoder::start ( )

Starts the video decoder.

Returns
True, if succeeded
See also
isStarted().

◆ stop()

bool Ocean::Media::Android::VideoDecoder::stop ( )

Stops the video decoder.

Returns
True, if succeeded

Field Documentation

◆ decoder_

NativeMediaLibrary::ScopedAMediaCodec Ocean::Media::Android::VideoDecoder::decoder_
protected

The Android media decoder used to decode the video.

◆ isStarted_

bool Ocean::Media::Android::VideoDecoder::isStarted_ = false
protected

True, if the decoder is currently started.

◆ lock_

Lock Ocean::Media::Android::VideoDecoder::lock_
mutableprotected

The decoder's lock.

◆ nativeMediaLibrarySubscription_

NativeMediaLibrary::ScopedSubscription Ocean::Media::Android::VideoDecoder::nativeMediaLibrarySubscription_
protected

The subscription for the native media library.


The documentation for this class was generated from the following file: