DASH Media Packaging SDK
 All Classes Namespaces Functions Variables Typedefs Enumerations 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 <memory>
11 #include <set>
12 #include <string>
13 
14 #include "packager/base/compiler_specific.h"
15 #include "packager/media/base/decryptor_source.h"
16 #include "packager/media/base/media_parser.h"
17 #include "packager/media/base/media_sample.h"
18 #include "packager/media/formats/webm/webm_parser.h"
19 #include "packager/media/formats/webm/webm_tracks_parser.h"
20 
21 namespace shaka {
22 namespace media {
23 
25  public:
28  enum {
31 
35  };
36 
37  private:
38  // Helper class that manages per-track state.
39  class Track {
40  public:
41  Track(int track_num,
42  bool is_video,
43  int64_t default_duration,
44  const MediaParser::NewSampleCB& new_sample_cb);
45  ~Track();
46 
47  int track_num() const { return track_num_; }
48 
49  // If |last_added_buffer_missing_duration_| is set, updates its duration
50  // relative to |buffer|'s timestamp, and emits it and unsets
51  // |last_added_buffer_missing_duration_|. Otherwise, if |buffer| is missing
52  // duration, saves |buffer| into |last_added_buffer_missing_duration_|.
53  bool EmitBuffer(const std::shared_ptr<MediaSample>& buffer);
54 
55  // If |last_added_buffer_missing_duration_| is set, estimate the duration
56  // for this buffer using helper function GetDurationEstimate() then emits it
57  // and unsets |last_added_buffer_missing_duration_| (This method helps
58  // stream parser emit all buffers in a media segment).
59  bool ApplyDurationEstimateIfNeeded();
60 
61  // Clears all buffer state, including any possibly held-aside buffer that
62  // was missing duration.
63  void Reset();
64 
65  private:
66  // Helper that sanity-checks |buffer| duration, updates
67  // |estimated_next_frame_duration_|, and emits |buffer|.
68  // Returns false if |buffer| failed sanity check and therefore was not
69  // emitted. Returns true otherwise.
70  bool EmitBufferHelp(const std::shared_ptr<MediaSample>& buffer);
71 
72  // Helper function that calculates the buffer duration to use in
73  // ApplyDurationEstimateIfNeeded().
74  int64_t GetDurationEstimate();
75 
76  int track_num_;
77  bool is_video_;
78 
79  // Holding the sample that is missing duration. The duration will be
80  // computed from the difference in timestamp when next sample arrives; or
81  // estimated if it is the last sample in this track.
82  std::shared_ptr<MediaSample> last_added_buffer_missing_duration_;
83 
84  // If kNoTimestamp, then |estimated_next_frame_duration_| will be used.
85  int64_t default_duration_;
86 
87  // If kNoTimestamp, then a hardcoded default value will be used. This
88  // estimate is the maximum duration seen so far for this track, and is used
89  // only if |default_duration_| is kNoTimestamp.
90  int64_t estimated_next_frame_duration_;
91 
92  MediaParser::NewSampleCB new_sample_cb_;
93  };
94 
95  typedef std::map<int, Track> TextTrackMap;
96 
97  public:
120  WebMClusterParser(int64_t timecode_scale,
121  std::shared_ptr<AudioStreamInfo> audio_stream_info,
122  std::shared_ptr<VideoStreamInfo> video_stream_info,
123  int64_t audio_default_duration,
124  int64_t video_default_duration,
125  const WebMTracksParser::TextTracks& text_tracks,
126  const std::set<int64_t>& ignored_tracks,
127  const std::string& audio_encryption_key_id,
128  const std::string& video_encryption_key_id,
129  const MediaParser::NewSampleCB& new_sample_cb,
130  const MediaParser::InitCB& init_cb,
131  KeySource* decryption_key_source);
132  ~WebMClusterParser() override;
133 
135  void Reset();
136 
140  bool Flush() WARN_UNUSED_RESULT;
141 
146  int Parse(const uint8_t* buf, int size);
147 
148  int64_t cluster_start_time() const { return cluster_start_time_; }
149 
151  bool cluster_ended() const { return cluster_ended_; }
152 
153  private:
154  // WebMParserClient methods.
155  WebMParserClient* OnListStart(int id) override;
156  bool OnListEnd(int id) override;
157  bool OnUInt(int id, int64_t val) override;
158  bool OnBinary(int id, const uint8_t* data, int size) override;
159 
160  bool ParseBlock(bool is_simple_block,
161  const uint8_t* buf,
162  int size,
163  const uint8_t* additional,
164  int additional_size,
165  int duration,
166  int64_t discard_padding,
167  bool reference_block_set);
168  bool OnBlock(bool is_simple_block,
169  int track_num,
170  int timecode,
171  int duration,
172  const uint8_t* data,
173  int size,
174  const uint8_t* additional,
175  int additional_size,
176  int64_t discard_padding,
177  bool is_key_frame);
178 
179  // Resets the Track objects associated with each text track.
180  void ResetTextTracks();
181 
182  // Search for the indicated track_num among the text tracks. Returns NULL
183  // if that track num is not a text track.
184  Track* FindTextTrack(int track_num);
185 
186  // Multiplier used to convert timecodes into microseconds.
187  double timecode_multiplier_;
188 
189  std::shared_ptr<AudioStreamInfo> audio_stream_info_;
190  std::shared_ptr<VideoStreamInfo> video_stream_info_;
191  std::set<int64_t> ignored_tracks_;
192 
193  std::unique_ptr<DecryptorSource> decryptor_source_;
194  std::string audio_encryption_key_id_;
195  std::string video_encryption_key_id_;
196 
197  WebMListParser parser_;
198 
199  // Indicates whether init_cb has been executed. |init_cb| is executed when we
200  // have codec configuration of video stream, which is extracted from the first
201  // video sample.
202  bool initialized_;
203  MediaParser::InitCB init_cb_;
204 
205  int64_t last_block_timecode_ = -1;
206  std::unique_ptr<uint8_t[]> block_data_;
207  int block_data_size_ = -1;
208  int64_t block_duration_ = -1;
209  int64_t block_add_id_ = -1;
210 
211  std::unique_ptr<uint8_t[]> block_additional_data_;
212  // Must be 0 if |block_additional_data_| is null. Must be > 0 if
213  // |block_additional_data_| is NOT null.
214  int block_additional_data_size_ = 0;
215 
216  int64_t discard_padding_ = -1;
217  bool discard_padding_set_ = false;
218 
219  bool reference_block_set_ = false;
220 
221  int64_t cluster_timecode_ = -1;
222  int64_t cluster_start_time_;
223  bool cluster_ended_ = false;
224 
225  Track audio_;
226  Track video_;
227  TextTrackMap text_track_map_;
228 
229  DISALLOW_COPY_AND_ASSIGN(WebMClusterParser);
230 };
231 
232 } // namespace media
233 } // namespace shaka
234 
235 #endif // MEDIA_FORMATS_WEBM_WEBM_CLUSTER_PARSER_H_
int Parse(const uint8_t *buf, int size)
base::Callback< void(const std::vector< std::shared_ptr< StreamInfo > > &stream_info)> InitCB
Definition: media_parser.h:34
base::Callback< bool(uint32_t track_id, const std::shared_ptr< MediaSample > &media_sample)> NewSampleCB
Definition: media_parser.h:43
bool Flush() WARN_UNUSED_RESULT
WebMClusterParser(int64_t timecode_scale, std::shared_ptr< AudioStreamInfo > audio_stream_info, std::shared_ptr< VideoStreamInfo > video_stream_info, int64_t audio_default_duration, int64_t video_default_duration, const WebMTracksParser::TextTracks &text_tracks, const std::set< int64_t > &ignored_tracks, const std::string &audio_encryption_key_id, const std::string &video_encryption_key_id, const MediaParser::NewSampleCB &new_sample_cb, const MediaParser::InitCB &init_cb, KeySource *decryption_key_source)
void Reset()
Resets the parser state so it can accept a new cluster.