| 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 // Implements the Demuxer interface using FFmpeg's libavformat. At this time | 5 // Implements the Demuxer interface using FFmpeg's libavformat. At this time |
| 6 // will support demuxing any audio/video format thrown at it. The streams | 6 // will support demuxing any audio/video format thrown at it. The streams |
| 7 // output mime types audio/x-ffmpeg and video/x-ffmpeg and include an integer | 7 // output mime types audio/x-ffmpeg and video/x-ffmpeg and include an integer |
| 8 // key FFmpegCodecID which contains the CodecID enumeration value. The CodecIDs | 8 // key FFmpegCodecID which contains the CodecID enumeration value. The CodecIDs |
| 9 // can be used to create and initialize the corresponding FFmpeg decoder. | 9 // can be used to create and initialize the corresponding FFmpeg decoder. |
| 10 // | 10 // |
| (...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 49 | 49 |
| 50 typedef scoped_ptr_malloc<AVPacket, ScopedPtrAVFreePacket> ScopedAVPacket; | 50 typedef scoped_ptr_malloc<AVPacket, ScopedPtrAVFreePacket> ScopedAVPacket; |
| 51 | 51 |
| 52 class FFmpegDemuxerStream : public DemuxerStream { | 52 class FFmpegDemuxerStream : public DemuxerStream { |
| 53 public: | 53 public: |
| 54 // Keeps a copy of |demuxer| and initializes itself using information | 54 // Keeps a copy of |demuxer| and initializes itself using information |
| 55 // inside |stream|. Both parameters must outlive |this|. | 55 // inside |stream|. Both parameters must outlive |this|. |
| 56 FFmpegDemuxerStream(FFmpegDemuxer* demuxer, AVStream* stream); | 56 FFmpegDemuxerStream(FFmpegDemuxer* demuxer, AVStream* stream); |
| 57 | 57 |
| 58 // Returns true is this stream has pending reads, false otherwise. | 58 // Returns true is this stream has pending reads, false otherwise. |
| 59 // | |
| 60 // Safe to call on any thread. | |
| 61 bool HasPendingReads(); | 59 bool HasPendingReads(); |
| 62 | 60 |
| 63 // Enqueues the given AVPacket. If |packet| is NULL an end of stream packet | 61 // Enqueues the given AVPacket. If |packet| is NULL an end of stream packet |
| 64 // is enqueued. | 62 // is enqueued. |
| 65 void EnqueuePacket(ScopedAVPacket packet); | 63 void EnqueuePacket(ScopedAVPacket packet); |
| 66 | 64 |
| 67 // Signals to empty the buffer queue and mark next packet as discontinuous. | 65 // Signals to empty the buffer queue and mark next packet as discontinuous. |
| 68 void FlushBuffers(); | 66 void FlushBuffers(); |
| 69 | 67 |
| 70 // Empties the queues and ignores any additional calls to Read(). | 68 // Empties the queues and ignores any additional calls to Read(). |
| 71 void Stop(); | 69 void Stop(); |
| 72 | 70 |
| 73 // Returns the duration of this stream. | 71 // Returns the duration of this stream. |
| 74 base::TimeDelta duration(); | 72 base::TimeDelta duration(); |
| 75 | 73 |
| 76 // DemuxerStream implementation. | 74 // DemuxerStream implementation. |
| 77 virtual Type type() OVERRIDE; | 75 virtual Type type() OVERRIDE; |
| 78 | |
| 79 // If |buffer_queue_| is not empty will execute on caller's thread, otherwise | |
| 80 // will post ReadTask to execute on demuxer's thread. Read will acquire | |
| 81 // |lock_| for the life of the function so that means |read_cb| must | |
| 82 // not make calls into FFmpegDemuxerStream directly or that may cause a | |
| 83 // deadlock. |read_cb| should execute as quickly as possible because | |
| 84 // |lock_| is held throughout the life of the callback. | |
| 85 virtual void Read(const ReadCB& read_cb) OVERRIDE; | 76 virtual void Read(const ReadCB& read_cb) OVERRIDE; |
| 86 virtual void EnableBitstreamConverter() OVERRIDE; | 77 virtual void EnableBitstreamConverter() OVERRIDE; |
| 87 virtual const AudioDecoderConfig& audio_decoder_config() OVERRIDE; | 78 virtual const AudioDecoderConfig& audio_decoder_config() OVERRIDE; |
| 88 virtual const VideoDecoderConfig& video_decoder_config() OVERRIDE; | 79 virtual const VideoDecoderConfig& video_decoder_config() OVERRIDE; |
| 89 | 80 |
| 90 // Returns the range of buffered data in this stream. | 81 // Returns the range of buffered data in this stream. |
| 91 Ranges<base::TimeDelta> GetBufferedRanges() const; | 82 Ranges<base::TimeDelta> GetBufferedRanges() const; |
| 92 | 83 |
| 93 // Returns elapsed time based on the already queued packets. | 84 // Returns elapsed time based on the already queued packets. |
| 94 // Used to determine stream duration when it's not known ahead of time. | 85 // Used to determine stream duration when it's not known ahead of time. |
| 95 base::TimeDelta GetElapsedTime() const; | 86 base::TimeDelta GetElapsedTime() const; |
| 96 | 87 |
| 97 protected: | 88 protected: |
| 98 virtual ~FFmpegDemuxerStream(); | 89 virtual ~FFmpegDemuxerStream(); |
| 99 | 90 |
| 100 private: | 91 private: |
| 101 friend class FFmpegDemuxerTest; | 92 friend class FFmpegDemuxerTest; |
| 102 | 93 |
| 103 // Carries out enqueuing a pending read on the demuxer thread. | 94 // Runs callbacks in |read_queue_| for each available |buffer_queue_|, calling |
| 104 void ReadTask(const ReadCB& read_cb); | 95 // NotifyHasPendingRead() if there are still pending items in |read_queue_|. |
| 105 | 96 void SatisfyPendingReads(); |
| 106 // Attempts to fulfill a single pending read by dequeueing a buffer and read | |
| 107 // callback pair and executing the callback. The calling function must | |
| 108 // acquire |lock_| before calling this function. | |
| 109 void FulfillPendingRead(); | |
| 110 | 97 |
| 111 // Converts an FFmpeg stream timestamp into a base::TimeDelta. | 98 // Converts an FFmpeg stream timestamp into a base::TimeDelta. |
| 112 static base::TimeDelta ConvertStreamTimestamp(const AVRational& time_base, | 99 static base::TimeDelta ConvertStreamTimestamp(const AVRational& time_base, |
| 113 int64 timestamp); | 100 int64 timestamp); |
| 114 | 101 |
| 115 FFmpegDemuxer* demuxer_; | 102 FFmpegDemuxer* demuxer_; |
| 103 scoped_refptr<base::MessageLoopProxy> message_loop_; |
| 116 AVStream* stream_; | 104 AVStream* stream_; |
| 117 AudioDecoderConfig audio_config_; | 105 AudioDecoderConfig audio_config_; |
| 118 VideoDecoderConfig video_config_; | 106 VideoDecoderConfig video_config_; |
| 119 Type type_; | 107 Type type_; |
| 120 base::TimeDelta duration_; | 108 base::TimeDelta duration_; |
| 121 bool stopped_; | 109 bool stopped_; |
| 122 base::TimeDelta last_packet_timestamp_; | 110 base::TimeDelta last_packet_timestamp_; |
| 123 Ranges<base::TimeDelta> buffered_ranges_; | 111 Ranges<base::TimeDelta> buffered_ranges_; |
| 124 | 112 |
| 125 typedef std::deque<scoped_refptr<DecoderBuffer> > BufferQueue; | 113 typedef std::deque<scoped_refptr<DecoderBuffer> > BufferQueue; |
| 126 BufferQueue buffer_queue_; | 114 BufferQueue buffer_queue_; |
| 127 | 115 |
| 128 typedef std::deque<ReadCB> ReadQueue; | 116 typedef std::deque<ReadCB> ReadQueue; |
| 129 ReadQueue read_queue_; | 117 ReadQueue read_queue_; |
| 130 | 118 |
| 131 scoped_ptr<FFmpegH264ToAnnexBBitstreamConverter> bitstream_converter_; | 119 scoped_ptr<FFmpegH264ToAnnexBBitstreamConverter> bitstream_converter_; |
| 132 bool bitstream_converter_enabled_; | 120 bool bitstream_converter_enabled_; |
| 133 | 121 |
| 134 // Used to synchronize access to |buffer_queue_|, |read_queue_|, and | |
| 135 // |stopped_|. This is so other threads can get access to buffers that have | |
| 136 // already been demuxed without having the demuxer thread sending the | |
| 137 // buffers. |lock_| must be acquired before any access to |buffer_queue_|, | |
| 138 // |read_queue_|, or |stopped_|. | |
| 139 mutable base::Lock lock_; | |
| 140 | |
| 141 DISALLOW_COPY_AND_ASSIGN(FFmpegDemuxerStream); | 122 DISALLOW_COPY_AND_ASSIGN(FFmpegDemuxerStream); |
| 142 }; | 123 }; |
| 143 | 124 |
| 144 class MEDIA_EXPORT FFmpegDemuxer : public Demuxer { | 125 class MEDIA_EXPORT FFmpegDemuxer : public Demuxer { |
| 145 public: | 126 public: |
| 146 FFmpegDemuxer(const scoped_refptr<base::MessageLoopProxy>& message_loop, | 127 FFmpegDemuxer(const scoped_refptr<base::MessageLoopProxy>& message_loop, |
| 147 const scoped_refptr<DataSource>& data_source); | 128 const scoped_refptr<DataSource>& data_source); |
| 148 | 129 |
| 149 // Posts a task to perform additional demuxing. | |
| 150 virtual void PostDemuxTask(); | |
| 151 | |
| 152 // Demuxer implementation. | 130 // Demuxer implementation. |
| 153 virtual void Initialize(DemuxerHost* host, | 131 virtual void Initialize(DemuxerHost* host, |
| 154 const PipelineStatusCB& status_cb) OVERRIDE; | 132 const PipelineStatusCB& status_cb) OVERRIDE; |
| 155 virtual void Stop(const base::Closure& callback) OVERRIDE; | 133 virtual void Stop(const base::Closure& callback) OVERRIDE; |
| 156 virtual void Seek(base::TimeDelta time, const PipelineStatusCB& cb) OVERRIDE; | 134 virtual void Seek(base::TimeDelta time, const PipelineStatusCB& cb) OVERRIDE; |
| 157 virtual void OnAudioRendererDisabled() OVERRIDE; | 135 virtual void OnAudioRendererDisabled() OVERRIDE; |
| 158 virtual void SetPlaybackRate(float playback_rate) OVERRIDE; | 136 virtual void SetPlaybackRate(float playback_rate) OVERRIDE; |
| 159 virtual scoped_refptr<DemuxerStream> GetStream( | 137 virtual scoped_refptr<DemuxerStream> GetStream( |
| 160 DemuxerStream::Type type) OVERRIDE; | 138 DemuxerStream::Type type) OVERRIDE; |
| 161 virtual base::TimeDelta GetStartTime() const OVERRIDE; | 139 virtual base::TimeDelta GetStartTime() const OVERRIDE; |
| 162 | 140 |
| 163 // Provide access to FFmpegDemuxerStream. | 141 // Allow FFmpegDemuxerStream to notify us when it requires more data or has |
| 164 scoped_refptr<base::MessageLoopProxy> message_loop(); | 142 // updated information about what buffered data is available. |
| 165 | 143 void NotifyHasPendingRead(); |
| 166 // Allow FFmpegDemuxerStream to notify us when there is updated information | |
| 167 // about what buffered data is available. | |
| 168 void NotifyBufferingChanged(); | 144 void NotifyBufferingChanged(); |
| 169 | 145 |
| 170 private: | 146 private: |
| 171 // To allow tests access to privates. | 147 // To allow tests access to privates. |
| 172 friend class FFmpegDemuxerTest; | 148 friend class FFmpegDemuxerTest; |
| 173 | 149 |
| 174 virtual ~FFmpegDemuxer(); | 150 virtual ~FFmpegDemuxer(); |
| 175 | 151 |
| 176 // Carries out initialization on the demuxer thread. | 152 // Carries out initialization on the demuxer thread. |
| 177 void InitializeTask(DemuxerHost* host, const PipelineStatusCB& status_cb); | 153 void InitializeTask(DemuxerHost* host, const PipelineStatusCB& status_cb); |
| (...skipping 77 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 255 // FFmpegURLProtocol implementation and corresponding glue bits. | 231 // FFmpegURLProtocol implementation and corresponding glue bits. |
| 256 BlockingUrlProtocol url_protocol_; | 232 BlockingUrlProtocol url_protocol_; |
| 257 scoped_ptr<FFmpegGlue> glue_; | 233 scoped_ptr<FFmpegGlue> glue_; |
| 258 | 234 |
| 259 DISALLOW_COPY_AND_ASSIGN(FFmpegDemuxer); | 235 DISALLOW_COPY_AND_ASSIGN(FFmpegDemuxer); |
| 260 }; | 236 }; |
| 261 | 237 |
| 262 } // namespace media | 238 } // namespace media |
| 263 | 239 |
| 264 #endif // MEDIA_FILTERS_FFMPEG_DEMUXER_H_ | 240 #endif // MEDIA_FILTERS_FFMPEG_DEMUXER_H_ |
| OLD | NEW |