Handle some edge cases when seeking while starting up OMXCodec

These were exposed by the new preview-seekframe while paused code.
In particular, the codec may have been in state RECONFIGURING when attempting
to seek, or we may have initiated flushing of the output port and this may not
have completed yet by the time we want to reconfigure the output port.

Change-Id: Id7640ade11dbc7205a22f648ea0b5e3e9b49cf4b
related-to-bug: 3392259
diff --git a/include/media/stagefright/OMXCodec.h b/include/media/stagefright/OMXCodec.h
index 82948cb..f7d837a 100644
--- a/include/media/stagefright/OMXCodec.h
+++ b/include/media/stagefright/OMXCodec.h
@@ -175,6 +175,7 @@
     int64_t mSeekTimeUs;
     ReadOptions::SeekMode mSeekMode;
     int64_t mTargetTimeUs;
+    bool mOutputPortSettingsChangedPending;
 
     MediaBuffer *mLeftOverBuffer;
 
diff --git a/media/libstagefright/OMXCodec.cpp b/media/libstagefright/OMXCodec.cpp
index 247ace7..fccd68c 100644
--- a/media/libstagefright/OMXCodec.cpp
+++ b/media/libstagefright/OMXCodec.cpp
@@ -1434,6 +1434,7 @@
       mSeekTimeUs(-1),
       mSeekMode(ReadOptions::SEEK_CLOSEST_SYNC),
       mTargetTimeUs(-1),
+      mOutputPortSettingsChangedPending(false),
       mLeftOverBuffer(NULL),
       mPaused(false),
       mNativeWindow(nativeWindow) {
@@ -2344,6 +2345,14 @@
                     drainInputBuffers();
                     fillOutputBuffers();
                 }
+
+                if (mOutputPortSettingsChangedPending) {
+                    CODEC_LOGV(
+                            "Honoring deferred output port settings change.");
+
+                    mOutputPortSettingsChangedPending = false;
+                    onPortSettingsChanged(kPortIndexOutput);
+                }
             }
 
             break;
@@ -2407,6 +2416,8 @@
 
             CODEC_LOGV("Now Executing.");
 
+            mOutputPortSettingsChangedPending = false;
+
             setState(EXECUTING);
 
             // Buffers will be submitted to the component in the first
@@ -2520,6 +2531,14 @@
 
     CHECK_EQ((int)mState, (int)EXECUTING);
     CHECK_EQ(portIndex, (OMX_U32)kPortIndexOutput);
+    CHECK(!mOutputPortSettingsChangedPending);
+
+    if (mPortStatus[kPortIndexOutput] != ENABLED) {
+        CODEC_LOGV("Deferring output port settings change.");
+        mOutputPortSettingsChangedPending = true;
+        return;
+    }
+
     setState(RECONFIGURING);
 
     if (mQuirks & kNeedsFlushBeforeDisable) {
@@ -2712,6 +2731,7 @@
                 if (srcBuffer->meta_data()->findInt64(
                             kKeyTargetTime, &targetTimeUs)
                         && targetTimeUs >= 0) {
+                    CODEC_LOGV("targetTimeUs = %lld us", targetTimeUs);
                     mTargetTimeUs = targetTimeUs;
                 } else {
                     mTargetTimeUs = -1;
@@ -3385,6 +3405,14 @@
     }
 
     if (seeking) {
+        while (mState == RECONFIGURING) {
+            mBufferFilled.wait(mLock);
+        }
+
+        if (mState != EXECUTING) {
+            return UNKNOWN_ERROR;
+        }
+
         CODEC_LOGV("seeking to %lld us (%.2f secs)", seekTimeUs, seekTimeUs / 1E6);
 
         mSignalledEOS = false;