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