1 // Copyright 2012 The Chromium Authors
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_
33 #include "base/callback.h"
34 #include "base/memory/raw_ptr.h"
35 #include "base/memory/weak_ptr.h"
36 #include "base/task/sequenced_task_runner.h"
37 #include "base/time/time.h"
38 #include "media/base/audio_decoder_config.h"
39 #include "media/base/decoder_buffer.h"
40 #include "media/base/decoder_buffer_queue.h"
41 #include "media/base/demuxer.h"
42 #include "media/base/media_log.h"
43 #include "media/base/pipeline_status.h"
44 #include "media/base/text_track_config.h"
45 #include "media/base/timestamp_constants.h"
46 #include "media/base/video_decoder_config.h"
47 #include "media/ffmpeg/scoped_av_packet.h"
48 #include "media/filters/blocking_url_protocol.h"
49 #include "media/media_buildflags.h"
51 // FFmpeg forward declarations.
52 struct AVFormatContext;
59 class FFmpegBitstreamConverter;
63 class MEDIA_EXPORT FFmpegDemuxerStream : public DemuxerStream {
65 // Attempts to create FFmpegDemuxerStream form the given AVStream. Will return
66 // null if the AVStream cannot be translated into a valid decoder config.
68 // FFmpegDemuxerStream keeps a copy of |demuxer| and initializes itself using
69 // information inside |stream|. Both parameters must outlive |this|.
70 static std::unique_ptr<FFmpegDemuxerStream> Create(FFmpegDemuxer* demuxer,
74 FFmpegDemuxerStream(const FFmpegDemuxerStream&) = delete;
75 FFmpegDemuxerStream& operator=(const FFmpegDemuxerStream&) = delete;
77 ~FFmpegDemuxerStream() override;
79 // Enqueues the given AVPacket. It is invalid to queue a |packet| after
80 // SetEndOfStream() has been called.
81 void EnqueuePacket(ScopedAVPacket packet);
83 // Enters the end of stream state. After delivering remaining queued buffers
84 // only end of stream buffers will be delivered.
85 void SetEndOfStream();
87 // Drops queued buffers and clears end of stream state.
88 // Passing |preserve_packet_position| will prevent replay of already seen
90 void FlushBuffers(bool preserve_packet_position);
92 // Empties the queues and ignores any additional calls to Read().
95 // Aborts any pending reads.
98 base::TimeDelta duration() const { return duration_; }
100 // Enables fixes for files with negative timestamps. Normally all timestamps
101 // are rebased against FFmpegDemuxer::start_time() whenever that value is
102 // negative. When this fix is enabled, only AUDIO stream packets will be
103 // rebased to time zero, all other stream types will use the muxed timestamp.
105 // Further, when no codec delay is present, all AUDIO packets which originally
106 // had negative timestamps will be marked for post-decode discard. When codec
107 // delay is present, it is assumed the decoder will handle discard and does
108 // not need the AUDIO packets to be marked for discard; just rebased to zero.
109 void enable_negative_timestamp_fixups() {
110 fixup_negative_timestamps_ = true;
112 void enable_chained_ogg_fixups() { fixup_chained_ogg_ = true; }
114 // DemuxerStream implementation.
115 Type type() const override;
116 StreamLiveness liveness() const override;
117 void Read(ReadCB read_cb) override;
118 void EnableBitstreamConverter() override;
119 bool SupportsConfigChanges() override;
120 AudioDecoderConfig audio_decoder_config() override;
121 VideoDecoderConfig video_decoder_config() override;
123 bool IsEnabled() const;
124 void SetEnabled(bool enabled, base::TimeDelta timestamp);
126 void SetLiveness(StreamLiveness liveness);
128 // Returns the range of buffered data in this stream.
129 Ranges<base::TimeDelta> GetBufferedRanges() const;
131 // Returns true if this stream has capacity for additional data.
132 bool HasAvailableCapacity();
134 // Returns the total buffer size FFMpegDemuxerStream is holding onto.
135 size_t MemoryUsage() const;
137 TextKind GetTextKind() const;
139 // Returns the value associated with |key| in the metadata for the avstream.
140 // Returns an empty string if the key is not present.
141 std::string GetMetadata(const char* key) const;
143 AVStream* av_stream() const { return stream_; }
145 base::TimeDelta start_time() const { return start_time_; }
146 void set_start_time(base::TimeDelta time) { start_time_ = time; }
149 friend class FFmpegDemuxerTest;
151 // Use FFmpegDemuxerStream::Create to construct.
152 // Audio/Video streams must include their respective DecoderConfig. At most
153 // one DecoderConfig should be provided (leaving the other nullptr). Both
154 // configs should be null for text streams.
155 FFmpegDemuxerStream(FFmpegDemuxer* demuxer,
157 std::unique_ptr<AudioDecoderConfig> audio_config,
158 std::unique_ptr<VideoDecoderConfig> video_config,
159 MediaLog* media_log);
161 // Runs |read_cb_| if present with the front of |buffer_queue_|, calling
162 // NotifyCapacityAvailable() if capacity is still available.
163 void SatisfyPendingRead();
165 // Converts an FFmpeg stream timestamp into a base::TimeDelta.
166 static base::TimeDelta ConvertStreamTimestamp(const AVRational& time_base,
169 // Resets any currently active bitstream converter.
170 void ResetBitstreamConverter();
172 // Create new bitstream converter, destroying active converter if present.
173 void InitBitstreamConverter();
175 raw_ptr<FFmpegDemuxer> demuxer_;
176 scoped_refptr<base::SequencedTaskRunner> task_runner_;
177 raw_ptr<AVStream> stream_;
178 base::TimeDelta start_time_;
179 std::unique_ptr<AudioDecoderConfig> audio_config_;
180 std::unique_ptr<VideoDecoderConfig> video_config_;
181 raw_ptr<MediaLog> media_log_;
182 Type type_ = UNKNOWN;
183 StreamLiveness liveness_ = StreamLiveness::kUnknown;
184 base::TimeDelta duration_;
186 base::TimeDelta last_packet_timestamp_;
187 base::TimeDelta last_packet_duration_;
188 Ranges<base::TimeDelta> buffered_ranges_;
190 bool waiting_for_keyframe_;
193 DecoderBufferQueue buffer_queue_;
196 #if BUILDFLAG(USE_PROPRIETARY_CODECS)
197 std::unique_ptr<FFmpegBitstreamConverter> bitstream_converter_;
200 std::string encryption_key_id_;
201 bool fixup_negative_timestamps_;
202 bool fixup_chained_ogg_;
204 int num_discarded_packet_warnings_;
205 int64_t last_packet_pos_;
206 int64_t last_packet_dts_;
209 class MEDIA_EXPORT FFmpegDemuxer : public Demuxer {
211 FFmpegDemuxer(const scoped_refptr<base::SequencedTaskRunner>& task_runner,
212 DataSource* data_source,
213 const EncryptedMediaInitDataCB& encrypted_media_init_data_cb,
214 MediaTracksUpdatedCB media_tracks_updated_cb,
218 FFmpegDemuxer(const FFmpegDemuxer&) = delete;
219 FFmpegDemuxer& operator=(const FFmpegDemuxer&) = delete;
221 ~FFmpegDemuxer() override;
223 // Demuxer implementation.
224 std::string GetDisplayName() const override;
225 void Initialize(DemuxerHost* host, PipelineStatusCallback init_cb) override;
226 void AbortPendingReads() override;
227 void Stop() override;
228 void StartWaitingForSeek(base::TimeDelta seek_time) override;
229 void CancelPendingSeek(base::TimeDelta seek_time) override;
230 void Seek(base::TimeDelta time, PipelineStatusCallback cb) override;
231 base::Time GetTimelineOffset() const override;
232 std::vector<DemuxerStream*> GetAllStreams() override;
233 base::TimeDelta GetStartTime() const override;
234 int64_t GetMemoryUsage() const override;
235 absl::optional<container_names::MediaContainerName> GetContainerForMetrics()
238 // Calls |encrypted_media_init_data_cb_| with the initialization data
239 // encountered in the file.
240 void OnEncryptedMediaInitData(EmeInitDataType init_data_type,
241 const std::string& encryption_key_id);
243 // Allow FFmpegDemuxerStream to notify us when there is updated information
244 // about capacity and what buffered data is available.
245 void NotifyCapacityAvailable();
246 void NotifyBufferingChanged();
248 // Allow FFmpegDemxuerStream to notify us about an error.
249 void NotifyDemuxerError(PipelineStatus error);
251 void OnEnabledAudioTracksChanged(const std::vector<MediaTrack::Id>& track_ids,
252 base::TimeDelta curr_time,
253 TrackChangeCB change_completed_cb) override;
255 void OnSelectedVideoTrackChanged(const std::vector<MediaTrack::Id>& track_ids,
256 base::TimeDelta curr_time,
257 TrackChangeCB change_completed_cb) override;
259 // The lowest demuxed timestamp. If negative, DemuxerStreams must use this to
260 // adjust packet timestamps such that external clients see a zero-based
262 base::TimeDelta start_time() const { return start_time_; }
264 // Task runner used to execute blocking FFmpeg operations.
265 scoped_refptr<base::SequencedTaskRunner> ffmpeg_task_runner() {
266 return blocking_task_runner_;
269 container_names::MediaContainerName container() const {
270 return glue_ ? glue_->container() : container_names::CONTAINER_UNKNOWN;
274 // To allow tests access to privates.
275 friend class FFmpegDemuxerTest;
277 // Helper for vide and audio track changing.
278 void FindAndEnableProperTracks(const std::vector<MediaTrack::Id>& track_ids,
279 base::TimeDelta curr_time,
280 DemuxerStream::Type track_type,
281 TrackChangeCB change_completed_cb);
283 // FFmpeg callbacks during initialization.
284 void OnOpenContextDone(bool result);
285 void OnFindStreamInfoDone(int result);
287 void LogMetadata(AVFormatContext* avctx, base::TimeDelta max_duration);
289 // Finds the stream with the lowest known start time (i.e. not kNoTimestamp
290 // start time) with enabled status matching |enabled|.
291 FFmpegDemuxerStream* FindStreamWithLowestStartTimestamp(bool enabled);
293 // Finds a preferred stream for seeking to |seek_time|. Preference is
294 // typically given to video streams, unless the |seek_time| is earlier than
295 // the start time of the video stream. In that case a stream with the earliest
296 // start time is preferred. Disabled streams are considered only as the last
298 FFmpegDemuxerStream* FindPreferredStreamForSeeking(base::TimeDelta seek_time);
300 // FFmpeg callbacks during seeking.
301 void OnSeekFrameSuccess();
303 // FFmpeg callbacks during reading + helper method to initiate reads.
304 void ReadFrameIfNeeded();
305 void OnReadFrameDone(ScopedAVPacket packet, int result);
307 // Returns true iff any stream has additional capacity. Note that streams can
308 // go over capacity depending on how the file is muxed.
309 bool StreamsHaveAvailableCapacity();
311 // Returns true if the maximum allowed memory usage has been reached.
312 bool IsMaxMemoryUsageReached() const;
314 // Signal all FFmpegDemuxerStreams that the stream has ended.
315 void StreamHasEnded();
317 // Called by |url_protocol_| whenever |data_source_| returns a read error.
318 void OnDataSourceError();
320 // Returns the first stream from |streams_| that matches |type| as an
321 // FFmpegDemuxerStream and is enabled.
322 FFmpegDemuxerStream* GetFirstEnabledFFmpegStream(
323 DemuxerStream::Type type) const;
325 // Called after the streams have been collected from the media, to allow
326 // the text renderer to bind each text stream to the cue rendering engine.
327 void AddTextStreams();
329 void SetLiveness(StreamLiveness liveness);
331 void SeekInternal(base::TimeDelta time, base::OnceClosure seek_cb);
332 void OnVideoSeekedForTrackChange(DemuxerStream* video_stream,
333 base::OnceClosure seek_completed_cb);
334 void SeekOnVideoTrackChange(base::TimeDelta seek_to_time,
335 TrackChangeCB seek_completed_cb,
336 DemuxerStream::Type stream_type,
337 const std::vector<DemuxerStream*>& streams);
339 // Executes |init_cb_| with |status| and closes out the async trace.
340 void RunInitCB(PipelineStatus status);
342 // Executes |pending_seek_cb_| with |status| and closes out the async trace.
343 void RunPendingSeekCB(PipelineStatus status);
345 raw_ptr<DemuxerHost> host_ = nullptr;
347 scoped_refptr<base::SequencedTaskRunner> task_runner_;
349 // Task runner on which all blocking FFmpeg operations are executed; retrieved
350 // from base::ThreadPoolInstance.
351 scoped_refptr<base::SequencedTaskRunner> blocking_task_runner_;
353 PipelineStatusCallback init_cb_;
355 // Indicates if Stop() has been called.
356 bool stopped_ = false;
358 // Tracks if there's an outstanding av_read_frame() operation.
360 // TODO(scherkus): Allow more than one read in flight for higher read
361 // throughput using demuxer_bench to verify improvements.
362 bool pending_read_ = false;
364 // Tracks if there's an outstanding av_seek_frame() operation. Used to discard
365 // results of pre-seek av_read_frame() operations.
366 PipelineStatusCallback pending_seek_cb_;
368 // |streams_| mirrors the AVStream array in AVFormatContext. It contains
369 // FFmpegDemuxerStreams encapsluating AVStream objects at the same index.
371 // Since we only support a single audio and video stream, |streams_| will
372 // contain NULL entries for additional audio/video streams as well as for
373 // stream types that we do not currently support.
375 // Once initialized, operations on FFmpegDemuxerStreams should be carried out
376 // on the demuxer thread.
377 using StreamVector = std::vector<std::unique_ptr<FFmpegDemuxerStream>>;
378 StreamVector streams_;
380 // Provides asynchronous IO to this demuxer. Consumed by |url_protocol_| to
381 // integrate with libavformat.
382 raw_ptr<DataSource> data_source_;
384 raw_ptr<MediaLog> media_log_;
386 // Derived bitrate after initialization has completed.
389 // The first timestamp of the audio or video stream, whichever is lower. This
390 // is used to adjust timestamps so that external consumers always see a zero
392 base::TimeDelta start_time_ = kNoTimestamp;
394 // The Time associated with timestamp 0. Set to a null
395 // time if the file doesn't have an association to Time.
396 base::Time timeline_offset_;
398 // Set if we know duration of the audio stream. Used when processing end of
399 // stream -- at this moment we definitely know duration.
400 bool duration_known_ = false;
401 base::TimeDelta duration_;
403 // FFmpegURLProtocol implementation and corresponding glue bits.
404 std::unique_ptr<BlockingUrlProtocol> url_protocol_;
405 std::unique_ptr<FFmpegGlue> glue_;
407 const EncryptedMediaInitDataCB encrypted_media_init_data_cb_;
409 const MediaTracksUpdatedCB media_tracks_updated_cb_;
411 std::map<MediaTrack::Id, FFmpegDemuxerStream*> track_id_to_demux_stream_map_;
413 const bool is_local_file_;
415 // NOTE: Weak pointers must be invalidated before all other member variables.
416 base::WeakPtr<FFmpegDemuxer> weak_this_;
417 base::WeakPtrFactory<FFmpegDemuxer> cancel_pending_seek_factory_{this};
418 base::WeakPtrFactory<FFmpegDemuxer> weak_factory_{this};
423 #endif // MEDIA_FILTERS_FFMPEG_DEMUXER_H_