Merge "Detect and handle premature termination of a recording session" into kraken
diff --git a/include/media/mediarecorder.h b/include/media/mediarecorder.h
index 9ea6c7b..eead166 100644
--- a/include/media/mediarecorder.h
+++ b/include/media/mediarecorder.h
@@ -135,7 +135,8 @@
 enum media_recorder_info_type {
     MEDIA_RECORDER_INFO_UNKNOWN                   = 1,
     MEDIA_RECORDER_INFO_MAX_DURATION_REACHED      = 800,
-    MEDIA_RECORDER_INFO_MAX_FILESIZE_REACHED      = 801
+    MEDIA_RECORDER_INFO_MAX_FILESIZE_REACHED      = 801,
+    MEDIA_RECORDER_INFO_STOP_PREMATURELY          = 802
 };
 
 // ----------------------------------------------------------------------------
diff --git a/media/libmediaplayerservice/StagefrightRecorder.cpp b/media/libmediaplayerservice/StagefrightRecorder.cpp
index cb08100..8404779 100644
--- a/media/libmediaplayerservice/StagefrightRecorder.cpp
+++ b/media/libmediaplayerservice/StagefrightRecorder.cpp
@@ -269,7 +269,7 @@
 
 status_t StagefrightRecorder::setParamMaxDurationOrFileSize(int64_t limit,
         bool limit_is_duration) {
-    LOGV("setParamMaxDurationOrFileSize: limit (%d) for %s",
+    LOGV("setParamMaxDurationOrFileSize: limit (%lld) for %s",
             limit, limit_is_duration?"duration":"size");
     if (limit_is_duration) {  // limit is in ms
         if (limit <= 1000) {  // XXX: 1 second
@@ -563,11 +563,6 @@
             || mVideoSource == VIDEO_SOURCE_CAMERA) {
         CHECK(mCamera != NULL);
 
-        if (mCamera == 0) {
-            mCamera = Camera::connect(0);
-        }
-        CHECK(mCamera != NULL);
-
         // Set the actual video recording frame size
         CameraParameters params(mCamera->getParameters());
         params.setPreviewSize(mVideoWidth, mVideoHeight);
diff --git a/media/libstagefright/AMRWriter.cpp b/media/libstagefright/AMRWriter.cpp
index 0d54235..aec7394 100644
--- a/media/libstagefright/AMRWriter.cpp
+++ b/media/libstagefright/AMRWriter.cpp
@@ -162,6 +162,7 @@
 void AMRWriter::threadFunc() {
     mEstimatedDurationUs = 0;
     mEstimatedSizeBytes = 0;
+    bool stoppedPrematurely = true;
     while (!mDone) {
         MediaBuffer *buffer;
         status_t err = mSource->read(&buffer);
@@ -202,10 +203,22 @@
             break;
         }
 
+        // XXX: How to tell it is stopped prematurely?
+        if (stoppedPrematurely) {
+            stoppedPrematurely = false;
+        }
+
         buffer->release();
         buffer = NULL;
     }
 
+    if (stoppedPrematurely) {
+        notify(MEDIA_RECORDER_EVENT_INFO, MEDIA_RECORDER_INFO_STOP_PREMATURELY, 0);
+    }
+
+    fflush(mFile);
+    fclose(mFile);
+    mFile = NULL;
     mReachedEOS = true;
 }
 
diff --git a/media/libstagefright/MPEG4Writer.cpp b/media/libstagefright/MPEG4Writer.cpp
index 788534d..5361f92 100644
--- a/media/libstagefright/MPEG4Writer.cpp
+++ b/media/libstagefright/MPEG4Writer.cpp
@@ -924,7 +924,9 @@
         buffer = NULL;
     }
 
-    CHECK(!mSampleInfos.empty());
+    if (mSampleInfos.empty()) {
+        mOwner->notify(MEDIA_RECORDER_EVENT_INFO, MEDIA_RECORDER_INFO_STOP_PREMATURELY, 0);
+    }
 
     // Last chunk
     if (!mChunkSamples.empty()) {