Merge "Report errors to applications if AudioRecord->start() fails" into honeycomb
diff --git a/media/libstagefright/AudioSource.cpp b/media/libstagefright/AudioSource.cpp
index 235d752..f96df18 100644
--- a/media/libstagefright/AudioSource.cpp
+++ b/media/libstagefright/AudioSource.cpp
@@ -91,14 +91,17 @@
         mStartTimeUs = startTimeUs;
     }
     status_t err = mRecord->start();
-
     if (err == OK) {
         mGroup = new MediaBufferGroup;
         mGroup->add_buffer(new MediaBuffer(kMaxBufferSize));
 
         mStarted = true;
+    } else {
+        delete mRecord;
+        mRecord = NULL;
     }
 
+
     return err;
 }
 
diff --git a/media/libstagefright/MPEG4Writer.cpp b/media/libstagefright/MPEG4Writer.cpp
index a47ee3a..d1a497f8 100644
--- a/media/libstagefright/MPEG4Writer.cpp
+++ b/media/libstagefright/MPEG4Writer.cpp
@@ -78,6 +78,7 @@
     volatile bool mDone;
     volatile bool mPaused;
     volatile bool mResumed;
+    volatile bool mStarted;
     bool mIsAvc;
     bool mIsAudio;
     bool mIsMPEG4;
@@ -951,6 +952,7 @@
       mDone(false),
       mPaused(false),
       mResumed(false),
+      mStarted(false),
       mTrackDurationUs(0),
       mEstimatedTrackSizeBytes(0),
       mSamplesHaveSameSize(true),
@@ -1279,6 +1281,7 @@
     pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE);
 
     mDone = false;
+    mStarted = true;
     mTrackDurationUs = 0;
     mReachedEOS = false;
     mEstimatedTrackSizeBytes = 0;
@@ -1307,10 +1310,14 @@
 
 status_t MPEG4Writer::Track::stop() {
     LOGD("Stopping %s track", mIsAudio? "Audio": "Video");
+    if (!mStarted) {
+        LOGE("Stop() called but track is not started");
+        return ERROR_END_OF_STREAM;
+    }
+
     if (mDone) {
         return OK;
     }
-
     mDone = true;
 
     void *dummy;
diff --git a/media/libstagefright/codecs/aacenc/AACEncoder.cpp b/media/libstagefright/codecs/aacenc/AACEncoder.cpp
index 9524884..a8b1292 100644
--- a/media/libstagefright/codecs/aacenc/AACEncoder.cpp
+++ b/media/libstagefright/codecs/aacenc/AACEncoder.cpp
@@ -151,7 +151,11 @@
     mInputFrame = new int16_t[mChannels * kNumSamplesPerFrame];
     CHECK(mInputFrame != NULL);
 
-    mSource->start(params);
+    status_t err = mSource->start(params);
+    if (err != OK) {
+         LOGE("AudioSource is not available");
+        return err;
+    }
 
     mStarted = true;
 
@@ -159,11 +163,6 @@
 }
 
 status_t AACEncoder::stop() {
-    if (!mStarted) {
-        LOGW("Call stop() when encoder has not started");
-        return OK;
-    }
-
     if (mInputBuffer) {
         mInputBuffer->release();
         mInputBuffer = NULL;
@@ -172,8 +171,17 @@
     delete mBufferGroup;
     mBufferGroup = NULL;
 
-    mSource->stop();
+    if (mInputFrame) {
+        delete[] mInputFrame;
+        mInputFrame = NULL;
+    }
 
+    if (!mStarted) {
+        LOGW("Call stop() when encoder has not started");
+        return ERROR_END_OF_STREAM;
+    }
+
+    mSource->stop();
     if (mEncoderHandle) {
         CHECK_EQ(VO_ERR_NONE, mApiHandle->Uninit(mEncoderHandle));
         mEncoderHandle = NULL;
@@ -182,10 +190,6 @@
     mApiHandle = NULL;
 
     mStarted = false;
-    if (mInputFrame) {
-        delete[] mInputFrame;
-        mInputFrame = NULL;
-    }
 
     return OK;
 }