Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(181)

Side by Side Diff: media/filters/ffmpeg_demuxer.cc

Issue 335273002: Fix seeking when the start time is non-zero. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Rebase. Remove vector. Created 6 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « media/filters/ffmpeg_demuxer.h ('k') | media/filters/ffmpeg_demuxer_unittest.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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/filters/ffmpeg_demuxer.h" 5 #include "media/filters/ffmpeg_demuxer.h"
6 6
7 #include <algorithm> 7 #include <algorithm>
8 #include <string> 8 #include <string>
9 9
10 #include "base/base64.h" 10 #include "base/base64.h"
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after
48 } 48 }
49 49
50 return base::Time(); 50 return base::Time();
51 } 51 }
52 52
53 static base::TimeDelta FramesToTimeDelta(int frames, double sample_rate) { 53 static base::TimeDelta FramesToTimeDelta(int frames, double sample_rate) {
54 return base::TimeDelta::FromMicroseconds( 54 return base::TimeDelta::FromMicroseconds(
55 frames * base::Time::kMicrosecondsPerSecond / sample_rate); 55 frames * base::Time::kMicrosecondsPerSecond / sample_rate);
56 } 56 }
57 57
58 static base::TimeDelta ExtractStartTime(AVFormatContext* format_context,
59 AVStream* stream) {
60 if (stream->start_time == static_cast<int64_t>(AV_NOPTS_VALUE))
61 return kNoTimestamp();
62
63 // First try to use the |start_time| value directly.
64 base::TimeDelta start_time =
65 ConvertFromTimeBase(stream->time_base, stream->start_time);
66
67 // Next see if the first buffered pts value is usable.
68 if (stream->pts_buffer[0] != static_cast<int64_t>(AV_NOPTS_VALUE)) {
69 const base::TimeDelta buffered_pts =
70 ConvertFromTimeBase(stream->time_base, stream->pts_buffer[0]);
71 if (buffered_pts < start_time)
72 start_time = buffered_pts;
73 }
74
75 if (!format_context->packet_buffer)
76 return start_time;
77
78 // Compare against the packets buffered during avformat_find_stream_info().
79 struct AVPacketList* packet_buffer = format_context->packet_buffer;
80 while (packet_buffer != format_context->packet_buffer_end) {
81 if (packet_buffer->pkt.stream_index == stream->index) {
82 const base::TimeDelta packet_pts =
83 ConvertFromTimeBase(stream->time_base, packet_buffer->pkt.pts);
84 if (packet_pts < start_time)
85 start_time = packet_pts;
86 }
87 packet_buffer = packet_buffer->next;
88 }
89
90 // NOTE: Do not use AVStream->first_dts since |start_time| should be a
91 // presentation timestamp.
92 return start_time;
93 }
94
58 // 95 //
59 // FFmpegDemuxerStream 96 // FFmpegDemuxerStream
60 // 97 //
61 FFmpegDemuxerStream::FFmpegDemuxerStream( 98 FFmpegDemuxerStream::FFmpegDemuxerStream(
62 FFmpegDemuxer* demuxer, 99 FFmpegDemuxer* demuxer,
63 AVStream* stream) 100 AVStream* stream,
101 bool discard_negative_timestamps)
64 : demuxer_(demuxer), 102 : demuxer_(demuxer),
65 task_runner_(base::MessageLoopProxy::current()), 103 task_runner_(base::MessageLoopProxy::current()),
66 stream_(stream), 104 stream_(stream),
67 type_(UNKNOWN), 105 type_(UNKNOWN),
68 end_of_stream_(false), 106 end_of_stream_(false),
69 last_packet_timestamp_(kNoTimestamp()), 107 last_packet_timestamp_(kNoTimestamp()),
70 bitstream_converter_enabled_(false) { 108 bitstream_converter_enabled_(false),
109 discard_negative_timestamps_(discard_negative_timestamps) {
71 DCHECK(demuxer_); 110 DCHECK(demuxer_);
72 111
73 bool is_encrypted = false; 112 bool is_encrypted = false;
74 113
75 // Determine our media format. 114 // Determine our media format.
76 switch (stream->codec->codec_type) { 115 switch (stream->codec->codec_type) {
77 case AVMEDIA_TYPE_AUDIO: 116 case AVMEDIA_TYPE_AUDIO:
78 type_ = AUDIO; 117 type_ = AUDIO;
79 AVStreamToAudioDecoderConfig(stream, &audio_config_, true); 118 AVStreamToAudioDecoderConfig(stream, &audio_config_, true);
80 is_encrypted = audio_config_.is_encrypted(); 119 is_encrypted = audio_config_.is_encrypted();
(...skipping 138 matching lines...) Expand 10 before | Expand all | Expand 10 after
219 audio_decoder_config().samples_per_second(); 258 audio_decoder_config().samples_per_second();
220 buffer->set_discard_padding(std::make_pair( 259 buffer->set_discard_padding(std::make_pair(
221 FramesToTimeDelta(discard_front_samples, samples_per_second), 260 FramesToTimeDelta(discard_front_samples, samples_per_second),
222 FramesToTimeDelta(discard_end_samples, samples_per_second))); 261 FramesToTimeDelta(discard_end_samples, samples_per_second)));
223 } 262 }
224 263
225 if (decrypt_config) 264 if (decrypt_config)
226 buffer->set_decrypt_config(decrypt_config.Pass()); 265 buffer->set_decrypt_config(decrypt_config.Pass());
227 } 266 }
228 267
229 buffer->set_timestamp(ConvertStreamTimestamp( 268 buffer->set_duration(
230 stream_->time_base, packet->pts)); 269 ConvertStreamTimestamp(stream_->time_base, packet->duration));
231 buffer->set_duration(ConvertStreamTimestamp( 270
232 stream_->time_base, packet->duration)); 271 // Note: If pts is AV_NOPTS_VALUE, stream_timestamp will be kNoTimestamp().
233 if (buffer->timestamp() != kNoTimestamp() && 272 const base::TimeDelta stream_timestamp =
234 last_packet_timestamp_ != kNoTimestamp() && 273 ConvertStreamTimestamp(stream_->time_base, packet->pts);
235 last_packet_timestamp_ < buffer->timestamp()) { 274
236 buffered_ranges_.Add(last_packet_timestamp_, buffer->timestamp()); 275 if (stream_timestamp != kNoTimestamp()) {
237 demuxer_->NotifyBufferingChanged(); 276 buffer->set_timestamp(stream_timestamp - demuxer_->start_time());
277
278 // If enabled, mark packets with negative timestamps for post-decode
279 // discard.
280 if (discard_negative_timestamps_ && stream_timestamp < base::TimeDelta()) {
281 if (stream_timestamp + buffer->duration() < base::TimeDelta()) {
282 // Discard the entier packet if it's entirely before zero.
283 buffer->set_discard_padding(
284 std::make_pair(kInfiniteDuration(), base::TimeDelta()));
285 } else {
286 // Only discard part of the frame if it overlaps zero.
287 buffer->set_discard_padding(
288 std::make_pair(-stream_timestamp, base::TimeDelta()));
289 }
290 }
291
292 if (last_packet_timestamp_ != kNoTimestamp() &&
293 last_packet_timestamp_ < buffer->timestamp()) {
294 buffered_ranges_.Add(last_packet_timestamp_, buffer->timestamp());
295 demuxer_->NotifyBufferingChanged();
296 }
297
298 // The demuxer should always output positive timestamps.
299 DCHECK(buffer->timestamp() >= base::TimeDelta());
300 } else {
301 buffer->set_timestamp(kNoTimestamp());
238 } 302 }
303
304 // TODO(dalecurtis): This allows transitions from <valid ts> -> <no timestamp>
305 // which shouldn't be allowed. See http://crbug.com/384532
239 last_packet_timestamp_ = buffer->timestamp(); 306 last_packet_timestamp_ = buffer->timestamp();
240 307
241 buffer_queue_.Push(buffer); 308 buffer_queue_.Push(buffer);
242 SatisfyPendingRead(); 309 SatisfyPendingRead();
243 } 310 }
244 311
245 void FFmpegDemuxerStream::SetEndOfStream() { 312 void FFmpegDemuxerStream::SetEndOfStream() {
246 DCHECK(task_runner_->BelongsToCurrentThread()); 313 DCHECK(task_runner_->BelongsToCurrentThread());
247 end_of_stream_ = true; 314 end_of_stream_ = true;
248 SatisfyPendingRead(); 315 SatisfyPendingRead();
(...skipping 12 matching lines...) Expand all
261 buffer_queue_.Clear(); 328 buffer_queue_.Clear();
262 if (!read_cb_.is_null()) { 329 if (!read_cb_.is_null()) {
263 base::ResetAndReturn(&read_cb_).Run( 330 base::ResetAndReturn(&read_cb_).Run(
264 DemuxerStream::kOk, DecoderBuffer::CreateEOSBuffer()); 331 DemuxerStream::kOk, DecoderBuffer::CreateEOSBuffer());
265 } 332 }
266 demuxer_ = NULL; 333 demuxer_ = NULL;
267 stream_ = NULL; 334 stream_ = NULL;
268 end_of_stream_ = true; 335 end_of_stream_ = true;
269 } 336 }
270 337
271 base::TimeDelta FFmpegDemuxerStream::duration() {
272 return duration_;
273 }
274
275 DemuxerStream::Type FFmpegDemuxerStream::type() { 338 DemuxerStream::Type FFmpegDemuxerStream::type() {
276 DCHECK(task_runner_->BelongsToCurrentThread()); 339 DCHECK(task_runner_->BelongsToCurrentThread());
277 return type_; 340 return type_;
278 } 341 }
279 342
280 void FFmpegDemuxerStream::Read(const ReadCB& read_cb) { 343 void FFmpegDemuxerStream::Read(const ReadCB& read_cb) {
281 DCHECK(task_runner_->BelongsToCurrentThread()); 344 DCHECK(task_runner_->BelongsToCurrentThread());
282 CHECK(read_cb_.is_null()) << "Overlapping reads are not supported"; 345 CHECK(read_cb_.is_null()) << "Overlapping reads are not supported";
283 read_cb_ = BindToCurrentLoop(read_cb); 346 read_cb_ = BindToCurrentLoop(read_cb);
284 347
(...skipping 124 matching lines...) Expand 10 before | Expand all | Expand 10 after
409 const scoped_refptr<MediaLog>& media_log) 472 const scoped_refptr<MediaLog>& media_log)
410 : host_(NULL), 473 : host_(NULL),
411 task_runner_(task_runner), 474 task_runner_(task_runner),
412 blocking_thread_("FFmpegDemuxer"), 475 blocking_thread_("FFmpegDemuxer"),
413 pending_read_(false), 476 pending_read_(false),
414 pending_seek_(false), 477 pending_seek_(false),
415 data_source_(data_source), 478 data_source_(data_source),
416 media_log_(media_log), 479 media_log_(media_log),
417 bitrate_(0), 480 bitrate_(0),
418 start_time_(kNoTimestamp()), 481 start_time_(kNoTimestamp()),
482 preferred_stream_for_seeking_(-1, kNoTimestamp()),
483 fallback_stream_for_seeking_(-1, kNoTimestamp()),
419 liveness_(LIVENESS_UNKNOWN), 484 liveness_(LIVENESS_UNKNOWN),
420 text_enabled_(false), 485 text_enabled_(false),
421 duration_known_(false), 486 duration_known_(false),
422 need_key_cb_(need_key_cb), 487 need_key_cb_(need_key_cb),
423 weak_factory_(this) { 488 weak_factory_(this) {
424 DCHECK(task_runner_.get()); 489 DCHECK(task_runner_.get());
425 DCHECK(data_source_); 490 DCHECK(data_source_);
426 } 491 }
427 492
428 FFmpegDemuxer::~FFmpegDemuxer() {} 493 FFmpegDemuxer::~FFmpegDemuxer() {}
429 494
430 void FFmpegDemuxer::Stop(const base::Closure& callback) { 495 void FFmpegDemuxer::Stop(const base::Closure& callback) {
431 DCHECK(task_runner_->BelongsToCurrentThread()); 496 DCHECK(task_runner_->BelongsToCurrentThread());
432 url_protocol_->Abort(); 497 url_protocol_->Abort();
433 data_source_->Stop( 498 data_source_->Stop(
434 BindToCurrentLoop(base::Bind(&FFmpegDemuxer::OnDataSourceStopped, 499 BindToCurrentLoop(base::Bind(&FFmpegDemuxer::OnDataSourceStopped,
435 weak_factory_.GetWeakPtr(), 500 weak_factory_.GetWeakPtr(),
436 BindToCurrentLoop(callback)))); 501 BindToCurrentLoop(callback))));
437 data_source_ = NULL; 502 data_source_ = NULL;
438 } 503 }
439 504
440 void FFmpegDemuxer::Seek(base::TimeDelta time, const PipelineStatusCB& cb) { 505 void FFmpegDemuxer::Seek(base::TimeDelta time, const PipelineStatusCB& cb) {
441 DCHECK(task_runner_->BelongsToCurrentThread()); 506 DCHECK(task_runner_->BelongsToCurrentThread());
442 CHECK(!pending_seek_); 507 CHECK(!pending_seek_);
443 508
444 // TODO(scherkus): Inspect |pending_read_| and cancel IO via |blocking_url_|, 509 // TODO(scherkus): Inspect |pending_read_| and cancel IO via |blocking_url_|,
445 // otherwise we can end up waiting for a pre-seek read to complete even though 510 // otherwise we can end up waiting for a pre-seek read to complete even though
446 // we know we're going to drop it on the floor. 511 // we know we're going to drop it on the floor.
447 512
448 // Always seek to a timestamp less than or equal to the desired timestamp. 513 // FFmpeg requires seeks to be adjusted according to the lowest starting time.
449 int flags = AVSEEK_FLAG_BACKWARD; 514 const base::TimeDelta seek_time = time + start_time_;
450 515
451 // Passing -1 as our stream index lets FFmpeg pick a default stream. FFmpeg 516 // Choose the preferred stream if |seek_time| occurs after its starting time,
452 // will attempt to use the lowest-index video stream, if present, followed by 517 // otherwise use the fallback stream.
453 // the lowest-index audio stream. 518 DCHECK(preferred_stream_for_seeking_.second != kNoTimestamp());
519 const int stream_index = seek_time >= preferred_stream_for_seeking_.second
520 ? preferred_stream_for_seeking_.first
521 : fallback_stream_for_seeking_.first;
522 DCHECK_NE(stream_index, -1);
523
524 const AVStream* seeking_stream =
525 glue_->format_context()->streams[stream_index];
526
454 pending_seek_ = true; 527 pending_seek_ = true;
455 base::PostTaskAndReplyWithResult( 528 base::PostTaskAndReplyWithResult(
456 blocking_thread_.message_loop_proxy().get(), 529 blocking_thread_.message_loop_proxy().get(),
457 FROM_HERE, 530 FROM_HERE,
458 base::Bind(&av_seek_frame, 531 base::Bind(&av_seek_frame,
459 glue_->format_context(), 532 glue_->format_context(),
460 -1, 533 seeking_stream->index,
461 time.InMicroseconds(), 534 ConvertToTimeBase(seeking_stream->time_base, seek_time),
462 flags), 535 // Always seek to a timestamp <= to the desired timestamp.
536 AVSEEK_FLAG_BACKWARD),
463 base::Bind( 537 base::Bind(
464 &FFmpegDemuxer::OnSeekFrameDone, weak_factory_.GetWeakPtr(), cb)); 538 &FFmpegDemuxer::OnSeekFrameDone, weak_factory_.GetWeakPtr(), cb));
465 } 539 }
466 540
467 void FFmpegDemuxer::Initialize(DemuxerHost* host, 541 void FFmpegDemuxer::Initialize(DemuxerHost* host,
468 const PipelineStatusCB& status_cb, 542 const PipelineStatusCB& status_cb,
469 bool enable_text_tracks) { 543 bool enable_text_tracks) {
470 DCHECK(task_runner_->BelongsToCurrentThread()); 544 DCHECK(task_runner_->BelongsToCurrentThread());
471 host_ = host; 545 host_ = host;
472 text_enabled_ = enable_text_tracks; 546 text_enabled_ = enable_text_tracks;
(...skipping 28 matching lines...) Expand all
501 DemuxerStream::Type type) const { 575 DemuxerStream::Type type) const {
502 StreamVector::const_iterator iter; 576 StreamVector::const_iterator iter;
503 for (iter = streams_.begin(); iter != streams_.end(); ++iter) { 577 for (iter = streams_.begin(); iter != streams_.end(); ++iter) {
504 if (*iter && (*iter)->type() == type) { 578 if (*iter && (*iter)->type() == type) {
505 return *iter; 579 return *iter;
506 } 580 }
507 } 581 }
508 return NULL; 582 return NULL;
509 } 583 }
510 584
511 base::TimeDelta FFmpegDemuxer::GetStartTime() const {
512 DCHECK(task_runner_->BelongsToCurrentThread());
513 return start_time_;
514 }
515
516 base::Time FFmpegDemuxer::GetTimelineOffset() const { 585 base::Time FFmpegDemuxer::GetTimelineOffset() const {
517 return timeline_offset_; 586 return timeline_offset_;
518 } 587 }
519 588
520 Demuxer::Liveness FFmpegDemuxer::GetLiveness() const { 589 Demuxer::Liveness FFmpegDemuxer::GetLiveness() const {
521 DCHECK(task_runner_->BelongsToCurrentThread()); 590 DCHECK(task_runner_->BelongsToCurrentThread());
522 return liveness_; 591 return liveness_;
523 } 592 }
524 593
525 void FFmpegDemuxer::AddTextStreams() { 594 void FFmpegDemuxer::AddTextStreams() {
(...skipping 95 matching lines...) Expand 10 before | Expand all | Expand 10 after
621 // partial playback. At least one audio or video stream must be playable. 690 // partial playback. At least one audio or video stream must be playable.
622 AVFormatContext* format_context = glue_->format_context(); 691 AVFormatContext* format_context = glue_->format_context();
623 streams_.resize(format_context->nb_streams); 692 streams_.resize(format_context->nb_streams);
624 693
625 AVStream* audio_stream = NULL; 694 AVStream* audio_stream = NULL;
626 AudioDecoderConfig audio_config; 695 AudioDecoderConfig audio_config;
627 696
628 AVStream* video_stream = NULL; 697 AVStream* video_stream = NULL;
629 VideoDecoderConfig video_config; 698 VideoDecoderConfig video_config;
630 699
700 // If available, |start_time_| will be set to the lowest stream start time.
701 start_time_ = kInfiniteDuration();
702
631 base::TimeDelta max_duration; 703 base::TimeDelta max_duration;
632 for (size_t i = 0; i < format_context->nb_streams; ++i) { 704 for (size_t i = 0; i < format_context->nb_streams; ++i) {
633 AVStream* stream = format_context->streams[i]; 705 AVStream* stream = format_context->streams[i];
634 AVCodecContext* codec_context = stream->codec; 706 const AVCodecContext* codec_context = stream->codec;
635 AVMediaType codec_type = codec_context->codec_type; 707 const AVMediaType codec_type = codec_context->codec_type;
708 bool discard_negative_timestamps = false;
636 709
637 if (codec_type == AVMEDIA_TYPE_AUDIO) { 710 if (codec_type == AVMEDIA_TYPE_AUDIO) {
638 if (audio_stream) 711 if (audio_stream)
639 continue; 712 continue;
640 713
641 // Log the codec detected, whether it is supported or not. 714 // Log the codec detected, whether it is supported or not.
642 UMA_HISTOGRAM_SPARSE_SLOWLY("Media.DetectedAudioCodec", 715 UMA_HISTOGRAM_SPARSE_SLOWLY("Media.DetectedAudioCodec",
643 codec_context->codec_id); 716 codec_context->codec_id);
644 // Ensure the codec is supported. IsValidConfig() also checks that the 717 // Ensure the codec is supported. IsValidConfig() also checks that the
645 // channel layout and sample format are valid. 718 // channel layout and sample format are valid.
646 AVStreamToAudioDecoderConfig(stream, &audio_config, false); 719 AVStreamToAudioDecoderConfig(stream, &audio_config, false);
647 if (!audio_config.IsValidConfig()) 720 if (!audio_config.IsValidConfig())
648 continue; 721 continue;
649 audio_stream = stream; 722 audio_stream = stream;
723
724 // Enable post-decode frame dropping for packets with negative timestamps
725 // as outlined in section A.2 in the Ogg Vorbis spec:
726 // http://xiph.org/vorbis/doc/Vorbis_I_spec.html
727 discard_negative_timestamps =
728 audio_config.codec() == kCodecVorbis &&
729 strcmp(glue_->format_context()->iformat->name, "ogg") == 0;
650 } else if (codec_type == AVMEDIA_TYPE_VIDEO) { 730 } else if (codec_type == AVMEDIA_TYPE_VIDEO) {
651 if (video_stream) 731 if (video_stream)
652 continue; 732 continue;
653 733
654 // Log the codec detected, whether it is supported or not. 734 // Log the codec detected, whether it is supported or not.
655 UMA_HISTOGRAM_SPARSE_SLOWLY("Media.DetectedVideoCodec", 735 UMA_HISTOGRAM_SPARSE_SLOWLY("Media.DetectedVideoCodec",
656 codec_context->codec_id); 736 codec_context->codec_id);
657 // Ensure the codec is supported. IsValidConfig() also checks that the 737 // Ensure the codec is supported. IsValidConfig() also checks that the
658 // frame size and visible size are valid. 738 // frame size and visible size are valid.
659 AVStreamToVideoDecoderConfig(stream, &video_config, false); 739 AVStreamToVideoDecoderConfig(stream, &video_config, false);
660 740
661 if (!video_config.IsValidConfig()) 741 if (!video_config.IsValidConfig())
662 continue; 742 continue;
663 video_stream = stream; 743 video_stream = stream;
664 } else if (codec_type == AVMEDIA_TYPE_SUBTITLE) { 744 } else if (codec_type == AVMEDIA_TYPE_SUBTITLE) {
665 if (codec_context->codec_id != AV_CODEC_ID_WEBVTT || !text_enabled_) { 745 if (codec_context->codec_id != AV_CODEC_ID_WEBVTT || !text_enabled_) {
666 continue; 746 continue;
667 } 747 }
668 } else { 748 } else {
669 continue; 749 continue;
670 } 750 }
671 751
672 streams_[i] = new FFmpegDemuxerStream(this, stream); 752 streams_[i] =
753 new FFmpegDemuxerStream(this, stream, discard_negative_timestamps);
673 max_duration = std::max(max_duration, streams_[i]->duration()); 754 max_duration = std::max(max_duration, streams_[i]->duration());
674 755
675 if (stream->first_dts != static_cast<int64_t>(AV_NOPTS_VALUE)) { 756 const base::TimeDelta start_time = ExtractStartTime(format_context, stream);
676 const base::TimeDelta first_dts = ConvertFromTimeBase( 757 if (start_time == kNoTimestamp())
677 stream->time_base, stream->first_dts); 758 continue;
678 if (start_time_ == kNoTimestamp() || first_dts < start_time_) 759
679 start_time_ = first_dts; 760 if (start_time < start_time_) {
761 start_time_ = start_time;
762
763 // Choose the stream with the lowest starting time as the fallback stream
764 // for seeking. Video should always be preferred.
765 fallback_stream_for_seeking_ = std::make_pair(i, start_time);
680 } 766 }
767
768 // Always prefer the video stream for seeking. If none exists, we'll swap
769 // the fallback stream with the preferred stream below.
770 if (codec_type == AVMEDIA_TYPE_VIDEO)
771 preferred_stream_for_seeking_ = std::make_pair(i, start_time);
681 } 772 }
682 773
683 if (!audio_stream && !video_stream) { 774 if (!audio_stream && !video_stream) {
684 status_cb.Run(DEMUXER_ERROR_NO_SUPPORTED_STREAMS); 775 status_cb.Run(DEMUXER_ERROR_NO_SUPPORTED_STREAMS);
685 return; 776 return;
686 } 777 }
687 778
688 if (text_enabled_) 779 if (text_enabled_)
689 AddTextStreams(); 780 AddTextStreams();
690 781
691 if (format_context->duration != static_cast<int64_t>(AV_NOPTS_VALUE)) { 782 if (format_context->duration != static_cast<int64_t>(AV_NOPTS_VALUE)) {
692 // If there is a duration value in the container use that to find the 783 // If there is a duration value in the container use that to find the
693 // maximum between it and the duration from A/V streams. 784 // maximum between it and the duration from A/V streams.
694 const AVRational av_time_base = {1, AV_TIME_BASE}; 785 const AVRational av_time_base = {1, AV_TIME_BASE};
695 max_duration = 786 max_duration =
696 std::max(max_duration, 787 std::max(max_duration,
697 ConvertFromTimeBase(av_time_base, format_context->duration)); 788 ConvertFromTimeBase(av_time_base, format_context->duration));
698 } else { 789 } else {
699 // The duration is unknown, in which case this is likely a live stream. 790 // The duration is unknown, in which case this is likely a live stream.
700 max_duration = kInfiniteDuration(); 791 max_duration = kInfiniteDuration();
701 } 792 }
702 793
703 // Some demuxers, like WAV, do not put timestamps on their frames. We 794 // If no start time could be determined, default to zero and prefer the video
704 // assume the the start time is 0. 795 // stream over the audio stream for seeking. E.g., The WAV demuxer does not
705 if (start_time_ == kNoTimestamp()) 796 // put timestamps on its frames.
797 if (start_time_ == kInfiniteDuration()) {
706 start_time_ = base::TimeDelta(); 798 start_time_ = base::TimeDelta();
799 preferred_stream_for_seeking_ = std::make_pair(
800 video_stream ? video_stream->index : audio_stream->index, start_time_);
801 } else if (!video_stream) {
802 // If no video stream exists, use the audio or text stream found above.
803 preferred_stream_for_seeking_ = fallback_stream_for_seeking_;
804 fallback_stream_for_seeking_ = std::make_pair(-1, kNoTimestamp());
acolwell GONE FROM CHROMIUM 2014/06/17 21:39:43 Is there any harm in letting fallback_stream_for_s
DaleCurtis 2014/06/17 22:30:42 Not really, it just made the DCHECKs in Seek() mor
805 } else if (video_stream->index == fallback_stream_for_seeking_.first) {
806 // Ensure both the fallback and preferred streams are not the same.
807 fallback_stream_for_seeking_ = std::make_pair(-1, kNoTimestamp());
acolwell GONE FROM CHROMIUM 2014/06/17 21:39:43 Why is this bad?
DaleCurtis 2014/06/17 22:30:42 Ditto.
808 }
707 809
708 // MPEG-4 B-frames cause grief for a simple container like AVI. Enable PTS 810 // MPEG-4 B-frames cause grief for a simple container like AVI. Enable PTS
709 // generation so we always get timestamps, see http://crbug.com/169570 811 // generation so we always get timestamps, see http://crbug.com/169570
710 if (strcmp(format_context->iformat->name, "avi") == 0) 812 if (strcmp(format_context->iformat->name, "avi") == 0)
711 format_context->flags |= AVFMT_FLAG_GENPTS; 813 format_context->flags |= AVFMT_FLAG_GENPTS;
712 814
713 timeline_offset_ = ExtractTimelineOffset(format_context); 815 // For testing purposes, don't overwrite the timeline offset if set already.
816 if (timeline_offset_.is_null())
817 timeline_offset_ = ExtractTimelineOffset(format_context);
818
819 // Since we're shifting the externally visible start time to zero, we need to
820 // adjust the timeline offset to compensate.
821 if (!timeline_offset_.is_null())
822 timeline_offset_ += start_time_;
714 823
715 if (max_duration == kInfiniteDuration() && !timeline_offset_.is_null()) { 824 if (max_duration == kInfiniteDuration() && !timeline_offset_.is_null()) {
716 liveness_ = LIVENESS_LIVE; 825 liveness_ = LIVENESS_LIVE;
717 } else if (max_duration != kInfiniteDuration()) { 826 } else if (max_duration != kInfiniteDuration()) {
718 liveness_ = LIVENESS_RECORDED; 827 liveness_ = LIVENESS_RECORDED;
719 } else { 828 } else {
720 liveness_ = LIVENESS_UNKNOWN; 829 liveness_ = LIVENESS_UNKNOWN;
721 } 830 }
722 831
723 // Good to go: set the duration and bitrate and notify we're done 832 // Good to go: set the duration and bitrate and notify we're done
(...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after
776 video_codec->time_base.num, 885 video_codec->time_base.num,
777 video_codec->time_base.den)); 886 video_codec->time_base.den));
778 media_log_->SetStringProperty( 887 media_log_->SetStringProperty(
779 "video_format", VideoFrame::FormatToString(video_config.format())); 888 "video_format", VideoFrame::FormatToString(video_config.format()));
780 media_log_->SetBooleanProperty("video_is_encrypted", 889 media_log_->SetBooleanProperty("video_is_encrypted",
781 video_config.is_encrypted()); 890 video_config.is_encrypted());
782 } else { 891 } else {
783 media_log_->SetBooleanProperty("found_video_stream", false); 892 media_log_->SetBooleanProperty("found_video_stream", false);
784 } 893 }
785 894
786
787 media_log_->SetTimeProperty("max_duration", max_duration); 895 media_log_->SetTimeProperty("max_duration", max_duration);
788 media_log_->SetTimeProperty("start_time", start_time_); 896 media_log_->SetTimeProperty("start_time", start_time_);
789 media_log_->SetIntegerProperty("bitrate", bitrate_); 897 media_log_->SetIntegerProperty("bitrate", bitrate_);
790 898
791 status_cb.Run(PIPELINE_OK); 899 status_cb.Run(PIPELINE_OK);
792 } 900 }
793 901
794 void FFmpegDemuxer::OnSeekFrameDone(const PipelineStatusCB& cb, int result) { 902 void FFmpegDemuxer::OnSeekFrameDone(const PipelineStatusCB& cb, int result) {
795 DCHECK(task_runner_->BelongsToCurrentThread()); 903 DCHECK(task_runner_->BelongsToCurrentThread());
796 CHECK(pending_seek_); 904 CHECK(pending_seek_);
(...skipping 215 matching lines...) Expand 10 before | Expand all | Expand 10 after
1012 } 1120 }
1013 for (size_t i = 0; i < buffered.size(); ++i) 1121 for (size_t i = 0; i < buffered.size(); ++i)
1014 host_->AddBufferedTimeRange(buffered.start(i), buffered.end(i)); 1122 host_->AddBufferedTimeRange(buffered.start(i), buffered.end(i));
1015 } 1123 }
1016 1124
1017 void FFmpegDemuxer::OnDataSourceError() { 1125 void FFmpegDemuxer::OnDataSourceError() {
1018 host_->OnDemuxerError(PIPELINE_ERROR_READ); 1126 host_->OnDemuxerError(PIPELINE_ERROR_READ);
1019 } 1127 }
1020 1128
1021 } // namespace media 1129 } // namespace media
OLDNEW
« no previous file with comments | « media/filters/ffmpeg_demuxer.h ('k') | media/filters/ffmpeg_demuxer_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698