blob: d57e52678906d52c56cfdad726beff54e86c215f [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
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080028#include <ui/Surface.h>
29#include <ui/Camera.h>
Mathias Agopian07952722009-05-19 19:08:10 -070030#include <binder/IMemory.h>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080031
32using namespace android;
33
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080034struct fields_t {
35 jfieldID context;
36 jfieldID surface;
37 jmethodID post_event;
38};
39
40static fields_t fields;
41static Mutex sLock;
42
Dave Sparks5e271152009-06-23 17:30:11 -070043// provides persistent context for calls from native code to Java
44class JNICameraContext: public CameraListener
45{
46public:
47 JNICameraContext(JNIEnv* env, jobject weak_this, jclass clazz, const sp<Camera>& camera);
48 ~JNICameraContext() { release(); }
49 virtual void notify(int32_t msgType, int32_t ext1, int32_t ext2);
50 virtual void postData(int32_t msgType, const sp<IMemory>& dataPtr);
Dave Sparks59c1a932009-07-08 15:56:53 -070051 virtual void postDataTimestamp(nsecs_t timestamp, int32_t msgType, const sp<IMemory>& dataPtr);
Andrew Harp94927df2009-10-20 01:47:05 -040052 void addCallbackBuffer(JNIEnv *env, jbyteArray cbb);
53 void setCallbackMode(JNIEnv *env, bool installed, bool manualMode);
Dave Sparks5e271152009-06-23 17:30:11 -070054 sp<Camera> getCamera() { Mutex::Autolock _l(mLock); return mCamera; }
55 void release();
56
57private:
58 void copyAndPost(JNIEnv* env, const sp<IMemory>& dataPtr, int msgType);
Andrew Harp94927df2009-10-20 01:47:05 -040059 void clearCallbackBuffers_l(JNIEnv *env);
Dave Sparks5e271152009-06-23 17:30:11 -070060
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080061 jobject mCameraJObjectWeak; // weak reference to java object
62 jclass mCameraJClass; // strong reference to java class
Wu-cheng Liffe1cf22009-09-10 16:49:17 +080063 sp<Camera> mCamera; // strong reference to native object
Dave Sparks5e271152009-06-23 17:30:11 -070064 Mutex mLock;
Andrew Harp94927df2009-10-20 01:47:05 -040065
66 Vector<jbyteArray> mCallbackBuffers; // Global reference application managed byte[]
67 bool mManualBufferMode; // Whether to use application managed buffers.
68 bool mManualCameraCallbackSet; // Whether the callback has been set, used to reduce unnecessary calls to set the callback.
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;
Andrew Harp94927df2009-10-20 01:47:05 -040093
94 mManualBufferMode = false;
95 mManualCameraCallbackSet = false;
Dave Sparks5e271152009-06-23 17:30:11 -070096}
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080097
Dave Sparks5e271152009-06-23 17:30:11 -070098void JNICameraContext::release()
99{
100 LOGV("release");
101 Mutex::Autolock _l(mLock);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800102 JNIEnv *env = AndroidRuntime::getJNIEnv();
Dave Sparks5e271152009-06-23 17:30:11 -0700103
104 if (mCameraJObjectWeak != NULL) {
105 env->DeleteGlobalRef(mCameraJObjectWeak);
106 mCameraJObjectWeak = NULL;
107 }
108 if (mCameraJClass != NULL) {
109 env->DeleteGlobalRef(mCameraJClass);
110 mCameraJClass = NULL;
111 }
Andrew Harp94927df2009-10-20 01:47:05 -0400112 clearCallbackBuffers_l(env);
Dave Sparks5e271152009-06-23 17:30:11 -0700113 mCamera.clear();
114}
115
116void JNICameraContext::notify(int32_t msgType, int32_t ext1, int32_t ext2)
117{
118 LOGV("notify");
119
120 // VM pointer will be NULL if object is released
121 Mutex::Autolock _l(mLock);
122 if (mCameraJObjectWeak == NULL) {
123 LOGW("callback on dead camera object");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800124 return;
125 }
Dave Sparks5e271152009-06-23 17:30:11 -0700126 JNIEnv *env = AndroidRuntime::getJNIEnv();
Dave Sparksc62f9bd2009-06-26 13:33:32 -0700127 env->CallStaticVoidMethod(mCameraJClass, fields.post_event,
Chih-Chung Chang6157de02009-09-24 15:29:35 -0700128 mCameraJObjectWeak, msgType, ext1, ext2, NULL);
Dave Sparks5e271152009-06-23 17:30:11 -0700129}
130
131void JNICameraContext::copyAndPost(JNIEnv* env, const sp<IMemory>& dataPtr, int msgType)
132{
133 jbyteArray obj = NULL;
134
135 // allocate Java byte array and copy data
136 if (dataPtr != NULL) {
137 ssize_t offset;
138 size_t size;
139 sp<IMemoryHeap> heap = dataPtr->getMemory(&offset, &size);
140 LOGV("postData: off=%d, size=%d", offset, size);
141 uint8_t *heapBase = (uint8_t*)heap->base();
142
143 if (heapBase != NULL) {
Dave Sparksc4ca4202009-07-13 09:38:20 -0700144 const jbyte* data = reinterpret_cast<const jbyte*>(heapBase + offset);
Andrew Harp94927df2009-10-20 01:47:05 -0400145
146 if (!mManualBufferMode) {
147 LOGV("Allocating callback buffer");
148 obj = env->NewByteArray(size);
149 } else {
150 // Vector access should be protected by lock in postData()
151 if(!mCallbackBuffers.isEmpty()) {
152 LOGV("Using callback buffer from queue of length %d", mCallbackBuffers.size());
153 jbyteArray globalBuffer = mCallbackBuffers.itemAt(0);
154 mCallbackBuffers.removeAt(0);
155
156 obj = (jbyteArray)env->NewLocalRef(globalBuffer);
157 env->DeleteGlobalRef(globalBuffer);
158
159 if (obj != NULL) {
160 jsize bufferLength = env->GetArrayLength(obj);
161 if ((int)bufferLength < (int)size) {
162 LOGE("Manually set buffer was too small! Expected %d bytes, but got %d!",
163 size, bufferLength);
164 env->DeleteLocalRef(obj);
165 return;
166 }
167 }
168 }
169
170 if(mCallbackBuffers.isEmpty()) {
171 LOGW("Out of buffers, clearing callback!");
172 mCamera->setPreviewCallbackFlags(FRAME_CALLBACK_FLAG_NOOP);
173 mManualCameraCallbackSet = false;
174
175 if (obj == NULL) {
176 return;
177 }
178 }
179 }
180
Dave Sparks5e271152009-06-23 17:30:11 -0700181 if (obj == NULL) {
182 LOGE("Couldn't allocate byte array for JPEG data");
183 env->ExceptionClear();
184 } else {
Dave Sparksa95f4952009-07-10 18:13:36 -0700185 env->SetByteArrayRegion(obj, 0, size, data);
Dave Sparks5e271152009-06-23 17:30:11 -0700186 }
187 } else {
188 LOGE("image heap is NULL");
189 }
190 }
191
192 // post image data to Java
193 env->CallStaticVoidMethod(mCameraJClass, fields.post_event,
194 mCameraJObjectWeak, msgType, 0, 0, obj);
195 if (obj) {
196 env->DeleteLocalRef(obj);
197 }
198}
199
200void JNICameraContext::postData(int32_t msgType, const sp<IMemory>& dataPtr)
201{
202 // VM pointer will be NULL if object is released
203 Mutex::Autolock _l(mLock);
204 JNIEnv *env = AndroidRuntime::getJNIEnv();
Dave Sparksd0cbb1a2009-06-29 19:03:33 -0700205 if (mCameraJObjectWeak == NULL) {
206 LOGW("callback on dead camera object");
207 return;
208 }
Dave Sparks5e271152009-06-23 17:30:11 -0700209
210 // return data based on callback type
211 switch(msgType) {
Dave Sparks5e271152009-06-23 17:30:11 -0700212 case CAMERA_MSG_VIDEO_FRAME:
Dave Sparksc62f9bd2009-06-26 13:33:32 -0700213 // should never happen
Dave Sparks5e271152009-06-23 17:30:11 -0700214 break;
Dave Sparksc62f9bd2009-06-26 13:33:32 -0700215 // don't return raw data to Java
Dave Sparks5e271152009-06-23 17:30:11 -0700216 case CAMERA_MSG_RAW_IMAGE:
217 LOGV("rawCallback");
218 env->CallStaticVoidMethod(mCameraJClass, fields.post_event,
Dave Sparksc62f9bd2009-06-26 13:33:32 -0700219 mCameraJObjectWeak, msgType, 0, 0, NULL);
Dave Sparks5e271152009-06-23 17:30:11 -0700220 break;
221 default:
Dave Sparksda578562009-07-21 08:43:29 -0700222 // TODO: Change to LOGV
223 LOGD("dataCallback(%d, %p)", msgType, dataPtr.get());
Dave Sparksc62f9bd2009-06-26 13:33:32 -0700224 copyAndPost(env, dataPtr, msgType);
Dave Sparks5e271152009-06-23 17:30:11 -0700225 break;
226 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800227}
228
Dave Sparks59c1a932009-07-08 15:56:53 -0700229void JNICameraContext::postDataTimestamp(nsecs_t timestamp, int32_t msgType, const sp<IMemory>& dataPtr)
230{
231 // TODO: plumb up to Java. For now, just drop the timestamp
232 postData(msgType, dataPtr);
233}
234
Andrew Harp94927df2009-10-20 01:47:05 -0400235void JNICameraContext::setCallbackMode(JNIEnv *env, bool installed, bool manualMode)
236{
237 Mutex::Autolock _l(mLock);
238 mManualBufferMode = manualMode;
239 mManualCameraCallbackSet = false;
240
241 // In order to limit the over usage of binder threads, all non-manual buffer
242 // callbacks use FRAME_CALLBACK_FLAG_BARCODE_SCANNER mode now.
243 //
244 // Continuous callbacks will have the callback re-registered from handleMessage.
245 // Manual buffer mode will operate as fast as possible, relying on the finite supply
246 // of buffers for throttling.
247
248 if (!installed) {
249 mCamera->setPreviewCallbackFlags(FRAME_CALLBACK_FLAG_NOOP);
250 clearCallbackBuffers_l(env);
251 } else if (mManualBufferMode) {
252 if (!mCallbackBuffers.isEmpty()) {
253 mCamera->setPreviewCallbackFlags(FRAME_CALLBACK_FLAG_CAMERA);
254 mManualCameraCallbackSet = true;
255 }
256 } else {
257 mCamera->setPreviewCallbackFlags(FRAME_CALLBACK_FLAG_BARCODE_SCANNER);
258 clearCallbackBuffers_l(env);
259 }
260}
261
262void JNICameraContext::addCallbackBuffer(JNIEnv *env, jbyteArray cbb)
263{
264 if (cbb != NULL) {
265 Mutex::Autolock _l(mLock);
266 jbyteArray callbackBuffer = (jbyteArray)env->NewGlobalRef(cbb);
267 mCallbackBuffers.push(cbb);
268
269 LOGV("Adding callback buffer to queue, %d total", mCallbackBuffers.size());
270
271 // We want to make sure the camera knows we're ready for the next frame.
272 // This may have come unset had we not had a callbackbuffer ready for it last time.
273 if (mManualBufferMode && !mManualCameraCallbackSet) {
274 mCamera->setPreviewCallbackFlags(FRAME_CALLBACK_FLAG_CAMERA);
275 mManualCameraCallbackSet = true;
276 }
277 } else {
278 LOGE("Null byte array!");
279 }
280}
281
282void JNICameraContext::clearCallbackBuffers_l(JNIEnv *env)
283{
284 LOGV("Clearing callback buffers, %d remained", mCallbackBuffers.size());
285 while(!mCallbackBuffers.isEmpty()) {
286 env->DeleteGlobalRef(mCallbackBuffers.top());
287 mCallbackBuffers.pop();
288 }
289}
290
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800291// connect to camera service
292static void android_hardware_Camera_native_setup(JNIEnv *env, jobject thiz, jobject weak_this)
293{
294 sp<Camera> camera = Camera::connect();
295
296 if (camera == NULL) {
Wu-cheng Lifa3e5562009-05-04 19:38:43 +0800297 jniThrowException(env, "java/lang/RuntimeException",
298 "Fail to connect to camera service");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800299 return;
300 }
301
302 // make sure camera hardware is alive
303 if (camera->getStatus() != NO_ERROR) {
Wu-cheng Liba55b362009-06-10 16:55:39 +0800304 jniThrowException(env, "java/lang/RuntimeException", "Camera initialization failed");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800305 return;
306 }
307
308 jclass clazz = env->GetObjectClass(thiz);
309 if (clazz == NULL) {
Wu-cheng Liba55b362009-06-10 16:55:39 +0800310 jniThrowException(env, "java/lang/RuntimeException", "Can't find android/hardware/Camera");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800311 return;
312 }
313
314 // We use a weak reference so the Camera object can be garbage collected.
315 // The reference is only used as a proxy for callbacks.
Dave Sparks5e271152009-06-23 17:30:11 -0700316 sp<JNICameraContext> context = new JNICameraContext(env, weak_this, clazz, camera);
317 context->incStrong(thiz);
318 camera->setListener(context);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800319
320 // save context in opaque field
Dave Sparks5e271152009-06-23 17:30:11 -0700321 env->SetIntField(thiz, fields.context, (int)context.get());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800322}
323
324// disconnect from camera service
325// It's okay to call this when the native camera context is already null.
326// This handles the case where the user has called release() and the
327// finalizer is invoked later.
328static void android_hardware_Camera_release(JNIEnv *env, jobject thiz)
329{
Dave Sparksda578562009-07-21 08:43:29 -0700330 // TODO: Change to LOGV
331 LOGD("release camera");
Dave Sparks5e271152009-06-23 17:30:11 -0700332 JNICameraContext* context = NULL;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800333 sp<Camera> camera;
334 {
335 Mutex::Autolock _l(sLock);
Dave Sparks5e271152009-06-23 17:30:11 -0700336 context = reinterpret_cast<JNICameraContext*>(env->GetIntField(thiz, fields.context));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800337
338 // Make sure we do not attempt to callback on a deleted Java object.
339 env->SetIntField(thiz, fields.context, 0);
340 }
341
342 // clean up if release has not been called before
343 if (context != NULL) {
Dave Sparks5e271152009-06-23 17:30:11 -0700344 camera = context->getCamera();
345 context->release();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800346 LOGV("native_release: context=%p camera=%p", context, camera.get());
347
348 // clear callbacks
349 if (camera != NULL) {
Dave Sparks5e271152009-06-23 17:30:11 -0700350 camera->setPreviewCallbackFlags(FRAME_CALLBACK_FLAG_NOOP);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800351 camera->disconnect();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800352 }
353
354 // remove context to prevent further Java access
Dave Sparks5e271152009-06-23 17:30:11 -0700355 context->decStrong(thiz);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800356 }
357}
358
359static void android_hardware_Camera_setPreviewDisplay(JNIEnv *env, jobject thiz, jobject jSurface)
360{
361 LOGV("setPreviewDisplay");
362 sp<Camera> camera = get_native_camera(env, thiz, NULL);
363 if (camera == 0) return;
364
Wu-cheng Lib8a10fe2009-06-23 23:37:36 +0800365 sp<Surface> surface = NULL;
366 if (jSurface != NULL) {
367 surface = reinterpret_cast<Surface*>(env->GetIntField(jSurface, fields.surface));
368 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800369 if (camera->setPreviewDisplay(surface) != NO_ERROR) {
370 jniThrowException(env, "java/io/IOException", "setPreviewDisplay failed");
371 }
372}
373
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800374static void android_hardware_Camera_startPreview(JNIEnv *env, jobject thiz)
375{
376 LOGV("startPreview");
377 sp<Camera> camera = get_native_camera(env, thiz, NULL);
378 if (camera == 0) return;
379
380 if (camera->startPreview() != NO_ERROR) {
Wu-cheng Liba55b362009-06-10 16:55:39 +0800381 jniThrowException(env, "java/lang/RuntimeException", "startPreview failed");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800382 return;
383 }
384}
385
386static void android_hardware_Camera_stopPreview(JNIEnv *env, jobject thiz)
387{
388 LOGV("stopPreview");
389 sp<Camera> c = get_native_camera(env, thiz, NULL);
390 if (c == 0) return;
391
392 c->stopPreview();
393}
394
395static bool android_hardware_Camera_previewEnabled(JNIEnv *env, jobject thiz)
396{
397 LOGV("previewEnabled");
398 sp<Camera> c = get_native_camera(env, thiz, NULL);
399 if (c == 0) return false;
400
401 return c->previewEnabled();
402}
403
Andrew Harp94927df2009-10-20 01:47:05 -0400404static void android_hardware_Camera_setHasPreviewCallback(JNIEnv *env, jobject thiz, jboolean installed, jboolean manualBuffer)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800405{
Andrew Harp94927df2009-10-20 01:47:05 -0400406 LOGV("setHasPreviewCallback: installed:%d, manualBuffer:%d", (int)installed, (int)manualBuffer);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800407 // Important: Only install preview_callback if the Java code has called
408 // setPreviewCallback() with a non-null value, otherwise we'd pay to memcpy
409 // each preview frame for nothing.
Dave Sparks5e271152009-06-23 17:30:11 -0700410 JNICameraContext* context;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800411 sp<Camera> camera = get_native_camera(env, thiz, &context);
412 if (camera == 0) return;
413
Andrew Harp94927df2009-10-20 01:47:05 -0400414 // setCallbackMode will take care of setting the context flags and calling
415 // camera->setPreviewCallbackFlags within a mutex for us.
416 context->setCallbackMode(env, installed, manualBuffer);
417}
418
419static void android_hardware_Camera_addCallbackBuffer(JNIEnv *env, jobject thiz, jbyteArray bytes) {
420 LOGV("addCallbackBuffer");
421
422 JNICameraContext* context = reinterpret_cast<JNICameraContext*>(env->GetIntField(thiz, fields.context));
423
424 if (context != NULL) {
425 context->addCallbackBuffer(env, bytes);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800426 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800427}
428
429static void android_hardware_Camera_autoFocus(JNIEnv *env, jobject thiz)
430{
431 LOGV("autoFocus");
Dave Sparks5e271152009-06-23 17:30:11 -0700432 JNICameraContext* context;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800433 sp<Camera> c = get_native_camera(env, thiz, &context);
434 if (c == 0) return;
435
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800436 if (c->autoFocus() != NO_ERROR) {
Wu-cheng Liba55b362009-06-10 16:55:39 +0800437 jniThrowException(env, "java/lang/RuntimeException", "autoFocus failed");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800438 }
439}
440
Chih-Chung Chang244f8c22009-09-15 14:51:56 +0800441static void android_hardware_Camera_cancelAutoFocus(JNIEnv *env, jobject thiz)
442{
443 LOGV("cancelAutoFocus");
444 JNICameraContext* context;
445 sp<Camera> c = get_native_camera(env, thiz, &context);
446 if (c == 0) return;
447
448 if (c->cancelAutoFocus() != NO_ERROR) {
449 jniThrowException(env, "java/lang/RuntimeException", "cancelAutoFocus failed");
450 }
451}
452
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800453static void android_hardware_Camera_takePicture(JNIEnv *env, jobject thiz)
454{
455 LOGV("takePicture");
Dave Sparks5e271152009-06-23 17:30:11 -0700456 JNICameraContext* context;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800457 sp<Camera> camera = get_native_camera(env, thiz, &context);
458 if (camera == 0) return;
459
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800460 if (camera->takePicture() != NO_ERROR) {
Wu-cheng Liba55b362009-06-10 16:55:39 +0800461 jniThrowException(env, "java/lang/RuntimeException", "takePicture failed");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800462 return;
463 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800464}
465
466static void android_hardware_Camera_setParameters(JNIEnv *env, jobject thiz, jstring params)
467{
468 LOGV("setParameters");
469 sp<Camera> camera = get_native_camera(env, thiz, NULL);
470 if (camera == 0) return;
471
472 const jchar* str = env->GetStringCritical(params, 0);
473 String8 params8;
474 if (params) {
475 params8 = String8(str, env->GetStringLength(params));
476 env->ReleaseStringCritical(params, str);
477 }
478 if (camera->setParameters(params8) != NO_ERROR) {
Wu-cheng Liba55b362009-06-10 16:55:39 +0800479 jniThrowException(env, "java/lang/RuntimeException", "setParameters failed");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800480 return;
481 }
482}
483
484static jstring android_hardware_Camera_getParameters(JNIEnv *env, jobject thiz)
485{
486 LOGV("getParameters");
487 sp<Camera> camera = get_native_camera(env, thiz, NULL);
488 if (camera == 0) return 0;
489
490 return env->NewStringUTF(camera->getParameters().string());
491}
492
493static void android_hardware_Camera_reconnect(JNIEnv *env, jobject thiz)
494{
495 LOGV("reconnect");
496 sp<Camera> camera = get_native_camera(env, thiz, NULL);
497 if (camera == 0) return;
498
499 if (camera->reconnect() != NO_ERROR) {
500 jniThrowException(env, "java/io/IOException", "reconnect failed");
501 return;
502 }
503}
504
Wu-cheng Liffe1cf22009-09-10 16:49:17 +0800505static void android_hardware_Camera_lock(JNIEnv *env, jobject thiz)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800506{
507 LOGV("lock");
508 sp<Camera> camera = get_native_camera(env, thiz, NULL);
Wu-cheng Liffe1cf22009-09-10 16:49:17 +0800509 if (camera == 0) return;
510
511 if (camera->lock() != NO_ERROR) {
512 jniThrowException(env, "java/lang/RuntimeException", "lock failed");
513 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800514}
515
Wu-cheng Liffe1cf22009-09-10 16:49:17 +0800516static void android_hardware_Camera_unlock(JNIEnv *env, jobject thiz)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800517{
518 LOGV("unlock");
519 sp<Camera> camera = get_native_camera(env, thiz, NULL);
Wu-cheng Liffe1cf22009-09-10 16:49:17 +0800520 if (camera == 0) return;
521
522 if (camera->unlock() != NO_ERROR) {
523 jniThrowException(env, "java/lang/RuntimeException", "unlock failed");
524 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800525}
526
Wu-cheng Li36f68b82009-09-28 16:14:58 -0700527static void android_hardware_Camera_startSmoothZoom(JNIEnv *env, jobject thiz, jint value)
528{
529 LOGD("startSmoothZoom");
530 sp<Camera> camera = get_native_camera(env, thiz, NULL);
531 if (camera == 0) return;
532
533 if (camera->sendCommand(CAMERA_CMD_START_SMOOTH_ZOOM, value, 0) != NO_ERROR) {
534 jniThrowException(env, "java/lang/RuntimeException", "start smooth zoom failed");
535 }
536}
537
538static void android_hardware_Camera_stopSmoothZoom(JNIEnv *env, jobject thiz)
539{
540 LOGD("stopSmoothZoom");
541 sp<Camera> camera = get_native_camera(env, thiz, NULL);
542 if (camera == 0) return;
543
544 if (camera->sendCommand(CAMERA_CMD_STOP_SMOOTH_ZOOM, 0, 0) != NO_ERROR) {
545 jniThrowException(env, "java/lang/RuntimeException", "stop smooth zoom failed");
546 }
547}
548
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800549//-------------------------------------------------
550
551static JNINativeMethod camMethods[] = {
552 { "native_setup",
553 "(Ljava/lang/Object;)V",
554 (void*)android_hardware_Camera_native_setup },
555 { "native_release",
556 "()V",
557 (void*)android_hardware_Camera_release },
558 { "setPreviewDisplay",
559 "(Landroid/view/Surface;)V",
560 (void *)android_hardware_Camera_setPreviewDisplay },
561 { "startPreview",
562 "()V",
563 (void *)android_hardware_Camera_startPreview },
564 { "stopPreview",
565 "()V",
566 (void *)android_hardware_Camera_stopPreview },
567 { "previewEnabled",
568 "()Z",
569 (void *)android_hardware_Camera_previewEnabled },
570 { "setHasPreviewCallback",
571 "(ZZ)V",
572 (void *)android_hardware_Camera_setHasPreviewCallback },
Andrew Harp94927df2009-10-20 01:47:05 -0400573 { "addCallbackBuffer",
574 "([B)V",
575 (void *)android_hardware_Camera_addCallbackBuffer },
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800576 { "native_autoFocus",
577 "()V",
578 (void *)android_hardware_Camera_autoFocus },
Chih-Chung Chang244f8c22009-09-15 14:51:56 +0800579 { "native_cancelAutoFocus",
580 "()V",
581 (void *)android_hardware_Camera_cancelAutoFocus },
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800582 { "native_takePicture",
583 "()V",
584 (void *)android_hardware_Camera_takePicture },
585 { "native_setParameters",
586 "(Ljava/lang/String;)V",
587 (void *)android_hardware_Camera_setParameters },
588 { "native_getParameters",
589 "()Ljava/lang/String;",
590 (void *)android_hardware_Camera_getParameters },
591 { "reconnect",
592 "()V",
593 (void*)android_hardware_Camera_reconnect },
594 { "lock",
Wu-cheng Liffe1cf22009-09-10 16:49:17 +0800595 "()V",
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800596 (void*)android_hardware_Camera_lock },
597 { "unlock",
Wu-cheng Liffe1cf22009-09-10 16:49:17 +0800598 "()V",
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800599 (void*)android_hardware_Camera_unlock },
Wu-cheng Li36f68b82009-09-28 16:14:58 -0700600 { "startSmoothZoom",
601 "(I)V",
602 (void *)android_hardware_Camera_startSmoothZoom },
603 { "stopSmoothZoom",
604 "()V",
605 (void *)android_hardware_Camera_stopSmoothZoom },
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800606};
607
608struct field {
609 const char *class_name;
610 const char *field_name;
611 const char *field_type;
612 jfieldID *jfield;
613};
614
615static int find_fields(JNIEnv *env, field *fields, int count)
616{
617 for (int i = 0; i < count; i++) {
618 field *f = &fields[i];
619 jclass clazz = env->FindClass(f->class_name);
620 if (clazz == NULL) {
621 LOGE("Can't find %s", f->class_name);
622 return -1;
623 }
624
625 jfieldID field = env->GetFieldID(clazz, f->field_name, f->field_type);
626 if (field == NULL) {
627 LOGE("Can't find %s.%s", f->class_name, f->field_name);
628 return -1;
629 }
630
631 *(f->jfield) = field;
632 }
633
634 return 0;
635}
636
637// Get all the required offsets in java class and register native functions
638int register_android_hardware_Camera(JNIEnv *env)
639{
640 field fields_to_find[] = {
641 { "android/hardware/Camera", "mNativeContext", "I", &fields.context },
642 { "android/view/Surface", "mSurface", "I", &fields.surface }
643 };
644
645 if (find_fields(env, fields_to_find, NELEM(fields_to_find)) < 0)
646 return -1;
647
648 jclass clazz = env->FindClass("android/hardware/Camera");
649 fields.post_event = env->GetStaticMethodID(clazz, "postEventFromNative",
650 "(Ljava/lang/Object;IIILjava/lang/Object;)V");
651 if (fields.post_event == NULL) {
652 LOGE("Can't find android/hardware/Camera.postEventFromNative");
653 return -1;
654 }
655
656
657 // Register native functions
658 return AndroidRuntime::registerNativeMethods(env, "android/hardware/Camera",
659 camMethods, NELEM(camMethods));
660}
661