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 | |
Mark Salyzyn | 8539403 | 2014-04-16 10:28:37 -0700 | [diff] [blame] | 17 | #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 Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 24 | //#define LOG_NDEBUG 0 |
| 25 | #define LOG_TAG "MediaRecorderJNI" |
| 26 | #include <utils/Log.h> |
| 27 | |
Mathias Agopian | 8335f1c | 2012-02-25 18:48:35 -0800 | [diff] [blame] | 28 | #include <gui/Surface.h> |
Mathias Agopian | 000479f | 2010-02-09 17:46:37 -0800 | [diff] [blame] | 29 | #include <camera/ICameraService.h> |
| 30 | #include <camera/Camera.h> |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 31 | #include <media/mediarecorder.h> |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 32 | #include <utils/threads.h> |
| 33 | |
| 34 | #include "jni.h" |
| 35 | #include "JNIHelp.h" |
| 36 | #include "android_runtime/AndroidRuntime.h" |
| 37 | |
Dima Zavin | 34bb419 | 2011-05-11 14:15:23 -0700 | [diff] [blame] | 38 | #include <system/audio.h> |
Mathias Agopian | 3866f0d | 2013-02-11 22:08:48 -0800 | [diff] [blame] | 39 | #include <android_runtime/android_view_Surface.h> |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 40 | |
| 41 | // ---------------------------------------------------------------------------- |
| 42 | |
| 43 | using namespace android; |
| 44 | |
| 45 | // ---------------------------------------------------------------------------- |
| 46 | |
| 47 | // 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] | 48 | 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] | 49 | |
| 50 | struct fields_t { |
| 51 | jfieldID context; |
| 52 | jfieldID surface; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 53 | |
| 54 | jmethodID post_event; |
| 55 | }; |
| 56 | static fields_t fields; |
| 57 | |
| 58 | static Mutex sLock; |
| 59 | |
| 60 | // ---------------------------------------------------------------------------- |
| 61 | // ref-counted object for callbacks |
| 62 | class JNIMediaRecorderListener: public MediaRecorderListener |
| 63 | { |
| 64 | public: |
| 65 | JNIMediaRecorderListener(JNIEnv* env, jobject thiz, jobject weak_thiz); |
| 66 | ~JNIMediaRecorderListener(); |
| 67 | void notify(int msg, int ext1, int ext2); |
| 68 | private: |
| 69 | JNIMediaRecorderListener(); |
| 70 | jclass mClass; // Reference to MediaRecorder class |
| 71 | jobject mObject; // Weak ref to MediaRecorder Java object to call on |
| 72 | }; |
| 73 | |
| 74 | JNIMediaRecorderListener::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 Block | 3762c31 | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 81 | ALOGE("Can't find android/media/MediaRecorder"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 82 | 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 | |
| 92 | JNIMediaRecorderListener::~JNIMediaRecorderListener() |
| 93 | { |
| 94 | // remove global references |
| 95 | JNIEnv *env = AndroidRuntime::getJNIEnv(); |
| 96 | env->DeleteGlobalRef(mObject); |
| 97 | env->DeleteGlobalRef(mClass); |
| 98 | } |
| 99 | |
| 100 | void JNIMediaRecorderListener::notify(int msg, int ext1, int ext2) |
| 101 | { |
Steve Block | 71f2cf1 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 102 | ALOGV("JNIMediaRecorderListener::notify"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 103 | |
| 104 | JNIEnv *env = AndroidRuntime::getJNIEnv(); |
| 105 | env->CallStaticVoidMethod(mClass, fields.post_event, mObject, msg, ext1, ext2, 0); |
| 106 | } |
| 107 | |
| 108 | // ---------------------------------------------------------------------------- |
| 109 | |
| 110 | static sp<Surface> get_surface(JNIEnv* env, jobject clazz) |
| 111 | { |
Steve Block | 71f2cf1 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 112 | ALOGV("get_surface"); |
Mathias Agopian | 3866f0d | 2013-02-11 22:08:48 -0800 | [diff] [blame] | 113 | return android_view_Surface_getSurface(env, clazz); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 114 | } |
| 115 | |
| 116 | // Returns true if it throws an exception. |
| 117 | static bool process_media_recorder_call(JNIEnv *env, status_t opStatus, const char* exception, const char* message) |
| 118 | { |
Steve Block | 71f2cf1 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 119 | ALOGV("process_media_recorder_call"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 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 | |
Pannag Sanketi | cac873b | 2011-07-25 17:20:50 -0700 | [diff] [blame] | 130 | static sp<MediaRecorder> getMediaRecorder(JNIEnv* env, jobject thiz) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 131 | { |
| 132 | Mutex::Autolock l(sLock); |
Ashok Bhat | 075e9a1 | 2014-01-06 13:45:09 +0000 | [diff] [blame] | 133 | MediaRecorder* const p = (MediaRecorder*)env->GetLongField(thiz, fields.context); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 134 | return sp<MediaRecorder>(p); |
| 135 | } |
| 136 | |
| 137 | static sp<MediaRecorder> setMediaRecorder(JNIEnv* env, jobject thiz, const sp<MediaRecorder>& recorder) |
| 138 | { |
| 139 | Mutex::Autolock l(sLock); |
Ashok Bhat | 075e9a1 | 2014-01-06 13:45:09 +0000 | [diff] [blame] | 140 | sp<MediaRecorder> old = (MediaRecorder*)env->GetLongField(thiz, fields.context); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 141 | if (recorder.get()) { |
| 142 | recorder->incStrong(thiz); |
| 143 | } |
| 144 | if (old != 0) { |
| 145 | old->decStrong(thiz); |
| 146 | } |
Ashok Bhat | 075e9a1 | 2014-01-06 13:45:09 +0000 | [diff] [blame] | 147 | env->SetLongField(thiz, fields.context, (jlong)recorder.get()); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 148 | return old; |
| 149 | } |
| 150 | |
| 151 | |
| 152 | static void android_media_MediaRecorder_setCamera(JNIEnv* env, jobject thiz, jobject camera) |
| 153 | { |
James Dong | 429a3b5 | 2009-05-11 10:58:03 -0700 | [diff] [blame] | 154 | // we should not pass a null camera to get_native_camera() call. |
| 155 | if (camera == NULL) { |
Elliott Hughes | 15dd15f | 2011-04-08 17:42:34 -0700 | [diff] [blame] | 156 | jniThrowNullPointerException(env, "camera object is a NULL pointer"); |
James Dong | 429a3b5 | 2009-05-11 10:58:03 -0700 | [diff] [blame] | 157 | return; |
| 158 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 159 | sp<Camera> c = get_native_camera(env, camera, NULL); |
| 160 | sp<MediaRecorder> mr = getMediaRecorder(env, thiz); |
Wu-cheng Li | 42419ce | 2011-06-01 17:22:24 +0800 | [diff] [blame] | 161 | 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] | 162 | "java/lang/RuntimeException", "setCamera failed."); |
| 163 | } |
| 164 | |
| 165 | static void |
| 166 | android_media_MediaRecorder_setVideoSource(JNIEnv *env, jobject thiz, jint vs) |
| 167 | { |
Steve Block | 71f2cf1 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 168 | ALOGV("setVideoSource(%d)", vs); |
Jianhong Jiang | 2bcda90 | 2009-06-08 08:50:42 -0700 | [diff] [blame] | 169 | 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] | 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 |
| 178 | android_media_MediaRecorder_setAudioSource(JNIEnv *env, jobject thiz, jint as) |
| 179 | { |
Steve Block | 71f2cf1 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 180 | ALOGV("setAudioSource(%d)", as); |
Dima Zavin | 24fc2fb | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 181 | if (as < AUDIO_SOURCE_DEFAULT || as >= AUDIO_SOURCE_CNT) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 182 | jniThrowException(env, "java/lang/IllegalArgumentException", "Invalid audio source"); |
| 183 | return; |
| 184 | } |
Jianhong Jiang | 2bcda90 | 2009-06-08 08:50:42 -0700 | [diff] [blame] | 185 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 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 |
| 191 | android_media_MediaRecorder_setOutputFormat(JNIEnv *env, jobject thiz, jint of) |
| 192 | { |
Steve Block | 71f2cf1 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 193 | ALOGV("setOutputFormat(%d)", of); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 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 |
| 203 | android_media_MediaRecorder_setVideoEncoder(JNIEnv *env, jobject thiz, jint ve) |
| 204 | { |
Steve Block | 71f2cf1 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 205 | ALOGV("setVideoEncoder(%d)", ve); |
Jianhong Jiang | 2bcda90 | 2009-06-08 08:50:42 -0700 | [diff] [blame] | 206 | 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] | 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 |
| 215 | android_media_MediaRecorder_setAudioEncoder(JNIEnv *env, jobject thiz, jint ae) |
| 216 | { |
Steve Block | 71f2cf1 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 217 | ALOGV("setAudioEncoder(%d)", ae); |
Jianhong Jiang | 2bcda90 | 2009-06-08 08:50:42 -0700 | [diff] [blame] | 218 | 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] | 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 |
James Dong | 0fc6bc4 | 2010-02-26 19:36:35 -0800 | [diff] [blame] | 227 | android_media_MediaRecorder_setParameter(JNIEnv *env, jobject thiz, jstring params) |
Jianhong Jiang | 2bcda90 | 2009-06-08 08:50:42 -0700 | [diff] [blame] | 228 | { |
Steve Block | 71f2cf1 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 229 | ALOGV("setParameter()"); |
Jianhong Jiang | 2bcda90 | 2009-06-08 08:50:42 -0700 | [diff] [blame] | 230 | if (params == NULL) |
| 231 | { |
Steve Block | 3762c31 | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 232 | ALOGE("Invalid or empty params string. This parameter will be ignored."); |
Jianhong Jiang | 2bcda90 | 2009-06-08 08:50:42 -0700 | [diff] [blame] | 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 | { |
Steve Block | 3762c31 | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 241 | ALOGE("Failed to covert jstring to String8. This parameter will be ignored."); |
Jianhong Jiang | 2bcda90 | 2009-06-08 08:50:42 -0700 | [diff] [blame] | 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 |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 250 | android_media_MediaRecorder_setOutputFileFD(JNIEnv *env, jobject thiz, jobject fileDescriptor, jlong offset, jlong length) |
| 251 | { |
Steve Block | 71f2cf1 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 252 | ALOGV("setOutputFile"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 253 | if (fileDescriptor == NULL) { |
| 254 | jniThrowException(env, "java/lang/IllegalArgumentException", NULL); |
| 255 | return; |
| 256 | } |
Elliott Hughes | a3804cf | 2011-04-11 16:50:19 -0700 | [diff] [blame] | 257 | int fd = jniGetFDFromFileDescriptor(env, fileDescriptor); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 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 |
| 264 | android_media_MediaRecorder_setVideoSize(JNIEnv *env, jobject thiz, jint width, jint height) |
| 265 | { |
Steve Block | 71f2cf1 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 266 | ALOGV("setVideoSize(%d, %d)", width, height); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 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 |
| 277 | android_media_MediaRecorder_setVideoFrameRate(JNIEnv *env, jobject thiz, jint rate) |
| 278 | { |
Steve Block | 71f2cf1 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 279 | ALOGV("setVideoFrameRate(%d)", rate); |
James Dong | cbe7c54 | 2009-07-30 11:16:11 -0700 | [diff] [blame] | 280 | if (rate <= 0) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 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 |
The Android Open Source Project | ba87e3e | 2009-03-13 13:04:22 -0700 | [diff] [blame] | 289 | android_media_MediaRecorder_setMaxDuration(JNIEnv *env, jobject thiz, jint max_duration_ms) |
| 290 | { |
Steve Block | 71f2cf1 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 291 | ALOGV("setMaxDuration(%d)", max_duration_ms); |
The Android Open Source Project | ba87e3e | 2009-03-13 13:04:22 -0700 | [diff] [blame] | 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 |
The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 301 | android_media_MediaRecorder_setMaxFileSize( |
| 302 | JNIEnv *env, jobject thiz, jlong max_filesize_bytes) |
| 303 | { |
Steve Block | 71f2cf1 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 304 | ALOGV("setMaxFileSize(%lld)", max_filesize_bytes); |
The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 305 | sp<MediaRecorder> mr = getMediaRecorder(env, thiz); |
| 306 | |
| 307 | char params[64]; |
Mark Salyzyn | 8539403 | 2014-04-16 10:28:37 -0700 | [diff] [blame] | 308 | sprintf(params, "max-filesize=%" PRId64, max_filesize_bytes); |
The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 309 | |
| 310 | process_media_recorder_call(env, mr->setParameters(String8(params)), "java/lang/RuntimeException", "setMaxFileSize failed."); |
| 311 | } |
| 312 | |
| 313 | static void |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 314 | android_media_MediaRecorder_prepare(JNIEnv *env, jobject thiz) |
| 315 | { |
Steve Block | 71f2cf1 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 316 | ALOGV("prepare"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 317 | sp<MediaRecorder> mr = getMediaRecorder(env, thiz); |
| 318 | |
| 319 | jobject surface = env->GetObjectField(thiz, fields.surface); |
| 320 | if (surface != NULL) { |
James Dong | 457d5ae | 2010-06-09 10:19:33 -0700 | [diff] [blame] | 321 | const sp<Surface> native_surface = get_surface(env, surface); |
James Dong | d790c648 | 2010-08-12 10:18:01 -0700 | [diff] [blame] | 322 | |
| 323 | // The application may misbehave and |
| 324 | // the preview surface becomes unavailable |
| 325 | if (native_surface.get() == 0) { |
Steve Block | 3762c31 | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 326 | ALOGE("Application lost the surface"); |
James Dong | d790c648 | 2010-08-12 10:18:01 -0700 | [diff] [blame] | 327 | jniThrowException(env, "java/io/IOException", "invalid preview surface"); |
| 328 | return; |
| 329 | } |
| 330 | |
Mathias Agopian | bfcae35 | 2013-02-14 15:32:50 -0800 | [diff] [blame] | 331 | ALOGI("prepare: surface=%p", native_surface.get()); |
Mathias Agopian | 4a05f43 | 2013-03-12 18:43:34 -0700 | [diff] [blame] | 332 | 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] | 333 | return; |
| 334 | } |
| 335 | } |
| 336 | process_media_recorder_call(env, mr->prepare(), "java/io/IOException", "prepare failed."); |
| 337 | } |
| 338 | |
Ashok Bhat | 075e9a1 | 2014-01-06 13:45:09 +0000 | [diff] [blame] | 339 | static jint |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 340 | android_media_MediaRecorder_native_getMaxAmplitude(JNIEnv *env, jobject thiz) |
| 341 | { |
Steve Block | 71f2cf1 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 342 | ALOGV("getMaxAmplitude"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 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."); |
Ashok Bhat | 075e9a1 | 2014-01-06 13:45:09 +0000 | [diff] [blame] | 346 | return (jint) result; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 347 | } |
| 348 | |
Chong Zhang | 83cc994 | 2014-01-02 12:10:04 -0800 | [diff] [blame] | 349 | static jobject |
| 350 | android_media_MediaRecorder_getSurface(JNIEnv *env, jobject thiz) |
| 351 | { |
| 352 | ALOGV("getSurface"); |
| 353 | sp<MediaRecorder> mr = getMediaRecorder(env, thiz); |
| 354 | |
| 355 | sp<IGraphicBufferProducer> bufferProducer = mr->querySurfaceMediaSourceFromMediaServer(); |
| 356 | if (bufferProducer == NULL) { |
| 357 | jniThrowException( |
| 358 | env, |
| 359 | "java/lang/IllegalStateException", |
| 360 | "failed to get surface"); |
| 361 | return NULL; |
| 362 | } |
| 363 | |
| 364 | // Wrap the IGBP in a Java-language Surface. |
| 365 | return android_view_Surface_createFromIGraphicBufferProducer(env, |
| 366 | bufferProducer); |
| 367 | } |
| 368 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 369 | static void |
| 370 | android_media_MediaRecorder_start(JNIEnv *env, jobject thiz) |
| 371 | { |
Steve Block | 71f2cf1 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 372 | ALOGV("start"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 373 | sp<MediaRecorder> mr = getMediaRecorder(env, thiz); |
| 374 | process_media_recorder_call(env, mr->start(), "java/lang/RuntimeException", "start failed."); |
| 375 | } |
| 376 | |
| 377 | static void |
| 378 | android_media_MediaRecorder_stop(JNIEnv *env, jobject thiz) |
| 379 | { |
Steve Block | 71f2cf1 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 380 | ALOGV("stop"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 381 | sp<MediaRecorder> mr = getMediaRecorder(env, thiz); |
| 382 | process_media_recorder_call(env, mr->stop(), "java/lang/RuntimeException", "stop failed."); |
| 383 | } |
| 384 | |
| 385 | static void |
| 386 | android_media_MediaRecorder_native_reset(JNIEnv *env, jobject thiz) |
| 387 | { |
Steve Block | 71f2cf1 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 388 | ALOGV("native_reset"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 389 | sp<MediaRecorder> mr = getMediaRecorder(env, thiz); |
| 390 | process_media_recorder_call(env, mr->reset(), "java/lang/RuntimeException", "native_reset failed."); |
| 391 | } |
| 392 | |
| 393 | static void |
| 394 | android_media_MediaRecorder_release(JNIEnv *env, jobject thiz) |
| 395 | { |
Steve Block | 71f2cf1 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 396 | ALOGV("release"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 397 | sp<MediaRecorder> mr = setMediaRecorder(env, thiz, 0); |
| 398 | if (mr != NULL) { |
| 399 | mr->setListener(NULL); |
The Android Open Source Project | 4df2423 | 2009-03-05 14:34:35 -0800 | [diff] [blame] | 400 | mr->release(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 401 | } |
| 402 | } |
| 403 | |
Marco Nelissen | 4935d05 | 2009-08-03 11:12:58 -0700 | [diff] [blame] | 404 | // This function gets some field IDs, which in turn causes class initialization. |
| 405 | // It is called from a static block in MediaRecorder, which won't run until the |
| 406 | // first time an instance of this class is used. |
| 407 | static void |
| 408 | android_media_MediaRecorder_native_init(JNIEnv *env) |
| 409 | { |
| 410 | jclass clazz; |
| 411 | |
| 412 | clazz = env->FindClass("android/media/MediaRecorder"); |
| 413 | if (clazz == NULL) { |
Marco Nelissen | 4935d05 | 2009-08-03 11:12:58 -0700 | [diff] [blame] | 414 | return; |
| 415 | } |
| 416 | |
Ashok Bhat | 075e9a1 | 2014-01-06 13:45:09 +0000 | [diff] [blame] | 417 | fields.context = env->GetFieldID(clazz, "mNativeContext", "J"); |
Marco Nelissen | 4935d05 | 2009-08-03 11:12:58 -0700 | [diff] [blame] | 418 | if (fields.context == NULL) { |
Marco Nelissen | 4935d05 | 2009-08-03 11:12:58 -0700 | [diff] [blame] | 419 | return; |
| 420 | } |
| 421 | |
| 422 | fields.surface = env->GetFieldID(clazz, "mSurface", "Landroid/view/Surface;"); |
| 423 | if (fields.surface == NULL) { |
Marco Nelissen | 4935d05 | 2009-08-03 11:12:58 -0700 | [diff] [blame] | 424 | return; |
| 425 | } |
| 426 | |
| 427 | jclass surface = env->FindClass("android/view/Surface"); |
| 428 | if (surface == NULL) { |
Marco Nelissen | 4935d05 | 2009-08-03 11:12:58 -0700 | [diff] [blame] | 429 | return; |
| 430 | } |
| 431 | |
Marco Nelissen | 4935d05 | 2009-08-03 11:12:58 -0700 | [diff] [blame] | 432 | fields.post_event = env->GetStaticMethodID(clazz, "postEventFromNative", |
| 433 | "(Ljava/lang/Object;IIILjava/lang/Object;)V"); |
| 434 | if (fields.post_event == NULL) { |
Marco Nelissen | 4935d05 | 2009-08-03 11:12:58 -0700 | [diff] [blame] | 435 | return; |
| 436 | } |
| 437 | } |
| 438 | |
| 439 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 440 | static void |
Eino-Ville Talvala | 788717c | 2013-02-15 18:30:15 -0800 | [diff] [blame] | 441 | android_media_MediaRecorder_native_setup(JNIEnv *env, jobject thiz, jobject weak_this, |
| 442 | jstring packageName) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 443 | { |
Steve Block | 71f2cf1 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 444 | ALOGV("setup"); |
Eino-Ville Talvala | 788717c | 2013-02-15 18:30:15 -0800 | [diff] [blame] | 445 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 446 | sp<MediaRecorder> mr = new MediaRecorder(); |
| 447 | if (mr == NULL) { |
| 448 | jniThrowException(env, "java/lang/RuntimeException", "Out of memory"); |
| 449 | return; |
| 450 | } |
| 451 | if (mr->initCheck() != NO_ERROR) { |
James Dong | 8d3b910 | 2010-09-09 18:43:48 -0700 | [diff] [blame] | 452 | 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] | 453 | return; |
| 454 | } |
| 455 | |
| 456 | // create new listener and give it to MediaRecorder |
| 457 | sp<JNIMediaRecorderListener> listener = new JNIMediaRecorderListener(env, thiz, weak_this); |
| 458 | mr->setListener(listener); |
| 459 | |
Eino-Ville Talvala | 788717c | 2013-02-15 18:30:15 -0800 | [diff] [blame] | 460 | // Convert client name jstring to String16 |
| 461 | const char16_t *rawClientName = env->GetStringChars(packageName, NULL); |
| 462 | jsize rawClientNameLen = env->GetStringLength(packageName); |
| 463 | String16 clientName(rawClientName, rawClientNameLen); |
| 464 | env->ReleaseStringChars(packageName, rawClientName); |
| 465 | |
| 466 | // pass client package name for permissions tracking |
| 467 | mr->setClientName(clientName); |
| 468 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 469 | setMediaRecorder(env, thiz, mr); |
| 470 | } |
| 471 | |
| 472 | static void |
| 473 | android_media_MediaRecorder_native_finalize(JNIEnv *env, jobject thiz) |
| 474 | { |
Steve Block | 71f2cf1 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 475 | ALOGV("finalize"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 476 | android_media_MediaRecorder_release(env, thiz); |
| 477 | } |
| 478 | |
| 479 | // ---------------------------------------------------------------------------- |
| 480 | |
| 481 | static JNINativeMethod gMethods[] = { |
| 482 | {"setCamera", "(Landroid/hardware/Camera;)V", (void *)android_media_MediaRecorder_setCamera}, |
| 483 | {"setVideoSource", "(I)V", (void *)android_media_MediaRecorder_setVideoSource}, |
| 484 | {"setAudioSource", "(I)V", (void *)android_media_MediaRecorder_setAudioSource}, |
| 485 | {"setOutputFormat", "(I)V", (void *)android_media_MediaRecorder_setOutputFormat}, |
| 486 | {"setVideoEncoder", "(I)V", (void *)android_media_MediaRecorder_setVideoEncoder}, |
| 487 | {"setAudioEncoder", "(I)V", (void *)android_media_MediaRecorder_setAudioEncoder}, |
James Dong | 0fc6bc4 | 2010-02-26 19:36:35 -0800 | [diff] [blame] | 488 | {"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] | 489 | {"_setOutputFile", "(Ljava/io/FileDescriptor;JJ)V", (void *)android_media_MediaRecorder_setOutputFileFD}, |
| 490 | {"setVideoSize", "(II)V", (void *)android_media_MediaRecorder_setVideoSize}, |
| 491 | {"setVideoFrameRate", "(I)V", (void *)android_media_MediaRecorder_setVideoFrameRate}, |
The Android Open Source Project | ba87e3e | 2009-03-13 13:04:22 -0700 | [diff] [blame] | 492 | {"setMaxDuration", "(I)V", (void *)android_media_MediaRecorder_setMaxDuration}, |
The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 493 | {"setMaxFileSize", "(J)V", (void *)android_media_MediaRecorder_setMaxFileSize}, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 494 | {"_prepare", "()V", (void *)android_media_MediaRecorder_prepare}, |
Chong Zhang | 83cc994 | 2014-01-02 12:10:04 -0800 | [diff] [blame] | 495 | {"getSurface", "()Landroid/view/Surface;", (void *)android_media_MediaRecorder_getSurface}, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 496 | {"getMaxAmplitude", "()I", (void *)android_media_MediaRecorder_native_getMaxAmplitude}, |
| 497 | {"start", "()V", (void *)android_media_MediaRecorder_start}, |
| 498 | {"stop", "()V", (void *)android_media_MediaRecorder_stop}, |
Marco Nelissen | 4935d05 | 2009-08-03 11:12:58 -0700 | [diff] [blame] | 499 | {"native_reset", "()V", (void *)android_media_MediaRecorder_native_reset}, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 500 | {"release", "()V", (void *)android_media_MediaRecorder_release}, |
Marco Nelissen | 4935d05 | 2009-08-03 11:12:58 -0700 | [diff] [blame] | 501 | {"native_init", "()V", (void *)android_media_MediaRecorder_native_init}, |
Eino-Ville Talvala | 788717c | 2013-02-15 18:30:15 -0800 | [diff] [blame] | 502 | {"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] | 503 | {"native_finalize", "()V", (void *)android_media_MediaRecorder_native_finalize}, |
| 504 | }; |
| 505 | |
| 506 | static const char* const kClassPathName = "android/media/MediaRecorder"; |
| 507 | |
Marco Nelissen | 4935d05 | 2009-08-03 11:12:58 -0700 | [diff] [blame] | 508 | // This function only registers the native methods, and is called from |
| 509 | // JNI_OnLoad in android_media_MediaPlayer.cpp |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 510 | int register_android_media_MediaRecorder(JNIEnv *env) |
| 511 | { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 512 | return AndroidRuntime::registerNativeMethods(env, |
| 513 | "android/media/MediaRecorder", gMethods, NELEM(gMethods)); |
| 514 | } |