Merge "stagefright: prevent allocating stale buffers for OMX decoders" into mnc-dev
diff --git a/media/libstagefright/ACodec.cpp b/media/libstagefright/ACodec.cpp
index c6e45af..7452e4b 100644
--- a/media/libstagefright/ACodec.cpp
+++ b/media/libstagefright/ACodec.cpp
@@ -5242,6 +5242,7 @@
         if (err == OK) {
             info->mStatus = BufferInfo::OWNED_BY_NATIVE_WINDOW;
         } else {
+            ALOGE("queueBuffer failed in onOutputBufferDrained: %d", err);
             mCodec->signalError(OMX_ErrorUndefined, makeNoSideEffectStatus(err));
             info->mStatus = BufferInfo::OWNED_BY_US;
             // keeping read fence as write fence to avoid clobbering
diff --git a/media/libstagefright/MediaCodec.cpp b/media/libstagefright/MediaCodec.cpp
index b576cd9..69f44ed 100644
--- a/media/libstagefright/MediaCodec.cpp
+++ b/media/libstagefright/MediaCodec.cpp
@@ -2528,7 +2528,25 @@
         err = native_window_api_connect(surface.get(), NATIVE_WINDOW_API_MEDIA);
         if (err == BAD_VALUE) {
             ALOGI("native window already connected. Assuming no change of surface");
-        } else if (err != OK) {
+        } else if (err == OK) {
+            // Require a fresh set of buffers after each connect by using a unique generation
+            // number. Rely on the fact that max supported process id by Linux is 2^22.
+            // PID is never 0 so we don't have to worry that we use the default generation of 0.
+            // TODO: come up with a unique scheme if other producers also set the generation number.
+            static uint32_t mSurfaceGeneration = 0;
+            uint32_t generation = (getpid() << 10) | (++mSurfaceGeneration & ((1 << 10) - 1));
+            surface->setGenerationNumber(generation);
+            ALOGI("[%s] setting surface generation to %u", mComponentName.c_str(), generation);
+
+            // HACK: clear any free buffers. Remove when connect will automatically do this.
+            // This is needed as the consumer may be holding onto stale frames that it can reattach
+            // to this surface after disconnect/connect, and those free frames would inherit the new
+            // generation number. Disconnecting after setting a unique generation prevents this.
+            native_window_api_disconnect(surface.get(), NATIVE_WINDOW_API_MEDIA);
+            err = native_window_api_connect(surface.get(), NATIVE_WINDOW_API_MEDIA);
+        }
+
+        if (err != OK) {
             ALOGE("native_window_api_connect returned an error: %s (%d)", strerror(-err), err);
         }
     }
@@ -2538,6 +2556,8 @@
 status_t MediaCodec::disconnectFromSurface() {
     status_t err = OK;
     if (mSurface != NULL) {
+        // Resetting generation is not technically needed, but there is no need to keep it either
+        mSurface->setGenerationNumber(0);
         err = native_window_api_disconnect(mSurface.get(), NATIVE_WINDOW_API_MEDIA);
         if (err != OK) {
             ALOGW("native_window_api_disconnect returned an error: %s (%d)", strerror(-err), err);
diff --git a/media/libstagefright/MediaSync.cpp b/media/libstagefright/MediaSync.cpp
index b402e48..52077a7 100644
--- a/media/libstagefright/MediaSync.cpp
+++ b/media/libstagefright/MediaSync.cpp
@@ -558,7 +558,6 @@
             return;
         }
     }
-    ++mNumOutstandingBuffers;
 
     // Acquire and detach the buffer from the input.
     BufferItem bufferItem;
@@ -567,6 +566,7 @@
         ALOGE("acquiring buffer from input failed (%d)", status);
         return;
     }
+    ++mNumOutstandingBuffers;
 
     ALOGV("acquired buffer %#llx from input", (long long)bufferItem.mGraphicBuffer->getId());
 
@@ -608,6 +608,7 @@
 
     // Attach and queue the buffer to the output.
     int slot;
+    mOutput->setGenerationNumber(bufferItem.mGraphicBuffer->getGenerationNumber());
     status_t status = mOutput->attachBuffer(&slot, bufferItem.mGraphicBuffer);
     ALOGE_IF(status != NO_ERROR, "attaching buffer to output failed (%d)", status);
     if (status == NO_ERROR) {
@@ -695,16 +696,13 @@
         ALOGE_IF(status != NO_ERROR, "releasing buffer to input failed (%d)", status);
     }
 
-    if (status != NO_ERROR) {
-        // TODO: do we need to try to return this buffer later?
-        return;
-    }
-
-    ALOGV("released buffer %#llx to input", (long long)oldBuffer->getId());
-
     // Notify any waiting onFrameAvailable calls.
     --mNumOutstandingBuffers;
     mReleaseCondition.signal();
+
+    if (status == NO_ERROR) {
+        ALOGV("released buffer %#llx to input", (long long)oldBuffer->getId());
+    }
 }
 
 void MediaSync::onAbandoned_l(bool isInput) {
diff --git a/media/libstagefright/omx/GraphicBufferSource.cpp b/media/libstagefright/omx/GraphicBufferSource.cpp
index a424819..1a7dc9d 100644
--- a/media/libstagefright/omx/GraphicBufferSource.cpp
+++ b/media/libstagefright/omx/GraphicBufferSource.cpp
@@ -843,13 +843,15 @@
         mConsumer->detachBuffer(id);
         mBufferSlot[id] = NULL;
 
-        mConsumer->attachBuffer(&id, buffer);
-        mConsumer->releaseBuffer(
-                id, 0, EGL_NO_DISPLAY, EGL_NO_SYNC_KHR, fence);
+        if (mConsumer->attachBuffer(&id, buffer) == OK) {
+            mConsumer->releaseBuffer(
+                    id, 0, EGL_NO_DISPLAY, EGL_NO_SYNC_KHR, fence);
+        }
     } else {
         mConsumer->releaseBuffer(
                 id, frameNum, EGL_NO_DISPLAY, EGL_NO_SYNC_KHR, fence);
     }
+    id = -1; // invalidate id
     mNumBufferAcquired--;
 }