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