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> |
Eric Laurent | fb00fc7 | 2017-05-25 18:17:12 -0700 | [diff] [blame] | 22 | #include <binder/ProcessState.h> |
François Gaffie | 2443760 | 2018-04-23 15:08:59 +0200 | [diff] [blame] | 23 | #include <binder/IPCThreadState.h> |
Eric Laurent | 21da647 | 2017-11-09 16:29:26 -0800 | [diff] [blame] | 24 | #include <media/AudioResamplerPublic.h> |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 25 | #include <media/AudioSystem.h> |
Glenn Kasten | 1ab85ec | 2013-05-31 09:18:43 -0700 | [diff] [blame] | 26 | #include <media/IAudioFlinger.h> |
Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 27 | #include <media/IAudioPolicyService.h> |
François Gaffie | d0ba9ed | 2018-11-05 11:50:42 +0100 | [diff] [blame] | 28 | #include <media/TypeConverter.h> |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 29 | #include <math.h> |
| 30 | |
Dima Zavin | 6476024 | 2011-05-11 14:15:23 -0700 | [diff] [blame] | 31 | #include <system/audio.h> |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 32 | |
Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 33 | // ---------------------------------------------------------------------------- |
Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 34 | |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 35 | namespace android { |
| 36 | |
| 37 | // client singleton for AudioFlinger binder interface |
| 38 | Mutex AudioSystem::gLock; |
Glenn Kasten | d2d089f | 2014-11-05 11:48:12 -0800 | [diff] [blame] | 39 | Mutex AudioSystem::gLockAPS; |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 40 | sp<IAudioFlinger> AudioSystem::gAudioFlinger; |
| 41 | sp<AudioSystem::AudioFlingerClient> AudioSystem::gAudioFlingerClient; |
| 42 | audio_error_callback AudioSystem::gAudioErrorCallback = NULL; |
Jean-Michel Trivi | f613d42 | 2015-04-23 18:41:29 -0700 | [diff] [blame] | 43 | dynamic_policy_callback AudioSystem::gDynPolicyCallback = NULL; |
Svet Ganov | f4ddfef | 2018-01-16 07:37:58 -0800 | [diff] [blame] | 44 | record_config_callback AudioSystem::gRecordConfigCallback = NULL; |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 45 | |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 46 | // establish binder interface to AudioFlinger service |
Eric Laurent | 0ebd5f9 | 2014-11-19 19:04:52 -0800 | [diff] [blame] | 47 | const sp<IAudioFlinger> AudioSystem::get_audio_flinger() |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 48 | { |
Eric Laurent | 0ebd5f9 | 2014-11-19 19:04:52 -0800 | [diff] [blame] | 49 | sp<IAudioFlinger> af; |
| 50 | sp<AudioFlingerClient> afc; |
| 51 | { |
| 52 | Mutex::Autolock _l(gLock); |
| 53 | if (gAudioFlinger == 0) { |
| 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; |
| 60 | ALOGW("AudioFlinger not published, waiting..."); |
| 61 | usleep(500000); // 0.5 s |
| 62 | } while (true); |
| 63 | if (gAudioFlingerClient == NULL) { |
| 64 | gAudioFlingerClient = new AudioFlingerClient(); |
| 65 | } else { |
| 66 | if (gAudioErrorCallback) { |
| 67 | gAudioErrorCallback(NO_ERROR); |
| 68 | } |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 69 | } |
Eric Laurent | 0ebd5f9 | 2014-11-19 19:04:52 -0800 | [diff] [blame] | 70 | binder->linkToDeath(gAudioFlingerClient); |
| 71 | gAudioFlinger = interface_cast<IAudioFlinger>(binder); |
| 72 | LOG_ALWAYS_FATAL_IF(gAudioFlinger == 0); |
| 73 | afc = gAudioFlingerClient; |
Eric Laurent | fb00fc7 | 2017-05-25 18:17:12 -0700 | [diff] [blame] | 74 | // Make sure callbacks can be received by gAudioFlingerClient |
| 75 | ProcessState::self()->startThreadPool(); |
Glenn Kasten | e53b9ea | 2012-03-12 16:29:55 -0700 | [diff] [blame] | 76 | } |
Eric Laurent | 0ebd5f9 | 2014-11-19 19:04:52 -0800 | [diff] [blame] | 77 | af = gAudioFlinger; |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 78 | } |
Eric Laurent | 0ebd5f9 | 2014-11-19 19:04:52 -0800 | [diff] [blame] | 79 | if (afc != 0) { |
François Gaffie | 2443760 | 2018-04-23 15:08:59 +0200 | [diff] [blame] | 80 | int64_t token = IPCThreadState::self()->clearCallingIdentity(); |
Eric Laurent | 0ebd5f9 | 2014-11-19 19:04:52 -0800 | [diff] [blame] | 81 | af->registerClient(afc); |
François Gaffie | 2443760 | 2018-04-23 15:08:59 +0200 | [diff] [blame] | 82 | IPCThreadState::self()->restoreCallingIdentity(token); |
Eric Laurent | 0ebd5f9 | 2014-11-19 19:04:52 -0800 | [diff] [blame] | 83 | } |
| 84 | return af; |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 85 | } |
| 86 | |
Eric Laurent | 296fb13 | 2015-05-01 11:38:42 -0700 | [diff] [blame] | 87 | const sp<AudioSystem::AudioFlingerClient> AudioSystem::getAudioFlingerClient() |
| 88 | { |
| 89 | // calling get_audio_flinger() will initialize gAudioFlingerClient if needed |
| 90 | const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger(); |
| 91 | if (af == 0) return 0; |
| 92 | Mutex::Autolock _l(gLock); |
| 93 | return gAudioFlingerClient; |
| 94 | } |
| 95 | |
| 96 | sp<AudioIoDescriptor> AudioSystem::getIoDescriptor(audio_io_handle_t ioHandle) |
| 97 | { |
| 98 | sp<AudioIoDescriptor> desc; |
| 99 | const sp<AudioFlingerClient> afc = getAudioFlingerClient(); |
| 100 | if (afc != 0) { |
| 101 | desc = afc->getIoDescriptor(ioHandle); |
| 102 | } |
| 103 | return desc; |
| 104 | } |
| 105 | |
Eric Laurent | 4629161 | 2013-07-18 14:38:44 -0700 | [diff] [blame] | 106 | /* static */ status_t AudioSystem::checkAudioFlinger() |
| 107 | { |
| 108 | if (defaultServiceManager()->checkService(String16("media.audio_flinger")) != 0) { |
| 109 | return NO_ERROR; |
| 110 | } |
| 111 | return DEAD_OBJECT; |
| 112 | } |
| 113 | |
Glenn Kasten | 4a8308b | 2016-04-18 14:10:01 -0700 | [diff] [blame] | 114 | // FIXME Declare in binder opcode order, similarly to IAudioFlinger.h and IAudioFlinger.cpp |
| 115 | |
Glenn Kasten | 4944acb | 2013-08-19 08:39:20 -0700 | [diff] [blame] | 116 | status_t AudioSystem::muteMicrophone(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 | return af->setMicMute(state); |
| 121 | } |
| 122 | |
Glenn Kasten | 4944acb | 2013-08-19 08:39:20 -0700 | [diff] [blame] | 123 | status_t AudioSystem::isMicrophoneMuted(bool* state) |
| 124 | { |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 125 | const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger(); |
| 126 | if (af == 0) return PERMISSION_DENIED; |
| 127 | *state = af->getMicMute(); |
| 128 | return NO_ERROR; |
| 129 | } |
| 130 | |
| 131 | status_t AudioSystem::setMasterVolume(float value) |
| 132 | { |
| 133 | const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger(); |
| 134 | if (af == 0) return PERMISSION_DENIED; |
| 135 | af->setMasterVolume(value); |
| 136 | return NO_ERROR; |
| 137 | } |
| 138 | |
| 139 | status_t AudioSystem::setMasterMute(bool mute) |
| 140 | { |
| 141 | const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger(); |
| 142 | if (af == 0) return PERMISSION_DENIED; |
| 143 | af->setMasterMute(mute); |
| 144 | return NO_ERROR; |
| 145 | } |
| 146 | |
| 147 | status_t AudioSystem::getMasterVolume(float* volume) |
| 148 | { |
| 149 | const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger(); |
| 150 | if (af == 0) return PERMISSION_DENIED; |
| 151 | *volume = af->masterVolume(); |
| 152 | return NO_ERROR; |
| 153 | } |
| 154 | |
| 155 | status_t AudioSystem::getMasterMute(bool* mute) |
| 156 | { |
| 157 | const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger(); |
| 158 | if (af == 0) return PERMISSION_DENIED; |
| 159 | *mute = af->masterMute(); |
| 160 | return NO_ERROR; |
| 161 | } |
| 162 | |
Glenn Kasten | 72ef00d | 2012-01-17 11:09:42 -0800 | [diff] [blame] | 163 | status_t AudioSystem::setStreamVolume(audio_stream_type_t stream, float value, |
| 164 | audio_io_handle_t output) |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 165 | { |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 166 | 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] | 167 | const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger(); |
| 168 | if (af == 0) return PERMISSION_DENIED; |
Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 169 | af->setStreamVolume(stream, value, output); |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 170 | return NO_ERROR; |
| 171 | } |
| 172 | |
Glenn Kasten | fff6d71 | 2012-01-12 16:38:12 -0800 | [diff] [blame] | 173 | 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] | 174 | { |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 175 | 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] | 176 | const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger(); |
| 177 | if (af == 0) return PERMISSION_DENIED; |
| 178 | af->setStreamMute(stream, mute); |
| 179 | return NO_ERROR; |
| 180 | } |
| 181 | |
Glenn Kasten | 72ef00d | 2012-01-17 11:09:42 -0800 | [diff] [blame] | 182 | status_t AudioSystem::getStreamVolume(audio_stream_type_t stream, float* volume, |
| 183 | audio_io_handle_t output) |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 184 | { |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 185 | 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] | 186 | const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger(); |
| 187 | if (af == 0) return PERMISSION_DENIED; |
Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 188 | *volume = af->streamVolume(stream, output); |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 189 | return NO_ERROR; |
| 190 | } |
| 191 | |
Glenn Kasten | fff6d71 | 2012-01-12 16:38:12 -0800 | [diff] [blame] | 192 | 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] | 193 | { |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 194 | 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] | 195 | const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger(); |
| 196 | if (af == 0) return PERMISSION_DENIED; |
| 197 | *mute = af->streamMute(stream); |
| 198 | return NO_ERROR; |
| 199 | } |
| 200 | |
Glenn Kasten | f78aee7 | 2012-01-04 11:00:47 -0800 | [diff] [blame] | 201 | status_t AudioSystem::setMode(audio_mode_t mode) |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 202 | { |
Glenn Kasten | 930f4ca | 2012-01-06 16:47:31 -0800 | [diff] [blame] | 203 | 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] | 204 | const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger(); |
| 205 | if (af == 0) return PERMISSION_DENIED; |
| 206 | return af->setMode(mode); |
| 207 | } |
| 208 | |
Glenn Kasten | 4944acb | 2013-08-19 08:39:20 -0700 | [diff] [blame] | 209 | status_t AudioSystem::setParameters(audio_io_handle_t ioHandle, const String8& keyValuePairs) |
| 210 | { |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 211 | const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger(); |
| 212 | if (af == 0) return PERMISSION_DENIED; |
Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 213 | return af->setParameters(ioHandle, keyValuePairs); |
| 214 | } |
| 215 | |
Glenn Kasten | 4944acb | 2013-08-19 08:39:20 -0700 | [diff] [blame] | 216 | String8 AudioSystem::getParameters(audio_io_handle_t ioHandle, const String8& keys) |
| 217 | { |
Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 218 | const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger(); |
| 219 | String8 result = String8(""); |
| 220 | if (af == 0) return result; |
| 221 | |
| 222 | result = af->getParameters(ioHandle, keys); |
| 223 | return result; |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 224 | } |
| 225 | |
Glenn Kasten | c23885e | 2013-12-19 16:35:18 -0800 | [diff] [blame] | 226 | status_t AudioSystem::setParameters(const String8& keyValuePairs) |
| 227 | { |
Glenn Kasten | 142f519 | 2014-03-25 17:44:59 -0700 | [diff] [blame] | 228 | return setParameters(AUDIO_IO_HANDLE_NONE, keyValuePairs); |
Glenn Kasten | c23885e | 2013-12-19 16:35:18 -0800 | [diff] [blame] | 229 | } |
| 230 | |
| 231 | String8 AudioSystem::getParameters(const String8& keys) |
| 232 | { |
Glenn Kasten | 142f519 | 2014-03-25 17:44:59 -0700 | [diff] [blame] | 233 | return getParameters(AUDIO_IO_HANDLE_NONE, keys); |
Glenn Kasten | c23885e | 2013-12-19 16:35:18 -0800 | [diff] [blame] | 234 | } |
| 235 | |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 236 | // convert volume steps to natural log scale |
| 237 | |
| 238 | // change this value to change volume scaling |
| 239 | static const float dBPerStep = 0.5f; |
| 240 | // shouldn't need to touch these |
| 241 | static const float dBConvert = -dBPerStep * 2.302585093f / 20.0f; |
| 242 | static const float dBConvertInverse = 1.0f / dBConvert; |
| 243 | |
| 244 | float AudioSystem::linearToLog(int volume) |
| 245 | { |
| 246 | // float v = volume ? exp(float(100 - volume) * dBConvert) : 0; |
Steve Block | b8a8052 | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 247 | // ALOGD("linearToLog(%d)=%f", volume, v); |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 248 | // return v; |
| 249 | return volume ? exp(float(100 - volume) * dBConvert) : 0; |
| 250 | } |
| 251 | |
| 252 | int AudioSystem::logToLinear(float volume) |
| 253 | { |
| 254 | // int v = volume ? 100 - int(dBConvertInverse * log(volume) + 0.5) : 0; |
Steve Block | b8a8052 | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 255 | // ALOGD("logTolinear(%d)=%f", v, volume); |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 256 | // return v; |
| 257 | return volume ? 100 - int(dBConvertInverse * log(volume) + 0.5) : 0; |
| 258 | } |
| 259 | |
Eric Laurent | 21da647 | 2017-11-09 16:29:26 -0800 | [diff] [blame] | 260 | /* static */ size_t AudioSystem::calculateMinFrameCount( |
| 261 | uint32_t afLatencyMs, uint32_t afFrameCount, uint32_t afSampleRate, |
| 262 | uint32_t sampleRate, float speed /*, uint32_t notificationsPerBufferReq*/) |
| 263 | { |
| 264 | // Ensure that buffer depth covers at least audio hardware latency |
| 265 | uint32_t minBufCount = afLatencyMs / ((1000 * afFrameCount) / afSampleRate); |
| 266 | if (minBufCount < 2) { |
| 267 | minBufCount = 2; |
| 268 | } |
| 269 | #if 0 |
| 270 | // The notificationsPerBufferReq parameter is not yet used for non-fast tracks, |
| 271 | // but keeping the code here to make it easier to add later. |
| 272 | if (minBufCount < notificationsPerBufferReq) { |
| 273 | minBufCount = notificationsPerBufferReq; |
| 274 | } |
| 275 | #endif |
| 276 | ALOGV("calculateMinFrameCount afLatency %u afFrameCount %u afSampleRate %u " |
| 277 | "sampleRate %u speed %f minBufCount: %u" /*" notificationsPerBufferReq %u"*/, |
| 278 | afLatencyMs, afFrameCount, afSampleRate, sampleRate, speed, minBufCount |
| 279 | /*, notificationsPerBufferReq*/); |
| 280 | return minBufCount * sourceFramesNeededWithTimestretch( |
| 281 | sampleRate, afFrameCount, afSampleRate, speed); |
| 282 | } |
| 283 | |
| 284 | |
Glenn Kasten | 3b16c76 | 2012-11-14 08:44:39 -0800 | [diff] [blame] | 285 | 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] | 286 | { |
Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 287 | audio_io_handle_t output; |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 288 | |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 289 | if (streamType == AUDIO_STREAM_DEFAULT) { |
| 290 | streamType = AUDIO_STREAM_MUSIC; |
Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 291 | } |
| 292 | |
Glenn Kasten | fff6d71 | 2012-01-12 16:38:12 -0800 | [diff] [blame] | 293 | output = getOutput(streamType); |
Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 294 | if (output == 0) { |
| 295 | return PERMISSION_DENIED; |
| 296 | } |
| 297 | |
Jean-Michel Trivi | b7f24b1 | 2014-06-11 10:05:30 -0700 | [diff] [blame] | 298 | return getSamplingRate(output, samplingRate); |
Eric Laurent | 1a9ed11 | 2012-03-20 18:36:01 -0700 | [diff] [blame] | 299 | } |
| 300 | |
Glenn Kasten | 2c073da | 2016-02-26 09:14:08 -0800 | [diff] [blame] | 301 | status_t AudioSystem::getSamplingRate(audio_io_handle_t ioHandle, |
Glenn Kasten | 3b16c76 | 2012-11-14 08:44:39 -0800 | [diff] [blame] | 302 | uint32_t* samplingRate) |
Eric Laurent | 1a9ed11 | 2012-03-20 18:36:01 -0700 | [diff] [blame] | 303 | { |
Eric Laurent | f6778fd | 2014-11-18 17:26:58 -0800 | [diff] [blame] | 304 | const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger(); |
| 305 | if (af == 0) return PERMISSION_DENIED; |
Glenn Kasten | 2c073da | 2016-02-26 09:14:08 -0800 | [diff] [blame] | 306 | sp<AudioIoDescriptor> desc = getIoDescriptor(ioHandle); |
| 307 | if (desc == 0) { |
| 308 | *samplingRate = af->sampleRate(ioHandle); |
Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 309 | } else { |
Glenn Kasten | 2c073da | 2016-02-26 09:14:08 -0800 | [diff] [blame] | 310 | *samplingRate = desc->mSamplingRate; |
Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 311 | } |
Glenn Kasten | f94006c | 2014-01-08 08:56:06 -0800 | [diff] [blame] | 312 | if (*samplingRate == 0) { |
Glenn Kasten | 2c073da | 2016-02-26 09:14:08 -0800 | [diff] [blame] | 313 | ALOGE("AudioSystem::getSamplingRate failed for ioHandle %d", ioHandle); |
Glenn Kasten | f94006c | 2014-01-08 08:56:06 -0800 | [diff] [blame] | 314 | return BAD_VALUE; |
| 315 | } |
Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 316 | |
Glenn Kasten | 2c073da | 2016-02-26 09:14:08 -0800 | [diff] [blame] | 317 | ALOGV("getSamplingRate() ioHandle %d, sampling rate %u", ioHandle, *samplingRate); |
Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 318 | |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 319 | return NO_ERROR; |
| 320 | } |
| 321 | |
Glenn Kasten | e33054e | 2012-11-14 12:54:39 -0800 | [diff] [blame] | 322 | 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] | 323 | { |
Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 324 | audio_io_handle_t output; |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 325 | |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 326 | if (streamType == AUDIO_STREAM_DEFAULT) { |
| 327 | streamType = AUDIO_STREAM_MUSIC; |
Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 328 | } |
Eric Laurent | 48f7f5e | 2009-04-02 09:32:43 -0700 | [diff] [blame] | 329 | |
Glenn Kasten | fff6d71 | 2012-01-12 16:38:12 -0800 | [diff] [blame] | 330 | output = getOutput(streamType); |
Glenn Kasten | 142f519 | 2014-03-25 17:44:59 -0700 | [diff] [blame] | 331 | if (output == AUDIO_IO_HANDLE_NONE) { |
Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 332 | return PERMISSION_DENIED; |
| 333 | } |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 334 | |
Jean-Michel Trivi | b7f24b1 | 2014-06-11 10:05:30 -0700 | [diff] [blame] | 335 | return getFrameCount(output, frameCount); |
Eric Laurent | 1a9ed11 | 2012-03-20 18:36:01 -0700 | [diff] [blame] | 336 | } |
| 337 | |
Glenn Kasten | 2c073da | 2016-02-26 09:14:08 -0800 | [diff] [blame] | 338 | status_t AudioSystem::getFrameCount(audio_io_handle_t ioHandle, |
Glenn Kasten | e33054e | 2012-11-14 12:54:39 -0800 | [diff] [blame] | 339 | size_t* frameCount) |
Eric Laurent | 1a9ed11 | 2012-03-20 18:36:01 -0700 | [diff] [blame] | 340 | { |
Eric Laurent | f6778fd | 2014-11-18 17:26:58 -0800 | [diff] [blame] | 341 | const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger(); |
| 342 | if (af == 0) return PERMISSION_DENIED; |
Glenn Kasten | 2c073da | 2016-02-26 09:14:08 -0800 | [diff] [blame] | 343 | sp<AudioIoDescriptor> desc = getIoDescriptor(ioHandle); |
| 344 | if (desc == 0) { |
| 345 | *frameCount = af->frameCount(ioHandle); |
Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 346 | } else { |
Glenn Kasten | 2c073da | 2016-02-26 09:14:08 -0800 | [diff] [blame] | 347 | *frameCount = desc->mFrameCount; |
Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 348 | } |
Glenn Kasten | f94006c | 2014-01-08 08:56:06 -0800 | [diff] [blame] | 349 | if (*frameCount == 0) { |
Glenn Kasten | 2c073da | 2016-02-26 09:14:08 -0800 | [diff] [blame] | 350 | ALOGE("AudioSystem::getFrameCount failed for ioHandle %d", ioHandle); |
Glenn Kasten | f94006c | 2014-01-08 08:56:06 -0800 | [diff] [blame] | 351 | return BAD_VALUE; |
| 352 | } |
Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 353 | |
Glenn Kasten | 2c073da | 2016-02-26 09:14:08 -0800 | [diff] [blame] | 354 | ALOGV("getFrameCount() ioHandle %d, frameCount %zu", ioHandle, *frameCount); |
Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 355 | |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 356 | return NO_ERROR; |
| 357 | } |
| 358 | |
Glenn Kasten | fff6d71 | 2012-01-12 16:38:12 -0800 | [diff] [blame] | 359 | 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] | 360 | { |
Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 361 | audio_io_handle_t output; |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 362 | |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 363 | if (streamType == AUDIO_STREAM_DEFAULT) { |
| 364 | streamType = AUDIO_STREAM_MUSIC; |
Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 365 | } |
Eric Laurent | 48f7f5e | 2009-04-02 09:32:43 -0700 | [diff] [blame] | 366 | |
Glenn Kasten | fff6d71 | 2012-01-12 16:38:12 -0800 | [diff] [blame] | 367 | output = getOutput(streamType); |
Glenn Kasten | 142f519 | 2014-03-25 17:44:59 -0700 | [diff] [blame] | 368 | if (output == AUDIO_IO_HANDLE_NONE) { |
Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 369 | return PERMISSION_DENIED; |
| 370 | } |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 371 | |
Glenn Kasten | 241618f | 2014-03-25 17:48:57 -0700 | [diff] [blame] | 372 | return getLatency(output, latency); |
Eric Laurent | 1a9ed11 | 2012-03-20 18:36:01 -0700 | [diff] [blame] | 373 | } |
| 374 | |
| 375 | status_t AudioSystem::getLatency(audio_io_handle_t output, |
Eric Laurent | 1a9ed11 | 2012-03-20 18:36:01 -0700 | [diff] [blame] | 376 | uint32_t* latency) |
| 377 | { |
Eric Laurent | f6778fd | 2014-11-18 17:26:58 -0800 | [diff] [blame] | 378 | const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger(); |
| 379 | if (af == 0) return PERMISSION_DENIED; |
Eric Laurent | 296fb13 | 2015-05-01 11:38:42 -0700 | [diff] [blame] | 380 | sp<AudioIoDescriptor> outputDesc = getIoDescriptor(output); |
Eric Laurent | 73e26b6 | 2015-04-27 16:55:58 -0700 | [diff] [blame] | 381 | if (outputDesc == 0) { |
Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 382 | *latency = af->latency(output); |
| 383 | } else { |
Eric Laurent | 73e26b6 | 2015-04-27 16:55:58 -0700 | [diff] [blame] | 384 | *latency = outputDesc->mLatency; |
Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 385 | } |
| 386 | |
Glenn Kasten | 241618f | 2014-03-25 17:48:57 -0700 | [diff] [blame] | 387 | ALOGV("getLatency() output %d, latency %d", output, *latency); |
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 | |
Glenn Kasten | dd8104c | 2012-07-02 12:42:44 -0700 | [diff] [blame] | 392 | status_t AudioSystem::getInputBufferSize(uint32_t sampleRate, audio_format_t format, |
| 393 | audio_channel_mask_t channelMask, size_t* buffSize) |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 394 | { |
Eric Laurent | 296fb13 | 2015-05-01 11:38:42 -0700 | [diff] [blame] | 395 | const sp<AudioFlingerClient> afc = getAudioFlingerClient(); |
| 396 | if (afc == 0) { |
| 397 | return NO_INIT; |
| 398 | } |
| 399 | return afc->getInputBufferSize(sampleRate, format, channelMask, buffSize); |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 400 | } |
| 401 | |
Eric Laurent | f0ee6f4 | 2009-10-21 08:14:22 -0700 | [diff] [blame] | 402 | status_t AudioSystem::setVoiceVolume(float value) |
| 403 | { |
| 404 | const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger(); |
| 405 | if (af == 0) return PERMISSION_DENIED; |
| 406 | return af->setVoiceVolume(value); |
| 407 | } |
| 408 | |
Kévin PETIT | 377b2ec | 2014-02-03 12:35:36 +0000 | [diff] [blame] | 409 | status_t AudioSystem::getRenderPosition(audio_io_handle_t output, uint32_t *halFrames, |
Glenn Kasten | 0ed1959 | 2014-03-26 07:50:05 -0700 | [diff] [blame] | 410 | uint32_t *dspFrames) |
Eric Laurent | 342e9cf | 2010-01-19 17:37:09 -0800 | [diff] [blame] | 411 | { |
| 412 | const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger(); |
| 413 | if (af == 0) return PERMISSION_DENIED; |
| 414 | |
Richard Fitzgerald | ad3af33 | 2013-03-25 16:54:37 +0000 | [diff] [blame] | 415 | return af->getRenderPosition(halFrames, dspFrames, output); |
Eric Laurent | 342e9cf | 2010-01-19 17:37:09 -0800 | [diff] [blame] | 416 | } |
| 417 | |
Glenn Kasten | 4944acb | 2013-08-19 08:39:20 -0700 | [diff] [blame] | 418 | uint32_t AudioSystem::getInputFramesLost(audio_io_handle_t ioHandle) |
| 419 | { |
Eric Laurent | 05bca2f | 2010-02-26 02:47:27 -0800 | [diff] [blame] | 420 | const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger(); |
Glenn Kasten | 5f972c0 | 2014-01-13 09:59:31 -0800 | [diff] [blame] | 421 | uint32_t result = 0; |
Eric Laurent | 05bca2f | 2010-02-26 02:47:27 -0800 | [diff] [blame] | 422 | if (af == 0) return result; |
Glenn Kasten | 142f519 | 2014-03-25 17:44:59 -0700 | [diff] [blame] | 423 | if (ioHandle == AUDIO_IO_HANDLE_NONE) return result; |
Eric Laurent | 05bca2f | 2010-02-26 02:47:27 -0800 | [diff] [blame] | 424 | |
| 425 | result = af->getInputFramesLost(ioHandle); |
| 426 | return result; |
| 427 | } |
| 428 | |
Glenn Kasten | eeecb98 | 2016-02-26 10:44:04 -0800 | [diff] [blame] | 429 | audio_unique_id_t AudioSystem::newAudioUniqueId(audio_unique_id_use_t use) |
Glenn Kasten | 4944acb | 2013-08-19 08:39:20 -0700 | [diff] [blame] | 430 | { |
Mikhail Naganov | 2996f67 | 2019-04-18 12:29:59 -0700 | [diff] [blame] | 431 | // Must not use AF as IDs will re-roll on audioserver restart, b/130369529. |
Eric Laurent | be916aa | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 432 | const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger(); |
Eric Laurent | de3f839 | 2014-07-27 18:38:22 -0700 | [diff] [blame] | 433 | if (af == 0) return AUDIO_UNIQUE_ID_ALLOCATE; |
Glenn Kasten | eeecb98 | 2016-02-26 10:44:04 -0800 | [diff] [blame] | 434 | return af->newAudioUniqueId(use); |
Eric Laurent | be916aa | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 435 | } |
| 436 | |
Glenn Kasten | d848eb4 | 2016-03-08 13:42:11 -0800 | [diff] [blame] | 437 | void AudioSystem::acquireAudioSessionId(audio_session_t audioSession, pid_t pid) |
Glenn Kasten | 4944acb | 2013-08-19 08:39:20 -0700 | [diff] [blame] | 438 | { |
Marco Nelissen | 3a34bef | 2011-08-02 13:33:41 -0700 | [diff] [blame] | 439 | const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger(); |
| 440 | if (af != 0) { |
Marco Nelissen | d457c97 | 2014-02-11 08:47:07 -0800 | [diff] [blame] | 441 | af->acquireAudioSessionId(audioSession, pid); |
Marco Nelissen | 3a34bef | 2011-08-02 13:33:41 -0700 | [diff] [blame] | 442 | } |
| 443 | } |
| 444 | |
Glenn Kasten | d848eb4 | 2016-03-08 13:42:11 -0800 | [diff] [blame] | 445 | void AudioSystem::releaseAudioSessionId(audio_session_t audioSession, pid_t pid) |
Glenn Kasten | 4944acb | 2013-08-19 08:39:20 -0700 | [diff] [blame] | 446 | { |
Marco Nelissen | 3a34bef | 2011-08-02 13:33:41 -0700 | [diff] [blame] | 447 | const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger(); |
| 448 | if (af != 0) { |
Marco Nelissen | d457c97 | 2014-02-11 08:47:07 -0800 | [diff] [blame] | 449 | af->releaseAudioSessionId(audioSession, pid); |
Marco Nelissen | 3a34bef | 2011-08-02 13:33:41 -0700 | [diff] [blame] | 450 | } |
| 451 | } |
| 452 | |
Eric Laurent | 93c3d41 | 2014-08-01 14:48:35 -0700 | [diff] [blame] | 453 | audio_hw_sync_t AudioSystem::getAudioHwSyncForSession(audio_session_t sessionId) |
| 454 | { |
| 455 | const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger(); |
| 456 | if (af == 0) return AUDIO_HW_SYNC_INVALID; |
| 457 | return af->getAudioHwSyncForSession(sessionId); |
| 458 | } |
| 459 | |
Eric Laurent | 72e3f39 | 2015-05-20 14:43:50 -0700 | [diff] [blame] | 460 | status_t AudioSystem::systemReady() |
| 461 | { |
| 462 | const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger(); |
| 463 | if (af == 0) return NO_INIT; |
| 464 | return af->systemReady(); |
| 465 | } |
| 466 | |
Glenn Kasten | 4a8308b | 2016-04-18 14:10:01 -0700 | [diff] [blame] | 467 | status_t AudioSystem::getFrameCountHAL(audio_io_handle_t ioHandle, |
| 468 | size_t* frameCount) |
| 469 | { |
| 470 | const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger(); |
| 471 | if (af == 0) return PERMISSION_DENIED; |
| 472 | sp<AudioIoDescriptor> desc = getIoDescriptor(ioHandle); |
| 473 | if (desc == 0) { |
| 474 | *frameCount = af->frameCountHAL(ioHandle); |
| 475 | } else { |
| 476 | *frameCount = desc->mFrameCountHAL; |
| 477 | } |
| 478 | if (*frameCount == 0) { |
| 479 | ALOGE("AudioSystem::getFrameCountHAL failed for ioHandle %d", ioHandle); |
| 480 | return BAD_VALUE; |
| 481 | } |
| 482 | |
| 483 | ALOGV("getFrameCountHAL() ioHandle %d, frameCount %zu", ioHandle, *frameCount); |
| 484 | |
| 485 | return NO_ERROR; |
| 486 | } |
| 487 | |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 488 | // --------------------------------------------------------------------------- |
| 489 | |
Eric Laurent | 73e26b6 | 2015-04-27 16:55:58 -0700 | [diff] [blame] | 490 | |
| 491 | void AudioSystem::AudioFlingerClient::clearIoCache() |
| 492 | { |
| 493 | Mutex::Autolock _l(mLock); |
| 494 | mIoDescriptors.clear(); |
| 495 | mInBuffSize = 0; |
| 496 | mInSamplingRate = 0; |
| 497 | mInFormat = AUDIO_FORMAT_DEFAULT; |
| 498 | mInChannelMask = AUDIO_CHANNEL_NONE; |
| 499 | } |
| 500 | |
Glenn Kasten | 4944acb | 2013-08-19 08:39:20 -0700 | [diff] [blame] | 501 | void AudioSystem::AudioFlingerClient::binderDied(const wp<IBinder>& who __unused) |
| 502 | { |
Eric Laurent | f6778fd | 2014-11-18 17:26:58 -0800 | [diff] [blame] | 503 | audio_error_callback cb = NULL; |
| 504 | { |
| 505 | Mutex::Autolock _l(AudioSystem::gLock); |
| 506 | AudioSystem::gAudioFlinger.clear(); |
| 507 | cb = gAudioErrorCallback; |
| 508 | } |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 509 | |
Eric Laurent | 73e26b6 | 2015-04-27 16:55:58 -0700 | [diff] [blame] | 510 | // clear output handles and stream to output map caches |
| 511 | clearIoCache(); |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 512 | |
Eric Laurent | f6778fd | 2014-11-18 17:26:58 -0800 | [diff] [blame] | 513 | if (cb) { |
| 514 | cb(DEAD_OBJECT); |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 515 | } |
Steve Block | 5ff1dd5 | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 516 | ALOGW("AudioFlinger server died!"); |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 517 | } |
| 518 | |
Eric Laurent | 73e26b6 | 2015-04-27 16:55:58 -0700 | [diff] [blame] | 519 | void AudioSystem::AudioFlingerClient::ioConfigChanged(audio_io_config_event event, |
| 520 | const sp<AudioIoDescriptor>& ioDesc) { |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 521 | ALOGV("ioConfigChanged() event %d", event); |
Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 522 | |
Eric Laurent | 73e26b6 | 2015-04-27 16:55:58 -0700 | [diff] [blame] | 523 | if (ioDesc == 0 || ioDesc->mIoHandle == AUDIO_IO_HANDLE_NONE) return; |
Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 524 | |
Eric Laurent | 296fb13 | 2015-05-01 11:38:42 -0700 | [diff] [blame] | 525 | audio_port_handle_t deviceId = AUDIO_PORT_HANDLE_NONE; |
Eric Laurent | 09f1ed2 | 2019-04-24 17:45:17 -0700 | [diff] [blame] | 526 | std::vector<sp<AudioDeviceCallback>> callbacksToCall; |
Eric Laurent | 296fb13 | 2015-05-01 11:38:42 -0700 | [diff] [blame] | 527 | { |
| 528 | Mutex::Autolock _l(mLock); |
Eric Laurent | 09f1ed2 | 2019-04-24 17:45:17 -0700 | [diff] [blame] | 529 | auto callbacks = std::map<audio_port_handle_t, wp<AudioDeviceCallback>>(); |
Eric Laurent | 296fb13 | 2015-05-01 11:38:42 -0700 | [diff] [blame] | 530 | |
| 531 | switch (event) { |
| 532 | case AUDIO_OUTPUT_OPENED: |
Eric Laurent | ad2e7b9 | 2017-09-14 20:06:42 -0700 | [diff] [blame] | 533 | case AUDIO_OUTPUT_REGISTERED: |
| 534 | case AUDIO_INPUT_OPENED: |
| 535 | case AUDIO_INPUT_REGISTERED: { |
Praveen Chavan | 49fdeaf | 2015-09-29 02:25:47 -0700 | [diff] [blame] | 536 | sp<AudioIoDescriptor> oldDesc = getIoDescriptor_l(ioDesc->mIoHandle); |
Eric Laurent | 7c1ec5f | 2015-07-09 14:52:47 -0700 | [diff] [blame] | 537 | if (oldDesc == 0) { |
| 538 | mIoDescriptors.add(ioDesc->mIoHandle, ioDesc); |
| 539 | } else { |
| 540 | deviceId = oldDesc->getDeviceId(); |
| 541 | mIoDescriptors.replaceValueFor(ioDesc->mIoHandle, ioDesc); |
Eric Laurent | 296fb13 | 2015-05-01 11:38:42 -0700 | [diff] [blame] | 542 | } |
Eric Laurent | 296fb13 | 2015-05-01 11:38:42 -0700 | [diff] [blame] | 543 | |
| 544 | if (ioDesc->getDeviceId() != AUDIO_PORT_HANDLE_NONE) { |
| 545 | deviceId = ioDesc->getDeviceId(); |
Eric Laurent | ad2e7b9 | 2017-09-14 20:06:42 -0700 | [diff] [blame] | 546 | if (event == AUDIO_OUTPUT_OPENED || event == AUDIO_INPUT_OPENED) { |
Eric Laurent | 09f1ed2 | 2019-04-24 17:45:17 -0700 | [diff] [blame] | 547 | auto it = mAudioDeviceCallbacks.find(ioDesc->mIoHandle); |
| 548 | if (it != mAudioDeviceCallbacks.end()) { |
| 549 | callbacks = it->second; |
Francois Gaffie | 24a9fb0 | 2019-01-18 17:51:34 +0100 | [diff] [blame] | 550 | } |
| 551 | } |
Eric Laurent | 296fb13 | 2015-05-01 11:38:42 -0700 | [diff] [blame] | 552 | } |
Eric Laurent | ad2e7b9 | 2017-09-14 20:06:42 -0700 | [diff] [blame] | 553 | ALOGV("ioConfigChanged() new %s %s %d samplingRate %u, format %#x channel mask %#x " |
| 554 | "frameCount %zu deviceId %d", |
| 555 | event == AUDIO_OUTPUT_OPENED || event == AUDIO_OUTPUT_REGISTERED ? |
| 556 | "output" : "input", |
| 557 | event == AUDIO_OUTPUT_OPENED || event == AUDIO_INPUT_OPENED ? |
| 558 | "opened" : "registered", |
Eric Laurent | 296fb13 | 2015-05-01 11:38:42 -0700 | [diff] [blame] | 559 | ioDesc->mIoHandle, ioDesc->mSamplingRate, ioDesc->mFormat, ioDesc->mChannelMask, |
| 560 | ioDesc->mFrameCount, ioDesc->getDeviceId()); |
| 561 | } break; |
| 562 | case AUDIO_OUTPUT_CLOSED: |
| 563 | case AUDIO_INPUT_CLOSED: { |
Praveen Chavan | 49fdeaf | 2015-09-29 02:25:47 -0700 | [diff] [blame] | 564 | if (getIoDescriptor_l(ioDesc->mIoHandle) == 0) { |
Eric Laurent | 296fb13 | 2015-05-01 11:38:42 -0700 | [diff] [blame] | 565 | ALOGW("ioConfigChanged() closing unknown %s %d", |
| 566 | event == AUDIO_OUTPUT_CLOSED ? "output" : "input", ioDesc->mIoHandle); |
| 567 | break; |
| 568 | } |
| 569 | ALOGV("ioConfigChanged() %s %d closed", |
Eric Laurent | 73e26b6 | 2015-04-27 16:55:58 -0700 | [diff] [blame] | 570 | event == AUDIO_OUTPUT_CLOSED ? "output" : "input", ioDesc->mIoHandle); |
Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 571 | |
Eric Laurent | 296fb13 | 2015-05-01 11:38:42 -0700 | [diff] [blame] | 572 | mIoDescriptors.removeItem(ioDesc->mIoHandle); |
Eric Laurent | 09f1ed2 | 2019-04-24 17:45:17 -0700 | [diff] [blame] | 573 | mAudioDeviceCallbacks.erase(ioDesc->mIoHandle); |
Eric Laurent | 296fb13 | 2015-05-01 11:38:42 -0700 | [diff] [blame] | 574 | } break; |
| 575 | |
| 576 | case AUDIO_OUTPUT_CONFIG_CHANGED: |
| 577 | case AUDIO_INPUT_CONFIG_CHANGED: { |
Praveen Chavan | 49fdeaf | 2015-09-29 02:25:47 -0700 | [diff] [blame] | 578 | sp<AudioIoDescriptor> oldDesc = getIoDescriptor_l(ioDesc->mIoHandle); |
Eric Laurent | 296fb13 | 2015-05-01 11:38:42 -0700 | [diff] [blame] | 579 | if (oldDesc == 0) { |
| 580 | ALOGW("ioConfigChanged() modifying unknown output! %d", ioDesc->mIoHandle); |
| 581 | break; |
| 582 | } |
| 583 | |
| 584 | deviceId = oldDesc->getDeviceId(); |
| 585 | mIoDescriptors.replaceValueFor(ioDesc->mIoHandle, ioDesc); |
| 586 | |
| 587 | if (deviceId != ioDesc->getDeviceId()) { |
| 588 | deviceId = ioDesc->getDeviceId(); |
Eric Laurent | 09f1ed2 | 2019-04-24 17:45:17 -0700 | [diff] [blame] | 589 | auto it = mAudioDeviceCallbacks.find(ioDesc->mIoHandle); |
| 590 | if (it != mAudioDeviceCallbacks.end()) { |
| 591 | callbacks = it->second; |
| 592 | } |
Eric Laurent | 296fb13 | 2015-05-01 11:38:42 -0700 | [diff] [blame] | 593 | } |
| 594 | ALOGV("ioConfigChanged() new config for %s %d samplingRate %u, format %#x " |
Glenn Kasten | 4a8308b | 2016-04-18 14:10:01 -0700 | [diff] [blame] | 595 | "channel mask %#x frameCount %zu frameCountHAL %zu deviceId %d", |
Eric Laurent | 296fb13 | 2015-05-01 11:38:42 -0700 | [diff] [blame] | 596 | event == AUDIO_OUTPUT_CONFIG_CHANGED ? "output" : "input", |
| 597 | ioDesc->mIoHandle, ioDesc->mSamplingRate, ioDesc->mFormat, |
Glenn Kasten | d3bb645 | 2016-12-05 18:14:37 -0800 | [diff] [blame] | 598 | ioDesc->mChannelMask, ioDesc->mFrameCount, ioDesc->mFrameCountHAL, |
| 599 | ioDesc->getDeviceId()); |
Eric Laurent | 296fb13 | 2015-05-01 11:38:42 -0700 | [diff] [blame] | 600 | |
Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 601 | } break; |
Eric Laurent | 09f1ed2 | 2019-04-24 17:45:17 -0700 | [diff] [blame] | 602 | case AUDIO_CLIENT_STARTED: { |
| 603 | sp<AudioIoDescriptor> oldDesc = getIoDescriptor_l(ioDesc->mIoHandle); |
| 604 | if (oldDesc == 0) { |
| 605 | ALOGW("ioConfigChanged() start client on unknown io! %d", ioDesc->mIoHandle); |
| 606 | break; |
| 607 | } |
| 608 | ALOGV("ioConfigChanged() AUDIO_CLIENT_STARTED io %d port %d num callbacks %zu", |
| 609 | ioDesc->mIoHandle, ioDesc->mPortId, mAudioDeviceCallbacks.size()); |
| 610 | oldDesc->mPatch = ioDesc->mPatch; |
| 611 | auto it = mAudioDeviceCallbacks.find(ioDesc->mIoHandle); |
| 612 | if (it != mAudioDeviceCallbacks.end()) { |
| 613 | auto cbks = it->second; |
| 614 | auto it2 = cbks.find(ioDesc->mPortId); |
| 615 | if (it2 != cbks.end()) { |
| 616 | callbacks.emplace(ioDesc->mPortId, it2->second); |
| 617 | deviceId = oldDesc->getDeviceId(); |
Francois Gaffie | 24a9fb0 | 2019-01-18 17:51:34 +0100 | [diff] [blame] | 618 | } |
Francois Gaffie | 24a9fb0 | 2019-01-18 17:51:34 +0100 | [diff] [blame] | 619 | } |
Eric Laurent | 09f1ed2 | 2019-04-24 17:45:17 -0700 | [diff] [blame] | 620 | } break; |
| 621 | } |
| 622 | |
| 623 | for (auto wpCbk : callbacks) { |
| 624 | sp<AudioDeviceCallback> spCbk = wpCbk.second.promote(); |
| 625 | if (spCbk != nullptr) { |
| 626 | callbacksToCall.push_back(spCbk); |
| 627 | } |
Eric Laurent | ad2e7b9 | 2017-09-14 20:06:42 -0700 | [diff] [blame] | 628 | } |
Eric Laurent | 4463ff5 | 2019-02-07 13:56:09 -0800 | [diff] [blame] | 629 | } |
| 630 | |
| 631 | // Callbacks must be called without mLock held. May lead to dead lock if calling for |
| 632 | // example getRoutedDevice that updates the device and tries to acquire mLock. |
Eric Laurent | 09f1ed2 | 2019-04-24 17:45:17 -0700 | [diff] [blame] | 633 | for (auto cb : callbacksToCall) { |
| 634 | // If callbacksToCall is not empty, it implies ioDesc->mIoHandle and deviceId are valid |
| 635 | cb->onAudioDeviceUpdate(ioDesc->mIoHandle, deviceId); |
Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 636 | } |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 637 | } |
| 638 | |
Eric Laurent | 73e26b6 | 2015-04-27 16:55:58 -0700 | [diff] [blame] | 639 | status_t AudioSystem::AudioFlingerClient::getInputBufferSize( |
| 640 | uint32_t sampleRate, audio_format_t format, |
| 641 | audio_channel_mask_t channelMask, size_t* buffSize) |
| 642 | { |
| 643 | const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger(); |
| 644 | if (af == 0) { |
| 645 | return PERMISSION_DENIED; |
| 646 | } |
| 647 | Mutex::Autolock _l(mLock); |
| 648 | // Do we have a stale mInBuffSize or are we requesting the input buffer size for new values |
| 649 | if ((mInBuffSize == 0) || (sampleRate != mInSamplingRate) || (format != mInFormat) |
| 650 | || (channelMask != mInChannelMask)) { |
| 651 | size_t inBuffSize = af->getInputBufferSize(sampleRate, format, channelMask); |
| 652 | if (inBuffSize == 0) { |
Glenn Kasten | 49f36ba | 2017-12-06 13:02:02 -0800 | [diff] [blame] | 653 | ALOGE("AudioSystem::getInputBufferSize failed sampleRate %d format %#x channelMask %#x", |
Eric Laurent | 73e26b6 | 2015-04-27 16:55:58 -0700 | [diff] [blame] | 654 | sampleRate, format, channelMask); |
| 655 | return BAD_VALUE; |
| 656 | } |
| 657 | // A benign race is possible here: we could overwrite a fresher cache entry |
| 658 | // save the request params |
| 659 | mInSamplingRate = sampleRate; |
| 660 | mInFormat = format; |
| 661 | mInChannelMask = channelMask; |
| 662 | |
| 663 | mInBuffSize = inBuffSize; |
| 664 | } |
| 665 | |
| 666 | *buffSize = mInBuffSize; |
| 667 | |
| 668 | return NO_ERROR; |
| 669 | } |
| 670 | |
Praveen Chavan | 49fdeaf | 2015-09-29 02:25:47 -0700 | [diff] [blame] | 671 | sp<AudioIoDescriptor> AudioSystem::AudioFlingerClient::getIoDescriptor_l(audio_io_handle_t ioHandle) |
Eric Laurent | 73e26b6 | 2015-04-27 16:55:58 -0700 | [diff] [blame] | 672 | { |
| 673 | sp<AudioIoDescriptor> desc; |
| 674 | ssize_t index = mIoDescriptors.indexOfKey(ioHandle); |
| 675 | if (index >= 0) { |
| 676 | desc = mIoDescriptors.valueAt(index); |
| 677 | } |
| 678 | return desc; |
| 679 | } |
| 680 | |
Praveen Chavan | 49fdeaf | 2015-09-29 02:25:47 -0700 | [diff] [blame] | 681 | sp<AudioIoDescriptor> AudioSystem::AudioFlingerClient::getIoDescriptor(audio_io_handle_t ioHandle) |
| 682 | { |
| 683 | Mutex::Autolock _l(mLock); |
| 684 | return getIoDescriptor_l(ioHandle); |
| 685 | } |
| 686 | |
Eric Laurent | 296fb13 | 2015-05-01 11:38:42 -0700 | [diff] [blame] | 687 | status_t AudioSystem::AudioFlingerClient::addAudioDeviceCallback( |
Eric Laurent | 09f1ed2 | 2019-04-24 17:45:17 -0700 | [diff] [blame] | 688 | const wp<AudioDeviceCallback>& callback, audio_io_handle_t audioIo, |
| 689 | audio_port_handle_t portId) |
Eric Laurent | 296fb13 | 2015-05-01 11:38:42 -0700 | [diff] [blame] | 690 | { |
Eric Laurent | 09f1ed2 | 2019-04-24 17:45:17 -0700 | [diff] [blame] | 691 | ALOGV("%s audioIo %d portId %d", __func__, audioIo, portId); |
Eric Laurent | 4463ff5 | 2019-02-07 13:56:09 -0800 | [diff] [blame] | 692 | Mutex::Autolock _l(mLock); |
Eric Laurent | 09f1ed2 | 2019-04-24 17:45:17 -0700 | [diff] [blame] | 693 | auto& callbacks = mAudioDeviceCallbacks.emplace(audioIo, std::map<audio_port_handle_t, wp<AudioDeviceCallback>>()).first->second; |
| 694 | auto result = callbacks.try_emplace(portId, callback); |
| 695 | if (!result.second) { |
| 696 | return INVALID_OPERATION; |
Eric Laurent | 296fb13 | 2015-05-01 11:38:42 -0700 | [diff] [blame] | 697 | } |
Eric Laurent | 296fb13 | 2015-05-01 11:38:42 -0700 | [diff] [blame] | 698 | return NO_ERROR; |
| 699 | } |
| 700 | |
| 701 | status_t AudioSystem::AudioFlingerClient::removeAudioDeviceCallback( |
Eric Laurent | 09f1ed2 | 2019-04-24 17:45:17 -0700 | [diff] [blame] | 702 | const wp<AudioDeviceCallback>& callback __unused, audio_io_handle_t audioIo, |
| 703 | audio_port_handle_t portId) |
Eric Laurent | 296fb13 | 2015-05-01 11:38:42 -0700 | [diff] [blame] | 704 | { |
Eric Laurent | 09f1ed2 | 2019-04-24 17:45:17 -0700 | [diff] [blame] | 705 | ALOGV("%s audioIo %d portId %d", __func__, audioIo, portId); |
Eric Laurent | 4463ff5 | 2019-02-07 13:56:09 -0800 | [diff] [blame] | 706 | Mutex::Autolock _l(mLock); |
Eric Laurent | 09f1ed2 | 2019-04-24 17:45:17 -0700 | [diff] [blame] | 707 | auto it = mAudioDeviceCallbacks.find(audioIo); |
| 708 | if (it == mAudioDeviceCallbacks.end()) { |
Eric Laurent | 296fb13 | 2015-05-01 11:38:42 -0700 | [diff] [blame] | 709 | return INVALID_OPERATION; |
| 710 | } |
Eric Laurent | 09f1ed2 | 2019-04-24 17:45:17 -0700 | [diff] [blame] | 711 | if (it->second.erase(portId) == 0) { |
Eric Laurent | 296fb13 | 2015-05-01 11:38:42 -0700 | [diff] [blame] | 712 | return INVALID_OPERATION; |
| 713 | } |
Eric Laurent | 09f1ed2 | 2019-04-24 17:45:17 -0700 | [diff] [blame] | 714 | if (it->second.size() == 0) { |
| 715 | mAudioDeviceCallbacks.erase(audioIo); |
Eric Laurent | 296fb13 | 2015-05-01 11:38:42 -0700 | [diff] [blame] | 716 | } |
| 717 | return NO_ERROR; |
| 718 | } |
| 719 | |
| 720 | /* static */ void AudioSystem::setErrorCallback(audio_error_callback cb) |
Glenn Kasten | 4944acb | 2013-08-19 08:39:20 -0700 | [diff] [blame] | 721 | { |
Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 722 | Mutex::Autolock _l(gLock); |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 723 | gAudioErrorCallback = cb; |
| 724 | } |
| 725 | |
Jean-Michel Trivi | f613d42 | 2015-04-23 18:41:29 -0700 | [diff] [blame] | 726 | /*static*/ void AudioSystem::setDynPolicyCallback(dynamic_policy_callback cb) |
| 727 | { |
| 728 | Mutex::Autolock _l(gLock); |
| 729 | gDynPolicyCallback = cb; |
| 730 | } |
| 731 | |
Jean-Michel Trivi | 2f4fe9f | 2015-12-04 16:20:59 -0800 | [diff] [blame] | 732 | /*static*/ void AudioSystem::setRecordConfigCallback(record_config_callback cb) |
| 733 | { |
| 734 | Mutex::Autolock _l(gLock); |
| 735 | gRecordConfigCallback = cb; |
| 736 | } |
| 737 | |
Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 738 | // client singleton for AudioPolicyService binder interface |
Glenn Kasten | d2d089f | 2014-11-05 11:48:12 -0800 | [diff] [blame] | 739 | // protected by gLockAPS |
Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 740 | sp<IAudioPolicyService> AudioSystem::gAudioPolicyService; |
| 741 | sp<AudioSystem::AudioPolicyServiceClient> AudioSystem::gAudioPolicyServiceClient; |
| 742 | |
| 743 | |
Glenn Kasten | 18a6d90 | 2012-09-24 11:27:56 -0700 | [diff] [blame] | 744 | // establish binder interface to AudioPolicy service |
Eric Laurent | 0ebd5f9 | 2014-11-19 19:04:52 -0800 | [diff] [blame] | 745 | const sp<IAudioPolicyService> AudioSystem::get_audio_policy_service() |
Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 746 | { |
Eric Laurent | 0ebd5f9 | 2014-11-19 19:04:52 -0800 | [diff] [blame] | 747 | sp<IAudioPolicyService> ap; |
| 748 | sp<AudioPolicyServiceClient> apc; |
| 749 | { |
| 750 | Mutex::Autolock _l(gLockAPS); |
| 751 | if (gAudioPolicyService == 0) { |
| 752 | sp<IServiceManager> sm = defaultServiceManager(); |
| 753 | sp<IBinder> binder; |
| 754 | do { |
| 755 | binder = sm->getService(String16("media.audio_policy")); |
| 756 | if (binder != 0) |
| 757 | break; |
| 758 | ALOGW("AudioPolicyService not published, waiting..."); |
| 759 | usleep(500000); // 0.5 s |
| 760 | } while (true); |
| 761 | if (gAudioPolicyServiceClient == NULL) { |
| 762 | gAudioPolicyServiceClient = new AudioPolicyServiceClient(); |
| 763 | } |
| 764 | binder->linkToDeath(gAudioPolicyServiceClient); |
| 765 | gAudioPolicyService = interface_cast<IAudioPolicyService>(binder); |
| 766 | LOG_ALWAYS_FATAL_IF(gAudioPolicyService == 0); |
| 767 | apc = gAudioPolicyServiceClient; |
Eric Laurent | fb00fc7 | 2017-05-25 18:17:12 -0700 | [diff] [blame] | 768 | // Make sure callbacks can be received by gAudioPolicyServiceClient |
| 769 | ProcessState::self()->startThreadPool(); |
Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 770 | } |
Eric Laurent | 0ebd5f9 | 2014-11-19 19:04:52 -0800 | [diff] [blame] | 771 | ap = gAudioPolicyService; |
| 772 | } |
| 773 | if (apc != 0) { |
François Gaffie | 2443760 | 2018-04-23 15:08:59 +0200 | [diff] [blame] | 774 | int64_t token = IPCThreadState::self()->clearCallingIdentity(); |
Eric Laurent | 0ebd5f9 | 2014-11-19 19:04:52 -0800 | [diff] [blame] | 775 | ap->registerClient(apc); |
François Gaffie | 2443760 | 2018-04-23 15:08:59 +0200 | [diff] [blame] | 776 | ap->setAudioPortCallbacksEnabled(apc->isAudioPortCbEnabled()); |
François Gaffie | cfe1732 | 2018-11-07 13:41:29 +0100 | [diff] [blame] | 777 | ap->setAudioVolumeGroupCallbacksEnabled(apc->isAudioVolumeGroupCbEnabled()); |
François Gaffie | 2443760 | 2018-04-23 15:08:59 +0200 | [diff] [blame] | 778 | IPCThreadState::self()->restoreCallingIdentity(token); |
Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 779 | } |
Glenn Kasten | d2d089f | 2014-11-05 11:48:12 -0800 | [diff] [blame] | 780 | |
Eric Laurent | 0ebd5f9 | 2014-11-19 19:04:52 -0800 | [diff] [blame] | 781 | return ap; |
Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 782 | } |
| 783 | |
Glenn Kasten | fb1fdc9 | 2013-07-10 17:03:19 -0700 | [diff] [blame] | 784 | // --------------------------------------------------------------------------- |
| 785 | |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 786 | status_t AudioSystem::setDeviceConnectionState(audio_devices_t device, |
| 787 | audio_policy_dev_state_t state, |
Paul McLean | e743a47 | 2015-01-28 11:07:31 -0800 | [diff] [blame] | 788 | const char *device_address, |
Aniket Kumar Lata | 4e46470 | 2019-01-10 23:38:46 -0800 | [diff] [blame] | 789 | const char *device_name, |
| 790 | audio_format_t encodedFormat) |
Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 791 | { |
| 792 | const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service(); |
Eric Laurent | 71b63e3 | 2011-09-02 14:20:56 -0700 | [diff] [blame] | 793 | const char *address = ""; |
Paul McLean | e743a47 | 2015-01-28 11:07:31 -0800 | [diff] [blame] | 794 | const char *name = ""; |
Eric Laurent | 71b63e3 | 2011-09-02 14:20:56 -0700 | [diff] [blame] | 795 | |
Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 796 | if (aps == 0) return PERMISSION_DENIED; |
| 797 | |
Eric Laurent | 71b63e3 | 2011-09-02 14:20:56 -0700 | [diff] [blame] | 798 | if (device_address != NULL) { |
| 799 | address = device_address; |
| 800 | } |
Paul McLean | e743a47 | 2015-01-28 11:07:31 -0800 | [diff] [blame] | 801 | if (device_name != NULL) { |
| 802 | name = device_name; |
| 803 | } |
Aniket Kumar Lata | 4e46470 | 2019-01-10 23:38:46 -0800 | [diff] [blame] | 804 | return aps->setDeviceConnectionState(device, state, address, name, encodedFormat); |
Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 805 | } |
| 806 | |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 807 | audio_policy_dev_state_t AudioSystem::getDeviceConnectionState(audio_devices_t device, |
Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 808 | const char *device_address) |
| 809 | { |
| 810 | const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service(); |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 811 | if (aps == 0) return AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE; |
Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 812 | |
| 813 | return aps->getDeviceConnectionState(device, device_address); |
| 814 | } |
| 815 | |
Pavlin Radoslavov | f862bc6 | 2016-12-26 18:57:22 -0800 | [diff] [blame] | 816 | status_t AudioSystem::handleDeviceConfigChange(audio_devices_t device, |
| 817 | const char *device_address, |
Aniket Kumar Lata | 4e46470 | 2019-01-10 23:38:46 -0800 | [diff] [blame] | 818 | const char *device_name, |
| 819 | audio_format_t encodedFormat) |
Pavlin Radoslavov | f862bc6 | 2016-12-26 18:57:22 -0800 | [diff] [blame] | 820 | { |
| 821 | const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service(); |
| 822 | const char *address = ""; |
| 823 | const char *name = ""; |
| 824 | |
| 825 | if (aps == 0) return PERMISSION_DENIED; |
| 826 | |
| 827 | if (device_address != NULL) { |
| 828 | address = device_address; |
| 829 | } |
| 830 | if (device_name != NULL) { |
| 831 | name = device_name; |
| 832 | } |
Aniket Kumar Lata | 4e46470 | 2019-01-10 23:38:46 -0800 | [diff] [blame] | 833 | return aps->handleDeviceConfigChange(device, address, name, encodedFormat); |
Pavlin Radoslavov | f862bc6 | 2016-12-26 18:57:22 -0800 | [diff] [blame] | 834 | } |
| 835 | |
Glenn Kasten | f78aee7 | 2012-01-04 11:00:47 -0800 | [diff] [blame] | 836 | status_t AudioSystem::setPhoneState(audio_mode_t state) |
Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 837 | { |
Glenn Kasten | 347966c | 2012-01-18 14:58:32 -0800 | [diff] [blame] | 838 | if (uint32_t(state) >= AUDIO_MODE_CNT) return BAD_VALUE; |
Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 839 | const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service(); |
| 840 | if (aps == 0) return PERMISSION_DENIED; |
| 841 | |
| 842 | return aps->setPhoneState(state); |
| 843 | } |
| 844 | |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 845 | 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] | 846 | { |
| 847 | const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service(); |
| 848 | if (aps == 0) return PERMISSION_DENIED; |
| 849 | return aps->setForceUse(usage, config); |
| 850 | } |
| 851 | |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 852 | 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] | 853 | { |
| 854 | const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service(); |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 855 | if (aps == 0) return AUDIO_POLICY_FORCE_NONE; |
Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 856 | return aps->getForceUse(usage); |
| 857 | } |
| 858 | |
| 859 | |
Eric Laurent | f4e6345 | 2017-11-06 19:31:46 +0000 | [diff] [blame] | 860 | audio_io_handle_t AudioSystem::getOutput(audio_stream_type_t stream) |
Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 861 | { |
Eric Laurent | 1a9ed11 | 2012-03-20 18:36:01 -0700 | [diff] [blame] | 862 | const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service(); |
| 863 | if (aps == 0) return 0; |
Eric Laurent | f4e6345 | 2017-11-06 19:31:46 +0000 | [diff] [blame] | 864 | return aps->getOutput(stream); |
Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 865 | } |
| 866 | |
Eric Laurent | 4298441 | 2019-05-09 17:57:03 -0700 | [diff] [blame^] | 867 | status_t AudioSystem::getOutputForAttr(audio_attributes_t *attr, |
Eric Laurent | e83b55d | 2014-11-14 10:06:21 -0800 | [diff] [blame] | 868 | audio_io_handle_t *output, |
| 869 | audio_session_t session, |
| 870 | audio_stream_type_t *stream, |
Nadav Bar | 766fb02 | 2018-01-07 12:18:03 +0200 | [diff] [blame] | 871 | pid_t pid, |
Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 872 | uid_t uid, |
Eric Laurent | 20b9ef0 | 2016-12-05 11:03:16 -0800 | [diff] [blame] | 873 | const audio_config_t *config, |
Eric Laurent | e83b55d | 2014-11-14 10:06:21 -0800 | [diff] [blame] | 874 | audio_output_flags_t flags, |
Eric Laurent | 9ae8c59 | 2017-06-22 17:17:09 -0700 | [diff] [blame] | 875 | audio_port_handle_t *selectedDeviceId, |
Kevin Rocard | 153f92d | 2018-12-18 18:33:28 -0800 | [diff] [blame] | 876 | audio_port_handle_t *portId, |
| 877 | std::vector<audio_io_handle_t> *secondaryOutputs) |
Jean-Michel Trivi | 5bd3f38 | 2014-06-13 16:06:54 -0700 | [diff] [blame] | 878 | { |
Jean-Michel Trivi | 5bd3f38 | 2014-06-13 16:06:54 -0700 | [diff] [blame] | 879 | const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service(); |
Eric Laurent | e83b55d | 2014-11-14 10:06:21 -0800 | [diff] [blame] | 880 | if (aps == 0) return NO_INIT; |
Nadav Bar | 766fb02 | 2018-01-07 12:18:03 +0200 | [diff] [blame] | 881 | return aps->getOutputForAttr(attr, output, session, stream, pid, uid, |
Eric Laurent | 20b9ef0 | 2016-12-05 11:03:16 -0800 | [diff] [blame] | 882 | config, |
Kevin Rocard | 153f92d | 2018-12-18 18:33:28 -0800 | [diff] [blame] | 883 | flags, selectedDeviceId, portId, secondaryOutputs); |
Jean-Michel Trivi | 5bd3f38 | 2014-06-13 16:06:54 -0700 | [diff] [blame] | 884 | } |
| 885 | |
Eric Laurent | d7fe086 | 2018-07-14 16:48:01 -0700 | [diff] [blame] | 886 | status_t AudioSystem::startOutput(audio_port_handle_t portId) |
Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 887 | { |
| 888 | const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service(); |
| 889 | if (aps == 0) return PERMISSION_DENIED; |
Eric Laurent | d7fe086 | 2018-07-14 16:48:01 -0700 | [diff] [blame] | 890 | return aps->startOutput(portId); |
Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 891 | } |
| 892 | |
Eric Laurent | d7fe086 | 2018-07-14 16:48:01 -0700 | [diff] [blame] | 893 | status_t AudioSystem::stopOutput(audio_port_handle_t portId) |
Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 894 | { |
| 895 | const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service(); |
| 896 | if (aps == 0) return PERMISSION_DENIED; |
Eric Laurent | d7fe086 | 2018-07-14 16:48:01 -0700 | [diff] [blame] | 897 | return aps->stopOutput(portId); |
Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 898 | } |
| 899 | |
Eric Laurent | d7fe086 | 2018-07-14 16:48:01 -0700 | [diff] [blame] | 900 | void AudioSystem::releaseOutput(audio_port_handle_t portId) |
Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 901 | { |
| 902 | const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service(); |
| 903 | if (aps == 0) return; |
Eric Laurent | d7fe086 | 2018-07-14 16:48:01 -0700 | [diff] [blame] | 904 | aps->releaseOutput(portId); |
Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 905 | } |
| 906 | |
Eric Laurent | caf7f48 | 2014-11-25 17:50:47 -0800 | [diff] [blame] | 907 | status_t AudioSystem::getInputForAttr(const audio_attributes_t *attr, |
| 908 | audio_io_handle_t *input, |
Mikhail Naganov | 2996f67 | 2019-04-18 12:29:59 -0700 | [diff] [blame] | 909 | audio_unique_id_t riid, |
Eric Laurent | caf7f48 | 2014-11-25 17:50:47 -0800 | [diff] [blame] | 910 | audio_session_t session, |
Eric Laurent | b2379ba | 2016-05-23 17:42:12 -0700 | [diff] [blame] | 911 | pid_t pid, |
Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 912 | uid_t uid, |
Eric Laurent | fee1976 | 2018-01-29 18:44:13 -0800 | [diff] [blame] | 913 | const String16& opPackageName, |
Eric Laurent | 20b9ef0 | 2016-12-05 11:03:16 -0800 | [diff] [blame] | 914 | const audio_config_base_t *config, |
Paul McLean | 466dc8e | 2015-04-17 13:15:36 -0600 | [diff] [blame] | 915 | audio_input_flags_t flags, |
Eric Laurent | 9ae8c59 | 2017-06-22 17:17:09 -0700 | [diff] [blame] | 916 | audio_port_handle_t *selectedDeviceId, |
Eric Laurent | 20b9ef0 | 2016-12-05 11:03:16 -0800 | [diff] [blame] | 917 | audio_port_handle_t *portId) |
Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 918 | { |
| 919 | const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service(); |
Eric Laurent | caf7f48 | 2014-11-25 17:50:47 -0800 | [diff] [blame] | 920 | if (aps == 0) return NO_INIT; |
Paul McLean | 466dc8e | 2015-04-17 13:15:36 -0600 | [diff] [blame] | 921 | return aps->getInputForAttr( |
Mikhail Naganov | 2996f67 | 2019-04-18 12:29:59 -0700 | [diff] [blame] | 922 | attr, input, riid, session, pid, uid, opPackageName, |
Eric Laurent | 20b9ef0 | 2016-12-05 11:03:16 -0800 | [diff] [blame] | 923 | config, flags, selectedDeviceId, portId); |
Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 924 | } |
| 925 | |
Eric Laurent | 4eb58f1 | 2018-12-07 16:41:02 -0800 | [diff] [blame] | 926 | status_t AudioSystem::startInput(audio_port_handle_t portId) |
Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 927 | { |
| 928 | const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service(); |
| 929 | if (aps == 0) return PERMISSION_DENIED; |
Eric Laurent | 4eb58f1 | 2018-12-07 16:41:02 -0800 | [diff] [blame] | 930 | return aps->startInput(portId); |
Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 931 | } |
| 932 | |
Eric Laurent | fee1976 | 2018-01-29 18:44:13 -0800 | [diff] [blame] | 933 | status_t AudioSystem::stopInput(audio_port_handle_t portId) |
Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 934 | { |
| 935 | const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service(); |
| 936 | if (aps == 0) return PERMISSION_DENIED; |
Eric Laurent | fee1976 | 2018-01-29 18:44:13 -0800 | [diff] [blame] | 937 | return aps->stopInput(portId); |
Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 938 | } |
| 939 | |
Eric Laurent | fee1976 | 2018-01-29 18:44:13 -0800 | [diff] [blame] | 940 | void AudioSystem::releaseInput(audio_port_handle_t portId) |
Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 941 | { |
| 942 | const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service(); |
| 943 | if (aps == 0) return; |
Eric Laurent | fee1976 | 2018-01-29 18:44:13 -0800 | [diff] [blame] | 944 | aps->releaseInput(portId); |
Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 945 | } |
| 946 | |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 947 | status_t AudioSystem::initStreamVolume(audio_stream_type_t stream, |
Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 948 | int indexMin, |
| 949 | int indexMax) |
| 950 | { |
| 951 | const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service(); |
| 952 | if (aps == 0) return PERMISSION_DENIED; |
| 953 | return aps->initStreamVolume(stream, indexMin, indexMax); |
| 954 | } |
| 955 | |
Eric Laurent | 83844cc | 2011-11-18 16:43:31 -0800 | [diff] [blame] | 956 | status_t AudioSystem::setStreamVolumeIndex(audio_stream_type_t stream, |
| 957 | int index, |
| 958 | audio_devices_t device) |
Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 959 | { |
| 960 | const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service(); |
| 961 | if (aps == 0) return PERMISSION_DENIED; |
Eric Laurent | 83844cc | 2011-11-18 16:43:31 -0800 | [diff] [blame] | 962 | return aps->setStreamVolumeIndex(stream, index, device); |
Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 963 | } |
| 964 | |
Eric Laurent | 83844cc | 2011-11-18 16:43:31 -0800 | [diff] [blame] | 965 | status_t AudioSystem::getStreamVolumeIndex(audio_stream_type_t stream, |
| 966 | int *index, |
| 967 | audio_devices_t device) |
Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 968 | { |
| 969 | const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service(); |
| 970 | if (aps == 0) return PERMISSION_DENIED; |
Eric Laurent | 83844cc | 2011-11-18 16:43:31 -0800 | [diff] [blame] | 971 | return aps->getStreamVolumeIndex(stream, index, device); |
Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 972 | } |
| 973 | |
François Gaffie | cfe1732 | 2018-11-07 13:41:29 +0100 | [diff] [blame] | 974 | status_t AudioSystem::setVolumeIndexForAttributes(const audio_attributes_t &attr, |
| 975 | int index, |
| 976 | audio_devices_t device) |
| 977 | { |
| 978 | const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service(); |
| 979 | if (aps == 0) return PERMISSION_DENIED; |
| 980 | return aps->setVolumeIndexForAttributes(attr, index, device); |
| 981 | } |
| 982 | |
| 983 | status_t AudioSystem::getVolumeIndexForAttributes(const audio_attributes_t &attr, |
| 984 | int &index, |
| 985 | audio_devices_t device) |
| 986 | { |
| 987 | const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service(); |
| 988 | if (aps == 0) return PERMISSION_DENIED; |
| 989 | return aps->getVolumeIndexForAttributes(attr, index, device); |
| 990 | } |
| 991 | |
| 992 | status_t AudioSystem::getMaxVolumeIndexForAttributes(const audio_attributes_t &attr, int &index) |
| 993 | { |
| 994 | const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service(); |
| 995 | if (aps == 0) return PERMISSION_DENIED; |
| 996 | return aps->getMaxVolumeIndexForAttributes(attr, index); |
| 997 | } |
| 998 | |
| 999 | status_t AudioSystem::getMinVolumeIndexForAttributes(const audio_attributes_t &attr, int &index) |
| 1000 | { |
| 1001 | const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service(); |
| 1002 | if (aps == 0) return PERMISSION_DENIED; |
| 1003 | return aps->getMinVolumeIndexForAttributes(attr, index); |
| 1004 | } |
| 1005 | |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 1006 | uint32_t AudioSystem::getStrategyForStream(audio_stream_type_t stream) |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 1007 | { |
| 1008 | const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service(); |
François Gaffie | d0ba9ed | 2018-11-05 11:50:42 +0100 | [diff] [blame] | 1009 | if (aps == 0) return PRODUCT_STRATEGY_NONE; |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 1010 | return aps->getStrategyForStream(stream); |
| 1011 | } |
| 1012 | |
Eric Laurent | 6374252 | 2012-03-08 13:42:42 -0800 | [diff] [blame] | 1013 | audio_devices_t AudioSystem::getDevicesForStream(audio_stream_type_t stream) |
Glenn Kasten | 6b2718c | 2011-02-04 13:54:26 -0800 | [diff] [blame] | 1014 | { |
| 1015 | const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service(); |
Glenn Kasten | 45faf7e | 2014-01-17 10:23:01 -0800 | [diff] [blame] | 1016 | if (aps == 0) return AUDIO_DEVICE_NONE; |
Glenn Kasten | 6b2718c | 2011-02-04 13:54:26 -0800 | [diff] [blame] | 1017 | return aps->getDevicesForStream(stream); |
| 1018 | } |
| 1019 | |
Glenn Kasten | 58e5aa3 | 2012-06-20 14:08:14 -0700 | [diff] [blame] | 1020 | audio_io_handle_t AudioSystem::getOutputForEffect(const effect_descriptor_t *desc) |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 1021 | { |
| 1022 | const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service(); |
Glenn Kasten | efa6ea9 | 2014-01-08 09:10:43 -0800 | [diff] [blame] | 1023 | // FIXME change return type to status_t, and return PERMISSION_DENIED here |
Glenn Kasten | 142f519 | 2014-03-25 17:44:59 -0700 | [diff] [blame] | 1024 | if (aps == 0) return AUDIO_IO_HANDLE_NONE; |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 1025 | return aps->getOutputForEffect(desc); |
| 1026 | } |
| 1027 | |
Glenn Kasten | 58e5aa3 | 2012-06-20 14:08:14 -0700 | [diff] [blame] | 1028 | status_t AudioSystem::registerEffect(const effect_descriptor_t *desc, |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 1029 | audio_io_handle_t io, |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 1030 | uint32_t strategy, |
Glenn Kasten | d848eb4 | 2016-03-08 13:42:11 -0800 | [diff] [blame] | 1031 | audio_session_t session, |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 1032 | int id) |
| 1033 | { |
| 1034 | const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service(); |
| 1035 | if (aps == 0) return PERMISSION_DENIED; |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 1036 | return aps->registerEffect(desc, io, strategy, session, id); |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 1037 | } |
| 1038 | |
| 1039 | status_t AudioSystem::unregisterEffect(int id) |
| 1040 | { |
| 1041 | const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service(); |
| 1042 | if (aps == 0) return PERMISSION_DENIED; |
| 1043 | return aps->unregisterEffect(id); |
| 1044 | } |
| 1045 | |
Eric Laurent | db7c079 | 2011-08-10 10:37:50 -0700 | [diff] [blame] | 1046 | status_t AudioSystem::setEffectEnabled(int id, bool enabled) |
| 1047 | { |
| 1048 | const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service(); |
| 1049 | if (aps == 0) return PERMISSION_DENIED; |
| 1050 | return aps->setEffectEnabled(id, enabled); |
| 1051 | } |
| 1052 | |
Eric Laurent | 6c79632 | 2019-04-09 14:13:17 -0700 | [diff] [blame] | 1053 | status_t AudioSystem::moveEffectsToIo(const std::vector<int>& ids, audio_io_handle_t io) |
| 1054 | { |
| 1055 | const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service(); |
| 1056 | if (aps == 0) return PERMISSION_DENIED; |
| 1057 | return aps->moveEffectsToIo(ids, io); |
| 1058 | } |
| 1059 | |
Glenn Kasten | fff6d71 | 2012-01-12 16:38:12 -0800 | [diff] [blame] | 1060 | 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] | 1061 | { |
Eric Laurent | eda6c36 | 2011-02-02 09:33:30 -0800 | [diff] [blame] | 1062 | const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service(); |
| 1063 | if (aps == 0) return PERMISSION_DENIED; |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 1064 | if (state == NULL) return BAD_VALUE; |
Eric Laurent | eda6c36 | 2011-02-02 09:33:30 -0800 | [diff] [blame] | 1065 | *state = aps->isStreamActive(stream, inPastMs); |
| 1066 | return NO_ERROR; |
| 1067 | } |
| 1068 | |
Jean-Michel Trivi | 272ab54 | 2013-02-04 16:26:02 -0800 | [diff] [blame] | 1069 | status_t AudioSystem::isStreamActiveRemotely(audio_stream_type_t stream, bool* state, |
| 1070 | uint32_t inPastMs) |
| 1071 | { |
| 1072 | const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service(); |
| 1073 | if (aps == 0) return PERMISSION_DENIED; |
| 1074 | if (state == NULL) return BAD_VALUE; |
| 1075 | *state = aps->isStreamActiveRemotely(stream, inPastMs); |
| 1076 | return NO_ERROR; |
| 1077 | } |
| 1078 | |
Jean-Michel Trivi | d708603 | 2012-10-10 12:11:16 -0700 | [diff] [blame] | 1079 | status_t AudioSystem::isSourceActive(audio_source_t stream, bool* state) |
| 1080 | { |
| 1081 | const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service(); |
| 1082 | if (aps == 0) return PERMISSION_DENIED; |
| 1083 | if (state == NULL) return BAD_VALUE; |
| 1084 | *state = aps->isSourceActive(stream); |
| 1085 | return NO_ERROR; |
| 1086 | } |
| 1087 | |
Glenn Kasten | 3b16c76 | 2012-11-14 08:44:39 -0800 | [diff] [blame] | 1088 | uint32_t AudioSystem::getPrimaryOutputSamplingRate() |
Glenn Kasten | cc0f1cf | 2012-09-24 11:27:18 -0700 | [diff] [blame] | 1089 | { |
| 1090 | const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger(); |
| 1091 | if (af == 0) return 0; |
| 1092 | return af->getPrimaryOutputSamplingRate(); |
| 1093 | } |
| 1094 | |
Glenn Kasten | e33054e | 2012-11-14 12:54:39 -0800 | [diff] [blame] | 1095 | size_t AudioSystem::getPrimaryOutputFrameCount() |
Glenn Kasten | cc0f1cf | 2012-09-24 11:27:18 -0700 | [diff] [blame] | 1096 | { |
| 1097 | const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger(); |
| 1098 | if (af == 0) return 0; |
| 1099 | return af->getPrimaryOutputFrameCount(); |
| 1100 | } |
Eric Laurent | eda6c36 | 2011-02-02 09:33:30 -0800 | [diff] [blame] | 1101 | |
Andy Hung | 6f248bb | 2018-01-23 14:04:37 -0800 | [diff] [blame] | 1102 | status_t AudioSystem::setLowRamDevice(bool isLowRamDevice, int64_t totalMemory) |
Glenn Kasten | 4182c4e | 2013-07-15 14:45:07 -0700 | [diff] [blame] | 1103 | { |
| 1104 | const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger(); |
| 1105 | if (af == 0) return PERMISSION_DENIED; |
Andy Hung | 6f248bb | 2018-01-23 14:04:37 -0800 | [diff] [blame] | 1106 | return af->setLowRamDevice(isLowRamDevice, totalMemory); |
Glenn Kasten | 4182c4e | 2013-07-15 14:45:07 -0700 | [diff] [blame] | 1107 | } |
| 1108 | |
Eric Laurent | 9f6530f | 2011-08-30 10:18:54 -0700 | [diff] [blame] | 1109 | void AudioSystem::clearAudioConfigCache() |
| 1110 | { |
Glenn Kasten | d2d089f | 2014-11-05 11:48:12 -0800 | [diff] [blame] | 1111 | // called by restoreTrack_l(), which needs new IAudioFlinger and IAudioPolicyService instances |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 1112 | ALOGV("clearAudioConfigCache()"); |
Eric Laurent | f6778fd | 2014-11-18 17:26:58 -0800 | [diff] [blame] | 1113 | { |
| 1114 | Mutex::Autolock _l(gLock); |
Eric Laurent | 296fb13 | 2015-05-01 11:38:42 -0700 | [diff] [blame] | 1115 | if (gAudioFlingerClient != 0) { |
| 1116 | gAudioFlingerClient->clearIoCache(); |
| 1117 | } |
Glenn Kasten | d2d089f | 2014-11-05 11:48:12 -0800 | [diff] [blame] | 1118 | gAudioFlinger.clear(); |
| 1119 | } |
| 1120 | { |
| 1121 | Mutex::Autolock _l(gLockAPS); |
| 1122 | gAudioPolicyService.clear(); |
| 1123 | } |
Eric Laurent | 9f6530f | 2011-08-30 10:18:54 -0700 | [diff] [blame] | 1124 | } |
| 1125 | |
Kevin Rocard | b99cc75 | 2019-03-21 20:52:24 -0700 | [diff] [blame] | 1126 | status_t AudioSystem::setAllowedCapturePolicy(uid_t uid, audio_flags_mask_t flags) { |
| 1127 | const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service(); |
| 1128 | if (aps == nullptr) return PERMISSION_DENIED; |
| 1129 | return aps->setAllowedCapturePolicy(uid, flags); |
| 1130 | } |
| 1131 | |
Richard Fitzgerald | ad3af33 | 2013-03-25 16:54:37 +0000 | [diff] [blame] | 1132 | bool AudioSystem::isOffloadSupported(const audio_offload_info_t& info) |
| 1133 | { |
| 1134 | ALOGV("isOffloadSupported()"); |
| 1135 | const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service(); |
| 1136 | if (aps == 0) return false; |
| 1137 | return aps->isOffloadSupported(info); |
| 1138 | } |
| 1139 | |
Eric Laurent | 203b1a1 | 2014-04-01 10:34:16 -0700 | [diff] [blame] | 1140 | status_t AudioSystem::listAudioPorts(audio_port_role_t role, |
| 1141 | audio_port_type_t type, |
| 1142 | unsigned int *num_ports, |
| 1143 | struct audio_port *ports, |
| 1144 | unsigned int *generation) |
| 1145 | { |
| 1146 | const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service(); |
| 1147 | if (aps == 0) return PERMISSION_DENIED; |
| 1148 | return aps->listAudioPorts(role, type, num_ports, ports, generation); |
| 1149 | } |
| 1150 | |
| 1151 | status_t AudioSystem::getAudioPort(struct audio_port *port) |
| 1152 | { |
| 1153 | const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service(); |
| 1154 | if (aps == 0) return PERMISSION_DENIED; |
| 1155 | return aps->getAudioPort(port); |
| 1156 | } |
| 1157 | |
| 1158 | status_t AudioSystem::createAudioPatch(const struct audio_patch *patch, |
| 1159 | audio_patch_handle_t *handle) |
| 1160 | { |
| 1161 | const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service(); |
| 1162 | if (aps == 0) return PERMISSION_DENIED; |
| 1163 | return aps->createAudioPatch(patch, handle); |
| 1164 | } |
| 1165 | |
| 1166 | status_t AudioSystem::releaseAudioPatch(audio_patch_handle_t handle) |
| 1167 | { |
| 1168 | const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service(); |
| 1169 | if (aps == 0) return PERMISSION_DENIED; |
| 1170 | return aps->releaseAudioPatch(handle); |
| 1171 | } |
| 1172 | |
| 1173 | status_t AudioSystem::listAudioPatches(unsigned int *num_patches, |
| 1174 | struct audio_patch *patches, |
| 1175 | unsigned int *generation) |
| 1176 | { |
| 1177 | const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service(); |
| 1178 | if (aps == 0) return PERMISSION_DENIED; |
| 1179 | return aps->listAudioPatches(num_patches, patches, generation); |
| 1180 | } |
| 1181 | |
| 1182 | status_t AudioSystem::setAudioPortConfig(const struct audio_port_config *config) |
| 1183 | { |
| 1184 | const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service(); |
| 1185 | if (aps == 0) return PERMISSION_DENIED; |
| 1186 | return aps->setAudioPortConfig(config); |
| 1187 | } |
| 1188 | |
Eric Laurent | 296fb13 | 2015-05-01 11:38:42 -0700 | [diff] [blame] | 1189 | status_t AudioSystem::addAudioPortCallback(const sp<AudioPortCallback>& callback) |
Eric Laurent | b52c152 | 2014-05-20 11:27:36 -0700 | [diff] [blame] | 1190 | { |
Eric Laurent | b28753e | 2015-04-01 13:06:28 -0700 | [diff] [blame] | 1191 | const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service(); |
| 1192 | if (aps == 0) return PERMISSION_DENIED; |
| 1193 | |
| 1194 | Mutex::Autolock _l(gLockAPS); |
| 1195 | if (gAudioPolicyServiceClient == 0) { |
| 1196 | return NO_INIT; |
| 1197 | } |
Eric Laurent | e8726fe | 2015-06-26 09:39:24 -0700 | [diff] [blame] | 1198 | int ret = gAudioPolicyServiceClient->addAudioPortCallback(callback); |
| 1199 | if (ret == 1) { |
| 1200 | aps->setAudioPortCallbacksEnabled(true); |
| 1201 | } |
| 1202 | return (ret < 0) ? INVALID_OPERATION : NO_ERROR; |
Eric Laurent | b52c152 | 2014-05-20 11:27:36 -0700 | [diff] [blame] | 1203 | } |
| 1204 | |
Jean-Michel Trivi | f613d42 | 2015-04-23 18:41:29 -0700 | [diff] [blame] | 1205 | /*static*/ |
Eric Laurent | 296fb13 | 2015-05-01 11:38:42 -0700 | [diff] [blame] | 1206 | status_t AudioSystem::removeAudioPortCallback(const sp<AudioPortCallback>& callback) |
Eric Laurent | b28753e | 2015-04-01 13:06:28 -0700 | [diff] [blame] | 1207 | { |
| 1208 | const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service(); |
| 1209 | if (aps == 0) return PERMISSION_DENIED; |
| 1210 | |
| 1211 | Mutex::Autolock _l(gLockAPS); |
| 1212 | if (gAudioPolicyServiceClient == 0) { |
| 1213 | return NO_INIT; |
| 1214 | } |
Eric Laurent | e8726fe | 2015-06-26 09:39:24 -0700 | [diff] [blame] | 1215 | int ret = gAudioPolicyServiceClient->removeAudioPortCallback(callback); |
| 1216 | if (ret == 0) { |
| 1217 | aps->setAudioPortCallbacksEnabled(false); |
| 1218 | } |
| 1219 | return (ret < 0) ? INVALID_OPERATION : NO_ERROR; |
Eric Laurent | 296fb13 | 2015-05-01 11:38:42 -0700 | [diff] [blame] | 1220 | } |
| 1221 | |
François Gaffie | cfe1732 | 2018-11-07 13:41:29 +0100 | [diff] [blame] | 1222 | status_t AudioSystem::addAudioVolumeGroupCallback(const sp<AudioVolumeGroupCallback>& callback) |
| 1223 | { |
| 1224 | const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service(); |
| 1225 | if (aps == 0) return PERMISSION_DENIED; |
| 1226 | |
| 1227 | Mutex::Autolock _l(gLockAPS); |
| 1228 | if (gAudioPolicyServiceClient == 0) { |
| 1229 | return NO_INIT; |
| 1230 | } |
| 1231 | int ret = gAudioPolicyServiceClient->addAudioVolumeGroupCallback(callback); |
| 1232 | if (ret == 1) { |
| 1233 | aps->setAudioVolumeGroupCallbacksEnabled(true); |
| 1234 | } |
| 1235 | return (ret < 0) ? INVALID_OPERATION : NO_ERROR; |
| 1236 | } |
| 1237 | |
| 1238 | status_t AudioSystem::removeAudioVolumeGroupCallback(const sp<AudioVolumeGroupCallback>& callback) |
| 1239 | { |
| 1240 | const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service(); |
| 1241 | if (aps == 0) return PERMISSION_DENIED; |
| 1242 | |
| 1243 | Mutex::Autolock _l(gLockAPS); |
| 1244 | if (gAudioPolicyServiceClient == 0) { |
| 1245 | return NO_INIT; |
| 1246 | } |
| 1247 | int ret = gAudioPolicyServiceClient->removeAudioVolumeGroupCallback(callback); |
| 1248 | if (ret == 0) { |
| 1249 | aps->setAudioVolumeGroupCallbacksEnabled(false); |
| 1250 | } |
| 1251 | return (ret < 0) ? INVALID_OPERATION : NO_ERROR; |
| 1252 | } |
| 1253 | |
Eric Laurent | 296fb13 | 2015-05-01 11:38:42 -0700 | [diff] [blame] | 1254 | status_t AudioSystem::addAudioDeviceCallback( |
Eric Laurent | 09f1ed2 | 2019-04-24 17:45:17 -0700 | [diff] [blame] | 1255 | const wp<AudioDeviceCallback>& callback, audio_io_handle_t audioIo, |
| 1256 | audio_port_handle_t portId) |
Eric Laurent | 296fb13 | 2015-05-01 11:38:42 -0700 | [diff] [blame] | 1257 | { |
| 1258 | const sp<AudioFlingerClient> afc = getAudioFlingerClient(); |
| 1259 | if (afc == 0) { |
| 1260 | return NO_INIT; |
| 1261 | } |
Eric Laurent | 09f1ed2 | 2019-04-24 17:45:17 -0700 | [diff] [blame] | 1262 | status_t status = afc->addAudioDeviceCallback(callback, audioIo, portId); |
Eric Laurent | 7c1ec5f | 2015-07-09 14:52:47 -0700 | [diff] [blame] | 1263 | if (status == NO_ERROR) { |
| 1264 | const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger(); |
| 1265 | if (af != 0) { |
| 1266 | af->registerClient(afc); |
| 1267 | } |
| 1268 | } |
| 1269 | return status; |
Eric Laurent | 296fb13 | 2015-05-01 11:38:42 -0700 | [diff] [blame] | 1270 | } |
| 1271 | |
| 1272 | status_t AudioSystem::removeAudioDeviceCallback( |
Eric Laurent | 09f1ed2 | 2019-04-24 17:45:17 -0700 | [diff] [blame] | 1273 | const wp<AudioDeviceCallback>& callback, audio_io_handle_t audioIo, |
| 1274 | audio_port_handle_t portId) |
Eric Laurent | 296fb13 | 2015-05-01 11:38:42 -0700 | [diff] [blame] | 1275 | { |
| 1276 | const sp<AudioFlingerClient> afc = getAudioFlingerClient(); |
| 1277 | if (afc == 0) { |
| 1278 | return NO_INIT; |
| 1279 | } |
Eric Laurent | 09f1ed2 | 2019-04-24 17:45:17 -0700 | [diff] [blame] | 1280 | return afc->removeAudioDeviceCallback(callback, audioIo, portId); |
Eric Laurent | 296fb13 | 2015-05-01 11:38:42 -0700 | [diff] [blame] | 1281 | } |
| 1282 | |
| 1283 | audio_port_handle_t AudioSystem::getDeviceIdForIo(audio_io_handle_t audioIo) |
| 1284 | { |
| 1285 | const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger(); |
| 1286 | if (af == 0) return PERMISSION_DENIED; |
| 1287 | const sp<AudioIoDescriptor> desc = getIoDescriptor(audioIo); |
| 1288 | if (desc == 0) { |
| 1289 | return AUDIO_PORT_HANDLE_NONE; |
| 1290 | } |
| 1291 | return desc->getDeviceId(); |
Eric Laurent | b28753e | 2015-04-01 13:06:28 -0700 | [diff] [blame] | 1292 | } |
| 1293 | |
Eric Laurent | df3dc7e | 2014-07-27 18:39:40 -0700 | [diff] [blame] | 1294 | status_t AudioSystem::acquireSoundTriggerSession(audio_session_t *session, |
| 1295 | audio_io_handle_t *ioHandle, |
| 1296 | audio_devices_t *device) |
| 1297 | { |
| 1298 | const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service(); |
| 1299 | if (aps == 0) return PERMISSION_DENIED; |
| 1300 | return aps->acquireSoundTriggerSession(session, ioHandle, device); |
| 1301 | } |
| 1302 | |
| 1303 | status_t AudioSystem::releaseSoundTriggerSession(audio_session_t session) |
| 1304 | { |
| 1305 | const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service(); |
| 1306 | if (aps == 0) return PERMISSION_DENIED; |
| 1307 | return aps->releaseSoundTriggerSession(session); |
| 1308 | } |
Eric Laurent | bb6c9a0 | 2014-09-25 14:11:47 -0700 | [diff] [blame] | 1309 | |
| 1310 | audio_mode_t AudioSystem::getPhoneState() |
| 1311 | { |
| 1312 | const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service(); |
| 1313 | if (aps == 0) return AUDIO_MODE_INVALID; |
| 1314 | return aps->getPhoneState(); |
| 1315 | } |
| 1316 | |
Chih-Hung Hsieh | e964d4e | 2016-08-09 14:31:32 -0700 | [diff] [blame] | 1317 | status_t AudioSystem::registerPolicyMixes(const Vector<AudioMix>& mixes, bool registration) |
Eric Laurent | baac183 | 2014-12-01 17:52:59 -0800 | [diff] [blame] | 1318 | { |
| 1319 | const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service(); |
| 1320 | if (aps == 0) return PERMISSION_DENIED; |
| 1321 | return aps->registerPolicyMixes(mixes, registration); |
| 1322 | } |
Eric Laurent | bb6c9a0 | 2014-09-25 14:11:47 -0700 | [diff] [blame] | 1323 | |
Jean-Michel Trivi | bda70da | 2018-12-19 07:30:15 -0800 | [diff] [blame] | 1324 | status_t AudioSystem::setUidDeviceAffinities(uid_t uid, const Vector<AudioDeviceTypeAddr>& devices) |
| 1325 | { |
| 1326 | const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service(); |
| 1327 | if (aps == 0) return PERMISSION_DENIED; |
| 1328 | return aps->setUidDeviceAffinities(uid, devices); |
| 1329 | } |
| 1330 | |
| 1331 | status_t AudioSystem::removeUidDeviceAffinities(uid_t uid) { |
| 1332 | const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service(); |
| 1333 | if (aps == 0) return PERMISSION_DENIED; |
| 1334 | return aps->removeUidDeviceAffinities(uid); |
| 1335 | } |
| 1336 | |
Eric Laurent | 554a277 | 2015-04-10 11:29:24 -0700 | [diff] [blame] | 1337 | status_t AudioSystem::startAudioSource(const struct audio_port_config *source, |
| 1338 | const audio_attributes_t *attributes, |
Eric Laurent | 3e6c7e1 | 2018-07-27 17:09:23 -0700 | [diff] [blame] | 1339 | audio_port_handle_t *portId) |
Eric Laurent | 554a277 | 2015-04-10 11:29:24 -0700 | [diff] [blame] | 1340 | { |
| 1341 | const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service(); |
| 1342 | if (aps == 0) return PERMISSION_DENIED; |
Eric Laurent | 3e6c7e1 | 2018-07-27 17:09:23 -0700 | [diff] [blame] | 1343 | return aps->startAudioSource(source, attributes, portId); |
Eric Laurent | 554a277 | 2015-04-10 11:29:24 -0700 | [diff] [blame] | 1344 | } |
| 1345 | |
Eric Laurent | 3e6c7e1 | 2018-07-27 17:09:23 -0700 | [diff] [blame] | 1346 | status_t AudioSystem::stopAudioSource(audio_port_handle_t portId) |
Eric Laurent | 554a277 | 2015-04-10 11:29:24 -0700 | [diff] [blame] | 1347 | { |
| 1348 | const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service(); |
| 1349 | if (aps == 0) return PERMISSION_DENIED; |
Eric Laurent | 3e6c7e1 | 2018-07-27 17:09:23 -0700 | [diff] [blame] | 1350 | return aps->stopAudioSource(portId); |
Eric Laurent | 554a277 | 2015-04-10 11:29:24 -0700 | [diff] [blame] | 1351 | } |
| 1352 | |
Andy Hung | 2ddee19 | 2015-12-18 17:34:44 -0800 | [diff] [blame] | 1353 | status_t AudioSystem::setMasterMono(bool mono) |
| 1354 | { |
| 1355 | const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service(); |
| 1356 | if (aps == 0) return PERMISSION_DENIED; |
| 1357 | return aps->setMasterMono(mono); |
| 1358 | } |
| 1359 | |
| 1360 | status_t AudioSystem::getMasterMono(bool *mono) |
| 1361 | { |
| 1362 | const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service(); |
| 1363 | if (aps == 0) return PERMISSION_DENIED; |
| 1364 | return aps->getMasterMono(mono); |
| 1365 | } |
| 1366 | |
Richard Folke Tullberg | 3fae037 | 2017-01-13 09:04:25 +0100 | [diff] [blame] | 1367 | status_t AudioSystem::setMasterBalance(float balance) |
| 1368 | { |
| 1369 | const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger(); |
| 1370 | if (af == 0) return PERMISSION_DENIED; |
| 1371 | return af->setMasterBalance(balance); |
| 1372 | } |
| 1373 | |
| 1374 | status_t AudioSystem::getMasterBalance(float *balance) |
| 1375 | { |
| 1376 | const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger(); |
| 1377 | if (af == 0) return PERMISSION_DENIED; |
| 1378 | return af->getMasterBalance(balance); |
| 1379 | } |
| 1380 | |
Eric Laurent | ac9cef5 | 2017-06-09 15:46:26 -0700 | [diff] [blame] | 1381 | float AudioSystem::getStreamVolumeDB(audio_stream_type_t stream, int index, audio_devices_t device) |
| 1382 | { |
| 1383 | const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service(); |
| 1384 | if (aps == 0) return NAN; |
| 1385 | return aps->getStreamVolumeDB(stream, index, device); |
| 1386 | } |
| 1387 | |
jiabin | 46a76fa | 2018-01-05 10:18:21 -0800 | [diff] [blame] | 1388 | status_t AudioSystem::getMicrophones(std::vector<media::MicrophoneInfo> *microphones) |
| 1389 | { |
| 1390 | const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger(); |
| 1391 | if (af == 0) return PERMISSION_DENIED; |
| 1392 | return af->getMicrophones(microphones); |
| 1393 | } |
| 1394 | |
jiabin | 8177290 | 2018-04-02 17:52:27 -0700 | [diff] [blame] | 1395 | status_t AudioSystem::getSurroundFormats(unsigned int *numSurroundFormats, |
| 1396 | audio_format_t *surroundFormats, |
| 1397 | bool *surroundFormatsEnabled, |
| 1398 | bool reported) |
| 1399 | { |
| 1400 | const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service(); |
| 1401 | if (aps == 0) return PERMISSION_DENIED; |
| 1402 | return aps->getSurroundFormats( |
| 1403 | numSurroundFormats, surroundFormats, surroundFormatsEnabled, reported); |
| 1404 | } |
| 1405 | |
| 1406 | status_t AudioSystem::setSurroundFormatEnabled(audio_format_t audioFormat, bool enabled) |
| 1407 | { |
| 1408 | const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service(); |
| 1409 | if (aps == 0) return PERMISSION_DENIED; |
| 1410 | return aps->setSurroundFormatEnabled(audioFormat, enabled); |
| 1411 | } |
| 1412 | |
Eric Laurent | b78763e | 2018-10-17 10:08:02 -0700 | [diff] [blame] | 1413 | status_t AudioSystem::setAssistantUid(uid_t uid) |
| 1414 | { |
| 1415 | const sp <IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service(); |
| 1416 | if (aps == 0) return PERMISSION_DENIED; |
| 1417 | |
| 1418 | return aps->setAssistantUid(uid); |
| 1419 | } |
| 1420 | |
| 1421 | status_t AudioSystem::setA11yServicesUids(const std::vector<uid_t>& uids) |
| 1422 | { |
| 1423 | const sp <IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service(); |
| 1424 | if (aps == 0) return PERMISSION_DENIED; |
| 1425 | |
| 1426 | return aps->setA11yServicesUids(uids); |
| 1427 | } |
| 1428 | |
jiabin | 6012f91 | 2018-11-02 17:06:30 -0700 | [diff] [blame] | 1429 | bool AudioSystem::isHapticPlaybackSupported() |
| 1430 | { |
| 1431 | const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service(); |
| 1432 | if (aps == 0) return false; |
| 1433 | return aps->isHapticPlaybackSupported(); |
| 1434 | } |
| 1435 | |
Arun Mirpuri | 11029ad | 2018-12-19 20:45:19 -0800 | [diff] [blame] | 1436 | status_t AudioSystem::getHwOffloadEncodingFormatsSupportedForA2DP( |
François Gaffie | d0ba9ed | 2018-11-05 11:50:42 +0100 | [diff] [blame] | 1437 | std::vector<audio_format_t> *formats) { |
| 1438 | const sp <IAudioPolicyService> |
| 1439 | & aps = AudioSystem::get_audio_policy_service(); |
| 1440 | if (aps == 0) return PERMISSION_DENIED; |
| 1441 | return aps->getHwOffloadEncodingFormatsSupportedForA2DP(formats); |
| 1442 | } |
| 1443 | |
| 1444 | status_t AudioSystem::listAudioProductStrategies(AudioProductStrategyVector &strategies) |
Arun Mirpuri | 11029ad | 2018-12-19 20:45:19 -0800 | [diff] [blame] | 1445 | { |
| 1446 | const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service(); |
| 1447 | if (aps == 0) return PERMISSION_DENIED; |
François Gaffie | d0ba9ed | 2018-11-05 11:50:42 +0100 | [diff] [blame] | 1448 | return aps->listAudioProductStrategies(strategies); |
| 1449 | } |
| 1450 | |
| 1451 | audio_attributes_t AudioSystem::streamTypeToAttributes(audio_stream_type_t stream) |
| 1452 | { |
| 1453 | AudioProductStrategyVector strategies; |
| 1454 | listAudioProductStrategies(strategies); |
| 1455 | for (const auto &strategy : strategies) { |
| 1456 | auto attrVect = strategy.getAudioAttributes(); |
| 1457 | auto iter = std::find_if(begin(attrVect), end(attrVect), [&stream](const auto &attributes) { |
| 1458 | return attributes.getStreamType() == stream; }); |
| 1459 | if (iter != end(attrVect)) { |
| 1460 | return iter->getAttributes(); |
| 1461 | } |
| 1462 | } |
| 1463 | ALOGE("invalid stream type %s when converting to attributes", toString(stream).c_str()); |
| 1464 | return AUDIO_ATTRIBUTES_INITIALIZER; |
| 1465 | } |
| 1466 | |
| 1467 | audio_stream_type_t AudioSystem::attributesToStreamType(const audio_attributes_t &attr) |
| 1468 | { |
François Gaffie | 4b2018b | 2018-11-07 11:18:59 +0100 | [diff] [blame] | 1469 | product_strategy_t psId; |
| 1470 | status_t ret = AudioSystem::getProductStrategyFromAudioAttributes(AudioAttributes(attr), psId); |
| 1471 | if (ret != NO_ERROR) { |
| 1472 | ALOGE("no strategy found for attributes %s", toString(attr).c_str()); |
| 1473 | return AUDIO_STREAM_MUSIC; |
| 1474 | } |
François Gaffie | d0ba9ed | 2018-11-05 11:50:42 +0100 | [diff] [blame] | 1475 | AudioProductStrategyVector strategies; |
| 1476 | listAudioProductStrategies(strategies); |
| 1477 | for (const auto &strategy : strategies) { |
François Gaffie | 4b2018b | 2018-11-07 11:18:59 +0100 | [diff] [blame] | 1478 | if (strategy.getId() == psId) { |
François Gaffie | d0ba9ed | 2018-11-05 11:50:42 +0100 | [diff] [blame] | 1479 | auto attrVect = strategy.getAudioAttributes(); |
| 1480 | auto iter = std::find_if(begin(attrVect), end(attrVect), [&attr](const auto &refAttr) { |
| 1481 | return AudioProductStrategy::attributesMatches( |
| 1482 | refAttr.getAttributes(), attr); }); |
| 1483 | if (iter != end(attrVect)) { |
| 1484 | return iter->getStreamType(); |
| 1485 | } |
| 1486 | } |
| 1487 | } |
| 1488 | ALOGE("invalid attributes %s when converting to stream", toString(attr).c_str()); |
| 1489 | return AUDIO_STREAM_MUSIC; |
| 1490 | } |
| 1491 | |
François Gaffie | 4b2018b | 2018-11-07 11:18:59 +0100 | [diff] [blame] | 1492 | status_t AudioSystem::getProductStrategyFromAudioAttributes(const AudioAttributes &aa, |
| 1493 | product_strategy_t &productStrategy) |
François Gaffie | d0ba9ed | 2018-11-05 11:50:42 +0100 | [diff] [blame] | 1494 | { |
| 1495 | const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service(); |
François Gaffie | 4b2018b | 2018-11-07 11:18:59 +0100 | [diff] [blame] | 1496 | if (aps == 0) return PERMISSION_DENIED; |
| 1497 | return aps->getProductStrategyFromAudioAttributes(aa,productStrategy); |
| 1498 | } |
| 1499 | |
| 1500 | status_t AudioSystem::listAudioVolumeGroups(AudioVolumeGroupVector &groups) |
| 1501 | { |
| 1502 | const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service(); |
| 1503 | if (aps == 0) return PERMISSION_DENIED; |
| 1504 | return aps->listAudioVolumeGroups(groups); |
| 1505 | } |
| 1506 | |
| 1507 | status_t AudioSystem::getVolumeGroupFromAudioAttributes(const AudioAttributes &aa, |
| 1508 | volume_group_t &volumeGroup) |
| 1509 | { |
| 1510 | const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service(); |
| 1511 | if (aps == 0) return PERMISSION_DENIED; |
| 1512 | return aps->getVolumeGroupFromAudioAttributes(aa, volumeGroup); |
Arun Mirpuri | 11029ad | 2018-12-19 20:45:19 -0800 | [diff] [blame] | 1513 | } |
Eric Laurent | b78763e | 2018-10-17 10:08:02 -0700 | [diff] [blame] | 1514 | |
Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 1515 | // --------------------------------------------------------------------------- |
| 1516 | |
Eric Laurent | e8726fe | 2015-06-26 09:39:24 -0700 | [diff] [blame] | 1517 | int AudioSystem::AudioPolicyServiceClient::addAudioPortCallback( |
Eric Laurent | 296fb13 | 2015-05-01 11:38:42 -0700 | [diff] [blame] | 1518 | const sp<AudioPortCallback>& callback) |
Eric Laurent | b28753e | 2015-04-01 13:06:28 -0700 | [diff] [blame] | 1519 | { |
| 1520 | Mutex::Autolock _l(mLock); |
| 1521 | for (size_t i = 0; i < mAudioPortCallbacks.size(); i++) { |
Eric Laurent | 296fb13 | 2015-05-01 11:38:42 -0700 | [diff] [blame] | 1522 | if (mAudioPortCallbacks[i] == callback) { |
Eric Laurent | e8726fe | 2015-06-26 09:39:24 -0700 | [diff] [blame] | 1523 | return -1; |
Eric Laurent | b28753e | 2015-04-01 13:06:28 -0700 | [diff] [blame] | 1524 | } |
| 1525 | } |
Eric Laurent | 296fb13 | 2015-05-01 11:38:42 -0700 | [diff] [blame] | 1526 | mAudioPortCallbacks.add(callback); |
Eric Laurent | e8726fe | 2015-06-26 09:39:24 -0700 | [diff] [blame] | 1527 | return mAudioPortCallbacks.size(); |
Eric Laurent | b28753e | 2015-04-01 13:06:28 -0700 | [diff] [blame] | 1528 | } |
| 1529 | |
Eric Laurent | e8726fe | 2015-06-26 09:39:24 -0700 | [diff] [blame] | 1530 | int AudioSystem::AudioPolicyServiceClient::removeAudioPortCallback( |
Eric Laurent | 296fb13 | 2015-05-01 11:38:42 -0700 | [diff] [blame] | 1531 | const sp<AudioPortCallback>& callback) |
Eric Laurent | b28753e | 2015-04-01 13:06:28 -0700 | [diff] [blame] | 1532 | { |
| 1533 | Mutex::Autolock _l(mLock); |
| 1534 | size_t i; |
| 1535 | for (i = 0; i < mAudioPortCallbacks.size(); i++) { |
Eric Laurent | 296fb13 | 2015-05-01 11:38:42 -0700 | [diff] [blame] | 1536 | if (mAudioPortCallbacks[i] == callback) { |
Eric Laurent | b28753e | 2015-04-01 13:06:28 -0700 | [diff] [blame] | 1537 | break; |
| 1538 | } |
| 1539 | } |
| 1540 | if (i == mAudioPortCallbacks.size()) { |
Eric Laurent | e8726fe | 2015-06-26 09:39:24 -0700 | [diff] [blame] | 1541 | return -1; |
Eric Laurent | b28753e | 2015-04-01 13:06:28 -0700 | [diff] [blame] | 1542 | } |
| 1543 | mAudioPortCallbacks.removeAt(i); |
Eric Laurent | e8726fe | 2015-06-26 09:39:24 -0700 | [diff] [blame] | 1544 | return mAudioPortCallbacks.size(); |
Eric Laurent | b28753e | 2015-04-01 13:06:28 -0700 | [diff] [blame] | 1545 | } |
| 1546 | |
Eric Laurent | 296fb13 | 2015-05-01 11:38:42 -0700 | [diff] [blame] | 1547 | |
Eric Laurent | b28753e | 2015-04-01 13:06:28 -0700 | [diff] [blame] | 1548 | void AudioSystem::AudioPolicyServiceClient::onAudioPortListUpdate() |
| 1549 | { |
| 1550 | Mutex::Autolock _l(mLock); |
| 1551 | for (size_t i = 0; i < mAudioPortCallbacks.size(); i++) { |
| 1552 | mAudioPortCallbacks[i]->onAudioPortListUpdate(); |
| 1553 | } |
| 1554 | } |
| 1555 | |
| 1556 | void AudioSystem::AudioPolicyServiceClient::onAudioPatchListUpdate() |
| 1557 | { |
| 1558 | Mutex::Autolock _l(mLock); |
| 1559 | for (size_t i = 0; i < mAudioPortCallbacks.size(); i++) { |
| 1560 | mAudioPortCallbacks[i]->onAudioPatchListUpdate(); |
| 1561 | } |
| 1562 | } |
| 1563 | |
François Gaffie | cfe1732 | 2018-11-07 13:41:29 +0100 | [diff] [blame] | 1564 | // ---------------------------------------------------------------------------- |
| 1565 | int AudioSystem::AudioPolicyServiceClient::addAudioVolumeGroupCallback( |
| 1566 | const sp<AudioVolumeGroupCallback>& callback) |
| 1567 | { |
| 1568 | Mutex::Autolock _l(mLock); |
| 1569 | for (size_t i = 0; i < mAudioVolumeGroupCallback.size(); i++) { |
| 1570 | if (mAudioVolumeGroupCallback[i] == callback) { |
| 1571 | return -1; |
| 1572 | } |
| 1573 | } |
| 1574 | mAudioVolumeGroupCallback.add(callback); |
| 1575 | return mAudioVolumeGroupCallback.size(); |
| 1576 | } |
| 1577 | |
| 1578 | int AudioSystem::AudioPolicyServiceClient::removeAudioVolumeGroupCallback( |
| 1579 | const sp<AudioVolumeGroupCallback>& callback) |
| 1580 | { |
| 1581 | Mutex::Autolock _l(mLock); |
| 1582 | size_t i; |
| 1583 | for (i = 0; i < mAudioVolumeGroupCallback.size(); i++) { |
| 1584 | if (mAudioVolumeGroupCallback[i] == callback) { |
| 1585 | break; |
| 1586 | } |
| 1587 | } |
| 1588 | if (i == mAudioVolumeGroupCallback.size()) { |
| 1589 | return -1; |
| 1590 | } |
| 1591 | mAudioVolumeGroupCallback.removeAt(i); |
| 1592 | return mAudioVolumeGroupCallback.size(); |
| 1593 | } |
| 1594 | |
| 1595 | void AudioSystem::AudioPolicyServiceClient::onAudioVolumeGroupChanged(volume_group_t group, |
| 1596 | int flags) |
| 1597 | { |
| 1598 | Mutex::Autolock _l(mLock); |
| 1599 | for (size_t i = 0; i < mAudioVolumeGroupCallback.size(); i++) { |
| 1600 | mAudioVolumeGroupCallback[i]->onAudioVolumeGroupChanged(group, flags); |
| 1601 | } |
| 1602 | } |
| 1603 | // ---------------------------------------------------------------------------- |
| 1604 | |
Jean-Michel Trivi | de80105 | 2015-04-14 19:10:14 -0700 | [diff] [blame] | 1605 | void AudioSystem::AudioPolicyServiceClient::onDynamicPolicyMixStateUpdate( |
| 1606 | String8 regId, int32_t state) |
| 1607 | { |
Jean-Michel Trivi | f613d42 | 2015-04-23 18:41:29 -0700 | [diff] [blame] | 1608 | ALOGV("AudioPolicyServiceClient::onDynamicPolicyMixStateUpdate(%s, %d)", regId.string(), state); |
| 1609 | dynamic_policy_callback cb = NULL; |
| 1610 | { |
| 1611 | Mutex::Autolock _l(AudioSystem::gLock); |
| 1612 | cb = gDynPolicyCallback; |
| 1613 | } |
| 1614 | |
| 1615 | if (cb != NULL) { |
| 1616 | cb(DYNAMIC_POLICY_EVENT_MIX_STATE_UPDATE, regId, state); |
| 1617 | } |
Jean-Michel Trivi | de80105 | 2015-04-14 19:10:14 -0700 | [diff] [blame] | 1618 | } |
| 1619 | |
Jean-Michel Trivi | 2f4fe9f | 2015-12-04 16:20:59 -0800 | [diff] [blame] | 1620 | void AudioSystem::AudioPolicyServiceClient::onRecordingConfigurationUpdate( |
Eric Laurent | a9f8665 | 2018-11-28 17:23:11 -0800 | [diff] [blame] | 1621 | int event, |
| 1622 | const record_client_info_t *clientInfo, |
| 1623 | const audio_config_base_t *clientConfig, |
| 1624 | std::vector<effect_descriptor_t> clientEffects, |
| 1625 | const audio_config_base_t *deviceConfig, |
| 1626 | std::vector<effect_descriptor_t> effects, |
| 1627 | audio_patch_handle_t patchHandle, |
| 1628 | audio_source_t source) { |
Jean-Michel Trivi | 2f4fe9f | 2015-12-04 16:20:59 -0800 | [diff] [blame] | 1629 | record_config_callback cb = NULL; |
| 1630 | { |
| 1631 | Mutex::Autolock _l(AudioSystem::gLock); |
| 1632 | cb = gRecordConfigCallback; |
| 1633 | } |
| 1634 | |
| 1635 | if (cb != NULL) { |
Eric Laurent | a9f8665 | 2018-11-28 17:23:11 -0800 | [diff] [blame] | 1636 | cb(event, clientInfo, clientConfig, clientEffects, |
| 1637 | deviceConfig, effects, patchHandle, source); |
Jean-Michel Trivi | 2f4fe9f | 2015-12-04 16:20:59 -0800 | [diff] [blame] | 1638 | } |
| 1639 | } |
| 1640 | |
Glenn Kasten | 4944acb | 2013-08-19 08:39:20 -0700 | [diff] [blame] | 1641 | void AudioSystem::AudioPolicyServiceClient::binderDied(const wp<IBinder>& who __unused) |
| 1642 | { |
Glenn Kasten | d2d089f | 2014-11-05 11:48:12 -0800 | [diff] [blame] | 1643 | { |
Eric Laurent | b28753e | 2015-04-01 13:06:28 -0700 | [diff] [blame] | 1644 | Mutex::Autolock _l(mLock); |
| 1645 | for (size_t i = 0; i < mAudioPortCallbacks.size(); i++) { |
| 1646 | mAudioPortCallbacks[i]->onServiceDied(); |
Glenn Kasten | d2d089f | 2014-11-05 11:48:12 -0800 | [diff] [blame] | 1647 | } |
François Gaffie | cfe1732 | 2018-11-07 13:41:29 +0100 | [diff] [blame] | 1648 | for (size_t i = 0; i < mAudioVolumeGroupCallback.size(); i++) { |
| 1649 | mAudioVolumeGroupCallback[i]->onServiceDied(); |
| 1650 | } |
Eric Laurent | b52c152 | 2014-05-20 11:27:36 -0700 | [diff] [blame] | 1651 | } |
Glenn Kasten | d2d089f | 2014-11-05 11:48:12 -0800 | [diff] [blame] | 1652 | { |
| 1653 | Mutex::Autolock _l(gLockAPS); |
| 1654 | AudioSystem::gAudioPolicyService.clear(); |
| 1655 | } |
Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 1656 | |
Steve Block | 5ff1dd5 | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 1657 | ALOGW("AudioPolicyService server died!"); |
Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 1658 | } |
| 1659 | |
Glenn Kasten | 40bc906 | 2015-03-20 09:09:33 -0700 | [diff] [blame] | 1660 | } // namespace android |