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