blob: 57b5aa632b8d793a1046981b48d140b829ee7463 [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 {
146 jbyte *bytes = env->GetByteArrayElements(obj, NULL);
147 memcpy(bytes, data, size);
148 env->ReleaseByteArrayElements(obj, bytes, 0);
149
150 }
151 } else {
152 LOGE("image heap is NULL");
153 }
154 }
155
156 // post image data to Java
157 env->CallStaticVoidMethod(mCameraJClass, fields.post_event,
158 mCameraJObjectWeak, msgType, 0, 0, obj);
159 if (obj) {
160 env->DeleteLocalRef(obj);
161 }
162}
163
164void JNICameraContext::postData(int32_t msgType, const sp<IMemory>& dataPtr)
165{
166 // VM pointer will be NULL if object is released
167 Mutex::Autolock _l(mLock);
168 JNIEnv *env = AndroidRuntime::getJNIEnv();
Dave Sparksd0cbb1a2009-06-29 19:03:33 -0700169 if (mCameraJObjectWeak == NULL) {
170 LOGW("callback on dead camera object");
171 return;
172 }
Dave Sparks5e271152009-06-23 17:30:11 -0700173
174 // return data based on callback type
175 switch(msgType) {
Dave Sparks5e271152009-06-23 17:30:11 -0700176 case CAMERA_MSG_VIDEO_FRAME:
Dave Sparksc62f9bd2009-06-26 13:33:32 -0700177 // should never happen
Dave Sparks5e271152009-06-23 17:30:11 -0700178 break;
Dave Sparksc62f9bd2009-06-26 13:33:32 -0700179 // don't return raw data to Java
Dave Sparks5e271152009-06-23 17:30:11 -0700180 case CAMERA_MSG_RAW_IMAGE:
181 LOGV("rawCallback");
182 env->CallStaticVoidMethod(mCameraJClass, fields.post_event,
Dave Sparksc62f9bd2009-06-26 13:33:32 -0700183 mCameraJObjectWeak, msgType, 0, 0, NULL);
Dave Sparks5e271152009-06-23 17:30:11 -0700184 break;
185 default:
186 LOGV("dataCallback(%d, %p)", msgType, dataPtr.get());
Dave Sparksc62f9bd2009-06-26 13:33:32 -0700187 copyAndPost(env, dataPtr, msgType);
Dave Sparks5e271152009-06-23 17:30:11 -0700188 break;
189 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800190}
191
Dave Sparks59c1a932009-07-08 15:56:53 -0700192void JNICameraContext::postDataTimestamp(nsecs_t timestamp, int32_t msgType, const sp<IMemory>& dataPtr)
193{
194 // TODO: plumb up to Java. For now, just drop the timestamp
195 postData(msgType, dataPtr);
196}
197
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800198// connect to camera service
199static void android_hardware_Camera_native_setup(JNIEnv *env, jobject thiz, jobject weak_this)
200{
201 sp<Camera> camera = Camera::connect();
202
203 if (camera == NULL) {
Wu-cheng Lifa3e5562009-05-04 19:38:43 +0800204 jniThrowException(env, "java/lang/RuntimeException",
205 "Fail to connect to camera service");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800206 return;
207 }
208
209 // make sure camera hardware is alive
210 if (camera->getStatus() != NO_ERROR) {
Wu-cheng Liba55b362009-06-10 16:55:39 +0800211 jniThrowException(env, "java/lang/RuntimeException", "Camera initialization failed");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800212 return;
213 }
214
215 jclass clazz = env->GetObjectClass(thiz);
216 if (clazz == NULL) {
Wu-cheng Liba55b362009-06-10 16:55:39 +0800217 jniThrowException(env, "java/lang/RuntimeException", "Can't find android/hardware/Camera");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800218 return;
219 }
220
221 // We use a weak reference so the Camera object can be garbage collected.
222 // The reference is only used as a proxy for callbacks.
Dave Sparks5e271152009-06-23 17:30:11 -0700223 sp<JNICameraContext> context = new JNICameraContext(env, weak_this, clazz, camera);
224 context->incStrong(thiz);
225 camera->setListener(context);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800226
227 // save context in opaque field
Dave Sparks5e271152009-06-23 17:30:11 -0700228 env->SetIntField(thiz, fields.context, (int)context.get());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800229}
230
231// disconnect from camera service
232// It's okay to call this when the native camera context is already null.
233// This handles the case where the user has called release() and the
234// finalizer is invoked later.
235static void android_hardware_Camera_release(JNIEnv *env, jobject thiz)
236{
Dave Sparks5e271152009-06-23 17:30:11 -0700237 JNICameraContext* context = NULL;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800238 sp<Camera> camera;
239 {
240 Mutex::Autolock _l(sLock);
Dave Sparks5e271152009-06-23 17:30:11 -0700241 context = reinterpret_cast<JNICameraContext*>(env->GetIntField(thiz, fields.context));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800242
243 // Make sure we do not attempt to callback on a deleted Java object.
244 env->SetIntField(thiz, fields.context, 0);
245 }
246
247 // clean up if release has not been called before
248 if (context != NULL) {
Dave Sparks5e271152009-06-23 17:30:11 -0700249 camera = context->getCamera();
250 context->release();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800251 LOGV("native_release: context=%p camera=%p", context, camera.get());
252
253 // clear callbacks
254 if (camera != NULL) {
Dave Sparks5e271152009-06-23 17:30:11 -0700255 camera->setPreviewCallbackFlags(FRAME_CALLBACK_FLAG_NOOP);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800256 camera->disconnect();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800257 }
258
259 // remove context to prevent further Java access
Dave Sparks5e271152009-06-23 17:30:11 -0700260 context->decStrong(thiz);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800261 }
262}
263
264static void android_hardware_Camera_setPreviewDisplay(JNIEnv *env, jobject thiz, jobject jSurface)
265{
266 LOGV("setPreviewDisplay");
267 sp<Camera> camera = get_native_camera(env, thiz, NULL);
268 if (camera == 0) return;
269
Wu-cheng Lib8a10fe2009-06-23 23:37:36 +0800270 sp<Surface> surface = NULL;
271 if (jSurface != NULL) {
272 surface = reinterpret_cast<Surface*>(env->GetIntField(jSurface, fields.surface));
273 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800274 if (camera->setPreviewDisplay(surface) != NO_ERROR) {
275 jniThrowException(env, "java/io/IOException", "setPreviewDisplay failed");
276 }
277}
278
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800279static void android_hardware_Camera_startPreview(JNIEnv *env, jobject thiz)
280{
281 LOGV("startPreview");
282 sp<Camera> camera = get_native_camera(env, thiz, NULL);
283 if (camera == 0) return;
284
285 if (camera->startPreview() != NO_ERROR) {
Wu-cheng Liba55b362009-06-10 16:55:39 +0800286 jniThrowException(env, "java/lang/RuntimeException", "startPreview failed");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800287 return;
288 }
289}
290
291static void android_hardware_Camera_stopPreview(JNIEnv *env, jobject thiz)
292{
293 LOGV("stopPreview");
294 sp<Camera> c = get_native_camera(env, thiz, NULL);
295 if (c == 0) return;
296
297 c->stopPreview();
298}
299
300static bool android_hardware_Camera_previewEnabled(JNIEnv *env, jobject thiz)
301{
302 LOGV("previewEnabled");
303 sp<Camera> c = get_native_camera(env, thiz, NULL);
304 if (c == 0) return false;
305
306 return c->previewEnabled();
307}
308
309static void android_hardware_Camera_setHasPreviewCallback(JNIEnv *env, jobject thiz, jboolean installed, jboolean oneshot)
310{
311 // Important: Only install preview_callback if the Java code has called
312 // setPreviewCallback() with a non-null value, otherwise we'd pay to memcpy
313 // each preview frame for nothing.
Dave Sparks5e271152009-06-23 17:30:11 -0700314 JNICameraContext* context;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800315 sp<Camera> camera = get_native_camera(env, thiz, &context);
316 if (camera == 0) return;
317
318 int callback_flag;
319 if (installed) {
320 callback_flag = oneshot ? FRAME_CALLBACK_FLAG_BARCODE_SCANNER : FRAME_CALLBACK_FLAG_CAMERA;
321 } else {
322 callback_flag = FRAME_CALLBACK_FLAG_NOOP;
323 }
Dave Sparks5e271152009-06-23 17:30:11 -0700324 camera->setPreviewCallbackFlags(callback_flag);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800325}
326
327static void android_hardware_Camera_autoFocus(JNIEnv *env, jobject thiz)
328{
329 LOGV("autoFocus");
Dave Sparks5e271152009-06-23 17:30:11 -0700330 JNICameraContext* context;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800331 sp<Camera> c = get_native_camera(env, thiz, &context);
332 if (c == 0) return;
333
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800334 if (c->autoFocus() != NO_ERROR) {
Wu-cheng Liba55b362009-06-10 16:55:39 +0800335 jniThrowException(env, "java/lang/RuntimeException", "autoFocus failed");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800336 }
337}
338
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800339static void android_hardware_Camera_takePicture(JNIEnv *env, jobject thiz)
340{
341 LOGV("takePicture");
Dave Sparks5e271152009-06-23 17:30:11 -0700342 JNICameraContext* context;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800343 sp<Camera> camera = get_native_camera(env, thiz, &context);
344 if (camera == 0) return;
345
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800346 if (camera->takePicture() != NO_ERROR) {
Wu-cheng Liba55b362009-06-10 16:55:39 +0800347 jniThrowException(env, "java/lang/RuntimeException", "takePicture failed");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800348 return;
349 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800350}
351
352static void android_hardware_Camera_setParameters(JNIEnv *env, jobject thiz, jstring params)
353{
354 LOGV("setParameters");
355 sp<Camera> camera = get_native_camera(env, thiz, NULL);
356 if (camera == 0) return;
357
358 const jchar* str = env->GetStringCritical(params, 0);
359 String8 params8;
360 if (params) {
361 params8 = String8(str, env->GetStringLength(params));
362 env->ReleaseStringCritical(params, str);
363 }
364 if (camera->setParameters(params8) != NO_ERROR) {
Wu-cheng Liba55b362009-06-10 16:55:39 +0800365 jniThrowException(env, "java/lang/RuntimeException", "setParameters failed");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800366 return;
367 }
368}
369
370static jstring android_hardware_Camera_getParameters(JNIEnv *env, jobject thiz)
371{
372 LOGV("getParameters");
373 sp<Camera> camera = get_native_camera(env, thiz, NULL);
374 if (camera == 0) return 0;
375
376 return env->NewStringUTF(camera->getParameters().string());
377}
378
379static void android_hardware_Camera_reconnect(JNIEnv *env, jobject thiz)
380{
381 LOGV("reconnect");
382 sp<Camera> camera = get_native_camera(env, thiz, NULL);
383 if (camera == 0) return;
384
385 if (camera->reconnect() != NO_ERROR) {
386 jniThrowException(env, "java/io/IOException", "reconnect failed");
387 return;
388 }
389}
390
391static jint android_hardware_Camera_lock(JNIEnv *env, jobject thiz)
392{
393 LOGV("lock");
394 sp<Camera> camera = get_native_camera(env, thiz, NULL);
395 if (camera == 0) return INVALID_OPERATION;
396 return (jint) camera->lock();
397}
398
399static jint android_hardware_Camera_unlock(JNIEnv *env, jobject thiz)
400{
401 LOGV("unlock");
402 sp<Camera> camera = get_native_camera(env, thiz, NULL);
403 if (camera == 0) return INVALID_OPERATION;
404 return (jint) camera->unlock();
405}
406
407//-------------------------------------------------
408
409static JNINativeMethod camMethods[] = {
410 { "native_setup",
411 "(Ljava/lang/Object;)V",
412 (void*)android_hardware_Camera_native_setup },
413 { "native_release",
414 "()V",
415 (void*)android_hardware_Camera_release },
416 { "setPreviewDisplay",
417 "(Landroid/view/Surface;)V",
418 (void *)android_hardware_Camera_setPreviewDisplay },
419 { "startPreview",
420 "()V",
421 (void *)android_hardware_Camera_startPreview },
422 { "stopPreview",
423 "()V",
424 (void *)android_hardware_Camera_stopPreview },
425 { "previewEnabled",
426 "()Z",
427 (void *)android_hardware_Camera_previewEnabled },
428 { "setHasPreviewCallback",
429 "(ZZ)V",
430 (void *)android_hardware_Camera_setHasPreviewCallback },
431 { "native_autoFocus",
432 "()V",
433 (void *)android_hardware_Camera_autoFocus },
434 { "native_takePicture",
435 "()V",
436 (void *)android_hardware_Camera_takePicture },
437 { "native_setParameters",
438 "(Ljava/lang/String;)V",
439 (void *)android_hardware_Camera_setParameters },
440 { "native_getParameters",
441 "()Ljava/lang/String;",
442 (void *)android_hardware_Camera_getParameters },
443 { "reconnect",
444 "()V",
445 (void*)android_hardware_Camera_reconnect },
446 { "lock",
447 "()I",
448 (void*)android_hardware_Camera_lock },
449 { "unlock",
450 "()I",
451 (void*)android_hardware_Camera_unlock },
452};
453
454struct field {
455 const char *class_name;
456 const char *field_name;
457 const char *field_type;
458 jfieldID *jfield;
459};
460
461static int find_fields(JNIEnv *env, field *fields, int count)
462{
463 for (int i = 0; i < count; i++) {
464 field *f = &fields[i];
465 jclass clazz = env->FindClass(f->class_name);
466 if (clazz == NULL) {
467 LOGE("Can't find %s", f->class_name);
468 return -1;
469 }
470
471 jfieldID field = env->GetFieldID(clazz, f->field_name, f->field_type);
472 if (field == NULL) {
473 LOGE("Can't find %s.%s", f->class_name, f->field_name);
474 return -1;
475 }
476
477 *(f->jfield) = field;
478 }
479
480 return 0;
481}
482
483// Get all the required offsets in java class and register native functions
484int register_android_hardware_Camera(JNIEnv *env)
485{
486 field fields_to_find[] = {
487 { "android/hardware/Camera", "mNativeContext", "I", &fields.context },
488 { "android/view/Surface", "mSurface", "I", &fields.surface }
489 };
490
491 if (find_fields(env, fields_to_find, NELEM(fields_to_find)) < 0)
492 return -1;
493
494 jclass clazz = env->FindClass("android/hardware/Camera");
495 fields.post_event = env->GetStaticMethodID(clazz, "postEventFromNative",
496 "(Ljava/lang/Object;IIILjava/lang/Object;)V");
497 if (fields.post_event == NULL) {
498 LOGE("Can't find android/hardware/Camera.postEventFromNative");
499 return -1;
500 }
501
502
503 // Register native functions
504 return AndroidRuntime::registerNativeMethods(env, "android/hardware/Camera",
505 camMethods, NELEM(camMethods));
506}
507