blob: fd69cad67e3b649a572894c063ee7d15486665ac [file] [log] [blame]
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001/*
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
Mathias Agopian8335f1c2012-02-25 18:48:35 -080021#include <gui/Surface.h>
Mathias Agopian000479f2010-02-09 17:46:37 -080022#include <camera/ICameraService.h>
23#include <camera/Camera.h>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080024#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
Dima Zavin34bb4192011-05-11 14:15:23 -070036#include <system/audio.h>
Mathias Agopian3866f0d2013-02-11 22:08:48 -080037#include <android_runtime/android_view_Surface.h>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080038
39// ----------------------------------------------------------------------------
40
41using namespace android;
42
43// ----------------------------------------------------------------------------
44
45// helper function to extract a native Camera object from a Camera Java object
Dave Sparks5e271152009-06-23 17:30:11 -070046extern sp<Camera> get_native_camera(JNIEnv *env, jobject thiz, struct JNICameraContext** context);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080047
48struct fields_t {
49 jfieldID context;
50 jfieldID surface;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080051
52 jmethodID post_event;
53};
54static fields_t fields;
55
56static Mutex sLock;
57
58// ----------------------------------------------------------------------------
59// ref-counted object for callbacks
60class JNIMediaRecorderListener: public MediaRecorderListener
61{
62public:
63 JNIMediaRecorderListener(JNIEnv* env, jobject thiz, jobject weak_thiz);
64 ~JNIMediaRecorderListener();
65 void notify(int msg, int ext1, int ext2);
66private:
67 JNIMediaRecorderListener();
68 jclass mClass; // Reference to MediaRecorder class
69 jobject mObject; // Weak ref to MediaRecorder Java object to call on
70};
71
72JNIMediaRecorderListener::JNIMediaRecorderListener(JNIEnv* env, jobject thiz, jobject weak_thiz)
73{
74
75 // Hold onto the MediaRecorder class for use in calling the static method
76 // that posts events to the application thread.
77 jclass clazz = env->GetObjectClass(thiz);
78 if (clazz == NULL) {
Steve Block3762c312012-01-06 19:20:56 +000079 ALOGE("Can't find android/media/MediaRecorder");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080080 jniThrowException(env, "java/lang/Exception", NULL);
81 return;
82 }
83 mClass = (jclass)env->NewGlobalRef(clazz);
84
85 // We use a weak reference so the MediaRecorder object can be garbage collected.
86 // The reference is only used as a proxy for callbacks.
87 mObject = env->NewGlobalRef(weak_thiz);
88}
89
90JNIMediaRecorderListener::~JNIMediaRecorderListener()
91{
92 // remove global references
93 JNIEnv *env = AndroidRuntime::getJNIEnv();
94 env->DeleteGlobalRef(mObject);
95 env->DeleteGlobalRef(mClass);
96}
97
98void JNIMediaRecorderListener::notify(int msg, int ext1, int ext2)
99{
Steve Block71f2cf12011-10-20 11:56:00 +0100100 ALOGV("JNIMediaRecorderListener::notify");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800101
102 JNIEnv *env = AndroidRuntime::getJNIEnv();
103 env->CallStaticVoidMethod(mClass, fields.post_event, mObject, msg, ext1, ext2, 0);
104}
105
106// ----------------------------------------------------------------------------
107
108static sp<Surface> get_surface(JNIEnv* env, jobject clazz)
109{
Steve Block71f2cf12011-10-20 11:56:00 +0100110 ALOGV("get_surface");
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800111 return android_view_Surface_getSurface(env, clazz);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800112}
113
114// Returns true if it throws an exception.
115static bool process_media_recorder_call(JNIEnv *env, status_t opStatus, const char* exception, const char* message)
116{
Steve Block71f2cf12011-10-20 11:56:00 +0100117 ALOGV("process_media_recorder_call");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800118 if (opStatus == (status_t)INVALID_OPERATION) {
119 jniThrowException(env, "java/lang/IllegalStateException", NULL);
120 return true;
121 } else if (opStatus != (status_t)OK) {
122 jniThrowException(env, exception, message);
123 return true;
124 }
125 return false;
126}
127
Pannag Sanketicac873b2011-07-25 17:20:50 -0700128static sp<MediaRecorder> getMediaRecorder(JNIEnv* env, jobject thiz)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800129{
130 Mutex::Autolock l(sLock);
Ashok Bhat075e9a12014-01-06 13:45:09 +0000131 MediaRecorder* const p = (MediaRecorder*)env->GetLongField(thiz, fields.context);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800132 return sp<MediaRecorder>(p);
133}
134
135static sp<MediaRecorder> setMediaRecorder(JNIEnv* env, jobject thiz, const sp<MediaRecorder>& recorder)
136{
137 Mutex::Autolock l(sLock);
Ashok Bhat075e9a12014-01-06 13:45:09 +0000138 sp<MediaRecorder> old = (MediaRecorder*)env->GetLongField(thiz, fields.context);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800139 if (recorder.get()) {
140 recorder->incStrong(thiz);
141 }
142 if (old != 0) {
143 old->decStrong(thiz);
144 }
Ashok Bhat075e9a12014-01-06 13:45:09 +0000145 env->SetLongField(thiz, fields.context, (jlong)recorder.get());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800146 return old;
147}
148
149
150static void android_media_MediaRecorder_setCamera(JNIEnv* env, jobject thiz, jobject camera)
151{
James Dong429a3b52009-05-11 10:58:03 -0700152 // we should not pass a null camera to get_native_camera() call.
153 if (camera == NULL) {
Elliott Hughes15dd15f2011-04-08 17:42:34 -0700154 jniThrowNullPointerException(env, "camera object is a NULL pointer");
James Dong429a3b52009-05-11 10:58:03 -0700155 return;
156 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800157 sp<Camera> c = get_native_camera(env, camera, NULL);
158 sp<MediaRecorder> mr = getMediaRecorder(env, thiz);
Wu-cheng Li42419ce2011-06-01 17:22:24 +0800159 process_media_recorder_call(env, mr->setCamera(c->remote(), c->getRecordingProxy()),
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800160 "java/lang/RuntimeException", "setCamera failed.");
161}
162
163static void
164android_media_MediaRecorder_setVideoSource(JNIEnv *env, jobject thiz, jint vs)
165{
Steve Block71f2cf12011-10-20 11:56:00 +0100166 ALOGV("setVideoSource(%d)", vs);
Jianhong Jiang2bcda902009-06-08 08:50:42 -0700167 if (vs < VIDEO_SOURCE_DEFAULT || vs >= VIDEO_SOURCE_LIST_END) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800168 jniThrowException(env, "java/lang/IllegalArgumentException", "Invalid video source");
169 return;
170 }
171 sp<MediaRecorder> mr = getMediaRecorder(env, thiz);
172 process_media_recorder_call(env, mr->setVideoSource(vs), "java/lang/RuntimeException", "setVideoSource failed.");
173}
174
175static void
176android_media_MediaRecorder_setAudioSource(JNIEnv *env, jobject thiz, jint as)
177{
Steve Block71f2cf12011-10-20 11:56:00 +0100178 ALOGV("setAudioSource(%d)", as);
Dima Zavin24fc2fb2011-04-19 22:30:36 -0700179 if (as < AUDIO_SOURCE_DEFAULT || as >= AUDIO_SOURCE_CNT) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800180 jniThrowException(env, "java/lang/IllegalArgumentException", "Invalid audio source");
181 return;
182 }
Jianhong Jiang2bcda902009-06-08 08:50:42 -0700183
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800184 sp<MediaRecorder> mr = getMediaRecorder(env, thiz);
185 process_media_recorder_call(env, mr->setAudioSource(as), "java/lang/RuntimeException", "setAudioSource failed.");
186}
187
188static void
189android_media_MediaRecorder_setOutputFormat(JNIEnv *env, jobject thiz, jint of)
190{
Steve Block71f2cf12011-10-20 11:56:00 +0100191 ALOGV("setOutputFormat(%d)", of);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800192 if (of < OUTPUT_FORMAT_DEFAULT || of >= OUTPUT_FORMAT_LIST_END) {
193 jniThrowException(env, "java/lang/IllegalArgumentException", "Invalid output format");
194 return;
195 }
196 sp<MediaRecorder> mr = getMediaRecorder(env, thiz);
197 process_media_recorder_call(env, mr->setOutputFormat(of), "java/lang/RuntimeException", "setOutputFormat failed.");
198}
199
200static void
201android_media_MediaRecorder_setVideoEncoder(JNIEnv *env, jobject thiz, jint ve)
202{
Steve Block71f2cf12011-10-20 11:56:00 +0100203 ALOGV("setVideoEncoder(%d)", ve);
Jianhong Jiang2bcda902009-06-08 08:50:42 -0700204 if (ve < VIDEO_ENCODER_DEFAULT || ve >= VIDEO_ENCODER_LIST_END) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800205 jniThrowException(env, "java/lang/IllegalArgumentException", "Invalid video encoder");
206 return;
207 }
208 sp<MediaRecorder> mr = getMediaRecorder(env, thiz);
209 process_media_recorder_call(env, mr->setVideoEncoder(ve), "java/lang/RuntimeException", "setVideoEncoder failed.");
210}
211
212static void
213android_media_MediaRecorder_setAudioEncoder(JNIEnv *env, jobject thiz, jint ae)
214{
Steve Block71f2cf12011-10-20 11:56:00 +0100215 ALOGV("setAudioEncoder(%d)", ae);
Jianhong Jiang2bcda902009-06-08 08:50:42 -0700216 if (ae < AUDIO_ENCODER_DEFAULT || ae >= AUDIO_ENCODER_LIST_END) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800217 jniThrowException(env, "java/lang/IllegalArgumentException", "Invalid audio encoder");
218 return;
219 }
220 sp<MediaRecorder> mr = getMediaRecorder(env, thiz);
221 process_media_recorder_call(env, mr->setAudioEncoder(ae), "java/lang/RuntimeException", "setAudioEncoder failed.");
222}
223
224static void
James Dong0fc6bc42010-02-26 19:36:35 -0800225android_media_MediaRecorder_setParameter(JNIEnv *env, jobject thiz, jstring params)
Jianhong Jiang2bcda902009-06-08 08:50:42 -0700226{
Steve Block71f2cf12011-10-20 11:56:00 +0100227 ALOGV("setParameter()");
Jianhong Jiang2bcda902009-06-08 08:50:42 -0700228 if (params == NULL)
229 {
Steve Block3762c312012-01-06 19:20:56 +0000230 ALOGE("Invalid or empty params string. This parameter will be ignored.");
Jianhong Jiang2bcda902009-06-08 08:50:42 -0700231 return;
232 }
233
234 sp<MediaRecorder> mr = getMediaRecorder(env, thiz);
235
236 const char* params8 = env->GetStringUTFChars(params, NULL);
237 if (params8 == NULL)
238 {
Steve Block3762c312012-01-06 19:20:56 +0000239 ALOGE("Failed to covert jstring to String8. This parameter will be ignored.");
Jianhong Jiang2bcda902009-06-08 08:50:42 -0700240 return;
241 }
242
243 process_media_recorder_call(env, mr->setParameters(String8(params8)), "java/lang/RuntimeException", "setParameter failed.");
244 env->ReleaseStringUTFChars(params,params8);
245}
246
247static void
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800248android_media_MediaRecorder_setOutputFileFD(JNIEnv *env, jobject thiz, jobject fileDescriptor, jlong offset, jlong length)
249{
Steve Block71f2cf12011-10-20 11:56:00 +0100250 ALOGV("setOutputFile");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800251 if (fileDescriptor == NULL) {
252 jniThrowException(env, "java/lang/IllegalArgumentException", NULL);
253 return;
254 }
Elliott Hughesa3804cf2011-04-11 16:50:19 -0700255 int fd = jniGetFDFromFileDescriptor(env, fileDescriptor);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800256 sp<MediaRecorder> mr = getMediaRecorder(env, thiz);
257 status_t opStatus = mr->setOutputFile(fd, offset, length);
258 process_media_recorder_call(env, opStatus, "java/io/IOException", "setOutputFile failed.");
259}
260
261static void
262android_media_MediaRecorder_setVideoSize(JNIEnv *env, jobject thiz, jint width, jint height)
263{
Steve Block71f2cf12011-10-20 11:56:00 +0100264 ALOGV("setVideoSize(%d, %d)", width, height);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800265 sp<MediaRecorder> mr = getMediaRecorder(env, thiz);
266
267 if (width <= 0 || height <= 0) {
268 jniThrowException(env, "java/lang/IllegalArgumentException", "invalid video size");
269 return;
270 }
271 process_media_recorder_call(env, mr->setVideoSize(width, height), "java/lang/RuntimeException", "setVideoSize failed.");
272}
273
274static void
275android_media_MediaRecorder_setVideoFrameRate(JNIEnv *env, jobject thiz, jint rate)
276{
Steve Block71f2cf12011-10-20 11:56:00 +0100277 ALOGV("setVideoFrameRate(%d)", rate);
James Dongcbe7c542009-07-30 11:16:11 -0700278 if (rate <= 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800279 jniThrowException(env, "java/lang/IllegalArgumentException", "invalid frame rate");
280 return;
281 }
282 sp<MediaRecorder> mr = getMediaRecorder(env, thiz);
283 process_media_recorder_call(env, mr->setVideoFrameRate(rate), "java/lang/RuntimeException", "setVideoFrameRate failed.");
284}
285
286static void
The Android Open Source Projectba87e3e2009-03-13 13:04:22 -0700287android_media_MediaRecorder_setMaxDuration(JNIEnv *env, jobject thiz, jint max_duration_ms)
288{
Steve Block71f2cf12011-10-20 11:56:00 +0100289 ALOGV("setMaxDuration(%d)", max_duration_ms);
The Android Open Source Projectba87e3e2009-03-13 13:04:22 -0700290 sp<MediaRecorder> mr = getMediaRecorder(env, thiz);
291
292 char params[64];
293 sprintf(params, "max-duration=%d", max_duration_ms);
294
295 process_media_recorder_call(env, mr->setParameters(String8(params)), "java/lang/RuntimeException", "setMaxDuration failed.");
296}
297
298static void
The Android Open Source Project10592532009-03-18 17:39:46 -0700299android_media_MediaRecorder_setMaxFileSize(
300 JNIEnv *env, jobject thiz, jlong max_filesize_bytes)
301{
Steve Block71f2cf12011-10-20 11:56:00 +0100302 ALOGV("setMaxFileSize(%lld)", max_filesize_bytes);
The Android Open Source Project10592532009-03-18 17:39:46 -0700303 sp<MediaRecorder> mr = getMediaRecorder(env, thiz);
304
305 char params[64];
306 sprintf(params, "max-filesize=%lld", max_filesize_bytes);
307
308 process_media_recorder_call(env, mr->setParameters(String8(params)), "java/lang/RuntimeException", "setMaxFileSize failed.");
309}
310
311static void
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800312android_media_MediaRecorder_prepare(JNIEnv *env, jobject thiz)
313{
Steve Block71f2cf12011-10-20 11:56:00 +0100314 ALOGV("prepare");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800315 sp<MediaRecorder> mr = getMediaRecorder(env, thiz);
316
317 jobject surface = env->GetObjectField(thiz, fields.surface);
318 if (surface != NULL) {
James Dong457d5ae2010-06-09 10:19:33 -0700319 const sp<Surface> native_surface = get_surface(env, surface);
James Dongd790c6482010-08-12 10:18:01 -0700320
321 // The application may misbehave and
322 // the preview surface becomes unavailable
323 if (native_surface.get() == 0) {
Steve Block3762c312012-01-06 19:20:56 +0000324 ALOGE("Application lost the surface");
James Dongd790c6482010-08-12 10:18:01 -0700325 jniThrowException(env, "java/io/IOException", "invalid preview surface");
326 return;
327 }
328
Mathias Agopianbfcae352013-02-14 15:32:50 -0800329 ALOGI("prepare: surface=%p", native_surface.get());
Mathias Agopian4a05f432013-03-12 18:43:34 -0700330 if (process_media_recorder_call(env, mr->setPreviewSurface(native_surface->getIGraphicBufferProducer()), "java/lang/RuntimeException", "setPreviewSurface failed.")) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800331 return;
332 }
333 }
334 process_media_recorder_call(env, mr->prepare(), "java/io/IOException", "prepare failed.");
335}
336
Ashok Bhat075e9a12014-01-06 13:45:09 +0000337static jint
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800338android_media_MediaRecorder_native_getMaxAmplitude(JNIEnv *env, jobject thiz)
339{
Steve Block71f2cf12011-10-20 11:56:00 +0100340 ALOGV("getMaxAmplitude");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800341 sp<MediaRecorder> mr = getMediaRecorder(env, thiz);
342 int result = 0;
343 process_media_recorder_call(env, mr->getMaxAmplitude(&result), "java/lang/RuntimeException", "getMaxAmplitude failed.");
Ashok Bhat075e9a12014-01-06 13:45:09 +0000344 return (jint) result;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800345}
346
Chong Zhang83cc9942014-01-02 12:10:04 -0800347static jobject
348android_media_MediaRecorder_getSurface(JNIEnv *env, jobject thiz)
349{
350 ALOGV("getSurface");
351 sp<MediaRecorder> mr = getMediaRecorder(env, thiz);
352
353 sp<IGraphicBufferProducer> bufferProducer = mr->querySurfaceMediaSourceFromMediaServer();
354 if (bufferProducer == NULL) {
355 jniThrowException(
356 env,
357 "java/lang/IllegalStateException",
358 "failed to get surface");
359 return NULL;
360 }
361
362 // Wrap the IGBP in a Java-language Surface.
363 return android_view_Surface_createFromIGraphicBufferProducer(env,
364 bufferProducer);
365}
366
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800367static void
368android_media_MediaRecorder_start(JNIEnv *env, jobject thiz)
369{
Steve Block71f2cf12011-10-20 11:56:00 +0100370 ALOGV("start");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800371 sp<MediaRecorder> mr = getMediaRecorder(env, thiz);
372 process_media_recorder_call(env, mr->start(), "java/lang/RuntimeException", "start failed.");
373}
374
375static void
376android_media_MediaRecorder_stop(JNIEnv *env, jobject thiz)
377{
Steve Block71f2cf12011-10-20 11:56:00 +0100378 ALOGV("stop");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800379 sp<MediaRecorder> mr = getMediaRecorder(env, thiz);
380 process_media_recorder_call(env, mr->stop(), "java/lang/RuntimeException", "stop failed.");
381}
382
383static void
384android_media_MediaRecorder_native_reset(JNIEnv *env, jobject thiz)
385{
Steve Block71f2cf12011-10-20 11:56:00 +0100386 ALOGV("native_reset");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800387 sp<MediaRecorder> mr = getMediaRecorder(env, thiz);
388 process_media_recorder_call(env, mr->reset(), "java/lang/RuntimeException", "native_reset failed.");
389}
390
391static void
392android_media_MediaRecorder_release(JNIEnv *env, jobject thiz)
393{
Steve Block71f2cf12011-10-20 11:56:00 +0100394 ALOGV("release");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800395 sp<MediaRecorder> mr = setMediaRecorder(env, thiz, 0);
396 if (mr != NULL) {
397 mr->setListener(NULL);
The Android Open Source Project4df24232009-03-05 14:34:35 -0800398 mr->release();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800399 }
400}
401
Marco Nelissen4935d052009-08-03 11:12:58 -0700402// This function gets some field IDs, which in turn causes class initialization.
403// It is called from a static block in MediaRecorder, which won't run until the
404// first time an instance of this class is used.
405static void
406android_media_MediaRecorder_native_init(JNIEnv *env)
407{
408 jclass clazz;
409
410 clazz = env->FindClass("android/media/MediaRecorder");
411 if (clazz == NULL) {
Marco Nelissen4935d052009-08-03 11:12:58 -0700412 return;
413 }
414
Ashok Bhat075e9a12014-01-06 13:45:09 +0000415 fields.context = env->GetFieldID(clazz, "mNativeContext", "J");
Marco Nelissen4935d052009-08-03 11:12:58 -0700416 if (fields.context == NULL) {
Marco Nelissen4935d052009-08-03 11:12:58 -0700417 return;
418 }
419
420 fields.surface = env->GetFieldID(clazz, "mSurface", "Landroid/view/Surface;");
421 if (fields.surface == NULL) {
Marco Nelissen4935d052009-08-03 11:12:58 -0700422 return;
423 }
424
425 jclass surface = env->FindClass("android/view/Surface");
426 if (surface == NULL) {
Marco Nelissen4935d052009-08-03 11:12:58 -0700427 return;
428 }
429
Marco Nelissen4935d052009-08-03 11:12:58 -0700430 fields.post_event = env->GetStaticMethodID(clazz, "postEventFromNative",
431 "(Ljava/lang/Object;IIILjava/lang/Object;)V");
432 if (fields.post_event == NULL) {
Marco Nelissen4935d052009-08-03 11:12:58 -0700433 return;
434 }
435}
436
437
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800438static void
Eino-Ville Talvala788717c2013-02-15 18:30:15 -0800439android_media_MediaRecorder_native_setup(JNIEnv *env, jobject thiz, jobject weak_this,
440 jstring packageName)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800441{
Steve Block71f2cf12011-10-20 11:56:00 +0100442 ALOGV("setup");
Eino-Ville Talvala788717c2013-02-15 18:30:15 -0800443
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800444 sp<MediaRecorder> mr = new MediaRecorder();
445 if (mr == NULL) {
446 jniThrowException(env, "java/lang/RuntimeException", "Out of memory");
447 return;
448 }
449 if (mr->initCheck() != NO_ERROR) {
James Dong8d3b9102010-09-09 18:43:48 -0700450 jniThrowException(env, "java/lang/RuntimeException", "Unable to initialize media recorder");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800451 return;
452 }
453
454 // create new listener and give it to MediaRecorder
455 sp<JNIMediaRecorderListener> listener = new JNIMediaRecorderListener(env, thiz, weak_this);
456 mr->setListener(listener);
457
Eino-Ville Talvala788717c2013-02-15 18:30:15 -0800458 // Convert client name jstring to String16
459 const char16_t *rawClientName = env->GetStringChars(packageName, NULL);
460 jsize rawClientNameLen = env->GetStringLength(packageName);
461 String16 clientName(rawClientName, rawClientNameLen);
462 env->ReleaseStringChars(packageName, rawClientName);
463
464 // pass client package name for permissions tracking
465 mr->setClientName(clientName);
466
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800467 setMediaRecorder(env, thiz, mr);
468}
469
470static void
471android_media_MediaRecorder_native_finalize(JNIEnv *env, jobject thiz)
472{
Steve Block71f2cf12011-10-20 11:56:00 +0100473 ALOGV("finalize");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800474 android_media_MediaRecorder_release(env, thiz);
475}
476
477// ----------------------------------------------------------------------------
478
479static JNINativeMethod gMethods[] = {
480 {"setCamera", "(Landroid/hardware/Camera;)V", (void *)android_media_MediaRecorder_setCamera},
481 {"setVideoSource", "(I)V", (void *)android_media_MediaRecorder_setVideoSource},
482 {"setAudioSource", "(I)V", (void *)android_media_MediaRecorder_setAudioSource},
483 {"setOutputFormat", "(I)V", (void *)android_media_MediaRecorder_setOutputFormat},
484 {"setVideoEncoder", "(I)V", (void *)android_media_MediaRecorder_setVideoEncoder},
485 {"setAudioEncoder", "(I)V", (void *)android_media_MediaRecorder_setAudioEncoder},
James Dong0fc6bc42010-02-26 19:36:35 -0800486 {"setParameter", "(Ljava/lang/String;)V", (void *)android_media_MediaRecorder_setParameter},
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800487 {"_setOutputFile", "(Ljava/io/FileDescriptor;JJ)V", (void *)android_media_MediaRecorder_setOutputFileFD},
488 {"setVideoSize", "(II)V", (void *)android_media_MediaRecorder_setVideoSize},
489 {"setVideoFrameRate", "(I)V", (void *)android_media_MediaRecorder_setVideoFrameRate},
The Android Open Source Projectba87e3e2009-03-13 13:04:22 -0700490 {"setMaxDuration", "(I)V", (void *)android_media_MediaRecorder_setMaxDuration},
The Android Open Source Project10592532009-03-18 17:39:46 -0700491 {"setMaxFileSize", "(J)V", (void *)android_media_MediaRecorder_setMaxFileSize},
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800492 {"_prepare", "()V", (void *)android_media_MediaRecorder_prepare},
Chong Zhang83cc9942014-01-02 12:10:04 -0800493 {"getSurface", "()Landroid/view/Surface;", (void *)android_media_MediaRecorder_getSurface},
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800494 {"getMaxAmplitude", "()I", (void *)android_media_MediaRecorder_native_getMaxAmplitude},
495 {"start", "()V", (void *)android_media_MediaRecorder_start},
496 {"stop", "()V", (void *)android_media_MediaRecorder_stop},
Marco Nelissen4935d052009-08-03 11:12:58 -0700497 {"native_reset", "()V", (void *)android_media_MediaRecorder_native_reset},
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800498 {"release", "()V", (void *)android_media_MediaRecorder_release},
Marco Nelissen4935d052009-08-03 11:12:58 -0700499 {"native_init", "()V", (void *)android_media_MediaRecorder_native_init},
Eino-Ville Talvala788717c2013-02-15 18:30:15 -0800500 {"native_setup", "(Ljava/lang/Object;Ljava/lang/String;)V", (void *)android_media_MediaRecorder_native_setup},
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800501 {"native_finalize", "()V", (void *)android_media_MediaRecorder_native_finalize},
502};
503
504static const char* const kClassPathName = "android/media/MediaRecorder";
505
Marco Nelissen4935d052009-08-03 11:12:58 -0700506// This function only registers the native methods, and is called from
507// JNI_OnLoad in android_media_MediaPlayer.cpp
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800508int register_android_media_MediaRecorder(JNIEnv *env)
509{
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800510 return AndroidRuntime::registerNativeMethods(env,
511 "android/media/MediaRecorder", gMethods, NELEM(gMethods));
512}