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