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