Signal errors to the client instead of asserting in ACodec.

Also make sure NuPlayer can properly shutdown in certain edge cases.

Change-Id: Iceb16d600d87ba66c802e60e95bf62f66487a453
diff --git a/media/libmediaplayerservice/nuplayer/NuPlayer.cpp b/media/libmediaplayerservice/nuplayer/NuPlayer.cpp
index 7fb141a..1f08a91 100644
--- a/media/libmediaplayerservice/nuplayer/NuPlayer.cpp
+++ b/media/libmediaplayerservice/nuplayer/NuPlayer.cpp
@@ -316,9 +316,11 @@
                                 &cropLeft, &cropTop, &cropRight, &cropBottom));
 
                     LOGV("Video output format changed to %d x %d "
-                         "(crop: %d, %d, %d, %d)",
+                         "(crop: %d x %d @ (%d, %d))",
                          width, height,
-                         cropLeft, cropTop, cropRight, cropBottom);
+                         (cropRight - cropLeft + 1),
+                         (cropBottom - cropTop + 1),
+                         cropLeft, cropTop);
 
                     notifyListener(
                             MEDIA_SET_VIDEO_SIZE,
diff --git a/media/libmediaplayerservice/nuplayer/NuPlayerRenderer.cpp b/media/libmediaplayerservice/nuplayer/NuPlayerRenderer.cpp
index 35ed43f..8f213da 100644
--- a/media/libmediaplayerservice/nuplayer/NuPlayerRenderer.cpp
+++ b/media/libmediaplayerservice/nuplayer/NuPlayerRenderer.cpp
@@ -395,29 +395,40 @@
         postDrainVideoQueue();
     }
 
-    if (mSyncQueues && !mAudioQueue.empty() && !mVideoQueue.empty()) {
-        int64_t firstAudioTimeUs;
-        int64_t firstVideoTimeUs;
-        CHECK((*mAudioQueue.begin()).mBuffer->meta()
-                ->findInt64("timeUs", &firstAudioTimeUs));
-        CHECK((*mVideoQueue.begin()).mBuffer->meta()
-                ->findInt64("timeUs", &firstVideoTimeUs));
-
-        int64_t diff = firstVideoTimeUs - firstAudioTimeUs;
-
-        LOGV("queueDiff = %.2f secs", diff / 1E6);
-
-        if (diff > 100000ll) {
-            // Audio data starts More than 0.1 secs before video.
-            // Drop some audio.
-
-            (*mAudioQueue.begin()).mNotifyConsumed->post();
-            mAudioQueue.erase(mAudioQueue.begin());
-            return;
-        }
-
-        syncQueuesDone();
+    if (!mSyncQueues || mAudioQueue.empty() || mVideoQueue.empty()) {
+        return;
     }
+
+    sp<ABuffer> firstAudioBuffer = (*mAudioQueue.begin()).mBuffer;
+    sp<ABuffer> firstVideoBuffer = (*mVideoQueue.begin()).mBuffer;
+
+    if (firstAudioBuffer == NULL || firstVideoBuffer == NULL) {
+        // EOS signalled on either queue.
+        syncQueuesDone();
+        return;
+    }
+
+    int64_t firstAudioTimeUs;
+    int64_t firstVideoTimeUs;
+    CHECK(firstAudioBuffer->meta()
+            ->findInt64("timeUs", &firstAudioTimeUs));
+    CHECK(firstVideoBuffer->meta()
+            ->findInt64("timeUs", &firstVideoTimeUs));
+
+    int64_t diff = firstVideoTimeUs - firstAudioTimeUs;
+
+    LOGV("queueDiff = %.2f secs", diff / 1E6);
+
+    if (diff > 100000ll) {
+        // Audio data starts More than 0.1 secs before video.
+        // Drop some audio.
+
+        (*mAudioQueue.begin()).mNotifyConsumed->post();
+        mAudioQueue.erase(mAudioQueue.begin());
+        return;
+    }
+
+    syncQueuesDone();
 }
 
 void NuPlayer::Renderer::syncQueuesDone() {
diff --git a/media/libstagefright/ACodec.cpp b/media/libstagefright/ACodec.cpp
index 5d91f6a..e9dc61c 100644
--- a/media/libstagefright/ACodec.cpp
+++ b/media/libstagefright/ACodec.cpp
@@ -1738,7 +1738,17 @@
 void ACodec::LoadedToIdleState::stateEntered() {
     LOGV("[%s] Now Loaded->Idle", mCodec->mComponentName.c_str());
 
-    CHECK_EQ(allocateBuffers(), (status_t)OK);
+    status_t err;
+    if ((err = allocateBuffers()) != OK) {
+        LOGE("Failed to allocate buffers after transitioning to IDLE state "
+             "(error 0x%08x)",
+             err);
+
+        sp<AMessage> notify = mCodec->mNotify->dup();
+        notify->setInt32("what", ACodec::kWhatError);
+        notify->setInt32("omx-error", OMX_ErrorUndefined);
+        notify->post();
+    }
 }
 
 status_t ACodec::LoadedToIdleState::allocateBuffers() {
@@ -2046,8 +2056,18 @@
                             mCodec->mNode, OMX_CommandPortEnable, kPortIndexOutput),
                          (status_t)OK);
 
-                CHECK_EQ(mCodec->allocateBuffersOnPort(kPortIndexOutput),
-                         (status_t)OK);
+                status_t err;
+                if ((err = mCodec->allocateBuffersOnPort(
+                                kPortIndexOutput)) != OK) {
+                    LOGE("Failed to allocate output port buffers after "
+                         "port reconfiguration (error 0x%08x)",
+                         err);
+
+                    sp<AMessage> notify = mCodec->mNotify->dup();
+                    notify->setInt32("what", ACodec::kWhatError);
+                    notify->setInt32("omx-error", OMX_ErrorUndefined);
+                    notify->post();
+                }
 
                 return true;
             } else if (data1 == (OMX_U32)OMX_CommandPortEnable) {