media: Fix memory leak when recording duration exceeds duration limit.

When recording got to the duration limit, MPEG4Writer should also stop the source
before exiting the loop. This will stop the video/audio encoder from copying
and encoding buffer. Otherwise, those encoded buffers will be leaked as they are
not released inside MPEG4Writer.

Also fix the other cases when malformed file is generated.

Bug:31530524
Change-Id: I0233f13e3d2a9110b35375a7394e6c8c36494342
diff --git a/media/libstagefright/MPEG4Writer.cpp b/media/libstagefright/MPEG4Writer.cpp
index f84f484..5e96c2b 100644
--- a/media/libstagefright/MPEG4Writer.cpp
+++ b/media/libstagefright/MPEG4Writer.cpp
@@ -2481,12 +2481,16 @@
             ALOGW("Recorded file size exceeds limit %" PRId64 "bytes",
                     mOwner->mMaxFileSizeLimitBytes);
             mOwner->notify(MEDIA_RECORDER_EVENT_INFO, MEDIA_RECORDER_INFO_MAX_FILESIZE_REACHED, 0);
+            copy->release();
+            mSource->stop();
             break;
         }
         if (mOwner->exceedsFileDurationLimit()) {
             ALOGW("Recorded file duration exceeds limit %" PRId64 "microseconds",
                     mOwner->mMaxFileDurationLimitUs);
             mOwner->notify(MEDIA_RECORDER_EVENT_INFO, MEDIA_RECORDER_INFO_MAX_DURATION_REACHED, 0);
+            copy->release();
+            mSource->stop();
             break;
         }
 
@@ -2507,6 +2511,7 @@
             int64_t durExcludingEarlierPausesUs = timestampUs - previousPausedDurationUs;
             if (WARN_UNLESS(durExcludingEarlierPausesUs >= 0ll, "for %s track", trackName)) {
                 copy->release();
+                mSource->stop();
                 mIsMalformed = true;
                 break;
             }
@@ -2514,6 +2519,7 @@
             int64_t pausedDurationUs = durExcludingEarlierPausesUs - mTrackDurationUs;
             if (WARN_UNLESS(pausedDurationUs >= lastDurationUs, "for %s track", trackName)) {
                 copy->release();
+                mSource->stop();
                 mIsMalformed = true;
                 break;
             }
@@ -2525,6 +2531,7 @@
         timestampUs -= previousPausedDurationUs;
         if (WARN_UNLESS(timestampUs >= 0ll, "for %s track", trackName)) {
             copy->release();
+            mSource->stop();
             mIsMalformed = true;
             break;
         }
@@ -2553,6 +2560,7 @@
                     timestampUs + kMaxCttsOffsetTimeUs - decodingTimeUs;
             if (WARN_UNLESS(cttsOffsetTimeUs >= 0ll, "for %s track", trackName)) {
                 copy->release();
+                mSource->stop();
                 mIsMalformed = true;
                 break;
             }
@@ -2566,6 +2574,7 @@
                     (cttsOffsetTimeUs * mTimeScale + 500000LL) / 1000000LL;
             if (WARN_UNLESS(currCttsOffsetTimeTicks <= 0x0FFFFFFFFLL, "for %s track", trackName)) {
                 copy->release();
+                mSource->stop();
                 mIsMalformed = true;
                 break;
             }
@@ -2609,6 +2618,7 @@
 
         if (WARN_UNLESS(timestampUs >= 0ll, "for %s track", trackName)) {
             copy->release();
+            mSource->stop();
             mIsMalformed = true;
             break;
         }