Make use of CreateWebUIForRenderManager (doesn't change the actual behavior)
[chromium-blink-merge.git] / media / filters / ffmpeg_demuxer.h
blob1056d07dc9a561d3562ef36a7caa647ab98f86cc
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
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 <vector>
27 #include "base/callback.h"
28 #include "base/gtest_prod_util.h"
29 #include "base/threading/thread.h"
30 #include "media/base/audio_decoder_config.h"
31 #include "media/base/decoder_buffer.h"
32 #include "media/base/decoder_buffer_queue.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/blocking_url_protocol.h"
38 // FFmpeg forward declarations.
39 struct AVPacket;
40 struct AVRational;
41 struct AVStream;
43 namespace media {
45 class FFmpegDemuxer;
46 class FFmpegGlue;
47 class FFmpegH264ToAnnexBBitstreamConverter;
48 class ScopedPtrAVFreePacket;
50 typedef scoped_ptr_malloc<AVPacket, ScopedPtrAVFreePacket> ScopedAVPacket;
52 class FFmpegDemuxerStream : public DemuxerStream {
53 public:
54 // Keeps a copy of |demuxer| and initializes itself using information
55 // inside |stream|. Both parameters must outlive |this|.
56 FFmpegDemuxerStream(FFmpegDemuxer* demuxer, AVStream* stream);
58 // Enqueues the given AVPacket. It is invalid to queue a |packet| after
59 // SetEndOfStream() has been called.
60 void EnqueuePacket(ScopedAVPacket packet);
62 // Enters the end of stream state. After delivering remaining queued buffers
63 // only end of stream buffers will be delivered.
64 void SetEndOfStream();
66 // Drops queued buffers and clears end of stream state.
67 void FlushBuffers();
69 // Empties the queues and ignores any additional calls to Read().
70 void Stop();
72 // Returns the duration of this stream.
73 base::TimeDelta duration();
75 // DemuxerStream implementation.
76 virtual Type type() OVERRIDE;
77 virtual void Read(const ReadCB& read_cb) OVERRIDE;
78 virtual void EnableBitstreamConverter() OVERRIDE;
79 virtual const AudioDecoderConfig& audio_decoder_config() OVERRIDE;
80 virtual const VideoDecoderConfig& video_decoder_config() OVERRIDE;
82 // Returns the range of buffered data in this stream.
83 Ranges<base::TimeDelta> GetBufferedRanges() const;
85 // Returns elapsed time based on the already queued packets.
86 // Used to determine stream duration when it's not known ahead of time.
87 base::TimeDelta GetElapsedTime() const;
89 // Returns true if this stream has capacity for additional data.
90 bool HasAvailableCapacity();
92 protected:
93 virtual ~FFmpegDemuxerStream();
95 private:
96 friend class FFmpegDemuxerTest;
98 // Runs |read_cb_| if present with the front of |buffer_queue_|, calling
99 // NotifyCapacityAvailable() if capacity is still available.
100 void SatisfyPendingRead();
102 // Converts an FFmpeg stream timestamp into a base::TimeDelta.
103 static base::TimeDelta ConvertStreamTimestamp(const AVRational& time_base,
104 int64 timestamp);
106 FFmpegDemuxer* demuxer_;
107 scoped_refptr<base::MessageLoopProxy> message_loop_;
108 AVStream* stream_;
109 AudioDecoderConfig audio_config_;
110 VideoDecoderConfig video_config_;
111 Type type_;
112 base::TimeDelta duration_;
113 bool stopped_;
114 bool end_of_stream_;
115 base::TimeDelta last_packet_timestamp_;
116 Ranges<base::TimeDelta> buffered_ranges_;
118 DecoderBufferQueue buffer_queue_;
119 ReadCB read_cb_;
121 scoped_ptr<FFmpegH264ToAnnexBBitstreamConverter> bitstream_converter_;
122 bool bitstream_converter_enabled_;
124 DISALLOW_COPY_AND_ASSIGN(FFmpegDemuxerStream);
127 class MEDIA_EXPORT FFmpegDemuxer : public Demuxer {
128 public:
129 FFmpegDemuxer(const scoped_refptr<base::MessageLoopProxy>& message_loop,
130 const scoped_refptr<DataSource>& data_source);
132 // Demuxer implementation.
133 virtual void Initialize(DemuxerHost* host,
134 const PipelineStatusCB& status_cb) OVERRIDE;
135 virtual void Stop(const base::Closure& callback) OVERRIDE;
136 virtual void Seek(base::TimeDelta time, const PipelineStatusCB& cb) OVERRIDE;
137 virtual void OnAudioRendererDisabled() OVERRIDE;
138 virtual void SetPlaybackRate(float playback_rate) OVERRIDE;
139 virtual scoped_refptr<DemuxerStream> GetStream(
140 DemuxerStream::Type type) OVERRIDE;
141 virtual base::TimeDelta GetStartTime() const OVERRIDE;
143 // Allow FFmpegDemuxerStream to notify us when there is updated information
144 // about capacity and what buffered data is available.
145 void NotifyCapacityAvailable();
146 void NotifyBufferingChanged();
148 private:
149 // To allow tests access to privates.
150 friend class FFmpegDemuxerTest;
152 virtual ~FFmpegDemuxer();
154 // FFmpeg callbacks during initialization.
155 void OnOpenContextDone(const PipelineStatusCB& status_cb, bool result);
156 void OnFindStreamInfoDone(const PipelineStatusCB& status_cb, int result);
158 // FFmpeg callbacks during seeking.
159 void OnSeekFrameDone(const PipelineStatusCB& cb, int result);
161 // FFmpeg callbacks during reading + helper method to initiate reads.
162 void ReadFrameIfNeeded();
163 void OnReadFrameDone(ScopedAVPacket packet, int result);
165 // DataSource callbacks during stopping.
166 void OnDataSourceStopped(const base::Closure& callback);
168 // Returns true iff any stream has additional capacity. Note that streams can
169 // go over capacity depending on how the file is muxed.
170 bool StreamsHaveAvailableCapacity();
172 // Signal all FFmpegDemuxerStreams that the stream has ended.
173 void StreamHasEnded();
175 // Called by |url_protocol_| whenever |data_source_| returns a read error.
176 void OnDataSourceError();
178 // Returns the stream from |streams_| that matches |type| as an
179 // FFmpegDemuxerStream.
180 scoped_refptr<FFmpegDemuxerStream> GetFFmpegStream(
181 DemuxerStream::Type type) const;
183 DemuxerHost* host_;
185 scoped_refptr<base::MessageLoopProxy> message_loop_;
187 // Thread on which all blocking FFmpeg operations are executed.
188 base::Thread blocking_thread_;
190 // Tracks if there's an outstanding av_read_frame() operation.
192 // TODO(scherkus): Allow more than one read in flight for higher read
193 // throughput using demuxer_bench to verify improvements.
194 bool pending_read_;
196 // Tracks if there's an outstanding av_seek_frame() operation. Used to discard
197 // results of pre-seek av_read_frame() operations.
198 bool pending_seek_;
200 // |streams_| mirrors the AVStream array in |format_context_|. It contains
201 // FFmpegDemuxerStreams encapsluating AVStream objects at the same index.
203 // Since we only support a single audio and video stream, |streams_| will
204 // contain NULL entries for additional audio/video streams as well as for
205 // stream types that we do not currently support.
207 // Once initialized, operations on FFmpegDemuxerStreams should be carried out
208 // on the demuxer thread.
209 typedef std::vector<scoped_refptr<FFmpegDemuxerStream> > StreamVector;
210 StreamVector streams_;
212 // Reference to the data source. Asynchronous read requests are submitted to
213 // this object.
214 scoped_refptr<DataSource> data_source_;
216 // Derived bitrate after initialization has completed.
217 int bitrate_;
219 // The first timestamp of the opened media file. This is used to set the
220 // starting clock value to match the timestamps in the media file. Default
221 // is 0.
222 base::TimeDelta start_time_;
224 // Whether audio has been disabled for this demuxer (in which case this class
225 // drops packets destined for AUDIO demuxer streams on the floor).
226 bool audio_disabled_;
228 // Set if we know duration of the audio stream. Used when processing end of
229 // stream -- at this moment we definitely know duration.
230 bool duration_known_;
232 // FFmpegURLProtocol implementation and corresponding glue bits.
233 BlockingUrlProtocol url_protocol_;
234 scoped_ptr<FFmpegGlue> glue_;
236 DISALLOW_COPY_AND_ASSIGN(FFmpegDemuxer);
239 } // namespace media
241 #endif // MEDIA_FILTERS_FFMPEG_DEMUXER_H_