| 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 #include "media/formats/webm/webm_cluster_parser.h" | 5 #include "media/formats/webm/webm_cluster_parser.h" |
| 6 | 6 |
| 7 #include <vector> | 7 #include <vector> |
| 8 | 8 |
| 9 #include "base/logging.h" | 9 #include "base/logging.h" |
| 10 #include "base/sys_byteorder.h" | 10 #include "base/sys_byteorder.h" |
| 11 #include "media/base/buffers.h" | 11 #include "media/base/buffers.h" |
| 12 #include "media/base/decrypt_config.h" | 12 #include "media/base/decrypt_config.h" |
| 13 #include "media/filters/webvtt_util.h" | 13 #include "media/filters/webvtt_util.h" |
| 14 #include "media/formats/webm/webm_constants.h" | 14 #include "media/formats/webm/webm_constants.h" |
| 15 #include "media/formats/webm/webm_crypto_helpers.h" | 15 #include "media/formats/webm/webm_crypto_helpers.h" |
| 16 #include "media/formats/webm/webm_webvtt_parser.h" | 16 #include "media/formats/webm/webm_webvtt_parser.h" |
| 17 | 17 |
| 18 // Arbitrarily-chosen numbers to estimate the duration of a buffer if none is |
| 19 // set and there is not enough information to get a better estimate. |
| 20 // TODO(wolenetz/acolwell): Parse audio codebook to determine missing audio |
| 21 // frame durations. See http://crbug.com/351166. |
| 22 static int kDefaultAudioBufferDurationInMs = 23; // Common 1k samples @44.1kHz |
| 23 static int kDefaultVideoBufferDurationInMs = 42; // Low 24fps to reduce stalls |
| 24 |
| 18 namespace media { | 25 namespace media { |
| 19 | 26 |
| 20 WebMClusterParser::WebMClusterParser( | 27 WebMClusterParser::WebMClusterParser( |
| 21 int64 timecode_scale, int audio_track_num, int video_track_num, | 28 int64 timecode_scale, |
| 29 int audio_track_num, |
| 30 base::TimeDelta audio_default_duration, |
| 31 int video_track_num, |
| 32 base::TimeDelta video_default_duration, |
| 22 const WebMTracksParser::TextTracks& text_tracks, | 33 const WebMTracksParser::TextTracks& text_tracks, |
| 23 const std::set<int64>& ignored_tracks, | 34 const std::set<int64>& ignored_tracks, |
| 24 const std::string& audio_encryption_key_id, | 35 const std::string& audio_encryption_key_id, |
| 25 const std::string& video_encryption_key_id, | 36 const std::string& video_encryption_key_id, |
| 26 const LogCB& log_cb) | 37 const LogCB& log_cb) |
| 27 : timecode_multiplier_(timecode_scale / 1000.0), | 38 : timecode_multiplier_(timecode_scale / 1000.0), |
| 28 ignored_tracks_(ignored_tracks), | 39 ignored_tracks_(ignored_tracks), |
| 29 audio_encryption_key_id_(audio_encryption_key_id), | 40 audio_encryption_key_id_(audio_encryption_key_id), |
| 30 video_encryption_key_id_(video_encryption_key_id), | 41 video_encryption_key_id_(video_encryption_key_id), |
| 31 parser_(kWebMIdCluster, this), | 42 parser_(kWebMIdCluster, this), |
| 32 last_block_timecode_(-1), | 43 last_block_timecode_(-1), |
| 33 block_data_size_(-1), | 44 block_data_size_(-1), |
| 34 block_duration_(-1), | 45 block_duration_(-1), |
| 35 block_add_id_(-1), | 46 block_add_id_(-1), |
| 36 block_additional_data_size_(-1), | 47 block_additional_data_size_(-1), |
| 37 discard_padding_(-1), | 48 discard_padding_(-1), |
| 38 cluster_timecode_(-1), | 49 cluster_timecode_(-1), |
| 39 cluster_start_time_(kNoTimestamp()), | 50 cluster_start_time_(kNoTimestamp()), |
| 40 cluster_ended_(false), | 51 cluster_ended_(false), |
| 41 audio_(audio_track_num, false), | 52 audio_(audio_track_num, false, audio_default_duration), |
| 42 video_(video_track_num, true), | 53 video_(video_track_num, true, video_default_duration), |
| 43 log_cb_(log_cb) { | 54 log_cb_(log_cb) { |
| 44 for (WebMTracksParser::TextTracks::const_iterator it = text_tracks.begin(); | 55 for (WebMTracksParser::TextTracks::const_iterator it = text_tracks.begin(); |
| 45 it != text_tracks.end(); | 56 it != text_tracks.end(); |
| 46 ++it) { | 57 ++it) { |
| 47 text_track_map_.insert(std::make_pair(it->first, Track(it->first, false))); | 58 text_track_map_.insert(std::make_pair( |
| 59 it->first, Track(it->first, false, kNoTimestamp()))); |
| 48 } | 60 } |
| 49 } | 61 } |
| 50 | 62 |
| 51 WebMClusterParser::~WebMClusterParser() {} | 63 WebMClusterParser::~WebMClusterParser() {} |
| 52 | 64 |
| 53 void WebMClusterParser::Reset() { | 65 void WebMClusterParser::Reset() { |
| 54 last_block_timecode_ = -1; | 66 last_block_timecode_ = -1; |
| 55 cluster_timecode_ = -1; | 67 cluster_timecode_ = -1; |
| 56 cluster_start_time_ = kNoTimestamp(); | 68 cluster_start_time_ = kNoTimestamp(); |
| 57 cluster_ended_ = false; | 69 cluster_ended_ = false; |
| 58 parser_.Reset(); | 70 parser_.Reset(); |
| 59 audio_.Reset(); | 71 audio_.Reset(); |
| 60 video_.Reset(); | 72 video_.Reset(); |
| 61 ResetTextTracks(); | 73 ResetTextTracks(); |
| 62 } | 74 } |
| 63 | 75 |
| 64 int WebMClusterParser::Parse(const uint8* buf, int size) { | 76 int WebMClusterParser::Parse(const uint8* buf, int size) { |
| 65 audio_.Reset(); | 77 audio_.ClearBuffersButKeepLastIfMissingDuration(); |
| 66 video_.Reset(); | 78 video_.ClearBuffersButKeepLastIfMissingDuration(); |
| 67 ResetTextTracks(); | 79 ResetTextTracks(); |
| 68 | 80 |
| 69 int result = parser_.Parse(buf, size); | 81 int result = parser_.Parse(buf, size); |
| 70 | 82 |
| 71 if (result < 0) { | 83 if (result < 0) { |
| 72 cluster_ended_ = false; | 84 cluster_ended_ = false; |
| 73 return result; | 85 return result; |
| 74 } | 86 } |
| 75 | 87 |
| 76 cluster_ended_ = parser_.IsParsingComplete(); | 88 cluster_ended_ = parser_.IsParsingComplete(); |
| (...skipping 15 matching lines...) Expand all Loading... |
| 92 // call. | 104 // call. |
| 93 parser_.Reset(); | 105 parser_.Reset(); |
| 94 | 106 |
| 95 last_block_timecode_ = -1; | 107 last_block_timecode_ = -1; |
| 96 cluster_timecode_ = -1; | 108 cluster_timecode_ = -1; |
| 97 } | 109 } |
| 98 | 110 |
| 99 return result; | 111 return result; |
| 100 } | 112 } |
| 101 | 113 |
| 114 const WebMClusterParser::BufferQueue& WebMClusterParser::GetAudioBuffers() { |
| 115 if (cluster_ended_) |
| 116 audio_.ApplyDurationDefaultOrEstimateIfNeeded(); |
| 117 return audio_.buffers(); |
| 118 } |
| 119 |
| 120 const WebMClusterParser::BufferQueue& WebMClusterParser::GetVideoBuffers() { |
| 121 if (cluster_ended_) |
| 122 video_.ApplyDurationDefaultOrEstimateIfNeeded(); |
| 123 return video_.buffers(); |
| 124 } |
| 125 |
| 102 const WebMClusterParser::TextBufferQueueMap& | 126 const WebMClusterParser::TextBufferQueueMap& |
| 103 WebMClusterParser::GetTextBuffers() { | 127 WebMClusterParser::GetTextBuffers() { |
| 104 // Translate our |text_track_map_| into |text_buffers_map_|, inserting rows in | 128 // Translate our |text_track_map_| into |text_buffers_map_|, inserting rows in |
| 105 // the output only for non-empty text buffer queues in |text_track_map_|. | 129 // the output only for non-empty text buffer queues in |text_track_map_|. |
| 106 text_buffers_map_.clear(); | 130 text_buffers_map_.clear(); |
| 107 for (TextTrackMap::const_iterator itr = text_track_map_.begin(); | 131 for (TextTrackMap::const_iterator itr = text_track_map_.begin(); |
| 108 itr != text_track_map_.end(); | 132 itr != text_track_map_.end(); |
| 109 ++itr) { | 133 ++itr) { |
| 134 // Per OnBlock(), all text buffers should already have valid durations, so |
| 135 // there is no need to call |
| 136 // itr->second.ApplyDurationDefaultOrEstimateIfNeeded() here. |
| 110 const BufferQueue& text_buffers = itr->second.buffers(); | 137 const BufferQueue& text_buffers = itr->second.buffers(); |
| 111 if (!text_buffers.empty()) | 138 if (!text_buffers.empty()) |
| 112 text_buffers_map_.insert(std::make_pair(itr->first, text_buffers)); | 139 text_buffers_map_.insert(std::make_pair(itr->first, text_buffers)); |
| 113 } | 140 } |
| 114 | 141 |
| 115 return text_buffers_map_; | 142 return text_buffers_map_; |
| 116 } | 143 } |
| 117 | 144 |
| 118 WebMParserClient* WebMClusterParser::OnListStart(int id) { | 145 WebMParserClient* WebMClusterParser::OnListStart(int id) { |
| 119 if (id == kWebMIdCluster) { | 146 if (id == kWebMIdCluster) { |
| (...skipping 263 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 383 } | 410 } |
| 384 | 411 |
| 385 if (discard_padding != 0) { | 412 if (discard_padding != 0) { |
| 386 buffer->set_discard_padding(base::TimeDelta::FromMicroseconds( | 413 buffer->set_discard_padding(base::TimeDelta::FromMicroseconds( |
| 387 discard_padding / 1000)); | 414 discard_padding / 1000)); |
| 388 } | 415 } |
| 389 | 416 |
| 390 return track->AddBuffer(buffer); | 417 return track->AddBuffer(buffer); |
| 391 } | 418 } |
| 392 | 419 |
| 393 WebMClusterParser::Track::Track(int track_num, bool is_video) | 420 WebMClusterParser::Track::Track(int track_num, bool is_video, |
| 421 base::TimeDelta default_duration) |
| 394 : track_num_(track_num), | 422 : track_num_(track_num), |
| 395 is_video_(is_video) { | 423 is_video_(is_video), |
| 424 default_duration_(default_duration), |
| 425 estimated_next_frame_duration_(kNoTimestamp()) { |
| 426 DCHECK(default_duration_ == kNoTimestamp() || |
| 427 default_duration_ > base::TimeDelta()); |
| 396 } | 428 } |
| 397 | 429 |
| 398 WebMClusterParser::Track::~Track() {} | 430 WebMClusterParser::Track::~Track() {} |
| 399 | 431 |
| 400 bool WebMClusterParser::Track::AddBuffer( | 432 bool WebMClusterParser::Track::AddBuffer( |
| 401 const scoped_refptr<StreamParserBuffer>& buffer) { | 433 const scoped_refptr<StreamParserBuffer>& buffer) { |
| 402 DVLOG(2) << "AddBuffer() : " << track_num_ | 434 DVLOG(2) << "AddBuffer() : " << track_num_ |
| 403 << " ts " << buffer->timestamp().InSecondsF() | 435 << " ts " << buffer->timestamp().InSecondsF() |
| 404 << " dur " << buffer->duration().InSecondsF() | 436 << " dur " << buffer->duration().InSecondsF() |
| 405 << " kf " << buffer->IsKeyframe() | 437 << " kf " << buffer->IsKeyframe() |
| 406 << " size " << buffer->data_size(); | 438 << " size " << buffer->data_size(); |
| 407 | 439 |
| 408 buffers_.push_back(buffer); | 440 if (last_added_buffer_missing_duration_) { |
| 409 return true; | 441 base::TimeDelta derived_duration = |
| 442 buffer->timestamp() - last_added_buffer_missing_duration_->timestamp(); |
| 443 last_added_buffer_missing_duration_->set_duration(derived_duration); |
| 444 |
| 445 DVLOG(2) << "AddBuffer() : applied derived duration to held-back buffer : " |
| 446 << " ts " |
| 447 << last_added_buffer_missing_duration_->timestamp().InSecondsF() |
| 448 << " dur " |
| 449 << last_added_buffer_missing_duration_->duration().InSecondsF() |
| 450 << " kf " << last_added_buffer_missing_duration_->IsKeyframe() |
| 451 << " size " << last_added_buffer_missing_duration_->data_size(); |
| 452 scoped_refptr<StreamParserBuffer> updated_buffer = |
| 453 last_added_buffer_missing_duration_; |
| 454 last_added_buffer_missing_duration_ = NULL; |
| 455 if (!QueueBuffer(updated_buffer)) |
| 456 return false; |
| 457 } |
| 458 |
| 459 if (buffer->duration() == kNoTimestamp()) { |
| 460 last_added_buffer_missing_duration_ = buffer; |
| 461 DVLOG(2) << "AddBuffer() : holding back buffer that is missing duration"; |
| 462 return true; |
| 463 } |
| 464 |
| 465 return QueueBuffer(buffer); |
| 466 } |
| 467 |
| 468 void WebMClusterParser::Track::ApplyDurationDefaultOrEstimateIfNeeded() { |
| 469 if (!last_added_buffer_missing_duration_) |
| 470 return; |
| 471 |
| 472 last_added_buffer_missing_duration_->set_duration( |
| 473 GetDurationDefaultOrEstimate()); |
| 474 |
| 475 DVLOG(2) << "ApplyDurationDefaultOrEstimateIfNeeded() : new dur : " |
| 476 << " ts " |
| 477 << last_added_buffer_missing_duration_->timestamp().InSecondsF() |
| 478 << " dur " |
| 479 << last_added_buffer_missing_duration_->duration().InSecondsF() |
| 480 << " kf " << last_added_buffer_missing_duration_->IsKeyframe() |
| 481 << " size " << last_added_buffer_missing_duration_->data_size(); |
| 482 |
| 483 // Don't use the applied duration as a future estimation (don't use |
| 484 // QueueBuffer() here.) |
| 485 buffers_.push_back(last_added_buffer_missing_duration_); |
| 486 last_added_buffer_missing_duration_ = NULL; |
| 487 } |
| 488 |
| 489 void WebMClusterParser::Track::ClearBuffersButKeepLastIfMissingDuration() { |
| 490 // Note that |estimated_next_frame_duration_| is not reset, so it can be |
| 491 // reused on subsequent buffers added to this instance. |
| 492 buffers_.clear(); |
| 410 } | 493 } |
| 411 | 494 |
| 412 void WebMClusterParser::Track::Reset() { | 495 void WebMClusterParser::Track::Reset() { |
| 413 buffers_.clear(); | 496 ClearBuffersButKeepLastIfMissingDuration(); |
| 497 last_added_buffer_missing_duration_ = NULL; |
| 414 } | 498 } |
| 415 | 499 |
| 416 bool WebMClusterParser::Track::IsKeyframe(const uint8* data, int size) const { | 500 bool WebMClusterParser::Track::IsKeyframe(const uint8* data, int size) const { |
| 417 // For now, assume that all blocks are keyframes for datatypes other than | 501 // For now, assume that all blocks are keyframes for datatypes other than |
| 418 // video. This is a valid assumption for Vorbis, WebVTT, & Opus. | 502 // video. This is a valid assumption for Vorbis, WebVTT, & Opus. |
| 419 if (!is_video_) | 503 if (!is_video_) |
| 420 return true; | 504 return true; |
| 421 | 505 |
| 422 // Make sure the block is big enough for the minimal keyframe header size. | 506 // Make sure the block is big enough for the minimal keyframe header size. |
| 423 if (size < 7) | 507 if (size < 7) |
| 424 return false; | 508 return false; |
| 425 | 509 |
| 426 // The LSb of the first byte must be a 0 for a keyframe. | 510 // The LSb of the first byte must be a 0 for a keyframe. |
| 427 // http://tools.ietf.org/html/rfc6386 Section 19.1 | 511 // http://tools.ietf.org/html/rfc6386 Section 19.1 |
| 428 if ((data[0] & 0x01) != 0) | 512 if ((data[0] & 0x01) != 0) |
| 429 return false; | 513 return false; |
| 430 | 514 |
| 431 // Verify VP8 keyframe startcode. | 515 // Verify VP8 keyframe startcode. |
| 432 // http://tools.ietf.org/html/rfc6386 Section 19.1 | 516 // http://tools.ietf.org/html/rfc6386 Section 19.1 |
| 433 if (data[3] != 0x9d || data[4] != 0x01 || data[5] != 0x2a) | 517 if (data[3] != 0x9d || data[4] != 0x01 || data[5] != 0x2a) |
| 434 return false; | 518 return false; |
| 435 | 519 |
| 436 return true; | 520 return true; |
| 437 } | 521 } |
| 438 | 522 |
| 523 bool WebMClusterParser::Track::QueueBuffer( |
| 524 const scoped_refptr<StreamParserBuffer>& buffer) { |
| 525 DCHECK(!last_added_buffer_missing_duration_); |
| 526 base::TimeDelta duration = buffer->duration(); |
| 527 if (duration < base::TimeDelta() || duration == kNoTimestamp()) { |
| 528 DVLOG(2) << "QueueBuffer() : Invalid buffer duration: " |
| 529 << duration.InSecondsF(); |
| 530 return false; |
| 531 } |
| 532 |
| 533 estimated_next_frame_duration_ = std::max(duration, |
| 534 estimated_next_frame_duration_); |
| 535 buffers_.push_back(buffer); |
| 536 return true; |
| 537 } |
| 538 |
| 539 base::TimeDelta WebMClusterParser::Track::GetDurationDefaultOrEstimate() { |
| 540 base::TimeDelta duration = default_duration_; |
| 541 if (duration != kNoTimestamp()) { |
| 542 DVLOG(3) << __FUNCTION__ << " : using TrackEntry DefaultDuration"; |
| 543 } else if (estimated_next_frame_duration_ != kNoTimestamp()) { |
| 544 DVLOG(3) << __FUNCTION__ << " : using estimated duration"; |
| 545 duration = estimated_next_frame_duration_; |
| 546 } else { |
| 547 DVLOG(3) << __FUNCTION__ << " : using hardcoded default duration"; |
| 548 if (is_video_) { |
| 549 duration = base::TimeDelta::FromMilliseconds( |
| 550 kDefaultVideoBufferDurationInMs); |
| 551 } else { |
| 552 duration = base::TimeDelta::FromMilliseconds( |
| 553 kDefaultAudioBufferDurationInMs); |
| 554 } |
| 555 } |
| 556 |
| 557 DCHECK(duration > base::TimeDelta()); |
| 558 DCHECK(duration != kNoTimestamp()); |
| 559 return duration; |
| 560 } |
| 561 |
| 439 void WebMClusterParser::ResetTextTracks() { | 562 void WebMClusterParser::ResetTextTracks() { |
| 440 text_buffers_map_.clear(); | 563 text_buffers_map_.clear(); |
| 441 for (TextTrackMap::iterator it = text_track_map_.begin(); | 564 for (TextTrackMap::iterator it = text_track_map_.begin(); |
| 442 it != text_track_map_.end(); | 565 it != text_track_map_.end(); |
| 443 ++it) { | 566 ++it) { |
| 444 it->second.Reset(); | 567 it->second.Reset(); |
| 445 } | 568 } |
| 446 } | 569 } |
| 447 | 570 |
| 448 WebMClusterParser::Track* | 571 WebMClusterParser::Track* |
| 449 WebMClusterParser::FindTextTrack(int track_num) { | 572 WebMClusterParser::FindTextTrack(int track_num) { |
| 450 const TextTrackMap::iterator it = text_track_map_.find(track_num); | 573 const TextTrackMap::iterator it = text_track_map_.find(track_num); |
| 451 | 574 |
| 452 if (it == text_track_map_.end()) | 575 if (it == text_track_map_.end()) |
| 453 return NULL; | 576 return NULL; |
| 454 | 577 |
| 455 return &it->second; | 578 return &it->second; |
| 456 } | 579 } |
| 457 | 580 |
| 458 } // namespace media | 581 } // namespace media |
| OLD | NEW |