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 <vector> | 26 #include <vector> |
27 | 27 |
28 #include "base/callback.h" | 28 #include "base/callback.h" |
29 #include "base/gtest_prod_util.h" | 29 #include "base/gtest_prod_util.h" |
| 30 #include "base/memory/scoped_vector.h" |
30 #include "base/threading/thread.h" | 31 #include "base/threading/thread.h" |
31 #include "media/base/audio_decoder_config.h" | 32 #include "media/base/audio_decoder_config.h" |
32 #include "media/base/decoder_buffer.h" | 33 #include "media/base/decoder_buffer.h" |
33 #include "media/base/decoder_buffer_queue.h" | 34 #include "media/base/decoder_buffer_queue.h" |
34 #include "media/base/demuxer.h" | 35 #include "media/base/demuxer.h" |
35 #include "media/base/pipeline.h" | 36 #include "media/base/pipeline.h" |
36 #include "media/base/video_decoder_config.h" | 37 #include "media/base/video_decoder_config.h" |
37 #include "media/filters/blocking_url_protocol.h" | 38 #include "media/filters/blocking_url_protocol.h" |
38 | 39 |
39 // FFmpeg forward declarations. | 40 // FFmpeg forward declarations. |
(...skipping 16 matching lines...) Expand all Loading... |
56 class FFmpegH264ToAnnexBBitstreamConverter; | 57 class FFmpegH264ToAnnexBBitstreamConverter; |
57 class ScopedPtrAVFreePacket; | 58 class ScopedPtrAVFreePacket; |
58 | 59 |
59 typedef scoped_ptr_malloc<AVPacket, ScopedPtrAVFreePacket> ScopedAVPacket; | 60 typedef scoped_ptr_malloc<AVPacket, ScopedPtrAVFreePacket> ScopedAVPacket; |
60 | 61 |
61 class FFmpegDemuxerStream : public DemuxerStream { | 62 class FFmpegDemuxerStream : public DemuxerStream { |
62 public: | 63 public: |
63 // Keeps a copy of |demuxer| and initializes itself using information | 64 // Keeps a copy of |demuxer| and initializes itself using information |
64 // inside |stream|. Both parameters must outlive |this|. | 65 // inside |stream|. Both parameters must outlive |this|. |
65 FFmpegDemuxerStream(FFmpegDemuxer* demuxer, AVStream* stream); | 66 FFmpegDemuxerStream(FFmpegDemuxer* demuxer, AVStream* stream); |
| 67 virtual ~FFmpegDemuxerStream(); |
66 | 68 |
67 // 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 |
68 // SetEndOfStream() has been called. | 70 // SetEndOfStream() has been called. |
69 void EnqueuePacket(ScopedAVPacket packet); | 71 void EnqueuePacket(ScopedAVPacket packet); |
70 | 72 |
71 // Enters the end of stream state. After delivering remaining queued buffers | 73 // Enters the end of stream state. After delivering remaining queued buffers |
72 // only end of stream buffers will be delivered. | 74 // only end of stream buffers will be delivered. |
73 void SetEndOfStream(); | 75 void SetEndOfStream(); |
74 | 76 |
75 // Drops queued buffers and clears end of stream state. | 77 // Drops queued buffers and clears end of stream state. |
(...skipping 15 matching lines...) Expand all Loading... |
91 // Returns the range of buffered data in this stream. | 93 // Returns the range of buffered data in this stream. |
92 Ranges<base::TimeDelta> GetBufferedRanges() const; | 94 Ranges<base::TimeDelta> GetBufferedRanges() const; |
93 | 95 |
94 // Returns elapsed time based on the already queued packets. | 96 // Returns elapsed time based on the already queued packets. |
95 // Used to determine stream duration when it's not known ahead of time. | 97 // Used to determine stream duration when it's not known ahead of time. |
96 base::TimeDelta GetElapsedTime() const; | 98 base::TimeDelta GetElapsedTime() const; |
97 | 99 |
98 // Returns true if this stream has capacity for additional data. | 100 // Returns true if this stream has capacity for additional data. |
99 bool HasAvailableCapacity(); | 101 bool HasAvailableCapacity(); |
100 | 102 |
101 protected: | |
102 virtual ~FFmpegDemuxerStream(); | |
103 | |
104 private: | 103 private: |
105 friend class FFmpegDemuxerTest; | 104 friend class FFmpegDemuxerTest; |
106 | 105 |
107 // Runs |read_cb_| if present with the front of |buffer_queue_|, calling | 106 // Runs |read_cb_| if present with the front of |buffer_queue_|, calling |
108 // NotifyCapacityAvailable() if capacity is still available. | 107 // NotifyCapacityAvailable() if capacity is still available. |
109 void SatisfyPendingRead(); | 108 void SatisfyPendingRead(); |
110 | 109 |
111 // Converts an FFmpeg stream timestamp into a base::TimeDelta. | 110 // Converts an FFmpeg stream timestamp into a base::TimeDelta. |
112 static base::TimeDelta ConvertStreamTimestamp(const AVRational& time_base, | 111 static base::TimeDelta ConvertStreamTimestamp(const AVRational& time_base, |
113 int64 timestamp); | 112 int64 timestamp); |
(...skipping 19 matching lines...) Expand all Loading... |
133 std::string encryption_key_id_; | 132 std::string encryption_key_id_; |
134 | 133 |
135 DISALLOW_COPY_AND_ASSIGN(FFmpegDemuxerStream); | 134 DISALLOW_COPY_AND_ASSIGN(FFmpegDemuxerStream); |
136 }; | 135 }; |
137 | 136 |
138 class MEDIA_EXPORT FFmpegDemuxer : public Demuxer { | 137 class MEDIA_EXPORT FFmpegDemuxer : public Demuxer { |
139 public: | 138 public: |
140 FFmpegDemuxer(const scoped_refptr<base::MessageLoopProxy>& message_loop, | 139 FFmpegDemuxer(const scoped_refptr<base::MessageLoopProxy>& message_loop, |
141 const scoped_refptr<DataSource>& data_source, | 140 const scoped_refptr<DataSource>& data_source, |
142 const FFmpegNeedKeyCB& need_key_cb); | 141 const FFmpegNeedKeyCB& need_key_cb); |
| 142 virtual ~FFmpegDemuxer(); |
143 | 143 |
144 // Demuxer implementation. | 144 // Demuxer implementation. |
145 virtual void Initialize(DemuxerHost* host, | 145 virtual void Initialize(DemuxerHost* host, |
146 const PipelineStatusCB& status_cb) OVERRIDE; | 146 const PipelineStatusCB& status_cb) OVERRIDE; |
147 virtual void Stop(const base::Closure& callback) OVERRIDE; | 147 virtual void Stop(const base::Closure& callback) OVERRIDE; |
148 virtual void Seek(base::TimeDelta time, const PipelineStatusCB& cb) OVERRIDE; | 148 virtual void Seek(base::TimeDelta time, const PipelineStatusCB& cb) OVERRIDE; |
149 virtual void OnAudioRendererDisabled() OVERRIDE; | 149 virtual void OnAudioRendererDisabled() OVERRIDE; |
150 virtual void SetPlaybackRate(float playback_rate) OVERRIDE; | 150 virtual void SetPlaybackRate(float playback_rate) OVERRIDE; |
151 virtual scoped_refptr<DemuxerStream> GetStream( | 151 virtual DemuxerStream* GetStream(DemuxerStream::Type type) OVERRIDE; |
152 DemuxerStream::Type type) OVERRIDE; | |
153 virtual base::TimeDelta GetStartTime() const OVERRIDE; | 152 virtual base::TimeDelta GetStartTime() const OVERRIDE; |
154 | 153 |
155 // Calls |need_key_cb_| with the initialization data encountered in the file. | 154 // Calls |need_key_cb_| with the initialization data encountered in the file. |
156 void FireNeedKey(const std::string& init_data_type, | 155 void FireNeedKey(const std::string& init_data_type, |
157 const std::string& encryption_key_id); | 156 const std::string& encryption_key_id); |
158 | 157 |
159 // Allow FFmpegDemuxerStream to notify us when there is updated information | 158 // Allow FFmpegDemuxerStream to notify us when there is updated information |
160 // about capacity and what buffered data is available. | 159 // about capacity and what buffered data is available. |
161 void NotifyCapacityAvailable(); | 160 void NotifyCapacityAvailable(); |
162 void NotifyBufferingChanged(); | 161 void NotifyBufferingChanged(); |
163 | 162 |
164 private: | 163 private: |
165 // To allow tests access to privates. | 164 // To allow tests access to privates. |
166 friend class FFmpegDemuxerTest; | 165 friend class FFmpegDemuxerTest; |
167 | 166 |
168 virtual ~FFmpegDemuxer(); | |
169 | |
170 // FFmpeg callbacks during initialization. | 167 // FFmpeg callbacks during initialization. |
171 void OnOpenContextDone(const PipelineStatusCB& status_cb, bool result); | 168 void OnOpenContextDone(const PipelineStatusCB& status_cb, bool result); |
172 void OnFindStreamInfoDone(const PipelineStatusCB& status_cb, int result); | 169 void OnFindStreamInfoDone(const PipelineStatusCB& status_cb, int result); |
173 | 170 |
174 // FFmpeg callbacks during seeking. | 171 // FFmpeg callbacks during seeking. |
175 void OnSeekFrameDone(const PipelineStatusCB& cb, int result); | 172 void OnSeekFrameDone(const PipelineStatusCB& cb, int result); |
176 | 173 |
177 // FFmpeg callbacks during reading + helper method to initiate reads. | 174 // FFmpeg callbacks during reading + helper method to initiate reads. |
178 void ReadFrameIfNeeded(); | 175 void ReadFrameIfNeeded(); |
179 void OnReadFrameDone(ScopedAVPacket packet, int result); | 176 void OnReadFrameDone(ScopedAVPacket packet, int result); |
180 | 177 |
181 // DataSource callbacks during stopping. | 178 // DataSource callbacks during stopping. |
182 void OnDataSourceStopped(const base::Closure& callback); | 179 void OnDataSourceStopped(const base::Closure& callback); |
183 | 180 |
184 // Returns true iff any stream has additional capacity. Note that streams can | 181 // Returns true iff any stream has additional capacity. Note that streams can |
185 // go over capacity depending on how the file is muxed. | 182 // go over capacity depending on how the file is muxed. |
186 bool StreamsHaveAvailableCapacity(); | 183 bool StreamsHaveAvailableCapacity(); |
187 | 184 |
188 // Signal all FFmpegDemuxerStreams that the stream has ended. | 185 // Signal all FFmpegDemuxerStreams that the stream has ended. |
189 void StreamHasEnded(); | 186 void StreamHasEnded(); |
190 | 187 |
191 // Called by |url_protocol_| whenever |data_source_| returns a read error. | 188 // Called by |url_protocol_| whenever |data_source_| returns a read error. |
192 void OnDataSourceError(); | 189 void OnDataSourceError(); |
193 | 190 |
194 // Returns the stream from |streams_| that matches |type| as an | 191 // Returns the stream from |streams_| that matches |type| as an |
195 // FFmpegDemuxerStream. | 192 // FFmpegDemuxerStream. |
196 scoped_refptr<FFmpegDemuxerStream> GetFFmpegStream( | 193 FFmpegDemuxerStream* GetFFmpegStream(DemuxerStream::Type type) const; |
197 DemuxerStream::Type type) const; | |
198 | 194 |
199 DemuxerHost* host_; | 195 DemuxerHost* host_; |
200 | 196 |
201 scoped_refptr<base::MessageLoopProxy> message_loop_; | 197 scoped_refptr<base::MessageLoopProxy> message_loop_; |
| 198 base::WeakPtrFactory<FFmpegDemuxer> weak_factory_; |
| 199 base::WeakPtr<FFmpegDemuxer> weak_this_; |
202 | 200 |
203 // Thread on which all blocking FFmpeg operations are executed. | 201 // Thread on which all blocking FFmpeg operations are executed. |
204 base::Thread blocking_thread_; | 202 base::Thread blocking_thread_; |
205 | 203 |
206 // Tracks if there's an outstanding av_read_frame() operation. | 204 // Tracks if there's an outstanding av_read_frame() operation. |
207 // | 205 // |
208 // TODO(scherkus): Allow more than one read in flight for higher read | 206 // TODO(scherkus): Allow more than one read in flight for higher read |
209 // throughput using demuxer_bench to verify improvements. | 207 // throughput using demuxer_bench to verify improvements. |
210 bool pending_read_; | 208 bool pending_read_; |
211 | 209 |
212 // Tracks if there's an outstanding av_seek_frame() operation. Used to discard | 210 // Tracks if there's an outstanding av_seek_frame() operation. Used to discard |
213 // results of pre-seek av_read_frame() operations. | 211 // results of pre-seek av_read_frame() operations. |
214 bool pending_seek_; | 212 bool pending_seek_; |
215 | 213 |
216 // |streams_| mirrors the AVStream array in |format_context_|. It contains | 214 // |streams_| mirrors the AVStream array in |format_context_|. It contains |
217 // FFmpegDemuxerStreams encapsluating AVStream objects at the same index. | 215 // FFmpegDemuxerStreams encapsluating AVStream objects at the same index. |
218 // | 216 // |
219 // Since we only support a single audio and video stream, |streams_| will | 217 // Since we only support a single audio and video stream, |streams_| will |
220 // contain NULL entries for additional audio/video streams as well as for | 218 // contain NULL entries for additional audio/video streams as well as for |
221 // stream types that we do not currently support. | 219 // stream types that we do not currently support. |
222 // | 220 // |
223 // Once initialized, operations on FFmpegDemuxerStreams should be carried out | 221 // Once initialized, operations on FFmpegDemuxerStreams should be carried out |
224 // on the demuxer thread. | 222 // on the demuxer thread. |
225 typedef std::vector<scoped_refptr<FFmpegDemuxerStream> > StreamVector; | 223 typedef ScopedVector<FFmpegDemuxerStream> StreamVector; |
226 StreamVector streams_; | 224 StreamVector streams_; |
227 | 225 |
228 // Reference to the data source. Asynchronous read requests are submitted to | 226 // Reference to the data source. Asynchronous read requests are submitted to |
229 // this object. | 227 // this object. |
230 scoped_refptr<DataSource> data_source_; | 228 scoped_refptr<DataSource> data_source_; |
231 | 229 |
232 // Derived bitrate after initialization has completed. | 230 // Derived bitrate after initialization has completed. |
233 int bitrate_; | 231 int bitrate_; |
234 | 232 |
235 // The first timestamp of the opened media file. This is used to set the | 233 // The first timestamp of the opened media file. This is used to set the |
(...skipping 14 matching lines...) Expand all Loading... |
250 scoped_ptr<FFmpegGlue> glue_; | 248 scoped_ptr<FFmpegGlue> glue_; |
251 | 249 |
252 const FFmpegNeedKeyCB need_key_cb_; | 250 const FFmpegNeedKeyCB need_key_cb_; |
253 | 251 |
254 DISALLOW_COPY_AND_ASSIGN(FFmpegDemuxer); | 252 DISALLOW_COPY_AND_ASSIGN(FFmpegDemuxer); |
255 }; | 253 }; |
256 | 254 |
257 } // namespace media | 255 } // namespace media |
258 | 256 |
259 #endif // MEDIA_FILTERS_FFMPEG_DEMUXER_H_ | 257 #endif // MEDIA_FILTERS_FFMPEG_DEMUXER_H_ |
OLD | NEW |