blob: 4687ee07f6fdc4a6e10240b73dddbfb4910e8b2a [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);
James Donge00cab72011-02-17 16:38:06 -080056 void addCallbackBuffer(JNIEnv *env, jbyteArray cbb, int msgType);
Andrew Harp94927df2009-10-20 01:47:05 -040057 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; }
James Donge00cab72011-02-17 16:38:06 -080059 bool isRawImageCallbackBufferAvailable() const;
Dave Sparks5e271152009-06-23 17:30:11 -070060 void release();
61
62private:
63 void copyAndPost(JNIEnv* env, const sp<IMemory>& dataPtr, int msgType);
James Donge00cab72011-02-17 16:38:06 -080064 void clearCallbackBuffers_l(JNIEnv *env, Vector<jbyteArray> *buffers);
Andrew Harp94927df2009-10-20 01:47:05 -040065 void clearCallbackBuffers_l(JNIEnv *env);
James Donge00cab72011-02-17 16:38:06 -080066 jbyteArray getCallbackBuffer(JNIEnv *env, Vector<jbyteArray> *buffers, size_t bufferSize);
Dave Sparks5e271152009-06-23 17:30:11 -070067
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080068 jobject mCameraJObjectWeak; // weak reference to java object
69 jclass mCameraJClass; // strong reference to java class
Wu-cheng Liffe1cf22009-09-10 16:49:17 +080070 sp<Camera> mCamera; // strong reference to native object
Dave Sparks5e271152009-06-23 17:30:11 -070071 Mutex mLock;
Andrew Harp94927df2009-10-20 01:47:05 -040072
James Donge00cab72011-02-17 16:38:06 -080073 /*
74 * Global reference application-managed raw image buffer queue.
75 *
76 * Manual-only mode is supported for raw image callbacks, which is
77 * set whenever method addCallbackBuffer() with msgType =
78 * CAMERA_MSG_RAW_IMAGE is called; otherwise, null is returned
79 * with raw image callbacks.
80 */
81 Vector<jbyteArray> mRawImageCallbackBuffers;
82
83 /*
84 * Application-managed preview buffer queue and the flags
85 * associated with the usage of the preview buffer callback.
86 */
Andrew Harp94927df2009-10-20 01:47:05 -040087 Vector<jbyteArray> mCallbackBuffers; // Global reference application managed byte[]
88 bool mManualBufferMode; // Whether to use application managed buffers.
James Donge00cab72011-02-17 16:38:06 -080089 bool mManualCameraCallbackSet; // Whether the callback has been set, used to
90 // reduce unnecessary calls to set the callback.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080091};
92
James Donge00cab72011-02-17 16:38:06 -080093bool JNICameraContext::isRawImageCallbackBufferAvailable() const
94{
95 return !mRawImageCallbackBuffers.isEmpty();
96}
97
Dave Sparks5e271152009-06-23 17:30:11 -070098sp<Camera> get_native_camera(JNIEnv *env, jobject thiz, JNICameraContext** pContext)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080099{
100 sp<Camera> camera;
101 Mutex::Autolock _l(sLock);
Dave Sparks5e271152009-06-23 17:30:11 -0700102 JNICameraContext* context = reinterpret_cast<JNICameraContext*>(env->GetIntField(thiz, fields.context));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800103 if (context != NULL) {
Dave Sparks5e271152009-06-23 17:30:11 -0700104 camera = context->getCamera();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800105 }
106 LOGV("get_native_camera: context=%p, camera=%p", context, camera.get());
107 if (camera == 0) {
Elliott Hughes69a017b2011-04-08 14:10:28 -0700108 jniThrowRuntimeException(env, "Method called after release()");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800109 }
110
111 if (pContext != NULL) *pContext = context;
112 return camera;
113}
114
Dave Sparks5e271152009-06-23 17:30:11 -0700115JNICameraContext::JNICameraContext(JNIEnv* env, jobject weak_this, jclass clazz, const sp<Camera>& camera)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800116{
Dave Sparks5e271152009-06-23 17:30:11 -0700117 mCameraJObjectWeak = env->NewGlobalRef(weak_this);
118 mCameraJClass = (jclass)env->NewGlobalRef(clazz);
119 mCamera = camera;
Andrew Harp94927df2009-10-20 01:47:05 -0400120
121 mManualBufferMode = false;
122 mManualCameraCallbackSet = false;
Dave Sparks5e271152009-06-23 17:30:11 -0700123}
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800124
Dave Sparks5e271152009-06-23 17:30:11 -0700125void JNICameraContext::release()
126{
127 LOGV("release");
128 Mutex::Autolock _l(mLock);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800129 JNIEnv *env = AndroidRuntime::getJNIEnv();
Dave Sparks5e271152009-06-23 17:30:11 -0700130
131 if (mCameraJObjectWeak != NULL) {
132 env->DeleteGlobalRef(mCameraJObjectWeak);
133 mCameraJObjectWeak = NULL;
134 }
135 if (mCameraJClass != NULL) {
136 env->DeleteGlobalRef(mCameraJClass);
137 mCameraJClass = NULL;
138 }
Andrew Harp94927df2009-10-20 01:47:05 -0400139 clearCallbackBuffers_l(env);
Dave Sparks5e271152009-06-23 17:30:11 -0700140 mCamera.clear();
141}
142
143void JNICameraContext::notify(int32_t msgType, int32_t ext1, int32_t ext2)
144{
145 LOGV("notify");
146
147 // VM pointer will be NULL if object is released
148 Mutex::Autolock _l(mLock);
149 if (mCameraJObjectWeak == NULL) {
150 LOGW("callback on dead camera object");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800151 return;
152 }
Dave Sparks5e271152009-06-23 17:30:11 -0700153 JNIEnv *env = AndroidRuntime::getJNIEnv();
James Donge00cab72011-02-17 16:38:06 -0800154
155 /*
156 * If the notification or msgType is CAMERA_MSG_RAW_IMAGE_NOTIFY, change it
157 * to CAMERA_MSG_RAW_IMAGE since CAMERA_MSG_RAW_IMAGE_NOTIFY is not exposed
158 * to the Java app.
159 */
160 if (msgType == CAMERA_MSG_RAW_IMAGE_NOTIFY) {
161 msgType = CAMERA_MSG_RAW_IMAGE;
162 }
163
Dave Sparksc62f9bd2009-06-26 13:33:32 -0700164 env->CallStaticVoidMethod(mCameraJClass, fields.post_event,
Chih-Chung Chang6157de02009-09-24 15:29:35 -0700165 mCameraJObjectWeak, msgType, ext1, ext2, NULL);
Dave Sparks5e271152009-06-23 17:30:11 -0700166}
167
James Donge00cab72011-02-17 16:38:06 -0800168jbyteArray JNICameraContext::getCallbackBuffer(
169 JNIEnv* env, Vector<jbyteArray>* buffers, size_t bufferSize)
170{
171 jbyteArray obj = NULL;
172
173 // Vector access should be protected by lock in postData()
174 if (!buffers->isEmpty()) {
175 LOGV("Using callback buffer from queue of length %d", buffers->size());
176 jbyteArray globalBuffer = buffers->itemAt(0);
177 buffers->removeAt(0);
178
179 obj = (jbyteArray)env->NewLocalRef(globalBuffer);
180 env->DeleteGlobalRef(globalBuffer);
181
182 if (obj != NULL) {
183 jsize bufferLength = env->GetArrayLength(obj);
184 if ((int)bufferLength < (int)bufferSize) {
185 LOGE("Callback buffer was too small! Expected %d bytes, but got %d bytes!",
186 bufferSize, bufferLength);
187 env->DeleteLocalRef(obj);
188 return NULL;
189 }
190 }
191 }
192
193 return obj;
194}
195
Dave Sparks5e271152009-06-23 17:30:11 -0700196void JNICameraContext::copyAndPost(JNIEnv* env, const sp<IMemory>& dataPtr, int msgType)
197{
198 jbyteArray obj = NULL;
199
200 // allocate Java byte array and copy data
201 if (dataPtr != NULL) {
202 ssize_t offset;
203 size_t size;
204 sp<IMemoryHeap> heap = dataPtr->getMemory(&offset, &size);
James Donge00cab72011-02-17 16:38:06 -0800205 LOGV("copyAndPost: off=%ld, size=%d", offset, size);
Dave Sparks5e271152009-06-23 17:30:11 -0700206 uint8_t *heapBase = (uint8_t*)heap->base();
207
208 if (heapBase != NULL) {
Dave Sparksc4ca4202009-07-13 09:38:20 -0700209 const jbyte* data = reinterpret_cast<const jbyte*>(heapBase + offset);
Andrew Harp94927df2009-10-20 01:47:05 -0400210
211 if (!mManualBufferMode) {
212 LOGV("Allocating callback buffer");
213 obj = env->NewByteArray(size);
214 } else {
James Donge00cab72011-02-17 16:38:06 -0800215 switch (msgType) {
216 case CAMERA_MSG_PREVIEW_FRAME: {
217 obj = getCallbackBuffer(env, &mCallbackBuffers, size);
Andrew Harp94927df2009-10-20 01:47:05 -0400218
James Donge00cab72011-02-17 16:38:06 -0800219 if (mCallbackBuffers.isEmpty()) {
220 LOGV("Out of buffers, clearing callback!");
Iliyan Malchev9c7ac0d2011-04-14 16:51:21 -0700221 mCamera->setPreviewCallbackFlags(CAMERA_FRAME_CALLBACK_FLAG_NOOP);
James Donge00cab72011-02-17 16:38:06 -0800222 mManualCameraCallbackSet = false;
Andrew Harp94927df2009-10-20 01:47:05 -0400223
James Donge00cab72011-02-17 16:38:06 -0800224 if (obj == NULL) {
225 return;
226 }
Andrew Harp94927df2009-10-20 01:47:05 -0400227 }
James Donge00cab72011-02-17 16:38:06 -0800228 break;
Andrew Harp94927df2009-10-20 01:47:05 -0400229 }
James Donge00cab72011-02-17 16:38:06 -0800230 case CAMERA_MSG_RAW_IMAGE: {
231 obj = getCallbackBuffer(env, &mRawImageCallbackBuffers, size);
232 break;
233 }
234 default: {
Elliott Hughes69a017b2011-04-08 14:10:28 -0700235 jniThrowRuntimeException(env, "Unsupported message type");
Andrew Harp94927df2009-10-20 01:47:05 -0400236 return;
237 }
238 }
239 }
240
Dave Sparks5e271152009-06-23 17:30:11 -0700241 if (obj == NULL) {
242 LOGE("Couldn't allocate byte array for JPEG data");
243 env->ExceptionClear();
244 } else {
Dave Sparksa95f4952009-07-10 18:13:36 -0700245 env->SetByteArrayRegion(obj, 0, size, data);
Dave Sparks5e271152009-06-23 17:30:11 -0700246 }
247 } else {
248 LOGE("image heap is NULL");
249 }
250 }
251
252 // post image data to Java
253 env->CallStaticVoidMethod(mCameraJClass, fields.post_event,
254 mCameraJObjectWeak, msgType, 0, 0, obj);
255 if (obj) {
256 env->DeleteLocalRef(obj);
257 }
258}
259
260void JNICameraContext::postData(int32_t msgType, const sp<IMemory>& dataPtr)
261{
262 // VM pointer will be NULL if object is released
263 Mutex::Autolock _l(mLock);
264 JNIEnv *env = AndroidRuntime::getJNIEnv();
Dave Sparksd0cbb1a2009-06-29 19:03:33 -0700265 if (mCameraJObjectWeak == NULL) {
266 LOGW("callback on dead camera object");
267 return;
268 }
Dave Sparks5e271152009-06-23 17:30:11 -0700269
270 // return data based on callback type
James Donge00cab72011-02-17 16:38:06 -0800271 switch (msgType) {
272 case CAMERA_MSG_VIDEO_FRAME:
273 // should never happen
274 break;
275
276 // For backward-compatibility purpose, if there is no callback
277 // buffer for raw image, the callback returns null.
278 case CAMERA_MSG_RAW_IMAGE:
279 LOGV("rawCallback");
280 if (mRawImageCallbackBuffers.isEmpty()) {
281 env->CallStaticVoidMethod(mCameraJClass, fields.post_event,
282 mCameraJObjectWeak, msgType, 0, 0, NULL);
283 } else {
284 copyAndPost(env, dataPtr, msgType);
285 }
286 break;
287
288 default:
289 LOGV("dataCallback(%d, %p)", msgType, dataPtr.get());
290 copyAndPost(env, dataPtr, msgType);
291 break;
Dave Sparks5e271152009-06-23 17:30:11 -0700292 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800293}
294
Dave Sparks59c1a932009-07-08 15:56:53 -0700295void JNICameraContext::postDataTimestamp(nsecs_t timestamp, int32_t msgType, const sp<IMemory>& dataPtr)
296{
297 // TODO: plumb up to Java. For now, just drop the timestamp
298 postData(msgType, dataPtr);
299}
300
Andrew Harp94927df2009-10-20 01:47:05 -0400301void JNICameraContext::setCallbackMode(JNIEnv *env, bool installed, bool manualMode)
302{
303 Mutex::Autolock _l(mLock);
304 mManualBufferMode = manualMode;
305 mManualCameraCallbackSet = false;
306
307 // In order to limit the over usage of binder threads, all non-manual buffer
Iliyan Malchev9c7ac0d2011-04-14 16:51:21 -0700308 // callbacks use CAMERA_FRAME_CALLBACK_FLAG_BARCODE_SCANNER mode now.
Andrew Harp94927df2009-10-20 01:47:05 -0400309 //
310 // Continuous callbacks will have the callback re-registered from handleMessage.
311 // Manual buffer mode will operate as fast as possible, relying on the finite supply
312 // of buffers for throttling.
313
314 if (!installed) {
Iliyan Malchev9c7ac0d2011-04-14 16:51:21 -0700315 mCamera->setPreviewCallbackFlags(CAMERA_FRAME_CALLBACK_FLAG_NOOP);
James Donge00cab72011-02-17 16:38:06 -0800316 clearCallbackBuffers_l(env, &mCallbackBuffers);
Andrew Harp94927df2009-10-20 01:47:05 -0400317 } else if (mManualBufferMode) {
318 if (!mCallbackBuffers.isEmpty()) {
Iliyan Malchev9c7ac0d2011-04-14 16:51:21 -0700319 mCamera->setPreviewCallbackFlags(CAMERA_FRAME_CALLBACK_FLAG_CAMERA);
Andrew Harp94927df2009-10-20 01:47:05 -0400320 mManualCameraCallbackSet = true;
321 }
322 } else {
Iliyan Malchev9c7ac0d2011-04-14 16:51:21 -0700323 mCamera->setPreviewCallbackFlags(CAMERA_FRAME_CALLBACK_FLAG_BARCODE_SCANNER);
James Donge00cab72011-02-17 16:38:06 -0800324 clearCallbackBuffers_l(env, &mCallbackBuffers);
Andrew Harp94927df2009-10-20 01:47:05 -0400325 }
326}
327
James Donge00cab72011-02-17 16:38:06 -0800328void JNICameraContext::addCallbackBuffer(
329 JNIEnv *env, jbyteArray cbb, int msgType)
Andrew Harp94927df2009-10-20 01:47:05 -0400330{
James Donge00cab72011-02-17 16:38:06 -0800331 LOGV("addCallbackBuffer: 0x%x", msgType);
Andrew Harp94927df2009-10-20 01:47:05 -0400332 if (cbb != NULL) {
333 Mutex::Autolock _l(mLock);
James Donge00cab72011-02-17 16:38:06 -0800334 switch (msgType) {
335 case CAMERA_MSG_PREVIEW_FRAME: {
336 jbyteArray callbackBuffer = (jbyteArray)env->NewGlobalRef(cbb);
337 mCallbackBuffers.push(callbackBuffer);
Andrew Harp94927df2009-10-20 01:47:05 -0400338
James Donge00cab72011-02-17 16:38:06 -0800339 LOGV("Adding callback buffer to queue, %d total",
340 mCallbackBuffers.size());
Andrew Harp94927df2009-10-20 01:47:05 -0400341
James Donge00cab72011-02-17 16:38:06 -0800342 // We want to make sure the camera knows we're ready for the
343 // next frame. This may have come unset had we not had a
344 // callbackbuffer ready for it last time.
345 if (mManualBufferMode && !mManualCameraCallbackSet) {
Iliyan Malchev9c7ac0d2011-04-14 16:51:21 -0700346 mCamera->setPreviewCallbackFlags(CAMERA_FRAME_CALLBACK_FLAG_CAMERA);
James Donge00cab72011-02-17 16:38:06 -0800347 mManualCameraCallbackSet = true;
348 }
349 break;
350 }
351 case CAMERA_MSG_RAW_IMAGE: {
352 jbyteArray callbackBuffer = (jbyteArray)env->NewGlobalRef(cbb);
353 mRawImageCallbackBuffers.push(callbackBuffer);
354 break;
355 }
356 default: {
357 jniThrowException(env,
358 "java/lang/IllegalArgumentException",
359 "Unsupported message type");
360 return;
361 }
Andrew Harp94927df2009-10-20 01:47:05 -0400362 }
363 } else {
364 LOGE("Null byte array!");
365 }
366}
367
368void JNICameraContext::clearCallbackBuffers_l(JNIEnv *env)
369{
James Donge00cab72011-02-17 16:38:06 -0800370 clearCallbackBuffers_l(env, &mCallbackBuffers);
371 clearCallbackBuffers_l(env, &mRawImageCallbackBuffers);
372}
373
374void JNICameraContext::clearCallbackBuffers_l(JNIEnv *env, Vector<jbyteArray> *buffers) {
375 LOGV("Clearing callback buffers, %d remained", buffers->size());
376 while (!buffers->isEmpty()) {
377 env->DeleteGlobalRef(buffers->top());
378 buffers->pop();
Andrew Harp94927df2009-10-20 01:47:05 -0400379 }
380}
381
Chih-Chung Change25cc652010-05-06 16:36:58 +0800382static jint android_hardware_Camera_getNumberOfCameras(JNIEnv *env, jobject thiz)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800383{
Chih-Chung Change25cc652010-05-06 16:36:58 +0800384 return Camera::getNumberOfCameras();
385}
386
Chih-Chung Changb8bb78f2010-06-10 13:32:16 +0800387static void android_hardware_Camera_getCameraInfo(JNIEnv *env, jobject thiz,
388 jint cameraId, jobject info_obj)
389{
390 CameraInfo cameraInfo;
391 status_t rc = Camera::getCameraInfo(cameraId, &cameraInfo);
392 if (rc != NO_ERROR) {
Elliott Hughes69a017b2011-04-08 14:10:28 -0700393 jniThrowRuntimeException(env, "Fail to get camera info");
Chih-Chung Changb8bb78f2010-06-10 13:32:16 +0800394 return;
395 }
396 env->SetIntField(info_obj, fields.facing, cameraInfo.facing);
397 env->SetIntField(info_obj, fields.orientation, cameraInfo.orientation);
398}
399
Chih-Chung Change25cc652010-05-06 16:36:58 +0800400// connect to camera service
401static void android_hardware_Camera_native_setup(JNIEnv *env, jobject thiz,
402 jobject weak_this, jint cameraId)
403{
404 sp<Camera> camera = Camera::connect(cameraId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800405
406 if (camera == NULL) {
Elliott Hughes69a017b2011-04-08 14:10:28 -0700407 jniThrowRuntimeException(env, "Fail to connect to camera service");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800408 return;
409 }
410
411 // make sure camera hardware is alive
412 if (camera->getStatus() != NO_ERROR) {
Elliott Hughes69a017b2011-04-08 14:10:28 -0700413 jniThrowRuntimeException(env, "Camera initialization failed");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800414 return;
415 }
416
417 jclass clazz = env->GetObjectClass(thiz);
418 if (clazz == NULL) {
Elliott Hughes69a017b2011-04-08 14:10:28 -0700419 jniThrowRuntimeException(env, "Can't find android/hardware/Camera");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800420 return;
421 }
422
423 // We use a weak reference so the Camera object can be garbage collected.
424 // The reference is only used as a proxy for callbacks.
Dave Sparks5e271152009-06-23 17:30:11 -0700425 sp<JNICameraContext> context = new JNICameraContext(env, weak_this, clazz, camera);
426 context->incStrong(thiz);
427 camera->setListener(context);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800428
429 // save context in opaque field
Dave Sparks5e271152009-06-23 17:30:11 -0700430 env->SetIntField(thiz, fields.context, (int)context.get());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800431}
432
433// disconnect from camera service
434// It's okay to call this when the native camera context is already null.
435// This handles the case where the user has called release() and the
436// finalizer is invoked later.
437static void android_hardware_Camera_release(JNIEnv *env, jobject thiz)
438{
Dave Sparksda578562009-07-21 08:43:29 -0700439 // TODO: Change to LOGV
Joe Onoratof5d95cb2010-01-07 21:48:32 -0500440 LOGV("release camera");
Dave Sparks5e271152009-06-23 17:30:11 -0700441 JNICameraContext* context = NULL;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800442 sp<Camera> camera;
443 {
444 Mutex::Autolock _l(sLock);
Dave Sparks5e271152009-06-23 17:30:11 -0700445 context = reinterpret_cast<JNICameraContext*>(env->GetIntField(thiz, fields.context));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800446
447 // Make sure we do not attempt to callback on a deleted Java object.
448 env->SetIntField(thiz, fields.context, 0);
449 }
450
451 // clean up if release has not been called before
452 if (context != NULL) {
Dave Sparks5e271152009-06-23 17:30:11 -0700453 camera = context->getCamera();
454 context->release();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800455 LOGV("native_release: context=%p camera=%p", context, camera.get());
456
457 // clear callbacks
458 if (camera != NULL) {
Iliyan Malchev9c7ac0d2011-04-14 16:51:21 -0700459 camera->setPreviewCallbackFlags(CAMERA_FRAME_CALLBACK_FLAG_NOOP);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800460 camera->disconnect();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800461 }
462
463 // remove context to prevent further Java access
Dave Sparks5e271152009-06-23 17:30:11 -0700464 context->decStrong(thiz);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800465 }
466}
467
468static void android_hardware_Camera_setPreviewDisplay(JNIEnv *env, jobject thiz, jobject jSurface)
469{
470 LOGV("setPreviewDisplay");
471 sp<Camera> camera = get_native_camera(env, thiz, NULL);
472 if (camera == 0) return;
473
Wu-cheng Lib8a10fe2009-06-23 23:37:36 +0800474 sp<Surface> surface = NULL;
475 if (jSurface != NULL) {
476 surface = reinterpret_cast<Surface*>(env->GetIntField(jSurface, fields.surface));
477 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800478 if (camera->setPreviewDisplay(surface) != NO_ERROR) {
479 jniThrowException(env, "java/io/IOException", "setPreviewDisplay failed");
480 }
481}
482
Jamie Gennisfd6f39e2010-12-20 12:15:00 -0800483static void android_hardware_Camera_setPreviewTexture(JNIEnv *env,
484 jobject thiz, jobject jSurfaceTexture)
485{
486 LOGV("setPreviewTexture");
487 sp<Camera> camera = get_native_camera(env, thiz, NULL);
488 if (camera == 0) return;
489
490 sp<SurfaceTexture> surfaceTexture = NULL;
491 if (jSurfaceTexture != NULL) {
492 surfaceTexture = reinterpret_cast<SurfaceTexture*>(env->GetIntField(
493 jSurfaceTexture, fields.surfaceTexture));
494 }
495 if (camera->setPreviewTexture(surfaceTexture) != NO_ERROR) {
496 jniThrowException(env, "java/io/IOException",
497 "setPreviewTexture failed");
498 }
499}
500
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800501static void android_hardware_Camera_startPreview(JNIEnv *env, jobject thiz)
502{
503 LOGV("startPreview");
504 sp<Camera> camera = get_native_camera(env, thiz, NULL);
505 if (camera == 0) return;
506
507 if (camera->startPreview() != NO_ERROR) {
Elliott Hughes69a017b2011-04-08 14:10:28 -0700508 jniThrowRuntimeException(env, "startPreview failed");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800509 return;
510 }
511}
512
513static void android_hardware_Camera_stopPreview(JNIEnv *env, jobject thiz)
514{
515 LOGV("stopPreview");
516 sp<Camera> c = get_native_camera(env, thiz, NULL);
517 if (c == 0) return;
518
519 c->stopPreview();
520}
521
522static bool android_hardware_Camera_previewEnabled(JNIEnv *env, jobject thiz)
523{
524 LOGV("previewEnabled");
525 sp<Camera> c = get_native_camera(env, thiz, NULL);
526 if (c == 0) return false;
527
528 return c->previewEnabled();
529}
530
Andrew Harp94927df2009-10-20 01:47:05 -0400531static void android_hardware_Camera_setHasPreviewCallback(JNIEnv *env, jobject thiz, jboolean installed, jboolean manualBuffer)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800532{
Andrew Harp94927df2009-10-20 01:47:05 -0400533 LOGV("setHasPreviewCallback: installed:%d, manualBuffer:%d", (int)installed, (int)manualBuffer);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800534 // Important: Only install preview_callback if the Java code has called
535 // setPreviewCallback() with a non-null value, otherwise we'd pay to memcpy
536 // each preview frame for nothing.
Dave Sparks5e271152009-06-23 17:30:11 -0700537 JNICameraContext* context;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800538 sp<Camera> camera = get_native_camera(env, thiz, &context);
539 if (camera == 0) return;
540
Andrew Harp94927df2009-10-20 01:47:05 -0400541 // setCallbackMode will take care of setting the context flags and calling
542 // camera->setPreviewCallbackFlags within a mutex for us.
543 context->setCallbackMode(env, installed, manualBuffer);
544}
545
James Donge00cab72011-02-17 16:38:06 -0800546static void android_hardware_Camera_addCallbackBuffer(JNIEnv *env, jobject thiz, jbyteArray bytes, int msgType) {
547 LOGV("addCallbackBuffer: 0x%x", msgType);
Andrew Harp94927df2009-10-20 01:47:05 -0400548
549 JNICameraContext* context = reinterpret_cast<JNICameraContext*>(env->GetIntField(thiz, fields.context));
550
551 if (context != NULL) {
James Donge00cab72011-02-17 16:38:06 -0800552 context->addCallbackBuffer(env, bytes, msgType);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800553 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800554}
555
556static void android_hardware_Camera_autoFocus(JNIEnv *env, jobject thiz)
557{
558 LOGV("autoFocus");
Dave Sparks5e271152009-06-23 17:30:11 -0700559 JNICameraContext* context;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800560 sp<Camera> c = get_native_camera(env, thiz, &context);
561 if (c == 0) return;
562
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800563 if (c->autoFocus() != NO_ERROR) {
Elliott Hughes69a017b2011-04-08 14:10:28 -0700564 jniThrowRuntimeException(env, "autoFocus failed");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800565 }
566}
567
Chih-Chung Chang244f8c22009-09-15 14:51:56 +0800568static void android_hardware_Camera_cancelAutoFocus(JNIEnv *env, jobject thiz)
569{
570 LOGV("cancelAutoFocus");
571 JNICameraContext* context;
572 sp<Camera> c = get_native_camera(env, thiz, &context);
573 if (c == 0) return;
574
575 if (c->cancelAutoFocus() != NO_ERROR) {
Elliott Hughes69a017b2011-04-08 14:10:28 -0700576 jniThrowRuntimeException(env, "cancelAutoFocus failed");
Chih-Chung Chang244f8c22009-09-15 14:51:56 +0800577 }
578}
579
James Donge00cab72011-02-17 16:38:06 -0800580static void android_hardware_Camera_takePicture(JNIEnv *env, jobject thiz, int msgType)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800581{
582 LOGV("takePicture");
Dave Sparks5e271152009-06-23 17:30:11 -0700583 JNICameraContext* context;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800584 sp<Camera> camera = get_native_camera(env, thiz, &context);
585 if (camera == 0) return;
586
James Donge00cab72011-02-17 16:38:06 -0800587 /*
588 * When CAMERA_MSG_RAW_IMAGE is requested, if the raw image callback
589 * buffer is available, CAMERA_MSG_RAW_IMAGE is enabled to get the
590 * notification _and_ the data; otherwise, CAMERA_MSG_RAW_IMAGE_NOTIFY
591 * is enabled to receive the callback notification but no data.
592 *
593 * Note that CAMERA_MSG_RAW_IMAGE_NOTIFY is not exposed to the
594 * Java application.
595 */
596 if (msgType & CAMERA_MSG_RAW_IMAGE) {
597 LOGV("Enable raw image callback buffer");
598 if (!context->isRawImageCallbackBufferAvailable()) {
599 LOGV("Enable raw image notification, since no callback buffer exists");
600 msgType &= ~CAMERA_MSG_RAW_IMAGE;
601 msgType |= CAMERA_MSG_RAW_IMAGE_NOTIFY;
602 }
603 }
604
605 if (camera->takePicture(msgType) != NO_ERROR) {
Elliott Hughes69a017b2011-04-08 14:10:28 -0700606 jniThrowRuntimeException(env, "takePicture failed");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800607 return;
608 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800609}
610
611static void android_hardware_Camera_setParameters(JNIEnv *env, jobject thiz, jstring params)
612{
613 LOGV("setParameters");
614 sp<Camera> camera = get_native_camera(env, thiz, NULL);
615 if (camera == 0) return;
616
617 const jchar* str = env->GetStringCritical(params, 0);
618 String8 params8;
619 if (params) {
620 params8 = String8(str, env->GetStringLength(params));
621 env->ReleaseStringCritical(params, str);
622 }
623 if (camera->setParameters(params8) != NO_ERROR) {
Elliott Hughes69a017b2011-04-08 14:10:28 -0700624 jniThrowRuntimeException(env, "setParameters failed");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800625 return;
626 }
627}
628
629static jstring android_hardware_Camera_getParameters(JNIEnv *env, jobject thiz)
630{
631 LOGV("getParameters");
632 sp<Camera> camera = get_native_camera(env, thiz, NULL);
633 if (camera == 0) return 0;
634
635 return env->NewStringUTF(camera->getParameters().string());
636}
637
638static void android_hardware_Camera_reconnect(JNIEnv *env, jobject thiz)
639{
640 LOGV("reconnect");
641 sp<Camera> camera = get_native_camera(env, thiz, NULL);
642 if (camera == 0) return;
643
644 if (camera->reconnect() != NO_ERROR) {
645 jniThrowException(env, "java/io/IOException", "reconnect failed");
646 return;
647 }
648}
649
Wu-cheng Liffe1cf22009-09-10 16:49:17 +0800650static void android_hardware_Camera_lock(JNIEnv *env, jobject thiz)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800651{
652 LOGV("lock");
653 sp<Camera> camera = get_native_camera(env, thiz, NULL);
Wu-cheng Liffe1cf22009-09-10 16:49:17 +0800654 if (camera == 0) return;
655
656 if (camera->lock() != NO_ERROR) {
Elliott Hughes69a017b2011-04-08 14:10:28 -0700657 jniThrowRuntimeException(env, "lock failed");
Wu-cheng Liffe1cf22009-09-10 16:49:17 +0800658 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800659}
660
Wu-cheng Liffe1cf22009-09-10 16:49:17 +0800661static void android_hardware_Camera_unlock(JNIEnv *env, jobject thiz)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800662{
663 LOGV("unlock");
664 sp<Camera> camera = get_native_camera(env, thiz, NULL);
Wu-cheng Liffe1cf22009-09-10 16:49:17 +0800665 if (camera == 0) return;
666
667 if (camera->unlock() != NO_ERROR) {
Elliott Hughes69a017b2011-04-08 14:10:28 -0700668 jniThrowRuntimeException(env, "unlock failed");
Wu-cheng Liffe1cf22009-09-10 16:49:17 +0800669 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800670}
671
Wu-cheng Li36f68b82009-09-28 16:14:58 -0700672static void android_hardware_Camera_startSmoothZoom(JNIEnv *env, jobject thiz, jint value)
673{
Joe Onoratof5d95cb2010-01-07 21:48:32 -0500674 LOGV("startSmoothZoom");
Wu-cheng Li36f68b82009-09-28 16:14:58 -0700675 sp<Camera> camera = get_native_camera(env, thiz, NULL);
676 if (camera == 0) return;
677
Wu-cheng Li0ca25192010-03-29 16:21:12 +0800678 status_t rc = camera->sendCommand(CAMERA_CMD_START_SMOOTH_ZOOM, value, 0);
679 if (rc == BAD_VALUE) {
680 char msg[64];
681 sprintf(msg, "invalid zoom value=%d", value);
682 jniThrowException(env, "java/lang/IllegalArgumentException", msg);
683 } else if (rc != NO_ERROR) {
Elliott Hughes69a017b2011-04-08 14:10:28 -0700684 jniThrowRuntimeException(env, "start smooth zoom failed");
Wu-cheng Li36f68b82009-09-28 16:14:58 -0700685 }
686}
687
688static void android_hardware_Camera_stopSmoothZoom(JNIEnv *env, jobject thiz)
689{
Joe Onoratof5d95cb2010-01-07 21:48:32 -0500690 LOGV("stopSmoothZoom");
Wu-cheng Li36f68b82009-09-28 16:14:58 -0700691 sp<Camera> camera = get_native_camera(env, thiz, NULL);
692 if (camera == 0) return;
693
694 if (camera->sendCommand(CAMERA_CMD_STOP_SMOOTH_ZOOM, 0, 0) != NO_ERROR) {
Elliott Hughes69a017b2011-04-08 14:10:28 -0700695 jniThrowRuntimeException(env, "stop smooth zoom failed");
Wu-cheng Li36f68b82009-09-28 16:14:58 -0700696 }
697}
698
Chih-Chung Changd1d77062010-01-22 17:49:48 -0800699static void android_hardware_Camera_setDisplayOrientation(JNIEnv *env, jobject thiz,
700 jint value)
701{
702 LOGV("setDisplayOrientation");
703 sp<Camera> camera = get_native_camera(env, thiz, NULL);
704 if (camera == 0) return;
705
706 if (camera->sendCommand(CAMERA_CMD_SET_DISPLAY_ORIENTATION, value, 0) != NO_ERROR) {
Elliott Hughes69a017b2011-04-08 14:10:28 -0700707 jniThrowRuntimeException(env, "set display orientation failed");
Chih-Chung Changd1d77062010-01-22 17:49:48 -0800708 }
709}
710
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800711//-------------------------------------------------
712
713static JNINativeMethod camMethods[] = {
Chih-Chung Change25cc652010-05-06 16:36:58 +0800714 { "getNumberOfCameras",
715 "()I",
716 (void *)android_hardware_Camera_getNumberOfCameras },
Chih-Chung Changb8bb78f2010-06-10 13:32:16 +0800717 { "getCameraInfo",
718 "(ILandroid/hardware/Camera$CameraInfo;)V",
719 (void*)android_hardware_Camera_getCameraInfo },
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800720 { "native_setup",
Chih-Chung Change25cc652010-05-06 16:36:58 +0800721 "(Ljava/lang/Object;I)V",
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800722 (void*)android_hardware_Camera_native_setup },
723 { "native_release",
724 "()V",
725 (void*)android_hardware_Camera_release },
726 { "setPreviewDisplay",
727 "(Landroid/view/Surface;)V",
728 (void *)android_hardware_Camera_setPreviewDisplay },
Jamie Gennisfd6f39e2010-12-20 12:15:00 -0800729 { "setPreviewTexture",
730 "(Landroid/graphics/SurfaceTexture;)V",
731 (void *)android_hardware_Camera_setPreviewTexture },
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800732 { "startPreview",
733 "()V",
734 (void *)android_hardware_Camera_startPreview },
735 { "stopPreview",
736 "()V",
737 (void *)android_hardware_Camera_stopPreview },
738 { "previewEnabled",
739 "()Z",
740 (void *)android_hardware_Camera_previewEnabled },
741 { "setHasPreviewCallback",
742 "(ZZ)V",
743 (void *)android_hardware_Camera_setHasPreviewCallback },
James Donge00cab72011-02-17 16:38:06 -0800744 { "_addCallbackBuffer",
745 "([BI)V",
Andrew Harp94927df2009-10-20 01:47:05 -0400746 (void *)android_hardware_Camera_addCallbackBuffer },
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800747 { "native_autoFocus",
748 "()V",
749 (void *)android_hardware_Camera_autoFocus },
Chih-Chung Chang244f8c22009-09-15 14:51:56 +0800750 { "native_cancelAutoFocus",
751 "()V",
752 (void *)android_hardware_Camera_cancelAutoFocus },
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800753 { "native_takePicture",
James Donge00cab72011-02-17 16:38:06 -0800754 "(I)V",
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800755 (void *)android_hardware_Camera_takePicture },
756 { "native_setParameters",
757 "(Ljava/lang/String;)V",
758 (void *)android_hardware_Camera_setParameters },
759 { "native_getParameters",
760 "()Ljava/lang/String;",
761 (void *)android_hardware_Camera_getParameters },
762 { "reconnect",
763 "()V",
764 (void*)android_hardware_Camera_reconnect },
765 { "lock",
Wu-cheng Liffe1cf22009-09-10 16:49:17 +0800766 "()V",
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800767 (void*)android_hardware_Camera_lock },
768 { "unlock",
Wu-cheng Liffe1cf22009-09-10 16:49:17 +0800769 "()V",
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800770 (void*)android_hardware_Camera_unlock },
Wu-cheng Li36f68b82009-09-28 16:14:58 -0700771 { "startSmoothZoom",
772 "(I)V",
773 (void *)android_hardware_Camera_startSmoothZoom },
774 { "stopSmoothZoom",
775 "()V",
776 (void *)android_hardware_Camera_stopSmoothZoom },
Chih-Chung Changd1d77062010-01-22 17:49:48 -0800777 { "setDisplayOrientation",
778 "(I)V",
779 (void *)android_hardware_Camera_setDisplayOrientation },
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800780};
781
782struct field {
783 const char *class_name;
784 const char *field_name;
785 const char *field_type;
786 jfieldID *jfield;
787};
788
789static int find_fields(JNIEnv *env, field *fields, int count)
790{
791 for (int i = 0; i < count; i++) {
792 field *f = &fields[i];
793 jclass clazz = env->FindClass(f->class_name);
794 if (clazz == NULL) {
795 LOGE("Can't find %s", f->class_name);
796 return -1;
797 }
798
799 jfieldID field = env->GetFieldID(clazz, f->field_name, f->field_type);
800 if (field == NULL) {
801 LOGE("Can't find %s.%s", f->class_name, f->field_name);
802 return -1;
803 }
804
805 *(f->jfield) = field;
806 }
807
808 return 0;
809}
810
811// Get all the required offsets in java class and register native functions
812int register_android_hardware_Camera(JNIEnv *env)
813{
814 field fields_to_find[] = {
815 { "android/hardware/Camera", "mNativeContext", "I", &fields.context },
Chih-Chung Changb8bb78f2010-06-10 13:32:16 +0800816 { "android/view/Surface", ANDROID_VIEW_SURFACE_JNI_ID, "I", &fields.surface },
Jamie Gennisfd6f39e2010-12-20 12:15:00 -0800817 { "android/graphics/SurfaceTexture",
818 ANDROID_GRAPHICS_SURFACETEXTURE_JNI_ID, "I", &fields.surfaceTexture },
Wu-cheng Li3fd5fa42010-09-15 16:06:20 -0700819 { "android/hardware/Camera$CameraInfo", "facing", "I", &fields.facing },
820 { "android/hardware/Camera$CameraInfo", "orientation", "I", &fields.orientation },
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800821 };
822
823 if (find_fields(env, fields_to_find, NELEM(fields_to_find)) < 0)
824 return -1;
825
826 jclass clazz = env->FindClass("android/hardware/Camera");
827 fields.post_event = env->GetStaticMethodID(clazz, "postEventFromNative",
828 "(Ljava/lang/Object;IIILjava/lang/Object;)V");
829 if (fields.post_event == NULL) {
830 LOGE("Can't find android/hardware/Camera.postEventFromNative");
831 return -1;
832 }
833
834
835 // Register native functions
836 return AndroidRuntime::registerNativeMethods(env, "android/hardware/Camera",
837 camMethods, NELEM(camMethods));
838}