Camera2: Fix recording bugs

 - Release recording stream on shutdown
 - Return recording gralloc buffers to queue when they are received by
   camera service after recording has been stopped (which is normal
   behavior thanks to pipeline latencies in camera pipelines)

Bug: 6243944
Change-Id: Ia13a501c726613fd1e404733456dbd10d7b00132
diff --git a/services/camera/libcameraservice/Camera2Client.cpp b/services/camera/libcameraservice/Camera2Client.cpp
index 8d4add4..9d904e8 100644
--- a/services/camera/libcameraservice/Camera2Client.cpp
+++ b/services/camera/libcameraservice/Camera2Client.cpp
@@ -338,6 +338,11 @@
         mCaptureStreamId = NO_STREAM;
     }
 
+    if (mRecordingStreamId != NO_STREAM) {
+        mDevice->deleteStream(mRecordingStreamId);
+        mRecordingStreamId = NO_STREAM;
+    }
+
     CameraService::Client::disconnect();
 }
 
@@ -1492,10 +1497,12 @@
     {
         Mutex::Autolock icl(mICameraLock);
         // TODO: Signal errors here upstream
+        bool discardData = false;
         if (mState != RECORD && mState != VIDEO_SNAPSHOT) {
-            ALOGE("%s: Camera %d: Recording image buffer produced unexpectedly!",
+            ALOGV("%s: Camera %d: Discarding recording image buffers received after "
+                    "recording done",
                     __FUNCTION__, mCameraId);
-            return;
+            discardData = true;
         }
 
         CpuConsumer::LockedBuffer imgBuffer;
@@ -1509,9 +1516,14 @@
         if (imgBuffer.format != (int)kRecordingFormat) {
             ALOGE("%s: Camera %d: Unexpected recording format: %x",
                     __FUNCTION__, mCameraId, imgBuffer.format);
+            discardData = true;
+        }
+
+        if (discardData) {
             mRecordingConsumer->unlockBuffer(imgBuffer);
             return;
         }
+
         size_t bufferSize = imgBuffer.width * imgBuffer.height * 3 / 2;
 
         if (mRecordingHeap == 0 ||