Gnash  0.8.11dev
Public Types | Public Member Functions | List of all members
gnash::media::MediaParser Class Referenceabstract

The MediaParser class provides cursor-based access to encoded media frames. More...

#include <MediaParser.h>

Inheritance diagram for gnash::media::MediaParser:
gnash::media::ffmpeg::MediaParserFfmpeg gnash::media::FLVParser gnash::media::gst::MediaParserGst gnash::media::haiku::MediaParserHaiku

Public Types

typedef std::multimap< std::uint64_t, std::shared_ptr< SimpleBuffer > > MetaTags
 A container for executable MetaTags contained in media streams. More...
 
typedef std::vector< MetaTags::mapped_type > OrderedMetaTags
 

Public Member Functions

 MediaParser (std::unique_ptr< IOChannel > stream)
 
virtual ~MediaParser ()
 
virtual bool seek (std::uint32_t &time)=0
 Seeks to the closest possible position the given position, and returns the new position. More...
 
DSOEXPORT std::uint64_t getBufferLength () const
 Returns mininum length of available buffers in milliseconds. More...
 
DSOEXPORT bool isBufferEmpty () const
 Return true if both audio and video buffers are empty. More...
 
DSOEXPORT std::uint_fast64_t getBufferTime () const
 Return the time we want the parser thread to maintain in the buffer. More...
 
DSOEXPORT void setBufferTime (std::uint_fast64_t t)
 Set the time we want the parser thread to maintain in the buffer. More...
 
DSOEXPORT bool nextFrameTimestamp (std::uint64_t &ts) const
 Get timestamp of the next frame available, if any. More...
 
DSOEXPORT bool nextVideoFrameTimestamp (std::uint64_t &ts) const
 Get timestamp of the video frame which would be returned on nextVideoFrame. More...
 
DSOEXPORT std::unique_ptr< EncodedVideoFramenextVideoFrame ()
 Returns the next video frame in the parsed buffer, advancing video cursor. More...
 
DSOEXPORT bool nextAudioFrameTimestamp (std::uint64_t &ts) const
 Get timestamp of the audio frame which would be returned on nextAudioFrame. More...
 
DSOEXPORT std::unique_ptr< EncodedAudioFramenextAudioFrame ()
 Returns the next audio frame in the parsed buffer, advancing audio cursor. More...
 
VideoInfogetVideoInfo ()
 Returns a VideoInfo class about the videostream. More...
 
AudioInfogetAudioInfo ()
 Returns a AudioInfo class about the audiostream. More...
 
bool parsingCompleted () const
 Return true of parsing is completed. More...
 
virtual bool indexingCompleted () const
 Return true of indexing is completed. More...
 
virtual std::uint64_t getBytesLoaded () const
 Return number of bytes parsed so far. More...
 
std::uint64_t getBytesTotal () const
 Return total number of bytes in input. More...
 
virtual bool parseNextChunk ()=0
 Parse next chunk of input. More...
 
virtual void fetchMetaTags (OrderedMetaTags &tags, std::uint64_t ts)
 Retrieve any parsed metadata tags up to a specified timestamp. More...
 
virtual boost::optional< Id3InfogetId3Info () const
 Get ID3 data from the parsed stream if it exists. More...
 
std::unique_ptr< VideoInfo_videoInfo
 Subclasses must set the following variables: More...
 
std::unique_ptr< AudioInfo_audioInfo
 Info about the audio stream (if any) More...
 
bool _parsingComplete
 Whether the parsing is complete or not. More...
 
std::atomic< std::uint_fast64_t > _bytesLoaded
 Number of bytes loaded. More...
 
std::unique_ptr< IOChannel_stream
 The stream used to access the file. More...
 
std::mutex _streamMutex
 
std::atomic< std::uint_fast64_t > _bufferTime
 
std::thread _parserThread
 
std::atomic< bool > _parserThreadKillRequested
 
std::condition_variable _parserThreadWakeup
 
std::mutex _qMutex
 mutex protecting access to the a/v encoded frames queues More...
 
bool _seekRequest
 
void startParserThread ()
 }@ More...
 
void stopParserThread ()
 Stop the parser thread. More...
 
void clearBuffers ()
 Clear the a/v buffers. More...
 
void pushEncodedAudioFrame (std::unique_ptr< EncodedAudioFrame > frame)
 Push an encoded audio frame to buffer. More...
 
void pushEncodedVideoFrame (std::unique_ptr< EncodedVideoFrame > frame)
 Push an encoded video frame to buffer. More...
 
void parserLoop ()
 
bool parserThreadKillRequested () const
 
