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