The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 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 <ui/Surface.h> |
| 27 | #include <ui/Camera.h> |
Mathias Agopian | 0795272 | 2009-05-19 19:08:10 -0700 | [diff] [blame] | 28 | #include <binder/IMemory.h> |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 29 | |
| 30 | using namespace android; |
| 31 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 32 | struct fields_t { |
| 33 | jfieldID context; |
| 34 | jfieldID surface; |
| 35 | jmethodID post_event; |
| 36 | }; |
| 37 | |
| 38 | static fields_t fields; |
| 39 | static Mutex sLock; |
| 40 | |
Dave Sparks | 5e27115 | 2009-06-23 17:30:11 -0700 | [diff] [blame] | 41 | // provides persistent context for calls from native code to Java |
| 42 | class JNICameraContext: public CameraListener |
| 43 | { |
| 44 | public: |
| 45 | JNICameraContext(JNIEnv* env, jobject weak_this, jclass clazz, const sp<Camera>& camera); |
| 46 | ~JNICameraContext() { release(); } |
| 47 | virtual void notify(int32_t msgType, int32_t ext1, int32_t ext2); |
| 48 | virtual void postData(int32_t msgType, const sp<IMemory>& dataPtr); |
Dave Sparks | 59c1a93 | 2009-07-08 15:56:53 -0700 | [diff] [blame] | 49 | virtual void postDataTimestamp(nsecs_t timestamp, int32_t msgType, const sp<IMemory>& dataPtr); |
Dave Sparks | 5e27115 | 2009-06-23 17:30:11 -0700 | [diff] [blame] | 50 | sp<Camera> getCamera() { Mutex::Autolock _l(mLock); return mCamera; } |
| 51 | void release(); |
| 52 | |
| 53 | private: |
| 54 | void copyAndPost(JNIEnv* env, const sp<IMemory>& dataPtr, int msgType); |
| 55 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 56 | jobject mCameraJObjectWeak; // weak reference to java object |
| 57 | jclass mCameraJClass; // strong reference to java class |
Wu-cheng Li | ffe1cf2 | 2009-09-10 16:49:17 +0800 | [diff] [blame] | 58 | sp<Camera> mCamera; // strong reference to native object |
Dave Sparks | 5e27115 | 2009-06-23 17:30:11 -0700 | [diff] [blame] | 59 | Mutex mLock; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 60 | }; |
| 61 | |
Dave Sparks | 5e27115 | 2009-06-23 17:30:11 -0700 | [diff] [blame] | 62 | sp<Camera> get_native_camera(JNIEnv *env, jobject thiz, JNICameraContext** pContext) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 63 | { |
| 64 | sp<Camera> camera; |
| 65 | Mutex::Autolock _l(sLock); |
Dave Sparks | 5e27115 | 2009-06-23 17:30:11 -0700 | [diff] [blame] | 66 | JNICameraContext* context = reinterpret_cast<JNICameraContext*>(env->GetIntField(thiz, fields.context)); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 67 | if (context != NULL) { |
Dave Sparks | 5e27115 | 2009-06-23 17:30:11 -0700 | [diff] [blame] | 68 | camera = context->getCamera(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 69 | } |
| 70 | LOGV("get_native_camera: context=%p, camera=%p", context, camera.get()); |
| 71 | if (camera == 0) { |
| 72 | jniThrowException(env, "java/lang/RuntimeException", "Method called after release()"); |
| 73 | } |
| 74 | |
| 75 | if (pContext != NULL) *pContext = context; |
| 76 | return camera; |
| 77 | } |
| 78 | |
Dave Sparks | 5e27115 | 2009-06-23 17:30:11 -0700 | [diff] [blame] | 79 | JNICameraContext::JNICameraContext(JNIEnv* env, jobject weak_this, jclass clazz, const sp<Camera>& camera) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 80 | { |
Dave Sparks | 5e27115 | 2009-06-23 17:30:11 -0700 | [diff] [blame] | 81 | mCameraJObjectWeak = env->NewGlobalRef(weak_this); |
| 82 | mCameraJClass = (jclass)env->NewGlobalRef(clazz); |
| 83 | mCamera = camera; |
| 84 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 85 | |
Dave Sparks | 5e27115 | 2009-06-23 17:30:11 -0700 | [diff] [blame] | 86 | void JNICameraContext::release() |
| 87 | { |
| 88 | LOGV("release"); |
| 89 | Mutex::Autolock _l(mLock); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 90 | JNIEnv *env = AndroidRuntime::getJNIEnv(); |
Dave Sparks | 5e27115 | 2009-06-23 17:30:11 -0700 | [diff] [blame] | 91 | |
| 92 | if (mCameraJObjectWeak != NULL) { |
| 93 | env->DeleteGlobalRef(mCameraJObjectWeak); |
| 94 | mCameraJObjectWeak = NULL; |
| 95 | } |
| 96 | if (mCameraJClass != NULL) { |
| 97 | env->DeleteGlobalRef(mCameraJClass); |
| 98 | mCameraJClass = NULL; |
| 99 | } |
| 100 | mCamera.clear(); |
| 101 | } |
| 102 | |
| 103 | void JNICameraContext::notify(int32_t msgType, int32_t ext1, int32_t ext2) |
| 104 | { |
| 105 | LOGV("notify"); |
| 106 | |
| 107 | // VM pointer will be NULL if object is released |
| 108 | Mutex::Autolock _l(mLock); |
| 109 | if (mCameraJObjectWeak == NULL) { |
| 110 | LOGW("callback on dead camera object"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 111 | return; |
| 112 | } |
Dave Sparks | 5e27115 | 2009-06-23 17:30:11 -0700 | [diff] [blame] | 113 | JNIEnv *env = AndroidRuntime::getJNIEnv(); |
Dave Sparks | c62f9bd | 2009-06-26 13:33:32 -0700 | [diff] [blame] | 114 | env->CallStaticVoidMethod(mCameraJClass, fields.post_event, |
Chih-Chung Chang | 6157de0 | 2009-09-24 15:29:35 -0700 | [diff] [blame^] | 115 | mCameraJObjectWeak, msgType, ext1, ext2, NULL); |
Dave Sparks | 5e27115 | 2009-06-23 17:30:11 -0700 | [diff] [blame] | 116 | } |
| 117 | |
| 118 | void JNICameraContext::copyAndPost(JNIEnv* env, const sp<IMemory>& dataPtr, int msgType) |
| 119 | { |
| 120 | jbyteArray obj = NULL; |
| 121 | |
| 122 | // allocate Java byte array and copy data |
| 123 | if (dataPtr != NULL) { |
| 124 | ssize_t offset; |
| 125 | size_t size; |
| 126 | sp<IMemoryHeap> heap = dataPtr->getMemory(&offset, &size); |
| 127 | LOGV("postData: off=%d, size=%d", offset, size); |
| 128 | uint8_t *heapBase = (uint8_t*)heap->base(); |
| 129 | |
| 130 | if (heapBase != NULL) { |
Dave Sparks | c4ca420 | 2009-07-13 09:38:20 -0700 | [diff] [blame] | 131 | const jbyte* data = reinterpret_cast<const jbyte*>(heapBase + offset); |
Dave Sparks | 5e27115 | 2009-06-23 17:30:11 -0700 | [diff] [blame] | 132 | obj = env->NewByteArray(size); |
| 133 | if (obj == NULL) { |
| 134 | LOGE("Couldn't allocate byte array for JPEG data"); |
| 135 | env->ExceptionClear(); |
| 136 | } else { |
Dave Sparks | a95f495 | 2009-07-10 18:13:36 -0700 | [diff] [blame] | 137 | env->SetByteArrayRegion(obj, 0, size, data); |
Dave Sparks | 5e27115 | 2009-06-23 17:30:11 -0700 | [diff] [blame] | 138 | } |
| 139 | } else { |
| 140 | LOGE("image heap is NULL"); |
| 141 | } |
| 142 | } |
| 143 | |
| 144 | // post image data to Java |
| 145 | env->CallStaticVoidMethod(mCameraJClass, fields.post_event, |
| 146 | mCameraJObjectWeak, msgType, 0, 0, obj); |
| 147 | if (obj) { |
| 148 | env->DeleteLocalRef(obj); |
| 149 | } |
| 150 | } |
| 151 | |
| 152 | void JNICameraContext::postData(int32_t msgType, const sp<IMemory>& dataPtr) |
| 153 | { |
| 154 | // VM pointer will be NULL if object is released |
| 155 | Mutex::Autolock _l(mLock); |
| 156 | JNIEnv *env = AndroidRuntime::getJNIEnv(); |
Dave Sparks | d0cbb1a | 2009-06-29 19:03:33 -0700 | [diff] [blame] | 157 | if (mCameraJObjectWeak == NULL) { |
| 158 | LOGW("callback on dead camera object"); |
| 159 | return; |
| 160 | } |
Dave Sparks | 5e27115 | 2009-06-23 17:30:11 -0700 | [diff] [blame] | 161 | |
| 162 | // return data based on callback type |
| 163 | switch(msgType) { |
Dave Sparks | 5e27115 | 2009-06-23 17:30:11 -0700 | [diff] [blame] | 164 | case CAMERA_MSG_VIDEO_FRAME: |
Dave Sparks | c62f9bd | 2009-06-26 13:33:32 -0700 | [diff] [blame] | 165 | // should never happen |
Dave Sparks | 5e27115 | 2009-06-23 17:30:11 -0700 | [diff] [blame] | 166 | break; |
Dave Sparks | c62f9bd | 2009-06-26 13:33:32 -0700 | [diff] [blame] | 167 | // don't return raw data to Java |
Dave Sparks | 5e27115 | 2009-06-23 17:30:11 -0700 | [diff] [blame] | 168 | case CAMERA_MSG_RAW_IMAGE: |
| 169 | LOGV("rawCallback"); |
| 170 | env->CallStaticVoidMethod(mCameraJClass, fields.post_event, |
Dave Sparks | c62f9bd | 2009-06-26 13:33:32 -0700 | [diff] [blame] | 171 | mCameraJObjectWeak, msgType, 0, 0, NULL); |
Dave Sparks | 5e27115 | 2009-06-23 17:30:11 -0700 | [diff] [blame] | 172 | break; |
| 173 | default: |
Dave Sparks | da57856 | 2009-07-21 08:43:29 -0700 | [diff] [blame] | 174 | // TODO: Change to LOGV |
| 175 | LOGD("dataCallback(%d, %p)", msgType, dataPtr.get()); |
Dave Sparks | c62f9bd | 2009-06-26 13:33:32 -0700 | [diff] [blame] | 176 | copyAndPost(env, dataPtr, msgType); |
Dave Sparks | 5e27115 | 2009-06-23 17:30:11 -0700 | [diff] [blame] | 177 | break; |
| 178 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 179 | } |
| 180 | |
Dave Sparks | 59c1a93 | 2009-07-08 15:56:53 -0700 | [diff] [blame] | 181 | void JNICameraContext::postDataTimestamp(nsecs_t timestamp, int32_t msgType, const sp<IMemory>& dataPtr) |
| 182 | { |
| 183 | // TODO: plumb up to Java. For now, just drop the timestamp |
| 184 | postData(msgType, dataPtr); |
| 185 | } |
| 186 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 187 | // connect to camera service |
| 188 | static void android_hardware_Camera_native_setup(JNIEnv *env, jobject thiz, jobject weak_this) |
| 189 | { |
| 190 | sp<Camera> camera = Camera::connect(); |
| 191 | |
| 192 | if (camera == NULL) { |
Wu-cheng Li | fa3e556 | 2009-05-04 19:38:43 +0800 | [diff] [blame] | 193 | jniThrowException(env, "java/lang/RuntimeException", |
| 194 | "Fail to connect to camera service"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 195 | return; |
| 196 | } |
| 197 | |
| 198 | // make sure camera hardware is alive |
| 199 | if (camera->getStatus() != NO_ERROR) { |
Wu-cheng Li | ba55b36 | 2009-06-10 16:55:39 +0800 | [diff] [blame] | 200 | jniThrowException(env, "java/lang/RuntimeException", "Camera initialization failed"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 201 | return; |
| 202 | } |
| 203 | |
| 204 | jclass clazz = env->GetObjectClass(thiz); |
| 205 | if (clazz == NULL) { |
Wu-cheng Li | ba55b36 | 2009-06-10 16:55:39 +0800 | [diff] [blame] | 206 | jniThrowException(env, "java/lang/RuntimeException", "Can't find android/hardware/Camera"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 207 | return; |
| 208 | } |
| 209 | |
| 210 | // We use a weak reference so the Camera object can be garbage collected. |
| 211 | // The reference is only used as a proxy for callbacks. |
Dave Sparks | 5e27115 | 2009-06-23 17:30:11 -0700 | [diff] [blame] | 212 | sp<JNICameraContext> context = new JNICameraContext(env, weak_this, clazz, camera); |
| 213 | context->incStrong(thiz); |
| 214 | camera->setListener(context); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 215 | |
| 216 | // save context in opaque field |
Dave Sparks | 5e27115 | 2009-06-23 17:30:11 -0700 | [diff] [blame] | 217 | env->SetIntField(thiz, fields.context, (int)context.get()); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 218 | } |
| 219 | |
| 220 | // disconnect from camera service |
| 221 | // It's okay to call this when the native camera context is already null. |
| 222 | // This handles the case where the user has called release() and the |
| 223 | // finalizer is invoked later. |
| 224 | static void android_hardware_Camera_release(JNIEnv *env, jobject thiz) |
| 225 | { |
Dave Sparks | da57856 | 2009-07-21 08:43:29 -0700 | [diff] [blame] | 226 | // TODO: Change to LOGV |
| 227 | LOGD("release camera"); |
Dave Sparks | 5e27115 | 2009-06-23 17:30:11 -0700 | [diff] [blame] | 228 | JNICameraContext* context = NULL; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 229 | sp<Camera> camera; |
| 230 | { |
| 231 | Mutex::Autolock _l(sLock); |
Dave Sparks | 5e27115 | 2009-06-23 17:30:11 -0700 | [diff] [blame] | 232 | context = reinterpret_cast<JNICameraContext*>(env->GetIntField(thiz, fields.context)); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 233 | |
| 234 | // Make sure we do not attempt to callback on a deleted Java object. |
| 235 | env->SetIntField(thiz, fields.context, 0); |
| 236 | } |
| 237 | |
| 238 | // clean up if release has not been called before |
| 239 | if (context != NULL) { |
Dave Sparks | 5e27115 | 2009-06-23 17:30:11 -0700 | [diff] [blame] | 240 | camera = context->getCamera(); |
| 241 | context->release(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 242 | LOGV("native_release: context=%p camera=%p", context, camera.get()); |
| 243 | |
| 244 | // clear callbacks |
| 245 | if (camera != NULL) { |
Dave Sparks | 5e27115 | 2009-06-23 17:30:11 -0700 | [diff] [blame] | 246 | camera->setPreviewCallbackFlags(FRAME_CALLBACK_FLAG_NOOP); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 247 | camera->disconnect(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 248 | } |
| 249 | |
| 250 | // remove context to prevent further Java access |
Dave Sparks | 5e27115 | 2009-06-23 17:30:11 -0700 | [diff] [blame] | 251 | context->decStrong(thiz); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 252 | } |
| 253 | } |
| 254 | |
| 255 | static void android_hardware_Camera_setPreviewDisplay(JNIEnv *env, jobject thiz, jobject jSurface) |
| 256 | { |
| 257 | LOGV("setPreviewDisplay"); |
| 258 | sp<Camera> camera = get_native_camera(env, thiz, NULL); |
| 259 | if (camera == 0) return; |
| 260 | |
Wu-cheng Li | b8a10fe | 2009-06-23 23:37:36 +0800 | [diff] [blame] | 261 | sp<Surface> surface = NULL; |
| 262 | if (jSurface != NULL) { |
| 263 | surface = reinterpret_cast<Surface*>(env->GetIntField(jSurface, fields.surface)); |
| 264 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 265 | if (camera->setPreviewDisplay(surface) != NO_ERROR) { |
| 266 | jniThrowException(env, "java/io/IOException", "setPreviewDisplay failed"); |
| 267 | } |
| 268 | } |
| 269 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 270 | static void android_hardware_Camera_startPreview(JNIEnv *env, jobject thiz) |
| 271 | { |
| 272 | LOGV("startPreview"); |
| 273 | sp<Camera> camera = get_native_camera(env, thiz, NULL); |
| 274 | if (camera == 0) return; |
| 275 | |
| 276 | if (camera->startPreview() != NO_ERROR) { |
Wu-cheng Li | ba55b36 | 2009-06-10 16:55:39 +0800 | [diff] [blame] | 277 | jniThrowException(env, "java/lang/RuntimeException", "startPreview failed"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 278 | return; |
| 279 | } |
| 280 | } |
| 281 | |
| 282 | static void android_hardware_Camera_stopPreview(JNIEnv *env, jobject thiz) |
| 283 | { |
| 284 | LOGV("stopPreview"); |
| 285 | sp<Camera> c = get_native_camera(env, thiz, NULL); |
| 286 | if (c == 0) return; |
| 287 | |
| 288 | c->stopPreview(); |
| 289 | } |
| 290 | |
| 291 | static bool android_hardware_Camera_previewEnabled(JNIEnv *env, jobject thiz) |
| 292 | { |
| 293 | LOGV("previewEnabled"); |
| 294 | sp<Camera> c = get_native_camera(env, thiz, NULL); |
| 295 | if (c == 0) return false; |
| 296 | |
| 297 | return c->previewEnabled(); |
| 298 | } |
| 299 | |
| 300 | static void android_hardware_Camera_setHasPreviewCallback(JNIEnv *env, jobject thiz, jboolean installed, jboolean oneshot) |
| 301 | { |
| 302 | // Important: Only install preview_callback if the Java code has called |
| 303 | // setPreviewCallback() with a non-null value, otherwise we'd pay to memcpy |
| 304 | // each preview frame for nothing. |
Dave Sparks | 5e27115 | 2009-06-23 17:30:11 -0700 | [diff] [blame] | 305 | JNICameraContext* context; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 306 | sp<Camera> camera = get_native_camera(env, thiz, &context); |
| 307 | if (camera == 0) return; |
| 308 | |
| 309 | int callback_flag; |
| 310 | if (installed) { |
| 311 | callback_flag = oneshot ? FRAME_CALLBACK_FLAG_BARCODE_SCANNER : FRAME_CALLBACK_FLAG_CAMERA; |
| 312 | } else { |
| 313 | callback_flag = FRAME_CALLBACK_FLAG_NOOP; |
| 314 | } |
Dave Sparks | 5e27115 | 2009-06-23 17:30:11 -0700 | [diff] [blame] | 315 | camera->setPreviewCallbackFlags(callback_flag); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 316 | } |
| 317 | |
| 318 | static void android_hardware_Camera_autoFocus(JNIEnv *env, jobject thiz) |
| 319 | { |
| 320 | LOGV("autoFocus"); |
Dave Sparks | 5e27115 | 2009-06-23 17:30:11 -0700 | [diff] [blame] | 321 | JNICameraContext* context; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 322 | sp<Camera> c = get_native_camera(env, thiz, &context); |
| 323 | if (c == 0) return; |
| 324 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 325 | if (c->autoFocus() != NO_ERROR) { |
Wu-cheng Li | ba55b36 | 2009-06-10 16:55:39 +0800 | [diff] [blame] | 326 | jniThrowException(env, "java/lang/RuntimeException", "autoFocus failed"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 327 | } |
| 328 | } |
| 329 | |
Chih-Chung Chang | 244f8c2 | 2009-09-15 14:51:56 +0800 | [diff] [blame] | 330 | static void android_hardware_Camera_cancelAutoFocus(JNIEnv *env, jobject thiz) |
| 331 | { |
| 332 | LOGV("cancelAutoFocus"); |
| 333 | JNICameraContext* context; |
| 334 | sp<Camera> c = get_native_camera(env, thiz, &context); |
| 335 | if (c == 0) return; |
| 336 | |
| 337 | if (c->cancelAutoFocus() != NO_ERROR) { |
| 338 | jniThrowException(env, "java/lang/RuntimeException", "cancelAutoFocus failed"); |
| 339 | } |
| 340 | } |
| 341 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 342 | static void android_hardware_Camera_takePicture(JNIEnv *env, jobject thiz) |
| 343 | { |
| 344 | LOGV("takePicture"); |
Dave Sparks | 5e27115 | 2009-06-23 17:30:11 -0700 | [diff] [blame] | 345 | JNICameraContext* context; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 346 | sp<Camera> camera = get_native_camera(env, thiz, &context); |
| 347 | if (camera == 0) return; |
| 348 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 349 | if (camera->takePicture() != NO_ERROR) { |
Wu-cheng Li | ba55b36 | 2009-06-10 16:55:39 +0800 | [diff] [blame] | 350 | jniThrowException(env, "java/lang/RuntimeException", "takePicture failed"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 351 | return; |
| 352 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 353 | } |
| 354 | |
| 355 | static void android_hardware_Camera_setParameters(JNIEnv *env, jobject thiz, jstring params) |
| 356 | { |
| 357 | LOGV("setParameters"); |
| 358 | sp<Camera> camera = get_native_camera(env, thiz, NULL); |
| 359 | if (camera == 0) return; |
| 360 | |
| 361 | const jchar* str = env->GetStringCritical(params, 0); |
| 362 | String8 params8; |
| 363 | if (params) { |
| 364 | params8 = String8(str, env->GetStringLength(params)); |
| 365 | env->ReleaseStringCritical(params, str); |
| 366 | } |
| 367 | if (camera->setParameters(params8) != NO_ERROR) { |
Wu-cheng Li | ba55b36 | 2009-06-10 16:55:39 +0800 | [diff] [blame] | 368 | jniThrowException(env, "java/lang/RuntimeException", "setParameters failed"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 369 | return; |
| 370 | } |
| 371 | } |
| 372 | |
| 373 | static jstring android_hardware_Camera_getParameters(JNIEnv *env, jobject thiz) |
| 374 | { |
| 375 | LOGV("getParameters"); |
| 376 | sp<Camera> camera = get_native_camera(env, thiz, NULL); |
| 377 | if (camera == 0) return 0; |
| 378 | |
| 379 | return env->NewStringUTF(camera->getParameters().string()); |
| 380 | } |
| 381 | |
| 382 | static void android_hardware_Camera_reconnect(JNIEnv *env, jobject thiz) |
| 383 | { |
| 384 | LOGV("reconnect"); |
| 385 | sp<Camera> camera = get_native_camera(env, thiz, NULL); |
| 386 | if (camera == 0) return; |
| 387 | |
| 388 | if (camera->reconnect() != NO_ERROR) { |
| 389 | jniThrowException(env, "java/io/IOException", "reconnect failed"); |
| 390 | return; |
| 391 | } |
| 392 | } |
| 393 | |
Wu-cheng Li | ffe1cf2 | 2009-09-10 16:49:17 +0800 | [diff] [blame] | 394 | static void android_hardware_Camera_lock(JNIEnv *env, jobject thiz) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 395 | { |
| 396 | LOGV("lock"); |
| 397 | sp<Camera> camera = get_native_camera(env, thiz, NULL); |
Wu-cheng Li | ffe1cf2 | 2009-09-10 16:49:17 +0800 | [diff] [blame] | 398 | if (camera == 0) return; |
| 399 | |
| 400 | if (camera->lock() != NO_ERROR) { |
| 401 | jniThrowException(env, "java/lang/RuntimeException", "lock failed"); |
| 402 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 403 | } |
| 404 | |
Wu-cheng Li | ffe1cf2 | 2009-09-10 16:49:17 +0800 | [diff] [blame] | 405 | static void android_hardware_Camera_unlock(JNIEnv *env, jobject thiz) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 406 | { |
| 407 | LOGV("unlock"); |
| 408 | sp<Camera> camera = get_native_camera(env, thiz, NULL); |
Wu-cheng Li | ffe1cf2 | 2009-09-10 16:49:17 +0800 | [diff] [blame] | 409 | if (camera == 0) return; |
| 410 | |
| 411 | if (camera->unlock() != NO_ERROR) { |
| 412 | jniThrowException(env, "java/lang/RuntimeException", "unlock failed"); |
| 413 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 414 | } |
| 415 | |
| 416 | //------------------------------------------------- |
| 417 | |
| 418 | static JNINativeMethod camMethods[] = { |
| 419 | { "native_setup", |
| 420 | "(Ljava/lang/Object;)V", |
| 421 | (void*)android_hardware_Camera_native_setup }, |
| 422 | { "native_release", |
| 423 | "()V", |
| 424 | (void*)android_hardware_Camera_release }, |
| 425 | { "setPreviewDisplay", |
| 426 | "(Landroid/view/Surface;)V", |
| 427 | (void *)android_hardware_Camera_setPreviewDisplay }, |
| 428 | { "startPreview", |
| 429 | "()V", |
| 430 | (void *)android_hardware_Camera_startPreview }, |
| 431 | { "stopPreview", |
| 432 | "()V", |
| 433 | (void *)android_hardware_Camera_stopPreview }, |
| 434 | { "previewEnabled", |
| 435 | "()Z", |
| 436 | (void *)android_hardware_Camera_previewEnabled }, |
| 437 | { "setHasPreviewCallback", |
| 438 | "(ZZ)V", |
| 439 | (void *)android_hardware_Camera_setHasPreviewCallback }, |
| 440 | { "native_autoFocus", |
| 441 | "()V", |
| 442 | (void *)android_hardware_Camera_autoFocus }, |
Chih-Chung Chang | 244f8c2 | 2009-09-15 14:51:56 +0800 | [diff] [blame] | 443 | { "native_cancelAutoFocus", |
| 444 | "()V", |
| 445 | (void *)android_hardware_Camera_cancelAutoFocus }, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 446 | { "native_takePicture", |
| 447 | "()V", |
| 448 | (void *)android_hardware_Camera_takePicture }, |
| 449 | { "native_setParameters", |
| 450 | "(Ljava/lang/String;)V", |
| 451 | (void *)android_hardware_Camera_setParameters }, |
| 452 | { "native_getParameters", |
| 453 | "()Ljava/lang/String;", |
| 454 | (void *)android_hardware_Camera_getParameters }, |
| 455 | { "reconnect", |
| 456 | "()V", |
| 457 | (void*)android_hardware_Camera_reconnect }, |
| 458 | { "lock", |
Wu-cheng Li | ffe1cf2 | 2009-09-10 16:49:17 +0800 | [diff] [blame] | 459 | "()V", |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 460 | (void*)android_hardware_Camera_lock }, |
| 461 | { "unlock", |
Wu-cheng Li | ffe1cf2 | 2009-09-10 16:49:17 +0800 | [diff] [blame] | 462 | "()V", |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 463 | (void*)android_hardware_Camera_unlock }, |
| 464 | }; |
| 465 | |
| 466 | struct field { |
| 467 | const char *class_name; |
| 468 | const char *field_name; |
| 469 | const char *field_type; |
| 470 | jfieldID *jfield; |
| 471 | }; |
| 472 | |
| 473 | static int find_fields(JNIEnv *env, field *fields, int count) |
| 474 | { |
| 475 | for (int i = 0; i < count; i++) { |
| 476 | field *f = &fields[i]; |
| 477 | jclass clazz = env->FindClass(f->class_name); |
| 478 | if (clazz == NULL) { |
| 479 | LOGE("Can't find %s", f->class_name); |
| 480 | return -1; |
| 481 | } |
| 482 | |
| 483 | jfieldID field = env->GetFieldID(clazz, f->field_name, f->field_type); |
| 484 | if (field == NULL) { |
| 485 | LOGE("Can't find %s.%s", f->class_name, f->field_name); |
| 486 | return -1; |
| 487 | } |
| 488 | |
| 489 | *(f->jfield) = field; |
| 490 | } |
| 491 | |
| 492 | return 0; |
| 493 | } |
| 494 | |
| 495 | // Get all the required offsets in java class and register native functions |
| 496 | int register_android_hardware_Camera(JNIEnv *env) |
| 497 | { |
| 498 | field fields_to_find[] = { |
| 499 | { "android/hardware/Camera", "mNativeContext", "I", &fields.context }, |
| 500 | { "android/view/Surface", "mSurface", "I", &fields.surface } |
| 501 | }; |
| 502 | |
| 503 | if (find_fields(env, fields_to_find, NELEM(fields_to_find)) < 0) |
| 504 | return -1; |
| 505 | |
| 506 | jclass clazz = env->FindClass("android/hardware/Camera"); |
| 507 | fields.post_event = env->GetStaticMethodID(clazz, "postEventFromNative", |
| 508 | "(Ljava/lang/Object;IIILjava/lang/Object;)V"); |
| 509 | if (fields.post_event == NULL) { |
| 510 | LOGE("Can't find android/hardware/Camera.postEventFromNative"); |
| 511 | return -1; |
| 512 | } |
| 513 | |
| 514 | |
| 515 | // Register native functions |
| 516 | return AndroidRuntime::registerNativeMethods(env, "android/hardware/Camera", |
| 517 | camMethods, NELEM(camMethods)); |
| 518 | } |
| 519 | |