OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef MEDIA_FORMATS_WEBM_WEBM_CLUSTER_PARSER_H_ | 5 #ifndef MEDIA_FORMATS_WEBM_WEBM_CLUSTER_PARSER_H_ |
6 #define MEDIA_FORMATS_WEBM_WEBM_CLUSTER_PARSER_H_ | 6 #define MEDIA_FORMATS_WEBM_WEBM_CLUSTER_PARSER_H_ |
7 | 7 |
8 #include <deque> | 8 #include <deque> |
9 #include <map> | 9 #include <map> |
10 #include <set> | 10 #include <set> |
11 #include <string> | 11 #include <string> |
12 | 12 |
13 #include "base/memory/scoped_ptr.h" | 13 #include "base/memory/scoped_ptr.h" |
| 14 #include "media/base/audio_decoder_config.h" |
14 #include "media/base/media_export.h" | 15 #include "media/base/media_export.h" |
15 #include "media/base/media_log.h" | 16 #include "media/base/media_log.h" |
16 #include "media/base/stream_parser.h" | 17 #include "media/base/stream_parser.h" |
17 #include "media/base/stream_parser_buffer.h" | 18 #include "media/base/stream_parser_buffer.h" |
18 #include "media/formats/webm/webm_parser.h" | 19 #include "media/formats/webm/webm_parser.h" |
19 #include "media/formats/webm/webm_tracks_parser.h" | 20 #include "media/formats/webm/webm_tracks_parser.h" |
20 | 21 |
21 namespace media { | 22 namespace media { |
22 | 23 |
23 class MEDIA_EXPORT WebMClusterParser : public WebMParserClient { | 24 class MEDIA_EXPORT WebMClusterParser : public WebMParserClient { |
24 public: | 25 public: |
25 typedef StreamParser::TrackId TrackId; | 26 typedef StreamParser::TrackId TrackId; |
26 typedef std::deque<scoped_refptr<StreamParserBuffer> > BufferQueue; | 27 typedef std::deque<scoped_refptr<StreamParserBuffer> > BufferQueue; |
27 typedef std::map<TrackId, const BufferQueue> TextBufferQueueMap; | 28 typedef std::map<TrackId, const BufferQueue> TextBufferQueueMap; |
28 | 29 |
29 // Arbitrarily-chosen numbers to estimate the duration of a buffer if none is | 30 // Arbitrarily-chosen numbers to estimate the duration of a buffer if none is |
30 // set and there is not enough information to get a better estimate. | 31 // set and there is not enough information to get a better estimate. |
31 // TODO(wolenetz/acolwell): Parse audio codebook to determine missing audio | |
32 // frame durations. See http://crbug.com/351166. | |
33 enum { | 32 enum { |
34 kDefaultAudioBufferDurationInMs = 23, // Common 1k samples @44.1kHz | 33 kDefaultAudioBufferDurationInMs = 23, // Common 1k samples @44.1kHz |
35 kDefaultVideoBufferDurationInMs = 42 // Low 24fps to reduce stalls | 34 kDefaultVideoBufferDurationInMs = 42 // Low 24fps to reduce stalls |
36 }; | 35 }; |
37 | 36 |
| 37 // Opus packets encode the duration and other parameters in the 5 most |
| 38 // significant bits of the first byte. The index in this array corresponds |
| 39 // to the duration of each frame of the packet in microseconds. See |
| 40 // https://tools.ietf.org/html/rfc6716#page-14 |
| 41 static const uint16_t kOpusFrameDurationsMu[]; |
| 42 |
38 private: | 43 private: |
39 // Helper class that manages per-track state. | 44 // Helper class that manages per-track state. |
40 class Track { | 45 class Track { |
41 public: | 46 public: |
42 Track(int track_num, | 47 Track(int track_num, |
43 bool is_video, | 48 bool is_video, |
44 base::TimeDelta default_duration, | 49 base::TimeDelta default_duration, |
45 const LogCB& log_cb); | 50 const LogCB& log_cb); |
46 ~Track(); | 51 ~Track(); |
47 | 52 |
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
80 | 85 |
81 // Clears all buffer state, including any possibly held-aside buffer that | 86 // Clears all buffer state, including any possibly held-aside buffer that |
82 // was missing duration, and all contents of |buffers_| and | 87 // was missing duration, and all contents of |buffers_| and |
83 // |ready_buffers_|. | 88 // |ready_buffers_|. |
84 void Reset(); | 89 void Reset(); |
85 | 90 |
86 // Helper function used to inspect block data to determine if the | 91 // Helper function used to inspect block data to determine if the |
87 // block is a keyframe. | 92 // block is a keyframe. |
88 // |data| contains the bytes in the block. | 93 // |data| contains the bytes in the block. |
89 // |size| indicates the number of bytes in |data|. | 94 // |size| indicates the number of bytes in |data|. |
90 bool IsKeyframe(const uint8* data, int size) const; | 95 bool IsKeyframe(const uint8_t* data, int size) const; |
91 | 96 |
92 base::TimeDelta default_duration() const { return default_duration_; } | 97 base::TimeDelta default_duration() const { return default_duration_; } |
93 | 98 |
94 private: | 99 private: |
95 // Helper that sanity-checks |buffer| duration, updates | 100 // Helper that sanity-checks |buffer| duration, updates |
96 // |estimated_next_frame_duration_|, and adds |buffer| to |buffers_|. | 101 // |estimated_next_frame_duration_|, and adds |buffer| to |buffers_|. |
97 // Returns false if |buffer| failed sanity check and therefore was not added | 102 // Returns false if |buffer| failed sanity check and therefore was not added |
98 // to |buffers_|. Returns true otherwise. | 103 // to |buffers_|. Returns true otherwise. |
99 bool QueueBuffer(const scoped_refptr<StreamParserBuffer>& buffer); | 104 bool QueueBuffer(const scoped_refptr<StreamParserBuffer>& buffer); |
100 | 105 |
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
136 public: | 141 public: |
137 WebMClusterParser(int64 timecode_scale, | 142 WebMClusterParser(int64 timecode_scale, |
138 int audio_track_num, | 143 int audio_track_num, |
139 base::TimeDelta audio_default_duration, | 144 base::TimeDelta audio_default_duration, |
140 int video_track_num, | 145 int video_track_num, |
141 base::TimeDelta video_default_duration, | 146 base::TimeDelta video_default_duration, |
142 const WebMTracksParser::TextTracks& text_tracks, | 147 const WebMTracksParser::TextTracks& text_tracks, |
143 const std::set<int64>& ignored_tracks, | 148 const std::set<int64>& ignored_tracks, |
144 const std::string& audio_encryption_key_id, | 149 const std::string& audio_encryption_key_id, |
145 const std::string& video_encryption_key_id, | 150 const std::string& video_encryption_key_id, |
| 151 const AudioCodec audio_codec_, |
146 const LogCB& log_cb); | 152 const LogCB& log_cb); |
147 ~WebMClusterParser() override; | 153 ~WebMClusterParser() override; |
148 | 154 |
149 // Resets the parser state so it can accept a new cluster. | 155 // Resets the parser state so it can accept a new cluster. |
150 void Reset(); | 156 void Reset(); |
151 | 157 |
152 // Parses a WebM cluster element in |buf|. | 158 // Parses a WebM cluster element in |buf|. |
153 // | 159 // |
154 // Returns -1 if the parse fails. | 160 // Returns -1 if the parse fails. |
155 // Returns 0 if more data is needed. | 161 // Returns 0 if more data is needed. |
156 // Returns the number of bytes parsed on success. | 162 // Returns the number of bytes parsed on success. |
157 int Parse(const uint8* buf, int size); | 163 int Parse(const uint8_t* buf, int size); |
158 | 164 |
159 base::TimeDelta cluster_start_time() const { return cluster_start_time_; } | 165 base::TimeDelta cluster_start_time() const { return cluster_start_time_; } |
160 | 166 |
161 // Get the current ready buffers resulting from Parse(). | 167 // Get the current ready buffers resulting from Parse(). |
162 // If the parse reached the end of cluster and the last buffer was held aside | 168 // If the parse reached the end of cluster and the last buffer was held aside |
163 // due to missing duration, the buffer is given an estimated duration and | 169 // due to missing duration, the buffer is given an estimated duration and |
164 // included in the result. | 170 // included in the result. |
165 // Otherwise, if there are is a buffer held aside due to missing duration for | 171 // Otherwise, if there are is a buffer held aside due to missing duration for |
166 // any of the tracks, no buffers with same or greater (decode) timestamp will | 172 // any of the tracks, no buffers with same or greater (decode) timestamp will |
167 // be included in the buffers. | 173 // be included in the buffers. |
(...skipping 19 matching lines...) Expand all Loading... |
187 const TextBufferQueueMap& GetTextBuffers(); | 193 const TextBufferQueueMap& GetTextBuffers(); |
188 | 194 |
189 // Returns true if the last Parse() call stopped at the end of a cluster. | 195 // Returns true if the last Parse() call stopped at the end of a cluster. |
190 bool cluster_ended() const { return cluster_ended_; } | 196 bool cluster_ended() const { return cluster_ended_; } |
191 | 197 |
192 private: | 198 private: |
193 // WebMParserClient methods. | 199 // WebMParserClient methods. |
194 WebMParserClient* OnListStart(int id) override; | 200 WebMParserClient* OnListStart(int id) override; |
195 bool OnListEnd(int id) override; | 201 bool OnListEnd(int id) override; |
196 bool OnUInt(int id, int64 val) override; | 202 bool OnUInt(int id, int64 val) override; |
197 bool OnBinary(int id, const uint8* data, int size) override; | 203 bool OnBinary(int id, const uint8_t* data, int size) override; |
198 | 204 |
199 bool ParseBlock(bool is_simple_block, const uint8* buf, int size, | 205 bool ParseBlock(bool is_simple_block, |
200 const uint8* additional, int additional_size, int duration, | 206 const uint8_t* buf, |
| 207 int size, |
| 208 const uint8_t* additional, |
| 209 int additional_size, |
| 210 int duration, |
201 int64 discard_padding); | 211 int64 discard_padding); |
202 bool OnBlock(bool is_simple_block, int track_num, int timecode, int duration, | 212 bool OnBlock(bool is_simple_block, |
203 int flags, const uint8* data, int size, | 213 int track_num, |
204 const uint8* additional, int additional_size, | 214 int timecode, |
| 215 int duration, |
| 216 int flags, |
| 217 const uint8_t* data, |
| 218 int size, |
| 219 const uint8_t* additional, |
| 220 int additional_size, |
205 int64 discard_padding); | 221 int64 discard_padding); |
206 | 222 |
207 // Resets the Track objects associated with each text track. | 223 // Resets the Track objects associated with each text track. |
208 void ResetTextTracks(); | 224 void ResetTextTracks(); |
209 | 225 |
210 // Clears the the ready buffers associated with each text track. | 226 // Clears the the ready buffers associated with each text track. |
211 void ClearTextTrackReadyBuffers(); | 227 void ClearTextTrackReadyBuffers(); |
212 | 228 |
213 // Helper method for Get{Audio,Video,Text}Buffers() that recomputes | 229 // Helper method for Get{Audio,Video,Text}Buffers() that recomputes |
214 // |ready_buffer_upper_bound_| and calls ExtractReadyBuffers() on each track. | 230 // |ready_buffer_upper_bound_| and calls ExtractReadyBuffers() on each track. |
215 // If |cluster_ended_| is true, first applies duration estimate if needed for | 231 // If |cluster_ended_| is true, first applies duration estimate if needed for |
216 // |audio_| and |video_| and sets |ready_buffer_upper_bound_| to | 232 // |audio_| and |video_| and sets |ready_buffer_upper_bound_| to |
217 // kInfiniteDuration(). Otherwise, sets |ready_buffer_upper_bound_| to the | 233 // kInfiniteDuration(). Otherwise, sets |ready_buffer_upper_bound_| to the |
218 // minimum upper bound across |audio_| and |video_|. (Text tracks can have no | 234 // minimum upper bound across |audio_| and |video_|. (Text tracks can have no |
219 // buffers missing duration, so they are not involved in calculating the upper | 235 // buffers missing duration, so they are not involved in calculating the upper |
220 // bound.) | 236 // bound.) |
221 // Parse() or Reset() must be called between calls to UpdateReadyBuffers() to | 237 // Parse() or Reset() must be called between calls to UpdateReadyBuffers() to |
222 // clear each track's ready buffers and to reset |ready_buffer_upper_bound_| | 238 // clear each track's ready buffers and to reset |ready_buffer_upper_bound_| |
223 // to kNoDecodeTimestamp(). | 239 // to kNoDecodeTimestamp(). |
224 void UpdateReadyBuffers(); | 240 void UpdateReadyBuffers(); |
225 | 241 |
226 // Search for the indicated track_num among the text tracks. Returns NULL | 242 // Search for the indicated track_num among the text tracks. Returns NULL |
227 // if that track num is not a text track. | 243 // if that track num is not a text track. |
228 Track* FindTextTrack(int track_num); | 244 Track* FindTextTrack(int track_num); |
229 | 245 |
| 246 // Attempts to read the duration from the encoded audio data, returning as |
| 247 // TimeDelta or kNoTimestamp() if duration cannot be retrieved. This obviously |
| 248 // violates layering rules, but is useful for MSE to know duration in cases |
| 249 // where it isn't explicitly given and cannot be calculated for Blocks at the |
| 250 // end of a Cluster (the next Cluster in playback-order may not be the next |
| 251 // Cluster we parse, so we can't simply use the delta of the first Block in |
| 252 // the next Cluster). Avoid calling if encrypted; may produce unexpected |
| 253 // output. See implementation for supported codecs. |
| 254 base::TimeDelta TryGetEncodedAudioDuration(const uint8_t* data, int size); |
| 255 |
| 256 // Reads Opus packet header to determine packet duration. Duration returned |
| 257 // as TimeDelta or kNoTimestamp() upon failure to read duration from packet. |
| 258 base::TimeDelta ReadOpusDuration(const uint8_t* data, int size); |
| 259 |
| 260 // Tracks the number of MEDIA_LOGs made in process of reading encoded |
| 261 // duration. Useful to prevent log spam. |
| 262 int num_duration_errors_; |
| 263 |
230 double timecode_multiplier_; // Multiplier used to convert timecodes into | 264 double timecode_multiplier_; // Multiplier used to convert timecodes into |
231 // microseconds. | 265 // microseconds. |
232 std::set<int64> ignored_tracks_; | 266 std::set<int64> ignored_tracks_; |
233 std::string audio_encryption_key_id_; | 267 std::string audio_encryption_key_id_; |
234 std::string video_encryption_key_id_; | 268 std::string video_encryption_key_id_; |
| 269 const AudioCodec audio_codec_; |
235 | 270 |
236 WebMListParser parser_; | 271 WebMListParser parser_; |
237 | 272 |
238 int64 last_block_timecode_; | 273 int64 last_block_timecode_; |
239 scoped_ptr<uint8[]> block_data_; | 274 scoped_ptr<uint8_t[]> block_data_; |
240 int block_data_size_; | 275 int block_data_size_; |
241 int64 block_duration_; | 276 int64 block_duration_; |
242 int64 block_add_id_; | 277 int64 block_add_id_; |
243 | 278 |
244 scoped_ptr<uint8[]> block_additional_data_; | 279 scoped_ptr<uint8_t[]> block_additional_data_; |
245 // Must be 0 if |block_additional_data_| is null. Must be > 0 if | 280 // Must be 0 if |block_additional_data_| is null. Must be > 0 if |
246 // |block_additional_data_| is NOT null. | 281 // |block_additional_data_| is NOT null. |
247 int block_additional_data_size_; | 282 int block_additional_data_size_; |
248 | 283 |
249 int64 discard_padding_; | 284 int64 discard_padding_; |
250 bool discard_padding_set_; | 285 bool discard_padding_set_; |
251 | 286 |
252 int64 cluster_timecode_; | 287 int64 cluster_timecode_; |
253 base::TimeDelta cluster_start_time_; | 288 base::TimeDelta cluster_start_time_; |
254 bool cluster_ended_; | 289 bool cluster_ended_; |
(...skipping 16 matching lines...) Expand all Loading... |
271 DecodeTimestamp ready_buffer_upper_bound_; | 306 DecodeTimestamp ready_buffer_upper_bound_; |
272 | 307 |
273 LogCB log_cb_; | 308 LogCB log_cb_; |
274 | 309 |
275 DISALLOW_IMPLICIT_CONSTRUCTORS(WebMClusterParser); | 310 DISALLOW_IMPLICIT_CONSTRUCTORS(WebMClusterParser); |
276 }; | 311 }; |
277 | 312 |
278 } // namespace media | 313 } // namespace media |
279 | 314 |
280 #endif // MEDIA_FORMATS_WEBM_WEBM_CLUSTER_PARSER_H_ | 315 #endif // MEDIA_FORMATS_WEBM_WEBM_CLUSTER_PARSER_H_ |
OLD | NEW |