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