libstagefright: Checks before freeing buffers on a port

- OMXCodec calls dequeueBuffer on the NativeWindow in a loop
  and registers output buffers with the OMX component.
- If a dequeueBuffer fails, the buffers already registered
  with the OMX component were not getting freed laeding
  to a memory leak.
- On an error, check state of OMXCodec before freeing buffers on a port.

CRs-Fixed: 393352

(cherry picked from commit f2e982497c5d16db14812ee09cef5cbdf0d96331)

Change-Id: Ib4eb48c189197d253e4175708ce1778b9c92f315
diff --git a/media/libstagefright/OMXCodec.cpp b/media/libstagefright/OMXCodec.cpp
index 8bd91ea..a4131f9 100644
--- a/media/libstagefright/OMXCodec.cpp
+++ b/media/libstagefright/OMXCodec.cpp
@@ -2037,9 +2037,15 @@
         cancelEnd = def.nBufferCountActual;
     }
 
-    for (OMX_U32 i = cancelStart; i < cancelEnd; i++) {
-        BufferInfo *info = &mPortBuffers[kPortIndexOutput].editItemAt(i);
-        cancelBufferToNativeWindow(info);
+
+    if (err != 0 &&
+        ((mState == LOADED) || (mState == LOADED_TO_IDLE))) {
+        freeBuffersOnPort(kPortIndexOutput);
+    } else {
+        for (OMX_U32 i = cancelStart; i < cancelEnd; i++) {
+            BufferInfo *info = &mPortBuffers[kPortIndexOutput].editItemAt(i);
+            cancelBufferToNativeWindow(info);
+        }
     }
 
     return err;