The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2006-2007 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | #define LOG_TAG "AudioSystem" |
| 18 | //#define LOG_NDEBUG 0 |
| 19 | |
| 20 | #include <utils/Log.h> |
Mathias Agopian | 7562408 | 2009-05-19 19:08:10 -0700 | [diff] [blame] | 21 | #include <binder/IServiceManager.h> |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 22 | #include <media/AudioSystem.h> |
Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 23 | #include <media/IAudioPolicyService.h> |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 24 | #include <math.h> |
| 25 | |
Dima Zavin | 6476024 | 2011-05-11 14:15:23 -0700 | [diff] [blame] | 26 | #include <system/audio.h> |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 27 | |
Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 28 | // ---------------------------------------------------------------------------- |
Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 29 | |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 30 | namespace android { |
| 31 | |
| 32 | // client singleton for AudioFlinger binder interface |
| 33 | Mutex AudioSystem::gLock; |
| 34 | sp<IAudioFlinger> AudioSystem::gAudioFlinger; |
| 35 | sp<AudioSystem::AudioFlingerClient> AudioSystem::gAudioFlingerClient; |
| 36 | audio_error_callback AudioSystem::gAudioErrorCallback = NULL; |
| 37 | // Cached values |
Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 38 | DefaultKeyedVector<int, audio_io_handle_t> AudioSystem::gStreamOutputMap(0); |
| 39 | DefaultKeyedVector<audio_io_handle_t, AudioSystem::OutputDescriptor *> AudioSystem::gOutputs(0); |
| 40 | |
Glenn Kasten | f8c1a6f | 2012-01-10 09:01:19 -0800 | [diff] [blame] | 41 | // Cached values for recording queries, all protected by gLock |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 42 | uint32_t AudioSystem::gPrevInSamplingRate = 16000; |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 43 | int AudioSystem::gPrevInFormat = AUDIO_FORMAT_PCM_16_BIT; |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 44 | int AudioSystem::gPrevInChannelCount = 1; |
| 45 | size_t AudioSystem::gInBuffSize = 0; |
| 46 | |
| 47 | |
| 48 | // establish binder interface to AudioFlinger service |
| 49 | const sp<IAudioFlinger>& AudioSystem::get_audio_flinger() |
| 50 | { |
| 51 | Mutex::Autolock _l(gLock); |
| 52 | if (gAudioFlinger.get() == 0) { |
| 53 | sp<IServiceManager> sm = defaultServiceManager(); |
| 54 | sp<IBinder> binder; |
| 55 | do { |
| 56 | binder = sm->getService(String16("media.audio_flinger")); |
| 57 | if (binder != 0) |
| 58 | break; |
Steve Block | 5ff1dd5 | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 59 | ALOGW("AudioFlinger not published, waiting..."); |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 60 | usleep(500000); // 0.5 s |
| 61 | } while(true); |
| 62 | if (gAudioFlingerClient == NULL) { |
| 63 | gAudioFlingerClient = new AudioFlingerClient(); |
| 64 | } else { |
| 65 | if (gAudioErrorCallback) { |
| 66 | gAudioErrorCallback(NO_ERROR); |
| 67 | } |
| 68 | } |
| 69 | binder->linkToDeath(gAudioFlingerClient); |
| 70 | gAudioFlinger = interface_cast<IAudioFlinger>(binder); |
| 71 | gAudioFlinger->registerClient(gAudioFlingerClient); |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 72 | } |
Steve Block | 29357bc | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 73 | ALOGE_IF(gAudioFlinger==0, "no AudioFlinger!?"); |
Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 74 | |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 75 | return gAudioFlinger; |
| 76 | } |
| 77 | |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 78 | status_t AudioSystem::muteMicrophone(bool state) { |
| 79 | const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger(); |
| 80 | if (af == 0) return PERMISSION_DENIED; |
| 81 | return af->setMicMute(state); |
| 82 | } |
| 83 | |
| 84 | status_t AudioSystem::isMicrophoneMuted(bool* state) { |
| 85 | const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger(); |
| 86 | if (af == 0) return PERMISSION_DENIED; |
| 87 | *state = af->getMicMute(); |
| 88 | return NO_ERROR; |
| 89 | } |
| 90 | |
| 91 | status_t AudioSystem::setMasterVolume(float value) |
| 92 | { |
| 93 | const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger(); |
| 94 | if (af == 0) return PERMISSION_DENIED; |
| 95 | af->setMasterVolume(value); |
| 96 | return NO_ERROR; |
| 97 | } |
| 98 | |
| 99 | status_t AudioSystem::setMasterMute(bool mute) |
| 100 | { |
| 101 | const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger(); |
| 102 | if (af == 0) return PERMISSION_DENIED; |
| 103 | af->setMasterMute(mute); |
| 104 | return NO_ERROR; |
| 105 | } |
| 106 | |
| 107 | status_t AudioSystem::getMasterVolume(float* volume) |
| 108 | { |
| 109 | const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger(); |
| 110 | if (af == 0) return PERMISSION_DENIED; |
| 111 | *volume = af->masterVolume(); |
| 112 | return NO_ERROR; |
| 113 | } |
| 114 | |
| 115 | status_t AudioSystem::getMasterMute(bool* mute) |
| 116 | { |
| 117 | const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger(); |
| 118 | if (af == 0) return PERMISSION_DENIED; |
| 119 | *mute = af->masterMute(); |
| 120 | return NO_ERROR; |
| 121 | } |
| 122 | |
Glenn Kasten | fff6d71 | 2012-01-12 16:38:12 -0800 | [diff] [blame] | 123 | status_t AudioSystem::setStreamVolume(audio_stream_type_t stream, float value, int output) |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 124 | { |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 125 | if (uint32_t(stream) >= AUDIO_STREAM_CNT) return BAD_VALUE; |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 126 | const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger(); |
| 127 | if (af == 0) return PERMISSION_DENIED; |
Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 128 | af->setStreamVolume(stream, value, output); |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 129 | return NO_ERROR; |
| 130 | } |
| 131 | |
Glenn Kasten | fff6d71 | 2012-01-12 16:38:12 -0800 | [diff] [blame] | 132 | status_t AudioSystem::setStreamMute(audio_stream_type_t stream, bool mute) |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 133 | { |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 134 | if (uint32_t(stream) >= AUDIO_STREAM_CNT) return BAD_VALUE; |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 135 | const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger(); |
| 136 | if (af == 0) return PERMISSION_DENIED; |
| 137 | af->setStreamMute(stream, mute); |
| 138 | return NO_ERROR; |
| 139 | } |
| 140 | |
Glenn Kasten | fff6d71 | 2012-01-12 16:38:12 -0800 | [diff] [blame] | 141 | status_t AudioSystem::getStreamVolume(audio_stream_type_t stream, float* volume, int output) |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 142 | { |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 143 | if (uint32_t(stream) >= AUDIO_STREAM_CNT) return BAD_VALUE; |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 144 | const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger(); |
| 145 | if (af == 0) return PERMISSION_DENIED; |
Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 146 | *volume = af->streamVolume(stream, output); |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 147 | return NO_ERROR; |
| 148 | } |
| 149 | |
Glenn Kasten | fff6d71 | 2012-01-12 16:38:12 -0800 | [diff] [blame] | 150 | status_t AudioSystem::getStreamMute(audio_stream_type_t stream, bool* mute) |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 151 | { |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 152 | if (uint32_t(stream) >= AUDIO_STREAM_CNT) return BAD_VALUE; |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 153 | const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger(); |
| 154 | if (af == 0) return PERMISSION_DENIED; |
| 155 | *mute = af->streamMute(stream); |
| 156 | return NO_ERROR; |
| 157 | } |
| 158 | |
| 159 | status_t AudioSystem::setMode(int mode) |
| 160 | { |
Glenn Kasten | 930f4ca | 2012-01-06 16:47:31 -0800 | [diff] [blame] | 161 | if (uint32_t(mode) >= AUDIO_MODE_CNT) return BAD_VALUE; |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 162 | const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger(); |
| 163 | if (af == 0) return PERMISSION_DENIED; |
| 164 | return af->setMode(mode); |
| 165 | } |
| 166 | |
Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 167 | status_t AudioSystem::setParameters(audio_io_handle_t ioHandle, const String8& keyValuePairs) { |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 168 | const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger(); |
| 169 | if (af == 0) return PERMISSION_DENIED; |
Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 170 | return af->setParameters(ioHandle, keyValuePairs); |
| 171 | } |
| 172 | |
| 173 | String8 AudioSystem::getParameters(audio_io_handle_t ioHandle, const String8& keys) { |
| 174 | const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger(); |
| 175 | String8 result = String8(""); |
| 176 | if (af == 0) return result; |
| 177 | |
| 178 | result = af->getParameters(ioHandle, keys); |
| 179 | return result; |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 180 | } |
| 181 | |
| 182 | // convert volume steps to natural log scale |
| 183 | |
| 184 | // change this value to change volume scaling |
| 185 | static const float dBPerStep = 0.5f; |
| 186 | // shouldn't need to touch these |
| 187 | static const float dBConvert = -dBPerStep * 2.302585093f / 20.0f; |
| 188 | static const float dBConvertInverse = 1.0f / dBConvert; |
| 189 | |
| 190 | float AudioSystem::linearToLog(int volume) |
| 191 | { |
| 192 | // float v = volume ? exp(float(100 - volume) * dBConvert) : 0; |
Steve Block | b8a8052 | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 193 | // ALOGD("linearToLog(%d)=%f", volume, v); |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 194 | // return v; |
| 195 | return volume ? exp(float(100 - volume) * dBConvert) : 0; |
| 196 | } |
| 197 | |
| 198 | int AudioSystem::logToLinear(float volume) |
| 199 | { |
| 200 | // int v = volume ? 100 - int(dBConvertInverse * log(volume) + 0.5) : 0; |
Steve Block | b8a8052 | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 201 | // ALOGD("logTolinear(%d)=%f", v, volume); |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 202 | // return v; |
| 203 | return volume ? 100 - int(dBConvertInverse * log(volume) + 0.5) : 0; |
| 204 | } |
| 205 | |
Glenn Kasten | fff6d71 | 2012-01-12 16:38:12 -0800 | [diff] [blame] | 206 | status_t AudioSystem::getOutputSamplingRate(int* samplingRate, audio_stream_type_t streamType) |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 207 | { |
Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 208 | OutputDescriptor *outputDesc; |
| 209 | audio_io_handle_t output; |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 210 | |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 211 | if (streamType == AUDIO_STREAM_DEFAULT) { |
| 212 | streamType = AUDIO_STREAM_MUSIC; |
Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 213 | } |
| 214 | |
Glenn Kasten | fff6d71 | 2012-01-12 16:38:12 -0800 | [diff] [blame] | 215 | output = getOutput(streamType); |
Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 216 | if (output == 0) { |
| 217 | return PERMISSION_DENIED; |
| 218 | } |
| 219 | |
| 220 | gLock.lock(); |
| 221 | outputDesc = AudioSystem::gOutputs.valueFor(output); |
| 222 | if (outputDesc == 0) { |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 223 | ALOGV("getOutputSamplingRate() no output descriptor for output %d in gOutputs", output); |
Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 224 | gLock.unlock(); |
| 225 | const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger(); |
| 226 | if (af == 0) return PERMISSION_DENIED; |
| 227 | *samplingRate = af->sampleRate(output); |
| 228 | } else { |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 229 | ALOGV("getOutputSamplingRate() reading from output desc"); |
Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 230 | *samplingRate = outputDesc->samplingRate; |
| 231 | gLock.unlock(); |
| 232 | } |
| 233 | |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 234 | ALOGV("getOutputSamplingRate() streamType %d, output %d, sampling rate %d", streamType, output, *samplingRate); |
Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 235 | |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 236 | return NO_ERROR; |
| 237 | } |
| 238 | |
Glenn Kasten | fff6d71 | 2012-01-12 16:38:12 -0800 | [diff] [blame] | 239 | status_t AudioSystem::getOutputFrameCount(int* frameCount, audio_stream_type_t streamType) |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 240 | { |
Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 241 | OutputDescriptor *outputDesc; |
| 242 | audio_io_handle_t output; |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 243 | |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 244 | if (streamType == AUDIO_STREAM_DEFAULT) { |
| 245 | streamType = AUDIO_STREAM_MUSIC; |
Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 246 | } |
Eric Laurent | 48f7f5e | 2009-04-02 09:32:43 -0700 | [diff] [blame] | 247 | |
Glenn Kasten | fff6d71 | 2012-01-12 16:38:12 -0800 | [diff] [blame] | 248 | output = getOutput(streamType); |
Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 249 | if (output == 0) { |
| 250 | return PERMISSION_DENIED; |
| 251 | } |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 252 | |
Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 253 | gLock.lock(); |
| 254 | outputDesc = AudioSystem::gOutputs.valueFor(output); |
| 255 | if (outputDesc == 0) { |
| 256 | gLock.unlock(); |
| 257 | const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger(); |
| 258 | if (af == 0) return PERMISSION_DENIED; |
| 259 | *frameCount = af->frameCount(output); |
| 260 | } else { |
| 261 | *frameCount = outputDesc->frameCount; |
| 262 | gLock.unlock(); |
| 263 | } |
| 264 | |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 265 | ALOGV("getOutputFrameCount() streamType %d, output %d, frameCount %d", streamType, output, *frameCount); |
Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 266 | |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 267 | return NO_ERROR; |
| 268 | } |
| 269 | |
Glenn Kasten | fff6d71 | 2012-01-12 16:38:12 -0800 | [diff] [blame] | 270 | status_t AudioSystem::getOutputLatency(uint32_t* latency, audio_stream_type_t streamType) |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 271 | { |
Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 272 | OutputDescriptor *outputDesc; |
| 273 | audio_io_handle_t output; |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 274 | |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 275 | if (streamType == AUDIO_STREAM_DEFAULT) { |
| 276 | streamType = AUDIO_STREAM_MUSIC; |
Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 277 | } |
Eric Laurent | 48f7f5e | 2009-04-02 09:32:43 -0700 | [diff] [blame] | 278 | |
Glenn Kasten | fff6d71 | 2012-01-12 16:38:12 -0800 | [diff] [blame] | 279 | output = getOutput(streamType); |
Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 280 | if (output == 0) { |
| 281 | return PERMISSION_DENIED; |
| 282 | } |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 283 | |
Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 284 | gLock.lock(); |
| 285 | outputDesc = AudioSystem::gOutputs.valueFor(output); |
| 286 | if (outputDesc == 0) { |
| 287 | gLock.unlock(); |
| 288 | const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger(); |
| 289 | if (af == 0) return PERMISSION_DENIED; |
| 290 | *latency = af->latency(output); |
| 291 | } else { |
| 292 | *latency = outputDesc->latency; |
| 293 | gLock.unlock(); |
| 294 | } |
| 295 | |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 296 | ALOGV("getOutputLatency() streamType %d, output %d, latency %d", streamType, output, *latency); |
Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 297 | |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 298 | return NO_ERROR; |
| 299 | } |
| 300 | |
Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 301 | status_t AudioSystem::getInputBufferSize(uint32_t sampleRate, int format, int channelCount, |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 302 | size_t* buffSize) |
| 303 | { |
Glenn Kasten | f8c1a6f | 2012-01-10 09:01:19 -0800 | [diff] [blame] | 304 | gLock.lock(); |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 305 | // Do we have a stale gInBufferSize or are we requesting the input buffer size for new values |
Glenn Kasten | f8c1a6f | 2012-01-10 09:01:19 -0800 | [diff] [blame] | 306 | size_t inBuffSize = gInBuffSize; |
| 307 | if ((inBuffSize == 0) || (sampleRate != gPrevInSamplingRate) || (format != gPrevInFormat) |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 308 | || (channelCount != gPrevInChannelCount)) { |
Glenn Kasten | f8c1a6f | 2012-01-10 09:01:19 -0800 | [diff] [blame] | 309 | gLock.unlock(); |
| 310 | const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger(); |
| 311 | if (af == 0) { |
| 312 | return PERMISSION_DENIED; |
| 313 | } |
| 314 | inBuffSize = af->getInputBufferSize(sampleRate, format, channelCount); |
| 315 | gLock.lock(); |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 316 | // save the request params |
| 317 | gPrevInSamplingRate = sampleRate; |
Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 318 | gPrevInFormat = format; |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 319 | gPrevInChannelCount = channelCount; |
| 320 | |
Glenn Kasten | f8c1a6f | 2012-01-10 09:01:19 -0800 | [diff] [blame] | 321 | gInBuffSize = inBuffSize; |
Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 322 | } |
Glenn Kasten | f8c1a6f | 2012-01-10 09:01:19 -0800 | [diff] [blame] | 323 | gLock.unlock(); |
| 324 | *buffSize = inBuffSize; |
Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 325 | |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 326 | return NO_ERROR; |
| 327 | } |
| 328 | |
Eric Laurent | f0ee6f4 | 2009-10-21 08:14:22 -0700 | [diff] [blame] | 329 | status_t AudioSystem::setVoiceVolume(float value) |
| 330 | { |
| 331 | const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger(); |
| 332 | if (af == 0) return PERMISSION_DENIED; |
| 333 | return af->setVoiceVolume(value); |
| 334 | } |
| 335 | |
Glenn Kasten | fff6d71 | 2012-01-12 16:38:12 -0800 | [diff] [blame] | 336 | status_t AudioSystem::getRenderPosition(uint32_t *halFrames, uint32_t *dspFrames, audio_stream_type_t stream) |
Eric Laurent | 342e9cf | 2010-01-19 17:37:09 -0800 | [diff] [blame] | 337 | { |
| 338 | const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger(); |
| 339 | if (af == 0) return PERMISSION_DENIED; |
| 340 | |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 341 | if (stream == AUDIO_STREAM_DEFAULT) { |
| 342 | stream = AUDIO_STREAM_MUSIC; |
Eric Laurent | 342e9cf | 2010-01-19 17:37:09 -0800 | [diff] [blame] | 343 | } |
| 344 | |
Glenn Kasten | fff6d71 | 2012-01-12 16:38:12 -0800 | [diff] [blame] | 345 | return af->getRenderPosition(halFrames, dspFrames, getOutput(stream)); |
Eric Laurent | 342e9cf | 2010-01-19 17:37:09 -0800 | [diff] [blame] | 346 | } |
| 347 | |
Eric Laurent | 05bca2f | 2010-02-26 02:47:27 -0800 | [diff] [blame] | 348 | unsigned int AudioSystem::getInputFramesLost(audio_io_handle_t ioHandle) { |
| 349 | const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger(); |
| 350 | unsigned int result = 0; |
| 351 | if (af == 0) return result; |
Eric Laurent | be55a2d | 2010-03-11 14:47:00 -0800 | [diff] [blame] | 352 | if (ioHandle == 0) return result; |
Eric Laurent | 05bca2f | 2010-02-26 02:47:27 -0800 | [diff] [blame] | 353 | |
| 354 | result = af->getInputFramesLost(ioHandle); |
| 355 | return result; |
| 356 | } |
| 357 | |
Eric Laurent | be916aa | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 358 | int AudioSystem::newAudioSessionId() { |
| 359 | const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger(); |
| 360 | if (af == 0) return 0; |
| 361 | return af->newAudioSessionId(); |
| 362 | } |
| 363 | |
Marco Nelissen | 3a34bef | 2011-08-02 13:33:41 -0700 | [diff] [blame] | 364 | void AudioSystem::acquireAudioSessionId(int audioSession) { |
| 365 | const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger(); |
| 366 | if (af != 0) { |
| 367 | af->acquireAudioSessionId(audioSession); |
| 368 | } |
| 369 | } |
| 370 | |
| 371 | void AudioSystem::releaseAudioSessionId(int audioSession) { |
| 372 | const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger(); |
| 373 | if (af != 0) { |
| 374 | af->releaseAudioSessionId(audioSession); |
| 375 | } |
| 376 | } |
| 377 | |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 378 | // --------------------------------------------------------------------------- |
| 379 | |
Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 380 | void AudioSystem::AudioFlingerClient::binderDied(const wp<IBinder>& who) { |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 381 | Mutex::Autolock _l(AudioSystem::gLock); |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 382 | |
Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 383 | AudioSystem::gAudioFlinger.clear(); |
Eric Laurent | 0ef583f | 2010-01-25 10:27:15 -0800 | [diff] [blame] | 384 | // clear output handles and stream to output map caches |
| 385 | AudioSystem::gStreamOutputMap.clear(); |
| 386 | AudioSystem::gOutputs.clear(); |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 387 | |
| 388 | if (gAudioErrorCallback) { |
| 389 | gAudioErrorCallback(DEAD_OBJECT); |
| 390 | } |
Steve Block | 5ff1dd5 | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 391 | ALOGW("AudioFlinger server died!"); |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 392 | } |
| 393 | |
Eric Laurent | fa2877b | 2009-07-28 08:44:33 -0700 | [diff] [blame] | 394 | void AudioSystem::AudioFlingerClient::ioConfigChanged(int event, int ioHandle, void *param2) { |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 395 | ALOGV("ioConfigChanged() event %d", event); |
Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 396 | OutputDescriptor *desc; |
| 397 | uint32_t stream; |
| 398 | |
Eric Laurent | fa2877b | 2009-07-28 08:44:33 -0700 | [diff] [blame] | 399 | if (ioHandle == 0) return; |
Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 400 | |
| 401 | Mutex::Autolock _l(AudioSystem::gLock); |
| 402 | |
| 403 | switch (event) { |
| 404 | case STREAM_CONFIG_CHANGED: |
| 405 | if (param2 == 0) break; |
| 406 | stream = *(uint32_t *)param2; |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 407 | ALOGV("ioConfigChanged() STREAM_CONFIG_CHANGED stream %d, output %d", stream, ioHandle); |
Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 408 | if (gStreamOutputMap.indexOfKey(stream) >= 0) { |
| 409 | gStreamOutputMap.replaceValueFor(stream, ioHandle); |
| 410 | } |
| 411 | break; |
| 412 | case OUTPUT_OPENED: { |
| 413 | if (gOutputs.indexOfKey(ioHandle) >= 0) { |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 414 | ALOGV("ioConfigChanged() opening already existing output! %d", ioHandle); |
Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 415 | break; |
| 416 | } |
| 417 | if (param2 == 0) break; |
| 418 | desc = (OutputDescriptor *)param2; |
| 419 | |
| 420 | OutputDescriptor *outputDesc = new OutputDescriptor(*desc); |
| 421 | gOutputs.add(ioHandle, outputDesc); |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 422 | ALOGV("ioConfigChanged() new output samplingRate %d, format %d channels %d frameCount %d latency %d", |
Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 423 | outputDesc->samplingRate, outputDesc->format, outputDesc->channels, outputDesc->frameCount, outputDesc->latency); |
| 424 | } break; |
| 425 | case OUTPUT_CLOSED: { |
| 426 | if (gOutputs.indexOfKey(ioHandle) < 0) { |
Steve Block | 5ff1dd5 | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 427 | ALOGW("ioConfigChanged() closing unknow output! %d", ioHandle); |
Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 428 | break; |
| 429 | } |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 430 | ALOGV("ioConfigChanged() output %d closed", ioHandle); |
Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 431 | |
| 432 | gOutputs.removeItem(ioHandle); |
| 433 | for (int i = gStreamOutputMap.size() - 1; i >= 0 ; i--) { |
| 434 | if (gStreamOutputMap.valueAt(i) == ioHandle) { |
| 435 | gStreamOutputMap.removeItemsAt(i); |
| 436 | } |
| 437 | } |
| 438 | } break; |
| 439 | |
| 440 | case OUTPUT_CONFIG_CHANGED: { |
| 441 | int index = gOutputs.indexOfKey(ioHandle); |
| 442 | if (index < 0) { |
Steve Block | 5ff1dd5 | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 443 | ALOGW("ioConfigChanged() modifying unknow output! %d", ioHandle); |
Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 444 | break; |
| 445 | } |
| 446 | if (param2 == 0) break; |
| 447 | desc = (OutputDescriptor *)param2; |
| 448 | |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 449 | ALOGV("ioConfigChanged() new config for output %d samplingRate %d, format %d channels %d frameCount %d latency %d", |
Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 450 | ioHandle, desc->samplingRate, desc->format, |
| 451 | desc->channels, desc->frameCount, desc->latency); |
| 452 | OutputDescriptor *outputDesc = gOutputs.valueAt(index); |
| 453 | delete outputDesc; |
| 454 | outputDesc = new OutputDescriptor(*desc); |
| 455 | gOutputs.replaceValueFor(ioHandle, outputDesc); |
| 456 | } break; |
| 457 | case INPUT_OPENED: |
| 458 | case INPUT_CLOSED: |
| 459 | case INPUT_CONFIG_CHANGED: |
| 460 | break; |
| 461 | |
| 462 | } |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 463 | } |
| 464 | |
| 465 | void AudioSystem::setErrorCallback(audio_error_callback cb) { |
Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 466 | Mutex::Autolock _l(gLock); |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 467 | gAudioErrorCallback = cb; |
| 468 | } |
| 469 | |
Glenn Kasten | fff6d71 | 2012-01-12 16:38:12 -0800 | [diff] [blame] | 470 | bool AudioSystem::routedToA2dpOutput(audio_stream_type_t streamType) { |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 471 | switch(streamType) { |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 472 | case AUDIO_STREAM_MUSIC: |
| 473 | case AUDIO_STREAM_VOICE_CALL: |
| 474 | case AUDIO_STREAM_BLUETOOTH_SCO: |
| 475 | case AUDIO_STREAM_SYSTEM: |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 476 | return true; |
| 477 | default: |
| 478 | return false; |
| 479 | } |
| 480 | } |
| 481 | |
| 482 | |
Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 483 | // client singleton for AudioPolicyService binder interface |
| 484 | sp<IAudioPolicyService> AudioSystem::gAudioPolicyService; |
| 485 | sp<AudioSystem::AudioPolicyServiceClient> AudioSystem::gAudioPolicyServiceClient; |
| 486 | |
| 487 | |
| 488 | // establish binder interface to AudioFlinger service |
| 489 | const sp<IAudioPolicyService>& AudioSystem::get_audio_policy_service() |
| 490 | { |
| 491 | gLock.lock(); |
| 492 | if (gAudioPolicyService.get() == 0) { |
| 493 | sp<IServiceManager> sm = defaultServiceManager(); |
| 494 | sp<IBinder> binder; |
| 495 | do { |
| 496 | binder = sm->getService(String16("media.audio_policy")); |
| 497 | if (binder != 0) |
| 498 | break; |
Steve Block | 5ff1dd5 | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 499 | ALOGW("AudioPolicyService not published, waiting..."); |
Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 500 | usleep(500000); // 0.5 s |
| 501 | } while(true); |
| 502 | if (gAudioPolicyServiceClient == NULL) { |
| 503 | gAudioPolicyServiceClient = new AudioPolicyServiceClient(); |
| 504 | } |
| 505 | binder->linkToDeath(gAudioPolicyServiceClient); |
| 506 | gAudioPolicyService = interface_cast<IAudioPolicyService>(binder); |
| 507 | gLock.unlock(); |
| 508 | } else { |
| 509 | gLock.unlock(); |
| 510 | } |
| 511 | return gAudioPolicyService; |
| 512 | } |
| 513 | |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 514 | status_t AudioSystem::setDeviceConnectionState(audio_devices_t device, |
| 515 | audio_policy_dev_state_t state, |
| 516 | const char *device_address) |
Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 517 | { |
| 518 | const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service(); |
Eric Laurent | 71b63e3 | 2011-09-02 14:20:56 -0700 | [diff] [blame] | 519 | const char *address = ""; |
| 520 | |
Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 521 | if (aps == 0) return PERMISSION_DENIED; |
| 522 | |
Eric Laurent | 71b63e3 | 2011-09-02 14:20:56 -0700 | [diff] [blame] | 523 | if (device_address != NULL) { |
| 524 | address = device_address; |
| 525 | } |
| 526 | |
| 527 | return aps->setDeviceConnectionState(device, state, address); |
Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 528 | } |
| 529 | |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 530 | audio_policy_dev_state_t AudioSystem::getDeviceConnectionState(audio_devices_t device, |
Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 531 | const char *device_address) |
| 532 | { |
| 533 | const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service(); |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 534 | if (aps == 0) return AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE; |
Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 535 | |
| 536 | return aps->getDeviceConnectionState(device, device_address); |
| 537 | } |
| 538 | |
| 539 | status_t AudioSystem::setPhoneState(int state) |
| 540 | { |
| 541 | const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service(); |
| 542 | if (aps == 0) return PERMISSION_DENIED; |
| 543 | |
| 544 | return aps->setPhoneState(state); |
| 545 | } |
| 546 | |
| 547 | status_t AudioSystem::setRingerMode(uint32_t mode, uint32_t mask) |
| 548 | { |
| 549 | const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service(); |
| 550 | if (aps == 0) return PERMISSION_DENIED; |
| 551 | return aps->setRingerMode(mode, mask); |
| 552 | } |
| 553 | |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 554 | status_t AudioSystem::setForceUse(audio_policy_force_use_t usage, audio_policy_forced_cfg_t config) |
Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 555 | { |
| 556 | const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service(); |
| 557 | if (aps == 0) return PERMISSION_DENIED; |
| 558 | return aps->setForceUse(usage, config); |
| 559 | } |
| 560 | |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 561 | audio_policy_forced_cfg_t AudioSystem::getForceUse(audio_policy_force_use_t usage) |
Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 562 | { |
| 563 | const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service(); |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 564 | if (aps == 0) return AUDIO_POLICY_FORCE_NONE; |
Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 565 | return aps->getForceUse(usage); |
| 566 | } |
| 567 | |
| 568 | |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 569 | audio_io_handle_t AudioSystem::getOutput(audio_stream_type_t stream, |
Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 570 | uint32_t samplingRate, |
| 571 | uint32_t format, |
| 572 | uint32_t channels, |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 573 | audio_policy_output_flags_t flags) |
Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 574 | { |
Eric Laurent | fa2877b | 2009-07-28 08:44:33 -0700 | [diff] [blame] | 575 | audio_io_handle_t output = 0; |
Eric Laurent | be55a2d | 2010-03-11 14:47:00 -0800 | [diff] [blame] | 576 | // Do not use stream to output map cache if the direct output |
| 577 | // flag is set or if we are likely to use a direct output |
| 578 | // (e.g voice call stream @ 8kHz could use BT SCO device and be routed to |
| 579 | // a direct output on some platforms). |
| 580 | // TODO: the output cache and stream to output mapping implementation needs to |
| 581 | // be reworked for proper operation with direct outputs. This code is too specific |
| 582 | // to the first use case we want to cover (Voice Recognition and Voice Dialer over |
| 583 | // Bluetooth SCO |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 584 | if ((flags & AUDIO_POLICY_OUTPUT_FLAG_DIRECT) == 0 && |
| 585 | ((stream != AUDIO_STREAM_VOICE_CALL && stream != AUDIO_STREAM_BLUETOOTH_SCO) || |
| 586 | channels != AUDIO_CHANNEL_OUT_MONO || |
Eric Laurent | be55a2d | 2010-03-11 14:47:00 -0800 | [diff] [blame] | 587 | (samplingRate != 8000 && samplingRate != 16000))) { |
Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 588 | Mutex::Autolock _l(gLock); |
| 589 | output = AudioSystem::gStreamOutputMap.valueFor(stream); |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 590 | ALOGV_IF((output != 0), "getOutput() read %d from cache for stream %d", output, stream); |
Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 591 | } |
Eric Laurent | fa2877b | 2009-07-28 08:44:33 -0700 | [diff] [blame] | 592 | if (output == 0) { |
Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 593 | const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service(); |
Eric Laurent | fa2877b | 2009-07-28 08:44:33 -0700 | [diff] [blame] | 594 | if (aps == 0) return 0; |
Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 595 | output = aps->getOutput(stream, samplingRate, format, channels, flags); |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 596 | if ((flags & AUDIO_POLICY_OUTPUT_FLAG_DIRECT) == 0) { |
Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 597 | Mutex::Autolock _l(gLock); |
| 598 | AudioSystem::gStreamOutputMap.add(stream, output); |
| 599 | } |
| 600 | } |
| 601 | return output; |
| 602 | } |
| 603 | |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 604 | status_t AudioSystem::startOutput(audio_io_handle_t output, |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 605 | audio_stream_type_t stream, |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 606 | int session) |
Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 607 | { |
| 608 | const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service(); |
| 609 | if (aps == 0) return PERMISSION_DENIED; |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 610 | return aps->startOutput(output, stream, session); |
Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 611 | } |
| 612 | |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 613 | status_t AudioSystem::stopOutput(audio_io_handle_t output, |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 614 | audio_stream_type_t stream, |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 615 | int session) |
Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 616 | { |
| 617 | const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service(); |
| 618 | if (aps == 0) return PERMISSION_DENIED; |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 619 | return aps->stopOutput(output, stream, session); |
Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 620 | } |
| 621 | |
| 622 | void AudioSystem::releaseOutput(audio_io_handle_t output) |
| 623 | { |
| 624 | const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service(); |
| 625 | if (aps == 0) return; |
| 626 | aps->releaseOutput(output); |
| 627 | } |
| 628 | |
| 629 | audio_io_handle_t AudioSystem::getInput(int inputSource, |
| 630 | uint32_t samplingRate, |
| 631 | uint32_t format, |
| 632 | uint32_t channels, |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 633 | audio_in_acoustics_t acoustics, |
| 634 | int sessionId) |
Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 635 | { |
| 636 | const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service(); |
Eric Laurent | fa2877b | 2009-07-28 08:44:33 -0700 | [diff] [blame] | 637 | if (aps == 0) return 0; |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 638 | return aps->getInput(inputSource, samplingRate, format, channels, acoustics, sessionId); |
Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 639 | } |
| 640 | |
| 641 | status_t AudioSystem::startInput(audio_io_handle_t input) |
| 642 | { |
| 643 | const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service(); |
| 644 | if (aps == 0) return PERMISSION_DENIED; |
| 645 | return aps->startInput(input); |
| 646 | } |
| 647 | |
| 648 | status_t AudioSystem::stopInput(audio_io_handle_t input) |
| 649 | { |
| 650 | const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service(); |
| 651 | if (aps == 0) return PERMISSION_DENIED; |
| 652 | return aps->stopInput(input); |
| 653 | } |
| 654 | |
| 655 | void AudioSystem::releaseInput(audio_io_handle_t input) |
| 656 | { |
| 657 | const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service(); |
| 658 | if (aps == 0) return; |
| 659 | aps->releaseInput(input); |
| 660 | } |
| 661 | |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 662 | status_t AudioSystem::initStreamVolume(audio_stream_type_t stream, |
Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 663 | int indexMin, |
| 664 | int indexMax) |
| 665 | { |
| 666 | const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service(); |
| 667 | if (aps == 0) return PERMISSION_DENIED; |
| 668 | return aps->initStreamVolume(stream, indexMin, indexMax); |
| 669 | } |
| 670 | |
Eric Laurent | 83844cc | 2011-11-18 16:43:31 -0800 | [diff] [blame^] | 671 | status_t AudioSystem::setStreamVolumeIndex(audio_stream_type_t stream, |
| 672 | int index, |
| 673 | audio_devices_t device) |
Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 674 | { |
| 675 | const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service(); |
| 676 | if (aps == 0) return PERMISSION_DENIED; |
Eric Laurent | 83844cc | 2011-11-18 16:43:31 -0800 | [diff] [blame^] | 677 | return aps->setStreamVolumeIndex(stream, index, device); |
Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 678 | } |
| 679 | |
Eric Laurent | 83844cc | 2011-11-18 16:43:31 -0800 | [diff] [blame^] | 680 | status_t AudioSystem::getStreamVolumeIndex(audio_stream_type_t stream, |
| 681 | int *index, |
| 682 | audio_devices_t device) |
Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 683 | { |
| 684 | const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service(); |
| 685 | if (aps == 0) return PERMISSION_DENIED; |
Eric Laurent | 83844cc | 2011-11-18 16:43:31 -0800 | [diff] [blame^] | 686 | return aps->getStreamVolumeIndex(stream, index, device); |
Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 687 | } |
| 688 | |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 689 | uint32_t AudioSystem::getStrategyForStream(audio_stream_type_t stream) |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 690 | { |
| 691 | const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service(); |
| 692 | if (aps == 0) return 0; |
| 693 | return aps->getStrategyForStream(stream); |
| 694 | } |
| 695 | |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 696 | uint32_t AudioSystem::getDevicesForStream(audio_stream_type_t stream) |
Glenn Kasten | 6b2718c | 2011-02-04 13:54:26 -0800 | [diff] [blame] | 697 | { |
| 698 | const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service(); |
| 699 | if (aps == 0) return 0; |
| 700 | return aps->getDevicesForStream(stream); |
| 701 | } |
| 702 | |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 703 | audio_io_handle_t AudioSystem::getOutputForEffect(effect_descriptor_t *desc) |
| 704 | { |
| 705 | const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service(); |
| 706 | if (aps == 0) return PERMISSION_DENIED; |
| 707 | return aps->getOutputForEffect(desc); |
| 708 | } |
| 709 | |
| 710 | status_t AudioSystem::registerEffect(effect_descriptor_t *desc, |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 711 | audio_io_handle_t io, |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 712 | uint32_t strategy, |
| 713 | int session, |
| 714 | int id) |
| 715 | { |
| 716 | const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service(); |
| 717 | if (aps == 0) return PERMISSION_DENIED; |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 718 | return aps->registerEffect(desc, io, strategy, session, id); |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 719 | } |
| 720 | |
| 721 | status_t AudioSystem::unregisterEffect(int id) |
| 722 | { |
| 723 | const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service(); |
| 724 | if (aps == 0) return PERMISSION_DENIED; |
| 725 | return aps->unregisterEffect(id); |
| 726 | } |
| 727 | |
Eric Laurent | db7c079 | 2011-08-10 10:37:50 -0700 | [diff] [blame] | 728 | status_t AudioSystem::setEffectEnabled(int id, bool enabled) |
| 729 | { |
| 730 | const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service(); |
| 731 | if (aps == 0) return PERMISSION_DENIED; |
| 732 | return aps->setEffectEnabled(id, enabled); |
| 733 | } |
| 734 | |
Glenn Kasten | fff6d71 | 2012-01-12 16:38:12 -0800 | [diff] [blame] | 735 | status_t AudioSystem::isStreamActive(audio_stream_type_t stream, bool* state, uint32_t inPastMs) |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 736 | { |
Eric Laurent | eda6c36 | 2011-02-02 09:33:30 -0800 | [diff] [blame] | 737 | const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service(); |
| 738 | if (aps == 0) return PERMISSION_DENIED; |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 739 | if (state == NULL) return BAD_VALUE; |
Eric Laurent | eda6c36 | 2011-02-02 09:33:30 -0800 | [diff] [blame] | 740 | *state = aps->isStreamActive(stream, inPastMs); |
| 741 | return NO_ERROR; |
| 742 | } |
| 743 | |
| 744 | |
Eric Laurent | 9f6530f | 2011-08-30 10:18:54 -0700 | [diff] [blame] | 745 | void AudioSystem::clearAudioConfigCache() |
| 746 | { |
| 747 | Mutex::Autolock _l(gLock); |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 748 | ALOGV("clearAudioConfigCache()"); |
Eric Laurent | 9f6530f | 2011-08-30 10:18:54 -0700 | [diff] [blame] | 749 | gStreamOutputMap.clear(); |
| 750 | gOutputs.clear(); |
| 751 | } |
| 752 | |
Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 753 | // --------------------------------------------------------------------------- |
| 754 | |
| 755 | void AudioSystem::AudioPolicyServiceClient::binderDied(const wp<IBinder>& who) { |
| 756 | Mutex::Autolock _l(AudioSystem::gLock); |
| 757 | AudioSystem::gAudioPolicyService.clear(); |
| 758 | |
Steve Block | 5ff1dd5 | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 759 | ALOGW("AudioPolicyService server died!"); |
Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 760 | } |
| 761 | |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 762 | }; // namespace android |
| 763 | |