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 | { |
Eric Laurent | be916aa | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 431 | const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger(); |
Eric Laurent | de3f839 | 2014-07-27 18:38:22 -0700 | [diff] [blame] | 432 | if (af == 0) return AUDIO_UNIQUE_ID_ALLOCATE; |
Glenn Kasten | eeecb98 | 2016-02-26 10:44:04 -0800 | [diff] [blame] | 433 | return af->newAudioUniqueId(use); |
Eric Laurent | be916aa | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 434 | } |
| 435 | |
Glenn Kasten | d848eb4 | 2016-03-08 13:42:11 -0800 | [diff] [blame] | 436 | void AudioSystem::acquireAudioSessionId(audio_session_t audioSession, pid_t pid) |
Glenn Kasten | 4944acb | 2013-08-19 08:39:20 -0700 | [diff] [blame] | 437 | { |
Marco Nelissen | 3a34bef | 2011-08-02 13:33:41 -0700 | [diff] [blame] | 438 | const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger(); |
| 439 | if (af != 0) { |
Marco Nelissen | d457c97 | 2014-02-11 08:47:07 -0800 | [diff] [blame] | 440 | af->acquireAudioSessionId(audioSession, pid); |
Marco Nelissen | 3a34bef | 2011-08-02 13:33:41 -0700 | [diff] [blame] | 441 | } |
| 442 | } |
| 443 | |
Glenn Kasten | d848eb4 | 2016-03-08 13:42:11 -0800 | [diff] [blame] | 444 | void AudioSystem::releaseAudioSessionId(audio_session_t audioSession, pid_t pid) |
Glenn Kasten | 4944acb | 2013-08-19 08:39:20 -0700 | [diff] [blame] | 445 | { |
Marco Nelissen | 3a34bef | 2011-08-02 13:33:41 -0700 | [diff] [blame] | 446 | const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger(); |
| 447 | if (af != 0) { |
Marco Nelissen | d457c97 | 2014-02-11 08:47:07 -0800 | [diff] [blame] | 448 | af->releaseAudioSessionId(audioSession, pid); |
Marco Nelissen | 3a34bef | 2011-08-02 13:33:41 -0700 | [diff] [blame] | 449 | } |
| 450 | } |
| 451 | |
Eric Laurent | 93c3d41 | 2014-08-01 14:48:35 -0700 | [diff] [blame] | 452 | audio_hw_sync_t AudioSystem::getAudioHwSyncForSession(audio_session_t sessionId) |
| 453 | { |
| 454 | const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger(); |
| 455 | if (af == 0) return AUDIO_HW_SYNC_INVALID; |
| 456 | return af->getAudioHwSyncForSession(sessionId); |
| 457 | } |
| 458 | |
Eric Laurent | 72e3f39 | 2015-05-20 14:43:50 -0700 | [diff] [blame] | 459 | status_t AudioSystem::systemReady() |
| 460 | { |
| 461 | const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger(); |
| 462 | if (af == 0) return NO_INIT; |
| 463 | return af->systemReady(); |
| 464 | } |
| 465 | |
Glenn Kasten | 4a8308b | 2016-04-18 14:10:01 -0700 | [diff] [blame] | 466 | status_t AudioSystem::getFrameCountHAL(audio_io_handle_t ioHandle, |
| 467 | size_t* frameCount) |
| 468 | { |
| 469 | const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger(); |
| 470 | if (af == 0) return PERMISSION_DENIED; |
| 471 | sp<AudioIoDescriptor> desc = getIoDescriptor(ioHandle); |
| 472 | if (desc == 0) { |
| 473 | *frameCount = af->frameCountHAL(ioHandle); |
| 474 | } else { |
| 475 | *frameCount = desc->mFrameCountHAL; |
| 476 | } |
| 477 | if (*frameCount == 0) { |
| 478 | ALOGE("AudioSystem::getFrameCountHAL failed for ioHandle %d", ioHandle); |
| 479 | return BAD_VALUE; |
| 480 | } |
| 481 | |
| 482 | ALOGV("getFrameCountHAL() ioHandle %d, frameCount %zu", ioHandle, *frameCount); |
| 483 | |
| 484 | return NO_ERROR; |
| 485 | } |
| 486 | |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 487 | // --------------------------------------------------------------------------- |
| 488 | |
Eric Laurent | 73e26b6 | 2015-04-27 16:55:58 -0700 | [diff] [blame] | 489 | |
| 490 | void AudioSystem::AudioFlingerClient::clearIoCache() |
| 491 | { |
| 492 | Mutex::Autolock _l(mLock); |
| 493 | mIoDescriptors.clear(); |
| 494 | mInBuffSize = 0; |
| 495 | mInSamplingRate = 0; |
| 496 | mInFormat = AUDIO_FORMAT_DEFAULT; |
| 497 | mInChannelMask = AUDIO_CHANNEL_NONE; |
| 498 | } |
| 499 | |
Glenn Kasten | 4944acb | 2013-08-19 08:39:20 -0700 | [diff] [blame] | 500 | void AudioSystem::AudioFlingerClient::binderDied(const wp<IBinder>& who __unused) |
| 501 | { |
Eric Laurent | f6778fd | 2014-11-18 17:26:58 -0800 | [diff] [blame] | 502 | audio_error_callback cb = NULL; |
| 503 | { |
| 504 | Mutex::Autolock _l(AudioSystem::gLock); |
| 505 | AudioSystem::gAudioFlinger.clear(); |
| 506 | cb = gAudioErrorCallback; |
| 507 | } |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 508 | |
Eric Laurent | 73e26b6 | 2015-04-27 16:55:58 -0700 | [diff] [blame] | 509 | // clear output handles and stream to output map caches |
| 510 | clearIoCache(); |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 511 | |
Eric Laurent | f6778fd | 2014-11-18 17:26:58 -0800 | [diff] [blame] | 512 | if (cb) { |
| 513 | cb(DEAD_OBJECT); |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 514 | } |
Steve Block | 5ff1dd5 | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 515 | ALOGW("AudioFlinger server died!"); |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 516 | } |
| 517 | |
Eric Laurent | 73e26b6 | 2015-04-27 16:55:58 -0700 | [diff] [blame] | 518 | void AudioSystem::AudioFlingerClient::ioConfigChanged(audio_io_config_event event, |
| 519 | const sp<AudioIoDescriptor>& ioDesc) { |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 520 | ALOGV("ioConfigChanged() event %d", event); |
Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 521 | |
Eric Laurent | 73e26b6 | 2015-04-27 16:55:58 -0700 | [diff] [blame] | 522 | if (ioDesc == 0 || ioDesc->mIoHandle == AUDIO_IO_HANDLE_NONE) return; |
Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 523 | |
Eric Laurent | 296fb13 | 2015-05-01 11:38:42 -0700 | [diff] [blame] | 524 | audio_port_handle_t deviceId = AUDIO_PORT_HANDLE_NONE; |
Eric Laurent | 4463ff5 | 2019-02-07 13:56:09 -0800 | [diff] [blame^] | 525 | Vector<sp<AudioDeviceCallback>> callbacksToCall; |
Eric Laurent | 296fb13 | 2015-05-01 11:38:42 -0700 | [diff] [blame] | 526 | { |
| 527 | Mutex::Autolock _l(mLock); |
Eric Laurent | 4463ff5 | 2019-02-07 13:56:09 -0800 | [diff] [blame^] | 528 | bool deviceValidOrChanged = false; |
| 529 | bool sendCallbacks = false; |
| 530 | ssize_t ioIndex = -1; |
Eric Laurent | 296fb13 | 2015-05-01 11:38:42 -0700 | [diff] [blame] | 531 | |
| 532 | switch (event) { |
| 533 | case AUDIO_OUTPUT_OPENED: |
Eric Laurent | ad2e7b9 | 2017-09-14 20:06:42 -0700 | [diff] [blame] | 534 | case AUDIO_OUTPUT_REGISTERED: |
| 535 | case AUDIO_INPUT_OPENED: |
| 536 | case AUDIO_INPUT_REGISTERED: { |
Praveen Chavan | 49fdeaf | 2015-09-29 02:25:47 -0700 | [diff] [blame] | 537 | sp<AudioIoDescriptor> oldDesc = getIoDescriptor_l(ioDesc->mIoHandle); |
Eric Laurent | 7c1ec5f | 2015-07-09 14:52:47 -0700 | [diff] [blame] | 538 | if (oldDesc == 0) { |
| 539 | mIoDescriptors.add(ioDesc->mIoHandle, ioDesc); |
| 540 | } else { |
| 541 | deviceId = oldDesc->getDeviceId(); |
| 542 | mIoDescriptors.replaceValueFor(ioDesc->mIoHandle, ioDesc); |
Eric Laurent | 296fb13 | 2015-05-01 11:38:42 -0700 | [diff] [blame] | 543 | } |
Eric Laurent | 296fb13 | 2015-05-01 11:38:42 -0700 | [diff] [blame] | 544 | |
| 545 | if (ioDesc->getDeviceId() != AUDIO_PORT_HANDLE_NONE) { |
| 546 | deviceId = ioDesc->getDeviceId(); |
Eric Laurent | ad2e7b9 | 2017-09-14 20:06:42 -0700 | [diff] [blame] | 547 | if (event == AUDIO_OUTPUT_OPENED || event == AUDIO_INPUT_OPENED) { |
Eric Laurent | 4463ff5 | 2019-02-07 13:56:09 -0800 | [diff] [blame^] | 548 | ioIndex = mAudioDeviceCallbackProxies.indexOfKey(ioDesc->mIoHandle); |
Eric Laurent | ad2e7b9 | 2017-09-14 20:06:42 -0700 | [diff] [blame] | 549 | if (ioIndex >= 0) { |
Eric Laurent | 4463ff5 | 2019-02-07 13:56:09 -0800 | [diff] [blame^] | 550 | sendCallbacks = true; |
Francois Gaffie | 24a9fb0 | 2019-01-18 17:51:34 +0100 | [diff] [blame] | 551 | deviceValidOrChanged = true; |
| 552 | } |
| 553 | } |
| 554 | if (event == AUDIO_OUTPUT_REGISTERED || event == AUDIO_INPUT_REGISTERED) { |
Eric Laurent | 4463ff5 | 2019-02-07 13:56:09 -0800 | [diff] [blame^] | 555 | ioIndex = mAudioDeviceCallbackProxies.indexOfKey(ioDesc->mIoHandle); |
| 556 | sendCallbacks = (ioIndex >= 0) |
| 557 | && !mAudioDeviceCallbackProxies.valueAt(ioIndex).notifiedOnce(); |
Eric Laurent | 296fb13 | 2015-05-01 11:38:42 -0700 | [diff] [blame] | 558 | } |
| 559 | } |
Eric Laurent | ad2e7b9 | 2017-09-14 20:06:42 -0700 | [diff] [blame] | 560 | ALOGV("ioConfigChanged() new %s %s %d samplingRate %u, format %#x channel mask %#x " |
| 561 | "frameCount %zu deviceId %d", |
| 562 | event == AUDIO_OUTPUT_OPENED || event == AUDIO_OUTPUT_REGISTERED ? |
| 563 | "output" : "input", |
| 564 | event == AUDIO_OUTPUT_OPENED || event == AUDIO_INPUT_OPENED ? |
| 565 | "opened" : "registered", |
Eric Laurent | 296fb13 | 2015-05-01 11:38:42 -0700 | [diff] [blame] | 566 | ioDesc->mIoHandle, ioDesc->mSamplingRate, ioDesc->mFormat, ioDesc->mChannelMask, |
| 567 | ioDesc->mFrameCount, ioDesc->getDeviceId()); |
| 568 | } break; |
| 569 | case AUDIO_OUTPUT_CLOSED: |
| 570 | case AUDIO_INPUT_CLOSED: { |
Praveen Chavan | 49fdeaf | 2015-09-29 02:25:47 -0700 | [diff] [blame] | 571 | if (getIoDescriptor_l(ioDesc->mIoHandle) == 0) { |
Eric Laurent | 296fb13 | 2015-05-01 11:38:42 -0700 | [diff] [blame] | 572 | ALOGW("ioConfigChanged() closing unknown %s %d", |
| 573 | event == AUDIO_OUTPUT_CLOSED ? "output" : "input", ioDesc->mIoHandle); |
| 574 | break; |
| 575 | } |
| 576 | ALOGV("ioConfigChanged() %s %d closed", |
Eric Laurent | 73e26b6 | 2015-04-27 16:55:58 -0700 | [diff] [blame] | 577 | event == AUDIO_OUTPUT_CLOSED ? "output" : "input", ioDesc->mIoHandle); |
Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 578 | |
Eric Laurent | 296fb13 | 2015-05-01 11:38:42 -0700 | [diff] [blame] | 579 | mIoDescriptors.removeItem(ioDesc->mIoHandle); |
Eric Laurent | 4463ff5 | 2019-02-07 13:56:09 -0800 | [diff] [blame^] | 580 | mAudioDeviceCallbackProxies.removeItem(ioDesc->mIoHandle); |
Eric Laurent | 296fb13 | 2015-05-01 11:38:42 -0700 | [diff] [blame] | 581 | } break; |
| 582 | |
| 583 | case AUDIO_OUTPUT_CONFIG_CHANGED: |
| 584 | case AUDIO_INPUT_CONFIG_CHANGED: { |
Praveen Chavan | 49fdeaf | 2015-09-29 02:25:47 -0700 | [diff] [blame] | 585 | sp<AudioIoDescriptor> oldDesc = getIoDescriptor_l(ioDesc->mIoHandle); |
Eric Laurent | 296fb13 | 2015-05-01 11:38:42 -0700 | [diff] [blame] | 586 | if (oldDesc == 0) { |
| 587 | ALOGW("ioConfigChanged() modifying unknown output! %d", ioDesc->mIoHandle); |
| 588 | break; |
| 589 | } |
| 590 | |
| 591 | deviceId = oldDesc->getDeviceId(); |
| 592 | mIoDescriptors.replaceValueFor(ioDesc->mIoHandle, ioDesc); |
| 593 | |
| 594 | if (deviceId != ioDesc->getDeviceId()) { |
Francois Gaffie | 24a9fb0 | 2019-01-18 17:51:34 +0100 | [diff] [blame] | 595 | deviceValidOrChanged = true; |
Eric Laurent | 296fb13 | 2015-05-01 11:38:42 -0700 | [diff] [blame] | 596 | deviceId = ioDesc->getDeviceId(); |
Eric Laurent | 4463ff5 | 2019-02-07 13:56:09 -0800 | [diff] [blame^] | 597 | ioIndex = mAudioDeviceCallbackProxies.indexOfKey(ioDesc->mIoHandle); |
| 598 | sendCallbacks = ioIndex >= 0; |
Eric Laurent | 296fb13 | 2015-05-01 11:38:42 -0700 | [diff] [blame] | 599 | } |
| 600 | ALOGV("ioConfigChanged() new config for %s %d samplingRate %u, format %#x " |
Glenn Kasten | 4a8308b | 2016-04-18 14:10:01 -0700 | [diff] [blame] | 601 | "channel mask %#x frameCount %zu frameCountHAL %zu deviceId %d", |
Eric Laurent | 296fb13 | 2015-05-01 11:38:42 -0700 | [diff] [blame] | 602 | event == AUDIO_OUTPUT_CONFIG_CHANGED ? "output" : "input", |
| 603 | ioDesc->mIoHandle, ioDesc->mSamplingRate, ioDesc->mFormat, |
Glenn Kasten | d3bb645 | 2016-12-05 18:14:37 -0800 | [diff] [blame] | 604 | ioDesc->mChannelMask, ioDesc->mFrameCount, ioDesc->mFrameCountHAL, |
| 605 | ioDesc->getDeviceId()); |
Eric Laurent | 296fb13 | 2015-05-01 11:38:42 -0700 | [diff] [blame] | 606 | |
Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 607 | } break; |
Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 608 | } |
Eric Laurent | 4463ff5 | 2019-02-07 13:56:09 -0800 | [diff] [blame^] | 609 | |
| 610 | // sendCallbacks true => ioDesc->mIoHandle and deviceId are valid |
| 611 | if (sendCallbacks) { |
| 612 | AudioDeviceCallbackProxies &callbackProxies = |
| 613 | mAudioDeviceCallbackProxies.editValueAt(ioIndex); |
| 614 | for (size_t i = 0; i < callbackProxies.size(); ) { |
| 615 | sp<AudioDeviceCallback> callback = callbackProxies[i]->callback(); |
| 616 | if (callback.get() != nullptr) { |
| 617 | // Call the callback only if the device actually changed, the input or output |
| 618 | // was opened or closed or the client was newly registered and the callback |
| 619 | // was never called |
| 620 | if (!callbackProxies[i]->notifiedOnce() || deviceValidOrChanged) { |
| 621 | callbacksToCall.add(callback); |
| 622 | callbackProxies[i]->setNotifiedOnce(); |
| 623 | } |
| 624 | i++; |
| 625 | } else { |
| 626 | callbackProxies.removeAt(i); |
Francois Gaffie | 24a9fb0 | 2019-01-18 17:51:34 +0100 | [diff] [blame] | 627 | } |
Francois Gaffie | 24a9fb0 | 2019-01-18 17:51:34 +0100 | [diff] [blame] | 628 | } |
Eric Laurent | 4463ff5 | 2019-02-07 13:56:09 -0800 | [diff] [blame^] | 629 | callbackProxies.setNotifiedOnce(); |
Eric Laurent | ad2e7b9 | 2017-09-14 20:06:42 -0700 | [diff] [blame] | 630 | } |
Eric Laurent | 4463ff5 | 2019-02-07 13:56:09 -0800 | [diff] [blame^] | 631 | } |
| 632 | |
| 633 | // Callbacks must be called without mLock held. May lead to dead lock if calling for |
| 634 | // example getRoutedDevice that updates the device and tries to acquire mLock. |
| 635 | for (size_t i = 0; i < callbacksToCall.size(); i++) { |
| 636 | callbacksToCall[i]->onAudioDeviceUpdate(ioDesc->mIoHandle, deviceId); |
Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 637 | } |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 638 | } |
| 639 | |
Eric Laurent | 73e26b6 | 2015-04-27 16:55:58 -0700 | [diff] [blame] | 640 | status_t AudioSystem::AudioFlingerClient::getInputBufferSize( |
| 641 | uint32_t sampleRate, audio_format_t format, |
| 642 | audio_channel_mask_t channelMask, size_t* buffSize) |
| 643 | { |
| 644 | const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger(); |
| 645 | if (af == 0) { |
| 646 | return PERMISSION_DENIED; |
| 647 | } |
| 648 | Mutex::Autolock _l(mLock); |
| 649 | // Do we have a stale mInBuffSize or are we requesting the input buffer size for new values |
| 650 | if ((mInBuffSize == 0) || (sampleRate != mInSamplingRate) || (format != mInFormat) |
| 651 | || (channelMask != mInChannelMask)) { |
| 652 | size_t inBuffSize = af->getInputBufferSize(sampleRate, format, channelMask); |
| 653 | if (inBuffSize == 0) { |
Glenn Kasten | 49f36ba | 2017-12-06 13:02:02 -0800 | [diff] [blame] | 654 | ALOGE("AudioSystem::getInputBufferSize failed sampleRate %d format %#x channelMask %#x", |
Eric Laurent | 73e26b6 | 2015-04-27 16:55:58 -0700 | [diff] [blame] | 655 | sampleRate, format, channelMask); |
| 656 | return BAD_VALUE; |
| 657 | } |
| 658 | // A benign race is possible here: we could overwrite a fresher cache entry |
| 659 | // save the request params |
| 660 | mInSamplingRate = sampleRate; |
| 661 | mInFormat = format; |
| 662 | mInChannelMask = channelMask; |
| 663 | |
| 664 | mInBuffSize = inBuffSize; |
| 665 | } |
| 666 | |
| 667 | *buffSize = mInBuffSize; |
| 668 | |
| 669 | return NO_ERROR; |
| 670 | } |
| 671 | |
Praveen Chavan | 49fdeaf | 2015-09-29 02:25:47 -0700 | [diff] [blame] | 672 | sp<AudioIoDescriptor> AudioSystem::AudioFlingerClient::getIoDescriptor_l(audio_io_handle_t ioHandle) |
Eric Laurent | 73e26b6 | 2015-04-27 16:55:58 -0700 | [diff] [blame] | 673 | { |
| 674 | sp<AudioIoDescriptor> desc; |
| 675 | ssize_t index = mIoDescriptors.indexOfKey(ioHandle); |
| 676 | if (index >= 0) { |
| 677 | desc = mIoDescriptors.valueAt(index); |
| 678 | } |
| 679 | return desc; |
| 680 | } |
| 681 | |
Praveen Chavan | 49fdeaf | 2015-09-29 02:25:47 -0700 | [diff] [blame] | 682 | sp<AudioIoDescriptor> AudioSystem::AudioFlingerClient::getIoDescriptor(audio_io_handle_t ioHandle) |
| 683 | { |
| 684 | Mutex::Autolock _l(mLock); |
| 685 | return getIoDescriptor_l(ioHandle); |
| 686 | } |
| 687 | |
Eric Laurent | 296fb13 | 2015-05-01 11:38:42 -0700 | [diff] [blame] | 688 | status_t AudioSystem::AudioFlingerClient::addAudioDeviceCallback( |
Eric Laurent | ad2e7b9 | 2017-09-14 20:06:42 -0700 | [diff] [blame] | 689 | const wp<AudioDeviceCallback>& callback, audio_io_handle_t audioIo) |
Eric Laurent | 296fb13 | 2015-05-01 11:38:42 -0700 | [diff] [blame] | 690 | { |
Eric Laurent | 4463ff5 | 2019-02-07 13:56:09 -0800 | [diff] [blame^] | 691 | Mutex::Autolock _l(mLock); |
| 692 | AudioDeviceCallbackProxies callbackProxies; |
| 693 | ssize_t ioIndex = mAudioDeviceCallbackProxies.indexOfKey(audioIo); |
Eric Laurent | 296fb13 | 2015-05-01 11:38:42 -0700 | [diff] [blame] | 694 | if (ioIndex >= 0) { |
Eric Laurent | 4463ff5 | 2019-02-07 13:56:09 -0800 | [diff] [blame^] | 695 | callbackProxies = mAudioDeviceCallbackProxies.valueAt(ioIndex); |
Eric Laurent | 296fb13 | 2015-05-01 11:38:42 -0700 | [diff] [blame] | 696 | } |
| 697 | |
Eric Laurent | 4463ff5 | 2019-02-07 13:56:09 -0800 | [diff] [blame^] | 698 | for (size_t cbIndex = 0; cbIndex < callbackProxies.size(); cbIndex++) { |
| 699 | sp<AudioDeviceCallback> cbk = callbackProxies[cbIndex]->callback(); |
| 700 | if (cbk.get() == callback.unsafe_get()) { |
Eric Laurent | 296fb13 | 2015-05-01 11:38:42 -0700 | [diff] [blame] | 701 | return INVALID_OPERATION; |
| 702 | } |
| 703 | } |
Eric Laurent | 4463ff5 | 2019-02-07 13:56:09 -0800 | [diff] [blame^] | 704 | callbackProxies.add(new AudioDeviceCallbackProxy(callback)); |
| 705 | callbackProxies.resetNotifiedOnce(); |
| 706 | mAudioDeviceCallbackProxies.replaceValueFor(audioIo, callbackProxies); |
Eric Laurent | 296fb13 | 2015-05-01 11:38:42 -0700 | [diff] [blame] | 707 | return NO_ERROR; |
| 708 | } |
| 709 | |
| 710 | status_t AudioSystem::AudioFlingerClient::removeAudioDeviceCallback( |
Eric Laurent | ad2e7b9 | 2017-09-14 20:06:42 -0700 | [diff] [blame] | 711 | const wp<AudioDeviceCallback>& callback, audio_io_handle_t audioIo) |
Eric Laurent | 296fb13 | 2015-05-01 11:38:42 -0700 | [diff] [blame] | 712 | { |
Eric Laurent | 4463ff5 | 2019-02-07 13:56:09 -0800 | [diff] [blame^] | 713 | Mutex::Autolock _l(mLock); |
| 714 | ssize_t ioIndex = mAudioDeviceCallbackProxies.indexOfKey(audioIo); |
Eric Laurent | 296fb13 | 2015-05-01 11:38:42 -0700 | [diff] [blame] | 715 | if (ioIndex < 0) { |
| 716 | return INVALID_OPERATION; |
| 717 | } |
Eric Laurent | 4463ff5 | 2019-02-07 13:56:09 -0800 | [diff] [blame^] | 718 | AudioDeviceCallbackProxies callbackProxies = mAudioDeviceCallbackProxies.valueAt(ioIndex); |
Eric Laurent | 296fb13 | 2015-05-01 11:38:42 -0700 | [diff] [blame] | 719 | size_t cbIndex; |
Eric Laurent | 4463ff5 | 2019-02-07 13:56:09 -0800 | [diff] [blame^] | 720 | for (cbIndex = 0; cbIndex < callbackProxies.size(); cbIndex++) { |
| 721 | sp<AudioDeviceCallback> cbk = callbackProxies[cbIndex]->callback(); |
| 722 | if (cbk.get() == callback.unsafe_get()) { |
Eric Laurent | 296fb13 | 2015-05-01 11:38:42 -0700 | [diff] [blame] | 723 | break; |
| 724 | } |
| 725 | } |
Eric Laurent | 4463ff5 | 2019-02-07 13:56:09 -0800 | [diff] [blame^] | 726 | if (cbIndex == callbackProxies.size()) { |
Eric Laurent | 296fb13 | 2015-05-01 11:38:42 -0700 | [diff] [blame] | 727 | return INVALID_OPERATION; |
| 728 | } |
Eric Laurent | 4463ff5 | 2019-02-07 13:56:09 -0800 | [diff] [blame^] | 729 | callbackProxies.removeAt(cbIndex); |
| 730 | if (callbackProxies.size() != 0) { |
| 731 | mAudioDeviceCallbackProxies.replaceValueFor(audioIo, callbackProxies); |
Eric Laurent | 296fb13 | 2015-05-01 11:38:42 -0700 | [diff] [blame] | 732 | } else { |
Eric Laurent | 4463ff5 | 2019-02-07 13:56:09 -0800 | [diff] [blame^] | 733 | mAudioDeviceCallbackProxies.removeItem(audioIo); |
Eric Laurent | 296fb13 | 2015-05-01 11:38:42 -0700 | [diff] [blame] | 734 | } |
| 735 | return NO_ERROR; |
| 736 | } |
| 737 | |
| 738 | /* static */ void AudioSystem::setErrorCallback(audio_error_callback cb) |
Glenn Kasten | 4944acb | 2013-08-19 08:39:20 -0700 | [diff] [blame] | 739 | { |
Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 740 | Mutex::Autolock _l(gLock); |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 741 | gAudioErrorCallback = cb; |
| 742 | } |
| 743 | |
Jean-Michel Trivi | f613d42 | 2015-04-23 18:41:29 -0700 | [diff] [blame] | 744 | /*static*/ void AudioSystem::setDynPolicyCallback(dynamic_policy_callback cb) |
| 745 | { |
| 746 | Mutex::Autolock _l(gLock); |
| 747 | gDynPolicyCallback = cb; |
| 748 | } |
| 749 | |
Jean-Michel Trivi | 2f4fe9f | 2015-12-04 16:20:59 -0800 | [diff] [blame] | 750 | /*static*/ void AudioSystem::setRecordConfigCallback(record_config_callback cb) |
| 751 | { |
| 752 | Mutex::Autolock _l(gLock); |
| 753 | gRecordConfigCallback = cb; |
| 754 | } |
| 755 | |
Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 756 | // client singleton for AudioPolicyService binder interface |
Glenn Kasten | d2d089f | 2014-11-05 11:48:12 -0800 | [diff] [blame] | 757 | // protected by gLockAPS |
Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 758 | sp<IAudioPolicyService> AudioSystem::gAudioPolicyService; |
| 759 | sp<AudioSystem::AudioPolicyServiceClient> AudioSystem::gAudioPolicyServiceClient; |
| 760 | |
| 761 | |
Glenn Kasten | 18a6d90 | 2012-09-24 11:27:56 -0700 | [diff] [blame] | 762 | // establish binder interface to AudioPolicy service |
Eric Laurent | 0ebd5f9 | 2014-11-19 19:04:52 -0800 | [diff] [blame] | 763 | const sp<IAudioPolicyService> AudioSystem::get_audio_policy_service() |
Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 764 | { |
Eric Laurent | 0ebd5f9 | 2014-11-19 19:04:52 -0800 | [diff] [blame] | 765 | sp<IAudioPolicyService> ap; |
| 766 | sp<AudioPolicyServiceClient> apc; |
| 767 | { |
| 768 | Mutex::Autolock _l(gLockAPS); |
| 769 | if (gAudioPolicyService == 0) { |
| 770 | sp<IServiceManager> sm = defaultServiceManager(); |
| 771 | sp<IBinder> binder; |
| 772 | do { |
| 773 | binder = sm->getService(String16("media.audio_policy")); |
| 774 | if (binder != 0) |
| 775 | break; |
| 776 | ALOGW("AudioPolicyService not published, waiting..."); |
| 777 | usleep(500000); // 0.5 s |
| 778 | } while (true); |
| 779 | if (gAudioPolicyServiceClient == NULL) { |
| 780 | gAudioPolicyServiceClient = new AudioPolicyServiceClient(); |
| 781 | } |
| 782 | binder->linkToDeath(gAudioPolicyServiceClient); |
| 783 | gAudioPolicyService = interface_cast<IAudioPolicyService>(binder); |
| 784 | LOG_ALWAYS_FATAL_IF(gAudioPolicyService == 0); |
| 785 | apc = gAudioPolicyServiceClient; |
Eric Laurent | fb00fc7 | 2017-05-25 18:17:12 -0700 | [diff] [blame] | 786 | // Make sure callbacks can be received by gAudioPolicyServiceClient |
| 787 | ProcessState::self()->startThreadPool(); |
Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 788 | } |
Eric Laurent | 0ebd5f9 | 2014-11-19 19:04:52 -0800 | [diff] [blame] | 789 | ap = gAudioPolicyService; |
| 790 | } |
| 791 | if (apc != 0) { |
François Gaffie | 2443760 | 2018-04-23 15:08:59 +0200 | [diff] [blame] | 792 | int64_t token = IPCThreadState::self()->clearCallingIdentity(); |
Eric Laurent | 0ebd5f9 | 2014-11-19 19:04:52 -0800 | [diff] [blame] | 793 | ap->registerClient(apc); |
François Gaffie | 2443760 | 2018-04-23 15:08:59 +0200 | [diff] [blame] | 794 | ap->setAudioPortCallbacksEnabled(apc->isAudioPortCbEnabled()); |
| 795 | IPCThreadState::self()->restoreCallingIdentity(token); |
Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 796 | } |
Glenn Kasten | d2d089f | 2014-11-05 11:48:12 -0800 | [diff] [blame] | 797 | |
Eric Laurent | 0ebd5f9 | 2014-11-19 19:04:52 -0800 | [diff] [blame] | 798 | return ap; |
Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 799 | } |
| 800 | |
Glenn Kasten | fb1fdc9 | 2013-07-10 17:03:19 -0700 | [diff] [blame] | 801 | // --------------------------------------------------------------------------- |
| 802 | |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 803 | status_t AudioSystem::setDeviceConnectionState(audio_devices_t device, |
| 804 | audio_policy_dev_state_t state, |
Paul McLean | e743a47 | 2015-01-28 11:07:31 -0800 | [diff] [blame] | 805 | const char *device_address, |
Aniket Kumar Lata | 4e46470 | 2019-01-10 23:38:46 -0800 | [diff] [blame] | 806 | const char *device_name, |
| 807 | audio_format_t encodedFormat) |
Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 808 | { |
| 809 | const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service(); |
Eric Laurent | 71b63e3 | 2011-09-02 14:20:56 -0700 | [diff] [blame] | 810 | const char *address = ""; |
Paul McLean | e743a47 | 2015-01-28 11:07:31 -0800 | [diff] [blame] | 811 | const char *name = ""; |
Eric Laurent | 71b63e3 | 2011-09-02 14:20:56 -0700 | [diff] [blame] | 812 | |
Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 813 | if (aps == 0) return PERMISSION_DENIED; |
| 814 | |
Eric Laurent | 71b63e3 | 2011-09-02 14:20:56 -0700 | [diff] [blame] | 815 | if (device_address != NULL) { |
| 816 | address = device_address; |
| 817 | } |
Paul McLean | e743a47 | 2015-01-28 11:07:31 -0800 | [diff] [blame] | 818 | if (device_name != NULL) { |
| 819 | name = device_name; |
| 820 | } |
Aniket Kumar Lata | 4e46470 | 2019-01-10 23:38:46 -0800 | [diff] [blame] | 821 | return aps->setDeviceConnectionState(device, state, address, name, encodedFormat); |
Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 822 | } |
| 823 | |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 824 | audio_policy_dev_state_t AudioSystem::getDeviceConnectionState(audio_devices_t device, |
Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 825 | const char *device_address) |
| 826 | { |
| 827 | const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service(); |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 828 | if (aps == 0) return AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE; |
Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 829 | |
| 830 | return aps->getDeviceConnectionState(device, device_address); |
| 831 | } |
| 832 | |
Pavlin Radoslavov | f862bc6 | 2016-12-26 18:57:22 -0800 | [diff] [blame] | 833 | status_t AudioSystem::handleDeviceConfigChange(audio_devices_t device, |
| 834 | const char *device_address, |
Aniket Kumar Lata | 4e46470 | 2019-01-10 23:38:46 -0800 | [diff] [blame] | 835 | const char *device_name, |
| 836 | audio_format_t encodedFormat) |
Pavlin Radoslavov | f862bc6 | 2016-12-26 18:57:22 -0800 | [diff] [blame] | 837 | { |
| 838 | const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service(); |
| 839 | const char *address = ""; |
| 840 | const char *name = ""; |
| 841 | |
| 842 | if (aps == 0) return PERMISSION_DENIED; |
| 843 | |
| 844 | if (device_address != NULL) { |
| 845 | address = device_address; |
| 846 | } |
| 847 | if (device_name != NULL) { |
| 848 | name = device_name; |
| 849 | } |
Aniket Kumar Lata | 4e46470 | 2019-01-10 23:38:46 -0800 | [diff] [blame] | 850 | return aps->handleDeviceConfigChange(device, address, name, encodedFormat); |
Pavlin Radoslavov | f862bc6 | 2016-12-26 18:57:22 -0800 | [diff] [blame] | 851 | } |
| 852 | |
Glenn Kasten | f78aee7 | 2012-01-04 11:00:47 -0800 | [diff] [blame] | 853 | status_t AudioSystem::setPhoneState(audio_mode_t state) |
Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 854 | { |
Glenn Kasten | 347966c | 2012-01-18 14:58:32 -0800 | [diff] [blame] | 855 | if (uint32_t(state) >= AUDIO_MODE_CNT) return BAD_VALUE; |
Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 856 | const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service(); |
| 857 | if (aps == 0) return PERMISSION_DENIED; |
| 858 | |
| 859 | return aps->setPhoneState(state); |
| 860 | } |
| 861 | |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 862 | 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] | 863 | { |
| 864 | const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service(); |
| 865 | if (aps == 0) return PERMISSION_DENIED; |
| 866 | return aps->setForceUse(usage, config); |
| 867 | } |
| 868 | |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 869 | 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] | 870 | { |
| 871 | const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service(); |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 872 | if (aps == 0) return AUDIO_POLICY_FORCE_NONE; |
Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 873 | return aps->getForceUse(usage); |
| 874 | } |
| 875 | |
| 876 | |
Eric Laurent | f4e6345 | 2017-11-06 19:31:46 +0000 | [diff] [blame] | 877 | audio_io_handle_t AudioSystem::getOutput(audio_stream_type_t stream) |
Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 878 | { |
Eric Laurent | 1a9ed11 | 2012-03-20 18:36:01 -0700 | [diff] [blame] | 879 | const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service(); |
| 880 | if (aps == 0) return 0; |
Eric Laurent | f4e6345 | 2017-11-06 19:31:46 +0000 | [diff] [blame] | 881 | return aps->getOutput(stream); |
Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 882 | } |
| 883 | |
Eric Laurent | e83b55d | 2014-11-14 10:06:21 -0800 | [diff] [blame] | 884 | status_t AudioSystem::getOutputForAttr(const audio_attributes_t *attr, |
| 885 | audio_io_handle_t *output, |
| 886 | audio_session_t session, |
| 887 | audio_stream_type_t *stream, |
Nadav Bar | 766fb02 | 2018-01-07 12:18:03 +0200 | [diff] [blame] | 888 | pid_t pid, |
Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 889 | uid_t uid, |
Eric Laurent | 20b9ef0 | 2016-12-05 11:03:16 -0800 | [diff] [blame] | 890 | const audio_config_t *config, |
Eric Laurent | e83b55d | 2014-11-14 10:06:21 -0800 | [diff] [blame] | 891 | audio_output_flags_t flags, |
Eric Laurent | 9ae8c59 | 2017-06-22 17:17:09 -0700 | [diff] [blame] | 892 | audio_port_handle_t *selectedDeviceId, |
Kevin Rocard | 153f92d | 2018-12-18 18:33:28 -0800 | [diff] [blame] | 893 | audio_port_handle_t *portId, |
| 894 | std::vector<audio_io_handle_t> *secondaryOutputs) |
Jean-Michel Trivi | 5bd3f38 | 2014-06-13 16:06:54 -0700 | [diff] [blame] | 895 | { |
Jean-Michel Trivi | 5bd3f38 | 2014-06-13 16:06:54 -0700 | [diff] [blame] | 896 | const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service(); |
Eric Laurent | e83b55d | 2014-11-14 10:06:21 -0800 | [diff] [blame] | 897 | if (aps == 0) return NO_INIT; |
Nadav Bar | 766fb02 | 2018-01-07 12:18:03 +0200 | [diff] [blame] | 898 | return aps->getOutputForAttr(attr, output, session, stream, pid, uid, |
Eric Laurent | 20b9ef0 | 2016-12-05 11:03:16 -0800 | [diff] [blame] | 899 | config, |
Kevin Rocard | 153f92d | 2018-12-18 18:33:28 -0800 | [diff] [blame] | 900 | flags, selectedDeviceId, portId, secondaryOutputs); |
Jean-Michel Trivi | 5bd3f38 | 2014-06-13 16:06:54 -0700 | [diff] [blame] | 901 | } |
| 902 | |
Eric Laurent | d7fe086 | 2018-07-14 16:48:01 -0700 | [diff] [blame] | 903 | status_t AudioSystem::startOutput(audio_port_handle_t portId) |
Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 904 | { |
| 905 | const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service(); |
| 906 | if (aps == 0) return PERMISSION_DENIED; |
Eric Laurent | d7fe086 | 2018-07-14 16:48:01 -0700 | [diff] [blame] | 907 | return aps->startOutput(portId); |
Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 908 | } |
| 909 | |
Eric Laurent | d7fe086 | 2018-07-14 16:48:01 -0700 | [diff] [blame] | 910 | status_t AudioSystem::stopOutput(audio_port_handle_t portId) |
Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 911 | { |
| 912 | const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service(); |
| 913 | if (aps == 0) return PERMISSION_DENIED; |
Eric Laurent | d7fe086 | 2018-07-14 16:48:01 -0700 | [diff] [blame] | 914 | return aps->stopOutput(portId); |
Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 915 | } |
| 916 | |
Eric Laurent | d7fe086 | 2018-07-14 16:48:01 -0700 | [diff] [blame] | 917 | void AudioSystem::releaseOutput(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(); |
| 920 | if (aps == 0) return; |
Eric Laurent | d7fe086 | 2018-07-14 16:48:01 -0700 | [diff] [blame] | 921 | aps->releaseOutput(portId); |
Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 922 | } |
| 923 | |
Eric Laurent | caf7f48 | 2014-11-25 17:50:47 -0800 | [diff] [blame] | 924 | status_t AudioSystem::getInputForAttr(const audio_attributes_t *attr, |
| 925 | audio_io_handle_t *input, |
| 926 | audio_session_t session, |
Eric Laurent | b2379ba | 2016-05-23 17:42:12 -0700 | [diff] [blame] | 927 | pid_t pid, |
Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 928 | uid_t uid, |
Eric Laurent | fee1976 | 2018-01-29 18:44:13 -0800 | [diff] [blame] | 929 | const String16& opPackageName, |
Eric Laurent | 20b9ef0 | 2016-12-05 11:03:16 -0800 | [diff] [blame] | 930 | const audio_config_base_t *config, |
Paul McLean | 466dc8e | 2015-04-17 13:15:36 -0600 | [diff] [blame] | 931 | audio_input_flags_t flags, |
Eric Laurent | 9ae8c59 | 2017-06-22 17:17:09 -0700 | [diff] [blame] | 932 | audio_port_handle_t *selectedDeviceId, |
Eric Laurent | 20b9ef0 | 2016-12-05 11:03:16 -0800 | [diff] [blame] | 933 | 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(); |
Eric Laurent | caf7f48 | 2014-11-25 17:50:47 -0800 | [diff] [blame] | 936 | if (aps == 0) return NO_INIT; |
Paul McLean | 466dc8e | 2015-04-17 13:15:36 -0600 | [diff] [blame] | 937 | return aps->getInputForAttr( |
Eric Laurent | fee1976 | 2018-01-29 18:44:13 -0800 | [diff] [blame] | 938 | attr, input, session, pid, uid, opPackageName, |
Eric Laurent | 20b9ef0 | 2016-12-05 11:03:16 -0800 | [diff] [blame] | 939 | config, flags, selectedDeviceId, portId); |
Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 940 | } |
| 941 | |
Eric Laurent | 4eb58f1 | 2018-12-07 16:41:02 -0800 | [diff] [blame] | 942 | status_t AudioSystem::startInput(audio_port_handle_t portId) |
Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 943 | { |
| 944 | const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service(); |
| 945 | if (aps == 0) return PERMISSION_DENIED; |
Eric Laurent | 4eb58f1 | 2018-12-07 16:41:02 -0800 | [diff] [blame] | 946 | return aps->startInput(portId); |
Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 947 | } |
| 948 | |
Eric Laurent | fee1976 | 2018-01-29 18:44:13 -0800 | [diff] [blame] | 949 | status_t AudioSystem::stopInput(audio_port_handle_t portId) |
Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 950 | { |
| 951 | const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service(); |
| 952 | if (aps == 0) return PERMISSION_DENIED; |
Eric Laurent | fee1976 | 2018-01-29 18:44:13 -0800 | [diff] [blame] | 953 | return aps->stopInput(portId); |
Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 954 | } |
| 955 | |
Eric Laurent | fee1976 | 2018-01-29 18:44:13 -0800 | [diff] [blame] | 956 | void AudioSystem::releaseInput(audio_port_handle_t portId) |
Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 957 | { |
| 958 | const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service(); |
| 959 | if (aps == 0) return; |
Eric Laurent | fee1976 | 2018-01-29 18:44:13 -0800 | [diff] [blame] | 960 | aps->releaseInput(portId); |
Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 961 | } |
| 962 | |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 963 | status_t AudioSystem::initStreamVolume(audio_stream_type_t stream, |
Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 964 | int indexMin, |
| 965 | int indexMax) |
| 966 | { |
| 967 | const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service(); |
| 968 | if (aps == 0) return PERMISSION_DENIED; |
| 969 | return aps->initStreamVolume(stream, indexMin, indexMax); |
| 970 | } |
| 971 | |
Eric Laurent | 83844cc | 2011-11-18 16:43:31 -0800 | [diff] [blame] | 972 | status_t AudioSystem::setStreamVolumeIndex(audio_stream_type_t stream, |
| 973 | int index, |
| 974 | audio_devices_t device) |
Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 975 | { |
| 976 | const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service(); |
| 977 | if (aps == 0) return PERMISSION_DENIED; |
Eric Laurent | 83844cc | 2011-11-18 16:43:31 -0800 | [diff] [blame] | 978 | return aps->setStreamVolumeIndex(stream, index, device); |
Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 979 | } |
| 980 | |
Eric Laurent | 83844cc | 2011-11-18 16:43:31 -0800 | [diff] [blame] | 981 | status_t AudioSystem::getStreamVolumeIndex(audio_stream_type_t stream, |
| 982 | int *index, |
| 983 | audio_devices_t device) |
Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 984 | { |
| 985 | const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service(); |
| 986 | if (aps == 0) return PERMISSION_DENIED; |
Eric Laurent | 83844cc | 2011-11-18 16:43:31 -0800 | [diff] [blame] | 987 | return aps->getStreamVolumeIndex(stream, index, device); |
Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 988 | } |
| 989 | |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 990 | uint32_t AudioSystem::getStrategyForStream(audio_stream_type_t stream) |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 991 | { |
| 992 | const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service(); |
François Gaffie | d0ba9ed | 2018-11-05 11:50:42 +0100 | [diff] [blame] | 993 | if (aps == 0) return PRODUCT_STRATEGY_NONE; |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 994 | return aps->getStrategyForStream(stream); |
| 995 | } |
| 996 | |
Eric Laurent | 6374252 | 2012-03-08 13:42:42 -0800 | [diff] [blame] | 997 | audio_devices_t AudioSystem::getDevicesForStream(audio_stream_type_t stream) |
Glenn Kasten | 6b2718c | 2011-02-04 13:54:26 -0800 | [diff] [blame] | 998 | { |
| 999 | const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service(); |
Glenn Kasten | 45faf7e | 2014-01-17 10:23:01 -0800 | [diff] [blame] | 1000 | if (aps == 0) return AUDIO_DEVICE_NONE; |
Glenn Kasten | 6b2718c | 2011-02-04 13:54:26 -0800 | [diff] [blame] | 1001 | return aps->getDevicesForStream(stream); |
| 1002 | } |
| 1003 | |
Glenn Kasten | 58e5aa3 | 2012-06-20 14:08:14 -0700 | [diff] [blame] | 1004 | audio_io_handle_t AudioSystem::getOutputForEffect(const effect_descriptor_t *desc) |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 1005 | { |
| 1006 | const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service(); |
Glenn Kasten | efa6ea9 | 2014-01-08 09:10:43 -0800 | [diff] [blame] | 1007 | // FIXME change return type to status_t, and return PERMISSION_DENIED here |
Glenn Kasten | 142f519 | 2014-03-25 17:44:59 -0700 | [diff] [blame] | 1008 | if (aps == 0) return AUDIO_IO_HANDLE_NONE; |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 1009 | return aps->getOutputForEffect(desc); |
| 1010 | } |
| 1011 | |
Glenn Kasten | 58e5aa3 | 2012-06-20 14:08:14 -0700 | [diff] [blame] | 1012 | status_t AudioSystem::registerEffect(const effect_descriptor_t *desc, |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 1013 | audio_io_handle_t io, |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 1014 | uint32_t strategy, |
Glenn Kasten | d848eb4 | 2016-03-08 13:42:11 -0800 | [diff] [blame] | 1015 | audio_session_t session, |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 1016 | int id) |
| 1017 | { |
| 1018 | const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service(); |
| 1019 | if (aps == 0) return PERMISSION_DENIED; |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 1020 | return aps->registerEffect(desc, io, strategy, session, id); |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 1021 | } |
| 1022 | |
| 1023 | status_t AudioSystem::unregisterEffect(int id) |
| 1024 | { |
| 1025 | const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service(); |
| 1026 | if (aps == 0) return PERMISSION_DENIED; |
| 1027 | return aps->unregisterEffect(id); |
| 1028 | } |
| 1029 | |
Eric Laurent | db7c079 | 2011-08-10 10:37:50 -0700 | [diff] [blame] | 1030 | status_t AudioSystem::setEffectEnabled(int id, bool enabled) |
| 1031 | { |
| 1032 | const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service(); |
| 1033 | if (aps == 0) return PERMISSION_DENIED; |
| 1034 | return aps->setEffectEnabled(id, enabled); |
| 1035 | } |
| 1036 | |
Glenn Kasten | fff6d71 | 2012-01-12 16:38:12 -0800 | [diff] [blame] | 1037 | 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] | 1038 | { |
Eric Laurent | eda6c36 | 2011-02-02 09:33:30 -0800 | [diff] [blame] | 1039 | const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service(); |
| 1040 | if (aps == 0) return PERMISSION_DENIED; |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 1041 | if (state == NULL) return BAD_VALUE; |
Eric Laurent | eda6c36 | 2011-02-02 09:33:30 -0800 | [diff] [blame] | 1042 | *state = aps->isStreamActive(stream, inPastMs); |
| 1043 | return NO_ERROR; |
| 1044 | } |
| 1045 | |
Jean-Michel Trivi | 272ab54 | 2013-02-04 16:26:02 -0800 | [diff] [blame] | 1046 | status_t AudioSystem::isStreamActiveRemotely(audio_stream_type_t stream, bool* state, |
| 1047 | uint32_t inPastMs) |
| 1048 | { |
| 1049 | const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service(); |
| 1050 | if (aps == 0) return PERMISSION_DENIED; |
| 1051 | if (state == NULL) return BAD_VALUE; |
| 1052 | *state = aps->isStreamActiveRemotely(stream, inPastMs); |
| 1053 | return NO_ERROR; |
| 1054 | } |
| 1055 | |
Jean-Michel Trivi | d708603 | 2012-10-10 12:11:16 -0700 | [diff] [blame] | 1056 | status_t AudioSystem::isSourceActive(audio_source_t stream, bool* state) |
| 1057 | { |
| 1058 | const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service(); |
| 1059 | if (aps == 0) return PERMISSION_DENIED; |
| 1060 | if (state == NULL) return BAD_VALUE; |
| 1061 | *state = aps->isSourceActive(stream); |
| 1062 | return NO_ERROR; |
| 1063 | } |
| 1064 | |
Glenn Kasten | 3b16c76 | 2012-11-14 08:44:39 -0800 | [diff] [blame] | 1065 | uint32_t AudioSystem::getPrimaryOutputSamplingRate() |
Glenn Kasten | cc0f1cf | 2012-09-24 11:27:18 -0700 | [diff] [blame] | 1066 | { |
| 1067 | const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger(); |
| 1068 | if (af == 0) return 0; |
| 1069 | return af->getPrimaryOutputSamplingRate(); |
| 1070 | } |
| 1071 | |
Glenn Kasten | e33054e | 2012-11-14 12:54:39 -0800 | [diff] [blame] | 1072 | size_t AudioSystem::getPrimaryOutputFrameCount() |
Glenn Kasten | cc0f1cf | 2012-09-24 11:27:18 -0700 | [diff] [blame] | 1073 | { |
| 1074 | const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger(); |
| 1075 | if (af == 0) return 0; |
| 1076 | return af->getPrimaryOutputFrameCount(); |
| 1077 | } |
Eric Laurent | eda6c36 | 2011-02-02 09:33:30 -0800 | [diff] [blame] | 1078 | |
Andy Hung | 6f248bb | 2018-01-23 14:04:37 -0800 | [diff] [blame] | 1079 | status_t AudioSystem::setLowRamDevice(bool isLowRamDevice, int64_t totalMemory) |
Glenn Kasten | 4182c4e | 2013-07-15 14:45:07 -0700 | [diff] [blame] | 1080 | { |
| 1081 | const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger(); |
| 1082 | if (af == 0) return PERMISSION_DENIED; |
Andy Hung | 6f248bb | 2018-01-23 14:04:37 -0800 | [diff] [blame] | 1083 | return af->setLowRamDevice(isLowRamDevice, totalMemory); |
Glenn Kasten | 4182c4e | 2013-07-15 14:45:07 -0700 | [diff] [blame] | 1084 | } |
| 1085 | |
Eric Laurent | 9f6530f | 2011-08-30 10:18:54 -0700 | [diff] [blame] | 1086 | void AudioSystem::clearAudioConfigCache() |
| 1087 | { |
Glenn Kasten | d2d089f | 2014-11-05 11:48:12 -0800 | [diff] [blame] | 1088 | // called by restoreTrack_l(), which needs new IAudioFlinger and IAudioPolicyService instances |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 1089 | ALOGV("clearAudioConfigCache()"); |
Eric Laurent | f6778fd | 2014-11-18 17:26:58 -0800 | [diff] [blame] | 1090 | { |
| 1091 | Mutex::Autolock _l(gLock); |
Eric Laurent | 296fb13 | 2015-05-01 11:38:42 -0700 | [diff] [blame] | 1092 | if (gAudioFlingerClient != 0) { |
| 1093 | gAudioFlingerClient->clearIoCache(); |
| 1094 | } |
Glenn Kasten | d2d089f | 2014-11-05 11:48:12 -0800 | [diff] [blame] | 1095 | gAudioFlinger.clear(); |
| 1096 | } |
| 1097 | { |
| 1098 | Mutex::Autolock _l(gLockAPS); |
| 1099 | gAudioPolicyService.clear(); |
| 1100 | } |
Eric Laurent | 9f6530f | 2011-08-30 10:18:54 -0700 | [diff] [blame] | 1101 | } |
| 1102 | |
Richard Fitzgerald | ad3af33 | 2013-03-25 16:54:37 +0000 | [diff] [blame] | 1103 | bool AudioSystem::isOffloadSupported(const audio_offload_info_t& info) |
| 1104 | { |
| 1105 | ALOGV("isOffloadSupported()"); |
| 1106 | const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service(); |
| 1107 | if (aps == 0) return false; |
| 1108 | return aps->isOffloadSupported(info); |
| 1109 | } |
| 1110 | |
Eric Laurent | 203b1a1 | 2014-04-01 10:34:16 -0700 | [diff] [blame] | 1111 | status_t AudioSystem::listAudioPorts(audio_port_role_t role, |
| 1112 | audio_port_type_t type, |
| 1113 | unsigned int *num_ports, |
| 1114 | struct audio_port *ports, |
| 1115 | unsigned int *generation) |
| 1116 | { |
| 1117 | const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service(); |
| 1118 | if (aps == 0) return PERMISSION_DENIED; |
| 1119 | return aps->listAudioPorts(role, type, num_ports, ports, generation); |
| 1120 | } |
| 1121 | |
| 1122 | status_t AudioSystem::getAudioPort(struct audio_port *port) |
| 1123 | { |
| 1124 | const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service(); |
| 1125 | if (aps == 0) return PERMISSION_DENIED; |
| 1126 | return aps->getAudioPort(port); |
| 1127 | } |
| 1128 | |
| 1129 | status_t AudioSystem::createAudioPatch(const struct audio_patch *patch, |
| 1130 | audio_patch_handle_t *handle) |
| 1131 | { |
| 1132 | const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service(); |
| 1133 | if (aps == 0) return PERMISSION_DENIED; |
| 1134 | return aps->createAudioPatch(patch, handle); |
| 1135 | } |
| 1136 | |
| 1137 | status_t AudioSystem::releaseAudioPatch(audio_patch_handle_t handle) |
| 1138 | { |
| 1139 | const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service(); |
| 1140 | if (aps == 0) return PERMISSION_DENIED; |
| 1141 | return aps->releaseAudioPatch(handle); |
| 1142 | } |
| 1143 | |
| 1144 | status_t AudioSystem::listAudioPatches(unsigned int *num_patches, |
| 1145 | struct audio_patch *patches, |
| 1146 | unsigned int *generation) |
| 1147 | { |
| 1148 | const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service(); |
| 1149 | if (aps == 0) return PERMISSION_DENIED; |
| 1150 | return aps->listAudioPatches(num_patches, patches, generation); |
| 1151 | } |
| 1152 | |
| 1153 | status_t AudioSystem::setAudioPortConfig(const struct audio_port_config *config) |
| 1154 | { |
| 1155 | const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service(); |
| 1156 | if (aps == 0) return PERMISSION_DENIED; |
| 1157 | return aps->setAudioPortConfig(config); |
| 1158 | } |
| 1159 | |
Eric Laurent | 296fb13 | 2015-05-01 11:38:42 -0700 | [diff] [blame] | 1160 | status_t AudioSystem::addAudioPortCallback(const sp<AudioPortCallback>& callback) |
Eric Laurent | b52c152 | 2014-05-20 11:27:36 -0700 | [diff] [blame] | 1161 | { |
Eric Laurent | b28753e | 2015-04-01 13:06:28 -0700 | [diff] [blame] | 1162 | const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service(); |
| 1163 | if (aps == 0) return PERMISSION_DENIED; |
| 1164 | |
| 1165 | Mutex::Autolock _l(gLockAPS); |
| 1166 | if (gAudioPolicyServiceClient == 0) { |
| 1167 | return NO_INIT; |
| 1168 | } |
Eric Laurent | e8726fe | 2015-06-26 09:39:24 -0700 | [diff] [blame] | 1169 | int ret = gAudioPolicyServiceClient->addAudioPortCallback(callback); |
| 1170 | if (ret == 1) { |
| 1171 | aps->setAudioPortCallbacksEnabled(true); |
| 1172 | } |
| 1173 | return (ret < 0) ? INVALID_OPERATION : NO_ERROR; |
Eric Laurent | b52c152 | 2014-05-20 11:27:36 -0700 | [diff] [blame] | 1174 | } |
| 1175 | |
Jean-Michel Trivi | f613d42 | 2015-04-23 18:41:29 -0700 | [diff] [blame] | 1176 | /*static*/ |
Eric Laurent | 296fb13 | 2015-05-01 11:38:42 -0700 | [diff] [blame] | 1177 | status_t AudioSystem::removeAudioPortCallback(const sp<AudioPortCallback>& callback) |
Eric Laurent | b28753e | 2015-04-01 13:06:28 -0700 | [diff] [blame] | 1178 | { |
| 1179 | const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service(); |
| 1180 | if (aps == 0) return PERMISSION_DENIED; |
| 1181 | |
| 1182 | Mutex::Autolock _l(gLockAPS); |
| 1183 | if (gAudioPolicyServiceClient == 0) { |
| 1184 | return NO_INIT; |
| 1185 | } |
Eric Laurent | e8726fe | 2015-06-26 09:39:24 -0700 | [diff] [blame] | 1186 | int ret = gAudioPolicyServiceClient->removeAudioPortCallback(callback); |
| 1187 | if (ret == 0) { |
| 1188 | aps->setAudioPortCallbacksEnabled(false); |
| 1189 | } |
| 1190 | return (ret < 0) ? INVALID_OPERATION : NO_ERROR; |
Eric Laurent | 296fb13 | 2015-05-01 11:38:42 -0700 | [diff] [blame] | 1191 | } |
| 1192 | |
| 1193 | status_t AudioSystem::addAudioDeviceCallback( |
Eric Laurent | ad2e7b9 | 2017-09-14 20:06:42 -0700 | [diff] [blame] | 1194 | const wp<AudioDeviceCallback>& callback, audio_io_handle_t audioIo) |
Eric Laurent | 296fb13 | 2015-05-01 11:38:42 -0700 | [diff] [blame] | 1195 | { |
| 1196 | const sp<AudioFlingerClient> afc = getAudioFlingerClient(); |
| 1197 | if (afc == 0) { |
| 1198 | return NO_INIT; |
| 1199 | } |
Eric Laurent | 7c1ec5f | 2015-07-09 14:52:47 -0700 | [diff] [blame] | 1200 | status_t status = afc->addAudioDeviceCallback(callback, audioIo); |
| 1201 | if (status == NO_ERROR) { |
| 1202 | const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger(); |
| 1203 | if (af != 0) { |
| 1204 | af->registerClient(afc); |
| 1205 | } |
| 1206 | } |
| 1207 | return status; |
Eric Laurent | 296fb13 | 2015-05-01 11:38:42 -0700 | [diff] [blame] | 1208 | } |
| 1209 | |
| 1210 | status_t AudioSystem::removeAudioDeviceCallback( |
Eric Laurent | ad2e7b9 | 2017-09-14 20:06:42 -0700 | [diff] [blame] | 1211 | const wp<AudioDeviceCallback>& callback, audio_io_handle_t audioIo) |
Eric Laurent | 296fb13 | 2015-05-01 11:38:42 -0700 | [diff] [blame] | 1212 | { |
| 1213 | const sp<AudioFlingerClient> afc = getAudioFlingerClient(); |
| 1214 | if (afc == 0) { |
| 1215 | return NO_INIT; |
| 1216 | } |
| 1217 | return afc->removeAudioDeviceCallback(callback, audioIo); |
| 1218 | } |
| 1219 | |
| 1220 | audio_port_handle_t AudioSystem::getDeviceIdForIo(audio_io_handle_t audioIo) |
| 1221 | { |
| 1222 | const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger(); |
| 1223 | if (af == 0) return PERMISSION_DENIED; |
| 1224 | const sp<AudioIoDescriptor> desc = getIoDescriptor(audioIo); |
| 1225 | if (desc == 0) { |
| 1226 | return AUDIO_PORT_HANDLE_NONE; |
| 1227 | } |
| 1228 | return desc->getDeviceId(); |
Eric Laurent | b28753e | 2015-04-01 13:06:28 -0700 | [diff] [blame] | 1229 | } |
| 1230 | |
Eric Laurent | df3dc7e | 2014-07-27 18:39:40 -0700 | [diff] [blame] | 1231 | status_t AudioSystem::acquireSoundTriggerSession(audio_session_t *session, |
| 1232 | audio_io_handle_t *ioHandle, |
| 1233 | audio_devices_t *device) |
| 1234 | { |
| 1235 | const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service(); |
| 1236 | if (aps == 0) return PERMISSION_DENIED; |
| 1237 | return aps->acquireSoundTriggerSession(session, ioHandle, device); |
| 1238 | } |
| 1239 | |
| 1240 | status_t AudioSystem::releaseSoundTriggerSession(audio_session_t session) |
| 1241 | { |
| 1242 | const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service(); |
| 1243 | if (aps == 0) return PERMISSION_DENIED; |
| 1244 | return aps->releaseSoundTriggerSession(session); |
| 1245 | } |
Eric Laurent | bb6c9a0 | 2014-09-25 14:11:47 -0700 | [diff] [blame] | 1246 | |
| 1247 | audio_mode_t AudioSystem::getPhoneState() |
| 1248 | { |
| 1249 | const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service(); |
| 1250 | if (aps == 0) return AUDIO_MODE_INVALID; |
| 1251 | return aps->getPhoneState(); |
| 1252 | } |
| 1253 | |
Chih-Hung Hsieh | e964d4e | 2016-08-09 14:31:32 -0700 | [diff] [blame] | 1254 | status_t AudioSystem::registerPolicyMixes(const Vector<AudioMix>& mixes, bool registration) |
Eric Laurent | baac183 | 2014-12-01 17:52:59 -0800 | [diff] [blame] | 1255 | { |
| 1256 | const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service(); |
| 1257 | if (aps == 0) return PERMISSION_DENIED; |
| 1258 | return aps->registerPolicyMixes(mixes, registration); |
| 1259 | } |
Eric Laurent | bb6c9a0 | 2014-09-25 14:11:47 -0700 | [diff] [blame] | 1260 | |
Jean-Michel Trivi | bda70da | 2018-12-19 07:30:15 -0800 | [diff] [blame] | 1261 | status_t AudioSystem::setUidDeviceAffinities(uid_t uid, const Vector<AudioDeviceTypeAddr>& devices) |
| 1262 | { |
| 1263 | const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service(); |
| 1264 | if (aps == 0) return PERMISSION_DENIED; |
| 1265 | return aps->setUidDeviceAffinities(uid, devices); |
| 1266 | } |
| 1267 | |
| 1268 | status_t AudioSystem::removeUidDeviceAffinities(uid_t uid) { |
| 1269 | const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service(); |
| 1270 | if (aps == 0) return PERMISSION_DENIED; |
| 1271 | return aps->removeUidDeviceAffinities(uid); |
| 1272 | } |
| 1273 | |
Eric Laurent | 554a277 | 2015-04-10 11:29:24 -0700 | [diff] [blame] | 1274 | status_t AudioSystem::startAudioSource(const struct audio_port_config *source, |
| 1275 | const audio_attributes_t *attributes, |
Eric Laurent | 3e6c7e1 | 2018-07-27 17:09:23 -0700 | [diff] [blame] | 1276 | audio_port_handle_t *portId) |
Eric Laurent | 554a277 | 2015-04-10 11:29:24 -0700 | [diff] [blame] | 1277 | { |
| 1278 | const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service(); |
| 1279 | if (aps == 0) return PERMISSION_DENIED; |
Eric Laurent | 3e6c7e1 | 2018-07-27 17:09:23 -0700 | [diff] [blame] | 1280 | return aps->startAudioSource(source, attributes, portId); |
Eric Laurent | 554a277 | 2015-04-10 11:29:24 -0700 | [diff] [blame] | 1281 | } |
| 1282 | |
Eric Laurent | 3e6c7e1 | 2018-07-27 17:09:23 -0700 | [diff] [blame] | 1283 | status_t AudioSystem::stopAudioSource(audio_port_handle_t portId) |
Eric Laurent | 554a277 | 2015-04-10 11:29:24 -0700 | [diff] [blame] | 1284 | { |
| 1285 | const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service(); |
| 1286 | if (aps == 0) return PERMISSION_DENIED; |
Eric Laurent | 3e6c7e1 | 2018-07-27 17:09:23 -0700 | [diff] [blame] | 1287 | return aps->stopAudioSource(portId); |
Eric Laurent | 554a277 | 2015-04-10 11:29:24 -0700 | [diff] [blame] | 1288 | } |
| 1289 | |
Andy Hung | 2ddee19 | 2015-12-18 17:34:44 -0800 | [diff] [blame] | 1290 | status_t AudioSystem::setMasterMono(bool mono) |
| 1291 | { |
| 1292 | const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service(); |
| 1293 | if (aps == 0) return PERMISSION_DENIED; |
| 1294 | return aps->setMasterMono(mono); |
| 1295 | } |
| 1296 | |
| 1297 | status_t AudioSystem::getMasterMono(bool *mono) |
| 1298 | { |
| 1299 | const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service(); |
| 1300 | if (aps == 0) return PERMISSION_DENIED; |
| 1301 | return aps->getMasterMono(mono); |
| 1302 | } |
| 1303 | |
Richard Folke Tullberg | 3fae037 | 2017-01-13 09:04:25 +0100 | [diff] [blame] | 1304 | status_t AudioSystem::setMasterBalance(float balance) |
| 1305 | { |
| 1306 | const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger(); |
| 1307 | if (af == 0) return PERMISSION_DENIED; |
| 1308 | return af->setMasterBalance(balance); |
| 1309 | } |
| 1310 | |
| 1311 | status_t AudioSystem::getMasterBalance(float *balance) |
| 1312 | { |
| 1313 | const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger(); |
| 1314 | if (af == 0) return PERMISSION_DENIED; |
| 1315 | return af->getMasterBalance(balance); |
| 1316 | } |
| 1317 | |
Eric Laurent | ac9cef5 | 2017-06-09 15:46:26 -0700 | [diff] [blame] | 1318 | float AudioSystem::getStreamVolumeDB(audio_stream_type_t stream, int index, audio_devices_t device) |
| 1319 | { |
| 1320 | const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service(); |
| 1321 | if (aps == 0) return NAN; |
| 1322 | return aps->getStreamVolumeDB(stream, index, device); |
| 1323 | } |
| 1324 | |
jiabin | 46a76fa | 2018-01-05 10:18:21 -0800 | [diff] [blame] | 1325 | status_t AudioSystem::getMicrophones(std::vector<media::MicrophoneInfo> *microphones) |
| 1326 | { |
| 1327 | const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger(); |
| 1328 | if (af == 0) return PERMISSION_DENIED; |
| 1329 | return af->getMicrophones(microphones); |
| 1330 | } |
| 1331 | |
jiabin | 8177290 | 2018-04-02 17:52:27 -0700 | [diff] [blame] | 1332 | status_t AudioSystem::getSurroundFormats(unsigned int *numSurroundFormats, |
| 1333 | audio_format_t *surroundFormats, |
| 1334 | bool *surroundFormatsEnabled, |
| 1335 | bool reported) |
| 1336 | { |
| 1337 | const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service(); |
| 1338 | if (aps == 0) return PERMISSION_DENIED; |
| 1339 | return aps->getSurroundFormats( |
| 1340 | numSurroundFormats, surroundFormats, surroundFormatsEnabled, reported); |
| 1341 | } |
| 1342 | |
| 1343 | status_t AudioSystem::setSurroundFormatEnabled(audio_format_t audioFormat, bool enabled) |
| 1344 | { |
| 1345 | const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service(); |
| 1346 | if (aps == 0) return PERMISSION_DENIED; |
| 1347 | return aps->setSurroundFormatEnabled(audioFormat, enabled); |
| 1348 | } |
| 1349 | |
Eric Laurent | b78763e | 2018-10-17 10:08:02 -0700 | [diff] [blame] | 1350 | status_t AudioSystem::setAssistantUid(uid_t uid) |
| 1351 | { |
| 1352 | const sp <IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service(); |
| 1353 | if (aps == 0) return PERMISSION_DENIED; |
| 1354 | |
| 1355 | return aps->setAssistantUid(uid); |
| 1356 | } |
| 1357 | |
| 1358 | status_t AudioSystem::setA11yServicesUids(const std::vector<uid_t>& uids) |
| 1359 | { |
| 1360 | const sp <IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service(); |
| 1361 | if (aps == 0) return PERMISSION_DENIED; |
| 1362 | |
| 1363 | return aps->setA11yServicesUids(uids); |
| 1364 | } |
| 1365 | |
jiabin | 6012f91 | 2018-11-02 17:06:30 -0700 | [diff] [blame] | 1366 | bool AudioSystem::isHapticPlaybackSupported() |
| 1367 | { |
| 1368 | const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service(); |
| 1369 | if (aps == 0) return false; |
| 1370 | return aps->isHapticPlaybackSupported(); |
| 1371 | } |
| 1372 | |
Arun Mirpuri | 11029ad | 2018-12-19 20:45:19 -0800 | [diff] [blame] | 1373 | status_t AudioSystem::getHwOffloadEncodingFormatsSupportedForA2DP( |
François Gaffie | d0ba9ed | 2018-11-05 11:50:42 +0100 | [diff] [blame] | 1374 | std::vector<audio_format_t> *formats) { |
| 1375 | const sp <IAudioPolicyService> |
| 1376 | & aps = AudioSystem::get_audio_policy_service(); |
| 1377 | if (aps == 0) return PERMISSION_DENIED; |
| 1378 | return aps->getHwOffloadEncodingFormatsSupportedForA2DP(formats); |
| 1379 | } |
| 1380 | |
| 1381 | status_t AudioSystem::listAudioProductStrategies(AudioProductStrategyVector &strategies) |
Arun Mirpuri | 11029ad | 2018-12-19 20:45:19 -0800 | [diff] [blame] | 1382 | { |
| 1383 | const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service(); |
| 1384 | if (aps == 0) return PERMISSION_DENIED; |
François Gaffie | d0ba9ed | 2018-11-05 11:50:42 +0100 | [diff] [blame] | 1385 | return aps->listAudioProductStrategies(strategies); |
| 1386 | } |
| 1387 | |
| 1388 | audio_attributes_t AudioSystem::streamTypeToAttributes(audio_stream_type_t stream) |
| 1389 | { |
| 1390 | AudioProductStrategyVector strategies; |
| 1391 | listAudioProductStrategies(strategies); |
| 1392 | for (const auto &strategy : strategies) { |
| 1393 | auto attrVect = strategy.getAudioAttributes(); |
| 1394 | auto iter = std::find_if(begin(attrVect), end(attrVect), [&stream](const auto &attributes) { |
| 1395 | return attributes.getStreamType() == stream; }); |
| 1396 | if (iter != end(attrVect)) { |
| 1397 | return iter->getAttributes(); |
| 1398 | } |
| 1399 | } |
| 1400 | ALOGE("invalid stream type %s when converting to attributes", toString(stream).c_str()); |
| 1401 | return AUDIO_ATTRIBUTES_INITIALIZER; |
| 1402 | } |
| 1403 | |
| 1404 | audio_stream_type_t AudioSystem::attributesToStreamType(const audio_attributes_t &attr) |
| 1405 | { |
| 1406 | product_strategy_t strategyId = |
| 1407 | AudioSystem::getProductStrategyFromAudioAttributes(AudioAttributes(attr)); |
| 1408 | AudioProductStrategyVector strategies; |
| 1409 | listAudioProductStrategies(strategies); |
| 1410 | for (const auto &strategy : strategies) { |
| 1411 | if (strategy.getId() == strategyId) { |
| 1412 | auto attrVect = strategy.getAudioAttributes(); |
| 1413 | auto iter = std::find_if(begin(attrVect), end(attrVect), [&attr](const auto &refAttr) { |
| 1414 | return AudioProductStrategy::attributesMatches( |
| 1415 | refAttr.getAttributes(), attr); }); |
| 1416 | if (iter != end(attrVect)) { |
| 1417 | return iter->getStreamType(); |
| 1418 | } |
| 1419 | } |
| 1420 | } |
| 1421 | ALOGE("invalid attributes %s when converting to stream", toString(attr).c_str()); |
| 1422 | return AUDIO_STREAM_MUSIC; |
| 1423 | } |
| 1424 | |
| 1425 | product_strategy_t AudioSystem::getProductStrategyFromAudioAttributes(const AudioAttributes &aa) |
| 1426 | { |
| 1427 | const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service(); |
| 1428 | if (aps == 0) return PRODUCT_STRATEGY_NONE; |
| 1429 | return aps->getProductStrategyFromAudioAttributes(aa); |
Arun Mirpuri | 11029ad | 2018-12-19 20:45:19 -0800 | [diff] [blame] | 1430 | } |
Eric Laurent | b78763e | 2018-10-17 10:08:02 -0700 | [diff] [blame] | 1431 | |
Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 1432 | // --------------------------------------------------------------------------- |
| 1433 | |
Eric Laurent | e8726fe | 2015-06-26 09:39:24 -0700 | [diff] [blame] | 1434 | int AudioSystem::AudioPolicyServiceClient::addAudioPortCallback( |
Eric Laurent | 296fb13 | 2015-05-01 11:38:42 -0700 | [diff] [blame] | 1435 | const sp<AudioPortCallback>& callback) |
Eric Laurent | b28753e | 2015-04-01 13:06:28 -0700 | [diff] [blame] | 1436 | { |
| 1437 | Mutex::Autolock _l(mLock); |
| 1438 | for (size_t i = 0; i < mAudioPortCallbacks.size(); i++) { |
Eric Laurent | 296fb13 | 2015-05-01 11:38:42 -0700 | [diff] [blame] | 1439 | if (mAudioPortCallbacks[i] == callback) { |
Eric Laurent | e8726fe | 2015-06-26 09:39:24 -0700 | [diff] [blame] | 1440 | return -1; |
Eric Laurent | b28753e | 2015-04-01 13:06:28 -0700 | [diff] [blame] | 1441 | } |
| 1442 | } |
Eric Laurent | 296fb13 | 2015-05-01 11:38:42 -0700 | [diff] [blame] | 1443 | mAudioPortCallbacks.add(callback); |
Eric Laurent | e8726fe | 2015-06-26 09:39:24 -0700 | [diff] [blame] | 1444 | return mAudioPortCallbacks.size(); |
Eric Laurent | b28753e | 2015-04-01 13:06:28 -0700 | [diff] [blame] | 1445 | } |
| 1446 | |
Eric Laurent | e8726fe | 2015-06-26 09:39:24 -0700 | [diff] [blame] | 1447 | int AudioSystem::AudioPolicyServiceClient::removeAudioPortCallback( |
Eric Laurent | 296fb13 | 2015-05-01 11:38:42 -0700 | [diff] [blame] | 1448 | const sp<AudioPortCallback>& callback) |
Eric Laurent | b28753e | 2015-04-01 13:06:28 -0700 | [diff] [blame] | 1449 | { |
| 1450 | Mutex::Autolock _l(mLock); |
| 1451 | size_t i; |
| 1452 | for (i = 0; i < mAudioPortCallbacks.size(); i++) { |
Eric Laurent | 296fb13 | 2015-05-01 11:38:42 -0700 | [diff] [blame] | 1453 | if (mAudioPortCallbacks[i] == callback) { |
Eric Laurent | b28753e | 2015-04-01 13:06:28 -0700 | [diff] [blame] | 1454 | break; |
| 1455 | } |
| 1456 | } |
| 1457 | if (i == mAudioPortCallbacks.size()) { |
Eric Laurent | e8726fe | 2015-06-26 09:39:24 -0700 | [diff] [blame] | 1458 | return -1; |
Eric Laurent | b28753e | 2015-04-01 13:06:28 -0700 | [diff] [blame] | 1459 | } |
| 1460 | mAudioPortCallbacks.removeAt(i); |
Eric Laurent | e8726fe | 2015-06-26 09:39:24 -0700 | [diff] [blame] | 1461 | return mAudioPortCallbacks.size(); |
Eric Laurent | b28753e | 2015-04-01 13:06:28 -0700 | [diff] [blame] | 1462 | } |
| 1463 | |
Eric Laurent | 296fb13 | 2015-05-01 11:38:42 -0700 | [diff] [blame] | 1464 | |
Eric Laurent | b28753e | 2015-04-01 13:06:28 -0700 | [diff] [blame] | 1465 | void AudioSystem::AudioPolicyServiceClient::onAudioPortListUpdate() |
| 1466 | { |
| 1467 | Mutex::Autolock _l(mLock); |
| 1468 | for (size_t i = 0; i < mAudioPortCallbacks.size(); i++) { |
| 1469 | mAudioPortCallbacks[i]->onAudioPortListUpdate(); |
| 1470 | } |
| 1471 | } |
| 1472 | |
| 1473 | void AudioSystem::AudioPolicyServiceClient::onAudioPatchListUpdate() |
| 1474 | { |
| 1475 | Mutex::Autolock _l(mLock); |
| 1476 | for (size_t i = 0; i < mAudioPortCallbacks.size(); i++) { |
| 1477 | mAudioPortCallbacks[i]->onAudioPatchListUpdate(); |
| 1478 | } |
| 1479 | } |
| 1480 | |
Jean-Michel Trivi | de80105 | 2015-04-14 19:10:14 -0700 | [diff] [blame] | 1481 | void AudioSystem::AudioPolicyServiceClient::onDynamicPolicyMixStateUpdate( |
| 1482 | String8 regId, int32_t state) |
| 1483 | { |
Jean-Michel Trivi | f613d42 | 2015-04-23 18:41:29 -0700 | [diff] [blame] | 1484 | ALOGV("AudioPolicyServiceClient::onDynamicPolicyMixStateUpdate(%s, %d)", regId.string(), state); |
| 1485 | dynamic_policy_callback cb = NULL; |
| 1486 | { |
| 1487 | Mutex::Autolock _l(AudioSystem::gLock); |
| 1488 | cb = gDynPolicyCallback; |
| 1489 | } |
| 1490 | |
| 1491 | if (cb != NULL) { |
| 1492 | cb(DYNAMIC_POLICY_EVENT_MIX_STATE_UPDATE, regId, state); |
| 1493 | } |
Jean-Michel Trivi | de80105 | 2015-04-14 19:10:14 -0700 | [diff] [blame] | 1494 | } |
| 1495 | |
Jean-Michel Trivi | 2f4fe9f | 2015-12-04 16:20:59 -0800 | [diff] [blame] | 1496 | void AudioSystem::AudioPolicyServiceClient::onRecordingConfigurationUpdate( |
Eric Laurent | a9f8665 | 2018-11-28 17:23:11 -0800 | [diff] [blame] | 1497 | int event, |
| 1498 | const record_client_info_t *clientInfo, |
| 1499 | const audio_config_base_t *clientConfig, |
| 1500 | std::vector<effect_descriptor_t> clientEffects, |
| 1501 | const audio_config_base_t *deviceConfig, |
| 1502 | std::vector<effect_descriptor_t> effects, |
| 1503 | audio_patch_handle_t patchHandle, |
| 1504 | audio_source_t source) { |
Jean-Michel Trivi | 2f4fe9f | 2015-12-04 16:20:59 -0800 | [diff] [blame] | 1505 | record_config_callback cb = NULL; |
| 1506 | { |
| 1507 | Mutex::Autolock _l(AudioSystem::gLock); |
| 1508 | cb = gRecordConfigCallback; |
| 1509 | } |
| 1510 | |
| 1511 | if (cb != NULL) { |
Eric Laurent | a9f8665 | 2018-11-28 17:23:11 -0800 | [diff] [blame] | 1512 | cb(event, clientInfo, clientConfig, clientEffects, |
| 1513 | deviceConfig, effects, patchHandle, source); |
Jean-Michel Trivi | 2f4fe9f | 2015-12-04 16:20:59 -0800 | [diff] [blame] | 1514 | } |
| 1515 | } |
| 1516 | |
Glenn Kasten | 4944acb | 2013-08-19 08:39:20 -0700 | [diff] [blame] | 1517 | void AudioSystem::AudioPolicyServiceClient::binderDied(const wp<IBinder>& who __unused) |
| 1518 | { |
Glenn Kasten | d2d089f | 2014-11-05 11:48:12 -0800 | [diff] [blame] | 1519 | { |
Eric Laurent | b28753e | 2015-04-01 13:06:28 -0700 | [diff] [blame] | 1520 | Mutex::Autolock _l(mLock); |
| 1521 | for (size_t i = 0; i < mAudioPortCallbacks.size(); i++) { |
| 1522 | mAudioPortCallbacks[i]->onServiceDied(); |
Glenn Kasten | d2d089f | 2014-11-05 11:48:12 -0800 | [diff] [blame] | 1523 | } |
Eric Laurent | b52c152 | 2014-05-20 11:27:36 -0700 | [diff] [blame] | 1524 | } |
Glenn Kasten | d2d089f | 2014-11-05 11:48:12 -0800 | [diff] [blame] | 1525 | { |
| 1526 | Mutex::Autolock _l(gLockAPS); |
| 1527 | AudioSystem::gAudioPolicyService.clear(); |
| 1528 | } |
Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 1529 | |
Steve Block | 5ff1dd5 | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 1530 | ALOGW("AudioPolicyService server died!"); |
Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 1531 | } |
| 1532 | |
Glenn Kasten | 40bc906 | 2015-03-20 09:09:33 -0700 | [diff] [blame] | 1533 | } // namespace android |