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