blob: 9f70509f43c01273a3942dcd04d4ebdef57dc6c6 [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 Agopian000479f2010-02-09 17:46:37 -080029#include <surfaceflinger/Surface.h>
30#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;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080041 jmethodID post_event;
42};
43
44static fields_t fields;
45static Mutex sLock;
46
Dave Sparks5e271152009-06-23 17:30:11 -070047// provides persistent context for calls from native code to Java
48class JNICameraContext: public CameraListener
49{
50public:
51 JNICameraContext(JNIEnv* env, jobject weak_this, jclass clazz, const sp<Camera>& camera);
52 ~JNICameraContext() { release(); }
53 virtual void notify(int32_t msgType, int32_t ext1, int32_t ext2);
54 virtual void postData(int32_t msgType, const sp<IMemory>& dataPtr);
Dave Sparks59c1a932009-07-08 15:56:53 -070055 virtual void postDataTimestamp(nsecs_t timestamp, int32_t msgType, const sp<IMemory>& dataPtr);
Andrew Harp94927df2009-10-20 01:47:05 -040056 void addCallbackBuffer(JNIEnv *env, jbyteArray cbb);
57 void setCallbackMode(JNIEnv *env, bool installed, bool manualMode);
Dave Sparks5e271152009-06-23 17:30:11 -070058 sp<Camera> getCamera() { Mutex::Autolock _l(mLock); return mCamera; }
59 void release();
60
61private:
62 void copyAndPost(JNIEnv* env, const sp<IMemory>& dataPtr, int msgType);
Andrew Harp94927df2009-10-20 01:47:05 -040063 void clearCallbackBuffers_l(JNIEnv *env);
Dave Sparks5e271152009-06-23 17:30:11 -070064
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
Wu-cheng Liffe1cf22009-09-10 16:49:17 +080067 sp<Camera> mCamera; // strong reference to native object
Dave Sparks5e271152009-06-23 17:30:11 -070068 Mutex mLock;
Andrew Harp94927df2009-10-20 01:47:05 -040069
70 Vector<jbyteArray> mCallbackBuffers; // Global reference application managed byte[]
71 bool mManualBufferMode; // Whether to use application managed buffers.
72 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 -080073};
74
Dave Sparks5e271152009-06-23 17:30:11 -070075sp<Camera> get_native_camera(JNIEnv *env, jobject thiz, JNICameraContext** pContext)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080076{
77 sp<Camera> camera;
78 Mutex::Autolock _l(sLock);
Dave Sparks5e271152009-06-23 17:30:11 -070079 JNICameraContext* context = reinterpret_cast<JNICameraContext*>(env->GetIntField(thiz, fields.context));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080080 if (context != NULL) {
Dave Sparks5e271152009-06-23 17:30:11 -070081 camera = context->getCamera();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080082 }
83 LOGV("get_native_camera: context=%p, camera=%p", context, camera.get());
84 if (camera == 0) {
85 jniThrowException(env, "java/lang/RuntimeException", "Method called after release()");
86 }
87
88 if (pContext != NULL) *pContext = context;
89 return camera;
90}
91
Dave Sparks5e271152009-06-23 17:30:11 -070092JNICameraContext::JNICameraContext(JNIEnv* env, jobject weak_this, jclass clazz, const sp<Camera>& camera)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080093{
Dave Sparks5e271152009-06-23 17:30:11 -070094 mCameraJObjectWeak = env->NewGlobalRef(weak_this);
95 mCameraJClass = (jclass)env->NewGlobalRef(clazz);
96 mCamera = camera;
Andrew Harp94927df2009-10-20 01:47:05 -040097
98 mManualBufferMode = false;
99 mManualCameraCallbackSet = false;
Dave Sparks5e271152009-06-23 17:30:11 -0700100}
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800101
Dave Sparks5e271152009-06-23 17:30:11 -0700102void JNICameraContext::release()
103{
104 LOGV("release");
105 Mutex::Autolock _l(mLock);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800106 JNIEnv *env = AndroidRuntime::getJNIEnv();
Dave Sparks5e271152009-06-23 17:30:11 -0700107
108 if (mCameraJObjectWeak != NULL) {
109 env->DeleteGlobalRef(mCameraJObjectWeak);
110 mCameraJObjectWeak = NULL;
111 }
112 if (mCameraJClass != NULL) {
113 env->DeleteGlobalRef(mCameraJClass);
114 mCameraJClass = NULL;
115 }
Andrew Harp94927df2009-10-20 01:47:05 -0400116 clearCallbackBuffers_l(env);
Dave Sparks5e271152009-06-23 17:30:11 -0700117 mCamera.clear();
118}
119
120void JNICameraContext::notify(int32_t msgType, int32_t ext1, int32_t ext2)
121{
122 LOGV("notify");
123
124 // VM pointer will be NULL if object is released
125 Mutex::Autolock _l(mLock);
126 if (mCameraJObjectWeak == NULL) {
127 LOGW("callback on dead camera object");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800128 return;
129 }
Dave Sparks5e271152009-06-23 17:30:11 -0700130 JNIEnv *env = AndroidRuntime::getJNIEnv();
Dave Sparksc62f9bd2009-06-26 13:33:32 -0700131 env->CallStaticVoidMethod(mCameraJClass, fields.post_event,
Chih-Chung Chang6157de02009-09-24 15:29:35 -0700132 mCameraJObjectWeak, msgType, ext1, ext2, NULL);
Dave Sparks5e271152009-06-23 17:30:11 -0700133}
134
135void JNICameraContext::copyAndPost(JNIEnv* env, const sp<IMemory>& dataPtr, int msgType)
136{
137 jbyteArray obj = NULL;
138
139 // allocate Java byte array and copy data
140 if (dataPtr != NULL) {
141 ssize_t offset;
142 size_t size;
143 sp<IMemoryHeap> heap = dataPtr->getMemory(&offset, &size);
144 LOGV("postData: off=%d, size=%d", offset, size);
145 uint8_t *heapBase = (uint8_t*)heap->base();
146
147 if (heapBase != NULL) {
Dave Sparksc4ca4202009-07-13 09:38:20 -0700148 const jbyte* data = reinterpret_cast<const jbyte*>(heapBase + offset);
Andrew Harp94927df2009-10-20 01:47:05 -0400149
150 if (!mManualBufferMode) {
151 LOGV("Allocating callback buffer");
152 obj = env->NewByteArray(size);
153 } else {
154 // Vector access should be protected by lock in postData()
155 if(!mCallbackBuffers.isEmpty()) {
156 LOGV("Using callback buffer from queue of length %d", mCallbackBuffers.size());
157 jbyteArray globalBuffer = mCallbackBuffers.itemAt(0);
158 mCallbackBuffers.removeAt(0);
159
160 obj = (jbyteArray)env->NewLocalRef(globalBuffer);
161 env->DeleteGlobalRef(globalBuffer);
162
163 if (obj != NULL) {
164 jsize bufferLength = env->GetArrayLength(obj);
165 if ((int)bufferLength < (int)size) {
166 LOGE("Manually set buffer was too small! Expected %d bytes, but got %d!",
167 size, bufferLength);
168 env->DeleteLocalRef(obj);
169 return;
170 }
171 }
172 }
173
174 if(mCallbackBuffers.isEmpty()) {
Wu-cheng Lif5dbf6a2010-01-30 22:34:48 +0800175 LOGV("Out of buffers, clearing callback!");
Andrew Harp94927df2009-10-20 01:47:05 -0400176 mCamera->setPreviewCallbackFlags(FRAME_CALLBACK_FLAG_NOOP);
177 mManualCameraCallbackSet = false;
178
179 if (obj == NULL) {
180 return;
181 }
182 }
183 }
184
Dave Sparks5e271152009-06-23 17:30:11 -0700185 if (obj == NULL) {
186 LOGE("Couldn't allocate byte array for JPEG data");
187 env->ExceptionClear();
188 } else {
Dave Sparksa95f4952009-07-10 18:13:36 -0700189 env->SetByteArrayRegion(obj, 0, size, data);
Dave Sparks5e271152009-06-23 17:30:11 -0700190 }
191 } else {
192 LOGE("image heap is NULL");
193 }
194 }
195
196 // post image data to Java
197 env->CallStaticVoidMethod(mCameraJClass, fields.post_event,
198 mCameraJObjectWeak, msgType, 0, 0, obj);
199 if (obj) {
200 env->DeleteLocalRef(obj);
201 }
202}
203
204void JNICameraContext::postData(int32_t msgType, const sp<IMemory>& dataPtr)
205{
206 // VM pointer will be NULL if object is released
207 Mutex::Autolock _l(mLock);
208 JNIEnv *env = AndroidRuntime::getJNIEnv();
Dave Sparksd0cbb1a2009-06-29 19:03:33 -0700209 if (mCameraJObjectWeak == NULL) {
210 LOGW("callback on dead camera object");
211 return;
212 }
Dave Sparks5e271152009-06-23 17:30:11 -0700213
214 // return data based on callback type
215 switch(msgType) {
Dave Sparks5e271152009-06-23 17:30:11 -0700216 case CAMERA_MSG_VIDEO_FRAME:
Dave Sparksc62f9bd2009-06-26 13:33:32 -0700217 // should never happen
Dave Sparks5e271152009-06-23 17:30:11 -0700218 break;
Dave Sparksc62f9bd2009-06-26 13:33:32 -0700219 // don't return raw data to Java
Dave Sparks5e271152009-06-23 17:30:11 -0700220 case CAMERA_MSG_RAW_IMAGE:
221 LOGV("rawCallback");
222 env->CallStaticVoidMethod(mCameraJClass, fields.post_event,
Dave Sparksc62f9bd2009-06-26 13:33:32 -0700223 mCameraJObjectWeak, msgType, 0, 0, NULL);
Dave Sparks5e271152009-06-23 17:30:11 -0700224 break;
225 default:
Dave Sparksda578562009-07-21 08:43:29 -0700226 // TODO: Change to LOGV
Dave Sparksc95a1762010-01-04 08:55:04 -0800227 LOGV("dataCallback(%d, %p)", msgType, dataPtr.get());
Dave Sparksc62f9bd2009-06-26 13:33:32 -0700228 copyAndPost(env, dataPtr, msgType);
Dave Sparks5e271152009-06-23 17:30:11 -0700229 break;
230 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800231}
232
Dave Sparks59c1a932009-07-08 15:56:53 -0700233void JNICameraContext::postDataTimestamp(nsecs_t timestamp, int32_t msgType, const sp<IMemory>& dataPtr)
234{
235 // TODO: plumb up to Java. For now, just drop the timestamp
236 postData(msgType, dataPtr);
237}
238
Andrew Harp94927df2009-10-20 01:47:05 -0400239void JNICameraContext::setCallbackMode(JNIEnv *env, bool installed, bool manualMode)
240{
241 Mutex::Autolock _l(mLock);
242 mManualBufferMode = manualMode;
243 mManualCameraCallbackSet = false;
244
245 // In order to limit the over usage of binder threads, all non-manual buffer
246 // callbacks use FRAME_CALLBACK_FLAG_BARCODE_SCANNER mode now.
247 //
248 // Continuous callbacks will have the callback re-registered from handleMessage.
249 // Manual buffer mode will operate as fast as possible, relying on the finite supply
250 // of buffers for throttling.
251
252 if (!installed) {
253 mCamera->setPreviewCallbackFlags(FRAME_CALLBACK_FLAG_NOOP);
254 clearCallbackBuffers_l(env);
255 } else if (mManualBufferMode) {
256 if (!mCallbackBuffers.isEmpty()) {
257 mCamera->setPreviewCallbackFlags(FRAME_CALLBACK_FLAG_CAMERA);
258 mManualCameraCallbackSet = true;
259 }
260 } else {
261 mCamera->setPreviewCallbackFlags(FRAME_CALLBACK_FLAG_BARCODE_SCANNER);
262 clearCallbackBuffers_l(env);
263 }
264}
265
266void JNICameraContext::addCallbackBuffer(JNIEnv *env, jbyteArray cbb)
267{
268 if (cbb != NULL) {
269 Mutex::Autolock _l(mLock);
270 jbyteArray callbackBuffer = (jbyteArray)env->NewGlobalRef(cbb);
271 mCallbackBuffers.push(cbb);
272
273 LOGV("Adding callback buffer to queue, %d total", mCallbackBuffers.size());
274
275 // We want to make sure the camera knows we're ready for the next frame.
276 // This may have come unset had we not had a callbackbuffer ready for it last time.
277 if (mManualBufferMode && !mManualCameraCallbackSet) {
278 mCamera->setPreviewCallbackFlags(FRAME_CALLBACK_FLAG_CAMERA);
279 mManualCameraCallbackSet = true;
280 }
281 } else {
282 LOGE("Null byte array!");
283 }
284}
285
286void JNICameraContext::clearCallbackBuffers_l(JNIEnv *env)
287{
288 LOGV("Clearing callback buffers, %d remained", mCallbackBuffers.size());
289 while(!mCallbackBuffers.isEmpty()) {
290 env->DeleteGlobalRef(mCallbackBuffers.top());
291 mCallbackBuffers.pop();
292 }
293}
294
Chih-Chung Change25cc652010-05-06 16:36:58 +0800295static jint android_hardware_Camera_getNumberOfCameras(JNIEnv *env, jobject thiz)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800296{
Chih-Chung Change25cc652010-05-06 16:36:58 +0800297 return Camera::getNumberOfCameras();
298}
299
Chih-Chung Changb8bb78f2010-06-10 13:32:16 +0800300static void android_hardware_Camera_getCameraInfo(JNIEnv *env, jobject thiz,
301 jint cameraId, jobject info_obj)
302{
303 CameraInfo cameraInfo;
304 status_t rc = Camera::getCameraInfo(cameraId, &cameraInfo);
305 if (rc != NO_ERROR) {
306 jniThrowException(env, "java/lang/RuntimeException",
307 "Fail to get camera info");
308 return;
309 }
310 env->SetIntField(info_obj, fields.facing, cameraInfo.facing);
311 env->SetIntField(info_obj, fields.orientation, cameraInfo.orientation);
312}
313
Chih-Chung Change25cc652010-05-06 16:36:58 +0800314// connect to camera service
315static void android_hardware_Camera_native_setup(JNIEnv *env, jobject thiz,
316 jobject weak_this, jint cameraId)
317{
318 sp<Camera> camera = Camera::connect(cameraId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800319
320 if (camera == NULL) {
Wu-cheng Lifa3e5562009-05-04 19:38:43 +0800321 jniThrowException(env, "java/lang/RuntimeException",
322 "Fail to connect to camera service");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800323 return;
324 }
325
326 // make sure camera hardware is alive
327 if (camera->getStatus() != NO_ERROR) {
Wu-cheng Liba55b362009-06-10 16:55:39 +0800328 jniThrowException(env, "java/lang/RuntimeException", "Camera initialization failed");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800329 return;
330 }
331
332 jclass clazz = env->GetObjectClass(thiz);
333 if (clazz == NULL) {
Wu-cheng Liba55b362009-06-10 16:55:39 +0800334 jniThrowException(env, "java/lang/RuntimeException", "Can't find android/hardware/Camera");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800335 return;
336 }
337
338 // We use a weak reference so the Camera object can be garbage collected.
339 // The reference is only used as a proxy for callbacks.
Dave Sparks5e271152009-06-23 17:30:11 -0700340 sp<JNICameraContext> context = new JNICameraContext(env, weak_this, clazz, camera);
341 context->incStrong(thiz);
342 camera->setListener(context);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800343
344 // save context in opaque field
Dave Sparks5e271152009-06-23 17:30:11 -0700345 env->SetIntField(thiz, fields.context, (int)context.get());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800346}
347
348// disconnect from camera service
349// It's okay to call this when the native camera context is already null.
350// This handles the case where the user has called release() and the
351// finalizer is invoked later.
352static void android_hardware_Camera_release(JNIEnv *env, jobject thiz)
353{
Dave Sparksda578562009-07-21 08:43:29 -0700354 // TODO: Change to LOGV
Joe Onoratof5d95cb2010-01-07 21:48:32 -0500355 LOGV("release camera");
Dave Sparks5e271152009-06-23 17:30:11 -0700356 JNICameraContext* context = NULL;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800357 sp<Camera> camera;
358 {
359 Mutex::Autolock _l(sLock);
Dave Sparks5e271152009-06-23 17:30:11 -0700360 context = reinterpret_cast<JNICameraContext*>(env->GetIntField(thiz, fields.context));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800361
362 // Make sure we do not attempt to callback on a deleted Java object.
363 env->SetIntField(thiz, fields.context, 0);
364 }
365
366 // clean up if release has not been called before
367 if (context != NULL) {
Dave Sparks5e271152009-06-23 17:30:11 -0700368 camera = context->getCamera();
369 context->release();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800370 LOGV("native_release: context=%p camera=%p", context, camera.get());
371
372 // clear callbacks
373 if (camera != NULL) {
Dave Sparks5e271152009-06-23 17:30:11 -0700374 camera->setPreviewCallbackFlags(FRAME_CALLBACK_FLAG_NOOP);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800375 camera->disconnect();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800376 }
377
378 // remove context to prevent further Java access
Dave Sparks5e271152009-06-23 17:30:11 -0700379 context->decStrong(thiz);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800380 }
381}
382
383static void android_hardware_Camera_setPreviewDisplay(JNIEnv *env, jobject thiz, jobject jSurface)
384{
385 LOGV("setPreviewDisplay");
386 sp<Camera> camera = get_native_camera(env, thiz, NULL);
387 if (camera == 0) return;
388
Wu-cheng Lib8a10fe2009-06-23 23:37:36 +0800389 sp<Surface> surface = NULL;
390 if (jSurface != NULL) {
391 surface = reinterpret_cast<Surface*>(env->GetIntField(jSurface, fields.surface));
392 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800393 if (camera->setPreviewDisplay(surface) != NO_ERROR) {
394 jniThrowException(env, "java/io/IOException", "setPreviewDisplay failed");
395 }
396}
397
Jamie Gennisfd6f39e2010-12-20 12:15:00 -0800398static void android_hardware_Camera_setPreviewTexture(JNIEnv *env,
399 jobject thiz, jobject jSurfaceTexture)
400{
401 LOGV("setPreviewTexture");
402 sp<Camera> camera = get_native_camera(env, thiz, NULL);
403 if (camera == 0) return;
404
405 sp<SurfaceTexture> surfaceTexture = NULL;
406 if (jSurfaceTexture != NULL) {
407 surfaceTexture = reinterpret_cast<SurfaceTexture*>(env->GetIntField(
408 jSurfaceTexture, fields.surfaceTexture));
409 }
410 if (camera->setPreviewTexture(surfaceTexture) != NO_ERROR) {
411 jniThrowException(env, "java/io/IOException",
412 "setPreviewTexture failed");
413 }
414}
415
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800416static void android_hardware_Camera_startPreview(JNIEnv *env, jobject thiz)
417{
418 LOGV("startPreview");
419 sp<Camera> camera = get_native_camera(env, thiz, NULL);
420 if (camera == 0) return;
421
422 if (camera->startPreview() != NO_ERROR) {
Wu-cheng Liba55b362009-06-10 16:55:39 +0800423 jniThrowException(env, "java/lang/RuntimeException", "startPreview failed");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800424 return;
425 }
426}
427
428static void android_hardware_Camera_stopPreview(JNIEnv *env, jobject thiz)
429{
430 LOGV("stopPreview");
431 sp<Camera> c = get_native_camera(env, thiz, NULL);
432 if (c == 0) return;
433
434 c->stopPreview();
435}
436
437static bool android_hardware_Camera_previewEnabled(JNIEnv *env, jobject thiz)
438{
439 LOGV("previewEnabled");
440 sp<Camera> c = get_native_camera(env, thiz, NULL);
441 if (c == 0) return false;
442
443 return c->previewEnabled();
444}
445
Andrew Harp94927df2009-10-20 01:47:05 -0400446static void android_hardware_Camera_setHasPreviewCallback(JNIEnv *env, jobject thiz, jboolean installed, jboolean manualBuffer)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800447{
Andrew Harp94927df2009-10-20 01:47:05 -0400448 LOGV("setHasPreviewCallback: installed:%d, manualBuffer:%d", (int)installed, (int)manualBuffer);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800449 // Important: Only install preview_callback if the Java code has called
450 // setPreviewCallback() with a non-null value, otherwise we'd pay to memcpy
451 // each preview frame for nothing.
Dave Sparks5e271152009-06-23 17:30:11 -0700452 JNICameraContext* context;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800453 sp<Camera> camera = get_native_camera(env, thiz, &context);
454 if (camera == 0) return;
455
Andrew Harp94927df2009-10-20 01:47:05 -0400456 // setCallbackMode will take care of setting the context flags and calling
457 // camera->setPreviewCallbackFlags within a mutex for us.
458 context->setCallbackMode(env, installed, manualBuffer);
459}
460
461static void android_hardware_Camera_addCallbackBuffer(JNIEnv *env, jobject thiz, jbyteArray bytes) {
462 LOGV("addCallbackBuffer");
463
464 JNICameraContext* context = reinterpret_cast<JNICameraContext*>(env->GetIntField(thiz, fields.context));
465
466 if (context != NULL) {
467 context->addCallbackBuffer(env, bytes);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800468 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800469}
470
471static void android_hardware_Camera_autoFocus(JNIEnv *env, jobject thiz)
472{
473 LOGV("autoFocus");
Dave Sparks5e271152009-06-23 17:30:11 -0700474 JNICameraContext* context;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800475 sp<Camera> c = get_native_camera(env, thiz, &context);
476 if (c == 0) return;
477
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800478 if (c->autoFocus() != NO_ERROR) {
Wu-cheng Liba55b362009-06-10 16:55:39 +0800479 jniThrowException(env, "java/lang/RuntimeException", "autoFocus failed");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800480 }
481}
482
Chih-Chung Chang244f8c22009-09-15 14:51:56 +0800483static void android_hardware_Camera_cancelAutoFocus(JNIEnv *env, jobject thiz)
484{
485 LOGV("cancelAutoFocus");
486 JNICameraContext* context;
487 sp<Camera> c = get_native_camera(env, thiz, &context);
488 if (c == 0) return;
489
490 if (c->cancelAutoFocus() != NO_ERROR) {
491 jniThrowException(env, "java/lang/RuntimeException", "cancelAutoFocus failed");
492 }
493}
494
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800495static void android_hardware_Camera_takePicture(JNIEnv *env, jobject thiz)
496{
497 LOGV("takePicture");
Dave Sparks5e271152009-06-23 17:30:11 -0700498 JNICameraContext* context;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800499 sp<Camera> camera = get_native_camera(env, thiz, &context);
500 if (camera == 0) return;
501
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800502 if (camera->takePicture() != NO_ERROR) {
Wu-cheng Liba55b362009-06-10 16:55:39 +0800503 jniThrowException(env, "java/lang/RuntimeException", "takePicture failed");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800504 return;
505 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800506}
507
508static void android_hardware_Camera_setParameters(JNIEnv *env, jobject thiz, jstring params)
509{
510 LOGV("setParameters");
511 sp<Camera> camera = get_native_camera(env, thiz, NULL);
512 if (camera == 0) return;
513
514 const jchar* str = env->GetStringCritical(params, 0);
515 String8 params8;
516 if (params) {
517 params8 = String8(str, env->GetStringLength(params));
518 env->ReleaseStringCritical(params, str);
519 }
520 if (camera->setParameters(params8) != NO_ERROR) {
Wu-cheng Liba55b362009-06-10 16:55:39 +0800521 jniThrowException(env, "java/lang/RuntimeException", "setParameters failed");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800522 return;
523 }
524}
525
526static jstring android_hardware_Camera_getParameters(JNIEnv *env, jobject thiz)
527{
528 LOGV("getParameters");
529 sp<Camera> camera = get_native_camera(env, thiz, NULL);
530 if (camera == 0) return 0;
531
532 return env->NewStringUTF(camera->getParameters().string());
533}
534
535static void android_hardware_Camera_reconnect(JNIEnv *env, jobject thiz)
536{
537 LOGV("reconnect");
538 sp<Camera> camera = get_native_camera(env, thiz, NULL);
539 if (camera == 0) return;
540
541 if (camera->reconnect() != NO_ERROR) {
542 jniThrowException(env, "java/io/IOException", "reconnect failed");
543 return;
544 }
545}
546
Wu-cheng Liffe1cf22009-09-10 16:49:17 +0800547static void android_hardware_Camera_lock(JNIEnv *env, jobject thiz)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800548{
549 LOGV("lock");
550 sp<Camera> camera = get_native_camera(env, thiz, NULL);
Wu-cheng Liffe1cf22009-09-10 16:49:17 +0800551 if (camera == 0) return;
552
553 if (camera->lock() != NO_ERROR) {
554 jniThrowException(env, "java/lang/RuntimeException", "lock failed");
555 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800556}
557
Wu-cheng Liffe1cf22009-09-10 16:49:17 +0800558static void android_hardware_Camera_unlock(JNIEnv *env, jobject thiz)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800559{
560 LOGV("unlock");
561 sp<Camera> camera = get_native_camera(env, thiz, NULL);
Wu-cheng Liffe1cf22009-09-10 16:49:17 +0800562 if (camera == 0) return;
563
564 if (camera->unlock() != NO_ERROR) {
565 jniThrowException(env, "java/lang/RuntimeException", "unlock failed");
566 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800567}
568
Wu-cheng Li36f68b82009-09-28 16:14:58 -0700569static void android_hardware_Camera_startSmoothZoom(JNIEnv *env, jobject thiz, jint value)
570{
Joe Onoratof5d95cb2010-01-07 21:48:32 -0500571 LOGV("startSmoothZoom");
Wu-cheng Li36f68b82009-09-28 16:14:58 -0700572 sp<Camera> camera = get_native_camera(env, thiz, NULL);
573 if (camera == 0) return;
574
Wu-cheng Li0ca25192010-03-29 16:21:12 +0800575 status_t rc = camera->sendCommand(CAMERA_CMD_START_SMOOTH_ZOOM, value, 0);
576 if (rc == BAD_VALUE) {
577 char msg[64];
578 sprintf(msg, "invalid zoom value=%d", value);
579 jniThrowException(env, "java/lang/IllegalArgumentException", msg);
580 } else if (rc != NO_ERROR) {
Wu-cheng Li36f68b82009-09-28 16:14:58 -0700581 jniThrowException(env, "java/lang/RuntimeException", "start smooth zoom failed");
582 }
583}
584
585static void android_hardware_Camera_stopSmoothZoom(JNIEnv *env, jobject thiz)
586{
Joe Onoratof5d95cb2010-01-07 21:48:32 -0500587 LOGV("stopSmoothZoom");
Wu-cheng Li36f68b82009-09-28 16:14:58 -0700588 sp<Camera> camera = get_native_camera(env, thiz, NULL);
589 if (camera == 0) return;
590
591 if (camera->sendCommand(CAMERA_CMD_STOP_SMOOTH_ZOOM, 0, 0) != NO_ERROR) {
592 jniThrowException(env, "java/lang/RuntimeException", "stop smooth zoom failed");
593 }
594}
595
Chih-Chung Changd1d77062010-01-22 17:49:48 -0800596static void android_hardware_Camera_setDisplayOrientation(JNIEnv *env, jobject thiz,
597 jint value)
598{
599 LOGV("setDisplayOrientation");
600 sp<Camera> camera = get_native_camera(env, thiz, NULL);
601 if (camera == 0) return;
602
603 if (camera->sendCommand(CAMERA_CMD_SET_DISPLAY_ORIENTATION, value, 0) != NO_ERROR) {
604 jniThrowException(env, "java/lang/RuntimeException", "set display orientation failed");
605 }
606}
607
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800608//-------------------------------------------------
609
610static JNINativeMethod camMethods[] = {
Chih-Chung Change25cc652010-05-06 16:36:58 +0800611 { "getNumberOfCameras",
612 "()I",
613 (void *)android_hardware_Camera_getNumberOfCameras },
Chih-Chung Changb8bb78f2010-06-10 13:32:16 +0800614 { "getCameraInfo",
615 "(ILandroid/hardware/Camera$CameraInfo;)V",
616 (void*)android_hardware_Camera_getCameraInfo },
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800617 { "native_setup",
Chih-Chung Change25cc652010-05-06 16:36:58 +0800618 "(Ljava/lang/Object;I)V",
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800619 (void*)android_hardware_Camera_native_setup },
620 { "native_release",
621 "()V",
622 (void*)android_hardware_Camera_release },
623 { "setPreviewDisplay",
624 "(Landroid/view/Surface;)V",
625 (void *)android_hardware_Camera_setPreviewDisplay },
Jamie Gennisfd6f39e2010-12-20 12:15:00 -0800626 { "setPreviewTexture",
627 "(Landroid/graphics/SurfaceTexture;)V",
628 (void *)android_hardware_Camera_setPreviewTexture },
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800629 { "startPreview",
630 "()V",
631 (void *)android_hardware_Camera_startPreview },
632 { "stopPreview",
633 "()V",
634 (void *)android_hardware_Camera_stopPreview },
635 { "previewEnabled",
636 "()Z",
637 (void *)android_hardware_Camera_previewEnabled },
638 { "setHasPreviewCallback",
639 "(ZZ)V",
640 (void *)android_hardware_Camera_setHasPreviewCallback },
Andrew Harp94927df2009-10-20 01:47:05 -0400641 { "addCallbackBuffer",
642 "([B)V",
643 (void *)android_hardware_Camera_addCallbackBuffer },
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800644 { "native_autoFocus",
645 "()V",
646 (void *)android_hardware_Camera_autoFocus },
Chih-Chung Chang244f8c22009-09-15 14:51:56 +0800647 { "native_cancelAutoFocus",
648 "()V",
649 (void *)android_hardware_Camera_cancelAutoFocus },
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800650 { "native_takePicture",
651 "()V",
652 (void *)android_hardware_Camera_takePicture },
653 { "native_setParameters",
654 "(Ljava/lang/String;)V",
655 (void *)android_hardware_Camera_setParameters },
656 { "native_getParameters",
657 "()Ljava/lang/String;",
658 (void *)android_hardware_Camera_getParameters },
659 { "reconnect",
660 "()V",
661 (void*)android_hardware_Camera_reconnect },
662 { "lock",
Wu-cheng Liffe1cf22009-09-10 16:49:17 +0800663 "()V",
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800664 (void*)android_hardware_Camera_lock },
665 { "unlock",
Wu-cheng Liffe1cf22009-09-10 16:49:17 +0800666 "()V",
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800667 (void*)android_hardware_Camera_unlock },
Wu-cheng Li36f68b82009-09-28 16:14:58 -0700668 { "startSmoothZoom",
669 "(I)V",
670 (void *)android_hardware_Camera_startSmoothZoom },
671 { "stopSmoothZoom",
672 "()V",
673 (void *)android_hardware_Camera_stopSmoothZoom },
Chih-Chung Changd1d77062010-01-22 17:49:48 -0800674 { "setDisplayOrientation",
675 "(I)V",
676 (void *)android_hardware_Camera_setDisplayOrientation },
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800677};
678
679struct field {
680 const char *class_name;
681 const char *field_name;
682 const char *field_type;
683 jfieldID *jfield;
684};
685
686static int find_fields(JNIEnv *env, field *fields, int count)
687{
688 for (int i = 0; i < count; i++) {
689 field *f = &fields[i];
690 jclass clazz = env->FindClass(f->class_name);
691 if (clazz == NULL) {
692 LOGE("Can't find %s", f->class_name);
693 return -1;
694 }
695
696 jfieldID field = env->GetFieldID(clazz, f->field_name, f->field_type);
697 if (field == NULL) {
698 LOGE("Can't find %s.%s", f->class_name, f->field_name);
699 return -1;
700 }
701
702 *(f->jfield) = field;
703 }
704
705 return 0;
706}
707
708// Get all the required offsets in java class and register native functions
709int register_android_hardware_Camera(JNIEnv *env)
710{
711 field fields_to_find[] = {
712 { "android/hardware/Camera", "mNativeContext", "I", &fields.context },
Chih-Chung Changb8bb78f2010-06-10 13:32:16 +0800713 { "android/view/Surface", ANDROID_VIEW_SURFACE_JNI_ID, "I", &fields.surface },
Jamie Gennisfd6f39e2010-12-20 12:15:00 -0800714 { "android/graphics/SurfaceTexture",
715 ANDROID_GRAPHICS_SURFACETEXTURE_JNI_ID, "I", &fields.surfaceTexture },
Wu-cheng Li3fd5fa42010-09-15 16:06:20 -0700716 { "android/hardware/Camera$CameraInfo", "facing", "I", &fields.facing },
717 { "android/hardware/Camera$CameraInfo", "orientation", "I", &fields.orientation },
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800718 };
719
720 if (find_fields(env, fields_to_find, NELEM(fields_to_find)) < 0)
721 return -1;
722
723 jclass clazz = env->FindClass("android/hardware/Camera");
724 fields.post_event = env->GetStaticMethodID(clazz, "postEventFromNative",
725 "(Ljava/lang/Object;IIILjava/lang/Object;)V");
726 if (fields.post_event == NULL) {
727 LOGE("Can't find android/hardware/Camera.postEventFromNative");
728 return -1;
729 }
730
731
732 // Register native functions
733 return AndroidRuntime::registerNativeMethods(env, "android/hardware/Camera",
734 camMethods, NELEM(camMethods));
735}