Chromium Code Reviews| Index: media/gpu/android_video_decode_accelerator.cc |
| diff --git a/media/gpu/android_video_decode_accelerator.cc b/media/gpu/android_video_decode_accelerator.cc |
| index f6b7178318127a2e80e5f487788c70daad1e162d..ee1df4fbe1a561881b4d95cedb302be90d1984c9 100644 |
| --- a/media/gpu/android_video_decode_accelerator.cc |
| +++ b/media/gpu/android_video_decode_accelerator.cc |
| @@ -318,6 +318,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() { |
| @@ -415,13 +416,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(); |
| @@ -503,8 +511,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(); |
| @@ -674,7 +684,7 @@ bool AndroidVideoDecodeAccelerator::DequeueOutput() { |
| switch (status) { |
| case media::MEDIA_CODEC_ERROR: |
| - // Do not post an error if we are draining for reset and destroy. |
| + // Do not post an error if we are draining for reset or destroy. |
| // Instead, run the drain completion task. |
| if (IsDrainingForResetOrDestroy()) { |
| DVLOG(1) << __FUNCTION__ << ": error while codec draining"; |
| @@ -940,7 +950,11 @@ void AndroidVideoDecodeAccelerator::Flush() { |
| DVLOG(1) << __FUNCTION__; |
| DCHECK(thread_checker_.CalledOnValidThread()); |
| - StartCodecDrain(DRAIN_FOR_FLUSH); |
| + if (state_ == SURFACE_DESTROYED) { |
| + 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_); |
| + } |
| + |
| // 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,83 @@ gpu::gles2::TextureRef* AndroidVideoDecodeAccelerator::GetTextureForPicture( |
| return texture_ref; |
| } |
| +void SynchronouslyDrainCodec() { |
| + // The timeout for dequeueing input/output buffers picked mostly arbitrarily. |
| + // It's about as long as a working MediaCodec might conceivably delay before |
| + // returning something. If the timeout is exceeded the MediaCodec might hang |
| + // when we try to delete it later, but that's effectively the same thing as |
| + // waiting longer here anyway. |
| + const base::TimeDelta kTimeout = base::TimeDelta::FromSeconds(3); |
| + strategy_->ReleaseCodecBuffers(output_picture_buffers_); |
| + |
| + // If we're in the middle of a drain already, don't queue another EOS; just |
| + // drain until we get the one already queued. |
| + if (drain_type_ != DRAIN_TYPE_NONE) { |
| + int input_buf_index = -1; |
| + media::MediaCodecStatus status = |
| + media_codec_->DequeueInputBuffer(kTimeout, &input_buf_index); |
|
liberato (no reviews please)
2016/05/04 14:19:56
input buffers might become available after we deco
watk
2016/05/04 18:18:46
Do we care about input buffers? We only need to qu
liberato (no reviews please)
2016/05/04 18:30:00
my concern is how does one know that an input buff
|
| + if (status != media::MEDIA_CODEC_OK) |
| + return; |
| + DCHECK_NE(input_buf_index, -1); |
| + |
| + media_codec_->QueueEOS(input_buf_index); |
| + } |
| + |
| + // Drain all of the output buffers. |
| + for (;;) { |
| + int32_t buf_index = -1; |
| + size_t offset = 0; |
| + size_t size = 0; |
| + base::TimeDelta presentation_timestamp; |
| + bool eos = false; |
| + media::MediaCodecStatus status = |
| + media_codec_->DequeueOutputBuffer(kTimeout, &buf_index, &offset, &size, |
| + &presentation_timestamp, &eos, NULL); |
| + switch (status) { |
| + case media::MEDIA_CODEC_ERROR: |
| + case media::MEDIA_CODEC_DEQUEUE_OUTPUT_AGAIN_LATER: |
| + return; |
| + case media::MEDIA_CODEC_OUTPUT_FORMAT_CHANGED: |
| + case media::MEDIA_CODEC_OUTPUT_BUFFERS_CHANGED: |
| + continue; |
| + case media::MEDIA_CODEC_OK: |
| + DCHECK_GE(buf_index, 0); |
| + if (eos) |
| + return; |
| + media_codec_->ReleaseOutputBuffer(buf_index, false); |
| + } |
| + } |
| +} |
| + |
| +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; |
| + |
| + // Some VP8 files require a complete MediaCodec drain before we can call |
| + // MediaCodec.flush() or MediaCodec.reset() so we must synchronously drain |
| + // the MediaCodec here. http://crbug.com/598963. |
| + if (media_codec_ && codec_config_->codec_ == media::kCodecVP8) |
| + SynchronouslyDrainCodec(); |
| + |
| + // If we're currently asynchronously configuring a codec, it will be destroyed |
| + // 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 either the drain was |
| + // finished above, or the drain can no longer proceed anyway. |
| + if (drain_type_ != DRAIN_TYPE_NONE) |
| + OnDrainCompleted(); |
| +} |
| + |
| void AndroidVideoDecodeAccelerator::OnFrameAvailable() { |
| // Remember: this may be on any thread. |
| DCHECK(strategy_); |