OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 // Implements the Demuxer interface using FFmpeg's libavformat. At this time | 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 | 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 | 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 | 8 // key FFmpegCodecID which contains the CodecID enumeration value. The CodecIDs |
9 // can be used to create and initialize the corresponding FFmpeg decoder. | 9 // can be used to create and initialize the corresponding FFmpeg decoder. |
10 // | 10 // |
11 // FFmpegDemuxer sets the duration of pipeline during initialization by using | 11 // FFmpegDemuxer sets the duration of pipeline during initialization by using |
12 // the duration of the longest audio/video stream. | 12 // the duration of the longest audio/video stream. |
13 // | 13 // |
14 // NOTE: since FFmpegDemuxer reads packets sequentially without seeking, media | 14 // NOTE: since FFmpegDemuxer reads packets sequentially without seeking, media |
15 // files with very large drift between audio/video streams may result in | 15 // files with very large drift between audio/video streams may result in |
16 // excessive memory consumption. | 16 // excessive memory consumption. |
17 // | 17 // |
18 // When stopped, FFmpegDemuxer and FFmpegDemuxerStream release all callbacks | 18 // When stopped, FFmpegDemuxer and FFmpegDemuxerStream release all callbacks |
19 // and buffered packets. Reads from a stopped FFmpegDemuxerStream will not be | 19 // and buffered packets. Reads from a stopped FFmpegDemuxerStream will not be |
20 // replied to. | 20 // replied to. |
21 | 21 |
22 #ifndef MEDIA_FILTERS_FFMPEG_DEMUXER_H_ | 22 #ifndef MEDIA_FILTERS_FFMPEG_DEMUXER_H_ |
23 #define MEDIA_FILTERS_FFMPEG_DEMUXER_H_ | 23 #define MEDIA_FILTERS_FFMPEG_DEMUXER_H_ |
24 | 24 |
25 #include <string> | 25 #include <string> |
| 26 #include <utility> |
26 #include <vector> | 27 #include <vector> |
27 | 28 |
28 #include "base/callback.h" | 29 #include "base/callback.h" |
29 #include "base/gtest_prod_util.h" | 30 #include "base/gtest_prod_util.h" |
30 #include "base/memory/scoped_vector.h" | 31 #include "base/memory/scoped_vector.h" |
31 #include "base/threading/thread.h" | 32 #include "base/threading/thread.h" |
32 #include "media/base/audio_decoder_config.h" | 33 #include "media/base/audio_decoder_config.h" |
33 #include "media/base/decoder_buffer.h" | 34 #include "media/base/decoder_buffer.h" |
34 #include "media/base/decoder_buffer_queue.h" | 35 #include "media/base/decoder_buffer_queue.h" |
35 #include "media/base/demuxer.h" | 36 #include "media/base/demuxer.h" |
(...skipping 12 matching lines...) Expand all Loading... |
48 | 49 |
49 class MediaLog; | 50 class MediaLog; |
50 class FFmpegDemuxer; | 51 class FFmpegDemuxer; |
51 class FFmpegGlue; | 52 class FFmpegGlue; |
52 class FFmpegH264ToAnnexBBitstreamConverter; | 53 class FFmpegH264ToAnnexBBitstreamConverter; |
53 | 54 |
54 typedef scoped_ptr<AVPacket, ScopedPtrAVFreePacket> ScopedAVPacket; | 55 typedef scoped_ptr<AVPacket, ScopedPtrAVFreePacket> ScopedAVPacket; |
55 | 56 |
56 class FFmpegDemuxerStream : public DemuxerStream { | 57 class FFmpegDemuxerStream : public DemuxerStream { |
57 public: | 58 public: |
58 // Keeps a copy of |demuxer| and initializes itself using information | 59 // Keeps a copy of |demuxer| and initializes itself using information inside |
59 // inside |stream|. Both parameters must outlive |this|. | 60 // |stream|. Both parameters must outlive |this|. |
60 FFmpegDemuxerStream(FFmpegDemuxer* demuxer, AVStream* stream); | 61 // |discard_negative_timestamps| tells the DemuxerStream that all packets with |
| 62 // negative timestamps should be marked for post-decode discard. All decoded |
| 63 // data before time zero will be discarded. |
| 64 FFmpegDemuxerStream(FFmpegDemuxer* demuxer, |
| 65 AVStream* stream, |
| 66 bool discard_negative_timestamps); |
61 virtual ~FFmpegDemuxerStream(); | 67 virtual ~FFmpegDemuxerStream(); |
62 | 68 |
63 // Enqueues the given AVPacket. It is invalid to queue a |packet| after | 69 // Enqueues the given AVPacket. It is invalid to queue a |packet| after |
64 // SetEndOfStream() has been called. | 70 // SetEndOfStream() has been called. |
65 void EnqueuePacket(ScopedAVPacket packet); | 71 void EnqueuePacket(ScopedAVPacket packet); |
66 | 72 |
67 // Enters the end of stream state. After delivering remaining queued buffers | 73 // Enters the end of stream state. After delivering remaining queued buffers |
68 // only end of stream buffers will be delivered. | 74 // only end of stream buffers will be delivered. |
69 void SetEndOfStream(); | 75 void SetEndOfStream(); |
70 | 76 |
71 // Drops queued buffers and clears end of stream state. | 77 // Drops queued buffers and clears end of stream state. |
72 void FlushBuffers(); | 78 void FlushBuffers(); |
73 | 79 |
74 // Empties the queues and ignores any additional calls to Read(). | 80 // Empties the queues and ignores any additional calls to Read(). |
75 void Stop(); | 81 void Stop(); |
76 | 82 |
77 // Returns the duration of this stream. | 83 base::TimeDelta duration() const { return duration_; } |
78 base::TimeDelta duration(); | |
79 | 84 |
80 // DemuxerStream implementation. | 85 // DemuxerStream implementation. |
81 virtual Type type() OVERRIDE; | 86 virtual Type type() OVERRIDE; |
82 virtual void Read(const ReadCB& read_cb) OVERRIDE; | 87 virtual void Read(const ReadCB& read_cb) OVERRIDE; |
83 virtual void EnableBitstreamConverter() OVERRIDE; | 88 virtual void EnableBitstreamConverter() OVERRIDE; |
84 virtual bool SupportsConfigChanges() OVERRIDE; | 89 virtual bool SupportsConfigChanges() OVERRIDE; |
85 virtual AudioDecoderConfig audio_decoder_config() OVERRIDE; | 90 virtual AudioDecoderConfig audio_decoder_config() OVERRIDE; |
86 virtual VideoDecoderConfig video_decoder_config() OVERRIDE; | 91 virtual VideoDecoderConfig video_decoder_config() OVERRIDE; |
87 | 92 |
88 // Returns the range of buffered data in this stream. | 93 // Returns the range of buffered data in this stream. |
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
129 DecoderBufferQueue buffer_queue_; | 134 DecoderBufferQueue buffer_queue_; |
130 ReadCB read_cb_; | 135 ReadCB read_cb_; |
131 | 136 |
132 #if defined(USE_PROPRIETARY_CODECS) | 137 #if defined(USE_PROPRIETARY_CODECS) |
133 scoped_ptr<FFmpegH264ToAnnexBBitstreamConverter> bitstream_converter_; | 138 scoped_ptr<FFmpegH264ToAnnexBBitstreamConverter> bitstream_converter_; |
134 #endif | 139 #endif |
135 | 140 |
136 bool bitstream_converter_enabled_; | 141 bool bitstream_converter_enabled_; |
137 | 142 |
138 std::string encryption_key_id_; | 143 std::string encryption_key_id_; |
| 144 const bool discard_negative_timestamps_; |
139 | 145 |
140 DISALLOW_COPY_AND_ASSIGN(FFmpegDemuxerStream); | 146 DISALLOW_COPY_AND_ASSIGN(FFmpegDemuxerStream); |
141 }; | 147 }; |
142 | 148 |
143 class MEDIA_EXPORT FFmpegDemuxer : public Demuxer { | 149 class MEDIA_EXPORT FFmpegDemuxer : public Demuxer { |
144 public: | 150 public: |
145 FFmpegDemuxer(const scoped_refptr<base::SingleThreadTaskRunner>& task_runner, | 151 FFmpegDemuxer(const scoped_refptr<base::SingleThreadTaskRunner>& task_runner, |
146 DataSource* data_source, | 152 DataSource* data_source, |
147 const NeedKeyCB& need_key_cb, | 153 const NeedKeyCB& need_key_cb, |
148 const scoped_refptr<MediaLog>& media_log); | 154 const scoped_refptr<MediaLog>& media_log); |
149 virtual ~FFmpegDemuxer(); | 155 virtual ~FFmpegDemuxer(); |
150 | 156 |
151 // Demuxer implementation. | 157 // Demuxer implementation. |
152 virtual void Initialize(DemuxerHost* host, | 158 virtual void Initialize(DemuxerHost* host, |
153 const PipelineStatusCB& status_cb, | 159 const PipelineStatusCB& status_cb, |
154 bool enable_text_tracks) OVERRIDE; | 160 bool enable_text_tracks) OVERRIDE; |
155 virtual void Stop(const base::Closure& callback) OVERRIDE; | 161 virtual void Stop(const base::Closure& callback) OVERRIDE; |
156 virtual void Seek(base::TimeDelta time, const PipelineStatusCB& cb) OVERRIDE; | 162 virtual void Seek(base::TimeDelta time, const PipelineStatusCB& cb) OVERRIDE; |
157 virtual DemuxerStream* GetStream(DemuxerStream::Type type) OVERRIDE; | 163 virtual DemuxerStream* GetStream(DemuxerStream::Type type) OVERRIDE; |
158 virtual base::TimeDelta GetStartTime() const OVERRIDE; | |
159 virtual base::Time GetTimelineOffset() const OVERRIDE; | 164 virtual base::Time GetTimelineOffset() const OVERRIDE; |
160 virtual Liveness GetLiveness() const OVERRIDE; | 165 virtual Liveness GetLiveness() const OVERRIDE; |
161 | 166 |
162 // Calls |need_key_cb_| with the initialization data encountered in the file. | 167 // Calls |need_key_cb_| with the initialization data encountered in the file. |
163 void FireNeedKey(const std::string& init_data_type, | 168 void FireNeedKey(const std::string& init_data_type, |
164 const std::string& encryption_key_id); | 169 const std::string& encryption_key_id); |
165 | 170 |
166 // Allow FFmpegDemuxerStream to notify us when there is updated information | 171 // Allow FFmpegDemuxerStream to notify us when there is updated information |
167 // about capacity and what buffered data is available. | 172 // about capacity and what buffered data is available. |
168 void NotifyCapacityAvailable(); | 173 void NotifyCapacityAvailable(); |
169 void NotifyBufferingChanged(); | 174 void NotifyBufferingChanged(); |
170 | 175 |
| 176 // The lowest demuxed timestamp. DemuxerStream's must use this to adjust |
| 177 // packet timestamps such that external clients see a zero-based timeline. |
| 178 base::TimeDelta start_time() const { return start_time_; } |
| 179 |
171 private: | 180 private: |
172 // To allow tests access to privates. | 181 // To allow tests access to privates. |
173 friend class FFmpegDemuxerTest; | 182 friend class FFmpegDemuxerTest; |
174 | 183 |
175 // FFmpeg callbacks during initialization. | 184 // FFmpeg callbacks during initialization. |
176 void OnOpenContextDone(const PipelineStatusCB& status_cb, bool result); | 185 void OnOpenContextDone(const PipelineStatusCB& status_cb, bool result); |
177 void OnFindStreamInfoDone(const PipelineStatusCB& status_cb, int result); | 186 void OnFindStreamInfoDone(const PipelineStatusCB& status_cb, int result); |
178 | 187 |
179 // FFmpeg callbacks during seeking. | 188 // FFmpeg callbacks during seeking. |
180 void OnSeekFrameDone(const PipelineStatusCB& cb, int result); | 189 void OnSeekFrameDone(const PipelineStatusCB& cb, int result); |
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
217 // Tracks if there's an outstanding av_read_frame() operation. | 226 // Tracks if there's an outstanding av_read_frame() operation. |
218 // | 227 // |
219 // TODO(scherkus): Allow more than one read in flight for higher read | 228 // TODO(scherkus): Allow more than one read in flight for higher read |
220 // throughput using demuxer_bench to verify improvements. | 229 // throughput using demuxer_bench to verify improvements. |
221 bool pending_read_; | 230 bool pending_read_; |
222 | 231 |
223 // Tracks if there's an outstanding av_seek_frame() operation. Used to discard | 232 // Tracks if there's an outstanding av_seek_frame() operation. Used to discard |
224 // results of pre-seek av_read_frame() operations. | 233 // results of pre-seek av_read_frame() operations. |
225 bool pending_seek_; | 234 bool pending_seek_; |
226 | 235 |
227 // |streams_| mirrors the AVStream array in |format_context_|. It contains | 236 // |streams_| mirrors the AVStream array in AVFormatContext. It contains |
228 // FFmpegDemuxerStreams encapsluating AVStream objects at the same index. | 237 // FFmpegDemuxerStreams encapsluating AVStream objects at the same index. |
229 // | 238 // |
230 // Since we only support a single audio and video stream, |streams_| will | 239 // Since we only support a single audio and video stream, |streams_| will |
231 // contain NULL entries for additional audio/video streams as well as for | 240 // contain NULL entries for additional audio/video streams as well as for |
232 // stream types that we do not currently support. | 241 // stream types that we do not currently support. |
233 // | 242 // |
234 // Once initialized, operations on FFmpegDemuxerStreams should be carried out | 243 // Once initialized, operations on FFmpegDemuxerStreams should be carried out |
235 // on the demuxer thread. | 244 // on the demuxer thread. |
236 typedef ScopedVector<FFmpegDemuxerStream> StreamVector; | 245 typedef ScopedVector<FFmpegDemuxerStream> StreamVector; |
237 StreamVector streams_; | 246 StreamVector streams_; |
238 | 247 |
239 // Provides asynchronous IO to this demuxer. Consumed by |url_protocol_| to | 248 // Provides asynchronous IO to this demuxer. Consumed by |url_protocol_| to |
240 // integrate with libavformat. | 249 // integrate with libavformat. |
241 DataSource* data_source_; | 250 DataSource* data_source_; |
242 | 251 |
243 scoped_refptr<MediaLog> media_log_; | 252 scoped_refptr<MediaLog> media_log_; |
244 | 253 |
245 // Derived bitrate after initialization has completed. | 254 // Derived bitrate after initialization has completed. |
246 int bitrate_; | 255 int bitrate_; |
247 | 256 |
248 // The first timestamp of the opened media file. This is used to set the | 257 // The first timestamp of the audio or video stream, whichever is lower. This |
249 // starting clock value to match the timestamps in the media file. Default | 258 // is used to adjust timestamps so that external consumers always see a zero |
250 // is 0. | 259 // based timeline. |
251 base::TimeDelta start_time_; | 260 base::TimeDelta start_time_; |
252 | 261 |
| 262 // The index and start time of the preferred streams for seeking. Filled upon |
| 263 // completion of OnFindStreamInfoDone(). Each info entry represents an index |
| 264 // into |streams_| and the start time of that stream. |
| 265 // |
| 266 // Seek() will attempt to use |preferred_stream_for_seeking_| if the seek |
| 267 // point occurs after its associated start time. Otherwise it will use |
| 268 // |fallback_stream_for_seeking_|. |
| 269 typedef std::pair<int, base::TimeDelta> StreamSeekInfo; |
| 270 StreamSeekInfo preferred_stream_for_seeking_; |
| 271 StreamSeekInfo fallback_stream_for_seeking_; |
| 272 |
253 // The Time associated with timestamp 0. Set to a null | 273 // The Time associated with timestamp 0. Set to a null |
254 // time if the file doesn't have an association to Time. | 274 // time if the file doesn't have an association to Time. |
255 base::Time timeline_offset_; | 275 base::Time timeline_offset_; |
256 | 276 |
257 // Liveness of the stream. | 277 // Liveness of the stream. |
258 Liveness liveness_; | 278 Liveness liveness_; |
259 | 279 |
260 // Whether text streams have been enabled for this demuxer. | 280 // Whether text streams have been enabled for this demuxer. |
261 bool text_enabled_; | 281 bool text_enabled_; |
262 | 282 |
263 // Set if we know duration of the audio stream. Used when processing end of | 283 // Set if we know duration of the audio stream. Used when processing end of |
264 // stream -- at this moment we definitely know duration. | 284 // stream -- at this moment we definitely know duration. |
265 bool duration_known_; | 285 bool duration_known_; |
266 | 286 |
267 // FFmpegURLProtocol implementation and corresponding glue bits. | 287 // FFmpegURLProtocol implementation and corresponding glue bits. |
268 scoped_ptr<BlockingUrlProtocol> url_protocol_; | 288 scoped_ptr<BlockingUrlProtocol> url_protocol_; |
269 scoped_ptr<FFmpegGlue> glue_; | 289 scoped_ptr<FFmpegGlue> glue_; |
270 | 290 |
271 const NeedKeyCB need_key_cb_; | 291 const NeedKeyCB need_key_cb_; |
272 | 292 |
273 // NOTE: Weak pointers must be invalidated before all other member variables. | 293 // NOTE: Weak pointers must be invalidated before all other member variables. |
274 base::WeakPtrFactory<FFmpegDemuxer> weak_factory_; | 294 base::WeakPtrFactory<FFmpegDemuxer> weak_factory_; |
275 | 295 |
276 DISALLOW_COPY_AND_ASSIGN(FFmpegDemuxer); | 296 DISALLOW_COPY_AND_ASSIGN(FFmpegDemuxer); |
277 }; | 297 }; |
278 | 298 |
279 } // namespace media | 299 } // namespace media |
280 | 300 |
281 #endif // MEDIA_FILTERS_FFMPEG_DEMUXER_H_ | 301 #endif // MEDIA_FILTERS_FFMPEG_DEMUXER_H_ |
OLD | NEW |