1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
|
/*
**
** Copyright 2008, The Android Open Source Project
**
** Licensed under the Apache License, Version 2.0 (the "License");
** you may not use this file except in compliance with the License.
** You may obtain a copy of the License at
**
** http://www.apache.org/licenses/LICENSE-2.0
**
** Unless required by applicable law or agreed to in writing, software
** distributed under the License is distributed on an "AS IS" BASIS,
** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
** See the License for the specific language governing permissions and
** limitations under the License.
*/
//#define LOG_NDEBUG 0
#define LOG_TAG "Camera-JNI"
#include <utils/Log.h>
#include "jni.h"
#include "JNIHelp.h"
#include "android_runtime/AndroidRuntime.h"
#include <ui/Surface.h>
#include <ui/Camera.h>
#include <utils/IMemory.h>
using namespace android;
enum CallbackMessageID {
kShutterCallback = 0,
kRawCallback = 1,
kJpegCallback = 2,
kPreviewCallback = 3,
kAutoFocusCallback = 4,
kErrorCallback = 5
};
enum CameraError {
kCameraErrorUnknown = 1,
kCameraErrorMediaServer = 100
};
struct fields_t {
jfieldID context;
jfieldID surface;
jmethodID post_event;
};
static fields_t fields;
static Mutex sLock;
struct camera_context_t {
jobject mCameraJObjectWeak; // weak reference to java object
jclass mCameraJClass; // strong reference to java class
sp<Camera> mCamera; // strong reference to native object
};
sp<Camera> get_native_camera(JNIEnv *env, jobject thiz, camera_context_t** pContext)
{
sp<Camera> camera;
Mutex::Autolock _l(sLock);
camera_context_t* context = reinterpret_cast<camera_context_t*>(env->GetIntField(thiz, fields.context));
if (context != NULL) {
camera = context->mCamera;
}
LOGV("get_native_camera: context=%p, camera=%p", context, camera.get());
if (camera == 0) {
jniThrowException(env, "java/lang/RuntimeException", "Method called after release()");
}
if (pContext != NULL) *pContext = context;
return camera;
}
static void err_callback(status_t err, void *cookie)
{
camera_context_t* context = reinterpret_cast<camera_context_t*>(cookie);
if ((context == NULL) || (context->mCamera == 0)) return;
LOGV("err_callback: context=%p, camera=%p", context, context->mCamera.get());
int error;
switch (err) {
case DEAD_OBJECT:
error = kCameraErrorMediaServer;
break;
default:
error = kCameraErrorUnknown;
break;
}
JNIEnv *env = AndroidRuntime::getJNIEnv();
if (env == NULL) {
LOGE("err_callback on dead VM");
return;
}
env->CallStaticVoidMethod(context->mCameraJClass, fields.post_event,
context->mCameraJObjectWeak, kErrorCallback, error, 0, NULL);
}
// connect to camera service
static void android_hardware_Camera_native_setup(JNIEnv *env, jobject thiz, jobject weak_this)
{
sp<Camera> camera = Camera::connect();
if (camera == NULL) {
jniThrowException(env, "java/lang/RuntimeException",
"Fail to connect to camera service");
return;
}
// make sure camera hardware is alive
if (camera->getStatus() != NO_ERROR) {
jniThrowException(env, "java/lang/RuntimeException", "Camera initialization failed");
return;
}
jclass clazz = env->GetObjectClass(thiz);
if (clazz == NULL) {
jniThrowException(env, "java/lang/RuntimeException", "Can't find android/hardware/Camera");
return;
}
// We use a weak reference so the Camera object can be garbage collected.
// The reference is only used as a proxy for callbacks.
camera_context_t* context = new camera_context_t;
context->mCameraJObjectWeak = env->NewGlobalRef(weak_this);
context->mCameraJClass = (jclass)env->NewGlobalRef(clazz);
context->mCamera = camera;
// save context in opaque field
env->SetIntField(thiz, fields.context, (int)context);
LOGV("native_setup: mCameraJObjectWeak=%x, camera_obj=%x, context=%p",
(int)context->mCameraJObjectWeak, (int)thiz, context);
// set error callback
camera->setErrorCallback(err_callback, context);
}
// disconnect from camera service
// It's okay to call this when the native camera context is already null.
// This handles the case where the user has called release() and the
// finalizer is invoked later.
static void android_hardware_Camera_release(JNIEnv *env, jobject thiz)
{
camera_context_t* context = NULL;
sp<Camera> camera;
{
Mutex::Autolock _l(sLock);
context = reinterpret_cast<camera_context_t*>(env->GetIntField(thiz, fields.context));
// Make sure we do not attempt to callback on a deleted Java object.
env->SetIntField(thiz, fields.context, 0);
}
// clean up if release has not been called before
if (context != NULL) {
camera = context->mCamera;
context->mCamera.clear();
LOGV("native_release: context=%p camera=%p", context, camera.get());
// clear callbacks
if (camera != NULL) {
camera->setPreviewCallback(NULL, NULL, FRAME_CALLBACK_FLAG_NOOP);
camera->setErrorCallback(NULL, NULL);
camera->disconnect();
env->DeleteGlobalRef(context->mCameraJObjectWeak);
env->DeleteGlobalRef(context->mCameraJClass);
}
// remove context to prevent further Java access
delete context;
}
}
static void android_hardware_Camera_setPreviewDisplay(JNIEnv *env, jobject thiz, jobject jSurface)
{
LOGV("setPreviewDisplay");
sp<Camera> camera = get_native_camera(env, thiz, NULL);
if (camera == 0) return;
sp<Surface> surface = reinterpret_cast<Surface*>(env->GetIntField(jSurface, fields.surface));
if (camera->setPreviewDisplay(surface) != NO_ERROR) {
jniThrowException(env, "java/io/IOException", "setPreviewDisplay failed");
}
}
static void preview_callback(const sp<IMemory>& mem, void *cookie)
{
LOGV("preview_callback");
JNIEnv *env = AndroidRuntime::getJNIEnv();
if (env == NULL) {
LOGE("preview_callback on dead VM");
return;
}
camera_context_t* context = reinterpret_cast<camera_context_t*>(cookie);
if ((context == NULL) || (context->mCamera == 0)) {
LOGW("context or camera is NULL in preview_callback");
return;
}
LOGV("native_release: context=%p camera=%p", context, context->mCamera.get());
int arg1 = 0, arg2 = 0;
jobject obj = NULL;
ssize_t offset;
size_t size;
sp<IMemoryHeap> heap = mem->getMemory(&offset, &size);
uint8_t *data = ((uint8_t *)heap->base()) + offset;
jbyteArray array = env->NewByteArray(size);
if (array == NULL) {
LOGE("Couldn't allocate byte array for YUV data");
env->ExceptionClear();
return;
}
jbyte *bytes = env->GetByteArrayElements(array, NULL);
memcpy(bytes, data, size);
env->ReleaseByteArrayElements(array, bytes, 0);
obj = array;
env->CallStaticVoidMethod(context->mCameraJClass, fields.post_event,
context->mCameraJObjectWeak, kPreviewCallback, arg1, arg2, obj);
env->DeleteLocalRef(array);
}
static void android_hardware_Camera_startPreview(JNIEnv *env, jobject thiz)
{
LOGV("startPreview");
sp<Camera> camera = get_native_camera(env, thiz, NULL);
if (camera == 0) return;
if (camera->startPreview() != NO_ERROR) {
jniThrowException(env, "java/lang/RuntimeException", "startPreview failed");
return;
}
}
static void android_hardware_Camera_stopPreview(JNIEnv *env, jobject thiz)
{
LOGV("stopPreview");
sp<Camera> c = get_native_camera(env, thiz, NULL);
if (c == 0) return;
c->stopPreview();
}
static bool android_hardware_Camera_previewEnabled(JNIEnv *env, jobject thiz)
{
LOGV("previewEnabled");
sp<Camera> c = get_native_camera(env, thiz, NULL);
if (c == 0) return false;
return c->previewEnabled();
}
static void android_hardware_Camera_setHasPreviewCallback(JNIEnv *env, jobject thiz, jboolean installed, jboolean oneshot)
{
// Important: Only install preview_callback if the Java code has called
// setPreviewCallback() with a non-null value, otherwise we'd pay to memcpy
// each preview frame for nothing.
camera_context_t* context;
sp<Camera> camera = get_native_camera(env, thiz, &context);
if (camera == 0) return;
int callback_flag;
if (installed) {
callback_flag = oneshot ? FRAME_CALLBACK_FLAG_BARCODE_SCANNER : FRAME_CALLBACK_FLAG_CAMERA;
} else {
callback_flag = FRAME_CALLBACK_FLAG_NOOP;
}
camera->setPreviewCallback(installed ? preview_callback : NULL, context, callback_flag);
}
static void autofocus_callback_impl(bool success, void *cookie)
{
LOGV("autoFocusCallback");
camera_context_t* context = reinterpret_cast<camera_context_t*>(cookie);
JNIEnv *env = AndroidRuntime::getJNIEnv();
if (env == NULL) {
LOGE("autofocus_callback on dead VM");
return;
}
env->CallStaticVoidMethod(context->mCameraJClass, fields.post_event,
context->mCameraJObjectWeak, kAutoFocusCallback, success, 0, NULL);
}
static void android_hardware_Camera_autoFocus(JNIEnv *env, jobject thiz)
{
LOGV("autoFocus");
camera_context_t* context;
sp<Camera> c = get_native_camera(env, thiz, &context);
if (c == 0) return;
c->setAutoFocusCallback(autofocus_callback_impl, context);
if (c->autoFocus() != NO_ERROR) {
jniThrowException(env, "java/lang/RuntimeException", "autoFocus failed");
}
}
static void jpeg_callback(const sp<IMemory>& mem, void *cookie)
{
LOGV("jpegCallback");
camera_context_t* context = reinterpret_cast<camera_context_t*>(cookie);
JNIEnv *env = AndroidRuntime::getJNIEnv();
if (env == NULL) {
LOGE("jpeg`_callback on dead VM");
return;
}
int arg1 = 0, arg2 = 0;
jobject obj = NULL;
if (mem == NULL) {
env->CallStaticVoidMethod(context->mCameraJClass, fields.post_event,
context->mCameraJObjectWeak, kJpegCallback, arg1, arg2, NULL);
return;
}
ssize_t offset;
size_t size;
sp<IMemoryHeap> heap = mem->getMemory(&offset, &size);
LOGV("jpeg_callback: mem off=%d, size=%d", offset, size);
uint8_t *heap_base = (uint8_t *)heap->base();
if (heap_base == NULL) {
LOGE("YUV heap is NULL");
return;
}
uint8_t *data = heap_base + offset;
jbyteArray array = env->NewByteArray(size);
if (array == NULL) {
LOGE("Couldn't allocate byte array for JPEG data");
env->ExceptionClear();
return;
}
jbyte *bytes = env->GetByteArrayElements(array, NULL);
memcpy(bytes, data, size);
env->ReleaseByteArrayElements(array, bytes, 0);
obj = array;
env->CallStaticVoidMethod(context->mCameraJClass, fields.post_event,
context->mCameraJObjectWeak, kJpegCallback, arg1, arg2, obj);
env->DeleteLocalRef(array);
}
static void shutter_callback_impl(void *cookie)
{
LOGV("shutterCallback");
camera_context_t* context = reinterpret_cast<camera_context_t*>(cookie);
JNIEnv *env = AndroidRuntime::getJNIEnv();
if (env == NULL) {
LOGE("shutter_callback on dead VM");
return;
}
env->CallStaticVoidMethod(context->mCameraJClass, fields.post_event,
context->mCameraJObjectWeak, kShutterCallback, 0, 0, NULL);
}
static void raw_callback(const sp<IMemory>& mem __attribute__((unused)),
void *cookie)
{
LOGV("rawCallback");
camera_context_t* context = reinterpret_cast<camera_context_t*>(cookie);
JNIEnv *env = AndroidRuntime::getJNIEnv();
if (env == NULL) {
LOGE("raw_callback on dead VM");
return;
}
env->CallStaticVoidMethod(context->mCameraJClass, fields.post_event,
context->mCameraJObjectWeak, kRawCallback, 0, 0, NULL);
}
static void android_hardware_Camera_takePicture(JNIEnv *env, jobject thiz)
{
LOGV("takePicture");
camera_context_t* context;
sp<Camera> camera = get_native_camera(env, thiz, &context);
if (camera == 0) return;
camera->setShutterCallback(shutter_callback_impl, context);
camera->setRawCallback(raw_callback, context);
camera->setJpegCallback(jpeg_callback, context);
if (camera->takePicture() != NO_ERROR) {
jniThrowException(env, "java/lang/RuntimeException", "takePicture failed");
return;
}
return;
}
static void android_hardware_Camera_setParameters(JNIEnv *env, jobject thiz, jstring params)
{
LOGV("setParameters");
sp<Camera> camera = get_native_camera(env, thiz, NULL);
if (camera == 0) return;
const jchar* str = env->GetStringCritical(params, 0);
String8 params8;
if (params) {
params8 = String8(str, env->GetStringLength(params));
env->ReleaseStringCritical(params, str);
}
if (camera->setParameters(params8) != NO_ERROR) {
jniThrowException(env, "java/lang/RuntimeException", "setParameters failed");
return;
}
}
static jstring android_hardware_Camera_getParameters(JNIEnv *env, jobject thiz)
{
LOGV("getParameters");
sp<Camera> camera = get_native_camera(env, thiz, NULL);
if (camera == 0) return 0;
return env->NewStringUTF(camera->getParameters().string());
}
static void android_hardware_Camera_reconnect(JNIEnv *env, jobject thiz)
{
LOGV("reconnect");
sp<Camera> camera = get_native_camera(env, thiz, NULL);
if (camera == 0) return;
if (camera->reconnect() != NO_ERROR) {
jniThrowException(env, "java/io/IOException", "reconnect failed");
return;
}
}
static jint android_hardware_Camera_lock(JNIEnv *env, jobject thiz)
{
LOGV("lock");
sp<Camera> camera = get_native_camera(env, thiz, NULL);
if (camera == 0) return INVALID_OPERATION;
return (jint) camera->lock();
}
static jint android_hardware_Camera_unlock(JNIEnv *env, jobject thiz)
{
LOGV("unlock");
sp<Camera> camera = get_native_camera(env, thiz, NULL);
if (camera == 0) return INVALID_OPERATION;
return (jint) camera->unlock();
}
//-------------------------------------------------
static JNINativeMethod camMethods[] = {
{ "native_setup",
"(Ljava/lang/Object;)V",
(void*)android_hardware_Camera_native_setup },
{ "native_release",
"()V",
(void*)android_hardware_Camera_release },
{ "setPreviewDisplay",
"(Landroid/view/Surface;)V",
(void *)android_hardware_Camera_setPreviewDisplay },
{ "startPreview",
"()V",
(void *)android_hardware_Camera_startPreview },
{ "stopPreview",
"()V",
(void *)android_hardware_Camera_stopPreview },
{ "previewEnabled",
"()Z",
(void *)android_hardware_Camera_previewEnabled },
{ "setHasPreviewCallback",
"(ZZ)V",
(void *)android_hardware_Camera_setHasPreviewCallback },
{ "native_autoFocus",
"()V",
(void *)android_hardware_Camera_autoFocus },
{ "native_takePicture",
"()V",
(void *)android_hardware_Camera_takePicture },
{ "native_setParameters",
"(Ljava/lang/String;)V",
(void *)android_hardware_Camera_setParameters },
{ "native_getParameters",
"()Ljava/lang/String;",
(void *)android_hardware_Camera_getParameters },
{ "reconnect",
"()V",
(void*)android_hardware_Camera_reconnect },
{ "lock",
"()I",
(void*)android_hardware_Camera_lock },
{ "unlock",
"()I",
(void*)android_hardware_Camera_unlock },
};
struct field {
const char *class_name;
const char *field_name;
const char *field_type;
jfieldID *jfield;
};
static int find_fields(JNIEnv *env, field *fields, int count)
{
for (int i = 0; i < count; i++) {
field *f = &fields[i];
jclass clazz = env->FindClass(f->class_name);
if (clazz == NULL) {
LOGE("Can't find %s", f->class_name);
return -1;
}
jfieldID field = env->GetFieldID(clazz, f->field_name, f->field_type);
if (field == NULL) {
LOGE("Can't find %s.%s", f->class_name, f->field_name);
return -1;
}
*(f->jfield) = field;
}
return 0;
}
// Get all the required offsets in java class and register native functions
int register_android_hardware_Camera(JNIEnv *env)
{
field fields_to_find[] = {
{ "android/hardware/Camera", "mNativeContext", "I", &fields.context },
{ "android/view/Surface", "mSurface", "I", &fields.surface }
};
if (find_fields(env, fields_to_find, NELEM(fields_to_find)) < 0)
return -1;
jclass clazz = env->FindClass("android/hardware/Camera");
fields.post_event = env->GetStaticMethodID(clazz, "postEventFromNative",
"(Ljava/lang/Object;IIILjava/lang/Object;)V");
if (fields.post_event == NULL) {
LOGE("Can't find android/hardware/Camera.postEventFromNative");
return -1;
}
// Register native functions
return AndroidRuntime::registerNativeMethods(env, "android/hardware/Camera",
camMethods, NELEM(camMethods));
}
|