am 5bb603c1: am 2c157e0f: Merge "Make sure MediaCodec::stop() and MediaCodec::release() still return" into jb-mr2-dev

* commit '5bb603c1e1984dcac0bc7b0e4f82c623710d54a8':
  Make sure MediaCodec::stop() and MediaCodec::release() still return
diff --git a/media/libstagefright/MediaCodec.cpp b/media/libstagefright/MediaCodec.cpp
index 0d89c0f..e4e95d2 100644
--- a/media/libstagefright/MediaCodec.cpp
+++ b/media/libstagefright/MediaCodec.cpp
@@ -534,6 +534,20 @@
                             // the shutdown complete notification.
 
                             sendErrorReponse = false;
+
+                            if (omxError == OMX_ErrorResourcesLost
+                                    && internalError == DEAD_OBJECT) {
+                                // MediaServer died, there definitely won't
+                                // be a shutdown complete notification after
+                                // all.
+
+                                // note that we're directly going from
+                                // STOPPING->UNINITIALIZED, instead of the
+                                // usual STOPPING->INITIALIZED state.
+                                setState(UNINITIALIZED);
+
+                                (new AMessage)->postReply(mReplyID);
+                            }
                             break;
                         }
 
@@ -1013,8 +1027,16 @@
 
             if (mState != INITIALIZED
                     && mState != CONFIGURED && mState != STARTED) {
+                // We may be in "UNINITIALIZED" state already without the
+                // client being aware of this if media server died while
+                // we were being stopped. The client would assume that
+                // after stop() returned, it would be safe to call release()
+                // and it should be in this case, no harm to allow a release()
+                // if we're already uninitialized.
                 sp<AMessage> response = new AMessage;
-                response->setInt32("err", INVALID_OPERATION);
+                response->setInt32(
+                        "err",
+                        mState == UNINITIALIZED ? OK : INVALID_OPERATION);
 
                 response->postReply(replyID);
                 break;