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* platform_config_for_test) | |
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 (platform_config_for_test) { | |
DaleCurtis
2017/04/20 19:10:43
This will probably trip presubmit scripts; if so,
liberato (no reviews please)
2017/04/20 22:01:38
renamed to |optional_platform_config|. goes bette
| |
255 platform_config_ = *platform_config_for_test; | |
256 } else { | |
257 platform_config_.sdk_int = | |
258 base::android::BuildInfo::GetInstance()->sdk_int(); | |
259 platform_config_.allow_setsurface = | |
260 MediaCodecUtil::IsSetOutputSurfaceSupported(); | |
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 |
DaleCurtis
2017/04/20 19:10:43
Decode() w/o :: is the preferred style.
liberato (no reviews please)
2017/04/20 22:01:38
done, and elsewhere.
| |
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 // Also note that we might choose to defer surface creation for the sync path, |
DaleCurtis
2017/04/20 19:10:42
Line break?
liberato (no reviews please)
2017/04/20 22:01:38
Done.
| |
393 overlay_config.ready_cb = | 396 // which won't get here. We'll exit above, successfully, during init, and |
394 base::Bind(&AndroidVideoDecodeAccelerator::OnOverlayReady, | 397 // will fall through to the below when ::Decode calls us back. That's okay. |
DaleCurtis
2017/04/20 19:10:43
Ditto on naming.
liberato (no reviews please)
2017/04/20 22:01:38
Done.
| |
395 weak_this_factory_.GetWeakPtr()); | 398 // We only handle this case specially since |overlay_helper_| is allowed to |
396 overlay_config.failed_cb = | 399 // post callbacks to us. Here, we guarantee that the sync case is actually |
397 base::Bind(&AndroidVideoDecodeAccelerator::OnOverlayFailed, | 400 // resolved synchronously. The only exception will be if we need to defer |
398 weak_this_factory_.GetWeakPtr()); | 401 // surface creation for other reasons, in which case the sync path with just |
399 overlay_config.destroyed_cb = | 402 // signal success optimistically. |
400 base::Bind(&AndroidVideoDecodeAccelerator::OnSurfaceDestroyed, | 403 if (during_initialize_ && !deferred_initialization_pending_) { |
401 weak_this_factory_.GetWeakPtr()); | 404 DCHECK_EQ(config_.surface_id, SurfaceManager::kNoSurfaceID); |
402 // TODO(liberato): make |surface_id| the overlay config token. If we're | 405 OnTransitionToOrFromOverlay(nullptr); |
403 // using CVV, then we'll need a CVV factory impl that understands it. | |
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; | 406 return; |
415 } | 407 } |
416 | 408 |
417 // We're creating a SurfaceTexture. | 409 // If we have a surface, then notify |overlay_helper_| about it. |
418 InitializePictureBufferManager(); | 410 std::unique_ptr<AndroidOverlayFactory> factory; |
411 if (config_.surface_id != SurfaceManager::kNoSurfaceID) { | |
412 factory = | |
413 base::MakeUnique<ContentVideoViewOverlayFactory>(config_.surface_id); | |
414 } | |
415 | |
416 // Notify |overlay_helper_| that we've started. This guarantees that we'll | |
417 // get a callback. It might not be a synchronous callback, but we're not in | |
418 // the synchronous case. It will be soon, though. For pre-M, we rely on the | |
419 // fact that |overlay_helper_| won't tell us to use a SurfaceTexture while | |
420 // waiting for an overlay to become ready, for example. | |
421 overlay_helper_->Startup( | |
422 base::Bind(&AndroidVideoDecodeAccelerator::OnTransitionToOrFromOverlay, | |
423 GetWeakPtr()), | |
424 base::Bind(&AndroidVideoDecodeAccelerator::OnTransitionToOrFromOverlay, | |
425 GetWeakPtr(), nullptr), | |
426 base::Bind(&AndroidVideoDecodeAccelerator::OnStopUsingOverlayImmediately, | |
427 GetWeakPtr()), | |
428 std::move(factory)); | |
419 } | 429 } |
420 | 430 |
421 void AndroidVideoDecodeAccelerator::OnOverlayReady(AndroidOverlay* overlay) { | 431 void AndroidVideoDecodeAccelerator::OnTransitionToOrFromOverlay( |
422 DCHECK(!defer_surface_creation_); | 432 std::unique_ptr<AndroidOverlay> overlay) { |
423 DCHECK_EQ(state_, WAITING_FOR_SURFACE); | 433 // If we're waiting for a surface (e.g., during startup), then proceed |
424 DCHECK(incoming_bundle_); | 434 // immediately. Otherwise, wait for Dequeue to handle it. This can probably |
435 // be merged with UpdateSurface. | |
436 if (state_ == BEFORE_OVERLAY_INIT) { | |
437 DCHECK(!incoming_overlay_); | |
438 incoming_bundle_ = new AVDASurfaceBundle(std::move(overlay)); | |
439 InitializePictureBufferManager(); | |
440 return; | |
441 } | |
425 | 442 |
426 InitializePictureBufferManager(); | 443 // If, for some reason, |overlay_helper_| decides that we really should |
427 } | 444 // change our output surface pre-M, ignore it. For example, if the |
445 // compositor tells us that it can't use an overlay, well, there's not much | |
446 // that we can do here unless we start falling forward to keyframes. | |
447 if (!platform_config_.allow_setsurface) | |
448 return; | |
428 | 449 |
429 void AndroidVideoDecodeAccelerator::OnOverlayFailed(AndroidOverlay* overlay) { | 450 // If we're using a SurfaceTexture and are told to switch to one, then just |
430 NOTIFY_ERROR(PLATFORM_FAILURE, "Surface is not available"); | 451 // do nothing. |overlay_helper_| doesn't really know if we've switched to |
452 // SurfaceTexture or not. Note that it can't ask us to switch to the same | |
453 // overlay we're using, since it's unique_ptr. | |
454 if (!overlay && codec_config_->surface_bundle && | |
455 !codec_config_->surface_bundle->overlay) { | |
456 // Also stop transitioning to an overlay, if we were doing so. | |
457 incoming_overlay_.reset(); | |
458 return; | |
459 } | |
460 | |
461 incoming_overlay_ = std::move(overlay); | |
431 } | 462 } |
432 | 463 |
433 void AndroidVideoDecodeAccelerator::InitializePictureBufferManager() { | 464 void AndroidVideoDecodeAccelerator::InitializePictureBufferManager() { |
434 DCHECK(!defer_surface_creation_); | 465 DCHECK(!defer_surface_creation_); |
435 DCHECK(incoming_bundle_); | 466 DCHECK(incoming_bundle_); |
436 | 467 |
437 if (!make_context_current_cb_.Run()) { | 468 if (!make_context_current_cb_.Run()) { |
438 NOTIFY_ERROR(PLATFORM_FAILURE, | 469 NOTIFY_ERROR(PLATFORM_FAILURE, |
439 "Failed to make this decoder's GL context current"); | 470 "Failed to make this decoder's GL context current"); |
440 incoming_bundle_ = nullptr; | 471 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. | 497 // 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. | 498 // If we get here with a codec, then we must setSurface. |
468 if (media_codec_) { | 499 if (media_codec_) { |
469 // TODO(liberato): fail on api check? | 500 // TODO(liberato): fail on api check? |
470 if (!media_codec_->SetSurface(incoming_bundle_->j_surface().obj())) { | 501 if (!media_codec_->SetSurface(incoming_bundle_->j_surface().obj())) { |
471 NOTIFY_ERROR(PLATFORM_FAILURE, "MediaCodec failed to switch surfaces."); | 502 NOTIFY_ERROR(PLATFORM_FAILURE, "MediaCodec failed to switch surfaces."); |
472 // We're not going to use |incoming_bundle_|. | 503 // We're not going to use |incoming_bundle_|. |
473 } else { | 504 } else { |
474 // We've switched surfaces, so replace |surface_bundle|. | 505 // We've switched surfaces, so replace |surface_bundle|. |
475 codec_config_->surface_bundle = incoming_bundle_; | 506 codec_config_->surface_bundle = incoming_bundle_; |
476 // We could be in WAITING_FOR_SURFACE, but we're not anymore. | 507 // We could be in BEFORE_OVERLAY_INIT, but we're not anymore. |
477 state_ = NO_ERROR; | 508 state_ = NO_ERROR; |
478 } | 509 } |
479 incoming_bundle_ = nullptr; | 510 incoming_bundle_ = nullptr; |
480 return; | 511 return; |
481 } | 512 } |
482 | 513 |
483 // We're going to create a codec with |incoming_bundle_|. It might fail, but | 514 // 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 | 515 // 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 | 516 // 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 | 517 // SetSurface), there shouldn't be any previous bundle. However, this is the |
(...skipping 18 matching lines...) Expand all Loading... | |
505 } | 536 } |
506 | 537 |
507 // In all other cases, we don't have to wait for the codec. | 538 // In all other cases, we don't have to wait for the codec. |
508 ConfigureMediaCodecAsynchronously(); | 539 ConfigureMediaCodecAsynchronously(); |
509 } | 540 } |
510 | 541 |
511 void AndroidVideoDecodeAccelerator::DoIOTask(bool start_timer) { | 542 void AndroidVideoDecodeAccelerator::DoIOTask(bool start_timer) { |
512 DCHECK(thread_checker_.CalledOnValidThread()); | 543 DCHECK(thread_checker_.CalledOnValidThread()); |
513 TRACE_EVENT0("media", "AVDA::DoIOTask"); | 544 TRACE_EVENT0("media", "AVDA::DoIOTask"); |
514 if (state_ == ERROR || state_ == WAITING_FOR_CODEC || | 545 if (state_ == ERROR || state_ == WAITING_FOR_CODEC || |
515 state_ == SURFACE_DESTROYED || state_ == WAITING_FOR_SURFACE) { | 546 state_ == SURFACE_DESTROYED || state_ == BEFORE_OVERLAY_INIT) { |
516 return; | 547 return; |
517 } | 548 } |
518 | 549 |
519 picture_buffer_manager_.MaybeRenderEarly(); | 550 picture_buffer_manager_.MaybeRenderEarly(); |
520 bool did_work = false, did_input = false, did_output = false; | 551 bool did_work = false, did_input = false, did_output = false; |
521 do { | 552 do { |
522 did_input = QueueInput(); | 553 did_input = QueueInput(); |
523 did_output = DequeueOutput(); | 554 did_output = DequeueOutput(); |
524 if (did_input || did_output) | 555 if (did_input || did_output) |
525 did_work = true; | 556 did_work = true; |
526 } while (did_input || did_output); | 557 } while (did_input || did_output); |
527 | 558 |
528 ManageTimer(did_work || start_timer); | 559 ManageTimer(did_work || start_timer); |
529 } | 560 } |
530 | 561 |
531 bool AndroidVideoDecodeAccelerator::QueueInput() { | 562 bool AndroidVideoDecodeAccelerator::QueueInput() { |
532 DCHECK(thread_checker_.CalledOnValidThread()); | 563 DCHECK(thread_checker_.CalledOnValidThread()); |
533 TRACE_EVENT0("media", "AVDA::QueueInput"); | 564 TRACE_EVENT0("media", "AVDA::QueueInput"); |
534 if (state_ == ERROR || state_ == WAITING_FOR_CODEC || | 565 if (state_ == ERROR || state_ == WAITING_FOR_CODEC || |
535 state_ == WAITING_FOR_KEY || state_ == WAITING_FOR_SURFACE) { | 566 state_ == WAITING_FOR_KEY || state_ == BEFORE_OVERLAY_INIT) { |
536 return false; | 567 return false; |
537 } | 568 } |
538 if (bitstreams_notified_in_advance_.size() > kMaxBitstreamsNotifiedInAdvance) | 569 if (bitstreams_notified_in_advance_.size() > kMaxBitstreamsNotifiedInAdvance) |
539 return false; | 570 return false; |
540 if (pending_bitstream_records_.empty()) | 571 if (pending_bitstream_records_.empty()) |
541 return false; | 572 return false; |
542 | 573 |
543 int input_buf_index = pending_input_buf_index_; | 574 int input_buf_index = pending_input_buf_index_; |
544 | 575 |
545 // Do not dequeue a new input buffer if we failed with MEDIA_CODEC_NO_KEY. | 576 // 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 | 672 // We should call NotifyEndOfBitstreamBuffer(), when no more decoded output |
642 // will be returned from the bitstream buffer. However, MediaCodec API is | 673 // will be returned from the bitstream buffer. However, MediaCodec API is |
643 // not enough to guarantee it. | 674 // not enough to guarantee it. |
644 // So, here, we calls NotifyEndOfBitstreamBuffer() in advance in order to | 675 // So, here, we calls NotifyEndOfBitstreamBuffer() in advance in order to |
645 // keep getting more bitstreams from the client, and throttle them by using | 676 // keep getting more bitstreams from the client, and throttle them by using |
646 // |bitstreams_notified_in_advance_|. | 677 // |bitstreams_notified_in_advance_|. |
647 // TODO(dwkang): check if there is a way to remove this workaround. | 678 // TODO(dwkang): check if there is a way to remove this workaround. |
648 base::ThreadTaskRunnerHandle::Get()->PostTask( | 679 base::ThreadTaskRunnerHandle::Get()->PostTask( |
649 FROM_HERE, | 680 FROM_HERE, |
650 base::Bind(&AndroidVideoDecodeAccelerator::NotifyEndOfBitstreamBuffer, | 681 base::Bind(&AndroidVideoDecodeAccelerator::NotifyEndOfBitstreamBuffer, |
651 weak_this_factory_.GetWeakPtr(), bitstream_buffer.id())); | 682 GetWeakPtr(), bitstream_buffer.id())); |
652 bitstreams_notified_in_advance_.push_back(bitstream_buffer.id()); | 683 bitstreams_notified_in_advance_.push_back(bitstream_buffer.id()); |
653 | 684 |
654 if (status != MEDIA_CODEC_OK) { | 685 if (status != MEDIA_CODEC_OK) { |
655 NOTIFY_ERROR(PLATFORM_FAILURE, "QueueInputBuffer failed:" << status); | 686 NOTIFY_ERROR(PLATFORM_FAILURE, "QueueInputBuffer failed:" << status); |
656 return false; | 687 return false; |
657 } | 688 } |
658 | 689 |
659 return true; | 690 return true; |
660 } | 691 } |
661 | 692 |
662 bool AndroidVideoDecodeAccelerator::DequeueOutput() { | 693 bool AndroidVideoDecodeAccelerator::DequeueOutput() { |
663 DCHECK(thread_checker_.CalledOnValidThread()); | 694 DCHECK(thread_checker_.CalledOnValidThread()); |
664 TRACE_EVENT0("media", "AVDA::DequeueOutput"); | 695 TRACE_EVENT0("media", "AVDA::DequeueOutput"); |
665 if (state_ == ERROR || state_ == WAITING_FOR_CODEC || | 696 if (state_ == ERROR || state_ == WAITING_FOR_CODEC || |
666 state_ == WAITING_FOR_SURFACE) { | 697 state_ == BEFORE_OVERLAY_INIT) { |
667 return false; | 698 return false; |
668 } | 699 } |
669 // If we're draining for reset or destroy, then we don't need picture buffers | 700 // 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, | 701 // 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 | 702 // 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, | 703 // exist anymore. From the pipeline's point of view, for Destroy at least, |
673 // the VDA is already gone. | 704 // the VDA is already gone. |
674 if (picturebuffers_requested_ && output_picture_buffers_.empty() && | 705 if (picturebuffers_requested_ && output_picture_buffers_.empty() && |
675 !IsDrainingForResetOrDestroy()) { | 706 !IsDrainingForResetOrDestroy()) { |
676 return false; | 707 return false; |
677 } | 708 } |
678 if (!output_picture_buffers_.empty() && free_picture_ids_.empty() && | 709 if (!output_picture_buffers_.empty() && free_picture_ids_.empty() && |
679 !IsDrainingForResetOrDestroy()) { | 710 !IsDrainingForResetOrDestroy()) { |
680 // Don't have any picture buffer to send. Need to wait. | 711 // Don't have any picture buffer to send. Need to wait. |
681 return false; | 712 return false; |
682 } | 713 } |
683 | 714 |
684 // If we're waiting to switch surfaces pause output release until we have all | 715 // 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 | 716 // picture buffers returned. This is so we can ensure the right flags are set |
686 // on the picture buffers returned to the client. | 717 // on the picture buffers returned to the client. |
687 if (pending_surface_id_) { | 718 if (incoming_overlay_) { |
688 if (picture_buffer_manager_.HasUnrenderedPictures()) | 719 if (picture_buffer_manager_.HasUnrenderedPictures()) |
689 return false; | 720 return false; |
690 if (!UpdateSurface()) | 721 if (!UpdateSurface()) |
691 return false; | 722 return false; |
692 // If we can't allocate the incoming surface yet, then stop here. | 723 // If we can't allocate the incoming surface yet, then stop here. |
724 // TODO(liberato): can this happen now? We won't be notified about the | |
DaleCurtis
2017/04/20 19:10:42
Switch to DCHECK then?
liberato (no reviews please)
2017/04/20 22:01:38
Done.
| |
725 // overlay if it's not ready. | |
693 if (state_ != NO_ERROR) | 726 if (state_ != NO_ERROR) |
694 return false; | 727 return false; |
695 } | 728 } |
696 | 729 |
697 bool eos = false; | 730 bool eos = false; |
698 base::TimeDelta presentation_timestamp; | 731 base::TimeDelta presentation_timestamp; |
699 int32_t buf_index = 0; | 732 int32_t buf_index = 0; |
700 do { | 733 do { |
701 size_t offset = 0; | 734 size_t offset = 0; |
702 size_t size = 0; | 735 size_t size = 0; |
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
745 // decoded images. Breaking their connection to the decoded image will | 778 // decoded images. Breaking their connection to the decoded image will |
746 // cause rendering of black frames. Instead, we let the existing | 779 // cause rendering of black frames. Instead, we let the existing |
747 // PictureBuffers live on and we simply update their size the next time | 780 // 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 | 781 // they're attached to an image of the new resolution. See the |
749 // size update in |SendDecodedFrameToClient| and https://crbug/587994. | 782 // size update in |SendDecodedFrameToClient| and https://crbug/587994. |
750 if (output_picture_buffers_.empty() && !picturebuffers_requested_) { | 783 if (output_picture_buffers_.empty() && !picturebuffers_requested_) { |
751 picturebuffers_requested_ = true; | 784 picturebuffers_requested_ = true; |
752 base::ThreadTaskRunnerHandle::Get()->PostTask( | 785 base::ThreadTaskRunnerHandle::Get()->PostTask( |
753 FROM_HERE, | 786 FROM_HERE, |
754 base::Bind(&AndroidVideoDecodeAccelerator::RequestPictureBuffers, | 787 base::Bind(&AndroidVideoDecodeAccelerator::RequestPictureBuffers, |
755 weak_this_factory_.GetWeakPtr())); | 788 GetWeakPtr())); |
756 return false; | 789 return false; |
757 } | 790 } |
758 | 791 |
759 return true; | 792 return true; |
760 } | 793 } |
761 | 794 |
762 case MEDIA_CODEC_OUTPUT_BUFFERS_CHANGED: | 795 case MEDIA_CODEC_OUTPUT_BUFFERS_CHANGED: |
763 break; | 796 break; |
764 | 797 |
765 case MEDIA_CODEC_OK: | 798 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, | 910 picture_buffer_manager_.UseCodecBufferForPictureBuffer(codec_buffer_index, |
878 picture_buffer); | 911 picture_buffer); |
879 } | 912 } |
880 | 913 |
881 void AndroidVideoDecodeAccelerator::Decode( | 914 void AndroidVideoDecodeAccelerator::Decode( |
882 const BitstreamBuffer& bitstream_buffer) { | 915 const BitstreamBuffer& bitstream_buffer) { |
883 DCHECK(thread_checker_.CalledOnValidThread()); | 916 DCHECK(thread_checker_.CalledOnValidThread()); |
884 | 917 |
885 // If we deferred getting a surface, then start getting one now. | 918 // If we deferred getting a surface, then start getting one now. |
886 if (defer_surface_creation_) { | 919 if (defer_surface_creation_) { |
887 // This is a little strange in that we're not really waiting for a surface | 920 // We should still ne in BEFORE_OVERLAY_INIT, since we've deferred doing it |
DaleCurtis
2017/04/20 19:10:43
s/ne/be/
liberato (no reviews please)
2017/04/20 22:01:38
Done.
| |
888 // yet -- we haven't requested one. We're still in WAITING_FOR_SURFACE as | 921 // until now. |
889 // the initial state set during construction. | 922 DCHECK_EQ(state_, BEFORE_OVERLAY_INIT); |
890 DCHECK_EQ(state_, WAITING_FOR_SURFACE); | |
891 defer_surface_creation_ = false; | 923 defer_surface_creation_ = false; |
892 StartSurfaceCreation(); | 924 StartOverlayHelper(); |
893 if (state_ == ERROR) { | 925 if (state_ == ERROR) { |
894 DLOG(ERROR) << "Failed deferred surface and MediaCodec initialization."; | 926 DLOG(ERROR) << "Failed deferred surface and MediaCodec initialization."; |
895 return; | 927 return; |
896 } | 928 } |
897 } | 929 } |
898 | 930 |
899 // If we previously deferred a codec restart, take care of it now. This can | 931 // 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. | 932 // happen on older devices where configuration changes require a codec reset. |
901 if (codec_needs_reset_) { | 933 if (codec_needs_reset_) { |
902 DCHECK(!drain_type_); | 934 DCHECK(!drain_type_); |
903 ResetCodecState(); | 935 ResetCodecState(); |
904 } | 936 } |
905 | 937 |
906 if (bitstream_buffer.id() >= 0 && bitstream_buffer.size() > 0) { | 938 if (bitstream_buffer.id() >= 0 && bitstream_buffer.size() > 0) { |
907 DecodeBuffer(bitstream_buffer); | 939 DecodeBuffer(bitstream_buffer); |
908 return; | 940 return; |
909 } | 941 } |
910 | 942 |
911 if (base::SharedMemory::IsHandleValid(bitstream_buffer.handle())) | 943 if (base::SharedMemory::IsHandleValid(bitstream_buffer.handle())) |
912 base::SharedMemory::CloseHandle(bitstream_buffer.handle()); | 944 base::SharedMemory::CloseHandle(bitstream_buffer.handle()); |
913 | 945 |
914 if (bitstream_buffer.id() < 0) { | 946 if (bitstream_buffer.id() < 0) { |
915 NOTIFY_ERROR(INVALID_ARGUMENT, | 947 NOTIFY_ERROR(INVALID_ARGUMENT, |
916 "Invalid bistream_buffer, id: " << bitstream_buffer.id()); | 948 "Invalid bistream_buffer, id: " << bitstream_buffer.id()); |
917 } else { | 949 } else { |
918 base::ThreadTaskRunnerHandle::Get()->PostTask( | 950 base::ThreadTaskRunnerHandle::Get()->PostTask( |
919 FROM_HERE, | 951 FROM_HERE, |
920 base::Bind(&AndroidVideoDecodeAccelerator::NotifyEndOfBitstreamBuffer, | 952 base::Bind(&AndroidVideoDecodeAccelerator::NotifyEndOfBitstreamBuffer, |
921 weak_this_factory_.GetWeakPtr(), bitstream_buffer.id())); | 953 GetWeakPtr(), bitstream_buffer.id())); |
922 } | 954 } |
923 } | 955 } |
924 | 956 |
925 void AndroidVideoDecodeAccelerator::DecodeBuffer( | 957 void AndroidVideoDecodeAccelerator::DecodeBuffer( |
926 const BitstreamBuffer& bitstream_buffer) { | 958 const BitstreamBuffer& bitstream_buffer) { |
927 pending_bitstream_records_.push(BitstreamRecord(bitstream_buffer)); | 959 pending_bitstream_records_.push(BitstreamRecord(bitstream_buffer)); |
928 TRACE_COUNTER1("media", "AVDA::PendingBitstreamBufferCount", | 960 TRACE_COUNTER1("media", "AVDA::PendingBitstreamBufferCount", |
929 pending_bitstream_records_.size()); | 961 pending_bitstream_records_.size()); |
930 | 962 |
931 DoIOTask(true); | 963 DoIOTask(true); |
(...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
990 } | 1022 } |
991 | 1023 |
992 void AndroidVideoDecodeAccelerator::Flush() { | 1024 void AndroidVideoDecodeAccelerator::Flush() { |
993 DVLOG(1) << __func__; | 1025 DVLOG(1) << __func__; |
994 DCHECK(thread_checker_.CalledOnValidThread()); | 1026 DCHECK(thread_checker_.CalledOnValidThread()); |
995 StartCodecDrain(DRAIN_FOR_FLUSH); | 1027 StartCodecDrain(DRAIN_FOR_FLUSH); |
996 } | 1028 } |
997 | 1029 |
998 void AndroidVideoDecodeAccelerator::ConfigureMediaCodecAsynchronously() { | 1030 void AndroidVideoDecodeAccelerator::ConfigureMediaCodecAsynchronously() { |
999 DCHECK(thread_checker_.CalledOnValidThread()); | 1031 DCHECK(thread_checker_.CalledOnValidThread()); |
1032 DCHECK(!media_codec_); | |
1000 | 1033 |
1001 DCHECK_NE(state_, WAITING_FOR_CODEC); | 1034 DCHECK_NE(state_, WAITING_FOR_CODEC); |
1002 state_ = WAITING_FOR_CODEC; | 1035 state_ = WAITING_FOR_CODEC; |
1003 | 1036 |
1004 ReleaseCodec(); | |
1005 | |
1006 base::Optional<TaskType> task_type = | 1037 base::Optional<TaskType> task_type = |
1007 codec_allocator_->TaskTypeForAllocation(); | 1038 codec_allocator_->TaskTypeForAllocation(); |
1008 if (!task_type) { | 1039 if (!task_type) { |
1009 // If there is no free thread, then just fail. | 1040 // If there is no free thread, then just fail. |
1010 OnCodecConfigured(nullptr); | 1041 OnCodecConfigured(nullptr); |
1011 return; | 1042 return; |
1012 } | 1043 } |
1013 | 1044 |
1014 // If autodetection is disallowed, fall back to Chrome's software decoders | 1045 // If autodetection is disallowed, fall back to Chrome's software decoders |
1015 // instead of using the software decoders provided by MediaCodec. | 1046 // instead of using the software decoders provided by MediaCodec. |
1016 if (task_type == TaskType::SW_CODEC && | 1047 if (task_type == TaskType::SW_CODEC && |
1017 IsMediaCodecSoftwareDecodingForbidden()) { | 1048 IsMediaCodecSoftwareDecodingForbidden()) { |
1018 OnCodecConfigured(nullptr); | 1049 OnCodecConfigured(nullptr); |
1019 return; | 1050 return; |
1020 } | 1051 } |
1021 | 1052 |
1022 codec_config_->task_type = task_type.value(); | 1053 codec_config_->task_type = task_type.value(); |
1023 codec_allocator_->CreateMediaCodecAsync(weak_this_factory_.GetWeakPtr(), | 1054 codec_allocator_->CreateMediaCodecAsync(GetWeakPtr(), codec_config_); |
1024 codec_config_); | |
1025 } | 1055 } |
1026 | 1056 |
1027 void AndroidVideoDecodeAccelerator::ConfigureMediaCodecSynchronously() { | 1057 void AndroidVideoDecodeAccelerator::ConfigureMediaCodecSynchronously() { |
1028 DCHECK(thread_checker_.CalledOnValidThread()); | 1058 DCHECK(thread_checker_.CalledOnValidThread()); |
1029 DCHECK(!media_codec_); | 1059 DCHECK(!media_codec_); |
1030 DCHECK_NE(state_, WAITING_FOR_CODEC); | 1060 DCHECK_NE(state_, WAITING_FOR_CODEC); |
1031 state_ = WAITING_FOR_CODEC; | 1061 state_ = WAITING_FOR_CODEC; |
1032 | 1062 |
1033 base::Optional<TaskType> task_type = | 1063 base::Optional<TaskType> task_type = |
1034 codec_allocator_->TaskTypeForAllocation(); | 1064 codec_allocator_->TaskTypeForAllocation(); |
1035 if (!task_type) { | 1065 if (!task_type) { |
1036 // If there is no free thread, then just fail. | 1066 // If there is no free thread, then just fail. |
1037 OnCodecConfigured(nullptr); | 1067 OnCodecConfigured(nullptr); |
1038 return; | 1068 return; |
1039 } | 1069 } |
1040 | 1070 |
1041 codec_config_->task_type = task_type.value(); | 1071 codec_config_->task_type = task_type.value(); |
1042 std::unique_ptr<MediaCodecBridge> media_codec = | 1072 std::unique_ptr<MediaCodecBridge> media_codec = |
1043 AVDACodecAllocator::GetInstance()->CreateMediaCodecSync(codec_config_); | 1073 codec_allocator_->CreateMediaCodecSync(codec_config_); |
1044 // Note that |media_codec| might be null, which will NotifyError. | 1074 // Note that |media_codec| might be null, which will NotifyError. |
1045 OnCodecConfigured(std::move(media_codec)); | 1075 OnCodecConfigured(std::move(media_codec)); |
1046 } | 1076 } |
1047 | 1077 |
1048 void AndroidVideoDecodeAccelerator::OnCodecConfigured( | 1078 void AndroidVideoDecodeAccelerator::OnCodecConfigured( |
1049 std::unique_ptr<MediaCodecBridge> media_codec) { | 1079 std::unique_ptr<MediaCodecBridge> media_codec) { |
1050 DCHECK(thread_checker_.CalledOnValidThread()); | 1080 DCHECK(thread_checker_.CalledOnValidThread()); |
1051 DCHECK(state_ == WAITING_FOR_CODEC || state_ == SURFACE_DESTROYED); | 1081 DCHECK(state_ == WAITING_FOR_CODEC || state_ == SURFACE_DESTROYED); |
1052 // If we are supposed to notify that initialization is complete, then do so | 1082 // If we are supposed to notify that initialization is complete, then do so |
1053 // before returning. Otherwise, this is a reconfiguration. | 1083 // before returning. Otherwise, this is a reconfiguration. |
1054 | 1084 |
1085 DCHECK(!media_codec_); | |
1086 media_codec_ = std::move(media_codec); | |
1087 | |
1055 // If |state_| changed to SURFACE_DESTROYED while we were configuring a codec, | 1088 // 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. | 1089 // 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) { | 1090 if (state_ == SURFACE_DESTROYED) { |
1061 if (deferred_initialization_pending_) { | 1091 if (deferred_initialization_pending_) { |
1062 // Losing the output surface is not considered an error state, so notify | 1092 // Losing the output surface is not considered an error state, so notify |
1063 // success. The client will destroy this soon. | 1093 // success. The client will destroy |this| soon. |
1064 NotifyInitializationSucceeded(); | 1094 NotifyInitializationSucceeded(); |
1065 } | 1095 } |
1096 | |
1097 // Post it to the right thread. | |
1098 ReleaseCodecAndBundle(); | |
1066 return; | 1099 return; |
1067 } | 1100 } |
1068 | 1101 |
1069 DCHECK(!media_codec_); | |
1070 media_codec_ = std::move(media_codec); | |
1071 picture_buffer_manager_.CodecChanged(media_codec_.get()); | 1102 picture_buffer_manager_.CodecChanged(media_codec_.get()); |
1072 if (!media_codec_) { | 1103 if (!media_codec_) { |
1073 NOTIFY_ERROR(PLATFORM_FAILURE, "Failed to create MediaCodec"); | 1104 NOTIFY_ERROR(PLATFORM_FAILURE, "Failed to create MediaCodec"); |
1074 return; | 1105 return; |
1075 } | 1106 } |
1076 | 1107 |
1077 if (deferred_initialization_pending_) | 1108 if (deferred_initialization_pending_) |
1078 NotifyInitializationSucceeded(); | 1109 NotifyInitializationSucceeded(); |
1079 | 1110 |
1080 state_ = NO_ERROR; | 1111 state_ = NO_ERROR; |
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1127 if (!drain_type_) { | 1158 if (!drain_type_) { |
1128 NOTIFY_ERROR(PLATFORM_FAILURE, "Unexpected EOS"); | 1159 NOTIFY_ERROR(PLATFORM_FAILURE, "Unexpected EOS"); |
1129 return; | 1160 return; |
1130 } | 1161 } |
1131 | 1162 |
1132 switch (*drain_type_) { | 1163 switch (*drain_type_) { |
1133 case DRAIN_FOR_FLUSH: | 1164 case DRAIN_FOR_FLUSH: |
1134 ResetCodecState(); | 1165 ResetCodecState(); |
1135 base::ThreadTaskRunnerHandle::Get()->PostTask( | 1166 base::ThreadTaskRunnerHandle::Get()->PostTask( |
1136 FROM_HERE, base::Bind(&AndroidVideoDecodeAccelerator::NotifyFlushDone, | 1167 FROM_HERE, base::Bind(&AndroidVideoDecodeAccelerator::NotifyFlushDone, |
1137 weak_this_factory_.GetWeakPtr())); | 1168 GetWeakPtr())); |
1138 break; | 1169 break; |
1139 case DRAIN_FOR_RESET: | 1170 case DRAIN_FOR_RESET: |
1140 ResetCodecState(); | 1171 ResetCodecState(); |
1141 base::ThreadTaskRunnerHandle::Get()->PostTask( | 1172 base::ThreadTaskRunnerHandle::Get()->PostTask( |
1142 FROM_HERE, base::Bind(&AndroidVideoDecodeAccelerator::NotifyResetDone, | 1173 FROM_HERE, base::Bind(&AndroidVideoDecodeAccelerator::NotifyResetDone, |
1143 weak_this_factory_.GetWeakPtr())); | 1174 GetWeakPtr())); |
1144 break; | 1175 break; |
1145 case DRAIN_FOR_DESTROY: | 1176 case DRAIN_FOR_DESTROY: |
1146 base::ThreadTaskRunnerHandle::Get()->PostTask( | 1177 base::ThreadTaskRunnerHandle::Get()->PostTask( |
1147 FROM_HERE, base::Bind(&AndroidVideoDecodeAccelerator::ActualDestroy, | 1178 FROM_HERE, base::Bind(&AndroidVideoDecodeAccelerator::ActualDestroy, |
1148 weak_this_factory_.GetWeakPtr())); | 1179 GetWeakPtr())); |
1149 break; | 1180 break; |
1150 } | 1181 } |
1151 drain_type_.reset(); | 1182 drain_type_.reset(); |
1152 } | 1183 } |
1153 | 1184 |
1154 void AndroidVideoDecodeAccelerator::ResetCodecState() { | 1185 void AndroidVideoDecodeAccelerator::ResetCodecState() { |
1155 DCHECK(thread_checker_.CalledOnValidThread()); | 1186 DCHECK(thread_checker_.CalledOnValidThread()); |
1156 | 1187 |
1157 // If there is already a reset in flight, then that counts. This can really | 1188 // If there is already a reset in flight, then that counts. This can really |
1158 // only happen if somebody calls Reset. | 1189 // only happen if somebody calls Reset. |
1159 // If the surface is destroyed or we're in an error state there's nothing to | 1190 // 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 | 1191 // do. Note that BEFORE_OVERLAY_INIT implies that we have no codec, but it's |
1161 // included for completeness. | 1192 // included for completeness. |
1162 if (state_ == WAITING_FOR_CODEC || state_ == SURFACE_DESTROYED || | 1193 if (state_ == WAITING_FOR_CODEC || state_ == SURFACE_DESTROYED || |
1163 state_ == WAITING_FOR_SURFACE || state_ == ERROR || !media_codec_) { | 1194 state_ == BEFORE_OVERLAY_INIT || state_ == ERROR || !media_codec_) { |
1164 return; | 1195 return; |
1165 } | 1196 } |
1166 | 1197 |
1167 bitstream_buffers_in_decoder_.clear(); | 1198 bitstream_buffers_in_decoder_.clear(); |
1168 | 1199 |
1169 if (pending_input_buf_index_ != -1) { | 1200 if (pending_input_buf_index_ != -1) { |
1170 // The data for that index exists in the input buffer, but corresponding | 1201 // 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. | 1202 // shm block been deleted. Check that it is safe to flush the codec, i.e. |
1172 // |pending_bitstream_records_| is empty. | 1203 // |pending_bitstream_records_| is empty. |
1173 // TODO(timav): keep shm block for that buffer and remove this restriction. | 1204 // 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. | 1217 // Flush the codec if possible, or create a new one if not. |
1187 if (!MediaCodecUtil::CodecNeedsFlushWorkaround(media_codec_.get())) { | 1218 if (!MediaCodecUtil::CodecNeedsFlushWorkaround(media_codec_.get())) { |
1188 DVLOG(3) << __func__ << " Flushing MediaCodec."; | 1219 DVLOG(3) << __func__ << " Flushing MediaCodec."; |
1189 media_codec_->Flush(); | 1220 media_codec_->Flush(); |
1190 // Since we just flushed all the output buffers, make sure that nothing is | 1221 // Since we just flushed all the output buffers, make sure that nothing is |
1191 // using them. | 1222 // using them. |
1192 picture_buffer_manager_.CodecChanged(media_codec_.get()); | 1223 picture_buffer_manager_.CodecChanged(media_codec_.get()); |
1193 } else { | 1224 } else { |
1194 DVLOG(3) << __func__ << " Deleting the MediaCodec and creating a new one."; | 1225 DVLOG(3) << __func__ << " Deleting the MediaCodec and creating a new one."; |
1195 GetManager()->StopTimer(this); | 1226 GetManager()->StopTimer(this); |
1196 // Note that this will release the codec, then allocate a new one. It will | 1227 // 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. | 1228 // 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 | 1229 // 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. | 1230 // thread as is used to release the old one, so it's serialized anyway. |
1231 ReleaseCodec(); | |
1200 ConfigureMediaCodecAsynchronously(); | 1232 ConfigureMediaCodecAsynchronously(); |
1201 } | 1233 } |
1202 } | 1234 } |
1203 | 1235 |
1204 void AndroidVideoDecodeAccelerator::Reset() { | 1236 void AndroidVideoDecodeAccelerator::Reset() { |
1205 DVLOG(1) << __func__; | 1237 DVLOG(1) << __func__; |
1206 DCHECK(thread_checker_.CalledOnValidThread()); | 1238 DCHECK(thread_checker_.CalledOnValidThread()); |
1207 TRACE_EVENT0("media", "AVDA::Reset"); | 1239 TRACE_EVENT0("media", "AVDA::Reset"); |
1208 | 1240 |
1209 if (defer_surface_creation_) { | 1241 if (defer_surface_creation_) { |
1210 DCHECK(!media_codec_); | 1242 DCHECK(!media_codec_); |
1211 DCHECK(pending_bitstream_records_.empty()); | 1243 DCHECK(pending_bitstream_records_.empty()); |
1212 DCHECK_EQ(state_, WAITING_FOR_SURFACE); | 1244 DCHECK_EQ(state_, BEFORE_OVERLAY_INIT); |
1213 base::ThreadTaskRunnerHandle::Get()->PostTask( | 1245 base::ThreadTaskRunnerHandle::Get()->PostTask( |
1214 FROM_HERE, base::Bind(&AndroidVideoDecodeAccelerator::NotifyResetDone, | 1246 FROM_HERE, base::Bind(&AndroidVideoDecodeAccelerator::NotifyResetDone, |
1215 weak_this_factory_.GetWeakPtr())); | 1247 GetWeakPtr())); |
1216 return; | 1248 return; |
1217 } | 1249 } |
1218 | 1250 |
1219 while (!pending_bitstream_records_.empty()) { | 1251 while (!pending_bitstream_records_.empty()) { |
1220 int32_t bitstream_buffer_id = | 1252 int32_t bitstream_buffer_id = |
1221 pending_bitstream_records_.front().buffer.id(); | 1253 pending_bitstream_records_.front().buffer.id(); |
1222 pending_bitstream_records_.pop(); | 1254 pending_bitstream_records_.pop(); |
1223 | 1255 |
1224 if (bitstream_buffer_id != -1) { | 1256 if (bitstream_buffer_id != -1) { |
1225 base::ThreadTaskRunnerHandle::Get()->PostTask( | 1257 base::ThreadTaskRunnerHandle::Get()->PostTask( |
1226 FROM_HERE, | 1258 FROM_HERE, |
1227 base::Bind(&AndroidVideoDecodeAccelerator::NotifyEndOfBitstreamBuffer, | 1259 base::Bind(&AndroidVideoDecodeAccelerator::NotifyEndOfBitstreamBuffer, |
1228 weak_this_factory_.GetWeakPtr(), bitstream_buffer_id)); | 1260 GetWeakPtr(), bitstream_buffer_id)); |
1229 } | 1261 } |
1230 } | 1262 } |
1231 TRACE_COUNTER1("media", "AVDA::PendingBitstreamBufferCount", 0); | 1263 TRACE_COUNTER1("media", "AVDA::PendingBitstreamBufferCount", 0); |
1232 bitstreams_notified_in_advance_.clear(); | 1264 bitstreams_notified_in_advance_.clear(); |
1233 | 1265 |
1234 picture_buffer_manager_.ReleaseCodecBuffers(output_picture_buffers_); | 1266 picture_buffer_manager_.ReleaseCodecBuffers(output_picture_buffers_); |
1235 StartCodecDrain(DRAIN_FOR_RESET); | 1267 StartCodecDrain(DRAIN_FOR_RESET); |
1236 } | 1268 } |
1237 | 1269 |
1238 void AndroidVideoDecodeAccelerator::SetSurface(int32_t surface_id) { | 1270 void AndroidVideoDecodeAccelerator::SetSurface(int32_t surface_id) { |
1239 DVLOG(1) << __func__; | 1271 DVLOG(1) << __func__; |
1240 DCHECK(thread_checker_.CalledOnValidThread()); | 1272 DCHECK(thread_checker_.CalledOnValidThread()); |
1241 | 1273 |
1242 if (surface_id == this->surface_id()) { | 1274 // It's possible that we'll receive a SetSurface before initializing the |
1243 pending_surface_id_.reset(); | 1275 // overlay helper. For example, if we defer surface creation, then we'll |
1276 // signal success to WMPI before initializing it. WMPI is free to change the | |
1277 // surface. In this case, just pretend that |surface_id| is the initial one. | |
1278 if (state_ == BEFORE_OVERLAY_INIT) { | |
1279 config_.surface_id = surface_id; | |
1244 return; | 1280 return; |
1245 } | 1281 } |
1246 | 1282 |
1247 // Surface changes never take effect immediately, they will be handled during | 1283 std::unique_ptr<AndroidOverlayFactory> factory; |
1248 // DequeOutput() once we get to a good switch point or immediately during an | 1284 if (surface_id != SurfaceManager::kNoSurfaceID) { |
DaleCurtis
2017/04/20 19:10:43
Drop {} for single-line if.
liberato (no reviews please)
2017/04/20 22:01:38
Done.
| |
1249 // OnSurfaceDestroyed() call. | 1285 factory = base::MakeUnique<ContentVideoViewOverlayFactory>(surface_id); |
1250 pending_surface_id_ = surface_id; | 1286 } |
1287 | |
1288 overlay_helper_->OnOverlayFactory(std::move(factory)); | |
1251 } | 1289 } |
1252 | 1290 |
1253 void AndroidVideoDecodeAccelerator::Destroy() { | 1291 void AndroidVideoDecodeAccelerator::Destroy() { |
1254 DVLOG(1) << __func__; | 1292 DVLOG(1) << __func__; |
1255 DCHECK(thread_checker_.CalledOnValidThread()); | 1293 DCHECK(thread_checker_.CalledOnValidThread()); |
1256 | 1294 |
1257 picture_buffer_manager_.Destroy(output_picture_buffers_); | 1295 picture_buffer_manager_.Destroy(output_picture_buffers_); |
1258 client_ = nullptr; | 1296 client_ = nullptr; |
1259 | 1297 |
1260 // We don't want to queue more inputs while draining. | 1298 // We don't want to queue more inputs while draining. |
1261 std::queue<BitstreamRecord>().swap(pending_bitstream_records_); | 1299 std::queue<BitstreamRecord>().swap(pending_bitstream_records_); |
1262 StartCodecDrain(DRAIN_FOR_DESTROY); | 1300 StartCodecDrain(DRAIN_FOR_DESTROY); |
1263 } | 1301 } |
1264 | 1302 |
1265 void AndroidVideoDecodeAccelerator::ActualDestroy() { | 1303 void AndroidVideoDecodeAccelerator::ActualDestroy() { |
1266 DVLOG(1) << __func__; | 1304 DVLOG(1) << __func__; |
1267 DCHECK(thread_checker_.CalledOnValidThread()); | 1305 DCHECK(thread_checker_.CalledOnValidThread()); |
1268 | 1306 |
1269 // Note that async codec construction might still be in progress. In that | 1307 // 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 | 1308 // case, the codec will be deleted when it completes once we invalidate all |
1271 // our weak refs. | 1309 // our weak refs. |
1272 weak_this_factory_.InvalidateWeakPtrs(); | 1310 weak_this_factory_.InvalidateWeakPtrs(); |
1273 GetManager()->StopTimer(this); | 1311 GetManager()->StopTimer(this); |
1274 ReleaseCodec(); | 1312 ReleaseCodecAndBundle(); |
1275 | 1313 |
1276 delete this; | 1314 delete this; |
1277 } | 1315 } |
1278 | 1316 |
1279 bool AndroidVideoDecodeAccelerator::TryToSetupDecodeOnSeparateThread( | 1317 bool AndroidVideoDecodeAccelerator::TryToSetupDecodeOnSeparateThread( |
1280 const base::WeakPtr<Client>& decode_client, | 1318 const base::WeakPtr<Client>& decode_client, |
1281 const scoped_refptr<base::SingleThreadTaskRunner>& decode_task_runner) { | 1319 const scoped_refptr<base::SingleThreadTaskRunner>& decode_task_runner) { |
1282 return false; | 1320 return false; |
1283 } | 1321 } |
1284 | 1322 |
1285 const gfx::Size& AndroidVideoDecodeAccelerator::GetSize() const { | 1323 const gfx::Size& AndroidVideoDecodeAccelerator::GetSize() const { |
1286 return size_; | 1324 return size_; |
1287 } | 1325 } |
1288 | 1326 |
1289 base::WeakPtr<gpu::gles2::GLES2Decoder> | 1327 base::WeakPtr<gpu::gles2::GLES2Decoder> |
1290 AndroidVideoDecodeAccelerator::GetGlDecoder() const { | 1328 AndroidVideoDecodeAccelerator::GetGlDecoder() const { |
1291 return get_gles2_decoder_cb_.Run(); | 1329 return get_gles2_decoder_cb_.Run(); |
1292 } | 1330 } |
1293 | 1331 |
1294 void AndroidVideoDecodeAccelerator::OnSurfaceDestroyed( | 1332 void AndroidVideoDecodeAccelerator::OnStopUsingOverlayImmediately( |
1295 AndroidOverlay* overlay) { | 1333 AndroidOverlay* overlay) { |
1296 DVLOG(1) << __func__; | 1334 DVLOG(1) << __func__; |
1297 TRACE_EVENT0("media", "AVDA::OnSurfaceDestroyed"); | 1335 TRACE_EVENT0("media", "AVDA::OnStopUsingOverlayImmediately"); |
1298 DCHECK(thread_checker_.CalledOnValidThread()); | 1336 DCHECK(thread_checker_.CalledOnValidThread()); |
1299 | 1337 |
1300 // We cannot get here if we're before surface allocation, since we transition | 1338 // 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 | 1339 // 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, | 1340 // 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 | 1341 // then we could just update the config to use a SurfaceTexture and return |
1304 // without changing state. | 1342 // without changing state. |
1305 DCHECK_NE(state_, WAITING_FOR_SURFACE); | 1343 DCHECK_NE(state_, BEFORE_OVERLAY_INIT); |
1344 | |
1345 // If we're transitioning to |overlay|, then just stop here. We're not also | |
1346 // using the overlay if we're transitioning to it. | |
1347 if (incoming_overlay_.has_value() && (*incoming_overlay_).get() == overlay) { | |
DaleCurtis
2017/04/20 19:10:43
just if (incoming_overlay_ && incoming_overlay_.ge
liberato (no reviews please)
2017/04/20 22:01:38
Done.
| |
1348 incoming_overlay_.reset(); | |
1349 return; | |
1350 } | |
1351 | |
1352 // If we have no codec, or if our current config doesn't refer to |overlay|, | |
1353 // then do nothing. |overlay| might be for some overlay that's waiting for | |
1354 // codec destruction on some other thread. | |
1355 if (!codec_config_->surface_bundle || | |
1356 codec_config_->surface_bundle->overlay.get() != overlay) { | |
1357 return; | |
1358 } | |
1359 | |
1360 // If we have a codec, or if codec allocation is in flight, then it's using an | |
1361 // overlay that was destroyed. | |
1362 if (state_ == WAITING_FOR_CODEC) { | |
1363 // What we should do here is to set |incoming_overlay_| to nullptr, to start | |
1364 // a transistion to SurfaceTexture. OnCodecConfigured could notice that | |
1365 // there's an incoming overlay, and then immediately transition the codec / | |
1366 // drop and re-allocate the codec using it. However, for CVV, that won't | |
1367 // work, since CVV-based overlays block the main thread waiting for the | |
1368 // overlay to be dropped, so OnCodecConfigured won't run. For DS, it's the | |
1369 // right thing. | |
1370 // So, for now, we just fail, and let OnCodecConfigured drop the codec. | |
1371 // Note that this case really can only happen on pre-M anyway, unless it's | |
1372 // during initial construction. This will result in the overlay being | |
1373 // destroyed after timeout, since OnCodecConfigured can't run until the | |
1374 // synchronous CVV destruction quits. | |
1375 state_ = SURFACE_DESTROYED; | |
1376 return; | |
1377 } | |
1306 | 1378 |
1307 // If the API is available avoid having to restart the decoder in order to | 1379 // 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 | 1380 // 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. | 1381 // callback, the MediaCodec will throw an error as the surface is destroyed. |
1310 if (base::android::BuildInfo::GetInstance()->sdk_int() >= 23) { | 1382 if (platform_config_.allow_setsurface) { |
1311 // Since we can't wait for a transition, we must invalidate all outstanding | 1383 // 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. | 1384 // picture buffers to avoid putting the GL system in a broken state. |
1313 picture_buffer_manager_.ReleaseCodecBuffers(output_picture_buffers_); | 1385 picture_buffer_manager_.ReleaseCodecBuffers(output_picture_buffers_); |
1314 | 1386 |
1315 // Switch away from the surface being destroyed to a surface texture. | 1387 // If we aren't transitioning to some other surface, then transition to a |
1316 DCHECK_NE(surface_id(), SurfaceManager::kNoSurfaceID); | 1388 // SurfaceTexture. Remember that, if |incoming_overlay_| is an overlay, |
1389 // then it's already ready and can be transitioned to immediately. We were | |
1390 // just waiting for codec buffers to come back, but we just dropped them. | |
1391 if (!incoming_overlay_) | |
1392 incoming_overlay_ = std::unique_ptr<AndroidOverlay>(); | |
DaleCurtis
2017/04/20 19:10:43
.reset() ?
liberato (no reviews please)
2017/04/20 22:01:38
we want it to has_value() of a nullptr -- i.e., tr
| |
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 |