Camera: Changes to avoid stretch in camcorder preview.

b/7178594

Camcorder preview stretch reported when we switch between resolution.
VFE will modify the preview size if preview and video aspect ratio are
not matching. Application will set video size only before recording
which can result in using old video size.

Change-Id: If65bf5c4ff1a9283e2d00af91d8a223606792b46
diff --git a/camera/QCameraHWI.cpp b/camera/QCameraHWI.cpp
index ae716fc..264a763 100755
--- a/camera/QCameraHWI.cpp
+++ b/camera/QCameraHWI.cpp
@@ -1289,21 +1289,7 @@
     case QCAMERA_HAL_PREVIEW_STARTED:
         //remember recordinghint value set by app
         mAppRecordingHint = mRecordingHint;
-        if (mRecordingHint == FALSE || mRestartPreview) {
-            ALOGE("%s: start recording when hint is false, stop preview first", __func__);
-            stopPreviewInternal();
-            mPreviewState = QCAMERA_HAL_PREVIEW_STOPPED;
-
-            // Set recording hint to TRUE
-            mRecordingHint = TRUE;
-            setRecordingHintValue(mRecordingHint);
-
-            // start preview again
-            mPreviewState = QCAMERA_HAL_PREVIEW_START;
-            if (startPreview2() == NO_ERROR)
-                mPreviewState = QCAMERA_HAL_PREVIEW_STARTED;
-            mRestartPreview = false;
-        }
+        pausePreviewForVideo();
         ret =  mStreamRecord->start();
         if (MM_CAMERA_OK != ret){
             ALOGE("%s: error - mStreamRecord->start!", __func__);
@@ -1612,10 +1598,11 @@
        frame->snapshot.thumbnail.idx = frame->video.video.idx;
     }
 
-    dim.picture_width = pme->mDimension.video_width;
-    dim.picture_height = pme->mDimension.video_height;
-    dim.ui_thumbnail_width = pme->mDimension.video_width;
-    dim.ui_thumbnail_height = pme->mDimension.video_height;
+    dim.picture_width = pme->mVideoWidth;
+    dim.picture_height = pme->mVideoHeight;
+    dim.ui_thumbnail_width = pme->mVideoWidth;
+    dim.ui_thumbnail_height = pme->mVideoHeight;
+
     if (mNuberOfVFEOutputs == 1){
        dim.main_img_format = pme->mDimension.prev_format;
        dim.thumb_format = pme->mDimension.prev_format;
@@ -1624,7 +1611,7 @@
        dim.thumb_format = pme->mDimension.enc_format;
     }
 
-    mJpegMaxSize = pme->mDimension.video_width * pme->mDimension.video_width * 1.5;
+    mJpegMaxSize = dim.picture_width * dim.picture_height * 1.5;
 
     ALOGE("Picture w = %d , h = %d, size = %d",dim.picture_width,dim.picture_height,mJpegMaxSize);
      if (pme->mStreamLiveSnap){
@@ -1649,10 +1636,6 @@
     ((QCameraStream_Snapshot*)(pme->mStreamLiveSnap))->takePictureLiveshot(frame,&dim,mJpegMaxSize);
 
 #else
-
-
-
-
   if(MM_CAMERA_OK != cam_evt_buf_done(pme->mCameraId,frame )) {
     ALOGE(" BUF DONE FAILED");
   }
@@ -2659,6 +2642,60 @@
     }
 }
 
