blob: 2874be48403e39df984166d48e015798a90da4c6 [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
26#include <ui/Surface.h>
27#include <ui/Camera.h>
28#include <utils/IMemory.h>
29
30using namespace android;
31
32enum CallbackMessageID {
33 kShutterCallback = 0,
34 kRawCallback = 1,
35 kJpegCallback = 2,
36 kPreviewCallback = 3,
37 kAutoFocusCallback = 4,
38 kErrorCallback = 5
39};
40
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080041struct fields_t {
42 jfieldID context;
43 jfieldID surface;
44 jmethodID post_event;
45};
46
47static fields_t fields;
48static Mutex sLock;
49
Dave Sparks5e271152009-06-23 17:30:11 -070050// provides persistent context for calls from native code to Java
51class JNICameraContext: public CameraListener
52{
53public:
54 JNICameraContext(JNIEnv* env, jobject weak_this, jclass clazz, const sp<Camera>& camera);
55 ~JNICameraContext() { release(); }
56 virtual void notify(int32_t msgType, int32_t ext1, int32_t ext2);
57 virtual void postData(int32_t msgType, const sp<IMemory>& dataPtr);
Dave Sparks59c1a932009-07-08 15:56:53 -070058 virtual void postDataTimestamp(nsecs_t timestamp, int32_t msgType, const sp<IMemory>& dataPtr);
Dave Sparks5e271152009-06-23 17:30:11 -070059 sp<Camera> getCamera() { Mutex::Autolock _l(mLock); return mCamera; }
60 void release();
61
62private:
63 void copyAndPost(JNIEnv* env, const sp<IMemory>& dataPtr, int msgType);
64
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080065 jobject mCameraJObjectWeak; // weak reference to java object
66 jclass mCameraJClass; // strong reference to java class
67 sp<Camera> mCamera; // strong reference to native object
Dave Sparks5e271152009-06-23 17:30:11 -070068 Mutex mLock;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080069};
70
Dave Sparks5e271152009-06-23 17:30:11 -070071sp<Camera> get_native_camera(JNIEnv *env, jobject thiz, JNICameraContext** pContext)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080072{
73 sp<Camera> camera;
74 Mutex::Autolock _l(sLock);
Dave Sparks5e271152009-06-23 17:30:11 -070075 JNICameraContext* context = reinterpret_cast<JNICameraContext*>(env->GetIntField(thiz, fields.context));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080076 if (context != NULL) {
Dave Sparks5e271152009-06-23 17:30:11 -070077 camera = context->getCamera();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080078 }
79 LOGV("get_native_camera: context=%p, camera=%p", context, camera.get());
80 if (camera == 0) {
81 jniThrowException(env, "java/lang/RuntimeException", "Method called after release()");
82 }
83
84 if (pContext != NULL) *pContext = context;
85 return camera;
86}
87
Dave Sparks5e271152009-06-23 17:30:11 -070088JNICameraContext::JNICameraContext(JNIEnv* env, jobject weak_this, jclass clazz, const sp<Camera>& camera)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080089{
Dave Sparks5e271152009-06-23 17:30:11 -070090 mCameraJObjectWeak = env->NewGlobalRef(weak_this);
91 mCameraJClass = (jclass)env->NewGlobalRef(clazz);
92 mCamera = camera;
93}
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080094
Dave Sparks5e271152009-06-23 17:30:11 -070095void JNICameraContext::release()
96{
97 LOGV("release");
98 Mutex::Autolock _l(mLock);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080099 JNIEnv *env = AndroidRuntime::getJNIEnv();
Dave Sparks5e271152009-06-23 17:30:11 -0700100
101 if (mCameraJObjectWeak != NULL) {
102 env->DeleteGlobalRef(mCameraJObjectWeak);
103 mCameraJObjectWeak = NULL;
104 }
105 if (mCameraJClass != NULL) {
106 env->DeleteGlobalRef(mCameraJClass);
107 mCameraJClass = NULL;
108 }
109 mCamera.clear();
110}
111
112void JNICameraContext::notify(int32_t msgType, int32_t ext1, int32_t ext2)
113{
114 LOGV("notify");
115
116 // VM pointer will be NULL if object is released
117 Mutex::Autolock _l(mLock);
118 if (mCameraJObjectWeak == NULL) {
119 LOGW("callback on dead camera object");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800120 return;
121 }
Dave Sparks5e271152009-06-23 17:30:11 -0700122 JNIEnv *env = AndroidRuntime::getJNIEnv();
Dave Sparksc62f9bd2009-06-26 13:33:32 -0700123 env->CallStaticVoidMethod(mCameraJClass, fields.post_event,
124 mCameraJObjectWeak, msgType, ext1, ext2);
Dave Sparks5e271152009-06-23 17:30:11 -0700125}
126
127void JNICameraContext::copyAndPost(JNIEnv* env, const sp<IMemory>& dataPtr, int msgType)
128{
129 jbyteArray obj = NULL;
130
131 // allocate Java byte array and copy data
132 if (dataPtr != NULL) {
133 ssize_t offset;
134 size_t size;
135 sp<IMemoryHeap> heap = dataPtr->getMemory(&offset, &size);
136 LOGV("postData: off=%d, size=%d", offset, size);
137 uint8_t *heapBase = (uint8_t*)heap->base();
138
139 if (heapBase != NULL) {
140 uint8_t *data = heapBase + offset;
141 obj = env->NewByteArray(size);
142 if (obj == NULL) {
143 LOGE("Couldn't allocate byte array for JPEG data");
144 env->ExceptionClear();
145 } else {
Dave Sparksa95f4952009-07-10 18:13:36 -0700146 env->SetByteArrayRegion(obj, 0, size, data);
Dave Sparks5e271152009-06-23 17:30:11 -0700147 }
148 } else {
149 LOGE("image heap is NULL");
150 }
151 }
152
153 // post image data to Java
154 env->CallStaticVoidMethod(mCameraJClass, fields.post_event,
155 mCameraJObjectWeak, msgType, 0, 0, obj);
156 if (obj) {
157 env->DeleteLocalRef(obj);
158 }
159}
160
161void JNICameraContext::postData(int32_t msgType, const sp<IMemory>& dataPtr)
162{
163 // VM pointer will be NULL if object is released
164 Mutex::Autolock _l(mLock);
165 JNIEnv *env = AndroidRuntime::getJNIEnv();
Dave Sparksd0cbb1a2009-06-29 19:03:33 -0700166 if (mCameraJObjectWeak == NULL) {
167 LOGW("callback on dead camera object");
168 return;
169 }
Dave Sparks5e271152009-06-23 17:30:11 -0700170
171 // return data based on callback type
172 switch(msgType) {
Dave Sparks5e271152009-06-23 17:30:11 -0700173 case CAMERA_MSG_VIDEO_FRAME:
Dave Sparksc62f9bd2009-06-26 13:33:32 -0700174 // should never happen
Dave Sparks5e271152009-06-23 17:30:11 -0700175 break;
Dave Sparksc62f9bd2009-06-26 13:33:32 -0700176 // don't return raw data to Java
Dave Sparks5e271152009-06-23 17:30:11 -0700177 case CAMERA_MSG_RAW_IMAGE:
178 LOGV("rawCallback");
179 env->CallStaticVoidMethod(mCameraJClass, fields.post_event,
Dave Sparksc62f9bd2009-06-26 13:33:32 -0700180 mCameraJObjectWeak, msgType, 0, 0, NULL);
Dave Sparks5e271152009-06-23 17:30:11 -0700181 break;
182 default:
183 LOGV("dataCallback(%d, %p)", msgType, dataPtr.get());
Dave Sparksc62f9bd2009-06-26 13:33:32 -0700184 copyAndPost(env, dataPtr, msgType);
Dave Sparks5e271152009-06-23 17:30:11 -0700185 break;
186 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800187}
188
Dave Sparks59c1a932009-07-08 15:56:53 -0700189void JNICameraContext::postDataTimestamp(nsecs_t timestamp, int32_t msgType, const sp<IMemory>& dataPtr)
190{
191 // TODO: plumb up to Java. For now, just drop the timestamp
192 postData(msgType, dataPtr);
193}
194
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800195// connect to camera service
196static void android_hardware_Camera_native_setup(JNIEnv *env, jobject thiz, jobject weak_this)
197{
198 sp<Camera> camera = Camera::connect();
199
200 if (camera == NULL) {
Wu-cheng Lifa3e5562009-05-04 19:38:43 +0800201 jniThrowException(env, "java/lang/RuntimeException",
202 "Fail to connect to camera service");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800203 return;
204 }
205
206 // make sure camera hardware is alive
207 if (camera->getStatus() != NO_ERROR) {
Wu-cheng Liba55b362009-06-10 16:55:39 +0800208 jniThrowException(env, "java/lang/RuntimeException", "Camera initialization failed");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800209 return;
210 }
211
212 jclass clazz = env->GetObjectClass(thiz);
213 if (clazz == NULL) {
Wu-cheng Liba55b362009-06-10 16:55:39 +0800214 jniThrowException(env, "java/lang/RuntimeException", "Can't find android/hardware/Camera");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800215 return;
216 }
217
218 // We use a weak reference so the Camera object can be garbage collected.
219 // The reference is only used as a proxy for callbacks.
Dave Sparks5e271152009-06-23 17:30:11 -0700220 sp<JNICameraContext> context = new JNICameraContext(env, weak_this, clazz, camera);
221 context->incStrong(thiz);
222 camera->setListener(context);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800223
224 // save context in opaque field
Dave Sparks5e271152009-06-23 17:30:11 -0700225 env->SetIntField(thiz, fields.context, (int)context.get());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800226}
227
228// disconnect from camera service
229// It's okay to call this when the native camera context is already null.
230// This handles the case where the user has called release() and the
231// finalizer is invoked later.
232static void android_hardware_Camera_release(JNIEnv *env, jobject thiz)
233{
Dave Sparks5e271152009-06-23 17:30:11 -0700234 JNICameraContext* context = NULL;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800235 sp<Camera> camera;
236 {
237 Mutex::Autolock _l(sLock);
Dave Sparks5e271152009-06-23 17:30:11 -0700238 context = reinterpret_cast<JNICameraContext*>(env->GetIntField(thiz, fields.context));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800239
240 // Make sure we do not attempt to callback on a deleted Java object.
241 env->SetIntField(thiz, fields.context, 0);
242 }
243
244 // clean up if release has not been called before
245 if (context != NULL) {
Dave Sparks5e271152009-06-23 17:30:11 -0700246 camera = context->getCamera();
247 context->release();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800248 LOGV("native_release: context=%p camera=%p", context, camera.get());
249
250 // clear callbacks
251 if (camera != NULL) {
Dave Sparks5e271152009-06-23 17:30:11 -0700252 camera->setPreviewCallbackFlags(FRAME_CALLBACK_FLAG_NOOP);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800253 camera->disconnect();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800254 }
255
256 // remove context to prevent further Java access
Dave Sparks5e271152009-06-23 17:30:11 -0700257 context->decStrong(thiz);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800258 }
259}
260
261static void android_hardware_Camera_setPreviewDisplay(JNIEnv *env, jobject thiz, jobject jSurface)
262{
263 LOGV("setPreviewDisplay");
264 sp<Camera> camera = get_native_camera(env, thiz, NULL);
265 if (camera == 0) return;
266
Wu-cheng Lib8a10fe2009-06-23 23:37:36 +0800267 sp<Surface> surface = NULL;
268 if (jSurface != NULL) {
269 surface = reinterpret_cast<Surface*>(env->GetIntField(jSurface, fields.surface));
270 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800271 if (camera->setPreviewDisplay(surface) != NO_ERROR) {
272 jniThrowException(env, "java/io/IOException", "setPreviewDisplay failed");
273 }
274}
275
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800276static void android_hardware_Camera_startPreview(JNIEnv *env, jobject thiz)
277{
278 LOGV("startPreview");
279 sp<Camera> camera = get_native_camera(env, thiz, NULL);
280 if (camera == 0) return;
281
282 if (camera->startPreview() != NO_ERROR) {
Wu-cheng Liba55b362009-06-10 16:55:39 +0800283 jniThrowException(env, "java/lang/RuntimeException", "startPreview failed");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800284 return;
285 }
286}
287
288static void android_hardware_Camera_stopPreview(JNIEnv *env, jobject thiz)
289{
290 LOGV("stopPreview");
291 sp<Camera> c = get_native_camera(env, thiz, NULL);
292 if (c == 0) return;
293
294 c->stopPreview();
295}
296
297static bool android_hardware_Camera_previewEnabled(JNIEnv *env, jobject thiz)
298{
299 LOGV("previewEnabled");
300 sp<Camera> c = get_native_camera(env, thiz, NULL);
301 if (c == 0) return false;
302
303 return c->previewEnabled();
304}
305
306static void android_hardware_Camera_setHasPreviewCallback(JNIEnv *env, jobject thiz, jboolean installed, jboolean oneshot)
307{
308 // Important: Only install preview_callback if the Java code has called
309 // setPreviewCallback() with a non-null value, otherwise we'd pay to memcpy
310 // each preview frame for nothing.
Dave Sparks5e271152009-06-23 17:30:11 -0700311 JNICameraContext* context;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800312 sp<Camera> camera = get_native_camera(env, thiz, &context);
313 if (camera == 0) return;
314
315 int callback_flag;
316 if (installed) {
317 callback_flag = oneshot ? FRAME_CALLBACK_FLAG_BARCODE_SCANNER : FRAME_CALLBACK_FLAG_CAMERA;
318 } else {
319 callback_flag = FRAME_CALLBACK_FLAG_NOOP;
320 }
Dave Sparks5e271152009-06-23 17:30:11 -0700321 camera->setPreviewCallbackFlags(callback_flag);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800322}
323
324static void android_hardware_Camera_autoFocus(JNIEnv *env, jobject thiz)
325{
326 LOGV("autoFocus");
Dave Sparks5e271152009-06-23 17:30:11 -0700327 JNICameraContext* context;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800328 sp<Camera> c = get_native_camera(env, thiz, &context);
329 if (c == 0) return;
330
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800331 if (c->autoFocus() != NO_ERROR) {
Wu-cheng Liba55b362009-06-10 16:55:39 +0800332 jniThrowException(env, "java/lang/RuntimeException", "autoFocus failed");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800333 }
334}
335
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800336static void android_hardware_Camera_takePicture(JNIEnv *env, jobject thiz)
337{
338 LOGV("takePicture");
Dave Sparks5e271152009-06-23 17:30:11 -0700339 JNICameraContext* context;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800340 sp<Camera> camera = get_native_camera(env, thiz, &context);
341 if (camera == 0) return;
342
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800343 if (camera->takePicture() != NO_ERROR) {
Wu-cheng Liba55b362009-06-10 16:55:39 +0800344 jniThrowException(env, "java/lang/RuntimeException", "takePicture failed");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800345 return;
346 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800347}
348
349static void android_hardware_Camera_setParameters(JNIEnv *env, jobject thiz, jstring params)
350{
351 LOGV("setParameters");
352 sp<Camera> camera = get_native_camera(env, thiz, NULL);
353 if (camera == 0) return;
354
355 const jchar* str = env->GetStringCritical(params, 0);
356 String8 params8;
357 if (params) {
358 params8 = String8(str, env->GetStringLength(params));
359 env->ReleaseStringCritical(params, str);
360 }
361 if (camera->setParameters(params8) != NO_ERROR) {
Wu-cheng Liba55b362009-06-10 16:55:39 +0800362 jniThrowException(env, "java/lang/RuntimeException", "setParameters failed");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800363 return;
364 }
365}
366
367static jstring android_hardware_Camera_getParameters(JNIEnv *env, jobject thiz)
368{
369 LOGV("getParameters");
370 sp<Camera> camera = get_native_camera(env, thiz, NULL);
371 if (camera == 0) return 0;
372
373 return env->NewStringUTF(camera->getParameters().string());
374}
375
376static void android_hardware_Camera_reconnect(JNIEnv *env, jobject thiz)
377{
378 LOGV("reconnect");
379 sp<Camera> camera = get_native_camera(env, thiz, NULL);
380 if (camera == 0) return;
381
382 if (camera->reconnect() != NO_ERROR) {
383 jniThrowException(env, "java/io/IOException", "reconnect failed");
384 return;
385 }
386}
387
388static jint android_hardware_Camera_lock(JNIEnv *env, jobject thiz)
389{
390 LOGV("lock");
391 sp<Camera> camera = get_native_camera(env, thiz, NULL);
392 if (camera == 0) return INVALID_OPERATION;
393 return (jint) camera->lock();
394}
395
396static jint android_hardware_Camera_unlock(JNIEnv *env, jobject thiz)
397{
398 LOGV("unlock");
399 sp<Camera> camera = get_native_camera(env, thiz, NULL);
400 if (camera == 0) return INVALID_OPERATION;
401 return (jint) camera->unlock();
402}
403
404//-------------------------------------------------
405
406static JNINativeMethod camMethods[] = {
407 { "native_setup",
408 "(Ljava/lang/Object;)V",
409 (void*)android_hardware_Camera_native_setup },
410 { "native_release",
411 "()V",
412 (void*)android_hardware_Camera_release },
413 { "setPreviewDisplay",
414 "(Landroid/view/Surface;)V",
415 (void *)android_hardware_Camera_setPreviewDisplay },
416 { "startPreview",
417 "()V",
418 (void *)android_hardware_Camera_startPreview },
419 { "stopPreview",
420 "()V",
421 (void *)android_hardware_Camera_stopPreview },
422 { "previewEnabled",
423 "()Z",
424 (void *)android_hardware_Camera_previewEnabled },
425 { "setHasPreviewCallback",
426 "(ZZ)V",
427 (void *)android_hardware_Camera_setHasPreviewCallback },
428 { "native_autoFocus",
429 "()V",
430 (void *)android_hardware_Camera_autoFocus },
431 { "native_takePicture",
432 "()V",
433 (void *)android_hardware_Camera_takePicture },
434 { "native_setParameters",
435 "(Ljava/lang/String;)V",
436 (void *)android_hardware_Camera_setParameters },
437 { "native_getParameters",
438 "()Ljava/lang/String;",
439 (void *)android_hardware_Camera_getParameters },
440 { "reconnect",
441 "()V",
442 (void*)android_hardware_Camera_reconnect },
443 { "lock",
444 "()I",
445 (void*)android_hardware_Camera_lock },
446 { "unlock",
447 "()I",
448 (void*)android_hardware_Camera_unlock },
449};
450
451struct field {
452 const char *class_name;
453 const char *field_name;
454 const char *field_type;
455 jfieldID *jfield;
456};
457
458static int find_fields(JNIEnv *env, field *fields, int count)
459{
460 for (int i = 0; i < count; i++) {
461 field *f = &fields[i];
462 jclass clazz = env->FindClass(f->class_name);
463 if (clazz == NULL) {
464 LOGE("Can't find %s", f->class_name);
465 return -1;
466 }
467
468 jfieldID field = env->GetFieldID(clazz, f->field_name, f->field_type);
469 if (field == NULL) {
470 LOGE("Can't find %s.%s", f->class_name, f->field_name);
471 return -1;
472 }
473
474 *(f->jfield) = field;
475 }
476
477 return 0;
478}
479
480// Get all the required offsets in java class and register native functions
481int register_android_hardware_Camera(JNIEnv *env)
482{
483 field fields_to_find[] = {
484 { "android/hardware/Camera", "mNativeContext", "I", &fields.context },
485 { "android/view/Surface", "mSurface", "I", &fields.surface }
486 };
487
488 if (find_fields(env, fields_to_find, NELEM(fields_to_find)) < 0)
489 return -1;
490
491 jclass clazz = env->FindClass("android/hardware/Camera");
492 fields.post_event = env->GetStaticMethodID(clazz, "postEventFromNative",
493 "(Ljava/lang/Object;IIILjava/lang/Object;)V");
494 if (fields.post_event == NULL) {
495 LOGE("Can't find android/hardware/Camera.postEventFromNative");
496 return -1;
497 }
498
499
500 // Register native functions
501 return AndroidRuntime::registerNativeMethods(env, "android/hardware/Camera",
502 camMethods, NELEM(camMethods));
503}
504