srpcgen: Use 'const char*' for string parameters
[chromium-blink-merge.git] / media / filters / ffmpeg_demuxer.h
blob5db09b33e108797153e6eb725971476aa396dc25
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
3 // found in the LICENSE file.
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
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
9 // can be used to create and initialize the corresponding FFmpeg decoder.
11 // FFmpegDemuxer sets the duration of pipeline during initialization by using
12 // the duration of the longest audio/video stream.
14 // NOTE: since FFmpegDemuxer reads packets sequentially without seeking, media
15 // files with very large drift between audio/video streams may result in
16 // excessive memory consumption.
18 // When stopped, FFmpegDemuxer and FFmpegDemuxerStream release all callbacks
19 // and buffered packets. Reads from a stopped FFmpegDemuxerStream will not be
20 // replied to.
22 #ifndef MEDIA_FILTERS_FFMPEG_DEMUXER_H_
23 #define MEDIA_FILTERS_FFMPEG_DEMUXER_H_
25 #include <deque>
26 #include <vector>
28 #include "base/callback.h"
29 #include "base/gtest_prod_util.h"
30 #include "base/synchronization/waitable_event.h"
31 #include "media/base/audio_decoder_config.h"
32 #include "media/base/buffers.h"
33 #include "media/base/demuxer.h"
34 #include "media/base/pipeline.h"
35 #include "media/base/video_decoder_config.h"
36 #include "media/filters/ffmpeg_glue.h"
38 // FFmpeg forward declarations.
39 struct AVFormatContext;
40 struct AVPacket;
41 struct AVRational;
42 struct AVStream;
44 namespace media {
46 class BitstreamConverter;
47 class FFmpegDemuxer;
49 class FFmpegDemuxerStream : public DemuxerStream {
50 public:
51 // Keeps a copy of |demuxer| and initializes itself using information
52 // inside |stream|. Both parameters must outlive |this|.
53 FFmpegDemuxerStream(FFmpegDemuxer* demuxer, AVStream* stream);
55 // Returns true is this stream has pending reads, false otherwise.
57 // Safe to call on any thread.
58 bool HasPendingReads();
60 // Enqueues and takes ownership over the given AVPacket.
61 void EnqueuePacket(AVPacket* packet);
63 // Signals to empty the buffer queue and mark next packet as discontinuous.
64 void FlushBuffers();
66 // Empties the queues and ignores any additional calls to Read().
67 void Stop();
69 // Returns the duration of this stream.
70 base::TimeDelta duration();
72 // DemuxerStream implementation.
73 virtual Type type() OVERRIDE;
75 // If |buffer_queue_| is not empty will execute on caller's thread, otherwise
76 // will post ReadTask to execute on demuxer's thread. Read will acquire
77 // |lock_| for the life of the function so that means |read_callback| must
78 // not make calls into FFmpegDemuxerStream directly or that may cause a
79 // deadlock. |read_callback| should execute as quickly as possible because
80 // |lock_| is held throughout the life of the callback.
81 virtual void Read(const ReadCallback& read_callback) OVERRIDE;
82 virtual void EnableBitstreamConverter() OVERRIDE;
83 virtual const AudioDecoderConfig& audio_decoder_config() OVERRIDE;
84 virtual const VideoDecoderConfig& video_decoder_config() OVERRIDE;
86 private:
87 friend class FFmpegDemuxerTest;
88 virtual ~FFmpegDemuxerStream();
90 // Carries out enqueuing a pending read on the demuxer thread.
91 void ReadTask(const ReadCallback& read_callback);
93 // Attempts to fulfill a single pending read by dequeueing a buffer and read
94 // callback pair and executing the callback. The calling function must
95 // acquire |lock_| before calling this function.
96 void FulfillPendingRead();
98 // Converts an FFmpeg stream timestamp into a base::TimeDelta.
99 static base::TimeDelta ConvertStreamTimestamp(const AVRational& time_base,
100 int64 timestamp);
102 FFmpegDemuxer* demuxer_;
103 AVStream* stream_;
104 AudioDecoderConfig audio_config_;
105 VideoDecoderConfig video_config_;
106 Type type_;
107 base::TimeDelta duration_;
108 bool discontinuous_;
109 bool stopped_;
111 typedef std::deque<scoped_refptr<Buffer> > BufferQueue;
112 BufferQueue buffer_queue_;
114 typedef std::deque<ReadCallback> ReadQueue;
115 ReadQueue read_queue_;
117 // Used to translate bitstream formats.
118 scoped_ptr<BitstreamConverter> bitstream_converter_;
120 // Used to synchronize access to |buffer_queue_|, |read_queue_|, and
121 // |stopped_|. This is so other threads can get access to buffers that have
122 // already been demuxed without having the demuxer thread sending the
123 // buffers. |lock_| must be acquired before any access to |buffer_queue_|,
124 // |read_queue_|, or |stopped_|.
125 base::Lock lock_;
127 DISALLOW_COPY_AND_ASSIGN(FFmpegDemuxerStream);
130 class MEDIA_EXPORT FFmpegDemuxer : public Demuxer, public FFmpegURLProtocol {
131 public:
132 FFmpegDemuxer(MessageLoop* message_loop, bool local_source);
133 virtual ~FFmpegDemuxer();
135 // Posts a task to perform additional demuxing.
136 virtual void PostDemuxTask();
138 void Initialize(
139 DataSource* data_source, const PipelineStatusCB& callback);
141 // Demuxer implementation.
142 virtual void Stop(const base::Closure& callback) OVERRIDE;
143 virtual void Seek(base::TimeDelta time, const PipelineStatusCB& cb) OVERRIDE;
144 virtual void OnAudioRendererDisabled() OVERRIDE;
145 virtual void set_host(DemuxerHost* demuxer_host) OVERRIDE;
146 virtual void SetPlaybackRate(float playback_rate) OVERRIDE;
147 virtual scoped_refptr<DemuxerStream> GetStream(
148 DemuxerStream::Type type) OVERRIDE;
149 virtual void SetPreload(Preload preload) OVERRIDE;
150 virtual base::TimeDelta GetStartTime() const OVERRIDE;
151 virtual int GetBitrate() OVERRIDE;
152 virtual bool IsLocalSource() OVERRIDE;
153 virtual bool IsSeekable() OVERRIDE;
155 // FFmpegURLProtocol implementation.
156 virtual size_t Read(size_t size, uint8* data) OVERRIDE;
157 virtual bool GetPosition(int64* position_out) OVERRIDE;
158 virtual bool SetPosition(int64 position) OVERRIDE;
159 virtual bool GetSize(int64* size_out) OVERRIDE;
160 virtual bool IsStreaming() OVERRIDE;
162 // Provide access to FFmpegDemuxerStream.
163 MessageLoop* message_loop();
165 // For testing purposes.
166 void disable_first_seek_hack_for_testing() { first_seek_hack_ = false; }
168 private:
169 // Only allow a factory to create this class.
170 friend class MockFFmpegDemuxer;
171 FRIEND_TEST_ALL_PREFIXES(FFmpegDemuxerTest, ProtocolRead);
173 // Carries out initialization on the demuxer thread.
174 void InitializeTask(
175 DataSource* data_source, const PipelineStatusCB& callback);
177 // Carries out a seek on the demuxer thread.
178 void SeekTask(base::TimeDelta time, const PipelineStatusCB& cb);
180 // Carries out demuxing and satisfying stream reads on the demuxer thread.
181 void DemuxTask();
183 // Carries out stopping the demuxer streams on the demuxer thread.
184 void StopTask(const base::Closure& callback);
186 // Carries out disabling the audio stream on the demuxer thread.
187 void DisableAudioStreamTask();
189 // Returns true if any of the streams have pending reads. Since we lazily
190 // post a DemuxTask() for every read, we use this method to quickly terminate
191 // the tasks if there is no work to do.
193 // Must be called on the demuxer thread.
194 bool StreamsHavePendingReads();
196 // Signal all FFmpegDemuxerStream that the stream has ended.
198 // Must be called on the demuxer thread.
199 void StreamHasEnded();
201 // Read callback method to be passed to DataSource. When the asynchronous
202 // read has completed, this method will be called from DataSource with
203 // number of bytes read or kDataSource in case of error.
204 void OnReadCompleted(size_t size);
206 // Wait for asynchronous read to complete and return number of bytes read.
207 virtual size_t WaitForRead();
209 // Signal that read has completed, and |size| bytes have been read.
210 virtual void SignalReadCompleted(size_t size);
212 MessageLoop* message_loop_;
214 // True if the media is a local resource, false if the media require network
215 // access to be loaded.
216 bool local_source_;
218 // FFmpeg context handle.
219 AVFormatContext* format_context_;
221 // |streams_| mirrors the AVStream array in |format_context_|. It contains
222 // FFmpegDemuxerStreams encapsluating AVStream objects at the same index.
224 // Since we only support a single audio and video stream, |streams_| will
225 // contain NULL entries for additional audio/video streams as well as for
226 // stream types that we do not currently support.
228 // Once initialized, operations on FFmpegDemuxerStreams should be carried out
229 // on the demuxer thread.
230 typedef std::vector<scoped_refptr<FFmpegDemuxerStream> > StreamVector;
231 StreamVector streams_;
233 // Reference to the data source. Asynchronous read requests are submitted to
234 // this object.
235 scoped_refptr<DataSource> data_source_;
237 // This member is used to block on read method calls from FFmpeg and wait
238 // until the asynchronous reads in the data source to complete. It is also
239 // signaled when the demuxer is being stopped.
240 base::WaitableEvent read_event_;
242 // Flag to indicate if read has ever failed. Once set to true, it will
243 // never be reset. This flag is set true and accessed in Read().
244 bool read_has_failed_;
246 size_t last_read_bytes_;
247 int64 read_position_;
249 // Initialization can happen before set_host() is called, in which case we
250 // store these bits for deferred reporting to the DemuxerHost when we get one.
251 base::TimeDelta max_duration_;
252 PipelineStatus deferred_status_;
254 // Used to skip the implicit "first seek" to avoid resetting FFmpeg's internal
255 // state.
256 bool first_seek_hack_;
258 // The first timestamp of the opened media file. This is used to set the
259 // starting clock value to match the timestamps in the media file. Default
260 // is 0.
261 base::TimeDelta start_time_;
263 DISALLOW_COPY_AND_ASSIGN(FFmpegDemuxer);
266 } // namespace media
268 #endif // MEDIA_FILTERS_FFMPEG_DEMUXER_H_