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