[MD settings] moving attached() code
[chromium-blink-merge.git] / media / filters / ffmpeg_demuxer.h
blobbc7c3bd525320e2bd33c04923b9e0aa83264934a
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 <string>
26 #include <utility>
27 #include <vector>
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.
44 struct AVPacket;
45 struct AVRational;
46 struct AVStream;
48 namespace media {
50 class MediaLog;
51 class FFmpegBitstreamConverter;
52 class FFmpegDemuxer;
53 class FFmpegGlue;
55 typedef scoped_ptr<AVPacket, ScopedPtrAVFreePacket> ScopedAVPacket;
57 class FFmpegDemuxerStream : public DemuxerStream {
58 public:
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 ~FFmpegDemuxerStream() override;
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.
73 void FlushBuffers();
75 // Empties the queues and ignores any additional calls to Read().
76 void Stop();
78 base::TimeDelta duration() const { return duration_; }
80 // Enables fixes for files with negative timestamps. Normally all timestamps
81 // are rebased against FFmpegDemuxer::start_time() whenever that value is
82 // negative. When this fix is enabled, only AUDIO stream packets will be
83 // rebased to time zero, all other stream types will use the muxed timestamp.
85 // Further, when no codec delay is present, all AUDIO packets which originally
86 // had negative timestamps will be marked for post-decode discard. When codec
87 // delay is present, it is assumed the decoder will handle discard and does
88 // not need the AUDIO packets to be marked for discard; just rebased to zero.
89 void enable_negative_timestamp_fixups() {
90 fixup_negative_timestamps_ = true;
93 // DemuxerStream implementation.
94 Type type() const override;
95 Liveness liveness() const override;
96 void Read(const ReadCB& read_cb) override;
97 void EnableBitstreamConverter() override;
98 bool SupportsConfigChanges() override;
99 AudioDecoderConfig audio_decoder_config() override;
100 VideoDecoderConfig video_decoder_config() override;
101 VideoRotation video_rotation() override;
103 void SetLiveness(Liveness liveness);
105 // Returns the range of buffered data in this stream.
106 Ranges<base::TimeDelta> GetBufferedRanges() const;
108 // Returns elapsed time based on the already queued packets.
109 // Used to determine stream duration when it's not known ahead of time.
110 base::TimeDelta GetElapsedTime() const;
112 // Returns true if this stream has capacity for additional data.
113 bool HasAvailableCapacity();
115 // Returns the total buffer size FFMpegDemuxerStream is holding onto.
116 size_t MemoryUsage() const;
118 TextKind GetTextKind() const;
120 // Returns the value associated with |key| in the metadata for the avstream.
121 // Returns an empty string if the key is not present.
122 std::string GetMetadata(const char* key) const;
124 private:
125 friend class FFmpegDemuxerTest;
127 // Runs |read_cb_| if present with the front of |buffer_queue_|, calling
128 // NotifyCapacityAvailable() if capacity is still available.
129 void SatisfyPendingRead();
131 // Converts an FFmpeg stream timestamp into a base::TimeDelta.
132 static base::TimeDelta ConvertStreamTimestamp(const AVRational& time_base,
133 int64 timestamp);
135 // Resets any currently active bitstream converter.
136 void ResetBitstreamConverter();
138 // Create new bitstream converter, destroying active converter if present.
139 void InitBitstreamConverter();
141 FFmpegDemuxer* demuxer_;
142 scoped_refptr<base::SingleThreadTaskRunner> task_runner_;
143 AVStream* stream_;
144 AudioDecoderConfig audio_config_;
145 VideoDecoderConfig video_config_;
146 Type type_;
147 Liveness liveness_;
148 base::TimeDelta duration_;
149 bool end_of_stream_;
150 base::TimeDelta last_packet_timestamp_;
151 base::TimeDelta last_packet_duration_;
152 Ranges<base::TimeDelta> buffered_ranges_;
153 VideoRotation video_rotation_;
155 DecoderBufferQueue buffer_queue_;
156 ReadCB read_cb_;
158 #if defined(USE_PROPRIETARY_CODECS)
159 scoped_ptr<FFmpegBitstreamConverter> bitstream_converter_;
160 #endif
162 std::string encryption_key_id_;
163 bool fixup_negative_timestamps_;
165 DISALLOW_COPY_AND_ASSIGN(FFmpegDemuxerStream);
168 class MEDIA_EXPORT FFmpegDemuxer : public Demuxer {
169 public:
170 FFmpegDemuxer(const scoped_refptr<base::SingleThreadTaskRunner>& task_runner,
171 DataSource* data_source,
172 const EncryptedMediaInitDataCB& encrypted_media_init_data_cb,
173 const scoped_refptr<MediaLog>& media_log);
174 ~FFmpegDemuxer() override;
176 // Demuxer implementation.
177 std::string GetDisplayName() const override;
178 void Initialize(DemuxerHost* host,
179 const PipelineStatusCB& status_cb,
180 bool enable_text_tracks) override;
181 void Stop() override;
182 void Seek(base::TimeDelta time, const PipelineStatusCB& cb) override;
183 base::Time GetTimelineOffset() const override;
184 DemuxerStream* GetStream(DemuxerStream::Type type) override;
185 base::TimeDelta GetStartTime() const override;
187 // Calls |encrypted_media_init_data_cb_| with the initialization data
188 // encountered in the file.
189 void OnEncryptedMediaInitData(EmeInitDataType init_data_type,
190 const std::string& encryption_key_id);
192 // Allow FFmpegDemuxerStream to notify us when there is updated information
193 // about capacity and what buffered data is available.
194 void NotifyCapacityAvailable();
195 void NotifyBufferingChanged();
197 // The lowest demuxed timestamp. If negative, DemuxerStreams must use this to
198 // adjust packet timestamps such that external clients see a zero-based
199 // timeline.
200 base::TimeDelta start_time() const { return start_time_; }
202 private:
203 // To allow tests access to privates.
204 friend class FFmpegDemuxerTest;
206 // FFmpeg callbacks during initialization.
207 void OnOpenContextDone(const PipelineStatusCB& status_cb, bool result);
208 void OnFindStreamInfoDone(const PipelineStatusCB& status_cb, int result);
210 // FFmpeg callbacks during seeking.
211 void OnSeekFrameDone(const PipelineStatusCB& cb, int result);
213 // FFmpeg callbacks during reading + helper method to initiate reads.
214 void ReadFrameIfNeeded();
215 void OnReadFrameDone(ScopedAVPacket packet, int result);
217 // Returns true iff any stream has additional capacity. Note that streams can
218 // go over capacity depending on how the file is muxed.
219 bool StreamsHaveAvailableCapacity();
221 // Returns true if the maximum allowed memory usage has been reached.
222 bool IsMaxMemoryUsageReached() const;
224 // Signal all FFmpegDemuxerStreams that the stream has ended.
225 void StreamHasEnded();
227 // Called by |url_protocol_| whenever |data_source_| returns a read error.
228 void OnDataSourceError();
230 // Returns the stream from |streams_| that matches |type| as an
231 // FFmpegDemuxerStream.
232 FFmpegDemuxerStream* GetFFmpegStream(DemuxerStream::Type type) const;
234 // Called after the streams have been collected from the media, to allow
235 // the text renderer to bind each text stream to the cue rendering engine.
236 void AddTextStreams();
238 void SetLiveness(DemuxerStream::Liveness liveness);
240 DemuxerHost* host_;
242 scoped_refptr<base::SingleThreadTaskRunner> task_runner_;
244 // Thread on which all blocking FFmpeg operations are executed.
245 base::Thread blocking_thread_;
247 // Tracks if there's an outstanding av_read_frame() operation.
249 // TODO(scherkus): Allow more than one read in flight for higher read
250 // throughput using demuxer_bench to verify improvements.
251 bool pending_read_;
253 // Tracks if there's an outstanding av_seek_frame() operation. Used to discard
254 // results of pre-seek av_read_frame() operations.
255 bool pending_seek_;
257 // |streams_| mirrors the AVStream array in AVFormatContext. It contains
258 // FFmpegDemuxerStreams encapsluating AVStream objects at the same index.
260 // Since we only support a single audio and video stream, |streams_| will
261 // contain NULL entries for additional audio/video streams as well as for
262 // stream types that we do not currently support.
264 // Once initialized, operations on FFmpegDemuxerStreams should be carried out
265 // on the demuxer thread.
266 typedef ScopedVector<FFmpegDemuxerStream> StreamVector;
267 StreamVector streams_;
269 // Provides asynchronous IO to this demuxer. Consumed by |url_protocol_| to
270 // integrate with libavformat.
271 DataSource* data_source_;
273 scoped_refptr<MediaLog> media_log_;
275 // Derived bitrate after initialization has completed.
276 int bitrate_;
278 // The first timestamp of the audio or video stream, whichever is lower. This
279 // is used to adjust timestamps so that external consumers always see a zero
280 // based timeline.
281 base::TimeDelta start_time_;
283 // The index and start time of the preferred streams for seeking. Filled upon
284 // completion of OnFindStreamInfoDone(). Each info entry represents an index
285 // into |streams_| and the start time of that stream.
287 // Seek() will attempt to use |preferred_stream_for_seeking_| if the seek
288 // point occurs after its associated start time. Otherwise it will use
289 // |fallback_stream_for_seeking_|.
290 typedef std::pair<int, base::TimeDelta> StreamSeekInfo;
291 StreamSeekInfo preferred_stream_for_seeking_;
292 StreamSeekInfo fallback_stream_for_seeking_;
294 // The Time associated with timestamp 0. Set to a null
295 // time if the file doesn't have an association to Time.
296 base::Time timeline_offset_;
298 // Whether text streams have been enabled for this demuxer.
299 bool text_enabled_;
301 // Set if we know duration of the audio stream. Used when processing end of
302 // stream -- at this moment we definitely know duration.
303 bool duration_known_;
305 // FFmpegURLProtocol implementation and corresponding glue bits.
306 scoped_ptr<BlockingUrlProtocol> url_protocol_;
307 scoped_ptr<FFmpegGlue> glue_;
309 const EncryptedMediaInitDataCB encrypted_media_init_data_cb_;
311 // NOTE: Weak pointers must be invalidated before all other member variables.
312 base::WeakPtrFactory<FFmpegDemuxer> weak_factory_;
314 DISALLOW_COPY_AND_ASSIGN(FFmpegDemuxer);
317 } // namespace media
319 #endif // MEDIA_FILTERS_FFMPEG_DEMUXER_H_