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