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