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 "base/memory/scoped_ptr.h" 14 #include "media/base/media_export.h" 15 #include "media/base/media_log.h" 16 #include "media/base/stream_parser.h" 17 #include "media/base/stream_parser_buffer.h" 18 #include "media/formats/webm/webm_parser.h" 19 #include "media/formats/webm/webm_tracks_parser.h" 20 21 namespace media { 22 23 class MEDIA_EXPORT WebMClusterParser : public WebMParserClient { 24 public: 25 typedef StreamParser::TrackId TrackId; 26 typedef std::deque<scoped_refptr<StreamParserBuffer> > BufferQueue; 27 typedef std::map<TrackId, const BufferQueue> TextBufferQueueMap; 28 29 // 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 // TODO(wolenetz/acolwell): Parse audio codebook to determine missing audio 32 // frame durations. See http://crbug.com/351166. 33 enum { 34 kDefaultAudioBufferDurationInMs = 23, // Common 1k samples @44.1kHz 35 kDefaultVideoBufferDurationInMs = 42 // Low 24fps to reduce stalls 36 }; 37 38 private: 39 // Helper class that manages per-track state. 40 class Track { 41 public: 42 Track(int track_num, 43 bool is_video, 44 base::TimeDelta default_duration, 45 const LogCB& log_cb); 46 ~Track(); 47 48 int track_num() const { return track_num_; } 49 50 // If a buffer is currently held aside pending duration calculation, returns 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_; } 60 61 // If |last_added_buffer_missing_duration_| is set, updates its duration 62 // relative to |buffer|'s timestamp, and adds it to |buffers_| and unsets 63 // |last_added_buffer_missing_duration_|. Then, if |buffer| is missing 64 // duration, saves |buffer| into |last_added_buffer_missing_duration_|, or 65 // otherwise adds |buffer| to |buffers_|. 66 bool AddBuffer(const scoped_refptr<StreamParserBuffer>& buffer); 67 68 // If |last_added_buffer_missing_duration_| is set, updates its duration to 69 // be non-kNoTimestamp() value of |estimated_next_frame_duration_| or an 70 // arbitrary default, then adds it to |buffers_| and unsets 71 // |last_added_buffer_missing_duration_|. (This method helps stream parser 72 // emit all buffers in a media segment before signaling end of segment.) 73 void ApplyDurationEstimateIfNeeded(); 74 75 // Clears |ready_buffers_| (use ExtractReadyBuffers() to fill it again). 76 // Leaves as-is |buffers_| and any possibly held-aside buffer that is 77 // missing duration. 78 void ClearReadyBuffers(); 79 80 // Clears all buffer state, including any possibly held-aside buffer that 81 // was missing duration, and all contents of |buffers_| and 82 // |ready_buffers_|. 83 void Reset(); 84 85 // Helper function used to inspect block data to determine if the 86 // block is a keyframe. 87 // |data| contains the bytes in the block. 88 // |size| indicates the number of bytes in |data|. 89 bool IsKeyframe(const uint8* data, int size) const; 90 91 base::TimeDelta default_duration() const { return default_duration_; } 92 93 private: 94 // Helper that sanity-checks |buffer| duration, updates 95 // |estimated_next_frame_duration_|, and adds |buffer| to |buffers_|. 96 // Returns false if |buffer| failed sanity check and therefore was not added 97 // to |buffers_|. Returns true otherwise. 98 bool QueueBuffer(const scoped_refptr<StreamParserBuffer>& buffer); 99 100 // Helper that calculates the buffer duration to use in 101 // ApplyDurationEstimateIfNeeded(). 102 base::TimeDelta GetDurationEstimate(); 103 104 int track_num_; 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_; 112 scoped_refptr<StreamParserBuffer> last_added_buffer_missing_duration_; 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 122 // If kNoTimestamp(), then |estimated_next_frame_duration_| will be used. 123 base::TimeDelta default_duration_; 124 125 // If kNoTimestamp(), then a default value will be used. This estimate is 126 // the maximum duration seen or derived so far for this track, and is valid 127 // only if |default_duration_| is kNoTimestamp(). 128 base::TimeDelta estimated_next_frame_duration_; 129 130 LogCB log_cb_; 131 }; 132 133 typedef std::map<int, Track> TextTrackMap; 134 135 public: 136 WebMClusterParser(int64 timecode_scale, 137 int audio_track_num, 138 base::TimeDelta audio_default_duration, 139 int video_track_num, 140 base::TimeDelta video_default_duration, 141 const WebMTracksParser::TextTracks& text_tracks, 142 const std::set<int64>& ignored_tracks, 143 const std::string& audio_encryption_key_id, 144 const std::string& video_encryption_key_id, 145 const LogCB& log_cb); 146 virtual ~WebMClusterParser(); 147 148 // Resets the parser state so it can accept a new cluster. 149 void Reset(); 150 151 // Parses a WebM cluster element in |buf|. 152 // 153 // Returns -1 if the parse fails. 154 // Returns 0 if more data is needed. 155 // Returns the number of bytes parsed on success. 156 int Parse(const uint8* buf, int size); 157 158 base::TimeDelta cluster_start_time() const { return cluster_start_time_; } 159 160 // Get the current ready buffers resulting from Parse(). 161 // If the parse reached the end of cluster and the last buffer was held aside 162 // due to missing duration, the buffer is given an estimated duration and 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. 172 const BufferQueue& GetAudioBuffers(); 173 const BufferQueue& GetVideoBuffers(); 174 175 // Constructs and returns a subset of |text_track_map_| containing only 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). 181 // The returned map is cleared by Parse() or Reset() and updated by the next 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. 186 const TextBufferQueueMap& GetTextBuffers(); 187 188 // Returns true if the last Parse() call stopped at the end of a cluster. 189 bool cluster_ended() const { return cluster_ended_; } 190 191 private: 192 // WebMParserClient methods. 193 virtual WebMParserClient* OnListStart(int id) OVERRIDE; 194 virtual bool OnListEnd(int id) OVERRIDE; 195 virtual bool OnUInt(int id, int64 val) OVERRIDE; 196 virtual bool OnBinary(int id, const uint8* data, int size) OVERRIDE; 197 198 bool ParseBlock(bool is_simple_block, const uint8* buf, int size, 199 const uint8* additional, int additional_size, int duration, 200 int64 discard_padding); 201 bool OnBlock(bool is_simple_block, int track_num, int timecode, int duration, 202 int flags, const uint8* data, int size, 203 const uint8* additional, int additional_size, 204 int64 discard_padding); 205 206 // Resets the Track objects associated with each text track. 207 void ResetTextTracks(); 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 225 // Search for the indicated track_num among the text tracks. Returns NULL 226 // if that track num is not a text track. 227 Track* FindTextTrack(int track_num); 228 229 double timecode_multiplier_; // Multiplier used to convert timecodes into 230 // microseconds. 231 std::set<int64> ignored_tracks_; 232 std::string audio_encryption_key_id_; 233 std::string video_encryption_key_id_; 234 235 WebMListParser parser_; 236 237 int64 last_block_timecode_; 238 scoped_ptr<uint8[]> block_data_; 239 int block_data_size_; 240 int64 block_duration_; 241 int64 block_add_id_; 242 scoped_ptr<uint8[]> block_additional_data_; 243 int block_additional_data_size_; 244 int64 discard_padding_; 245 bool discard_padding_set_; 246 247 int64 cluster_timecode_; 248 base::TimeDelta cluster_start_time_; 249 bool cluster_ended_; 250 251 Track audio_; 252 Track video_; 253 TextTrackMap text_track_map_; 254 255 // Subset of |text_track_map_| maintained by GetTextBuffers(), and cleared by 256 // ClearTextTrackReadyBuffers(). Callers of GetTextBuffers() get a const-ref 257 // to this member. 258 TextBufferQueueMap text_buffers_map_; 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 268 LogCB log_cb_; 269 270 DISALLOW_IMPLICIT_CONSTRUCTORS(WebMClusterParser); 271 }; 272 273 } // namespace media 274 275 #endif // MEDIA_FORMATS_WEBM_WEBM_CLUSTER_PARSER_H_ 276