Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(40)

Side by Side Diff: media/base/android/media_codec_decoder.h

Issue 1287423004: MediaCodecPlayer implementation (stage 5 - reconfiguration) (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@mtplayer-cleanuptest
Patch Set: Avoid potential frame skipping after decoder drain with a new prerolling mode Created 5 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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
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
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 NotCompletedAndNeedsPreroll() const; 190 bool NotCompletedAndNeedsPreroll() const;
188 191
192 // Sets preroll mode to kPrerollTillNextFrame.
qinmin 2015/08/27 19:38:42 PrerollTillNextFrame is protected and its hard for
Tima Vaisburd 2015/08/27 20:39:47 I did SetDecodingUntilOutputIsPresent() and change
193 void SetPrerollTillNextFrame();
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.
231
232 // Returns true if the decoder is in kPrerolling state.
225 bool IsPrerollingForTests() const; 233 bool IsPrerollingForTests() const;
226 234
235 // Drain decoder and reconfigure for each |kConfigChanged|.
qinmin 2015/08/27 19:38:42 s/Drain/Drains/ s/reconfigure/reconfigures/
Tima Vaisburd 2015/08/27 20:39:47 Done.
236 void SetAlwaysReconfigureForTests();
237
238 // Sets the notification to be called when MediaCodec is created.
239 void SetCodecCreatedCallbackForTests(base::Closure cb);
240
227 protected: 241 protected:
228 enum RenderMode { 242 enum RenderMode {
229 kRenderSkip = 0, 243 kRenderSkip = 0,
230 kRenderAfterPreroll, 244 kRenderAfterPreroll,
231 kRenderNow, 245 kRenderNow,
232 }; 246 };
233 247
234 // Returns true if the new DemuxerConfigs requires MediaCodec 248 // Returns true if the new DemuxerConfigs requires MediaCodec
235 // reconfiguration. 249 // reconfiguration.
236 virtual bool IsCodecReconfigureNeeded(const DemuxerConfigs& curr, 250 virtual bool IsCodecReconfigureNeeded(const DemuxerConfigs& next) const = 0;
237 const DemuxerConfigs& next) const = 0;
238 251
239 // Does the part of MediaCodecBridge configuration that is specific 252 // Does the part of MediaCodecBridge configuration that is specific
240 // to audio or video. 253 // to audio or video.
241 virtual ConfigStatus ConfigureInternal() = 0; 254 virtual ConfigStatus ConfigureInternal() = 0;
242 255
243 // Associates PTS with device time so we can calculate delays. 256 // Associates PTS with device time so we can calculate delays.
244 // We use delays for video decoder only. 257 // We use delays for video decoder only.
245 virtual void AssociateCurrentTimeWithPTS(base::TimeDelta current_time) {} 258 virtual void AssociateCurrentTimeWithPTS(base::TimeDelta current_time) {}
246 259
247 // Invalidate delay calculation. We use delays for video decoder only. 260 // Invalidate delay calculation. We use delays for video decoder only.
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after
295 // input and output buffers. 308 // input and output buffers.
296 base::Thread decoder_thread_; 309 base::Thread decoder_thread_;
297 310
298 // The queue of access units. 311 // The queue of access units.
299 AccessUnitQueue au_queue_; 312 AccessUnitQueue au_queue_;
300 313
301 // Flag forces reconfiguration even if |media_codec_bridge_| exists. Currently 314 // Flag forces reconfiguration even if |media_codec_bridge_| exists. Currently
302 // is set by video decoder when the video surface changes. 315 // is set by video decoder when the video surface changes.
303 bool needs_reconfigure_; 316 bool needs_reconfigure_;
304 317
318 // Flag forces to drain decoder in the process of dynamic reconfiguration.
319 bool drain_decoder_;
320
321 // For tests only. Forces to always reconfigure for |kConfigChanged| unit.
322 bool always_reconfigure_for_tests_;
323
324 // For tests only. Callback to be callned when MediaCodec is created.
325 base::Closure codec_created_for_tests_cb_;
326
305 private: 327 private:
306 enum DecoderState { 328 enum DecoderState {
307 kStopped = 0, 329 kStopped = 0,
308 kPrefetching, 330 kPrefetching,
309 kPrefetched, 331 kPrefetched,
310 kPrerolling, 332 kPrerolling,
311 kPrerolled, 333 kPrerolled,
312 kRunning, 334 kRunning,
313 kStopping, 335 kStopping,
314 kInEmergencyStop, 336 kInEmergencyStop,
315 kError, 337 kError,
316 }; 338 };
317 339
340 enum PrerollMode {
341 kNoPreroll = 0,
342 kPrerollTillNextFrame,
343 kPrerollTillPTS,
344 };
345
318 // Helper method that processes an error from MediaCodec. 346 // Helper method that processes an error from MediaCodec.
319 void OnCodecError(); 347 void OnCodecError();
320 348
321 // Requests data. Ensures there is no more than one request at a time. 349 // Requests data. Ensures there is no more than one request at a time.
322 void RequestData(); 350 void RequestData();
323 351
324 // Prefetching callback that is posted to Media thread 352 // Prefetching callback that is posted to Media thread
325 // in the kPrefetching state. 353 // in the kPrefetching state.
326 void PrefetchNextChunk(); 354 void PrefetchNextChunk();
327 355
(...skipping 18 matching lines...) Expand all
346 374
347 // Private Data. 375 // Private Data.
348 376
349 // External data request callback that is passed to decoder. 377 // External data request callback that is passed to decoder.
350 base::Closure external_request_data_cb_; 378 base::Closure external_request_data_cb_;
351 379
352 // These notifications are called on corresponding conditions. 380 // These notifications are called on corresponding conditions.
353 base::Closure prefetch_done_cb_; 381 base::Closure prefetch_done_cb_;
354 base::Closure starvation_cb_; 382 base::Closure starvation_cb_;
355 base::Closure preroll_done_cb_; 383 base::Closure preroll_done_cb_;
384 base::Closure decoder_drained_cb_;
356 base::Closure stop_done_cb_; 385 base::Closure stop_done_cb_;
357 base::Closure error_cb_; 386 base::Closure error_cb_;
358 387
359 // Data request callback that is posted by decoder internally. 388 // Data request callback that is posted by decoder internally.
360 base::Closure request_data_cb_; 389 base::Closure request_data_cb_;
361 390
362 // Callback used to post OnCodecError method. 391 // Callback used to post OnCodecError method.
363 base::Closure internal_error_cb_; 392 base::Closure internal_error_cb_;
364 393
365 // Callback for posting OnPrerollDone method. 394 // Callback for posting OnPrerollDone method.
366 base::Closure internal_preroll_done_cb_; 395 base::Closure internal_preroll_done_cb_;
367 396
368 // Internal state. 397 // Internal state.
369 DecoderState state_; 398 DecoderState state_;
370 mutable base::Lock state_lock_; 399 mutable base::Lock state_lock_;
371 400
372 // Preroll timestamp is set if we need preroll and cleared after we done it. 401 // Preroll timestamp is set if we need preroll and cleared after we done it.
373 base::TimeDelta preroll_timestamp_; 402 base::TimeDelta preroll_timestamp_;
374 403
375 // Indicates that playback should start with preroll. 404 // The preroll mode. If not |kNoPreroll|, the playback should start with
376 bool needs_preroll_; 405 // preroll.
406 PrerollMode preroll_mode_;
377 407
378 // Flag is set when the EOS is enqueued into MediaCodec. Reset by Flush. 408 // Flag is set when the EOS is enqueued into MediaCodec. Reset by Flush.
379 bool eos_enqueued_; 409 bool eos_enqueued_;
380 410
381 // Flag is set when the EOS is received in MediaCodec output. Reset by Flush. 411 // Flag is set when the EOS is received in MediaCodec output. Reset by Flush.
382 bool completed_; 412 bool completed_;
383 413
384 // Flag to ensure we post last frame notification once. 414 // Flag to ensure we post last frame notification once.
385 bool last_frame_posted_; 415 bool last_frame_posted_;
386 416
(...skipping 10 matching lines...) Expand all
397 427
398 // NOTE: Weak pointers must be invalidated before all other member variables. 428 // NOTE: Weak pointers must be invalidated before all other member variables.
399 base::WeakPtrFactory<MediaCodecDecoder> weak_factory_; 429 base::WeakPtrFactory<MediaCodecDecoder> weak_factory_;
400 430
401 DISALLOW_COPY_AND_ASSIGN(MediaCodecDecoder); 431 DISALLOW_COPY_AND_ASSIGN(MediaCodecDecoder);
402 }; 432 };
403 433
404 } // namespace media 434 } // namespace media
405 435
406 #endif // MEDIA_BASE_ANDROID_MEDIA_CODEC_DECODER_H_ 436 #endif // MEDIA_BASE_ANDROID_MEDIA_CODEC_DECODER_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698