void waitIfNeeded (std::unique_lock< std::mutex > &qMutexLock)
 
void wakeupParserThread ()
 
bool bufferFull () const
 Method to check if buffer is full w/out locking the _qMutex. More...
 

Detailed Description

The MediaParser class provides cursor-based access to encoded media frames.

Cursor-based access allow seeking as close as possible to a specified time and fetching frames from there on, sequentially. See seek(), nextVideoFrame(), nextAudioFrame()

Input is received from a IOChannel object.

Member Typedef Documentation

§ MetaTags

typedef std::multimap<std::uint64_t, std::shared_ptr<SimpleBuffer> > gnash::media::MediaParser::MetaTags

A container for executable MetaTags contained in media streams.

Presently only known in FLV.

§ OrderedMetaTags

typedef std::vector<MetaTags::mapped_type> gnash::media::MediaParser::OrderedMetaTags

Constructor & Destructor Documentation

§ MediaParser()

gnash::media::MediaParser::MediaParser ( std::unique_ptr< IOChannel stream)

§ ~MediaParser()

gnash::media::MediaParser::~MediaParser ( )
virtual

References stopParserThread().

Member Function Documentation

§ bufferFull()

bool gnash::media::MediaParser::bufferFull ( ) const
protected

Method to check if buffer is full w/out locking the _qMutex.

This is intended for being called by waitIfNeeded, which is passed a locked lock on _qMutex, and by parseNextChunk to determine whether to index-only or also push on queue.

References getBufferTime(), and gnash::log_debug().

Referenced by gnash::media::FLVParser::parseNextChunk(), and waitIfNeeded().

§ clearBuffers()

void gnash::media::MediaParser::clearBuffers ( )
protected

§ fetchMetaTags()

void gnash::media::MediaParser::fetchMetaTags ( OrderedMetaTags tags,
std::uint64_t  ts 
)
virtual

Retrieve any parsed metadata tags up to a specified timestamp.

Parameters
tsThe latest timestamp to retrieve metadata for.
tagsThis is filled with shared pointers to metatags in timestamp order. Ownership of the data is shared. It is destroyed automatically along with the last owner. Metadata is currently only parsed from FLV streams. The default is a no-op.

Reimplemented in gnash::media::FLVParser.

§ getAudioInfo()

AudioInfo* gnash::media::MediaParser::getAudioInfo ( )
inline

Returns a AudioInfo class about the audiostream.

Returns
a AudioInfo class about the audiostream, or zero if unknown (no audio or not enough data parsed yet).

§ getBufferLength()

std::uint64_t gnash::media::MediaParser::getBufferLength ( ) const

Returns mininum length of available buffers in milliseconds.

TODO: FIXME: NOTE: this is currently used by NetStream.bufferLength but is bogus as it doesn't take the current playhead cursor time into account. A proper way would be having a getLastBufferTime () interface here, returning minimun timestamp of last available frames and let NetSTream::bufferLength() use that with playhead time to find out...

References _qMutex.

§ getBufferTime()

DSOEXPORT std::uint_fast64_t gnash::media::MediaParser::getBufferTime ( ) const
inline

Return the time we want the parser thread to maintain in the buffer.

Referenced by bufferFull().

§ getBytesLoaded()

virtual std::uint64_t gnash::media::MediaParser::getBytesLoaded ( ) const
inlinevirtual

§ getBytesTotal()

std::uint64_t gnash::media::MediaParser::getBytesTotal ( ) const
inline

Return total number of bytes in input.

§ getId3Info()

boost::optional< Id3Info > gnash::media::MediaParser::getId3Info ( ) const
virtual

Get ID3 data from the parsed stream if it exists.

It's best to do this only when parsingComplete is true.

Reimplemented in gnash::media::gst::MediaParserGst, and gnash::media::ffmpeg::MediaParserFfmpeg.

References _, _audioInfo, _videoInfo, gnash::log_debug(), gnash::log_error(), parseNextChunk(), and parsingCompleted().

§ getVideoInfo()

VideoInfo* gnash::media::MediaParser::getVideoInfo ( )
inline

Returns a VideoInfo class about the videostream.

Returns
a VideoInfo class about the videostream, or zero if unknown (no video or not enough data parsed yet).

§ indexingCompleted()

virtual bool gnash::media::MediaParser::indexingCompleted ( ) const
inlinevirtual

Return true of indexing is completed.

If this function returns false, parseNextChunk will be called even when buffers are full. Parsers supporting indexing separated from parsing should override this method and have parseNextChunk figure if they only need to index or to parse based on bufferFull.

