1 /*
2 * Copyright (C) 2008 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 //#define LOG_NDEBUG 0
18 #define LOG_TAG "MediaRecorderJNI"
19 #include <utils/Log.h>
20
21 #include <gui/Surface.h>
22 #include <camera/ICameraService.h>
23 #include <camera/Camera.h>
24 #include <media/mediarecorder.h>
25 #include <stdio.h>
26 #include <assert.h>
27 #include <limits.h>
28 #include <unistd.h>
29 #include <fcntl.h>
30 #include <utils/threads.h>
31
32 #include "jni.h"
33 #include "JNIHelp.h"
34 #include "android_runtime/AndroidRuntime.h"
35
36 #include <system/audio.h>
37
38 // ----------------------------------------------------------------------------
39
40 using namespace android;
41
42 // ----------------------------------------------------------------------------
43
44 // helper function to extract a native Camera object from a Camera Java object
45 extern sp<Camera> get_native_camera(JNIEnv *env, jobject thiz, struct JNICameraContext** context);
46
47 struct fields_t {
48 jfieldID context;
49 jfieldID surface;
50 /* actually in android.view.Surface XXX */
51 jfieldID surface_native;
52
53 jmethodID post_event;
54 };
55 static fields_t fields;
56
57 static Mutex sLock;
58
59 // ----------------------------------------------------------------------------
60 // ref-counted object for callbacks
61 class JNIMediaRecorderListener: public MediaRecorderListener
62 {
63 public:
64 JNIMediaRecorderListener(JNIEnv* env, jobject thiz, jobject weak_thiz);
65 ~JNIMediaRecorderListener();
66 void notify(int msg, int ext1, int ext2);
67 private:
68 JNIMediaRecorderListener();
69 jclass mClass; // Reference to MediaRecorder class
70 jobject mObject; // Weak ref to MediaRecorder Java object to call on
71 };
72
JNIMediaRecorderListener(JNIEnv * env,jobject thiz,jobject weak_thiz)73 JNIMediaRecorderListener::JNIMediaRecorderListener(JNIEnv* env, jobject thiz, jobject weak_thiz)
74 {
75
76 // Hold onto the MediaRecorder class for use in calling the static method
77 // that posts events to the application thread.
78 jclass clazz = env->GetObjectClass(thiz);
79 if (clazz == NULL) {
80 ALOGE("Can't find android/media/MediaRecorder");
81 jniThrowException(env, "java/lang/Exception", NULL);
82 return;
83 }
84 mClass = (jclass)env->NewGlobalRef(clazz);
85
86 // We use a weak reference so the MediaRecorder object can be garbage collected.
87 // The reference is only used as a proxy for callbacks.
88 mObject = env->NewGlobalRef(weak_thiz);
89 }
90
~JNIMediaRecorderListener()91 JNIMediaRecorderListener::~JNIMediaRecorderListener()
92 {
93 // remove global references
94 JNIEnv *env = AndroidRuntime::getJNIEnv();
95 env->DeleteGlobalRef(mObject);
96 env->DeleteGlobalRef(mClass);
97 }
98
notify(int msg,int ext1,int ext2)99 void JNIMediaRecorderListener::notify(int msg, int ext1, int ext2)
100 {
101 ALOGV("JNIMediaRecorderListener::notify");
102
103 JNIEnv *env = AndroidRuntime::getJNIEnv();
104 env->CallStaticVoidMethod(mClass, fields.post_event, mObject, msg, ext1, ext2, 0);
105 }
106
107 // ----------------------------------------------------------------------------
108
get_surface(JNIEnv * env,jobject clazz)109 static sp<Surface> get_surface(JNIEnv* env, jobject clazz)
110 {
111 ALOGV("get_surface");
112 Surface* const p = (Surface*)env->GetIntField(clazz, fields.surface_native);
113 return sp<Surface>(p);
114 }
115
116 // Returns true if it throws an exception.
process_media_recorder_call(JNIEnv * env,status_t opStatus,const char * exception,const char * message)117 static bool process_media_recorder_call(JNIEnv *env, status_t opStatus, const char* exception, const char* message)
118 {
119 ALOGV("process_media_recorder_call");
120 if (opStatus == (status_t)INVALID_OPERATION) {
121 jniThrowException(env, "java/lang/IllegalStateException", NULL);
122 return true;
123 } else if (opStatus != (status_t)OK) {
124 jniThrowException(env, exception, message);
125 return true;
126 }
127 return false;
128 }
129
getMediaRecorder(JNIEnv * env,jobject thiz)130 static sp<MediaRecorder> getMediaRecorder(JNIEnv* env, jobject thiz)
131 {
132 Mutex::Autolock l(sLock);
133 MediaRecorder* const p = (MediaRecorder*)env->GetIntField(thiz, fields.context);
134 return sp<MediaRecorder>(p);
135 }
136
setMediaRecorder(JNIEnv * env,jobject thiz,const sp<MediaRecorder> & recorder)137 static sp<MediaRecorder> setMediaRecorder(JNIEnv* env, jobject thiz, const sp<MediaRecorder>& recorder)
138 {
139 Mutex::Autolock l(sLock);
140 sp<MediaRecorder> old = (MediaRecorder*)env->GetIntField(thiz, fields.context);
141 if (recorder.get()) {
142 recorder->incStrong(thiz);
143 }
144 if (old != 0) {
145 old->decStrong(thiz);
146 }
147 env->SetIntField(thiz, fields.context, (int)recorder.get());
148 return old;
149 }
150
151
android_media_MediaRecorder_setCamera(JNIEnv * env,jobject thiz,jobject camera)152 static void android_media_MediaRecorder_setCamera(JNIEnv* env, jobject thiz, jobject camera)
153 {
154 // we should not pass a null camera to get_native_camera() call.
155 if (camera == NULL) {
156 jniThrowNullPointerException(env, "camera object is a NULL pointer");
157 return;
158 }
159 sp<Camera> c = get_native_camera(env, camera, NULL);
160 sp<MediaRecorder> mr = getMediaRecorder(env, thiz);
161 process_media_recorder_call(env, mr->setCamera(c->remote(), c->getRecordingProxy()),
162 "java/lang/RuntimeException", "setCamera failed.");
163 }
164
165 static void
android_media_MediaRecorder_setVideoSource(JNIEnv * env,jobject thiz,jint vs)166 android_media_MediaRecorder_setVideoSource(JNIEnv *env, jobject thiz, jint vs)
167 {
168 ALOGV("setVideoSource(%d)", vs);
169 if (vs < VIDEO_SOURCE_DEFAULT || vs >= VIDEO_SOURCE_LIST_END) {
170 jniThrowException(env, "java/lang/IllegalArgumentException", "Invalid video source");
171 return;
172 }
173 sp<MediaRecorder> mr = getMediaRecorder(env, thiz);
174 process_media_recorder_call(env, mr->setVideoSource(vs), "java/lang/RuntimeException", "setVideoSource failed.");
175 }
176
177 static void
android_media_MediaRecorder_setAudioSource(JNIEnv * env,jobject thiz,jint as)178 android_media_MediaRecorder_setAudioSource(JNIEnv *env, jobject thiz, jint as)
179 {
180 ALOGV("setAudioSource(%d)", as);
181 if (as < AUDIO_SOURCE_DEFAULT || as >= AUDIO_SOURCE_CNT) {
182 jniThrowException(env, "java/lang/IllegalArgumentException", "Invalid audio source");
183 return;
184 }
185
186 sp<MediaRecorder> mr = getMediaRecorder(env, thiz);
187 process_media_recorder_call(env, mr->setAudioSource(as), "java/lang/RuntimeException", "setAudioSource failed.");
188 }
189
190 static void
android_media_MediaRecorder_setOutputFormat(JNIEnv * env,jobject thiz,jint of)191 android_media_MediaRecorder_setOutputFormat(JNIEnv *env, jobject thiz, jint of)
192 {
193 ALOGV("setOutputFormat(%d)", of);
194 if (of < OUTPUT_FORMAT_DEFAULT || of >= OUTPUT_FORMAT_LIST_END) {
195 jniThrowException(env, "java/lang/IllegalArgumentException", "Invalid output format");
196 return;
197 }
198 sp<MediaRecorder> mr = getMediaRecorder(env, thiz);
199 process_media_recorder_call(env, mr->setOutputFormat(of), "java/lang/RuntimeException", "setOutputFormat failed.");
200 }
201
202 static void
android_media_MediaRecorder_setVideoEncoder(JNIEnv * env,jobject thiz,jint ve)203 android_media_MediaRecorder_setVideoEncoder(JNIEnv *env, jobject thiz, jint ve)
204 {
205 ALOGV("setVideoEncoder(%d)", ve);
206 if (ve < VIDEO_ENCODER_DEFAULT || ve >= VIDEO_ENCODER_LIST_END) {
207 jniThrowException(env, "java/lang/IllegalArgumentException", "Invalid video encoder");
208 return;
209 }
210 sp<MediaRecorder> mr = getMediaRecorder(env, thiz);
211 process_media_recorder_call(env, mr->setVideoEncoder(ve), "java/lang/RuntimeException", "setVideoEncoder failed.");
212 }
213
214 static void
android_media_MediaRecorder_setAudioEncoder(JNIEnv * env,jobject thiz,jint ae)215 android_media_MediaRecorder_setAudioEncoder(JNIEnv *env, jobject thiz, jint ae)
216 {
217 ALOGV("setAudioEncoder(%d)", ae);
218 if (ae < AUDIO_ENCODER_DEFAULT || ae >= AUDIO_ENCODER_LIST_END) {
219 jniThrowException(env, "java/lang/IllegalArgumentException", "Invalid audio encoder");
220 return;
221 }
222 sp<MediaRecorder> mr = getMediaRecorder(env, thiz);
223 process_media_recorder_call(env, mr->setAudioEncoder(ae), "java/lang/RuntimeException", "setAudioEncoder failed.");
224 }
225
226 static void
android_media_MediaRecorder_setParameter(JNIEnv * env,jobject thiz,jstring params)227 android_media_MediaRecorder_setParameter(JNIEnv *env, jobject thiz, jstring params)
228 {
229 ALOGV("setParameter()");
230 if (params == NULL)
231 {
232 ALOGE("Invalid or empty params string. This parameter will be ignored.");
233 return;
234 }
235
236 sp<MediaRecorder> mr = getMediaRecorder(env, thiz);
237
238 const char* params8 = env->GetStringUTFChars(params, NULL);
239 if (params8 == NULL)
240 {
241 ALOGE("Failed to covert jstring to String8. This parameter will be ignored.");
242 return;
243 }
244
245 process_media_recorder_call(env, mr->setParameters(String8(params8)), "java/lang/RuntimeException", "setParameter failed.");
246 env->ReleaseStringUTFChars(params,params8);
247 }
248
249 static void
android_media_MediaRecorder_setOutputFileFD(JNIEnv * env,jobject thiz,jobject fileDescriptor,jlong offset,jlong length)250 android_media_MediaRecorder_setOutputFileFD(JNIEnv *env, jobject thiz, jobject fileDescriptor, jlong offset, jlong length)
251 {
252 ALOGV("setOutputFile");
253 if (fileDescriptor == NULL) {
254 jniThrowException(env, "java/lang/IllegalArgumentException", NULL);
255 return;
256 }
257 int fd = jniGetFDFromFileDescriptor(env, fileDescriptor);
258 sp<MediaRecorder> mr = getMediaRecorder(env, thiz);
259 status_t opStatus = mr->setOutputFile(fd, offset, length);
260 process_media_recorder_call(env, opStatus, "java/io/IOException", "setOutputFile failed.");
261 }
262
263 static void
android_media_MediaRecorder_setVideoSize(JNIEnv * env,jobject thiz,jint width,jint height)264 android_media_MediaRecorder_setVideoSize(JNIEnv *env, jobject thiz, jint width, jint height)
265 {
266 ALOGV("setVideoSize(%d, %d)", width, height);
267 sp<MediaRecorder> mr = getMediaRecorder(env, thiz);
268
269 if (width <= 0 || height <= 0) {
270 jniThrowException(env, "java/lang/IllegalArgumentException", "invalid video size");
271 return;
272 }
273 process_media_recorder_call(env, mr->setVideoSize(width, height), "java/lang/RuntimeException", "setVideoSize failed.");
274 }
275
276 static void
android_media_MediaRecorder_setVideoFrameRate(JNIEnv * env,jobject thiz,jint rate)277 android_media_MediaRecorder_setVideoFrameRate(JNIEnv *env, jobject thiz, jint rate)
278 {
279 ALOGV("setVideoFrameRate(%d)", rate);
280 if (rate <= 0) {
281 jniThrowException(env, "java/lang/IllegalArgumentException", "invalid frame rate");
282 return;
283 }
284 sp<MediaRecorder> mr = getMediaRecorder(env, thiz);
285 process_media_recorder_call(env, mr->setVideoFrameRate(rate), "java/lang/RuntimeException", "setVideoFrameRate failed.");
286 }
287
288 static void
android_media_MediaRecorder_setMaxDuration(JNIEnv * env,jobject thiz,jint max_duration_ms)289 android_media_MediaRecorder_setMaxDuration(JNIEnv *env, jobject thiz, jint max_duration_ms)
290 {
291 ALOGV("setMaxDuration(%d)", max_duration_ms);
292 sp<MediaRecorder> mr = getMediaRecorder(env, thiz);
293
294 char params[64];
295 sprintf(params, "max-duration=%d", max_duration_ms);
296
297 process_media_recorder_call(env, mr->setParameters(String8(params)), "java/lang/RuntimeException", "setMaxDuration failed.");
298 }
299
300 static void
android_media_MediaRecorder_setMaxFileSize(JNIEnv * env,jobject thiz,jlong max_filesize_bytes)301 android_media_MediaRecorder_setMaxFileSize(
302 JNIEnv *env, jobject thiz, jlong max_filesize_bytes)
303 {
304 ALOGV("setMaxFileSize(%lld)", max_filesize_bytes);
305 sp<MediaRecorder> mr = getMediaRecorder(env, thiz);
306
307 char params[64];
308 sprintf(params, "max-filesize=%lld", max_filesize_bytes);
309
310 process_media_recorder_call(env, mr->setParameters(String8(params)), "java/lang/RuntimeException", "setMaxFileSize failed.");
311 }
312
313 static void
android_media_MediaRecorder_prepare(JNIEnv * env,jobject thiz)314 android_media_MediaRecorder_prepare(JNIEnv *env, jobject thiz)
315 {
316 ALOGV("prepare");
317 sp<MediaRecorder> mr = getMediaRecorder(env, thiz);
318
319 jobject surface = env->GetObjectField(thiz, fields.surface);
320 if (surface != NULL) {
321 const sp<Surface> native_surface = get_surface(env, surface);
322
323 // The application may misbehave and
324 // the preview surface becomes unavailable
325 if (native_surface.get() == 0) {
326 ALOGE("Application lost the surface");
327 jniThrowException(env, "java/io/IOException", "invalid preview surface");
328 return;
329 }
330
331 ALOGI("prepare: surface=%p (identity=%d)", native_surface.get(), native_surface->getIdentity());
332 if (process_media_recorder_call(env, mr->setPreviewSurface(native_surface), "java/lang/RuntimeException", "setPreviewSurface failed.")) {
333 return;
334 }
335 }
336 process_media_recorder_call(env, mr->prepare(), "java/io/IOException", "prepare failed.");
337 }
338
339 static int
android_media_MediaRecorder_native_getMaxAmplitude(JNIEnv * env,jobject thiz)340 android_media_MediaRecorder_native_getMaxAmplitude(JNIEnv *env, jobject thiz)
341 {
342 ALOGV("getMaxAmplitude");
343 sp<MediaRecorder> mr = getMediaRecorder(env, thiz);
344 int result = 0;
345 process_media_recorder_call(env, mr->getMaxAmplitude(&result), "java/lang/RuntimeException", "getMaxAmplitude failed.");
346 return result;
347 }
348
349 static void
android_media_MediaRecorder_start(JNIEnv * env,jobject thiz)350 android_media_MediaRecorder_start(JNIEnv *env, jobject thiz)
351 {
352 ALOGV("start");
353 sp<MediaRecorder> mr = getMediaRecorder(env, thiz);
354 process_media_recorder_call(env, mr->start(), "java/lang/RuntimeException", "start failed.");
355 }
356
357 static void
android_media_MediaRecorder_stop(JNIEnv * env,jobject thiz)358 android_media_MediaRecorder_stop(JNIEnv *env, jobject thiz)
359 {
360 ALOGV("stop");
361 sp<MediaRecorder> mr = getMediaRecorder(env, thiz);
362 process_media_recorder_call(env, mr->stop(), "java/lang/RuntimeException", "stop failed.");
363 }
364
365 static void
android_media_MediaRecorder_native_reset(JNIEnv * env,jobject thiz)366 android_media_MediaRecorder_native_reset(JNIEnv *env, jobject thiz)
367 {
368 ALOGV("native_reset");
369 sp<MediaRecorder> mr = getMediaRecorder(env, thiz);
370 process_media_recorder_call(env, mr->reset(), "java/lang/RuntimeException", "native_reset failed.");
371 }
372
373 static void
android_media_MediaRecorder_release(JNIEnv * env,jobject thiz)374 android_media_MediaRecorder_release(JNIEnv *env, jobject thiz)
375 {
376 ALOGV("release");
377 sp<MediaRecorder> mr = setMediaRecorder(env, thiz, 0);
378 if (mr != NULL) {
379 mr->setListener(NULL);
380 mr->release();
381 }
382 }
383
384 // This function gets some field IDs, which in turn causes class initialization.
385 // It is called from a static block in MediaRecorder, which won't run until the
386 // first time an instance of this class is used.
387 static void
android_media_MediaRecorder_native_init(JNIEnv * env)388 android_media_MediaRecorder_native_init(JNIEnv *env)
389 {
390 jclass clazz;
391
392 clazz = env->FindClass("android/media/MediaRecorder");
393 if (clazz == NULL) {
394 return;
395 }
396
397 fields.context = env->GetFieldID(clazz, "mNativeContext", "I");
398 if (fields.context == NULL) {
399 return;
400 }
401
402 fields.surface = env->GetFieldID(clazz, "mSurface", "Landroid/view/Surface;");
403 if (fields.surface == NULL) {
404 return;
405 }
406
407 jclass surface = env->FindClass("android/view/Surface");
408 if (surface == NULL) {
409 return;
410 }
411
412 fields.surface_native = env->GetFieldID(surface, ANDROID_VIEW_SURFACE_JNI_ID, "I");
413 if (fields.surface_native == NULL) {
414 return;
415 }
416
417 fields.post_event = env->GetStaticMethodID(clazz, "postEventFromNative",
418 "(Ljava/lang/Object;IIILjava/lang/Object;)V");
419 if (fields.post_event == NULL) {
420 return;
421 }
422 }
423
424
425 static void
android_media_MediaRecorder_native_setup(JNIEnv * env,jobject thiz,jobject weak_this)426 android_media_MediaRecorder_native_setup(JNIEnv *env, jobject thiz, jobject weak_this)
427 {
428 ALOGV("setup");
429 sp<MediaRecorder> mr = new MediaRecorder();
430 if (mr == NULL) {
431 jniThrowException(env, "java/lang/RuntimeException", "Out of memory");
432 return;
433 }
434 if (mr->initCheck() != NO_ERROR) {
435 jniThrowException(env, "java/lang/RuntimeException", "Unable to initialize media recorder");
436 return;
437 }
438
439 // create new listener and give it to MediaRecorder
440 sp<JNIMediaRecorderListener> listener = new JNIMediaRecorderListener(env, thiz, weak_this);
441 mr->setListener(listener);
442
443 setMediaRecorder(env, thiz, mr);
444 }
445
446 static void
android_media_MediaRecorder_native_finalize(JNIEnv * env,jobject thiz)447 android_media_MediaRecorder_native_finalize(JNIEnv *env, jobject thiz)
448 {
449 ALOGV("finalize");
450 android_media_MediaRecorder_release(env, thiz);
451 }
452
453 // ----------------------------------------------------------------------------
454
455 static JNINativeMethod gMethods[] = {
456 {"setCamera", "(Landroid/hardware/Camera;)V", (void *)android_media_MediaRecorder_setCamera},
457 {"setVideoSource", "(I)V", (void *)android_media_MediaRecorder_setVideoSource},
458 {"setAudioSource", "(I)V", (void *)android_media_MediaRecorder_setAudioSource},
459 {"setOutputFormat", "(I)V", (void *)android_media_MediaRecorder_setOutputFormat},
460 {"setVideoEncoder", "(I)V", (void *)android_media_MediaRecorder_setVideoEncoder},
461 {"setAudioEncoder", "(I)V", (void *)android_media_MediaRecorder_setAudioEncoder},
462 {"setParameter", "(Ljava/lang/String;)V", (void *)android_media_MediaRecorder_setParameter},
463 {"_setOutputFile", "(Ljava/io/FileDescriptor;JJ)V", (void *)android_media_MediaRecorder_setOutputFileFD},
464 {"setVideoSize", "(II)V", (void *)android_media_MediaRecorder_setVideoSize},
465 {"setVideoFrameRate", "(I)V", (void *)android_media_MediaRecorder_setVideoFrameRate},
466 {"setMaxDuration", "(I)V", (void *)android_media_MediaRecorder_setMaxDuration},
467 {"setMaxFileSize", "(J)V", (void *)android_media_MediaRecorder_setMaxFileSize},
468 {"_prepare", "()V", (void *)android_media_MediaRecorder_prepare},
469 {"getMaxAmplitude", "()I", (void *)android_media_MediaRecorder_native_getMaxAmplitude},
470 {"start", "()V", (void *)android_media_MediaRecorder_start},
471 {"stop", "()V", (void *)android_media_MediaRecorder_stop},
472 {"native_reset", "()V", (void *)android_media_MediaRecorder_native_reset},
473 {"release", "()V", (void *)android_media_MediaRecorder_release},
474 {"native_init", "()V", (void *)android_media_MediaRecorder_native_init},
475 {"native_setup", "(Ljava/lang/Object;)V", (void *)android_media_MediaRecorder_native_setup},
476 {"native_finalize", "()V", (void *)android_media_MediaRecorder_native_finalize},
477 };
478
479 static const char* const kClassPathName = "android/media/MediaRecorder";
480
481 // This function only registers the native methods, and is called from
482 // JNI_OnLoad in android_media_MediaPlayer.cpp
register_android_media_MediaRecorder(JNIEnv * env)483 int register_android_media_MediaRecorder(JNIEnv *env)
484 {
485 return AndroidRuntime::registerNativeMethods(env,
486 "android/media/MediaRecorder", gMethods, NELEM(gMethods));
487 }
488