blob: a9a62f8becb4151c8d1915773f0b92cd261a5877 [file] [log] [blame]
Glenn Kastenb3db2132012-01-19 08:59:58 -08001/*
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002**
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 Kastenc81d31c2012-03-13 14:46:23 -070019#include <utils/Log.h>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080020
Glenn Kastenc81d31c2012-03-13 14:46:23 -070021#include <jni.h>
22#include <JNIHelp.h>
23#include <android_runtime/AndroidRuntime.h>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080024
25#include <media/AudioSystem.h>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080026
Dima Zavin34bb4192011-05-11 14:15:23 -070027#include <system/audio.h>
Dima Zavin290029d2011-06-13 18:16:26 -070028#include <system/audio_policy.h>
Dima Zavin24fc2fb2011-04-19 22:30:36 -070029
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080030// ----------------------------------------------------------------------------
31
32using namespace android;
33
Glenn Kastened0079d2011-04-04 10:50:50 -070034static const char* const kClassPathName = "android/media/AudioSystem";
35
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080036enum AudioError {
37 kAudioStatusOk = 0,
38 kAudioStatusError = 1,
39 kAudioStatusMediaServerDied = 100
40};
41
42static int check_AudioSystem_Command(status_t status)
43{
Eric Laurentdfb881f2013-07-18 14:41:39 -070044 switch (status) {
45 case DEAD_OBJECT:
46 return kAudioStatusMediaServerDied;
47 case NO_ERROR:
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080048 return kAudioStatusOk;
Eric Laurentdfb881f2013-07-18 14:41:39 -070049 default:
50 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080051 }
Eric Laurentdfb881f2013-07-18 14:41:39 -070052 return kAudioStatusError;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080053}
54
Ashok Bhat075e9a12014-01-06 13:45:09 +000055static jint
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080056android_media_AudioSystem_muteMicrophone(JNIEnv *env, jobject thiz, jboolean on)
57{
Ashok Bhat075e9a12014-01-06 13:45:09 +000058 return (jint) check_AudioSystem_Command(AudioSystem::muteMicrophone(on));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080059}
60
61static jboolean
62android_media_AudioSystem_isMicrophoneMuted(JNIEnv *env, jobject thiz)
63{
64 bool state = false;
65 AudioSystem::isMicrophoneMuted(&state);
66 return state;
67}
68
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080069static jboolean
Eric Laurent25101b02011-02-02 09:33:30 -080070android_media_AudioSystem_isStreamActive(JNIEnv *env, jobject thiz, jint stream, jint inPastMs)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080071{
72 bool state = false;
Glenn Kastenbc1d77b2012-01-12 16:38:12 -080073 AudioSystem::isStreamActive((audio_stream_type_t) stream, &state, inPastMs);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080074 return state;
75}
76
Jean-Michel Trivid6770542012-10-10 12:03:41 -070077static jboolean
Jean-Michel Trivi679d5042013-02-04 16:24:09 -080078android_media_AudioSystem_isStreamActiveRemotely(JNIEnv *env, jobject thiz, jint stream,
79 jint inPastMs)
80{
81 bool state = false;
82 AudioSystem::isStreamActiveRemotely((audio_stream_type_t) stream, &state, inPastMs);
83 return state;
84}
85
86static jboolean
Jean-Michel Trivid6770542012-10-10 12:03:41 -070087android_media_AudioSystem_isSourceActive(JNIEnv *env, jobject thiz, jint source)
88{
89 bool state = false;
90 AudioSystem::isSourceActive((audio_source_t) source, &state);
91 return state;
92}
93
Ashok Bhat075e9a12014-01-06 13:45:09 +000094static jint
Eric Laurenta553c252009-07-17 12:17:14 -070095android_media_AudioSystem_setParameters(JNIEnv *env, jobject thiz, jstring keyValuePairs)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080096{
Eric Laurenta553c252009-07-17 12:17:14 -070097 const jchar* c_keyValuePairs = env->GetStringCritical(keyValuePairs, 0);
98 String8 c_keyValuePairs8;
99 if (keyValuePairs) {
100 c_keyValuePairs8 = String8(c_keyValuePairs, env->GetStringLength(keyValuePairs));
101 env->ReleaseStringCritical(keyValuePairs, c_keyValuePairs);
102 }
Glenn Kastenc94752a2013-12-19 12:40:33 -0800103 int status = check_AudioSystem_Command(AudioSystem::setParameters(c_keyValuePairs8));
Ashok Bhat075e9a12014-01-06 13:45:09 +0000104 return (jint) status;
Eric Laurenta553c252009-07-17 12:17:14 -0700105}
106
107static jstring
108android_media_AudioSystem_getParameters(JNIEnv *env, jobject thiz, jstring keys)
109{
110 const jchar* c_keys = env->GetStringCritical(keys, 0);
111 String8 c_keys8;
112 if (keys) {
113 c_keys8 = String8(c_keys, env->GetStringLength(keys));
114 env->ReleaseStringCritical(keys, c_keys);
115 }
Glenn Kastenc94752a2013-12-19 12:40:33 -0800116 return env->NewStringUTF(AudioSystem::getParameters(c_keys8).string());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800117}
118
Glenn Kastened0079d2011-04-04 10:50:50 -0700119static void
120android_media_AudioSystem_error_callback(status_t err)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800121{
Eric Laurent539719a2013-07-18 14:08:00 -0700122 JNIEnv *env = AndroidRuntime::getJNIEnv();
Eric Laurent0e7a4d92010-10-15 12:33:16 -0700123 if (env == NULL) {
124 return;
125 }
126
Glenn Kastened0079d2011-04-04 10:50:50 -0700127 jclass clazz = env->FindClass(kClassPathName);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800128
Eric Laurentdfb881f2013-07-18 14:41:39 -0700129 env->CallStaticVoidMethod(clazz, env->GetStaticMethodID(clazz,
130 "errorCallbackFromNative","(I)V"),
131 check_AudioSystem_Command(err));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800132}
133
Ashok Bhat075e9a12014-01-06 13:45:09 +0000134static jint
Eric Laurenta553c252009-07-17 12:17:14 -0700135android_media_AudioSystem_setDeviceConnectionState(JNIEnv *env, jobject thiz, jint device, jint state, jstring device_address)
136{
137 const char *c_address = env->GetStringUTFChars(device_address, NULL);
Dima Zavin24fc2fb2011-04-19 22:30:36 -0700138 int status = check_AudioSystem_Command(AudioSystem::setDeviceConnectionState(static_cast <audio_devices_t>(device),
139 static_cast <audio_policy_dev_state_t>(state),
Eric Laurenta553c252009-07-17 12:17:14 -0700140 c_address));
141 env->ReleaseStringUTFChars(device_address, c_address);
Ashok Bhat075e9a12014-01-06 13:45:09 +0000142 return (jint) status;
Eric Laurenta553c252009-07-17 12:17:14 -0700143}
144
Ashok Bhat075e9a12014-01-06 13:45:09 +0000145static jint
Eric Laurenta553c252009-07-17 12:17:14 -0700146android_media_AudioSystem_getDeviceConnectionState(JNIEnv *env, jobject thiz, jint device, jstring device_address)
147{
148 const char *c_address = env->GetStringUTFChars(device_address, NULL);
Dima Zavin24fc2fb2011-04-19 22:30:36 -0700149 int state = static_cast <int>(AudioSystem::getDeviceConnectionState(static_cast <audio_devices_t>(device),
Eric Laurenta553c252009-07-17 12:17:14 -0700150 c_address));
151 env->ReleaseStringUTFChars(device_address, c_address);
Ashok Bhat075e9a12014-01-06 13:45:09 +0000152 return (jint) state;
Eric Laurenta553c252009-07-17 12:17:14 -0700153}
154
Ashok Bhat075e9a12014-01-06 13:45:09 +0000155static jint
Eric Laurenta553c252009-07-17 12:17:14 -0700156android_media_AudioSystem_setPhoneState(JNIEnv *env, jobject thiz, jint state)
157{
Ashok Bhat075e9a12014-01-06 13:45:09 +0000158 return (jint) check_AudioSystem_Command(AudioSystem::setPhoneState((audio_mode_t) state));
Eric Laurenta553c252009-07-17 12:17:14 -0700159}
160
Ashok Bhat075e9a12014-01-06 13:45:09 +0000161static jint
Eric Laurenta553c252009-07-17 12:17:14 -0700162android_media_AudioSystem_setForceUse(JNIEnv *env, jobject thiz, jint usage, jint config)
163{
Ashok Bhat075e9a12014-01-06 13:45:09 +0000164 return (jint) check_AudioSystem_Command(AudioSystem::setForceUse(static_cast <audio_policy_force_use_t>(usage),
Dima Zavin24fc2fb2011-04-19 22:30:36 -0700165 static_cast <audio_policy_forced_cfg_t>(config)));
Eric Laurenta553c252009-07-17 12:17:14 -0700166}
167
Ashok Bhat075e9a12014-01-06 13:45:09 +0000168static jint
Eric Laurenta553c252009-07-17 12:17:14 -0700169android_media_AudioSystem_getForceUse(JNIEnv *env, jobject thiz, jint usage)
170{
Ashok Bhat075e9a12014-01-06 13:45:09 +0000171 return static_cast <jint>(AudioSystem::getForceUse(static_cast <audio_policy_force_use_t>(usage)));
Eric Laurenta553c252009-07-17 12:17:14 -0700172}
173
Ashok Bhat075e9a12014-01-06 13:45:09 +0000174static jint
Eric Laurenta553c252009-07-17 12:17:14 -0700175android_media_AudioSystem_initStreamVolume(JNIEnv *env, jobject thiz, jint stream, jint indexMin, jint indexMax)
176{
Ashok Bhat075e9a12014-01-06 13:45:09 +0000177 return (jint) check_AudioSystem_Command(AudioSystem::initStreamVolume(static_cast <audio_stream_type_t>(stream),
Eric Laurenta553c252009-07-17 12:17:14 -0700178 indexMin,
179 indexMax));
180}
181
Ashok Bhat075e9a12014-01-06 13:45:09 +0000182static jint
Eric Laurent9bc8358d2011-11-18 16:43:31 -0800183android_media_AudioSystem_setStreamVolumeIndex(JNIEnv *env,
184 jobject thiz,
185 jint stream,
186 jint index,
187 jint device)
Eric Laurenta553c252009-07-17 12:17:14 -0700188{
Ashok Bhat075e9a12014-01-06 13:45:09 +0000189 return (jint) check_AudioSystem_Command(
Eric Laurent9bc8358d2011-11-18 16:43:31 -0800190 AudioSystem::setStreamVolumeIndex(static_cast <audio_stream_type_t>(stream),
191 index,
192 (audio_devices_t)device));
Eric Laurenta553c252009-07-17 12:17:14 -0700193}
194
Ashok Bhat075e9a12014-01-06 13:45:09 +0000195static jint
Eric Laurent9bc8358d2011-11-18 16:43:31 -0800196android_media_AudioSystem_getStreamVolumeIndex(JNIEnv *env,
197 jobject thiz,
198 jint stream,
199 jint device)
Eric Laurenta553c252009-07-17 12:17:14 -0700200{
201 int index;
Eric Laurent9bc8358d2011-11-18 16:43:31 -0800202 if (AudioSystem::getStreamVolumeIndex(static_cast <audio_stream_type_t>(stream),
203 &index,
204 (audio_devices_t)device)
205 != NO_ERROR) {
Eric Laurenta553c252009-07-17 12:17:14 -0700206 index = -1;
207 }
Ashok Bhat075e9a12014-01-06 13:45:09 +0000208 return (jint) index;
Eric Laurenta553c252009-07-17 12:17:14 -0700209}
210
Ashok Bhat075e9a12014-01-06 13:45:09 +0000211static jint
Mike Lockwoodcbdb49d2011-10-20 12:54:05 -0400212android_media_AudioSystem_setMasterVolume(JNIEnv *env, jobject thiz, jfloat value)
213{
Ashok Bhat075e9a12014-01-06 13:45:09 +0000214 return (jint) check_AudioSystem_Command(AudioSystem::setMasterVolume(value));
Mike Lockwoodcbdb49d2011-10-20 12:54:05 -0400215}
216
217static jfloat
218android_media_AudioSystem_getMasterVolume(JNIEnv *env, jobject thiz)
219{
220 float value;
221 if (AudioSystem::getMasterVolume(&value) != NO_ERROR) {
222 value = -1.0;
223 }
224 return value;
225}
226
Ashok Bhat075e9a12014-01-06 13:45:09 +0000227static jint
Mike Lockwoodcbdb49d2011-10-20 12:54:05 -0400228android_media_AudioSystem_setMasterMute(JNIEnv *env, jobject thiz, jboolean mute)
229{
Ashok Bhat075e9a12014-01-06 13:45:09 +0000230 return (jint) check_AudioSystem_Command(AudioSystem::setMasterMute(mute));
Mike Lockwoodcbdb49d2011-10-20 12:54:05 -0400231}
232
233static jfloat
234android_media_AudioSystem_getMasterMute(JNIEnv *env, jobject thiz)
235{
236 bool mute;
237 if (AudioSystem::getMasterMute(&mute) != NO_ERROR) {
238 mute = false;
239 }
240 return mute;
241}
242
Glenn Kasten8b4b97a2011-02-04 13:54:26 -0800243static jint
244android_media_AudioSystem_getDevicesForStream(JNIEnv *env, jobject thiz, jint stream)
245{
Dima Zavin24fc2fb2011-04-19 22:30:36 -0700246 return (jint) AudioSystem::getDevicesForStream(static_cast <audio_stream_type_t>(stream));
Glenn Kasten8b4b97a2011-02-04 13:54:26 -0800247}
248
Glenn Kastenc6c43652012-09-24 17:32:30 -0700249static jint
250android_media_AudioSystem_getPrimaryOutputSamplingRate(JNIEnv *env, jobject clazz)
251{
252 return (jint) AudioSystem::getPrimaryOutputSamplingRate();
253}
254
255static jint
256android_media_AudioSystem_getPrimaryOutputFrameCount(JNIEnv *env, jobject clazz)
257{
258 return (jint) AudioSystem::getPrimaryOutputFrameCount();
259}
260
Oliver Woodman61dcdf32013-06-26 12:43:36 +0100261static jint
262android_media_AudioSystem_getOutputLatency(JNIEnv *env, jobject clazz, jint stream)
263{
264 uint32_t afLatency;
265 if (AudioSystem::getOutputLatency(&afLatency, static_cast <audio_stream_type_t>(stream))
266 != NO_ERROR) {
267 afLatency = -1;
268 }
269 return (jint) afLatency;
270}
271
Glenn Kastenfd116ad2013-07-12 17:10:39 -0700272static jint
273android_media_AudioSystem_setLowRamDevice(JNIEnv *env, jobject clazz, jboolean isLowRamDevice)
274{
275 return (jint) AudioSystem::setLowRamDevice((bool) isLowRamDevice);
276}
277
Ashok Bhat075e9a12014-01-06 13:45:09 +0000278static jint
Eric Laurentdfb881f2013-07-18 14:41:39 -0700279android_media_AudioSystem_checkAudioFlinger(JNIEnv *env, jobject clazz)
280{
Ashok Bhat075e9a12014-01-06 13:45:09 +0000281 return (jint) check_AudioSystem_Command(AudioSystem::checkAudioFlinger());
Eric Laurentdfb881f2013-07-18 14:41:39 -0700282}
283
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800284// ----------------------------------------------------------------------------
285
286static JNINativeMethod gMethods[] = {
Eric Laurenta553c252009-07-17 12:17:14 -0700287 {"setParameters", "(Ljava/lang/String;)I", (void *)android_media_AudioSystem_setParameters},
288 {"getParameters", "(Ljava/lang/String;)Ljava/lang/String;", (void *)android_media_AudioSystem_getParameters},
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800289 {"muteMicrophone", "(Z)I", (void *)android_media_AudioSystem_muteMicrophone},
290 {"isMicrophoneMuted", "()Z", (void *)android_media_AudioSystem_isMicrophoneMuted},
Jean-Michel Trivid6770542012-10-10 12:03:41 -0700291 {"isStreamActive", "(II)Z", (void *)android_media_AudioSystem_isStreamActive},
Jean-Michel Trivi679d5042013-02-04 16:24:09 -0800292 {"isStreamActiveRemotely","(II)Z", (void *)android_media_AudioSystem_isStreamActiveRemotely},
Jean-Michel Trivid6770542012-10-10 12:03:41 -0700293 {"isSourceActive", "(I)Z", (void *)android_media_AudioSystem_isSourceActive},
Eric Laurenta553c252009-07-17 12:17:14 -0700294 {"setDeviceConnectionState", "(IILjava/lang/String;)I", (void *)android_media_AudioSystem_setDeviceConnectionState},
295 {"getDeviceConnectionState", "(ILjava/lang/String;)I", (void *)android_media_AudioSystem_getDeviceConnectionState},
296 {"setPhoneState", "(I)I", (void *)android_media_AudioSystem_setPhoneState},
Eric Laurenta553c252009-07-17 12:17:14 -0700297 {"setForceUse", "(II)I", (void *)android_media_AudioSystem_setForceUse},
298 {"getForceUse", "(I)I", (void *)android_media_AudioSystem_getForceUse},
299 {"initStreamVolume", "(III)I", (void *)android_media_AudioSystem_initStreamVolume},
Eric Laurent9bc8358d2011-11-18 16:43:31 -0800300 {"setStreamVolumeIndex","(III)I", (void *)android_media_AudioSystem_setStreamVolumeIndex},
301 {"getStreamVolumeIndex","(II)I", (void *)android_media_AudioSystem_getStreamVolumeIndex},
Mike Lockwoodcbdb49d2011-10-20 12:54:05 -0400302 {"setMasterVolume", "(F)I", (void *)android_media_AudioSystem_setMasterVolume},
303 {"getMasterVolume", "()F", (void *)android_media_AudioSystem_getMasterVolume},
304 {"setMasterMute", "(Z)I", (void *)android_media_AudioSystem_setMasterMute},
305 {"getMasterMute", "()Z", (void *)android_media_AudioSystem_getMasterMute},
Glenn Kasten8b4b97a2011-02-04 13:54:26 -0800306 {"getDevicesForStream", "(I)I", (void *)android_media_AudioSystem_getDevicesForStream},
Glenn Kastenc6c43652012-09-24 17:32:30 -0700307 {"getPrimaryOutputSamplingRate", "()I", (void *)android_media_AudioSystem_getPrimaryOutputSamplingRate},
308 {"getPrimaryOutputFrameCount", "()I", (void *)android_media_AudioSystem_getPrimaryOutputFrameCount},
Oliver Woodman61dcdf32013-06-26 12:43:36 +0100309 {"getOutputLatency", "(I)I", (void *)android_media_AudioSystem_getOutputLatency},
Glenn Kastenfd116ad2013-07-12 17:10:39 -0700310 {"setLowRamDevice", "(Z)I", (void *)android_media_AudioSystem_setLowRamDevice},
Eric Laurentdfb881f2013-07-18 14:41:39 -0700311 {"checkAudioFlinger", "()I", (void *)android_media_AudioSystem_checkAudioFlinger},
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800312};
313
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800314int register_android_media_AudioSystem(JNIEnv *env)
315{
316 AudioSystem::setErrorCallback(android_media_AudioSystem_error_callback);
Glenn Kasten18db49a2012-03-12 16:29:55 -0700317
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800318 return AndroidRuntime::registerNativeMethods(env,
Glenn Kastened0079d2011-04-04 10:50:50 -0700319 kClassPathName, gMethods, NELEM(gMethods));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800320}