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