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 <set>
11 #include <string>
12 
13 #include "packager/base/compiler_specific.h"
14 #include "packager/base/memory/scoped_ptr.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 scoped_refptr<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 scoped_refptr<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  scoped_refptr<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  scoped_refptr<AudioStreamInfo> audio_stream_info,
122  scoped_refptr<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 OnBlock(bool is_simple_block,
168  int track_num,
169  int timecode,
170  int duration,
171  int flags,
172  const uint8_t* data,
173  int size,
174  const uint8_t* additional,
175  int additional_size,
176  int64_t discard_padding);
177 
178  // Resets the Track objects associated with each text track.
179  void ResetTextTracks();
180 
181  // Search for the indicated track_num among the text tracks. Returns NULL
182  // if that track num is not a text track.
183  Track* FindTextTrack(int track_num);
184 
185  // Multiplier used to convert timecodes into microseconds.
186  double timecode_multiplier_;
187 
188  scoped_refptr<AudioStreamInfo> audio_stream_info_;
189  scoped_refptr<VideoStreamInfo> video_stream_info_;
190  std::set<int64_t> ignored_tracks_;
191 
192  scoped_ptr<DecryptorSource> decryptor_source_;
193  std::string audio_encryption_key_id_;
194  std::string video_encryption_key_id_;
195 
196  WebMListParser parser_;
197 
198  // Indicates whether init_cb has been executed. |init_cb| is executed when we
199  // have codec configuration of video stream, which is extracted from the first
200  // video sample.
201  bool initialized_;
202  MediaParser::InitCB init_cb_;
203 
204  int64_t last_block_timecode_ = -1;
205  scoped_ptr<uint8_t[]> block_data_;
206  int block_data_size_ = -1;
207  int64_t block_duration_ = -1;
208  int64_t block_add_id_ = -1;
209 
210  scoped_ptr<uint8_t[]> block_additional_data_;
211  // Must be 0 if |block_additional_data_| is null. Must be > 0 if
212  // |block_additional_data_| is NOT null.
213  int block_additional_data_size_ = 0;
214 
215  int64_t discard_padding_ = -1;
216  bool discard_padding_set_ = false;
217 
218  int64_t cluster_timecode_ = -1;
219  int64_t cluster_start_time_;
220  bool cluster_ended_ = false;
221 
222  Track audio_;
223  Track video_;
224  TextTrackMap text_track_map_;
225 
226  DISALLOW_COPY_AND_ASSIGN(WebMClusterParser);
227 };
228 
229 } // namespace media
230 } // namespace shaka
231 
232 #endif // MEDIA_FORMATS_WEBM_WEBM_CLUSTER_PARSER_H_
base::Callback< void(const std::vector< scoped_refptr< StreamInfo > > &stream_info)> InitCB
Definition: media_parser.h:35
int Parse(const uint8_t *buf, int size)
bool Flush() WARN_UNUSED_RESULT
base::Callback< bool(uint32_t track_id, const scoped_refptr< MediaSample > &media_sample)> NewSampleCB
Definition: media_parser.h:44
WebMClusterParser(int64_t timecode_scale, scoped_refptr< AudioStreamInfo > audio_stream_info, scoped_refptr< 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.