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