blob: 1430913fcf234c9df18e83ef1467283fa4c1a577 [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,
Eric Laurentcbc3f6f2025-01-09 10:48:53 +00001402 audio_port_handle_t* portId,
1403 audio_source_t* source) {
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001404 if (attr == NULL) {
1405 ALOGE("getInputForAttr NULL attr - shouldn't happen");
1406 return BAD_VALUE;
1407 }
1408 if (input == NULL) {
1409 ALOGE("getInputForAttr NULL input - shouldn't happen");
1410 return BAD_VALUE;
1411 }
1412 if (selectedDeviceId == NULL) {
1413 ALOGE("getInputForAttr NULL selectedDeviceId - shouldn't happen");
1414 return BAD_VALUE;
1415 }
1416 if (portId == NULL) {
1417 ALOGE("getInputForAttr NULL portId - shouldn't happen");
1418 return BAD_VALUE;
1419 }
1420
Andy Hung264fa4c2024-02-21 15:52:12 -08001421 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Andy Hungce3d1102024-09-29 21:47:19 -07001422 if (aps == nullptr) return NO_INIT;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001423
Mikhail Naganov1c400902023-05-17 11:48:43 -07001424 media::audio::common::AudioAttributes attrAidl = VALUE_OR_RETURN_STATUS(
1425 legacy2aidl_audio_attributes_t_AudioAttributes(*attr));
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001426 int32_t inputAidl = VALUE_OR_RETURN_STATUS(legacy2aidl_audio_io_handle_t_int32_t(*input));
1427 int32_t riidAidl = VALUE_OR_RETURN_STATUS(legacy2aidl_audio_unique_id_t_int32_t(riid));
1428 int32_t sessionAidl = VALUE_OR_RETURN_STATUS(legacy2aidl_audio_session_t_int32_t(session));
Mikhail Naganovdbf03642021-08-25 18:15:32 -07001429 AudioConfigBase configAidl = VALUE_OR_RETURN_STATUS(
Mikhail Naganovde3fa182021-07-30 15:06:42 -07001430 legacy2aidl_audio_config_base_t_AudioConfigBase(*config, true /*isInput*/));
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001431 int32_t flagsAidl = VALUE_OR_RETURN_STATUS(legacy2aidl_audio_input_flags_t_int32_t_mask(flags));
Eric Laurentf99edd32021-02-01 15:57:33 +01001432 int32_t selectedDeviceIdAidl = VALUE_OR_RETURN_STATUS(
1433 legacy2aidl_audio_port_handle_t_int32_t(*selectedDeviceId));
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001434
1435 media::GetInputForAttrResponse response;
1436
Atneya Nair89ff0432024-11-19 12:47:28 -08001437 const Status res = aps->getInputForAttr(attrAidl, inputAidl, riidAidl, sessionAidl,
1438 attributionSource, configAidl, flagsAidl,
1439 selectedDeviceIdAidl, &response);
1440 if (!res.isOk()) {
1441 ALOGE("getInputForAttr error: %s", res.toString8().c_str());
jiabinf1c73972022-04-14 16:28:52 -07001442 *config = VALUE_OR_RETURN_STATUS(
1443 aidl2legacy_AudioConfigBase_audio_config_base_t(response.config, true /*isInput*/));
Atneya Nair89ff0432024-11-19 12:47:28 -08001444 return statusTFromBinderStatus(res);
jiabinf1c73972022-04-14 16:28:52 -07001445 }
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001446
1447 *input = VALUE_OR_RETURN_STATUS(aidl2legacy_int32_t_audio_io_handle_t(response.input));
1448 *selectedDeviceId = VALUE_OR_RETURN_STATUS(
1449 aidl2legacy_int32_t_audio_port_handle_t(response.selectedDeviceId));
1450 *portId = VALUE_OR_RETURN_STATUS(aidl2legacy_int32_t_audio_port_handle_t(response.portId));
Eric Laurentcbc3f6f2025-01-09 10:48:53 +00001451 *source = VALUE_OR_RETURN_STATUS(aidl2legacy_AudioSource_audio_source_t(response.source));
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001452 return OK;
Eric Laurentc2f1f072009-07-17 12:17:14 -07001453}
1454
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001455status_t AudioSystem::startInput(audio_port_handle_t portId) {
Andy Hung264fa4c2024-02-21 15:52:12 -08001456 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Andy Hungce3d1102024-09-29 21:47:19 -07001457 if (aps == nullptr) return AudioPolicyServiceTraits::getError();
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001458
1459 int32_t portIdAidl = VALUE_OR_RETURN_STATUS(legacy2aidl_audio_port_handle_t_int32_t(portId));
1460 return statusTFromBinderStatus(aps->startInput(portIdAidl));
Eric Laurentc2f1f072009-07-17 12:17:14 -07001461}
1462
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001463status_t AudioSystem::stopInput(audio_port_handle_t portId) {
Andy Hung264fa4c2024-02-21 15:52:12 -08001464 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Andy Hungce3d1102024-09-29 21:47:19 -07001465 if (aps == nullptr) return AudioPolicyServiceTraits::getError();
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001466
1467 int32_t portIdAidl = VALUE_OR_RETURN_STATUS(legacy2aidl_audio_port_handle_t_int32_t(portId));
1468 return statusTFromBinderStatus(aps->stopInput(portIdAidl));
Eric Laurentc2f1f072009-07-17 12:17:14 -07001469}
1470
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001471void AudioSystem::releaseInput(audio_port_handle_t portId) {
Andy Hung264fa4c2024-02-21 15:52:12 -08001472 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Andy Hungce3d1102024-09-29 21:47:19 -07001473 if (aps == nullptr) return;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001474
1475 auto status = [&]() -> status_t {
1476 int32_t portIdAidl = VALUE_OR_RETURN_STATUS(
1477 legacy2aidl_audio_port_handle_t_int32_t(portId));
1478 RETURN_STATUS_IF_ERROR(statusTFromBinderStatus(aps->releaseInput(portIdAidl)));
1479 return OK;
1480 }();
1481
1482 // Ignore status.
1483 (void) status;
Eric Laurentc2f1f072009-07-17 12:17:14 -07001484}
1485
Vlad Popa87e0e582024-05-20 18:49:20 -07001486status_t AudioSystem::setDeviceAbsoluteVolumeEnabled(audio_devices_t deviceType,
1487 const char *address,
1488 bool enabled,
1489 audio_stream_type_t streamToDriveAbs) {
1490 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Andy Hungce3d1102024-09-29 21:47:19 -07001491 if (aps == nullptr) return AudioPolicyServiceTraits::getError();
Vlad Popa87e0e582024-05-20 18:49:20 -07001492
1493 AudioDevice deviceAidl = VALUE_OR_RETURN_STATUS(
1494 legacy2aidl_audio_device_AudioDevice(deviceType, address));
1495 AudioStreamType streamToDriveAbsAidl = VALUE_OR_RETURN_STATUS(
1496 legacy2aidl_audio_stream_type_t_AudioStreamType(streamToDriveAbs));
1497 return statusTFromBinderStatus(
1498 aps->setDeviceAbsoluteVolumeEnabled(deviceAidl, enabled, streamToDriveAbsAidl));
1499}
1500
Dima Zavinfce7a472011-04-19 22:30:36 -07001501status_t AudioSystem::initStreamVolume(audio_stream_type_t stream,
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001502 int indexMin,
1503 int indexMax) {
Andy Hung264fa4c2024-02-21 15:52:12 -08001504 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Andy Hungce3d1102024-09-29 21:47:19 -07001505 if (aps == nullptr) return AudioPolicyServiceTraits::getError();
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001506
Mikhail Naganovdbf03642021-08-25 18:15:32 -07001507 AudioStreamType streamAidl = VALUE_OR_RETURN_STATUS(
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001508 legacy2aidl_audio_stream_type_t_AudioStreamType(stream));
1509 int32_t indexMinAidl = VALUE_OR_RETURN_STATUS(convertIntegral<int32_t>(indexMin));
1510 int32_t indexMaxAidl = VALUE_OR_RETURN_STATUS(convertIntegral<int32_t>(indexMax));
Mikhail Naganovec3d5792022-05-06 00:19:55 +00001511 status_t status = statusTFromBinderStatus(
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001512 aps->initStreamVolume(streamAidl, indexMinAidl, indexMaxAidl));
Mikhail Naganovec3d5792022-05-06 00:19:55 +00001513 return status;
Eric Laurentc2f1f072009-07-17 12:17:14 -07001514}
1515
Eric Laurent83844cc2011-11-18 16:43:31 -08001516status_t AudioSystem::setStreamVolumeIndex(audio_stream_type_t stream,
1517 int index,
Vlad Popa1e865e62024-08-15 19:11:42 -07001518 bool muted,
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001519 audio_devices_t device) {
Andy Hung264fa4c2024-02-21 15:52:12 -08001520 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Andy Hungce3d1102024-09-29 21:47:19 -07001521 if (aps == nullptr) return AudioPolicyServiceTraits::getError();
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001522
Mikhail Naganovdbf03642021-08-25 18:15:32 -07001523 AudioStreamType streamAidl = VALUE_OR_RETURN_STATUS(
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001524 legacy2aidl_audio_stream_type_t_AudioStreamType(stream));
1525 int32_t indexAidl = VALUE_OR_RETURN_STATUS(convertIntegral<int32_t>(index));
Mikhail Naganovf4a75362021-09-16 00:02:54 +00001526 AudioDeviceDescription deviceAidl = VALUE_OR_RETURN_STATUS(
Mikhail Naganov21a32ec2021-07-08 14:40:12 -07001527 legacy2aidl_audio_devices_t_AudioDeviceDescription(device));
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001528 return statusTFromBinderStatus(
Vlad Popa1e865e62024-08-15 19:11:42 -07001529 aps->setStreamVolumeIndex(streamAidl, deviceAidl, indexAidl, muted));
Eric Laurentc2f1f072009-07-17 12:17:14 -07001530}
1531
Eric Laurent83844cc2011-11-18 16:43:31 -08001532status_t AudioSystem::getStreamVolumeIndex(audio_stream_type_t stream,
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001533 int* index,
1534 audio_devices_t device) {
Andy Hung264fa4c2024-02-21 15:52:12 -08001535 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Andy Hungce3d1102024-09-29 21:47:19 -07001536 if (aps == nullptr) return AudioPolicyServiceTraits::getError();
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001537
Mikhail Naganovdbf03642021-08-25 18:15:32 -07001538 AudioStreamType streamAidl = VALUE_OR_RETURN_STATUS(
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001539 legacy2aidl_audio_stream_type_t_AudioStreamType(stream));
Mikhail Naganovf4a75362021-09-16 00:02:54 +00001540 AudioDeviceDescription deviceAidl = VALUE_OR_RETURN_STATUS(
Mikhail Naganov21a32ec2021-07-08 14:40:12 -07001541 legacy2aidl_audio_devices_t_AudioDeviceDescription(device));
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001542 int32_t indexAidl;
1543 RETURN_STATUS_IF_ERROR(statusTFromBinderStatus(
1544 aps->getStreamVolumeIndex(streamAidl, deviceAidl, &indexAidl)));
1545 if (index != nullptr) {
1546 *index = VALUE_OR_RETURN_STATUS(convertIntegral<int>(indexAidl));
1547 }
1548 return OK;
Eric Laurentc2f1f072009-07-17 12:17:14 -07001549}
1550
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001551status_t AudioSystem::setVolumeIndexForAttributes(const audio_attributes_t& attr,
François Gaffiecfe17322018-11-07 13:41:29 +01001552 int index,
Vlad Popa1e865e62024-08-15 19:11:42 -07001553 bool muted,
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001554 audio_devices_t device) {
Andy Hung264fa4c2024-02-21 15:52:12 -08001555 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Andy Hungce3d1102024-09-29 21:47:19 -07001556 if (aps == nullptr) return AudioPolicyServiceTraits::getError();
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001557
Mikhail Naganov1c400902023-05-17 11:48:43 -07001558 media::audio::common::AudioAttributes attrAidl = VALUE_OR_RETURN_STATUS(
1559 legacy2aidl_audio_attributes_t_AudioAttributes(attr));
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001560 int32_t indexAidl = VALUE_OR_RETURN_STATUS(convertIntegral<int32_t>(index));
Mikhail Naganovf4a75362021-09-16 00:02:54 +00001561 AudioDeviceDescription deviceAidl = VALUE_OR_RETURN_STATUS(
Mikhail Naganov21a32ec2021-07-08 14:40:12 -07001562 legacy2aidl_audio_devices_t_AudioDeviceDescription(device));
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001563 return statusTFromBinderStatus(
Vlad Popa1e865e62024-08-15 19:11:42 -07001564 aps->setVolumeIndexForAttributes(attrAidl, deviceAidl, indexAidl, muted));
François Gaffiecfe17322018-11-07 13:41:29 +01001565}
1566
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001567status_t AudioSystem::getVolumeIndexForAttributes(const audio_attributes_t& attr,
1568 int& index,
1569 audio_devices_t device) {
Andy Hung264fa4c2024-02-21 15:52:12 -08001570 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Andy Hungce3d1102024-09-29 21:47:19 -07001571 if (aps == nullptr) return AudioPolicyServiceTraits::getError();
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001572
Mikhail Naganov1c400902023-05-17 11:48:43 -07001573 media::audio::common::AudioAttributes attrAidl = VALUE_OR_RETURN_STATUS(
1574 legacy2aidl_audio_attributes_t_AudioAttributes(attr));
Mikhail Naganovf4a75362021-09-16 00:02:54 +00001575 AudioDeviceDescription deviceAidl = VALUE_OR_RETURN_STATUS(
Mikhail Naganov21a32ec2021-07-08 14:40:12 -07001576 legacy2aidl_audio_devices_t_AudioDeviceDescription(device));
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001577 int32_t indexAidl;
1578 RETURN_STATUS_IF_ERROR(statusTFromBinderStatus(
1579 aps->getVolumeIndexForAttributes(attrAidl, deviceAidl, &indexAidl)));
1580 index = VALUE_OR_RETURN_STATUS(convertIntegral<int>(indexAidl));
1581 return OK;
François Gaffiecfe17322018-11-07 13:41:29 +01001582}
1583
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001584status_t AudioSystem::getMaxVolumeIndexForAttributes(const audio_attributes_t& attr, int& index) {
Andy Hung264fa4c2024-02-21 15:52:12 -08001585 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Andy Hungce3d1102024-09-29 21:47:19 -07001586 if (aps == nullptr) return AudioPolicyServiceTraits::getError();
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001587
Mikhail Naganov1c400902023-05-17 11:48:43 -07001588 media::audio::common::AudioAttributes attrAidl = VALUE_OR_RETURN_STATUS(
1589 legacy2aidl_audio_attributes_t_AudioAttributes(attr));
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001590 int32_t indexAidl;
1591 RETURN_STATUS_IF_ERROR(statusTFromBinderStatus(
1592 aps->getMaxVolumeIndexForAttributes(attrAidl, &indexAidl)));
1593 index = VALUE_OR_RETURN_STATUS(convertIntegral<int>(indexAidl));
1594 return OK;
François Gaffiecfe17322018-11-07 13:41:29 +01001595}
1596
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001597status_t AudioSystem::getMinVolumeIndexForAttributes(const audio_attributes_t& attr, int& index) {
Andy Hung264fa4c2024-02-21 15:52:12 -08001598 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Andy Hungce3d1102024-09-29 21:47:19 -07001599 if (aps == nullptr) return AudioPolicyServiceTraits::getError();
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001600
Mikhail Naganov1c400902023-05-17 11:48:43 -07001601 media::audio::common::AudioAttributes attrAidl = VALUE_OR_RETURN_STATUS(
1602 legacy2aidl_audio_attributes_t_AudioAttributes(attr));
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001603 int32_t indexAidl;
1604 RETURN_STATUS_IF_ERROR(statusTFromBinderStatus(
1605 aps->getMinVolumeIndexForAttributes(attrAidl, &indexAidl)));
1606 index = VALUE_OR_RETURN_STATUS(convertIntegral<int>(indexAidl));
1607 return OK;
François Gaffiecfe17322018-11-07 13:41:29 +01001608}
1609
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001610product_strategy_t AudioSystem::getStrategyForStream(audio_stream_type_t stream) {
Andy Hung264fa4c2024-02-21 15:52:12 -08001611 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Andy Hungce3d1102024-09-29 21:47:19 -07001612 if (aps == nullptr) return PRODUCT_STRATEGY_NONE;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001613
1614 auto result = [&]() -> ConversionResult<product_strategy_t> {
Mikhail Naganovdbf03642021-08-25 18:15:32 -07001615 AudioStreamType streamAidl = VALUE_OR_RETURN(
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001616 legacy2aidl_audio_stream_type_t_AudioStreamType(stream));
1617 int32_t resultAidl;
1618 RETURN_IF_ERROR(statusTFromBinderStatus(
1619 aps->getStrategyForStream(streamAidl, &resultAidl)));
1620 return aidl2legacy_int32_t_product_strategy_t(resultAidl);
1621 }();
1622 return result.value_or(PRODUCT_STRATEGY_NONE);
Eric Laurentde070132010-07-13 04:45:46 -07001623}
1624
François Gaffie1e2b56f2022-04-01 14:34:29 +02001625status_t AudioSystem::getDevicesForAttributes(const audio_attributes_t& aa,
Andy Hung6d23c0f2022-02-16 09:37:15 -08001626 AudioDeviceTypeAddrVector* devices,
1627 bool forVolume) {
Jean-Michel Trivif41599b2020-01-07 14:22:08 -08001628 if (devices == nullptr) {
1629 return BAD_VALUE;
1630 }
Andy Hung264fa4c2024-02-21 15:52:12 -08001631 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Andy Hungce3d1102024-09-29 21:47:19 -07001632 if (aps == nullptr) return AudioPolicyServiceTraits::getError();
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001633
Mikhail Naganov1c400902023-05-17 11:48:43 -07001634 media::audio::common::AudioAttributes aaAidl = VALUE_OR_RETURN_STATUS(
1635 legacy2aidl_audio_attributes_t_AudioAttributes(aa));
Mikhail Naganovf4a75362021-09-16 00:02:54 +00001636 std::vector<AudioDevice> retAidl;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001637 RETURN_STATUS_IF_ERROR(
Andy Hung6d23c0f2022-02-16 09:37:15 -08001638 statusTFromBinderStatus(aps->getDevicesForAttributes(aaAidl, forVolume, &retAidl)));
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001639 *devices = VALUE_OR_RETURN_STATUS(
1640 convertContainer<AudioDeviceTypeAddrVector>(
1641 retAidl,
1642 aidl2legacy_AudioDeviceTypeAddress));
1643 return OK;
Jean-Michel Trivif41599b2020-01-07 14:22:08 -08001644}
1645
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001646audio_io_handle_t AudioSystem::getOutputForEffect(const effect_descriptor_t* desc) {
Andy Hung264fa4c2024-02-21 15:52:12 -08001647 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Glenn Kastenefa6ea92014-01-08 09:10:43 -08001648 // FIXME change return type to status_t, and return PERMISSION_DENIED here
Andy Hungce3d1102024-09-29 21:47:19 -07001649 if (aps == nullptr) return AUDIO_IO_HANDLE_NONE;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001650
1651 auto result = [&]() -> ConversionResult<audio_io_handle_t> {
1652 media::EffectDescriptor descAidl = VALUE_OR_RETURN(
1653 legacy2aidl_effect_descriptor_t_EffectDescriptor(*desc));
1654 int32_t retAidl;
1655 RETURN_IF_ERROR(
1656 statusTFromBinderStatus(aps->getOutputForEffect(descAidl, &retAidl)));
1657 return aidl2legacy_int32_t_audio_io_handle_t(retAidl);
1658 }();
1659
1660 return result.value_or(AUDIO_IO_HANDLE_NONE);
Eric Laurentde070132010-07-13 04:45:46 -07001661}
1662
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001663status_t AudioSystem::registerEffect(const effect_descriptor_t* desc,
1664 audio_io_handle_t io,
1665 product_strategy_t strategy,
1666 audio_session_t session,
1667 int id) {
Andy Hung264fa4c2024-02-21 15:52:12 -08001668 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Andy Hungce3d1102024-09-29 21:47:19 -07001669 if (aps == nullptr) return AudioPolicyServiceTraits::getError();
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001670
1671 media::EffectDescriptor descAidl = VALUE_OR_RETURN_STATUS(
1672 legacy2aidl_effect_descriptor_t_EffectDescriptor(*desc));
1673 int32_t ioAidl = VALUE_OR_RETURN_STATUS(legacy2aidl_audio_io_handle_t_int32_t(io));
1674 int32_t strategyAidl = VALUE_OR_RETURN_STATUS(aidl2legacy_int32_t_product_strategy_t(strategy));
1675 int32_t sessionAidl = VALUE_OR_RETURN_STATUS(legacy2aidl_audio_session_t_int32_t(session));
1676 int32_t idAidl = VALUE_OR_RETURN_STATUS(convertReinterpret<int32_t>(id));
1677 return statusTFromBinderStatus(
1678 aps->registerEffect(descAidl, ioAidl, strategyAidl, sessionAidl, idAidl));
Eric Laurentde070132010-07-13 04:45:46 -07001679}
1680
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001681status_t AudioSystem::unregisterEffect(int id) {
Andy Hung264fa4c2024-02-21 15:52:12 -08001682 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Andy Hungce3d1102024-09-29 21:47:19 -07001683 if (aps == nullptr) return AudioPolicyServiceTraits::getError();
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001684
1685 int32_t idAidl = VALUE_OR_RETURN_STATUS(convertReinterpret<int32_t>(id));
1686 return statusTFromBinderStatus(
1687 aps->unregisterEffect(idAidl));
Eric Laurentde070132010-07-13 04:45:46 -07001688}
1689
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001690status_t AudioSystem::setEffectEnabled(int id, bool enabled) {
Andy Hung264fa4c2024-02-21 15:52:12 -08001691 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Andy Hungce3d1102024-09-29 21:47:19 -07001692 if (aps == nullptr) return AudioPolicyServiceTraits::getError();
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001693
1694 int32_t idAidl = VALUE_OR_RETURN_STATUS(convertReinterpret<int32_t>(id));
1695 return statusTFromBinderStatus(
1696 aps->setEffectEnabled(idAidl, enabled));
Eric Laurentdb7c0792011-08-10 10:37:50 -07001697}
1698
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001699status_t AudioSystem::moveEffectsToIo(const std::vector<int>& ids, audio_io_handle_t io) {
Andy Hung264fa4c2024-02-21 15:52:12 -08001700 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Andy Hungce3d1102024-09-29 21:47:19 -07001701 if (aps == nullptr) return AudioPolicyServiceTraits::getError();
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001702
1703 std::vector<int32_t> idsAidl = VALUE_OR_RETURN_STATUS(
1704 convertContainer<std::vector<int32_t>>(ids, convertReinterpret<int32_t, int>));
1705 int32_t ioAidl = VALUE_OR_RETURN_STATUS(legacy2aidl_audio_io_handle_t_int32_t(io));
1706 return statusTFromBinderStatus(aps->moveEffectsToIo(idsAidl, ioAidl));
Eric Laurent6c796322019-04-09 14:13:17 -07001707}
1708
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001709status_t AudioSystem::isStreamActive(audio_stream_type_t stream, bool* state, uint32_t inPastMs) {
Andy Hung264fa4c2024-02-21 15:52:12 -08001710 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Andy Hungce3d1102024-09-29 21:47:19 -07001711 if (aps == nullptr) return AudioPolicyServiceTraits::getError();
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001712 if (state == NULL) return BAD_VALUE;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001713
Mikhail Naganovdbf03642021-08-25 18:15:32 -07001714 AudioStreamType streamAidl = VALUE_OR_RETURN_STATUS(
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001715 legacy2aidl_audio_stream_type_t_AudioStreamType(stream));
1716 int32_t inPastMsAidl = VALUE_OR_RETURN_STATUS(convertIntegral<int32_t>(inPastMs));
1717 RETURN_STATUS_IF_ERROR(statusTFromBinderStatus(
1718 aps->isStreamActive(streamAidl, inPastMsAidl, state)));
1719 return OK;
Eric Laurenteda6c362011-02-02 09:33:30 -08001720}
1721
Jean-Michel Trivi272ab542013-02-04 16:26:02 -08001722status_t AudioSystem::isStreamActiveRemotely(audio_stream_type_t stream, bool* state,
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001723 uint32_t inPastMs) {
Andy Hung264fa4c2024-02-21 15:52:12 -08001724 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Andy Hungce3d1102024-09-29 21:47:19 -07001725 if (aps == nullptr) return AudioPolicyServiceTraits::getError();
Jean-Michel Trivi272ab542013-02-04 16:26:02 -08001726 if (state == NULL) return BAD_VALUE;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001727
Mikhail Naganovdbf03642021-08-25 18:15:32 -07001728 AudioStreamType streamAidl = VALUE_OR_RETURN_STATUS(
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001729 legacy2aidl_audio_stream_type_t_AudioStreamType(stream));
1730 int32_t inPastMsAidl = VALUE_OR_RETURN_STATUS(convertIntegral<int32_t>(inPastMs));
1731 RETURN_STATUS_IF_ERROR(statusTFromBinderStatus(
1732 aps->isStreamActiveRemotely(streamAidl, inPastMsAidl, state)));
1733 return OK;
Jean-Michel Trivi272ab542013-02-04 16:26:02 -08001734}
1735
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001736status_t AudioSystem::isSourceActive(audio_source_t stream, bool* state) {
Andy Hung264fa4c2024-02-21 15:52:12 -08001737 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Andy Hungce3d1102024-09-29 21:47:19 -07001738 if (aps == nullptr) return AudioPolicyServiceTraits::getError();
Jean-Michel Trivid7086032012-10-10 12:11:16 -07001739 if (state == NULL) return BAD_VALUE;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001740
Mikhail Naganovddceecc2021-09-03 13:58:56 -07001741 AudioSource streamAidl = VALUE_OR_RETURN_STATUS(
1742 legacy2aidl_audio_source_t_AudioSource(stream));
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001743 RETURN_STATUS_IF_ERROR(statusTFromBinderStatus(
1744 aps->isSourceActive(streamAidl, state)));
1745 return OK;
Jean-Michel Trivid7086032012-10-10 12:11:16 -07001746}
1747
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001748uint32_t AudioSystem::getPrimaryOutputSamplingRate() {
Andy Hung264fa4c2024-02-21 15:52:12 -08001749 const sp<IAudioFlinger> af = get_audio_flinger();
Andy Hungce3d1102024-09-29 21:47:19 -07001750 if (af == nullptr) return 0;
Glenn Kastencc0f1cf2012-09-24 11:27:18 -07001751 return af->getPrimaryOutputSamplingRate();
1752}
1753
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001754size_t AudioSystem::getPrimaryOutputFrameCount() {
Andy Hung264fa4c2024-02-21 15:52:12 -08001755 const sp<IAudioFlinger> af = get_audio_flinger();
Andy Hungce3d1102024-09-29 21:47:19 -07001756 if (af == nullptr) return 0;
Glenn Kastencc0f1cf2012-09-24 11:27:18 -07001757 return af->getPrimaryOutputFrameCount();
1758}
Eric Laurenteda6c362011-02-02 09:33:30 -08001759
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001760status_t AudioSystem::setLowRamDevice(bool isLowRamDevice, int64_t totalMemory) {
Andy Hung264fa4c2024-02-21 15:52:12 -08001761 const sp<IAudioFlinger> af = get_audio_flinger();
Andy Hungce3d1102024-09-29 21:47:19 -07001762 if (af == nullptr) return AudioFlingerServiceTraits::getError();
Andy Hung6f248bb2018-01-23 14:04:37 -08001763 return af->setLowRamDevice(isLowRamDevice, totalMemory);
Glenn Kasten4182c4e2013-07-15 14:45:07 -07001764}
1765
Hayden Gomes524159d2019-12-23 14:41:47 -08001766status_t AudioSystem::setSupportedSystemUsages(const std::vector<audio_usage_t>& systemUsages) {
Andy Hung264fa4c2024-02-21 15:52:12 -08001767 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Andy Hungce3d1102024-09-29 21:47:19 -07001768 if (aps == nullptr) return AudioPolicyServiceTraits::getError();
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001769
Mikhail Naganovdbf03642021-08-25 18:15:32 -07001770 std::vector<AudioUsage> systemUsagesAidl = VALUE_OR_RETURN_STATUS(
1771 convertContainer<std::vector<AudioUsage>>(systemUsages,
1772 legacy2aidl_audio_usage_t_AudioUsage));
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001773 return statusTFromBinderStatus(aps->setSupportedSystemUsages(systemUsagesAidl));
Hayden Gomes524159d2019-12-23 14:41:47 -08001774}
1775
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001776status_t AudioSystem::setAllowedCapturePolicy(uid_t uid, audio_flags_mask_t capturePolicy) {
Andy Hung264fa4c2024-02-21 15:52:12 -08001777 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Andy Hungce3d1102024-09-29 21:47:19 -07001778 if (aps == nullptr) return AudioPolicyServiceTraits::getError();
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001779
1780 int32_t uidAidl = VALUE_OR_RETURN_STATUS(legacy2aidl_uid_t_int32_t(uid));
1781 int32_t capturePolicyAidl = VALUE_OR_RETURN_STATUS(
1782 legacy2aidl_audio_flags_mask_t_int32_t_mask(capturePolicy));
1783 return statusTFromBinderStatus(aps->setAllowedCapturePolicy(uidAidl, capturePolicyAidl));
Kevin Rocardb99cc752019-03-21 20:52:24 -07001784}
1785
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001786audio_offload_mode_t AudioSystem::getOffloadSupport(const audio_offload_info_t& info) {
Eric Laurent90fe31c2020-11-26 20:06:35 +01001787 ALOGV("%s", __func__);
Andy Hung264fa4c2024-02-21 15:52:12 -08001788 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Andy Hungce3d1102024-09-29 21:47:19 -07001789 if (aps == nullptr) return AUDIO_OFFLOAD_NOT_SUPPORTED;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001790
1791 auto result = [&]() -> ConversionResult<audio_offload_mode_t> {
Mikhail Naganovdbf03642021-08-25 18:15:32 -07001792 AudioOffloadInfo infoAidl = VALUE_OR_RETURN(
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001793 legacy2aidl_audio_offload_info_t_AudioOffloadInfo(info));
1794 media::AudioOffloadMode retAidl;
1795 RETURN_IF_ERROR(
1796 statusTFromBinderStatus(aps->getOffloadSupport(infoAidl, &retAidl)));
1797 return aidl2legacy_AudioOffloadMode_audio_offload_mode_t(retAidl);
1798 }();
1799
1800 return result.value_or(static_cast<audio_offload_mode_t>(0));
Richard Fitzgeraldad3af332013-03-25 16:54:37 +00001801}
1802
Eric Laurent203b1a12014-04-01 10:34:16 -07001803status_t AudioSystem::listAudioPorts(audio_port_role_t role,
1804 audio_port_type_t type,
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001805 unsigned int* num_ports,
1806 struct audio_port_v7* ports,
1807 unsigned int* generation) {
1808 if (num_ports == nullptr || (*num_ports != 0 && ports == nullptr) ||
1809 generation == nullptr) {
1810 return BAD_VALUE;
1811 }
1812
Andy Hung264fa4c2024-02-21 15:52:12 -08001813 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Andy Hungce3d1102024-09-29 21:47:19 -07001814 if (aps == nullptr) return AudioPolicyServiceTraits::getError();
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001815
1816 media::AudioPortRole roleAidl = VALUE_OR_RETURN_STATUS(
1817 legacy2aidl_audio_port_role_t_AudioPortRole(role));
1818 media::AudioPortType typeAidl = VALUE_OR_RETURN_STATUS(
1819 legacy2aidl_audio_port_type_t_AudioPortType(type));
Mikhail Naganov0078ee52021-09-30 23:06:20 +00001820 Int numPortsAidl;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001821 numPortsAidl.value = VALUE_OR_RETURN_STATUS(convertIntegral<int32_t>(*num_ports));
Atneya Nair638a6e42022-12-18 16:45:15 -08001822 std::vector<media::AudioPortFw> portsAidl;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001823 int32_t generationAidl;
1824
1825 RETURN_STATUS_IF_ERROR(statusTFromBinderStatus(
1826 aps->listAudioPorts(roleAidl, typeAidl, &numPortsAidl, &portsAidl, &generationAidl)));
1827 *num_ports = VALUE_OR_RETURN_STATUS(convertIntegral<unsigned int>(numPortsAidl.value));
1828 *generation = VALUE_OR_RETURN_STATUS(convertIntegral<unsigned int>(generationAidl));
1829 RETURN_STATUS_IF_ERROR(convertRange(portsAidl.begin(), portsAidl.end(), ports,
Mikhail Naganov87227252023-01-13 17:38:10 -08001830 aidl2legacy_AudioPortFw_audio_port_v7));
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001831 return OK;
Eric Laurent203b1a12014-04-01 10:34:16 -07001832}
1833
Mikhail Naganov5edc5ed2023-03-23 14:52:15 -07001834status_t AudioSystem::listDeclaredDevicePorts(media::AudioPortRole role,
1835 std::vector<media::AudioPortFw>* result) {
1836 if (result == nullptr) return BAD_VALUE;
Andy Hung264fa4c2024-02-21 15:52:12 -08001837 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Andy Hungce3d1102024-09-29 21:47:19 -07001838 if (aps == nullptr) return AudioPolicyServiceTraits::getError();
Mikhail Naganov5edc5ed2023-03-23 14:52:15 -07001839 RETURN_STATUS_IF_ERROR(statusTFromBinderStatus(aps->listDeclaredDevicePorts(role, result)));
1840 return OK;
1841}
1842
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001843status_t AudioSystem::getAudioPort(struct audio_port_v7* port) {
1844 if (port == nullptr) {
1845 return BAD_VALUE;
1846 }
Andy Hung264fa4c2024-02-21 15:52:12 -08001847 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Andy Hungce3d1102024-09-29 21:47:19 -07001848 if (aps == nullptr) return AudioPolicyServiceTraits::getError();
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001849
Atneya Nair638a6e42022-12-18 16:45:15 -08001850 media::AudioPortFw portAidl;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001851 RETURN_STATUS_IF_ERROR(
Mikhail Naganov17031562022-02-23 23:00:27 +00001852 statusTFromBinderStatus(aps->getAudioPort(port->id, &portAidl)));
Mikhail Naganov87227252023-01-13 17:38:10 -08001853 *port = VALUE_OR_RETURN_STATUS(aidl2legacy_AudioPortFw_audio_port_v7(portAidl));
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001854 return OK;
Eric Laurent203b1a12014-04-01 10:34:16 -07001855}
1856
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001857status_t AudioSystem::createAudioPatch(const struct audio_patch* patch,
1858 audio_patch_handle_t* handle) {
1859 if (patch == nullptr || handle == nullptr) {
1860 return BAD_VALUE;
1861 }
1862
Andy Hung264fa4c2024-02-21 15:52:12 -08001863 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Andy Hungce3d1102024-09-29 21:47:19 -07001864 if (aps == nullptr) return AudioPolicyServiceTraits::getError();
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001865
Atneya Nair3afdbd12022-12-18 16:14:18 -08001866 media::AudioPatchFw patchAidl = VALUE_OR_RETURN_STATUS(
Mikhail Naganov87227252023-01-13 17:38:10 -08001867 legacy2aidl_audio_patch_AudioPatchFw(*patch));
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001868 int32_t handleAidl = VALUE_OR_RETURN_STATUS(legacy2aidl_audio_patch_handle_t_int32_t(*handle));
1869 RETURN_STATUS_IF_ERROR(
1870 statusTFromBinderStatus(aps->createAudioPatch(patchAidl, handleAidl, &handleAidl)));
1871 *handle = VALUE_OR_RETURN_STATUS(aidl2legacy_int32_t_audio_patch_handle_t(handleAidl));
1872 return OK;
Eric Laurent203b1a12014-04-01 10:34:16 -07001873}
1874
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001875status_t AudioSystem::releaseAudioPatch(audio_patch_handle_t handle) {
Andy Hung264fa4c2024-02-21 15:52:12 -08001876 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Andy Hungce3d1102024-09-29 21:47:19 -07001877 if (aps == nullptr) return AudioPolicyServiceTraits::getError();
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001878
1879 int32_t handleAidl = VALUE_OR_RETURN_STATUS(legacy2aidl_audio_patch_handle_t_int32_t(handle));
1880 return statusTFromBinderStatus(aps->releaseAudioPatch(handleAidl));
Eric Laurent203b1a12014-04-01 10:34:16 -07001881}
1882
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001883status_t AudioSystem::listAudioPatches(unsigned int* num_patches,
1884 struct audio_patch* patches,
1885 unsigned int* generation) {
1886 if (num_patches == nullptr || (*num_patches != 0 && patches == nullptr) ||
1887 generation == nullptr) {
1888 return BAD_VALUE;
1889 }
1890
Andy Hung264fa4c2024-02-21 15:52:12 -08001891 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Andy Hungce3d1102024-09-29 21:47:19 -07001892 if (aps == nullptr) return AudioPolicyServiceTraits::getError();
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001893
1894
Mikhail Naganov0078ee52021-09-30 23:06:20 +00001895 Int numPatchesAidl;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001896 numPatchesAidl.value = VALUE_OR_RETURN_STATUS(convertIntegral<int32_t>(*num_patches));
Atneya Nair3afdbd12022-12-18 16:14:18 -08001897 std::vector<media::AudioPatchFw> patchesAidl;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001898 int32_t generationAidl;
1899
1900 RETURN_STATUS_IF_ERROR(statusTFromBinderStatus(
1901 aps->listAudioPatches(&numPatchesAidl, &patchesAidl, &generationAidl)));
1902 *num_patches = VALUE_OR_RETURN_STATUS(convertIntegral<unsigned int>(numPatchesAidl.value));
1903 *generation = VALUE_OR_RETURN_STATUS(convertIntegral<unsigned int>(generationAidl));
1904 RETURN_STATUS_IF_ERROR(convertRange(patchesAidl.begin(), patchesAidl.end(), patches,
Mikhail Naganov87227252023-01-13 17:38:10 -08001905 aidl2legacy_AudioPatchFw_audio_patch));
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001906 return OK;
Eric Laurent203b1a12014-04-01 10:34:16 -07001907}
1908
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001909status_t AudioSystem::setAudioPortConfig(const struct audio_port_config* config) {
1910 if (config == nullptr) {
1911 return BAD_VALUE;
1912 }
1913
Andy Hung264fa4c2024-02-21 15:52:12 -08001914 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Andy Hungce3d1102024-09-29 21:47:19 -07001915 if (aps == nullptr) return AudioPolicyServiceTraits::getError();
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001916
Atneya Nair7a9594f2022-12-18 17:26:26 -08001917 media::AudioPortConfigFw configAidl = VALUE_OR_RETURN_STATUS(
Mikhail Naganov87227252023-01-13 17:38:10 -08001918 legacy2aidl_audio_port_config_AudioPortConfigFw(*config));
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001919 return statusTFromBinderStatus(aps->setAudioPortConfig(configAidl));
Eric Laurent203b1a12014-04-01 10:34:16 -07001920}
1921
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001922status_t AudioSystem::addAudioPortCallback(const sp<AudioPortCallback>& callback) {
Andy Hung264fa4c2024-02-21 15:52:12 -08001923 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Andy Hungce3d1102024-09-29 21:47:19 -07001924 if (aps == nullptr) return AudioPolicyServiceTraits::getError();
1925 const auto apc = getAudioPolicyClient();
Andy Hung88e71732024-02-21 16:44:27 -08001926 if (apc == nullptr) return NO_INIT;
Eric Laurentb28753e2015-04-01 13:06:28 -07001927
Andy Hung88e71732024-02-21 16:44:27 -08001928 std::lock_guard _l(gApsCallbackMutex);
1929 const int ret = apc->addAudioPortCallback(callback);
Eric Laurente8726fe2015-06-26 09:39:24 -07001930 if (ret == 1) {
1931 aps->setAudioPortCallbacksEnabled(true);
1932 }
1933 return (ret < 0) ? INVALID_OPERATION : NO_ERROR;
Eric Laurentb52c1522014-05-20 11:27:36 -07001934}
1935
Jean-Michel Trivif613d422015-04-23 18:41:29 -07001936/*static*/
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001937status_t AudioSystem::removeAudioPortCallback(const sp<AudioPortCallback>& callback) {
Andy Hung264fa4c2024-02-21 15:52:12 -08001938 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Andy Hungce3d1102024-09-29 21:47:19 -07001939 if (aps == nullptr) return AudioPolicyServiceTraits::getError();
1940 const auto apc = AudioSystem::getAudioPolicyClient();
Andy Hung88e71732024-02-21 16:44:27 -08001941 if (apc == nullptr) return NO_INIT;
Eric Laurentb28753e2015-04-01 13:06:28 -07001942
Andy Hung88e71732024-02-21 16:44:27 -08001943 std::lock_guard _l(gApsCallbackMutex);
1944 const int ret = apc->removeAudioPortCallback(callback);
Eric Laurente8726fe2015-06-26 09:39:24 -07001945 if (ret == 0) {
1946 aps->setAudioPortCallbacksEnabled(false);
1947 }
1948 return (ret < 0) ? INVALID_OPERATION : NO_ERROR;
Eric Laurent296fb132015-05-01 11:38:42 -07001949}
1950
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001951status_t AudioSystem::addAudioVolumeGroupCallback(const sp<AudioVolumeGroupCallback>& callback) {
Andy Hung264fa4c2024-02-21 15:52:12 -08001952 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Andy Hungce3d1102024-09-29 21:47:19 -07001953 if (aps == nullptr) return AudioPolicyServiceTraits::getError();
1954 const auto apc = AudioSystem::getAudioPolicyClient();
Andy Hung88e71732024-02-21 16:44:27 -08001955 if (apc == nullptr) return NO_INIT;
François Gaffiecfe17322018-11-07 13:41:29 +01001956
Andy Hung88e71732024-02-21 16:44:27 -08001957 std::lock_guard _l(gApsCallbackMutex);
1958 const int ret = apc->addAudioVolumeGroupCallback(callback);
François Gaffiecfe17322018-11-07 13:41:29 +01001959 if (ret == 1) {
1960 aps->setAudioVolumeGroupCallbacksEnabled(true);
1961 }
1962 return (ret < 0) ? INVALID_OPERATION : NO_ERROR;
1963}
1964
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001965status_t AudioSystem::removeAudioVolumeGroupCallback(const sp<AudioVolumeGroupCallback>& callback) {
Andy Hung264fa4c2024-02-21 15:52:12 -08001966 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Andy Hungce3d1102024-09-29 21:47:19 -07001967 if (aps == nullptr) return AudioPolicyServiceTraits::getError();
1968 const auto apc = AudioSystem::getAudioPolicyClient();
Andy Hung88e71732024-02-21 16:44:27 -08001969 if (apc == nullptr) return NO_INIT;
François Gaffiecfe17322018-11-07 13:41:29 +01001970
Andy Hung88e71732024-02-21 16:44:27 -08001971 std::lock_guard _l(gApsCallbackMutex);
1972 const int ret = apc->removeAudioVolumeGroupCallback(callback);
François Gaffiecfe17322018-11-07 13:41:29 +01001973 if (ret == 0) {
1974 aps->setAudioVolumeGroupCallbacksEnabled(false);
1975 }
1976 return (ret < 0) ? INVALID_OPERATION : NO_ERROR;
1977}
1978
Eric Laurent296fb132015-05-01 11:38:42 -07001979status_t AudioSystem::addAudioDeviceCallback(
Eric Laurent09f1ed22019-04-24 17:45:17 -07001980 const wp<AudioDeviceCallback>& callback, audio_io_handle_t audioIo,
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001981 audio_port_handle_t portId) {
Eric Laurent296fb132015-05-01 11:38:42 -07001982 const sp<AudioFlingerClient> afc = getAudioFlingerClient();
1983 if (afc == 0) {
1984 return NO_INIT;
1985 }
Eric Laurent09f1ed22019-04-24 17:45:17 -07001986 status_t status = afc->addAudioDeviceCallback(callback, audioIo, portId);
Eric Laurent7c1ec5f2015-07-09 14:52:47 -07001987 if (status == NO_ERROR) {
Andy Hung264fa4c2024-02-21 15:52:12 -08001988 const sp<IAudioFlinger> af = get_audio_flinger();
Eric Laurent7c1ec5f2015-07-09 14:52:47 -07001989 if (af != 0) {
1990 af->registerClient(afc);
1991 }
1992 }
1993 return status;
Eric Laurent296fb132015-05-01 11:38:42 -07001994}
1995
1996status_t AudioSystem::removeAudioDeviceCallback(
Eric Laurent09f1ed22019-04-24 17:45:17 -07001997 const wp<AudioDeviceCallback>& callback, audio_io_handle_t audioIo,
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001998 audio_port_handle_t portId) {
Eric Laurent296fb132015-05-01 11:38:42 -07001999 const sp<AudioFlingerClient> afc = getAudioFlingerClient();
2000 if (afc == 0) {
2001 return NO_INIT;
2002 }
Eric Laurent09f1ed22019-04-24 17:45:17 -07002003 return afc->removeAudioDeviceCallback(callback, audioIo, portId);
Eric Laurent296fb132015-05-01 11:38:42 -07002004}
2005
Eric Laurent076e7c72022-05-03 18:12:28 +02002006status_t AudioSystem::addSupportedLatencyModesCallback(
2007 const sp<SupportedLatencyModesCallback>& callback) {
2008 const sp<AudioFlingerClient> afc = getAudioFlingerClient();
2009 if (afc == 0) {
2010 return NO_INIT;
2011 }
2012 return afc->addSupportedLatencyModesCallback(callback);
2013}
2014
2015status_t AudioSystem::removeSupportedLatencyModesCallback(
2016 const sp<SupportedLatencyModesCallback>& callback) {
2017 const sp<AudioFlingerClient> afc = getAudioFlingerClient();
2018 if (afc == 0) {
2019 return NO_INIT;
2020 }
2021 return afc->removeSupportedLatencyModesCallback(callback);
2022}
2023
Robert Wub7f8edc2024-11-04 19:54:38 +00002024status_t AudioSystem::getDeviceIdsForIo(audio_io_handle_t audioIo, DeviceIdVector& deviceIds) {
Andy Hung264fa4c2024-02-21 15:52:12 -08002025 const sp<IAudioFlinger> af = get_audio_flinger();
Andy Hungce3d1102024-09-29 21:47:19 -07002026 if (af == nullptr) return AudioFlingerServiceTraits::getError();
Eric Laurent296fb132015-05-01 11:38:42 -07002027 const sp<AudioIoDescriptor> desc = getIoDescriptor(audioIo);
2028 if (desc == 0) {
Robert Wub7f8edc2024-11-04 19:54:38 +00002029 deviceIds.clear();
2030 } else {
2031 deviceIds = desc->getDeviceIds();
Eric Laurent296fb132015-05-01 11:38:42 -07002032 }
Robert Wub7f8edc2024-11-04 19:54:38 +00002033 return OK;
Eric Laurentb28753e2015-04-01 13:06:28 -07002034}
2035
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002036status_t AudioSystem::acquireSoundTriggerSession(audio_session_t* session,
2037 audio_io_handle_t* ioHandle,
2038 audio_devices_t* device) {
2039 if (session == nullptr || ioHandle == nullptr || device == nullptr) {
2040 return BAD_VALUE;
2041 }
Andy Hung264fa4c2024-02-21 15:52:12 -08002042 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Andy Hungce3d1102024-09-29 21:47:19 -07002043 if (aps == nullptr) return AudioPolicyServiceTraits::getError();
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002044
2045 media::SoundTriggerSession retAidl;
2046 RETURN_STATUS_IF_ERROR(
2047 statusTFromBinderStatus(aps->acquireSoundTriggerSession(&retAidl)));
2048 *session = VALUE_OR_RETURN_STATUS(aidl2legacy_int32_t_audio_session_t(retAidl.session));
2049 *ioHandle = VALUE_OR_RETURN_STATUS(aidl2legacy_int32_t_audio_io_handle_t(retAidl.ioHandle));
Mikhail Naganov21a32ec2021-07-08 14:40:12 -07002050 *device = VALUE_OR_RETURN_STATUS(
2051 aidl2legacy_AudioDeviceDescription_audio_devices_t(retAidl.device));
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002052 return OK;
Eric Laurentdf3dc7e2014-07-27 18:39:40 -07002053}
2054
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002055status_t AudioSystem::releaseSoundTriggerSession(audio_session_t session) {
Andy Hung264fa4c2024-02-21 15:52:12 -08002056 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Andy Hungce3d1102024-09-29 21:47:19 -07002057 if (aps == nullptr) return AudioPolicyServiceTraits::getError();
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002058
2059 int32_t sessionAidl = VALUE_OR_RETURN_STATUS(legacy2aidl_audio_session_t_int32_t(session));
2060 return statusTFromBinderStatus(aps->releaseSoundTriggerSession(sessionAidl));
Eric Laurentdf3dc7e2014-07-27 18:39:40 -07002061}
Eric Laurentbb6c9a02014-09-25 14:11:47 -07002062
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002063audio_mode_t AudioSystem::getPhoneState() {
Andy Hung264fa4c2024-02-21 15:52:12 -08002064 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Andy Hungce3d1102024-09-29 21:47:19 -07002065 if (aps == nullptr) return AUDIO_MODE_INVALID;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002066
2067 auto result = [&]() -> ConversionResult<audio_mode_t> {
Mikhail Naganovddceecc2021-09-03 13:58:56 -07002068 media::audio::common::AudioMode retAidl;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002069 RETURN_IF_ERROR(statusTFromBinderStatus(aps->getPhoneState(&retAidl)));
2070 return aidl2legacy_AudioMode_audio_mode_t(retAidl);
2071 }();
2072
2073 return result.value_or(AUDIO_MODE_INVALID);
Eric Laurentbb6c9a02014-09-25 14:11:47 -07002074}
2075
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002076status_t AudioSystem::registerPolicyMixes(const Vector<AudioMix>& mixes, bool registration) {
Andy Hung264fa4c2024-02-21 15:52:12 -08002077 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Andy Hungce3d1102024-09-29 21:47:19 -07002078 if (aps == nullptr) return AudioPolicyServiceTraits::getError();
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002079
2080 size_t mixesSize = std::min(mixes.size(), size_t{MAX_MIXES_PER_POLICY});
2081 std::vector<media::AudioMix> mixesAidl;
2082 RETURN_STATUS_IF_ERROR(
2083 convertRange(mixes.begin(), mixes.begin() + mixesSize, std::back_inserter(mixesAidl),
2084 legacy2aidl_AudioMix));
2085 return statusTFromBinderStatus(aps->registerPolicyMixes(mixesAidl, registration));
Eric Laurentbaac1832014-12-01 17:52:59 -08002086}
Eric Laurentbb6c9a02014-09-25 14:11:47 -07002087
Marvin Raminbdefaf02023-11-01 09:10:32 +01002088status_t AudioSystem::getRegisteredPolicyMixes(std::vector<AudioMix>& mixes) {
2089 if (!audio_flags::audio_mix_test_api()) {
2090 return INVALID_OPERATION;
2091 }
2092
2093 const sp<IAudioPolicyService> aps = AudioSystem::get_audio_policy_service();
Andy Hungce3d1102024-09-29 21:47:19 -07002094 if (aps == nullptr) return AudioPolicyServiceTraits::getError();
Marvin Raminbdefaf02023-11-01 09:10:32 +01002095
2096 std::vector<::android::media::AudioMix> aidlMixes;
2097 Status status = aps->getRegisteredPolicyMixes(&aidlMixes);
2098
2099 for (const auto& aidlMix : aidlMixes) {
2100 AudioMix mix = VALUE_OR_RETURN_STATUS(aidl2legacy_AudioMix(aidlMix));
2101 mixes.push_back(mix);
2102 }
2103
2104 return statusTFromBinderStatus(status);
2105}
2106
Jan Sebechlebsky0af8e872023-08-11 14:45:08 +02002107status_t AudioSystem::updatePolicyMixes(
2108 const std::vector<std::pair<AudioMix, std::vector<AudioMixMatchCriterion>>>&
2109 mixesWithUpdates) {
Andy Hung264fa4c2024-02-21 15:52:12 -08002110 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Andy Hungce3d1102024-09-29 21:47:19 -07002111 if (aps == nullptr) return AudioPolicyServiceTraits::getError();
Jan Sebechlebsky0af8e872023-08-11 14:45:08 +02002112
2113 std::vector<media::AudioMixUpdate> updatesAidl;
2114 updatesAidl.reserve(mixesWithUpdates.size());
2115
2116 for (const auto& update : mixesWithUpdates) {
2117 media::AudioMixUpdate updateAidl;
2118 updateAidl.audioMix = VALUE_OR_RETURN_STATUS(legacy2aidl_AudioMix(update.first));
2119 RETURN_STATUS_IF_ERROR(convertRange(update.second.begin(), update.second.end(),
2120 std::back_inserter(updateAidl.newCriteria),
2121 legacy2aidl_AudioMixMatchCriterion));
2122 updatesAidl.emplace_back(updateAidl);
2123 }
2124
2125 return statusTFromBinderStatus(aps->updatePolicyMixes(updatesAidl));
2126}
2127
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002128status_t AudioSystem::setUidDeviceAffinities(uid_t uid, const AudioDeviceTypeAddrVector& devices) {
Andy Hung264fa4c2024-02-21 15:52:12 -08002129 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Andy Hungce3d1102024-09-29 21:47:19 -07002130 if (aps == nullptr) return AudioPolicyServiceTraits::getError();
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002131
2132 int32_t uidAidl = VALUE_OR_RETURN_STATUS(legacy2aidl_uid_t_int32_t(uid));
Mikhail Naganovf4a75362021-09-16 00:02:54 +00002133 std::vector<AudioDevice> devicesAidl = VALUE_OR_RETURN_STATUS(
2134 convertContainer<std::vector<AudioDevice>>(devices,
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002135 legacy2aidl_AudioDeviceTypeAddress));
2136 return statusTFromBinderStatus(aps->setUidDeviceAffinities(uidAidl, devicesAidl));
Jean-Michel Trivibda70da2018-12-19 07:30:15 -08002137}
2138
2139status_t AudioSystem::removeUidDeviceAffinities(uid_t uid) {
Andy Hung264fa4c2024-02-21 15:52:12 -08002140 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Andy Hungce3d1102024-09-29 21:47:19 -07002141 if (aps == nullptr) return AudioPolicyServiceTraits::getError();
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002142
2143 int32_t uidAidl = VALUE_OR_RETURN_STATUS(legacy2aidl_uid_t_int32_t(uid));
2144 return statusTFromBinderStatus(aps->removeUidDeviceAffinities(uidAidl));
Jean-Michel Trivibda70da2018-12-19 07:30:15 -08002145}
2146
Oscar Azucena90e77632019-11-27 17:12:28 -08002147status_t AudioSystem::setUserIdDeviceAffinities(int userId,
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002148 const AudioDeviceTypeAddrVector& devices) {
Andy Hung264fa4c2024-02-21 15:52:12 -08002149 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Andy Hungce3d1102024-09-29 21:47:19 -07002150 if (aps == nullptr) return AudioPolicyServiceTraits::getError();
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002151
2152 int32_t userIdAidl = VALUE_OR_RETURN_STATUS(convertReinterpret<int32_t>(userId));
Mikhail Naganovf4a75362021-09-16 00:02:54 +00002153 std::vector<AudioDevice> devicesAidl = VALUE_OR_RETURN_STATUS(
2154 convertContainer<std::vector<AudioDevice>>(devices,
2155 legacy2aidl_AudioDeviceTypeAddress));
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002156 return statusTFromBinderStatus(
2157 aps->setUserIdDeviceAffinities(userIdAidl, devicesAidl));
Oscar Azucena90e77632019-11-27 17:12:28 -08002158}
2159
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002160status_t AudioSystem::removeUserIdDeviceAffinities(int userId) {
Andy Hung264fa4c2024-02-21 15:52:12 -08002161 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Andy Hungce3d1102024-09-29 21:47:19 -07002162 if (aps == nullptr) return AudioPolicyServiceTraits::getError();
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002163 int32_t userIdAidl = VALUE_OR_RETURN_STATUS(convertReinterpret<int32_t>(userId));
2164 return statusTFromBinderStatus(aps->removeUserIdDeviceAffinities(userIdAidl));
Oscar Azucena90e77632019-11-27 17:12:28 -08002165}
2166
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002167status_t AudioSystem::startAudioSource(const struct audio_port_config* source,
2168 const audio_attributes_t* attributes,
2169 audio_port_handle_t* portId) {
2170 if (source == nullptr || attributes == nullptr || portId == nullptr) {
2171 return BAD_VALUE;
2172 }
Andy Hung264fa4c2024-02-21 15:52:12 -08002173 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Andy Hungce3d1102024-09-29 21:47:19 -07002174 if (aps == nullptr) return AudioPolicyServiceTraits::getError();
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002175
Atneya Nair7a9594f2022-12-18 17:26:26 -08002176 media::AudioPortConfigFw sourceAidl = VALUE_OR_RETURN_STATUS(
Mikhail Naganov87227252023-01-13 17:38:10 -08002177 legacy2aidl_audio_port_config_AudioPortConfigFw(*source));
Mikhail Naganov1c400902023-05-17 11:48:43 -07002178 media::audio::common::AudioAttributes attributesAidl = VALUE_OR_RETURN_STATUS(
2179 legacy2aidl_audio_attributes_t_AudioAttributes(*attributes));
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002180 int32_t portIdAidl;
2181 RETURN_STATUS_IF_ERROR(statusTFromBinderStatus(
2182 aps->startAudioSource(sourceAidl, attributesAidl, &portIdAidl)));
2183 *portId = VALUE_OR_RETURN_STATUS(aidl2legacy_int32_t_audio_port_handle_t(portIdAidl));
2184 return OK;
Eric Laurent554a2772015-04-10 11:29:24 -07002185}
2186
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002187status_t AudioSystem::stopAudioSource(audio_port_handle_t portId) {
Andy Hung264fa4c2024-02-21 15:52:12 -08002188 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Andy Hungce3d1102024-09-29 21:47:19 -07002189 if (aps == nullptr) return AudioPolicyServiceTraits::getError();
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002190
2191 int32_t portIdAidl = VALUE_OR_RETURN_STATUS(legacy2aidl_audio_port_handle_t_int32_t(portId));
2192 return statusTFromBinderStatus(aps->stopAudioSource(portIdAidl));
Eric Laurent554a2772015-04-10 11:29:24 -07002193}
2194
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002195status_t AudioSystem::setMasterMono(bool mono) {
Andy Hung264fa4c2024-02-21 15:52:12 -08002196 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Andy Hungce3d1102024-09-29 21:47:19 -07002197 if (aps == nullptr) return AudioPolicyServiceTraits::getError();
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002198 return statusTFromBinderStatus(aps->setMasterMono(mono));
Andy Hung2ddee192015-12-18 17:34:44 -08002199}
2200
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002201status_t AudioSystem::getMasterMono(bool* mono) {
2202 if (mono == nullptr) {
2203 return BAD_VALUE;
2204 }
Andy Hung264fa4c2024-02-21 15:52:12 -08002205 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Andy Hungce3d1102024-09-29 21:47:19 -07002206 if (aps == nullptr) return AudioPolicyServiceTraits::getError();
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002207 return statusTFromBinderStatus(aps->getMasterMono(mono));
Andy Hung2ddee192015-12-18 17:34:44 -08002208}
2209
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002210status_t AudioSystem::setMasterBalance(float balance) {
Andy Hung264fa4c2024-02-21 15:52:12 -08002211 const sp<IAudioFlinger> af = get_audio_flinger();
Andy Hungce3d1102024-09-29 21:47:19 -07002212 if (af == nullptr) return AudioFlingerServiceTraits::getError();
Richard Folke Tullberg3fae0372017-01-13 09:04:25 +01002213 return af->setMasterBalance(balance);
2214}
2215
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002216status_t AudioSystem::getMasterBalance(float* balance) {
Andy Hung264fa4c2024-02-21 15:52:12 -08002217 const sp<IAudioFlinger> af = get_audio_flinger();
Andy Hungce3d1102024-09-29 21:47:19 -07002218 if (af == nullptr) return AudioFlingerServiceTraits::getError();
Richard Folke Tullberg3fae0372017-01-13 09:04:25 +01002219 return af->getMasterBalance(balance);
2220}
2221
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002222float
2223AudioSystem::getStreamVolumeDB(audio_stream_type_t stream, int index, audio_devices_t device) {
Andy Hung264fa4c2024-02-21 15:52:12 -08002224 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Andy Hungce3d1102024-09-29 21:47:19 -07002225 if (aps == nullptr) return NAN;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002226
2227 auto result = [&]() -> ConversionResult<float> {
Mikhail Naganovdbf03642021-08-25 18:15:32 -07002228 AudioStreamType streamAidl = VALUE_OR_RETURN(
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002229 legacy2aidl_audio_stream_type_t_AudioStreamType(stream));
2230 int32_t indexAidl = VALUE_OR_RETURN(convertIntegral<int32_t>(index));
Mikhail Naganovf4a75362021-09-16 00:02:54 +00002231 AudioDeviceDescription deviceAidl = VALUE_OR_RETURN(
Mikhail Naganov21a32ec2021-07-08 14:40:12 -07002232 legacy2aidl_audio_devices_t_AudioDeviceDescription(device));
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002233 float retAidl;
2234 RETURN_IF_ERROR(statusTFromBinderStatus(
2235 aps->getStreamVolumeDB(streamAidl, indexAidl, deviceAidl, &retAidl)));
2236 return retAidl;
2237 }();
2238 return result.value_or(NAN);
Eric Laurentac9cef52017-06-09 15:46:26 -07002239}
2240
Mikhail Naganovd5d9de72023-02-13 11:45:03 -08002241status_t AudioSystem::getMicrophones(std::vector<media::MicrophoneInfoFw>* microphones) {
Andy Hung264fa4c2024-02-21 15:52:12 -08002242 const sp<IAudioFlinger> af = get_audio_flinger();
Andy Hungce3d1102024-09-29 21:47:19 -07002243 if (af == nullptr) return AudioFlingerServiceTraits::getError();
jiabin46a76fa2018-01-05 10:18:21 -08002244 return af->getMicrophones(microphones);
2245}
2246
Eric Laurent42896a02019-09-27 15:40:33 -07002247status_t AudioSystem::setAudioHalPids(const std::vector<pid_t>& pids) {
Andy Hung264fa4c2024-02-21 15:52:12 -08002248 const sp<IAudioFlinger> af = get_audio_flinger();
Andy Hungce3d1102024-09-29 21:47:19 -07002249 if (af == nullptr) return AudioFlingerServiceTraits::getError();
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002250 return af->setAudioHalPids(pids);
Eric Laurent42896a02019-09-27 15:40:33 -07002251}
2252
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002253status_t AudioSystem::getSurroundFormats(unsigned int* numSurroundFormats,
2254 audio_format_t* surroundFormats,
Kriti Dang6537def2021-03-02 13:46:59 +01002255 bool* surroundFormatsEnabled) {
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002256 if (numSurroundFormats == nullptr || (*numSurroundFormats != 0 &&
2257 (surroundFormats == nullptr ||
2258 surroundFormatsEnabled == nullptr))) {
2259 return BAD_VALUE;
2260 }
2261
Andy Hung264fa4c2024-02-21 15:52:12 -08002262 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Andy Hungce3d1102024-09-29 21:47:19 -07002263 if (aps == nullptr) return AudioPolicyServiceTraits::getError();
Mikhail Naganov0078ee52021-09-30 23:06:20 +00002264 Int numSurroundFormatsAidl;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002265 numSurroundFormatsAidl.value =
2266 VALUE_OR_RETURN_STATUS(convertIntegral<int32_t>(*numSurroundFormats));
Mikhail Naganov57bd06f2021-08-10 16:41:54 -07002267 std::vector<AudioFormatDescription> surroundFormatsAidl;
Kriti Dang877b27e2021-02-02 12:10:40 +01002268 std::vector<bool> surroundFormatsEnabledAidl;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002269 RETURN_STATUS_IF_ERROR(statusTFromBinderStatus(
Kriti Dang6537def2021-03-02 13:46:59 +01002270 aps->getSurroundFormats(&numSurroundFormatsAidl, &surroundFormatsAidl,
2271 &surroundFormatsEnabledAidl)));
Kriti Dang877b27e2021-02-02 12:10:40 +01002272
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002273 *numSurroundFormats = VALUE_OR_RETURN_STATUS(
2274 convertIntegral<unsigned int>(numSurroundFormatsAidl.value));
2275 RETURN_STATUS_IF_ERROR(
2276 convertRange(surroundFormatsAidl.begin(), surroundFormatsAidl.end(), surroundFormats,
Mikhail Naganovb60bd1b2021-07-15 17:31:43 -07002277 aidl2legacy_AudioFormatDescription_audio_format_t));
Kriti Dang877b27e2021-02-02 12:10:40 +01002278 std::copy(surroundFormatsEnabledAidl.begin(), surroundFormatsEnabledAidl.end(),
2279 surroundFormatsEnabled);
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002280 return OK;
jiabin81772902018-04-02 17:52:27 -07002281}
2282
Kriti Dang6537def2021-03-02 13:46:59 +01002283status_t AudioSystem::getReportedSurroundFormats(unsigned int* numSurroundFormats,
2284 audio_format_t* surroundFormats) {
2285 if (numSurroundFormats == nullptr || (*numSurroundFormats != 0 && surroundFormats == nullptr)) {
2286 return BAD_VALUE;
2287 }
2288
Andy Hung264fa4c2024-02-21 15:52:12 -08002289 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Andy Hungce3d1102024-09-29 21:47:19 -07002290 if (aps == nullptr) return AudioPolicyServiceTraits::getError();
Mikhail Naganov0078ee52021-09-30 23:06:20 +00002291 Int numSurroundFormatsAidl;
Kriti Dang6537def2021-03-02 13:46:59 +01002292 numSurroundFormatsAidl.value =
2293 VALUE_OR_RETURN_STATUS(convertIntegral<int32_t>(*numSurroundFormats));
Mikhail Naganov57bd06f2021-08-10 16:41:54 -07002294 std::vector<AudioFormatDescription> surroundFormatsAidl;
Kriti Dang6537def2021-03-02 13:46:59 +01002295 RETURN_STATUS_IF_ERROR(statusTFromBinderStatus(
2296 aps->getReportedSurroundFormats(&numSurroundFormatsAidl, &surroundFormatsAidl)));
2297
2298 *numSurroundFormats = VALUE_OR_RETURN_STATUS(
2299 convertIntegral<unsigned int>(numSurroundFormatsAidl.value));
2300 RETURN_STATUS_IF_ERROR(
2301 convertRange(surroundFormatsAidl.begin(), surroundFormatsAidl.end(), surroundFormats,
Mikhail Naganovb60bd1b2021-07-15 17:31:43 -07002302 aidl2legacy_AudioFormatDescription_audio_format_t));
Kriti Dang6537def2021-03-02 13:46:59 +01002303 return OK;
2304}
2305
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002306status_t AudioSystem::setSurroundFormatEnabled(audio_format_t audioFormat, bool enabled) {
Andy Hung264fa4c2024-02-21 15:52:12 -08002307 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Andy Hungce3d1102024-09-29 21:47:19 -07002308 if (aps == nullptr) return AudioPolicyServiceTraits::getError();
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002309
Mikhail Naganov57bd06f2021-08-10 16:41:54 -07002310 AudioFormatDescription audioFormatAidl = VALUE_OR_RETURN_STATUS(
Mikhail Naganovb60bd1b2021-07-15 17:31:43 -07002311 legacy2aidl_audio_format_t_AudioFormatDescription(audioFormat));
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002312 return statusTFromBinderStatus(
2313 aps->setSurroundFormatEnabled(audioFormatAidl, enabled));
jiabin81772902018-04-02 17:52:27 -07002314}
2315
Oscar Azucena829d90d2022-01-28 17:17:56 -08002316status_t AudioSystem::setAssistantServicesUids(const std::vector<uid_t>& uids) {
Andy Hung264fa4c2024-02-21 15:52:12 -08002317 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Andy Hungce3d1102024-09-29 21:47:19 -07002318 if (aps == nullptr) return AudioPolicyServiceTraits::getError();
Eric Laurentb78763e2018-10-17 10:08:02 -07002319
Oscar Azucena829d90d2022-01-28 17:17:56 -08002320 std::vector<int32_t> uidsAidl = VALUE_OR_RETURN_STATUS(
2321 convertContainer<std::vector<int32_t>>(uids, legacy2aidl_uid_t_int32_t));
2322 return statusTFromBinderStatus(aps->setAssistantServicesUids(uidsAidl));
Ahaan Ugalef51ce002021-08-04 16:34:20 -07002323}
2324
Oscar Azucenac2cdda32022-01-31 19:10:39 -08002325status_t AudioSystem::setActiveAssistantServicesUids(const std::vector<uid_t>& activeUids) {
Andy Hung264fa4c2024-02-21 15:52:12 -08002326 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Andy Hungce3d1102024-09-29 21:47:19 -07002327 if (aps == nullptr) return AudioPolicyServiceTraits::getError();
Oscar Azucenac2cdda32022-01-31 19:10:39 -08002328
2329 std::vector<int32_t> activeUidsAidl = VALUE_OR_RETURN_STATUS(
2330 convertContainer<std::vector<int32_t>>(activeUids, legacy2aidl_uid_t_int32_t));
2331 return statusTFromBinderStatus(aps->setActiveAssistantServicesUids(activeUidsAidl));
2332}
2333
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002334status_t AudioSystem::setA11yServicesUids(const std::vector<uid_t>& uids) {
Andy Hung264fa4c2024-02-21 15:52:12 -08002335 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Andy Hungce3d1102024-09-29 21:47:19 -07002336 if (aps == nullptr) return AudioPolicyServiceTraits::getError();
Eric Laurentb78763e2018-10-17 10:08:02 -07002337
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002338 std::vector<int32_t> uidsAidl = VALUE_OR_RETURN_STATUS(
2339 convertContainer<std::vector<int32_t>>(uids, legacy2aidl_uid_t_int32_t));
2340 return statusTFromBinderStatus(aps->setA11yServicesUids(uidsAidl));
Eric Laurentb78763e2018-10-17 10:08:02 -07002341}
2342
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002343status_t AudioSystem::setCurrentImeUid(uid_t uid) {
Andy Hung264fa4c2024-02-21 15:52:12 -08002344 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Andy Hungce3d1102024-09-29 21:47:19 -07002345 if (aps == nullptr) return AudioPolicyServiceTraits::getError();
Kohsuke Yatoha623a132020-03-24 20:10:26 -07002346
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002347 int32_t uidAidl = VALUE_OR_RETURN_STATUS(legacy2aidl_uid_t_int32_t(uid));
2348 return statusTFromBinderStatus(aps->setCurrentImeUid(uidAidl));
Kohsuke Yatoha623a132020-03-24 20:10:26 -07002349}
2350
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002351bool AudioSystem::isHapticPlaybackSupported() {
Andy Hung264fa4c2024-02-21 15:52:12 -08002352 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Andy Hungce3d1102024-09-29 21:47:19 -07002353 if (aps == nullptr) return false;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002354
2355 auto result = [&]() -> ConversionResult<bool> {
2356 bool retVal;
2357 RETURN_IF_ERROR(
2358 statusTFromBinderStatus(aps->isHapticPlaybackSupported(&retVal)));
2359 return retVal;
2360 }();
2361 return result.value_or(false);
jiabin6012f912018-11-02 17:06:30 -07002362}
2363
Carter Hsu325a8eb2022-01-19 19:56:51 +08002364bool AudioSystem::isUltrasoundSupported() {
Andy Hung264fa4c2024-02-21 15:52:12 -08002365 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Andy Hungce3d1102024-09-29 21:47:19 -07002366 if (aps == nullptr) return false;
Carter Hsu325a8eb2022-01-19 19:56:51 +08002367
2368 auto result = [&]() -> ConversionResult<bool> {
2369 bool retVal;
2370 RETURN_IF_ERROR(
2371 statusTFromBinderStatus(aps->isUltrasoundSupported(&retVal)));
2372 return retVal;
2373 }();
2374 return result.value_or(false);
2375}
2376
Pattydd807582021-11-04 21:01:03 +08002377status_t AudioSystem::getHwOffloadFormatsSupportedForBluetoothMedia(
2378 audio_devices_t device, std::vector<audio_format_t>* formats) {
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002379 if (formats == nullptr) {
2380 return BAD_VALUE;
2381 }
2382
Andy Hung264fa4c2024-02-21 15:52:12 -08002383 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Andy Hungce3d1102024-09-29 21:47:19 -07002384 if (aps == nullptr) return AudioPolicyServiceTraits::getError();
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002385
Mikhail Naganov57bd06f2021-08-10 16:41:54 -07002386 std::vector<AudioFormatDescription> formatsAidl;
Pattydd807582021-11-04 21:01:03 +08002387 AudioDeviceDescription deviceAidl = VALUE_OR_RETURN_STATUS(
2388 legacy2aidl_audio_devices_t_AudioDeviceDescription(device));
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002389 RETURN_STATUS_IF_ERROR(statusTFromBinderStatus(
Pattydd807582021-11-04 21:01:03 +08002390 aps->getHwOffloadFormatsSupportedForBluetoothMedia(deviceAidl, &formatsAidl)));
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002391 *formats = VALUE_OR_RETURN_STATUS(
Mikhail Naganovb60bd1b2021-07-15 17:31:43 -07002392 convertContainer<std::vector<audio_format_t>>(
2393 formatsAidl,
2394 aidl2legacy_AudioFormatDescription_audio_format_t));
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002395 return OK;
François Gaffied0ba9ed2018-11-05 11:50:42 +01002396}
2397
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002398status_t AudioSystem::listAudioProductStrategies(AudioProductStrategyVector& strategies) {
Andy Hung264fa4c2024-02-21 15:52:12 -08002399 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Andy Hungce3d1102024-09-29 21:47:19 -07002400 if (aps == nullptr) return AudioPolicyServiceTraits::getError();
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002401
2402 std::vector<media::AudioProductStrategy> strategiesAidl;
2403 RETURN_STATUS_IF_ERROR(statusTFromBinderStatus(
2404 aps->listAudioProductStrategies(&strategiesAidl)));
2405 strategies = VALUE_OR_RETURN_STATUS(
2406 convertContainer<AudioProductStrategyVector>(strategiesAidl,
2407 aidl2legacy_AudioProductStrategy));
2408 return OK;
François Gaffied0ba9ed2018-11-05 11:50:42 +01002409}
2410
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002411audio_attributes_t AudioSystem::streamTypeToAttributes(audio_stream_type_t stream) {
François Gaffied0ba9ed2018-11-05 11:50:42 +01002412 AudioProductStrategyVector strategies;
2413 listAudioProductStrategies(strategies);
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002414 for (const auto& strategy : strategies) {
François Gaffie1e2b56f2022-04-01 14:34:29 +02002415 auto attrVect = strategy.getVolumeGroupAttributes();
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002416 auto iter = std::find_if(begin(attrVect), end(attrVect), [&stream](const auto& attributes) {
2417 return attributes.getStreamType() == stream;
2418 });
François Gaffied0ba9ed2018-11-05 11:50:42 +01002419 if (iter != end(attrVect)) {
2420 return iter->getAttributes();
2421 }
2422 }
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002423 ALOGE("invalid stream type %s when converting to attributes", toString(stream).c_str());
François Gaffied0ba9ed2018-11-05 11:50:42 +01002424 return AUDIO_ATTRIBUTES_INITIALIZER;
2425}
2426
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002427audio_stream_type_t AudioSystem::attributesToStreamType(const audio_attributes_t& attr) {
François Gaffie4b2018b2018-11-07 11:18:59 +01002428 product_strategy_t psId;
François Gaffie1e2b56f2022-04-01 14:34:29 +02002429 status_t ret = AudioSystem::getProductStrategyFromAudioAttributes(attr, psId);
François Gaffie4b2018b2018-11-07 11:18:59 +01002430 if (ret != NO_ERROR) {
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002431 ALOGE("no strategy found for attributes %s", toString(attr).c_str());
François Gaffie4b2018b2018-11-07 11:18:59 +01002432 return AUDIO_STREAM_MUSIC;
2433 }
François Gaffied0ba9ed2018-11-05 11:50:42 +01002434 AudioProductStrategyVector strategies;
2435 listAudioProductStrategies(strategies);
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002436 for (const auto& strategy : strategies) {
François Gaffie4b2018b2018-11-07 11:18:59 +01002437 if (strategy.getId() == psId) {
François Gaffie1e2b56f2022-04-01 14:34:29 +02002438 auto attrVect = strategy.getVolumeGroupAttributes();
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002439 auto iter = std::find_if(begin(attrVect), end(attrVect), [&attr](const auto& refAttr) {
François Gaffie8d7fd5a2023-01-17 17:28:09 +01002440 return refAttr.matchesScore(attr) > 0;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002441 });
François Gaffied0ba9ed2018-11-05 11:50:42 +01002442 if (iter != end(attrVect)) {
2443 return iter->getStreamType();
2444 }
2445 }
2446 }
Jean-Michel Trivied678652019-12-19 13:39:30 -08002447 switch (attr.usage) {
2448 case AUDIO_USAGE_VIRTUAL_SOURCE:
2449 // virtual source is not expected to have an associated product strategy
2450 break;
2451 default:
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002452 ALOGE("invalid attributes %s when converting to stream", toString(attr).c_str());
Jean-Michel Trivied678652019-12-19 13:39:30 -08002453 break;
2454 }
François Gaffied0ba9ed2018-11-05 11:50:42 +01002455 return AUDIO_STREAM_MUSIC;
2456}
2457
François Gaffie1e2b56f2022-04-01 14:34:29 +02002458status_t AudioSystem::getProductStrategyFromAudioAttributes(const audio_attributes_t& aa,
Francois Gaffie11b65922020-09-24 16:59:08 +02002459 product_strategy_t& productStrategy,
2460 bool fallbackOnDefault) {
Andy Hung264fa4c2024-02-21 15:52:12 -08002461 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Andy Hungce3d1102024-09-29 21:47:19 -07002462 if (aps == nullptr) return AudioPolicyServiceTraits::getError();
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002463
Mikhail Naganov1c400902023-05-17 11:48:43 -07002464 media::audio::common::AudioAttributes aaAidl = VALUE_OR_RETURN_STATUS(
2465 legacy2aidl_audio_attributes_t_AudioAttributes(aa));
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002466 int32_t productStrategyAidl;
2467
2468 RETURN_STATUS_IF_ERROR(statusTFromBinderStatus(
Francois Gaffie11b65922020-09-24 16:59:08 +02002469 aps->getProductStrategyFromAudioAttributes(aaAidl, fallbackOnDefault,
2470 &productStrategyAidl)));
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002471 productStrategy = VALUE_OR_RETURN_STATUS(
2472 aidl2legacy_int32_t_product_strategy_t(productStrategyAidl));
2473 return OK;
François Gaffie4b2018b2018-11-07 11:18:59 +01002474}
2475
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002476status_t AudioSystem::listAudioVolumeGroups(AudioVolumeGroupVector& groups) {
Andy Hung264fa4c2024-02-21 15:52:12 -08002477 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Andy Hungce3d1102024-09-29 21:47:19 -07002478 if (aps == nullptr) return AudioPolicyServiceTraits::getError();
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002479
2480 std::vector<media::AudioVolumeGroup> groupsAidl;
2481 RETURN_STATUS_IF_ERROR(
2482 statusTFromBinderStatus(aps->listAudioVolumeGroups(&groupsAidl)));
2483 groups = VALUE_OR_RETURN_STATUS(
2484 convertContainer<AudioVolumeGroupVector>(groupsAidl, aidl2legacy_AudioVolumeGroup));
2485 return OK;
François Gaffie4b2018b2018-11-07 11:18:59 +01002486}
2487
François Gaffie1e2b56f2022-04-01 14:34:29 +02002488status_t AudioSystem::getVolumeGroupFromAudioAttributes(const audio_attributes_t &aa,
Francois Gaffie11b65922020-09-24 16:59:08 +02002489 volume_group_t& volumeGroup,
2490 bool fallbackOnDefault) {
Andy Hung264fa4c2024-02-21 15:52:12 -08002491 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Andy Hungce3d1102024-09-29 21:47:19 -07002492 if (aps == nullptr) return AudioPolicyServiceTraits::getError();
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002493
Mikhail Naganov1c400902023-05-17 11:48:43 -07002494 media::audio::common::AudioAttributes aaAidl = VALUE_OR_RETURN_STATUS(
2495 legacy2aidl_audio_attributes_t_AudioAttributes(aa));
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002496 int32_t volumeGroupAidl;
2497 RETURN_STATUS_IF_ERROR(statusTFromBinderStatus(
Francois Gaffie11b65922020-09-24 16:59:08 +02002498 aps->getVolumeGroupFromAudioAttributes(aaAidl, fallbackOnDefault, &volumeGroupAidl)));
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002499 volumeGroup = VALUE_OR_RETURN_STATUS(aidl2legacy_int32_t_volume_group_t(volumeGroupAidl));
2500 return OK;
Arun Mirpuri11029ad2018-12-19 20:45:19 -08002501}
Eric Laurentb78763e2018-10-17 10:08:02 -07002502
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002503status_t AudioSystem::setRttEnabled(bool enabled) {
Andy Hung264fa4c2024-02-21 15:52:12 -08002504 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Andy Hungce3d1102024-09-29 21:47:19 -07002505 if (aps == nullptr) return AudioPolicyServiceTraits::getError();
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002506 return statusTFromBinderStatus(aps->setRttEnabled(enabled));
Eric Laurent6ede98f2019-06-11 14:50:30 -07002507}
2508
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002509bool AudioSystem::isCallScreenModeSupported() {
Andy Hung264fa4c2024-02-21 15:52:12 -08002510 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Andy Hungce3d1102024-09-29 21:47:19 -07002511 if (aps == nullptr) return false;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002512
2513 auto result = [&]() -> ConversionResult<bool> {
2514 bool retAidl;
2515 RETURN_IF_ERROR(
2516 statusTFromBinderStatus(aps->isCallScreenModeSupported(&retAidl)));
2517 return retAidl;
2518 }();
2519 return result.value_or(false);
Eric Laurent8340e672019-11-06 11:01:08 -08002520}
2521
jiabin0a488932020-08-07 17:32:40 -07002522status_t AudioSystem::setDevicesRoleForStrategy(product_strategy_t strategy,
2523 device_role_t role,
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002524 const AudioDeviceTypeAddrVector& devices) {
Andy Hung264fa4c2024-02-21 15:52:12 -08002525 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Andy Hungce3d1102024-09-29 21:47:19 -07002526 if (aps == nullptr) return AudioPolicyServiceTraits::getError();
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002527
2528 int32_t strategyAidl = VALUE_OR_RETURN_STATUS(legacy2aidl_product_strategy_t_int32_t(strategy));
2529 media::DeviceRole roleAidl = VALUE_OR_RETURN_STATUS(legacy2aidl_device_role_t_DeviceRole(role));
Mikhail Naganovf4a75362021-09-16 00:02:54 +00002530 std::vector<AudioDevice> devicesAidl = VALUE_OR_RETURN_STATUS(
2531 convertContainer<std::vector<AudioDevice>>(devices,
2532 legacy2aidl_AudioDeviceTypeAddress));
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002533 return statusTFromBinderStatus(
2534 aps->setDevicesRoleForStrategy(strategyAidl, roleAidl, devicesAidl));
Jean-Michel Trivi30857152019-11-01 11:04:15 -07002535}
2536
Paul Wang5d7cdb52022-11-22 09:45:06 +00002537status_t AudioSystem::removeDevicesRoleForStrategy(product_strategy_t strategy,
2538 device_role_t role,
2539 const AudioDeviceTypeAddrVector& devices) {
Andy Hung264fa4c2024-02-21 15:52:12 -08002540 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Andy Hungce3d1102024-09-29 21:47:19 -07002541 if (aps == nullptr) return AudioPolicyServiceTraits::getError();
Paul Wang5d7cdb52022-11-22 09:45:06 +00002542
2543 int32_t strategyAidl = VALUE_OR_RETURN_STATUS(legacy2aidl_product_strategy_t_int32_t(strategy));
2544 media::DeviceRole roleAidl = VALUE_OR_RETURN_STATUS(legacy2aidl_device_role_t_DeviceRole(role));
2545 std::vector<AudioDevice> devicesAidl = VALUE_OR_RETURN_STATUS(
2546 convertContainer<std::vector<AudioDevice>>(devices,
2547 legacy2aidl_AudioDeviceTypeAddress));
2548 return statusTFromBinderStatus(
2549 aps->removeDevicesRoleForStrategy(strategyAidl, roleAidl, devicesAidl));
2550}
2551
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002552status_t
Paul Wang5d7cdb52022-11-22 09:45:06 +00002553AudioSystem::clearDevicesRoleForStrategy(product_strategy_t strategy, device_role_t role) {
Andy Hung264fa4c2024-02-21 15:52:12 -08002554 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Andy Hungce3d1102024-09-29 21:47:19 -07002555 if (aps == nullptr) return AudioPolicyServiceTraits::getError();
2556
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002557 int32_t strategyAidl = VALUE_OR_RETURN_STATUS(legacy2aidl_product_strategy_t_int32_t(strategy));
2558 media::DeviceRole roleAidl = VALUE_OR_RETURN_STATUS(legacy2aidl_device_role_t_DeviceRole(role));
2559 return statusTFromBinderStatus(
Paul Wang5d7cdb52022-11-22 09:45:06 +00002560 aps->clearDevicesRoleForStrategy(strategyAidl, roleAidl));
Jean-Michel Trivi30857152019-11-01 11:04:15 -07002561}
2562
jiabin0a488932020-08-07 17:32:40 -07002563status_t AudioSystem::getDevicesForRoleAndStrategy(product_strategy_t strategy,
2564 device_role_t role,
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002565 AudioDeviceTypeAddrVector& devices) {
Andy Hung264fa4c2024-02-21 15:52:12 -08002566 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Andy Hungce3d1102024-09-29 21:47:19 -07002567 if (aps == nullptr) return AudioPolicyServiceTraits::getError();
2568
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002569 int32_t strategyAidl = VALUE_OR_RETURN_STATUS(legacy2aidl_product_strategy_t_int32_t(strategy));
2570 media::DeviceRole roleAidl = VALUE_OR_RETURN_STATUS(legacy2aidl_device_role_t_DeviceRole(role));
Mikhail Naganovf4a75362021-09-16 00:02:54 +00002571 std::vector<AudioDevice> devicesAidl;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002572 RETURN_STATUS_IF_ERROR(statusTFromBinderStatus(
2573 aps->getDevicesForRoleAndStrategy(strategyAidl, roleAidl, &devicesAidl)));
2574 devices = VALUE_OR_RETURN_STATUS(
2575 convertContainer<AudioDeviceTypeAddrVector>(devicesAidl,
2576 aidl2legacy_AudioDeviceTypeAddress));
2577 return OK;
Jean-Michel Trivi30857152019-11-01 11:04:15 -07002578}
2579
Jiabin Huang3b98d322020-09-03 17:54:16 +00002580status_t AudioSystem::setDevicesRoleForCapturePreset(audio_source_t audioSource,
2581 device_role_t role,
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002582 const AudioDeviceTypeAddrVector& devices) {
Andy Hung264fa4c2024-02-21 15:52:12 -08002583 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Andy Hungce3d1102024-09-29 21:47:19 -07002584 if (aps == nullptr) return AudioPolicyServiceTraits::getError();
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002585
Mikhail Naganovddceecc2021-09-03 13:58:56 -07002586 AudioSource audioSourceAidl = VALUE_OR_RETURN_STATUS(
2587 legacy2aidl_audio_source_t_AudioSource(audioSource));
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002588 media::DeviceRole roleAidl = VALUE_OR_RETURN_STATUS(legacy2aidl_device_role_t_DeviceRole(role));
Mikhail Naganovf4a75362021-09-16 00:02:54 +00002589 std::vector<AudioDevice> devicesAidl = VALUE_OR_RETURN_STATUS(
2590 convertContainer<std::vector<AudioDevice>>(devices,
2591 legacy2aidl_AudioDeviceTypeAddress));
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002592 return statusTFromBinderStatus(
2593 aps->setDevicesRoleForCapturePreset(audioSourceAidl, roleAidl, devicesAidl));
Jiabin Huang3b98d322020-09-03 17:54:16 +00002594}
2595
2596status_t AudioSystem::addDevicesRoleForCapturePreset(audio_source_t audioSource,
2597 device_role_t role,
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002598 const AudioDeviceTypeAddrVector& devices) {
Andy Hung264fa4c2024-02-21 15:52:12 -08002599 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Andy Hungce3d1102024-09-29 21:47:19 -07002600 if (aps == nullptr) return AudioPolicyServiceTraits::getError();
2601
Mikhail Naganovddceecc2021-09-03 13:58:56 -07002602 AudioSource audioSourceAidl = VALUE_OR_RETURN_STATUS(
2603 legacy2aidl_audio_source_t_AudioSource(audioSource));
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002604 media::DeviceRole roleAidl = VALUE_OR_RETURN_STATUS(legacy2aidl_device_role_t_DeviceRole(role));
Mikhail Naganovf4a75362021-09-16 00:02:54 +00002605 std::vector<AudioDevice> devicesAidl = VALUE_OR_RETURN_STATUS(
2606 convertContainer<std::vector<AudioDevice>>(devices,
2607 legacy2aidl_AudioDeviceTypeAddress));
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002608 return statusTFromBinderStatus(
2609 aps->addDevicesRoleForCapturePreset(audioSourceAidl, roleAidl, devicesAidl));
Jiabin Huang3b98d322020-09-03 17:54:16 +00002610}
2611
2612status_t AudioSystem::removeDevicesRoleForCapturePreset(
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002613 audio_source_t audioSource, device_role_t role, const AudioDeviceTypeAddrVector& devices) {
Andy Hung264fa4c2024-02-21 15:52:12 -08002614 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Andy Hungce3d1102024-09-29 21:47:19 -07002615 if (aps == nullptr) return AudioPolicyServiceTraits::getError();
2616
Mikhail Naganovddceecc2021-09-03 13:58:56 -07002617 AudioSource audioSourceAidl = VALUE_OR_RETURN_STATUS(
2618 legacy2aidl_audio_source_t_AudioSource(audioSource));
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002619 media::DeviceRole roleAidl = VALUE_OR_RETURN_STATUS(legacy2aidl_device_role_t_DeviceRole(role));
Mikhail Naganovf4a75362021-09-16 00:02:54 +00002620 std::vector<AudioDevice> devicesAidl = VALUE_OR_RETURN_STATUS(
2621 convertContainer<std::vector<AudioDevice>>(devices,
2622 legacy2aidl_AudioDeviceTypeAddress));
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002623 return statusTFromBinderStatus(
2624 aps->removeDevicesRoleForCapturePreset(audioSourceAidl, roleAidl, devicesAidl));
Jiabin Huang3b98d322020-09-03 17:54:16 +00002625}
2626
2627status_t AudioSystem::clearDevicesRoleForCapturePreset(audio_source_t audioSource,
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002628 device_role_t role) {
Andy Hung264fa4c2024-02-21 15:52:12 -08002629 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Andy Hungce3d1102024-09-29 21:47:19 -07002630 if (aps == nullptr) return AudioPolicyServiceTraits::getError();
2631
Mikhail Naganovddceecc2021-09-03 13:58:56 -07002632 AudioSource audioSourceAidl = VALUE_OR_RETURN_STATUS(
2633 legacy2aidl_audio_source_t_AudioSource(audioSource));
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002634 media::DeviceRole roleAidl = VALUE_OR_RETURN_STATUS(legacy2aidl_device_role_t_DeviceRole(role));
2635 return statusTFromBinderStatus(
2636 aps->clearDevicesRoleForCapturePreset(audioSourceAidl, roleAidl));
Jiabin Huang3b98d322020-09-03 17:54:16 +00002637}
2638
2639status_t AudioSystem::getDevicesForRoleAndCapturePreset(audio_source_t audioSource,
2640 device_role_t role,
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002641 AudioDeviceTypeAddrVector& devices) {
Andy Hung264fa4c2024-02-21 15:52:12 -08002642 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Andy Hungce3d1102024-09-29 21:47:19 -07002643 if (aps == nullptr) return AudioPolicyServiceTraits::getError();
Mikhail Naganovddceecc2021-09-03 13:58:56 -07002644 AudioSource audioSourceAidl = VALUE_OR_RETURN_STATUS(
2645 legacy2aidl_audio_source_t_AudioSource(audioSource));
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002646 media::DeviceRole roleAidl = VALUE_OR_RETURN_STATUS(legacy2aidl_device_role_t_DeviceRole(role));
Mikhail Naganovf4a75362021-09-16 00:02:54 +00002647 std::vector<AudioDevice> devicesAidl;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002648 RETURN_STATUS_IF_ERROR(statusTFromBinderStatus(
2649 aps->getDevicesForRoleAndCapturePreset(audioSourceAidl, roleAidl, &devicesAidl)));
2650 devices = VALUE_OR_RETURN_STATUS(
2651 convertContainer<AudioDeviceTypeAddrVector>(devicesAidl,
2652 aidl2legacy_AudioDeviceTypeAddress));
2653 return OK;
Jiabin Huang3b98d322020-09-03 17:54:16 +00002654}
2655
Eric Laurent81dd0f52021-07-05 11:54:40 +02002656status_t AudioSystem::getSpatializer(const sp<media::INativeSpatializerCallback>& callback,
2657 sp<media::ISpatializer>* spatializer) {
Andy Hung264fa4c2024-02-21 15:52:12 -08002658 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Eric Laurent81dd0f52021-07-05 11:54:40 +02002659 if (spatializer == nullptr) {
2660 return BAD_VALUE;
2661 }
Andy Hungce3d1102024-09-29 21:47:19 -07002662 if (aps == nullptr) return AudioPolicyServiceTraits::getError();
Eric Laurent81dd0f52021-07-05 11:54:40 +02002663 media::GetSpatializerResponse response;
2664 RETURN_STATUS_IF_ERROR(statusTFromBinderStatus(
2665 aps->getSpatializer(callback, &response)));
2666
2667 *spatializer = response.spatializer;
2668 return OK;
2669}
2670
2671status_t AudioSystem::canBeSpatialized(const audio_attributes_t *attr,
2672 const audio_config_t *config,
2673 const AudioDeviceTypeAddrVector &devices,
2674 bool *canBeSpatialized) {
Andy Hung264fa4c2024-02-21 15:52:12 -08002675 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Ram Mohan43297012022-08-30 16:11:49 +05302676 if (canBeSpatialized == nullptr) {
2677 return BAD_VALUE;
2678 }
Andy Hungce3d1102024-09-29 21:47:19 -07002679 if (aps == nullptr) return AudioPolicyServiceTraits::getError();
Eric Laurent81dd0f52021-07-05 11:54:40 +02002680 audio_attributes_t attributes = attr != nullptr ? *attr : AUDIO_ATTRIBUTES_INITIALIZER;
2681 audio_config_t configuration = config != nullptr ? *config : AUDIO_CONFIG_INITIALIZER;
2682
Mikhail Naganov1c400902023-05-17 11:48:43 -07002683 std::optional<media::audio::common::AudioAttributes> attrAidl = VALUE_OR_RETURN_STATUS(
2684 legacy2aidl_audio_attributes_t_AudioAttributes(attributes));
Mikhail Naganovdbf03642021-08-25 18:15:32 -07002685 std::optional<AudioConfig> configAidl = VALUE_OR_RETURN_STATUS(
Eric Laurent81dd0f52021-07-05 11:54:40 +02002686 legacy2aidl_audio_config_t_AudioConfig(configuration, false /*isInput*/));
Mikhail Naganovf4a75362021-09-16 00:02:54 +00002687 std::vector<AudioDevice> devicesAidl = VALUE_OR_RETURN_STATUS(
2688 convertContainer<std::vector<AudioDevice>>(devices,
2689 legacy2aidl_AudioDeviceTypeAddress));
Eric Laurent81dd0f52021-07-05 11:54:40 +02002690 RETURN_STATUS_IF_ERROR(statusTFromBinderStatus(
2691 aps->canBeSpatialized(attrAidl, configAidl, devicesAidl, canBeSpatialized)));
2692 return OK;
2693}
2694
Vlad Popae3fd1c22022-11-07 21:03:18 +01002695status_t AudioSystem::getSoundDoseInterface(const sp<media::ISoundDoseCallback>& callback,
2696 sp<media::ISoundDose>* soundDose) {
Andy Hung264fa4c2024-02-21 15:52:12 -08002697 const sp<IAudioFlinger> af = get_audio_flinger();
Andy Hungce3d1102024-09-29 21:47:19 -07002698 if (af == nullptr) return AudioFlingerServiceTraits::getError();
Vlad Popae3fd1c22022-11-07 21:03:18 +01002699 if (soundDose == nullptr) {
2700 return BAD_VALUE;
2701 }
Vlad Popa63f047e2022-11-05 14:09:19 +01002702
Vlad Popae3fd1c22022-11-07 21:03:18 +01002703 RETURN_STATUS_IF_ERROR(af->getSoundDoseInterface(callback, soundDose));
2704 return OK;
Vlad Popa63f047e2022-11-05 14:09:19 +01002705}
2706
jiabin2b9d5a12021-12-10 01:06:29 +00002707status_t AudioSystem::getDirectPlaybackSupport(const audio_attributes_t *attr,
2708 const audio_config_t *config,
2709 audio_direct_mode_t* directMode) {
2710 if (attr == nullptr || config == nullptr || directMode == nullptr) {
2711 return BAD_VALUE;
2712 }
2713
Andy Hung264fa4c2024-02-21 15:52:12 -08002714 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Andy Hungce3d1102024-09-29 21:47:19 -07002715 if (aps == nullptr) return AudioPolicyServiceTraits::getError();
jiabin2b9d5a12021-12-10 01:06:29 +00002716
Mikhail Naganov1c400902023-05-17 11:48:43 -07002717 media::audio::common::AudioAttributes attrAidl = VALUE_OR_RETURN_STATUS(
2718 legacy2aidl_audio_attributes_t_AudioAttributes(*attr));
jiabin2b9d5a12021-12-10 01:06:29 +00002719 AudioConfig configAidl = VALUE_OR_RETURN_STATUS(
2720 legacy2aidl_audio_config_t_AudioConfig(*config, false /*isInput*/));
2721
2722 media::AudioDirectMode retAidl;
2723 RETURN_STATUS_IF_ERROR(statusTFromBinderStatus(
2724 aps->getDirectPlaybackSupport(attrAidl, configAidl, &retAidl)));
2725 *directMode = VALUE_OR_RETURN_STATUS(aidl2legacy_int32_t_audio_direct_mode_t_mask(
2726 static_cast<int32_t>(retAidl)));
2727 return NO_ERROR;
2728}
2729
Dorin Drimusf2196d82022-01-03 12:11:18 +01002730status_t AudioSystem::getDirectProfilesForAttributes(const audio_attributes_t* attr,
2731 std::vector<audio_profile>* audioProfiles) {
Mikhail Naganovefc504b2022-06-22 03:24:59 +00002732 if (attr == nullptr || audioProfiles == nullptr) {
Dorin Drimusf2196d82022-01-03 12:11:18 +01002733 return BAD_VALUE;
2734 }
2735
Andy Hung264fa4c2024-02-21 15:52:12 -08002736 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Andy Hungce3d1102024-09-29 21:47:19 -07002737 if (aps == nullptr) return AudioPolicyServiceTraits::getError();
Dorin Drimusf2196d82022-01-03 12:11:18 +01002738
Mikhail Naganov1c400902023-05-17 11:48:43 -07002739 media::audio::common::AudioAttributes attrAidl = VALUE_OR_RETURN_STATUS(
2740 legacy2aidl_audio_attributes_t_AudioAttributes(*attr));
Dorin Drimusf2196d82022-01-03 12:11:18 +01002741
2742 std::vector<media::audio::common::AudioProfile> audioProfilesAidl;
2743 RETURN_STATUS_IF_ERROR(statusTFromBinderStatus(
2744 aps->getDirectProfilesForAttributes(attrAidl, &audioProfilesAidl)));
2745 *audioProfiles = VALUE_OR_RETURN_STATUS(convertContainer<std::vector<audio_profile>>(
2746 audioProfilesAidl, aidl2legacy_AudioProfile_audio_profile, false /*isInput*/));
2747
2748 return NO_ERROR;
2749}
Eric Laurent81dd0f52021-07-05 11:54:40 +02002750
Eric Laurent076e7c72022-05-03 18:12:28 +02002751status_t AudioSystem::setRequestedLatencyMode(
2752 audio_io_handle_t output, audio_latency_mode_t mode) {
Andy Hung264fa4c2024-02-21 15:52:12 -08002753 const sp<IAudioFlinger> af = get_audio_flinger();
Andy Hungce3d1102024-09-29 21:47:19 -07002754 if (af == nullptr) return AudioFlingerServiceTraits::getError();
Eric Laurent076e7c72022-05-03 18:12:28 +02002755 return af->setRequestedLatencyMode(output, mode);
2756}
2757
2758status_t AudioSystem::getSupportedLatencyModes(audio_io_handle_t output,
2759 std::vector<audio_latency_mode_t>* modes) {
Andy Hung264fa4c2024-02-21 15:52:12 -08002760 const sp<IAudioFlinger> af = get_audio_flinger();
Andy Hungce3d1102024-09-29 21:47:19 -07002761 if (af == nullptr) return AudioFlingerServiceTraits::getError();
Eric Laurent076e7c72022-05-03 18:12:28 +02002762 return af->getSupportedLatencyModes(output, modes);
2763}
2764
Eric Laurent50d72582022-12-20 20:20:23 +01002765status_t AudioSystem::setBluetoothVariableLatencyEnabled(bool enabled) {
Andy Hung264fa4c2024-02-21 15:52:12 -08002766 const sp<IAudioFlinger> af = get_audio_flinger();
Andy Hungce3d1102024-09-29 21:47:19 -07002767 if (af == nullptr) return AudioFlingerServiceTraits::getError();
Eric Laurent50d72582022-12-20 20:20:23 +01002768 return af->setBluetoothVariableLatencyEnabled(enabled);
Eric Laurent52057642022-12-16 11:45:07 +01002769}
2770
Eric Laurent50d72582022-12-20 20:20:23 +01002771status_t AudioSystem::isBluetoothVariableLatencyEnabled(
2772 bool *enabled) {
Andy Hung264fa4c2024-02-21 15:52:12 -08002773 const sp<IAudioFlinger> af = get_audio_flinger();
Andy Hungce3d1102024-09-29 21:47:19 -07002774 if (af == nullptr) return AudioFlingerServiceTraits::getError();
Eric Laurent50d72582022-12-20 20:20:23 +01002775 return af->isBluetoothVariableLatencyEnabled(enabled);
2776}
2777
2778status_t AudioSystem::supportsBluetoothVariableLatency(
Eric Laurent52057642022-12-16 11:45:07 +01002779 bool *support) {
Andy Hung264fa4c2024-02-21 15:52:12 -08002780 const sp<IAudioFlinger> af = get_audio_flinger();
Andy Hungce3d1102024-09-29 21:47:19 -07002781 if (af == nullptr) return AudioFlingerServiceTraits::getError();
Eric Laurent50d72582022-12-20 20:20:23 +01002782 return af->supportsBluetoothVariableLatency(support);
Eric Laurent52057642022-12-16 11:45:07 +01002783}
2784
Mikhail Naganovffd97712023-05-03 17:45:36 -07002785status_t AudioSystem::getAudioPolicyConfig(media::AudioPolicyConfig *config) {
Andy Hung264fa4c2024-02-21 15:52:12 -08002786 const sp<IAudioFlinger> af = get_audio_flinger();
Andy Hungce3d1102024-09-29 21:47:19 -07002787 if (af == nullptr) return AudioFlingerServiceTraits::getError();
Mikhail Naganovffd97712023-05-03 17:45:36 -07002788 return af->getAudioPolicyConfig(config);
2789}
2790
Ytai Ben-Tsvi5120eda2020-03-26 09:41:15 -07002791class CaptureStateListenerImpl : public media::BnCaptureStateListener,
2792 public IBinder::DeathRecipient {
2793public:
Ytai Ben-Tsvid9f82832020-08-27 09:31:10 -07002794 CaptureStateListenerImpl(
2795 const sp<IAudioPolicyService>& aps,
2796 const sp<AudioSystem::CaptureStateListener>& listener)
Ytai Ben-Tsvia46b6d32020-08-31 13:29:11 -07002797 : mAps(aps), mListener(listener) {}
2798
2799 void init() {
Ytai Ben-Tsvid9f82832020-08-27 09:31:10 -07002800 bool active;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002801 status_t status = statusTFromBinderStatus(
2802 mAps->registerSoundTriggerCaptureStateListener(this, &active));
Ytai Ben-Tsvid9f82832020-08-27 09:31:10 -07002803 if (status != NO_ERROR) {
2804 mListener->onServiceDied();
2805 return;
2806 }
2807 mListener->onStateChanged(active);
Ytai Ben-Tsvia46b6d32020-08-31 13:29:11 -07002808 IInterface::asBinder(mAps)->linkToDeath(this);
Ytai Ben-Tsvid9f82832020-08-27 09:31:10 -07002809 }
2810
Ytai Ben-Tsvi5120eda2020-03-26 09:41:15 -07002811 binder::Status setCaptureState(bool active) override {
Andy Hungacd955a2024-02-16 17:14:36 -08002812 std::lock_guard _l(AudioSystem::gSoundTriggerMutex);
Ytai Ben-Tsvid9f82832020-08-27 09:31:10 -07002813 mListener->onStateChanged(active);
Ytai Ben-Tsvi5120eda2020-03-26 09:41:15 -07002814 return binder::Status::ok();
2815 }
2816
2817 void binderDied(const wp<IBinder>&) override {
Andy Hungacd955a2024-02-16 17:14:36 -08002818 std::lock_guard _l(AudioSystem::gSoundTriggerMutex);
Ytai Ben-Tsvid9f82832020-08-27 09:31:10 -07002819 mListener->onServiceDied();
Andy Hungacd955a2024-02-16 17:14:36 -08002820 AudioSystem::gSoundTriggerCaptureStateListener = nullptr;
Ytai Ben-Tsvi5120eda2020-03-26 09:41:15 -07002821 }
Ytai Ben-Tsvid9f82832020-08-27 09:31:10 -07002822
2823private:
2824 // Need this in order to keep the death receipent alive.
2825 sp<IAudioPolicyService> mAps;
2826 sp<AudioSystem::CaptureStateListener> mListener;
Ytai Ben-Tsvi5120eda2020-03-26 09:41:15 -07002827};
2828
2829status_t AudioSystem::registerSoundTriggerCaptureStateListener(
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002830 const sp<CaptureStateListener>& listener) {
Ytai Ben-Tsvid9f82832020-08-27 09:31:10 -07002831 LOG_ALWAYS_FATAL_IF(listener == nullptr);
2832
Andy Hung264fa4c2024-02-21 15:52:12 -08002833 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Andy Hungce3d1102024-09-29 21:47:19 -07002834 if (aps == nullptr) return AudioPolicyServiceTraits::getError();
Ytai Ben-Tsvi5120eda2020-03-26 09:41:15 -07002835
Andy Hungacd955a2024-02-16 17:14:36 -08002836 std::lock_guard _l(AudioSystem::gSoundTriggerMutex);
Ytai Ben-Tsvid9f82832020-08-27 09:31:10 -07002837 gSoundTriggerCaptureStateListener = new CaptureStateListenerImpl(aps, listener);
Ytai Ben-Tsvia46b6d32020-08-31 13:29:11 -07002838 gSoundTriggerCaptureStateListener->init();
Ytai Ben-Tsvi5120eda2020-03-26 09:41:15 -07002839
Ytai Ben-Tsvi5120eda2020-03-26 09:41:15 -07002840 return NO_ERROR;
2841}
2842
jiabin1319f5a2021-03-30 22:21:24 +00002843status_t AudioSystem::setVibratorInfos(
2844 const std::vector<media::AudioVibratorInfo>& vibratorInfos) {
Andy Hung264fa4c2024-02-21 15:52:12 -08002845 const sp<IAudioFlinger> af = get_audio_flinger();
Andy Hungce3d1102024-09-29 21:47:19 -07002846 if (af == nullptr) return AudioFlingerServiceTraits::getError();
jiabin1319f5a2021-03-30 22:21:24 +00002847 return af->setVibratorInfos(vibratorInfos);
2848}
2849
Jiabin Huangaa6e9e32024-10-21 17:19:28 +00002850status_t AudioSystem::getMmapPolicyInfos(
jiabine99d0882021-09-17 05:21:25 +00002851 AudioMMapPolicyType policyType, std::vector<AudioMMapPolicyInfo> *policyInfos) {
Jiabin Huangaa6e9e32024-10-21 17:19:28 +00002852 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Andy Hungce3d1102024-09-29 21:47:19 -07002853 if (aps == nullptr) return AudioPolicyServiceTraits::getError();
Jiabin Huangaa6e9e32024-10-21 17:19:28 +00002854 return statusTFromBinderStatus(aps->getMmapPolicyInfos(policyType, policyInfos));
Jiabin Huangebe64102021-09-07 20:01:07 +00002855}
2856
jiabine504e7b2021-09-18 00:27:08 +00002857int32_t AudioSystem::getAAudioMixerBurstCount() {
Andy Hung264fa4c2024-02-21 15:52:12 -08002858 const sp<IAudioFlinger> af = get_audio_flinger();
Andy Hungce3d1102024-09-29 21:47:19 -07002859 if (af == nullptr) return AudioFlingerServiceTraits::getError();
jiabine504e7b2021-09-18 00:27:08 +00002860 return af->getAAudioMixerBurstCount();
2861}
2862
2863int32_t AudioSystem::getAAudioHardwareBurstMinUsec() {
Andy Hung264fa4c2024-02-21 15:52:12 -08002864 const sp<IAudioFlinger> af = get_audio_flinger();
Andy Hungce3d1102024-09-29 21:47:19 -07002865 if (af == nullptr) return AudioFlingerServiceTraits::getError();
jiabine504e7b2021-09-18 00:27:08 +00002866 return af->getAAudioHardwareBurstMinUsec();
2867}
2868
jiabina84c3d32022-12-02 18:59:55 +00002869status_t AudioSystem::getSupportedMixerAttributes(
2870 audio_port_handle_t portId, std::vector<audio_mixer_attributes_t> *mixerAttrs) {
Andy Hung264fa4c2024-02-21 15:52:12 -08002871 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Andy Hungce3d1102024-09-29 21:47:19 -07002872 if (aps == nullptr) return AudioPolicyServiceTraits::getError();
jiabina84c3d32022-12-02 18:59:55 +00002873
2874 int32_t portIdAidl = VALUE_OR_RETURN_STATUS(legacy2aidl_audio_port_handle_t_int32_t(portId));
2875 std::vector<media::AudioMixerAttributesInternal> _aidlReturn;
2876 RETURN_STATUS_IF_ERROR(statusTFromBinderStatus(
2877 aps->getSupportedMixerAttributes(portIdAidl, &_aidlReturn)));
2878 *mixerAttrs = VALUE_OR_RETURN_STATUS(
2879 convertContainer<std::vector<audio_mixer_attributes_t>>(
2880 _aidlReturn,
2881 aidl2legacy_AudioMixerAttributesInternal_audio_mixer_attributes_t));
2882 return OK;
2883}
2884
2885status_t AudioSystem::setPreferredMixerAttributes(const audio_attributes_t *attr,
2886 audio_port_handle_t portId,
2887 uid_t uid,
2888 const audio_mixer_attributes_t *mixerAttr) {
Andy Hung264fa4c2024-02-21 15:52:12 -08002889 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Andy Hungce3d1102024-09-29 21:47:19 -07002890 if (aps == nullptr) return AudioPolicyServiceTraits::getError();
jiabina84c3d32022-12-02 18:59:55 +00002891
Mikhail Naganov1c400902023-05-17 11:48:43 -07002892 media::audio::common::AudioAttributes attrAidl = VALUE_OR_RETURN_STATUS(
2893 legacy2aidl_audio_attributes_t_AudioAttributes(*attr));
jiabina84c3d32022-12-02 18:59:55 +00002894 media::AudioMixerAttributesInternal mixerAttrAidl = VALUE_OR_RETURN_STATUS(
2895 legacy2aidl_audio_mixer_attributes_t_AudioMixerAttributesInternal(*mixerAttr));
2896 int32_t uidAidl = VALUE_OR_RETURN_STATUS(legacy2aidl_uid_t_int32_t(uid));
2897 int32_t portIdAidl = VALUE_OR_RETURN_STATUS(legacy2aidl_audio_port_handle_t_int32_t(portId));
2898
2899 return statusTFromBinderStatus(
2900 aps->setPreferredMixerAttributes(attrAidl, portIdAidl, uidAidl, mixerAttrAidl));
2901}
2902
2903status_t AudioSystem::getPreferredMixerAttributes(
2904 const audio_attributes_t *attr,
2905 audio_port_handle_t portId,
2906 std::optional<audio_mixer_attributes_t> *mixerAttr) {
Andy Hung264fa4c2024-02-21 15:52:12 -08002907 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Andy Hungce3d1102024-09-29 21:47:19 -07002908 if (aps == nullptr) return AudioPolicyServiceTraits::getError();
jiabina84c3d32022-12-02 18:59:55 +00002909
Mikhail Naganov1c400902023-05-17 11:48:43 -07002910 media::audio::common::AudioAttributes attrAidl = VALUE_OR_RETURN_STATUS(
2911 legacy2aidl_audio_attributes_t_AudioAttributes(*attr));
jiabina84c3d32022-12-02 18:59:55 +00002912 int32_t portIdAidl = VALUE_OR_RETURN_STATUS(legacy2aidl_audio_port_handle_t_int32_t(portId));
2913 std::optional<media::AudioMixerAttributesInternal> _aidlReturn;
2914 RETURN_STATUS_IF_ERROR(statusTFromBinderStatus(
2915 aps->getPreferredMixerAttributes(attrAidl, portIdAidl, &_aidlReturn)));
2916
2917 if (_aidlReturn.has_value()) {
2918 *mixerAttr = VALUE_OR_RETURN_STATUS(
2919 aidl2legacy_AudioMixerAttributesInternal_audio_mixer_attributes_t(
2920 _aidlReturn.value()));
2921 }
2922 return NO_ERROR;
2923}
2924
2925status_t AudioSystem::clearPreferredMixerAttributes(const audio_attributes_t *attr,
2926 audio_port_handle_t portId,
2927 uid_t uid) {
Andy Hung264fa4c2024-02-21 15:52:12 -08002928 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Andy Hungce3d1102024-09-29 21:47:19 -07002929 if (aps == nullptr) return AudioPolicyServiceTraits::getError();
jiabina84c3d32022-12-02 18:59:55 +00002930
Mikhail Naganov1c400902023-05-17 11:48:43 -07002931 media::audio::common::AudioAttributes attrAidl = VALUE_OR_RETURN_STATUS(
2932 legacy2aidl_audio_attributes_t_AudioAttributes(*attr));
jiabina84c3d32022-12-02 18:59:55 +00002933 int32_t uidAidl = VALUE_OR_RETURN_STATUS(legacy2aidl_uid_t_int32_t(uid));
2934 int32_t portIdAidl = VALUE_OR_RETURN_STATUS(legacy2aidl_audio_port_handle_t_int32_t(portId));
2935 return statusTFromBinderStatus(
2936 aps->clearPreferredMixerAttributes(attrAidl, portIdAidl, uidAidl));
2937}
2938
Jiabin Huangaa6e9e32024-10-21 17:19:28 +00002939status_t AudioSystem::getMmapPolicyForDevice(AudioMMapPolicyType policyType,
2940 audio_devices_t device,
2941 AudioMMapPolicyInfo *policyInfo) {
2942 const sp<IAudioPolicyService> aps = get_audio_policy_service();
2943 if (aps == nullptr) {
2944 return PERMISSION_DENIED;
2945 }
2946 policyInfo->device.type = VALUE_OR_RETURN_STATUS(
2947 legacy2aidl_audio_devices_t_AudioDeviceDescription(device));
2948 return statusTFromBinderStatus(aps->getMmapPolicyForDevice(policyType, policyInfo));
2949}
2950
Eric Laurentc2f1f072009-07-17 12:17:14 -07002951// ---------------------------------------------------------------------------
2952
Eric Laurente8726fe2015-06-26 09:39:24 -07002953int AudioSystem::AudioPolicyServiceClient::addAudioPortCallback(
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002954 const sp<AudioPortCallback>& callback) {
Andy Hungacd955a2024-02-16 17:14:36 -08002955 std::lock_guard _l(mMutex);
Andy Hungd211ce02024-02-16 15:34:07 -08002956 return mAudioPortCallbacks.insert(callback).second ? mAudioPortCallbacks.size() : -1;
Eric Laurentb28753e2015-04-01 13:06:28 -07002957}
2958
Eric Laurente8726fe2015-06-26 09:39:24 -07002959int AudioSystem::AudioPolicyServiceClient::removeAudioPortCallback(
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002960 const sp<AudioPortCallback>& callback) {
Andy Hungacd955a2024-02-16 17:14:36 -08002961 std::lock_guard _l(mMutex);
Andy Hungd211ce02024-02-16 15:34:07 -08002962 return mAudioPortCallbacks.erase(callback) > 0 ? mAudioPortCallbacks.size() : -1;
Eric Laurentb28753e2015-04-01 13:06:28 -07002963}
2964
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002965Status AudioSystem::AudioPolicyServiceClient::onAudioPortListUpdate() {
Andy Hungacd955a2024-02-16 17:14:36 -08002966 std::lock_guard _l(mMutex);
Andy Hungd211ce02024-02-16 15:34:07 -08002967 for (const auto& callback : mAudioPortCallbacks) {
2968 callback->onAudioPortListUpdate();
Eric Laurentb28753e2015-04-01 13:06:28 -07002969 }
Ytai Ben-Tsvi7e7a79d2020-12-15 16:48:16 -08002970 return Status::ok();
Eric Laurentb28753e2015-04-01 13:06:28 -07002971}
2972
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002973Status AudioSystem::AudioPolicyServiceClient::onAudioPatchListUpdate() {
Andy Hungacd955a2024-02-16 17:14:36 -08002974 std::lock_guard _l(mMutex);
Andy Hungd211ce02024-02-16 15:34:07 -08002975 for (const auto& callback : mAudioPortCallbacks) {
2976 callback->onAudioPatchListUpdate();
Eric Laurentb28753e2015-04-01 13:06:28 -07002977 }
Ytai Ben-Tsvi7e7a79d2020-12-15 16:48:16 -08002978 return Status::ok();
Eric Laurentb28753e2015-04-01 13:06:28 -07002979}
2980
François Gaffiecfe17322018-11-07 13:41:29 +01002981// ----------------------------------------------------------------------------
2982int AudioSystem::AudioPolicyServiceClient::addAudioVolumeGroupCallback(
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002983 const sp<AudioVolumeGroupCallback>& callback) {
Andy Hungacd955a2024-02-16 17:14:36 -08002984 std::lock_guard _l(mMutex);
Andy Hungd211ce02024-02-16 15:34:07 -08002985 return mAudioVolumeGroupCallbacks.insert(callback).second
2986 ? mAudioVolumeGroupCallbacks.size() : -1;
François Gaffiecfe17322018-11-07 13:41:29 +01002987}
2988
2989int AudioSystem::AudioPolicyServiceClient::removeAudioVolumeGroupCallback(
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002990 const sp<AudioVolumeGroupCallback>& callback) {
Andy Hungacd955a2024-02-16 17:14:36 -08002991 std::lock_guard _l(mMutex);
Andy Hungd211ce02024-02-16 15:34:07 -08002992 return mAudioVolumeGroupCallbacks.erase(callback) > 0
2993 ? mAudioVolumeGroupCallbacks.size() : -1;
François Gaffiecfe17322018-11-07 13:41:29 +01002994}
2995
Ytai Ben-Tsvi7e7a79d2020-12-15 16:48:16 -08002996Status AudioSystem::AudioPolicyServiceClient::onAudioVolumeGroupChanged(int32_t group,
2997 int32_t flags) {
2998 volume_group_t groupLegacy = VALUE_OR_RETURN_BINDER_STATUS(
2999 aidl2legacy_int32_t_volume_group_t(group));
3000 int flagsLegacy = VALUE_OR_RETURN_BINDER_STATUS(convertReinterpret<int>(flags));
3001
Andy Hungacd955a2024-02-16 17:14:36 -08003002 std::lock_guard _l(mMutex);
Andy Hungd211ce02024-02-16 15:34:07 -08003003 for (const auto& callback : mAudioVolumeGroupCallbacks) {
3004 callback->onAudioVolumeGroupChanged(groupLegacy, flagsLegacy);
François Gaffiecfe17322018-11-07 13:41:29 +01003005 }
Ytai Ben-Tsvi7e7a79d2020-12-15 16:48:16 -08003006 return Status::ok();
François Gaffiecfe17322018-11-07 13:41:29 +01003007}
3008// ----------------------------------------------------------------------------
3009
Ytai Ben-Tsvi7e7a79d2020-12-15 16:48:16 -08003010Status AudioSystem::AudioPolicyServiceClient::onDynamicPolicyMixStateUpdate(
3011 const ::std::string& regId, int32_t state) {
3012 ALOGV("AudioPolicyServiceClient::onDynamicPolicyMixStateUpdate(%s, %d)", regId.c_str(), state);
3013
3014 String8 regIdLegacy = VALUE_OR_RETURN_BINDER_STATUS(aidl2legacy_string_view_String8(regId));
3015 int stateLegacy = VALUE_OR_RETURN_BINDER_STATUS(convertReinterpret<int>(state));
Jean-Michel Trivif613d422015-04-23 18:41:29 -07003016 dynamic_policy_callback cb = NULL;
3017 {
Andy Hungacd955a2024-02-16 17:14:36 -08003018 std::lock_guard _l(AudioSystem::gMutex);
Jean-Michel Trivif613d422015-04-23 18:41:29 -07003019 cb = gDynPolicyCallback;
3020 }
3021
3022 if (cb != NULL) {
Ytai Ben-Tsvi7e7a79d2020-12-15 16:48:16 -08003023 cb(DYNAMIC_POLICY_EVENT_MIX_STATE_UPDATE, regIdLegacy, stateLegacy);
Jean-Michel Trivif613d422015-04-23 18:41:29 -07003024 }
Ytai Ben-Tsvi7e7a79d2020-12-15 16:48:16 -08003025 return Status::ok();
Jean-Michel Trivide801052015-04-14 19:10:14 -07003026}
3027
Ytai Ben-Tsvi7e7a79d2020-12-15 16:48:16 -08003028Status AudioSystem::AudioPolicyServiceClient::onRecordingConfigurationUpdate(
3029 int32_t event,
3030 const media::RecordClientInfo& clientInfo,
Mikhail Naganovdbf03642021-08-25 18:15:32 -07003031 const AudioConfigBase& clientConfig,
Ytai Ben-Tsvi7e7a79d2020-12-15 16:48:16 -08003032 const std::vector<media::EffectDescriptor>& clientEffects,
Mikhail Naganovdbf03642021-08-25 18:15:32 -07003033 const AudioConfigBase& deviceConfig,
Ytai Ben-Tsvi7e7a79d2020-12-15 16:48:16 -08003034 const std::vector<media::EffectDescriptor>& effects,
3035 int32_t patchHandle,
Mikhail Naganovddceecc2021-09-03 13:58:56 -07003036 AudioSource source) {
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -08003037 record_config_callback cb = NULL;
3038 {
Andy Hungacd955a2024-02-16 17:14:36 -08003039 std::lock_guard _l(AudioSystem::gMutex);
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -08003040 cb = gRecordConfigCallback;
3041 }
3042
3043 if (cb != NULL) {
Ytai Ben-Tsvi7e7a79d2020-12-15 16:48:16 -08003044 int eventLegacy = VALUE_OR_RETURN_BINDER_STATUS(convertReinterpret<int>(event));
3045 record_client_info_t clientInfoLegacy = VALUE_OR_RETURN_BINDER_STATUS(
3046 aidl2legacy_RecordClientInfo_record_client_info_t(clientInfo));
3047 audio_config_base_t clientConfigLegacy = VALUE_OR_RETURN_BINDER_STATUS(
Mikhail Naganovde3fa182021-07-30 15:06:42 -07003048 aidl2legacy_AudioConfigBase_audio_config_base_t(clientConfig, true /*isInput*/));
Ytai Ben-Tsvi7e7a79d2020-12-15 16:48:16 -08003049 std::vector<effect_descriptor_t> clientEffectsLegacy = VALUE_OR_RETURN_BINDER_STATUS(
3050 convertContainer<std::vector<effect_descriptor_t>>(
3051 clientEffects,
3052 aidl2legacy_EffectDescriptor_effect_descriptor_t));
3053 audio_config_base_t deviceConfigLegacy = VALUE_OR_RETURN_BINDER_STATUS(
Mikhail Naganovde3fa182021-07-30 15:06:42 -07003054 aidl2legacy_AudioConfigBase_audio_config_base_t(deviceConfig, true /*isInput*/));
Ytai Ben-Tsvi7e7a79d2020-12-15 16:48:16 -08003055 std::vector<effect_descriptor_t> effectsLegacy = VALUE_OR_RETURN_BINDER_STATUS(
3056 convertContainer<std::vector<effect_descriptor_t>>(
3057 effects,
3058 aidl2legacy_EffectDescriptor_effect_descriptor_t));
3059 audio_patch_handle_t patchHandleLegacy = VALUE_OR_RETURN_BINDER_STATUS(
3060 aidl2legacy_int32_t_audio_patch_handle_t(patchHandle));
3061 audio_source_t sourceLegacy = VALUE_OR_RETURN_BINDER_STATUS(
Mikhail Naganovddceecc2021-09-03 13:58:56 -07003062 aidl2legacy_AudioSource_audio_source_t(source));
Ytai Ben-Tsvi7e7a79d2020-12-15 16:48:16 -08003063 cb(eventLegacy, &clientInfoLegacy, &clientConfigLegacy, clientEffectsLegacy,
3064 &deviceConfigLegacy, effectsLegacy, patchHandleLegacy, sourceLegacy);
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -08003065 }
Ytai Ben-Tsvi7e7a79d2020-12-15 16:48:16 -08003066 return Status::ok();
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -08003067}
3068
Jean-Michel Trivi9a6b9ad2020-10-22 16:46:43 -07003069Status AudioSystem::AudioPolicyServiceClient::onRoutingUpdated() {
3070 routing_callback cb = NULL;
3071 {
Andy Hungacd955a2024-02-16 17:14:36 -08003072 std::lock_guard _l(AudioSystem::gMutex);
Jean-Michel Trivi9a6b9ad2020-10-22 16:46:43 -07003073 cb = gRoutingCallback;
3074 }
3075
3076 if (cb != NULL) {
3077 cb();
3078 }
3079 return Status::ok();
3080}
3081
Jean-Michel Trivi78f2b302022-04-15 18:18:41 +00003082Status AudioSystem::AudioPolicyServiceClient::onVolumeRangeInitRequest() {
3083 vol_range_init_req_callback cb = NULL;
3084 {
Andy Hungacd955a2024-02-16 17:14:36 -08003085 std::lock_guard _l(AudioSystem::gMutex);
Jean-Michel Trivi78f2b302022-04-15 18:18:41 +00003086 cb = gVolRangeInitReqCallback;
3087 }
3088
3089 if (cb != NULL) {
3090 cb();
3091 }
3092 return Status::ok();
3093}
3094
Andy Hungce3d1102024-09-29 21:47:19 -07003095void AudioSystem::AudioPolicyServiceClient::onServiceDied() {
3096 std::lock_guard _l(mMutex);
3097 for (const auto& callback : mAudioPortCallbacks) {
3098 callback->onServiceDied();
Eric Laurentb52c1522014-05-20 11:27:36 -07003099 }
Andy Hungce3d1102024-09-29 21:47:19 -07003100 for (const auto& callback : mAudioVolumeGroupCallbacks) {
3101 callback->onServiceDied();
3102 }
Eric Laurentc2f1f072009-07-17 12:17:14 -07003103}
3104
Ytai Ben-Tsvi7e7a79d2020-12-15 16:48:16 -08003105ConversionResult<record_client_info_t>
3106aidl2legacy_RecordClientInfo_record_client_info_t(const media::RecordClientInfo& aidl) {
3107 record_client_info_t legacy;
3108 legacy.riid = VALUE_OR_RETURN(aidl2legacy_int32_t_audio_unique_id_t(aidl.riid));
3109 legacy.uid = VALUE_OR_RETURN(aidl2legacy_int32_t_uid_t(aidl.uid));
3110 legacy.session = VALUE_OR_RETURN(aidl2legacy_int32_t_audio_session_t(aidl.session));
Mikhail Naganovddceecc2021-09-03 13:58:56 -07003111 legacy.source = VALUE_OR_RETURN(aidl2legacy_AudioSource_audio_source_t(aidl.source));
Ytai Ben-Tsvi7e7a79d2020-12-15 16:48:16 -08003112 legacy.port_id = VALUE_OR_RETURN(aidl2legacy_int32_t_audio_port_handle_t(aidl.portId));
3113 legacy.silenced = aidl.silenced;
3114 return legacy;
3115}
3116
3117ConversionResult<media::RecordClientInfo>
3118legacy2aidl_record_client_info_t_RecordClientInfo(const record_client_info_t& legacy) {
3119 media::RecordClientInfo aidl;
3120 aidl.riid = VALUE_OR_RETURN(legacy2aidl_audio_unique_id_t_int32_t(legacy.riid));
3121 aidl.uid = VALUE_OR_RETURN(legacy2aidl_uid_t_int32_t(legacy.uid));
3122 aidl.session = VALUE_OR_RETURN(legacy2aidl_audio_session_t_int32_t(legacy.session));
Mikhail Naganovddceecc2021-09-03 13:58:56 -07003123 aidl.source = VALUE_OR_RETURN(legacy2aidl_audio_source_t_AudioSource(legacy.source));
Ytai Ben-Tsvi7e7a79d2020-12-15 16:48:16 -08003124 aidl.portId = VALUE_OR_RETURN(legacy2aidl_audio_port_handle_t_int32_t(legacy.port_id));
3125 aidl.silenced = legacy.silenced;
3126 return aidl;
3127}
3128
Glenn Kasten40bc9062015-03-20 09:09:33 -07003129} // namespace android