Initial checkin for pause and resume control

Change-Id: Ibdcf7bea5fb66baa81878704ba4091dfcfe382ee
diff --git a/media/libstagefright/AMRWriter.cpp b/media/libstagefright/AMRWriter.cpp
index aec7394..8951f5b 100644
--- a/media/libstagefright/AMRWriter.cpp
+++ b/media/libstagefright/AMRWriter.cpp
@@ -29,13 +29,17 @@
 AMRWriter::AMRWriter(const char *filename)
     : mFile(fopen(filename, "wb")),
       mInitCheck(mFile != NULL ? OK : NO_INIT),
-      mStarted(false) {
+      mStarted(false),
+      mPaused(false),
+      mResumed(false) {
 }
 
 AMRWriter::AMRWriter(int fd)
     : mFile(fdopen(fd, "wb")),
       mInitCheck(mFile != NULL ? OK : NO_INIT),
-      mStarted(false) {
+      mStarted(false),
+      mPaused(false),
+      mResumed(false) {
 }
 
 AMRWriter::~AMRWriter() {
@@ -98,10 +102,19 @@
         return mInitCheck;
     }
 
-    if (mStarted || mSource == NULL) {
+    if (mSource == NULL) {
         return UNKNOWN_ERROR;
     }
 
+    if (mStarted && mPaused) {
+        mPaused = false;
+        mResumed = true;
+        return OK;
+    } else if (mStarted) {
+        // Already started, does nothing
+        return OK;
+    }
+
     status_t err = mSource->start();
 
     if (err != OK) {
@@ -123,6 +136,13 @@
     return OK;
 }
 
+void AMRWriter::pause() {
+    if (!mStarted) {
+        return;
+    }
+    mPaused = true;
+}
+
 void AMRWriter::stop() {
     if (!mStarted) {
         return;
@@ -163,6 +183,9 @@
     mEstimatedDurationUs = 0;
     mEstimatedSizeBytes = 0;
     bool stoppedPrematurely = true;
+    int64_t previousPausedDurationUs = 0;
+    int64_t maxTimestampUs = 0;
+
     while (!mDone) {
         MediaBuffer *buffer;
         status_t err = mSource->read(&buffer);
@@ -171,6 +194,12 @@
             break;
         }
 
+        if (mPaused) {
+            buffer->release();
+            buffer = NULL;
+            continue;
+        }
+
         mEstimatedSizeBytes += buffer->range_length();
         if (exceedsFileSizeLimit()) {
             buffer->release();
@@ -184,6 +213,17 @@
         if (timestampUs > mEstimatedDurationUs) {
             mEstimatedDurationUs = timestampUs;
         }
+        if (mResumed) {
+            previousPausedDurationUs += (timestampUs - maxTimestampUs - 20000);
+            mResumed = false;
+        }
+        timestampUs -= previousPausedDurationUs;
+        LOGV("time stamp: %lld, previous paused duration: %lld",
+                timestampUs, previousPausedDurationUs);
+        if (timestampUs > maxTimestampUs) {
+            maxTimestampUs = timestampUs;
+        }
+
         if (exceedsFileDurationLimit()) {
             buffer->release();
             buffer = NULL;