The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 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 | |
Mathias Agopian | 8335f1c | 2012-02-25 18:48:35 -0800 | [diff] [blame] | 21 | #include <gui/Surface.h> |
Mathias Agopian | 000479f | 2010-02-09 17:46:37 -0800 | [diff] [blame] | 22 | #include <camera/ICameraService.h> |
| 23 | #include <camera/Camera.h> |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 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 | |
Dima Zavin | 34bb419 | 2011-05-11 14:15:23 -0700 | [diff] [blame] | 36 | #include <system/audio.h> |
Mathias Agopian | 3866f0d | 2013-02-11 22:08:48 -0800 | [diff] [blame] | 37 | #include <android_runtime/android_view_Surface.h> |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 38 | |
| 39 | // ---------------------------------------------------------------------------- |
| 40 | |
| 41 | using namespace android; |
| 42 | |
| 43 | // ---------------------------------------------------------------------------- |
| 44 | |
| 45 | // helper function to extract a native Camera object from a Camera Java object |
Dave Sparks | 5e27115 | 2009-06-23 17:30:11 -0700 | [diff] [blame] | 46 | extern sp<Camera> get_native_camera(JNIEnv *env, jobject thiz, struct JNICameraContext** context); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 47 | |
| 48 | struct fields_t { |
| 49 | jfieldID context; |
| 50 | jfieldID surface; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 51 | |
| 52 | jmethodID post_event; |
| 53 | }; |
| 54 | static fields_t fields; |
| 55 | |
| 56 | static Mutex sLock; |
| 57 | |
| 58 | // ---------------------------------------------------------------------------- |
| 59 | // ref-counted object for callbacks |
| 60 | class JNIMediaRecorderListener: public MediaRecorderListener |
| 61 | { |
| 62 | public: |
| 63 | JNIMediaRecorderListener(JNIEnv* env, jobject thiz, jobject weak_thiz); |
| 64 | ~JNIMediaRecorderListener(); |
| 65 | void notify(int msg, int ext1, int ext2); |
| 66 | private: |
| 67 | JNIMediaRecorderListener(); |
| 68 | jclass mClass; // Reference to MediaRecorder class |
| 69 | jobject mObject; // Weak ref to MediaRecorder Java object to call on |
| 70 | }; |
| 71 | |
| 72 | JNIMediaRecorderListener::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 Block | 3762c31 | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 79 | ALOGE("Can't find android/media/MediaRecorder"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 80 | 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 | |
| 90 | JNIMediaRecorderListener::~JNIMediaRecorderListener() |
| 91 | { |
| 92 | // remove global references |
| 93 | JNIEnv *env = AndroidRuntime::getJNIEnv(); |
| 94 | env->DeleteGlobalRef(mObject); |
| 95 | env->DeleteGlobalRef(mClass); |
| 96 | } |
| 97 | |
| 98 | void JNIMediaRecorderListener::notify(int msg, int ext1, int ext2) |
| 99 | { |
Steve Block | 71f2cf1 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 100 | ALOGV("JNIMediaRecorderListener::notify"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 101 | |
| 102 | JNIEnv *env = AndroidRuntime::getJNIEnv(); |
| 103 | env->CallStaticVoidMethod(mClass, fields.post_event, mObject, msg, ext1, ext2, 0); |
| 104 | } |
| 105 | |
| 106 | // ---------------------------------------------------------------------------- |
| 107 | |
| 108 | static sp<Surface> get_surface(JNIEnv* env, jobject clazz) |
| 109 | { |
Steve Block | 71f2cf1 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 110 | ALOGV("get_surface"); |
Mathias Agopian | 3866f0d | 2013-02-11 22:08:48 -0800 | [diff] [blame] | 111 | return android_view_Surface_getSurface(env, clazz); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 112 | } |
| 113 | |
| 114 | // Returns true if it throws an exception. |
| 115 | static bool process_media_recorder_call(JNIEnv *env, status_t opStatus, const char* exception, const char* message) |
| 116 | { |
Steve Block | 71f2cf1 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 117 | ALOGV("process_media_recorder_call"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 118 | 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 Sanketi | cac873b | 2011-07-25 17:20:50 -0700 | [diff] [blame] | 128 | static sp<MediaRecorder> getMediaRecorder(JNIEnv* env, jobject thiz) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 129 | { |
| 130 | Mutex::Autolock l(sLock); |
Ashok Bhat | 075e9a1 | 2014-01-06 13:45:09 +0000 | [diff] [blame] | 131 | MediaRecorder* const p = (MediaRecorder*)env->GetLongField(thiz, fields.context); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 132 | return sp<MediaRecorder>(p); |
| 133 | } |
| 134 | |
| 135 | static sp<MediaRecorder> setMediaRecorder(JNIEnv* env, jobject thiz, const sp<MediaRecorder>& recorder) |
| 136 | { |
| 137 | Mutex::Autolock l(sLock); |
Ashok Bhat | 075e9a1 | 2014-01-06 13:45:09 +0000 | [diff] [blame] | 138 | sp<MediaRecorder> old = (MediaRecorder*)env->GetLongField(thiz, fields.context); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 139 | if (recorder.get()) { |
| 140 | recorder->incStrong(thiz); |
| 141 | } |
| 142 | if (old != 0) { |
| 143 | old->decStrong(thiz); |
| 144 | } |
Ashok Bhat | 075e9a1 | 2014-01-06 13:45:09 +0000 | [diff] [blame] | 145 | env->SetLongField(thiz, fields.context, (jlong)recorder.get()); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 146 | return old; |
| 147 | } |
| 148 | |
| 149 | |
| 150 | static void android_media_MediaRecorder_setCamera(JNIEnv* env, jobject thiz, jobject camera) |
| 151 | { |
James Dong | 429a3b5 | 2009-05-11 10:58:03 -0700 | [diff] [blame] | 152 | // we should not pass a null camera to get_native_camera() call. |
| 153 | if (camera == NULL) { |
Elliott Hughes | 15dd15f | 2011-04-08 17:42:34 -0700 | [diff] [blame] | 154 | jniThrowNullPointerException(env, "camera object is a NULL pointer"); |
James Dong | 429a3b5 | 2009-05-11 10:58:03 -0700 | [diff] [blame] | 155 | return; |
| 156 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 157 | sp<Camera> c = get_native_camera(env, camera, NULL); |
| 158 | sp<MediaRecorder> mr = getMediaRecorder(env, thiz); |
Wu-cheng Li | 42419ce | 2011-06-01 17:22:24 +0800 | [diff] [blame] | 159 | process_media_recorder_call(env, mr->setCamera(c->remote(), c->getRecordingProxy()), |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 160 | "java/lang/RuntimeException", "setCamera failed."); |
| 161 | } |
| 162 | |
| 163 | static void |
| 164 | android_media_MediaRecorder_setVideoSource(JNIEnv *env, jobject thiz, jint vs) |
| 165 | { |
Steve Block | 71f2cf1 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 166 | ALOGV("setVideoSource(%d)", vs); |
Jianhong Jiang | 2bcda90 | 2009-06-08 08:50:42 -0700 | [diff] [blame] | 167 | if (vs < VIDEO_SOURCE_DEFAULT || vs >= VIDEO_SOURCE_LIST_END) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 168 | 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 | |
| 175 | static void |
| 176 | android_media_MediaRecorder_setAudioSource(JNIEnv *env, jobject thiz, jint as) |
| 177 | { |
Steve Block | 71f2cf1 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 178 | ALOGV("setAudioSource(%d)", as); |
Dima Zavin | 24fc2fb | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 179 | if (as < AUDIO_SOURCE_DEFAULT || as >= AUDIO_SOURCE_CNT) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 180 | jniThrowException(env, "java/lang/IllegalArgumentException", "Invalid audio source"); |
| 181 | return; |
| 182 | } |
Jianhong Jiang | 2bcda90 | 2009-06-08 08:50:42 -0700 | [diff] [blame] | 183 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 184 | sp<MediaRecorder> mr = getMediaRecorder(env, thiz); |
| 185 | process_media_recorder_call(env, mr->setAudioSource(as), "java/lang/RuntimeException", "setAudioSource failed."); |
| 186 | } |
| 187 | |
| 188 | static void |
| 189 | android_media_MediaRecorder_setOutputFormat(JNIEnv *env, jobject thiz, jint of) |
| 190 | { |
Steve Block | 71f2cf1 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 191 | ALOGV("setOutputFormat(%d)", of); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 192 | 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 | |
| 200 | static void |
| 201 | android_media_MediaRecorder_setVideoEncoder(JNIEnv *env, jobject thiz, jint ve) |
| 202 | { |
Steve Block | 71f2cf1 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 203 | ALOGV("setVideoEncoder(%d)", ve); |
Jianhong Jiang | 2bcda90 | 2009-06-08 08:50:42 -0700 | [diff] [blame] | 204 | if (ve < VIDEO_ENCODER_DEFAULT || ve >= VIDEO_ENCODER_LIST_END) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 205 | 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 | |
| 212 | static void |
| 213 | android_media_MediaRecorder_setAudioEncoder(JNIEnv *env, jobject thiz, jint ae) |
| 214 | { |
Steve Block | 71f2cf1 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 215 | ALOGV("setAudioEncoder(%d)", ae); |
Jianhong Jiang | 2bcda90 | 2009-06-08 08:50:42 -0700 | [diff] [blame] | 216 | if (ae < AUDIO_ENCODER_DEFAULT || ae >= AUDIO_ENCODER_LIST_END) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 217 | 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 | |
| 224 | static void |
James Dong | 0fc6bc4 | 2010-02-26 19:36:35 -0800 | [diff] [blame] | 225 | android_media_MediaRecorder_setParameter(JNIEnv *env, jobject thiz, jstring params) |
Jianhong Jiang | 2bcda90 | 2009-06-08 08:50:42 -0700 | [diff] [blame] | 226 | { |
Steve Block | 71f2cf1 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 227 | ALOGV("setParameter()"); |
Jianhong Jiang | 2bcda90 | 2009-06-08 08:50:42 -0700 | [diff] [blame] | 228 | if (params == NULL) |
| 229 | { |
Steve Block | 3762c31 | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 230 | ALOGE("Invalid or empty params string. This parameter will be ignored."); |
Jianhong Jiang | 2bcda90 | 2009-06-08 08:50:42 -0700 | [diff] [blame] | 231 | 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 Block | 3762c31 | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 239 | ALOGE("Failed to covert jstring to String8. This parameter will be ignored."); |
Jianhong Jiang | 2bcda90 | 2009-06-08 08:50:42 -0700 | [diff] [blame] | 240 | 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 | |
| 247 | static void |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 248 | android_media_MediaRecorder_setOutputFileFD(JNIEnv *env, jobject thiz, jobject fileDescriptor, jlong offset, jlong length) |
| 249 | { |
Steve Block | 71f2cf1 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 250 | ALOGV("setOutputFile"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 251 | if (fileDescriptor == NULL) { |
| 252 | jniThrowException(env, "java/lang/IllegalArgumentException", NULL); |
| 253 | return; |
| 254 | } |
Elliott Hughes | a3804cf | 2011-04-11 16:50:19 -0700 | [diff] [blame] | 255 | int fd = jniGetFDFromFileDescriptor(env, fileDescriptor); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 256 | 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 | |
| 261 | static void |
| 262 | android_media_MediaRecorder_setVideoSize(JNIEnv *env, jobject thiz, jint width, jint height) |
| 263 | { |
Steve Block | 71f2cf1 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 264 | ALOGV("setVideoSize(%d, %d)", width, height); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 265 | 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 | |
| 274 | static void |
| 275 | android_media_MediaRecorder_setVideoFrameRate(JNIEnv *env, jobject thiz, jint rate) |
| 276 | { |
Steve Block | 71f2cf1 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 277 | ALOGV("setVideoFrameRate(%d)", rate); |
James Dong | cbe7c54 | 2009-07-30 11:16:11 -0700 | [diff] [blame] | 278 | if (rate <= 0) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 279 | 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 | |
| 286 | static void |
The Android Open Source Project | ba87e3e | 2009-03-13 13:04:22 -0700 | [diff] [blame] | 287 | android_media_MediaRecorder_setMaxDuration(JNIEnv *env, jobject thiz, jint max_duration_ms) |
| 288 | { |
Steve Block | 71f2cf1 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 289 | ALOGV("setMaxDuration(%d)", max_duration_ms); |
The Android Open Source Project | ba87e3e | 2009-03-13 13:04:22 -0700 | [diff] [blame] | 290 | 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 | |
| 298 | static void |
The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 299 | android_media_MediaRecorder_setMaxFileSize( |
| 300 | JNIEnv *env, jobject thiz, jlong max_filesize_bytes) |
| 301 | { |
Steve Block | 71f2cf1 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 302 | ALOGV("setMaxFileSize(%lld)", max_filesize_bytes); |
The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 303 | 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 | |
| 311 | static void |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 312 | android_media_MediaRecorder_prepare(JNIEnv *env, jobject thiz) |
| 313 | { |
Steve Block | 71f2cf1 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 314 | ALOGV("prepare"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 315 | sp<MediaRecorder> mr = getMediaRecorder(env, thiz); |
| 316 | |
| 317 | jobject surface = env->GetObjectField(thiz, fields.surface); |
| 318 | if (surface != NULL) { |
James Dong | 457d5ae | 2010-06-09 10:19:33 -0700 | [diff] [blame] | 319 | const sp<Surface> native_surface = get_surface(env, surface); |
James Dong | d790c648 | 2010-08-12 10:18:01 -0700 | [diff] [blame] | 320 | |
| 321 | // The application may misbehave and |
| 322 | // the preview surface becomes unavailable |
| 323 | if (native_surface.get() == 0) { |
Steve Block | 3762c31 | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 324 | ALOGE("Application lost the surface"); |
James Dong | d790c648 | 2010-08-12 10:18:01 -0700 | [diff] [blame] | 325 | jniThrowException(env, "java/io/IOException", "invalid preview surface"); |
| 326 | return; |
| 327 | } |
| 328 | |
Mathias Agopian | bfcae35 | 2013-02-14 15:32:50 -0800 | [diff] [blame] | 329 | ALOGI("prepare: surface=%p", native_surface.get()); |
Mathias Agopian | 4a05f43 | 2013-03-12 18:43:34 -0700 | [diff] [blame] | 330 | if (process_media_recorder_call(env, mr->setPreviewSurface(native_surface->getIGraphicBufferProducer()), "java/lang/RuntimeException", "setPreviewSurface failed.")) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 331 | return; |
| 332 | } |
| 333 | } |
| 334 | process_media_recorder_call(env, mr->prepare(), "java/io/IOException", "prepare failed."); |
| 335 | } |
| 336 | |
Ashok Bhat | 075e9a1 | 2014-01-06 13:45:09 +0000 | [diff] [blame] | 337 | static jint |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 338 | android_media_MediaRecorder_native_getMaxAmplitude(JNIEnv *env, jobject thiz) |
| 339 | { |
Steve Block | 71f2cf1 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 340 | ALOGV("getMaxAmplitude"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 341 | 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 Bhat | 075e9a1 | 2014-01-06 13:45:09 +0000 | [diff] [blame] | 344 | return (jint) result; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 345 | } |
| 346 | |
Chong Zhang | 83cc994 | 2014-01-02 12:10:04 -0800 | [diff] [blame] | 347 | static jobject |
| 348 | android_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 Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 367 | static void |
| 368 | android_media_MediaRecorder_start(JNIEnv *env, jobject thiz) |
| 369 | { |
Steve Block | 71f2cf1 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 370 | ALOGV("start"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 371 | sp<MediaRecorder> mr = getMediaRecorder(env, thiz); |
| 372 | process_media_recorder_call(env, mr->start(), "java/lang/RuntimeException", "start failed."); |
| 373 | } |
| 374 | |
| 375 | static void |
| 376 | android_media_MediaRecorder_stop(JNIEnv *env, jobject thiz) |
| 377 | { |
Steve Block | 71f2cf1 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 378 | ALOGV("stop"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 379 | sp<MediaRecorder> mr = getMediaRecorder(env, thiz); |
| 380 | process_media_recorder_call(env, mr->stop(), "java/lang/RuntimeException", "stop failed."); |
| 381 | } |
| 382 | |
| 383 | static void |
| 384 | android_media_MediaRecorder_native_reset(JNIEnv *env, jobject thiz) |
| 385 | { |
Steve Block | 71f2cf1 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 386 | ALOGV("native_reset"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 387 | sp<MediaRecorder> mr = getMediaRecorder(env, thiz); |
| 388 | process_media_recorder_call(env, mr->reset(), "java/lang/RuntimeException", "native_reset failed."); |
| 389 | } |
| 390 | |
| 391 | static void |
| 392 | android_media_MediaRecorder_release(JNIEnv *env, jobject thiz) |
| 393 | { |
Steve Block | 71f2cf1 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 394 | ALOGV("release"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 395 | sp<MediaRecorder> mr = setMediaRecorder(env, thiz, 0); |
| 396 | if (mr != NULL) { |
| 397 | mr->setListener(NULL); |
The Android Open Source Project | 4df2423 | 2009-03-05 14:34:35 -0800 | [diff] [blame] | 398 | mr->release(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 399 | } |
| 400 | } |
| 401 | |
Marco Nelissen | 4935d05 | 2009-08-03 11:12:58 -0700 | [diff] [blame] | 402 | // 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. |
| 405 | static void |
| 406 | android_media_MediaRecorder_native_init(JNIEnv *env) |
| 407 | { |
| 408 | jclass clazz; |
| 409 | |
| 410 | clazz = env->FindClass("android/media/MediaRecorder"); |
| 411 | if (clazz == NULL) { |
Marco Nelissen | 4935d05 | 2009-08-03 11:12:58 -0700 | [diff] [blame] | 412 | return; |
| 413 | } |
| 414 | |
Ashok Bhat | 075e9a1 | 2014-01-06 13:45:09 +0000 | [diff] [blame] | 415 | fields.context = env->GetFieldID(clazz, "mNativeContext", "J"); |
Marco Nelissen | 4935d05 | 2009-08-03 11:12:58 -0700 | [diff] [blame] | 416 | if (fields.context == NULL) { |
Marco Nelissen | 4935d05 | 2009-08-03 11:12:58 -0700 | [diff] [blame] | 417 | return; |
| 418 | } |
| 419 | |
| 420 | fields.surface = env->GetFieldID(clazz, "mSurface", "Landroid/view/Surface;"); |
| 421 | if (fields.surface == NULL) { |
Marco Nelissen | 4935d05 | 2009-08-03 11:12:58 -0700 | [diff] [blame] | 422 | return; |
| 423 | } |
| 424 | |
| 425 | jclass surface = env->FindClass("android/view/Surface"); |
| 426 | if (surface == NULL) { |
Marco Nelissen | 4935d05 | 2009-08-03 11:12:58 -0700 | [diff] [blame] | 427 | return; |
| 428 | } |
| 429 | |
Marco Nelissen | 4935d05 | 2009-08-03 11:12:58 -0700 | [diff] [blame] | 430 | fields.post_event = env->GetStaticMethodID(clazz, "postEventFromNative", |
| 431 | "(Ljava/lang/Object;IIILjava/lang/Object;)V"); |
| 432 | if (fields.post_event == NULL) { |
Marco Nelissen | 4935d05 | 2009-08-03 11:12:58 -0700 | [diff] [blame] | 433 | return; |
| 434 | } |
| 435 | } |
| 436 | |
| 437 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 438 | static void |
Eino-Ville Talvala | 788717c | 2013-02-15 18:30:15 -0800 | [diff] [blame] | 439 | android_media_MediaRecorder_native_setup(JNIEnv *env, jobject thiz, jobject weak_this, |
| 440 | jstring packageName) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 441 | { |
Steve Block | 71f2cf1 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 442 | ALOGV("setup"); |
Eino-Ville Talvala | 788717c | 2013-02-15 18:30:15 -0800 | [diff] [blame] | 443 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 444 | 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 Dong | 8d3b910 | 2010-09-09 18:43:48 -0700 | [diff] [blame] | 450 | jniThrowException(env, "java/lang/RuntimeException", "Unable to initialize media recorder"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 451 | 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 Talvala | 788717c | 2013-02-15 18:30:15 -0800 | [diff] [blame] | 458 | // 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 Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 467 | setMediaRecorder(env, thiz, mr); |
| 468 | } |
| 469 | |
| 470 | static void |
| 471 | android_media_MediaRecorder_native_finalize(JNIEnv *env, jobject thiz) |
| 472 | { |
Steve Block | 71f2cf1 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 473 | ALOGV("finalize"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 474 | android_media_MediaRecorder_release(env, thiz); |
| 475 | } |
| 476 | |
| 477 | // ---------------------------------------------------------------------------- |
| 478 | |
| 479 | static 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 Dong | 0fc6bc4 | 2010-02-26 19:36:35 -0800 | [diff] [blame] | 486 | {"setParameter", "(Ljava/lang/String;)V", (void *)android_media_MediaRecorder_setParameter}, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 487 | {"_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 Project | ba87e3e | 2009-03-13 13:04:22 -0700 | [diff] [blame] | 490 | {"setMaxDuration", "(I)V", (void *)android_media_MediaRecorder_setMaxDuration}, |
The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 491 | {"setMaxFileSize", "(J)V", (void *)android_media_MediaRecorder_setMaxFileSize}, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 492 | {"_prepare", "()V", (void *)android_media_MediaRecorder_prepare}, |
Chong Zhang | 83cc994 | 2014-01-02 12:10:04 -0800 | [diff] [blame] | 493 | {"getSurface", "()Landroid/view/Surface;", (void *)android_media_MediaRecorder_getSurface}, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 494 | {"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 Nelissen | 4935d05 | 2009-08-03 11:12:58 -0700 | [diff] [blame] | 497 | {"native_reset", "()V", (void *)android_media_MediaRecorder_native_reset}, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 498 | {"release", "()V", (void *)android_media_MediaRecorder_release}, |
Marco Nelissen | 4935d05 | 2009-08-03 11:12:58 -0700 | [diff] [blame] | 499 | {"native_init", "()V", (void *)android_media_MediaRecorder_native_init}, |
Eino-Ville Talvala | 788717c | 2013-02-15 18:30:15 -0800 | [diff] [blame] | 500 | {"native_setup", "(Ljava/lang/Object;Ljava/lang/String;)V", (void *)android_media_MediaRecorder_native_setup}, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 501 | {"native_finalize", "()V", (void *)android_media_MediaRecorder_native_finalize}, |
| 502 | }; |
| 503 | |
| 504 | static const char* const kClassPathName = "android/media/MediaRecorder"; |
| 505 | |
Marco Nelissen | 4935d05 | 2009-08-03 11:12:58 -0700 | [diff] [blame] | 506 | // This function only registers the native methods, and is called from |
| 507 | // JNI_OnLoad in android_media_MediaPlayer.cpp |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 508 | int register_android_media_MediaRecorder(JNIEnv *env) |
| 509 | { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 510 | return AndroidRuntime::registerNativeMethods(env, |
| 511 | "android/media/MediaRecorder", gMethods, NELEM(gMethods)); |
| 512 | } |