Pin Chrome's shortcut to the Win10 Start menu on install and OS upgrade.
[chromium-blink-merge.git] / media / filters / ffmpeg_demuxer.cc
blob80cb7adcbee7a0c13c7630b2d14d4b676db7af61
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
3 // found in the LICENSE file.
5 #include "media/filters/ffmpeg_demuxer.h"
7 #include <algorithm>
9 #include "base/base64.h"
10 #include "base/bind.h"
11 #include "base/callback_helpers.h"
12 #include "base/memory/scoped_ptr.h"
13 #include "base/metrics/sparse_histogram.h"
14 #include "base/single_thread_task_runner.h"
15 #include "base/strings/string_number_conversions.h"
16 #include "base/strings/string_util.h"
17 #include "base/strings/stringprintf.h"
18 #include "base/sys_byteorder.h"
19 #include "base/task_runner_util.h"
20 #include "base/thread_task_runner_handle.h"
21 #include "base/time/time.h"
22 #include "media/base/bind_to_current_loop.h"
23 #include "media/base/decrypt_config.h"
24 #include "media/base/limits.h"
25 #include "media/base/media_log.h"
26 #include "media/ffmpeg/ffmpeg_common.h"
27 #include "media/filters/ffmpeg_aac_bitstream_converter.h"
28 #include "media/filters/ffmpeg_bitstream_converter.h"
29 #include "media/filters/ffmpeg_glue.h"
30 #include "media/filters/ffmpeg_h264_to_annex_b_bitstream_converter.h"
31 #include "media/filters/webvtt_util.h"
32 #include "media/formats/webm/webm_crypto_helpers.h"
34 namespace media {
36 static base::Time ExtractTimelineOffset(AVFormatContext* format_context) {
37 if (strstr(format_context->iformat->name, "webm") ||
38 strstr(format_context->iformat->name, "matroska")) {
39 const AVDictionaryEntry* entry =
40 av_dict_get(format_context->metadata, "creation_time", NULL, 0);
42 base::Time timeline_offset;
43 if (entry != NULL && entry->value != NULL &&
44 FFmpegUTCDateToTime(entry->value, &timeline_offset)) {
45 return timeline_offset;
49 return base::Time();
52 static base::TimeDelta FramesToTimeDelta(int frames, double sample_rate) {
53 return base::TimeDelta::FromMicroseconds(
54 frames * base::Time::kMicrosecondsPerSecond / sample_rate);
57 static base::TimeDelta ExtractStartTime(AVStream* stream,
58 base::TimeDelta start_time_estimate) {
59 DCHECK(start_time_estimate != kNoTimestamp());
60 if (stream->start_time == static_cast<int64_t>(AV_NOPTS_VALUE)) {
61 return start_time_estimate == kInfiniteDuration() ? kNoTimestamp()
62 : start_time_estimate;
65 // First try the lower of the estimate and the |start_time| value.
66 base::TimeDelta start_time =
67 std::min(ConvertFromTimeBase(stream->time_base, stream->start_time),
68 start_time_estimate);
70 // Next see if the first buffered pts value is usable.
71 if (stream->pts_buffer[0] != static_cast<int64_t>(AV_NOPTS_VALUE)) {
72 const base::TimeDelta buffered_pts =
73 ConvertFromTimeBase(stream->time_base, stream->pts_buffer[0]);
74 if (buffered_pts < start_time)
75 start_time = buffered_pts;
78 // NOTE: Do not use AVStream->first_dts since |start_time| should be a
79 // presentation timestamp.
80 return start_time;
84 // FFmpegDemuxerStream
86 FFmpegDemuxerStream::FFmpegDemuxerStream(FFmpegDemuxer* demuxer,
87 AVStream* stream)
88 : demuxer_(demuxer),
89 task_runner_(base::ThreadTaskRunnerHandle::Get()),
90 stream_(stream),
91 type_(UNKNOWN),
92 liveness_(LIVENESS_UNKNOWN),
93 end_of_stream_(false),
94 last_packet_timestamp_(kNoTimestamp()),
95 last_packet_duration_(kNoTimestamp()),
96 video_rotation_(VIDEO_ROTATION_0),
97 fixup_negative_ogg_timestamps_(false) {
98 DCHECK(demuxer_);
100 bool is_encrypted = false;
101 int rotation = 0;
102 AVDictionaryEntry* rotation_entry = NULL;
104 // Determine our media format.
105 switch (stream->codec->codec_type) {
106 case AVMEDIA_TYPE_AUDIO:
107 type_ = AUDIO;
108 AVStreamToAudioDecoderConfig(stream, &audio_config_, true);
109 is_encrypted = audio_config_.is_encrypted();
110 break;
111 case AVMEDIA_TYPE_VIDEO:
112 type_ = VIDEO;
113 AVStreamToVideoDecoderConfig(stream, &video_config_, true);
114 is_encrypted = video_config_.is_encrypted();
116 rotation_entry = av_dict_get(stream->metadata, "rotate", NULL, 0);
117 if (rotation_entry && rotation_entry->value && rotation_entry->value[0])
118 base::StringToInt(rotation_entry->value, &rotation);
120 switch (rotation) {
121 case 0:
122 break;
123 case 90:
124 video_rotation_ = VIDEO_ROTATION_90;
125 break;
126 case 180:
127 video_rotation_ = VIDEO_ROTATION_180;
128 break;
129 case 270:
130 video_rotation_ = VIDEO_ROTATION_270;
131 break;
132 default:
133 LOG(ERROR) << "Unsupported video rotation metadata: " << rotation;
134 break;
137 break;
138 case AVMEDIA_TYPE_SUBTITLE:
139 type_ = TEXT;
140 break;
141 default:
142 NOTREACHED();
143 break;
146 // Calculate the duration.
147 duration_ = ConvertStreamTimestamp(stream->time_base, stream->duration);
149 if (is_encrypted) {
150 AVDictionaryEntry* key = av_dict_get(stream->metadata, "enc_key_id", NULL,
152 DCHECK(key);
153 DCHECK(key->value);
154 if (!key || !key->value)
155 return;
156 base::StringPiece base64_key_id(key->value);
157 std::string enc_key_id;
158 base::Base64Decode(base64_key_id, &enc_key_id);
159 DCHECK(!enc_key_id.empty());
160 if (enc_key_id.empty())
161 return;
163 encryption_key_id_.assign(enc_key_id);
164 demuxer_->OnEncryptedMediaInitData(EmeInitDataType::WEBM, enc_key_id);
168 FFmpegDemuxerStream::~FFmpegDemuxerStream() {
169 DCHECK(!demuxer_);
170 DCHECK(read_cb_.is_null());
171 DCHECK(buffer_queue_.IsEmpty());
174 void FFmpegDemuxerStream::EnqueuePacket(ScopedAVPacket packet) {
175 DCHECK(task_runner_->BelongsToCurrentThread());
177 if (!demuxer_ || end_of_stream_) {
178 NOTREACHED() << "Attempted to enqueue packet on a stopped stream";
179 return;
182 #if defined(USE_PROPRIETARY_CODECS)
183 // Convert the packet if there is a bitstream filter.
184 if (packet->data && bitstream_converter_ &&
185 !bitstream_converter_->ConvertPacket(packet.get())) {
186 LOG(ERROR) << "Format conversion failed.";
188 #endif
190 // Get side data if any. For now, the only type of side_data is VP8 Alpha. We
191 // keep this generic so that other side_data types in the future can be
192 // handled the same way as well.
193 av_packet_split_side_data(packet.get());
195 scoped_refptr<DecoderBuffer> buffer;
197 if (type() == DemuxerStream::TEXT) {
198 int id_size = 0;
199 uint8* id_data = av_packet_get_side_data(
200 packet.get(),
201 AV_PKT_DATA_WEBVTT_IDENTIFIER,
202 &id_size);
204 int settings_size = 0;
205 uint8* settings_data = av_packet_get_side_data(
206 packet.get(),
207 AV_PKT_DATA_WEBVTT_SETTINGS,
208 &settings_size);
210 std::vector<uint8> side_data;
211 MakeSideData(id_data, id_data + id_size,
212 settings_data, settings_data + settings_size,
213 &side_data);
215 buffer = DecoderBuffer::CopyFrom(packet.get()->data, packet.get()->size,
216 side_data.data(), side_data.size());
217 } else {
218 int side_data_size = 0;
219 uint8* side_data = av_packet_get_side_data(
220 packet.get(),
221 AV_PKT_DATA_MATROSKA_BLOCKADDITIONAL,
222 &side_data_size);
224 scoped_ptr<DecryptConfig> decrypt_config;
225 int data_offset = 0;
226 if ((type() == DemuxerStream::AUDIO && audio_config_.is_encrypted()) ||
227 (type() == DemuxerStream::VIDEO && video_config_.is_encrypted())) {
228 if (!WebMCreateDecryptConfig(
229 packet->data, packet->size,
230 reinterpret_cast<const uint8*>(encryption_key_id_.data()),
231 encryption_key_id_.size(),
232 &decrypt_config,
233 &data_offset)) {
234 LOG(ERROR) << "Creation of DecryptConfig failed.";
238 // If a packet is returned by FFmpeg's av_parser_parse2() the packet will
239 // reference inner memory of FFmpeg. As such we should transfer the packet
240 // into memory we control.
241 if (side_data_size > 0) {
242 buffer = DecoderBuffer::CopyFrom(packet.get()->data + data_offset,
243 packet.get()->size - data_offset,
244 side_data, side_data_size);
245 } else {
246 buffer = DecoderBuffer::CopyFrom(packet.get()->data + data_offset,
247 packet.get()->size - data_offset);
250 int skip_samples_size = 0;
251 const uint32* skip_samples_ptr =
252 reinterpret_cast<const uint32*>(av_packet_get_side_data(
253 packet.get(), AV_PKT_DATA_SKIP_SAMPLES, &skip_samples_size));
254 const int kSkipSamplesValidSize = 10;
255 const int kSkipEndSamplesOffset = 1;
256 if (skip_samples_size >= kSkipSamplesValidSize) {
257 // Because FFmpeg rolls codec delay and skip samples into one we can only
258 // allow front discard padding on the first buffer. Otherwise the discard
259 // helper can't figure out which data to discard. See AudioDiscardHelper.
260 int discard_front_samples = base::ByteSwapToLE32(*skip_samples_ptr);
261 if (last_packet_timestamp_ != kNoTimestamp() && discard_front_samples) {
262 DLOG(ERROR) << "Skip samples are only allowed for the first packet.";
263 discard_front_samples = 0;
266 const int discard_end_samples =
267 base::ByteSwapToLE32(*(skip_samples_ptr + kSkipEndSamplesOffset));
268 const int samples_per_second =
269 audio_decoder_config().samples_per_second();
270 buffer->set_discard_padding(std::make_pair(
271 FramesToTimeDelta(discard_front_samples, samples_per_second),
272 FramesToTimeDelta(discard_end_samples, samples_per_second)));
275 if (decrypt_config)
276 buffer->set_decrypt_config(decrypt_config.Pass());
279 if (packet->duration >= 0) {
280 buffer->set_duration(
281 ConvertStreamTimestamp(stream_->time_base, packet->duration));
282 } else {
283 // TODO(wolenetz): Remove when FFmpeg stops returning negative durations.
284 // https://crbug.com/394418
285 DVLOG(1) << "FFmpeg returned a buffer with a negative duration! "
286 << packet->duration;
287 buffer->set_duration(kNoTimestamp());
290 // Note: If pts is AV_NOPTS_VALUE, stream_timestamp will be kNoTimestamp().
291 const base::TimeDelta stream_timestamp =
292 ConvertStreamTimestamp(stream_->time_base, packet->pts);
294 if (stream_timestamp != kNoTimestamp()) {
295 const bool is_audio = type() == AUDIO;
297 // If this is an OGG file with negative timestamps don't rebase any other
298 // stream types against the negative starting time.
299 base::TimeDelta start_time = demuxer_->start_time();
300 if (fixup_negative_ogg_timestamps_ && !is_audio &&
301 start_time < base::TimeDelta()) {
302 start_time = base::TimeDelta();
305 // Don't rebase timestamps for positive start times, the HTML Media Spec
306 // details this in section "4.8.10.6 Offsets into the media resource." We
307 // will still need to rebase timestamps before seeking with FFmpeg though.
308 if (start_time > base::TimeDelta())
309 start_time = base::TimeDelta();
311 buffer->set_timestamp(stream_timestamp - start_time);
313 // If enabled, mark audio packets with negative timestamps for post-decode
314 // discard.
315 if (fixup_negative_ogg_timestamps_ && is_audio &&
316 stream_timestamp < base::TimeDelta() &&
317 buffer->duration() != kNoTimestamp()) {
318 if (stream_timestamp + buffer->duration() < base::TimeDelta()) {
319 // Discard the entire packet if it's entirely before zero.
320 buffer->set_discard_padding(
321 std::make_pair(kInfiniteDuration(), base::TimeDelta()));
322 } else {
323 // Only discard part of the frame if it overlaps zero.
324 buffer->set_discard_padding(
325 std::make_pair(-stream_timestamp, base::TimeDelta()));
328 } else {
329 // If this happens on the first packet, decoders will throw an error.
330 buffer->set_timestamp(kNoTimestamp());
333 if (last_packet_timestamp_ != kNoTimestamp()) {
334 // FFmpeg doesn't support chained ogg correctly. Instead of guaranteeing
335 // continuity across links in the chain it uses the timestamp information
336 // from each link directly. Doing so can lead to timestamps which appear to
337 // go backwards in time.
339 // If the new link starts with a negative timestamp or a timestamp less than
340 // the original (positive) |start_time|, we will get a negative timestamp
341 // here. It's also possible FFmpeg returns kNoTimestamp() here if it's not
342 // able to work out a timestamp using the previous link and the next.
344 // Fixing chained ogg is non-trivial, so for now just reuse the last good
345 // timestamp. The decoder will rewrite the timestamps to be sample accurate
346 // later. See http://crbug.com/396864.
347 if (fixup_negative_ogg_timestamps_ &&
348 (buffer->timestamp() == kNoTimestamp() ||
349 buffer->timestamp() < last_packet_timestamp_)) {
350 buffer->set_timestamp(last_packet_timestamp_ +
351 (last_packet_duration_ != kNoTimestamp()
352 ? last_packet_duration_
353 : base::TimeDelta::FromMicroseconds(1)));
356 // The demuxer should always output positive timestamps.
357 DCHECK(buffer->timestamp() >= base::TimeDelta());
358 DCHECK(buffer->timestamp() != kNoTimestamp());
360 if (last_packet_timestamp_ < buffer->timestamp()) {
361 buffered_ranges_.Add(last_packet_timestamp_, buffer->timestamp());
362 demuxer_->NotifyBufferingChanged();
366 if (packet.get()->flags & AV_PKT_FLAG_KEY)
367 buffer->set_is_key_frame(true);
369 last_packet_timestamp_ = buffer->timestamp();
370 last_packet_duration_ = buffer->duration();
372 buffer_queue_.Push(buffer);
373 SatisfyPendingRead();
376 void FFmpegDemuxerStream::SetEndOfStream() {
377 DCHECK(task_runner_->BelongsToCurrentThread());
378 end_of_stream_ = true;
379 SatisfyPendingRead();
382 void FFmpegDemuxerStream::FlushBuffers() {
383 DCHECK(task_runner_->BelongsToCurrentThread());
384 DCHECK(read_cb_.is_null()) << "There should be no pending read";
386 // H264 and AAC require that we resend the header after flush.
387 // Reset bitstream for converter to do so.
388 // This is related to chromium issue 140371 (http://crbug.com/140371).
389 ResetBitstreamConverter();
391 buffer_queue_.Clear();
392 end_of_stream_ = false;
393 last_packet_timestamp_ = kNoTimestamp();
394 last_packet_duration_ = kNoTimestamp();
397 void FFmpegDemuxerStream::Stop() {
398 DCHECK(task_runner_->BelongsToCurrentThread());
399 buffer_queue_.Clear();
400 if (!read_cb_.is_null()) {
401 base::ResetAndReturn(&read_cb_).Run(
402 DemuxerStream::kOk, DecoderBuffer::CreateEOSBuffer());
404 demuxer_ = NULL;
405 stream_ = NULL;
406 end_of_stream_ = true;
409 DemuxerStream::Type FFmpegDemuxerStream::type() const {
410 DCHECK(task_runner_->BelongsToCurrentThread());
411 return type_;
414 DemuxerStream::Liveness FFmpegDemuxerStream::liveness() const {
415 DCHECK(task_runner_->BelongsToCurrentThread());
416 return liveness_;
419 void FFmpegDemuxerStream::Read(const ReadCB& read_cb) {
420 DCHECK(task_runner_->BelongsToCurrentThread());
421 CHECK(read_cb_.is_null()) << "Overlapping reads are not supported";
422 read_cb_ = BindToCurrentLoop(read_cb);
424 // Don't accept any additional reads if we've been told to stop.
425 // The |demuxer_| may have been destroyed in the pipeline thread.
427 // TODO(scherkus): it would be cleaner to reply with an error message.
428 if (!demuxer_) {
429 base::ResetAndReturn(&read_cb_).Run(
430 DemuxerStream::kOk, DecoderBuffer::CreateEOSBuffer());
431 return;
434 SatisfyPendingRead();
437 void FFmpegDemuxerStream::EnableBitstreamConverter() {
438 DCHECK(task_runner_->BelongsToCurrentThread());
440 #if defined(USE_PROPRIETARY_CODECS)
441 InitBitstreamConverter();
442 #else
443 NOTREACHED() << "Proprietary codecs not enabled.";
444 #endif
447 void FFmpegDemuxerStream::ResetBitstreamConverter() {
448 #if defined(USE_PROPRIETARY_CODECS)
449 if (bitstream_converter_)
450 InitBitstreamConverter();
451 #endif // defined(USE_PROPRIETARY_CODECS)
454 void FFmpegDemuxerStream::InitBitstreamConverter() {
455 #if defined(USE_PROPRIETARY_CODECS)
456 if (stream_->codec->codec_id == AV_CODEC_ID_H264) {
457 bitstream_converter_.reset(
458 new FFmpegH264ToAnnexBBitstreamConverter(stream_->codec));
459 } else if (stream_->codec->codec_id == AV_CODEC_ID_AAC) {
460 bitstream_converter_.reset(
461 new FFmpegAACBitstreamConverter(stream_->codec));
463 #endif // defined(USE_PROPRIETARY_CODECS)
466 bool FFmpegDemuxerStream::SupportsConfigChanges() { return false; }
468 AudioDecoderConfig FFmpegDemuxerStream::audio_decoder_config() {
469 DCHECK(task_runner_->BelongsToCurrentThread());
470 CHECK_EQ(type_, AUDIO);
471 return audio_config_;
474 VideoDecoderConfig FFmpegDemuxerStream::video_decoder_config() {
475 DCHECK(task_runner_->BelongsToCurrentThread());
476 CHECK_EQ(type_, VIDEO);
477 return video_config_;
480 VideoRotation FFmpegDemuxerStream::video_rotation() {
481 return video_rotation_;
484 void FFmpegDemuxerStream::SetLiveness(Liveness liveness) {
485 DCHECK(task_runner_->BelongsToCurrentThread());
486 DCHECK_EQ(liveness_, LIVENESS_UNKNOWN);
487 liveness_ = liveness;
490 base::TimeDelta FFmpegDemuxerStream::GetElapsedTime() const {
491 return ConvertStreamTimestamp(stream_->time_base, stream_->cur_dts);
494 Ranges<base::TimeDelta> FFmpegDemuxerStream::GetBufferedRanges() const {
495 return buffered_ranges_;
498 void FFmpegDemuxerStream::SatisfyPendingRead() {
499 DCHECK(task_runner_->BelongsToCurrentThread());
500 if (!read_cb_.is_null()) {
501 if (!buffer_queue_.IsEmpty()) {
502 base::ResetAndReturn(&read_cb_).Run(
503 DemuxerStream::kOk, buffer_queue_.Pop());
504 } else if (end_of_stream_) {
505 base::ResetAndReturn(&read_cb_).Run(
506 DemuxerStream::kOk, DecoderBuffer::CreateEOSBuffer());
510 // Have capacity? Ask for more!
511 if (HasAvailableCapacity() && !end_of_stream_) {
512 demuxer_->NotifyCapacityAvailable();
516 bool FFmpegDemuxerStream::HasAvailableCapacity() {
517 // TODO(scherkus): Remove this return and reenable time-based capacity
518 // after our data sources support canceling/concurrent reads, see
519 // http://crbug.com/165762 for details.
520 #if 1
521 return !read_cb_.is_null();
522 #else
523 // Try to have one second's worth of encoded data per stream.
524 const base::TimeDelta kCapacity = base::TimeDelta::FromSeconds(1);
525 return buffer_queue_.IsEmpty() || buffer_queue_.Duration() < kCapacity;
526 #endif
529 size_t FFmpegDemuxerStream::MemoryUsage() const {
530 return buffer_queue_.data_size();
533 TextKind FFmpegDemuxerStream::GetTextKind() const {
534 DCHECK_EQ(type_, DemuxerStream::TEXT);
536 if (stream_->disposition & AV_DISPOSITION_CAPTIONS)
537 return kTextCaptions;
539 if (stream_->disposition & AV_DISPOSITION_DESCRIPTIONS)
540 return kTextDescriptions;
542 if (stream_->disposition & AV_DISPOSITION_METADATA)
543 return kTextMetadata;
545 return kTextSubtitles;
548 std::string FFmpegDemuxerStream::GetMetadata(const char* key) const {
549 const AVDictionaryEntry* entry =
550 av_dict_get(stream_->metadata, key, NULL, 0);
551 return (entry == NULL || entry->value == NULL) ? "" : entry->value;
554 // static
555 base::TimeDelta FFmpegDemuxerStream::ConvertStreamTimestamp(
556 const AVRational& time_base, int64 timestamp) {
557 if (timestamp == static_cast<int64>(AV_NOPTS_VALUE))
558 return kNoTimestamp();
560 return ConvertFromTimeBase(time_base, timestamp);
564 // FFmpegDemuxer
566 FFmpegDemuxer::FFmpegDemuxer(
567 const scoped_refptr<base::SingleThreadTaskRunner>& task_runner,
568 DataSource* data_source,
569 const EncryptedMediaInitDataCB& encrypted_media_init_data_cb,
570 const scoped_refptr<MediaLog>& media_log)
571 : host_(NULL),
572 task_runner_(task_runner),
573 blocking_thread_("FFmpegDemuxer"),
574 pending_read_(false),
575 pending_seek_(false),
576 data_source_(data_source),
577 media_log_(media_log),
578 bitrate_(0),
579 start_time_(kNoTimestamp()),
580 preferred_stream_for_seeking_(-1, kNoTimestamp()),
581 fallback_stream_for_seeking_(-1, kNoTimestamp()),
582 text_enabled_(false),
583 duration_known_(false),
584 encrypted_media_init_data_cb_(encrypted_media_init_data_cb),
585 weak_factory_(this) {
586 DCHECK(task_runner_.get());
587 DCHECK(data_source_);
590 FFmpegDemuxer::~FFmpegDemuxer() {}
592 void FFmpegDemuxer::Stop() {
593 DCHECK(task_runner_->BelongsToCurrentThread());
595 // The order of Stop() and Abort() is important here. If Abort() is called
596 // first, control may pass into FFmpeg where it can destruct buffers that are
597 // in the process of being fulfilled by the DataSource.
598 data_source_->Stop();
599 url_protocol_->Abort();
601 // This will block until all tasks complete. Note that after this returns it's
602 // possible for reply tasks (e.g., OnReadFrameDone()) to be queued on this
603 // thread. Each of the reply task methods must check whether we've stopped the
604 // thread and drop their results on the floor.
605 blocking_thread_.Stop();
607 StreamVector::iterator iter;
608 for (iter = streams_.begin(); iter != streams_.end(); ++iter) {
609 if (*iter)
610 (*iter)->Stop();
613 data_source_ = NULL;
616 void FFmpegDemuxer::Seek(base::TimeDelta time, const PipelineStatusCB& cb) {
617 DCHECK(task_runner_->BelongsToCurrentThread());
618 CHECK(!pending_seek_);
620 // TODO(scherkus): Inspect |pending_read_| and cancel IO via |blocking_url_|,
621 // otherwise we can end up waiting for a pre-seek read to complete even though
622 // we know we're going to drop it on the floor.
624 // FFmpeg requires seeks to be adjusted according to the lowest starting time.
625 // Since EnqueuePacket() rebased negative timestamps by the start time, we
626 // must correct the shift here.
628 // Additionally, to workaround limitations in how we expose seekable ranges to
629 // Blink (http://crbug.com/137275), we also want to clamp seeks before the
630 // start time to the start time.
631 const base::TimeDelta seek_time =
632 start_time_ < base::TimeDelta() ? time + start_time_
633 : time < start_time_ ? start_time_ : time;
635 // Choose the seeking stream based on whether it contains the seek time, if no
636 // match can be found prefer the preferred stream.
638 // TODO(dalecurtis): Currently FFmpeg does not ensure that all streams in a
639 // given container will demux all packets after the seek point. Instead it
640 // only guarantees that all packets after the file position of the seek will
641 // be demuxed. It's an open question whether FFmpeg should fix this:
642 // http://lists.ffmpeg.org/pipermail/ffmpeg-devel/2014-June/159212.html
643 // Tracked by http://crbug.com/387996.
644 DCHECK(preferred_stream_for_seeking_.second != kNoTimestamp());
645 const int stream_index =
646 seek_time < preferred_stream_for_seeking_.second &&
647 seek_time >= fallback_stream_for_seeking_.second
648 ? fallback_stream_for_seeking_.first
649 : preferred_stream_for_seeking_.first;
650 DCHECK_NE(stream_index, -1);
652 const AVStream* seeking_stream =
653 glue_->format_context()->streams[stream_index];
655 pending_seek_ = true;
656 base::PostTaskAndReplyWithResult(
657 blocking_thread_.task_runner().get(),
658 FROM_HERE,
659 base::Bind(&av_seek_frame,
660 glue_->format_context(),
661 seeking_stream->index,
662 ConvertToTimeBase(seeking_stream->time_base, seek_time),
663 // Always seek to a timestamp <= to the desired timestamp.
664 AVSEEK_FLAG_BACKWARD),
665 base::Bind(
666 &FFmpegDemuxer::OnSeekFrameDone, weak_factory_.GetWeakPtr(), cb));
669 std::string FFmpegDemuxer::GetDisplayName() const {
670 return "FFmpegDemuxer";
673 void FFmpegDemuxer::Initialize(DemuxerHost* host,
674 const PipelineStatusCB& status_cb,
675 bool enable_text_tracks) {
676 DCHECK(task_runner_->BelongsToCurrentThread());
677 host_ = host;
678 text_enabled_ = enable_text_tracks;
680 url_protocol_.reset(new BlockingUrlProtocol(data_source_, BindToCurrentLoop(
681 base::Bind(&FFmpegDemuxer::OnDataSourceError, base::Unretained(this)))));
682 glue_.reset(new FFmpegGlue(url_protocol_.get()));
683 AVFormatContext* format_context = glue_->format_context();
685 // Disable ID3v1 tag reading to avoid costly seeks to end of file for data we
686 // don't use. FFmpeg will only read ID3v1 tags if no other metadata is
687 // available, so add a metadata entry to ensure some is always present.
688 av_dict_set(&format_context->metadata, "skip_id3v1_tags", "", 0);
690 // Ensure ffmpeg doesn't give up too early while looking for stream params;
691 // this does not increase the amount of data downloaded. The default value
692 // is 5 AV_TIME_BASE units (1 second each), which prevents some oddly muxed
693 // streams from being detected properly; this value was chosen arbitrarily.
694 format_context->max_analyze_duration2 = 60 * AV_TIME_BASE;
696 // Open the AVFormatContext using our glue layer.
697 CHECK(blocking_thread_.Start());
698 base::PostTaskAndReplyWithResult(
699 blocking_thread_.task_runner().get(),
700 FROM_HERE,
701 base::Bind(&FFmpegGlue::OpenContext, base::Unretained(glue_.get())),
702 base::Bind(&FFmpegDemuxer::OnOpenContextDone,
703 weak_factory_.GetWeakPtr(),
704 status_cb));
707 base::Time FFmpegDemuxer::GetTimelineOffset() const {
708 return timeline_offset_;
711 DemuxerStream* FFmpegDemuxer::GetStream(DemuxerStream::Type type) {
712 DCHECK(task_runner_->BelongsToCurrentThread());
713 return GetFFmpegStream(type);
716 FFmpegDemuxerStream* FFmpegDemuxer::GetFFmpegStream(
717 DemuxerStream::Type type) const {
718 StreamVector::const_iterator iter;
719 for (iter = streams_.begin(); iter != streams_.end(); ++iter) {
720 if (*iter && (*iter)->type() == type) {
721 return *iter;
724 return NULL;
727 base::TimeDelta FFmpegDemuxer::GetStartTime() const {
728 return std::max(start_time_, base::TimeDelta());
731 void FFmpegDemuxer::AddTextStreams() {
732 DCHECK(task_runner_->BelongsToCurrentThread());
734 for (StreamVector::size_type idx = 0; idx < streams_.size(); ++idx) {
735 FFmpegDemuxerStream* stream = streams_[idx];
736 if (stream == NULL || stream->type() != DemuxerStream::TEXT)
737 continue;
739 TextKind kind = stream->GetTextKind();
740 std::string title = stream->GetMetadata("title");
741 std::string language = stream->GetMetadata("language");
743 // TODO: Implement "id" metadata in FFMPEG.
744 // See: http://crbug.com/323183
745 host_->AddTextStream(stream, TextTrackConfig(kind, title, language,
746 std::string()));
750 // Helper for calculating the bitrate of the media based on information stored
751 // in |format_context| or failing that the size and duration of the media.
753 // Returns 0 if a bitrate could not be determined.
754 static int CalculateBitrate(
755 AVFormatContext* format_context,
756 const base::TimeDelta& duration,
757 int64 filesize_in_bytes) {
758 // If there is a bitrate set on the container, use it.
759 if (format_context->bit_rate > 0)
760 return format_context->bit_rate;
762 // Then try to sum the bitrates individually per stream.
763 int bitrate = 0;
764 for (size_t i = 0; i < format_context->nb_streams; ++i) {
765 AVCodecContext* codec_context = format_context->streams[i]->codec;
766 bitrate += codec_context->bit_rate;
768 if (bitrate > 0)
769 return bitrate;
771 // See if we can approximate the bitrate as long as we have a filesize and
772 // valid duration.
773 if (duration.InMicroseconds() <= 0 ||
774 duration == kInfiniteDuration() ||
775 filesize_in_bytes == 0) {
776 return 0;
779 // Do math in floating point as we'd overflow an int64 if the filesize was
780 // larger than ~1073GB.
781 double bytes = filesize_in_bytes;
782 double duration_us = duration.InMicroseconds();
783 return bytes * 8000000.0 / duration_us;
786 void FFmpegDemuxer::OnOpenContextDone(const PipelineStatusCB& status_cb,
787 bool result) {
788 DCHECK(task_runner_->BelongsToCurrentThread());
789 if (!blocking_thread_.IsRunning()) {
790 MEDIA_LOG(ERROR, media_log_) << GetDisplayName() << ": bad state";
791 status_cb.Run(PIPELINE_ERROR_ABORT);
792 return;
795 if (!result) {
796 MEDIA_LOG(ERROR, media_log_) << GetDisplayName() << ": open context failed";
797 status_cb.Run(DEMUXER_ERROR_COULD_NOT_OPEN);
798 return;
801 // Fully initialize AVFormatContext by parsing the stream a little.
802 base::PostTaskAndReplyWithResult(
803 blocking_thread_.task_runner().get(),
804 FROM_HERE,
805 base::Bind(&avformat_find_stream_info,
806 glue_->format_context(),
807 static_cast<AVDictionary**>(NULL)),
808 base::Bind(&FFmpegDemuxer::OnFindStreamInfoDone,
809 weak_factory_.GetWeakPtr(),
810 status_cb));
813 void FFmpegDemuxer::OnFindStreamInfoDone(const PipelineStatusCB& status_cb,
814 int result) {
815 DCHECK(task_runner_->BelongsToCurrentThread());
816 if (!blocking_thread_.IsRunning() || !data_source_) {
817 MEDIA_LOG(ERROR, media_log_) << GetDisplayName() << ": bad state";
818 status_cb.Run(PIPELINE_ERROR_ABORT);
819 return;
822 if (result < 0) {
823 MEDIA_LOG(ERROR, media_log_) << GetDisplayName()
824 << ": find stream info failed";
825 status_cb.Run(DEMUXER_ERROR_COULD_NOT_PARSE);
826 return;
829 // Create demuxer stream entries for each possible AVStream. Each stream
830 // is examined to determine if it is supported or not (is the codec enabled
831 // for it in this release?). Unsupported streams are skipped, allowing for
832 // partial playback. At least one audio or video stream must be playable.
833 AVFormatContext* format_context = glue_->format_context();
834 streams_.resize(format_context->nb_streams);
836 // Estimate the start time for each stream by looking through the packets
837 // buffered during avformat_find_stream_info(). These values will be
838 // considered later when determining the actual stream start time.
840 // These packets haven't been completely processed yet, so only look through
841 // these values if the AVFormatContext has a valid start time.
843 // If no estimate is found, the stream entry will be kInfiniteDuration().
844 std::vector<base::TimeDelta> start_time_estimates(format_context->nb_streams,
845 kInfiniteDuration());
846 const AVFormatInternal* internal = format_context->internal;
847 if (internal && internal->packet_buffer &&
848 format_context->start_time != static_cast<int64>(AV_NOPTS_VALUE)) {
849 struct AVPacketList* packet_buffer = internal->packet_buffer;
850 while (packet_buffer != internal->packet_buffer_end) {
851 DCHECK_LT(static_cast<size_t>(packet_buffer->pkt.stream_index),
852 start_time_estimates.size());
853 const AVStream* stream =
854 format_context->streams[packet_buffer->pkt.stream_index];
855 if (packet_buffer->pkt.pts != static_cast<int64>(AV_NOPTS_VALUE)) {
856 const base::TimeDelta packet_pts =
857 ConvertFromTimeBase(stream->time_base, packet_buffer->pkt.pts);
858 if (packet_pts < start_time_estimates[stream->index])
859 start_time_estimates[stream->index] = packet_pts;
861 packet_buffer = packet_buffer->next;
865 AVStream* audio_stream = NULL;
866 AudioDecoderConfig audio_config;
868 AVStream* video_stream = NULL;
869 VideoDecoderConfig video_config;
871 // If available, |start_time_| will be set to the lowest stream start time.
872 start_time_ = kInfiniteDuration();
874 base::TimeDelta max_duration;
875 for (size_t i = 0; i < format_context->nb_streams; ++i) {
876 AVStream* stream = format_context->streams[i];
877 const AVCodecContext* codec_context = stream->codec;
878 const AVMediaType codec_type = codec_context->codec_type;
880 if (codec_type == AVMEDIA_TYPE_AUDIO) {
881 if (audio_stream)
882 continue;
884 // Log the codec detected, whether it is supported or not.
885 UMA_HISTOGRAM_SPARSE_SLOWLY("Media.DetectedAudioCodec",
886 codec_context->codec_id);
887 // Ensure the codec is supported. IsValidConfig() also checks that the
888 // channel layout and sample format are valid.
889 AVStreamToAudioDecoderConfig(stream, &audio_config, false);
890 if (!audio_config.IsValidConfig())
891 continue;
892 audio_stream = stream;
893 } else if (codec_type == AVMEDIA_TYPE_VIDEO) {
894 if (video_stream)
895 continue;
897 // Log the codec detected, whether it is supported or not.
898 UMA_HISTOGRAM_SPARSE_SLOWLY("Media.DetectedVideoCodec",
899 codec_context->codec_id);
900 // Ensure the codec is supported. IsValidConfig() also checks that the
901 // frame size and visible size are valid.
902 AVStreamToVideoDecoderConfig(stream, &video_config, false);
904 if (!video_config.IsValidConfig())
905 continue;
906 video_stream = stream;
907 } else if (codec_type == AVMEDIA_TYPE_SUBTITLE) {
908 if (codec_context->codec_id != AV_CODEC_ID_WEBVTT || !text_enabled_) {
909 continue;
911 } else {
912 continue;
915 streams_[i] = new FFmpegDemuxerStream(this, stream);
916 max_duration = std::max(max_duration, streams_[i]->duration());
918 const base::TimeDelta start_time =
919 ExtractStartTime(stream, start_time_estimates[i]);
920 const bool has_start_time = start_time != kNoTimestamp();
922 // Always prefer the video stream for seeking. If none exists, we'll swap
923 // the fallback stream with the preferred stream below.
924 if (codec_type == AVMEDIA_TYPE_VIDEO) {
925 preferred_stream_for_seeking_ =
926 StreamSeekInfo(i, has_start_time ? start_time : base::TimeDelta());
929 if (!has_start_time)
930 continue;
932 if (start_time < start_time_) {
933 start_time_ = start_time;
935 // Choose the stream with the lowest starting time as the fallback stream
936 // for seeking. Video should always be preferred.
937 fallback_stream_for_seeking_ = StreamSeekInfo(i, start_time);
941 if (!audio_stream && !video_stream) {
942 MEDIA_LOG(ERROR, media_log_) << GetDisplayName()
943 << ": no supported streams";
944 status_cb.Run(DEMUXER_ERROR_NO_SUPPORTED_STREAMS);
945 return;
948 if (text_enabled_)
949 AddTextStreams();
951 if (format_context->duration != static_cast<int64_t>(AV_NOPTS_VALUE)) {
952 // If there is a duration value in the container use that to find the
953 // maximum between it and the duration from A/V streams.
954 const AVRational av_time_base = {1, AV_TIME_BASE};
955 max_duration =
956 std::max(max_duration,
957 ConvertFromTimeBase(av_time_base, format_context->duration));
958 } else {
959 // The duration is unknown, in which case this is likely a live stream.
960 max_duration = kInfiniteDuration();
963 // Ogg has some peculiarities around negative timestamps, so use this flag to
964 // setup the FFmpegDemuxerStreams appropriately.
966 // Post-decode frame dropping for packets with negative timestamps is outlined
967 // in section A.2 in the Ogg Vorbis spec:
968 // http://xiph.org/vorbis/doc/Vorbis_I_spec.html
969 if (strcmp(format_context->iformat->name, "ogg") == 0 && audio_stream &&
970 audio_stream->codec->codec_id == AV_CODEC_ID_VORBIS) {
971 for (size_t i = 0; i < streams_.size(); ++i) {
972 if (streams_[i])
973 streams_[i]->enable_negative_timestamp_fixups_for_ogg();
976 // Fixup the seeking information to avoid selecting the audio stream simply
977 // because it has a lower starting time.
978 if (fallback_stream_for_seeking_.first == audio_stream->index &&
979 fallback_stream_for_seeking_.second < base::TimeDelta()) {
980 fallback_stream_for_seeking_.second = base::TimeDelta();
984 // If no start time could be determined, default to zero and prefer the video
985 // stream over the audio stream for seeking. E.g., The WAV demuxer does not
986 // put timestamps on its frames.
987 if (start_time_ == kInfiniteDuration()) {
988 start_time_ = base::TimeDelta();
989 preferred_stream_for_seeking_ = StreamSeekInfo(
990 video_stream ? video_stream->index : audio_stream->index, start_time_);
991 } else if (!video_stream) {
992 // If no video stream exists, use the audio or text stream found above.
993 preferred_stream_for_seeking_ = fallback_stream_for_seeking_;
996 // MPEG-4 B-frames cause grief for a simple container like AVI. Enable PTS
997 // generation so we always get timestamps, see http://crbug.com/169570
998 if (strcmp(format_context->iformat->name, "avi") == 0)
999 format_context->flags |= AVFMT_FLAG_GENPTS;
1001 // For testing purposes, don't overwrite the timeline offset if set already.
1002 if (timeline_offset_.is_null())
1003 timeline_offset_ = ExtractTimelineOffset(format_context);
1005 // Since we're shifting the externally visible start time to zero, we need to
1006 // adjust the timeline offset to compensate.
1007 if (!timeline_offset_.is_null() && start_time_ < base::TimeDelta())
1008 timeline_offset_ += start_time_;
1010 if (max_duration == kInfiniteDuration() && !timeline_offset_.is_null()) {
1011 SetLiveness(DemuxerStream::LIVENESS_LIVE);
1012 } else if (max_duration != kInfiniteDuration()) {
1013 SetLiveness(DemuxerStream::LIVENESS_RECORDED);
1014 } else {
1015 SetLiveness(DemuxerStream::LIVENESS_UNKNOWN);
1018 // Good to go: set the duration and bitrate and notify we're done
1019 // initializing.
1020 host_->SetDuration(max_duration);
1021 duration_known_ = (max_duration != kInfiniteDuration());
1023 int64 filesize_in_bytes = 0;
1024 url_protocol_->GetSize(&filesize_in_bytes);
1025 bitrate_ = CalculateBitrate(format_context, max_duration, filesize_in_bytes);
1026 if (bitrate_ > 0)
1027 data_source_->SetBitrate(bitrate_);
1029 // Audio logging
1030 if (audio_stream) {
1031 AVCodecContext* audio_codec = audio_stream->codec;
1032 media_log_->SetBooleanProperty("found_audio_stream", true);
1034 SampleFormat sample_format = audio_config.sample_format();
1035 std::string sample_name = SampleFormatToString(sample_format);
1037 media_log_->SetStringProperty("audio_sample_format", sample_name);
1039 AVCodec* codec = avcodec_find_decoder(audio_codec->codec_id);
1040 if (codec) {
1041 media_log_->SetStringProperty("audio_codec_name", codec->name);
1044 media_log_->SetIntegerProperty("audio_channels_count",
1045 audio_codec->channels);
1046 media_log_->SetIntegerProperty("audio_samples_per_second",
1047 audio_config.samples_per_second());
1048 } else {
1049 media_log_->SetBooleanProperty("found_audio_stream", false);
1052 // Video logging
1053 if (video_stream) {
1054 AVCodecContext* video_codec = video_stream->codec;
1055 media_log_->SetBooleanProperty("found_video_stream", true);
1057 AVCodec* codec = avcodec_find_decoder(video_codec->codec_id);
1058 if (codec) {
1059 media_log_->SetStringProperty("video_codec_name", codec->name);
1060 } else if (video_codec->codec_id == AV_CODEC_ID_VP9) {
1061 // ffmpeg doesn't know about VP9 decoder. So we need to log it explicitly.
1062 media_log_->SetStringProperty("video_codec_name", "vp9");
1065 media_log_->SetIntegerProperty("width", video_codec->width);
1066 media_log_->SetIntegerProperty("height", video_codec->height);
1067 media_log_->SetIntegerProperty("coded_width",
1068 video_codec->coded_width);
1069 media_log_->SetIntegerProperty("coded_height",
1070 video_codec->coded_height);
1071 media_log_->SetStringProperty(
1072 "time_base",
1073 base::StringPrintf("%d/%d",
1074 video_codec->time_base.num,
1075 video_codec->time_base.den));
1076 media_log_->SetStringProperty(
1077 "video_format", VideoPixelFormatToString(video_config.format()));
1078 media_log_->SetBooleanProperty("video_is_encrypted",
1079 video_config.is_encrypted());
1080 } else {
1081 media_log_->SetBooleanProperty("found_video_stream", false);
1084 media_log_->SetTimeProperty("max_duration", max_duration);
1085 media_log_->SetTimeProperty("start_time", start_time_);
1086 media_log_->SetIntegerProperty("bitrate", bitrate_);
1088 status_cb.Run(PIPELINE_OK);
1091 void FFmpegDemuxer::OnSeekFrameDone(const PipelineStatusCB& cb, int result) {
1092 DCHECK(task_runner_->BelongsToCurrentThread());
1093 CHECK(pending_seek_);
1094 pending_seek_ = false;
1096 if (!blocking_thread_.IsRunning()) {
1097 MEDIA_LOG(ERROR, media_log_) << GetDisplayName() << ": bad state";
1098 cb.Run(PIPELINE_ERROR_ABORT);
1099 return;
1102 if (result < 0) {
1103 // Use VLOG(1) instead of NOTIMPLEMENTED() to prevent the message being
1104 // captured from stdout and contaminates testing.
1105 // TODO(scherkus): Implement this properly and signal error (BUG=23447).
1106 VLOG(1) << "Not implemented";
1109 // Tell streams to flush buffers due to seeking.
1110 StreamVector::iterator iter;
1111 for (iter = streams_.begin(); iter != streams_.end(); ++iter) {
1112 if (*iter)
1113 (*iter)->FlushBuffers();
1116 // Resume reading until capacity.
1117 ReadFrameIfNeeded();
1119 // Notify we're finished seeking.
1120 cb.Run(PIPELINE_OK);
1123 void FFmpegDemuxer::ReadFrameIfNeeded() {
1124 DCHECK(task_runner_->BelongsToCurrentThread());
1126 // Make sure we have work to do before reading.
1127 if (!blocking_thread_.IsRunning() || !StreamsHaveAvailableCapacity() ||
1128 pending_read_ || pending_seek_) {
1129 return;
1132 // Allocate and read an AVPacket from the media. Save |packet_ptr| since
1133 // evaluation order of packet.get() and base::Passed(&packet) is
1134 // undefined.
1135 ScopedAVPacket packet(new AVPacket());
1136 AVPacket* packet_ptr = packet.get();
1138 pending_read_ = true;
1139 base::PostTaskAndReplyWithResult(
1140 blocking_thread_.task_runner().get(),
1141 FROM_HERE,
1142 base::Bind(&av_read_frame, glue_->format_context(), packet_ptr),
1143 base::Bind(&FFmpegDemuxer::OnReadFrameDone,
1144 weak_factory_.GetWeakPtr(),
1145 base::Passed(&packet)));
1148 void FFmpegDemuxer::OnReadFrameDone(ScopedAVPacket packet, int result) {
1149 DCHECK(task_runner_->BelongsToCurrentThread());
1150 DCHECK(pending_read_);
1151 pending_read_ = false;
1153 if (!blocking_thread_.IsRunning() || pending_seek_) {
1154 return;
1157 // Consider the stream as ended if:
1158 // - either underlying ffmpeg returned an error
1159 // - or FFMpegDemuxer reached the maximum allowed memory usage.
1160 if (result < 0 || IsMaxMemoryUsageReached()) {
1161 // Update the duration based on the highest elapsed time across all streams
1162 // if it was previously unknown.
1163 if (!duration_known_) {
1164 base::TimeDelta max_duration;
1166 for (StreamVector::iterator iter = streams_.begin();
1167 iter != streams_.end();
1168 ++iter) {
1169 if (!*iter)
1170 continue;
1172 base::TimeDelta duration = (*iter)->GetElapsedTime();
1173 if (duration != kNoTimestamp() && duration > max_duration)
1174 max_duration = duration;
1177 if (max_duration > base::TimeDelta()) {
1178 host_->SetDuration(max_duration);
1179 duration_known_ = true;
1182 // If we have reached the end of stream, tell the downstream filters about
1183 // the event.
1184 StreamHasEnded();
1185 return;
1188 // Queue the packet with the appropriate stream.
1189 DCHECK_GE(packet->stream_index, 0);
1190 DCHECK_LT(packet->stream_index, static_cast<int>(streams_.size()));
1192 // Defend against ffmpeg giving us a bad stream index.
1193 if (packet->stream_index >= 0 &&
1194 packet->stream_index < static_cast<int>(streams_.size()) &&
1195 streams_[packet->stream_index]) {
1196 // TODO(scherkus): Fix demuxing upstream to never return packets w/o data
1197 // when av_read_frame() returns success code. See bug comment for ideas:
1199 // https://code.google.com/p/chromium/issues/detail?id=169133#c10
1200 if (!packet->data) {
1201 ScopedAVPacket new_packet(new AVPacket());
1202 av_new_packet(new_packet.get(), 0);
1203 av_packet_copy_props(new_packet.get(), packet.get());
1204 packet.swap(new_packet);
1207 // Special case for opus in ogg. FFmpeg is pre-trimming the codec delay
1208 // from the packet timestamp. Chrome expects to handle this itself inside
1209 // the decoder, so shift timestamps by the delay in this case.
1210 // TODO(dalecurtis): Try to get fixed upstream. See http://crbug.com/328207
1211 if (strcmp(glue_->format_context()->iformat->name, "ogg") == 0) {
1212 const AVCodecContext* codec_context =
1213 glue_->format_context()->streams[packet->stream_index]->codec;
1214 if (codec_context->codec_id == AV_CODEC_ID_OPUS &&
1215 codec_context->delay > 0) {
1216 packet->pts += codec_context->delay;
1220 FFmpegDemuxerStream* demuxer_stream = streams_[packet->stream_index];
1221 demuxer_stream->EnqueuePacket(packet.Pass());
1224 // Keep reading until we've reached capacity.
1225 ReadFrameIfNeeded();
1228 bool FFmpegDemuxer::StreamsHaveAvailableCapacity() {
1229 DCHECK(task_runner_->BelongsToCurrentThread());
1230 StreamVector::iterator iter;
1231 for (iter = streams_.begin(); iter != streams_.end(); ++iter) {
1232 if (*iter && (*iter)->HasAvailableCapacity()) {
1233 return true;
1236 return false;
1239 bool FFmpegDemuxer::IsMaxMemoryUsageReached() const {
1240 DCHECK(task_runner_->BelongsToCurrentThread());
1242 // Max allowed memory usage, all streams combined.
1243 const size_t kDemuxerMemoryLimit = 150 * 1024 * 1024;
1245 size_t memory_left = kDemuxerMemoryLimit;
1246 for (StreamVector::const_iterator iter = streams_.begin();
1247 iter != streams_.end(); ++iter) {
1248 if (!(*iter))
1249 continue;
1251 size_t stream_memory_usage = (*iter)->MemoryUsage();
1252 if (stream_memory_usage > memory_left)
1253 return true;
1254 memory_left -= stream_memory_usage;
1256 return false;
1259 void FFmpegDemuxer::StreamHasEnded() {
1260 DCHECK(task_runner_->BelongsToCurrentThread());
1261 StreamVector::iterator iter;
1262 for (iter = streams_.begin(); iter != streams_.end(); ++iter) {
1263 if (!*iter)
1264 continue;
1265 (*iter)->SetEndOfStream();
1269 void FFmpegDemuxer::OnEncryptedMediaInitData(
1270 EmeInitDataType init_data_type,
1271 const std::string& encryption_key_id) {
1272 std::vector<uint8> key_id_local(encryption_key_id.begin(),
1273 encryption_key_id.end());
1274 encrypted_media_init_data_cb_.Run(init_data_type, key_id_local);
1277 void FFmpegDemuxer::NotifyCapacityAvailable() {
1278 DCHECK(task_runner_->BelongsToCurrentThread());
1279 ReadFrameIfNeeded();
1282 void FFmpegDemuxer::NotifyBufferingChanged() {
1283 DCHECK(task_runner_->BelongsToCurrentThread());
1284 Ranges<base::TimeDelta> buffered;
1285 FFmpegDemuxerStream* audio = GetFFmpegStream(DemuxerStream::AUDIO);
1286 FFmpegDemuxerStream* video = GetFFmpegStream(DemuxerStream::VIDEO);
1287 if (audio && video) {
1288 buffered = audio->GetBufferedRanges().IntersectionWith(
1289 video->GetBufferedRanges());
1290 } else if (audio) {
1291 buffered = audio->GetBufferedRanges();
1292 } else if (video) {
1293 buffered = video->GetBufferedRanges();
1295 for (size_t i = 0; i < buffered.size(); ++i)
1296 host_->AddBufferedTimeRange(buffered.start(i), buffered.end(i));
1299 void FFmpegDemuxer::OnDataSourceError() {
1300 MEDIA_LOG(ERROR, media_log_) << GetDisplayName() << ": data source error";
1301 host_->OnDemuxerError(PIPELINE_ERROR_READ);
1304 void FFmpegDemuxer::SetLiveness(DemuxerStream::Liveness liveness) {
1305 DCHECK(task_runner_->BelongsToCurrentThread());
1306 for (const auto& stream : streams_) { // |stream| is a ref to a pointer.
1307 if (stream)
1308 stream->SetLiveness(liveness);
1312 } // namespace media