Gnash  0.8.11dev
FLVParser.h
Go to the documentation of this file.
1 // FLVParser.h: Flash Video file format parser, for Gnash.
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 
21 
22 // Information about the FLV format can be found at http://osflash.org/flv
23 
24 #ifndef GNASH_FLVPARSER_H
25 #define GNASH_FLVPARSER_H
26 
27 #include "dsodefs.h"
28 #include "MediaParser.h" // for inheritance
29 
30 #include <memory>
31 #include <map>
32 
33 #include <boost/thread/mutex.hpp>
34 
35 namespace gnash {
36 namespace media {
37 
39 //
43 {
44 public:
45 
47  //
56  ExtraVideoInfoFlv(boost::uint8_t* extradata, size_t datasize)
57  :
58  data(extradata),
59  size(datasize)
60  {
61  }
62 
64  boost::scoped_array<boost::uint8_t> data;
65 
67  size_t size;
68 };
69 
71 //
75 {
76 public:
77 
79  //
88  ExtraAudioInfoFlv(boost::uint8_t* extradata, size_t datasize)
89  :
90  data(extradata),
91  size(datasize)
92  {
93  }
94 
96  boost::scoped_array<boost::uint8_t> data;
97 
99  size_t size;
100 };
101 
104 {
105 
106 public:
107 
109  //
113  static const size_t paddingBytes = 8;
114 
118  //
123  FLVParser(std::auto_ptr<IOChannel> lt);
124 
126  ~FLVParser();
127 
128  // see dox in MediaParser.h
129  virtual bool seek(boost::uint32_t&);
130 
131  // see dox in MediaParser.h
132  virtual bool parseNextChunk();
133 
134  // see dox in MediaParser.h
135  boost::uint64_t getBytesLoaded() const;
136 
137  // see dox in MediaParser.h
138  bool indexingCompleted() const
139  {
140  return _indexingCompleted;
141  }
142 
144  //
149  //
154  //
155  virtual void fetchMetaTags(OrderedMetaTags& tags, boost::uint64_t ts);
156 
157 private:
158 
159  enum tagType
160  {
161  FLV_AUDIO_TAG = 0x08,
162  FLV_VIDEO_TAG = 0x09,
163  FLV_META_TAG = 0x12
164  };
165 
166  struct FLVTag : public boost::noncopyable
167  {
168  FLVTag(boost::uint8_t* stream)
169  :
170  type(stream[0]),
171  body_size(getUInt24(stream+1)),
172  timestamp(getUInt24(stream+4) | (stream[7] << 24) )
173  {}
174 
176  boost::uint8_t type;
177  boost::uint32_t body_size;
178  boost::uint32_t timestamp;
179  };
180 
181  struct FLVAudioTag : public boost::noncopyable
182  {
183  FLVAudioTag(const boost::uint8_t& byte)
184  :
185  codec( (byte & 0xf0) >> 4 ),
186  samplerate( flv_audio_rates[(byte & 0x0C) >> 2] ),
187  samplesize( 1 + ((byte & 0x02) >> 1)),
188  stereo( (byte & 0x01) )
189  {
190  }
191 
193  boost::uint8_t codec;
194 
195  boost::uint16_t samplerate;
196 
198  boost::uint8_t samplesize;
199 
200  bool stereo;
201 
202  private:
203 
204  static const boost::uint16_t flv_audio_rates[];
205 
206  };
207 
208  enum frameType
209  {
210  FLV_VIDEO_KEYFRAME = 1,
211  FLV_VIDEO_INTERLACED = 2,
212  FLV_VIDEO_DISPOSABLE = 3
213  };
214 
215  struct FLVVideoTag : public boost::noncopyable
216  {
217  FLVVideoTag(const boost::uint8_t& byte)
218  :
219  frametype( (byte & 0xf0) >> 4 ),
220  codec( byte & 0x0f )
221  {}
222 
224  boost::uint8_t frametype;
226  boost::uint8_t codec;
227  };
228 
230  //
234  bool parseNextTag(bool index_only);
235 
236  std::auto_ptr<EncodedAudioFrame> parseAudioTag(const FLVTag& flvtag,
237  const FLVAudioTag& audiotag, boost::uint32_t thisTagPos);
238 
239  std::auto_ptr<EncodedVideoFrame> parseVideoTag(const FLVTag& flvtag,
240  const FLVVideoTag& videotag, boost::uint32_t thisTagPos);
241 
242  void indexAudioTag(const FLVTag& tag, boost::uint32_t thisTagPos);
243 
244  void indexVideoTag(const FLVTag& tag, const FLVVideoTag& videotag,
245  boost::uint32_t thisTagPos);
246 
248  bool parseHeader();
249 
253  static boost::uint32_t getUInt24(boost::uint8_t* in);
254 
257  boost::uint64_t _lastParsedPosition;
258 
260  boost::uint64_t _nextPosToIndex;
261 
263  //
267  size_t _nextAudioFrame;
268 
270  //
274  size_t _nextVideoFrame;
275 
277  bool _audio;
278 
280  bool _video;
281 
282  std::auto_ptr<EncodedAudioFrame>
283  readAudioFrame(boost::uint32_t dataSize, boost::uint32_t timestamp);
284 
285  std::auto_ptr<EncodedVideoFrame>
286  readVideoFrame(boost::uint32_t dataSize, boost::uint32_t timestamp);
287 
291  typedef std::map<boost::uint64_t, long> CuePointsMap;
292  CuePointsMap _cuePoints;
293 
294  bool _indexingCompleted;
295 
296  MetaTags _metaTags;
297 
298  boost::mutex _metaTagsMutex;
299 };
300 
301 } // end of gnash::media namespace
302 } // end of gnash namespace
303 
304 #endif
The FLVParser class parses FLV streams.
Definition: FLVParser.h:103
boost::uint32_t ts
Definition: LocalConnection_as.cpp:152
Extra info about a video stream.
Definition: MediaParser.h:346
boost::scoped_array< boost::uint8_t > data
Audio stream header.
Definition: FLVParser.h:96
ExtraVideoInfoFlv(boost::uint8_t *extradata, size_t datasize)
Construct an ExtraVideoInfoFlv.
Definition: FLVParser.h:56
bool indexingCompleted() const
Return true of indexing is completed.
Definition: FLVParser.h:138
Anonymous namespace for callbacks, local functions, event handlers etc.
Definition: dbus_ext.cpp:40
type
Definition: GnashKey.h:329
Extra info about an audio stream.
Definition: MediaParser.h:271
Extra audoi info found in some FLV embedded streams.
Definition: FLVParser.h:74
size_t size
Video stream header size.
Definition: FLVParser.h:67
The MediaParser class provides cursor-based access to encoded media frames.
Definition: MediaParser.h:439
boost::scoped_array< boost::uint8_t > data
Video stream header.
Definition: FLVParser.h:64
#define DSOEXPORT
Definition: dsodefs.h:55
size_t size
Audio stream header size.
Definition: FLVParser.h:99
ExtraAudioInfoFlv(boost::uint8_t *extradata, size_t datasize)
Construct an ExtraAudioInfoFlv.
Definition: FLVParser.h:88
Extra video info found in some FLV embedded streams.
Definition: FLVParser.h:42