| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 #include "media/webm/webm_stream_parser.h" | 5 #include "media/webm/webm_stream_parser.h" |
| 6 | 6 |
| 7 #include <string> | 7 #include <string> |
| 8 | 8 |
| 9 #include "base/callback.h" | 9 #include "base/callback.h" |
| 10 #include "base/logging.h" | 10 #include "base/logging.h" |
| 11 #include "base/stl_util.h" |
| 11 #include "media/webm/webm_cluster_parser.h" | 12 #include "media/webm/webm_cluster_parser.h" |
| 12 #include "media/webm/webm_constants.h" | 13 #include "media/webm/webm_constants.h" |
| 13 #include "media/webm/webm_content_encodings.h" | 14 #include "media/webm/webm_content_encodings.h" |
| 14 #include "media/webm/webm_crypto_helpers.h" | 15 #include "media/webm/webm_crypto_helpers.h" |
| 15 #include "media/webm/webm_info_parser.h" | 16 #include "media/webm/webm_info_parser.h" |
| 16 #include "media/webm/webm_tracks_parser.h" | 17 #include "media/webm/webm_tracks_parser.h" |
| 17 | 18 |
| 18 namespace media { | 19 namespace media { |
| 19 | 20 |
| 20 WebMStreamParser::WebMStreamParser() | 21 WebMStreamParser::WebMStreamParser() |
| 21 : state_(kWaitingForInit), | 22 : state_(kWaitingForInit), |
| 22 waiting_for_buffers_(false) { | 23 waiting_for_buffers_(false) { |
| 23 } | 24 } |
| 24 | 25 |
| 25 WebMStreamParser::~WebMStreamParser() {} | 26 WebMStreamParser::~WebMStreamParser() { |
| 27 STLDeleteValues(&text_track_map_); |
| 28 } |
| 26 | 29 |
| 27 void WebMStreamParser::Init(const InitCB& init_cb, | 30 void WebMStreamParser::Init(const InitCB& init_cb, |
| 28 const NewConfigCB& config_cb, | 31 const NewConfigCB& config_cb, |
| 29 const NewBuffersCB& audio_cb, | 32 const NewBuffersCB& audio_cb, |
| 30 const NewBuffersCB& video_cb, | 33 const NewBuffersCB& video_cb, |
| 34 const NewTextBuffersCB& text_cb, |
| 31 const NeedKeyCB& need_key_cb, | 35 const NeedKeyCB& need_key_cb, |
| 36 const AddTextTrackCB& add_text_track_cb, |
| 32 const NewMediaSegmentCB& new_segment_cb, | 37 const NewMediaSegmentCB& new_segment_cb, |
| 33 const base::Closure& end_of_segment_cb, | 38 const base::Closure& end_of_segment_cb, |
| 34 const LogCB& log_cb) { | 39 const LogCB& log_cb) { |
| 35 DCHECK_EQ(state_, kWaitingForInit); | 40 DCHECK_EQ(state_, kWaitingForInit); |
| 36 DCHECK(init_cb_.is_null()); | 41 DCHECK(init_cb_.is_null()); |
| 37 DCHECK(!init_cb.is_null()); | 42 DCHECK(!init_cb.is_null()); |
| 38 DCHECK(!config_cb.is_null()); | 43 DCHECK(!config_cb.is_null()); |
| 39 DCHECK(!audio_cb.is_null() || !video_cb.is_null()); | 44 DCHECK(!audio_cb.is_null() || !video_cb.is_null()); |
| 45 DCHECK(!text_cb.is_null()); |
| 40 DCHECK(!need_key_cb.is_null()); | 46 DCHECK(!need_key_cb.is_null()); |
| 41 DCHECK(!new_segment_cb.is_null()); | 47 DCHECK(!new_segment_cb.is_null()); |
| 42 DCHECK(!end_of_segment_cb.is_null()); | 48 DCHECK(!end_of_segment_cb.is_null()); |
| 43 | 49 |
| 44 ChangeState(kParsingHeaders); | 50 ChangeState(kParsingHeaders); |
| 45 init_cb_ = init_cb; | 51 init_cb_ = init_cb; |
| 46 config_cb_ = config_cb; | 52 config_cb_ = config_cb; |
| 47 audio_cb_ = audio_cb; | 53 audio_cb_ = audio_cb; |
| 48 video_cb_ = video_cb; | 54 video_cb_ = video_cb; |
| 55 text_cb_ = text_cb; |
| 49 need_key_cb_ = need_key_cb; | 56 need_key_cb_ = need_key_cb; |
| 57 add_text_track_cb_ = add_text_track_cb; |
| 50 new_segment_cb_ = new_segment_cb; | 58 new_segment_cb_ = new_segment_cb; |
| 51 end_of_segment_cb_ = end_of_segment_cb; | 59 end_of_segment_cb_ = end_of_segment_cb; |
| 52 log_cb_ = log_cb; | 60 log_cb_ = log_cb; |
| 53 } | 61 } |
| 54 | 62 |
| 55 void WebMStreamParser::Flush() { | 63 void WebMStreamParser::Flush() { |
| 56 DCHECK_NE(state_, kWaitingForInit); | 64 DCHECK_NE(state_, kWaitingForInit); |
| 57 | 65 |
| 58 byte_queue_.Reset(); | 66 byte_queue_.Reset(); |
| 59 | 67 |
| (...skipping 131 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 191 | 199 |
| 192 const VideoDecoderConfig& video_config = tracks_parser.video_decoder_config(); | 200 const VideoDecoderConfig& video_config = tracks_parser.video_decoder_config(); |
| 193 if (video_config.is_encrypted()) | 201 if (video_config.is_encrypted()) |
| 194 FireNeedKey(tracks_parser.video_encryption_key_id()); | 202 FireNeedKey(tracks_parser.video_encryption_key_id()); |
| 195 | 203 |
| 196 if (!config_cb_.Run(audio_config, video_config)) { | 204 if (!config_cb_.Run(audio_config, video_config)) { |
| 197 DVLOG(1) << "New config data isn't allowed."; | 205 DVLOG(1) << "New config data isn't allowed."; |
| 198 return -1; | 206 return -1; |
| 199 } | 207 } |
| 200 | 208 |
| 209 typedef WebMTracksParser::TextTracks TextTracks; |
| 210 const TextTracks& text_tracks = tracks_parser.text_tracks(); |
| 211 |
| 212 for (TextTracks::const_iterator itr = text_tracks.begin(); |
| 213 itr != text_tracks.end(); ++itr) { |
| 214 const WebMTracksParser::TextTrackInfo& text_track_info = itr->second; |
| 215 |
| 216 // TODO(matthewjheaney): verify that WebVTT uses ISO 639-2 for lang |
| 217 scoped_ptr<TextTrack> text_track = |
| 218 add_text_track_cb_.Run(text_track_info.kind, |
| 219 text_track_info.name, |
| 220 text_track_info.language); |
| 221 |
| 222 // Assume ownership of pointer, and cache the text track object, for use |
| 223 // later when we have text track buffers. (The text track objects are |
| 224 // deallocated in the dtor for this class.) |
| 225 |
| 226 if (text_track) |
| 227 text_track_map_.insert(std::make_pair(itr->first, text_track.release())); |
| 228 } |
| 229 |
| 201 cluster_parser_.reset(new WebMClusterParser( | 230 cluster_parser_.reset(new WebMClusterParser( |
| 202 info_parser.timecode_scale(), | 231 info_parser.timecode_scale(), |
| 203 tracks_parser.audio_track_num(), | 232 tracks_parser.audio_track_num(), |
| 204 tracks_parser.video_track_num(), | 233 tracks_parser.video_track_num(), |
| 205 tracks_parser.text_tracks(), | 234 text_tracks, |
| 206 tracks_parser.ignored_tracks(), | 235 tracks_parser.ignored_tracks(), |
| 207 tracks_parser.audio_encryption_key_id(), | 236 tracks_parser.audio_encryption_key_id(), |
| 208 tracks_parser.video_encryption_key_id(), | 237 tracks_parser.video_encryption_key_id(), |
| 209 log_cb_)); | 238 log_cb_)); |
| 210 | 239 |
| 211 ChangeState(kParsingClusters); | 240 ChangeState(kParsingClusters); |
| 212 | 241 |
| 213 if (!init_cb_.is_null()) { | 242 if (!init_cb_.is_null()) { |
| 214 init_cb_.Run(true, duration); | 243 init_cb_.Run(true, duration); |
| 215 init_cb_.Reset(); | 244 init_cb_.Reset(); |
| (...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 261 new_segment_cb_.Run(cluster_start_time); | 290 new_segment_cb_.Run(cluster_start_time); |
| 262 waiting_for_buffers_ = false; | 291 waiting_for_buffers_ = false; |
| 263 } | 292 } |
| 264 | 293 |
| 265 if (!audio_buffers.empty() && !audio_cb_.Run(audio_buffers)) | 294 if (!audio_buffers.empty() && !audio_cb_.Run(audio_buffers)) |
| 266 return -1; | 295 return -1; |
| 267 | 296 |
| 268 if (!video_buffers.empty() && !video_cb_.Run(video_buffers)) | 297 if (!video_buffers.empty() && !video_cb_.Run(video_buffers)) |
| 269 return -1; | 298 return -1; |
| 270 | 299 |
| 300 WebMClusterParser::TextTrackIterator text_track_iter = |
| 301 cluster_parser_->CreateTextTrackIterator(); |
| 302 |
| 303 int text_track_num; |
| 304 const BufferQueue* text_buffers; |
| 305 |
| 306 while (text_track_iter(&text_track_num, &text_buffers)) { |
| 307 TextTrackMap::iterator find_result = text_track_map_.find(text_track_num); |
| 308 |
| 309 if (find_result == text_track_map_.end()) |
| 310 continue; |
| 311 |
| 312 TextTrack* const text_track = find_result->second; |
| 313 |
| 314 if (!text_buffers->empty() && !text_cb_.Run(text_track, *text_buffers)) |
| 315 return -1; |
| 316 } |
| 317 |
| 271 if (cluster_ended) | 318 if (cluster_ended) |
| 272 end_of_segment_cb_.Run(); | 319 end_of_segment_cb_.Run(); |
| 273 | 320 |
| 274 return bytes_parsed; | 321 return bytes_parsed; |
| 275 } | 322 } |
| 276 | 323 |
| 277 void WebMStreamParser::FireNeedKey(const std::string& key_id) { | 324 void WebMStreamParser::FireNeedKey(const std::string& key_id) { |
| 278 int key_id_size = key_id.size(); | 325 int key_id_size = key_id.size(); |
| 279 DCHECK_GT(key_id_size, 0); | 326 DCHECK_GT(key_id_size, 0); |
| 280 scoped_ptr<uint8[]> key_id_array(new uint8[key_id_size]); | 327 scoped_ptr<uint8[]> key_id_array(new uint8[key_id_size]); |
| 281 memcpy(key_id_array.get(), key_id.data(), key_id_size); | 328 memcpy(key_id_array.get(), key_id.data(), key_id_size); |
| 282 need_key_cb_.Run(kWebMEncryptInitDataType, key_id_array.Pass(), key_id_size); | 329 need_key_cb_.Run(kWebMEncryptInitDataType, key_id_array.Pass(), key_id_size); |
| 283 } | 330 } |
| 284 | 331 |
| 285 } // namespace media | 332 } // namespace media |
| OLD | NEW |