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