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