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