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(). |
52 base::TimeDelta GetReadyUpperBound(); | |
53 | |
54 // Prepares |ready_buffers_| for retrieval. Prior to calling, | |
55 // |ready_buffers_| must be empty. Moves all |buffers_| with timestamp | |
56 // before |before_timestamp| to |ready_buffers_|, preserving their order. | |
57 void ExtractReadyBuffers(const base::TimeDelta before_timestamp); | |
58 | |
59 // Gets |ready_buffers_|. | |
acolwell GONE FROM CHROMIUM
2014/04/26 00:29:35
nit: Remove. You can see what it does right here.
wolenetz
2014/04/26 00:37:38
Done.
| |
60 const BufferQueue& ready_buffers() const { return ready_buffers_; } | |
47 | 61 |
48 // If |last_added_buffer_missing_duration_| is set, updates its duration | 62 // If |last_added_buffer_missing_duration_| is set, updates its duration |
49 // relative to |buffer|'s timestamp, and adds it to |buffers_| and unsets | 63 // relative to |buffer|'s timestamp, and adds it to |buffers_| and unsets |
50 // |last_added_buffer_missing_duration_|. Then, if |buffer| is missing | 64 // |last_added_buffer_missing_duration_|. Then, if |buffer| is missing |
51 // duration, saves |buffer| into |last_added_buffer_missing_duration_|, or | 65 // duration, saves |buffer| into |last_added_buffer_missing_duration_|, or |
52 // otherwise adds |buffer| to |buffers_|. | 66 // otherwise adds |buffer| to |buffers_|. |
53 bool AddBuffer(const scoped_refptr<StreamParserBuffer>& buffer); | 67 bool AddBuffer(const scoped_refptr<StreamParserBuffer>& buffer); |
54 | 68 |
55 // If |last_added_buffer_missing_duration_| is set, updates its duration to | 69 // If |last_added_buffer_missing_duration_| is set, updates its duration to |
56 // be non-kNoTimestamp() value of |estimated_next_frame_duration_| or an | 70 // be non-kNoTimestamp() value of |estimated_next_frame_duration_| or an |
57 // arbitrary default, then adds it to |buffers_| and unsets | 71 // arbitrary default, then adds it to |buffers_| and unsets |
58 // |last_added_buffer_missing_duration_|. (This method helps stream parser | 72 // |last_added_buffer_missing_duration_|. (This method helps stream parser |
59 // emit all buffers in a media segment before signaling end of segment.) | 73 // emit all buffers in a media segment before signaling end of segment.) |
60 void ApplyDurationEstimateIfNeeded(); | 74 void ApplyDurationEstimateIfNeeded(); |
61 | 75 |
62 // Clears all buffer state, except a possibly held-aside buffer that is | 76 // Clears |ready_buffers_| (use ExtractReadyBuffers() to fill it again). |
77 // Leaves as-is |buffers_| and any possibly held-aside buffer that is | |
63 // missing duration. | 78 // missing duration. |
64 void ClearBuffersButKeepLastIfMissingDuration(); | 79 void ClearReadyBuffers(); |
65 | 80 |
66 // Clears all buffer state, including any possibly held-aside buffer that | 81 // Clears all buffer state, including any possibly held-aside buffer that |
67 // was missing duration. | 82 // was missing duration, and all contents of |buffers_| and |
83 // |ready_buffers_|. | |
68 void Reset(); | 84 void Reset(); |
69 | 85 |
70 // Helper function used to inspect block data to determine if the | 86 // Helper function used to inspect block data to determine if the |
71 // block is a keyframe. | 87 // block is a keyframe. |
72 // |data| contains the bytes in the block. | 88 // |data| contains the bytes in the block. |
73 // |size| indicates the number of bytes in |data|. | 89 // |size| indicates the number of bytes in |data|. |
74 bool IsKeyframe(const uint8* data, int size) const; | 90 bool IsKeyframe(const uint8* data, int size) const; |
75 | 91 |
76 base::TimeDelta default_duration() const { return default_duration_; } | 92 base::TimeDelta default_duration() const { return default_duration_; } |
77 | 93 |
78 private: | 94 private: |
79 // Helper that sanity-checks |buffer| duration, updates | 95 // Helper that sanity-checks |buffer| duration, updates |
80 // |estimated_next_frame_duration_|, and adds |buffer| to |buffers_|. | 96 // |estimated_next_frame_duration_|, and adds |buffer| to |buffers_|. |
81 // Returns false if |buffer| failed sanity check and therefore was not added | 97 // Returns false if |buffer| failed sanity check and therefore was not added |
82 // to |buffers_|. Returns true otherwise. | 98 // to |buffers_|. Returns true otherwise. |
83 bool QueueBuffer(const scoped_refptr<StreamParserBuffer>& buffer); | 99 bool QueueBuffer(const scoped_refptr<StreamParserBuffer>& buffer); |
84 | 100 |
85 // Helper that calculates the buffer duration to use in | 101 // Helper that calculates the buffer duration to use in |
86 // ApplyDurationEstimateIfNeeded(). | 102 // ApplyDurationEstimateIfNeeded(). |
87 base::TimeDelta GetDurationEstimate(); | 103 base::TimeDelta GetDurationEstimate(); |
88 | 104 |
89 int track_num_; | 105 int track_num_; |
90 std::deque<scoped_refptr<StreamParserBuffer> > buffers_; | |
91 bool is_video_; | 106 bool is_video_; |
107 | |
108 // Parsed track buffers, each with duration and in (decode) timestamp order, | |
109 // that have not yet been extracted into |ready_buffers_|. Note that up to | |
110 // one additional buffer missing duration may be tracked by | |
111 // |last_added_buffer_missing_duration_|. | |
112 BufferQueue buffers_; | |
92 scoped_refptr<StreamParserBuffer> last_added_buffer_missing_duration_; | 113 scoped_refptr<StreamParserBuffer> last_added_buffer_missing_duration_; |
93 | 114 |
115 // Buffers in (decode) timestamp order that were previously parsed into and | |
116 // extracted from |buffers_|. Buffers are moved from |buffers_| to | |
117 // |ready_buffers_| by ExtractReadyBuffers() if they are below a specified | |
118 // upper bound timestamp. Track users can therefore extract only those | |
119 // parsed buffers which are "ready" for emission (all before some maximum | |
120 // timestamp). | |
121 BufferQueue ready_buffers_; | |
122 | |
94 // If kNoTimestamp(), then |estimated_next_frame_duration_| will be used. | 123 // If kNoTimestamp(), then |estimated_next_frame_duration_| will be used. |
95 base::TimeDelta default_duration_; | 124 base::TimeDelta default_duration_; |
125 | |
96 // If kNoTimestamp(), then a default value will be used. This estimate is | 126 // 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 | 127 // the maximum duration seen or derived so far for this track, and is valid |
98 // only if |default_duration_| is kNoTimestamp(). | 128 // 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_; | 129 base::TimeDelta estimated_next_frame_duration_; |
130 | |
131 LogCB log_cb_; | |
103 }; | 132 }; |
104 | 133 |
105 typedef std::map<int, Track> TextTrackMap; | 134 typedef std::map<int, Track> TextTrackMap; |
106 | 135 |
107 public: | 136 public: |
108 typedef std::deque<scoped_refptr<StreamParserBuffer> > BufferQueue; | |
109 typedef std::map<TrackId, const BufferQueue> TextBufferQueueMap; | |
110 | |
111 WebMClusterParser(int64 timecode_scale, | 137 WebMClusterParser(int64 timecode_scale, |
112 int audio_track_num, | 138 int audio_track_num, |
113 base::TimeDelta audio_default_duration, | 139 base::TimeDelta audio_default_duration, |
114 int video_track_num, | 140 int video_track_num, |
115 base::TimeDelta video_default_duration, | 141 base::TimeDelta video_default_duration, |
116 const WebMTracksParser::TextTracks& text_tracks, | 142 const WebMTracksParser::TextTracks& text_tracks, |
117 const std::set<int64>& ignored_tracks, | 143 const std::set<int64>& ignored_tracks, |
118 const std::string& audio_encryption_key_id, | 144 const std::string& audio_encryption_key_id, |
119 const std::string& video_encryption_key_id, | 145 const std::string& video_encryption_key_id, |
120 const LogCB& log_cb); | 146 const LogCB& log_cb); |
121 virtual ~WebMClusterParser(); | 147 virtual ~WebMClusterParser(); |
122 | 148 |
123 // Resets the parser state so it can accept a new cluster. | 149 // Resets the parser state so it can accept a new cluster. |
124 void Reset(); | 150 void Reset(); |
125 | 151 |
126 // Parses a WebM cluster element in |buf|. | 152 // Parses a WebM cluster element in |buf|. |
127 // | 153 // |
128 // Returns -1 if the parse fails. | 154 // Returns -1 if the parse fails. |
129 // Returns 0 if more data is needed. | 155 // Returns 0 if more data is needed. |
130 // Returns the number of bytes parsed on success. | 156 // Returns the number of bytes parsed on success. |
131 int Parse(const uint8* buf, int size); | 157 int Parse(const uint8* buf, int size); |
132 | 158 |
133 base::TimeDelta cluster_start_time() const { return cluster_start_time_; } | 159 base::TimeDelta cluster_start_time() const { return cluster_start_time_; } |
134 | 160 |
135 // Get the buffers resulting from Parse(). | 161 // Get the current ready buffers resulting from Parse(). |
136 // If the parse reached the end of cluster and the last buffer was held aside | 162 // 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 | 163 // due to missing duration, the buffer is given an estimated duration and |
138 // included in the result. | 164 // included in the result. |
165 // 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 | |
167 // be included in the buffers. | |
168 // The returned deques are cleared by Parse() or Reset() and updated by the | |
169 // next calls to Get{Audio,Video}Buffers(). | |
170 // If no Parse() or Reset() has occurred since the last call to Get{Audio, | |
171 // Video,Text}Buffers(), then the previous BufferQueue& is returned again | |
172 // without any recalculation. | |
139 const BufferQueue& GetAudioBuffers(); | 173 const BufferQueue& GetAudioBuffers(); |
140 const BufferQueue& GetVideoBuffers(); | 174 const BufferQueue& GetVideoBuffers(); |
141 | 175 |
142 // Constructs and returns a subset of |text_track_map_| containing only | 176 // Constructs and returns a subset of |text_track_map_| containing only |
143 // tracks with non-empty buffer queues produced by the last Parse(). | 177 // tracks with non-empty buffer queues produced by the last Parse() and |
178 // filtered to exclude any buffers that have (decode) timestamp same or | |
179 // greater than the lowest (decode) timestamp across all tracks of any buffer | |
180 // held aside due to missing duration (unless the end of cluster has been | |
181 // reached). | |
144 // The returned map is cleared by Parse() or Reset() and updated by the next | 182 // The returned map is cleared by Parse() or Reset() and updated by the next |
145 // call to GetTextBuffers(). | 183 // call to GetTextBuffers(). |
184 // If no Parse() or Reset() has occurred since the last call to | |
185 // GetTextBuffers(), then the previous TextBufferQueueMap& is returned again | |
186 // without any recalculation. | |
146 const TextBufferQueueMap& GetTextBuffers(); | 187 const TextBufferQueueMap& GetTextBuffers(); |
147 | 188 |
148 // Returns true if the last Parse() call stopped at the end of a cluster. | 189 // Returns true if the last Parse() call stopped at the end of a cluster. |
149 bool cluster_ended() const { return cluster_ended_; } | 190 bool cluster_ended() const { return cluster_ended_; } |
150 | 191 |
151 private: | 192 private: |
152 // WebMParserClient methods. | 193 // WebMParserClient methods. |
153 virtual WebMParserClient* OnListStart(int id) OVERRIDE; | 194 virtual WebMParserClient* OnListStart(int id) OVERRIDE; |
154 virtual bool OnListEnd(int id) OVERRIDE; | 195 virtual bool OnListEnd(int id) OVERRIDE; |
155 virtual bool OnUInt(int id, int64 val) OVERRIDE; | 196 virtual bool OnUInt(int id, int64 val) OVERRIDE; |
156 virtual bool OnBinary(int id, const uint8* data, int size) OVERRIDE; | 197 virtual bool OnBinary(int id, const uint8* data, int size) OVERRIDE; |
157 | 198 |
158 bool ParseBlock(bool is_simple_block, const uint8* buf, int size, | 199 bool ParseBlock(bool is_simple_block, const uint8* buf, int size, |
159 const uint8* additional, int additional_size, int duration, | 200 const uint8* additional, int additional_size, int duration, |
160 int64 discard_padding); | 201 int64 discard_padding); |
161 bool OnBlock(bool is_simple_block, int track_num, int timecode, int duration, | 202 bool OnBlock(bool is_simple_block, int track_num, int timecode, int duration, |
162 int flags, const uint8* data, int size, | 203 int flags, const uint8* data, int size, |
163 const uint8* additional, int additional_size, | 204 const uint8* additional, int additional_size, |
164 int64 discard_padding); | 205 int64 discard_padding); |
165 | 206 |
166 // Resets the Track objects associated with each text track. | 207 // Resets the Track objects associated with each text track. |
167 void ResetTextTracks(); | 208 void ResetTextTracks(); |
168 | 209 |
210 // Clears the the ready buffers associated with each text track. | |
211 void ClearTextTrackReadyBuffers(); | |
212 | |
213 // Helper method for Get{Audio,Video,Text}Buffers() that recomputes | |
214 // |ready_buffer_upper_bound_| and calls ExtractReadyBuffers() on each track. | |
215 // If |cluster_ended_| is true, first applies duration estimate if needed for | |
216 // |audio_| and |video_| and sets |ready_buffer_upper_bound_| to | |
217 // kInfiniteDuration(). Otherwise, sets |ready_buffer_upper_bound_| to the | |
218 // 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 | |
220 // bound.) | |
221 // 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_| | |
223 // to kNoTimestamp(). | |
224 void UpdateReadyBuffers(); | |
225 | |
169 // Search for the indicated track_num among the text tracks. Returns NULL | 226 // Search for the indicated track_num among the text tracks. Returns NULL |
170 // if that track num is not a text track. | 227 // if that track num is not a text track. |
171 Track* FindTextTrack(int track_num); | 228 Track* FindTextTrack(int track_num); |
172 | 229 |
173 double timecode_multiplier_; // Multiplier used to convert timecodes into | 230 double timecode_multiplier_; // Multiplier used to convert timecodes into |
174 // microseconds. | 231 // microseconds. |
175 std::set<int64> ignored_tracks_; | 232 std::set<int64> ignored_tracks_; |
176 std::string audio_encryption_key_id_; | 233 std::string audio_encryption_key_id_; |
177 std::string video_encryption_key_id_; | 234 std::string video_encryption_key_id_; |
178 | 235 |
(...skipping 11 matching lines...) Expand all Loading... | |
190 | 247 |
191 int64 cluster_timecode_; | 248 int64 cluster_timecode_; |
192 base::TimeDelta cluster_start_time_; | 249 base::TimeDelta cluster_start_time_; |
193 bool cluster_ended_; | 250 bool cluster_ended_; |
194 | 251 |
195 Track audio_; | 252 Track audio_; |
196 Track video_; | 253 Track video_; |
197 TextTrackMap text_track_map_; | 254 TextTrackMap text_track_map_; |
198 | 255 |
199 // Subset of |text_track_map_| maintained by GetTextBuffers(), and cleared by | 256 // Subset of |text_track_map_| maintained by GetTextBuffers(), and cleared by |
200 // ResetTextTracks(). Callers of GetTextBuffers() get a const-ref to this | 257 // ClearTextTrackReadyBuffers(). Callers of GetTextBuffers() get a const-ref |
201 // member. | 258 // to this member. |
202 TextBufferQueueMap text_buffers_map_; | 259 TextBufferQueueMap text_buffers_map_; |
203 | 260 |
261 // Limits the range of buffers returned by Get{Audio,Video,Text}Buffers() to | |
262 // this exclusive upper bound. Set to kNoTimestamp(), meaning not yet | |
263 // calculated, by Reset() and Parse(). If kNoTimestamp(), then | |
264 // Get{Audio,Video,Text}Buffers() will calculate it to be the minimum (decode) | |
265 // timestamp across all tracks' |last_buffer_missing_duration_|, or | |
266 // kInfiniteDuration() if no buffers are currently missing duration. | |
267 base::TimeDelta ready_buffer_upper_bound_; | |
268 | |
204 LogCB log_cb_; | 269 LogCB log_cb_; |
205 | 270 |
206 DISALLOW_IMPLICIT_CONSTRUCTORS(WebMClusterParser); | 271 DISALLOW_IMPLICIT_CONSTRUCTORS(WebMClusterParser); |
207 }; | 272 }; |
208 | 273 |
209 } // namespace media | 274 } // namespace media |
210 | 275 |
211 #endif // MEDIA_FORMATS_WEBM_WEBM_CLUSTER_PARSER_H_ | 276 #endif // MEDIA_FORMATS_WEBM_WEBM_CLUSTER_PARSER_H_ |
OLD | NEW |