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/media_export.h" | 14 #include "media/base/media_export.h" |
15 #include "media/base/media_log.h" | 15 #include "media/base/media_log.h" |
16 #include "media/base/stream_parser.h" | 16 #include "media/base/stream_parser.h" |
17 #include "media/base/stream_parser_buffer.h" | 17 #include "media/base/stream_parser_buffer.h" |
18 #include "media/formats/webm/webm_parser.h" | 18 #include "media/formats/webm/webm_parser.h" |
19 #include "media/formats/webm/webm_tracks_parser.h" | 19 #include "media/formats/webm/webm_tracks_parser.h" |
20 | 20 |
21 namespace media { | 21 namespace media { |
22 | 22 |
23 class MEDIA_EXPORT WebMClusterParser : public WebMParserClient { | 23 class MEDIA_EXPORT WebMClusterParser : public WebMParserClient { |
24 public: | 24 public: |
25 typedef StreamParser::TrackId TrackId; | 25 typedef StreamParser::TrackId TrackId; |
26 typedef std::deque<scoped_refptr<StreamParserBuffer> > BufferQueue; | |
27 typedef std::map<TrackId, const BufferQueue> TextBufferQueueMap; | |
26 | 28 |
27 // Arbitrarily-chosen numbers to estimate the duration of a buffer if none is | 29 // Arbitrarily-chosen numbers to estimate the duration of a buffer if none is |
28 // set and there is not enough information to get a better estimate. | 30 // set and there is not enough information to get a better estimate. |
29 // TODO(wolenetz/acolwell): Parse audio codebook to determine missing audio | 31 // TODO(wolenetz/acolwell): Parse audio codebook to determine missing audio |
30 // frame durations. See http://crbug.com/351166. | 32 // frame durations. See http://crbug.com/351166. |
31 enum { | 33 enum { |
32 kDefaultAudioBufferDurationInMs = 23, // Common 1k samples @44.1kHz | 34 kDefaultAudioBufferDurationInMs = 23, // Common 1k samples @44.1kHz |
33 kDefaultVideoBufferDurationInMs = 42 // Low 24fps to reduce stalls | 35 kDefaultVideoBufferDurationInMs = 42 // Low 24fps to reduce stalls |
34 }; | 36 }; |
35 | 37 |
36 private: | 38 private: |
37 // Helper class that manages per-track state. | 39 // Helper class that manages per-track state. |
38 class Track { | 40 class Track { |
39 public: | 41 public: |
40 Track(int track_num, bool is_video, base::TimeDelta default_duration); | 42 Track(int track_num, |
43 bool is_video, | |
44 base::TimeDelta default_duration, | |
45 const LogCB& log_cb); | |
41 ~Track(); | 46 ~Track(); |
42 | 47 |
43 int track_num() const { return track_num_; } | 48 int track_num() const { return track_num_; } |
44 const std::deque<scoped_refptr<StreamParserBuffer> >& buffers() const { | 49 |
45 return buffers_; | 50 // If a buffer is currently held aside pending duration calculation, returns |
46 } | 51 // its decode timestamp. Otherwise, returns kInfiniteDuration(). Across all |
52 // tracks being parsed by a WebMClusterParser, the minimum value of their | |
acolwell GONE FROM CHROMIUM
2014/04/23 22:36:39
nit: It seems like text from about here and below
wolenetz
2014/04/25 20:04:21
Done.
| |
53 // GetReadyUpperBound() determines the exclusive maximum buffer decode | |
54 // timestamp limit for buffers ready for emission. This mechanism ensures | |
55 // any buffers held aside for duration calculation also prevent emission of | |
56 // buffers from other tracks with same or greater decode timestamp. See also | |
57 // WebMClusterParser::UpdateReadyBuffers(). | |
58 base::TimeDelta GetReadyUpperBound(); | |
59 | |
60 // Extracts and returns |ready_buffers_|, which are the initial portion of | |
acolwell GONE FROM CHROMIUM
2014/04/23 22:36:39
nit: ditto. I think a smaller comment here would b
wolenetz
2014/04/25 20:04:21
Done.
| |
61 // |buffers_|, if any, excluding all buffers with timestamp at or after | |
62 // |before_timestamp|. Removes all ready buffers from |buffers_|. If | |
63 // |before_timestamp| is kInfiniteDuration(), then |ready_buffers| is filled | |
64 // with all of |buffers_|. Note, QueueBuffer() ensures that |buffers_| is in | |
65 // non-decreasing timestamp order. | |
66 // The returned BufferQueue& is cleared by ClearReadyBuffers() and Reset(), | |
67 // and it must be empty prior to the next ExtractReadyBuffers() call. | |
68 // ready_buffers() does not do any extraction, rather, it just returns | |
69 // the current |ready_buffers_| even if not yet extracted since last clear. | |
70 const BufferQueue& ExtractReadyBuffers( | |
71 const base::TimeDelta before_timestamp); | |
72 const BufferQueue& ready_buffers() const { return ready_buffers_; } | |
47 | 73 |
48 // If |last_added_buffer_missing_duration_| is set, updates its duration | 74 // If |last_added_buffer_missing_duration_| is set, updates its duration |
49 // relative to |buffer|'s timestamp, and adds it to |buffers_| and unsets | 75 // relative to |buffer|'s timestamp, and adds it to |buffers_| and unsets |
50 // |last_added_buffer_missing_duration_|. Then, if |buffer| is missing | 76 // |last_added_buffer_missing_duration_|. Then, if |buffer| is missing |
51 // duration, saves |buffer| into |last_added_buffer_missing_duration_|, or | 77 // duration, saves |buffer| into |last_added_buffer_missing_duration_|, or |
52 // otherwise adds |buffer| to |buffers_|. | 78 // otherwise adds |buffer| to |buffers_|. |
53 bool AddBuffer(const scoped_refptr<StreamParserBuffer>& buffer); | 79 bool AddBuffer(const scoped_refptr<StreamParserBuffer>& buffer); |
54 | 80 |
55 // If |last_added_buffer_missing_duration_| is set, updates its duration to | 81 // If |last_added_buffer_missing_duration_| is set, updates its duration to |
56 // be non-kNoTimestamp() value of |estimated_next_frame_duration_| or an | 82 // be non-kNoTimestamp() value of |estimated_next_frame_duration_| or an |
57 // arbitrary default, then adds it to |buffers_| and unsets | 83 // arbitrary default, then adds it to |buffers_| and unsets |
58 // |last_added_buffer_missing_duration_|. (This method helps stream parser | 84 // |last_added_buffer_missing_duration_|. (This method helps stream parser |
59 // emit all buffers in a media segment before signaling end of segment.) | 85 // emit all buffers in a media segment before signaling end of segment.) |
60 void ApplyDurationEstimateIfNeeded(); | 86 void ApplyDurationEstimateIfNeeded(); |
61 | 87 |
62 // Clears all buffer state, except a possibly held-aside buffer that is | 88 // Clears |ready_buffers_| (use ExtractReadyBuffers() to fill it again). |
89 // Leaves as-is |buffers_| and any possibly held-aside buffer that is | |
63 // missing duration. | 90 // missing duration. |
64 void ClearBuffersButKeepLastIfMissingDuration(); | 91 void ClearReadyBuffers(); |
65 | 92 |
66 // Clears all buffer state, including any possibly held-aside buffer that | 93 // Clears all buffer state, including any possibly held-aside buffer that |
67 // was missing duration. | 94 // was missing duration, and all contents of |buffers_| and |
95 // |ready_buffers_|. | |
68 void Reset(); | 96 void Reset(); |
69 | 97 |
70 // Helper function used to inspect block data to determine if the | 98 // Helper function used to inspect block data to determine if the |
71 // block is a keyframe. | 99 // block is a keyframe. |
72 // |data| contains the bytes in the block. | 100 // |data| contains the bytes in the block. |
73 // |size| indicates the number of bytes in |data|. | 101 // |size| indicates the number of bytes in |data|. |
74 bool IsKeyframe(const uint8* data, int size) const; | 102 bool IsKeyframe(const uint8* data, int size) const; |
75 | 103 |
76 base::TimeDelta default_duration() const { return default_duration_; } | 104 base::TimeDelta default_duration() const { return default_duration_; } |
77 | 105 |
78 private: | 106 private: |
79 // Helper that sanity-checks |buffer| duration, updates | 107 // Helper that sanity-checks |buffer| duration, updates |
80 // |estimated_next_frame_duration_|, and adds |buffer| to |buffers_|. | 108 // |estimated_next_frame_duration_|, and adds |buffer| to |buffers_|. |
81 // Returns false if |buffer| failed sanity check and therefore was not added | 109 // Returns false if |buffer| failed sanity check and therefore was not added |
82 // to |buffers_|. Returns true otherwise. | 110 // to |buffers_|. Returns true otherwise. |
83 bool QueueBuffer(const scoped_refptr<StreamParserBuffer>& buffer); | 111 bool QueueBuffer(const scoped_refptr<StreamParserBuffer>& buffer); |
84 | 112 |
85 // Helper that calculates the buffer duration to use in | 113 // Helper that calculates the buffer duration to use in |
86 // ApplyDurationEstimateIfNeeded(). | 114 // ApplyDurationEstimateIfNeeded(). |
87 base::TimeDelta GetDurationEstimate(); | 115 base::TimeDelta GetDurationEstimate(); |
88 | 116 |
89 int track_num_; | 117 int track_num_; |
90 std::deque<scoped_refptr<StreamParserBuffer> > buffers_; | 118 BufferQueue buffers_; |
91 bool is_video_; | 119 bool is_video_; |
92 scoped_refptr<StreamParserBuffer> last_added_buffer_missing_duration_; | 120 scoped_refptr<StreamParserBuffer> last_added_buffer_missing_duration_; |
93 | 121 |
122 // Extracted buffers from |buffers_|, maintained by ExtractReadyBuffers(), | |
123 // and cleared by Reset() and ClearReadyBuffers(). | |
acolwell GONE FROM CHROMIUM
2014/04/23 22:36:39
nit: This comment doesn't really explain the diffe
wolenetz
2014/04/25 20:04:21
Done.
| |
124 BufferQueue ready_buffers_; | |
125 | |
94 // If kNoTimestamp(), then |estimated_next_frame_duration_| will be used. | 126 // If kNoTimestamp(), then |estimated_next_frame_duration_| will be used. |
95 base::TimeDelta default_duration_; | 127 base::TimeDelta default_duration_; |
128 | |
96 // If kNoTimestamp(), then a default value will be used. This estimate is | 129 // If kNoTimestamp(), then a default value will be used. This estimate is |
97 // the maximum duration seen or derived so far for this track, and is valid | 130 // the maximum duration seen or derived so far for this track, and is valid |
98 // only if |default_duration_| is kNoTimestamp(). | 131 // only if |default_duration_| is kNoTimestamp(). |
99 // | |
100 // TODO(wolenetz): Add unittests for duration estimation and default | |
101 // duration handling. http://crbug.com/361786 . | |
102 base::TimeDelta estimated_next_frame_duration_; | 132 base::TimeDelta estimated_next_frame_duration_; |
133 | |
134 LogCB log_cb_; | |
103 }; | 135 }; |
104 | 136 |
105 typedef std::map<int, Track> TextTrackMap; | 137 typedef std::map<int, Track> TextTrackMap; |
106 | 138 |
107 public: | 139 public: |
108 typedef std::deque<scoped_refptr<StreamParserBuffer> > BufferQueue; | |
109 typedef std::map<TrackId, const BufferQueue> TextBufferQueueMap; | |
110 | |
111 WebMClusterParser(int64 timecode_scale, | 140 WebMClusterParser(int64 timecode_scale, |
112 int audio_track_num, | 141 int audio_track_num, |
113 base::TimeDelta audio_default_duration, | 142 base::TimeDelta audio_default_duration, |
114 int video_track_num, | 143 int video_track_num, |
115 base::TimeDelta video_default_duration, | 144 base::TimeDelta video_default_duration, |
116 const WebMTracksParser::TextTracks& text_tracks, | 145 const WebMTracksParser::TextTracks& text_tracks, |
117 const std::set<int64>& ignored_tracks, | 146 const std::set<int64>& ignored_tracks, |
118 const std::string& audio_encryption_key_id, | 147 const std::string& audio_encryption_key_id, |
119 const std::string& video_encryption_key_id, | 148 const std::string& video_encryption_key_id, |
120 const LogCB& log_cb); | 149 const LogCB& log_cb); |
121 virtual ~WebMClusterParser(); | 150 virtual ~WebMClusterParser(); |
122 | 151 |
123 // Resets the parser state so it can accept a new cluster. | 152 // Resets the parser state so it can accept a new cluster. |
124 void Reset(); | 153 void Reset(); |
125 | 154 |
126 // Parses a WebM cluster element in |buf|. | 155 // Parses a WebM cluster element in |buf|. |
127 // | 156 // |
128 // Returns -1 if the parse fails. | 157 // Returns -1 if the parse fails. |
129 // Returns 0 if more data is needed. | 158 // Returns 0 if more data is needed. |
130 // Returns the number of bytes parsed on success. | 159 // Returns the number of bytes parsed on success. |
131 int Parse(const uint8* buf, int size); | 160 int Parse(const uint8* buf, int size); |
132 | 161 |
133 base::TimeDelta cluster_start_time() const { return cluster_start_time_; } | 162 base::TimeDelta cluster_start_time() const { return cluster_start_time_; } |
134 | 163 |
135 // Get the buffers resulting from Parse(). | 164 // Get the current ready buffers resulting from Parse(). |
136 // If the parse reached the end of cluster and the last buffer was held aside | 165 // If the parse reached the end of cluster and the last buffer was held aside |
137 // due to missing duration, the buffer is given an estimated duration and | 166 // due to missing duration, the buffer is given an estimated duration and |
138 // included in the result. | 167 // included in the result. |
168 // Otherwise, if there are is a buffer held aside due to missing duration for | |
169 // any of the tracks, no buffers with same or greater (decode) timestamp will | |
170 // be included in the buffers. | |
171 // The returned deques are cleared by Parse() or Reset() and updated by the | |
172 // next calls to Get{Audio,Video}Buffers(). | |
173 // If no Parse() or Reset() has occurred since the last call to Get{Audio, | |
174 // Video,Text}Buffers(), then the previous BufferQueue& is returned again | |
175 // without any recalculation. | |
139 const BufferQueue& GetAudioBuffers(); | 176 const BufferQueue& GetAudioBuffers(); |
140 const BufferQueue& GetVideoBuffers(); | 177 const BufferQueue& GetVideoBuffers(); |
141 | 178 |
142 // Constructs and returns a subset of |text_track_map_| containing only | 179 // Constructs and returns a subset of |text_track_map_| containing only |
143 // tracks with non-empty buffer queues produced by the last Parse(). | 180 // tracks with non-empty buffer queues produced by the last Parse() and |
181 // filtered to exclude any buffers that have (decode) timestamp same or | |
182 // greater than the lowest (decode) timestamp across all tracks of any buffer | |
183 // held aside due to missing duration (unless the end of cluster has been | |
184 // reached). | |
144 // The returned map is cleared by Parse() or Reset() and updated by the next | 185 // The returned map is cleared by Parse() or Reset() and updated by the next |
145 // call to GetTextBuffers(). | 186 // call to GetTextBuffers(). |
187 // If no Parse() or Reset() has occurred since the last call to | |
188 // GetTextBuffers(), then the previous TextBufferQueueMap& is returned again | |
189 // without any recalculation. | |
146 const TextBufferQueueMap& GetTextBuffers(); | 190 const TextBufferQueueMap& GetTextBuffers(); |
147 | 191 |
148 // Returns true if the last Parse() call stopped at the end of a cluster. | 192 // Returns true if the last Parse() call stopped at the end of a cluster. |
149 bool cluster_ended() const { return cluster_ended_; } | 193 bool cluster_ended() const { return cluster_ended_; } |
150 | 194 |
151 private: | 195 private: |
152 // WebMParserClient methods. | 196 // WebMParserClient methods. |
153 virtual WebMParserClient* OnListStart(int id) OVERRIDE; | 197 virtual WebMParserClient* OnListStart(int id) OVERRIDE; |
154 virtual bool OnListEnd(int id) OVERRIDE; | 198 virtual bool OnListEnd(int id) OVERRIDE; |
155 virtual bool OnUInt(int id, int64 val) OVERRIDE; | 199 virtual bool OnUInt(int id, int64 val) OVERRIDE; |
156 virtual bool OnBinary(int id, const uint8* data, int size) OVERRIDE; | 200 virtual bool OnBinary(int id, const uint8* data, int size) OVERRIDE; |
157 | 201 |
158 bool ParseBlock(bool is_simple_block, const uint8* buf, int size, | 202 bool ParseBlock(bool is_simple_block, const uint8* buf, int size, |
159 const uint8* additional, int additional_size, int duration, | 203 const uint8* additional, int additional_size, int duration, |
160 int64 discard_padding); | 204 int64 discard_padding); |
161 bool OnBlock(bool is_simple_block, int track_num, int timecode, int duration, | 205 bool OnBlock(bool is_simple_block, int track_num, int timecode, int duration, |
162 int flags, const uint8* data, int size, | 206 int flags, const uint8* data, int size, |
163 const uint8* additional, int additional_size, | 207 const uint8* additional, int additional_size, |
164 int64 discard_padding); | 208 int64 discard_padding); |
165 | 209 |
166 // Resets the Track objects associated with each text track. | 210 // Resets the Track objects associated with each text track. |
167 void ResetTextTracks(); | 211 void ResetTextTracks(); |
168 | 212 |
213 // Clears the the ready buffers associated with each text track. | |
214 void ClearTextTrackReadyBuffers(); | |
215 | |
216 // Helper method for Get{Audio,Video,Text}Buffers() that recomputes | |
217 // |ready_buffer_upper_bound_| and calls ExtractReadyBuffers() on each track. | |
218 // If |cluster_ended_| is true, first applies duration estimate if needed for | |
219 // |audio_| and |video_| and sets |ready_buffer_upper_bound_| to | |
220 // kInfiniteDuration(). Otherwise, sets |ready_buffer_upper_bound_| to the | |
221 // minimum upper bound across |audio_| and |video_|. (Text tracks can have no | |
222 // buffers missing duration, so they are not involved in calculating the upper | |
223 // bound.) | |
224 // Parse() or Reset() must be called between calls to UpdateReadyBuffers() to | |
225 // clear each track's ready buffers and to reset |ready_buffer_upper_bound_| | |
226 // to kNoTimestamp(). | |
227 void UpdateReadyBuffers(); | |
228 | |
169 // Search for the indicated track_num among the text tracks. Returns NULL | 229 // Search for the indicated track_num among the text tracks. Returns NULL |
170 // if that track num is not a text track. | 230 // if that track num is not a text track. |
171 Track* FindTextTrack(int track_num); | 231 Track* FindTextTrack(int track_num); |
172 | 232 |
173 double timecode_multiplier_; // Multiplier used to convert timecodes into | 233 double timecode_multiplier_; // Multiplier used to convert timecodes into |
174 // microseconds. | 234 // microseconds. |
175 std::set<int64> ignored_tracks_; | 235 std::set<int64> ignored_tracks_; |
176 std::string audio_encryption_key_id_; | 236 std::string audio_encryption_key_id_; |
177 std::string video_encryption_key_id_; | 237 std::string video_encryption_key_id_; |
178 | 238 |
(...skipping 11 matching lines...) Expand all Loading... | |
190 | 250 |
191 int64 cluster_timecode_; | 251 int64 cluster_timecode_; |
192 base::TimeDelta cluster_start_time_; | 252 base::TimeDelta cluster_start_time_; |
193 bool cluster_ended_; | 253 bool cluster_ended_; |
194 | 254 |
195 Track audio_; | 255 Track audio_; |
196 Track video_; | 256 Track video_; |
197 TextTrackMap text_track_map_; | 257 TextTrackMap text_track_map_; |
198 | 258 |
199 // Subset of |text_track_map_| maintained by GetTextBuffers(), and cleared by | 259 // Subset of |text_track_map_| maintained by GetTextBuffers(), and cleared by |
200 // ResetTextTracks(). Callers of GetTextBuffers() get a const-ref to this | 260 // ClearTextTrackReadyBuffers(). Callers of GetTextBuffers() get a const-ref |
201 // member. | 261 // to this member. |
202 TextBufferQueueMap text_buffers_map_; | 262 TextBufferQueueMap text_buffers_map_; |
203 | 263 |
264 // Limits the range of buffers returned by Get{Audio,Video,Text}Buffers() to | |
265 // this exclusive upper bound. Set to kNoTimestamp(), meaning not yet | |
266 // calculated, by Reset() and Parse(). If kNoTimestamp(), then | |
267 // Get{Audio,Video,Text}Buffers() will calculate it to be the minimum (decode) | |
268 // timestamp across all tracks' |last_buffer_missing_duration_|, or | |
269 // kInfiniteDuration() if no buffers are currently missing duration. | |
270 base::TimeDelta ready_buffer_upper_bound_; | |
271 | |
204 LogCB log_cb_; | 272 LogCB log_cb_; |
205 | 273 |
206 DISALLOW_IMPLICIT_CONSTRUCTORS(WebMClusterParser); | 274 DISALLOW_IMPLICIT_CONSTRUCTORS(WebMClusterParser); |
207 }; | 275 }; |
208 | 276 |
209 } // namespace media | 277 } // namespace media |
210 | 278 |
211 #endif // MEDIA_FORMATS_WEBM_WEBM_CLUSTER_PARSER_H_ | 279 #endif // MEDIA_FORMATS_WEBM_WEBM_CLUSTER_PARSER_H_ |
OLD | NEW |