| 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 // |
| 11 // FFmpegDemuxer sets the duration of pipeline during initialization by using | 11 // FFmpegDemuxer sets the duration of pipeline during initialization by using |
| 12 // the duration of the longest audio/video stream. | 12 // the duration of the longest audio/video stream. |
| 13 // | 13 // |
| 14 // NOTE: since FFmpegDemuxer reads packets sequentially without seeking, media | 14 // NOTE: since FFmpegDemuxer reads packets sequentially without seeking, media |
| 15 // files with very large drift between audio/video streams may result in | 15 // files with very large drift between audio/video streams may result in |
| 16 // excessive memory consumption. | 16 // excessive memory consumption. |
| 17 // | 17 // |
| 18 // When stopped, FFmpegDemuxer and FFmpegDemuxerStream release all callbacks | 18 // When stopped, FFmpegDemuxer and FFmpegDemuxerStream release all callbacks |
| 19 // and buffered packets. Reads from a stopped FFmpegDemuxerStream will not be | 19 // and buffered packets. Reads from a stopped FFmpegDemuxerStream will not be |
| 20 // replied to. | 20 // replied to. |
| 21 | 21 |
| 22 #ifndef MEDIA_FILTERS_FFMPEG_DEMUXER_H_ | 22 #ifndef MEDIA_FILTERS_FFMPEG_DEMUXER_H_ |
| 23 #define MEDIA_FILTERS_FFMPEG_DEMUXER_H_ | 23 #define MEDIA_FILTERS_FFMPEG_DEMUXER_H_ |
| 24 | 24 |
| 25 #include <deque> | 25 #include <deque> |
| 26 #include <vector> | 26 #include <vector> |
| 27 | 27 |
| 28 #include "base/callback.h" | 28 #include "base/callback.h" |
| 29 #include "base/gtest_prod_util.h" | 29 #include "base/gtest_prod_util.h" |
| 30 #include "base/synchronization/waitable_event.h" | |
| 31 #include "media/base/audio_decoder_config.h" | 30 #include "media/base/audio_decoder_config.h" |
| 32 #include "media/base/decoder_buffer.h" | 31 #include "media/base/decoder_buffer.h" |
| 33 #include "media/base/demuxer.h" | 32 #include "media/base/demuxer.h" |
| 34 #include "media/base/pipeline.h" | 33 #include "media/base/pipeline.h" |
| 35 #include "media/base/video_decoder_config.h" | 34 #include "media/base/video_decoder_config.h" |
| 36 #include "media/filters/ffmpeg_glue.h" | 35 #include "media/filters/blocking_url_protocol.h" |
| 37 | 36 |
| 38 // FFmpeg forward declarations. | 37 // FFmpeg forward declarations. |
| 39 struct AVPacket; | 38 struct AVPacket; |
| 40 struct AVRational; | 39 struct AVRational; |
| 41 struct AVStream; | 40 struct AVStream; |
| 42 | 41 |
| 43 namespace media { | 42 namespace media { |
| 44 | 43 |
| 45 class FFmpegDemuxer; | 44 class FFmpegDemuxer; |
| 45 class FFmpegGlue; |
| 46 class FFmpegH264ToAnnexBBitstreamConverter; | 46 class FFmpegH264ToAnnexBBitstreamConverter; |
| 47 class ScopedPtrAVFreePacket; | 47 class ScopedPtrAVFreePacket; |
| 48 | 48 |
| 49 class FFmpegDemuxerStream : public DemuxerStream { | 49 class FFmpegDemuxerStream : public DemuxerStream { |
| 50 public: | 50 public: |
| 51 // Keeps a copy of |demuxer| and initializes itself using information | 51 // Keeps a copy of |demuxer| and initializes itself using information |
| 52 // inside |stream|. Both parameters must outlive |this|. | 52 // inside |stream|. Both parameters must outlive |this|. |
| 53 FFmpegDemuxerStream(FFmpegDemuxer* demuxer, AVStream* stream); | 53 FFmpegDemuxerStream(FFmpegDemuxer* demuxer, AVStream* stream); |
| 54 | 54 |
| 55 // Returns true is this stream has pending reads, false otherwise. | 55 // Returns true is this stream has pending reads, false otherwise. |
| (...skipping 75 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 131 // Used to synchronize access to |buffer_queue_|, |read_queue_|, and | 131 // Used to synchronize access to |buffer_queue_|, |read_queue_|, and |
| 132 // |stopped_|. This is so other threads can get access to buffers that have | 132 // |stopped_|. This is so other threads can get access to buffers that have |
| 133 // already been demuxed without having the demuxer thread sending the | 133 // already been demuxed without having the demuxer thread sending the |
| 134 // buffers. |lock_| must be acquired before any access to |buffer_queue_|, | 134 // buffers. |lock_| must be acquired before any access to |buffer_queue_|, |
| 135 // |read_queue_|, or |stopped_|. | 135 // |read_queue_|, or |stopped_|. |
| 136 mutable base::Lock lock_; | 136 mutable base::Lock lock_; |
| 137 | 137 |
| 138 DISALLOW_COPY_AND_ASSIGN(FFmpegDemuxerStream); | 138 DISALLOW_COPY_AND_ASSIGN(FFmpegDemuxerStream); |
| 139 }; | 139 }; |
| 140 | 140 |
| 141 class MEDIA_EXPORT FFmpegDemuxer : public Demuxer, public FFmpegURLProtocol { | 141 class MEDIA_EXPORT FFmpegDemuxer : public Demuxer { |
| 142 public: | 142 public: |
| 143 FFmpegDemuxer(const scoped_refptr<base::MessageLoopProxy>& message_loop, | 143 FFmpegDemuxer(const scoped_refptr<base::MessageLoopProxy>& message_loop, |
| 144 const scoped_refptr<DataSource>& data_source); | 144 const scoped_refptr<DataSource>& data_source); |
| 145 | 145 |
| 146 // Posts a task to perform additional demuxing. | 146 // Posts a task to perform additional demuxing. |
| 147 virtual void PostDemuxTask(); | 147 virtual void PostDemuxTask(); |
| 148 | 148 |
| 149 // Demuxer implementation. | 149 // Demuxer implementation. |
| 150 virtual void Initialize(DemuxerHost* host, | 150 virtual void Initialize(DemuxerHost* host, |
| 151 const PipelineStatusCB& status_cb) OVERRIDE; | 151 const PipelineStatusCB& status_cb) OVERRIDE; |
| 152 virtual void Stop(const base::Closure& callback) OVERRIDE; | 152 virtual void Stop(const base::Closure& callback) OVERRIDE; |
| 153 virtual void Seek(base::TimeDelta time, const PipelineStatusCB& cb) OVERRIDE; | 153 virtual void Seek(base::TimeDelta time, const PipelineStatusCB& cb) OVERRIDE; |
| 154 virtual void OnAudioRendererDisabled() OVERRIDE; | 154 virtual void OnAudioRendererDisabled() OVERRIDE; |
| 155 virtual void SetPlaybackRate(float playback_rate) OVERRIDE; | 155 virtual void SetPlaybackRate(float playback_rate) OVERRIDE; |
| 156 virtual scoped_refptr<DemuxerStream> GetStream( | 156 virtual scoped_refptr<DemuxerStream> GetStream( |
| 157 DemuxerStream::Type type) OVERRIDE; | 157 DemuxerStream::Type type) OVERRIDE; |
| 158 virtual base::TimeDelta GetStartTime() const OVERRIDE; | 158 virtual base::TimeDelta GetStartTime() const OVERRIDE; |
| 159 | 159 |
| 160 // FFmpegURLProtocol implementation. | |
| 161 virtual int Read(int size, uint8* data) OVERRIDE; | |
| 162 virtual bool GetPosition(int64* position_out) OVERRIDE; | |
| 163 virtual bool SetPosition(int64 position) OVERRIDE; | |
| 164 virtual bool GetSize(int64* size_out) OVERRIDE; | |
| 165 virtual bool IsStreaming() OVERRIDE; | |
| 166 | |
| 167 // Provide access to FFmpegDemuxerStream. | 160 // Provide access to FFmpegDemuxerStream. |
| 168 scoped_refptr<base::MessageLoopProxy> message_loop(); | 161 scoped_refptr<base::MessageLoopProxy> message_loop(); |
| 169 | 162 |
| 170 // Allow FFmpegDemuxerStream to notify us when there is updated information | 163 // Allow FFmpegDemuxerStream to notify us when there is updated information |
| 171 // about what buffered data is available. | 164 // about what buffered data is available. |
| 172 void NotifyBufferingChanged(); | 165 void NotifyBufferingChanged(); |
| 173 | 166 |
| 174 private: | 167 private: |
| 175 // To allow tests access to privates. | 168 // To allow tests access to privates. |
| 176 friend class FFmpegDemuxerTest; | 169 friend class FFmpegDemuxerTest; |
| (...skipping 20 matching lines...) Expand all Loading... |
| 197 // the tasks if there is no work to do. | 190 // the tasks if there is no work to do. |
| 198 // | 191 // |
| 199 // Must be called on the demuxer thread. | 192 // Must be called on the demuxer thread. |
| 200 bool StreamsHavePendingReads(); | 193 bool StreamsHavePendingReads(); |
| 201 | 194 |
| 202 // Signal all FFmpegDemuxerStream that the stream has ended. | 195 // Signal all FFmpegDemuxerStream that the stream has ended. |
| 203 // | 196 // |
| 204 // Must be called on the demuxer thread. | 197 // Must be called on the demuxer thread. |
| 205 void StreamHasEnded(); | 198 void StreamHasEnded(); |
| 206 | 199 |
| 207 // Wait for asynchronous read to complete and return number of bytes read. | 200 // Called by |url_protocol_| whenever |data_source_| returns a read error. |
| 208 virtual int WaitForRead(); | 201 void OnDataSourceError(); |
| 209 | |
| 210 // Signal the blocked thread that the read has completed, with |size| bytes | |
| 211 // read or kReadError in case of error. | |
| 212 virtual void SignalReadCompleted(int size); | |
| 213 | 202 |
| 214 // Returns the stream from |streams_| that matches |type| as an | 203 // Returns the stream from |streams_| that matches |type| as an |
| 215 // FFmpegDemuxerStream. | 204 // FFmpegDemuxerStream. |
| 216 scoped_refptr<FFmpegDemuxerStream> GetFFmpegStream( | 205 scoped_refptr<FFmpegDemuxerStream> GetFFmpegStream( |
| 217 DemuxerStream::Type type) const; | 206 DemuxerStream::Type type) const; |
| 218 | 207 |
| 219 DemuxerHost* host_; | 208 DemuxerHost* host_; |
| 220 | 209 |
| 221 scoped_refptr<base::MessageLoopProxy> message_loop_; | 210 scoped_refptr<base::MessageLoopProxy> message_loop_; |
| 222 | 211 |
| 223 // |streams_| mirrors the AVStream array in |format_context_|. It contains | 212 // |streams_| mirrors the AVStream array in |format_context_|. It contains |
| 224 // FFmpegDemuxerStreams encapsluating AVStream objects at the same index. | 213 // FFmpegDemuxerStreams encapsluating AVStream objects at the same index. |
| 225 // | 214 // |
| 226 // Since we only support a single audio and video stream, |streams_| will | 215 // Since we only support a single audio and video stream, |streams_| will |
| 227 // contain NULL entries for additional audio/video streams as well as for | 216 // contain NULL entries for additional audio/video streams as well as for |
| 228 // stream types that we do not currently support. | 217 // stream types that we do not currently support. |
| 229 // | 218 // |
| 230 // Once initialized, operations on FFmpegDemuxerStreams should be carried out | 219 // Once initialized, operations on FFmpegDemuxerStreams should be carried out |
| 231 // on the demuxer thread. | 220 // on the demuxer thread. |
| 232 typedef std::vector<scoped_refptr<FFmpegDemuxerStream> > StreamVector; | 221 typedef std::vector<scoped_refptr<FFmpegDemuxerStream> > StreamVector; |
| 233 StreamVector streams_; | 222 StreamVector streams_; |
| 234 | 223 |
| 235 // Reference to the data source. Asynchronous read requests are submitted to | 224 // Reference to the data source. Asynchronous read requests are submitted to |
| 236 // this object. | 225 // this object. |
| 237 scoped_refptr<DataSource> data_source_; | 226 scoped_refptr<DataSource> data_source_; |
| 238 | 227 |
| 239 // This member is used to block on read method calls from FFmpeg and wait | |
| 240 // until the asynchronous reads in the data source to complete. It is also | |
| 241 // signaled when the demuxer is being stopped. | |
| 242 base::WaitableEvent read_event_; | |
| 243 | |
| 244 // Flag to indicate if read has ever failed. Once set to true, it will | |
| 245 // never be reset. This flag is set true and accessed in Read(). | |
| 246 bool read_has_failed_; | |
| 247 | |
| 248 int last_read_bytes_; | |
| 249 int64 read_position_; | |
| 250 | |
| 251 // Derived bitrate after initialization has completed. | 228 // Derived bitrate after initialization has completed. |
| 252 int bitrate_; | 229 int bitrate_; |
| 253 | 230 |
| 254 // The first timestamp of the opened media file. This is used to set the | 231 // The first timestamp of the opened media file. This is used to set the |
| 255 // starting clock value to match the timestamps in the media file. Default | 232 // starting clock value to match the timestamps in the media file. Default |
| 256 // is 0. | 233 // is 0. |
| 257 base::TimeDelta start_time_; | 234 base::TimeDelta start_time_; |
| 258 | 235 |
| 259 // Whether audio has been disabled for this demuxer (in which case this class | 236 // Whether audio has been disabled for this demuxer (in which case this class |
| 260 // drops packets destined for AUDIO demuxer streams on the floor). | 237 // drops packets destined for AUDIO demuxer streams on the floor). |
| 261 bool audio_disabled_; | 238 bool audio_disabled_; |
| 262 | 239 |
| 263 // Set if we know duration of the audio stream. Used when processing end of | 240 // Set if we know duration of the audio stream. Used when processing end of |
| 264 // stream -- at this moment we definitely know duration. | 241 // stream -- at this moment we definitely know duration. |
| 265 bool duration_known_; | 242 bool duration_known_; |
| 266 | 243 |
| 244 // FFmpegURLProtocol implementation and corresponding glue bits. |
| 245 BlockingUrlProtocol url_protocol_; |
| 267 scoped_ptr<FFmpegGlue> glue_; | 246 scoped_ptr<FFmpegGlue> glue_; |
| 268 | 247 |
| 269 DISALLOW_COPY_AND_ASSIGN(FFmpegDemuxer); | 248 DISALLOW_COPY_AND_ASSIGN(FFmpegDemuxer); |
| 270 }; | 249 }; |
| 271 | 250 |
| 272 } // namespace media | 251 } // namespace media |
| 273 | 252 |
| 274 #endif // MEDIA_FILTERS_FFMPEG_DEMUXER_H_ | 253 #endif // MEDIA_FILTERS_FFMPEG_DEMUXER_H_ |
| OLD | NEW |