From 4b710f086070fabe022b3a1f474bfcbec842b8fc Mon Sep 17 00:00:00 2001 From: Ronghua Wu Date: Mon, 14 Sep 2015 15:44:04 -0700 Subject: libstagefright: don't reclaim codec when there's buffer owned by client. Notify the client and try to reclaim again in 0.5s. Bug: 23703241 Bug: 23949540 Change-Id: I1afe50c71635645668bfb73ffa0d801765b5ae3c --- media/libstagefright/MediaCodec.cpp | 42 ++++++++++++++++++++++++++++++++++++- 1 file changed, 41 insertions(+), 1 deletion(-) (limited to 'media/libstagefright/MediaCodec.cpp') diff --git a/media/libstagefright/MediaCodec.cpp b/media/libstagefright/MediaCodec.cpp index 7019537..dc6009b 100644 --- a/media/libstagefright/MediaCodec.cpp +++ b/media/libstagefright/MediaCodec.cpp @@ -63,6 +63,7 @@ static bool isResourceError(status_t err) { } static const int kMaxRetry = 2; +static const int kMaxReclaimWaitTimeInUs = 500000; // 0.5s struct ResourceManagerClient : public BnResourceManagerClient { ResourceManagerClient(MediaCodec* codec) : mMediaCodec(codec) {} @@ -74,6 +75,12 @@ struct ResourceManagerClient : public BnResourceManagerClient { return true; } status_t err = codec->reclaim(); + if (err == WOULD_BLOCK) { + ALOGD("Wait for the client to release codec."); + usleep(kMaxReclaimWaitTimeInUs); + ALOGD("Try to reclaim again."); + err = codec->reclaim(true /* force */); + } if (err != OK) { ALOGW("ResourceManagerClient failed to release codec with err %d", err); } @@ -571,10 +578,26 @@ status_t MediaCodec::stop() { return PostAndAwaitResponse(msg, &response); } -status_t MediaCodec::reclaim() { +bool MediaCodec::hasPendingBuffer(int portIndex) { + const Vector &buffers = mPortBuffers[portIndex]; + for (size_t i = 0; i < buffers.size(); ++i) { + const BufferInfo &info = buffers.itemAt(i); + if (info.mOwnedByClient) { + return true; + } + } + return false; +} + +bool MediaCodec::hasPendingBuffer() { + return hasPendingBuffer(kPortIndexInput) || hasPendingBuffer(kPortIndexOutput); +} + +status_t MediaCodec::reclaim(bool force) { ALOGD("MediaCodec::reclaim(%p) %s", this, mInitName.c_str()); sp msg = new AMessage(kWhatRelease, this); msg->setInt32("reclaimed", 1); + msg->setInt32("force", force ? 1 : 0); sp response; return PostAndAwaitResponse(msg, &response); @@ -1787,6 +1810,23 @@ void MediaCodec::onMessageReceived(const sp &msg) { msg->findInt32("reclaimed", &reclaimed); if (reclaimed) { mReleasedByResourceManager = true; + + int32_t force = 0; + msg->findInt32("force", &force); + if (!force && hasPendingBuffer()) { + ALOGW("Can't reclaim codec right now due to pending buffers."); + + // return WOULD_BLOCK to ask resource manager to retry later. + sp response = new AMessage; + response->setInt32("err", WOULD_BLOCK); + response->postReply(replyID); + + // notify the async client + if (mFlags & kFlagIsAsync) { + onError(DEAD_OBJECT, ACTION_CODE_FATAL); + } + break; + } } if (!((mFlags & kFlagIsComponentAllocated) && targetState == UNINITIALIZED) // See 1 -- cgit v1.1