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