blob: 3b1b1d7875a11059012cfe833cafe001ef88eb90 [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
Mark Salyzyn85394032014-04-16 10:28:37 -070017#include <assert.h>
18#include <fcntl.h>
19#include <inttypes.h>
20#include <limits.h>
21#include <stdio.h>
22#include <unistd.h>
23
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080024//#define LOG_NDEBUG 0
25#define LOG_TAG "MediaRecorderJNI"
26#include <utils/Log.h>
27
Mathias Agopian8335f1c2012-02-25 18:48:35 -080028#include <gui/Surface.h>
Mathias Agopian000479f2010-02-09 17:46:37 -080029#include <camera/ICameraService.h>
30#include <camera/Camera.h>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080031#include <media/mediarecorder.h>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080032#include <utils/threads.h>
33
34#include "jni.h"
35#include "JNIHelp.h"
36#include "android_runtime/AndroidRuntime.h"
37
Dima Zavin34bb4192011-05-11 14:15:23 -070038#include <system/audio.h>
Mathias Agopian3866f0d2013-02-11 22:08:48 -080039#include <android_runtime/android_view_Surface.h>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080040
41// ----------------------------------------------------------------------------
42
43using namespace android;
44
45// ----------------------------------------------------------------------------
46
47// helper function to extract a native Camera object from a Camera Java object
Dave Sparks5e271152009-06-23 17:30:11 -070048extern sp<Camera> get_native_camera(JNIEnv *env, jobject thiz, struct JNICameraContext** context);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080049
50struct fields_t {
51 jfieldID context;
52 jfieldID surface;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080053
54 jmethodID post_event;
55};
56static fields_t fields;
57
58static Mutex sLock;
59
60// ----------------------------------------------------------------------------
61// ref-counted object for callbacks
62class JNIMediaRecorderListener: public MediaRecorderListener
63{
64public:
65 JNIMediaRecorderListener(JNIEnv* env, jobject thiz, jobject weak_thiz);
66 ~JNIMediaRecorderListener();
67 void notify(int msg, int ext1, int ext2);
68private:
69 JNIMediaRecorderListener();
70 jclass mClass; // Reference to MediaRecorder class
71 jobject mObject; // Weak ref to MediaRecorder Java object to call on
72};
73
74JNIMediaRecorderListener::JNIMediaRecorderListener(JNIEnv* env, jobject thiz, jobject weak_thiz)
75{
76
77 // Hold onto the MediaRecorder class for use in calling the static method
78 // that posts events to the application thread.
79 jclass clazz = env->GetObjectClass(thiz);
80 if (clazz == NULL) {
Steve Block3762c312012-01-06 19:20:56 +000081 ALOGE("Can't find android/media/MediaRecorder");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080082 jniThrowException(env, "java/lang/Exception", NULL);
83 return;
84 }
85 mClass = (jclass)env->NewGlobalRef(clazz);
86
87 // We use a weak reference so the MediaRecorder object can be garbage collected.
88 // The reference is only used as a proxy for callbacks.
89 mObject = env->NewGlobalRef(weak_thiz);
90}
91
92JNIMediaRecorderListener::~JNIMediaRecorderListener()
93{
94 // remove global references
95 JNIEnv *env = AndroidRuntime::getJNIEnv();
96 env->DeleteGlobalRef(mObject);
97 env->DeleteGlobalRef(mClass);
98}
99
100void JNIMediaRecorderListener::notify(int msg, int ext1, int ext2)
101{
Steve Block71f2cf12011-10-20 11:56:00 +0100102 ALOGV("JNIMediaRecorderListener::notify");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800103
104 JNIEnv *env = AndroidRuntime::getJNIEnv();
105 env->CallStaticVoidMethod(mClass, fields.post_event, mObject, msg, ext1, ext2, 0);
106}
107
108// ----------------------------------------------------------------------------
109
110static sp<Surface> get_surface(JNIEnv* env, jobject clazz)
111{
Steve Block71f2cf12011-10-20 11:56:00 +0100112 ALOGV("get_surface");
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800113 return android_view_Surface_getSurface(env, clazz);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800114}
115
116// Returns true if it throws an exception.
117static bool process_media_recorder_call(JNIEnv *env, status_t opStatus, const char* exception, const char* message)
118{
Steve Block71f2cf12011-10-20 11:56:00 +0100119 ALOGV("process_media_recorder_call");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800120 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
Pannag Sanketicac873b2011-07-25 17:20:50 -0700130static sp<MediaRecorder> getMediaRecorder(JNIEnv* env, jobject thiz)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800131{
132 Mutex::Autolock l(sLock);
Ashok Bhat075e9a12014-01-06 13:45:09 +0000133 MediaRecorder* const p = (MediaRecorder*)env->GetLongField(thiz, fields.context);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800134 return sp<MediaRecorder>(p);
135}
136
137static sp<MediaRecorder> setMediaRecorder(JNIEnv* env, jobject thiz, const sp<MediaRecorder>& recorder)
138{
139 Mutex::Autolock l(sLock);
Ashok Bhat075e9a12014-01-06 13:45:09 +0000140 sp<MediaRecorder> old = (MediaRecorder*)env->GetLongField(thiz, fields.context);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800141 if (recorder.get()) {
142 recorder->incStrong(thiz);
143 }
144 if (old != 0) {
145 old->decStrong(thiz);
146 }
Ashok Bhat075e9a12014-01-06 13:45:09 +0000147 env->SetLongField(thiz, fields.context, (jlong)recorder.get());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800148 return old;
149}
150
151
152static void android_media_MediaRecorder_setCamera(JNIEnv* env, jobject thiz, jobject camera)
153{
James Dong429a3b52009-05-11 10:58:03 -0700154 // we should not pass a null camera to get_native_camera() call.
155 if (camera == NULL) {
Elliott Hughes15dd15f2011-04-08 17:42:34 -0700156 jniThrowNullPointerException(env, "camera object is a NULL pointer");
James Dong429a3b52009-05-11 10:58:03 -0700157 return;
158 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800159 sp<Camera> c = get_native_camera(env, camera, NULL);
Eino-Ville Talvala6a5b1db2014-07-14 12:02:21 -0700160 if (c == NULL) {
161 // get_native_camera will throw an exception in this case
162 return;
163 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800164 sp<MediaRecorder> mr = getMediaRecorder(env, thiz);
Wu-cheng Li42419ce2011-06-01 17:22:24 +0800165 process_media_recorder_call(env, mr->setCamera(c->remote(), c->getRecordingProxy()),
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800166 "java/lang/RuntimeException", "setCamera failed.");
167}
168
169static void
170android_media_MediaRecorder_setVideoSource(JNIEnv *env, jobject thiz, jint vs)
171{
Steve Block71f2cf12011-10-20 11:56:00 +0100172 ALOGV("setVideoSource(%d)", vs);
Jianhong Jiang2bcda902009-06-08 08:50:42 -0700173 if (vs < VIDEO_SOURCE_DEFAULT || vs >= VIDEO_SOURCE_LIST_END) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800174 jniThrowException(env, "java/lang/IllegalArgumentException", "Invalid video source");
175 return;
176 }
177 sp<MediaRecorder> mr = getMediaRecorder(env, thiz);
178 process_media_recorder_call(env, mr->setVideoSource(vs), "java/lang/RuntimeException", "setVideoSource failed.");
179}
180
181static void
182android_media_MediaRecorder_setAudioSource(JNIEnv *env, jobject thiz, jint as)
183{
Steve Block71f2cf12011-10-20 11:56:00 +0100184 ALOGV("setAudioSource(%d)", as);
Hochi Huang4a7873f2014-10-22 22:10:02 +0800185 if (as < AUDIO_SOURCE_DEFAULT ||
186 (as >= AUDIO_SOURCE_CNT && as != AUDIO_SOURCE_FM_TUNER)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800187 jniThrowException(env, "java/lang/IllegalArgumentException", "Invalid audio source");
188 return;
189 }
Jianhong Jiang2bcda902009-06-08 08:50:42 -0700190
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800191 sp<MediaRecorder> mr = getMediaRecorder(env, thiz);
192 process_media_recorder_call(env, mr->setAudioSource(as), "java/lang/RuntimeException", "setAudioSource failed.");
193}
194
195static void
196android_media_MediaRecorder_setOutputFormat(JNIEnv *env, jobject thiz, jint of)
197{
Steve Block71f2cf12011-10-20 11:56:00 +0100198 ALOGV("setOutputFormat(%d)", of);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800199 if (of < OUTPUT_FORMAT_DEFAULT || of >= OUTPUT_FORMAT_LIST_END) {
200 jniThrowException(env, "java/lang/IllegalArgumentException", "Invalid output format");
201 return;
202 }
203 sp<MediaRecorder> mr = getMediaRecorder(env, thiz);
204 process_media_recorder_call(env, mr->setOutputFormat(of), "java/lang/RuntimeException", "setOutputFormat failed.");
205}
206
207static void
208android_media_MediaRecorder_setVideoEncoder(JNIEnv *env, jobject thiz, jint ve)
209{
Steve Block71f2cf12011-10-20 11:56:00 +0100210 ALOGV("setVideoEncoder(%d)", ve);
Jianhong Jiang2bcda902009-06-08 08:50:42 -0700211 if (ve < VIDEO_ENCODER_DEFAULT || ve >= VIDEO_ENCODER_LIST_END) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800212 jniThrowException(env, "java/lang/IllegalArgumentException", "Invalid video encoder");
213 return;
214 }
215 sp<MediaRecorder> mr = getMediaRecorder(env, thiz);
216 process_media_recorder_call(env, mr->setVideoEncoder(ve), "java/lang/RuntimeException", "setVideoEncoder failed.");
217}
218
219static void
220android_media_MediaRecorder_setAudioEncoder(JNIEnv *env, jobject thiz, jint ae)
221{
Steve Block71f2cf12011-10-20 11:56:00 +0100222 ALOGV("setAudioEncoder(%d)", ae);
Jianhong Jiang2bcda902009-06-08 08:50:42 -0700223 if (ae < AUDIO_ENCODER_DEFAULT || ae >= AUDIO_ENCODER_LIST_END) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800224 jniThrowException(env, "java/lang/IllegalArgumentException", "Invalid audio encoder");
225 return;
226 }
227 sp<MediaRecorder> mr = getMediaRecorder(env, thiz);
228 process_media_recorder_call(env, mr->setAudioEncoder(ae), "java/lang/RuntimeException", "setAudioEncoder failed.");
229}
230
231static void
James Dong0fc6bc42010-02-26 19:36:35 -0800232android_media_MediaRecorder_setParameter(JNIEnv *env, jobject thiz, jstring params)
Jianhong Jiang2bcda902009-06-08 08:50:42 -0700233{
Steve Block71f2cf12011-10-20 11:56:00 +0100234 ALOGV("setParameter()");
Jianhong Jiang2bcda902009-06-08 08:50:42 -0700235 if (params == NULL)
236 {
Steve Block3762c312012-01-06 19:20:56 +0000237 ALOGE("Invalid or empty params string. This parameter will be ignored.");
Jianhong Jiang2bcda902009-06-08 08:50:42 -0700238 return;
239 }
240
241 sp<MediaRecorder> mr = getMediaRecorder(env, thiz);
242
243 const char* params8 = env->GetStringUTFChars(params, NULL);
244 if (params8 == NULL)
245 {
Steve Block3762c312012-01-06 19:20:56 +0000246 ALOGE("Failed to covert jstring to String8. This parameter will be ignored.");
Jianhong Jiang2bcda902009-06-08 08:50:42 -0700247 return;
248 }
249
250 process_media_recorder_call(env, mr->setParameters(String8(params8)), "java/lang/RuntimeException", "setParameter failed.");
251 env->ReleaseStringUTFChars(params,params8);
252}
253
254static void
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800255android_media_MediaRecorder_setOutputFileFD(JNIEnv *env, jobject thiz, jobject fileDescriptor, jlong offset, jlong length)
256{
Steve Block71f2cf12011-10-20 11:56:00 +0100257 ALOGV("setOutputFile");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800258 if (fileDescriptor == NULL) {
259 jniThrowException(env, "java/lang/IllegalArgumentException", NULL);
260 return;
261 }
Elliott Hughesa3804cf2011-04-11 16:50:19 -0700262 int fd = jniGetFDFromFileDescriptor(env, fileDescriptor);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800263 sp<MediaRecorder> mr = getMediaRecorder(env, thiz);
264 status_t opStatus = mr->setOutputFile(fd, offset, length);
265 process_media_recorder_call(env, opStatus, "java/io/IOException", "setOutputFile failed.");
266}
267
268static void
269android_media_MediaRecorder_setVideoSize(JNIEnv *env, jobject thiz, jint width, jint height)
270{
Steve Block71f2cf12011-10-20 11:56:00 +0100271 ALOGV("setVideoSize(%d, %d)", width, height);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800272 sp<MediaRecorder> mr = getMediaRecorder(env, thiz);
273
274 if (width <= 0 || height <= 0) {
275 jniThrowException(env, "java/lang/IllegalArgumentException", "invalid video size");
276 return;
277 }
278 process_media_recorder_call(env, mr->setVideoSize(width, height), "java/lang/RuntimeException", "setVideoSize failed.");
279}
280
281static void
282android_media_MediaRecorder_setVideoFrameRate(JNIEnv *env, jobject thiz, jint rate)
283{
Steve Block71f2cf12011-10-20 11:56:00 +0100284 ALOGV("setVideoFrameRate(%d)", rate);
James Dongcbe7c542009-07-30 11:16:11 -0700285 if (rate <= 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800286 jniThrowException(env, "java/lang/IllegalArgumentException", "invalid frame rate");
287 return;
288 }
289 sp<MediaRecorder> mr = getMediaRecorder(env, thiz);
290 process_media_recorder_call(env, mr->setVideoFrameRate(rate), "java/lang/RuntimeException", "setVideoFrameRate failed.");
291}
292
293static void
The Android Open Source Projectba87e3e2009-03-13 13:04:22 -0700294android_media_MediaRecorder_setMaxDuration(JNIEnv *env, jobject thiz, jint max_duration_ms)
295{
Steve Block71f2cf12011-10-20 11:56:00 +0100296 ALOGV("setMaxDuration(%d)", max_duration_ms);
The Android Open Source Projectba87e3e2009-03-13 13:04:22 -0700297 sp<MediaRecorder> mr = getMediaRecorder(env, thiz);
298
299 char params[64];
300 sprintf(params, "max-duration=%d", max_duration_ms);
301
302 process_media_recorder_call(env, mr->setParameters(String8(params)), "java/lang/RuntimeException", "setMaxDuration failed.");
303}
304
305static void
The Android Open Source Project10592532009-03-18 17:39:46 -0700306android_media_MediaRecorder_setMaxFileSize(
307 JNIEnv *env, jobject thiz, jlong max_filesize_bytes)
308{
Steve Block71f2cf12011-10-20 11:56:00 +0100309 ALOGV("setMaxFileSize(%lld)", max_filesize_bytes);
The Android Open Source Project10592532009-03-18 17:39:46 -0700310 sp<MediaRecorder> mr = getMediaRecorder(env, thiz);
311
312 char params[64];
Mark Salyzyn85394032014-04-16 10:28:37 -0700313 sprintf(params, "max-filesize=%" PRId64, max_filesize_bytes);
The Android Open Source Project10592532009-03-18 17:39:46 -0700314
315 process_media_recorder_call(env, mr->setParameters(String8(params)), "java/lang/RuntimeException", "setMaxFileSize failed.");
316}
317
318static void
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800319android_media_MediaRecorder_prepare(JNIEnv *env, jobject thiz)
320{
Steve Block71f2cf12011-10-20 11:56:00 +0100321 ALOGV("prepare");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800322 sp<MediaRecorder> mr = getMediaRecorder(env, thiz);
323
324 jobject surface = env->GetObjectField(thiz, fields.surface);
325 if (surface != NULL) {
James Dong457d5ae2010-06-09 10:19:33 -0700326 const sp<Surface> native_surface = get_surface(env, surface);
James Dongd790c6482010-08-12 10:18:01 -0700327
328 // The application may misbehave and
329 // the preview surface becomes unavailable
330 if (native_surface.get() == 0) {
Steve Block3762c312012-01-06 19:20:56 +0000331 ALOGE("Application lost the surface");
James Dongd790c6482010-08-12 10:18:01 -0700332 jniThrowException(env, "java/io/IOException", "invalid preview surface");
333 return;
334 }
335
Mathias Agopianbfcae352013-02-14 15:32:50 -0800336 ALOGI("prepare: surface=%p", native_surface.get());
Mathias Agopian4a05f432013-03-12 18:43:34 -0700337 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 -0800338 return;
339 }
340 }
341 process_media_recorder_call(env, mr->prepare(), "java/io/IOException", "prepare failed.");
342}
343
Ashok Bhat075e9a12014-01-06 13:45:09 +0000344static jint
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800345android_media_MediaRecorder_native_getMaxAmplitude(JNIEnv *env, jobject thiz)
346{
Steve Block71f2cf12011-10-20 11:56:00 +0100347 ALOGV("getMaxAmplitude");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800348 sp<MediaRecorder> mr = getMediaRecorder(env, thiz);
349 int result = 0;
350 process_media_recorder_call(env, mr->getMaxAmplitude(&result), "java/lang/RuntimeException", "getMaxAmplitude failed.");
Ashok Bhat075e9a12014-01-06 13:45:09 +0000351 return (jint) result;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800352}
353
Chong Zhang83cc9942014-01-02 12:10:04 -0800354static jobject
355android_media_MediaRecorder_getSurface(JNIEnv *env, jobject thiz)
356{
357 ALOGV("getSurface");
358 sp<MediaRecorder> mr = getMediaRecorder(env, thiz);
359
360 sp<IGraphicBufferProducer> bufferProducer = mr->querySurfaceMediaSourceFromMediaServer();
361 if (bufferProducer == NULL) {
362 jniThrowException(
363 env,
364 "java/lang/IllegalStateException",
365 "failed to get surface");
366 return NULL;
367 }
368
369 // Wrap the IGBP in a Java-language Surface.
370 return android_view_Surface_createFromIGraphicBufferProducer(env,
371 bufferProducer);
372}
373
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800374static void
375android_media_MediaRecorder_start(JNIEnv *env, jobject thiz)
376{
Steve Block71f2cf12011-10-20 11:56:00 +0100377 ALOGV("start");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800378 sp<MediaRecorder> mr = getMediaRecorder(env, thiz);
379 process_media_recorder_call(env, mr->start(), "java/lang/RuntimeException", "start failed.");
380}
381
382static void
383android_media_MediaRecorder_stop(JNIEnv *env, jobject thiz)
384{
Steve Block71f2cf12011-10-20 11:56:00 +0100385 ALOGV("stop");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800386 sp<MediaRecorder> mr = getMediaRecorder(env, thiz);
387 process_media_recorder_call(env, mr->stop(), "java/lang/RuntimeException", "stop failed.");
388}
389
390static void
391android_media_MediaRecorder_native_reset(JNIEnv *env, jobject thiz)
392{
Steve Block71f2cf12011-10-20 11:56:00 +0100393 ALOGV("native_reset");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800394 sp<MediaRecorder> mr = getMediaRecorder(env, thiz);
395 process_media_recorder_call(env, mr->reset(), "java/lang/RuntimeException", "native_reset failed.");
396}
397
398static void
399android_media_MediaRecorder_release(JNIEnv *env, jobject thiz)
400{
Steve Block71f2cf12011-10-20 11:56:00 +0100401 ALOGV("release");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800402 sp<MediaRecorder> mr = setMediaRecorder(env, thiz, 0);
403 if (mr != NULL) {
404 mr->setListener(NULL);
The Android Open Source Project4df24232009-03-05 14:34:35 -0800405 mr->release();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800406 }
407}
408
Marco Nelissen4935d052009-08-03 11:12:58 -0700409// This function gets some field IDs, which in turn causes class initialization.
410// It is called from a static block in MediaRecorder, which won't run until the
411// first time an instance of this class is used.
412static void
413android_media_MediaRecorder_native_init(JNIEnv *env)
414{
415 jclass clazz;
416
417 clazz = env->FindClass("android/media/MediaRecorder");
418 if (clazz == NULL) {
Marco Nelissen4935d052009-08-03 11:12:58 -0700419 return;
420 }
421
Ashok Bhat075e9a12014-01-06 13:45:09 +0000422 fields.context = env->GetFieldID(clazz, "mNativeContext", "J");
Marco Nelissen4935d052009-08-03 11:12:58 -0700423 if (fields.context == NULL) {
Marco Nelissen4935d052009-08-03 11:12:58 -0700424 return;
425 }
426
427 fields.surface = env->GetFieldID(clazz, "mSurface", "Landroid/view/Surface;");
428 if (fields.surface == NULL) {
Marco Nelissen4935d052009-08-03 11:12:58 -0700429 return;
430 }
431
432 jclass surface = env->FindClass("android/view/Surface");
433 if (surface == NULL) {
Marco Nelissen4935d052009-08-03 11:12:58 -0700434 return;
435 }
436
Marco Nelissen4935d052009-08-03 11:12:58 -0700437 fields.post_event = env->GetStaticMethodID(clazz, "postEventFromNative",
438 "(Ljava/lang/Object;IIILjava/lang/Object;)V");
439 if (fields.post_event == NULL) {
Marco Nelissen4935d052009-08-03 11:12:58 -0700440 return;
441 }
442}
443
444
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800445static void
Eino-Ville Talvala788717c2013-02-15 18:30:15 -0800446android_media_MediaRecorder_native_setup(JNIEnv *env, jobject thiz, jobject weak_this,
447 jstring packageName)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800448{
Steve Block71f2cf12011-10-20 11:56:00 +0100449 ALOGV("setup");
Eino-Ville Talvala788717c2013-02-15 18:30:15 -0800450
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800451 sp<MediaRecorder> mr = new MediaRecorder();
452 if (mr == NULL) {
453 jniThrowException(env, "java/lang/RuntimeException", "Out of memory");
454 return;
455 }
456 if (mr->initCheck() != NO_ERROR) {
James Dong8d3b9102010-09-09 18:43:48 -0700457 jniThrowException(env, "java/lang/RuntimeException", "Unable to initialize media recorder");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800458 return;
459 }
460
461 // create new listener and give it to MediaRecorder
462 sp<JNIMediaRecorderListener> listener = new JNIMediaRecorderListener(env, thiz, weak_this);
463 mr->setListener(listener);
464
Eino-Ville Talvala788717c2013-02-15 18:30:15 -0800465 // Convert client name jstring to String16
466 const char16_t *rawClientName = env->GetStringChars(packageName, NULL);
467 jsize rawClientNameLen = env->GetStringLength(packageName);
468 String16 clientName(rawClientName, rawClientNameLen);
469 env->ReleaseStringChars(packageName, rawClientName);
470
471 // pass client package name for permissions tracking
472 mr->setClientName(clientName);
473
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800474 setMediaRecorder(env, thiz, mr);
475}
476
477static void
478android_media_MediaRecorder_native_finalize(JNIEnv *env, jobject thiz)
479{
Steve Block71f2cf12011-10-20 11:56:00 +0100480 ALOGV("finalize");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800481 android_media_MediaRecorder_release(env, thiz);
482}
483
484// ----------------------------------------------------------------------------
485
486static JNINativeMethod gMethods[] = {
487 {"setCamera", "(Landroid/hardware/Camera;)V", (void *)android_media_MediaRecorder_setCamera},
488 {"setVideoSource", "(I)V", (void *)android_media_MediaRecorder_setVideoSource},
489 {"setAudioSource", "(I)V", (void *)android_media_MediaRecorder_setAudioSource},
490 {"setOutputFormat", "(I)V", (void *)android_media_MediaRecorder_setOutputFormat},
491 {"setVideoEncoder", "(I)V", (void *)android_media_MediaRecorder_setVideoEncoder},
492 {"setAudioEncoder", "(I)V", (void *)android_media_MediaRecorder_setAudioEncoder},
James Dong0fc6bc42010-02-26 19:36:35 -0800493 {"setParameter", "(Ljava/lang/String;)V", (void *)android_media_MediaRecorder_setParameter},
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800494 {"_setOutputFile", "(Ljava/io/FileDescriptor;JJ)V", (void *)android_media_MediaRecorder_setOutputFileFD},
495 {"setVideoSize", "(II)V", (void *)android_media_MediaRecorder_setVideoSize},
496 {"setVideoFrameRate", "(I)V", (void *)android_media_MediaRecorder_setVideoFrameRate},
The Android Open Source Projectba87e3e2009-03-13 13:04:22 -0700497 {"setMaxDuration", "(I)V", (void *)android_media_MediaRecorder_setMaxDuration},
The Android Open Source Project10592532009-03-18 17:39:46 -0700498 {"setMaxFileSize", "(J)V", (void *)android_media_MediaRecorder_setMaxFileSize},
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800499 {"_prepare", "()V", (void *)android_media_MediaRecorder_prepare},
Chong Zhang83cc9942014-01-02 12:10:04 -0800500 {"getSurface", "()Landroid/view/Surface;", (void *)android_media_MediaRecorder_getSurface},
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800501 {"getMaxAmplitude", "()I", (void *)android_media_MediaRecorder_native_getMaxAmplitude},
502 {"start", "()V", (void *)android_media_MediaRecorder_start},
503 {"stop", "()V", (void *)android_media_MediaRecorder_stop},
Marco Nelissen4935d052009-08-03 11:12:58 -0700504 {"native_reset", "()V", (void *)android_media_MediaRecorder_native_reset},
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800505 {"release", "()V", (void *)android_media_MediaRecorder_release},
Marco Nelissen4935d052009-08-03 11:12:58 -0700506 {"native_init", "()V", (void *)android_media_MediaRecorder_native_init},
Eino-Ville Talvala788717c2013-02-15 18:30:15 -0800507 {"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 -0800508 {"native_finalize", "()V", (void *)android_media_MediaRecorder_native_finalize},
509};
510
511static const char* const kClassPathName = "android/media/MediaRecorder";
512
Marco Nelissen4935d052009-08-03 11:12:58 -0700513// This function only registers the native methods, and is called from
514// JNI_OnLoad in android_media_MediaPlayer.cpp
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800515int register_android_media_MediaRecorder(JNIEnv *env)
516{
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800517 return AndroidRuntime::registerNativeMethods(env,
518 "android/media/MediaRecorder", gMethods, NELEM(gMethods));
519}