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
22 #ifndef MEDIA_FILTERS_FFMPEG_DEMUXER_H_
23 #define MEDIA_FILTERS_FFMPEG_DEMUXER_H_
29 #include "base/callback.h"
30 #include "base/gtest_prod_util.h"
31 #include "base/memory/scoped_vector.h"
32 #include "base/threading/thread.h"
33 #include "media/base/audio_decoder_config.h"
34 #include "media/base/decoder_buffer.h"
35 #include "media/base/decoder_buffer_queue.h"
36 #include "media/base/demuxer.h"
37 #include "media/base/pipeline.h"
38 #include "media/base/text_track_config.h"
39 #include "media/base/video_decoder_config.h"
40 #include "media/ffmpeg/ffmpeg_deleters.h"
41 #include "media/filters/blocking_url_protocol.h"
43 // FFmpeg forward declarations.
53 class FFmpegH264ToAnnexBBitstreamConverter;
55 typedef scoped_ptr<AVPacket, ScopedPtrAVFreePacket> ScopedAVPacket;
57 class FFmpegDemuxerStream : public DemuxerStream {
59 // Keeps a copy of |demuxer| and initializes itself using information inside
60 // |stream|. Both parameters must outlive |this|.
61 FFmpegDemuxerStream(FFmpegDemuxer* demuxer, AVStream* stream);
62 virtual ~FFmpegDemuxerStream();
64 // Enqueues the given AVPacket. It is invalid to queue a |packet| after
65 // SetEndOfStream() has been called.
66 void EnqueuePacket(ScopedAVPacket packet);
68 // Enters the end of stream state. After delivering remaining queued buffers
69 // only end of stream buffers will be delivered.
70 void SetEndOfStream();
72 // Drops queued buffers and clears end of stream state.
75 // Empties the queues and ignores any additional calls to Read().
78 base::TimeDelta duration() const { return duration_; }
80 // Enables fixes for ogg files with negative timestamps. For AUDIO streams,
81 // all packets with negative timestamps will be marked for post-decode
82 // discard. For all other stream types, if FFmpegDemuxer::start_time() is
83 // negative, it will not be used to shift timestamps during EnqueuePacket().
84 void enable_negative_timestamp_fixups_for_ogg() {
85 fixup_negative_ogg_timestamps_ = true;
88 // DemuxerStream implementation.
89 virtual Type type() OVERRIDE;
90 virtual void Read(const ReadCB& read_cb) OVERRIDE;
91 virtual void EnableBitstreamConverter() OVERRIDE;
92 virtual bool SupportsConfigChanges() OVERRIDE;
93 virtual AudioDecoderConfig audio_decoder_config() OVERRIDE;
94 virtual VideoDecoderConfig video_decoder_config() OVERRIDE;
95 virtual VideoRotation video_rotation() OVERRIDE;
97 // Returns the range of buffered data in this stream.
98 Ranges<base::TimeDelta> GetBufferedRanges() const;
100 // Returns elapsed time based on the already queued packets.
101 // Used to determine stream duration when it's not known ahead of time.
102 base::TimeDelta GetElapsedTime() const;
104 // Returns true if this stream has capacity for additional data.
105 bool HasAvailableCapacity();
107 // Returns the total buffer size FFMpegDemuxerStream is holding onto.
108 size_t MemoryUsage() const;
110 TextKind GetTextKind() const;
112 // Returns the value associated with |key| in the metadata for the avstream.
113 // Returns an empty string if the key is not present.
114 std::string GetMetadata(const char* key) const;
117 friend class FFmpegDemuxerTest;
119 // Runs |read_cb_| if present with the front of |buffer_queue_|, calling
120 // NotifyCapacityAvailable() if capacity is still available.
121 void SatisfyPendingRead();
123 // Converts an FFmpeg stream timestamp into a base::TimeDelta.
124 static base::TimeDelta ConvertStreamTimestamp(const AVRational& time_base,
127 FFmpegDemuxer* demuxer_;
128 scoped_refptr<base::SingleThreadTaskRunner> task_runner_;
130 AudioDecoderConfig audio_config_;
131 VideoDecoderConfig video_config_;
133 base::TimeDelta duration_;
135 base::TimeDelta last_packet_timestamp_;
136 base::TimeDelta last_packet_duration_;
137 Ranges<base::TimeDelta> buffered_ranges_;
138 VideoRotation video_rotation_;
140 DecoderBufferQueue buffer_queue_;
143 #if defined(USE_PROPRIETARY_CODECS)
144 scoped_ptr<FFmpegH264ToAnnexBBitstreamConverter> bitstream_converter_;
147 bool bitstream_converter_enabled_;
149 std::string encryption_key_id_;
150 bool fixup_negative_ogg_timestamps_;
152 DISALLOW_COPY_AND_ASSIGN(FFmpegDemuxerStream);
155 class MEDIA_EXPORT FFmpegDemuxer : public Demuxer {
157 FFmpegDemuxer(const scoped_refptr<base::SingleThreadTaskRunner>& task_runner,
158 DataSource* data_source,
159 const NeedKeyCB& need_key_cb,
160 const scoped_refptr<MediaLog>& media_log);
161 virtual ~FFmpegDemuxer();
163 // Demuxer implementation.
164 virtual void Initialize(DemuxerHost* host,
165 const PipelineStatusCB& status_cb,
166 bool enable_text_tracks) OVERRIDE;
167 virtual void Stop(const base::Closure& callback) OVERRIDE;
168 virtual void Seek(base::TimeDelta time, const PipelineStatusCB& cb) OVERRIDE;
169 virtual DemuxerStream* GetStream(DemuxerStream::Type type) OVERRIDE;
170 virtual base::Time GetTimelineOffset() const OVERRIDE;
171 virtual Liveness GetLiveness() const OVERRIDE;
173 // Calls |need_key_cb_| with the initialization data encountered in the file.
174 void FireNeedKey(const std::string& init_data_type,
175 const std::string& encryption_key_id);
177 // Allow FFmpegDemuxerStream to notify us when there is updated information
178 // about capacity and what buffered data is available.
179 void NotifyCapacityAvailable();
180 void NotifyBufferingChanged();
182 // The lowest demuxed timestamp. DemuxerStream's must use this to adjust
183 // packet timestamps such that external clients see a zero-based timeline.
184 base::TimeDelta start_time() const { return start_time_; }
187 // To allow tests access to privates.
188 friend class FFmpegDemuxerTest;
190 // FFmpeg callbacks during initialization.
191 void OnOpenContextDone(const PipelineStatusCB& status_cb, bool result);
192 void OnFindStreamInfoDone(const PipelineStatusCB& status_cb, int result);
194 // FFmpeg callbacks during seeking.
195 void OnSeekFrameDone(const PipelineStatusCB& cb, int result);
197 // FFmpeg callbacks during reading + helper method to initiate reads.
198 void ReadFrameIfNeeded();
199 void OnReadFrameDone(ScopedAVPacket packet, int result);
201 // Returns true iff any stream has additional capacity. Note that streams can
202 // go over capacity depending on how the file is muxed.
203 bool StreamsHaveAvailableCapacity();
205 // Returns true if the maximum allowed memory usage has been reached.
206 bool IsMaxMemoryUsageReached() const;
208 // Signal all FFmpegDemuxerStreams that the stream has ended.
209 void StreamHasEnded();
211 // Called by |url_protocol_| whenever |data_source_| returns a read error.
212 void OnDataSourceError();
214 // Returns the stream from |streams_| that matches |type| as an
215 // FFmpegDemuxerStream.
216 FFmpegDemuxerStream* GetFFmpegStream(DemuxerStream::Type type) const;
218 // Called after the streams have been collected from the media, to allow
219 // the text renderer to bind each text stream to the cue rendering engine.
220 void AddTextStreams();
224 scoped_refptr<base::SingleThreadTaskRunner> task_runner_;
226 // Thread on which all blocking FFmpeg operations are executed.
227 base::Thread blocking_thread_;
229 // Tracks if there's an outstanding av_read_frame() operation.
231 // TODO(scherkus): Allow more than one read in flight for higher read
232 // throughput using demuxer_bench to verify improvements.
235 // Tracks if there's an outstanding av_seek_frame() operation. Used to discard
236 // results of pre-seek av_read_frame() operations.
239 // |streams_| mirrors the AVStream array in AVFormatContext. It contains
240 // FFmpegDemuxerStreams encapsluating AVStream objects at the same index.
242 // Since we only support a single audio and video stream, |streams_| will
243 // contain NULL entries for additional audio/video streams as well as for
244 // stream types that we do not currently support.
246 // Once initialized, operations on FFmpegDemuxerStreams should be carried out
247 // on the demuxer thread.
248 typedef ScopedVector<FFmpegDemuxerStream> StreamVector;
249 StreamVector streams_;
251 // Provides asynchronous IO to this demuxer. Consumed by |url_protocol_| to
252 // integrate with libavformat.
253 DataSource* data_source_;
255 scoped_refptr<MediaLog> media_log_;
257 // Derived bitrate after initialization has completed.
260 // The first timestamp of the audio or video stream, whichever is lower. This
261 // is used to adjust timestamps so that external consumers always see a zero
263 base::TimeDelta start_time_;
265 // The index and start time of the preferred streams for seeking. Filled upon
266 // completion of OnFindStreamInfoDone(). Each info entry represents an index
267 // into |streams_| and the start time of that stream.
269 // Seek() will attempt to use |preferred_stream_for_seeking_| if the seek
270 // point occurs after its associated start time. Otherwise it will use
271 // |fallback_stream_for_seeking_|.
272 typedef std::pair<int, base::TimeDelta> StreamSeekInfo;
273 StreamSeekInfo preferred_stream_for_seeking_;
274 StreamSeekInfo fallback_stream_for_seeking_;
276 // The Time associated with timestamp 0. Set to a null
277 // time if the file doesn't have an association to Time.
278 base::Time timeline_offset_;
280 // Liveness of the stream.
283 // Whether text streams have been enabled for this demuxer.
286 // Set if we know duration of the audio stream. Used when processing end of
287 // stream -- at this moment we definitely know duration.
288 bool duration_known_;
290 // FFmpegURLProtocol implementation and corresponding glue bits.
291 scoped_ptr<BlockingUrlProtocol> url_protocol_;
292 scoped_ptr<FFmpegGlue> glue_;
294 const NeedKeyCB need_key_cb_;
296 // NOTE: Weak pointers must be invalidated before all other member variables.
297 base::WeakPtrFactory<FFmpegDemuxer> weak_factory_;
299 DISALLOW_COPY_AND_ASSIGN(FFmpegDemuxer);
304 #endif // MEDIA_FILTERS_FFMPEG_DEMUXER_H_