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  int64_t default_duration() const { return default_duration_; }
71 
72  private:
73  // Helper that sanity-checks |buffer| duration, updates
74  // |estimated_next_frame_duration_|, and emits |buffer|.
75  // Returns false if |buffer| failed sanity check and therefore was not
76  // emitted. Returns true otherwise.
77  bool EmitBufferHelp(const scoped_refptr<MediaSample>& buffer);
78 
79  // Helper that calculates the buffer duration to use in
80  // ApplyDurationEstimateIfNeeded().
81  int64_t GetDurationEstimate();
82 
83  // Counts the number of estimated durations used in this track. Used to
84  // prevent log spam for LOG()s about estimated duration.
85  int num_duration_estimates_ = 0;
86 
87  int track_num_;
88  bool is_video_;
89 
90  // Parsed track buffers, each with duration and in (decode) timestamp order,
91  // that have not yet been emitted. Note that up to one additional buffer
92  // missing duration may be tracked by |last_added_buffer_missing_duration_|.
93  scoped_refptr<MediaSample> last_added_buffer_missing_duration_;
94 
95  // If kNoTimestamp, then |estimated_next_frame_duration_| will be used.
96  int64_t default_duration_;
97 
98  // If kNoTimestamp, then a default value will be used. This estimate is the
99  // maximum duration seen so far for this track, and is used only if
100  // |default_duration_| is kNoTimestamp.
101  int64_t estimated_next_frame_duration_;
102 
103  MediaParser::NewSampleCB new_sample_cb_;
104  };
105 
106  typedef std::map<int, Track> TextTrackMap;
107 
108  public:
109  WebMClusterParser(int64_t timecode_scale,
110  scoped_refptr<AudioStreamInfo> audio_stream_info,
111  scoped_refptr<VideoStreamInfo> video_stream_info,
112  int64_t audio_default_duration,
113  int64_t video_default_duration,
114  const WebMTracksParser::TextTracks& text_tracks,
115  const std::set<int64_t>& ignored_tracks,
116  const std::string& audio_encryption_key_id,
117  const std::string& video_encryption_key_id,
118  const MediaParser::NewSampleCB& new_sample_cb,
119  const MediaParser::InitCB& init_cb);
120  ~WebMClusterParser() override;
121 
123  void Reset();
124 
127  void Flush();
128 
133  int Parse(const uint8_t* buf, int size);
134 
135  int64_t cluster_start_time() const { return cluster_start_time_; }
136 
138  bool cluster_ended() const { return cluster_ended_; }
139 
140  private:
141  // WebMParserClient methods.
142  WebMParserClient* OnListStart(int id) override;
143  bool OnListEnd(int id) override;
144  bool OnUInt(int id, int64_t val) override;
145  bool OnBinary(int id, const uint8_t* data, int size) override;
146 
147  bool ParseBlock(bool is_simple_block,
148  const uint8_t* buf,
149  int size,
150  const uint8_t* additional,
151  int additional_size,
152  int duration,
153  int64_t discard_padding);
154  bool OnBlock(bool is_simple_block,
155  int track_num,
156  int timecode,
157  int duration,
158  int flags,
159  const uint8_t* data,
160  int size,
161  const uint8_t* additional,
162  int additional_size,
163  int64_t discard_padding);
164 
165  // Resets the Track objects associated with each text track.
166  void ResetTextTracks();
167 
168  // Search for the indicated track_num among the text tracks. Returns NULL
169  // if that track num is not a text track.
170  Track* FindTextTrack(int track_num);
171 
172  // Attempts to read the duration from the encoded audio data, returning as
173  // kNoTimestamp if duration cannot be retrieved.
174  // Avoid calling if encrypted; may produce unexpected output. See
175  // implementation for supported codecs.
176  int64_t TryGetEncodedAudioDuration(const uint8_t* data, int size);
177 
178  // Reads Opus packet header to determine packet duration. Duration returned
179  // as kNoTimestamp upon failure to read duration from packet.
180  int64_t ReadOpusDuration(const uint8_t* data, int size);
181 
182  // Tracks the number of LOGs made in process of reading encoded duration.
183  // Useful to prevent log spam.
184  int num_duration_errors_ = 0;
185 
186  double timecode_multiplier_; // Multiplier used to convert timecodes into
187  // microseconds.
188  scoped_refptr<AudioStreamInfo> audio_stream_info_;
189  scoped_refptr<VideoStreamInfo> video_stream_info_;
190  std::set<int64_t> ignored_tracks_;
191  std::string audio_encryption_key_id_;
192  std::string video_encryption_key_id_;
193 
194  WebMListParser parser_;
195 
196  // Indicates whether init_cb has been executed. |init_cb| is executed when we
197  // have codec configuration of video stream, which is extracted from the first
198  // video sample.
199  bool initialized_;
200  MediaParser::InitCB init_cb_;
201 
202  int64_t last_block_timecode_ = -1;
203  scoped_ptr<uint8_t[]> block_data_;
204  int block_data_size_ = -1;
205  int64_t block_duration_ = -1;
206  int64_t block_add_id_ = -1;
207 
208  scoped_ptr<uint8_t[]> block_additional_data_;
209  // Must be 0 if |block_additional_data_| is null. Must be > 0 if
210  // |block_additional_data_| is NOT null.
211  int block_additional_data_size_ = 0;
212 
213  int64_t discard_padding_ = -1;
214  bool discard_padding_set_ = false;
215 
216  int64_t cluster_timecode_ = -1;
217  int64_t cluster_start_time_;
218  bool cluster_ended_ = false;
219 
220  Track audio_;
221  Track video_;
222  TextTrackMap text_track_map_;
223 
224  DISALLOW_IMPLICIT_CONSTRUCTORS(WebMClusterParser);
225 };
226 
227 } // namespace media
228 } // namespace edash_packager
229 
230 #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
base::Callback< void(const std::vector< scoped_refptr< StreamInfo > > &stream_info)> InitCB
Definition: media_parser.h:34