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); |
Eino-Ville Talvala | 6a5b1db | 2014-07-14 12:02:21 -0700 | [diff] [blame] | 160 | if (c == NULL) { |
| 161 | // get_native_camera will throw an exception in this case |
| 162 | return; |
| 163 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 164 | sp<MediaRecorder> mr = getMediaRecorder(env, thiz); |
Wu-cheng Li | 42419ce | 2011-06-01 17:22:24 +0800 | [diff] [blame] | 165 | 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] | 166 | "java/lang/RuntimeException", "setCamera failed."); |
| 167 | } |
| 168 | |
| 169 | static void |
| 170 | android_media_MediaRecorder_setVideoSource(JNIEnv *env, jobject thiz, jint vs) |
| 171 | { |
Steve Block | 71f2cf1 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 172 | ALOGV("setVideoSource(%d)", vs); |
Jianhong Jiang | 2bcda90 | 2009-06-08 08:50:42 -0700 | [diff] [blame] | 173 | 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] | 174 | 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 | |
| 181 | static void |
| 182 | android_media_MediaRecorder_setAudioSource(JNIEnv *env, jobject thiz, jint as) |
| 183 | { |
Steve Block | 71f2cf1 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 184 | ALOGV("setAudioSource(%d)", as); |
Hochi Huang | 4a7873f | 2014-10-22 22:10:02 +0800 | [diff] [blame] | 185 | if (as < AUDIO_SOURCE_DEFAULT || |
| 186 | (as >= AUDIO_SOURCE_CNT && as != AUDIO_SOURCE_FM_TUNER)) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 187 | jniThrowException(env, "java/lang/IllegalArgumentException", "Invalid audio source"); |
| 188 | return; |
| 189 | } |
Jianhong Jiang | 2bcda90 | 2009-06-08 08:50:42 -0700 | [diff] [blame] | 190 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 191 | sp<MediaRecorder> mr = getMediaRecorder(env, thiz); |
| 192 | process_media_recorder_call(env, mr->setAudioSource(as), "java/lang/RuntimeException", "setAudioSource failed."); |
| 193 | } |
| 194 | |
| 195 | static void |
| 196 | android_media_MediaRecorder_setOutputFormat(JNIEnv *env, jobject thiz, jint of) |
| 197 | { |
Steve Block | 71f2cf1 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 198 | ALOGV("setOutputFormat(%d)", of); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 199 | 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 | |
| 207 | static void |
| 208 | android_media_MediaRecorder_setVideoEncoder(JNIEnv *env, jobject thiz, jint ve) |
| 209 | { |
Steve Block | 71f2cf1 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 210 | ALOGV("setVideoEncoder(%d)", ve); |
Jianhong Jiang | 2bcda90 | 2009-06-08 08:50:42 -0700 | [diff] [blame] | 211 | 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] | 212 | 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 | |
| 219 | static void |
| 220 | android_media_MediaRecorder_setAudioEncoder(JNIEnv *env, jobject thiz, jint ae) |
| 221 | { |
Steve Block | 71f2cf1 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 222 | ALOGV("setAudioEncoder(%d)", ae); |
Jianhong Jiang | 2bcda90 | 2009-06-08 08:50:42 -0700 | [diff] [blame] | 223 | 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] | 224 | 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 | |
| 231 | static void |
James Dong | 0fc6bc4 | 2010-02-26 19:36:35 -0800 | [diff] [blame] | 232 | android_media_MediaRecorder_setParameter(JNIEnv *env, jobject thiz, jstring params) |
Jianhong Jiang | 2bcda90 | 2009-06-08 08:50:42 -0700 | [diff] [blame] | 233 | { |
Steve Block | 71f2cf1 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 234 | ALOGV("setParameter()"); |
Jianhong Jiang | 2bcda90 | 2009-06-08 08:50:42 -0700 | [diff] [blame] | 235 | if (params == NULL) |
| 236 | { |
Steve Block | 3762c31 | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 237 | ALOGE("Invalid or empty params string. This parameter will be ignored."); |
Jianhong Jiang | 2bcda90 | 2009-06-08 08:50:42 -0700 | [diff] [blame] | 238 | 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 Block | 3762c31 | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 246 | ALOGE("Failed to covert jstring to String8. This parameter will be ignored."); |
Jianhong Jiang | 2bcda90 | 2009-06-08 08:50:42 -0700 | [diff] [blame] | 247 | 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 | |
| 254 | static void |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 255 | android_media_MediaRecorder_setOutputFileFD(JNIEnv *env, jobject thiz, jobject fileDescriptor, jlong offset, jlong length) |
| 256 | { |
Steve Block | 71f2cf1 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 257 | ALOGV("setOutputFile"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 258 | if (fileDescriptor == NULL) { |
| 259 | jniThrowException(env, "java/lang/IllegalArgumentException", NULL); |
| 260 | return; |
| 261 | } |
Elliott Hughes | a3804cf | 2011-04-11 16:50:19 -0700 | [diff] [blame] | 262 | int fd = jniGetFDFromFileDescriptor(env, fileDescriptor); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 263 | 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 | |
| 268 | static void |
| 269 | android_media_MediaRecorder_setVideoSize(JNIEnv *env, jobject thiz, jint width, jint height) |
| 270 | { |
Steve Block | 71f2cf1 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 271 | ALOGV("setVideoSize(%d, %d)", width, height); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 272 | 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 | |
| 281 | static void |
| 282 | android_media_MediaRecorder_setVideoFrameRate(JNIEnv *env, jobject thiz, jint rate) |
| 283 | { |
Steve Block | 71f2cf1 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 284 | ALOGV("setVideoFrameRate(%d)", rate); |
James Dong | cbe7c54 | 2009-07-30 11:16:11 -0700 | [diff] [blame] | 285 | if (rate <= 0) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 286 | 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 | |
| 293 | static void |
The Android Open Source Project | ba87e3e | 2009-03-13 13:04:22 -0700 | [diff] [blame] | 294 | android_media_MediaRecorder_setMaxDuration(JNIEnv *env, jobject thiz, jint max_duration_ms) |
| 295 | { |
Steve Block | 71f2cf1 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 296 | ALOGV("setMaxDuration(%d)", max_duration_ms); |
The Android Open Source Project | ba87e3e | 2009-03-13 13:04:22 -0700 | [diff] [blame] | 297 | 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 | |
| 305 | static void |
The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 306 | android_media_MediaRecorder_setMaxFileSize( |
| 307 | JNIEnv *env, jobject thiz, jlong max_filesize_bytes) |
| 308 | { |
Steve Block | 71f2cf1 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 309 | ALOGV("setMaxFileSize(%lld)", max_filesize_bytes); |
The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 310 | sp<MediaRecorder> mr = getMediaRecorder(env, thiz); |
| 311 | |
| 312 | char params[64]; |
Mark Salyzyn | 8539403 | 2014-04-16 10:28:37 -0700 | [diff] [blame] | 313 | sprintf(params, "max-filesize=%" PRId64, max_filesize_bytes); |
The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 314 | |
| 315 | process_media_recorder_call(env, mr->setParameters(String8(params)), "java/lang/RuntimeException", "setMaxFileSize failed."); |
| 316 | } |
| 317 | |
| 318 | static void |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 319 | android_media_MediaRecorder_prepare(JNIEnv *env, jobject thiz) |
| 320 | { |
Steve Block | 71f2cf1 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 321 | ALOGV("prepare"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 322 | sp<MediaRecorder> mr = getMediaRecorder(env, thiz); |
| 323 | |
| 324 | jobject surface = env->GetObjectField(thiz, fields.surface); |
| 325 | if (surface != NULL) { |
James Dong | 457d5ae | 2010-06-09 10:19:33 -0700 | [diff] [blame] | 326 | const sp<Surface> native_surface = get_surface(env, surface); |
James Dong | d790c648 | 2010-08-12 10:18:01 -0700 | [diff] [blame] | 327 | |
| 328 | // The application may misbehave and |
| 329 | // the preview surface becomes unavailable |
| 330 | if (native_surface.get() == 0) { |
Steve Block | 3762c31 | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 331 | ALOGE("Application lost the surface"); |
James Dong | d790c648 | 2010-08-12 10:18:01 -0700 | [diff] [blame] | 332 | jniThrowException(env, "java/io/IOException", "invalid preview surface"); |
| 333 | return; |
| 334 | } |
| 335 | |
Mathias Agopian | bfcae35 | 2013-02-14 15:32:50 -0800 | [diff] [blame] | 336 | ALOGI("prepare: surface=%p", native_surface.get()); |
Mathias Agopian | 4a05f43 | 2013-03-12 18:43:34 -0700 | [diff] [blame] | 337 | 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] | 338 | return; |
| 339 | } |
| 340 | } |
| 341 | process_media_recorder_call(env, mr->prepare(), "java/io/IOException", "prepare failed."); |
| 342 | } |
| 343 | |
Ashok Bhat | 075e9a1 | 2014-01-06 13:45:09 +0000 | [diff] [blame] | 344 | static jint |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 345 | android_media_MediaRecorder_native_getMaxAmplitude(JNIEnv *env, jobject thiz) |
| 346 | { |
Steve Block | 71f2cf1 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 347 | ALOGV("getMaxAmplitude"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 348 | 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 Bhat | 075e9a1 | 2014-01-06 13:45:09 +0000 | [diff] [blame] | 351 | return (jint) result; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 352 | } |
| 353 | |
Chong Zhang | 83cc994 | 2014-01-02 12:10:04 -0800 | [diff] [blame] | 354 | static jobject |
| 355 | android_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 Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 374 | static void |
| 375 | android_media_MediaRecorder_start(JNIEnv *env, jobject thiz) |
| 376 | { |
Steve Block | 71f2cf1 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 377 | ALOGV("start"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 378 | sp<MediaRecorder> mr = getMediaRecorder(env, thiz); |
| 379 | process_media_recorder_call(env, mr->start(), "java/lang/RuntimeException", "start failed."); |
| 380 | } |
| 381 | |
| 382 | static void |
| 383 | android_media_MediaRecorder_stop(JNIEnv *env, jobject thiz) |
| 384 | { |
Steve Block | 71f2cf1 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 385 | ALOGV("stop"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 386 | sp<MediaRecorder> mr = getMediaRecorder(env, thiz); |
| 387 | process_media_recorder_call(env, mr->stop(), "java/lang/RuntimeException", "stop failed."); |
| 388 | } |
| 389 | |
| 390 | static void |
| 391 | android_media_MediaRecorder_native_reset(JNIEnv *env, jobject thiz) |
| 392 | { |
Steve Block | 71f2cf1 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 393 | ALOGV("native_reset"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 394 | sp<MediaRecorder> mr = getMediaRecorder(env, thiz); |
| 395 | process_media_recorder_call(env, mr->reset(), "java/lang/RuntimeException", "native_reset failed."); |
| 396 | } |
| 397 | |
| 398 | static void |
| 399 | android_media_MediaRecorder_release(JNIEnv *env, jobject thiz) |
| 400 | { |
Steve Block | 71f2cf1 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 401 | ALOGV("release"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 402 | sp<MediaRecorder> mr = setMediaRecorder(env, thiz, 0); |
| 403 | if (mr != NULL) { |
| 404 | mr->setListener(NULL); |
The Android Open Source Project | 4df2423 | 2009-03-05 14:34:35 -0800 | [diff] [blame] | 405 | mr->release(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 406 | } |
| 407 | } |
| 408 | |
Marco Nelissen | 4935d05 | 2009-08-03 11:12:58 -0700 | [diff] [blame] | 409 | // 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. |
| 412 | static void |
| 413 | android_media_MediaRecorder_native_init(JNIEnv *env) |
| 414 | { |
| 415 | jclass clazz; |
| 416 | |
| 417 | clazz = env->FindClass("android/media/MediaRecorder"); |
| 418 | if (clazz == NULL) { |
Marco Nelissen | 4935d05 | 2009-08-03 11:12:58 -0700 | [diff] [blame] | 419 | return; |
| 420 | } |
| 421 | |
Ashok Bhat | 075e9a1 | 2014-01-06 13:45:09 +0000 | [diff] [blame] | 422 | fields.context = env->GetFieldID(clazz, "mNativeContext", "J"); |
Marco Nelissen | 4935d05 | 2009-08-03 11:12:58 -0700 | [diff] [blame] | 423 | if (fields.context == NULL) { |
Marco Nelissen | 4935d05 | 2009-08-03 11:12:58 -0700 | [diff] [blame] | 424 | return; |
| 425 | } |
| 426 | |
| 427 | fields.surface = env->GetFieldID(clazz, "mSurface", "Landroid/view/Surface;"); |
| 428 | if (fields.surface == NULL) { |
Marco Nelissen | 4935d05 | 2009-08-03 11:12:58 -0700 | [diff] [blame] | 429 | return; |
| 430 | } |
| 431 | |
| 432 | jclass surface = env->FindClass("android/view/Surface"); |
| 433 | if (surface == NULL) { |
Marco Nelissen | 4935d05 | 2009-08-03 11:12:58 -0700 | [diff] [blame] | 434 | return; |
| 435 | } |
| 436 | |
Marco Nelissen | 4935d05 | 2009-08-03 11:12:58 -0700 | [diff] [blame] | 437 | fields.post_event = env->GetStaticMethodID(clazz, "postEventFromNative", |
| 438 | "(Ljava/lang/Object;IIILjava/lang/Object;)V"); |
| 439 | if (fields.post_event == NULL) { |
Marco Nelissen | 4935d05 | 2009-08-03 11:12:58 -0700 | [diff] [blame] | 440 | return; |
| 441 | } |
| 442 | } |
| 443 | |
| 444 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 445 | static void |
Eino-Ville Talvala | 788717c | 2013-02-15 18:30:15 -0800 | [diff] [blame] | 446 | android_media_MediaRecorder_native_setup(JNIEnv *env, jobject thiz, jobject weak_this, |
| 447 | jstring packageName) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 448 | { |
Steve Block | 71f2cf1 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 449 | ALOGV("setup"); |
Eino-Ville Talvala | 788717c | 2013-02-15 18:30:15 -0800 | [diff] [blame] | 450 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 451 | 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 Dong | 8d3b910 | 2010-09-09 18:43:48 -0700 | [diff] [blame] | 457 | 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] | 458 | 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 Talvala | 788717c | 2013-02-15 18:30:15 -0800 | [diff] [blame] | 465 | // 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 Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 474 | setMediaRecorder(env, thiz, mr); |
| 475 | } |
| 476 | |
| 477 | static void |
| 478 | android_media_MediaRecorder_native_finalize(JNIEnv *env, jobject thiz) |
| 479 | { |
Steve Block | 71f2cf1 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 480 | ALOGV("finalize"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 481 | android_media_MediaRecorder_release(env, thiz); |
| 482 | } |
| 483 | |
| 484 | // ---------------------------------------------------------------------------- |
| 485 | |
| 486 | static 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 Dong | 0fc6bc4 | 2010-02-26 19:36:35 -0800 | [diff] [blame] | 493 | {"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] | 494 | {"_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 Project | ba87e3e | 2009-03-13 13:04:22 -0700 | [diff] [blame] | 497 | {"setMaxDuration", "(I)V", (void *)android_media_MediaRecorder_setMaxDuration}, |
The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 498 | {"setMaxFileSize", "(J)V", (void *)android_media_MediaRecorder_setMaxFileSize}, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 499 | {"_prepare", "()V", (void *)android_media_MediaRecorder_prepare}, |
Chong Zhang | 83cc994 | 2014-01-02 12:10:04 -0800 | [diff] [blame] | 500 | {"getSurface", "()Landroid/view/Surface;", (void *)android_media_MediaRecorder_getSurface}, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 501 | {"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 Nelissen | 4935d05 | 2009-08-03 11:12:58 -0700 | [diff] [blame] | 504 | {"native_reset", "()V", (void *)android_media_MediaRecorder_native_reset}, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 505 | {"release", "()V", (void *)android_media_MediaRecorder_release}, |
Marco Nelissen | 4935d05 | 2009-08-03 11:12:58 -0700 | [diff] [blame] | 506 | {"native_init", "()V", (void *)android_media_MediaRecorder_native_init}, |
Eino-Ville Talvala | 788717c | 2013-02-15 18:30:15 -0800 | [diff] [blame] | 507 | {"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] | 508 | {"native_finalize", "()V", (void *)android_media_MediaRecorder_native_finalize}, |
| 509 | }; |
| 510 | |
| 511 | static const char* const kClassPathName = "android/media/MediaRecorder"; |
| 512 | |
Marco Nelissen | 4935d05 | 2009-08-03 11:12:58 -0700 | [diff] [blame] | 513 | // This function only registers the native methods, and is called from |
| 514 | // JNI_OnLoad in android_media_MediaPlayer.cpp |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 515 | int register_android_media_MediaRecorder(JNIEnv *env) |
| 516 | { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 517 | return AndroidRuntime::registerNativeMethods(env, |
| 518 | "android/media/MediaRecorder", gMethods, NELEM(gMethods)); |
| 519 | } |