Gnash  0.8.11dev
MediaParser.h
Go to the documentation of this file.
1 // MediaParser.h: Base class for media parsers
2 //
3 // Copyright (C) 2007, 2008, 2009, 2010, 2011, 2012
4 // Free Software Foundation, Inc.
5 //
6 // This program is free software; you can redistribute it and/or modify
7 // it under the terms of the GNU General Public License as published by
8 // the Free Software Foundation; either version 3 of the License, or
9 // (at your option) any later version.
10 //
11 // This program is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
15 //
16 // You should have received a copy of the GNU General Public License
17 // along with this program; if not, write to the Free Software
18 // Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19 
20 #ifndef GNASH_MEDIAPARSER_H
21 #define GNASH_MEDIAPARSER_H
22 
23 #include <atomic>
24 #include <thread>
25 #include <mutex>
26 #include <condition_variable>
27 #include <memory>
28 #include <deque>
29 #include <map>
30 #include <vector>
31 #include <iosfwd> // for output operator forward declarations
32 #include <boost/optional.hpp>
33 
34 #include "IOChannel.h" // for inlines
35 #include "dsodefs.h" // DSOEXPORT
36 
37 // Undefine this to load/parse media files in main thread
38 #define LOAD_MEDIA_IN_A_SEPARATE_THREAD 1
39 
40 namespace gnash {
41  class SimpleBuffer;
42  namespace media {
43  struct Id3Info;
44  }
45 }
46 
47 namespace gnash {
48 namespace media {
49 
50 
53 {
55  KEY_FRAME = 1,
56 
59 
62 };
63 
66 {
69 
72 };
73 
76 {
79 
82 
85 
88 
91 
94 
97 
98  // NOTE: if you add more elements here remember to
99  // also add them to the output operator!
100 };
101 
102 DSOEXPORT std::ostream& operator<< (std::ostream& os, const videoCodecType& t);
103 
105 //
123 {
125  //
136 
138  //
149 
151  //
165 
168 
170  //
175 
177  //
182 
185 
188 
189  // NOTE: if you add more elements here remember to
190  // also add them to the output operator!
191 };
192 
193 DSOEXPORT std::ostream& operator<< (std::ostream& os, const audioCodecType& t);
194 
196 //
203 {
204 
205 public:
206 
208  //
233  AudioInfo(int codeci, std::uint16_t sampleRatei,
234  std::uint16_t sampleSizei, bool stereoi,
235  std::uint64_t durationi, codecType typei)
236  :
237  codec(codeci),
238  sampleRate(sampleRatei),
239  sampleSize(sampleSizei),
240  stereo(stereoi),
241  duration(durationi),
242  type(typei)
243  {
244  }
245 
247  //
252  int codec;
253 
254  std::uint16_t sampleRate;
255 
257  std::uint16_t sampleSize;
258 
259  bool stereo;
260 
261  std::uint64_t duration;
262 
264 
266  //
270  class ExtraInfo {
271  public:
272  virtual ~ExtraInfo() {}
273  };
274 
276  //
279  std::unique_ptr<ExtraInfo> extra;
280 };
281 
283 //
289 {
290 public:
291 
293  //
320  VideoInfo(int codeci, std::uint16_t widthi, std::uint16_t heighti,
321  std::uint16_t frameRatei, std::uint64_t durationi,
322  codecType typei)
323  :
324  codec(codeci),
325  width(widthi),
326  height(heighti),
327  frameRate(frameRatei),
328  duration(durationi),
329  type(typei)
330  {
331  }
332 
333  int codec;
334  std::uint16_t width;
335  std::uint16_t height;
336  std::uint16_t frameRate;
337  std::uint64_t duration;
339 
341  //
345  class ExtraInfo {
346  public:
347  virtual ~ExtraInfo() {}
348  };
349 
351  //
354  std::unique_ptr<ExtraInfo> extra;
355 };
356 
357 DSOEXPORT std::ostream& operator << (std::ostream& os, const VideoInfo& vi);
358 
359 
361 
362 public:
363  virtual ~EncodedExtraData() {}
364 
365 };
366 
369 {
370 public:
371 
373  //
386  EncodedVideoFrame(std::uint8_t* data, std::uint32_t size,
387  unsigned int frameNum,
388  std::uint64_t timestamp=0)
389  :
390  _size(size),
391  _data(data),
392  _frameNum(frameNum),
393  _timestamp(timestamp)
394  {}
395 
397  const std::uint8_t* data() const { return _data.get(); }
398 
400  std::uint32_t dataSize() const { return _size; }
401 
403  std::uint64_t timestamp() const { return _timestamp; }
404 
406  unsigned frameNum() const { return _frameNum; }
407 
408  // FIXME: should have better encapsulation for this sort of stuff.
409  std::unique_ptr<EncodedExtraData> extradata;
410 private:
411 
412  std::uint32_t _size;
413  std::unique_ptr<std::uint8_t[]> _data;
414  unsigned int _frameNum;
415  std::uint64_t _timestamp;
416 };
417 
420 {
421 public:
422  std::uint32_t dataSize;
423  std::unique_ptr<std::uint8_t[]> data;
424  std::uint64_t timestamp;
425 
426  // FIXME: should have better encapsulation for this sort of stuff.
427  std::unique_ptr<EncodedExtraData> extradata;
428 };
429 
431 //
439 {
440 public:
441 
443  //
445  typedef std::multimap<std::uint64_t, std::shared_ptr<SimpleBuffer> >
447 
448  typedef std::vector<MetaTags::mapped_type> OrderedMetaTags;
449 
450  MediaParser(std::unique_ptr<IOChannel> stream);
451 
452  // Classes with virtual methods (virtual classes)
453  // must have a virtual destructor, or the destructors
454  // of subclasses will never be invoked, tipically resulting
455  // in memory leaks..
456  //
457  virtual ~MediaParser();
458 
462  //
469  virtual bool seek(std::uint32_t& time)=0;
470 
472  //
480  DSOEXPORT std::uint64_t getBufferLength() const;
481 
483  //
485  DSOEXPORT bool isBufferEmpty() const;
486 
488  DSOEXPORT std::uint_fast64_t getBufferTime() const
489  {
490  return _bufferTime.load();
491  }
492 
494  //
498  DSOEXPORT void setBufferTime(std::uint_fast64_t t)
499  {
500  _bufferTime=t;
501  }
502 
504  //
510  DSOEXPORT bool nextFrameTimestamp(std::uint64_t& ts) const;
511 
513  //
519  DSOEXPORT bool nextVideoFrameTimestamp(std::uint64_t& ts) const;
520 
522  //
528  DSOEXPORT std::unique_ptr<EncodedVideoFrame> nextVideoFrame();
529 
531  //
537  DSOEXPORT bool nextAudioFrameTimestamp(std::uint64_t& ts) const;
538 
540  //
546  DSOEXPORT std::unique_ptr<EncodedAudioFrame> nextAudioFrame();
547 
549  //
553  VideoInfo* getVideoInfo() { return _videoInfo.get(); }
554 
556  //
560  AudioInfo* getAudioInfo() { return _audioInfo.get(); }
561 
563  //
569  bool parsingCompleted() const { return _parsingComplete; }
570 
572  //
579  virtual bool indexingCompleted() const { return true; }
580 
582  virtual std::uint64_t getBytesLoaded() const { return 0; }
583 
585  std::uint64_t getBytesTotal() const
586  {
587  return _stream->size();
588  }
589 
591  //
599  virtual bool parseNextChunk()=0;
600 
602  //
607  //
610  virtual void fetchMetaTags(OrderedMetaTags& tags, std::uint64_t ts);
611 
613  //
615  virtual boost::optional<Id3Info> getId3Info() const;
616 
617 protected:
618 
620 
622  std::unique_ptr<VideoInfo> _videoInfo;
623 
625  std::unique_ptr<AudioInfo> _audioInfo;
626 
629 
631  std::atomic<std::uint_fast64_t> _bytesLoaded;
632 
634 
636  void startParserThread();
637 
639  //
645  void stopParserThread();
646 
648  void clearBuffers();
649 
651  //
654  void pushEncodedAudioFrame(std::unique_ptr<EncodedAudioFrame> frame);
655 
657  //
660  void pushEncodedVideoFrame(std::unique_ptr<EncodedVideoFrame> frame);
661 
663  std::unique_ptr<IOChannel> _stream;
664  mutable std::mutex _streamMutex;
665 
674  void parserLoop();
675 
677  {
678  return _parserThreadKillRequested.load();
679  }
680 
681  std::atomic<std::uint_fast64_t> _bufferTime;
682 
683  std::thread _parserThread;
684  std::atomic<bool> _parserThreadKillRequested;
685  std::condition_variable _parserThreadWakeup;
686 
692  void waitIfNeeded(std::unique_lock<std::mutex>& qMutexLock);
693 
694  void wakeupParserThread();
695 
697  mutable std::mutex _qMutex;
698 
700  //
706  bool bufferFull() const;
707 
712 
713 private:
714 
715  typedef std::deque<std::unique_ptr<EncodedVideoFrame>> VideoFrames;
716  typedef std::deque<std::unique_ptr<EncodedAudioFrame>> AudioFrames;
717 
719  //
724  const EncodedVideoFrame* peekNextVideoFrame() const;
725 
727  //
732  const EncodedAudioFrame* peekNextAudioFrame() const;
733 
734 
736  //
739  VideoFrames _videoFrames;
740 
742  //
745  AudioFrames _audioFrames;
746 
747  void requestParserThreadKill()
748  {
749  _parserThreadKillRequested=true;
750  _parserThreadWakeup.notify_all();
751  }
752 
754  std::uint64_t audioBufferLength() const;
755 
757  std::uint64_t videoBufferLength() const;
758 
760  std::uint64_t getBufferLengthNoLock() const;
761 
762 };
763 
764 
765 } // gnash.media namespace
766 } // namespace gnash
767 
768 #endif // __MEDIAPARSER_H__
bool parsingCompleted() const
Return true of parsing is completed.
Definition: MediaParser.h:569
Extra info about a video stream.
Definition: MediaParser.h:345
std::unique_ptr< AudioInfo > _audioInfo
Info about the audio stream (if any)
Definition: MediaParser.h:625
MP3 format.
Definition: MediaParser.h:164
The internal flash codec ids.
Definition: MediaParser.h:68
Signed Linear PCM, unspecified byte order.
Definition: MediaParser.h:135
virtual ~ExtraInfo()
Definition: MediaParser.h:272
An encoded audio frame.
Definition: MediaParser.h:419
int codec
Definition: MediaParser.h:333
std::uint64_t getBytesTotal() const
Return total number of bytes in input.
Definition: MediaParser.h:585
Proprietary simple format.
Definition: MediaParser.h:181
std::thread _parserThread
Definition: MediaParser.h:683
ADPCM format.
Definition: MediaParser.h:148
Disposable interlaced frames.
Definition: MediaParser.h:61
std::uint32_t ts
Definition: LocalConnection_as.cpp:150
std::mutex _qMutex
mutex protecting access to the a/v encoded frames queues
Definition: MediaParser.h:697
codecType type
Definition: MediaParser.h:263
Information about a video stream.
Definition: MediaParser.h:288
virtual bool indexingCompleted() const
Return true of indexing is completed.
Definition: MediaParser.h:579
bool _parsingComplete
Whether the parsing is complete or not.
Definition: MediaParser.h:628
std::atomic< bool > _parserThreadKillRequested
Definition: MediaParser.h:684
std::unique_ptr< EncodedExtraData > extradata
Definition: MediaParser.h:409
AudioInfo * getAudioInfo()
Returns a AudioInfo class about the audiostream.
Definition: MediaParser.h:560
std::uint16_t width
Definition: MediaParser.h:334
std::uint32_t dataSize() const
Return size of data buffer.
Definition: MediaParser.h:400
SimpleBuffer data
Definition: LocalConnection_as.cpp:151
Anonymous namespace for callbacks, local functions, event handlers etc.
Definition: dbus_ext.cpp:40
unsigned frameNum() const
Return video frame number.
Definition: MediaParser.h:406
An encoded video frame.
Definition: MediaParser.h:368
type
Definition: GnashKey.h:329
Linear PCM, strictly little-endian.
Definition: MediaParser.h:167
std::uint64_t timestamp() const
Return video frame presentation timestamp.
Definition: MediaParser.h:403
std::unique_ptr< EncodedExtraData > extradata
Definition: MediaParser.h:427
Extra info about an audio stream.
Definition: MediaParser.h:270
virtual ~EncodedExtraData()
Definition: MediaParser.h:363
Custom codecs ids.
Definition: MediaParser.h:71
Definition: klash_part.cpp:329
std::uint16_t frameRate
Definition: MediaParser.h:336
codecType
The type of the codec id passed in the AudioInfo or VideoInfo class.
Definition: MediaParser.h:65
std::atomic< std::uint_fast64_t > _bufferTime
Definition: MediaParser.h:681
DSOEXPORT void setBufferTime(std::uint_fast64_t t)
Set the time we want the parser thread to maintain in the buffer.
Definition: MediaParser.h:498
std::uint16_t sampleSize
Size of each sample, in bytes.
Definition: MediaParser.h:257
std::uint16_t height
Definition: MediaParser.h:335
std::atomic< std::uint_fast64_t > _bytesLoaded
Number of bytes loaded.
Definition: MediaParser.h:631
Definition: GnashKey.h:166
std::uint64_t duration
Definition: MediaParser.h:261
The MediaParser class provides cursor-based access to encoded media frames.
Definition: MediaParser.h:438
EncodedVideoFrame(std::uint8_t *data, std::uint32_t size, unsigned int frameNum, std::uint64_t timestamp=0)
Create an encoded video frame.
Definition: MediaParser.h:386
Definition: klash_part.cpp:329
Screenvideo codec.
Definition: MediaParser.h:84
On2 VP6 Alpha video codec.
Definition: MediaParser.h:90
std::unique_ptr< VideoInfo > _videoInfo
Subclasses must set the following variables:
Definition: MediaParser.h:622
videoFrameType
Video frame types.
Definition: MediaParser.h:52
Key frames.
Definition: MediaParser.h:55
std::multimap< std::uint64_t, std::shared_ptr< SimpleBuffer > > MetaTags
A container for executable MetaTags contained in media streams.
Definition: MediaParser.h:446
H263/SVQ3 video codec.
Definition: MediaParser.h:81
virtual ~ExtraInfo()
Definition: MediaParser.h:347
std::uint32_t dataSize
Definition: MediaParser.h:422
std::unique_ptr< IOChannel > _stream
The stream used to access the file.
Definition: MediaParser.h:663
Always 16kHz mono.
Definition: MediaParser.h:187
std::ostream & operator<<(std::ostream &os, const VideoInfo &vi)
Definition: MediaParser.cpp:432
std::unique_ptr< ExtraInfo > extra
Extra info about video stream, if when needed.
Definition: MediaParser.h:354
videoCodecType
Video codec ids as defined in flash.
Definition: MediaParser.h:75
std::uint64_t duration
Definition: MediaParser.h:337
#define DSOEXPORT
Definition: dsodefs.h:55
AudioInfo(int codeci, std::uint16_t sampleRatei, std::uint16_t sampleSizei, bool stereoi, std::uint64_t durationi, codecType typei)
Construct an AudioInfo object.
Definition: MediaParser.h:233
std::vector< MetaTags::mapped_type > OrderedMetaTags
Definition: MediaParser.h:448
VideoInfo(int codeci, std::uint16_t widthi, std::uint16_t heighti, std::uint16_t frameRatei, std::uint64_t durationi, codecType typei)
Construct a VideoInfo object.
Definition: MediaParser.h:320
int codec
Codec identifier.
Definition: MediaParser.h:252
Information about an audio stream.
Definition: MediaParser.h:202
bool _seekRequest
Definition: MediaParser.h:711
bool parserThreadKillRequested() const
Definition: MediaParser.h:676
std::unique_ptr< ExtraInfo > extra
Extra info about audio stream, if when needed.
Definition: MediaParser.h:279
std::mutex _streamMutex
Definition: MediaParser.h:664
On2 VP6 video codec.
Definition: MediaParser.h:87
std::uint64_t timestamp
Definition: MediaParser.h:424
const std::uint8_t * data() const
Return pointer to actual data. Ownership retained by this class.
Definition: MediaParser.h:397
Proprietary simple format. Always 5Khz mono ?
Definition: MediaParser.h:174
Screenvideo2 codec.
Definition: MediaParser.h:93
audioCodecType
Audio codec ids as defined in flash.
Definition: MediaParser.h:122
std::unique_ptr< std::uint8_t[]> data
Definition: MediaParser.h:423
std::uint16_t sampleRate
Definition: MediaParser.h:254
Definition: MediaParser.h:360
Advanced Audio Coding.
Definition: MediaParser.h:184
No video codec.
Definition: MediaParser.h:78
virtual std::uint64_t getBytesLoaded() const
Return number of bytes parsed so far.
Definition: MediaParser.h:582
MPEG-4 Part 10, or Advanced Video Coding.
Definition: MediaParser.h:96
codecType type
Definition: MediaParser.h:338
std::condition_variable _parserThreadWakeup
Definition: MediaParser.h:685
VideoInfo * getVideoInfo()
Returns a VideoInfo class about the videostream.
Definition: MediaParser.h:553
DSOEXPORT std::uint_fast64_t getBufferTime() const
Return the time we want the parser thread to maintain in the buffer.
Definition: MediaParser.h:488
bool stereo
Definition: MediaParser.h:259
Interlaced frames.
Definition: MediaParser.h:58