Chromium Code Reviews| Index: content/common/gpu/media/android_video_decode_accelerator.cc |
| diff --git a/content/common/gpu/media/android_video_decode_accelerator.cc b/content/common/gpu/media/android_video_decode_accelerator.cc |
| index 55b1855a546d482fd89f87b95d6361c0d89e88ab..a86f41f269557edde509cbf40e4912e05b1c618f 100644 |
| --- a/content/common/gpu/media/android_video_decode_accelerator.cc |
| +++ b/content/common/gpu/media/android_video_decode_accelerator.cc |
| @@ -319,6 +319,7 @@ AndroidVideoDecodeAccelerator::AndroidVideoDecodeAccelerator( |
| error_sequence_token_(0), |
| defer_errors_(false), |
| deferred_initialization_pending_(false), |
| + surface_id_(media::VideoDecodeAccelerator::Config::kNoSurfaceID), |
| weak_this_factory_(this) {} |
| AndroidVideoDecodeAccelerator::~AndroidVideoDecodeAccelerator() { |
| @@ -416,13 +417,20 @@ bool AndroidVideoDecodeAccelerator::Initialize(const Config& config, |
| return false; |
| } |
| - codec_config_->surface_ = strategy_->Initialize(config.surface_id); |
| + surface_id_ = config.surface_id; |
| + codec_config_->surface_ = strategy_->Initialize(surface_id_); |
| if (codec_config_->surface_.IsEmpty()) { |
| LOG(ERROR) << "Failed to initialize the backing strategy. The returned " |
| "Java surface is empty."; |
| return false; |
| } |
| + on_destroying_surface_cb_ = |
| + base::Bind(&AndroidVideoDecodeAccelerator::OnDestroyingSurface, |
| + weak_this_factory_.GetWeakPtr()); |
| + AVDASurfaceTracker::GetInstance()->RegisterOnDestroyingSurfaceCallback( |
| + on_destroying_surface_cb_); |
| + |
| // TODO(watk,liberato): move this into the strategy. |
| scoped_refptr<gfx::SurfaceTexture> surface_texture = |
| strategy_->GetSurfaceTexture(); |
| @@ -504,8 +512,10 @@ void AndroidVideoDecodeAccelerator::SetCdm(int cdm_id) { |
| void AndroidVideoDecodeAccelerator::DoIOTask(bool start_timer) { |
| DCHECK(thread_checker_.CalledOnValidThread()); |
| TRACE_EVENT0("media", "AVDA::DoIOTask"); |
| - if (state_ == ERROR || state_ == WAITING_FOR_CODEC) |
| + if (state_ == ERROR || state_ == WAITING_FOR_CODEC || |
| + state_ == SURFACE_DESTROYED) { |
| return; |
| + } |
| strategy_->MaybeRenderEarly(); |
| bool did_work = QueueInput(); |
| @@ -940,7 +950,11 @@ void AndroidVideoDecodeAccelerator::Flush() { |
| DVLOG(1) << __FUNCTION__; |
| DCHECK(thread_checker_.CalledOnValidThread()); |
| - StartCodecDrain(DRAIN_FOR_FLUSH); |
| + if (state_ == SURFACE_DESTROYED) { |
|
DaleCurtis
2016/04/27 21:20:35
No unnecessary parens.
|
| + NotifyFlushDone(); |
| + } else { |
| + StartCodecDrain(DRAIN_FOR_FLUSH); |
| + } |
| } |
| void AndroidVideoDecodeAccelerator::ConfigureMediaCodecAsynchronously() { |
| @@ -1006,22 +1020,28 @@ AndroidVideoDecodeAccelerator::ConfigureMediaCodecOnAnyThread( |
| void AndroidVideoDecodeAccelerator::OnCodecConfigured( |
| std::unique_ptr<media::VideoCodecBridge> media_codec) { |
| DCHECK(thread_checker_.CalledOnValidThread()); |
| - DCHECK_EQ(state_, WAITING_FOR_CODEC); |
| - |
| - media_codec_ = std::move(media_codec); |
| + DCHECK(state_ == WAITING_FOR_CODEC || state_ == SURFACE_DESTROYED); |
| // Record one instance of the codec being initialized. |
| RecordFormatChangedMetric(FormatChangedValue::CodecInitialized); |
| - strategy_->CodecChanged(media_codec_.get()); |
| - |
| // If we are supposed to notify that initialization is complete, then do so |
| // now. Otherwise, this is a reconfiguration. |
| if (deferred_initialization_pending_) { |
| - NotifyInitializationComplete(!!media_codec_); |
| + // Losing the output surface is not considered an error state, so notify |
| + // success. The client will destroy this soon. |
| + NotifyInitializationComplete(state_ == SURFACE_DESTROYED ? true |
| + : !!media_codec); |
| deferred_initialization_pending_ = false; |
| } |
| + // If |state_| changed to SURFACE_DESTROYED while we were configuring a codec, |
| + // then the codec is already invalid so we return early and drop it. |
| + if (state_ == SURFACE_DESTROYED) |
| + return; |
| + |
| + media_codec_ = std::move(media_codec); |
| + strategy_->CodecChanged(media_codec_.get()); |
| if (!media_codec_) { |
| POST_ERROR(PLATFORM_FAILURE, "Failed to create MediaCodec."); |
| return; |
| @@ -1097,7 +1117,8 @@ void AndroidVideoDecodeAccelerator::ResetCodecState( |
| // If there is already a reset in flight, then that counts. This can really |
| // only happen if somebody calls Reset. |
| - if (state_ == WAITING_FOR_CODEC) { |
| + // If the surface is destroyed there's nothing to do. |
| + if (state_ == WAITING_FOR_CODEC || state_ == SURFACE_DESTROYED) { |
| if (!done_cb.is_null()) |
| done_cb.Run(); |
| return; |
| @@ -1224,6 +1245,11 @@ void AndroidVideoDecodeAccelerator::ActualDestroy() { |
| DVLOG(1) << __FUNCTION__; |
| DCHECK(thread_checker_.CalledOnValidThread()); |
| + if (!on_destroying_surface_cb_.is_null()) { |
| + AVDASurfaceTracker::GetInstance()->UnregisterOnDestroyingSurfaceCallback( |
| + on_destroying_surface_cb_); |
| + } |
|
DaleCurtis
2016/04/27 21:20:35
Clear surface destruction cb?
|
| + |
| // Note that async codec construction might still be in progress. In that |
| // case, the codec will be deleted when it completes once we invalidate all |
| // our weak refs. |
| @@ -1277,6 +1303,29 @@ gpu::gles2::TextureRef* AndroidVideoDecodeAccelerator::GetTextureForPicture( |
| return texture_ref; |
| } |
| +void AndroidVideoDecodeAccelerator::OnDestroyingSurface(int surface_id) { |
| + DCHECK(thread_checker_.CalledOnValidThread()); |
| + TRACE_EVENT0("media", "AVDA::OnDestroyingSurface"); |
| + DVLOG(1) << __FUNCTION__ << " surface_id: " << surface_id; |
| + |
| + if (surface_id != surface_id_) |
| + return; |
| + |
| + // If we're currently asynchronously configuring a codec, it will be destroyed |
|
DaleCurtis
2016/04/27 21:20:35
Hmm, this may have bad interactions where we need
watk
2016/05/02 17:49:00
Oh, good point. :( This is going to be painful.
|
| + // when configuration completes and it notices that |state_| has changed to |
| + // SURFACE_DESTROYED. |
| + state_ = SURFACE_DESTROYED; |
| + if (media_codec_) { |
| + media_codec_.reset(); |
| + strategy_->CodecChanged(media_codec_.get()); |
| + } |
| + // If we're draining, signal completion now because the drain can no longer |
|
DaleCurtis
2016/04/27 21:20:35
Line above.
|
| + // proceed. |
| + if (drain_type_ != DRAIN_TYPE_NONE) { |
|
DaleCurtis
2016/04/27 21:20:36
No unnecessary parens.
|
| + OnDrainCompleted(); |
| + } |
| +} |
| + |
| void AndroidVideoDecodeAccelerator::OnFrameAvailable() { |
| // Remember: this may be on any thread. |
| DCHECK(strategy_); |