blob: 59057e6c99ad9526dfc845de75f334455003ef03 [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"
Igor Murashkinc99db2b2012-10-29 13:38:10 -070025#include <android_runtime/android_graphics_SurfaceTexture.h>
Mathias Agopian3866f0d2013-02-11 22:08:48 -080026#include <android_runtime/android_view_Surface.h>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080027
Eino-Ville Talvalaf7c6c5a2012-09-19 11:46:11 -070028#include <cutils/properties.h>
Andrew Harp94927df2009-10-20 01:47:05 -040029#include <utils/Vector.h>
Ruben Brunkfeb50af2014-05-09 19:58:49 -070030#include <utils/Errors.h>
Andrew Harp94927df2009-10-20 01:47:05 -040031
Andy McFaddend47f7d82012-12-18 09:48:38 -080032#include <gui/GLConsumer.h>
Mathias Agopian8335f1c2012-02-25 18:48:35 -080033#include <gui/Surface.h>
Mathias Agopian000479f2010-02-09 17:46:37 -080034#include <camera/Camera.h>
Mathias Agopian07952722009-05-19 19:08:10 -070035#include <binder/IMemory.h>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080036
37using namespace android;
38
Igor Murashkina1d66272014-06-20 11:22:11 -070039enum {
40 // Keep up to date with Camera.java
41 CAMERA_HAL_API_VERSION_NORMAL_CONNECT = -2,
42};
43
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080044struct fields_t {
45 jfieldID context;
Chih-Chung Changb8bb78f2010-06-10 13:32:16 +080046 jfieldID facing;
47 jfieldID orientation;
Eino-Ville Talvalaf7c6c5a2012-09-19 11:46:11 -070048 jfieldID canDisableShutterSound;
Wu-cheng Lif0d6a482011-07-28 05:30:59 +080049 jfieldID face_rect;
Wu-cheng Li4c2292e2011-07-22 02:37:11 +080050 jfieldID face_score;
51 jfieldID rect_left;
52 jfieldID rect_top;
53 jfieldID rect_right;
54 jfieldID rect_bottom;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080055 jmethodID post_event;
Wu-cheng Libb1e2752011-07-30 05:00:37 +080056 jmethodID rect_constructor;
57 jmethodID face_constructor;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080058};
59
60static fields_t fields;
61static Mutex sLock;
62
Dave Sparks5e271152009-06-23 17:30:11 -070063// provides persistent context for calls from native code to Java
64class JNICameraContext: public CameraListener
65{
66public:
67 JNICameraContext(JNIEnv* env, jobject weak_this, jclass clazz, const sp<Camera>& camera);
68 ~JNICameraContext() { release(); }
69 virtual void notify(int32_t msgType, int32_t ext1, int32_t ext2);
Wu-cheng Libb1e2752011-07-30 05:00:37 +080070 virtual void postData(int32_t msgType, const sp<IMemory>& dataPtr,
71 camera_frame_metadata_t *metadata);
Dave Sparks59c1a932009-07-08 15:56:53 -070072 virtual void postDataTimestamp(nsecs_t timestamp, int32_t msgType, const sp<IMemory>& dataPtr);
Wu-cheng Libb1e2752011-07-30 05:00:37 +080073 void postMetadata(JNIEnv *env, int32_t msgType, camera_frame_metadata_t *metadata);
James Donge00cab72011-02-17 16:38:06 -080074 void addCallbackBuffer(JNIEnv *env, jbyteArray cbb, int msgType);
Andrew Harp94927df2009-10-20 01:47:05 -040075 void setCallbackMode(JNIEnv *env, bool installed, bool manualMode);
Dave Sparks5e271152009-06-23 17:30:11 -070076 sp<Camera> getCamera() { Mutex::Autolock _l(mLock); return mCamera; }
James Donge00cab72011-02-17 16:38:06 -080077 bool isRawImageCallbackBufferAvailable() const;
Dave Sparks5e271152009-06-23 17:30:11 -070078 void release();
79
80private:
81 void copyAndPost(JNIEnv* env, const sp<IMemory>& dataPtr, int msgType);
James Donge00cab72011-02-17 16:38:06 -080082 void clearCallbackBuffers_l(JNIEnv *env, Vector<jbyteArray> *buffers);
Andrew Harp94927df2009-10-20 01:47:05 -040083 void clearCallbackBuffers_l(JNIEnv *env);
James Donge00cab72011-02-17 16:38:06 -080084 jbyteArray getCallbackBuffer(JNIEnv *env, Vector<jbyteArray> *buffers, size_t bufferSize);
Dave Sparks5e271152009-06-23 17:30:11 -070085
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080086 jobject mCameraJObjectWeak; // weak reference to java object
87 jclass mCameraJClass; // strong reference to java class
Wu-cheng Liffe1cf22009-09-10 16:49:17 +080088 sp<Camera> mCamera; // strong reference to native object
Wu-cheng Libb1e2752011-07-30 05:00:37 +080089 jclass mFaceClass; // strong reference to Face class
90 jclass mRectClass; // strong reference to Rect class
Dave Sparks5e271152009-06-23 17:30:11 -070091 Mutex mLock;
Andrew Harp94927df2009-10-20 01:47:05 -040092
James Donge00cab72011-02-17 16:38:06 -080093 /*
94 * Global reference application-managed raw image buffer queue.
95 *
96 * Manual-only mode is supported for raw image callbacks, which is
97 * set whenever method addCallbackBuffer() with msgType =
98 * CAMERA_MSG_RAW_IMAGE is called; otherwise, null is returned
99 * with raw image callbacks.
100 */
101 Vector<jbyteArray> mRawImageCallbackBuffers;
102
103 /*
104 * Application-managed preview buffer queue and the flags
105 * associated with the usage of the preview buffer callback.
106 */
Andrew Harp94927df2009-10-20 01:47:05 -0400107 Vector<jbyteArray> mCallbackBuffers; // Global reference application managed byte[]
108 bool mManualBufferMode; // Whether to use application managed buffers.
James Donge00cab72011-02-17 16:38:06 -0800109 bool mManualCameraCallbackSet; // Whether the callback has been set, used to
110 // reduce unnecessary calls to set the callback.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800111};
112
James Donge00cab72011-02-17 16:38:06 -0800113bool JNICameraContext::isRawImageCallbackBufferAvailable() const
114{
115 return !mRawImageCallbackBuffers.isEmpty();
116}
117
Dave Sparks5e271152009-06-23 17:30:11 -0700118sp<Camera> get_native_camera(JNIEnv *env, jobject thiz, JNICameraContext** pContext)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800119{
120 sp<Camera> camera;
121 Mutex::Autolock _l(sLock);
Ashok Bhat4838e332014-01-03 14:37:19 +0000122 JNICameraContext* context = reinterpret_cast<JNICameraContext*>(env->GetLongField(thiz, fields.context));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800123 if (context != NULL) {
Dave Sparks5e271152009-06-23 17:30:11 -0700124 camera = context->getCamera();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800125 }
Steve Block71f2cf12011-10-20 11:56:00 +0100126 ALOGV("get_native_camera: context=%p, camera=%p", context, camera.get());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800127 if (camera == 0) {
Elliott Hughes69a017b2011-04-08 14:10:28 -0700128 jniThrowRuntimeException(env, "Method called after release()");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800129 }
130
131 if (pContext != NULL) *pContext = context;
132 return camera;
133}
134
Dave Sparks5e271152009-06-23 17:30:11 -0700135JNICameraContext::JNICameraContext(JNIEnv* env, jobject weak_this, jclass clazz, const sp<Camera>& camera)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800136{
Dave Sparks5e271152009-06-23 17:30:11 -0700137 mCameraJObjectWeak = env->NewGlobalRef(weak_this);
138 mCameraJClass = (jclass)env->NewGlobalRef(clazz);
139 mCamera = camera;
Andrew Harp94927df2009-10-20 01:47:05 -0400140
Wu-cheng Libb1e2752011-07-30 05:00:37 +0800141 jclass faceClazz = env->FindClass("android/hardware/Camera$Face");
142 mFaceClass = (jclass) env->NewGlobalRef(faceClazz);
143
144 jclass rectClazz = env->FindClass("android/graphics/Rect");
145 mRectClass = (jclass) env->NewGlobalRef(rectClazz);
146
Andrew Harp94927df2009-10-20 01:47:05 -0400147 mManualBufferMode = false;
148 mManualCameraCallbackSet = false;
Dave Sparks5e271152009-06-23 17:30:11 -0700149}
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800150
Dave Sparks5e271152009-06-23 17:30:11 -0700151void JNICameraContext::release()
152{
Steve Block71f2cf12011-10-20 11:56:00 +0100153 ALOGV("release");
Dave Sparks5e271152009-06-23 17:30:11 -0700154 Mutex::Autolock _l(mLock);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800155 JNIEnv *env = AndroidRuntime::getJNIEnv();
Dave Sparks5e271152009-06-23 17:30:11 -0700156
157 if (mCameraJObjectWeak != NULL) {
158 env->DeleteGlobalRef(mCameraJObjectWeak);
159 mCameraJObjectWeak = NULL;
160 }
161 if (mCameraJClass != NULL) {
162 env->DeleteGlobalRef(mCameraJClass);
163 mCameraJClass = NULL;
164 }
Wu-cheng Libb1e2752011-07-30 05:00:37 +0800165 if (mFaceClass != NULL) {
166 env->DeleteGlobalRef(mFaceClass);
167 mFaceClass = NULL;
168 }
169 if (mRectClass != NULL) {
170 env->DeleteGlobalRef(mRectClass);
171 mRectClass = NULL;
172 }
Andrew Harp94927df2009-10-20 01:47:05 -0400173 clearCallbackBuffers_l(env);
Dave Sparks5e271152009-06-23 17:30:11 -0700174 mCamera.clear();
175}
176
177void JNICameraContext::notify(int32_t msgType, int32_t ext1, int32_t ext2)
178{
Steve Block71f2cf12011-10-20 11:56:00 +0100179 ALOGV("notify");
Dave Sparks5e271152009-06-23 17:30:11 -0700180
181 // VM pointer will be NULL if object is released
182 Mutex::Autolock _l(mLock);
183 if (mCameraJObjectWeak == NULL) {
Steve Block8564c8d2012-01-05 23:22:43 +0000184 ALOGW("callback on dead camera object");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800185 return;
186 }
Dave Sparks5e271152009-06-23 17:30:11 -0700187 JNIEnv *env = AndroidRuntime::getJNIEnv();
James Donge00cab72011-02-17 16:38:06 -0800188
189 /*
190 * If the notification or msgType is CAMERA_MSG_RAW_IMAGE_NOTIFY, change it
191 * to CAMERA_MSG_RAW_IMAGE since CAMERA_MSG_RAW_IMAGE_NOTIFY is not exposed
192 * to the Java app.
193 */
194 if (msgType == CAMERA_MSG_RAW_IMAGE_NOTIFY) {
195 msgType = CAMERA_MSG_RAW_IMAGE;
196 }
197
Dave Sparksc62f9bd2009-06-26 13:33:32 -0700198 env->CallStaticVoidMethod(mCameraJClass, fields.post_event,
Chih-Chung Chang6157de02009-09-24 15:29:35 -0700199 mCameraJObjectWeak, msgType, ext1, ext2, NULL);
Dave Sparks5e271152009-06-23 17:30:11 -0700200}
201
James Donge00cab72011-02-17 16:38:06 -0800202jbyteArray JNICameraContext::getCallbackBuffer(
203 JNIEnv* env, Vector<jbyteArray>* buffers, size_t bufferSize)
204{
205 jbyteArray obj = NULL;
206
207 // Vector access should be protected by lock in postData()
208 if (!buffers->isEmpty()) {
Dan Albert46d84442014-11-18 16:07:51 -0800209 ALOGV("Using callback buffer from queue of length %zu", buffers->size());
James Donge00cab72011-02-17 16:38:06 -0800210 jbyteArray globalBuffer = buffers->itemAt(0);
211 buffers->removeAt(0);
212
213 obj = (jbyteArray)env->NewLocalRef(globalBuffer);
214 env->DeleteGlobalRef(globalBuffer);
215
216 if (obj != NULL) {
217 jsize bufferLength = env->GetArrayLength(obj);
218 if ((int)bufferLength < (int)bufferSize) {
Dan Albert46d84442014-11-18 16:07:51 -0800219 ALOGE("Callback buffer was too small! Expected %zu bytes, but got %d bytes!",
James Donge00cab72011-02-17 16:38:06 -0800220 bufferSize, bufferLength);
221 env->DeleteLocalRef(obj);
222 return NULL;
223 }
224 }
225 }
226
227 return obj;
228}
229
Dave Sparks5e271152009-06-23 17:30:11 -0700230void JNICameraContext::copyAndPost(JNIEnv* env, const sp<IMemory>& dataPtr, int msgType)
231{
232 jbyteArray obj = NULL;
233
234 // allocate Java byte array and copy data
235 if (dataPtr != NULL) {
236 ssize_t offset;
237 size_t size;
238 sp<IMemoryHeap> heap = dataPtr->getMemory(&offset, &size);
Glenn Kasten2fbf25b2014-03-28 15:41:58 -0700239 ALOGV("copyAndPost: off=%zd, size=%zu", offset, size);
Dave Sparks5e271152009-06-23 17:30:11 -0700240 uint8_t *heapBase = (uint8_t*)heap->base();
241
242 if (heapBase != NULL) {
Dave Sparksc4ca4202009-07-13 09:38:20 -0700243 const jbyte* data = reinterpret_cast<const jbyte*>(heapBase + offset);
Andrew Harp94927df2009-10-20 01:47:05 -0400244
Wu-cheng Li5f0ef5f2011-09-21 16:40:23 +0800245 if (msgType == CAMERA_MSG_RAW_IMAGE) {
246 obj = getCallbackBuffer(env, &mRawImageCallbackBuffers, size);
247 } else if (msgType == CAMERA_MSG_PREVIEW_FRAME && mManualBufferMode) {
248 obj = getCallbackBuffer(env, &mCallbackBuffers, size);
Andrew Harp94927df2009-10-20 01:47:05 -0400249
Wu-cheng Li5f0ef5f2011-09-21 16:40:23 +0800250 if (mCallbackBuffers.isEmpty()) {
Steve Block71f2cf12011-10-20 11:56:00 +0100251 ALOGV("Out of buffers, clearing callback!");
Wu-cheng Li5f0ef5f2011-09-21 16:40:23 +0800252 mCamera->setPreviewCallbackFlags(CAMERA_FRAME_CALLBACK_FLAG_NOOP);
253 mManualCameraCallbackSet = false;
Andrew Harp94927df2009-10-20 01:47:05 -0400254
Wu-cheng Li5f0ef5f2011-09-21 16:40:23 +0800255 if (obj == NULL) {
Andrew Harp94927df2009-10-20 01:47:05 -0400256 return;
257 }
258 }
Wu-cheng Li5f0ef5f2011-09-21 16:40:23 +0800259 } else {
Steve Block71f2cf12011-10-20 11:56:00 +0100260 ALOGV("Allocating callback buffer");
Wu-cheng Li5f0ef5f2011-09-21 16:40:23 +0800261 obj = env->NewByteArray(size);
Andrew Harp94927df2009-10-20 01:47:05 -0400262 }
263
Dave Sparks5e271152009-06-23 17:30:11 -0700264 if (obj == NULL) {
Steve Block3762c312012-01-06 19:20:56 +0000265 ALOGE("Couldn't allocate byte array for JPEG data");
Dave Sparks5e271152009-06-23 17:30:11 -0700266 env->ExceptionClear();
267 } else {
Dave Sparksa95f4952009-07-10 18:13:36 -0700268 env->SetByteArrayRegion(obj, 0, size, data);
Dave Sparks5e271152009-06-23 17:30:11 -0700269 }
270 } else {
Steve Block3762c312012-01-06 19:20:56 +0000271 ALOGE("image heap is NULL");
Dave Sparks5e271152009-06-23 17:30:11 -0700272 }
273 }
274
275 // post image data to Java
276 env->CallStaticVoidMethod(mCameraJClass, fields.post_event,
277 mCameraJObjectWeak, msgType, 0, 0, obj);
278 if (obj) {
279 env->DeleteLocalRef(obj);
280 }
281}
282
Wu-cheng Libb1e2752011-07-30 05:00:37 +0800283void JNICameraContext::postData(int32_t msgType, const sp<IMemory>& dataPtr,
284 camera_frame_metadata_t *metadata)
Dave Sparks5e271152009-06-23 17:30:11 -0700285{
286 // VM pointer will be NULL if object is released
287 Mutex::Autolock _l(mLock);
288 JNIEnv *env = AndroidRuntime::getJNIEnv();
Dave Sparksd0cbb1a2009-06-29 19:03:33 -0700289 if (mCameraJObjectWeak == NULL) {
Steve Block8564c8d2012-01-05 23:22:43 +0000290 ALOGW("callback on dead camera object");
Dave Sparksd0cbb1a2009-06-29 19:03:33 -0700291 return;
292 }
Dave Sparks5e271152009-06-23 17:30:11 -0700293
Wu-cheng Libb1e2752011-07-30 05:00:37 +0800294 int32_t dataMsgType = msgType & ~CAMERA_MSG_PREVIEW_METADATA;
295
Dave Sparks5e271152009-06-23 17:30:11 -0700296 // return data based on callback type
Wu-cheng Libb1e2752011-07-30 05:00:37 +0800297 switch (dataMsgType) {
James Donge00cab72011-02-17 16:38:06 -0800298 case CAMERA_MSG_VIDEO_FRAME:
299 // should never happen
300 break;
301
302 // For backward-compatibility purpose, if there is no callback
303 // buffer for raw image, the callback returns null.
304 case CAMERA_MSG_RAW_IMAGE:
Steve Block71f2cf12011-10-20 11:56:00 +0100305 ALOGV("rawCallback");
James Donge00cab72011-02-17 16:38:06 -0800306 if (mRawImageCallbackBuffers.isEmpty()) {
307 env->CallStaticVoidMethod(mCameraJClass, fields.post_event,
Wu-cheng Libb1e2752011-07-30 05:00:37 +0800308 mCameraJObjectWeak, dataMsgType, 0, 0, NULL);
James Donge00cab72011-02-17 16:38:06 -0800309 } else {
Wu-cheng Libb1e2752011-07-30 05:00:37 +0800310 copyAndPost(env, dataPtr, dataMsgType);
James Donge00cab72011-02-17 16:38:06 -0800311 }
312 break;
313
Wu-cheng Libb1e2752011-07-30 05:00:37 +0800314 // There is no data.
315 case 0:
James Donge00cab72011-02-17 16:38:06 -0800316 break;
Wu-cheng Libb1e2752011-07-30 05:00:37 +0800317
318 default:
Steve Block71f2cf12011-10-20 11:56:00 +0100319 ALOGV("dataCallback(%d, %p)", dataMsgType, dataPtr.get());
Wu-cheng Libb1e2752011-07-30 05:00:37 +0800320 copyAndPost(env, dataPtr, dataMsgType);
321 break;
322 }
323
324 // post frame metadata to Java
325 if (metadata && (msgType & CAMERA_MSG_PREVIEW_METADATA)) {
326 postMetadata(env, CAMERA_MSG_PREVIEW_METADATA, metadata);
Dave Sparks5e271152009-06-23 17:30:11 -0700327 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800328}
329
Dave Sparks59c1a932009-07-08 15:56:53 -0700330void JNICameraContext::postDataTimestamp(nsecs_t timestamp, int32_t msgType, const sp<IMemory>& dataPtr)
331{
332 // TODO: plumb up to Java. For now, just drop the timestamp
Wu-cheng Libb1e2752011-07-30 05:00:37 +0800333 postData(msgType, dataPtr, NULL);
334}
335
336void JNICameraContext::postMetadata(JNIEnv *env, int32_t msgType, camera_frame_metadata_t *metadata)
337{
338 jobjectArray obj = NULL;
339 obj = (jobjectArray) env->NewObjectArray(metadata->number_of_faces,
340 mFaceClass, NULL);
341 if (obj == NULL) {
Steve Block3762c312012-01-06 19:20:56 +0000342 ALOGE("Couldn't allocate face metadata array");
Wu-cheng Libb1e2752011-07-30 05:00:37 +0800343 return;
344 }
345
346 for (int i = 0; i < metadata->number_of_faces; i++) {
347 jobject face = env->NewObject(mFaceClass, fields.face_constructor);
348 env->SetObjectArrayElement(obj, i, face);
349
350 jobject rect = env->NewObject(mRectClass, fields.rect_constructor);
351 env->SetIntField(rect, fields.rect_left, metadata->faces[i].rect[0]);
352 env->SetIntField(rect, fields.rect_top, metadata->faces[i].rect[1]);
353 env->SetIntField(rect, fields.rect_right, metadata->faces[i].rect[2]);
354 env->SetIntField(rect, fields.rect_bottom, metadata->faces[i].rect[3]);
355
356 env->SetObjectField(face, fields.face_rect, rect);
357 env->SetIntField(face, fields.face_score, metadata->faces[i].score);
358
359 env->DeleteLocalRef(face);
360 env->DeleteLocalRef(rect);
361 }
362 env->CallStaticVoidMethod(mCameraJClass, fields.post_event,
363 mCameraJObjectWeak, msgType, 0, 0, obj);
364 env->DeleteLocalRef(obj);
Dave Sparks59c1a932009-07-08 15:56:53 -0700365}
366
Andrew Harp94927df2009-10-20 01:47:05 -0400367void JNICameraContext::setCallbackMode(JNIEnv *env, bool installed, bool manualMode)
368{
369 Mutex::Autolock _l(mLock);
370 mManualBufferMode = manualMode;
371 mManualCameraCallbackSet = false;
372
373 // In order to limit the over usage of binder threads, all non-manual buffer
Iliyan Malchev9c7ac0d2011-04-14 16:51:21 -0700374 // callbacks use CAMERA_FRAME_CALLBACK_FLAG_BARCODE_SCANNER mode now.
Andrew Harp94927df2009-10-20 01:47:05 -0400375 //
376 // Continuous callbacks will have the callback re-registered from handleMessage.
377 // Manual buffer mode will operate as fast as possible, relying on the finite supply
378 // of buffers for throttling.
379
380 if (!installed) {
Iliyan Malchev9c7ac0d2011-04-14 16:51:21 -0700381 mCamera->setPreviewCallbackFlags(CAMERA_FRAME_CALLBACK_FLAG_NOOP);
James Donge00cab72011-02-17 16:38:06 -0800382 clearCallbackBuffers_l(env, &mCallbackBuffers);
Andrew Harp94927df2009-10-20 01:47:05 -0400383 } else if (mManualBufferMode) {
384 if (!mCallbackBuffers.isEmpty()) {
Iliyan Malchev9c7ac0d2011-04-14 16:51:21 -0700385 mCamera->setPreviewCallbackFlags(CAMERA_FRAME_CALLBACK_FLAG_CAMERA);
Andrew Harp94927df2009-10-20 01:47:05 -0400386 mManualCameraCallbackSet = true;
387 }
388 } else {
Iliyan Malchev9c7ac0d2011-04-14 16:51:21 -0700389 mCamera->setPreviewCallbackFlags(CAMERA_FRAME_CALLBACK_FLAG_BARCODE_SCANNER);
James Donge00cab72011-02-17 16:38:06 -0800390 clearCallbackBuffers_l(env, &mCallbackBuffers);
Andrew Harp94927df2009-10-20 01:47:05 -0400391 }
392}
393
James Donge00cab72011-02-17 16:38:06 -0800394void JNICameraContext::addCallbackBuffer(
395 JNIEnv *env, jbyteArray cbb, int msgType)
Andrew Harp94927df2009-10-20 01:47:05 -0400396{
Steve Block71f2cf12011-10-20 11:56:00 +0100397 ALOGV("addCallbackBuffer: 0x%x", msgType);
Andrew Harp94927df2009-10-20 01:47:05 -0400398 if (cbb != NULL) {
399 Mutex::Autolock _l(mLock);
James Donge00cab72011-02-17 16:38:06 -0800400 switch (msgType) {
401 case CAMERA_MSG_PREVIEW_FRAME: {
402 jbyteArray callbackBuffer = (jbyteArray)env->NewGlobalRef(cbb);
403 mCallbackBuffers.push(callbackBuffer);
Andrew Harp94927df2009-10-20 01:47:05 -0400404
Dan Albert46d84442014-11-18 16:07:51 -0800405 ALOGV("Adding callback buffer to queue, %zu total",
James Donge00cab72011-02-17 16:38:06 -0800406 mCallbackBuffers.size());
Andrew Harp94927df2009-10-20 01:47:05 -0400407
James Donge00cab72011-02-17 16:38:06 -0800408 // We want to make sure the camera knows we're ready for the
409 // next frame. This may have come unset had we not had a
410 // callbackbuffer ready for it last time.
411 if (mManualBufferMode && !mManualCameraCallbackSet) {
Iliyan Malchev9c7ac0d2011-04-14 16:51:21 -0700412 mCamera->setPreviewCallbackFlags(CAMERA_FRAME_CALLBACK_FLAG_CAMERA);
James Donge00cab72011-02-17 16:38:06 -0800413 mManualCameraCallbackSet = true;
414 }
415 break;
416 }
417 case CAMERA_MSG_RAW_IMAGE: {
418 jbyteArray callbackBuffer = (jbyteArray)env->NewGlobalRef(cbb);
419 mRawImageCallbackBuffers.push(callbackBuffer);
420 break;
421 }
422 default: {
423 jniThrowException(env,
424 "java/lang/IllegalArgumentException",
425 "Unsupported message type");
426 return;
427 }
Andrew Harp94927df2009-10-20 01:47:05 -0400428 }
429 } else {
Steve Block3762c312012-01-06 19:20:56 +0000430 ALOGE("Null byte array!");
Andrew Harp94927df2009-10-20 01:47:05 -0400431 }
432}
433
434void JNICameraContext::clearCallbackBuffers_l(JNIEnv *env)
435{
James Donge00cab72011-02-17 16:38:06 -0800436 clearCallbackBuffers_l(env, &mCallbackBuffers);
437 clearCallbackBuffers_l(env, &mRawImageCallbackBuffers);
438}
439
440void JNICameraContext::clearCallbackBuffers_l(JNIEnv *env, Vector<jbyteArray> *buffers) {
Dan Albert46d84442014-11-18 16:07:51 -0800441 ALOGV("Clearing callback buffers, %zu remained", buffers->size());
James Donge00cab72011-02-17 16:38:06 -0800442 while (!buffers->isEmpty()) {
443 env->DeleteGlobalRef(buffers->top());
444 buffers->pop();
Andrew Harp94927df2009-10-20 01:47:05 -0400445 }
446}
447
Chih-Chung Change25cc652010-05-06 16:36:58 +0800448static jint android_hardware_Camera_getNumberOfCameras(JNIEnv *env, jobject thiz)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800449{
Chih-Chung Change25cc652010-05-06 16:36:58 +0800450 return Camera::getNumberOfCameras();
451}
452
Chih-Chung Changb8bb78f2010-06-10 13:32:16 +0800453static void android_hardware_Camera_getCameraInfo(JNIEnv *env, jobject thiz,
454 jint cameraId, jobject info_obj)
455{
456 CameraInfo cameraInfo;
457 status_t rc = Camera::getCameraInfo(cameraId, &cameraInfo);
458 if (rc != NO_ERROR) {
Elliott Hughes69a017b2011-04-08 14:10:28 -0700459 jniThrowRuntimeException(env, "Fail to get camera info");
Chih-Chung Changb8bb78f2010-06-10 13:32:16 +0800460 return;
461 }
462 env->SetIntField(info_obj, fields.facing, cameraInfo.facing);
463 env->SetIntField(info_obj, fields.orientation, cameraInfo.orientation);
Eino-Ville Talvalaf7c6c5a2012-09-19 11:46:11 -0700464
465 char value[PROPERTY_VALUE_MAX];
466 property_get("ro.camera.sound.forced", value, "0");
467 jboolean canDisableShutterSound = (strncmp(value, "0", 2) == 0);
468 env->SetBooleanField(info_obj, fields.canDisableShutterSound,
469 canDisableShutterSound);
Chih-Chung Changb8bb78f2010-06-10 13:32:16 +0800470}
471
Chih-Chung Change25cc652010-05-06 16:36:58 +0800472// connect to camera service
Ruben Brunkfeb50af2014-05-09 19:58:49 -0700473static jint android_hardware_Camera_native_setup(JNIEnv *env, jobject thiz,
Zhijun He4c913802014-06-16 16:42:35 -0700474 jobject weak_this, jint cameraId, jint halVersion, jstring clientPackageName)
Chih-Chung Change25cc652010-05-06 16:36:58 +0800475{
Eino-Ville Talvala788717c2013-02-15 18:30:15 -0800476 // Convert jstring to String16
477 const char16_t *rawClientName = env->GetStringChars(clientPackageName, NULL);
478 jsize rawClientNameLen = env->GetStringLength(clientPackageName);
479 String16 clientName(rawClientName, rawClientNameLen);
480 env->ReleaseStringChars(clientPackageName, rawClientName);
481
Zhijun He4c913802014-06-16 16:42:35 -0700482 sp<Camera> camera;
Igor Murashkina1d66272014-06-20 11:22:11 -0700483 if (halVersion == CAMERA_HAL_API_VERSION_NORMAL_CONNECT) {
484 // Default path: hal version is don't care, do normal camera connect.
Zhijun He4c913802014-06-16 16:42:35 -0700485 camera = Camera::connect(cameraId, clientName,
486 Camera::USE_CALLING_UID);
487 } else {
488 jint status = Camera::connectLegacy(cameraId, halVersion, clientName,
489 Camera::USE_CALLING_UID, camera);
490 if (status != NO_ERROR) {
491 return status;
492 }
493 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800494
495 if (camera == NULL) {
Ruben Brunkfeb50af2014-05-09 19:58:49 -0700496 return -EACCES;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800497 }
498
499 // make sure camera hardware is alive
500 if (camera->getStatus() != NO_ERROR) {
Ruben Brunkfeb50af2014-05-09 19:58:49 -0700501 return NO_INIT;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800502 }
503
504 jclass clazz = env->GetObjectClass(thiz);
505 if (clazz == NULL) {
Ruben Brunkfeb50af2014-05-09 19:58:49 -0700506 // This should never happen
Elliott Hughes69a017b2011-04-08 14:10:28 -0700507 jniThrowRuntimeException(env, "Can't find android/hardware/Camera");
Ruben Brunkfeb50af2014-05-09 19:58:49 -0700508 return INVALID_OPERATION;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800509 }
510
511 // We use a weak reference so the Camera object can be garbage collected.
512 // The reference is only used as a proxy for callbacks.
Dave Sparks5e271152009-06-23 17:30:11 -0700513 sp<JNICameraContext> context = new JNICameraContext(env, weak_this, clazz, camera);
Mathias Agopianb1d90c82013-03-06 17:45:42 -0800514 context->incStrong((void*)android_hardware_Camera_native_setup);
Dave Sparks5e271152009-06-23 17:30:11 -0700515 camera->setListener(context);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800516
517 // save context in opaque field
Ashok Bhat4838e332014-01-03 14:37:19 +0000518 env->SetLongField(thiz, fields.context, (jlong)context.get());
Ruben Brunkfeb50af2014-05-09 19:58:49 -0700519 return NO_ERROR;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800520}
521
522// disconnect from camera service
523// It's okay to call this when the native camera context is already null.
524// This handles the case where the user has called release() and the
525// finalizer is invoked later.
526static void android_hardware_Camera_release(JNIEnv *env, jobject thiz)
527{
Steve Block71f2cf12011-10-20 11:56:00 +0100528 ALOGV("release camera");
Dave Sparks5e271152009-06-23 17:30:11 -0700529 JNICameraContext* context = NULL;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800530 sp<Camera> camera;
531 {
532 Mutex::Autolock _l(sLock);
Ashok Bhat4838e332014-01-03 14:37:19 +0000533 context = reinterpret_cast<JNICameraContext*>(env->GetLongField(thiz, fields.context));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800534
535 // Make sure we do not attempt to callback on a deleted Java object.
Ashok Bhat4838e332014-01-03 14:37:19 +0000536 env->SetLongField(thiz, fields.context, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800537 }
538
539 // clean up if release has not been called before
540 if (context != NULL) {
Dave Sparks5e271152009-06-23 17:30:11 -0700541 camera = context->getCamera();
542 context->release();
Steve Block71f2cf12011-10-20 11:56:00 +0100543 ALOGV("native_release: context=%p camera=%p", context, camera.get());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800544
545 // clear callbacks
546 if (camera != NULL) {
Iliyan Malchev9c7ac0d2011-04-14 16:51:21 -0700547 camera->setPreviewCallbackFlags(CAMERA_FRAME_CALLBACK_FLAG_NOOP);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800548 camera->disconnect();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800549 }
550
551 // remove context to prevent further Java access
Mathias Agopianb1d90c82013-03-06 17:45:42 -0800552 context->decStrong((void*)android_hardware_Camera_native_setup);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800553 }
554}
555
Ruben Brunkfeb50af2014-05-09 19:58:49 -0700556static void android_hardware_Camera_setPreviewSurface(JNIEnv *env, jobject thiz, jobject jSurface)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800557{
Ruben Brunkfeb50af2014-05-09 19:58:49 -0700558 ALOGV("setPreviewSurface");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800559 sp<Camera> camera = get_native_camera(env, thiz, NULL);
560 if (camera == 0) return;
561
Mathias Agopian4a05f432013-03-12 18:43:34 -0700562 sp<IGraphicBufferProducer> gbp;
Jesse Hallaa70f222013-02-21 15:06:27 -0800563 sp<Surface> surface;
564 if (jSurface) {
565 surface = android_view_Surface_getSurface(env, jSurface);
Mathias Agopian4a05f432013-03-12 18:43:34 -0700566 if (surface != NULL) {
567 gbp = surface->getIGraphicBufferProducer();
568 }
Jesse Hallaa70f222013-02-21 15:06:27 -0800569 }
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800570
Eino-Ville Talvala7b297792013-08-21 14:39:22 -0700571 if (camera->setPreviewTarget(gbp) != NO_ERROR) {
Mathias Agopian4a05f432013-03-12 18:43:34 -0700572 jniThrowException(env, "java/io/IOException", "setPreviewTexture failed");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800573 }
574}
575
Jamie Gennisfd6f39e2010-12-20 12:15:00 -0800576static void android_hardware_Camera_setPreviewTexture(JNIEnv *env,
577 jobject thiz, jobject jSurfaceTexture)
578{
Steve Block71f2cf12011-10-20 11:56:00 +0100579 ALOGV("setPreviewTexture");
Jamie Gennisfd6f39e2010-12-20 12:15:00 -0800580 sp<Camera> camera = get_native_camera(env, thiz, NULL);
581 if (camera == 0) return;
582
Mathias Agopian52a9a102013-08-02 01:38:38 -0700583 sp<IGraphicBufferProducer> producer = NULL;
Jamie Gennisfd6f39e2010-12-20 12:15:00 -0800584 if (jSurfaceTexture != NULL) {
Mathias Agopian52a9a102013-08-02 01:38:38 -0700585 producer = SurfaceTexture_getProducer(env, jSurfaceTexture);
586 if (producer == NULL) {
Daniel Lam2e76c992012-02-23 14:35:13 -0800587 jniThrowException(env, "java/lang/IllegalArgumentException",
588 "SurfaceTexture already released in setPreviewTexture");
589 return;
590 }
591
Jamie Gennisfd6f39e2010-12-20 12:15:00 -0800592 }
Daniel Lam2e76c992012-02-23 14:35:13 -0800593
Eino-Ville Talvala7b297792013-08-21 14:39:22 -0700594 if (camera->setPreviewTarget(producer) != NO_ERROR) {
Jamie Gennisfd6f39e2010-12-20 12:15:00 -0800595 jniThrowException(env, "java/io/IOException",
596 "setPreviewTexture failed");
597 }
598}
599
Eino-Ville Talvala7005b672013-04-02 15:46:38 -0700600static void android_hardware_Camera_setPreviewCallbackSurface(JNIEnv *env,
601 jobject thiz, jobject jSurface)
602{
603 ALOGV("setPreviewCallbackSurface");
604 JNICameraContext* context;
605 sp<Camera> camera = get_native_camera(env, thiz, &context);
606 if (camera == 0) return;
607
608 sp<IGraphicBufferProducer> gbp;
609 sp<Surface> surface;
610 if (jSurface) {
611 surface = android_view_Surface_getSurface(env, jSurface);
612 if (surface != NULL) {
613 gbp = surface->getIGraphicBufferProducer();
614 }
615 }
616 // Clear out normal preview callbacks
617 context->setCallbackMode(env, false, false);
618 // Then set up callback surface
619 if (camera->setPreviewCallbackTarget(gbp) != NO_ERROR) {
620 jniThrowException(env, "java/io/IOException", "setPreviewCallbackTarget failed");
621 }
622}
623
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800624static void android_hardware_Camera_startPreview(JNIEnv *env, jobject thiz)
625{
Steve Block71f2cf12011-10-20 11:56:00 +0100626 ALOGV("startPreview");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800627 sp<Camera> camera = get_native_camera(env, thiz, NULL);
628 if (camera == 0) return;
629
630 if (camera->startPreview() != NO_ERROR) {
Elliott Hughes69a017b2011-04-08 14:10:28 -0700631 jniThrowRuntimeException(env, "startPreview failed");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800632 return;
633 }
634}
635
636static void android_hardware_Camera_stopPreview(JNIEnv *env, jobject thiz)
637{
Steve Block71f2cf12011-10-20 11:56:00 +0100638 ALOGV("stopPreview");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800639 sp<Camera> c = get_native_camera(env, thiz, NULL);
640 if (c == 0) return;
641
642 c->stopPreview();
643}
644
Ashok Bhat4838e332014-01-03 14:37:19 +0000645static jboolean android_hardware_Camera_previewEnabled(JNIEnv *env, jobject thiz)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800646{
Steve Block71f2cf12011-10-20 11:56:00 +0100647 ALOGV("previewEnabled");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800648 sp<Camera> c = get_native_camera(env, thiz, NULL);
Ashok Bhat4838e332014-01-03 14:37:19 +0000649 if (c == 0) return JNI_FALSE;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800650
Ashok Bhat4838e332014-01-03 14:37:19 +0000651 return c->previewEnabled() ? JNI_TRUE : JNI_FALSE;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800652}
653
Andrew Harp94927df2009-10-20 01:47:05 -0400654static void android_hardware_Camera_setHasPreviewCallback(JNIEnv *env, jobject thiz, jboolean installed, jboolean manualBuffer)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800655{
Steve Block71f2cf12011-10-20 11:56:00 +0100656 ALOGV("setHasPreviewCallback: installed:%d, manualBuffer:%d", (int)installed, (int)manualBuffer);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800657 // Important: Only install preview_callback if the Java code has called
658 // setPreviewCallback() with a non-null value, otherwise we'd pay to memcpy
659 // each preview frame for nothing.
Dave Sparks5e271152009-06-23 17:30:11 -0700660 JNICameraContext* context;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800661 sp<Camera> camera = get_native_camera(env, thiz, &context);
662 if (camera == 0) return;
663
Andrew Harp94927df2009-10-20 01:47:05 -0400664 // setCallbackMode will take care of setting the context flags and calling
665 // camera->setPreviewCallbackFlags within a mutex for us.
666 context->setCallbackMode(env, installed, manualBuffer);
667}
668
Ashok Bhat4838e332014-01-03 14:37:19 +0000669static void android_hardware_Camera_addCallbackBuffer(JNIEnv *env, jobject thiz, jbyteArray bytes, jint msgType) {
Steve Block71f2cf12011-10-20 11:56:00 +0100670 ALOGV("addCallbackBuffer: 0x%x", msgType);
Andrew Harp94927df2009-10-20 01:47:05 -0400671
Ashok Bhat4838e332014-01-03 14:37:19 +0000672 JNICameraContext* context = reinterpret_cast<JNICameraContext*>(env->GetLongField(thiz, fields.context));
Andrew Harp94927df2009-10-20 01:47:05 -0400673
674 if (context != NULL) {
James Donge00cab72011-02-17 16:38:06 -0800675 context->addCallbackBuffer(env, bytes, msgType);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800676 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800677}
678
679static void android_hardware_Camera_autoFocus(JNIEnv *env, jobject thiz)
680{
Steve Block71f2cf12011-10-20 11:56:00 +0100681 ALOGV("autoFocus");
Dave Sparks5e271152009-06-23 17:30:11 -0700682 JNICameraContext* context;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800683 sp<Camera> c = get_native_camera(env, thiz, &context);
684 if (c == 0) return;
685
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800686 if (c->autoFocus() != NO_ERROR) {
Elliott Hughes69a017b2011-04-08 14:10:28 -0700687 jniThrowRuntimeException(env, "autoFocus failed");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800688 }
689}
690
Chih-Chung Chang244f8c22009-09-15 14:51:56 +0800691static void android_hardware_Camera_cancelAutoFocus(JNIEnv *env, jobject thiz)
692{
Steve Block71f2cf12011-10-20 11:56:00 +0100693 ALOGV("cancelAutoFocus");
Chih-Chung Chang244f8c22009-09-15 14:51:56 +0800694 JNICameraContext* context;
695 sp<Camera> c = get_native_camera(env, thiz, &context);
696 if (c == 0) return;
697
698 if (c->cancelAutoFocus() != NO_ERROR) {
Elliott Hughes69a017b2011-04-08 14:10:28 -0700699 jniThrowRuntimeException(env, "cancelAutoFocus failed");
Chih-Chung Chang244f8c22009-09-15 14:51:56 +0800700 }
701}
702
Ashok Bhat4838e332014-01-03 14:37:19 +0000703static void android_hardware_Camera_takePicture(JNIEnv *env, jobject thiz, jint msgType)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800704{
Steve Block71f2cf12011-10-20 11:56:00 +0100705 ALOGV("takePicture");
Dave Sparks5e271152009-06-23 17:30:11 -0700706 JNICameraContext* context;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800707 sp<Camera> camera = get_native_camera(env, thiz, &context);
708 if (camera == 0) return;
709
James Donge00cab72011-02-17 16:38:06 -0800710 /*
711 * When CAMERA_MSG_RAW_IMAGE is requested, if the raw image callback
712 * buffer is available, CAMERA_MSG_RAW_IMAGE is enabled to get the
713 * notification _and_ the data; otherwise, CAMERA_MSG_RAW_IMAGE_NOTIFY
714 * is enabled to receive the callback notification but no data.
715 *
716 * Note that CAMERA_MSG_RAW_IMAGE_NOTIFY is not exposed to the
717 * Java application.
718 */
719 if (msgType & CAMERA_MSG_RAW_IMAGE) {
Steve Block71f2cf12011-10-20 11:56:00 +0100720 ALOGV("Enable raw image callback buffer");
James Donge00cab72011-02-17 16:38:06 -0800721 if (!context->isRawImageCallbackBufferAvailable()) {
Steve Block71f2cf12011-10-20 11:56:00 +0100722 ALOGV("Enable raw image notification, since no callback buffer exists");
James Donge00cab72011-02-17 16:38:06 -0800723 msgType &= ~CAMERA_MSG_RAW_IMAGE;
724 msgType |= CAMERA_MSG_RAW_IMAGE_NOTIFY;
725 }
726 }
727
728 if (camera->takePicture(msgType) != NO_ERROR) {
Elliott Hughes69a017b2011-04-08 14:10:28 -0700729 jniThrowRuntimeException(env, "takePicture failed");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800730 return;
731 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800732}
733
734static void android_hardware_Camera_setParameters(JNIEnv *env, jobject thiz, jstring params)
735{
Steve Block71f2cf12011-10-20 11:56:00 +0100736 ALOGV("setParameters");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800737 sp<Camera> camera = get_native_camera(env, thiz, NULL);
738 if (camera == 0) return;
739
740 const jchar* str = env->GetStringCritical(params, 0);
741 String8 params8;
742 if (params) {
743 params8 = String8(str, env->GetStringLength(params));
744 env->ReleaseStringCritical(params, str);
745 }
746 if (camera->setParameters(params8) != NO_ERROR) {
Elliott Hughes69a017b2011-04-08 14:10:28 -0700747 jniThrowRuntimeException(env, "setParameters failed");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800748 return;
749 }
750}
751
752static jstring android_hardware_Camera_getParameters(JNIEnv *env, jobject thiz)
753{
Steve Block71f2cf12011-10-20 11:56:00 +0100754 ALOGV("getParameters");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800755 sp<Camera> camera = get_native_camera(env, thiz, NULL);
756 if (camera == 0) return 0;
757
Wu-cheng Lia1c41e12012-02-23 19:01:00 -0800758 String8 params8 = camera->getParameters();
759 if (params8.isEmpty()) {
760 jniThrowRuntimeException(env, "getParameters failed (empty parameters)");
761 return 0;
762 }
763 return env->NewStringUTF(params8.string());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800764}
765
766static void android_hardware_Camera_reconnect(JNIEnv *env, jobject thiz)
767{
Steve Block71f2cf12011-10-20 11:56:00 +0100768 ALOGV("reconnect");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800769 sp<Camera> camera = get_native_camera(env, thiz, NULL);
770 if (camera == 0) return;
771
772 if (camera->reconnect() != NO_ERROR) {
773 jniThrowException(env, "java/io/IOException", "reconnect failed");
774 return;
775 }
776}
777
Wu-cheng Liffe1cf22009-09-10 16:49:17 +0800778static void android_hardware_Camera_lock(JNIEnv *env, jobject thiz)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800779{
Steve Block71f2cf12011-10-20 11:56:00 +0100780 ALOGV("lock");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800781 sp<Camera> camera = get_native_camera(env, thiz, NULL);
Wu-cheng Liffe1cf22009-09-10 16:49:17 +0800782 if (camera == 0) return;
783
784 if (camera->lock() != NO_ERROR) {
Elliott Hughes69a017b2011-04-08 14:10:28 -0700785 jniThrowRuntimeException(env, "lock failed");
Wu-cheng Liffe1cf22009-09-10 16:49:17 +0800786 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800787}
788
Wu-cheng Liffe1cf22009-09-10 16:49:17 +0800789static void android_hardware_Camera_unlock(JNIEnv *env, jobject thiz)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800790{
Steve Block71f2cf12011-10-20 11:56:00 +0100791 ALOGV("unlock");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800792 sp<Camera> camera = get_native_camera(env, thiz, NULL);
Wu-cheng Liffe1cf22009-09-10 16:49:17 +0800793 if (camera == 0) return;
794
795 if (camera->unlock() != NO_ERROR) {
Elliott Hughes69a017b2011-04-08 14:10:28 -0700796 jniThrowRuntimeException(env, "unlock failed");
Wu-cheng Liffe1cf22009-09-10 16:49:17 +0800797 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800798}
799
Wu-cheng Li36f68b82009-09-28 16:14:58 -0700800static void android_hardware_Camera_startSmoothZoom(JNIEnv *env, jobject thiz, jint value)
801{
Steve Block71f2cf12011-10-20 11:56:00 +0100802 ALOGV("startSmoothZoom");
Wu-cheng Li36f68b82009-09-28 16:14:58 -0700803 sp<Camera> camera = get_native_camera(env, thiz, NULL);
804 if (camera == 0) return;
805
Wu-cheng Li0ca25192010-03-29 16:21:12 +0800806 status_t rc = camera->sendCommand(CAMERA_CMD_START_SMOOTH_ZOOM, value, 0);
807 if (rc == BAD_VALUE) {
808 char msg[64];
809 sprintf(msg, "invalid zoom value=%d", value);
810 jniThrowException(env, "java/lang/IllegalArgumentException", msg);
811 } else if (rc != NO_ERROR) {
Elliott Hughes69a017b2011-04-08 14:10:28 -0700812 jniThrowRuntimeException(env, "start smooth zoom failed");
Wu-cheng Li36f68b82009-09-28 16:14:58 -0700813 }
814}
815
816static void android_hardware_Camera_stopSmoothZoom(JNIEnv *env, jobject thiz)
817{
Steve Block71f2cf12011-10-20 11:56:00 +0100818 ALOGV("stopSmoothZoom");
Wu-cheng Li36f68b82009-09-28 16:14:58 -0700819 sp<Camera> camera = get_native_camera(env, thiz, NULL);
820 if (camera == 0) return;
821
822 if (camera->sendCommand(CAMERA_CMD_STOP_SMOOTH_ZOOM, 0, 0) != NO_ERROR) {
Elliott Hughes69a017b2011-04-08 14:10:28 -0700823 jniThrowRuntimeException(env, "stop smooth zoom failed");
Wu-cheng Li36f68b82009-09-28 16:14:58 -0700824 }
825}
826
Chih-Chung Changd1d77062010-01-22 17:49:48 -0800827static void android_hardware_Camera_setDisplayOrientation(JNIEnv *env, jobject thiz,
828 jint value)
829{
Steve Block71f2cf12011-10-20 11:56:00 +0100830 ALOGV("setDisplayOrientation");
Chih-Chung Changd1d77062010-01-22 17:49:48 -0800831 sp<Camera> camera = get_native_camera(env, thiz, NULL);
832 if (camera == 0) return;
833
834 if (camera->sendCommand(CAMERA_CMD_SET_DISPLAY_ORIENTATION, value, 0) != NO_ERROR) {
Elliott Hughes69a017b2011-04-08 14:10:28 -0700835 jniThrowRuntimeException(env, "set display orientation failed");
Chih-Chung Changd1d77062010-01-22 17:49:48 -0800836 }
837}
838
Eino-Ville Talvala69fe5272012-09-07 12:26:40 -0700839static jboolean android_hardware_Camera_enableShutterSound(JNIEnv *env, jobject thiz,
840 jboolean enabled)
841{
842 ALOGV("enableShutterSound");
843 sp<Camera> camera = get_native_camera(env, thiz, NULL);
844 if (camera == 0) return JNI_FALSE;
845
846 int32_t value = (enabled == JNI_TRUE) ? 1 : 0;
847 status_t rc = camera->sendCommand(CAMERA_CMD_ENABLE_SHUTTER_SOUND, value, 0);
848 if (rc == NO_ERROR) {
849 return JNI_TRUE;
850 } else if (rc == PERMISSION_DENIED) {
851 return JNI_FALSE;
852 } else {
853 jniThrowRuntimeException(env, "enable shutter sound failed");
854 return JNI_FALSE;
855 }
856}
857
Wu-cheng Li4c2292e2011-07-22 02:37:11 +0800858static void android_hardware_Camera_startFaceDetection(JNIEnv *env, jobject thiz,
Wu-cheng Libb1e2752011-07-30 05:00:37 +0800859 jint type)
Wu-cheng Li4c2292e2011-07-22 02:37:11 +0800860{
Steve Block71f2cf12011-10-20 11:56:00 +0100861 ALOGV("startFaceDetection");
Wu-cheng Li4c2292e2011-07-22 02:37:11 +0800862 JNICameraContext* context;
863 sp<Camera> camera = get_native_camera(env, thiz, &context);
864 if (camera == 0) return;
865
866 status_t rc = camera->sendCommand(CAMERA_CMD_START_FACE_DETECTION, type, 0);
867 if (rc == BAD_VALUE) {
868 char msg[64];
869 snprintf(msg, sizeof(msg), "invalid face detection type=%d", type);
870 jniThrowException(env, "java/lang/IllegalArgumentException", msg);
871 } else if (rc != NO_ERROR) {
872 jniThrowRuntimeException(env, "start face detection failed");
873 }
874}
875
876static void android_hardware_Camera_stopFaceDetection(JNIEnv *env, jobject thiz)
877{
Steve Block71f2cf12011-10-20 11:56:00 +0100878 ALOGV("stopFaceDetection");
Wu-cheng Li4c2292e2011-07-22 02:37:11 +0800879 sp<Camera> camera = get_native_camera(env, thiz, NULL);
880 if (camera == 0) return;
881
882 if (camera->sendCommand(CAMERA_CMD_STOP_FACE_DETECTION, 0, 0) != NO_ERROR) {
883 jniThrowRuntimeException(env, "stop face detection failed");
884 }
885}
886
Wu-cheng Li9d062cf2011-11-14 20:30:14 +0800887static void android_hardware_Camera_enableFocusMoveCallback(JNIEnv *env, jobject thiz, jint enable)
888{
Wu-cheng Li02097522011-11-30 11:06:04 +0800889 ALOGV("enableFocusMoveCallback");
Wu-cheng Li9d062cf2011-11-14 20:30:14 +0800890 sp<Camera> camera = get_native_camera(env, thiz, NULL);
891 if (camera == 0) return;
892
893 if (camera->sendCommand(CAMERA_CMD_ENABLE_FOCUS_MOVE_MSG, enable, 0) != NO_ERROR) {
894 jniThrowRuntimeException(env, "enable focus move callback failed");
895 }
896}
897
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800898//-------------------------------------------------
899
900static JNINativeMethod camMethods[] = {
Chih-Chung Change25cc652010-05-06 16:36:58 +0800901 { "getNumberOfCameras",
902 "()I",
903 (void *)android_hardware_Camera_getNumberOfCameras },
Eino-Ville Talvala4f8e5ce2012-10-08 18:16:35 -0700904 { "_getCameraInfo",
Chih-Chung Changb8bb78f2010-06-10 13:32:16 +0800905 "(ILandroid/hardware/Camera$CameraInfo;)V",
906 (void*)android_hardware_Camera_getCameraInfo },
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800907 { "native_setup",
Zhijun He4c913802014-06-16 16:42:35 -0700908 "(Ljava/lang/Object;IILjava/lang/String;)I",
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800909 (void*)android_hardware_Camera_native_setup },
910 { "native_release",
911 "()V",
912 (void*)android_hardware_Camera_release },
Ruben Brunkfeb50af2014-05-09 19:58:49 -0700913 { "setPreviewSurface",
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800914 "(Landroid/view/Surface;)V",
Ruben Brunkfeb50af2014-05-09 19:58:49 -0700915 (void *)android_hardware_Camera_setPreviewSurface },
Jamie Gennisfd6f39e2010-12-20 12:15:00 -0800916 { "setPreviewTexture",
917 "(Landroid/graphics/SurfaceTexture;)V",
918 (void *)android_hardware_Camera_setPreviewTexture },
Eino-Ville Talvala7005b672013-04-02 15:46:38 -0700919 { "setPreviewCallbackSurface",
920 "(Landroid/view/Surface;)V",
921 (void *)android_hardware_Camera_setPreviewCallbackSurface },
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800922 { "startPreview",
923 "()V",
924 (void *)android_hardware_Camera_startPreview },
Wu-cheng Li4c2292e2011-07-22 02:37:11 +0800925 { "_stopPreview",
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800926 "()V",
927 (void *)android_hardware_Camera_stopPreview },
928 { "previewEnabled",
929 "()Z",
930 (void *)android_hardware_Camera_previewEnabled },
931 { "setHasPreviewCallback",
932 "(ZZ)V",
933 (void *)android_hardware_Camera_setHasPreviewCallback },
James Donge00cab72011-02-17 16:38:06 -0800934 { "_addCallbackBuffer",
935 "([BI)V",
Andrew Harp94927df2009-10-20 01:47:05 -0400936 (void *)android_hardware_Camera_addCallbackBuffer },
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800937 { "native_autoFocus",
938 "()V",
939 (void *)android_hardware_Camera_autoFocus },
Chih-Chung Chang244f8c22009-09-15 14:51:56 +0800940 { "native_cancelAutoFocus",
941 "()V",
942 (void *)android_hardware_Camera_cancelAutoFocus },
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800943 { "native_takePicture",
James Donge00cab72011-02-17 16:38:06 -0800944 "(I)V",
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800945 (void *)android_hardware_Camera_takePicture },
946 { "native_setParameters",
947 "(Ljava/lang/String;)V",
948 (void *)android_hardware_Camera_setParameters },
949 { "native_getParameters",
950 "()Ljava/lang/String;",
951 (void *)android_hardware_Camera_getParameters },
952 { "reconnect",
953 "()V",
954 (void*)android_hardware_Camera_reconnect },
955 { "lock",
Wu-cheng Liffe1cf22009-09-10 16:49:17 +0800956 "()V",
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800957 (void*)android_hardware_Camera_lock },
958 { "unlock",
Wu-cheng Liffe1cf22009-09-10 16:49:17 +0800959 "()V",
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800960 (void*)android_hardware_Camera_unlock },
Wu-cheng Li36f68b82009-09-28 16:14:58 -0700961 { "startSmoothZoom",
962 "(I)V",
963 (void *)android_hardware_Camera_startSmoothZoom },
964 { "stopSmoothZoom",
965 "()V",
966 (void *)android_hardware_Camera_stopSmoothZoom },
Chih-Chung Changd1d77062010-01-22 17:49:48 -0800967 { "setDisplayOrientation",
968 "(I)V",
969 (void *)android_hardware_Camera_setDisplayOrientation },
Eino-Ville Talvala4f8e5ce2012-10-08 18:16:35 -0700970 { "_enableShutterSound",
Eino-Ville Talvala69fe5272012-09-07 12:26:40 -0700971 "(Z)Z",
972 (void *)android_hardware_Camera_enableShutterSound },
Wu-cheng Li4c2292e2011-07-22 02:37:11 +0800973 { "_startFaceDetection",
974 "(I)V",
975 (void *)android_hardware_Camera_startFaceDetection },
976 { "_stopFaceDetection",
977 "()V",
978 (void *)android_hardware_Camera_stopFaceDetection},
Wu-cheng Li9d062cf2011-11-14 20:30:14 +0800979 { "enableFocusMoveCallback",
980 "(I)V",
981 (void *)android_hardware_Camera_enableFocusMoveCallback},
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800982};
983
984struct field {
985 const char *class_name;
986 const char *field_name;
987 const char *field_type;
988 jfieldID *jfield;
989};
990
991static int find_fields(JNIEnv *env, field *fields, int count)
992{
993 for (int i = 0; i < count; i++) {
994 field *f = &fields[i];
995 jclass clazz = env->FindClass(f->class_name);
996 if (clazz == NULL) {
Steve Block3762c312012-01-06 19:20:56 +0000997 ALOGE("Can't find %s", f->class_name);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800998 return -1;
999 }
1000
1001 jfieldID field = env->GetFieldID(clazz, f->field_name, f->field_type);
1002 if (field == NULL) {
Steve Block3762c312012-01-06 19:20:56 +00001003 ALOGE("Can't find %s.%s", f->class_name, f->field_name);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001004 return -1;
1005 }
1006
1007 *(f->jfield) = field;
1008 }
1009
1010 return 0;
1011}
1012
1013// Get all the required offsets in java class and register native functions
1014int register_android_hardware_Camera(JNIEnv *env)
1015{
1016 field fields_to_find[] = {
Ashok Bhat4838e332014-01-03 14:37:19 +00001017 { "android/hardware/Camera", "mNativeContext", "J", &fields.context },
Wu-cheng Li3fd5fa42010-09-15 16:06:20 -07001018 { "android/hardware/Camera$CameraInfo", "facing", "I", &fields.facing },
1019 { "android/hardware/Camera$CameraInfo", "orientation", "I", &fields.orientation },
Eino-Ville Talvalaf7c6c5a2012-09-19 11:46:11 -07001020 { "android/hardware/Camera$CameraInfo", "canDisableShutterSound", "Z",
1021 &fields.canDisableShutterSound },
Wu-cheng Lif0d6a482011-07-28 05:30:59 +08001022 { "android/hardware/Camera$Face", "rect", "Landroid/graphics/Rect;", &fields.face_rect },
1023 { "android/hardware/Camera$Face", "score", "I", &fields.face_score },
Wu-cheng Li4c2292e2011-07-22 02:37:11 +08001024 { "android/graphics/Rect", "left", "I", &fields.rect_left },
1025 { "android/graphics/Rect", "top", "I", &fields.rect_top },
1026 { "android/graphics/Rect", "right", "I", &fields.rect_right },
1027 { "android/graphics/Rect", "bottom", "I", &fields.rect_bottom },
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001028 };
1029
1030 if (find_fields(env, fields_to_find, NELEM(fields_to_find)) < 0)
1031 return -1;
1032
1033 jclass clazz = env->FindClass("android/hardware/Camera");
1034 fields.post_event = env->GetStaticMethodID(clazz, "postEventFromNative",
1035 "(Ljava/lang/Object;IIILjava/lang/Object;)V");
1036 if (fields.post_event == NULL) {
Steve Block3762c312012-01-06 19:20:56 +00001037 ALOGE("Can't find android/hardware/Camera.postEventFromNative");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001038 return -1;
1039 }
1040
Wu-cheng Libb1e2752011-07-30 05:00:37 +08001041 clazz = env->FindClass("android/graphics/Rect");
1042 fields.rect_constructor = env->GetMethodID(clazz, "<init>", "()V");
1043 if (fields.rect_constructor == NULL) {
Steve Block3762c312012-01-06 19:20:56 +00001044 ALOGE("Can't find android/graphics/Rect.Rect()");
Wu-cheng Libb1e2752011-07-30 05:00:37 +08001045 return -1;
1046 }
1047
1048 clazz = env->FindClass("android/hardware/Camera$Face");
1049 fields.face_constructor = env->GetMethodID(clazz, "<init>", "()V");
1050 if (fields.face_constructor == NULL) {
Steve Block3762c312012-01-06 19:20:56 +00001051 ALOGE("Can't find android/hardware/Camera$Face.Face()");
Wu-cheng Libb1e2752011-07-30 05:00:37 +08001052 return -1;
1053 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001054
1055 // Register native functions
1056 return AndroidRuntime::registerNativeMethods(env, "android/hardware/Camera",
1057 camMethods, NELEM(camMethods));
1058}