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

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

Issue 1735803002: Implemented passing media track info from ffmpeg into blink (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: wolenetz@ CR feedback + better track info extraction in ffmpeg Created 4 years, 9 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
« 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 <utility> 8 #include <utility>
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_helpers.h" 12 #include "base/callback_helpers.h"
13 #include "base/macros.h" 13 #include "base/macros.h"
14 #include "base/memory/scoped_ptr.h" 14 #include "base/memory/scoped_ptr.h"
15 #include "base/metrics/histogram_macros.h" 15 #include "base/metrics/histogram_macros.h"
16 #include "base/metrics/sparse_histogram.h" 16 #include "base/metrics/sparse_histogram.h"
17 #include "base/single_thread_task_runner.h" 17 #include "base/single_thread_task_runner.h"
18 #include "base/strings/string_number_conversions.h" 18 #include "base/strings/string_number_conversions.h"
19 #include "base/strings/string_util.h" 19 #include "base/strings/string_util.h"
20 #include "base/strings/stringprintf.h" 20 #include "base/strings/stringprintf.h"
21 #include "base/sys_byteorder.h" 21 #include "base/sys_byteorder.h"
22 #include "base/task_runner_util.h" 22 #include "base/task_runner_util.h"
23 #include "base/thread_task_runner_handle.h" 23 #include "base/thread_task_runner_handle.h"
24 #include "base/time/time.h" 24 #include "base/time/time.h"
25 #include "media/audio/sample_rates.h" 25 #include "media/audio/sample_rates.h"
26 #include "media/base/bind_to_current_loop.h" 26 #include "media/base/bind_to_current_loop.h"
27 #include "media/base/decrypt_config.h" 27 #include "media/base/decrypt_config.h"
28 #include "media/base/limits.h" 28 #include "media/base/limits.h"
29 #include "media/base/media_log.h" 29 #include "media/base/media_log.h"
30 #include "media/base/media_tracks.h"
30 #include "media/base/timestamp_constants.h" 31 #include "media/base/timestamp_constants.h"
31 #include "media/ffmpeg/ffmpeg_common.h" 32 #include "media/ffmpeg/ffmpeg_common.h"
32 #include "media/filters/ffmpeg_aac_bitstream_converter.h" 33 #include "media/filters/ffmpeg_aac_bitstream_converter.h"
33 #include "media/filters/ffmpeg_bitstream_converter.h" 34 #include "media/filters/ffmpeg_bitstream_converter.h"
34 #include "media/filters/ffmpeg_glue.h" 35 #include "media/filters/ffmpeg_glue.h"
35 #include "media/filters/ffmpeg_h264_to_annex_b_bitstream_converter.h" 36 #include "media/filters/ffmpeg_h264_to_annex_b_bitstream_converter.h"
36 #include "media/filters/webvtt_util.h" 37 #include "media/filters/webvtt_util.h"
37 #include "media/formats/webm/webm_crypto_helpers.h" 38 #include "media/formats/webm/webm_crypto_helpers.h"
38 #include "media/media_features.h" 39 #include "media/media_features.h"
39 40
(...skipping 694 matching lines...) Expand 10 before | Expand all | Expand 10 after
734 return ConvertFromTimeBase(time_base, timestamp); 735 return ConvertFromTimeBase(time_base, timestamp);
735 } 736 }
736 737
737 // 738 //
738 // FFmpegDemuxer 739 // FFmpegDemuxer
739 // 740 //
740 FFmpegDemuxer::FFmpegDemuxer( 741 FFmpegDemuxer::FFmpegDemuxer(
741 const scoped_refptr<base::SingleThreadTaskRunner>& task_runner, 742 const scoped_refptr<base::SingleThreadTaskRunner>& task_runner,
742 DataSource* data_source, 743 DataSource* data_source,
743 const EncryptedMediaInitDataCB& encrypted_media_init_data_cb, 744 const EncryptedMediaInitDataCB& encrypted_media_init_data_cb,
745 const MediaTracksUpdatedCB& media_tracks_updated_cb,
744 const scoped_refptr<MediaLog>& media_log) 746 const scoped_refptr<MediaLog>& media_log)
745 : host_(NULL), 747 : host_(NULL),
746 task_runner_(task_runner), 748 task_runner_(task_runner),
747 blocking_thread_("FFmpegDemuxer"), 749 blocking_thread_("FFmpegDemuxer"),
748 pending_read_(false), 750 pending_read_(false),
749 pending_seek_(false), 751 pending_seek_(false),
750 data_source_(data_source), 752 data_source_(data_source),
751 media_log_(media_log), 753 media_log_(media_log),
752 bitrate_(0), 754 bitrate_(0),
753 start_time_(kNoTimestamp()), 755 start_time_(kNoTimestamp()),
754 preferred_stream_for_seeking_(-1, kNoTimestamp()), 756 preferred_stream_for_seeking_(-1, kNoTimestamp()),
755 fallback_stream_for_seeking_(-1, kNoTimestamp()), 757 fallback_stream_for_seeking_(-1, kNoTimestamp()),
756 text_enabled_(false), 758 text_enabled_(false),
757 duration_known_(false), 759 duration_known_(false),
758 encrypted_media_init_data_cb_(encrypted_media_init_data_cb), 760 encrypted_media_init_data_cb_(encrypted_media_init_data_cb),
761 media_tracks_updated_cb_(media_tracks_updated_cb),
759 weak_factory_(this) { 762 weak_factory_(this) {
760 DCHECK(task_runner_.get()); 763 DCHECK(task_runner_.get());
761 DCHECK(data_source_); 764 DCHECK(data_source_);
765 DCHECK(!media_tracks_updated_cb_.is_null());
762 } 766 }
763 767
764 FFmpegDemuxer::~FFmpegDemuxer() {} 768 FFmpegDemuxer::~FFmpegDemuxer() {}
765 769
766 void FFmpegDemuxer::Stop() { 770 void FFmpegDemuxer::Stop() {
767 DCHECK(task_runner_->BelongsToCurrentThread()); 771 DCHECK(task_runner_->BelongsToCurrentThread());
768 772
769 // The order of Stop() and Abort() is important here. If Abort() is called 773 // The order of Stop() and Abort() is important here. If Abort() is called
770 // first, control may pass into FFmpeg where it can destruct buffers that are 774 // first, control may pass into FFmpeg where it can destruct buffers that are
771 // in the process of being fulfilled by the DataSource. 775 // in the process of being fulfilled by the DataSource.
(...skipping 275 matching lines...) Expand 10 before | Expand all | Expand 10 after
1047 if (packet_buffer->pkt.pts != static_cast<int64_t>(AV_NOPTS_VALUE)) { 1051 if (packet_buffer->pkt.pts != static_cast<int64_t>(AV_NOPTS_VALUE)) {
1048 const base::TimeDelta packet_pts = 1052 const base::TimeDelta packet_pts =
1049 ConvertFromTimeBase(stream->time_base, packet_buffer->pkt.pts); 1053 ConvertFromTimeBase(stream->time_base, packet_buffer->pkt.pts);
1050 if (packet_pts < start_time_estimates[stream->index]) 1054 if (packet_pts < start_time_estimates[stream->index])
1051 start_time_estimates[stream->index] = packet_pts; 1055 start_time_estimates[stream->index] = packet_pts;
1052 } 1056 }
1053 packet_buffer = packet_buffer->next; 1057 packet_buffer = packet_buffer->next;
1054 } 1058 }
1055 } 1059 }
1056 1060
1061 scoped_ptr<MediaTracks> media_tracks(new MediaTracks());
1057 AVStream* audio_stream = NULL; 1062 AVStream* audio_stream = NULL;
1058 AudioDecoderConfig audio_config; 1063 AudioDecoderConfig audio_config;
1059 AVStream* video_stream = NULL; 1064 AVStream* video_stream = NULL;
1060 VideoDecoderConfig video_config; 1065 VideoDecoderConfig video_config;
1061 1066
1062 // If available, |start_time_| will be set to the lowest stream start time. 1067 // If available, |start_time_| will be set to the lowest stream start time.
1063 start_time_ = kInfiniteDuration(); 1068 start_time_ = kInfiniteDuration();
1064 1069
1065 base::TimeDelta max_duration; 1070 base::TimeDelta max_duration;
1066 for (size_t i = 0; i < format_context->nb_streams; ++i) { 1071 for (size_t i = 0; i < format_context->nb_streams; ++i) {
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after
1116 // things like: codec, channel layout, sample/pixel format, etc... 1121 // things like: codec, channel layout, sample/pixel format, etc...
1117 scoped_ptr<FFmpegDemuxerStream> demuxer_stream = 1122 scoped_ptr<FFmpegDemuxerStream> demuxer_stream =
1118 FFmpegDemuxerStream::Create(this, stream, media_log_); 1123 FFmpegDemuxerStream::Create(this, stream, media_log_);
1119 if (demuxer_stream.get()) { 1124 if (demuxer_stream.get()) {
1120 streams_[i] = demuxer_stream.release(); 1125 streams_[i] = demuxer_stream.release();
1121 } else { 1126 } else {
1122 // This AVStream does not successfully convert. 1127 // This AVStream does not successfully convert.
1123 continue; 1128 continue;
1124 } 1129 }
1125 1130
1131 AVDictionaryEntry* lang_tag =
1132 av_dict_get(stream->metadata, "language", NULL, 0);
1133 std::string track_language = "und";
1134 if (lang_tag && lang_tag->value)
1135 track_language = lang_tag->value;
1136
1137 std::string track_label = "";
1138 AVDictionaryEntry* handler_tag =
1139 av_dict_get(stream->metadata, "handler_name", NULL, 0);
1140 if (handler_tag && handler_tag->value)
1141 track_label = handler_tag->value;
1142
1126 // Note when we find our audio/video stream (we only want one of each) and 1143 // Note when we find our audio/video stream (we only want one of each) and
1127 // record src= playback UMA stats for the stream's decoder config. 1144 // record src= playback UMA stats for the stream's decoder config.
1128 if (codec_type == AVMEDIA_TYPE_AUDIO) { 1145 if (codec_type == AVMEDIA_TYPE_AUDIO) {
1129 CHECK(!audio_stream); 1146 CHECK(!audio_stream);
1130 audio_stream = stream; 1147 audio_stream = stream;
1131 audio_config = streams_[i]->audio_decoder_config(); 1148 audio_config = streams_[i]->audio_decoder_config();
1132 RecordAudioCodecStats(audio_config); 1149 RecordAudioCodecStats(audio_config);
1150
1151 media_tracks->AddAudioTrack(audio_config, base::UintToString(stream->id),
1152 "main", track_label, track_language);
1133 } else if (codec_type == AVMEDIA_TYPE_VIDEO) { 1153 } else if (codec_type == AVMEDIA_TYPE_VIDEO) {
1134 CHECK(!video_stream); 1154 CHECK(!video_stream);
1135 video_stream = stream; 1155 video_stream = stream;
1136 video_config = streams_[i]->video_decoder_config(); 1156 video_config = streams_[i]->video_decoder_config();
1137 RecordVideoCodecStats(video_config, stream->codec->color_range); 1157 RecordVideoCodecStats(video_config, stream->codec->color_range);
1158
1159 media_tracks->AddVideoTrack(video_config, base::UintToString(stream->id),
1160 "main", track_label, track_language);
1138 } 1161 }
1139 1162
1140 max_duration = std::max(max_duration, streams_[i]->duration()); 1163 max_duration = std::max(max_duration, streams_[i]->duration());
1141 1164
1142 const base::TimeDelta start_time = 1165 const base::TimeDelta start_time =
1143 ExtractStartTime(stream, start_time_estimates[i]); 1166 ExtractStartTime(stream, start_time_estimates[i]);
1144 const bool has_start_time = start_time != kNoTimestamp(); 1167 const bool has_start_time = start_time != kNoTimestamp();
1145 1168
1146 // Always prefer the video stream for seeking. If none exists, we'll swap 1169 // Always prefer the video stream for seeking. If none exists, we'll swap
1147 // the fallback stream with the preferred stream below. 1170 // the fallback stream with the preferred stream below.
(...skipping 165 matching lines...) Expand 10 before | Expand all | Expand 10 after
1313 media_log_->SetBooleanProperty("video_is_encrypted", 1336 media_log_->SetBooleanProperty("video_is_encrypted",
1314 video_config.is_encrypted()); 1337 video_config.is_encrypted());
1315 } else { 1338 } else {
1316 media_log_->SetBooleanProperty("found_video_stream", false); 1339 media_log_->SetBooleanProperty("found_video_stream", false);
1317 } 1340 }
1318 1341
1319 media_log_->SetTimeProperty("max_duration", max_duration); 1342 media_log_->SetTimeProperty("max_duration", max_duration);
1320 media_log_->SetTimeProperty("start_time", start_time_); 1343 media_log_->SetTimeProperty("start_time", start_time_);
1321 media_log_->SetIntegerProperty("bitrate", bitrate_); 1344 media_log_->SetIntegerProperty("bitrate", bitrate_);
1322 1345
1346 media_tracks_updated_cb_.Run(std::move(media_tracks));
1347
1323 status_cb.Run(PIPELINE_OK); 1348 status_cb.Run(PIPELINE_OK);
1324 } 1349 }
1325 1350
1326 void FFmpegDemuxer::OnSeekFrameDone(const PipelineStatusCB& cb, int result) { 1351 void FFmpegDemuxer::OnSeekFrameDone(const PipelineStatusCB& cb, int result) {
1327 DCHECK(task_runner_->BelongsToCurrentThread()); 1352 DCHECK(task_runner_->BelongsToCurrentThread());
1328 CHECK(pending_seek_); 1353 CHECK(pending_seek_);
1329 pending_seek_ = false; 1354 pending_seek_ = false;
1330 1355
1331 if (!blocking_thread_.IsRunning()) { 1356 if (!blocking_thread_.IsRunning()) {
1332 MEDIA_LOG(ERROR, media_log_) << GetDisplayName() << ": bad state"; 1357 MEDIA_LOG(ERROR, media_log_) << GetDisplayName() << ": bad state";
(...skipping 191 matching lines...) Expand 10 before | Expand all | Expand 10 after
1524 1549
1525 void FFmpegDemuxer::SetLiveness(DemuxerStream::Liveness liveness) { 1550 void FFmpegDemuxer::SetLiveness(DemuxerStream::Liveness liveness) {
1526 DCHECK(task_runner_->BelongsToCurrentThread()); 1551 DCHECK(task_runner_->BelongsToCurrentThread());
1527 for (const auto& stream : streams_) { // |stream| is a ref to a pointer. 1552 for (const auto& stream : streams_) { // |stream| is a ref to a pointer.
1528 if (stream) 1553 if (stream)
1529 stream->SetLiveness(liveness); 1554 stream->SetLiveness(liveness);
1530 } 1555 }
1531 } 1556 }
1532 1557
1533 } // namespace media 1558 } // 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