Glenn Kasten | b3db213 | 2012-01-19 08:59:58 -0800 | [diff] [blame] | 1 | /* |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 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" |
Glenn Kasten | c81d31c | 2012-03-13 14:46:23 -0700 | [diff] [blame] | 19 | #include <utils/Log.h> |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 20 | |
| 21 | #include <stdio.h> |
| 22 | #include <unistd.h> |
| 23 | #include <fcntl.h> |
| 24 | #include <math.h> |
| 25 | |
Glenn Kasten | c81d31c | 2012-03-13 14:46:23 -0700 | [diff] [blame] | 26 | #include <jni.h> |
| 27 | #include <JNIHelp.h> |
| 28 | #include <android_runtime/AndroidRuntime.h> |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 29 | |
| 30 | #include <media/AudioSystem.h> |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 31 | |
Dima Zavin | 34bb419 | 2011-05-11 14:15:23 -0700 | [diff] [blame] | 32 | #include <system/audio.h> |
Dima Zavin | 290029d | 2011-06-13 18:16:26 -0700 | [diff] [blame] | 33 | #include <system/audio_policy.h> |
Dima Zavin | 24fc2fb | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 34 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 35 | // ---------------------------------------------------------------------------- |
| 36 | |
| 37 | using namespace android; |
| 38 | |
Glenn Kasten | ed0079d | 2011-04-04 10:50:50 -0700 | [diff] [blame] | 39 | static const char* const kClassPathName = "android/media/AudioSystem"; |
| 40 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 41 | enum AudioError { |
| 42 | kAudioStatusOk = 0, |
| 43 | kAudioStatusError = 1, |
| 44 | kAudioStatusMediaServerDied = 100 |
| 45 | }; |
| 46 | |
| 47 | static int check_AudioSystem_Command(status_t status) |
| 48 | { |
| 49 | if (status == NO_ERROR) { |
| 50 | return kAudioStatusOk; |
| 51 | } else { |
| 52 | return kAudioStatusError; |
| 53 | } |
| 54 | } |
| 55 | |
| 56 | static int |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 57 | android_media_AudioSystem_muteMicrophone(JNIEnv *env, jobject thiz, jboolean on) |
| 58 | { |
| 59 | return check_AudioSystem_Command(AudioSystem::muteMicrophone(on)); |
| 60 | } |
| 61 | |
| 62 | static jboolean |
| 63 | android_media_AudioSystem_isMicrophoneMuted(JNIEnv *env, jobject thiz) |
| 64 | { |
| 65 | bool state = false; |
| 66 | AudioSystem::isMicrophoneMuted(&state); |
| 67 | return state; |
| 68 | } |
| 69 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 70 | static jboolean |
Eric Laurent | 25101b0 | 2011-02-02 09:33:30 -0800 | [diff] [blame] | 71 | android_media_AudioSystem_isStreamActive(JNIEnv *env, jobject thiz, jint stream, jint inPastMs) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 72 | { |
| 73 | bool state = false; |
Glenn Kasten | bc1d77b | 2012-01-12 16:38:12 -0800 | [diff] [blame] | 74 | AudioSystem::isStreamActive((audio_stream_type_t) stream, &state, inPastMs); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 75 | return state; |
| 76 | } |
| 77 | |
Jean-Michel Trivi | d677054 | 2012-10-10 12:03:41 -0700 | [diff] [blame] | 78 | static jboolean |
Jean-Michel Trivi | 679d504 | 2013-02-04 16:24:09 -0800 | [diff] [blame] | 79 | android_media_AudioSystem_isStreamActiveRemotely(JNIEnv *env, jobject thiz, jint stream, |
| 80 | jint inPastMs) |
| 81 | { |
| 82 | bool state = false; |
| 83 | AudioSystem::isStreamActiveRemotely((audio_stream_type_t) stream, &state, inPastMs); |
| 84 | return state; |
| 85 | } |
| 86 | |
| 87 | static jboolean |
Jean-Michel Trivi | d677054 | 2012-10-10 12:03:41 -0700 | [diff] [blame] | 88 | android_media_AudioSystem_isSourceActive(JNIEnv *env, jobject thiz, jint source) |
| 89 | { |
| 90 | bool state = false; |
| 91 | AudioSystem::isSourceActive((audio_source_t) source, &state); |
| 92 | return state; |
| 93 | } |
| 94 | |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 95 | static int |
| 96 | android_media_AudioSystem_setParameters(JNIEnv *env, jobject thiz, jstring keyValuePairs) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 97 | { |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 98 | const jchar* c_keyValuePairs = env->GetStringCritical(keyValuePairs, 0); |
| 99 | String8 c_keyValuePairs8; |
| 100 | if (keyValuePairs) { |
| 101 | c_keyValuePairs8 = String8(c_keyValuePairs, env->GetStringLength(keyValuePairs)); |
| 102 | env->ReleaseStringCritical(keyValuePairs, c_keyValuePairs); |
| 103 | } |
| 104 | int status = check_AudioSystem_Command(AudioSystem::setParameters(0, c_keyValuePairs8)); |
| 105 | return status; |
| 106 | } |
| 107 | |
| 108 | static jstring |
| 109 | android_media_AudioSystem_getParameters(JNIEnv *env, jobject thiz, jstring keys) |
| 110 | { |
| 111 | const jchar* c_keys = env->GetStringCritical(keys, 0); |
| 112 | String8 c_keys8; |
| 113 | if (keys) { |
| 114 | c_keys8 = String8(c_keys, env->GetStringLength(keys)); |
| 115 | env->ReleaseStringCritical(keys, c_keys); |
| 116 | } |
| 117 | return env->NewStringUTF(AudioSystem::getParameters(0, c_keys8).string()); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 118 | } |
| 119 | |
Glenn Kasten | ed0079d | 2011-04-04 10:50:50 -0700 | [diff] [blame] | 120 | static void |
| 121 | android_media_AudioSystem_error_callback(status_t err) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 122 | { |
| 123 | JNIEnv *env = AndroidRuntime::getJNIEnv(); |
Eric Laurent | 0e7a4d9 | 2010-10-15 12:33:16 -0700 | [diff] [blame] | 124 | if (env == NULL) { |
| 125 | return; |
| 126 | } |
| 127 | |
Glenn Kasten | ed0079d | 2011-04-04 10:50:50 -0700 | [diff] [blame] | 128 | jclass clazz = env->FindClass(kClassPathName); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 129 | |
| 130 | int error; |
| 131 | |
| 132 | switch (err) { |
| 133 | case DEAD_OBJECT: |
| 134 | error = kAudioStatusMediaServerDied; |
| 135 | break; |
| 136 | case NO_ERROR: |
| 137 | error = kAudioStatusOk; |
| 138 | break; |
| 139 | default: |
| 140 | error = kAudioStatusError; |
| 141 | break; |
| 142 | } |
| 143 | |
| 144 | env->CallStaticVoidMethod(clazz, env->GetStaticMethodID(clazz, "errorCallbackFromNative","(I)V"), error); |
| 145 | } |
| 146 | |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 147 | static int |
| 148 | android_media_AudioSystem_setDeviceConnectionState(JNIEnv *env, jobject thiz, jint device, jint state, jstring device_address) |
| 149 | { |
| 150 | const char *c_address = env->GetStringUTFChars(device_address, NULL); |
Dima Zavin | 24fc2fb | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 151 | int status = check_AudioSystem_Command(AudioSystem::setDeviceConnectionState(static_cast <audio_devices_t>(device), |
| 152 | static_cast <audio_policy_dev_state_t>(state), |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 153 | c_address)); |
| 154 | env->ReleaseStringUTFChars(device_address, c_address); |
| 155 | return status; |
| 156 | } |
| 157 | |
| 158 | static int |
| 159 | android_media_AudioSystem_getDeviceConnectionState(JNIEnv *env, jobject thiz, jint device, jstring device_address) |
| 160 | { |
| 161 | const char *c_address = env->GetStringUTFChars(device_address, NULL); |
Dima Zavin | 24fc2fb | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 162 | int state = static_cast <int>(AudioSystem::getDeviceConnectionState(static_cast <audio_devices_t>(device), |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 163 | c_address)); |
| 164 | env->ReleaseStringUTFChars(device_address, c_address); |
| 165 | return state; |
| 166 | } |
| 167 | |
| 168 | static int |
| 169 | android_media_AudioSystem_setPhoneState(JNIEnv *env, jobject thiz, jint state) |
| 170 | { |
Glenn Kasten | accb114 | 2012-01-04 11:00:47 -0800 | [diff] [blame] | 171 | return check_AudioSystem_Command(AudioSystem::setPhoneState((audio_mode_t) state)); |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 172 | } |
| 173 | |
| 174 | static int |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 175 | android_media_AudioSystem_setForceUse(JNIEnv *env, jobject thiz, jint usage, jint config) |
| 176 | { |
Dima Zavin | 24fc2fb | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 177 | return check_AudioSystem_Command(AudioSystem::setForceUse(static_cast <audio_policy_force_use_t>(usage), |
| 178 | static_cast <audio_policy_forced_cfg_t>(config))); |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 179 | } |
| 180 | |
| 181 | static int |
| 182 | android_media_AudioSystem_getForceUse(JNIEnv *env, jobject thiz, jint usage) |
| 183 | { |
Dima Zavin | 24fc2fb | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 184 | return static_cast <int>(AudioSystem::getForceUse(static_cast <audio_policy_force_use_t>(usage))); |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 185 | } |
| 186 | |
| 187 | static int |
| 188 | android_media_AudioSystem_initStreamVolume(JNIEnv *env, jobject thiz, jint stream, jint indexMin, jint indexMax) |
| 189 | { |
Dima Zavin | 24fc2fb | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 190 | return check_AudioSystem_Command(AudioSystem::initStreamVolume(static_cast <audio_stream_type_t>(stream), |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 191 | indexMin, |
| 192 | indexMax)); |
| 193 | } |
| 194 | |
| 195 | static int |
Eric Laurent | 9bc8358d | 2011-11-18 16:43:31 -0800 | [diff] [blame] | 196 | android_media_AudioSystem_setStreamVolumeIndex(JNIEnv *env, |
| 197 | jobject thiz, |
| 198 | jint stream, |
| 199 | jint index, |
| 200 | jint device) |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 201 | { |
Eric Laurent | 9bc8358d | 2011-11-18 16:43:31 -0800 | [diff] [blame] | 202 | return check_AudioSystem_Command( |
| 203 | AudioSystem::setStreamVolumeIndex(static_cast <audio_stream_type_t>(stream), |
| 204 | index, |
| 205 | (audio_devices_t)device)); |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 206 | } |
| 207 | |
| 208 | static int |
Eric Laurent | 9bc8358d | 2011-11-18 16:43:31 -0800 | [diff] [blame] | 209 | android_media_AudioSystem_getStreamVolumeIndex(JNIEnv *env, |
| 210 | jobject thiz, |
| 211 | jint stream, |
| 212 | jint device) |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 213 | { |
| 214 | int index; |
Eric Laurent | 9bc8358d | 2011-11-18 16:43:31 -0800 | [diff] [blame] | 215 | if (AudioSystem::getStreamVolumeIndex(static_cast <audio_stream_type_t>(stream), |
| 216 | &index, |
| 217 | (audio_devices_t)device) |
| 218 | != NO_ERROR) { |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 219 | index = -1; |
| 220 | } |
| 221 | return index; |
| 222 | } |
| 223 | |
Mike Lockwood | cbdb49d | 2011-10-20 12:54:05 -0400 | [diff] [blame] | 224 | static int |
| 225 | android_media_AudioSystem_setMasterVolume(JNIEnv *env, jobject thiz, jfloat value) |
| 226 | { |
| 227 | return check_AudioSystem_Command(AudioSystem::setMasterVolume(value)); |
| 228 | } |
| 229 | |
| 230 | static jfloat |
| 231 | android_media_AudioSystem_getMasterVolume(JNIEnv *env, jobject thiz) |
| 232 | { |
| 233 | float value; |
| 234 | if (AudioSystem::getMasterVolume(&value) != NO_ERROR) { |
| 235 | value = -1.0; |
| 236 | } |
| 237 | return value; |
| 238 | } |
| 239 | |
| 240 | static int |
| 241 | android_media_AudioSystem_setMasterMute(JNIEnv *env, jobject thiz, jboolean mute) |
| 242 | { |
| 243 | return check_AudioSystem_Command(AudioSystem::setMasterMute(mute)); |
| 244 | } |
| 245 | |
| 246 | static jfloat |
| 247 | android_media_AudioSystem_getMasterMute(JNIEnv *env, jobject thiz) |
| 248 | { |
| 249 | bool mute; |
| 250 | if (AudioSystem::getMasterMute(&mute) != NO_ERROR) { |
| 251 | mute = false; |
| 252 | } |
| 253 | return mute; |
| 254 | } |
| 255 | |
Glenn Kasten | 8b4b97a | 2011-02-04 13:54:26 -0800 | [diff] [blame] | 256 | static jint |
| 257 | android_media_AudioSystem_getDevicesForStream(JNIEnv *env, jobject thiz, jint stream) |
| 258 | { |
Dima Zavin | 24fc2fb | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 259 | return (jint) AudioSystem::getDevicesForStream(static_cast <audio_stream_type_t>(stream)); |
Glenn Kasten | 8b4b97a | 2011-02-04 13:54:26 -0800 | [diff] [blame] | 260 | } |
| 261 | |
Glenn Kasten | c6c4365 | 2012-09-24 17:32:30 -0700 | [diff] [blame] | 262 | static jint |
| 263 | android_media_AudioSystem_getPrimaryOutputSamplingRate(JNIEnv *env, jobject clazz) |
| 264 | { |
| 265 | return (jint) AudioSystem::getPrimaryOutputSamplingRate(); |
| 266 | } |
| 267 | |
| 268 | static jint |
| 269 | android_media_AudioSystem_getPrimaryOutputFrameCount(JNIEnv *env, jobject clazz) |
| 270 | { |
| 271 | return (jint) AudioSystem::getPrimaryOutputFrameCount(); |
| 272 | } |
| 273 | |
Oliver Woodman | 61dcdf3 | 2013-06-26 12:43:36 +0100 | [diff] [blame] | 274 | static jint |
| 275 | android_media_AudioSystem_getOutputLatency(JNIEnv *env, jobject clazz, jint stream) |
| 276 | { |
| 277 | uint32_t afLatency; |
| 278 | if (AudioSystem::getOutputLatency(&afLatency, static_cast <audio_stream_type_t>(stream)) |
| 279 | != NO_ERROR) { |
| 280 | afLatency = -1; |
| 281 | } |
| 282 | return (jint) afLatency; |
| 283 | } |
| 284 | |
Glenn Kasten | fd116ad | 2013-07-12 17:10:39 -0700 | [diff] [blame^] | 285 | static jint |
| 286 | android_media_AudioSystem_setLowRamDevice(JNIEnv *env, jobject clazz, jboolean isLowRamDevice) |
| 287 | { |
| 288 | return (jint) AudioSystem::setLowRamDevice((bool) isLowRamDevice); |
| 289 | } |
| 290 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 291 | // ---------------------------------------------------------------------------- |
| 292 | |
| 293 | static JNINativeMethod gMethods[] = { |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 294 | {"setParameters", "(Ljava/lang/String;)I", (void *)android_media_AudioSystem_setParameters}, |
| 295 | {"getParameters", "(Ljava/lang/String;)Ljava/lang/String;", (void *)android_media_AudioSystem_getParameters}, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 296 | {"muteMicrophone", "(Z)I", (void *)android_media_AudioSystem_muteMicrophone}, |
| 297 | {"isMicrophoneMuted", "()Z", (void *)android_media_AudioSystem_isMicrophoneMuted}, |
Jean-Michel Trivi | d677054 | 2012-10-10 12:03:41 -0700 | [diff] [blame] | 298 | {"isStreamActive", "(II)Z", (void *)android_media_AudioSystem_isStreamActive}, |
Jean-Michel Trivi | 679d504 | 2013-02-04 16:24:09 -0800 | [diff] [blame] | 299 | {"isStreamActiveRemotely","(II)Z", (void *)android_media_AudioSystem_isStreamActiveRemotely}, |
Jean-Michel Trivi | d677054 | 2012-10-10 12:03:41 -0700 | [diff] [blame] | 300 | {"isSourceActive", "(I)Z", (void *)android_media_AudioSystem_isSourceActive}, |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 301 | {"setDeviceConnectionState", "(IILjava/lang/String;)I", (void *)android_media_AudioSystem_setDeviceConnectionState}, |
| 302 | {"getDeviceConnectionState", "(ILjava/lang/String;)I", (void *)android_media_AudioSystem_getDeviceConnectionState}, |
| 303 | {"setPhoneState", "(I)I", (void *)android_media_AudioSystem_setPhoneState}, |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 304 | {"setForceUse", "(II)I", (void *)android_media_AudioSystem_setForceUse}, |
| 305 | {"getForceUse", "(I)I", (void *)android_media_AudioSystem_getForceUse}, |
| 306 | {"initStreamVolume", "(III)I", (void *)android_media_AudioSystem_initStreamVolume}, |
Eric Laurent | 9bc8358d | 2011-11-18 16:43:31 -0800 | [diff] [blame] | 307 | {"setStreamVolumeIndex","(III)I", (void *)android_media_AudioSystem_setStreamVolumeIndex}, |
| 308 | {"getStreamVolumeIndex","(II)I", (void *)android_media_AudioSystem_getStreamVolumeIndex}, |
Mike Lockwood | cbdb49d | 2011-10-20 12:54:05 -0400 | [diff] [blame] | 309 | {"setMasterVolume", "(F)I", (void *)android_media_AudioSystem_setMasterVolume}, |
| 310 | {"getMasterVolume", "()F", (void *)android_media_AudioSystem_getMasterVolume}, |
| 311 | {"setMasterMute", "(Z)I", (void *)android_media_AudioSystem_setMasterMute}, |
| 312 | {"getMasterMute", "()Z", (void *)android_media_AudioSystem_getMasterMute}, |
Glenn Kasten | 8b4b97a | 2011-02-04 13:54:26 -0800 | [diff] [blame] | 313 | {"getDevicesForStream", "(I)I", (void *)android_media_AudioSystem_getDevicesForStream}, |
Glenn Kasten | c6c4365 | 2012-09-24 17:32:30 -0700 | [diff] [blame] | 314 | {"getPrimaryOutputSamplingRate", "()I", (void *)android_media_AudioSystem_getPrimaryOutputSamplingRate}, |
| 315 | {"getPrimaryOutputFrameCount", "()I", (void *)android_media_AudioSystem_getPrimaryOutputFrameCount}, |
Oliver Woodman | 61dcdf3 | 2013-06-26 12:43:36 +0100 | [diff] [blame] | 316 | {"getOutputLatency", "(I)I", (void *)android_media_AudioSystem_getOutputLatency}, |
Glenn Kasten | fd116ad | 2013-07-12 17:10:39 -0700 | [diff] [blame^] | 317 | {"setLowRamDevice", "(Z)I", (void *)android_media_AudioSystem_setLowRamDevice}, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 318 | }; |
| 319 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 320 | int register_android_media_AudioSystem(JNIEnv *env) |
| 321 | { |
| 322 | AudioSystem::setErrorCallback(android_media_AudioSystem_error_callback); |
Glenn Kasten | 18db49a | 2012-03-12 16:29:55 -0700 | [diff] [blame] | 323 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 324 | return AndroidRuntime::registerNativeMethods(env, |
Glenn Kasten | ed0079d | 2011-04-04 10:50:50 -0700 | [diff] [blame] | 325 | kClassPathName, gMethods, NELEM(gMethods)); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 326 | } |