19066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project/*
29066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project**
39066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project** Copyright 2008, The Android Open Source Project
49066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project**
59066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project** Licensed under the Apache License, Version 2.0 (the "License");
69066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project** you may not use this file except in compliance with the License.
79066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project** You may obtain a copy of the License at
89066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project**
99066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project**     http://www.apache.org/licenses/LICENSE-2.0
109066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project**
119066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project** Unless required by applicable law or agreed to in writing, software
129066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project** distributed under the License is distributed on an "AS IS" BASIS,
139066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
149066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project** See the License for the specific language governing permissions and
159066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project** limitations under the License.
169066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project*/
179066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
189066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project//#define LOG_NDEBUG 0
199066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project#define LOG_TAG "Camera-JNI"
209066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project#include <utils/Log.h>
219066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
229066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project#include "jni.h"
239066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project#include "JNIHelp.h"
249066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project#include "android_runtime/AndroidRuntime.h"
25c99db2bc460cc795947d99076da380e22a21e493Igor Murashkin#include <android_runtime/android_graphics_SurfaceTexture.h>
263866f0d581ceaa165710feeee9f37fe1b0d7067dMathias Agopian#include <android_runtime/android_view_Surface.h>
279066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
28f7c6c5ad64c8e5d20e1806cc98b9919334c69614Eino-Ville Talvala#include <cutils/properties.h>
2994927dffce1626898b59579dfc5af53b5de8cef6Andrew Harp#include <utils/Vector.h>
30feb50af361e4305a25758966b6b5df2738c00259Ruben Brunk#include <utils/Errors.h>
3194927dffce1626898b59579dfc5af53b5de8cef6Andrew Harp
32d47f7d8b5fe3a3861d7cbdc5f912235407823c8eAndy McFadden#include <gui/GLConsumer.h>
338335f1ccccedb6655d96d9d5b697a7f0938235ddMathias Agopian#include <gui/Surface.h>
34000479f9e325b4e426a67033abd92d47da412725Mathias Agopian#include <camera/Camera.h>
350795272aa226f4e965968a03daddc53ce30b7cdaMathias Agopian#include <binder/IMemory.h>
369066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
379066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectusing namespace android;
389066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
39a1d662716b3da384dfe3a758f079e0cbd089784aIgor Murashkinenum {
40a1d662716b3da384dfe3a758f079e0cbd089784aIgor Murashkin    // Keep up to date with Camera.java
41a1d662716b3da384dfe3a758f079e0cbd089784aIgor Murashkin    CAMERA_HAL_API_VERSION_NORMAL_CONNECT = -2,
42a1d662716b3da384dfe3a758f079e0cbd089784aIgor Murashkin};
43a1d662716b3da384dfe3a758f079e0cbd089784aIgor Murashkin
449066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectstruct fields_t {
459066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    jfieldID    context;
46b8bb78f54b48868465a9d69d65fda08524ab5ae1Chih-Chung Chang    jfieldID    facing;
47b8bb78f54b48868465a9d69d65fda08524ab5ae1Chih-Chung Chang    jfieldID    orientation;
48f7c6c5ad64c8e5d20e1806cc98b9919334c69614Eino-Ville Talvala    jfieldID    canDisableShutterSound;
49f0d6a48c6ad852b84687127f14b1ffde02416dc4Wu-cheng Li    jfieldID    face_rect;
504c2292efbbfe7338138a9a0e8eefeaabdef1a0b4Wu-cheng Li    jfieldID    face_score;
510601ab1618e95d455230c4801a40cbc5c25221feIgor Murashkin    jfieldID    face_id;
520601ab1618e95d455230c4801a40cbc5c25221feIgor Murashkin    jfieldID    face_left_eye;
530601ab1618e95d455230c4801a40cbc5c25221feIgor Murashkin    jfieldID    face_right_eye;
540601ab1618e95d455230c4801a40cbc5c25221feIgor Murashkin    jfieldID    face_mouth;
554c2292efbbfe7338138a9a0e8eefeaabdef1a0b4Wu-cheng Li    jfieldID    rect_left;
564c2292efbbfe7338138a9a0e8eefeaabdef1a0b4Wu-cheng Li    jfieldID    rect_top;
574c2292efbbfe7338138a9a0e8eefeaabdef1a0b4Wu-cheng Li    jfieldID    rect_right;
584c2292efbbfe7338138a9a0e8eefeaabdef1a0b4Wu-cheng Li    jfieldID    rect_bottom;
590601ab1618e95d455230c4801a40cbc5c25221feIgor Murashkin    jfieldID    point_x;
600601ab1618e95d455230c4801a40cbc5c25221feIgor Murashkin    jfieldID    point_y;
619066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    jmethodID   post_event;
62bb1e275c0e684dd213f124da77110cdd9d6f090cWu-cheng Li    jmethodID   rect_constructor;
63bb1e275c0e684dd213f124da77110cdd9d6f090cWu-cheng Li    jmethodID   face_constructor;
640601ab1618e95d455230c4801a40cbc5c25221feIgor Murashkin    jmethodID   point_constructor;
659066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project};
669066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
679066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectstatic fields_t fields;
689066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectstatic Mutex sLock;
699066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
705e27115995c4506dddf4f1271effd4fb4c935ad3Dave Sparks// provides persistent context for calls from native code to Java
715e27115995c4506dddf4f1271effd4fb4c935ad3Dave Sparksclass JNICameraContext: public CameraListener
725e27115995c4506dddf4f1271effd4fb4c935ad3Dave Sparks{
735e27115995c4506dddf4f1271effd4fb4c935ad3Dave Sparkspublic:
745e27115995c4506dddf4f1271effd4fb4c935ad3Dave Sparks    JNICameraContext(JNIEnv* env, jobject weak_this, jclass clazz, const sp<Camera>& camera);
755e27115995c4506dddf4f1271effd4fb4c935ad3Dave Sparks    ~JNICameraContext() { release(); }
765e27115995c4506dddf4f1271effd4fb4c935ad3Dave Sparks    virtual void notify(int32_t msgType, int32_t ext1, int32_t ext2);
77bb1e275c0e684dd213f124da77110cdd9d6f090cWu-cheng Li    virtual void postData(int32_t msgType, const sp<IMemory>& dataPtr,
78bb1e275c0e684dd213f124da77110cdd9d6f090cWu-cheng Li                          camera_frame_metadata_t *metadata);
7959c1a935295cb30c2ba2f759855e89c174b42a07Dave Sparks    virtual void postDataTimestamp(nsecs_t timestamp, int32_t msgType, const sp<IMemory>& dataPtr);
80bb1e275c0e684dd213f124da77110cdd9d6f090cWu-cheng Li    void postMetadata(JNIEnv *env, int32_t msgType, camera_frame_metadata_t *metadata);
81e00cab707dcaf6f05adb5ccb9c80fdf25c483427James Dong    void addCallbackBuffer(JNIEnv *env, jbyteArray cbb, int msgType);
8294927dffce1626898b59579dfc5af53b5de8cef6Andrew Harp    void setCallbackMode(JNIEnv *env, bool installed, bool manualMode);
835e27115995c4506dddf4f1271effd4fb4c935ad3Dave Sparks    sp<Camera> getCamera() { Mutex::Autolock _l(mLock); return mCamera; }
84e00cab707dcaf6f05adb5ccb9c80fdf25c483427James Dong    bool isRawImageCallbackBufferAvailable() const;
855e27115995c4506dddf4f1271effd4fb4c935ad3Dave Sparks    void release();
865e27115995c4506dddf4f1271effd4fb4c935ad3Dave Sparks
875e27115995c4506dddf4f1271effd4fb4c935ad3Dave Sparksprivate:
885e27115995c4506dddf4f1271effd4fb4c935ad3Dave Sparks    void copyAndPost(JNIEnv* env, const sp<IMemory>& dataPtr, int msgType);
89e00cab707dcaf6f05adb5ccb9c80fdf25c483427James Dong    void clearCallbackBuffers_l(JNIEnv *env, Vector<jbyteArray> *buffers);
9094927dffce1626898b59579dfc5af53b5de8cef6Andrew Harp    void clearCallbackBuffers_l(JNIEnv *env);
91e00cab707dcaf6f05adb5ccb9c80fdf25c483427James Dong    jbyteArray getCallbackBuffer(JNIEnv *env, Vector<jbyteArray> *buffers, size_t bufferSize);
925e27115995c4506dddf4f1271effd4fb4c935ad3Dave Sparks
939066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    jobject     mCameraJObjectWeak;     // weak reference to java object
949066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    jclass      mCameraJClass;          // strong reference to java class
95ffe1cf251a4f8469695b8acfa37270684dc1b70cWu-cheng Li    sp<Camera>  mCamera;                // strong reference to native object
96bb1e275c0e684dd213f124da77110cdd9d6f090cWu-cheng Li    jclass      mFaceClass;  // strong reference to Face class
97bb1e275c0e684dd213f124da77110cdd9d6f090cWu-cheng Li    jclass      mRectClass;  // strong reference to Rect class
980601ab1618e95d455230c4801a40cbc5c25221feIgor Murashkin    jclass      mPointClass;  // strong reference to Point class
995e27115995c4506dddf4f1271effd4fb4c935ad3Dave Sparks    Mutex       mLock;
10094927dffce1626898b59579dfc5af53b5de8cef6Andrew Harp
101e00cab707dcaf6f05adb5ccb9c80fdf25c483427James Dong    /*
102e00cab707dcaf6f05adb5ccb9c80fdf25c483427James Dong     * Global reference application-managed raw image buffer queue.
103e00cab707dcaf6f05adb5ccb9c80fdf25c483427James Dong     *
104e00cab707dcaf6f05adb5ccb9c80fdf25c483427James Dong     * Manual-only mode is supported for raw image callbacks, which is
105e00cab707dcaf6f05adb5ccb9c80fdf25c483427James Dong     * set whenever method addCallbackBuffer() with msgType =
106e00cab707dcaf6f05adb5ccb9c80fdf25c483427James Dong     * CAMERA_MSG_RAW_IMAGE is called; otherwise, null is returned
107e00cab707dcaf6f05adb5ccb9c80fdf25c483427James Dong     * with raw image callbacks.
108e00cab707dcaf6f05adb5ccb9c80fdf25c483427James Dong     */
109e00cab707dcaf6f05adb5ccb9c80fdf25c483427James Dong    Vector<jbyteArray> mRawImageCallbackBuffers;
110e00cab707dcaf6f05adb5ccb9c80fdf25c483427James Dong
111e00cab707dcaf6f05adb5ccb9c80fdf25c483427James Dong    /*
112e00cab707dcaf6f05adb5ccb9c80fdf25c483427James Dong     * Application-managed preview buffer queue and the flags
113e00cab707dcaf6f05adb5ccb9c80fdf25c483427James Dong     * associated with the usage of the preview buffer callback.
114e00cab707dcaf6f05adb5ccb9c80fdf25c483427James Dong     */
11594927dffce1626898b59579dfc5af53b5de8cef6Andrew Harp    Vector<jbyteArray> mCallbackBuffers; // Global reference application managed byte[]
11694927dffce1626898b59579dfc5af53b5de8cef6Andrew Harp    bool mManualBufferMode;              // Whether to use application managed buffers.
117e00cab707dcaf6f05adb5ccb9c80fdf25c483427James Dong    bool mManualCameraCallbackSet;       // Whether the callback has been set, used to
118e00cab707dcaf6f05adb5ccb9c80fdf25c483427James Dong                                         // reduce unnecessary calls to set the callback.
1199066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project};
1209066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
121e00cab707dcaf6f05adb5ccb9c80fdf25c483427James Dongbool JNICameraContext::isRawImageCallbackBufferAvailable() const
122e00cab707dcaf6f05adb5ccb9c80fdf25c483427James Dong{
123e00cab707dcaf6f05adb5ccb9c80fdf25c483427James Dong    return !mRawImageCallbackBuffers.isEmpty();
124e00cab707dcaf6f05adb5ccb9c80fdf25c483427James Dong}
125e00cab707dcaf6f05adb5ccb9c80fdf25c483427James Dong
1265e27115995c4506dddf4f1271effd4fb4c935ad3Dave Sparkssp<Camera> get_native_camera(JNIEnv *env, jobject thiz, JNICameraContext** pContext)
1279066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project{
1289066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    sp<Camera> camera;
1299066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    Mutex::Autolock _l(sLock);
1304838e338129e09a37b8c8ad5e44f85ec5cd3aea1Ashok Bhat    JNICameraContext* context = reinterpret_cast<JNICameraContext*>(env->GetLongField(thiz, fields.context));
1319066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    if (context != NULL) {
1325e27115995c4506dddf4f1271effd4fb4c935ad3Dave Sparks        camera = context->getCamera();
1339066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
13471f2cf116aab893e224056c38ab146bd1538dd3eSteve Block    ALOGV("get_native_camera: context=%p, camera=%p", context, camera.get());
1359066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    if (camera == 0) {
136f9133347492a662a89f2aeaf977b60a048d2b647Eino-Ville Talvala        jniThrowRuntimeException(env,
137f9133347492a662a89f2aeaf977b60a048d2b647Eino-Ville Talvala                "Camera is being used after Camera.release() was called");
1389066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
1399066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
1409066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    if (pContext != NULL) *pContext = context;
1419066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    return camera;
1429066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project}
1439066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
1445e27115995c4506dddf4f1271effd4fb4c935ad3Dave SparksJNICameraContext::JNICameraContext(JNIEnv* env, jobject weak_this, jclass clazz, const sp<Camera>& camera)
1459066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project{
1465e27115995c4506dddf4f1271effd4fb4c935ad3Dave Sparks    mCameraJObjectWeak = env->NewGlobalRef(weak_this);
1475e27115995c4506dddf4f1271effd4fb4c935ad3Dave Sparks    mCameraJClass = (jclass)env->NewGlobalRef(clazz);
1485e27115995c4506dddf4f1271effd4fb4c935ad3Dave Sparks    mCamera = camera;
14994927dffce1626898b59579dfc5af53b5de8cef6Andrew Harp
150bb1e275c0e684dd213f124da77110cdd9d6f090cWu-cheng Li    jclass faceClazz = env->FindClass("android/hardware/Camera$Face");
151bb1e275c0e684dd213f124da77110cdd9d6f090cWu-cheng Li    mFaceClass = (jclass) env->NewGlobalRef(faceClazz);
152bb1e275c0e684dd213f124da77110cdd9d6f090cWu-cheng Li
153bb1e275c0e684dd213f124da77110cdd9d6f090cWu-cheng Li    jclass rectClazz = env->FindClass("android/graphics/Rect");
154bb1e275c0e684dd213f124da77110cdd9d6f090cWu-cheng Li    mRectClass = (jclass) env->NewGlobalRef(rectClazz);
155bb1e275c0e684dd213f124da77110cdd9d6f090cWu-cheng Li
1560601ab1618e95d455230c4801a40cbc5c25221feIgor Murashkin    jclass pointClazz = env->FindClass("android/graphics/Point");
1570601ab1618e95d455230c4801a40cbc5c25221feIgor Murashkin    mPointClass = (jclass) env->NewGlobalRef(pointClazz);
1580601ab1618e95d455230c4801a40cbc5c25221feIgor Murashkin
15994927dffce1626898b59579dfc5af53b5de8cef6Andrew Harp    mManualBufferMode = false;
16094927dffce1626898b59579dfc5af53b5de8cef6Andrew Harp    mManualCameraCallbackSet = false;
1615e27115995c4506dddf4f1271effd4fb4c935ad3Dave Sparks}
1629066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
1635e27115995c4506dddf4f1271effd4fb4c935ad3Dave Sparksvoid JNICameraContext::release()
1645e27115995c4506dddf4f1271effd4fb4c935ad3Dave Sparks{
16571f2cf116aab893e224056c38ab146bd1538dd3eSteve Block    ALOGV("release");
1665e27115995c4506dddf4f1271effd4fb4c935ad3Dave Sparks    Mutex::Autolock _l(mLock);
1675e27115995c4506dddf4f1271effd4fb4c935ad3Dave Sparks    JNIEnv *env = AndroidRuntime::getJNIEnv();
1689066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
1695e27115995c4506dddf4f1271effd4fb4c935ad3Dave Sparks    if (mCameraJObjectWeak != NULL) {
1705e27115995c4506dddf4f1271effd4fb4c935ad3Dave Sparks        env->DeleteGlobalRef(mCameraJObjectWeak);
1715e27115995c4506dddf4f1271effd4fb4c935ad3Dave Sparks        mCameraJObjectWeak = NULL;
1725e27115995c4506dddf4f1271effd4fb4c935ad3Dave Sparks    }
1735e27115995c4506dddf4f1271effd4fb4c935ad3Dave Sparks    if (mCameraJClass != NULL) {
1745e27115995c4506dddf4f1271effd4fb4c935ad3Dave Sparks        env->DeleteGlobalRef(mCameraJClass);
1755e27115995c4506dddf4f1271effd4fb4c935ad3Dave Sparks        mCameraJClass = NULL;
1765e27115995c4506dddf4f1271effd4fb4c935ad3Dave Sparks    }
177bb1e275c0e684dd213f124da77110cdd9d6f090cWu-cheng Li    if (mFaceClass != NULL) {
178bb1e275c0e684dd213f124da77110cdd9d6f090cWu-cheng Li        env->DeleteGlobalRef(mFaceClass);
179bb1e275c0e684dd213f124da77110cdd9d6f090cWu-cheng Li        mFaceClass = NULL;
180bb1e275c0e684dd213f124da77110cdd9d6f090cWu-cheng Li    }
181bb1e275c0e684dd213f124da77110cdd9d6f090cWu-cheng Li    if (mRectClass != NULL) {
182bb1e275c0e684dd213f124da77110cdd9d6f090cWu-cheng Li        env->DeleteGlobalRef(mRectClass);
183bb1e275c0e684dd213f124da77110cdd9d6f090cWu-cheng Li        mRectClass = NULL;
184bb1e275c0e684dd213f124da77110cdd9d6f090cWu-cheng Li    }
1850601ab1618e95d455230c4801a40cbc5c25221feIgor Murashkin    if (mPointClass != NULL) {
1860601ab1618e95d455230c4801a40cbc5c25221feIgor Murashkin        env->DeleteGlobalRef(mPointClass);
1870601ab1618e95d455230c4801a40cbc5c25221feIgor Murashkin        mPointClass = NULL;
1880601ab1618e95d455230c4801a40cbc5c25221feIgor Murashkin    }
18994927dffce1626898b59579dfc5af53b5de8cef6Andrew Harp    clearCallbackBuffers_l(env);
1905e27115995c4506dddf4f1271effd4fb4c935ad3Dave Sparks    mCamera.clear();
1915e27115995c4506dddf4f1271effd4fb4c935ad3Dave Sparks}
1925e27115995c4506dddf4f1271effd4fb4c935ad3Dave Sparks
1935e27115995c4506dddf4f1271effd4fb4c935ad3Dave Sparksvoid JNICameraContext::notify(int32_t msgType, int32_t ext1, int32_t ext2)
1945e27115995c4506dddf4f1271effd4fb4c935ad3Dave Sparks{
19571f2cf116aab893e224056c38ab146bd1538dd3eSteve Block    ALOGV("notify");
1965e27115995c4506dddf4f1271effd4fb4c935ad3Dave Sparks
1975e27115995c4506dddf4f1271effd4fb4c935ad3Dave Sparks    // VM pointer will be NULL if object is released
1985e27115995c4506dddf4f1271effd4fb4c935ad3Dave Sparks    Mutex::Autolock _l(mLock);
1995e27115995c4506dddf4f1271effd4fb4c935ad3Dave Sparks    if (mCameraJObjectWeak == NULL) {
2008564c8da817a845353d213acd8636b76f567b234Steve Block        ALOGW("callback on dead camera object");
2015e27115995c4506dddf4f1271effd4fb4c935ad3Dave Sparks        return;
2025e27115995c4506dddf4f1271effd4fb4c935ad3Dave Sparks    }
2035e27115995c4506dddf4f1271effd4fb4c935ad3Dave Sparks    JNIEnv *env = AndroidRuntime::getJNIEnv();
204e00cab707dcaf6f05adb5ccb9c80fdf25c483427James Dong
205e00cab707dcaf6f05adb5ccb9c80fdf25c483427James Dong    /*
206e00cab707dcaf6f05adb5ccb9c80fdf25c483427James Dong     * If the notification or msgType is CAMERA_MSG_RAW_IMAGE_NOTIFY, change it
207e00cab707dcaf6f05adb5ccb9c80fdf25c483427James Dong     * to CAMERA_MSG_RAW_IMAGE since CAMERA_MSG_RAW_IMAGE_NOTIFY is not exposed
208e00cab707dcaf6f05adb5ccb9c80fdf25c483427James Dong     * to the Java app.
209e00cab707dcaf6f05adb5ccb9c80fdf25c483427James Dong     */
210e00cab707dcaf6f05adb5ccb9c80fdf25c483427James Dong    if (msgType == CAMERA_MSG_RAW_IMAGE_NOTIFY) {
211e00cab707dcaf6f05adb5ccb9c80fdf25c483427James Dong        msgType = CAMERA_MSG_RAW_IMAGE;
212e00cab707dcaf6f05adb5ccb9c80fdf25c483427James Dong    }
213e00cab707dcaf6f05adb5ccb9c80fdf25c483427James Dong
214c62f9bd13327937aa2d2f20b44215397120634c1Dave Sparks    env->CallStaticVoidMethod(mCameraJClass, fields.post_event,
2156157de0e2d87568d6d5071b11aeb033a7986b279Chih-Chung Chang            mCameraJObjectWeak, msgType, ext1, ext2, NULL);
2165e27115995c4506dddf4f1271effd4fb4c935ad3Dave Sparks}
2175e27115995c4506dddf4f1271effd4fb4c935ad3Dave Sparks
218e00cab707dcaf6f05adb5ccb9c80fdf25c483427James DongjbyteArray JNICameraContext::getCallbackBuffer(
219e00cab707dcaf6f05adb5ccb9c80fdf25c483427James Dong        JNIEnv* env, Vector<jbyteArray>* buffers, size_t bufferSize)
220e00cab707dcaf6f05adb5ccb9c80fdf25c483427James Dong{
221e00cab707dcaf6f05adb5ccb9c80fdf25c483427James Dong    jbyteArray obj = NULL;
222e00cab707dcaf6f05adb5ccb9c80fdf25c483427James Dong
223e00cab707dcaf6f05adb5ccb9c80fdf25c483427James Dong    // Vector access should be protected by lock in postData()
224e00cab707dcaf6f05adb5ccb9c80fdf25c483427James Dong    if (!buffers->isEmpty()) {
22571f2cf116aab893e224056c38ab146bd1538dd3eSteve Block        ALOGV("Using callback buffer from queue of length %d", buffers->size());
226e00cab707dcaf6f05adb5ccb9c80fdf25c483427James Dong        jbyteArray globalBuffer = buffers->itemAt(0);
227e00cab707dcaf6f05adb5ccb9c80fdf25c483427James Dong        buffers->removeAt(0);
228e00cab707dcaf6f05adb5ccb9c80fdf25c483427James Dong
229e00cab707dcaf6f05adb5ccb9c80fdf25c483427James Dong        obj = (jbyteArray)env->NewLocalRef(globalBuffer);
230e00cab707dcaf6f05adb5ccb9c80fdf25c483427James Dong        env->DeleteGlobalRef(globalBuffer);
231e00cab707dcaf6f05adb5ccb9c80fdf25c483427James Dong
232e00cab707dcaf6f05adb5ccb9c80fdf25c483427James Dong        if (obj != NULL) {
233e00cab707dcaf6f05adb5ccb9c80fdf25c483427James Dong            jsize bufferLength = env->GetArrayLength(obj);
234e00cab707dcaf6f05adb5ccb9c80fdf25c483427James Dong            if ((int)bufferLength < (int)bufferSize) {
2353762c311729fe9f3af085c14c5c1fb471d994c03Steve Block                ALOGE("Callback buffer was too small! Expected %d bytes, but got %d bytes!",
236e00cab707dcaf6f05adb5ccb9c80fdf25c483427James Dong                    bufferSize, bufferLength);
237e00cab707dcaf6f05adb5ccb9c80fdf25c483427James Dong                env->DeleteLocalRef(obj);
238e00cab707dcaf6f05adb5ccb9c80fdf25c483427James Dong                return NULL;
239e00cab707dcaf6f05adb5ccb9c80fdf25c483427James Dong            }
240e00cab707dcaf6f05adb5ccb9c80fdf25c483427James Dong        }
241e00cab707dcaf6f05adb5ccb9c80fdf25c483427James Dong    }
242e00cab707dcaf6f05adb5ccb9c80fdf25c483427James Dong
243e00cab707dcaf6f05adb5ccb9c80fdf25c483427James Dong    return obj;
244e00cab707dcaf6f05adb5ccb9c80fdf25c483427James Dong}
245e00cab707dcaf6f05adb5ccb9c80fdf25c483427James Dong
2465e27115995c4506dddf4f1271effd4fb4c935ad3Dave Sparksvoid JNICameraContext::copyAndPost(JNIEnv* env, const sp<IMemory>& dataPtr, int msgType)
2475e27115995c4506dddf4f1271effd4fb4c935ad3Dave Sparks{
2485e27115995c4506dddf4f1271effd4fb4c935ad3Dave Sparks    jbyteArray obj = NULL;
2495e27115995c4506dddf4f1271effd4fb4c935ad3Dave Sparks
2505e27115995c4506dddf4f1271effd4fb4c935ad3Dave Sparks    // allocate Java byte array and copy data
2515e27115995c4506dddf4f1271effd4fb4c935ad3Dave Sparks    if (dataPtr != NULL) {
2525e27115995c4506dddf4f1271effd4fb4c935ad3Dave Sparks        ssize_t offset;
2535e27115995c4506dddf4f1271effd4fb4c935ad3Dave Sparks        size_t size;
2545e27115995c4506dddf4f1271effd4fb4c935ad3Dave Sparks        sp<IMemoryHeap> heap = dataPtr->getMemory(&offset, &size);
2552fbf25b50f8534c78f3b9063d35a0b5970ff8f1eGlenn Kasten        ALOGV("copyAndPost: off=%zd, size=%zu", offset, size);
2565e27115995c4506dddf4f1271effd4fb4c935ad3Dave Sparks        uint8_t *heapBase = (uint8_t*)heap->base();
2575e27115995c4506dddf4f1271effd4fb4c935ad3Dave Sparks
2585e27115995c4506dddf4f1271effd4fb4c935ad3Dave Sparks        if (heapBase != NULL) {
259c4ca420625183a8efa619e06287b47f3d2e72283Dave Sparks            const jbyte* data = reinterpret_cast<const jbyte*>(heapBase + offset);
26094927dffce1626898b59579dfc5af53b5de8cef6Andrew Harp
2615f0ef5fb0c2e65ca3d0aa4fcd2b300d4cd0a2b4bWu-cheng Li            if (msgType == CAMERA_MSG_RAW_IMAGE) {
2625f0ef5fb0c2e65ca3d0aa4fcd2b300d4cd0a2b4bWu-cheng Li                obj = getCallbackBuffer(env, &mRawImageCallbackBuffers, size);
2635f0ef5fb0c2e65ca3d0aa4fcd2b300d4cd0a2b4bWu-cheng Li            } else if (msgType == CAMERA_MSG_PREVIEW_FRAME && mManualBufferMode) {
2645f0ef5fb0c2e65ca3d0aa4fcd2b300d4cd0a2b4bWu-cheng Li                obj = getCallbackBuffer(env, &mCallbackBuffers, size);
2655f0ef5fb0c2e65ca3d0aa4fcd2b300d4cd0a2b4bWu-cheng Li
2665f0ef5fb0c2e65ca3d0aa4fcd2b300d4cd0a2b4bWu-cheng Li                if (mCallbackBuffers.isEmpty()) {
26771f2cf116aab893e224056c38ab146bd1538dd3eSteve Block                    ALOGV("Out of buffers, clearing callback!");
2685f0ef5fb0c2e65ca3d0aa4fcd2b300d4cd0a2b4bWu-cheng Li                    mCamera->setPreviewCallbackFlags(CAMERA_FRAME_CALLBACK_FLAG_NOOP);
2695f0ef5fb0c2e65ca3d0aa4fcd2b300d4cd0a2b4bWu-cheng Li                    mManualCameraCallbackSet = false;
2705f0ef5fb0c2e65ca3d0aa4fcd2b300d4cd0a2b4bWu-cheng Li
2715f0ef5fb0c2e65ca3d0aa4fcd2b300d4cd0a2b4bWu-cheng Li                    if (obj == NULL) {
27294927dffce1626898b59579dfc5af53b5de8cef6Andrew Harp                        return;
27394927dffce1626898b59579dfc5af53b5de8cef6Andrew Harp                    }
27494927dffce1626898b59579dfc5af53b5de8cef6Andrew Harp                }
2755f0ef5fb0c2e65ca3d0aa4fcd2b300d4cd0a2b4bWu-cheng Li            } else {
27671f2cf116aab893e224056c38ab146bd1538dd3eSteve Block                ALOGV("Allocating callback buffer");
2775f0ef5fb0c2e65ca3d0aa4fcd2b300d4cd0a2b4bWu-cheng Li                obj = env->NewByteArray(size);
27894927dffce1626898b59579dfc5af53b5de8cef6Andrew Harp            }
27994927dffce1626898b59579dfc5af53b5de8cef6Andrew Harp
2805e27115995c4506dddf4f1271effd4fb4c935ad3Dave Sparks            if (obj == NULL) {
2813762c311729fe9f3af085c14c5c1fb471d994c03Steve Block                ALOGE("Couldn't allocate byte array for JPEG data");
2825e27115995c4506dddf4f1271effd4fb4c935ad3Dave Sparks                env->ExceptionClear();
2835e27115995c4506dddf4f1271effd4fb4c935ad3Dave Sparks            } else {
284a95f495f75d0516ad48576500f64a3ffea037af8Dave Sparks                env->SetByteArrayRegion(obj, 0, size, data);
2855e27115995c4506dddf4f1271effd4fb4c935ad3Dave Sparks            }
2865e27115995c4506dddf4f1271effd4fb4c935ad3Dave Sparks        } else {
2873762c311729fe9f3af085c14c5c1fb471d994c03Steve Block            ALOGE("image heap is NULL");
2885e27115995c4506dddf4f1271effd4fb4c935ad3Dave Sparks        }
2895e27115995c4506dddf4f1271effd4fb4c935ad3Dave Sparks    }
2909066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
2915e27115995c4506dddf4f1271effd4fb4c935ad3Dave Sparks    // post image data to Java
2925e27115995c4506dddf4f1271effd4fb4c935ad3Dave Sparks    env->CallStaticVoidMethod(mCameraJClass, fields.post_event,
2935e27115995c4506dddf4f1271effd4fb4c935ad3Dave Sparks            mCameraJObjectWeak, msgType, 0, 0, obj);
2945e27115995c4506dddf4f1271effd4fb4c935ad3Dave Sparks    if (obj) {
2955e27115995c4506dddf4f1271effd4fb4c935ad3Dave Sparks        env->DeleteLocalRef(obj);
2965e27115995c4506dddf4f1271effd4fb4c935ad3Dave Sparks    }
2975e27115995c4506dddf4f1271effd4fb4c935ad3Dave Sparks}
2985e27115995c4506dddf4f1271effd4fb4c935ad3Dave Sparks
299bb1e275c0e684dd213f124da77110cdd9d6f090cWu-cheng Livoid JNICameraContext::postData(int32_t msgType, const sp<IMemory>& dataPtr,
300bb1e275c0e684dd213f124da77110cdd9d6f090cWu-cheng Li                                camera_frame_metadata_t *metadata)
3015e27115995c4506dddf4f1271effd4fb4c935ad3Dave Sparks{
3025e27115995c4506dddf4f1271effd4fb4c935ad3Dave Sparks    // VM pointer will be NULL if object is released
3035e27115995c4506dddf4f1271effd4fb4c935ad3Dave Sparks    Mutex::Autolock _l(mLock);
3049066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    JNIEnv *env = AndroidRuntime::getJNIEnv();
305d0cbb1a52d1b2b8b04a122501d696938b295e502Dave Sparks    if (mCameraJObjectWeak == NULL) {
3068564c8da817a845353d213acd8636b76f567b234Steve Block        ALOGW("callback on dead camera object");
307d0cbb1a52d1b2b8b04a122501d696938b295e502Dave Sparks        return;
308d0cbb1a52d1b2b8b04a122501d696938b295e502Dave Sparks    }
3095e27115995c4506dddf4f1271effd4fb4c935ad3Dave Sparks
310bb1e275c0e684dd213f124da77110cdd9d6f090cWu-cheng Li    int32_t dataMsgType = msgType & ~CAMERA_MSG_PREVIEW_METADATA;
311bb1e275c0e684dd213f124da77110cdd9d6f090cWu-cheng Li
3125e27115995c4506dddf4f1271effd4fb4c935ad3Dave Sparks    // return data based on callback type
313bb1e275c0e684dd213f124da77110cdd9d6f090cWu-cheng Li    switch (dataMsgType) {
314e00cab707dcaf6f05adb5ccb9c80fdf25c483427James Dong        case CAMERA_MSG_VIDEO_FRAME:
315e00cab707dcaf6f05adb5ccb9c80fdf25c483427James Dong            // should never happen
316e00cab707dcaf6f05adb5ccb9c80fdf25c483427James Dong            break;
317e00cab707dcaf6f05adb5ccb9c80fdf25c483427James Dong
318e00cab707dcaf6f05adb5ccb9c80fdf25c483427James Dong        // For backward-compatibility purpose, if there is no callback
319e00cab707dcaf6f05adb5ccb9c80fdf25c483427James Dong        // buffer for raw image, the callback returns null.
320e00cab707dcaf6f05adb5ccb9c80fdf25c483427James Dong        case CAMERA_MSG_RAW_IMAGE:
32171f2cf116aab893e224056c38ab146bd1538dd3eSteve Block            ALOGV("rawCallback");
322e00cab707dcaf6f05adb5ccb9c80fdf25c483427James Dong            if (mRawImageCallbackBuffers.isEmpty()) {
323e00cab707dcaf6f05adb5ccb9c80fdf25c483427James Dong                env->CallStaticVoidMethod(mCameraJClass, fields.post_event,
324bb1e275c0e684dd213f124da77110cdd9d6f090cWu-cheng Li                        mCameraJObjectWeak, dataMsgType, 0, 0, NULL);
325e00cab707dcaf6f05adb5ccb9c80fdf25c483427James Dong            } else {
326bb1e275c0e684dd213f124da77110cdd9d6f090cWu-cheng Li                copyAndPost(env, dataPtr, dataMsgType);
327e00cab707dcaf6f05adb5ccb9c80fdf25c483427James Dong            }
328e00cab707dcaf6f05adb5ccb9c80fdf25c483427James Dong            break;
329e00cab707dcaf6f05adb5ccb9c80fdf25c483427James Dong
330bb1e275c0e684dd213f124da77110cdd9d6f090cWu-cheng Li        // There is no data.
331bb1e275c0e684dd213f124da77110cdd9d6f090cWu-cheng Li        case 0:
332bb1e275c0e684dd213f124da77110cdd9d6f090cWu-cheng Li            break;
333bb1e275c0e684dd213f124da77110cdd9d6f090cWu-cheng Li
334e00cab707dcaf6f05adb5ccb9c80fdf25c483427James Dong        default:
33571f2cf116aab893e224056c38ab146bd1538dd3eSteve Block            ALOGV("dataCallback(%d, %p)", dataMsgType, dataPtr.get());
336bb1e275c0e684dd213f124da77110cdd9d6f090cWu-cheng Li            copyAndPost(env, dataPtr, dataMsgType);
337e00cab707dcaf6f05adb5ccb9c80fdf25c483427James Dong            break;
3389066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
339bb1e275c0e684dd213f124da77110cdd9d6f090cWu-cheng Li
340bb1e275c0e684dd213f124da77110cdd9d6f090cWu-cheng Li    // post frame metadata to Java
341bb1e275c0e684dd213f124da77110cdd9d6f090cWu-cheng Li    if (metadata && (msgType & CAMERA_MSG_PREVIEW_METADATA)) {
342bb1e275c0e684dd213f124da77110cdd9d6f090cWu-cheng Li        postMetadata(env, CAMERA_MSG_PREVIEW_METADATA, metadata);
343bb1e275c0e684dd213f124da77110cdd9d6f090cWu-cheng Li    }
3449066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project}
3459066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
34659c1a935295cb30c2ba2f759855e89c174b42a07Dave Sparksvoid JNICameraContext::postDataTimestamp(nsecs_t timestamp, int32_t msgType, const sp<IMemory>& dataPtr)
34759c1a935295cb30c2ba2f759855e89c174b42a07Dave Sparks{
34859c1a935295cb30c2ba2f759855e89c174b42a07Dave Sparks    // TODO: plumb up to Java. For now, just drop the timestamp
349bb1e275c0e684dd213f124da77110cdd9d6f090cWu-cheng Li    postData(msgType, dataPtr, NULL);
350bb1e275c0e684dd213f124da77110cdd9d6f090cWu-cheng Li}
351bb1e275c0e684dd213f124da77110cdd9d6f090cWu-cheng Li
352bb1e275c0e684dd213f124da77110cdd9d6f090cWu-cheng Livoid JNICameraContext::postMetadata(JNIEnv *env, int32_t msgType, camera_frame_metadata_t *metadata)
353bb1e275c0e684dd213f124da77110cdd9d6f090cWu-cheng Li{
354bb1e275c0e684dd213f124da77110cdd9d6f090cWu-cheng Li    jobjectArray obj = NULL;
355bb1e275c0e684dd213f124da77110cdd9d6f090cWu-cheng Li    obj = (jobjectArray) env->NewObjectArray(metadata->number_of_faces,
356bb1e275c0e684dd213f124da77110cdd9d6f090cWu-cheng Li                                             mFaceClass, NULL);
357bb1e275c0e684dd213f124da77110cdd9d6f090cWu-cheng Li    if (obj == NULL) {
3583762c311729fe9f3af085c14c5c1fb471d994c03Steve Block        ALOGE("Couldn't allocate face metadata array");
359bb1e275c0e684dd213f124da77110cdd9d6f090cWu-cheng Li        return;
360bb1e275c0e684dd213f124da77110cdd9d6f090cWu-cheng Li    }
361bb1e275c0e684dd213f124da77110cdd9d6f090cWu-cheng Li
362bb1e275c0e684dd213f124da77110cdd9d6f090cWu-cheng Li    for (int i = 0; i < metadata->number_of_faces; i++) {
363bb1e275c0e684dd213f124da77110cdd9d6f090cWu-cheng Li        jobject face = env->NewObject(mFaceClass, fields.face_constructor);
364bb1e275c0e684dd213f124da77110cdd9d6f090cWu-cheng Li        env->SetObjectArrayElement(obj, i, face);
365bb1e275c0e684dd213f124da77110cdd9d6f090cWu-cheng Li
366bb1e275c0e684dd213f124da77110cdd9d6f090cWu-cheng Li        jobject rect = env->NewObject(mRectClass, fields.rect_constructor);
367bb1e275c0e684dd213f124da77110cdd9d6f090cWu-cheng Li        env->SetIntField(rect, fields.rect_left, metadata->faces[i].rect[0]);
368bb1e275c0e684dd213f124da77110cdd9d6f090cWu-cheng Li        env->SetIntField(rect, fields.rect_top, metadata->faces[i].rect[1]);
369bb1e275c0e684dd213f124da77110cdd9d6f090cWu-cheng Li        env->SetIntField(rect, fields.rect_right, metadata->faces[i].rect[2]);
370bb1e275c0e684dd213f124da77110cdd9d6f090cWu-cheng Li        env->SetIntField(rect, fields.rect_bottom, metadata->faces[i].rect[3]);
371bb1e275c0e684dd213f124da77110cdd9d6f090cWu-cheng Li
372bb1e275c0e684dd213f124da77110cdd9d6f090cWu-cheng Li        env->SetObjectField(face, fields.face_rect, rect);
373bb1e275c0e684dd213f124da77110cdd9d6f090cWu-cheng Li        env->SetIntField(face, fields.face_score, metadata->faces[i].score);
374bb1e275c0e684dd213f124da77110cdd9d6f090cWu-cheng Li
3750601ab1618e95d455230c4801a40cbc5c25221feIgor Murashkin        bool optionalFields = metadata->faces[i].id != 0
3760601ab1618e95d455230c4801a40cbc5c25221feIgor Murashkin            && metadata->faces[i].left_eye[0] != -2000 && metadata->faces[i].left_eye[1] != -2000
3770601ab1618e95d455230c4801a40cbc5c25221feIgor Murashkin            && metadata->faces[i].right_eye[0] != -2000 && metadata->faces[i].right_eye[1] != -2000
3780601ab1618e95d455230c4801a40cbc5c25221feIgor Murashkin            && metadata->faces[i].mouth[0] != -2000 && metadata->faces[i].mouth[1] != -2000;
3790601ab1618e95d455230c4801a40cbc5c25221feIgor Murashkin        if (optionalFields) {
3800601ab1618e95d455230c4801a40cbc5c25221feIgor Murashkin            int32_t id = metadata->faces[i].id;
3810601ab1618e95d455230c4801a40cbc5c25221feIgor Murashkin            env->SetIntField(face, fields.face_id, id);
3820601ab1618e95d455230c4801a40cbc5c25221feIgor Murashkin
3830601ab1618e95d455230c4801a40cbc5c25221feIgor Murashkin            jobject leftEye = env->NewObject(mPointClass, fields.point_constructor);
3840601ab1618e95d455230c4801a40cbc5c25221feIgor Murashkin            env->SetIntField(leftEye, fields.point_x, metadata->faces[i].left_eye[0]);
3850601ab1618e95d455230c4801a40cbc5c25221feIgor Murashkin            env->SetIntField(leftEye, fields.point_y, metadata->faces[i].left_eye[1]);
3860601ab1618e95d455230c4801a40cbc5c25221feIgor Murashkin            env->SetObjectField(face, fields.face_left_eye, leftEye);
3870601ab1618e95d455230c4801a40cbc5c25221feIgor Murashkin            env->DeleteLocalRef(leftEye);
3880601ab1618e95d455230c4801a40cbc5c25221feIgor Murashkin
3890601ab1618e95d455230c4801a40cbc5c25221feIgor Murashkin            jobject rightEye = env->NewObject(mPointClass, fields.point_constructor);
3900601ab1618e95d455230c4801a40cbc5c25221feIgor Murashkin            env->SetIntField(rightEye, fields.point_x, metadata->faces[i].right_eye[0]);
3910601ab1618e95d455230c4801a40cbc5c25221feIgor Murashkin            env->SetIntField(rightEye, fields.point_y, metadata->faces[i].right_eye[1]);
3920601ab1618e95d455230c4801a40cbc5c25221feIgor Murashkin            env->SetObjectField(face, fields.face_right_eye, rightEye);
3930601ab1618e95d455230c4801a40cbc5c25221feIgor Murashkin            env->DeleteLocalRef(rightEye);
3940601ab1618e95d455230c4801a40cbc5c25221feIgor Murashkin
3950601ab1618e95d455230c4801a40cbc5c25221feIgor Murashkin            jobject mouth = env->NewObject(mPointClass, fields.point_constructor);
3960601ab1618e95d455230c4801a40cbc5c25221feIgor Murashkin            env->SetIntField(mouth, fields.point_x, metadata->faces[i].mouth[0]);
3970601ab1618e95d455230c4801a40cbc5c25221feIgor Murashkin            env->SetIntField(mouth, fields.point_y, metadata->faces[i].mouth[1]);
3980601ab1618e95d455230c4801a40cbc5c25221feIgor Murashkin            env->SetObjectField(face, fields.face_mouth, mouth);
3990601ab1618e95d455230c4801a40cbc5c25221feIgor Murashkin            env->DeleteLocalRef(mouth);
4000601ab1618e95d455230c4801a40cbc5c25221feIgor Murashkin        }
4010601ab1618e95d455230c4801a40cbc5c25221feIgor Murashkin
402bb1e275c0e684dd213f124da77110cdd9d6f090cWu-cheng Li        env->DeleteLocalRef(face);
403bb1e275c0e684dd213f124da77110cdd9d6f090cWu-cheng Li        env->DeleteLocalRef(rect);
404bb1e275c0e684dd213f124da77110cdd9d6f090cWu-cheng Li    }
405bb1e275c0e684dd213f124da77110cdd9d6f090cWu-cheng Li    env->CallStaticVoidMethod(mCameraJClass, fields.post_event,
406bb1e275c0e684dd213f124da77110cdd9d6f090cWu-cheng Li            mCameraJObjectWeak, msgType, 0, 0, obj);
407bb1e275c0e684dd213f124da77110cdd9d6f090cWu-cheng Li    env->DeleteLocalRef(obj);
40859c1a935295cb30c2ba2f759855e89c174b42a07Dave Sparks}
40959c1a935295cb30c2ba2f759855e89c174b42a07Dave Sparks
41094927dffce1626898b59579dfc5af53b5de8cef6Andrew Harpvoid JNICameraContext::setCallbackMode(JNIEnv *env, bool installed, bool manualMode)
41194927dffce1626898b59579dfc5af53b5de8cef6Andrew Harp{
41294927dffce1626898b59579dfc5af53b5de8cef6Andrew Harp    Mutex::Autolock _l(mLock);
41394927dffce1626898b59579dfc5af53b5de8cef6Andrew Harp    mManualBufferMode = manualMode;
41494927dffce1626898b59579dfc5af53b5de8cef6Andrew Harp    mManualCameraCallbackSet = false;
41594927dffce1626898b59579dfc5af53b5de8cef6Andrew Harp
41694927dffce1626898b59579dfc5af53b5de8cef6Andrew Harp    // In order to limit the over usage of binder threads, all non-manual buffer
4179c7ac0d142eaf4b083a309299ab023932a9bb109Iliyan Malchev    // callbacks use CAMERA_FRAME_CALLBACK_FLAG_BARCODE_SCANNER mode now.
41894927dffce1626898b59579dfc5af53b5de8cef6Andrew Harp    //
41994927dffce1626898b59579dfc5af53b5de8cef6Andrew Harp    // Continuous callbacks will have the callback re-registered from handleMessage.
42094927dffce1626898b59579dfc5af53b5de8cef6Andrew Harp    // Manual buffer mode will operate as fast as possible, relying on the finite supply
42194927dffce1626898b59579dfc5af53b5de8cef6Andrew Harp    // of buffers for throttling.
42294927dffce1626898b59579dfc5af53b5de8cef6Andrew Harp
42394927dffce1626898b59579dfc5af53b5de8cef6Andrew Harp    if (!installed) {
4249c7ac0d142eaf4b083a309299ab023932a9bb109Iliyan Malchev        mCamera->setPreviewCallbackFlags(CAMERA_FRAME_CALLBACK_FLAG_NOOP);
425e00cab707dcaf6f05adb5ccb9c80fdf25c483427James Dong        clearCallbackBuffers_l(env, &mCallbackBuffers);
42694927dffce1626898b59579dfc5af53b5de8cef6Andrew Harp    } else if (mManualBufferMode) {
42794927dffce1626898b59579dfc5af53b5de8cef6Andrew Harp        if (!mCallbackBuffers.isEmpty()) {
4289c7ac0d142eaf4b083a309299ab023932a9bb109Iliyan Malchev            mCamera->setPreviewCallbackFlags(CAMERA_FRAME_CALLBACK_FLAG_CAMERA);
42994927dffce1626898b59579dfc5af53b5de8cef6Andrew Harp            mManualCameraCallbackSet = true;
43094927dffce1626898b59579dfc5af53b5de8cef6Andrew Harp        }
43194927dffce1626898b59579dfc5af53b5de8cef6Andrew Harp    } else {
4329c7ac0d142eaf4b083a309299ab023932a9bb109Iliyan Malchev        mCamera->setPreviewCallbackFlags(CAMERA_FRAME_CALLBACK_FLAG_BARCODE_SCANNER);
433e00cab707dcaf6f05adb5ccb9c80fdf25c483427James Dong        clearCallbackBuffers_l(env, &mCallbackBuffers);
43494927dffce1626898b59579dfc5af53b5de8cef6Andrew Harp    }
43594927dffce1626898b59579dfc5af53b5de8cef6Andrew Harp}
43694927dffce1626898b59579dfc5af53b5de8cef6Andrew Harp
437e00cab707dcaf6f05adb5ccb9c80fdf25c483427James Dongvoid JNICameraContext::addCallbackBuffer(
438e00cab707dcaf6f05adb5ccb9c80fdf25c483427James Dong        JNIEnv *env, jbyteArray cbb, int msgType)
43994927dffce1626898b59579dfc5af53b5de8cef6Andrew Harp{
44071f2cf116aab893e224056c38ab146bd1538dd3eSteve Block    ALOGV("addCallbackBuffer: 0x%x", msgType);
44194927dffce1626898b59579dfc5af53b5de8cef6Andrew Harp    if (cbb != NULL) {
44294927dffce1626898b59579dfc5af53b5de8cef6Andrew Harp        Mutex::Autolock _l(mLock);
443e00cab707dcaf6f05adb5ccb9c80fdf25c483427James Dong        switch (msgType) {
444e00cab707dcaf6f05adb5ccb9c80fdf25c483427James Dong            case CAMERA_MSG_PREVIEW_FRAME: {
445e00cab707dcaf6f05adb5ccb9c80fdf25c483427James Dong                jbyteArray callbackBuffer = (jbyteArray)env->NewGlobalRef(cbb);
446e00cab707dcaf6f05adb5ccb9c80fdf25c483427James Dong                mCallbackBuffers.push(callbackBuffer);
447e00cab707dcaf6f05adb5ccb9c80fdf25c483427James Dong
44871f2cf116aab893e224056c38ab146bd1538dd3eSteve Block                ALOGV("Adding callback buffer to queue, %d total",
449e00cab707dcaf6f05adb5ccb9c80fdf25c483427James Dong                        mCallbackBuffers.size());
450e00cab707dcaf6f05adb5ccb9c80fdf25c483427James Dong
451e00cab707dcaf6f05adb5ccb9c80fdf25c483427James Dong                // We want to make sure the camera knows we're ready for the
452e00cab707dcaf6f05adb5ccb9c80fdf25c483427James Dong                // next frame. This may have come unset had we not had a
453e00cab707dcaf6f05adb5ccb9c80fdf25c483427James Dong                // callbackbuffer ready for it last time.
454e00cab707dcaf6f05adb5ccb9c80fdf25c483427James Dong                if (mManualBufferMode && !mManualCameraCallbackSet) {
4559c7ac0d142eaf4b083a309299ab023932a9bb109Iliyan Malchev                    mCamera->setPreviewCallbackFlags(CAMERA_FRAME_CALLBACK_FLAG_CAMERA);
456e00cab707dcaf6f05adb5ccb9c80fdf25c483427James Dong                    mManualCameraCallbackSet = true;
457e00cab707dcaf6f05adb5ccb9c80fdf25c483427James Dong                }
458e00cab707dcaf6f05adb5ccb9c80fdf25c483427James Dong                break;
459e00cab707dcaf6f05adb5ccb9c80fdf25c483427James Dong            }
460e00cab707dcaf6f05adb5ccb9c80fdf25c483427James Dong            case CAMERA_MSG_RAW_IMAGE: {
461e00cab707dcaf6f05adb5ccb9c80fdf25c483427James Dong                jbyteArray callbackBuffer = (jbyteArray)env->NewGlobalRef(cbb);
462e00cab707dcaf6f05adb5ccb9c80fdf25c483427James Dong                mRawImageCallbackBuffers.push(callbackBuffer);
463e00cab707dcaf6f05adb5ccb9c80fdf25c483427James Dong                break;
464e00cab707dcaf6f05adb5ccb9c80fdf25c483427James Dong            }
465e00cab707dcaf6f05adb5ccb9c80fdf25c483427James Dong            default: {
466e00cab707dcaf6f05adb5ccb9c80fdf25c483427James Dong                jniThrowException(env,
467e00cab707dcaf6f05adb5ccb9c80fdf25c483427James Dong                        "java/lang/IllegalArgumentException",
468e00cab707dcaf6f05adb5ccb9c80fdf25c483427James Dong                        "Unsupported message type");
469e00cab707dcaf6f05adb5ccb9c80fdf25c483427James Dong                return;
470e00cab707dcaf6f05adb5ccb9c80fdf25c483427James Dong            }
47194927dffce1626898b59579dfc5af53b5de8cef6Andrew Harp        }
47294927dffce1626898b59579dfc5af53b5de8cef6Andrew Harp    } else {
4733762c311729fe9f3af085c14c5c1fb471d994c03Steve Block       ALOGE("Null byte array!");
47494927dffce1626898b59579dfc5af53b5de8cef6Andrew Harp    }
47594927dffce1626898b59579dfc5af53b5de8cef6Andrew Harp}
47694927dffce1626898b59579dfc5af53b5de8cef6Andrew Harp
47794927dffce1626898b59579dfc5af53b5de8cef6Andrew Harpvoid JNICameraContext::clearCallbackBuffers_l(JNIEnv *env)
47894927dffce1626898b59579dfc5af53b5de8cef6Andrew Harp{
479e00cab707dcaf6f05adb5ccb9c80fdf25c483427James Dong    clearCallbackBuffers_l(env, &mCallbackBuffers);
480e00cab707dcaf6f05adb5ccb9c80fdf25c483427James Dong    clearCallbackBuffers_l(env, &mRawImageCallbackBuffers);
481e00cab707dcaf6f05adb5ccb9c80fdf25c483427James Dong}
482e00cab707dcaf6f05adb5ccb9c80fdf25c483427James Dong
483e00cab707dcaf6f05adb5ccb9c80fdf25c483427James Dongvoid JNICameraContext::clearCallbackBuffers_l(JNIEnv *env, Vector<jbyteArray> *buffers) {
48471f2cf116aab893e224056c38ab146bd1538dd3eSteve Block    ALOGV("Clearing callback buffers, %d remained", buffers->size());
485e00cab707dcaf6f05adb5ccb9c80fdf25c483427James Dong    while (!buffers->isEmpty()) {
486e00cab707dcaf6f05adb5ccb9c80fdf25c483427James Dong        env->DeleteGlobalRef(buffers->top());
487e00cab707dcaf6f05adb5ccb9c80fdf25c483427James Dong        buffers->pop();
48894927dffce1626898b59579dfc5af53b5de8cef6Andrew Harp    }
48994927dffce1626898b59579dfc5af53b5de8cef6Andrew Harp}
49094927dffce1626898b59579dfc5af53b5de8cef6Andrew Harp
491e25cc656392d8866e163f78b60c7791455d0fb44Chih-Chung Changstatic jint android_hardware_Camera_getNumberOfCameras(JNIEnv *env, jobject thiz)
492e25cc656392d8866e163f78b60c7791455d0fb44Chih-Chung Chang{
493e25cc656392d8866e163f78b60c7791455d0fb44Chih-Chung Chang    return Camera::getNumberOfCameras();
494e25cc656392d8866e163f78b60c7791455d0fb44Chih-Chung Chang}
495e25cc656392d8866e163f78b60c7791455d0fb44Chih-Chung Chang
496b8bb78f54b48868465a9d69d65fda08524ab5ae1Chih-Chung Changstatic void android_hardware_Camera_getCameraInfo(JNIEnv *env, jobject thiz,
497b8bb78f54b48868465a9d69d65fda08524ab5ae1Chih-Chung Chang    jint cameraId, jobject info_obj)
498b8bb78f54b48868465a9d69d65fda08524ab5ae1Chih-Chung Chang{
499b8bb78f54b48868465a9d69d65fda08524ab5ae1Chih-Chung Chang    CameraInfo cameraInfo;
500b8bb78f54b48868465a9d69d65fda08524ab5ae1Chih-Chung Chang    status_t rc = Camera::getCameraInfo(cameraId, &cameraInfo);
501b8bb78f54b48868465a9d69d65fda08524ab5ae1Chih-Chung Chang    if (rc != NO_ERROR) {
50269a017bc1d1649350f830dfada5c6ed5eac0b770Elliott Hughes        jniThrowRuntimeException(env, "Fail to get camera info");
503b8bb78f54b48868465a9d69d65fda08524ab5ae1Chih-Chung Chang        return;
504b8bb78f54b48868465a9d69d65fda08524ab5ae1Chih-Chung Chang    }
505b8bb78f54b48868465a9d69d65fda08524ab5ae1Chih-Chung Chang    env->SetIntField(info_obj, fields.facing, cameraInfo.facing);
506b8bb78f54b48868465a9d69d65fda08524ab5ae1Chih-Chung Chang    env->SetIntField(info_obj, fields.orientation, cameraInfo.orientation);
507f7c6c5ad64c8e5d20e1806cc98b9919334c69614Eino-Ville Talvala
508f7c6c5ad64c8e5d20e1806cc98b9919334c69614Eino-Ville Talvala    char value[PROPERTY_VALUE_MAX];
509f7c6c5ad64c8e5d20e1806cc98b9919334c69614Eino-Ville Talvala    property_get("ro.camera.sound.forced", value, "0");
510f7c6c5ad64c8e5d20e1806cc98b9919334c69614Eino-Ville Talvala    jboolean canDisableShutterSound = (strncmp(value, "0", 2) == 0);
511f7c6c5ad64c8e5d20e1806cc98b9919334c69614Eino-Ville Talvala    env->SetBooleanField(info_obj, fields.canDisableShutterSound,
512f7c6c5ad64c8e5d20e1806cc98b9919334c69614Eino-Ville Talvala            canDisableShutterSound);
513b8bb78f54b48868465a9d69d65fda08524ab5ae1Chih-Chung Chang}
514b8bb78f54b48868465a9d69d65fda08524ab5ae1Chih-Chung Chang
5159066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project// connect to camera service
516feb50af361e4305a25758966b6b5df2738c00259Ruben Brunkstatic jint android_hardware_Camera_native_setup(JNIEnv *env, jobject thiz,
5174c913801141163362dd5bb7c9784c83f69c11054Zhijun He    jobject weak_this, jint cameraId, jint halVersion, jstring clientPackageName)
5189066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project{
519788717ca599c714d58b2cb5deea1d37b4a711c07Eino-Ville Talvala    // Convert jstring to String16
520788717ca599c714d58b2cb5deea1d37b4a711c07Eino-Ville Talvala    const char16_t *rawClientName = env->GetStringChars(clientPackageName, NULL);
521788717ca599c714d58b2cb5deea1d37b4a711c07Eino-Ville Talvala    jsize rawClientNameLen = env->GetStringLength(clientPackageName);
522788717ca599c714d58b2cb5deea1d37b4a711c07Eino-Ville Talvala    String16 clientName(rawClientName, rawClientNameLen);
523788717ca599c714d58b2cb5deea1d37b4a711c07Eino-Ville Talvala    env->ReleaseStringChars(clientPackageName, rawClientName);
524788717ca599c714d58b2cb5deea1d37b4a711c07Eino-Ville Talvala
5254c913801141163362dd5bb7c9784c83f69c11054Zhijun He    sp<Camera> camera;
526a1d662716b3da384dfe3a758f079e0cbd089784aIgor Murashkin    if (halVersion == CAMERA_HAL_API_VERSION_NORMAL_CONNECT) {
527a1d662716b3da384dfe3a758f079e0cbd089784aIgor Murashkin        // Default path: hal version is don't care, do normal camera connect.
5284c913801141163362dd5bb7c9784c83f69c11054Zhijun He        camera = Camera::connect(cameraId, clientName,
5294c913801141163362dd5bb7c9784c83f69c11054Zhijun He                Camera::USE_CALLING_UID);
5304c913801141163362dd5bb7c9784c83f69c11054Zhijun He    } else {
5314c913801141163362dd5bb7c9784c83f69c11054Zhijun He        jint status = Camera::connectLegacy(cameraId, halVersion, clientName,
5324c913801141163362dd5bb7c9784c83f69c11054Zhijun He                Camera::USE_CALLING_UID, camera);
5334c913801141163362dd5bb7c9784c83f69c11054Zhijun He        if (status != NO_ERROR) {
5344c913801141163362dd5bb7c9784c83f69c11054Zhijun He            return status;
5354c913801141163362dd5bb7c9784c83f69c11054Zhijun He        }
5364c913801141163362dd5bb7c9784c83f69c11054Zhijun He    }
5379066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
5389066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    if (camera == NULL) {
539feb50af361e4305a25758966b6b5df2738c00259Ruben Brunk        return -EACCES;
5409066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
5419066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
5429066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    // make sure camera hardware is alive
5439066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    if (camera->getStatus() != NO_ERROR) {
544feb50af361e4305a25758966b6b5df2738c00259Ruben Brunk        return NO_INIT;
5459066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
5469066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
5479066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    jclass clazz = env->GetObjectClass(thiz);
5489066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    if (clazz == NULL) {
549feb50af361e4305a25758966b6b5df2738c00259Ruben Brunk        // This should never happen
55069a017bc1d1649350f830dfada5c6ed5eac0b770Elliott Hughes        jniThrowRuntimeException(env, "Can't find android/hardware/Camera");
551feb50af361e4305a25758966b6b5df2738c00259Ruben Brunk        return INVALID_OPERATION;
5529066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
5539066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
5549066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    // We use a weak reference so the Camera object can be garbage collected.
5559066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    // The reference is only used as a proxy for callbacks.
5565e27115995c4506dddf4f1271effd4fb4c935ad3Dave Sparks    sp<JNICameraContext> context = new JNICameraContext(env, weak_this, clazz, camera);
557b1d90c8f60f71422196c337f1d078b68867f5710Mathias Agopian    context->incStrong((void*)android_hardware_Camera_native_setup);
5585e27115995c4506dddf4f1271effd4fb4c935ad3Dave Sparks    camera->setListener(context);
5599066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
5609066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    // save context in opaque field
5614838e338129e09a37b8c8ad5e44f85ec5cd3aea1Ashok Bhat    env->SetLongField(thiz, fields.context, (jlong)context.get());
562feb50af361e4305a25758966b6b5df2738c00259Ruben Brunk    return NO_ERROR;
5639066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project}
5649066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
5659066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project// disconnect from camera service
5669066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project// It's okay to call this when the native camera context is already null.
5679066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project// This handles the case where the user has called release() and the
5689066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project// finalizer is invoked later.
5699066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectstatic void android_hardware_Camera_release(JNIEnv *env, jobject thiz)
5709066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project{
57171f2cf116aab893e224056c38ab146bd1538dd3eSteve Block    ALOGV("release camera");
5725e27115995c4506dddf4f1271effd4fb4c935ad3Dave Sparks    JNICameraContext* context = NULL;
5739066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    sp<Camera> camera;
5749066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    {
5759066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        Mutex::Autolock _l(sLock);
5764838e338129e09a37b8c8ad5e44f85ec5cd3aea1Ashok Bhat        context = reinterpret_cast<JNICameraContext*>(env->GetLongField(thiz, fields.context));
5779066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
5789066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        // Make sure we do not attempt to callback on a deleted Java object.
5794838e338129e09a37b8c8ad5e44f85ec5cd3aea1Ashok Bhat        env->SetLongField(thiz, fields.context, 0);
5809066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
5819066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
5829066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    // clean up if release has not been called before
5839066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    if (context != NULL) {
5845e27115995c4506dddf4f1271effd4fb4c935ad3Dave Sparks        camera = context->getCamera();
5855e27115995c4506dddf4f1271effd4fb4c935ad3Dave Sparks        context->release();
58671f2cf116aab893e224056c38ab146bd1538dd3eSteve Block        ALOGV("native_release: context=%p camera=%p", context, camera.get());
5879066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
5889066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        // clear callbacks
5899066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        if (camera != NULL) {
5909c7ac0d142eaf4b083a309299ab023932a9bb109Iliyan Malchev            camera->setPreviewCallbackFlags(CAMERA_FRAME_CALLBACK_FLAG_NOOP);
5919066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            camera->disconnect();
5929066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        }
5939066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
5949066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        // remove context to prevent further Java access
595b1d90c8f60f71422196c337f1d078b68867f5710Mathias Agopian        context->decStrong((void*)android_hardware_Camera_native_setup);
5969066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
5979066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project}
5989066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
599feb50af361e4305a25758966b6b5df2738c00259Ruben Brunkstatic void android_hardware_Camera_setPreviewSurface(JNIEnv *env, jobject thiz, jobject jSurface)
6009066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project{
601feb50af361e4305a25758966b6b5df2738c00259Ruben Brunk    ALOGV("setPreviewSurface");
6029066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    sp<Camera> camera = get_native_camera(env, thiz, NULL);
6039066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    if (camera == 0) return;
6049066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
6054a05f436989a49e06ec762aac9507ad13fcc5e23Mathias Agopian    sp<IGraphicBufferProducer> gbp;
606aa70f2217cd01e4f09e35c3bf450b20682563378Jesse Hall    sp<Surface> surface;
607aa70f2217cd01e4f09e35c3bf450b20682563378Jesse Hall    if (jSurface) {
608aa70f2217cd01e4f09e35c3bf450b20682563378Jesse Hall        surface = android_view_Surface_getSurface(env, jSurface);
6094a05f436989a49e06ec762aac9507ad13fcc5e23Mathias Agopian        if (surface != NULL) {
6104a05f436989a49e06ec762aac9507ad13fcc5e23Mathias Agopian            gbp = surface->getIGraphicBufferProducer();
6114a05f436989a49e06ec762aac9507ad13fcc5e23Mathias Agopian        }
612aa70f2217cd01e4f09e35c3bf450b20682563378Jesse Hall    }
6133866f0d581ceaa165710feeee9f37fe1b0d7067dMathias Agopian
6147b29779d970deb13c41e4a762df9c16544d83a05Eino-Ville Talvala    if (camera->setPreviewTarget(gbp) != NO_ERROR) {
6154a05f436989a49e06ec762aac9507ad13fcc5e23Mathias Agopian        jniThrowException(env, "java/io/IOException", "setPreviewTexture failed");
6169066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
6179066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project}
6189066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
619fd6f39e147dc077139a41b9c2a4039fbaa20224eJamie Gennisstatic void android_hardware_Camera_setPreviewTexture(JNIEnv *env,
620fd6f39e147dc077139a41b9c2a4039fbaa20224eJamie Gennis        jobject thiz, jobject jSurfaceTexture)
621fd6f39e147dc077139a41b9c2a4039fbaa20224eJamie Gennis{
62271f2cf116aab893e224056c38ab146bd1538dd3eSteve Block    ALOGV("setPreviewTexture");
623fd6f39e147dc077139a41b9c2a4039fbaa20224eJamie Gennis    sp<Camera> camera = get_native_camera(env, thiz, NULL);
624fd6f39e147dc077139a41b9c2a4039fbaa20224eJamie Gennis    if (camera == 0) return;
625fd6f39e147dc077139a41b9c2a4039fbaa20224eJamie Gennis
62652a9a10b6b8c7b7a9f97777541841b94d4fd9754Mathias Agopian    sp<IGraphicBufferProducer> producer = NULL;
627fd6f39e147dc077139a41b9c2a4039fbaa20224eJamie Gennis    if (jSurfaceTexture != NULL) {
62852a9a10b6b8c7b7a9f97777541841b94d4fd9754Mathias Agopian        producer = SurfaceTexture_getProducer(env, jSurfaceTexture);
62952a9a10b6b8c7b7a9f97777541841b94d4fd9754Mathias Agopian        if (producer == NULL) {
6302e76c992bfa955cb637417005941e3e93810a99dDaniel Lam            jniThrowException(env, "java/lang/IllegalArgumentException",
6312e76c992bfa955cb637417005941e3e93810a99dDaniel Lam                    "SurfaceTexture already released in setPreviewTexture");
6322e76c992bfa955cb637417005941e3e93810a99dDaniel Lam            return;
6332e76c992bfa955cb637417005941e3e93810a99dDaniel Lam        }
6342e76c992bfa955cb637417005941e3e93810a99dDaniel Lam
635fd6f39e147dc077139a41b9c2a4039fbaa20224eJamie Gennis    }
6362e76c992bfa955cb637417005941e3e93810a99dDaniel Lam
6377b29779d970deb13c41e4a762df9c16544d83a05Eino-Ville Talvala    if (camera->setPreviewTarget(producer) != NO_ERROR) {
638fd6f39e147dc077139a41b9c2a4039fbaa20224eJamie Gennis        jniThrowException(env, "java/io/IOException",
639fd6f39e147dc077139a41b9c2a4039fbaa20224eJamie Gennis                "setPreviewTexture failed");
640fd6f39e147dc077139a41b9c2a4039fbaa20224eJamie Gennis    }
641fd6f39e147dc077139a41b9c2a4039fbaa20224eJamie Gennis}
642fd6f39e147dc077139a41b9c2a4039fbaa20224eJamie Gennis
6437005b67c127d229017840925d2bc1bd84c43aaf5Eino-Ville Talvalastatic void android_hardware_Camera_setPreviewCallbackSurface(JNIEnv *env,
6447005b67c127d229017840925d2bc1bd84c43aaf5Eino-Ville Talvala        jobject thiz, jobject jSurface)
6457005b67c127d229017840925d2bc1bd84c43aaf5Eino-Ville Talvala{
6467005b67c127d229017840925d2bc1bd84c43aaf5Eino-Ville Talvala    ALOGV("setPreviewCallbackSurface");
6477005b67c127d229017840925d2bc1bd84c43aaf5Eino-Ville Talvala    JNICameraContext* context;
6487005b67c127d229017840925d2bc1bd84c43aaf5Eino-Ville Talvala    sp<Camera> camera = get_native_camera(env, thiz, &context);
6497005b67c127d229017840925d2bc1bd84c43aaf5Eino-Ville Talvala    if (camera == 0) return;
6507005b67c127d229017840925d2bc1bd84c43aaf5Eino-Ville Talvala
6517005b67c127d229017840925d2bc1bd84c43aaf5Eino-Ville Talvala    sp<IGraphicBufferProducer> gbp;
6527005b67c127d229017840925d2bc1bd84c43aaf5Eino-Ville Talvala    sp<Surface> surface;
6537005b67c127d229017840925d2bc1bd84c43aaf5Eino-Ville Talvala    if (jSurface) {
6547005b67c127d229017840925d2bc1bd84c43aaf5Eino-Ville Talvala        surface = android_view_Surface_getSurface(env, jSurface);
6557005b67c127d229017840925d2bc1bd84c43aaf5Eino-Ville Talvala        if (surface != NULL) {
6567005b67c127d229017840925d2bc1bd84c43aaf5Eino-Ville Talvala            gbp = surface->getIGraphicBufferProducer();
6577005b67c127d229017840925d2bc1bd84c43aaf5Eino-Ville Talvala        }
6587005b67c127d229017840925d2bc1bd84c43aaf5Eino-Ville Talvala    }
6597005b67c127d229017840925d2bc1bd84c43aaf5Eino-Ville Talvala    // Clear out normal preview callbacks
6607005b67c127d229017840925d2bc1bd84c43aaf5Eino-Ville Talvala    context->setCallbackMode(env, false, false);
6617005b67c127d229017840925d2bc1bd84c43aaf5Eino-Ville Talvala    // Then set up callback surface
6627005b67c127d229017840925d2bc1bd84c43aaf5Eino-Ville Talvala    if (camera->setPreviewCallbackTarget(gbp) != NO_ERROR) {
6637005b67c127d229017840925d2bc1bd84c43aaf5Eino-Ville Talvala        jniThrowException(env, "java/io/IOException", "setPreviewCallbackTarget failed");
6647005b67c127d229017840925d2bc1bd84c43aaf5Eino-Ville Talvala    }
6657005b67c127d229017840925d2bc1bd84c43aaf5Eino-Ville Talvala}
6667005b67c127d229017840925d2bc1bd84c43aaf5Eino-Ville Talvala
6679066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectstatic void android_hardware_Camera_startPreview(JNIEnv *env, jobject thiz)
6689066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project{
66971f2cf116aab893e224056c38ab146bd1538dd3eSteve Block    ALOGV("startPreview");
6709066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    sp<Camera> camera = get_native_camera(env, thiz, NULL);
6719066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    if (camera == 0) return;
6729066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
6739066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    if (camera->startPreview() != NO_ERROR) {
67469a017bc1d1649350f830dfada5c6ed5eac0b770Elliott Hughes        jniThrowRuntimeException(env, "startPreview failed");
6759066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        return;
6769066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
6779066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project}
6789066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
6799066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectstatic void android_hardware_Camera_stopPreview(JNIEnv *env, jobject thiz)
6809066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project{
68171f2cf116aab893e224056c38ab146bd1538dd3eSteve Block    ALOGV("stopPreview");
6829066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    sp<Camera> c = get_native_camera(env, thiz, NULL);
6839066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    if (c == 0) return;
6849066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
6859066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    c->stopPreview();
6869066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project}
6879066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
6884838e338129e09a37b8c8ad5e44f85ec5cd3aea1Ashok Bhatstatic jboolean android_hardware_Camera_previewEnabled(JNIEnv *env, jobject thiz)
6899066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project{
69071f2cf116aab893e224056c38ab146bd1538dd3eSteve Block    ALOGV("previewEnabled");
6919066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    sp<Camera> c = get_native_camera(env, thiz, NULL);
6924838e338129e09a37b8c8ad5e44f85ec5cd3aea1Ashok Bhat    if (c == 0) return JNI_FALSE;
6939066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
6944838e338129e09a37b8c8ad5e44f85ec5cd3aea1Ashok Bhat    return c->previewEnabled() ? JNI_TRUE : JNI_FALSE;
6959066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project}
6969066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
69794927dffce1626898b59579dfc5af53b5de8cef6Andrew Harpstatic void android_hardware_Camera_setHasPreviewCallback(JNIEnv *env, jobject thiz, jboolean installed, jboolean manualBuffer)
6989066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project{
69971f2cf116aab893e224056c38ab146bd1538dd3eSteve Block    ALOGV("setHasPreviewCallback: installed:%d, manualBuffer:%d", (int)installed, (int)manualBuffer);
7009066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    // Important: Only install preview_callback if the Java code has called
7019066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    // setPreviewCallback() with a non-null value, otherwise we'd pay to memcpy
7029066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    // each preview frame for nothing.
7035e27115995c4506dddf4f1271effd4fb4c935ad3Dave Sparks    JNICameraContext* context;
7049066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    sp<Camera> camera = get_native_camera(env, thiz, &context);
7059066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    if (camera == 0) return;
7069066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
70794927dffce1626898b59579dfc5af53b5de8cef6Andrew Harp    // setCallbackMode will take care of setting the context flags and calling
70894927dffce1626898b59579dfc5af53b5de8cef6Andrew Harp    // camera->setPreviewCallbackFlags within a mutex for us.
70994927dffce1626898b59579dfc5af53b5de8cef6Andrew Harp    context->setCallbackMode(env, installed, manualBuffer);
71094927dffce1626898b59579dfc5af53b5de8cef6Andrew Harp}
71194927dffce1626898b59579dfc5af53b5de8cef6Andrew Harp
7124838e338129e09a37b8c8ad5e44f85ec5cd3aea1Ashok Bhatstatic void android_hardware_Camera_addCallbackBuffer(JNIEnv *env, jobject thiz, jbyteArray bytes, jint msgType) {
71371f2cf116aab893e224056c38ab146bd1538dd3eSteve Block    ALOGV("addCallbackBuffer: 0x%x", msgType);
71494927dffce1626898b59579dfc5af53b5de8cef6Andrew Harp
7154838e338129e09a37b8c8ad5e44f85ec5cd3aea1Ashok Bhat    JNICameraContext* context = reinterpret_cast<JNICameraContext*>(env->GetLongField(thiz, fields.context));
71694927dffce1626898b59579dfc5af53b5de8cef6Andrew Harp
71794927dffce1626898b59579dfc5af53b5de8cef6Andrew Harp    if (context != NULL) {
718e00cab707dcaf6f05adb5ccb9c80fdf25c483427James Dong        context->addCallbackBuffer(env, bytes, msgType);
7199066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
7209066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project}
7219066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
7229066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectstatic void android_hardware_Camera_autoFocus(JNIEnv *env, jobject thiz)
7239066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project{
72471f2cf116aab893e224056c38ab146bd1538dd3eSteve Block    ALOGV("autoFocus");
7255e27115995c4506dddf4f1271effd4fb4c935ad3Dave Sparks    JNICameraContext* context;
7269066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    sp<Camera> c = get_native_camera(env, thiz, &context);
7279066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    if (c == 0) return;
7289066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
7299066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    if (c->autoFocus() != NO_ERROR) {
73069a017bc1d1649350f830dfada5c6ed5eac0b770Elliott Hughes        jniThrowRuntimeException(env, "autoFocus failed");
7319066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
7329066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project}
7339066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
734244f8c26365a303d9dd861bd48a29a4b48578da1Chih-Chung Changstatic void android_hardware_Camera_cancelAutoFocus(JNIEnv *env, jobject thiz)
735244f8c26365a303d9dd861bd48a29a4b48578da1Chih-Chung Chang{
73671f2cf116aab893e224056c38ab146bd1538dd3eSteve Block    ALOGV("cancelAutoFocus");
737244f8c26365a303d9dd861bd48a29a4b48578da1Chih-Chung Chang    JNICameraContext* context;
738244f8c26365a303d9dd861bd48a29a4b48578da1Chih-Chung Chang    sp<Camera> c = get_native_camera(env, thiz, &context);
739244f8c26365a303d9dd861bd48a29a4b48578da1Chih-Chung Chang    if (c == 0) return;
740244f8c26365a303d9dd861bd48a29a4b48578da1Chih-Chung Chang
741244f8c26365a303d9dd861bd48a29a4b48578da1Chih-Chung Chang    if (c->cancelAutoFocus() != NO_ERROR) {
74269a017bc1d1649350f830dfada5c6ed5eac0b770Elliott Hughes        jniThrowRuntimeException(env, "cancelAutoFocus failed");
743244f8c26365a303d9dd861bd48a29a4b48578da1Chih-Chung Chang    }
744244f8c26365a303d9dd861bd48a29a4b48578da1Chih-Chung Chang}
745244f8c26365a303d9dd861bd48a29a4b48578da1Chih-Chung Chang
7464838e338129e09a37b8c8ad5e44f85ec5cd3aea1Ashok Bhatstatic void android_hardware_Camera_takePicture(JNIEnv *env, jobject thiz, jint msgType)
7479066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project{
74871f2cf116aab893e224056c38ab146bd1538dd3eSteve Block    ALOGV("takePicture");
7495e27115995c4506dddf4f1271effd4fb4c935ad3Dave Sparks    JNICameraContext* context;
7509066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    sp<Camera> camera = get_native_camera(env, thiz, &context);
7519066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    if (camera == 0) return;
7529066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
753e00cab707dcaf6f05adb5ccb9c80fdf25c483427James Dong    /*
754e00cab707dcaf6f05adb5ccb9c80fdf25c483427James Dong     * When CAMERA_MSG_RAW_IMAGE is requested, if the raw image callback
755e00cab707dcaf6f05adb5ccb9c80fdf25c483427James Dong     * buffer is available, CAMERA_MSG_RAW_IMAGE is enabled to get the
756e00cab707dcaf6f05adb5ccb9c80fdf25c483427James Dong     * notification _and_ the data; otherwise, CAMERA_MSG_RAW_IMAGE_NOTIFY
757e00cab707dcaf6f05adb5ccb9c80fdf25c483427James Dong     * is enabled to receive the callback notification but no data.
758e00cab707dcaf6f05adb5ccb9c80fdf25c483427James Dong     *
759e00cab707dcaf6f05adb5ccb9c80fdf25c483427James Dong     * Note that CAMERA_MSG_RAW_IMAGE_NOTIFY is not exposed to the
760e00cab707dcaf6f05adb5ccb9c80fdf25c483427James Dong     * Java application.
761e00cab707dcaf6f05adb5ccb9c80fdf25c483427James Dong     */
762e00cab707dcaf6f05adb5ccb9c80fdf25c483427James Dong    if (msgType & CAMERA_MSG_RAW_IMAGE) {
76371f2cf116aab893e224056c38ab146bd1538dd3eSteve Block        ALOGV("Enable raw image callback buffer");
764e00cab707dcaf6f05adb5ccb9c80fdf25c483427James Dong        if (!context->isRawImageCallbackBufferAvailable()) {
76571f2cf116aab893e224056c38ab146bd1538dd3eSteve Block            ALOGV("Enable raw image notification, since no callback buffer exists");
766e00cab707dcaf6f05adb5ccb9c80fdf25c483427James Dong            msgType &= ~CAMERA_MSG_RAW_IMAGE;
767e00cab707dcaf6f05adb5ccb9c80fdf25c483427James Dong            msgType |= CAMERA_MSG_RAW_IMAGE_NOTIFY;
768e00cab707dcaf6f05adb5ccb9c80fdf25c483427James Dong        }
769e00cab707dcaf6f05adb5ccb9c80fdf25c483427James Dong    }
770e00cab707dcaf6f05adb5ccb9c80fdf25c483427James Dong
771e00cab707dcaf6f05adb5ccb9c80fdf25c483427James Dong    if (camera->takePicture(msgType) != NO_ERROR) {
77269a017bc1d1649350f830dfada5c6ed5eac0b770Elliott Hughes        jniThrowRuntimeException(env, "takePicture failed");
7739066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        return;
7749066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
7759066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project}
7769066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
7779066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectstatic void android_hardware_Camera_setParameters(JNIEnv *env, jobject thiz, jstring params)
7789066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project{
77971f2cf116aab893e224056c38ab146bd1538dd3eSteve Block    ALOGV("setParameters");
7809066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    sp<Camera> camera = get_native_camera(env, thiz, NULL);
7819066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    if (camera == 0) return;
7829066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
7839066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    const jchar* str = env->GetStringCritical(params, 0);
7849066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    String8 params8;
7859066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    if (params) {
7869066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        params8 = String8(str, env->GetStringLength(params));
7879066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        env->ReleaseStringCritical(params, str);
7889066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
7899066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    if (camera->setParameters(params8) != NO_ERROR) {
79069a017bc1d1649350f830dfada5c6ed5eac0b770Elliott Hughes        jniThrowRuntimeException(env, "setParameters failed");
7919066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        return;
7929066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
7939066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project}
7949066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
7959066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectstatic jstring android_hardware_Camera_getParameters(JNIEnv *env, jobject thiz)
7969066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project{
79771f2cf116aab893e224056c38ab146bd1538dd3eSteve Block    ALOGV("getParameters");
7989066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    sp<Camera> camera = get_native_camera(env, thiz, NULL);
7999066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    if (camera == 0) return 0;
8009066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
801a1c41e13b521cdd611a8fc46e43b5ae0a2c00238Wu-cheng Li    String8 params8 = camera->getParameters();
802a1c41e13b521cdd611a8fc46e43b5ae0a2c00238Wu-cheng Li    if (params8.isEmpty()) {
803a1c41e13b521cdd611a8fc46e43b5ae0a2c00238Wu-cheng Li        jniThrowRuntimeException(env, "getParameters failed (empty parameters)");
804a1c41e13b521cdd611a8fc46e43b5ae0a2c00238Wu-cheng Li        return 0;
805a1c41e13b521cdd611a8fc46e43b5ae0a2c00238Wu-cheng Li    }
806a1c41e13b521cdd611a8fc46e43b5ae0a2c00238Wu-cheng Li    return env->NewStringUTF(params8.string());
8079066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project}
8089066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
8099066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectstatic void android_hardware_Camera_reconnect(JNIEnv *env, jobject thiz)
8109066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project{
81171f2cf116aab893e224056c38ab146bd1538dd3eSteve Block    ALOGV("reconnect");
8129066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    sp<Camera> camera = get_native_camera(env, thiz, NULL);
8139066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    if (camera == 0) return;
8149066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
8159066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    if (camera->reconnect() != NO_ERROR) {
8169066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        jniThrowException(env, "java/io/IOException", "reconnect failed");
8179066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        return;
8189066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
8199066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project}
8209066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
821ffe1cf251a4f8469695b8acfa37270684dc1b70cWu-cheng Listatic void android_hardware_Camera_lock(JNIEnv *env, jobject thiz)
8229066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project{
82371f2cf116aab893e224056c38ab146bd1538dd3eSteve Block    ALOGV("lock");
8249066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    sp<Camera> camera = get_native_camera(env, thiz, NULL);
825ffe1cf251a4f8469695b8acfa37270684dc1b70cWu-cheng Li    if (camera == 0) return;
826ffe1cf251a4f8469695b8acfa37270684dc1b70cWu-cheng Li
827ffe1cf251a4f8469695b8acfa37270684dc1b70cWu-cheng Li    if (camera->lock() != NO_ERROR) {
82869a017bc1d1649350f830dfada5c6ed5eac0b770Elliott Hughes        jniThrowRuntimeException(env, "lock failed");
829ffe1cf251a4f8469695b8acfa37270684dc1b70cWu-cheng Li    }
8309066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project}
8319066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
832ffe1cf251a4f8469695b8acfa37270684dc1b70cWu-cheng Listatic void android_hardware_Camera_unlock(JNIEnv *env, jobject thiz)
8339066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project{
83471f2cf116aab893e224056c38ab146bd1538dd3eSteve Block    ALOGV("unlock");
8359066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    sp<Camera> camera = get_native_camera(env, thiz, NULL);
836ffe1cf251a4f8469695b8acfa37270684dc1b70cWu-cheng Li    if (camera == 0) return;
837ffe1cf251a4f8469695b8acfa37270684dc1b70cWu-cheng Li
838ffe1cf251a4f8469695b8acfa37270684dc1b70cWu-cheng Li    if (camera->unlock() != NO_ERROR) {
83969a017bc1d1649350f830dfada5c6ed5eac0b770Elliott Hughes        jniThrowRuntimeException(env, "unlock failed");
840ffe1cf251a4f8469695b8acfa37270684dc1b70cWu-cheng Li    }
8419066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project}
8429066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
84336f68b8f24df906c969581b0b8e1a47f95dc03cbWu-cheng Listatic void android_hardware_Camera_startSmoothZoom(JNIEnv *env, jobject thiz, jint value)
84436f68b8f24df906c969581b0b8e1a47f95dc03cbWu-cheng Li{
84571f2cf116aab893e224056c38ab146bd1538dd3eSteve Block    ALOGV("startSmoothZoom");
84636f68b8f24df906c969581b0b8e1a47f95dc03cbWu-cheng Li    sp<Camera> camera = get_native_camera(env, thiz, NULL);
84736f68b8f24df906c969581b0b8e1a47f95dc03cbWu-cheng Li    if (camera == 0) return;
84836f68b8f24df906c969581b0b8e1a47f95dc03cbWu-cheng Li
8490ca25191c663ef229f1f475b17899f2017ed6980Wu-cheng Li    status_t rc = camera->sendCommand(CAMERA_CMD_START_SMOOTH_ZOOM, value, 0);
8500ca25191c663ef229f1f475b17899f2017ed6980Wu-cheng Li    if (rc == BAD_VALUE) {
8510ca25191c663ef229f1f475b17899f2017ed6980Wu-cheng Li        char msg[64];
8520ca25191c663ef229f1f475b17899f2017ed6980Wu-cheng Li        sprintf(msg, "invalid zoom value=%d", value);
8530ca25191c663ef229f1f475b17899f2017ed6980Wu-cheng Li        jniThrowException(env, "java/lang/IllegalArgumentException", msg);
8540ca25191c663ef229f1f475b17899f2017ed6980Wu-cheng Li    } else if (rc != NO_ERROR) {
85569a017bc1d1649350f830dfada5c6ed5eac0b770Elliott Hughes        jniThrowRuntimeException(env, "start smooth zoom failed");
85636f68b8f24df906c969581b0b8e1a47f95dc03cbWu-cheng Li    }
85736f68b8f24df906c969581b0b8e1a47f95dc03cbWu-cheng Li}
85836f68b8f24df906c969581b0b8e1a47f95dc03cbWu-cheng Li
85936f68b8f24df906c969581b0b8e1a47f95dc03cbWu-cheng Listatic void android_hardware_Camera_stopSmoothZoom(JNIEnv *env, jobject thiz)
86036f68b8f24df906c969581b0b8e1a47f95dc03cbWu-cheng Li{
86171f2cf116aab893e224056c38ab146bd1538dd3eSteve Block    ALOGV("stopSmoothZoom");
86236f68b8f24df906c969581b0b8e1a47f95dc03cbWu-cheng Li    sp<Camera> camera = get_native_camera(env, thiz, NULL);
86336f68b8f24df906c969581b0b8e1a47f95dc03cbWu-cheng Li    if (camera == 0) return;
86436f68b8f24df906c969581b0b8e1a47f95dc03cbWu-cheng Li
86536f68b8f24df906c969581b0b8e1a47f95dc03cbWu-cheng Li    if (camera->sendCommand(CAMERA_CMD_STOP_SMOOTH_ZOOM, 0, 0) != NO_ERROR) {
86669a017bc1d1649350f830dfada5c6ed5eac0b770Elliott Hughes        jniThrowRuntimeException(env, "stop smooth zoom failed");
86736f68b8f24df906c969581b0b8e1a47f95dc03cbWu-cheng Li    }
86836f68b8f24df906c969581b0b8e1a47f95dc03cbWu-cheng Li}
86936f68b8f24df906c969581b0b8e1a47f95dc03cbWu-cheng Li
870d1d7706fce19a9a0cf71ff9b65f3aba9b89eeb3bChih-Chung Changstatic void android_hardware_Camera_setDisplayOrientation(JNIEnv *env, jobject thiz,
871d1d7706fce19a9a0cf71ff9b65f3aba9b89eeb3bChih-Chung Chang        jint value)
872d1d7706fce19a9a0cf71ff9b65f3aba9b89eeb3bChih-Chung Chang{
87371f2cf116aab893e224056c38ab146bd1538dd3eSteve Block    ALOGV("setDisplayOrientation");
874d1d7706fce19a9a0cf71ff9b65f3aba9b89eeb3bChih-Chung Chang    sp<Camera> camera = get_native_camera(env, thiz, NULL);
875d1d7706fce19a9a0cf71ff9b65f3aba9b89eeb3bChih-Chung Chang    if (camera == 0) return;
876d1d7706fce19a9a0cf71ff9b65f3aba9b89eeb3bChih-Chung Chang
877d1d7706fce19a9a0cf71ff9b65f3aba9b89eeb3bChih-Chung Chang    if (camera->sendCommand(CAMERA_CMD_SET_DISPLAY_ORIENTATION, value, 0) != NO_ERROR) {
87869a017bc1d1649350f830dfada5c6ed5eac0b770Elliott Hughes        jniThrowRuntimeException(env, "set display orientation failed");
879d1d7706fce19a9a0cf71ff9b65f3aba9b89eeb3bChih-Chung Chang    }
880d1d7706fce19a9a0cf71ff9b65f3aba9b89eeb3bChih-Chung Chang}
881d1d7706fce19a9a0cf71ff9b65f3aba9b89eeb3bChih-Chung Chang
88269fe527bea12f262bfb1a5838f40e3433ad3cb78Eino-Ville Talvalastatic jboolean android_hardware_Camera_enableShutterSound(JNIEnv *env, jobject thiz,
88369fe527bea12f262bfb1a5838f40e3433ad3cb78Eino-Ville Talvala        jboolean enabled)
88469fe527bea12f262bfb1a5838f40e3433ad3cb78Eino-Ville Talvala{
88569fe527bea12f262bfb1a5838f40e3433ad3cb78Eino-Ville Talvala    ALOGV("enableShutterSound");
88669fe527bea12f262bfb1a5838f40e3433ad3cb78Eino-Ville Talvala    sp<Camera> camera = get_native_camera(env, thiz, NULL);
88769fe527bea12f262bfb1a5838f40e3433ad3cb78Eino-Ville Talvala    if (camera == 0) return JNI_FALSE;
88869fe527bea12f262bfb1a5838f40e3433ad3cb78Eino-Ville Talvala
88969fe527bea12f262bfb1a5838f40e3433ad3cb78Eino-Ville Talvala    int32_t value = (enabled == JNI_TRUE) ? 1 : 0;
89069fe527bea12f262bfb1a5838f40e3433ad3cb78Eino-Ville Talvala    status_t rc = camera->sendCommand(CAMERA_CMD_ENABLE_SHUTTER_SOUND, value, 0);
89169fe527bea12f262bfb1a5838f40e3433ad3cb78Eino-Ville Talvala    if (rc == NO_ERROR) {
89269fe527bea12f262bfb1a5838f40e3433ad3cb78Eino-Ville Talvala        return JNI_TRUE;
89369fe527bea12f262bfb1a5838f40e3433ad3cb78Eino-Ville Talvala    } else if (rc == PERMISSION_DENIED) {
89469fe527bea12f262bfb1a5838f40e3433ad3cb78Eino-Ville Talvala        return JNI_FALSE;
89569fe527bea12f262bfb1a5838f40e3433ad3cb78Eino-Ville Talvala    } else {
89669fe527bea12f262bfb1a5838f40e3433ad3cb78Eino-Ville Talvala        jniThrowRuntimeException(env, "enable shutter sound failed");
89769fe527bea12f262bfb1a5838f40e3433ad3cb78Eino-Ville Talvala        return JNI_FALSE;
89869fe527bea12f262bfb1a5838f40e3433ad3cb78Eino-Ville Talvala    }
89969fe527bea12f262bfb1a5838f40e3433ad3cb78Eino-Ville Talvala}
90069fe527bea12f262bfb1a5838f40e3433ad3cb78Eino-Ville Talvala
9014c2292efbbfe7338138a9a0e8eefeaabdef1a0b4Wu-cheng Listatic void android_hardware_Camera_startFaceDetection(JNIEnv *env, jobject thiz,
902bb1e275c0e684dd213f124da77110cdd9d6f090cWu-cheng Li        jint type)
9034c2292efbbfe7338138a9a0e8eefeaabdef1a0b4Wu-cheng Li{
90471f2cf116aab893e224056c38ab146bd1538dd3eSteve Block    ALOGV("startFaceDetection");
9054c2292efbbfe7338138a9a0e8eefeaabdef1a0b4Wu-cheng Li    JNICameraContext* context;
9064c2292efbbfe7338138a9a0e8eefeaabdef1a0b4Wu-cheng Li    sp<Camera> camera = get_native_camera(env, thiz, &context);
9074c2292efbbfe7338138a9a0e8eefeaabdef1a0b4Wu-cheng Li    if (camera == 0) return;
9084c2292efbbfe7338138a9a0e8eefeaabdef1a0b4Wu-cheng Li
9094c2292efbbfe7338138a9a0e8eefeaabdef1a0b4Wu-cheng Li    status_t rc = camera->sendCommand(CAMERA_CMD_START_FACE_DETECTION, type, 0);
9104c2292efbbfe7338138a9a0e8eefeaabdef1a0b4Wu-cheng Li    if (rc == BAD_VALUE) {
9114c2292efbbfe7338138a9a0e8eefeaabdef1a0b4Wu-cheng Li        char msg[64];
9124c2292efbbfe7338138a9a0e8eefeaabdef1a0b4Wu-cheng Li        snprintf(msg, sizeof(msg), "invalid face detection type=%d", type);
9134c2292efbbfe7338138a9a0e8eefeaabdef1a0b4Wu-cheng Li        jniThrowException(env, "java/lang/IllegalArgumentException", msg);
9144c2292efbbfe7338138a9a0e8eefeaabdef1a0b4Wu-cheng Li    } else if (rc != NO_ERROR) {
9154c2292efbbfe7338138a9a0e8eefeaabdef1a0b4Wu-cheng Li        jniThrowRuntimeException(env, "start face detection failed");
9164c2292efbbfe7338138a9a0e8eefeaabdef1a0b4Wu-cheng Li    }
9174c2292efbbfe7338138a9a0e8eefeaabdef1a0b4Wu-cheng Li}
9184c2292efbbfe7338138a9a0e8eefeaabdef1a0b4Wu-cheng Li
9194c2292efbbfe7338138a9a0e8eefeaabdef1a0b4Wu-cheng Listatic void android_hardware_Camera_stopFaceDetection(JNIEnv *env, jobject thiz)
9204c2292efbbfe7338138a9a0e8eefeaabdef1a0b4Wu-cheng Li{
92171f2cf116aab893e224056c38ab146bd1538dd3eSteve Block    ALOGV("stopFaceDetection");
9224c2292efbbfe7338138a9a0e8eefeaabdef1a0b4Wu-cheng Li    sp<Camera> camera = get_native_camera(env, thiz, NULL);
9234c2292efbbfe7338138a9a0e8eefeaabdef1a0b4Wu-cheng Li    if (camera == 0) return;
9244c2292efbbfe7338138a9a0e8eefeaabdef1a0b4Wu-cheng Li
9254c2292efbbfe7338138a9a0e8eefeaabdef1a0b4Wu-cheng Li    if (camera->sendCommand(CAMERA_CMD_STOP_FACE_DETECTION, 0, 0) != NO_ERROR) {
9264c2292efbbfe7338138a9a0e8eefeaabdef1a0b4Wu-cheng Li        jniThrowRuntimeException(env, "stop face detection failed");
9274c2292efbbfe7338138a9a0e8eefeaabdef1a0b4Wu-cheng Li    }
9284c2292efbbfe7338138a9a0e8eefeaabdef1a0b4Wu-cheng Li}
9294c2292efbbfe7338138a9a0e8eefeaabdef1a0b4Wu-cheng Li
9309d062cfe94dcd24b1d61fc83e5de565cb67965b1Wu-cheng Listatic void android_hardware_Camera_enableFocusMoveCallback(JNIEnv *env, jobject thiz, jint enable)
9319d062cfe94dcd24b1d61fc83e5de565cb67965b1Wu-cheng Li{
932020975236c53fe2eeb1ada57f27258802858d98fWu-cheng Li    ALOGV("enableFocusMoveCallback");
9339d062cfe94dcd24b1d61fc83e5de565cb67965b1Wu-cheng Li    sp<Camera> camera = get_native_camera(env, thiz, NULL);
9349d062cfe94dcd24b1d61fc83e5de565cb67965b1Wu-cheng Li    if (camera == 0) return;
9359d062cfe94dcd24b1d61fc83e5de565cb67965b1Wu-cheng Li
9369d062cfe94dcd24b1d61fc83e5de565cb67965b1Wu-cheng Li    if (camera->sendCommand(CAMERA_CMD_ENABLE_FOCUS_MOVE_MSG, enable, 0) != NO_ERROR) {
9379d062cfe94dcd24b1d61fc83e5de565cb67965b1Wu-cheng Li        jniThrowRuntimeException(env, "enable focus move callback failed");
9389d062cfe94dcd24b1d61fc83e5de565cb67965b1Wu-cheng Li    }
9399d062cfe94dcd24b1d61fc83e5de565cb67965b1Wu-cheng Li}
9409d062cfe94dcd24b1d61fc83e5de565cb67965b1Wu-cheng Li
9419066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project//-------------------------------------------------
9429066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
9439066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectstatic JNINativeMethod camMethods[] = {
944e25cc656392d8866e163f78b60c7791455d0fb44Chih-Chung Chang  { "getNumberOfCameras",
945e25cc656392d8866e163f78b60c7791455d0fb44Chih-Chung Chang    "()I",
946e25cc656392d8866e163f78b60c7791455d0fb44Chih-Chung Chang    (void *)android_hardware_Camera_getNumberOfCameras },
9474f8e5ce21df881796d05a1b2117dd8868570a57bEino-Ville Talvala  { "_getCameraInfo",
948b8bb78f54b48868465a9d69d65fda08524ab5ae1Chih-Chung Chang    "(ILandroid/hardware/Camera$CameraInfo;)V",
949b8bb78f54b48868465a9d69d65fda08524ab5ae1Chih-Chung Chang    (void*)android_hardware_Camera_getCameraInfo },
9509066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project  { "native_setup",
9514c913801141163362dd5bb7c9784c83f69c11054Zhijun He    "(Ljava/lang/Object;IILjava/lang/String;)I",
9529066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    (void*)android_hardware_Camera_native_setup },
9539066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project  { "native_release",
9549066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    "()V",
9559066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    (void*)android_hardware_Camera_rele