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

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

Issue 23702007: Render inband text tracks in the media pipeline (Closed) Base URL: http://git.chromium.org/chromium/src.git@master
Patch Set: incorporate aaron's comments (10/22) Created 7 years, 2 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
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"
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"
15 #include "base/memory/scoped_ptr.h" 14 #include "base/memory/scoped_ptr.h"
16 #include "base/message_loop/message_loop.h" 15 #include "base/message_loop/message_loop.h"
17 #include "base/metrics/sparse_histogram.h" 16 #include "base/metrics/sparse_histogram.h"
18 #include "base/stl_util.h"
19 #include "base/strings/string_util.h" 17 #include "base/strings/string_util.h"
20 #include "base/strings/stringprintf.h" 18 #include "base/strings/stringprintf.h"
21 #include "base/task_runner_util.h" 19 #include "base/task_runner_util.h"
22 #include "base/time/time.h" 20 #include "base/time/time.h"
23 #include "media/base/audio_decoder_config.h" 21 #include "media/base/audio_decoder_config.h"
24 #include "media/base/bind_to_loop.h" 22 #include "media/base/bind_to_loop.h"
25 #include "media/base/decoder_buffer.h" 23 #include "media/base/decoder_buffer.h"
26 #include "media/base/decrypt_config.h" 24 #include "media/base/decrypt_config.h"
27 #include "media/base/limits.h" 25 #include "media/base/limits.h"
28 #include "media/base/media_log.h" 26 #include "media/base/media_log.h"
29 #include "media/base/media_switches.h"
30 #include "media/base/video_decoder_config.h" 27 #include "media/base/video_decoder_config.h"
31 #include "media/ffmpeg/ffmpeg_common.h" 28 #include "media/ffmpeg/ffmpeg_common.h"
32 #include "media/filters/ffmpeg_glue.h" 29 #include "media/filters/ffmpeg_glue.h"
33 #include "media/filters/ffmpeg_h264_to_annex_b_bitstream_converter.h" 30 #include "media/filters/ffmpeg_h264_to_annex_b_bitstream_converter.h"
34 #include "media/webm/webm_crypto_helpers.h" 31 #include "media/webm/webm_crypto_helpers.h"
35 32
36 namespace media { 33 namespace media {
37 34
38 // 35 //
39 // FFmpegDemuxerStream 36 // FFmpegDemuxerStream
(...skipping 17 matching lines...) Expand all
57 case AVMEDIA_TYPE_AUDIO: 54 case AVMEDIA_TYPE_AUDIO:
58 type_ = AUDIO; 55 type_ = AUDIO;
59 AVStreamToAudioDecoderConfig(stream, &audio_config_, true); 56 AVStreamToAudioDecoderConfig(stream, &audio_config_, true);
60 is_encrypted = audio_config_.is_encrypted(); 57 is_encrypted = audio_config_.is_encrypted();
61 break; 58 break;
62 case AVMEDIA_TYPE_VIDEO: 59 case AVMEDIA_TYPE_VIDEO:
63 type_ = VIDEO; 60 type_ = VIDEO;
64 AVStreamToVideoDecoderConfig(stream, &video_config_, true); 61 AVStreamToVideoDecoderConfig(stream, &video_config_, true);
65 is_encrypted = video_config_.is_encrypted(); 62 is_encrypted = video_config_.is_encrypted();
66 break; 63 break;
64 case AVMEDIA_TYPE_SUBTITLE:
65 type_ = TEXT;
66 break;
67 default: 67 default:
68 NOTREACHED(); 68 NOTREACHED();
69 break; 69 break;
70 } 70 }
71 71
72 // Calculate the duration. 72 // Calculate the duration.
73 duration_ = ConvertStreamTimestamp(stream->time_base, stream->duration); 73 duration_ = ConvertStreamTimestamp(stream->time_base, stream->duration);
74 74
75 if (stream_->codec->codec_id == AV_CODEC_ID_H264) { 75 if (stream_->codec->codec_id == AV_CODEC_ID_H264) {
76 bitstream_converter_.reset( 76 bitstream_converter_.reset(
(...skipping 26 matching lines...) Expand all
103 NOTREACHED() << "Attempted to enqueue packet on a stopped stream"; 103 NOTREACHED() << "Attempted to enqueue packet on a stopped stream";
104 return; 104 return;
105 } 105 }
106 106
107 // Convert the packet if there is a bitstream filter. 107 // Convert the packet if there is a bitstream filter.
108 if (packet->data && bitstream_converter_enabled_ && 108 if (packet->data && bitstream_converter_enabled_ &&
109 !bitstream_converter_->ConvertPacket(packet.get())) { 109 !bitstream_converter_->ConvertPacket(packet.get())) {
110 LOG(ERROR) << "Format conversion failed."; 110 LOG(ERROR) << "Format conversion failed.";
111 } 111 }
112 112
113 // Get side data if any. For now, the only type of side_data is VP8 Alpha. We 113 scoped_refptr<DecoderBuffer> buffer;
114 // keep this generic so that other side_data types in the future can be 114
115 // handled the same way as well. 115 // Get side data if any. For now, the only types of side_data are VP8 Alpha,
116 // and WebVTT id and settings. We keep this generic so that other side_data
117 // types in the future can be handled the same way as well.
116 av_packet_split_side_data(packet.get()); 118 av_packet_split_side_data(packet.get());
117 int side_data_size = 0; 119 if (type() == DemuxerStream::TEXT) {
118 uint8* side_data = av_packet_get_side_data( 120 int id_size = 0;
119 packet.get(), 121 uint8* id_data = av_packet_get_side_data(
120 AV_PKT_DATA_MATROSKA_BLOCKADDITIONAL, 122 packet.get(),
121 &side_data_size); 123 AV_PKT_DATA_WEBVTT_IDENTIFIER,
124 &id_size);
122 125
123 // If a packet is returned by FFmpeg's av_parser_parse2() the packet will 126 int settings_size = 0;
124 // reference inner memory of FFmpeg. As such we should transfer the packet 127 uint8* settings_data = av_packet_get_side_data(
125 // into memory we control. 128 packet.get(),
126 scoped_refptr<DecoderBuffer> buffer; 129 AV_PKT_DATA_WEBVTT_SETTINGS,
127 if (side_data_size > 0) { 130 &settings_size);
131
132 std::vector<uint8> side_data;
133 DecoderBuffer::MakeSideData(id_data, id_data + id_size,
134 settings_data, settings_data + settings_size,
135 &side_data);
136
128 buffer = DecoderBuffer::CopyFrom(packet.get()->data, packet.get()->size, 137 buffer = DecoderBuffer::CopyFrom(packet.get()->data, packet.get()->size,
129 side_data, side_data_size); 138 side_data.data(), side_data.size());
130 } else { 139 } else {
131 buffer = DecoderBuffer::CopyFrom(packet.get()->data, packet.get()->size); 140 int side_data_size = 0;
141 uint8* side_data = av_packet_get_side_data(
142 packet.get(),
143 AV_PKT_DATA_MATROSKA_BLOCKADDITIONAL,
144 &side_data_size);
145
146 // If a packet is returned by FFmpeg's av_parser_parse2() the packet will
147 // reference inner memory of FFmpeg. As such we should transfer the packet
148 // into memory we control.
149 if (side_data_size > 0) {
150 buffer = DecoderBuffer::CopyFrom(packet.get()->data, packet.get()->size,
151 side_data, side_data_size);
152 } else {
153 buffer = DecoderBuffer::CopyFrom(packet.get()->data, packet.get()->size);
154 }
132 } 155 }
133 156
134 if ((type() == DemuxerStream::AUDIO && audio_config_.is_encrypted()) || 157 if ((type() == DemuxerStream::AUDIO && audio_config_.is_encrypted()) ||
135 (type() == DemuxerStream::VIDEO && video_config_.is_encrypted())) { 158 (type() == DemuxerStream::VIDEO && video_config_.is_encrypted())) {
136 scoped_ptr<DecryptConfig> config(WebMCreateDecryptConfig( 159 scoped_ptr<DecryptConfig> config(WebMCreateDecryptConfig(
137 packet->data, packet->size, 160 packet->data, packet->size,
138 reinterpret_cast<const uint8*>(encryption_key_id_.data()), 161 reinterpret_cast<const uint8*>(encryption_key_id_.data()),
139 encryption_key_id_.size())); 162 encryption_key_id_.size()));
140 if (!config) 163 if (!config)
141 LOG(ERROR) << "Creation of DecryptConfig failed."; 164 LOG(ERROR) << "Creation of DecryptConfig failed.";
142 buffer->set_decrypt_config(config.Pass()); 165 buffer->set_decrypt_config(config.Pass());
143 } 166 }
144 167
145 buffer->set_timestamp(ConvertStreamTimestamp( 168 buffer->set_timestamp(ConvertStreamTimestamp(
146 stream_->time_base, packet->pts)); 169 stream_->time_base, packet->pts));
147 buffer->set_duration(ConvertStreamTimestamp( 170 buffer->set_duration(ConvertStreamTimestamp(
(...skipping 117 matching lines...) Expand 10 before | Expand all | Expand 10 after
265 // TODO(scherkus): Remove early return and reenable time-based capacity 288 // TODO(scherkus): Remove early return and reenable time-based capacity
266 // after our data sources support canceling/concurrent reads, see 289 // after our data sources support canceling/concurrent reads, see
267 // http://crbug.com/165762 for details. 290 // http://crbug.com/165762 for details.
268 return !read_cb_.is_null(); 291 return !read_cb_.is_null();
269 292
270 // Try to have one second's worth of encoded data per stream. 293 // Try to have one second's worth of encoded data per stream.
271 const base::TimeDelta kCapacity = base::TimeDelta::FromSeconds(1); 294 const base::TimeDelta kCapacity = base::TimeDelta::FromSeconds(1);
272 return buffer_queue_.IsEmpty() || buffer_queue_.Duration() < kCapacity; 295 return buffer_queue_.IsEmpty() || buffer_queue_.Duration() < kCapacity;
273 } 296 }
274 297
298 TextKind FFmpegDemuxerStream::GetTextKind() const {
299 DCHECK_EQ(type_, DemuxerStream::TEXT);
300
301 if (stream_->disposition & AV_DISPOSITION_CAPTIONS)
302 return kTextCaptions;
303
304 if (stream_->disposition & AV_DISPOSITION_DESCRIPTIONS)
305 return kTextDescriptions;
306
307 if (stream_->disposition & AV_DISPOSITION_METADATA)
308 return kTextMetadata;
309
310 return kTextSubtitles;
311 }
312
313 std::string FFmpegDemuxerStream::GetMetadata(const char* key) const {
314 const AVDictionaryEntry* entry =
315 av_dict_get(stream_->metadata, key, NULL, 0);
316 return (entry == NULL || entry->value == NULL) ? "" : entry->value;
317 }
318
275 // static 319 // static
276 base::TimeDelta FFmpegDemuxerStream::ConvertStreamTimestamp( 320 base::TimeDelta FFmpegDemuxerStream::ConvertStreamTimestamp(
277 const AVRational& time_base, int64 timestamp) { 321 const AVRational& time_base, int64 timestamp) {
278 if (timestamp == static_cast<int64>(AV_NOPTS_VALUE)) 322 if (timestamp == static_cast<int64>(AV_NOPTS_VALUE))
279 return kNoTimestamp(); 323 return kNoTimestamp();
280 324
281 return ConvertFromTimeBase(time_base, timestamp); 325 return ConvertFromTimeBase(time_base, timestamp);
282 } 326 }
283 327
284 // 328 //
285 // FFmpegDemuxer 329 // FFmpegDemuxer
286 // 330 //
287 FFmpegDemuxer::FFmpegDemuxer( 331 FFmpegDemuxer::FFmpegDemuxer(
288 const scoped_refptr<base::MessageLoopProxy>& message_loop, 332 const scoped_refptr<base::MessageLoopProxy>& message_loop,
289 DataSource* data_source, 333 DataSource* data_source,
290 const NeedKeyCB& need_key_cb, 334 const NeedKeyCB& need_key_cb,
335 bool text_enabled,
291 const scoped_refptr<MediaLog>& media_log) 336 const scoped_refptr<MediaLog>& media_log)
292 : host_(NULL), 337 : host_(NULL),
293 message_loop_(message_loop), 338 message_loop_(message_loop),
294 weak_factory_(this), 339 weak_factory_(this),
295 blocking_thread_("FFmpegDemuxer"), 340 blocking_thread_("FFmpegDemuxer"),
296 pending_read_(false), 341 pending_read_(false),
297 pending_seek_(false), 342 pending_seek_(false),
298 data_source_(data_source), 343 data_source_(data_source),
299 media_log_(media_log), 344 media_log_(media_log),
300 bitrate_(0), 345 bitrate_(0),
301 start_time_(kNoTimestamp()), 346 start_time_(kNoTimestamp()),
302 audio_disabled_(false), 347 audio_disabled_(false),
348 text_enabled_(text_enabled),
303 duration_known_(false), 349 duration_known_(false),
304 url_protocol_(data_source, BindToLoop(message_loop_, base::Bind( 350 url_protocol_(data_source, BindToLoop(message_loop_, base::Bind(
305 &FFmpegDemuxer::OnDataSourceError, base::Unretained(this)))), 351 &FFmpegDemuxer::OnDataSourceError, base::Unretained(this)))),
306 need_key_cb_(need_key_cb) { 352 need_key_cb_(need_key_cb) {
307 DCHECK(message_loop_.get()); 353 DCHECK(message_loop_.get());
308 DCHECK(data_source_); 354 DCHECK(data_source_);
309 } 355 }
310 356
311 FFmpegDemuxer::~FFmpegDemuxer() {} 357 FFmpegDemuxer::~FFmpegDemuxer() {}
312 358
(...skipping 84 matching lines...) Expand 10 before | Expand all | Expand 10 after
397 } 443 }
398 } 444 }
399 return NULL; 445 return NULL;
400 } 446 }
401 447
402 base::TimeDelta FFmpegDemuxer::GetStartTime() const { 448 base::TimeDelta FFmpegDemuxer::GetStartTime() const {
403 DCHECK(message_loop_->BelongsToCurrentThread()); 449 DCHECK(message_loop_->BelongsToCurrentThread());
404 return start_time_; 450 return start_time_;
405 } 451 }
406 452
453 void FFmpegDemuxer::AddTextStreams() {
454 DCHECK(message_loop_->BelongsToCurrentThread());
455
456 for (StreamVector::size_type idx = 0; idx < streams_.size(); ++idx) {
457 FFmpegDemuxerStream* stream = streams_[idx];
458 if (stream == NULL || stream->type() != DemuxerStream::TEXT)
459 continue;
460
461 TextKind kind = stream->GetTextKind();
462 std::string title = stream->GetMetadata("title");
463 std::string language = stream->GetMetadata("language");
464
465 host_->AddTextStream(stream, TextTrackConfig(kind, title, language));
466 }
467 }
468
407 // Helper for calculating the bitrate of the media based on information stored 469 // Helper for calculating the bitrate of the media based on information stored
408 // in |format_context| or failing that the size and duration of the media. 470 // in |format_context| or failing that the size and duration of the media.
409 // 471 //
410 // Returns 0 if a bitrate could not be determined. 472 // Returns 0 if a bitrate could not be determined.
411 static int CalculateBitrate( 473 static int CalculateBitrate(
412 AVFormatContext* format_context, 474 AVFormatContext* format_context,
413 const base::TimeDelta& duration, 475 const base::TimeDelta& duration,
414 int64 filesize_in_bytes) { 476 int64 filesize_in_bytes) {
415 // If there is a bitrate set on the container, use it. 477 // If there is a bitrate set on the container, use it.
416 if (format_context->bit_rate > 0) 478 if (format_context->bit_rate > 0)
(...skipping 98 matching lines...) Expand 10 before | Expand all | Expand 10 after
515 // Log the codec detected, whether it is supported or not. 577 // Log the codec detected, whether it is supported or not.
516 UMA_HISTOGRAM_SPARSE_SLOWLY("Media.DetectedVideoCodec", 578 UMA_HISTOGRAM_SPARSE_SLOWLY("Media.DetectedVideoCodec",
517 codec_context->codec_id); 579 codec_context->codec_id);
518 // Ensure the codec is supported. IsValidConfig() also checks that the 580 // Ensure the codec is supported. IsValidConfig() also checks that the
519 // frame size and visible size are valid. 581 // frame size and visible size are valid.
520 AVStreamToVideoDecoderConfig(stream, &video_config, false); 582 AVStreamToVideoDecoderConfig(stream, &video_config, false);
521 583
522 if (!video_config.IsValidConfig()) 584 if (!video_config.IsValidConfig())
523 continue; 585 continue;
524 video_stream = stream; 586 video_stream = stream;
587 } else if (codec_type == AVMEDIA_TYPE_SUBTITLE) {
588 if (codec_context->codec_id != AV_CODEC_ID_WEBVTT || !text_enabled_) {
589 continue;
590 }
525 } else { 591 } else {
526 continue; 592 continue;
527 } 593 }
528 594
529 streams_[i] = new FFmpegDemuxerStream(this, stream); 595 streams_[i] = new FFmpegDemuxerStream(this, stream);
530 max_duration = std::max(max_duration, streams_[i]->duration()); 596 max_duration = std::max(max_duration, streams_[i]->duration());
531 597
532 if (stream->first_dts != static_cast<int64_t>(AV_NOPTS_VALUE)) { 598 if (stream->first_dts != static_cast<int64_t>(AV_NOPTS_VALUE)) {
533 const base::TimeDelta first_dts = ConvertFromTimeBase( 599 const base::TimeDelta first_dts = ConvertFromTimeBase(
534 stream->time_base, stream->first_dts); 600 stream->time_base, stream->first_dts);
535 if (start_time_ == kNoTimestamp() || first_dts < start_time_) 601 if (start_time_ == kNoTimestamp() || first_dts < start_time_)
536 start_time_ = first_dts; 602 start_time_ = first_dts;
537 } 603 }
538 } 604 }
539 605
540 if (!audio_stream && !video_stream) { 606 if (!audio_stream && !video_stream) {
541 status_cb.Run(DEMUXER_ERROR_NO_SUPPORTED_STREAMS); 607 status_cb.Run(DEMUXER_ERROR_NO_SUPPORTED_STREAMS);
542 return; 608 return;
543 } 609 }
544 610
611 if (text_enabled_)
612 AddTextStreams();
613
545 if (format_context->duration != static_cast<int64_t>(AV_NOPTS_VALUE)) { 614 if (format_context->duration != static_cast<int64_t>(AV_NOPTS_VALUE)) {
546 // If there is a duration value in the container use that to find the 615 // If there is a duration value in the container use that to find the
547 // maximum between it and the duration from A/V streams. 616 // maximum between it and the duration from A/V streams.
548 const AVRational av_time_base = {1, AV_TIME_BASE}; 617 const AVRational av_time_base = {1, AV_TIME_BASE};
549 max_duration = 618 max_duration =
550 std::max(max_duration, 619 std::max(max_duration,
551 ConvertFromTimeBase(av_time_base, format_context->duration)); 620 ConvertFromTimeBase(av_time_base, format_context->duration));
552 } else { 621 } else {
553 // The duration is unknown, in which case this is likely a live stream. 622 // The duration is unknown, in which case this is likely a live stream.
554 max_duration = kInfiniteDuration(); 623 max_duration = kInfiniteDuration();
(...skipping 274 matching lines...) Expand 10 before | Expand all | Expand 10 after
829 } 898 }
830 for (size_t i = 0; i < buffered.size(); ++i) 899 for (size_t i = 0; i < buffered.size(); ++i)
831 host_->AddBufferedTimeRange(buffered.start(i), buffered.end(i)); 900 host_->AddBufferedTimeRange(buffered.start(i), buffered.end(i));
832 } 901 }
833 902
834 void FFmpegDemuxer::OnDataSourceError() { 903 void FFmpegDemuxer::OnDataSourceError() {
835 host_->OnDemuxerError(PIPELINE_ERROR_READ); 904 host_->OnDemuxerError(PIPELINE_ERROR_READ);
836 } 905 }
837 906
838 } // namespace media 907 } // namespace media
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698