android_media_AudioSystem.cpp revision da996f390e17e16f2dfa60e972e7ebc4f868f37e
1/* //device/libs/android_runtime/android_media_AudioSystem.cpp 2** 3** Copyright 2006, The Android Open Source Project 4** 5** Licensed under the Apache License, Version 2.0 (the "License"); 6** you may not use this file except in compliance with the License. 7** You may obtain a copy of the License at 8** 9** http://www.apache.org/licenses/LICENSE-2.0 10** 11** Unless required by applicable law or agreed to in writing, software 12** distributed under the License is distributed on an "AS IS" BASIS, 13** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14** See the License for the specific language governing permissions and 15** limitations under the License. 16*/ 17 18#define LOG_TAG "AudioSystem" 19#include "utils/Log.h" 20 21#include <stdio.h> 22#include <unistd.h> 23#include <fcntl.h> 24#include <math.h> 25 26#include "jni.h" 27#include "JNIHelp.h" 28#include "android_runtime/AndroidRuntime.h" 29 30#include <media/AudioSystem.h> 31#include <media/AudioTrack.h> 32 33// ---------------------------------------------------------------------------- 34 35using namespace android; 36 37enum AudioError { 38 kAudioStatusOk = 0, 39 kAudioStatusError = 1, 40 kAudioStatusMediaServerDied = 100 41}; 42 43static int check_AudioSystem_Command(status_t status) 44{ 45 if (status == NO_ERROR) { 46 return kAudioStatusOk; 47 } else { 48 return kAudioStatusError; 49 } 50} 51 52static int 53android_media_AudioSystem_setVolume(JNIEnv *env, jobject clazz, jint type, jint volume) 54{ 55 LOGV("setVolume(%d)", int(volume)); 56 57 return check_AudioSystem_Command(AudioSystem::setStreamVolume(type, AudioSystem::linearToLog(volume))); 58} 59 60static int 61android_media_AudioSystem_getVolume(JNIEnv *env, jobject clazz, jint type) 62{ 63 float v; 64 int v_int = -1; 65 if (AudioSystem::getStreamVolume(int(type), &v) == NO_ERROR) { 66 v_int = AudioSystem::logToLinear(v); 67 } 68 return v_int; 69} 70 71static int 72android_media_AudioSystem_muteMicrophone(JNIEnv *env, jobject thiz, jboolean on) 73{ 74 return check_AudioSystem_Command(AudioSystem::muteMicrophone(on)); 75} 76 77static jboolean 78android_media_AudioSystem_isMicrophoneMuted(JNIEnv *env, jobject thiz) 79{ 80 bool state = false; 81 AudioSystem::isMicrophoneMuted(&state); 82 return state; 83} 84 85static int 86android_media_AudioSystem_setRouting(JNIEnv *env, jobject clazz, jint mode, jint routes, jint mask) 87{ 88 return check_AudioSystem_Command(AudioSystem::setRouting(mode, uint32_t(routes), uint32_t(mask))); 89} 90 91static jint 92android_media_AudioSystem_getRouting(JNIEnv *env, jobject clazz, jint mode) 93{ 94 uint32_t routes = -1; 95 AudioSystem::getRouting(mode, &routes); 96 return jint(routes); 97} 98 99static int 100android_media_AudioSystem_setMode(JNIEnv *env, jobject clazz, jint mode) 101{ 102 return check_AudioSystem_Command(AudioSystem::setMode(mode)); 103} 104 105static jint 106android_media_AudioSystem_getMode(JNIEnv *env, jobject clazz) 107{ 108 int mode = AudioSystem::MODE_INVALID; 109 AudioSystem::getMode(&mode); 110 return jint(mode); 111} 112 113static jboolean 114android_media_AudioSystem_isMusicActive(JNIEnv *env, jobject thiz) 115{ 116 bool state = false; 117 AudioSystem::isMusicActive(&state); 118 return state; 119} 120 121// Temporary interface, do not use 122// TODO: Replace with a more generic key:value get/set mechanism 123static void 124android_media_AudioSystem_setParameter(JNIEnv *env, jobject thiz, jstring key, jstring value) 125{ 126 const char *c_key = env->GetStringUTFChars(key, NULL); 127 const char *c_value = env->GetStringUTFChars(value, NULL); 128 AudioSystem::setParameter(c_key, c_value); 129 env->ReleaseStringUTFChars(key, c_key); 130 env->ReleaseStringUTFChars(value, c_value); 131} 132 133void android_media_AudioSystem_error_callback(status_t err) 134{ 135 JNIEnv *env = AndroidRuntime::getJNIEnv(); 136 jclass clazz = env->FindClass("android/media/AudioSystem"); 137 138 int error; 139 140 switch (err) { 141 case DEAD_OBJECT: 142 error = kAudioStatusMediaServerDied; 143 break; 144 case NO_ERROR: 145 error = kAudioStatusOk; 146 break; 147 default: 148 error = kAudioStatusError; 149 break; 150 } 151 152 env->CallStaticVoidMethod(clazz, env->GetStaticMethodID(clazz, "errorCallbackFromNative","(I)V"), error); 153} 154 155// ---------------------------------------------------------------------------- 156 157static JNINativeMethod gMethods[] = { 158 {"setVolume", "(II)I", (void *)android_media_AudioSystem_setVolume}, 159 {"getVolume", "(I)I", (void *)android_media_AudioSystem_getVolume}, 160 {"setParameter", "(Ljava/lang/String;Ljava/lang/String;)V", (void *)android_media_AudioSystem_setParameter}, 161 {"muteMicrophone", "(Z)I", (void *)android_media_AudioSystem_muteMicrophone}, 162 {"isMicrophoneMuted", "()Z", (void *)android_media_AudioSystem_isMicrophoneMuted}, 163 {"setRouting", "(III)I", (void *)android_media_AudioSystem_setRouting}, 164 {"getRouting", "(I)I", (void *)android_media_AudioSystem_getRouting}, 165 {"setMode", "(I)I", (void *)android_media_AudioSystem_setMode}, 166 {"getMode", "()I", (void *)android_media_AudioSystem_getMode}, 167 {"isMusicActive", "()Z", (void *)android_media_AudioSystem_isMusicActive}, 168}; 169 170const char* const kClassPathName = "android/media/AudioSystem"; 171 172int register_android_media_AudioSystem(JNIEnv *env) 173{ 174 AudioSystem::setErrorCallback(android_media_AudioSystem_error_callback); 175 176 return AndroidRuntime::registerNativeMethods(env, 177 "android/media/AudioSystem", gMethods, NELEM(gMethods)); 178} 179