Reimplemented in gnash::media::FLVParser.

Referenced by waitIfNeeded().

§ isBufferEmpty()

bool gnash::media::MediaParser::isBufferEmpty ( ) const

Return true if both audio and video buffers are empty.

NOTE: locks _qMutex

References _qMutex.

§ nextAudioFrame()

std::unique_ptr< EncodedAudioFrame > gnash::media::MediaParser::nextAudioFrame ( )

Returns the next audio frame in the parsed buffer, advancing audio cursor.

If no frame has been played before the first frame is returned. If there is no more frames in the parsed buffer NULL is returned. you can check with parsingCompleted() to know wheter this is due to EOF reached.

References _audioInfo, _parserThreadWakeup, _qMutex, gnash::log_debug(), parseNextChunk(), and parsingCompleted().

§ nextAudioFrameTimestamp()

bool gnash::media::MediaParser::nextAudioFrameTimestamp ( std::uint64_t &  ts) const

Get timestamp of the audio frame which would be returned on nextAudioFrame.

Returns
false if there no video frame left (either none or no more)

NOTE: locks _qMutex

References _audioInfo, _qMutex, parseNextChunk(), parsingCompleted(), and gnash::media::EncodedAudioFrame::timestamp.

§ nextFrameTimestamp()

bool gnash::media::MediaParser::nextFrameTimestamp ( std::uint64_t &  ts) const

Get timestamp of the next frame available, if any.

Parameters
tswill be set to timestamp of next available frame
Returns
false if no frame is available yet

NOTE: locks _qMutex

References _qMutex, _videoInfo, parseNextChunk(), and parsingCompleted().

§ nextVideoFrame()

std::unique_ptr< EncodedVideoFrame > gnash::media::MediaParser::nextVideoFrame ( )

Returns the next video frame in the parsed buffer, advancing video cursor.

If no frame has been played before the first frame is returned. If there is no more frames in the parsed buffer NULL is returned. you can check with parsingCompleted() to know wheter this is due to EOF reached.

References _parserThreadWakeup, _qMutex, _videoInfo, gnash::log_debug(), parseNextChunk(), and parsingCompleted().

§ nextVideoFrameTimestamp()

bool gnash::media::MediaParser::nextVideoFrameTimestamp ( std::uint64_t &  ts) const

Get timestamp of the video frame which would be returned on nextVideoFrame.

Returns
false if there no video frame left (either none or no more)

NOTE: locks _qMutex

References _qMutex, and gnash::media::EncodedVideoFrame::timestamp().

§ parseNextChunk()

virtual bool gnash::media::MediaParser::parseNextChunk ( )
pure virtual

Parse next chunk of input.

The implementations are required to parse a small chunk of input, so to avoid blocking too much if parsing conditions change (ie: seek or destruction requested)

When LOAD_MEDIA_IN_A_SEPARATE_THREAD is defined, this should never be called by users (consider protected).

Implemented in gnash::media::FLVParser, gnash::media::gst::MediaParserGst, gnash::media::ffmpeg::MediaParserFfmpeg, and gnash::media::haiku::MediaParserHaiku.

Referenced by getId3Info(), nextAudioFrame(), nextAudioFrameTimestamp(), nextFrameTimestamp(), nextVideoFrame(), and parserLoop().

§ parserLoop()

void gnash::media::MediaParser::parserLoop ( )
protected

The parser loop runs in a separate thread and calls parseNextChunk until killed.

parseNextChunk is expected to push encoded frames on the queue, which may trigger the thread to be put to sleep when queues are full or parsing was completed.

References _qMutex, gnash::gnashSleep(), parseNextChunk(), parserThreadKillRequested(), and waitIfNeeded().

Referenced by startParserThread().

§ parserThreadKillRequested()

bool gnash::media::MediaParser::parserThreadKillRequested ( ) const
inlineprotected

Referenced by parserLoop(), and waitIfNeeded().

§ parsingCompleted()

bool gnash::media::MediaParser::parsingCompleted ( ) const
inline

Return true of parsing is completed.

If this function returns true, any call to nextVideoFrame() or nextAudioFrame() will always return NULL

TODO: make thread-safe

Referenced by getId3Info(), nextAudioFrame(), nextAudioFrameTimestamp(), nextFrameTimestamp(), nextVideoFrame(), and waitIfNeeded().

§ pushEncodedAudioFrame()

void gnash::media::MediaParser::pushEncodedAudioFrame ( std::unique_ptr< EncodedAudioFrame frame)
protected

Push an encoded audio frame to buffer.

