Detect and handle premature termination of a recording session

Change-Id: Ifb83b19f3e68580345b23efed5d5956fb81baeb8
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;
 }