| OLD | NEW |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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" |
| 11 #include "base/string_util.h" | 11 #include "base/string_util.h" |
| 12 #include "base/time.h" | 12 #include "base/time.h" |
| 13 #include "media/base/data_buffer.h" | 13 #include "media/base/data_buffer.h" |
| 14 #include "media/base/limits.h" | 14 #include "media/base/limits.h" |
| 15 #include "media/base/media_switches.h" | 15 #include "media/base/media_switches.h" |
| 16 #include "media/ffmpeg/ffmpeg_common.h" | 16 #include "media/ffmpeg/ffmpeg_common.h" |
| 17 #include "media/filters/bitstream_converter.h" | 17 #include "media/filters/bitstream_converter.h" |
| 18 #include "media/filters/ffmpeg_demuxer.h" | 18 #include "media/filters/ffmpeg_demuxer.h" |
| 19 #include "media/filters/ffmpeg_glue.h" | 19 #include "media/filters/ffmpeg_glue.h" |
| 20 #include "media/filters/ffmpeg_h264_bitstream_converter.h" | 20 #include "media/filters/ffmpeg_h264_bitstream_converter.h" |
| 21 | 21 |
| 22 namespace media { | 22 namespace media { |
| 23 | 23 |
| 24 // | 24 // |
| 25 // AVPacketBuffer | 25 // AVPacketBuffer |
| 26 // | 26 // |
| 27 class AVPacketBuffer : public Buffer { | 27 class AVPacketBuffer : public Buffer { |
| 28 public: | 28 public: |
| 29 AVPacketBuffer(AVPacket* packet, const base::TimeDelta& timestamp, | 29 AVPacketBuffer(scoped_ptr_malloc<AVPacket, ScopedPtrAVFreePacket> packet, |
| 30 const base::TimeDelta& timestamp, |
| 30 const base::TimeDelta& duration) | 31 const base::TimeDelta& duration) |
| 31 : packet_(packet) { | 32 : packet_(packet.Pass()) { |
| 32 SetTimestamp(timestamp); | 33 SetTimestamp(timestamp); |
| 33 SetDuration(duration); | 34 SetDuration(duration); |
| 34 } | 35 } |
| 35 | 36 |
| 36 virtual ~AVPacketBuffer() { | 37 virtual ~AVPacketBuffer() { |
| 37 } | 38 } |
| 38 | 39 |
| 39 // Buffer implementation. | 40 // Buffer implementation. |
| 40 virtual const uint8* GetData() const { | 41 virtual const uint8* GetData() const { |
| 41 return reinterpret_cast<const uint8*>(packet_->data); | 42 return reinterpret_cast<const uint8*>(packet_->data); |
| (...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 91 } | 92 } |
| 92 | 93 |
| 93 bool FFmpegDemuxerStream::HasPendingReads() { | 94 bool FFmpegDemuxerStream::HasPendingReads() { |
| 94 DCHECK_EQ(MessageLoop::current(), demuxer_->message_loop()); | 95 DCHECK_EQ(MessageLoop::current(), demuxer_->message_loop()); |
| 95 base::AutoLock auto_lock(lock_); | 96 base::AutoLock auto_lock(lock_); |
| 96 DCHECK(!stopped_ || read_queue_.empty()) | 97 DCHECK(!stopped_ || read_queue_.empty()) |
| 97 << "Read queue should have been emptied if demuxing stream is stopped"; | 98 << "Read queue should have been emptied if demuxing stream is stopped"; |
| 98 return !read_queue_.empty(); | 99 return !read_queue_.empty(); |
| 99 } | 100 } |
| 100 | 101 |
| 101 void FFmpegDemuxerStream::EnqueuePacket(AVPacket* packet) { | 102 void FFmpegDemuxerStream::EnqueuePacket( |
| 103 scoped_ptr_malloc<AVPacket, ScopedPtrAVFreePacket> packet) { |
| 102 DCHECK_EQ(MessageLoop::current(), demuxer_->message_loop()); | 104 DCHECK_EQ(MessageLoop::current(), demuxer_->message_loop()); |
| 103 base::TimeDelta timestamp = | 105 base::TimeDelta timestamp = |
| 104 ConvertStreamTimestamp(stream_->time_base, packet->pts); | 106 ConvertStreamTimestamp(stream_->time_base, packet->pts); |
| 105 base::TimeDelta duration = | 107 base::TimeDelta duration = |
| 106 ConvertStreamTimestamp(stream_->time_base, packet->duration); | 108 ConvertStreamTimestamp(stream_->time_base, packet->duration); |
| 107 | 109 |
| 108 base::AutoLock auto_lock(lock_); | 110 base::AutoLock auto_lock(lock_); |
| 109 if (stopped_) { | 111 if (stopped_) { |
| 110 NOTREACHED() << "Attempted to enqueue packet on a stopped stream"; | 112 NOTREACHED() << "Attempted to enqueue packet on a stopped stream"; |
| 111 return; | 113 return; |
| 112 } | 114 } |
| 113 | 115 |
| 114 // Convert if the packet if there is bitstream filter. | 116 // Convert if the packet if there is bitstream filter. |
| 115 if (packet->data && bitstream_converter_.get() && | 117 if (packet->data && bitstream_converter_.get() && |
| 116 !bitstream_converter_->ConvertPacket(packet)) { | 118 !bitstream_converter_->ConvertPacket(packet.get())) { |
| 117 LOG(ERROR) << "Format converstion failed."; | 119 LOG(ERROR) << "Format converstion failed."; |
| 118 } | 120 } |
| 119 | 121 |
| 120 // Enqueue the callback and attempt to satisfy a read immediately. | 122 // Enqueue the callback and attempt to satisfy a read immediately. |
| 121 scoped_refptr<Buffer> buffer( | 123 scoped_refptr<Buffer> buffer( |
| 122 new AVPacketBuffer(packet, timestamp, duration)); | 124 new AVPacketBuffer(packet.Pass(), timestamp, duration)); |
| 123 if (!buffer) { | 125 if (!buffer) { |
| 124 NOTREACHED() << "Unable to allocate AVPacketBuffer"; | 126 NOTREACHED() << "Unable to allocate AVPacketBuffer"; |
| 125 return; | 127 return; |
| 126 } | 128 } |
| 127 buffer_queue_.push_back(buffer); | 129 buffer_queue_.push_back(buffer); |
| 128 FulfillPendingRead(); | 130 FulfillPendingRead(); |
| 129 return; | 131 return; |
| 130 } | 132 } |
| 131 | 133 |
| 132 void FFmpegDemuxerStream::FlushBuffers() { | 134 void FFmpegDemuxerStream::FlushBuffers() { |
| (...skipping 531 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 664 FFmpegDemuxerStream* demuxer_stream = streams_[packet->stream_index]; | 666 FFmpegDemuxerStream* demuxer_stream = streams_[packet->stream_index]; |
| 665 | 667 |
| 666 // If a packet is returned by FFmpeg's av_parser_parse2() | 668 // If a packet is returned by FFmpeg's av_parser_parse2() |
| 667 // the packet will reference an inner memory of FFmpeg. | 669 // the packet will reference an inner memory of FFmpeg. |
| 668 // In this case, the packet's "destruct" member is NULL, | 670 // In this case, the packet's "destruct" member is NULL, |
| 669 // and it MUST be duplicated. This fixes issue with MP3 and possibly | 671 // and it MUST be duplicated. This fixes issue with MP3 and possibly |
| 670 // other codecs. It is safe to call this function even if the packet does | 672 // other codecs. It is safe to call this function even if the packet does |
| 671 // not refer to inner memory from FFmpeg. | 673 // not refer to inner memory from FFmpeg. |
| 672 av_dup_packet(packet.get()); | 674 av_dup_packet(packet.get()); |
| 673 | 675 |
| 674 // The stream takes ownership of the AVPacket. | 676 demuxer_stream->EnqueuePacket(packet.Pass()); |
| 675 demuxer_stream->EnqueuePacket(packet.release()); | |
| 676 } | 677 } |
| 677 | 678 |
| 678 // Create a loop by posting another task. This allows seek and message loop | 679 // Create a loop by posting another task. This allows seek and message loop |
| 679 // quit tasks to get processed. | 680 // quit tasks to get processed. |
| 680 if (StreamsHavePendingReads()) { | 681 if (StreamsHavePendingReads()) { |
| 681 PostDemuxTask(); | 682 PostDemuxTask(); |
| 682 } | 683 } |
| 683 } | 684 } |
| 684 | 685 |
| 685 void FFmpegDemuxer::StopTask(const base::Closure& callback) { | 686 void FFmpegDemuxer::StopTask(const base::Closure& callback) { |
| (...skipping 30 matching lines...) Expand all Loading... |
| 716 } | 717 } |
| 717 return false; | 718 return false; |
| 718 } | 719 } |
| 719 | 720 |
| 720 void FFmpegDemuxer::StreamHasEnded() { | 721 void FFmpegDemuxer::StreamHasEnded() { |
| 721 DCHECK_EQ(MessageLoop::current(), message_loop_); | 722 DCHECK_EQ(MessageLoop::current(), message_loop_); |
| 722 StreamVector::iterator iter; | 723 StreamVector::iterator iter; |
| 723 for (iter = streams_.begin(); iter != streams_.end(); ++iter) { | 724 for (iter = streams_.begin(); iter != streams_.end(); ++iter) { |
| 724 if (!*iter) | 725 if (!*iter) |
| 725 continue; | 726 continue; |
| 726 AVPacket* packet = new AVPacket(); | 727 scoped_ptr_malloc<AVPacket, ScopedPtrAVFreePacket> packet(new AVPacket()); |
| 727 memset(packet, 0, sizeof(*packet)); | 728 memset(packet.get(), 0, sizeof(*packet.get())); |
| 728 (*iter)->EnqueuePacket(packet); | 729 (*iter)->EnqueuePacket(packet.Pass()); |
| 729 } | 730 } |
| 730 } | 731 } |
| 731 | 732 |
| 732 void FFmpegDemuxer::OnReadCompleted(size_t size) { | 733 void FFmpegDemuxer::OnReadCompleted(size_t size) { |
| 733 SignalReadCompleted(size); | 734 SignalReadCompleted(size); |
| 734 } | 735 } |
| 735 | 736 |
| 736 size_t FFmpegDemuxer::WaitForRead() { | 737 size_t FFmpegDemuxer::WaitForRead() { |
| 737 read_event_.Wait(); | 738 read_event_.Wait(); |
| 738 return last_read_bytes_; | 739 return last_read_bytes_; |
| 739 } | 740 } |
| 740 | 741 |
| 741 void FFmpegDemuxer::SignalReadCompleted(size_t size) { | 742 void FFmpegDemuxer::SignalReadCompleted(size_t size) { |
| 742 last_read_bytes_ = size; | 743 last_read_bytes_ = size; |
| 743 read_event_.Signal(); | 744 read_event_.Signal(); |
| 744 } | 745 } |
| 745 | 746 |
| 746 } // namespace media | 747 } // namespace media |
| OLD | NEW |