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 
35  private:
36  // Helper class that manages per-track state.
37  class Track {
38  public:
39  Track(int track_num,
40  bool is_video,
41  int64_t default_duration,
42  const MediaParser::NewSampleCB& new_sample_cb);
43  ~Track();
44 
45  int track_num() const { return track_num_; }
46 
47  // If |last_added_buffer_missing_duration_| is set, updates its duration
48  // relative to |buffer|'s timestamp, and emits it and unsets
49  // |last_added_buffer_missing_duration_|. Otherwise, if |buffer| is missing
50  // duration, saves |buffer| into |last_added_buffer_missing_duration_|.
51  bool EmitBuffer(const scoped_refptr<MediaSample>& buffer);
52 
53  // If |last_added_buffer_missing_duration_| is set, estimate the duration
54  // for this buffer using helper function GetDurationEstimate() then emits it
55  // and unsets |last_added_buffer_missing_duration_| (This method helps
56  // stream parser emit all buffers in a media segment).
57  void ApplyDurationEstimateIfNeeded();
58 
59  // Clears all buffer state, including any possibly held-aside buffer that
60  // was missing duration.
61  void Reset();
62 
63  private:
64  // Helper that sanity-checks |buffer| duration, updates
65  // |estimated_next_frame_duration_|, and emits |buffer|.
66  // Returns false if |buffer| failed sanity check and therefore was not
67  // emitted. Returns true otherwise.
68  bool EmitBufferHelp(const scoped_refptr<MediaSample>& buffer);
69 
70  // Helper function that calculates the buffer duration to use in
71  // ApplyDurationEstimateIfNeeded().
72  int64_t GetDurationEstimate();
73 
74  int track_num_;
75  bool is_video_;
76 
77  // Holding the sample that is missing duration. The duration will be
78  // computed from the difference in timestamp when next sample arrives; or
79  // estimated if it is the last sample in this track.
80  scoped_refptr<MediaSample> last_added_buffer_missing_duration_;
81 
82  // If kNoTimestamp, then |estimated_next_frame_duration_| will be used.
83  int64_t default_duration_;
84 
85  // If kNoTimestamp, then a hardcoded default value will be used. This
86  // estimate is the maximum duration seen so far for this track, and is used
87  // only if |default_duration_| is kNoTimestamp.
88  int64_t estimated_next_frame_duration_;
89 
90  MediaParser::NewSampleCB new_sample_cb_;
91  };
92 
93  typedef std::map<int, Track> TextTrackMap;
94 
95  public:
96  WebMClusterParser(int64_t timecode_scale,
97  scoped_refptr<AudioStreamInfo> audio_stream_info,
98  scoped_refptr<VideoStreamInfo> video_stream_info,
99  int64_t audio_default_duration,
100  int64_t video_default_duration,
101  const WebMTracksParser::TextTracks& text_tracks,
102  const std::set<int64_t>& ignored_tracks,
103  const std::string& audio_encryption_key_id,
104  const std::string& video_encryption_key_id,
105  const MediaParser::NewSampleCB& new_sample_cb,
106  const MediaParser::InitCB& init_cb);
107  ~WebMClusterParser() override;
108 
110  void Reset();
111 
114  void Flush();
115 
120  int Parse(const uint8_t* buf, int size);
121 
122  int64_t cluster_start_time() const { return cluster_start_time_; }
123 
125  bool cluster_ended() const { return cluster_ended_; }
126 
127  private:
128  // WebMParserClient methods.
129  WebMParserClient* OnListStart(int id) override;
130  bool OnListEnd(int id) override;
131  bool OnUInt(int id, int64_t val) override;
132  bool OnBinary(int id, const uint8_t* data, int size) override;
133 
134  bool ParseBlock(bool is_simple_block,
135  const uint8_t* buf,
136  int size,
137  const uint8_t* additional,
138  int additional_size,
139  int duration,
140  int64_t discard_padding);
141  bool OnBlock(bool is_simple_block,
142  int track_num,
143  int timecode,
144  int duration,
145  int flags,
146  const uint8_t* data,
147  int size,
148  const uint8_t* additional,
149  int additional_size,
150  int64_t discard_padding);
151 
152  // Resets the Track objects associated with each text track.
153  void ResetTextTracks();
154 
155  // Search for the indicated track_num among the text tracks. Returns NULL
156  // if that track num is not a text track.
157  Track* FindTextTrack(int track_num);
158 
159  // Multiplier used to convert timecodes into microseconds.
160  double timecode_multiplier_;
161 
162  scoped_refptr<AudioStreamInfo> audio_stream_info_;
163  scoped_refptr<VideoStreamInfo> video_stream_info_;
164  std::set<int64_t> ignored_tracks_;
165  std::string audio_encryption_key_id_;
166  std::string video_encryption_key_id_;
167 
168  WebMListParser parser_;
169 
170  // Indicates whether init_cb has been executed. |init_cb| is executed when we
171  // have codec configuration of video stream, which is extracted from the first
172  // video sample.
173  bool initialized_;
174  MediaParser::InitCB init_cb_;
175 
176  int64_t last_block_timecode_ = -1;
177  scoped_ptr<uint8_t[]> block_data_;
178  int block_data_size_ = -1;
179  int64_t block_duration_ = -1;
180  int64_t block_add_id_ = -1;
181 
182  scoped_ptr<uint8_t[]> block_additional_data_;
183  // Must be 0 if |block_additional_data_| is null. Must be > 0 if
184  // |block_additional_data_| is NOT null.
185  int block_additional_data_size_ = 0;
186 
187  int64_t discard_padding_ = -1;
188  bool discard_padding_set_ = false;
189 
190  int64_t cluster_timecode_ = -1;
191  int64_t cluster_start_time_;
192  bool cluster_ended_ = false;
193 
194  Track audio_;
195  Track video_;
196  TextTrackMap text_track_map_;
197 
198  DISALLOW_COPY_AND_ASSIGN(WebMClusterParser);
199 };
200 
201 } // namespace media
202 } // namespace edash_packager
203 
204 #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