OLD | NEW |
---|---|
1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2013 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 #include "media/gpu/android_video_decode_accelerator.h" | 5 #include "media/gpu/android_video_decode_accelerator.h" |
6 | 6 |
7 #include <stddef.h> | 7 #include <stddef.h> |
8 | 8 |
9 #include <memory> | 9 #include <memory> |
10 | 10 |
(...skipping 16 matching lines...) Expand all Loading... | |
27 #include "gpu/command_buffer/service/mailbox_manager.h" | 27 #include "gpu/command_buffer/service/mailbox_manager.h" |
28 #include "gpu/ipc/service/gpu_channel.h" | 28 #include "gpu/ipc/service/gpu_channel.h" |
29 #include "media/base/android/media_codec_bridge_impl.h" | 29 #include "media/base/android/media_codec_bridge_impl.h" |
30 #include "media/base/android/media_codec_util.h" | 30 #include "media/base/android/media_codec_util.h" |
31 #include "media/base/bind_to_current_loop.h" | 31 #include "media/base/bind_to_current_loop.h" |
32 #include "media/base/bitstream_buffer.h" | 32 #include "media/base/bitstream_buffer.h" |
33 #include "media/base/limits.h" | 33 #include "media/base/limits.h" |
34 #include "media/base/media.h" | 34 #include "media/base/media.h" |
35 #include "media/base/timestamp_constants.h" | 35 #include "media/base/timestamp_constants.h" |
36 #include "media/base/video_decoder_config.h" | 36 #include "media/base/video_decoder_config.h" |
37 #include "media/gpu/avda_overlay_helper_impl.h" | |
37 #include "media/gpu/avda_picture_buffer_manager.h" | 38 #include "media/gpu/avda_picture_buffer_manager.h" |
38 #include "media/gpu/content_video_view_overlay.h" | 39 #include "media/gpu/content_video_view_overlay.h" |
40 #include "media/gpu/content_video_view_overlay_factory.h" | |
39 #include "media/gpu/shared_memory_region.h" | 41 #include "media/gpu/shared_memory_region.h" |
40 #include "media/video/picture.h" | 42 #include "media/video/picture.h" |
41 #include "ui/gl/android/scoped_java_surface.h" | 43 #include "ui/gl/android/scoped_java_surface.h" |
42 #include "ui/gl/android/surface_texture.h" | 44 #include "ui/gl/android/surface_texture.h" |
43 #include "ui/gl/gl_bindings.h" | 45 #include "ui/gl/gl_bindings.h" |
44 | 46 |
45 #if defined(ENABLE_MOJO_MEDIA_IN_GPU_PROCESS) | 47 #if defined(ENABLE_MOJO_MEDIA_IN_GPU_PROCESS) |
46 #include "media/mojo/services/mojo_cdm_service.h" // nogncheck | 48 #include "media/mojo/services/mojo_cdm_service.h" // nogncheck |
47 #endif | 49 #endif |
48 | 50 |
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
102 constexpr base::TimeDelta DecodePollDelay = | 104 constexpr base::TimeDelta DecodePollDelay = |
103 base::TimeDelta::FromMilliseconds(10); | 105 base::TimeDelta::FromMilliseconds(10); |
104 | 106 |
105 constexpr base::TimeDelta NoWaitTimeOut = base::TimeDelta::FromMicroseconds(0); | 107 constexpr base::TimeDelta NoWaitTimeOut = base::TimeDelta::FromMicroseconds(0); |
106 | 108 |
107 constexpr base::TimeDelta IdleTimerTimeOut = base::TimeDelta::FromSeconds(1); | 109 constexpr base::TimeDelta IdleTimerTimeOut = base::TimeDelta::FromSeconds(1); |
108 | 110 |
109 // On low end devices (< KitKat is always low-end due to buggy MediaCodec), | 111 // On low end devices (< KitKat is always low-end due to buggy MediaCodec), |
110 // defer the surface creation until the codec is actually used if we know no | 112 // defer the surface creation until the codec is actually used if we know no |
111 // software fallback exists. | 113 // software fallback exists. |
112 bool ShouldDeferSurfaceCreation(AVDACodecAllocator* codec_allocator, | 114 bool ShouldDeferSurfaceCreation( |
113 int surface_id, | 115 AVDACodecAllocator* codec_allocator, |
114 VideoCodec codec) { | 116 int surface_id, |
115 return surface_id == SurfaceManager::kNoSurfaceID && codec == kCodecH264 && | 117 VideoCodec codec, |
116 codec_allocator->IsAnyRegisteredAVDA() && | 118 const AndroidVideoDecodeAccelerator::PlatformConfig& platform_config) { |
117 base::android::BuildInfo::GetInstance()->sdk_int() <= 18; | 119 return platform_config.force_deferred_surface_creation || |
120 (surface_id == SurfaceManager::kNoSurfaceID && codec == kCodecH264 && | |
121 codec_allocator->IsAnyRegisteredAVDA() && | |
122 platform_config.sdk_int <= 18); | |
118 } | 123 } |
119 | 124 |
120 } // namespace | 125 } // namespace |
121 | 126 |
122 // AVDAManager manages shared resources for a number of AVDA instances. | 127 // AVDAManager manages shared resources for a number of AVDA instances. |
123 // Its responsibilities include: | 128 // Its responsibilities include: |
124 // - Starting and stopping a shared "construction" thread for instantiating and | 129 // - Starting and stopping a shared "construction" thread for instantiating and |
125 // releasing MediaCodecs. | 130 // releasing MediaCodecs. |
126 // - Detecting when a task has hung on the construction thread so AVDAs can | 131 // - Detecting when a task has hung on the construction thread so AVDAs can |
127 // stop using it. | 132 // stop using it. |
(...skipping 91 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
219 } | 224 } |
220 | 225 |
221 AndroidVideoDecodeAccelerator::BitstreamRecord::BitstreamRecord( | 226 AndroidVideoDecodeAccelerator::BitstreamRecord::BitstreamRecord( |
222 BitstreamRecord&& other) | 227 BitstreamRecord&& other) |
223 : buffer(std::move(other.buffer)), memory(std::move(other.memory)) {} | 228 : buffer(std::move(other.buffer)), memory(std::move(other.memory)) {} |
224 | 229 |
225 AndroidVideoDecodeAccelerator::BitstreamRecord::~BitstreamRecord() {} | 230 AndroidVideoDecodeAccelerator::BitstreamRecord::~BitstreamRecord() {} |
226 | 231 |
227 AndroidVideoDecodeAccelerator::AndroidVideoDecodeAccelerator( | 232 AndroidVideoDecodeAccelerator::AndroidVideoDecodeAccelerator( |
228 AVDACodecAllocator* codec_allocator, | 233 AVDACodecAllocator* codec_allocator, |
234 std::unique_ptr<AVDAOverlayHelper> overlay_helper, | |
229 const MakeGLContextCurrentCallback& make_context_current_cb, | 235 const MakeGLContextCurrentCallback& make_context_current_cb, |
230 const GetGLES2DecoderCallback& get_gles2_decoder_cb) | 236 const GetGLES2DecoderCallback& get_gles2_decoder_cb, |
237 const PlatformConfig* optional_platform_config) | |
231 : client_(nullptr), | 238 : client_(nullptr), |
232 codec_allocator_(codec_allocator), | 239 codec_allocator_(codec_allocator), |
233 make_context_current_cb_(make_context_current_cb), | 240 make_context_current_cb_(make_context_current_cb), |
234 get_gles2_decoder_cb_(get_gles2_decoder_cb), | 241 get_gles2_decoder_cb_(get_gles2_decoder_cb), |
235 state_(WAITING_FOR_SURFACE), | 242 state_(BEFORE_OVERLAY_INIT), |
236 picturebuffers_requested_(false), | 243 picturebuffers_requested_(false), |
237 picture_buffer_manager_(this), | 244 picture_buffer_manager_(this), |
238 media_drm_bridge_cdm_context_(nullptr), | 245 media_drm_bridge_cdm_context_(nullptr), |
239 cdm_registration_id_(0), | 246 cdm_registration_id_(0), |
240 pending_input_buf_index_(-1), | 247 pending_input_buf_index_(-1), |
241 during_initialize_(false), | 248 during_initialize_(false), |
242 deferred_initialization_pending_(false), | 249 deferred_initialization_pending_(false), |
243 codec_needs_reset_(false), | 250 codec_needs_reset_(false), |
244 defer_surface_creation_(false), | 251 defer_surface_creation_(false), |
245 weak_this_factory_(this) {} | 252 overlay_helper_(std::move(overlay_helper)), |
253 weak_this_factory_(this) { | |
254 if (optional_platform_config) { | |
255 platform_config_ = *optional_platform_config; | |
256 } else { | |
257 platform_config_.sdk_int = | |
258 base::android::BuildInfo::GetInstance()->sdk_int(); | |
259 platform_config_.allow_setsurface = | |
260 MediaCodecUtil::IsSetOutputSurfaceSupported(); | |
watk
2017/04/22 00:29:07
Could we construct a default one where we construc
liberato (no reviews please)
2017/04/27 18:30:12
Done.
| |
261 } | |
262 } | |
246 | 263 |
247 AndroidVideoDecodeAccelerator::~AndroidVideoDecodeAccelerator() { | 264 AndroidVideoDecodeAccelerator::~AndroidVideoDecodeAccelerator() { |
248 DCHECK(thread_checker_.CalledOnValidThread()); | 265 DCHECK(thread_checker_.CalledOnValidThread()); |
249 GetManager()->StopTimer(this); | 266 GetManager()->StopTimer(this); |
250 codec_allocator_->StopThread(this); | 267 codec_allocator_->StopThread(this); |
251 | 268 |
252 #if defined(ENABLE_MOJO_MEDIA_IN_GPU_PROCESS) | 269 #if defined(ENABLE_MOJO_MEDIA_IN_GPU_PROCESS) |
253 if (!media_drm_bridge_cdm_context_) | 270 if (!media_drm_bridge_cdm_context_) |
254 return; | 271 return; |
255 | 272 |
(...skipping 25 matching lines...) Expand all Loading... | |
281 return false; | 298 return false; |
282 } | 299 } |
283 | 300 |
284 DCHECK(client); | 301 DCHECK(client); |
285 client_ = client; | 302 client_ = client; |
286 config_ = config; | 303 config_ = config; |
287 codec_config_ = new CodecConfig(); | 304 codec_config_ = new CodecConfig(); |
288 codec_config_->codec = VideoCodecProfileToVideoCodec(config.profile); | 305 codec_config_->codec = VideoCodecProfileToVideoCodec(config.profile); |
289 codec_config_->initial_expected_coded_size = | 306 codec_config_->initial_expected_coded_size = |
290 config.initial_expected_coded_size; | 307 config.initial_expected_coded_size; |
291 incoming_bundle_ = new AVDASurfaceBundle(config_.surface_id); | |
292 | 308 |
293 if (codec_config_->codec != kCodecVP8 && codec_config_->codec != kCodecVP9 && | 309 if (codec_config_->codec != kCodecVP8 && codec_config_->codec != kCodecVP9 && |
294 #if BUILDFLAG(ENABLE_HEVC_DEMUXING) | 310 #if BUILDFLAG(ENABLE_HEVC_DEMUXING) |
295 codec_config_->codec != kCodecHEVC && | 311 codec_config_->codec != kCodecHEVC && |
296 #endif | 312 #endif |
297 codec_config_->codec != kCodecH264) { | 313 codec_config_->codec != kCodecH264) { |
298 DLOG(ERROR) << "Unsupported profile: " << GetProfileName(config.profile); | 314 DLOG(ERROR) << "Unsupported profile: " << GetProfileName(config.profile); |
299 return false; | 315 return false; |
300 } | 316 } |
301 | 317 |
(...skipping 21 matching lines...) Expand all Loading... | |
323 // SetSurface() can't be called before Initialize(), so we pick up our first | 339 // SetSurface() can't be called before Initialize(), so we pick up our first |
324 // surface ID from the codec configuration. | 340 // surface ID from the codec configuration. |
325 DCHECK(!pending_surface_id_); | 341 DCHECK(!pending_surface_id_); |
326 | 342 |
327 // We signaled that we support deferred initialization, so see if the client | 343 // We signaled that we support deferred initialization, so see if the client |
328 // does also. | 344 // does also. |
329 deferred_initialization_pending_ = config.is_deferred_initialization_allowed; | 345 deferred_initialization_pending_ = config.is_deferred_initialization_allowed; |
330 | 346 |
331 // If we're low on resources, we may decide to defer creation of the surface | 347 // If we're low on resources, we may decide to defer creation of the surface |
332 // until the codec is actually used. | 348 // until the codec is actually used. |
333 if (ShouldDeferSurfaceCreation(codec_allocator_, surface_id(), | 349 if (ShouldDeferSurfaceCreation(codec_allocator_, config_.surface_id, |
334 codec_config_->codec)) { | 350 codec_config_->codec, platform_config_)) { |
335 // We should never be here if a SurfaceView is required. | 351 // We should never be here if a SurfaceView is required. |
336 DCHECK_EQ(surface_id(), SurfaceManager::kNoSurfaceID); | 352 // TODO(liberato): This really isn't true with AndroidOverlay. |
353 DCHECK_EQ(config_.surface_id, SurfaceManager::kNoSurfaceID); | |
337 defer_surface_creation_ = true; | 354 defer_surface_creation_ = true; |
338 } | 355 } |
339 | 356 |
340 if (!codec_allocator_->StartThread(this)) { | 357 if (!codec_allocator_->StartThread(this)) { |
341 LOG(ERROR) << "Unable to start thread"; | 358 LOG(ERROR) << "Unable to start thread"; |
342 return false; | 359 return false; |
343 } | 360 } |
344 | 361 |
345 // For encrypted media, start by initializing the CDM. Otherwise, start with | 362 // For encrypted media, start by initializing the CDM. Otherwise, start with |
346 // the surface. | 363 // the surface. |
347 if (config_.is_encrypted()) { | 364 if (config_.is_encrypted()) { |
348 if (!deferred_initialization_pending_) { | 365 if (!deferred_initialization_pending_) { |
349 DLOG(ERROR) | 366 DLOG(ERROR) |
350 << "Deferred initialization must be used for encrypted streams"; | 367 << "Deferred initialization must be used for encrypted streams"; |
351 return false; | 368 return false; |
352 } | 369 } |
353 InitializeCdm(); | 370 InitializeCdm(); |
354 } else { | 371 } else { |
355 StartSurfaceCreation(); | 372 StartOverlayHelper(); |
356 } | 373 } |
357 | 374 |
358 // Fail / complete / defer initialization. | 375 // Fail / complete / defer initialization. |
359 return state_ != ERROR; | 376 return state_ != ERROR; |
360 } | 377 } |
361 | 378 |
362 void AndroidVideoDecodeAccelerator::StartSurfaceCreation() { | 379 void AndroidVideoDecodeAccelerator::StartOverlayHelper() { |
363 // We might be called during Initialize, during deferred initialization, or | 380 DCHECK_EQ(state_, BEFORE_OVERLAY_INIT); |
364 // afterwards (::Decode, for deferred surface init, UpdateSurface). | |
365 DCHECK(incoming_bundle_); | |
366 | 381 |
367 // We should not yet have an overlay. | 382 // If we're trying to defer surface creation, then don't notify the helper |
368 DCHECK(!incoming_bundle_->overlay); | 383 // that it may start getting surfaces yet. We'll do that later. |
369 | |
370 // Note that we don't enforce that for any SurfaceTexture or its Surface, | |
371 // since there might be a codec that's using them. They'll get cleared | |
372 // later, in InitializePictureBufferManager. | |
373 | |
374 // If surface creation is deferred, then do nothing except signal that init | |
375 // is complete, if needed. We might still fail to get a surface or codec, | |
376 // which would normally be an init error. Since we're deferring init until a | |
377 // decode to save resources, though, we're signaling success now. If we're | |
378 // wrong, then decoding might fail when we might have been able to use a | |
379 // fallback renderer in WMPI if we failed init. | |
380 if (defer_surface_creation_) { | 384 if (defer_surface_creation_) { |
381 if (deferred_initialization_pending_) | 385 if (deferred_initialization_pending_) |
382 NotifyInitializationSucceeded(); | 386 NotifyInitializationSucceeded(); |
383 | |
384 return; | 387 return; |
385 } | 388 } |
386 | 389 |
387 if (incoming_bundle_->surface_id != SurfaceManager::kNoSurfaceID) { | 390 // Handle the sync path, which must use SurfaceTexture anyway. Note that we |
388 // Create the overlay. Note that it will never call us back immediately. | 391 // check both |during_initialize_| and |deferred_initialization_pending_|, |
389 // It will post when the surface is available. | 392 // since we might get here during deferred surface creation. In that case, |
390 AndroidOverlay::Config overlay_config; | 393 // Decode will call us (after clearing |defer_surface_creation_|), but |
391 // We use weak ptrs here since |overlay| can outlive us, if we send it for | 394 // deferred init will have already been signaled optimistically as success. |
392 // async codec config. | 395 // |
393 overlay_config.ready_cb = | 396 // Also note that we might choose to defer surface creation for the sync path, |
394 base::Bind(&AndroidVideoDecodeAccelerator::OnOverlayReady, | 397 // which won't get here. We'll exit above, successfully, during init, and |
395 weak_this_factory_.GetWeakPtr()); | 398 // will fall through to the below when Decode calls us back. That's okay. |
396 overlay_config.failed_cb = | 399 // We only handle this case specially since |overlay_helper_| is allowed to |
397 base::Bind(&AndroidVideoDecodeAccelerator::OnOverlayFailed, | 400 // post callbacks to us. Here, we guarantee that the sync case is actually |
398 weak_this_factory_.GetWeakPtr()); | 401 // resolved synchronously. The only exception will be if we need to defer |
399 overlay_config.destroyed_cb = | 402 // surface creation for other reasons, in which case the sync path with just |
400 base::Bind(&AndroidVideoDecodeAccelerator::OnSurfaceDestroyed, | 403 // signal success optimistically. |
401 weak_this_factory_.GetWeakPtr()); | 404 if (during_initialize_ && !deferred_initialization_pending_) { |
402 // TODO(liberato): make |surface_id| the overlay config token. If we're | 405 DCHECK_EQ(config_.surface_id, SurfaceManager::kNoSurfaceID); |
403 // using CVV, then we'll need a CVV factory impl that understands it. | 406 OnTransitionToOrFromOverlay(nullptr); |
404 incoming_bundle_->overlay = base::MakeUnique<ContentVideoViewOverlay>( | |
405 incoming_bundle_->surface_id, overlay_config); | |
406 // We have to wait for some other AVDA instance to free up the surface. | |
407 // OnOverlayReady will be called when it's available. | |
408 // Note that if we aren't deferring init, then we'll signal success, and | |
409 // if we fail later then it will fail decoding instead. However, since | |
410 // nobody that provides a SurfaceView requires sync init, it doesn't matter. | |
411 // Also remember that ContentVideoViewOverlay will not call OnOverlayReady | |
412 // before it returns. | |
413 state_ = WAITING_FOR_SURFACE; | |
414 return; | 407 return; |
415 } | 408 } |
416 | 409 |
417 // We're creating a SurfaceTexture. | 410 // If we have a surface, then notify |overlay_helper_| about it. |
418 InitializePictureBufferManager(); | 411 std::unique_ptr<AndroidOverlayFactory> factory; |
412 if (config_.surface_id != SurfaceManager::kNoSurfaceID) { | |
413 factory = | |
414 base::MakeUnique<ContentVideoViewOverlayFactory>(config_.surface_id); | |
415 } | |
416 | |
417 // Notify |overlay_helper_| that we've started. This guarantees that we'll | |
418 // get a callback. It might not be a synchronous callback, but we're not in | |
419 // the synchronous case. It will be soon, though. For pre-M, we rely on the | |
420 // fact that |overlay_helper_| won't tell us to use a SurfaceTexture while | |
421 // waiting for an overlay to become ready, for example. | |
422 overlay_helper_->Startup( | |
423 base::Bind(&AndroidVideoDecodeAccelerator::OnTransitionToOrFromOverlay, | |
424 GetWeakPtr()), | |
425 base::Bind(&AndroidVideoDecodeAccelerator::OnTransitionToOrFromOverlay, | |
426 GetWeakPtr(), nullptr), | |
427 base::Bind(&AndroidVideoDecodeAccelerator::OnStopUsingOverlayImmediately, | |
428 GetWeakPtr()), | |
429 std::move(factory)); | |
419 } | 430 } |
420 | 431 |
421 void AndroidVideoDecodeAccelerator::OnOverlayReady(AndroidOverlay* overlay) { | 432 void AndroidVideoDecodeAccelerator::OnTransitionToOrFromOverlay( |
422 DCHECK(!defer_surface_creation_); | 433 std::unique_ptr<AndroidOverlay> overlay) { |
423 DCHECK_EQ(state_, WAITING_FOR_SURFACE); | 434 // If we're waiting for a surface (e.g., during startup), then proceed |
424 DCHECK(incoming_bundle_); | 435 // immediately. Otherwise, wait for Dequeue to handle it. This can probably |
436 // be merged with UpdateSurface. | |
437 if (state_ == BEFORE_OVERLAY_INIT) { | |
438 DCHECK(!incoming_overlay_); | |
439 incoming_bundle_ = new AVDASurfaceBundle(std::move(overlay)); | |
440 InitializePictureBufferManager(); | |
441 return; | |
442 } | |
425 | 443 |
426 InitializePictureBufferManager(); | 444 // If, for some reason, |overlay_helper_| decides that we really should |
427 } | 445 // change our output surface pre-M, ignore it. For example, if the |
446 // compositor tells us that it can't use an overlay, well, there's not much | |
447 // that we can do here unless we start falling forward to keyframes. | |
448 if (!platform_config_.allow_setsurface) | |
449 return; | |
428 | 450 |
429 void AndroidVideoDecodeAccelerator::OnOverlayFailed(AndroidOverlay* overlay) { | 451 // If we're using a SurfaceTexture and are told to switch to one, then just |
430 NOTIFY_ERROR(PLATFORM_FAILURE, "Surface is not available"); | 452 // do nothing. |overlay_helper_| doesn't really know if we've switched to |
453 // SurfaceTexture or not. Note that it can't ask us to switch to the same | |
454 // overlay we're using, since it's unique_ptr. | |
455 if (!overlay && codec_config_->surface_bundle && | |
456 !codec_config_->surface_bundle->overlay) { | |
457 // Also stop transitioning to an overlay, if we were doing so. | |
458 incoming_overlay_.reset(); | |
459 return; | |
460 } | |
461 | |
462 incoming_overlay_ = std::move(overlay); | |
431 } | 463 } |
432 | 464 |
433 void AndroidVideoDecodeAccelerator::InitializePictureBufferManager() { | 465 void AndroidVideoDecodeAccelerator::InitializePictureBufferManager() { |
434 DCHECK(!defer_surface_creation_); | 466 DCHECK(!defer_surface_creation_); |
435 DCHECK(incoming_bundle_); | 467 DCHECK(incoming_bundle_); |
436 | 468 |
437 if (!make_context_current_cb_.Run()) { | 469 if (!make_context_current_cb_.Run()) { |
438 NOTIFY_ERROR(PLATFORM_FAILURE, | 470 NOTIFY_ERROR(PLATFORM_FAILURE, |
439 "Failed to make this decoder's GL context current"); | 471 "Failed to make this decoder's GL context current"); |
440 incoming_bundle_ = nullptr; | 472 incoming_bundle_ = nullptr; |
(...skipping 25 matching lines...) Expand all Loading... | |
466 // do not try to allocate a new codec; we might not be at a keyframe, etc. | 498 // do not try to allocate a new codec; we might not be at a keyframe, etc. |
467 // If we get here with a codec, then we must setSurface. | 499 // If we get here with a codec, then we must setSurface. |
468 if (media_codec_) { | 500 if (media_codec_) { |
469 // TODO(liberato): fail on api check? | 501 // TODO(liberato): fail on api check? |
470 if (!media_codec_->SetSurface(incoming_bundle_->j_surface().obj())) { | 502 if (!media_codec_->SetSurface(incoming_bundle_->j_surface().obj())) { |
471 NOTIFY_ERROR(PLATFORM_FAILURE, "MediaCodec failed to switch surfaces."); | 503 NOTIFY_ERROR(PLATFORM_FAILURE, "MediaCodec failed to switch surfaces."); |
472 // We're not going to use |incoming_bundle_|. | 504 // We're not going to use |incoming_bundle_|. |
473 } else { | 505 } else { |
474 // We've switched surfaces, so replace |surface_bundle|. | 506 // We've switched surfaces, so replace |surface_bundle|. |
475 codec_config_->surface_bundle = incoming_bundle_; | 507 codec_config_->surface_bundle = incoming_bundle_; |
476 // We could be in WAITING_FOR_SURFACE, but we're not anymore. | 508 // We could be in BEFORE_OVERLAY_INIT, but we're not anymore. |
477 state_ = NO_ERROR; | 509 state_ = NO_ERROR; |
478 } | 510 } |
479 incoming_bundle_ = nullptr; | 511 incoming_bundle_ = nullptr; |
480 return; | 512 return; |
481 } | 513 } |
482 | 514 |
483 // We're going to create a codec with |incoming_bundle_|. It might fail, but | 515 // We're going to create a codec with |incoming_bundle_|. It might fail, but |
484 // either way, we're done with any previous bundle. Note that, since we | 516 // either way, we're done with any previous bundle. Note that, since we |
485 // never get here after init (i.e., we never change surfaces without using | 517 // never get here after init (i.e., we never change surfaces without using |
486 // SetSurface), there shouldn't be any previous bundle. However, this is the | 518 // SetSurface), there shouldn't be any previous bundle. However, this is the |
487 // right thing to do even if we can switch. | 519 // right thing to do even if we can switch. |
488 codec_config_->surface_bundle = incoming_bundle_; | 520 codec_config_->surface_bundle = incoming_bundle_; |
489 incoming_bundle_ = nullptr; | 521 incoming_bundle_ = nullptr; |
490 | 522 |
491 // If the client doesn't support deferred initialization (WebRTC), then we | 523 // If the client doesn't support deferred initialization (WebRTC), then we |
492 // should complete it now and return a meaningful result. Note that it would | 524 // should complete it now and return a meaningful result. Note that it would |
493 // be nice if we didn't have to worry about starting codec configuration at | 525 // be nice if we didn't have to worry about starting codec configuration at |
494 // all (::Initialize or the wrapper can do it), but then they have to remember | 526 // all (::Initialize or the wrapper can do it), but then they have to remember |
495 // not to start codec config if we have to wait for the cdm. It's somewhat | 527 // not to start codec config if we have to wait for the cdm. It's somewhat |
496 // clearer for us to handle both cases. | 528 // clearer for us to handle both cases. |
497 // For this to be a case for sync configuration, we must be called from | 529 // For this to be a case for sync configuration, we must be called from |
498 // Initialize(), and the client must not want deferred init. Note that having | 530 // Initialize(), and the client must not want deferred init. Note that having |
499 // |deferred_initialization_pending_| false by itself isn't enough; if we're | 531 // |deferred_initialization_pending_| false by itself isn't enough; if we're |
500 // deferring surface creation, then we'll finish deferred init before asking | 532 // deferring surface creation, then we'll finish deferred init before asking |
501 // for the surface. We'll be called via ::Decode. | 533 // for the surface. We'll be called via Decode. |
502 if (during_initialize_ && !deferred_initialization_pending_) { | 534 if (during_initialize_ && !deferred_initialization_pending_) { |
503 ConfigureMediaCodecSynchronously(); | 535 ConfigureMediaCodecSynchronously(); |
504 return; | 536 return; |
505 } | 537 } |
506 | 538 |
507 // In all other cases, we don't have to wait for the codec. | 539 // In all other cases, we don't have to wait for the codec. |
508 ConfigureMediaCodecAsynchronously(); | 540 ConfigureMediaCodecAsynchronously(); |
509 } | 541 } |
510 | 542 |
511 void AndroidVideoDecodeAccelerator::DoIOTask(bool start_timer) { | 543 void AndroidVideoDecodeAccelerator::DoIOTask(bool start_timer) { |
512 DCHECK(thread_checker_.CalledOnValidThread()); | 544 DCHECK(thread_checker_.CalledOnValidThread()); |
513 TRACE_EVENT0("media", "AVDA::DoIOTask"); | 545 TRACE_EVENT0("media", "AVDA::DoIOTask"); |
514 if (state_ == ERROR || state_ == WAITING_FOR_CODEC || | 546 if (state_ == ERROR || state_ == WAITING_FOR_CODEC || |
515 state_ == SURFACE_DESTROYED || state_ == WAITING_FOR_SURFACE) { | 547 state_ == SURFACE_DESTROYED || state_ == BEFORE_OVERLAY_INIT) { |
516 return; | 548 return; |
517 } | 549 } |
518 | 550 |
519 picture_buffer_manager_.MaybeRenderEarly(); | 551 picture_buffer_manager_.MaybeRenderEarly(); |
520 bool did_work = false, did_input = false, did_output = false; | 552 bool did_work = false, did_input = false, did_output = false; |
521 do { | 553 do { |
522 did_input = QueueInput(); | 554 did_input = QueueInput(); |
523 did_output = DequeueOutput(); | 555 did_output = DequeueOutput(); |
524 if (did_input || did_output) | 556 if (did_input || did_output) |
525 did_work = true; | 557 did_work = true; |
526 } while (did_input || did_output); | 558 } while (did_input || did_output); |
527 | 559 |
528 ManageTimer(did_work || start_timer); | 560 ManageTimer(did_work || start_timer); |
529 } | 561 } |
530 | 562 |
531 bool AndroidVideoDecodeAccelerator::QueueInput() { | 563 bool AndroidVideoDecodeAccelerator::QueueInput() { |
532 DCHECK(thread_checker_.CalledOnValidThread()); | 564 DCHECK(thread_checker_.CalledOnValidThread()); |
533 TRACE_EVENT0("media", "AVDA::QueueInput"); | 565 TRACE_EVENT0("media", "AVDA::QueueInput"); |
534 if (state_ == ERROR || state_ == WAITING_FOR_CODEC || | 566 if (state_ == ERROR || state_ == WAITING_FOR_CODEC || |
535 state_ == WAITING_FOR_KEY || state_ == WAITING_FOR_SURFACE) { | 567 state_ == WAITING_FOR_KEY || state_ == BEFORE_OVERLAY_INIT) { |
536 return false; | 568 return false; |
537 } | 569 } |
538 if (bitstreams_notified_in_advance_.size() > kMaxBitstreamsNotifiedInAdvance) | 570 if (bitstreams_notified_in_advance_.size() > kMaxBitstreamsNotifiedInAdvance) |
539 return false; | 571 return false; |
540 if (pending_bitstream_records_.empty()) | 572 if (pending_bitstream_records_.empty()) |
541 return false; | 573 return false; |
542 | 574 |
543 int input_buf_index = pending_input_buf_index_; | 575 int input_buf_index = pending_input_buf_index_; |
544 | 576 |
545 // Do not dequeue a new input buffer if we failed with MEDIA_CODEC_NO_KEY. | 577 // Do not dequeue a new input buffer if we failed with MEDIA_CODEC_NO_KEY. |
(...skipping 95 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
641 // We should call NotifyEndOfBitstreamBuffer(), when no more decoded output | 673 // We should call NotifyEndOfBitstreamBuffer(), when no more decoded output |
642 // will be returned from the bitstream buffer. However, MediaCodec API is | 674 // will be returned from the bitstream buffer. However, MediaCodec API is |
643 // not enough to guarantee it. | 675 // not enough to guarantee it. |
644 // So, here, we calls NotifyEndOfBitstreamBuffer() in advance in order to | 676 // So, here, we calls NotifyEndOfBitstreamBuffer() in advance in order to |
645 // keep getting more bitstreams from the client, and throttle them by using | 677 // keep getting more bitstreams from the client, and throttle them by using |
646 // |bitstreams_notified_in_advance_|. | 678 // |bitstreams_notified_in_advance_|. |
647 // TODO(dwkang): check if there is a way to remove this workaround. | 679 // TODO(dwkang): check if there is a way to remove this workaround. |
648 base::ThreadTaskRunnerHandle::Get()->PostTask( | 680 base::ThreadTaskRunnerHandle::Get()->PostTask( |
649 FROM_HERE, | 681 FROM_HERE, |
650 base::Bind(&AndroidVideoDecodeAccelerator::NotifyEndOfBitstreamBuffer, | 682 base::Bind(&AndroidVideoDecodeAccelerator::NotifyEndOfBitstreamBuffer, |
651 weak_this_factory_.GetWeakPtr(), bitstream_buffer.id())); | 683 GetWeakPtr(), bitstream_buffer.id())); |
652 bitstreams_notified_in_advance_.push_back(bitstream_buffer.id()); | 684 bitstreams_notified_in_advance_.push_back(bitstream_buffer.id()); |
653 | 685 |
654 if (status != MEDIA_CODEC_OK) { | 686 if (status != MEDIA_CODEC_OK) { |
655 NOTIFY_ERROR(PLATFORM_FAILURE, "QueueInputBuffer failed:" << status); | 687 NOTIFY_ERROR(PLATFORM_FAILURE, "QueueInputBuffer failed:" << status); |
656 return false; | 688 return false; |
657 } | 689 } |
658 | 690 |
659 return true; | 691 return true; |
660 } | 692 } |
661 | 693 |
662 bool AndroidVideoDecodeAccelerator::DequeueOutput() { | 694 bool AndroidVideoDecodeAccelerator::DequeueOutput() { |
663 DCHECK(thread_checker_.CalledOnValidThread()); | 695 DCHECK(thread_checker_.CalledOnValidThread()); |
664 TRACE_EVENT0("media", "AVDA::DequeueOutput"); | 696 TRACE_EVENT0("media", "AVDA::DequeueOutput"); |
665 if (state_ == ERROR || state_ == WAITING_FOR_CODEC || | 697 if (state_ == ERROR || state_ == WAITING_FOR_CODEC || |
666 state_ == WAITING_FOR_SURFACE) { | 698 state_ == BEFORE_OVERLAY_INIT) { |
667 return false; | 699 return false; |
668 } | 700 } |
669 // If we're draining for reset or destroy, then we don't need picture buffers | 701 // If we're draining for reset or destroy, then we don't need picture buffers |
670 // since we won't send any decoded frames anyway. There might not be any, | 702 // since we won't send any decoded frames anyway. There might not be any, |
671 // since the pipeline might not be sending them back and / or they don't | 703 // since the pipeline might not be sending them back and / or they don't |
672 // exist anymore. From the pipeline's point of view, for Destroy at least, | 704 // exist anymore. From the pipeline's point of view, for Destroy at least, |
673 // the VDA is already gone. | 705 // the VDA is already gone. |
674 if (picturebuffers_requested_ && output_picture_buffers_.empty() && | 706 if (picturebuffers_requested_ && output_picture_buffers_.empty() && |
675 !IsDrainingForResetOrDestroy()) { | 707 !IsDrainingForResetOrDestroy()) { |
676 return false; | 708 return false; |
677 } | 709 } |
678 if (!output_picture_buffers_.empty() && free_picture_ids_.empty() && | 710 if (!output_picture_buffers_.empty() && free_picture_ids_.empty() && |
679 !IsDrainingForResetOrDestroy()) { | 711 !IsDrainingForResetOrDestroy()) { |
680 // Don't have any picture buffer to send. Need to wait. | 712 // Don't have any picture buffer to send. Need to wait. |
681 return false; | 713 return false; |
682 } | 714 } |
683 | 715 |
684 // If we're waiting to switch surfaces pause output release until we have all | 716 // If we're waiting to switch surfaces pause output release until we have all |
685 // picture buffers returned. This is so we can ensure the right flags are set | 717 // picture buffers returned. This is so we can ensure the right flags are set |
686 // on the picture buffers returned to the client. | 718 // on the picture buffers returned to the client. |
687 if (pending_surface_id_) { | 719 if (incoming_overlay_) { |
688 if (picture_buffer_manager_.HasUnrenderedPictures()) | 720 if (picture_buffer_manager_.HasUnrenderedPictures()) |
689 return false; | 721 return false; |
690 if (!UpdateSurface()) | 722 if (!UpdateSurface()) |
691 return false; | 723 return false; |
692 // If we can't allocate the incoming surface yet, then stop here. | 724 |
693 if (state_ != NO_ERROR) | 725 // UpdateSurface should fail if we've transitioned to the error state. |
694 return false; | 726 DCHECK(state_ == NO_ERROR); |
695 } | 727 } |
696 | 728 |
697 bool eos = false; | 729 bool eos = false; |
698 base::TimeDelta presentation_timestamp; | 730 base::TimeDelta presentation_timestamp; |
699 int32_t buf_index = 0; | 731 int32_t buf_index = 0; |
700 do { | 732 do { |
701 size_t offset = 0; | 733 size_t offset = 0; |
702 size_t size = 0; | 734 size_t size = 0; |
703 | 735 |
704 TRACE_EVENT_BEGIN0("media", "AVDA::DequeueOutput"); | 736 TRACE_EVENT_BEGIN0("media", "AVDA::DequeueOutput"); |
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
745 // decoded images. Breaking their connection to the decoded image will | 777 // decoded images. Breaking their connection to the decoded image will |
746 // cause rendering of black frames. Instead, we let the existing | 778 // cause rendering of black frames. Instead, we let the existing |
747 // PictureBuffers live on and we simply update their size the next time | 779 // PictureBuffers live on and we simply update their size the next time |
748 // they're attached to an image of the new resolution. See the | 780 // they're attached to an image of the new resolution. See the |
749 // size update in |SendDecodedFrameToClient| and https://crbug/587994. | 781 // size update in |SendDecodedFrameToClient| and https://crbug/587994. |
750 if (output_picture_buffers_.empty() && !picturebuffers_requested_) { | 782 if (output_picture_buffers_.empty() && !picturebuffers_requested_) { |
751 picturebuffers_requested_ = true; | 783 picturebuffers_requested_ = true; |
752 base::ThreadTaskRunnerHandle::Get()->PostTask( | 784 base::ThreadTaskRunnerHandle::Get()->PostTask( |
753 FROM_HERE, | 785 FROM_HERE, |
754 base::Bind(&AndroidVideoDecodeAccelerator::RequestPictureBuffers, | 786 base::Bind(&AndroidVideoDecodeAccelerator::RequestPictureBuffers, |
755 weak_this_factory_.GetWeakPtr())); | 787 GetWeakPtr())); |
756 return false; | 788 return false; |
757 } | 789 } |
758 | 790 |
759 return true; | 791 return true; |
760 } | 792 } |
761 | 793 |
762 case MEDIA_CODEC_OUTPUT_BUFFERS_CHANGED: | 794 case MEDIA_CODEC_OUTPUT_BUFFERS_CHANGED: |
763 break; | 795 break; |
764 | 796 |
765 case MEDIA_CODEC_OK: | 797 case MEDIA_CODEC_OK: |
(...skipping 111 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
877 picture_buffer_manager_.UseCodecBufferForPictureBuffer(codec_buffer_index, | 909 picture_buffer_manager_.UseCodecBufferForPictureBuffer(codec_buffer_index, |
878 picture_buffer); | 910 picture_buffer); |
879 } | 911 } |
880 | 912 |
881 void AndroidVideoDecodeAccelerator::Decode( | 913 void AndroidVideoDecodeAccelerator::Decode( |
882 const BitstreamBuffer& bitstream_buffer) { | 914 const BitstreamBuffer& bitstream_buffer) { |
883 DCHECK(thread_checker_.CalledOnValidThread()); | 915 DCHECK(thread_checker_.CalledOnValidThread()); |
884 | 916 |
885 // If we deferred getting a surface, then start getting one now. | 917 // If we deferred getting a surface, then start getting one now. |
886 if (defer_surface_creation_) { | 918 if (defer_surface_creation_) { |
887 // This is a little strange in that we're not really waiting for a surface | 919 // We should still be in BEFORE_OVERLAY_INIT, since we've deferred doing it |
888 // yet -- we haven't requested one. We're still in WAITING_FOR_SURFACE as | 920 // until now. |
889 // the initial state set during construction. | 921 DCHECK_EQ(state_, BEFORE_OVERLAY_INIT); |
890 DCHECK_EQ(state_, WAITING_FOR_SURFACE); | |
891 defer_surface_creation_ = false; | 922 defer_surface_creation_ = false; |
892 StartSurfaceCreation(); | 923 StartOverlayHelper(); |
893 if (state_ == ERROR) { | 924 if (state_ == ERROR) { |
894 DLOG(ERROR) << "Failed deferred surface and MediaCodec initialization."; | 925 DLOG(ERROR) << "Failed deferred surface and MediaCodec initialization."; |
895 return; | 926 return; |
896 } | 927 } |
897 } | 928 } |
898 | 929 |
899 // If we previously deferred a codec restart, take care of it now. This can | 930 // If we previously deferred a codec restart, take care of it now. This can |
900 // happen on older devices where configuration changes require a codec reset. | 931 // happen on older devices where configuration changes require a codec reset. |
901 if (codec_needs_reset_) { | 932 if (codec_needs_reset_) { |
902 DCHECK(!drain_type_); | 933 DCHECK(!drain_type_); |
903 ResetCodecState(); | 934 ResetCodecState(); |
904 } | 935 } |
905 | 936 |
906 if (bitstream_buffer.id() >= 0 && bitstream_buffer.size() > 0) { | 937 if (bitstream_buffer.id() >= 0 && bitstream_buffer.size() > 0) { |
907 DecodeBuffer(bitstream_buffer); | 938 DecodeBuffer(bitstream_buffer); |
908 return; | 939 return; |
909 } | 940 } |
910 | 941 |
911 if (base::SharedMemory::IsHandleValid(bitstream_buffer.handle())) | 942 if (base::SharedMemory::IsHandleValid(bitstream_buffer.handle())) |
912 base::SharedMemory::CloseHandle(bitstream_buffer.handle()); | 943 base::SharedMemory::CloseHandle(bitstream_buffer.handle()); |
913 | 944 |
914 if (bitstream_buffer.id() < 0) { | 945 if (bitstream_buffer.id() < 0) { |
915 NOTIFY_ERROR(INVALID_ARGUMENT, | 946 NOTIFY_ERROR(INVALID_ARGUMENT, |
916 "Invalid bistream_buffer, id: " << bitstream_buffer.id()); | 947 "Invalid bistream_buffer, id: " << bitstream_buffer.id()); |
917 } else { | 948 } else { |
918 base::ThreadTaskRunnerHandle::Get()->PostTask( | 949 base::ThreadTaskRunnerHandle::Get()->PostTask( |
919 FROM_HERE, | 950 FROM_HERE, |
920 base::Bind(&AndroidVideoDecodeAccelerator::NotifyEndOfBitstreamBuffer, | 951 base::Bind(&AndroidVideoDecodeAccelerator::NotifyEndOfBitstreamBuffer, |
921 weak_this_factory_.GetWeakPtr(), bitstream_buffer.id())); | 952 GetWeakPtr(), bitstream_buffer.id())); |
922 } | 953 } |
923 } | 954 } |
924 | 955 |
925 void AndroidVideoDecodeAccelerator::DecodeBuffer( | 956 void AndroidVideoDecodeAccelerator::DecodeBuffer( |
926 const BitstreamBuffer& bitstream_buffer) { | 957 const BitstreamBuffer& bitstream_buffer) { |
927 pending_bitstream_records_.push(BitstreamRecord(bitstream_buffer)); | 958 pending_bitstream_records_.push(BitstreamRecord(bitstream_buffer)); |
928 TRACE_COUNTER1("media", "AVDA::PendingBitstreamBufferCount", | 959 TRACE_COUNTER1("media", "AVDA::PendingBitstreamBufferCount", |
929 pending_bitstream_records_.size()); | 960 pending_bitstream_records_.size()); |
930 | 961 |
931 DoIOTask(true); | 962 DoIOTask(true); |
(...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
990 } | 1021 } |
991 | 1022 |
992 void AndroidVideoDecodeAccelerator::Flush() { | 1023 void AndroidVideoDecodeAccelerator::Flush() { |
993 DVLOG(1) << __func__; | 1024 DVLOG(1) << __func__; |
994 DCHECK(thread_checker_.CalledOnValidThread()); | 1025 DCHECK(thread_checker_.CalledOnValidThread()); |
995 StartCodecDrain(DRAIN_FOR_FLUSH); | 1026 StartCodecDrain(DRAIN_FOR_FLUSH); |
996 } | 1027 } |
997 | 1028 |
998 void AndroidVideoDecodeAccelerator::ConfigureMediaCodecAsynchronously() { | 1029 void AndroidVideoDecodeAccelerator::ConfigureMediaCodecAsynchronously() { |
999 DCHECK(thread_checker_.CalledOnValidThread()); | 1030 DCHECK(thread_checker_.CalledOnValidThread()); |
1031 DCHECK(!media_codec_); | |
1000 | 1032 |
1001 DCHECK_NE(state_, WAITING_FOR_CODEC); | 1033 DCHECK_NE(state_, WAITING_FOR_CODEC); |
1002 state_ = WAITING_FOR_CODEC; | 1034 state_ = WAITING_FOR_CODEC; |
1003 | 1035 |
1004 ReleaseCodec(); | |
1005 | |
1006 base::Optional<TaskType> task_type = | 1036 base::Optional<TaskType> task_type = |
1007 codec_allocator_->TaskTypeForAllocation(); | 1037 codec_allocator_->TaskTypeForAllocation(); |
1008 if (!task_type) { | 1038 if (!task_type) { |
1009 // If there is no free thread, then just fail. | 1039 // If there is no free thread, then just fail. |
1010 OnCodecConfigured(nullptr); | 1040 OnCodecConfigured(nullptr); |
1011 return; | 1041 return; |
1012 } | 1042 } |
1013 | 1043 |
1014 // If autodetection is disallowed, fall back to Chrome's software decoders | 1044 // If autodetection is disallowed, fall back to Chrome's software decoders |
1015 // instead of using the software decoders provided by MediaCodec. | 1045 // instead of using the software decoders provided by MediaCodec. |
1016 if (task_type == TaskType::SW_CODEC && | 1046 if (task_type == TaskType::SW_CODEC && |
1017 IsMediaCodecSoftwareDecodingForbidden()) { | 1047 IsMediaCodecSoftwareDecodingForbidden()) { |
1018 OnCodecConfigured(nullptr); | 1048 OnCodecConfigured(nullptr); |
1019 return; | 1049 return; |
1020 } | 1050 } |
1021 | 1051 |
1022 codec_config_->task_type = task_type.value(); | 1052 codec_config_->task_type = task_type.value(); |
1023 codec_allocator_->CreateMediaCodecAsync(weak_this_factory_.GetWeakPtr(), | 1053 codec_allocator_->CreateMediaCodecAsync(GetWeakPtr(), codec_config_); |
1024 codec_config_); | |
1025 } | 1054 } |
1026 | 1055 |
1027 void AndroidVideoDecodeAccelerator::ConfigureMediaCodecSynchronously() { | 1056 void AndroidVideoDecodeAccelerator::ConfigureMediaCodecSynchronously() { |
1028 DCHECK(thread_checker_.CalledOnValidThread()); | 1057 DCHECK(thread_checker_.CalledOnValidThread()); |
1029 DCHECK(!media_codec_); | 1058 DCHECK(!media_codec_); |
1030 DCHECK_NE(state_, WAITING_FOR_CODEC); | 1059 DCHECK_NE(state_, WAITING_FOR_CODEC); |
1031 state_ = WAITING_FOR_CODEC; | 1060 state_ = WAITING_FOR_CODEC; |
1032 | 1061 |
1033 base::Optional<TaskType> task_type = | 1062 base::Optional<TaskType> task_type = |
1034 codec_allocator_->TaskTypeForAllocation(); | 1063 codec_allocator_->TaskTypeForAllocation(); |
1035 if (!task_type) { | 1064 if (!task_type) { |
1036 // If there is no free thread, then just fail. | 1065 // If there is no free thread, then just fail. |
1037 OnCodecConfigured(nullptr); | 1066 OnCodecConfigured(nullptr); |
1038 return; | 1067 return; |
1039 } | 1068 } |
1040 | 1069 |
1041 codec_config_->task_type = task_type.value(); | 1070 codec_config_->task_type = task_type.value(); |
1042 std::unique_ptr<MediaCodecBridge> media_codec = | 1071 std::unique_ptr<MediaCodecBridge> media_codec = |
1043 AVDACodecAllocator::GetInstance()->CreateMediaCodecSync(codec_config_); | 1072 codec_allocator_->CreateMediaCodecSync(codec_config_); |
1044 // Note that |media_codec| might be null, which will NotifyError. | 1073 // Note that |media_codec| might be null, which will NotifyError. |
1045 OnCodecConfigured(std::move(media_codec)); | 1074 OnCodecConfigured(std::move(media_codec)); |
1046 } | 1075 } |
1047 | 1076 |
1048 void AndroidVideoDecodeAccelerator::OnCodecConfigured( | 1077 void AndroidVideoDecodeAccelerator::OnCodecConfigured( |
1049 std::unique_ptr<MediaCodecBridge> media_codec) { | 1078 std::unique_ptr<MediaCodecBridge> media_codec) { |
1050 DCHECK(thread_checker_.CalledOnValidThread()); | 1079 DCHECK(thread_checker_.CalledOnValidThread()); |
1051 DCHECK(state_ == WAITING_FOR_CODEC || state_ == SURFACE_DESTROYED); | 1080 DCHECK(state_ == WAITING_FOR_CODEC || state_ == SURFACE_DESTROYED); |
1052 // If we are supposed to notify that initialization is complete, then do so | 1081 // If we are supposed to notify that initialization is complete, then do so |
1053 // before returning. Otherwise, this is a reconfiguration. | 1082 // before returning. Otherwise, this is a reconfiguration. |
1054 | 1083 |
1084 DCHECK(!media_codec_); | |
1085 media_codec_ = std::move(media_codec); | |
1086 | |
1055 // If |state_| changed to SURFACE_DESTROYED while we were configuring a codec, | 1087 // If |state_| changed to SURFACE_DESTROYED while we were configuring a codec, |
1056 // then the codec is already invalid so we return early and drop it. | 1088 // then the codec is already invalid so we return early and drop it. |
1057 // TODO(liberato): We're going to drop the codec when |media_codec| goes out | |
1058 // of scope, on this thread. We really should post it to the proper thread | |
1059 // to avoid potentially hanging. | |
1060 if (state_ == SURFACE_DESTROYED) { | 1089 if (state_ == SURFACE_DESTROYED) { |
1061 if (deferred_initialization_pending_) { | 1090 if (deferred_initialization_pending_) { |
1062 // Losing the output surface is not considered an error state, so notify | 1091 // Losing the output surface is not considered an error state, so notify |
1063 // success. The client will destroy this soon. | 1092 // success. The client will destroy |this| soon. |
1064 NotifyInitializationSucceeded(); | 1093 NotifyInitializationSucceeded(); |
1065 } | 1094 } |
1095 | |
1096 // Post it to the right thread. | |
1097 ReleaseCodecAndBundle(); | |
1066 return; | 1098 return; |
1067 } | 1099 } |
1068 | 1100 |
1069 DCHECK(!media_codec_); | |
1070 media_codec_ = std::move(media_codec); | |
1071 picture_buffer_manager_.CodecChanged(media_codec_.get()); | 1101 picture_buffer_manager_.CodecChanged(media_codec_.get()); |
1072 if (!media_codec_) { | 1102 if (!media_codec_) { |
1073 NOTIFY_ERROR(PLATFORM_FAILURE, "Failed to create MediaCodec"); | 1103 NOTIFY_ERROR(PLATFORM_FAILURE, "Failed to create MediaCodec"); |
1074 return; | 1104 return; |
1075 } | 1105 } |
1076 | 1106 |
1077 if (deferred_initialization_pending_) | 1107 if (deferred_initialization_pending_) |
1078 NotifyInitializationSucceeded(); | 1108 NotifyInitializationSucceeded(); |
1079 | 1109 |
1080 state_ = NO_ERROR; | 1110 state_ = NO_ERROR; |
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1127 if (!drain_type_) { | 1157 if (!drain_type_) { |
1128 NOTIFY_ERROR(PLATFORM_FAILURE, "Unexpected EOS"); | 1158 NOTIFY_ERROR(PLATFORM_FAILURE, "Unexpected EOS"); |
1129 return; | 1159 return; |
1130 } | 1160 } |
1131 | 1161 |
1132 switch (*drain_type_) { | 1162 switch (*drain_type_) { |
1133 case DRAIN_FOR_FLUSH: | 1163 case DRAIN_FOR_FLUSH: |
1134 ResetCodecState(); | 1164 ResetCodecState(); |
1135 base::ThreadTaskRunnerHandle::Get()->PostTask( | 1165 base::ThreadTaskRunnerHandle::Get()->PostTask( |
1136 FROM_HERE, base::Bind(&AndroidVideoDecodeAccelerator::NotifyFlushDone, | 1166 FROM_HERE, base::Bind(&AndroidVideoDecodeAccelerator::NotifyFlushDone, |
1137 weak_this_factory_.GetWeakPtr())); | 1167 GetWeakPtr())); |
1138 break; | 1168 break; |
1139 case DRAIN_FOR_RESET: | 1169 case DRAIN_FOR_RESET: |
1140 ResetCodecState(); | 1170 ResetCodecState(); |
1141 base::ThreadTaskRunnerHandle::Get()->PostTask( | 1171 base::ThreadTaskRunnerHandle::Get()->PostTask( |
1142 FROM_HERE, base::Bind(&AndroidVideoDecodeAccelerator::NotifyResetDone, | 1172 FROM_HERE, base::Bind(&AndroidVideoDecodeAccelerator::NotifyResetDone, |
1143 weak_this_factory_.GetWeakPtr())); | 1173 GetWeakPtr())); |
1144 break; | 1174 break; |
1145 case DRAIN_FOR_DESTROY: | 1175 case DRAIN_FOR_DESTROY: |
1146 base::ThreadTaskRunnerHandle::Get()->PostTask( | 1176 base::ThreadTaskRunnerHandle::Get()->PostTask( |
1147 FROM_HERE, base::Bind(&AndroidVideoDecodeAccelerator::ActualDestroy, | 1177 FROM_HERE, base::Bind(&AndroidVideoDecodeAccelerator::ActualDestroy, |
1148 weak_this_factory_.GetWeakPtr())); | 1178 GetWeakPtr())); |
1149 break; | 1179 break; |
1150 } | 1180 } |
1151 drain_type_.reset(); | 1181 drain_type_.reset(); |
1152 } | 1182 } |
1153 | 1183 |
1154 void AndroidVideoDecodeAccelerator::ResetCodecState() { | 1184 void AndroidVideoDecodeAccelerator::ResetCodecState() { |
1155 DCHECK(thread_checker_.CalledOnValidThread()); | 1185 DCHECK(thread_checker_.CalledOnValidThread()); |
1156 | 1186 |
1157 // If there is already a reset in flight, then that counts. This can really | 1187 // If there is already a reset in flight, then that counts. This can really |
1158 // only happen if somebody calls Reset. | 1188 // only happen if somebody calls Reset. |
1159 // If the surface is destroyed or we're in an error state there's nothing to | 1189 // If the surface is destroyed or we're in an error state there's nothing to |
1160 // do. Note that WAITING_FOR_SURFACE implies that we have no codec, but it's | 1190 // do. Note that BEFORE_OVERLAY_INIT implies that we have no codec, but it's |
1161 // included for completeness. | 1191 // included for completeness. |
1162 if (state_ == WAITING_FOR_CODEC || state_ == SURFACE_DESTROYED || | 1192 if (state_ == WAITING_FOR_CODEC || state_ == SURFACE_DESTROYED || |
1163 state_ == WAITING_FOR_SURFACE || state_ == ERROR || !media_codec_) { | 1193 state_ == BEFORE_OVERLAY_INIT || state_ == ERROR || !media_codec_) { |
1164 return; | 1194 return; |
1165 } | 1195 } |
1166 | 1196 |
1167 bitstream_buffers_in_decoder_.clear(); | 1197 bitstream_buffers_in_decoder_.clear(); |
1168 | 1198 |
1169 if (pending_input_buf_index_ != -1) { | 1199 if (pending_input_buf_index_ != -1) { |
1170 // The data for that index exists in the input buffer, but corresponding | 1200 // The data for that index exists in the input buffer, but corresponding |
1171 // shm block been deleted. Check that it is safe to flush the codec, i.e. | 1201 // shm block been deleted. Check that it is safe to flush the codec, i.e. |
1172 // |pending_bitstream_records_| is empty. | 1202 // |pending_bitstream_records_| is empty. |
1173 // TODO(timav): keep shm block for that buffer and remove this restriction. | 1203 // TODO(timav): keep shm block for that buffer and remove this restriction. |
(...skipping 12 matching lines...) Expand all Loading... | |
1186 // Flush the codec if possible, or create a new one if not. | 1216 // Flush the codec if possible, or create a new one if not. |
1187 if (!MediaCodecUtil::CodecNeedsFlushWorkaround(media_codec_.get())) { | 1217 if (!MediaCodecUtil::CodecNeedsFlushWorkaround(media_codec_.get())) { |
1188 DVLOG(3) << __func__ << " Flushing MediaCodec."; | 1218 DVLOG(3) << __func__ << " Flushing MediaCodec."; |
1189 media_codec_->Flush(); | 1219 media_codec_->Flush(); |
1190 // Since we just flushed all the output buffers, make sure that nothing is | 1220 // Since we just flushed all the output buffers, make sure that nothing is |
1191 // using them. | 1221 // using them. |
1192 picture_buffer_manager_.CodecChanged(media_codec_.get()); | 1222 picture_buffer_manager_.CodecChanged(media_codec_.get()); |
1193 } else { | 1223 } else { |
1194 DVLOG(3) << __func__ << " Deleting the MediaCodec and creating a new one."; | 1224 DVLOG(3) << __func__ << " Deleting the MediaCodec and creating a new one."; |
1195 GetManager()->StopTimer(this); | 1225 GetManager()->StopTimer(this); |
1196 // Note that this will release the codec, then allocate a new one. It will | 1226 // Release the codec, retain the bundle, and allocate a new codec. It will |
1197 // not wait for the old one to finish up with the surface, which is bad. | 1227 // not wait for the old one to finish up with the bundle, which is bad. It |
1198 // It works (usually) because it ends up allocating the codec on the same | 1228 // works (usually) because it ends up allocating the codec on the same |
1199 // thread as is used to release the old one, so it's serialized anyway. | 1229 // thread as is used to release the old one, so it's serialized anyway. |
1230 ReleaseCodec(); | |
1200 ConfigureMediaCodecAsynchronously(); | 1231 ConfigureMediaCodecAsynchronously(); |
1201 } | 1232 } |
1202 } | 1233 } |
1203 | 1234 |
1204 void AndroidVideoDecodeAccelerator::Reset() { | 1235 void AndroidVideoDecodeAccelerator::Reset() { |
1205 DVLOG(1) << __func__; | 1236 DVLOG(1) << __func__; |
1206 DCHECK(thread_checker_.CalledOnValidThread()); | 1237 DCHECK(thread_checker_.CalledOnValidThread()); |
1207 TRACE_EVENT0("media", "AVDA::Reset"); | 1238 TRACE_EVENT0("media", "AVDA::Reset"); |
1208 | 1239 |
1209 if (defer_surface_creation_) { | 1240 if (defer_surface_creation_) { |
1210 DCHECK(!media_codec_); | 1241 DCHECK(!media_codec_); |
1211 DCHECK(pending_bitstream_records_.empty()); | 1242 DCHECK(pending_bitstream_records_.empty()); |
1212 DCHECK_EQ(state_, WAITING_FOR_SURFACE); | 1243 DCHECK_EQ(state_, BEFORE_OVERLAY_INIT); |
1213 base::ThreadTaskRunnerHandle::Get()->PostTask( | 1244 base::ThreadTaskRunnerHandle::Get()->PostTask( |
1214 FROM_HERE, base::Bind(&AndroidVideoDecodeAccelerator::NotifyResetDone, | 1245 FROM_HERE, base::Bind(&AndroidVideoDecodeAccelerator::NotifyResetDone, |
1215 weak_this_factory_.GetWeakPtr())); | 1246 GetWeakPtr())); |
1216 return; | 1247 return; |
1217 } | 1248 } |
1218 | 1249 |
1219 while (!pending_bitstream_records_.empty()) { | 1250 while (!pending_bitstream_records_.empty()) { |
1220 int32_t bitstream_buffer_id = | 1251 int32_t bitstream_buffer_id = |
1221 pending_bitstream_records_.front().buffer.id(); | 1252 pending_bitstream_records_.front().buffer.id(); |
1222 pending_bitstream_records_.pop(); | 1253 pending_bitstream_records_.pop(); |
1223 | 1254 |
1224 if (bitstream_buffer_id != -1) { | 1255 if (bitstream_buffer_id != -1) { |
1225 base::ThreadTaskRunnerHandle::Get()->PostTask( | 1256 base::ThreadTaskRunnerHandle::Get()->PostTask( |
1226 FROM_HERE, | 1257 FROM_HERE, |
1227 base::Bind(&AndroidVideoDecodeAccelerator::NotifyEndOfBitstreamBuffer, | 1258 base::Bind(&AndroidVideoDecodeAccelerator::NotifyEndOfBitstreamBuffer, |
1228 weak_this_factory_.GetWeakPtr(), bitstream_buffer_id)); | 1259 GetWeakPtr(), bitstream_buffer_id)); |
1229 } | 1260 } |
1230 } | 1261 } |
1231 TRACE_COUNTER1("media", "AVDA::PendingBitstreamBufferCount", 0); | 1262 TRACE_COUNTER1("media", "AVDA::PendingBitstreamBufferCount", 0); |
1232 bitstreams_notified_in_advance_.clear(); | 1263 bitstreams_notified_in_advance_.clear(); |
1233 | 1264 |
1234 picture_buffer_manager_.ReleaseCodecBuffers(output_picture_buffers_); | 1265 picture_buffer_manager_.ReleaseCodecBuffers(output_picture_buffers_); |
1235 StartCodecDrain(DRAIN_FOR_RESET); | 1266 StartCodecDrain(DRAIN_FOR_RESET); |
1236 } | 1267 } |
1237 | 1268 |
1238 void AndroidVideoDecodeAccelerator::SetSurface(int32_t surface_id) { | 1269 void AndroidVideoDecodeAccelerator::SetSurface(int32_t surface_id) { |
1239 DVLOG(1) << __func__; | 1270 DVLOG(1) << __func__; |
1240 DCHECK(thread_checker_.CalledOnValidThread()); | 1271 DCHECK(thread_checker_.CalledOnValidThread()); |
1241 | 1272 |
1242 if (surface_id == this->surface_id()) { | 1273 // It's possible that we'll receive a SetSurface before initializing the |
1243 pending_surface_id_.reset(); | 1274 // overlay helper. For example, if we defer surface creation, then we'll |
1275 // signal success to WMPI before initializing it. WMPI is free to change the | |
1276 // surface. In this case, just pretend that |surface_id| is the initial one. | |
1277 if (state_ == BEFORE_OVERLAY_INIT) { | |
1278 config_.surface_id = surface_id; | |
1244 return; | 1279 return; |
1245 } | 1280 } |
1246 | 1281 |
1247 // Surface changes never take effect immediately, they will be handled during | 1282 std::unique_ptr<AndroidOverlayFactory> factory; |
1248 // DequeOutput() once we get to a good switch point or immediately during an | 1283 if (surface_id != SurfaceManager::kNoSurfaceID) |
1249 // OnSurfaceDestroyed() call. | 1284 factory = base::MakeUnique<ContentVideoViewOverlayFactory>(surface_id); |
1250 pending_surface_id_ = surface_id; | 1285 |
1286 overlay_helper_->OnOverlayFactory(std::move(factory)); | |
1251 } | 1287 } |
1252 | 1288 |
1253 void AndroidVideoDecodeAccelerator::Destroy() { | 1289 void AndroidVideoDecodeAccelerator::Destroy() { |
1254 DVLOG(1) << __func__; | 1290 DVLOG(1) << __func__; |
1255 DCHECK(thread_checker_.CalledOnValidThread()); | 1291 DCHECK(thread_checker_.CalledOnValidThread()); |
1256 | 1292 |
1257 picture_buffer_manager_.Destroy(output_picture_buffers_); | 1293 picture_buffer_manager_.Destroy(output_picture_buffers_); |
1258 client_ = nullptr; | 1294 client_ = nullptr; |
1259 | 1295 |
1260 // We don't want to queue more inputs while draining. | 1296 // We don't want to queue more inputs while draining. |
1261 std::queue<BitstreamRecord>().swap(pending_bitstream_records_); | 1297 std::queue<BitstreamRecord>().swap(pending_bitstream_records_); |
1262 StartCodecDrain(DRAIN_FOR_DESTROY); | 1298 StartCodecDrain(DRAIN_FOR_DESTROY); |
1263 } | 1299 } |
1264 | 1300 |
1265 void AndroidVideoDecodeAccelerator::ActualDestroy() { | 1301 void AndroidVideoDecodeAccelerator::ActualDestroy() { |
1266 DVLOG(1) << __func__; | 1302 DVLOG(1) << __func__; |
1267 DCHECK(thread_checker_.CalledOnValidThread()); | 1303 DCHECK(thread_checker_.CalledOnValidThread()); |
1268 | 1304 |
1269 // Note that async codec construction might still be in progress. In that | 1305 // Note that async codec construction might still be in progress. In that |
1270 // case, the codec will be deleted when it completes once we invalidate all | 1306 // case, the codec will be deleted when it completes once we invalidate all |
1271 // our weak refs. | 1307 // our weak refs. |
1272 weak_this_factory_.InvalidateWeakPtrs(); | 1308 weak_this_factory_.InvalidateWeakPtrs(); |
1273 GetManager()->StopTimer(this); | 1309 GetManager()->StopTimer(this); |
1274 ReleaseCodec(); | 1310 ReleaseCodecAndBundle(); |
1275 | 1311 |
1276 delete this; | 1312 delete this; |
1277 } | 1313 } |
1278 | 1314 |
1279 bool AndroidVideoDecodeAccelerator::TryToSetupDecodeOnSeparateThread( | 1315 bool AndroidVideoDecodeAccelerator::TryToSetupDecodeOnSeparateThread( |
1280 const base::WeakPtr<Client>& decode_client, | 1316 const base::WeakPtr<Client>& decode_client, |
1281 const scoped_refptr<base::SingleThreadTaskRunner>& decode_task_runner) { | 1317 const scoped_refptr<base::SingleThreadTaskRunner>& decode_task_runner) { |
1282 return false; | 1318 return false; |
1283 } | 1319 } |
1284 | 1320 |
1285 const gfx::Size& AndroidVideoDecodeAccelerator::GetSize() const { | 1321 const gfx::Size& AndroidVideoDecodeAccelerator::GetSize() const { |
1286 return size_; | 1322 return size_; |
1287 } | 1323 } |
1288 | 1324 |
1289 base::WeakPtr<gpu::gles2::GLES2Decoder> | 1325 base::WeakPtr<gpu::gles2::GLES2Decoder> |
1290 AndroidVideoDecodeAccelerator::GetGlDecoder() const { | 1326 AndroidVideoDecodeAccelerator::GetGlDecoder() const { |
1291 return get_gles2_decoder_cb_.Run(); | 1327 return get_gles2_decoder_cb_.Run(); |
1292 } | 1328 } |
1293 | 1329 |
1294 void AndroidVideoDecodeAccelerator::OnSurfaceDestroyed( | 1330 void AndroidVideoDecodeAccelerator::OnStopUsingOverlayImmediately( |
1295 AndroidOverlay* overlay) { | 1331 AndroidOverlay* overlay) { |
1296 DVLOG(1) << __func__; | 1332 DVLOG(1) << __func__; |
1297 TRACE_EVENT0("media", "AVDA::OnSurfaceDestroyed"); | 1333 TRACE_EVENT0("media", "AVDA::OnStopUsingOverlayImmediately"); |
1298 DCHECK(thread_checker_.CalledOnValidThread()); | 1334 DCHECK(thread_checker_.CalledOnValidThread()); |
1299 | 1335 |
1300 // We cannot get here if we're before surface allocation, since we transition | 1336 // We cannot get here if we're before surface allocation, since we transition |
1301 // to WAITING_FOR_CODEC (or NO_ERROR, if sync) when we get the surface without | 1337 // to WAITING_FOR_CODEC (or NO_ERROR, if sync) when we get the surface without |
1302 // posting. If we do ever lose the surface before starting codec allocation, | 1338 // posting. If we do ever lose the surface before starting codec allocation, |
1303 // then we could just update the config to use a SurfaceTexture and return | 1339 // then we could just update the config to use a SurfaceTexture and return |
1304 // without changing state. | 1340 // without changing state. |
1305 DCHECK_NE(state_, WAITING_FOR_SURFACE); | 1341 DCHECK_NE(state_, BEFORE_OVERLAY_INIT); |
1342 | |
1343 // If we're transitioning to |overlay|, then just stop here. We're not also | |
1344 // using the overlay if we're transitioning to it. | |
1345 if (incoming_overlay_ && incoming_overlay_->get() == overlay) { | |
1346 incoming_overlay_.reset(); | |
1347 return; | |
1348 } | |
1349 | |
1350 // If we have no codec, or if our current config doesn't refer to |overlay|, | |
1351 // then do nothing. |overlay| might be for some overlay that's waiting for | |
1352 // codec destruction on some other thread. | |
1353 if (!codec_config_->surface_bundle || | |
1354 codec_config_->surface_bundle->overlay.get() != overlay) { | |
1355 return; | |
1356 } | |
1357 | |
1358 // If we have a codec, or if codec allocation is in flight, then it's using an | |
1359 // overlay that was destroyed. | |
1360 if (state_ == WAITING_FOR_CODEC) { | |
1361 // What we should do here is to set |incoming_overlay_| to nullptr, to start | |
1362 // a transistion to SurfaceTexture. OnCodecConfigured could notice that | |
1363 // there's an incoming overlay, and then immediately transition the codec / | |
1364 // drop and re-allocate the codec using it. However, for CVV, that won't | |
1365 // work, since CVV-based overlays block the main thread waiting for the | |
1366 // overlay to be dropped, so OnCodecConfigured won't run. For DS, it's the | |
1367 // right thing. | |
1368 // So, for now, we just fail, and let OnCodecConfigured drop the codec. | |
1369 // Note that this case really can only happen on pre-M anyway, unless it's | |
1370 // during initial construction. This will result in the overlay being | |
1371 // destroyed after timeout, since OnCodecConfigured can't run until the | |
1372 // synchronous CVV destruction quits. | |
1373 state_ = SURFACE_DESTROYED; | |
1374 return; | |
1375 } | |
1306 | 1376 |
1307 // If the API is available avoid having to restart the decoder in order to | 1377 // If the API is available avoid having to restart the decoder in order to |
1308 // leave fullscreen. If we don't clear the surface immediately during this | 1378 // leave fullscreen. If we don't clear the surface immediately during this |
1309 // callback, the MediaCodec will throw an error as the surface is destroyed. | 1379 // callback, the MediaCodec will throw an error as the surface is destroyed. |
1310 if (base::android::BuildInfo::GetInstance()->sdk_int() >= 23) { | 1380 if (platform_config_.allow_setsurface) { |
1311 // Since we can't wait for a transition, we must invalidate all outstanding | 1381 // Since we can't wait for a transition, we must invalidate all outstanding |
1312 // picture buffers to avoid putting the GL system in a broken state. | 1382 // picture buffers to avoid putting the GL system in a broken state. |
1313 picture_buffer_manager_.ReleaseCodecBuffers(output_picture_buffers_); | 1383 picture_buffer_manager_.ReleaseCodecBuffers(output_picture_buffers_); |
1314 | 1384 |
1315 // Switch away from the surface being destroyed to a surface texture. | 1385 // If we aren't transitioning to some other surface, then transition to a |
1316 DCHECK_NE(surface_id(), SurfaceManager::kNoSurfaceID); | 1386 // SurfaceTexture. Remember that, if |incoming_overlay_| is an overlay, |
1387 // then it's already ready and can be transitioned to immediately. We were | |
1388 // just waiting for codec buffers to come back, but we just dropped them. | |
1389 // Note that we want |incoming_overlay_| to has_value(), but that value | |
1390 // should be a nullptr to indicate that we should switch to SurfaceTexture. | |
1391 if (!incoming_overlay_) | |
1392 incoming_overlay_ = std::unique_ptr<AndroidOverlay>(); | |
1317 | 1393 |
1318 // The leaving fullscreen notification may come in before this point. | |
1319 if (pending_surface_id_) | |
1320 DCHECK_EQ(pending_surface_id_.value(), SurfaceManager::kNoSurfaceID); | |
1321 | |
1322 pending_surface_id_ = SurfaceManager::kNoSurfaceID; | |
1323 UpdateSurface(); | 1394 UpdateSurface(); |
1324 // Switching to a SurfaceTexture should never need to wait. If it does, | 1395 // Switching to a SurfaceTexture should never need to wait. If it does, |
1325 // then the codec might still be using the destroyed surface, which is bad. | 1396 // then the codec might still be using the destroyed surface, which is bad. |
1326 DCHECK_NE(state_, WAITING_FOR_SURFACE); | |
1327 return; | 1397 return; |
1328 } | 1398 } |
1329 | 1399 |
1330 // If we're currently asynchronously configuring a codec, it will be destroyed | 1400 // If we're currently asynchronously configuring a codec, it will be destroyed |
1331 // when configuration completes and it notices that |state_| has changed to | 1401 // when configuration completes and it notices that |state_| has changed to |
1332 // SURFACE_DESTROYED. | 1402 // SURFACE_DESTROYED. |
1333 state_ = SURFACE_DESTROYED; | 1403 state_ = SURFACE_DESTROYED; |
1334 ReleaseCodec(); | 1404 ReleaseCodecAndBundle(); |
1335 | 1405 |
1336 // If we're draining, signal completion now because the drain can no longer | 1406 // If we're draining, signal completion now because the drain can no longer |
1337 // proceed. | 1407 // proceed. |
1338 if (drain_type_) | 1408 if (drain_type_) |
1339 OnDrainCompleted(); | 1409 OnDrainCompleted(); |
1340 } | 1410 } |
1341 | 1411 |
1342 void AndroidVideoDecodeAccelerator::InitializeCdm() { | 1412 void AndroidVideoDecodeAccelerator::InitializeCdm() { |
1343 DVLOG(2) << __func__ << ": " << config_.cdm_id; | 1413 DVLOG(2) << __func__ << ": " << config_.cdm_id; |
1344 | 1414 |
(...skipping 13 matching lines...) Expand all Loading... | |
1358 | 1428 |
1359 // Register CDM callbacks. The callbacks registered will be posted back to | 1429 // Register CDM callbacks. The callbacks registered will be posted back to |
1360 // this thread via BindToCurrentLoop. | 1430 // this thread via BindToCurrentLoop. |
1361 | 1431 |
1362 // Since |this| holds a reference to the |cdm_|, by the time the CDM is | 1432 // Since |this| holds a reference to the |cdm_|, by the time the CDM is |
1363 // destructed, UnregisterPlayer() must have been called and |this| has been | 1433 // destructed, UnregisterPlayer() must have been called and |this| has been |
1364 // destructed as well. So the |cdm_unset_cb| will never have a chance to be | 1434 // destructed as well. So the |cdm_unset_cb| will never have a chance to be |
1365 // called. | 1435 // called. |
1366 // TODO(xhwang): Remove |cdm_unset_cb| after it's not used on all platforms. | 1436 // TODO(xhwang): Remove |cdm_unset_cb| after it's not used on all platforms. |
1367 cdm_registration_id_ = media_drm_bridge_cdm_context_->RegisterPlayer( | 1437 cdm_registration_id_ = media_drm_bridge_cdm_context_->RegisterPlayer( |
1368 BindToCurrentLoop(base::Bind(&AndroidVideoDecodeAccelerator::OnKeyAdded, | 1438 BindToCurrentLoop( |
1369 weak_this_factory_.GetWeakPtr())), | 1439 base::Bind(&AndroidVideoDecodeAccelerator::OnKeyAdded, GetWeakPtr())), |
1370 base::Bind(&base::DoNothing)); | 1440 base::Bind(&base::DoNothing)); |
1371 | 1441 |
1372 // Deferred initialization will continue in OnMediaCryptoReady(). | 1442 // Deferred initialization will continue in OnMediaCryptoReady(). |
1373 media_drm_bridge_cdm_context_->SetMediaCryptoReadyCB(BindToCurrentLoop( | 1443 media_drm_bridge_cdm_context_->SetMediaCryptoReadyCB( |
1374 base::Bind(&AndroidVideoDecodeAccelerator::OnMediaCryptoReady, | 1444 BindToCurrentLoop(base::Bind( |
1375 weak_this_factory_.GetWeakPtr()))); | 1445 &AndroidVideoDecodeAccelerator::OnMediaCryptoReady, GetWeakPtr()))); |
1376 #endif // !defined(ENABLE_MOJO_MEDIA_IN_GPU_PROCESS) | 1446 #endif // !defined(ENABLE_MOJO_MEDIA_IN_GPU_PROCESS) |
1377 } | 1447 } |
1378 | 1448 |
1379 void AndroidVideoDecodeAccelerator::OnMediaCryptoReady( | 1449 void AndroidVideoDecodeAccelerator::OnMediaCryptoReady( |
1380 MediaDrmBridgeCdmContext::JavaObjectPtr media_crypto, | 1450 MediaDrmBridgeCdmContext::JavaObjectPtr media_crypto, |
1381 bool needs_protected_surface) { | 1451 bool needs_protected_surface) { |
1382 DVLOG(1) << __func__; | 1452 DVLOG(1) << __func__; |
1383 | 1453 |
1384 if (!media_crypto) { | 1454 if (!media_crypto) { |
1385 LOG(ERROR) << "MediaCrypto is not available, can't play encrypted stream."; | 1455 LOG(ERROR) << "MediaCrypto is not available, can't play encrypted stream."; |
1386 cdm_for_reference_holding_only_ = nullptr; | 1456 cdm_for_reference_holding_only_ = nullptr; |
1387 media_drm_bridge_cdm_context_ = nullptr; | 1457 media_drm_bridge_cdm_context_ = nullptr; |
1388 NOTIFY_ERROR(PLATFORM_FAILURE, "MediaCrypto is not available"); | 1458 NOTIFY_ERROR(PLATFORM_FAILURE, "MediaCrypto is not available"); |
1389 return; | 1459 return; |
1390 } | 1460 } |
1391 | 1461 |
1392 DCHECK(!media_crypto->is_null()); | 1462 DCHECK(!media_crypto->is_null()); |
1393 | 1463 |
1394 // We assume this is a part of the initialization process, thus MediaCodec | 1464 // We assume this is a part of the initialization process, thus MediaCodec |
1395 // is not created yet. | 1465 // is not created yet. |
1396 DCHECK(!media_codec_); | 1466 DCHECK(!media_codec_); |
1397 DCHECK(deferred_initialization_pending_); | 1467 DCHECK(deferred_initialization_pending_); |
1398 | 1468 |
1399 codec_config_->media_crypto = std::move(media_crypto); | 1469 codec_config_->media_crypto = std::move(media_crypto); |
1400 codec_config_->needs_protected_surface = needs_protected_surface; | 1470 codec_config_->needs_protected_surface = needs_protected_surface; |
1401 | 1471 |
1402 // After receiving |media_crypto_| we can start with surface creation. | 1472 // After receiving |media_crypto_| we can start with surface creation. |
1403 StartSurfaceCreation(); | 1473 StartOverlayHelper(); |
1404 } | 1474 } |
1405 | 1475 |
1406 void AndroidVideoDecodeAccelerator::OnKeyAdded() { | 1476 void AndroidVideoDecodeAccelerator::OnKeyAdded() { |
1407 DVLOG(1) << __func__; | 1477 DVLOG(1) << __func__; |
1408 | 1478 |
1409 // This can also be called before initial surface allocation has completed, | 1479 // This can also be called before initial surface allocation has completed, |
1410 // so we might not have a surface / codec yet. In that case, we'll never | 1480 // so we might not have a surface / codec yet. In that case, we'll never |
1411 // transition to WAITING_FOR_KEY, which is fine. | 1481 // transition to WAITING_FOR_KEY, which is fine. |
1412 if (state_ == WAITING_FOR_KEY) | 1482 if (state_ == WAITING_FOR_KEY) |
1413 state_ = NO_ERROR; | 1483 state_ = NO_ERROR; |
(...skipping 169 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1583 | 1653 |
1584 bool AndroidVideoDecodeAccelerator::IsMediaCodecSoftwareDecodingForbidden() | 1654 bool AndroidVideoDecodeAccelerator::IsMediaCodecSoftwareDecodingForbidden() |
1585 const { | 1655 const { |
1586 // Prevent MediaCodec from using its internal software decoders when we have | 1656 // Prevent MediaCodec from using its internal software decoders when we have |
1587 // more secure and up to date versions in the renderer process. | 1657 // more secure and up to date versions in the renderer process. |
1588 return !config_.is_encrypted() && (codec_config_->codec == kCodecVP8 || | 1658 return !config_.is_encrypted() && (codec_config_->codec == kCodecVP8 || |
1589 codec_config_->codec == kCodecVP9); | 1659 codec_config_->codec == kCodecVP9); |
1590 } | 1660 } |
1591 | 1661 |
1592 bool AndroidVideoDecodeAccelerator::UpdateSurface() { | 1662 bool AndroidVideoDecodeAccelerator::UpdateSurface() { |
1593 DCHECK(pending_surface_id_); | 1663 DCHECK(incoming_overlay_); |
1594 DCHECK_NE(surface_id(), pending_surface_id_.value()); | |
1595 DCHECK(surface_id() == SurfaceManager::kNoSurfaceID || | |
1596 pending_surface_id_.value() == SurfaceManager::kNoSurfaceID); | |
1597 | |
1598 const int new_surface_id = pending_surface_id_.value(); | |
1599 pending_surface_id_.reset(); | |
1600 | 1664 |
1601 // Start surface creation. Note that if we're called via surfaceDestroyed, | 1665 // Start surface creation. Note that if we're called via surfaceDestroyed, |
1602 // then this must complete synchronously or it will DCHECK. Otherwise, we | 1666 // then this must complete synchronously or it will DCHECK. Otherwise, we |
1603 // might still be using the destroyed surface. We don't enforce this, but | 1667 // might still be using the destroyed surface. We don't enforce this, but |
1604 // it's worth remembering that there are cases where it's required. | 1668 // it's worth remembering that there are cases where it's required. |
1605 // Note that we don't re-use |surface_bundle|, since the codec is using it! | 1669 // Note that we don't re-use |surface_bundle|, since the codec is using it! |
1606 incoming_bundle_ = new AVDASurfaceBundle(new_surface_id); | 1670 incoming_bundle_ = |
1607 StartSurfaceCreation(); | 1671 new AVDASurfaceBundle(std::move(incoming_overlay_.value())); |
1672 incoming_overlay_.reset(); | |
1673 InitializePictureBufferManager(); | |
1608 if (state_ == ERROR) { | 1674 if (state_ == ERROR) { |
1609 // This might be called from OnSurfaceDestroyed(), so we have to release the | 1675 // This might be called from OnSurfaceDestroyed(), so we have to release the |
1610 // MediaCodec if we failed to switch the surface. We reset the surface ID | 1676 // MediaCodec if we failed to switch the surface. We reset the surface ID |
1611 // to the previous one, since failures never result in the codec using the | 1677 // to the previous one, since failures never result in the codec using the |
1612 // new surface. This is only guaranteed because of how OnCodecConfigured | 1678 // new surface. This is only guaranteed because of how OnCodecConfigured |
1613 // works. If it could fail after getting a codec, then this assumption | 1679 // works. If it could fail after getting a codec, then this assumption |
1614 // wouldn't be necessarily true anymore. | 1680 // wouldn't be necessarily true anymore. |
1615 // Also note that we might not have switched surfaces yet, which is also bad | 1681 // Also note that we might not have switched surfaces yet, which is also bad |
1616 // for OnSurfaceDestroyed, because of WAITING_FOR_SURFACE. Shouldn't | 1682 // for OnSurfaceDestroyed, because of BEFORE_OVERLAY_INIT. Shouldn't |
1617 // happen with SurfaceTexture, and OnSurfaceDestroyed checks for it. In | 1683 // happen with SurfaceTexture, and OnSurfaceDestroyed checks for it. In |
1618 // either case, we definitely should not still have an incoming bundle; it | 1684 // either case, we definitely should not still have an incoming bundle; it |
1619 // should have been dropped. | 1685 // should have been dropped. |
1620 DCHECK(!incoming_bundle_); | 1686 DCHECK(!incoming_bundle_); |
1621 ReleaseCodec(); | 1687 ReleaseCodecAndBundle(); |
1622 } | 1688 } |
1623 | 1689 |
1624 return state_ != ERROR; | 1690 return state_ != ERROR; |
1625 } | 1691 } |
1626 | 1692 |
1693 base::WeakPtr<AndroidVideoDecodeAccelerator> | |
1694 AndroidVideoDecodeAccelerator::GetWeakPtr() { | |
1695 return weak_this_factory_.GetWeakPtr(); | |
1696 } | |
1697 | |
1627 void AndroidVideoDecodeAccelerator::ReleaseCodec() { | 1698 void AndroidVideoDecodeAccelerator::ReleaseCodec() { |
1628 if (!media_codec_) | 1699 if (!media_codec_) |
1629 return; | 1700 return; |
1630 | 1701 |
1631 picture_buffer_manager_.CodecChanged(nullptr); | 1702 picture_buffer_manager_.CodecChanged(nullptr); |
1632 codec_allocator_->ReleaseMediaCodec(std::move(media_codec_), | 1703 codec_allocator_->ReleaseMediaCodec(std::move(media_codec_), |
1633 codec_config_->task_type, | 1704 codec_config_->task_type, |
1634 codec_config_->surface_bundle); | 1705 codec_config_->surface_bundle); |
1635 } | 1706 } |
1636 | 1707 |
1708 void AndroidVideoDecodeAccelerator::ReleaseCodecAndBundle() { | |
1709 ReleaseCodec(); | |
1710 codec_config_->surface_bundle = nullptr; | |
1711 } | |
1712 | |
1637 } // namespace media | 1713 } // namespace media |
OLD | NEW |