blob: 307293f9a845375a5b26fe16226916227eee0152 [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"
24#include "android_runtime/AndroidRuntime.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>
30
Andy McFaddend47f7d82012-12-18 09:48:38 -080031#include <gui/GLConsumer.h>
Mathias Agopian8335f1c2012-02-25 18:48:35 -080032#include <gui/Surface.h>
Mathias Agopian000479f2010-02-09 17:46:37 -080033#include <camera/Camera.h>
Mathias Agopian07952722009-05-19 19:08:10 -070034#include <binder/IMemory.h>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080035
36using namespace android;
37
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080038struct fields_t {
39 jfieldID context;
Chih-Chung Changb8bb78f2010-06-10 13:32:16 +080040 jfieldID facing;
41 jfieldID orientation;
Eino-Ville Talvalaf7c6c5a2012-09-19 11:46:11 -070042 jfieldID canDisableShutterSound;
Wu-cheng Lif0d6a482011-07-28 05:30:59 +080043 jfieldID face_rect;
Wu-cheng Li4c2292e2011-07-22 02:37:11 +080044 jfieldID face_score;
45 jfieldID rect_left;
46 jfieldID rect_top;
47 jfieldID rect_right;
48 jfieldID rect_bottom;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080049 jmethodID post_event;
Wu-cheng Libb1e2752011-07-30 05:00:37 +080050 jmethodID rect_constructor;
51 jmethodID face_constructor;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080052};
53
54static fields_t fields;
55static Mutex sLock;
56
Dave Sparks5e271152009-06-23 17:30:11 -070057// provides persistent context for calls from native code to Java
58class JNICameraContext: public CameraListener
59{
60public:
61 JNICameraContext(JNIEnv* env, jobject weak_this, jclass clazz, const sp<Camera>& camera);
62 ~JNICameraContext() { release(); }
63 virtual void notify(int32_t msgType, int32_t ext1, int32_t ext2);
Wu-cheng Libb1e2752011-07-30 05:00:37 +080064 virtual void postData(int32_t msgType, const sp<IMemory>& dataPtr,
65 camera_frame_metadata_t *metadata);
Dave Sparks59c1a932009-07-08 15:56:53 -070066 virtual void postDataTimestamp(nsecs_t timestamp, int32_t msgType, const sp<IMemory>& dataPtr);
Wu-cheng Libb1e2752011-07-30 05:00:37 +080067 void postMetadata(JNIEnv *env, int32_t msgType, camera_frame_metadata_t *metadata);
James Donge00cab72011-02-17 16:38:06 -080068 void addCallbackBuffer(JNIEnv *env, jbyteArray cbb, int msgType);
Andrew Harp94927df2009-10-20 01:47:05 -040069 void setCallbackMode(JNIEnv *env, bool installed, bool manualMode);
Dave Sparks5e271152009-06-23 17:30:11 -070070 sp<Camera> getCamera() { Mutex::Autolock _l(mLock); return mCamera; }
James Donge00cab72011-02-17 16:38:06 -080071 bool isRawImageCallbackBufferAvailable() const;
Dave Sparks5e271152009-06-23 17:30:11 -070072 void release();
73
74private:
75 void copyAndPost(JNIEnv* env, const sp<IMemory>& dataPtr, int msgType);
James Donge00cab72011-02-17 16:38:06 -080076 void clearCallbackBuffers_l(JNIEnv *env, Vector<jbyteArray> *buffers);
Andrew Harp94927df2009-10-20 01:47:05 -040077 void clearCallbackBuffers_l(JNIEnv *env);
James Donge00cab72011-02-17 16:38:06 -080078 jbyteArray getCallbackBuffer(JNIEnv *env, Vector<jbyteArray> *buffers, size_t bufferSize);
Dave Sparks5e271152009-06-23 17:30:11 -070079
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080080 jobject mCameraJObjectWeak; // weak reference to java object
81 jclass mCameraJClass; // strong reference to java class
Wu-cheng Liffe1cf22009-09-10 16:49:17 +080082 sp<Camera> mCamera; // strong reference to native object
Wu-cheng Libb1e2752011-07-30 05:00:37 +080083 jclass mFaceClass; // strong reference to Face class
84 jclass mRectClass; // strong reference to Rect class
Dave Sparks5e271152009-06-23 17:30:11 -070085 Mutex mLock;
Andrew Harp94927df2009-10-20 01:47:05 -040086
James Donge00cab72011-02-17 16:38:06 -080087 /*
88 * Global reference application-managed raw image buffer queue.
89 *
90 * Manual-only mode is supported for raw image callbacks, which is
91 * set whenever method addCallbackBuffer() with msgType =
92 * CAMERA_MSG_RAW_IMAGE is called; otherwise, null is returned
93 * with raw image callbacks.
94 */
95 Vector<jbyteArray> mRawImageCallbackBuffers;
96
97 /*
98 * Application-managed preview buffer queue and the flags
99 * associated with the usage of the preview buffer callback.
100 */
Andrew Harp94927df2009-10-20 01:47:05 -0400101 Vector<jbyteArray> mCallbackBuffers; // Global reference application managed byte[]
102 bool mManualBufferMode; // Whether to use application managed buffers.
James Donge00cab72011-02-17 16:38:06 -0800103 bool mManualCameraCallbackSet; // Whether the callback has been set, used to
104 // reduce unnecessary calls to set the callback.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800105};
106
James Donge00cab72011-02-17 16:38:06 -0800107bool JNICameraContext::isRawImageCallbackBufferAvailable() const
108{
109 return !mRawImageCallbackBuffers.isEmpty();
110}
111
Dave Sparks5e271152009-06-23 17:30:11 -0700112sp<Camera> get_native_camera(JNIEnv *env, jobject thiz, JNICameraContext** pContext)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800113{
114 sp<Camera> camera;
115 Mutex::Autolock _l(sLock);
Ashok Bhat4838e332014-01-03 14:37:19 +0000116 JNICameraContext* context = reinterpret_cast<JNICameraContext*>(env->GetLongField(thiz, fields.context));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800117 if (context != NULL) {
Dave Sparks5e271152009-06-23 17:30:11 -0700118 camera = context->getCamera();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800119 }
Steve Block71f2cf12011-10-20 11:56:00 +0100120 ALOGV("get_native_camera: context=%p, camera=%p", context, camera.get());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800121 if (camera == 0) {
Elliott Hughes69a017b2011-04-08 14:10:28 -0700122 jniThrowRuntimeException(env, "Method called after release()");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800123 }
124
125 if (pContext != NULL) *pContext = context;
126 return camera;
127}
128
Dave Sparks5e271152009-06-23 17:30:11 -0700129JNICameraContext::JNICameraContext(JNIEnv* env, jobject weak_this, jclass clazz, const sp<Camera>& camera)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800130{
Dave Sparks5e271152009-06-23 17:30:11 -0700131 mCameraJObjectWeak = env->NewGlobalRef(weak_this);
132 mCameraJClass = (jclass)env->NewGlobalRef(clazz);
133 mCamera = camera;
Andrew Harp94927df2009-10-20 01:47:05 -0400134
Wu-cheng Libb1e2752011-07-30 05:00:37 +0800135 jclass faceClazz = env->FindClass("android/hardware/Camera$Face");
136 mFaceClass = (jclass) env->NewGlobalRef(faceClazz);
137
138 jclass rectClazz = env->FindClass("android/graphics/Rect");
139 mRectClass = (jclass) env->NewGlobalRef(rectClazz);
140
Andrew Harp94927df2009-10-20 01:47:05 -0400141 mManualBufferMode = false;
142 mManualCameraCallbackSet = false;
Dave Sparks5e271152009-06-23 17:30:11 -0700143}
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800144
Dave Sparks5e271152009-06-23 17:30:11 -0700145void JNICameraContext::release()
146{
Steve Block71f2cf12011-10-20 11:56:00 +0100147 ALOGV("release");
Dave Sparks5e271152009-06-23 17:30:11 -0700148 Mutex::Autolock _l(mLock);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800149 JNIEnv *env = AndroidRuntime::getJNIEnv();
Dave Sparks5e271152009-06-23 17:30:11 -0700150
151 if (mCameraJObjectWeak != NULL) {
152 env->DeleteGlobalRef(mCameraJObjectWeak);
153 mCameraJObjectWeak = NULL;
154 }
155 if (mCameraJClass != NULL) {
156 env->DeleteGlobalRef(mCameraJClass);
157 mCameraJClass = NULL;
158 }
Wu-cheng Libb1e2752011-07-30 05:00:37 +0800159 if (mFaceClass != NULL) {
160 env->DeleteGlobalRef(mFaceClass);
161 mFaceClass = NULL;
162 }
163 if (mRectClass != NULL) {
164 env->DeleteGlobalRef(mRectClass);
165 mRectClass = NULL;
166 }
Andrew Harp94927df2009-10-20 01:47:05 -0400167 clearCallbackBuffers_l(env);
Dave Sparks5e271152009-06-23 17:30:11 -0700168 mCamera.clear();
169}
170
171void JNICameraContext::notify(int32_t msgType, int32_t ext1, int32_t ext2)
172{
Steve Block71f2cf12011-10-20 11:56:00 +0100173 ALOGV("notify");
Dave Sparks5e271152009-06-23 17:30:11 -0700174
175 // VM pointer will be NULL if object is released
176 Mutex::Autolock _l(mLock);
177 if (mCameraJObjectWeak == NULL) {
Steve Block8564c8d2012-01-05 23:22:43 +0000178 ALOGW("callback on dead camera object");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800179 return;
180 }
Dave Sparks5e271152009-06-23 17:30:11 -0700181 JNIEnv *env = AndroidRuntime::getJNIEnv();
James Donge00cab72011-02-17 16:38:06 -0800182
183 /*
184 * If the notification or msgType is CAMERA_MSG_RAW_IMAGE_NOTIFY, change it
185 * to CAMERA_MSG_RAW_IMAGE since CAMERA_MSG_RAW_IMAGE_NOTIFY is not exposed
186 * to the Java app.
187 */
188 if (msgType == CAMERA_MSG_RAW_IMAGE_NOTIFY) {
189 msgType = CAMERA_MSG_RAW_IMAGE;
190 }
191
Dave Sparksc62f9bd2009-06-26 13:33:32 -0700192 env->CallStaticVoidMethod(mCameraJClass, fields.post_event,
Chih-Chung Chang6157de02009-09-24 15:29:35 -0700193 mCameraJObjectWeak, msgType, ext1, ext2, NULL);
Dave Sparks5e271152009-06-23 17:30:11 -0700194}
195
James Donge00cab72011-02-17 16:38:06 -0800196jbyteArray JNICameraContext::getCallbackBuffer(
197 JNIEnv* env, Vector<jbyteArray>* buffers, size_t bufferSize)
198{
199 jbyteArray obj = NULL;
200
201 // Vector access should be protected by lock in postData()
202 if (!buffers->isEmpty()) {
Steve Block71f2cf12011-10-20 11:56:00 +0100203 ALOGV("Using callback buffer from queue of length %d", buffers->size());
James Donge00cab72011-02-17 16:38:06 -0800204 jbyteArray globalBuffer = buffers->itemAt(0);
205 buffers->removeAt(0);
206
207 obj = (jbyteArray)env->NewLocalRef(globalBuffer);
208 env->DeleteGlobalRef(globalBuffer);
209
210 if (obj != NULL) {
211 jsize bufferLength = env->GetArrayLength(obj);
212 if ((int)bufferLength < (int)bufferSize) {
Steve Block3762c312012-01-06 19:20:56 +0000213 ALOGE("Callback buffer was too small! Expected %d bytes, but got %d bytes!",
James Donge00cab72011-02-17 16:38:06 -0800214 bufferSize, bufferLength);
215 env->DeleteLocalRef(obj);
216 return NULL;
217 }
218 }
219 }
220
221 return obj;
222}
223
Dave Sparks5e271152009-06-23 17:30:11 -0700224void JNICameraContext::copyAndPost(JNIEnv* env, const sp<IMemory>& dataPtr, int msgType)
225{
226 jbyteArray obj = NULL;
227
228 // allocate Java byte array and copy data
229 if (dataPtr != NULL) {
230 ssize_t offset;
231 size_t size;
232 sp<IMemoryHeap> heap = dataPtr->getMemory(&offset, &size);
Glenn Kasten2fbf25b2014-03-28 15:41:58 -0700233 ALOGV("copyAndPost: off=%zd, size=%zu", offset, size);
Dave Sparks5e271152009-06-23 17:30:11 -0700234 uint8_t *heapBase = (uint8_t*)heap->base();
235
236 if (heapBase != NULL) {
Dave Sparksc4ca4202009-07-13 09:38:20 -0700237 const jbyte* data = reinterpret_cast<const jbyte*>(heapBase + offset);
Andrew Harp94927df2009-10-20 01:47:05 -0400238
Wu-cheng Li5f0ef5f2011-09-21 16:40:23 +0800239 if (msgType == CAMERA_MSG_RAW_IMAGE) {
240 obj = getCallbackBuffer(env, &mRawImageCallbackBuffers, size);
241 } else if (msgType == CAMERA_MSG_PREVIEW_FRAME && mManualBufferMode) {
242 obj = getCallbackBuffer(env, &mCallbackBuffers, size);
Andrew Harp94927df2009-10-20 01:47:05 -0400243
Wu-cheng Li5f0ef5f2011-09-21 16:40:23 +0800244 if (mCallbackBuffers.isEmpty()) {
Steve Block71f2cf12011-10-20 11:56:00 +0100245 ALOGV("Out of buffers, clearing callback!");
Wu-cheng Li5f0ef5f2011-09-21 16:40:23 +0800246 mCamera->setPreviewCallbackFlags(CAMERA_FRAME_CALLBACK_FLAG_NOOP);
247 mManualCameraCallbackSet = false;
Andrew Harp94927df2009-10-20 01:47:05 -0400248
Wu-cheng Li5f0ef5f2011-09-21 16:40:23 +0800249 if (obj == NULL) {
Andrew Harp94927df2009-10-20 01:47:05 -0400250 return;
251 }
252 }
Wu-cheng Li5f0ef5f2011-09-21 16:40:23 +0800253 } else {
Steve Block71f2cf12011-10-20 11:56:00 +0100254 ALOGV("Allocating callback buffer");
Wu-cheng Li5f0ef5f2011-09-21 16:40:23 +0800255 obj = env->NewByteArray(size);
Andrew Harp94927df2009-10-20 01:47:05 -0400256 }
257
Dave Sparks5e271152009-06-23 17:30:11 -0700258 if (obj == NULL) {
Steve Block3762c312012-01-06 19:20:56 +0000259 ALOGE("Couldn't allocate byte array for JPEG data");
Dave Sparks5e271152009-06-23 17:30:11 -0700260 env->ExceptionClear();
261 } else {
Dave Sparksa95f4952009-07-10 18:13:36 -0700262 env->SetByteArrayRegion(obj, 0, size, data);
Dave Sparks5e271152009-06-23 17:30:11 -0700263 }
264 } else {
Steve Block3762c312012-01-06 19:20:56 +0000265 ALOGE("image heap is NULL");
Dave Sparks5e271152009-06-23 17:30:11 -0700266 }
267 }
268
269 // post image data to Java
270 env->CallStaticVoidMethod(mCameraJClass, fields.post_event,
271 mCameraJObjectWeak, msgType, 0, 0, obj);
272 if (obj) {
273 env->DeleteLocalRef(obj);
274 }
275}
276
Wu-cheng Libb1e2752011-07-30 05:00:37 +0800277void JNICameraContext::postData(int32_t msgType, const sp<IMemory>& dataPtr,
278 camera_frame_metadata_t *metadata)
Dave Sparks5e271152009-06-23 17:30:11 -0700279{
280 // VM pointer will be NULL if object is released
281 Mutex::Autolock _l(mLock);
282 JNIEnv *env = AndroidRuntime::getJNIEnv();
Dave Sparksd0cbb1a2009-06-29 19:03:33 -0700283 if (mCameraJObjectWeak == NULL) {
Steve Block8564c8d2012-01-05 23:22:43 +0000284 ALOGW("callback on dead camera object");
Dave Sparksd0cbb1a2009-06-29 19:03:33 -0700285 return;
286 }
Dave Sparks5e271152009-06-23 17:30:11 -0700287
Wu-cheng Libb1e2752011-07-30 05:00:37 +0800288 int32_t dataMsgType = msgType & ~CAMERA_MSG_PREVIEW_METADATA;
289
Dave Sparks5e271152009-06-23 17:30:11 -0700290 // return data based on callback type
Wu-cheng Libb1e2752011-07-30 05:00:37 +0800291 switch (dataMsgType) {
James Donge00cab72011-02-17 16:38:06 -0800292 case CAMERA_MSG_VIDEO_FRAME:
293 // should never happen
294 break;
295
296 // For backward-compatibility purpose, if there is no callback
297 // buffer for raw image, the callback returns null.
298 case CAMERA_MSG_RAW_IMAGE:
Steve Block71f2cf12011-10-20 11:56:00 +0100299 ALOGV("rawCallback");
James Donge00cab72011-02-17 16:38:06 -0800300 if (mRawImageCallbackBuffers.isEmpty()) {
301 env->CallStaticVoidMethod(mCameraJClass, fields.post_event,
Wu-cheng Libb1e2752011-07-30 05:00:37 +0800302 mCameraJObjectWeak, dataMsgType, 0, 0, NULL);
James Donge00cab72011-02-17 16:38:06 -0800303 } else {
Wu-cheng Libb1e2752011-07-30 05:00:37 +0800304 copyAndPost(env, dataPtr, dataMsgType);
James Donge00cab72011-02-17 16:38:06 -0800305 }
306 break;
307
Wu-cheng Libb1e2752011-07-30 05:00:37 +0800308 // There is no data.
309 case 0:
James Donge00cab72011-02-17 16:38:06 -0800310 break;
Wu-cheng Libb1e2752011-07-30 05:00:37 +0800311
312 default:
Steve Block71f2cf12011-10-20 11:56:00 +0100313 ALOGV("dataCallback(%d, %p)", dataMsgType, dataPtr.get());
Wu-cheng Libb1e2752011-07-30 05:00:37 +0800314 copyAndPost(env, dataPtr, dataMsgType);
315 break;
316 }
317
318 // post frame metadata to Java
319 if (metadata && (msgType & CAMERA_MSG_PREVIEW_METADATA)) {
320 postMetadata(env, CAMERA_MSG_PREVIEW_METADATA, metadata);
Dave Sparks5e271152009-06-23 17:30:11 -0700321 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800322}
323
Dave Sparks59c1a932009-07-08 15:56:53 -0700324void JNICameraContext::postDataTimestamp(nsecs_t timestamp, int32_t msgType, const sp<IMemory>& dataPtr)
325{
326 // TODO: plumb up to Java. For now, just drop the timestamp
Wu-cheng Libb1e2752011-07-30 05:00:37 +0800327 postData(msgType, dataPtr, NULL);
328}
329
330void JNICameraContext::postMetadata(JNIEnv *env, int32_t msgType, camera_frame_metadata_t *metadata)
331{
332 jobjectArray obj = NULL;
333 obj = (jobjectArray) env->NewObjectArray(metadata->number_of_faces,
334 mFaceClass, NULL);
335 if (obj == NULL) {
Steve Block3762c312012-01-06 19:20:56 +0000336 ALOGE("Couldn't allocate face metadata array");
Wu-cheng Libb1e2752011-07-30 05:00:37 +0800337 return;
338 }
339
340 for (int i = 0; i < metadata->number_of_faces; i++) {
341 jobject face = env->NewObject(mFaceClass, fields.face_constructor);
342 env->SetObjectArrayElement(obj, i, face);
343
344 jobject rect = env->NewObject(mRectClass, fields.rect_constructor);
345 env->SetIntField(rect, fields.rect_left, metadata->faces[i].rect[0]);
346 env->SetIntField(rect, fields.rect_top, metadata->faces[i].rect[1]);
347 env->SetIntField(rect, fields.rect_right, metadata->faces[i].rect[2]);
348 env->SetIntField(rect, fields.rect_bottom, metadata->faces[i].rect[3]);
349
350 env->SetObjectField(face, fields.face_rect, rect);
351 env->SetIntField(face, fields.face_score, metadata->faces[i].score);
352
353 env->DeleteLocalRef(face);
354 env->DeleteLocalRef(rect);
355 }
356 env->CallStaticVoidMethod(mCameraJClass, fields.post_event,
357 mCameraJObjectWeak, msgType, 0, 0, obj);
358 env->DeleteLocalRef(obj);
Dave Sparks59c1a932009-07-08 15:56:53 -0700359}
360
Andrew Harp94927df2009-10-20 01:47:05 -0400361void JNICameraContext::setCallbackMode(JNIEnv *env, bool installed, bool manualMode)
362{
363 Mutex::Autolock _l(mLock);
364 mManualBufferMode = manualMode;
365 mManualCameraCallbackSet = false;
366
367 // In order to limit the over usage of binder threads, all non-manual buffer
Iliyan Malchev9c7ac0d2011-04-14 16:51:21 -0700368 // callbacks use CAMERA_FRAME_CALLBACK_FLAG_BARCODE_SCANNER mode now.
Andrew Harp94927df2009-10-20 01:47:05 -0400369 //
370 // Continuous callbacks will have the callback re-registered from handleMessage.
371 // Manual buffer mode will operate as fast as possible, relying on the finite supply
372 // of buffers for throttling.
373
374 if (!installed) {
Iliyan Malchev9c7ac0d2011-04-14 16:51:21 -0700375 mCamera->setPreviewCallbackFlags(CAMERA_FRAME_CALLBACK_FLAG_NOOP);
James Donge00cab72011-02-17 16:38:06 -0800376 clearCallbackBuffers_l(env, &mCallbackBuffers);
Andrew Harp94927df2009-10-20 01:47:05 -0400377 } else if (mManualBufferMode) {
378 if (!mCallbackBuffers.isEmpty()) {
Iliyan Malchev9c7ac0d2011-04-14 16:51:21 -0700379 mCamera->setPreviewCallbackFlags(CAMERA_FRAME_CALLBACK_FLAG_CAMERA);
Andrew Harp94927df2009-10-20 01:47:05 -0400380 mManualCameraCallbackSet = true;
381 }
382 } else {
Iliyan Malchev9c7ac0d2011-04-14 16:51:21 -0700383 mCamera->setPreviewCallbackFlags(CAMERA_FRAME_CALLBACK_FLAG_BARCODE_SCANNER);
James Donge00cab72011-02-17 16:38:06 -0800384 clearCallbackBuffers_l(env, &mCallbackBuffers);
Andrew Harp94927df2009-10-20 01:47:05 -0400385 }
386}
387
James Donge00cab72011-02-17 16:38:06 -0800388void JNICameraContext::addCallbackBuffer(
389 JNIEnv *env, jbyteArray cbb, int msgType)
Andrew Harp94927df2009-10-20 01:47:05 -0400390{
Steve Block71f2cf12011-10-20 11:56:00 +0100391 ALOGV("addCallbackBuffer: 0x%x", msgType);
Andrew Harp94927df2009-10-20 01:47:05 -0400392 if (cbb != NULL) {
393 Mutex::Autolock _l(mLock);
James Donge00cab72011-02-17 16:38:06 -0800394 switch (msgType) {
395 case CAMERA_MSG_PREVIEW_FRAME: {
396 jbyteArray callbackBuffer = (jbyteArray)env->NewGlobalRef(cbb);
397 mCallbackBuffers.push(callbackBuffer);
Andrew Harp94927df2009-10-20 01:47:05 -0400398
Steve Block71f2cf12011-10-20 11:56:00 +0100399 ALOGV("Adding callback buffer to queue, %d total",
James Donge00cab72011-02-17 16:38:06 -0800400 mCallbackBuffers.size());
Andrew Harp94927df2009-10-20 01:47:05 -0400401
James Donge00cab72011-02-17 16:38:06 -0800402 // We want to make sure the camera knows we're ready for the
403 // next frame. This may have come unset had we not had a
404 // callbackbuffer ready for it last time.
405 if (mManualBufferMode && !mManualCameraCallbackSet) {
Iliyan Malchev9c7ac0d2011-04-14 16:51:21 -0700406 mCamera->setPreviewCallbackFlags(CAMERA_FRAME_CALLBACK_FLAG_CAMERA);
James Donge00cab72011-02-17 16:38:06 -0800407 mManualCameraCallbackSet = true;
408 }
409 break;
410 }
411 case CAMERA_MSG_RAW_IMAGE: {
412 jbyteArray callbackBuffer = (jbyteArray)env->NewGlobalRef(cbb);
413 mRawImageCallbackBuffers.push(callbackBuffer);
414 break;
415 }
416 default: {
417 jniThrowException(env,
418 "java/lang/IllegalArgumentException",
419 "Unsupported message type");
420 return;
421 }
Andrew Harp94927df2009-10-20 01:47:05 -0400422 }
423 } else {
Steve Block3762c312012-01-06 19:20:56 +0000424 ALOGE("Null byte array!");
Andrew Harp94927df2009-10-20 01:47:05 -0400425 }
426}
427
428void JNICameraContext::clearCallbackBuffers_l(JNIEnv *env)
429{
James Donge00cab72011-02-17 16:38:06 -0800430 clearCallbackBuffers_l(env, &mCallbackBuffers);
431 clearCallbackBuffers_l(env, &mRawImageCallbackBuffers);
432}
433
434void JNICameraContext::clearCallbackBuffers_l(JNIEnv *env, Vector<jbyteArray> *buffers) {
Steve Block71f2cf12011-10-20 11:56:00 +0100435 ALOGV("Clearing callback buffers, %d remained", buffers->size());
James Donge00cab72011-02-17 16:38:06 -0800436 while (!buffers->isEmpty()) {
437 env->DeleteGlobalRef(buffers->top());
438 buffers->pop();
Andrew Harp94927df2009-10-20 01:47:05 -0400439 }
440}
441
Chih-Chung Change25cc652010-05-06 16:36:58 +0800442static jint android_hardware_Camera_getNumberOfCameras(JNIEnv *env, jobject thiz)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800443{
Chih-Chung Change25cc652010-05-06 16:36:58 +0800444 return Camera::getNumberOfCameras();
445}
446
Chih-Chung Changb8bb78f2010-06-10 13:32:16 +0800447static void android_hardware_Camera_getCameraInfo(JNIEnv *env, jobject thiz,
448 jint cameraId, jobject info_obj)
449{
450 CameraInfo cameraInfo;
451 status_t rc = Camera::getCameraInfo(cameraId, &cameraInfo);
452 if (rc != NO_ERROR) {
Elliott Hughes69a017b2011-04-08 14:10:28 -0700453 jniThrowRuntimeException(env, "Fail to get camera info");
Chih-Chung Changb8bb78f2010-06-10 13:32:16 +0800454 return;
455 }
456 env->SetIntField(info_obj, fields.facing, cameraInfo.facing);
457 env->SetIntField(info_obj, fields.orientation, cameraInfo.orientation);
Eino-Ville Talvalaf7c6c5a2012-09-19 11:46:11 -0700458
459 char value[PROPERTY_VALUE_MAX];
460 property_get("ro.camera.sound.forced", value, "0");
461 jboolean canDisableShutterSound = (strncmp(value, "0", 2) == 0);
462 env->SetBooleanField(info_obj, fields.canDisableShutterSound,
463 canDisableShutterSound);
Chih-Chung Changb8bb78f2010-06-10 13:32:16 +0800464}
465
Chih-Chung Change25cc652010-05-06 16:36:58 +0800466// connect to camera service
467static void android_hardware_Camera_native_setup(JNIEnv *env, jobject thiz,
Eino-Ville Talvala788717c2013-02-15 18:30:15 -0800468 jobject weak_this, jint cameraId, jstring clientPackageName)
Chih-Chung Change25cc652010-05-06 16:36:58 +0800469{
Eino-Ville Talvala788717c2013-02-15 18:30:15 -0800470 // Convert jstring to String16
471 const char16_t *rawClientName = env->GetStringChars(clientPackageName, NULL);
472 jsize rawClientNameLen = env->GetStringLength(clientPackageName);
473 String16 clientName(rawClientName, rawClientNameLen);
474 env->ReleaseStringChars(clientPackageName, rawClientName);
475
476 sp<Camera> camera = Camera::connect(cameraId, clientName,
477 Camera::USE_CALLING_UID);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800478
479 if (camera == NULL) {
Elliott Hughes69a017b2011-04-08 14:10:28 -0700480 jniThrowRuntimeException(env, "Fail to connect to camera service");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800481 return;
482 }
483
484 // make sure camera hardware is alive
485 if (camera->getStatus() != NO_ERROR) {
Elliott Hughes69a017b2011-04-08 14:10:28 -0700486 jniThrowRuntimeException(env, "Camera initialization failed");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800487 return;
488 }
489
490 jclass clazz = env->GetObjectClass(thiz);
491 if (clazz == NULL) {
Elliott Hughes69a017b2011-04-08 14:10:28 -0700492 jniThrowRuntimeException(env, "Can't find android/hardware/Camera");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800493 return;
494 }
495
496 // We use a weak reference so the Camera object can be garbage collected.
497 // The reference is only used as a proxy for callbacks.
Dave Sparks5e271152009-06-23 17:30:11 -0700498 sp<JNICameraContext> context = new JNICameraContext(env, weak_this, clazz, camera);
Mathias Agopianb1d90c82013-03-06 17:45:42 -0800499 context->incStrong((void*)android_hardware_Camera_native_setup);
Dave Sparks5e271152009-06-23 17:30:11 -0700500 camera->setListener(context);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800501
502 // save context in opaque field
Ashok Bhat4838e332014-01-03 14:37:19 +0000503 env->SetLongField(thiz, fields.context, (jlong)context.get());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800504}
505
506// disconnect from camera service
507// It's okay to call this when the native camera context is already null.
508// This handles the case where the user has called release() and the
509// finalizer is invoked later.
510static void android_hardware_Camera_release(JNIEnv *env, jobject thiz)
511{
Steve Block71f2cf12011-10-20 11:56:00 +0100512 // TODO: Change to ALOGV
513 ALOGV("release camera");
Dave Sparks5e271152009-06-23 17:30:11 -0700514 JNICameraContext* context = NULL;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800515 sp<Camera> camera;
516 {
517 Mutex::Autolock _l(sLock);
Ashok Bhat4838e332014-01-03 14:37:19 +0000518 context = reinterpret_cast<JNICameraContext*>(env->GetLongField(thiz, fields.context));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800519
520 // Make sure we do not attempt to callback on a deleted Java object.
Ashok Bhat4838e332014-01-03 14:37:19 +0000521 env->SetLongField(thiz, fields.context, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800522 }
523
524 // clean up if release has not been called before
525 if (context != NULL) {
Dave Sparks5e271152009-06-23 17:30:11 -0700526 camera = context->getCamera();
527 context->release();
Steve Block71f2cf12011-10-20 11:56:00 +0100528 ALOGV("native_release: context=%p camera=%p", context, camera.get());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800529
530 // clear callbacks
531 if (camera != NULL) {
Iliyan Malchev9c7ac0d2011-04-14 16:51:21 -0700532 camera->setPreviewCallbackFlags(CAMERA_FRAME_CALLBACK_FLAG_NOOP);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800533 camera->disconnect();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800534 }
535
536 // remove context to prevent further Java access
Mathias Agopianb1d90c82013-03-06 17:45:42 -0800537 context->decStrong((void*)android_hardware_Camera_native_setup);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800538 }
539}
540
541static void android_hardware_Camera_setPreviewDisplay(JNIEnv *env, jobject thiz, jobject jSurface)
542{
Steve Block71f2cf12011-10-20 11:56:00 +0100543 ALOGV("setPreviewDisplay");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800544 sp<Camera> camera = get_native_camera(env, thiz, NULL);
545 if (camera == 0) return;
546
Mathias Agopian4a05f432013-03-12 18:43:34 -0700547 sp<IGraphicBufferProducer> gbp;
Jesse Hallaa70f222013-02-21 15:06:27 -0800548 sp<Surface> surface;
549 if (jSurface) {
550 surface = android_view_Surface_getSurface(env, jSurface);
Mathias Agopian4a05f432013-03-12 18:43:34 -0700551 if (surface != NULL) {
552 gbp = surface->getIGraphicBufferProducer();
553 }
Jesse Hallaa70f222013-02-21 15:06:27 -0800554 }
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800555
Eino-Ville Talvala7b297792013-08-21 14:39:22 -0700556 if (camera->setPreviewTarget(gbp) != NO_ERROR) {
Mathias Agopian4a05f432013-03-12 18:43:34 -0700557 jniThrowException(env, "java/io/IOException", "setPreviewTexture failed");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800558 }
559}
560
Jamie Gennisfd6f39e2010-12-20 12:15:00 -0800561static void android_hardware_Camera_setPreviewTexture(JNIEnv *env,
562 jobject thiz, jobject jSurfaceTexture)
563{
Steve Block71f2cf12011-10-20 11:56:00 +0100564 ALOGV("setPreviewTexture");
Jamie Gennisfd6f39e2010-12-20 12:15:00 -0800565 sp<Camera> camera = get_native_camera(env, thiz, NULL);
566 if (camera == 0) return;
567
Mathias Agopian52a9a102013-08-02 01:38:38 -0700568 sp<IGraphicBufferProducer> producer = NULL;
Jamie Gennisfd6f39e2010-12-20 12:15:00 -0800569 if (jSurfaceTexture != NULL) {
Mathias Agopian52a9a102013-08-02 01:38:38 -0700570 producer = SurfaceTexture_getProducer(env, jSurfaceTexture);
571 if (producer == NULL) {
Daniel Lam2e76c992012-02-23 14:35:13 -0800572 jniThrowException(env, "java/lang/IllegalArgumentException",
573 "SurfaceTexture already released in setPreviewTexture");
574 return;
575 }
576
Jamie Gennisfd6f39e2010-12-20 12:15:00 -0800577 }
Daniel Lam2e76c992012-02-23 14:35:13 -0800578
Eino-Ville Talvala7b297792013-08-21 14:39:22 -0700579 if (camera->setPreviewTarget(producer) != NO_ERROR) {
Jamie Gennisfd6f39e2010-12-20 12:15:00 -0800580 jniThrowException(env, "java/io/IOException",
581 "setPreviewTexture failed");
582 }
583}
584
Eino-Ville Talvala7005b672013-04-02 15:46:38 -0700585static void android_hardware_Camera_setPreviewCallbackSurface(JNIEnv *env,
586 jobject thiz, jobject jSurface)
587{
588 ALOGV("setPreviewCallbackSurface");
589 JNICameraContext* context;
590 sp<Camera> camera = get_native_camera(env, thiz, &context);
591 if (camera == 0) return;
592
593 sp<IGraphicBufferProducer> gbp;
594 sp<Surface> surface;
595 if (jSurface) {
596 surface = android_view_Surface_getSurface(env, jSurface);
597 if (surface != NULL) {
598 gbp = surface->getIGraphicBufferProducer();
599 }
600 }
601 // Clear out normal preview callbacks
602 context->setCallbackMode(env, false, false);
603 // Then set up callback surface
604 if (camera->setPreviewCallbackTarget(gbp) != NO_ERROR) {
605 jniThrowException(env, "java/io/IOException", "setPreviewCallbackTarget failed");
606 }
607}
608
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800609static void android_hardware_Camera_startPreview(JNIEnv *env, jobject thiz)
610{
Steve Block71f2cf12011-10-20 11:56:00 +0100611 ALOGV("startPreview");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800612 sp<Camera> camera = get_native_camera(env, thiz, NULL);
613 if (camera == 0) return;
614
615 if (camera->startPreview() != NO_ERROR) {
Elliott Hughes69a017b2011-04-08 14:10:28 -0700616 jniThrowRuntimeException(env, "startPreview failed");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800617 return;
618 }
619}
620
621static void android_hardware_Camera_stopPreview(JNIEnv *env, jobject thiz)
622{
Steve Block71f2cf12011-10-20 11:56:00 +0100623 ALOGV("stopPreview");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800624 sp<Camera> c = get_native_camera(env, thiz, NULL);
625 if (c == 0) return;
626
627 c->stopPreview();
628}
629
Ashok Bhat4838e332014-01-03 14:37:19 +0000630static jboolean android_hardware_Camera_previewEnabled(JNIEnv *env, jobject thiz)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800631{
Steve Block71f2cf12011-10-20 11:56:00 +0100632 ALOGV("previewEnabled");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800633 sp<Camera> c = get_native_camera(env, thiz, NULL);
Ashok Bhat4838e332014-01-03 14:37:19 +0000634 if (c == 0) return JNI_FALSE;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800635
Ashok Bhat4838e332014-01-03 14:37:19 +0000636 return c->previewEnabled() ? JNI_TRUE : JNI_FALSE;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800637}
638
Andrew Harp94927df2009-10-20 01:47:05 -0400639static void android_hardware_Camera_setHasPreviewCallback(JNIEnv *env, jobject thiz, jboolean installed, jboolean manualBuffer)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800640{
Steve Block71f2cf12011-10-20 11:56:00 +0100641 ALOGV("setHasPreviewCallback: installed:%d, manualBuffer:%d", (int)installed, (int)manualBuffer);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800642 // Important: Only install preview_callback if the Java code has called
643 // setPreviewCallback() with a non-null value, otherwise we'd pay to memcpy
644 // each preview frame for nothing.
Dave Sparks5e271152009-06-23 17:30:11 -0700645 JNICameraContext* context;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800646 sp<Camera> camera = get_native_camera(env, thiz, &context);
647 if (camera == 0) return;
648
Andrew Harp94927df2009-10-20 01:47:05 -0400649 // setCallbackMode will take care of setting the context flags and calling
650 // camera->setPreviewCallbackFlags within a mutex for us.
651 context->setCallbackMode(env, installed, manualBuffer);
652}
653
Ashok Bhat4838e332014-01-03 14:37:19 +0000654static void android_hardware_Camera_addCallbackBuffer(JNIEnv *env, jobject thiz, jbyteArray bytes, jint msgType) {
Steve Block71f2cf12011-10-20 11:56:00 +0100655 ALOGV("addCallbackBuffer: 0x%x", msgType);
Andrew Harp94927df2009-10-20 01:47:05 -0400656
Ashok Bhat4838e332014-01-03 14:37:19 +0000657 JNICameraContext* context = reinterpret_cast<JNICameraContext*>(env->GetLongField(thiz, fields.context));
Andrew Harp94927df2009-10-20 01:47:05 -0400658
659 if (context != NULL) {
James Donge00cab72011-02-17 16:38:06 -0800660 context->addCallbackBuffer(env, bytes, msgType);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800661 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800662}
663
664static void android_hardware_Camera_autoFocus(JNIEnv *env, jobject thiz)
665{
Steve Block71f2cf12011-10-20 11:56:00 +0100666 ALOGV("autoFocus");
Dave Sparks5e271152009-06-23 17:30:11 -0700667 JNICameraContext* context;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800668 sp<Camera> c = get_native_camera(env, thiz, &context);
669 if (c == 0) return;
670
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800671 if (c->autoFocus() != NO_ERROR) {
Elliott Hughes69a017b2011-04-08 14:10:28 -0700672 jniThrowRuntimeException(env, "autoFocus failed");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800673 }
674}
675
Chih-Chung Chang244f8c22009-09-15 14:51:56 +0800676static void android_hardware_Camera_cancelAutoFocus(JNIEnv *env, jobject thiz)
677{
Steve Block71f2cf12011-10-20 11:56:00 +0100678 ALOGV("cancelAutoFocus");
Chih-Chung Chang244f8c22009-09-15 14:51:56 +0800679 JNICameraContext* context;
680 sp<Camera> c = get_native_camera(env, thiz, &context);
681 if (c == 0) return;
682
683 if (c->cancelAutoFocus() != NO_ERROR) {
Elliott Hughes69a017b2011-04-08 14:10:28 -0700684 jniThrowRuntimeException(env, "cancelAutoFocus failed");
Chih-Chung Chang244f8c22009-09-15 14:51:56 +0800685 }
686}
687
Ashok Bhat4838e332014-01-03 14:37:19 +0000688static void android_hardware_Camera_takePicture(JNIEnv *env, jobject thiz, jint msgType)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800689{
Steve Block71f2cf12011-10-20 11:56:00 +0100690 ALOGV("takePicture");
Dave Sparks5e271152009-06-23 17:30:11 -0700691 JNICameraContext* context;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800692 sp<Camera> camera = get_native_camera(env, thiz, &context);
693 if (camera == 0) return;
694
James Donge00cab72011-02-17 16:38:06 -0800695 /*
696 * When CAMERA_MSG_RAW_IMAGE is requested, if the raw image callback
697 * buffer is available, CAMERA_MSG_RAW_IMAGE is enabled to get the
698 * notification _and_ the data; otherwise, CAMERA_MSG_RAW_IMAGE_NOTIFY
699 * is enabled to receive the callback notification but no data.
700 *
701 * Note that CAMERA_MSG_RAW_IMAGE_NOTIFY is not exposed to the
702 * Java application.
703 */
704 if (msgType & CAMERA_MSG_RAW_IMAGE) {
Steve Block71f2cf12011-10-20 11:56:00 +0100705 ALOGV("Enable raw image callback buffer");
James Donge00cab72011-02-17 16:38:06 -0800706 if (!context->isRawImageCallbackBufferAvailable()) {
Steve Block71f2cf12011-10-20 11:56:00 +0100707 ALOGV("Enable raw image notification, since no callback buffer exists");
James Donge00cab72011-02-17 16:38:06 -0800708 msgType &= ~CAMERA_MSG_RAW_IMAGE;
709 msgType |= CAMERA_MSG_RAW_IMAGE_NOTIFY;
710 }
711 }
712
713 if (camera->takePicture(msgType) != NO_ERROR) {
Elliott Hughes69a017b2011-04-08 14:10:28 -0700714 jniThrowRuntimeException(env, "takePicture failed");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800715 return;
716 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800717}
718
719static void android_hardware_Camera_setParameters(JNIEnv *env, jobject thiz, jstring params)
720{
Steve Block71f2cf12011-10-20 11:56:00 +0100721 ALOGV("setParameters");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800722 sp<Camera> camera = get_native_camera(env, thiz, NULL);
723 if (camera == 0) return;
724
725 const jchar* str = env->GetStringCritical(params, 0);
726 String8 params8;
727 if (params) {
728 params8 = String8(str, env->GetStringLength(params));
729 env->ReleaseStringCritical(params, str);
730 }
731 if (camera->setParameters(params8) != NO_ERROR) {
Elliott Hughes69a017b2011-04-08 14:10:28 -0700732 jniThrowRuntimeException(env, "setParameters failed");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800733 return;
734 }
735}
736
737static jstring android_hardware_Camera_getParameters(JNIEnv *env, jobject thiz)
738{
Steve Block71f2cf12011-10-20 11:56:00 +0100739 ALOGV("getParameters");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800740 sp<Camera> camera = get_native_camera(env, thiz, NULL);
741 if (camera == 0) return 0;
742
Wu-cheng Lia1c41e12012-02-23 19:01:00 -0800743 String8 params8 = camera->getParameters();
744 if (params8.isEmpty()) {
745 jniThrowRuntimeException(env, "getParameters failed (empty parameters)");
746 return 0;
747 }
748 return env->NewStringUTF(params8.string());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800749}
750
751static void android_hardware_Camera_reconnect(JNIEnv *env, jobject thiz)
752{
Steve Block71f2cf12011-10-20 11:56:00 +0100753 ALOGV("reconnect");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800754 sp<Camera> camera = get_native_camera(env, thiz, NULL);
755 if (camera == 0) return;
756
757 if (camera->reconnect() != NO_ERROR) {
758 jniThrowException(env, "java/io/IOException", "reconnect failed");
759 return;
760 }
761}
762
Wu-cheng Liffe1cf22009-09-10 16:49:17 +0800763static void android_hardware_Camera_lock(JNIEnv *env, jobject thiz)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800764{
Steve Block71f2cf12011-10-20 11:56:00 +0100765 ALOGV("lock");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800766 sp<Camera> camera = get_native_camera(env, thiz, NULL);
Wu-cheng Liffe1cf22009-09-10 16:49:17 +0800767 if (camera == 0) return;
768
769 if (camera->lock() != NO_ERROR) {
Elliott Hughes69a017b2011-04-08 14:10:28 -0700770 jniThrowRuntimeException(env, "lock failed");
Wu-cheng Liffe1cf22009-09-10 16:49:17 +0800771 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800772}
773
Wu-cheng Liffe1cf22009-09-10 16:49:17 +0800774static void android_hardware_Camera_unlock(JNIEnv *env, jobject thiz)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800775{
Steve Block71f2cf12011-10-20 11:56:00 +0100776 ALOGV("unlock");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800777 sp<Camera> camera = get_native_camera(env, thiz, NULL);
Wu-cheng Liffe1cf22009-09-10 16:49:17 +0800778 if (camera == 0) return;
779
780 if (camera->unlock() != NO_ERROR) {
Elliott Hughes69a017b2011-04-08 14:10:28 -0700781 jniThrowRuntimeException(env, "unlock failed");
Wu-cheng Liffe1cf22009-09-10 16:49:17 +0800782 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800783}
784
Wu-cheng Li36f68b82009-09-28 16:14:58 -0700785static void android_hardware_Camera_startSmoothZoom(JNIEnv *env, jobject thiz, jint value)
786{
Steve Block71f2cf12011-10-20 11:56:00 +0100787 ALOGV("startSmoothZoom");
Wu-cheng Li36f68b82009-09-28 16:14:58 -0700788 sp<Camera> camera = get_native_camera(env, thiz, NULL);
789 if (camera == 0) return;
790
Wu-cheng Li0ca25192010-03-29 16:21:12 +0800791 status_t rc = camera->sendCommand(CAMERA_CMD_START_SMOOTH_ZOOM, value, 0);
792 if (rc == BAD_VALUE) {
793 char msg[64];
794 sprintf(msg, "invalid zoom value=%d", value);
795 jniThrowException(env, "java/lang/IllegalArgumentException", msg);
796 } else if (rc != NO_ERROR) {
Elliott Hughes69a017b2011-04-08 14:10:28 -0700797 jniThrowRuntimeException(env, "start smooth zoom failed");
Wu-cheng Li36f68b82009-09-28 16:14:58 -0700798 }
799}
800
801static void android_hardware_Camera_stopSmoothZoom(JNIEnv *env, jobject thiz)
802{
Steve Block71f2cf12011-10-20 11:56:00 +0100803 ALOGV("stopSmoothZoom");
Wu-cheng Li36f68b82009-09-28 16:14:58 -0700804 sp<Camera> camera = get_native_camera(env, thiz, NULL);
805 if (camera == 0) return;
806
807 if (camera->sendCommand(CAMERA_CMD_STOP_SMOOTH_ZOOM, 0, 0) != NO_ERROR) {
Elliott Hughes69a017b2011-04-08 14:10:28 -0700808 jniThrowRuntimeException(env, "stop smooth zoom failed");
Wu-cheng Li36f68b82009-09-28 16:14:58 -0700809 }
810}
811
Chih-Chung Changd1d77062010-01-22 17:49:48 -0800812static void android_hardware_Camera_setDisplayOrientation(JNIEnv *env, jobject thiz,
813 jint value)
814{
Steve Block71f2cf12011-10-20 11:56:00 +0100815 ALOGV("setDisplayOrientation");
Chih-Chung Changd1d77062010-01-22 17:49:48 -0800816 sp<Camera> camera = get_native_camera(env, thiz, NULL);
817 if (camera == 0) return;
818
819 if (camera->sendCommand(CAMERA_CMD_SET_DISPLAY_ORIENTATION, value, 0) != NO_ERROR) {
Elliott Hughes69a017b2011-04-08 14:10:28 -0700820 jniThrowRuntimeException(env, "set display orientation failed");
Chih-Chung Changd1d77062010-01-22 17:49:48 -0800821 }
822}
823
Eino-Ville Talvala69fe5272012-09-07 12:26:40 -0700824static jboolean android_hardware_Camera_enableShutterSound(JNIEnv *env, jobject thiz,
825 jboolean enabled)
826{
827 ALOGV("enableShutterSound");
828 sp<Camera> camera = get_native_camera(env, thiz, NULL);
829 if (camera == 0) return JNI_FALSE;
830
831 int32_t value = (enabled == JNI_TRUE) ? 1 : 0;
832 status_t rc = camera->sendCommand(CAMERA_CMD_ENABLE_SHUTTER_SOUND, value, 0);
833 if (rc == NO_ERROR) {
834 return JNI_TRUE;
835 } else if (rc == PERMISSION_DENIED) {
836 return JNI_FALSE;
837 } else {
838 jniThrowRuntimeException(env, "enable shutter sound failed");
839 return JNI_FALSE;
840 }
841}
842
Wu-cheng Li4c2292e2011-07-22 02:37:11 +0800843static void android_hardware_Camera_startFaceDetection(JNIEnv *env, jobject thiz,
Wu-cheng Libb1e2752011-07-30 05:00:37 +0800844 jint type)
Wu-cheng Li4c2292e2011-07-22 02:37:11 +0800845{
Steve Block71f2cf12011-10-20 11:56:00 +0100846 ALOGV("startFaceDetection");
Wu-cheng Li4c2292e2011-07-22 02:37:11 +0800847 JNICameraContext* context;
848 sp<Camera> camera = get_native_camera(env, thiz, &context);
849 if (camera == 0) return;
850
851 status_t rc = camera->sendCommand(CAMERA_CMD_START_FACE_DETECTION, type, 0);
852 if (rc == BAD_VALUE) {
853 char msg[64];
854 snprintf(msg, sizeof(msg), "invalid face detection type=%d", type);
855 jniThrowException(env, "java/lang/IllegalArgumentException", msg);
856 } else if (rc != NO_ERROR) {
857 jniThrowRuntimeException(env, "start face detection failed");
858 }
859}
860
861static void android_hardware_Camera_stopFaceDetection(JNIEnv *env, jobject thiz)
862{
Steve Block71f2cf12011-10-20 11:56:00 +0100863 ALOGV("stopFaceDetection");
Wu-cheng Li4c2292e2011-07-22 02:37:11 +0800864 sp<Camera> camera = get_native_camera(env, thiz, NULL);
865 if (camera == 0) return;
866
867 if (camera->sendCommand(CAMERA_CMD_STOP_FACE_DETECTION, 0, 0) != NO_ERROR) {
868 jniThrowRuntimeException(env, "stop face detection failed");
869 }
870}
871
Wu-cheng Li9d062cf2011-11-14 20:30:14 +0800872static void android_hardware_Camera_enableFocusMoveCallback(JNIEnv *env, jobject thiz, jint enable)
873{
Wu-cheng Li02097522011-11-30 11:06:04 +0800874 ALOGV("enableFocusMoveCallback");
Wu-cheng Li9d062cf2011-11-14 20:30:14 +0800875 sp<Camera> camera = get_native_camera(env, thiz, NULL);
876 if (camera == 0) return;
877
878 if (camera->sendCommand(CAMERA_CMD_ENABLE_FOCUS_MOVE_MSG, enable, 0) != NO_ERROR) {
879 jniThrowRuntimeException(env, "enable focus move callback failed");
880 }
881}
882
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800883//-------------------------------------------------
884
885static JNINativeMethod camMethods[] = {
Chih-Chung Change25cc652010-05-06 16:36:58 +0800886 { "getNumberOfCameras",
887 "()I",
888 (void *)android_hardware_Camera_getNumberOfCameras },
Eino-Ville Talvala4f8e5ce2012-10-08 18:16:35 -0700889 { "_getCameraInfo",
Chih-Chung Changb8bb78f2010-06-10 13:32:16 +0800890 "(ILandroid/hardware/Camera$CameraInfo;)V",
891 (void*)android_hardware_Camera_getCameraInfo },
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800892 { "native_setup",
Eino-Ville Talvala788717c2013-02-15 18:30:15 -0800893 "(Ljava/lang/Object;ILjava/lang/String;)V",
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800894 (void*)android_hardware_Camera_native_setup },
895 { "native_release",
896 "()V",
897 (void*)android_hardware_Camera_release },
898 { "setPreviewDisplay",
899 "(Landroid/view/Surface;)V",
900 (void *)android_hardware_Camera_setPreviewDisplay },
Jamie Gennisfd6f39e2010-12-20 12:15:00 -0800901 { "setPreviewTexture",
902 "(Landroid/graphics/SurfaceTexture;)V",
903 (void *)android_hardware_Camera_setPreviewTexture },
Eino-Ville Talvala7005b672013-04-02 15:46:38 -0700904 { "setPreviewCallbackSurface",
905 "(Landroid/view/Surface;)V",
906 (void *)android_hardware_Camera_setPreviewCallbackSurface },
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800907 { "startPreview",
908 "()V",
909 (void *)android_hardware_Camera_startPreview },
Wu-cheng Li4c2292e2011-07-22 02:37:11 +0800910 { "_stopPreview",
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800911 "()V",
912 (void *)android_hardware_Camera_stopPreview },
913 { "previewEnabled",
914 "()Z",
915 (void *)android_hardware_Camera_previewEnabled },
916 { "setHasPreviewCallback",
917 "(ZZ)V",
918 (void *)android_hardware_Camera_setHasPreviewCallback },
James Donge00cab72011-02-17 16:38:06 -0800919 { "_addCallbackBuffer",
920 "([BI)V",
Andrew Harp94927df2009-10-20 01:47:05 -0400921 (void *)android_hardware_Camera_addCallbackBuffer },
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800922 { "native_autoFocus",
923 "()V",
924 (void *)android_hardware_Camera_autoFocus },
Chih-Chung Chang244f8c22009-09-15 14:51:56 +0800925 { "native_cancelAutoFocus",
926 "()V",
927 (void *)android_hardware_Camera_cancelAutoFocus },
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800928 { "native_takePicture",
James Donge00cab72011-02-17 16:38:06 -0800929 "(I)V",
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800930 (void *)android_hardware_Camera_takePicture },
931 { "native_setParameters",
932 "(Ljava/lang/String;)V",
933 (void *)android_hardware_Camera_setParameters },
934 { "native_getParameters",
935 "()Ljava/lang/String;",
936 (void *)android_hardware_Camera_getParameters },
937 { "reconnect",
938 "()V",
939 (void*)android_hardware_Camera_reconnect },
940 { "lock",
Wu-cheng Liffe1cf22009-09-10 16:49:17 +0800941 "()V",
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800942 (void*)android_hardware_Camera_lock },
943 { "unlock",
Wu-cheng Liffe1cf22009-09-10 16:49:17 +0800944 "()V",
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800945 (void*)android_hardware_Camera_unlock },
Wu-cheng Li36f68b82009-09-28 16:14:58 -0700946 { "startSmoothZoom",
947 "(I)V",
948 (void *)android_hardware_Camera_startSmoothZoom },
949 { "stopSmoothZoom",
950 "()V",
951 (void *)android_hardware_Camera_stopSmoothZoom },
Chih-Chung Changd1d77062010-01-22 17:49:48 -0800952 { "setDisplayOrientation",
953 "(I)V",
954 (void *)android_hardware_Camera_setDisplayOrientation },
Eino-Ville Talvala4f8e5ce2012-10-08 18:16:35 -0700955 { "_enableShutterSound",
Eino-Ville Talvala69fe5272012-09-07 12:26:40 -0700956 "(Z)Z",
957 (void *)android_hardware_Camera_enableShutterSound },
Wu-cheng Li4c2292e2011-07-22 02:37:11 +0800958 { "_startFaceDetection",
959 "(I)V",
960 (void *)android_hardware_Camera_startFaceDetection },
961 { "_stopFaceDetection",
962 "()V",
963 (void *)android_hardware_Camera_stopFaceDetection},
Wu-cheng Li9d062cf2011-11-14 20:30:14 +0800964 { "enableFocusMoveCallback",
965 "(I)V",
966 (void *)android_hardware_Camera_enableFocusMoveCallback},
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800967};
968
969struct field {
970 const char *class_name;
971 const char *field_name;
972 const char *field_type;
973 jfieldID *jfield;
974};
975
976static int find_fields(JNIEnv *env, field *fields, int count)
977{
978 for (int i = 0; i < count; i++) {
979 field *f = &fields[i];
980 jclass clazz = env->FindClass(f->class_name);
981 if (clazz == NULL) {
Steve Block3762c312012-01-06 19:20:56 +0000982 ALOGE("Can't find %s", f->class_name);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800983 return -1;
984 }
985
986 jfieldID field = env->GetFieldID(clazz, f->field_name, f->field_type);
987 if (field == NULL) {
Steve Block3762c312012-01-06 19:20:56 +0000988 ALOGE("Can't find %s.%s", f->class_name, f->field_name);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800989 return -1;
990 }
991
992 *(f->jfield) = field;
993 }
994
995 return 0;
996}
997
998// Get all the required offsets in java class and register native functions
999int register_android_hardware_Camera(JNIEnv *env)
1000{
1001 field fields_to_find[] = {
Ashok Bhat4838e332014-01-03 14:37:19 +00001002 { "android/hardware/Camera", "mNativeContext", "J", &fields.context },
Wu-cheng Li3fd5fa42010-09-15 16:06:20 -07001003 { "android/hardware/Camera$CameraInfo", "facing", "I", &fields.facing },
1004 { "android/hardware/Camera$CameraInfo", "orientation", "I", &fields.orientation },
Eino-Ville Talvalaf7c6c5a2012-09-19 11:46:11 -07001005 { "android/hardware/Camera$CameraInfo", "canDisableShutterSound", "Z",
1006 &fields.canDisableShutterSound },
Wu-cheng Lif0d6a482011-07-28 05:30:59 +08001007 { "android/hardware/Camera$Face", "rect", "Landroid/graphics/Rect;", &fields.face_rect },
1008 { "android/hardware/Camera$Face", "score", "I", &fields.face_score },
Wu-cheng Li4c2292e2011-07-22 02:37:11 +08001009 { "android/graphics/Rect", "left", "I", &fields.rect_left },
1010 { "android/graphics/Rect", "top", "I", &fields.rect_top },
1011 { "android/graphics/Rect", "right", "I", &fields.rect_right },
1012 { "android/graphics/Rect", "bottom", "I", &fields.rect_bottom },
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001013 };
1014
1015 if (find_fields(env, fields_to_find, NELEM(fields_to_find)) < 0)
1016 return -1;
1017
1018 jclass clazz = env->FindClass("android/hardware/Camera");
1019 fields.post_event = env->GetStaticMethodID(clazz, "postEventFromNative",
1020 "(Ljava/lang/Object;IIILjava/lang/Object;)V");
1021 if (fields.post_event == NULL) {
Steve Block3762c312012-01-06 19:20:56 +00001022 ALOGE("Can't find android/hardware/Camera.postEventFromNative");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001023 return -1;
1024 }
1025
Wu-cheng Libb1e2752011-07-30 05:00:37 +08001026 clazz = env->FindClass("android/graphics/Rect");
1027 fields.rect_constructor = env->GetMethodID(clazz, "<init>", "()V");
1028 if (fields.rect_constructor == NULL) {
Steve Block3762c312012-01-06 19:20:56 +00001029 ALOGE("Can't find android/graphics/Rect.Rect()");
Wu-cheng Libb1e2752011-07-30 05:00:37 +08001030 return -1;
1031 }
1032
1033 clazz = env->FindClass("android/hardware/Camera$Face");
1034 fields.face_constructor = env->GetMethodID(clazz, "<init>", "()V");
1035 if (fields.face_constructor == NULL) {
Steve Block3762c312012-01-06 19:20:56 +00001036 ALOGE("Can't find android/hardware/Camera$Face.Face()");
Wu-cheng Libb1e2752011-07-30 05:00:37 +08001037 return -1;
1038 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001039
1040 // Register native functions
1041 return AndroidRuntime::registerNativeMethods(env, "android/hardware/Camera",
1042 camMethods, NELEM(camMethods));
1043}