Cast: Skip receiver log messages with time delta that can't be encoded.
[chromium-blink-merge.git] / media / filters / ffmpeg_demuxer.h
blob32cba81bd7888cc7294d53e1ad936f7d606faf5b
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 <vector>
28 #include "base/callback.h"
29 #include "base/gtest_prod_util.h"
30 #include "base/memory/scoped_vector.h"
31 #include "base/threading/thread.h"
32 #include "media/base/audio_decoder_config.h"
33 #include "media/base/decoder_buffer.h"
34 #include "media/base/decoder_buffer_queue.h"
35 #include "media/base/demuxer.h"
36 #include "media/base/pipeline.h"
37 #include "media/base/text_track_config.h"
38 #include "media/base/video_decoder_config.h"
39 #include "media/filters/blocking_url_protocol.h"
41 // FFmpeg forward declarations.
42 struct AVPacket;
43 struct AVRational;
44 struct AVStream;
46 namespace media {
48 class MediaLog;
49 class FFmpegDemuxer;
50 class FFmpegGlue;
51 class FFmpegH264ToAnnexBBitstreamConverter;
52 class ScopedPtrAVFreePacket;
54 typedef scoped_ptr_malloc<AVPacket, ScopedPtrAVFreePacket> ScopedAVPacket;
56 class FFmpegDemuxerStream : public DemuxerStream {
57 public:
58 // Keeps a copy of |demuxer| and initializes itself using information
59 // inside |stream|. Both parameters must outlive |this|.
60 FFmpegDemuxerStream(FFmpegDemuxer* demuxer, AVStream* stream);
61 virtual ~FFmpegDemuxerStream();
63 // Enqueues the given AVPacket. It is invalid to queue a |packet| after
64 // SetEndOfStream() has been called.
65 void EnqueuePacket(ScopedAVPacket packet);
67 // Enters the end of stream state. After delivering remaining queued buffers
68 // only end of stream buffers will be delivered.
69 void SetEndOfStream();
71 // Drops queued buffers and clears end of stream state.
72 void FlushBuffers();
74 // Empties the queues and ignores any additional calls to Read().
75 void Stop();
77 // Returns the duration of this stream.
78 base::TimeDelta duration();
80 // DemuxerStream implementation.
81 virtual Type type() OVERRIDE;
82 virtual void Read(const ReadCB& read_cb) OVERRIDE;
83 virtual void EnableBitstreamConverter() OVERRIDE;
84 virtual AudioDecoderConfig audio_decoder_config() OVERRIDE;
85 virtual VideoDecoderConfig video_decoder_config() OVERRIDE;
87 // Returns the range of buffered data in this stream.
88 Ranges<base::TimeDelta> GetBufferedRanges() const;
90 // Returns elapsed time based on the already queued packets.
91 // Used to determine stream duration when it's not known ahead of time.
92 base::TimeDelta GetElapsedTime() const;
94 // Returns true if this stream has capacity for additional data.
95 bool HasAvailableCapacity();
97 // Returns the total buffer size FFMpegDemuxerStream is holding onto.
98 size_t MemoryUsage() const;
100 TextKind GetTextKind() const;
102 // Returns the value associated with |key| in the metadata for the avstream.
103 // Returns an empty string if the key is not present.
104 std::string GetMetadata(const char* key) const;
106 private:
107 friend class FFmpegDemuxerTest;
109 // Runs |read_cb_| if present with the front of |buffer_queue_|, calling
110 // NotifyCapacityAvailable() if capacity is still available.
111 void SatisfyPendingRead();
113 // Converts an FFmpeg stream timestamp into a base::TimeDelta.
114 static base::TimeDelta ConvertStreamTimestamp(const AVRational& time_base,
115 int64 timestamp);
117 FFmpegDemuxer* demuxer_;
118 scoped_refptr<base::SingleThreadTaskRunner> task_runner_;
119 AVStream* stream_;
120 AudioDecoderConfig audio_config_;
121 VideoDecoderConfig video_config_;
122 Type type_;
123 base::TimeDelta duration_;
124 bool end_of_stream_;
125 base::TimeDelta last_packet_timestamp_;
126 Ranges<base::TimeDelta> buffered_ranges_;
128 DecoderBufferQueue buffer_queue_;
129 ReadCB read_cb_;
131 scoped_ptr<FFmpegH264ToAnnexBBitstreamConverter> bitstream_converter_;
132 bool bitstream_converter_enabled_;
134 std::string encryption_key_id_;
136 DISALLOW_COPY_AND_ASSIGN(FFmpegDemuxerStream);
139 class MEDIA_EXPORT FFmpegDemuxer : public Demuxer {
140 public:
141 FFmpegDemuxer(const scoped_refptr<base::SingleThreadTaskRunner>& task_runner,
142 DataSource* data_source,
143 const NeedKeyCB& need_key_cb,
144 const scoped_refptr<MediaLog>& media_log);
145 virtual ~FFmpegDemuxer();
147 // Demuxer implementation.
148 virtual void Initialize(DemuxerHost* host,
149 const PipelineStatusCB& status_cb,
150 bool enable_text_tracks) OVERRIDE;
151 virtual void Stop(const base::Closure& callback) OVERRIDE;
152 virtual void Seek(base::TimeDelta time, const PipelineStatusCB& cb) OVERRIDE;
153 virtual void OnAudioRendererDisabled() OVERRIDE;
154 virtual DemuxerStream* GetStream(DemuxerStream::Type type) OVERRIDE;
155 virtual base::TimeDelta GetStartTime() const OVERRIDE;
157 // Calls |need_key_cb_| with the initialization data encountered in the file.
158 void FireNeedKey(const std::string& init_data_type,
159 const std::string& encryption_key_id);
161 // Allow FFmpegDemuxerStream to notify us when there is updated information
162 // about capacity and what buffered data is available.
163 void NotifyCapacityAvailable();
164 void NotifyBufferingChanged();
166 private:
167 // To allow tests access to privates.
168 friend class FFmpegDemuxerTest;
170 // FFmpeg callbacks during initialization.
171 void OnOpenContextDone(const PipelineStatusCB& status_cb, bool result);
172 void OnFindStreamInfoDone(const PipelineStatusCB& status_cb, int result);
174 // FFmpeg callbacks during seeking.
175 void OnSeekFrameDone(const PipelineStatusCB& cb, int result);
177 // FFmpeg callbacks during reading + helper method to initiate reads.
178 void ReadFrameIfNeeded();
179 void OnReadFrameDone(ScopedAVPacket packet, int result);
181 // DataSource callbacks during stopping.
182 void OnDataSourceStopped(const base::Closure& callback);
184 // Returns true iff any stream has additional capacity. Note that streams can
185 // go over capacity depending on how the file is muxed.
186 bool StreamsHaveAvailableCapacity();
188 // Returns true if the maximum allowed memory usage has been reached.
189 bool IsMaxMemoryUsageReached() const;
191 // Signal all FFmpegDemuxerStreams that the stream has ended.
192 void StreamHasEnded();
194 // Called by |url_protocol_| whenever |data_source_| returns a read error.
195 void OnDataSourceError();
197 // Returns the stream from |streams_| that matches |type| as an
198 // FFmpegDemuxerStream.
199 FFmpegDemuxerStream* GetFFmpegStream(DemuxerStream::Type type) const;
201 // Called after the streams have been collected from the media, to allow
202 // the text renderer to bind each text stream to the cue rendering engine.
203 void AddTextStreams();
205 DemuxerHost* host_;
207 scoped_refptr<base::SingleThreadTaskRunner> task_runner_;
208 base::WeakPtrFactory<FFmpegDemuxer> weak_factory_;
209 base::WeakPtr<FFmpegDemuxer> weak_this_;
211 // Thread on which all blocking FFmpeg operations are executed.
212 base::Thread blocking_thread_;
214 // Tracks if there's an outstanding av_read_frame() operation.
216 // TODO(scherkus): Allow more than one read in flight for higher read
217 // throughput using demuxer_bench to verify improvements.
218 bool pending_read_;
220 // Tracks if there's an outstanding av_seek_frame() operation. Used to discard
221 // results of pre-seek av_read_frame() operations.
222 bool pending_seek_;
224 // |streams_| mirrors the AVStream array in |format_context_|. It contains
225 // FFmpegDemuxerStreams encapsluating AVStream objects at the same index.
227 // Since we only support a single audio and video stream, |streams_| will
228 // contain NULL entries for additional audio/video streams as well as for
229 // stream types that we do not currently support.
231 // Once initialized, operations on FFmpegDemuxerStreams should be carried out
232 // on the demuxer thread.
233 typedef ScopedVector<FFmpegDemuxerStream> StreamVector;
234 StreamVector streams_;
236 // Provides asynchronous IO to this demuxer. Consumed by |url_protocol_| to
237 // integrate with libavformat.
238 DataSource* data_source_;
240 scoped_refptr<MediaLog> media_log_;
242 // Derived bitrate after initialization has completed.
243 int bitrate_;
245 // The first timestamp of the opened media file. This is used to set the
246 // starting clock value to match the timestamps in the media file. Default
247 // is 0.
248 base::TimeDelta start_time_;
250 // Whether audio has been disabled for this demuxer (in which case this class
251 // drops packets destined for AUDIO demuxer streams on the floor).
252 bool audio_disabled_;
254 // Whether text streams have been enabled for this demuxer.
255 bool text_enabled_;
257 // Set if we know duration of the audio stream. Used when processing end of
258 // stream -- at this moment we definitely know duration.
259 bool duration_known_;
261 // FFmpegURLProtocol implementation and corresponding glue bits.
262 scoped_ptr<BlockingUrlProtocol> url_protocol_;
263 scoped_ptr<FFmpegGlue> glue_;
265 const NeedKeyCB need_key_cb_;
267 DISALLOW_COPY_AND_ASSIGN(FFmpegDemuxer);
270 } // namespace media
272 #endif // MEDIA_FILTERS_FFMPEG_DEMUXER_H_