DASH Media Packaging SDK
 All Classes Namespaces Functions Variables Typedefs Enumerator
webm_cluster_parser.h
1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #ifndef MEDIA_FORMATS_WEBM_WEBM_CLUSTER_PARSER_H_
6 #define MEDIA_FORMATS_WEBM_WEBM_CLUSTER_PARSER_H_
7 
8 #include <deque>
9 #include <map>
10 #include <set>
11 #include <string>
12 
13 #include "packager/base/memory/scoped_ptr.h"
14 #include "packager/media/base/media_parser.h"
15 #include "packager/media/base/media_sample.h"
16 #include "packager/media/formats/webm/webm_parser.h"
17 #include "packager/media/formats/webm/webm_tracks_parser.h"
18 
19 namespace edash_packager {
20 namespace media {
21 
23  public:
26  enum {
29 
33  };
34 
39  static const uint16_t kOpusFrameDurationsMu[];
40 
41  private:
42  // Helper class that manages per-track state.
43  class Track {
44  public:
45  Track(int track_num,
46  bool is_video,
47  int64_t default_duration,
48  const MediaParser::NewSampleCB& new_sample_cb);
49  ~Track();
50 
51  int track_num() const { return track_num_; }
52 
53  // If |last_added_buffer_missing_duration_| is set, updates its duration
54  // relative to |buffer|'s timestamp, and emits it and unsets
55  // |last_added_buffer_missing_duration_|. Otherwise, if |buffer| is missing
56  // duration, saves |buffer| into |last_added_buffer_missing_duration_|.
57  bool EmitBuffer(const scoped_refptr<MediaSample>& buffer);
58 
59  // If |last_added_buffer_missing_duration_| is set, updates its duration to
60  // be non-kNoTimestamp value of |estimated_next_frame_duration_| or a
61  // hard-coded default, then emits it and unsets
62  // |last_added_buffer_missing_duration_|. (This method helps stream parser
63  // emit all buffers in a media segment before signaling end of segment.)
64  void ApplyDurationEstimateIfNeeded();
65 
66  // Clears all buffer state, including any possibly held-aside buffer that
67  // was missing duration.
68  void Reset();
69 
70  // Helper function used to inspect block data to determine if the
71  // block is a keyframe.
72  // |data| contains the bytes in the block.
73  // |size| indicates the number of bytes in |data|.
74  bool IsKeyframe(const uint8_t* data, int size) const;
75 
76  int64_t default_duration() const { return default_duration_; }
77 
78  private:
79  // Helper that sanity-checks |buffer| duration, updates
80  // |estimated_next_frame_duration_|, and emits |buffer|.
81  // Returns false if |buffer| failed sanity check and therefore was not
82  // emitted. Returns true otherwise.
83  bool EmitBufferHelp(const scoped_refptr<MediaSample>& buffer);
84 
85  // Helper that calculates the buffer duration to use in
86  // ApplyDurationEstimateIfNeeded().
87  int64_t GetDurationEstimate();
88 
89  // Counts the number of estimated durations used in this track. Used to
90  // prevent log spam for LOG()s about estimated duration.
91  int num_duration_estimates_ = 0;
92 
93  int track_num_;
94  bool is_video_;
95 
96  // Parsed track buffers, each with duration and in (decode) timestamp order,
97  // that have not yet been emitted. Note that up to one additional buffer
98  // missing duration may be tracked by |last_added_buffer_missing_duration_|.
99  scoped_refptr<MediaSample> last_added_buffer_missing_duration_;
100 
101  // If kNoTimestamp, then |estimated_next_frame_duration_| will be used.
102  int64_t default_duration_;
103 
104  // If kNoTimestamp, then a default value will be used. This estimate is the
105  // maximum duration seen so far for this track, and is used only if
106  // |default_duration_| is kNoTimestamp.
107  int64_t estimated_next_frame_duration_;
108 
109  MediaParser::NewSampleCB new_sample_cb_;
110  };
111 
112  typedef std::map<int, Track> TextTrackMap;
113 
114  public:
115  WebMClusterParser(int64_t timecode_scale,
116  int audio_track_num,
117  int64_t audio_default_duration,
118  int video_track_num,
119  int64_t video_default_duration,
120  const WebMTracksParser::TextTracks& text_tracks,
121  const std::set<int64_t>& ignored_tracks,
122  const std::string& audio_encryption_key_id,
123  const std::string& video_encryption_key_id,
124  const AudioCodec audio_codec,
125  const MediaParser::NewSampleCB& new_sample_cb);
126  ~WebMClusterParser() override;
127 
129  void Reset();
130 
133  void Flush();
134 
139  int Parse(const uint8_t* buf, int size);
140 
141  int64_t cluster_start_time() const { return cluster_start_time_; }
142 
144  bool cluster_ended() const { return cluster_ended_; }
145 
146  private:
147  // WebMParserClient methods.
148  WebMParserClient* OnListStart(int id) override;
149  bool OnListEnd(int id) override;
150  bool OnUInt(int id, int64_t val) override;
151  bool OnBinary(int id, const uint8_t* data, int size) override;
152 
153  bool ParseBlock(bool is_simple_block,
154  const uint8_t* buf,
155  int size,
156  const uint8_t* additional,
157  int additional_size,
158  int duration,
159  int64_t discard_padding);
160  bool OnBlock(bool is_simple_block,
161  int track_num,
162  int timecode,
163  int duration,
164  int flags,
165  const uint8_t* data,
166  int size,
167  const uint8_t* additional,
168  int additional_size,
169  int64_t discard_padding);
170 
171  // Resets the Track objects associated with each text track.
172  void ResetTextTracks();
173 
174  // Search for the indicated track_num among the text tracks. Returns NULL
175  // if that track num is not a text track.
176  Track* FindTextTrack(int track_num);
177 
178  // Attempts to read the duration from the encoded audio data, returning as
179  // kNoTimestamp if duration cannot be retrieved.
180  // Avoid calling if encrypted; may produce unexpected output. See
181  // implementation for supported codecs.
182  int64_t TryGetEncodedAudioDuration(const uint8_t* data, int size);
183 
184  // Reads Opus packet header to determine packet duration. Duration returned
185  // as kNoTimestamp upon failure to read duration from packet.
186  int64_t ReadOpusDuration(const uint8_t* data, int size);
187 
188  // Tracks the number of LOGs made in process of reading encoded duration.
189  // Useful to prevent log spam.
190  int num_duration_errors_ = 0;
191 
192  double timecode_multiplier_; // Multiplier used to convert timecodes into
193  // microseconds.
194  std::set<int64_t> ignored_tracks_;
195  std::string audio_encryption_key_id_;
196  std::string video_encryption_key_id_;
197  const AudioCodec audio_codec_;
198 
199  WebMListParser parser_;
200 
201  int64_t last_block_timecode_ = -1;
202  scoped_ptr<uint8_t[]> block_data_;
203  int block_data_size_ = -1;
204  int64_t block_duration_ = -1;
205  int64_t block_add_id_ = -1;
206 
207  scoped_ptr<uint8_t[]> block_additional_data_;
208  // Must be 0 if |block_additional_data_| is null. Must be > 0 if
209  // |block_additional_data_| is NOT null.
210  int block_additional_data_size_ = 0;
211 
212  int64_t discard_padding_ = -1;
213  bool discard_padding_set_ = false;
214 
215  int64_t cluster_timecode_ = -1;
216  int64_t cluster_start_time_;
217  bool cluster_ended_ = false;
218 
219  Track audio_;
220  Track video_;
221  TextTrackMap text_track_map_;
222 
223  DISALLOW_IMPLICIT_CONSTRUCTORS(WebMClusterParser);
224 };
225 
226 } // namespace media
227 } // namespace edash_packager
228 
229 #endif // MEDIA_FORMATS_WEBM_WEBM_CLUSTER_PARSER_H_
void Reset()
Resets the parser state so it can accept a new cluster.
int Parse(const uint8_t *buf, int size)
base::Callback< bool(uint32_t track_id, const scoped_refptr< MediaSample > &media_sample)> NewSampleCB
Definition: media_parser.h:43