blob: 6f433c5de3414b9d0c588fafa7c41514d5f6d78f [file] [log] [blame]
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001/*
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"
Andreas Gampeed6b9df2014-11-20 22:02:20 -080024#include "core_jni_helpers.h"
Igor Murashkinc99db2b2012-10-29 13:38:10 -070025#include <android_runtime/android_graphics_SurfaceTexture.h>
Mathias Agopian3866f0d2013-02-11 22:08:48 -080026#include <android_runtime/android_view_Surface.h>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080027
Eino-Ville Talvalaf7c6c5a2012-09-19 11:46:11 -070028#include <cutils/properties.h>
Andrew Harp94927df2009-10-20 01:47:05 -040029#include <utils/Vector.h>
Ruben Brunkfeb50af2014-05-09 19:58:49 -070030#include <utils/Errors.h>
Andrew Harp94927df2009-10-20 01:47:05 -040031
Andy McFaddend47f7d82012-12-18 09:48:38 -080032#include <gui/GLConsumer.h>
Mathias Agopian8335f1c2012-02-25 18:48:35 -080033#include <gui/Surface.h>
Mathias Agopian000479f2010-02-09 17:46:37 -080034#include <camera/Camera.h>
Mathias Agopian07952722009-05-19 19:08:10 -070035#include <binder/IMemory.h>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080036
37using namespace android;
38
Igor Murashkina1d66272014-06-20 11:22:11 -070039enum {
40 // Keep up to date with Camera.java
41 CAMERA_HAL_API_VERSION_NORMAL_CONNECT = -2,
42};
43
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080044struct fields_t {
45 jfieldID context;
Chih-Chung Changb8bb78f2010-06-10 13:32:16 +080046 jfieldID facing;
47 jfieldID orientation;
Eino-Ville Talvalaf7c6c5a2012-09-19 11:46:11 -070048 jfieldID canDisableShutterSound;
Wu-cheng Lif0d6a482011-07-28 05:30:59 +080049 jfieldID face_rect;
Wu-cheng Li4c2292e2011-07-22 02:37:11 +080050 jfieldID face_score;
51 jfieldID rect_left;
52 jfieldID rect_top;
53 jfieldID rect_right;
54 jfieldID rect_bottom;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080055 jmethodID post_event;
Wu-cheng Libb1e2752011-07-30 05:00:37 +080056 jmethodID rect_constructor;
57 jmethodID face_constructor;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080058};
59
60static fields_t fields;
61static Mutex sLock;
62
Dave Sparks5e271152009-06-23 17:30:11 -070063// provides persistent context for calls from native code to Java
64class JNICameraContext: public CameraListener
65{
66public:
67 JNICameraContext(JNIEnv* env, jobject weak_this, jclass clazz, const sp<Camera>& camera);
68 ~JNICameraContext() { release(); }
69 virtual void notify(int32_t msgType, int32_t ext1, int32_t ext2);
Wu-cheng Libb1e2752011-07-30 05:00:37 +080070 virtual void postData(int32_t msgType, const sp<IMemory>& dataPtr,
71 camera_frame_metadata_t *metadata);
Dave Sparks59c1a932009-07-08 15:56:53 -070072 virtual void postDataTimestamp(nsecs_t timestamp, int32_t msgType, const sp<IMemory>& dataPtr);
Wu-cheng Libb1e2752011-07-30 05:00:37 +080073 void postMetadata(JNIEnv *env, int32_t msgType, camera_frame_metadata_t *metadata);
James Donge00cab72011-02-17 16:38:06 -080074 void addCallbackBuffer(JNIEnv *env, jbyteArray cbb, int msgType);
Andrew Harp94927df2009-10-20 01:47:05 -040075 void setCallbackMode(JNIEnv *env, bool installed, bool manualMode);
Dave Sparks5e271152009-06-23 17:30:11 -070076 sp<Camera> getCamera() { Mutex::Autolock _l(mLock); return mCamera; }
James Donge00cab72011-02-17 16:38:06 -080077 bool isRawImageCallbackBufferAvailable() const;
Dave Sparks5e271152009-06-23 17:30:11 -070078 void release();
79
80private:
81 void copyAndPost(JNIEnv* env, const sp<IMemory>& dataPtr, int msgType);
James Donge00cab72011-02-17 16:38:06 -080082 void clearCallbackBuffers_l(JNIEnv *env, Vector<jbyteArray> *buffers);
Andrew Harp94927df2009-10-20 01:47:05 -040083 void clearCallbackBuffers_l(JNIEnv *env);
James Donge00cab72011-02-17 16:38:06 -080084 jbyteArray getCallbackBuffer(JNIEnv *env, Vector<jbyteArray> *buffers, size_t bufferSize);
Dave Sparks5e271152009-06-23 17:30:11 -070085
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080086 jobject mCameraJObjectWeak; // weak reference to java object
87 jclass mCameraJClass; // strong reference to java class
Wu-cheng Liffe1cf22009-09-10 16:49:17 +080088 sp<Camera> mCamera; // strong reference to native object
Wu-cheng Libb1e2752011-07-30 05:00:37 +080089 jclass mFaceClass; // strong reference to Face class
90 jclass mRectClass; // strong reference to Rect class
Dave Sparks5e271152009-06-23 17:30:11 -070091 Mutex mLock;
Andrew Harp94927df2009-10-20 01:47:05 -040092
James Donge00cab72011-02-17 16:38:06 -080093 /*
94 * Global reference application-managed raw image buffer queue.
95 *
96 * Manual-only mode is supported for raw image callbacks, which is
97 * set whenever method addCallbackBuffer() with msgType =
98 * CAMERA_MSG_RAW_IMAGE is called; otherwise, null is returned
99 * with raw image callbacks.
100 */
101 Vector<jbyteArray> mRawImageCallbackBuffers;
102
103 /*
104 * Application-managed preview buffer queue and the flags
105 * associated with the usage of the preview buffer callback.
106 */
Andrew Harp94927df2009-10-20 01:47:05 -0400107 Vector<jbyteArray> mCallbackBuffers; // Global reference application managed byte[]
108 bool mManualBufferMode; // Whether to use application managed buffers.
James Donge00cab72011-02-17 16:38:06 -0800109 bool mManualCameraCallbackSet; // Whether the callback has been set, used to
110 // reduce unnecessary calls to set the callback.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800111};
112
James Donge00cab72011-02-17 16:38:06 -0800113bool JNICameraContext::isRawImageCallbackBufferAvailable() const
114{
115 return !mRawImageCallbackBuffers.isEmpty();
116}
117
Dave Sparks5e271152009-06-23 17:30:11 -0700118sp<Camera> get_native_camera(JNIEnv *env, jobject thiz, JNICameraContext** pContext)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800119{
120 sp<Camera> camera;
121 Mutex::Autolock _l(sLock);
Ashok Bhat4838e332014-01-03 14:37:19 +0000122 JNICameraContext* context = reinterpret_cast<JNICameraContext*>(env->GetLongField(thiz, fields.context));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800123 if (context != NULL) {
Dave Sparks5e271152009-06-23 17:30:11 -0700124 camera = context->getCamera();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800125 }
Steve Block71f2cf12011-10-20 11:56:00 +0100126 ALOGV("get_native_camera: context=%p, camera=%p", context, camera.get());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800127 if (camera == 0) {
Elliott Hughes69a017b2011-04-08 14:10:28 -0700128 jniThrowRuntimeException(env, "Method called after release()");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800129 }
130
131 if (pContext != NULL) *pContext = context;
132 return camera;
133}
134
Dave Sparks5e271152009-06-23 17:30:11 -0700135JNICameraContext::JNICameraContext(JNIEnv* env, jobject weak_this, jclass clazz, const sp<Camera>& camera)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800136{
Dave Sparks5e271152009-06-23 17:30:11 -0700137 mCameraJObjectWeak = env->NewGlobalRef(weak_this);
138 mCameraJClass = (jclass)env->NewGlobalRef(clazz);
139 mCamera = camera;
Andrew Harp94927df2009-10-20 01:47:05 -0400140
Wu-cheng Libb1e2752011-07-30 05:00:37 +0800141 jclass faceClazz = env->FindClass("android/hardware/Camera$Face");
142 mFaceClass = (jclass) env->NewGlobalRef(faceClazz);
143
144 jclass rectClazz = env->FindClass("android/graphics/Rect");
145 mRectClass = (jclass) env->NewGlobalRef(rectClazz);
146
Andrew Harp94927df2009-10-20 01:47:05 -0400147 mManualBufferMode = false;
148 mManualCameraCallbackSet = false;
Dave Sparks5e271152009-06-23 17:30:11 -0700149}
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800150
Dave Sparks5e271152009-06-23 17:30:11 -0700151void JNICameraContext::release()
152{
Steve Block71f2cf12011-10-20 11:56:00 +0100153 ALOGV("release");
Dave Sparks5e271152009-06-23 17:30:11 -0700154 Mutex::Autolock _l(mLock);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800155 JNIEnv *env = AndroidRuntime::getJNIEnv();
Dave Sparks5e271152009-06-23 17:30:11 -0700156
157 if (mCameraJObjectWeak != NULL) {
158 env->DeleteGlobalRef(mCameraJObjectWeak);
159 mCameraJObjectWeak = NULL;
160 }
161 if (mCameraJClass != NULL) {
162 env->DeleteGlobalRef(mCameraJClass);
163 mCameraJClass = NULL;
164 }
Wu-cheng Libb1e2752011-07-30 05:00:37 +0800165 if (mFaceClass != NULL) {
166 env->DeleteGlobalRef(mFaceClass);
167 mFaceClass = NULL;
168 }
169 if (mRectClass != NULL) {
170 env->DeleteGlobalRef(mRectClass);
171 mRectClass = NULL;
172 }
Andrew Harp94927df2009-10-20 01:47:05 -0400173 clearCallbackBuffers_l(env);
Dave Sparks5e271152009-06-23 17:30:11 -0700174 mCamera.clear();
175}
176
177void JNICameraContext::notify(int32_t msgType, int32_t ext1, int32_t ext2)
178{
Steve Block71f2cf12011-10-20 11:56:00 +0100179 ALOGV("notify");
Dave Sparks5e271152009-06-23 17:30:11 -0700180
181 // VM pointer will be NULL if object is released
182 Mutex::Autolock _l(mLock);
183 if (mCameraJObjectWeak == NULL) {
Steve Block8564c8d2012-01-05 23:22:43 +0000184 ALOGW("callback on dead camera object");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800185 return;
186 }
Dave Sparks5e271152009-06-23 17:30:11 -0700187 JNIEnv *env = AndroidRuntime::getJNIEnv();
James Donge00cab72011-02-17 16:38:06 -0800188
189 /*
190 * If the notification or msgType is CAMERA_MSG_RAW_IMAGE_NOTIFY, change it
191 * to CAMERA_MSG_RAW_IMAGE since CAMERA_MSG_RAW_IMAGE_NOTIFY is not exposed
192 * to the Java app.
193 */
194 if (msgType == CAMERA_MSG_RAW_IMAGE_NOTIFY) {
195 msgType = CAMERA_MSG_RAW_IMAGE;
196 }
197
Dave Sparksc62f9bd2009-06-26 13:33:32 -0700198 env->CallStaticVoidMethod(mCameraJClass, fields.post_event,
Chih-Chung Chang6157de02009-09-24 15:29:35 -0700199 mCameraJObjectWeak, msgType, ext1, ext2, NULL);
Dave Sparks5e271152009-06-23 17:30:11 -0700200}
201
James Donge00cab72011-02-17 16:38:06 -0800202jbyteArray JNICameraContext::getCallbackBuffer(
203 JNIEnv* env, Vector<jbyteArray>* buffers, size_t bufferSize)
204{
205 jbyteArray obj = NULL;
206
207 // Vector access should be protected by lock in postData()
208 if (!buffers->isEmpty()) {
Dan Albert46d84442014-11-18 16:07:51 -0800209 ALOGV("Using callback buffer from queue of length %zu", buffers->size());
James Donge00cab72011-02-17 16:38:06 -0800210 jbyteArray globalBuffer = buffers->itemAt(0);
211 buffers->removeAt(0);
212
213 obj = (jbyteArray)env->NewLocalRef(globalBuffer);
214 env->DeleteGlobalRef(globalBuffer);
215
216 if (obj != NULL) {
217 jsize bufferLength = env->GetArrayLength(obj);
218 if ((int)bufferLength < (int)bufferSize) {
Dan Albert46d84442014-11-18 16:07:51 -0800219 ALOGE("Callback buffer was too small! Expected %zu bytes, but got %d bytes!",
James Donge00cab72011-02-17 16:38:06 -0800220 bufferSize, bufferLength);
221 env->DeleteLocalRef(obj);
222 return NULL;
223 }
224 }
225 }
226
227 return obj;
228}
229
Dave Sparks5e271152009-06-23 17:30:11 -0700230void JNICameraContext::copyAndPost(JNIEnv* env, const sp<IMemory>& dataPtr, int msgType)
231{
232 jbyteArray obj = NULL;
233
234 // allocate Java byte array and copy data
235 if (dataPtr != NULL) {
236 ssize_t offset;
237 size_t size;
238 sp<IMemoryHeap> heap = dataPtr->getMemory(&offset, &size);
Glenn Kasten2fbf25b2014-03-28 15:41:58 -0700239 ALOGV("copyAndPost: off=%zd, size=%zu", offset, size);
Dave Sparks5e271152009-06-23 17:30:11 -0700240 uint8_t *heapBase = (uint8_t*)heap->base();
241
242 if (heapBase != NULL) {
Dave Sparksc4ca4202009-07-13 09:38:20 -0700243 const jbyte* data = reinterpret_cast<const jbyte*>(heapBase + offset);
Andrew Harp94927df2009-10-20 01:47:05 -0400244
Wu-cheng Li5f0ef5f2011-09-21 16:40:23 +0800245 if (msgType == CAMERA_MSG_RAW_IMAGE) {
246 obj = getCallbackBuffer(env, &mRawImageCallbackBuffers, size);
247 } else if (msgType == CAMERA_MSG_PREVIEW_FRAME && mManualBufferMode) {
248 obj = getCallbackBuffer(env, &mCallbackBuffers, size);
Andrew Harp94927df2009-10-20 01:47:05 -0400249
Wu-cheng Li5f0ef5f2011-09-21 16:40:23 +0800250 if (mCallbackBuffers.isEmpty()) {
Steve Block71f2cf12011-10-20 11:56:00 +0100251 ALOGV("Out of buffers, clearing callback!");
Wu-cheng Li5f0ef5f2011-09-21 16:40:23 +0800252 mCamera->setPreviewCallbackFlags(CAMERA_FRAME_CALLBACK_FLAG_NOOP);
253 mManualCameraCallbackSet = false;
Andrew Harp94927df2009-10-20 01:47:05 -0400254
Wu-cheng Li5f0ef5f2011-09-21 16:40:23 +0800255 if (obj == NULL) {
Andrew Harp94927df2009-10-20 01:47:05 -0400256 return;
257 }
258 }
Wu-cheng Li5f0ef5f2011-09-21 16:40:23 +0800259 } else {
Steve Block71f2cf12011-10-20 11:56:00 +0100260 ALOGV("Allocating callback buffer");
Wu-cheng Li5f0ef5f2011-09-21 16:40:23 +0800261 obj = env->NewByteArray(size);
Andrew Harp94927df2009-10-20 01:47:05 -0400262 }
263
Dave Sparks5e271152009-06-23 17:30:11 -0700264 if (obj == NULL) {
Steve Block3762c312012-01-06 19:20:56 +0000265 ALOGE("Couldn't allocate byte array for JPEG data");
Dave Sparks5e271152009-06-23 17:30:11 -0700266 env->ExceptionClear();
267 } else {
Dave Sparksa95f4952009-07-10 18:13:36 -0700268 env->SetByteArrayRegion(obj, 0, size, data);
Dave Sparks5e271152009-06-23 17:30:11 -0700269 }
270 } else {
Steve Block3762c312012-01-06 19:20:56 +0000271 ALOGE("image heap is NULL");
Dave Sparks5e271152009-06-23 17:30:11 -0700272 }
273 }
274
275 // post image data to Java
276 env->CallStaticVoidMethod(mCameraJClass, fields.post_event,
277 mCameraJObjectWeak, msgType, 0, 0, obj);
278 if (obj) {
279 env->DeleteLocalRef(obj);
280 }
281}
282
Wu-cheng Libb1e2752011-07-30 05:00:37 +0800283void JNICameraContext::postData(int32_t msgType, const sp<IMemory>& dataPtr,
284 camera_frame_metadata_t *metadata)
Dave Sparks5e271152009-06-23 17:30:11 -0700285{
286 // VM pointer will be NULL if object is released
287 Mutex::Autolock _l(mLock);
288 JNIEnv *env = AndroidRuntime::getJNIEnv();
Dave Sparksd0cbb1a2009-06-29 19:03:33 -0700289 if (mCameraJObjectWeak == NULL) {
Steve Block8564c8d2012-01-05 23:22:43 +0000290 ALOGW("callback on dead camera object");
Dave Sparksd0cbb1a2009-06-29 19:03:33 -0700291 return;
292 }
Dave Sparks5e271152009-06-23 17:30:11 -0700293
Wu-cheng Libb1e2752011-07-30 05:00:37 +0800294 int32_t dataMsgType = msgType & ~CAMERA_MSG_PREVIEW_METADATA;
295
Dave Sparks5e271152009-06-23 17:30:11 -0700296 // return data based on callback type
Wu-cheng Libb1e2752011-07-30 05:00:37 +0800297 switch (dataMsgType) {
James Donge00cab72011-02-17 16:38:06 -0800298 case CAMERA_MSG_VIDEO_FRAME:
299 // should never happen
300 break;
301
302 // For backward-compatibility purpose, if there is no callback
303 // buffer for raw image, the callback returns null.
304 case CAMERA_MSG_RAW_IMAGE:
Steve Block71f2cf12011-10-20 11:56:00 +0100305 ALOGV("rawCallback");
James Donge00cab72011-02-17 16:38:06 -0800306 if (mRawImageCallbackBuffers.isEmpty()) {
307 env->CallStaticVoidMethod(mCameraJClass, fields.post_event,
Wu-cheng Libb1e2752011-07-30 05:00:37 +0800308 mCameraJObjectWeak, dataMsgType, 0, 0, NULL);
James Donge00cab72011-02-17 16:38:06 -0800309 } else {
Wu-cheng Libb1e2752011-07-30 05:00:37 +0800310 copyAndPost(env, dataPtr, dataMsgType);
James Donge00cab72011-02-17 16:38:06 -0800311 }
312 break;
313
Wu-cheng Libb1e2752011-07-30 05:00:37 +0800314 // There is no data.
315 case 0:
James Donge00cab72011-02-17 16:38:06 -0800316 break;
Wu-cheng Libb1e2752011-07-30 05:00:37 +0800317
318 default:
Steve Block71f2cf12011-10-20 11:56:00 +0100319 ALOGV("dataCallback(%d, %p)", dataMsgType, dataPtr.get());
Wu-cheng Libb1e2752011-07-30 05:00:37 +0800320 copyAndPost(env, dataPtr, dataMsgType);
321 break;
322 }
323
324 // post frame metadata to Java
325 if (metadata && (msgType & CAMERA_MSG_PREVIEW_METADATA)) {
326 postMetadata(env, CAMERA_MSG_PREVIEW_METADATA, metadata);
Dave Sparks5e271152009-06-23 17:30:11 -0700327 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800328}
329
Dave Sparks59c1a932009-07-08 15:56:53 -0700330void JNICameraContext::postDataTimestamp(nsecs_t timestamp, int32_t msgType, const sp<IMemory>& dataPtr)
331{
332 // TODO: plumb up to Java. For now, just drop the timestamp
Wu-cheng Libb1e2752011-07-30 05:00:37 +0800333 postData(msgType, dataPtr, NULL);
334}
335
336void JNICameraContext::postMetadata(JNIEnv *env, int32_t msgType, camera_frame_metadata_t *metadata)
337{
338 jobjectArray obj = NULL;
339 obj = (jobjectArray) env->NewObjectArray(metadata->number_of_faces,
340 mFaceClass, NULL);
341 if (obj == NULL) {
Steve Block3762c312012-01-06 19:20:56 +0000342 ALOGE("Couldn't allocate face metadata array");
Wu-cheng Libb1e2752011-07-30 05:00:37 +0800343 return;
344 }
345
346 for (int i = 0; i < metadata->number_of_faces; i++) {
347 jobject face = env->NewObject(mFaceClass, fields.face_constructor);
348 env->SetObjectArrayElement(obj, i, face);
349
350 jobject rect = env->NewObject(mRectClass, fields.rect_constructor);
351 env->SetIntField(rect, fields.rect_left, metadata->faces[i].rect[0]);
352 env->SetIntField(rect, fields.rect_top, metadata->faces[i].rect[1]);
353 env->SetIntField(rect, fields.rect_right, metadata->faces[i].rect[2]);
354 env->SetIntField(rect, fields.rect_bottom, metadata->faces[i].rect[3]);
355
356 env->SetObjectField(face, fields.face_rect, rect);
357 env->SetIntField(face, fields.face_score, metadata->faces[i].score);
358
359 env->DeleteLocalRef(face);
360 env->DeleteLocalRef(rect);
361 }
362 env->CallStaticVoidMethod(mCameraJClass, fields.post_event,
363 mCameraJObjectWeak, msgType, 0, 0, obj);
364 env->DeleteLocalRef(obj);
Dave Sparks59c1a932009-07-08 15:56:53 -0700365}
366
Andrew Harp94927df2009-10-20 01:47:05 -0400367void JNICameraContext::setCallbackMode(JNIEnv *env, bool installed, bool manualMode)
368{
369 Mutex::Autolock _l(mLock);
370 mManualBufferMode = manualMode;
371 mManualCameraCallbackSet = false;
372
373 // In order to limit the over usage of binder threads, all non-manual buffer
Iliyan Malchev9c7ac0d2011-04-14 16:51:21 -0700374 // callbacks use CAMERA_FRAME_CALLBACK_FLAG_BARCODE_SCANNER mode now.
Andrew Harp94927df2009-10-20 01:47:05 -0400375 //
376 // Continuous callbacks will have the callback re-registered from handleMessage.
377 // Manual buffer mode will operate as fast as possible, relying on the finite supply
378 // of buffers for throttling.
379
380 if (!installed) {
Iliyan Malchev9c7ac0d2011-04-14 16:51:21 -0700381 mCamera->setPreviewCallbackFlags(CAMERA_FRAME_CALLBACK_FLAG_NOOP);
James Donge00cab72011-02-17 16:38:06 -0800382 clearCallbackBuffers_l(env, &mCallbackBuffers);
Andrew Harp94927df2009-10-20 01:47:05 -0400383 } else if (mManualBufferMode) {
384 if (!mCallbackBuffers.isEmpty()) {
Iliyan Malchev9c7ac0d2011-04-14 16:51:21 -0700385 mCamera->setPreviewCallbackFlags(CAMERA_FRAME_CALLBACK_FLAG_CAMERA);
Andrew Harp94927df2009-10-20 01:47:05 -0400386 mManualCameraCallbackSet = true;
387 }
388 } else {
Iliyan Malchev9c7ac0d2011-04-14 16:51:21 -0700389 mCamera->setPreviewCallbackFlags(CAMERA_FRAME_CALLBACK_FLAG_BARCODE_SCANNER);
James Donge00cab72011-02-17 16:38:06 -0800390 clearCallbackBuffers_l(env, &mCallbackBuffers);
Andrew Harp94927df2009-10-20 01:47:05 -0400391 }
392}
393
James Donge00cab72011-02-17 16:38:06 -0800394void JNICameraContext::addCallbackBuffer(
395 JNIEnv *env, jbyteArray cbb, int msgType)
Andrew Harp94927df2009-10-20 01:47:05 -0400396{
Steve Block71f2cf12011-10-20 11:56:00 +0100397 ALOGV("addCallbackBuffer: 0x%x", msgType);
Andrew Harp94927df2009-10-20 01:47:05 -0400398 if (cbb != NULL) {
399 Mutex::Autolock _l(mLock);
James Donge00cab72011-02-17 16:38:06 -0800400 switch (msgType) {
401 case CAMERA_MSG_PREVIEW_FRAME: {
402 jbyteArray callbackBuffer = (jbyteArray)env->NewGlobalRef(cbb);
403 mCallbackBuffers.push(callbackBuffer);
Andrew Harp94927df2009-10-20 01:47:05 -0400404
Dan Albert46d84442014-11-18 16:07:51 -0800405 ALOGV("Adding callback buffer to queue, %zu total",
James Donge00cab72011-02-17 16:38:06 -0800406 mCallbackBuffers.size());
Andrew Harp94927df2009-10-20 01:47:05 -0400407
James Donge00cab72011-02-17 16:38:06 -0800408 // We want to make sure the camera knows we're ready for the
409 // next frame. This may have come unset had we not had a
410 // callbackbuffer ready for it last time.
411 if (mManualBufferMode && !mManualCameraCallbackSet) {
Iliyan Malchev9c7ac0d2011-04-14 16:51:21 -0700412 mCamera->setPreviewCallbackFlags(CAMERA_FRAME_CALLBACK_FLAG_CAMERA);
James Donge00cab72011-02-17 16:38:06 -0800413 mManualCameraCallbackSet = true;
414 }
415 break;
416 }
417 case CAMERA_MSG_RAW_IMAGE: {
418 jbyteArray callbackBuffer = (jbyteArray)env->NewGlobalRef(cbb);
419 mRawImageCallbackBuffers.push(callbackBuffer);
420 break;
421 }
422 default: {
423 jniThrowException(env,
424 "java/lang/IllegalArgumentException",
425 "Unsupported message type");
426 return;
427 }
Andrew Harp94927df2009-10-20 01:47:05 -0400428 }
429 } else {
Steve Block3762c312012-01-06 19:20:56 +0000430 ALOGE("Null byte array!");
Andrew Harp94927df2009-10-20 01:47:05 -0400431 }
432}
433
434void JNICameraContext::clearCallbackBuffers_l(JNIEnv *env)
435{
James Donge00cab72011-02-17 16:38:06 -0800436 clearCallbackBuffers_l(env, &mCallbackBuffers);
437 clearCallbackBuffers_l(env, &mRawImageCallbackBuffers);
438}
439
440void JNICameraContext::clearCallbackBuffers_l(JNIEnv *env, Vector<jbyteArray> *buffers) {
Dan Albert46d84442014-11-18 16:07:51 -0800441 ALOGV("Clearing callback buffers, %zu remained", buffers->size());
James Donge00cab72011-02-17 16:38:06 -0800442 while (!buffers->isEmpty()) {
443 env->DeleteGlobalRef(buffers->top());
444 buffers->pop();
Andrew Harp94927df2009-10-20 01:47:05 -0400445 }
446}
447
Chih-Chung Change25cc652010-05-06 16:36:58 +0800448static jint android_hardware_Camera_getNumberOfCameras(JNIEnv *env, jobject thiz)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800449{
Chih-Chung Change25cc652010-05-06 16:36:58 +0800450 return Camera::getNumberOfCameras();
451}
452
Chih-Chung Changb8bb78f2010-06-10 13:32:16 +0800453static void android_hardware_Camera_getCameraInfo(JNIEnv *env, jobject thiz,
454 jint cameraId, jobject info_obj)
455{
456 CameraInfo cameraInfo;
457 status_t rc = Camera::getCameraInfo(cameraId, &cameraInfo);
458 if (rc != NO_ERROR) {
Elliott Hughes69a017b2011-04-08 14:10:28 -0700459 jniThrowRuntimeException(env, "Fail to get camera info");
Chih-Chung Changb8bb78f2010-06-10 13:32:16 +0800460 return;
461 }
462 env->SetIntField(info_obj, fields.facing, cameraInfo.facing);
463 env->SetIntField(info_obj, fields.orientation, cameraInfo.orientation);
Eino-Ville Talvalaf7c6c5a2012-09-19 11:46:11 -0700464
465 char value[PROPERTY_VALUE_MAX];
466 property_get("ro.camera.sound.forced", value, "0");
467 jboolean canDisableShutterSound = (strncmp(value, "0", 2) == 0);
468 env->SetBooleanField(info_obj, fields.canDisableShutterSound,
469 canDisableShutterSound);
Chih-Chung Changb8bb78f2010-06-10 13:32:16 +0800470}
471
Chih-Chung Change25cc652010-05-06 16:36:58 +0800472// connect to camera service
Ruben Brunkfeb50af2014-05-09 19:58:49 -0700473static jint android_hardware_Camera_native_setup(JNIEnv *env, jobject thiz,
Zhijun He4c913802014-06-16 16:42:35 -0700474 jobject weak_this, jint cameraId, jint halVersion, jstring clientPackageName)
Chih-Chung Change25cc652010-05-06 16:36:58 +0800475{
Eino-Ville Talvala788717c2013-02-15 18:30:15 -0800476 // Convert jstring to String16
Dan Albert66987492014-11-20 11:41:21 -0800477 const char16_t *rawClientName = reinterpret_cast<const char16_t*>(
478 env->GetStringChars(clientPackageName, NULL));
Eino-Ville Talvala788717c2013-02-15 18:30:15 -0800479 jsize rawClientNameLen = env->GetStringLength(clientPackageName);
480 String16 clientName(rawClientName, rawClientNameLen);
Dan Albert66987492014-11-20 11:41:21 -0800481 env->ReleaseStringChars(clientPackageName,
482 reinterpret_cast<const jchar*>(rawClientName));
Eino-Ville Talvala788717c2013-02-15 18:30:15 -0800483
Zhijun He4c913802014-06-16 16:42:35 -0700484 sp<Camera> camera;
Igor Murashkina1d66272014-06-20 11:22:11 -0700485 if (halVersion == CAMERA_HAL_API_VERSION_NORMAL_CONNECT) {
486 // Default path: hal version is don't care, do normal camera connect.
Zhijun He4c913802014-06-16 16:42:35 -0700487 camera = Camera::connect(cameraId, clientName,
488 Camera::USE_CALLING_UID);
489 } else {
490 jint status = Camera::connectLegacy(cameraId, halVersion, clientName,
491 Camera::USE_CALLING_UID, camera);
492 if (status != NO_ERROR) {
493 return status;
494 }
495 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800496
497 if (camera == NULL) {
Ruben Brunkfeb50af2014-05-09 19:58:49 -0700498 return -EACCES;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800499 }
500
501 // make sure camera hardware is alive
502 if (camera->getStatus() != NO_ERROR) {
Ruben Brunkfeb50af2014-05-09 19:58:49 -0700503 return NO_INIT;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800504 }
505
506 jclass clazz = env->GetObjectClass(thiz);
507 if (clazz == NULL) {
Ruben Brunkfeb50af2014-05-09 19:58:49 -0700508 // This should never happen
Elliott Hughes69a017b2011-04-08 14:10:28 -0700509 jniThrowRuntimeException(env, "Can't find android/hardware/Camera");
Ruben Brunkfeb50af2014-05-09 19:58:49 -0700510 return INVALID_OPERATION;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800511 }
512
513 // We use a weak reference so the Camera object can be garbage collected.
514 // The reference is only used as a proxy for callbacks.
Dave Sparks5e271152009-06-23 17:30:11 -0700515 sp<JNICameraContext> context = new JNICameraContext(env, weak_this, clazz, camera);
Mathias Agopianb1d90c82013-03-06 17:45:42 -0800516 context->incStrong((void*)android_hardware_Camera_native_setup);
Dave Sparks5e271152009-06-23 17:30:11 -0700517 camera->setListener(context);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800518
519 // save context in opaque field
Ashok Bhat4838e332014-01-03 14:37:19 +0000520 env->SetLongField(thiz, fields.context, (jlong)context.get());
Ruben Brunkfeb50af2014-05-09 19:58:49 -0700521 return NO_ERROR;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800522}
523
524// disconnect from camera service
525// It's okay to call this when the native camera context is already null.
526// This handles the case where the user has called release() and the
527// finalizer is invoked later.
528static void android_hardware_Camera_release(JNIEnv *env, jobject thiz)
529{
Steve Block71f2cf12011-10-20 11:56:00 +0100530 ALOGV("release camera");
Dave Sparks5e271152009-06-23 17:30:11 -0700531 JNICameraContext* context = NULL;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800532 sp<Camera> camera;
533 {
534 Mutex::Autolock _l(sLock);
Ashok Bhat4838e332014-01-03 14:37:19 +0000535 context = reinterpret_cast<JNICameraContext*>(env->GetLongField(thiz, fields.context));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800536
537 // Make sure we do not attempt to callback on a deleted Java object.
Ashok Bhat4838e332014-01-03 14:37:19 +0000538 env->SetLongField(thiz, fields.context, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800539 }
540
541 // clean up if release has not been called before
542 if (context != NULL) {
Dave Sparks5e271152009-06-23 17:30:11 -0700543 camera = context->getCamera();
544 context->release();
Steve Block71f2cf12011-10-20 11:56:00 +0100545 ALOGV("native_release: context=%p camera=%p", context, camera.get());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800546
547 // clear callbacks
548 if (camera != NULL) {
Iliyan Malchev9c7ac0d2011-04-14 16:51:21 -0700549 camera->setPreviewCallbackFlags(CAMERA_FRAME_CALLBACK_FLAG_NOOP);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800550 camera->disconnect();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800551 }
552
553 // remove context to prevent further Java access
Mathias Agopianb1d90c82013-03-06 17:45:42 -0800554 context->decStrong((void*)android_hardware_Camera_native_setup);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800555 }
556}
557
Ruben Brunkfeb50af2014-05-09 19:58:49 -0700558static void android_hardware_Camera_setPreviewSurface(JNIEnv *env, jobject thiz, jobject jSurface)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800559{
Ruben Brunkfeb50af2014-05-09 19:58:49 -0700560 ALOGV("setPreviewSurface");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800561 sp<Camera> camera = get_native_camera(env, thiz, NULL);
562 if (camera == 0) return;
563
Mathias Agopian4a05f432013-03-12 18:43:34 -0700564 sp<IGraphicBufferProducer> gbp;
Jesse Hallaa70f222013-02-21 15:06:27 -0800565 sp<Surface> surface;
566 if (jSurface) {
567 surface = android_view_Surface_getSurface(env, jSurface);
Mathias Agopian4a05f432013-03-12 18:43:34 -0700568 if (surface != NULL) {
569 gbp = surface->getIGraphicBufferProducer();
570 }
Jesse Hallaa70f222013-02-21 15:06:27 -0800571 }
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800572
Eino-Ville Talvala7b297792013-08-21 14:39:22 -0700573 if (camera->setPreviewTarget(gbp) != NO_ERROR) {
Mathias Agopian4a05f432013-03-12 18:43:34 -0700574 jniThrowException(env, "java/io/IOException", "setPreviewTexture failed");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800575 }
576}
577
Jamie Gennisfd6f39e2010-12-20 12:15:00 -0800578static void android_hardware_Camera_setPreviewTexture(JNIEnv *env,
579 jobject thiz, jobject jSurfaceTexture)
580{
Steve Block71f2cf12011-10-20 11:56:00 +0100581 ALOGV("setPreviewTexture");
Jamie Gennisfd6f39e2010-12-20 12:15:00 -0800582 sp<Camera> camera = get_native_camera(env, thiz, NULL);
583 if (camera == 0) return;
584
Mathias Agopian52a9a102013-08-02 01:38:38 -0700585 sp<IGraphicBufferProducer> producer = NULL;
Jamie Gennisfd6f39e2010-12-20 12:15:00 -0800586 if (jSurfaceTexture != NULL) {
Mathias Agopian52a9a102013-08-02 01:38:38 -0700587 producer = SurfaceTexture_getProducer(env, jSurfaceTexture);
588 if (producer == NULL) {
Daniel Lam2e76c992012-02-23 14:35:13 -0800589 jniThrowException(env, "java/lang/IllegalArgumentException",
590 "SurfaceTexture already released in setPreviewTexture");
591 return;
592 }
593
Jamie Gennisfd6f39e2010-12-20 12:15:00 -0800594 }
Daniel Lam2e76c992012-02-23 14:35:13 -0800595
Eino-Ville Talvala7b297792013-08-21 14:39:22 -0700596 if (camera->setPreviewTarget(producer) != NO_ERROR) {
Jamie Gennisfd6f39e2010-12-20 12:15:00 -0800597 jniThrowException(env, "java/io/IOException",
598 "setPreviewTexture failed");
599 }
600}
601
Eino-Ville Talvala7005b672013-04-02 15:46:38 -0700602static void android_hardware_Camera_setPreviewCallbackSurface(JNIEnv *env,
603 jobject thiz, jobject jSurface)
604{
605 ALOGV("setPreviewCallbackSurface");
606 JNICameraContext* context;
607 sp<Camera> camera = get_native_camera(env, thiz, &context);
608 if (camera == 0) return;
609
610 sp<IGraphicBufferProducer> gbp;
611 sp<Surface> surface;
612 if (jSurface) {
613 surface = android_view_Surface_getSurface(env, jSurface);
614 if (surface != NULL) {
615 gbp = surface->getIGraphicBufferProducer();
616 }
617 }
618 // Clear out normal preview callbacks
619 context->setCallbackMode(env, false, false);
620 // Then set up callback surface
621 if (camera->setPreviewCallbackTarget(gbp) != NO_ERROR) {
622 jniThrowException(env, "java/io/IOException", "setPreviewCallbackTarget failed");
623 }
624}
625
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800626static void android_hardware_Camera_startPreview(JNIEnv *env, jobject thiz)
627{
Steve Block71f2cf12011-10-20 11:56:00 +0100628 ALOGV("startPreview");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800629 sp<Camera> camera = get_native_camera(env, thiz, NULL);
630 if (camera == 0) return;
631
632 if (camera->startPreview() != NO_ERROR) {
Elliott Hughes69a017b2011-04-08 14:10:28 -0700633 jniThrowRuntimeException(env, "startPreview failed");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800634 return;
635 }
636}
637
638static void android_hardware_Camera_stopPreview(JNIEnv *env, jobject thiz)
639{
Steve Block71f2cf12011-10-20 11:56:00 +0100640 ALOGV("stopPreview");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800641 sp<Camera> c = get_native_camera(env, thiz, NULL);
642 if (c == 0) return;
643
644 c->stopPreview();
645}
646
Ashok Bhat4838e332014-01-03 14:37:19 +0000647static jboolean android_hardware_Camera_previewEnabled(JNIEnv *env, jobject thiz)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800648{
Steve Block71f2cf12011-10-20 11:56:00 +0100649 ALOGV("previewEnabled");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800650 sp<Camera> c = get_native_camera(env, thiz, NULL);
Ashok Bhat4838e332014-01-03 14:37:19 +0000651 if (c == 0) return JNI_FALSE;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800652
Ashok Bhat4838e332014-01-03 14:37:19 +0000653 return c->previewEnabled() ? JNI_TRUE : JNI_FALSE;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800654}
655
Andrew Harp94927df2009-10-20 01:47:05 -0400656static void android_hardware_Camera_setHasPreviewCallback(JNIEnv *env, jobject thiz, jboolean installed, jboolean manualBuffer)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800657{
Steve Block71f2cf12011-10-20 11:56:00 +0100658 ALOGV("setHasPreviewCallback: installed:%d, manualBuffer:%d", (int)installed, (int)manualBuffer);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800659 // Important: Only install preview_callback if the Java code has called
660 // setPreviewCallback() with a non-null value, otherwise we'd pay to memcpy
661 // each preview frame for nothing.
Dave Sparks5e271152009-06-23 17:30:11 -0700662 JNICameraContext* context;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800663 sp<Camera> camera = get_native_camera(env, thiz, &context);
664 if (camera == 0) return;
665
Andrew Harp94927df2009-10-20 01:47:05 -0400666 // setCallbackMode will take care of setting the context flags and calling
667 // camera->setPreviewCallbackFlags within a mutex for us.
668 context->setCallbackMode(env, installed, manualBuffer);
669}
670
Ashok Bhat4838e332014-01-03 14:37:19 +0000671static void android_hardware_Camera_addCallbackBuffer(JNIEnv *env, jobject thiz, jbyteArray bytes, jint msgType) {
Steve Block71f2cf12011-10-20 11:56:00 +0100672 ALOGV("addCallbackBuffer: 0x%x", msgType);
Andrew Harp94927df2009-10-20 01:47:05 -0400673
Ashok Bhat4838e332014-01-03 14:37:19 +0000674 JNICameraContext* context = reinterpret_cast<JNICameraContext*>(env->GetLongField(thiz, fields.context));
Andrew Harp94927df2009-10-20 01:47:05 -0400675
676 if (context != NULL) {
James Donge00cab72011-02-17 16:38:06 -0800677 context->addCallbackBuffer(env, bytes, msgType);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800678 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800679}
680
681static void android_hardware_Camera_autoFocus(JNIEnv *env, jobject thiz)
682{
Steve Block71f2cf12011-10-20 11:56:00 +0100683 ALOGV("autoFocus");
Dave Sparks5e271152009-06-23 17:30:11 -0700684 JNICameraContext* context;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800685 sp<Camera> c = get_native_camera(env, thiz, &context);
686 if (c == 0) return;
687
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800688 if (c->autoFocus() != NO_ERROR) {
Elliott Hughes69a017b2011-04-08 14:10:28 -0700689 jniThrowRuntimeException(env, "autoFocus failed");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800690 }
691}
692
Chih-Chung Chang244f8c22009-09-15 14:51:56 +0800693static void android_hardware_Camera_cancelAutoFocus(JNIEnv *env, jobject thiz)
694{
Steve Block71f2cf12011-10-20 11:56:00 +0100695 ALOGV("cancelAutoFocus");
Chih-Chung Chang244f8c22009-09-15 14:51:56 +0800696 JNICameraContext* context;
697 sp<Camera> c = get_native_camera(env, thiz, &context);
698 if (c == 0) return;
699
700 if (c->cancelAutoFocus() != NO_ERROR) {
Elliott Hughes69a017b2011-04-08 14:10:28 -0700701 jniThrowRuntimeException(env, "cancelAutoFocus failed");
Chih-Chung Chang244f8c22009-09-15 14:51:56 +0800702 }
703}
704
Ashok Bhat4838e332014-01-03 14:37:19 +0000705static void android_hardware_Camera_takePicture(JNIEnv *env, jobject thiz, jint msgType)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800706{
Steve Block71f2cf12011-10-20 11:56:00 +0100707 ALOGV("takePicture");
Dave Sparks5e271152009-06-23 17:30:11 -0700708 JNICameraContext* context;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800709 sp<Camera> camera = get_native_camera(env, thiz, &context);
710 if (camera == 0) return;
711
James Donge00cab72011-02-17 16:38:06 -0800712 /*
713 * When CAMERA_MSG_RAW_IMAGE is requested, if the raw image callback
714 * buffer is available, CAMERA_MSG_RAW_IMAGE is enabled to get the
715 * notification _and_ the data; otherwise, CAMERA_MSG_RAW_IMAGE_NOTIFY
716 * is enabled to receive the callback notification but no data.
717 *
718 * Note that CAMERA_MSG_RAW_IMAGE_NOTIFY is not exposed to the
719 * Java application.
720 */
721 if (msgType & CAMERA_MSG_RAW_IMAGE) {
Steve Block71f2cf12011-10-20 11:56:00 +0100722 ALOGV("Enable raw image callback buffer");
James Donge00cab72011-02-17 16:38:06 -0800723 if (!context->isRawImageCallbackBufferAvailable()) {
Steve Block71f2cf12011-10-20 11:56:00 +0100724 ALOGV("Enable raw image notification, since no callback buffer exists");
James Donge00cab72011-02-17 16:38:06 -0800725 msgType &= ~CAMERA_MSG_RAW_IMAGE;
726 msgType |= CAMERA_MSG_RAW_IMAGE_NOTIFY;
727 }
728 }
729
730 if (camera->takePicture(msgType) != NO_ERROR) {
Elliott Hughes69a017b2011-04-08 14:10:28 -0700731 jniThrowRuntimeException(env, "takePicture failed");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800732 return;
733 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800734}
735
736static void android_hardware_Camera_setParameters(JNIEnv *env, jobject thiz, jstring params)
737{
Steve Block71f2cf12011-10-20 11:56:00 +0100738 ALOGV("setParameters");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800739 sp<Camera> camera = get_native_camera(env, thiz, NULL);
740 if (camera == 0) return;
741
742 const jchar* str = env->GetStringCritical(params, 0);
743 String8 params8;
744 if (params) {
Dan Albert66987492014-11-20 11:41:21 -0800745 params8 = String8(reinterpret_cast<const char16_t*>(str),
746 env->GetStringLength(params));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800747 env->ReleaseStringCritical(params, str);
748 }
749 if (camera->setParameters(params8) != NO_ERROR) {
Elliott Hughes69a017b2011-04-08 14:10:28 -0700750 jniThrowRuntimeException(env, "setParameters failed");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800751 return;
752 }
753}
754
755static jstring android_hardware_Camera_getParameters(JNIEnv *env, jobject thiz)
756{
Steve Block71f2cf12011-10-20 11:56:00 +0100757 ALOGV("getParameters");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800758 sp<Camera> camera = get_native_camera(env, thiz, NULL);
759 if (camera == 0) return 0;
760
Wu-cheng Lia1c41e12012-02-23 19:01:00 -0800761 String8 params8 = camera->getParameters();
762 if (params8.isEmpty()) {
763 jniThrowRuntimeException(env, "getParameters failed (empty parameters)");
764 return 0;
765 }
766 return env->NewStringUTF(params8.string());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800767}
768
769static void android_hardware_Camera_reconnect(JNIEnv *env, jobject thiz)
770{
Steve Block71f2cf12011-10-20 11:56:00 +0100771 ALOGV("reconnect");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800772 sp<Camera> camera = get_native_camera(env, thiz, NULL);
773 if (camera == 0) return;
774
775 if (camera->reconnect() != NO_ERROR) {
776 jniThrowException(env, "java/io/IOException", "reconnect failed");
777 return;
778 }
779}
780
Wu-cheng Liffe1cf22009-09-10 16:49:17 +0800781static void android_hardware_Camera_lock(JNIEnv *env, jobject thiz)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800782{
Steve Block71f2cf12011-10-20 11:56:00 +0100783 ALOGV("lock");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800784 sp<Camera> camera = get_native_camera(env, thiz, NULL);
Wu-cheng Liffe1cf22009-09-10 16:49:17 +0800785 if (camera == 0) return;
786
787 if (camera->lock() != NO_ERROR) {
Elliott Hughes69a017b2011-04-08 14:10:28 -0700788 jniThrowRuntimeException(env, "lock failed");
Wu-cheng Liffe1cf22009-09-10 16:49:17 +0800789 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800790}
791
Wu-cheng Liffe1cf22009-09-10 16:49:17 +0800792static void android_hardware_Camera_unlock(JNIEnv *env, jobject thiz)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800793{
Steve Block71f2cf12011-10-20 11:56:00 +0100794 ALOGV("unlock");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800795 sp<Camera> camera = get_native_camera(env, thiz, NULL);
Wu-cheng Liffe1cf22009-09-10 16:49:17 +0800796 if (camera == 0) return;
797
798 if (camera->unlock() != NO_ERROR) {
Elliott Hughes69a017b2011-04-08 14:10:28 -0700799 jniThrowRuntimeException(env, "unlock failed");
Wu-cheng Liffe1cf22009-09-10 16:49:17 +0800800 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800801}
802
Wu-cheng Li36f68b82009-09-28 16:14:58 -0700803static void android_hardware_Camera_startSmoothZoom(JNIEnv *env, jobject thiz, jint value)
804{
Steve Block71f2cf12011-10-20 11:56:00 +0100805 ALOGV("startSmoothZoom");
Wu-cheng Li36f68b82009-09-28 16:14:58 -0700806 sp<Camera> camera = get_native_camera(env, thiz, NULL);
807 if (camera == 0) return;
808
Wu-cheng Li0ca25192010-03-29 16:21:12 +0800809 status_t rc = camera->sendCommand(CAMERA_CMD_START_SMOOTH_ZOOM, value, 0);
810 if (rc == BAD_VALUE) {
811 char msg[64];
812 sprintf(msg, "invalid zoom value=%d", value);
813 jniThrowException(env, "java/lang/IllegalArgumentException", msg);
814 } else if (rc != NO_ERROR) {
Elliott Hughes69a017b2011-04-08 14:10:28 -0700815 jniThrowRuntimeException(env, "start smooth zoom failed");
Wu-cheng Li36f68b82009-09-28 16:14:58 -0700816 }
817}
818
819static void android_hardware_Camera_stopSmoothZoom(JNIEnv *env, jobject thiz)
820{
Steve Block71f2cf12011-10-20 11:56:00 +0100821 ALOGV("stopSmoothZoom");
Wu-cheng Li36f68b82009-09-28 16:14:58 -0700822 sp<Camera> camera = get_native_camera(env, thiz, NULL);
823 if (camera == 0) return;
824
825 if (camera->sendCommand(CAMERA_CMD_STOP_SMOOTH_ZOOM, 0, 0) != NO_ERROR) {
Elliott Hughes69a017b2011-04-08 14:10:28 -0700826 jniThrowRuntimeException(env, "stop smooth zoom failed");
Wu-cheng Li36f68b82009-09-28 16:14:58 -0700827 }
828}
829
Chih-Chung Changd1d77062010-01-22 17:49:48 -0800830static void android_hardware_Camera_setDisplayOrientation(JNIEnv *env, jobject thiz,
831 jint value)
832{
Steve Block71f2cf12011-10-20 11:56:00 +0100833 ALOGV("setDisplayOrientation");
Chih-Chung Changd1d77062010-01-22 17:49:48 -0800834 sp<Camera> camera = get_native_camera(env, thiz, NULL);
835 if (camera == 0) return;
836
837 if (camera->sendCommand(CAMERA_CMD_SET_DISPLAY_ORIENTATION, value, 0) != NO_ERROR) {
Elliott Hughes69a017b2011-04-08 14:10:28 -0700838 jniThrowRuntimeException(env, "set display orientation failed");
Chih-Chung Changd1d77062010-01-22 17:49:48 -0800839 }
840}
841
Eino-Ville Talvala69fe5272012-09-07 12:26:40 -0700842static jboolean android_hardware_Camera_enableShutterSound(JNIEnv *env, jobject thiz,
843 jboolean enabled)
844{
845 ALOGV("enableShutterSound");
846 sp<Camera> camera = get_native_camera(env, thiz, NULL);
847 if (camera == 0) return JNI_FALSE;
848
849 int32_t value = (enabled == JNI_TRUE) ? 1 : 0;
850 status_t rc = camera->sendCommand(CAMERA_CMD_ENABLE_SHUTTER_SOUND, value, 0);
851 if (rc == NO_ERROR) {
852 return JNI_TRUE;
853 } else if (rc == PERMISSION_DENIED) {
854 return JNI_FALSE;
855 } else {
856 jniThrowRuntimeException(env, "enable shutter sound failed");
857 return JNI_FALSE;
858 }
859}
860
Wu-cheng Li4c2292e2011-07-22 02:37:11 +0800861static void android_hardware_Camera_startFaceDetection(JNIEnv *env, jobject thiz,
Wu-cheng Libb1e2752011-07-30 05:00:37 +0800862 jint type)
Wu-cheng Li4c2292e2011-07-22 02:37:11 +0800863{
Steve Block71f2cf12011-10-20 11:56:00 +0100864 ALOGV("startFaceDetection");
Wu-cheng Li4c2292e2011-07-22 02:37:11 +0800865 JNICameraContext* context;
866 sp<Camera> camera = get_native_camera(env, thiz, &context);
867 if (camera == 0) return;
868
869 status_t rc = camera->sendCommand(CAMERA_CMD_START_FACE_DETECTION, type, 0);
870 if (rc == BAD_VALUE) {
871 char msg[64];
872 snprintf(msg, sizeof(msg), "invalid face detection type=%d", type);
873 jniThrowException(env, "java/lang/IllegalArgumentException", msg);
874 } else if (rc != NO_ERROR) {
875 jniThrowRuntimeException(env, "start face detection failed");
876 }
877}
878
879static void android_hardware_Camera_stopFaceDetection(JNIEnv *env, jobject thiz)
880{
Steve Block71f2cf12011-10-20 11:56:00 +0100881 ALOGV("stopFaceDetection");
Wu-cheng Li4c2292e2011-07-22 02:37:11 +0800882 sp<Camera> camera = get_native_camera(env, thiz, NULL);
883 if (camera == 0) return;
884
885 if (camera->sendCommand(CAMERA_CMD_STOP_FACE_DETECTION, 0, 0) != NO_ERROR) {
886 jniThrowRuntimeException(env, "stop face detection failed");
887 }
888}
889
Wu-cheng Li9d062cf2011-11-14 20:30:14 +0800890static void android_hardware_Camera_enableFocusMoveCallback(JNIEnv *env, jobject thiz, jint enable)
891{
Wu-cheng Li02097522011-11-30 11:06:04 +0800892 ALOGV("enableFocusMoveCallback");
Wu-cheng Li9d062cf2011-11-14 20:30:14 +0800893 sp<Camera> camera = get_native_camera(env, thiz, NULL);
894 if (camera == 0) return;
895
896 if (camera->sendCommand(CAMERA_CMD_ENABLE_FOCUS_MOVE_MSG, enable, 0) != NO_ERROR) {
897 jniThrowRuntimeException(env, "enable focus move callback failed");
898 }
899}
900
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800901//-------------------------------------------------
902
903static JNINativeMethod camMethods[] = {
Chih-Chung Change25cc652010-05-06 16:36:58 +0800904 { "getNumberOfCameras",
905 "()I",
906 (void *)android_hardware_Camera_getNumberOfCameras },
Eino-Ville Talvala4f8e5ce2012-10-08 18:16:35 -0700907 { "_getCameraInfo",
Chih-Chung Changb8bb78f2010-06-10 13:32:16 +0800908 "(ILandroid/hardware/Camera$CameraInfo;)V",
909 (void*)android_hardware_Camera_getCameraInfo },
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800910 { "native_setup",
Zhijun He4c913802014-06-16 16:42:35 -0700911 "(Ljava/lang/Object;IILjava/lang/String;)I",
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800912 (void*)android_hardware_Camera_native_setup },
913 { "native_release",
914 "()V",
915 (void*)android_hardware_Camera_release },
Ruben Brunkfeb50af2014-05-09 19:58:49 -0700916 { "setPreviewSurface",
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800917 "(Landroid/view/Surface;)V",
Ruben Brunkfeb50af2014-05-09 19:58:49 -0700918 (void *)android_hardware_Camera_setPreviewSurface },
Jamie Gennisfd6f39e2010-12-20 12:15:00 -0800919 { "setPreviewTexture",
920 "(Landroid/graphics/SurfaceTexture;)V",
921 (void *)android_hardware_Camera_setPreviewTexture },
Eino-Ville Talvala7005b672013-04-02 15:46:38 -0700922 { "setPreviewCallbackSurface",
923 "(Landroid/view/Surface;)V",
924 (void *)android_hardware_Camera_setPreviewCallbackSurface },
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800925 { "startPreview",
926 "()V",
927 (void *)android_hardware_Camera_startPreview },
Wu-cheng Li4c2292e2011-07-22 02:37:11 +0800928 { "_stopPreview",
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800929 "()V",
930 (void *)android_hardware_Camera_stopPreview },
931 { "previewEnabled",
932 "()Z",
933 (void *)android_hardware_Camera_previewEnabled },
934 { "setHasPreviewCallback",
935 "(ZZ)V",
936 (void *)android_hardware_Camera_setHasPreviewCallback },
James Donge00cab72011-02-17 16:38:06 -0800937 { "_addCallbackBuffer",
938 "([BI)V",
Andrew Harp94927df2009-10-20 01:47:05 -0400939 (void *)android_hardware_Camera_addCallbackBuffer },
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800940 { "native_autoFocus",
941 "()V",
942 (void *)android_hardware_Camera_autoFocus },
Chih-Chung Chang244f8c22009-09-15 14:51:56 +0800943 { "native_cancelAutoFocus",
944 "()V",
945 (void *)android_hardware_Camera_cancelAutoFocus },
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800946 { "native_takePicture",
James Donge00cab72011-02-17 16:38:06 -0800947 "(I)V",
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800948 (void *)android_hardware_Camera_takePicture },
949 { "native_setParameters",
950 "(Ljava/lang/String;)V",
951 (void *)android_hardware_Camera_setParameters },
952 { "native_getParameters",
953 "()Ljava/lang/String;",
954 (void *)android_hardware_Camera_getParameters },
955 { "reconnect",
956 "()V",
957 (void*)android_hardware_Camera_reconnect },
958 { "lock",
Wu-cheng Liffe1cf22009-09-10 16:49:17 +0800959 "()V",
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800960 (void*)android_hardware_Camera_lock },
961 { "unlock",
Wu-cheng Liffe1cf22009-09-10 16:49:17 +0800962 "()V",
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800963 (void*)android_hardware_Camera_unlock },
Wu-cheng Li36f68b82009-09-28 16:14:58 -0700964 { "startSmoothZoom",
965 "(I)V",
966 (void *)android_hardware_Camera_startSmoothZoom },
967 { "stopSmoothZoom",
968 "()V",
969 (void *)android_hardware_Camera_stopSmoothZoom },
Chih-Chung Changd1d77062010-01-22 17:49:48 -0800970 { "setDisplayOrientation",
971 "(I)V",
972 (void *)android_hardware_Camera_setDisplayOrientation },
Eino-Ville Talvala4f8e5ce2012-10-08 18:16:35 -0700973 { "_enableShutterSound",
Eino-Ville Talvala69fe5272012-09-07 12:26:40 -0700974 "(Z)Z",
975 (void *)android_hardware_Camera_enableShutterSound },
Wu-cheng Li4c2292e2011-07-22 02:37:11 +0800976 { "_startFaceDetection",
977 "(I)V",
978 (void *)android_hardware_Camera_startFaceDetection },
979 { "_stopFaceDetection",
980 "()V",
981 (void *)android_hardware_Camera_stopFaceDetection},
Wu-cheng Li9d062cf2011-11-14 20:30:14 +0800982 { "enableFocusMoveCallback",
983 "(I)V",
984 (void *)android_hardware_Camera_enableFocusMoveCallback},
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800985};
986
987struct field {
988 const char *class_name;
989 const char *field_name;
990 const char *field_type;
991 jfieldID *jfield;
992};
993
Andreas Gampeed6b9df2014-11-20 22:02:20 -0800994static void find_fields(JNIEnv *env, field *fields, int count)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800995{
996 for (int i = 0; i < count; i++) {
997 field *f = &fields[i];
Andreas Gampeed6b9df2014-11-20 22:02:20 -0800998 jclass clazz = FindClassOrDie(env, f->class_name);
999 jfieldID field = GetFieldIDOrDie(env, clazz, f->field_name, f->field_type);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001000 *(f->jfield) = field;
1001 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001002}
1003
1004// Get all the required offsets in java class and register native functions
1005int register_android_hardware_Camera(JNIEnv *env)
1006{
1007 field fields_to_find[] = {
Ashok Bhat4838e332014-01-03 14:37:19 +00001008 { "android/hardware/Camera", "mNativeContext", "J", &fields.context },
Wu-cheng Li3fd5fa42010-09-15 16:06:20 -07001009 { "android/hardware/Camera$CameraInfo", "facing", "I", &fields.facing },
1010 { "android/hardware/Camera$CameraInfo", "orientation", "I", &fields.orientation },
Eino-Ville Talvalaf7c6c5a2012-09-19 11:46:11 -07001011 { "android/hardware/Camera$CameraInfo", "canDisableShutterSound", "Z",
1012 &fields.canDisableShutterSound },
Wu-cheng Lif0d6a482011-07-28 05:30:59 +08001013 { "android/hardware/Camera$Face", "rect", "Landroid/graphics/Rect;", &fields.face_rect },
1014 { "android/hardware/Camera$Face", "score", "I", &fields.face_score },
Wu-cheng Li4c2292e2011-07-22 02:37:11 +08001015 { "android/graphics/Rect", "left", "I", &fields.rect_left },
1016 { "android/graphics/Rect", "top", "I", &fields.rect_top },
1017 { "android/graphics/Rect", "right", "I", &fields.rect_right },
1018 { "android/graphics/Rect", "bottom", "I", &fields.rect_bottom },
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001019 };
1020
Andreas Gampeed6b9df2014-11-20 22:02:20 -08001021 find_fields(env, fields_to_find, NELEM(fields_to_find));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001022
Andreas Gampeed6b9df2014-11-20 22:02:20 -08001023 jclass clazz = FindClassOrDie(env, "android/hardware/Camera");
1024 fields.post_event = GetStaticMethodIDOrDie(env, clazz, "postEventFromNative",
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001025 "(Ljava/lang/Object;IIILjava/lang/Object;)V");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001026
Andreas Gampeed6b9df2014-11-20 22:02:20 -08001027 clazz = FindClassOrDie(env, "android/graphics/Rect");
1028 fields.rect_constructor = GetMethodIDOrDie(env, clazz, "<init>", "()V");
Wu-cheng Libb1e2752011-07-30 05:00:37 +08001029
Andreas Gampeed6b9df2014-11-20 22:02:20 -08001030 clazz = FindClassOrDie(env, "android/hardware/Camera$Face");
1031 fields.face_constructor = GetMethodIDOrDie(env, clazz, "<init>", "()V");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001032
1033 // Register native functions
Andreas Gampeed6b9df2014-11-20 22:02:20 -08001034 return RegisterMethodsOrDie(env, "android/hardware/Camera", camMethods, NELEM(camMethods));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001035}