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 | |
Andrew Harp | 94927df | 2009-10-20 01:47:05 -0400 | [diff] [blame] | 26 | #include <utils/Vector.h> |
| 27 | |
Mathias Agopian | 000479f | 2010-02-09 17:46:37 -0800 | [diff] [blame] | 28 | #include <surfaceflinger/Surface.h> |
| 29 | #include <camera/Camera.h> |
Mathias Agopian | 0795272 | 2009-05-19 19:08:10 -0700 | [diff] [blame] | 30 | #include <binder/IMemory.h> |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 31 | |
| 32 | using namespace android; |
| 33 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 34 | struct fields_t { |
| 35 | jfieldID context; |
| 36 | jfieldID surface; |
| 37 | jmethodID post_event; |
| 38 | }; |
| 39 | |
| 40 | static fields_t fields; |
| 41 | static Mutex sLock; |
| 42 | |
Dave Sparks | 5e27115 | 2009-06-23 17:30:11 -0700 | [diff] [blame] | 43 | // provides persistent context for calls from native code to Java |
| 44 | class JNICameraContext: public CameraListener |
| 45 | { |
| 46 | public: |
| 47 | JNICameraContext(JNIEnv* env, jobject weak_this, jclass clazz, const sp<Camera>& camera); |
| 48 | ~JNICameraContext() { release(); } |
| 49 | virtual void notify(int32_t msgType, int32_t ext1, int32_t ext2); |
| 50 | virtual void postData(int32_t msgType, const sp<IMemory>& dataPtr); |
Dave Sparks | 59c1a93 | 2009-07-08 15:56:53 -0700 | [diff] [blame] | 51 | virtual void postDataTimestamp(nsecs_t timestamp, int32_t msgType, const sp<IMemory>& dataPtr); |
Andrew Harp | 94927df | 2009-10-20 01:47:05 -0400 | [diff] [blame] | 52 | void addCallbackBuffer(JNIEnv *env, jbyteArray cbb); |
| 53 | void setCallbackMode(JNIEnv *env, bool installed, bool manualMode); |
Dave Sparks | 5e27115 | 2009-06-23 17:30:11 -0700 | [diff] [blame] | 54 | sp<Camera> getCamera() { Mutex::Autolock _l(mLock); return mCamera; } |
| 55 | void release(); |
| 56 | |
| 57 | private: |
| 58 | void copyAndPost(JNIEnv* env, const sp<IMemory>& dataPtr, int msgType); |
Andrew Harp | 94927df | 2009-10-20 01:47:05 -0400 | [diff] [blame] | 59 | void clearCallbackBuffers_l(JNIEnv *env); |
Dave Sparks | 5e27115 | 2009-06-23 17:30:11 -0700 | [diff] [blame] | 60 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 61 | jobject mCameraJObjectWeak; // weak reference to java object |
| 62 | jclass mCameraJClass; // strong reference to java class |
Wu-cheng Li | ffe1cf2 | 2009-09-10 16:49:17 +0800 | [diff] [blame] | 63 | sp<Camera> mCamera; // strong reference to native object |
Dave Sparks | 5e27115 | 2009-06-23 17:30:11 -0700 | [diff] [blame] | 64 | Mutex mLock; |
Andrew Harp | 94927df | 2009-10-20 01:47:05 -0400 | [diff] [blame] | 65 | |
| 66 | Vector<jbyteArray> mCallbackBuffers; // Global reference application managed byte[] |
| 67 | bool mManualBufferMode; // Whether to use application managed buffers. |
| 68 | bool mManualCameraCallbackSet; // Whether the callback has been set, used to reduce unnecessary calls to set the callback. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 69 | }; |
| 70 | |
Dave Sparks | 5e27115 | 2009-06-23 17:30:11 -0700 | [diff] [blame] | 71 | 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] | 72 | { |
| 73 | sp<Camera> camera; |
| 74 | Mutex::Autolock _l(sLock); |
Dave Sparks | 5e27115 | 2009-06-23 17:30:11 -0700 | [diff] [blame] | 75 | 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] | 76 | if (context != NULL) { |
Dave Sparks | 5e27115 | 2009-06-23 17:30:11 -0700 | [diff] [blame] | 77 | camera = context->getCamera(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 78 | } |
| 79 | LOGV("get_native_camera: context=%p, camera=%p", context, camera.get()); |
| 80 | if (camera == 0) { |
| 81 | jniThrowException(env, "java/lang/RuntimeException", "Method called after release()"); |
| 82 | } |
| 83 | |
| 84 | if (pContext != NULL) *pContext = context; |
| 85 | return camera; |
| 86 | } |
| 87 | |
Dave Sparks | 5e27115 | 2009-06-23 17:30:11 -0700 | [diff] [blame] | 88 | 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] | 89 | { |
Dave Sparks | 5e27115 | 2009-06-23 17:30:11 -0700 | [diff] [blame] | 90 | mCameraJObjectWeak = env->NewGlobalRef(weak_this); |
| 91 | mCameraJClass = (jclass)env->NewGlobalRef(clazz); |
| 92 | mCamera = camera; |
Andrew Harp | 94927df | 2009-10-20 01:47:05 -0400 | [diff] [blame] | 93 | |
| 94 | mManualBufferMode = false; |
| 95 | mManualCameraCallbackSet = false; |
Dave Sparks | 5e27115 | 2009-06-23 17:30:11 -0700 | [diff] [blame] | 96 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 97 | |
Dave Sparks | 5e27115 | 2009-06-23 17:30:11 -0700 | [diff] [blame] | 98 | void JNICameraContext::release() |
| 99 | { |
| 100 | LOGV("release"); |
| 101 | Mutex::Autolock _l(mLock); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 102 | JNIEnv *env = AndroidRuntime::getJNIEnv(); |
Dave Sparks | 5e27115 | 2009-06-23 17:30:11 -0700 | [diff] [blame] | 103 | |
| 104 | if (mCameraJObjectWeak != NULL) { |
| 105 | env->DeleteGlobalRef(mCameraJObjectWeak); |
| 106 | mCameraJObjectWeak = NULL; |
| 107 | } |
| 108 | if (mCameraJClass != NULL) { |
| 109 | env->DeleteGlobalRef(mCameraJClass); |
| 110 | mCameraJClass = NULL; |
| 111 | } |
Andrew Harp | 94927df | 2009-10-20 01:47:05 -0400 | [diff] [blame] | 112 | clearCallbackBuffers_l(env); |
Dave Sparks | 5e27115 | 2009-06-23 17:30:11 -0700 | [diff] [blame] | 113 | mCamera.clear(); |
| 114 | } |
| 115 | |
| 116 | void JNICameraContext::notify(int32_t msgType, int32_t ext1, int32_t ext2) |
| 117 | { |
| 118 | LOGV("notify"); |
| 119 | |
| 120 | // VM pointer will be NULL if object is released |
| 121 | Mutex::Autolock _l(mLock); |
| 122 | if (mCameraJObjectWeak == NULL) { |
| 123 | LOGW("callback on dead camera object"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 124 | return; |
| 125 | } |
Dave Sparks | 5e27115 | 2009-06-23 17:30:11 -0700 | [diff] [blame] | 126 | JNIEnv *env = AndroidRuntime::getJNIEnv(); |
Dave Sparks | c62f9bd | 2009-06-26 13:33:32 -0700 | [diff] [blame] | 127 | env->CallStaticVoidMethod(mCameraJClass, fields.post_event, |
Chih-Chung Chang | 6157de0 | 2009-09-24 15:29:35 -0700 | [diff] [blame] | 128 | mCameraJObjectWeak, msgType, ext1, ext2, NULL); |
Dave Sparks | 5e27115 | 2009-06-23 17:30:11 -0700 | [diff] [blame] | 129 | } |
| 130 | |
| 131 | void JNICameraContext::copyAndPost(JNIEnv* env, const sp<IMemory>& dataPtr, int msgType) |
| 132 | { |
| 133 | jbyteArray obj = NULL; |
| 134 | |
| 135 | // allocate Java byte array and copy data |
| 136 | if (dataPtr != NULL) { |
| 137 | ssize_t offset; |
| 138 | size_t size; |
| 139 | sp<IMemoryHeap> heap = dataPtr->getMemory(&offset, &size); |
| 140 | LOGV("postData: off=%d, size=%d", offset, size); |
| 141 | uint8_t *heapBase = (uint8_t*)heap->base(); |
| 142 | |
| 143 | if (heapBase != NULL) { |
Dave Sparks | c4ca420 | 2009-07-13 09:38:20 -0700 | [diff] [blame] | 144 | const jbyte* data = reinterpret_cast<const jbyte*>(heapBase + offset); |
Andrew Harp | 94927df | 2009-10-20 01:47:05 -0400 | [diff] [blame] | 145 | |
| 146 | if (!mManualBufferMode) { |
| 147 | LOGV("Allocating callback buffer"); |
| 148 | obj = env->NewByteArray(size); |
| 149 | } else { |
| 150 | // Vector access should be protected by lock in postData() |
| 151 | if(!mCallbackBuffers.isEmpty()) { |
| 152 | LOGV("Using callback buffer from queue of length %d", mCallbackBuffers.size()); |
| 153 | jbyteArray globalBuffer = mCallbackBuffers.itemAt(0); |
| 154 | mCallbackBuffers.removeAt(0); |
| 155 | |
| 156 | obj = (jbyteArray)env->NewLocalRef(globalBuffer); |
| 157 | env->DeleteGlobalRef(globalBuffer); |
| 158 | |
| 159 | if (obj != NULL) { |
| 160 | jsize bufferLength = env->GetArrayLength(obj); |
| 161 | if ((int)bufferLength < (int)size) { |
| 162 | LOGE("Manually set buffer was too small! Expected %d bytes, but got %d!", |
| 163 | size, bufferLength); |
| 164 | env->DeleteLocalRef(obj); |
| 165 | return; |
| 166 | } |
| 167 | } |
| 168 | } |
| 169 | |
| 170 | if(mCallbackBuffers.isEmpty()) { |
Wu-cheng Li | f5dbf6a | 2010-01-30 22:34:48 +0800 | [diff] [blame] | 171 | LOGV("Out of buffers, clearing callback!"); |
Andrew Harp | 94927df | 2009-10-20 01:47:05 -0400 | [diff] [blame] | 172 | mCamera->setPreviewCallbackFlags(FRAME_CALLBACK_FLAG_NOOP); |
| 173 | mManualCameraCallbackSet = false; |
| 174 | |
| 175 | if (obj == NULL) { |
| 176 | return; |
| 177 | } |
| 178 | } |
| 179 | } |
| 180 | |
Dave Sparks | 5e27115 | 2009-06-23 17:30:11 -0700 | [diff] [blame] | 181 | if (obj == NULL) { |
| 182 | LOGE("Couldn't allocate byte array for JPEG data"); |
| 183 | env->ExceptionClear(); |
| 184 | } else { |
Dave Sparks | a95f495 | 2009-07-10 18:13:36 -0700 | [diff] [blame] | 185 | env->SetByteArrayRegion(obj, 0, size, data); |
Dave Sparks | 5e27115 | 2009-06-23 17:30:11 -0700 | [diff] [blame] | 186 | } |
| 187 | } else { |
| 188 | LOGE("image heap is NULL"); |
| 189 | } |
| 190 | } |
| 191 | |
| 192 | // post image data to Java |
| 193 | env->CallStaticVoidMethod(mCameraJClass, fields.post_event, |
| 194 | mCameraJObjectWeak, msgType, 0, 0, obj); |
| 195 | if (obj) { |
| 196 | env->DeleteLocalRef(obj); |
| 197 | } |
| 198 | } |
| 199 | |
| 200 | void JNICameraContext::postData(int32_t msgType, const sp<IMemory>& dataPtr) |
| 201 | { |
| 202 | // VM pointer will be NULL if object is released |
| 203 | Mutex::Autolock _l(mLock); |
| 204 | JNIEnv *env = AndroidRuntime::getJNIEnv(); |
Dave Sparks | d0cbb1a | 2009-06-29 19:03:33 -0700 | [diff] [blame] | 205 | if (mCameraJObjectWeak == NULL) { |
| 206 | LOGW("callback on dead camera object"); |
| 207 | return; |
| 208 | } |
Dave Sparks | 5e27115 | 2009-06-23 17:30:11 -0700 | [diff] [blame] | 209 | |
| 210 | // return data based on callback type |
| 211 | switch(msgType) { |
Dave Sparks | 5e27115 | 2009-06-23 17:30:11 -0700 | [diff] [blame] | 212 | case CAMERA_MSG_VIDEO_FRAME: |
Dave Sparks | c62f9bd | 2009-06-26 13:33:32 -0700 | [diff] [blame] | 213 | // should never happen |
Dave Sparks | 5e27115 | 2009-06-23 17:30:11 -0700 | [diff] [blame] | 214 | break; |
Dave Sparks | c62f9bd | 2009-06-26 13:33:32 -0700 | [diff] [blame] | 215 | // don't return raw data to Java |
Dave Sparks | 5e27115 | 2009-06-23 17:30:11 -0700 | [diff] [blame] | 216 | case CAMERA_MSG_RAW_IMAGE: |
| 217 | LOGV("rawCallback"); |
| 218 | env->CallStaticVoidMethod(mCameraJClass, fields.post_event, |
Dave Sparks | c62f9bd | 2009-06-26 13:33:32 -0700 | [diff] [blame] | 219 | mCameraJObjectWeak, msgType, 0, 0, NULL); |
Dave Sparks | 5e27115 | 2009-06-23 17:30:11 -0700 | [diff] [blame] | 220 | break; |
| 221 | default: |
Dave Sparks | da57856 | 2009-07-21 08:43:29 -0700 | [diff] [blame] | 222 | // TODO: Change to LOGV |
Dave Sparks | c95a176 | 2010-01-04 08:55:04 -0800 | [diff] [blame] | 223 | LOGV("dataCallback(%d, %p)", msgType, dataPtr.get()); |
Dave Sparks | c62f9bd | 2009-06-26 13:33:32 -0700 | [diff] [blame] | 224 | copyAndPost(env, dataPtr, msgType); |
Dave Sparks | 5e27115 | 2009-06-23 17:30:11 -0700 | [diff] [blame] | 225 | break; |
| 226 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 227 | } |
| 228 | |
Dave Sparks | 59c1a93 | 2009-07-08 15:56:53 -0700 | [diff] [blame] | 229 | void JNICameraContext::postDataTimestamp(nsecs_t timestamp, int32_t msgType, const sp<IMemory>& dataPtr) |
| 230 | { |
| 231 | // TODO: plumb up to Java. For now, just drop the timestamp |
| 232 | postData(msgType, dataPtr); |
| 233 | } |
| 234 | |
Andrew Harp | 94927df | 2009-10-20 01:47:05 -0400 | [diff] [blame] | 235 | void JNICameraContext::setCallbackMode(JNIEnv *env, bool installed, bool manualMode) |
| 236 | { |
| 237 | Mutex::Autolock _l(mLock); |
| 238 | mManualBufferMode = manualMode; |
| 239 | mManualCameraCallbackSet = false; |
| 240 | |
| 241 | // In order to limit the over usage of binder threads, all non-manual buffer |
| 242 | // callbacks use FRAME_CALLBACK_FLAG_BARCODE_SCANNER mode now. |
| 243 | // |
| 244 | // Continuous callbacks will have the callback re-registered from handleMessage. |
| 245 | // Manual buffer mode will operate as fast as possible, relying on the finite supply |
| 246 | // of buffers for throttling. |
| 247 | |
| 248 | if (!installed) { |
| 249 | mCamera->setPreviewCallbackFlags(FRAME_CALLBACK_FLAG_NOOP); |
| 250 | clearCallbackBuffers_l(env); |
| 251 | } else if (mManualBufferMode) { |
| 252 | if (!mCallbackBuffers.isEmpty()) { |
| 253 | mCamera->setPreviewCallbackFlags(FRAME_CALLBACK_FLAG_CAMERA); |
| 254 | mManualCameraCallbackSet = true; |
| 255 | } |
| 256 | } else { |
| 257 | mCamera->setPreviewCallbackFlags(FRAME_CALLBACK_FLAG_BARCODE_SCANNER); |
| 258 | clearCallbackBuffers_l(env); |
| 259 | } |
| 260 | } |
| 261 | |
| 262 | void JNICameraContext::addCallbackBuffer(JNIEnv *env, jbyteArray cbb) |
| 263 | { |
| 264 | if (cbb != NULL) { |
| 265 | Mutex::Autolock _l(mLock); |
| 266 | jbyteArray callbackBuffer = (jbyteArray)env->NewGlobalRef(cbb); |
| 267 | mCallbackBuffers.push(cbb); |
| 268 | |
| 269 | LOGV("Adding callback buffer to queue, %d total", mCallbackBuffers.size()); |
| 270 | |
| 271 | // We want to make sure the camera knows we're ready for the next frame. |
| 272 | // This may have come unset had we not had a callbackbuffer ready for it last time. |
| 273 | if (mManualBufferMode && !mManualCameraCallbackSet) { |
| 274 | mCamera->setPreviewCallbackFlags(FRAME_CALLBACK_FLAG_CAMERA); |
| 275 | mManualCameraCallbackSet = true; |
| 276 | } |
| 277 | } else { |
| 278 | LOGE("Null byte array!"); |
| 279 | } |
| 280 | } |
| 281 | |
| 282 | void JNICameraContext::clearCallbackBuffers_l(JNIEnv *env) |
| 283 | { |
| 284 | LOGV("Clearing callback buffers, %d remained", mCallbackBuffers.size()); |
| 285 | while(!mCallbackBuffers.isEmpty()) { |
| 286 | env->DeleteGlobalRef(mCallbackBuffers.top()); |
| 287 | mCallbackBuffers.pop(); |
| 288 | } |
| 289 | } |
| 290 | |
Chih-Chung Chang | e25cc65 | 2010-05-06 16:36:58 +0800 | [diff] [blame^] | 291 | static jint android_hardware_Camera_getNumberOfCameras(JNIEnv *env, jobject thiz) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 292 | { |
Chih-Chung Chang | e25cc65 | 2010-05-06 16:36:58 +0800 | [diff] [blame^] | 293 | return Camera::getNumberOfCameras(); |
| 294 | } |
| 295 | |
| 296 | // connect to camera service |
| 297 | static void android_hardware_Camera_native_setup(JNIEnv *env, jobject thiz, |
| 298 | jobject weak_this, jint cameraId) |
| 299 | { |
| 300 | sp<Camera> camera = Camera::connect(cameraId); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 301 | |
| 302 | if (camera == NULL) { |
Wu-cheng Li | fa3e556 | 2009-05-04 19:38:43 +0800 | [diff] [blame] | 303 | jniThrowException(env, "java/lang/RuntimeException", |
| 304 | "Fail to connect to camera service"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 305 | return; |
| 306 | } |
| 307 | |
| 308 | // make sure camera hardware is alive |
| 309 | if (camera->getStatus() != NO_ERROR) { |
Wu-cheng Li | ba55b36 | 2009-06-10 16:55:39 +0800 | [diff] [blame] | 310 | jniThrowException(env, "java/lang/RuntimeException", "Camera initialization failed"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 311 | return; |
| 312 | } |
| 313 | |
| 314 | jclass clazz = env->GetObjectClass(thiz); |
| 315 | if (clazz == NULL) { |
Wu-cheng Li | ba55b36 | 2009-06-10 16:55:39 +0800 | [diff] [blame] | 316 | 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] | 317 | return; |
| 318 | } |
| 319 | |
| 320 | // We use a weak reference so the Camera object can be garbage collected. |
| 321 | // The reference is only used as a proxy for callbacks. |
Dave Sparks | 5e27115 | 2009-06-23 17:30:11 -0700 | [diff] [blame] | 322 | sp<JNICameraContext> context = new JNICameraContext(env, weak_this, clazz, camera); |
| 323 | context->incStrong(thiz); |
| 324 | camera->setListener(context); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 325 | |
| 326 | // save context in opaque field |
Dave Sparks | 5e27115 | 2009-06-23 17:30:11 -0700 | [diff] [blame] | 327 | env->SetIntField(thiz, fields.context, (int)context.get()); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 328 | } |
| 329 | |
| 330 | // disconnect from camera service |
| 331 | // It's okay to call this when the native camera context is already null. |
| 332 | // This handles the case where the user has called release() and the |
| 333 | // finalizer is invoked later. |
| 334 | static void android_hardware_Camera_release(JNIEnv *env, jobject thiz) |
| 335 | { |
Dave Sparks | da57856 | 2009-07-21 08:43:29 -0700 | [diff] [blame] | 336 | // TODO: Change to LOGV |
Joe Onorato | f5d95cb | 2010-01-07 21:48:32 -0500 | [diff] [blame] | 337 | LOGV("release camera"); |
Dave Sparks | 5e27115 | 2009-06-23 17:30:11 -0700 | [diff] [blame] | 338 | JNICameraContext* context = NULL; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 339 | sp<Camera> camera; |
| 340 | { |
| 341 | Mutex::Autolock _l(sLock); |
Dave Sparks | 5e27115 | 2009-06-23 17:30:11 -0700 | [diff] [blame] | 342 | context = reinterpret_cast<JNICameraContext*>(env->GetIntField(thiz, fields.context)); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 343 | |
| 344 | // Make sure we do not attempt to callback on a deleted Java object. |
| 345 | env->SetIntField(thiz, fields.context, 0); |
| 346 | } |
| 347 | |
| 348 | // clean up if release has not been called before |
| 349 | if (context != NULL) { |
Dave Sparks | 5e27115 | 2009-06-23 17:30:11 -0700 | [diff] [blame] | 350 | camera = context->getCamera(); |
| 351 | context->release(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 352 | LOGV("native_release: context=%p camera=%p", context, camera.get()); |
| 353 | |
| 354 | // clear callbacks |
| 355 | if (camera != NULL) { |
Dave Sparks | 5e27115 | 2009-06-23 17:30:11 -0700 | [diff] [blame] | 356 | camera->setPreviewCallbackFlags(FRAME_CALLBACK_FLAG_NOOP); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 357 | camera->disconnect(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 358 | } |
| 359 | |
| 360 | // remove context to prevent further Java access |
Dave Sparks | 5e27115 | 2009-06-23 17:30:11 -0700 | [diff] [blame] | 361 | context->decStrong(thiz); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 362 | } |
| 363 | } |
| 364 | |
| 365 | static void android_hardware_Camera_setPreviewDisplay(JNIEnv *env, jobject thiz, jobject jSurface) |
| 366 | { |
| 367 | LOGV("setPreviewDisplay"); |
| 368 | sp<Camera> camera = get_native_camera(env, thiz, NULL); |
| 369 | if (camera == 0) return; |
| 370 | |
Wu-cheng Li | b8a10fe | 2009-06-23 23:37:36 +0800 | [diff] [blame] | 371 | sp<Surface> surface = NULL; |
| 372 | if (jSurface != NULL) { |
| 373 | surface = reinterpret_cast<Surface*>(env->GetIntField(jSurface, fields.surface)); |
| 374 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 375 | if (camera->setPreviewDisplay(surface) != NO_ERROR) { |
| 376 | jniThrowException(env, "java/io/IOException", "setPreviewDisplay failed"); |
| 377 | } |
| 378 | } |
| 379 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 380 | static void android_hardware_Camera_startPreview(JNIEnv *env, jobject thiz) |
| 381 | { |
| 382 | LOGV("startPreview"); |
| 383 | sp<Camera> camera = get_native_camera(env, thiz, NULL); |
| 384 | if (camera == 0) return; |
| 385 | |
| 386 | if (camera->startPreview() != NO_ERROR) { |
Wu-cheng Li | ba55b36 | 2009-06-10 16:55:39 +0800 | [diff] [blame] | 387 | jniThrowException(env, "java/lang/RuntimeException", "startPreview failed"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 388 | return; |
| 389 | } |
| 390 | } |
| 391 | |
| 392 | static void android_hardware_Camera_stopPreview(JNIEnv *env, jobject thiz) |
| 393 | { |
| 394 | LOGV("stopPreview"); |
| 395 | sp<Camera> c = get_native_camera(env, thiz, NULL); |
| 396 | if (c == 0) return; |
| 397 | |
| 398 | c->stopPreview(); |
| 399 | } |
| 400 | |
| 401 | static bool android_hardware_Camera_previewEnabled(JNIEnv *env, jobject thiz) |
| 402 | { |
| 403 | LOGV("previewEnabled"); |
| 404 | sp<Camera> c = get_native_camera(env, thiz, NULL); |
| 405 | if (c == 0) return false; |
| 406 | |
| 407 | return c->previewEnabled(); |
| 408 | } |
| 409 | |
Andrew Harp | 94927df | 2009-10-20 01:47:05 -0400 | [diff] [blame] | 410 | static void android_hardware_Camera_setHasPreviewCallback(JNIEnv *env, jobject thiz, jboolean installed, jboolean manualBuffer) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 411 | { |
Andrew Harp | 94927df | 2009-10-20 01:47:05 -0400 | [diff] [blame] | 412 | LOGV("setHasPreviewCallback: installed:%d, manualBuffer:%d", (int)installed, (int)manualBuffer); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 413 | // Important: Only install preview_callback if the Java code has called |
| 414 | // setPreviewCallback() with a non-null value, otherwise we'd pay to memcpy |
| 415 | // each preview frame for nothing. |
Dave Sparks | 5e27115 | 2009-06-23 17:30:11 -0700 | [diff] [blame] | 416 | JNICameraContext* context; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 417 | sp<Camera> camera = get_native_camera(env, thiz, &context); |
| 418 | if (camera == 0) return; |
| 419 | |
Andrew Harp | 94927df | 2009-10-20 01:47:05 -0400 | [diff] [blame] | 420 | // setCallbackMode will take care of setting the context flags and calling |
| 421 | // camera->setPreviewCallbackFlags within a mutex for us. |
| 422 | context->setCallbackMode(env, installed, manualBuffer); |
| 423 | } |
| 424 | |
| 425 | static void android_hardware_Camera_addCallbackBuffer(JNIEnv *env, jobject thiz, jbyteArray bytes) { |
| 426 | LOGV("addCallbackBuffer"); |
| 427 | |
| 428 | JNICameraContext* context = reinterpret_cast<JNICameraContext*>(env->GetIntField(thiz, fields.context)); |
| 429 | |
| 430 | if (context != NULL) { |
| 431 | context->addCallbackBuffer(env, bytes); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 432 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 433 | } |
| 434 | |
| 435 | static void android_hardware_Camera_autoFocus(JNIEnv *env, jobject thiz) |
| 436 | { |
| 437 | LOGV("autoFocus"); |
Dave Sparks | 5e27115 | 2009-06-23 17:30:11 -0700 | [diff] [blame] | 438 | JNICameraContext* context; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 439 | sp<Camera> c = get_native_camera(env, thiz, &context); |
| 440 | if (c == 0) return; |
| 441 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 442 | if (c->autoFocus() != NO_ERROR) { |
Wu-cheng Li | ba55b36 | 2009-06-10 16:55:39 +0800 | [diff] [blame] | 443 | jniThrowException(env, "java/lang/RuntimeException", "autoFocus failed"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 444 | } |
| 445 | } |
| 446 | |
Chih-Chung Chang | 244f8c2 | 2009-09-15 14:51:56 +0800 | [diff] [blame] | 447 | static void android_hardware_Camera_cancelAutoFocus(JNIEnv *env, jobject thiz) |
| 448 | { |
| 449 | LOGV("cancelAutoFocus"); |
| 450 | JNICameraContext* context; |
| 451 | sp<Camera> c = get_native_camera(env, thiz, &context); |
| 452 | if (c == 0) return; |
| 453 | |
| 454 | if (c->cancelAutoFocus() != NO_ERROR) { |
| 455 | jniThrowException(env, "java/lang/RuntimeException", "cancelAutoFocus failed"); |
| 456 | } |
| 457 | } |
| 458 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 459 | static void android_hardware_Camera_takePicture(JNIEnv *env, jobject thiz) |
| 460 | { |
| 461 | LOGV("takePicture"); |
Dave Sparks | 5e27115 | 2009-06-23 17:30:11 -0700 | [diff] [blame] | 462 | JNICameraContext* context; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 463 | sp<Camera> camera = get_native_camera(env, thiz, &context); |
| 464 | if (camera == 0) return; |
| 465 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 466 | if (camera->takePicture() != NO_ERROR) { |
Wu-cheng Li | ba55b36 | 2009-06-10 16:55:39 +0800 | [diff] [blame] | 467 | jniThrowException(env, "java/lang/RuntimeException", "takePicture failed"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 468 | return; |
| 469 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 470 | } |
| 471 | |
| 472 | static void android_hardware_Camera_setParameters(JNIEnv *env, jobject thiz, jstring params) |
| 473 | { |
| 474 | LOGV("setParameters"); |
| 475 | sp<Camera> camera = get_native_camera(env, thiz, NULL); |
| 476 | if (camera == 0) return; |
| 477 | |
| 478 | const jchar* str = env->GetStringCritical(params, 0); |
| 479 | String8 params8; |
| 480 | if (params) { |
| 481 | params8 = String8(str, env->GetStringLength(params)); |
| 482 | env->ReleaseStringCritical(params, str); |
| 483 | } |
| 484 | if (camera->setParameters(params8) != NO_ERROR) { |
Wu-cheng Li | ba55b36 | 2009-06-10 16:55:39 +0800 | [diff] [blame] | 485 | jniThrowException(env, "java/lang/RuntimeException", "setParameters failed"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 486 | return; |
| 487 | } |
| 488 | } |
| 489 | |
| 490 | static jstring android_hardware_Camera_getParameters(JNIEnv *env, jobject thiz) |
| 491 | { |
| 492 | LOGV("getParameters"); |
| 493 | sp<Camera> camera = get_native_camera(env, thiz, NULL); |
| 494 | if (camera == 0) return 0; |
| 495 | |
| 496 | return env->NewStringUTF(camera->getParameters().string()); |
| 497 | } |
| 498 | |
| 499 | static void android_hardware_Camera_reconnect(JNIEnv *env, jobject thiz) |
| 500 | { |
| 501 | LOGV("reconnect"); |
| 502 | sp<Camera> camera = get_native_camera(env, thiz, NULL); |
| 503 | if (camera == 0) return; |
| 504 | |
| 505 | if (camera->reconnect() != NO_ERROR) { |
| 506 | jniThrowException(env, "java/io/IOException", "reconnect failed"); |
| 507 | return; |
| 508 | } |
| 509 | } |
| 510 | |
Wu-cheng Li | ffe1cf2 | 2009-09-10 16:49:17 +0800 | [diff] [blame] | 511 | 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] | 512 | { |
| 513 | LOGV("lock"); |
| 514 | sp<Camera> camera = get_native_camera(env, thiz, NULL); |
Wu-cheng Li | ffe1cf2 | 2009-09-10 16:49:17 +0800 | [diff] [blame] | 515 | if (camera == 0) return; |
| 516 | |
| 517 | if (camera->lock() != NO_ERROR) { |
| 518 | jniThrowException(env, "java/lang/RuntimeException", "lock failed"); |
| 519 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 520 | } |
| 521 | |
Wu-cheng Li | ffe1cf2 | 2009-09-10 16:49:17 +0800 | [diff] [blame] | 522 | 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] | 523 | { |
| 524 | LOGV("unlock"); |
| 525 | sp<Camera> camera = get_native_camera(env, thiz, NULL); |
Wu-cheng Li | ffe1cf2 | 2009-09-10 16:49:17 +0800 | [diff] [blame] | 526 | if (camera == 0) return; |
| 527 | |
| 528 | if (camera->unlock() != NO_ERROR) { |
| 529 | jniThrowException(env, "java/lang/RuntimeException", "unlock failed"); |
| 530 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 531 | } |
| 532 | |
Wu-cheng Li | 36f68b8 | 2009-09-28 16:14:58 -0700 | [diff] [blame] | 533 | static void android_hardware_Camera_startSmoothZoom(JNIEnv *env, jobject thiz, jint value) |
| 534 | { |
Joe Onorato | f5d95cb | 2010-01-07 21:48:32 -0500 | [diff] [blame] | 535 | LOGV("startSmoothZoom"); |
Wu-cheng Li | 36f68b8 | 2009-09-28 16:14:58 -0700 | [diff] [blame] | 536 | sp<Camera> camera = get_native_camera(env, thiz, NULL); |
| 537 | if (camera == 0) return; |
| 538 | |
Wu-cheng Li | 0ca2519 | 2010-03-29 16:21:12 +0800 | [diff] [blame] | 539 | status_t rc = camera->sendCommand(CAMERA_CMD_START_SMOOTH_ZOOM, value, 0); |
| 540 | if (rc == BAD_VALUE) { |
| 541 | char msg[64]; |
| 542 | sprintf(msg, "invalid zoom value=%d", value); |
| 543 | jniThrowException(env, "java/lang/IllegalArgumentException", msg); |
| 544 | } else if (rc != NO_ERROR) { |
Wu-cheng Li | 36f68b8 | 2009-09-28 16:14:58 -0700 | [diff] [blame] | 545 | jniThrowException(env, "java/lang/RuntimeException", "start smooth zoom failed"); |
| 546 | } |
| 547 | } |
| 548 | |
| 549 | static void android_hardware_Camera_stopSmoothZoom(JNIEnv *env, jobject thiz) |
| 550 | { |
Joe Onorato | f5d95cb | 2010-01-07 21:48:32 -0500 | [diff] [blame] | 551 | LOGV("stopSmoothZoom"); |
Wu-cheng Li | 36f68b8 | 2009-09-28 16:14:58 -0700 | [diff] [blame] | 552 | sp<Camera> camera = get_native_camera(env, thiz, NULL); |
| 553 | if (camera == 0) return; |
| 554 | |
| 555 | if (camera->sendCommand(CAMERA_CMD_STOP_SMOOTH_ZOOM, 0, 0) != NO_ERROR) { |
| 556 | jniThrowException(env, "java/lang/RuntimeException", "stop smooth zoom failed"); |
| 557 | } |
| 558 | } |
| 559 | |
Chih-Chung Chang | d1d7706 | 2010-01-22 17:49:48 -0800 | [diff] [blame] | 560 | static void android_hardware_Camera_setDisplayOrientation(JNIEnv *env, jobject thiz, |
| 561 | jint value) |
| 562 | { |
| 563 | LOGV("setDisplayOrientation"); |
| 564 | sp<Camera> camera = get_native_camera(env, thiz, NULL); |
| 565 | if (camera == 0) return; |
| 566 | |
| 567 | if (camera->sendCommand(CAMERA_CMD_SET_DISPLAY_ORIENTATION, value, 0) != NO_ERROR) { |
| 568 | jniThrowException(env, "java/lang/RuntimeException", "set display orientation failed"); |
| 569 | } |
| 570 | } |
| 571 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 572 | //------------------------------------------------- |
| 573 | |
| 574 | static JNINativeMethod camMethods[] = { |
Chih-Chung Chang | e25cc65 | 2010-05-06 16:36:58 +0800 | [diff] [blame^] | 575 | { "getNumberOfCameras", |
| 576 | "()I", |
| 577 | (void *)android_hardware_Camera_getNumberOfCameras }, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 578 | { "native_setup", |
Chih-Chung Chang | e25cc65 | 2010-05-06 16:36:58 +0800 | [diff] [blame^] | 579 | "(Ljava/lang/Object;I)V", |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 580 | (void*)android_hardware_Camera_native_setup }, |
| 581 | { "native_release", |
| 582 | "()V", |
| 583 | (void*)android_hardware_Camera_release }, |
| 584 | { "setPreviewDisplay", |
| 585 | "(Landroid/view/Surface;)V", |
| 586 | (void *)android_hardware_Camera_setPreviewDisplay }, |
| 587 | { "startPreview", |
| 588 | "()V", |
| 589 | (void *)android_hardware_Camera_startPreview }, |
| 590 | { "stopPreview", |
| 591 | "()V", |
| 592 | (void *)android_hardware_Camera_stopPreview }, |
| 593 | { "previewEnabled", |
| 594 | "()Z", |
| 595 | (void *)android_hardware_Camera_previewEnabled }, |
| 596 | { "setHasPreviewCallback", |
| 597 | "(ZZ)V", |
| 598 | (void *)android_hardware_Camera_setHasPreviewCallback }, |
Andrew Harp | 94927df | 2009-10-20 01:47:05 -0400 | [diff] [blame] | 599 | { "addCallbackBuffer", |
| 600 | "([B)V", |
| 601 | (void *)android_hardware_Camera_addCallbackBuffer }, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 602 | { "native_autoFocus", |
| 603 | "()V", |
| 604 | (void *)android_hardware_Camera_autoFocus }, |
Chih-Chung Chang | 244f8c2 | 2009-09-15 14:51:56 +0800 | [diff] [blame] | 605 | { "native_cancelAutoFocus", |
| 606 | "()V", |
| 607 | (void *)android_hardware_Camera_cancelAutoFocus }, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 608 | { "native_takePicture", |
| 609 | "()V", |
| 610 | (void *)android_hardware_Camera_takePicture }, |
| 611 | { "native_setParameters", |
| 612 | "(Ljava/lang/String;)V", |
| 613 | (void *)android_hardware_Camera_setParameters }, |
| 614 | { "native_getParameters", |
| 615 | "()Ljava/lang/String;", |
| 616 | (void *)android_hardware_Camera_getParameters }, |
| 617 | { "reconnect", |
| 618 | "()V", |
| 619 | (void*)android_hardware_Camera_reconnect }, |
| 620 | { "lock", |
Wu-cheng Li | ffe1cf2 | 2009-09-10 16:49:17 +0800 | [diff] [blame] | 621 | "()V", |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 622 | (void*)android_hardware_Camera_lock }, |
| 623 | { "unlock", |
Wu-cheng Li | ffe1cf2 | 2009-09-10 16:49:17 +0800 | [diff] [blame] | 624 | "()V", |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 625 | (void*)android_hardware_Camera_unlock }, |
Wu-cheng Li | 36f68b8 | 2009-09-28 16:14:58 -0700 | [diff] [blame] | 626 | { "startSmoothZoom", |
| 627 | "(I)V", |
| 628 | (void *)android_hardware_Camera_startSmoothZoom }, |
| 629 | { "stopSmoothZoom", |
| 630 | "()V", |
| 631 | (void *)android_hardware_Camera_stopSmoothZoom }, |
Chih-Chung Chang | d1d7706 | 2010-01-22 17:49:48 -0800 | [diff] [blame] | 632 | { "setDisplayOrientation", |
| 633 | "(I)V", |
| 634 | (void *)android_hardware_Camera_setDisplayOrientation }, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 635 | }; |
| 636 | |
| 637 | struct field { |
| 638 | const char *class_name; |
| 639 | const char *field_name; |
| 640 | const char *field_type; |
| 641 | jfieldID *jfield; |
| 642 | }; |
| 643 | |
| 644 | static int find_fields(JNIEnv *env, field *fields, int count) |
| 645 | { |
| 646 | for (int i = 0; i < count; i++) { |
| 647 | field *f = &fields[i]; |
| 648 | jclass clazz = env->FindClass(f->class_name); |
| 649 | if (clazz == NULL) { |
| 650 | LOGE("Can't find %s", f->class_name); |
| 651 | return -1; |
| 652 | } |
| 653 | |
| 654 | jfieldID field = env->GetFieldID(clazz, f->field_name, f->field_type); |
| 655 | if (field == NULL) { |
| 656 | LOGE("Can't find %s.%s", f->class_name, f->field_name); |
| 657 | return -1; |
| 658 | } |
| 659 | |
| 660 | *(f->jfield) = field; |
| 661 | } |
| 662 | |
| 663 | return 0; |
| 664 | } |
| 665 | |
| 666 | // Get all the required offsets in java class and register native functions |
| 667 | int register_android_hardware_Camera(JNIEnv *env) |
| 668 | { |
| 669 | field fields_to_find[] = { |
| 670 | { "android/hardware/Camera", "mNativeContext", "I", &fields.context }, |
Mathias Agopian | 8b13832 | 2010-04-12 16:22:15 -0700 | [diff] [blame] | 671 | { "android/view/Surface", ANDROID_VIEW_SURFACE_JNI_ID, "I", &fields.surface } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 672 | }; |
| 673 | |
| 674 | if (find_fields(env, fields_to_find, NELEM(fields_to_find)) < 0) |
| 675 | return -1; |
| 676 | |
| 677 | jclass clazz = env->FindClass("android/hardware/Camera"); |
| 678 | fields.post_event = env->GetStaticMethodID(clazz, "postEventFromNative", |
| 679 | "(Ljava/lang/Object;IIILjava/lang/Object;)V"); |
| 680 | if (fields.post_event == NULL) { |
| 681 | LOGE("Can't find android/hardware/Camera.postEventFromNative"); |
| 682 | return -1; |
| 683 | } |
| 684 | |
| 685 | |
| 686 | // Register native functions |
| 687 | return AndroidRuntime::registerNativeMethods(env, "android/hardware/Camera", |
| 688 | camMethods, NELEM(camMethods)); |
| 689 | } |
| 690 | |