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; |
Glenn Kasten | d2d089f | 2014-11-05 11:48:12 -0800 | [diff] [blame] | 35 | Mutex AudioSystem::gLockAPS; |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 36 | sp<IAudioFlinger> AudioSystem::gAudioFlinger; |
| 37 | sp<AudioSystem::AudioFlingerClient> AudioSystem::gAudioFlingerClient; |
| 38 | audio_error_callback AudioSystem::gAudioErrorCallback = NULL; |
Jean-Michel Trivi | f613d42 | 2015-04-23 18:41:29 -0700 | [diff] [blame] | 39 | dynamic_policy_callback AudioSystem::gDynPolicyCallback = NULL; |
Jean-Michel Trivi | 2f4fe9f | 2015-12-04 16:20:59 -0800 | [diff] [blame] | 40 | record_config_callback AudioSystem::gRecordConfigCallback = NULL; |
Glenn Kasten | 211eeaf | 2012-01-20 09:37:45 -0800 | [diff] [blame] | 41 | |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 42 | |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 43 | // establish binder interface to AudioFlinger service |
Eric Laurent | 0ebd5f9 | 2014-11-19 19:04:52 -0800 | [diff] [blame] | 44 | const sp<IAudioFlinger> AudioSystem::get_audio_flinger() |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 45 | { |
Eric Laurent | 0ebd5f9 | 2014-11-19 19:04:52 -0800 | [diff] [blame] | 46 | sp<IAudioFlinger> af; |
| 47 | sp<AudioFlingerClient> afc; |
| 48 | { |
| 49 | Mutex::Autolock _l(gLock); |
| 50 | if (gAudioFlinger == 0) { |
| 51 | sp<IServiceManager> sm = defaultServiceManager(); |
| 52 | sp<IBinder> binder; |
| 53 | do { |
| 54 | binder = sm->getService(String16("media.audio_flinger")); |
| 55 | if (binder != 0) |
| 56 | break; |
| 57 | ALOGW("AudioFlinger not published, waiting..."); |
| 58 | usleep(500000); // 0.5 s |
| 59 | } while (true); |
| 60 | if (gAudioFlingerClient == NULL) { |
| 61 | gAudioFlingerClient = new AudioFlingerClient(); |
| 62 | } else { |
| 63 | if (gAudioErrorCallback) { |
| 64 | gAudioErrorCallback(NO_ERROR); |
| 65 | } |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 66 | } |
Eric Laurent | 0ebd5f9 | 2014-11-19 19:04:52 -0800 | [diff] [blame] | 67 | binder->linkToDeath(gAudioFlingerClient); |
| 68 | gAudioFlinger = interface_cast<IAudioFlinger>(binder); |
| 69 | LOG_ALWAYS_FATAL_IF(gAudioFlinger == 0); |
| 70 | afc = gAudioFlingerClient; |
Glenn Kasten | e53b9ea | 2012-03-12 16:29:55 -0700 | [diff] [blame] | 71 | } |
Eric Laurent | 0ebd5f9 | 2014-11-19 19:04:52 -0800 | [diff] [blame] | 72 | af = gAudioFlinger; |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 73 | } |
Eric Laurent | 0ebd5f9 | 2014-11-19 19:04:52 -0800 | [diff] [blame] | 74 | if (afc != 0) { |
| 75 | af->registerClient(afc); |
| 76 | } |
| 77 | return af; |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 78 | } |
| 79 | |
Eric Laurent | 296fb13 | 2015-05-01 11:38:42 -0700 | [diff] [blame] | 80 | const sp<AudioSystem::AudioFlingerClient> AudioSystem::getAudioFlingerClient() |
| 81 | { |
| 82 | // calling get_audio_flinger() will initialize gAudioFlingerClient if needed |
| 83 | const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger(); |
| 84 | if (af == 0) return 0; |
| 85 | Mutex::Autolock _l(gLock); |
| 86 | return gAudioFlingerClient; |
| 87 | } |
| 88 | |
| 89 | sp<AudioIoDescriptor> AudioSystem::getIoDescriptor(audio_io_handle_t ioHandle) |
| 90 | { |
| 91 | sp<AudioIoDescriptor> desc; |
| 92 | const sp<AudioFlingerClient> afc = getAudioFlingerClient(); |
| 93 | if (afc != 0) { |
| 94 | desc = afc->getIoDescriptor(ioHandle); |
| 95 | } |
| 96 | return desc; |
| 97 | } |
| 98 | |
Eric Laurent | 4629161 | 2013-07-18 14:38:44 -0700 | [diff] [blame] | 99 | /* static */ status_t AudioSystem::checkAudioFlinger() |
| 100 | { |
| 101 | if (defaultServiceManager()->checkService(String16("media.audio_flinger")) != 0) { |
| 102 | return NO_ERROR; |
| 103 | } |
| 104 | return DEAD_OBJECT; |
| 105 | } |
| 106 | |
Glenn Kasten | 4a8308b | 2016-04-18 14:10:01 -0700 | [diff] [blame^] | 107 | // FIXME Declare in binder opcode order, similarly to IAudioFlinger.h and IAudioFlinger.cpp |
| 108 | |
Glenn Kasten | 4944acb | 2013-08-19 08:39:20 -0700 | [diff] [blame] | 109 | status_t AudioSystem::muteMicrophone(bool state) |
| 110 | { |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 111 | const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger(); |
| 112 | if (af == 0) return PERMISSION_DENIED; |
| 113 | return af->setMicMute(state); |
| 114 | } |
| 115 | |
Glenn Kasten | 4944acb | 2013-08-19 08:39:20 -0700 | [diff] [blame] | 116 | status_t AudioSystem::isMicrophoneMuted(bool* state) |
| 117 | { |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 118 | const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger(); |
| 119 | if (af == 0) return PERMISSION_DENIED; |
| 120 | *state = af->getMicMute(); |
| 121 | return NO_ERROR; |
| 122 | } |
| 123 | |
| 124 | status_t AudioSystem::setMasterVolume(float value) |
| 125 | { |
| 126 | const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger(); |
| 127 | if (af == 0) return PERMISSION_DENIED; |
| 128 | af->setMasterVolume(value); |
| 129 | return NO_ERROR; |
| 130 | } |
| 131 | |
| 132 | status_t AudioSystem::setMasterMute(bool mute) |
| 133 | { |
| 134 | const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger(); |
| 135 | if (af == 0) return PERMISSION_DENIED; |
| 136 | af->setMasterMute(mute); |
| 137 | return NO_ERROR; |
| 138 | } |
| 139 | |
| 140 | status_t AudioSystem::getMasterVolume(float* volume) |
| 141 | { |
| 142 | const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger(); |
| 143 | if (af == 0) return PERMISSION_DENIED; |
| 144 | *volume = af->masterVolume(); |
| 145 | return NO_ERROR; |
| 146 | } |
| 147 | |
| 148 | status_t AudioSystem::getMasterMute(bool* mute) |
| 149 | { |
| 150 | const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger(); |
| 151 | if (af == 0) return PERMISSION_DENIED; |
| 152 | *mute = af->masterMute(); |
| 153 | return NO_ERROR; |
| 154 | } |
| 155 | |
Glenn Kasten | 72ef00d | 2012-01-17 11:09:42 -0800 | [diff] [blame] | 156 | status_t AudioSystem::setStreamVolume(audio_stream_type_t stream, float value, |
| 157 | audio_io_handle_t output) |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 158 | { |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 159 | 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] | 160 | const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger(); |
| 161 | if (af == 0) return PERMISSION_DENIED; |
Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 162 | af->setStreamVolume(stream, value, output); |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 163 | return NO_ERROR; |
| 164 | } |
| 165 | |
Glenn Kasten | fff6d71 | 2012-01-12 16:38:12 -0800 | [diff] [blame] | 166 | 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] | 167 | { |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 168 | 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] | 169 | const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger(); |
| 170 | if (af == 0) return PERMISSION_DENIED; |
| 171 | af->setStreamMute(stream, mute); |
| 172 | return NO_ERROR; |
| 173 | } |
| 174 | |
Glenn Kasten | 72ef00d | 2012-01-17 11:09:42 -0800 | [diff] [blame] | 175 | status_t AudioSystem::getStreamVolume(audio_stream_type_t stream, float* volume, |
| 176 | audio_io_handle_t output) |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 177 | { |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 178 | 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] | 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 | *volume = af->streamVolume(stream, output); |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 182 | return NO_ERROR; |
| 183 | } |
| 184 | |
Glenn Kasten | fff6d71 | 2012-01-12 16:38:12 -0800 | [diff] [blame] | 185 | 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] | 186 | { |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 187 | 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] | 188 | const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger(); |
| 189 | if (af == 0) return PERMISSION_DENIED; |
| 190 | *mute = af->streamMute(stream); |
| 191 | return NO_ERROR; |
| 192 | } |
| 193 | |
Glenn Kasten | f78aee7 | 2012-01-04 11:00:47 -0800 | [diff] [blame] | 194 | status_t AudioSystem::setMode(audio_mode_t mode) |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 195 | { |
Glenn Kasten | 930f4ca | 2012-01-06 16:47:31 -0800 | [diff] [blame] | 196 | 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] | 197 | const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger(); |
| 198 | if (af == 0) return PERMISSION_DENIED; |
| 199 | return af->setMode(mode); |
| 200 | } |
| 201 | |
Glenn Kasten | 4944acb | 2013-08-19 08:39:20 -0700 | [diff] [blame] | 202 | status_t AudioSystem::setParameters(audio_io_handle_t ioHandle, const String8& keyValuePairs) |
| 203 | { |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 204 | const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger(); |
| 205 | if (af == 0) return PERMISSION_DENIED; |
Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 206 | return af->setParameters(ioHandle, keyValuePairs); |
| 207 | } |
| 208 | |
Glenn Kasten | 4944acb | 2013-08-19 08:39:20 -0700 | [diff] [blame] | 209 | String8 AudioSystem::getParameters(audio_io_handle_t ioHandle, const String8& keys) |
| 210 | { |
Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 211 | const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger(); |
| 212 | String8 result = String8(""); |
| 213 | if (af == 0) return result; |
| 214 | |
| 215 | result = af->getParameters(ioHandle, keys); |
| 216 | return result; |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 217 | } |
| 218 | |
Glenn Kasten | c23885e | 2013-12-19 16:35:18 -0800 | [diff] [blame] | 219 | status_t AudioSystem::setParameters(const String8& keyValuePairs) |
| 220 | { |
Glenn Kasten | 142f519 | 2014-03-25 17:44:59 -0700 | [diff] [blame] | 221 | return setParameters(AUDIO_IO_HANDLE_NONE, keyValuePairs); |
Glenn Kasten | c23885e | 2013-12-19 16:35:18 -0800 | [diff] [blame] | 222 | } |
| 223 | |
| 224 | String8 AudioSystem::getParameters(const String8& keys) |
| 225 | { |
Glenn Kasten | 142f519 | 2014-03-25 17:44:59 -0700 | [diff] [blame] | 226 | return getParameters(AUDIO_IO_HANDLE_NONE, keys); |
Glenn Kasten | c23885e | 2013-12-19 16:35:18 -0800 | [diff] [blame] | 227 | } |
| 228 | |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 229 | // convert volume steps to natural log scale |
| 230 | |
| 231 | // change this value to change volume scaling |
| 232 | static const float dBPerStep = 0.5f; |
| 233 | // shouldn't need to touch these |
| 234 | static const float dBConvert = -dBPerStep * 2.302585093f / 20.0f; |
| 235 | static const float dBConvertInverse = 1.0f / dBConvert; |
| 236 | |
| 237 | float AudioSystem::linearToLog(int volume) |
| 238 | { |
| 239 | // float v = volume ? exp(float(100 - volume) * dBConvert) : 0; |
Steve Block | b8a8052 | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 240 | // ALOGD("linearToLog(%d)=%f", volume, v); |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 241 | // return v; |
| 242 | return volume ? exp(float(100 - volume) * dBConvert) : 0; |
| 243 | } |
| 244 | |
| 245 | int AudioSystem::logToLinear(float volume) |
| 246 | { |
| 247 | // int v = volume ? 100 - int(dBConvertInverse * log(volume) + 0.5) : 0; |
Steve Block | b8a8052 | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 248 | // ALOGD("logTolinear(%d)=%f", v, volume); |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 249 | // return v; |
| 250 | return volume ? 100 - int(dBConvertInverse * log(volume) + 0.5) : 0; |
| 251 | } |
| 252 | |
Glenn Kasten | 3b16c76 | 2012-11-14 08:44:39 -0800 | [diff] [blame] | 253 | 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] | 254 | { |
Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 255 | audio_io_handle_t output; |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 256 | |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 257 | if (streamType == AUDIO_STREAM_DEFAULT) { |
| 258 | streamType = AUDIO_STREAM_MUSIC; |
Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 259 | } |
| 260 | |
Glenn Kasten | fff6d71 | 2012-01-12 16:38:12 -0800 | [diff] [blame] | 261 | output = getOutput(streamType); |
Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 262 | if (output == 0) { |
| 263 | return PERMISSION_DENIED; |
| 264 | } |
| 265 | |
Jean-Michel Trivi | b7f24b1 | 2014-06-11 10:05:30 -0700 | [diff] [blame] | 266 | return getSamplingRate(output, samplingRate); |
Eric Laurent | 1a9ed11 | 2012-03-20 18:36:01 -0700 | [diff] [blame] | 267 | } |
| 268 | |
Glenn Kasten | 2c073da | 2016-02-26 09:14:08 -0800 | [diff] [blame] | 269 | status_t AudioSystem::getSamplingRate(audio_io_handle_t ioHandle, |
Glenn Kasten | 3b16c76 | 2012-11-14 08:44:39 -0800 | [diff] [blame] | 270 | uint32_t* samplingRate) |
Eric Laurent | 1a9ed11 | 2012-03-20 18:36:01 -0700 | [diff] [blame] | 271 | { |
Eric Laurent | f6778fd | 2014-11-18 17:26:58 -0800 | [diff] [blame] | 272 | const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger(); |
| 273 | if (af == 0) return PERMISSION_DENIED; |
Glenn Kasten | 2c073da | 2016-02-26 09:14:08 -0800 | [diff] [blame] | 274 | sp<AudioIoDescriptor> desc = getIoDescriptor(ioHandle); |
| 275 | if (desc == 0) { |
| 276 | *samplingRate = af->sampleRate(ioHandle); |
Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 277 | } else { |
Glenn Kasten | 2c073da | 2016-02-26 09:14:08 -0800 | [diff] [blame] | 278 | *samplingRate = desc->mSamplingRate; |
Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 279 | } |
Glenn Kasten | f94006c | 2014-01-08 08:56:06 -0800 | [diff] [blame] | 280 | if (*samplingRate == 0) { |
Glenn Kasten | 2c073da | 2016-02-26 09:14:08 -0800 | [diff] [blame] | 281 | ALOGE("AudioSystem::getSamplingRate failed for ioHandle %d", ioHandle); |
Glenn Kasten | f94006c | 2014-01-08 08:56:06 -0800 | [diff] [blame] | 282 | return BAD_VALUE; |
| 283 | } |
Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 284 | |
Glenn Kasten | 2c073da | 2016-02-26 09:14:08 -0800 | [diff] [blame] | 285 | ALOGV("getSamplingRate() ioHandle %d, sampling rate %u", ioHandle, *samplingRate); |
Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 286 | |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 287 | return NO_ERROR; |
| 288 | } |
| 289 | |
Glenn Kasten | e33054e | 2012-11-14 12:54:39 -0800 | [diff] [blame] | 290 | 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] | 291 | { |
Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 292 | audio_io_handle_t output; |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 293 | |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 294 | if (streamType == AUDIO_STREAM_DEFAULT) { |
| 295 | streamType = AUDIO_STREAM_MUSIC; |
Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 296 | } |
Eric Laurent | 48f7f5e | 2009-04-02 09:32:43 -0700 | [diff] [blame] | 297 | |
Glenn Kasten | fff6d71 | 2012-01-12 16:38:12 -0800 | [diff] [blame] | 298 | output = getOutput(streamType); |
Glenn Kasten | 142f519 | 2014-03-25 17:44:59 -0700 | [diff] [blame] | 299 | if (output == AUDIO_IO_HANDLE_NONE) { |
Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 300 | return PERMISSION_DENIED; |
| 301 | } |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 302 | |
Jean-Michel Trivi | b7f24b1 | 2014-06-11 10:05:30 -0700 | [diff] [blame] | 303 | return getFrameCount(output, frameCount); |
Eric Laurent | 1a9ed11 | 2012-03-20 18:36:01 -0700 | [diff] [blame] | 304 | } |
| 305 | |
Glenn Kasten | 2c073da | 2016-02-26 09:14:08 -0800 | [diff] [blame] | 306 | status_t AudioSystem::getFrameCount(audio_io_handle_t ioHandle, |
Glenn Kasten | e33054e | 2012-11-14 12:54:39 -0800 | [diff] [blame] | 307 | size_t* frameCount) |
Eric Laurent | 1a9ed11 | 2012-03-20 18:36:01 -0700 | [diff] [blame] | 308 | { |
Eric Laurent | f6778fd | 2014-11-18 17:26:58 -0800 | [diff] [blame] | 309 | const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger(); |
| 310 | if (af == 0) return PERMISSION_DENIED; |
Glenn Kasten | 2c073da | 2016-02-26 09:14:08 -0800 | [diff] [blame] | 311 | sp<AudioIoDescriptor> desc = getIoDescriptor(ioHandle); |
| 312 | if (desc == 0) { |
| 313 | *frameCount = af->frameCount(ioHandle); |
Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 314 | } else { |
Glenn Kasten | 2c073da | 2016-02-26 09:14:08 -0800 | [diff] [blame] | 315 | *frameCount = desc->mFrameCount; |
Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 316 | } |
Glenn Kasten | f94006c | 2014-01-08 08:56:06 -0800 | [diff] [blame] | 317 | if (*frameCount == 0) { |
Glenn Kasten | 2c073da | 2016-02-26 09:14:08 -0800 | [diff] [blame] | 318 | ALOGE("AudioSystem::getFrameCount failed for ioHandle %d", ioHandle); |
Glenn Kasten | f94006c | 2014-01-08 08:56:06 -0800 | [diff] [blame] | 319 | return BAD_VALUE; |
| 320 | } |
Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 321 | |
Glenn Kasten | 2c073da | 2016-02-26 09:14:08 -0800 | [diff] [blame] | 322 | ALOGV("getFrameCount() ioHandle %d, frameCount %zu", ioHandle, *frameCount); |
Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 323 | |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 324 | return NO_ERROR; |
| 325 | } |
| 326 | |
Glenn Kasten | fff6d71 | 2012-01-12 16:38:12 -0800 | [diff] [blame] | 327 | 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] | 328 | { |
Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 329 | audio_io_handle_t output; |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 330 | |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 331 | if (streamType == AUDIO_STREAM_DEFAULT) { |
| 332 | streamType = AUDIO_STREAM_MUSIC; |
Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 333 | } |
Eric Laurent | 48f7f5e | 2009-04-02 09:32:43 -0700 | [diff] [blame] | 334 | |
Glenn Kasten | fff6d71 | 2012-01-12 16:38:12 -0800 | [diff] [blame] | 335 | output = getOutput(streamType); |
Glenn Kasten | 142f519 | 2014-03-25 17:44:59 -0700 | [diff] [blame] | 336 | if (output == AUDIO_IO_HANDLE_NONE) { |
Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 337 | return PERMISSION_DENIED; |
| 338 | } |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 339 | |
Glenn Kasten | 241618f | 2014-03-25 17:48:57 -0700 | [diff] [blame] | 340 | return getLatency(output, latency); |
Eric Laurent | 1a9ed11 | 2012-03-20 18:36:01 -0700 | [diff] [blame] | 341 | } |
| 342 | |
| 343 | status_t AudioSystem::getLatency(audio_io_handle_t output, |
Eric Laurent | 1a9ed11 | 2012-03-20 18:36:01 -0700 | [diff] [blame] | 344 | uint32_t* latency) |
| 345 | { |
Eric Laurent | f6778fd | 2014-11-18 17:26:58 -0800 | [diff] [blame] | 346 | const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger(); |
| 347 | if (af == 0) return PERMISSION_DENIED; |
Eric Laurent | 296fb13 | 2015-05-01 11:38:42 -0700 | [diff] [blame] | 348 | sp<AudioIoDescriptor> outputDesc = getIoDescriptor(output); |
Eric Laurent | 73e26b6 | 2015-04-27 16:55:58 -0700 | [diff] [blame] | 349 | if (outputDesc == 0) { |
Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 350 | *latency = af->latency(output); |
| 351 | } else { |
Eric Laurent | 73e26b6 | 2015-04-27 16:55:58 -0700 | [diff] [blame] | 352 | *latency = outputDesc->mLatency; |
Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 353 | } |
| 354 | |
Glenn Kasten | 241618f | 2014-03-25 17:48:57 -0700 | [diff] [blame] | 355 | ALOGV("getLatency() output %d, latency %d", output, *latency); |
Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 356 | |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 357 | return NO_ERROR; |
| 358 | } |
| 359 | |
Glenn Kasten | dd8104c | 2012-07-02 12:42:44 -0700 | [diff] [blame] | 360 | status_t AudioSystem::getInputBufferSize(uint32_t sampleRate, audio_format_t format, |
| 361 | audio_channel_mask_t channelMask, size_t* buffSize) |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 362 | { |
Eric Laurent | 296fb13 | 2015-05-01 11:38:42 -0700 | [diff] [blame] | 363 | const sp<AudioFlingerClient> afc = getAudioFlingerClient(); |
| 364 | if (afc == 0) { |
| 365 | return NO_INIT; |
| 366 | } |
| 367 | return afc->getInputBufferSize(sampleRate, format, channelMask, buffSize); |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 368 | } |
| 369 | |
Eric Laurent | f0ee6f4 | 2009-10-21 08:14:22 -0700 | [diff] [blame] | 370 | status_t AudioSystem::setVoiceVolume(float value) |
| 371 | { |
| 372 | const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger(); |
| 373 | if (af == 0) return PERMISSION_DENIED; |
| 374 | return af->setVoiceVolume(value); |
| 375 | } |
| 376 | |
Kévin PETIT | 377b2ec | 2014-02-03 12:35:36 +0000 | [diff] [blame] | 377 | status_t AudioSystem::getRenderPosition(audio_io_handle_t output, uint32_t *halFrames, |
Glenn Kasten | 0ed1959 | 2014-03-26 07:50:05 -0700 | [diff] [blame] | 378 | uint32_t *dspFrames) |
Eric Laurent | 342e9cf | 2010-01-19 17:37:09 -0800 | [diff] [blame] | 379 | { |
| 380 | const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger(); |
| 381 | if (af == 0) return PERMISSION_DENIED; |
| 382 | |
Richard Fitzgerald | ad3af33 | 2013-03-25 16:54:37 +0000 | [diff] [blame] | 383 | return af->getRenderPosition(halFrames, dspFrames, output); |
Eric Laurent | 342e9cf | 2010-01-19 17:37:09 -0800 | [diff] [blame] | 384 | } |
| 385 | |
Glenn Kasten | 4944acb | 2013-08-19 08:39:20 -0700 | [diff] [blame] | 386 | uint32_t AudioSystem::getInputFramesLost(audio_io_handle_t ioHandle) |
| 387 | { |
Eric Laurent | 05bca2f | 2010-02-26 02:47:27 -0800 | [diff] [blame] | 388 | const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger(); |
Glenn Kasten | 5f972c0 | 2014-01-13 09:59:31 -0800 | [diff] [blame] | 389 | uint32_t result = 0; |
Eric Laurent | 05bca2f | 2010-02-26 02:47:27 -0800 | [diff] [blame] | 390 | if (af == 0) return result; |
Glenn Kasten | 142f519 | 2014-03-25 17:44:59 -0700 | [diff] [blame] | 391 | if (ioHandle == AUDIO_IO_HANDLE_NONE) return result; |
Eric Laurent | 05bca2f | 2010-02-26 02:47:27 -0800 | [diff] [blame] | 392 | |
| 393 | result = af->getInputFramesLost(ioHandle); |
| 394 | return result; |
| 395 | } |
| 396 | |
Glenn Kasten | eeecb98 | 2016-02-26 10:44:04 -0800 | [diff] [blame] | 397 | audio_unique_id_t AudioSystem::newAudioUniqueId(audio_unique_id_use_t use) |
Glenn Kasten | 4944acb | 2013-08-19 08:39:20 -0700 | [diff] [blame] | 398 | { |
Eric Laurent | be916aa | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 399 | const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger(); |
Eric Laurent | de3f839 | 2014-07-27 18:38:22 -0700 | [diff] [blame] | 400 | if (af == 0) return AUDIO_UNIQUE_ID_ALLOCATE; |
Glenn Kasten | eeecb98 | 2016-02-26 10:44:04 -0800 | [diff] [blame] | 401 | return af->newAudioUniqueId(use); |
Eric Laurent | be916aa | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 402 | } |
| 403 | |
Glenn Kasten | d848eb4 | 2016-03-08 13:42:11 -0800 | [diff] [blame] | 404 | void AudioSystem::acquireAudioSessionId(audio_session_t audioSession, pid_t pid) |
Glenn Kasten | 4944acb | 2013-08-19 08:39:20 -0700 | [diff] [blame] | 405 | { |
Marco Nelissen | 3a34bef | 2011-08-02 13:33:41 -0700 | [diff] [blame] | 406 | const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger(); |
| 407 | if (af != 0) { |
Marco Nelissen | d457c97 | 2014-02-11 08:47:07 -0800 | [diff] [blame] | 408 | af->acquireAudioSessionId(audioSession, pid); |
Marco Nelissen | 3a34bef | 2011-08-02 13:33:41 -0700 | [diff] [blame] | 409 | } |
| 410 | } |
| 411 | |
Glenn Kasten | d848eb4 | 2016-03-08 13:42:11 -0800 | [diff] [blame] | 412 | void AudioSystem::releaseAudioSessionId(audio_session_t audioSession, pid_t pid) |
Glenn Kasten | 4944acb | 2013-08-19 08:39:20 -0700 | [diff] [blame] | 413 | { |
Marco Nelissen | 3a34bef | 2011-08-02 13:33:41 -0700 | [diff] [blame] | 414 | const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger(); |
| 415 | if (af != 0) { |
Marco Nelissen | d457c97 | 2014-02-11 08:47:07 -0800 | [diff] [blame] | 416 | af->releaseAudioSessionId(audioSession, pid); |
Marco Nelissen | 3a34bef | 2011-08-02 13:33:41 -0700 | [diff] [blame] | 417 | } |
| 418 | } |
| 419 | |
Eric Laurent | 93c3d41 | 2014-08-01 14:48:35 -0700 | [diff] [blame] | 420 | audio_hw_sync_t AudioSystem::getAudioHwSyncForSession(audio_session_t sessionId) |
| 421 | { |
| 422 | const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger(); |
| 423 | if (af == 0) return AUDIO_HW_SYNC_INVALID; |
| 424 | return af->getAudioHwSyncForSession(sessionId); |
| 425 | } |
| 426 | |
Eric Laurent | 72e3f39 | 2015-05-20 14:43:50 -0700 | [diff] [blame] | 427 | status_t AudioSystem::systemReady() |
| 428 | { |
| 429 | const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger(); |
| 430 | if (af == 0) return NO_INIT; |
| 431 | return af->systemReady(); |
| 432 | } |
| 433 | |
Glenn Kasten | 4a8308b | 2016-04-18 14:10:01 -0700 | [diff] [blame^] | 434 | status_t AudioSystem::getFrameCountHAL(audio_io_handle_t ioHandle, |
| 435 | size_t* frameCount) |
| 436 | { |
| 437 | const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger(); |
| 438 | if (af == 0) return PERMISSION_DENIED; |
| 439 | sp<AudioIoDescriptor> desc = getIoDescriptor(ioHandle); |
| 440 | if (desc == 0) { |
| 441 | *frameCount = af->frameCountHAL(ioHandle); |
| 442 | } else { |
| 443 | *frameCount = desc->mFrameCountHAL; |
| 444 | } |
| 445 | if (*frameCount == 0) { |
| 446 | ALOGE("AudioSystem::getFrameCountHAL failed for ioHandle %d", ioHandle); |
| 447 | return BAD_VALUE; |
| 448 | } |
| 449 | |
| 450 | ALOGV("getFrameCountHAL() ioHandle %d, frameCount %zu", ioHandle, *frameCount); |
| 451 | |
| 452 | return NO_ERROR; |
| 453 | } |
| 454 | |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 455 | // --------------------------------------------------------------------------- |
| 456 | |
Eric Laurent | 73e26b6 | 2015-04-27 16:55:58 -0700 | [diff] [blame] | 457 | |
| 458 | void AudioSystem::AudioFlingerClient::clearIoCache() |
| 459 | { |
| 460 | Mutex::Autolock _l(mLock); |
| 461 | mIoDescriptors.clear(); |
| 462 | mInBuffSize = 0; |
| 463 | mInSamplingRate = 0; |
| 464 | mInFormat = AUDIO_FORMAT_DEFAULT; |
| 465 | mInChannelMask = AUDIO_CHANNEL_NONE; |
| 466 | } |
| 467 | |
Glenn Kasten | 4944acb | 2013-08-19 08:39:20 -0700 | [diff] [blame] | 468 | void AudioSystem::AudioFlingerClient::binderDied(const wp<IBinder>& who __unused) |
| 469 | { |
Eric Laurent | f6778fd | 2014-11-18 17:26:58 -0800 | [diff] [blame] | 470 | audio_error_callback cb = NULL; |
| 471 | { |
| 472 | Mutex::Autolock _l(AudioSystem::gLock); |
| 473 | AudioSystem::gAudioFlinger.clear(); |
| 474 | cb = gAudioErrorCallback; |
| 475 | } |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 476 | |
Eric Laurent | 73e26b6 | 2015-04-27 16:55:58 -0700 | [diff] [blame] | 477 | // clear output handles and stream to output map caches |
| 478 | clearIoCache(); |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 479 | |
Eric Laurent | f6778fd | 2014-11-18 17:26:58 -0800 | [diff] [blame] | 480 | if (cb) { |
| 481 | cb(DEAD_OBJECT); |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 482 | } |
Steve Block | 5ff1dd5 | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 483 | ALOGW("AudioFlinger server died!"); |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 484 | } |
| 485 | |
Eric Laurent | 73e26b6 | 2015-04-27 16:55:58 -0700 | [diff] [blame] | 486 | void AudioSystem::AudioFlingerClient::ioConfigChanged(audio_io_config_event event, |
| 487 | const sp<AudioIoDescriptor>& ioDesc) { |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 488 | ALOGV("ioConfigChanged() event %d", event); |
Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 489 | |
Eric Laurent | 73e26b6 | 2015-04-27 16:55:58 -0700 | [diff] [blame] | 490 | if (ioDesc == 0 || ioDesc->mIoHandle == AUDIO_IO_HANDLE_NONE) return; |
Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 491 | |
Eric Laurent | 296fb13 | 2015-05-01 11:38:42 -0700 | [diff] [blame] | 492 | audio_port_handle_t deviceId = AUDIO_PORT_HANDLE_NONE; |
| 493 | Vector < sp<AudioDeviceCallback> > callbacks; |
Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 494 | |
Eric Laurent | 296fb13 | 2015-05-01 11:38:42 -0700 | [diff] [blame] | 495 | { |
| 496 | Mutex::Autolock _l(mLock); |
| 497 | |
| 498 | switch (event) { |
| 499 | case AUDIO_OUTPUT_OPENED: |
| 500 | case AUDIO_INPUT_OPENED: { |
Praveen Chavan | 49fdeaf | 2015-09-29 02:25:47 -0700 | [diff] [blame] | 501 | sp<AudioIoDescriptor> oldDesc = getIoDescriptor_l(ioDesc->mIoHandle); |
Eric Laurent | 7c1ec5f | 2015-07-09 14:52:47 -0700 | [diff] [blame] | 502 | if (oldDesc == 0) { |
| 503 | mIoDescriptors.add(ioDesc->mIoHandle, ioDesc); |
| 504 | } else { |
| 505 | deviceId = oldDesc->getDeviceId(); |
| 506 | mIoDescriptors.replaceValueFor(ioDesc->mIoHandle, ioDesc); |
Eric Laurent | 296fb13 | 2015-05-01 11:38:42 -0700 | [diff] [blame] | 507 | } |
Eric Laurent | 296fb13 | 2015-05-01 11:38:42 -0700 | [diff] [blame] | 508 | |
| 509 | if (ioDesc->getDeviceId() != AUDIO_PORT_HANDLE_NONE) { |
| 510 | deviceId = ioDesc->getDeviceId(); |
| 511 | ssize_t ioIndex = mAudioDeviceCallbacks.indexOfKey(ioDesc->mIoHandle); |
| 512 | if (ioIndex >= 0) { |
| 513 | callbacks = mAudioDeviceCallbacks.valueAt(ioIndex); |
| 514 | } |
| 515 | } |
| 516 | ALOGV("ioConfigChanged() new %s opened %d samplingRate %u, format %#x channel mask %#x " |
| 517 | "frameCount %zu deviceId %d", event == AUDIO_OUTPUT_OPENED ? "output" : "input", |
| 518 | ioDesc->mIoHandle, ioDesc->mSamplingRate, ioDesc->mFormat, ioDesc->mChannelMask, |
| 519 | ioDesc->mFrameCount, ioDesc->getDeviceId()); |
| 520 | } break; |
| 521 | case AUDIO_OUTPUT_CLOSED: |
| 522 | case AUDIO_INPUT_CLOSED: { |
Praveen Chavan | 49fdeaf | 2015-09-29 02:25:47 -0700 | [diff] [blame] | 523 | if (getIoDescriptor_l(ioDesc->mIoHandle) == 0) { |
Eric Laurent | 296fb13 | 2015-05-01 11:38:42 -0700 | [diff] [blame] | 524 | ALOGW("ioConfigChanged() closing unknown %s %d", |
| 525 | event == AUDIO_OUTPUT_CLOSED ? "output" : "input", ioDesc->mIoHandle); |
| 526 | break; |
| 527 | } |
| 528 | ALOGV("ioConfigChanged() %s %d closed", |
Eric Laurent | 73e26b6 | 2015-04-27 16:55:58 -0700 | [diff] [blame] | 529 | event == AUDIO_OUTPUT_CLOSED ? "output" : "input", ioDesc->mIoHandle); |
Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 530 | |
Eric Laurent | 296fb13 | 2015-05-01 11:38:42 -0700 | [diff] [blame] | 531 | mIoDescriptors.removeItem(ioDesc->mIoHandle); |
| 532 | mAudioDeviceCallbacks.removeItem(ioDesc->mIoHandle); |
| 533 | } break; |
| 534 | |
| 535 | case AUDIO_OUTPUT_CONFIG_CHANGED: |
| 536 | case AUDIO_INPUT_CONFIG_CHANGED: { |
Praveen Chavan | 49fdeaf | 2015-09-29 02:25:47 -0700 | [diff] [blame] | 537 | sp<AudioIoDescriptor> oldDesc = getIoDescriptor_l(ioDesc->mIoHandle); |
Eric Laurent | 296fb13 | 2015-05-01 11:38:42 -0700 | [diff] [blame] | 538 | if (oldDesc == 0) { |
| 539 | ALOGW("ioConfigChanged() modifying unknown output! %d", ioDesc->mIoHandle); |
| 540 | break; |
| 541 | } |
| 542 | |
| 543 | deviceId = oldDesc->getDeviceId(); |
| 544 | mIoDescriptors.replaceValueFor(ioDesc->mIoHandle, ioDesc); |
| 545 | |
| 546 | if (deviceId != ioDesc->getDeviceId()) { |
| 547 | deviceId = ioDesc->getDeviceId(); |
| 548 | ssize_t ioIndex = mAudioDeviceCallbacks.indexOfKey(ioDesc->mIoHandle); |
| 549 | if (ioIndex >= 0) { |
| 550 | callbacks = mAudioDeviceCallbacks.valueAt(ioIndex); |
| 551 | } |
| 552 | } |
| 553 | ALOGV("ioConfigChanged() new config for %s %d samplingRate %u, format %#x " |
Glenn Kasten | 4a8308b | 2016-04-18 14:10:01 -0700 | [diff] [blame^] | 554 | "channel mask %#x frameCount %zu frameCountHAL %zu deviceId %d", |
Eric Laurent | 296fb13 | 2015-05-01 11:38:42 -0700 | [diff] [blame] | 555 | event == AUDIO_OUTPUT_CONFIG_CHANGED ? "output" : "input", |
| 556 | ioDesc->mIoHandle, ioDesc->mSamplingRate, ioDesc->mFormat, |
Glenn Kasten | 4a8308b | 2016-04-18 14:10:01 -0700 | [diff] [blame^] | 557 | ioDesc->mChannelMask, ioDesc->mFrameCount, ioDesc->mFrameCountHAL, ioDesc->getDeviceId()); |
Eric Laurent | 296fb13 | 2015-05-01 11:38:42 -0700 | [diff] [blame] | 558 | |
Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 559 | } break; |
Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 560 | } |
Eric Laurent | 296fb13 | 2015-05-01 11:38:42 -0700 | [diff] [blame] | 561 | } |
| 562 | // callbacks.size() != 0 => ioDesc->mIoHandle and deviceId are valid |
| 563 | for (size_t i = 0; i < callbacks.size(); i++) { |
| 564 | callbacks[i]->onAudioDeviceUpdate(ioDesc->mIoHandle, deviceId); |
Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 565 | } |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 566 | } |
| 567 | |
Eric Laurent | 73e26b6 | 2015-04-27 16:55:58 -0700 | [diff] [blame] | 568 | status_t AudioSystem::AudioFlingerClient::getInputBufferSize( |
| 569 | uint32_t sampleRate, audio_format_t format, |
| 570 | audio_channel_mask_t channelMask, size_t* buffSize) |
| 571 | { |
| 572 | const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger(); |
| 573 | if (af == 0) { |
| 574 | return PERMISSION_DENIED; |
| 575 | } |
| 576 | Mutex::Autolock _l(mLock); |
| 577 | // Do we have a stale mInBuffSize or are we requesting the input buffer size for new values |
| 578 | if ((mInBuffSize == 0) || (sampleRate != mInSamplingRate) || (format != mInFormat) |
| 579 | || (channelMask != mInChannelMask)) { |
| 580 | size_t inBuffSize = af->getInputBufferSize(sampleRate, format, channelMask); |
| 581 | if (inBuffSize == 0) { |
| 582 | ALOGE("AudioSystem::getInputBufferSize failed sampleRate %d format %#x channelMask %x", |
| 583 | sampleRate, format, channelMask); |
| 584 | return BAD_VALUE; |
| 585 | } |
| 586 | // A benign race is possible here: we could overwrite a fresher cache entry |
| 587 | // save the request params |
| 588 | mInSamplingRate = sampleRate; |
| 589 | mInFormat = format; |
| 590 | mInChannelMask = channelMask; |
| 591 | |
| 592 | mInBuffSize = inBuffSize; |
| 593 | } |
| 594 | |
| 595 | *buffSize = mInBuffSize; |
| 596 | |
| 597 | return NO_ERROR; |
| 598 | } |
| 599 | |
Praveen Chavan | 49fdeaf | 2015-09-29 02:25:47 -0700 | [diff] [blame] | 600 | sp<AudioIoDescriptor> AudioSystem::AudioFlingerClient::getIoDescriptor_l(audio_io_handle_t ioHandle) |
Eric Laurent | 73e26b6 | 2015-04-27 16:55:58 -0700 | [diff] [blame] | 601 | { |
| 602 | sp<AudioIoDescriptor> desc; |
| 603 | ssize_t index = mIoDescriptors.indexOfKey(ioHandle); |
| 604 | if (index >= 0) { |
| 605 | desc = mIoDescriptors.valueAt(index); |
| 606 | } |
| 607 | return desc; |
| 608 | } |
| 609 | |
Praveen Chavan | 49fdeaf | 2015-09-29 02:25:47 -0700 | [diff] [blame] | 610 | sp<AudioIoDescriptor> AudioSystem::AudioFlingerClient::getIoDescriptor(audio_io_handle_t ioHandle) |
| 611 | { |
| 612 | Mutex::Autolock _l(mLock); |
| 613 | return getIoDescriptor_l(ioHandle); |
| 614 | } |
| 615 | |
Eric Laurent | 296fb13 | 2015-05-01 11:38:42 -0700 | [diff] [blame] | 616 | status_t AudioSystem::AudioFlingerClient::addAudioDeviceCallback( |
| 617 | const sp<AudioDeviceCallback>& callback, audio_io_handle_t audioIo) |
| 618 | { |
| 619 | Mutex::Autolock _l(mLock); |
| 620 | Vector < sp<AudioDeviceCallback> > callbacks; |
| 621 | ssize_t ioIndex = mAudioDeviceCallbacks.indexOfKey(audioIo); |
| 622 | if (ioIndex >= 0) { |
| 623 | callbacks = mAudioDeviceCallbacks.valueAt(ioIndex); |
| 624 | } |
| 625 | |
| 626 | for (size_t cbIndex = 0; cbIndex < callbacks.size(); cbIndex++) { |
| 627 | if (callbacks[cbIndex] == callback) { |
| 628 | return INVALID_OPERATION; |
| 629 | } |
| 630 | } |
| 631 | callbacks.add(callback); |
| 632 | |
| 633 | mAudioDeviceCallbacks.replaceValueFor(audioIo, callbacks); |
| 634 | return NO_ERROR; |
| 635 | } |
| 636 | |
| 637 | status_t AudioSystem::AudioFlingerClient::removeAudioDeviceCallback( |
| 638 | const sp<AudioDeviceCallback>& callback, audio_io_handle_t audioIo) |
| 639 | { |
| 640 | Mutex::Autolock _l(mLock); |
| 641 | ssize_t ioIndex = mAudioDeviceCallbacks.indexOfKey(audioIo); |
| 642 | if (ioIndex < 0) { |
| 643 | return INVALID_OPERATION; |
| 644 | } |
| 645 | Vector < sp<AudioDeviceCallback> > callbacks = mAudioDeviceCallbacks.valueAt(ioIndex); |
| 646 | |
| 647 | size_t cbIndex; |
| 648 | for (cbIndex = 0; cbIndex < callbacks.size(); cbIndex++) { |
| 649 | if (callbacks[cbIndex] == callback) { |
| 650 | break; |
| 651 | } |
| 652 | } |
| 653 | if (cbIndex == callbacks.size()) { |
| 654 | return INVALID_OPERATION; |
| 655 | } |
| 656 | callbacks.removeAt(cbIndex); |
| 657 | if (callbacks.size() != 0) { |
| 658 | mAudioDeviceCallbacks.replaceValueFor(audioIo, callbacks); |
| 659 | } else { |
| 660 | mAudioDeviceCallbacks.removeItem(audioIo); |
| 661 | } |
| 662 | return NO_ERROR; |
| 663 | } |
| 664 | |
| 665 | /* static */ void AudioSystem::setErrorCallback(audio_error_callback cb) |
Glenn Kasten | 4944acb | 2013-08-19 08:39:20 -0700 | [diff] [blame] | 666 | { |
Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 667 | Mutex::Autolock _l(gLock); |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 668 | gAudioErrorCallback = cb; |
| 669 | } |
| 670 | |
Jean-Michel Trivi | f613d42 | 2015-04-23 18:41:29 -0700 | [diff] [blame] | 671 | /*static*/ void AudioSystem::setDynPolicyCallback(dynamic_policy_callback cb) |
| 672 | { |
| 673 | Mutex::Autolock _l(gLock); |
| 674 | gDynPolicyCallback = cb; |
| 675 | } |
| 676 | |
Jean-Michel Trivi | 2f4fe9f | 2015-12-04 16:20:59 -0800 | [diff] [blame] | 677 | /*static*/ void AudioSystem::setRecordConfigCallback(record_config_callback cb) |
| 678 | { |
| 679 | Mutex::Autolock _l(gLock); |
| 680 | gRecordConfigCallback = cb; |
| 681 | } |
| 682 | |
Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 683 | // client singleton for AudioPolicyService binder interface |
Glenn Kasten | d2d089f | 2014-11-05 11:48:12 -0800 | [diff] [blame] | 684 | // protected by gLockAPS |
Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 685 | sp<IAudioPolicyService> AudioSystem::gAudioPolicyService; |
| 686 | sp<AudioSystem::AudioPolicyServiceClient> AudioSystem::gAudioPolicyServiceClient; |
| 687 | |
| 688 | |
Glenn Kasten | 18a6d90 | 2012-09-24 11:27:56 -0700 | [diff] [blame] | 689 | // establish binder interface to AudioPolicy service |
Eric Laurent | 0ebd5f9 | 2014-11-19 19:04:52 -0800 | [diff] [blame] | 690 | const sp<IAudioPolicyService> AudioSystem::get_audio_policy_service() |
Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 691 | { |
Eric Laurent | 0ebd5f9 | 2014-11-19 19:04:52 -0800 | [diff] [blame] | 692 | sp<IAudioPolicyService> ap; |
| 693 | sp<AudioPolicyServiceClient> apc; |
| 694 | { |
| 695 | Mutex::Autolock _l(gLockAPS); |
| 696 | if (gAudioPolicyService == 0) { |
| 697 | sp<IServiceManager> sm = defaultServiceManager(); |
| 698 | sp<IBinder> binder; |
| 699 | do { |
| 700 | binder = sm->getService(String16("media.audio_policy")); |
| 701 | if (binder != 0) |
| 702 | break; |
| 703 | ALOGW("AudioPolicyService not published, waiting..."); |
| 704 | usleep(500000); // 0.5 s |
| 705 | } while (true); |
| 706 | if (gAudioPolicyServiceClient == NULL) { |
| 707 | gAudioPolicyServiceClient = new AudioPolicyServiceClient(); |
| 708 | } |
| 709 | binder->linkToDeath(gAudioPolicyServiceClient); |
| 710 | gAudioPolicyService = interface_cast<IAudioPolicyService>(binder); |
| 711 | LOG_ALWAYS_FATAL_IF(gAudioPolicyService == 0); |
| 712 | apc = gAudioPolicyServiceClient; |
Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 713 | } |
Eric Laurent | 0ebd5f9 | 2014-11-19 19:04:52 -0800 | [diff] [blame] | 714 | ap = gAudioPolicyService; |
| 715 | } |
| 716 | if (apc != 0) { |
| 717 | ap->registerClient(apc); |
Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 718 | } |
Glenn Kasten | d2d089f | 2014-11-05 11:48:12 -0800 | [diff] [blame] | 719 | |
Eric Laurent | 0ebd5f9 | 2014-11-19 19:04:52 -0800 | [diff] [blame] | 720 | return ap; |
Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 721 | } |
| 722 | |
Glenn Kasten | fb1fdc9 | 2013-07-10 17:03:19 -0700 | [diff] [blame] | 723 | // --------------------------------------------------------------------------- |
| 724 | |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 725 | status_t AudioSystem::setDeviceConnectionState(audio_devices_t device, |
| 726 | audio_policy_dev_state_t state, |
Paul McLean | e743a47 | 2015-01-28 11:07:31 -0800 | [diff] [blame] | 727 | const char *device_address, |
| 728 | const char *device_name) |
Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 729 | { |
| 730 | const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service(); |
Eric Laurent | 71b63e3 | 2011-09-02 14:20:56 -0700 | [diff] [blame] | 731 | const char *address = ""; |
Paul McLean | e743a47 | 2015-01-28 11:07:31 -0800 | [diff] [blame] | 732 | const char *name = ""; |
Eric Laurent | 71b63e3 | 2011-09-02 14:20:56 -0700 | [diff] [blame] | 733 | |
Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 734 | if (aps == 0) return PERMISSION_DENIED; |
| 735 | |
Eric Laurent | 71b63e3 | 2011-09-02 14:20:56 -0700 | [diff] [blame] | 736 | if (device_address != NULL) { |
| 737 | address = device_address; |
| 738 | } |
Paul McLean | e743a47 | 2015-01-28 11:07:31 -0800 | [diff] [blame] | 739 | if (device_name != NULL) { |
| 740 | name = device_name; |
| 741 | } |
| 742 | return aps->setDeviceConnectionState(device, state, address, name); |
Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 743 | } |
| 744 | |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 745 | audio_policy_dev_state_t AudioSystem::getDeviceConnectionState(audio_devices_t device, |
Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 746 | const char *device_address) |
| 747 | { |
| 748 | const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service(); |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 749 | if (aps == 0) return AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE; |
Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 750 | |
| 751 | return aps->getDeviceConnectionState(device, device_address); |
| 752 | } |
| 753 | |
Glenn Kasten | f78aee7 | 2012-01-04 11:00:47 -0800 | [diff] [blame] | 754 | status_t AudioSystem::setPhoneState(audio_mode_t state) |
Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 755 | { |
Glenn Kasten | 347966c | 2012-01-18 14:58:32 -0800 | [diff] [blame] | 756 | if (uint32_t(state) >= AUDIO_MODE_CNT) return BAD_VALUE; |
Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 757 | const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service(); |
| 758 | if (aps == 0) return PERMISSION_DENIED; |
| 759 | |
| 760 | return aps->setPhoneState(state); |
| 761 | } |
| 762 | |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 763 | 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] | 764 | { |
| 765 | const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service(); |
| 766 | if (aps == 0) return PERMISSION_DENIED; |
| 767 | return aps->setForceUse(usage, config); |
| 768 | } |
| 769 | |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 770 | 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] | 771 | { |
| 772 | const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service(); |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 773 | if (aps == 0) return AUDIO_POLICY_FORCE_NONE; |
Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 774 | return aps->getForceUse(usage); |
| 775 | } |
| 776 | |
| 777 | |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 778 | audio_io_handle_t AudioSystem::getOutput(audio_stream_type_t stream, |
Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 779 | uint32_t samplingRate, |
Glenn Kasten | 58f3021 | 2012-01-12 12:27:51 -0800 | [diff] [blame] | 780 | audio_format_t format, |
Glenn Kasten | 254af18 | 2012-07-03 14:59:05 -0700 | [diff] [blame] | 781 | audio_channel_mask_t channelMask, |
Richard Fitzgerald | ad3af33 | 2013-03-25 16:54:37 +0000 | [diff] [blame] | 782 | audio_output_flags_t flags, |
| 783 | const audio_offload_info_t *offloadInfo) |
Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 784 | { |
Eric Laurent | 1a9ed11 | 2012-03-20 18:36:01 -0700 | [diff] [blame] | 785 | const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service(); |
| 786 | if (aps == 0) return 0; |
Richard Fitzgerald | ad3af33 | 2013-03-25 16:54:37 +0000 | [diff] [blame] | 787 | return aps->getOutput(stream, samplingRate, format, channelMask, flags, offloadInfo); |
Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 788 | } |
| 789 | |
Eric Laurent | e83b55d | 2014-11-14 10:06:21 -0800 | [diff] [blame] | 790 | status_t AudioSystem::getOutputForAttr(const audio_attributes_t *attr, |
| 791 | audio_io_handle_t *output, |
| 792 | audio_session_t session, |
| 793 | audio_stream_type_t *stream, |
Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 794 | uid_t uid, |
Eric Laurent | e83b55d | 2014-11-14 10:06:21 -0800 | [diff] [blame] | 795 | uint32_t samplingRate, |
| 796 | audio_format_t format, |
| 797 | audio_channel_mask_t channelMask, |
| 798 | audio_output_flags_t flags, |
Paul McLean | aa98119 | 2015-03-21 09:55:15 -0700 | [diff] [blame] | 799 | audio_port_handle_t selectedDeviceId, |
Eric Laurent | e83b55d | 2014-11-14 10:06:21 -0800 | [diff] [blame] | 800 | const audio_offload_info_t *offloadInfo) |
Jean-Michel Trivi | 5bd3f38 | 2014-06-13 16:06:54 -0700 | [diff] [blame] | 801 | { |
Jean-Michel Trivi | 5bd3f38 | 2014-06-13 16:06:54 -0700 | [diff] [blame] | 802 | const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service(); |
Eric Laurent | e83b55d | 2014-11-14 10:06:21 -0800 | [diff] [blame] | 803 | if (aps == 0) return NO_INIT; |
Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 804 | return aps->getOutputForAttr(attr, output, session, stream, uid, |
Eric Laurent | e83b55d | 2014-11-14 10:06:21 -0800 | [diff] [blame] | 805 | samplingRate, format, channelMask, |
Paul McLean | aa98119 | 2015-03-21 09:55:15 -0700 | [diff] [blame] | 806 | flags, selectedDeviceId, offloadInfo); |
Jean-Michel Trivi | 5bd3f38 | 2014-06-13 16:06:54 -0700 | [diff] [blame] | 807 | } |
| 808 | |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 809 | status_t AudioSystem::startOutput(audio_io_handle_t output, |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 810 | audio_stream_type_t stream, |
Eric Laurent | e83b55d | 2014-11-14 10:06:21 -0800 | [diff] [blame] | 811 | audio_session_t session) |
Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 812 | { |
| 813 | const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service(); |
| 814 | if (aps == 0) return PERMISSION_DENIED; |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 815 | return aps->startOutput(output, stream, session); |
Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 816 | } |
| 817 | |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 818 | status_t AudioSystem::stopOutput(audio_io_handle_t output, |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 819 | audio_stream_type_t stream, |
Eric Laurent | e83b55d | 2014-11-14 10:06:21 -0800 | [diff] [blame] | 820 | audio_session_t session) |
Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 821 | { |
| 822 | const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service(); |
| 823 | if (aps == 0) return PERMISSION_DENIED; |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 824 | return aps->stopOutput(output, stream, session); |
Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 825 | } |
| 826 | |
Eric Laurent | e83b55d | 2014-11-14 10:06:21 -0800 | [diff] [blame] | 827 | void AudioSystem::releaseOutput(audio_io_handle_t output, |
| 828 | audio_stream_type_t stream, |
| 829 | audio_session_t session) |
Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 830 | { |
| 831 | const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service(); |
| 832 | if (aps == 0) return; |
Eric Laurent | e83b55d | 2014-11-14 10:06:21 -0800 | [diff] [blame] | 833 | aps->releaseOutput(output, stream, session); |
Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 834 | } |
| 835 | |
Eric Laurent | caf7f48 | 2014-11-25 17:50:47 -0800 | [diff] [blame] | 836 | status_t AudioSystem::getInputForAttr(const audio_attributes_t *attr, |
| 837 | audio_io_handle_t *input, |
| 838 | audio_session_t session, |
Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 839 | uid_t uid, |
Eric Laurent | caf7f48 | 2014-11-25 17:50:47 -0800 | [diff] [blame] | 840 | uint32_t samplingRate, |
| 841 | audio_format_t format, |
| 842 | audio_channel_mask_t channelMask, |
Paul McLean | 466dc8e | 2015-04-17 13:15:36 -0600 | [diff] [blame] | 843 | audio_input_flags_t flags, |
| 844 | audio_port_handle_t selectedDeviceId) |
Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 845 | { |
| 846 | const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service(); |
Eric Laurent | caf7f48 | 2014-11-25 17:50:47 -0800 | [diff] [blame] | 847 | if (aps == 0) return NO_INIT; |
Paul McLean | 466dc8e | 2015-04-17 13:15:36 -0600 | [diff] [blame] | 848 | return aps->getInputForAttr( |
Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 849 | attr, input, session, uid, samplingRate, format, channelMask, flags, selectedDeviceId); |
Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 850 | } |
| 851 | |
Eric Laurent | 4dc6806 | 2014-07-28 17:26:49 -0700 | [diff] [blame] | 852 | status_t AudioSystem::startInput(audio_io_handle_t input, |
| 853 | audio_session_t session) |
Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 854 | { |
| 855 | const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service(); |
| 856 | if (aps == 0) return PERMISSION_DENIED; |
Eric Laurent | 4dc6806 | 2014-07-28 17:26:49 -0700 | [diff] [blame] | 857 | return aps->startInput(input, session); |
Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 858 | } |
| 859 | |
Eric Laurent | 4dc6806 | 2014-07-28 17:26:49 -0700 | [diff] [blame] | 860 | status_t AudioSystem::stopInput(audio_io_handle_t input, |
| 861 | audio_session_t session) |
Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 862 | { |
| 863 | const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service(); |
| 864 | if (aps == 0) return PERMISSION_DENIED; |
Eric Laurent | 4dc6806 | 2014-07-28 17:26:49 -0700 | [diff] [blame] | 865 | return aps->stopInput(input, session); |
Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 866 | } |
| 867 | |
Eric Laurent | 4dc6806 | 2014-07-28 17:26:49 -0700 | [diff] [blame] | 868 | void AudioSystem::releaseInput(audio_io_handle_t input, |
| 869 | audio_session_t session) |
Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 870 | { |
| 871 | const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service(); |
| 872 | if (aps == 0) return; |
Eric Laurent | 4dc6806 | 2014-07-28 17:26:49 -0700 | [diff] [blame] | 873 | aps->releaseInput(input, session); |
Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 874 | } |
| 875 | |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 876 | status_t AudioSystem::initStreamVolume(audio_stream_type_t stream, |
Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 877 | int indexMin, |
| 878 | int indexMax) |
| 879 | { |
| 880 | const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service(); |
| 881 | if (aps == 0) return PERMISSION_DENIED; |
| 882 | return aps->initStreamVolume(stream, indexMin, indexMax); |
| 883 | } |
| 884 | |
Eric Laurent | 83844cc | 2011-11-18 16:43:31 -0800 | [diff] [blame] | 885 | status_t AudioSystem::setStreamVolumeIndex(audio_stream_type_t stream, |
| 886 | int index, |
| 887 | audio_devices_t device) |
Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 888 | { |
| 889 | const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service(); |
| 890 | if (aps == 0) return PERMISSION_DENIED; |
Eric Laurent | 83844cc | 2011-11-18 16:43:31 -0800 | [diff] [blame] | 891 | return aps->setStreamVolumeIndex(stream, index, device); |
Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 892 | } |
| 893 | |
Eric Laurent | 83844cc | 2011-11-18 16:43:31 -0800 | [diff] [blame] | 894 | status_t AudioSystem::getStreamVolumeIndex(audio_stream_type_t stream, |
| 895 | int *index, |
| 896 | audio_devices_t device) |
Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 897 | { |
| 898 | const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service(); |
| 899 | if (aps == 0) return PERMISSION_DENIED; |
Eric Laurent | 83844cc | 2011-11-18 16:43:31 -0800 | [diff] [blame] | 900 | return aps->getStreamVolumeIndex(stream, index, device); |
Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 901 | } |
| 902 | |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 903 | uint32_t AudioSystem::getStrategyForStream(audio_stream_type_t stream) |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 904 | { |
| 905 | const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service(); |
| 906 | if (aps == 0) return 0; |
| 907 | return aps->getStrategyForStream(stream); |
| 908 | } |
| 909 | |
Eric Laurent | 6374252 | 2012-03-08 13:42:42 -0800 | [diff] [blame] | 910 | audio_devices_t AudioSystem::getDevicesForStream(audio_stream_type_t stream) |
Glenn Kasten | 6b2718c | 2011-02-04 13:54:26 -0800 | [diff] [blame] | 911 | { |
| 912 | const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service(); |
Glenn Kasten | 45faf7e | 2014-01-17 10:23:01 -0800 | [diff] [blame] | 913 | if (aps == 0) return AUDIO_DEVICE_NONE; |
Glenn Kasten | 6b2718c | 2011-02-04 13:54:26 -0800 | [diff] [blame] | 914 | return aps->getDevicesForStream(stream); |
| 915 | } |
| 916 | |
Glenn Kasten | 58e5aa3 | 2012-06-20 14:08:14 -0700 | [diff] [blame] | 917 | audio_io_handle_t AudioSystem::getOutputForEffect(const effect_descriptor_t *desc) |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 918 | { |
| 919 | const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service(); |
Glenn Kasten | efa6ea9 | 2014-01-08 09:10:43 -0800 | [diff] [blame] | 920 | // FIXME change return type to status_t, and return PERMISSION_DENIED here |
Glenn Kasten | 142f519 | 2014-03-25 17:44:59 -0700 | [diff] [blame] | 921 | if (aps == 0) return AUDIO_IO_HANDLE_NONE; |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 922 | return aps->getOutputForEffect(desc); |
| 923 | } |
| 924 | |
Glenn Kasten | 58e5aa3 | 2012-06-20 14:08:14 -0700 | [diff] [blame] | 925 | status_t AudioSystem::registerEffect(const effect_descriptor_t *desc, |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 926 | audio_io_handle_t io, |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 927 | uint32_t strategy, |
Glenn Kasten | d848eb4 | 2016-03-08 13:42:11 -0800 | [diff] [blame] | 928 | audio_session_t session, |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 929 | int id) |
| 930 | { |
| 931 | const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service(); |
| 932 | if (aps == 0) return PERMISSION_DENIED; |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 933 | return aps->registerEffect(desc, io, strategy, session, id); |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 934 | } |
| 935 | |
| 936 | status_t AudioSystem::unregisterEffect(int id) |
| 937 | { |
| 938 | const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service(); |
| 939 | if (aps == 0) return PERMISSION_DENIED; |
| 940 | return aps->unregisterEffect(id); |
| 941 | } |
| 942 | |
Eric Laurent | db7c079 | 2011-08-10 10:37:50 -0700 | [diff] [blame] | 943 | status_t AudioSystem::setEffectEnabled(int id, bool enabled) |
| 944 | { |
| 945 | const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service(); |
| 946 | if (aps == 0) return PERMISSION_DENIED; |
| 947 | return aps->setEffectEnabled(id, enabled); |
| 948 | } |
| 949 | |
Glenn Kasten | fff6d71 | 2012-01-12 16:38:12 -0800 | [diff] [blame] | 950 | 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] | 951 | { |
Eric Laurent | eda6c36 | 2011-02-02 09:33:30 -0800 | [diff] [blame] | 952 | const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service(); |
| 953 | if (aps == 0) return PERMISSION_DENIED; |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 954 | if (state == NULL) return BAD_VALUE; |
Eric Laurent | eda6c36 | 2011-02-02 09:33:30 -0800 | [diff] [blame] | 955 | *state = aps->isStreamActive(stream, inPastMs); |
| 956 | return NO_ERROR; |
| 957 | } |
| 958 | |
Jean-Michel Trivi | 272ab54 | 2013-02-04 16:26:02 -0800 | [diff] [blame] | 959 | status_t AudioSystem::isStreamActiveRemotely(audio_stream_type_t stream, bool* state, |
| 960 | uint32_t inPastMs) |
| 961 | { |
| 962 | const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service(); |
| 963 | if (aps == 0) return PERMISSION_DENIED; |
| 964 | if (state == NULL) return BAD_VALUE; |
| 965 | *state = aps->isStreamActiveRemotely(stream, inPastMs); |
| 966 | return NO_ERROR; |
| 967 | } |
| 968 | |
Jean-Michel Trivi | d708603 | 2012-10-10 12:11:16 -0700 | [diff] [blame] | 969 | status_t AudioSystem::isSourceActive(audio_source_t stream, bool* state) |
| 970 | { |
| 971 | const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service(); |
| 972 | if (aps == 0) return PERMISSION_DENIED; |
| 973 | if (state == NULL) return BAD_VALUE; |
| 974 | *state = aps->isSourceActive(stream); |
| 975 | return NO_ERROR; |
| 976 | } |
| 977 | |
Glenn Kasten | 3b16c76 | 2012-11-14 08:44:39 -0800 | [diff] [blame] | 978 | uint32_t AudioSystem::getPrimaryOutputSamplingRate() |
Glenn Kasten | cc0f1cf | 2012-09-24 11:27:18 -0700 | [diff] [blame] | 979 | { |
| 980 | const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger(); |
| 981 | if (af == 0) return 0; |
| 982 | return af->getPrimaryOutputSamplingRate(); |
| 983 | } |
| 984 | |
Glenn Kasten | e33054e | 2012-11-14 12:54:39 -0800 | [diff] [blame] | 985 | size_t AudioSystem::getPrimaryOutputFrameCount() |
Glenn Kasten | cc0f1cf | 2012-09-24 11:27:18 -0700 | [diff] [blame] | 986 | { |
| 987 | const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger(); |
| 988 | if (af == 0) return 0; |
| 989 | return af->getPrimaryOutputFrameCount(); |
| 990 | } |
Eric Laurent | eda6c36 | 2011-02-02 09:33:30 -0800 | [diff] [blame] | 991 | |
Glenn Kasten | 4182c4e | 2013-07-15 14:45:07 -0700 | [diff] [blame] | 992 | status_t AudioSystem::setLowRamDevice(bool isLowRamDevice) |
| 993 | { |
| 994 | const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger(); |
| 995 | if (af == 0) return PERMISSION_DENIED; |
| 996 | return af->setLowRamDevice(isLowRamDevice); |
| 997 | } |
| 998 | |
Eric Laurent | 9f6530f | 2011-08-30 10:18:54 -0700 | [diff] [blame] | 999 | void AudioSystem::clearAudioConfigCache() |
| 1000 | { |
Glenn Kasten | d2d089f | 2014-11-05 11:48:12 -0800 | [diff] [blame] | 1001 | // called by restoreTrack_l(), which needs new IAudioFlinger and IAudioPolicyService instances |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 1002 | ALOGV("clearAudioConfigCache()"); |
Eric Laurent | f6778fd | 2014-11-18 17:26:58 -0800 | [diff] [blame] | 1003 | { |
| 1004 | Mutex::Autolock _l(gLock); |
Eric Laurent | 296fb13 | 2015-05-01 11:38:42 -0700 | [diff] [blame] | 1005 | if (gAudioFlingerClient != 0) { |
| 1006 | gAudioFlingerClient->clearIoCache(); |
| 1007 | } |
Glenn Kasten | d2d089f | 2014-11-05 11:48:12 -0800 | [diff] [blame] | 1008 | gAudioFlinger.clear(); |
| 1009 | } |
| 1010 | { |
| 1011 | Mutex::Autolock _l(gLockAPS); |
| 1012 | gAudioPolicyService.clear(); |
| 1013 | } |
Eric Laurent | 9f6530f | 2011-08-30 10:18:54 -0700 | [diff] [blame] | 1014 | } |
| 1015 | |
Richard Fitzgerald | ad3af33 | 2013-03-25 16:54:37 +0000 | [diff] [blame] | 1016 | bool AudioSystem::isOffloadSupported(const audio_offload_info_t& info) |
| 1017 | { |
| 1018 | ALOGV("isOffloadSupported()"); |
| 1019 | const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service(); |
| 1020 | if (aps == 0) return false; |
| 1021 | return aps->isOffloadSupported(info); |
| 1022 | } |
| 1023 | |
Eric Laurent | 203b1a1 | 2014-04-01 10:34:16 -0700 | [diff] [blame] | 1024 | status_t AudioSystem::listAudioPorts(audio_port_role_t role, |
| 1025 | audio_port_type_t type, |
| 1026 | unsigned int *num_ports, |
| 1027 | struct audio_port *ports, |
| 1028 | unsigned int *generation) |
| 1029 | { |
| 1030 | const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service(); |
| 1031 | if (aps == 0) return PERMISSION_DENIED; |
| 1032 | return aps->listAudioPorts(role, type, num_ports, ports, generation); |
| 1033 | } |
| 1034 | |
| 1035 | status_t AudioSystem::getAudioPort(struct audio_port *port) |
| 1036 | { |
| 1037 | const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service(); |
| 1038 | if (aps == 0) return PERMISSION_DENIED; |
| 1039 | return aps->getAudioPort(port); |
| 1040 | } |
| 1041 | |
| 1042 | status_t AudioSystem::createAudioPatch(const struct audio_patch *patch, |
| 1043 | audio_patch_handle_t *handle) |
| 1044 | { |
| 1045 | const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service(); |
| 1046 | if (aps == 0) return PERMISSION_DENIED; |
| 1047 | return aps->createAudioPatch(patch, handle); |
| 1048 | } |
| 1049 | |
| 1050 | status_t AudioSystem::releaseAudioPatch(audio_patch_handle_t handle) |
| 1051 | { |
| 1052 | const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service(); |
| 1053 | if (aps == 0) return PERMISSION_DENIED; |
| 1054 | return aps->releaseAudioPatch(handle); |
| 1055 | } |
| 1056 | |
| 1057 | status_t AudioSystem::listAudioPatches(unsigned int *num_patches, |
| 1058 | struct audio_patch *patches, |
| 1059 | unsigned int *generation) |
| 1060 | { |
| 1061 | const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service(); |
| 1062 | if (aps == 0) return PERMISSION_DENIED; |
| 1063 | return aps->listAudioPatches(num_patches, patches, generation); |
| 1064 | } |
| 1065 | |
| 1066 | status_t AudioSystem::setAudioPortConfig(const struct audio_port_config *config) |
| 1067 | { |
| 1068 | const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service(); |
| 1069 | if (aps == 0) return PERMISSION_DENIED; |
| 1070 | return aps->setAudioPortConfig(config); |
| 1071 | } |
| 1072 | |
Eric Laurent | 296fb13 | 2015-05-01 11:38:42 -0700 | [diff] [blame] | 1073 | status_t AudioSystem::addAudioPortCallback(const sp<AudioPortCallback>& callback) |
Eric Laurent | b52c152 | 2014-05-20 11:27:36 -0700 | [diff] [blame] | 1074 | { |
Eric Laurent | b28753e | 2015-04-01 13:06:28 -0700 | [diff] [blame] | 1075 | const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service(); |
| 1076 | if (aps == 0) return PERMISSION_DENIED; |
| 1077 | |
| 1078 | Mutex::Autolock _l(gLockAPS); |
| 1079 | if (gAudioPolicyServiceClient == 0) { |
| 1080 | return NO_INIT; |
| 1081 | } |
Eric Laurent | e8726fe | 2015-06-26 09:39:24 -0700 | [diff] [blame] | 1082 | int ret = gAudioPolicyServiceClient->addAudioPortCallback(callback); |
| 1083 | if (ret == 1) { |
| 1084 | aps->setAudioPortCallbacksEnabled(true); |
| 1085 | } |
| 1086 | return (ret < 0) ? INVALID_OPERATION : NO_ERROR; |
Eric Laurent | b52c152 | 2014-05-20 11:27:36 -0700 | [diff] [blame] | 1087 | } |
| 1088 | |
Jean-Michel Trivi | f613d42 | 2015-04-23 18:41:29 -0700 | [diff] [blame] | 1089 | /*static*/ |
Eric Laurent | 296fb13 | 2015-05-01 11:38:42 -0700 | [diff] [blame] | 1090 | status_t AudioSystem::removeAudioPortCallback(const sp<AudioPortCallback>& callback) |
Eric Laurent | b28753e | 2015-04-01 13:06:28 -0700 | [diff] [blame] | 1091 | { |
| 1092 | const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service(); |
| 1093 | if (aps == 0) return PERMISSION_DENIED; |
| 1094 | |
| 1095 | Mutex::Autolock _l(gLockAPS); |
| 1096 | if (gAudioPolicyServiceClient == 0) { |
| 1097 | return NO_INIT; |
| 1098 | } |
Eric Laurent | e8726fe | 2015-06-26 09:39:24 -0700 | [diff] [blame] | 1099 | int ret = gAudioPolicyServiceClient->removeAudioPortCallback(callback); |
| 1100 | if (ret == 0) { |
| 1101 | aps->setAudioPortCallbacksEnabled(false); |
| 1102 | } |
| 1103 | return (ret < 0) ? INVALID_OPERATION : NO_ERROR; |
Eric Laurent | 296fb13 | 2015-05-01 11:38:42 -0700 | [diff] [blame] | 1104 | } |
| 1105 | |
| 1106 | status_t AudioSystem::addAudioDeviceCallback( |
| 1107 | const sp<AudioDeviceCallback>& callback, audio_io_handle_t audioIo) |
| 1108 | { |
| 1109 | const sp<AudioFlingerClient> afc = getAudioFlingerClient(); |
| 1110 | if (afc == 0) { |
| 1111 | return NO_INIT; |
| 1112 | } |
Eric Laurent | 7c1ec5f | 2015-07-09 14:52:47 -0700 | [diff] [blame] | 1113 | status_t status = afc->addAudioDeviceCallback(callback, audioIo); |
| 1114 | if (status == NO_ERROR) { |
| 1115 | const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger(); |
| 1116 | if (af != 0) { |
| 1117 | af->registerClient(afc); |
| 1118 | } |
| 1119 | } |
| 1120 | return status; |
Eric Laurent | 296fb13 | 2015-05-01 11:38:42 -0700 | [diff] [blame] | 1121 | } |
| 1122 | |
| 1123 | status_t AudioSystem::removeAudioDeviceCallback( |
| 1124 | const sp<AudioDeviceCallback>& callback, audio_io_handle_t audioIo) |
| 1125 | { |
| 1126 | const sp<AudioFlingerClient> afc = getAudioFlingerClient(); |
| 1127 | if (afc == 0) { |
| 1128 | return NO_INIT; |
| 1129 | } |
| 1130 | return afc->removeAudioDeviceCallback(callback, audioIo); |
| 1131 | } |
| 1132 | |
| 1133 | audio_port_handle_t AudioSystem::getDeviceIdForIo(audio_io_handle_t audioIo) |
| 1134 | { |
| 1135 | const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger(); |
| 1136 | if (af == 0) return PERMISSION_DENIED; |
| 1137 | const sp<AudioIoDescriptor> desc = getIoDescriptor(audioIo); |
| 1138 | if (desc == 0) { |
| 1139 | return AUDIO_PORT_HANDLE_NONE; |
| 1140 | } |
| 1141 | return desc->getDeviceId(); |
Eric Laurent | b28753e | 2015-04-01 13:06:28 -0700 | [diff] [blame] | 1142 | } |
| 1143 | |
Eric Laurent | df3dc7e | 2014-07-27 18:39:40 -0700 | [diff] [blame] | 1144 | status_t AudioSystem::acquireSoundTriggerSession(audio_session_t *session, |
| 1145 | audio_io_handle_t *ioHandle, |
| 1146 | audio_devices_t *device) |
| 1147 | { |
| 1148 | const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service(); |
| 1149 | if (aps == 0) return PERMISSION_DENIED; |
| 1150 | return aps->acquireSoundTriggerSession(session, ioHandle, device); |
| 1151 | } |
| 1152 | |
| 1153 | status_t AudioSystem::releaseSoundTriggerSession(audio_session_t session) |
| 1154 | { |
| 1155 | const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service(); |
| 1156 | if (aps == 0) return PERMISSION_DENIED; |
| 1157 | return aps->releaseSoundTriggerSession(session); |
| 1158 | } |
Eric Laurent | bb6c9a0 | 2014-09-25 14:11:47 -0700 | [diff] [blame] | 1159 | |
| 1160 | audio_mode_t AudioSystem::getPhoneState() |
| 1161 | { |
| 1162 | const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service(); |
| 1163 | if (aps == 0) return AUDIO_MODE_INVALID; |
| 1164 | return aps->getPhoneState(); |
| 1165 | } |
| 1166 | |
Eric Laurent | baac183 | 2014-12-01 17:52:59 -0800 | [diff] [blame] | 1167 | status_t AudioSystem::registerPolicyMixes(Vector<AudioMix> mixes, bool registration) |
| 1168 | { |
| 1169 | const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service(); |
| 1170 | if (aps == 0) return PERMISSION_DENIED; |
| 1171 | return aps->registerPolicyMixes(mixes, registration); |
| 1172 | } |
Eric Laurent | bb6c9a0 | 2014-09-25 14:11:47 -0700 | [diff] [blame] | 1173 | |
Eric Laurent | 554a277 | 2015-04-10 11:29:24 -0700 | [diff] [blame] | 1174 | status_t AudioSystem::startAudioSource(const struct audio_port_config *source, |
| 1175 | const audio_attributes_t *attributes, |
| 1176 | audio_io_handle_t *handle) |
| 1177 | { |
| 1178 | const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service(); |
| 1179 | if (aps == 0) return PERMISSION_DENIED; |
| 1180 | return aps->startAudioSource(source, attributes, handle); |
| 1181 | } |
| 1182 | |
| 1183 | status_t AudioSystem::stopAudioSource(audio_io_handle_t handle) |
| 1184 | { |
| 1185 | const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service(); |
| 1186 | if (aps == 0) return PERMISSION_DENIED; |
| 1187 | return aps->stopAudioSource(handle); |
| 1188 | } |
| 1189 | |
Andy Hung | 2ddee19 | 2015-12-18 17:34:44 -0800 | [diff] [blame] | 1190 | status_t AudioSystem::setMasterMono(bool mono) |
| 1191 | { |
| 1192 | const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service(); |
| 1193 | if (aps == 0) return PERMISSION_DENIED; |
| 1194 | return aps->setMasterMono(mono); |
| 1195 | } |
| 1196 | |
| 1197 | status_t AudioSystem::getMasterMono(bool *mono) |
| 1198 | { |
| 1199 | const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service(); |
| 1200 | if (aps == 0) return PERMISSION_DENIED; |
| 1201 | return aps->getMasterMono(mono); |
| 1202 | } |
| 1203 | |
Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 1204 | // --------------------------------------------------------------------------- |
| 1205 | |
Eric Laurent | e8726fe | 2015-06-26 09:39:24 -0700 | [diff] [blame] | 1206 | int AudioSystem::AudioPolicyServiceClient::addAudioPortCallback( |
Eric Laurent | 296fb13 | 2015-05-01 11:38:42 -0700 | [diff] [blame] | 1207 | const sp<AudioPortCallback>& callback) |
Eric Laurent | b28753e | 2015-04-01 13:06:28 -0700 | [diff] [blame] | 1208 | { |
| 1209 | Mutex::Autolock _l(mLock); |
| 1210 | for (size_t i = 0; i < mAudioPortCallbacks.size(); i++) { |
Eric Laurent | 296fb13 | 2015-05-01 11:38:42 -0700 | [diff] [blame] | 1211 | if (mAudioPortCallbacks[i] == callback) { |
Eric Laurent | e8726fe | 2015-06-26 09:39:24 -0700 | [diff] [blame] | 1212 | return -1; |
Eric Laurent | b28753e | 2015-04-01 13:06:28 -0700 | [diff] [blame] | 1213 | } |
| 1214 | } |
Eric Laurent | 296fb13 | 2015-05-01 11:38:42 -0700 | [diff] [blame] | 1215 | mAudioPortCallbacks.add(callback); |
Eric Laurent | e8726fe | 2015-06-26 09:39:24 -0700 | [diff] [blame] | 1216 | return mAudioPortCallbacks.size(); |
Eric Laurent | b28753e | 2015-04-01 13:06:28 -0700 | [diff] [blame] | 1217 | } |
| 1218 | |
Eric Laurent | e8726fe | 2015-06-26 09:39:24 -0700 | [diff] [blame] | 1219 | int AudioSystem::AudioPolicyServiceClient::removeAudioPortCallback( |
Eric Laurent | 296fb13 | 2015-05-01 11:38:42 -0700 | [diff] [blame] | 1220 | const sp<AudioPortCallback>& callback) |
Eric Laurent | b28753e | 2015-04-01 13:06:28 -0700 | [diff] [blame] | 1221 | { |
| 1222 | Mutex::Autolock _l(mLock); |
| 1223 | size_t i; |
| 1224 | for (i = 0; i < mAudioPortCallbacks.size(); i++) { |
Eric Laurent | 296fb13 | 2015-05-01 11:38:42 -0700 | [diff] [blame] | 1225 | if (mAudioPortCallbacks[i] == callback) { |
Eric Laurent | b28753e | 2015-04-01 13:06:28 -0700 | [diff] [blame] | 1226 | break; |
| 1227 | } |
| 1228 | } |
| 1229 | if (i == mAudioPortCallbacks.size()) { |
Eric Laurent | e8726fe | 2015-06-26 09:39:24 -0700 | [diff] [blame] | 1230 | return -1; |
Eric Laurent | b28753e | 2015-04-01 13:06:28 -0700 | [diff] [blame] | 1231 | } |
| 1232 | mAudioPortCallbacks.removeAt(i); |
Eric Laurent | e8726fe | 2015-06-26 09:39:24 -0700 | [diff] [blame] | 1233 | return mAudioPortCallbacks.size(); |
Eric Laurent | b28753e | 2015-04-01 13:06:28 -0700 | [diff] [blame] | 1234 | } |
| 1235 | |
Eric Laurent | 296fb13 | 2015-05-01 11:38:42 -0700 | [diff] [blame] | 1236 | |
Eric Laurent | b28753e | 2015-04-01 13:06:28 -0700 | [diff] [blame] | 1237 | void AudioSystem::AudioPolicyServiceClient::onAudioPortListUpdate() |
| 1238 | { |
| 1239 | Mutex::Autolock _l(mLock); |
| 1240 | for (size_t i = 0; i < mAudioPortCallbacks.size(); i++) { |
| 1241 | mAudioPortCallbacks[i]->onAudioPortListUpdate(); |
| 1242 | } |
| 1243 | } |
| 1244 | |
| 1245 | void AudioSystem::AudioPolicyServiceClient::onAudioPatchListUpdate() |
| 1246 | { |
| 1247 | Mutex::Autolock _l(mLock); |
| 1248 | for (size_t i = 0; i < mAudioPortCallbacks.size(); i++) { |
| 1249 | mAudioPortCallbacks[i]->onAudioPatchListUpdate(); |
| 1250 | } |
| 1251 | } |
| 1252 | |
Jean-Michel Trivi | de80105 | 2015-04-14 19:10:14 -0700 | [diff] [blame] | 1253 | void AudioSystem::AudioPolicyServiceClient::onDynamicPolicyMixStateUpdate( |
| 1254 | String8 regId, int32_t state) |
| 1255 | { |
Jean-Michel Trivi | f613d42 | 2015-04-23 18:41:29 -0700 | [diff] [blame] | 1256 | ALOGV("AudioPolicyServiceClient::onDynamicPolicyMixStateUpdate(%s, %d)", regId.string(), state); |
| 1257 | dynamic_policy_callback cb = NULL; |
| 1258 | { |
| 1259 | Mutex::Autolock _l(AudioSystem::gLock); |
| 1260 | cb = gDynPolicyCallback; |
| 1261 | } |
| 1262 | |
| 1263 | if (cb != NULL) { |
| 1264 | cb(DYNAMIC_POLICY_EVENT_MIX_STATE_UPDATE, regId, state); |
| 1265 | } |
Jean-Michel Trivi | de80105 | 2015-04-14 19:10:14 -0700 | [diff] [blame] | 1266 | } |
| 1267 | |
Jean-Michel Trivi | 2f4fe9f | 2015-12-04 16:20:59 -0800 | [diff] [blame] | 1268 | void AudioSystem::AudioPolicyServiceClient::onRecordingConfigurationUpdate( |
Jean-Michel Trivi | 7281aa9 | 2016-02-17 15:33:40 -0800 | [diff] [blame] | 1269 | int event, audio_session_t session, audio_source_t source, |
Jean-Michel Trivi | 8c7cf3b | 2016-02-25 17:08:24 -0800 | [diff] [blame] | 1270 | const audio_config_base_t *clientConfig, const audio_config_base_t *deviceConfig, |
| 1271 | audio_patch_handle_t patchHandle) { |
Jean-Michel Trivi | 2f4fe9f | 2015-12-04 16:20:59 -0800 | [diff] [blame] | 1272 | record_config_callback cb = NULL; |
| 1273 | { |
| 1274 | Mutex::Autolock _l(AudioSystem::gLock); |
| 1275 | cb = gRecordConfigCallback; |
| 1276 | } |
| 1277 | |
| 1278 | if (cb != NULL) { |
Jean-Michel Trivi | 8c7cf3b | 2016-02-25 17:08:24 -0800 | [diff] [blame] | 1279 | cb(event, session, source, clientConfig, deviceConfig, patchHandle); |
Jean-Michel Trivi | 2f4fe9f | 2015-12-04 16:20:59 -0800 | [diff] [blame] | 1280 | } |
| 1281 | } |
| 1282 | |
Glenn Kasten | 4944acb | 2013-08-19 08:39:20 -0700 | [diff] [blame] | 1283 | void AudioSystem::AudioPolicyServiceClient::binderDied(const wp<IBinder>& who __unused) |
| 1284 | { |
Glenn Kasten | d2d089f | 2014-11-05 11:48:12 -0800 | [diff] [blame] | 1285 | { |
Eric Laurent | b28753e | 2015-04-01 13:06:28 -0700 | [diff] [blame] | 1286 | Mutex::Autolock _l(mLock); |
| 1287 | for (size_t i = 0; i < mAudioPortCallbacks.size(); i++) { |
| 1288 | mAudioPortCallbacks[i]->onServiceDied(); |
Glenn Kasten | d2d089f | 2014-11-05 11:48:12 -0800 | [diff] [blame] | 1289 | } |
Eric Laurent | b52c152 | 2014-05-20 11:27:36 -0700 | [diff] [blame] | 1290 | } |
Glenn Kasten | d2d089f | 2014-11-05 11:48:12 -0800 | [diff] [blame] | 1291 | { |
| 1292 | Mutex::Autolock _l(gLockAPS); |
| 1293 | AudioSystem::gAudioPolicyService.clear(); |
| 1294 | } |
Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 1295 | |
Steve Block | 5ff1dd5 | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 1296 | ALOGW("AudioPolicyService server died!"); |
Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 1297 | } |
| 1298 | |
Glenn Kasten | 40bc906 | 2015-03-20 09:09:33 -0700 | [diff] [blame] | 1299 | } // namespace android |