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