OLD | NEW |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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 #ifndef MEDIA_BASE_ANDROID_MEDIA_CODEC_DECODER_H_ | 5 #ifndef MEDIA_BASE_ANDROID_MEDIA_CODEC_DECODER_H_ |
6 #define MEDIA_BASE_ANDROID_MEDIA_CODEC_DECODER_H_ | 6 #define MEDIA_BASE_ANDROID_MEDIA_CODEC_DECODER_H_ |
7 | 7 |
8 #include "base/android/scoped_java_ref.h" | 8 #include "base/android/scoped_java_ref.h" |
9 #include "base/callback.h" | 9 #include "base/callback.h" |
10 #include "base/macros.h" | 10 #include "base/macros.h" |
(...skipping 127 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
138 // called on this thread, and callbacks are delivered on this thread. | 138 // called on this thread, and callbacks are delivered on this thread. |
139 // The MediaCodecPlayer uses a dedicated (Media) thread for this. | 139 // The MediaCodecPlayer uses a dedicated (Media) thread for this. |
140 // external_request_data_cb: | 140 // external_request_data_cb: |
141 // Called periodically as the amount of internally stored data decreases. | 141 // Called periodically as the amount of internally stored data decreases. |
142 // The receiver should call OnDemuxerDataAvailable() with more data. | 142 // The receiver should call OnDemuxerDataAvailable() with more data. |
143 // starvation_cb: | 143 // starvation_cb: |
144 // Called when starvation is detected. The decoder state does not change. | 144 // Called when starvation is detected. The decoder state does not change. |
145 // The player is supposed to stop and then prefetch the decoder. | 145 // The player is supposed to stop and then prefetch the decoder. |
146 // stop_done_cb: | 146 // stop_done_cb: |
147 // Called when async stop request is completed. | 147 // Called when async stop request is completed. |
| 148 // decoder_drained_cb: |
| 149 // Called when decoder is drained for reconfiguration. |
148 // error_cb: | 150 // error_cb: |
149 // Called when a MediaCodec error occurred. If this happens, a player has | 151 // Called when a MediaCodec error occurred. If this happens, a player has |
150 // to either call ReleaseDecoderResources() or destroy the decoder object. | 152 // to either call ReleaseDecoderResources() or destroy the decoder object. |
151 // decoder_thread_name: | 153 // decoder_thread_name: |
152 // The thread name to be passed to decoder thread constructor. | 154 // The thread name to be passed to decoder thread constructor. |
153 MediaCodecDecoder( | 155 MediaCodecDecoder( |
154 const scoped_refptr<base::SingleThreadTaskRunner>& media_task_runner, | 156 const scoped_refptr<base::SingleThreadTaskRunner>& media_task_runner, |
155 const base::Closure& external_request_data_cb, | 157 const base::Closure& external_request_data_cb, |
156 const base::Closure& starvation_cb, | 158 const base::Closure& starvation_cb, |
| 159 const base::Closure& decoder_drained_cb, |
157 const base::Closure& stop_done_cb, | 160 const base::Closure& stop_done_cb, |
158 const base::Closure& error_cb, | 161 const base::Closure& error_cb, |
159 const char* decoder_thread_name); | 162 const char* decoder_thread_name); |
160 virtual ~MediaCodecDecoder(); | 163 virtual ~MediaCodecDecoder(); |
161 | 164 |
162 virtual const char* class_name() const; | 165 virtual const char* class_name() const; |
163 | 166 |
164 // MediaCodecDecoder exists through the whole lifetime of the player | 167 // MediaCodecDecoder exists through the whole lifetime of the player |
165 // to support dynamic addition and removal of the streams. | 168 // to support dynamic addition and removal of the streams. |
166 // This method returns true if the current stream (audio or video) | 169 // This method returns true if the current stream (audio or video) |
(...skipping 12 matching lines...) Expand all Loading... |
179 | 182 |
180 // Releases MediaCodecBridge and any related buffers or references. | 183 // Releases MediaCodecBridge and any related buffers or references. |
181 virtual void ReleaseMediaCodec(); | 184 virtual void ReleaseMediaCodec(); |
182 | 185 |
183 // Returns corresponding conditions. | 186 // Returns corresponding conditions. |
184 bool IsPrefetchingOrPlaying() const; | 187 bool IsPrefetchingOrPlaying() const; |
185 bool IsStopped() const; | 188 bool IsStopped() const; |
186 bool IsCompleted() const; | 189 bool IsCompleted() const; |
187 bool NeedsPreroll() const; | 190 bool NeedsPreroll() const; |
188 | 191 |
| 192 // Sets the |needs_preroll_| flag. |
| 193 void SetNeedsPreroll(); |
| 194 |
189 base::android::ScopedJavaLocalRef<jobject> GetMediaCrypto(); | 195 base::android::ScopedJavaLocalRef<jobject> GetMediaCrypto(); |
190 | 196 |
191 // Starts prefetching: accumulates enough data in AccessUnitQueue. | 197 // Starts prefetching: accumulates enough data in AccessUnitQueue. |
192 // Decoder thread is not running. | 198 // Decoder thread is not running. |
193 void Prefetch(const base::Closure& prefetch_done_cb); | 199 void Prefetch(const base::Closure& prefetch_done_cb); |
194 | 200 |
195 // Configures MediaCodec. | 201 // Configures MediaCodec. |
196 ConfigStatus Configure(); | 202 ConfigStatus Configure(); |
197 | 203 |
198 // Starts the decoder for prerolling. This method starts the decoder thread. | 204 // Starts the decoder for prerolling. This method starts the decoder thread. |
199 bool Preroll(base::TimeDelta preroll_timestamp, | 205 bool Preroll(base::TimeDelta preroll_timestamp, |
200 const base::Closure& preroll_done_cb); | 206 const base::Closure& preroll_done_cb); |
201 | 207 |
202 // Starts the decoder after preroll is not needed, starting decoder thread | 208 // Starts the decoder after preroll is not needed, starting decoder thread |
203 // if it has not started yet. | 209 // if it has not started yet. |
204 bool Start(base::TimeDelta start_timestamp); | 210 bool Start(base::TimeDelta start_timestamp); |
205 | 211 |
206 // Stops the playback process synchronously. This method stops the decoder | 212 // Stops the playback process synchronously. This method stops the decoder |
207 // thread synchronously, and then releases all MediaCodec buffers. | 213 // thread synchronously, and then releases all MediaCodec buffers. |
208 void SyncStop(); | 214 void SyncStop(); |
209 | 215 |
210 // Requests to stop the playback and returns. | 216 // Requests to stop the playback and returns. |
211 // Decoder will stop asynchronously after all the dequeued output buffers | 217 // Decoder will stop asynchronously after all the dequeued output buffers |
212 // are rendered. | 218 // are rendered. |
213 void RequestToStop(); | 219 void RequestToStop(); |
214 | 220 |
215 // Notification posted when asynchronous stop is done or playback completed. | 221 // Notification posted when asynchronous stop is done or playback completed. |
216 void OnLastFrameRendered(bool completed); | 222 void OnLastFrameRendered(bool eos_encountered); |
217 | 223 |
218 // Notification posted when last prerolled frame has been returned to codec. | 224 // Notification posted when last prerolled frame has been returned to codec. |
219 void OnPrerollDone(); | 225 void OnPrerollDone(); |
220 | 226 |
221 // Puts the incoming data into AccessUnitQueue. | 227 // Puts the incoming data into AccessUnitQueue. |
222 void OnDemuxerDataAvailable(const DemuxerData& data); | 228 void OnDemuxerDataAvailable(const DemuxerData& data); |
223 | 229 |
224 // For testing only. Returns true if the decoder is in kPrerolling state. | 230 // For testing only. Returns true if the decoder is in kPrerolling state. |
225 bool IsPrerollingForTests() const; | 231 bool IsPrerollingForTests() const; |
226 | 232 |
| 233 // For testing only. Drain decoder and reconfigure for each |kConfigChanged|. |
| 234 void SetAlwaysReconfigureForTests(); |
| 235 |
227 protected: | 236 protected: |
228 enum RenderMode { | 237 enum RenderMode { |
229 kRenderSkip = 0, | 238 kRenderSkip = 0, |
230 kRenderAfterPreroll, | 239 kRenderAfterPreroll, |
231 kRenderNow, | 240 kRenderNow, |
232 }; | 241 }; |
233 | 242 |
234 // Returns true if the new DemuxerConfigs requires MediaCodec | 243 // Returns true if the new DemuxerConfigs requires MediaCodec |
235 // reconfiguration. | 244 // reconfiguration. |
236 virtual bool IsCodecReconfigureNeeded(const DemuxerConfigs& curr, | 245 virtual bool IsCodecReconfigureNeeded(const DemuxerConfigs& next) const = 0; |
237 const DemuxerConfigs& next) const = 0; | |
238 | 246 |
239 // Does the part of MediaCodecBridge configuration that is specific | 247 // Does the part of MediaCodecBridge configuration that is specific |
240 // to audio or video. | 248 // to audio or video. |
241 virtual ConfigStatus ConfigureInternal() = 0; | 249 virtual ConfigStatus ConfigureInternal() = 0; |
242 | 250 |
243 // Associates PTS with device time so we can calculate delays. | 251 // Associates PTS with device time so we can calculate delays. |
244 // We use delays for video decoder only. | 252 // We use delays for video decoder only. |
245 virtual void AssociateCurrentTimeWithPTS(base::TimeDelta current_time) {} | 253 virtual void AssociateCurrentTimeWithPTS(base::TimeDelta current_time) {} |
246 | 254 |
247 // Invalidate delay calculation. We use delays for video decoder only. | 255 // Invalidate delay calculation. We use delays for video decoder only. |
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
291 // input and output buffers. | 299 // input and output buffers. |
292 base::Thread decoder_thread_; | 300 base::Thread decoder_thread_; |
293 | 301 |
294 // The queue of access units. | 302 // The queue of access units. |
295 AccessUnitQueue au_queue_; | 303 AccessUnitQueue au_queue_; |
296 | 304 |
297 // Flag forces reconfiguration even if |media_codec_bridge_| exists. Currently | 305 // Flag forces reconfiguration even if |media_codec_bridge_| exists. Currently |
298 // is set by video decoder when the video surface changes. | 306 // is set by video decoder when the video surface changes. |
299 bool needs_reconfigure_; | 307 bool needs_reconfigure_; |
300 | 308 |
| 309 // Flag forces to drain decoder in the process of dynamic reconfiguration. |
| 310 bool drain_decoder_; |
| 311 |
| 312 // For tests only. Forces to always reconfigure for |kConfigChanged| unit. |
| 313 bool always_reconfigure_for_tests_; |
| 314 |
301 private: | 315 private: |
302 enum DecoderState { | 316 enum DecoderState { |
303 kStopped = 0, | 317 kStopped = 0, |
304 kPrefetching, | 318 kPrefetching, |
305 kPrefetched, | 319 kPrefetched, |
306 kPrerolling, | 320 kPrerolling, |
307 kPrerolled, | 321 kPrerolled, |
308 kRunning, | 322 kRunning, |
309 kStopping, | 323 kStopping, |
310 kInEmergencyStop, | 324 kInEmergencyStop, |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
342 | 356 |
343 // Private Data. | 357 // Private Data. |
344 | 358 |
345 // External data request callback that is passed to decoder. | 359 // External data request callback that is passed to decoder. |
346 base::Closure external_request_data_cb_; | 360 base::Closure external_request_data_cb_; |
347 | 361 |
348 // These notifications are called on corresponding conditions. | 362 // These notifications are called on corresponding conditions. |
349 base::Closure prefetch_done_cb_; | 363 base::Closure prefetch_done_cb_; |
350 base::Closure starvation_cb_; | 364 base::Closure starvation_cb_; |
351 base::Closure preroll_done_cb_; | 365 base::Closure preroll_done_cb_; |
| 366 base::Closure decoder_drained_cb_; |
352 base::Closure stop_done_cb_; | 367 base::Closure stop_done_cb_; |
353 base::Closure error_cb_; | 368 base::Closure error_cb_; |
354 | 369 |
355 // Data request callback that is posted by decoder internally. | 370 // Data request callback that is posted by decoder internally. |
356 base::Closure request_data_cb_; | 371 base::Closure request_data_cb_; |
357 | 372 |
358 // Callback used to post OnCodecError method. | 373 // Callback used to post OnCodecError method. |
359 base::Closure internal_error_cb_; | 374 base::Closure internal_error_cb_; |
360 | 375 |
361 // Callback for posting OnPrerollDone method. | 376 // Callback for posting OnPrerollDone method. |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
393 | 408 |
394 // NOTE: Weak pointers must be invalidated before all other member variables. | 409 // NOTE: Weak pointers must be invalidated before all other member variables. |
395 base::WeakPtrFactory<MediaCodecDecoder> weak_factory_; | 410 base::WeakPtrFactory<MediaCodecDecoder> weak_factory_; |
396 | 411 |
397 DISALLOW_COPY_AND_ASSIGN(MediaCodecDecoder); | 412 DISALLOW_COPY_AND_ASSIGN(MediaCodecDecoder); |
398 }; | 413 }; |
399 | 414 |
400 } // namespace media | 415 } // namespace media |
401 | 416 |
402 #endif // MEDIA_BASE_ANDROID_MEDIA_CODEC_DECODER_H_ | 417 #endif // MEDIA_BASE_ANDROID_MEDIA_CODEC_DECODER_H_ |
OLD | NEW |