Make sure that key frame is generated for timelapse video recording if there are at least two input video frames from camera source.

This will fix the stop failure issue where we have to wait n * time_interval before a key frame can be received by the file writer, where
 o n is the actual number of buffers advertised by the video encoder
 o time_interval is the interval settings for timelapse video recording
   specifying the time distance between neighboring input video frames

The fix includes two parts:
o OMXCodec will not submit all n buffers at one time, but instead submit one input
  frame at one time if it become available.

o Timelapse camera source made available the first two input frames and do not skip
  them so that the first compressed output frame data can be received regardless
  the specified time_interval

bug - 3367659

Change-Id: Ia68cc2cb0d71aa7dc54540e9ad82fae911ad530b
diff --git a/include/media/stagefright/CameraSource.h b/include/media/stagefright/CameraSource.h
index 794355b..4a39fbf2 100644
--- a/include/media/stagefright/CameraSource.h
+++ b/include/media/stagefright/CameraSource.h
@@ -158,6 +158,7 @@
     int32_t mNumFramesReceived;
     int64_t mLastFrameTimestampUs;
     bool mStarted;
+    int32_t mNumFramesEncoded;
 
     CameraSource(const sp<ICamera>& camera, int32_t cameraId,
                  Size videoSize, int32_t frameRate,
@@ -189,7 +190,6 @@
     List<int64_t> mFrameTimes;
 
     int64_t mFirstFrameTimeUs;
-    int32_t mNumFramesEncoded;
     int32_t mNumFramesDropped;
     int32_t mNumGlitches;
     int64_t mGlitchDurationThresholdUs;
diff --git a/include/media/stagefright/OMXCodec.h b/include/media/stagefright/OMXCodec.h
index 3251c28..82948cb 100644
--- a/include/media/stagefright/OMXCodec.h
+++ b/include/media/stagefright/OMXCodec.h
@@ -47,6 +47,9 @@
 
         // Store meta data in video buffers
         kStoreMetaDataInVideoBuffers = 32,
+
+        // Only submit one input buffer at one time.
+        kOnlySubmitOneInputBufferAtOneTime = 64,
     };
     static sp<MediaSource> Create(
             const sp<IOMX> &omx,
@@ -192,6 +195,7 @@
     Condition mBufferFilled;
 
     bool mIsMetaDataStoredInVideoBuffers;
+    bool mOnlySubmitOneBufferAtOneTime;
 
     OMXCodec(const sp<IOMX> &omx, IOMX::node_id node, uint32_t quirks,
              bool isEncoder, const char *mime, const char *componentName,
diff --git a/media/libmediaplayerservice/StagefrightRecorder.cpp b/media/libmediaplayerservice/StagefrightRecorder.cpp
index 65df68c..f134cba 100644
--- a/media/libmediaplayerservice/StagefrightRecorder.cpp
+++ b/media/libmediaplayerservice/StagefrightRecorder.cpp
@@ -1239,6 +1239,14 @@
         encoder_flags |= OMXCodec::kHardwareCodecsOnly;
         encoder_flags |= OMXCodec::kStoreMetaDataInVideoBuffers;
     }
+
+    // Do not wait for all the input buffers to become available.
+    // This give timelapse video recording faster response in
+    // receiving output from video encoder component.
+    if (mCaptureTimeLapse) {
+        encoder_flags |= OMXCodec::kOnlySubmitOneInputBufferAtOneTime;
+    }
+
     sp<MediaSource> encoder = OMXCodec::Create(
             client.interface(), enc_meta,
             true /* createEncoder */, cameraSource,
diff --git a/media/libstagefright/CameraSource.cpp b/media/libstagefright/CameraSource.cpp
index b1c6b18..66e0657 100644
--- a/media/libstagefright/CameraSource.cpp
+++ b/media/libstagefright/CameraSource.cpp
@@ -147,8 +147,8 @@
       mNumFramesReceived(0),
       mLastFrameTimestampUs(0),
       mStarted(false),
-      mFirstFrameTimeUs(0),
       mNumFramesEncoded(0),
+      mFirstFrameTimeUs(0),
       mNumFramesDropped(0),
       mNumGlitches(0),
       mGlitchDurationThresholdUs(200000),
diff --git a/media/libstagefright/CameraSourceTimeLapse.cpp b/media/libstagefright/CameraSourceTimeLapse.cpp
index b58b9d8..e6fe618 100644
--- a/media/libstagefright/CameraSourceTimeLapse.cpp
+++ b/media/libstagefright/CameraSourceTimeLapse.cpp
@@ -491,7 +491,10 @@
             }
         }
 
-        if (*timestampUs <
+        // Workaround to bypass the first 2 input frames for skipping.
+        // The first 2 output frames from the encoder are: decoder specific info and
+        // the compressed video frame data for the first input video frame.
+        if (mNumFramesEncoded >= 1 && *timestampUs <
                 (mLastTimeLapseFrameRealTimestampUs + mTimeBetweenTimeLapseFrameCaptureUs)) {
             // Skip all frames from last encoded frame until
             // sufficient time (mTimeBetweenTimeLapseFrameCaptureUs) has passed.
diff --git a/media/libstagefright/OMXCodec.cpp b/media/libstagefright/OMXCodec.cpp
index 7c6e561..2a19b25 100644
--- a/media/libstagefright/OMXCodec.cpp
+++ b/media/libstagefright/OMXCodec.cpp
@@ -522,6 +522,12 @@
     if (flags & kStoreMetaDataInVideoBuffers) {
         mIsMetaDataStoredInVideoBuffers = true;
     }
+
+    mOnlySubmitOneBufferAtOneTime = false;
+    if (flags & kOnlySubmitOneInputBufferAtOneTime) {
+        mOnlySubmitOneBufferAtOneTime = true;
+    }
+
     if (!(flags & kIgnoreCodecSpecificData)) {
         uint32_t type;
         const void *data;
@@ -2610,7 +2616,17 @@
 
     Vector<BufferInfo> *buffers = &mPortBuffers[kPortIndexInput];
     for (size_t i = 0; i < buffers->size(); ++i) {
-        if (!drainInputBuffer(&buffers->editItemAt(i))) {
+        BufferInfo *info = &buffers->editItemAt(i);
+
+        if (info->mStatus != OWNED_BY_US) {
+            continue;
+        }
+
+        if (!drainInputBuffer(info)) {
+            break;
+        }
+
+        if (mOnlySubmitOneBufferAtOneTime) {
             break;
         }
     }