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