Fix callback sequence on picture taking

Also fixes a bug in JPEG converter.

Change-Id: I5bbeec96ec2bb0a9a6a333a3798880bb0b837c65
diff --git a/tools/emulator/system/camera/CallbackNotifier.cpp b/tools/emulator/system/camera/CallbackNotifier.cpp
index cec471f..7d83bb1 100755
--- a/tools/emulator/system/camera/CallbackNotifier.cpp
+++ b/tools/emulator/system/camera/CallbackNotifier.cpp
@@ -217,6 +217,16 @@
     }
 
     if (mTakingPicture) {
+        /* This happens just once. */
+        mTakingPicture = false;
+        /* The sequence of callbacks during picture taking is:
+         *  - CAMERA_MSG_SHUTTER
+         *  - CAMERA_MSG_RAW_IMAGE_NOTIFY
+         *  - CAMERA_MSG_COMPRESSED_IMAGE
+         */
+        if (isMessageEnabled(CAMERA_MSG_SHUTTER)) {
+            mNotifyCB(CAMERA_MSG_SHUTTER, 0, 0, mCBOpaque);
+        }
         if (isMessageEnabled(CAMERA_MSG_RAW_IMAGE_NOTIFY)) {
             mNotifyCB(CAMERA_MSG_RAW_IMAGE_NOTIFY, 0, 0, mCBOpaque);
         }
@@ -240,11 +250,6 @@
                 LOGE("%s: Compression failure in CAMERA_MSG_VIDEO_FRAME", __FUNCTION__);
             }
         }
-        if (isMessageEnabled(CAMERA_MSG_SHUTTER)) {
-            mNotifyCB(CAMERA_MSG_SHUTTER, 0, 0, mCBOpaque);
-        }
-        /* This happens just once. */
-        mTakingPicture = false;
     }
 }
 
diff --git a/tools/emulator/system/camera/EmulatedCamera.cpp b/tools/emulator/system/camera/EmulatedCamera.cpp
index 909760b..8f50eb8 100755
--- a/tools/emulator/system/camera/EmulatedCamera.cpp
+++ b/tools/emulator/system/camera/EmulatedCamera.cpp
@@ -503,15 +503,16 @@
 {
     LOGV("%s", __FUNCTION__);
 
+    EmulatedCameraDevice* camera_dev = getCameraDevice();
+    if (camera_dev->isStarted()) {
+        camera_dev->stopDeliveringFrames();
+        camera_dev->stopDevice();
+    }
+
     status_t res = mPreviewWindow.startPreview();
     if (res != NO_ERROR) {
         return res;
     }
-    if (getCameraDevice()->isStarted()) {
-        return NO_ERROR;
-    }
-
-    EmulatedCameraDevice* camera_dev = getCameraDevice();
 
     /* Make sure camera device is connected. */
     if (!camera_dev->isConnected()) {
@@ -573,15 +574,17 @@
     LOGV("%s", __FUNCTION__);
 
     status_t res = NO_ERROR;
-    /* Stop the camera. */
-    if (getCameraDevice()->isStarted()) {
-        getCameraDevice()->stopDeliveringFrames();
-        res = getCameraDevice()->stopDevice();
-    }
+    if (mPreviewWindow.isPreviewEnabled()) {
+        /* Stop the camera. */
+        if (getCameraDevice()->isStarted()) {
+            getCameraDevice()->stopDeliveringFrames();
+            res = getCameraDevice()->stopDevice();
+        }
 
-    if (res == NO_ERROR) {
-        /* Disable preview as well. */
-        mPreviewWindow.stopPreview();
+        if (res == NO_ERROR) {
+            /* Disable preview as well. */
+            mPreviewWindow.stopPreview();
+        }
     }
 
     return NO_ERROR;
diff --git a/tools/emulator/system/camera/JpegCompressor.cpp b/tools/emulator/system/camera/JpegCompressor.cpp
index d8976c2..0e538a1 100644
--- a/tools/emulator/system/camera/JpegCompressor.cpp
+++ b/tools/emulator/system/camera/JpegCompressor.cpp
@@ -50,7 +50,7 @@
     offsets[0] = 0;
     offsets[1] = width * height;
     mStrides[0] = width;
-    mStrides[1] = mStrides[2] = width / 4;
+    mStrides[1] = width;
     if (encode(&mStream, pY, width, height, offsets, quality)) {
         LOGV("%s: Compressed JPEG: %d[%dx%d] -> %d bytes",
              __FUNCTION__, (width * height * 12) / 8, width, height, mStream.getOffset());
@@ -59,7 +59,6 @@
         LOGE("%s: JPEG compression failed", __FUNCTION__);
         return errno ? errno : EINVAL;
     }
-
 }
 
 }; /* namespace android */