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, 2013, 2014, 2015, 2016
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 <iostream> // for output operator
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 inline std::ostream&
194 operator<< (std::ostream& os, const audioCodecType& t)
195 {
196  switch (t)
197  {
198  case AUDIO_CODEC_RAW:
199  os << "Raw";
200  break;
201  case AUDIO_CODEC_ADPCM:
202  os << "ADPCM";
203  break;
204  case AUDIO_CODEC_MP3:
205  os << "MP3";
206  break;
208  os << "Uncompressed";
209  break;
211  os << "Nellymoser 8Hz mono";
212  break;
214  os << "Nellymoser";
215  break;
216  case AUDIO_CODEC_AAC:
217  os << "Advanced Audio Coding";
218  break;
219  case AUDIO_CODEC_SPEEX:
220  os << "Speex";
221  break;
222  default:
223  os << "unknown/invalid codec " << static_cast<int>(t);
224  break;
225  }
226  return os;
227 }
228 
230 //
237 {
238 
239 public:
240 
242  //
267  AudioInfo(int codeci, std::uint16_t sampleRatei,
268  std::uint16_t sampleSizei, bool stereoi,
269  std::uint64_t durationi, codecType typei)
270  :
271  codec(codeci),
272  sampleRate(sampleRatei),
273  sampleSize(sampleSizei),
274  stereo(stereoi),
275  duration(durationi),
276  type(typei)
277  {
278  }
279 
281  //
286  int codec;
287 
288  std::uint16_t sampleRate;
289 
291  std::uint16_t sampleSize;
292 
293  bool stereo;
294 
295  std::uint64_t duration;
296 
298 
300  //
304  class ExtraInfo {
305  public:
306  virtual ~ExtraInfo() {}
307  };
308 
310  //
313  std::unique_ptr<ExtraInfo> extra;
314 };
315 
317 //
323 {
324 public:
325 
327  //
354  VideoInfo(int codeci, std::uint16_t widthi, std::uint16_t heighti,
355  std::uint16_t frameRatei, std::uint64_t durationi,
356  codecType typei)
357  :
358  codec(codeci),
359  width(widthi),
360  height(heighti),
361  frameRate(frameRatei),
362  duration(durationi),
363  type(typei)
364  {
365  }
366 
367  int codec;
368  std::uint16_t width;
369  std::uint16_t height;
370  std::uint16_t frameRate;
371  std::uint64_t duration;
373 
375  //
379  class ExtraInfo {
380  public:
381  virtual ~ExtraInfo() {}
382  };
383 
385  //
388  std::unique_ptr<ExtraInfo> extra;
389 };
390 
391 DSOEXPORT std::ostream& operator << (std::ostream& os, const VideoInfo& vi);
392 
393 
395 
396 public:
397  virtual ~EncodedExtraData() {}
398 
399 };
400 
403 {
404 public:
405 
407  //
420  EncodedVideoFrame(std::uint8_t* data, std::uint32_t size,
421  unsigned int frameNum,
422  std::uint64_t timestamp=0)
423  :
424  _size(size),
425  _data(data),
426  _frameNum(frameNum),
427  _timestamp(timestamp)
428  {}
429 
431  const std::uint8_t* data() const { return _data.get(); }
432 
434  std::uint32_t dataSize() const { return _size; }
435 
437  std::uint64_t timestamp() const { return _timestamp; }
438 
440  unsigned frameNum() const { return _frameNum; }
441 
442  // FIXME: should have better encapsulation for this sort of stuff.
443  std::unique_ptr<EncodedExtraData> extradata;
444 private:
445 
446  std::uint32_t _size;
447  std::unique_ptr<std::uint8_t[]> _data;
448  unsigned int _frameNum;
449  std::uint64_t _timestamp;
450 };
451 
454 {
455 public:
456  std::uint32_t dataSize;
457  std::unique_ptr<std::uint8_t[]> data;
458  std::uint64_t timestamp;
459 
460  // FIXME: should have better encapsulation for this sort of stuff.
461  std::unique_ptr<EncodedExtraData> extradata;
462 };
463 
465 //
473 {
474 public:
475 
477  //
479  typedef std::multimap<std::uint64_t, std::shared_ptr<SimpleBuffer> >
481 
482  typedef std::vector<MetaTags::mapped_type> OrderedMetaTags;
483 
484  MediaParser(std::unique_ptr<IOChannel> stream);
485 
486  // Classes with virtual methods (virtual classes)
487  // must have a virtual destructor, or the destructors
488  // of subclasses will never be invoked, tipically resulting
489  // in memory leaks..
490  //
491  virtual ~MediaParser();
492 
496  //
503  virtual bool seek(std::uint32_t& time)=0;
504 
506  //
514  DSOEXPORT std::uint64_t getBufferLength() const;
515 
517  //
519  DSOEXPORT bool isBufferEmpty() const;
520 
522  DSOEXPORT std::uint_fast64_t getBufferTime() const
523  {
524  return _bufferTime.load();
525  }
526 
528  //
532  DSOEXPORT void setBufferTime(std::uint_fast64_t t)
533  {
534  _bufferTime=t;
535  }
536 
538  //
544  DSOEXPORT bool nextFrameTimestamp(std::uint64_t& ts) const;
545 
547  //
553  DSOEXPORT bool nextVideoFrameTimestamp(std::uint64_t& ts) const;
554 
556  //
562  DSOEXPORT std::unique_ptr<EncodedVideoFrame> nextVideoFrame();
563 
565  //
571  DSOEXPORT bool nextAudioFrameTimestamp(std::uint64_t& ts) const;
572 
574  //
580  DSOEXPORT std::unique_ptr<EncodedAudioFrame> nextAudioFrame();
581 
583  //
587  VideoInfo* getVideoInfo() { return _videoInfo.get(); }
588 
590  //
594  AudioInfo* getAudioInfo() { return _audioInfo.get(); }
595 
597  //
603  bool parsingCompleted() const { return _parsingComplete; }
604 
606  //
613  virtual bool indexingCompleted() const { return true; }
614 
616  virtual std::uint64_t getBytesLoaded() const { return 0; }
617 
619  std::uint64_t getBytesTotal() const
620  {
621  return _stream->size();
622  }
623 
625  //
633  virtual bool parseNextChunk()=0;
634 
636  //
641  //
644  virtual void fetchMetaTags(OrderedMetaTags& tags, std::uint64_t ts);
645 
647  //
649  virtual boost::optional<Id3Info> getId3Info() const;
650 
651 protected:
652 
654 
656  std::unique_ptr<VideoInfo> _videoInfo;
657 
659  std::unique_ptr<AudioInfo> _audioInfo;
660 
663 
665  std::atomic<std::uint_fast64_t> _bytesLoaded;
666 
668 
670  void startParserThread();
671 
673  //
679  void stopParserThread();
680 
682  void clearBuffers();
683 
685  //
688  void pushEncodedAudioFrame(std::unique_ptr<EncodedAudioFrame> frame);
689 
691  //
694  void pushEncodedVideoFrame(std::unique_ptr<EncodedVideoFrame> frame);
695 
697  std::unique_ptr<IOChannel> _stream;
698  mutable std::mutex _streamMutex;
699 
708  void parserLoop();
709 
711  {
712  return _parserThreadKillRequested.load();
713  }
714 
715  std::atomic<std::uint_fast64_t> _bufferTime;
716 
717  std::thread _parserThread;
718  std::atomic<bool> _parserThreadKillRequested;
719  std::condition_variable _parserThreadWakeup;
720 
726  void waitIfNeeded(std::unique_lock<std::mutex>& qMutexLock);
727 
728  void wakeupParserThread();
729 
731  mutable std::mutex _qMutex;
732 
734  //
740  bool bufferFull() const;
741 
746 
747 private:
748 
749  typedef std::deque<std::unique_ptr<EncodedVideoFrame>> VideoFrames;
750  typedef std::deque<std::unique_ptr<EncodedAudioFrame>> AudioFrames;
751 
753  //
758  const EncodedVideoFrame* peekNextVideoFrame() const;
759 
761  //
766  const EncodedAudioFrame* peekNextAudioFrame() const;
767 
768 
770  //
773  VideoFrames _videoFrames;
774 
776  //
779  AudioFrames _audioFrames;
780 
781  void requestParserThreadKill()
782  {
783  _parserThreadKillRequested=true;
784  _parserThreadWakeup.notify_all();
785  }
786 
788  std::uint64_t audioBufferLength() const;
789 
791  std::uint64_t videoBufferLength() const;
792 
794  std::uint64_t getBufferLengthNoLock() const;
795 
796 };
797 
798 
799 } // gnash.media namespace
800 } // namespace gnash
801 
802 #endif // __MEDIAPARSER_H__
bool parsingCompleted() const
Return true of parsing is completed.
Definition: MediaParser.h:603
Extra info about a video stream.
Definition: MediaParser.h:379
std::unique_ptr< AudioInfo > _audioInfo
Info about the audio stream (if any)
Definition: MediaParser.h:659
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:306
An encoded audio frame.
Definition: MediaParser.h:453
int codec
Definition: MediaParser.h:367
std::uint64_t getBytesTotal() const
Return total number of bytes in input.
Definition: MediaParser.h:619
Proprietary simple format.
Definition: MediaParser.h:181
std::thread _parserThread
Definition: MediaParser.h:717
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:731
codecType type
Definition: MediaParser.h:297
Information about a video stream.
Definition: MediaParser.h:322
bool _parsingComplete
Whether the parsing is complete or not.
Definition: MediaParser.h:662
std::atomic< bool > _parserThreadKillRequested
Definition: MediaParser.h:718
std::unique_ptr< EncodedExtraData > extradata
Definition: MediaParser.h:443
const std::uint8_t * data() const
Return pointer to actual data. Ownership retained by this class.
Definition: MediaParser.h:431
AudioInfo * getAudioInfo()
Returns a AudioInfo class about the audiostream.
Definition: MediaParser.h:594
std::uint16_t width
Definition: MediaParser.h:368
SimpleBuffer data
Definition: LocalConnection_as.cpp:151
Anonymous namespace for callbacks, local functions, event handlers etc.
Definition: dbus_ext.cpp:40
An encoded video frame.
Definition: MediaParser.h:402
type
Definition: GnashKey.h:329
Linear PCM, strictly little-endian.
Definition: MediaParser.h:167
std::unique_ptr< EncodedExtraData > extradata
Definition: MediaParser.h:461
Extra info about an audio stream.
Definition: MediaParser.h:304
virtual ~EncodedExtraData()
Definition: MediaParser.h:397
Custom codecs ids.
Definition: MediaParser.h:71
Definition: klash_part.cpp:329
std::uint16_t frameRate
Definition: MediaParser.h:370
bool parserThreadKillRequested() const
Definition: MediaParser.h:710
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:715
DSOEXPORT void setBufferTime(std::uint_fast64_t t)
Set the time we want the parser thread to maintain in the buffer.
Definition: MediaParser.h:532
std::uint16_t sampleSize
Size of each sample, in bytes.
Definition: MediaParser.h:291
std::uint16_t height
Definition: MediaParser.h:369
std::atomic< std::uint_fast64_t > _bytesLoaded
Number of bytes loaded.
Definition: MediaParser.h:665
Definition: GnashKey.h:166
std::uint64_t duration
Definition: MediaParser.h:295
The MediaParser class provides cursor-based access to encoded media frames.
Definition: MediaParser.h:472
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:420
Definition: klash_part.cpp:329
Screenvideo codec.
Definition: MediaParser.h:84
On2 VP6 Alpha video codec.
Definition: MediaParser.h:90
std::uint64_t timestamp() const
Return video frame presentation timestamp.
Definition: MediaParser.h:437
std::unique_ptr< VideoInfo > _videoInfo
Subclasses must set the following variables:
Definition: MediaParser.h:656
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:480
H263/SVQ3 video codec.
Definition: MediaParser.h:81
virtual ~ExtraInfo()
Definition: MediaParser.h:381
std::uint32_t dataSize
Definition: MediaParser.h:456
std::unique_ptr< IOChannel > _stream
The stream used to access the file.
Definition: MediaParser.h:697
Always 16kHz mono.
Definition: MediaParser.h:187
std::ostream & operator<<(std::ostream &os, const VideoInfo &vi)
Definition: MediaParser.cpp:432
std::uint32_t dataSize() const
Return size of data buffer.
Definition: MediaParser.h:434
unsigned frameNum() const
Return video frame number.
Definition: MediaParser.h:440
std::unique_ptr< ExtraInfo > extra
Extra info about video stream, if when needed.
Definition: MediaParser.h:388
videoCodecType
Video codec ids as defined in flash.
Definition: MediaParser.h:75
std::uint64_t duration
Definition: MediaParser.h:371
#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:267
std::vector< MetaTags::mapped_type > OrderedMetaTags
Definition: MediaParser.h:482
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:354
virtual std::uint64_t getBytesLoaded() const
Return number of bytes parsed so far.
Definition: MediaParser.h:616
int codec
Codec identifier.
Definition: MediaParser.h:286
Information about an audio stream.
Definition: MediaParser.h:236
virtual bool indexingCompleted() const
Return true of indexing is completed.
Definition: MediaParser.h:613
bool _seekRequest
Definition: MediaParser.h:745
std::unique_ptr< ExtraInfo > extra
Extra info about audio stream, if when needed.
Definition: MediaParser.h:313
std::mutex _streamMutex
Definition: MediaParser.h:698
On2 VP6 video codec.
Definition: MediaParser.h:87
std::uint64_t timestamp
Definition: MediaParser.h:458
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:457
std::uint16_t sampleRate
Definition: MediaParser.h:288
Definition: MediaParser.h:394
Advanced Audio Coding.
Definition: MediaParser.h:184
No video codec.
Definition: MediaParser.h:78
MPEG-4 Part 10, or Advanced Video Coding.
Definition: MediaParser.h:96
codecType type
Definition: MediaParser.h:372
std::condition_variable _parserThreadWakeup
Definition: MediaParser.h:719
DSOEXPORT std::uint_fast64_t getBufferTime() const
Return the time we want the parser thread to maintain in the buffer.
Definition: MediaParser.h:522
VideoInfo * getVideoInfo()
Returns a VideoInfo class about the videostream.
Definition: MediaParser.h:587
bool stereo
Definition: MediaParser.h:293
Interlaced frames.
Definition: MediaParser.h:58