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