Home | History | Annotate | Download | only in jni
      1 /*
      2 **
      3 ** Copyright 2008, The Android Open Source Project
      4 **
      5 ** Licensed under the Apache License, Version 2.0 (the "License");
      6 ** you may not use this file except in compliance with the License.
      7 ** You may obtain a copy of the License at
      8 **
      9 **     http://www.apache.org/licenses/LICENSE-2.0
     10 **
     11 ** Unless required by applicable law or agreed to in writing, software
     12 ** distributed under the License is distributed on an "AS IS" BASIS,
     13 ** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     14 ** See the License for the specific language governing permissions and
     15 ** limitations under the License.
     16 */
     17 
     18 //#define LOG_NDEBUG 0
     19 #define LOG_TAG "Camera-JNI"
     20 #include <utils/Log.h>
     21 
     22 #include "jni.h"
     23 #include "JNIHelp.h"
     24 #include "android_runtime/AndroidRuntime.h"
     25 #include <android_runtime/android_graphics_SurfaceTexture.h>
     26 #include <android_runtime/android_view_Surface.h>
     27 
     28 #include <cutils/properties.h>
     29 #include <utils/Vector.h>
     30 #include <utils/Errors.h>
     31 
     32 #include <gui/GLConsumer.h>
     33 #include <gui/Surface.h>
     34 #include <camera/Camera.h>
     35 #include <binder/IMemory.h>
     36 
     37 using namespace android;
     38 
     39 enum {
     40     // Keep up to date with Camera.java
     41     CAMERA_HAL_API_VERSION_NORMAL_CONNECT = -2,
     42 };
     43 
     44 struct fields_t {
     45     jfieldID    context;
     46     jfieldID    facing;
     47     jfieldID    orientation;
     48     jfieldID    canDisableShutterSound;
     49     jfieldID    face_rect;
     50     jfieldID    face_score;
     51     jfieldID    face_id;
     52     jfieldID    face_left_eye;
     53     jfieldID    face_right_eye;
     54     jfieldID    face_mouth;
     55     jfieldID    rect_left;
     56     jfieldID    rect_top;
     57     jfieldID    rect_right;
     58     jfieldID    rect_bottom;
     59     jfieldID    point_x;
     60     jfieldID    point_y;
     61     jmethodID   post_event;
     62     jmethodID   rect_constructor;
     63     jmethodID   face_constructor;
     64     jmethodID   point_constructor;
     65 };
     66 
     67 static fields_t fields;
     68 static Mutex sLock;
     69 
     70 // provides persistent context for calls from native code to Java
     71 class JNICameraContext: public CameraListener
     72 {
     73 public:
     74     JNICameraContext(JNIEnv* env, jobject weak_this, jclass clazz, const sp<Camera>& camera);
     75     ~JNICameraContext() { release(); }
     76     virtual void notify(int32_t msgType, int32_t ext1, int32_t ext2);
     77     virtual void postData(int32_t msgType, const sp<IMemory>& dataPtr,
     78                           camera_frame_metadata_t *metadata);
     79     virtual void postDataTimestamp(nsecs_t timestamp, int32_t msgType, const sp<IMemory>& dataPtr);
     80     void postMetadata(JNIEnv *env, int32_t msgType, camera_frame_metadata_t *metadata);
     81     void addCallbackBuffer(JNIEnv *env, jbyteArray cbb, int msgType);
     82     void setCallbackMode(JNIEnv *env, bool installed, bool manualMode);
     83     sp<Camera> getCamera() { Mutex::Autolock _l(mLock); return mCamera; }
     84     bool isRawImageCallbackBufferAvailable() const;
     85     void release();
     86 
     87 private:
     88     void copyAndPost(JNIEnv* env, const sp<IMemory>& dataPtr, int msgType);
     89     void clearCallbackBuffers_l(JNIEnv *env, Vector<jbyteArray> *buffers);
     90     void clearCallbackBuffers_l(JNIEnv *env);
     91     jbyteArray getCallbackBuffer(JNIEnv *env, Vector<jbyteArray> *buffers, size_t bufferSize);
     92 
     93     jobject     mCameraJObjectWeak;     // weak reference to java object
     94     jclass      mCameraJClass;          // strong reference to java class
     95     sp<Camera>  mCamera;                // strong reference to native object
     96     jclass      mFaceClass;  // strong reference to Face class
     97     jclass      mRectClass;  // strong reference to Rect class
     98     jclass      mPointClass;  // strong reference to Point class
     99     Mutex       mLock;
    100 
    101     /*
    102      * Global reference application-managed raw image buffer queue.
    103      *
    104      * Manual-only mode is supported for raw image callbacks, which is
    105      * set whenever method addCallbackBuffer() with msgType =
    106      * CAMERA_MSG_RAW_IMAGE is called; otherwise, null is returned
    107      * with raw image callbacks.
    108      */
    109     Vector<jbyteArray> mRawImageCallbackBuffers;
    110 
    111     /*
    112      * Application-managed preview buffer queue and the flags
    113      * associated with the usage of the preview buffer callback.
    114      */
    115     Vector<jbyteArray> mCallbackBuffers; // Global reference application managed byte[]
    116     bool mManualBufferMode;              // Whether to use application managed buffers.
    117     bool mManualCameraCallbackSet;       // Whether the callback has been set, used to
    118                                          // reduce unnecessary calls to set the callback.
    119 };
    120 
    121 bool JNICameraContext::isRawImageCallbackBufferAvailable() const
    122 {
    123     return !mRawImageCallbackBuffers.isEmpty();
    124 }
    125 
    126 sp<Camera> get_native_camera(JNIEnv *env, jobject thiz, JNICameraContext** pContext)
    127 {
    128     sp<Camera> camera;
    129     Mutex::Autolock _l(sLock);
    130     JNICameraContext* context = reinterpret_cast<JNICameraContext*>(env->GetLongField(thiz, fields.context));
    131     if (context != NULL) {
    132         camera = context->getCamera();
    133     }
    134     ALOGV("get_native_camera: context=%p, camera=%p", context, camera.get());
    135     if (camera == 0) {
    136         jniThrowRuntimeException(env,
    137                 "Camera is being used after Camera.release() was called");
    138     }
    139 
    140     if (pContext != NULL) *pContext = context;
    141     return camera;
    142 }
    143 
    144 JNICameraContext::JNICameraContext(JNIEnv* env, jobject weak_this, jclass clazz, const sp<Camera>& camera)
    145 {
    146     mCameraJObjectWeak = env->NewGlobalRef(weak_this);
    147     mCameraJClass = (jclass)env->NewGlobalRef(clazz);
    148     mCamera = camera;
    149 
    150     jclass faceClazz = env->FindClass("android/hardware/Camera$Face");
    151     mFaceClass = (jclass) env->NewGlobalRef(faceClazz);
    152 
    153     jclass rectClazz = env->FindClass("android/graphics/Rect");
    154     mRectClass = (jclass) env->NewGlobalRef(rectClazz);
    155 
    156     jclass pointClazz = env->FindClass("android/graphics/Point");
    157     mPointClass = (jclass) env->NewGlobalRef(pointClazz);
    158 
    159     mManualBufferMode = false;
    160     mManualCameraCallbackSet = false;
    161 }
    162 
    163 void JNICameraContext::release()
    164 {
    165     ALOGV("release");
    166     Mutex::Autolock _l(mLock);
    167     JNIEnv *env = AndroidRuntime::getJNIEnv();
    168 
    169     if (mCameraJObjectWeak != NULL) {
    170         env->DeleteGlobalRef(mCameraJObjectWeak);
    171         mCameraJObjectWeak = NULL;
    172     }
    173     if (mCameraJClass != NULL) {
    174         env->DeleteGlobalRef(mCameraJClass);
    175         mCameraJClass = NULL;
    176     }
    177     if (mFaceClass != NULL) {
    178         env->DeleteGlobalRef(mFaceClass);
    179         mFaceClass = NULL;
    180     }
    181     if (mRectClass != NULL) {
    182         env->DeleteGlobalRef(mRectClass);
    183         mRectClass = NULL;
    184     }
    185     if (mPointClass != NULL) {
    186         env->DeleteGlobalRef(mPointClass);
    187         mPointClass = NULL;
    188     }
    189     clearCallbackBuffers_l(env);
    190     mCamera.clear();
    191 }
    192 
    193 void JNICameraContext::notify(int32_t msgType, int32_t ext1, int32_t ext2)
    194 {
    195     ALOGV("notify");
    196 
    197     // VM pointer will be NULL if object is released
    198     Mutex::Autolock _l(mLock);
    199     if (mCameraJObjectWeak == NULL) {
    200         ALOGW("callback on dead camera object");
    201         return;
    202     }
    203     JNIEnv *env = AndroidRuntime::getJNIEnv();
    204 
    205     /*
    206      * If the notification or msgType is CAMERA_MSG_RAW_IMAGE_NOTIFY, change it
    207      * to CAMERA_MSG_RAW_IMAGE since CAMERA_MSG_RAW_IMAGE_NOTIFY is not exposed
    208      * to the Java app.
    209      */
    210     if (msgType == CAMERA_MSG_RAW_IMAGE_NOTIFY) {
    211         msgType = CAMERA_MSG_RAW_IMAGE;
    212     }
    213 
    214     env->CallStaticVoidMethod(mCameraJClass, fields.post_event,
    215             mCameraJObjectWeak, msgType, ext1, ext2, NULL);
    216 }
    217 
    218 jbyteArray JNICameraContext::getCallbackBuffer(
    219         JNIEnv* env, Vector<jbyteArray>* buffers, size_t bufferSize)
    220 {
    221     jbyteArray obj = NULL;
    222 
    223     // Vector access should be protected by lock in postData()
    224     if (!buffers->isEmpty()) {
    225         ALOGV("Using callback buffer from queue of length %d", buffers->size());
    226         jbyteArray globalBuffer = buffers->itemAt(0);
    227         buffers->removeAt(0);
    228 
    229         obj = (jbyteArray)env->NewLocalRef(globalBuffer);
    230         env->DeleteGlobalRef(globalBuffer);
    231 
    232         if (obj != NULL) {
    233             jsize bufferLength = env->GetArrayLength(obj);
    234             if ((int)bufferLength < (int)bufferSize) {
    235                 ALOGE("Callback buffer was too small! Expected %d bytes, but got %d bytes!",
    236                     bufferSize, bufferLength);
    237                 env->DeleteLocalRef(obj);
    238                 return NULL;
    239             }
    240         }
    241     }
    242 
    243     return obj;
    244 }
    245 
    246 void JNICameraContext::copyAndPost(JNIEnv* env, const sp<IMemory>& dataPtr, int msgType)
    247 {
    248     jbyteArray obj = NULL;
    249 
    250     // allocate Java byte array and copy data
    251     if (dataPtr != NULL) {
    252         ssize_t offset;
    253         size_t size;
    254         sp<IMemoryHeap> heap = dataPtr->getMemory(&offset, &size);
    255         ALOGV("copyAndPost: off=%zd, size=%zu", offset, size);
    256         uint8_t *heapBase = (uint8_t*)heap->base();
    257 
    258         if (heapBase != NULL) {
    259             const jbyte* data = reinterpret_cast<const jbyte*>(heapBase + offset);
    260 
    261             if (msgType == CAMERA_MSG_RAW_IMAGE) {
    262                 obj = getCallbackBuffer(env, &mRawImageCallbackBuffers, size);
    263             } else if (msgType == CAMERA_MSG_PREVIEW_FRAME && mManualBufferMode) {
    264                 obj = getCallbackBuffer(env, &mCallbackBuffers, size);
    265 
    266                 if (mCallbackBuffers.isEmpty()) {
    267                     ALOGV("Out of buffers, clearing callback!");
    268                     mCamera->setPreviewCallbackFlags(CAMERA_FRAME_CALLBACK_FLAG_NOOP);
    269                     mManualCameraCallbackSet = false;
    270 
    271                     if (obj == NULL) {
    272                         return;
    273                     }
    274                 }
    275             } else {
    276                 ALOGV("Allocating callback buffer");
    277                 obj = env->NewByteArray(size);
    278             }
    279 
    280             if (obj == NULL) {
    281                 ALOGE("Couldn't allocate byte array for JPEG data");
    282                 env->ExceptionClear();
    283             } else {
    284                 env->SetByteArrayRegion(obj, 0, size, data);
    285             }
    286         } else {
    287             ALOGE("image heap is NULL");
    288         }
    289     }
    290 
    291     // post image data to Java
    292     env->CallStaticVoidMethod(mCameraJClass, fields.post_event,
    293             mCameraJObjectWeak, msgType, 0, 0, obj);
    294     if (obj) {
    295         env->DeleteLocalRef(obj);
    296     }
    297 }
    298 
    299 void JNICameraContext::postData(int32_t msgType, const sp<IMemory>& dataPtr,
    300                                 camera_frame_metadata_t *metadata)
    301 {
    302     // VM pointer will be NULL if object is released
    303     Mutex::Autolock _l(mLock);
    304     JNIEnv *env = AndroidRuntime::getJNIEnv();
    305     if (mCameraJObjectWeak == NULL) {
    306         ALOGW("callback on dead camera object");
    307         return;
    308     }
    309 
    310     int32_t dataMsgType = msgType & ~CAMERA_MSG_PREVIEW_METADATA;
    311 
    312     // return data based on callback type
    313     switch (dataMsgType) {
    314         case CAMERA_MSG_VIDEO_FRAME:
    315             // should never happen
    316             break;
    317 
    318         // For backward-compatibility purpose, if there is no callback
    319         // buffer for raw image, the callback returns null.
    320         case CAMERA_MSG_RAW_IMAGE:
    321             ALOGV("rawCallback");
    322             if (mRawImageCallbackBuffers.isEmpty()) {
    323                 env->CallStaticVoidMethod(mCameraJClass, fields.post_event,
    324                         mCameraJObjectWeak, dataMsgType, 0, 0, NULL);
    325             } else {
    326                 copyAndPost(env, dataPtr, dataMsgType);
    327             }
    328             break;
    329 
    330         // There is no data.
    331         case 0:
    332             break;
    333 
    334         default:
    335             ALOGV("dataCallback(%d, %p)", dataMsgType, dataPtr.get());
    336             copyAndPost(env, dataPtr, dataMsgType);
    337             break;
    338     }
    339 
    340     // post frame metadata to Java
    341     if (metadata && (msgType & CAMERA_MSG_PREVIEW_METADATA)) {
    342         postMetadata(env, CAMERA_MSG_PREVIEW_METADATA, metadata);
    343     }
    344 }
    345 
    346 void JNICameraContext::postDataTimestamp(nsecs_t timestamp, int32_t msgType, const sp<IMemory>& dataPtr)
    347 {
    348     // TODO: plumb up to Java. For now, just drop the timestamp
    349     postData(msgType, dataPtr, NULL);
    350 }
    351 
    352 void JNICameraContext::postMetadata(JNIEnv *env, int32_t msgType, camera_frame_metadata_t *metadata)
    353 {
    354     jobjectArray obj = NULL;
    355     obj = (jobjectArray) env->NewObjectArray(metadata->number_of_faces,
    356                                              mFaceClass, NULL);
    357     if (obj == NULL) {
    358         ALOGE("Couldn't allocate face metadata array");
    359         return;
    360     }
    361 
    362     for (int i = 0; i < metadata->number_of_faces; i++) {
    363         jobject face = env->NewObject(mFaceClass, fields.face_constructor);
    364         env->SetObjectArrayElement(obj, i, face);
    365 
    366         jobject rect = env->NewObject(mRectClass, fields.rect_constructor);
    367         env->SetIntField(rect, fields.rect_left, metadata->faces[i].rect[0]);
    368         env->SetIntField(rect, fields.rect_top, metadata->faces[i].rect[1]);
    369         env->SetIntField(rect, fields.rect_right, metadata->faces[i].rect[2]);
    370         env->SetIntField(rect, fields.rect_bottom, metadata->faces[i].rect[3]);
    371 
    372         env->SetObjectField(face, fields.face_rect, rect);
    373         env->SetIntField(face, fields.face_score, metadata->faces[i].score);
    374 
    375         bool optionalFields = metadata->faces[i].id != 0
    376             && metadata->faces[i].left_eye[0] != -2000 && metadata->faces[i].left_eye[1] != -2000
    377             && metadata->faces[i].right_eye[0] != -2000 && metadata->faces[i].right_eye[1] != -2000
    378             && metadata->faces[i].mouth[0] != -2000 && metadata->faces[i].mouth[1] != -2000;
    379         if (optionalFields) {
    380             int32_t id = metadata->faces[i].id;
    381             env->SetIntField(face, fields.face_id, id);
    382 
    383             jobject leftEye = env->NewObject(mPointClass, fields.point_constructor);
    384             env->SetIntField(leftEye, fields.point_x, metadata->faces[i].left_eye[0]);
    385             env->SetIntField(leftEye, fields.point_y, metadata->faces[i].left_eye[1]);
    386             env->SetObjectField(face, fields.face_left_eye, leftEye);
    387             env->DeleteLocalRef(leftEye);
    388 
    389             jobject rightEye = env->NewObject(mPointClass, fields.point_constructor);
    390             env->SetIntField(rightEye, fields.point_x, metadata->faces[i].right_eye[0]);
    391             env->SetIntField(rightEye, fields.point_y, metadata->faces[i].right_eye[1]);
    392             env->SetObjectField(face, fields.face_right_eye, rightEye);
    393             env->DeleteLocalRef(rightEye);
    394 
    395             jobject mouth = env->NewObject(mPointClass, fields.point_constructor);
    396             env->SetIntField(mouth, fields.point_x, metadata->faces[i].mouth[0]);
    397             env->SetIntField(mouth, fields.point_y, metadata->faces[i].mouth[1]);
    398             env->SetObjectField(face, fields.face_mouth, mouth);
    399             env->DeleteLocalRef(mouth);
    400         }
    401 
    402         env->DeleteLocalRef(face);
    403         env->DeleteLocalRef(rect);
    404     }
    405     env->CallStaticVoidMethod(mCameraJClass, fields.post_event,
    406             mCameraJObjectWeak, msgType, 0, 0, obj);
    407     env->DeleteLocalRef(obj);
    408 }
    409 
    410 void JNICameraContext::setCallbackMode(JNIEnv *env, bool installed, bool manualMode)
    411 {
    412     Mutex::Autolock _l(mLock);
    413     mManualBufferMode = manualMode;
    414     mManualCameraCallbackSet = false;
    415 
    416     // In order to limit the over usage of binder threads, all non-manual buffer
    417     // callbacks use CAMERA_FRAME_CALLBACK_FLAG_BARCODE_SCANNER mode now.
    418     //
    419     // Continuous callbacks will have the callback re-registered from handleMessage.
    420     // Manual buffer mode will operate as fast as possible, relying on the finite supply
    421     // of buffers for throttling.
    422 
    423     if (!installed) {
    424         mCamera->setPreviewCallbackFlags(CAMERA_FRAME_CALLBACK_FLAG_NOOP);
    425         clearCallbackBuffers_l(env, &mCallbackBuffers);
    426     } else if (mManualBufferMode) {
    427         if (!mCallbackBuffers.isEmpty()) {
    428             mCamera->setPreviewCallbackFlags(CAMERA_FRAME_CALLBACK_FLAG_CAMERA);
    429             mManualCameraCallbackSet = true;
    430         }
    431     } else {
    432         mCamera->setPreviewCallbackFlags(CAMERA_FRAME_CALLBACK_FLAG_BARCODE_SCANNER);
    433         clearCallbackBuffers_l(env, &mCallbackBuffers);
    434     }
    435 }
    436 
    437 void JNICameraContext::addCallbackBuffer(
    438         JNIEnv *env, jbyteArray cbb, int msgType)
    439 {
    440     ALOGV("addCallbackBuffer: 0x%x", msgType);
    441     if (cbb != NULL) {
    442         Mutex::Autolock _l(mLock);
    443         switch (msgType) {
    444             case CAMERA_MSG_PREVIEW_FRAME: {
    445                 jbyteArray callbackBuffer = (jbyteArray)env->NewGlobalRef(cbb);
    446                 mCallbackBuffers.push(callbackBuffer);
    447 
    448                 ALOGV("Adding callback buffer to queue, %d total",
    449                         mCallbackBuffers.size());
    450 
    451                 // We want to make sure the camera knows we're ready for the
    452                 // next frame. This may have come unset had we not had a
    453                 // callbackbuffer ready for it last time.
    454                 if (mManualBufferMode && !mManualCameraCallbackSet) {
    455                     mCamera->setPreviewCallbackFlags(CAMERA_FRAME_CALLBACK_FLAG_CAMERA);
    456                     mManualCameraCallbackSet = true;
    457                 }
    458                 break;
    459             }
    460             case CAMERA_MSG_RAW_IMAGE: {
    461                 jbyteArray callbackBuffer = (jbyteArray)env->NewGlobalRef(cbb);
    462                 mRawImageCallbackBuffers.push(callbackBuffer);
    463                 break;
    464             }
    465             default: {
    466                 jniThrowException(env,
    467                         "java/lang/IllegalArgumentException",
    468                         "Unsupported message type");
    469                 return;
    470             }
    471         }
    472     } else {
    473        ALOGE("Null byte array!");
    474     }
    475 }
    476 
    477 void JNICameraContext::clearCallbackBuffers_l(JNIEnv *env)
    478 {
    479     clearCallbackBuffers_l(env, &mCallbackBuffers);
    480     clearCallbackBuffers_l(env, &mRawImageCallbackBuffers);
    481 }
    482 
    483 void JNICameraContext::clearCallbackBuffers_l(JNIEnv *env, Vector<jbyteArray> *buffers) {
    484     ALOGV("Clearing callback buffers, %d remained", buffers->size());
    485     while (!buffers->isEmpty()) {
    486         env->DeleteGlobalRef(buffers->top());
    487         buffers->pop();
    488     }
    489 }
    490 
    491 static jint android_hardware_Camera_getNumberOfCameras(JNIEnv *env, jobject thiz)
    492 {
    493     return Camera::getNumberOfCameras();
    494 }
    495 
    496 static void android_hardware_Camera_getCameraInfo(JNIEnv *env, jobject thiz,
    497     jint cameraId, jobject info_obj)
    498 {
    499     CameraInfo cameraInfo;
    500     status_t rc = Camera::getCameraInfo(cameraId, &cameraInfo);
    501     if (rc != NO_ERROR) {
    502         jniThrowRuntimeException(env, "Fail to get camera info");
    503         return;
    504     }
    505     env->SetIntField(info_obj, fields.facing, cameraInfo.facing);
    506     env->SetIntField(info_obj, fields.orientation, cameraInfo.orientation);
    507 
    508     char value[PROPERTY_VALUE_MAX];
    509     property_get("ro.camera.sound.forced", value, "0");
    510     jboolean canDisableShutterSound = (strncmp(value, "0", 2) == 0);
    511     env->SetBooleanField(info_obj, fields.canDisableShutterSound,
    512             canDisableShutterSound);
    513 }
    514 
    515 // connect to camera service
    516 static jint android_hardware_Camera_native_setup(JNIEnv *env, jobject thiz,
    517     jobject weak_this, jint cameraId, jint halVersion, jstring clientPackageName)
    518 {
    519     // Convert jstring to String16
    520     const char16_t *rawClientName = env->GetStringChars(clientPackageName, NULL);
    521     jsize rawClientNameLen = env->GetStringLength(clientPackageName);
    522     String16 clientName(rawClientName, rawClientNameLen);
    523     env->ReleaseStringChars(clientPackageName, rawClientName);
    524 
    525     sp<Camera> camera;
    526     if (halVersion == CAMERA_HAL_API_VERSION_NORMAL_CONNECT) {
    527         // Default path: hal version is don't care, do normal camera connect.
    528         camera = Camera::connect(cameraId, clientName,
    529                 Camera::USE_CALLING_UID);
    530     } else {
    531         jint status = Camera::connectLegacy(cameraId, halVersion, clientName,
    532                 Camera::USE_CALLING_UID, camera);
    533         if (status != NO_ERROR) {
    534             return status;
    535         }
    536     }
    537 
    538     if (camera == NULL) {
    539         return -EACCES;
    540     }
    541 
    542     // make sure camera hardware is alive
    543     if (camera->getStatus() != NO_ERROR) {
    544         return NO_INIT;
    545     }
    546 
    547     jclass clazz = env->GetObjectClass(thiz);
    548     if (clazz == NULL) {
    549         // This should never happen
    550         jniThrowRuntimeException(env, "Can't find android/hardware/Camera");
    551         return INVALID_OPERATION;
    552     }
    553 
    554     // We use a weak reference so the Camera object can be garbage collected.
    555     // The reference is only used as a proxy for callbacks.
    556     sp<JNICameraContext> context = new JNICameraContext(env, weak_this, clazz, camera);
    557     context->incStrong((void*)android_hardware_Camera_native_setup);
    558     camera->setListener(context);
    559 
    560     // save context in opaque field
    561     env->SetLongField(thiz, fields.context, (jlong)context.get());
    562     return NO_ERROR;
    563 }
    564 
    565 // disconnect from camera service
    566 // It's okay to call this when the native camera context is already null.
    567 // This handles the case where the user has called release() and the
    568 // finalizer is invoked later.
    569 static void android_hardware_Camera_release(JNIEnv *env, jobject thiz)
    570 {
    571     ALOGV("release camera");
    572     JNICameraContext* context = NULL;
    573     sp<Camera> camera;
    574     {
    575         Mutex::Autolock _l(sLock);
    576         context = reinterpret_cast<JNICameraContext*>(env->GetLongField(thiz, fields.context));
    577 
    578         // Make sure we do not attempt to callback on a deleted Java object.
    579         env->SetLongField(thiz, fields.context, 0);
    580     }
    581 
    582     // clean up if release has not been called before
    583     if (context != NULL) {
    584         camera = context->getCamera();
    585         context->release();
    586         ALOGV("native_release: context=%p camera=%p", context, camera.get());
    587 
    588         // clear callbacks
    589         if (camera != NULL) {
    590             camera->setPreviewCallbackFlags(CAMERA_FRAME_CALLBACK_FLAG_NOOP);
    591             camera->disconnect();
    592         }
    593 
    594         // remove context to prevent further Java access
    595         context->decStrong((void*)android_hardware_Camera_native_setup);
    596     }
    597 }
    598 
    599 static void android_hardware_Camera_setPreviewSurface(JNIEnv *env, jobject thiz, jobject jSurface)
    600 {
    601     ALOGV("setPreviewSurface");
    602     sp<Camera> camera = get_native_camera(env, thiz, NULL);
    603     if (camera == 0) return;
    604 
    605     sp<IGraphicBufferProducer> gbp;
    606     sp<Surface> surface;
    607     if (jSurface) {
    608         surface = android_view_Surface_getSurface(env, jSurface);
    609         if (surface != NULL) {
    610             gbp = surface->getIGraphicBufferProducer();
    611         }
    612     }
    613 
    614     if (camera->setPreviewTarget(gbp) != NO_ERROR) {
    615         jniThrowException(env, "java/io/IOException", "setPreviewTexture failed");
    616     }
    617 }
    618 
    619 static void android_hardware_Camera_setPreviewTexture(JNIEnv *env,
    620         jobject thiz, jobject jSurfaceTexture)
    621 {
    622     ALOGV("setPreviewTexture");
    623     sp<Camera> camera = get_native_camera(env, thiz, NULL);
    624     if (camera == 0) return;
    625 
    626     sp<IGraphicBufferProducer> producer = NULL;
    627     if (jSurfaceTexture != NULL) {
    628         producer = SurfaceTexture_getProducer(env, jSurfaceTexture);
    629         if (producer == NULL) {
    630             jniThrowException(env, "java/lang/IllegalArgumentException",
    631                     "SurfaceTexture already released in setPreviewTexture");
    632             return;
    633         }
    634 
    635     }
    636 
    637     if (camera->setPreviewTarget(producer) != NO_ERROR) {
    638         jniThrowException(env, "java/io/IOException",
    639                 "setPreviewTexture failed");
    640     }
    641 }
    642 
    643 static void android_hardware_Camera_setPreviewCallbackSurface(JNIEnv *env,
    644         jobject thiz, jobject jSurface)
    645 {
    646     ALOGV("setPreviewCallbackSurface");
    647     JNICameraContext* context;
    648     sp<Camera> camera = get_native_camera(env, thiz, &context);
    649     if (camera == 0) return;
    650 
    651     sp<IGraphicBufferProducer> gbp;
    652     sp<Surface> surface;
    653     if (jSurface) {
    654         surface = android_view_Surface_getSurface(env, jSurface);
    655         if (surface != NULL) {
    656             gbp = surface->getIGraphicBufferProducer();
    657         }
    658     }
    659     // Clear out normal preview callbacks
    660     context->setCallbackMode(env, false, false);
    661     // Then set up callback surface
    662     if (camera->setPreviewCallbackTarget(gbp) != NO_ERROR) {
    663         jniThrowException(env, "java/io/IOException", "setPreviewCallbackTarget failed");
    664     }
    665 }
    666 
    667 static void android_hardware_Camera_startPreview(JNIEnv *env, jobject thiz)
    668 {
    669     ALOGV("startPreview");
    670     sp<Camera> camera = get_native_camera(env, thiz, NULL);
    671     if (camera == 0) return;
    672 
    673     if (camera->startPreview() != NO_ERROR) {
    674         jniThrowRuntimeException(env, "startPreview failed");
    675         return;
    676     }
    677 }
    678 
    679 static void android_hardware_Camera_stopPreview(JNIEnv *env, jobject thiz)
    680 {
    681     ALOGV("stopPreview");
    682     sp<Camera> c = get_native_camera(env, thiz, NULL);
    683     if (c == 0) return;
    684 
    685     c->stopPreview();
    686 }
    687 
    688 static jboolean android_hardware_Camera_previewEnabled(JNIEnv *env, jobject thiz)
    689 {
    690     ALOGV("previewEnabled");
    691     sp<Camera> c = get_native_camera(env, thiz, NULL);
    692     if (c == 0) return JNI_FALSE;
    693 
    694     return c->previewEnabled() ? JNI_TRUE : JNI_FALSE;
    695 }
    696 
    697 static void android_hardware_Camera_setHasPreviewCallback(JNIEnv *env, jobject thiz, jboolean installed, jboolean manualBuffer)
    698 {
    699     ALOGV("setHasPreviewCallback: installed:%d, manualBuffer:%d", (int)installed, (int)manualBuffer);
    700     // Important: Only install preview_callback if the Java code has called
    701     // setPreviewCallback() with a non-null value, otherwise we'd pay to memcpy
    702     // each preview frame for nothing.
    703     JNICameraContext* context;
    704     sp<Camera> camera = get_native_camera(env, thiz, &context);
    705     if (camera == 0) return;
    706 
    707     // setCallbackMode will take care of setting the context flags and calling
    708     // camera->setPreviewCallbackFlags within a mutex for us.
    709     context->setCallbackMode(env, installed, manualBuffer);
    710 }
    711 
    712 static void android_hardware_Camera_addCallbackBuffer(JNIEnv *env, jobject thiz, jbyteArray bytes, jint msgType) {
    713     ALOGV("addCallbackBuffer: 0x%x", msgType);
    714 
    715     JNICameraContext* context = reinterpret_cast<JNICameraContext*>(env->GetLongField(thiz, fields.context));
    716 
    717     if (context != NULL) {
    718         context->addCallbackBuffer(env, bytes, msgType);
    719     }
    720 }
    721 
    722 static void android_hardware_Camera_autoFocus(JNIEnv *env, jobject thiz)
    723 {
    724     ALOGV("autoFocus");
    725     JNICameraContext* context;
    726     sp<Camera> c = get_native_camera(env, thiz, &context);
    727     if (c == 0) return;
    728 
    729     if (c->autoFocus() != NO_ERROR) {
    730         jniThrowRuntimeException(env, "autoFocus failed");
    731     }
    732 }
    733 
    734 static void android_hardware_Camera_cancelAutoFocus(JNIEnv *env, jobject thiz)
    735 {
    736     ALOGV("cancelAutoFocus");
    737     JNICameraContext* context;
    738     sp<Camera> c = get_native_camera(env, thiz, &context);
    739     if (c == 0) return;
    740 
    741     if (c->cancelAutoFocus() != NO_ERROR) {
    742         jniThrowRuntimeException(env, "cancelAutoFocus failed");
    743     }
    744 }
    745 
    746 static void android_hardware_Camera_takePicture(JNIEnv *env, jobject thiz, jint msgType)
    747 {
    748     ALOGV("takePicture");
    749     JNICameraContext* context;
    750     sp<Camera> camera = get_native_camera(env, thiz, &context);
    751     if (camera == 0) return;
    752 
    753     /*
    754      * When CAMERA_MSG_RAW_IMAGE is requested, if the raw image callback
    755      * buffer is available, CAMERA_MSG_RAW_IMAGE is enabled to get the
    756      * notification _and_ the data; otherwise, CAMERA_MSG_RAW_IMAGE_NOTIFY
    757      * is enabled to receive the callback notification but no data.
    758      *
    759      * Note that CAMERA_MSG_RAW_IMAGE_NOTIFY is not exposed to the
    760      * Java application.
    761      */
    762     if (msgType & CAMERA_MSG_RAW_IMAGE) {
    763         ALOGV("Enable raw image callback buffer");
    764         if (!context->isRawImageCallbackBufferAvailable()) {
    765             ALOGV("Enable raw image notification, since no callback buffer exists");
    766             msgType &= ~CAMERA_MSG_RAW_IMAGE;
    767             msgType |= CAMERA_MSG_RAW_IMAGE_NOTIFY;
    768         }
    769     }
    770 
    771     if (camera->takePicture(msgType) != NO_ERROR) {
    772         jniThrowRuntimeException(env, "takePicture failed");
    773         return;
    774     }
    775 }
    776 
    777 static void android_hardware_Camera_setParameters(JNIEnv *env, jobject thiz, jstring params)
    778 {
    779     ALOGV("setParameters");
    780     sp<Camera> camera = get_native_camera(env, thiz, NULL);
    781     if (camera == 0) return;
    782 
    783     const jchar* str = env->GetStringCritical(params, 0);
    784     String8 params8;
    785     if (params) {
    786         params8 = String8(str, env->GetStringLength(params));
    787         env->ReleaseStringCritical(params, str);
    788     }
    789     if (camera->setParameters(params8) != NO_ERROR) {
    790         jniThrowRuntimeException(env, "setParameters failed");
    791         return;
    792     }
    793 }
    794 
    795 static jstring android_hardware_Camera_getParameters(JNIEnv *env, jobject thiz)
    796 {
    797     ALOGV("getParameters");
    798     sp<Camera> camera = get_native_camera(env, thiz, NULL);
    799     if (camera == 0) return 0;
    800 
    801     String8 params8 = camera->getParameters();
    802     if (params8.isEmpty()) {
    803         jniThrowRuntimeException(env, "getParameters failed (empty parameters)");
    804         return 0;
    805     }
    806     return env->NewStringUTF(params8.string());
    807 }
    808 
    809 static void android_hardware_Camera_reconnect(JNIEnv *env, jobject thiz)
    810 {
    811     ALOGV("reconnect");
    812     sp<Camera> camera = get_native_camera(env, thiz, NULL);
    813     if (camera == 0) return;
    814 
    815     if (camera->reconnect() != NO_ERROR) {
    816         jniThrowException(env, "java/io/IOException", "reconnect failed");
    817         return;
    818     }
    819 }
    820 
    821 static void android_hardware_Camera_lock(JNIEnv *env, jobject thiz)
    822 {
    823     ALOGV("lock");
    824     sp<Camera> camera = get_native_camera(env, thiz, NULL);
    825     if (camera == 0) return;
    826 
    827     if (camera->lock() != NO_ERROR) {
    828         jniThrowRuntimeException(env, "lock failed");
    829     }
    830 }
    831 
    832 static void android_hardware_Camera_unlock(JNIEnv *env, jobject thiz)
    833 {
    834     ALOGV("unlock");
    835     sp<Camera> camera = get_native_camera(env, thiz, NULL);
    836     if (camera == 0) return;
    837 
    838     if (camera->unlock() != NO_ERROR) {
    839         jniThrowRuntimeException(env, "unlock failed");
    840     }
    841 }
    842 
    843 static void android_hardware_Camera_startSmoothZoom(JNIEnv *env, jobject thiz, jint value)
    844 {
    845     ALOGV("startSmoothZoom");
    846     sp<Camera> camera = get_native_camera(env, thiz, NULL);
    847     if (camera == 0) return;
    848 
    849     status_t rc = camera->sendCommand(CAMERA_CMD_START_SMOOTH_ZOOM, value, 0);
    850     if (rc == BAD_VALUE) {
    851         char msg[64];
    852         sprintf(msg, "invalid zoom value=%d", value);
    853         jniThrowException(env, "java/lang/IllegalArgumentException", msg);
    854     } else if (rc != NO_ERROR) {
    855         jniThrowRuntimeException(env, "start smooth zoom failed");
    856     }
    857 }
    858 
    859 static void android_hardware_Camera_stopSmoothZoom(JNIEnv *env, jobject thiz)
    860 {
    861     ALOGV("stopSmoothZoom");
    862     sp<Camera> camera = get_native_camera(env, thiz, NULL);
    863     if (camera == 0) return;
    864 
    865     if (camera->sendCommand(CAMERA_CMD_STOP_SMOOTH_ZOOM, 0, 0) != NO_ERROR) {
    866         jniThrowRuntimeException(env, "stop smooth zoom failed");
    867     }
    868 }
    869 
    870 static void android_hardware_Camera_setDisplayOrientation(JNIEnv *env, jobject thiz,
    871         jint value)
    872 {
    873     ALOGV("setDisplayOrientation");
    874     sp<Camera> camera = get_native_camera(env, thiz, NULL);
    875     if (camera == 0) return;
    876 
    877     if (camera->sendCommand(CAMERA_CMD_SET_DISPLAY_ORIENTATION, value, 0) != NO_ERROR) {
    878         jniThrowRuntimeException(env, "set display orientation failed");
    879     }
    880 }
    881 
    882 static jboolean android_hardware_Camera_enableShutterSound(JNIEnv *env, jobject thiz,
    883         jboolean enabled)
    884 {
    885     ALOGV("enableShutterSound");
    886     sp<Camera> camera = get_native_camera(env, thiz, NULL);
    887     if (camera == 0) return JNI_FALSE;
    888 
    889     int32_t value = (enabled == JNI_TRUE) ? 1 : 0;
    890     status_t rc = camera->sendCommand(CAMERA_CMD_ENABLE_SHUTTER_SOUND, value, 0);
    891     if (rc == NO_ERROR) {
    892         return JNI_TRUE;
    893     } else if (rc == PERMISSION_DENIED) {
    894         return JNI_FALSE;
    895     } else {
    896         jniThrowRuntimeException(env, "enable shutter sound failed");
    897         return JNI_FALSE;
    898     }
    899 }
    900 
    901 static void android_hardware_Camera_startFaceDetection(JNIEnv *env, jobject thiz,
    902         jint type)
    903 {
    904     ALOGV("startFaceDetection");
    905     JNICameraContext* context;
    906     sp<Camera> camera = get_native_camera(env, thiz, &context);
    907     if (camera == 0) return;
    908 
    909     status_t rc = camera->sendCommand(CAMERA_CMD_START_FACE_DETECTION, type, 0);
    910     if (rc == BAD_VALUE) {
    911         char msg[64];
    912         snprintf(msg, sizeof(msg), "invalid face detection type=%d", type);
    913         jniThrowException(env, "java/lang/IllegalArgumentException", msg);
    914     } else if (rc != NO_ERROR) {
    915         jniThrowRuntimeException(env, "start face detection failed");
    916     }
    917 }
    918 
    919 static void android_hardware_Camera_stopFaceDetection(JNIEnv *env, jobject thiz)
    920 {
    921     ALOGV("stopFaceDetection");
    922     sp<Camera> camera = get_native_camera(env, thiz, NULL);
    923     if (camera == 0) return;
    924 
    925     if (camera->sendCommand(CAMERA_CMD_STOP_FACE_DETECTION, 0, 0) != NO_ERROR) {
    926         jniThrowRuntimeException(env, "stop face detection failed");
    927     }
    928 }
    929 
    930 static void android_hardware_Camera_enableFocusMoveCallback(JNIEnv *env, jobject thiz, jint enable)
    931 {
    932     ALOGV("enableFocusMoveCallback");
    933     sp<Camera> camera = get_native_camera(env, thiz, NULL);
    934     if (camera == 0) return;
    935 
    936     if (camera->sendCommand(CAMERA_CMD_ENABLE_FOCUS_MOVE_MSG, enable, 0) != NO_ERROR) {
    937         jniThrowRuntimeException(env, "enable focus move callback failed");
    938     }
    939 }
    940 
    941 //-------------------------------------------------
    942 
    943 static JNINativeMethod camMethods[] = {
    944   { "getNumberOfCameras",
    945     "()I",
    946     (void *)android_hardware_Camera_getNumberOfCameras },
    947   { "_getCameraInfo",
    948     "(ILandroid/hardware/Camera$CameraInfo;)V",
    949     (void*)android_hardware_Camera_getCameraInfo },
    950   { "native_setup",
    951     "(Ljava/lang/Object;IILjava/lang/String;)I",
    952     (void*)android_hardware_Camera_native_setup },
    953   { "native_release",
    954     "()V",
    955     (void*)android_hardware_Camera_release },
    956   { "setPreviewSurface",
    957     "(Landroid/view/Surface;)V",
    958     (void *)android_hardware_Camera_setPreviewSurface },
    959   { "setPreviewTexture",
    960     "(Landroid/graphics/SurfaceTexture;)V",
    961     (void *)android_hardware_Camera_setPreviewTexture },
    962   { "setPreviewCallbackSurface",
    963     "(Landroid/view/Surface;)V",
    964     (void *)android_hardware_Camera_setPreviewCallbackSurface },
    965   { "startPreview",
    966     "()V",
    967     (void *)android_hardware_Camera_startPreview },
    968   { "_stopPreview",
    969     "()V",
    970     (void *)android_hardware_Camera_stopPreview },
    971   { "previewEnabled",
    972     "()Z",
    973     (void *)android_hardware_Camera_previewEnabled },
    974   { "setHasPreviewCallback",
    975     "(ZZ)V",
    976     (void *)android_hardware_Camera_setHasPreviewCallback },
    977   { "_addCallbackBuffer",
    978     "([BI)V",
    979     (void *)android_hardware_Camera_addCallbackBuffer },
    980   { "native_autoFocus",
    981     "()V",
    982     (void *)android_hardware_Camera_autoFocus },
    983   { "native_cancelAutoFocus",
    984     "()V",
    985     (void *)android_hardware_Camera_cancelAutoFocus },
    986   { "native_takePicture",
    987     "(I)V",
    988     (void *)android_hardware_Camera_takePicture },
    989   { "native_setParameters",
    990     "(Ljava/lang/String;)V",
    991     (void *)android_hardware_Camera_setParameters },
    992   { "native_getParameters",
    993     "()Ljava/lang/String;",
    994     (void *)android_hardware_Camera_getParameters },
    995   { "reconnect",
    996     "()V",
    997     (void*)android_hardware_Camera_reconnect },
    998   { "lock",
    999     "()V",
   1000     (void*)android_hardware_Camera_lock },
   1001   { "unlock",
   1002     "()V",
   1003     (void*)android_hardware_Camera_unlock },
   1004   { "startSmoothZoom",
   1005     "(I)V",
   1006     (void *)android_hardware_Camera_startSmoothZoom },
   1007   { "stopSmoothZoom",
   1008     "()V",
   1009     (void *)android_hardware_Camera_stopSmoothZoom },
   1010   { "setDisplayOrientation",
   1011     "(I)V",
   1012     (void *)android_hardware_Camera_setDisplayOrientation },
   1013   { "_enableShutterSound",
   1014     "(Z)Z",
   1015     (void *)android_hardware_Camera_enableShutterSound },
   1016   { "_startFaceDetection",
   1017     "(I)V",
   1018     (void *)android_hardware_Camera_startFaceDetection },
   1019   { "_stopFaceDetection",
   1020     "()V",
   1021     (void *)android_hardware_Camera_stopFaceDetection},
   1022   { "enableFocusMoveCallback",
   1023     "(I)V",
   1024     (void *)android_hardware_Camera_enableFocusMoveCallback},
   1025 };
   1026 
   1027 struct field {
   1028     const char *class_name;
   1029     const char *field_name;
   1030     const char *field_type;
   1031     jfieldID   *jfield;
   1032 };
   1033 
   1034 static int find_fields(JNIEnv *env, field *fields, int count)
   1035 {
   1036     for (int i = 0; i < count; i++) {
   1037         field *f = &fields[i];
   1038         jclass clazz = env->FindClass(f->class_name);
   1039         if (clazz == NULL) {
   1040             ALOGE("Can't find %s", f->class_name);
   1041             return -1;
   1042         }
   1043 
   1044         jfieldID field = env->GetFieldID(clazz, f->field_name, f->field_type);
   1045         if (field == NULL) {
   1046             ALOGE("Can't find %s.%s", f->class_name, f->field_name);
   1047             return -1;
   1048         }
   1049 
   1050         *(f->jfield) = field;
   1051     }
   1052 
   1053     return 0;
   1054 }
   1055 
   1056 // Get all the required offsets in java class and register native functions
   1057 int register_android_hardware_Camera(JNIEnv *env)
   1058 {
   1059     field fields_to_find[] = {
   1060         { "android/hardware/Camera", "mNativeContext",   "J", &fields.context },
   1061         { "android/hardware/Camera$CameraInfo", "facing",   "I", &fields.facing },
   1062         { "android/hardware/Camera$CameraInfo", "orientation",   "I", &fields.orientation },
   1063         { "android/hardware/Camera$CameraInfo", "canDisableShutterSound",   "Z",
   1064           &fields.canDisableShutterSound },
   1065         { "android/hardware/Camera$Face", "rect", "Landroid/graphics/Rect;", &fields.face_rect },
   1066         { "android/hardware/Camera$Face", "leftEye", "Landroid/graphics/Point;", &fields.face_left_eye},
   1067         { "android/hardware/Camera$Face", "rightEye", "Landroid/graphics/Point;", &fields.face_right_eye},
   1068         { "android/hardware/Camera$Face", "mouth", "Landroid/graphics/Point;", &fields.face_mouth},
   1069         { "android/hardware/Camera$Face", "score", "I", &fields.face_score },
   1070         { "android/hardware/Camera$Face", "id", "I", &fields.face_id},
   1071         { "android/graphics/Rect", "left", "I", &fields.rect_left },
   1072         { "android/graphics/Rect", "top", "I", &fields.rect_top },
   1073         { "android/graphics/Rect", "right", "I", &fields.rect_right },
   1074         { "android/graphics/Rect", "bottom", "I", &fields.rect_bottom },
   1075         { "android/graphics/Point", "x", "I", &fields.point_x},
   1076         { "android/graphics/Point", "y", "I", &fields.point_y},
   1077     };
   1078 
   1079     if (find_fields(env, fields_to_find, NELEM(fields_to_find)) < 0)
   1080         return -1;
   1081 
   1082     jclass clazz = env->FindClass("android/hardware/Camera");
   1083     fields.post_event = env->GetStaticMethodID(clazz, "postEventFromNative",
   1084                                                "(Ljava/lang/Object;IIILjava/lang/Object;)V");
   1085     if (fields.post_event == NULL) {
   1086         ALOGE("Can't find android/hardware/Camera.postEventFromNative");
   1087         return -1;
   1088     }
   1089 
   1090     clazz = env->FindClass("android/graphics/Rect");
   1091     fields.rect_constructor = env->GetMethodID(clazz, "<init>", "()V");
   1092     if (fields.rect_constructor == NULL) {
   1093         ALOGE("Can't find android/graphics/Rect.Rect()");
   1094         return -1;
   1095     }
   1096 
   1097     clazz = env->FindClass("android/hardware/Camera$Face");
   1098     fields.face_constructor = env->GetMethodID(clazz, "<init>", "()V");
   1099     if (fields.face_constructor == NULL) {
   1100         ALOGE("Can't find android/hardware/Camera$Face.Face()");
   1101         return -1;
   1102     }
   1103 
   1104     clazz = env->FindClass("android/graphics/Point");
   1105     fields.point_constructor = env->GetMethodID(clazz, "<init>", "()V");
   1106     if (fields.point_constructor == NULL) {
   1107         ALOGE("Can't find android/graphics/Point()");
   1108         return -1;
   1109     }
   1110 
   1111     // Register native functions
   1112     return AndroidRuntime::registerNativeMethods(env, "android/hardware/Camera",
   1113                                               camMethods, NELEM(camMethods));
   1114 }
   1115