Update To 11.40.268.0
[platform/framework/web/crosswalk.git] / src / media / filters / ffmpeg_demuxer.h
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.
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 <string>
26 #include <utility>
27 #include <vector>
28
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"
42
43 // FFmpeg forward declarations.
44 struct AVPacket;
45 struct AVRational;
46 struct AVStream;
47
48 namespace media {
49
50 class MediaLog;
51 class FFmpegBitstreamConverter;
52 class FFmpegDemuxer;
53 class FFmpegGlue;
54
55 typedef scoped_ptr<AVPacket, ScopedPtrAVFreePacket> ScopedAVPacket;
56
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;
63
64   // Enqueues the given AVPacket. It is invalid to queue a |packet| after
65   // SetEndOfStream() has been called.
66   void EnqueuePacket(ScopedAVPacket packet);
67
68   // Enters the end of stream state. After delivering remaining queued buffers
69   // only end of stream buffers will be delivered.
70   void SetEndOfStream();
71
72   // Drops queued buffers and clears end of stream state.
73   void FlushBuffers();
74
75   // Empties the queues and ignores any additional calls to Read().
76   void Stop();
77
78   base::TimeDelta duration() const { return duration_; }
79
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;
86   }
87
88   // DemuxerStream implementation.
89   Type type() override;
90   void Read(const ReadCB& read_cb) override;
91   void EnableBitstreamConverter() override;
92   bool SupportsConfigChanges() override;
93   AudioDecoderConfig audio_decoder_config() override;
94   VideoDecoderConfig video_decoder_config() override;
95   VideoRotation video_rotation() override;
96
97   // Returns the range of buffered data in this stream.
98   Ranges<base::TimeDelta> GetBufferedRanges() const;
99
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;
103
104   // Returns true if this stream has capacity for additional data.
105   bool HasAvailableCapacity();
106
107   // Returns the total buffer size FFMpegDemuxerStream is holding onto.
108   size_t MemoryUsage() const;
109
110   TextKind GetTextKind() const;
111
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;
115
116  private:
117   friend class FFmpegDemuxerTest;
118
119   // Runs |read_cb_| if present with the front of |buffer_queue_|, calling
120   // NotifyCapacityAvailable() if capacity is still available.
121   void SatisfyPendingRead();
122
123   // Converts an FFmpeg stream timestamp into a base::TimeDelta.
124   static base::TimeDelta ConvertStreamTimestamp(const AVRational& time_base,
125                                                 int64 timestamp);
126
127   // Resets any currently active bitstream converter.
128   void ResetBitstreamConverter();
129
130   // Create new bitstream converter, destroying active converter if present.
131   void InitBitstreamConverter();
132
133   FFmpegDemuxer* demuxer_;
134   scoped_refptr<base::SingleThreadTaskRunner> task_runner_;
135   AVStream* stream_;
136   AudioDecoderConfig audio_config_;
137   VideoDecoderConfig video_config_;
138   Type type_;
139   base::TimeDelta duration_;
140   bool end_of_stream_;
141   base::TimeDelta last_packet_timestamp_;
142   base::TimeDelta last_packet_duration_;
143   Ranges<base::TimeDelta> buffered_ranges_;
144   VideoRotation video_rotation_;
145
146   DecoderBufferQueue buffer_queue_;
147   ReadCB read_cb_;
148
149 #if defined(USE_PROPRIETARY_CODECS)
150   scoped_ptr<FFmpegBitstreamConverter> bitstream_converter_;
151 #endif
152
153   std::string encryption_key_id_;
154   bool fixup_negative_ogg_timestamps_;
155
156   DISALLOW_COPY_AND_ASSIGN(FFmpegDemuxerStream);
157 };
158
159 class MEDIA_EXPORT FFmpegDemuxer : public Demuxer {
160  public:
161   FFmpegDemuxer(const scoped_refptr<base::SingleThreadTaskRunner>& task_runner,
162                 DataSource* data_source,
163                 const NeedKeyCB& need_key_cb,
164                 const scoped_refptr<MediaLog>& media_log);
165   ~FFmpegDemuxer() override;
166
167   // Demuxer implementation.
168   void Initialize(DemuxerHost* host,
169                   const PipelineStatusCB& status_cb,
170                   bool enable_text_tracks) override;
171   void Stop() override;
172   void Seek(base::TimeDelta time, const PipelineStatusCB& cb) override;
173   base::Time GetTimelineOffset() const override;
174   DemuxerStream* GetStream(DemuxerStream::Type type) override;
175   base::TimeDelta GetStartTime() const override;
176   Liveness GetLiveness() const override;
177
178   // Calls |need_key_cb_| with the initialization data encountered in the file.
179   void FireNeedKey(const std::string& init_data_type,
180                    const std::string& encryption_key_id);
181
182   // Allow FFmpegDemuxerStream to notify us when there is updated information
183   // about capacity and what buffered data is available.
184   void NotifyCapacityAvailable();
185   void NotifyBufferingChanged();
186
187   // The lowest demuxed timestamp.  If negative, DemuxerStreams must use this to
188   // adjust packet timestamps such that external clients see a zero-based
189   // timeline.
190   base::TimeDelta start_time() const { return start_time_; }
191
192  private:
193   // To allow tests access to privates.
194   friend class FFmpegDemuxerTest;
195
196   // FFmpeg callbacks during initialization.
197   void OnOpenContextDone(const PipelineStatusCB& status_cb, bool result);
198   void OnFindStreamInfoDone(const PipelineStatusCB& status_cb, int result);
199
200   // FFmpeg callbacks during seeking.
201   void OnSeekFrameDone(const PipelineStatusCB& cb, int result);
202
203   // FFmpeg callbacks during reading + helper method to initiate reads.
204   void ReadFrameIfNeeded();
205   void OnReadFrameDone(ScopedAVPacket packet, int result);
206
207   // Returns true iff any stream has additional capacity. Note that streams can
208   // go over capacity depending on how the file is muxed.
209   bool StreamsHaveAvailableCapacity();
210
211   // Returns true if the maximum allowed memory usage has been reached.
212   bool IsMaxMemoryUsageReached() const;
213
214   // Signal all FFmpegDemuxerStreams that the stream has ended.
215   void StreamHasEnded();
216
217   // Called by |url_protocol_| whenever |data_source_| returns a read error.
218   void OnDataSourceError();
219
220   // Returns the stream from |streams_| that matches |type| as an
221   // FFmpegDemuxerStream.
222   FFmpegDemuxerStream* GetFFmpegStream(DemuxerStream::Type type) const;
223
224   // Called after the streams have been collected from the media, to allow
225   // the text renderer to bind each text stream to the cue rendering engine.
226   void AddTextStreams();
227
228   DemuxerHost* host_;
229
230   scoped_refptr<base::SingleThreadTaskRunner> task_runner_;
231
232   // Thread on which all blocking FFmpeg operations are executed.
233   base::Thread blocking_thread_;
234
235   // Tracks if there's an outstanding av_read_frame() operation.
236   //
237   // TODO(scherkus): Allow more than one read in flight for higher read
238   // throughput using demuxer_bench to verify improvements.
239   bool pending_read_;
240
241   // Tracks if there's an outstanding av_seek_frame() operation. Used to discard
242   // results of pre-seek av_read_frame() operations.
243   bool pending_seek_;
244
245   // |streams_| mirrors the AVStream array in AVFormatContext. It contains
246   // FFmpegDemuxerStreams encapsluating AVStream objects at the same index.
247   //
248   // Since we only support a single audio and video stream, |streams_| will
249   // contain NULL entries for additional audio/video streams as well as for
250   // stream types that we do not currently support.
251   //
252   // Once initialized, operations on FFmpegDemuxerStreams should be carried out
253   // on the demuxer thread.
254   typedef ScopedVector<FFmpegDemuxerStream> StreamVector;
255   StreamVector streams_;
256
257   // Provides asynchronous IO to this demuxer. Consumed by |url_protocol_| to
258   // integrate with libavformat.
259   DataSource* data_source_;
260
261   scoped_refptr<MediaLog> media_log_;
262
263   // Derived bitrate after initialization has completed.
264   int bitrate_;
265
266   // The first timestamp of the audio or video stream, whichever is lower.  This
267   // is used to adjust timestamps so that external consumers always see a zero
268   // based timeline.
269   base::TimeDelta start_time_;
270
271   // The index and start time of the preferred streams for seeking.  Filled upon
272   // completion of OnFindStreamInfoDone().  Each info entry represents an index
273   // into |streams_| and the start time of that stream.
274   //
275   // Seek() will attempt to use |preferred_stream_for_seeking_| if the seek
276   // point occurs after its associated start time.  Otherwise it will use
277   // |fallback_stream_for_seeking_|.
278   typedef std::pair<int, base::TimeDelta> StreamSeekInfo;
279   StreamSeekInfo preferred_stream_for_seeking_;
280   StreamSeekInfo fallback_stream_for_seeking_;
281
282   // The Time associated with timestamp 0. Set to a null
283   // time if the file doesn't have an association to Time.
284   base::Time timeline_offset_;
285
286   // Liveness of the stream.
287   Liveness liveness_;
288
289   // Whether text streams have been enabled for this demuxer.
290   bool text_enabled_;
291
292   // Set if we know duration of the audio stream. Used when processing end of
293   // stream -- at this moment we definitely know duration.
294   bool duration_known_;
295
296   // FFmpegURLProtocol implementation and corresponding glue bits.
297   scoped_ptr<BlockingUrlProtocol> url_protocol_;
298   scoped_ptr<FFmpegGlue> glue_;
299
300   const NeedKeyCB need_key_cb_;
301
302   // NOTE: Weak pointers must be invalidated before all other member variables.
303   base::WeakPtrFactory<FFmpegDemuxer> weak_factory_;
304
305   DISALLOW_COPY_AND_ASSIGN(FFmpegDemuxer);
306 };
307
308 }  // namespace media
309
310 #endif  // MEDIA_FILTERS_FFMPEG_DEMUXER_H_