+void QCameraHardwareInterface::pausePreviewForVideo()
+{
+    status_t ret = NO_ERROR;
+    bool restart = FALSE;
+    cam_ctrl_dimension_t dim;
+
+    /*  get existing preview information, by qury mm_camera*/
+    memset(&dim, 0, sizeof(cam_ctrl_dimension_t));
+    ret = cam_config_get_parm(mCameraId, MM_CAMERA_PARM_DIMENSION,&dim);
+
+    if (MM_CAMERA_OK != ret) {
+      ALOGE("%s: X error- can't get preview dimension!", __func__);
+      return;
+    }
+
+    if (mRestartPreview) {
+        mRestartPreview = false;
+        ALOGE("%s: Restarting Preview. Video Size changed",__func__);
+        restart |= TRUE;
+    }
+    if (mRecordingHint == FALSE) {
+        // Set recording hint to TRUE
+        mRecordingHint = TRUE;
+        setRecordingHintValue(mRecordingHint);
+        ALOGE("%s: Restarting Preview. Hint not set",__func__);
+        restart |= TRUE;
+    }
+
+    if(dim.video_width != mVideoWidth || dim.video_height != mVideoHeight){
+        ALOGE("%s: Restarting Preview. New Video Size set",__func__);
+        restart |= TRUE;
+    }
+
+    if (restart) {
+        stopPreviewInternal();
+        mPreviewState = QCAMERA_HAL_PREVIEW_STOPPED;
+
+        mDimension.display_width = mPreviewWidth;
+        mDimension.display_height= mPreviewHeight;
+        mDimension.orig_video_width = mVideoWidth;
+        mDimension.orig_video_height = mVideoHeight;
+        mDimension.video_width = mVideoWidth;
+        mDimension.video_height = mVideoHeight;
+
+        // start preview again
+        mPreviewState = QCAMERA_HAL_PREVIEW_START;
+        if (startPreview2() == NO_ERROR){
+            mPreviewState = QCAMERA_HAL_PREVIEW_STARTED;
+        }else{
+            ALOGE("%s: Restart for Video Failed",__func__);
+        }
+    }
+}
+
 /**/
 bool QCameraHardwareInterface::getHdrInfoAndSetExp( int max_num_frm, int *num_frame, int *exp)
 {
diff --git a/camera/QCameraHWI.h b/camera/QCameraHWI.h
index d46de2b..1bd27a5 100755
--- a/camera/QCameraHWI.h
+++ b/camera/QCameraHWI.h
@@ -550,6 +550,7 @@
     //status_t startPreviewZSL();
     void pausePreviewForSnapshot();
     void pausePreviewForZSL();
+    void pausePreviewForVideo();
     status_t resumePreviewAfterSnapshot();
 
     status_t runFaceDetection();
@@ -688,7 +689,7 @@
 
     cam_ctrl_dimension_t mDimension;
     int  mPreviewWidth, mPreviewHeight;
-    int  videoWidth, videoHeight;
+    int  mVideoWidth, mVideoHeight;
     int  thumbnailWidth, thumbnailHeight;
     int  maxSnapshotWidth, maxSnapshotHeight;
     int  mPreviewFormat;
diff --git a/camera/QCameraHWI_Parm.cpp b/camera/QCameraHWI_Parm.cpp
index a8a762a..287e05c 100755
--- a/camera/QCameraHWI_Parm.cpp
+++ b/camera/QCameraHWI_Parm.cpp
@@ -2472,26 +2472,26 @@
         //If application didn't set this parameter string, use the values from
         //getPreviewSize() as video dimensions.
         ALOGE("No Record Size requested, use the preview dimensions");
-        videoWidth = mPreviewWidth;
-        videoHeight = mPreviewHeight;
+        mVideoWidth = mPreviewWidth;
+        mVideoHeight = mPreviewHeight;
     } else {
         //Extract the record witdh and height that application requested.
         ALOGV("%s: requested record size %s", __func__, str);
-        if(!parse_size(str, videoWidth, videoHeight)) {
+        if(!parse_size(str, mVideoWidth, mVideoHeight)) {
             parse_size(str_t, old_vid_w, old_vid_h);
-            if(old_vid_w != videoWidth || old_vid_h != videoHeight) {
+            if(old_vid_w != mVideoWidth || old_vid_h != mVideoHeight) {
                 mRestartPreview = true; 
                 ALOGE("%s: Video sizes changes, Restart preview...", __func__, str);
             }
             mParameters.set(QCameraParameters::KEY_VIDEO_SIZE, str);
             //VFE output1 shouldn't be greater than VFE output2.
-            if( (mPreviewWidth > videoWidth) || (mPreviewHeight > videoHeight)) {
+            if( (mPreviewWidth > mVideoWidth) || (mPreviewHeight > mVideoHeight)) {
                 //Set preview sizes as record sizes.
                 ALOGE("Preview size %dx%d is greater than record size %dx%d,\
                         resetting preview size to record size",mPreviewWidth,
-                        mPreviewHeight, videoWidth, videoHeight);
-                mPreviewWidth = videoWidth;
-                mPreviewHeight = videoHeight;
+                        mPreviewHeight, mVideoWidth, mVideoHeight);
+                mPreviewWidth = mVideoWidth;
+                mPreviewHeight = mVideoHeight;
                 mParameters.setPreviewSize(mPreviewWidth, mPreviewHeight);
             }
 
@@ -2503,8 +2503,8 @@
                  * request different preview sizes like 768x432
                  */
                 ALOGE("3D mod is on");
-                mPreviewWidth = videoWidth;
-                mPreviewHeight = videoHeight;
+                mPreviewWidth = mVideoWidth;
+                mPreviewHeight = mVideoHeight;
                 mParameters.setPreviewSize(mPreviewWidth, mPreviewHeight);
             }
         } else {
@@ -2514,13 +2514,7 @@
         }
     }
     ALOGE("%s: preview dimensions: %dx%d", __func__, mPreviewWidth, mPreviewHeight);
-    ALOGE("%s: video dimensions: %dx%d", __func__, videoWidth, videoHeight);
-    mDimension.display_width = mPreviewWidth;
-    mDimension.display_height= mPreviewHeight;
-    mDimension.orig_video_width = videoWidth;
-    mDimension.orig_video_height = videoHeight;
-    mDimension.video_width = videoWidth;
-    mDimension.video_height = videoHeight;
+    ALOGE("%s: video dimensions: %dx%d", __func__, mVideoWidth, mVideoHeight);
 
     ALOGV("%s: X", __func__);
     return NO_ERROR;
@@ -2581,8 +2575,14 @@
             ALOGE("setPreviewSize:  width: %d   heigh: %d", width, height);
             mPreviewWidth = width;
             mPreviewHeight = height;
-            mDimension.display_width = width;
-            mDimension.display_height = height;
+
+            mDimension.display_width = mPreviewWidth;
+            mDimension.display_height= mPreviewHeight;
+            mDimension.orig_video_width = mPreviewWidth;
+            mDimension.orig_video_height = mPreviewHeight;
+            mDimension.video_width = mPreviewWidth;
+            mDimension.video_height = mPreviewHeight;
+
             return NO_ERROR;
         }
     }