Upload upstream chromium 108.0.5359.1
[platform/framework/web/chromium-efl.git] / media / filters / ffmpeg_demuxer.h
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.
4
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.
10 //
11 // FFmpegDemuxer sets the duration of pipeline during initialization by using
12 // the duration of the longest audio/video stream.
13 //
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.
17 //
18 // When stopped, FFmpegDemuxer and FFmpegDemuxerStream release all callbacks
19 // and buffered packets.  Reads from a stopped FFmpegDemuxerStream will not be
20 // replied to.
21
22 #ifndef MEDIA_FILTERS_FFMPEG_DEMUXER_H_
23 #define MEDIA_FILTERS_FFMPEG_DEMUXER_H_
24
25 #include <stddef.h>
26 #include <stdint.h>
27
28 #include <memory>
29 #include <string>
30 #include <utility>
31 #include <vector>
32
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"
50
51 // FFmpeg forward declarations.
52 struct AVFormatContext;
53 struct AVRational;
54 struct AVStream;
55
56 namespace media {
57
58 class MediaLog;
59 class FFmpegBitstreamConverter;
60 class FFmpegDemuxer;
61 class FFmpegGlue;
62
63 class MEDIA_EXPORT FFmpegDemuxerStream : public DemuxerStream {
64  public:
65   // Attempts to create FFmpegDemuxerStream form the given AVStream. Will return
66   // null if the AVStream cannot be translated into a valid decoder config.
67   //
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,
71                                                      AVStream* stream,
72                                                      MediaLog* media_log);
73
74   FFmpegDemuxerStream(const FFmpegDemuxerStream&) = delete;
75   FFmpegDemuxerStream& operator=(const FFmpegDemuxerStream&) = delete;
76
77   ~FFmpegDemuxerStream() override;
78
79   // Enqueues the given AVPacket. It is invalid to queue a |packet| after
80   // SetEndOfStream() has been called.
81   void EnqueuePacket(ScopedAVPacket packet);
82
83   // Enters the end of stream state. After delivering remaining queued buffers
84   // only end of stream buffers will be delivered.
85   void SetEndOfStream();
86
87   // Drops queued buffers and clears end of stream state.
88   // Passing |preserve_packet_position| will prevent replay of already seen
89   // packets.
90   void FlushBuffers(bool preserve_packet_position);
91
92   // Empties the queues and ignores any additional calls to Read().
93   void Stop();
94
95   // Aborts any pending reads.
96   void Abort();
97
98   base::TimeDelta duration() const { return duration_; }
99
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.
104   //
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;
111   }
112   void enable_chained_ogg_fixups() { fixup_chained_ogg_ = true; }
113
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;
122
123   bool IsEnabled() const;
124   void SetEnabled(bool enabled, base::TimeDelta timestamp);
125
126   void SetLiveness(StreamLiveness liveness);
127
128   // Returns the range of buffered data in this stream.
129   Ranges<base::TimeDelta> GetBufferedRanges() const;
130
131   // Returns true if this stream has capacity for additional data.
132   bool HasAvailableCapacity();
133
134   // Returns the total buffer size FFMpegDemuxerStream is holding onto.
135   size_t MemoryUsage() const;
136
137   TextKind GetTextKind() const;
138
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;
142
143   AVStream* av_stream() const { return stream_; }
144
145   base::TimeDelta start_time() const { return start_time_; }
146   void set_start_time(base::TimeDelta time) { start_time_ = time; }
147
148  private:
149   friend class FFmpegDemuxerTest;
150
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,
156                       AVStream* stream,
157                       std::unique_ptr<AudioDecoderConfig> audio_config,
158                       std::unique_ptr<VideoDecoderConfig> video_config,
159                       MediaLog* media_log);
160
161   // Runs |read_cb_| if present with the front of |buffer_queue_|, calling
162   // NotifyCapacityAvailable() if capacity is still available.
163   void SatisfyPendingRead();
164
165   // Converts an FFmpeg stream timestamp into a base::TimeDelta.
166   static base::TimeDelta ConvertStreamTimestamp(const AVRational& time_base,
167                                                 int64_t timestamp);
168
169   // Resets any currently active bitstream converter.
170   void ResetBitstreamConverter();
171
172   // Create new bitstream converter, destroying active converter if present.
173   void InitBitstreamConverter();
174
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_;
185   bool end_of_stream_;
186   base::TimeDelta last_packet_timestamp_;
187   base::TimeDelta last_packet_duration_;
188   Ranges<base::TimeDelta> buffered_ranges_;
189   bool is_enabled_;
190   bool waiting_for_keyframe_;
191   bool aborted_;
192
193   DecoderBufferQueue buffer_queue_;
194   ReadCB read_cb_;
195
196 #if BUILDFLAG(USE_PROPRIETARY_CODECS)
197   std::unique_ptr<FFmpegBitstreamConverter> bitstream_converter_;
198 #endif
199
200   std::string encryption_key_id_;
201   bool fixup_negative_timestamps_;
202   bool fixup_chained_ogg_;
203
204   int num_discarded_packet_warnings_;
205   int64_t last_packet_pos_;
206   int64_t last_packet_dts_;
207 };
208
209 class MEDIA_EXPORT FFmpegDemuxer : public Demuxer {
210  public:
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,
215                 MediaLog* media_log,
216                 bool is_local_file);
217
218   FFmpegDemuxer(const FFmpegDemuxer&) = delete;
219   FFmpegDemuxer& operator=(const FFmpegDemuxer&) = delete;
220
221   ~FFmpegDemuxer() override;
222
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()
236       const override;
237
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);
242
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();
247
248   // Allow FFmpegDemxuerStream to notify us about an error.
249   void NotifyDemuxerError(PipelineStatus error);
250
251   void OnEnabledAudioTracksChanged(const std::vector<MediaTrack::Id>& track_ids,
252                                    base::TimeDelta curr_time,
253                                    TrackChangeCB change_completed_cb) override;
254
255   void OnSelectedVideoTrackChanged(const std::vector<MediaTrack::Id>& track_ids,
256                                    base::TimeDelta curr_time,
257                                    TrackChangeCB change_completed_cb) override;
258
259   // The lowest demuxed timestamp.  If negative, DemuxerStreams must use this to
260   // adjust packet timestamps such that external clients see a zero-based
261   // timeline.
262   base::TimeDelta start_time() const { return start_time_; }
263
264   // Task runner used to execute blocking FFmpeg operations.
265   scoped_refptr<base::SequencedTaskRunner> ffmpeg_task_runner() {
266     return blocking_task_runner_;
267   }
268
269   container_names::MediaContainerName container() const {
270     return glue_ ? glue_->container() : container_names::CONTAINER_UNKNOWN;
271   }
272
273  private:
274   // To allow tests access to privates.
275   friend class FFmpegDemuxerTest;
276
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);
282
283   // FFmpeg callbacks during initialization.
284   void OnOpenContextDone(bool result);
285   void OnFindStreamInfoDone(int result);
286
287   void LogMetadata(AVFormatContext* avctx, base::TimeDelta max_duration);
288
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);
292
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
297   // fallback option.
298   FFmpegDemuxerStream* FindPreferredStreamForSeeking(base::TimeDelta seek_time);
299
300   // FFmpeg callbacks during seeking.
301   void OnSeekFrameSuccess();
302
303   // FFmpeg callbacks during reading + helper method to initiate reads.
304   void ReadFrameIfNeeded();
305   void OnReadFrameDone(ScopedAVPacket packet, int result);
306
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();
310
311   // Returns true if the maximum allowed memory usage has been reached.
312   bool IsMaxMemoryUsageReached() const;
313
314   // Signal all FFmpegDemuxerStreams that the stream has ended.
315   void StreamHasEnded();
316
317   // Called by |url_protocol_| whenever |data_source_| returns a read error.
318   void OnDataSourceError();
319
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;
324
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();
328
329   void SetLiveness(StreamLiveness liveness);
330
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);
338
339   // Executes |init_cb_| with |status| and closes out the async trace.
340   void RunInitCB(PipelineStatus status);
341
342   // Executes |pending_seek_cb_| with |status| and closes out the async trace.
343   void RunPendingSeekCB(PipelineStatus status);
344
345   raw_ptr<DemuxerHost> host_ = nullptr;
346
347   scoped_refptr<base::SequencedTaskRunner> task_runner_;
348
349   // Task runner on which all blocking FFmpeg operations are executed; retrieved
350   // from base::ThreadPoolInstance.
351   scoped_refptr<base::SequencedTaskRunner> blocking_task_runner_;
352
353   PipelineStatusCallback init_cb_;
354
355   // Indicates if Stop() has been called.
356   bool stopped_ = false;
357
358   // Tracks if there's an outstanding av_read_frame() operation.
359   //
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;
363
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_;
367
368   // |streams_| mirrors the AVStream array in AVFormatContext. It contains
369   // FFmpegDemuxerStreams encapsluating AVStream objects at the same index.
370   //
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.
374   //
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_;
379
380   // Provides asynchronous IO to this demuxer. Consumed by |url_protocol_| to
381   // integrate with libavformat.
382   raw_ptr<DataSource> data_source_;
383
384   raw_ptr<MediaLog> media_log_;
385
386   // Derived bitrate after initialization has completed.
387   int bitrate_ = 0;
388
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
391   // based timeline.
392   base::TimeDelta start_time_ = kNoTimestamp;
393
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_;
397
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_;
402
403   // FFmpegURLProtocol implementation and corresponding glue bits.
404   std::unique_ptr<BlockingUrlProtocol> url_protocol_;
405   std::unique_ptr<FFmpegGlue> glue_;
406
407   const EncryptedMediaInitDataCB encrypted_media_init_data_cb_;
408
409   const MediaTracksUpdatedCB media_tracks_updated_cb_;
410
411   std::map<MediaTrack::Id, FFmpegDemuxerStream*> track_id_to_demux_stream_map_;
412
413   const bool is_local_file_;
414
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};
419 };
420
421 }  // namespace media
422
423 #endif  // MEDIA_FILTERS_FFMPEG_DEMUXER_H_