19066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project/*
29066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * Copyright (C) 2008 The Android Open Source Project
39066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project *
49066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * Licensed under the Apache License, Version 2.0 (the "License");
59066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * you may not use this file except in compliance with the License.
69066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * You may obtain a copy of the License at
79066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project *
89066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project *     http://www.apache.org/licenses/LICENSE-2.0
99066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project *
109066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * Unless required by applicable law or agreed to in writing, software
119066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * distributed under the License is distributed on an "AS IS" BASIS,
129066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
139066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * See the License for the specific language governing permissions and
149066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * limitations under the License.
159066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project */
169066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
17853940331ff2be48ed3e63f459845e5e43d0a131Mark Salyzyn#include <assert.h>
18853940331ff2be48ed3e63f459845e5e43d0a131Mark Salyzyn#include <fcntl.h>
19853940331ff2be48ed3e63f459845e5e43d0a131Mark Salyzyn#include <inttypes.h>
20853940331ff2be48ed3e63f459845e5e43d0a131Mark Salyzyn#include <limits.h>
21853940331ff2be48ed3e63f459845e5e43d0a131Mark Salyzyn#include <stdio.h>
22853940331ff2be48ed3e63f459845e5e43d0a131Mark Salyzyn#include <unistd.h>
23853940331ff2be48ed3e63f459845e5e43d0a131Mark Salyzyn
249066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project//#define LOG_NDEBUG 0
259066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project#define LOG_TAG "MediaRecorderJNI"
269066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project#include <utils/Log.h>
279066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
288335f1ccccedb6655d96d9d5b697a7f0938235ddMathias Agopian#include <gui/Surface.h>
29000479f9e325b4e426a67033abd92d47da412725Mathias Agopian#include <camera/ICameraService.h>
30000479f9e325b4e426a67033abd92d47da412725Mathias Agopian#include <camera/Camera.h>
319066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project#include <media/mediarecorder.h>
329066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project#include <utils/threads.h>
339066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
349066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project#include "jni.h"
359066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project#include "JNIHelp.h"
369066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project#include "android_runtime/AndroidRuntime.h"
379066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
3834bb419e5946ab28112e9e27a4d1b3928d31e0e2Dima Zavin#include <system/audio.h>
393866f0d581ceaa165710feeee9f37fe1b0d7067dMathias Agopian#include <android_runtime/android_view_Surface.h>
409066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
419066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project// ----------------------------------------------------------------------------
429066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
439066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectusing namespace android;
449066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
459066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project// ----------------------------------------------------------------------------
469066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
479066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project// helper function to extract a native Camera object from a Camera Java object
485e27115995c4506dddf4f1271effd4fb4c935ad3Dave Sparksextern sp<Camera> get_native_camera(JNIEnv *env, jobject thiz, struct JNICameraContext** context);
499066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
509066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectstruct fields_t {
519066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    jfieldID    context;
529066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    jfieldID    surface;
539066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
549066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    jmethodID   post_event;
559066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project};
569066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectstatic fields_t fields;
579066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
589066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectstatic Mutex sLock;
599066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
609066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project// ----------------------------------------------------------------------------
619066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project// ref-counted object for callbacks
629066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectclass JNIMediaRecorderListener: public MediaRecorderListener
639066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project{
649066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectpublic:
659066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    JNIMediaRecorderListener(JNIEnv* env, jobject thiz, jobject weak_thiz);
669066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    ~JNIMediaRecorderListener();
679066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    void notify(int msg, int ext1, int ext2);
689066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectprivate:
699066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    JNIMediaRecorderListener();
709066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    jclass      mClass;     // Reference to MediaRecorder class
719066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    jobject     mObject;    // Weak ref to MediaRecorder Java object to call on
729066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project};
739066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
749066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source ProjectJNIMediaRecorderListener::JNIMediaRecorderListener(JNIEnv* env, jobject thiz, jobject weak_thiz)
759066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project{
769066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
779066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    // Hold onto the MediaRecorder class for use in calling the static method
789066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    // that posts events to the application thread.
799066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    jclass clazz = env->GetObjectClass(thiz);
809066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    if (clazz == NULL) {
813762c311729fe9f3af085c14c5c1fb471d994c03Steve Block        ALOGE("Can't find android/media/MediaRecorder");
829066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        jniThrowException(env, "java/lang/Exception", NULL);
839066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        return;
849066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
859066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    mClass = (jclass)env->NewGlobalRef(clazz);
869066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
879066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    // We use a weak reference so the MediaRecorder object can be garbage collected.
889066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    // The reference is only used as a proxy for callbacks.
899066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    mObject  = env->NewGlobalRef(weak_thiz);
909066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project}
919066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
929066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source ProjectJNIMediaRecorderListener::~JNIMediaRecorderListener()
939066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project{
949066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    // remove global references
959066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    JNIEnv *env = AndroidRuntime::getJNIEnv();
969066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    env->DeleteGlobalRef(mObject);
979066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    env->DeleteGlobalRef(mClass);
989066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project}
999066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
1009066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectvoid JNIMediaRecorderListener::notify(int msg, int ext1, int ext2)
1019066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project{
10271f2cf116aab893e224056c38ab146bd1538dd3eSteve Block    ALOGV("JNIMediaRecorderListener::notify");
1039066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
1049066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    JNIEnv *env = AndroidRuntime::getJNIEnv();
10567f304e2ca0d7a93730669b143bb30ef6a3f7199Haitao Feng    env->CallStaticVoidMethod(mClass, fields.post_event, mObject, msg, ext1, ext2, NULL);
1069066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project}
1079066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
1089066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project// ----------------------------------------------------------------------------
1099066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
1109066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectstatic sp<Surface> get_surface(JNIEnv* env, jobject clazz)
1119066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project{
11271f2cf116aab893e224056c38ab146bd1538dd3eSteve Block    ALOGV("get_surface");
1133866f0d581ceaa165710feeee9f37fe1b0d7067dMathias Agopian    return android_view_Surface_getSurface(env, clazz);
1149066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project}
1159066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
1169066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project// Returns true if it throws an exception.
1179066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectstatic bool process_media_recorder_call(JNIEnv *env, status_t opStatus, const char* exception, const char* message)
1189066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project{
11971f2cf116aab893e224056c38ab146bd1538dd3eSteve Block    ALOGV("process_media_recorder_call");
1209066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    if (opStatus == (status_t)INVALID_OPERATION) {
1219066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        jniThrowException(env, "java/lang/IllegalStateException", NULL);
1229066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        return true;
1239066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    } else if (opStatus != (status_t)OK) {
1249066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        jniThrowException(env, exception, message);
1259066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        return true;
1269066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
1279066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    return false;
1289066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project}
1299066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
130cac873bd4ad6ffc8587e3d77fece5abcf2804f4fPannag Sanketistatic sp<MediaRecorder> getMediaRecorder(JNIEnv* env, jobject thiz)
1319066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project{
1329066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    Mutex::Autolock l(sLock);
133075e9a19ce645752f8282bc19c91b25978a7dc52Ashok Bhat    MediaRecorder* const p = (MediaRecorder*)env->GetLongField(thiz, fields.context);
1349066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    return sp<MediaRecorder>(p);
1359066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project}
1369066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
1379066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectstatic sp<MediaRecorder> setMediaRecorder(JNIEnv* env, jobject thiz, const sp<MediaRecorder>& recorder)
1389066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project{
1399066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    Mutex::Autolock l(sLock);
140075e9a19ce645752f8282bc19c91b25978a7dc52Ashok Bhat    sp<MediaRecorder> old = (MediaRecorder*)env->GetLongField(thiz, fields.context);
1419066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    if (recorder.get()) {
1429066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        recorder->incStrong(thiz);
1439066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
1449066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    if (old != 0) {
1459066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        old->decStrong(thiz);
1469066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
147075e9a19ce645752f8282bc19c91b25978a7dc52Ashok Bhat    env->SetLongField(thiz, fields.context, (jlong)recorder.get());
1489066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    return old;
1499066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project}
1509066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
1519066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
1529066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectstatic void android_media_MediaRecorder_setCamera(JNIEnv* env, jobject thiz, jobject camera)
1539066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project{
154429a3b5f1d94327e5cbb2dc80667af4574bba388James Dong    // we should not pass a null camera to get_native_camera() call.
155429a3b5f1d94327e5cbb2dc80667af4574bba388James Dong    if (camera == NULL) {
15615dd15fd572df6b6f785dff75f66e9b99f40322aElliott Hughes        jniThrowNullPointerException(env, "camera object is a NULL pointer");
157429a3b5f1d94327e5cbb2dc80667af4574bba388James Dong        return;
158429a3b5f1d94327e5cbb2dc80667af4574bba388James Dong    }
1599066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    sp<Camera> c = get_native_camera(env, camera, NULL);
1606a5b1db177b71970a2d00eb6d24b8317914eab29Eino-Ville Talvala    if (c == NULL) {
1616a5b1db177b71970a2d00eb6d24b8317914eab29Eino-Ville Talvala        // get_native_camera will throw an exception in this case
1626a5b1db177b71970a2d00eb6d24b8317914eab29Eino-Ville Talvala        return;
1636a5b1db177b71970a2d00eb6d24b8317914eab29Eino-Ville Talvala    }
1649066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    sp<MediaRecorder> mr = getMediaRecorder(env, thiz);
16542419ce28a09eb63e29a8fef87e6f5534f41902fWu-cheng Li    process_media_recorder_call(env, mr->setCamera(c->remote(), c->getRecordingProxy()),
1669066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            "java/lang/RuntimeException", "setCamera failed.");
1679066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project}
1689066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
1699066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectstatic void
1709066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectandroid_media_MediaRecorder_setVideoSource(JNIEnv *env, jobject thiz, jint vs)
1719066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project{
17271f2cf116aab893e224056c38ab146bd1538dd3eSteve Block    ALOGV("setVideoSource(%d)", vs);
1732bcda90c0234f67f210a96f195b355493ca7d1ffJianhong Jiang    if (vs < VIDEO_SOURCE_DEFAULT || vs >= VIDEO_SOURCE_LIST_END) {
1749066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        jniThrowException(env, "java/lang/IllegalArgumentException", "Invalid video source");
1759066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        return;
1769066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
1779066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    sp<MediaRecorder> mr = getMediaRecorder(env, thiz);
1789066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    process_media_recorder_call(env, mr->setVideoSource(vs), "java/lang/RuntimeException", "setVideoSource failed.");
1799066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project}
1809066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
1819066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectstatic void
1829066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectandroid_media_MediaRecorder_setAudioSource(JNIEnv *env, jobject thiz, jint as)
1839066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project{
18471f2cf116aab893e224056c38ab146bd1538dd3eSteve Block    ALOGV("setAudioSource(%d)", as);
1854a7873f19afecb01ef06b0f3dc4dde0a13a17ab3Hochi Huang    if (as < AUDIO_SOURCE_DEFAULT ||
1864a7873f19afecb01ef06b0f3dc4dde0a13a17ab3Hochi Huang        (as >= AUDIO_SOURCE_CNT && as != AUDIO_SOURCE_FM_TUNER)) {
1879066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        jniThrowException(env, "java/lang/IllegalArgumentException", "Invalid audio source");
1889066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        return;
1899066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
1902bcda90c0234f67f210a96f195b355493ca7d1ffJianhong Jiang
1919066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    sp<MediaRecorder> mr = getMediaRecorder(env, thiz);
1929066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    process_media_recorder_call(env, mr->setAudioSource(as), "java/lang/RuntimeException", "setAudioSource failed.");
1939066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project}
1949066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
1959066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectstatic void
1969066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectandroid_media_MediaRecorder_setOutputFormat(JNIEnv *env, jobject thiz, jint of)
1979066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project{
19871f2cf116aab893e224056c38ab146bd1538dd3eSteve Block    ALOGV("setOutputFormat(%d)", of);
1999066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    if (of < OUTPUT_FORMAT_DEFAULT || of >= OUTPUT_FORMAT_LIST_END) {
2009066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        jniThrowException(env, "java/lang/IllegalArgumentException", "Invalid output format");
2019066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        return;
2029066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
2039066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    sp<MediaRecorder> mr = getMediaRecorder(env, thiz);
2049066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    process_media_recorder_call(env, mr->setOutputFormat(of), "java/lang/RuntimeException", "setOutputFormat failed.");
2059066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project}
2069066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
2079066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectstatic void
2089066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectandroid_media_MediaRecorder_setVideoEncoder(JNIEnv *env, jobject thiz, jint ve)
2099066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project{
21071f2cf116aab893e224056c38ab146bd1538dd3eSteve Block    ALOGV("setVideoEncoder(%d)", ve);
2112bcda90c0234f67f210a96f195b355493ca7d1ffJianhong Jiang    if (ve < VIDEO_ENCODER_DEFAULT || ve >= VIDEO_ENCODER_LIST_END) {
2129066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        jniThrowException(env, "java/lang/IllegalArgumentException", "Invalid video encoder");
2139066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        return;
2149066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
2159066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    sp<MediaRecorder> mr = getMediaRecorder(env, thiz);
2169066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    process_media_recorder_call(env, mr->setVideoEncoder(ve), "java/lang/RuntimeException", "setVideoEncoder failed.");
2179066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project}
2189066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
2199066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectstatic void
2209066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectandroid_media_MediaRecorder_setAudioEncoder(JNIEnv *env, jobject thiz, jint ae)
2219066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project{
22271f2cf116aab893e224056c38ab146bd1538dd3eSteve Block    ALOGV("setAudioEncoder(%d)", ae);
2232bcda90c0234f67f210a96f195b355493ca7d1ffJianhong Jiang    if (ae < AUDIO_ENCODER_DEFAULT || ae >= AUDIO_ENCODER_LIST_END) {
2249066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        jniThrowException(env, "java/lang/IllegalArgumentException", "Invalid audio encoder");
2259066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        return;
2269066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
2279066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    sp<MediaRecorder> mr = getMediaRecorder(env, thiz);
2289066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    process_media_recorder_call(env, mr->setAudioEncoder(ae), "java/lang/RuntimeException", "setAudioEncoder failed.");
2299066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project}
2309066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
2319066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectstatic void
2320fc6bc4cac6391f048f0f2748b3e979effe0924bJames Dongandroid_media_MediaRecorder_setParameter(JNIEnv *env, jobject thiz, jstring params)
2332bcda90c0234f67f210a96f195b355493ca7d1ffJianhong Jiang{
23471f2cf116aab893e224056c38ab146bd1538dd3eSteve Block    ALOGV("setParameter()");
2352bcda90c0234f67f210a96f195b355493ca7d1ffJianhong Jiang    if (params == NULL)
2362bcda90c0234f67f210a96f195b355493ca7d1ffJianhong Jiang    {
2373762c311729fe9f3af085c14c5c1fb471d994c03Steve Block        ALOGE("Invalid or empty params string.  This parameter will be ignored.");
2382bcda90c0234f67f210a96f195b355493ca7d1ffJianhong Jiang        return;
2392bcda90c0234f67f210a96f195b355493ca7d1ffJianhong Jiang    }
2402bcda90c0234f67f210a96f195b355493ca7d1ffJianhong Jiang
2412bcda90c0234f67f210a96f195b355493ca7d1ffJianhong Jiang    sp<MediaRecorder> mr = getMediaRecorder(env, thiz);
2422bcda90c0234f67f210a96f195b355493ca7d1ffJianhong Jiang
2432bcda90c0234f67f210a96f195b355493ca7d1ffJianhong Jiang    const char* params8 = env->GetStringUTFChars(params, NULL);
2442bcda90c0234f67f210a96f195b355493ca7d1ffJianhong Jiang    if (params8 == NULL)
2452bcda90c0234f67f210a96f195b355493ca7d1ffJianhong Jiang    {
2463762c311729fe9f3af085c14c5c1fb471d994c03Steve Block        ALOGE("Failed to covert jstring to String8.  This parameter will be ignored.");
2472bcda90c0234f67f210a96f195b355493ca7d1ffJianhong Jiang        return;
2482bcda90c0234f67f210a96f195b355493ca7d1ffJianhong Jiang    }
2492bcda90c0234f67f210a96f195b355493ca7d1ffJianhong Jiang
2502bcda90c0234f67f210a96f195b355493ca7d1ffJianhong Jiang    process_media_recorder_call(env, mr->setParameters(String8(params8)), "java/lang/RuntimeException", "setParameter failed.");
2512bcda90c0234f67f210a96f195b355493ca7d1ffJianhong Jiang    env->ReleaseStringUTFChars(params,params8);
2522bcda90c0234f67f210a96f195b355493ca7d1ffJianhong Jiang}
2532bcda90c0234f67f210a96f195b355493ca7d1ffJianhong Jiang
2542bcda90c0234f67f210a96f195b355493ca7d1ffJianhong Jiangstatic void
2559066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectandroid_media_MediaRecorder_setOutputFileFD(JNIEnv *env, jobject thiz, jobject fileDescriptor, jlong offset, jlong length)
2569066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project{
25771f2cf116aab893e224056c38ab146bd1538dd3eSteve Block    ALOGV("setOutputFile");
2589066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    if (fileDescriptor == NULL) {
2599066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        jniThrowException(env, "java/lang/IllegalArgumentException", NULL);
2609066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        return;
2619066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
262a3804cf77f0edd93f6247a055cdafb856b117eecElliott Hughes    int fd = jniGetFDFromFileDescriptor(env, fileDescriptor);
2639066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    sp<MediaRecorder> mr = getMediaRecorder(env, thiz);
2649066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    status_t opStatus = mr->setOutputFile(fd, offset, length);
2659066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    process_media_recorder_call(env, opStatus, "java/io/IOException", "setOutputFile failed.");
2669066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project}
2679066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
2689066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectstatic void
2699066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectandroid_media_MediaRecorder_setVideoSize(JNIEnv *env, jobject thiz, jint width, jint height)
2709066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project{
27171f2cf116aab893e224056c38ab146bd1538dd3eSteve Block    ALOGV("setVideoSize(%d, %d)", width, height);
2729066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    sp<MediaRecorder> mr = getMediaRecorder(env, thiz);
2739066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
2749066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    if (width <= 0 || height <= 0) {
2759066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        jniThrowException(env, "java/lang/IllegalArgumentException", "invalid video size");
2769066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        return;
2779066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
2789066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    process_media_recorder_call(env, mr->setVideoSize(width, height), "java/lang/RuntimeException", "setVideoSize failed.");
2799066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project}
2809066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
2819066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectstatic void
2829066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectandroid_media_MediaRecorder_setVideoFrameRate(JNIEnv *env, jobject thiz, jint rate)
2839066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project{
28471f2cf116aab893e224056c38ab146bd1538dd3eSteve Block    ALOGV("setVideoFrameRate(%d)", rate);
285cbe7c5441c9af004dae93cff09787e21771beda8James Dong    if (rate <= 0) {
2869066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        jniThrowException(env, "java/lang/IllegalArgumentException", "invalid frame rate");
2879066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        return;
2889066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
2899066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    sp<MediaRecorder> mr = getMediaRecorder(env, thiz);
2909066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    process_media_recorder_call(env, mr->setVideoFrameRate(rate), "java/lang/RuntimeException", "setVideoFrameRate failed.");
2919066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project}
2929066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
2939066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectstatic void
294ba87e3e6c985e7175152993b5efcc7dd2f0e1c93The Android Open Source Projectandroid_media_MediaRecorder_setMaxDuration(JNIEnv *env, jobject thiz, jint max_duration_ms)
295ba87e3e6c985e7175152993b5efcc7dd2f0e1c93The Android Open Source Project{
29671f2cf116aab893e224056c38ab146bd1538dd3eSteve Block    ALOGV("setMaxDuration(%d)", max_duration_ms);
297ba87e3e6c985e7175152993b5efcc7dd2f0e1c93The Android Open Source Project    sp<MediaRecorder> mr = getMediaRecorder(env, thiz);
298ba87e3e6c985e7175152993b5efcc7dd2f0e1c93The Android Open Source Project
299ba87e3e6c985e7175152993b5efcc7dd2f0e1c93The Android Open Source Project    char params[64];
300ba87e3e6c985e7175152993b5efcc7dd2f0e1c93The Android Open Source Project    sprintf(params, "max-duration=%d", max_duration_ms);
301ba87e3e6c985e7175152993b5efcc7dd2f0e1c93The Android Open Source Project
302ba87e3e6c985e7175152993b5efcc7dd2f0e1c93The Android Open Source Project    process_media_recorder_call(env, mr->setParameters(String8(params)), "java/lang/RuntimeException", "setMaxDuration failed.");
303ba87e3e6c985e7175152993b5efcc7dd2f0e1c93The Android Open Source Project}
304ba87e3e6c985e7175152993b5efcc7dd2f0e1c93The Android Open Source Project
305ba87e3e6c985e7175152993b5efcc7dd2f0e1c93The Android Open Source Projectstatic void
306105925376f8d0f6b318c9938c7b83ef7fef094daThe Android Open Source Projectandroid_media_MediaRecorder_setMaxFileSize(
307105925376f8d0f6b318c9938c7b83ef7fef094daThe Android Open Source Project        JNIEnv *env, jobject thiz, jlong max_filesize_bytes)
308105925376f8d0f6b318c9938c7b83ef7fef094daThe Android Open Source Project{
30971f2cf116aab893e224056c38ab146bd1538dd3eSteve Block    ALOGV("setMaxFileSize(%lld)", max_filesize_bytes);
310105925376f8d0f6b318c9938c7b83ef7fef094daThe Android Open Source Project    sp<MediaRecorder> mr = getMediaRecorder(env, thiz);
311105925376f8d0f6b318c9938c7b83ef7fef094daThe Android Open Source Project
312105925376f8d0f6b318c9938c7b83ef7fef094daThe Android Open Source Project    char params[64];
313853940331ff2be48ed3e63f459845e5e43d0a131Mark Salyzyn    sprintf(params, "max-filesize=%" PRId64, max_filesize_bytes);
314105925376f8d0f6b318c9938c7b83ef7fef094daThe Android Open Source Project
315105925376f8d0f6b318c9938c7b83ef7fef094daThe Android Open Source Project    process_media_recorder_call(env, mr->setParameters(String8(params)), "java/lang/RuntimeException", "setMaxFileSize failed.");
316105925376f8d0f6b318c9938c7b83ef7fef094daThe Android Open Source Project}
317105925376f8d0f6b318c9938c7b83ef7fef094daThe Android Open Source Project
318105925376f8d0f6b318c9938c7b83ef7fef094daThe Android Open Source Projectstatic void
3199066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectandroid_media_MediaRecorder_prepare(JNIEnv *env, jobject thiz)
3209066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project{
32171f2cf116aab893e224056c38ab146bd1538dd3eSteve Block    ALOGV("prepare");
3229066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    sp<MediaRecorder> mr = getMediaRecorder(env, thiz);
3239066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
3249066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    jobject surface = env->GetObjectField(thiz, fields.surface);
3259066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    if (surface != NULL) {
326457d5ae3c9e40c8336b65ea954d1bd5ae1622917James Dong        const sp<Surface> native_surface = get_surface(env, surface);
327d790c6485a388a3e8045dbd18b903b20a7dcf0d9James Dong
328d790c6485a388a3e8045dbd18b903b20a7dcf0d9James Dong        // The application may misbehave and
329d790c6485a388a3e8045dbd18b903b20a7dcf0d9James Dong        // the preview surface becomes unavailable
330d790c6485a388a3e8045dbd18b903b20a7dcf0d9James Dong        if (native_surface.get() == 0) {
3313762c311729fe9f3af085c14c5c1fb471d994c03Steve Block            ALOGE("Application lost the surface");
332d790c6485a388a3e8045dbd18b903b20a7dcf0d9James Dong            jniThrowException(env, "java/io/IOException", "invalid preview surface");
333d790c6485a388a3e8045dbd18b903b20a7dcf0d9James Dong            return;
334d790c6485a388a3e8045dbd18b903b20a7dcf0d9James Dong        }
335d790c6485a388a3e8045dbd18b903b20a7dcf0d9James Dong
336bfcae3595900b79401d635022bd6d30a5b5a6576Mathias Agopian        ALOGI("prepare: surface=%p", native_surface.get());
3374a05f436989a49e06ec762aac9507ad13fcc5e23Mathias Agopian        if (process_media_recorder_call(env, mr->setPreviewSurface(native_surface->getIGraphicBufferProducer()), "java/lang/RuntimeException", "setPreviewSurface failed.")) {
3389066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            return;
3399066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        }
3409066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
3419066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    process_media_recorder_call(env, mr->prepare(), "java/io/IOException", "prepare failed.");
3429066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project}
3439066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
344075e9a19ce645752f8282bc19c91b25978a7dc52Ashok Bhatstatic jint
3459066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectandroid_media_MediaRecorder_native_getMaxAmplitude(JNIEnv *env, jobject thiz)
3469066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project{
34771f2cf116aab893e224056c38ab146bd1538dd3eSteve Block    ALOGV("getMaxAmplitude");
3489066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    sp<MediaRecorder> mr = getMediaRecorder(env, thiz);
3499066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    int result = 0;
3509066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    process_media_recorder_call(env, mr->getMaxAmplitude(&result), "java/lang/RuntimeException", "getMaxAmplitude failed.");
351075e9a19ce645752f8282bc19c91b25978a7dc52Ashok Bhat    return (jint) result;
3529066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project}
3539066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
35483cc994ba40a7227c62a65ccb5addf3a23ff6350Chong Zhangstatic jobject
35583cc994ba40a7227c62a65ccb5addf3a23ff6350Chong Zhangandroid_media_MediaRecorder_getSurface(JNIEnv *env, jobject thiz)
35683cc994ba40a7227c62a65ccb5addf3a23ff6350Chong Zhang{
35783cc994ba40a7227c62a65ccb5addf3a23ff6350Chong Zhang    ALOGV("getSurface");
35883cc994ba40a7227c62a65ccb5addf3a23ff6350Chong Zhang    sp<MediaRecorder> mr = getMediaRecorder(env, thiz);
35983cc994ba40a7227c62a65ccb5addf3a23ff6350Chong Zhang
36083cc994ba40a7227c62a65ccb5addf3a23ff6350Chong Zhang    sp<IGraphicBufferProducer> bufferProducer = mr->querySurfaceMediaSourceFromMediaServer();
36183cc994ba40a7227c62a65ccb5addf3a23ff6350Chong Zhang    if (bufferProducer == NULL) {
36283cc994ba40a7227c62a65ccb5addf3a23ff6350Chong Zhang        jniThrowException(
36383cc994ba40a7227c62a65ccb5addf3a23ff6350Chong Zhang                env,
36483cc994ba40a7227c62a65ccb5addf3a23ff6350Chong Zhang                "java/lang/IllegalStateException",
36583cc994ba40a7227c62a65ccb5addf3a23ff6350Chong Zhang                "failed to get surface");
36683cc994ba40a7227c62a65ccb5addf3a23ff6350Chong Zhang        return NULL;
36783cc994ba40a7227c62a65ccb5addf3a23ff6350Chong Zhang    }
36883cc994ba40a7227c62a65ccb5addf3a23ff6350Chong Zhang
36983cc994ba40a7227c62a65ccb5addf3a23ff6350Chong Zhang    // Wrap the IGBP in a Java-language Surface.
37083cc994ba40a7227c62a65ccb5addf3a23ff6350Chong Zhang    return android_view_Surface_createFromIGraphicBufferProducer(env,
37183cc994ba40a7227c62a65ccb5addf3a23ff6350Chong Zhang            bufferProducer);
37283cc994ba40a7227c62a65ccb5addf3a23ff6350Chong Zhang}
37383cc994ba40a7227c62a65ccb5addf3a23ff6350Chong Zhang
3749066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectstatic void
3759066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectandroid_media_MediaRecorder_start(JNIEnv *env, jobject thiz)
3769066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project{
37771f2cf116aab893e224056c38ab146bd1538dd3eSteve Block    ALOGV("start");
3789066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    sp<MediaRecorder> mr = getMediaRecorder(env, thiz);
3799066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    process_media_recorder_call(env, mr->start(), "java/lang/RuntimeException", "start failed.");
3809066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project}
3819066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
3829066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectstatic void
3839066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectandroid_media_MediaRecorder_stop(JNIEnv *env, jobject thiz)
3849066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project{
38571f2cf116aab893e224056c38ab146bd1538dd3eSteve Block    ALOGV("stop");
3869066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    sp<MediaRecorder> mr = getMediaRecorder(env, thiz);
3879066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    process_media_recorder_call(env, mr->stop(), "java/lang/RuntimeException", "stop failed.");
3889066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project}
3899066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
3909066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectstatic void
3919066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectandroid_media_MediaRecorder_native_reset(JNIEnv *env, jobject thiz)
3929066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project{
39371f2cf116aab893e224056c38ab146bd1538dd3eSteve Block    ALOGV("native_reset");
3949066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    sp<MediaRecorder> mr = getMediaRecorder(env, thiz);
3959066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    process_media_recorder_call(env, mr->reset(), "java/lang/RuntimeException", "native_reset failed.");
3969066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project}
3979066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
3989066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectstatic void
3999066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectandroid_media_MediaRecorder_release(JNIEnv *env, jobject thiz)
4009066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project{
40171f2cf116aab893e224056c38ab146bd1538dd3eSteve Block    ALOGV("release");
4029066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    sp<MediaRecorder> mr = setMediaRecorder(env, thiz, 0);
4039066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    if (mr != NULL) {
4049066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        mr->setListener(NULL);
4054df2423a947bcd3f024cc3d3a1a315a8dc428598The Android Open Source Project        mr->release();
4069066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
4079066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project}
4089066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
4094935d05eaa306cef88cf0ab13eca386f270409ecMarco Nelissen// This function gets some field IDs, which in turn causes class initialization.
4104935d05eaa306cef88cf0ab13eca386f270409ecMarco Nelissen// It is called from a static block in MediaRecorder, which won't run until the
4114935d05eaa306cef88cf0ab13eca386f270409ecMarco Nelissen// first time an instance of this class is used.
4124935d05eaa306cef88cf0ab13eca386f270409ecMarco Nelissenstatic void
4134935d05eaa306cef88cf0ab13eca386f270409ecMarco Nelissenandroid_media_MediaRecorder_native_init(JNIEnv *env)
4144935d05eaa306cef88cf0ab13eca386f270409ecMarco Nelissen{
4154935d05eaa306cef88cf0ab13eca386f270409ecMarco Nelissen    jclass clazz;
4164935d05eaa306cef88cf0ab13eca386f270409ecMarco Nelissen
4174935d05eaa306cef88cf0ab13eca386f270409ecMarco Nelissen    clazz = env->FindClass("android/media/MediaRecorder");
4184935d05eaa306cef88cf0ab13eca386f270409ecMarco Nelissen    if (clazz == NULL) {
4194935d05eaa306cef88cf0ab13eca386f270409ecMarco Nelissen        return;
4204935d05eaa306cef88cf0ab13eca386f270409ecMarco Nelissen    }
4214935d05eaa306cef88cf0ab13eca386f270409ecMarco Nelissen
422075e9a19ce645752f8282bc19c91b25978a7dc52Ashok Bhat    fields.context = env->GetFieldID(clazz, "mNativeContext", "J");
4234935d05eaa306cef88cf0ab13eca386f270409ecMarco Nelissen    if (fields.context == NULL) {
4244935d05eaa306cef88cf0ab13eca386f270409ecMarco Nelissen        return;
4254935d05eaa306cef88cf0ab13eca386f270409ecMarco Nelissen    }
4264935d05eaa306cef88cf0ab13eca386f270409ecMarco Nelissen
4274935d05eaa306cef88cf0ab13eca386f270409ecMarco Nelissen    fields.surface = env->GetFieldID(clazz, "mSurface", "Landroid/view/Surface;");
4284935d05eaa306cef88cf0ab13eca386f270409ecMarco Nelissen    if (fields.surface == NULL) {
4294935d05eaa306cef88cf0ab13eca386f270409ecMarco Nelissen        return;
4304935d05eaa306cef88cf0ab13eca386f270409ecMarco Nelissen    }
4314935d05eaa306cef88cf0ab13eca386f270409ecMarco Nelissen
4324935d05eaa306cef88cf0ab13eca386f270409ecMarco Nelissen    jclass surface = env->FindClass("android/view/Surface");
4334935d05eaa306cef88cf0ab13eca386f270409ecMarco Nelissen    if (surface == NULL) {
4344935d05eaa306cef88cf0ab13eca386f270409ecMarco Nelissen        return;
4354935d05eaa306cef88cf0ab13eca386f270409ecMarco Nelissen    }
4364935d05eaa306cef88cf0ab13eca386f270409ecMarco Nelissen
4374935d05eaa306cef88cf0ab13eca386f270409ecMarco Nelissen    fields.post_event = env->GetStaticMethodID(clazz, "postEventFromNative",
4384935d05eaa306cef88cf0ab13eca386f270409ecMarco Nelissen                                               "(Ljava/lang/Object;IIILjava/lang/Object;)V");
4394935d05eaa306cef88cf0ab13eca386f270409ecMarco Nelissen    if (fields.post_event == NULL) {
4404935d05eaa306cef88cf0ab13eca386f270409ecMarco Nelissen        return;
4414935d05eaa306cef88cf0ab13eca386f270409ecMarco Nelissen    }
4424935d05eaa306cef88cf0ab13eca386f270409ecMarco Nelissen}
4434935d05eaa306cef88cf0ab13eca386f270409ecMarco Nelissen
4444935d05eaa306cef88cf0ab13eca386f270409ecMarco Nelissen
4459066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectstatic void
446788717ca599c714d58b2cb5deea1d37b4a711c07Eino-Ville Talvalaandroid_media_MediaRecorder_native_setup(JNIEnv *env, jobject thiz, jobject weak_this,
447788717ca599c714d58b2cb5deea1d37b4a711c07Eino-Ville Talvala                                         jstring packageName)
4489066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project{
44971f2cf116aab893e224056c38ab146bd1538dd3eSteve Block    ALOGV("setup");
450788717ca599c714d58b2cb5deea1d37b4a711c07Eino-Ville Talvala
4519066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    sp<MediaRecorder> mr = new MediaRecorder();
4529066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    if (mr == NULL) {
4539066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        jniThrowException(env, "java/lang/RuntimeException", "Out of memory");
4549066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        return;
4559066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
4569066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    if (mr->initCheck() != NO_ERROR) {
4578d3b9100a660591cca01f1df801aaf9830b9e3baJames Dong        jniThrowException(env, "java/lang/RuntimeException", "Unable to initialize media recorder");
4589066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        return;
4599066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
4609066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
4619066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    // create new listener and give it to MediaRecorder
4629066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    sp<JNIMediaRecorderListener> listener = new JNIMediaRecorderListener(env, thiz, weak_this);
4639066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    mr->setListener(listener);
4649066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
465788717ca599c714d58b2cb5deea1d37b4a711c07Eino-Ville Talvala   // Convert client name jstring to String16
466788717ca599c714d58b2cb5deea1d37b4a711c07Eino-Ville Talvala    const char16_t *rawClientName = env->GetStringChars(packageName, NULL);
467788717ca599c714d58b2cb5deea1d37b4a711c07Eino-Ville Talvala    jsize rawClientNameLen = env->GetStringLength(packageName);
468788717ca599c714d58b2cb5deea1d37b4a711c07Eino-Ville Talvala    String16 clientName(rawClientName, rawClientNameLen);
469788717ca599c714d58b2cb5deea1d37b4a711c07Eino-Ville Talvala    env->ReleaseStringChars(packageName, rawClientName);
470788717ca599c714d58b2cb5deea1d37b4a711c07Eino-Ville Talvala
471788717ca599c714d58b2cb5deea1d37b4a711c07Eino-Ville Talvala    // pass client package name for permissions tracking
472788717ca599c714d58b2cb5deea1d37b4a711c07Eino-Ville Talvala    mr->setClientName(clientName);
473788717ca599c714d58b2cb5deea1d37b4a711c07Eino-Ville Talvala
4749066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    setMediaRecorder(env, thiz, mr);
4759066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project}
4769066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
4779066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectstatic void
4789066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectandroid_media_MediaRecorder_native_finalize(JNIEnv *env, jobject thiz)
4799066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project{
48071f2cf116aab893e224056c38ab146bd1538dd3eSteve Block    ALOGV("finalize");
4819066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    android_media_MediaRecorder_release(env, thiz);
4829066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project}
4839066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
4849066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project// ----------------------------------------------------------------------------
4859066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
4869066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectstatic JNINativeMethod gMethods[] = {
4879066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    {"setCamera",            "(Landroid/hardware/Camera;)V",    (void *)android_media_MediaRecorder_setCamera},
4889066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    {"setVideoSource",       "(I)V",                            (void *)android_media_MediaRecorder_setVideoSource},
4899066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    {"setAudioSource",       "(I)V",                            (void *)android_media_MediaRecorder_setAudioSource},
4909066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    {"setOutputFormat",      "(I)V",                            (void *)android_media_MediaRecorder_setOutputFormat},
4919066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    {"setVideoEncoder",      "(I)V",                            (void *)android_media_MediaRecorder_setVideoEncoder},
4929066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    {"setAudioEncoder",      "(I)V",                            (void *)android_media_MediaRecorder_setAudioEncoder},
4930fc6bc4cac6391f048f0f2748b3e979effe0924bJames Dong    {"setParameter",         "(Ljava/lang/String;)V",           (void *)android_media_MediaRecorder_setParameter},
4949066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    {"_setOutputFile",       "(Ljava/io/FileDescriptor;JJ)V",   (void *)android_media_MediaRecorder_setOutputFileFD},
4959066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    {"setVideoSize",         "(II)V",                           (void *)android_media_MediaRecorder_setVideoSize},
4969066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    {"setVideoFrameRate",    "(I)V",                            (void *)android_media_MediaRecorder_setVideoFrameRate},
497ba87e3e6c985e7175152993b5efcc7dd2f0e1c93The Android Open Source Project    {"setMaxDuration",       "(I)V",                            (void *)android_media_MediaRecorder_setMaxDuration},
498105925376f8d0f6b318c9938c7b83ef7fef094daThe Android Open Source Project    {"setMaxFileSize",       "(J)V",                            (void *)android_media_MediaRecorder_setMaxFileSize},
4999066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    {"_prepare",             "()V",                             (void *)android_media_MediaRecorder_prepare},
50083cc994ba40a7227c62a65ccb5addf3a23ff6350Chong Zhang    {"getSurface",           "()Landroid/view/Surface;",        (void *)android_media_MediaRecorder_getSurface},
5019066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    {"getMaxAmplitude",      "()I",                             (void *)android_media_MediaRecorder_native_getMaxAmplitude},
5029066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    {"start",                "()V",                             (void *)android_media_MediaRecorder_start},
5039066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    {"stop",                 "()V",                             (void *)android_media_MediaRecorder_stop},
5044935d05eaa306cef88cf0ab13eca386f270409ecMarco Nelissen    {"native_reset",         "()V",                             (void *)android_media_MediaRecorder_native_reset},
5059066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    {"release",              "()V",                             (void *)android_media_MediaRecorder_release},
5064935d05eaa306cef88cf0ab13eca386f270409ecMarco Nelissen    {"native_init",          "()V",                             (void *)android_media_MediaRecorder_native_init},
507788717ca599c714d58b2cb5deea1d37b4a711c07Eino-Ville Talvala    {"native_setup",         "(Ljava/lang/Object;Ljava/lang/String;)V", (void *)android_media_MediaRecorder_native_setup},
5089066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    {"native_finalize",      "()V",                             (void *)android_media_MediaRecorder_native_finalize},
5099066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project};
5109066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
5119066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectstatic const char* const kClassPathName = "android/media/MediaRecorder";
5129066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
5134935d05eaa306cef88cf0ab13eca386f270409ecMarco Nelissen// This function only registers the native methods, and is called from
5144935d05eaa306cef88cf0ab13eca386f270409ecMarco Nelissen// JNI_OnLoad in android_media_MediaPlayer.cpp
5159066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectint register_android_media_MediaRecorder(JNIEnv *env)
5169066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project{
5179066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    return AndroidRuntime::registerNativeMethods(env,
5189066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                "android/media/MediaRecorder", gMethods, NELEM(gMethods));
5199066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project}
520