| 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> |
| Ytai Ben-Tsvi | 5120eda | 2020-03-26 09:41:15 -0700 | [diff] [blame] | 21 | |
| Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 22 | #include <android/media/IAudioPolicyService.h> |
| Ytai Ben-Tsvi | 5120eda | 2020-03-26 09:41:15 -0700 | [diff] [blame] | 23 | #include <android/media/BnCaptureStateListener.h> |
| Mathias Agopian | 7562408 | 2009-05-19 19:08:10 -0700 | [diff] [blame] | 24 | #include <binder/IServiceManager.h> |
| Eric Laurent | fb00fc7 | 2017-05-25 18:17:12 -0700 | [diff] [blame] | 25 | #include <binder/ProcessState.h> |
| François Gaffie | 2443760 | 2018-04-23 15:08:59 +0200 | [diff] [blame] | 26 | #include <binder/IPCThreadState.h> |
| Ytai Ben-Tsvi | 10dc0a6 | 2020-09-18 11:31:55 -0700 | [diff] [blame] | 27 | #include <media/AidlConversion.h> |
| Eric Laurent | 21da647 | 2017-11-09 16:29:26 -0800 | [diff] [blame] | 28 | #include <media/AudioResamplerPublic.h> |
| The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 29 | #include <media/AudioSystem.h> |
| Glenn Kasten | 1ab85ec | 2013-05-31 09:18:43 -0700 | [diff] [blame] | 30 | #include <media/IAudioFlinger.h> |
| Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 31 | #include <media/PolicyAidlConversion.h> |
| François Gaffie | d0ba9ed | 2018-11-05 11:50:42 +0100 | [diff] [blame] | 32 | #include <media/TypeConverter.h> |
| The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 33 | #include <math.h> |
| 34 | |
| Dima Zavin | 6476024 | 2011-05-11 14:15:23 -0700 | [diff] [blame] | 35 | #include <system/audio.h> |
| Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 36 | #include <android/media/GetInputForAttrResponse.h> |
| Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 37 | |
| Ytai Ben-Tsvi | 1ff7569 | 2020-11-06 12:16:12 -0800 | [diff] [blame] | 38 | #define VALUE_OR_RETURN_BINDER_STATUS(x) \ |
| Ytai Ben-Tsvi | 10dc0a6 | 2020-09-18 11:31:55 -0700 | [diff] [blame] | 39 | ({ auto _tmp = (x); \ |
| Andy Hung | 1131b6e | 2020-12-08 20:47:45 -0800 | [diff] [blame] | 40 | if (!_tmp.ok()) return aidl_utils::binderStatusFromStatusT(_tmp.error()); \ |
| Ytai Ben-Tsvi | a381520 | 2020-10-28 14:58:08 -0700 | [diff] [blame] | 41 | std::move(_tmp.value()); }) |
| Ytai Ben-Tsvi | 10dc0a6 | 2020-09-18 11:31:55 -0700 | [diff] [blame] | 42 | |
| Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 43 | // ---------------------------------------------------------------------------- |
| Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 44 | |
| The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 45 | namespace android { |
| Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 46 | using aidl_utils::statusTFromBinderStatus; |
| Ytai Ben-Tsvi | 10dc0a6 | 2020-09-18 11:31:55 -0700 | [diff] [blame] | 47 | using binder::Status; |
| Mikhail Naganov | 57bd06f | 2021-08-10 16:41:54 -0700 | [diff] [blame] | 48 | using content::AttributionSourceState; |
| Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 49 | using media::IAudioPolicyService; |
| Mikhail Naganov | dbf0364 | 2021-08-25 18:15:32 -0700 | [diff] [blame] | 50 | using media::audio::common::AudioConfig; |
| 51 | using media::audio::common::AudioConfigBase; |
| Mikhail Naganov | f4a7536 | 2021-09-16 00:02:54 +0000 | [diff] [blame] | 52 | using media::audio::common::AudioDevice; |
| 53 | using media::audio::common::AudioDeviceDescription; |
| Mikhail Naganov | 57bd06f | 2021-08-10 16:41:54 -0700 | [diff] [blame] | 54 | using media::audio::common::AudioFormatDescription; |
| jiabin | e99d088 | 2021-09-17 05:21:25 +0000 | [diff] [blame] | 55 | using media::audio::common::AudioMMapPolicyInfo; |
| 56 | using media::audio::common::AudioMMapPolicyType; |
| Mikhail Naganov | dbf0364 | 2021-08-25 18:15:32 -0700 | [diff] [blame] | 57 | using media::audio::common::AudioOffloadInfo; |
| Mikhail Naganov | ddceecc | 2021-09-03 13:58:56 -0700 | [diff] [blame] | 58 | using media::audio::common::AudioSource; |
| Mikhail Naganov | dbf0364 | 2021-08-25 18:15:32 -0700 | [diff] [blame] | 59 | using media::audio::common::AudioStreamType; |
| 60 | using media::audio::common::AudioUsage; |
| Mikhail Naganov | 0078ee5 | 2021-09-30 23:06:20 +0000 | [diff] [blame] | 61 | using media::audio::common::Int; |
| Ytai Ben-Tsvi | 10dc0a6 | 2020-09-18 11:31:55 -0700 | [diff] [blame] | 62 | |
| The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 63 | // client singleton for AudioFlinger binder interface |
| 64 | Mutex AudioSystem::gLock; |
| Ytai Ben-Tsvi | 000c3e4 | 2020-01-09 15:26:40 -0800 | [diff] [blame] | 65 | Mutex AudioSystem::gLockErrorCallbacks; |
| Glenn Kasten | d2d089f | 2014-11-05 11:48:12 -0800 | [diff] [blame] | 66 | Mutex AudioSystem::gLockAPS; |
| The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 67 | sp<IAudioFlinger> AudioSystem::gAudioFlinger; |
| 68 | sp<AudioSystem::AudioFlingerClient> AudioSystem::gAudioFlingerClient; |
| Ytai Ben-Tsvi | 000c3e4 | 2020-01-09 15:26:40 -0800 | [diff] [blame] | 69 | std::set<audio_error_callback> AudioSystem::gAudioErrorCallbacks; |
| Jean-Michel Trivi | f613d42 | 2015-04-23 18:41:29 -0700 | [diff] [blame] | 70 | dynamic_policy_callback AudioSystem::gDynPolicyCallback = NULL; |
| Svet Ganov | f4ddfef | 2018-01-16 07:37:58 -0800 | [diff] [blame] | 71 | record_config_callback AudioSystem::gRecordConfigCallback = NULL; |
| Jean-Michel Trivi | 9a6b9ad | 2020-10-22 16:46:43 -0700 | [diff] [blame] | 72 | routing_callback AudioSystem::gRoutingCallback = NULL; |
| Jean-Michel Trivi | 78f2b30 | 2022-04-15 18:18:41 +0000 | [diff] [blame] | 73 | vol_range_init_req_callback AudioSystem::gVolRangeInitReqCallback = NULL; |
| The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 74 | |
| Ytai Ben-Tsvi | 5120eda | 2020-03-26 09:41:15 -0700 | [diff] [blame] | 75 | // Required to be held while calling into gSoundTriggerCaptureStateListener. |
| Ytai Ben-Tsvi | d9f8283 | 2020-08-27 09:31:10 -0700 | [diff] [blame] | 76 | class CaptureStateListenerImpl; |
| Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 77 | |
| Ytai Ben-Tsvi | 5120eda | 2020-03-26 09:41:15 -0700 | [diff] [blame] | 78 | Mutex gSoundTriggerCaptureStateListenerLock; |
| Ytai Ben-Tsvi | d9f8283 | 2020-08-27 09:31:10 -0700 | [diff] [blame] | 79 | sp<CaptureStateListenerImpl> gSoundTriggerCaptureStateListener = nullptr; |
| Ytai Ben-Tsvi | 5120eda | 2020-03-26 09:41:15 -0700 | [diff] [blame] | 80 | |
| Ahaan Ugale | 058f94b | 2021-05-21 02:23:50 -0700 | [diff] [blame] | 81 | // Binder for the AudioFlinger service that's passed to this client process from the system server. |
| 82 | // This allows specific isolated processes to access the audio system. Currently used only for the |
| 83 | // HotwordDetectionService. |
| Andy Hung | 938ef43 | 2023-01-04 11:45:50 -0800 | [diff] [blame] | 84 | static sp<IBinder> gAudioFlingerBinder = nullptr; |
| Ahaan Ugale | 058f94b | 2021-05-21 02:23:50 -0700 | [diff] [blame] | 85 | |
| 86 | void AudioSystem::setAudioFlingerBinder(const sp<IBinder>& audioFlinger) { |
| 87 | if (audioFlinger->getInterfaceDescriptor() != media::IAudioFlingerService::descriptor) { |
| 88 | ALOGE("setAudioFlingerBinder: received a binder of type %s", |
| 89 | String8(audioFlinger->getInterfaceDescriptor()).string()); |
| 90 | return; |
| 91 | } |
| 92 | Mutex::Autolock _l(gLock); |
| 93 | if (gAudioFlinger != nullptr) { |
| 94 | ALOGW("setAudioFlingerBinder: ignoring; AudioFlinger connection already established."); |
| 95 | return; |
| 96 | } |
| 97 | gAudioFlingerBinder = audioFlinger; |
| 98 | } |
| 99 | |
| Andy Hung | 938ef43 | 2023-01-04 11:45:50 -0800 | [diff] [blame] | 100 | static sp<IAudioFlinger> gLocalAudioFlinger; // set if we are local. |
| 101 | |
| 102 | status_t AudioSystem::setLocalAudioFlinger(const sp<IAudioFlinger>& af) { |
| 103 | Mutex::Autolock _l(gLock); |
| 104 | if (gAudioFlinger != nullptr) return INVALID_OPERATION; |
| 105 | gLocalAudioFlinger = af; |
| 106 | return OK; |
| 107 | } |
| 108 | |
| The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 109 | // establish binder interface to AudioFlinger service |
| Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 110 | const sp<IAudioFlinger> AudioSystem::get_audio_flinger() { |
| Eric Laurent | 0ebd5f9 | 2014-11-19 19:04:52 -0800 | [diff] [blame] | 111 | sp<IAudioFlinger> af; |
| 112 | sp<AudioFlingerClient> afc; |
| Mikhail Naganov | 69330d4 | 2020-04-08 19:29:50 +0000 | [diff] [blame] | 113 | bool reportNoError = false; |
| Eric Laurent | 0ebd5f9 | 2014-11-19 19:04:52 -0800 | [diff] [blame] | 114 | { |
| 115 | Mutex::Autolock _l(gLock); |
| Andy Hung | 938ef43 | 2023-01-04 11:45:50 -0800 | [diff] [blame] | 116 | if (gAudioFlinger != nullptr) { |
| 117 | return gAudioFlinger; |
| 118 | } |
| 119 | |
| 120 | if (gAudioFlingerClient == nullptr) { |
| 121 | gAudioFlingerClient = sp<AudioFlingerClient>::make(); |
| 122 | } else { |
| 123 | reportNoError = true; |
| 124 | } |
| 125 | |
| 126 | if (gLocalAudioFlinger != nullptr) { |
| 127 | gAudioFlinger = gLocalAudioFlinger; |
| 128 | } else { |
| Eric Laurent | 0ebd5f9 | 2014-11-19 19:04:52 -0800 | [diff] [blame] | 129 | sp<IBinder> binder; |
| Ahaan Ugale | 058f94b | 2021-05-21 02:23:50 -0700 | [diff] [blame] | 130 | if (gAudioFlingerBinder != nullptr) { |
| 131 | binder = gAudioFlingerBinder; |
| 132 | } else { |
| 133 | sp<IServiceManager> sm = defaultServiceManager(); |
| 134 | do { |
| 135 | binder = sm->getService(String16(IAudioFlinger::DEFAULT_SERVICE_NAME)); |
| Andy Hung | 938ef43 | 2023-01-04 11:45:50 -0800 | [diff] [blame] | 136 | if (binder != nullptr) break; |
| Ahaan Ugale | 058f94b | 2021-05-21 02:23:50 -0700 | [diff] [blame] | 137 | ALOGW("AudioFlinger not published, waiting..."); |
| 138 | usleep(500000); // 0.5 s |
| 139 | } while (true); |
| 140 | } |
| Eric Laurent | 0ebd5f9 | 2014-11-19 19:04:52 -0800 | [diff] [blame] | 141 | binder->linkToDeath(gAudioFlingerClient); |
| Andy Hung | 938ef43 | 2023-01-04 11:45:50 -0800 | [diff] [blame] | 142 | const auto afs = interface_cast<media::IAudioFlingerService>(binder); |
| 143 | LOG_ALWAYS_FATAL_IF(afs == nullptr); |
| 144 | gAudioFlinger = sp<AudioFlingerClientAdapter>::make(afs); |
| Glenn Kasten | e53b9ea | 2012-03-12 16:29:55 -0700 | [diff] [blame] | 145 | } |
| Andy Hung | 938ef43 | 2023-01-04 11:45:50 -0800 | [diff] [blame] | 146 | afc = gAudioFlingerClient; |
| Eric Laurent | 0ebd5f9 | 2014-11-19 19:04:52 -0800 | [diff] [blame] | 147 | af = gAudioFlinger; |
| Andy Hung | 938ef43 | 2023-01-04 11:45:50 -0800 | [diff] [blame] | 148 | // Make sure callbacks can be received by gAudioFlingerClient |
| 149 | ProcessState::self()->startThreadPool(); |
| The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 150 | } |
| Andy Hung | 938ef43 | 2023-01-04 11:45:50 -0800 | [diff] [blame] | 151 | const int64_t token = IPCThreadState::self()->clearCallingIdentity(); |
| 152 | af->registerClient(afc); |
| 153 | IPCThreadState::self()->restoreCallingIdentity(token); |
| Mikhail Naganov | 69330d4 | 2020-04-08 19:29:50 +0000 | [diff] [blame] | 154 | if (reportNoError) reportError(NO_ERROR); |
| Eric Laurent | 0ebd5f9 | 2014-11-19 19:04:52 -0800 | [diff] [blame] | 155 | return af; |
| The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 156 | } |
| 157 | |
| Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 158 | const sp<AudioSystem::AudioFlingerClient> AudioSystem::getAudioFlingerClient() { |
| Eric Laurent | 296fb13 | 2015-05-01 11:38:42 -0700 | [diff] [blame] | 159 | // calling get_audio_flinger() will initialize gAudioFlingerClient if needed |
| 160 | const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger(); |
| 161 | if (af == 0) return 0; |
| 162 | Mutex::Autolock _l(gLock); |
| 163 | return gAudioFlingerClient; |
| 164 | } |
| 165 | |
| Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 166 | sp<AudioIoDescriptor> AudioSystem::getIoDescriptor(audio_io_handle_t ioHandle) { |
| Eric Laurent | 296fb13 | 2015-05-01 11:38:42 -0700 | [diff] [blame] | 167 | sp<AudioIoDescriptor> desc; |
| 168 | const sp<AudioFlingerClient> afc = getAudioFlingerClient(); |
| 169 | if (afc != 0) { |
| 170 | desc = afc->getIoDescriptor(ioHandle); |
| 171 | } |
| 172 | return desc; |
| 173 | } |
| 174 | |
| Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 175 | /* static */ status_t AudioSystem::checkAudioFlinger() { |
| Eric Laurent | 4629161 | 2013-07-18 14:38:44 -0700 | [diff] [blame] | 176 | if (defaultServiceManager()->checkService(String16("media.audio_flinger")) != 0) { |
| 177 | return NO_ERROR; |
| 178 | } |
| 179 | return DEAD_OBJECT; |
| 180 | } |
| 181 | |
| Glenn Kasten | 4a8308b | 2016-04-18 14:10:01 -0700 | [diff] [blame] | 182 | // FIXME Declare in binder opcode order, similarly to IAudioFlinger.h and IAudioFlinger.cpp |
| 183 | |
| Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 184 | status_t AudioSystem::muteMicrophone(bool state) { |
| The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 185 | const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger(); |
| 186 | if (af == 0) return PERMISSION_DENIED; |
| 187 | return af->setMicMute(state); |
| 188 | } |
| 189 | |
| Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 190 | status_t AudioSystem::isMicrophoneMuted(bool* state) { |
| 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 | *state = af->getMicMute(); |
| 194 | return NO_ERROR; |
| 195 | } |
| 196 | |
| Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 197 | status_t AudioSystem::setMasterVolume(float value) { |
| The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 198 | const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger(); |
| 199 | if (af == 0) return PERMISSION_DENIED; |
| 200 | af->setMasterVolume(value); |
| 201 | return NO_ERROR; |
| 202 | } |
| 203 | |
| Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 204 | status_t AudioSystem::setMasterMute(bool mute) { |
| The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 205 | const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger(); |
| 206 | if (af == 0) return PERMISSION_DENIED; |
| 207 | af->setMasterMute(mute); |
| 208 | return NO_ERROR; |
| 209 | } |
| 210 | |
| Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 211 | status_t AudioSystem::getMasterVolume(float* volume) { |
| The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 212 | const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger(); |
| 213 | if (af == 0) return PERMISSION_DENIED; |
| 214 | *volume = af->masterVolume(); |
| 215 | return NO_ERROR; |
| 216 | } |
| 217 | |
| Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 218 | status_t AudioSystem::getMasterMute(bool* mute) { |
| The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 219 | const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger(); |
| 220 | if (af == 0) return PERMISSION_DENIED; |
| 221 | *mute = af->masterMute(); |
| 222 | return NO_ERROR; |
| 223 | } |
| 224 | |
| Glenn Kasten | 72ef00d | 2012-01-17 11:09:42 -0800 | [diff] [blame] | 225 | status_t AudioSystem::setStreamVolume(audio_stream_type_t stream, float value, |
| Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 226 | audio_io_handle_t output) { |
| Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 227 | 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] | 228 | const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger(); |
| 229 | if (af == 0) return PERMISSION_DENIED; |
| Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 230 | af->setStreamVolume(stream, value, output); |
| The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 231 | return NO_ERROR; |
| 232 | } |
| 233 | |
| Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 234 | status_t AudioSystem::setStreamMute(audio_stream_type_t stream, bool mute) { |
| Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 235 | 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] | 236 | const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger(); |
| 237 | if (af == 0) return PERMISSION_DENIED; |
| 238 | af->setStreamMute(stream, mute); |
| 239 | return NO_ERROR; |
| 240 | } |
| 241 | |
| Glenn Kasten | 72ef00d | 2012-01-17 11:09:42 -0800 | [diff] [blame] | 242 | status_t AudioSystem::getStreamVolume(audio_stream_type_t stream, float* volume, |
| Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 243 | audio_io_handle_t output) { |
| Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 244 | 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] | 245 | const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger(); |
| 246 | if (af == 0) return PERMISSION_DENIED; |
| Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 247 | *volume = af->streamVolume(stream, output); |
| The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 248 | return NO_ERROR; |
| 249 | } |
| 250 | |
| Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 251 | status_t AudioSystem::getStreamMute(audio_stream_type_t stream, bool* mute) { |
| Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 252 | 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] | 253 | const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger(); |
| 254 | if (af == 0) return PERMISSION_DENIED; |
| 255 | *mute = af->streamMute(stream); |
| 256 | return NO_ERROR; |
| 257 | } |
| 258 | |
| Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 259 | status_t AudioSystem::setMode(audio_mode_t mode) { |
| Glenn Kasten | 930f4ca | 2012-01-06 16:47:31 -0800 | [diff] [blame] | 260 | 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] | 261 | const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger(); |
| 262 | if (af == 0) return PERMISSION_DENIED; |
| 263 | return af->setMode(mode); |
| 264 | } |
| 265 | |
| Mikhail Naganov | e93a086 | 2023-03-15 17:06:59 -0700 | [diff] [blame] | 266 | status_t AudioSystem::setSimulateDeviceConnections(bool enabled) { |
| 267 | const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger(); |
| 268 | if (af == 0) return PERMISSION_DENIED; |
| 269 | return af->setSimulateDeviceConnections(enabled); |
| 270 | } |
| 271 | |
| Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 272 | status_t AudioSystem::setParameters(audio_io_handle_t ioHandle, const String8& keyValuePairs) { |
| The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 273 | const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger(); |
| 274 | if (af == 0) return PERMISSION_DENIED; |
| Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 275 | return af->setParameters(ioHandle, keyValuePairs); |
| 276 | } |
| 277 | |
| Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 278 | String8 AudioSystem::getParameters(audio_io_handle_t ioHandle, const String8& keys) { |
| Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 279 | const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger(); |
| 280 | String8 result = String8(""); |
| 281 | if (af == 0) return result; |
| 282 | |
| 283 | result = af->getParameters(ioHandle, keys); |
| 284 | return result; |
| The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 285 | } |
| 286 | |
| Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 287 | status_t AudioSystem::setParameters(const String8& keyValuePairs) { |
| Glenn Kasten | 142f519 | 2014-03-25 17:44:59 -0700 | [diff] [blame] | 288 | return setParameters(AUDIO_IO_HANDLE_NONE, keyValuePairs); |
| Glenn Kasten | c23885e | 2013-12-19 16:35:18 -0800 | [diff] [blame] | 289 | } |
| 290 | |
| Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 291 | String8 AudioSystem::getParameters(const String8& keys) { |
| Glenn Kasten | 142f519 | 2014-03-25 17:44:59 -0700 | [diff] [blame] | 292 | return getParameters(AUDIO_IO_HANDLE_NONE, keys); |
| Glenn Kasten | c23885e | 2013-12-19 16:35:18 -0800 | [diff] [blame] | 293 | } |
| 294 | |
| The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 295 | // convert volume steps to natural log scale |
| 296 | |
| 297 | // change this value to change volume scaling |
| 298 | static const float dBPerStep = 0.5f; |
| 299 | // shouldn't need to touch these |
| 300 | static const float dBConvert = -dBPerStep * 2.302585093f / 20.0f; |
| 301 | static const float dBConvertInverse = 1.0f / dBConvert; |
| 302 | |
| Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 303 | float AudioSystem::linearToLog(int volume) { |
| The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 304 | // float v = volume ? exp(float(100 - volume) * dBConvert) : 0; |
| Steve Block | b8a8052 | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 305 | // ALOGD("linearToLog(%d)=%f", volume, v); |
| The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 306 | // return v; |
| 307 | return volume ? exp(float(100 - volume) * dBConvert) : 0; |
| 308 | } |
| 309 | |
| Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 310 | int AudioSystem::logToLinear(float volume) { |
| The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 311 | // int v = volume ? 100 - int(dBConvertInverse * log(volume) + 0.5) : 0; |
| Steve Block | b8a8052 | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 312 | // ALOGD("logTolinear(%d)=%f", v, volume); |
| The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 313 | // return v; |
| 314 | return volume ? 100 - int(dBConvertInverse * log(volume) + 0.5) : 0; |
| 315 | } |
| 316 | |
| Eric Laurent | 21da647 | 2017-11-09 16:29:26 -0800 | [diff] [blame] | 317 | /* static */ size_t AudioSystem::calculateMinFrameCount( |
| 318 | uint32_t afLatencyMs, uint32_t afFrameCount, uint32_t afSampleRate, |
| Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 319 | uint32_t sampleRate, float speed /*, uint32_t notificationsPerBufferReq*/) { |
| Eric Laurent | 21da647 | 2017-11-09 16:29:26 -0800 | [diff] [blame] | 320 | // Ensure that buffer depth covers at least audio hardware latency |
| 321 | uint32_t minBufCount = afLatencyMs / ((1000 * afFrameCount) / afSampleRate); |
| 322 | if (minBufCount < 2) { |
| 323 | minBufCount = 2; |
| 324 | } |
| 325 | #if 0 |
| Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 326 | // The notificationsPerBufferReq parameter is not yet used for non-fast tracks, |
| 327 | // but keeping the code here to make it easier to add later. |
| 328 | if (minBufCount < notificationsPerBufferReq) { |
| 329 | minBufCount = notificationsPerBufferReq; |
| 330 | } |
| Eric Laurent | 21da647 | 2017-11-09 16:29:26 -0800 | [diff] [blame] | 331 | #endif |
| 332 | ALOGV("calculateMinFrameCount afLatency %u afFrameCount %u afSampleRate %u " |
| Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 333 | "sampleRate %u speed %f minBufCount: %u" /*" notificationsPerBufferReq %u"*/, |
| 334 | afLatencyMs, afFrameCount, afSampleRate, sampleRate, speed, minBufCount |
| 335 | /*, notificationsPerBufferReq*/); |
| Eric Laurent | 21da647 | 2017-11-09 16:29:26 -0800 | [diff] [blame] | 336 | return minBufCount * sourceFramesNeededWithTimestretch( |
| 337 | sampleRate, afFrameCount, afSampleRate, speed); |
| 338 | } |
| 339 | |
| 340 | |
| Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 341 | status_t |
| 342 | AudioSystem::getOutputSamplingRate(uint32_t* samplingRate, audio_stream_type_t streamType) { |
| Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 343 | audio_io_handle_t output; |
| The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 344 | |
| Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 345 | if (streamType == AUDIO_STREAM_DEFAULT) { |
| 346 | streamType = AUDIO_STREAM_MUSIC; |
| Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 347 | } |
| 348 | |
| Glenn Kasten | fff6d71 | 2012-01-12 16:38:12 -0800 | [diff] [blame] | 349 | output = getOutput(streamType); |
| Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 350 | if (output == 0) { |
| 351 | return PERMISSION_DENIED; |
| 352 | } |
| 353 | |
| Jean-Michel Trivi | b7f24b1 | 2014-06-11 10:05:30 -0700 | [diff] [blame] | 354 | return getSamplingRate(output, samplingRate); |
| Eric Laurent | 1a9ed11 | 2012-03-20 18:36:01 -0700 | [diff] [blame] | 355 | } |
| 356 | |
| Glenn Kasten | 2c073da | 2016-02-26 09:14:08 -0800 | [diff] [blame] | 357 | status_t AudioSystem::getSamplingRate(audio_io_handle_t ioHandle, |
| Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 358 | uint32_t* samplingRate) { |
| Eric Laurent | f6778fd | 2014-11-18 17:26:58 -0800 | [diff] [blame] | 359 | const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger(); |
| 360 | if (af == 0) return PERMISSION_DENIED; |
| Glenn Kasten | 2c073da | 2016-02-26 09:14:08 -0800 | [diff] [blame] | 361 | sp<AudioIoDescriptor> desc = getIoDescriptor(ioHandle); |
| 362 | if (desc == 0) { |
| 363 | *samplingRate = af->sampleRate(ioHandle); |
| Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 364 | } else { |
| Mikhail Naganov | 88536df | 2021-07-26 17:30:29 -0700 | [diff] [blame] | 365 | *samplingRate = desc->getSamplingRate(); |
| Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 366 | } |
| Glenn Kasten | f94006c | 2014-01-08 08:56:06 -0800 | [diff] [blame] | 367 | if (*samplingRate == 0) { |
| Glenn Kasten | 2c073da | 2016-02-26 09:14:08 -0800 | [diff] [blame] | 368 | ALOGE("AudioSystem::getSamplingRate failed for ioHandle %d", ioHandle); |
| Glenn Kasten | f94006c | 2014-01-08 08:56:06 -0800 | [diff] [blame] | 369 | return BAD_VALUE; |
| 370 | } |
| Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 371 | |
| Glenn Kasten | 2c073da | 2016-02-26 09:14:08 -0800 | [diff] [blame] | 372 | ALOGV("getSamplingRate() ioHandle %d, sampling rate %u", ioHandle, *samplingRate); |
| Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 373 | |
| The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 374 | return NO_ERROR; |
| 375 | } |
| 376 | |
| Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 377 | status_t AudioSystem::getOutputFrameCount(size_t* frameCount, audio_stream_type_t streamType) { |
| Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 378 | audio_io_handle_t output; |
| The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 379 | |
| Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 380 | if (streamType == AUDIO_STREAM_DEFAULT) { |
| 381 | streamType = AUDIO_STREAM_MUSIC; |
| Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 382 | } |
| Eric Laurent | 48f7f5e | 2009-04-02 09:32:43 -0700 | [diff] [blame] | 383 | |
| Glenn Kasten | fff6d71 | 2012-01-12 16:38:12 -0800 | [diff] [blame] | 384 | output = getOutput(streamType); |
| Glenn Kasten | 142f519 | 2014-03-25 17:44:59 -0700 | [diff] [blame] | 385 | if (output == AUDIO_IO_HANDLE_NONE) { |
| Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 386 | return PERMISSION_DENIED; |
| 387 | } |
| The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 388 | |
| Jean-Michel Trivi | b7f24b1 | 2014-06-11 10:05:30 -0700 | [diff] [blame] | 389 | return getFrameCount(output, frameCount); |
| Eric Laurent | 1a9ed11 | 2012-03-20 18:36:01 -0700 | [diff] [blame] | 390 | } |
| 391 | |
| Glenn Kasten | 2c073da | 2016-02-26 09:14:08 -0800 | [diff] [blame] | 392 | status_t AudioSystem::getFrameCount(audio_io_handle_t ioHandle, |
| Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 393 | size_t* frameCount) { |
| Eric Laurent | f6778fd | 2014-11-18 17:26:58 -0800 | [diff] [blame] | 394 | const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger(); |
| 395 | if (af == 0) return PERMISSION_DENIED; |
| Glenn Kasten | 2c073da | 2016-02-26 09:14:08 -0800 | [diff] [blame] | 396 | sp<AudioIoDescriptor> desc = getIoDescriptor(ioHandle); |
| 397 | if (desc == 0) { |
| 398 | *frameCount = af->frameCount(ioHandle); |
| Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 399 | } else { |
| Mikhail Naganov | 88536df | 2021-07-26 17:30:29 -0700 | [diff] [blame] | 400 | *frameCount = desc->getFrameCount(); |
| Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 401 | } |
| Glenn Kasten | f94006c | 2014-01-08 08:56:06 -0800 | [diff] [blame] | 402 | if (*frameCount == 0) { |
| Glenn Kasten | 2c073da | 2016-02-26 09:14:08 -0800 | [diff] [blame] | 403 | ALOGE("AudioSystem::getFrameCount failed for ioHandle %d", ioHandle); |
| Glenn Kasten | f94006c | 2014-01-08 08:56:06 -0800 | [diff] [blame] | 404 | return BAD_VALUE; |
| 405 | } |
| Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 406 | |
| Glenn Kasten | 2c073da | 2016-02-26 09:14:08 -0800 | [diff] [blame] | 407 | ALOGV("getFrameCount() ioHandle %d, frameCount %zu", ioHandle, *frameCount); |
| Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 408 | |
| The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 409 | return NO_ERROR; |
| 410 | } |
| 411 | |
| Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 412 | status_t AudioSystem::getOutputLatency(uint32_t* latency, audio_stream_type_t streamType) { |
| Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 413 | audio_io_handle_t output; |
| The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 414 | |
| Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 415 | if (streamType == AUDIO_STREAM_DEFAULT) { |
| 416 | streamType = AUDIO_STREAM_MUSIC; |
| Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 417 | } |
| Eric Laurent | 48f7f5e | 2009-04-02 09:32:43 -0700 | [diff] [blame] | 418 | |
| Glenn Kasten | fff6d71 | 2012-01-12 16:38:12 -0800 | [diff] [blame] | 419 | output = getOutput(streamType); |
| Glenn Kasten | 142f519 | 2014-03-25 17:44:59 -0700 | [diff] [blame] | 420 | if (output == AUDIO_IO_HANDLE_NONE) { |
| Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 421 | return PERMISSION_DENIED; |
| 422 | } |
| The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 423 | |
| Glenn Kasten | 241618f | 2014-03-25 17:48:57 -0700 | [diff] [blame] | 424 | return getLatency(output, latency); |
| Eric Laurent | 1a9ed11 | 2012-03-20 18:36:01 -0700 | [diff] [blame] | 425 | } |
| 426 | |
| 427 | status_t AudioSystem::getLatency(audio_io_handle_t output, |
| Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 428 | uint32_t* latency) { |
| Eric Laurent | f6778fd | 2014-11-18 17:26:58 -0800 | [diff] [blame] | 429 | const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger(); |
| 430 | if (af == 0) return PERMISSION_DENIED; |
| Eric Laurent | 296fb13 | 2015-05-01 11:38:42 -0700 | [diff] [blame] | 431 | sp<AudioIoDescriptor> outputDesc = getIoDescriptor(output); |
| Eric Laurent | 73e26b6 | 2015-04-27 16:55:58 -0700 | [diff] [blame] | 432 | if (outputDesc == 0) { |
| Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 433 | *latency = af->latency(output); |
| 434 | } else { |
| Mikhail Naganov | 88536df | 2021-07-26 17:30:29 -0700 | [diff] [blame] | 435 | *latency = outputDesc->getLatency(); |
| Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 436 | } |
| 437 | |
| Glenn Kasten | 241618f | 2014-03-25 17:48:57 -0700 | [diff] [blame] | 438 | ALOGV("getLatency() output %d, latency %d", output, *latency); |
| Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 439 | |
| The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 440 | return NO_ERROR; |
| 441 | } |
| 442 | |
| Glenn Kasten | dd8104c | 2012-07-02 12:42:44 -0700 | [diff] [blame] | 443 | status_t AudioSystem::getInputBufferSize(uint32_t sampleRate, audio_format_t format, |
| Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 444 | audio_channel_mask_t channelMask, size_t* buffSize) { |
| Eric Laurent | 296fb13 | 2015-05-01 11:38:42 -0700 | [diff] [blame] | 445 | const sp<AudioFlingerClient> afc = getAudioFlingerClient(); |
| 446 | if (afc == 0) { |
| 447 | return NO_INIT; |
| 448 | } |
| 449 | return afc->getInputBufferSize(sampleRate, format, channelMask, buffSize); |
| The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 450 | } |
| 451 | |
| Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 452 | status_t AudioSystem::setVoiceVolume(float value) { |
| Eric Laurent | f0ee6f4 | 2009-10-21 08:14:22 -0700 | [diff] [blame] | 453 | const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger(); |
| 454 | if (af == 0) return PERMISSION_DENIED; |
| 455 | return af->setVoiceVolume(value); |
| 456 | } |
| 457 | |
| Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 458 | status_t AudioSystem::getRenderPosition(audio_io_handle_t output, uint32_t* halFrames, |
| 459 | uint32_t* dspFrames) { |
| Eric Laurent | 342e9cf | 2010-01-19 17:37:09 -0800 | [diff] [blame] | 460 | const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger(); |
| 461 | if (af == 0) return PERMISSION_DENIED; |
| 462 | |
| Richard Fitzgerald | ad3af33 | 2013-03-25 16:54:37 +0000 | [diff] [blame] | 463 | return af->getRenderPosition(halFrames, dspFrames, output); |
| Eric Laurent | 342e9cf | 2010-01-19 17:37:09 -0800 | [diff] [blame] | 464 | } |
| 465 | |
| Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 466 | uint32_t AudioSystem::getInputFramesLost(audio_io_handle_t ioHandle) { |
| Eric Laurent | 05bca2f | 2010-02-26 02:47:27 -0800 | [diff] [blame] | 467 | const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger(); |
| Glenn Kasten | 5f972c0 | 2014-01-13 09:59:31 -0800 | [diff] [blame] | 468 | uint32_t result = 0; |
| Eric Laurent | 05bca2f | 2010-02-26 02:47:27 -0800 | [diff] [blame] | 469 | if (af == 0) return result; |
| Glenn Kasten | 142f519 | 2014-03-25 17:44:59 -0700 | [diff] [blame] | 470 | if (ioHandle == AUDIO_IO_HANDLE_NONE) return result; |
| Eric Laurent | 05bca2f | 2010-02-26 02:47:27 -0800 | [diff] [blame] | 471 | |
| 472 | result = af->getInputFramesLost(ioHandle); |
| 473 | return result; |
| 474 | } |
| 475 | |
| Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 476 | audio_unique_id_t AudioSystem::newAudioUniqueId(audio_unique_id_use_t use) { |
| Mikhail Naganov | 2996f67 | 2019-04-18 12:29:59 -0700 | [diff] [blame] | 477 | // Must not use AF as IDs will re-roll on audioserver restart, b/130369529. |
| Eric Laurent | be916aa | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 478 | const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger(); |
| Eric Laurent | de3f839 | 2014-07-27 18:38:22 -0700 | [diff] [blame] | 479 | if (af == 0) return AUDIO_UNIQUE_ID_ALLOCATE; |
| Glenn Kasten | eeecb98 | 2016-02-26 10:44:04 -0800 | [diff] [blame] | 480 | return af->newAudioUniqueId(use); |
| Eric Laurent | be916aa | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 481 | } |
| 482 | |
| Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 483 | void AudioSystem::acquireAudioSessionId(audio_session_t audioSession, pid_t pid, uid_t uid) { |
| Marco Nelissen | 3a34bef | 2011-08-02 13:33:41 -0700 | [diff] [blame] | 484 | const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger(); |
| 485 | if (af != 0) { |
| Andy Hung | 8b0bfd9 | 2019-12-23 13:11:11 -0800 | [diff] [blame] | 486 | af->acquireAudioSessionId(audioSession, pid, uid); |
| Marco Nelissen | 3a34bef | 2011-08-02 13:33:41 -0700 | [diff] [blame] | 487 | } |
| 488 | } |
| 489 | |
| Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 490 | void AudioSystem::releaseAudioSessionId(audio_session_t audioSession, pid_t pid) { |
| Marco Nelissen | 3a34bef | 2011-08-02 13:33:41 -0700 | [diff] [blame] | 491 | const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger(); |
| 492 | if (af != 0) { |
| Marco Nelissen | d457c97 | 2014-02-11 08:47:07 -0800 | [diff] [blame] | 493 | af->releaseAudioSessionId(audioSession, pid); |
| Marco Nelissen | 3a34bef | 2011-08-02 13:33:41 -0700 | [diff] [blame] | 494 | } |
| 495 | } |
| 496 | |
| Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 497 | audio_hw_sync_t AudioSystem::getAudioHwSyncForSession(audio_session_t sessionId) { |
| Eric Laurent | 93c3d41 | 2014-08-01 14:48:35 -0700 | [diff] [blame] | 498 | const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger(); |
| 499 | if (af == 0) return AUDIO_HW_SYNC_INVALID; |
| 500 | return af->getAudioHwSyncForSession(sessionId); |
| 501 | } |
| 502 | |
| Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 503 | status_t AudioSystem::systemReady() { |
| Eric Laurent | 72e3f39 | 2015-05-20 14:43:50 -0700 | [diff] [blame] | 504 | const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger(); |
| 505 | if (af == 0) return NO_INIT; |
| 506 | return af->systemReady(); |
| 507 | } |
| 508 | |
| Eric Laurent | d66d7a1 | 2021-07-13 13:35:32 +0200 | [diff] [blame] | 509 | status_t AudioSystem::audioPolicyReady() { |
| 510 | const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger(); |
| 511 | if (af == 0) return NO_INIT; |
| 512 | return af->audioPolicyReady(); |
| 513 | } |
| 514 | |
| Glenn Kasten | 4a8308b | 2016-04-18 14:10:01 -0700 | [diff] [blame] | 515 | status_t AudioSystem::getFrameCountHAL(audio_io_handle_t ioHandle, |
| Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 516 | size_t* frameCount) { |
| Glenn Kasten | 4a8308b | 2016-04-18 14:10:01 -0700 | [diff] [blame] | 517 | const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger(); |
| 518 | if (af == 0) return PERMISSION_DENIED; |
| 519 | sp<AudioIoDescriptor> desc = getIoDescriptor(ioHandle); |
| 520 | if (desc == 0) { |
| 521 | *frameCount = af->frameCountHAL(ioHandle); |
| 522 | } else { |
| Mikhail Naganov | 88536df | 2021-07-26 17:30:29 -0700 | [diff] [blame] | 523 | *frameCount = desc->getFrameCountHAL(); |
| Glenn Kasten | 4a8308b | 2016-04-18 14:10:01 -0700 | [diff] [blame] | 524 | } |
| 525 | if (*frameCount == 0) { |
| 526 | ALOGE("AudioSystem::getFrameCountHAL failed for ioHandle %d", ioHandle); |
| 527 | return BAD_VALUE; |
| 528 | } |
| 529 | |
| 530 | ALOGV("getFrameCountHAL() ioHandle %d, frameCount %zu", ioHandle, *frameCount); |
| 531 | |
| 532 | return NO_ERROR; |
| 533 | } |
| 534 | |
| The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 535 | // --------------------------------------------------------------------------- |
| 536 | |
| Eric Laurent | 73e26b6 | 2015-04-27 16:55:58 -0700 | [diff] [blame] | 537 | |
| Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 538 | void AudioSystem::AudioFlingerClient::clearIoCache() { |
| Eric Laurent | 73e26b6 | 2015-04-27 16:55:58 -0700 | [diff] [blame] | 539 | Mutex::Autolock _l(mLock); |
| 540 | mIoDescriptors.clear(); |
| 541 | mInBuffSize = 0; |
| 542 | mInSamplingRate = 0; |
| 543 | mInFormat = AUDIO_FORMAT_DEFAULT; |
| 544 | mInChannelMask = AUDIO_CHANNEL_NONE; |
| 545 | } |
| 546 | |
| Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 547 | void AudioSystem::AudioFlingerClient::binderDied(const wp<IBinder>& who __unused) { |
| Eric Laurent | f6778fd | 2014-11-18 17:26:58 -0800 | [diff] [blame] | 548 | { |
| 549 | Mutex::Autolock _l(AudioSystem::gLock); |
| 550 | AudioSystem::gAudioFlinger.clear(); |
| Eric Laurent | f6778fd | 2014-11-18 17:26:58 -0800 | [diff] [blame] | 551 | } |
| The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 552 | |
| Eric Laurent | 73e26b6 | 2015-04-27 16:55:58 -0700 | [diff] [blame] | 553 | // clear output handles and stream to output map caches |
| 554 | clearIoCache(); |
| The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 555 | |
| Ytai Ben-Tsvi | 000c3e4 | 2020-01-09 15:26:40 -0800 | [diff] [blame] | 556 | reportError(DEAD_OBJECT); |
| 557 | |
| Steve Block | 5ff1dd5 | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 558 | ALOGW("AudioFlinger server died!"); |
| The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 559 | } |
| 560 | |
| Ytai Ben-Tsvi | 10dc0a6 | 2020-09-18 11:31:55 -0700 | [diff] [blame] | 561 | Status AudioSystem::AudioFlingerClient::ioConfigChanged( |
| 562 | media::AudioIoConfigEvent _event, |
| 563 | const media::AudioIoDescriptor& _ioDesc) { |
| Mikhail Naganov | 88536df | 2021-07-26 17:30:29 -0700 | [diff] [blame] | 564 | audio_io_config_event_t event = VALUE_OR_RETURN_BINDER_STATUS( |
| 565 | aidl2legacy_AudioIoConfigEvent_audio_io_config_event_t(_event)); |
| Ytai Ben-Tsvi | 10dc0a6 | 2020-09-18 11:31:55 -0700 | [diff] [blame] | 566 | sp<AudioIoDescriptor> ioDesc( |
| Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 567 | VALUE_OR_RETURN_BINDER_STATUS( |
| 568 | aidl2legacy_AudioIoDescriptor_AudioIoDescriptor(_ioDesc))); |
| Ytai Ben-Tsvi | 10dc0a6 | 2020-09-18 11:31:55 -0700 | [diff] [blame] | 569 | |
| Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 570 | ALOGV("ioConfigChanged() event %d", event); |
| Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 571 | |
| Mikhail Naganov | 88536df | 2021-07-26 17:30:29 -0700 | [diff] [blame] | 572 | if (ioDesc->getIoHandle() == AUDIO_IO_HANDLE_NONE) return Status::ok(); |
| Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 573 | |
| Eric Laurent | 296fb13 | 2015-05-01 11:38:42 -0700 | [diff] [blame] | 574 | audio_port_handle_t deviceId = AUDIO_PORT_HANDLE_NONE; |
| Eric Laurent | 09f1ed2 | 2019-04-24 17:45:17 -0700 | [diff] [blame] | 575 | std::vector<sp<AudioDeviceCallback>> callbacksToCall; |
| Eric Laurent | 296fb13 | 2015-05-01 11:38:42 -0700 | [diff] [blame] | 576 | { |
| 577 | Mutex::Autolock _l(mLock); |
| Eric Laurent | 09f1ed2 | 2019-04-24 17:45:17 -0700 | [diff] [blame] | 578 | auto callbacks = std::map<audio_port_handle_t, wp<AudioDeviceCallback>>(); |
| Eric Laurent | 296fb13 | 2015-05-01 11:38:42 -0700 | [diff] [blame] | 579 | |
| 580 | switch (event) { |
| Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 581 | case AUDIO_OUTPUT_OPENED: |
| 582 | case AUDIO_OUTPUT_REGISTERED: |
| 583 | case AUDIO_INPUT_OPENED: |
| 584 | case AUDIO_INPUT_REGISTERED: { |
| Mikhail Naganov | 88536df | 2021-07-26 17:30:29 -0700 | [diff] [blame] | 585 | sp<AudioIoDescriptor> oldDesc = getIoDescriptor_l(ioDesc->getIoHandle()); |
| Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 586 | if (oldDesc == 0) { |
| Mikhail Naganov | 88536df | 2021-07-26 17:30:29 -0700 | [diff] [blame] | 587 | mIoDescriptors.add(ioDesc->getIoHandle(), ioDesc); |
| Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 588 | } else { |
| 589 | deviceId = oldDesc->getDeviceId(); |
| Mikhail Naganov | 88536df | 2021-07-26 17:30:29 -0700 | [diff] [blame] | 590 | mIoDescriptors.replaceValueFor(ioDesc->getIoHandle(), ioDesc); |
| Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 591 | } |
| 592 | |
| 593 | if (ioDesc->getDeviceId() != AUDIO_PORT_HANDLE_NONE) { |
| 594 | deviceId = ioDesc->getDeviceId(); |
| 595 | if (event == AUDIO_OUTPUT_OPENED || event == AUDIO_INPUT_OPENED) { |
| Mikhail Naganov | 88536df | 2021-07-26 17:30:29 -0700 | [diff] [blame] | 596 | auto it = mAudioDeviceCallbacks.find(ioDesc->getIoHandle()); |
| Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 597 | if (it != mAudioDeviceCallbacks.end()) { |
| 598 | callbacks = it->second; |
| 599 | } |
| 600 | } |
| 601 | } |
| Mikhail Naganov | 88536df | 2021-07-26 17:30:29 -0700 | [diff] [blame] | 602 | ALOGV("ioConfigChanged() new %s %s %s", |
| Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 603 | event == AUDIO_OUTPUT_OPENED || event == AUDIO_OUTPUT_REGISTERED ? |
| 604 | "output" : "input", |
| 605 | event == AUDIO_OUTPUT_OPENED || event == AUDIO_INPUT_OPENED ? |
| 606 | "opened" : "registered", |
| Mikhail Naganov | 88536df | 2021-07-26 17:30:29 -0700 | [diff] [blame] | 607 | ioDesc->toDebugString().c_str()); |
| Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 608 | } |
| 609 | break; |
| 610 | case AUDIO_OUTPUT_CLOSED: |
| 611 | case AUDIO_INPUT_CLOSED: { |
| Mikhail Naganov | 88536df | 2021-07-26 17:30:29 -0700 | [diff] [blame] | 612 | if (getIoDescriptor_l(ioDesc->getIoHandle()) == 0) { |
| Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 613 | ALOGW("ioConfigChanged() closing unknown %s %d", |
| Mikhail Naganov | 88536df | 2021-07-26 17:30:29 -0700 | [diff] [blame] | 614 | event == AUDIO_OUTPUT_CLOSED ? "output" : "input", ioDesc->getIoHandle()); |
| Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 615 | break; |
| 616 | } |
| 617 | ALOGV("ioConfigChanged() %s %d closed", |
| Mikhail Naganov | 88536df | 2021-07-26 17:30:29 -0700 | [diff] [blame] | 618 | event == AUDIO_OUTPUT_CLOSED ? "output" : "input", ioDesc->getIoHandle()); |
| Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 619 | |
| Mikhail Naganov | 88536df | 2021-07-26 17:30:29 -0700 | [diff] [blame] | 620 | mIoDescriptors.removeItem(ioDesc->getIoHandle()); |
| 621 | mAudioDeviceCallbacks.erase(ioDesc->getIoHandle()); |
| Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 622 | } |
| 623 | break; |
| 624 | |
| 625 | case AUDIO_OUTPUT_CONFIG_CHANGED: |
| 626 | case AUDIO_INPUT_CONFIG_CHANGED: { |
| Mikhail Naganov | 88536df | 2021-07-26 17:30:29 -0700 | [diff] [blame] | 627 | sp<AudioIoDescriptor> oldDesc = getIoDescriptor_l(ioDesc->getIoHandle()); |
| Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 628 | if (oldDesc == 0) { |
| 629 | ALOGW("ioConfigChanged() modifying unknown %s! %d", |
| 630 | event == AUDIO_OUTPUT_CONFIG_CHANGED ? "output" : "input", |
| Mikhail Naganov | 88536df | 2021-07-26 17:30:29 -0700 | [diff] [blame] | 631 | ioDesc->getIoHandle()); |
| Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 632 | break; |
| 633 | } |
| 634 | |
| Eric Laurent | 7c1ec5f | 2015-07-09 14:52:47 -0700 | [diff] [blame] | 635 | deviceId = oldDesc->getDeviceId(); |
| Mikhail Naganov | 88536df | 2021-07-26 17:30:29 -0700 | [diff] [blame] | 636 | mIoDescriptors.replaceValueFor(ioDesc->getIoHandle(), ioDesc); |
| Eric Laurent | 296fb13 | 2015-05-01 11:38:42 -0700 | [diff] [blame] | 637 | |
| Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 638 | if (deviceId != ioDesc->getDeviceId()) { |
| 639 | deviceId = ioDesc->getDeviceId(); |
| Mikhail Naganov | 88536df | 2021-07-26 17:30:29 -0700 | [diff] [blame] | 640 | auto it = mAudioDeviceCallbacks.find(ioDesc->getIoHandle()); |
| Eric Laurent | 09f1ed2 | 2019-04-24 17:45:17 -0700 | [diff] [blame] | 641 | if (it != mAudioDeviceCallbacks.end()) { |
| 642 | callbacks = it->second; |
| Francois Gaffie | 24a9fb0 | 2019-01-18 17:51:34 +0100 | [diff] [blame] | 643 | } |
| 644 | } |
| Mikhail Naganov | 88536df | 2021-07-26 17:30:29 -0700 | [diff] [blame] | 645 | ALOGV("ioConfigChanged() new config for %s %s", |
| Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 646 | event == AUDIO_OUTPUT_CONFIG_CHANGED ? "output" : "input", |
| Mikhail Naganov | 88536df | 2021-07-26 17:30:29 -0700 | [diff] [blame] | 647 | ioDesc->toDebugString().c_str()); |
| Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 648 | |
| Eric Laurent | 296fb13 | 2015-05-01 11:38:42 -0700 | [diff] [blame] | 649 | } |
| Eric Laurent | 296fb13 | 2015-05-01 11:38:42 -0700 | [diff] [blame] | 650 | break; |
| Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 651 | case AUDIO_CLIENT_STARTED: { |
| Mikhail Naganov | 88536df | 2021-07-26 17:30:29 -0700 | [diff] [blame] | 652 | sp<AudioIoDescriptor> oldDesc = getIoDescriptor_l(ioDesc->getIoHandle()); |
| Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 653 | if (oldDesc == 0) { |
| Mikhail Naganov | 88536df | 2021-07-26 17:30:29 -0700 | [diff] [blame] | 654 | ALOGW("ioConfigChanged() start client on unknown io! %d", |
| 655 | ioDesc->getIoHandle()); |
| Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 656 | break; |
| 657 | } |
| 658 | ALOGV("ioConfigChanged() AUDIO_CLIENT_STARTED io %d port %d num callbacks %zu", |
| Mikhail Naganov | 88536df | 2021-07-26 17:30:29 -0700 | [diff] [blame] | 659 | ioDesc->getIoHandle(), ioDesc->getPortId(), mAudioDeviceCallbacks.size()); |
| 660 | oldDesc->setPatch(ioDesc->getPatch()); |
| 661 | auto it = mAudioDeviceCallbacks.find(ioDesc->getIoHandle()); |
| Eric Laurent | 09f1ed2 | 2019-04-24 17:45:17 -0700 | [diff] [blame] | 662 | if (it != mAudioDeviceCallbacks.end()) { |
| Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 663 | auto cbks = it->second; |
| Mikhail Naganov | 88536df | 2021-07-26 17:30:29 -0700 | [diff] [blame] | 664 | auto it2 = cbks.find(ioDesc->getPortId()); |
| Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 665 | if (it2 != cbks.end()) { |
| Mikhail Naganov | 88536df | 2021-07-26 17:30:29 -0700 | [diff] [blame] | 666 | callbacks.emplace(ioDesc->getPortId(), it2->second); |
| Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 667 | deviceId = oldDesc->getDeviceId(); |
| 668 | } |
| Eric Laurent | 09f1ed2 | 2019-04-24 17:45:17 -0700 | [diff] [blame] | 669 | } |
| Eric Laurent | 296fb13 | 2015-05-01 11:38:42 -0700 | [diff] [blame] | 670 | } |
| Eric Laurent | 09f1ed2 | 2019-04-24 17:45:17 -0700 | [diff] [blame] | 671 | break; |
| Eric Laurent | 09f1ed2 | 2019-04-24 17:45:17 -0700 | [diff] [blame] | 672 | } |
| 673 | |
| 674 | for (auto wpCbk : callbacks) { |
| 675 | sp<AudioDeviceCallback> spCbk = wpCbk.second.promote(); |
| 676 | if (spCbk != nullptr) { |
| 677 | callbacksToCall.push_back(spCbk); |
| 678 | } |
| Eric Laurent | ad2e7b9 | 2017-09-14 20:06:42 -0700 | [diff] [blame] | 679 | } |
| Eric Laurent | 4463ff5 | 2019-02-07 13:56:09 -0800 | [diff] [blame] | 680 | } |
| 681 | |
| 682 | // Callbacks must be called without mLock held. May lead to dead lock if calling for |
| 683 | // example getRoutedDevice that updates the device and tries to acquire mLock. |
| Eric Laurent | 09f1ed2 | 2019-04-24 17:45:17 -0700 | [diff] [blame] | 684 | for (auto cb : callbacksToCall) { |
| Mikhail Naganov | 88536df | 2021-07-26 17:30:29 -0700 | [diff] [blame] | 685 | // If callbacksToCall is not empty, it implies ioDesc->getIoHandle() and deviceId are valid |
| 686 | cb->onAudioDeviceUpdate(ioDesc->getIoHandle(), deviceId); |
| Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 687 | } |
| Ytai Ben-Tsvi | 10dc0a6 | 2020-09-18 11:31:55 -0700 | [diff] [blame] | 688 | |
| 689 | return Status::ok(); |
| The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 690 | } |
| 691 | |
| Eric Laurent | 0825031 | 2022-05-03 18:12:28 +0200 | [diff] [blame] | 692 | Status AudioSystem::AudioFlingerClient::onSupportedLatencyModesChanged( |
| Mikhail Naganov | b1a075b | 2022-12-18 02:48:14 +0000 | [diff] [blame] | 693 | int output, const std::vector<media::audio::common::AudioLatencyMode>& latencyModes) { |
| Eric Laurent | 0825031 | 2022-05-03 18:12:28 +0200 | [diff] [blame] | 694 | audio_io_handle_t outputLegacy = VALUE_OR_RETURN_BINDER_STATUS( |
| 695 | aidl2legacy_int32_t_audio_io_handle_t(output)); |
| 696 | std::vector<audio_latency_mode_t> modesLegacy = VALUE_OR_RETURN_BINDER_STATUS( |
| 697 | convertContainer<std::vector<audio_latency_mode_t>>( |
| Mikhail Naganov | b1a075b | 2022-12-18 02:48:14 +0000 | [diff] [blame] | 698 | latencyModes, aidl2legacy_AudioLatencyMode_audio_latency_mode_t)); |
| Eric Laurent | 0825031 | 2022-05-03 18:12:28 +0200 | [diff] [blame] | 699 | |
| 700 | std::vector<sp<SupportedLatencyModesCallback>> callbacks; |
| 701 | { |
| 702 | Mutex::Autolock _l(mLock); |
| 703 | for (auto callback : mSupportedLatencyModesCallbacks) { |
| 704 | if (auto ref = callback.promote(); ref != nullptr) { |
| 705 | callbacks.push_back(ref); |
| 706 | } |
| 707 | } |
| 708 | } |
| 709 | for (const auto& callback : callbacks) { |
| 710 | callback->onSupportedLatencyModesChanged(outputLegacy, modesLegacy); |
| 711 | } |
| 712 | |
| 713 | return Status::ok(); |
| 714 | } |
| 715 | |
| Eric Laurent | 73e26b6 | 2015-04-27 16:55:58 -0700 | [diff] [blame] | 716 | status_t AudioSystem::AudioFlingerClient::getInputBufferSize( |
| Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 717 | uint32_t sampleRate, audio_format_t format, |
| 718 | audio_channel_mask_t channelMask, size_t* buffSize) { |
| Eric Laurent | 73e26b6 | 2015-04-27 16:55:58 -0700 | [diff] [blame] | 719 | const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger(); |
| 720 | if (af == 0) { |
| 721 | return PERMISSION_DENIED; |
| 722 | } |
| 723 | Mutex::Autolock _l(mLock); |
| 724 | // Do we have a stale mInBuffSize or are we requesting the input buffer size for new values |
| 725 | if ((mInBuffSize == 0) || (sampleRate != mInSamplingRate) || (format != mInFormat) |
| 726 | || (channelMask != mInChannelMask)) { |
| 727 | size_t inBuffSize = af->getInputBufferSize(sampleRate, format, channelMask); |
| 728 | if (inBuffSize == 0) { |
| Glenn Kasten | 49f36ba | 2017-12-06 13:02:02 -0800 | [diff] [blame] | 729 | ALOGE("AudioSystem::getInputBufferSize failed sampleRate %d format %#x channelMask %#x", |
| Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 730 | sampleRate, format, channelMask); |
| Eric Laurent | 73e26b6 | 2015-04-27 16:55:58 -0700 | [diff] [blame] | 731 | return BAD_VALUE; |
| 732 | } |
| 733 | // A benign race is possible here: we could overwrite a fresher cache entry |
| 734 | // save the request params |
| 735 | mInSamplingRate = sampleRate; |
| 736 | mInFormat = format; |
| 737 | mInChannelMask = channelMask; |
| 738 | |
| 739 | mInBuffSize = inBuffSize; |
| 740 | } |
| 741 | |
| 742 | *buffSize = mInBuffSize; |
| 743 | |
| 744 | return NO_ERROR; |
| 745 | } |
| 746 | |
| Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 747 | sp<AudioIoDescriptor> |
| 748 | AudioSystem::AudioFlingerClient::getIoDescriptor_l(audio_io_handle_t ioHandle) { |
| Eric Laurent | 73e26b6 | 2015-04-27 16:55:58 -0700 | [diff] [blame] | 749 | sp<AudioIoDescriptor> desc; |
| 750 | ssize_t index = mIoDescriptors.indexOfKey(ioHandle); |
| 751 | if (index >= 0) { |
| 752 | desc = mIoDescriptors.valueAt(index); |
| 753 | } |
| 754 | return desc; |
| 755 | } |
| 756 | |
| Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 757 | sp<AudioIoDescriptor> AudioSystem::AudioFlingerClient::getIoDescriptor(audio_io_handle_t ioHandle) { |
| Praveen Chavan | 49fdeaf | 2015-09-29 02:25:47 -0700 | [diff] [blame] | 758 | Mutex::Autolock _l(mLock); |
| 759 | return getIoDescriptor_l(ioHandle); |
| 760 | } |
| 761 | |
| Eric Laurent | 296fb13 | 2015-05-01 11:38:42 -0700 | [diff] [blame] | 762 | status_t AudioSystem::AudioFlingerClient::addAudioDeviceCallback( |
| Eric Laurent | 09f1ed2 | 2019-04-24 17:45:17 -0700 | [diff] [blame] | 763 | const wp<AudioDeviceCallback>& callback, audio_io_handle_t audioIo, |
| Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 764 | audio_port_handle_t portId) { |
| Eric Laurent | 09f1ed2 | 2019-04-24 17:45:17 -0700 | [diff] [blame] | 765 | ALOGV("%s audioIo %d portId %d", __func__, audioIo, portId); |
| Eric Laurent | 4463ff5 | 2019-02-07 13:56:09 -0800 | [diff] [blame] | 766 | Mutex::Autolock _l(mLock); |
| Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 767 | auto& callbacks = mAudioDeviceCallbacks.emplace( |
| 768 | audioIo, |
| 769 | std::map<audio_port_handle_t, wp<AudioDeviceCallback>>()).first->second; |
| Eric Laurent | 09f1ed2 | 2019-04-24 17:45:17 -0700 | [diff] [blame] | 770 | auto result = callbacks.try_emplace(portId, callback); |
| 771 | if (!result.second) { |
| 772 | return INVALID_OPERATION; |
| Eric Laurent | 296fb13 | 2015-05-01 11:38:42 -0700 | [diff] [blame] | 773 | } |
| Eric Laurent | 296fb13 | 2015-05-01 11:38:42 -0700 | [diff] [blame] | 774 | return NO_ERROR; |
| 775 | } |
| 776 | |
| 777 | status_t AudioSystem::AudioFlingerClient::removeAudioDeviceCallback( |
| Eric Laurent | 09f1ed2 | 2019-04-24 17:45:17 -0700 | [diff] [blame] | 778 | const wp<AudioDeviceCallback>& callback __unused, audio_io_handle_t audioIo, |
| Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 779 | audio_port_handle_t portId) { |
| Eric Laurent | 09f1ed2 | 2019-04-24 17:45:17 -0700 | [diff] [blame] | 780 | ALOGV("%s audioIo %d portId %d", __func__, audioIo, portId); |
| Eric Laurent | 4463ff5 | 2019-02-07 13:56:09 -0800 | [diff] [blame] | 781 | Mutex::Autolock _l(mLock); |
| Eric Laurent | 09f1ed2 | 2019-04-24 17:45:17 -0700 | [diff] [blame] | 782 | auto it = mAudioDeviceCallbacks.find(audioIo); |
| 783 | if (it == mAudioDeviceCallbacks.end()) { |
| Eric Laurent | 296fb13 | 2015-05-01 11:38:42 -0700 | [diff] [blame] | 784 | return INVALID_OPERATION; |
| 785 | } |
| Eric Laurent | 09f1ed2 | 2019-04-24 17:45:17 -0700 | [diff] [blame] | 786 | if (it->second.erase(portId) == 0) { |
| Eric Laurent | 296fb13 | 2015-05-01 11:38:42 -0700 | [diff] [blame] | 787 | return INVALID_OPERATION; |
| 788 | } |
| Eric Laurent | 09f1ed2 | 2019-04-24 17:45:17 -0700 | [diff] [blame] | 789 | if (it->second.size() == 0) { |
| 790 | mAudioDeviceCallbacks.erase(audioIo); |
| Eric Laurent | 296fb13 | 2015-05-01 11:38:42 -0700 | [diff] [blame] | 791 | } |
| 792 | return NO_ERROR; |
| 793 | } |
| 794 | |
| Eric Laurent | 0825031 | 2022-05-03 18:12:28 +0200 | [diff] [blame] | 795 | status_t AudioSystem::AudioFlingerClient::addSupportedLatencyModesCallback( |
| 796 | const sp<SupportedLatencyModesCallback>& callback) { |
| 797 | Mutex::Autolock _l(mLock); |
| 798 | if (std::find(mSupportedLatencyModesCallbacks.begin(), |
| 799 | mSupportedLatencyModesCallbacks.end(), |
| 800 | callback) != mSupportedLatencyModesCallbacks.end()) { |
| 801 | return INVALID_OPERATION; |
| 802 | } |
| 803 | mSupportedLatencyModesCallbacks.push_back(callback); |
| 804 | return NO_ERROR; |
| 805 | } |
| 806 | |
| 807 | status_t AudioSystem::AudioFlingerClient::removeSupportedLatencyModesCallback( |
| 808 | const sp<SupportedLatencyModesCallback>& callback) { |
| 809 | Mutex::Autolock _l(mLock); |
| 810 | auto it = std::find(mSupportedLatencyModesCallbacks.begin(), |
| 811 | mSupportedLatencyModesCallbacks.end(), |
| 812 | callback); |
| 813 | if (it == mSupportedLatencyModesCallbacks.end()) { |
| 814 | return INVALID_OPERATION; |
| 815 | } |
| 816 | mSupportedLatencyModesCallbacks.erase(it); |
| 817 | return NO_ERROR; |
| 818 | } |
| 819 | |
| Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 820 | /* static */ uintptr_t AudioSystem::addErrorCallback(audio_error_callback cb) { |
| Ytai Ben-Tsvi | 000c3e4 | 2020-01-09 15:26:40 -0800 | [diff] [blame] | 821 | Mutex::Autolock _l(gLockErrorCallbacks); |
| 822 | gAudioErrorCallbacks.insert(cb); |
| 823 | return reinterpret_cast<uintptr_t>(cb); |
| 824 | } |
| 825 | |
| 826 | /* static */ void AudioSystem::removeErrorCallback(uintptr_t cb) { |
| 827 | Mutex::Autolock _l(gLockErrorCallbacks); |
| 828 | gAudioErrorCallbacks.erase(reinterpret_cast<audio_error_callback>(cb)); |
| 829 | } |
| 830 | |
| 831 | /* static */ void AudioSystem::reportError(status_t err) { |
| 832 | Mutex::Autolock _l(gLockErrorCallbacks); |
| 833 | for (auto callback : gAudioErrorCallbacks) { |
| Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 834 | callback(err); |
| Ytai Ben-Tsvi | 000c3e4 | 2020-01-09 15:26:40 -0800 | [diff] [blame] | 835 | } |
| The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 836 | } |
| 837 | |
| Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 838 | /*static*/ void AudioSystem::setDynPolicyCallback(dynamic_policy_callback cb) { |
| Jean-Michel Trivi | f613d42 | 2015-04-23 18:41:29 -0700 | [diff] [blame] | 839 | Mutex::Autolock _l(gLock); |
| 840 | gDynPolicyCallback = cb; |
| 841 | } |
| 842 | |
| Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 843 | /*static*/ void AudioSystem::setRecordConfigCallback(record_config_callback cb) { |
| Jean-Michel Trivi | 2f4fe9f | 2015-12-04 16:20:59 -0800 | [diff] [blame] | 844 | Mutex::Autolock _l(gLock); |
| 845 | gRecordConfigCallback = cb; |
| 846 | } |
| 847 | |
| Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 848 | /*static*/ void AudioSystem::setRoutingCallback(routing_callback cb) { |
| Jean-Michel Trivi | 9a6b9ad | 2020-10-22 16:46:43 -0700 | [diff] [blame] | 849 | Mutex::Autolock _l(gLock); |
| 850 | gRoutingCallback = cb; |
| 851 | } |
| 852 | |
| Jean-Michel Trivi | 78f2b30 | 2022-04-15 18:18:41 +0000 | [diff] [blame] | 853 | /*static*/ void AudioSystem::setVolInitReqCallback(vol_range_init_req_callback cb) { |
| 854 | Mutex::Autolock _l(gLock); |
| 855 | gVolRangeInitReqCallback = cb; |
| 856 | } |
| 857 | |
| Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 858 | // client singleton for AudioPolicyService binder interface |
| Glenn Kasten | d2d089f | 2014-11-05 11:48:12 -0800 | [diff] [blame] | 859 | // protected by gLockAPS |
| Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 860 | sp<IAudioPolicyService> AudioSystem::gAudioPolicyService; |
| 861 | sp<AudioSystem::AudioPolicyServiceClient> AudioSystem::gAudioPolicyServiceClient; |
| 862 | |
| 863 | |
| Glenn Kasten | 18a6d90 | 2012-09-24 11:27:56 -0700 | [diff] [blame] | 864 | // establish binder interface to AudioPolicy service |
| Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 865 | const sp<IAudioPolicyService> AudioSystem::get_audio_policy_service() { |
| Eric Laurent | 0ebd5f9 | 2014-11-19 19:04:52 -0800 | [diff] [blame] | 866 | sp<IAudioPolicyService> ap; |
| 867 | sp<AudioPolicyServiceClient> apc; |
| 868 | { |
| 869 | Mutex::Autolock _l(gLockAPS); |
| 870 | if (gAudioPolicyService == 0) { |
| 871 | sp<IServiceManager> sm = defaultServiceManager(); |
| 872 | sp<IBinder> binder; |
| 873 | do { |
| 874 | binder = sm->getService(String16("media.audio_policy")); |
| 875 | if (binder != 0) |
| 876 | break; |
| 877 | ALOGW("AudioPolicyService not published, waiting..."); |
| 878 | usleep(500000); // 0.5 s |
| 879 | } while (true); |
| 880 | if (gAudioPolicyServiceClient == NULL) { |
| 881 | gAudioPolicyServiceClient = new AudioPolicyServiceClient(); |
| 882 | } |
| 883 | binder->linkToDeath(gAudioPolicyServiceClient); |
| 884 | gAudioPolicyService = interface_cast<IAudioPolicyService>(binder); |
| 885 | LOG_ALWAYS_FATAL_IF(gAudioPolicyService == 0); |
| 886 | apc = gAudioPolicyServiceClient; |
| Eric Laurent | fb00fc7 | 2017-05-25 18:17:12 -0700 | [diff] [blame] | 887 | // Make sure callbacks can be received by gAudioPolicyServiceClient |
| 888 | ProcessState::self()->startThreadPool(); |
| Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 889 | } |
| Eric Laurent | 0ebd5f9 | 2014-11-19 19:04:52 -0800 | [diff] [blame] | 890 | ap = gAudioPolicyService; |
| 891 | } |
| 892 | if (apc != 0) { |
| François Gaffie | 2443760 | 2018-04-23 15:08:59 +0200 | [diff] [blame] | 893 | int64_t token = IPCThreadState::self()->clearCallingIdentity(); |
| Eric Laurent | 0ebd5f9 | 2014-11-19 19:04:52 -0800 | [diff] [blame] | 894 | ap->registerClient(apc); |
| François Gaffie | 2443760 | 2018-04-23 15:08:59 +0200 | [diff] [blame] | 895 | ap->setAudioPortCallbacksEnabled(apc->isAudioPortCbEnabled()); |
| François Gaffie | cfe1732 | 2018-11-07 13:41:29 +0100 | [diff] [blame] | 896 | ap->setAudioVolumeGroupCallbacksEnabled(apc->isAudioVolumeGroupCbEnabled()); |
| François Gaffie | 2443760 | 2018-04-23 15:08:59 +0200 | [diff] [blame] | 897 | IPCThreadState::self()->restoreCallingIdentity(token); |
| Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 898 | } |
| Glenn Kasten | d2d089f | 2014-11-05 11:48:12 -0800 | [diff] [blame] | 899 | |
| Eric Laurent | 0ebd5f9 | 2014-11-19 19:04:52 -0800 | [diff] [blame] | 900 | return ap; |
| Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 901 | } |
| 902 | |
| Mikhail Naganov | ec3d579 | 2022-05-06 00:19:55 +0000 | [diff] [blame] | 903 | void AudioSystem::clearAudioPolicyService() { |
| 904 | Mutex::Autolock _l(gLockAPS); |
| 905 | gAudioPolicyService.clear(); |
| 906 | } |
| 907 | |
| Glenn Kasten | fb1fdc9 | 2013-07-10 17:03:19 -0700 | [diff] [blame] | 908 | // --------------------------------------------------------------------------- |
| 909 | |
| Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 910 | void AudioSystem::onNewAudioModulesAvailable() { |
| Mikhail Naganov | 88b30d2 | 2020-03-09 19:43:13 +0000 | [diff] [blame] | 911 | const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service(); |
| 912 | if (aps == 0) return; |
| 913 | aps->onNewAudioModulesAvailable(); |
| 914 | } |
| 915 | |
| Nathalie Le Clair | 88fa275 | 2021-11-23 13:03:41 +0100 | [diff] [blame] | 916 | status_t AudioSystem::setDeviceConnectionState(audio_policy_dev_state_t state, |
| 917 | const android::media::audio::common::AudioPort& port, |
| Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 918 | audio_format_t encodedFormat) { |
| Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 919 | const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service(); |
| Eric Laurent | 71b63e3 | 2011-09-02 14:20:56 -0700 | [diff] [blame] | 920 | |
| Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 921 | if (aps == 0) return PERMISSION_DENIED; |
| 922 | |
| Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 923 | return statusTFromBinderStatus( |
| 924 | aps->setDeviceConnectionState( |
| Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 925 | VALUE_OR_RETURN_STATUS( |
| 926 | legacy2aidl_audio_policy_dev_state_t_AudioPolicyDeviceState(state)), |
| Nathalie Le Clair | 88fa275 | 2021-11-23 13:03:41 +0100 | [diff] [blame] | 927 | port, |
| Mikhail Naganov | b60bd1b | 2021-07-15 17:31:43 -0700 | [diff] [blame] | 928 | VALUE_OR_RETURN_STATUS( |
| 929 | legacy2aidl_audio_format_t_AudioFormatDescription(encodedFormat)))); |
| Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 930 | } |
| 931 | |
| Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 932 | audio_policy_dev_state_t AudioSystem::getDeviceConnectionState(audio_devices_t device, |
| Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 933 | const char* device_address) { |
| Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 934 | const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service(); |
| Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 935 | if (aps == 0) return AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE; |
| Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 936 | |
| Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 937 | auto result = [&]() -> ConversionResult<audio_policy_dev_state_t> { |
| Mikhail Naganov | 932cb96 | 2021-09-16 01:05:27 +0000 | [diff] [blame] | 938 | AudioDevice deviceAidl = VALUE_OR_RETURN( |
| 939 | legacy2aidl_audio_device_AudioDevice(device, device_address)); |
| Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 940 | |
| 941 | media::AudioPolicyDeviceState result; |
| 942 | RETURN_IF_ERROR(statusTFromBinderStatus( |
| 943 | aps->getDeviceConnectionState(deviceAidl, &result))); |
| 944 | |
| 945 | return aidl2legacy_AudioPolicyDeviceState_audio_policy_dev_state_t(result); |
| 946 | }(); |
| 947 | return result.value_or(AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE); |
| Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 948 | } |
| 949 | |
| Pavlin Radoslavov | f862bc6 | 2016-12-26 18:57:22 -0800 | [diff] [blame] | 950 | status_t AudioSystem::handleDeviceConfigChange(audio_devices_t device, |
| Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 951 | const char* device_address, |
| 952 | const char* device_name, |
| 953 | audio_format_t encodedFormat) { |
| Pavlin Radoslavov | f862bc6 | 2016-12-26 18:57:22 -0800 | [diff] [blame] | 954 | const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service(); |
| Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 955 | const char* address = ""; |
| 956 | const char* name = ""; |
| Pavlin Radoslavov | f862bc6 | 2016-12-26 18:57:22 -0800 | [diff] [blame] | 957 | |
| 958 | if (aps == 0) return PERMISSION_DENIED; |
| 959 | |
| 960 | if (device_address != NULL) { |
| 961 | address = device_address; |
| 962 | } |
| 963 | if (device_name != NULL) { |
| 964 | name = device_name; |
| 965 | } |
| Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 966 | |
| Mikhail Naganov | 932cb96 | 2021-09-16 01:05:27 +0000 | [diff] [blame] | 967 | AudioDevice deviceAidl = VALUE_OR_RETURN_STATUS( |
| 968 | legacy2aidl_audio_device_AudioDevice(device, address)); |
| Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 969 | |
| 970 | return statusTFromBinderStatus( |
| 971 | aps->handleDeviceConfigChange(deviceAidl, name, VALUE_OR_RETURN_STATUS( |
| Mikhail Naganov | b60bd1b | 2021-07-15 17:31:43 -0700 | [diff] [blame] | 972 | legacy2aidl_audio_format_t_AudioFormatDescription(encodedFormat)))); |
| Pavlin Radoslavov | f862bc6 | 2016-12-26 18:57:22 -0800 | [diff] [blame] | 973 | } |
| 974 | |
| Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 975 | status_t AudioSystem::setPhoneState(audio_mode_t state, uid_t uid) { |
| Glenn Kasten | 347966c | 2012-01-18 14:58:32 -0800 | [diff] [blame] | 976 | if (uint32_t(state) >= AUDIO_MODE_CNT) return BAD_VALUE; |
| Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 977 | const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service(); |
| 978 | if (aps == 0) return PERMISSION_DENIED; |
| 979 | |
| Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 980 | return statusTFromBinderStatus(aps->setPhoneState( |
| 981 | VALUE_OR_RETURN_STATUS(legacy2aidl_audio_mode_t_AudioMode(state)), |
| 982 | VALUE_OR_RETURN_STATUS(legacy2aidl_uid_t_int32_t(uid)))); |
| Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 983 | } |
| 984 | |
| Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 985 | status_t |
| 986 | 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] | 987 | const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service(); |
| 988 | if (aps == 0) return PERMISSION_DENIED; |
| Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 989 | |
| 990 | return statusTFromBinderStatus( |
| 991 | aps->setForceUse( |
| 992 | VALUE_OR_RETURN_STATUS( |
| 993 | legacy2aidl_audio_policy_force_use_t_AudioPolicyForceUse(usage)), |
| 994 | VALUE_OR_RETURN_STATUS( |
| 995 | legacy2aidl_audio_policy_forced_cfg_t_AudioPolicyForcedConfig( |
| 996 | config)))); |
| Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 997 | } |
| 998 | |
| Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 999 | 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] | 1000 | const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service(); |
| Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 1001 | if (aps == 0) return AUDIO_POLICY_FORCE_NONE; |
| Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 1002 | |
| 1003 | auto result = [&]() -> ConversionResult<audio_policy_forced_cfg_t> { |
| 1004 | media::AudioPolicyForceUse usageAidl = VALUE_OR_RETURN( |
| 1005 | legacy2aidl_audio_policy_force_use_t_AudioPolicyForceUse(usage)); |
| 1006 | media::AudioPolicyForcedConfig configAidl; |
| 1007 | RETURN_IF_ERROR(statusTFromBinderStatus( |
| 1008 | aps->getForceUse(usageAidl, &configAidl))); |
| 1009 | return aidl2legacy_AudioPolicyForcedConfig_audio_policy_forced_cfg_t(configAidl); |
| 1010 | }(); |
| 1011 | |
| 1012 | return result.value_or(AUDIO_POLICY_FORCE_NONE); |
| Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 1013 | } |
| 1014 | |
| 1015 | |
| Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 1016 | audio_io_handle_t AudioSystem::getOutput(audio_stream_type_t stream) { |
| Eric Laurent | 1a9ed11 | 2012-03-20 18:36:01 -0700 | [diff] [blame] | 1017 | const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service(); |
| Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 1018 | if (aps == 0) return AUDIO_IO_HANDLE_NONE; |
| 1019 | |
| 1020 | auto result = [&]() -> ConversionResult<audio_io_handle_t> { |
| Mikhail Naganov | dbf0364 | 2021-08-25 18:15:32 -0700 | [diff] [blame] | 1021 | AudioStreamType streamAidl = VALUE_OR_RETURN( |
| Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 1022 | legacy2aidl_audio_stream_type_t_AudioStreamType(stream)); |
| 1023 | int32_t outputAidl; |
| 1024 | RETURN_IF_ERROR( |
| 1025 | statusTFromBinderStatus(aps->getOutput(streamAidl, &outputAidl))); |
| 1026 | return aidl2legacy_int32_t_audio_io_handle_t(outputAidl); |
| 1027 | }(); |
| 1028 | |
| 1029 | return result.value_or(AUDIO_IO_HANDLE_NONE); |
| Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 1030 | } |
| 1031 | |
| Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 1032 | status_t AudioSystem::getOutputForAttr(audio_attributes_t* attr, |
| 1033 | audio_io_handle_t* output, |
| 1034 | audio_session_t session, |
| 1035 | audio_stream_type_t* stream, |
| Svet Ganov | 3e5f14f | 2021-05-13 22:51:08 +0000 | [diff] [blame] | 1036 | const AttributionSourceState& attributionSource, |
| Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 1037 | const audio_config_t* config, |
| 1038 | audio_output_flags_t flags, |
| 1039 | audio_port_handle_t* selectedDeviceId, |
| 1040 | audio_port_handle_t* portId, |
| Eric Laurent | b0a7bc9 | 2022-04-05 15:06:08 +0200 | [diff] [blame] | 1041 | std::vector<audio_io_handle_t>* secondaryOutputs, |
| 1042 | bool *isSpatialized) { |
| Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 1043 | if (attr == nullptr) { |
| 1044 | ALOGE("%s NULL audio attributes", __func__); |
| 1045 | return BAD_VALUE; |
| 1046 | } |
| 1047 | if (output == nullptr) { |
| 1048 | ALOGE("%s NULL output - shouldn't happen", __func__); |
| 1049 | return BAD_VALUE; |
| 1050 | } |
| 1051 | if (selectedDeviceId == nullptr) { |
| 1052 | ALOGE("%s NULL selectedDeviceId - shouldn't happen", __func__); |
| 1053 | return BAD_VALUE; |
| 1054 | } |
| 1055 | if (portId == nullptr) { |
| 1056 | ALOGE("%s NULL portId - shouldn't happen", __func__); |
| 1057 | return BAD_VALUE; |
| 1058 | } |
| 1059 | if (secondaryOutputs == nullptr) { |
| 1060 | ALOGE("%s NULL secondaryOutputs - shouldn't happen", __func__); |
| 1061 | return BAD_VALUE; |
| 1062 | } |
| 1063 | |
| Jean-Michel Trivi | 5bd3f38 | 2014-06-13 16:06:54 -0700 | [diff] [blame] | 1064 | const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service(); |
| Eric Laurent | e83b55d | 2014-11-14 10:06:21 -0800 | [diff] [blame] | 1065 | if (aps == 0) return NO_INIT; |
| Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 1066 | |
| Mikhail Naganov | 2a1cf61 | 2023-05-17 11:48:43 -0700 | [diff] [blame] | 1067 | media::audio::common::AudioAttributes attrAidl = VALUE_OR_RETURN_STATUS( |
| 1068 | legacy2aidl_audio_attributes_t_AudioAttributes(*attr)); |
| Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 1069 | int32_t sessionAidl = VALUE_OR_RETURN_STATUS(legacy2aidl_audio_session_t_int32_t(session)); |
| Mikhail Naganov | dbf0364 | 2021-08-25 18:15:32 -0700 | [diff] [blame] | 1070 | AudioConfig configAidl = VALUE_OR_RETURN_STATUS( |
| Mikhail Naganov | de3fa18 | 2021-07-30 15:06:42 -0700 | [diff] [blame] | 1071 | legacy2aidl_audio_config_t_AudioConfig(*config, false /*isInput*/)); |
| Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 1072 | int32_t flagsAidl = VALUE_OR_RETURN_STATUS( |
| 1073 | legacy2aidl_audio_output_flags_t_int32_t_mask(flags)); |
| Eric Laurent | f99edd3 | 2021-02-01 15:57:33 +0100 | [diff] [blame] | 1074 | int32_t selectedDeviceIdAidl = VALUE_OR_RETURN_STATUS( |
| 1075 | legacy2aidl_audio_port_handle_t_int32_t(*selectedDeviceId)); |
| Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 1076 | |
| 1077 | media::GetOutputForAttrResponse responseAidl; |
| 1078 | |
| 1079 | RETURN_STATUS_IF_ERROR(statusTFromBinderStatus( |
| Svet Ganov | 3e5f14f | 2021-05-13 22:51:08 +0000 | [diff] [blame] | 1080 | aps->getOutputForAttr(attrAidl, sessionAidl, attributionSource, configAidl, flagsAidl, |
| Eric Laurent | f99edd3 | 2021-02-01 15:57:33 +0100 | [diff] [blame] | 1081 | selectedDeviceIdAidl, &responseAidl))); |
| Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 1082 | |
| 1083 | *output = VALUE_OR_RETURN_STATUS( |
| 1084 | aidl2legacy_int32_t_audio_io_handle_t(responseAidl.output)); |
| 1085 | |
| 1086 | if (stream != nullptr) { |
| 1087 | *stream = VALUE_OR_RETURN_STATUS( |
| 1088 | aidl2legacy_AudioStreamType_audio_stream_type_t(responseAidl.stream)); |
| 1089 | } |
| 1090 | *selectedDeviceId = VALUE_OR_RETURN_STATUS( |
| 1091 | aidl2legacy_int32_t_audio_port_handle_t(responseAidl.selectedDeviceId)); |
| 1092 | *portId = VALUE_OR_RETURN_STATUS(aidl2legacy_int32_t_audio_port_handle_t(responseAidl.portId)); |
| 1093 | *secondaryOutputs = VALUE_OR_RETURN_STATUS(convertContainer<std::vector<audio_io_handle_t>>( |
| 1094 | responseAidl.secondaryOutputs, aidl2legacy_int32_t_audio_io_handle_t)); |
| Eric Laurent | b0a7bc9 | 2022-04-05 15:06:08 +0200 | [diff] [blame] | 1095 | *isSpatialized = responseAidl.isSpatialized; |
| Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 1096 | |
| 1097 | return OK; |
| Jean-Michel Trivi | 5bd3f38 | 2014-06-13 16:06:54 -0700 | [diff] [blame] | 1098 | } |
| 1099 | |
| Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 1100 | status_t AudioSystem::startOutput(audio_port_handle_t portId) { |
| Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 1101 | const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service(); |
| 1102 | if (aps == 0) return PERMISSION_DENIED; |
| Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 1103 | |
| 1104 | int32_t portIdAidl = VALUE_OR_RETURN_STATUS(legacy2aidl_audio_port_handle_t_int32_t(portId)); |
| 1105 | return statusTFromBinderStatus(aps->startOutput(portIdAidl)); |
| Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 1106 | } |
| 1107 | |
| Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 1108 | status_t AudioSystem::stopOutput(audio_port_handle_t portId) { |
| Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 1109 | const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service(); |
| 1110 | if (aps == 0) return PERMISSION_DENIED; |
| Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 1111 | |
| 1112 | int32_t portIdAidl = VALUE_OR_RETURN_STATUS(legacy2aidl_audio_port_handle_t_int32_t(portId)); |
| 1113 | return statusTFromBinderStatus(aps->stopOutput(portIdAidl)); |
| Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 1114 | } |
| 1115 | |
| Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 1116 | void AudioSystem::releaseOutput(audio_port_handle_t portId) { |
| Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 1117 | const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service(); |
| 1118 | if (aps == 0) return; |
| Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 1119 | |
| 1120 | auto status = [&]() -> status_t { |
| 1121 | int32_t portIdAidl = VALUE_OR_RETURN_STATUS( |
| 1122 | legacy2aidl_audio_port_handle_t_int32_t(portId)); |
| 1123 | RETURN_STATUS_IF_ERROR(statusTFromBinderStatus(aps->releaseOutput(portIdAidl))); |
| 1124 | return OK; |
| 1125 | }(); |
| 1126 | |
| 1127 | // Ignore status. |
| 1128 | (void) status; |
| Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 1129 | } |
| 1130 | |
| Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 1131 | status_t AudioSystem::getInputForAttr(const audio_attributes_t* attr, |
| 1132 | audio_io_handle_t* input, |
| 1133 | audio_unique_id_t riid, |
| 1134 | audio_session_t session, |
| Svet Ganov | 3e5f14f | 2021-05-13 22:51:08 +0000 | [diff] [blame] | 1135 | const AttributionSourceState &attributionSource, |
| Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 1136 | const audio_config_base_t* config, |
| 1137 | audio_input_flags_t flags, |
| 1138 | audio_port_handle_t* selectedDeviceId, |
| 1139 | audio_port_handle_t* portId) { |
| 1140 | if (attr == NULL) { |
| 1141 | ALOGE("getInputForAttr NULL attr - shouldn't happen"); |
| 1142 | return BAD_VALUE; |
| 1143 | } |
| 1144 | if (input == NULL) { |
| 1145 | ALOGE("getInputForAttr NULL input - shouldn't happen"); |
| 1146 | return BAD_VALUE; |
| 1147 | } |
| 1148 | if (selectedDeviceId == NULL) { |
| 1149 | ALOGE("getInputForAttr NULL selectedDeviceId - shouldn't happen"); |
| 1150 | return BAD_VALUE; |
| 1151 | } |
| 1152 | if (portId == NULL) { |
| 1153 | ALOGE("getInputForAttr NULL portId - shouldn't happen"); |
| 1154 | return BAD_VALUE; |
| 1155 | } |
| 1156 | |
| Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 1157 | const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service(); |
| Eric Laurent | caf7f48 | 2014-11-25 17:50:47 -0800 | [diff] [blame] | 1158 | if (aps == 0) return NO_INIT; |
| Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 1159 | |
| Mikhail Naganov | 2a1cf61 | 2023-05-17 11:48:43 -0700 | [diff] [blame] | 1160 | media::audio::common::AudioAttributes attrAidl = VALUE_OR_RETURN_STATUS( |
| 1161 | legacy2aidl_audio_attributes_t_AudioAttributes(*attr)); |
| Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 1162 | int32_t inputAidl = VALUE_OR_RETURN_STATUS(legacy2aidl_audio_io_handle_t_int32_t(*input)); |
| 1163 | int32_t riidAidl = VALUE_OR_RETURN_STATUS(legacy2aidl_audio_unique_id_t_int32_t(riid)); |
| 1164 | int32_t sessionAidl = VALUE_OR_RETURN_STATUS(legacy2aidl_audio_session_t_int32_t(session)); |
| Mikhail Naganov | dbf0364 | 2021-08-25 18:15:32 -0700 | [diff] [blame] | 1165 | AudioConfigBase configAidl = VALUE_OR_RETURN_STATUS( |
| Mikhail Naganov | de3fa18 | 2021-07-30 15:06:42 -0700 | [diff] [blame] | 1166 | legacy2aidl_audio_config_base_t_AudioConfigBase(*config, true /*isInput*/)); |
| Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 1167 | int32_t flagsAidl = VALUE_OR_RETURN_STATUS(legacy2aidl_audio_input_flags_t_int32_t_mask(flags)); |
| Eric Laurent | f99edd3 | 2021-02-01 15:57:33 +0100 | [diff] [blame] | 1168 | int32_t selectedDeviceIdAidl = VALUE_OR_RETURN_STATUS( |
| 1169 | legacy2aidl_audio_port_handle_t_int32_t(*selectedDeviceId)); |
| Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 1170 | |
| 1171 | media::GetInputForAttrResponse response; |
| 1172 | |
| 1173 | RETURN_STATUS_IF_ERROR(statusTFromBinderStatus( |
| Svet Ganov | 3e5f14f | 2021-05-13 22:51:08 +0000 | [diff] [blame] | 1174 | aps->getInputForAttr(attrAidl, inputAidl, riidAidl, sessionAidl, attributionSource, |
| Philip P. Moltmann | bda4575 | 2020-07-17 16:41:18 -0700 | [diff] [blame] | 1175 | configAidl, flagsAidl, selectedDeviceIdAidl, &response))); |
| Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 1176 | |
| 1177 | *input = VALUE_OR_RETURN_STATUS(aidl2legacy_int32_t_audio_io_handle_t(response.input)); |
| 1178 | *selectedDeviceId = VALUE_OR_RETURN_STATUS( |
| 1179 | aidl2legacy_int32_t_audio_port_handle_t(response.selectedDeviceId)); |
| 1180 | *portId = VALUE_OR_RETURN_STATUS(aidl2legacy_int32_t_audio_port_handle_t(response.portId)); |
| 1181 | |
| 1182 | return OK; |
| Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 1183 | } |
| 1184 | |
| Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 1185 | status_t AudioSystem::startInput(audio_port_handle_t portId) { |
| Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 1186 | const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service(); |
| 1187 | if (aps == 0) return PERMISSION_DENIED; |
| Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 1188 | |
| 1189 | int32_t portIdAidl = VALUE_OR_RETURN_STATUS(legacy2aidl_audio_port_handle_t_int32_t(portId)); |
| 1190 | return statusTFromBinderStatus(aps->startInput(portIdAidl)); |
| Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 1191 | } |
| 1192 | |
| Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 1193 | status_t AudioSystem::stopInput(audio_port_handle_t portId) { |
| Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 1194 | const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service(); |
| 1195 | if (aps == 0) return PERMISSION_DENIED; |
| Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 1196 | |
| 1197 | int32_t portIdAidl = VALUE_OR_RETURN_STATUS(legacy2aidl_audio_port_handle_t_int32_t(portId)); |
| 1198 | return statusTFromBinderStatus(aps->stopInput(portIdAidl)); |
| Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 1199 | } |
| 1200 | |
| Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 1201 | void AudioSystem::releaseInput(audio_port_handle_t portId) { |
| Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 1202 | const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service(); |
| 1203 | if (aps == 0) return; |
| Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 1204 | |
| 1205 | auto status = [&]() -> status_t { |
| 1206 | int32_t portIdAidl = VALUE_OR_RETURN_STATUS( |
| 1207 | legacy2aidl_audio_port_handle_t_int32_t(portId)); |
| 1208 | RETURN_STATUS_IF_ERROR(statusTFromBinderStatus(aps->releaseInput(portIdAidl))); |
| 1209 | return OK; |
| 1210 | }(); |
| 1211 | |
| 1212 | // Ignore status. |
| 1213 | (void) status; |
| Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 1214 | } |
| 1215 | |
| Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 1216 | status_t AudioSystem::initStreamVolume(audio_stream_type_t stream, |
| Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 1217 | int indexMin, |
| 1218 | int indexMax) { |
| Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 1219 | const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service(); |
| 1220 | if (aps == 0) return PERMISSION_DENIED; |
| Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 1221 | |
| Mikhail Naganov | dbf0364 | 2021-08-25 18:15:32 -0700 | [diff] [blame] | 1222 | AudioStreamType streamAidl = VALUE_OR_RETURN_STATUS( |
| Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 1223 | legacy2aidl_audio_stream_type_t_AudioStreamType(stream)); |
| 1224 | int32_t indexMinAidl = VALUE_OR_RETURN_STATUS(convertIntegral<int32_t>(indexMin)); |
| 1225 | int32_t indexMaxAidl = VALUE_OR_RETURN_STATUS(convertIntegral<int32_t>(indexMax)); |
| Mikhail Naganov | ec3d579 | 2022-05-06 00:19:55 +0000 | [diff] [blame] | 1226 | status_t status = statusTFromBinderStatus( |
| Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 1227 | aps->initStreamVolume(streamAidl, indexMinAidl, indexMaxAidl)); |
| Mikhail Naganov | ec3d579 | 2022-05-06 00:19:55 +0000 | [diff] [blame] | 1228 | if (status == DEAD_OBJECT) { |
| 1229 | // This is a critical operation since w/o proper stream volumes no audio |
| 1230 | // will be heard. Make sure we recover from a failure in any case. |
| 1231 | ALOGE("Received DEAD_OBJECT from APS, clearing the client"); |
| 1232 | clearAudioPolicyService(); |
| 1233 | } |
| 1234 | return status; |
| Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 1235 | } |
| 1236 | |
| Eric Laurent | 83844cc | 2011-11-18 16:43:31 -0800 | [diff] [blame] | 1237 | status_t AudioSystem::setStreamVolumeIndex(audio_stream_type_t stream, |
| 1238 | int index, |
| Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 1239 | audio_devices_t device) { |
| Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 1240 | const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service(); |
| 1241 | if (aps == 0) return PERMISSION_DENIED; |
| Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 1242 | |
| Mikhail Naganov | dbf0364 | 2021-08-25 18:15:32 -0700 | [diff] [blame] | 1243 | AudioStreamType streamAidl = VALUE_OR_RETURN_STATUS( |
| Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 1244 | legacy2aidl_audio_stream_type_t_AudioStreamType(stream)); |
| 1245 | int32_t indexAidl = VALUE_OR_RETURN_STATUS(convertIntegral<int32_t>(index)); |
| Mikhail Naganov | f4a7536 | 2021-09-16 00:02:54 +0000 | [diff] [blame] | 1246 | AudioDeviceDescription deviceAidl = VALUE_OR_RETURN_STATUS( |
| Mikhail Naganov | 21a32ec | 2021-07-08 14:40:12 -0700 | [diff] [blame] | 1247 | legacy2aidl_audio_devices_t_AudioDeviceDescription(device)); |
| Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 1248 | return statusTFromBinderStatus( |
| 1249 | aps->setStreamVolumeIndex(streamAidl, deviceAidl, indexAidl)); |
| Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 1250 | } |
| 1251 | |
| Eric Laurent | 83844cc | 2011-11-18 16:43:31 -0800 | [diff] [blame] | 1252 | status_t AudioSystem::getStreamVolumeIndex(audio_stream_type_t stream, |
| Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 1253 | int* index, |
| 1254 | audio_devices_t device) { |
| Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 1255 | const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service(); |
| 1256 | if (aps == 0) return PERMISSION_DENIED; |
| Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 1257 | |
| Mikhail Naganov | dbf0364 | 2021-08-25 18:15:32 -0700 | [diff] [blame] | 1258 | AudioStreamType streamAidl = VALUE_OR_RETURN_STATUS( |
| Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 1259 | legacy2aidl_audio_stream_type_t_AudioStreamType(stream)); |
| Mikhail Naganov | f4a7536 | 2021-09-16 00:02:54 +0000 | [diff] [blame] | 1260 | AudioDeviceDescription deviceAidl = VALUE_OR_RETURN_STATUS( |
| Mikhail Naganov | 21a32ec | 2021-07-08 14:40:12 -0700 | [diff] [blame] | 1261 | legacy2aidl_audio_devices_t_AudioDeviceDescription(device)); |
| Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 1262 | int32_t indexAidl; |
| 1263 | RETURN_STATUS_IF_ERROR(statusTFromBinderStatus( |
| 1264 | aps->getStreamVolumeIndex(streamAidl, deviceAidl, &indexAidl))); |
| 1265 | if (index != nullptr) { |
| 1266 | *index = VALUE_OR_RETURN_STATUS(convertIntegral<int>(indexAidl)); |
| 1267 | } |
| 1268 | return OK; |
| Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 1269 | } |
| 1270 | |
| Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 1271 | status_t AudioSystem::setVolumeIndexForAttributes(const audio_attributes_t& attr, |
| François Gaffie | cfe1732 | 2018-11-07 13:41:29 +0100 | [diff] [blame] | 1272 | int index, |
| Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 1273 | audio_devices_t device) { |
| François Gaffie | cfe1732 | 2018-11-07 13:41:29 +0100 | [diff] [blame] | 1274 | const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service(); |
| 1275 | if (aps == 0) return PERMISSION_DENIED; |
| Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 1276 | |
| Mikhail Naganov | 2a1cf61 | 2023-05-17 11:48:43 -0700 | [diff] [blame] | 1277 | media::audio::common::AudioAttributes attrAidl = VALUE_OR_RETURN_STATUS( |
| 1278 | legacy2aidl_audio_attributes_t_AudioAttributes(attr)); |
| Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 1279 | int32_t indexAidl = VALUE_OR_RETURN_STATUS(convertIntegral<int32_t>(index)); |
| Mikhail Naganov | f4a7536 | 2021-09-16 00:02:54 +0000 | [diff] [blame] | 1280 | AudioDeviceDescription deviceAidl = VALUE_OR_RETURN_STATUS( |
| Mikhail Naganov | 21a32ec | 2021-07-08 14:40:12 -0700 | [diff] [blame] | 1281 | legacy2aidl_audio_devices_t_AudioDeviceDescription(device)); |
| Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 1282 | return statusTFromBinderStatus( |
| 1283 | aps->setVolumeIndexForAttributes(attrAidl, deviceAidl, indexAidl)); |
| François Gaffie | cfe1732 | 2018-11-07 13:41:29 +0100 | [diff] [blame] | 1284 | } |
| 1285 | |
| Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 1286 | status_t AudioSystem::getVolumeIndexForAttributes(const audio_attributes_t& attr, |
| 1287 | int& index, |
| 1288 | audio_devices_t device) { |
| François Gaffie | cfe1732 | 2018-11-07 13:41:29 +0100 | [diff] [blame] | 1289 | const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service(); |
| 1290 | if (aps == 0) return PERMISSION_DENIED; |
| Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 1291 | |
| Mikhail Naganov | 2a1cf61 | 2023-05-17 11:48:43 -0700 | [diff] [blame] | 1292 | media::audio::common::AudioAttributes attrAidl = VALUE_OR_RETURN_STATUS( |
| 1293 | legacy2aidl_audio_attributes_t_AudioAttributes(attr)); |
| Mikhail Naganov | f4a7536 | 2021-09-16 00:02:54 +0000 | [diff] [blame] | 1294 | AudioDeviceDescription deviceAidl = VALUE_OR_RETURN_STATUS( |
| Mikhail Naganov | 21a32ec | 2021-07-08 14:40:12 -0700 | [diff] [blame] | 1295 | legacy2aidl_audio_devices_t_AudioDeviceDescription(device)); |
| Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 1296 | int32_t indexAidl; |
| 1297 | RETURN_STATUS_IF_ERROR(statusTFromBinderStatus( |
| 1298 | aps->getVolumeIndexForAttributes(attrAidl, deviceAidl, &indexAidl))); |
| 1299 | index = VALUE_OR_RETURN_STATUS(convertIntegral<int>(indexAidl)); |
| 1300 | return OK; |
| François Gaffie | cfe1732 | 2018-11-07 13:41:29 +0100 | [diff] [blame] | 1301 | } |
| 1302 | |
| Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 1303 | status_t AudioSystem::getMaxVolumeIndexForAttributes(const audio_attributes_t& attr, int& index) { |
| François Gaffie | cfe1732 | 2018-11-07 13:41:29 +0100 | [diff] [blame] | 1304 | const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service(); |
| 1305 | if (aps == 0) return PERMISSION_DENIED; |
| Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 1306 | |
| Mikhail Naganov | 2a1cf61 | 2023-05-17 11:48:43 -0700 | [diff] [blame] | 1307 | media::audio::common::AudioAttributes attrAidl = VALUE_OR_RETURN_STATUS( |
| 1308 | legacy2aidl_audio_attributes_t_AudioAttributes(attr)); |
| Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 1309 | int32_t indexAidl; |
| 1310 | RETURN_STATUS_IF_ERROR(statusTFromBinderStatus( |
| 1311 | aps->getMaxVolumeIndexForAttributes(attrAidl, &indexAidl))); |
| 1312 | index = VALUE_OR_RETURN_STATUS(convertIntegral<int>(indexAidl)); |
| 1313 | return OK; |
| François Gaffie | cfe1732 | 2018-11-07 13:41:29 +0100 | [diff] [blame] | 1314 | } |
| 1315 | |
| Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 1316 | status_t AudioSystem::getMinVolumeIndexForAttributes(const audio_attributes_t& attr, int& index) { |
| François Gaffie | cfe1732 | 2018-11-07 13:41:29 +0100 | [diff] [blame] | 1317 | const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service(); |
| 1318 | if (aps == 0) return PERMISSION_DENIED; |
| Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 1319 | |
| Mikhail Naganov | 2a1cf61 | 2023-05-17 11:48:43 -0700 | [diff] [blame] | 1320 | media::audio::common::AudioAttributes attrAidl = VALUE_OR_RETURN_STATUS( |
| 1321 | legacy2aidl_audio_attributes_t_AudioAttributes(attr)); |
| Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 1322 | int32_t indexAidl; |
| 1323 | RETURN_STATUS_IF_ERROR(statusTFromBinderStatus( |
| 1324 | aps->getMinVolumeIndexForAttributes(attrAidl, &indexAidl))); |
| 1325 | index = VALUE_OR_RETURN_STATUS(convertIntegral<int>(indexAidl)); |
| 1326 | return OK; |
| François Gaffie | cfe1732 | 2018-11-07 13:41:29 +0100 | [diff] [blame] | 1327 | } |
| 1328 | |
| Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 1329 | product_strategy_t AudioSystem::getStrategyForStream(audio_stream_type_t stream) { |
| Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 1330 | const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service(); |
| François Gaffie | d0ba9ed | 2018-11-05 11:50:42 +0100 | [diff] [blame] | 1331 | if (aps == 0) return PRODUCT_STRATEGY_NONE; |
| Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 1332 | |
| 1333 | auto result = [&]() -> ConversionResult<product_strategy_t> { |
| Mikhail Naganov | dbf0364 | 2021-08-25 18:15:32 -0700 | [diff] [blame] | 1334 | AudioStreamType streamAidl = VALUE_OR_RETURN( |
| Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 1335 | legacy2aidl_audio_stream_type_t_AudioStreamType(stream)); |
| 1336 | int32_t resultAidl; |
| 1337 | RETURN_IF_ERROR(statusTFromBinderStatus( |
| 1338 | aps->getStrategyForStream(streamAidl, &resultAidl))); |
| 1339 | return aidl2legacy_int32_t_product_strategy_t(resultAidl); |
| 1340 | }(); |
| 1341 | return result.value_or(PRODUCT_STRATEGY_NONE); |
| Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 1342 | } |
| 1343 | |
| François Gaffie | f6d3bbe | 2022-04-01 14:34:29 +0200 | [diff] [blame] | 1344 | status_t AudioSystem::getDevicesForAttributes(const audio_attributes_t& aa, |
| Andy Hung | 6d23c0f | 2022-02-16 09:37:15 -0800 | [diff] [blame] | 1345 | AudioDeviceTypeAddrVector* devices, |
| 1346 | bool forVolume) { |
| Jean-Michel Trivi | f41599b | 2020-01-07 14:22:08 -0800 | [diff] [blame] | 1347 | if (devices == nullptr) { |
| 1348 | return BAD_VALUE; |
| 1349 | } |
| 1350 | const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service(); |
| 1351 | if (aps == 0) return PERMISSION_DENIED; |
| Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 1352 | |
| Mikhail Naganov | 2a1cf61 | 2023-05-17 11:48:43 -0700 | [diff] [blame] | 1353 | media::audio::common::AudioAttributes aaAidl = VALUE_OR_RETURN_STATUS( |
| 1354 | legacy2aidl_audio_attributes_t_AudioAttributes(aa)); |
| Mikhail Naganov | f4a7536 | 2021-09-16 00:02:54 +0000 | [diff] [blame] | 1355 | std::vector<AudioDevice> retAidl; |
| Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 1356 | RETURN_STATUS_IF_ERROR( |
| Andy Hung | 6d23c0f | 2022-02-16 09:37:15 -0800 | [diff] [blame] | 1357 | statusTFromBinderStatus(aps->getDevicesForAttributes(aaAidl, forVolume, &retAidl))); |
| Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 1358 | *devices = VALUE_OR_RETURN_STATUS( |
| 1359 | convertContainer<AudioDeviceTypeAddrVector>( |
| 1360 | retAidl, |
| 1361 | aidl2legacy_AudioDeviceTypeAddress)); |
| 1362 | return OK; |
| Jean-Michel Trivi | f41599b | 2020-01-07 14:22:08 -0800 | [diff] [blame] | 1363 | } |
| 1364 | |
| Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 1365 | audio_io_handle_t AudioSystem::getOutputForEffect(const effect_descriptor_t* desc) { |
| Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 1366 | const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service(); |
| Glenn Kasten | efa6ea9 | 2014-01-08 09:10:43 -0800 | [diff] [blame] | 1367 | // FIXME change return type to status_t, and return PERMISSION_DENIED here |
| Glenn Kasten | 142f519 | 2014-03-25 17:44:59 -0700 | [diff] [blame] | 1368 | if (aps == 0) return AUDIO_IO_HANDLE_NONE; |
| Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 1369 | |
| 1370 | auto result = [&]() -> ConversionResult<audio_io_handle_t> { |
| 1371 | media::EffectDescriptor descAidl = VALUE_OR_RETURN( |
| 1372 | legacy2aidl_effect_descriptor_t_EffectDescriptor(*desc)); |
| 1373 | int32_t retAidl; |
| 1374 | RETURN_IF_ERROR( |
| 1375 | statusTFromBinderStatus(aps->getOutputForEffect(descAidl, &retAidl))); |
| 1376 | return aidl2legacy_int32_t_audio_io_handle_t(retAidl); |
| 1377 | }(); |
| 1378 | |
| 1379 | return result.value_or(AUDIO_IO_HANDLE_NONE); |
| Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 1380 | } |
| 1381 | |
| Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 1382 | status_t AudioSystem::registerEffect(const effect_descriptor_t* desc, |
| 1383 | audio_io_handle_t io, |
| 1384 | product_strategy_t strategy, |
| 1385 | audio_session_t session, |
| 1386 | int id) { |
| Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 1387 | const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service(); |
| 1388 | if (aps == 0) return PERMISSION_DENIED; |
| Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 1389 | |
| 1390 | media::EffectDescriptor descAidl = VALUE_OR_RETURN_STATUS( |
| 1391 | legacy2aidl_effect_descriptor_t_EffectDescriptor(*desc)); |
| 1392 | int32_t ioAidl = VALUE_OR_RETURN_STATUS(legacy2aidl_audio_io_handle_t_int32_t(io)); |
| 1393 | int32_t strategyAidl = VALUE_OR_RETURN_STATUS(aidl2legacy_int32_t_product_strategy_t(strategy)); |
| 1394 | int32_t sessionAidl = VALUE_OR_RETURN_STATUS(legacy2aidl_audio_session_t_int32_t(session)); |
| 1395 | int32_t idAidl = VALUE_OR_RETURN_STATUS(convertReinterpret<int32_t>(id)); |
| 1396 | return statusTFromBinderStatus( |
| 1397 | aps->registerEffect(descAidl, ioAidl, strategyAidl, sessionAidl, idAidl)); |
| Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 1398 | } |
| 1399 | |
| Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 1400 | status_t AudioSystem::unregisterEffect(int id) { |
| Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 1401 | const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service(); |
| 1402 | if (aps == 0) return PERMISSION_DENIED; |
| Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 1403 | |
| 1404 | int32_t idAidl = VALUE_OR_RETURN_STATUS(convertReinterpret<int32_t>(id)); |
| 1405 | return statusTFromBinderStatus( |
| 1406 | aps->unregisterEffect(idAidl)); |
| Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 1407 | } |
| 1408 | |
| Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 1409 | status_t AudioSystem::setEffectEnabled(int id, bool enabled) { |
| Eric Laurent | db7c079 | 2011-08-10 10:37:50 -0700 | [diff] [blame] | 1410 | const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service(); |
| 1411 | if (aps == 0) return PERMISSION_DENIED; |
| Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 1412 | |
| 1413 | int32_t idAidl = VALUE_OR_RETURN_STATUS(convertReinterpret<int32_t>(id)); |
| 1414 | return statusTFromBinderStatus( |
| 1415 | aps->setEffectEnabled(idAidl, enabled)); |
| Eric Laurent | db7c079 | 2011-08-10 10:37:50 -0700 | [diff] [blame] | 1416 | } |
| 1417 | |
| Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 1418 | status_t AudioSystem::moveEffectsToIo(const std::vector<int>& ids, audio_io_handle_t io) { |
| Eric Laurent | 6c79632 | 2019-04-09 14:13:17 -0700 | [diff] [blame] | 1419 | const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service(); |
| 1420 | if (aps == 0) return PERMISSION_DENIED; |
| Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 1421 | |
| 1422 | std::vector<int32_t> idsAidl = VALUE_OR_RETURN_STATUS( |
| 1423 | convertContainer<std::vector<int32_t>>(ids, convertReinterpret<int32_t, int>)); |
| 1424 | int32_t ioAidl = VALUE_OR_RETURN_STATUS(legacy2aidl_audio_io_handle_t_int32_t(io)); |
| 1425 | return statusTFromBinderStatus(aps->moveEffectsToIo(idsAidl, ioAidl)); |
| Eric Laurent | 6c79632 | 2019-04-09 14:13:17 -0700 | [diff] [blame] | 1426 | } |
| 1427 | |
| Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 1428 | status_t AudioSystem::isStreamActive(audio_stream_type_t stream, bool* state, uint32_t inPastMs) { |
| Eric Laurent | eda6c36 | 2011-02-02 09:33:30 -0800 | [diff] [blame] | 1429 | const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service(); |
| 1430 | if (aps == 0) return PERMISSION_DENIED; |
| Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 1431 | if (state == NULL) return BAD_VALUE; |
| Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 1432 | |
| Mikhail Naganov | dbf0364 | 2021-08-25 18:15:32 -0700 | [diff] [blame] | 1433 | AudioStreamType streamAidl = VALUE_OR_RETURN_STATUS( |
| Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 1434 | legacy2aidl_audio_stream_type_t_AudioStreamType(stream)); |
| 1435 | int32_t inPastMsAidl = VALUE_OR_RETURN_STATUS(convertIntegral<int32_t>(inPastMs)); |
| 1436 | RETURN_STATUS_IF_ERROR(statusTFromBinderStatus( |
| 1437 | aps->isStreamActive(streamAidl, inPastMsAidl, state))); |
| 1438 | return OK; |
| Eric Laurent | eda6c36 | 2011-02-02 09:33:30 -0800 | [diff] [blame] | 1439 | } |
| 1440 | |
| Jean-Michel Trivi | 272ab54 | 2013-02-04 16:26:02 -0800 | [diff] [blame] | 1441 | status_t AudioSystem::isStreamActiveRemotely(audio_stream_type_t stream, bool* state, |
| Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 1442 | uint32_t inPastMs) { |
| Jean-Michel Trivi | 272ab54 | 2013-02-04 16:26:02 -0800 | [diff] [blame] | 1443 | const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service(); |
| 1444 | if (aps == 0) return PERMISSION_DENIED; |
| 1445 | if (state == NULL) return BAD_VALUE; |
| Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 1446 | |
| Mikhail Naganov | dbf0364 | 2021-08-25 18:15:32 -0700 | [diff] [blame] | 1447 | AudioStreamType streamAidl = VALUE_OR_RETURN_STATUS( |
| Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 1448 | legacy2aidl_audio_stream_type_t_AudioStreamType(stream)); |
| 1449 | int32_t inPastMsAidl = VALUE_OR_RETURN_STATUS(convertIntegral<int32_t>(inPastMs)); |
| 1450 | RETURN_STATUS_IF_ERROR(statusTFromBinderStatus( |
| 1451 | aps->isStreamActiveRemotely(streamAidl, inPastMsAidl, state))); |
| 1452 | return OK; |
| Jean-Michel Trivi | 272ab54 | 2013-02-04 16:26:02 -0800 | [diff] [blame] | 1453 | } |
| 1454 | |
| Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 1455 | status_t AudioSystem::isSourceActive(audio_source_t stream, bool* state) { |
| Jean-Michel Trivi | d708603 | 2012-10-10 12:11:16 -0700 | [diff] [blame] | 1456 | const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service(); |
| 1457 | if (aps == 0) return PERMISSION_DENIED; |
| 1458 | if (state == NULL) return BAD_VALUE; |
| Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 1459 | |
| Mikhail Naganov | ddceecc | 2021-09-03 13:58:56 -0700 | [diff] [blame] | 1460 | AudioSource streamAidl = VALUE_OR_RETURN_STATUS( |
| 1461 | legacy2aidl_audio_source_t_AudioSource(stream)); |
| Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 1462 | RETURN_STATUS_IF_ERROR(statusTFromBinderStatus( |
| 1463 | aps->isSourceActive(streamAidl, state))); |
| 1464 | return OK; |
| Jean-Michel Trivi | d708603 | 2012-10-10 12:11:16 -0700 | [diff] [blame] | 1465 | } |
| 1466 | |
| Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 1467 | uint32_t AudioSystem::getPrimaryOutputSamplingRate() { |
| Glenn Kasten | cc0f1cf | 2012-09-24 11:27:18 -0700 | [diff] [blame] | 1468 | const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger(); |
| 1469 | if (af == 0) return 0; |
| 1470 | return af->getPrimaryOutputSamplingRate(); |
| 1471 | } |
| 1472 | |
| Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 1473 | size_t AudioSystem::getPrimaryOutputFrameCount() { |
| Glenn Kasten | cc0f1cf | 2012-09-24 11:27:18 -0700 | [diff] [blame] | 1474 | const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger(); |
| 1475 | if (af == 0) return 0; |
| 1476 | return af->getPrimaryOutputFrameCount(); |
| 1477 | } |
| Eric Laurent | eda6c36 | 2011-02-02 09:33:30 -0800 | [diff] [blame] | 1478 | |
| Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 1479 | status_t AudioSystem::setLowRamDevice(bool isLowRamDevice, int64_t totalMemory) { |
| Glenn Kasten | 4182c4e | 2013-07-15 14:45:07 -0700 | [diff] [blame] | 1480 | const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger(); |
| 1481 | if (af == 0) return PERMISSION_DENIED; |
| Andy Hung | 6f248bb | 2018-01-23 14:04:37 -0800 | [diff] [blame] | 1482 | return af->setLowRamDevice(isLowRamDevice, totalMemory); |
| Glenn Kasten | 4182c4e | 2013-07-15 14:45:07 -0700 | [diff] [blame] | 1483 | } |
| 1484 | |
| Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 1485 | void AudioSystem::clearAudioConfigCache() { |
| Glenn Kasten | d2d089f | 2014-11-05 11:48:12 -0800 | [diff] [blame] | 1486 | // called by restoreTrack_l(), which needs new IAudioFlinger and IAudioPolicyService instances |
| Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 1487 | ALOGV("clearAudioConfigCache()"); |
| Eric Laurent | f6778fd | 2014-11-18 17:26:58 -0800 | [diff] [blame] | 1488 | { |
| 1489 | Mutex::Autolock _l(gLock); |
| Eric Laurent | 296fb13 | 2015-05-01 11:38:42 -0700 | [diff] [blame] | 1490 | if (gAudioFlingerClient != 0) { |
| 1491 | gAudioFlingerClient->clearIoCache(); |
| 1492 | } |
| Glenn Kasten | d2d089f | 2014-11-05 11:48:12 -0800 | [diff] [blame] | 1493 | gAudioFlinger.clear(); |
| 1494 | } |
| Mikhail Naganov | ec3d579 | 2022-05-06 00:19:55 +0000 | [diff] [blame] | 1495 | clearAudioPolicyService(); |
| Eric Laurent | 9f6530f | 2011-08-30 10:18:54 -0700 | [diff] [blame] | 1496 | } |
| 1497 | |
| Hayden Gomes | 524159d | 2019-12-23 14:41:47 -0800 | [diff] [blame] | 1498 | status_t AudioSystem::setSupportedSystemUsages(const std::vector<audio_usage_t>& systemUsages) { |
| 1499 | const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service(); |
| 1500 | if (aps == nullptr) return PERMISSION_DENIED; |
| Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 1501 | |
| Mikhail Naganov | dbf0364 | 2021-08-25 18:15:32 -0700 | [diff] [blame] | 1502 | std::vector<AudioUsage> systemUsagesAidl = VALUE_OR_RETURN_STATUS( |
| 1503 | convertContainer<std::vector<AudioUsage>>(systemUsages, |
| 1504 | legacy2aidl_audio_usage_t_AudioUsage)); |
| Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 1505 | return statusTFromBinderStatus(aps->setSupportedSystemUsages(systemUsagesAidl)); |
| Hayden Gomes | 524159d | 2019-12-23 14:41:47 -0800 | [diff] [blame] | 1506 | } |
| 1507 | |
| Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 1508 | status_t AudioSystem::setAllowedCapturePolicy(uid_t uid, audio_flags_mask_t capturePolicy) { |
| Kevin Rocard | b99cc75 | 2019-03-21 20:52:24 -0700 | [diff] [blame] | 1509 | const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service(); |
| 1510 | if (aps == nullptr) return PERMISSION_DENIED; |
| Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 1511 | |
| 1512 | int32_t uidAidl = VALUE_OR_RETURN_STATUS(legacy2aidl_uid_t_int32_t(uid)); |
| 1513 | int32_t capturePolicyAidl = VALUE_OR_RETURN_STATUS( |
| 1514 | legacy2aidl_audio_flags_mask_t_int32_t_mask(capturePolicy)); |
| 1515 | return statusTFromBinderStatus(aps->setAllowedCapturePolicy(uidAidl, capturePolicyAidl)); |
| Kevin Rocard | b99cc75 | 2019-03-21 20:52:24 -0700 | [diff] [blame] | 1516 | } |
| 1517 | |
| Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 1518 | audio_offload_mode_t AudioSystem::getOffloadSupport(const audio_offload_info_t& info) { |
| Eric Laurent | 90fe31c | 2020-11-26 20:06:35 +0100 | [diff] [blame] | 1519 | ALOGV("%s", __func__); |
| Richard Fitzgerald | ad3af33 | 2013-03-25 16:54:37 +0000 | [diff] [blame] | 1520 | const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service(); |
| Eric Laurent | 90fe31c | 2020-11-26 20:06:35 +0100 | [diff] [blame] | 1521 | if (aps == 0) return AUDIO_OFFLOAD_NOT_SUPPORTED; |
| Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 1522 | |
| 1523 | auto result = [&]() -> ConversionResult<audio_offload_mode_t> { |
| Mikhail Naganov | dbf0364 | 2021-08-25 18:15:32 -0700 | [diff] [blame] | 1524 | AudioOffloadInfo infoAidl = VALUE_OR_RETURN( |
| Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 1525 | legacy2aidl_audio_offload_info_t_AudioOffloadInfo(info)); |
| 1526 | media::AudioOffloadMode retAidl; |
| 1527 | RETURN_IF_ERROR( |
| 1528 | statusTFromBinderStatus(aps->getOffloadSupport(infoAidl, &retAidl))); |
| 1529 | return aidl2legacy_AudioOffloadMode_audio_offload_mode_t(retAidl); |
| 1530 | }(); |
| 1531 | |
| 1532 | return result.value_or(static_cast<audio_offload_mode_t>(0)); |
| Richard Fitzgerald | ad3af33 | 2013-03-25 16:54:37 +0000 | [diff] [blame] | 1533 | } |
| 1534 | |
| Eric Laurent | 203b1a1 | 2014-04-01 10:34:16 -0700 | [diff] [blame] | 1535 | status_t AudioSystem::listAudioPorts(audio_port_role_t role, |
| 1536 | audio_port_type_t type, |
| Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 1537 | unsigned int* num_ports, |
| 1538 | struct audio_port_v7* ports, |
| 1539 | unsigned int* generation) { |
| 1540 | if (num_ports == nullptr || (*num_ports != 0 && ports == nullptr) || |
| 1541 | generation == nullptr) { |
| 1542 | return BAD_VALUE; |
| 1543 | } |
| 1544 | |
| Eric Laurent | 203b1a1 | 2014-04-01 10:34:16 -0700 | [diff] [blame] | 1545 | const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service(); |
| 1546 | if (aps == 0) return PERMISSION_DENIED; |
| Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 1547 | |
| 1548 | media::AudioPortRole roleAidl = VALUE_OR_RETURN_STATUS( |
| 1549 | legacy2aidl_audio_port_role_t_AudioPortRole(role)); |
| 1550 | media::AudioPortType typeAidl = VALUE_OR_RETURN_STATUS( |
| 1551 | legacy2aidl_audio_port_type_t_AudioPortType(type)); |
| Mikhail Naganov | 0078ee5 | 2021-09-30 23:06:20 +0000 | [diff] [blame] | 1552 | Int numPortsAidl; |
| Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 1553 | numPortsAidl.value = VALUE_OR_RETURN_STATUS(convertIntegral<int32_t>(*num_ports)); |
| Atneya Nair | c18e9a1 | 2022-12-18 16:45:15 -0800 | [diff] [blame] | 1554 | std::vector<media::AudioPortFw> portsAidl; |
| Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 1555 | int32_t generationAidl; |
| 1556 | |
| 1557 | RETURN_STATUS_IF_ERROR(statusTFromBinderStatus( |
| 1558 | aps->listAudioPorts(roleAidl, typeAidl, &numPortsAidl, &portsAidl, &generationAidl))); |
| 1559 | *num_ports = VALUE_OR_RETURN_STATUS(convertIntegral<unsigned int>(numPortsAidl.value)); |
| 1560 | *generation = VALUE_OR_RETURN_STATUS(convertIntegral<unsigned int>(generationAidl)); |
| 1561 | RETURN_STATUS_IF_ERROR(convertRange(portsAidl.begin(), portsAidl.end(), ports, |
| Mikhail Naganov | 8722725 | 2023-01-13 17:38:10 -0800 | [diff] [blame] | 1562 | aidl2legacy_AudioPortFw_audio_port_v7)); |
| Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 1563 | return OK; |
| Eric Laurent | 203b1a1 | 2014-04-01 10:34:16 -0700 | [diff] [blame] | 1564 | } |
| 1565 | |
| Mikhail Naganov | 15f4671 | 2023-03-23 14:52:15 -0700 | [diff] [blame] | 1566 | status_t AudioSystem::listDeclaredDevicePorts(media::AudioPortRole role, |
| 1567 | std::vector<media::AudioPortFw>* result) { |
| 1568 | if (result == nullptr) return BAD_VALUE; |
| 1569 | const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service(); |
| 1570 | if (aps == 0) return PERMISSION_DENIED; |
| 1571 | RETURN_STATUS_IF_ERROR(statusTFromBinderStatus(aps->listDeclaredDevicePorts(role, result))); |
| 1572 | return OK; |
| 1573 | } |
| 1574 | |
| Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 1575 | status_t AudioSystem::getAudioPort(struct audio_port_v7* port) { |
| 1576 | if (port == nullptr) { |
| 1577 | return BAD_VALUE; |
| 1578 | } |
| Eric Laurent | 203b1a1 | 2014-04-01 10:34:16 -0700 | [diff] [blame] | 1579 | const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service(); |
| 1580 | if (aps == 0) return PERMISSION_DENIED; |
| Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 1581 | |
| Atneya Nair | c18e9a1 | 2022-12-18 16:45:15 -0800 | [diff] [blame] | 1582 | media::AudioPortFw portAidl; |
| Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 1583 | RETURN_STATUS_IF_ERROR( |
| Mikhail Naganov | 1703156 | 2022-02-23 23:00:27 +0000 | [diff] [blame] | 1584 | statusTFromBinderStatus(aps->getAudioPort(port->id, &portAidl))); |
| Mikhail Naganov | 8722725 | 2023-01-13 17:38:10 -0800 | [diff] [blame] | 1585 | *port = VALUE_OR_RETURN_STATUS(aidl2legacy_AudioPortFw_audio_port_v7(portAidl)); |
| Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 1586 | return OK; |
| Eric Laurent | 203b1a1 | 2014-04-01 10:34:16 -0700 | [diff] [blame] | 1587 | } |
| 1588 | |
| Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 1589 | status_t AudioSystem::createAudioPatch(const struct audio_patch* patch, |
| 1590 | audio_patch_handle_t* handle) { |
| 1591 | if (patch == nullptr || handle == nullptr) { |
| 1592 | return BAD_VALUE; |
| 1593 | } |
| 1594 | |
| Eric Laurent | 203b1a1 | 2014-04-01 10:34:16 -0700 | [diff] [blame] | 1595 | const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service(); |
| 1596 | if (aps == 0) return PERMISSION_DENIED; |
| Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 1597 | |
| Atneya Nair | b6c50a3 | 2022-12-18 16:14:18 -0800 | [diff] [blame] | 1598 | media::AudioPatchFw patchAidl = VALUE_OR_RETURN_STATUS( |
| Mikhail Naganov | 8722725 | 2023-01-13 17:38:10 -0800 | [diff] [blame] | 1599 | legacy2aidl_audio_patch_AudioPatchFw(*patch)); |
| Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 1600 | int32_t handleAidl = VALUE_OR_RETURN_STATUS(legacy2aidl_audio_patch_handle_t_int32_t(*handle)); |
| 1601 | RETURN_STATUS_IF_ERROR( |
| 1602 | statusTFromBinderStatus(aps->createAudioPatch(patchAidl, handleAidl, &handleAidl))); |
| 1603 | *handle = VALUE_OR_RETURN_STATUS(aidl2legacy_int32_t_audio_patch_handle_t(handleAidl)); |
| 1604 | return OK; |
| Eric Laurent | 203b1a1 | 2014-04-01 10:34:16 -0700 | [diff] [blame] | 1605 | } |
| 1606 | |
| Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 1607 | status_t AudioSystem::releaseAudioPatch(audio_patch_handle_t handle) { |
| Eric Laurent | 203b1a1 | 2014-04-01 10:34:16 -0700 | [diff] [blame] | 1608 | const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service(); |
| 1609 | if (aps == 0) return PERMISSION_DENIED; |
| Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 1610 | |
| 1611 | int32_t handleAidl = VALUE_OR_RETURN_STATUS(legacy2aidl_audio_patch_handle_t_int32_t(handle)); |
| 1612 | return statusTFromBinderStatus(aps->releaseAudioPatch(handleAidl)); |
| Eric Laurent | 203b1a1 | 2014-04-01 10:34:16 -0700 | [diff] [blame] | 1613 | } |
| 1614 | |
| Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 1615 | status_t AudioSystem::listAudioPatches(unsigned int* num_patches, |
| 1616 | struct audio_patch* patches, |
| 1617 | unsigned int* generation) { |
| 1618 | if (num_patches == nullptr || (*num_patches != 0 && patches == nullptr) || |
| 1619 | generation == nullptr) { |
| 1620 | return BAD_VALUE; |
| 1621 | } |
| 1622 | |
| Eric Laurent | 203b1a1 | 2014-04-01 10:34:16 -0700 | [diff] [blame] | 1623 | const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service(); |
| 1624 | if (aps == 0) return PERMISSION_DENIED; |
| Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 1625 | |
| 1626 | |
| Mikhail Naganov | 0078ee5 | 2021-09-30 23:06:20 +0000 | [diff] [blame] | 1627 | Int numPatchesAidl; |
| Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 1628 | numPatchesAidl.value = VALUE_OR_RETURN_STATUS(convertIntegral<int32_t>(*num_patches)); |
| Atneya Nair | b6c50a3 | 2022-12-18 16:14:18 -0800 | [diff] [blame] | 1629 | std::vector<media::AudioPatchFw> patchesAidl; |
| Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 1630 | int32_t generationAidl; |
| 1631 | |
| 1632 | RETURN_STATUS_IF_ERROR(statusTFromBinderStatus( |
| 1633 | aps->listAudioPatches(&numPatchesAidl, &patchesAidl, &generationAidl))); |
| 1634 | *num_patches = VALUE_OR_RETURN_STATUS(convertIntegral<unsigned int>(numPatchesAidl.value)); |
| 1635 | *generation = VALUE_OR_RETURN_STATUS(convertIntegral<unsigned int>(generationAidl)); |
| 1636 | RETURN_STATUS_IF_ERROR(convertRange(patchesAidl.begin(), patchesAidl.end(), patches, |
| Mikhail Naganov | 8722725 | 2023-01-13 17:38:10 -0800 | [diff] [blame] | 1637 | aidl2legacy_AudioPatchFw_audio_patch)); |
| Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 1638 | return OK; |
| Eric Laurent | 203b1a1 | 2014-04-01 10:34:16 -0700 | [diff] [blame] | 1639 | } |
| 1640 | |
| Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 1641 | status_t AudioSystem::setAudioPortConfig(const struct audio_port_config* config) { |
| 1642 | if (config == nullptr) { |
| 1643 | return BAD_VALUE; |
| 1644 | } |
| 1645 | |
| Eric Laurent | 203b1a1 | 2014-04-01 10:34:16 -0700 | [diff] [blame] | 1646 | const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service(); |
| 1647 | if (aps == 0) return PERMISSION_DENIED; |
| Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 1648 | |
| Atneya Nair | b8de69b | 2022-12-18 17:26:26 -0800 | [diff] [blame] | 1649 | media::AudioPortConfigFw configAidl = VALUE_OR_RETURN_STATUS( |
| Mikhail Naganov | 8722725 | 2023-01-13 17:38:10 -0800 | [diff] [blame] | 1650 | legacy2aidl_audio_port_config_AudioPortConfigFw(*config)); |
| Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 1651 | return statusTFromBinderStatus(aps->setAudioPortConfig(configAidl)); |
| Eric Laurent | 203b1a1 | 2014-04-01 10:34:16 -0700 | [diff] [blame] | 1652 | } |
| 1653 | |
| Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 1654 | status_t AudioSystem::addAudioPortCallback(const sp<AudioPortCallback>& callback) { |
| Eric Laurent | b28753e | 2015-04-01 13:06:28 -0700 | [diff] [blame] | 1655 | const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service(); |
| 1656 | if (aps == 0) return PERMISSION_DENIED; |
| 1657 | |
| 1658 | Mutex::Autolock _l(gLockAPS); |
| 1659 | if (gAudioPolicyServiceClient == 0) { |
| 1660 | return NO_INIT; |
| 1661 | } |
| Eric Laurent | e8726fe | 2015-06-26 09:39:24 -0700 | [diff] [blame] | 1662 | int ret = gAudioPolicyServiceClient->addAudioPortCallback(callback); |
| 1663 | if (ret == 1) { |
| 1664 | aps->setAudioPortCallbacksEnabled(true); |
| 1665 | } |
| 1666 | return (ret < 0) ? INVALID_OPERATION : NO_ERROR; |
| Eric Laurent | b52c152 | 2014-05-20 11:27:36 -0700 | [diff] [blame] | 1667 | } |
| 1668 | |
| Jean-Michel Trivi | f613d42 | 2015-04-23 18:41:29 -0700 | [diff] [blame] | 1669 | /*static*/ |
| Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 1670 | status_t AudioSystem::removeAudioPortCallback(const sp<AudioPortCallback>& callback) { |
| Eric Laurent | b28753e | 2015-04-01 13:06:28 -0700 | [diff] [blame] | 1671 | const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service(); |
| 1672 | if (aps == 0) return PERMISSION_DENIED; |
| 1673 | |
| 1674 | Mutex::Autolock _l(gLockAPS); |
| 1675 | if (gAudioPolicyServiceClient == 0) { |
| 1676 | return NO_INIT; |
| 1677 | } |
| Eric Laurent | e8726fe | 2015-06-26 09:39:24 -0700 | [diff] [blame] | 1678 | int ret = gAudioPolicyServiceClient->removeAudioPortCallback(callback); |
| 1679 | if (ret == 0) { |
| 1680 | aps->setAudioPortCallbacksEnabled(false); |
| 1681 | } |
| 1682 | return (ret < 0) ? INVALID_OPERATION : NO_ERROR; |
| Eric Laurent | 296fb13 | 2015-05-01 11:38:42 -0700 | [diff] [blame] | 1683 | } |
| 1684 | |
| Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 1685 | status_t AudioSystem::addAudioVolumeGroupCallback(const sp<AudioVolumeGroupCallback>& callback) { |
| François Gaffie | cfe1732 | 2018-11-07 13:41:29 +0100 | [diff] [blame] | 1686 | const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service(); |
| 1687 | if (aps == 0) return PERMISSION_DENIED; |
| 1688 | |
| 1689 | Mutex::Autolock _l(gLockAPS); |
| 1690 | if (gAudioPolicyServiceClient == 0) { |
| 1691 | return NO_INIT; |
| 1692 | } |
| 1693 | int ret = gAudioPolicyServiceClient->addAudioVolumeGroupCallback(callback); |
| 1694 | if (ret == 1) { |
| 1695 | aps->setAudioVolumeGroupCallbacksEnabled(true); |
| 1696 | } |
| 1697 | return (ret < 0) ? INVALID_OPERATION : NO_ERROR; |
| 1698 | } |
| 1699 | |
| Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 1700 | status_t AudioSystem::removeAudioVolumeGroupCallback(const sp<AudioVolumeGroupCallback>& callback) { |
| François Gaffie | cfe1732 | 2018-11-07 13:41:29 +0100 | [diff] [blame] | 1701 | const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service(); |
| 1702 | if (aps == 0) return PERMISSION_DENIED; |
| 1703 | |
| 1704 | Mutex::Autolock _l(gLockAPS); |
| 1705 | if (gAudioPolicyServiceClient == 0) { |
| 1706 | return NO_INIT; |
| 1707 | } |
| 1708 | int ret = gAudioPolicyServiceClient->removeAudioVolumeGroupCallback(callback); |
| 1709 | if (ret == 0) { |
| 1710 | aps->setAudioVolumeGroupCallbacksEnabled(false); |
| 1711 | } |
| 1712 | return (ret < 0) ? INVALID_OPERATION : NO_ERROR; |
| 1713 | } |
| 1714 | |
| Eric Laurent | 296fb13 | 2015-05-01 11:38:42 -0700 | [diff] [blame] | 1715 | status_t AudioSystem::addAudioDeviceCallback( |
| Eric Laurent | 09f1ed2 | 2019-04-24 17:45:17 -0700 | [diff] [blame] | 1716 | const wp<AudioDeviceCallback>& callback, audio_io_handle_t audioIo, |
| Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 1717 | audio_port_handle_t portId) { |
| Eric Laurent | 296fb13 | 2015-05-01 11:38:42 -0700 | [diff] [blame] | 1718 | const sp<AudioFlingerClient> afc = getAudioFlingerClient(); |
| 1719 | if (afc == 0) { |
| 1720 | return NO_INIT; |
| 1721 | } |
| Eric Laurent | 09f1ed2 | 2019-04-24 17:45:17 -0700 | [diff] [blame] | 1722 | status_t status = afc->addAudioDeviceCallback(callback, audioIo, portId); |
| Eric Laurent | 7c1ec5f | 2015-07-09 14:52:47 -0700 | [diff] [blame] | 1723 | if (status == NO_ERROR) { |
| 1724 | const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger(); |
| 1725 | if (af != 0) { |
| 1726 | af->registerClient(afc); |
| 1727 | } |
| 1728 | } |
| 1729 | return status; |
| Eric Laurent | 296fb13 | 2015-05-01 11:38:42 -0700 | [diff] [blame] | 1730 | } |
| 1731 | |
| 1732 | status_t AudioSystem::removeAudioDeviceCallback( |
| Eric Laurent | 09f1ed2 | 2019-04-24 17:45:17 -0700 | [diff] [blame] | 1733 | const wp<AudioDeviceCallback>& callback, audio_io_handle_t audioIo, |
| Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 1734 | audio_port_handle_t portId) { |
| Eric Laurent | 296fb13 | 2015-05-01 11:38:42 -0700 | [diff] [blame] | 1735 | const sp<AudioFlingerClient> afc = getAudioFlingerClient(); |
| 1736 | if (afc == 0) { |
| 1737 | return NO_INIT; |
| 1738 | } |
| Eric Laurent | 09f1ed2 | 2019-04-24 17:45:17 -0700 | [diff] [blame] | 1739 | return afc->removeAudioDeviceCallback(callback, audioIo, portId); |
| Eric Laurent | 296fb13 | 2015-05-01 11:38:42 -0700 | [diff] [blame] | 1740 | } |
| 1741 | |
| Eric Laurent | 0825031 | 2022-05-03 18:12:28 +0200 | [diff] [blame] | 1742 | status_t AudioSystem::addSupportedLatencyModesCallback( |
| 1743 | const sp<SupportedLatencyModesCallback>& callback) { |
| 1744 | const sp<AudioFlingerClient> afc = getAudioFlingerClient(); |
| 1745 | if (afc == 0) { |
| 1746 | return NO_INIT; |
| 1747 | } |
| 1748 | return afc->addSupportedLatencyModesCallback(callback); |
| 1749 | } |
| 1750 | |
| 1751 | status_t AudioSystem::removeSupportedLatencyModesCallback( |
| 1752 | const sp<SupportedLatencyModesCallback>& callback) { |
| 1753 | const sp<AudioFlingerClient> afc = getAudioFlingerClient(); |
| 1754 | if (afc == 0) { |
| 1755 | return NO_INIT; |
| 1756 | } |
| 1757 | return afc->removeSupportedLatencyModesCallback(callback); |
| 1758 | } |
| 1759 | |
| Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 1760 | audio_port_handle_t AudioSystem::getDeviceIdForIo(audio_io_handle_t audioIo) { |
| Eric Laurent | 296fb13 | 2015-05-01 11:38:42 -0700 | [diff] [blame] | 1761 | const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger(); |
| 1762 | if (af == 0) return PERMISSION_DENIED; |
| 1763 | const sp<AudioIoDescriptor> desc = getIoDescriptor(audioIo); |
| 1764 | if (desc == 0) { |
| 1765 | return AUDIO_PORT_HANDLE_NONE; |
| 1766 | } |
| 1767 | return desc->getDeviceId(); |
| Eric Laurent | b28753e | 2015-04-01 13:06:28 -0700 | [diff] [blame] | 1768 | } |
| 1769 | |
| Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 1770 | status_t AudioSystem::acquireSoundTriggerSession(audio_session_t* session, |
| 1771 | audio_io_handle_t* ioHandle, |
| 1772 | audio_devices_t* device) { |
| 1773 | if (session == nullptr || ioHandle == nullptr || device == nullptr) { |
| 1774 | return BAD_VALUE; |
| 1775 | } |
| Eric Laurent | df3dc7e | 2014-07-27 18:39:40 -0700 | [diff] [blame] | 1776 | const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service(); |
| 1777 | if (aps == 0) return PERMISSION_DENIED; |
| Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 1778 | |
| 1779 | media::SoundTriggerSession retAidl; |
| 1780 | RETURN_STATUS_IF_ERROR( |
| 1781 | statusTFromBinderStatus(aps->acquireSoundTriggerSession(&retAidl))); |
| 1782 | *session = VALUE_OR_RETURN_STATUS(aidl2legacy_int32_t_audio_session_t(retAidl.session)); |
| 1783 | *ioHandle = VALUE_OR_RETURN_STATUS(aidl2legacy_int32_t_audio_io_handle_t(retAidl.ioHandle)); |
| Mikhail Naganov | 21a32ec | 2021-07-08 14:40:12 -0700 | [diff] [blame] | 1784 | *device = VALUE_OR_RETURN_STATUS( |
| 1785 | aidl2legacy_AudioDeviceDescription_audio_devices_t(retAidl.device)); |
| Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 1786 | return OK; |
| Eric Laurent | df3dc7e | 2014-07-27 18:39:40 -0700 | [diff] [blame] | 1787 | } |
| 1788 | |
| Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 1789 | status_t AudioSystem::releaseSoundTriggerSession(audio_session_t session) { |
| Eric Laurent | df3dc7e | 2014-07-27 18:39:40 -0700 | [diff] [blame] | 1790 | const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service(); |
| 1791 | if (aps == 0) return PERMISSION_DENIED; |
| Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 1792 | |
| 1793 | int32_t sessionAidl = VALUE_OR_RETURN_STATUS(legacy2aidl_audio_session_t_int32_t(session)); |
| 1794 | return statusTFromBinderStatus(aps->releaseSoundTriggerSession(sessionAidl)); |
| Eric Laurent | df3dc7e | 2014-07-27 18:39:40 -0700 | [diff] [blame] | 1795 | } |
| Eric Laurent | bb6c9a0 | 2014-09-25 14:11:47 -0700 | [diff] [blame] | 1796 | |
| Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 1797 | audio_mode_t AudioSystem::getPhoneState() { |
| Eric Laurent | bb6c9a0 | 2014-09-25 14:11:47 -0700 | [diff] [blame] | 1798 | const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service(); |
| 1799 | if (aps == 0) return AUDIO_MODE_INVALID; |
| Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 1800 | |
| 1801 | auto result = [&]() -> ConversionResult<audio_mode_t> { |
| Mikhail Naganov | ddceecc | 2021-09-03 13:58:56 -0700 | [diff] [blame] | 1802 | media::audio::common::AudioMode retAidl; |
| Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 1803 | RETURN_IF_ERROR(statusTFromBinderStatus(aps->getPhoneState(&retAidl))); |
| 1804 | return aidl2legacy_AudioMode_audio_mode_t(retAidl); |
| 1805 | }(); |
| 1806 | |
| 1807 | return result.value_or(AUDIO_MODE_INVALID); |
| Eric Laurent | bb6c9a0 | 2014-09-25 14:11:47 -0700 | [diff] [blame] | 1808 | } |
| 1809 | |
| Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 1810 | status_t AudioSystem::registerPolicyMixes(const Vector<AudioMix>& mixes, bool registration) { |
| Eric Laurent | baac183 | 2014-12-01 17:52:59 -0800 | [diff] [blame] | 1811 | const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service(); |
| 1812 | if (aps == 0) return PERMISSION_DENIED; |
| Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 1813 | |
| 1814 | size_t mixesSize = std::min(mixes.size(), size_t{MAX_MIXES_PER_POLICY}); |
| 1815 | std::vector<media::AudioMix> mixesAidl; |
| 1816 | RETURN_STATUS_IF_ERROR( |
| 1817 | convertRange(mixes.begin(), mixes.begin() + mixesSize, std::back_inserter(mixesAidl), |
| 1818 | legacy2aidl_AudioMix)); |
| 1819 | return statusTFromBinderStatus(aps->registerPolicyMixes(mixesAidl, registration)); |
| Eric Laurent | baac183 | 2014-12-01 17:52:59 -0800 | [diff] [blame] | 1820 | } |
| Eric Laurent | bb6c9a0 | 2014-09-25 14:11:47 -0700 | [diff] [blame] | 1821 | |
| Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 1822 | status_t AudioSystem::setUidDeviceAffinities(uid_t uid, const AudioDeviceTypeAddrVector& devices) { |
| Jean-Michel Trivi | bda70da | 2018-12-19 07:30:15 -0800 | [diff] [blame] | 1823 | const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service(); |
| 1824 | if (aps == 0) return PERMISSION_DENIED; |
| Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 1825 | |
| 1826 | int32_t uidAidl = VALUE_OR_RETURN_STATUS(legacy2aidl_uid_t_int32_t(uid)); |
| Mikhail Naganov | f4a7536 | 2021-09-16 00:02:54 +0000 | [diff] [blame] | 1827 | std::vector<AudioDevice> devicesAidl = VALUE_OR_RETURN_STATUS( |
| 1828 | convertContainer<std::vector<AudioDevice>>(devices, |
| Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 1829 | legacy2aidl_AudioDeviceTypeAddress)); |
| 1830 | return statusTFromBinderStatus(aps->setUidDeviceAffinities(uidAidl, devicesAidl)); |
| Jean-Michel Trivi | bda70da | 2018-12-19 07:30:15 -0800 | [diff] [blame] | 1831 | } |
| 1832 | |
| 1833 | status_t AudioSystem::removeUidDeviceAffinities(uid_t uid) { |
| 1834 | const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service(); |
| 1835 | if (aps == 0) return PERMISSION_DENIED; |
| Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 1836 | |
| 1837 | int32_t uidAidl = VALUE_OR_RETURN_STATUS(legacy2aidl_uid_t_int32_t(uid)); |
| 1838 | return statusTFromBinderStatus(aps->removeUidDeviceAffinities(uidAidl)); |
| Jean-Michel Trivi | bda70da | 2018-12-19 07:30:15 -0800 | [diff] [blame] | 1839 | } |
| 1840 | |
| Oscar Azucena | 90e7763 | 2019-11-27 17:12:28 -0800 | [diff] [blame] | 1841 | status_t AudioSystem::setUserIdDeviceAffinities(int userId, |
| Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 1842 | const AudioDeviceTypeAddrVector& devices) { |
| Oscar Azucena | 90e7763 | 2019-11-27 17:12:28 -0800 | [diff] [blame] | 1843 | const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service(); |
| 1844 | if (aps == 0) return PERMISSION_DENIED; |
| Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 1845 | |
| 1846 | int32_t userIdAidl = VALUE_OR_RETURN_STATUS(convertReinterpret<int32_t>(userId)); |
| Mikhail Naganov | f4a7536 | 2021-09-16 00:02:54 +0000 | [diff] [blame] | 1847 | std::vector<AudioDevice> devicesAidl = VALUE_OR_RETURN_STATUS( |
| 1848 | convertContainer<std::vector<AudioDevice>>(devices, |
| 1849 | legacy2aidl_AudioDeviceTypeAddress)); |
| Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 1850 | return statusTFromBinderStatus( |
| 1851 | aps->setUserIdDeviceAffinities(userIdAidl, devicesAidl)); |
| Oscar Azucena | 90e7763 | 2019-11-27 17:12:28 -0800 | [diff] [blame] | 1852 | } |
| 1853 | |
| Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 1854 | status_t AudioSystem::removeUserIdDeviceAffinities(int userId) { |
| Oscar Azucena | 90e7763 | 2019-11-27 17:12:28 -0800 | [diff] [blame] | 1855 | const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service(); |
| 1856 | if (aps == 0) return PERMISSION_DENIED; |
| Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 1857 | int32_t userIdAidl = VALUE_OR_RETURN_STATUS(convertReinterpret<int32_t>(userId)); |
| 1858 | return statusTFromBinderStatus(aps->removeUserIdDeviceAffinities(userIdAidl)); |
| Oscar Azucena | 90e7763 | 2019-11-27 17:12:28 -0800 | [diff] [blame] | 1859 | } |
| 1860 | |
| Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 1861 | status_t AudioSystem::startAudioSource(const struct audio_port_config* source, |
| 1862 | const audio_attributes_t* attributes, |
| 1863 | audio_port_handle_t* portId) { |
| 1864 | if (source == nullptr || attributes == nullptr || portId == nullptr) { |
| 1865 | return BAD_VALUE; |
| 1866 | } |
| Eric Laurent | 554a277 | 2015-04-10 11:29:24 -0700 | [diff] [blame] | 1867 | const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service(); |
| 1868 | if (aps == 0) return PERMISSION_DENIED; |
| Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 1869 | |
| Atneya Nair | b8de69b | 2022-12-18 17:26:26 -0800 | [diff] [blame] | 1870 | media::AudioPortConfigFw sourceAidl = VALUE_OR_RETURN_STATUS( |
| Mikhail Naganov | 8722725 | 2023-01-13 17:38:10 -0800 | [diff] [blame] | 1871 | legacy2aidl_audio_port_config_AudioPortConfigFw(*source)); |
| Mikhail Naganov | 2a1cf61 | 2023-05-17 11:48:43 -0700 | [diff] [blame] | 1872 | media::audio::common::AudioAttributes attributesAidl = VALUE_OR_RETURN_STATUS( |
| 1873 | legacy2aidl_audio_attributes_t_AudioAttributes(*attributes)); |
| Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 1874 | int32_t portIdAidl; |
| 1875 | RETURN_STATUS_IF_ERROR(statusTFromBinderStatus( |
| 1876 | aps->startAudioSource(sourceAidl, attributesAidl, &portIdAidl))); |
| 1877 | *portId = VALUE_OR_RETURN_STATUS(aidl2legacy_int32_t_audio_port_handle_t(portIdAidl)); |
| 1878 | return OK; |
| Eric Laurent | 554a277 | 2015-04-10 11:29:24 -0700 | [diff] [blame] | 1879 | } |
| 1880 | |
| Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 1881 | status_t AudioSystem::stopAudioSource(audio_port_handle_t portId) { |
| Eric Laurent | 554a277 | 2015-04-10 11:29:24 -0700 | [diff] [blame] | 1882 | const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service(); |
| 1883 | if (aps == 0) return PERMISSION_DENIED; |
| Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 1884 | |
| 1885 | int32_t portIdAidl = VALUE_OR_RETURN_STATUS(legacy2aidl_audio_port_handle_t_int32_t(portId)); |
| 1886 | return statusTFromBinderStatus(aps->stopAudioSource(portIdAidl)); |
| Eric Laurent | 554a277 | 2015-04-10 11:29:24 -0700 | [diff] [blame] | 1887 | } |
| 1888 | |
| Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 1889 | status_t AudioSystem::setMasterMono(bool mono) { |
| Andy Hung | 2ddee19 | 2015-12-18 17:34:44 -0800 | [diff] [blame] | 1890 | const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service(); |
| 1891 | if (aps == 0) return PERMISSION_DENIED; |
| Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 1892 | return statusTFromBinderStatus(aps->setMasterMono(mono)); |
| Andy Hung | 2ddee19 | 2015-12-18 17:34:44 -0800 | [diff] [blame] | 1893 | } |
| 1894 | |
| Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 1895 | status_t AudioSystem::getMasterMono(bool* mono) { |
| 1896 | if (mono == nullptr) { |
| 1897 | return BAD_VALUE; |
| 1898 | } |
| Andy Hung | 2ddee19 | 2015-12-18 17:34:44 -0800 | [diff] [blame] | 1899 | const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service(); |
| 1900 | if (aps == 0) return PERMISSION_DENIED; |
| Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 1901 | return statusTFromBinderStatus(aps->getMasterMono(mono)); |
| Andy Hung | 2ddee19 | 2015-12-18 17:34:44 -0800 | [diff] [blame] | 1902 | } |
| 1903 | |
| Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 1904 | status_t AudioSystem::setMasterBalance(float balance) { |
| Richard Folke Tullberg | 3fae037 | 2017-01-13 09:04:25 +0100 | [diff] [blame] | 1905 | const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger(); |
| 1906 | if (af == 0) return PERMISSION_DENIED; |
| 1907 | return af->setMasterBalance(balance); |
| 1908 | } |
| 1909 | |
| Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 1910 | status_t AudioSystem::getMasterBalance(float* balance) { |
| Richard Folke Tullberg | 3fae037 | 2017-01-13 09:04:25 +0100 | [diff] [blame] | 1911 | const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger(); |
| 1912 | if (af == 0) return PERMISSION_DENIED; |
| 1913 | return af->getMasterBalance(balance); |
| 1914 | } |
| 1915 | |
| Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 1916 | float |
| 1917 | AudioSystem::getStreamVolumeDB(audio_stream_type_t stream, int index, audio_devices_t device) { |
| Eric Laurent | ac9cef5 | 2017-06-09 15:46:26 -0700 | [diff] [blame] | 1918 | const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service(); |
| 1919 | if (aps == 0) return NAN; |
| Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 1920 | |
| 1921 | auto result = [&]() -> ConversionResult<float> { |
| Mikhail Naganov | dbf0364 | 2021-08-25 18:15:32 -0700 | [diff] [blame] | 1922 | AudioStreamType streamAidl = VALUE_OR_RETURN( |
| Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 1923 | legacy2aidl_audio_stream_type_t_AudioStreamType(stream)); |
| 1924 | int32_t indexAidl = VALUE_OR_RETURN(convertIntegral<int32_t>(index)); |
| Mikhail Naganov | f4a7536 | 2021-09-16 00:02:54 +0000 | [diff] [blame] | 1925 | AudioDeviceDescription deviceAidl = VALUE_OR_RETURN( |
| Mikhail Naganov | 21a32ec | 2021-07-08 14:40:12 -0700 | [diff] [blame] | 1926 | legacy2aidl_audio_devices_t_AudioDeviceDescription(device)); |
| Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 1927 | float retAidl; |
| 1928 | RETURN_IF_ERROR(statusTFromBinderStatus( |
| 1929 | aps->getStreamVolumeDB(streamAidl, indexAidl, deviceAidl, &retAidl))); |
| 1930 | return retAidl; |
| 1931 | }(); |
| 1932 | return result.value_or(NAN); |
| Eric Laurent | ac9cef5 | 2017-06-09 15:46:26 -0700 | [diff] [blame] | 1933 | } |
| 1934 | |
| Mikhail Naganov | 2a6a301 | 2023-02-13 11:45:03 -0800 | [diff] [blame] | 1935 | status_t AudioSystem::getMicrophones(std::vector<media::MicrophoneInfoFw>* microphones) { |
| jiabin | 46a76fa | 2018-01-05 10:18:21 -0800 | [diff] [blame] | 1936 | const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger(); |
| 1937 | if (af == 0) return PERMISSION_DENIED; |
| 1938 | return af->getMicrophones(microphones); |
| 1939 | } |
| 1940 | |
| Eric Laurent | 42896a0 | 2019-09-27 15:40:33 -0700 | [diff] [blame] | 1941 | status_t AudioSystem::setAudioHalPids(const std::vector<pid_t>& pids) { |
| Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 1942 | const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger(); |
| 1943 | if (af == nullptr) return PERMISSION_DENIED; |
| 1944 | return af->setAudioHalPids(pids); |
| Eric Laurent | 42896a0 | 2019-09-27 15:40:33 -0700 | [diff] [blame] | 1945 | } |
| 1946 | |
| Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 1947 | status_t AudioSystem::getSurroundFormats(unsigned int* numSurroundFormats, |
| 1948 | audio_format_t* surroundFormats, |
| Kriti Dang | 6537def | 2021-03-02 13:46:59 +0100 | [diff] [blame] | 1949 | bool* surroundFormatsEnabled) { |
| Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 1950 | if (numSurroundFormats == nullptr || (*numSurroundFormats != 0 && |
| 1951 | (surroundFormats == nullptr || |
| 1952 | surroundFormatsEnabled == nullptr))) { |
| 1953 | return BAD_VALUE; |
| 1954 | } |
| 1955 | |
| jiabin | 8177290 | 2018-04-02 17:52:27 -0700 | [diff] [blame] | 1956 | const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service(); |
| 1957 | if (aps == 0) return PERMISSION_DENIED; |
| Mikhail Naganov | 0078ee5 | 2021-09-30 23:06:20 +0000 | [diff] [blame] | 1958 | Int numSurroundFormatsAidl; |
| Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 1959 | numSurroundFormatsAidl.value = |
| 1960 | VALUE_OR_RETURN_STATUS(convertIntegral<int32_t>(*numSurroundFormats)); |
| Mikhail Naganov | 57bd06f | 2021-08-10 16:41:54 -0700 | [diff] [blame] | 1961 | std::vector<AudioFormatDescription> surroundFormatsAidl; |
| Kriti Dang | 877b27e | 2021-02-02 12:10:40 +0100 | [diff] [blame] | 1962 | std::vector<bool> surroundFormatsEnabledAidl; |
| Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 1963 | RETURN_STATUS_IF_ERROR(statusTFromBinderStatus( |
| Kriti Dang | 6537def | 2021-03-02 13:46:59 +0100 | [diff] [blame] | 1964 | aps->getSurroundFormats(&numSurroundFormatsAidl, &surroundFormatsAidl, |
| 1965 | &surroundFormatsEnabledAidl))); |
| Kriti Dang | 877b27e | 2021-02-02 12:10:40 +0100 | [diff] [blame] | 1966 | |
| Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 1967 | *numSurroundFormats = VALUE_OR_RETURN_STATUS( |
| 1968 | convertIntegral<unsigned int>(numSurroundFormatsAidl.value)); |
| 1969 | RETURN_STATUS_IF_ERROR( |
| 1970 | convertRange(surroundFormatsAidl.begin(), surroundFormatsAidl.end(), surroundFormats, |
| Mikhail Naganov | b60bd1b | 2021-07-15 17:31:43 -0700 | [diff] [blame] | 1971 | aidl2legacy_AudioFormatDescription_audio_format_t)); |
| Kriti Dang | 877b27e | 2021-02-02 12:10:40 +0100 | [diff] [blame] | 1972 | std::copy(surroundFormatsEnabledAidl.begin(), surroundFormatsEnabledAidl.end(), |
| 1973 | surroundFormatsEnabled); |
| Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 1974 | return OK; |
| jiabin | 8177290 | 2018-04-02 17:52:27 -0700 | [diff] [blame] | 1975 | } |
| 1976 | |
| Kriti Dang | 6537def | 2021-03-02 13:46:59 +0100 | [diff] [blame] | 1977 | status_t AudioSystem::getReportedSurroundFormats(unsigned int* numSurroundFormats, |
| 1978 | audio_format_t* surroundFormats) { |
| 1979 | if (numSurroundFormats == nullptr || (*numSurroundFormats != 0 && surroundFormats == nullptr)) { |
| 1980 | return BAD_VALUE; |
| 1981 | } |
| 1982 | |
| 1983 | const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service(); |
| 1984 | if (aps == 0) return PERMISSION_DENIED; |
| Mikhail Naganov | 0078ee5 | 2021-09-30 23:06:20 +0000 | [diff] [blame] | 1985 | Int numSurroundFormatsAidl; |
| Kriti Dang | 6537def | 2021-03-02 13:46:59 +0100 | [diff] [blame] | 1986 | numSurroundFormatsAidl.value = |
| 1987 | VALUE_OR_RETURN_STATUS(convertIntegral<int32_t>(*numSurroundFormats)); |
| Mikhail Naganov | 57bd06f | 2021-08-10 16:41:54 -0700 | [diff] [blame] | 1988 | std::vector<AudioFormatDescription> surroundFormatsAidl; |
| Kriti Dang | 6537def | 2021-03-02 13:46:59 +0100 | [diff] [blame] | 1989 | RETURN_STATUS_IF_ERROR(statusTFromBinderStatus( |
| 1990 | aps->getReportedSurroundFormats(&numSurroundFormatsAidl, &surroundFormatsAidl))); |
| 1991 | |
| 1992 | *numSurroundFormats = VALUE_OR_RETURN_STATUS( |
| 1993 | convertIntegral<unsigned int>(numSurroundFormatsAidl.value)); |
| 1994 | RETURN_STATUS_IF_ERROR( |
| 1995 | convertRange(surroundFormatsAidl.begin(), surroundFormatsAidl.end(), surroundFormats, |
| Mikhail Naganov | b60bd1b | 2021-07-15 17:31:43 -0700 | [diff] [blame] | 1996 | aidl2legacy_AudioFormatDescription_audio_format_t)); |
| Kriti Dang | 6537def | 2021-03-02 13:46:59 +0100 | [diff] [blame] | 1997 | return OK; |
| 1998 | } |
| 1999 | |
| Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 2000 | status_t AudioSystem::setSurroundFormatEnabled(audio_format_t audioFormat, bool enabled) { |
| jiabin | 8177290 | 2018-04-02 17:52:27 -0700 | [diff] [blame] | 2001 | const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service(); |
| 2002 | if (aps == 0) return PERMISSION_DENIED; |
| Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 2003 | |
| Mikhail Naganov | 57bd06f | 2021-08-10 16:41:54 -0700 | [diff] [blame] | 2004 | AudioFormatDescription audioFormatAidl = VALUE_OR_RETURN_STATUS( |
| Mikhail Naganov | b60bd1b | 2021-07-15 17:31:43 -0700 | [diff] [blame] | 2005 | legacy2aidl_audio_format_t_AudioFormatDescription(audioFormat)); |
| Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 2006 | return statusTFromBinderStatus( |
| 2007 | aps->setSurroundFormatEnabled(audioFormatAidl, enabled)); |
| jiabin | 8177290 | 2018-04-02 17:52:27 -0700 | [diff] [blame] | 2008 | } |
| 2009 | |
| Oscar Azucena | 829d90d | 2022-01-28 17:17:56 -0800 | [diff] [blame] | 2010 | status_t AudioSystem::setAssistantServicesUids(const std::vector<uid_t>& uids) { |
| Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 2011 | const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service(); |
| Eric Laurent | b78763e | 2018-10-17 10:08:02 -0700 | [diff] [blame] | 2012 | if (aps == 0) return PERMISSION_DENIED; |
| 2013 | |
| Oscar Azucena | 829d90d | 2022-01-28 17:17:56 -0800 | [diff] [blame] | 2014 | std::vector<int32_t> uidsAidl = VALUE_OR_RETURN_STATUS( |
| 2015 | convertContainer<std::vector<int32_t>>(uids, legacy2aidl_uid_t_int32_t)); |
| 2016 | return statusTFromBinderStatus(aps->setAssistantServicesUids(uidsAidl)); |
| Ahaan Ugale | f51ce00 | 2021-08-04 16:34:20 -0700 | [diff] [blame] | 2017 | } |
| 2018 | |
| Oscar Azucena | c2cdda3 | 2022-01-31 19:10:39 -0800 | [diff] [blame] | 2019 | status_t AudioSystem::setActiveAssistantServicesUids(const std::vector<uid_t>& activeUids) { |
| 2020 | const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service(); |
| 2021 | if (aps == 0) return PERMISSION_DENIED; |
| 2022 | |
| 2023 | std::vector<int32_t> activeUidsAidl = VALUE_OR_RETURN_STATUS( |
| 2024 | convertContainer<std::vector<int32_t>>(activeUids, legacy2aidl_uid_t_int32_t)); |
| 2025 | return statusTFromBinderStatus(aps->setActiveAssistantServicesUids(activeUidsAidl)); |
| 2026 | } |
| 2027 | |
| Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 2028 | status_t AudioSystem::setA11yServicesUids(const std::vector<uid_t>& uids) { |
| 2029 | const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service(); |
| Eric Laurent | b78763e | 2018-10-17 10:08:02 -0700 | [diff] [blame] | 2030 | if (aps == 0) return PERMISSION_DENIED; |
| 2031 | |
| Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 2032 | std::vector<int32_t> uidsAidl = VALUE_OR_RETURN_STATUS( |
| 2033 | convertContainer<std::vector<int32_t>>(uids, legacy2aidl_uid_t_int32_t)); |
| 2034 | return statusTFromBinderStatus(aps->setA11yServicesUids(uidsAidl)); |
| Eric Laurent | b78763e | 2018-10-17 10:08:02 -0700 | [diff] [blame] | 2035 | } |
| 2036 | |
| Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 2037 | status_t AudioSystem::setCurrentImeUid(uid_t uid) { |
| 2038 | const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service(); |
| Kohsuke Yatoh | a623a13 | 2020-03-24 20:10:26 -0700 | [diff] [blame] | 2039 | if (aps == 0) return PERMISSION_DENIED; |
| 2040 | |
| Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 2041 | int32_t uidAidl = VALUE_OR_RETURN_STATUS(legacy2aidl_uid_t_int32_t(uid)); |
| 2042 | return statusTFromBinderStatus(aps->setCurrentImeUid(uidAidl)); |
| Kohsuke Yatoh | a623a13 | 2020-03-24 20:10:26 -0700 | [diff] [blame] | 2043 | } |
| 2044 | |
| Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 2045 | bool AudioSystem::isHapticPlaybackSupported() { |
| jiabin | 6012f91 | 2018-11-02 17:06:30 -0700 | [diff] [blame] | 2046 | const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service(); |
| 2047 | if (aps == 0) return false; |
| Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 2048 | |
| 2049 | auto result = [&]() -> ConversionResult<bool> { |
| 2050 | bool retVal; |
| 2051 | RETURN_IF_ERROR( |
| 2052 | statusTFromBinderStatus(aps->isHapticPlaybackSupported(&retVal))); |
| 2053 | return retVal; |
| 2054 | }(); |
| 2055 | return result.value_or(false); |
| jiabin | 6012f91 | 2018-11-02 17:06:30 -0700 | [diff] [blame] | 2056 | } |
| 2057 | |
| Carter Hsu | 325a8eb | 2022-01-19 19:56:51 +0800 | [diff] [blame] | 2058 | bool AudioSystem::isUltrasoundSupported() { |
| 2059 | const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service(); |
| 2060 | if (aps == 0) return false; |
| 2061 | |
| 2062 | auto result = [&]() -> ConversionResult<bool> { |
| 2063 | bool retVal; |
| 2064 | RETURN_IF_ERROR( |
| 2065 | statusTFromBinderStatus(aps->isUltrasoundSupported(&retVal))); |
| 2066 | return retVal; |
| 2067 | }(); |
| 2068 | return result.value_or(false); |
| 2069 | } |
| 2070 | |
| Patty | dd80758 | 2021-11-04 21:01:03 +0800 | [diff] [blame] | 2071 | status_t AudioSystem::getHwOffloadFormatsSupportedForBluetoothMedia( |
| 2072 | audio_devices_t device, std::vector<audio_format_t>* formats) { |
| Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 2073 | if (formats == nullptr) { |
| 2074 | return BAD_VALUE; |
| 2075 | } |
| 2076 | |
| 2077 | const sp<IAudioPolicyService> |
| 2078 | & aps = AudioSystem::get_audio_policy_service(); |
| François Gaffie | d0ba9ed | 2018-11-05 11:50:42 +0100 | [diff] [blame] | 2079 | if (aps == 0) return PERMISSION_DENIED; |
| Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 2080 | |
| Mikhail Naganov | 57bd06f | 2021-08-10 16:41:54 -0700 | [diff] [blame] | 2081 | std::vector<AudioFormatDescription> formatsAidl; |
| Patty | dd80758 | 2021-11-04 21:01:03 +0800 | [diff] [blame] | 2082 | AudioDeviceDescription deviceAidl = VALUE_OR_RETURN_STATUS( |
| 2083 | legacy2aidl_audio_devices_t_AudioDeviceDescription(device)); |
| Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 2084 | RETURN_STATUS_IF_ERROR(statusTFromBinderStatus( |
| Patty | dd80758 | 2021-11-04 21:01:03 +0800 | [diff] [blame] | 2085 | aps->getHwOffloadFormatsSupportedForBluetoothMedia(deviceAidl, &formatsAidl))); |
| Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 2086 | *formats = VALUE_OR_RETURN_STATUS( |
| Mikhail Naganov | b60bd1b | 2021-07-15 17:31:43 -0700 | [diff] [blame] | 2087 | convertContainer<std::vector<audio_format_t>>( |
| 2088 | formatsAidl, |
| 2089 | aidl2legacy_AudioFormatDescription_audio_format_t)); |
| Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 2090 | return OK; |
| François Gaffie | d0ba9ed | 2018-11-05 11:50:42 +0100 | [diff] [blame] | 2091 | } |
| 2092 | |
| Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 2093 | status_t AudioSystem::listAudioProductStrategies(AudioProductStrategyVector& strategies) { |
| Arun Mirpuri | 11029ad | 2018-12-19 20:45:19 -0800 | [diff] [blame] | 2094 | const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service(); |
| 2095 | if (aps == 0) return PERMISSION_DENIED; |
| Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 2096 | |
| 2097 | std::vector<media::AudioProductStrategy> strategiesAidl; |
| 2098 | RETURN_STATUS_IF_ERROR(statusTFromBinderStatus( |
| 2099 | aps->listAudioProductStrategies(&strategiesAidl))); |
| 2100 | strategies = VALUE_OR_RETURN_STATUS( |
| 2101 | convertContainer<AudioProductStrategyVector>(strategiesAidl, |
| 2102 | aidl2legacy_AudioProductStrategy)); |
| 2103 | return OK; |
| François Gaffie | d0ba9ed | 2018-11-05 11:50:42 +0100 | [diff] [blame] | 2104 | } |
| 2105 | |
| Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 2106 | audio_attributes_t AudioSystem::streamTypeToAttributes(audio_stream_type_t stream) { |
| François Gaffie | d0ba9ed | 2018-11-05 11:50:42 +0100 | [diff] [blame] | 2107 | AudioProductStrategyVector strategies; |
| 2108 | listAudioProductStrategies(strategies); |
| Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 2109 | for (const auto& strategy : strategies) { |
| François Gaffie | f6d3bbe | 2022-04-01 14:34:29 +0200 | [diff] [blame] | 2110 | auto attrVect = strategy.getVolumeGroupAttributes(); |
| Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 2111 | auto iter = std::find_if(begin(attrVect), end(attrVect), [&stream](const auto& attributes) { |
| 2112 | return attributes.getStreamType() == stream; |
| 2113 | }); |
| François Gaffie | d0ba9ed | 2018-11-05 11:50:42 +0100 | [diff] [blame] | 2114 | if (iter != end(attrVect)) { |
| 2115 | return iter->getAttributes(); |
| 2116 | } |
| 2117 | } |
| Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 2118 | ALOGE("invalid stream type %s when converting to attributes", toString(stream).c_str()); |
| François Gaffie | d0ba9ed | 2018-11-05 11:50:42 +0100 | [diff] [blame] | 2119 | return AUDIO_ATTRIBUTES_INITIALIZER; |
| 2120 | } |
| 2121 | |
| Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 2122 | audio_stream_type_t AudioSystem::attributesToStreamType(const audio_attributes_t& attr) { |
| François Gaffie | 4b2018b | 2018-11-07 11:18:59 +0100 | [diff] [blame] | 2123 | product_strategy_t psId; |
| François Gaffie | f6d3bbe | 2022-04-01 14:34:29 +0200 | [diff] [blame] | 2124 | status_t ret = AudioSystem::getProductStrategyFromAudioAttributes(attr, psId); |
| François Gaffie | 4b2018b | 2018-11-07 11:18:59 +0100 | [diff] [blame] | 2125 | if (ret != NO_ERROR) { |
| Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 2126 | ALOGE("no strategy found for attributes %s", toString(attr).c_str()); |
| François Gaffie | 4b2018b | 2018-11-07 11:18:59 +0100 | [diff] [blame] | 2127 | return AUDIO_STREAM_MUSIC; |
| 2128 | } |
| François Gaffie | d0ba9ed | 2018-11-05 11:50:42 +0100 | [diff] [blame] | 2129 | AudioProductStrategyVector strategies; |
| 2130 | listAudioProductStrategies(strategies); |
| Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 2131 | for (const auto& strategy : strategies) { |
| François Gaffie | 4b2018b | 2018-11-07 11:18:59 +0100 | [diff] [blame] | 2132 | if (strategy.getId() == psId) { |
| François Gaffie | f6d3bbe | 2022-04-01 14:34:29 +0200 | [diff] [blame] | 2133 | auto attrVect = strategy.getVolumeGroupAttributes(); |
| Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 2134 | auto iter = std::find_if(begin(attrVect), end(attrVect), [&attr](const auto& refAttr) { |
| 2135 | return AudioProductStrategy::attributesMatches( |
| 2136 | refAttr.getAttributes(), attr); |
| 2137 | }); |
| François Gaffie | d0ba9ed | 2018-11-05 11:50:42 +0100 | [diff] [blame] | 2138 | if (iter != end(attrVect)) { |
| 2139 | return iter->getStreamType(); |
| 2140 | } |
| 2141 | } |
| 2142 | } |
| Jean-Michel Trivi | ed67865 | 2019-12-19 13:39:30 -0800 | [diff] [blame] | 2143 | switch (attr.usage) { |
| 2144 | case AUDIO_USAGE_VIRTUAL_SOURCE: |
| 2145 | // virtual source is not expected to have an associated product strategy |
| 2146 | break; |
| 2147 | default: |
| Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 2148 | ALOGE("invalid attributes %s when converting to stream", toString(attr).c_str()); |
| Jean-Michel Trivi | ed67865 | 2019-12-19 13:39:30 -0800 | [diff] [blame] | 2149 | break; |
| 2150 | } |
| François Gaffie | d0ba9ed | 2018-11-05 11:50:42 +0100 | [diff] [blame] | 2151 | return AUDIO_STREAM_MUSIC; |
| 2152 | } |
| 2153 | |
| François Gaffie | f6d3bbe | 2022-04-01 14:34:29 +0200 | [diff] [blame] | 2154 | status_t AudioSystem::getProductStrategyFromAudioAttributes(const audio_attributes_t& aa, |
| Francois Gaffie | 11b6592 | 2020-09-24 16:59:08 +0200 | [diff] [blame] | 2155 | product_strategy_t& productStrategy, |
| 2156 | bool fallbackOnDefault) { |
| François Gaffie | d0ba9ed | 2018-11-05 11:50:42 +0100 | [diff] [blame] | 2157 | const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service(); |
| François Gaffie | 4b2018b | 2018-11-07 11:18:59 +0100 | [diff] [blame] | 2158 | if (aps == 0) return PERMISSION_DENIED; |
| Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 2159 | |
| Mikhail Naganov | 2a1cf61 | 2023-05-17 11:48:43 -0700 | [diff] [blame] | 2160 | media::audio::common::AudioAttributes aaAidl = VALUE_OR_RETURN_STATUS( |
| 2161 | legacy2aidl_audio_attributes_t_AudioAttributes(aa)); |
| Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 2162 | int32_t productStrategyAidl; |
| 2163 | |
| 2164 | RETURN_STATUS_IF_ERROR(statusTFromBinderStatus( |
| Francois Gaffie | 11b6592 | 2020-09-24 16:59:08 +0200 | [diff] [blame] | 2165 | aps->getProductStrategyFromAudioAttributes(aaAidl, fallbackOnDefault, |
| 2166 | &productStrategyAidl))); |
| Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 2167 | productStrategy = VALUE_OR_RETURN_STATUS( |
| 2168 | aidl2legacy_int32_t_product_strategy_t(productStrategyAidl)); |
| 2169 | return OK; |
| François Gaffie | 4b2018b | 2018-11-07 11:18:59 +0100 | [diff] [blame] | 2170 | } |
| 2171 | |
| Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 2172 | status_t AudioSystem::listAudioVolumeGroups(AudioVolumeGroupVector& groups) { |
| François Gaffie | 4b2018b | 2018-11-07 11:18:59 +0100 | [diff] [blame] | 2173 | const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service(); |
| 2174 | if (aps == 0) return PERMISSION_DENIED; |
| Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 2175 | |
| 2176 | std::vector<media::AudioVolumeGroup> groupsAidl; |
| 2177 | RETURN_STATUS_IF_ERROR( |
| 2178 | statusTFromBinderStatus(aps->listAudioVolumeGroups(&groupsAidl))); |
| 2179 | groups = VALUE_OR_RETURN_STATUS( |
| 2180 | convertContainer<AudioVolumeGroupVector>(groupsAidl, aidl2legacy_AudioVolumeGroup)); |
| 2181 | return OK; |
| François Gaffie | 4b2018b | 2018-11-07 11:18:59 +0100 | [diff] [blame] | 2182 | } |
| 2183 | |
| François Gaffie | f6d3bbe | 2022-04-01 14:34:29 +0200 | [diff] [blame] | 2184 | status_t AudioSystem::getVolumeGroupFromAudioAttributes(const audio_attributes_t &aa, |
| Francois Gaffie | 11b6592 | 2020-09-24 16:59:08 +0200 | [diff] [blame] | 2185 | volume_group_t& volumeGroup, |
| 2186 | bool fallbackOnDefault) { |
| François Gaffie | 4b2018b | 2018-11-07 11:18:59 +0100 | [diff] [blame] | 2187 | const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service(); |
| 2188 | if (aps == 0) return PERMISSION_DENIED; |
| Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 2189 | |
| Mikhail Naganov | 2a1cf61 | 2023-05-17 11:48:43 -0700 | [diff] [blame] | 2190 | media::audio::common::AudioAttributes aaAidl = VALUE_OR_RETURN_STATUS( |
| 2191 | legacy2aidl_audio_attributes_t_AudioAttributes(aa)); |
| Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 2192 | int32_t volumeGroupAidl; |
| 2193 | RETURN_STATUS_IF_ERROR(statusTFromBinderStatus( |
| Francois Gaffie | 11b6592 | 2020-09-24 16:59:08 +0200 | [diff] [blame] | 2194 | aps->getVolumeGroupFromAudioAttributes(aaAidl, fallbackOnDefault, &volumeGroupAidl))); |
| Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 2195 | volumeGroup = VALUE_OR_RETURN_STATUS(aidl2legacy_int32_t_volume_group_t(volumeGroupAidl)); |
| 2196 | return OK; |
| Arun Mirpuri | 11029ad | 2018-12-19 20:45:19 -0800 | [diff] [blame] | 2197 | } |
| Eric Laurent | b78763e | 2018-10-17 10:08:02 -0700 | [diff] [blame] | 2198 | |
| Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 2199 | status_t AudioSystem::setRttEnabled(bool enabled) { |
| Eric Laurent | 6ede98f | 2019-06-11 14:50:30 -0700 | [diff] [blame] | 2200 | const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service(); |
| 2201 | if (aps == 0) return PERMISSION_DENIED; |
| Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 2202 | return statusTFromBinderStatus(aps->setRttEnabled(enabled)); |
| Eric Laurent | 6ede98f | 2019-06-11 14:50:30 -0700 | [diff] [blame] | 2203 | } |
| 2204 | |
| Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 2205 | bool AudioSystem::isCallScreenModeSupported() { |
| Eric Laurent | 8340e67 | 2019-11-06 11:01:08 -0800 | [diff] [blame] | 2206 | const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service(); |
| 2207 | if (aps == 0) return false; |
| Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 2208 | |
| 2209 | auto result = [&]() -> ConversionResult<bool> { |
| 2210 | bool retAidl; |
| 2211 | RETURN_IF_ERROR( |
| 2212 | statusTFromBinderStatus(aps->isCallScreenModeSupported(&retAidl))); |
| 2213 | return retAidl; |
| 2214 | }(); |
| 2215 | return result.value_or(false); |
| Eric Laurent | 8340e67 | 2019-11-06 11:01:08 -0800 | [diff] [blame] | 2216 | } |
| 2217 | |
| jiabin | 0a48893 | 2020-08-07 17:32:40 -0700 | [diff] [blame] | 2218 | status_t AudioSystem::setDevicesRoleForStrategy(product_strategy_t strategy, |
| 2219 | device_role_t role, |
| Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 2220 | const AudioDeviceTypeAddrVector& devices) { |
| Jean-Michel Trivi | 3085715 | 2019-11-01 11:04:15 -0700 | [diff] [blame] | 2221 | const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service(); |
| 2222 | if (aps == 0) { |
| 2223 | return PERMISSION_DENIED; |
| 2224 | } |
| Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 2225 | |
| 2226 | int32_t strategyAidl = VALUE_OR_RETURN_STATUS(legacy2aidl_product_strategy_t_int32_t(strategy)); |
| 2227 | media::DeviceRole roleAidl = VALUE_OR_RETURN_STATUS(legacy2aidl_device_role_t_DeviceRole(role)); |
| Mikhail Naganov | f4a7536 | 2021-09-16 00:02:54 +0000 | [diff] [blame] | 2228 | std::vector<AudioDevice> devicesAidl = VALUE_OR_RETURN_STATUS( |
| 2229 | convertContainer<std::vector<AudioDevice>>(devices, |
| 2230 | legacy2aidl_AudioDeviceTypeAddress)); |
| Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 2231 | return statusTFromBinderStatus( |
| 2232 | aps->setDevicesRoleForStrategy(strategyAidl, roleAidl, devicesAidl)); |
| Jean-Michel Trivi | 3085715 | 2019-11-01 11:04:15 -0700 | [diff] [blame] | 2233 | } |
| 2234 | |
| Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 2235 | status_t |
| 2236 | AudioSystem::removeDevicesRoleForStrategy(product_strategy_t strategy, device_role_t role) { |
| Jean-Michel Trivi | 3085715 | 2019-11-01 11:04:15 -0700 | [diff] [blame] | 2237 | const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service(); |
| 2238 | if (aps == 0) { |
| 2239 | return PERMISSION_DENIED; |
| 2240 | } |
| Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 2241 | int32_t strategyAidl = VALUE_OR_RETURN_STATUS(legacy2aidl_product_strategy_t_int32_t(strategy)); |
| 2242 | media::DeviceRole roleAidl = VALUE_OR_RETURN_STATUS(legacy2aidl_device_role_t_DeviceRole(role)); |
| 2243 | return statusTFromBinderStatus( |
| 2244 | aps->removeDevicesRoleForStrategy(strategyAidl, roleAidl)); |
| Jean-Michel Trivi | 3085715 | 2019-11-01 11:04:15 -0700 | [diff] [blame] | 2245 | } |
| 2246 | |
| jiabin | 0a48893 | 2020-08-07 17:32:40 -0700 | [diff] [blame] | 2247 | status_t AudioSystem::getDevicesForRoleAndStrategy(product_strategy_t strategy, |
| 2248 | device_role_t role, |
| Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 2249 | AudioDeviceTypeAddrVector& devices) { |
| Jean-Michel Trivi | 3085715 | 2019-11-01 11:04:15 -0700 | [diff] [blame] | 2250 | const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service(); |
| 2251 | if (aps == 0) { |
| 2252 | return PERMISSION_DENIED; |
| 2253 | } |
| Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 2254 | int32_t strategyAidl = VALUE_OR_RETURN_STATUS(legacy2aidl_product_strategy_t_int32_t(strategy)); |
| 2255 | media::DeviceRole roleAidl = VALUE_OR_RETURN_STATUS(legacy2aidl_device_role_t_DeviceRole(role)); |
| Mikhail Naganov | f4a7536 | 2021-09-16 00:02:54 +0000 | [diff] [blame] | 2256 | std::vector<AudioDevice> devicesAidl; |
| Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 2257 | RETURN_STATUS_IF_ERROR(statusTFromBinderStatus( |
| 2258 | aps->getDevicesForRoleAndStrategy(strategyAidl, roleAidl, &devicesAidl))); |
| 2259 | devices = VALUE_OR_RETURN_STATUS( |
| 2260 | convertContainer<AudioDeviceTypeAddrVector>(devicesAidl, |
| 2261 | aidl2legacy_AudioDeviceTypeAddress)); |
| 2262 | return OK; |
| Jean-Michel Trivi | 3085715 | 2019-11-01 11:04:15 -0700 | [diff] [blame] | 2263 | } |
| 2264 | |
| Jiabin Huang | 3b98d32 | 2020-09-03 17:54:16 +0000 | [diff] [blame] | 2265 | status_t AudioSystem::setDevicesRoleForCapturePreset(audio_source_t audioSource, |
| 2266 | device_role_t role, |
| Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 2267 | const AudioDeviceTypeAddrVector& devices) { |
| Jiabin Huang | 3b98d32 | 2020-09-03 17:54:16 +0000 | [diff] [blame] | 2268 | const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service(); |
| 2269 | if (aps == 0) { |
| 2270 | return PERMISSION_DENIED; |
| 2271 | } |
| Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 2272 | |
| Mikhail Naganov | ddceecc | 2021-09-03 13:58:56 -0700 | [diff] [blame] | 2273 | AudioSource audioSourceAidl = VALUE_OR_RETURN_STATUS( |
| 2274 | legacy2aidl_audio_source_t_AudioSource(audioSource)); |
| Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 2275 | media::DeviceRole roleAidl = VALUE_OR_RETURN_STATUS(legacy2aidl_device_role_t_DeviceRole(role)); |
| Mikhail Naganov | f4a7536 | 2021-09-16 00:02:54 +0000 | [diff] [blame] | 2276 | std::vector<AudioDevice> devicesAidl = VALUE_OR_RETURN_STATUS( |
| 2277 | convertContainer<std::vector<AudioDevice>>(devices, |
| 2278 | legacy2aidl_AudioDeviceTypeAddress)); |
| Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 2279 | return statusTFromBinderStatus( |
| 2280 | aps->setDevicesRoleForCapturePreset(audioSourceAidl, roleAidl, devicesAidl)); |
| Jiabin Huang | 3b98d32 | 2020-09-03 17:54:16 +0000 | [diff] [blame] | 2281 | } |
| 2282 | |
| 2283 | status_t AudioSystem::addDevicesRoleForCapturePreset(audio_source_t audioSource, |
| 2284 | device_role_t role, |
| Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 2285 | const AudioDeviceTypeAddrVector& devices) { |
| Jiabin Huang | 3b98d32 | 2020-09-03 17:54:16 +0000 | [diff] [blame] | 2286 | const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service(); |
| 2287 | if (aps == 0) { |
| 2288 | return PERMISSION_DENIED; |
| 2289 | } |
| Mikhail Naganov | ddceecc | 2021-09-03 13:58:56 -0700 | [diff] [blame] | 2290 | AudioSource audioSourceAidl = VALUE_OR_RETURN_STATUS( |
| 2291 | legacy2aidl_audio_source_t_AudioSource(audioSource)); |
| Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 2292 | media::DeviceRole roleAidl = VALUE_OR_RETURN_STATUS(legacy2aidl_device_role_t_DeviceRole(role)); |
| Mikhail Naganov | f4a7536 | 2021-09-16 00:02:54 +0000 | [diff] [blame] | 2293 | std::vector<AudioDevice> devicesAidl = VALUE_OR_RETURN_STATUS( |
| 2294 | convertContainer<std::vector<AudioDevice>>(devices, |
| 2295 | legacy2aidl_AudioDeviceTypeAddress)); |
| Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 2296 | return statusTFromBinderStatus( |
| 2297 | aps->addDevicesRoleForCapturePreset(audioSourceAidl, roleAidl, devicesAidl)); |
| Jiabin Huang | 3b98d32 | 2020-09-03 17:54:16 +0000 | [diff] [blame] | 2298 | } |
| 2299 | |
| 2300 | status_t AudioSystem::removeDevicesRoleForCapturePreset( |
| Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 2301 | audio_source_t audioSource, device_role_t role, const AudioDeviceTypeAddrVector& devices) { |
| Jiabin Huang | 3b98d32 | 2020-09-03 17:54:16 +0000 | [diff] [blame] | 2302 | const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service(); |
| 2303 | if (aps == 0) { |
| 2304 | return PERMISSION_DENIED; |
| 2305 | } |
| Mikhail Naganov | ddceecc | 2021-09-03 13:58:56 -0700 | [diff] [blame] | 2306 | AudioSource audioSourceAidl = VALUE_OR_RETURN_STATUS( |
| 2307 | legacy2aidl_audio_source_t_AudioSource(audioSource)); |
| Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 2308 | media::DeviceRole roleAidl = VALUE_OR_RETURN_STATUS(legacy2aidl_device_role_t_DeviceRole(role)); |
| Mikhail Naganov | f4a7536 | 2021-09-16 00:02:54 +0000 | [diff] [blame] | 2309 | std::vector<AudioDevice> devicesAidl = VALUE_OR_RETURN_STATUS( |
| 2310 | convertContainer<std::vector<AudioDevice>>(devices, |
| 2311 | legacy2aidl_AudioDeviceTypeAddress)); |
| Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 2312 | return statusTFromBinderStatus( |
| 2313 | aps->removeDevicesRoleForCapturePreset(audioSourceAidl, roleAidl, devicesAidl)); |
| Jiabin Huang | 3b98d32 | 2020-09-03 17:54:16 +0000 | [diff] [blame] | 2314 | } |
| 2315 | |
| 2316 | status_t AudioSystem::clearDevicesRoleForCapturePreset(audio_source_t audioSource, |
| Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 2317 | device_role_t role) { |
| Jiabin Huang | 3b98d32 | 2020-09-03 17:54:16 +0000 | [diff] [blame] | 2318 | const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service(); |
| 2319 | if (aps == 0) { |
| 2320 | return PERMISSION_DENIED; |
| 2321 | } |
| Mikhail Naganov | ddceecc | 2021-09-03 13:58:56 -0700 | [diff] [blame] | 2322 | AudioSource audioSourceAidl = VALUE_OR_RETURN_STATUS( |
| 2323 | legacy2aidl_audio_source_t_AudioSource(audioSource)); |
| Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 2324 | media::DeviceRole roleAidl = VALUE_OR_RETURN_STATUS(legacy2aidl_device_role_t_DeviceRole(role)); |
| 2325 | return statusTFromBinderStatus( |
| 2326 | aps->clearDevicesRoleForCapturePreset(audioSourceAidl, roleAidl)); |
| Jiabin Huang | 3b98d32 | 2020-09-03 17:54:16 +0000 | [diff] [blame] | 2327 | } |
| 2328 | |
| 2329 | status_t AudioSystem::getDevicesForRoleAndCapturePreset(audio_source_t audioSource, |
| 2330 | device_role_t role, |
| Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 2331 | AudioDeviceTypeAddrVector& devices) { |
| Jiabin Huang | 3b98d32 | 2020-09-03 17:54:16 +0000 | [diff] [blame] | 2332 | const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service(); |
| 2333 | if (aps == 0) { |
| 2334 | return PERMISSION_DENIED; |
| 2335 | } |
| Mikhail Naganov | ddceecc | 2021-09-03 13:58:56 -0700 | [diff] [blame] | 2336 | AudioSource audioSourceAidl = VALUE_OR_RETURN_STATUS( |
| 2337 | legacy2aidl_audio_source_t_AudioSource(audioSource)); |
| Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 2338 | media::DeviceRole roleAidl = VALUE_OR_RETURN_STATUS(legacy2aidl_device_role_t_DeviceRole(role)); |
| Mikhail Naganov | f4a7536 | 2021-09-16 00:02:54 +0000 | [diff] [blame] | 2339 | std::vector<AudioDevice> devicesAidl; |
| Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 2340 | RETURN_STATUS_IF_ERROR(statusTFromBinderStatus( |
| 2341 | aps->getDevicesForRoleAndCapturePreset(audioSourceAidl, roleAidl, &devicesAidl))); |
| 2342 | devices = VALUE_OR_RETURN_STATUS( |
| 2343 | convertContainer<AudioDeviceTypeAddrVector>(devicesAidl, |
| 2344 | aidl2legacy_AudioDeviceTypeAddress)); |
| 2345 | return OK; |
| Jiabin Huang | 3b98d32 | 2020-09-03 17:54:16 +0000 | [diff] [blame] | 2346 | } |
| 2347 | |
| Eric Laurent | 81dd0f5 | 2021-07-05 11:54:40 +0200 | [diff] [blame] | 2348 | status_t AudioSystem::getSpatializer(const sp<media::INativeSpatializerCallback>& callback, |
| 2349 | sp<media::ISpatializer>* spatializer) { |
| 2350 | const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service(); |
| 2351 | if (spatializer == nullptr) { |
| 2352 | return BAD_VALUE; |
| 2353 | } |
| 2354 | if (aps == 0) { |
| 2355 | return PERMISSION_DENIED; |
| 2356 | } |
| 2357 | media::GetSpatializerResponse response; |
| 2358 | RETURN_STATUS_IF_ERROR(statusTFromBinderStatus( |
| 2359 | aps->getSpatializer(callback, &response))); |
| 2360 | |
| 2361 | *spatializer = response.spatializer; |
| 2362 | return OK; |
| 2363 | } |
| 2364 | |
| 2365 | status_t AudioSystem::canBeSpatialized(const audio_attributes_t *attr, |
| 2366 | const audio_config_t *config, |
| 2367 | const AudioDeviceTypeAddrVector &devices, |
| 2368 | bool *canBeSpatialized) { |
| 2369 | const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service(); |
| Ram Mohan | 4329701 | 2022-08-30 16:11:49 +0530 | [diff] [blame] | 2370 | if (canBeSpatialized == nullptr) { |
| 2371 | return BAD_VALUE; |
| 2372 | } |
| Eric Laurent | 81dd0f5 | 2021-07-05 11:54:40 +0200 | [diff] [blame] | 2373 | if (aps == 0) { |
| 2374 | return PERMISSION_DENIED; |
| 2375 | } |
| 2376 | audio_attributes_t attributes = attr != nullptr ? *attr : AUDIO_ATTRIBUTES_INITIALIZER; |
| 2377 | audio_config_t configuration = config != nullptr ? *config : AUDIO_CONFIG_INITIALIZER; |
| 2378 | |
| Mikhail Naganov | 2a1cf61 | 2023-05-17 11:48:43 -0700 | [diff] [blame] | 2379 | std::optional<media::audio::common::AudioAttributes> attrAidl = VALUE_OR_RETURN_STATUS( |
| 2380 | legacy2aidl_audio_attributes_t_AudioAttributes(attributes)); |
| Mikhail Naganov | dbf0364 | 2021-08-25 18:15:32 -0700 | [diff] [blame] | 2381 | std::optional<AudioConfig> configAidl = VALUE_OR_RETURN_STATUS( |
| Eric Laurent | 81dd0f5 | 2021-07-05 11:54:40 +0200 | [diff] [blame] | 2382 | legacy2aidl_audio_config_t_AudioConfig(configuration, false /*isInput*/)); |
| Mikhail Naganov | f4a7536 | 2021-09-16 00:02:54 +0000 | [diff] [blame] | 2383 | std::vector<AudioDevice> devicesAidl = VALUE_OR_RETURN_STATUS( |
| 2384 | convertContainer<std::vector<AudioDevice>>(devices, |
| 2385 | legacy2aidl_AudioDeviceTypeAddress)); |
| Eric Laurent | 81dd0f5 | 2021-07-05 11:54:40 +0200 | [diff] [blame] | 2386 | RETURN_STATUS_IF_ERROR(statusTFromBinderStatus( |
| 2387 | aps->canBeSpatialized(attrAidl, configAidl, devicesAidl, canBeSpatialized))); |
| 2388 | return OK; |
| 2389 | } |
| 2390 | |
| jiabin | 2b9d5a1 | 2021-12-10 01:06:29 +0000 | [diff] [blame] | 2391 | status_t AudioSystem::getDirectPlaybackSupport(const audio_attributes_t *attr, |
| 2392 | const audio_config_t *config, |
| 2393 | audio_direct_mode_t* directMode) { |
| 2394 | if (attr == nullptr || config == nullptr || directMode == nullptr) { |
| 2395 | return BAD_VALUE; |
| 2396 | } |
| 2397 | |
| 2398 | const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service(); |
| 2399 | if (aps == 0) { |
| 2400 | return PERMISSION_DENIED; |
| 2401 | } |
| 2402 | |
| Mikhail Naganov | 2a1cf61 | 2023-05-17 11:48:43 -0700 | [diff] [blame] | 2403 | media::audio::common::AudioAttributes attrAidl = VALUE_OR_RETURN_STATUS( |
| 2404 | legacy2aidl_audio_attributes_t_AudioAttributes(*attr)); |
| jiabin | 2b9d5a1 | 2021-12-10 01:06:29 +0000 | [diff] [blame] | 2405 | AudioConfig configAidl = VALUE_OR_RETURN_STATUS( |
| 2406 | legacy2aidl_audio_config_t_AudioConfig(*config, false /*isInput*/)); |
| 2407 | |
| 2408 | media::AudioDirectMode retAidl; |
| 2409 | RETURN_STATUS_IF_ERROR(statusTFromBinderStatus( |
| 2410 | aps->getDirectPlaybackSupport(attrAidl, configAidl, &retAidl))); |
| 2411 | *directMode = VALUE_OR_RETURN_STATUS(aidl2legacy_int32_t_audio_direct_mode_t_mask( |
| 2412 | static_cast<int32_t>(retAidl))); |
| 2413 | return NO_ERROR; |
| 2414 | } |
| 2415 | |
| Dorin Drimus | f2196d8 | 2022-01-03 12:11:18 +0100 | [diff] [blame] | 2416 | status_t AudioSystem::getDirectProfilesForAttributes(const audio_attributes_t* attr, |
| 2417 | std::vector<audio_profile>* audioProfiles) { |
| Mikhail Naganov | 7b75108 | 2022-06-22 03:24:59 +0000 | [diff] [blame] | 2418 | if (attr == nullptr || audioProfiles == nullptr) { |
| Dorin Drimus | f2196d8 | 2022-01-03 12:11:18 +0100 | [diff] [blame] | 2419 | return BAD_VALUE; |
| 2420 | } |
| 2421 | |
| 2422 | const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service(); |
| 2423 | if (aps == 0) { |
| 2424 | return PERMISSION_DENIED; |
| 2425 | } |
| 2426 | |
| Mikhail Naganov | 2a1cf61 | 2023-05-17 11:48:43 -0700 | [diff] [blame] | 2427 | media::audio::common::AudioAttributes attrAidl = VALUE_OR_RETURN_STATUS( |
| 2428 | legacy2aidl_audio_attributes_t_AudioAttributes(*attr)); |
| Dorin Drimus | f2196d8 | 2022-01-03 12:11:18 +0100 | [diff] [blame] | 2429 | |
| 2430 | std::vector<media::audio::common::AudioProfile> audioProfilesAidl; |
| 2431 | RETURN_STATUS_IF_ERROR(statusTFromBinderStatus( |
| 2432 | aps->getDirectProfilesForAttributes(attrAidl, &audioProfilesAidl))); |
| 2433 | *audioProfiles = VALUE_OR_RETURN_STATUS(convertContainer<std::vector<audio_profile>>( |
| 2434 | audioProfilesAidl, aidl2legacy_AudioProfile_audio_profile, false /*isInput*/)); |
| 2435 | |
| 2436 | return NO_ERROR; |
| 2437 | } |
| Eric Laurent | 81dd0f5 | 2021-07-05 11:54:40 +0200 | [diff] [blame] | 2438 | |
| Eric Laurent | 0825031 | 2022-05-03 18:12:28 +0200 | [diff] [blame] | 2439 | status_t AudioSystem::setRequestedLatencyMode( |
| 2440 | audio_io_handle_t output, audio_latency_mode_t mode) { |
| 2441 | const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger(); |
| 2442 | if (af == nullptr) { |
| 2443 | return PERMISSION_DENIED; |
| 2444 | } |
| 2445 | return af->setRequestedLatencyMode(output, mode); |
| 2446 | } |
| 2447 | |
| 2448 | status_t AudioSystem::getSupportedLatencyModes(audio_io_handle_t output, |
| 2449 | std::vector<audio_latency_mode_t>* modes) { |
| 2450 | const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger(); |
| 2451 | if (af == nullptr) { |
| 2452 | return PERMISSION_DENIED; |
| 2453 | } |
| 2454 | return af->getSupportedLatencyModes(output, modes); |
| 2455 | } |
| 2456 | |
| Eric Laurent | 49879b7 | 2022-12-20 20:20:23 +0100 | [diff] [blame] | 2457 | status_t AudioSystem::setBluetoothVariableLatencyEnabled(bool enabled) { |
| Eric Laurent | 01eb164 | 2022-12-16 11:45:07 +0100 | [diff] [blame] | 2458 | const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger(); |
| 2459 | if (af == nullptr) { |
| 2460 | return PERMISSION_DENIED; |
| 2461 | } |
| Eric Laurent | 49879b7 | 2022-12-20 20:20:23 +0100 | [diff] [blame] | 2462 | return af->setBluetoothVariableLatencyEnabled(enabled); |
| Eric Laurent | 01eb164 | 2022-12-16 11:45:07 +0100 | [diff] [blame] | 2463 | } |
| 2464 | |
| Eric Laurent | 49879b7 | 2022-12-20 20:20:23 +0100 | [diff] [blame] | 2465 | status_t AudioSystem::isBluetoothVariableLatencyEnabled( |
| 2466 | bool *enabled) { |
| 2467 | const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger(); |
| 2468 | if (af == nullptr) { |
| 2469 | return PERMISSION_DENIED; |
| 2470 | } |
| 2471 | return af->isBluetoothVariableLatencyEnabled(enabled); |
| 2472 | } |
| 2473 | |
| 2474 | status_t AudioSystem::supportsBluetoothVariableLatency( |
| Eric Laurent | 01eb164 | 2022-12-16 11:45:07 +0100 | [diff] [blame] | 2475 | bool *support) { |
| 2476 | const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger(); |
| 2477 | if (af == nullptr) { |
| 2478 | return PERMISSION_DENIED; |
| 2479 | } |
| Eric Laurent | 49879b7 | 2022-12-20 20:20:23 +0100 | [diff] [blame] | 2480 | return af->supportsBluetoothVariableLatency(support); |
| Eric Laurent | 01eb164 | 2022-12-16 11:45:07 +0100 | [diff] [blame] | 2481 | } |
| 2482 | |
| Mikhail Naganov | 1fba38c | 2023-05-03 17:45:36 -0700 | [diff] [blame] | 2483 | status_t AudioSystem::getAudioPolicyConfig(media::AudioPolicyConfig *config) { |
| 2484 | const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger(); |
| 2485 | if (af == nullptr) { |
| 2486 | return PERMISSION_DENIED; |
| 2487 | } |
| 2488 | return af->getAudioPolicyConfig(config); |
| 2489 | } |
| 2490 | |
| Ytai Ben-Tsvi | 5120eda | 2020-03-26 09:41:15 -0700 | [diff] [blame] | 2491 | class CaptureStateListenerImpl : public media::BnCaptureStateListener, |
| 2492 | public IBinder::DeathRecipient { |
| 2493 | public: |
| Ytai Ben-Tsvi | d9f8283 | 2020-08-27 09:31:10 -0700 | [diff] [blame] | 2494 | CaptureStateListenerImpl( |
| 2495 | const sp<IAudioPolicyService>& aps, |
| 2496 | const sp<AudioSystem::CaptureStateListener>& listener) |
| Ytai Ben-Tsvi | a46b6d3 | 2020-08-31 13:29:11 -0700 | [diff] [blame] | 2497 | : mAps(aps), mListener(listener) {} |
| 2498 | |
| 2499 | void init() { |
| Ytai Ben-Tsvi | d9f8283 | 2020-08-27 09:31:10 -0700 | [diff] [blame] | 2500 | bool active; |
| Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 2501 | status_t status = statusTFromBinderStatus( |
| 2502 | mAps->registerSoundTriggerCaptureStateListener(this, &active)); |
| Ytai Ben-Tsvi | d9f8283 | 2020-08-27 09:31:10 -0700 | [diff] [blame] | 2503 | if (status != NO_ERROR) { |
| 2504 | mListener->onServiceDied(); |
| 2505 | return; |
| 2506 | } |
| 2507 | mListener->onStateChanged(active); |
| Ytai Ben-Tsvi | a46b6d3 | 2020-08-31 13:29:11 -0700 | [diff] [blame] | 2508 | IInterface::asBinder(mAps)->linkToDeath(this); |
| Ytai Ben-Tsvi | d9f8283 | 2020-08-27 09:31:10 -0700 | [diff] [blame] | 2509 | } |
| 2510 | |
| Ytai Ben-Tsvi | 5120eda | 2020-03-26 09:41:15 -0700 | [diff] [blame] | 2511 | binder::Status setCaptureState(bool active) override { |
| 2512 | Mutex::Autolock _l(gSoundTriggerCaptureStateListenerLock); |
| Ytai Ben-Tsvi | d9f8283 | 2020-08-27 09:31:10 -0700 | [diff] [blame] | 2513 | mListener->onStateChanged(active); |
| Ytai Ben-Tsvi | 5120eda | 2020-03-26 09:41:15 -0700 | [diff] [blame] | 2514 | return binder::Status::ok(); |
| 2515 | } |
| 2516 | |
| 2517 | void binderDied(const wp<IBinder>&) override { |
| 2518 | Mutex::Autolock _l(gSoundTriggerCaptureStateListenerLock); |
| Ytai Ben-Tsvi | d9f8283 | 2020-08-27 09:31:10 -0700 | [diff] [blame] | 2519 | mListener->onServiceDied(); |
| Ytai Ben-Tsvi | 5120eda | 2020-03-26 09:41:15 -0700 | [diff] [blame] | 2520 | gSoundTriggerCaptureStateListener = nullptr; |
| 2521 | } |
| Ytai Ben-Tsvi | d9f8283 | 2020-08-27 09:31:10 -0700 | [diff] [blame] | 2522 | |
| 2523 | private: |
| 2524 | // Need this in order to keep the death receipent alive. |
| 2525 | sp<IAudioPolicyService> mAps; |
| 2526 | sp<AudioSystem::CaptureStateListener> mListener; |
| Ytai Ben-Tsvi | 5120eda | 2020-03-26 09:41:15 -0700 | [diff] [blame] | 2527 | }; |
| 2528 | |
| 2529 | status_t AudioSystem::registerSoundTriggerCaptureStateListener( |
| Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 2530 | const sp<CaptureStateListener>& listener) { |
| Ytai Ben-Tsvi | d9f8283 | 2020-08-27 09:31:10 -0700 | [diff] [blame] | 2531 | LOG_ALWAYS_FATAL_IF(listener == nullptr); |
| 2532 | |
| Ytai Ben-Tsvi | 5120eda | 2020-03-26 09:41:15 -0700 | [diff] [blame] | 2533 | const sp<IAudioPolicyService>& aps = |
| 2534 | AudioSystem::get_audio_policy_service(); |
| 2535 | if (aps == 0) { |
| 2536 | return PERMISSION_DENIED; |
| 2537 | } |
| 2538 | |
| Ytai Ben-Tsvi | 5120eda | 2020-03-26 09:41:15 -0700 | [diff] [blame] | 2539 | Mutex::Autolock _l(gSoundTriggerCaptureStateListenerLock); |
| Ytai Ben-Tsvi | d9f8283 | 2020-08-27 09:31:10 -0700 | [diff] [blame] | 2540 | gSoundTriggerCaptureStateListener = new CaptureStateListenerImpl(aps, listener); |
| Ytai Ben-Tsvi | a46b6d3 | 2020-08-31 13:29:11 -0700 | [diff] [blame] | 2541 | gSoundTriggerCaptureStateListener->init(); |
| Ytai Ben-Tsvi | 5120eda | 2020-03-26 09:41:15 -0700 | [diff] [blame] | 2542 | |
| Ytai Ben-Tsvi | 5120eda | 2020-03-26 09:41:15 -0700 | [diff] [blame] | 2543 | return NO_ERROR; |
| 2544 | } |
| 2545 | |
| jiabin | 1319f5a | 2021-03-30 22:21:24 +0000 | [diff] [blame] | 2546 | status_t AudioSystem::setVibratorInfos( |
| 2547 | const std::vector<media::AudioVibratorInfo>& vibratorInfos) { |
| 2548 | const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger(); |
| 2549 | if (af == nullptr) { |
| 2550 | return PERMISSION_DENIED; |
| 2551 | } |
| 2552 | return af->setVibratorInfos(vibratorInfos); |
| 2553 | } |
| 2554 | |
| Jiabin Huang | ebe6410 | 2021-09-07 20:01:07 +0000 | [diff] [blame] | 2555 | status_t AudioSystem::getMmapPolicyInfo( |
| jiabin | e99d088 | 2021-09-17 05:21:25 +0000 | [diff] [blame] | 2556 | AudioMMapPolicyType policyType, std::vector<AudioMMapPolicyInfo> *policyInfos) { |
| Jiabin Huang | ebe6410 | 2021-09-07 20:01:07 +0000 | [diff] [blame] | 2557 | const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger(); |
| 2558 | if (af == nullptr) { |
| 2559 | return PERMISSION_DENIED; |
| 2560 | } |
| 2561 | return af->getMmapPolicyInfos(policyType, policyInfos); |
| 2562 | } |
| 2563 | |
| jiabin | e504e7b | 2021-09-18 00:27:08 +0000 | [diff] [blame] | 2564 | int32_t AudioSystem::getAAudioMixerBurstCount() { |
| 2565 | const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger(); |
| 2566 | if (af == nullptr) { |
| 2567 | return PERMISSION_DENIED; |
| 2568 | } |
| 2569 | return af->getAAudioMixerBurstCount(); |
| 2570 | } |
| 2571 | |
| 2572 | int32_t AudioSystem::getAAudioHardwareBurstMinUsec() { |
| 2573 | const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger(); |
| 2574 | if (af == nullptr) { |
| 2575 | return PERMISSION_DENIED; |
| 2576 | } |
| 2577 | return af->getAAudioHardwareBurstMinUsec(); |
| 2578 | } |
| 2579 | |
| Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 2580 | // --------------------------------------------------------------------------- |
| 2581 | |
| Eric Laurent | e8726fe | 2015-06-26 09:39:24 -0700 | [diff] [blame] | 2582 | int AudioSystem::AudioPolicyServiceClient::addAudioPortCallback( |
| Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 2583 | const sp<AudioPortCallback>& callback) { |
| Eric Laurent | b28753e | 2015-04-01 13:06:28 -0700 | [diff] [blame] | 2584 | Mutex::Autolock _l(mLock); |
| 2585 | for (size_t i = 0; i < mAudioPortCallbacks.size(); i++) { |
| Eric Laurent | 296fb13 | 2015-05-01 11:38:42 -0700 | [diff] [blame] | 2586 | if (mAudioPortCallbacks[i] == callback) { |
| Eric Laurent | e8726fe | 2015-06-26 09:39:24 -0700 | [diff] [blame] | 2587 | return -1; |
| Eric Laurent | b28753e | 2015-04-01 13:06:28 -0700 | [diff] [blame] | 2588 | } |
| 2589 | } |
| Eric Laurent | 296fb13 | 2015-05-01 11:38:42 -0700 | [diff] [blame] | 2590 | mAudioPortCallbacks.add(callback); |
| Eric Laurent | e8726fe | 2015-06-26 09:39:24 -0700 | [diff] [blame] | 2591 | return mAudioPortCallbacks.size(); |
| Eric Laurent | b28753e | 2015-04-01 13:06:28 -0700 | [diff] [blame] | 2592 | } |
| 2593 | |
| Eric Laurent | e8726fe | 2015-06-26 09:39:24 -0700 | [diff] [blame] | 2594 | int AudioSystem::AudioPolicyServiceClient::removeAudioPortCallback( |
| Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 2595 | const sp<AudioPortCallback>& callback) { |
| Eric Laurent | b28753e | 2015-04-01 13:06:28 -0700 | [diff] [blame] | 2596 | Mutex::Autolock _l(mLock); |
| 2597 | size_t i; |
| 2598 | for (i = 0; i < mAudioPortCallbacks.size(); i++) { |
| Eric Laurent | 296fb13 | 2015-05-01 11:38:42 -0700 | [diff] [blame] | 2599 | if (mAudioPortCallbacks[i] == callback) { |
| Eric Laurent | b28753e | 2015-04-01 13:06:28 -0700 | [diff] [blame] | 2600 | break; |
| 2601 | } |
| 2602 | } |
| 2603 | if (i == mAudioPortCallbacks.size()) { |
| Eric Laurent | e8726fe | 2015-06-26 09:39:24 -0700 | [diff] [blame] | 2604 | return -1; |
| Eric Laurent | b28753e | 2015-04-01 13:06:28 -0700 | [diff] [blame] | 2605 | } |
| 2606 | mAudioPortCallbacks.removeAt(i); |
| Eric Laurent | e8726fe | 2015-06-26 09:39:24 -0700 | [diff] [blame] | 2607 | return mAudioPortCallbacks.size(); |
| Eric Laurent | b28753e | 2015-04-01 13:06:28 -0700 | [diff] [blame] | 2608 | } |
| 2609 | |
| Eric Laurent | 296fb13 | 2015-05-01 11:38:42 -0700 | [diff] [blame] | 2610 | |
| Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 2611 | Status AudioSystem::AudioPolicyServiceClient::onAudioPortListUpdate() { |
| Eric Laurent | b28753e | 2015-04-01 13:06:28 -0700 | [diff] [blame] | 2612 | Mutex::Autolock _l(mLock); |
| 2613 | for (size_t i = 0; i < mAudioPortCallbacks.size(); i++) { |
| 2614 | mAudioPortCallbacks[i]->onAudioPortListUpdate(); |
| 2615 | } |
| Ytai Ben-Tsvi | 7e7a79d | 2020-12-15 16:48:16 -0800 | [diff] [blame] | 2616 | return Status::ok(); |
| Eric Laurent | b28753e | 2015-04-01 13:06:28 -0700 | [diff] [blame] | 2617 | } |
| 2618 | |
| Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 2619 | Status AudioSystem::AudioPolicyServiceClient::onAudioPatchListUpdate() { |
| Eric Laurent | b28753e | 2015-04-01 13:06:28 -0700 | [diff] [blame] | 2620 | Mutex::Autolock _l(mLock); |
| 2621 | for (size_t i = 0; i < mAudioPortCallbacks.size(); i++) { |
| 2622 | mAudioPortCallbacks[i]->onAudioPatchListUpdate(); |
| 2623 | } |
| Ytai Ben-Tsvi | 7e7a79d | 2020-12-15 16:48:16 -0800 | [diff] [blame] | 2624 | return Status::ok(); |
| Eric Laurent | b28753e | 2015-04-01 13:06:28 -0700 | [diff] [blame] | 2625 | } |
| 2626 | |
| François Gaffie | cfe1732 | 2018-11-07 13:41:29 +0100 | [diff] [blame] | 2627 | // ---------------------------------------------------------------------------- |
| 2628 | int AudioSystem::AudioPolicyServiceClient::addAudioVolumeGroupCallback( |
| Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 2629 | const sp<AudioVolumeGroupCallback>& callback) { |
| François Gaffie | cfe1732 | 2018-11-07 13:41:29 +0100 | [diff] [blame] | 2630 | Mutex::Autolock _l(mLock); |
| 2631 | for (size_t i = 0; i < mAudioVolumeGroupCallback.size(); i++) { |
| 2632 | if (mAudioVolumeGroupCallback[i] == callback) { |
| 2633 | return -1; |
| 2634 | } |
| 2635 | } |
| 2636 | mAudioVolumeGroupCallback.add(callback); |
| 2637 | return mAudioVolumeGroupCallback.size(); |
| 2638 | } |
| 2639 | |
| 2640 | int AudioSystem::AudioPolicyServiceClient::removeAudioVolumeGroupCallback( |
| Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 2641 | const sp<AudioVolumeGroupCallback>& callback) { |
| François Gaffie | cfe1732 | 2018-11-07 13:41:29 +0100 | [diff] [blame] | 2642 | Mutex::Autolock _l(mLock); |
| 2643 | size_t i; |
| 2644 | for (i = 0; i < mAudioVolumeGroupCallback.size(); i++) { |
| 2645 | if (mAudioVolumeGroupCallback[i] == callback) { |
| 2646 | break; |
| 2647 | } |
| 2648 | } |
| 2649 | if (i == mAudioVolumeGroupCallback.size()) { |
| 2650 | return -1; |
| 2651 | } |
| 2652 | mAudioVolumeGroupCallback.removeAt(i); |
| 2653 | return mAudioVolumeGroupCallback.size(); |
| 2654 | } |
| 2655 | |
| Ytai Ben-Tsvi | 7e7a79d | 2020-12-15 16:48:16 -0800 | [diff] [blame] | 2656 | Status AudioSystem::AudioPolicyServiceClient::onAudioVolumeGroupChanged(int32_t group, |
| 2657 | int32_t flags) { |
| 2658 | volume_group_t groupLegacy = VALUE_OR_RETURN_BINDER_STATUS( |
| 2659 | aidl2legacy_int32_t_volume_group_t(group)); |
| 2660 | int flagsLegacy = VALUE_OR_RETURN_BINDER_STATUS(convertReinterpret<int>(flags)); |
| 2661 | |
| François Gaffie | cfe1732 | 2018-11-07 13:41:29 +0100 | [diff] [blame] | 2662 | Mutex::Autolock _l(mLock); |
| 2663 | for (size_t i = 0; i < mAudioVolumeGroupCallback.size(); i++) { |
| Ytai Ben-Tsvi | 7e7a79d | 2020-12-15 16:48:16 -0800 | [diff] [blame] | 2664 | mAudioVolumeGroupCallback[i]->onAudioVolumeGroupChanged(groupLegacy, flagsLegacy); |
| François Gaffie | cfe1732 | 2018-11-07 13:41:29 +0100 | [diff] [blame] | 2665 | } |
| Ytai Ben-Tsvi | 7e7a79d | 2020-12-15 16:48:16 -0800 | [diff] [blame] | 2666 | return Status::ok(); |
| François Gaffie | cfe1732 | 2018-11-07 13:41:29 +0100 | [diff] [blame] | 2667 | } |
| 2668 | // ---------------------------------------------------------------------------- |
| 2669 | |
| Ytai Ben-Tsvi | 7e7a79d | 2020-12-15 16:48:16 -0800 | [diff] [blame] | 2670 | Status AudioSystem::AudioPolicyServiceClient::onDynamicPolicyMixStateUpdate( |
| 2671 | const ::std::string& regId, int32_t state) { |
| 2672 | ALOGV("AudioPolicyServiceClient::onDynamicPolicyMixStateUpdate(%s, %d)", regId.c_str(), state); |
| 2673 | |
| 2674 | String8 regIdLegacy = VALUE_OR_RETURN_BINDER_STATUS(aidl2legacy_string_view_String8(regId)); |
| 2675 | int stateLegacy = VALUE_OR_RETURN_BINDER_STATUS(convertReinterpret<int>(state)); |
| Jean-Michel Trivi | f613d42 | 2015-04-23 18:41:29 -0700 | [diff] [blame] | 2676 | dynamic_policy_callback cb = NULL; |
| 2677 | { |
| 2678 | Mutex::Autolock _l(AudioSystem::gLock); |
| 2679 | cb = gDynPolicyCallback; |
| 2680 | } |
| 2681 | |
| 2682 | if (cb != NULL) { |
| Ytai Ben-Tsvi | 7e7a79d | 2020-12-15 16:48:16 -0800 | [diff] [blame] | 2683 | cb(DYNAMIC_POLICY_EVENT_MIX_STATE_UPDATE, regIdLegacy, stateLegacy); |
| Jean-Michel Trivi | f613d42 | 2015-04-23 18:41:29 -0700 | [diff] [blame] | 2684 | } |
| Ytai Ben-Tsvi | 7e7a79d | 2020-12-15 16:48:16 -0800 | [diff] [blame] | 2685 | return Status::ok(); |
| Jean-Michel Trivi | de80105 | 2015-04-14 19:10:14 -0700 | [diff] [blame] | 2686 | } |
| 2687 | |
| Ytai Ben-Tsvi | 7e7a79d | 2020-12-15 16:48:16 -0800 | [diff] [blame] | 2688 | Status AudioSystem::AudioPolicyServiceClient::onRecordingConfigurationUpdate( |
| 2689 | int32_t event, |
| 2690 | const media::RecordClientInfo& clientInfo, |
| Mikhail Naganov | dbf0364 | 2021-08-25 18:15:32 -0700 | [diff] [blame] | 2691 | const AudioConfigBase& clientConfig, |
| Ytai Ben-Tsvi | 7e7a79d | 2020-12-15 16:48:16 -0800 | [diff] [blame] | 2692 | const std::vector<media::EffectDescriptor>& clientEffects, |
| Mikhail Naganov | dbf0364 | 2021-08-25 18:15:32 -0700 | [diff] [blame] | 2693 | const AudioConfigBase& deviceConfig, |
| Ytai Ben-Tsvi | 7e7a79d | 2020-12-15 16:48:16 -0800 | [diff] [blame] | 2694 | const std::vector<media::EffectDescriptor>& effects, |
| 2695 | int32_t patchHandle, |
| Mikhail Naganov | ddceecc | 2021-09-03 13:58:56 -0700 | [diff] [blame] | 2696 | AudioSource source) { |
| Jean-Michel Trivi | 2f4fe9f | 2015-12-04 16:20:59 -0800 | [diff] [blame] | 2697 | record_config_callback cb = NULL; |
| 2698 | { |
| 2699 | Mutex::Autolock _l(AudioSystem::gLock); |
| 2700 | cb = gRecordConfigCallback; |
| 2701 | } |
| 2702 | |
| 2703 | if (cb != NULL) { |
| Ytai Ben-Tsvi | 7e7a79d | 2020-12-15 16:48:16 -0800 | [diff] [blame] | 2704 | int eventLegacy = VALUE_OR_RETURN_BINDER_STATUS(convertReinterpret<int>(event)); |
| 2705 | record_client_info_t clientInfoLegacy = VALUE_OR_RETURN_BINDER_STATUS( |
| 2706 | aidl2legacy_RecordClientInfo_record_client_info_t(clientInfo)); |
| 2707 | audio_config_base_t clientConfigLegacy = VALUE_OR_RETURN_BINDER_STATUS( |
| Mikhail Naganov | de3fa18 | 2021-07-30 15:06:42 -0700 | [diff] [blame] | 2708 | aidl2legacy_AudioConfigBase_audio_config_base_t(clientConfig, true /*isInput*/)); |
| Ytai Ben-Tsvi | 7e7a79d | 2020-12-15 16:48:16 -0800 | [diff] [blame] | 2709 | std::vector<effect_descriptor_t> clientEffectsLegacy = VALUE_OR_RETURN_BINDER_STATUS( |
| 2710 | convertContainer<std::vector<effect_descriptor_t>>( |
| 2711 | clientEffects, |
| 2712 | aidl2legacy_EffectDescriptor_effect_descriptor_t)); |
| 2713 | audio_config_base_t deviceConfigLegacy = VALUE_OR_RETURN_BINDER_STATUS( |
| Mikhail Naganov | de3fa18 | 2021-07-30 15:06:42 -0700 | [diff] [blame] | 2714 | aidl2legacy_AudioConfigBase_audio_config_base_t(deviceConfig, true /*isInput*/)); |
| Ytai Ben-Tsvi | 7e7a79d | 2020-12-15 16:48:16 -0800 | [diff] [blame] | 2715 | std::vector<effect_descriptor_t> effectsLegacy = VALUE_OR_RETURN_BINDER_STATUS( |
| 2716 | convertContainer<std::vector<effect_descriptor_t>>( |
| 2717 | effects, |
| 2718 | aidl2legacy_EffectDescriptor_effect_descriptor_t)); |
| 2719 | audio_patch_handle_t patchHandleLegacy = VALUE_OR_RETURN_BINDER_STATUS( |
| 2720 | aidl2legacy_int32_t_audio_patch_handle_t(patchHandle)); |
| 2721 | audio_source_t sourceLegacy = VALUE_OR_RETURN_BINDER_STATUS( |
| Mikhail Naganov | ddceecc | 2021-09-03 13:58:56 -0700 | [diff] [blame] | 2722 | aidl2legacy_AudioSource_audio_source_t(source)); |
| Ytai Ben-Tsvi | 7e7a79d | 2020-12-15 16:48:16 -0800 | [diff] [blame] | 2723 | cb(eventLegacy, &clientInfoLegacy, &clientConfigLegacy, clientEffectsLegacy, |
| 2724 | &deviceConfigLegacy, effectsLegacy, patchHandleLegacy, sourceLegacy); |
| Jean-Michel Trivi | 2f4fe9f | 2015-12-04 16:20:59 -0800 | [diff] [blame] | 2725 | } |
| Ytai Ben-Tsvi | 7e7a79d | 2020-12-15 16:48:16 -0800 | [diff] [blame] | 2726 | return Status::ok(); |
| Jean-Michel Trivi | 2f4fe9f | 2015-12-04 16:20:59 -0800 | [diff] [blame] | 2727 | } |
| 2728 | |
| Jean-Michel Trivi | 9a6b9ad | 2020-10-22 16:46:43 -0700 | [diff] [blame] | 2729 | Status AudioSystem::AudioPolicyServiceClient::onRoutingUpdated() { |
| 2730 | routing_callback cb = NULL; |
| 2731 | { |
| 2732 | Mutex::Autolock _l(AudioSystem::gLock); |
| 2733 | cb = gRoutingCallback; |
| 2734 | } |
| 2735 | |
| 2736 | if (cb != NULL) { |
| 2737 | cb(); |
| 2738 | } |
| 2739 | return Status::ok(); |
| 2740 | } |
| 2741 | |
| Jean-Michel Trivi | 78f2b30 | 2022-04-15 18:18:41 +0000 | [diff] [blame] | 2742 | Status AudioSystem::AudioPolicyServiceClient::onVolumeRangeInitRequest() { |
| 2743 | vol_range_init_req_callback cb = NULL; |
| 2744 | { |
| 2745 | Mutex::Autolock _l(AudioSystem::gLock); |
| 2746 | cb = gVolRangeInitReqCallback; |
| 2747 | } |
| 2748 | |
| 2749 | if (cb != NULL) { |
| 2750 | cb(); |
| 2751 | } |
| 2752 | return Status::ok(); |
| 2753 | } |
| 2754 | |
| Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 2755 | void AudioSystem::AudioPolicyServiceClient::binderDied(const wp<IBinder>& who __unused) { |
| Glenn Kasten | d2d089f | 2014-11-05 11:48:12 -0800 | [diff] [blame] | 2756 | { |
| Eric Laurent | b28753e | 2015-04-01 13:06:28 -0700 | [diff] [blame] | 2757 | Mutex::Autolock _l(mLock); |
| 2758 | for (size_t i = 0; i < mAudioPortCallbacks.size(); i++) { |
| 2759 | mAudioPortCallbacks[i]->onServiceDied(); |
| Glenn Kasten | d2d089f | 2014-11-05 11:48:12 -0800 | [diff] [blame] | 2760 | } |
| François Gaffie | cfe1732 | 2018-11-07 13:41:29 +0100 | [diff] [blame] | 2761 | for (size_t i = 0; i < mAudioVolumeGroupCallback.size(); i++) { |
| 2762 | mAudioVolumeGroupCallback[i]->onServiceDied(); |
| 2763 | } |
| Eric Laurent | b52c152 | 2014-05-20 11:27:36 -0700 | [diff] [blame] | 2764 | } |
| Mikhail Naganov | ec3d579 | 2022-05-06 00:19:55 +0000 | [diff] [blame] | 2765 | AudioSystem::clearAudioPolicyService(); |
| Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 2766 | |
| Steve Block | 5ff1dd5 | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 2767 | ALOGW("AudioPolicyService server died!"); |
| Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 2768 | } |
| 2769 | |
| Ytai Ben-Tsvi | 7e7a79d | 2020-12-15 16:48:16 -0800 | [diff] [blame] | 2770 | ConversionResult<record_client_info_t> |
| 2771 | aidl2legacy_RecordClientInfo_record_client_info_t(const media::RecordClientInfo& aidl) { |
| 2772 | record_client_info_t legacy; |
| 2773 | legacy.riid = VALUE_OR_RETURN(aidl2legacy_int32_t_audio_unique_id_t(aidl.riid)); |
| 2774 | legacy.uid = VALUE_OR_RETURN(aidl2legacy_int32_t_uid_t(aidl.uid)); |
| 2775 | legacy.session = VALUE_OR_RETURN(aidl2legacy_int32_t_audio_session_t(aidl.session)); |
| Mikhail Naganov | ddceecc | 2021-09-03 13:58:56 -0700 | [diff] [blame] | 2776 | legacy.source = VALUE_OR_RETURN(aidl2legacy_AudioSource_audio_source_t(aidl.source)); |
| Ytai Ben-Tsvi | 7e7a79d | 2020-12-15 16:48:16 -0800 | [diff] [blame] | 2777 | legacy.port_id = VALUE_OR_RETURN(aidl2legacy_int32_t_audio_port_handle_t(aidl.portId)); |
| 2778 | legacy.silenced = aidl.silenced; |
| 2779 | return legacy; |
| 2780 | } |
| 2781 | |
| 2782 | ConversionResult<media::RecordClientInfo> |
| 2783 | legacy2aidl_record_client_info_t_RecordClientInfo(const record_client_info_t& legacy) { |
| 2784 | media::RecordClientInfo aidl; |
| 2785 | aidl.riid = VALUE_OR_RETURN(legacy2aidl_audio_unique_id_t_int32_t(legacy.riid)); |
| 2786 | aidl.uid = VALUE_OR_RETURN(legacy2aidl_uid_t_int32_t(legacy.uid)); |
| 2787 | aidl.session = VALUE_OR_RETURN(legacy2aidl_audio_session_t_int32_t(legacy.session)); |
| Mikhail Naganov | ddceecc | 2021-09-03 13:58:56 -0700 | [diff] [blame] | 2788 | aidl.source = VALUE_OR_RETURN(legacy2aidl_audio_source_t_AudioSource(legacy.source)); |
| Ytai Ben-Tsvi | 7e7a79d | 2020-12-15 16:48:16 -0800 | [diff] [blame] | 2789 | aidl.portId = VALUE_OR_RETURN(legacy2aidl_audio_port_handle_t_int32_t(legacy.port_id)); |
| 2790 | aidl.silenced = legacy.silenced; |
| 2791 | return aidl; |
| 2792 | } |
| 2793 | |
| Glenn Kasten | 40bc906 | 2015-03-20 09:09:33 -0700 | [diff] [blame] | 2794 | } // namespace android |