blob: c675c343c923de79f365d2dd44708890a0e4625c [file] [log] [blame]
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001/*
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-Tsvi5120eda2020-03-26 09:41:15 -070021
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -080022#include <android/media/IAudioPolicyService.h>
Jan Sebechlebsky0af8e872023-08-11 14:45:08 +020023#include <android/media/AudioMixUpdate.h>
Ytai Ben-Tsvi5120eda2020-03-26 09:41:15 -070024#include <android/media/BnCaptureStateListener.h>
Marvin Raminbdefaf02023-11-01 09:10:32 +010025#include <android_media_audiopolicy.h>
Mathias Agopian75624082009-05-19 19:08:10 -070026#include <binder/IServiceManager.h>
Eric Laurentfb00fc72017-05-25 18:17:12 -070027#include <binder/ProcessState.h>
François Gaffie24437602018-04-23 15:08:59 +020028#include <binder/IPCThreadState.h>
Andy Hungce3d1102024-09-29 21:47:19 -070029#include <cutils/properties.h>
Ytai Ben-Tsvi10dc0a62020-09-18 11:31:55 -070030#include <media/AidlConversion.h>
Eric Laurent21da6472017-11-09 16:29:26 -080031#include <media/AudioResamplerPublic.h>
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080032#include <media/AudioSystem.h>
Glenn Kasten1ab85ec2013-05-31 09:18:43 -070033#include <media/IAudioFlinger.h>
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -080034#include <media/PolicyAidlConversion.h>
François Gaffied0ba9ed2018-11-05 11:50:42 +010035#include <media/TypeConverter.h>
Andy Hungce3d1102024-09-29 21:47:19 -070036#include <mediautils/ServiceSingleton.h>
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080037#include <math.h>
Andy Hungd6346072024-11-25 15:23:40 -080038#include <private/android_filesystem_config.h>
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080039
Dima Zavin64760242011-05-11 14:15:23 -070040#include <system/audio.h>
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -080041#include <android/media/GetInputForAttrResponse.h>
jiabina84c3d32022-12-02 18:59:55 +000042#include <android/media/AudioMixerAttributesInternal.h>
Dima Zavinfce7a472011-04-19 22:30:36 -070043
Ytai Ben-Tsvi1ff75692020-11-06 12:16:12 -080044#define VALUE_OR_RETURN_BINDER_STATUS(x) \
Ytai Ben-Tsvi10dc0a62020-09-18 11:31:55 -070045 ({ auto _tmp = (x); \
Andy Hung1131b6e2020-12-08 20:47:45 -080046 if (!_tmp.ok()) return aidl_utils::binderStatusFromStatusT(_tmp.error()); \
Ytai Ben-Tsvia3815202020-10-28 14:58:08 -070047 std::move(_tmp.value()); })
Ytai Ben-Tsvi10dc0a62020-09-18 11:31:55 -070048
Eric Laurentc2f1f072009-07-17 12:17:14 -070049// ----------------------------------------------------------------------------
Eric Laurentc2f1f072009-07-17 12:17:14 -070050
Marvin Raminbdefaf02023-11-01 09:10:32 +010051namespace audio_flags = android::media::audiopolicy;
52
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080053namespace android {
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -080054using aidl_utils::statusTFromBinderStatus;
Ytai Ben-Tsvi10dc0a62020-09-18 11:31:55 -070055using binder::Status;
Mikhail Naganov57bd06f2021-08-10 16:41:54 -070056using content::AttributionSourceState;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -080057using media::IAudioPolicyService;
Mikhail Naganovdbf03642021-08-25 18:15:32 -070058using media::audio::common::AudioConfig;
59using media::audio::common::AudioConfigBase;
Mikhail Naganovf4a75362021-09-16 00:02:54 +000060using media::audio::common::AudioDevice;
61using media::audio::common::AudioDeviceDescription;
Mikhail Naganov57bd06f2021-08-10 16:41:54 -070062using media::audio::common::AudioFormatDescription;
jiabine99d0882021-09-17 05:21:25 +000063using media::audio::common::AudioMMapPolicyInfo;
64using media::audio::common::AudioMMapPolicyType;
Mikhail Naganovdbf03642021-08-25 18:15:32 -070065using media::audio::common::AudioOffloadInfo;
Mikhail Naganovddceecc2021-09-03 13:58:56 -070066using media::audio::common::AudioSource;
Mikhail Naganovdbf03642021-08-25 18:15:32 -070067using media::audio::common::AudioStreamType;
68using media::audio::common::AudioUsage;
Mikhail Naganov0078ee52021-09-30 23:06:20 +000069using media::audio::common::Int;
Ytai Ben-Tsvi10dc0a62020-09-18 11:31:55 -070070
Andy Hungacd955a2024-02-16 17:14:36 -080071std::mutex AudioSystem::gMutex;
Jean-Michel Trivif613d422015-04-23 18:41:29 -070072dynamic_policy_callback AudioSystem::gDynPolicyCallback = NULL;
Svet Ganovf4ddfef2018-01-16 07:37:58 -080073record_config_callback AudioSystem::gRecordConfigCallback = NULL;
Jean-Michel Trivi9a6b9ad2020-10-22 16:46:43 -070074routing_callback AudioSystem::gRoutingCallback = NULL;
Jean-Michel Trivi78f2b302022-04-15 18:18:41 +000075vol_range_init_req_callback AudioSystem::gVolRangeInitReqCallback = NULL;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080076
Andy Hung88e71732024-02-21 16:44:27 -080077std::mutex AudioSystem::gApsCallbackMutex;
Andy Hungacd955a2024-02-16 17:14:36 -080078std::mutex AudioSystem::gErrorCallbacksMutex;
79std::set<audio_error_callback> AudioSystem::gAudioErrorCallbacks;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -080080
Andy Hungacd955a2024-02-16 17:14:36 -080081std::mutex AudioSystem::gSoundTriggerMutex;
82sp<CaptureStateListenerImpl> AudioSystem::gSoundTriggerCaptureStateListener;
Ytai Ben-Tsvi5120eda2020-03-26 09:41:15 -070083
Andy Hungce3d1102024-09-29 21:47:19 -070084// ----------------------------
85
86// AudioSystem is the client side interface to AudioFlinger (AF) and AudioPolicy (AP).
87//
88// For clients:
89// We use the ServiceSingleton class in mediautils to fetch the AF/AP service.
90// The ServiceSingleton offers service prefetch, automatic
91// new service notification, automatic binder death notification.
92//
93// AudioFlingerServiceTraits and AudioPolicyServiceTraits are passed into
94// ServiceSingleton to provide interaction with the service notifications and
95// binder death notifications.
96//
Andy Hunga5af5072024-11-11 16:15:10 -080097// If the AF/AP service is unavailable for kServiceClientWaitMs from ServiceManager,
Andy Hungce3d1102024-09-29 21:47:19 -070098// ServiceSingleton will return a nullptr service handle resulting in the same dead object error
99// as if the service died (which it did, otherwise we'd be returning the cached handle).
100//
101// Potential deadlock sequence:
102// 1) audioserver reboots.
103// 2) App clients call into AudioService (system server) obtaining binder threads,
104// these calls blocking for audioserver reboot completion (or waiting for a mutex
105// held by those blocked threads).
106// 3) AudioFlinger and AudioPolicyManager services need to call into system server
107// during initialization. It can't because app clients hold all the binder threads
108// in the threadpool.
109// 4) We have a resource deadlock between (2) and (3) potentially causing an ANR and
110// further reinitialization.
111// 5) However, after the service wait timeout kServiceWaitNs, the calls for (2) will
112// return an error and resolve itself, breaking the resource deadlock in (4).
113//
114// At this time, it is a matter of experimentation whether the service timeout is
115// applied only for system server, and we let other clients block indefinitely.
116//
117// For audio services:
118// AudioFlinger and AudioPolicy may call back into AudioSystem. When doing
119// so it should not hold any mutexes. There is no service wait as AudioFlinger
120// and AudioPolicy are in-process with each other, and the call proceeds without
121// binder. The setLocalService() method is used to set the service interfaces
122// within audioserver to bypass the ServiceManager lookup.
123//
124
125// Wait timeout for AudioFlinger or AudioPolicy service before returning with null.
126// Such an audioserver failure is considered benign as the ground truth is stored in
127// the Java AudioService and can be restored once audioserver has finished initialization.
128//
Andy Hunga2fb29c2024-11-11 15:56:41 -0800129// TODO(b/375691003) We use 5s as a conservative timeout value, and will tune closer to 3s.
Andy Hungce3d1102024-09-29 21:47:19 -0700130// Too small a value (i.e. less than 1s would churn repeated calls to get the service).
Andy Hunga5af5072024-11-11 16:15:10 -0800131// The value can be tuned by the property audio.service.client_wait_ms.
Andy Hunga2fb29c2024-11-11 15:56:41 -0800132static constexpr int32_t kServiceClientWaitMs = 5'000;
Andy Hungce3d1102024-09-29 21:47:19 -0700133
Andy Hunga5af5072024-11-11 16:15:10 -0800134static constexpr const char kServiceWaitProperty[] = "audio.service.client_wait_ms";
Andy Hungce3d1102024-09-29 21:47:19 -0700135
136// AudioFlingerServiceTraits is a collection of methods that parameterize the
137// ServiceSingleton handler for IAudioFlinger
138
139class AudioFlingerServiceTraits {
Andy Hungda1fb072024-02-20 19:08:08 -0800140public:
Andy Hungce3d1102024-09-29 21:47:19 -0700141 // ------- required by ServiceSingleton
Ahaan Ugale058f94b2021-05-21 02:23:50 -0700142
Andy Hungce3d1102024-09-29 21:47:19 -0700143 static constexpr const char* getServiceName() { return "media.audio_flinger"; }
144
145 static void onNewService(const sp<media::IAudioFlingerService>& afs) {
146 onNewServiceWithAdapter(createServiceAdapter(afs));
147 }
148
Andy Hunge20145d2024-11-25 16:22:37 -0800149 static void onServiceDied(const sp<media::IAudioFlingerService>& service) {
150 ALOGW("%s: %s service died %p", __func__, getServiceName(), service.get());
Andy Hungda1fb072024-02-20 19:08:08 -0800151 {
Andy Hungce3d1102024-09-29 21:47:19 -0700152 std::lock_guard l(mMutex);
Andy Hunge20145d2024-11-25 16:22:37 -0800153 if (!mValid) {
154 ALOGW("%s: %s service already invalidated, ignoring", __func__, getServiceName());
155 return;
156 }
157 if (!mService || mService->getDelegate() != service) {
158 ALOGW("%s: %s unmatched service death pointers, ignoring",
159 __func__, getServiceName());
160 return;
161 }
Andy Hungce3d1102024-09-29 21:47:19 -0700162 mValid = false;
Andy Hungc4f7d872024-11-20 20:29:32 -0800163 if (mClient) {
164 mClient->clearIoCache();
165 } else {
166 ALOGW("%s: null client", __func__);
167 }
Andy Hung66fc0bd2023-01-04 11:45:50 -0800168 }
Andy Hungce3d1102024-09-29 21:47:19 -0700169 AudioSystem::reportError(DEAD_OBJECT);
170 }
Andy Hung66fc0bd2023-01-04 11:45:50 -0800171
Andy Hungce3d1102024-09-29 21:47:19 -0700172 static constexpr mediautils::ServiceOptions options() {
173 return mediautils::ServiceOptions::kNone;
174 }
Andy Hung66fc0bd2023-01-04 11:45:50 -0800175
Andy Hungce3d1102024-09-29 21:47:19 -0700176 // ------- required by AudioSystem
Andy Hungda1fb072024-02-20 19:08:08 -0800177
Andy Hungce3d1102024-09-29 21:47:19 -0700178 static sp<IAudioFlinger> getService(
179 std::chrono::milliseconds waitMs = std::chrono::milliseconds{-1}) {
180 static bool init = false;
181 audio_utils::unique_lock ul(mMutex);
182 if (!init) {
183 if (!mDisableThreadPoolStart) {
184 ProcessState::self()->startThreadPool();
Ahaan Ugale058f94b2021-05-21 02:23:50 -0700185 }
Andy Hungd6346072024-11-25 15:23:40 -0800186 if (multiuser_get_app_id(getuid()) == AID_AUDIOSERVER) {
187 mediautils::skipService<media::IAudioFlingerService>(mediautils::SkipMode::kWait);
188 mWaitMs = std::chrono::milliseconds(INT32_MAX);
189 } else {
190 mediautils::initService<media::IAudioFlingerService, AudioFlingerServiceTraits>();
191 mWaitMs = std::chrono::milliseconds(
192 property_get_int32(kServiceWaitProperty, kServiceClientWaitMs));
193 }
Andy Hungce3d1102024-09-29 21:47:19 -0700194 init = true;
Glenn Kastene53b9ea2012-03-12 16:29:55 -0700195 }
Andy Hungce3d1102024-09-29 21:47:19 -0700196 if (mValid) return mService;
197 if (waitMs.count() < 0) waitMs = mWaitMs;
Andy Hungda1fb072024-02-20 19:08:08 -0800198 ul.unlock();
Andy Hungce3d1102024-09-29 21:47:19 -0700199
200 // mediautils::getService() installs a persistent new service notification.
201 auto service = mediautils::getService<
202 media::IAudioFlingerService>(waitMs);
203 ALOGD("%s: checking for service %s: %p", __func__, getServiceName(), service.get());
204
205 ul.lock();
206 // return the IAudioFlinger interface which is adapted
207 // from the media::IAudioFlingerService.
208 return mService;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800209 }
Andy Hungda1fb072024-02-20 19:08:08 -0800210
Andy Hungce3d1102024-09-29 21:47:19 -0700211 static sp<AudioSystem::AudioFlingerClient> getClient() {
212 audio_utils::unique_lock ul(mMutex);
213 if (mValid) return mClient;
214 ul.unlock();
Andy Hungda1fb072024-02-20 19:08:08 -0800215
Andy Hungce3d1102024-09-29 21:47:19 -0700216 auto service = getService();
217 ALOGD("%s: checking for service: %p", __func__, service.get());
218
219 ul.lock();
Andy Hungda1fb072024-02-20 19:08:08 -0800220 return mClient;
221 }
222
Andy Hungce3d1102024-09-29 21:47:19 -0700223 static void setBinder(const sp<IBinder>& binder) {
224 setLocalService(createServiceAdapter(
225 mediautils::interfaceFromBinder<media::IAudioFlingerService>(binder)));
226 }
227
228 static status_t setLocalService(const sp<IAudioFlinger>& af) {
229 mediautils::skipService<media::IAudioFlingerService>();
230 sp<IAudioFlinger> old;
Andy Hunge20145d2024-11-25 16:22:37 -0800231
232 audio_utils::unique_lock ul(mMutex);
233 old = mService;
234 if (old) {
235 ul.unlock();
236 onServiceDied(old->getDelegate());
237 ul.lock();
238 ALOGW_IF(old != mService,
239 "%s: service changed during callback, continuing.", __func__);
Andy Hungda1fb072024-02-20 19:08:08 -0800240 }
Andy Hunge20145d2024-11-25 16:22:37 -0800241 mService = af;
242 ul.unlock();
Andy Hungce3d1102024-09-29 21:47:19 -0700243 if (af) onNewServiceWithAdapter(af);
244 return OK;
Andy Hungda1fb072024-02-20 19:08:08 -0800245 }
246
Andy Hungce3d1102024-09-29 21:47:19 -0700247 static void disableThreadPoolStart() {
248 mDisableThreadPoolStart = true;
Andy Hungda1fb072024-02-20 19:08:08 -0800249 }
250
Andy Hungce3d1102024-09-29 21:47:19 -0700251 static bool isValid() {
252 audio_utils::unique_lock ul(mMutex);
253 if (mValid) return true;
254 ul.unlock();
255 (void)getService({});
256 ul.lock();
257 return mValid;
258 }
259
260 // called to determine error on nullptr service return.
261 static constexpr status_t getError() {
262 return DEAD_OBJECT;
Shraddha Basantwanif2aa2502024-04-08 11:01:57 +0000263 }
264
Andy Hungda1fb072024-02-20 19:08:08 -0800265private:
Andy Hungda1fb072024-02-20 19:08:08 -0800266
Andy Hungce3d1102024-09-29 21:47:19 -0700267 static void onNewServiceWithAdapter(const sp<IAudioFlinger>& service) {
268 ALOGD("%s: %s service obtained %p", __func__, getServiceName(), service.get());
269 sp<AudioSystem::AudioFlingerClient> client;
270 bool reportNoError = false;
271 {
272 std::lock_guard l(mMutex);
Andy Hunge20145d2024-11-25 16:22:37 -0800273 ALOGW_IF(mValid, "%s: %s service already valid, continuing with initialization",
274 __func__, getServiceName());
Andy Hungce3d1102024-09-29 21:47:19 -0700275 if (mClient == nullptr) {
276 mClient = sp<AudioSystem::AudioFlingerClient>::make();
277 } else {
278 mClient->clearIoCache();
279 reportNoError = true;
280 }
281 mService = service;
282 client = mClient;
283 mValid = true;
284 }
285 // TODO(b/375280520) consider registerClient() within mMutex lock.
Andy Hungda1fb072024-02-20 19:08:08 -0800286 const int64_t token = IPCThreadState::self()->clearCallingIdentity();
Andy Hungce3d1102024-09-29 21:47:19 -0700287 service->registerClient(client);
Andy Hungda1fb072024-02-20 19:08:08 -0800288 IPCThreadState::self()->restoreCallingIdentity(token);
Andy Hungce3d1102024-09-29 21:47:19 -0700289
290 if (reportNoError) AudioSystem::reportError(NO_ERROR);
Andy Hungda1fb072024-02-20 19:08:08 -0800291 }
292
293 static sp<IAudioFlinger> createServiceAdapter(
Andy Hungce3d1102024-09-29 21:47:19 -0700294 const sp<media::IAudioFlingerService>& af) {
295 return sp<AudioFlingerClientAdapter>::make(af);
Andy Hungda1fb072024-02-20 19:08:08 -0800296 }
297
Andy Hungce3d1102024-09-29 21:47:19 -0700298 static inline constinit std::mutex mMutex;
299 static inline constinit sp<AudioSystem::AudioFlingerClient> mClient GUARDED_BY(mMutex);
300 static inline constinit sp<IAudioFlinger> mService GUARDED_BY(mMutex);
Andy Hunga5af5072024-11-11 16:15:10 -0800301 static inline constinit std::chrono::milliseconds mWaitMs
302 GUARDED_BY(mMutex) {kServiceClientWaitMs};
Andy Hungce3d1102024-09-29 21:47:19 -0700303 static inline constinit bool mValid GUARDED_BY(mMutex) = false;
304 static inline constinit std::atomic_bool mDisableThreadPoolStart = false;
Andy Hungda1fb072024-02-20 19:08:08 -0800305};
306
Andy Hung264fa4c2024-02-21 15:52:12 -0800307sp<IAudioFlinger> AudioSystem::get_audio_flinger() {
Andy Hungce3d1102024-09-29 21:47:19 -0700308 return AudioFlingerServiceTraits::getService();
Akshata Kadam1d486b72023-08-07 05:49:48 +0000309}
310
Andy Hungda1fb072024-02-20 19:08:08 -0800311sp<AudioSystem::AudioFlingerClient> AudioSystem::getAudioFlingerClient() {
Andy Hungce3d1102024-09-29 21:47:19 -0700312 return AudioFlingerServiceTraits::getClient();
Andy Hungda1fb072024-02-20 19:08:08 -0800313}
314
315void AudioSystem::setAudioFlingerBinder(const sp<IBinder>& audioFlinger) {
Andy Hungce3d1102024-09-29 21:47:19 -0700316 AudioFlingerServiceTraits::setBinder(audioFlinger);
Andy Hungda1fb072024-02-20 19:08:08 -0800317}
318
319status_t AudioSystem::setLocalAudioFlinger(const sp<IAudioFlinger>& af) {
Andy Hungce3d1102024-09-29 21:47:19 -0700320 return AudioFlingerServiceTraits::setLocalService(af);
Eric Laurent296fb132015-05-01 11:38:42 -0700321}
322
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800323sp<AudioIoDescriptor> AudioSystem::getIoDescriptor(audio_io_handle_t ioHandle) {
Eric Laurent296fb132015-05-01 11:38:42 -0700324 sp<AudioIoDescriptor> desc;
325 const sp<AudioFlingerClient> afc = getAudioFlingerClient();
326 if (afc != 0) {
327 desc = afc->getIoDescriptor(ioHandle);
328 }
329 return desc;
330}
331
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800332/* static */ status_t AudioSystem::checkAudioFlinger() {
Andy Hungce3d1102024-09-29 21:47:19 -0700333 if (AudioFlingerServiceTraits::isValid()) return OK;
Eric Laurent46291612013-07-18 14:38:44 -0700334 return DEAD_OBJECT;
335}
336
Glenn Kasten4a8308b2016-04-18 14:10:01 -0700337// FIXME Declare in binder opcode order, similarly to IAudioFlinger.h and IAudioFlinger.cpp
338
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800339status_t AudioSystem::muteMicrophone(bool state) {
Andy Hung264fa4c2024-02-21 15:52:12 -0800340 const sp<IAudioFlinger> af = get_audio_flinger();
Andy Hungce3d1102024-09-29 21:47:19 -0700341 if (af == nullptr) return AudioFlingerServiceTraits::getError();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800342 return af->setMicMute(state);
343}
344
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800345status_t AudioSystem::isMicrophoneMuted(bool* state) {
Andy Hung264fa4c2024-02-21 15:52:12 -0800346 const sp<IAudioFlinger> af = get_audio_flinger();
Andy Hungce3d1102024-09-29 21:47:19 -0700347 if (af == nullptr) return AudioFlingerServiceTraits::getError();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800348 *state = af->getMicMute();
349 return NO_ERROR;
350}
351
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800352status_t AudioSystem::setMasterVolume(float value) {
Andy Hung264fa4c2024-02-21 15:52:12 -0800353 const sp<IAudioFlinger> af = get_audio_flinger();
Andy Hungce3d1102024-09-29 21:47:19 -0700354 if (af == nullptr) return AudioFlingerServiceTraits::getError();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800355 af->setMasterVolume(value);
356 return NO_ERROR;
357}
358
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800359status_t AudioSystem::setMasterMute(bool mute) {
Andy Hung264fa4c2024-02-21 15:52:12 -0800360 const sp<IAudioFlinger> af = get_audio_flinger();
Andy Hungce3d1102024-09-29 21:47:19 -0700361 if (af == nullptr) return AudioFlingerServiceTraits::getError();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800362 af->setMasterMute(mute);
363 return NO_ERROR;
364}
365
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800366status_t AudioSystem::getMasterVolume(float* volume) {
Andy Hung264fa4c2024-02-21 15:52:12 -0800367 const sp<IAudioFlinger> af = get_audio_flinger();
Andy Hungce3d1102024-09-29 21:47:19 -0700368 if (af == nullptr) return AudioFlingerServiceTraits::getError();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800369 *volume = af->masterVolume();
370 return NO_ERROR;
371}
372
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800373status_t AudioSystem::getMasterMute(bool* mute) {
Andy Hung264fa4c2024-02-21 15:52:12 -0800374 const sp<IAudioFlinger> af = get_audio_flinger();
Andy Hungce3d1102024-09-29 21:47:19 -0700375 if (af == nullptr) return AudioFlingerServiceTraits::getError();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800376 *mute = af->masterMute();
377 return NO_ERROR;
378}
379
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800380status_t AudioSystem::setStreamVolume(audio_stream_type_t stream, float value,
Vlad Popa1e865e62024-08-15 19:11:42 -0700381 bool muted, audio_io_handle_t output) {
Dima Zavinfce7a472011-04-19 22:30:36 -0700382 if (uint32_t(stream) >= AUDIO_STREAM_CNT) return BAD_VALUE;
Andy Hung264fa4c2024-02-21 15:52:12 -0800383 const sp<IAudioFlinger> af = get_audio_flinger();
Andy Hungce3d1102024-09-29 21:47:19 -0700384 if (af == nullptr) return AudioFlingerServiceTraits::getError();
Vlad Popa1e865e62024-08-15 19:11:42 -0700385 af->setStreamVolume(stream, value, muted, output);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800386 return NO_ERROR;
387}
388
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800389status_t AudioSystem::setStreamMute(audio_stream_type_t stream, bool mute) {
Dima Zavinfce7a472011-04-19 22:30:36 -0700390 if (uint32_t(stream) >= AUDIO_STREAM_CNT) return BAD_VALUE;
Andy Hung264fa4c2024-02-21 15:52:12 -0800391 const sp<IAudioFlinger> af = get_audio_flinger();
Andy Hungce3d1102024-09-29 21:47:19 -0700392 if (af == nullptr) return AudioFlingerServiceTraits::getError();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800393 af->setStreamMute(stream, mute);
394 return NO_ERROR;
395}
396
Andy Hung6b137d12024-08-27 22:35:17 +0000397status_t AudioSystem::setPortsVolume(
Vlad Popa1e865e62024-08-15 19:11:42 -0700398 const std::vector<audio_port_handle_t>& portIds, float volume, bool muted,
399 audio_io_handle_t output) {
Andy Hung6b137d12024-08-27 22:35:17 +0000400 const sp<IAudioFlinger> af = get_audio_flinger();
Andy Hungce3d1102024-09-29 21:47:19 -0700401 if (af == nullptr) return AudioFlingerServiceTraits::getError();
Andy Hung6b137d12024-08-27 22:35:17 +0000402 std::vector<int32_t> portIdsAidl = VALUE_OR_RETURN_STATUS(
403 convertContainer<std::vector<int32_t>>(
404 portIds, legacy2aidl_audio_port_handle_t_int32_t));
405 int32_t outputAidl = VALUE_OR_RETURN_STATUS(legacy2aidl_audio_io_handle_t_int32_t(output));
Vlad Popa1e865e62024-08-15 19:11:42 -0700406 af->setPortsVolume(portIdsAidl, volume, muted, outputAidl);
Andy Hung6b137d12024-08-27 22:35:17 +0000407 return NO_ERROR;
408}
409
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800410status_t AudioSystem::setMode(audio_mode_t mode) {
Glenn Kasten930f4ca2012-01-06 16:47:31 -0800411 if (uint32_t(mode) >= AUDIO_MODE_CNT) return BAD_VALUE;
Andy Hung264fa4c2024-02-21 15:52:12 -0800412 const sp<IAudioFlinger> af = get_audio_flinger();
Andy Hungce3d1102024-09-29 21:47:19 -0700413 if (af == nullptr) return AudioFlingerServiceTraits::getError();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800414 return af->setMode(mode);
415}
416
Mikhail Naganovb1ddbb02023-03-15 17:06:59 -0700417status_t AudioSystem::setSimulateDeviceConnections(bool enabled) {
Andy Hung264fa4c2024-02-21 15:52:12 -0800418 const sp<IAudioFlinger> af = get_audio_flinger();
Andy Hungce3d1102024-09-29 21:47:19 -0700419 if (af == nullptr) return AudioFlingerServiceTraits::getError();
Mikhail Naganovb1ddbb02023-03-15 17:06:59 -0700420 return af->setSimulateDeviceConnections(enabled);
421}
422
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800423status_t AudioSystem::setParameters(audio_io_handle_t ioHandle, const String8& keyValuePairs) {
Andy Hung264fa4c2024-02-21 15:52:12 -0800424 const sp<IAudioFlinger> af = get_audio_flinger();
Andy Hungce3d1102024-09-29 21:47:19 -0700425 if (af == nullptr) return AudioFlingerServiceTraits::getError();
Eric Laurentc2f1f072009-07-17 12:17:14 -0700426 return af->setParameters(ioHandle, keyValuePairs);
427}
428
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800429String8 AudioSystem::getParameters(audio_io_handle_t ioHandle, const String8& keys) {
Andy Hung264fa4c2024-02-21 15:52:12 -0800430 const sp<IAudioFlinger> af = get_audio_flinger();
Eric Laurentc2f1f072009-07-17 12:17:14 -0700431 String8 result = String8("");
Andy Hungce3d1102024-09-29 21:47:19 -0700432 if (af == nullptr) return result;
Eric Laurentc2f1f072009-07-17 12:17:14 -0700433
434 result = af->getParameters(ioHandle, keys);
435 return result;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800436}
437
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800438status_t AudioSystem::setParameters(const String8& keyValuePairs) {
Glenn Kasten142f5192014-03-25 17:44:59 -0700439 return setParameters(AUDIO_IO_HANDLE_NONE, keyValuePairs);
Glenn Kastenc23885e2013-12-19 16:35:18 -0800440}
441
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800442String8 AudioSystem::getParameters(const String8& keys) {
Glenn Kasten142f5192014-03-25 17:44:59 -0700443 return getParameters(AUDIO_IO_HANDLE_NONE, keys);
Glenn Kastenc23885e2013-12-19 16:35:18 -0800444}
445
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800446// convert volume steps to natural log scale
447
448// change this value to change volume scaling
Andy Hungacd955a2024-02-16 17:14:36 -0800449constexpr float kdbPerStep = 0.5f;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800450// shouldn't need to touch these
Andy Hungacd955a2024-02-16 17:14:36 -0800451constexpr float kdBConvert = -kdbPerStep * 2.302585093f / 20.0f;
452constexpr float kdBConvertInverse = 1.0f / kdBConvert;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800453
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800454float AudioSystem::linearToLog(int volume) {
Andy Hungacd955a2024-02-16 17:14:36 -0800455 // float v = volume ? exp(float(100 - volume) * kdBConvert) : 0;
Steve Blockb8a80522011-12-20 16:23:08 +0000456 // ALOGD("linearToLog(%d)=%f", volume, v);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800457 // return v;
Andy Hungacd955a2024-02-16 17:14:36 -0800458 return volume ? exp(float(100 - volume) * kdBConvert) : 0;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800459}
460
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800461int AudioSystem::logToLinear(float volume) {
Andy Hungacd955a2024-02-16 17:14:36 -0800462 // int v = volume ? 100 - int(kdBConvertInverse * log(volume) + 0.5) : 0;
Steve Blockb8a80522011-12-20 16:23:08 +0000463 // ALOGD("logTolinear(%d)=%f", v, volume);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800464 // return v;
Andy Hungacd955a2024-02-16 17:14:36 -0800465 return volume ? 100 - int(kdBConvertInverse * log(volume) + 0.5) : 0;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800466}
467
Eric Laurent21da6472017-11-09 16:29:26 -0800468/* static */ size_t AudioSystem::calculateMinFrameCount(
469 uint32_t afLatencyMs, uint32_t afFrameCount, uint32_t afSampleRate,
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800470 uint32_t sampleRate, float speed /*, uint32_t notificationsPerBufferReq*/) {
Eric Laurent21da6472017-11-09 16:29:26 -0800471 // Ensure that buffer depth covers at least audio hardware latency
472 uint32_t minBufCount = afLatencyMs / ((1000 * afFrameCount) / afSampleRate);
473 if (minBufCount < 2) {
474 minBufCount = 2;
475 }
476#if 0
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800477 // The notificationsPerBufferReq parameter is not yet used for non-fast tracks,
478 // but keeping the code here to make it easier to add later.
479 if (minBufCount < notificationsPerBufferReq) {
480 minBufCount = notificationsPerBufferReq;
481 }
Eric Laurent21da6472017-11-09 16:29:26 -0800482#endif
483 ALOGV("calculateMinFrameCount afLatency %u afFrameCount %u afSampleRate %u "
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800484 "sampleRate %u speed %f minBufCount: %u" /*" notificationsPerBufferReq %u"*/,
485 afLatencyMs, afFrameCount, afSampleRate, sampleRate, speed, minBufCount
486 /*, notificationsPerBufferReq*/);
Eric Laurent21da6472017-11-09 16:29:26 -0800487 return minBufCount * sourceFramesNeededWithTimestretch(
488 sampleRate, afFrameCount, afSampleRate, speed);
489}
490
491
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800492status_t
493AudioSystem::getOutputSamplingRate(uint32_t* samplingRate, audio_stream_type_t streamType) {
Eric Laurentc2f1f072009-07-17 12:17:14 -0700494 audio_io_handle_t output;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800495
Dima Zavinfce7a472011-04-19 22:30:36 -0700496 if (streamType == AUDIO_STREAM_DEFAULT) {
497 streamType = AUDIO_STREAM_MUSIC;
Eric Laurentc2f1f072009-07-17 12:17:14 -0700498 }
499
Glenn Kastenfff6d712012-01-12 16:38:12 -0800500 output = getOutput(streamType);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700501 if (output == 0) {
502 return PERMISSION_DENIED;
503 }
504
Jean-Michel Trivib7f24b12014-06-11 10:05:30 -0700505 return getSamplingRate(output, samplingRate);
Eric Laurent1a9ed112012-03-20 18:36:01 -0700506}
507
Glenn Kasten2c073da2016-02-26 09:14:08 -0800508status_t AudioSystem::getSamplingRate(audio_io_handle_t ioHandle,
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800509 uint32_t* samplingRate) {
Andy Hung264fa4c2024-02-21 15:52:12 -0800510 const sp<IAudioFlinger> af = get_audio_flinger();
Andy Hungce3d1102024-09-29 21:47:19 -0700511 if (af == nullptr) return AudioFlingerServiceTraits::getError();
Glenn Kasten2c073da2016-02-26 09:14:08 -0800512 sp<AudioIoDescriptor> desc = getIoDescriptor(ioHandle);
513 if (desc == 0) {
514 *samplingRate = af->sampleRate(ioHandle);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700515 } else {
Mikhail Naganov88536df2021-07-26 17:30:29 -0700516 *samplingRate = desc->getSamplingRate();
Eric Laurentc2f1f072009-07-17 12:17:14 -0700517 }
Glenn Kastenf94006c2014-01-08 08:56:06 -0800518 if (*samplingRate == 0) {
Glenn Kasten2c073da2016-02-26 09:14:08 -0800519 ALOGE("AudioSystem::getSamplingRate failed for ioHandle %d", ioHandle);
Glenn Kastenf94006c2014-01-08 08:56:06 -0800520 return BAD_VALUE;
521 }
Eric Laurentc2f1f072009-07-17 12:17:14 -0700522
Glenn Kasten2c073da2016-02-26 09:14:08 -0800523 ALOGV("getSamplingRate() ioHandle %d, sampling rate %u", ioHandle, *samplingRate);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700524
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800525 return NO_ERROR;
526}
527
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800528status_t AudioSystem::getOutputFrameCount(size_t* frameCount, audio_stream_type_t streamType) {
Eric Laurentc2f1f072009-07-17 12:17:14 -0700529 audio_io_handle_t output;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800530
Dima Zavinfce7a472011-04-19 22:30:36 -0700531 if (streamType == AUDIO_STREAM_DEFAULT) {
532 streamType = AUDIO_STREAM_MUSIC;
Eric Laurentc2f1f072009-07-17 12:17:14 -0700533 }
Eric Laurent48f7f5e2009-04-02 09:32:43 -0700534
Glenn Kastenfff6d712012-01-12 16:38:12 -0800535 output = getOutput(streamType);
Glenn Kasten142f5192014-03-25 17:44:59 -0700536 if (output == AUDIO_IO_HANDLE_NONE) {
Eric Laurentc2f1f072009-07-17 12:17:14 -0700537 return PERMISSION_DENIED;
538 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800539
Jean-Michel Trivib7f24b12014-06-11 10:05:30 -0700540 return getFrameCount(output, frameCount);
Eric Laurent1a9ed112012-03-20 18:36:01 -0700541}
542
Glenn Kasten2c073da2016-02-26 09:14:08 -0800543status_t AudioSystem::getFrameCount(audio_io_handle_t ioHandle,
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800544 size_t* frameCount) {
Andy Hung264fa4c2024-02-21 15:52:12 -0800545 const sp<IAudioFlinger> af = get_audio_flinger();
Andy Hungce3d1102024-09-29 21:47:19 -0700546 if (af == nullptr) return AudioFlingerServiceTraits::getError();
Glenn Kasten2c073da2016-02-26 09:14:08 -0800547 sp<AudioIoDescriptor> desc = getIoDescriptor(ioHandle);
548 if (desc == 0) {
549 *frameCount = af->frameCount(ioHandle);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700550 } else {
Mikhail Naganov88536df2021-07-26 17:30:29 -0700551 *frameCount = desc->getFrameCount();
Eric Laurentc2f1f072009-07-17 12:17:14 -0700552 }
Glenn Kastenf94006c2014-01-08 08:56:06 -0800553 if (*frameCount == 0) {
Glenn Kasten2c073da2016-02-26 09:14:08 -0800554 ALOGE("AudioSystem::getFrameCount failed for ioHandle %d", ioHandle);
Glenn Kastenf94006c2014-01-08 08:56:06 -0800555 return BAD_VALUE;
556 }
Eric Laurentc2f1f072009-07-17 12:17:14 -0700557
Glenn Kasten2c073da2016-02-26 09:14:08 -0800558 ALOGV("getFrameCount() ioHandle %d, frameCount %zu", ioHandle, *frameCount);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700559
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800560 return NO_ERROR;
561}
562
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800563status_t AudioSystem::getOutputLatency(uint32_t* latency, audio_stream_type_t streamType) {
Eric Laurentc2f1f072009-07-17 12:17:14 -0700564 audio_io_handle_t output;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800565
Dima Zavinfce7a472011-04-19 22:30:36 -0700566 if (streamType == AUDIO_STREAM_DEFAULT) {
567 streamType = AUDIO_STREAM_MUSIC;
Eric Laurentc2f1f072009-07-17 12:17:14 -0700568 }
Eric Laurent48f7f5e2009-04-02 09:32:43 -0700569
Glenn Kastenfff6d712012-01-12 16:38:12 -0800570 output = getOutput(streamType);
Glenn Kasten142f5192014-03-25 17:44:59 -0700571 if (output == AUDIO_IO_HANDLE_NONE) {
Eric Laurentc2f1f072009-07-17 12:17:14 -0700572 return PERMISSION_DENIED;
573 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800574
Glenn Kasten241618f2014-03-25 17:48:57 -0700575 return getLatency(output, latency);
Eric Laurent1a9ed112012-03-20 18:36:01 -0700576}
577
578status_t AudioSystem::getLatency(audio_io_handle_t output,
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800579 uint32_t* latency) {
Andy Hung264fa4c2024-02-21 15:52:12 -0800580 const sp<IAudioFlinger> af = get_audio_flinger();
Andy Hungce3d1102024-09-29 21:47:19 -0700581 if (af == nullptr) return AudioFlingerServiceTraits::getError();
Eric Laurent296fb132015-05-01 11:38:42 -0700582 sp<AudioIoDescriptor> outputDesc = getIoDescriptor(output);
Eric Laurent73e26b62015-04-27 16:55:58 -0700583 if (outputDesc == 0) {
Eric Laurentc2f1f072009-07-17 12:17:14 -0700584 *latency = af->latency(output);
585 } else {
Mikhail Naganov88536df2021-07-26 17:30:29 -0700586 *latency = outputDesc->getLatency();
Eric Laurentc2f1f072009-07-17 12:17:14 -0700587 }
588
Glenn Kasten241618f2014-03-25 17:48:57 -0700589 ALOGV("getLatency() output %d, latency %d", output, *latency);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700590
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800591 return NO_ERROR;
592}
593
Glenn Kastendd8104c2012-07-02 12:42:44 -0700594status_t AudioSystem::getInputBufferSize(uint32_t sampleRate, audio_format_t format,
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800595 audio_channel_mask_t channelMask, size_t* buffSize) {
Eric Laurent296fb132015-05-01 11:38:42 -0700596 const sp<AudioFlingerClient> afc = getAudioFlingerClient();
597 if (afc == 0) {
598 return NO_INIT;
599 }
600 return afc->getInputBufferSize(sampleRate, format, channelMask, buffSize);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800601}
602
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800603status_t AudioSystem::setVoiceVolume(float value) {
Andy Hung264fa4c2024-02-21 15:52:12 -0800604 const sp<IAudioFlinger> af = get_audio_flinger();
Andy Hungce3d1102024-09-29 21:47:19 -0700605 if (af == nullptr) return AudioFlingerServiceTraits::getError();
Eric Laurentf0ee6f42009-10-21 08:14:22 -0700606 return af->setVoiceVolume(value);
607}
608
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800609status_t AudioSystem::getRenderPosition(audio_io_handle_t output, uint32_t* halFrames,
610 uint32_t* dspFrames) {
Andy Hung264fa4c2024-02-21 15:52:12 -0800611 const sp<IAudioFlinger> af = get_audio_flinger();
Andy Hungce3d1102024-09-29 21:47:19 -0700612 if (af == nullptr) return AudioFlingerServiceTraits::getError();
Eric Laurent342e9cf2010-01-19 17:37:09 -0800613
Richard Fitzgeraldad3af332013-03-25 16:54:37 +0000614 return af->getRenderPosition(halFrames, dspFrames, output);
Eric Laurent342e9cf2010-01-19 17:37:09 -0800615}
616
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800617uint32_t AudioSystem::getInputFramesLost(audio_io_handle_t ioHandle) {
Andy Hung264fa4c2024-02-21 15:52:12 -0800618 const sp<IAudioFlinger> af = get_audio_flinger();
Glenn Kasten5f972c02014-01-13 09:59:31 -0800619 uint32_t result = 0;
Andy Hungce3d1102024-09-29 21:47:19 -0700620 if (af == nullptr) return result;
Glenn Kasten142f5192014-03-25 17:44:59 -0700621 if (ioHandle == AUDIO_IO_HANDLE_NONE) return result;
Eric Laurent05bca2f2010-02-26 02:47:27 -0800622
623 result = af->getInputFramesLost(ioHandle);
624 return result;
625}
626
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800627audio_unique_id_t AudioSystem::newAudioUniqueId(audio_unique_id_use_t use) {
Mikhail Naganov2996f672019-04-18 12:29:59 -0700628 // Must not use AF as IDs will re-roll on audioserver restart, b/130369529.
Andy Hung264fa4c2024-02-21 15:52:12 -0800629 const sp<IAudioFlinger> af = get_audio_flinger();
Andy Hungce3d1102024-09-29 21:47:19 -0700630 if (af == nullptr) return AUDIO_UNIQUE_ID_ALLOCATE;
Glenn Kasteneeecb982016-02-26 10:44:04 -0800631 return af->newAudioUniqueId(use);
Eric Laurentbe916aa2010-06-01 23:49:17 -0700632}
633
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800634void AudioSystem::acquireAudioSessionId(audio_session_t audioSession, pid_t pid, uid_t uid) {
Andy Hung264fa4c2024-02-21 15:52:12 -0800635 const sp<IAudioFlinger> af = get_audio_flinger();
Marco Nelissen3a34bef2011-08-02 13:33:41 -0700636 if (af != 0) {
Andy Hung8b0bfd92019-12-23 13:11:11 -0800637 af->acquireAudioSessionId(audioSession, pid, uid);
Marco Nelissen3a34bef2011-08-02 13:33:41 -0700638 }
639}
640
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800641void AudioSystem::releaseAudioSessionId(audio_session_t audioSession, pid_t pid) {
Andy Hung264fa4c2024-02-21 15:52:12 -0800642 const sp<IAudioFlinger> af = get_audio_flinger();
Marco Nelissen3a34bef2011-08-02 13:33:41 -0700643 if (af != 0) {
Marco Nelissend457c972014-02-11 08:47:07 -0800644 af->releaseAudioSessionId(audioSession, pid);
Marco Nelissen3a34bef2011-08-02 13:33:41 -0700645 }
646}
647
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800648audio_hw_sync_t AudioSystem::getAudioHwSyncForSession(audio_session_t sessionId) {
Andy Hung264fa4c2024-02-21 15:52:12 -0800649 const sp<IAudioFlinger> af = get_audio_flinger();
Andy Hungce3d1102024-09-29 21:47:19 -0700650 if (af == nullptr) return AUDIO_HW_SYNC_INVALID;
Eric Laurent93c3d412014-08-01 14:48:35 -0700651 return af->getAudioHwSyncForSession(sessionId);
652}
653
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800654status_t AudioSystem::systemReady() {
Andy Hung264fa4c2024-02-21 15:52:12 -0800655 const sp<IAudioFlinger> af = get_audio_flinger();
Andy Hungce3d1102024-09-29 21:47:19 -0700656 if (af == nullptr) return NO_INIT;
Eric Laurent72e3f392015-05-20 14:43:50 -0700657 return af->systemReady();
658}
659
Eric Laurentd66d7a12021-07-13 13:35:32 +0200660status_t AudioSystem::audioPolicyReady() {
Andy Hung264fa4c2024-02-21 15:52:12 -0800661 const sp<IAudioFlinger> af = get_audio_flinger();
Andy Hungce3d1102024-09-29 21:47:19 -0700662 if (af == nullptr) return NO_INIT;
Eric Laurentd66d7a12021-07-13 13:35:32 +0200663 return af->audioPolicyReady();
664}
665
Glenn Kasten4a8308b2016-04-18 14:10:01 -0700666status_t AudioSystem::getFrameCountHAL(audio_io_handle_t ioHandle,
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800667 size_t* frameCount) {
Andy Hung264fa4c2024-02-21 15:52:12 -0800668 const sp<IAudioFlinger> af = get_audio_flinger();
Andy Hungce3d1102024-09-29 21:47:19 -0700669 if (af == nullptr) return AudioFlingerServiceTraits::getError();
Glenn Kasten4a8308b2016-04-18 14:10:01 -0700670 sp<AudioIoDescriptor> desc = getIoDescriptor(ioHandle);
671 if (desc == 0) {
672 *frameCount = af->frameCountHAL(ioHandle);
673 } else {
Mikhail Naganov88536df2021-07-26 17:30:29 -0700674 *frameCount = desc->getFrameCountHAL();
Glenn Kasten4a8308b2016-04-18 14:10:01 -0700675 }
676 if (*frameCount == 0) {
677 ALOGE("AudioSystem::getFrameCountHAL failed for ioHandle %d", ioHandle);
678 return BAD_VALUE;
679 }
680
681 ALOGV("getFrameCountHAL() ioHandle %d, frameCount %zu", ioHandle, *frameCount);
682
683 return NO_ERROR;
684}
685
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800686// ---------------------------------------------------------------------------
687
Eric Laurent73e26b62015-04-27 16:55:58 -0700688
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800689void AudioSystem::AudioFlingerClient::clearIoCache() {
Andy Hungacd955a2024-02-16 17:14:36 -0800690 std::lock_guard _l(mMutex);
Eric Laurent73e26b62015-04-27 16:55:58 -0700691 mIoDescriptors.clear();
692 mInBuffSize = 0;
693 mInSamplingRate = 0;
694 mInFormat = AUDIO_FORMAT_DEFAULT;
695 mInChannelMask = AUDIO_CHANNEL_NONE;
696}
697
Ytai Ben-Tsvi10dc0a62020-09-18 11:31:55 -0700698Status AudioSystem::AudioFlingerClient::ioConfigChanged(
699 media::AudioIoConfigEvent _event,
700 const media::AudioIoDescriptor& _ioDesc) {
Mikhail Naganov88536df2021-07-26 17:30:29 -0700701 audio_io_config_event_t event = VALUE_OR_RETURN_BINDER_STATUS(
702 aidl2legacy_AudioIoConfigEvent_audio_io_config_event_t(_event));
Ytai Ben-Tsvi10dc0a62020-09-18 11:31:55 -0700703 sp<AudioIoDescriptor> ioDesc(
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800704 VALUE_OR_RETURN_BINDER_STATUS(
705 aidl2legacy_AudioIoDescriptor_AudioIoDescriptor(_ioDesc)));
Ytai Ben-Tsvi10dc0a62020-09-18 11:31:55 -0700706
Steve Block3856b092011-10-20 11:56:00 +0100707 ALOGV("ioConfigChanged() event %d", event);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700708
Mikhail Naganov88536df2021-07-26 17:30:29 -0700709 if (ioDesc->getIoHandle() == AUDIO_IO_HANDLE_NONE) return Status::ok();
Eric Laurentc2f1f072009-07-17 12:17:14 -0700710
Robert Wub7f8edc2024-11-04 19:54:38 +0000711 DeviceIdVector deviceIds;
Eric Laurent09f1ed22019-04-24 17:45:17 -0700712 std::vector<sp<AudioDeviceCallback>> callbacksToCall;
Eric Laurent296fb132015-05-01 11:38:42 -0700713 {
Andy Hungacd955a2024-02-16 17:14:36 -0800714 std::lock_guard _l(mMutex);
Eric Laurent09f1ed22019-04-24 17:45:17 -0700715 auto callbacks = std::map<audio_port_handle_t, wp<AudioDeviceCallback>>();
Eric Laurent296fb132015-05-01 11:38:42 -0700716
717 switch (event) {
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800718 case AUDIO_OUTPUT_OPENED:
719 case AUDIO_OUTPUT_REGISTERED:
720 case AUDIO_INPUT_OPENED:
721 case AUDIO_INPUT_REGISTERED: {
Andy Hungd211ce02024-02-16 15:34:07 -0800722 if (sp<AudioIoDescriptor> oldDesc = getIoDescriptor_l(ioDesc->getIoHandle())) {
Robert Wub7f8edc2024-11-04 19:54:38 +0000723 deviceIds = oldDesc->getDeviceIds();
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800724 }
Andy Hungd211ce02024-02-16 15:34:07 -0800725 mIoDescriptors[ioDesc->getIoHandle()] = ioDesc;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800726
Robert Wub7f8edc2024-11-04 19:54:38 +0000727 if (!ioDesc->getDeviceIds().empty()) {
728 deviceIds = ioDesc->getDeviceIds();
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800729 if (event == AUDIO_OUTPUT_OPENED || event == AUDIO_INPUT_OPENED) {
Mikhail Naganov88536df2021-07-26 17:30:29 -0700730 auto it = mAudioDeviceCallbacks.find(ioDesc->getIoHandle());
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800731 if (it != mAudioDeviceCallbacks.end()) {
732 callbacks = it->second;
733 }
734 }
735 }
Mikhail Naganov88536df2021-07-26 17:30:29 -0700736 ALOGV("ioConfigChanged() new %s %s %s",
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800737 event == AUDIO_OUTPUT_OPENED || event == AUDIO_OUTPUT_REGISTERED ?
738 "output" : "input",
739 event == AUDIO_OUTPUT_OPENED || event == AUDIO_INPUT_OPENED ?
740 "opened" : "registered",
Mikhail Naganov88536df2021-07-26 17:30:29 -0700741 ioDesc->toDebugString().c_str());
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800742 }
743 break;
744 case AUDIO_OUTPUT_CLOSED:
745 case AUDIO_INPUT_CLOSED: {
Mikhail Naganov88536df2021-07-26 17:30:29 -0700746 if (getIoDescriptor_l(ioDesc->getIoHandle()) == 0) {
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800747 ALOGW("ioConfigChanged() closing unknown %s %d",
Mikhail Naganov88536df2021-07-26 17:30:29 -0700748 event == AUDIO_OUTPUT_CLOSED ? "output" : "input", ioDesc->getIoHandle());
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800749 break;
750 }
751 ALOGV("ioConfigChanged() %s %d closed",
Mikhail Naganov88536df2021-07-26 17:30:29 -0700752 event == AUDIO_OUTPUT_CLOSED ? "output" : "input", ioDesc->getIoHandle());
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800753
Andy Hungd211ce02024-02-16 15:34:07 -0800754 mIoDescriptors.erase(ioDesc->getIoHandle());
Mikhail Naganov88536df2021-07-26 17:30:29 -0700755 mAudioDeviceCallbacks.erase(ioDesc->getIoHandle());
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800756 }
757 break;
758
759 case AUDIO_OUTPUT_CONFIG_CHANGED:
760 case AUDIO_INPUT_CONFIG_CHANGED: {
Mikhail Naganov88536df2021-07-26 17:30:29 -0700761 sp<AudioIoDescriptor> oldDesc = getIoDescriptor_l(ioDesc->getIoHandle());
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800762 if (oldDesc == 0) {
763 ALOGW("ioConfigChanged() modifying unknown %s! %d",
764 event == AUDIO_OUTPUT_CONFIG_CHANGED ? "output" : "input",
Mikhail Naganov88536df2021-07-26 17:30:29 -0700765 ioDesc->getIoHandle());
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800766 break;
767 }
768
Robert Wub7f8edc2024-11-04 19:54:38 +0000769 deviceIds = oldDesc->getDeviceIds();
Andy Hungd211ce02024-02-16 15:34:07 -0800770 mIoDescriptors[ioDesc->getIoHandle()] = ioDesc;
Eric Laurent296fb132015-05-01 11:38:42 -0700771
Robert Wub7f8edc2024-11-04 19:54:38 +0000772 DeviceIdVector ioDescDeviceIds = ioDesc->getDeviceIds();
773 if (!areDeviceIdsEqual(deviceIds, ioDescDeviceIds)) {
774 deviceIds = ioDescDeviceIds;
Mikhail Naganov88536df2021-07-26 17:30:29 -0700775 auto it = mAudioDeviceCallbacks.find(ioDesc->getIoHandle());
Eric Laurent09f1ed22019-04-24 17:45:17 -0700776 if (it != mAudioDeviceCallbacks.end()) {
777 callbacks = it->second;
Francois Gaffie24a9fb02019-01-18 17:51:34 +0100778 }
779 }
Mikhail Naganov88536df2021-07-26 17:30:29 -0700780 ALOGV("ioConfigChanged() new config for %s %s",
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800781 event == AUDIO_OUTPUT_CONFIG_CHANGED ? "output" : "input",
Mikhail Naganov88536df2021-07-26 17:30:29 -0700782 ioDesc->toDebugString().c_str());
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800783
Eric Laurent296fb132015-05-01 11:38:42 -0700784 }
Eric Laurent296fb132015-05-01 11:38:42 -0700785 break;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800786 case AUDIO_CLIENT_STARTED: {
Mikhail Naganov88536df2021-07-26 17:30:29 -0700787 sp<AudioIoDescriptor> oldDesc = getIoDescriptor_l(ioDesc->getIoHandle());
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800788 if (oldDesc == 0) {
Mikhail Naganov88536df2021-07-26 17:30:29 -0700789 ALOGW("ioConfigChanged() start client on unknown io! %d",
790 ioDesc->getIoHandle());
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800791 break;
792 }
793 ALOGV("ioConfigChanged() AUDIO_CLIENT_STARTED io %d port %d num callbacks %zu",
Mikhail Naganov88536df2021-07-26 17:30:29 -0700794 ioDesc->getIoHandle(), ioDesc->getPortId(), mAudioDeviceCallbacks.size());
795 oldDesc->setPatch(ioDesc->getPatch());
796 auto it = mAudioDeviceCallbacks.find(ioDesc->getIoHandle());
Eric Laurent09f1ed22019-04-24 17:45:17 -0700797 if (it != mAudioDeviceCallbacks.end()) {
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800798 auto cbks = it->second;
Mikhail Naganov88536df2021-07-26 17:30:29 -0700799 auto it2 = cbks.find(ioDesc->getPortId());
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800800 if (it2 != cbks.end()) {
Mikhail Naganov88536df2021-07-26 17:30:29 -0700801 callbacks.emplace(ioDesc->getPortId(), it2->second);
Robert Wub7f8edc2024-11-04 19:54:38 +0000802 deviceIds = oldDesc->getDeviceIds();
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800803 }
Eric Laurent09f1ed22019-04-24 17:45:17 -0700804 }
Eric Laurent296fb132015-05-01 11:38:42 -0700805 }
Eric Laurent09f1ed22019-04-24 17:45:17 -0700806 break;
Eric Laurent09f1ed22019-04-24 17:45:17 -0700807 }
808
809 for (auto wpCbk : callbacks) {
810 sp<AudioDeviceCallback> spCbk = wpCbk.second.promote();
811 if (spCbk != nullptr) {
812 callbacksToCall.push_back(spCbk);
813 }
Eric Laurentad2e7b92017-09-14 20:06:42 -0700814 }
Eric Laurent4463ff52019-02-07 13:56:09 -0800815 }
816
Andy Hungacd955a2024-02-16 17:14:36 -0800817 // Callbacks must be called without mMutex held. May lead to dead lock if calling for
818 // example getRoutedDevice that updates the device and tries to acquire mMutex.
Eric Laurent09f1ed22019-04-24 17:45:17 -0700819 for (auto cb : callbacksToCall) {
Mikhail Naganov88536df2021-07-26 17:30:29 -0700820 // If callbacksToCall is not empty, it implies ioDesc->getIoHandle() and deviceId are valid
Robert Wub7f8edc2024-11-04 19:54:38 +0000821 cb->onAudioDeviceUpdate(ioDesc->getIoHandle(), deviceIds);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700822 }
Ytai Ben-Tsvi10dc0a62020-09-18 11:31:55 -0700823
824 return Status::ok();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800825}
826
Eric Laurent076e7c72022-05-03 18:12:28 +0200827Status AudioSystem::AudioFlingerClient::onSupportedLatencyModesChanged(
Mikhail Naganovf53e1822022-12-18 02:48:14 +0000828 int output, const std::vector<media::audio::common::AudioLatencyMode>& latencyModes) {
Eric Laurent076e7c72022-05-03 18:12:28 +0200829 audio_io_handle_t outputLegacy = VALUE_OR_RETURN_BINDER_STATUS(
830 aidl2legacy_int32_t_audio_io_handle_t(output));
831 std::vector<audio_latency_mode_t> modesLegacy = VALUE_OR_RETURN_BINDER_STATUS(
832 convertContainer<std::vector<audio_latency_mode_t>>(
Mikhail Naganovf53e1822022-12-18 02:48:14 +0000833 latencyModes, aidl2legacy_AudioLatencyMode_audio_latency_mode_t));
Eric Laurent076e7c72022-05-03 18:12:28 +0200834
835 std::vector<sp<SupportedLatencyModesCallback>> callbacks;
836 {
Andy Hungacd955a2024-02-16 17:14:36 -0800837 std::lock_guard _l(mMutex);
Eric Laurent076e7c72022-05-03 18:12:28 +0200838 for (auto callback : mSupportedLatencyModesCallbacks) {
839 if (auto ref = callback.promote(); ref != nullptr) {
840 callbacks.push_back(ref);
841 }
842 }
843 }
844 for (const auto& callback : callbacks) {
845 callback->onSupportedLatencyModesChanged(outputLegacy, modesLegacy);
846 }
847
848 return Status::ok();
849}
850
Eric Laurent73e26b62015-04-27 16:55:58 -0700851status_t AudioSystem::AudioFlingerClient::getInputBufferSize(
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800852 uint32_t sampleRate, audio_format_t format,
853 audio_channel_mask_t channelMask, size_t* buffSize) {
Andy Hung264fa4c2024-02-21 15:52:12 -0800854 const sp<IAudioFlinger> af = get_audio_flinger();
Andy Hungce3d1102024-09-29 21:47:19 -0700855 if (af == nullptr) return AudioFlingerServiceTraits::getError();
Andy Hungacd955a2024-02-16 17:14:36 -0800856 std::lock_guard _l(mMutex);
Eric Laurent73e26b62015-04-27 16:55:58 -0700857 // Do we have a stale mInBuffSize or are we requesting the input buffer size for new values
858 if ((mInBuffSize == 0) || (sampleRate != mInSamplingRate) || (format != mInFormat)
859 || (channelMask != mInChannelMask)) {
860 size_t inBuffSize = af->getInputBufferSize(sampleRate, format, channelMask);
861 if (inBuffSize == 0) {
Glenn Kasten49f36ba2017-12-06 13:02:02 -0800862 ALOGE("AudioSystem::getInputBufferSize failed sampleRate %d format %#x channelMask %#x",
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800863 sampleRate, format, channelMask);
Eric Laurent73e26b62015-04-27 16:55:58 -0700864 return BAD_VALUE;
865 }
866 // A benign race is possible here: we could overwrite a fresher cache entry
867 // save the request params
868 mInSamplingRate = sampleRate;
869 mInFormat = format;
870 mInChannelMask = channelMask;
871
872 mInBuffSize = inBuffSize;
873 }
874
875 *buffSize = mInBuffSize;
876
877 return NO_ERROR;
878}
879
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800880sp<AudioIoDescriptor>
881AudioSystem::AudioFlingerClient::getIoDescriptor_l(audio_io_handle_t ioHandle) {
Andy Hungd211ce02024-02-16 15:34:07 -0800882 if (const auto it = mIoDescriptors.find(ioHandle);
883 it != mIoDescriptors.end()) {
884 return it->second;
Eric Laurent73e26b62015-04-27 16:55:58 -0700885 }
Andy Hungd211ce02024-02-16 15:34:07 -0800886 return {};
Eric Laurent73e26b62015-04-27 16:55:58 -0700887}
888
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800889sp<AudioIoDescriptor> AudioSystem::AudioFlingerClient::getIoDescriptor(audio_io_handle_t ioHandle) {
Andy Hungacd955a2024-02-16 17:14:36 -0800890 std::lock_guard _l(mMutex);
Praveen Chavan49fdeaf2015-09-29 02:25:47 -0700891 return getIoDescriptor_l(ioHandle);
892}
893
Eric Laurent296fb132015-05-01 11:38:42 -0700894status_t AudioSystem::AudioFlingerClient::addAudioDeviceCallback(
Eric Laurent09f1ed22019-04-24 17:45:17 -0700895 const wp<AudioDeviceCallback>& callback, audio_io_handle_t audioIo,
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800896 audio_port_handle_t portId) {
Eric Laurent09f1ed22019-04-24 17:45:17 -0700897 ALOGV("%s audioIo %d portId %d", __func__, audioIo, portId);
Andy Hungacd955a2024-02-16 17:14:36 -0800898 std::lock_guard _l(mMutex);
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800899 auto& callbacks = mAudioDeviceCallbacks.emplace(
900 audioIo,
901 std::map<audio_port_handle_t, wp<AudioDeviceCallback>>()).first->second;
Eric Laurent09f1ed22019-04-24 17:45:17 -0700902 auto result = callbacks.try_emplace(portId, callback);
903 if (!result.second) {
904 return INVALID_OPERATION;
Eric Laurent296fb132015-05-01 11:38:42 -0700905 }
Eric Laurent296fb132015-05-01 11:38:42 -0700906 return NO_ERROR;
907}
908
909status_t AudioSystem::AudioFlingerClient::removeAudioDeviceCallback(
Eric Laurent09f1ed22019-04-24 17:45:17 -0700910 const wp<AudioDeviceCallback>& callback __unused, audio_io_handle_t audioIo,
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800911 audio_port_handle_t portId) {
Eric Laurent09f1ed22019-04-24 17:45:17 -0700912 ALOGV("%s audioIo %d portId %d", __func__, audioIo, portId);
Andy Hungacd955a2024-02-16 17:14:36 -0800913 std::lock_guard _l(mMutex);
Eric Laurent09f1ed22019-04-24 17:45:17 -0700914 auto it = mAudioDeviceCallbacks.find(audioIo);
915 if (it == mAudioDeviceCallbacks.end()) {
Eric Laurent296fb132015-05-01 11:38:42 -0700916 return INVALID_OPERATION;
917 }
Eric Laurent09f1ed22019-04-24 17:45:17 -0700918 if (it->second.erase(portId) == 0) {
Eric Laurent296fb132015-05-01 11:38:42 -0700919 return INVALID_OPERATION;
920 }
Eric Laurent09f1ed22019-04-24 17:45:17 -0700921 if (it->second.size() == 0) {
922 mAudioDeviceCallbacks.erase(audioIo);
Eric Laurent296fb132015-05-01 11:38:42 -0700923 }
924 return NO_ERROR;
925}
926
Eric Laurent076e7c72022-05-03 18:12:28 +0200927status_t AudioSystem::AudioFlingerClient::addSupportedLatencyModesCallback(
928 const sp<SupportedLatencyModesCallback>& callback) {
Andy Hungacd955a2024-02-16 17:14:36 -0800929 std::lock_guard _l(mMutex);
Eric Laurent076e7c72022-05-03 18:12:28 +0200930 if (std::find(mSupportedLatencyModesCallbacks.begin(),
931 mSupportedLatencyModesCallbacks.end(),
932 callback) != mSupportedLatencyModesCallbacks.end()) {
933 return INVALID_OPERATION;
934 }
935 mSupportedLatencyModesCallbacks.push_back(callback);
936 return NO_ERROR;
937}
938
939status_t AudioSystem::AudioFlingerClient::removeSupportedLatencyModesCallback(
940 const sp<SupportedLatencyModesCallback>& callback) {
Andy Hungacd955a2024-02-16 17:14:36 -0800941 std::lock_guard _l(mMutex);
Eric Laurent076e7c72022-05-03 18:12:28 +0200942 auto it = std::find(mSupportedLatencyModesCallbacks.begin(),
943 mSupportedLatencyModesCallbacks.end(),
944 callback);
945 if (it == mSupportedLatencyModesCallbacks.end()) {
946 return INVALID_OPERATION;
947 }
948 mSupportedLatencyModesCallbacks.erase(it);
949 return NO_ERROR;
950}
951
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800952/* static */ uintptr_t AudioSystem::addErrorCallback(audio_error_callback cb) {
Andy Hungacd955a2024-02-16 17:14:36 -0800953 std::lock_guard _l(gErrorCallbacksMutex);
Ytai Ben-Tsvi000c3e42020-01-09 15:26:40 -0800954 gAudioErrorCallbacks.insert(cb);
955 return reinterpret_cast<uintptr_t>(cb);
956}
957
958/* static */ void AudioSystem::removeErrorCallback(uintptr_t cb) {
Andy Hungacd955a2024-02-16 17:14:36 -0800959 std::lock_guard _l(gErrorCallbacksMutex);
Ytai Ben-Tsvi000c3e42020-01-09 15:26:40 -0800960 gAudioErrorCallbacks.erase(reinterpret_cast<audio_error_callback>(cb));
961}
962
963/* static */ void AudioSystem::reportError(status_t err) {
Andy Hungacd955a2024-02-16 17:14:36 -0800964 std::lock_guard _l(gErrorCallbacksMutex);
Ytai Ben-Tsvi000c3e42020-01-09 15:26:40 -0800965 for (auto callback : gAudioErrorCallbacks) {
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800966 callback(err);
Ytai Ben-Tsvi000c3e42020-01-09 15:26:40 -0800967 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800968}
969
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800970/*static*/ void AudioSystem::setDynPolicyCallback(dynamic_policy_callback cb) {
Andy Hungacd955a2024-02-16 17:14:36 -0800971 std::lock_guard _l(gMutex);
Jean-Michel Trivif613d422015-04-23 18:41:29 -0700972 gDynPolicyCallback = cb;
973}
974
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800975/*static*/ void AudioSystem::setRecordConfigCallback(record_config_callback cb) {
Andy Hungacd955a2024-02-16 17:14:36 -0800976 std::lock_guard _l(gMutex);
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -0800977 gRecordConfigCallback = cb;
978}
979
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800980/*static*/ void AudioSystem::setRoutingCallback(routing_callback cb) {
Andy Hungacd955a2024-02-16 17:14:36 -0800981 std::lock_guard _l(gMutex);
Jean-Michel Trivi9a6b9ad2020-10-22 16:46:43 -0700982 gRoutingCallback = cb;
983}
984
Jean-Michel Trivi78f2b302022-04-15 18:18:41 +0000985/*static*/ void AudioSystem::setVolInitReqCallback(vol_range_init_req_callback cb) {
Andy Hungacd955a2024-02-16 17:14:36 -0800986 std::lock_guard _l(gMutex);
Jean-Michel Trivi78f2b302022-04-15 18:18:41 +0000987 gVolRangeInitReqCallback = cb;
988}
989
Andy Hungce3d1102024-09-29 21:47:19 -0700990
991// AudioPolicyServiceTraits is a collection of methods that parameterize the
992// ServiceSingleton class implementation of IAudioPolicyService.
993
994class AudioPolicyServiceTraits {
995public:
996 // ------- methods required by ServiceSingleton
997
998 static constexpr const char* getServiceName() { return "media.audio_policy"; }
999
1000 static void onNewService(const sp<IAudioPolicyService>& aps) {
1001 ALOGD("%s: %s service obtained %p", __func__, getServiceName(), aps.get());
1002 sp<AudioSystem::AudioPolicyServiceClient> client;
1003 {
1004 std::lock_guard l(mMutex);
Andy Hunge20145d2024-11-25 16:22:37 -08001005 ALOGW_IF(mValid, "%s: %s service already valid, continuing with initialization",
1006 __func__, getServiceName());
Andy Hungce3d1102024-09-29 21:47:19 -07001007 if (mClient == nullptr) {
1008 mClient = sp<AudioSystem::AudioPolicyServiceClient>::make();
1009 }
1010 client = mClient;
1011 mService = aps;
1012 mValid = true;
1013 }
1014 // TODO(b/375280520) consider registerClient() within mMutex lock.
Andy Hung88e71732024-02-21 16:44:27 -08001015 const int64_t token = IPCThreadState::self()->clearCallingIdentity();
Andy Hungce3d1102024-09-29 21:47:19 -07001016 aps->registerClient(client);
Eric Laurent30adca52024-12-04 14:49:49 +00001017 aps->setAudioPortCallbacksEnabled(client->isAudioPortCbEnabled());
1018 aps->setAudioVolumeGroupCallbacksEnabled(client->isAudioVolumeGroupCbEnabled());
François Gaffie24437602018-04-23 15:08:59 +02001019 IPCThreadState::self()->restoreCallingIdentity(token);
Eric Laurentc2f1f072009-07-17 12:17:14 -07001020 }
Glenn Kastend2d089f2014-11-05 11:48:12 -08001021
Andy Hunge20145d2024-11-25 16:22:37 -08001022 static void onServiceDied(const sp<IAudioPolicyService>& service) {
1023 ALOGW("%s: %s service died %p", __func__, getServiceName(), service.get());
Andy Hungce3d1102024-09-29 21:47:19 -07001024 sp<AudioSystem::AudioPolicyServiceClient> client;
1025 {
1026 std::lock_guard l(mMutex);
Andy Hunge20145d2024-11-25 16:22:37 -08001027 if (!mValid) {
1028 ALOGW("%s: %s service already invalidated, ignoring", __func__, getServiceName());
1029 return;
1030 }
1031 if (mService != service) {
1032 ALOGW("%s: %s unmatched service death pointers, ignoring",
1033 __func__, getServiceName());
1034 return;
1035 }
Andy Hungce3d1102024-09-29 21:47:19 -07001036 mValid = false;
1037 client = mClient;
1038 }
Andy Hungc4f7d872024-11-20 20:29:32 -08001039 if (client) {
1040 client->onServiceDied();
1041 } else {
1042 ALOGW("%s: null client", __func__);
1043 }
Andy Hungce3d1102024-09-29 21:47:19 -07001044 }
Andy Hung88e71732024-02-21 16:44:27 -08001045
Andy Hungce3d1102024-09-29 21:47:19 -07001046 static constexpr mediautils::ServiceOptions options() {
1047 return mediautils::ServiceOptions::kNone;
1048 }
1049
1050 // ------- methods required by AudioSystem
1051
1052 static sp<IAudioPolicyService> getService(
1053 std::chrono::milliseconds waitMs = std::chrono::milliseconds{-1}) {
1054 static bool init = false;
1055 audio_utils::unique_lock ul(mMutex);
1056 if (!init) {
1057 if (!mDisableThreadPoolStart) {
1058 ProcessState::self()->startThreadPool();
1059 }
Andy Hungd6346072024-11-25 15:23:40 -08001060 if (multiuser_get_app_id(getuid()) == AID_AUDIOSERVER) {
1061 mediautils::skipService<IAudioPolicyService>(mediautils::SkipMode::kWait);
1062 mWaitMs = std::chrono::milliseconds(INT32_MAX);
1063 } else {
1064 mediautils::initService<IAudioPolicyService, AudioPolicyServiceTraits>();
1065 mWaitMs = std::chrono::milliseconds(
1066 property_get_int32(kServiceWaitProperty, kServiceClientWaitMs));
1067 }
Andy Hungce3d1102024-09-29 21:47:19 -07001068 init = true;
1069 }
1070 if (mValid) return mService;
1071 if (waitMs.count() < 0) waitMs = mWaitMs;
1072 ul.unlock();
1073
1074 auto service = mediautils::getService<
1075 media::IAudioPolicyService>(waitMs);
1076 ALOGD("%s: checking for service %s: %p", __func__, getServiceName(), service.get());
1077
1078 // mediautils::getService() will return early if setLocalService() is called
1079 // (whereupon mService contained the actual local service pointer to use).
1080 // we should always return mService.
1081 ul.lock();
1082 return mService;
1083 }
1084
1085 static sp<AudioSystem::AudioPolicyServiceClient> getClient() {
1086 audio_utils::unique_lock ul(mMutex);
1087 if (mValid) return mClient;
1088 ul.unlock();
1089
1090 auto service = getService();
1091 ALOGD("%s: checking for service: %p", __func__, service.get());
1092
1093 ul.lock();
1094 return mClient;
1095 }
1096
1097 static status_t setLocalService(const sp<IAudioPolicyService>& aps) {
1098 mediautils::skipService<IAudioPolicyService>();
1099 sp<IAudioPolicyService> old;
Andy Hunge20145d2024-11-25 16:22:37 -08001100 audio_utils::unique_lock ul(mMutex);
1101 old = mService;
1102 if (old) {
1103 ul.unlock();
1104 onServiceDied(old);
1105 ul.lock();
1106 if (mService != old) {
1107 ALOGD("%s: service changed during callback, ignoring.", __func__);
1108 return OK;
1109 }
Andy Hungce3d1102024-09-29 21:47:19 -07001110 }
Andy Hunge20145d2024-11-25 16:22:37 -08001111 mService = aps;
1112 ul.unlock();
Andy Hungce3d1102024-09-29 21:47:19 -07001113 if (aps) onNewService(aps);
1114 return OK;
1115 }
1116
1117 static void disableThreadPoolStart() {
1118 mDisableThreadPoolStart = true;
1119 }
1120
1121 // called to determine error on nullptr service return.
1122 static constexpr status_t getError() {
1123 return DEAD_OBJECT;
1124 }
1125private:
1126
1127 static inline constinit std::mutex mMutex;
1128 static inline constinit sp<AudioSystem::AudioPolicyServiceClient> mClient GUARDED_BY(mMutex);
1129 static inline constinit sp<IAudioPolicyService> mService GUARDED_BY(mMutex);
1130 static inline constinit bool mValid GUARDED_BY(mMutex) = false;
Andy Hunga5af5072024-11-11 16:15:10 -08001131 static inline constinit std::chrono::milliseconds mWaitMs
1132 GUARDED_BY(mMutex) {kServiceClientWaitMs};
Andy Hungce3d1102024-09-29 21:47:19 -07001133 static inline constinit std::atomic_bool mDisableThreadPoolStart = false;
Andy Hung88e71732024-02-21 16:44:27 -08001134};
1135
Andy Hung88e71732024-02-21 16:44:27 -08001136
1137sp<IAudioPolicyService> AudioSystem::get_audio_policy_service() {
Andy Hungce3d1102024-09-29 21:47:19 -07001138 return AudioPolicyServiceTraits::getService();
Eric Laurentc2f1f072009-07-17 12:17:14 -07001139}
1140
Andy Hungce3d1102024-09-29 21:47:19 -07001141status_t AudioSystem::setLocalAudioPolicyService(const sp<IAudioPolicyService>& aps) {
1142 return AudioPolicyServiceTraits::setLocalService(aps);
1143}
1144
1145sp<AudioSystem::AudioPolicyServiceClient> AudioSystem::getAudioPolicyClient() {
1146 return AudioPolicyServiceTraits::getClient();
Mikhail Naganovec3d5792022-05-06 00:19:55 +00001147}
1148
Shraddha Basantwanif2aa2502024-04-08 11:01:57 +00001149void AudioSystem::disableThreadPool() {
Andy Hungce3d1102024-09-29 21:47:19 -07001150 AudioFlingerServiceTraits::disableThreadPoolStart();
1151 AudioPolicyServiceTraits::disableThreadPoolStart();
Shraddha Basantwanif2aa2502024-04-08 11:01:57 +00001152}
1153
Glenn Kastenfb1fdc92013-07-10 17:03:19 -07001154// ---------------------------------------------------------------------------
1155
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001156void AudioSystem::onNewAudioModulesAvailable() {
Andy Hung264fa4c2024-02-21 15:52:12 -08001157 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Andy Hungce3d1102024-09-29 21:47:19 -07001158 if (aps == nullptr) return;
Mikhail Naganov88b30d22020-03-09 19:43:13 +00001159 aps->onNewAudioModulesAvailable();
1160}
1161
Nathalie Le Clair88fa2752021-11-23 13:03:41 +01001162status_t AudioSystem::setDeviceConnectionState(audio_policy_dev_state_t state,
1163 const android::media::audio::common::AudioPort& port,
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001164 audio_format_t encodedFormat) {
Andy Hung264fa4c2024-02-21 15:52:12 -08001165 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Eric Laurent71b63e32011-09-02 14:20:56 -07001166
Andy Hungce3d1102024-09-29 21:47:19 -07001167 if (aps == nullptr) return AudioPolicyServiceTraits::getError();
Eric Laurentc2f1f072009-07-17 12:17:14 -07001168
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001169 return statusTFromBinderStatus(
1170 aps->setDeviceConnectionState(
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001171 VALUE_OR_RETURN_STATUS(
1172 legacy2aidl_audio_policy_dev_state_t_AudioPolicyDeviceState(state)),
Nathalie Le Clair88fa2752021-11-23 13:03:41 +01001173 port,
Mikhail Naganovb60bd1b2021-07-15 17:31:43 -07001174 VALUE_OR_RETURN_STATUS(
1175 legacy2aidl_audio_format_t_AudioFormatDescription(encodedFormat))));
Eric Laurentc2f1f072009-07-17 12:17:14 -07001176}
1177
Dima Zavinfce7a472011-04-19 22:30:36 -07001178audio_policy_dev_state_t AudioSystem::getDeviceConnectionState(audio_devices_t device,
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001179 const char* device_address) {
Andy Hung264fa4c2024-02-21 15:52:12 -08001180 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Andy Hungce3d1102024-09-29 21:47:19 -07001181 if (aps == nullptr) return AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE;
Eric Laurentc2f1f072009-07-17 12:17:14 -07001182
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001183 auto result = [&]() -> ConversionResult<audio_policy_dev_state_t> {
Mikhail Naganov932cb962021-09-16 01:05:27 +00001184 AudioDevice deviceAidl = VALUE_OR_RETURN(
1185 legacy2aidl_audio_device_AudioDevice(device, device_address));
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001186
1187 media::AudioPolicyDeviceState result;
1188 RETURN_IF_ERROR(statusTFromBinderStatus(
1189 aps->getDeviceConnectionState(deviceAidl, &result)));
1190
1191 return aidl2legacy_AudioPolicyDeviceState_audio_policy_dev_state_t(result);
1192 }();
1193 return result.value_or(AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE);
Eric Laurentc2f1f072009-07-17 12:17:14 -07001194}
1195
Pavlin Radoslavovf862bc62016-12-26 18:57:22 -08001196status_t AudioSystem::handleDeviceConfigChange(audio_devices_t device,
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001197 const char* device_address,
1198 const char* device_name,
1199 audio_format_t encodedFormat) {
Andy Hung264fa4c2024-02-21 15:52:12 -08001200 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001201 const char* address = "";
1202 const char* name = "";
Pavlin Radoslavovf862bc62016-12-26 18:57:22 -08001203
Andy Hungce3d1102024-09-29 21:47:19 -07001204 if (aps == nullptr) return AudioPolicyServiceTraits::getError();
Pavlin Radoslavovf862bc62016-12-26 18:57:22 -08001205
1206 if (device_address != NULL) {
1207 address = device_address;
1208 }
1209 if (device_name != NULL) {
1210 name = device_name;
1211 }
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001212
Mikhail Naganov932cb962021-09-16 01:05:27 +00001213 AudioDevice deviceAidl = VALUE_OR_RETURN_STATUS(
1214 legacy2aidl_audio_device_AudioDevice(device, address));
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001215
1216 return statusTFromBinderStatus(
1217 aps->handleDeviceConfigChange(deviceAidl, name, VALUE_OR_RETURN_STATUS(
Mikhail Naganovb60bd1b2021-07-15 17:31:43 -07001218 legacy2aidl_audio_format_t_AudioFormatDescription(encodedFormat))));
Pavlin Radoslavovf862bc62016-12-26 18:57:22 -08001219}
1220
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001221status_t AudioSystem::setPhoneState(audio_mode_t state, uid_t uid) {
Glenn Kasten347966c2012-01-18 14:58:32 -08001222 if (uint32_t(state) >= AUDIO_MODE_CNT) return BAD_VALUE;
Andy Hung264fa4c2024-02-21 15:52:12 -08001223 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Andy Hungce3d1102024-09-29 21:47:19 -07001224 if (aps == nullptr) return AudioPolicyServiceTraits::getError();
Eric Laurentc2f1f072009-07-17 12:17:14 -07001225
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001226 return statusTFromBinderStatus(aps->setPhoneState(
1227 VALUE_OR_RETURN_STATUS(legacy2aidl_audio_mode_t_AudioMode(state)),
1228 VALUE_OR_RETURN_STATUS(legacy2aidl_uid_t_int32_t(uid))));
Eric Laurentc2f1f072009-07-17 12:17:14 -07001229}
1230
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001231status_t
1232AudioSystem::setForceUse(audio_policy_force_use_t usage, audio_policy_forced_cfg_t config) {
Andy Hung264fa4c2024-02-21 15:52:12 -08001233 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Andy Hungce3d1102024-09-29 21:47:19 -07001234 if (aps == nullptr) return AudioPolicyServiceTraits::getError();
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001235
1236 return statusTFromBinderStatus(
1237 aps->setForceUse(
1238 VALUE_OR_RETURN_STATUS(
1239 legacy2aidl_audio_policy_force_use_t_AudioPolicyForceUse(usage)),
1240 VALUE_OR_RETURN_STATUS(
1241 legacy2aidl_audio_policy_forced_cfg_t_AudioPolicyForcedConfig(
1242 config))));
Eric Laurentc2f1f072009-07-17 12:17:14 -07001243}
1244
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001245audio_policy_forced_cfg_t AudioSystem::getForceUse(audio_policy_force_use_t usage) {
Andy Hung264fa4c2024-02-21 15:52:12 -08001246 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Andy Hungce3d1102024-09-29 21:47:19 -07001247 if (aps == nullptr) return AUDIO_POLICY_FORCE_NONE;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001248
1249 auto result = [&]() -> ConversionResult<audio_policy_forced_cfg_t> {
Mikhail Naganovd8670042024-11-20 21:17:52 +00001250 media::AudioPolicyForceUse usageAidl = VALUE_OR_RETURN(
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001251 legacy2aidl_audio_policy_force_use_t_AudioPolicyForceUse(usage));
Mikhail Naganovd8670042024-11-20 21:17:52 +00001252 media::AudioPolicyForcedConfig configAidl;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001253 RETURN_IF_ERROR(statusTFromBinderStatus(
1254 aps->getForceUse(usageAidl, &configAidl)));
1255 return aidl2legacy_AudioPolicyForcedConfig_audio_policy_forced_cfg_t(configAidl);
1256 }();
1257
1258 return result.value_or(AUDIO_POLICY_FORCE_NONE);
Eric Laurentc2f1f072009-07-17 12:17:14 -07001259}
1260
1261
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001262audio_io_handle_t AudioSystem::getOutput(audio_stream_type_t stream) {
Andy Hung264fa4c2024-02-21 15:52:12 -08001263 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Andy Hungce3d1102024-09-29 21:47:19 -07001264 if (aps == nullptr) return AUDIO_IO_HANDLE_NONE;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001265
1266 auto result = [&]() -> ConversionResult<audio_io_handle_t> {
Mikhail Naganovdbf03642021-08-25 18:15:32 -07001267 AudioStreamType streamAidl = VALUE_OR_RETURN(
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001268 legacy2aidl_audio_stream_type_t_AudioStreamType(stream));
1269 int32_t outputAidl;
1270 RETURN_IF_ERROR(
1271 statusTFromBinderStatus(aps->getOutput(streamAidl, &outputAidl)));
1272 return aidl2legacy_int32_t_audio_io_handle_t(outputAidl);
1273 }();
1274
1275 return result.value_or(AUDIO_IO_HANDLE_NONE);
Eric Laurentc2f1f072009-07-17 12:17:14 -07001276}
1277
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001278status_t AudioSystem::getOutputForAttr(audio_attributes_t* attr,
1279 audio_io_handle_t* output,
1280 audio_session_t session,
1281 audio_stream_type_t* stream,
Svet Ganov3e5f14f2021-05-13 22:51:08 +00001282 const AttributionSourceState& attributionSource,
jiabinf1c73972022-04-14 16:28:52 -07001283 audio_config_t* config,
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001284 audio_output_flags_t flags,
Robert Wufb971192024-10-30 21:54:35 +00001285 DeviceIdVector* selectedDeviceIds,
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001286 audio_port_handle_t* portId,
Eric Laurentb0a7bc92022-04-05 15:06:08 +02001287 std::vector<audio_io_handle_t>* secondaryOutputs,
jiabinc658e452022-10-21 20:52:21 +00001288 bool *isSpatialized,
Andy Hung6b137d12024-08-27 22:35:17 +00001289 bool *isBitPerfect,
Vlad Popa1e865e62024-08-15 19:11:42 -07001290 float *volume,
1291 bool *muted) {
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001292 if (attr == nullptr) {
1293 ALOGE("%s NULL audio attributes", __func__);
1294 return BAD_VALUE;
1295 }
1296 if (output == nullptr) {
1297 ALOGE("%s NULL output - shouldn't happen", __func__);
1298 return BAD_VALUE;
1299 }
Robert Wufb971192024-10-30 21:54:35 +00001300 if (selectedDeviceIds == nullptr) {
1301 ALOGE("%s NULL selectedDeviceIds - shouldn't happen", __func__);
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001302 return BAD_VALUE;
1303 }
1304 if (portId == nullptr) {
1305 ALOGE("%s NULL portId - shouldn't happen", __func__);
1306 return BAD_VALUE;
1307 }
1308 if (secondaryOutputs == nullptr) {
1309 ALOGE("%s NULL secondaryOutputs - shouldn't happen", __func__);
1310 return BAD_VALUE;
1311 }
1312
Andy Hung264fa4c2024-02-21 15:52:12 -08001313 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Andy Hungce3d1102024-09-29 21:47:19 -07001314 if (aps == nullptr) return NO_INIT;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001315
Mikhail Naganov1c400902023-05-17 11:48:43 -07001316 media::audio::common::AudioAttributes attrAidl = VALUE_OR_RETURN_STATUS(
1317 legacy2aidl_audio_attributes_t_AudioAttributes(*attr));
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001318 int32_t sessionAidl = VALUE_OR_RETURN_STATUS(legacy2aidl_audio_session_t_int32_t(session));
Mikhail Naganovdbf03642021-08-25 18:15:32 -07001319 AudioConfig configAidl = VALUE_OR_RETURN_STATUS(
Mikhail Naganovde3fa182021-07-30 15:06:42 -07001320 legacy2aidl_audio_config_t_AudioConfig(*config, false /*isInput*/));
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001321 int32_t flagsAidl = VALUE_OR_RETURN_STATUS(
1322 legacy2aidl_audio_output_flags_t_int32_t_mask(flags));
Robert Wufb971192024-10-30 21:54:35 +00001323 auto selectedDeviceIdsAidl = VALUE_OR_RETURN_STATUS(convertContainer<std::vector<int32_t>>(
1324 *selectedDeviceIds, legacy2aidl_audio_port_handle_t_int32_t));
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001325
1326 media::GetOutputForAttrResponse responseAidl;
1327
jiabinf1c73972022-04-14 16:28:52 -07001328 status_t status = statusTFromBinderStatus(
Svet Ganov3e5f14f2021-05-13 22:51:08 +00001329 aps->getOutputForAttr(attrAidl, sessionAidl, attributionSource, configAidl, flagsAidl,
Robert Wufb971192024-10-30 21:54:35 +00001330 selectedDeviceIdsAidl, &responseAidl));
jiabinf1c73972022-04-14 16:28:52 -07001331 if (status != NO_ERROR) {
1332 config->format = VALUE_OR_RETURN_STATUS(
Robert Wufb971192024-10-30 21:54:35 +00001333 aidl2legacy_AudioFormatDescription_audio_format_t(responseAidl.configBase.format));
jiabinf1c73972022-04-14 16:28:52 -07001334 config->channel_mask = VALUE_OR_RETURN_STATUS(
Robert Wufb971192024-10-30 21:54:35 +00001335 aidl2legacy_AudioChannelLayout_audio_channel_mask_t(
1336 responseAidl.configBase.channelMask, false /*isInput*/));
jiabinf1c73972022-04-14 16:28:52 -07001337 config->sample_rate = responseAidl.configBase.sampleRate;
1338 return status;
1339 }
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001340
1341 *output = VALUE_OR_RETURN_STATUS(
1342 aidl2legacy_int32_t_audio_io_handle_t(responseAidl.output));
1343
1344 if (stream != nullptr) {
1345 *stream = VALUE_OR_RETURN_STATUS(
1346 aidl2legacy_AudioStreamType_audio_stream_type_t(responseAidl.stream));
1347 }
Robert Wufb971192024-10-30 21:54:35 +00001348 *selectedDeviceIds = VALUE_OR_RETURN_STATUS(convertContainer<DeviceIdVector>(
1349 responseAidl.selectedDeviceIds, aidl2legacy_int32_t_audio_port_handle_t));
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001350 *portId = VALUE_OR_RETURN_STATUS(aidl2legacy_int32_t_audio_port_handle_t(responseAidl.portId));
1351 *secondaryOutputs = VALUE_OR_RETURN_STATUS(convertContainer<std::vector<audio_io_handle_t>>(
1352 responseAidl.secondaryOutputs, aidl2legacy_int32_t_audio_io_handle_t));
Eric Laurentb0a7bc92022-04-05 15:06:08 +02001353 *isSpatialized = responseAidl.isSpatialized;
jiabinc658e452022-10-21 20:52:21 +00001354 *isBitPerfect = responseAidl.isBitPerfect;
Eric Laurent0d13fea2022-11-04 17:12:08 +01001355 *attr = VALUE_OR_RETURN_STATUS(
Mikhail Naganov1c400902023-05-17 11:48:43 -07001356 aidl2legacy_AudioAttributes_audio_attributes_t(responseAidl.attr));
Andy Hung6b137d12024-08-27 22:35:17 +00001357 *volume = responseAidl.volume;
Vlad Popa1e865e62024-08-15 19:11:42 -07001358 *muted = responseAidl.muted;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001359
1360 return OK;
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -07001361}
1362
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001363status_t AudioSystem::startOutput(audio_port_handle_t portId) {
Andy Hung264fa4c2024-02-21 15:52:12 -08001364 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Andy Hungce3d1102024-09-29 21:47:19 -07001365 if (aps == nullptr) return AudioPolicyServiceTraits::getError();
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001366
1367 int32_t portIdAidl = VALUE_OR_RETURN_STATUS(legacy2aidl_audio_port_handle_t_int32_t(portId));
1368 return statusTFromBinderStatus(aps->startOutput(portIdAidl));
Eric Laurentc2f1f072009-07-17 12:17:14 -07001369}
1370
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001371status_t AudioSystem::stopOutput(audio_port_handle_t portId) {
Andy Hung264fa4c2024-02-21 15:52:12 -08001372 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Andy Hungce3d1102024-09-29 21:47:19 -07001373 if (aps == nullptr) return AudioPolicyServiceTraits::getError();
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001374
1375 int32_t portIdAidl = VALUE_OR_RETURN_STATUS(legacy2aidl_audio_port_handle_t_int32_t(portId));
1376 return statusTFromBinderStatus(aps->stopOutput(portIdAidl));
Eric Laurentc2f1f072009-07-17 12:17:14 -07001377}
1378
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001379void AudioSystem::releaseOutput(audio_port_handle_t portId) {
Andy Hung264fa4c2024-02-21 15:52:12 -08001380 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Andy Hungce3d1102024-09-29 21:47:19 -07001381 if (aps == nullptr) return;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001382
1383 auto status = [&]() -> status_t {
1384 int32_t portIdAidl = VALUE_OR_RETURN_STATUS(
1385 legacy2aidl_audio_port_handle_t_int32_t(portId));
1386 RETURN_STATUS_IF_ERROR(statusTFromBinderStatus(aps->releaseOutput(portIdAidl)));
1387 return OK;
1388 }();
1389
1390 // Ignore status.
1391 (void) status;
Eric Laurentc2f1f072009-07-17 12:17:14 -07001392}
1393
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001394status_t AudioSystem::getInputForAttr(const audio_attributes_t* attr,
1395 audio_io_handle_t* input,
1396 audio_unique_id_t riid,
1397 audio_session_t session,
Svet Ganov3e5f14f2021-05-13 22:51:08 +00001398 const AttributionSourceState &attributionSource,
jiabinf1c73972022-04-14 16:28:52 -07001399 audio_config_base_t* config,
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001400 audio_input_flags_t flags,
1401 audio_port_handle_t* selectedDeviceId,
1402 audio_port_handle_t* portId) {
1403 if (attr == NULL) {
1404 ALOGE("getInputForAttr NULL attr - shouldn't happen");
1405 return BAD_VALUE;
1406 }
1407 if (input == NULL) {
1408 ALOGE("getInputForAttr NULL input - shouldn't happen");
1409 return BAD_VALUE;
1410 }
1411 if (selectedDeviceId == NULL) {
1412 ALOGE("getInputForAttr NULL selectedDeviceId - shouldn't happen");
1413 return BAD_VALUE;
1414 }
1415 if (portId == NULL) {
1416 ALOGE("getInputForAttr NULL portId - shouldn't happen");
1417 return BAD_VALUE;
1418 }
1419
Andy Hung264fa4c2024-02-21 15:52:12 -08001420 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Andy Hungce3d1102024-09-29 21:47:19 -07001421 if (aps == nullptr) return NO_INIT;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001422
Mikhail Naganov1c400902023-05-17 11:48:43 -07001423 media::audio::common::AudioAttributes attrAidl = VALUE_OR_RETURN_STATUS(
1424 legacy2aidl_audio_attributes_t_AudioAttributes(*attr));
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001425 int32_t inputAidl = VALUE_OR_RETURN_STATUS(legacy2aidl_audio_io_handle_t_int32_t(*input));
1426 int32_t riidAidl = VALUE_OR_RETURN_STATUS(legacy2aidl_audio_unique_id_t_int32_t(riid));
1427 int32_t sessionAidl = VALUE_OR_RETURN_STATUS(legacy2aidl_audio_session_t_int32_t(session));
Mikhail Naganovdbf03642021-08-25 18:15:32 -07001428 AudioConfigBase configAidl = VALUE_OR_RETURN_STATUS(
Mikhail Naganovde3fa182021-07-30 15:06:42 -07001429 legacy2aidl_audio_config_base_t_AudioConfigBase(*config, true /*isInput*/));
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001430 int32_t flagsAidl = VALUE_OR_RETURN_STATUS(legacy2aidl_audio_input_flags_t_int32_t_mask(flags));
Eric Laurentf99edd32021-02-01 15:57:33 +01001431 int32_t selectedDeviceIdAidl = VALUE_OR_RETURN_STATUS(
1432 legacy2aidl_audio_port_handle_t_int32_t(*selectedDeviceId));
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001433
1434 media::GetInputForAttrResponse response;
1435
Atneya Nair89ff0432024-11-19 12:47:28 -08001436 const Status res = aps->getInputForAttr(attrAidl, inputAidl, riidAidl, sessionAidl,
1437 attributionSource, configAidl, flagsAidl,
1438 selectedDeviceIdAidl, &response);
1439 if (!res.isOk()) {
1440 ALOGE("getInputForAttr error: %s", res.toString8().c_str());
jiabinf1c73972022-04-14 16:28:52 -07001441 *config = VALUE_OR_RETURN_STATUS(
1442 aidl2legacy_AudioConfigBase_audio_config_base_t(response.config, true /*isInput*/));
Atneya Nair89ff0432024-11-19 12:47:28 -08001443 return statusTFromBinderStatus(res);
jiabinf1c73972022-04-14 16:28:52 -07001444 }
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001445
1446 *input = VALUE_OR_RETURN_STATUS(aidl2legacy_int32_t_audio_io_handle_t(response.input));
1447 *selectedDeviceId = VALUE_OR_RETURN_STATUS(
1448 aidl2legacy_int32_t_audio_port_handle_t(response.selectedDeviceId));
1449 *portId = VALUE_OR_RETURN_STATUS(aidl2legacy_int32_t_audio_port_handle_t(response.portId));
1450
1451 return OK;
Eric Laurentc2f1f072009-07-17 12:17:14 -07001452}
1453
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001454status_t AudioSystem::startInput(audio_port_handle_t portId) {
Andy Hung264fa4c2024-02-21 15:52:12 -08001455 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Andy Hungce3d1102024-09-29 21:47:19 -07001456 if (aps == nullptr) return AudioPolicyServiceTraits::getError();
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001457
1458 int32_t portIdAidl = VALUE_OR_RETURN_STATUS(legacy2aidl_audio_port_handle_t_int32_t(portId));
1459 return statusTFromBinderStatus(aps->startInput(portIdAidl));
Eric Laurentc2f1f072009-07-17 12:17:14 -07001460}
1461
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001462status_t AudioSystem::stopInput(audio_port_handle_t portId) {
Andy Hung264fa4c2024-02-21 15:52:12 -08001463 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Andy Hungce3d1102024-09-29 21:47:19 -07001464 if (aps == nullptr) return AudioPolicyServiceTraits::getError();
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001465
1466 int32_t portIdAidl = VALUE_OR_RETURN_STATUS(legacy2aidl_audio_port_handle_t_int32_t(portId));
1467 return statusTFromBinderStatus(aps->stopInput(portIdAidl));
Eric Laurentc2f1f072009-07-17 12:17:14 -07001468}
1469
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001470void AudioSystem::releaseInput(audio_port_handle_t portId) {
Andy Hung264fa4c2024-02-21 15:52:12 -08001471 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Andy Hungce3d1102024-09-29 21:47:19 -07001472 if (aps == nullptr) return;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001473
1474 auto status = [&]() -> status_t {
1475 int32_t portIdAidl = VALUE_OR_RETURN_STATUS(
1476 legacy2aidl_audio_port_handle_t_int32_t(portId));
1477 RETURN_STATUS_IF_ERROR(statusTFromBinderStatus(aps->releaseInput(portIdAidl)));
1478 return OK;
1479 }();
1480
1481 // Ignore status.
1482 (void) status;
Eric Laurentc2f1f072009-07-17 12:17:14 -07001483}
1484
Vlad Popa87e0e582024-05-20 18:49:20 -07001485status_t AudioSystem::setDeviceAbsoluteVolumeEnabled(audio_devices_t deviceType,
1486 const char *address,
1487 bool enabled,
1488 audio_stream_type_t streamToDriveAbs) {
1489 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Andy Hungce3d1102024-09-29 21:47:19 -07001490 if (aps == nullptr) return AudioPolicyServiceTraits::getError();
Vlad Popa87e0e582024-05-20 18:49:20 -07001491
1492 AudioDevice deviceAidl = VALUE_OR_RETURN_STATUS(
1493 legacy2aidl_audio_device_AudioDevice(deviceType, address));
1494 AudioStreamType streamToDriveAbsAidl = VALUE_OR_RETURN_STATUS(
1495 legacy2aidl_audio_stream_type_t_AudioStreamType(streamToDriveAbs));
1496 return statusTFromBinderStatus(
1497 aps->setDeviceAbsoluteVolumeEnabled(deviceAidl, enabled, streamToDriveAbsAidl));
1498}
1499
Dima Zavinfce7a472011-04-19 22:30:36 -07001500status_t AudioSystem::initStreamVolume(audio_stream_type_t stream,
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001501 int indexMin,
1502 int indexMax) {
Andy Hung264fa4c2024-02-21 15:52:12 -08001503 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Andy Hungce3d1102024-09-29 21:47:19 -07001504 if (aps == nullptr) return AudioPolicyServiceTraits::getError();
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001505
Mikhail Naganovdbf03642021-08-25 18:15:32 -07001506 AudioStreamType streamAidl = VALUE_OR_RETURN_STATUS(
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001507 legacy2aidl_audio_stream_type_t_AudioStreamType(stream));
1508 int32_t indexMinAidl = VALUE_OR_RETURN_STATUS(convertIntegral<int32_t>(indexMin));
1509 int32_t indexMaxAidl = VALUE_OR_RETURN_STATUS(convertIntegral<int32_t>(indexMax));
Mikhail Naganovec3d5792022-05-06 00:19:55 +00001510 status_t status = statusTFromBinderStatus(
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001511 aps->initStreamVolume(streamAidl, indexMinAidl, indexMaxAidl));
Mikhail Naganovec3d5792022-05-06 00:19:55 +00001512 return status;
Eric Laurentc2f1f072009-07-17 12:17:14 -07001513}
1514
Eric Laurent83844cc2011-11-18 16:43:31 -08001515status_t AudioSystem::setStreamVolumeIndex(audio_stream_type_t stream,
1516 int index,
Vlad Popa1e865e62024-08-15 19:11:42 -07001517 bool muted,
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001518 audio_devices_t device) {
Andy Hung264fa4c2024-02-21 15:52:12 -08001519 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Andy Hungce3d1102024-09-29 21:47:19 -07001520 if (aps == nullptr) return AudioPolicyServiceTraits::getError();
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001521
Mikhail Naganovdbf03642021-08-25 18:15:32 -07001522 AudioStreamType streamAidl = VALUE_OR_RETURN_STATUS(
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001523 legacy2aidl_audio_stream_type_t_AudioStreamType(stream));
1524 int32_t indexAidl = VALUE_OR_RETURN_STATUS(convertIntegral<int32_t>(index));
Mikhail Naganovf4a75362021-09-16 00:02:54 +00001525 AudioDeviceDescription deviceAidl = VALUE_OR_RETURN_STATUS(
Mikhail Naganov21a32ec2021-07-08 14:40:12 -07001526 legacy2aidl_audio_devices_t_AudioDeviceDescription(device));
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001527 return statusTFromBinderStatus(
Vlad Popa1e865e62024-08-15 19:11:42 -07001528 aps->setStreamVolumeIndex(streamAidl, deviceAidl, indexAidl, muted));
Eric Laurentc2f1f072009-07-17 12:17:14 -07001529}
1530
Eric Laurent83844cc2011-11-18 16:43:31 -08001531status_t AudioSystem::getStreamVolumeIndex(audio_stream_type_t stream,
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001532 int* index,
1533 audio_devices_t device) {
Andy Hung264fa4c2024-02-21 15:52:12 -08001534 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Andy Hungce3d1102024-09-29 21:47:19 -07001535 if (aps == nullptr) return AudioPolicyServiceTraits::getError();
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001536
Mikhail Naganovdbf03642021-08-25 18:15:32 -07001537 AudioStreamType streamAidl = VALUE_OR_RETURN_STATUS(
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001538 legacy2aidl_audio_stream_type_t_AudioStreamType(stream));
Mikhail Naganovf4a75362021-09-16 00:02:54 +00001539 AudioDeviceDescription deviceAidl = VALUE_OR_RETURN_STATUS(
Mikhail Naganov21a32ec2021-07-08 14:40:12 -07001540 legacy2aidl_audio_devices_t_AudioDeviceDescription(device));
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001541 int32_t indexAidl;
1542 RETURN_STATUS_IF_ERROR(statusTFromBinderStatus(
1543 aps->getStreamVolumeIndex(streamAidl, deviceAidl, &indexAidl)));
1544 if (index != nullptr) {
1545 *index = VALUE_OR_RETURN_STATUS(convertIntegral<int>(indexAidl));
1546 }
1547 return OK;
Eric Laurentc2f1f072009-07-17 12:17:14 -07001548}
1549
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001550status_t AudioSystem::setVolumeIndexForAttributes(const audio_attributes_t& attr,
François Gaffiecfe17322018-11-07 13:41:29 +01001551 int index,
Vlad Popa1e865e62024-08-15 19:11:42 -07001552 bool muted,
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001553 audio_devices_t device) {
Andy Hung264fa4c2024-02-21 15:52:12 -08001554 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Andy Hungce3d1102024-09-29 21:47:19 -07001555 if (aps == nullptr) return AudioPolicyServiceTraits::getError();
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001556
Mikhail Naganov1c400902023-05-17 11:48:43 -07001557 media::audio::common::AudioAttributes attrAidl = VALUE_OR_RETURN_STATUS(
1558 legacy2aidl_audio_attributes_t_AudioAttributes(attr));
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001559 int32_t indexAidl = VALUE_OR_RETURN_STATUS(convertIntegral<int32_t>(index));
Mikhail Naganovf4a75362021-09-16 00:02:54 +00001560 AudioDeviceDescription deviceAidl = VALUE_OR_RETURN_STATUS(
Mikhail Naganov21a32ec2021-07-08 14:40:12 -07001561 legacy2aidl_audio_devices_t_AudioDeviceDescription(device));
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001562 return statusTFromBinderStatus(
Vlad Popa1e865e62024-08-15 19:11:42 -07001563 aps->setVolumeIndexForAttributes(attrAidl, deviceAidl, indexAidl, muted));
François Gaffiecfe17322018-11-07 13:41:29 +01001564}
1565
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001566status_t AudioSystem::getVolumeIndexForAttributes(const audio_attributes_t& attr,
1567 int& index,
1568 audio_devices_t device) {
Andy Hung264fa4c2024-02-21 15:52:12 -08001569 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Andy Hungce3d1102024-09-29 21:47:19 -07001570 if (aps == nullptr) return AudioPolicyServiceTraits::getError();
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001571
Mikhail Naganov1c400902023-05-17 11:48:43 -07001572 media::audio::common::AudioAttributes attrAidl = VALUE_OR_RETURN_STATUS(
1573 legacy2aidl_audio_attributes_t_AudioAttributes(attr));
Mikhail Naganovf4a75362021-09-16 00:02:54 +00001574 AudioDeviceDescription deviceAidl = VALUE_OR_RETURN_STATUS(
Mikhail Naganov21a32ec2021-07-08 14:40:12 -07001575 legacy2aidl_audio_devices_t_AudioDeviceDescription(device));
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001576 int32_t indexAidl;
1577 RETURN_STATUS_IF_ERROR(statusTFromBinderStatus(
1578 aps->getVolumeIndexForAttributes(attrAidl, deviceAidl, &indexAidl)));
1579 index = VALUE_OR_RETURN_STATUS(convertIntegral<int>(indexAidl));
1580 return OK;
François Gaffiecfe17322018-11-07 13:41:29 +01001581}
1582
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001583status_t AudioSystem::getMaxVolumeIndexForAttributes(const audio_attributes_t& attr, int& index) {
Andy Hung264fa4c2024-02-21 15:52:12 -08001584 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Andy Hungce3d1102024-09-29 21:47:19 -07001585 if (aps == nullptr) return AudioPolicyServiceTraits::getError();
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001586
Mikhail Naganov1c400902023-05-17 11:48:43 -07001587 media::audio::common::AudioAttributes attrAidl = VALUE_OR_RETURN_STATUS(
1588 legacy2aidl_audio_attributes_t_AudioAttributes(attr));
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001589 int32_t indexAidl;
1590 RETURN_STATUS_IF_ERROR(statusTFromBinderStatus(
1591 aps->getMaxVolumeIndexForAttributes(attrAidl, &indexAidl)));
1592 index = VALUE_OR_RETURN_STATUS(convertIntegral<int>(indexAidl));
1593 return OK;
François Gaffiecfe17322018-11-07 13:41:29 +01001594}
1595
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001596status_t AudioSystem::getMinVolumeIndexForAttributes(const audio_attributes_t& attr, int& index) {
Andy Hung264fa4c2024-02-21 15:52:12 -08001597 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Andy Hungce3d1102024-09-29 21:47:19 -07001598 if (aps == nullptr) return AudioPolicyServiceTraits::getError();
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001599
Mikhail Naganov1c400902023-05-17 11:48:43 -07001600 media::audio::common::AudioAttributes attrAidl = VALUE_OR_RETURN_STATUS(
1601 legacy2aidl_audio_attributes_t_AudioAttributes(attr));
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001602 int32_t indexAidl;
1603 RETURN_STATUS_IF_ERROR(statusTFromBinderStatus(
1604 aps->getMinVolumeIndexForAttributes(attrAidl, &indexAidl)));
1605 index = VALUE_OR_RETURN_STATUS(convertIntegral<int>(indexAidl));
1606 return OK;
François Gaffiecfe17322018-11-07 13:41:29 +01001607}
1608
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001609product_strategy_t AudioSystem::getStrategyForStream(audio_stream_type_t stream) {
Andy Hung264fa4c2024-02-21 15:52:12 -08001610 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Andy Hungce3d1102024-09-29 21:47:19 -07001611 if (aps == nullptr) return PRODUCT_STRATEGY_NONE;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001612
1613 auto result = [&]() -> ConversionResult<product_strategy_t> {
Mikhail Naganovdbf03642021-08-25 18:15:32 -07001614 AudioStreamType streamAidl = VALUE_OR_RETURN(
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001615 legacy2aidl_audio_stream_type_t_AudioStreamType(stream));
1616 int32_t resultAidl;
1617 RETURN_IF_ERROR(statusTFromBinderStatus(
1618 aps->getStrategyForStream(streamAidl, &resultAidl)));
1619 return aidl2legacy_int32_t_product_strategy_t(resultAidl);
1620 }();
1621 return result.value_or(PRODUCT_STRATEGY_NONE);
Eric Laurentde070132010-07-13 04:45:46 -07001622}
1623
François Gaffie1e2b56f2022-04-01 14:34:29 +02001624status_t AudioSystem::getDevicesForAttributes(const audio_attributes_t& aa,
Andy Hung6d23c0f2022-02-16 09:37:15 -08001625 AudioDeviceTypeAddrVector* devices,
1626 bool forVolume) {
Jean-Michel Trivif41599b2020-01-07 14:22:08 -08001627 if (devices == nullptr) {
1628 return BAD_VALUE;
1629 }
Andy Hung264fa4c2024-02-21 15:52:12 -08001630 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Andy Hungce3d1102024-09-29 21:47:19 -07001631 if (aps == nullptr) return AudioPolicyServiceTraits::getError();
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001632
Mikhail Naganov1c400902023-05-17 11:48:43 -07001633 media::audio::common::AudioAttributes aaAidl = VALUE_OR_RETURN_STATUS(
1634 legacy2aidl_audio_attributes_t_AudioAttributes(aa));
Mikhail Naganovf4a75362021-09-16 00:02:54 +00001635 std::vector<AudioDevice> retAidl;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001636 RETURN_STATUS_IF_ERROR(
Andy Hung6d23c0f2022-02-16 09:37:15 -08001637 statusTFromBinderStatus(aps->getDevicesForAttributes(aaAidl, forVolume, &retAidl)));
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001638 *devices = VALUE_OR_RETURN_STATUS(
1639 convertContainer<AudioDeviceTypeAddrVector>(
1640 retAidl,
1641 aidl2legacy_AudioDeviceTypeAddress));
1642 return OK;
Jean-Michel Trivif41599b2020-01-07 14:22:08 -08001643}
1644
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001645audio_io_handle_t AudioSystem::getOutputForEffect(const effect_descriptor_t* desc) {
Andy Hung264fa4c2024-02-21 15:52:12 -08001646 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Glenn Kastenefa6ea92014-01-08 09:10:43 -08001647 // FIXME change return type to status_t, and return PERMISSION_DENIED here
Andy Hungce3d1102024-09-29 21:47:19 -07001648 if (aps == nullptr) return AUDIO_IO_HANDLE_NONE;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001649
1650 auto result = [&]() -> ConversionResult<audio_io_handle_t> {
1651 media::EffectDescriptor descAidl = VALUE_OR_RETURN(
1652 legacy2aidl_effect_descriptor_t_EffectDescriptor(*desc));
1653 int32_t retAidl;
1654 RETURN_IF_ERROR(
1655 statusTFromBinderStatus(aps->getOutputForEffect(descAidl, &retAidl)));
1656 return aidl2legacy_int32_t_audio_io_handle_t(retAidl);
1657 }();
1658
1659 return result.value_or(AUDIO_IO_HANDLE_NONE);
Eric Laurentde070132010-07-13 04:45:46 -07001660}
1661
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001662status_t AudioSystem::registerEffect(const effect_descriptor_t* desc,
1663 audio_io_handle_t io,
1664 product_strategy_t strategy,
1665 audio_session_t session,
1666 int id) {
Andy Hung264fa4c2024-02-21 15:52:12 -08001667 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Andy Hungce3d1102024-09-29 21:47:19 -07001668 if (aps == nullptr) return AudioPolicyServiceTraits::getError();
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001669
1670 media::EffectDescriptor descAidl = VALUE_OR_RETURN_STATUS(
1671 legacy2aidl_effect_descriptor_t_EffectDescriptor(*desc));
1672 int32_t ioAidl = VALUE_OR_RETURN_STATUS(legacy2aidl_audio_io_handle_t_int32_t(io));
1673 int32_t strategyAidl = VALUE_OR_RETURN_STATUS(aidl2legacy_int32_t_product_strategy_t(strategy));
1674 int32_t sessionAidl = VALUE_OR_RETURN_STATUS(legacy2aidl_audio_session_t_int32_t(session));
1675 int32_t idAidl = VALUE_OR_RETURN_STATUS(convertReinterpret<int32_t>(id));
1676 return statusTFromBinderStatus(
1677 aps->registerEffect(descAidl, ioAidl, strategyAidl, sessionAidl, idAidl));
Eric Laurentde070132010-07-13 04:45:46 -07001678}
1679
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001680status_t AudioSystem::unregisterEffect(int id) {
Andy Hung264fa4c2024-02-21 15:52:12 -08001681 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Andy Hungce3d1102024-09-29 21:47:19 -07001682 if (aps == nullptr) return AudioPolicyServiceTraits::getError();
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001683
1684 int32_t idAidl = VALUE_OR_RETURN_STATUS(convertReinterpret<int32_t>(id));
1685 return statusTFromBinderStatus(
1686 aps->unregisterEffect(idAidl));
Eric Laurentde070132010-07-13 04:45:46 -07001687}
1688
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001689status_t AudioSystem::setEffectEnabled(int id, bool enabled) {
Andy Hung264fa4c2024-02-21 15:52:12 -08001690 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Andy Hungce3d1102024-09-29 21:47:19 -07001691 if (aps == nullptr) return AudioPolicyServiceTraits::getError();
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001692
1693 int32_t idAidl = VALUE_OR_RETURN_STATUS(convertReinterpret<int32_t>(id));
1694 return statusTFromBinderStatus(
1695 aps->setEffectEnabled(idAidl, enabled));
Eric Laurentdb7c0792011-08-10 10:37:50 -07001696}
1697
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001698status_t AudioSystem::moveEffectsToIo(const std::vector<int>& ids, audio_io_handle_t io) {
Andy Hung264fa4c2024-02-21 15:52:12 -08001699 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Andy Hungce3d1102024-09-29 21:47:19 -07001700 if (aps == nullptr) return AudioPolicyServiceTraits::getError();
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001701
1702 std::vector<int32_t> idsAidl = VALUE_OR_RETURN_STATUS(
1703 convertContainer<std::vector<int32_t>>(ids, convertReinterpret<int32_t, int>));
1704 int32_t ioAidl = VALUE_OR_RETURN_STATUS(legacy2aidl_audio_io_handle_t_int32_t(io));
1705 return statusTFromBinderStatus(aps->moveEffectsToIo(idsAidl, ioAidl));
Eric Laurent6c796322019-04-09 14:13:17 -07001706}
1707
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001708status_t AudioSystem::isStreamActive(audio_stream_type_t stream, bool* state, uint32_t inPastMs) {
Andy Hung264fa4c2024-02-21 15:52:12 -08001709 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Andy Hungce3d1102024-09-29 21:47:19 -07001710 if (aps == nullptr) return AudioPolicyServiceTraits::getError();
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001711 if (state == NULL) return BAD_VALUE;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001712
Mikhail Naganovdbf03642021-08-25 18:15:32 -07001713 AudioStreamType streamAidl = VALUE_OR_RETURN_STATUS(
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001714 legacy2aidl_audio_stream_type_t_AudioStreamType(stream));
1715 int32_t inPastMsAidl = VALUE_OR_RETURN_STATUS(convertIntegral<int32_t>(inPastMs));
1716 RETURN_STATUS_IF_ERROR(statusTFromBinderStatus(
1717 aps->isStreamActive(streamAidl, inPastMsAidl, state)));
1718 return OK;
Eric Laurenteda6c362011-02-02 09:33:30 -08001719}
1720
Jean-Michel Trivi272ab542013-02-04 16:26:02 -08001721status_t AudioSystem::isStreamActiveRemotely(audio_stream_type_t stream, bool* state,
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001722 uint32_t inPastMs) {
Andy Hung264fa4c2024-02-21 15:52:12 -08001723 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Andy Hungce3d1102024-09-29 21:47:19 -07001724 if (aps == nullptr) return AudioPolicyServiceTraits::getError();
Jean-Michel Trivi272ab542013-02-04 16:26:02 -08001725 if (state == NULL) return BAD_VALUE;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001726
Mikhail Naganovdbf03642021-08-25 18:15:32 -07001727 AudioStreamType streamAidl = VALUE_OR_RETURN_STATUS(
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001728 legacy2aidl_audio_stream_type_t_AudioStreamType(stream));
1729 int32_t inPastMsAidl = VALUE_OR_RETURN_STATUS(convertIntegral<int32_t>(inPastMs));
1730 RETURN_STATUS_IF_ERROR(statusTFromBinderStatus(
1731 aps->isStreamActiveRemotely(streamAidl, inPastMsAidl, state)));
1732 return OK;
Jean-Michel Trivi272ab542013-02-04 16:26:02 -08001733}
1734
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001735status_t AudioSystem::isSourceActive(audio_source_t stream, bool* state) {
Andy Hung264fa4c2024-02-21 15:52:12 -08001736 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Andy Hungce3d1102024-09-29 21:47:19 -07001737 if (aps == nullptr) return AudioPolicyServiceTraits::getError();
Jean-Michel Trivid7086032012-10-10 12:11:16 -07001738 if (state == NULL) return BAD_VALUE;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001739
Mikhail Naganovddceecc2021-09-03 13:58:56 -07001740 AudioSource streamAidl = VALUE_OR_RETURN_STATUS(
1741 legacy2aidl_audio_source_t_AudioSource(stream));
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001742 RETURN_STATUS_IF_ERROR(statusTFromBinderStatus(
1743 aps->isSourceActive(streamAidl, state)));
1744 return OK;
Jean-Michel Trivid7086032012-10-10 12:11:16 -07001745}
1746
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001747uint32_t AudioSystem::getPrimaryOutputSamplingRate() {
Andy Hung264fa4c2024-02-21 15:52:12 -08001748 const sp<IAudioFlinger> af = get_audio_flinger();
Andy Hungce3d1102024-09-29 21:47:19 -07001749 if (af == nullptr) return 0;
Glenn Kastencc0f1cf2012-09-24 11:27:18 -07001750 return af->getPrimaryOutputSamplingRate();
1751}
1752
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001753size_t AudioSystem::getPrimaryOutputFrameCount() {
Andy Hung264fa4c2024-02-21 15:52:12 -08001754 const sp<IAudioFlinger> af = get_audio_flinger();
Andy Hungce3d1102024-09-29 21:47:19 -07001755 if (af == nullptr) return 0;
Glenn Kastencc0f1cf2012-09-24 11:27:18 -07001756 return af->getPrimaryOutputFrameCount();
1757}
Eric Laurenteda6c362011-02-02 09:33:30 -08001758
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001759status_t AudioSystem::setLowRamDevice(bool isLowRamDevice, int64_t totalMemory) {
Andy Hung264fa4c2024-02-21 15:52:12 -08001760 const sp<IAudioFlinger> af = get_audio_flinger();
Andy Hungce3d1102024-09-29 21:47:19 -07001761 if (af == nullptr) return AudioFlingerServiceTraits::getError();
Andy Hung6f248bb2018-01-23 14:04:37 -08001762 return af->setLowRamDevice(isLowRamDevice, totalMemory);
Glenn Kasten4182c4e2013-07-15 14:45:07 -07001763}
1764
Hayden Gomes524159d2019-12-23 14:41:47 -08001765status_t AudioSystem::setSupportedSystemUsages(const std::vector<audio_usage_t>& systemUsages) {
Andy Hung264fa4c2024-02-21 15:52:12 -08001766 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Andy Hungce3d1102024-09-29 21:47:19 -07001767 if (aps == nullptr) return AudioPolicyServiceTraits::getError();
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001768
Mikhail Naganovdbf03642021-08-25 18:15:32 -07001769 std::vector<AudioUsage> systemUsagesAidl = VALUE_OR_RETURN_STATUS(
1770 convertContainer<std::vector<AudioUsage>>(systemUsages,
1771 legacy2aidl_audio_usage_t_AudioUsage));
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001772 return statusTFromBinderStatus(aps->setSupportedSystemUsages(systemUsagesAidl));
Hayden Gomes524159d2019-12-23 14:41:47 -08001773}
1774
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001775status_t AudioSystem::setAllowedCapturePolicy(uid_t uid, audio_flags_mask_t capturePolicy) {
Andy Hung264fa4c2024-02-21 15:52:12 -08001776 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Andy Hungce3d1102024-09-29 21:47:19 -07001777 if (aps == nullptr) return AudioPolicyServiceTraits::getError();
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001778
1779 int32_t uidAidl = VALUE_OR_RETURN_STATUS(legacy2aidl_uid_t_int32_t(uid));
1780 int32_t capturePolicyAidl = VALUE_OR_RETURN_STATUS(
1781 legacy2aidl_audio_flags_mask_t_int32_t_mask(capturePolicy));
1782 return statusTFromBinderStatus(aps->setAllowedCapturePolicy(uidAidl, capturePolicyAidl));
Kevin Rocardb99cc752019-03-21 20:52:24 -07001783}
1784
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001785audio_offload_mode_t AudioSystem::getOffloadSupport(const audio_offload_info_t& info) {
Eric Laurent90fe31c2020-11-26 20:06:35 +01001786 ALOGV("%s", __func__);
Andy Hung264fa4c2024-02-21 15:52:12 -08001787 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Andy Hungce3d1102024-09-29 21:47:19 -07001788 if (aps == nullptr) return AUDIO_OFFLOAD_NOT_SUPPORTED;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001789
1790 auto result = [&]() -> ConversionResult<audio_offload_mode_t> {
Mikhail Naganovdbf03642021-08-25 18:15:32 -07001791 AudioOffloadInfo infoAidl = VALUE_OR_RETURN(
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001792 legacy2aidl_audio_offload_info_t_AudioOffloadInfo(info));
1793 media::AudioOffloadMode retAidl;
1794 RETURN_IF_ERROR(
1795 statusTFromBinderStatus(aps->getOffloadSupport(infoAidl, &retAidl)));
1796 return aidl2legacy_AudioOffloadMode_audio_offload_mode_t(retAidl);
1797 }();
1798
1799 return result.value_or(static_cast<audio_offload_mode_t>(0));
Richard Fitzgeraldad3af332013-03-25 16:54:37 +00001800}
1801
Eric Laurent203b1a12014-04-01 10:34:16 -07001802status_t AudioSystem::listAudioPorts(audio_port_role_t role,
1803 audio_port_type_t type,
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001804 unsigned int* num_ports,
1805 struct audio_port_v7* ports,
1806 unsigned int* generation) {
1807 if (num_ports == nullptr || (*num_ports != 0 && ports == nullptr) ||
1808 generation == nullptr) {
1809 return BAD_VALUE;
1810 }
1811
Andy Hung264fa4c2024-02-21 15:52:12 -08001812 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Andy Hungce3d1102024-09-29 21:47:19 -07001813 if (aps == nullptr) return AudioPolicyServiceTraits::getError();
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001814
1815 media::AudioPortRole roleAidl = VALUE_OR_RETURN_STATUS(
1816 legacy2aidl_audio_port_role_t_AudioPortRole(role));
1817 media::AudioPortType typeAidl = VALUE_OR_RETURN_STATUS(
1818 legacy2aidl_audio_port_type_t_AudioPortType(type));
Mikhail Naganov0078ee52021-09-30 23:06:20 +00001819 Int numPortsAidl;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001820 numPortsAidl.value = VALUE_OR_RETURN_STATUS(convertIntegral<int32_t>(*num_ports));
Atneya Nair638a6e42022-12-18 16:45:15 -08001821 std::vector<media::AudioPortFw> portsAidl;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001822 int32_t generationAidl;
1823
1824 RETURN_STATUS_IF_ERROR(statusTFromBinderStatus(
1825 aps->listAudioPorts(roleAidl, typeAidl, &numPortsAidl, &portsAidl, &generationAidl)));
1826 *num_ports = VALUE_OR_RETURN_STATUS(convertIntegral<unsigned int>(numPortsAidl.value));
1827 *generation = VALUE_OR_RETURN_STATUS(convertIntegral<unsigned int>(generationAidl));
1828 RETURN_STATUS_IF_ERROR(convertRange(portsAidl.begin(), portsAidl.end(), ports,
Mikhail Naganov87227252023-01-13 17:38:10 -08001829 aidl2legacy_AudioPortFw_audio_port_v7));
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001830 return OK;
Eric Laurent203b1a12014-04-01 10:34:16 -07001831}
1832
Mikhail Naganov5edc5ed2023-03-23 14:52:15 -07001833status_t AudioSystem::listDeclaredDevicePorts(media::AudioPortRole role,
1834 std::vector<media::AudioPortFw>* result) {
1835 if (result == nullptr) return BAD_VALUE;
Andy Hung264fa4c2024-02-21 15:52:12 -08001836 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Andy Hungce3d1102024-09-29 21:47:19 -07001837 if (aps == nullptr) return AudioPolicyServiceTraits::getError();
Mikhail Naganov5edc5ed2023-03-23 14:52:15 -07001838 RETURN_STATUS_IF_ERROR(statusTFromBinderStatus(aps->listDeclaredDevicePorts(role, result)));
1839 return OK;
1840}
1841
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001842status_t AudioSystem::getAudioPort(struct audio_port_v7* port) {
1843 if (port == nullptr) {
1844 return BAD_VALUE;
1845 }
Andy Hung264fa4c2024-02-21 15:52:12 -08001846 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Andy Hungce3d1102024-09-29 21:47:19 -07001847 if (aps == nullptr) return AudioPolicyServiceTraits::getError();
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001848
Atneya Nair638a6e42022-12-18 16:45:15 -08001849 media::AudioPortFw portAidl;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001850 RETURN_STATUS_IF_ERROR(
Mikhail Naganov17031562022-02-23 23:00:27 +00001851 statusTFromBinderStatus(aps->getAudioPort(port->id, &portAidl)));
Mikhail Naganov87227252023-01-13 17:38:10 -08001852 *port = VALUE_OR_RETURN_STATUS(aidl2legacy_AudioPortFw_audio_port_v7(portAidl));
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001853 return OK;
Eric Laurent203b1a12014-04-01 10:34:16 -07001854}
1855
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001856status_t AudioSystem::createAudioPatch(const struct audio_patch* patch,
1857 audio_patch_handle_t* handle) {
1858 if (patch == nullptr || handle == nullptr) {
1859 return BAD_VALUE;
1860 }
1861
Andy Hung264fa4c2024-02-21 15:52:12 -08001862 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Andy Hungce3d1102024-09-29 21:47:19 -07001863 if (aps == nullptr) return AudioPolicyServiceTraits::getError();
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001864
Atneya Nair3afdbd12022-12-18 16:14:18 -08001865 media::AudioPatchFw patchAidl = VALUE_OR_RETURN_STATUS(
Mikhail Naganov87227252023-01-13 17:38:10 -08001866 legacy2aidl_audio_patch_AudioPatchFw(*patch));
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001867 int32_t handleAidl = VALUE_OR_RETURN_STATUS(legacy2aidl_audio_patch_handle_t_int32_t(*handle));
1868 RETURN_STATUS_IF_ERROR(
1869 statusTFromBinderStatus(aps->createAudioPatch(patchAidl, handleAidl, &handleAidl)));
1870 *handle = VALUE_OR_RETURN_STATUS(aidl2legacy_int32_t_audio_patch_handle_t(handleAidl));
1871 return OK;
Eric Laurent203b1a12014-04-01 10:34:16 -07001872}
1873
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001874status_t AudioSystem::releaseAudioPatch(audio_patch_handle_t handle) {
Andy Hung264fa4c2024-02-21 15:52:12 -08001875 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Andy Hungce3d1102024-09-29 21:47:19 -07001876 if (aps == nullptr) return AudioPolicyServiceTraits::getError();
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001877
1878 int32_t handleAidl = VALUE_OR_RETURN_STATUS(legacy2aidl_audio_patch_handle_t_int32_t(handle));
1879 return statusTFromBinderStatus(aps->releaseAudioPatch(handleAidl));
Eric Laurent203b1a12014-04-01 10:34:16 -07001880}
1881
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001882status_t AudioSystem::listAudioPatches(unsigned int* num_patches,
1883 struct audio_patch* patches,
1884 unsigned int* generation) {
1885 if (num_patches == nullptr || (*num_patches != 0 && patches == nullptr) ||
1886 generation == nullptr) {
1887 return BAD_VALUE;
1888 }
1889
Andy Hung264fa4c2024-02-21 15:52:12 -08001890 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Andy Hungce3d1102024-09-29 21:47:19 -07001891 if (aps == nullptr) return AudioPolicyServiceTraits::getError();
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001892
1893
Mikhail Naganov0078ee52021-09-30 23:06:20 +00001894 Int numPatchesAidl;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001895 numPatchesAidl.value = VALUE_OR_RETURN_STATUS(convertIntegral<int32_t>(*num_patches));
Atneya Nair3afdbd12022-12-18 16:14:18 -08001896 std::vector<media::AudioPatchFw> patchesAidl;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001897 int32_t generationAidl;
1898
1899 RETURN_STATUS_IF_ERROR(statusTFromBinderStatus(
1900 aps->listAudioPatches(&numPatchesAidl, &patchesAidl, &generationAidl)));
1901 *num_patches = VALUE_OR_RETURN_STATUS(convertIntegral<unsigned int>(numPatchesAidl.value));
1902 *generation = VALUE_OR_RETURN_STATUS(convertIntegral<unsigned int>(generationAidl));
1903 RETURN_STATUS_IF_ERROR(convertRange(patchesAidl.begin(), patchesAidl.end(), patches,
Mikhail Naganov87227252023-01-13 17:38:10 -08001904 aidl2legacy_AudioPatchFw_audio_patch));
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001905 return OK;
Eric Laurent203b1a12014-04-01 10:34:16 -07001906}
1907
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001908status_t AudioSystem::setAudioPortConfig(const struct audio_port_config* config) {
1909 if (config == nullptr) {
1910 return BAD_VALUE;
1911 }
1912
Andy Hung264fa4c2024-02-21 15:52:12 -08001913 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Andy Hungce3d1102024-09-29 21:47:19 -07001914 if (aps == nullptr) return AudioPolicyServiceTraits::getError();
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001915
Atneya Nair7a9594f2022-12-18 17:26:26 -08001916 media::AudioPortConfigFw configAidl = VALUE_OR_RETURN_STATUS(
Mikhail Naganov87227252023-01-13 17:38:10 -08001917 legacy2aidl_audio_port_config_AudioPortConfigFw(*config));
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001918 return statusTFromBinderStatus(aps->setAudioPortConfig(configAidl));
Eric Laurent203b1a12014-04-01 10:34:16 -07001919}
1920
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001921status_t AudioSystem::addAudioPortCallback(const sp<AudioPortCallback>& callback) {
Andy Hung264fa4c2024-02-21 15:52:12 -08001922 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Andy Hungce3d1102024-09-29 21:47:19 -07001923 if (aps == nullptr) return AudioPolicyServiceTraits::getError();
1924 const auto apc = getAudioPolicyClient();
Andy Hung88e71732024-02-21 16:44:27 -08001925 if (apc == nullptr) return NO_INIT;
Eric Laurentb28753e2015-04-01 13:06:28 -07001926
Andy Hung88e71732024-02-21 16:44:27 -08001927 std::lock_guard _l(gApsCallbackMutex);
1928 const int ret = apc->addAudioPortCallback(callback);
Eric Laurente8726fe2015-06-26 09:39:24 -07001929 if (ret == 1) {
1930 aps->setAudioPortCallbacksEnabled(true);
1931 }
1932 return (ret < 0) ? INVALID_OPERATION : NO_ERROR;
Eric Laurentb52c1522014-05-20 11:27:36 -07001933}
1934
Jean-Michel Trivif613d422015-04-23 18:41:29 -07001935/*static*/
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001936status_t AudioSystem::removeAudioPortCallback(const sp<AudioPortCallback>& callback) {
Andy Hung264fa4c2024-02-21 15:52:12 -08001937 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Andy Hungce3d1102024-09-29 21:47:19 -07001938 if (aps == nullptr) return AudioPolicyServiceTraits::getError();
1939 const auto apc = AudioSystem::getAudioPolicyClient();
Andy Hung88e71732024-02-21 16:44:27 -08001940 if (apc == nullptr) return NO_INIT;
Eric Laurentb28753e2015-04-01 13:06:28 -07001941
Andy Hung88e71732024-02-21 16:44:27 -08001942 std::lock_guard _l(gApsCallbackMutex);
1943 const int ret = apc->removeAudioPortCallback(callback);
Eric Laurente8726fe2015-06-26 09:39:24 -07001944 if (ret == 0) {
1945 aps->setAudioPortCallbacksEnabled(false);
1946 }
1947 return (ret < 0) ? INVALID_OPERATION : NO_ERROR;
Eric Laurent296fb132015-05-01 11:38:42 -07001948}
1949
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001950status_t AudioSystem::addAudioVolumeGroupCallback(const sp<AudioVolumeGroupCallback>& callback) {
Andy Hung264fa4c2024-02-21 15:52:12 -08001951 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Andy Hungce3d1102024-09-29 21:47:19 -07001952 if (aps == nullptr) return AudioPolicyServiceTraits::getError();
1953 const auto apc = AudioSystem::getAudioPolicyClient();
Andy Hung88e71732024-02-21 16:44:27 -08001954 if (apc == nullptr) return NO_INIT;
François Gaffiecfe17322018-11-07 13:41:29 +01001955
Andy Hung88e71732024-02-21 16:44:27 -08001956 std::lock_guard _l(gApsCallbackMutex);
1957 const int ret = apc->addAudioVolumeGroupCallback(callback);
François Gaffiecfe17322018-11-07 13:41:29 +01001958 if (ret == 1) {
1959 aps->setAudioVolumeGroupCallbacksEnabled(true);
1960 }
1961 return (ret < 0) ? INVALID_OPERATION : NO_ERROR;
1962}
1963
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001964status_t AudioSystem::removeAudioVolumeGroupCallback(const sp<AudioVolumeGroupCallback>& callback) {
Andy Hung264fa4c2024-02-21 15:52:12 -08001965 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Andy Hungce3d1102024-09-29 21:47:19 -07001966 if (aps == nullptr) return AudioPolicyServiceTraits::getError();
1967 const auto apc = AudioSystem::getAudioPolicyClient();
Andy Hung88e71732024-02-21 16:44:27 -08001968 if (apc == nullptr) return NO_INIT;
François Gaffiecfe17322018-11-07 13:41:29 +01001969
Andy Hung88e71732024-02-21 16:44:27 -08001970 std::lock_guard _l(gApsCallbackMutex);
1971 const int ret = apc->removeAudioVolumeGroupCallback(callback);
François Gaffiecfe17322018-11-07 13:41:29 +01001972 if (ret == 0) {
1973 aps->setAudioVolumeGroupCallbacksEnabled(false);
1974 }
1975 return (ret < 0) ? INVALID_OPERATION : NO_ERROR;
1976}
1977
Eric Laurent296fb132015-05-01 11:38:42 -07001978status_t AudioSystem::addAudioDeviceCallback(
Eric Laurent09f1ed22019-04-24 17:45:17 -07001979 const wp<AudioDeviceCallback>& callback, audio_io_handle_t audioIo,
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001980 audio_port_handle_t portId) {
Eric Laurent296fb132015-05-01 11:38:42 -07001981 const sp<AudioFlingerClient> afc = getAudioFlingerClient();
1982 if (afc == 0) {
1983 return NO_INIT;
1984 }
Eric Laurent09f1ed22019-04-24 17:45:17 -07001985 status_t status = afc->addAudioDeviceCallback(callback, audioIo, portId);
Eric Laurent7c1ec5f2015-07-09 14:52:47 -07001986 if (status == NO_ERROR) {
Andy Hung264fa4c2024-02-21 15:52:12 -08001987 const sp<IAudioFlinger> af = get_audio_flinger();
Eric Laurent7c1ec5f2015-07-09 14:52:47 -07001988 if (af != 0) {
1989 af->registerClient(afc);
1990 }
1991 }
1992 return status;
Eric Laurent296fb132015-05-01 11:38:42 -07001993}
1994
1995status_t AudioSystem::removeAudioDeviceCallback(
Eric Laurent09f1ed22019-04-24 17:45:17 -07001996 const wp<AudioDeviceCallback>& callback, audio_io_handle_t audioIo,
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001997 audio_port_handle_t portId) {
Eric Laurent296fb132015-05-01 11:38:42 -07001998 const sp<AudioFlingerClient> afc = getAudioFlingerClient();
1999 if (afc == 0) {
2000 return NO_INIT;
2001 }
Eric Laurent09f1ed22019-04-24 17:45:17 -07002002 return afc->removeAudioDeviceCallback(callback, audioIo, portId);
Eric Laurent296fb132015-05-01 11:38:42 -07002003}
2004
Eric Laurent076e7c72022-05-03 18:12:28 +02002005status_t AudioSystem::addSupportedLatencyModesCallback(
2006 const sp<SupportedLatencyModesCallback>& callback) {
2007 const sp<AudioFlingerClient> afc = getAudioFlingerClient();
2008 if (afc == 0) {
2009 return NO_INIT;
2010 }
2011 return afc->addSupportedLatencyModesCallback(callback);
2012}
2013
2014status_t AudioSystem::removeSupportedLatencyModesCallback(
2015 const sp<SupportedLatencyModesCallback>& callback) {
2016 const sp<AudioFlingerClient> afc = getAudioFlingerClient();
2017 if (afc == 0) {
2018 return NO_INIT;
2019 }
2020 return afc->removeSupportedLatencyModesCallback(callback);
2021}
2022
Robert Wub7f8edc2024-11-04 19:54:38 +00002023status_t AudioSystem::getDeviceIdsForIo(audio_io_handle_t audioIo, DeviceIdVector& deviceIds) {
Andy Hung264fa4c2024-02-21 15:52:12 -08002024 const sp<IAudioFlinger> af = get_audio_flinger();
Andy Hungce3d1102024-09-29 21:47:19 -07002025 if (af == nullptr) return AudioFlingerServiceTraits::getError();
Eric Laurent296fb132015-05-01 11:38:42 -07002026 const sp<AudioIoDescriptor> desc = getIoDescriptor(audioIo);
2027 if (desc == 0) {
Robert Wub7f8edc2024-11-04 19:54:38 +00002028 deviceIds.clear();
2029 } else {
2030 deviceIds = desc->getDeviceIds();
Eric Laurent296fb132015-05-01 11:38:42 -07002031 }
Robert Wub7f8edc2024-11-04 19:54:38 +00002032 return OK;
Eric Laurentb28753e2015-04-01 13:06:28 -07002033}
2034
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002035status_t AudioSystem::acquireSoundTriggerSession(audio_session_t* session,
2036 audio_io_handle_t* ioHandle,
2037 audio_devices_t* device) {
2038 if (session == nullptr || ioHandle == nullptr || device == nullptr) {
2039 return BAD_VALUE;
2040 }
Andy Hung264fa4c2024-02-21 15:52:12 -08002041 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Andy Hungce3d1102024-09-29 21:47:19 -07002042 if (aps == nullptr) return AudioPolicyServiceTraits::getError();
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002043
2044 media::SoundTriggerSession retAidl;
2045 RETURN_STATUS_IF_ERROR(
2046 statusTFromBinderStatus(aps->acquireSoundTriggerSession(&retAidl)));
2047 *session = VALUE_OR_RETURN_STATUS(aidl2legacy_int32_t_audio_session_t(retAidl.session));
2048 *ioHandle = VALUE_OR_RETURN_STATUS(aidl2legacy_int32_t_audio_io_handle_t(retAidl.ioHandle));
Mikhail Naganov21a32ec2021-07-08 14:40:12 -07002049 *device = VALUE_OR_RETURN_STATUS(
2050 aidl2legacy_AudioDeviceDescription_audio_devices_t(retAidl.device));
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002051 return OK;
Eric Laurentdf3dc7e2014-07-27 18:39:40 -07002052}
2053
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002054status_t AudioSystem::releaseSoundTriggerSession(audio_session_t session) {
Andy Hung264fa4c2024-02-21 15:52:12 -08002055 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Andy Hungce3d1102024-09-29 21:47:19 -07002056 if (aps == nullptr) return AudioPolicyServiceTraits::getError();
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002057
2058 int32_t sessionAidl = VALUE_OR_RETURN_STATUS(legacy2aidl_audio_session_t_int32_t(session));
2059 return statusTFromBinderStatus(aps->releaseSoundTriggerSession(sessionAidl));
Eric Laurentdf3dc7e2014-07-27 18:39:40 -07002060}
Eric Laurentbb6c9a02014-09-25 14:11:47 -07002061
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002062audio_mode_t AudioSystem::getPhoneState() {
Andy Hung264fa4c2024-02-21 15:52:12 -08002063 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Andy Hungce3d1102024-09-29 21:47:19 -07002064 if (aps == nullptr) return AUDIO_MODE_INVALID;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002065
2066 auto result = [&]() -> ConversionResult<audio_mode_t> {
Mikhail Naganovddceecc2021-09-03 13:58:56 -07002067 media::audio::common::AudioMode retAidl;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002068 RETURN_IF_ERROR(statusTFromBinderStatus(aps->getPhoneState(&retAidl)));
2069 return aidl2legacy_AudioMode_audio_mode_t(retAidl);
2070 }();
2071
2072 return result.value_or(AUDIO_MODE_INVALID);
Eric Laurentbb6c9a02014-09-25 14:11:47 -07002073}
2074
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002075status_t AudioSystem::registerPolicyMixes(const Vector<AudioMix>& mixes, bool registration) {
Andy Hung264fa4c2024-02-21 15:52:12 -08002076 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Andy Hungce3d1102024-09-29 21:47:19 -07002077 if (aps == nullptr) return AudioPolicyServiceTraits::getError();
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002078
2079 size_t mixesSize = std::min(mixes.size(), size_t{MAX_MIXES_PER_POLICY});
2080 std::vector<media::AudioMix> mixesAidl;
2081 RETURN_STATUS_IF_ERROR(
2082 convertRange(mixes.begin(), mixes.begin() + mixesSize, std::back_inserter(mixesAidl),
2083 legacy2aidl_AudioMix));
2084 return statusTFromBinderStatus(aps->registerPolicyMixes(mixesAidl, registration));
Eric Laurentbaac1832014-12-01 17:52:59 -08002085}
Eric Laurentbb6c9a02014-09-25 14:11:47 -07002086
Marvin Raminbdefaf02023-11-01 09:10:32 +01002087status_t AudioSystem::getRegisteredPolicyMixes(std::vector<AudioMix>& mixes) {
2088 if (!audio_flags::audio_mix_test_api()) {
2089 return INVALID_OPERATION;
2090 }
2091
2092 const sp<IAudioPolicyService> aps = AudioSystem::get_audio_policy_service();
Andy Hungce3d1102024-09-29 21:47:19 -07002093 if (aps == nullptr) return AudioPolicyServiceTraits::getError();
Marvin Raminbdefaf02023-11-01 09:10:32 +01002094
2095 std::vector<::android::media::AudioMix> aidlMixes;
2096 Status status = aps->getRegisteredPolicyMixes(&aidlMixes);
2097
2098 for (const auto& aidlMix : aidlMixes) {
2099 AudioMix mix = VALUE_OR_RETURN_STATUS(aidl2legacy_AudioMix(aidlMix));
2100 mixes.push_back(mix);
2101 }
2102
2103 return statusTFromBinderStatus(status);
2104}
2105
Jan Sebechlebsky0af8e872023-08-11 14:45:08 +02002106status_t AudioSystem::updatePolicyMixes(
2107 const std::vector<std::pair<AudioMix, std::vector<AudioMixMatchCriterion>>>&
2108 mixesWithUpdates) {
Andy Hung264fa4c2024-02-21 15:52:12 -08002109 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Andy Hungce3d1102024-09-29 21:47:19 -07002110 if (aps == nullptr) return AudioPolicyServiceTraits::getError();
Jan Sebechlebsky0af8e872023-08-11 14:45:08 +02002111
2112 std::vector<media::AudioMixUpdate> updatesAidl;
2113 updatesAidl.reserve(mixesWithUpdates.size());
2114
2115 for (const auto& update : mixesWithUpdates) {
2116 media::AudioMixUpdate updateAidl;
2117 updateAidl.audioMix = VALUE_OR_RETURN_STATUS(legacy2aidl_AudioMix(update.first));
2118 RETURN_STATUS_IF_ERROR(convertRange(update.second.begin(), update.second.end(),
2119 std::back_inserter(updateAidl.newCriteria),
2120 legacy2aidl_AudioMixMatchCriterion));
2121 updatesAidl.emplace_back(updateAidl);
2122 }
2123
2124 return statusTFromBinderStatus(aps->updatePolicyMixes(updatesAidl));
2125}
2126
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002127status_t AudioSystem::setUidDeviceAffinities(uid_t uid, const AudioDeviceTypeAddrVector& devices) {
Andy Hung264fa4c2024-02-21 15:52:12 -08002128 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Andy Hungce3d1102024-09-29 21:47:19 -07002129 if (aps == nullptr) return AudioPolicyServiceTraits::getError();
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002130
2131 int32_t uidAidl = VALUE_OR_RETURN_STATUS(legacy2aidl_uid_t_int32_t(uid));
Mikhail Naganovf4a75362021-09-16 00:02:54 +00002132 std::vector<AudioDevice> devicesAidl = VALUE_OR_RETURN_STATUS(
2133 convertContainer<std::vector<AudioDevice>>(devices,
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002134 legacy2aidl_AudioDeviceTypeAddress));
2135 return statusTFromBinderStatus(aps->setUidDeviceAffinities(uidAidl, devicesAidl));
Jean-Michel Trivibda70da2018-12-19 07:30:15 -08002136}
2137
2138status_t AudioSystem::removeUidDeviceAffinities(uid_t uid) {
Andy Hung264fa4c2024-02-21 15:52:12 -08002139 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Andy Hungce3d1102024-09-29 21:47:19 -07002140 if (aps == nullptr) return AudioPolicyServiceTraits::getError();
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002141
2142 int32_t uidAidl = VALUE_OR_RETURN_STATUS(legacy2aidl_uid_t_int32_t(uid));
2143 return statusTFromBinderStatus(aps->removeUidDeviceAffinities(uidAidl));
Jean-Michel Trivibda70da2018-12-19 07:30:15 -08002144}
2145
Oscar Azucena90e77632019-11-27 17:12:28 -08002146status_t AudioSystem::setUserIdDeviceAffinities(int userId,
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002147 const AudioDeviceTypeAddrVector& devices) {
Andy Hung264fa4c2024-02-21 15:52:12 -08002148 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Andy Hungce3d1102024-09-29 21:47:19 -07002149 if (aps == nullptr) return AudioPolicyServiceTraits::getError();
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002150
2151 int32_t userIdAidl = VALUE_OR_RETURN_STATUS(convertReinterpret<int32_t>(userId));
Mikhail Naganovf4a75362021-09-16 00:02:54 +00002152 std::vector<AudioDevice> devicesAidl = VALUE_OR_RETURN_STATUS(
2153 convertContainer<std::vector<AudioDevice>>(devices,
2154 legacy2aidl_AudioDeviceTypeAddress));
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002155 return statusTFromBinderStatus(
2156 aps->setUserIdDeviceAffinities(userIdAidl, devicesAidl));
Oscar Azucena90e77632019-11-27 17:12:28 -08002157}
2158
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002159status_t AudioSystem::removeUserIdDeviceAffinities(int userId) {
Andy Hung264fa4c2024-02-21 15:52:12 -08002160 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Andy Hungce3d1102024-09-29 21:47:19 -07002161 if (aps == nullptr) return AudioPolicyServiceTraits::getError();
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002162 int32_t userIdAidl = VALUE_OR_RETURN_STATUS(convertReinterpret<int32_t>(userId));
2163 return statusTFromBinderStatus(aps->removeUserIdDeviceAffinities(userIdAidl));
Oscar Azucena90e77632019-11-27 17:12:28 -08002164}
2165
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002166status_t AudioSystem::startAudioSource(const struct audio_port_config* source,
2167 const audio_attributes_t* attributes,
2168 audio_port_handle_t* portId) {
2169 if (source == nullptr || attributes == nullptr || portId == nullptr) {
2170 return BAD_VALUE;
2171 }
Andy Hung264fa4c2024-02-21 15:52:12 -08002172 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Andy Hungce3d1102024-09-29 21:47:19 -07002173 if (aps == nullptr) return AudioPolicyServiceTraits::getError();
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002174
Atneya Nair7a9594f2022-12-18 17:26:26 -08002175 media::AudioPortConfigFw sourceAidl = VALUE_OR_RETURN_STATUS(
Mikhail Naganov87227252023-01-13 17:38:10 -08002176 legacy2aidl_audio_port_config_AudioPortConfigFw(*source));
Mikhail Naganov1c400902023-05-17 11:48:43 -07002177 media::audio::common::AudioAttributes attributesAidl = VALUE_OR_RETURN_STATUS(
2178 legacy2aidl_audio_attributes_t_AudioAttributes(*attributes));
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002179 int32_t portIdAidl;
2180 RETURN_STATUS_IF_ERROR(statusTFromBinderStatus(
2181 aps->startAudioSource(sourceAidl, attributesAidl, &portIdAidl)));
2182 *portId = VALUE_OR_RETURN_STATUS(aidl2legacy_int32_t_audio_port_handle_t(portIdAidl));
2183 return OK;
Eric Laurent554a2772015-04-10 11:29:24 -07002184}
2185
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002186status_t AudioSystem::stopAudioSource(audio_port_handle_t portId) {
Andy Hung264fa4c2024-02-21 15:52:12 -08002187 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Andy Hungce3d1102024-09-29 21:47:19 -07002188 if (aps == nullptr) return AudioPolicyServiceTraits::getError();
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002189
2190 int32_t portIdAidl = VALUE_OR_RETURN_STATUS(legacy2aidl_audio_port_handle_t_int32_t(portId));
2191 return statusTFromBinderStatus(aps->stopAudioSource(portIdAidl));
Eric Laurent554a2772015-04-10 11:29:24 -07002192}
2193
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002194status_t AudioSystem::setMasterMono(bool mono) {
Andy Hung264fa4c2024-02-21 15:52:12 -08002195 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Andy Hungce3d1102024-09-29 21:47:19 -07002196 if (aps == nullptr) return AudioPolicyServiceTraits::getError();
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002197 return statusTFromBinderStatus(aps->setMasterMono(mono));
Andy Hung2ddee192015-12-18 17:34:44 -08002198}
2199
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002200status_t AudioSystem::getMasterMono(bool* mono) {
2201 if (mono == nullptr) {
2202 return BAD_VALUE;
2203 }
Andy Hung264fa4c2024-02-21 15:52:12 -08002204 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Andy Hungce3d1102024-09-29 21:47:19 -07002205 if (aps == nullptr) return AudioPolicyServiceTraits::getError();
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002206 return statusTFromBinderStatus(aps->getMasterMono(mono));
Andy Hung2ddee192015-12-18 17:34:44 -08002207}
2208
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002209status_t AudioSystem::setMasterBalance(float balance) {
Andy Hung264fa4c2024-02-21 15:52:12 -08002210 const sp<IAudioFlinger> af = get_audio_flinger();
Andy Hungce3d1102024-09-29 21:47:19 -07002211 if (af == nullptr) return AudioFlingerServiceTraits::getError();
Richard Folke Tullberg3fae0372017-01-13 09:04:25 +01002212 return af->setMasterBalance(balance);
2213}
2214
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002215status_t AudioSystem::getMasterBalance(float* balance) {
Andy Hung264fa4c2024-02-21 15:52:12 -08002216 const sp<IAudioFlinger> af = get_audio_flinger();
Andy Hungce3d1102024-09-29 21:47:19 -07002217 if (af == nullptr) return AudioFlingerServiceTraits::getError();
Richard Folke Tullberg3fae0372017-01-13 09:04:25 +01002218 return af->getMasterBalance(balance);
2219}
2220
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002221float
2222AudioSystem::getStreamVolumeDB(audio_stream_type_t stream, int index, audio_devices_t device) {
Andy Hung264fa4c2024-02-21 15:52:12 -08002223 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Andy Hungce3d1102024-09-29 21:47:19 -07002224 if (aps == nullptr) return NAN;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002225
2226 auto result = [&]() -> ConversionResult<float> {
Mikhail Naganovdbf03642021-08-25 18:15:32 -07002227 AudioStreamType streamAidl = VALUE_OR_RETURN(
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002228 legacy2aidl_audio_stream_type_t_AudioStreamType(stream));
2229 int32_t indexAidl = VALUE_OR_RETURN(convertIntegral<int32_t>(index));
Mikhail Naganovf4a75362021-09-16 00:02:54 +00002230 AudioDeviceDescription deviceAidl = VALUE_OR_RETURN(
Mikhail Naganov21a32ec2021-07-08 14:40:12 -07002231 legacy2aidl_audio_devices_t_AudioDeviceDescription(device));
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002232 float retAidl;
2233 RETURN_IF_ERROR(statusTFromBinderStatus(
2234 aps->getStreamVolumeDB(streamAidl, indexAidl, deviceAidl, &retAidl)));
2235 return retAidl;
2236 }();
2237 return result.value_or(NAN);
Eric Laurentac9cef52017-06-09 15:46:26 -07002238}
2239
Mikhail Naganovd5d9de72023-02-13 11:45:03 -08002240status_t AudioSystem::getMicrophones(std::vector<media::MicrophoneInfoFw>* microphones) {
Andy Hung264fa4c2024-02-21 15:52:12 -08002241 const sp<IAudioFlinger> af = get_audio_flinger();
Andy Hungce3d1102024-09-29 21:47:19 -07002242 if (af == nullptr) return AudioFlingerServiceTraits::getError();
jiabin46a76fa2018-01-05 10:18:21 -08002243 return af->getMicrophones(microphones);
2244}
2245
Eric Laurent42896a02019-09-27 15:40:33 -07002246status_t AudioSystem::setAudioHalPids(const std::vector<pid_t>& pids) {
Andy Hung264fa4c2024-02-21 15:52:12 -08002247 const sp<IAudioFlinger> af = get_audio_flinger();
Andy Hungce3d1102024-09-29 21:47:19 -07002248 if (af == nullptr) return AudioFlingerServiceTraits::getError();
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002249 return af->setAudioHalPids(pids);
Eric Laurent42896a02019-09-27 15:40:33 -07002250}
2251
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002252status_t AudioSystem::getSurroundFormats(unsigned int* numSurroundFormats,
2253 audio_format_t* surroundFormats,
Kriti Dang6537def2021-03-02 13:46:59 +01002254 bool* surroundFormatsEnabled) {
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002255 if (numSurroundFormats == nullptr || (*numSurroundFormats != 0 &&
2256 (surroundFormats == nullptr ||
2257 surroundFormatsEnabled == nullptr))) {
2258 return BAD_VALUE;
2259 }
2260
Andy Hung264fa4c2024-02-21 15:52:12 -08002261 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Andy Hungce3d1102024-09-29 21:47:19 -07002262 if (aps == nullptr) return AudioPolicyServiceTraits::getError();
Mikhail Naganov0078ee52021-09-30 23:06:20 +00002263 Int numSurroundFormatsAidl;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002264 numSurroundFormatsAidl.value =
2265 VALUE_OR_RETURN_STATUS(convertIntegral<int32_t>(*numSurroundFormats));
Mikhail Naganov57bd06f2021-08-10 16:41:54 -07002266 std::vector<AudioFormatDescription> surroundFormatsAidl;
Kriti Dang877b27e2021-02-02 12:10:40 +01002267 std::vector<bool> surroundFormatsEnabledAidl;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002268 RETURN_STATUS_IF_ERROR(statusTFromBinderStatus(
Kriti Dang6537def2021-03-02 13:46:59 +01002269 aps->getSurroundFormats(&numSurroundFormatsAidl, &surroundFormatsAidl,
2270 &surroundFormatsEnabledAidl)));
Kriti Dang877b27e2021-02-02 12:10:40 +01002271
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002272 *numSurroundFormats = VALUE_OR_RETURN_STATUS(
2273 convertIntegral<unsigned int>(numSurroundFormatsAidl.value));
2274 RETURN_STATUS_IF_ERROR(
2275 convertRange(surroundFormatsAidl.begin(), surroundFormatsAidl.end(), surroundFormats,
Mikhail Naganovb60bd1b2021-07-15 17:31:43 -07002276 aidl2legacy_AudioFormatDescription_audio_format_t));
Kriti Dang877b27e2021-02-02 12:10:40 +01002277 std::copy(surroundFormatsEnabledAidl.begin(), surroundFormatsEnabledAidl.end(),
2278 surroundFormatsEnabled);
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002279 return OK;
jiabin81772902018-04-02 17:52:27 -07002280}
2281
Kriti Dang6537def2021-03-02 13:46:59 +01002282status_t AudioSystem::getReportedSurroundFormats(unsigned int* numSurroundFormats,
2283 audio_format_t* surroundFormats) {
2284 if (numSurroundFormats == nullptr || (*numSurroundFormats != 0 && surroundFormats == nullptr)) {
2285 return BAD_VALUE;
2286 }
2287
Andy Hung264fa4c2024-02-21 15:52:12 -08002288 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Andy Hungce3d1102024-09-29 21:47:19 -07002289 if (aps == nullptr) return AudioPolicyServiceTraits::getError();
Mikhail Naganov0078ee52021-09-30 23:06:20 +00002290 Int numSurroundFormatsAidl;
Kriti Dang6537def2021-03-02 13:46:59 +01002291 numSurroundFormatsAidl.value =
2292 VALUE_OR_RETURN_STATUS(convertIntegral<int32_t>(*numSurroundFormats));
Mikhail Naganov57bd06f2021-08-10 16:41:54 -07002293 std::vector<AudioFormatDescription> surroundFormatsAidl;
Kriti Dang6537def2021-03-02 13:46:59 +01002294 RETURN_STATUS_IF_ERROR(statusTFromBinderStatus(
2295 aps->getReportedSurroundFormats(&numSurroundFormatsAidl, &surroundFormatsAidl)));
2296
2297 *numSurroundFormats = VALUE_OR_RETURN_STATUS(
2298 convertIntegral<unsigned int>(numSurroundFormatsAidl.value));
2299 RETURN_STATUS_IF_ERROR(
2300 convertRange(surroundFormatsAidl.begin(), surroundFormatsAidl.end(), surroundFormats,
Mikhail Naganovb60bd1b2021-07-15 17:31:43 -07002301 aidl2legacy_AudioFormatDescription_audio_format_t));
Kriti Dang6537def2021-03-02 13:46:59 +01002302 return OK;
2303}
2304
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002305status_t AudioSystem::setSurroundFormatEnabled(audio_format_t audioFormat, bool enabled) {
Andy Hung264fa4c2024-02-21 15:52:12 -08002306 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Andy Hungce3d1102024-09-29 21:47:19 -07002307 if (aps == nullptr) return AudioPolicyServiceTraits::getError();
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002308
Mikhail Naganov57bd06f2021-08-10 16:41:54 -07002309 AudioFormatDescription audioFormatAidl = VALUE_OR_RETURN_STATUS(
Mikhail Naganovb60bd1b2021-07-15 17:31:43 -07002310 legacy2aidl_audio_format_t_AudioFormatDescription(audioFormat));
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002311 return statusTFromBinderStatus(
2312 aps->setSurroundFormatEnabled(audioFormatAidl, enabled));
jiabin81772902018-04-02 17:52:27 -07002313}
2314
Oscar Azucena829d90d2022-01-28 17:17:56 -08002315status_t AudioSystem::setAssistantServicesUids(const std::vector<uid_t>& uids) {
Andy Hung264fa4c2024-02-21 15:52:12 -08002316 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Andy Hungce3d1102024-09-29 21:47:19 -07002317 if (aps == nullptr) return AudioPolicyServiceTraits::getError();
Eric Laurentb78763e2018-10-17 10:08:02 -07002318
Oscar Azucena829d90d2022-01-28 17:17:56 -08002319 std::vector<int32_t> uidsAidl = VALUE_OR_RETURN_STATUS(
2320 convertContainer<std::vector<int32_t>>(uids, legacy2aidl_uid_t_int32_t));
2321 return statusTFromBinderStatus(aps->setAssistantServicesUids(uidsAidl));
Ahaan Ugalef51ce002021-08-04 16:34:20 -07002322}
2323
Oscar Azucenac2cdda32022-01-31 19:10:39 -08002324status_t AudioSystem::setActiveAssistantServicesUids(const std::vector<uid_t>& activeUids) {
Andy Hung264fa4c2024-02-21 15:52:12 -08002325 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Andy Hungce3d1102024-09-29 21:47:19 -07002326 if (aps == nullptr) return AudioPolicyServiceTraits::getError();
Oscar Azucenac2cdda32022-01-31 19:10:39 -08002327
2328 std::vector<int32_t> activeUidsAidl = VALUE_OR_RETURN_STATUS(
2329 convertContainer<std::vector<int32_t>>(activeUids, legacy2aidl_uid_t_int32_t));
2330 return statusTFromBinderStatus(aps->setActiveAssistantServicesUids(activeUidsAidl));
2331}
2332
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002333status_t AudioSystem::setA11yServicesUids(const std::vector<uid_t>& uids) {
Andy Hung264fa4c2024-02-21 15:52:12 -08002334 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Andy Hungce3d1102024-09-29 21:47:19 -07002335 if (aps == nullptr) return AudioPolicyServiceTraits::getError();
Eric Laurentb78763e2018-10-17 10:08:02 -07002336
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002337 std::vector<int32_t> uidsAidl = VALUE_OR_RETURN_STATUS(
2338 convertContainer<std::vector<int32_t>>(uids, legacy2aidl_uid_t_int32_t));
2339 return statusTFromBinderStatus(aps->setA11yServicesUids(uidsAidl));
Eric Laurentb78763e2018-10-17 10:08:02 -07002340}
2341
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002342status_t AudioSystem::setCurrentImeUid(uid_t uid) {
Andy Hung264fa4c2024-02-21 15:52:12 -08002343 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Andy Hungce3d1102024-09-29 21:47:19 -07002344 if (aps == nullptr) return AudioPolicyServiceTraits::getError();
Kohsuke Yatoha623a132020-03-24 20:10:26 -07002345
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002346 int32_t uidAidl = VALUE_OR_RETURN_STATUS(legacy2aidl_uid_t_int32_t(uid));
2347 return statusTFromBinderStatus(aps->setCurrentImeUid(uidAidl));
Kohsuke Yatoha623a132020-03-24 20:10:26 -07002348}
2349
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002350bool AudioSystem::isHapticPlaybackSupported() {
Andy Hung264fa4c2024-02-21 15:52:12 -08002351 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Andy Hungce3d1102024-09-29 21:47:19 -07002352 if (aps == nullptr) return false;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002353
2354 auto result = [&]() -> ConversionResult<bool> {
2355 bool retVal;
2356 RETURN_IF_ERROR(
2357 statusTFromBinderStatus(aps->isHapticPlaybackSupported(&retVal)));
2358 return retVal;
2359 }();
2360 return result.value_or(false);
jiabin6012f912018-11-02 17:06:30 -07002361}
2362
Carter Hsu325a8eb2022-01-19 19:56:51 +08002363bool AudioSystem::isUltrasoundSupported() {
Andy Hung264fa4c2024-02-21 15:52:12 -08002364 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Andy Hungce3d1102024-09-29 21:47:19 -07002365 if (aps == nullptr) return false;
Carter Hsu325a8eb2022-01-19 19:56:51 +08002366
2367 auto result = [&]() -> ConversionResult<bool> {
2368 bool retVal;
2369 RETURN_IF_ERROR(
2370 statusTFromBinderStatus(aps->isUltrasoundSupported(&retVal)));
2371 return retVal;
2372 }();
2373 return result.value_or(false);
2374}
2375
Pattydd807582021-11-04 21:01:03 +08002376status_t AudioSystem::getHwOffloadFormatsSupportedForBluetoothMedia(
2377 audio_devices_t device, std::vector<audio_format_t>* formats) {
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002378 if (formats == nullptr) {
2379 return BAD_VALUE;
2380 }
2381
Andy Hung264fa4c2024-02-21 15:52:12 -08002382 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Andy Hungce3d1102024-09-29 21:47:19 -07002383 if (aps == nullptr) return AudioPolicyServiceTraits::getError();
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002384
Mikhail Naganov57bd06f2021-08-10 16:41:54 -07002385 std::vector<AudioFormatDescription> formatsAidl;
Pattydd807582021-11-04 21:01:03 +08002386 AudioDeviceDescription deviceAidl = VALUE_OR_RETURN_STATUS(
2387 legacy2aidl_audio_devices_t_AudioDeviceDescription(device));
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002388 RETURN_STATUS_IF_ERROR(statusTFromBinderStatus(
Pattydd807582021-11-04 21:01:03 +08002389 aps->getHwOffloadFormatsSupportedForBluetoothMedia(deviceAidl, &formatsAidl)));
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002390 *formats = VALUE_OR_RETURN_STATUS(
Mikhail Naganovb60bd1b2021-07-15 17:31:43 -07002391 convertContainer<std::vector<audio_format_t>>(
2392 formatsAidl,
2393 aidl2legacy_AudioFormatDescription_audio_format_t));
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002394 return OK;
François Gaffied0ba9ed2018-11-05 11:50:42 +01002395}
2396
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002397status_t AudioSystem::listAudioProductStrategies(AudioProductStrategyVector& strategies) {
Andy Hung264fa4c2024-02-21 15:52:12 -08002398 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Andy Hungce3d1102024-09-29 21:47:19 -07002399 if (aps == nullptr) return AudioPolicyServiceTraits::getError();
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002400
2401 std::vector<media::AudioProductStrategy> strategiesAidl;
2402 RETURN_STATUS_IF_ERROR(statusTFromBinderStatus(
2403 aps->listAudioProductStrategies(&strategiesAidl)));
2404 strategies = VALUE_OR_RETURN_STATUS(
2405 convertContainer<AudioProductStrategyVector>(strategiesAidl,
2406 aidl2legacy_AudioProductStrategy));
2407 return OK;
François Gaffied0ba9ed2018-11-05 11:50:42 +01002408}
2409
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002410audio_attributes_t AudioSystem::streamTypeToAttributes(audio_stream_type_t stream) {
François Gaffied0ba9ed2018-11-05 11:50:42 +01002411 AudioProductStrategyVector strategies;
2412 listAudioProductStrategies(strategies);
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002413 for (const auto& strategy : strategies) {
François Gaffie1e2b56f2022-04-01 14:34:29 +02002414 auto attrVect = strategy.getVolumeGroupAttributes();
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002415 auto iter = std::find_if(begin(attrVect), end(attrVect), [&stream](const auto& attributes) {
2416 return attributes.getStreamType() == stream;
2417 });
François Gaffied0ba9ed2018-11-05 11:50:42 +01002418 if (iter != end(attrVect)) {
2419 return iter->getAttributes();
2420 }
2421 }
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002422 ALOGE("invalid stream type %s when converting to attributes", toString(stream).c_str());
François Gaffied0ba9ed2018-11-05 11:50:42 +01002423 return AUDIO_ATTRIBUTES_INITIALIZER;
2424}
2425
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002426audio_stream_type_t AudioSystem::attributesToStreamType(const audio_attributes_t& attr) {
François Gaffie4b2018b2018-11-07 11:18:59 +01002427 product_strategy_t psId;
François Gaffie1e2b56f2022-04-01 14:34:29 +02002428 status_t ret = AudioSystem::getProductStrategyFromAudioAttributes(attr, psId);
François Gaffie4b2018b2018-11-07 11:18:59 +01002429 if (ret != NO_ERROR) {
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002430 ALOGE("no strategy found for attributes %s", toString(attr).c_str());
François Gaffie4b2018b2018-11-07 11:18:59 +01002431 return AUDIO_STREAM_MUSIC;
2432 }
François Gaffied0ba9ed2018-11-05 11:50:42 +01002433 AudioProductStrategyVector strategies;
2434 listAudioProductStrategies(strategies);
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002435 for (const auto& strategy : strategies) {
François Gaffie4b2018b2018-11-07 11:18:59 +01002436 if (strategy.getId() == psId) {
François Gaffie1e2b56f2022-04-01 14:34:29 +02002437 auto attrVect = strategy.getVolumeGroupAttributes();
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002438 auto iter = std::find_if(begin(attrVect), end(attrVect), [&attr](const auto& refAttr) {
François Gaffie8d7fd5a2023-01-17 17:28:09 +01002439 return refAttr.matchesScore(attr) > 0;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002440 });
François Gaffied0ba9ed2018-11-05 11:50:42 +01002441 if (iter != end(attrVect)) {
2442 return iter->getStreamType();
2443 }
2444 }
2445 }
Jean-Michel Trivied678652019-12-19 13:39:30 -08002446 switch (attr.usage) {
2447 case AUDIO_USAGE_VIRTUAL_SOURCE:
2448 // virtual source is not expected to have an associated product strategy
2449 break;
2450 default:
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002451 ALOGE("invalid attributes %s when converting to stream", toString(attr).c_str());
Jean-Michel Trivied678652019-12-19 13:39:30 -08002452 break;
2453 }
François Gaffied0ba9ed2018-11-05 11:50:42 +01002454 return AUDIO_STREAM_MUSIC;
2455}
2456
François Gaffie1e2b56f2022-04-01 14:34:29 +02002457status_t AudioSystem::getProductStrategyFromAudioAttributes(const audio_attributes_t& aa,
Francois Gaffie11b65922020-09-24 16:59:08 +02002458 product_strategy_t& productStrategy,
2459 bool fallbackOnDefault) {
Andy Hung264fa4c2024-02-21 15:52:12 -08002460 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Andy Hungce3d1102024-09-29 21:47:19 -07002461 if (aps == nullptr) return AudioPolicyServiceTraits::getError();
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002462
Mikhail Naganov1c400902023-05-17 11:48:43 -07002463 media::audio::common::AudioAttributes aaAidl = VALUE_OR_RETURN_STATUS(
2464 legacy2aidl_audio_attributes_t_AudioAttributes(aa));
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002465 int32_t productStrategyAidl;
2466
2467 RETURN_STATUS_IF_ERROR(statusTFromBinderStatus(
Francois Gaffie11b65922020-09-24 16:59:08 +02002468 aps->getProductStrategyFromAudioAttributes(aaAidl, fallbackOnDefault,
2469 &productStrategyAidl)));
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002470 productStrategy = VALUE_OR_RETURN_STATUS(
2471 aidl2legacy_int32_t_product_strategy_t(productStrategyAidl));
2472 return OK;
François Gaffie4b2018b2018-11-07 11:18:59 +01002473}
2474
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002475status_t AudioSystem::listAudioVolumeGroups(AudioVolumeGroupVector& groups) {
Andy Hung264fa4c2024-02-21 15:52:12 -08002476 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Andy Hungce3d1102024-09-29 21:47:19 -07002477 if (aps == nullptr) return AudioPolicyServiceTraits::getError();
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002478
2479 std::vector<media::AudioVolumeGroup> groupsAidl;
2480 RETURN_STATUS_IF_ERROR(
2481 statusTFromBinderStatus(aps->listAudioVolumeGroups(&groupsAidl)));
2482 groups = VALUE_OR_RETURN_STATUS(
2483 convertContainer<AudioVolumeGroupVector>(groupsAidl, aidl2legacy_AudioVolumeGroup));
2484 return OK;
François Gaffie4b2018b2018-11-07 11:18:59 +01002485}
2486
François Gaffie1e2b56f2022-04-01 14:34:29 +02002487status_t AudioSystem::getVolumeGroupFromAudioAttributes(const audio_attributes_t &aa,
Francois Gaffie11b65922020-09-24 16:59:08 +02002488 volume_group_t& volumeGroup,
2489 bool fallbackOnDefault) {
Andy Hung264fa4c2024-02-21 15:52:12 -08002490 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Andy Hungce3d1102024-09-29 21:47:19 -07002491 if (aps == nullptr) return AudioPolicyServiceTraits::getError();
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002492
Mikhail Naganov1c400902023-05-17 11:48:43 -07002493 media::audio::common::AudioAttributes aaAidl = VALUE_OR_RETURN_STATUS(
2494 legacy2aidl_audio_attributes_t_AudioAttributes(aa));
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002495 int32_t volumeGroupAidl;
2496 RETURN_STATUS_IF_ERROR(statusTFromBinderStatus(
Francois Gaffie11b65922020-09-24 16:59:08 +02002497 aps->getVolumeGroupFromAudioAttributes(aaAidl, fallbackOnDefault, &volumeGroupAidl)));
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002498 volumeGroup = VALUE_OR_RETURN_STATUS(aidl2legacy_int32_t_volume_group_t(volumeGroupAidl));
2499 return OK;
Arun Mirpuri11029ad2018-12-19 20:45:19 -08002500}
Eric Laurentb78763e2018-10-17 10:08:02 -07002501
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002502status_t AudioSystem::setRttEnabled(bool enabled) {
Andy Hung264fa4c2024-02-21 15:52:12 -08002503 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Andy Hungce3d1102024-09-29 21:47:19 -07002504 if (aps == nullptr) return AudioPolicyServiceTraits::getError();
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002505 return statusTFromBinderStatus(aps->setRttEnabled(enabled));
Eric Laurent6ede98f2019-06-11 14:50:30 -07002506}
2507
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002508bool AudioSystem::isCallScreenModeSupported() {
Andy Hung264fa4c2024-02-21 15:52:12 -08002509 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Andy Hungce3d1102024-09-29 21:47:19 -07002510 if (aps == nullptr) return false;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002511
2512 auto result = [&]() -> ConversionResult<bool> {
2513 bool retAidl;
2514 RETURN_IF_ERROR(
2515 statusTFromBinderStatus(aps->isCallScreenModeSupported(&retAidl)));
2516 return retAidl;
2517 }();
2518 return result.value_or(false);
Eric Laurent8340e672019-11-06 11:01:08 -08002519}
2520
jiabin0a488932020-08-07 17:32:40 -07002521status_t AudioSystem::setDevicesRoleForStrategy(product_strategy_t strategy,
2522 device_role_t role,
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002523 const AudioDeviceTypeAddrVector& devices) {
Andy Hung264fa4c2024-02-21 15:52:12 -08002524 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Andy Hungce3d1102024-09-29 21:47:19 -07002525 if (aps == nullptr) return AudioPolicyServiceTraits::getError();
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002526
2527 int32_t strategyAidl = VALUE_OR_RETURN_STATUS(legacy2aidl_product_strategy_t_int32_t(strategy));
2528 media::DeviceRole roleAidl = VALUE_OR_RETURN_STATUS(legacy2aidl_device_role_t_DeviceRole(role));
Mikhail Naganovf4a75362021-09-16 00:02:54 +00002529 std::vector<AudioDevice> devicesAidl = VALUE_OR_RETURN_STATUS(
2530 convertContainer<std::vector<AudioDevice>>(devices,
2531 legacy2aidl_AudioDeviceTypeAddress));
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002532 return statusTFromBinderStatus(
2533 aps->setDevicesRoleForStrategy(strategyAidl, roleAidl, devicesAidl));
Jean-Michel Trivi30857152019-11-01 11:04:15 -07002534}
2535
Paul Wang5d7cdb52022-11-22 09:45:06 +00002536status_t AudioSystem::removeDevicesRoleForStrategy(product_strategy_t strategy,
2537 device_role_t role,
2538 const AudioDeviceTypeAddrVector& devices) {
Andy Hung264fa4c2024-02-21 15:52:12 -08002539 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Andy Hungce3d1102024-09-29 21:47:19 -07002540 if (aps == nullptr) return AudioPolicyServiceTraits::getError();
Paul Wang5d7cdb52022-11-22 09:45:06 +00002541
2542 int32_t strategyAidl = VALUE_OR_RETURN_STATUS(legacy2aidl_product_strategy_t_int32_t(strategy));
2543 media::DeviceRole roleAidl = VALUE_OR_RETURN_STATUS(legacy2aidl_device_role_t_DeviceRole(role));
2544 std::vector<AudioDevice> devicesAidl = VALUE_OR_RETURN_STATUS(
2545 convertContainer<std::vector<AudioDevice>>(devices,
2546 legacy2aidl_AudioDeviceTypeAddress));
2547 return statusTFromBinderStatus(
2548 aps->removeDevicesRoleForStrategy(strategyAidl, roleAidl, devicesAidl));
2549}
2550
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002551status_t
Paul Wang5d7cdb52022-11-22 09:45:06 +00002552AudioSystem::clearDevicesRoleForStrategy(product_strategy_t strategy, device_role_t role) {
Andy Hung264fa4c2024-02-21 15:52:12 -08002553 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Andy Hungce3d1102024-09-29 21:47:19 -07002554 if (aps == nullptr) return AudioPolicyServiceTraits::getError();
2555
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002556 int32_t strategyAidl = VALUE_OR_RETURN_STATUS(legacy2aidl_product_strategy_t_int32_t(strategy));
2557 media::DeviceRole roleAidl = VALUE_OR_RETURN_STATUS(legacy2aidl_device_role_t_DeviceRole(role));
2558 return statusTFromBinderStatus(
Paul Wang5d7cdb52022-11-22 09:45:06 +00002559 aps->clearDevicesRoleForStrategy(strategyAidl, roleAidl));
Jean-Michel Trivi30857152019-11-01 11:04:15 -07002560}
2561
jiabin0a488932020-08-07 17:32:40 -07002562status_t AudioSystem::getDevicesForRoleAndStrategy(product_strategy_t strategy,
2563 device_role_t role,
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002564 AudioDeviceTypeAddrVector& devices) {
Andy Hung264fa4c2024-02-21 15:52:12 -08002565 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Andy Hungce3d1102024-09-29 21:47:19 -07002566 if (aps == nullptr) return AudioPolicyServiceTraits::getError();
2567
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002568 int32_t strategyAidl = VALUE_OR_RETURN_STATUS(legacy2aidl_product_strategy_t_int32_t(strategy));
2569 media::DeviceRole roleAidl = VALUE_OR_RETURN_STATUS(legacy2aidl_device_role_t_DeviceRole(role));
Mikhail Naganovf4a75362021-09-16 00:02:54 +00002570 std::vector<AudioDevice> devicesAidl;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002571 RETURN_STATUS_IF_ERROR(statusTFromBinderStatus(
2572 aps->getDevicesForRoleAndStrategy(strategyAidl, roleAidl, &devicesAidl)));
2573 devices = VALUE_OR_RETURN_STATUS(
2574 convertContainer<AudioDeviceTypeAddrVector>(devicesAidl,
2575 aidl2legacy_AudioDeviceTypeAddress));
2576 return OK;
Jean-Michel Trivi30857152019-11-01 11:04:15 -07002577}
2578
Jiabin Huang3b98d322020-09-03 17:54:16 +00002579status_t AudioSystem::setDevicesRoleForCapturePreset(audio_source_t audioSource,
2580 device_role_t role,
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002581 const AudioDeviceTypeAddrVector& devices) {
Andy Hung264fa4c2024-02-21 15:52:12 -08002582 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Andy Hungce3d1102024-09-29 21:47:19 -07002583 if (aps == nullptr) return AudioPolicyServiceTraits::getError();
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002584
Mikhail Naganovddceecc2021-09-03 13:58:56 -07002585 AudioSource audioSourceAidl = VALUE_OR_RETURN_STATUS(
2586 legacy2aidl_audio_source_t_AudioSource(audioSource));
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002587 media::DeviceRole roleAidl = VALUE_OR_RETURN_STATUS(legacy2aidl_device_role_t_DeviceRole(role));
Mikhail Naganovf4a75362021-09-16 00:02:54 +00002588 std::vector<AudioDevice> devicesAidl = VALUE_OR_RETURN_STATUS(
2589 convertContainer<std::vector<AudioDevice>>(devices,
2590 legacy2aidl_AudioDeviceTypeAddress));
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002591 return statusTFromBinderStatus(
2592 aps->setDevicesRoleForCapturePreset(audioSourceAidl, roleAidl, devicesAidl));
Jiabin Huang3b98d322020-09-03 17:54:16 +00002593}
2594
2595status_t AudioSystem::addDevicesRoleForCapturePreset(audio_source_t audioSource,
2596 device_role_t role,
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002597 const AudioDeviceTypeAddrVector& devices) {
Andy Hung264fa4c2024-02-21 15:52:12 -08002598 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Andy Hungce3d1102024-09-29 21:47:19 -07002599 if (aps == nullptr) return AudioPolicyServiceTraits::getError();
2600
Mikhail Naganovddceecc2021-09-03 13:58:56 -07002601 AudioSource audioSourceAidl = VALUE_OR_RETURN_STATUS(
2602 legacy2aidl_audio_source_t_AudioSource(audioSource));
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002603 media::DeviceRole roleAidl = VALUE_OR_RETURN_STATUS(legacy2aidl_device_role_t_DeviceRole(role));
Mikhail Naganovf4a75362021-09-16 00:02:54 +00002604 std::vector<AudioDevice> devicesAidl = VALUE_OR_RETURN_STATUS(
2605 convertContainer<std::vector<AudioDevice>>(devices,
2606 legacy2aidl_AudioDeviceTypeAddress));
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002607 return statusTFromBinderStatus(
2608 aps->addDevicesRoleForCapturePreset(audioSourceAidl, roleAidl, devicesAidl));
Jiabin Huang3b98d322020-09-03 17:54:16 +00002609}
2610
2611status_t AudioSystem::removeDevicesRoleForCapturePreset(
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002612 audio_source_t audioSource, device_role_t role, const AudioDeviceTypeAddrVector& devices) {
Andy Hung264fa4c2024-02-21 15:52:12 -08002613 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Andy Hungce3d1102024-09-29 21:47:19 -07002614 if (aps == nullptr) return AudioPolicyServiceTraits::getError();
2615
Mikhail Naganovddceecc2021-09-03 13:58:56 -07002616 AudioSource audioSourceAidl = VALUE_OR_RETURN_STATUS(
2617 legacy2aidl_audio_source_t_AudioSource(audioSource));
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002618 media::DeviceRole roleAidl = VALUE_OR_RETURN_STATUS(legacy2aidl_device_role_t_DeviceRole(role));
Mikhail Naganovf4a75362021-09-16 00:02:54 +00002619 std::vector<AudioDevice> devicesAidl = VALUE_OR_RETURN_STATUS(
2620 convertContainer<std::vector<AudioDevice>>(devices,
2621 legacy2aidl_AudioDeviceTypeAddress));
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002622 return statusTFromBinderStatus(
2623 aps->removeDevicesRoleForCapturePreset(audioSourceAidl, roleAidl, devicesAidl));
Jiabin Huang3b98d322020-09-03 17:54:16 +00002624}
2625
2626status_t AudioSystem::clearDevicesRoleForCapturePreset(audio_source_t audioSource,
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002627 device_role_t role) {
Andy Hung264fa4c2024-02-21 15:52:12 -08002628 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Andy Hungce3d1102024-09-29 21:47:19 -07002629 if (aps == nullptr) return AudioPolicyServiceTraits::getError();
2630
Mikhail Naganovddceecc2021-09-03 13:58:56 -07002631 AudioSource audioSourceAidl = VALUE_OR_RETURN_STATUS(
2632 legacy2aidl_audio_source_t_AudioSource(audioSource));
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002633 media::DeviceRole roleAidl = VALUE_OR_RETURN_STATUS(legacy2aidl_device_role_t_DeviceRole(role));
2634 return statusTFromBinderStatus(
2635 aps->clearDevicesRoleForCapturePreset(audioSourceAidl, roleAidl));
Jiabin Huang3b98d322020-09-03 17:54:16 +00002636}
2637
2638status_t AudioSystem::getDevicesForRoleAndCapturePreset(audio_source_t audioSource,
2639 device_role_t role,
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002640 AudioDeviceTypeAddrVector& devices) {
Andy Hung264fa4c2024-02-21 15:52:12 -08002641 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Andy Hungce3d1102024-09-29 21:47:19 -07002642 if (aps == nullptr) return AudioPolicyServiceTraits::getError();
Mikhail Naganovddceecc2021-09-03 13:58:56 -07002643 AudioSource audioSourceAidl = VALUE_OR_RETURN_STATUS(
2644 legacy2aidl_audio_source_t_AudioSource(audioSource));
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002645 media::DeviceRole roleAidl = VALUE_OR_RETURN_STATUS(legacy2aidl_device_role_t_DeviceRole(role));
Mikhail Naganovf4a75362021-09-16 00:02:54 +00002646 std::vector<AudioDevice> devicesAidl;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002647 RETURN_STATUS_IF_ERROR(statusTFromBinderStatus(
2648 aps->getDevicesForRoleAndCapturePreset(audioSourceAidl, roleAidl, &devicesAidl)));
2649 devices = VALUE_OR_RETURN_STATUS(
2650 convertContainer<AudioDeviceTypeAddrVector>(devicesAidl,
2651 aidl2legacy_AudioDeviceTypeAddress));
2652 return OK;
Jiabin Huang3b98d322020-09-03 17:54:16 +00002653}
2654
Eric Laurent81dd0f52021-07-05 11:54:40 +02002655status_t AudioSystem::getSpatializer(const sp<media::INativeSpatializerCallback>& callback,
2656 sp<media::ISpatializer>* spatializer) {
Andy Hung264fa4c2024-02-21 15:52:12 -08002657 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Eric Laurent81dd0f52021-07-05 11:54:40 +02002658 if (spatializer == nullptr) {
2659 return BAD_VALUE;
2660 }
Andy Hungce3d1102024-09-29 21:47:19 -07002661 if (aps == nullptr) return AudioPolicyServiceTraits::getError();
Eric Laurent81dd0f52021-07-05 11:54:40 +02002662 media::GetSpatializerResponse response;
2663 RETURN_STATUS_IF_ERROR(statusTFromBinderStatus(
2664 aps->getSpatializer(callback, &response)));
2665
2666 *spatializer = response.spatializer;
2667 return OK;
2668}
2669
2670status_t AudioSystem::canBeSpatialized(const audio_attributes_t *attr,
2671 const audio_config_t *config,
2672 const AudioDeviceTypeAddrVector &devices,
2673 bool *canBeSpatialized) {
Andy Hung264fa4c2024-02-21 15:52:12 -08002674 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Ram Mohan43297012022-08-30 16:11:49 +05302675 if (canBeSpatialized == nullptr) {
2676 return BAD_VALUE;
2677 }
Andy Hungce3d1102024-09-29 21:47:19 -07002678 if (aps == nullptr) return AudioPolicyServiceTraits::getError();
Eric Laurent81dd0f52021-07-05 11:54:40 +02002679 audio_attributes_t attributes = attr != nullptr ? *attr : AUDIO_ATTRIBUTES_INITIALIZER;
2680 audio_config_t configuration = config != nullptr ? *config : AUDIO_CONFIG_INITIALIZER;
2681
Mikhail Naganov1c400902023-05-17 11:48:43 -07002682 std::optional<media::audio::common::AudioAttributes> attrAidl = VALUE_OR_RETURN_STATUS(
2683 legacy2aidl_audio_attributes_t_AudioAttributes(attributes));
Mikhail Naganovdbf03642021-08-25 18:15:32 -07002684 std::optional<AudioConfig> configAidl = VALUE_OR_RETURN_STATUS(
Eric Laurent81dd0f52021-07-05 11:54:40 +02002685 legacy2aidl_audio_config_t_AudioConfig(configuration, false /*isInput*/));
Mikhail Naganovf4a75362021-09-16 00:02:54 +00002686 std::vector<AudioDevice> devicesAidl = VALUE_OR_RETURN_STATUS(
2687 convertContainer<std::vector<AudioDevice>>(devices,
2688 legacy2aidl_AudioDeviceTypeAddress));
Eric Laurent81dd0f52021-07-05 11:54:40 +02002689 RETURN_STATUS_IF_ERROR(statusTFromBinderStatus(
2690 aps->canBeSpatialized(attrAidl, configAidl, devicesAidl, canBeSpatialized)));
2691 return OK;
2692}
2693
Vlad Popae3fd1c22022-11-07 21:03:18 +01002694status_t AudioSystem::getSoundDoseInterface(const sp<media::ISoundDoseCallback>& callback,
2695 sp<media::ISoundDose>* soundDose) {
Andy Hung264fa4c2024-02-21 15:52:12 -08002696 const sp<IAudioFlinger> af = get_audio_flinger();
Andy Hungce3d1102024-09-29 21:47:19 -07002697 if (af == nullptr) return AudioFlingerServiceTraits::getError();
Vlad Popae3fd1c22022-11-07 21:03:18 +01002698 if (soundDose == nullptr) {
2699 return BAD_VALUE;
2700 }
Vlad Popa63f047e2022-11-05 14:09:19 +01002701
Vlad Popae3fd1c22022-11-07 21:03:18 +01002702 RETURN_STATUS_IF_ERROR(af->getSoundDoseInterface(callback, soundDose));
2703 return OK;
Vlad Popa63f047e2022-11-05 14:09:19 +01002704}
2705
jiabin2b9d5a12021-12-10 01:06:29 +00002706status_t AudioSystem::getDirectPlaybackSupport(const audio_attributes_t *attr,
2707 const audio_config_t *config,
2708 audio_direct_mode_t* directMode) {
2709 if (attr == nullptr || config == nullptr || directMode == nullptr) {
2710 return BAD_VALUE;
2711 }
2712
Andy Hung264fa4c2024-02-21 15:52:12 -08002713 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Andy Hungce3d1102024-09-29 21:47:19 -07002714 if (aps == nullptr) return AudioPolicyServiceTraits::getError();
jiabin2b9d5a12021-12-10 01:06:29 +00002715
Mikhail Naganov1c400902023-05-17 11:48:43 -07002716 media::audio::common::AudioAttributes attrAidl = VALUE_OR_RETURN_STATUS(
2717 legacy2aidl_audio_attributes_t_AudioAttributes(*attr));
jiabin2b9d5a12021-12-10 01:06:29 +00002718 AudioConfig configAidl = VALUE_OR_RETURN_STATUS(
2719 legacy2aidl_audio_config_t_AudioConfig(*config, false /*isInput*/));
2720
2721 media::AudioDirectMode retAidl;
2722 RETURN_STATUS_IF_ERROR(statusTFromBinderStatus(
2723 aps->getDirectPlaybackSupport(attrAidl, configAidl, &retAidl)));
2724 *directMode = VALUE_OR_RETURN_STATUS(aidl2legacy_int32_t_audio_direct_mode_t_mask(
2725 static_cast<int32_t>(retAidl)));
2726 return NO_ERROR;
2727}
2728
Dorin Drimusf2196d82022-01-03 12:11:18 +01002729status_t AudioSystem::getDirectProfilesForAttributes(const audio_attributes_t* attr,
2730 std::vector<audio_profile>* audioProfiles) {
Mikhail Naganovefc504b2022-06-22 03:24:59 +00002731 if (attr == nullptr || audioProfiles == nullptr) {
Dorin Drimusf2196d82022-01-03 12:11:18 +01002732 return BAD_VALUE;
2733 }
2734
Andy Hung264fa4c2024-02-21 15:52:12 -08002735 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Andy Hungce3d1102024-09-29 21:47:19 -07002736 if (aps == nullptr) return AudioPolicyServiceTraits::getError();
Dorin Drimusf2196d82022-01-03 12:11:18 +01002737
Mikhail Naganov1c400902023-05-17 11:48:43 -07002738 media::audio::common::AudioAttributes attrAidl = VALUE_OR_RETURN_STATUS(
2739 legacy2aidl_audio_attributes_t_AudioAttributes(*attr));
Dorin Drimusf2196d82022-01-03 12:11:18 +01002740
2741 std::vector<media::audio::common::AudioProfile> audioProfilesAidl;
2742 RETURN_STATUS_IF_ERROR(statusTFromBinderStatus(
2743 aps->getDirectProfilesForAttributes(attrAidl, &audioProfilesAidl)));
2744 *audioProfiles = VALUE_OR_RETURN_STATUS(convertContainer<std::vector<audio_profile>>(
2745 audioProfilesAidl, aidl2legacy_AudioProfile_audio_profile, false /*isInput*/));
2746
2747 return NO_ERROR;
2748}
Eric Laurent81dd0f52021-07-05 11:54:40 +02002749
Eric Laurent076e7c72022-05-03 18:12:28 +02002750status_t AudioSystem::setRequestedLatencyMode(
2751 audio_io_handle_t output, audio_latency_mode_t mode) {
Andy Hung264fa4c2024-02-21 15:52:12 -08002752 const sp<IAudioFlinger> af = get_audio_flinger();
Andy Hungce3d1102024-09-29 21:47:19 -07002753 if (af == nullptr) return AudioFlingerServiceTraits::getError();
Eric Laurent076e7c72022-05-03 18:12:28 +02002754 return af->setRequestedLatencyMode(output, mode);
2755}
2756
2757status_t AudioSystem::getSupportedLatencyModes(audio_io_handle_t output,
2758 std::vector<audio_latency_mode_t>* modes) {
Andy Hung264fa4c2024-02-21 15:52:12 -08002759 const sp<IAudioFlinger> af = get_audio_flinger();
Andy Hungce3d1102024-09-29 21:47:19 -07002760 if (af == nullptr) return AudioFlingerServiceTraits::getError();
Eric Laurent076e7c72022-05-03 18:12:28 +02002761 return af->getSupportedLatencyModes(output, modes);
2762}
2763
Eric Laurent50d72582022-12-20 20:20:23 +01002764status_t AudioSystem::setBluetoothVariableLatencyEnabled(bool enabled) {
Andy Hung264fa4c2024-02-21 15:52:12 -08002765 const sp<IAudioFlinger> af = get_audio_flinger();
Andy Hungce3d1102024-09-29 21:47:19 -07002766 if (af == nullptr) return AudioFlingerServiceTraits::getError();
Eric Laurent50d72582022-12-20 20:20:23 +01002767 return af->setBluetoothVariableLatencyEnabled(enabled);
Eric Laurent52057642022-12-16 11:45:07 +01002768}
2769
Eric Laurent50d72582022-12-20 20:20:23 +01002770status_t AudioSystem::isBluetoothVariableLatencyEnabled(
2771 bool *enabled) {
Andy Hung264fa4c2024-02-21 15:52:12 -08002772 const sp<IAudioFlinger> af = get_audio_flinger();
Andy Hungce3d1102024-09-29 21:47:19 -07002773 if (af == nullptr) return AudioFlingerServiceTraits::getError();
Eric Laurent50d72582022-12-20 20:20:23 +01002774 return af->isBluetoothVariableLatencyEnabled(enabled);
2775}
2776
2777status_t AudioSystem::supportsBluetoothVariableLatency(
Eric Laurent52057642022-12-16 11:45:07 +01002778 bool *support) {
Andy Hung264fa4c2024-02-21 15:52:12 -08002779 const sp<IAudioFlinger> af = get_audio_flinger();
Andy Hungce3d1102024-09-29 21:47:19 -07002780 if (af == nullptr) return AudioFlingerServiceTraits::getError();
Eric Laurent50d72582022-12-20 20:20:23 +01002781 return af->supportsBluetoothVariableLatency(support);
Eric Laurent52057642022-12-16 11:45:07 +01002782}
2783
Mikhail Naganovffd97712023-05-03 17:45:36 -07002784status_t AudioSystem::getAudioPolicyConfig(media::AudioPolicyConfig *config) {
Andy Hung264fa4c2024-02-21 15:52:12 -08002785 const sp<IAudioFlinger> af = get_audio_flinger();
Andy Hungce3d1102024-09-29 21:47:19 -07002786 if (af == nullptr) return AudioFlingerServiceTraits::getError();
Mikhail Naganovffd97712023-05-03 17:45:36 -07002787 return af->getAudioPolicyConfig(config);
2788}
2789
Ytai Ben-Tsvi5120eda2020-03-26 09:41:15 -07002790class CaptureStateListenerImpl : public media::BnCaptureStateListener,
2791 public IBinder::DeathRecipient {
2792public:
Ytai Ben-Tsvid9f82832020-08-27 09:31:10 -07002793 CaptureStateListenerImpl(
2794 const sp<IAudioPolicyService>& aps,
2795 const sp<AudioSystem::CaptureStateListener>& listener)
Ytai Ben-Tsvia46b6d32020-08-31 13:29:11 -07002796 : mAps(aps), mListener(listener) {}
2797
2798 void init() {
Ytai Ben-Tsvid9f82832020-08-27 09:31:10 -07002799 bool active;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002800 status_t status = statusTFromBinderStatus(
2801 mAps->registerSoundTriggerCaptureStateListener(this, &active));
Ytai Ben-Tsvid9f82832020-08-27 09:31:10 -07002802 if (status != NO_ERROR) {
2803 mListener->onServiceDied();
2804 return;
2805 }
2806 mListener->onStateChanged(active);
Ytai Ben-Tsvia46b6d32020-08-31 13:29:11 -07002807 IInterface::asBinder(mAps)->linkToDeath(this);
Ytai Ben-Tsvid9f82832020-08-27 09:31:10 -07002808 }
2809
Ytai Ben-Tsvi5120eda2020-03-26 09:41:15 -07002810 binder::Status setCaptureState(bool active) override {
Andy Hungacd955a2024-02-16 17:14:36 -08002811 std::lock_guard _l(AudioSystem::gSoundTriggerMutex);
Ytai Ben-Tsvid9f82832020-08-27 09:31:10 -07002812 mListener->onStateChanged(active);
Ytai Ben-Tsvi5120eda2020-03-26 09:41:15 -07002813 return binder::Status::ok();
2814 }
2815
2816 void binderDied(const wp<IBinder>&) override {
Andy Hungacd955a2024-02-16 17:14:36 -08002817 std::lock_guard _l(AudioSystem::gSoundTriggerMutex);
Ytai Ben-Tsvid9f82832020-08-27 09:31:10 -07002818 mListener->onServiceDied();
Andy Hungacd955a2024-02-16 17:14:36 -08002819 AudioSystem::gSoundTriggerCaptureStateListener = nullptr;
Ytai Ben-Tsvi5120eda2020-03-26 09:41:15 -07002820 }
Ytai Ben-Tsvid9f82832020-08-27 09:31:10 -07002821
2822private:
2823 // Need this in order to keep the death receipent alive.
2824 sp<IAudioPolicyService> mAps;
2825 sp<AudioSystem::CaptureStateListener> mListener;
Ytai Ben-Tsvi5120eda2020-03-26 09:41:15 -07002826};
2827
2828status_t AudioSystem::registerSoundTriggerCaptureStateListener(
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002829 const sp<CaptureStateListener>& listener) {
Ytai Ben-Tsvid9f82832020-08-27 09:31:10 -07002830 LOG_ALWAYS_FATAL_IF(listener == nullptr);
2831
Andy Hung264fa4c2024-02-21 15:52:12 -08002832 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Andy Hungce3d1102024-09-29 21:47:19 -07002833 if (aps == nullptr) return AudioPolicyServiceTraits::getError();
Ytai Ben-Tsvi5120eda2020-03-26 09:41:15 -07002834
Andy Hungacd955a2024-02-16 17:14:36 -08002835 std::lock_guard _l(AudioSystem::gSoundTriggerMutex);
Ytai Ben-Tsvid9f82832020-08-27 09:31:10 -07002836 gSoundTriggerCaptureStateListener = new CaptureStateListenerImpl(aps, listener);
Ytai Ben-Tsvia46b6d32020-08-31 13:29:11 -07002837 gSoundTriggerCaptureStateListener->init();
Ytai Ben-Tsvi5120eda2020-03-26 09:41:15 -07002838
Ytai Ben-Tsvi5120eda2020-03-26 09:41:15 -07002839 return NO_ERROR;
2840}
2841
jiabin1319f5a2021-03-30 22:21:24 +00002842status_t AudioSystem::setVibratorInfos(
2843 const std::vector<media::AudioVibratorInfo>& vibratorInfos) {
Andy Hung264fa4c2024-02-21 15:52:12 -08002844 const sp<IAudioFlinger> af = get_audio_flinger();
Andy Hungce3d1102024-09-29 21:47:19 -07002845 if (af == nullptr) return AudioFlingerServiceTraits::getError();
jiabin1319f5a2021-03-30 22:21:24 +00002846 return af->setVibratorInfos(vibratorInfos);
2847}
2848
Jiabin Huangaa6e9e32024-10-21 17:19:28 +00002849status_t AudioSystem::getMmapPolicyInfos(
jiabine99d0882021-09-17 05:21:25 +00002850 AudioMMapPolicyType policyType, std::vector<AudioMMapPolicyInfo> *policyInfos) {
Jiabin Huangaa6e9e32024-10-21 17:19:28 +00002851 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Andy Hungce3d1102024-09-29 21:47:19 -07002852 if (aps == nullptr) return AudioPolicyServiceTraits::getError();
Jiabin Huangaa6e9e32024-10-21 17:19:28 +00002853 return statusTFromBinderStatus(aps->getMmapPolicyInfos(policyType, policyInfos));
Jiabin Huangebe64102021-09-07 20:01:07 +00002854}
2855
jiabine504e7b2021-09-18 00:27:08 +00002856int32_t AudioSystem::getAAudioMixerBurstCount() {
Andy Hung264fa4c2024-02-21 15:52:12 -08002857 const sp<IAudioFlinger> af = get_audio_flinger();
Andy Hungce3d1102024-09-29 21:47:19 -07002858 if (af == nullptr) return AudioFlingerServiceTraits::getError();
jiabine504e7b2021-09-18 00:27:08 +00002859 return af->getAAudioMixerBurstCount();
2860}
2861
2862int32_t AudioSystem::getAAudioHardwareBurstMinUsec() {
Andy Hung264fa4c2024-02-21 15:52:12 -08002863 const sp<IAudioFlinger> af = get_audio_flinger();
Andy Hungce3d1102024-09-29 21:47:19 -07002864 if (af == nullptr) return AudioFlingerServiceTraits::getError();
jiabine504e7b2021-09-18 00:27:08 +00002865 return af->getAAudioHardwareBurstMinUsec();
2866}
2867
jiabina84c3d32022-12-02 18:59:55 +00002868status_t AudioSystem::getSupportedMixerAttributes(
2869 audio_port_handle_t portId, std::vector<audio_mixer_attributes_t> *mixerAttrs) {
Andy Hung264fa4c2024-02-21 15:52:12 -08002870 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Andy Hungce3d1102024-09-29 21:47:19 -07002871 if (aps == nullptr) return AudioPolicyServiceTraits::getError();
jiabina84c3d32022-12-02 18:59:55 +00002872
2873 int32_t portIdAidl = VALUE_OR_RETURN_STATUS(legacy2aidl_audio_port_handle_t_int32_t(portId));
2874 std::vector<media::AudioMixerAttributesInternal> _aidlReturn;
2875 RETURN_STATUS_IF_ERROR(statusTFromBinderStatus(
2876 aps->getSupportedMixerAttributes(portIdAidl, &_aidlReturn)));
2877 *mixerAttrs = VALUE_OR_RETURN_STATUS(
2878 convertContainer<std::vector<audio_mixer_attributes_t>>(
2879 _aidlReturn,
2880 aidl2legacy_AudioMixerAttributesInternal_audio_mixer_attributes_t));
2881 return OK;
2882}
2883
2884status_t AudioSystem::setPreferredMixerAttributes(const audio_attributes_t *attr,
2885 audio_port_handle_t portId,
2886 uid_t uid,
2887 const audio_mixer_attributes_t *mixerAttr) {
Andy Hung264fa4c2024-02-21 15:52:12 -08002888 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Andy Hungce3d1102024-09-29 21:47:19 -07002889 if (aps == nullptr) return AudioPolicyServiceTraits::getError();
jiabina84c3d32022-12-02 18:59:55 +00002890
Mikhail Naganov1c400902023-05-17 11:48:43 -07002891 media::audio::common::AudioAttributes attrAidl = VALUE_OR_RETURN_STATUS(
2892 legacy2aidl_audio_attributes_t_AudioAttributes(*attr));
jiabina84c3d32022-12-02 18:59:55 +00002893 media::AudioMixerAttributesInternal mixerAttrAidl = VALUE_OR_RETURN_STATUS(
2894 legacy2aidl_audio_mixer_attributes_t_AudioMixerAttributesInternal(*mixerAttr));
2895 int32_t uidAidl = VALUE_OR_RETURN_STATUS(legacy2aidl_uid_t_int32_t(uid));
2896 int32_t portIdAidl = VALUE_OR_RETURN_STATUS(legacy2aidl_audio_port_handle_t_int32_t(portId));
2897
2898 return statusTFromBinderStatus(
2899 aps->setPreferredMixerAttributes(attrAidl, portIdAidl, uidAidl, mixerAttrAidl));
2900}
2901
2902status_t AudioSystem::getPreferredMixerAttributes(
2903 const audio_attributes_t *attr,
2904 audio_port_handle_t portId,
2905 std::optional<audio_mixer_attributes_t> *mixerAttr) {
Andy Hung264fa4c2024-02-21 15:52:12 -08002906 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Andy Hungce3d1102024-09-29 21:47:19 -07002907 if (aps == nullptr) return AudioPolicyServiceTraits::getError();
jiabina84c3d32022-12-02 18:59:55 +00002908
Mikhail Naganov1c400902023-05-17 11:48:43 -07002909 media::audio::common::AudioAttributes attrAidl = VALUE_OR_RETURN_STATUS(
2910 legacy2aidl_audio_attributes_t_AudioAttributes(*attr));
jiabina84c3d32022-12-02 18:59:55 +00002911 int32_t portIdAidl = VALUE_OR_RETURN_STATUS(legacy2aidl_audio_port_handle_t_int32_t(portId));
2912 std::optional<media::AudioMixerAttributesInternal> _aidlReturn;
2913 RETURN_STATUS_IF_ERROR(statusTFromBinderStatus(
2914 aps->getPreferredMixerAttributes(attrAidl, portIdAidl, &_aidlReturn)));
2915
2916 if (_aidlReturn.has_value()) {
2917 *mixerAttr = VALUE_OR_RETURN_STATUS(
2918 aidl2legacy_AudioMixerAttributesInternal_audio_mixer_attributes_t(
2919 _aidlReturn.value()));
2920 }
2921 return NO_ERROR;
2922}
2923
2924status_t AudioSystem::clearPreferredMixerAttributes(const audio_attributes_t *attr,
2925 audio_port_handle_t portId,
2926 uid_t uid) {
Andy Hung264fa4c2024-02-21 15:52:12 -08002927 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Andy Hungce3d1102024-09-29 21:47:19 -07002928 if (aps == nullptr) return AudioPolicyServiceTraits::getError();
jiabina84c3d32022-12-02 18:59:55 +00002929
Mikhail Naganov1c400902023-05-17 11:48:43 -07002930 media::audio::common::AudioAttributes attrAidl = VALUE_OR_RETURN_STATUS(
2931 legacy2aidl_audio_attributes_t_AudioAttributes(*attr));
jiabina84c3d32022-12-02 18:59:55 +00002932 int32_t uidAidl = VALUE_OR_RETURN_STATUS(legacy2aidl_uid_t_int32_t(uid));
2933 int32_t portIdAidl = VALUE_OR_RETURN_STATUS(legacy2aidl_audio_port_handle_t_int32_t(portId));
2934 return statusTFromBinderStatus(
2935 aps->clearPreferredMixerAttributes(attrAidl, portIdAidl, uidAidl));
2936}
2937
Jiabin Huangaa6e9e32024-10-21 17:19:28 +00002938status_t AudioSystem::getMmapPolicyForDevice(AudioMMapPolicyType policyType,
2939 audio_devices_t device,
2940 AudioMMapPolicyInfo *policyInfo) {
2941 const sp<IAudioPolicyService> aps = get_audio_policy_service();
2942 if (aps == nullptr) {
2943 return PERMISSION_DENIED;
2944 }
2945 policyInfo->device.type = VALUE_OR_RETURN_STATUS(
2946 legacy2aidl_audio_devices_t_AudioDeviceDescription(device));
2947 return statusTFromBinderStatus(aps->getMmapPolicyForDevice(policyType, policyInfo));
2948}
2949
Eric Laurentc2f1f072009-07-17 12:17:14 -07002950// ---------------------------------------------------------------------------
2951
Eric Laurente8726fe2015-06-26 09:39:24 -07002952int AudioSystem::AudioPolicyServiceClient::addAudioPortCallback(
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002953 const sp<AudioPortCallback>& callback) {
Andy Hungacd955a2024-02-16 17:14:36 -08002954 std::lock_guard _l(mMutex);
Andy Hungd211ce02024-02-16 15:34:07 -08002955 return mAudioPortCallbacks.insert(callback).second ? mAudioPortCallbacks.size() : -1;
Eric Laurentb28753e2015-04-01 13:06:28 -07002956}
2957
Eric Laurente8726fe2015-06-26 09:39:24 -07002958int AudioSystem::AudioPolicyServiceClient::removeAudioPortCallback(
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002959 const sp<AudioPortCallback>& callback) {
Andy Hungacd955a2024-02-16 17:14:36 -08002960 std::lock_guard _l(mMutex);
Andy Hungd211ce02024-02-16 15:34:07 -08002961 return mAudioPortCallbacks.erase(callback) > 0 ? mAudioPortCallbacks.size() : -1;
Eric Laurentb28753e2015-04-01 13:06:28 -07002962}
2963
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002964Status AudioSystem::AudioPolicyServiceClient::onAudioPortListUpdate() {
Andy Hungacd955a2024-02-16 17:14:36 -08002965 std::lock_guard _l(mMutex);
Andy Hungd211ce02024-02-16 15:34:07 -08002966 for (const auto& callback : mAudioPortCallbacks) {
2967 callback->onAudioPortListUpdate();
Eric Laurentb28753e2015-04-01 13:06:28 -07002968 }
Ytai Ben-Tsvi7e7a79d2020-12-15 16:48:16 -08002969 return Status::ok();
Eric Laurentb28753e2015-04-01 13:06:28 -07002970}
2971
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002972Status AudioSystem::AudioPolicyServiceClient::onAudioPatchListUpdate() {
Andy Hungacd955a2024-02-16 17:14:36 -08002973 std::lock_guard _l(mMutex);
Andy Hungd211ce02024-02-16 15:34:07 -08002974 for (const auto& callback : mAudioPortCallbacks) {
2975 callback->onAudioPatchListUpdate();
Eric Laurentb28753e2015-04-01 13:06:28 -07002976 }
Ytai Ben-Tsvi7e7a79d2020-12-15 16:48:16 -08002977 return Status::ok();
Eric Laurentb28753e2015-04-01 13:06:28 -07002978}
2979
François Gaffiecfe17322018-11-07 13:41:29 +01002980// ----------------------------------------------------------------------------
2981int AudioSystem::AudioPolicyServiceClient::addAudioVolumeGroupCallback(
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002982 const sp<AudioVolumeGroupCallback>& callback) {
Andy Hungacd955a2024-02-16 17:14:36 -08002983 std::lock_guard _l(mMutex);
Andy Hungd211ce02024-02-16 15:34:07 -08002984 return mAudioVolumeGroupCallbacks.insert(callback).second
2985 ? mAudioVolumeGroupCallbacks.size() : -1;
François Gaffiecfe17322018-11-07 13:41:29 +01002986}
2987
2988int AudioSystem::AudioPolicyServiceClient::removeAudioVolumeGroupCallback(
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002989 const sp<AudioVolumeGroupCallback>& callback) {
Andy Hungacd955a2024-02-16 17:14:36 -08002990 std::lock_guard _l(mMutex);
Andy Hungd211ce02024-02-16 15:34:07 -08002991 return mAudioVolumeGroupCallbacks.erase(callback) > 0
2992 ? mAudioVolumeGroupCallbacks.size() : -1;
François Gaffiecfe17322018-11-07 13:41:29 +01002993}
2994
Ytai Ben-Tsvi7e7a79d2020-12-15 16:48:16 -08002995Status AudioSystem::AudioPolicyServiceClient::onAudioVolumeGroupChanged(int32_t group,
2996 int32_t flags) {
2997 volume_group_t groupLegacy = VALUE_OR_RETURN_BINDER_STATUS(
2998 aidl2legacy_int32_t_volume_group_t(group));
2999 int flagsLegacy = VALUE_OR_RETURN_BINDER_STATUS(convertReinterpret<int>(flags));
3000
Andy Hungacd955a2024-02-16 17:14:36 -08003001 std::lock_guard _l(mMutex);
Andy Hungd211ce02024-02-16 15:34:07 -08003002 for (const auto& callback : mAudioVolumeGroupCallbacks) {
3003 callback->onAudioVolumeGroupChanged(groupLegacy, flagsLegacy);
François Gaffiecfe17322018-11-07 13:41:29 +01003004 }
Ytai Ben-Tsvi7e7a79d2020-12-15 16:48:16 -08003005 return Status::ok();
François Gaffiecfe17322018-11-07 13:41:29 +01003006}
3007// ----------------------------------------------------------------------------
3008
Ytai Ben-Tsvi7e7a79d2020-12-15 16:48:16 -08003009Status AudioSystem::AudioPolicyServiceClient::onDynamicPolicyMixStateUpdate(
3010 const ::std::string& regId, int32_t state) {
3011 ALOGV("AudioPolicyServiceClient::onDynamicPolicyMixStateUpdate(%s, %d)", regId.c_str(), state);
3012
3013 String8 regIdLegacy = VALUE_OR_RETURN_BINDER_STATUS(aidl2legacy_string_view_String8(regId));
3014 int stateLegacy = VALUE_OR_RETURN_BINDER_STATUS(convertReinterpret<int>(state));
Jean-Michel Trivif613d422015-04-23 18:41:29 -07003015 dynamic_policy_callback cb = NULL;
3016 {
Andy Hungacd955a2024-02-16 17:14:36 -08003017 std::lock_guard _l(AudioSystem::gMutex);
Jean-Michel Trivif613d422015-04-23 18:41:29 -07003018 cb = gDynPolicyCallback;
3019 }
3020
3021 if (cb != NULL) {
Ytai Ben-Tsvi7e7a79d2020-12-15 16:48:16 -08003022 cb(DYNAMIC_POLICY_EVENT_MIX_STATE_UPDATE, regIdLegacy, stateLegacy);
Jean-Michel Trivif613d422015-04-23 18:41:29 -07003023 }
Ytai Ben-Tsvi7e7a79d2020-12-15 16:48:16 -08003024 return Status::ok();
Jean-Michel Trivide801052015-04-14 19:10:14 -07003025}
3026
Ytai Ben-Tsvi7e7a79d2020-12-15 16:48:16 -08003027Status AudioSystem::AudioPolicyServiceClient::onRecordingConfigurationUpdate(
3028 int32_t event,
3029 const media::RecordClientInfo& clientInfo,
Mikhail Naganovdbf03642021-08-25 18:15:32 -07003030 const AudioConfigBase& clientConfig,
Ytai Ben-Tsvi7e7a79d2020-12-15 16:48:16 -08003031 const std::vector<media::EffectDescriptor>& clientEffects,
Mikhail Naganovdbf03642021-08-25 18:15:32 -07003032 const AudioConfigBase& deviceConfig,
Ytai Ben-Tsvi7e7a79d2020-12-15 16:48:16 -08003033 const std::vector<media::EffectDescriptor>& effects,
3034 int32_t patchHandle,
Mikhail Naganovddceecc2021-09-03 13:58:56 -07003035 AudioSource source) {
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -08003036 record_config_callback cb = NULL;
3037 {
Andy Hungacd955a2024-02-16 17:14:36 -08003038 std::lock_guard _l(AudioSystem::gMutex);
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -08003039 cb = gRecordConfigCallback;
3040 }
3041
3042 if (cb != NULL) {
Ytai Ben-Tsvi7e7a79d2020-12-15 16:48:16 -08003043 int eventLegacy = VALUE_OR_RETURN_BINDER_STATUS(convertReinterpret<int>(event));
3044 record_client_info_t clientInfoLegacy = VALUE_OR_RETURN_BINDER_STATUS(
3045 aidl2legacy_RecordClientInfo_record_client_info_t(clientInfo));
3046 audio_config_base_t clientConfigLegacy = VALUE_OR_RETURN_BINDER_STATUS(
Mikhail Naganovde3fa182021-07-30 15:06:42 -07003047 aidl2legacy_AudioConfigBase_audio_config_base_t(clientConfig, true /*isInput*/));
Ytai Ben-Tsvi7e7a79d2020-12-15 16:48:16 -08003048 std::vector<effect_descriptor_t> clientEffectsLegacy = VALUE_OR_RETURN_BINDER_STATUS(
3049 convertContainer<std::vector<effect_descriptor_t>>(
3050 clientEffects,
3051 aidl2legacy_EffectDescriptor_effect_descriptor_t));
3052 audio_config_base_t deviceConfigLegacy = VALUE_OR_RETURN_BINDER_STATUS(
Mikhail Naganovde3fa182021-07-30 15:06:42 -07003053 aidl2legacy_AudioConfigBase_audio_config_base_t(deviceConfig, true /*isInput*/));
Ytai Ben-Tsvi7e7a79d2020-12-15 16:48:16 -08003054 std::vector<effect_descriptor_t> effectsLegacy = VALUE_OR_RETURN_BINDER_STATUS(
3055 convertContainer<std::vector<effect_descriptor_t>>(
3056 effects,
3057 aidl2legacy_EffectDescriptor_effect_descriptor_t));
3058 audio_patch_handle_t patchHandleLegacy = VALUE_OR_RETURN_BINDER_STATUS(
3059 aidl2legacy_int32_t_audio_patch_handle_t(patchHandle));
3060 audio_source_t sourceLegacy = VALUE_OR_RETURN_BINDER_STATUS(
Mikhail Naganovddceecc2021-09-03 13:58:56 -07003061 aidl2legacy_AudioSource_audio_source_t(source));
Ytai Ben-Tsvi7e7a79d2020-12-15 16:48:16 -08003062 cb(eventLegacy, &clientInfoLegacy, &clientConfigLegacy, clientEffectsLegacy,
3063 &deviceConfigLegacy, effectsLegacy, patchHandleLegacy, sourceLegacy);
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -08003064 }
Ytai Ben-Tsvi7e7a79d2020-12-15 16:48:16 -08003065 return Status::ok();
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -08003066}
3067
Jean-Michel Trivi9a6b9ad2020-10-22 16:46:43 -07003068Status AudioSystem::AudioPolicyServiceClient::onRoutingUpdated() {
3069 routing_callback cb = NULL;
3070 {
Andy Hungacd955a2024-02-16 17:14:36 -08003071 std::lock_guard _l(AudioSystem::gMutex);
Jean-Michel Trivi9a6b9ad2020-10-22 16:46:43 -07003072 cb = gRoutingCallback;
3073 }
3074
3075 if (cb != NULL) {
3076 cb();
3077 }
3078 return Status::ok();
3079}
3080
Jean-Michel Trivi78f2b302022-04-15 18:18:41 +00003081Status AudioSystem::AudioPolicyServiceClient::onVolumeRangeInitRequest() {
3082 vol_range_init_req_callback cb = NULL;
3083 {
Andy Hungacd955a2024-02-16 17:14:36 -08003084 std::lock_guard _l(AudioSystem::gMutex);
Jean-Michel Trivi78f2b302022-04-15 18:18:41 +00003085 cb = gVolRangeInitReqCallback;
3086 }
3087
3088 if (cb != NULL) {
3089 cb();
3090 }
3091 return Status::ok();
3092}
3093
Andy Hungce3d1102024-09-29 21:47:19 -07003094void AudioSystem::AudioPolicyServiceClient::onServiceDied() {
3095 std::lock_guard _l(mMutex);
3096 for (const auto& callback : mAudioPortCallbacks) {
3097 callback->onServiceDied();
Eric Laurentb52c1522014-05-20 11:27:36 -07003098 }
Andy Hungce3d1102024-09-29 21:47:19 -07003099 for (const auto& callback : mAudioVolumeGroupCallbacks) {
3100 callback->onServiceDied();
3101 }
Eric Laurentc2f1f072009-07-17 12:17:14 -07003102}
3103
Ytai Ben-Tsvi7e7a79d2020-12-15 16:48:16 -08003104ConversionResult<record_client_info_t>
3105aidl2legacy_RecordClientInfo_record_client_info_t(const media::RecordClientInfo& aidl) {
3106 record_client_info_t legacy;
3107 legacy.riid = VALUE_OR_RETURN(aidl2legacy_int32_t_audio_unique_id_t(aidl.riid));
3108 legacy.uid = VALUE_OR_RETURN(aidl2legacy_int32_t_uid_t(aidl.uid));
3109 legacy.session = VALUE_OR_RETURN(aidl2legacy_int32_t_audio_session_t(aidl.session));
Mikhail Naganovddceecc2021-09-03 13:58:56 -07003110 legacy.source = VALUE_OR_RETURN(aidl2legacy_AudioSource_audio_source_t(aidl.source));
Ytai Ben-Tsvi7e7a79d2020-12-15 16:48:16 -08003111 legacy.port_id = VALUE_OR_RETURN(aidl2legacy_int32_t_audio_port_handle_t(aidl.portId));
3112 legacy.silenced = aidl.silenced;
3113 return legacy;
3114}
3115
3116ConversionResult<media::RecordClientInfo>
3117legacy2aidl_record_client_info_t_RecordClientInfo(const record_client_info_t& legacy) {
3118 media::RecordClientInfo aidl;
3119 aidl.riid = VALUE_OR_RETURN(legacy2aidl_audio_unique_id_t_int32_t(legacy.riid));
3120 aidl.uid = VALUE_OR_RETURN(legacy2aidl_uid_t_int32_t(legacy.uid));
3121 aidl.session = VALUE_OR_RETURN(legacy2aidl_audio_session_t_int32_t(legacy.session));
Mikhail Naganovddceecc2021-09-03 13:58:56 -07003122 aidl.source = VALUE_OR_RETURN(legacy2aidl_audio_source_t_AudioSource(legacy.source));
Ytai Ben-Tsvi7e7a79d2020-12-15 16:48:16 -08003123 aidl.portId = VALUE_OR_RETURN(legacy2aidl_audio_port_handle_t_int32_t(legacy.port_id));
3124 aidl.silenced = legacy.silenced;
3125 return aidl;
3126}
3127
Glenn Kasten40bc9062015-03-20 09:09:33 -07003128} // namespace android