Will wait on a condition if buffer is full or parsing was completed

References _qMutex, gnash::key::e, gnash::key::i, gnash::log_debug(), and waitIfNeeded().

Referenced by gnash::media::gst::MediaParserGst::getBytesLoaded(), gnash::media::FLVParser::parseNextChunk(), and gnash::media::ffmpeg::MediaParserFfmpeg::seek().

§ pushEncodedVideoFrame()

void gnash::media::MediaParser::pushEncodedVideoFrame ( std::unique_ptr< EncodedVideoFrame frame)
protected

Push an encoded video frame to buffer.

Will wait on a condition if buffer is full or parsing was completed

References _qMutex, gnash::key::e, gnash::key::i, gnash::log_debug(), and waitIfNeeded().

Referenced by gnash::media::gst::MediaParserGst::getBytesLoaded(), gnash::media::FLVParser::parseNextChunk(), and gnash::media::ffmpeg::MediaParserFfmpeg::seek().

§ seek()

virtual bool gnash::media::MediaParser::seek ( std::uint32_t &  time)
pure virtual

Seeks to the closest possible position the given position, and returns the new position.

Parameters
timeinput/output parameter, input requests a time, output return the actual time seeked to.
Returns
true if the seek was valid, false otherwise.

Implemented in gnash::media::FLVParser, gnash::media::gst::MediaParserGst, gnash::media::ffmpeg::MediaParserFfmpeg, and gnash::media::haiku::MediaParserHaiku.

§ setBufferTime()

DSOEXPORT void gnash::media::MediaParser::setBufferTime ( std::uint_fast64_t  t)
inline

Set the time we want the parser thread to maintain in the buffer.

Parameters
tNumber of milliseconds to keep in the buffers.

References DSOEXPORT, gnash::key::t, and ts.

§ startParserThread()

void gnash::media::MediaParser::startParserThread ( )
protected

§ stopParserThread()

void gnash::media::MediaParser::stopParserThread ( )
protected

Stop the parser thread.

This method should be always called by destructors of subclasses to ensure the parser thread won't attempt to access destroyed structures.

References _parserThread.

Referenced by gnash::media::FLVParser::~FLVParser(), ~MediaParser(), gnash::media::ffmpeg::MediaParserFfmpeg::~MediaParserFfmpeg(), and gnash::media::gst::MediaParserGst::~MediaParserGst().

§ waitIfNeeded()

void gnash::media::MediaParser::waitIfNeeded ( std::unique_lock< std::mutex > &  qMutexLock)
protected

Wait on the _parserThreadWakeup condition if buffer is full or parsing was completed.

Callers must pass a locked lock on _qMutex

References _parserThreadWakeup, bufferFull(), indexingCompleted(), gnash::log_debug(), parserThreadKillRequested(), and parsingCompleted().

Referenced by parserLoop(), pushEncodedAudioFrame(), and pushEncodedVideoFrame().

§ wakeupParserThread()

void gnash::media::MediaParser::wakeupParserThread ( )
protected

Member Data Documentation

§ _audioInfo

std::unique_ptr<AudioInfo> gnash::media::MediaParser::_audioInfo
protected

§ _bufferTime

std::atomic<std::uint_fast64_t> gnash::media::MediaParser::_bufferTime
protected

§ _bytesLoaded

std::atomic<std::uint_fast64_t> gnash::media::MediaParser::_bytesLoaded
protected

§ _parserThread

std::thread gnash::media::MediaParser::_parserThread
protected

§ _parserThreadKillRequested

std::atomic<bool> gnash::media::MediaParser::_parserThreadKillRequested
protected

§ _parserThreadWakeup

std::condition_variable gnash::media::MediaParser::_parserThreadWakeup
protected

§ _parsingComplete

bool gnash::media::MediaParser::_parsingComplete
protected

§ _qMutex

std::mutex gnash::media::MediaParser::_qMutex
mutableprotected

§ _seekRequest

bool gnash::media::MediaParser::_seekRequest
protected

On seek, this flag will be set, while holding a lock on _streamMutex. The parser, when obtained a lock on _streamMutex, will check this flag, if found to be true will clear the buffers and reset to false.

Referenced by gnash::media::FLVParser::parseNextChunk(), and gnash::media::FLVParser::seek().

§ _stream

std::unique_ptr<IOChannel> gnash::media::MediaParser::_stream
protected

§ _streamMutex

std::mutex gnash::media::MediaParser::_streamMutex
mutableprotected

§ _videoInfo

std::unique_ptr<VideoInfo> gnash::media::MediaParser::_videoInfo
protected

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