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