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

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

Issue 9860027: Remove DemuxerFactory and URL parameter from Pipeline. (Closed) Base URL: svn://chrome-svn/chrome/trunk/src
Patch Set: fix prerender Created 8 years, 8 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 | Annotate | Revision Log
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 "base/bind.h" 5 #include "base/bind.h"
6 #include "base/callback.h" 6 #include "base/callback.h"
7 #include "base/command_line.h" 7 #include "base/command_line.h"
8 #include "base/memory/scoped_ptr.h" 8 #include "base/memory/scoped_ptr.h"
9 #include "base/message_loop.h" 9 #include "base/message_loop.h"
10 #include "base/stl_util.h" 10 #include "base/stl_util.h"
(...skipping 258 matching lines...) Expand 10 before | Expand all | Expand 10 after
269 const AVRational& time_base, int64 timestamp) { 269 const AVRational& time_base, int64 timestamp) {
270 if (timestamp == static_cast<int64>(AV_NOPTS_VALUE)) 270 if (timestamp == static_cast<int64>(AV_NOPTS_VALUE))
271 return kNoTimestamp(); 271 return kNoTimestamp();
272 272
273 return ConvertFromTimeBase(time_base, timestamp); 273 return ConvertFromTimeBase(time_base, timestamp);
274 } 274 }
275 275
276 // 276 //
277 // FFmpegDemuxer 277 // FFmpegDemuxer
278 // 278 //
279 FFmpegDemuxer::FFmpegDemuxer(MessageLoop* message_loop, bool local_source) 279 FFmpegDemuxer::FFmpegDemuxer(
280 MessageLoop* message_loop,
281 const scoped_refptr<DataSource>& data_source,
282 bool local_source)
280 : message_loop_(message_loop), 283 : message_loop_(message_loop),
281 local_source_(local_source), 284 local_source_(local_source),
282 format_context_(NULL), 285 format_context_(NULL),
286 data_source_(data_source),
283 read_event_(false, false), 287 read_event_(false, false),
284 read_has_failed_(false), 288 read_has_failed_(false),
285 last_read_bytes_(0), 289 last_read_bytes_(0),
286 read_position_(0), 290 read_position_(0),
287 max_duration_(base::TimeDelta::FromMicroseconds(-1)), 291 bitrate_(0),
288 deferred_status_(PIPELINE_OK),
289 first_seek_hack_(true), 292 first_seek_hack_(true),
290 start_time_(kNoTimestamp()), 293 start_time_(kNoTimestamp()),
291 audio_disabled_(false) { 294 audio_disabled_(false) {
292 DCHECK(message_loop_); 295 DCHECK(message_loop_);
296 DCHECK(data_source_);
293 } 297 }
294 298
295 FFmpegDemuxer::~FFmpegDemuxer() { 299 FFmpegDemuxer::~FFmpegDemuxer() {
296 // In this destructor, we clean up resources held by FFmpeg. It is ugly to 300 // In this destructor, we clean up resources held by FFmpeg. It is ugly to
297 // close the codec contexts here because the corresponding codecs are opened 301 // close the codec contexts here because the corresponding codecs are opened
298 // in the decoder filters. By reaching this point, all filters should have 302 // in the decoder filters. By reaching this point, all filters should have
299 // stopped, so this is the only safe place to do the global clean up. 303 // stopped, so this is the only safe place to do the global clean up.
300 // TODO(hclam): close the codecs in the corresponding decoders. 304 // TODO(hclam): close the codecs in the corresponding decoders.
301 if (!format_context_) 305 if (!format_context_)
302 return; 306 return;
(...skipping 26 matching lines...) Expand all
329 data_source_->SetPlaybackRate(playback_rate); 333 data_source_->SetPlaybackRate(playback_rate);
330 } 334 }
331 335
332 void FFmpegDemuxer::OnAudioRendererDisabled() { 336 void FFmpegDemuxer::OnAudioRendererDisabled() {
333 message_loop_->PostTask(FROM_HERE, base::Bind( 337 message_loop_->PostTask(FROM_HERE, base::Bind(
334 &FFmpegDemuxer::DisableAudioStreamTask, this)); 338 &FFmpegDemuxer::DisableAudioStreamTask, this));
335 } 339 }
336 340
337 void FFmpegDemuxer::set_host(DemuxerHost* demuxer_host) { 341 void FFmpegDemuxer::set_host(DemuxerHost* demuxer_host) {
338 Demuxer::set_host(demuxer_host); 342 Demuxer::set_host(demuxer_host);
339 if (data_source_) 343 data_source_->set_host(demuxer_host);
340 data_source_->set_host(demuxer_host);
341 if (max_duration_.InMicroseconds() >= 0)
342 host()->SetDuration(max_duration_);
343 if (read_position_ > 0)
344 host()->SetCurrentReadPosition(read_position_);
345 if (deferred_status_ != PIPELINE_OK)
346 host()->OnDemuxerError(deferred_status_);
347 } 344 }
348 345
349 void FFmpegDemuxer::Initialize(DataSource* data_source, 346 void FFmpegDemuxer::Initialize(const PipelineStatusCB& status_cb) {
350 const PipelineStatusCB& status_cb) { 347 message_loop_->PostTask(FROM_HERE, base::Bind(
351 message_loop_->PostTask( 348 &FFmpegDemuxer::InitializeTask, this, status_cb));
352 FROM_HERE,
353 base::Bind(&FFmpegDemuxer::InitializeTask, this,
354 make_scoped_refptr(data_source), status_cb));
355 } 349 }
356 350
357 scoped_refptr<DemuxerStream> FFmpegDemuxer::GetStream( 351 scoped_refptr<DemuxerStream> FFmpegDemuxer::GetStream(
358 DemuxerStream::Type type) { 352 DemuxerStream::Type type) {
359 StreamVector::iterator iter; 353 StreamVector::iterator iter;
360 for (iter = streams_.begin(); iter != streams_.end(); ++iter) { 354 for (iter = streams_.begin(); iter != streams_.end(); ++iter) {
361 if (*iter && (*iter)->type() == type) { 355 if (*iter && (*iter)->type() == type) {
362 return *iter; 356 return *iter;
363 } 357 }
364 } 358 }
(...skipping 20 matching lines...) Expand all
385 379
386 // Asynchronous read from data source. 380 // Asynchronous read from data source.
387 data_source_->Read(read_position_, size, data, base::Bind( 381 data_source_->Read(read_position_, size, data, base::Bind(
388 &FFmpegDemuxer::SignalReadCompleted, this)); 382 &FFmpegDemuxer::SignalReadCompleted, this));
389 383
390 // TODO(hclam): The method is called on the demuxer thread and this method 384 // TODO(hclam): The method is called on the demuxer thread and this method
391 // call will block the thread. We need to implemented an additional thread to 385 // call will block the thread. We need to implemented an additional thread to
392 // let FFmpeg demuxer methods to run on. 386 // let FFmpeg demuxer methods to run on.
393 int last_read_bytes = WaitForRead(); 387 int last_read_bytes = WaitForRead();
394 if (last_read_bytes == DataSource::kReadError) { 388 if (last_read_bytes == DataSource::kReadError) {
395 if (host()) 389 host()->OnDemuxerError(PIPELINE_ERROR_READ);
396 host()->OnDemuxerError(PIPELINE_ERROR_READ);
397 else
398 deferred_status_ = PIPELINE_ERROR_READ;
399 390
400 // Returns with a negative number to signal an error to FFmpeg. 391 // Returns with a negative number to signal an error to FFmpeg.
401 read_has_failed_ = true; 392 read_has_failed_ = true;
402 return AVERROR(EIO); 393 return AVERROR(EIO);
403 } 394 }
404 read_position_ += last_read_bytes; 395 read_position_ += last_read_bytes;
405 396 host()->SetCurrentReadPosition(read_position_);
406 if (host())
407 host()->SetCurrentReadPosition(read_position_);
408 397
409 return last_read_bytes; 398 return last_read_bytes;
410 } 399 }
411 400
412 bool FFmpegDemuxer::GetPosition(int64* position_out) { 401 bool FFmpegDemuxer::GetPosition(int64* position_out) {
413 *position_out = read_position_; 402 *position_out = read_position_;
414 return true; 403 return true;
415 } 404 }
416 405
417 bool FFmpegDemuxer::SetPosition(int64 position) { 406 bool FFmpegDemuxer::SetPosition(int64 position) {
(...skipping 18 matching lines...) Expand all
436 bool FFmpegDemuxer::IsStreaming() { 425 bool FFmpegDemuxer::IsStreaming() {
437 DCHECK(data_source_); 426 DCHECK(data_source_);
438 427
439 return data_source_->IsStreaming(); 428 return data_source_->IsStreaming();
440 } 429 }
441 430
442 MessageLoop* FFmpegDemuxer::message_loop() { 431 MessageLoop* FFmpegDemuxer::message_loop() {
443 return message_loop_; 432 return message_loop_;
444 } 433 }
445 434
446 void FFmpegDemuxer::InitializeTask(DataSource* data_source, 435 // Helper for calculating the bitrate of the media based on information stored
447 const PipelineStatusCB& status_cb) { 436 // in |format_context| or failing that the size and duration of the media.
437 //
438 // Returns 0 if a bitrate could not be determined.
439 static int CalculateBitrate(
440 AVFormatContext* format_context,
441 const base::TimeDelta& duration,
442 int64 filesize_in_bytes) {
443 // If there is a bitrate set on the container, use it.
444 if (format_context->bit_rate > 0)
445 return format_context->bit_rate;
446
447 // Then try to sum the bitrates individually per stream.
448 int bitrate = 0;
449 for (size_t i = 0; i < format_context->nb_streams; ++i) {
450 AVCodecContext* codec_context = format_context->streams[i]->codec;
451 bitrate += codec_context->bit_rate;
452 }
453 if (bitrate > 0)
454 return bitrate;
455
456 // See if we can approximate the bitrate as long as we have a filesize and
457 // valid duration.
458 if (duration.InMicroseconds() <= 0 ||
459 duration == kInfiniteDuration() ||
460 filesize_in_bytes == 0) {
461 return 0;
462 }
463
464 // Do math in floating point as we'd overflow an int64 if the filesize was
465 // larger than ~1073GB.
466 double bytes = filesize_in_bytes;
467 double duration_us = duration.InMicroseconds();
468 return bytes * 8000000.0 / duration_us;
469 }
470
471 void FFmpegDemuxer::InitializeTask(const PipelineStatusCB& status_cb) {
448 DCHECK_EQ(MessageLoop::current(), message_loop_); 472 DCHECK_EQ(MessageLoop::current(), message_loop_);
449 473
450 data_source_ = data_source;
451 if (host())
452 data_source_->set_host(host());
453
454 // Add ourself to Protocol list and get our unique key. 474 // Add ourself to Protocol list and get our unique key.
455 std::string key = FFmpegGlue::GetInstance()->AddProtocol(this); 475 std::string key = FFmpegGlue::GetInstance()->AddProtocol(this);
456 476
457 // Open FFmpeg AVFormatContext. 477 // Open FFmpeg AVFormatContext.
458 DCHECK(!format_context_); 478 DCHECK(!format_context_);
459 AVFormatContext* context = NULL; 479 AVFormatContext* context = NULL;
460 int result = avformat_open_input(&context, key.c_str(), NULL, NULL); 480 int result = avformat_open_input(&context, key.c_str(), NULL, NULL);
461 481
462 // Remove ourself from protocol list. 482 // Remove ourself from protocol list.
463 FFmpegGlue::GetInstance()->RemoveProtocol(this); 483 FFmpegGlue::GetInstance()->RemoveProtocol(this);
(...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after
531 max_duration = kInfiniteDuration(); 551 max_duration = kInfiniteDuration();
532 } 552 }
533 553
534 // Some demuxers, like WAV, do not put timestamps on their frames. We 554 // Some demuxers, like WAV, do not put timestamps on their frames. We
535 // assume the the start time is 0. 555 // assume the the start time is 0.
536 if (start_time_ == kNoTimestamp()) 556 if (start_time_ == kNoTimestamp())
537 start_time_ = base::TimeDelta(); 557 start_time_ = base::TimeDelta();
538 558
539 // Good to go: set the duration and bitrate and notify we're done 559 // Good to go: set the duration and bitrate and notify we're done
540 // initializing. 560 // initializing.
541 if (host()) 561 host()->SetDuration(max_duration);
542 host()->SetDuration(max_duration);
543 max_duration_ = max_duration;
544 562
545 int bitrate = GetBitrate(); 563 int64 filesize_in_bytes = 0;
546 if (bitrate > 0) 564 GetSize(&filesize_in_bytes);
547 data_source_->SetBitrate(bitrate); 565 bitrate_ = CalculateBitrate(format_context_, max_duration, filesize_in_bytes);
566 if (bitrate_ > 0)
567 data_source_->SetBitrate(bitrate_);
548 568
549 status_cb.Run(PIPELINE_OK); 569 status_cb.Run(PIPELINE_OK);
550 } 570 }
551 571
572
552 int FFmpegDemuxer::GetBitrate() { 573 int FFmpegDemuxer::GetBitrate() {
553 DCHECK(format_context_); 574 DCHECK(format_context_) << "Initialize() has not been called";
554 575 return bitrate_;
555 // If there is a bitrate set on the container, use it.
556 if (format_context_->bit_rate > 0)
557 return format_context_->bit_rate;
558
559 // Then try to sum the bitrates individually per stream.
560 int bitrate = 0;
561 for (size_t i = 0; i < format_context_->nb_streams; ++i) {
562 AVCodecContext* codec_context = format_context_->streams[i]->codec;
563 bitrate += codec_context->bit_rate;
564 }
565 if (bitrate > 0)
566 return bitrate;
567
568 // See if we can approximate the bitrate as long as we have a filesize and
569 // valid duration.
570 int64 filesize_in_bytes;
571 if (max_duration_.InMicroseconds() <= 0 ||
572 max_duration_ == kInfiniteDuration() ||
573 !GetSize(&filesize_in_bytes)) {
574 return 0;
575 }
576
577 // Do math in floating point as we'd overflow an int64 if the filesize was
578 // larger than ~1073GB.
579 double bytes = filesize_in_bytes;
580 double duration = max_duration_.InMicroseconds();
581 return bytes * 8000000.0 / duration;
582 } 576 }
583 577
584 bool FFmpegDemuxer::IsLocalSource() { 578 bool FFmpegDemuxer::IsLocalSource() {
585 return local_source_; 579 return local_source_;
586 } 580 }
587 581
588 bool FFmpegDemuxer::IsSeekable() { 582 bool FFmpegDemuxer::IsSeekable() {
589 return !IsStreaming(); 583 return !IsStreaming();
590 } 584 }
591 585
(...skipping 140 matching lines...) Expand 10 before | Expand all | Expand 10 after
732 read_event_.Wait(); 726 read_event_.Wait();
733 return last_read_bytes_; 727 return last_read_bytes_;
734 } 728 }
735 729
736 void FFmpegDemuxer::SignalReadCompleted(int size) { 730 void FFmpegDemuxer::SignalReadCompleted(int size) {
737 last_read_bytes_ = size; 731 last_read_bytes_ = size;
738 read_event_.Signal(); 732 read_event_.Signal();
739 } 733 }
740 734
741 } // namespace media 735 } // namespace media
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698