blob: d1b1849be81b547f4d518a71e265aecdf8aede90 [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>
Ytai Ben-Tsvi10dc0a62020-09-18 11:31:55 -070029#include <media/AidlConversion.h>
Eric Laurent21da6472017-11-09 16:29:26 -080030#include <media/AudioResamplerPublic.h>
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080031#include <media/AudioSystem.h>
Glenn Kasten1ab85ec2013-05-31 09:18:43 -070032#include <media/IAudioFlinger.h>
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -080033#include <media/PolicyAidlConversion.h>
François Gaffied0ba9ed2018-11-05 11:50:42 +010034#include <media/TypeConverter.h>
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080035#include <math.h>
36
Dima Zavin64760242011-05-11 14:15:23 -070037#include <system/audio.h>
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -080038#include <android/media/GetInputForAttrResponse.h>
jiabina84c3d32022-12-02 18:59:55 +000039#include <android/media/AudioMixerAttributesInternal.h>
Dima Zavinfce7a472011-04-19 22:30:36 -070040
Ytai Ben-Tsvi1ff75692020-11-06 12:16:12 -080041#define VALUE_OR_RETURN_BINDER_STATUS(x) \
Ytai Ben-Tsvi10dc0a62020-09-18 11:31:55 -070042 ({ auto _tmp = (x); \
Andy Hung1131b6e2020-12-08 20:47:45 -080043 if (!_tmp.ok()) return aidl_utils::binderStatusFromStatusT(_tmp.error()); \
Ytai Ben-Tsvia3815202020-10-28 14:58:08 -070044 std::move(_tmp.value()); })
Ytai Ben-Tsvi10dc0a62020-09-18 11:31:55 -070045
Eric Laurentc2f1f072009-07-17 12:17:14 -070046// ----------------------------------------------------------------------------
Eric Laurentc2f1f072009-07-17 12:17:14 -070047
Marvin Raminbdefaf02023-11-01 09:10:32 +010048namespace audio_flags = android::media::audiopolicy;
49
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080050namespace android {
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -080051using aidl_utils::statusTFromBinderStatus;
Ytai Ben-Tsvi10dc0a62020-09-18 11:31:55 -070052using binder::Status;
Mikhail Naganov57bd06f2021-08-10 16:41:54 -070053using content::AttributionSourceState;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -080054using media::IAudioPolicyService;
Mikhail Naganovdbf03642021-08-25 18:15:32 -070055using media::audio::common::AudioConfig;
56using media::audio::common::AudioConfigBase;
Mikhail Naganovf4a75362021-09-16 00:02:54 +000057using media::audio::common::AudioDevice;
58using media::audio::common::AudioDeviceDescription;
Mikhail Naganov57bd06f2021-08-10 16:41:54 -070059using media::audio::common::AudioFormatDescription;
jiabine99d0882021-09-17 05:21:25 +000060using media::audio::common::AudioMMapPolicyInfo;
61using media::audio::common::AudioMMapPolicyType;
Mikhail Naganovdbf03642021-08-25 18:15:32 -070062using media::audio::common::AudioOffloadInfo;
Mikhail Naganovddceecc2021-09-03 13:58:56 -070063using media::audio::common::AudioSource;
Mikhail Naganovdbf03642021-08-25 18:15:32 -070064using media::audio::common::AudioStreamType;
65using media::audio::common::AudioUsage;
Mikhail Naganov0078ee52021-09-30 23:06:20 +000066using media::audio::common::Int;
Ytai Ben-Tsvi10dc0a62020-09-18 11:31:55 -070067
Andy Hungacd955a2024-02-16 17:14:36 -080068std::mutex AudioSystem::gMutex;
Jean-Michel Trivif613d422015-04-23 18:41:29 -070069dynamic_policy_callback AudioSystem::gDynPolicyCallback = NULL;
Svet Ganovf4ddfef2018-01-16 07:37:58 -080070record_config_callback AudioSystem::gRecordConfigCallback = NULL;
Jean-Michel Trivi9a6b9ad2020-10-22 16:46:43 -070071routing_callback AudioSystem::gRoutingCallback = NULL;
Jean-Michel Trivi78f2b302022-04-15 18:18:41 +000072vol_range_init_req_callback AudioSystem::gVolRangeInitReqCallback = NULL;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080073
Andy Hung88e71732024-02-21 16:44:27 -080074std::mutex AudioSystem::gApsCallbackMutex;
Andy Hungacd955a2024-02-16 17:14:36 -080075std::mutex AudioSystem::gErrorCallbacksMutex;
76std::set<audio_error_callback> AudioSystem::gAudioErrorCallbacks;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -080077
Andy Hungacd955a2024-02-16 17:14:36 -080078std::mutex AudioSystem::gSoundTriggerMutex;
79sp<CaptureStateListenerImpl> AudioSystem::gSoundTriggerCaptureStateListener;
Ytai Ben-Tsvi5120eda2020-03-26 09:41:15 -070080
Andy Hungacd955a2024-02-16 17:14:36 -080081// Sets the Binder for the AudioFlinger service, passed to this client process
82// from the system server.
Ahaan Ugale058f94b2021-05-21 02:23:50 -070083// This allows specific isolated processes to access the audio system. Currently used only for the
84// HotwordDetectionService.
Andy Hungda1fb072024-02-20 19:08:08 -080085template <typename ServiceInterface, typename Client, typename AidlInterface,
86 typename ServiceTraits>
87class ServiceHandler {
88public:
89 sp<ServiceInterface> getService(bool canStartThreadPool = true)
90 EXCLUDES(mMutex) NO_THREAD_SAFETY_ANALYSIS { // std::unique_ptr
91 sp<ServiceInterface> service;
92 sp<Client> client;
Ahaan Ugale058f94b2021-05-21 02:23:50 -070093
Andy Hungda1fb072024-02-20 19:08:08 -080094 bool reportNoError = false;
95 {
96 std::lock_guard _l(mMutex);
97 if (mService != nullptr) {
98 return mService;
99 }
Andy Hung66fc0bd2023-01-04 11:45:50 -0800100 }
101
Andy Hungda1fb072024-02-20 19:08:08 -0800102 std::unique_lock ul_only1thread(mSingleGetter);
103 std::unique_lock ul(mMutex);
104 if (mService != nullptr) {
105 return mService;
106 }
107 if (mClient == nullptr) {
108 mClient = sp<Client>::make();
Andy Hung66fc0bd2023-01-04 11:45:50 -0800109 } else {
110 reportNoError = true;
111 }
Andy Hungda1fb072024-02-20 19:08:08 -0800112 while (true) {
113 mService = mLocalService;
114 if (mService != nullptr) break;
Andy Hung66fc0bd2023-01-04 11:45:50 -0800115
Andy Hungda1fb072024-02-20 19:08:08 -0800116 sp<IBinder> binder = mBinder;
117 if (binder == nullptr) {
118 sp <IServiceManager> sm = defaultServiceManager();
119 binder = sm->checkService(String16(ServiceTraits::SERVICE_NAME));
Paul Wangbcf95f62023-02-28 15:38:16 +0000120 if (binder == nullptr) {
Andy Hungda1fb072024-02-20 19:08:08 -0800121 ALOGD("%s: waiting for %s", __func__, ServiceTraits::SERVICE_NAME);
122
123 // if the condition variable is present, setLocalService() and
124 // setBinder() is allowed to use it to notify us.
125 if (mCvGetter == nullptr) {
126 mCvGetter = std::make_shared<std::condition_variable>();
127 }
128 mCvGetter->wait_for(ul, std::chrono::seconds(1));
129 continue;
Paul Wangbcf95f62023-02-28 15:38:16 +0000130 }
Ahaan Ugale058f94b2021-05-21 02:23:50 -0700131 }
Andy Hungda1fb072024-02-20 19:08:08 -0800132 binder->linkToDeath(mClient);
133 auto aidlInterface = interface_cast<AidlInterface>(binder);
134 LOG_ALWAYS_FATAL_IF(aidlInterface == nullptr);
135 if constexpr (std::is_same_v<ServiceInterface, AidlInterface>) {
136 mService = std::move(aidlInterface);
137 } else /* constexpr */ {
138 mService = ServiceTraits::createServiceAdapter(aidlInterface);
139 }
140 break;
Glenn Kastene53b9ea2012-03-12 16:29:55 -0700141 }
Andy Hungda1fb072024-02-20 19:08:08 -0800142 if (mCvGetter) mCvGetter.reset(); // remove condition variable.
143 client = mClient;
144 service = mService;
145 // Make sure callbacks can be received by the client
146 if (canStartThreadPool) {
Akshata Kadam1d486b72023-08-07 05:49:48 +0000147 ProcessState::self()->startThreadPool();
148 }
Andy Hungda1fb072024-02-20 19:08:08 -0800149 ul.unlock();
150 ul_only1thread.unlock();
151 ServiceTraits::onServiceCreate(service, client);
152 if (reportNoError) AudioSystem::reportError(NO_ERROR);
153 return service;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800154 }
Andy Hungda1fb072024-02-20 19:08:08 -0800155
156 status_t setLocalService(const sp<ServiceInterface>& service) EXCLUDES(mMutex) {
157 std::lock_guard _l(mMutex);
158 // we allow clearing once set, but not a double non-null set.
159 if (mService != nullptr && service != nullptr) return INVALID_OPERATION;
160 mLocalService = service;
161 if (mCvGetter) mCvGetter->notify_one();
162 return OK;
163 }
164
165 sp<Client> getClient() EXCLUDES(mMutex) {
166 const auto service = getService();
167 if (service == nullptr) return nullptr;
168 std::lock_guard _l(mMutex);
169 return mClient;
170 }
171
172 void setBinder(const sp<IBinder>& binder) EXCLUDES(mMutex) {
173 std::lock_guard _l(mMutex);
174 if (mService != nullptr) {
175 ALOGW("%s: ignoring; %s connection already established.",
176 __func__, ServiceTraits::SERVICE_NAME);
177 return;
178 }
179 mBinder = binder;
180 if (mCvGetter) mCvGetter->notify_one();
181 }
182
183 void clearService() EXCLUDES(mMutex) {
184 std::lock_guard _l(mMutex);
185 mService.clear();
186 if (mClient) ServiceTraits::onClearService(mClient);
187 }
188
189private:
190 std::mutex mSingleGetter;
191 std::mutex mMutex;
192 std::shared_ptr<std::condition_variable> mCvGetter GUARDED_BY(mMutex);
193 sp<IBinder> mBinder GUARDED_BY(mMutex);
194 sp<ServiceInterface> mLocalService GUARDED_BY(mMutex);
195 sp<ServiceInterface> mService GUARDED_BY(mMutex);
196 sp<Client> mClient GUARDED_BY(mMutex);
197};
198
199struct AudioFlingerTraits {
200 static void onServiceCreate(
201 const sp<IAudioFlinger>& af, const sp<AudioSystem::AudioFlingerClient>& afc) {
202 const int64_t token = IPCThreadState::self()->clearCallingIdentity();
203 af->registerClient(afc);
204 IPCThreadState::self()->restoreCallingIdentity(token);
205 }
206
207 static sp<IAudioFlinger> createServiceAdapter(
208 const sp<media::IAudioFlingerService>& aidlInterface) {
209 return sp<AudioFlingerClientAdapter>::make(aidlInterface);
210 }
211
212 static void onClearService(const sp<AudioSystem::AudioFlingerClient>& afc) {
213 afc->clearIoCache();
214 }
215
216 static constexpr const char* SERVICE_NAME = IAudioFlinger::DEFAULT_SERVICE_NAME;
217};
218
219[[clang::no_destroy]] static constinit ServiceHandler<IAudioFlinger,
220 AudioSystem::AudioFlingerClient, media::IAudioFlingerService,
221 AudioFlingerTraits> gAudioFlingerServiceHandler;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800222
Andy Hung264fa4c2024-02-21 15:52:12 -0800223sp<IAudioFlinger> AudioSystem::get_audio_flinger() {
Andy Hungda1fb072024-02-20 19:08:08 -0800224 return gAudioFlingerServiceHandler.getService();
Akshata Kadam1d486b72023-08-07 05:49:48 +0000225}
226
Andy Hung264fa4c2024-02-21 15:52:12 -0800227sp<IAudioFlinger> AudioSystem::get_audio_flinger_for_fuzzer() {
Andy Hungda1fb072024-02-20 19:08:08 -0800228 return gAudioFlingerServiceHandler.getService(false /* canStartThreadPool */);
Akshata Kadam1d486b72023-08-07 05:49:48 +0000229}
230
Andy Hungda1fb072024-02-20 19:08:08 -0800231sp<AudioSystem::AudioFlingerClient> AudioSystem::getAudioFlingerClient() {
232 return gAudioFlingerServiceHandler.getClient();
233}
234
235void AudioSystem::setAudioFlingerBinder(const sp<IBinder>& audioFlinger) {
236 if (audioFlinger->getInterfaceDescriptor() != media::IAudioFlingerService::descriptor) {
237 ALOGE("%s: received a binder of type %s",
238 __func__, String8(audioFlinger->getInterfaceDescriptor()).c_str());
239 return;
240 }
241 gAudioFlingerServiceHandler.setBinder(audioFlinger);
242}
243
244status_t AudioSystem::setLocalAudioFlinger(const sp<IAudioFlinger>& af) {
245 return gAudioFlingerServiceHandler.setLocalService(af);
Eric Laurent296fb132015-05-01 11:38:42 -0700246}
247
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800248sp<AudioIoDescriptor> AudioSystem::getIoDescriptor(audio_io_handle_t ioHandle) {
Eric Laurent296fb132015-05-01 11:38:42 -0700249 sp<AudioIoDescriptor> desc;
250 const sp<AudioFlingerClient> afc = getAudioFlingerClient();
251 if (afc != 0) {
252 desc = afc->getIoDescriptor(ioHandle);
253 }
254 return desc;
255}
256
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800257/* static */ status_t AudioSystem::checkAudioFlinger() {
Eric Laurent46291612013-07-18 14:38:44 -0700258 if (defaultServiceManager()->checkService(String16("media.audio_flinger")) != 0) {
259 return NO_ERROR;
260 }
261 return DEAD_OBJECT;
262}
263
Glenn Kasten4a8308b2016-04-18 14:10:01 -0700264// FIXME Declare in binder opcode order, similarly to IAudioFlinger.h and IAudioFlinger.cpp
265
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800266status_t AudioSystem::muteMicrophone(bool state) {
Andy Hung264fa4c2024-02-21 15:52:12 -0800267 const sp<IAudioFlinger> af = get_audio_flinger();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800268 if (af == 0) return PERMISSION_DENIED;
269 return af->setMicMute(state);
270}
271
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800272status_t AudioSystem::isMicrophoneMuted(bool* state) {
Andy Hung264fa4c2024-02-21 15:52:12 -0800273 const sp<IAudioFlinger> af = get_audio_flinger();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800274 if (af == 0) return PERMISSION_DENIED;
275 *state = af->getMicMute();
276 return NO_ERROR;
277}
278
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800279status_t AudioSystem::setMasterVolume(float value) {
Andy Hung264fa4c2024-02-21 15:52:12 -0800280 const sp<IAudioFlinger> af = get_audio_flinger();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800281 if (af == 0) return PERMISSION_DENIED;
282 af->setMasterVolume(value);
283 return NO_ERROR;
284}
285
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800286status_t AudioSystem::setMasterMute(bool mute) {
Andy Hung264fa4c2024-02-21 15:52:12 -0800287 const sp<IAudioFlinger> af = get_audio_flinger();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800288 if (af == 0) return PERMISSION_DENIED;
289 af->setMasterMute(mute);
290 return NO_ERROR;
291}
292
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800293status_t AudioSystem::getMasterVolume(float* volume) {
Andy Hung264fa4c2024-02-21 15:52:12 -0800294 const sp<IAudioFlinger> af = get_audio_flinger();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800295 if (af == 0) return PERMISSION_DENIED;
296 *volume = af->masterVolume();
297 return NO_ERROR;
298}
299
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800300status_t AudioSystem::getMasterMute(bool* mute) {
Andy Hung264fa4c2024-02-21 15:52:12 -0800301 const sp<IAudioFlinger> af = get_audio_flinger();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800302 if (af == 0) return PERMISSION_DENIED;
303 *mute = af->masterMute();
304 return NO_ERROR;
305}
306
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800307status_t AudioSystem::setStreamVolume(audio_stream_type_t stream, float value,
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800308 audio_io_handle_t output) {
Dima Zavinfce7a472011-04-19 22:30:36 -0700309 if (uint32_t(stream) >= AUDIO_STREAM_CNT) return BAD_VALUE;
Andy Hung264fa4c2024-02-21 15:52:12 -0800310 const sp<IAudioFlinger> af = get_audio_flinger();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800311 if (af == 0) return PERMISSION_DENIED;
Eric Laurentc2f1f072009-07-17 12:17:14 -0700312 af->setStreamVolume(stream, value, output);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800313 return NO_ERROR;
314}
315
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800316status_t AudioSystem::setStreamMute(audio_stream_type_t stream, bool mute) {
Dima Zavinfce7a472011-04-19 22:30:36 -0700317 if (uint32_t(stream) >= AUDIO_STREAM_CNT) return BAD_VALUE;
Andy Hung264fa4c2024-02-21 15:52:12 -0800318 const sp<IAudioFlinger> af = get_audio_flinger();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800319 if (af == 0) return PERMISSION_DENIED;
320 af->setStreamMute(stream, mute);
321 return NO_ERROR;
322}
323
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800324status_t AudioSystem::getStreamVolume(audio_stream_type_t stream, float* volume,
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800325 audio_io_handle_t output) {
Dima Zavinfce7a472011-04-19 22:30:36 -0700326 if (uint32_t(stream) >= AUDIO_STREAM_CNT) return BAD_VALUE;
Andy Hung264fa4c2024-02-21 15:52:12 -0800327 const sp<IAudioFlinger> af = get_audio_flinger();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800328 if (af == 0) return PERMISSION_DENIED;
Eric Laurentc2f1f072009-07-17 12:17:14 -0700329 *volume = af->streamVolume(stream, output);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800330 return NO_ERROR;
331}
332
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800333status_t AudioSystem::getStreamMute(audio_stream_type_t stream, bool* mute) {
Dima Zavinfce7a472011-04-19 22:30:36 -0700334 if (uint32_t(stream) >= AUDIO_STREAM_CNT) return BAD_VALUE;
Andy Hung264fa4c2024-02-21 15:52:12 -0800335 const sp<IAudioFlinger> af = get_audio_flinger();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800336 if (af == 0) return PERMISSION_DENIED;
337 *mute = af->streamMute(stream);
338 return NO_ERROR;
339}
340
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800341status_t AudioSystem::setMode(audio_mode_t mode) {
Glenn Kasten930f4ca2012-01-06 16:47:31 -0800342 if (uint32_t(mode) >= AUDIO_MODE_CNT) return BAD_VALUE;
Andy Hung264fa4c2024-02-21 15:52:12 -0800343 const sp<IAudioFlinger> af = get_audio_flinger();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800344 if (af == 0) return PERMISSION_DENIED;
345 return af->setMode(mode);
346}
347
Mikhail Naganovb1ddbb02023-03-15 17:06:59 -0700348status_t AudioSystem::setSimulateDeviceConnections(bool enabled) {
Andy Hung264fa4c2024-02-21 15:52:12 -0800349 const sp<IAudioFlinger> af = get_audio_flinger();
Mikhail Naganovb1ddbb02023-03-15 17:06:59 -0700350 if (af == 0) return PERMISSION_DENIED;
351 return af->setSimulateDeviceConnections(enabled);
352}
353
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800354status_t AudioSystem::setParameters(audio_io_handle_t ioHandle, const String8& keyValuePairs) {
Andy Hung264fa4c2024-02-21 15:52:12 -0800355 const sp<IAudioFlinger> af = get_audio_flinger();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800356 if (af == 0) return PERMISSION_DENIED;
Eric Laurentc2f1f072009-07-17 12:17:14 -0700357 return af->setParameters(ioHandle, keyValuePairs);
358}
359
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800360String8 AudioSystem::getParameters(audio_io_handle_t ioHandle, const String8& keys) {
Andy Hung264fa4c2024-02-21 15:52:12 -0800361 const sp<IAudioFlinger> af = get_audio_flinger();
Eric Laurentc2f1f072009-07-17 12:17:14 -0700362 String8 result = String8("");
363 if (af == 0) return result;
364
365 result = af->getParameters(ioHandle, keys);
366 return result;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800367}
368
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800369status_t AudioSystem::setParameters(const String8& keyValuePairs) {
Glenn Kasten142f5192014-03-25 17:44:59 -0700370 return setParameters(AUDIO_IO_HANDLE_NONE, keyValuePairs);
Glenn Kastenc23885e2013-12-19 16:35:18 -0800371}
372
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800373String8 AudioSystem::getParameters(const String8& keys) {
Glenn Kasten142f5192014-03-25 17:44:59 -0700374 return getParameters(AUDIO_IO_HANDLE_NONE, keys);
Glenn Kastenc23885e2013-12-19 16:35:18 -0800375}
376
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800377// convert volume steps to natural log scale
378
379// change this value to change volume scaling
Andy Hungacd955a2024-02-16 17:14:36 -0800380constexpr float kdbPerStep = 0.5f;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800381// shouldn't need to touch these
Andy Hungacd955a2024-02-16 17:14:36 -0800382constexpr float kdBConvert = -kdbPerStep * 2.302585093f / 20.0f;
383constexpr float kdBConvertInverse = 1.0f / kdBConvert;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800384
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800385float AudioSystem::linearToLog(int volume) {
Andy Hungacd955a2024-02-16 17:14:36 -0800386 // float v = volume ? exp(float(100 - volume) * kdBConvert) : 0;
Steve Blockb8a80522011-12-20 16:23:08 +0000387 // ALOGD("linearToLog(%d)=%f", volume, v);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800388 // return v;
Andy Hungacd955a2024-02-16 17:14:36 -0800389 return volume ? exp(float(100 - volume) * kdBConvert) : 0;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800390}
391
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800392int AudioSystem::logToLinear(float volume) {
Andy Hungacd955a2024-02-16 17:14:36 -0800393 // int v = volume ? 100 - int(kdBConvertInverse * log(volume) + 0.5) : 0;
Steve Blockb8a80522011-12-20 16:23:08 +0000394 // ALOGD("logTolinear(%d)=%f", v, volume);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800395 // return v;
Andy Hungacd955a2024-02-16 17:14:36 -0800396 return volume ? 100 - int(kdBConvertInverse * log(volume) + 0.5) : 0;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800397}
398
Eric Laurent21da6472017-11-09 16:29:26 -0800399/* static */ size_t AudioSystem::calculateMinFrameCount(
400 uint32_t afLatencyMs, uint32_t afFrameCount, uint32_t afSampleRate,
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800401 uint32_t sampleRate, float speed /*, uint32_t notificationsPerBufferReq*/) {
Eric Laurent21da6472017-11-09 16:29:26 -0800402 // Ensure that buffer depth covers at least audio hardware latency
403 uint32_t minBufCount = afLatencyMs / ((1000 * afFrameCount) / afSampleRate);
404 if (minBufCount < 2) {
405 minBufCount = 2;
406 }
407#if 0
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800408 // The notificationsPerBufferReq parameter is not yet used for non-fast tracks,
409 // but keeping the code here to make it easier to add later.
410 if (minBufCount < notificationsPerBufferReq) {
411 minBufCount = notificationsPerBufferReq;
412 }
Eric Laurent21da6472017-11-09 16:29:26 -0800413#endif
414 ALOGV("calculateMinFrameCount afLatency %u afFrameCount %u afSampleRate %u "
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800415 "sampleRate %u speed %f minBufCount: %u" /*" notificationsPerBufferReq %u"*/,
416 afLatencyMs, afFrameCount, afSampleRate, sampleRate, speed, minBufCount
417 /*, notificationsPerBufferReq*/);
Eric Laurent21da6472017-11-09 16:29:26 -0800418 return minBufCount * sourceFramesNeededWithTimestretch(
419 sampleRate, afFrameCount, afSampleRate, speed);
420}
421
422
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800423status_t
424AudioSystem::getOutputSamplingRate(uint32_t* samplingRate, audio_stream_type_t streamType) {
Eric Laurentc2f1f072009-07-17 12:17:14 -0700425 audio_io_handle_t output;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800426
Dima Zavinfce7a472011-04-19 22:30:36 -0700427 if (streamType == AUDIO_STREAM_DEFAULT) {
428 streamType = AUDIO_STREAM_MUSIC;
Eric Laurentc2f1f072009-07-17 12:17:14 -0700429 }
430
Glenn Kastenfff6d712012-01-12 16:38:12 -0800431 output = getOutput(streamType);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700432 if (output == 0) {
433 return PERMISSION_DENIED;
434 }
435
Jean-Michel Trivib7f24b12014-06-11 10:05:30 -0700436 return getSamplingRate(output, samplingRate);
Eric Laurent1a9ed112012-03-20 18:36:01 -0700437}
438
Glenn Kasten2c073da2016-02-26 09:14:08 -0800439status_t AudioSystem::getSamplingRate(audio_io_handle_t ioHandle,
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800440 uint32_t* samplingRate) {
Andy Hung264fa4c2024-02-21 15:52:12 -0800441 const sp<IAudioFlinger> af = get_audio_flinger();
Eric Laurentf6778fd2014-11-18 17:26:58 -0800442 if (af == 0) return PERMISSION_DENIED;
Glenn Kasten2c073da2016-02-26 09:14:08 -0800443 sp<AudioIoDescriptor> desc = getIoDescriptor(ioHandle);
444 if (desc == 0) {
445 *samplingRate = af->sampleRate(ioHandle);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700446 } else {
Mikhail Naganov88536df2021-07-26 17:30:29 -0700447 *samplingRate = desc->getSamplingRate();
Eric Laurentc2f1f072009-07-17 12:17:14 -0700448 }
Glenn Kastenf94006c2014-01-08 08:56:06 -0800449 if (*samplingRate == 0) {
Glenn Kasten2c073da2016-02-26 09:14:08 -0800450 ALOGE("AudioSystem::getSamplingRate failed for ioHandle %d", ioHandle);
Glenn Kastenf94006c2014-01-08 08:56:06 -0800451 return BAD_VALUE;
452 }
Eric Laurentc2f1f072009-07-17 12:17:14 -0700453
Glenn Kasten2c073da2016-02-26 09:14:08 -0800454 ALOGV("getSamplingRate() ioHandle %d, sampling rate %u", ioHandle, *samplingRate);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700455
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800456 return NO_ERROR;
457}
458
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800459status_t AudioSystem::getOutputFrameCount(size_t* frameCount, audio_stream_type_t streamType) {
Eric Laurentc2f1f072009-07-17 12:17:14 -0700460 audio_io_handle_t output;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800461
Dima Zavinfce7a472011-04-19 22:30:36 -0700462 if (streamType == AUDIO_STREAM_DEFAULT) {
463 streamType = AUDIO_STREAM_MUSIC;
Eric Laurentc2f1f072009-07-17 12:17:14 -0700464 }
Eric Laurent48f7f5e2009-04-02 09:32:43 -0700465
Glenn Kastenfff6d712012-01-12 16:38:12 -0800466 output = getOutput(streamType);
Glenn Kasten142f5192014-03-25 17:44:59 -0700467 if (output == AUDIO_IO_HANDLE_NONE) {
Eric Laurentc2f1f072009-07-17 12:17:14 -0700468 return PERMISSION_DENIED;
469 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800470
Jean-Michel Trivib7f24b12014-06-11 10:05:30 -0700471 return getFrameCount(output, frameCount);
Eric Laurent1a9ed112012-03-20 18:36:01 -0700472}
473
Glenn Kasten2c073da2016-02-26 09:14:08 -0800474status_t AudioSystem::getFrameCount(audio_io_handle_t ioHandle,
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800475 size_t* frameCount) {
Andy Hung264fa4c2024-02-21 15:52:12 -0800476 const sp<IAudioFlinger> af = get_audio_flinger();
Eric Laurentf6778fd2014-11-18 17:26:58 -0800477 if (af == 0) return PERMISSION_DENIED;
Glenn Kasten2c073da2016-02-26 09:14:08 -0800478 sp<AudioIoDescriptor> desc = getIoDescriptor(ioHandle);
479 if (desc == 0) {
480 *frameCount = af->frameCount(ioHandle);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700481 } else {
Mikhail Naganov88536df2021-07-26 17:30:29 -0700482 *frameCount = desc->getFrameCount();
Eric Laurentc2f1f072009-07-17 12:17:14 -0700483 }
Glenn Kastenf94006c2014-01-08 08:56:06 -0800484 if (*frameCount == 0) {
Glenn Kasten2c073da2016-02-26 09:14:08 -0800485 ALOGE("AudioSystem::getFrameCount failed for ioHandle %d", ioHandle);
Glenn Kastenf94006c2014-01-08 08:56:06 -0800486 return BAD_VALUE;
487 }
Eric Laurentc2f1f072009-07-17 12:17:14 -0700488
Glenn Kasten2c073da2016-02-26 09:14:08 -0800489 ALOGV("getFrameCount() ioHandle %d, frameCount %zu", ioHandle, *frameCount);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700490
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800491 return NO_ERROR;
492}
493
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800494status_t AudioSystem::getOutputLatency(uint32_t* latency, audio_stream_type_t streamType) {
Eric Laurentc2f1f072009-07-17 12:17:14 -0700495 audio_io_handle_t output;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800496
Dima Zavinfce7a472011-04-19 22:30:36 -0700497 if (streamType == AUDIO_STREAM_DEFAULT) {
498 streamType = AUDIO_STREAM_MUSIC;
Eric Laurentc2f1f072009-07-17 12:17:14 -0700499 }
Eric Laurent48f7f5e2009-04-02 09:32:43 -0700500
Glenn Kastenfff6d712012-01-12 16:38:12 -0800501 output = getOutput(streamType);
Glenn Kasten142f5192014-03-25 17:44:59 -0700502 if (output == AUDIO_IO_HANDLE_NONE) {
Eric Laurentc2f1f072009-07-17 12:17:14 -0700503 return PERMISSION_DENIED;
504 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800505
Glenn Kasten241618f2014-03-25 17:48:57 -0700506 return getLatency(output, latency);
Eric Laurent1a9ed112012-03-20 18:36:01 -0700507}
508
509status_t AudioSystem::getLatency(audio_io_handle_t output,
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800510 uint32_t* latency) {
Andy Hung264fa4c2024-02-21 15:52:12 -0800511 const sp<IAudioFlinger> af = get_audio_flinger();
Eric Laurentf6778fd2014-11-18 17:26:58 -0800512 if (af == 0) return PERMISSION_DENIED;
Eric Laurent296fb132015-05-01 11:38:42 -0700513 sp<AudioIoDescriptor> outputDesc = getIoDescriptor(output);
Eric Laurent73e26b62015-04-27 16:55:58 -0700514 if (outputDesc == 0) {
Eric Laurentc2f1f072009-07-17 12:17:14 -0700515 *latency = af->latency(output);
516 } else {
Mikhail Naganov88536df2021-07-26 17:30:29 -0700517 *latency = outputDesc->getLatency();
Eric Laurentc2f1f072009-07-17 12:17:14 -0700518 }
519
Glenn Kasten241618f2014-03-25 17:48:57 -0700520 ALOGV("getLatency() output %d, latency %d", output, *latency);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700521
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800522 return NO_ERROR;
523}
524
Glenn Kastendd8104c2012-07-02 12:42:44 -0700525status_t AudioSystem::getInputBufferSize(uint32_t sampleRate, audio_format_t format,
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800526 audio_channel_mask_t channelMask, size_t* buffSize) {
Eric Laurent296fb132015-05-01 11:38:42 -0700527 const sp<AudioFlingerClient> afc = getAudioFlingerClient();
528 if (afc == 0) {
529 return NO_INIT;
530 }
531 return afc->getInputBufferSize(sampleRate, format, channelMask, buffSize);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800532}
533
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800534status_t AudioSystem::setVoiceVolume(float value) {
Andy Hung264fa4c2024-02-21 15:52:12 -0800535 const sp<IAudioFlinger> af = get_audio_flinger();
Eric Laurentf0ee6f42009-10-21 08:14:22 -0700536 if (af == 0) return PERMISSION_DENIED;
537 return af->setVoiceVolume(value);
538}
539
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800540status_t AudioSystem::getRenderPosition(audio_io_handle_t output, uint32_t* halFrames,
541 uint32_t* dspFrames) {
Andy Hung264fa4c2024-02-21 15:52:12 -0800542 const sp<IAudioFlinger> af = get_audio_flinger();
Eric Laurent342e9cf2010-01-19 17:37:09 -0800543 if (af == 0) return PERMISSION_DENIED;
544
Richard Fitzgeraldad3af332013-03-25 16:54:37 +0000545 return af->getRenderPosition(halFrames, dspFrames, output);
Eric Laurent342e9cf2010-01-19 17:37:09 -0800546}
547
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800548uint32_t AudioSystem::getInputFramesLost(audio_io_handle_t ioHandle) {
Andy Hung264fa4c2024-02-21 15:52:12 -0800549 const sp<IAudioFlinger> af = get_audio_flinger();
Glenn Kasten5f972c02014-01-13 09:59:31 -0800550 uint32_t result = 0;
Eric Laurent05bca2f2010-02-26 02:47:27 -0800551 if (af == 0) return result;
Glenn Kasten142f5192014-03-25 17:44:59 -0700552 if (ioHandle == AUDIO_IO_HANDLE_NONE) return result;
Eric Laurent05bca2f2010-02-26 02:47:27 -0800553
554 result = af->getInputFramesLost(ioHandle);
555 return result;
556}
557
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800558audio_unique_id_t AudioSystem::newAudioUniqueId(audio_unique_id_use_t use) {
Mikhail Naganov2996f672019-04-18 12:29:59 -0700559 // Must not use AF as IDs will re-roll on audioserver restart, b/130369529.
Andy Hung264fa4c2024-02-21 15:52:12 -0800560 const sp<IAudioFlinger> af = get_audio_flinger();
Eric Laurentde3f8392014-07-27 18:38:22 -0700561 if (af == 0) return AUDIO_UNIQUE_ID_ALLOCATE;
Glenn Kasteneeecb982016-02-26 10:44:04 -0800562 return af->newAudioUniqueId(use);
Eric Laurentbe916aa2010-06-01 23:49:17 -0700563}
564
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800565void AudioSystem::acquireAudioSessionId(audio_session_t audioSession, pid_t pid, uid_t uid) {
Andy Hung264fa4c2024-02-21 15:52:12 -0800566 const sp<IAudioFlinger> af = get_audio_flinger();
Marco Nelissen3a34bef2011-08-02 13:33:41 -0700567 if (af != 0) {
Andy Hung8b0bfd92019-12-23 13:11:11 -0800568 af->acquireAudioSessionId(audioSession, pid, uid);
Marco Nelissen3a34bef2011-08-02 13:33:41 -0700569 }
570}
571
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800572void AudioSystem::releaseAudioSessionId(audio_session_t audioSession, pid_t pid) {
Andy Hung264fa4c2024-02-21 15:52:12 -0800573 const sp<IAudioFlinger> af = get_audio_flinger();
Marco Nelissen3a34bef2011-08-02 13:33:41 -0700574 if (af != 0) {
Marco Nelissend457c972014-02-11 08:47:07 -0800575 af->releaseAudioSessionId(audioSession, pid);
Marco Nelissen3a34bef2011-08-02 13:33:41 -0700576 }
577}
578
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800579audio_hw_sync_t AudioSystem::getAudioHwSyncForSession(audio_session_t sessionId) {
Andy Hung264fa4c2024-02-21 15:52:12 -0800580 const sp<IAudioFlinger> af = get_audio_flinger();
Eric Laurent93c3d412014-08-01 14:48:35 -0700581 if (af == 0) return AUDIO_HW_SYNC_INVALID;
582 return af->getAudioHwSyncForSession(sessionId);
583}
584
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800585status_t AudioSystem::systemReady() {
Andy Hung264fa4c2024-02-21 15:52:12 -0800586 const sp<IAudioFlinger> af = get_audio_flinger();
Eric Laurent72e3f392015-05-20 14:43:50 -0700587 if (af == 0) return NO_INIT;
588 return af->systemReady();
589}
590
Eric Laurentd66d7a12021-07-13 13:35:32 +0200591status_t AudioSystem::audioPolicyReady() {
Andy Hung264fa4c2024-02-21 15:52:12 -0800592 const sp<IAudioFlinger> af = get_audio_flinger();
Eric Laurentd66d7a12021-07-13 13:35:32 +0200593 if (af == 0) return NO_INIT;
594 return af->audioPolicyReady();
595}
596
Glenn Kasten4a8308b2016-04-18 14:10:01 -0700597status_t AudioSystem::getFrameCountHAL(audio_io_handle_t ioHandle,
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800598 size_t* frameCount) {
Andy Hung264fa4c2024-02-21 15:52:12 -0800599 const sp<IAudioFlinger> af = get_audio_flinger();
Glenn Kasten4a8308b2016-04-18 14:10:01 -0700600 if (af == 0) return PERMISSION_DENIED;
601 sp<AudioIoDescriptor> desc = getIoDescriptor(ioHandle);
602 if (desc == 0) {
603 *frameCount = af->frameCountHAL(ioHandle);
604 } else {
Mikhail Naganov88536df2021-07-26 17:30:29 -0700605 *frameCount = desc->getFrameCountHAL();
Glenn Kasten4a8308b2016-04-18 14:10:01 -0700606 }
607 if (*frameCount == 0) {
608 ALOGE("AudioSystem::getFrameCountHAL failed for ioHandle %d", ioHandle);
609 return BAD_VALUE;
610 }
611
612 ALOGV("getFrameCountHAL() ioHandle %d, frameCount %zu", ioHandle, *frameCount);
613
614 return NO_ERROR;
615}
616
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800617// ---------------------------------------------------------------------------
618
Eric Laurent73e26b62015-04-27 16:55:58 -0700619
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800620void AudioSystem::AudioFlingerClient::clearIoCache() {
Andy Hungacd955a2024-02-16 17:14:36 -0800621 std::lock_guard _l(mMutex);
Eric Laurent73e26b62015-04-27 16:55:58 -0700622 mIoDescriptors.clear();
623 mInBuffSize = 0;
624 mInSamplingRate = 0;
625 mInFormat = AUDIO_FORMAT_DEFAULT;
626 mInChannelMask = AUDIO_CHANNEL_NONE;
627}
628
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800629void AudioSystem::AudioFlingerClient::binderDied(const wp<IBinder>& who __unused) {
Andy Hungda1fb072024-02-20 19:08:08 -0800630 gAudioFlingerServiceHandler.clearService();
Ytai Ben-Tsvi000c3e42020-01-09 15:26:40 -0800631 reportError(DEAD_OBJECT);
632
Steve Block5ff1dd52012-01-05 23:22:43 +0000633 ALOGW("AudioFlinger server died!");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800634}
635
Ytai Ben-Tsvi10dc0a62020-09-18 11:31:55 -0700636Status AudioSystem::AudioFlingerClient::ioConfigChanged(
637 media::AudioIoConfigEvent _event,
638 const media::AudioIoDescriptor& _ioDesc) {
Mikhail Naganov88536df2021-07-26 17:30:29 -0700639 audio_io_config_event_t event = VALUE_OR_RETURN_BINDER_STATUS(
640 aidl2legacy_AudioIoConfigEvent_audio_io_config_event_t(_event));
Ytai Ben-Tsvi10dc0a62020-09-18 11:31:55 -0700641 sp<AudioIoDescriptor> ioDesc(
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800642 VALUE_OR_RETURN_BINDER_STATUS(
643 aidl2legacy_AudioIoDescriptor_AudioIoDescriptor(_ioDesc)));
Ytai Ben-Tsvi10dc0a62020-09-18 11:31:55 -0700644
Steve Block3856b092011-10-20 11:56:00 +0100645 ALOGV("ioConfigChanged() event %d", event);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700646
Mikhail Naganov88536df2021-07-26 17:30:29 -0700647 if (ioDesc->getIoHandle() == AUDIO_IO_HANDLE_NONE) return Status::ok();
Eric Laurentc2f1f072009-07-17 12:17:14 -0700648
Eric Laurent296fb132015-05-01 11:38:42 -0700649 audio_port_handle_t deviceId = AUDIO_PORT_HANDLE_NONE;
Eric Laurent09f1ed22019-04-24 17:45:17 -0700650 std::vector<sp<AudioDeviceCallback>> callbacksToCall;
Eric Laurent296fb132015-05-01 11:38:42 -0700651 {
Andy Hungacd955a2024-02-16 17:14:36 -0800652 std::lock_guard _l(mMutex);
Eric Laurent09f1ed22019-04-24 17:45:17 -0700653 auto callbacks = std::map<audio_port_handle_t, wp<AudioDeviceCallback>>();
Eric Laurent296fb132015-05-01 11:38:42 -0700654
655 switch (event) {
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800656 case AUDIO_OUTPUT_OPENED:
657 case AUDIO_OUTPUT_REGISTERED:
658 case AUDIO_INPUT_OPENED:
659 case AUDIO_INPUT_REGISTERED: {
Andy Hungd211ce02024-02-16 15:34:07 -0800660 if (sp<AudioIoDescriptor> oldDesc = getIoDescriptor_l(ioDesc->getIoHandle())) {
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800661 deviceId = oldDesc->getDeviceId();
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800662 }
Andy Hungd211ce02024-02-16 15:34:07 -0800663 mIoDescriptors[ioDesc->getIoHandle()] = ioDesc;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800664
665 if (ioDesc->getDeviceId() != AUDIO_PORT_HANDLE_NONE) {
666 deviceId = ioDesc->getDeviceId();
667 if (event == AUDIO_OUTPUT_OPENED || event == AUDIO_INPUT_OPENED) {
Mikhail Naganov88536df2021-07-26 17:30:29 -0700668 auto it = mAudioDeviceCallbacks.find(ioDesc->getIoHandle());
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800669 if (it != mAudioDeviceCallbacks.end()) {
670 callbacks = it->second;
671 }
672 }
673 }
Mikhail Naganov88536df2021-07-26 17:30:29 -0700674 ALOGV("ioConfigChanged() new %s %s %s",
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800675 event == AUDIO_OUTPUT_OPENED || event == AUDIO_OUTPUT_REGISTERED ?
676 "output" : "input",
677 event == AUDIO_OUTPUT_OPENED || event == AUDIO_INPUT_OPENED ?
678 "opened" : "registered",
Mikhail Naganov88536df2021-07-26 17:30:29 -0700679 ioDesc->toDebugString().c_str());
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800680 }
681 break;
682 case AUDIO_OUTPUT_CLOSED:
683 case AUDIO_INPUT_CLOSED: {
Mikhail Naganov88536df2021-07-26 17:30:29 -0700684 if (getIoDescriptor_l(ioDesc->getIoHandle()) == 0) {
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800685 ALOGW("ioConfigChanged() closing unknown %s %d",
Mikhail Naganov88536df2021-07-26 17:30:29 -0700686 event == AUDIO_OUTPUT_CLOSED ? "output" : "input", ioDesc->getIoHandle());
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800687 break;
688 }
689 ALOGV("ioConfigChanged() %s %d closed",
Mikhail Naganov88536df2021-07-26 17:30:29 -0700690 event == AUDIO_OUTPUT_CLOSED ? "output" : "input", ioDesc->getIoHandle());
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800691
Andy Hungd211ce02024-02-16 15:34:07 -0800692 mIoDescriptors.erase(ioDesc->getIoHandle());
Mikhail Naganov88536df2021-07-26 17:30:29 -0700693 mAudioDeviceCallbacks.erase(ioDesc->getIoHandle());
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800694 }
695 break;
696
697 case AUDIO_OUTPUT_CONFIG_CHANGED:
698 case AUDIO_INPUT_CONFIG_CHANGED: {
Mikhail Naganov88536df2021-07-26 17:30:29 -0700699 sp<AudioIoDescriptor> oldDesc = getIoDescriptor_l(ioDesc->getIoHandle());
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800700 if (oldDesc == 0) {
701 ALOGW("ioConfigChanged() modifying unknown %s! %d",
702 event == AUDIO_OUTPUT_CONFIG_CHANGED ? "output" : "input",
Mikhail Naganov88536df2021-07-26 17:30:29 -0700703 ioDesc->getIoHandle());
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800704 break;
705 }
706
Eric Laurent7c1ec5f2015-07-09 14:52:47 -0700707 deviceId = oldDesc->getDeviceId();
Andy Hungd211ce02024-02-16 15:34:07 -0800708 mIoDescriptors[ioDesc->getIoHandle()] = ioDesc;
Eric Laurent296fb132015-05-01 11:38:42 -0700709
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800710 if (deviceId != ioDesc->getDeviceId()) {
711 deviceId = ioDesc->getDeviceId();
Mikhail Naganov88536df2021-07-26 17:30:29 -0700712 auto it = mAudioDeviceCallbacks.find(ioDesc->getIoHandle());
Eric Laurent09f1ed22019-04-24 17:45:17 -0700713 if (it != mAudioDeviceCallbacks.end()) {
714 callbacks = it->second;
Francois Gaffie24a9fb02019-01-18 17:51:34 +0100715 }
716 }
Mikhail Naganov88536df2021-07-26 17:30:29 -0700717 ALOGV("ioConfigChanged() new config for %s %s",
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800718 event == AUDIO_OUTPUT_CONFIG_CHANGED ? "output" : "input",
Mikhail Naganov88536df2021-07-26 17:30:29 -0700719 ioDesc->toDebugString().c_str());
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800720
Eric Laurent296fb132015-05-01 11:38:42 -0700721 }
Eric Laurent296fb132015-05-01 11:38:42 -0700722 break;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800723 case AUDIO_CLIENT_STARTED: {
Mikhail Naganov88536df2021-07-26 17:30:29 -0700724 sp<AudioIoDescriptor> oldDesc = getIoDescriptor_l(ioDesc->getIoHandle());
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800725 if (oldDesc == 0) {
Mikhail Naganov88536df2021-07-26 17:30:29 -0700726 ALOGW("ioConfigChanged() start client on unknown io! %d",
727 ioDesc->getIoHandle());
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800728 break;
729 }
730 ALOGV("ioConfigChanged() AUDIO_CLIENT_STARTED io %d port %d num callbacks %zu",
Mikhail Naganov88536df2021-07-26 17:30:29 -0700731 ioDesc->getIoHandle(), ioDesc->getPortId(), mAudioDeviceCallbacks.size());
732 oldDesc->setPatch(ioDesc->getPatch());
733 auto it = mAudioDeviceCallbacks.find(ioDesc->getIoHandle());
Eric Laurent09f1ed22019-04-24 17:45:17 -0700734 if (it != mAudioDeviceCallbacks.end()) {
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800735 auto cbks = it->second;
Mikhail Naganov88536df2021-07-26 17:30:29 -0700736 auto it2 = cbks.find(ioDesc->getPortId());
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800737 if (it2 != cbks.end()) {
Mikhail Naganov88536df2021-07-26 17:30:29 -0700738 callbacks.emplace(ioDesc->getPortId(), it2->second);
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800739 deviceId = oldDesc->getDeviceId();
740 }
Eric Laurent09f1ed22019-04-24 17:45:17 -0700741 }
Eric Laurent296fb132015-05-01 11:38:42 -0700742 }
Eric Laurent09f1ed22019-04-24 17:45:17 -0700743 break;
Eric Laurent09f1ed22019-04-24 17:45:17 -0700744 }
745
746 for (auto wpCbk : callbacks) {
747 sp<AudioDeviceCallback> spCbk = wpCbk.second.promote();
748 if (spCbk != nullptr) {
749 callbacksToCall.push_back(spCbk);
750 }
Eric Laurentad2e7b92017-09-14 20:06:42 -0700751 }
Eric Laurent4463ff52019-02-07 13:56:09 -0800752 }
753
Andy Hungacd955a2024-02-16 17:14:36 -0800754 // Callbacks must be called without mMutex held. May lead to dead lock if calling for
755 // example getRoutedDevice that updates the device and tries to acquire mMutex.
Eric Laurent09f1ed22019-04-24 17:45:17 -0700756 for (auto cb : callbacksToCall) {
Mikhail Naganov88536df2021-07-26 17:30:29 -0700757 // If callbacksToCall is not empty, it implies ioDesc->getIoHandle() and deviceId are valid
758 cb->onAudioDeviceUpdate(ioDesc->getIoHandle(), deviceId);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700759 }
Ytai Ben-Tsvi10dc0a62020-09-18 11:31:55 -0700760
761 return Status::ok();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800762}
763
Eric Laurent076e7c72022-05-03 18:12:28 +0200764Status AudioSystem::AudioFlingerClient::onSupportedLatencyModesChanged(
Mikhail Naganovf53e1822022-12-18 02:48:14 +0000765 int output, const std::vector<media::audio::common::AudioLatencyMode>& latencyModes) {
Eric Laurent076e7c72022-05-03 18:12:28 +0200766 audio_io_handle_t outputLegacy = VALUE_OR_RETURN_BINDER_STATUS(
767 aidl2legacy_int32_t_audio_io_handle_t(output));
768 std::vector<audio_latency_mode_t> modesLegacy = VALUE_OR_RETURN_BINDER_STATUS(
769 convertContainer<std::vector<audio_latency_mode_t>>(
Mikhail Naganovf53e1822022-12-18 02:48:14 +0000770 latencyModes, aidl2legacy_AudioLatencyMode_audio_latency_mode_t));
Eric Laurent076e7c72022-05-03 18:12:28 +0200771
772 std::vector<sp<SupportedLatencyModesCallback>> callbacks;
773 {
Andy Hungacd955a2024-02-16 17:14:36 -0800774 std::lock_guard _l(mMutex);
Eric Laurent076e7c72022-05-03 18:12:28 +0200775 for (auto callback : mSupportedLatencyModesCallbacks) {
776 if (auto ref = callback.promote(); ref != nullptr) {
777 callbacks.push_back(ref);
778 }
779 }
780 }
781 for (const auto& callback : callbacks) {
782 callback->onSupportedLatencyModesChanged(outputLegacy, modesLegacy);
783 }
784
785 return Status::ok();
786}
787
Eric Laurent73e26b62015-04-27 16:55:58 -0700788status_t AudioSystem::AudioFlingerClient::getInputBufferSize(
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800789 uint32_t sampleRate, audio_format_t format,
790 audio_channel_mask_t channelMask, size_t* buffSize) {
Andy Hung264fa4c2024-02-21 15:52:12 -0800791 const sp<IAudioFlinger> af = get_audio_flinger();
Eric Laurent73e26b62015-04-27 16:55:58 -0700792 if (af == 0) {
793 return PERMISSION_DENIED;
794 }
Andy Hungacd955a2024-02-16 17:14:36 -0800795 std::lock_guard _l(mMutex);
Eric Laurent73e26b62015-04-27 16:55:58 -0700796 // Do we have a stale mInBuffSize or are we requesting the input buffer size for new values
797 if ((mInBuffSize == 0) || (sampleRate != mInSamplingRate) || (format != mInFormat)
798 || (channelMask != mInChannelMask)) {
799 size_t inBuffSize = af->getInputBufferSize(sampleRate, format, channelMask);
800 if (inBuffSize == 0) {
Glenn Kasten49f36ba2017-12-06 13:02:02 -0800801 ALOGE("AudioSystem::getInputBufferSize failed sampleRate %d format %#x channelMask %#x",
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800802 sampleRate, format, channelMask);
Eric Laurent73e26b62015-04-27 16:55:58 -0700803 return BAD_VALUE;
804 }
805 // A benign race is possible here: we could overwrite a fresher cache entry
806 // save the request params
807 mInSamplingRate = sampleRate;
808 mInFormat = format;
809 mInChannelMask = channelMask;
810
811 mInBuffSize = inBuffSize;
812 }
813
814 *buffSize = mInBuffSize;
815
816 return NO_ERROR;
817}
818
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800819sp<AudioIoDescriptor>
820AudioSystem::AudioFlingerClient::getIoDescriptor_l(audio_io_handle_t ioHandle) {
Andy Hungd211ce02024-02-16 15:34:07 -0800821 if (const auto it = mIoDescriptors.find(ioHandle);
822 it != mIoDescriptors.end()) {
823 return it->second;
Eric Laurent73e26b62015-04-27 16:55:58 -0700824 }
Andy Hungd211ce02024-02-16 15:34:07 -0800825 return {};
Eric Laurent73e26b62015-04-27 16:55:58 -0700826}
827
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800828sp<AudioIoDescriptor> AudioSystem::AudioFlingerClient::getIoDescriptor(audio_io_handle_t ioHandle) {
Andy Hungacd955a2024-02-16 17:14:36 -0800829 std::lock_guard _l(mMutex);
Praveen Chavan49fdeaf2015-09-29 02:25:47 -0700830 return getIoDescriptor_l(ioHandle);
831}
832
Eric Laurent296fb132015-05-01 11:38:42 -0700833status_t AudioSystem::AudioFlingerClient::addAudioDeviceCallback(
Eric Laurent09f1ed22019-04-24 17:45:17 -0700834 const wp<AudioDeviceCallback>& callback, audio_io_handle_t audioIo,
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800835 audio_port_handle_t portId) {
Eric Laurent09f1ed22019-04-24 17:45:17 -0700836 ALOGV("%s audioIo %d portId %d", __func__, audioIo, portId);
Andy Hungacd955a2024-02-16 17:14:36 -0800837 std::lock_guard _l(mMutex);
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800838 auto& callbacks = mAudioDeviceCallbacks.emplace(
839 audioIo,
840 std::map<audio_port_handle_t, wp<AudioDeviceCallback>>()).first->second;
Eric Laurent09f1ed22019-04-24 17:45:17 -0700841 auto result = callbacks.try_emplace(portId, callback);
842 if (!result.second) {
843 return INVALID_OPERATION;
Eric Laurent296fb132015-05-01 11:38:42 -0700844 }
Eric Laurent296fb132015-05-01 11:38:42 -0700845 return NO_ERROR;
846}
847
848status_t AudioSystem::AudioFlingerClient::removeAudioDeviceCallback(
Eric Laurent09f1ed22019-04-24 17:45:17 -0700849 const wp<AudioDeviceCallback>& callback __unused, audio_io_handle_t audioIo,
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800850 audio_port_handle_t portId) {
Eric Laurent09f1ed22019-04-24 17:45:17 -0700851 ALOGV("%s audioIo %d portId %d", __func__, audioIo, portId);
Andy Hungacd955a2024-02-16 17:14:36 -0800852 std::lock_guard _l(mMutex);
Eric Laurent09f1ed22019-04-24 17:45:17 -0700853 auto it = mAudioDeviceCallbacks.find(audioIo);
854 if (it == mAudioDeviceCallbacks.end()) {
Eric Laurent296fb132015-05-01 11:38:42 -0700855 return INVALID_OPERATION;
856 }
Eric Laurent09f1ed22019-04-24 17:45:17 -0700857 if (it->second.erase(portId) == 0) {
Eric Laurent296fb132015-05-01 11:38:42 -0700858 return INVALID_OPERATION;
859 }
Eric Laurent09f1ed22019-04-24 17:45:17 -0700860 if (it->second.size() == 0) {
861 mAudioDeviceCallbacks.erase(audioIo);
Eric Laurent296fb132015-05-01 11:38:42 -0700862 }
863 return NO_ERROR;
864}
865
Eric Laurent076e7c72022-05-03 18:12:28 +0200866status_t AudioSystem::AudioFlingerClient::addSupportedLatencyModesCallback(
867 const sp<SupportedLatencyModesCallback>& callback) {
Andy Hungacd955a2024-02-16 17:14:36 -0800868 std::lock_guard _l(mMutex);
Eric Laurent076e7c72022-05-03 18:12:28 +0200869 if (std::find(mSupportedLatencyModesCallbacks.begin(),
870 mSupportedLatencyModesCallbacks.end(),
871 callback) != mSupportedLatencyModesCallbacks.end()) {
872 return INVALID_OPERATION;
873 }
874 mSupportedLatencyModesCallbacks.push_back(callback);
875 return NO_ERROR;
876}
877
878status_t AudioSystem::AudioFlingerClient::removeSupportedLatencyModesCallback(
879 const sp<SupportedLatencyModesCallback>& callback) {
Andy Hungacd955a2024-02-16 17:14:36 -0800880 std::lock_guard _l(mMutex);
Eric Laurent076e7c72022-05-03 18:12:28 +0200881 auto it = std::find(mSupportedLatencyModesCallbacks.begin(),
882 mSupportedLatencyModesCallbacks.end(),
883 callback);
884 if (it == mSupportedLatencyModesCallbacks.end()) {
885 return INVALID_OPERATION;
886 }
887 mSupportedLatencyModesCallbacks.erase(it);
888 return NO_ERROR;
889}
890
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800891/* static */ uintptr_t AudioSystem::addErrorCallback(audio_error_callback cb) {
Andy Hungacd955a2024-02-16 17:14:36 -0800892 std::lock_guard _l(gErrorCallbacksMutex);
Ytai Ben-Tsvi000c3e42020-01-09 15:26:40 -0800893 gAudioErrorCallbacks.insert(cb);
894 return reinterpret_cast<uintptr_t>(cb);
895}
896
897/* static */ void AudioSystem::removeErrorCallback(uintptr_t cb) {
Andy Hungacd955a2024-02-16 17:14:36 -0800898 std::lock_guard _l(gErrorCallbacksMutex);
Ytai Ben-Tsvi000c3e42020-01-09 15:26:40 -0800899 gAudioErrorCallbacks.erase(reinterpret_cast<audio_error_callback>(cb));
900}
901
902/* static */ void AudioSystem::reportError(status_t err) {
Andy Hungacd955a2024-02-16 17:14:36 -0800903 std::lock_guard _l(gErrorCallbacksMutex);
Ytai Ben-Tsvi000c3e42020-01-09 15:26:40 -0800904 for (auto callback : gAudioErrorCallbacks) {
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800905 callback(err);
Ytai Ben-Tsvi000c3e42020-01-09 15:26:40 -0800906 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800907}
908
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800909/*static*/ void AudioSystem::setDynPolicyCallback(dynamic_policy_callback cb) {
Andy Hungacd955a2024-02-16 17:14:36 -0800910 std::lock_guard _l(gMutex);
Jean-Michel Trivif613d422015-04-23 18:41:29 -0700911 gDynPolicyCallback = cb;
912}
913
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800914/*static*/ void AudioSystem::setRecordConfigCallback(record_config_callback cb) {
Andy Hungacd955a2024-02-16 17:14:36 -0800915 std::lock_guard _l(gMutex);
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -0800916 gRecordConfigCallback = cb;
917}
918
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800919/*static*/ void AudioSystem::setRoutingCallback(routing_callback cb) {
Andy Hungacd955a2024-02-16 17:14:36 -0800920 std::lock_guard _l(gMutex);
Jean-Michel Trivi9a6b9ad2020-10-22 16:46:43 -0700921 gRoutingCallback = cb;
922}
923
Jean-Michel Trivi78f2b302022-04-15 18:18:41 +0000924/*static*/ void AudioSystem::setVolInitReqCallback(vol_range_init_req_callback cb) {
Andy Hungacd955a2024-02-16 17:14:36 -0800925 std::lock_guard _l(gMutex);
Jean-Michel Trivi78f2b302022-04-15 18:18:41 +0000926 gVolRangeInitReqCallback = cb;
927}
928
Andy Hung88e71732024-02-21 16:44:27 -0800929struct AudioPolicyTraits {
930 static void onServiceCreate(const sp<IAudioPolicyService>& ap,
931 const sp<AudioSystem::AudioPolicyServiceClient>& apc) {
932 const int64_t token = IPCThreadState::self()->clearCallingIdentity();
Eric Laurent0ebd5f92014-11-19 19:04:52 -0800933 ap->registerClient(apc);
François Gaffie24437602018-04-23 15:08:59 +0200934 ap->setAudioPortCallbacksEnabled(apc->isAudioPortCbEnabled());
François Gaffiecfe17322018-11-07 13:41:29 +0100935 ap->setAudioVolumeGroupCallbacksEnabled(apc->isAudioVolumeGroupCbEnabled());
François Gaffie24437602018-04-23 15:08:59 +0200936 IPCThreadState::self()->restoreCallingIdentity(token);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700937 }
Glenn Kastend2d089f2014-11-05 11:48:12 -0800938
Andy Hung88e71732024-02-21 16:44:27 -0800939 static void onClearService(const sp<AudioSystem::AudioPolicyServiceClient>&) {}
940
941 static constexpr const char *SERVICE_NAME = "media.audio_policy";
942};
943
944[[clang::no_destroy]] static constinit ServiceHandler<IAudioPolicyService,
945 AudioSystem::AudioPolicyServiceClient, IAudioPolicyService,
946 AudioPolicyTraits> gAudioPolicyServiceHandler;
947
948status_t AudioSystem::setLocalAudioPolicyService(const sp<IAudioPolicyService>& aps) {
949 return gAudioPolicyServiceHandler.setLocalService(aps);
950}
951
952sp<IAudioPolicyService> AudioSystem::get_audio_policy_service() {
953 return gAudioPolicyServiceHandler.getService();
Eric Laurentc2f1f072009-07-17 12:17:14 -0700954}
955
Mikhail Naganovec3d5792022-05-06 00:19:55 +0000956void AudioSystem::clearAudioPolicyService() {
Andy Hung88e71732024-02-21 16:44:27 -0800957 gAudioPolicyServiceHandler.clearService();
Mikhail Naganovec3d5792022-05-06 00:19:55 +0000958}
959
Glenn Kastenfb1fdc92013-07-10 17:03:19 -0700960// ---------------------------------------------------------------------------
961
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800962void AudioSystem::onNewAudioModulesAvailable() {
Andy Hung264fa4c2024-02-21 15:52:12 -0800963 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Mikhail Naganov88b30d22020-03-09 19:43:13 +0000964 if (aps == 0) return;
965 aps->onNewAudioModulesAvailable();
966}
967
Nathalie Le Clair88fa2752021-11-23 13:03:41 +0100968status_t AudioSystem::setDeviceConnectionState(audio_policy_dev_state_t state,
969 const android::media::audio::common::AudioPort& port,
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800970 audio_format_t encodedFormat) {
Andy Hung264fa4c2024-02-21 15:52:12 -0800971 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Eric Laurent71b63e32011-09-02 14:20:56 -0700972
Eric Laurentc2f1f072009-07-17 12:17:14 -0700973 if (aps == 0) return PERMISSION_DENIED;
974
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800975 return statusTFromBinderStatus(
976 aps->setDeviceConnectionState(
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800977 VALUE_OR_RETURN_STATUS(
978 legacy2aidl_audio_policy_dev_state_t_AudioPolicyDeviceState(state)),
Nathalie Le Clair88fa2752021-11-23 13:03:41 +0100979 port,
Mikhail Naganovb60bd1b2021-07-15 17:31:43 -0700980 VALUE_OR_RETURN_STATUS(
981 legacy2aidl_audio_format_t_AudioFormatDescription(encodedFormat))));
Eric Laurentc2f1f072009-07-17 12:17:14 -0700982}
983
Dima Zavinfce7a472011-04-19 22:30:36 -0700984audio_policy_dev_state_t AudioSystem::getDeviceConnectionState(audio_devices_t device,
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800985 const char* device_address) {
Andy Hung264fa4c2024-02-21 15:52:12 -0800986 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Dima Zavinfce7a472011-04-19 22:30:36 -0700987 if (aps == 0) return AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE;
Eric Laurentc2f1f072009-07-17 12:17:14 -0700988
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800989 auto result = [&]() -> ConversionResult<audio_policy_dev_state_t> {
Mikhail Naganov932cb962021-09-16 01:05:27 +0000990 AudioDevice deviceAidl = VALUE_OR_RETURN(
991 legacy2aidl_audio_device_AudioDevice(device, device_address));
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800992
993 media::AudioPolicyDeviceState result;
994 RETURN_IF_ERROR(statusTFromBinderStatus(
995 aps->getDeviceConnectionState(deviceAidl, &result)));
996
997 return aidl2legacy_AudioPolicyDeviceState_audio_policy_dev_state_t(result);
998 }();
999 return result.value_or(AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE);
Eric Laurentc2f1f072009-07-17 12:17:14 -07001000}
1001
Pavlin Radoslavovf862bc62016-12-26 18:57:22 -08001002status_t AudioSystem::handleDeviceConfigChange(audio_devices_t device,
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001003 const char* device_address,
1004 const char* device_name,
1005 audio_format_t encodedFormat) {
Andy Hung264fa4c2024-02-21 15:52:12 -08001006 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001007 const char* address = "";
1008 const char* name = "";
Pavlin Radoslavovf862bc62016-12-26 18:57:22 -08001009
1010 if (aps == 0) return PERMISSION_DENIED;
1011
1012 if (device_address != NULL) {
1013 address = device_address;
1014 }
1015 if (device_name != NULL) {
1016 name = device_name;
1017 }
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001018
Mikhail Naganov932cb962021-09-16 01:05:27 +00001019 AudioDevice deviceAidl = VALUE_OR_RETURN_STATUS(
1020 legacy2aidl_audio_device_AudioDevice(device, address));
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001021
1022 return statusTFromBinderStatus(
1023 aps->handleDeviceConfigChange(deviceAidl, name, VALUE_OR_RETURN_STATUS(
Mikhail Naganovb60bd1b2021-07-15 17:31:43 -07001024 legacy2aidl_audio_format_t_AudioFormatDescription(encodedFormat))));
Pavlin Radoslavovf862bc62016-12-26 18:57:22 -08001025}
1026
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001027status_t AudioSystem::setPhoneState(audio_mode_t state, uid_t uid) {
Glenn Kasten347966c2012-01-18 14:58:32 -08001028 if (uint32_t(state) >= AUDIO_MODE_CNT) return BAD_VALUE;
Andy Hung264fa4c2024-02-21 15:52:12 -08001029 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Eric Laurentc2f1f072009-07-17 12:17:14 -07001030 if (aps == 0) return PERMISSION_DENIED;
1031
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001032 return statusTFromBinderStatus(aps->setPhoneState(
1033 VALUE_OR_RETURN_STATUS(legacy2aidl_audio_mode_t_AudioMode(state)),
1034 VALUE_OR_RETURN_STATUS(legacy2aidl_uid_t_int32_t(uid))));
Eric Laurentc2f1f072009-07-17 12:17:14 -07001035}
1036
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001037status_t
1038AudioSystem::setForceUse(audio_policy_force_use_t usage, audio_policy_forced_cfg_t config) {
Andy Hung264fa4c2024-02-21 15:52:12 -08001039 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Eric Laurentc2f1f072009-07-17 12:17:14 -07001040 if (aps == 0) return PERMISSION_DENIED;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001041
1042 return statusTFromBinderStatus(
1043 aps->setForceUse(
1044 VALUE_OR_RETURN_STATUS(
1045 legacy2aidl_audio_policy_force_use_t_AudioPolicyForceUse(usage)),
1046 VALUE_OR_RETURN_STATUS(
1047 legacy2aidl_audio_policy_forced_cfg_t_AudioPolicyForcedConfig(
1048 config))));
Eric Laurentc2f1f072009-07-17 12:17:14 -07001049}
1050
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001051audio_policy_forced_cfg_t AudioSystem::getForceUse(audio_policy_force_use_t usage) {
Andy Hung264fa4c2024-02-21 15:52:12 -08001052 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Dima Zavinfce7a472011-04-19 22:30:36 -07001053 if (aps == 0) return AUDIO_POLICY_FORCE_NONE;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001054
1055 auto result = [&]() -> ConversionResult<audio_policy_forced_cfg_t> {
1056 media::AudioPolicyForceUse usageAidl = VALUE_OR_RETURN(
1057 legacy2aidl_audio_policy_force_use_t_AudioPolicyForceUse(usage));
1058 media::AudioPolicyForcedConfig configAidl;
1059 RETURN_IF_ERROR(statusTFromBinderStatus(
1060 aps->getForceUse(usageAidl, &configAidl)));
1061 return aidl2legacy_AudioPolicyForcedConfig_audio_policy_forced_cfg_t(configAidl);
1062 }();
1063
1064 return result.value_or(AUDIO_POLICY_FORCE_NONE);
Eric Laurentc2f1f072009-07-17 12:17:14 -07001065}
1066
1067
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001068audio_io_handle_t AudioSystem::getOutput(audio_stream_type_t stream) {
Andy Hung264fa4c2024-02-21 15:52:12 -08001069 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001070 if (aps == 0) return AUDIO_IO_HANDLE_NONE;
1071
1072 auto result = [&]() -> ConversionResult<audio_io_handle_t> {
Mikhail Naganovdbf03642021-08-25 18:15:32 -07001073 AudioStreamType streamAidl = VALUE_OR_RETURN(
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001074 legacy2aidl_audio_stream_type_t_AudioStreamType(stream));
1075 int32_t outputAidl;
1076 RETURN_IF_ERROR(
1077 statusTFromBinderStatus(aps->getOutput(streamAidl, &outputAidl)));
1078 return aidl2legacy_int32_t_audio_io_handle_t(outputAidl);
1079 }();
1080
1081 return result.value_or(AUDIO_IO_HANDLE_NONE);
Eric Laurentc2f1f072009-07-17 12:17:14 -07001082}
1083
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001084status_t AudioSystem::getOutputForAttr(audio_attributes_t* attr,
1085 audio_io_handle_t* output,
1086 audio_session_t session,
1087 audio_stream_type_t* stream,
Svet Ganov3e5f14f2021-05-13 22:51:08 +00001088 const AttributionSourceState& attributionSource,
jiabinf1c73972022-04-14 16:28:52 -07001089 audio_config_t* config,
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001090 audio_output_flags_t flags,
1091 audio_port_handle_t* selectedDeviceId,
1092 audio_port_handle_t* portId,
Eric Laurentb0a7bc92022-04-05 15:06:08 +02001093 std::vector<audio_io_handle_t>* secondaryOutputs,
jiabinc658e452022-10-21 20:52:21 +00001094 bool *isSpatialized,
1095 bool *isBitPerfect) {
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001096 if (attr == nullptr) {
1097 ALOGE("%s NULL audio attributes", __func__);
1098 return BAD_VALUE;
1099 }
1100 if (output == nullptr) {
1101 ALOGE("%s NULL output - shouldn't happen", __func__);
1102 return BAD_VALUE;
1103 }
1104 if (selectedDeviceId == nullptr) {
1105 ALOGE("%s NULL selectedDeviceId - shouldn't happen", __func__);
1106 return BAD_VALUE;
1107 }
1108 if (portId == nullptr) {
1109 ALOGE("%s NULL portId - shouldn't happen", __func__);
1110 return BAD_VALUE;
1111 }
1112 if (secondaryOutputs == nullptr) {
1113 ALOGE("%s NULL secondaryOutputs - shouldn't happen", __func__);
1114 return BAD_VALUE;
1115 }
1116
Andy Hung264fa4c2024-02-21 15:52:12 -08001117 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Eric Laurente83b55d2014-11-14 10:06:21 -08001118 if (aps == 0) return NO_INIT;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001119
Mikhail Naganov1c400902023-05-17 11:48:43 -07001120 media::audio::common::AudioAttributes attrAidl = VALUE_OR_RETURN_STATUS(
1121 legacy2aidl_audio_attributes_t_AudioAttributes(*attr));
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001122 int32_t sessionAidl = VALUE_OR_RETURN_STATUS(legacy2aidl_audio_session_t_int32_t(session));
Mikhail Naganovdbf03642021-08-25 18:15:32 -07001123 AudioConfig configAidl = VALUE_OR_RETURN_STATUS(
Mikhail Naganovde3fa182021-07-30 15:06:42 -07001124 legacy2aidl_audio_config_t_AudioConfig(*config, false /*isInput*/));
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001125 int32_t flagsAidl = VALUE_OR_RETURN_STATUS(
1126 legacy2aidl_audio_output_flags_t_int32_t_mask(flags));
Eric Laurentf99edd32021-02-01 15:57:33 +01001127 int32_t selectedDeviceIdAidl = VALUE_OR_RETURN_STATUS(
1128 legacy2aidl_audio_port_handle_t_int32_t(*selectedDeviceId));
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001129
1130 media::GetOutputForAttrResponse responseAidl;
1131
jiabinf1c73972022-04-14 16:28:52 -07001132 status_t status = statusTFromBinderStatus(
Svet Ganov3e5f14f2021-05-13 22:51:08 +00001133 aps->getOutputForAttr(attrAidl, sessionAidl, attributionSource, configAidl, flagsAidl,
jiabinf1c73972022-04-14 16:28:52 -07001134 selectedDeviceIdAidl, &responseAidl));
1135 if (status != NO_ERROR) {
1136 config->format = VALUE_OR_RETURN_STATUS(
1137 aidl2legacy_AudioFormatDescription_audio_format_t(responseAidl.configBase.format));
1138 config->channel_mask = VALUE_OR_RETURN_STATUS(
1139 aidl2legacy_AudioChannelLayout_audio_channel_mask_t(
1140 responseAidl.configBase.channelMask, false /*isInput*/));
1141 config->sample_rate = responseAidl.configBase.sampleRate;
1142 return status;
1143 }
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001144
1145 *output = VALUE_OR_RETURN_STATUS(
1146 aidl2legacy_int32_t_audio_io_handle_t(responseAidl.output));
1147
1148 if (stream != nullptr) {
1149 *stream = VALUE_OR_RETURN_STATUS(
1150 aidl2legacy_AudioStreamType_audio_stream_type_t(responseAidl.stream));
1151 }
1152 *selectedDeviceId = VALUE_OR_RETURN_STATUS(
1153 aidl2legacy_int32_t_audio_port_handle_t(responseAidl.selectedDeviceId));
1154 *portId = VALUE_OR_RETURN_STATUS(aidl2legacy_int32_t_audio_port_handle_t(responseAidl.portId));
1155 *secondaryOutputs = VALUE_OR_RETURN_STATUS(convertContainer<std::vector<audio_io_handle_t>>(
1156 responseAidl.secondaryOutputs, aidl2legacy_int32_t_audio_io_handle_t));
Eric Laurentb0a7bc92022-04-05 15:06:08 +02001157 *isSpatialized = responseAidl.isSpatialized;
jiabinc658e452022-10-21 20:52:21 +00001158 *isBitPerfect = responseAidl.isBitPerfect;
Eric Laurent0d13fea2022-11-04 17:12:08 +01001159 *attr = VALUE_OR_RETURN_STATUS(
Mikhail Naganov1c400902023-05-17 11:48:43 -07001160 aidl2legacy_AudioAttributes_audio_attributes_t(responseAidl.attr));
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001161
1162 return OK;
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -07001163}
1164
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001165status_t AudioSystem::startOutput(audio_port_handle_t portId) {
Andy Hung264fa4c2024-02-21 15:52:12 -08001166 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Eric Laurentc2f1f072009-07-17 12:17:14 -07001167 if (aps == 0) return PERMISSION_DENIED;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001168
1169 int32_t portIdAidl = VALUE_OR_RETURN_STATUS(legacy2aidl_audio_port_handle_t_int32_t(portId));
1170 return statusTFromBinderStatus(aps->startOutput(portIdAidl));
Eric Laurentc2f1f072009-07-17 12:17:14 -07001171}
1172
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001173status_t AudioSystem::stopOutput(audio_port_handle_t portId) {
Andy Hung264fa4c2024-02-21 15:52:12 -08001174 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Eric Laurentc2f1f072009-07-17 12:17:14 -07001175 if (aps == 0) return PERMISSION_DENIED;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001176
1177 int32_t portIdAidl = VALUE_OR_RETURN_STATUS(legacy2aidl_audio_port_handle_t_int32_t(portId));
1178 return statusTFromBinderStatus(aps->stopOutput(portIdAidl));
Eric Laurentc2f1f072009-07-17 12:17:14 -07001179}
1180
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001181void AudioSystem::releaseOutput(audio_port_handle_t portId) {
Andy Hung264fa4c2024-02-21 15:52:12 -08001182 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Eric Laurentc2f1f072009-07-17 12:17:14 -07001183 if (aps == 0) return;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001184
1185 auto status = [&]() -> status_t {
1186 int32_t portIdAidl = VALUE_OR_RETURN_STATUS(
1187 legacy2aidl_audio_port_handle_t_int32_t(portId));
1188 RETURN_STATUS_IF_ERROR(statusTFromBinderStatus(aps->releaseOutput(portIdAidl)));
1189 return OK;
1190 }();
1191
1192 // Ignore status.
1193 (void) status;
Eric Laurentc2f1f072009-07-17 12:17:14 -07001194}
1195
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001196status_t AudioSystem::getInputForAttr(const audio_attributes_t* attr,
1197 audio_io_handle_t* input,
1198 audio_unique_id_t riid,
1199 audio_session_t session,
Svet Ganov3e5f14f2021-05-13 22:51:08 +00001200 const AttributionSourceState &attributionSource,
jiabinf1c73972022-04-14 16:28:52 -07001201 audio_config_base_t* config,
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001202 audio_input_flags_t flags,
1203 audio_port_handle_t* selectedDeviceId,
1204 audio_port_handle_t* portId) {
1205 if (attr == NULL) {
1206 ALOGE("getInputForAttr NULL attr - shouldn't happen");
1207 return BAD_VALUE;
1208 }
1209 if (input == NULL) {
1210 ALOGE("getInputForAttr NULL input - shouldn't happen");
1211 return BAD_VALUE;
1212 }
1213 if (selectedDeviceId == NULL) {
1214 ALOGE("getInputForAttr NULL selectedDeviceId - shouldn't happen");
1215 return BAD_VALUE;
1216 }
1217 if (portId == NULL) {
1218 ALOGE("getInputForAttr NULL portId - shouldn't happen");
1219 return BAD_VALUE;
1220 }
1221
Andy Hung264fa4c2024-02-21 15:52:12 -08001222 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Eric Laurentcaf7f482014-11-25 17:50:47 -08001223 if (aps == 0) return NO_INIT;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001224
Mikhail Naganov1c400902023-05-17 11:48:43 -07001225 media::audio::common::AudioAttributes attrAidl = VALUE_OR_RETURN_STATUS(
1226 legacy2aidl_audio_attributes_t_AudioAttributes(*attr));
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001227 int32_t inputAidl = VALUE_OR_RETURN_STATUS(legacy2aidl_audio_io_handle_t_int32_t(*input));
1228 int32_t riidAidl = VALUE_OR_RETURN_STATUS(legacy2aidl_audio_unique_id_t_int32_t(riid));
1229 int32_t sessionAidl = VALUE_OR_RETURN_STATUS(legacy2aidl_audio_session_t_int32_t(session));
Mikhail Naganovdbf03642021-08-25 18:15:32 -07001230 AudioConfigBase configAidl = VALUE_OR_RETURN_STATUS(
Mikhail Naganovde3fa182021-07-30 15:06:42 -07001231 legacy2aidl_audio_config_base_t_AudioConfigBase(*config, true /*isInput*/));
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001232 int32_t flagsAidl = VALUE_OR_RETURN_STATUS(legacy2aidl_audio_input_flags_t_int32_t_mask(flags));
Eric Laurentf99edd32021-02-01 15:57:33 +01001233 int32_t selectedDeviceIdAidl = VALUE_OR_RETURN_STATUS(
1234 legacy2aidl_audio_port_handle_t_int32_t(*selectedDeviceId));
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001235
1236 media::GetInputForAttrResponse response;
1237
jiabinf1c73972022-04-14 16:28:52 -07001238 status_t status = statusTFromBinderStatus(
Svet Ganov3e5f14f2021-05-13 22:51:08 +00001239 aps->getInputForAttr(attrAidl, inputAidl, riidAidl, sessionAidl, attributionSource,
jiabinf1c73972022-04-14 16:28:52 -07001240 configAidl, flagsAidl, selectedDeviceIdAidl, &response));
1241 if (status != NO_ERROR) {
1242 *config = VALUE_OR_RETURN_STATUS(
1243 aidl2legacy_AudioConfigBase_audio_config_base_t(response.config, true /*isInput*/));
1244 return status;
1245 }
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001246
1247 *input = VALUE_OR_RETURN_STATUS(aidl2legacy_int32_t_audio_io_handle_t(response.input));
1248 *selectedDeviceId = VALUE_OR_RETURN_STATUS(
1249 aidl2legacy_int32_t_audio_port_handle_t(response.selectedDeviceId));
1250 *portId = VALUE_OR_RETURN_STATUS(aidl2legacy_int32_t_audio_port_handle_t(response.portId));
1251
1252 return OK;
Eric Laurentc2f1f072009-07-17 12:17:14 -07001253}
1254
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001255status_t AudioSystem::startInput(audio_port_handle_t portId) {
Andy Hung264fa4c2024-02-21 15:52:12 -08001256 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Eric Laurentc2f1f072009-07-17 12:17:14 -07001257 if (aps == 0) return PERMISSION_DENIED;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001258
1259 int32_t portIdAidl = VALUE_OR_RETURN_STATUS(legacy2aidl_audio_port_handle_t_int32_t(portId));
1260 return statusTFromBinderStatus(aps->startInput(portIdAidl));
Eric Laurentc2f1f072009-07-17 12:17:14 -07001261}
1262
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001263status_t AudioSystem::stopInput(audio_port_handle_t portId) {
Andy Hung264fa4c2024-02-21 15:52:12 -08001264 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Eric Laurentc2f1f072009-07-17 12:17:14 -07001265 if (aps == 0) return PERMISSION_DENIED;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001266
1267 int32_t portIdAidl = VALUE_OR_RETURN_STATUS(legacy2aidl_audio_port_handle_t_int32_t(portId));
1268 return statusTFromBinderStatus(aps->stopInput(portIdAidl));
Eric Laurentc2f1f072009-07-17 12:17:14 -07001269}
1270
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001271void AudioSystem::releaseInput(audio_port_handle_t portId) {
Andy Hung264fa4c2024-02-21 15:52:12 -08001272 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Eric Laurentc2f1f072009-07-17 12:17:14 -07001273 if (aps == 0) return;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001274
1275 auto status = [&]() -> status_t {
1276 int32_t portIdAidl = VALUE_OR_RETURN_STATUS(
1277 legacy2aidl_audio_port_handle_t_int32_t(portId));
1278 RETURN_STATUS_IF_ERROR(statusTFromBinderStatus(aps->releaseInput(portIdAidl)));
1279 return OK;
1280 }();
1281
1282 // Ignore status.
1283 (void) status;
Eric Laurentc2f1f072009-07-17 12:17:14 -07001284}
1285
Dima Zavinfce7a472011-04-19 22:30:36 -07001286status_t AudioSystem::initStreamVolume(audio_stream_type_t stream,
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001287 int indexMin,
1288 int indexMax) {
Andy Hung264fa4c2024-02-21 15:52:12 -08001289 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Eric Laurentc2f1f072009-07-17 12:17:14 -07001290 if (aps == 0) return PERMISSION_DENIED;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001291
Mikhail Naganovdbf03642021-08-25 18:15:32 -07001292 AudioStreamType streamAidl = VALUE_OR_RETURN_STATUS(
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001293 legacy2aidl_audio_stream_type_t_AudioStreamType(stream));
1294 int32_t indexMinAidl = VALUE_OR_RETURN_STATUS(convertIntegral<int32_t>(indexMin));
1295 int32_t indexMaxAidl = VALUE_OR_RETURN_STATUS(convertIntegral<int32_t>(indexMax));
Mikhail Naganovec3d5792022-05-06 00:19:55 +00001296 status_t status = statusTFromBinderStatus(
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001297 aps->initStreamVolume(streamAidl, indexMinAidl, indexMaxAidl));
Mikhail Naganovec3d5792022-05-06 00:19:55 +00001298 if (status == DEAD_OBJECT) {
1299 // This is a critical operation since w/o proper stream volumes no audio
1300 // will be heard. Make sure we recover from a failure in any case.
1301 ALOGE("Received DEAD_OBJECT from APS, clearing the client");
1302 clearAudioPolicyService();
1303 }
1304 return status;
Eric Laurentc2f1f072009-07-17 12:17:14 -07001305}
1306
Eric Laurent83844cc2011-11-18 16:43:31 -08001307status_t AudioSystem::setStreamVolumeIndex(audio_stream_type_t stream,
1308 int index,
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001309 audio_devices_t device) {
Andy Hung264fa4c2024-02-21 15:52:12 -08001310 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Eric Laurentc2f1f072009-07-17 12:17:14 -07001311 if (aps == 0) return PERMISSION_DENIED;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001312
Mikhail Naganovdbf03642021-08-25 18:15:32 -07001313 AudioStreamType streamAidl = VALUE_OR_RETURN_STATUS(
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001314 legacy2aidl_audio_stream_type_t_AudioStreamType(stream));
1315 int32_t indexAidl = VALUE_OR_RETURN_STATUS(convertIntegral<int32_t>(index));
Mikhail Naganovf4a75362021-09-16 00:02:54 +00001316 AudioDeviceDescription deviceAidl = VALUE_OR_RETURN_STATUS(
Mikhail Naganov21a32ec2021-07-08 14:40:12 -07001317 legacy2aidl_audio_devices_t_AudioDeviceDescription(device));
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001318 return statusTFromBinderStatus(
1319 aps->setStreamVolumeIndex(streamAidl, deviceAidl, indexAidl));
Eric Laurentc2f1f072009-07-17 12:17:14 -07001320}
1321
Eric Laurent83844cc2011-11-18 16:43:31 -08001322status_t AudioSystem::getStreamVolumeIndex(audio_stream_type_t stream,
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001323 int* index,
1324 audio_devices_t device) {
Andy Hung264fa4c2024-02-21 15:52:12 -08001325 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Eric Laurentc2f1f072009-07-17 12:17:14 -07001326 if (aps == 0) return PERMISSION_DENIED;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001327
Mikhail Naganovdbf03642021-08-25 18:15:32 -07001328 AudioStreamType streamAidl = VALUE_OR_RETURN_STATUS(
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001329 legacy2aidl_audio_stream_type_t_AudioStreamType(stream));
Mikhail Naganovf4a75362021-09-16 00:02:54 +00001330 AudioDeviceDescription deviceAidl = VALUE_OR_RETURN_STATUS(
Mikhail Naganov21a32ec2021-07-08 14:40:12 -07001331 legacy2aidl_audio_devices_t_AudioDeviceDescription(device));
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001332 int32_t indexAidl;
1333 RETURN_STATUS_IF_ERROR(statusTFromBinderStatus(
1334 aps->getStreamVolumeIndex(streamAidl, deviceAidl, &indexAidl)));
1335 if (index != nullptr) {
1336 *index = VALUE_OR_RETURN_STATUS(convertIntegral<int>(indexAidl));
1337 }
1338 return OK;
Eric Laurentc2f1f072009-07-17 12:17:14 -07001339}
1340
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001341status_t AudioSystem::setVolumeIndexForAttributes(const audio_attributes_t& attr,
François Gaffiecfe17322018-11-07 13:41:29 +01001342 int index,
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001343 audio_devices_t device) {
Andy Hung264fa4c2024-02-21 15:52:12 -08001344 const sp<IAudioPolicyService> aps = get_audio_policy_service();
François Gaffiecfe17322018-11-07 13:41:29 +01001345 if (aps == 0) return PERMISSION_DENIED;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001346
Mikhail Naganov1c400902023-05-17 11:48:43 -07001347 media::audio::common::AudioAttributes attrAidl = VALUE_OR_RETURN_STATUS(
1348 legacy2aidl_audio_attributes_t_AudioAttributes(attr));
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001349 int32_t indexAidl = VALUE_OR_RETURN_STATUS(convertIntegral<int32_t>(index));
Mikhail Naganovf4a75362021-09-16 00:02:54 +00001350 AudioDeviceDescription deviceAidl = VALUE_OR_RETURN_STATUS(
Mikhail Naganov21a32ec2021-07-08 14:40:12 -07001351 legacy2aidl_audio_devices_t_AudioDeviceDescription(device));
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001352 return statusTFromBinderStatus(
1353 aps->setVolumeIndexForAttributes(attrAidl, deviceAidl, indexAidl));
François Gaffiecfe17322018-11-07 13:41:29 +01001354}
1355
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001356status_t AudioSystem::getVolumeIndexForAttributes(const audio_attributes_t& attr,
1357 int& index,
1358 audio_devices_t device) {
Andy Hung264fa4c2024-02-21 15:52:12 -08001359 const sp<IAudioPolicyService> aps = get_audio_policy_service();
François Gaffiecfe17322018-11-07 13:41:29 +01001360 if (aps == 0) return PERMISSION_DENIED;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001361
Mikhail Naganov1c400902023-05-17 11:48:43 -07001362 media::audio::common::AudioAttributes attrAidl = VALUE_OR_RETURN_STATUS(
1363 legacy2aidl_audio_attributes_t_AudioAttributes(attr));
Mikhail Naganovf4a75362021-09-16 00:02:54 +00001364 AudioDeviceDescription deviceAidl = VALUE_OR_RETURN_STATUS(
Mikhail Naganov21a32ec2021-07-08 14:40:12 -07001365 legacy2aidl_audio_devices_t_AudioDeviceDescription(device));
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001366 int32_t indexAidl;
1367 RETURN_STATUS_IF_ERROR(statusTFromBinderStatus(
1368 aps->getVolumeIndexForAttributes(attrAidl, deviceAidl, &indexAidl)));
1369 index = VALUE_OR_RETURN_STATUS(convertIntegral<int>(indexAidl));
1370 return OK;
François Gaffiecfe17322018-11-07 13:41:29 +01001371}
1372
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001373status_t AudioSystem::getMaxVolumeIndexForAttributes(const audio_attributes_t& attr, int& index) {
Andy Hung264fa4c2024-02-21 15:52:12 -08001374 const sp<IAudioPolicyService> aps = get_audio_policy_service();
François Gaffiecfe17322018-11-07 13:41:29 +01001375 if (aps == 0) return PERMISSION_DENIED;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001376
Mikhail Naganov1c400902023-05-17 11:48:43 -07001377 media::audio::common::AudioAttributes attrAidl = VALUE_OR_RETURN_STATUS(
1378 legacy2aidl_audio_attributes_t_AudioAttributes(attr));
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001379 int32_t indexAidl;
1380 RETURN_STATUS_IF_ERROR(statusTFromBinderStatus(
1381 aps->getMaxVolumeIndexForAttributes(attrAidl, &indexAidl)));
1382 index = VALUE_OR_RETURN_STATUS(convertIntegral<int>(indexAidl));
1383 return OK;
François Gaffiecfe17322018-11-07 13:41:29 +01001384}
1385
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001386status_t AudioSystem::getMinVolumeIndexForAttributes(const audio_attributes_t& attr, int& index) {
Andy Hung264fa4c2024-02-21 15:52:12 -08001387 const sp<IAudioPolicyService> aps = get_audio_policy_service();
François Gaffiecfe17322018-11-07 13:41:29 +01001388 if (aps == 0) return PERMISSION_DENIED;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001389
Mikhail Naganov1c400902023-05-17 11:48:43 -07001390 media::audio::common::AudioAttributes attrAidl = VALUE_OR_RETURN_STATUS(
1391 legacy2aidl_audio_attributes_t_AudioAttributes(attr));
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001392 int32_t indexAidl;
1393 RETURN_STATUS_IF_ERROR(statusTFromBinderStatus(
1394 aps->getMinVolumeIndexForAttributes(attrAidl, &indexAidl)));
1395 index = VALUE_OR_RETURN_STATUS(convertIntegral<int>(indexAidl));
1396 return OK;
François Gaffiecfe17322018-11-07 13:41:29 +01001397}
1398
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001399product_strategy_t AudioSystem::getStrategyForStream(audio_stream_type_t stream) {
Andy Hung264fa4c2024-02-21 15:52:12 -08001400 const sp<IAudioPolicyService> aps = get_audio_policy_service();
François Gaffied0ba9ed2018-11-05 11:50:42 +01001401 if (aps == 0) return PRODUCT_STRATEGY_NONE;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001402
1403 auto result = [&]() -> ConversionResult<product_strategy_t> {
Mikhail Naganovdbf03642021-08-25 18:15:32 -07001404 AudioStreamType streamAidl = VALUE_OR_RETURN(
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001405 legacy2aidl_audio_stream_type_t_AudioStreamType(stream));
1406 int32_t resultAidl;
1407 RETURN_IF_ERROR(statusTFromBinderStatus(
1408 aps->getStrategyForStream(streamAidl, &resultAidl)));
1409 return aidl2legacy_int32_t_product_strategy_t(resultAidl);
1410 }();
1411 return result.value_or(PRODUCT_STRATEGY_NONE);
Eric Laurentde070132010-07-13 04:45:46 -07001412}
1413
François Gaffie1e2b56f2022-04-01 14:34:29 +02001414status_t AudioSystem::getDevicesForAttributes(const audio_attributes_t& aa,
Andy Hung6d23c0f2022-02-16 09:37:15 -08001415 AudioDeviceTypeAddrVector* devices,
1416 bool forVolume) {
Jean-Michel Trivif41599b2020-01-07 14:22:08 -08001417 if (devices == nullptr) {
1418 return BAD_VALUE;
1419 }
Andy Hung264fa4c2024-02-21 15:52:12 -08001420 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Jean-Michel Trivif41599b2020-01-07 14:22:08 -08001421 if (aps == 0) return PERMISSION_DENIED;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001422
Mikhail Naganov1c400902023-05-17 11:48:43 -07001423 media::audio::common::AudioAttributes aaAidl = VALUE_OR_RETURN_STATUS(
1424 legacy2aidl_audio_attributes_t_AudioAttributes(aa));
Mikhail Naganovf4a75362021-09-16 00:02:54 +00001425 std::vector<AudioDevice> retAidl;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001426 RETURN_STATUS_IF_ERROR(
Andy Hung6d23c0f2022-02-16 09:37:15 -08001427 statusTFromBinderStatus(aps->getDevicesForAttributes(aaAidl, forVolume, &retAidl)));
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001428 *devices = VALUE_OR_RETURN_STATUS(
1429 convertContainer<AudioDeviceTypeAddrVector>(
1430 retAidl,
1431 aidl2legacy_AudioDeviceTypeAddress));
1432 return OK;
Jean-Michel Trivif41599b2020-01-07 14:22:08 -08001433}
1434
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001435audio_io_handle_t AudioSystem::getOutputForEffect(const effect_descriptor_t* desc) {
Andy Hung264fa4c2024-02-21 15:52:12 -08001436 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Glenn Kastenefa6ea92014-01-08 09:10:43 -08001437 // FIXME change return type to status_t, and return PERMISSION_DENIED here
Glenn Kasten142f5192014-03-25 17:44:59 -07001438 if (aps == 0) return AUDIO_IO_HANDLE_NONE;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001439
1440 auto result = [&]() -> ConversionResult<audio_io_handle_t> {
1441 media::EffectDescriptor descAidl = VALUE_OR_RETURN(
1442 legacy2aidl_effect_descriptor_t_EffectDescriptor(*desc));
1443 int32_t retAidl;
1444 RETURN_IF_ERROR(
1445 statusTFromBinderStatus(aps->getOutputForEffect(descAidl, &retAidl)));
1446 return aidl2legacy_int32_t_audio_io_handle_t(retAidl);
1447 }();
1448
1449 return result.value_or(AUDIO_IO_HANDLE_NONE);
Eric Laurentde070132010-07-13 04:45:46 -07001450}
1451
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001452status_t AudioSystem::registerEffect(const effect_descriptor_t* desc,
1453 audio_io_handle_t io,
1454 product_strategy_t strategy,
1455 audio_session_t session,
1456 int id) {
Andy Hung264fa4c2024-02-21 15:52:12 -08001457 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Eric Laurentde070132010-07-13 04:45:46 -07001458 if (aps == 0) return PERMISSION_DENIED;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001459
1460 media::EffectDescriptor descAidl = VALUE_OR_RETURN_STATUS(
1461 legacy2aidl_effect_descriptor_t_EffectDescriptor(*desc));
1462 int32_t ioAidl = VALUE_OR_RETURN_STATUS(legacy2aidl_audio_io_handle_t_int32_t(io));
1463 int32_t strategyAidl = VALUE_OR_RETURN_STATUS(aidl2legacy_int32_t_product_strategy_t(strategy));
1464 int32_t sessionAidl = VALUE_OR_RETURN_STATUS(legacy2aidl_audio_session_t_int32_t(session));
1465 int32_t idAidl = VALUE_OR_RETURN_STATUS(convertReinterpret<int32_t>(id));
1466 return statusTFromBinderStatus(
1467 aps->registerEffect(descAidl, ioAidl, strategyAidl, sessionAidl, idAidl));
Eric Laurentde070132010-07-13 04:45:46 -07001468}
1469
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001470status_t AudioSystem::unregisterEffect(int id) {
Andy Hung264fa4c2024-02-21 15:52:12 -08001471 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Eric Laurentde070132010-07-13 04:45:46 -07001472 if (aps == 0) return PERMISSION_DENIED;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001473
1474 int32_t idAidl = VALUE_OR_RETURN_STATUS(convertReinterpret<int32_t>(id));
1475 return statusTFromBinderStatus(
1476 aps->unregisterEffect(idAidl));
Eric Laurentde070132010-07-13 04:45:46 -07001477}
1478
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001479status_t AudioSystem::setEffectEnabled(int id, bool enabled) {
Andy Hung264fa4c2024-02-21 15:52:12 -08001480 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Eric Laurentdb7c0792011-08-10 10:37:50 -07001481 if (aps == 0) return PERMISSION_DENIED;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001482
1483 int32_t idAidl = VALUE_OR_RETURN_STATUS(convertReinterpret<int32_t>(id));
1484 return statusTFromBinderStatus(
1485 aps->setEffectEnabled(idAidl, enabled));
Eric Laurentdb7c0792011-08-10 10:37:50 -07001486}
1487
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001488status_t AudioSystem::moveEffectsToIo(const std::vector<int>& ids, audio_io_handle_t io) {
Andy Hung264fa4c2024-02-21 15:52:12 -08001489 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Eric Laurent6c796322019-04-09 14:13:17 -07001490 if (aps == 0) return PERMISSION_DENIED;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001491
1492 std::vector<int32_t> idsAidl = VALUE_OR_RETURN_STATUS(
1493 convertContainer<std::vector<int32_t>>(ids, convertReinterpret<int32_t, int>));
1494 int32_t ioAidl = VALUE_OR_RETURN_STATUS(legacy2aidl_audio_io_handle_t_int32_t(io));
1495 return statusTFromBinderStatus(aps->moveEffectsToIo(idsAidl, ioAidl));
Eric Laurent6c796322019-04-09 14:13:17 -07001496}
1497
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001498status_t AudioSystem::isStreamActive(audio_stream_type_t stream, bool* state, uint32_t inPastMs) {
Andy Hung264fa4c2024-02-21 15:52:12 -08001499 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Eric Laurenteda6c362011-02-02 09:33:30 -08001500 if (aps == 0) return PERMISSION_DENIED;
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001501 if (state == NULL) return BAD_VALUE;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001502
Mikhail Naganovdbf03642021-08-25 18:15:32 -07001503 AudioStreamType streamAidl = VALUE_OR_RETURN_STATUS(
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001504 legacy2aidl_audio_stream_type_t_AudioStreamType(stream));
1505 int32_t inPastMsAidl = VALUE_OR_RETURN_STATUS(convertIntegral<int32_t>(inPastMs));
1506 RETURN_STATUS_IF_ERROR(statusTFromBinderStatus(
1507 aps->isStreamActive(streamAidl, inPastMsAidl, state)));
1508 return OK;
Eric Laurenteda6c362011-02-02 09:33:30 -08001509}
1510
Jean-Michel Trivi272ab542013-02-04 16:26:02 -08001511status_t AudioSystem::isStreamActiveRemotely(audio_stream_type_t stream, bool* state,
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001512 uint32_t inPastMs) {
Andy Hung264fa4c2024-02-21 15:52:12 -08001513 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Jean-Michel Trivi272ab542013-02-04 16:26:02 -08001514 if (aps == 0) return PERMISSION_DENIED;
1515 if (state == NULL) return BAD_VALUE;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001516
Mikhail Naganovdbf03642021-08-25 18:15:32 -07001517 AudioStreamType streamAidl = VALUE_OR_RETURN_STATUS(
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001518 legacy2aidl_audio_stream_type_t_AudioStreamType(stream));
1519 int32_t inPastMsAidl = VALUE_OR_RETURN_STATUS(convertIntegral<int32_t>(inPastMs));
1520 RETURN_STATUS_IF_ERROR(statusTFromBinderStatus(
1521 aps->isStreamActiveRemotely(streamAidl, inPastMsAidl, state)));
1522 return OK;
Jean-Michel Trivi272ab542013-02-04 16:26:02 -08001523}
1524
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001525status_t AudioSystem::isSourceActive(audio_source_t stream, bool* state) {
Andy Hung264fa4c2024-02-21 15:52:12 -08001526 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Jean-Michel Trivid7086032012-10-10 12:11:16 -07001527 if (aps == 0) return PERMISSION_DENIED;
1528 if (state == NULL) return BAD_VALUE;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001529
Mikhail Naganovddceecc2021-09-03 13:58:56 -07001530 AudioSource streamAidl = VALUE_OR_RETURN_STATUS(
1531 legacy2aidl_audio_source_t_AudioSource(stream));
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001532 RETURN_STATUS_IF_ERROR(statusTFromBinderStatus(
1533 aps->isSourceActive(streamAidl, state)));
1534 return OK;
Jean-Michel Trivid7086032012-10-10 12:11:16 -07001535}
1536
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001537uint32_t AudioSystem::getPrimaryOutputSamplingRate() {
Andy Hung264fa4c2024-02-21 15:52:12 -08001538 const sp<IAudioFlinger> af = get_audio_flinger();
Glenn Kastencc0f1cf2012-09-24 11:27:18 -07001539 if (af == 0) return 0;
1540 return af->getPrimaryOutputSamplingRate();
1541}
1542
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001543size_t AudioSystem::getPrimaryOutputFrameCount() {
Andy Hung264fa4c2024-02-21 15:52:12 -08001544 const sp<IAudioFlinger> af = get_audio_flinger();
Glenn Kastencc0f1cf2012-09-24 11:27:18 -07001545 if (af == 0) return 0;
1546 return af->getPrimaryOutputFrameCount();
1547}
Eric Laurenteda6c362011-02-02 09:33:30 -08001548
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001549status_t AudioSystem::setLowRamDevice(bool isLowRamDevice, int64_t totalMemory) {
Andy Hung264fa4c2024-02-21 15:52:12 -08001550 const sp<IAudioFlinger> af = get_audio_flinger();
Glenn Kasten4182c4e2013-07-15 14:45:07 -07001551 if (af == 0) return PERMISSION_DENIED;
Andy Hung6f248bb2018-01-23 14:04:37 -08001552 return af->setLowRamDevice(isLowRamDevice, totalMemory);
Glenn Kasten4182c4e2013-07-15 14:45:07 -07001553}
1554
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001555void AudioSystem::clearAudioConfigCache() {
Glenn Kastend2d089f2014-11-05 11:48:12 -08001556 // called by restoreTrack_l(), which needs new IAudioFlinger and IAudioPolicyService instances
Steve Block3856b092011-10-20 11:56:00 +01001557 ALOGV("clearAudioConfigCache()");
Andy Hungda1fb072024-02-20 19:08:08 -08001558 gAudioFlingerServiceHandler.clearService();
Mikhail Naganovec3d5792022-05-06 00:19:55 +00001559 clearAudioPolicyService();
Eric Laurent9f6530f2011-08-30 10:18:54 -07001560}
1561
Hayden Gomes524159d2019-12-23 14:41:47 -08001562status_t AudioSystem::setSupportedSystemUsages(const std::vector<audio_usage_t>& systemUsages) {
Andy Hung264fa4c2024-02-21 15:52:12 -08001563 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Hayden Gomes524159d2019-12-23 14:41:47 -08001564 if (aps == nullptr) return PERMISSION_DENIED;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001565
Mikhail Naganovdbf03642021-08-25 18:15:32 -07001566 std::vector<AudioUsage> systemUsagesAidl = VALUE_OR_RETURN_STATUS(
1567 convertContainer<std::vector<AudioUsage>>(systemUsages,
1568 legacy2aidl_audio_usage_t_AudioUsage));
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001569 return statusTFromBinderStatus(aps->setSupportedSystemUsages(systemUsagesAidl));
Hayden Gomes524159d2019-12-23 14:41:47 -08001570}
1571
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001572status_t AudioSystem::setAllowedCapturePolicy(uid_t uid, audio_flags_mask_t capturePolicy) {
Andy Hung264fa4c2024-02-21 15:52:12 -08001573 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Kevin Rocardb99cc752019-03-21 20:52:24 -07001574 if (aps == nullptr) return PERMISSION_DENIED;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001575
1576 int32_t uidAidl = VALUE_OR_RETURN_STATUS(legacy2aidl_uid_t_int32_t(uid));
1577 int32_t capturePolicyAidl = VALUE_OR_RETURN_STATUS(
1578 legacy2aidl_audio_flags_mask_t_int32_t_mask(capturePolicy));
1579 return statusTFromBinderStatus(aps->setAllowedCapturePolicy(uidAidl, capturePolicyAidl));
Kevin Rocardb99cc752019-03-21 20:52:24 -07001580}
1581
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001582audio_offload_mode_t AudioSystem::getOffloadSupport(const audio_offload_info_t& info) {
Eric Laurent90fe31c2020-11-26 20:06:35 +01001583 ALOGV("%s", __func__);
Andy Hung264fa4c2024-02-21 15:52:12 -08001584 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Eric Laurent90fe31c2020-11-26 20:06:35 +01001585 if (aps == 0) return AUDIO_OFFLOAD_NOT_SUPPORTED;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001586
1587 auto result = [&]() -> ConversionResult<audio_offload_mode_t> {
Mikhail Naganovdbf03642021-08-25 18:15:32 -07001588 AudioOffloadInfo infoAidl = VALUE_OR_RETURN(
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001589 legacy2aidl_audio_offload_info_t_AudioOffloadInfo(info));
1590 media::AudioOffloadMode retAidl;
1591 RETURN_IF_ERROR(
1592 statusTFromBinderStatus(aps->getOffloadSupport(infoAidl, &retAidl)));
1593 return aidl2legacy_AudioOffloadMode_audio_offload_mode_t(retAidl);
1594 }();
1595
1596 return result.value_or(static_cast<audio_offload_mode_t>(0));
Richard Fitzgeraldad3af332013-03-25 16:54:37 +00001597}
1598
Eric Laurent203b1a12014-04-01 10:34:16 -07001599status_t AudioSystem::listAudioPorts(audio_port_role_t role,
1600 audio_port_type_t type,
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001601 unsigned int* num_ports,
1602 struct audio_port_v7* ports,
1603 unsigned int* generation) {
1604 if (num_ports == nullptr || (*num_ports != 0 && ports == nullptr) ||
1605 generation == nullptr) {
1606 return BAD_VALUE;
1607 }
1608
Andy Hung264fa4c2024-02-21 15:52:12 -08001609 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Eric Laurent203b1a12014-04-01 10:34:16 -07001610 if (aps == 0) return PERMISSION_DENIED;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001611
1612 media::AudioPortRole roleAidl = VALUE_OR_RETURN_STATUS(
1613 legacy2aidl_audio_port_role_t_AudioPortRole(role));
1614 media::AudioPortType typeAidl = VALUE_OR_RETURN_STATUS(
1615 legacy2aidl_audio_port_type_t_AudioPortType(type));
Mikhail Naganov0078ee52021-09-30 23:06:20 +00001616 Int numPortsAidl;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001617 numPortsAidl.value = VALUE_OR_RETURN_STATUS(convertIntegral<int32_t>(*num_ports));
Atneya Nair638a6e42022-12-18 16:45:15 -08001618 std::vector<media::AudioPortFw> portsAidl;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001619 int32_t generationAidl;
1620
1621 RETURN_STATUS_IF_ERROR(statusTFromBinderStatus(
1622 aps->listAudioPorts(roleAidl, typeAidl, &numPortsAidl, &portsAidl, &generationAidl)));
1623 *num_ports = VALUE_OR_RETURN_STATUS(convertIntegral<unsigned int>(numPortsAidl.value));
1624 *generation = VALUE_OR_RETURN_STATUS(convertIntegral<unsigned int>(generationAidl));
1625 RETURN_STATUS_IF_ERROR(convertRange(portsAidl.begin(), portsAidl.end(), ports,
Mikhail Naganov87227252023-01-13 17:38:10 -08001626 aidl2legacy_AudioPortFw_audio_port_v7));
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001627 return OK;
Eric Laurent203b1a12014-04-01 10:34:16 -07001628}
1629
Mikhail Naganov5edc5ed2023-03-23 14:52:15 -07001630status_t AudioSystem::listDeclaredDevicePorts(media::AudioPortRole role,
1631 std::vector<media::AudioPortFw>* result) {
1632 if (result == nullptr) return BAD_VALUE;
Andy Hung264fa4c2024-02-21 15:52:12 -08001633 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Mikhail Naganov5edc5ed2023-03-23 14:52:15 -07001634 if (aps == 0) return PERMISSION_DENIED;
1635 RETURN_STATUS_IF_ERROR(statusTFromBinderStatus(aps->listDeclaredDevicePorts(role, result)));
1636 return OK;
1637}
1638
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001639status_t AudioSystem::getAudioPort(struct audio_port_v7* port) {
1640 if (port == nullptr) {
1641 return BAD_VALUE;
1642 }
Andy Hung264fa4c2024-02-21 15:52:12 -08001643 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Eric Laurent203b1a12014-04-01 10:34:16 -07001644 if (aps == 0) return PERMISSION_DENIED;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001645
Atneya Nair638a6e42022-12-18 16:45:15 -08001646 media::AudioPortFw portAidl;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001647 RETURN_STATUS_IF_ERROR(
Mikhail Naganov17031562022-02-23 23:00:27 +00001648 statusTFromBinderStatus(aps->getAudioPort(port->id, &portAidl)));
Mikhail Naganov87227252023-01-13 17:38:10 -08001649 *port = VALUE_OR_RETURN_STATUS(aidl2legacy_AudioPortFw_audio_port_v7(portAidl));
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001650 return OK;
Eric Laurent203b1a12014-04-01 10:34:16 -07001651}
1652
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001653status_t AudioSystem::createAudioPatch(const struct audio_patch* patch,
1654 audio_patch_handle_t* handle) {
1655 if (patch == nullptr || handle == nullptr) {
1656 return BAD_VALUE;
1657 }
1658
Andy Hung264fa4c2024-02-21 15:52:12 -08001659 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Eric Laurent203b1a12014-04-01 10:34:16 -07001660 if (aps == 0) return PERMISSION_DENIED;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001661
Atneya Nair3afdbd12022-12-18 16:14:18 -08001662 media::AudioPatchFw patchAidl = VALUE_OR_RETURN_STATUS(
Mikhail Naganov87227252023-01-13 17:38:10 -08001663 legacy2aidl_audio_patch_AudioPatchFw(*patch));
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001664 int32_t handleAidl = VALUE_OR_RETURN_STATUS(legacy2aidl_audio_patch_handle_t_int32_t(*handle));
1665 RETURN_STATUS_IF_ERROR(
1666 statusTFromBinderStatus(aps->createAudioPatch(patchAidl, handleAidl, &handleAidl)));
1667 *handle = VALUE_OR_RETURN_STATUS(aidl2legacy_int32_t_audio_patch_handle_t(handleAidl));
1668 return OK;
Eric Laurent203b1a12014-04-01 10:34:16 -07001669}
1670
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001671status_t AudioSystem::releaseAudioPatch(audio_patch_handle_t handle) {
Andy Hung264fa4c2024-02-21 15:52:12 -08001672 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Eric Laurent203b1a12014-04-01 10:34:16 -07001673 if (aps == 0) return PERMISSION_DENIED;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001674
1675 int32_t handleAidl = VALUE_OR_RETURN_STATUS(legacy2aidl_audio_patch_handle_t_int32_t(handle));
1676 return statusTFromBinderStatus(aps->releaseAudioPatch(handleAidl));
Eric Laurent203b1a12014-04-01 10:34:16 -07001677}
1678
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001679status_t AudioSystem::listAudioPatches(unsigned int* num_patches,
1680 struct audio_patch* patches,
1681 unsigned int* generation) {
1682 if (num_patches == nullptr || (*num_patches != 0 && patches == nullptr) ||
1683 generation == nullptr) {
1684 return BAD_VALUE;
1685 }
1686
Andy Hung264fa4c2024-02-21 15:52:12 -08001687 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Eric Laurent203b1a12014-04-01 10:34:16 -07001688 if (aps == 0) return PERMISSION_DENIED;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001689
1690
Mikhail Naganov0078ee52021-09-30 23:06:20 +00001691 Int numPatchesAidl;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001692 numPatchesAidl.value = VALUE_OR_RETURN_STATUS(convertIntegral<int32_t>(*num_patches));
Atneya Nair3afdbd12022-12-18 16:14:18 -08001693 std::vector<media::AudioPatchFw> patchesAidl;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001694 int32_t generationAidl;
1695
1696 RETURN_STATUS_IF_ERROR(statusTFromBinderStatus(
1697 aps->listAudioPatches(&numPatchesAidl, &patchesAidl, &generationAidl)));
1698 *num_patches = VALUE_OR_RETURN_STATUS(convertIntegral<unsigned int>(numPatchesAidl.value));
1699 *generation = VALUE_OR_RETURN_STATUS(convertIntegral<unsigned int>(generationAidl));
1700 RETURN_STATUS_IF_ERROR(convertRange(patchesAidl.begin(), patchesAidl.end(), patches,
Mikhail Naganov87227252023-01-13 17:38:10 -08001701 aidl2legacy_AudioPatchFw_audio_patch));
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001702 return OK;
Eric Laurent203b1a12014-04-01 10:34:16 -07001703}
1704
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001705status_t AudioSystem::setAudioPortConfig(const struct audio_port_config* config) {
1706 if (config == nullptr) {
1707 return BAD_VALUE;
1708 }
1709
Andy Hung264fa4c2024-02-21 15:52:12 -08001710 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Eric Laurent203b1a12014-04-01 10:34:16 -07001711 if (aps == 0) return PERMISSION_DENIED;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001712
Atneya Nair7a9594f2022-12-18 17:26:26 -08001713 media::AudioPortConfigFw configAidl = VALUE_OR_RETURN_STATUS(
Mikhail Naganov87227252023-01-13 17:38:10 -08001714 legacy2aidl_audio_port_config_AudioPortConfigFw(*config));
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001715 return statusTFromBinderStatus(aps->setAudioPortConfig(configAidl));
Eric Laurent203b1a12014-04-01 10:34:16 -07001716}
1717
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001718status_t AudioSystem::addAudioPortCallback(const sp<AudioPortCallback>& callback) {
Andy Hung264fa4c2024-02-21 15:52:12 -08001719 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Eric Laurentb28753e2015-04-01 13:06:28 -07001720 if (aps == 0) return PERMISSION_DENIED;
Andy Hung88e71732024-02-21 16:44:27 -08001721 const auto apc = gAudioPolicyServiceHandler.getClient();
1722 if (apc == nullptr) return NO_INIT;
Eric Laurentb28753e2015-04-01 13:06:28 -07001723
Andy Hung88e71732024-02-21 16:44:27 -08001724 std::lock_guard _l(gApsCallbackMutex);
1725 const int ret = apc->addAudioPortCallback(callback);
Eric Laurente8726fe2015-06-26 09:39:24 -07001726 if (ret == 1) {
1727 aps->setAudioPortCallbacksEnabled(true);
1728 }
1729 return (ret < 0) ? INVALID_OPERATION : NO_ERROR;
Eric Laurentb52c1522014-05-20 11:27:36 -07001730}
1731
Jean-Michel Trivif613d422015-04-23 18:41:29 -07001732/*static*/
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001733status_t AudioSystem::removeAudioPortCallback(const sp<AudioPortCallback>& callback) {
Andy Hung264fa4c2024-02-21 15:52:12 -08001734 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Eric Laurentb28753e2015-04-01 13:06:28 -07001735 if (aps == 0) return PERMISSION_DENIED;
Andy Hung88e71732024-02-21 16:44:27 -08001736 const auto apc = gAudioPolicyServiceHandler.getClient();
1737 if (apc == nullptr) return NO_INIT;
Eric Laurentb28753e2015-04-01 13:06:28 -07001738
Andy Hung88e71732024-02-21 16:44:27 -08001739 std::lock_guard _l(gApsCallbackMutex);
1740 const int ret = apc->removeAudioPortCallback(callback);
Eric Laurente8726fe2015-06-26 09:39:24 -07001741 if (ret == 0) {
1742 aps->setAudioPortCallbacksEnabled(false);
1743 }
1744 return (ret < 0) ? INVALID_OPERATION : NO_ERROR;
Eric Laurent296fb132015-05-01 11:38:42 -07001745}
1746
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001747status_t AudioSystem::addAudioVolumeGroupCallback(const sp<AudioVolumeGroupCallback>& callback) {
Andy Hung264fa4c2024-02-21 15:52:12 -08001748 const sp<IAudioPolicyService> aps = get_audio_policy_service();
François Gaffiecfe17322018-11-07 13:41:29 +01001749 if (aps == 0) return PERMISSION_DENIED;
Andy Hung88e71732024-02-21 16:44:27 -08001750 const auto apc = gAudioPolicyServiceHandler.getClient();
1751 if (apc == nullptr) return NO_INIT;
François Gaffiecfe17322018-11-07 13:41:29 +01001752
Andy Hung88e71732024-02-21 16:44:27 -08001753 std::lock_guard _l(gApsCallbackMutex);
1754 const int ret = apc->addAudioVolumeGroupCallback(callback);
François Gaffiecfe17322018-11-07 13:41:29 +01001755 if (ret == 1) {
1756 aps->setAudioVolumeGroupCallbacksEnabled(true);
1757 }
1758 return (ret < 0) ? INVALID_OPERATION : NO_ERROR;
1759}
1760
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001761status_t AudioSystem::removeAudioVolumeGroupCallback(const sp<AudioVolumeGroupCallback>& callback) {
Andy Hung264fa4c2024-02-21 15:52:12 -08001762 const sp<IAudioPolicyService> aps = get_audio_policy_service();
François Gaffiecfe17322018-11-07 13:41:29 +01001763 if (aps == 0) return PERMISSION_DENIED;
Andy Hung88e71732024-02-21 16:44:27 -08001764 const auto apc = gAudioPolicyServiceHandler.getClient();
1765 if (apc == nullptr) return NO_INIT;
François Gaffiecfe17322018-11-07 13:41:29 +01001766
Andy Hung88e71732024-02-21 16:44:27 -08001767 std::lock_guard _l(gApsCallbackMutex);
1768 const int ret = apc->removeAudioVolumeGroupCallback(callback);
François Gaffiecfe17322018-11-07 13:41:29 +01001769 if (ret == 0) {
1770 aps->setAudioVolumeGroupCallbacksEnabled(false);
1771 }
1772 return (ret < 0) ? INVALID_OPERATION : NO_ERROR;
1773}
1774
Eric Laurent296fb132015-05-01 11:38:42 -07001775status_t AudioSystem::addAudioDeviceCallback(
Eric Laurent09f1ed22019-04-24 17:45:17 -07001776 const wp<AudioDeviceCallback>& callback, audio_io_handle_t audioIo,
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001777 audio_port_handle_t portId) {
Eric Laurent296fb132015-05-01 11:38:42 -07001778 const sp<AudioFlingerClient> afc = getAudioFlingerClient();
1779 if (afc == 0) {
1780 return NO_INIT;
1781 }
Eric Laurent09f1ed22019-04-24 17:45:17 -07001782 status_t status = afc->addAudioDeviceCallback(callback, audioIo, portId);
Eric Laurent7c1ec5f2015-07-09 14:52:47 -07001783 if (status == NO_ERROR) {
Andy Hung264fa4c2024-02-21 15:52:12 -08001784 const sp<IAudioFlinger> af = get_audio_flinger();
Eric Laurent7c1ec5f2015-07-09 14:52:47 -07001785 if (af != 0) {
1786 af->registerClient(afc);
1787 }
1788 }
1789 return status;
Eric Laurent296fb132015-05-01 11:38:42 -07001790}
1791
1792status_t AudioSystem::removeAudioDeviceCallback(
Eric Laurent09f1ed22019-04-24 17:45:17 -07001793 const wp<AudioDeviceCallback>& callback, audio_io_handle_t audioIo,
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001794 audio_port_handle_t portId) {
Eric Laurent296fb132015-05-01 11:38:42 -07001795 const sp<AudioFlingerClient> afc = getAudioFlingerClient();
1796 if (afc == 0) {
1797 return NO_INIT;
1798 }
Eric Laurent09f1ed22019-04-24 17:45:17 -07001799 return afc->removeAudioDeviceCallback(callback, audioIo, portId);
Eric Laurent296fb132015-05-01 11:38:42 -07001800}
1801
Eric Laurent076e7c72022-05-03 18:12:28 +02001802status_t AudioSystem::addSupportedLatencyModesCallback(
1803 const sp<SupportedLatencyModesCallback>& callback) {
1804 const sp<AudioFlingerClient> afc = getAudioFlingerClient();
1805 if (afc == 0) {
1806 return NO_INIT;
1807 }
1808 return afc->addSupportedLatencyModesCallback(callback);
1809}
1810
1811status_t AudioSystem::removeSupportedLatencyModesCallback(
1812 const sp<SupportedLatencyModesCallback>& callback) {
1813 const sp<AudioFlingerClient> afc = getAudioFlingerClient();
1814 if (afc == 0) {
1815 return NO_INIT;
1816 }
1817 return afc->removeSupportedLatencyModesCallback(callback);
1818}
1819
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001820audio_port_handle_t AudioSystem::getDeviceIdForIo(audio_io_handle_t audioIo) {
Andy Hung264fa4c2024-02-21 15:52:12 -08001821 const sp<IAudioFlinger> af = get_audio_flinger();
Eric Laurent296fb132015-05-01 11:38:42 -07001822 if (af == 0) return PERMISSION_DENIED;
1823 const sp<AudioIoDescriptor> desc = getIoDescriptor(audioIo);
1824 if (desc == 0) {
1825 return AUDIO_PORT_HANDLE_NONE;
1826 }
1827 return desc->getDeviceId();
Eric Laurentb28753e2015-04-01 13:06:28 -07001828}
1829
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001830status_t AudioSystem::acquireSoundTriggerSession(audio_session_t* session,
1831 audio_io_handle_t* ioHandle,
1832 audio_devices_t* device) {
1833 if (session == nullptr || ioHandle == nullptr || device == nullptr) {
1834 return BAD_VALUE;
1835 }
Andy Hung264fa4c2024-02-21 15:52:12 -08001836 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Eric Laurentdf3dc7e2014-07-27 18:39:40 -07001837 if (aps == 0) return PERMISSION_DENIED;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001838
1839 media::SoundTriggerSession retAidl;
1840 RETURN_STATUS_IF_ERROR(
1841 statusTFromBinderStatus(aps->acquireSoundTriggerSession(&retAidl)));
1842 *session = VALUE_OR_RETURN_STATUS(aidl2legacy_int32_t_audio_session_t(retAidl.session));
1843 *ioHandle = VALUE_OR_RETURN_STATUS(aidl2legacy_int32_t_audio_io_handle_t(retAidl.ioHandle));
Mikhail Naganov21a32ec2021-07-08 14:40:12 -07001844 *device = VALUE_OR_RETURN_STATUS(
1845 aidl2legacy_AudioDeviceDescription_audio_devices_t(retAidl.device));
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001846 return OK;
Eric Laurentdf3dc7e2014-07-27 18:39:40 -07001847}
1848
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001849status_t AudioSystem::releaseSoundTriggerSession(audio_session_t session) {
Andy Hung264fa4c2024-02-21 15:52:12 -08001850 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Eric Laurentdf3dc7e2014-07-27 18:39:40 -07001851 if (aps == 0) return PERMISSION_DENIED;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001852
1853 int32_t sessionAidl = VALUE_OR_RETURN_STATUS(legacy2aidl_audio_session_t_int32_t(session));
1854 return statusTFromBinderStatus(aps->releaseSoundTriggerSession(sessionAidl));
Eric Laurentdf3dc7e2014-07-27 18:39:40 -07001855}
Eric Laurentbb6c9a02014-09-25 14:11:47 -07001856
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001857audio_mode_t AudioSystem::getPhoneState() {
Andy Hung264fa4c2024-02-21 15:52:12 -08001858 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Eric Laurentbb6c9a02014-09-25 14:11:47 -07001859 if (aps == 0) return AUDIO_MODE_INVALID;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001860
1861 auto result = [&]() -> ConversionResult<audio_mode_t> {
Mikhail Naganovddceecc2021-09-03 13:58:56 -07001862 media::audio::common::AudioMode retAidl;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001863 RETURN_IF_ERROR(statusTFromBinderStatus(aps->getPhoneState(&retAidl)));
1864 return aidl2legacy_AudioMode_audio_mode_t(retAidl);
1865 }();
1866
1867 return result.value_or(AUDIO_MODE_INVALID);
Eric Laurentbb6c9a02014-09-25 14:11:47 -07001868}
1869
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001870status_t AudioSystem::registerPolicyMixes(const Vector<AudioMix>& mixes, bool registration) {
Andy Hung264fa4c2024-02-21 15:52:12 -08001871 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Eric Laurentbaac1832014-12-01 17:52:59 -08001872 if (aps == 0) return PERMISSION_DENIED;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001873
1874 size_t mixesSize = std::min(mixes.size(), size_t{MAX_MIXES_PER_POLICY});
1875 std::vector<media::AudioMix> mixesAidl;
1876 RETURN_STATUS_IF_ERROR(
1877 convertRange(mixes.begin(), mixes.begin() + mixesSize, std::back_inserter(mixesAidl),
1878 legacy2aidl_AudioMix));
1879 return statusTFromBinderStatus(aps->registerPolicyMixes(mixesAidl, registration));
Eric Laurentbaac1832014-12-01 17:52:59 -08001880}
Eric Laurentbb6c9a02014-09-25 14:11:47 -07001881
Marvin Raminbdefaf02023-11-01 09:10:32 +01001882status_t AudioSystem::getRegisteredPolicyMixes(std::vector<AudioMix>& mixes) {
1883 if (!audio_flags::audio_mix_test_api()) {
1884 return INVALID_OPERATION;
1885 }
1886
1887 const sp<IAudioPolicyService> aps = AudioSystem::get_audio_policy_service();
1888 if (aps == nullptr) return PERMISSION_DENIED;
1889
1890 std::vector<::android::media::AudioMix> aidlMixes;
1891 Status status = aps->getRegisteredPolicyMixes(&aidlMixes);
1892
1893 for (const auto& aidlMix : aidlMixes) {
1894 AudioMix mix = VALUE_OR_RETURN_STATUS(aidl2legacy_AudioMix(aidlMix));
1895 mixes.push_back(mix);
1896 }
1897
1898 return statusTFromBinderStatus(status);
1899}
1900
Jan Sebechlebsky0af8e872023-08-11 14:45:08 +02001901status_t AudioSystem::updatePolicyMixes(
1902 const std::vector<std::pair<AudioMix, std::vector<AudioMixMatchCriterion>>>&
1903 mixesWithUpdates) {
Andy Hung264fa4c2024-02-21 15:52:12 -08001904 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Jan Sebechlebsky0af8e872023-08-11 14:45:08 +02001905 if (aps == 0) return PERMISSION_DENIED;
1906
1907 std::vector<media::AudioMixUpdate> updatesAidl;
1908 updatesAidl.reserve(mixesWithUpdates.size());
1909
1910 for (const auto& update : mixesWithUpdates) {
1911 media::AudioMixUpdate updateAidl;
1912 updateAidl.audioMix = VALUE_OR_RETURN_STATUS(legacy2aidl_AudioMix(update.first));
1913 RETURN_STATUS_IF_ERROR(convertRange(update.second.begin(), update.second.end(),
1914 std::back_inserter(updateAidl.newCriteria),
1915 legacy2aidl_AudioMixMatchCriterion));
1916 updatesAidl.emplace_back(updateAidl);
1917 }
1918
1919 return statusTFromBinderStatus(aps->updatePolicyMixes(updatesAidl));
1920}
1921
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001922status_t AudioSystem::setUidDeviceAffinities(uid_t uid, const AudioDeviceTypeAddrVector& devices) {
Andy Hung264fa4c2024-02-21 15:52:12 -08001923 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Jean-Michel Trivibda70da2018-12-19 07:30:15 -08001924 if (aps == 0) return PERMISSION_DENIED;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001925
1926 int32_t uidAidl = VALUE_OR_RETURN_STATUS(legacy2aidl_uid_t_int32_t(uid));
Mikhail Naganovf4a75362021-09-16 00:02:54 +00001927 std::vector<AudioDevice> devicesAidl = VALUE_OR_RETURN_STATUS(
1928 convertContainer<std::vector<AudioDevice>>(devices,
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001929 legacy2aidl_AudioDeviceTypeAddress));
1930 return statusTFromBinderStatus(aps->setUidDeviceAffinities(uidAidl, devicesAidl));
Jean-Michel Trivibda70da2018-12-19 07:30:15 -08001931}
1932
1933status_t AudioSystem::removeUidDeviceAffinities(uid_t uid) {
Andy Hung264fa4c2024-02-21 15:52:12 -08001934 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Jean-Michel Trivibda70da2018-12-19 07:30:15 -08001935 if (aps == 0) return PERMISSION_DENIED;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001936
1937 int32_t uidAidl = VALUE_OR_RETURN_STATUS(legacy2aidl_uid_t_int32_t(uid));
1938 return statusTFromBinderStatus(aps->removeUidDeviceAffinities(uidAidl));
Jean-Michel Trivibda70da2018-12-19 07:30:15 -08001939}
1940
Oscar Azucena90e77632019-11-27 17:12:28 -08001941status_t AudioSystem::setUserIdDeviceAffinities(int userId,
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001942 const AudioDeviceTypeAddrVector& devices) {
Andy Hung264fa4c2024-02-21 15:52:12 -08001943 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Oscar Azucena90e77632019-11-27 17:12:28 -08001944 if (aps == 0) return PERMISSION_DENIED;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001945
1946 int32_t userIdAidl = VALUE_OR_RETURN_STATUS(convertReinterpret<int32_t>(userId));
Mikhail Naganovf4a75362021-09-16 00:02:54 +00001947 std::vector<AudioDevice> devicesAidl = VALUE_OR_RETURN_STATUS(
1948 convertContainer<std::vector<AudioDevice>>(devices,
1949 legacy2aidl_AudioDeviceTypeAddress));
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001950 return statusTFromBinderStatus(
1951 aps->setUserIdDeviceAffinities(userIdAidl, devicesAidl));
Oscar Azucena90e77632019-11-27 17:12:28 -08001952}
1953
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001954status_t AudioSystem::removeUserIdDeviceAffinities(int userId) {
Andy Hung264fa4c2024-02-21 15:52:12 -08001955 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Oscar Azucena90e77632019-11-27 17:12:28 -08001956 if (aps == 0) return PERMISSION_DENIED;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001957 int32_t userIdAidl = VALUE_OR_RETURN_STATUS(convertReinterpret<int32_t>(userId));
1958 return statusTFromBinderStatus(aps->removeUserIdDeviceAffinities(userIdAidl));
Oscar Azucena90e77632019-11-27 17:12:28 -08001959}
1960
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001961status_t AudioSystem::startAudioSource(const struct audio_port_config* source,
1962 const audio_attributes_t* attributes,
1963 audio_port_handle_t* portId) {
1964 if (source == nullptr || attributes == nullptr || portId == nullptr) {
1965 return BAD_VALUE;
1966 }
Andy Hung264fa4c2024-02-21 15:52:12 -08001967 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Eric Laurent554a2772015-04-10 11:29:24 -07001968 if (aps == 0) return PERMISSION_DENIED;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001969
Atneya Nair7a9594f2022-12-18 17:26:26 -08001970 media::AudioPortConfigFw sourceAidl = VALUE_OR_RETURN_STATUS(
Mikhail Naganov87227252023-01-13 17:38:10 -08001971 legacy2aidl_audio_port_config_AudioPortConfigFw(*source));
Mikhail Naganov1c400902023-05-17 11:48:43 -07001972 media::audio::common::AudioAttributes attributesAidl = VALUE_OR_RETURN_STATUS(
1973 legacy2aidl_audio_attributes_t_AudioAttributes(*attributes));
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001974 int32_t portIdAidl;
1975 RETURN_STATUS_IF_ERROR(statusTFromBinderStatus(
1976 aps->startAudioSource(sourceAidl, attributesAidl, &portIdAidl)));
1977 *portId = VALUE_OR_RETURN_STATUS(aidl2legacy_int32_t_audio_port_handle_t(portIdAidl));
1978 return OK;
Eric Laurent554a2772015-04-10 11:29:24 -07001979}
1980
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001981status_t AudioSystem::stopAudioSource(audio_port_handle_t portId) {
Andy Hung264fa4c2024-02-21 15:52:12 -08001982 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Eric Laurent554a2772015-04-10 11:29:24 -07001983 if (aps == 0) return PERMISSION_DENIED;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001984
1985 int32_t portIdAidl = VALUE_OR_RETURN_STATUS(legacy2aidl_audio_port_handle_t_int32_t(portId));
1986 return statusTFromBinderStatus(aps->stopAudioSource(portIdAidl));
Eric Laurent554a2772015-04-10 11:29:24 -07001987}
1988
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001989status_t AudioSystem::setMasterMono(bool mono) {
Andy Hung264fa4c2024-02-21 15:52:12 -08001990 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Andy Hung2ddee192015-12-18 17:34:44 -08001991 if (aps == 0) return PERMISSION_DENIED;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001992 return statusTFromBinderStatus(aps->setMasterMono(mono));
Andy Hung2ddee192015-12-18 17:34:44 -08001993}
1994
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001995status_t AudioSystem::getMasterMono(bool* mono) {
1996 if (mono == nullptr) {
1997 return BAD_VALUE;
1998 }
Andy Hung264fa4c2024-02-21 15:52:12 -08001999 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Andy Hung2ddee192015-12-18 17:34:44 -08002000 if (aps == 0) return PERMISSION_DENIED;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002001 return statusTFromBinderStatus(aps->getMasterMono(mono));
Andy Hung2ddee192015-12-18 17:34:44 -08002002}
2003
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002004status_t AudioSystem::setMasterBalance(float balance) {
Andy Hung264fa4c2024-02-21 15:52:12 -08002005 const sp<IAudioFlinger> af = get_audio_flinger();
Richard Folke Tullberg3fae0372017-01-13 09:04:25 +01002006 if (af == 0) return PERMISSION_DENIED;
2007 return af->setMasterBalance(balance);
2008}
2009
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002010status_t AudioSystem::getMasterBalance(float* balance) {
Andy Hung264fa4c2024-02-21 15:52:12 -08002011 const sp<IAudioFlinger> af = get_audio_flinger();
Richard Folke Tullberg3fae0372017-01-13 09:04:25 +01002012 if (af == 0) return PERMISSION_DENIED;
2013 return af->getMasterBalance(balance);
2014}
2015
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002016float
2017AudioSystem::getStreamVolumeDB(audio_stream_type_t stream, int index, audio_devices_t device) {
Andy Hung264fa4c2024-02-21 15:52:12 -08002018 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Eric Laurentac9cef52017-06-09 15:46:26 -07002019 if (aps == 0) return NAN;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002020
2021 auto result = [&]() -> ConversionResult<float> {
Mikhail Naganovdbf03642021-08-25 18:15:32 -07002022 AudioStreamType streamAidl = VALUE_OR_RETURN(
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002023 legacy2aidl_audio_stream_type_t_AudioStreamType(stream));
2024 int32_t indexAidl = VALUE_OR_RETURN(convertIntegral<int32_t>(index));
Mikhail Naganovf4a75362021-09-16 00:02:54 +00002025 AudioDeviceDescription deviceAidl = VALUE_OR_RETURN(
Mikhail Naganov21a32ec2021-07-08 14:40:12 -07002026 legacy2aidl_audio_devices_t_AudioDeviceDescription(device));
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002027 float retAidl;
2028 RETURN_IF_ERROR(statusTFromBinderStatus(
2029 aps->getStreamVolumeDB(streamAidl, indexAidl, deviceAidl, &retAidl)));
2030 return retAidl;
2031 }();
2032 return result.value_or(NAN);
Eric Laurentac9cef52017-06-09 15:46:26 -07002033}
2034
Mikhail Naganovd5d9de72023-02-13 11:45:03 -08002035status_t AudioSystem::getMicrophones(std::vector<media::MicrophoneInfoFw>* microphones) {
Andy Hung264fa4c2024-02-21 15:52:12 -08002036 const sp<IAudioFlinger> af = get_audio_flinger();
jiabin46a76fa2018-01-05 10:18:21 -08002037 if (af == 0) return PERMISSION_DENIED;
2038 return af->getMicrophones(microphones);
2039}
2040
Eric Laurent42896a02019-09-27 15:40:33 -07002041status_t AudioSystem::setAudioHalPids(const std::vector<pid_t>& pids) {
Andy Hung264fa4c2024-02-21 15:52:12 -08002042 const sp<IAudioFlinger> af = get_audio_flinger();
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002043 if (af == nullptr) return PERMISSION_DENIED;
2044 return af->setAudioHalPids(pids);
Eric Laurent42896a02019-09-27 15:40:33 -07002045}
2046
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002047status_t AudioSystem::getSurroundFormats(unsigned int* numSurroundFormats,
2048 audio_format_t* surroundFormats,
Kriti Dang6537def2021-03-02 13:46:59 +01002049 bool* surroundFormatsEnabled) {
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002050 if (numSurroundFormats == nullptr || (*numSurroundFormats != 0 &&
2051 (surroundFormats == nullptr ||
2052 surroundFormatsEnabled == nullptr))) {
2053 return BAD_VALUE;
2054 }
2055
Andy Hung264fa4c2024-02-21 15:52:12 -08002056 const sp<IAudioPolicyService> aps = get_audio_policy_service();
jiabin81772902018-04-02 17:52:27 -07002057 if (aps == 0) return PERMISSION_DENIED;
Mikhail Naganov0078ee52021-09-30 23:06:20 +00002058 Int numSurroundFormatsAidl;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002059 numSurroundFormatsAidl.value =
2060 VALUE_OR_RETURN_STATUS(convertIntegral<int32_t>(*numSurroundFormats));
Mikhail Naganov57bd06f2021-08-10 16:41:54 -07002061 std::vector<AudioFormatDescription> surroundFormatsAidl;
Kriti Dang877b27e2021-02-02 12:10:40 +01002062 std::vector<bool> surroundFormatsEnabledAidl;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002063 RETURN_STATUS_IF_ERROR(statusTFromBinderStatus(
Kriti Dang6537def2021-03-02 13:46:59 +01002064 aps->getSurroundFormats(&numSurroundFormatsAidl, &surroundFormatsAidl,
2065 &surroundFormatsEnabledAidl)));
Kriti Dang877b27e2021-02-02 12:10:40 +01002066
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002067 *numSurroundFormats = VALUE_OR_RETURN_STATUS(
2068 convertIntegral<unsigned int>(numSurroundFormatsAidl.value));
2069 RETURN_STATUS_IF_ERROR(
2070 convertRange(surroundFormatsAidl.begin(), surroundFormatsAidl.end(), surroundFormats,
Mikhail Naganovb60bd1b2021-07-15 17:31:43 -07002071 aidl2legacy_AudioFormatDescription_audio_format_t));
Kriti Dang877b27e2021-02-02 12:10:40 +01002072 std::copy(surroundFormatsEnabledAidl.begin(), surroundFormatsEnabledAidl.end(),
2073 surroundFormatsEnabled);
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002074 return OK;
jiabin81772902018-04-02 17:52:27 -07002075}
2076
Kriti Dang6537def2021-03-02 13:46:59 +01002077status_t AudioSystem::getReportedSurroundFormats(unsigned int* numSurroundFormats,
2078 audio_format_t* surroundFormats) {
2079 if (numSurroundFormats == nullptr || (*numSurroundFormats != 0 && surroundFormats == nullptr)) {
2080 return BAD_VALUE;
2081 }
2082
Andy Hung264fa4c2024-02-21 15:52:12 -08002083 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Kriti Dang6537def2021-03-02 13:46:59 +01002084 if (aps == 0) return PERMISSION_DENIED;
Mikhail Naganov0078ee52021-09-30 23:06:20 +00002085 Int numSurroundFormatsAidl;
Kriti Dang6537def2021-03-02 13:46:59 +01002086 numSurroundFormatsAidl.value =
2087 VALUE_OR_RETURN_STATUS(convertIntegral<int32_t>(*numSurroundFormats));
Mikhail Naganov57bd06f2021-08-10 16:41:54 -07002088 std::vector<AudioFormatDescription> surroundFormatsAidl;
Kriti Dang6537def2021-03-02 13:46:59 +01002089 RETURN_STATUS_IF_ERROR(statusTFromBinderStatus(
2090 aps->getReportedSurroundFormats(&numSurroundFormatsAidl, &surroundFormatsAidl)));
2091
2092 *numSurroundFormats = VALUE_OR_RETURN_STATUS(
2093 convertIntegral<unsigned int>(numSurroundFormatsAidl.value));
2094 RETURN_STATUS_IF_ERROR(
2095 convertRange(surroundFormatsAidl.begin(), surroundFormatsAidl.end(), surroundFormats,
Mikhail Naganovb60bd1b2021-07-15 17:31:43 -07002096 aidl2legacy_AudioFormatDescription_audio_format_t));
Kriti Dang6537def2021-03-02 13:46:59 +01002097 return OK;
2098}
2099
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002100status_t AudioSystem::setSurroundFormatEnabled(audio_format_t audioFormat, bool enabled) {
Andy Hung264fa4c2024-02-21 15:52:12 -08002101 const sp<IAudioPolicyService> aps = get_audio_policy_service();
jiabin81772902018-04-02 17:52:27 -07002102 if (aps == 0) return PERMISSION_DENIED;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002103
Mikhail Naganov57bd06f2021-08-10 16:41:54 -07002104 AudioFormatDescription audioFormatAidl = VALUE_OR_RETURN_STATUS(
Mikhail Naganovb60bd1b2021-07-15 17:31:43 -07002105 legacy2aidl_audio_format_t_AudioFormatDescription(audioFormat));
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002106 return statusTFromBinderStatus(
2107 aps->setSurroundFormatEnabled(audioFormatAidl, enabled));
jiabin81772902018-04-02 17:52:27 -07002108}
2109
Oscar Azucena829d90d2022-01-28 17:17:56 -08002110status_t AudioSystem::setAssistantServicesUids(const std::vector<uid_t>& uids) {
Andy Hung264fa4c2024-02-21 15:52:12 -08002111 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Eric Laurentb78763e2018-10-17 10:08:02 -07002112 if (aps == 0) return PERMISSION_DENIED;
2113
Oscar Azucena829d90d2022-01-28 17:17:56 -08002114 std::vector<int32_t> uidsAidl = VALUE_OR_RETURN_STATUS(
2115 convertContainer<std::vector<int32_t>>(uids, legacy2aidl_uid_t_int32_t));
2116 return statusTFromBinderStatus(aps->setAssistantServicesUids(uidsAidl));
Ahaan Ugalef51ce002021-08-04 16:34:20 -07002117}
2118
Oscar Azucenac2cdda32022-01-31 19:10:39 -08002119status_t AudioSystem::setActiveAssistantServicesUids(const std::vector<uid_t>& activeUids) {
Andy Hung264fa4c2024-02-21 15:52:12 -08002120 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Oscar Azucenac2cdda32022-01-31 19:10:39 -08002121 if (aps == 0) return PERMISSION_DENIED;
2122
2123 std::vector<int32_t> activeUidsAidl = VALUE_OR_RETURN_STATUS(
2124 convertContainer<std::vector<int32_t>>(activeUids, legacy2aidl_uid_t_int32_t));
2125 return statusTFromBinderStatus(aps->setActiveAssistantServicesUids(activeUidsAidl));
2126}
2127
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002128status_t AudioSystem::setA11yServicesUids(const std::vector<uid_t>& uids) {
Andy Hung264fa4c2024-02-21 15:52:12 -08002129 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Eric Laurentb78763e2018-10-17 10:08:02 -07002130 if (aps == 0) return PERMISSION_DENIED;
2131
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002132 std::vector<int32_t> uidsAidl = VALUE_OR_RETURN_STATUS(
2133 convertContainer<std::vector<int32_t>>(uids, legacy2aidl_uid_t_int32_t));
2134 return statusTFromBinderStatus(aps->setA11yServicesUids(uidsAidl));
Eric Laurentb78763e2018-10-17 10:08:02 -07002135}
2136
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002137status_t AudioSystem::setCurrentImeUid(uid_t uid) {
Andy Hung264fa4c2024-02-21 15:52:12 -08002138 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Kohsuke Yatoha623a132020-03-24 20:10:26 -07002139 if (aps == 0) return PERMISSION_DENIED;
2140
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002141 int32_t uidAidl = VALUE_OR_RETURN_STATUS(legacy2aidl_uid_t_int32_t(uid));
2142 return statusTFromBinderStatus(aps->setCurrentImeUid(uidAidl));
Kohsuke Yatoha623a132020-03-24 20:10:26 -07002143}
2144
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002145bool AudioSystem::isHapticPlaybackSupported() {
Andy Hung264fa4c2024-02-21 15:52:12 -08002146 const sp<IAudioPolicyService> aps = get_audio_policy_service();
jiabin6012f912018-11-02 17:06:30 -07002147 if (aps == 0) return false;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002148
2149 auto result = [&]() -> ConversionResult<bool> {
2150 bool retVal;
2151 RETURN_IF_ERROR(
2152 statusTFromBinderStatus(aps->isHapticPlaybackSupported(&retVal)));
2153 return retVal;
2154 }();
2155 return result.value_or(false);
jiabin6012f912018-11-02 17:06:30 -07002156}
2157
Carter Hsu325a8eb2022-01-19 19:56:51 +08002158bool AudioSystem::isUltrasoundSupported() {
Andy Hung264fa4c2024-02-21 15:52:12 -08002159 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Carter Hsu325a8eb2022-01-19 19:56:51 +08002160 if (aps == 0) return false;
2161
2162 auto result = [&]() -> ConversionResult<bool> {
2163 bool retVal;
2164 RETURN_IF_ERROR(
2165 statusTFromBinderStatus(aps->isUltrasoundSupported(&retVal)));
2166 return retVal;
2167 }();
2168 return result.value_or(false);
2169}
2170
Pattydd807582021-11-04 21:01:03 +08002171status_t AudioSystem::getHwOffloadFormatsSupportedForBluetoothMedia(
2172 audio_devices_t device, std::vector<audio_format_t>* formats) {
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002173 if (formats == nullptr) {
2174 return BAD_VALUE;
2175 }
2176
Andy Hung264fa4c2024-02-21 15:52:12 -08002177 const sp<IAudioPolicyService> aps = get_audio_policy_service();
François Gaffied0ba9ed2018-11-05 11:50:42 +01002178 if (aps == 0) return PERMISSION_DENIED;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002179
Mikhail Naganov57bd06f2021-08-10 16:41:54 -07002180 std::vector<AudioFormatDescription> formatsAidl;
Pattydd807582021-11-04 21:01:03 +08002181 AudioDeviceDescription deviceAidl = VALUE_OR_RETURN_STATUS(
2182 legacy2aidl_audio_devices_t_AudioDeviceDescription(device));
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002183 RETURN_STATUS_IF_ERROR(statusTFromBinderStatus(
Pattydd807582021-11-04 21:01:03 +08002184 aps->getHwOffloadFormatsSupportedForBluetoothMedia(deviceAidl, &formatsAidl)));
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002185 *formats = VALUE_OR_RETURN_STATUS(
Mikhail Naganovb60bd1b2021-07-15 17:31:43 -07002186 convertContainer<std::vector<audio_format_t>>(
2187 formatsAidl,
2188 aidl2legacy_AudioFormatDescription_audio_format_t));
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002189 return OK;
François Gaffied0ba9ed2018-11-05 11:50:42 +01002190}
2191
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002192status_t AudioSystem::listAudioProductStrategies(AudioProductStrategyVector& strategies) {
Andy Hung264fa4c2024-02-21 15:52:12 -08002193 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Arun Mirpuri11029ad2018-12-19 20:45:19 -08002194 if (aps == 0) return PERMISSION_DENIED;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002195
2196 std::vector<media::AudioProductStrategy> strategiesAidl;
2197 RETURN_STATUS_IF_ERROR(statusTFromBinderStatus(
2198 aps->listAudioProductStrategies(&strategiesAidl)));
2199 strategies = VALUE_OR_RETURN_STATUS(
2200 convertContainer<AudioProductStrategyVector>(strategiesAidl,
2201 aidl2legacy_AudioProductStrategy));
2202 return OK;
François Gaffied0ba9ed2018-11-05 11:50:42 +01002203}
2204
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002205audio_attributes_t AudioSystem::streamTypeToAttributes(audio_stream_type_t stream) {
François Gaffied0ba9ed2018-11-05 11:50:42 +01002206 AudioProductStrategyVector strategies;
2207 listAudioProductStrategies(strategies);
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002208 for (const auto& strategy : strategies) {
François Gaffie1e2b56f2022-04-01 14:34:29 +02002209 auto attrVect = strategy.getVolumeGroupAttributes();
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002210 auto iter = std::find_if(begin(attrVect), end(attrVect), [&stream](const auto& attributes) {
2211 return attributes.getStreamType() == stream;
2212 });
François Gaffied0ba9ed2018-11-05 11:50:42 +01002213 if (iter != end(attrVect)) {
2214 return iter->getAttributes();
2215 }
2216 }
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002217 ALOGE("invalid stream type %s when converting to attributes", toString(stream).c_str());
François Gaffied0ba9ed2018-11-05 11:50:42 +01002218 return AUDIO_ATTRIBUTES_INITIALIZER;
2219}
2220
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002221audio_stream_type_t AudioSystem::attributesToStreamType(const audio_attributes_t& attr) {
François Gaffie4b2018b2018-11-07 11:18:59 +01002222 product_strategy_t psId;
François Gaffie1e2b56f2022-04-01 14:34:29 +02002223 status_t ret = AudioSystem::getProductStrategyFromAudioAttributes(attr, psId);
François Gaffie4b2018b2018-11-07 11:18:59 +01002224 if (ret != NO_ERROR) {
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002225 ALOGE("no strategy found for attributes %s", toString(attr).c_str());
François Gaffie4b2018b2018-11-07 11:18:59 +01002226 return AUDIO_STREAM_MUSIC;
2227 }
François Gaffied0ba9ed2018-11-05 11:50:42 +01002228 AudioProductStrategyVector strategies;
2229 listAudioProductStrategies(strategies);
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002230 for (const auto& strategy : strategies) {
François Gaffie4b2018b2018-11-07 11:18:59 +01002231 if (strategy.getId() == psId) {
François Gaffie1e2b56f2022-04-01 14:34:29 +02002232 auto attrVect = strategy.getVolumeGroupAttributes();
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002233 auto iter = std::find_if(begin(attrVect), end(attrVect), [&attr](const auto& refAttr) {
François Gaffie8d7fd5a2023-01-17 17:28:09 +01002234 return refAttr.matchesScore(attr) > 0;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002235 });
François Gaffied0ba9ed2018-11-05 11:50:42 +01002236 if (iter != end(attrVect)) {
2237 return iter->getStreamType();
2238 }
2239 }
2240 }
Jean-Michel Trivied678652019-12-19 13:39:30 -08002241 switch (attr.usage) {
2242 case AUDIO_USAGE_VIRTUAL_SOURCE:
2243 // virtual source is not expected to have an associated product strategy
2244 break;
2245 default:
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002246 ALOGE("invalid attributes %s when converting to stream", toString(attr).c_str());
Jean-Michel Trivied678652019-12-19 13:39:30 -08002247 break;
2248 }
François Gaffied0ba9ed2018-11-05 11:50:42 +01002249 return AUDIO_STREAM_MUSIC;
2250}
2251
François Gaffie1e2b56f2022-04-01 14:34:29 +02002252status_t AudioSystem::getProductStrategyFromAudioAttributes(const audio_attributes_t& aa,
Francois Gaffie11b65922020-09-24 16:59:08 +02002253 product_strategy_t& productStrategy,
2254 bool fallbackOnDefault) {
Andy Hung264fa4c2024-02-21 15:52:12 -08002255 const sp<IAudioPolicyService> aps = get_audio_policy_service();
François Gaffie4b2018b2018-11-07 11:18:59 +01002256 if (aps == 0) return PERMISSION_DENIED;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002257
Mikhail Naganov1c400902023-05-17 11:48:43 -07002258 media::audio::common::AudioAttributes aaAidl = VALUE_OR_RETURN_STATUS(
2259 legacy2aidl_audio_attributes_t_AudioAttributes(aa));
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002260 int32_t productStrategyAidl;
2261
2262 RETURN_STATUS_IF_ERROR(statusTFromBinderStatus(
Francois Gaffie11b65922020-09-24 16:59:08 +02002263 aps->getProductStrategyFromAudioAttributes(aaAidl, fallbackOnDefault,
2264 &productStrategyAidl)));
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002265 productStrategy = VALUE_OR_RETURN_STATUS(
2266 aidl2legacy_int32_t_product_strategy_t(productStrategyAidl));
2267 return OK;
François Gaffie4b2018b2018-11-07 11:18:59 +01002268}
2269
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002270status_t AudioSystem::listAudioVolumeGroups(AudioVolumeGroupVector& groups) {
Andy Hung264fa4c2024-02-21 15:52:12 -08002271 const sp<IAudioPolicyService> aps = get_audio_policy_service();
François Gaffie4b2018b2018-11-07 11:18:59 +01002272 if (aps == 0) return PERMISSION_DENIED;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002273
2274 std::vector<media::AudioVolumeGroup> groupsAidl;
2275 RETURN_STATUS_IF_ERROR(
2276 statusTFromBinderStatus(aps->listAudioVolumeGroups(&groupsAidl)));
2277 groups = VALUE_OR_RETURN_STATUS(
2278 convertContainer<AudioVolumeGroupVector>(groupsAidl, aidl2legacy_AudioVolumeGroup));
2279 return OK;
François Gaffie4b2018b2018-11-07 11:18:59 +01002280}
2281
François Gaffie1e2b56f2022-04-01 14:34:29 +02002282status_t AudioSystem::getVolumeGroupFromAudioAttributes(const audio_attributes_t &aa,
Francois Gaffie11b65922020-09-24 16:59:08 +02002283 volume_group_t& volumeGroup,
2284 bool fallbackOnDefault) {
Andy Hung264fa4c2024-02-21 15:52:12 -08002285 const sp<IAudioPolicyService> aps = get_audio_policy_service();
François Gaffie4b2018b2018-11-07 11:18:59 +01002286 if (aps == 0) return PERMISSION_DENIED;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002287
Mikhail Naganov1c400902023-05-17 11:48:43 -07002288 media::audio::common::AudioAttributes aaAidl = VALUE_OR_RETURN_STATUS(
2289 legacy2aidl_audio_attributes_t_AudioAttributes(aa));
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002290 int32_t volumeGroupAidl;
2291 RETURN_STATUS_IF_ERROR(statusTFromBinderStatus(
Francois Gaffie11b65922020-09-24 16:59:08 +02002292 aps->getVolumeGroupFromAudioAttributes(aaAidl, fallbackOnDefault, &volumeGroupAidl)));
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002293 volumeGroup = VALUE_OR_RETURN_STATUS(aidl2legacy_int32_t_volume_group_t(volumeGroupAidl));
2294 return OK;
Arun Mirpuri11029ad2018-12-19 20:45:19 -08002295}
Eric Laurentb78763e2018-10-17 10:08:02 -07002296
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002297status_t AudioSystem::setRttEnabled(bool enabled) {
Andy Hung264fa4c2024-02-21 15:52:12 -08002298 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Eric Laurent6ede98f2019-06-11 14:50:30 -07002299 if (aps == 0) return PERMISSION_DENIED;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002300 return statusTFromBinderStatus(aps->setRttEnabled(enabled));
Eric Laurent6ede98f2019-06-11 14:50:30 -07002301}
2302
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002303bool AudioSystem::isCallScreenModeSupported() {
Andy Hung264fa4c2024-02-21 15:52:12 -08002304 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Eric Laurent8340e672019-11-06 11:01:08 -08002305 if (aps == 0) return false;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002306
2307 auto result = [&]() -> ConversionResult<bool> {
2308 bool retAidl;
2309 RETURN_IF_ERROR(
2310 statusTFromBinderStatus(aps->isCallScreenModeSupported(&retAidl)));
2311 return retAidl;
2312 }();
2313 return result.value_or(false);
Eric Laurent8340e672019-11-06 11:01:08 -08002314}
2315
jiabin0a488932020-08-07 17:32:40 -07002316status_t AudioSystem::setDevicesRoleForStrategy(product_strategy_t strategy,
2317 device_role_t role,
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002318 const AudioDeviceTypeAddrVector& devices) {
Andy Hung264fa4c2024-02-21 15:52:12 -08002319 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Jean-Michel Trivi30857152019-11-01 11:04:15 -07002320 if (aps == 0) {
2321 return PERMISSION_DENIED;
2322 }
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002323
2324 int32_t strategyAidl = VALUE_OR_RETURN_STATUS(legacy2aidl_product_strategy_t_int32_t(strategy));
2325 media::DeviceRole roleAidl = VALUE_OR_RETURN_STATUS(legacy2aidl_device_role_t_DeviceRole(role));
Mikhail Naganovf4a75362021-09-16 00:02:54 +00002326 std::vector<AudioDevice> devicesAidl = VALUE_OR_RETURN_STATUS(
2327 convertContainer<std::vector<AudioDevice>>(devices,
2328 legacy2aidl_AudioDeviceTypeAddress));
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002329 return statusTFromBinderStatus(
2330 aps->setDevicesRoleForStrategy(strategyAidl, roleAidl, devicesAidl));
Jean-Michel Trivi30857152019-11-01 11:04:15 -07002331}
2332
Paul Wang5d7cdb52022-11-22 09:45:06 +00002333status_t AudioSystem::removeDevicesRoleForStrategy(product_strategy_t strategy,
2334 device_role_t role,
2335 const AudioDeviceTypeAddrVector& devices) {
Andy Hung264fa4c2024-02-21 15:52:12 -08002336 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Paul Wang5d7cdb52022-11-22 09:45:06 +00002337 if (aps == 0) {
2338 return PERMISSION_DENIED;
2339 }
2340
2341 int32_t strategyAidl = VALUE_OR_RETURN_STATUS(legacy2aidl_product_strategy_t_int32_t(strategy));
2342 media::DeviceRole roleAidl = VALUE_OR_RETURN_STATUS(legacy2aidl_device_role_t_DeviceRole(role));
2343 std::vector<AudioDevice> devicesAidl = VALUE_OR_RETURN_STATUS(
2344 convertContainer<std::vector<AudioDevice>>(devices,
2345 legacy2aidl_AudioDeviceTypeAddress));
2346 return statusTFromBinderStatus(
2347 aps->removeDevicesRoleForStrategy(strategyAidl, roleAidl, devicesAidl));
2348}
2349
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002350status_t
Paul Wang5d7cdb52022-11-22 09:45:06 +00002351AudioSystem::clearDevicesRoleForStrategy(product_strategy_t strategy, device_role_t role) {
Andy Hung264fa4c2024-02-21 15:52:12 -08002352 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Jean-Michel Trivi30857152019-11-01 11:04:15 -07002353 if (aps == 0) {
2354 return PERMISSION_DENIED;
2355 }
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002356 int32_t strategyAidl = VALUE_OR_RETURN_STATUS(legacy2aidl_product_strategy_t_int32_t(strategy));
2357 media::DeviceRole roleAidl = VALUE_OR_RETURN_STATUS(legacy2aidl_device_role_t_DeviceRole(role));
2358 return statusTFromBinderStatus(
Paul Wang5d7cdb52022-11-22 09:45:06 +00002359 aps->clearDevicesRoleForStrategy(strategyAidl, roleAidl));
Jean-Michel Trivi30857152019-11-01 11:04:15 -07002360}
2361
jiabin0a488932020-08-07 17:32:40 -07002362status_t AudioSystem::getDevicesForRoleAndStrategy(product_strategy_t strategy,
2363 device_role_t role,
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002364 AudioDeviceTypeAddrVector& devices) {
Andy Hung264fa4c2024-02-21 15:52:12 -08002365 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Jean-Michel Trivi30857152019-11-01 11:04:15 -07002366 if (aps == 0) {
2367 return PERMISSION_DENIED;
2368 }
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002369 int32_t strategyAidl = VALUE_OR_RETURN_STATUS(legacy2aidl_product_strategy_t_int32_t(strategy));
2370 media::DeviceRole roleAidl = VALUE_OR_RETURN_STATUS(legacy2aidl_device_role_t_DeviceRole(role));
Mikhail Naganovf4a75362021-09-16 00:02:54 +00002371 std::vector<AudioDevice> devicesAidl;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002372 RETURN_STATUS_IF_ERROR(statusTFromBinderStatus(
2373 aps->getDevicesForRoleAndStrategy(strategyAidl, roleAidl, &devicesAidl)));
2374 devices = VALUE_OR_RETURN_STATUS(
2375 convertContainer<AudioDeviceTypeAddrVector>(devicesAidl,
2376 aidl2legacy_AudioDeviceTypeAddress));
2377 return OK;
Jean-Michel Trivi30857152019-11-01 11:04:15 -07002378}
2379
Jiabin Huang3b98d322020-09-03 17:54:16 +00002380status_t AudioSystem::setDevicesRoleForCapturePreset(audio_source_t audioSource,
2381 device_role_t role,
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002382 const AudioDeviceTypeAddrVector& devices) {
Andy Hung264fa4c2024-02-21 15:52:12 -08002383 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Jiabin Huang3b98d322020-09-03 17:54:16 +00002384 if (aps == 0) {
2385 return PERMISSION_DENIED;
2386 }
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002387
Mikhail Naganovddceecc2021-09-03 13:58:56 -07002388 AudioSource audioSourceAidl = VALUE_OR_RETURN_STATUS(
2389 legacy2aidl_audio_source_t_AudioSource(audioSource));
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002390 media::DeviceRole roleAidl = VALUE_OR_RETURN_STATUS(legacy2aidl_device_role_t_DeviceRole(role));
Mikhail Naganovf4a75362021-09-16 00:02:54 +00002391 std::vector<AudioDevice> devicesAidl = VALUE_OR_RETURN_STATUS(
2392 convertContainer<std::vector<AudioDevice>>(devices,
2393 legacy2aidl_AudioDeviceTypeAddress));
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002394 return statusTFromBinderStatus(
2395 aps->setDevicesRoleForCapturePreset(audioSourceAidl, roleAidl, devicesAidl));
Jiabin Huang3b98d322020-09-03 17:54:16 +00002396}
2397
2398status_t AudioSystem::addDevicesRoleForCapturePreset(audio_source_t audioSource,
2399 device_role_t role,
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002400 const AudioDeviceTypeAddrVector& devices) {
Andy Hung264fa4c2024-02-21 15:52:12 -08002401 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Jiabin Huang3b98d322020-09-03 17:54:16 +00002402 if (aps == 0) {
2403 return PERMISSION_DENIED;
2404 }
Mikhail Naganovddceecc2021-09-03 13:58:56 -07002405 AudioSource audioSourceAidl = VALUE_OR_RETURN_STATUS(
2406 legacy2aidl_audio_source_t_AudioSource(audioSource));
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002407 media::DeviceRole roleAidl = VALUE_OR_RETURN_STATUS(legacy2aidl_device_role_t_DeviceRole(role));
Mikhail Naganovf4a75362021-09-16 00:02:54 +00002408 std::vector<AudioDevice> devicesAidl = VALUE_OR_RETURN_STATUS(
2409 convertContainer<std::vector<AudioDevice>>(devices,
2410 legacy2aidl_AudioDeviceTypeAddress));
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002411 return statusTFromBinderStatus(
2412 aps->addDevicesRoleForCapturePreset(audioSourceAidl, roleAidl, devicesAidl));
Jiabin Huang3b98d322020-09-03 17:54:16 +00002413}
2414
2415status_t AudioSystem::removeDevicesRoleForCapturePreset(
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002416 audio_source_t audioSource, device_role_t role, const AudioDeviceTypeAddrVector& devices) {
Andy Hung264fa4c2024-02-21 15:52:12 -08002417 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Jiabin Huang3b98d322020-09-03 17:54:16 +00002418 if (aps == 0) {
2419 return PERMISSION_DENIED;
2420 }
Mikhail Naganovddceecc2021-09-03 13:58:56 -07002421 AudioSource audioSourceAidl = VALUE_OR_RETURN_STATUS(
2422 legacy2aidl_audio_source_t_AudioSource(audioSource));
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002423 media::DeviceRole roleAidl = VALUE_OR_RETURN_STATUS(legacy2aidl_device_role_t_DeviceRole(role));
Mikhail Naganovf4a75362021-09-16 00:02:54 +00002424 std::vector<AudioDevice> devicesAidl = VALUE_OR_RETURN_STATUS(
2425 convertContainer<std::vector<AudioDevice>>(devices,
2426 legacy2aidl_AudioDeviceTypeAddress));
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002427 return statusTFromBinderStatus(
2428 aps->removeDevicesRoleForCapturePreset(audioSourceAidl, roleAidl, devicesAidl));
Jiabin Huang3b98d322020-09-03 17:54:16 +00002429}
2430
2431status_t AudioSystem::clearDevicesRoleForCapturePreset(audio_source_t audioSource,
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002432 device_role_t role) {
Andy Hung264fa4c2024-02-21 15:52:12 -08002433 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Jiabin Huang3b98d322020-09-03 17:54:16 +00002434 if (aps == 0) {
2435 return PERMISSION_DENIED;
2436 }
Mikhail Naganovddceecc2021-09-03 13:58:56 -07002437 AudioSource audioSourceAidl = VALUE_OR_RETURN_STATUS(
2438 legacy2aidl_audio_source_t_AudioSource(audioSource));
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002439 media::DeviceRole roleAidl = VALUE_OR_RETURN_STATUS(legacy2aidl_device_role_t_DeviceRole(role));
2440 return statusTFromBinderStatus(
2441 aps->clearDevicesRoleForCapturePreset(audioSourceAidl, roleAidl));
Jiabin Huang3b98d322020-09-03 17:54:16 +00002442}
2443
2444status_t AudioSystem::getDevicesForRoleAndCapturePreset(audio_source_t audioSource,
2445 device_role_t role,
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002446 AudioDeviceTypeAddrVector& devices) {
Andy Hung264fa4c2024-02-21 15:52:12 -08002447 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Jiabin Huang3b98d322020-09-03 17:54:16 +00002448 if (aps == 0) {
2449 return PERMISSION_DENIED;
2450 }
Mikhail Naganovddceecc2021-09-03 13:58:56 -07002451 AudioSource audioSourceAidl = VALUE_OR_RETURN_STATUS(
2452 legacy2aidl_audio_source_t_AudioSource(audioSource));
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002453 media::DeviceRole roleAidl = VALUE_OR_RETURN_STATUS(legacy2aidl_device_role_t_DeviceRole(role));
Mikhail Naganovf4a75362021-09-16 00:02:54 +00002454 std::vector<AudioDevice> devicesAidl;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002455 RETURN_STATUS_IF_ERROR(statusTFromBinderStatus(
2456 aps->getDevicesForRoleAndCapturePreset(audioSourceAidl, roleAidl, &devicesAidl)));
2457 devices = VALUE_OR_RETURN_STATUS(
2458 convertContainer<AudioDeviceTypeAddrVector>(devicesAidl,
2459 aidl2legacy_AudioDeviceTypeAddress));
2460 return OK;
Jiabin Huang3b98d322020-09-03 17:54:16 +00002461}
2462
Eric Laurent81dd0f52021-07-05 11:54:40 +02002463status_t AudioSystem::getSpatializer(const sp<media::INativeSpatializerCallback>& callback,
2464 sp<media::ISpatializer>* spatializer) {
Andy Hung264fa4c2024-02-21 15:52:12 -08002465 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Eric Laurent81dd0f52021-07-05 11:54:40 +02002466 if (spatializer == nullptr) {
2467 return BAD_VALUE;
2468 }
2469 if (aps == 0) {
2470 return PERMISSION_DENIED;
2471 }
2472 media::GetSpatializerResponse response;
2473 RETURN_STATUS_IF_ERROR(statusTFromBinderStatus(
2474 aps->getSpatializer(callback, &response)));
2475
2476 *spatializer = response.spatializer;
2477 return OK;
2478}
2479
2480status_t AudioSystem::canBeSpatialized(const audio_attributes_t *attr,
2481 const audio_config_t *config,
2482 const AudioDeviceTypeAddrVector &devices,
2483 bool *canBeSpatialized) {
Andy Hung264fa4c2024-02-21 15:52:12 -08002484 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Ram Mohan43297012022-08-30 16:11:49 +05302485 if (canBeSpatialized == nullptr) {
2486 return BAD_VALUE;
2487 }
Eric Laurent81dd0f52021-07-05 11:54:40 +02002488 if (aps == 0) {
2489 return PERMISSION_DENIED;
2490 }
2491 audio_attributes_t attributes = attr != nullptr ? *attr : AUDIO_ATTRIBUTES_INITIALIZER;
2492 audio_config_t configuration = config != nullptr ? *config : AUDIO_CONFIG_INITIALIZER;
2493
Mikhail Naganov1c400902023-05-17 11:48:43 -07002494 std::optional<media::audio::common::AudioAttributes> attrAidl = VALUE_OR_RETURN_STATUS(
2495 legacy2aidl_audio_attributes_t_AudioAttributes(attributes));
Mikhail Naganovdbf03642021-08-25 18:15:32 -07002496 std::optional<AudioConfig> configAidl = VALUE_OR_RETURN_STATUS(
Eric Laurent81dd0f52021-07-05 11:54:40 +02002497 legacy2aidl_audio_config_t_AudioConfig(configuration, false /*isInput*/));
Mikhail Naganovf4a75362021-09-16 00:02:54 +00002498 std::vector<AudioDevice> devicesAidl = VALUE_OR_RETURN_STATUS(
2499 convertContainer<std::vector<AudioDevice>>(devices,
2500 legacy2aidl_AudioDeviceTypeAddress));
Eric Laurent81dd0f52021-07-05 11:54:40 +02002501 RETURN_STATUS_IF_ERROR(statusTFromBinderStatus(
2502 aps->canBeSpatialized(attrAidl, configAidl, devicesAidl, canBeSpatialized)));
2503 return OK;
2504}
2505
Vlad Popae3fd1c22022-11-07 21:03:18 +01002506status_t AudioSystem::getSoundDoseInterface(const sp<media::ISoundDoseCallback>& callback,
2507 sp<media::ISoundDose>* soundDose) {
Andy Hung264fa4c2024-02-21 15:52:12 -08002508 const sp<IAudioFlinger> af = get_audio_flinger();
Vlad Popa63f047e2022-11-05 14:09:19 +01002509 if (af == nullptr) {
2510 return PERMISSION_DENIED;
2511 }
Vlad Popae3fd1c22022-11-07 21:03:18 +01002512 if (soundDose == nullptr) {
2513 return BAD_VALUE;
2514 }
Vlad Popa63f047e2022-11-05 14:09:19 +01002515
Vlad Popae3fd1c22022-11-07 21:03:18 +01002516 RETURN_STATUS_IF_ERROR(af->getSoundDoseInterface(callback, soundDose));
2517 return OK;
Vlad Popa63f047e2022-11-05 14:09:19 +01002518}
2519
jiabin2b9d5a12021-12-10 01:06:29 +00002520status_t AudioSystem::getDirectPlaybackSupport(const audio_attributes_t *attr,
2521 const audio_config_t *config,
2522 audio_direct_mode_t* directMode) {
2523 if (attr == nullptr || config == nullptr || directMode == nullptr) {
2524 return BAD_VALUE;
2525 }
2526
Andy Hung264fa4c2024-02-21 15:52:12 -08002527 const sp<IAudioPolicyService> aps = get_audio_policy_service();
jiabin2b9d5a12021-12-10 01:06:29 +00002528 if (aps == 0) {
2529 return PERMISSION_DENIED;
2530 }
2531
Mikhail Naganov1c400902023-05-17 11:48:43 -07002532 media::audio::common::AudioAttributes attrAidl = VALUE_OR_RETURN_STATUS(
2533 legacy2aidl_audio_attributes_t_AudioAttributes(*attr));
jiabin2b9d5a12021-12-10 01:06:29 +00002534 AudioConfig configAidl = VALUE_OR_RETURN_STATUS(
2535 legacy2aidl_audio_config_t_AudioConfig(*config, false /*isInput*/));
2536
2537 media::AudioDirectMode retAidl;
2538 RETURN_STATUS_IF_ERROR(statusTFromBinderStatus(
2539 aps->getDirectPlaybackSupport(attrAidl, configAidl, &retAidl)));
2540 *directMode = VALUE_OR_RETURN_STATUS(aidl2legacy_int32_t_audio_direct_mode_t_mask(
2541 static_cast<int32_t>(retAidl)));
2542 return NO_ERROR;
2543}
2544
Dorin Drimusf2196d82022-01-03 12:11:18 +01002545status_t AudioSystem::getDirectProfilesForAttributes(const audio_attributes_t* attr,
2546 std::vector<audio_profile>* audioProfiles) {
Mikhail Naganovefc504b2022-06-22 03:24:59 +00002547 if (attr == nullptr || audioProfiles == nullptr) {
Dorin Drimusf2196d82022-01-03 12:11:18 +01002548 return BAD_VALUE;
2549 }
2550
Andy Hung264fa4c2024-02-21 15:52:12 -08002551 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Dorin Drimusf2196d82022-01-03 12:11:18 +01002552 if (aps == 0) {
2553 return PERMISSION_DENIED;
2554 }
2555
Mikhail Naganov1c400902023-05-17 11:48:43 -07002556 media::audio::common::AudioAttributes attrAidl = VALUE_OR_RETURN_STATUS(
2557 legacy2aidl_audio_attributes_t_AudioAttributes(*attr));
Dorin Drimusf2196d82022-01-03 12:11:18 +01002558
2559 std::vector<media::audio::common::AudioProfile> audioProfilesAidl;
2560 RETURN_STATUS_IF_ERROR(statusTFromBinderStatus(
2561 aps->getDirectProfilesForAttributes(attrAidl, &audioProfilesAidl)));
2562 *audioProfiles = VALUE_OR_RETURN_STATUS(convertContainer<std::vector<audio_profile>>(
2563 audioProfilesAidl, aidl2legacy_AudioProfile_audio_profile, false /*isInput*/));
2564
2565 return NO_ERROR;
2566}
Eric Laurent81dd0f52021-07-05 11:54:40 +02002567
Eric Laurent076e7c72022-05-03 18:12:28 +02002568status_t AudioSystem::setRequestedLatencyMode(
2569 audio_io_handle_t output, audio_latency_mode_t mode) {
Andy Hung264fa4c2024-02-21 15:52:12 -08002570 const sp<IAudioFlinger> af = get_audio_flinger();
Eric Laurent076e7c72022-05-03 18:12:28 +02002571 if (af == nullptr) {
2572 return PERMISSION_DENIED;
2573 }
2574 return af->setRequestedLatencyMode(output, mode);
2575}
2576
2577status_t AudioSystem::getSupportedLatencyModes(audio_io_handle_t output,
2578 std::vector<audio_latency_mode_t>* modes) {
Andy Hung264fa4c2024-02-21 15:52:12 -08002579 const sp<IAudioFlinger> af = get_audio_flinger();
Eric Laurent076e7c72022-05-03 18:12:28 +02002580 if (af == nullptr) {
2581 return PERMISSION_DENIED;
2582 }
2583 return af->getSupportedLatencyModes(output, modes);
2584}
2585
Eric Laurent50d72582022-12-20 20:20:23 +01002586status_t AudioSystem::setBluetoothVariableLatencyEnabled(bool enabled) {
Andy Hung264fa4c2024-02-21 15:52:12 -08002587 const sp<IAudioFlinger> af = get_audio_flinger();
Eric Laurent52057642022-12-16 11:45:07 +01002588 if (af == nullptr) {
2589 return PERMISSION_DENIED;
2590 }
Eric Laurent50d72582022-12-20 20:20:23 +01002591 return af->setBluetoothVariableLatencyEnabled(enabled);
Eric Laurent52057642022-12-16 11:45:07 +01002592}
2593
Eric Laurent50d72582022-12-20 20:20:23 +01002594status_t AudioSystem::isBluetoothVariableLatencyEnabled(
2595 bool *enabled) {
Andy Hung264fa4c2024-02-21 15:52:12 -08002596 const sp<IAudioFlinger> af = get_audio_flinger();
Eric Laurent50d72582022-12-20 20:20:23 +01002597 if (af == nullptr) {
2598 return PERMISSION_DENIED;
2599 }
2600 return af->isBluetoothVariableLatencyEnabled(enabled);
2601}
2602
2603status_t AudioSystem::supportsBluetoothVariableLatency(
Eric Laurent52057642022-12-16 11:45:07 +01002604 bool *support) {
Andy Hung264fa4c2024-02-21 15:52:12 -08002605 const sp<IAudioFlinger> af = get_audio_flinger();
Eric Laurent52057642022-12-16 11:45:07 +01002606 if (af == nullptr) {
2607 return PERMISSION_DENIED;
2608 }
Eric Laurent50d72582022-12-20 20:20:23 +01002609 return af->supportsBluetoothVariableLatency(support);
Eric Laurent52057642022-12-16 11:45:07 +01002610}
2611
Mikhail Naganovffd97712023-05-03 17:45:36 -07002612status_t AudioSystem::getAudioPolicyConfig(media::AudioPolicyConfig *config) {
Andy Hung264fa4c2024-02-21 15:52:12 -08002613 const sp<IAudioFlinger> af = get_audio_flinger();
Mikhail Naganovffd97712023-05-03 17:45:36 -07002614 if (af == nullptr) {
2615 return PERMISSION_DENIED;
2616 }
2617 return af->getAudioPolicyConfig(config);
2618}
2619
Ytai Ben-Tsvi5120eda2020-03-26 09:41:15 -07002620class CaptureStateListenerImpl : public media::BnCaptureStateListener,
2621 public IBinder::DeathRecipient {
2622public:
Ytai Ben-Tsvid9f82832020-08-27 09:31:10 -07002623 CaptureStateListenerImpl(
2624 const sp<IAudioPolicyService>& aps,
2625 const sp<AudioSystem::CaptureStateListener>& listener)
Ytai Ben-Tsvia46b6d32020-08-31 13:29:11 -07002626 : mAps(aps), mListener(listener) {}
2627
2628 void init() {
Ytai Ben-Tsvid9f82832020-08-27 09:31:10 -07002629 bool active;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002630 status_t status = statusTFromBinderStatus(
2631 mAps->registerSoundTriggerCaptureStateListener(this, &active));
Ytai Ben-Tsvid9f82832020-08-27 09:31:10 -07002632 if (status != NO_ERROR) {
2633 mListener->onServiceDied();
2634 return;
2635 }
2636 mListener->onStateChanged(active);
Ytai Ben-Tsvia46b6d32020-08-31 13:29:11 -07002637 IInterface::asBinder(mAps)->linkToDeath(this);
Ytai Ben-Tsvid9f82832020-08-27 09:31:10 -07002638 }
2639
Ytai Ben-Tsvi5120eda2020-03-26 09:41:15 -07002640 binder::Status setCaptureState(bool active) override {
Andy Hungacd955a2024-02-16 17:14:36 -08002641 std::lock_guard _l(AudioSystem::gSoundTriggerMutex);
Ytai Ben-Tsvid9f82832020-08-27 09:31:10 -07002642 mListener->onStateChanged(active);
Ytai Ben-Tsvi5120eda2020-03-26 09:41:15 -07002643 return binder::Status::ok();
2644 }
2645
2646 void binderDied(const wp<IBinder>&) override {
Andy Hungacd955a2024-02-16 17:14:36 -08002647 std::lock_guard _l(AudioSystem::gSoundTriggerMutex);
Ytai Ben-Tsvid9f82832020-08-27 09:31:10 -07002648 mListener->onServiceDied();
Andy Hungacd955a2024-02-16 17:14:36 -08002649 AudioSystem::gSoundTriggerCaptureStateListener = nullptr;
Ytai Ben-Tsvi5120eda2020-03-26 09:41:15 -07002650 }
Ytai Ben-Tsvid9f82832020-08-27 09:31:10 -07002651
2652private:
2653 // Need this in order to keep the death receipent alive.
2654 sp<IAudioPolicyService> mAps;
2655 sp<AudioSystem::CaptureStateListener> mListener;
Ytai Ben-Tsvi5120eda2020-03-26 09:41:15 -07002656};
2657
2658status_t AudioSystem::registerSoundTriggerCaptureStateListener(
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002659 const sp<CaptureStateListener>& listener) {
Ytai Ben-Tsvid9f82832020-08-27 09:31:10 -07002660 LOG_ALWAYS_FATAL_IF(listener == nullptr);
2661
Andy Hung264fa4c2024-02-21 15:52:12 -08002662 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Ytai Ben-Tsvi5120eda2020-03-26 09:41:15 -07002663 if (aps == 0) {
2664 return PERMISSION_DENIED;
2665 }
2666
Andy Hungacd955a2024-02-16 17:14:36 -08002667 std::lock_guard _l(AudioSystem::gSoundTriggerMutex);
Ytai Ben-Tsvid9f82832020-08-27 09:31:10 -07002668 gSoundTriggerCaptureStateListener = new CaptureStateListenerImpl(aps, listener);
Ytai Ben-Tsvia46b6d32020-08-31 13:29:11 -07002669 gSoundTriggerCaptureStateListener->init();
Ytai Ben-Tsvi5120eda2020-03-26 09:41:15 -07002670
Ytai Ben-Tsvi5120eda2020-03-26 09:41:15 -07002671 return NO_ERROR;
2672}
2673
jiabin1319f5a2021-03-30 22:21:24 +00002674status_t AudioSystem::setVibratorInfos(
2675 const std::vector<media::AudioVibratorInfo>& vibratorInfos) {
Andy Hung264fa4c2024-02-21 15:52:12 -08002676 const sp<IAudioFlinger> af = get_audio_flinger();
jiabin1319f5a2021-03-30 22:21:24 +00002677 if (af == nullptr) {
2678 return PERMISSION_DENIED;
2679 }
2680 return af->setVibratorInfos(vibratorInfos);
2681}
2682
Jiabin Huangebe64102021-09-07 20:01:07 +00002683status_t AudioSystem::getMmapPolicyInfo(
jiabine99d0882021-09-17 05:21:25 +00002684 AudioMMapPolicyType policyType, std::vector<AudioMMapPolicyInfo> *policyInfos) {
Andy Hung264fa4c2024-02-21 15:52:12 -08002685 const sp<IAudioFlinger> af = get_audio_flinger();
Jiabin Huangebe64102021-09-07 20:01:07 +00002686 if (af == nullptr) {
2687 return PERMISSION_DENIED;
2688 }
2689 return af->getMmapPolicyInfos(policyType, policyInfos);
2690}
2691
jiabine504e7b2021-09-18 00:27:08 +00002692int32_t AudioSystem::getAAudioMixerBurstCount() {
Andy Hung264fa4c2024-02-21 15:52:12 -08002693 const sp<IAudioFlinger> af = get_audio_flinger();
jiabine504e7b2021-09-18 00:27:08 +00002694 if (af == nullptr) {
2695 return PERMISSION_DENIED;
2696 }
2697 return af->getAAudioMixerBurstCount();
2698}
2699
2700int32_t AudioSystem::getAAudioHardwareBurstMinUsec() {
Andy Hung264fa4c2024-02-21 15:52:12 -08002701 const sp<IAudioFlinger> af = get_audio_flinger();
jiabine504e7b2021-09-18 00:27:08 +00002702 if (af == nullptr) {
2703 return PERMISSION_DENIED;
2704 }
2705 return af->getAAudioHardwareBurstMinUsec();
2706}
2707
jiabina84c3d32022-12-02 18:59:55 +00002708status_t AudioSystem::getSupportedMixerAttributes(
2709 audio_port_handle_t portId, std::vector<audio_mixer_attributes_t> *mixerAttrs) {
Andy Hung264fa4c2024-02-21 15:52:12 -08002710 const sp<IAudioPolicyService> aps = get_audio_policy_service();
jiabina84c3d32022-12-02 18:59:55 +00002711 if (aps == nullptr) {
2712 return PERMISSION_DENIED;
2713 }
2714
2715 int32_t portIdAidl = VALUE_OR_RETURN_STATUS(legacy2aidl_audio_port_handle_t_int32_t(portId));
2716 std::vector<media::AudioMixerAttributesInternal> _aidlReturn;
2717 RETURN_STATUS_IF_ERROR(statusTFromBinderStatus(
2718 aps->getSupportedMixerAttributes(portIdAidl, &_aidlReturn)));
2719 *mixerAttrs = VALUE_OR_RETURN_STATUS(
2720 convertContainer<std::vector<audio_mixer_attributes_t>>(
2721 _aidlReturn,
2722 aidl2legacy_AudioMixerAttributesInternal_audio_mixer_attributes_t));
2723 return OK;
2724}
2725
2726status_t AudioSystem::setPreferredMixerAttributes(const audio_attributes_t *attr,
2727 audio_port_handle_t portId,
2728 uid_t uid,
2729 const audio_mixer_attributes_t *mixerAttr) {
Andy Hung264fa4c2024-02-21 15:52:12 -08002730 const sp<IAudioPolicyService> aps = get_audio_policy_service();
jiabina84c3d32022-12-02 18:59:55 +00002731 if (aps == nullptr) {
2732 return PERMISSION_DENIED;
2733 }
2734
Mikhail Naganov1c400902023-05-17 11:48:43 -07002735 media::audio::common::AudioAttributes attrAidl = VALUE_OR_RETURN_STATUS(
2736 legacy2aidl_audio_attributes_t_AudioAttributes(*attr));
jiabina84c3d32022-12-02 18:59:55 +00002737 media::AudioMixerAttributesInternal mixerAttrAidl = VALUE_OR_RETURN_STATUS(
2738 legacy2aidl_audio_mixer_attributes_t_AudioMixerAttributesInternal(*mixerAttr));
2739 int32_t uidAidl = VALUE_OR_RETURN_STATUS(legacy2aidl_uid_t_int32_t(uid));
2740 int32_t portIdAidl = VALUE_OR_RETURN_STATUS(legacy2aidl_audio_port_handle_t_int32_t(portId));
2741
2742 return statusTFromBinderStatus(
2743 aps->setPreferredMixerAttributes(attrAidl, portIdAidl, uidAidl, mixerAttrAidl));
2744}
2745
2746status_t AudioSystem::getPreferredMixerAttributes(
2747 const audio_attributes_t *attr,
2748 audio_port_handle_t portId,
2749 std::optional<audio_mixer_attributes_t> *mixerAttr) {
Andy Hung264fa4c2024-02-21 15:52:12 -08002750 const sp<IAudioPolicyService> aps = get_audio_policy_service();
jiabina84c3d32022-12-02 18:59:55 +00002751 if (aps == nullptr) {
2752 return PERMISSION_DENIED;
2753 }
2754
Mikhail Naganov1c400902023-05-17 11:48:43 -07002755 media::audio::common::AudioAttributes attrAidl = VALUE_OR_RETURN_STATUS(
2756 legacy2aidl_audio_attributes_t_AudioAttributes(*attr));
jiabina84c3d32022-12-02 18:59:55 +00002757 int32_t portIdAidl = VALUE_OR_RETURN_STATUS(legacy2aidl_audio_port_handle_t_int32_t(portId));
2758 std::optional<media::AudioMixerAttributesInternal> _aidlReturn;
2759 RETURN_STATUS_IF_ERROR(statusTFromBinderStatus(
2760 aps->getPreferredMixerAttributes(attrAidl, portIdAidl, &_aidlReturn)));
2761
2762 if (_aidlReturn.has_value()) {
2763 *mixerAttr = VALUE_OR_RETURN_STATUS(
2764 aidl2legacy_AudioMixerAttributesInternal_audio_mixer_attributes_t(
2765 _aidlReturn.value()));
2766 }
2767 return NO_ERROR;
2768}
2769
2770status_t AudioSystem::clearPreferredMixerAttributes(const audio_attributes_t *attr,
2771 audio_port_handle_t portId,
2772 uid_t uid) {
Andy Hung264fa4c2024-02-21 15:52:12 -08002773 const sp<IAudioPolicyService> aps = get_audio_policy_service();
jiabina84c3d32022-12-02 18:59:55 +00002774 if (aps == nullptr) {
2775 return PERMISSION_DENIED;
2776 }
2777
Mikhail Naganov1c400902023-05-17 11:48:43 -07002778 media::audio::common::AudioAttributes attrAidl = VALUE_OR_RETURN_STATUS(
2779 legacy2aidl_audio_attributes_t_AudioAttributes(*attr));
jiabina84c3d32022-12-02 18:59:55 +00002780 int32_t uidAidl = VALUE_OR_RETURN_STATUS(legacy2aidl_uid_t_int32_t(uid));
2781 int32_t portIdAidl = VALUE_OR_RETURN_STATUS(legacy2aidl_audio_port_handle_t_int32_t(portId));
2782 return statusTFromBinderStatus(
2783 aps->clearPreferredMixerAttributes(attrAidl, portIdAidl, uidAidl));
2784}
2785
Eric Laurentc2f1f072009-07-17 12:17:14 -07002786// ---------------------------------------------------------------------------
2787
Eric Laurente8726fe2015-06-26 09:39:24 -07002788int AudioSystem::AudioPolicyServiceClient::addAudioPortCallback(
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002789 const sp<AudioPortCallback>& callback) {
Andy Hungacd955a2024-02-16 17:14:36 -08002790 std::lock_guard _l(mMutex);
Andy Hungd211ce02024-02-16 15:34:07 -08002791 return mAudioPortCallbacks.insert(callback).second ? mAudioPortCallbacks.size() : -1;
Eric Laurentb28753e2015-04-01 13:06:28 -07002792}
2793
Eric Laurente8726fe2015-06-26 09:39:24 -07002794int AudioSystem::AudioPolicyServiceClient::removeAudioPortCallback(
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002795 const sp<AudioPortCallback>& callback) {
Andy Hungacd955a2024-02-16 17:14:36 -08002796 std::lock_guard _l(mMutex);
Andy Hungd211ce02024-02-16 15:34:07 -08002797 return mAudioPortCallbacks.erase(callback) > 0 ? mAudioPortCallbacks.size() : -1;
Eric Laurentb28753e2015-04-01 13:06:28 -07002798}
2799
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002800Status AudioSystem::AudioPolicyServiceClient::onAudioPortListUpdate() {
Andy Hungacd955a2024-02-16 17:14:36 -08002801 std::lock_guard _l(mMutex);
Andy Hungd211ce02024-02-16 15:34:07 -08002802 for (const auto& callback : mAudioPortCallbacks) {
2803 callback->onAudioPortListUpdate();
Eric Laurentb28753e2015-04-01 13:06:28 -07002804 }
Ytai Ben-Tsvi7e7a79d2020-12-15 16:48:16 -08002805 return Status::ok();
Eric Laurentb28753e2015-04-01 13:06:28 -07002806}
2807
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002808Status AudioSystem::AudioPolicyServiceClient::onAudioPatchListUpdate() {
Andy Hungacd955a2024-02-16 17:14:36 -08002809 std::lock_guard _l(mMutex);
Andy Hungd211ce02024-02-16 15:34:07 -08002810 for (const auto& callback : mAudioPortCallbacks) {
2811 callback->onAudioPatchListUpdate();
Eric Laurentb28753e2015-04-01 13:06:28 -07002812 }
Ytai Ben-Tsvi7e7a79d2020-12-15 16:48:16 -08002813 return Status::ok();
Eric Laurentb28753e2015-04-01 13:06:28 -07002814}
2815
François Gaffiecfe17322018-11-07 13:41:29 +01002816// ----------------------------------------------------------------------------
2817int AudioSystem::AudioPolicyServiceClient::addAudioVolumeGroupCallback(
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002818 const sp<AudioVolumeGroupCallback>& callback) {
Andy Hungacd955a2024-02-16 17:14:36 -08002819 std::lock_guard _l(mMutex);
Andy Hungd211ce02024-02-16 15:34:07 -08002820 return mAudioVolumeGroupCallbacks.insert(callback).second
2821 ? mAudioVolumeGroupCallbacks.size() : -1;
François Gaffiecfe17322018-11-07 13:41:29 +01002822}
2823
2824int AudioSystem::AudioPolicyServiceClient::removeAudioVolumeGroupCallback(
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002825 const sp<AudioVolumeGroupCallback>& callback) {
Andy Hungacd955a2024-02-16 17:14:36 -08002826 std::lock_guard _l(mMutex);
Andy Hungd211ce02024-02-16 15:34:07 -08002827 return mAudioVolumeGroupCallbacks.erase(callback) > 0
2828 ? mAudioVolumeGroupCallbacks.size() : -1;
François Gaffiecfe17322018-11-07 13:41:29 +01002829}
2830
Ytai Ben-Tsvi7e7a79d2020-12-15 16:48:16 -08002831Status AudioSystem::AudioPolicyServiceClient::onAudioVolumeGroupChanged(int32_t group,
2832 int32_t flags) {
2833 volume_group_t groupLegacy = VALUE_OR_RETURN_BINDER_STATUS(
2834 aidl2legacy_int32_t_volume_group_t(group));
2835 int flagsLegacy = VALUE_OR_RETURN_BINDER_STATUS(convertReinterpret<int>(flags));
2836
Andy Hungacd955a2024-02-16 17:14:36 -08002837 std::lock_guard _l(mMutex);
Andy Hungd211ce02024-02-16 15:34:07 -08002838 for (const auto& callback : mAudioVolumeGroupCallbacks) {
2839 callback->onAudioVolumeGroupChanged(groupLegacy, flagsLegacy);
François Gaffiecfe17322018-11-07 13:41:29 +01002840 }
Ytai Ben-Tsvi7e7a79d2020-12-15 16:48:16 -08002841 return Status::ok();
François Gaffiecfe17322018-11-07 13:41:29 +01002842}
2843// ----------------------------------------------------------------------------
2844
Ytai Ben-Tsvi7e7a79d2020-12-15 16:48:16 -08002845Status AudioSystem::AudioPolicyServiceClient::onDynamicPolicyMixStateUpdate(
2846 const ::std::string& regId, int32_t state) {
2847 ALOGV("AudioPolicyServiceClient::onDynamicPolicyMixStateUpdate(%s, %d)", regId.c_str(), state);
2848
2849 String8 regIdLegacy = VALUE_OR_RETURN_BINDER_STATUS(aidl2legacy_string_view_String8(regId));
2850 int stateLegacy = VALUE_OR_RETURN_BINDER_STATUS(convertReinterpret<int>(state));
Jean-Michel Trivif613d422015-04-23 18:41:29 -07002851 dynamic_policy_callback cb = NULL;
2852 {
Andy Hungacd955a2024-02-16 17:14:36 -08002853 std::lock_guard _l(AudioSystem::gMutex);
Jean-Michel Trivif613d422015-04-23 18:41:29 -07002854 cb = gDynPolicyCallback;
2855 }
2856
2857 if (cb != NULL) {
Ytai Ben-Tsvi7e7a79d2020-12-15 16:48:16 -08002858 cb(DYNAMIC_POLICY_EVENT_MIX_STATE_UPDATE, regIdLegacy, stateLegacy);
Jean-Michel Trivif613d422015-04-23 18:41:29 -07002859 }
Ytai Ben-Tsvi7e7a79d2020-12-15 16:48:16 -08002860 return Status::ok();
Jean-Michel Trivide801052015-04-14 19:10:14 -07002861}
2862
Ytai Ben-Tsvi7e7a79d2020-12-15 16:48:16 -08002863Status AudioSystem::AudioPolicyServiceClient::onRecordingConfigurationUpdate(
2864 int32_t event,
2865 const media::RecordClientInfo& clientInfo,
Mikhail Naganovdbf03642021-08-25 18:15:32 -07002866 const AudioConfigBase& clientConfig,
Ytai Ben-Tsvi7e7a79d2020-12-15 16:48:16 -08002867 const std::vector<media::EffectDescriptor>& clientEffects,
Mikhail Naganovdbf03642021-08-25 18:15:32 -07002868 const AudioConfigBase& deviceConfig,
Ytai Ben-Tsvi7e7a79d2020-12-15 16:48:16 -08002869 const std::vector<media::EffectDescriptor>& effects,
2870 int32_t patchHandle,
Mikhail Naganovddceecc2021-09-03 13:58:56 -07002871 AudioSource source) {
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -08002872 record_config_callback cb = NULL;
2873 {
Andy Hungacd955a2024-02-16 17:14:36 -08002874 std::lock_guard _l(AudioSystem::gMutex);
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -08002875 cb = gRecordConfigCallback;
2876 }
2877
2878 if (cb != NULL) {
Ytai Ben-Tsvi7e7a79d2020-12-15 16:48:16 -08002879 int eventLegacy = VALUE_OR_RETURN_BINDER_STATUS(convertReinterpret<int>(event));
2880 record_client_info_t clientInfoLegacy = VALUE_OR_RETURN_BINDER_STATUS(
2881 aidl2legacy_RecordClientInfo_record_client_info_t(clientInfo));
2882 audio_config_base_t clientConfigLegacy = VALUE_OR_RETURN_BINDER_STATUS(
Mikhail Naganovde3fa182021-07-30 15:06:42 -07002883 aidl2legacy_AudioConfigBase_audio_config_base_t(clientConfig, true /*isInput*/));
Ytai Ben-Tsvi7e7a79d2020-12-15 16:48:16 -08002884 std::vector<effect_descriptor_t> clientEffectsLegacy = VALUE_OR_RETURN_BINDER_STATUS(
2885 convertContainer<std::vector<effect_descriptor_t>>(
2886 clientEffects,
2887 aidl2legacy_EffectDescriptor_effect_descriptor_t));
2888 audio_config_base_t deviceConfigLegacy = VALUE_OR_RETURN_BINDER_STATUS(
Mikhail Naganovde3fa182021-07-30 15:06:42 -07002889 aidl2legacy_AudioConfigBase_audio_config_base_t(deviceConfig, true /*isInput*/));
Ytai Ben-Tsvi7e7a79d2020-12-15 16:48:16 -08002890 std::vector<effect_descriptor_t> effectsLegacy = VALUE_OR_RETURN_BINDER_STATUS(
2891 convertContainer<std::vector<effect_descriptor_t>>(
2892 effects,
2893 aidl2legacy_EffectDescriptor_effect_descriptor_t));
2894 audio_patch_handle_t patchHandleLegacy = VALUE_OR_RETURN_BINDER_STATUS(
2895 aidl2legacy_int32_t_audio_patch_handle_t(patchHandle));
2896 audio_source_t sourceLegacy = VALUE_OR_RETURN_BINDER_STATUS(
Mikhail Naganovddceecc2021-09-03 13:58:56 -07002897 aidl2legacy_AudioSource_audio_source_t(source));
Ytai Ben-Tsvi7e7a79d2020-12-15 16:48:16 -08002898 cb(eventLegacy, &clientInfoLegacy, &clientConfigLegacy, clientEffectsLegacy,
2899 &deviceConfigLegacy, effectsLegacy, patchHandleLegacy, sourceLegacy);
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -08002900 }
Ytai Ben-Tsvi7e7a79d2020-12-15 16:48:16 -08002901 return Status::ok();
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -08002902}
2903
Jean-Michel Trivi9a6b9ad2020-10-22 16:46:43 -07002904Status AudioSystem::AudioPolicyServiceClient::onRoutingUpdated() {
2905 routing_callback cb = NULL;
2906 {
Andy Hungacd955a2024-02-16 17:14:36 -08002907 std::lock_guard _l(AudioSystem::gMutex);
Jean-Michel Trivi9a6b9ad2020-10-22 16:46:43 -07002908 cb = gRoutingCallback;
2909 }
2910
2911 if (cb != NULL) {
2912 cb();
2913 }
2914 return Status::ok();
2915}
2916
Jean-Michel Trivi78f2b302022-04-15 18:18:41 +00002917Status AudioSystem::AudioPolicyServiceClient::onVolumeRangeInitRequest() {
2918 vol_range_init_req_callback cb = NULL;
2919 {
Andy Hungacd955a2024-02-16 17:14:36 -08002920 std::lock_guard _l(AudioSystem::gMutex);
Jean-Michel Trivi78f2b302022-04-15 18:18:41 +00002921 cb = gVolRangeInitReqCallback;
2922 }
2923
2924 if (cb != NULL) {
2925 cb();
2926 }
2927 return Status::ok();
2928}
2929
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002930void AudioSystem::AudioPolicyServiceClient::binderDied(const wp<IBinder>& who __unused) {
Glenn Kastend2d089f2014-11-05 11:48:12 -08002931 {
Andy Hungacd955a2024-02-16 17:14:36 -08002932 std::lock_guard _l(mMutex);
Andy Hungd211ce02024-02-16 15:34:07 -08002933 for (const auto& callback : mAudioPortCallbacks) {
2934 callback->onServiceDied();
Glenn Kastend2d089f2014-11-05 11:48:12 -08002935 }
Andy Hungd211ce02024-02-16 15:34:07 -08002936 for (const auto& callback : mAudioVolumeGroupCallbacks) {
2937 callback->onServiceDied();
François Gaffiecfe17322018-11-07 13:41:29 +01002938 }
Eric Laurentb52c1522014-05-20 11:27:36 -07002939 }
Mikhail Naganovec3d5792022-05-06 00:19:55 +00002940 AudioSystem::clearAudioPolicyService();
Eric Laurentc2f1f072009-07-17 12:17:14 -07002941
Steve Block5ff1dd52012-01-05 23:22:43 +00002942 ALOGW("AudioPolicyService server died!");
Eric Laurentc2f1f072009-07-17 12:17:14 -07002943}
2944
Ytai Ben-Tsvi7e7a79d2020-12-15 16:48:16 -08002945ConversionResult<record_client_info_t>
2946aidl2legacy_RecordClientInfo_record_client_info_t(const media::RecordClientInfo& aidl) {
2947 record_client_info_t legacy;
2948 legacy.riid = VALUE_OR_RETURN(aidl2legacy_int32_t_audio_unique_id_t(aidl.riid));
2949 legacy.uid = VALUE_OR_RETURN(aidl2legacy_int32_t_uid_t(aidl.uid));
2950 legacy.session = VALUE_OR_RETURN(aidl2legacy_int32_t_audio_session_t(aidl.session));
Mikhail Naganovddceecc2021-09-03 13:58:56 -07002951 legacy.source = VALUE_OR_RETURN(aidl2legacy_AudioSource_audio_source_t(aidl.source));
Ytai Ben-Tsvi7e7a79d2020-12-15 16:48:16 -08002952 legacy.port_id = VALUE_OR_RETURN(aidl2legacy_int32_t_audio_port_handle_t(aidl.portId));
2953 legacy.silenced = aidl.silenced;
2954 return legacy;
2955}
2956
2957ConversionResult<media::RecordClientInfo>
2958legacy2aidl_record_client_info_t_RecordClientInfo(const record_client_info_t& legacy) {
2959 media::RecordClientInfo aidl;
2960 aidl.riid = VALUE_OR_RETURN(legacy2aidl_audio_unique_id_t_int32_t(legacy.riid));
2961 aidl.uid = VALUE_OR_RETURN(legacy2aidl_uid_t_int32_t(legacy.uid));
2962 aidl.session = VALUE_OR_RETURN(legacy2aidl_audio_session_t_int32_t(legacy.session));
Mikhail Naganovddceecc2021-09-03 13:58:56 -07002963 aidl.source = VALUE_OR_RETURN(legacy2aidl_audio_source_t_AudioSource(legacy.source));
Ytai Ben-Tsvi7e7a79d2020-12-15 16:48:16 -08002964 aidl.portId = VALUE_OR_RETURN(legacy2aidl_audio_port_handle_t_int32_t(legacy.port_id));
2965 aidl.silenced = legacy.silenced;
2966 return aidl;
2967}
2968
Glenn Kasten40bc9062015-03-20 09:09:33 -07002969} // namespace android