blob: d606c2d6162e5c7ca61a5092d96b2f9e6304f11c [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"
Andreas Gampeed6b9df2014-11-20 22:02:20 -080024#include "core_jni_helpers.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;
Igor Murashkin0601ab12014-09-18 15:17:20 -070051 jfieldID face_id;
52 jfieldID face_left_eye;
53 jfieldID face_right_eye;
54 jfieldID face_mouth;
Wu-cheng Li4c2292e2011-07-22 02:37:11 +080055 jfieldID rect_left;
56 jfieldID rect_top;
57 jfieldID rect_right;
58 jfieldID rect_bottom;
Igor Murashkin0601ab12014-09-18 15:17:20 -070059 jfieldID point_x;
60 jfieldID point_y;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080061 jmethodID post_event;
Wu-cheng Libb1e2752011-07-30 05:00:37 +080062 jmethodID rect_constructor;
63 jmethodID face_constructor;
Igor Murashkin0601ab12014-09-18 15:17:20 -070064 jmethodID point_constructor;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080065};
66
67static fields_t fields;
68static Mutex sLock;
69
Dave Sparks5e271152009-06-23 17:30:11 -070070// provides persistent context for calls from native code to Java
71class JNICameraContext: public CameraListener
72{
73public:
74 JNICameraContext(JNIEnv* env, jobject weak_this, jclass clazz, const sp<Camera>& camera);
75 ~JNICameraContext() { release(); }
76 virtual void notify(int32_t msgType, int32_t ext1, int32_t ext2);
Wu-cheng Libb1e2752011-07-30 05:00:37 +080077 virtual void postData(int32_t msgType, const sp<IMemory>& dataPtr,
78 camera_frame_metadata_t *metadata);
Dave Sparks59c1a932009-07-08 15:56:53 -070079 virtual void postDataTimestamp(nsecs_t timestamp, int32_t msgType, const sp<IMemory>& dataPtr);
Chien-Yu Chene2592712016-04-28 12:14:32 -070080 virtual void postRecordingFrameHandleTimestamp(nsecs_t timestamp, native_handle_t* handle);
Yin-Chia Yeh19ddea92017-03-21 18:48:44 -070081 virtual void postRecordingFrameHandleTimestampBatch(
82 const std::vector<nsecs_t>& timestamps,
83 const std::vector<native_handle_t*>& handles);
Wu-cheng Libb1e2752011-07-30 05:00:37 +080084 void postMetadata(JNIEnv *env, int32_t msgType, camera_frame_metadata_t *metadata);
James Donge00cab72011-02-17 16:38:06 -080085 void addCallbackBuffer(JNIEnv *env, jbyteArray cbb, int msgType);
Andrew Harp94927df2009-10-20 01:47:05 -040086 void setCallbackMode(JNIEnv *env, bool installed, bool manualMode);
Dave Sparks5e271152009-06-23 17:30:11 -070087 sp<Camera> getCamera() { Mutex::Autolock _l(mLock); return mCamera; }
James Donge00cab72011-02-17 16:38:06 -080088 bool isRawImageCallbackBufferAvailable() const;
Dave Sparks5e271152009-06-23 17:30:11 -070089 void release();
90
91private:
92 void copyAndPost(JNIEnv* env, const sp<IMemory>& dataPtr, int msgType);
James Donge00cab72011-02-17 16:38:06 -080093 void clearCallbackBuffers_l(JNIEnv *env, Vector<jbyteArray> *buffers);
Andrew Harp94927df2009-10-20 01:47:05 -040094 void clearCallbackBuffers_l(JNIEnv *env);
James Donge00cab72011-02-17 16:38:06 -080095 jbyteArray getCallbackBuffer(JNIEnv *env, Vector<jbyteArray> *buffers, size_t bufferSize);
Dave Sparks5e271152009-06-23 17:30:11 -070096
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080097 jobject mCameraJObjectWeak; // weak reference to java object
98 jclass mCameraJClass; // strong reference to java class
Wu-cheng Liffe1cf22009-09-10 16:49:17 +080099 sp<Camera> mCamera; // strong reference to native object
Wu-cheng Libb1e2752011-07-30 05:00:37 +0800100 jclass mFaceClass; // strong reference to Face class
101 jclass mRectClass; // strong reference to Rect class
Igor Murashkin0601ab12014-09-18 15:17:20 -0700102 jclass mPointClass; // strong reference to Point class
Dave Sparks5e271152009-06-23 17:30:11 -0700103 Mutex mLock;
Andrew Harp94927df2009-10-20 01:47:05 -0400104
James Donge00cab72011-02-17 16:38:06 -0800105 /*
106 * Global reference application-managed raw image buffer queue.
107 *
108 * Manual-only mode is supported for raw image callbacks, which is
109 * set whenever method addCallbackBuffer() with msgType =
110 * CAMERA_MSG_RAW_IMAGE is called; otherwise, null is returned
111 * with raw image callbacks.
112 */
113 Vector<jbyteArray> mRawImageCallbackBuffers;
114
115 /*
116 * Application-managed preview buffer queue and the flags
117 * associated with the usage of the preview buffer callback.
118 */
Andrew Harp94927df2009-10-20 01:47:05 -0400119 Vector<jbyteArray> mCallbackBuffers; // Global reference application managed byte[]
120 bool mManualBufferMode; // Whether to use application managed buffers.
James Donge00cab72011-02-17 16:38:06 -0800121 bool mManualCameraCallbackSet; // Whether the callback has been set, used to
122 // reduce unnecessary calls to set the callback.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800123};
124
James Donge00cab72011-02-17 16:38:06 -0800125bool JNICameraContext::isRawImageCallbackBufferAvailable() const
126{
127 return !mRawImageCallbackBuffers.isEmpty();
128}
129
Dave Sparks5e271152009-06-23 17:30:11 -0700130sp<Camera> get_native_camera(JNIEnv *env, jobject thiz, JNICameraContext** pContext)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800131{
132 sp<Camera> camera;
133 Mutex::Autolock _l(sLock);
Ashok Bhat4838e332014-01-03 14:37:19 +0000134 JNICameraContext* context = reinterpret_cast<JNICameraContext*>(env->GetLongField(thiz, fields.context));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800135 if (context != NULL) {
Dave Sparks5e271152009-06-23 17:30:11 -0700136 camera = context->getCamera();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800137 }
Steve Block71f2cf12011-10-20 11:56:00 +0100138 ALOGV("get_native_camera: context=%p, camera=%p", context, camera.get());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800139 if (camera == 0) {
Eino-Ville Talvalaf9133342015-01-12 17:01:11 -0800140 jniThrowRuntimeException(env,
141 "Camera is being used after Camera.release() was called");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800142 }
143
144 if (pContext != NULL) *pContext = context;
145 return camera;
146}
147
Dave Sparks5e271152009-06-23 17:30:11 -0700148JNICameraContext::JNICameraContext(JNIEnv* env, jobject weak_this, jclass clazz, const sp<Camera>& camera)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800149{
Dave Sparks5e271152009-06-23 17:30:11 -0700150 mCameraJObjectWeak = env->NewGlobalRef(weak_this);
151 mCameraJClass = (jclass)env->NewGlobalRef(clazz);
152 mCamera = camera;
Andrew Harp94927df2009-10-20 01:47:05 -0400153
Wu-cheng Libb1e2752011-07-30 05:00:37 +0800154 jclass faceClazz = env->FindClass("android/hardware/Camera$Face");
155 mFaceClass = (jclass) env->NewGlobalRef(faceClazz);
156
157 jclass rectClazz = env->FindClass("android/graphics/Rect");
158 mRectClass = (jclass) env->NewGlobalRef(rectClazz);
159
Igor Murashkin0601ab12014-09-18 15:17:20 -0700160 jclass pointClazz = env->FindClass("android/graphics/Point");
161 mPointClass = (jclass) env->NewGlobalRef(pointClazz);
162
Andrew Harp94927df2009-10-20 01:47:05 -0400163 mManualBufferMode = false;
164 mManualCameraCallbackSet = false;
Dave Sparks5e271152009-06-23 17:30:11 -0700165}
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800166
Dave Sparks5e271152009-06-23 17:30:11 -0700167void JNICameraContext::release()
168{
Steve Block71f2cf12011-10-20 11:56:00 +0100169 ALOGV("release");
Dave Sparks5e271152009-06-23 17:30:11 -0700170 Mutex::Autolock _l(mLock);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800171 JNIEnv *env = AndroidRuntime::getJNIEnv();
Dave Sparks5e271152009-06-23 17:30:11 -0700172
173 if (mCameraJObjectWeak != NULL) {
174 env->DeleteGlobalRef(mCameraJObjectWeak);
175 mCameraJObjectWeak = NULL;
176 }
177 if (mCameraJClass != NULL) {
178 env->DeleteGlobalRef(mCameraJClass);
179 mCameraJClass = NULL;
180 }
Wu-cheng Libb1e2752011-07-30 05:00:37 +0800181 if (mFaceClass != NULL) {
182 env->DeleteGlobalRef(mFaceClass);
183 mFaceClass = NULL;
184 }
185 if (mRectClass != NULL) {
186 env->DeleteGlobalRef(mRectClass);
187 mRectClass = NULL;
188 }
Igor Murashkin0601ab12014-09-18 15:17:20 -0700189 if (mPointClass != NULL) {
190 env->DeleteGlobalRef(mPointClass);
191 mPointClass = NULL;
192 }
Andrew Harp94927df2009-10-20 01:47:05 -0400193 clearCallbackBuffers_l(env);
Dave Sparks5e271152009-06-23 17:30:11 -0700194 mCamera.clear();
195}
196
197void JNICameraContext::notify(int32_t msgType, int32_t ext1, int32_t ext2)
198{
Steve Block71f2cf12011-10-20 11:56:00 +0100199 ALOGV("notify");
Dave Sparks5e271152009-06-23 17:30:11 -0700200
201 // VM pointer will be NULL if object is released
202 Mutex::Autolock _l(mLock);
203 if (mCameraJObjectWeak == NULL) {
Steve Block8564c8d2012-01-05 23:22:43 +0000204 ALOGW("callback on dead camera object");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800205 return;
206 }
Dave Sparks5e271152009-06-23 17:30:11 -0700207 JNIEnv *env = AndroidRuntime::getJNIEnv();
James Donge00cab72011-02-17 16:38:06 -0800208
209 /*
210 * If the notification or msgType is CAMERA_MSG_RAW_IMAGE_NOTIFY, change it
211 * to CAMERA_MSG_RAW_IMAGE since CAMERA_MSG_RAW_IMAGE_NOTIFY is not exposed
212 * to the Java app.
213 */
214 if (msgType == CAMERA_MSG_RAW_IMAGE_NOTIFY) {
215 msgType = CAMERA_MSG_RAW_IMAGE;
216 }
217
Dave Sparksc62f9bd2009-06-26 13:33:32 -0700218 env->CallStaticVoidMethod(mCameraJClass, fields.post_event,
Chih-Chung Chang6157de02009-09-24 15:29:35 -0700219 mCameraJObjectWeak, msgType, ext1, ext2, NULL);
Dave Sparks5e271152009-06-23 17:30:11 -0700220}
221
James Donge00cab72011-02-17 16:38:06 -0800222jbyteArray JNICameraContext::getCallbackBuffer(
223 JNIEnv* env, Vector<jbyteArray>* buffers, size_t bufferSize)
224{
225 jbyteArray obj = NULL;
226
227 // Vector access should be protected by lock in postData()
228 if (!buffers->isEmpty()) {
Dan Albert46d84442014-11-18 16:07:51 -0800229 ALOGV("Using callback buffer from queue of length %zu", buffers->size());
James Donge00cab72011-02-17 16:38:06 -0800230 jbyteArray globalBuffer = buffers->itemAt(0);
231 buffers->removeAt(0);
232
233 obj = (jbyteArray)env->NewLocalRef(globalBuffer);
234 env->DeleteGlobalRef(globalBuffer);
235
236 if (obj != NULL) {
237 jsize bufferLength = env->GetArrayLength(obj);
238 if ((int)bufferLength < (int)bufferSize) {
Dan Albert46d84442014-11-18 16:07:51 -0800239 ALOGE("Callback buffer was too small! Expected %zu bytes, but got %d bytes!",
James Donge00cab72011-02-17 16:38:06 -0800240 bufferSize, bufferLength);
241 env->DeleteLocalRef(obj);
242 return NULL;
243 }
244 }
245 }
246
247 return obj;
248}
249
Dave Sparks5e271152009-06-23 17:30:11 -0700250void JNICameraContext::copyAndPost(JNIEnv* env, const sp<IMemory>& dataPtr, int msgType)
251{
252 jbyteArray obj = NULL;
253
254 // allocate Java byte array and copy data
255 if (dataPtr != NULL) {
256 ssize_t offset;
257 size_t size;
258 sp<IMemoryHeap> heap = dataPtr->getMemory(&offset, &size);
Glenn Kasten2fbf25b2014-03-28 15:41:58 -0700259 ALOGV("copyAndPost: off=%zd, size=%zu", offset, size);
Dave Sparks5e271152009-06-23 17:30:11 -0700260 uint8_t *heapBase = (uint8_t*)heap->base();
261
262 if (heapBase != NULL) {
Dave Sparksc4ca4202009-07-13 09:38:20 -0700263 const jbyte* data = reinterpret_cast<const jbyte*>(heapBase + offset);
Andrew Harp94927df2009-10-20 01:47:05 -0400264
Wu-cheng Li5f0ef5f2011-09-21 16:40:23 +0800265 if (msgType == CAMERA_MSG_RAW_IMAGE) {
266 obj = getCallbackBuffer(env, &mRawImageCallbackBuffers, size);
267 } else if (msgType == CAMERA_MSG_PREVIEW_FRAME && mManualBufferMode) {
268 obj = getCallbackBuffer(env, &mCallbackBuffers, size);
Andrew Harp94927df2009-10-20 01:47:05 -0400269
Wu-cheng Li5f0ef5f2011-09-21 16:40:23 +0800270 if (mCallbackBuffers.isEmpty()) {
Steve Block71f2cf12011-10-20 11:56:00 +0100271 ALOGV("Out of buffers, clearing callback!");
Wu-cheng Li5f0ef5f2011-09-21 16:40:23 +0800272 mCamera->setPreviewCallbackFlags(CAMERA_FRAME_CALLBACK_FLAG_NOOP);
273 mManualCameraCallbackSet = false;
Andrew Harp94927df2009-10-20 01:47:05 -0400274
Wu-cheng Li5f0ef5f2011-09-21 16:40:23 +0800275 if (obj == NULL) {
Andrew Harp94927df2009-10-20 01:47:05 -0400276 return;
277 }
278 }
Wu-cheng Li5f0ef5f2011-09-21 16:40:23 +0800279 } else {
Steve Block71f2cf12011-10-20 11:56:00 +0100280 ALOGV("Allocating callback buffer");
Wu-cheng Li5f0ef5f2011-09-21 16:40:23 +0800281 obj = env->NewByteArray(size);
Andrew Harp94927df2009-10-20 01:47:05 -0400282 }
283
Dave Sparks5e271152009-06-23 17:30:11 -0700284 if (obj == NULL) {
Steve Block3762c312012-01-06 19:20:56 +0000285 ALOGE("Couldn't allocate byte array for JPEG data");
Dave Sparks5e271152009-06-23 17:30:11 -0700286 env->ExceptionClear();
287 } else {
Dave Sparksa95f4952009-07-10 18:13:36 -0700288 env->SetByteArrayRegion(obj, 0, size, data);
Dave Sparks5e271152009-06-23 17:30:11 -0700289 }
290 } else {
Steve Block3762c312012-01-06 19:20:56 +0000291 ALOGE("image heap is NULL");
Dave Sparks5e271152009-06-23 17:30:11 -0700292 }
293 }
294
295 // post image data to Java
296 env->CallStaticVoidMethod(mCameraJClass, fields.post_event,
297 mCameraJObjectWeak, msgType, 0, 0, obj);
298 if (obj) {
299 env->DeleteLocalRef(obj);
300 }
301}
302
Wu-cheng Libb1e2752011-07-30 05:00:37 +0800303void JNICameraContext::postData(int32_t msgType, const sp<IMemory>& dataPtr,
304 camera_frame_metadata_t *metadata)
Dave Sparks5e271152009-06-23 17:30:11 -0700305{
306 // VM pointer will be NULL if object is released
307 Mutex::Autolock _l(mLock);
308 JNIEnv *env = AndroidRuntime::getJNIEnv();
Dave Sparksd0cbb1a2009-06-29 19:03:33 -0700309 if (mCameraJObjectWeak == NULL) {
Steve Block8564c8d2012-01-05 23:22:43 +0000310 ALOGW("callback on dead camera object");
Dave Sparksd0cbb1a2009-06-29 19:03:33 -0700311 return;
312 }
Dave Sparks5e271152009-06-23 17:30:11 -0700313
Wu-cheng Libb1e2752011-07-30 05:00:37 +0800314 int32_t dataMsgType = msgType & ~CAMERA_MSG_PREVIEW_METADATA;
315
Dave Sparks5e271152009-06-23 17:30:11 -0700316 // return data based on callback type
Wu-cheng Libb1e2752011-07-30 05:00:37 +0800317 switch (dataMsgType) {
James Donge00cab72011-02-17 16:38:06 -0800318 case CAMERA_MSG_VIDEO_FRAME:
319 // should never happen
320 break;
321
322 // For backward-compatibility purpose, if there is no callback
323 // buffer for raw image, the callback returns null.
324 case CAMERA_MSG_RAW_IMAGE:
Steve Block71f2cf12011-10-20 11:56:00 +0100325 ALOGV("rawCallback");
James Donge00cab72011-02-17 16:38:06 -0800326 if (mRawImageCallbackBuffers.isEmpty()) {
327 env->CallStaticVoidMethod(mCameraJClass, fields.post_event,
Wu-cheng Libb1e2752011-07-30 05:00:37 +0800328 mCameraJObjectWeak, dataMsgType, 0, 0, NULL);
James Donge00cab72011-02-17 16:38:06 -0800329 } else {
Wu-cheng Libb1e2752011-07-30 05:00:37 +0800330 copyAndPost(env, dataPtr, dataMsgType);
James Donge00cab72011-02-17 16:38:06 -0800331 }
332 break;
333
Wu-cheng Libb1e2752011-07-30 05:00:37 +0800334 // There is no data.
335 case 0:
James Donge00cab72011-02-17 16:38:06 -0800336 break;
Wu-cheng Libb1e2752011-07-30 05:00:37 +0800337
338 default:
Steve Block71f2cf12011-10-20 11:56:00 +0100339 ALOGV("dataCallback(%d, %p)", dataMsgType, dataPtr.get());
Wu-cheng Libb1e2752011-07-30 05:00:37 +0800340 copyAndPost(env, dataPtr, dataMsgType);
341 break;
342 }
343
344 // post frame metadata to Java
345 if (metadata && (msgType & CAMERA_MSG_PREVIEW_METADATA)) {
346 postMetadata(env, CAMERA_MSG_PREVIEW_METADATA, metadata);
Dave Sparks5e271152009-06-23 17:30:11 -0700347 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800348}
349
Dave Sparks59c1a932009-07-08 15:56:53 -0700350void JNICameraContext::postDataTimestamp(nsecs_t timestamp, int32_t msgType, const sp<IMemory>& dataPtr)
351{
352 // TODO: plumb up to Java. For now, just drop the timestamp
Wu-cheng Libb1e2752011-07-30 05:00:37 +0800353 postData(msgType, dataPtr, NULL);
354}
355
Akhila musunuri44a59492016-07-21 13:33:11 +0530356void JNICameraContext::postRecordingFrameHandleTimestamp(nsecs_t, native_handle_t* handle) {
357 // Video buffers are not needed at app layer so just return the video buffers here.
358 // This may be called when stagefright just releases camera but there are still outstanding
359 // video buffers.
360 if (mCamera != nullptr) {
361 mCamera->releaseRecordingFrameHandle(handle);
362 } else {
363 native_handle_close(handle);
364 native_handle_delete(handle);
365 }
Chien-Yu Chene2592712016-04-28 12:14:32 -0700366}
367
Yin-Chia Yeh19ddea92017-03-21 18:48:44 -0700368void JNICameraContext::postRecordingFrameHandleTimestampBatch(
369 const std::vector<nsecs_t>&,
370 const std::vector<native_handle_t*>& handles) {
371 // Video buffers are not needed at app layer so just return the video buffers here.
372 // This may be called when stagefright just releases camera but there are still outstanding
373 // video buffers.
374 if (mCamera != nullptr) {
375 mCamera->releaseRecordingFrameHandleBatch(handles);
376 } else {
377 for (auto& handle : handles) {
378 native_handle_close(handle);
379 native_handle_delete(handle);
380 }
381 }
382}
383
Wu-cheng Libb1e2752011-07-30 05:00:37 +0800384void JNICameraContext::postMetadata(JNIEnv *env, int32_t msgType, camera_frame_metadata_t *metadata)
385{
386 jobjectArray obj = NULL;
387 obj = (jobjectArray) env->NewObjectArray(metadata->number_of_faces,
388 mFaceClass, NULL);
389 if (obj == NULL) {
Steve Block3762c312012-01-06 19:20:56 +0000390 ALOGE("Couldn't allocate face metadata array");
Wu-cheng Libb1e2752011-07-30 05:00:37 +0800391 return;
392 }
393
394 for (int i = 0; i < metadata->number_of_faces; i++) {
395 jobject face = env->NewObject(mFaceClass, fields.face_constructor);
396 env->SetObjectArrayElement(obj, i, face);
397
398 jobject rect = env->NewObject(mRectClass, fields.rect_constructor);
399 env->SetIntField(rect, fields.rect_left, metadata->faces[i].rect[0]);
400 env->SetIntField(rect, fields.rect_top, metadata->faces[i].rect[1]);
401 env->SetIntField(rect, fields.rect_right, metadata->faces[i].rect[2]);
402 env->SetIntField(rect, fields.rect_bottom, metadata->faces[i].rect[3]);
403
404 env->SetObjectField(face, fields.face_rect, rect);
405 env->SetIntField(face, fields.face_score, metadata->faces[i].score);
406
Igor Murashkin0601ab12014-09-18 15:17:20 -0700407 bool optionalFields = metadata->faces[i].id != 0
408 && metadata->faces[i].left_eye[0] != -2000 && metadata->faces[i].left_eye[1] != -2000
409 && metadata->faces[i].right_eye[0] != -2000 && metadata->faces[i].right_eye[1] != -2000
410 && metadata->faces[i].mouth[0] != -2000 && metadata->faces[i].mouth[1] != -2000;
411 if (optionalFields) {
412 int32_t id = metadata->faces[i].id;
413 env->SetIntField(face, fields.face_id, id);
414
415 jobject leftEye = env->NewObject(mPointClass, fields.point_constructor);
416 env->SetIntField(leftEye, fields.point_x, metadata->faces[i].left_eye[0]);
417 env->SetIntField(leftEye, fields.point_y, metadata->faces[i].left_eye[1]);
418 env->SetObjectField(face, fields.face_left_eye, leftEye);
419 env->DeleteLocalRef(leftEye);
420
421 jobject rightEye = env->NewObject(mPointClass, fields.point_constructor);
422 env->SetIntField(rightEye, fields.point_x, metadata->faces[i].right_eye[0]);
423 env->SetIntField(rightEye, fields.point_y, metadata->faces[i].right_eye[1]);
424 env->SetObjectField(face, fields.face_right_eye, rightEye);
425 env->DeleteLocalRef(rightEye);
426
427 jobject mouth = env->NewObject(mPointClass, fields.point_constructor);
428 env->SetIntField(mouth, fields.point_x, metadata->faces[i].mouth[0]);
429 env->SetIntField(mouth, fields.point_y, metadata->faces[i].mouth[1]);
430 env->SetObjectField(face, fields.face_mouth, mouth);
431 env->DeleteLocalRef(mouth);
432 }
433
Wu-cheng Libb1e2752011-07-30 05:00:37 +0800434 env->DeleteLocalRef(face);
435 env->DeleteLocalRef(rect);
436 }
437 env->CallStaticVoidMethod(mCameraJClass, fields.post_event,
438 mCameraJObjectWeak, msgType, 0, 0, obj);
439 env->DeleteLocalRef(obj);
Dave Sparks59c1a932009-07-08 15:56:53 -0700440}
441
Andrew Harp94927df2009-10-20 01:47:05 -0400442void JNICameraContext::setCallbackMode(JNIEnv *env, bool installed, bool manualMode)
443{
444 Mutex::Autolock _l(mLock);
445 mManualBufferMode = manualMode;
446 mManualCameraCallbackSet = false;
447
448 // In order to limit the over usage of binder threads, all non-manual buffer
Iliyan Malchev9c7ac0d2011-04-14 16:51:21 -0700449 // callbacks use CAMERA_FRAME_CALLBACK_FLAG_BARCODE_SCANNER mode now.
Andrew Harp94927df2009-10-20 01:47:05 -0400450 //
451 // Continuous callbacks will have the callback re-registered from handleMessage.
452 // Manual buffer mode will operate as fast as possible, relying on the finite supply
453 // of buffers for throttling.
454
455 if (!installed) {
Iliyan Malchev9c7ac0d2011-04-14 16:51:21 -0700456 mCamera->setPreviewCallbackFlags(CAMERA_FRAME_CALLBACK_FLAG_NOOP);
James Donge00cab72011-02-17 16:38:06 -0800457 clearCallbackBuffers_l(env, &mCallbackBuffers);
Andrew Harp94927df2009-10-20 01:47:05 -0400458 } else if (mManualBufferMode) {
459 if (!mCallbackBuffers.isEmpty()) {
Iliyan Malchev9c7ac0d2011-04-14 16:51:21 -0700460 mCamera->setPreviewCallbackFlags(CAMERA_FRAME_CALLBACK_FLAG_CAMERA);
Andrew Harp94927df2009-10-20 01:47:05 -0400461 mManualCameraCallbackSet = true;
462 }
463 } else {
Iliyan Malchev9c7ac0d2011-04-14 16:51:21 -0700464 mCamera->setPreviewCallbackFlags(CAMERA_FRAME_CALLBACK_FLAG_BARCODE_SCANNER);
James Donge00cab72011-02-17 16:38:06 -0800465 clearCallbackBuffers_l(env, &mCallbackBuffers);
Andrew Harp94927df2009-10-20 01:47:05 -0400466 }
467}
468
James Donge00cab72011-02-17 16:38:06 -0800469void JNICameraContext::addCallbackBuffer(
470 JNIEnv *env, jbyteArray cbb, int msgType)
Andrew Harp94927df2009-10-20 01:47:05 -0400471{
Steve Block71f2cf12011-10-20 11:56:00 +0100472 ALOGV("addCallbackBuffer: 0x%x", msgType);
Andrew Harp94927df2009-10-20 01:47:05 -0400473 if (cbb != NULL) {
474 Mutex::Autolock _l(mLock);
James Donge00cab72011-02-17 16:38:06 -0800475 switch (msgType) {
476 case CAMERA_MSG_PREVIEW_FRAME: {
477 jbyteArray callbackBuffer = (jbyteArray)env->NewGlobalRef(cbb);
478 mCallbackBuffers.push(callbackBuffer);
Andrew Harp94927df2009-10-20 01:47:05 -0400479
Dan Albert46d84442014-11-18 16:07:51 -0800480 ALOGV("Adding callback buffer to queue, %zu total",
James Donge00cab72011-02-17 16:38:06 -0800481 mCallbackBuffers.size());
Andrew Harp94927df2009-10-20 01:47:05 -0400482
James Donge00cab72011-02-17 16:38:06 -0800483 // We want to make sure the camera knows we're ready for the
484 // next frame. This may have come unset had we not had a
485 // callbackbuffer ready for it last time.
486 if (mManualBufferMode && !mManualCameraCallbackSet) {
Iliyan Malchev9c7ac0d2011-04-14 16:51:21 -0700487 mCamera->setPreviewCallbackFlags(CAMERA_FRAME_CALLBACK_FLAG_CAMERA);
James Donge00cab72011-02-17 16:38:06 -0800488 mManualCameraCallbackSet = true;
489 }
490 break;
491 }
492 case CAMERA_MSG_RAW_IMAGE: {
493 jbyteArray callbackBuffer = (jbyteArray)env->NewGlobalRef(cbb);
494 mRawImageCallbackBuffers.push(callbackBuffer);
495 break;
496 }
497 default: {
498 jniThrowException(env,
499 "java/lang/IllegalArgumentException",
500 "Unsupported message type");
501 return;
502 }
Andrew Harp94927df2009-10-20 01:47:05 -0400503 }
504 } else {
Steve Block3762c312012-01-06 19:20:56 +0000505 ALOGE("Null byte array!");
Andrew Harp94927df2009-10-20 01:47:05 -0400506 }
507}
508
509void JNICameraContext::clearCallbackBuffers_l(JNIEnv *env)
510{
James Donge00cab72011-02-17 16:38:06 -0800511 clearCallbackBuffers_l(env, &mCallbackBuffers);
512 clearCallbackBuffers_l(env, &mRawImageCallbackBuffers);
513}
514
515void JNICameraContext::clearCallbackBuffers_l(JNIEnv *env, Vector<jbyteArray> *buffers) {
Dan Albert46d84442014-11-18 16:07:51 -0800516 ALOGV("Clearing callback buffers, %zu remained", buffers->size());
James Donge00cab72011-02-17 16:38:06 -0800517 while (!buffers->isEmpty()) {
518 env->DeleteGlobalRef(buffers->top());
519 buffers->pop();
Andrew Harp94927df2009-10-20 01:47:05 -0400520 }
521}
522
Chih-Chung Change25cc652010-05-06 16:36:58 +0800523static jint android_hardware_Camera_getNumberOfCameras(JNIEnv *env, jobject thiz)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800524{
Chih-Chung Change25cc652010-05-06 16:36:58 +0800525 return Camera::getNumberOfCameras();
526}
527
Chih-Chung Changb8bb78f2010-06-10 13:32:16 +0800528static void android_hardware_Camera_getCameraInfo(JNIEnv *env, jobject thiz,
529 jint cameraId, jobject info_obj)
530{
531 CameraInfo cameraInfo;
Eino-Ville Talvala57176122015-08-14 13:11:16 -0700532 if (cameraId >= Camera::getNumberOfCameras() || cameraId < 0) {
533 ALOGE("%s: Unknown camera ID %d", __FUNCTION__, cameraId);
534 jniThrowRuntimeException(env, "Unknown camera ID");
535 return;
536 }
537
Chih-Chung Changb8bb78f2010-06-10 13:32:16 +0800538 status_t rc = Camera::getCameraInfo(cameraId, &cameraInfo);
539 if (rc != NO_ERROR) {
Elliott Hughes69a017b2011-04-08 14:10:28 -0700540 jniThrowRuntimeException(env, "Fail to get camera info");
Chih-Chung Changb8bb78f2010-06-10 13:32:16 +0800541 return;
542 }
543 env->SetIntField(info_obj, fields.facing, cameraInfo.facing);
544 env->SetIntField(info_obj, fields.orientation, cameraInfo.orientation);
Eino-Ville Talvalaf7c6c5a2012-09-19 11:46:11 -0700545
546 char value[PROPERTY_VALUE_MAX];
547 property_get("ro.camera.sound.forced", value, "0");
548 jboolean canDisableShutterSound = (strncmp(value, "0", 2) == 0);
549 env->SetBooleanField(info_obj, fields.canDisableShutterSound,
550 canDisableShutterSound);
Chih-Chung Changb8bb78f2010-06-10 13:32:16 +0800551}
552
Chih-Chung Change25cc652010-05-06 16:36:58 +0800553// connect to camera service
Ruben Brunkfeb50af2014-05-09 19:58:49 -0700554static jint android_hardware_Camera_native_setup(JNIEnv *env, jobject thiz,
Zhijun He4c913802014-06-16 16:42:35 -0700555 jobject weak_this, jint cameraId, jint halVersion, jstring clientPackageName)
Chih-Chung Change25cc652010-05-06 16:36:58 +0800556{
Eino-Ville Talvala788717c2013-02-15 18:30:15 -0800557 // Convert jstring to String16
Dan Albert66987492014-11-20 11:41:21 -0800558 const char16_t *rawClientName = reinterpret_cast<const char16_t*>(
559 env->GetStringChars(clientPackageName, NULL));
Eino-Ville Talvala788717c2013-02-15 18:30:15 -0800560 jsize rawClientNameLen = env->GetStringLength(clientPackageName);
561 String16 clientName(rawClientName, rawClientNameLen);
Dan Albert66987492014-11-20 11:41:21 -0800562 env->ReleaseStringChars(clientPackageName,
563 reinterpret_cast<const jchar*>(rawClientName));
Eino-Ville Talvala788717c2013-02-15 18:30:15 -0800564
Zhijun He4c913802014-06-16 16:42:35 -0700565 sp<Camera> camera;
Igor Murashkina1d66272014-06-20 11:22:11 -0700566 if (halVersion == CAMERA_HAL_API_VERSION_NORMAL_CONNECT) {
567 // Default path: hal version is don't care, do normal camera connect.
Zhijun He4c913802014-06-16 16:42:35 -0700568 camera = Camera::connect(cameraId, clientName,
Chien-Yu Chen225257a2015-12-18 14:20:46 -0800569 Camera::USE_CALLING_UID, Camera::USE_CALLING_PID);
Zhijun He4c913802014-06-16 16:42:35 -0700570 } else {
571 jint status = Camera::connectLegacy(cameraId, halVersion, clientName,
572 Camera::USE_CALLING_UID, camera);
573 if (status != NO_ERROR) {
574 return status;
575 }
576 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800577
578 if (camera == NULL) {
Ruben Brunkfeb50af2014-05-09 19:58:49 -0700579 return -EACCES;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800580 }
581
582 // make sure camera hardware is alive
583 if (camera->getStatus() != NO_ERROR) {
Ruben Brunkfeb50af2014-05-09 19:58:49 -0700584 return NO_INIT;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800585 }
586
587 jclass clazz = env->GetObjectClass(thiz);
588 if (clazz == NULL) {
Ruben Brunkfeb50af2014-05-09 19:58:49 -0700589 // This should never happen
Elliott Hughes69a017b2011-04-08 14:10:28 -0700590 jniThrowRuntimeException(env, "Can't find android/hardware/Camera");
Ruben Brunkfeb50af2014-05-09 19:58:49 -0700591 return INVALID_OPERATION;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800592 }
593
594 // We use a weak reference so the Camera object can be garbage collected.
595 // The reference is only used as a proxy for callbacks.
Dave Sparks5e271152009-06-23 17:30:11 -0700596 sp<JNICameraContext> context = new JNICameraContext(env, weak_this, clazz, camera);
Mathias Agopianb1d90c82013-03-06 17:45:42 -0800597 context->incStrong((void*)android_hardware_Camera_native_setup);
Dave Sparks5e271152009-06-23 17:30:11 -0700598 camera->setListener(context);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800599
600 // save context in opaque field
Ashok Bhat4838e332014-01-03 14:37:19 +0000601 env->SetLongField(thiz, fields.context, (jlong)context.get());
Eino-Ville Talvala6c91e2c2016-03-25 11:54:39 -0700602
603 // Update default display orientation in case the sensor is reverse-landscape
604 CameraInfo cameraInfo;
605 status_t rc = Camera::getCameraInfo(cameraId, &cameraInfo);
606 if (rc != NO_ERROR) {
607 return rc;
608 }
609 int defaultOrientation = 0;
610 switch (cameraInfo.orientation) {
611 case 0:
612 break;
613 case 90:
614 if (cameraInfo.facing == CAMERA_FACING_FRONT) {
615 defaultOrientation = 180;
616 }
617 break;
618 case 180:
619 defaultOrientation = 180;
620 break;
621 case 270:
622 if (cameraInfo.facing != CAMERA_FACING_FRONT) {
623 defaultOrientation = 180;
624 }
625 break;
626 default:
627 ALOGE("Unexpected camera orientation %d!", cameraInfo.orientation);
628 break;
629 }
630 if (defaultOrientation != 0) {
631 ALOGV("Setting default display orientation to %d", defaultOrientation);
632 rc = camera->sendCommand(CAMERA_CMD_SET_DISPLAY_ORIENTATION,
633 defaultOrientation, 0);
634 if (rc != NO_ERROR) {
635 ALOGE("Unable to update default orientation: %s (%d)",
636 strerror(-rc), rc);
637 return rc;
638 }
639 }
640
Ruben Brunkfeb50af2014-05-09 19:58:49 -0700641 return NO_ERROR;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800642}
643
644// disconnect from camera service
645// It's okay to call this when the native camera context is already null.
646// This handles the case where the user has called release() and the
647// finalizer is invoked later.
648static void android_hardware_Camera_release(JNIEnv *env, jobject thiz)
649{
Steve Block71f2cf12011-10-20 11:56:00 +0100650 ALOGV("release camera");
Dave Sparks5e271152009-06-23 17:30:11 -0700651 JNICameraContext* context = NULL;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800652 sp<Camera> camera;
653 {
654 Mutex::Autolock _l(sLock);
Ashok Bhat4838e332014-01-03 14:37:19 +0000655 context = reinterpret_cast<JNICameraContext*>(env->GetLongField(thiz, fields.context));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800656
657 // Make sure we do not attempt to callback on a deleted Java object.
Ashok Bhat4838e332014-01-03 14:37:19 +0000658 env->SetLongField(thiz, fields.context, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800659 }
660
661 // clean up if release has not been called before
662 if (context != NULL) {
Dave Sparks5e271152009-06-23 17:30:11 -0700663 camera = context->getCamera();
664 context->release();
Steve Block71f2cf12011-10-20 11:56:00 +0100665 ALOGV("native_release: context=%p camera=%p", context, camera.get());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800666
667 // clear callbacks
668 if (camera != NULL) {
Iliyan Malchev9c7ac0d2011-04-14 16:51:21 -0700669 camera->setPreviewCallbackFlags(CAMERA_FRAME_CALLBACK_FLAG_NOOP);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800670 camera->disconnect();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800671 }
672
673 // remove context to prevent further Java access
Mathias Agopianb1d90c82013-03-06 17:45:42 -0800674 context->decStrong((void*)android_hardware_Camera_native_setup);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800675 }
676}
677
Ruben Brunkfeb50af2014-05-09 19:58:49 -0700678static void android_hardware_Camera_setPreviewSurface(JNIEnv *env, jobject thiz, jobject jSurface)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800679{
Ruben Brunkfeb50af2014-05-09 19:58:49 -0700680 ALOGV("setPreviewSurface");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800681 sp<Camera> camera = get_native_camera(env, thiz, NULL);
682 if (camera == 0) return;
683
Mathias Agopian4a05f432013-03-12 18:43:34 -0700684 sp<IGraphicBufferProducer> gbp;
Jesse Hallaa70f222013-02-21 15:06:27 -0800685 sp<Surface> surface;
686 if (jSurface) {
687 surface = android_view_Surface_getSurface(env, jSurface);
Mathias Agopian4a05f432013-03-12 18:43:34 -0700688 if (surface != NULL) {
689 gbp = surface->getIGraphicBufferProducer();
690 }
Jesse Hallaa70f222013-02-21 15:06:27 -0800691 }
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800692
Eino-Ville Talvala7b297792013-08-21 14:39:22 -0700693 if (camera->setPreviewTarget(gbp) != NO_ERROR) {
Mathias Agopian4a05f432013-03-12 18:43:34 -0700694 jniThrowException(env, "java/io/IOException", "setPreviewTexture failed");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800695 }
696}
697
Jamie Gennisfd6f39e2010-12-20 12:15:00 -0800698static void android_hardware_Camera_setPreviewTexture(JNIEnv *env,
699 jobject thiz, jobject jSurfaceTexture)
700{
Steve Block71f2cf12011-10-20 11:56:00 +0100701 ALOGV("setPreviewTexture");
Jamie Gennisfd6f39e2010-12-20 12:15:00 -0800702 sp<Camera> camera = get_native_camera(env, thiz, NULL);
703 if (camera == 0) return;
704
Mathias Agopian52a9a102013-08-02 01:38:38 -0700705 sp<IGraphicBufferProducer> producer = NULL;
Jamie Gennisfd6f39e2010-12-20 12:15:00 -0800706 if (jSurfaceTexture != NULL) {
Mathias Agopian52a9a102013-08-02 01:38:38 -0700707 producer = SurfaceTexture_getProducer(env, jSurfaceTexture);
708 if (producer == NULL) {
Daniel Lam2e76c992012-02-23 14:35:13 -0800709 jniThrowException(env, "java/lang/IllegalArgumentException",
710 "SurfaceTexture already released in setPreviewTexture");
711 return;
712 }
713
Jamie Gennisfd6f39e2010-12-20 12:15:00 -0800714 }
Daniel Lam2e76c992012-02-23 14:35:13 -0800715
Eino-Ville Talvala7b297792013-08-21 14:39:22 -0700716 if (camera->setPreviewTarget(producer) != NO_ERROR) {
Jamie Gennisfd6f39e2010-12-20 12:15:00 -0800717 jniThrowException(env, "java/io/IOException",
718 "setPreviewTexture failed");
719 }
720}
721
Eino-Ville Talvala7005b672013-04-02 15:46:38 -0700722static void android_hardware_Camera_setPreviewCallbackSurface(JNIEnv *env,
723 jobject thiz, jobject jSurface)
724{
725 ALOGV("setPreviewCallbackSurface");
726 JNICameraContext* context;
727 sp<Camera> camera = get_native_camera(env, thiz, &context);
728 if (camera == 0) return;
729
730 sp<IGraphicBufferProducer> gbp;
731 sp<Surface> surface;
732 if (jSurface) {
733 surface = android_view_Surface_getSurface(env, jSurface);
734 if (surface != NULL) {
735 gbp = surface->getIGraphicBufferProducer();
736 }
737 }
738 // Clear out normal preview callbacks
739 context->setCallbackMode(env, false, false);
740 // Then set up callback surface
741 if (camera->setPreviewCallbackTarget(gbp) != NO_ERROR) {
742 jniThrowException(env, "java/io/IOException", "setPreviewCallbackTarget failed");
743 }
744}
745
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800746static void android_hardware_Camera_startPreview(JNIEnv *env, jobject thiz)
747{
Steve Block71f2cf12011-10-20 11:56:00 +0100748 ALOGV("startPreview");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800749 sp<Camera> camera = get_native_camera(env, thiz, NULL);
750 if (camera == 0) return;
751
752 if (camera->startPreview() != NO_ERROR) {
Elliott Hughes69a017b2011-04-08 14:10:28 -0700753 jniThrowRuntimeException(env, "startPreview failed");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800754 return;
755 }
756}
757
758static void android_hardware_Camera_stopPreview(JNIEnv *env, jobject thiz)
759{
Steve Block71f2cf12011-10-20 11:56:00 +0100760 ALOGV("stopPreview");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800761 sp<Camera> c = get_native_camera(env, thiz, NULL);
762 if (c == 0) return;
763
764 c->stopPreview();
765}
766
Ashok Bhat4838e332014-01-03 14:37:19 +0000767static jboolean android_hardware_Camera_previewEnabled(JNIEnv *env, jobject thiz)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800768{
Steve Block71f2cf12011-10-20 11:56:00 +0100769 ALOGV("previewEnabled");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800770 sp<Camera> c = get_native_camera(env, thiz, NULL);
Ashok Bhat4838e332014-01-03 14:37:19 +0000771 if (c == 0) return JNI_FALSE;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800772
Ashok Bhat4838e332014-01-03 14:37:19 +0000773 return c->previewEnabled() ? JNI_TRUE : JNI_FALSE;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800774}
775
Andrew Harp94927df2009-10-20 01:47:05 -0400776static void android_hardware_Camera_setHasPreviewCallback(JNIEnv *env, jobject thiz, jboolean installed, jboolean manualBuffer)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800777{
Steve Block71f2cf12011-10-20 11:56:00 +0100778 ALOGV("setHasPreviewCallback: installed:%d, manualBuffer:%d", (int)installed, (int)manualBuffer);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800779 // Important: Only install preview_callback if the Java code has called
780 // setPreviewCallback() with a non-null value, otherwise we'd pay to memcpy
781 // each preview frame for nothing.
Dave Sparks5e271152009-06-23 17:30:11 -0700782 JNICameraContext* context;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800783 sp<Camera> camera = get_native_camera(env, thiz, &context);
784 if (camera == 0) return;
785
Andrew Harp94927df2009-10-20 01:47:05 -0400786 // setCallbackMode will take care of setting the context flags and calling
787 // camera->setPreviewCallbackFlags within a mutex for us.
788 context->setCallbackMode(env, installed, manualBuffer);
789}
790
Ashok Bhat4838e332014-01-03 14:37:19 +0000791static void android_hardware_Camera_addCallbackBuffer(JNIEnv *env, jobject thiz, jbyteArray bytes, jint msgType) {
Steve Block71f2cf12011-10-20 11:56:00 +0100792 ALOGV("addCallbackBuffer: 0x%x", msgType);
Andrew Harp94927df2009-10-20 01:47:05 -0400793
Ashok Bhat4838e332014-01-03 14:37:19 +0000794 JNICameraContext* context = reinterpret_cast<JNICameraContext*>(env->GetLongField(thiz, fields.context));
Andrew Harp94927df2009-10-20 01:47:05 -0400795
796 if (context != NULL) {
James Donge00cab72011-02-17 16:38:06 -0800797 context->addCallbackBuffer(env, bytes, msgType);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800798 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800799}
800
801static void android_hardware_Camera_autoFocus(JNIEnv *env, jobject thiz)
802{
Steve Block71f2cf12011-10-20 11:56:00 +0100803 ALOGV("autoFocus");
Dave Sparks5e271152009-06-23 17:30:11 -0700804 JNICameraContext* context;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800805 sp<Camera> c = get_native_camera(env, thiz, &context);
806 if (c == 0) return;
807
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800808 if (c->autoFocus() != NO_ERROR) {
Elliott Hughes69a017b2011-04-08 14:10:28 -0700809 jniThrowRuntimeException(env, "autoFocus failed");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800810 }
811}
812
Chih-Chung Chang244f8c22009-09-15 14:51:56 +0800813static void android_hardware_Camera_cancelAutoFocus(JNIEnv *env, jobject thiz)
814{
Steve Block71f2cf12011-10-20 11:56:00 +0100815 ALOGV("cancelAutoFocus");
Chih-Chung Chang244f8c22009-09-15 14:51:56 +0800816 JNICameraContext* context;
817 sp<Camera> c = get_native_camera(env, thiz, &context);
818 if (c == 0) return;
819
820 if (c->cancelAutoFocus() != NO_ERROR) {
Elliott Hughes69a017b2011-04-08 14:10:28 -0700821 jniThrowRuntimeException(env, "cancelAutoFocus failed");
Chih-Chung Chang244f8c22009-09-15 14:51:56 +0800822 }
823}
824
Ashok Bhat4838e332014-01-03 14:37:19 +0000825static void android_hardware_Camera_takePicture(JNIEnv *env, jobject thiz, jint msgType)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800826{
Steve Block71f2cf12011-10-20 11:56:00 +0100827 ALOGV("takePicture");
Dave Sparks5e271152009-06-23 17:30:11 -0700828 JNICameraContext* context;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800829 sp<Camera> camera = get_native_camera(env, thiz, &context);
830 if (camera == 0) return;
831
James Donge00cab72011-02-17 16:38:06 -0800832 /*
833 * When CAMERA_MSG_RAW_IMAGE is requested, if the raw image callback
834 * buffer is available, CAMERA_MSG_RAW_IMAGE is enabled to get the
835 * notification _and_ the data; otherwise, CAMERA_MSG_RAW_IMAGE_NOTIFY
836 * is enabled to receive the callback notification but no data.
837 *
838 * Note that CAMERA_MSG_RAW_IMAGE_NOTIFY is not exposed to the
839 * Java application.
840 */
841 if (msgType & CAMERA_MSG_RAW_IMAGE) {
Steve Block71f2cf12011-10-20 11:56:00 +0100842 ALOGV("Enable raw image callback buffer");
James Donge00cab72011-02-17 16:38:06 -0800843 if (!context->isRawImageCallbackBufferAvailable()) {
Steve Block71f2cf12011-10-20 11:56:00 +0100844 ALOGV("Enable raw image notification, since no callback buffer exists");
James Donge00cab72011-02-17 16:38:06 -0800845 msgType &= ~CAMERA_MSG_RAW_IMAGE;
846 msgType |= CAMERA_MSG_RAW_IMAGE_NOTIFY;
847 }
848 }
849
850 if (camera->takePicture(msgType) != NO_ERROR) {
Elliott Hughes69a017b2011-04-08 14:10:28 -0700851 jniThrowRuntimeException(env, "takePicture failed");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800852 return;
853 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800854}
855
856static void android_hardware_Camera_setParameters(JNIEnv *env, jobject thiz, jstring params)
857{
Steve Block71f2cf12011-10-20 11:56:00 +0100858 ALOGV("setParameters");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800859 sp<Camera> camera = get_native_camera(env, thiz, NULL);
860 if (camera == 0) return;
861
862 const jchar* str = env->GetStringCritical(params, 0);
863 String8 params8;
864 if (params) {
Dan Albert66987492014-11-20 11:41:21 -0800865 params8 = String8(reinterpret_cast<const char16_t*>(str),
866 env->GetStringLength(params));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800867 env->ReleaseStringCritical(params, str);
868 }
869 if (camera->setParameters(params8) != NO_ERROR) {
Elliott Hughes69a017b2011-04-08 14:10:28 -0700870 jniThrowRuntimeException(env, "setParameters failed");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800871 return;
872 }
873}
874
875static jstring android_hardware_Camera_getParameters(JNIEnv *env, jobject thiz)
876{
Steve Block71f2cf12011-10-20 11:56:00 +0100877 ALOGV("getParameters");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800878 sp<Camera> camera = get_native_camera(env, thiz, NULL);
879 if (camera == 0) return 0;
880
Wu-cheng Lia1c41e12012-02-23 19:01:00 -0800881 String8 params8 = camera->getParameters();
882 if (params8.isEmpty()) {
883 jniThrowRuntimeException(env, "getParameters failed (empty parameters)");
884 return 0;
885 }
886 return env->NewStringUTF(params8.string());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800887}
888
889static void android_hardware_Camera_reconnect(JNIEnv *env, jobject thiz)
890{
Steve Block71f2cf12011-10-20 11:56:00 +0100891 ALOGV("reconnect");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800892 sp<Camera> camera = get_native_camera(env, thiz, NULL);
893 if (camera == 0) return;
894
895 if (camera->reconnect() != NO_ERROR) {
896 jniThrowException(env, "java/io/IOException", "reconnect failed");
897 return;
898 }
899}
900
Wu-cheng Liffe1cf22009-09-10 16:49:17 +0800901static void android_hardware_Camera_lock(JNIEnv *env, jobject thiz)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800902{
Steve Block71f2cf12011-10-20 11:56:00 +0100903 ALOGV("lock");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800904 sp<Camera> camera = get_native_camera(env, thiz, NULL);
Wu-cheng Liffe1cf22009-09-10 16:49:17 +0800905 if (camera == 0) return;
906
907 if (camera->lock() != NO_ERROR) {
Elliott Hughes69a017b2011-04-08 14:10:28 -0700908 jniThrowRuntimeException(env, "lock failed");
Wu-cheng Liffe1cf22009-09-10 16:49:17 +0800909 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800910}
911
Wu-cheng Liffe1cf22009-09-10 16:49:17 +0800912static void android_hardware_Camera_unlock(JNIEnv *env, jobject thiz)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800913{
Steve Block71f2cf12011-10-20 11:56:00 +0100914 ALOGV("unlock");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800915 sp<Camera> camera = get_native_camera(env, thiz, NULL);
Wu-cheng Liffe1cf22009-09-10 16:49:17 +0800916 if (camera == 0) return;
917
918 if (camera->unlock() != NO_ERROR) {
Elliott Hughes69a017b2011-04-08 14:10:28 -0700919 jniThrowRuntimeException(env, "unlock failed");
Wu-cheng Liffe1cf22009-09-10 16:49:17 +0800920 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800921}
922
Wu-cheng Li36f68b82009-09-28 16:14:58 -0700923static void android_hardware_Camera_startSmoothZoom(JNIEnv *env, jobject thiz, jint value)
924{
Steve Block71f2cf12011-10-20 11:56:00 +0100925 ALOGV("startSmoothZoom");
Wu-cheng Li36f68b82009-09-28 16:14:58 -0700926 sp<Camera> camera = get_native_camera(env, thiz, NULL);
927 if (camera == 0) return;
928
Wu-cheng Li0ca25192010-03-29 16:21:12 +0800929 status_t rc = camera->sendCommand(CAMERA_CMD_START_SMOOTH_ZOOM, value, 0);
930 if (rc == BAD_VALUE) {
931 char msg[64];
932 sprintf(msg, "invalid zoom value=%d", value);
933 jniThrowException(env, "java/lang/IllegalArgumentException", msg);
934 } else if (rc != NO_ERROR) {
Elliott Hughes69a017b2011-04-08 14:10:28 -0700935 jniThrowRuntimeException(env, "start smooth zoom failed");
Wu-cheng Li36f68b82009-09-28 16:14:58 -0700936 }
937}
938
939static void android_hardware_Camera_stopSmoothZoom(JNIEnv *env, jobject thiz)
940{
Steve Block71f2cf12011-10-20 11:56:00 +0100941 ALOGV("stopSmoothZoom");
Wu-cheng Li36f68b82009-09-28 16:14:58 -0700942 sp<Camera> camera = get_native_camera(env, thiz, NULL);
943 if (camera == 0) return;
944
945 if (camera->sendCommand(CAMERA_CMD_STOP_SMOOTH_ZOOM, 0, 0) != NO_ERROR) {
Elliott Hughes69a017b2011-04-08 14:10:28 -0700946 jniThrowRuntimeException(env, "stop smooth zoom failed");
Wu-cheng Li36f68b82009-09-28 16:14:58 -0700947 }
948}
949
Chih-Chung Changd1d77062010-01-22 17:49:48 -0800950static void android_hardware_Camera_setDisplayOrientation(JNIEnv *env, jobject thiz,
951 jint value)
952{
Steve Block71f2cf12011-10-20 11:56:00 +0100953 ALOGV("setDisplayOrientation");
Chih-Chung Changd1d77062010-01-22 17:49:48 -0800954 sp<Camera> camera = get_native_camera(env, thiz, NULL);
955 if (camera == 0) return;
956
957 if (camera->sendCommand(CAMERA_CMD_SET_DISPLAY_ORIENTATION, value, 0) != NO_ERROR) {
Elliott Hughes69a017b2011-04-08 14:10:28 -0700958 jniThrowRuntimeException(env, "set display orientation failed");
Chih-Chung Changd1d77062010-01-22 17:49:48 -0800959 }
960}
961
Eino-Ville Talvala69fe5272012-09-07 12:26:40 -0700962static jboolean android_hardware_Camera_enableShutterSound(JNIEnv *env, jobject thiz,
963 jboolean enabled)
964{
965 ALOGV("enableShutterSound");
966 sp<Camera> camera = get_native_camera(env, thiz, NULL);
967 if (camera == 0) return JNI_FALSE;
968
969 int32_t value = (enabled == JNI_TRUE) ? 1 : 0;
970 status_t rc = camera->sendCommand(CAMERA_CMD_ENABLE_SHUTTER_SOUND, value, 0);
971 if (rc == NO_ERROR) {
972 return JNI_TRUE;
973 } else if (rc == PERMISSION_DENIED) {
974 return JNI_FALSE;
975 } else {
976 jniThrowRuntimeException(env, "enable shutter sound failed");
977 return JNI_FALSE;
978 }
979}
980
Wu-cheng Li4c2292e2011-07-22 02:37:11 +0800981static void android_hardware_Camera_startFaceDetection(JNIEnv *env, jobject thiz,
Wu-cheng Libb1e2752011-07-30 05:00:37 +0800982 jint type)
Wu-cheng Li4c2292e2011-07-22 02:37:11 +0800983{
Steve Block71f2cf12011-10-20 11:56:00 +0100984 ALOGV("startFaceDetection");
Wu-cheng Li4c2292e2011-07-22 02:37:11 +0800985 JNICameraContext* context;
986 sp<Camera> camera = get_native_camera(env, thiz, &context);
987 if (camera == 0) return;
988
989 status_t rc = camera->sendCommand(CAMERA_CMD_START_FACE_DETECTION, type, 0);
990 if (rc == BAD_VALUE) {
991 char msg[64];
992 snprintf(msg, sizeof(msg), "invalid face detection type=%d", type);
993 jniThrowException(env, "java/lang/IllegalArgumentException", msg);
994 } else if (rc != NO_ERROR) {
995 jniThrowRuntimeException(env, "start face detection failed");
996 }
997}
998
999static void android_hardware_Camera_stopFaceDetection(JNIEnv *env, jobject thiz)
1000{
Steve Block71f2cf12011-10-20 11:56:00 +01001001 ALOGV("stopFaceDetection");
Wu-cheng Li4c2292e2011-07-22 02:37:11 +08001002 sp<Camera> camera = get_native_camera(env, thiz, NULL);
1003 if (camera == 0) return;
1004
1005 if (camera->sendCommand(CAMERA_CMD_STOP_FACE_DETECTION, 0, 0) != NO_ERROR) {
1006 jniThrowRuntimeException(env, "stop face detection failed");
1007 }
1008}
1009
Wu-cheng Li9d062cf2011-11-14 20:30:14 +08001010static void android_hardware_Camera_enableFocusMoveCallback(JNIEnv *env, jobject thiz, jint enable)
1011{
Wu-cheng Li02097522011-11-30 11:06:04 +08001012 ALOGV("enableFocusMoveCallback");
Wu-cheng Li9d062cf2011-11-14 20:30:14 +08001013 sp<Camera> camera = get_native_camera(env, thiz, NULL);
1014 if (camera == 0) return;
1015
1016 if (camera->sendCommand(CAMERA_CMD_ENABLE_FOCUS_MOVE_MSG, enable, 0) != NO_ERROR) {
1017 jniThrowRuntimeException(env, "enable focus move callback failed");
1018 }
1019}
1020
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001021//-------------------------------------------------
1022
Daniel Micay76f6a862015-09-19 17:31:01 -04001023static const JNINativeMethod camMethods[] = {
Chih-Chung Change25cc652010-05-06 16:36:58 +08001024 { "getNumberOfCameras",
1025 "()I",
1026 (void *)android_hardware_Camera_getNumberOfCameras },
Eino-Ville Talvala4f8e5ce2012-10-08 18:16:35 -07001027 { "_getCameraInfo",
Chih-Chung Changb8bb78f2010-06-10 13:32:16 +08001028 "(ILandroid/hardware/Camera$CameraInfo;)V",
1029 (void*)android_hardware_Camera_getCameraInfo },
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001030 { "native_setup",
Zhijun He4c913802014-06-16 16:42:35 -07001031 "(Ljava/lang/Object;IILjava/lang/String;)I",
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001032 (void*)android_hardware_Camera_native_setup },
1033 { "native_release",
1034 "()V",
1035 (void*)android_hardware_Camera_release },
Ruben Brunkfeb50af2014-05-09 19:58:49 -07001036 { "setPreviewSurface",
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001037 "(Landroid/view/Surface;)V",
Ruben Brunkfeb50af2014-05-09 19:58:49 -07001038 (void *)android_hardware_Camera_setPreviewSurface },
Jamie Gennisfd6f39e2010-12-20 12:15:00 -08001039 { "setPreviewTexture",
1040 "(Landroid/graphics/SurfaceTexture;)V",
1041 (void *)android_hardware_Camera_setPreviewTexture },
Eino-Ville Talvala7005b672013-04-02 15:46:38 -07001042 { "setPreviewCallbackSurface",
1043 "(Landroid/view/Surface;)V",
1044 (void *)android_hardware_Camera_setPreviewCallbackSurface },
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001045 { "startPreview",
1046 "()V",
1047 (void *)android_hardware_Camera_startPreview },
Wu-cheng Li4c2292e2011-07-22 02:37:11 +08001048 { "_stopPreview",
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001049 "()V",
1050 (void *)android_hardware_Camera_stopPreview },
1051 { "previewEnabled",
1052 "()Z",
1053 (void *)android_hardware_Camera_previewEnabled },
1054 { "setHasPreviewCallback",
1055 "(ZZ)V",
1056 (void *)android_hardware_Camera_setHasPreviewCallback },
James Donge00cab72011-02-17 16:38:06 -08001057 { "_addCallbackBuffer",
1058 "([BI)V",
Andrew Harp94927df2009-10-20 01:47:05 -04001059 (void *)android_hardware_Camera_addCallbackBuffer },
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001060 { "native_autoFocus",
1061 "()V",
1062 (void *)android_hardware_Camera_autoFocus },
Chih-Chung Chang244f8c22009-09-15 14:51:56 +08001063 { "native_cancelAutoFocus",
1064 "()V",
1065 (void *)android_hardware_Camera_cancelAutoFocus },
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001066 { "native_takePicture",
James Donge00cab72011-02-17 16:38:06 -08001067 "(I)V",
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001068 (void *)android_hardware_Camera_takePicture },
1069 { "native_setParameters",
1070 "(Ljava/lang/String;)V",
1071 (void *)android_hardware_Camera_setParameters },
1072 { "native_getParameters",
1073 "()Ljava/lang/String;",
1074 (void *)android_hardware_Camera_getParameters },
1075 { "reconnect",
1076 "()V",
1077 (void*)android_hardware_Camera_reconnect },
1078 { "lock",
Wu-cheng Liffe1cf22009-09-10 16:49:17 +08001079 "()V",
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001080 (void*)android_hardware_Camera_lock },
1081 { "unlock",
Wu-cheng Liffe1cf22009-09-10 16:49:17 +08001082 "()V",
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001083 (void*)android_hardware_Camera_unlock },
Wu-cheng Li36f68b82009-09-28 16:14:58 -07001084 { "startSmoothZoom",
1085 "(I)V",
1086 (void *)android_hardware_Camera_startSmoothZoom },
1087 { "stopSmoothZoom",
1088 "()V",
1089 (void *)android_hardware_Camera_stopSmoothZoom },
Chih-Chung Changd1d77062010-01-22 17:49:48 -08001090 { "setDisplayOrientation",
1091 "(I)V",
1092 (void *)android_hardware_Camera_setDisplayOrientation },
Eino-Ville Talvala4f8e5ce2012-10-08 18:16:35 -07001093 { "_enableShutterSound",
Eino-Ville Talvala69fe5272012-09-07 12:26:40 -07001094 "(Z)Z",
1095 (void *)android_hardware_Camera_enableShutterSound },
Wu-cheng Li4c2292e2011-07-22 02:37:11 +08001096 { "_startFaceDetection",
1097 "(I)V",
1098 (void *)android_hardware_Camera_startFaceDetection },
1099 { "_stopFaceDetection",
1100 "()V",
1101 (void *)android_hardware_Camera_stopFaceDetection},
Wu-cheng Li9d062cf2011-11-14 20:30:14 +08001102 { "enableFocusMoveCallback",
1103 "(I)V",
1104 (void *)android_hardware_Camera_enableFocusMoveCallback},
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001105};
1106
1107struct field {
1108 const char *class_name;
1109 const char *field_name;
1110 const char *field_type;
1111 jfieldID *jfield;
1112};
1113
Andreas Gampeed6b9df2014-11-20 22:02:20 -08001114static void find_fields(JNIEnv *env, field *fields, int count)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001115{
1116 for (int i = 0; i < count; i++) {
1117 field *f = &fields[i];
Andreas Gampeed6b9df2014-11-20 22:02:20 -08001118 jclass clazz = FindClassOrDie(env, f->class_name);
1119 jfieldID field = GetFieldIDOrDie(env, clazz, f->field_name, f->field_type);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001120 *(f->jfield) = field;
1121 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001122}
1123
1124// Get all the required offsets in java class and register native functions
1125int register_android_hardware_Camera(JNIEnv *env)
1126{
1127 field fields_to_find[] = {
Ashok Bhat4838e332014-01-03 14:37:19 +00001128 { "android/hardware/Camera", "mNativeContext", "J", &fields.context },
Wu-cheng Li3fd5fa42010-09-15 16:06:20 -07001129 { "android/hardware/Camera$CameraInfo", "facing", "I", &fields.facing },
1130 { "android/hardware/Camera$CameraInfo", "orientation", "I", &fields.orientation },
Eino-Ville Talvalaf7c6c5a2012-09-19 11:46:11 -07001131 { "android/hardware/Camera$CameraInfo", "canDisableShutterSound", "Z",
1132 &fields.canDisableShutterSound },
Wu-cheng Lif0d6a482011-07-28 05:30:59 +08001133 { "android/hardware/Camera$Face", "rect", "Landroid/graphics/Rect;", &fields.face_rect },
Igor Murashkin0601ab12014-09-18 15:17:20 -07001134 { "android/hardware/Camera$Face", "leftEye", "Landroid/graphics/Point;", &fields.face_left_eye},
1135 { "android/hardware/Camera$Face", "rightEye", "Landroid/graphics/Point;", &fields.face_right_eye},
1136 { "android/hardware/Camera$Face", "mouth", "Landroid/graphics/Point;", &fields.face_mouth},
Wu-cheng Lif0d6a482011-07-28 05:30:59 +08001137 { "android/hardware/Camera$Face", "score", "I", &fields.face_score },
Igor Murashkin0601ab12014-09-18 15:17:20 -07001138 { "android/hardware/Camera$Face", "id", "I", &fields.face_id},
Wu-cheng Li4c2292e2011-07-22 02:37:11 +08001139 { "android/graphics/Rect", "left", "I", &fields.rect_left },
1140 { "android/graphics/Rect", "top", "I", &fields.rect_top },
1141 { "android/graphics/Rect", "right", "I", &fields.rect_right },
1142 { "android/graphics/Rect", "bottom", "I", &fields.rect_bottom },
Igor Murashkin0601ab12014-09-18 15:17:20 -07001143 { "android/graphics/Point", "x", "I", &fields.point_x},
1144 { "android/graphics/Point", "y", "I", &fields.point_y},
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001145 };
1146
Andreas Gampeed6b9df2014-11-20 22:02:20 -08001147 find_fields(env, fields_to_find, NELEM(fields_to_find));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001148
Andreas Gampeed6b9df2014-11-20 22:02:20 -08001149 jclass clazz = FindClassOrDie(env, "android/hardware/Camera");
1150 fields.post_event = GetStaticMethodIDOrDie(env, clazz, "postEventFromNative",
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001151 "(Ljava/lang/Object;IIILjava/lang/Object;)V");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001152
Andreas Gampeed6b9df2014-11-20 22:02:20 -08001153 clazz = FindClassOrDie(env, "android/graphics/Rect");
1154 fields.rect_constructor = GetMethodIDOrDie(env, clazz, "<init>", "()V");
Wu-cheng Libb1e2752011-07-30 05:00:37 +08001155
Andreas Gampeed6b9df2014-11-20 22:02:20 -08001156 clazz = FindClassOrDie(env, "android/hardware/Camera$Face");
1157 fields.face_constructor = GetMethodIDOrDie(env, clazz, "<init>", "()V");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001158
Igor Murashkin0601ab12014-09-18 15:17:20 -07001159 clazz = env->FindClass("android/graphics/Point");
1160 fields.point_constructor = env->GetMethodID(clazz, "<init>", "()V");
1161 if (fields.point_constructor == NULL) {
1162 ALOGE("Can't find android/graphics/Point()");
1163 return -1;
1164 }
1165
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001166 // Register native functions
Andreas Gampeed6b9df2014-11-20 22:02:20 -08001167 return RegisterMethodsOrDie(env, "android/hardware/Camera", camMethods, NELEM(camMethods));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001168}