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/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" |
11 #include "base/bind.h" | 11 #include "base/bind.h" |
12 #include "base/callback.h" | 12 #include "base/callback.h" |
13 #include "base/callback_helpers.h" | 13 #include "base/callback_helpers.h" |
14 #include "base/command_line.h" | 14 #include "base/command_line.h" |
15 #include "base/memory/scoped_ptr.h" | 15 #include "base/memory/scoped_ptr.h" |
16 #include "base/message_loop/message_loop.h" | 16 #include "base/message_loop/message_loop.h" |
17 #include "base/metrics/sparse_histogram.h" | 17 #include "base/metrics/sparse_histogram.h" |
18 #include "base/stl_util.h" | 18 #include "base/stl_util.h" |
19 #include "base/strings/string_util.h" | 19 #include "base/strings/string_util.h" |
| 20 #include "base/strings/stringprintf.h" |
20 #include "base/task_runner_util.h" | 21 #include "base/task_runner_util.h" |
21 #include "base/time/time.h" | 22 #include "base/time/time.h" |
22 #include "media/base/audio_decoder_config.h" | 23 #include "media/base/audio_decoder_config.h" |
23 #include "media/base/bind_to_loop.h" | 24 #include "media/base/bind_to_loop.h" |
24 #include "media/base/decoder_buffer.h" | 25 #include "media/base/decoder_buffer.h" |
25 #include "media/base/decrypt_config.h" | 26 #include "media/base/decrypt_config.h" |
26 #include "media/base/limits.h" | 27 #include "media/base/limits.h" |
27 #include "media/base/media_log.h" | 28 #include "media/base/media_log.h" |
28 #include "media/base/media_switches.h" | 29 #include "media/base/media_switches.h" |
29 #include "media/base/video_decoder_config.h" | 30 #include "media/base/video_decoder_config.h" |
(...skipping 454 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
484 status_cb.Run(DEMUXER_ERROR_COULD_NOT_PARSE); | 485 status_cb.Run(DEMUXER_ERROR_COULD_NOT_PARSE); |
485 return; | 486 return; |
486 } | 487 } |
487 | 488 |
488 // Create demuxer stream entries for each possible AVStream. Each stream | 489 // Create demuxer stream entries for each possible AVStream. Each stream |
489 // is examined to determine if it is supported or not (is the codec enabled | 490 // is examined to determine if it is supported or not (is the codec enabled |
490 // for it in this release?). Unsupported streams are skipped, allowing for | 491 // for it in this release?). Unsupported streams are skipped, allowing for |
491 // partial playback. At least one audio or video stream must be playable. | 492 // partial playback. At least one audio or video stream must be playable. |
492 AVFormatContext* format_context = glue_->format_context(); | 493 AVFormatContext* format_context = glue_->format_context(); |
493 streams_.resize(format_context->nb_streams); | 494 streams_.resize(format_context->nb_streams); |
494 bool found_audio_stream = false; | 495 |
495 bool found_video_stream = false; | 496 AVStream* audio_stream = NULL; |
| 497 AudioDecoderConfig audio_config; |
| 498 |
| 499 AVStream* video_stream = NULL; |
| 500 VideoDecoderConfig video_config; |
496 | 501 |
497 base::TimeDelta max_duration; | 502 base::TimeDelta max_duration; |
498 for (size_t i = 0; i < format_context->nb_streams; ++i) { | 503 for (size_t i = 0; i < format_context->nb_streams; ++i) { |
499 AVStream* stream = format_context->streams[i]; | 504 AVStream* stream = format_context->streams[i]; |
500 AVCodecContext* codec_context = stream->codec; | 505 AVCodecContext* codec_context = stream->codec; |
501 AVMediaType codec_type = codec_context->codec_type; | 506 AVMediaType codec_type = codec_context->codec_type; |
502 | 507 |
503 if (codec_type == AVMEDIA_TYPE_AUDIO) { | 508 if (codec_type == AVMEDIA_TYPE_AUDIO) { |
504 if (found_audio_stream) | 509 if (audio_stream) |
505 continue; | 510 continue; |
| 511 |
506 // Log the codec detected, whether it is supported or not. | 512 // Log the codec detected, whether it is supported or not. |
507 UMA_HISTOGRAM_SPARSE_SLOWLY("Media.DetectedAudioCodec", | 513 UMA_HISTOGRAM_SPARSE_SLOWLY("Media.DetectedAudioCodec", |
508 codec_context->codec_id); | 514 codec_context->codec_id); |
509 // Ensure the codec is supported. IsValidConfig() also checks that the | 515 // Ensure the codec is supported. IsValidConfig() also checks that the |
510 // channel layout and sample format are valid. | 516 // channel layout and sample format are valid. |
511 AudioDecoderConfig audio_config; | |
512 AVStreamToAudioDecoderConfig(stream, &audio_config, false); | 517 AVStreamToAudioDecoderConfig(stream, &audio_config, false); |
513 if (!audio_config.IsValidConfig()) | 518 if (!audio_config.IsValidConfig()) |
514 continue; | 519 continue; |
515 found_audio_stream = true; | 520 audio_stream = stream; |
516 } else if (codec_type == AVMEDIA_TYPE_VIDEO) { | 521 } else if (codec_type == AVMEDIA_TYPE_VIDEO) { |
517 if (found_video_stream) | 522 if (video_stream) |
518 continue; | 523 continue; |
| 524 |
519 // Log the codec detected, whether it is supported or not. | 525 // Log the codec detected, whether it is supported or not. |
520 UMA_HISTOGRAM_SPARSE_SLOWLY("Media.DetectedVideoCodec", | 526 UMA_HISTOGRAM_SPARSE_SLOWLY("Media.DetectedVideoCodec", |
521 codec_context->codec_id); | 527 codec_context->codec_id); |
522 // Ensure the codec is supported. IsValidConfig() also checks that the | 528 // Ensure the codec is supported. IsValidConfig() also checks that the |
523 // frame size and visible size are valid. | 529 // frame size and visible size are valid. |
524 VideoDecoderConfig video_config; | |
525 AVStreamToVideoDecoderConfig(stream, &video_config, false); | 530 AVStreamToVideoDecoderConfig(stream, &video_config, false); |
| 531 |
526 if (!video_config.IsValidConfig()) | 532 if (!video_config.IsValidConfig()) |
527 continue; | 533 continue; |
528 found_video_stream = true; | 534 video_stream = stream; |
529 } else { | 535 } else { |
530 continue; | 536 continue; |
531 } | 537 } |
532 | 538 |
533 streams_[i] = new FFmpegDemuxerStream(this, stream); | 539 streams_[i] = new FFmpegDemuxerStream(this, stream); |
534 max_duration = std::max(max_duration, streams_[i]->duration()); | 540 max_duration = std::max(max_duration, streams_[i]->duration()); |
535 | 541 |
536 if (stream->first_dts != static_cast<int64_t>(AV_NOPTS_VALUE)) { | 542 if (stream->first_dts != static_cast<int64_t>(AV_NOPTS_VALUE)) { |
537 const base::TimeDelta first_dts = ConvertFromTimeBase( | 543 const base::TimeDelta first_dts = ConvertFromTimeBase( |
538 stream->time_base, stream->first_dts); | 544 stream->time_base, stream->first_dts); |
539 if (start_time_ == kNoTimestamp() || first_dts < start_time_) | 545 if (start_time_ == kNoTimestamp() || first_dts < start_time_) |
540 start_time_ = first_dts; | 546 start_time_ = first_dts; |
541 } | 547 } |
542 } | 548 } |
543 | 549 |
544 if (!found_audio_stream && !found_video_stream) { | 550 if (!audio_stream && !video_stream) { |
545 status_cb.Run(DEMUXER_ERROR_NO_SUPPORTED_STREAMS); | 551 status_cb.Run(DEMUXER_ERROR_NO_SUPPORTED_STREAMS); |
546 return; | 552 return; |
547 } | 553 } |
548 | 554 |
549 if (format_context->duration != static_cast<int64_t>(AV_NOPTS_VALUE)) { | 555 if (format_context->duration != static_cast<int64_t>(AV_NOPTS_VALUE)) { |
550 // If there is a duration value in the container use that to find the | 556 // If there is a duration value in the container use that to find the |
551 // maximum between it and the duration from A/V streams. | 557 // maximum between it and the duration from A/V streams. |
552 const AVRational av_time_base = {1, AV_TIME_BASE}; | 558 const AVRational av_time_base = {1, AV_TIME_BASE}; |
553 max_duration = | 559 max_duration = |
554 std::max(max_duration, | 560 std::max(max_duration, |
(...skipping 17 matching lines...) Expand all Loading... |
572 // initializing. | 578 // initializing. |
573 host_->SetDuration(max_duration); | 579 host_->SetDuration(max_duration); |
574 duration_known_ = (max_duration != kInfiniteDuration()); | 580 duration_known_ = (max_duration != kInfiniteDuration()); |
575 | 581 |
576 int64 filesize_in_bytes = 0; | 582 int64 filesize_in_bytes = 0; |
577 url_protocol_.GetSize(&filesize_in_bytes); | 583 url_protocol_.GetSize(&filesize_in_bytes); |
578 bitrate_ = CalculateBitrate(format_context, max_duration, filesize_in_bytes); | 584 bitrate_ = CalculateBitrate(format_context, max_duration, filesize_in_bytes); |
579 if (bitrate_ > 0) | 585 if (bitrate_ > 0) |
580 data_source_->SetBitrate(bitrate_); | 586 data_source_->SetBitrate(bitrate_); |
581 | 587 |
| 588 // Audio logging |
| 589 if (audio_stream) { |
| 590 AVCodecContext* audio_codec = audio_stream->codec; |
| 591 media_log_->SetBooleanProperty("found_audio_stream", true); |
| 592 |
| 593 SampleFormat sample_format = audio_config.sample_format(); |
| 594 std::string sample_name = SampleFormatToString(sample_format); |
| 595 |
| 596 media_log_->SetStringProperty("audio_sample_format", sample_name); |
| 597 |
| 598 media_log_->SetStringProperty("audio_codec_name", |
| 599 audio_codec->codec_name); |
| 600 media_log_->SetIntegerProperty("audio_sample_rate", |
| 601 audio_codec->sample_rate); |
| 602 media_log_->SetIntegerProperty("audio_channels_count", |
| 603 audio_codec->channels); |
| 604 media_log_->SetIntegerProperty("audio_samples_per_second", |
| 605 audio_config.samples_per_second()); |
| 606 } else { |
| 607 media_log_->SetBooleanProperty("found_audio_stream", false); |
| 608 } |
| 609 |
| 610 // Video logging |
| 611 if (video_stream) { |
| 612 AVCodecContext* video_codec = video_stream->codec; |
| 613 media_log_->SetBooleanProperty("found_video_stream", true); |
| 614 media_log_->SetStringProperty("video_codec_name", video_codec->codec_name); |
| 615 media_log_->SetIntegerProperty("width", video_codec->width); |
| 616 media_log_->SetIntegerProperty("height", video_codec->height); |
| 617 media_log_->SetIntegerProperty("coded_width", |
| 618 video_codec->coded_width); |
| 619 media_log_->SetIntegerProperty("coded_height", |
| 620 video_codec->coded_height); |
| 621 media_log_->SetStringProperty( |
| 622 "time_base", |
| 623 base::StringPrintf("%d/%d", |
| 624 video_codec->time_base.num, |
| 625 video_codec->time_base.den)); |
| 626 media_log_->SetStringProperty( |
| 627 "video_format", VideoFrame::FormatToString(video_config.format())); |
| 628 media_log_->SetBooleanProperty("video_is_encrypted", |
| 629 video_config.is_encrypted()); |
| 630 } else { |
| 631 media_log_->SetBooleanProperty("found_video_stream", false); |
| 632 } |
| 633 |
| 634 |
| 635 media_log_->SetDoubleProperty("max_duration", max_duration.InSecondsF()); |
| 636 media_log_->SetDoubleProperty("start_time", start_time_.InSecondsF()); |
| 637 media_log_->SetDoubleProperty("filesize_in_bytes", |
| 638 static_cast<double>(filesize_in_bytes)); |
| 639 media_log_->SetIntegerProperty("bitrate", bitrate_); |
| 640 |
582 status_cb.Run(PIPELINE_OK); | 641 status_cb.Run(PIPELINE_OK); |
583 } | 642 } |
584 | 643 |
585 void FFmpegDemuxer::OnSeekFrameDone(const PipelineStatusCB& cb, int result) { | 644 void FFmpegDemuxer::OnSeekFrameDone(const PipelineStatusCB& cb, int result) { |
586 DCHECK(message_loop_->BelongsToCurrentThread()); | 645 DCHECK(message_loop_->BelongsToCurrentThread()); |
587 CHECK(pending_seek_); | 646 CHECK(pending_seek_); |
588 pending_seek_ = false; | 647 pending_seek_ = false; |
589 | 648 |
590 if (!blocking_thread_.IsRunning()) { | 649 if (!blocking_thread_.IsRunning()) { |
591 cb.Run(PIPELINE_ERROR_ABORT); | 650 cb.Run(PIPELINE_ERROR_ABORT); |
(...skipping 185 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
777 } | 836 } |
778 for (size_t i = 0; i < buffered.size(); ++i) | 837 for (size_t i = 0; i < buffered.size(); ++i) |
779 host_->AddBufferedTimeRange(buffered.start(i), buffered.end(i)); | 838 host_->AddBufferedTimeRange(buffered.start(i), buffered.end(i)); |
780 } | 839 } |
781 | 840 |
782 void FFmpegDemuxer::OnDataSourceError() { | 841 void FFmpegDemuxer::OnDataSourceError() { |
783 host_->OnDemuxerError(PIPELINE_ERROR_READ); | 842 host_->OnDemuxerError(PIPELINE_ERROR_READ); |
784 } | 843 } |
785 | 844 |
786 } // namespace media | 845 } // namespace media |
OLD | NEW |