blob: 0c66b867c8dc6668ee3ab941dbbf13e720f8e20f [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"
25
Andrew Harp94927df2009-10-20 01:47:05 -040026#include <utils/Vector.h>
27
Jamie Gennisfd6f39e2010-12-20 12:15:00 -080028#include <gui/SurfaceTexture.h>
Mathias Agopian8335f1c2012-02-25 18:48:35 -080029#include <gui/Surface.h>
Mathias Agopian000479f2010-02-09 17:46:37 -080030#include <camera/Camera.h>
Mathias Agopian07952722009-05-19 19:08:10 -070031#include <binder/IMemory.h>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080032
33using namespace android;
34
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080035struct fields_t {
36 jfieldID context;
37 jfieldID surface;
Jamie Gennisfd6f39e2010-12-20 12:15:00 -080038 jfieldID surfaceTexture;
Chih-Chung Changb8bb78f2010-06-10 13:32:16 +080039 jfieldID facing;
40 jfieldID orientation;
Wu-cheng Lif0d6a482011-07-28 05:30:59 +080041 jfieldID face_rect;
Wu-cheng Li4c2292e2011-07-22 02:37:11 +080042 jfieldID face_score;
43 jfieldID rect_left;
44 jfieldID rect_top;
45 jfieldID rect_right;
46 jfieldID rect_bottom;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080047 jmethodID post_event;
Wu-cheng Libb1e2752011-07-30 05:00:37 +080048 jmethodID rect_constructor;
49 jmethodID face_constructor;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080050};
51
52static fields_t fields;
53static Mutex sLock;
54
Dave Sparks5e271152009-06-23 17:30:11 -070055// provides persistent context for calls from native code to Java
56class JNICameraContext: public CameraListener
57{
58public:
59 JNICameraContext(JNIEnv* env, jobject weak_this, jclass clazz, const sp<Camera>& camera);
60 ~JNICameraContext() { release(); }
61 virtual void notify(int32_t msgType, int32_t ext1, int32_t ext2);
Wu-cheng Libb1e2752011-07-30 05:00:37 +080062 virtual void postData(int32_t msgType, const sp<IMemory>& dataPtr,
63 camera_frame_metadata_t *metadata);
Dave Sparks59c1a932009-07-08 15:56:53 -070064 virtual void postDataTimestamp(nsecs_t timestamp, int32_t msgType, const sp<IMemory>& dataPtr);
Wu-cheng Libb1e2752011-07-30 05:00:37 +080065 void postMetadata(JNIEnv *env, int32_t msgType, camera_frame_metadata_t *metadata);
James Donge00cab72011-02-17 16:38:06 -080066 void addCallbackBuffer(JNIEnv *env, jbyteArray cbb, int msgType);
Andrew Harp94927df2009-10-20 01:47:05 -040067 void setCallbackMode(JNIEnv *env, bool installed, bool manualMode);
Dave Sparks5e271152009-06-23 17:30:11 -070068 sp<Camera> getCamera() { Mutex::Autolock _l(mLock); return mCamera; }
James Donge00cab72011-02-17 16:38:06 -080069 bool isRawImageCallbackBufferAvailable() const;
Dave Sparks5e271152009-06-23 17:30:11 -070070 void release();
71
72private:
73 void copyAndPost(JNIEnv* env, const sp<IMemory>& dataPtr, int msgType);
James Donge00cab72011-02-17 16:38:06 -080074 void clearCallbackBuffers_l(JNIEnv *env, Vector<jbyteArray> *buffers);
Andrew Harp94927df2009-10-20 01:47:05 -040075 void clearCallbackBuffers_l(JNIEnv *env);
James Donge00cab72011-02-17 16:38:06 -080076 jbyteArray getCallbackBuffer(JNIEnv *env, Vector<jbyteArray> *buffers, size_t bufferSize);
Dave Sparks5e271152009-06-23 17:30:11 -070077
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080078 jobject mCameraJObjectWeak; // weak reference to java object
79 jclass mCameraJClass; // strong reference to java class
Wu-cheng Liffe1cf22009-09-10 16:49:17 +080080 sp<Camera> mCamera; // strong reference to native object
Wu-cheng Libb1e2752011-07-30 05:00:37 +080081 jclass mFaceClass; // strong reference to Face class
82 jclass mRectClass; // strong reference to Rect class
Dave Sparks5e271152009-06-23 17:30:11 -070083 Mutex mLock;
Andrew Harp94927df2009-10-20 01:47:05 -040084
James Donge00cab72011-02-17 16:38:06 -080085 /*
86 * Global reference application-managed raw image buffer queue.
87 *
88 * Manual-only mode is supported for raw image callbacks, which is
89 * set whenever method addCallbackBuffer() with msgType =
90 * CAMERA_MSG_RAW_IMAGE is called; otherwise, null is returned
91 * with raw image callbacks.
92 */
93 Vector<jbyteArray> mRawImageCallbackBuffers;
94
95 /*
96 * Application-managed preview buffer queue and the flags
97 * associated with the usage of the preview buffer callback.
98 */
Andrew Harp94927df2009-10-20 01:47:05 -040099 Vector<jbyteArray> mCallbackBuffers; // Global reference application managed byte[]
100 bool mManualBufferMode; // Whether to use application managed buffers.
James Donge00cab72011-02-17 16:38:06 -0800101 bool mManualCameraCallbackSet; // Whether the callback has been set, used to
102 // reduce unnecessary calls to set the callback.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800103};
104
James Donge00cab72011-02-17 16:38:06 -0800105bool JNICameraContext::isRawImageCallbackBufferAvailable() const
106{
107 return !mRawImageCallbackBuffers.isEmpty();
108}
109
Dave Sparks5e271152009-06-23 17:30:11 -0700110sp<Camera> get_native_camera(JNIEnv *env, jobject thiz, JNICameraContext** pContext)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800111{
112 sp<Camera> camera;
113 Mutex::Autolock _l(sLock);
Dave Sparks5e271152009-06-23 17:30:11 -0700114 JNICameraContext* context = reinterpret_cast<JNICameraContext*>(env->GetIntField(thiz, fields.context));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800115 if (context != NULL) {
Dave Sparks5e271152009-06-23 17:30:11 -0700116 camera = context->getCamera();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800117 }
Steve Block71f2cf12011-10-20 11:56:00 +0100118 ALOGV("get_native_camera: context=%p, camera=%p", context, camera.get());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800119 if (camera == 0) {
Elliott Hughes69a017b2011-04-08 14:10:28 -0700120 jniThrowRuntimeException(env, "Method called after release()");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800121 }
122
123 if (pContext != NULL) *pContext = context;
124 return camera;
125}
126
Dave Sparks5e271152009-06-23 17:30:11 -0700127JNICameraContext::JNICameraContext(JNIEnv* env, jobject weak_this, jclass clazz, const sp<Camera>& camera)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800128{
Dave Sparks5e271152009-06-23 17:30:11 -0700129 mCameraJObjectWeak = env->NewGlobalRef(weak_this);
130 mCameraJClass = (jclass)env->NewGlobalRef(clazz);
131 mCamera = camera;
Andrew Harp94927df2009-10-20 01:47:05 -0400132
Wu-cheng Libb1e2752011-07-30 05:00:37 +0800133 jclass faceClazz = env->FindClass("android/hardware/Camera$Face");
134 mFaceClass = (jclass) env->NewGlobalRef(faceClazz);
135
136 jclass rectClazz = env->FindClass("android/graphics/Rect");
137 mRectClass = (jclass) env->NewGlobalRef(rectClazz);
138
Andrew Harp94927df2009-10-20 01:47:05 -0400139 mManualBufferMode = false;
140 mManualCameraCallbackSet = false;
Dave Sparks5e271152009-06-23 17:30:11 -0700141}
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800142
Dave Sparks5e271152009-06-23 17:30:11 -0700143void JNICameraContext::release()
144{
Steve Block71f2cf12011-10-20 11:56:00 +0100145 ALOGV("release");
Dave Sparks5e271152009-06-23 17:30:11 -0700146 Mutex::Autolock _l(mLock);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800147 JNIEnv *env = AndroidRuntime::getJNIEnv();
Dave Sparks5e271152009-06-23 17:30:11 -0700148
149 if (mCameraJObjectWeak != NULL) {
150 env->DeleteGlobalRef(mCameraJObjectWeak);
151 mCameraJObjectWeak = NULL;
152 }
153 if (mCameraJClass != NULL) {
154 env->DeleteGlobalRef(mCameraJClass);
155 mCameraJClass = NULL;
156 }
Wu-cheng Libb1e2752011-07-30 05:00:37 +0800157 if (mFaceClass != NULL) {
158 env->DeleteGlobalRef(mFaceClass);
159 mFaceClass = NULL;
160 }
161 if (mRectClass != NULL) {
162 env->DeleteGlobalRef(mRectClass);
163 mRectClass = NULL;
164 }
Andrew Harp94927df2009-10-20 01:47:05 -0400165 clearCallbackBuffers_l(env);
Dave Sparks5e271152009-06-23 17:30:11 -0700166 mCamera.clear();
167}
168
169void JNICameraContext::notify(int32_t msgType, int32_t ext1, int32_t ext2)
170{
Steve Block71f2cf12011-10-20 11:56:00 +0100171 ALOGV("notify");
Dave Sparks5e271152009-06-23 17:30:11 -0700172
173 // VM pointer will be NULL if object is released
174 Mutex::Autolock _l(mLock);
175 if (mCameraJObjectWeak == NULL) {
Steve Block8564c8d2012-01-05 23:22:43 +0000176 ALOGW("callback on dead camera object");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800177 return;
178 }
Dave Sparks5e271152009-06-23 17:30:11 -0700179 JNIEnv *env = AndroidRuntime::getJNIEnv();
James Donge00cab72011-02-17 16:38:06 -0800180
181 /*
182 * If the notification or msgType is CAMERA_MSG_RAW_IMAGE_NOTIFY, change it
183 * to CAMERA_MSG_RAW_IMAGE since CAMERA_MSG_RAW_IMAGE_NOTIFY is not exposed
184 * to the Java app.
185 */
186 if (msgType == CAMERA_MSG_RAW_IMAGE_NOTIFY) {
187 msgType = CAMERA_MSG_RAW_IMAGE;
188 }
189
Dave Sparksc62f9bd2009-06-26 13:33:32 -0700190 env->CallStaticVoidMethod(mCameraJClass, fields.post_event,
Chih-Chung Chang6157de02009-09-24 15:29:35 -0700191 mCameraJObjectWeak, msgType, ext1, ext2, NULL);
Dave Sparks5e271152009-06-23 17:30:11 -0700192}
193
James Donge00cab72011-02-17 16:38:06 -0800194jbyteArray JNICameraContext::getCallbackBuffer(
195 JNIEnv* env, Vector<jbyteArray>* buffers, size_t bufferSize)
196{
197 jbyteArray obj = NULL;
198
199 // Vector access should be protected by lock in postData()
200 if (!buffers->isEmpty()) {
Steve Block71f2cf12011-10-20 11:56:00 +0100201 ALOGV("Using callback buffer from queue of length %d", buffers->size());
James Donge00cab72011-02-17 16:38:06 -0800202 jbyteArray globalBuffer = buffers->itemAt(0);
203 buffers->removeAt(0);
204
205 obj = (jbyteArray)env->NewLocalRef(globalBuffer);
206 env->DeleteGlobalRef(globalBuffer);
207
208 if (obj != NULL) {
209 jsize bufferLength = env->GetArrayLength(obj);
210 if ((int)bufferLength < (int)bufferSize) {
Steve Block3762c312012-01-06 19:20:56 +0000211 ALOGE("Callback buffer was too small! Expected %d bytes, but got %d bytes!",
James Donge00cab72011-02-17 16:38:06 -0800212 bufferSize, bufferLength);
213 env->DeleteLocalRef(obj);
214 return NULL;
215 }
216 }
217 }
218
219 return obj;
220}
221
Dave Sparks5e271152009-06-23 17:30:11 -0700222void JNICameraContext::copyAndPost(JNIEnv* env, const sp<IMemory>& dataPtr, int msgType)
223{
224 jbyteArray obj = NULL;
225
226 // allocate Java byte array and copy data
227 if (dataPtr != NULL) {
228 ssize_t offset;
229 size_t size;
230 sp<IMemoryHeap> heap = dataPtr->getMemory(&offset, &size);
Steve Block71f2cf12011-10-20 11:56:00 +0100231 ALOGV("copyAndPost: off=%ld, size=%d", offset, size);
Dave Sparks5e271152009-06-23 17:30:11 -0700232 uint8_t *heapBase = (uint8_t*)heap->base();
233
234 if (heapBase != NULL) {
Dave Sparksc4ca4202009-07-13 09:38:20 -0700235 const jbyte* data = reinterpret_cast<const jbyte*>(heapBase + offset);
Andrew Harp94927df2009-10-20 01:47:05 -0400236
Wu-cheng Li5f0ef5f2011-09-21 16:40:23 +0800237 if (msgType == CAMERA_MSG_RAW_IMAGE) {
238 obj = getCallbackBuffer(env, &mRawImageCallbackBuffers, size);
239 } else if (msgType == CAMERA_MSG_PREVIEW_FRAME && mManualBufferMode) {
240 obj = getCallbackBuffer(env, &mCallbackBuffers, size);
Andrew Harp94927df2009-10-20 01:47:05 -0400241
Wu-cheng Li5f0ef5f2011-09-21 16:40:23 +0800242 if (mCallbackBuffers.isEmpty()) {
Steve Block71f2cf12011-10-20 11:56:00 +0100243 ALOGV("Out of buffers, clearing callback!");
Wu-cheng Li5f0ef5f2011-09-21 16:40:23 +0800244 mCamera->setPreviewCallbackFlags(CAMERA_FRAME_CALLBACK_FLAG_NOOP);
245 mManualCameraCallbackSet = false;
Andrew Harp94927df2009-10-20 01:47:05 -0400246
Wu-cheng Li5f0ef5f2011-09-21 16:40:23 +0800247 if (obj == NULL) {
Andrew Harp94927df2009-10-20 01:47:05 -0400248 return;
249 }
250 }
Wu-cheng Li5f0ef5f2011-09-21 16:40:23 +0800251 } else {
Steve Block71f2cf12011-10-20 11:56:00 +0100252 ALOGV("Allocating callback buffer");
Wu-cheng Li5f0ef5f2011-09-21 16:40:23 +0800253 obj = env->NewByteArray(size);
Andrew Harp94927df2009-10-20 01:47:05 -0400254 }
255
Dave Sparks5e271152009-06-23 17:30:11 -0700256 if (obj == NULL) {
Steve Block3762c312012-01-06 19:20:56 +0000257 ALOGE("Couldn't allocate byte array for JPEG data");
Dave Sparks5e271152009-06-23 17:30:11 -0700258 env->ExceptionClear();
259 } else {
Dave Sparksa95f4952009-07-10 18:13:36 -0700260 env->SetByteArrayRegion(obj, 0, size, data);
Dave Sparks5e271152009-06-23 17:30:11 -0700261 }
262 } else {
Steve Block3762c312012-01-06 19:20:56 +0000263 ALOGE("image heap is NULL");
Dave Sparks5e271152009-06-23 17:30:11 -0700264 }
265 }
266
267 // post image data to Java
268 env->CallStaticVoidMethod(mCameraJClass, fields.post_event,
269 mCameraJObjectWeak, msgType, 0, 0, obj);
270 if (obj) {
271 env->DeleteLocalRef(obj);
272 }
273}
274
Wu-cheng Libb1e2752011-07-30 05:00:37 +0800275void JNICameraContext::postData(int32_t msgType, const sp<IMemory>& dataPtr,
276 camera_frame_metadata_t *metadata)
Dave Sparks5e271152009-06-23 17:30:11 -0700277{
278 // VM pointer will be NULL if object is released
279 Mutex::Autolock _l(mLock);
280 JNIEnv *env = AndroidRuntime::getJNIEnv();
Dave Sparksd0cbb1a2009-06-29 19:03:33 -0700281 if (mCameraJObjectWeak == NULL) {
Steve Block8564c8d2012-01-05 23:22:43 +0000282 ALOGW("callback on dead camera object");
Dave Sparksd0cbb1a2009-06-29 19:03:33 -0700283 return;
284 }
Dave Sparks5e271152009-06-23 17:30:11 -0700285
Wu-cheng Libb1e2752011-07-30 05:00:37 +0800286 int32_t dataMsgType = msgType & ~CAMERA_MSG_PREVIEW_METADATA;
287
Dave Sparks5e271152009-06-23 17:30:11 -0700288 // return data based on callback type
Wu-cheng Libb1e2752011-07-30 05:00:37 +0800289 switch (dataMsgType) {
James Donge00cab72011-02-17 16:38:06 -0800290 case CAMERA_MSG_VIDEO_FRAME:
291 // should never happen
292 break;
293
294 // For backward-compatibility purpose, if there is no callback
295 // buffer for raw image, the callback returns null.
296 case CAMERA_MSG_RAW_IMAGE:
Steve Block71f2cf12011-10-20 11:56:00 +0100297 ALOGV("rawCallback");
James Donge00cab72011-02-17 16:38:06 -0800298 if (mRawImageCallbackBuffers.isEmpty()) {
299 env->CallStaticVoidMethod(mCameraJClass, fields.post_event,
Wu-cheng Libb1e2752011-07-30 05:00:37 +0800300 mCameraJObjectWeak, dataMsgType, 0, 0, NULL);
James Donge00cab72011-02-17 16:38:06 -0800301 } else {
Wu-cheng Libb1e2752011-07-30 05:00:37 +0800302 copyAndPost(env, dataPtr, dataMsgType);
James Donge00cab72011-02-17 16:38:06 -0800303 }
304 break;
305
Wu-cheng Libb1e2752011-07-30 05:00:37 +0800306 // There is no data.
307 case 0:
James Donge00cab72011-02-17 16:38:06 -0800308 break;
Wu-cheng Libb1e2752011-07-30 05:00:37 +0800309
310 default:
Steve Block71f2cf12011-10-20 11:56:00 +0100311 ALOGV("dataCallback(%d, %p)", dataMsgType, dataPtr.get());
Wu-cheng Libb1e2752011-07-30 05:00:37 +0800312 copyAndPost(env, dataPtr, dataMsgType);
313 break;
314 }
315
316 // post frame metadata to Java
317 if (metadata && (msgType & CAMERA_MSG_PREVIEW_METADATA)) {
318 postMetadata(env, CAMERA_MSG_PREVIEW_METADATA, metadata);
Dave Sparks5e271152009-06-23 17:30:11 -0700319 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800320}
321
Dave Sparks59c1a932009-07-08 15:56:53 -0700322void JNICameraContext::postDataTimestamp(nsecs_t timestamp, int32_t msgType, const sp<IMemory>& dataPtr)
323{
324 // TODO: plumb up to Java. For now, just drop the timestamp
Wu-cheng Libb1e2752011-07-30 05:00:37 +0800325 postData(msgType, dataPtr, NULL);
326}
327
328void JNICameraContext::postMetadata(JNIEnv *env, int32_t msgType, camera_frame_metadata_t *metadata)
329{
330 jobjectArray obj = NULL;
331 obj = (jobjectArray) env->NewObjectArray(metadata->number_of_faces,
332 mFaceClass, NULL);
333 if (obj == NULL) {
Steve Block3762c312012-01-06 19:20:56 +0000334 ALOGE("Couldn't allocate face metadata array");
Wu-cheng Libb1e2752011-07-30 05:00:37 +0800335 return;
336 }
337
338 for (int i = 0; i < metadata->number_of_faces; i++) {
339 jobject face = env->NewObject(mFaceClass, fields.face_constructor);
340 env->SetObjectArrayElement(obj, i, face);
341
342 jobject rect = env->NewObject(mRectClass, fields.rect_constructor);
343 env->SetIntField(rect, fields.rect_left, metadata->faces[i].rect[0]);
344 env->SetIntField(rect, fields.rect_top, metadata->faces[i].rect[1]);
345 env->SetIntField(rect, fields.rect_right, metadata->faces[i].rect[2]);
346 env->SetIntField(rect, fields.rect_bottom, metadata->faces[i].rect[3]);
347
348 env->SetObjectField(face, fields.face_rect, rect);
349 env->SetIntField(face, fields.face_score, metadata->faces[i].score);
350
351 env->DeleteLocalRef(face);
352 env->DeleteLocalRef(rect);
353 }
354 env->CallStaticVoidMethod(mCameraJClass, fields.post_event,
355 mCameraJObjectWeak, msgType, 0, 0, obj);
356 env->DeleteLocalRef(obj);
Dave Sparks59c1a932009-07-08 15:56:53 -0700357}
358
Andrew Harp94927df2009-10-20 01:47:05 -0400359void JNICameraContext::setCallbackMode(JNIEnv *env, bool installed, bool manualMode)
360{
361 Mutex::Autolock _l(mLock);
362 mManualBufferMode = manualMode;
363 mManualCameraCallbackSet = false;
364
365 // In order to limit the over usage of binder threads, all non-manual buffer
Iliyan Malchev9c7ac0d2011-04-14 16:51:21 -0700366 // callbacks use CAMERA_FRAME_CALLBACK_FLAG_BARCODE_SCANNER mode now.
Andrew Harp94927df2009-10-20 01:47:05 -0400367 //
368 // Continuous callbacks will have the callback re-registered from handleMessage.
369 // Manual buffer mode will operate as fast as possible, relying on the finite supply
370 // of buffers for throttling.
371
372 if (!installed) {
Iliyan Malchev9c7ac0d2011-04-14 16:51:21 -0700373 mCamera->setPreviewCallbackFlags(CAMERA_FRAME_CALLBACK_FLAG_NOOP);
James Donge00cab72011-02-17 16:38:06 -0800374 clearCallbackBuffers_l(env, &mCallbackBuffers);
Andrew Harp94927df2009-10-20 01:47:05 -0400375 } else if (mManualBufferMode) {
376 if (!mCallbackBuffers.isEmpty()) {
Iliyan Malchev9c7ac0d2011-04-14 16:51:21 -0700377 mCamera->setPreviewCallbackFlags(CAMERA_FRAME_CALLBACK_FLAG_CAMERA);
Andrew Harp94927df2009-10-20 01:47:05 -0400378 mManualCameraCallbackSet = true;
379 }
380 } else {
Iliyan Malchev9c7ac0d2011-04-14 16:51:21 -0700381 mCamera->setPreviewCallbackFlags(CAMERA_FRAME_CALLBACK_FLAG_BARCODE_SCANNER);
James Donge00cab72011-02-17 16:38:06 -0800382 clearCallbackBuffers_l(env, &mCallbackBuffers);
Andrew Harp94927df2009-10-20 01:47:05 -0400383 }
384}
385
James Donge00cab72011-02-17 16:38:06 -0800386void JNICameraContext::addCallbackBuffer(
387 JNIEnv *env, jbyteArray cbb, int msgType)
Andrew Harp94927df2009-10-20 01:47:05 -0400388{
Steve Block71f2cf12011-10-20 11:56:00 +0100389 ALOGV("addCallbackBuffer: 0x%x", msgType);
Andrew Harp94927df2009-10-20 01:47:05 -0400390 if (cbb != NULL) {
391 Mutex::Autolock _l(mLock);
James Donge00cab72011-02-17 16:38:06 -0800392 switch (msgType) {
393 case CAMERA_MSG_PREVIEW_FRAME: {
394 jbyteArray callbackBuffer = (jbyteArray)env->NewGlobalRef(cbb);
395 mCallbackBuffers.push(callbackBuffer);
Andrew Harp94927df2009-10-20 01:47:05 -0400396
Steve Block71f2cf12011-10-20 11:56:00 +0100397 ALOGV("Adding callback buffer to queue, %d total",
James Donge00cab72011-02-17 16:38:06 -0800398 mCallbackBuffers.size());
Andrew Harp94927df2009-10-20 01:47:05 -0400399
James Donge00cab72011-02-17 16:38:06 -0800400 // We want to make sure the camera knows we're ready for the
401 // next frame. This may have come unset had we not had a
402 // callbackbuffer ready for it last time.
403 if (mManualBufferMode && !mManualCameraCallbackSet) {
Iliyan Malchev9c7ac0d2011-04-14 16:51:21 -0700404 mCamera->setPreviewCallbackFlags(CAMERA_FRAME_CALLBACK_FLAG_CAMERA);
James Donge00cab72011-02-17 16:38:06 -0800405 mManualCameraCallbackSet = true;
406 }
407 break;
408 }
409 case CAMERA_MSG_RAW_IMAGE: {
410 jbyteArray callbackBuffer = (jbyteArray)env->NewGlobalRef(cbb);
411 mRawImageCallbackBuffers.push(callbackBuffer);
412 break;
413 }
414 default: {
415 jniThrowException(env,
416 "java/lang/IllegalArgumentException",
417 "Unsupported message type");
418 return;
419 }
Andrew Harp94927df2009-10-20 01:47:05 -0400420 }
421 } else {
Steve Block3762c312012-01-06 19:20:56 +0000422 ALOGE("Null byte array!");
Andrew Harp94927df2009-10-20 01:47:05 -0400423 }
424}
425
426void JNICameraContext::clearCallbackBuffers_l(JNIEnv *env)
427{
James Donge00cab72011-02-17 16:38:06 -0800428 clearCallbackBuffers_l(env, &mCallbackBuffers);
429 clearCallbackBuffers_l(env, &mRawImageCallbackBuffers);
430}
431
432void JNICameraContext::clearCallbackBuffers_l(JNIEnv *env, Vector<jbyteArray> *buffers) {
Steve Block71f2cf12011-10-20 11:56:00 +0100433 ALOGV("Clearing callback buffers, %d remained", buffers->size());
James Donge00cab72011-02-17 16:38:06 -0800434 while (!buffers->isEmpty()) {
435 env->DeleteGlobalRef(buffers->top());
436 buffers->pop();
Andrew Harp94927df2009-10-20 01:47:05 -0400437 }
438}
439
Chih-Chung Change25cc652010-05-06 16:36:58 +0800440static jint android_hardware_Camera_getNumberOfCameras(JNIEnv *env, jobject thiz)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800441{
Chih-Chung Change25cc652010-05-06 16:36:58 +0800442 return Camera::getNumberOfCameras();
443}
444
Chih-Chung Changb8bb78f2010-06-10 13:32:16 +0800445static void android_hardware_Camera_getCameraInfo(JNIEnv *env, jobject thiz,
446 jint cameraId, jobject info_obj)
447{
448 CameraInfo cameraInfo;
449 status_t rc = Camera::getCameraInfo(cameraId, &cameraInfo);
450 if (rc != NO_ERROR) {
Elliott Hughes69a017b2011-04-08 14:10:28 -0700451 jniThrowRuntimeException(env, "Fail to get camera info");
Chih-Chung Changb8bb78f2010-06-10 13:32:16 +0800452 return;
453 }
454 env->SetIntField(info_obj, fields.facing, cameraInfo.facing);
455 env->SetIntField(info_obj, fields.orientation, cameraInfo.orientation);
456}
457
Chih-Chung Change25cc652010-05-06 16:36:58 +0800458// connect to camera service
459static void android_hardware_Camera_native_setup(JNIEnv *env, jobject thiz,
460 jobject weak_this, jint cameraId)
461{
462 sp<Camera> camera = Camera::connect(cameraId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800463
464 if (camera == NULL) {
Elliott Hughes69a017b2011-04-08 14:10:28 -0700465 jniThrowRuntimeException(env, "Fail to connect to camera service");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800466 return;
467 }
468
469 // make sure camera hardware is alive
470 if (camera->getStatus() != NO_ERROR) {
Elliott Hughes69a017b2011-04-08 14:10:28 -0700471 jniThrowRuntimeException(env, "Camera initialization failed");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800472 return;
473 }
474
475 jclass clazz = env->GetObjectClass(thiz);
476 if (clazz == NULL) {
Elliott Hughes69a017b2011-04-08 14:10:28 -0700477 jniThrowRuntimeException(env, "Can't find android/hardware/Camera");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800478 return;
479 }
480
481 // We use a weak reference so the Camera object can be garbage collected.
482 // The reference is only used as a proxy for callbacks.
Dave Sparks5e271152009-06-23 17:30:11 -0700483 sp<JNICameraContext> context = new JNICameraContext(env, weak_this, clazz, camera);
484 context->incStrong(thiz);
485 camera->setListener(context);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800486
487 // save context in opaque field
Dave Sparks5e271152009-06-23 17:30:11 -0700488 env->SetIntField(thiz, fields.context, (int)context.get());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800489}
490
491// disconnect from camera service
492// It's okay to call this when the native camera context is already null.
493// This handles the case where the user has called release() and the
494// finalizer is invoked later.
495static void android_hardware_Camera_release(JNIEnv *env, jobject thiz)
496{
Steve Block71f2cf12011-10-20 11:56:00 +0100497 // TODO: Change to ALOGV
498 ALOGV("release camera");
Dave Sparks5e271152009-06-23 17:30:11 -0700499 JNICameraContext* context = NULL;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800500 sp<Camera> camera;
501 {
502 Mutex::Autolock _l(sLock);
Dave Sparks5e271152009-06-23 17:30:11 -0700503 context = reinterpret_cast<JNICameraContext*>(env->GetIntField(thiz, fields.context));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800504
505 // Make sure we do not attempt to callback on a deleted Java object.
506 env->SetIntField(thiz, fields.context, 0);
507 }
508
509 // clean up if release has not been called before
510 if (context != NULL) {
Dave Sparks5e271152009-06-23 17:30:11 -0700511 camera = context->getCamera();
512 context->release();
Steve Block71f2cf12011-10-20 11:56:00 +0100513 ALOGV("native_release: context=%p camera=%p", context, camera.get());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800514
515 // clear callbacks
516 if (camera != NULL) {
Iliyan Malchev9c7ac0d2011-04-14 16:51:21 -0700517 camera->setPreviewCallbackFlags(CAMERA_FRAME_CALLBACK_FLAG_NOOP);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800518 camera->disconnect();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800519 }
520
521 // remove context to prevent further Java access
Dave Sparks5e271152009-06-23 17:30:11 -0700522 context->decStrong(thiz);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800523 }
524}
525
526static void android_hardware_Camera_setPreviewDisplay(JNIEnv *env, jobject thiz, jobject jSurface)
527{
Steve Block71f2cf12011-10-20 11:56:00 +0100528 ALOGV("setPreviewDisplay");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800529 sp<Camera> camera = get_native_camera(env, thiz, NULL);
530 if (camera == 0) return;
531
Wu-cheng Lib8a10fe2009-06-23 23:37:36 +0800532 sp<Surface> surface = NULL;
533 if (jSurface != NULL) {
534 surface = reinterpret_cast<Surface*>(env->GetIntField(jSurface, fields.surface));
535 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800536 if (camera->setPreviewDisplay(surface) != NO_ERROR) {
537 jniThrowException(env, "java/io/IOException", "setPreviewDisplay failed");
538 }
539}
540
Jamie Gennisfd6f39e2010-12-20 12:15:00 -0800541static void android_hardware_Camera_setPreviewTexture(JNIEnv *env,
542 jobject thiz, jobject jSurfaceTexture)
543{
Steve Block71f2cf12011-10-20 11:56:00 +0100544 ALOGV("setPreviewTexture");
Jamie Gennisfd6f39e2010-12-20 12:15:00 -0800545 sp<Camera> camera = get_native_camera(env, thiz, NULL);
546 if (camera == 0) return;
547
Daniel Lam2e76c992012-02-23 14:35:13 -0800548 sp<BufferQueue> bufferQueue = NULL;
Jamie Gennisfd6f39e2010-12-20 12:15:00 -0800549 if (jSurfaceTexture != NULL) {
Daniel Lam2e76c992012-02-23 14:35:13 -0800550 sp<SurfaceTexture> surfaceTexture = reinterpret_cast<SurfaceTexture*>(env->GetIntField(
Jamie Gennisfd6f39e2010-12-20 12:15:00 -0800551 jSurfaceTexture, fields.surfaceTexture));
Daniel Lam2e76c992012-02-23 14:35:13 -0800552 if (surfaceTexture != NULL) {
553 bufferQueue = surfaceTexture->getBufferQueue();
554 }
555 else {
556 jniThrowException(env, "java/lang/IllegalArgumentException",
557 "SurfaceTexture already released in setPreviewTexture");
558 return;
559 }
560
Jamie Gennisfd6f39e2010-12-20 12:15:00 -0800561 }
Daniel Lam2e76c992012-02-23 14:35:13 -0800562
563 if (camera->setPreviewTexture(bufferQueue) != NO_ERROR) {
Jamie Gennisfd6f39e2010-12-20 12:15:00 -0800564 jniThrowException(env, "java/io/IOException",
565 "setPreviewTexture failed");
566 }
567}
568
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800569static void android_hardware_Camera_startPreview(JNIEnv *env, jobject thiz)
570{
Steve Block71f2cf12011-10-20 11:56:00 +0100571 ALOGV("startPreview");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800572 sp<Camera> camera = get_native_camera(env, thiz, NULL);
573 if (camera == 0) return;
574
575 if (camera->startPreview() != NO_ERROR) {
Elliott Hughes69a017b2011-04-08 14:10:28 -0700576 jniThrowRuntimeException(env, "startPreview failed");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800577 return;
578 }
579}
580
581static void android_hardware_Camera_stopPreview(JNIEnv *env, jobject thiz)
582{
Steve Block71f2cf12011-10-20 11:56:00 +0100583 ALOGV("stopPreview");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800584 sp<Camera> c = get_native_camera(env, thiz, NULL);
585 if (c == 0) return;
586
587 c->stopPreview();
588}
589
590static bool android_hardware_Camera_previewEnabled(JNIEnv *env, jobject thiz)
591{
Steve Block71f2cf12011-10-20 11:56:00 +0100592 ALOGV("previewEnabled");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800593 sp<Camera> c = get_native_camera(env, thiz, NULL);
594 if (c == 0) return false;
595
596 return c->previewEnabled();
597}
598
Andrew Harp94927df2009-10-20 01:47:05 -0400599static void android_hardware_Camera_setHasPreviewCallback(JNIEnv *env, jobject thiz, jboolean installed, jboolean manualBuffer)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800600{
Steve Block71f2cf12011-10-20 11:56:00 +0100601 ALOGV("setHasPreviewCallback: installed:%d, manualBuffer:%d", (int)installed, (int)manualBuffer);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800602 // Important: Only install preview_callback if the Java code has called
603 // setPreviewCallback() with a non-null value, otherwise we'd pay to memcpy
604 // each preview frame for nothing.
Dave Sparks5e271152009-06-23 17:30:11 -0700605 JNICameraContext* context;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800606 sp<Camera> camera = get_native_camera(env, thiz, &context);
607 if (camera == 0) return;
608
Andrew Harp94927df2009-10-20 01:47:05 -0400609 // setCallbackMode will take care of setting the context flags and calling
610 // camera->setPreviewCallbackFlags within a mutex for us.
611 context->setCallbackMode(env, installed, manualBuffer);
612}
613
James Donge00cab72011-02-17 16:38:06 -0800614static void android_hardware_Camera_addCallbackBuffer(JNIEnv *env, jobject thiz, jbyteArray bytes, int msgType) {
Steve Block71f2cf12011-10-20 11:56:00 +0100615 ALOGV("addCallbackBuffer: 0x%x", msgType);
Andrew Harp94927df2009-10-20 01:47:05 -0400616
617 JNICameraContext* context = reinterpret_cast<JNICameraContext*>(env->GetIntField(thiz, fields.context));
618
619 if (context != NULL) {
James Donge00cab72011-02-17 16:38:06 -0800620 context->addCallbackBuffer(env, bytes, msgType);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800621 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800622}
623
624static void android_hardware_Camera_autoFocus(JNIEnv *env, jobject thiz)
625{
Steve Block71f2cf12011-10-20 11:56:00 +0100626 ALOGV("autoFocus");
Dave Sparks5e271152009-06-23 17:30:11 -0700627 JNICameraContext* context;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800628 sp<Camera> c = get_native_camera(env, thiz, &context);
629 if (c == 0) return;
630
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800631 if (c->autoFocus() != NO_ERROR) {
Elliott Hughes69a017b2011-04-08 14:10:28 -0700632 jniThrowRuntimeException(env, "autoFocus failed");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800633 }
634}
635
Chih-Chung Chang244f8c22009-09-15 14:51:56 +0800636static void android_hardware_Camera_cancelAutoFocus(JNIEnv *env, jobject thiz)
637{
Steve Block71f2cf12011-10-20 11:56:00 +0100638 ALOGV("cancelAutoFocus");
Chih-Chung Chang244f8c22009-09-15 14:51:56 +0800639 JNICameraContext* context;
640 sp<Camera> c = get_native_camera(env, thiz, &context);
641 if (c == 0) return;
642
643 if (c->cancelAutoFocus() != NO_ERROR) {
Elliott Hughes69a017b2011-04-08 14:10:28 -0700644 jniThrowRuntimeException(env, "cancelAutoFocus failed");
Chih-Chung Chang244f8c22009-09-15 14:51:56 +0800645 }
646}
647
James Donge00cab72011-02-17 16:38:06 -0800648static void android_hardware_Camera_takePicture(JNIEnv *env, jobject thiz, int msgType)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800649{
Steve Block71f2cf12011-10-20 11:56:00 +0100650 ALOGV("takePicture");
Dave Sparks5e271152009-06-23 17:30:11 -0700651 JNICameraContext* context;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800652 sp<Camera> camera = get_native_camera(env, thiz, &context);
653 if (camera == 0) return;
654
James Donge00cab72011-02-17 16:38:06 -0800655 /*
656 * When CAMERA_MSG_RAW_IMAGE is requested, if the raw image callback
657 * buffer is available, CAMERA_MSG_RAW_IMAGE is enabled to get the
658 * notification _and_ the data; otherwise, CAMERA_MSG_RAW_IMAGE_NOTIFY
659 * is enabled to receive the callback notification but no data.
660 *
661 * Note that CAMERA_MSG_RAW_IMAGE_NOTIFY is not exposed to the
662 * Java application.
663 */
664 if (msgType & CAMERA_MSG_RAW_IMAGE) {
Steve Block71f2cf12011-10-20 11:56:00 +0100665 ALOGV("Enable raw image callback buffer");
James Donge00cab72011-02-17 16:38:06 -0800666 if (!context->isRawImageCallbackBufferAvailable()) {
Steve Block71f2cf12011-10-20 11:56:00 +0100667 ALOGV("Enable raw image notification, since no callback buffer exists");
James Donge00cab72011-02-17 16:38:06 -0800668 msgType &= ~CAMERA_MSG_RAW_IMAGE;
669 msgType |= CAMERA_MSG_RAW_IMAGE_NOTIFY;
670 }
671 }
672
673 if (camera->takePicture(msgType) != NO_ERROR) {
Elliott Hughes69a017b2011-04-08 14:10:28 -0700674 jniThrowRuntimeException(env, "takePicture failed");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800675 return;
676 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800677}
678
679static void android_hardware_Camera_setParameters(JNIEnv *env, jobject thiz, jstring params)
680{
Steve Block71f2cf12011-10-20 11:56:00 +0100681 ALOGV("setParameters");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800682 sp<Camera> camera = get_native_camera(env, thiz, NULL);
683 if (camera == 0) return;
684
685 const jchar* str = env->GetStringCritical(params, 0);
686 String8 params8;
687 if (params) {
688 params8 = String8(str, env->GetStringLength(params));
689 env->ReleaseStringCritical(params, str);
690 }
691 if (camera->setParameters(params8) != NO_ERROR) {
Elliott Hughes69a017b2011-04-08 14:10:28 -0700692 jniThrowRuntimeException(env, "setParameters failed");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800693 return;
694 }
695}
696
697static jstring android_hardware_Camera_getParameters(JNIEnv *env, jobject thiz)
698{
Steve Block71f2cf12011-10-20 11:56:00 +0100699 ALOGV("getParameters");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800700 sp<Camera> camera = get_native_camera(env, thiz, NULL);
701 if (camera == 0) return 0;
702
703 return env->NewStringUTF(camera->getParameters().string());
704}
705
706static void android_hardware_Camera_reconnect(JNIEnv *env, jobject thiz)
707{
Steve Block71f2cf12011-10-20 11:56:00 +0100708 ALOGV("reconnect");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800709 sp<Camera> camera = get_native_camera(env, thiz, NULL);
710 if (camera == 0) return;
711
712 if (camera->reconnect() != NO_ERROR) {
713 jniThrowException(env, "java/io/IOException", "reconnect failed");
714 return;
715 }
716}
717
Wu-cheng Liffe1cf22009-09-10 16:49:17 +0800718static void android_hardware_Camera_lock(JNIEnv *env, jobject thiz)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800719{
Steve Block71f2cf12011-10-20 11:56:00 +0100720 ALOGV("lock");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800721 sp<Camera> camera = get_native_camera(env, thiz, NULL);
Wu-cheng Liffe1cf22009-09-10 16:49:17 +0800722 if (camera == 0) return;
723
724 if (camera->lock() != NO_ERROR) {
Elliott Hughes69a017b2011-04-08 14:10:28 -0700725 jniThrowRuntimeException(env, "lock failed");
Wu-cheng Liffe1cf22009-09-10 16:49:17 +0800726 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800727}
728
Wu-cheng Liffe1cf22009-09-10 16:49:17 +0800729static void android_hardware_Camera_unlock(JNIEnv *env, jobject thiz)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800730{
Steve Block71f2cf12011-10-20 11:56:00 +0100731 ALOGV("unlock");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800732 sp<Camera> camera = get_native_camera(env, thiz, NULL);
Wu-cheng Liffe1cf22009-09-10 16:49:17 +0800733 if (camera == 0) return;
734
735 if (camera->unlock() != NO_ERROR) {
Elliott Hughes69a017b2011-04-08 14:10:28 -0700736 jniThrowRuntimeException(env, "unlock failed");
Wu-cheng Liffe1cf22009-09-10 16:49:17 +0800737 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800738}
739
Wu-cheng Li36f68b82009-09-28 16:14:58 -0700740static void android_hardware_Camera_startSmoothZoom(JNIEnv *env, jobject thiz, jint value)
741{
Steve Block71f2cf12011-10-20 11:56:00 +0100742 ALOGV("startSmoothZoom");
Wu-cheng Li36f68b82009-09-28 16:14:58 -0700743 sp<Camera> camera = get_native_camera(env, thiz, NULL);
744 if (camera == 0) return;
745
Wu-cheng Li0ca25192010-03-29 16:21:12 +0800746 status_t rc = camera->sendCommand(CAMERA_CMD_START_SMOOTH_ZOOM, value, 0);
747 if (rc == BAD_VALUE) {
748 char msg[64];
749 sprintf(msg, "invalid zoom value=%d", value);
750 jniThrowException(env, "java/lang/IllegalArgumentException", msg);
751 } else if (rc != NO_ERROR) {
Elliott Hughes69a017b2011-04-08 14:10:28 -0700752 jniThrowRuntimeException(env, "start smooth zoom failed");
Wu-cheng Li36f68b82009-09-28 16:14:58 -0700753 }
754}
755
756static void android_hardware_Camera_stopSmoothZoom(JNIEnv *env, jobject thiz)
757{
Steve Block71f2cf12011-10-20 11:56:00 +0100758 ALOGV("stopSmoothZoom");
Wu-cheng Li36f68b82009-09-28 16:14:58 -0700759 sp<Camera> camera = get_native_camera(env, thiz, NULL);
760 if (camera == 0) return;
761
762 if (camera->sendCommand(CAMERA_CMD_STOP_SMOOTH_ZOOM, 0, 0) != NO_ERROR) {
Elliott Hughes69a017b2011-04-08 14:10:28 -0700763 jniThrowRuntimeException(env, "stop smooth zoom failed");
Wu-cheng Li36f68b82009-09-28 16:14:58 -0700764 }
765}
766
Chih-Chung Changd1d77062010-01-22 17:49:48 -0800767static void android_hardware_Camera_setDisplayOrientation(JNIEnv *env, jobject thiz,
768 jint value)
769{
Steve Block71f2cf12011-10-20 11:56:00 +0100770 ALOGV("setDisplayOrientation");
Chih-Chung Changd1d77062010-01-22 17:49:48 -0800771 sp<Camera> camera = get_native_camera(env, thiz, NULL);
772 if (camera == 0) return;
773
774 if (camera->sendCommand(CAMERA_CMD_SET_DISPLAY_ORIENTATION, value, 0) != NO_ERROR) {
Elliott Hughes69a017b2011-04-08 14:10:28 -0700775 jniThrowRuntimeException(env, "set display orientation failed");
Chih-Chung Changd1d77062010-01-22 17:49:48 -0800776 }
777}
778
Wu-cheng Li4c2292e2011-07-22 02:37:11 +0800779static void android_hardware_Camera_startFaceDetection(JNIEnv *env, jobject thiz,
Wu-cheng Libb1e2752011-07-30 05:00:37 +0800780 jint type)
Wu-cheng Li4c2292e2011-07-22 02:37:11 +0800781{
Steve Block71f2cf12011-10-20 11:56:00 +0100782 ALOGV("startFaceDetection");
Wu-cheng Li4c2292e2011-07-22 02:37:11 +0800783 JNICameraContext* context;
784 sp<Camera> camera = get_native_camera(env, thiz, &context);
785 if (camera == 0) return;
786
787 status_t rc = camera->sendCommand(CAMERA_CMD_START_FACE_DETECTION, type, 0);
788 if (rc == BAD_VALUE) {
789 char msg[64];
790 snprintf(msg, sizeof(msg), "invalid face detection type=%d", type);
791 jniThrowException(env, "java/lang/IllegalArgumentException", msg);
792 } else if (rc != NO_ERROR) {
793 jniThrowRuntimeException(env, "start face detection failed");
794 }
795}
796
797static void android_hardware_Camera_stopFaceDetection(JNIEnv *env, jobject thiz)
798{
Steve Block71f2cf12011-10-20 11:56:00 +0100799 ALOGV("stopFaceDetection");
Wu-cheng Li4c2292e2011-07-22 02:37:11 +0800800 sp<Camera> camera = get_native_camera(env, thiz, NULL);
801 if (camera == 0) return;
802
803 if (camera->sendCommand(CAMERA_CMD_STOP_FACE_DETECTION, 0, 0) != NO_ERROR) {
804 jniThrowRuntimeException(env, "stop face detection failed");
805 }
806}
807
Wu-cheng Li9d062cf2011-11-14 20:30:14 +0800808static void android_hardware_Camera_enableFocusMoveCallback(JNIEnv *env, jobject thiz, jint enable)
809{
Wu-cheng Li02097522011-11-30 11:06:04 +0800810 ALOGV("enableFocusMoveCallback");
Wu-cheng Li9d062cf2011-11-14 20:30:14 +0800811 sp<Camera> camera = get_native_camera(env, thiz, NULL);
812 if (camera == 0) return;
813
814 if (camera->sendCommand(CAMERA_CMD_ENABLE_FOCUS_MOVE_MSG, enable, 0) != NO_ERROR) {
815 jniThrowRuntimeException(env, "enable focus move callback failed");
816 }
817}
818
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800819//-------------------------------------------------
820
821static JNINativeMethod camMethods[] = {
Chih-Chung Change25cc652010-05-06 16:36:58 +0800822 { "getNumberOfCameras",
823 "()I",
824 (void *)android_hardware_Camera_getNumberOfCameras },
Chih-Chung Changb8bb78f2010-06-10 13:32:16 +0800825 { "getCameraInfo",
826 "(ILandroid/hardware/Camera$CameraInfo;)V",
827 (void*)android_hardware_Camera_getCameraInfo },
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800828 { "native_setup",
Chih-Chung Change25cc652010-05-06 16:36:58 +0800829 "(Ljava/lang/Object;I)V",
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800830 (void*)android_hardware_Camera_native_setup },
831 { "native_release",
832 "()V",
833 (void*)android_hardware_Camera_release },
834 { "setPreviewDisplay",
835 "(Landroid/view/Surface;)V",
836 (void *)android_hardware_Camera_setPreviewDisplay },
Jamie Gennisfd6f39e2010-12-20 12:15:00 -0800837 { "setPreviewTexture",
838 "(Landroid/graphics/SurfaceTexture;)V",
839 (void *)android_hardware_Camera_setPreviewTexture },
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800840 { "startPreview",
841 "()V",
842 (void *)android_hardware_Camera_startPreview },
Wu-cheng Li4c2292e2011-07-22 02:37:11 +0800843 { "_stopPreview",
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800844 "()V",
845 (void *)android_hardware_Camera_stopPreview },
846 { "previewEnabled",
847 "()Z",
848 (void *)android_hardware_Camera_previewEnabled },
849 { "setHasPreviewCallback",
850 "(ZZ)V",
851 (void *)android_hardware_Camera_setHasPreviewCallback },
James Donge00cab72011-02-17 16:38:06 -0800852 { "_addCallbackBuffer",
853 "([BI)V",
Andrew Harp94927df2009-10-20 01:47:05 -0400854 (void *)android_hardware_Camera_addCallbackBuffer },
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800855 { "native_autoFocus",
856 "()V",
857 (void *)android_hardware_Camera_autoFocus },
Chih-Chung Chang244f8c22009-09-15 14:51:56 +0800858 { "native_cancelAutoFocus",
859 "()V",
860 (void *)android_hardware_Camera_cancelAutoFocus },
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800861 { "native_takePicture",
James Donge00cab72011-02-17 16:38:06 -0800862 "(I)V",
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800863 (void *)android_hardware_Camera_takePicture },
864 { "native_setParameters",
865 "(Ljava/lang/String;)V",
866 (void *)android_hardware_Camera_setParameters },
867 { "native_getParameters",
868 "()Ljava/lang/String;",
869 (void *)android_hardware_Camera_getParameters },
870 { "reconnect",
871 "()V",
872 (void*)android_hardware_Camera_reconnect },
873 { "lock",
Wu-cheng Liffe1cf22009-09-10 16:49:17 +0800874 "()V",
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800875 (void*)android_hardware_Camera_lock },
876 { "unlock",
Wu-cheng Liffe1cf22009-09-10 16:49:17 +0800877 "()V",
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800878 (void*)android_hardware_Camera_unlock },
Wu-cheng Li36f68b82009-09-28 16:14:58 -0700879 { "startSmoothZoom",
880 "(I)V",
881 (void *)android_hardware_Camera_startSmoothZoom },
882 { "stopSmoothZoom",
883 "()V",
884 (void *)android_hardware_Camera_stopSmoothZoom },
Chih-Chung Changd1d77062010-01-22 17:49:48 -0800885 { "setDisplayOrientation",
886 "(I)V",
887 (void *)android_hardware_Camera_setDisplayOrientation },
Wu-cheng Li4c2292e2011-07-22 02:37:11 +0800888 { "_startFaceDetection",
889 "(I)V",
890 (void *)android_hardware_Camera_startFaceDetection },
891 { "_stopFaceDetection",
892 "()V",
893 (void *)android_hardware_Camera_stopFaceDetection},
Wu-cheng Li9d062cf2011-11-14 20:30:14 +0800894 { "enableFocusMoveCallback",
895 "(I)V",
896 (void *)android_hardware_Camera_enableFocusMoveCallback},
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800897};
898
899struct field {
900 const char *class_name;
901 const char *field_name;
902 const char *field_type;
903 jfieldID *jfield;
904};
905
906static int find_fields(JNIEnv *env, field *fields, int count)
907{
908 for (int i = 0; i < count; i++) {
909 field *f = &fields[i];
910 jclass clazz = env->FindClass(f->class_name);
911 if (clazz == NULL) {
Steve Block3762c312012-01-06 19:20:56 +0000912 ALOGE("Can't find %s", f->class_name);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800913 return -1;
914 }
915
916 jfieldID field = env->GetFieldID(clazz, f->field_name, f->field_type);
917 if (field == NULL) {
Steve Block3762c312012-01-06 19:20:56 +0000918 ALOGE("Can't find %s.%s", f->class_name, f->field_name);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800919 return -1;
920 }
921
922 *(f->jfield) = field;
923 }
924
925 return 0;
926}
927
928// Get all the required offsets in java class and register native functions
929int register_android_hardware_Camera(JNIEnv *env)
930{
931 field fields_to_find[] = {
932 { "android/hardware/Camera", "mNativeContext", "I", &fields.context },
Chih-Chung Changb8bb78f2010-06-10 13:32:16 +0800933 { "android/view/Surface", ANDROID_VIEW_SURFACE_JNI_ID, "I", &fields.surface },
Jamie Gennisfd6f39e2010-12-20 12:15:00 -0800934 { "android/graphics/SurfaceTexture",
935 ANDROID_GRAPHICS_SURFACETEXTURE_JNI_ID, "I", &fields.surfaceTexture },
Wu-cheng Li3fd5fa42010-09-15 16:06:20 -0700936 { "android/hardware/Camera$CameraInfo", "facing", "I", &fields.facing },
937 { "android/hardware/Camera$CameraInfo", "orientation", "I", &fields.orientation },
Wu-cheng Lif0d6a482011-07-28 05:30:59 +0800938 { "android/hardware/Camera$Face", "rect", "Landroid/graphics/Rect;", &fields.face_rect },
939 { "android/hardware/Camera$Face", "score", "I", &fields.face_score },
Wu-cheng Li4c2292e2011-07-22 02:37:11 +0800940 { "android/graphics/Rect", "left", "I", &fields.rect_left },
941 { "android/graphics/Rect", "top", "I", &fields.rect_top },
942 { "android/graphics/Rect", "right", "I", &fields.rect_right },
943 { "android/graphics/Rect", "bottom", "I", &fields.rect_bottom },
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800944 };
945
946 if (find_fields(env, fields_to_find, NELEM(fields_to_find)) < 0)
947 return -1;
948
949 jclass clazz = env->FindClass("android/hardware/Camera");
950 fields.post_event = env->GetStaticMethodID(clazz, "postEventFromNative",
951 "(Ljava/lang/Object;IIILjava/lang/Object;)V");
952 if (fields.post_event == NULL) {
Steve Block3762c312012-01-06 19:20:56 +0000953 ALOGE("Can't find android/hardware/Camera.postEventFromNative");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800954 return -1;
955 }
956
Wu-cheng Libb1e2752011-07-30 05:00:37 +0800957 clazz = env->FindClass("android/graphics/Rect");
958 fields.rect_constructor = env->GetMethodID(clazz, "<init>", "()V");
959 if (fields.rect_constructor == NULL) {
Steve Block3762c312012-01-06 19:20:56 +0000960 ALOGE("Can't find android/graphics/Rect.Rect()");
Wu-cheng Libb1e2752011-07-30 05:00:37 +0800961 return -1;
962 }
963
964 clazz = env->FindClass("android/hardware/Camera$Face");
965 fields.face_constructor = env->GetMethodID(clazz, "<init>", "()V");
966 if (fields.face_constructor == NULL) {
Steve Block3762c312012-01-06 19:20:56 +0000967 ALOGE("Can't find android/hardware/Camera$Face.Face()");
Wu-cheng Libb1e2752011-07-30 05:00:37 +0800968 return -1;
969 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800970
971 // Register native functions
972 return AndroidRuntime::registerNativeMethods(env, "android/hardware/Camera",
973 camMethods, NELEM(camMethods));
974}