android_media_AudioSystem.cpp revision ed0079ddddd4070f3369b13b274222da2e69f4b9
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
37static const char* const kClassPathName = "android/media/AudioSystem";
38
39enum AudioError {
40    kAudioStatusOk = 0,
41    kAudioStatusError = 1,
42    kAudioStatusMediaServerDied = 100
43};
44
45static int check_AudioSystem_Command(status_t status)
46{
47    if (status == NO_ERROR) {
48        return kAudioStatusOk;
49    } else {
50        return kAudioStatusError;
51    }
52}
53
54static int
55android_media_AudioSystem_muteMicrophone(JNIEnv *env, jobject thiz, jboolean on)
56{
57    return check_AudioSystem_Command(AudioSystem::muteMicrophone(on));
58}
59
60static jboolean
61android_media_AudioSystem_isMicrophoneMuted(JNIEnv *env, jobject thiz)
62{
63    bool state = false;
64    AudioSystem::isMicrophoneMuted(&state);
65    return state;
66}
67
68static jboolean
69android_media_AudioSystem_isStreamActive(JNIEnv *env, jobject thiz, jint stream, jint inPastMs)
70{
71    bool state = false;
72    AudioSystem::isStreamActive(stream, &state, inPastMs);
73    return state;
74}
75
76static int
77android_media_AudioSystem_setParameters(JNIEnv *env, jobject thiz, jstring keyValuePairs)
78{
79    const jchar* c_keyValuePairs = env->GetStringCritical(keyValuePairs, 0);
80    String8 c_keyValuePairs8;
81    if (keyValuePairs) {
82        c_keyValuePairs8 = String8(c_keyValuePairs, env->GetStringLength(keyValuePairs));
83        env->ReleaseStringCritical(keyValuePairs, c_keyValuePairs);
84    }
85    int status = check_AudioSystem_Command(AudioSystem::setParameters(0, c_keyValuePairs8));
86    return status;
87}
88
89static jstring
90android_media_AudioSystem_getParameters(JNIEnv *env, jobject thiz, jstring keys)
91{
92    const jchar* c_keys = env->GetStringCritical(keys, 0);
93    String8 c_keys8;
94    if (keys) {
95        c_keys8 = String8(c_keys, env->GetStringLength(keys));
96        env->ReleaseStringCritical(keys, c_keys);
97    }
98    return env->NewStringUTF(AudioSystem::getParameters(0, c_keys8).string());
99}
100
101static void
102android_media_AudioSystem_error_callback(status_t err)
103{
104    JNIEnv *env = AndroidRuntime::getJNIEnv();
105    if (env == NULL) {
106        return;
107    }
108
109    jclass clazz = env->FindClass(kClassPathName);
110
111    int error;
112
113    switch (err) {
114    case DEAD_OBJECT:
115        error = kAudioStatusMediaServerDied;
116        break;
117    case NO_ERROR:
118        error = kAudioStatusOk;
119        break;
120    default:
121        error = kAudioStatusError;
122        break;
123    }
124
125    env->CallStaticVoidMethod(clazz, env->GetStaticMethodID(clazz, "errorCallbackFromNative","(I)V"), error);
126}
127
128static int
129android_media_AudioSystem_setDeviceConnectionState(JNIEnv *env, jobject thiz, jint device, jint state, jstring device_address)
130{
131    const char *c_address = env->GetStringUTFChars(device_address, NULL);
132    int status = check_AudioSystem_Command(AudioSystem::setDeviceConnectionState(static_cast <AudioSystem::audio_devices>(device),
133                                          static_cast <AudioSystem::device_connection_state>(state),
134                                          c_address));
135    env->ReleaseStringUTFChars(device_address, c_address);
136    return status;
137}
138
139static int
140android_media_AudioSystem_getDeviceConnectionState(JNIEnv *env, jobject thiz, jint device, jstring device_address)
141{
142    const char *c_address = env->GetStringUTFChars(device_address, NULL);
143    int state = static_cast <int>(AudioSystem::getDeviceConnectionState(static_cast <AudioSystem::audio_devices>(device),
144                                          c_address));
145    env->ReleaseStringUTFChars(device_address, c_address);
146    return state;
147}
148
149static int
150android_media_AudioSystem_setPhoneState(JNIEnv *env, jobject thiz, jint state)
151{
152    return check_AudioSystem_Command(AudioSystem::setPhoneState(state));
153}
154
155static int
156android_media_AudioSystem_setRingerMode(JNIEnv *env, jobject thiz, jint mode, jint mask)
157{
158    return check_AudioSystem_Command(AudioSystem::setRingerMode(mode, mask));
159}
160
161static int
162android_media_AudioSystem_setForceUse(JNIEnv *env, jobject thiz, jint usage, jint config)
163{
164    return check_AudioSystem_Command(AudioSystem::setForceUse(static_cast <AudioSystem::force_use>(usage),
165                                                           static_cast <AudioSystem::forced_config>(config)));
166}
167
168static int
169android_media_AudioSystem_getForceUse(JNIEnv *env, jobject thiz, jint usage)
170{
171    return static_cast <int>(AudioSystem::getForceUse(static_cast <AudioSystem::force_use>(usage)));
172}
173
174static int
175android_media_AudioSystem_initStreamVolume(JNIEnv *env, jobject thiz, jint stream, jint indexMin, jint indexMax)
176{
177    return check_AudioSystem_Command(AudioSystem::initStreamVolume(static_cast <AudioSystem::stream_type>(stream),
178                                                                   indexMin,
179                                                                   indexMax));
180}
181
182static int
183android_media_AudioSystem_setStreamVolumeIndex(JNIEnv *env, jobject thiz, jint stream, jint index)
184{
185    return check_AudioSystem_Command(AudioSystem::setStreamVolumeIndex(static_cast <AudioSystem::stream_type>(stream), index));
186}
187
188static int
189android_media_AudioSystem_getStreamVolumeIndex(JNIEnv *env, jobject thiz, jint stream)
190{
191    int index;
192    if (AudioSystem::getStreamVolumeIndex(static_cast <AudioSystem::stream_type>(stream), &index) != NO_ERROR) {
193        index = -1;
194    }
195    return index;
196}
197
198static jint
199android_media_AudioSystem_getDevicesForStream(JNIEnv *env, jobject thiz, jint stream)
200{
201    return (jint) AudioSystem::getDevicesForStream(static_cast <AudioSystem::stream_type>(stream));
202}
203
204// ----------------------------------------------------------------------------
205
206static JNINativeMethod gMethods[] = {
207    {"setParameters",        "(Ljava/lang/String;)I", (void *)android_media_AudioSystem_setParameters},
208    {"getParameters",        "(Ljava/lang/String;)Ljava/lang/String;", (void *)android_media_AudioSystem_getParameters},
209    {"muteMicrophone",      "(Z)I",     (void *)android_media_AudioSystem_muteMicrophone},
210    {"isMicrophoneMuted",   "()Z",      (void *)android_media_AudioSystem_isMicrophoneMuted},
211    {"isStreamActive",      "(II)Z",     (void *)android_media_AudioSystem_isStreamActive},
212    {"setDeviceConnectionState", "(IILjava/lang/String;)I", (void *)android_media_AudioSystem_setDeviceConnectionState},
213    {"getDeviceConnectionState", "(ILjava/lang/String;)I",  (void *)android_media_AudioSystem_getDeviceConnectionState},
214    {"setPhoneState",       "(I)I",     (void *)android_media_AudioSystem_setPhoneState},
215    {"setRingerMode",       "(II)I",    (void *)android_media_AudioSystem_setRingerMode},
216    {"setForceUse",         "(II)I",    (void *)android_media_AudioSystem_setForceUse},
217    {"getForceUse",         "(I)I",     (void *)android_media_AudioSystem_getForceUse},
218    {"initStreamVolume",    "(III)I",   (void *)android_media_AudioSystem_initStreamVolume},
219    {"setStreamVolumeIndex","(II)I",    (void *)android_media_AudioSystem_setStreamVolumeIndex},
220    {"getStreamVolumeIndex","(I)I",     (void *)android_media_AudioSystem_getStreamVolumeIndex},
221    {"getDevicesForStream", "(I)I",     (void *)android_media_AudioSystem_getDevicesForStream},
222};
223
224int register_android_media_AudioSystem(JNIEnv *env)
225{
226    AudioSystem::setErrorCallback(android_media_AudioSystem_error_callback);
227
228    return AndroidRuntime::registerNativeMethods(env,
229                kClassPathName, gMethods, NELEM(gMethods));
230}
231