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 <memory>
28 #include <map>
29 #include <mutex>
30 
31 #include <boost/utility.hpp> // noncopyable
32 
33 #include "dsodefs.h"
34 #include "MediaParser.h" // for inheritance
35 
36 namespace gnash {
37 namespace media {
38 
40 //
44 {
45 public:
46 
48  //
57  ExtraVideoInfoFlv(std::uint8_t* extradata, size_t datasize)
58  :
59  data(extradata),
60  size(datasize)
61  {
62  }
63 
65  std::unique_ptr<std::uint8_t[]> data;
66 
68  size_t size;
69 };
70 
72 //
76 {
77 public:
78 
80  //
89  ExtraAudioInfoFlv(std::uint8_t* extradata, size_t datasize)
90  :
91  data(extradata),
92  size(datasize)
93  {
94  }
95 
97  std::unique_ptr<std::uint8_t[]> data;
98 
100  size_t size;
101 };
102 
105 {
106 
107 public:
108 
110  //
114  static const size_t paddingBytes = 8;
115 
119  //
124  FLVParser(std::unique_ptr<IOChannel> lt);
125 
127  ~FLVParser();
128 
129  // see dox in MediaParser.h
130  virtual bool seek(std::uint32_t&);
131 
132  // see dox in MediaParser.h
133  virtual bool parseNextChunk();
134 
135  // see dox in MediaParser.h
136  std::uint64_t getBytesLoaded() const;
137 
138  // see dox in MediaParser.h
139  bool indexingCompleted() const
140  {
141  return _indexingCompleted;
142  }
143 
145  //
150  //
155  //
156  virtual void fetchMetaTags(OrderedMetaTags& tags, std::uint64_t ts);
157 
158 private:
159 
160  enum tagType
161  {
162  FLV_AUDIO_TAG = 0x08,
163  FLV_VIDEO_TAG = 0x09,
164  FLV_META_TAG = 0x12
165  };
166 
167  struct FLVTag : public boost::noncopyable
168  {
169  FLVTag(std::uint8_t* stream)
170  :
171  type(stream[0]),
172  body_size(getUInt24(stream+1)),
173  timestamp(getUInt24(stream+4) | (stream[7] << 24) )
174  {}
175 
177  std::uint8_t type;
178  std::uint32_t body_size;
179  std::uint32_t timestamp;
180  };
181 
182  struct FLVAudioTag : public boost::noncopyable
183  {
184  FLVAudioTag(const std::uint8_t& byte)
185  :
186  codec( (byte & 0xf0) >> 4 ),
187  samplerate( flv_audio_rates[(byte & 0x0C) >> 2] ),
188  samplesize( 1 + ((byte & 0x02) >> 1)),
189  stereo( (byte & 0x01) )
190  {
191  }
192 
194  std::uint8_t codec;
195 
196  std::uint16_t samplerate;
197 
199  std::uint8_t samplesize;
200 
201  bool stereo;
202 
203  private:
204 
205  static const std::uint16_t flv_audio_rates[];
206 
207  };
208 
209  enum frameType
210  {
211  FLV_VIDEO_KEYFRAME = 1,
212  FLV_VIDEO_INTERLACED = 2,
213  FLV_VIDEO_DISPOSABLE = 3
214  };
215 
216  struct FLVVideoTag : public boost::noncopyable
217  {
218  FLVVideoTag(const std::uint8_t& byte)
219  :
220  frametype( (byte & 0xf0) >> 4 ),
221  codec( byte & 0x0f )
222  {}
223 
225  std::uint8_t frametype;
227  std::uint8_t codec;
228  };
229 
231  //
235  bool parseNextTag(bool index_only);
236 
237  std::unique_ptr<EncodedAudioFrame> parseAudioTag(const FLVTag& flvtag,
238  const FLVAudioTag& audiotag, std::uint32_t thisTagPos);
239 
240  std::unique_ptr<EncodedVideoFrame> parseVideoTag(const FLVTag& flvtag,
241  const FLVVideoTag& videotag, std::uint32_t thisTagPos);
242 
243  void indexAudioTag(const FLVTag& tag, std::uint32_t thisTagPos);
244 
245  void indexVideoTag(const FLVTag& tag, const FLVVideoTag& videotag,
246  std::uint32_t thisTagPos);
247 
249  bool parseHeader();
250 
254  static std::uint32_t getUInt24(std::uint8_t* in);
255 
258  std::uint64_t _lastParsedPosition;
259 
261  std::uint64_t _nextPosToIndex;
262 
264  bool _audio;
265 
267  bool _video;
268 
269  std::unique_ptr<EncodedAudioFrame>
270  readAudioFrame(std::uint32_t dataSize, std::uint32_t timestamp);
271 
272  std::unique_ptr<EncodedVideoFrame>
273  readVideoFrame(std::uint32_t dataSize, std::uint32_t timestamp);
274 
278  typedef std::map<std::uint64_t, long> CuePointsMap;
279  CuePointsMap _cuePoints;
280 
281  bool _indexingCompleted;
282 
283  MetaTags _metaTags;
284 
285  std::mutex _metaTagsMutex;
286 };
287 
288 } // end of gnash::media namespace
289 } // end of gnash namespace
290 
291 #endif
bool indexingCompleted() const
Return true of indexing is completed.
Definition: FLVParser.h:139
The FLVParser class parses FLV streams.
Definition: FLVParser.h:104
Extra info about a video stream.
Definition: MediaParser.h:379
std::unique_ptr< std::uint8_t[]> data
Audio stream header.
Definition: FLVParser.h:97
std::uint32_t ts
Definition: LocalConnection_as.cpp:150
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:304
Extra audoi info found in some FLV embedded streams.
Definition: FLVParser.h:75
ExtraVideoInfoFlv(std::uint8_t *extradata, size_t datasize)
Construct an ExtraVideoInfoFlv.
Definition: FLVParser.h:57
std::unique_ptr< std::uint8_t[]> data
Video stream header.
Definition: FLVParser.h:65
size_t size
Video stream header size.
Definition: FLVParser.h:68
The MediaParser class provides cursor-based access to encoded media frames.
Definition: MediaParser.h:472
#define DSOEXPORT
Definition: dsodefs.h:55
std::vector< MetaTags::mapped_type > OrderedMetaTags
Definition: MediaParser.h:482
size_t size
Audio stream header size.
Definition: FLVParser.h:100
Extra video info found in some FLV embedded streams.
Definition: FLVParser.h:43
ExtraAudioInfoFlv(std::uint8_t *extradata, size_t datasize)
Construct an ExtraAudioInfoFlv.
Definition: FLVParser.h:89