blob: 3602e94f64f1cac8773b026d12e1dbba12dddd4f [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:
Shraddha Basantwanif2aa2502024-04-08 11:01:57 +000089 sp<ServiceInterface> getService()
Andy Hungda1fb072024-02-20 19:08:08 -080090 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
Shraddha Basantwanif2aa2502024-04-08 11:01:57 +0000146 if (mCanStartThreadPool) {
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
Shraddha Basantwanif2aa2502024-04-08 11:01:57 +0000189 void disableThreadPool() {
190 mCanStartThreadPool = false;
191 }
192
Andy Hungda1fb072024-02-20 19:08:08 -0800193private:
194 std::mutex mSingleGetter;
195 std::mutex mMutex;
196 std::shared_ptr<std::condition_variable> mCvGetter GUARDED_BY(mMutex);
197 sp<IBinder> mBinder GUARDED_BY(mMutex);
198 sp<ServiceInterface> mLocalService GUARDED_BY(mMutex);
199 sp<ServiceInterface> mService GUARDED_BY(mMutex);
200 sp<Client> mClient GUARDED_BY(mMutex);
Shraddha Basantwanif2aa2502024-04-08 11:01:57 +0000201 std::atomic<bool> mCanStartThreadPool = true;
Andy Hungda1fb072024-02-20 19:08:08 -0800202};
203
204struct AudioFlingerTraits {
205 static void onServiceCreate(
206 const sp<IAudioFlinger>& af, const sp<AudioSystem::AudioFlingerClient>& afc) {
207 const int64_t token = IPCThreadState::self()->clearCallingIdentity();
208 af->registerClient(afc);
209 IPCThreadState::self()->restoreCallingIdentity(token);
210 }
211
212 static sp<IAudioFlinger> createServiceAdapter(
213 const sp<media::IAudioFlingerService>& aidlInterface) {
214 return sp<AudioFlingerClientAdapter>::make(aidlInterface);
215 }
216
217 static void onClearService(const sp<AudioSystem::AudioFlingerClient>& afc) {
218 afc->clearIoCache();
219 }
220
221 static constexpr const char* SERVICE_NAME = IAudioFlinger::DEFAULT_SERVICE_NAME;
222};
223
224[[clang::no_destroy]] static constinit ServiceHandler<IAudioFlinger,
225 AudioSystem::AudioFlingerClient, media::IAudioFlingerService,
226 AudioFlingerTraits> gAudioFlingerServiceHandler;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800227
Andy Hung264fa4c2024-02-21 15:52:12 -0800228sp<IAudioFlinger> AudioSystem::get_audio_flinger() {
Andy Hungda1fb072024-02-20 19:08:08 -0800229 return gAudioFlingerServiceHandler.getService();
Akshata Kadam1d486b72023-08-07 05:49:48 +0000230}
231
Andy Hungda1fb072024-02-20 19:08:08 -0800232sp<AudioSystem::AudioFlingerClient> AudioSystem::getAudioFlingerClient() {
233 return gAudioFlingerServiceHandler.getClient();
234}
235
236void AudioSystem::setAudioFlingerBinder(const sp<IBinder>& audioFlinger) {
237 if (audioFlinger->getInterfaceDescriptor() != media::IAudioFlingerService::descriptor) {
238 ALOGE("%s: received a binder of type %s",
239 __func__, String8(audioFlinger->getInterfaceDescriptor()).c_str());
240 return;
241 }
242 gAudioFlingerServiceHandler.setBinder(audioFlinger);
243}
244
245status_t AudioSystem::setLocalAudioFlinger(const sp<IAudioFlinger>& af) {
246 return gAudioFlingerServiceHandler.setLocalService(af);
Eric Laurent296fb132015-05-01 11:38:42 -0700247}
248
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800249sp<AudioIoDescriptor> AudioSystem::getIoDescriptor(audio_io_handle_t ioHandle) {
Eric Laurent296fb132015-05-01 11:38:42 -0700250 sp<AudioIoDescriptor> desc;
251 const sp<AudioFlingerClient> afc = getAudioFlingerClient();
252 if (afc != 0) {
253 desc = afc->getIoDescriptor(ioHandle);
254 }
255 return desc;
256}
257
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800258/* static */ status_t AudioSystem::checkAudioFlinger() {
Eric Laurent46291612013-07-18 14:38:44 -0700259 if (defaultServiceManager()->checkService(String16("media.audio_flinger")) != 0) {
260 return NO_ERROR;
261 }
262 return DEAD_OBJECT;
263}
264
Glenn Kasten4a8308b2016-04-18 14:10:01 -0700265// FIXME Declare in binder opcode order, similarly to IAudioFlinger.h and IAudioFlinger.cpp
266
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800267status_t AudioSystem::muteMicrophone(bool state) {
Andy Hung264fa4c2024-02-21 15:52:12 -0800268 const sp<IAudioFlinger> af = get_audio_flinger();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800269 if (af == 0) return PERMISSION_DENIED;
270 return af->setMicMute(state);
271}
272
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800273status_t AudioSystem::isMicrophoneMuted(bool* state) {
Andy Hung264fa4c2024-02-21 15:52:12 -0800274 const sp<IAudioFlinger> af = get_audio_flinger();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800275 if (af == 0) return PERMISSION_DENIED;
276 *state = af->getMicMute();
277 return NO_ERROR;
278}
279
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800280status_t AudioSystem::setMasterVolume(float value) {
Andy Hung264fa4c2024-02-21 15:52:12 -0800281 const sp<IAudioFlinger> af = get_audio_flinger();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800282 if (af == 0) return PERMISSION_DENIED;
283 af->setMasterVolume(value);
284 return NO_ERROR;
285}
286
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800287status_t AudioSystem::setMasterMute(bool mute) {
Andy Hung264fa4c2024-02-21 15:52:12 -0800288 const sp<IAudioFlinger> af = get_audio_flinger();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800289 if (af == 0) return PERMISSION_DENIED;
290 af->setMasterMute(mute);
291 return NO_ERROR;
292}
293
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800294status_t AudioSystem::getMasterVolume(float* volume) {
Andy Hung264fa4c2024-02-21 15:52:12 -0800295 const sp<IAudioFlinger> af = get_audio_flinger();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800296 if (af == 0) return PERMISSION_DENIED;
297 *volume = af->masterVolume();
298 return NO_ERROR;
299}
300
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800301status_t AudioSystem::getMasterMute(bool* mute) {
Andy Hung264fa4c2024-02-21 15:52:12 -0800302 const sp<IAudioFlinger> af = get_audio_flinger();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800303 if (af == 0) return PERMISSION_DENIED;
304 *mute = af->masterMute();
305 return NO_ERROR;
306}
307
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800308status_t AudioSystem::setStreamVolume(audio_stream_type_t stream, float value,
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800309 audio_io_handle_t output) {
Dima Zavinfce7a472011-04-19 22:30:36 -0700310 if (uint32_t(stream) >= AUDIO_STREAM_CNT) return BAD_VALUE;
Andy Hung264fa4c2024-02-21 15:52:12 -0800311 const sp<IAudioFlinger> af = get_audio_flinger();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800312 if (af == 0) return PERMISSION_DENIED;
Eric Laurentc2f1f072009-07-17 12:17:14 -0700313 af->setStreamVolume(stream, value, output);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800314 return NO_ERROR;
315}
316
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800317status_t AudioSystem::setStreamMute(audio_stream_type_t stream, bool mute) {
Dima Zavinfce7a472011-04-19 22:30:36 -0700318 if (uint32_t(stream) >= AUDIO_STREAM_CNT) return BAD_VALUE;
Andy Hung264fa4c2024-02-21 15:52:12 -0800319 const sp<IAudioFlinger> af = get_audio_flinger();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800320 if (af == 0) return PERMISSION_DENIED;
321 af->setStreamMute(stream, mute);
322 return NO_ERROR;
323}
324
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800325status_t AudioSystem::setMode(audio_mode_t mode) {
Glenn Kasten930f4ca2012-01-06 16:47:31 -0800326 if (uint32_t(mode) >= AUDIO_MODE_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;
329 return af->setMode(mode);
330}
331
Mikhail Naganovb1ddbb02023-03-15 17:06:59 -0700332status_t AudioSystem::setSimulateDeviceConnections(bool enabled) {
Andy Hung264fa4c2024-02-21 15:52:12 -0800333 const sp<IAudioFlinger> af = get_audio_flinger();
Mikhail Naganovb1ddbb02023-03-15 17:06:59 -0700334 if (af == 0) return PERMISSION_DENIED;
335 return af->setSimulateDeviceConnections(enabled);
336}
337
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800338status_t AudioSystem::setParameters(audio_io_handle_t ioHandle, const String8& keyValuePairs) {
Andy Hung264fa4c2024-02-21 15:52:12 -0800339 const sp<IAudioFlinger> af = get_audio_flinger();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800340 if (af == 0) return PERMISSION_DENIED;
Eric Laurentc2f1f072009-07-17 12:17:14 -0700341 return af->setParameters(ioHandle, keyValuePairs);
342}
343
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800344String8 AudioSystem::getParameters(audio_io_handle_t ioHandle, const String8& keys) {
Andy Hung264fa4c2024-02-21 15:52:12 -0800345 const sp<IAudioFlinger> af = get_audio_flinger();
Eric Laurentc2f1f072009-07-17 12:17:14 -0700346 String8 result = String8("");
347 if (af == 0) return result;
348
349 result = af->getParameters(ioHandle, keys);
350 return result;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800351}
352
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800353status_t AudioSystem::setParameters(const String8& keyValuePairs) {
Glenn Kasten142f5192014-03-25 17:44:59 -0700354 return setParameters(AUDIO_IO_HANDLE_NONE, keyValuePairs);
Glenn Kastenc23885e2013-12-19 16:35:18 -0800355}
356
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800357String8 AudioSystem::getParameters(const String8& keys) {
Glenn Kasten142f5192014-03-25 17:44:59 -0700358 return getParameters(AUDIO_IO_HANDLE_NONE, keys);
Glenn Kastenc23885e2013-12-19 16:35:18 -0800359}
360
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800361// convert volume steps to natural log scale
362
363// change this value to change volume scaling
Andy Hungacd955a2024-02-16 17:14:36 -0800364constexpr float kdbPerStep = 0.5f;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800365// shouldn't need to touch these
Andy Hungacd955a2024-02-16 17:14:36 -0800366constexpr float kdBConvert = -kdbPerStep * 2.302585093f / 20.0f;
367constexpr float kdBConvertInverse = 1.0f / kdBConvert;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800368
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800369float AudioSystem::linearToLog(int volume) {
Andy Hungacd955a2024-02-16 17:14:36 -0800370 // float v = volume ? exp(float(100 - volume) * kdBConvert) : 0;
Steve Blockb8a80522011-12-20 16:23:08 +0000371 // ALOGD("linearToLog(%d)=%f", volume, v);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800372 // return v;
Andy Hungacd955a2024-02-16 17:14:36 -0800373 return volume ? exp(float(100 - volume) * kdBConvert) : 0;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800374}
375
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800376int AudioSystem::logToLinear(float volume) {
Andy Hungacd955a2024-02-16 17:14:36 -0800377 // int v = volume ? 100 - int(kdBConvertInverse * log(volume) + 0.5) : 0;
Steve Blockb8a80522011-12-20 16:23:08 +0000378 // ALOGD("logTolinear(%d)=%f", v, volume);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800379 // return v;
Andy Hungacd955a2024-02-16 17:14:36 -0800380 return volume ? 100 - int(kdBConvertInverse * log(volume) + 0.5) : 0;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800381}
382
Eric Laurent21da6472017-11-09 16:29:26 -0800383/* static */ size_t AudioSystem::calculateMinFrameCount(
384 uint32_t afLatencyMs, uint32_t afFrameCount, uint32_t afSampleRate,
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800385 uint32_t sampleRate, float speed /*, uint32_t notificationsPerBufferReq*/) {
Eric Laurent21da6472017-11-09 16:29:26 -0800386 // Ensure that buffer depth covers at least audio hardware latency
387 uint32_t minBufCount = afLatencyMs / ((1000 * afFrameCount) / afSampleRate);
388 if (minBufCount < 2) {
389 minBufCount = 2;
390 }
391#if 0
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800392 // The notificationsPerBufferReq parameter is not yet used for non-fast tracks,
393 // but keeping the code here to make it easier to add later.
394 if (minBufCount < notificationsPerBufferReq) {
395 minBufCount = notificationsPerBufferReq;
396 }
Eric Laurent21da6472017-11-09 16:29:26 -0800397#endif
398 ALOGV("calculateMinFrameCount afLatency %u afFrameCount %u afSampleRate %u "
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800399 "sampleRate %u speed %f minBufCount: %u" /*" notificationsPerBufferReq %u"*/,
400 afLatencyMs, afFrameCount, afSampleRate, sampleRate, speed, minBufCount
401 /*, notificationsPerBufferReq*/);
Eric Laurent21da6472017-11-09 16:29:26 -0800402 return minBufCount * sourceFramesNeededWithTimestretch(
403 sampleRate, afFrameCount, afSampleRate, speed);
404}
405
406
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800407status_t
408AudioSystem::getOutputSamplingRate(uint32_t* samplingRate, audio_stream_type_t streamType) {
Eric Laurentc2f1f072009-07-17 12:17:14 -0700409 audio_io_handle_t output;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800410
Dima Zavinfce7a472011-04-19 22:30:36 -0700411 if (streamType == AUDIO_STREAM_DEFAULT) {
412 streamType = AUDIO_STREAM_MUSIC;
Eric Laurentc2f1f072009-07-17 12:17:14 -0700413 }
414
Glenn Kastenfff6d712012-01-12 16:38:12 -0800415 output = getOutput(streamType);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700416 if (output == 0) {
417 return PERMISSION_DENIED;
418 }
419
Jean-Michel Trivib7f24b12014-06-11 10:05:30 -0700420 return getSamplingRate(output, samplingRate);
Eric Laurent1a9ed112012-03-20 18:36:01 -0700421}
422
Glenn Kasten2c073da2016-02-26 09:14:08 -0800423status_t AudioSystem::getSamplingRate(audio_io_handle_t ioHandle,
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800424 uint32_t* samplingRate) {
Andy Hung264fa4c2024-02-21 15:52:12 -0800425 const sp<IAudioFlinger> af = get_audio_flinger();
Eric Laurentf6778fd2014-11-18 17:26:58 -0800426 if (af == 0) return PERMISSION_DENIED;
Glenn Kasten2c073da2016-02-26 09:14:08 -0800427 sp<AudioIoDescriptor> desc = getIoDescriptor(ioHandle);
428 if (desc == 0) {
429 *samplingRate = af->sampleRate(ioHandle);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700430 } else {
Mikhail Naganov88536df2021-07-26 17:30:29 -0700431 *samplingRate = desc->getSamplingRate();
Eric Laurentc2f1f072009-07-17 12:17:14 -0700432 }
Glenn Kastenf94006c2014-01-08 08:56:06 -0800433 if (*samplingRate == 0) {
Glenn Kasten2c073da2016-02-26 09:14:08 -0800434 ALOGE("AudioSystem::getSamplingRate failed for ioHandle %d", ioHandle);
Glenn Kastenf94006c2014-01-08 08:56:06 -0800435 return BAD_VALUE;
436 }
Eric Laurentc2f1f072009-07-17 12:17:14 -0700437
Glenn Kasten2c073da2016-02-26 09:14:08 -0800438 ALOGV("getSamplingRate() ioHandle %d, sampling rate %u", ioHandle, *samplingRate);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700439
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800440 return NO_ERROR;
441}
442
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800443status_t AudioSystem::getOutputFrameCount(size_t* frameCount, audio_stream_type_t streamType) {
Eric Laurentc2f1f072009-07-17 12:17:14 -0700444 audio_io_handle_t output;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800445
Dima Zavinfce7a472011-04-19 22:30:36 -0700446 if (streamType == AUDIO_STREAM_DEFAULT) {
447 streamType = AUDIO_STREAM_MUSIC;
Eric Laurentc2f1f072009-07-17 12:17:14 -0700448 }
Eric Laurent48f7f5e2009-04-02 09:32:43 -0700449
Glenn Kastenfff6d712012-01-12 16:38:12 -0800450 output = getOutput(streamType);
Glenn Kasten142f5192014-03-25 17:44:59 -0700451 if (output == AUDIO_IO_HANDLE_NONE) {
Eric Laurentc2f1f072009-07-17 12:17:14 -0700452 return PERMISSION_DENIED;
453 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800454
Jean-Michel Trivib7f24b12014-06-11 10:05:30 -0700455 return getFrameCount(output, frameCount);
Eric Laurent1a9ed112012-03-20 18:36:01 -0700456}
457
Glenn Kasten2c073da2016-02-26 09:14:08 -0800458status_t AudioSystem::getFrameCount(audio_io_handle_t ioHandle,
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800459 size_t* frameCount) {
Andy Hung264fa4c2024-02-21 15:52:12 -0800460 const sp<IAudioFlinger> af = get_audio_flinger();
Eric Laurentf6778fd2014-11-18 17:26:58 -0800461 if (af == 0) return PERMISSION_DENIED;
Glenn Kasten2c073da2016-02-26 09:14:08 -0800462 sp<AudioIoDescriptor> desc = getIoDescriptor(ioHandle);
463 if (desc == 0) {
464 *frameCount = af->frameCount(ioHandle);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700465 } else {
Mikhail Naganov88536df2021-07-26 17:30:29 -0700466 *frameCount = desc->getFrameCount();
Eric Laurentc2f1f072009-07-17 12:17:14 -0700467 }
Glenn Kastenf94006c2014-01-08 08:56:06 -0800468 if (*frameCount == 0) {
Glenn Kasten2c073da2016-02-26 09:14:08 -0800469 ALOGE("AudioSystem::getFrameCount failed for ioHandle %d", ioHandle);
Glenn Kastenf94006c2014-01-08 08:56:06 -0800470 return BAD_VALUE;
471 }
Eric Laurentc2f1f072009-07-17 12:17:14 -0700472
Glenn Kasten2c073da2016-02-26 09:14:08 -0800473 ALOGV("getFrameCount() ioHandle %d, frameCount %zu", ioHandle, *frameCount);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700474
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800475 return NO_ERROR;
476}
477
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800478status_t AudioSystem::getOutputLatency(uint32_t* latency, audio_stream_type_t streamType) {
Eric Laurentc2f1f072009-07-17 12:17:14 -0700479 audio_io_handle_t output;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800480
Dima Zavinfce7a472011-04-19 22:30:36 -0700481 if (streamType == AUDIO_STREAM_DEFAULT) {
482 streamType = AUDIO_STREAM_MUSIC;
Eric Laurentc2f1f072009-07-17 12:17:14 -0700483 }
Eric Laurent48f7f5e2009-04-02 09:32:43 -0700484
Glenn Kastenfff6d712012-01-12 16:38:12 -0800485 output = getOutput(streamType);
Glenn Kasten142f5192014-03-25 17:44:59 -0700486 if (output == AUDIO_IO_HANDLE_NONE) {
Eric Laurentc2f1f072009-07-17 12:17:14 -0700487 return PERMISSION_DENIED;
488 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800489
Glenn Kasten241618f2014-03-25 17:48:57 -0700490 return getLatency(output, latency);
Eric Laurent1a9ed112012-03-20 18:36:01 -0700491}
492
493status_t AudioSystem::getLatency(audio_io_handle_t output,
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800494 uint32_t* latency) {
Andy Hung264fa4c2024-02-21 15:52:12 -0800495 const sp<IAudioFlinger> af = get_audio_flinger();
Eric Laurentf6778fd2014-11-18 17:26:58 -0800496 if (af == 0) return PERMISSION_DENIED;
Eric Laurent296fb132015-05-01 11:38:42 -0700497 sp<AudioIoDescriptor> outputDesc = getIoDescriptor(output);
Eric Laurent73e26b62015-04-27 16:55:58 -0700498 if (outputDesc == 0) {
Eric Laurentc2f1f072009-07-17 12:17:14 -0700499 *latency = af->latency(output);
500 } else {
Mikhail Naganov88536df2021-07-26 17:30:29 -0700501 *latency = outputDesc->getLatency();
Eric Laurentc2f1f072009-07-17 12:17:14 -0700502 }
503
Glenn Kasten241618f2014-03-25 17:48:57 -0700504 ALOGV("getLatency() output %d, latency %d", output, *latency);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700505
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800506 return NO_ERROR;
507}
508
Glenn Kastendd8104c2012-07-02 12:42:44 -0700509status_t AudioSystem::getInputBufferSize(uint32_t sampleRate, audio_format_t format,
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800510 audio_channel_mask_t channelMask, size_t* buffSize) {
Eric Laurent296fb132015-05-01 11:38:42 -0700511 const sp<AudioFlingerClient> afc = getAudioFlingerClient();
512 if (afc == 0) {
513 return NO_INIT;
514 }
515 return afc->getInputBufferSize(sampleRate, format, channelMask, buffSize);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800516}
517
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800518status_t AudioSystem::setVoiceVolume(float value) {
Andy Hung264fa4c2024-02-21 15:52:12 -0800519 const sp<IAudioFlinger> af = get_audio_flinger();
Eric Laurentf0ee6f42009-10-21 08:14:22 -0700520 if (af == 0) return PERMISSION_DENIED;
521 return af->setVoiceVolume(value);
522}
523
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800524status_t AudioSystem::getRenderPosition(audio_io_handle_t output, uint32_t* halFrames,
525 uint32_t* dspFrames) {
Andy Hung264fa4c2024-02-21 15:52:12 -0800526 const sp<IAudioFlinger> af = get_audio_flinger();
Eric Laurent342e9cf2010-01-19 17:37:09 -0800527 if (af == 0) return PERMISSION_DENIED;
528
Richard Fitzgeraldad3af332013-03-25 16:54:37 +0000529 return af->getRenderPosition(halFrames, dspFrames, output);
Eric Laurent342e9cf2010-01-19 17:37:09 -0800530}
531
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800532uint32_t AudioSystem::getInputFramesLost(audio_io_handle_t ioHandle) {
Andy Hung264fa4c2024-02-21 15:52:12 -0800533 const sp<IAudioFlinger> af = get_audio_flinger();
Glenn Kasten5f972c02014-01-13 09:59:31 -0800534 uint32_t result = 0;
Eric Laurent05bca2f2010-02-26 02:47:27 -0800535 if (af == 0) return result;
Glenn Kasten142f5192014-03-25 17:44:59 -0700536 if (ioHandle == AUDIO_IO_HANDLE_NONE) return result;
Eric Laurent05bca2f2010-02-26 02:47:27 -0800537
538 result = af->getInputFramesLost(ioHandle);
539 return result;
540}
541
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800542audio_unique_id_t AudioSystem::newAudioUniqueId(audio_unique_id_use_t use) {
Mikhail Naganov2996f672019-04-18 12:29:59 -0700543 // Must not use AF as IDs will re-roll on audioserver restart, b/130369529.
Andy Hung264fa4c2024-02-21 15:52:12 -0800544 const sp<IAudioFlinger> af = get_audio_flinger();
Eric Laurentde3f8392014-07-27 18:38:22 -0700545 if (af == 0) return AUDIO_UNIQUE_ID_ALLOCATE;
Glenn Kasteneeecb982016-02-26 10:44:04 -0800546 return af->newAudioUniqueId(use);
Eric Laurentbe916aa2010-06-01 23:49:17 -0700547}
548
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800549void AudioSystem::acquireAudioSessionId(audio_session_t audioSession, pid_t pid, uid_t uid) {
Andy Hung264fa4c2024-02-21 15:52:12 -0800550 const sp<IAudioFlinger> af = get_audio_flinger();
Marco Nelissen3a34bef2011-08-02 13:33:41 -0700551 if (af != 0) {
Andy Hung8b0bfd92019-12-23 13:11:11 -0800552 af->acquireAudioSessionId(audioSession, pid, uid);
Marco Nelissen3a34bef2011-08-02 13:33:41 -0700553 }
554}
555
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800556void AudioSystem::releaseAudioSessionId(audio_session_t audioSession, pid_t pid) {
Andy Hung264fa4c2024-02-21 15:52:12 -0800557 const sp<IAudioFlinger> af = get_audio_flinger();
Marco Nelissen3a34bef2011-08-02 13:33:41 -0700558 if (af != 0) {
Marco Nelissend457c972014-02-11 08:47:07 -0800559 af->releaseAudioSessionId(audioSession, pid);
Marco Nelissen3a34bef2011-08-02 13:33:41 -0700560 }
561}
562
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800563audio_hw_sync_t AudioSystem::getAudioHwSyncForSession(audio_session_t sessionId) {
Andy Hung264fa4c2024-02-21 15:52:12 -0800564 const sp<IAudioFlinger> af = get_audio_flinger();
Eric Laurent93c3d412014-08-01 14:48:35 -0700565 if (af == 0) return AUDIO_HW_SYNC_INVALID;
566 return af->getAudioHwSyncForSession(sessionId);
567}
568
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800569status_t AudioSystem::systemReady() {
Andy Hung264fa4c2024-02-21 15:52:12 -0800570 const sp<IAudioFlinger> af = get_audio_flinger();
Eric Laurent72e3f392015-05-20 14:43:50 -0700571 if (af == 0) return NO_INIT;
572 return af->systemReady();
573}
574
Eric Laurentd66d7a12021-07-13 13:35:32 +0200575status_t AudioSystem::audioPolicyReady() {
Andy Hung264fa4c2024-02-21 15:52:12 -0800576 const sp<IAudioFlinger> af = get_audio_flinger();
Eric Laurentd66d7a12021-07-13 13:35:32 +0200577 if (af == 0) return NO_INIT;
578 return af->audioPolicyReady();
579}
580
Glenn Kasten4a8308b2016-04-18 14:10:01 -0700581status_t AudioSystem::getFrameCountHAL(audio_io_handle_t ioHandle,
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800582 size_t* frameCount) {
Andy Hung264fa4c2024-02-21 15:52:12 -0800583 const sp<IAudioFlinger> af = get_audio_flinger();
Glenn Kasten4a8308b2016-04-18 14:10:01 -0700584 if (af == 0) return PERMISSION_DENIED;
585 sp<AudioIoDescriptor> desc = getIoDescriptor(ioHandle);
586 if (desc == 0) {
587 *frameCount = af->frameCountHAL(ioHandle);
588 } else {
Mikhail Naganov88536df2021-07-26 17:30:29 -0700589 *frameCount = desc->getFrameCountHAL();
Glenn Kasten4a8308b2016-04-18 14:10:01 -0700590 }
591 if (*frameCount == 0) {
592 ALOGE("AudioSystem::getFrameCountHAL failed for ioHandle %d", ioHandle);
593 return BAD_VALUE;
594 }
595
596 ALOGV("getFrameCountHAL() ioHandle %d, frameCount %zu", ioHandle, *frameCount);
597
598 return NO_ERROR;
599}
600
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800601// ---------------------------------------------------------------------------
602
Eric Laurent73e26b62015-04-27 16:55:58 -0700603
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800604void AudioSystem::AudioFlingerClient::clearIoCache() {
Andy Hungacd955a2024-02-16 17:14:36 -0800605 std::lock_guard _l(mMutex);
Eric Laurent73e26b62015-04-27 16:55:58 -0700606 mIoDescriptors.clear();
607 mInBuffSize = 0;
608 mInSamplingRate = 0;
609 mInFormat = AUDIO_FORMAT_DEFAULT;
610 mInChannelMask = AUDIO_CHANNEL_NONE;
611}
612
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800613void AudioSystem::AudioFlingerClient::binderDied(const wp<IBinder>& who __unused) {
Andy Hungda1fb072024-02-20 19:08:08 -0800614 gAudioFlingerServiceHandler.clearService();
Ytai Ben-Tsvi000c3e42020-01-09 15:26:40 -0800615 reportError(DEAD_OBJECT);
616
Steve Block5ff1dd52012-01-05 23:22:43 +0000617 ALOGW("AudioFlinger server died!");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800618}
619
Ytai Ben-Tsvi10dc0a62020-09-18 11:31:55 -0700620Status AudioSystem::AudioFlingerClient::ioConfigChanged(
621 media::AudioIoConfigEvent _event,
622 const media::AudioIoDescriptor& _ioDesc) {
Mikhail Naganov88536df2021-07-26 17:30:29 -0700623 audio_io_config_event_t event = VALUE_OR_RETURN_BINDER_STATUS(
624 aidl2legacy_AudioIoConfigEvent_audio_io_config_event_t(_event));
Ytai Ben-Tsvi10dc0a62020-09-18 11:31:55 -0700625 sp<AudioIoDescriptor> ioDesc(
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800626 VALUE_OR_RETURN_BINDER_STATUS(
627 aidl2legacy_AudioIoDescriptor_AudioIoDescriptor(_ioDesc)));
Ytai Ben-Tsvi10dc0a62020-09-18 11:31:55 -0700628
Steve Block3856b092011-10-20 11:56:00 +0100629 ALOGV("ioConfigChanged() event %d", event);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700630
Mikhail Naganov88536df2021-07-26 17:30:29 -0700631 if (ioDesc->getIoHandle() == AUDIO_IO_HANDLE_NONE) return Status::ok();
Eric Laurentc2f1f072009-07-17 12:17:14 -0700632
Eric Laurent296fb132015-05-01 11:38:42 -0700633 audio_port_handle_t deviceId = AUDIO_PORT_HANDLE_NONE;
Eric Laurent09f1ed22019-04-24 17:45:17 -0700634 std::vector<sp<AudioDeviceCallback>> callbacksToCall;
Eric Laurent296fb132015-05-01 11:38:42 -0700635 {
Andy Hungacd955a2024-02-16 17:14:36 -0800636 std::lock_guard _l(mMutex);
Eric Laurent09f1ed22019-04-24 17:45:17 -0700637 auto callbacks = std::map<audio_port_handle_t, wp<AudioDeviceCallback>>();
Eric Laurent296fb132015-05-01 11:38:42 -0700638
639 switch (event) {
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800640 case AUDIO_OUTPUT_OPENED:
641 case AUDIO_OUTPUT_REGISTERED:
642 case AUDIO_INPUT_OPENED:
643 case AUDIO_INPUT_REGISTERED: {
Andy Hungd211ce02024-02-16 15:34:07 -0800644 if (sp<AudioIoDescriptor> oldDesc = getIoDescriptor_l(ioDesc->getIoHandle())) {
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800645 deviceId = oldDesc->getDeviceId();
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800646 }
Andy Hungd211ce02024-02-16 15:34:07 -0800647 mIoDescriptors[ioDesc->getIoHandle()] = ioDesc;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800648
649 if (ioDesc->getDeviceId() != AUDIO_PORT_HANDLE_NONE) {
650 deviceId = ioDesc->getDeviceId();
651 if (event == AUDIO_OUTPUT_OPENED || event == AUDIO_INPUT_OPENED) {
Mikhail Naganov88536df2021-07-26 17:30:29 -0700652 auto it = mAudioDeviceCallbacks.find(ioDesc->getIoHandle());
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800653 if (it != mAudioDeviceCallbacks.end()) {
654 callbacks = it->second;
655 }
656 }
657 }
Mikhail Naganov88536df2021-07-26 17:30:29 -0700658 ALOGV("ioConfigChanged() new %s %s %s",
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800659 event == AUDIO_OUTPUT_OPENED || event == AUDIO_OUTPUT_REGISTERED ?
660 "output" : "input",
661 event == AUDIO_OUTPUT_OPENED || event == AUDIO_INPUT_OPENED ?
662 "opened" : "registered",
Mikhail Naganov88536df2021-07-26 17:30:29 -0700663 ioDesc->toDebugString().c_str());
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800664 }
665 break;
666 case AUDIO_OUTPUT_CLOSED:
667 case AUDIO_INPUT_CLOSED: {
Mikhail Naganov88536df2021-07-26 17:30:29 -0700668 if (getIoDescriptor_l(ioDesc->getIoHandle()) == 0) {
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800669 ALOGW("ioConfigChanged() closing unknown %s %d",
Mikhail Naganov88536df2021-07-26 17:30:29 -0700670 event == AUDIO_OUTPUT_CLOSED ? "output" : "input", ioDesc->getIoHandle());
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800671 break;
672 }
673 ALOGV("ioConfigChanged() %s %d closed",
Mikhail Naganov88536df2021-07-26 17:30:29 -0700674 event == AUDIO_OUTPUT_CLOSED ? "output" : "input", ioDesc->getIoHandle());
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800675
Andy Hungd211ce02024-02-16 15:34:07 -0800676 mIoDescriptors.erase(ioDesc->getIoHandle());
Mikhail Naganov88536df2021-07-26 17:30:29 -0700677 mAudioDeviceCallbacks.erase(ioDesc->getIoHandle());
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800678 }
679 break;
680
681 case AUDIO_OUTPUT_CONFIG_CHANGED:
682 case AUDIO_INPUT_CONFIG_CHANGED: {
Mikhail Naganov88536df2021-07-26 17:30:29 -0700683 sp<AudioIoDescriptor> oldDesc = getIoDescriptor_l(ioDesc->getIoHandle());
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800684 if (oldDesc == 0) {
685 ALOGW("ioConfigChanged() modifying unknown %s! %d",
686 event == AUDIO_OUTPUT_CONFIG_CHANGED ? "output" : "input",
Mikhail Naganov88536df2021-07-26 17:30:29 -0700687 ioDesc->getIoHandle());
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800688 break;
689 }
690
Eric Laurent7c1ec5f2015-07-09 14:52:47 -0700691 deviceId = oldDesc->getDeviceId();
Andy Hungd211ce02024-02-16 15:34:07 -0800692 mIoDescriptors[ioDesc->getIoHandle()] = ioDesc;
Eric Laurent296fb132015-05-01 11:38:42 -0700693
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800694 if (deviceId != ioDesc->getDeviceId()) {
695 deviceId = ioDesc->getDeviceId();
Mikhail Naganov88536df2021-07-26 17:30:29 -0700696 auto it = mAudioDeviceCallbacks.find(ioDesc->getIoHandle());
Eric Laurent09f1ed22019-04-24 17:45:17 -0700697 if (it != mAudioDeviceCallbacks.end()) {
698 callbacks = it->second;
Francois Gaffie24a9fb02019-01-18 17:51:34 +0100699 }
700 }
Mikhail Naganov88536df2021-07-26 17:30:29 -0700701 ALOGV("ioConfigChanged() new config for %s %s",
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800702 event == AUDIO_OUTPUT_CONFIG_CHANGED ? "output" : "input",
Mikhail Naganov88536df2021-07-26 17:30:29 -0700703 ioDesc->toDebugString().c_str());
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800704
Eric Laurent296fb132015-05-01 11:38:42 -0700705 }
Eric Laurent296fb132015-05-01 11:38:42 -0700706 break;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800707 case AUDIO_CLIENT_STARTED: {
Mikhail Naganov88536df2021-07-26 17:30:29 -0700708 sp<AudioIoDescriptor> oldDesc = getIoDescriptor_l(ioDesc->getIoHandle());
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800709 if (oldDesc == 0) {
Mikhail Naganov88536df2021-07-26 17:30:29 -0700710 ALOGW("ioConfigChanged() start client on unknown io! %d",
711 ioDesc->getIoHandle());
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800712 break;
713 }
714 ALOGV("ioConfigChanged() AUDIO_CLIENT_STARTED io %d port %d num callbacks %zu",
Mikhail Naganov88536df2021-07-26 17:30:29 -0700715 ioDesc->getIoHandle(), ioDesc->getPortId(), mAudioDeviceCallbacks.size());
716 oldDesc->setPatch(ioDesc->getPatch());
717 auto it = mAudioDeviceCallbacks.find(ioDesc->getIoHandle());
Eric Laurent09f1ed22019-04-24 17:45:17 -0700718 if (it != mAudioDeviceCallbacks.end()) {
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800719 auto cbks = it->second;
Mikhail Naganov88536df2021-07-26 17:30:29 -0700720 auto it2 = cbks.find(ioDesc->getPortId());
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800721 if (it2 != cbks.end()) {
Mikhail Naganov88536df2021-07-26 17:30:29 -0700722 callbacks.emplace(ioDesc->getPortId(), it2->second);
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800723 deviceId = oldDesc->getDeviceId();
724 }
Eric Laurent09f1ed22019-04-24 17:45:17 -0700725 }
Eric Laurent296fb132015-05-01 11:38:42 -0700726 }
Eric Laurent09f1ed22019-04-24 17:45:17 -0700727 break;
Eric Laurent09f1ed22019-04-24 17:45:17 -0700728 }
729
730 for (auto wpCbk : callbacks) {
731 sp<AudioDeviceCallback> spCbk = wpCbk.second.promote();
732 if (spCbk != nullptr) {
733 callbacksToCall.push_back(spCbk);
734 }
Eric Laurentad2e7b92017-09-14 20:06:42 -0700735 }
Eric Laurent4463ff52019-02-07 13:56:09 -0800736 }
737
Andy Hungacd955a2024-02-16 17:14:36 -0800738 // Callbacks must be called without mMutex held. May lead to dead lock if calling for
739 // example getRoutedDevice that updates the device and tries to acquire mMutex.
Eric Laurent09f1ed22019-04-24 17:45:17 -0700740 for (auto cb : callbacksToCall) {
Mikhail Naganov88536df2021-07-26 17:30:29 -0700741 // If callbacksToCall is not empty, it implies ioDesc->getIoHandle() and deviceId are valid
742 cb->onAudioDeviceUpdate(ioDesc->getIoHandle(), deviceId);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700743 }
Ytai Ben-Tsvi10dc0a62020-09-18 11:31:55 -0700744
745 return Status::ok();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800746}
747
Eric Laurent076e7c72022-05-03 18:12:28 +0200748Status AudioSystem::AudioFlingerClient::onSupportedLatencyModesChanged(
Mikhail Naganovf53e1822022-12-18 02:48:14 +0000749 int output, const std::vector<media::audio::common::AudioLatencyMode>& latencyModes) {
Eric Laurent076e7c72022-05-03 18:12:28 +0200750 audio_io_handle_t outputLegacy = VALUE_OR_RETURN_BINDER_STATUS(
751 aidl2legacy_int32_t_audio_io_handle_t(output));
752 std::vector<audio_latency_mode_t> modesLegacy = VALUE_OR_RETURN_BINDER_STATUS(
753 convertContainer<std::vector<audio_latency_mode_t>>(
Mikhail Naganovf53e1822022-12-18 02:48:14 +0000754 latencyModes, aidl2legacy_AudioLatencyMode_audio_latency_mode_t));
Eric Laurent076e7c72022-05-03 18:12:28 +0200755
756 std::vector<sp<SupportedLatencyModesCallback>> callbacks;
757 {
Andy Hungacd955a2024-02-16 17:14:36 -0800758 std::lock_guard _l(mMutex);
Eric Laurent076e7c72022-05-03 18:12:28 +0200759 for (auto callback : mSupportedLatencyModesCallbacks) {
760 if (auto ref = callback.promote(); ref != nullptr) {
761 callbacks.push_back(ref);
762 }
763 }
764 }
765 for (const auto& callback : callbacks) {
766 callback->onSupportedLatencyModesChanged(outputLegacy, modesLegacy);
767 }
768
769 return Status::ok();
770}
771
Eric Laurent73e26b62015-04-27 16:55:58 -0700772status_t AudioSystem::AudioFlingerClient::getInputBufferSize(
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800773 uint32_t sampleRate, audio_format_t format,
774 audio_channel_mask_t channelMask, size_t* buffSize) {
Andy Hung264fa4c2024-02-21 15:52:12 -0800775 const sp<IAudioFlinger> af = get_audio_flinger();
Eric Laurent73e26b62015-04-27 16:55:58 -0700776 if (af == 0) {
777 return PERMISSION_DENIED;
778 }
Andy Hungacd955a2024-02-16 17:14:36 -0800779 std::lock_guard _l(mMutex);
Eric Laurent73e26b62015-04-27 16:55:58 -0700780 // Do we have a stale mInBuffSize or are we requesting the input buffer size for new values
781 if ((mInBuffSize == 0) || (sampleRate != mInSamplingRate) || (format != mInFormat)
782 || (channelMask != mInChannelMask)) {
783 size_t inBuffSize = af->getInputBufferSize(sampleRate, format, channelMask);
784 if (inBuffSize == 0) {
Glenn Kasten49f36ba2017-12-06 13:02:02 -0800785 ALOGE("AudioSystem::getInputBufferSize failed sampleRate %d format %#x channelMask %#x",
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800786 sampleRate, format, channelMask);
Eric Laurent73e26b62015-04-27 16:55:58 -0700787 return BAD_VALUE;
788 }
789 // A benign race is possible here: we could overwrite a fresher cache entry
790 // save the request params
791 mInSamplingRate = sampleRate;
792 mInFormat = format;
793 mInChannelMask = channelMask;
794
795 mInBuffSize = inBuffSize;
796 }
797
798 *buffSize = mInBuffSize;
799
800 return NO_ERROR;
801}
802
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800803sp<AudioIoDescriptor>
804AudioSystem::AudioFlingerClient::getIoDescriptor_l(audio_io_handle_t ioHandle) {
Andy Hungd211ce02024-02-16 15:34:07 -0800805 if (const auto it = mIoDescriptors.find(ioHandle);
806 it != mIoDescriptors.end()) {
807 return it->second;
Eric Laurent73e26b62015-04-27 16:55:58 -0700808 }
Andy Hungd211ce02024-02-16 15:34:07 -0800809 return {};
Eric Laurent73e26b62015-04-27 16:55:58 -0700810}
811
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800812sp<AudioIoDescriptor> AudioSystem::AudioFlingerClient::getIoDescriptor(audio_io_handle_t ioHandle) {
Andy Hungacd955a2024-02-16 17:14:36 -0800813 std::lock_guard _l(mMutex);
Praveen Chavan49fdeaf2015-09-29 02:25:47 -0700814 return getIoDescriptor_l(ioHandle);
815}
816
Eric Laurent296fb132015-05-01 11:38:42 -0700817status_t AudioSystem::AudioFlingerClient::addAudioDeviceCallback(
Eric Laurent09f1ed22019-04-24 17:45:17 -0700818 const wp<AudioDeviceCallback>& callback, audio_io_handle_t audioIo,
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800819 audio_port_handle_t portId) {
Eric Laurent09f1ed22019-04-24 17:45:17 -0700820 ALOGV("%s audioIo %d portId %d", __func__, audioIo, portId);
Andy Hungacd955a2024-02-16 17:14:36 -0800821 std::lock_guard _l(mMutex);
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800822 auto& callbacks = mAudioDeviceCallbacks.emplace(
823 audioIo,
824 std::map<audio_port_handle_t, wp<AudioDeviceCallback>>()).first->second;
Eric Laurent09f1ed22019-04-24 17:45:17 -0700825 auto result = callbacks.try_emplace(portId, callback);
826 if (!result.second) {
827 return INVALID_OPERATION;
Eric Laurent296fb132015-05-01 11:38:42 -0700828 }
Eric Laurent296fb132015-05-01 11:38:42 -0700829 return NO_ERROR;
830}
831
832status_t AudioSystem::AudioFlingerClient::removeAudioDeviceCallback(
Eric Laurent09f1ed22019-04-24 17:45:17 -0700833 const wp<AudioDeviceCallback>& callback __unused, audio_io_handle_t audioIo,
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800834 audio_port_handle_t portId) {
Eric Laurent09f1ed22019-04-24 17:45:17 -0700835 ALOGV("%s audioIo %d portId %d", __func__, audioIo, portId);
Andy Hungacd955a2024-02-16 17:14:36 -0800836 std::lock_guard _l(mMutex);
Eric Laurent09f1ed22019-04-24 17:45:17 -0700837 auto it = mAudioDeviceCallbacks.find(audioIo);
838 if (it == mAudioDeviceCallbacks.end()) {
Eric Laurent296fb132015-05-01 11:38:42 -0700839 return INVALID_OPERATION;
840 }
Eric Laurent09f1ed22019-04-24 17:45:17 -0700841 if (it->second.erase(portId) == 0) {
Eric Laurent296fb132015-05-01 11:38:42 -0700842 return INVALID_OPERATION;
843 }
Eric Laurent09f1ed22019-04-24 17:45:17 -0700844 if (it->second.size() == 0) {
845 mAudioDeviceCallbacks.erase(audioIo);
Eric Laurent296fb132015-05-01 11:38:42 -0700846 }
847 return NO_ERROR;
848}
849
Eric Laurent076e7c72022-05-03 18:12:28 +0200850status_t AudioSystem::AudioFlingerClient::addSupportedLatencyModesCallback(
851 const sp<SupportedLatencyModesCallback>& callback) {
Andy Hungacd955a2024-02-16 17:14:36 -0800852 std::lock_guard _l(mMutex);
Eric Laurent076e7c72022-05-03 18:12:28 +0200853 if (std::find(mSupportedLatencyModesCallbacks.begin(),
854 mSupportedLatencyModesCallbacks.end(),
855 callback) != mSupportedLatencyModesCallbacks.end()) {
856 return INVALID_OPERATION;
857 }
858 mSupportedLatencyModesCallbacks.push_back(callback);
859 return NO_ERROR;
860}
861
862status_t AudioSystem::AudioFlingerClient::removeSupportedLatencyModesCallback(
863 const sp<SupportedLatencyModesCallback>& callback) {
Andy Hungacd955a2024-02-16 17:14:36 -0800864 std::lock_guard _l(mMutex);
Eric Laurent076e7c72022-05-03 18:12:28 +0200865 auto it = std::find(mSupportedLatencyModesCallbacks.begin(),
866 mSupportedLatencyModesCallbacks.end(),
867 callback);
868 if (it == mSupportedLatencyModesCallbacks.end()) {
869 return INVALID_OPERATION;
870 }
871 mSupportedLatencyModesCallbacks.erase(it);
872 return NO_ERROR;
873}
874
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800875/* static */ uintptr_t AudioSystem::addErrorCallback(audio_error_callback cb) {
Andy Hungacd955a2024-02-16 17:14:36 -0800876 std::lock_guard _l(gErrorCallbacksMutex);
Ytai Ben-Tsvi000c3e42020-01-09 15:26:40 -0800877 gAudioErrorCallbacks.insert(cb);
878 return reinterpret_cast<uintptr_t>(cb);
879}
880
881/* static */ void AudioSystem::removeErrorCallback(uintptr_t cb) {
Andy Hungacd955a2024-02-16 17:14:36 -0800882 std::lock_guard _l(gErrorCallbacksMutex);
Ytai Ben-Tsvi000c3e42020-01-09 15:26:40 -0800883 gAudioErrorCallbacks.erase(reinterpret_cast<audio_error_callback>(cb));
884}
885
886/* static */ void AudioSystem::reportError(status_t err) {
Andy Hungacd955a2024-02-16 17:14:36 -0800887 std::lock_guard _l(gErrorCallbacksMutex);
Ytai Ben-Tsvi000c3e42020-01-09 15:26:40 -0800888 for (auto callback : gAudioErrorCallbacks) {
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800889 callback(err);
Ytai Ben-Tsvi000c3e42020-01-09 15:26:40 -0800890 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800891}
892
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800893/*static*/ void AudioSystem::setDynPolicyCallback(dynamic_policy_callback cb) {
Andy Hungacd955a2024-02-16 17:14:36 -0800894 std::lock_guard _l(gMutex);
Jean-Michel Trivif613d422015-04-23 18:41:29 -0700895 gDynPolicyCallback = cb;
896}
897
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800898/*static*/ void AudioSystem::setRecordConfigCallback(record_config_callback cb) {
Andy Hungacd955a2024-02-16 17:14:36 -0800899 std::lock_guard _l(gMutex);
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -0800900 gRecordConfigCallback = cb;
901}
902
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800903/*static*/ void AudioSystem::setRoutingCallback(routing_callback cb) {
Andy Hungacd955a2024-02-16 17:14:36 -0800904 std::lock_guard _l(gMutex);
Jean-Michel Trivi9a6b9ad2020-10-22 16:46:43 -0700905 gRoutingCallback = cb;
906}
907
Jean-Michel Trivi78f2b302022-04-15 18:18:41 +0000908/*static*/ void AudioSystem::setVolInitReqCallback(vol_range_init_req_callback cb) {
Andy Hungacd955a2024-02-16 17:14:36 -0800909 std::lock_guard _l(gMutex);
Jean-Michel Trivi78f2b302022-04-15 18:18:41 +0000910 gVolRangeInitReqCallback = cb;
911}
912
Andy Hung88e71732024-02-21 16:44:27 -0800913struct AudioPolicyTraits {
914 static void onServiceCreate(const sp<IAudioPolicyService>& ap,
915 const sp<AudioSystem::AudioPolicyServiceClient>& apc) {
916 const int64_t token = IPCThreadState::self()->clearCallingIdentity();
Eric Laurent0ebd5f92014-11-19 19:04:52 -0800917 ap->registerClient(apc);
François Gaffie24437602018-04-23 15:08:59 +0200918 ap->setAudioPortCallbacksEnabled(apc->isAudioPortCbEnabled());
François Gaffiecfe17322018-11-07 13:41:29 +0100919 ap->setAudioVolumeGroupCallbacksEnabled(apc->isAudioVolumeGroupCbEnabled());
François Gaffie24437602018-04-23 15:08:59 +0200920 IPCThreadState::self()->restoreCallingIdentity(token);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700921 }
Glenn Kastend2d089f2014-11-05 11:48:12 -0800922
Andy Hung88e71732024-02-21 16:44:27 -0800923 static void onClearService(const sp<AudioSystem::AudioPolicyServiceClient>&) {}
924
925 static constexpr const char *SERVICE_NAME = "media.audio_policy";
926};
927
928[[clang::no_destroy]] static constinit ServiceHandler<IAudioPolicyService,
929 AudioSystem::AudioPolicyServiceClient, IAudioPolicyService,
930 AudioPolicyTraits> gAudioPolicyServiceHandler;
931
932status_t AudioSystem::setLocalAudioPolicyService(const sp<IAudioPolicyService>& aps) {
933 return gAudioPolicyServiceHandler.setLocalService(aps);
934}
935
936sp<IAudioPolicyService> AudioSystem::get_audio_policy_service() {
937 return gAudioPolicyServiceHandler.getService();
Eric Laurentc2f1f072009-07-17 12:17:14 -0700938}
939
Mikhail Naganovec3d5792022-05-06 00:19:55 +0000940void AudioSystem::clearAudioPolicyService() {
Andy Hung88e71732024-02-21 16:44:27 -0800941 gAudioPolicyServiceHandler.clearService();
Mikhail Naganovec3d5792022-05-06 00:19:55 +0000942}
943
Shraddha Basantwanif2aa2502024-04-08 11:01:57 +0000944void AudioSystem::disableThreadPool() {
945 gAudioFlingerServiceHandler.disableThreadPool();
946 gAudioPolicyServiceHandler.disableThreadPool();
947}
948
Glenn Kastenfb1fdc92013-07-10 17:03:19 -0700949// ---------------------------------------------------------------------------
950
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800951void AudioSystem::onNewAudioModulesAvailable() {
Andy Hung264fa4c2024-02-21 15:52:12 -0800952 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Mikhail Naganov88b30d22020-03-09 19:43:13 +0000953 if (aps == 0) return;
954 aps->onNewAudioModulesAvailable();
955}
956
Nathalie Le Clair88fa2752021-11-23 13:03:41 +0100957status_t AudioSystem::setDeviceConnectionState(audio_policy_dev_state_t state,
958 const android::media::audio::common::AudioPort& port,
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800959 audio_format_t encodedFormat) {
Andy Hung264fa4c2024-02-21 15:52:12 -0800960 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Eric Laurent71b63e32011-09-02 14:20:56 -0700961
Eric Laurentc2f1f072009-07-17 12:17:14 -0700962 if (aps == 0) return PERMISSION_DENIED;
963
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800964 return statusTFromBinderStatus(
965 aps->setDeviceConnectionState(
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800966 VALUE_OR_RETURN_STATUS(
967 legacy2aidl_audio_policy_dev_state_t_AudioPolicyDeviceState(state)),
Nathalie Le Clair88fa2752021-11-23 13:03:41 +0100968 port,
Mikhail Naganovb60bd1b2021-07-15 17:31:43 -0700969 VALUE_OR_RETURN_STATUS(
970 legacy2aidl_audio_format_t_AudioFormatDescription(encodedFormat))));
Eric Laurentc2f1f072009-07-17 12:17:14 -0700971}
972
Dima Zavinfce7a472011-04-19 22:30:36 -0700973audio_policy_dev_state_t AudioSystem::getDeviceConnectionState(audio_devices_t device,
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800974 const char* device_address) {
Andy Hung264fa4c2024-02-21 15:52:12 -0800975 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Dima Zavinfce7a472011-04-19 22:30:36 -0700976 if (aps == 0) return AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE;
Eric Laurentc2f1f072009-07-17 12:17:14 -0700977
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800978 auto result = [&]() -> ConversionResult<audio_policy_dev_state_t> {
Mikhail Naganov932cb962021-09-16 01:05:27 +0000979 AudioDevice deviceAidl = VALUE_OR_RETURN(
980 legacy2aidl_audio_device_AudioDevice(device, device_address));
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800981
982 media::AudioPolicyDeviceState result;
983 RETURN_IF_ERROR(statusTFromBinderStatus(
984 aps->getDeviceConnectionState(deviceAidl, &result)));
985
986 return aidl2legacy_AudioPolicyDeviceState_audio_policy_dev_state_t(result);
987 }();
988 return result.value_or(AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700989}
990
Pavlin Radoslavovf862bc62016-12-26 18:57:22 -0800991status_t AudioSystem::handleDeviceConfigChange(audio_devices_t device,
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800992 const char* device_address,
993 const char* device_name,
994 audio_format_t encodedFormat) {
Andy Hung264fa4c2024-02-21 15:52:12 -0800995 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800996 const char* address = "";
997 const char* name = "";
Pavlin Radoslavovf862bc62016-12-26 18:57:22 -0800998
999 if (aps == 0) return PERMISSION_DENIED;
1000
1001 if (device_address != NULL) {
1002 address = device_address;
1003 }
1004 if (device_name != NULL) {
1005 name = device_name;
1006 }
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001007
Mikhail Naganov932cb962021-09-16 01:05:27 +00001008 AudioDevice deviceAidl = VALUE_OR_RETURN_STATUS(
1009 legacy2aidl_audio_device_AudioDevice(device, address));
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001010
1011 return statusTFromBinderStatus(
1012 aps->handleDeviceConfigChange(deviceAidl, name, VALUE_OR_RETURN_STATUS(
Mikhail Naganovb60bd1b2021-07-15 17:31:43 -07001013 legacy2aidl_audio_format_t_AudioFormatDescription(encodedFormat))));
Pavlin Radoslavovf862bc62016-12-26 18:57:22 -08001014}
1015
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001016status_t AudioSystem::setPhoneState(audio_mode_t state, uid_t uid) {
Glenn Kasten347966c2012-01-18 14:58:32 -08001017 if (uint32_t(state) >= AUDIO_MODE_CNT) return BAD_VALUE;
Andy Hung264fa4c2024-02-21 15:52:12 -08001018 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Eric Laurentc2f1f072009-07-17 12:17:14 -07001019 if (aps == 0) return PERMISSION_DENIED;
1020
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001021 return statusTFromBinderStatus(aps->setPhoneState(
1022 VALUE_OR_RETURN_STATUS(legacy2aidl_audio_mode_t_AudioMode(state)),
1023 VALUE_OR_RETURN_STATUS(legacy2aidl_uid_t_int32_t(uid))));
Eric Laurentc2f1f072009-07-17 12:17:14 -07001024}
1025
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001026status_t
1027AudioSystem::setForceUse(audio_policy_force_use_t usage, audio_policy_forced_cfg_t config) {
Andy Hung264fa4c2024-02-21 15:52:12 -08001028 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Eric Laurentc2f1f072009-07-17 12:17:14 -07001029 if (aps == 0) return PERMISSION_DENIED;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001030
1031 return statusTFromBinderStatus(
1032 aps->setForceUse(
1033 VALUE_OR_RETURN_STATUS(
1034 legacy2aidl_audio_policy_force_use_t_AudioPolicyForceUse(usage)),
1035 VALUE_OR_RETURN_STATUS(
1036 legacy2aidl_audio_policy_forced_cfg_t_AudioPolicyForcedConfig(
1037 config))));
Eric Laurentc2f1f072009-07-17 12:17:14 -07001038}
1039
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001040audio_policy_forced_cfg_t AudioSystem::getForceUse(audio_policy_force_use_t usage) {
Andy Hung264fa4c2024-02-21 15:52:12 -08001041 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Dima Zavinfce7a472011-04-19 22:30:36 -07001042 if (aps == 0) return AUDIO_POLICY_FORCE_NONE;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001043
1044 auto result = [&]() -> ConversionResult<audio_policy_forced_cfg_t> {
1045 media::AudioPolicyForceUse usageAidl = VALUE_OR_RETURN(
1046 legacy2aidl_audio_policy_force_use_t_AudioPolicyForceUse(usage));
1047 media::AudioPolicyForcedConfig configAidl;
1048 RETURN_IF_ERROR(statusTFromBinderStatus(
1049 aps->getForceUse(usageAidl, &configAidl)));
1050 return aidl2legacy_AudioPolicyForcedConfig_audio_policy_forced_cfg_t(configAidl);
1051 }();
1052
1053 return result.value_or(AUDIO_POLICY_FORCE_NONE);
Eric Laurentc2f1f072009-07-17 12:17:14 -07001054}
1055
1056
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001057audio_io_handle_t AudioSystem::getOutput(audio_stream_type_t stream) {
Andy Hung264fa4c2024-02-21 15:52:12 -08001058 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001059 if (aps == 0) return AUDIO_IO_HANDLE_NONE;
1060
1061 auto result = [&]() -> ConversionResult<audio_io_handle_t> {
Mikhail Naganovdbf03642021-08-25 18:15:32 -07001062 AudioStreamType streamAidl = VALUE_OR_RETURN(
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001063 legacy2aidl_audio_stream_type_t_AudioStreamType(stream));
1064 int32_t outputAidl;
1065 RETURN_IF_ERROR(
1066 statusTFromBinderStatus(aps->getOutput(streamAidl, &outputAidl)));
1067 return aidl2legacy_int32_t_audio_io_handle_t(outputAidl);
1068 }();
1069
1070 return result.value_or(AUDIO_IO_HANDLE_NONE);
Eric Laurentc2f1f072009-07-17 12:17:14 -07001071}
1072
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001073status_t AudioSystem::getOutputForAttr(audio_attributes_t* attr,
1074 audio_io_handle_t* output,
1075 audio_session_t session,
1076 audio_stream_type_t* stream,
Svet Ganov3e5f14f2021-05-13 22:51:08 +00001077 const AttributionSourceState& attributionSource,
jiabinf1c73972022-04-14 16:28:52 -07001078 audio_config_t* config,
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001079 audio_output_flags_t flags,
1080 audio_port_handle_t* selectedDeviceId,
1081 audio_port_handle_t* portId,
Eric Laurentb0a7bc92022-04-05 15:06:08 +02001082 std::vector<audio_io_handle_t>* secondaryOutputs,
jiabinc658e452022-10-21 20:52:21 +00001083 bool *isSpatialized,
Pechetty Sravani (xWF)2e077f02024-08-27 01:46:20 +00001084 bool *isBitPerfect) {
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001085 if (attr == nullptr) {
1086 ALOGE("%s NULL audio attributes", __func__);
1087 return BAD_VALUE;
1088 }
1089 if (output == nullptr) {
1090 ALOGE("%s NULL output - shouldn't happen", __func__);
1091 return BAD_VALUE;
1092 }
1093 if (selectedDeviceId == nullptr) {
1094 ALOGE("%s NULL selectedDeviceId - shouldn't happen", __func__);
1095 return BAD_VALUE;
1096 }
1097 if (portId == nullptr) {
1098 ALOGE("%s NULL portId - shouldn't happen", __func__);
1099 return BAD_VALUE;
1100 }
1101 if (secondaryOutputs == nullptr) {
1102 ALOGE("%s NULL secondaryOutputs - shouldn't happen", __func__);
1103 return BAD_VALUE;
1104 }
1105
Andy Hung264fa4c2024-02-21 15:52:12 -08001106 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Eric Laurente83b55d2014-11-14 10:06:21 -08001107 if (aps == 0) return NO_INIT;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001108
Mikhail Naganov1c400902023-05-17 11:48:43 -07001109 media::audio::common::AudioAttributes attrAidl = VALUE_OR_RETURN_STATUS(
1110 legacy2aidl_audio_attributes_t_AudioAttributes(*attr));
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001111 int32_t sessionAidl = VALUE_OR_RETURN_STATUS(legacy2aidl_audio_session_t_int32_t(session));
Mikhail Naganovdbf03642021-08-25 18:15:32 -07001112 AudioConfig configAidl = VALUE_OR_RETURN_STATUS(
Mikhail Naganovde3fa182021-07-30 15:06:42 -07001113 legacy2aidl_audio_config_t_AudioConfig(*config, false /*isInput*/));
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001114 int32_t flagsAidl = VALUE_OR_RETURN_STATUS(
1115 legacy2aidl_audio_output_flags_t_int32_t_mask(flags));
Eric Laurentf99edd32021-02-01 15:57:33 +01001116 int32_t selectedDeviceIdAidl = VALUE_OR_RETURN_STATUS(
1117 legacy2aidl_audio_port_handle_t_int32_t(*selectedDeviceId));
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001118
1119 media::GetOutputForAttrResponse responseAidl;
1120
jiabinf1c73972022-04-14 16:28:52 -07001121 status_t status = statusTFromBinderStatus(
Svet Ganov3e5f14f2021-05-13 22:51:08 +00001122 aps->getOutputForAttr(attrAidl, sessionAidl, attributionSource, configAidl, flagsAidl,
jiabinf1c73972022-04-14 16:28:52 -07001123 selectedDeviceIdAidl, &responseAidl));
1124 if (status != NO_ERROR) {
1125 config->format = VALUE_OR_RETURN_STATUS(
1126 aidl2legacy_AudioFormatDescription_audio_format_t(responseAidl.configBase.format));
1127 config->channel_mask = VALUE_OR_RETURN_STATUS(
1128 aidl2legacy_AudioChannelLayout_audio_channel_mask_t(
1129 responseAidl.configBase.channelMask, false /*isInput*/));
1130 config->sample_rate = responseAidl.configBase.sampleRate;
1131 return status;
1132 }
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001133
1134 *output = VALUE_OR_RETURN_STATUS(
1135 aidl2legacy_int32_t_audio_io_handle_t(responseAidl.output));
1136
1137 if (stream != nullptr) {
1138 *stream = VALUE_OR_RETURN_STATUS(
1139 aidl2legacy_AudioStreamType_audio_stream_type_t(responseAidl.stream));
1140 }
1141 *selectedDeviceId = VALUE_OR_RETURN_STATUS(
1142 aidl2legacy_int32_t_audio_port_handle_t(responseAidl.selectedDeviceId));
1143 *portId = VALUE_OR_RETURN_STATUS(aidl2legacy_int32_t_audio_port_handle_t(responseAidl.portId));
1144 *secondaryOutputs = VALUE_OR_RETURN_STATUS(convertContainer<std::vector<audio_io_handle_t>>(
1145 responseAidl.secondaryOutputs, aidl2legacy_int32_t_audio_io_handle_t));
Eric Laurentb0a7bc92022-04-05 15:06:08 +02001146 *isSpatialized = responseAidl.isSpatialized;
jiabinc658e452022-10-21 20:52:21 +00001147 *isBitPerfect = responseAidl.isBitPerfect;
Eric Laurent0d13fea2022-11-04 17:12:08 +01001148 *attr = VALUE_OR_RETURN_STATUS(
Mikhail Naganov1c400902023-05-17 11:48:43 -07001149 aidl2legacy_AudioAttributes_audio_attributes_t(responseAidl.attr));
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001150
1151 return OK;
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -07001152}
1153
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001154status_t AudioSystem::startOutput(audio_port_handle_t portId) {
Andy Hung264fa4c2024-02-21 15:52:12 -08001155 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Eric Laurentc2f1f072009-07-17 12:17:14 -07001156 if (aps == 0) return PERMISSION_DENIED;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001157
1158 int32_t portIdAidl = VALUE_OR_RETURN_STATUS(legacy2aidl_audio_port_handle_t_int32_t(portId));
1159 return statusTFromBinderStatus(aps->startOutput(portIdAidl));
Eric Laurentc2f1f072009-07-17 12:17:14 -07001160}
1161
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001162status_t AudioSystem::stopOutput(audio_port_handle_t portId) {
Andy Hung264fa4c2024-02-21 15:52:12 -08001163 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Eric Laurentc2f1f072009-07-17 12:17:14 -07001164 if (aps == 0) return PERMISSION_DENIED;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001165
1166 int32_t portIdAidl = VALUE_OR_RETURN_STATUS(legacy2aidl_audio_port_handle_t_int32_t(portId));
1167 return statusTFromBinderStatus(aps->stopOutput(portIdAidl));
Eric Laurentc2f1f072009-07-17 12:17:14 -07001168}
1169
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001170void AudioSystem::releaseOutput(audio_port_handle_t portId) {
Andy Hung264fa4c2024-02-21 15:52:12 -08001171 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Eric Laurentc2f1f072009-07-17 12:17:14 -07001172 if (aps == 0) return;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001173
1174 auto status = [&]() -> status_t {
1175 int32_t portIdAidl = VALUE_OR_RETURN_STATUS(
1176 legacy2aidl_audio_port_handle_t_int32_t(portId));
1177 RETURN_STATUS_IF_ERROR(statusTFromBinderStatus(aps->releaseOutput(portIdAidl)));
1178 return OK;
1179 }();
1180
1181 // Ignore status.
1182 (void) status;
Eric Laurentc2f1f072009-07-17 12:17:14 -07001183}
1184
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001185status_t AudioSystem::getInputForAttr(const audio_attributes_t* attr,
1186 audio_io_handle_t* input,
1187 audio_unique_id_t riid,
1188 audio_session_t session,
Svet Ganov3e5f14f2021-05-13 22:51:08 +00001189 const AttributionSourceState &attributionSource,
jiabinf1c73972022-04-14 16:28:52 -07001190 audio_config_base_t* config,
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001191 audio_input_flags_t flags,
1192 audio_port_handle_t* selectedDeviceId,
1193 audio_port_handle_t* portId) {
1194 if (attr == NULL) {
1195 ALOGE("getInputForAttr NULL attr - shouldn't happen");
1196 return BAD_VALUE;
1197 }
1198 if (input == NULL) {
1199 ALOGE("getInputForAttr NULL input - shouldn't happen");
1200 return BAD_VALUE;
1201 }
1202 if (selectedDeviceId == NULL) {
1203 ALOGE("getInputForAttr NULL selectedDeviceId - shouldn't happen");
1204 return BAD_VALUE;
1205 }
1206 if (portId == NULL) {
1207 ALOGE("getInputForAttr NULL portId - shouldn't happen");
1208 return BAD_VALUE;
1209 }
1210
Andy Hung264fa4c2024-02-21 15:52:12 -08001211 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Eric Laurentcaf7f482014-11-25 17:50:47 -08001212 if (aps == 0) return NO_INIT;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001213
Mikhail Naganov1c400902023-05-17 11:48:43 -07001214 media::audio::common::AudioAttributes attrAidl = VALUE_OR_RETURN_STATUS(
1215 legacy2aidl_audio_attributes_t_AudioAttributes(*attr));
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001216 int32_t inputAidl = VALUE_OR_RETURN_STATUS(legacy2aidl_audio_io_handle_t_int32_t(*input));
1217 int32_t riidAidl = VALUE_OR_RETURN_STATUS(legacy2aidl_audio_unique_id_t_int32_t(riid));
1218 int32_t sessionAidl = VALUE_OR_RETURN_STATUS(legacy2aidl_audio_session_t_int32_t(session));
Mikhail Naganovdbf03642021-08-25 18:15:32 -07001219 AudioConfigBase configAidl = VALUE_OR_RETURN_STATUS(
Mikhail Naganovde3fa182021-07-30 15:06:42 -07001220 legacy2aidl_audio_config_base_t_AudioConfigBase(*config, true /*isInput*/));
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001221 int32_t flagsAidl = VALUE_OR_RETURN_STATUS(legacy2aidl_audio_input_flags_t_int32_t_mask(flags));
Eric Laurentf99edd32021-02-01 15:57:33 +01001222 int32_t selectedDeviceIdAidl = VALUE_OR_RETURN_STATUS(
1223 legacy2aidl_audio_port_handle_t_int32_t(*selectedDeviceId));
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001224
1225 media::GetInputForAttrResponse response;
1226
jiabinf1c73972022-04-14 16:28:52 -07001227 status_t status = statusTFromBinderStatus(
Svet Ganov3e5f14f2021-05-13 22:51:08 +00001228 aps->getInputForAttr(attrAidl, inputAidl, riidAidl, sessionAidl, attributionSource,
jiabinf1c73972022-04-14 16:28:52 -07001229 configAidl, flagsAidl, selectedDeviceIdAidl, &response));
1230 if (status != NO_ERROR) {
1231 *config = VALUE_OR_RETURN_STATUS(
1232 aidl2legacy_AudioConfigBase_audio_config_base_t(response.config, true /*isInput*/));
1233 return status;
1234 }
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001235
1236 *input = VALUE_OR_RETURN_STATUS(aidl2legacy_int32_t_audio_io_handle_t(response.input));
1237 *selectedDeviceId = VALUE_OR_RETURN_STATUS(
1238 aidl2legacy_int32_t_audio_port_handle_t(response.selectedDeviceId));
1239 *portId = VALUE_OR_RETURN_STATUS(aidl2legacy_int32_t_audio_port_handle_t(response.portId));
1240
1241 return OK;
Eric Laurentc2f1f072009-07-17 12:17:14 -07001242}
1243
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001244status_t AudioSystem::startInput(audio_port_handle_t portId) {
Andy Hung264fa4c2024-02-21 15:52:12 -08001245 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Eric Laurentc2f1f072009-07-17 12:17:14 -07001246 if (aps == 0) return PERMISSION_DENIED;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001247
1248 int32_t portIdAidl = VALUE_OR_RETURN_STATUS(legacy2aidl_audio_port_handle_t_int32_t(portId));
1249 return statusTFromBinderStatus(aps->startInput(portIdAidl));
Eric Laurentc2f1f072009-07-17 12:17:14 -07001250}
1251
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001252status_t AudioSystem::stopInput(audio_port_handle_t portId) {
Andy Hung264fa4c2024-02-21 15:52:12 -08001253 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Eric Laurentc2f1f072009-07-17 12:17:14 -07001254 if (aps == 0) return PERMISSION_DENIED;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001255
1256 int32_t portIdAidl = VALUE_OR_RETURN_STATUS(legacy2aidl_audio_port_handle_t_int32_t(portId));
1257 return statusTFromBinderStatus(aps->stopInput(portIdAidl));
Eric Laurentc2f1f072009-07-17 12:17:14 -07001258}
1259
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001260void AudioSystem::releaseInput(audio_port_handle_t portId) {
Andy Hung264fa4c2024-02-21 15:52:12 -08001261 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Eric Laurentc2f1f072009-07-17 12:17:14 -07001262 if (aps == 0) return;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001263
1264 auto status = [&]() -> status_t {
1265 int32_t portIdAidl = VALUE_OR_RETURN_STATUS(
1266 legacy2aidl_audio_port_handle_t_int32_t(portId));
1267 RETURN_STATUS_IF_ERROR(statusTFromBinderStatus(aps->releaseInput(portIdAidl)));
1268 return OK;
1269 }();
1270
1271 // Ignore status.
1272 (void) status;
Eric Laurentc2f1f072009-07-17 12:17:14 -07001273}
1274
Vlad Popa87e0e582024-05-20 18:49:20 -07001275status_t AudioSystem::setDeviceAbsoluteVolumeEnabled(audio_devices_t deviceType,
1276 const char *address,
1277 bool enabled,
1278 audio_stream_type_t streamToDriveAbs) {
1279 const sp<IAudioPolicyService> aps = get_audio_policy_service();
1280 if (aps == nullptr) return PERMISSION_DENIED;
1281
1282 AudioDevice deviceAidl = VALUE_OR_RETURN_STATUS(
1283 legacy2aidl_audio_device_AudioDevice(deviceType, address));
1284 AudioStreamType streamToDriveAbsAidl = VALUE_OR_RETURN_STATUS(
1285 legacy2aidl_audio_stream_type_t_AudioStreamType(streamToDriveAbs));
1286 return statusTFromBinderStatus(
1287 aps->setDeviceAbsoluteVolumeEnabled(deviceAidl, enabled, streamToDriveAbsAidl));
1288}
1289
Dima Zavinfce7a472011-04-19 22:30:36 -07001290status_t AudioSystem::initStreamVolume(audio_stream_type_t stream,
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001291 int indexMin,
1292 int indexMax) {
Andy Hung264fa4c2024-02-21 15:52:12 -08001293 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Eric Laurentc2f1f072009-07-17 12:17:14 -07001294 if (aps == 0) return PERMISSION_DENIED;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001295
Mikhail Naganovdbf03642021-08-25 18:15:32 -07001296 AudioStreamType streamAidl = VALUE_OR_RETURN_STATUS(
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001297 legacy2aidl_audio_stream_type_t_AudioStreamType(stream));
1298 int32_t indexMinAidl = VALUE_OR_RETURN_STATUS(convertIntegral<int32_t>(indexMin));
1299 int32_t indexMaxAidl = VALUE_OR_RETURN_STATUS(convertIntegral<int32_t>(indexMax));
Mikhail Naganovec3d5792022-05-06 00:19:55 +00001300 status_t status = statusTFromBinderStatus(
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001301 aps->initStreamVolume(streamAidl, indexMinAidl, indexMaxAidl));
Mikhail Naganovec3d5792022-05-06 00:19:55 +00001302 if (status == DEAD_OBJECT) {
1303 // This is a critical operation since w/o proper stream volumes no audio
1304 // will be heard. Make sure we recover from a failure in any case.
1305 ALOGE("Received DEAD_OBJECT from APS, clearing the client");
1306 clearAudioPolicyService();
1307 }
1308 return status;
Eric Laurentc2f1f072009-07-17 12:17:14 -07001309}
1310
Eric Laurent83844cc2011-11-18 16:43:31 -08001311status_t AudioSystem::setStreamVolumeIndex(audio_stream_type_t stream,
1312 int index,
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001313 audio_devices_t device) {
Andy Hung264fa4c2024-02-21 15:52:12 -08001314 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Eric Laurentc2f1f072009-07-17 12:17:14 -07001315 if (aps == 0) return PERMISSION_DENIED;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001316
Mikhail Naganovdbf03642021-08-25 18:15:32 -07001317 AudioStreamType streamAidl = VALUE_OR_RETURN_STATUS(
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001318 legacy2aidl_audio_stream_type_t_AudioStreamType(stream));
1319 int32_t indexAidl = VALUE_OR_RETURN_STATUS(convertIntegral<int32_t>(index));
Mikhail Naganovf4a75362021-09-16 00:02:54 +00001320 AudioDeviceDescription deviceAidl = VALUE_OR_RETURN_STATUS(
Mikhail Naganov21a32ec2021-07-08 14:40:12 -07001321 legacy2aidl_audio_devices_t_AudioDeviceDescription(device));
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001322 return statusTFromBinderStatus(
1323 aps->setStreamVolumeIndex(streamAidl, deviceAidl, indexAidl));
Eric Laurentc2f1f072009-07-17 12:17:14 -07001324}
1325
Eric Laurent83844cc2011-11-18 16:43:31 -08001326status_t AudioSystem::getStreamVolumeIndex(audio_stream_type_t stream,
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001327 int* index,
1328 audio_devices_t device) {
Andy Hung264fa4c2024-02-21 15:52:12 -08001329 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Eric Laurentc2f1f072009-07-17 12:17:14 -07001330 if (aps == 0) return PERMISSION_DENIED;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001331
Mikhail Naganovdbf03642021-08-25 18:15:32 -07001332 AudioStreamType streamAidl = VALUE_OR_RETURN_STATUS(
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001333 legacy2aidl_audio_stream_type_t_AudioStreamType(stream));
Mikhail Naganovf4a75362021-09-16 00:02:54 +00001334 AudioDeviceDescription deviceAidl = VALUE_OR_RETURN_STATUS(
Mikhail Naganov21a32ec2021-07-08 14:40:12 -07001335 legacy2aidl_audio_devices_t_AudioDeviceDescription(device));
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001336 int32_t indexAidl;
1337 RETURN_STATUS_IF_ERROR(statusTFromBinderStatus(
1338 aps->getStreamVolumeIndex(streamAidl, deviceAidl, &indexAidl)));
1339 if (index != nullptr) {
1340 *index = VALUE_OR_RETURN_STATUS(convertIntegral<int>(indexAidl));
1341 }
1342 return OK;
Eric Laurentc2f1f072009-07-17 12:17:14 -07001343}
1344
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001345status_t AudioSystem::setVolumeIndexForAttributes(const audio_attributes_t& attr,
François Gaffiecfe17322018-11-07 13:41:29 +01001346 int index,
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001347 audio_devices_t device) {
Andy Hung264fa4c2024-02-21 15:52:12 -08001348 const sp<IAudioPolicyService> aps = get_audio_policy_service();
François Gaffiecfe17322018-11-07 13:41:29 +01001349 if (aps == 0) return PERMISSION_DENIED;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001350
Mikhail Naganov1c400902023-05-17 11:48:43 -07001351 media::audio::common::AudioAttributes attrAidl = VALUE_OR_RETURN_STATUS(
1352 legacy2aidl_audio_attributes_t_AudioAttributes(attr));
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001353 int32_t indexAidl = VALUE_OR_RETURN_STATUS(convertIntegral<int32_t>(index));
Mikhail Naganovf4a75362021-09-16 00:02:54 +00001354 AudioDeviceDescription deviceAidl = VALUE_OR_RETURN_STATUS(
Mikhail Naganov21a32ec2021-07-08 14:40:12 -07001355 legacy2aidl_audio_devices_t_AudioDeviceDescription(device));
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001356 return statusTFromBinderStatus(
1357 aps->setVolumeIndexForAttributes(attrAidl, deviceAidl, indexAidl));
François Gaffiecfe17322018-11-07 13:41:29 +01001358}
1359
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001360status_t AudioSystem::getVolumeIndexForAttributes(const audio_attributes_t& attr,
1361 int& index,
1362 audio_devices_t device) {
Andy Hung264fa4c2024-02-21 15:52:12 -08001363 const sp<IAudioPolicyService> aps = get_audio_policy_service();
François Gaffiecfe17322018-11-07 13:41:29 +01001364 if (aps == 0) return PERMISSION_DENIED;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001365
Mikhail Naganov1c400902023-05-17 11:48:43 -07001366 media::audio::common::AudioAttributes attrAidl = VALUE_OR_RETURN_STATUS(
1367 legacy2aidl_audio_attributes_t_AudioAttributes(attr));
Mikhail Naganovf4a75362021-09-16 00:02:54 +00001368 AudioDeviceDescription deviceAidl = VALUE_OR_RETURN_STATUS(
Mikhail Naganov21a32ec2021-07-08 14:40:12 -07001369 legacy2aidl_audio_devices_t_AudioDeviceDescription(device));
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001370 int32_t indexAidl;
1371 RETURN_STATUS_IF_ERROR(statusTFromBinderStatus(
1372 aps->getVolumeIndexForAttributes(attrAidl, deviceAidl, &indexAidl)));
1373 index = VALUE_OR_RETURN_STATUS(convertIntegral<int>(indexAidl));
1374 return OK;
François Gaffiecfe17322018-11-07 13:41:29 +01001375}
1376
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001377status_t AudioSystem::getMaxVolumeIndexForAttributes(const audio_attributes_t& attr, int& index) {
Andy Hung264fa4c2024-02-21 15:52:12 -08001378 const sp<IAudioPolicyService> aps = get_audio_policy_service();
François Gaffiecfe17322018-11-07 13:41:29 +01001379 if (aps == 0) return PERMISSION_DENIED;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001380
Mikhail Naganov1c400902023-05-17 11:48:43 -07001381 media::audio::common::AudioAttributes attrAidl = VALUE_OR_RETURN_STATUS(
1382 legacy2aidl_audio_attributes_t_AudioAttributes(attr));
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001383 int32_t indexAidl;
1384 RETURN_STATUS_IF_ERROR(statusTFromBinderStatus(
1385 aps->getMaxVolumeIndexForAttributes(attrAidl, &indexAidl)));
1386 index = VALUE_OR_RETURN_STATUS(convertIntegral<int>(indexAidl));
1387 return OK;
François Gaffiecfe17322018-11-07 13:41:29 +01001388}
1389
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001390status_t AudioSystem::getMinVolumeIndexForAttributes(const audio_attributes_t& attr, int& index) {
Andy Hung264fa4c2024-02-21 15:52:12 -08001391 const sp<IAudioPolicyService> aps = get_audio_policy_service();
François Gaffiecfe17322018-11-07 13:41:29 +01001392 if (aps == 0) return PERMISSION_DENIED;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001393
Mikhail Naganov1c400902023-05-17 11:48:43 -07001394 media::audio::common::AudioAttributes attrAidl = VALUE_OR_RETURN_STATUS(
1395 legacy2aidl_audio_attributes_t_AudioAttributes(attr));
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001396 int32_t indexAidl;
1397 RETURN_STATUS_IF_ERROR(statusTFromBinderStatus(
1398 aps->getMinVolumeIndexForAttributes(attrAidl, &indexAidl)));
1399 index = VALUE_OR_RETURN_STATUS(convertIntegral<int>(indexAidl));
1400 return OK;
François Gaffiecfe17322018-11-07 13:41:29 +01001401}
1402
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001403product_strategy_t AudioSystem::getStrategyForStream(audio_stream_type_t stream) {
Andy Hung264fa4c2024-02-21 15:52:12 -08001404 const sp<IAudioPolicyService> aps = get_audio_policy_service();
François Gaffied0ba9ed2018-11-05 11:50:42 +01001405 if (aps == 0) return PRODUCT_STRATEGY_NONE;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001406
1407 auto result = [&]() -> ConversionResult<product_strategy_t> {
Mikhail Naganovdbf03642021-08-25 18:15:32 -07001408 AudioStreamType streamAidl = VALUE_OR_RETURN(
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001409 legacy2aidl_audio_stream_type_t_AudioStreamType(stream));
1410 int32_t resultAidl;
1411 RETURN_IF_ERROR(statusTFromBinderStatus(
1412 aps->getStrategyForStream(streamAidl, &resultAidl)));
1413 return aidl2legacy_int32_t_product_strategy_t(resultAidl);
1414 }();
1415 return result.value_or(PRODUCT_STRATEGY_NONE);
Eric Laurentde070132010-07-13 04:45:46 -07001416}
1417
François Gaffie1e2b56f2022-04-01 14:34:29 +02001418status_t AudioSystem::getDevicesForAttributes(const audio_attributes_t& aa,
Andy Hung6d23c0f2022-02-16 09:37:15 -08001419 AudioDeviceTypeAddrVector* devices,
1420 bool forVolume) {
Jean-Michel Trivif41599b2020-01-07 14:22:08 -08001421 if (devices == nullptr) {
1422 return BAD_VALUE;
1423 }
Andy Hung264fa4c2024-02-21 15:52:12 -08001424 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Jean-Michel Trivif41599b2020-01-07 14:22:08 -08001425 if (aps == 0) return PERMISSION_DENIED;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001426
Mikhail Naganov1c400902023-05-17 11:48:43 -07001427 media::audio::common::AudioAttributes aaAidl = VALUE_OR_RETURN_STATUS(
1428 legacy2aidl_audio_attributes_t_AudioAttributes(aa));
Mikhail Naganovf4a75362021-09-16 00:02:54 +00001429 std::vector<AudioDevice> retAidl;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001430 RETURN_STATUS_IF_ERROR(
Andy Hung6d23c0f2022-02-16 09:37:15 -08001431 statusTFromBinderStatus(aps->getDevicesForAttributes(aaAidl, forVolume, &retAidl)));
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001432 *devices = VALUE_OR_RETURN_STATUS(
1433 convertContainer<AudioDeviceTypeAddrVector>(
1434 retAidl,
1435 aidl2legacy_AudioDeviceTypeAddress));
1436 return OK;
Jean-Michel Trivif41599b2020-01-07 14:22:08 -08001437}
1438
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001439audio_io_handle_t AudioSystem::getOutputForEffect(const effect_descriptor_t* desc) {
Andy Hung264fa4c2024-02-21 15:52:12 -08001440 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Glenn Kastenefa6ea92014-01-08 09:10:43 -08001441 // FIXME change return type to status_t, and return PERMISSION_DENIED here
Glenn Kasten142f5192014-03-25 17:44:59 -07001442 if (aps == 0) return AUDIO_IO_HANDLE_NONE;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001443
1444 auto result = [&]() -> ConversionResult<audio_io_handle_t> {
1445 media::EffectDescriptor descAidl = VALUE_OR_RETURN(
1446 legacy2aidl_effect_descriptor_t_EffectDescriptor(*desc));
1447 int32_t retAidl;
1448 RETURN_IF_ERROR(
1449 statusTFromBinderStatus(aps->getOutputForEffect(descAidl, &retAidl)));
1450 return aidl2legacy_int32_t_audio_io_handle_t(retAidl);
1451 }();
1452
1453 return result.value_or(AUDIO_IO_HANDLE_NONE);
Eric Laurentde070132010-07-13 04:45:46 -07001454}
1455
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001456status_t AudioSystem::registerEffect(const effect_descriptor_t* desc,
1457 audio_io_handle_t io,
1458 product_strategy_t strategy,
1459 audio_session_t session,
1460 int id) {
Andy Hung264fa4c2024-02-21 15:52:12 -08001461 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Eric Laurentde070132010-07-13 04:45:46 -07001462 if (aps == 0) return PERMISSION_DENIED;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001463
1464 media::EffectDescriptor descAidl = VALUE_OR_RETURN_STATUS(
1465 legacy2aidl_effect_descriptor_t_EffectDescriptor(*desc));
1466 int32_t ioAidl = VALUE_OR_RETURN_STATUS(legacy2aidl_audio_io_handle_t_int32_t(io));
1467 int32_t strategyAidl = VALUE_OR_RETURN_STATUS(aidl2legacy_int32_t_product_strategy_t(strategy));
1468 int32_t sessionAidl = VALUE_OR_RETURN_STATUS(legacy2aidl_audio_session_t_int32_t(session));
1469 int32_t idAidl = VALUE_OR_RETURN_STATUS(convertReinterpret<int32_t>(id));
1470 return statusTFromBinderStatus(
1471 aps->registerEffect(descAidl, ioAidl, strategyAidl, sessionAidl, idAidl));
Eric Laurentde070132010-07-13 04:45:46 -07001472}
1473
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001474status_t AudioSystem::unregisterEffect(int id) {
Andy Hung264fa4c2024-02-21 15:52:12 -08001475 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Eric Laurentde070132010-07-13 04:45:46 -07001476 if (aps == 0) return PERMISSION_DENIED;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001477
1478 int32_t idAidl = VALUE_OR_RETURN_STATUS(convertReinterpret<int32_t>(id));
1479 return statusTFromBinderStatus(
1480 aps->unregisterEffect(idAidl));
Eric Laurentde070132010-07-13 04:45:46 -07001481}
1482
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001483status_t AudioSystem::setEffectEnabled(int id, bool enabled) {
Andy Hung264fa4c2024-02-21 15:52:12 -08001484 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Eric Laurentdb7c0792011-08-10 10:37:50 -07001485 if (aps == 0) return PERMISSION_DENIED;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001486
1487 int32_t idAidl = VALUE_OR_RETURN_STATUS(convertReinterpret<int32_t>(id));
1488 return statusTFromBinderStatus(
1489 aps->setEffectEnabled(idAidl, enabled));
Eric Laurentdb7c0792011-08-10 10:37:50 -07001490}
1491
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001492status_t AudioSystem::moveEffectsToIo(const std::vector<int>& ids, audio_io_handle_t io) {
Andy Hung264fa4c2024-02-21 15:52:12 -08001493 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Eric Laurent6c796322019-04-09 14:13:17 -07001494 if (aps == 0) return PERMISSION_DENIED;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001495
1496 std::vector<int32_t> idsAidl = VALUE_OR_RETURN_STATUS(
1497 convertContainer<std::vector<int32_t>>(ids, convertReinterpret<int32_t, int>));
1498 int32_t ioAidl = VALUE_OR_RETURN_STATUS(legacy2aidl_audio_io_handle_t_int32_t(io));
1499 return statusTFromBinderStatus(aps->moveEffectsToIo(idsAidl, ioAidl));
Eric Laurent6c796322019-04-09 14:13:17 -07001500}
1501
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001502status_t AudioSystem::isStreamActive(audio_stream_type_t stream, bool* state, uint32_t inPastMs) {
Andy Hung264fa4c2024-02-21 15:52:12 -08001503 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Eric Laurenteda6c362011-02-02 09:33:30 -08001504 if (aps == 0) return PERMISSION_DENIED;
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001505 if (state == NULL) return BAD_VALUE;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001506
Mikhail Naganovdbf03642021-08-25 18:15:32 -07001507 AudioStreamType streamAidl = VALUE_OR_RETURN_STATUS(
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001508 legacy2aidl_audio_stream_type_t_AudioStreamType(stream));
1509 int32_t inPastMsAidl = VALUE_OR_RETURN_STATUS(convertIntegral<int32_t>(inPastMs));
1510 RETURN_STATUS_IF_ERROR(statusTFromBinderStatus(
1511 aps->isStreamActive(streamAidl, inPastMsAidl, state)));
1512 return OK;
Eric Laurenteda6c362011-02-02 09:33:30 -08001513}
1514
Jean-Michel Trivi272ab542013-02-04 16:26:02 -08001515status_t AudioSystem::isStreamActiveRemotely(audio_stream_type_t stream, bool* state,
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001516 uint32_t inPastMs) {
Andy Hung264fa4c2024-02-21 15:52:12 -08001517 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Jean-Michel Trivi272ab542013-02-04 16:26:02 -08001518 if (aps == 0) return PERMISSION_DENIED;
1519 if (state == NULL) return BAD_VALUE;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001520
Mikhail Naganovdbf03642021-08-25 18:15:32 -07001521 AudioStreamType streamAidl = VALUE_OR_RETURN_STATUS(
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001522 legacy2aidl_audio_stream_type_t_AudioStreamType(stream));
1523 int32_t inPastMsAidl = VALUE_OR_RETURN_STATUS(convertIntegral<int32_t>(inPastMs));
1524 RETURN_STATUS_IF_ERROR(statusTFromBinderStatus(
1525 aps->isStreamActiveRemotely(streamAidl, inPastMsAidl, state)));
1526 return OK;
Jean-Michel Trivi272ab542013-02-04 16:26:02 -08001527}
1528
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001529status_t AudioSystem::isSourceActive(audio_source_t stream, bool* state) {
Andy Hung264fa4c2024-02-21 15:52:12 -08001530 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Jean-Michel Trivid7086032012-10-10 12:11:16 -07001531 if (aps == 0) return PERMISSION_DENIED;
1532 if (state == NULL) return BAD_VALUE;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001533
Mikhail Naganovddceecc2021-09-03 13:58:56 -07001534 AudioSource streamAidl = VALUE_OR_RETURN_STATUS(
1535 legacy2aidl_audio_source_t_AudioSource(stream));
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001536 RETURN_STATUS_IF_ERROR(statusTFromBinderStatus(
1537 aps->isSourceActive(streamAidl, state)));
1538 return OK;
Jean-Michel Trivid7086032012-10-10 12:11:16 -07001539}
1540
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001541uint32_t AudioSystem::getPrimaryOutputSamplingRate() {
Andy Hung264fa4c2024-02-21 15:52:12 -08001542 const sp<IAudioFlinger> af = get_audio_flinger();
Glenn Kastencc0f1cf2012-09-24 11:27:18 -07001543 if (af == 0) return 0;
1544 return af->getPrimaryOutputSamplingRate();
1545}
1546
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001547size_t AudioSystem::getPrimaryOutputFrameCount() {
Andy Hung264fa4c2024-02-21 15:52:12 -08001548 const sp<IAudioFlinger> af = get_audio_flinger();
Glenn Kastencc0f1cf2012-09-24 11:27:18 -07001549 if (af == 0) return 0;
1550 return af->getPrimaryOutputFrameCount();
1551}
Eric Laurenteda6c362011-02-02 09:33:30 -08001552
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001553status_t AudioSystem::setLowRamDevice(bool isLowRamDevice, int64_t totalMemory) {
Andy Hung264fa4c2024-02-21 15:52:12 -08001554 const sp<IAudioFlinger> af = get_audio_flinger();
Glenn Kasten4182c4e2013-07-15 14:45:07 -07001555 if (af == 0) return PERMISSION_DENIED;
Andy Hung6f248bb2018-01-23 14:04:37 -08001556 return af->setLowRamDevice(isLowRamDevice, totalMemory);
Glenn Kasten4182c4e2013-07-15 14:45:07 -07001557}
1558
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001559void AudioSystem::clearAudioConfigCache() {
Glenn Kastend2d089f2014-11-05 11:48:12 -08001560 // called by restoreTrack_l(), which needs new IAudioFlinger and IAudioPolicyService instances
Steve Block3856b092011-10-20 11:56:00 +01001561 ALOGV("clearAudioConfigCache()");
Andy Hungda1fb072024-02-20 19:08:08 -08001562 gAudioFlingerServiceHandler.clearService();
Mikhail Naganovec3d5792022-05-06 00:19:55 +00001563 clearAudioPolicyService();
Eric Laurent9f6530f2011-08-30 10:18:54 -07001564}
1565
Hayden Gomes524159d2019-12-23 14:41:47 -08001566status_t AudioSystem::setSupportedSystemUsages(const std::vector<audio_usage_t>& systemUsages) {
Andy Hung264fa4c2024-02-21 15:52:12 -08001567 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Hayden Gomes524159d2019-12-23 14:41:47 -08001568 if (aps == nullptr) return PERMISSION_DENIED;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001569
Mikhail Naganovdbf03642021-08-25 18:15:32 -07001570 std::vector<AudioUsage> systemUsagesAidl = VALUE_OR_RETURN_STATUS(
1571 convertContainer<std::vector<AudioUsage>>(systemUsages,
1572 legacy2aidl_audio_usage_t_AudioUsage));
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001573 return statusTFromBinderStatus(aps->setSupportedSystemUsages(systemUsagesAidl));
Hayden Gomes524159d2019-12-23 14:41:47 -08001574}
1575
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001576status_t AudioSystem::setAllowedCapturePolicy(uid_t uid, audio_flags_mask_t capturePolicy) {
Andy Hung264fa4c2024-02-21 15:52:12 -08001577 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Kevin Rocardb99cc752019-03-21 20:52:24 -07001578 if (aps == nullptr) return PERMISSION_DENIED;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001579
1580 int32_t uidAidl = VALUE_OR_RETURN_STATUS(legacy2aidl_uid_t_int32_t(uid));
1581 int32_t capturePolicyAidl = VALUE_OR_RETURN_STATUS(
1582 legacy2aidl_audio_flags_mask_t_int32_t_mask(capturePolicy));
1583 return statusTFromBinderStatus(aps->setAllowedCapturePolicy(uidAidl, capturePolicyAidl));
Kevin Rocardb99cc752019-03-21 20:52:24 -07001584}
1585
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001586audio_offload_mode_t AudioSystem::getOffloadSupport(const audio_offload_info_t& info) {
Eric Laurent90fe31c2020-11-26 20:06:35 +01001587 ALOGV("%s", __func__);
Andy Hung264fa4c2024-02-21 15:52:12 -08001588 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Eric Laurent90fe31c2020-11-26 20:06:35 +01001589 if (aps == 0) return AUDIO_OFFLOAD_NOT_SUPPORTED;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001590
1591 auto result = [&]() -> ConversionResult<audio_offload_mode_t> {
Mikhail Naganovdbf03642021-08-25 18:15:32 -07001592 AudioOffloadInfo infoAidl = VALUE_OR_RETURN(
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001593 legacy2aidl_audio_offload_info_t_AudioOffloadInfo(info));
1594 media::AudioOffloadMode retAidl;
1595 RETURN_IF_ERROR(
1596 statusTFromBinderStatus(aps->getOffloadSupport(infoAidl, &retAidl)));
1597 return aidl2legacy_AudioOffloadMode_audio_offload_mode_t(retAidl);
1598 }();
1599
1600 return result.value_or(static_cast<audio_offload_mode_t>(0));
Richard Fitzgeraldad3af332013-03-25 16:54:37 +00001601}
1602
Eric Laurent203b1a12014-04-01 10:34:16 -07001603status_t AudioSystem::listAudioPorts(audio_port_role_t role,
1604 audio_port_type_t type,
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001605 unsigned int* num_ports,
1606 struct audio_port_v7* ports,
1607 unsigned int* generation) {
1608 if (num_ports == nullptr || (*num_ports != 0 && ports == nullptr) ||
1609 generation == nullptr) {
1610 return BAD_VALUE;
1611 }
1612
Andy Hung264fa4c2024-02-21 15:52:12 -08001613 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Eric Laurent203b1a12014-04-01 10:34:16 -07001614 if (aps == 0) return PERMISSION_DENIED;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001615
1616 media::AudioPortRole roleAidl = VALUE_OR_RETURN_STATUS(
1617 legacy2aidl_audio_port_role_t_AudioPortRole(role));
1618 media::AudioPortType typeAidl = VALUE_OR_RETURN_STATUS(
1619 legacy2aidl_audio_port_type_t_AudioPortType(type));
Mikhail Naganov0078ee52021-09-30 23:06:20 +00001620 Int numPortsAidl;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001621 numPortsAidl.value = VALUE_OR_RETURN_STATUS(convertIntegral<int32_t>(*num_ports));
Atneya Nair638a6e42022-12-18 16:45:15 -08001622 std::vector<media::AudioPortFw> portsAidl;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001623 int32_t generationAidl;
1624
1625 RETURN_STATUS_IF_ERROR(statusTFromBinderStatus(
1626 aps->listAudioPorts(roleAidl, typeAidl, &numPortsAidl, &portsAidl, &generationAidl)));
1627 *num_ports = VALUE_OR_RETURN_STATUS(convertIntegral<unsigned int>(numPortsAidl.value));
1628 *generation = VALUE_OR_RETURN_STATUS(convertIntegral<unsigned int>(generationAidl));
1629 RETURN_STATUS_IF_ERROR(convertRange(portsAidl.begin(), portsAidl.end(), ports,
Mikhail Naganov87227252023-01-13 17:38:10 -08001630 aidl2legacy_AudioPortFw_audio_port_v7));
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001631 return OK;
Eric Laurent203b1a12014-04-01 10:34:16 -07001632}
1633
Mikhail Naganov5edc5ed2023-03-23 14:52:15 -07001634status_t AudioSystem::listDeclaredDevicePorts(media::AudioPortRole role,
1635 std::vector<media::AudioPortFw>* result) {
1636 if (result == nullptr) return BAD_VALUE;
Andy Hung264fa4c2024-02-21 15:52:12 -08001637 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Mikhail Naganov5edc5ed2023-03-23 14:52:15 -07001638 if (aps == 0) return PERMISSION_DENIED;
1639 RETURN_STATUS_IF_ERROR(statusTFromBinderStatus(aps->listDeclaredDevicePorts(role, result)));
1640 return OK;
1641}
1642
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001643status_t AudioSystem::getAudioPort(struct audio_port_v7* port) {
1644 if (port == nullptr) {
1645 return BAD_VALUE;
1646 }
Andy Hung264fa4c2024-02-21 15:52:12 -08001647 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Eric Laurent203b1a12014-04-01 10:34:16 -07001648 if (aps == 0) return PERMISSION_DENIED;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001649
Atneya Nair638a6e42022-12-18 16:45:15 -08001650 media::AudioPortFw portAidl;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001651 RETURN_STATUS_IF_ERROR(
Mikhail Naganov17031562022-02-23 23:00:27 +00001652 statusTFromBinderStatus(aps->getAudioPort(port->id, &portAidl)));
Mikhail Naganov87227252023-01-13 17:38:10 -08001653 *port = VALUE_OR_RETURN_STATUS(aidl2legacy_AudioPortFw_audio_port_v7(portAidl));
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001654 return OK;
Eric Laurent203b1a12014-04-01 10:34:16 -07001655}
1656
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001657status_t AudioSystem::createAudioPatch(const struct audio_patch* patch,
1658 audio_patch_handle_t* handle) {
1659 if (patch == nullptr || handle == nullptr) {
1660 return BAD_VALUE;
1661 }
1662
Andy Hung264fa4c2024-02-21 15:52:12 -08001663 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Eric Laurent203b1a12014-04-01 10:34:16 -07001664 if (aps == 0) return PERMISSION_DENIED;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001665
Atneya Nair3afdbd12022-12-18 16:14:18 -08001666 media::AudioPatchFw patchAidl = VALUE_OR_RETURN_STATUS(
Mikhail Naganov87227252023-01-13 17:38:10 -08001667 legacy2aidl_audio_patch_AudioPatchFw(*patch));
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001668 int32_t handleAidl = VALUE_OR_RETURN_STATUS(legacy2aidl_audio_patch_handle_t_int32_t(*handle));
1669 RETURN_STATUS_IF_ERROR(
1670 statusTFromBinderStatus(aps->createAudioPatch(patchAidl, handleAidl, &handleAidl)));
1671 *handle = VALUE_OR_RETURN_STATUS(aidl2legacy_int32_t_audio_patch_handle_t(handleAidl));
1672 return OK;
Eric Laurent203b1a12014-04-01 10:34:16 -07001673}
1674
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001675status_t AudioSystem::releaseAudioPatch(audio_patch_handle_t handle) {
Andy Hung264fa4c2024-02-21 15:52:12 -08001676 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Eric Laurent203b1a12014-04-01 10:34:16 -07001677 if (aps == 0) return PERMISSION_DENIED;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001678
1679 int32_t handleAidl = VALUE_OR_RETURN_STATUS(legacy2aidl_audio_patch_handle_t_int32_t(handle));
1680 return statusTFromBinderStatus(aps->releaseAudioPatch(handleAidl));
Eric Laurent203b1a12014-04-01 10:34:16 -07001681}
1682
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001683status_t AudioSystem::listAudioPatches(unsigned int* num_patches,
1684 struct audio_patch* patches,
1685 unsigned int* generation) {
1686 if (num_patches == nullptr || (*num_patches != 0 && patches == nullptr) ||
1687 generation == nullptr) {
1688 return BAD_VALUE;
1689 }
1690
Andy Hung264fa4c2024-02-21 15:52:12 -08001691 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Eric Laurent203b1a12014-04-01 10:34:16 -07001692 if (aps == 0) return PERMISSION_DENIED;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001693
1694
Mikhail Naganov0078ee52021-09-30 23:06:20 +00001695 Int numPatchesAidl;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001696 numPatchesAidl.value = VALUE_OR_RETURN_STATUS(convertIntegral<int32_t>(*num_patches));
Atneya Nair3afdbd12022-12-18 16:14:18 -08001697 std::vector<media::AudioPatchFw> patchesAidl;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001698 int32_t generationAidl;
1699
1700 RETURN_STATUS_IF_ERROR(statusTFromBinderStatus(
1701 aps->listAudioPatches(&numPatchesAidl, &patchesAidl, &generationAidl)));
1702 *num_patches = VALUE_OR_RETURN_STATUS(convertIntegral<unsigned int>(numPatchesAidl.value));
1703 *generation = VALUE_OR_RETURN_STATUS(convertIntegral<unsigned int>(generationAidl));
1704 RETURN_STATUS_IF_ERROR(convertRange(patchesAidl.begin(), patchesAidl.end(), patches,
Mikhail Naganov87227252023-01-13 17:38:10 -08001705 aidl2legacy_AudioPatchFw_audio_patch));
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001706 return OK;
Eric Laurent203b1a12014-04-01 10:34:16 -07001707}
1708
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001709status_t AudioSystem::setAudioPortConfig(const struct audio_port_config* config) {
1710 if (config == nullptr) {
1711 return BAD_VALUE;
1712 }
1713
Andy Hung264fa4c2024-02-21 15:52:12 -08001714 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Eric Laurent203b1a12014-04-01 10:34:16 -07001715 if (aps == 0) return PERMISSION_DENIED;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001716
Atneya Nair7a9594f2022-12-18 17:26:26 -08001717 media::AudioPortConfigFw configAidl = VALUE_OR_RETURN_STATUS(
Mikhail Naganov87227252023-01-13 17:38:10 -08001718 legacy2aidl_audio_port_config_AudioPortConfigFw(*config));
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001719 return statusTFromBinderStatus(aps->setAudioPortConfig(configAidl));
Eric Laurent203b1a12014-04-01 10:34:16 -07001720}
1721
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001722status_t AudioSystem::addAudioPortCallback(const sp<AudioPortCallback>& callback) {
Andy Hung264fa4c2024-02-21 15:52:12 -08001723 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Eric Laurentb28753e2015-04-01 13:06:28 -07001724 if (aps == 0) return PERMISSION_DENIED;
Andy Hung88e71732024-02-21 16:44:27 -08001725 const auto apc = gAudioPolicyServiceHandler.getClient();
1726 if (apc == nullptr) return NO_INIT;
Eric Laurentb28753e2015-04-01 13:06:28 -07001727
Andy Hung88e71732024-02-21 16:44:27 -08001728 std::lock_guard _l(gApsCallbackMutex);
1729 const int ret = apc->addAudioPortCallback(callback);
Eric Laurente8726fe2015-06-26 09:39:24 -07001730 if (ret == 1) {
1731 aps->setAudioPortCallbacksEnabled(true);
1732 }
1733 return (ret < 0) ? INVALID_OPERATION : NO_ERROR;
Eric Laurentb52c1522014-05-20 11:27:36 -07001734}
1735
Jean-Michel Trivif613d422015-04-23 18:41:29 -07001736/*static*/
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001737status_t AudioSystem::removeAudioPortCallback(const sp<AudioPortCallback>& callback) {
Andy Hung264fa4c2024-02-21 15:52:12 -08001738 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Eric Laurentb28753e2015-04-01 13:06:28 -07001739 if (aps == 0) return PERMISSION_DENIED;
Andy Hung88e71732024-02-21 16:44:27 -08001740 const auto apc = gAudioPolicyServiceHandler.getClient();
1741 if (apc == nullptr) return NO_INIT;
Eric Laurentb28753e2015-04-01 13:06:28 -07001742
Andy Hung88e71732024-02-21 16:44:27 -08001743 std::lock_guard _l(gApsCallbackMutex);
1744 const int ret = apc->removeAudioPortCallback(callback);
Eric Laurente8726fe2015-06-26 09:39:24 -07001745 if (ret == 0) {
1746 aps->setAudioPortCallbacksEnabled(false);
1747 }
1748 return (ret < 0) ? INVALID_OPERATION : NO_ERROR;
Eric Laurent296fb132015-05-01 11:38:42 -07001749}
1750
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001751status_t AudioSystem::addAudioVolumeGroupCallback(const sp<AudioVolumeGroupCallback>& callback) {
Andy Hung264fa4c2024-02-21 15:52:12 -08001752 const sp<IAudioPolicyService> aps = get_audio_policy_service();
François Gaffiecfe17322018-11-07 13:41:29 +01001753 if (aps == 0) return PERMISSION_DENIED;
Andy Hung88e71732024-02-21 16:44:27 -08001754 const auto apc = gAudioPolicyServiceHandler.getClient();
1755 if (apc == nullptr) return NO_INIT;
François Gaffiecfe17322018-11-07 13:41:29 +01001756
Andy Hung88e71732024-02-21 16:44:27 -08001757 std::lock_guard _l(gApsCallbackMutex);
1758 const int ret = apc->addAudioVolumeGroupCallback(callback);
François Gaffiecfe17322018-11-07 13:41:29 +01001759 if (ret == 1) {
1760 aps->setAudioVolumeGroupCallbacksEnabled(true);
1761 }
1762 return (ret < 0) ? INVALID_OPERATION : NO_ERROR;
1763}
1764
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001765status_t AudioSystem::removeAudioVolumeGroupCallback(const sp<AudioVolumeGroupCallback>& callback) {
Andy Hung264fa4c2024-02-21 15:52:12 -08001766 const sp<IAudioPolicyService> aps = get_audio_policy_service();
François Gaffiecfe17322018-11-07 13:41:29 +01001767 if (aps == 0) return PERMISSION_DENIED;
Andy Hung88e71732024-02-21 16:44:27 -08001768 const auto apc = gAudioPolicyServiceHandler.getClient();
1769 if (apc == nullptr) return NO_INIT;
François Gaffiecfe17322018-11-07 13:41:29 +01001770
Andy Hung88e71732024-02-21 16:44:27 -08001771 std::lock_guard _l(gApsCallbackMutex);
1772 const int ret = apc->removeAudioVolumeGroupCallback(callback);
François Gaffiecfe17322018-11-07 13:41:29 +01001773 if (ret == 0) {
1774 aps->setAudioVolumeGroupCallbacksEnabled(false);
1775 }
1776 return (ret < 0) ? INVALID_OPERATION : NO_ERROR;
1777}
1778
Eric Laurent296fb132015-05-01 11:38:42 -07001779status_t AudioSystem::addAudioDeviceCallback(
Eric Laurent09f1ed22019-04-24 17:45:17 -07001780 const wp<AudioDeviceCallback>& callback, audio_io_handle_t audioIo,
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001781 audio_port_handle_t portId) {
Eric Laurent296fb132015-05-01 11:38:42 -07001782 const sp<AudioFlingerClient> afc = getAudioFlingerClient();
1783 if (afc == 0) {
1784 return NO_INIT;
1785 }
Eric Laurent09f1ed22019-04-24 17:45:17 -07001786 status_t status = afc->addAudioDeviceCallback(callback, audioIo, portId);
Eric Laurent7c1ec5f2015-07-09 14:52:47 -07001787 if (status == NO_ERROR) {
Andy Hung264fa4c2024-02-21 15:52:12 -08001788 const sp<IAudioFlinger> af = get_audio_flinger();
Eric Laurent7c1ec5f2015-07-09 14:52:47 -07001789 if (af != 0) {
1790 af->registerClient(afc);
1791 }
1792 }
1793 return status;
Eric Laurent296fb132015-05-01 11:38:42 -07001794}
1795
1796status_t AudioSystem::removeAudioDeviceCallback(
Eric Laurent09f1ed22019-04-24 17:45:17 -07001797 const wp<AudioDeviceCallback>& callback, audio_io_handle_t audioIo,
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001798 audio_port_handle_t portId) {
Eric Laurent296fb132015-05-01 11:38:42 -07001799 const sp<AudioFlingerClient> afc = getAudioFlingerClient();
1800 if (afc == 0) {
1801 return NO_INIT;
1802 }
Eric Laurent09f1ed22019-04-24 17:45:17 -07001803 return afc->removeAudioDeviceCallback(callback, audioIo, portId);
Eric Laurent296fb132015-05-01 11:38:42 -07001804}
1805
Eric Laurent076e7c72022-05-03 18:12:28 +02001806status_t AudioSystem::addSupportedLatencyModesCallback(
1807 const sp<SupportedLatencyModesCallback>& callback) {
1808 const sp<AudioFlingerClient> afc = getAudioFlingerClient();
1809 if (afc == 0) {
1810 return NO_INIT;
1811 }
1812 return afc->addSupportedLatencyModesCallback(callback);
1813}
1814
1815status_t AudioSystem::removeSupportedLatencyModesCallback(
1816 const sp<SupportedLatencyModesCallback>& callback) {
1817 const sp<AudioFlingerClient> afc = getAudioFlingerClient();
1818 if (afc == 0) {
1819 return NO_INIT;
1820 }
1821 return afc->removeSupportedLatencyModesCallback(callback);
1822}
1823
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001824audio_port_handle_t AudioSystem::getDeviceIdForIo(audio_io_handle_t audioIo) {
Andy Hung264fa4c2024-02-21 15:52:12 -08001825 const sp<IAudioFlinger> af = get_audio_flinger();
Eric Laurent296fb132015-05-01 11:38:42 -07001826 if (af == 0) return PERMISSION_DENIED;
1827 const sp<AudioIoDescriptor> desc = getIoDescriptor(audioIo);
1828 if (desc == 0) {
1829 return AUDIO_PORT_HANDLE_NONE;
1830 }
1831 return desc->getDeviceId();
Eric Laurentb28753e2015-04-01 13:06:28 -07001832}
1833
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001834status_t AudioSystem::acquireSoundTriggerSession(audio_session_t* session,
1835 audio_io_handle_t* ioHandle,
1836 audio_devices_t* device) {
1837 if (session == nullptr || ioHandle == nullptr || device == nullptr) {
1838 return BAD_VALUE;
1839 }
Andy Hung264fa4c2024-02-21 15:52:12 -08001840 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Eric Laurentdf3dc7e2014-07-27 18:39:40 -07001841 if (aps == 0) return PERMISSION_DENIED;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001842
1843 media::SoundTriggerSession retAidl;
1844 RETURN_STATUS_IF_ERROR(
1845 statusTFromBinderStatus(aps->acquireSoundTriggerSession(&retAidl)));
1846 *session = VALUE_OR_RETURN_STATUS(aidl2legacy_int32_t_audio_session_t(retAidl.session));
1847 *ioHandle = VALUE_OR_RETURN_STATUS(aidl2legacy_int32_t_audio_io_handle_t(retAidl.ioHandle));
Mikhail Naganov21a32ec2021-07-08 14:40:12 -07001848 *device = VALUE_OR_RETURN_STATUS(
1849 aidl2legacy_AudioDeviceDescription_audio_devices_t(retAidl.device));
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001850 return OK;
Eric Laurentdf3dc7e2014-07-27 18:39:40 -07001851}
1852
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001853status_t AudioSystem::releaseSoundTriggerSession(audio_session_t session) {
Andy Hung264fa4c2024-02-21 15:52:12 -08001854 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Eric Laurentdf3dc7e2014-07-27 18:39:40 -07001855 if (aps == 0) return PERMISSION_DENIED;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001856
1857 int32_t sessionAidl = VALUE_OR_RETURN_STATUS(legacy2aidl_audio_session_t_int32_t(session));
1858 return statusTFromBinderStatus(aps->releaseSoundTriggerSession(sessionAidl));
Eric Laurentdf3dc7e2014-07-27 18:39:40 -07001859}
Eric Laurentbb6c9a02014-09-25 14:11:47 -07001860
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001861audio_mode_t AudioSystem::getPhoneState() {
Andy Hung264fa4c2024-02-21 15:52:12 -08001862 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Eric Laurentbb6c9a02014-09-25 14:11:47 -07001863 if (aps == 0) return AUDIO_MODE_INVALID;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001864
1865 auto result = [&]() -> ConversionResult<audio_mode_t> {
Mikhail Naganovddceecc2021-09-03 13:58:56 -07001866 media::audio::common::AudioMode retAidl;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001867 RETURN_IF_ERROR(statusTFromBinderStatus(aps->getPhoneState(&retAidl)));
1868 return aidl2legacy_AudioMode_audio_mode_t(retAidl);
1869 }();
1870
1871 return result.value_or(AUDIO_MODE_INVALID);
Eric Laurentbb6c9a02014-09-25 14:11:47 -07001872}
1873
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001874status_t AudioSystem::registerPolicyMixes(const Vector<AudioMix>& mixes, bool registration) {
Andy Hung264fa4c2024-02-21 15:52:12 -08001875 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Eric Laurentbaac1832014-12-01 17:52:59 -08001876 if (aps == 0) return PERMISSION_DENIED;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001877
1878 size_t mixesSize = std::min(mixes.size(), size_t{MAX_MIXES_PER_POLICY});
1879 std::vector<media::AudioMix> mixesAidl;
1880 RETURN_STATUS_IF_ERROR(
1881 convertRange(mixes.begin(), mixes.begin() + mixesSize, std::back_inserter(mixesAidl),
1882 legacy2aidl_AudioMix));
1883 return statusTFromBinderStatus(aps->registerPolicyMixes(mixesAidl, registration));
Eric Laurentbaac1832014-12-01 17:52:59 -08001884}
Eric Laurentbb6c9a02014-09-25 14:11:47 -07001885
Marvin Raminbdefaf02023-11-01 09:10:32 +01001886status_t AudioSystem::getRegisteredPolicyMixes(std::vector<AudioMix>& mixes) {
1887 if (!audio_flags::audio_mix_test_api()) {
1888 return INVALID_OPERATION;
1889 }
1890
1891 const sp<IAudioPolicyService> aps = AudioSystem::get_audio_policy_service();
1892 if (aps == nullptr) return PERMISSION_DENIED;
1893
1894 std::vector<::android::media::AudioMix> aidlMixes;
1895 Status status = aps->getRegisteredPolicyMixes(&aidlMixes);
1896
1897 for (const auto& aidlMix : aidlMixes) {
1898 AudioMix mix = VALUE_OR_RETURN_STATUS(aidl2legacy_AudioMix(aidlMix));
1899 mixes.push_back(mix);
1900 }
1901
1902 return statusTFromBinderStatus(status);
1903}
1904
Jan Sebechlebsky0af8e872023-08-11 14:45:08 +02001905status_t AudioSystem::updatePolicyMixes(
1906 const std::vector<std::pair<AudioMix, std::vector<AudioMixMatchCriterion>>>&
1907 mixesWithUpdates) {
Andy Hung264fa4c2024-02-21 15:52:12 -08001908 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Jan Sebechlebsky0af8e872023-08-11 14:45:08 +02001909 if (aps == 0) return PERMISSION_DENIED;
1910
1911 std::vector<media::AudioMixUpdate> updatesAidl;
1912 updatesAidl.reserve(mixesWithUpdates.size());
1913
1914 for (const auto& update : mixesWithUpdates) {
1915 media::AudioMixUpdate updateAidl;
1916 updateAidl.audioMix = VALUE_OR_RETURN_STATUS(legacy2aidl_AudioMix(update.first));
1917 RETURN_STATUS_IF_ERROR(convertRange(update.second.begin(), update.second.end(),
1918 std::back_inserter(updateAidl.newCriteria),
1919 legacy2aidl_AudioMixMatchCriterion));
1920 updatesAidl.emplace_back(updateAidl);
1921 }
1922
1923 return statusTFromBinderStatus(aps->updatePolicyMixes(updatesAidl));
1924}
1925
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001926status_t AudioSystem::setUidDeviceAffinities(uid_t uid, const AudioDeviceTypeAddrVector& devices) {
Andy Hung264fa4c2024-02-21 15:52:12 -08001927 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Jean-Michel Trivibda70da2018-12-19 07:30:15 -08001928 if (aps == 0) return PERMISSION_DENIED;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001929
1930 int32_t uidAidl = VALUE_OR_RETURN_STATUS(legacy2aidl_uid_t_int32_t(uid));
Mikhail Naganovf4a75362021-09-16 00:02:54 +00001931 std::vector<AudioDevice> devicesAidl = VALUE_OR_RETURN_STATUS(
1932 convertContainer<std::vector<AudioDevice>>(devices,
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001933 legacy2aidl_AudioDeviceTypeAddress));
1934 return statusTFromBinderStatus(aps->setUidDeviceAffinities(uidAidl, devicesAidl));
Jean-Michel Trivibda70da2018-12-19 07:30:15 -08001935}
1936
1937status_t AudioSystem::removeUidDeviceAffinities(uid_t uid) {
Andy Hung264fa4c2024-02-21 15:52:12 -08001938 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Jean-Michel Trivibda70da2018-12-19 07:30:15 -08001939 if (aps == 0) return PERMISSION_DENIED;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001940
1941 int32_t uidAidl = VALUE_OR_RETURN_STATUS(legacy2aidl_uid_t_int32_t(uid));
1942 return statusTFromBinderStatus(aps->removeUidDeviceAffinities(uidAidl));
Jean-Michel Trivibda70da2018-12-19 07:30:15 -08001943}
1944
Oscar Azucena90e77632019-11-27 17:12:28 -08001945status_t AudioSystem::setUserIdDeviceAffinities(int userId,
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001946 const AudioDeviceTypeAddrVector& devices) {
Andy Hung264fa4c2024-02-21 15:52:12 -08001947 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Oscar Azucena90e77632019-11-27 17:12:28 -08001948 if (aps == 0) return PERMISSION_DENIED;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001949
1950 int32_t userIdAidl = VALUE_OR_RETURN_STATUS(convertReinterpret<int32_t>(userId));
Mikhail Naganovf4a75362021-09-16 00:02:54 +00001951 std::vector<AudioDevice> devicesAidl = VALUE_OR_RETURN_STATUS(
1952 convertContainer<std::vector<AudioDevice>>(devices,
1953 legacy2aidl_AudioDeviceTypeAddress));
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001954 return statusTFromBinderStatus(
1955 aps->setUserIdDeviceAffinities(userIdAidl, devicesAidl));
Oscar Azucena90e77632019-11-27 17:12:28 -08001956}
1957
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001958status_t AudioSystem::removeUserIdDeviceAffinities(int userId) {
Andy Hung264fa4c2024-02-21 15:52:12 -08001959 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Oscar Azucena90e77632019-11-27 17:12:28 -08001960 if (aps == 0) return PERMISSION_DENIED;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001961 int32_t userIdAidl = VALUE_OR_RETURN_STATUS(convertReinterpret<int32_t>(userId));
1962 return statusTFromBinderStatus(aps->removeUserIdDeviceAffinities(userIdAidl));
Oscar Azucena90e77632019-11-27 17:12:28 -08001963}
1964
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001965status_t AudioSystem::startAudioSource(const struct audio_port_config* source,
1966 const audio_attributes_t* attributes,
1967 audio_port_handle_t* portId) {
1968 if (source == nullptr || attributes == nullptr || portId == nullptr) {
1969 return BAD_VALUE;
1970 }
Andy Hung264fa4c2024-02-21 15:52:12 -08001971 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Eric Laurent554a2772015-04-10 11:29:24 -07001972 if (aps == 0) return PERMISSION_DENIED;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001973
Atneya Nair7a9594f2022-12-18 17:26:26 -08001974 media::AudioPortConfigFw sourceAidl = VALUE_OR_RETURN_STATUS(
Mikhail Naganov87227252023-01-13 17:38:10 -08001975 legacy2aidl_audio_port_config_AudioPortConfigFw(*source));
Mikhail Naganov1c400902023-05-17 11:48:43 -07001976 media::audio::common::AudioAttributes attributesAidl = VALUE_OR_RETURN_STATUS(
1977 legacy2aidl_audio_attributes_t_AudioAttributes(*attributes));
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001978 int32_t portIdAidl;
1979 RETURN_STATUS_IF_ERROR(statusTFromBinderStatus(
1980 aps->startAudioSource(sourceAidl, attributesAidl, &portIdAidl)));
1981 *portId = VALUE_OR_RETURN_STATUS(aidl2legacy_int32_t_audio_port_handle_t(portIdAidl));
1982 return OK;
Eric Laurent554a2772015-04-10 11:29:24 -07001983}
1984
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001985status_t AudioSystem::stopAudioSource(audio_port_handle_t portId) {
Andy Hung264fa4c2024-02-21 15:52:12 -08001986 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Eric Laurent554a2772015-04-10 11:29:24 -07001987 if (aps == 0) return PERMISSION_DENIED;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001988
1989 int32_t portIdAidl = VALUE_OR_RETURN_STATUS(legacy2aidl_audio_port_handle_t_int32_t(portId));
1990 return statusTFromBinderStatus(aps->stopAudioSource(portIdAidl));
Eric Laurent554a2772015-04-10 11:29:24 -07001991}
1992
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001993status_t AudioSystem::setMasterMono(bool mono) {
Andy Hung264fa4c2024-02-21 15:52:12 -08001994 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Andy Hung2ddee192015-12-18 17:34:44 -08001995 if (aps == 0) return PERMISSION_DENIED;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001996 return statusTFromBinderStatus(aps->setMasterMono(mono));
Andy Hung2ddee192015-12-18 17:34:44 -08001997}
1998
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001999status_t AudioSystem::getMasterMono(bool* mono) {
2000 if (mono == nullptr) {
2001 return BAD_VALUE;
2002 }
Andy Hung264fa4c2024-02-21 15:52:12 -08002003 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Andy Hung2ddee192015-12-18 17:34:44 -08002004 if (aps == 0) return PERMISSION_DENIED;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002005 return statusTFromBinderStatus(aps->getMasterMono(mono));
Andy Hung2ddee192015-12-18 17:34:44 -08002006}
2007
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002008status_t AudioSystem::setMasterBalance(float balance) {
Andy Hung264fa4c2024-02-21 15:52:12 -08002009 const sp<IAudioFlinger> af = get_audio_flinger();
Richard Folke Tullberg3fae0372017-01-13 09:04:25 +01002010 if (af == 0) return PERMISSION_DENIED;
2011 return af->setMasterBalance(balance);
2012}
2013
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002014status_t AudioSystem::getMasterBalance(float* balance) {
Andy Hung264fa4c2024-02-21 15:52:12 -08002015 const sp<IAudioFlinger> af = get_audio_flinger();
Richard Folke Tullberg3fae0372017-01-13 09:04:25 +01002016 if (af == 0) return PERMISSION_DENIED;
2017 return af->getMasterBalance(balance);
2018}
2019
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002020float
2021AudioSystem::getStreamVolumeDB(audio_stream_type_t stream, int index, audio_devices_t device) {
Andy Hung264fa4c2024-02-21 15:52:12 -08002022 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Eric Laurentac9cef52017-06-09 15:46:26 -07002023 if (aps == 0) return NAN;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002024
2025 auto result = [&]() -> ConversionResult<float> {
Mikhail Naganovdbf03642021-08-25 18:15:32 -07002026 AudioStreamType streamAidl = VALUE_OR_RETURN(
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002027 legacy2aidl_audio_stream_type_t_AudioStreamType(stream));
2028 int32_t indexAidl = VALUE_OR_RETURN(convertIntegral<int32_t>(index));
Mikhail Naganovf4a75362021-09-16 00:02:54 +00002029 AudioDeviceDescription deviceAidl = VALUE_OR_RETURN(
Mikhail Naganov21a32ec2021-07-08 14:40:12 -07002030 legacy2aidl_audio_devices_t_AudioDeviceDescription(device));
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002031 float retAidl;
2032 RETURN_IF_ERROR(statusTFromBinderStatus(
2033 aps->getStreamVolumeDB(streamAidl, indexAidl, deviceAidl, &retAidl)));
2034 return retAidl;
2035 }();
2036 return result.value_or(NAN);
Eric Laurentac9cef52017-06-09 15:46:26 -07002037}
2038
Mikhail Naganovd5d9de72023-02-13 11:45:03 -08002039status_t AudioSystem::getMicrophones(std::vector<media::MicrophoneInfoFw>* microphones) {
Andy Hung264fa4c2024-02-21 15:52:12 -08002040 const sp<IAudioFlinger> af = get_audio_flinger();
jiabin46a76fa2018-01-05 10:18:21 -08002041 if (af == 0) return PERMISSION_DENIED;
2042 return af->getMicrophones(microphones);
2043}
2044
Eric Laurent42896a02019-09-27 15:40:33 -07002045status_t AudioSystem::setAudioHalPids(const std::vector<pid_t>& pids) {
Andy Hung264fa4c2024-02-21 15:52:12 -08002046 const sp<IAudioFlinger> af = get_audio_flinger();
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002047 if (af == nullptr) return PERMISSION_DENIED;
2048 return af->setAudioHalPids(pids);
Eric Laurent42896a02019-09-27 15:40:33 -07002049}
2050
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002051status_t AudioSystem::getSurroundFormats(unsigned int* numSurroundFormats,
2052 audio_format_t* surroundFormats,
Kriti Dang6537def2021-03-02 13:46:59 +01002053 bool* surroundFormatsEnabled) {
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002054 if (numSurroundFormats == nullptr || (*numSurroundFormats != 0 &&
2055 (surroundFormats == nullptr ||
2056 surroundFormatsEnabled == nullptr))) {
2057 return BAD_VALUE;
2058 }
2059
Andy Hung264fa4c2024-02-21 15:52:12 -08002060 const sp<IAudioPolicyService> aps = get_audio_policy_service();
jiabin81772902018-04-02 17:52:27 -07002061 if (aps == 0) return PERMISSION_DENIED;
Mikhail Naganov0078ee52021-09-30 23:06:20 +00002062 Int numSurroundFormatsAidl;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002063 numSurroundFormatsAidl.value =
2064 VALUE_OR_RETURN_STATUS(convertIntegral<int32_t>(*numSurroundFormats));
Mikhail Naganov57bd06f2021-08-10 16:41:54 -07002065 std::vector<AudioFormatDescription> surroundFormatsAidl;
Kriti Dang877b27e2021-02-02 12:10:40 +01002066 std::vector<bool> surroundFormatsEnabledAidl;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002067 RETURN_STATUS_IF_ERROR(statusTFromBinderStatus(
Kriti Dang6537def2021-03-02 13:46:59 +01002068 aps->getSurroundFormats(&numSurroundFormatsAidl, &surroundFormatsAidl,
2069 &surroundFormatsEnabledAidl)));
Kriti Dang877b27e2021-02-02 12:10:40 +01002070
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002071 *numSurroundFormats = VALUE_OR_RETURN_STATUS(
2072 convertIntegral<unsigned int>(numSurroundFormatsAidl.value));
2073 RETURN_STATUS_IF_ERROR(
2074 convertRange(surroundFormatsAidl.begin(), surroundFormatsAidl.end(), surroundFormats,
Mikhail Naganovb60bd1b2021-07-15 17:31:43 -07002075 aidl2legacy_AudioFormatDescription_audio_format_t));
Kriti Dang877b27e2021-02-02 12:10:40 +01002076 std::copy(surroundFormatsEnabledAidl.begin(), surroundFormatsEnabledAidl.end(),
2077 surroundFormatsEnabled);
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002078 return OK;
jiabin81772902018-04-02 17:52:27 -07002079}
2080
Kriti Dang6537def2021-03-02 13:46:59 +01002081status_t AudioSystem::getReportedSurroundFormats(unsigned int* numSurroundFormats,
2082 audio_format_t* surroundFormats) {
2083 if (numSurroundFormats == nullptr || (*numSurroundFormats != 0 && surroundFormats == nullptr)) {
2084 return BAD_VALUE;
2085 }
2086
Andy Hung264fa4c2024-02-21 15:52:12 -08002087 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Kriti Dang6537def2021-03-02 13:46:59 +01002088 if (aps == 0) return PERMISSION_DENIED;
Mikhail Naganov0078ee52021-09-30 23:06:20 +00002089 Int numSurroundFormatsAidl;
Kriti Dang6537def2021-03-02 13:46:59 +01002090 numSurroundFormatsAidl.value =
2091 VALUE_OR_RETURN_STATUS(convertIntegral<int32_t>(*numSurroundFormats));
Mikhail Naganov57bd06f2021-08-10 16:41:54 -07002092 std::vector<AudioFormatDescription> surroundFormatsAidl;
Kriti Dang6537def2021-03-02 13:46:59 +01002093 RETURN_STATUS_IF_ERROR(statusTFromBinderStatus(
2094 aps->getReportedSurroundFormats(&numSurroundFormatsAidl, &surroundFormatsAidl)));
2095
2096 *numSurroundFormats = VALUE_OR_RETURN_STATUS(
2097 convertIntegral<unsigned int>(numSurroundFormatsAidl.value));
2098 RETURN_STATUS_IF_ERROR(
2099 convertRange(surroundFormatsAidl.begin(), surroundFormatsAidl.end(), surroundFormats,
Mikhail Naganovb60bd1b2021-07-15 17:31:43 -07002100 aidl2legacy_AudioFormatDescription_audio_format_t));
Kriti Dang6537def2021-03-02 13:46:59 +01002101 return OK;
2102}
2103
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002104status_t AudioSystem::setSurroundFormatEnabled(audio_format_t audioFormat, bool enabled) {
Andy Hung264fa4c2024-02-21 15:52:12 -08002105 const sp<IAudioPolicyService> aps = get_audio_policy_service();
jiabin81772902018-04-02 17:52:27 -07002106 if (aps == 0) return PERMISSION_DENIED;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002107
Mikhail Naganov57bd06f2021-08-10 16:41:54 -07002108 AudioFormatDescription audioFormatAidl = VALUE_OR_RETURN_STATUS(
Mikhail Naganovb60bd1b2021-07-15 17:31:43 -07002109 legacy2aidl_audio_format_t_AudioFormatDescription(audioFormat));
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002110 return statusTFromBinderStatus(
2111 aps->setSurroundFormatEnabled(audioFormatAidl, enabled));
jiabin81772902018-04-02 17:52:27 -07002112}
2113
Oscar Azucena829d90d2022-01-28 17:17:56 -08002114status_t AudioSystem::setAssistantServicesUids(const std::vector<uid_t>& uids) {
Andy Hung264fa4c2024-02-21 15:52:12 -08002115 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Eric Laurentb78763e2018-10-17 10:08:02 -07002116 if (aps == 0) return PERMISSION_DENIED;
2117
Oscar Azucena829d90d2022-01-28 17:17:56 -08002118 std::vector<int32_t> uidsAidl = VALUE_OR_RETURN_STATUS(
2119 convertContainer<std::vector<int32_t>>(uids, legacy2aidl_uid_t_int32_t));
2120 return statusTFromBinderStatus(aps->setAssistantServicesUids(uidsAidl));
Ahaan Ugalef51ce002021-08-04 16:34:20 -07002121}
2122
Oscar Azucenac2cdda32022-01-31 19:10:39 -08002123status_t AudioSystem::setActiveAssistantServicesUids(const std::vector<uid_t>& activeUids) {
Andy Hung264fa4c2024-02-21 15:52:12 -08002124 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Oscar Azucenac2cdda32022-01-31 19:10:39 -08002125 if (aps == 0) return PERMISSION_DENIED;
2126
2127 std::vector<int32_t> activeUidsAidl = VALUE_OR_RETURN_STATUS(
2128 convertContainer<std::vector<int32_t>>(activeUids, legacy2aidl_uid_t_int32_t));
2129 return statusTFromBinderStatus(aps->setActiveAssistantServicesUids(activeUidsAidl));
2130}
2131
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002132status_t AudioSystem::setA11yServicesUids(const std::vector<uid_t>& uids) {
Andy Hung264fa4c2024-02-21 15:52:12 -08002133 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Eric Laurentb78763e2018-10-17 10:08:02 -07002134 if (aps == 0) return PERMISSION_DENIED;
2135
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002136 std::vector<int32_t> uidsAidl = VALUE_OR_RETURN_STATUS(
2137 convertContainer<std::vector<int32_t>>(uids, legacy2aidl_uid_t_int32_t));
2138 return statusTFromBinderStatus(aps->setA11yServicesUids(uidsAidl));
Eric Laurentb78763e2018-10-17 10:08:02 -07002139}
2140
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002141status_t AudioSystem::setCurrentImeUid(uid_t uid) {
Andy Hung264fa4c2024-02-21 15:52:12 -08002142 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Kohsuke Yatoha623a132020-03-24 20:10:26 -07002143 if (aps == 0) return PERMISSION_DENIED;
2144
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002145 int32_t uidAidl = VALUE_OR_RETURN_STATUS(legacy2aidl_uid_t_int32_t(uid));
2146 return statusTFromBinderStatus(aps->setCurrentImeUid(uidAidl));
Kohsuke Yatoha623a132020-03-24 20:10:26 -07002147}
2148
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002149bool AudioSystem::isHapticPlaybackSupported() {
Andy Hung264fa4c2024-02-21 15:52:12 -08002150 const sp<IAudioPolicyService> aps = get_audio_policy_service();
jiabin6012f912018-11-02 17:06:30 -07002151 if (aps == 0) return false;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002152
2153 auto result = [&]() -> ConversionResult<bool> {
2154 bool retVal;
2155 RETURN_IF_ERROR(
2156 statusTFromBinderStatus(aps->isHapticPlaybackSupported(&retVal)));
2157 return retVal;
2158 }();
2159 return result.value_or(false);
jiabin6012f912018-11-02 17:06:30 -07002160}
2161
Carter Hsu325a8eb2022-01-19 19:56:51 +08002162bool AudioSystem::isUltrasoundSupported() {
Andy Hung264fa4c2024-02-21 15:52:12 -08002163 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Carter Hsu325a8eb2022-01-19 19:56:51 +08002164 if (aps == 0) return false;
2165
2166 auto result = [&]() -> ConversionResult<bool> {
2167 bool retVal;
2168 RETURN_IF_ERROR(
2169 statusTFromBinderStatus(aps->isUltrasoundSupported(&retVal)));
2170 return retVal;
2171 }();
2172 return result.value_or(false);
2173}
2174
Pattydd807582021-11-04 21:01:03 +08002175status_t AudioSystem::getHwOffloadFormatsSupportedForBluetoothMedia(
2176 audio_devices_t device, std::vector<audio_format_t>* formats) {
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002177 if (formats == nullptr) {
2178 return BAD_VALUE;
2179 }
2180
Andy Hung264fa4c2024-02-21 15:52:12 -08002181 const sp<IAudioPolicyService> aps = get_audio_policy_service();
François Gaffied0ba9ed2018-11-05 11:50:42 +01002182 if (aps == 0) return PERMISSION_DENIED;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002183
Mikhail Naganov57bd06f2021-08-10 16:41:54 -07002184 std::vector<AudioFormatDescription> formatsAidl;
Pattydd807582021-11-04 21:01:03 +08002185 AudioDeviceDescription deviceAidl = VALUE_OR_RETURN_STATUS(
2186 legacy2aidl_audio_devices_t_AudioDeviceDescription(device));
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002187 RETURN_STATUS_IF_ERROR(statusTFromBinderStatus(
Pattydd807582021-11-04 21:01:03 +08002188 aps->getHwOffloadFormatsSupportedForBluetoothMedia(deviceAidl, &formatsAidl)));
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002189 *formats = VALUE_OR_RETURN_STATUS(
Mikhail Naganovb60bd1b2021-07-15 17:31:43 -07002190 convertContainer<std::vector<audio_format_t>>(
2191 formatsAidl,
2192 aidl2legacy_AudioFormatDescription_audio_format_t));
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002193 return OK;
François Gaffied0ba9ed2018-11-05 11:50:42 +01002194}
2195
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002196status_t AudioSystem::listAudioProductStrategies(AudioProductStrategyVector& strategies) {
Andy Hung264fa4c2024-02-21 15:52:12 -08002197 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Arun Mirpuri11029ad2018-12-19 20:45:19 -08002198 if (aps == 0) return PERMISSION_DENIED;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002199
2200 std::vector<media::AudioProductStrategy> strategiesAidl;
2201 RETURN_STATUS_IF_ERROR(statusTFromBinderStatus(
2202 aps->listAudioProductStrategies(&strategiesAidl)));
2203 strategies = VALUE_OR_RETURN_STATUS(
2204 convertContainer<AudioProductStrategyVector>(strategiesAidl,
2205 aidl2legacy_AudioProductStrategy));
2206 return OK;
François Gaffied0ba9ed2018-11-05 11:50:42 +01002207}
2208
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002209audio_attributes_t AudioSystem::streamTypeToAttributes(audio_stream_type_t stream) {
François Gaffied0ba9ed2018-11-05 11:50:42 +01002210 AudioProductStrategyVector strategies;
2211 listAudioProductStrategies(strategies);
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002212 for (const auto& strategy : strategies) {
François Gaffie1e2b56f2022-04-01 14:34:29 +02002213 auto attrVect = strategy.getVolumeGroupAttributes();
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002214 auto iter = std::find_if(begin(attrVect), end(attrVect), [&stream](const auto& attributes) {
2215 return attributes.getStreamType() == stream;
2216 });
François Gaffied0ba9ed2018-11-05 11:50:42 +01002217 if (iter != end(attrVect)) {
2218 return iter->getAttributes();
2219 }
2220 }
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002221 ALOGE("invalid stream type %s when converting to attributes", toString(stream).c_str());
François Gaffied0ba9ed2018-11-05 11:50:42 +01002222 return AUDIO_ATTRIBUTES_INITIALIZER;
2223}
2224
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002225audio_stream_type_t AudioSystem::attributesToStreamType(const audio_attributes_t& attr) {
François Gaffie4b2018b2018-11-07 11:18:59 +01002226 product_strategy_t psId;
François Gaffie1e2b56f2022-04-01 14:34:29 +02002227 status_t ret = AudioSystem::getProductStrategyFromAudioAttributes(attr, psId);
François Gaffie4b2018b2018-11-07 11:18:59 +01002228 if (ret != NO_ERROR) {
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002229 ALOGE("no strategy found for attributes %s", toString(attr).c_str());
François Gaffie4b2018b2018-11-07 11:18:59 +01002230 return AUDIO_STREAM_MUSIC;
2231 }
François Gaffied0ba9ed2018-11-05 11:50:42 +01002232 AudioProductStrategyVector strategies;
2233 listAudioProductStrategies(strategies);
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002234 for (const auto& strategy : strategies) {
François Gaffie4b2018b2018-11-07 11:18:59 +01002235 if (strategy.getId() == psId) {
François Gaffie1e2b56f2022-04-01 14:34:29 +02002236 auto attrVect = strategy.getVolumeGroupAttributes();
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002237 auto iter = std::find_if(begin(attrVect), end(attrVect), [&attr](const auto& refAttr) {
François Gaffie8d7fd5a2023-01-17 17:28:09 +01002238 return refAttr.matchesScore(attr) > 0;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002239 });
François Gaffied0ba9ed2018-11-05 11:50:42 +01002240 if (iter != end(attrVect)) {
2241 return iter->getStreamType();
2242 }
2243 }
2244 }
Jean-Michel Trivied678652019-12-19 13:39:30 -08002245 switch (attr.usage) {
2246 case AUDIO_USAGE_VIRTUAL_SOURCE:
2247 // virtual source is not expected to have an associated product strategy
2248 break;
2249 default:
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002250 ALOGE("invalid attributes %s when converting to stream", toString(attr).c_str());
Jean-Michel Trivied678652019-12-19 13:39:30 -08002251 break;
2252 }
François Gaffied0ba9ed2018-11-05 11:50:42 +01002253 return AUDIO_STREAM_MUSIC;
2254}
2255
François Gaffie1e2b56f2022-04-01 14:34:29 +02002256status_t AudioSystem::getProductStrategyFromAudioAttributes(const audio_attributes_t& aa,
Francois Gaffie11b65922020-09-24 16:59:08 +02002257 product_strategy_t& productStrategy,
2258 bool fallbackOnDefault) {
Andy Hung264fa4c2024-02-21 15:52:12 -08002259 const sp<IAudioPolicyService> aps = get_audio_policy_service();
François Gaffie4b2018b2018-11-07 11:18:59 +01002260 if (aps == 0) return PERMISSION_DENIED;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002261
Mikhail Naganov1c400902023-05-17 11:48:43 -07002262 media::audio::common::AudioAttributes aaAidl = VALUE_OR_RETURN_STATUS(
2263 legacy2aidl_audio_attributes_t_AudioAttributes(aa));
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002264 int32_t productStrategyAidl;
2265
2266 RETURN_STATUS_IF_ERROR(statusTFromBinderStatus(
Francois Gaffie11b65922020-09-24 16:59:08 +02002267 aps->getProductStrategyFromAudioAttributes(aaAidl, fallbackOnDefault,
2268 &productStrategyAidl)));
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002269 productStrategy = VALUE_OR_RETURN_STATUS(
2270 aidl2legacy_int32_t_product_strategy_t(productStrategyAidl));
2271 return OK;
François Gaffie4b2018b2018-11-07 11:18:59 +01002272}
2273
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002274status_t AudioSystem::listAudioVolumeGroups(AudioVolumeGroupVector& groups) {
Andy Hung264fa4c2024-02-21 15:52:12 -08002275 const sp<IAudioPolicyService> aps = get_audio_policy_service();
François Gaffie4b2018b2018-11-07 11:18:59 +01002276 if (aps == 0) return PERMISSION_DENIED;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002277
2278 std::vector<media::AudioVolumeGroup> groupsAidl;
2279 RETURN_STATUS_IF_ERROR(
2280 statusTFromBinderStatus(aps->listAudioVolumeGroups(&groupsAidl)));
2281 groups = VALUE_OR_RETURN_STATUS(
2282 convertContainer<AudioVolumeGroupVector>(groupsAidl, aidl2legacy_AudioVolumeGroup));
2283 return OK;
François Gaffie4b2018b2018-11-07 11:18:59 +01002284}
2285
François Gaffie1e2b56f2022-04-01 14:34:29 +02002286status_t AudioSystem::getVolumeGroupFromAudioAttributes(const audio_attributes_t &aa,
Francois Gaffie11b65922020-09-24 16:59:08 +02002287 volume_group_t& volumeGroup,
2288 bool fallbackOnDefault) {
Andy Hung264fa4c2024-02-21 15:52:12 -08002289 const sp<IAudioPolicyService> aps = get_audio_policy_service();
François Gaffie4b2018b2018-11-07 11:18:59 +01002290 if (aps == 0) return PERMISSION_DENIED;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002291
Mikhail Naganov1c400902023-05-17 11:48:43 -07002292 media::audio::common::AudioAttributes aaAidl = VALUE_OR_RETURN_STATUS(
2293 legacy2aidl_audio_attributes_t_AudioAttributes(aa));
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002294 int32_t volumeGroupAidl;
2295 RETURN_STATUS_IF_ERROR(statusTFromBinderStatus(
Francois Gaffie11b65922020-09-24 16:59:08 +02002296 aps->getVolumeGroupFromAudioAttributes(aaAidl, fallbackOnDefault, &volumeGroupAidl)));
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002297 volumeGroup = VALUE_OR_RETURN_STATUS(aidl2legacy_int32_t_volume_group_t(volumeGroupAidl));
2298 return OK;
Arun Mirpuri11029ad2018-12-19 20:45:19 -08002299}
Eric Laurentb78763e2018-10-17 10:08:02 -07002300
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002301status_t AudioSystem::setRttEnabled(bool enabled) {
Andy Hung264fa4c2024-02-21 15:52:12 -08002302 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Eric Laurent6ede98f2019-06-11 14:50:30 -07002303 if (aps == 0) return PERMISSION_DENIED;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002304 return statusTFromBinderStatus(aps->setRttEnabled(enabled));
Eric Laurent6ede98f2019-06-11 14:50:30 -07002305}
2306
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002307bool AudioSystem::isCallScreenModeSupported() {
Andy Hung264fa4c2024-02-21 15:52:12 -08002308 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Eric Laurent8340e672019-11-06 11:01:08 -08002309 if (aps == 0) return false;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002310
2311 auto result = [&]() -> ConversionResult<bool> {
2312 bool retAidl;
2313 RETURN_IF_ERROR(
2314 statusTFromBinderStatus(aps->isCallScreenModeSupported(&retAidl)));
2315 return retAidl;
2316 }();
2317 return result.value_or(false);
Eric Laurent8340e672019-11-06 11:01:08 -08002318}
2319
jiabin0a488932020-08-07 17:32:40 -07002320status_t AudioSystem::setDevicesRoleForStrategy(product_strategy_t strategy,
2321 device_role_t role,
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002322 const AudioDeviceTypeAddrVector& devices) {
Andy Hung264fa4c2024-02-21 15:52:12 -08002323 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Jean-Michel Trivi30857152019-11-01 11:04:15 -07002324 if (aps == 0) {
2325 return PERMISSION_DENIED;
2326 }
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002327
2328 int32_t strategyAidl = VALUE_OR_RETURN_STATUS(legacy2aidl_product_strategy_t_int32_t(strategy));
2329 media::DeviceRole roleAidl = VALUE_OR_RETURN_STATUS(legacy2aidl_device_role_t_DeviceRole(role));
Mikhail Naganovf4a75362021-09-16 00:02:54 +00002330 std::vector<AudioDevice> devicesAidl = VALUE_OR_RETURN_STATUS(
2331 convertContainer<std::vector<AudioDevice>>(devices,
2332 legacy2aidl_AudioDeviceTypeAddress));
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002333 return statusTFromBinderStatus(
2334 aps->setDevicesRoleForStrategy(strategyAidl, roleAidl, devicesAidl));
Jean-Michel Trivi30857152019-11-01 11:04:15 -07002335}
2336
Paul Wang5d7cdb52022-11-22 09:45:06 +00002337status_t AudioSystem::removeDevicesRoleForStrategy(product_strategy_t strategy,
2338 device_role_t role,
2339 const AudioDeviceTypeAddrVector& devices) {
Andy Hung264fa4c2024-02-21 15:52:12 -08002340 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Paul Wang5d7cdb52022-11-22 09:45:06 +00002341 if (aps == 0) {
2342 return PERMISSION_DENIED;
2343 }
2344
2345 int32_t strategyAidl = VALUE_OR_RETURN_STATUS(legacy2aidl_product_strategy_t_int32_t(strategy));
2346 media::DeviceRole roleAidl = VALUE_OR_RETURN_STATUS(legacy2aidl_device_role_t_DeviceRole(role));
2347 std::vector<AudioDevice> devicesAidl = VALUE_OR_RETURN_STATUS(
2348 convertContainer<std::vector<AudioDevice>>(devices,
2349 legacy2aidl_AudioDeviceTypeAddress));
2350 return statusTFromBinderStatus(
2351 aps->removeDevicesRoleForStrategy(strategyAidl, roleAidl, devicesAidl));
2352}
2353
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002354status_t
Paul Wang5d7cdb52022-11-22 09:45:06 +00002355AudioSystem::clearDevicesRoleForStrategy(product_strategy_t strategy, device_role_t role) {
Andy Hung264fa4c2024-02-21 15:52:12 -08002356 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Jean-Michel Trivi30857152019-11-01 11:04:15 -07002357 if (aps == 0) {
2358 return PERMISSION_DENIED;
2359 }
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002360 int32_t strategyAidl = VALUE_OR_RETURN_STATUS(legacy2aidl_product_strategy_t_int32_t(strategy));
2361 media::DeviceRole roleAidl = VALUE_OR_RETURN_STATUS(legacy2aidl_device_role_t_DeviceRole(role));
2362 return statusTFromBinderStatus(
Paul Wang5d7cdb52022-11-22 09:45:06 +00002363 aps->clearDevicesRoleForStrategy(strategyAidl, roleAidl));
Jean-Michel Trivi30857152019-11-01 11:04:15 -07002364}
2365
jiabin0a488932020-08-07 17:32:40 -07002366status_t AudioSystem::getDevicesForRoleAndStrategy(product_strategy_t strategy,
2367 device_role_t role,
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002368 AudioDeviceTypeAddrVector& devices) {
Andy Hung264fa4c2024-02-21 15:52:12 -08002369 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Jean-Michel Trivi30857152019-11-01 11:04:15 -07002370 if (aps == 0) {
2371 return PERMISSION_DENIED;
2372 }
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002373 int32_t strategyAidl = VALUE_OR_RETURN_STATUS(legacy2aidl_product_strategy_t_int32_t(strategy));
2374 media::DeviceRole roleAidl = VALUE_OR_RETURN_STATUS(legacy2aidl_device_role_t_DeviceRole(role));
Mikhail Naganovf4a75362021-09-16 00:02:54 +00002375 std::vector<AudioDevice> devicesAidl;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002376 RETURN_STATUS_IF_ERROR(statusTFromBinderStatus(
2377 aps->getDevicesForRoleAndStrategy(strategyAidl, roleAidl, &devicesAidl)));
2378 devices = VALUE_OR_RETURN_STATUS(
2379 convertContainer<AudioDeviceTypeAddrVector>(devicesAidl,
2380 aidl2legacy_AudioDeviceTypeAddress));
2381 return OK;
Jean-Michel Trivi30857152019-11-01 11:04:15 -07002382}
2383
Jiabin Huang3b98d322020-09-03 17:54:16 +00002384status_t AudioSystem::setDevicesRoleForCapturePreset(audio_source_t audioSource,
2385 device_role_t role,
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002386 const AudioDeviceTypeAddrVector& devices) {
Andy Hung264fa4c2024-02-21 15:52:12 -08002387 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Jiabin Huang3b98d322020-09-03 17:54:16 +00002388 if (aps == 0) {
2389 return PERMISSION_DENIED;
2390 }
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002391
Mikhail Naganovddceecc2021-09-03 13:58:56 -07002392 AudioSource audioSourceAidl = VALUE_OR_RETURN_STATUS(
2393 legacy2aidl_audio_source_t_AudioSource(audioSource));
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002394 media::DeviceRole roleAidl = VALUE_OR_RETURN_STATUS(legacy2aidl_device_role_t_DeviceRole(role));
Mikhail Naganovf4a75362021-09-16 00:02:54 +00002395 std::vector<AudioDevice> devicesAidl = VALUE_OR_RETURN_STATUS(
2396 convertContainer<std::vector<AudioDevice>>(devices,
2397 legacy2aidl_AudioDeviceTypeAddress));
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002398 return statusTFromBinderStatus(
2399 aps->setDevicesRoleForCapturePreset(audioSourceAidl, roleAidl, devicesAidl));
Jiabin Huang3b98d322020-09-03 17:54:16 +00002400}
2401
2402status_t AudioSystem::addDevicesRoleForCapturePreset(audio_source_t audioSource,
2403 device_role_t role,
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002404 const AudioDeviceTypeAddrVector& devices) {
Andy Hung264fa4c2024-02-21 15:52:12 -08002405 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Jiabin Huang3b98d322020-09-03 17:54:16 +00002406 if (aps == 0) {
2407 return PERMISSION_DENIED;
2408 }
Mikhail Naganovddceecc2021-09-03 13:58:56 -07002409 AudioSource audioSourceAidl = VALUE_OR_RETURN_STATUS(
2410 legacy2aidl_audio_source_t_AudioSource(audioSource));
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002411 media::DeviceRole roleAidl = VALUE_OR_RETURN_STATUS(legacy2aidl_device_role_t_DeviceRole(role));
Mikhail Naganovf4a75362021-09-16 00:02:54 +00002412 std::vector<AudioDevice> devicesAidl = VALUE_OR_RETURN_STATUS(
2413 convertContainer<std::vector<AudioDevice>>(devices,
2414 legacy2aidl_AudioDeviceTypeAddress));
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002415 return statusTFromBinderStatus(
2416 aps->addDevicesRoleForCapturePreset(audioSourceAidl, roleAidl, devicesAidl));
Jiabin Huang3b98d322020-09-03 17:54:16 +00002417}
2418
2419status_t AudioSystem::removeDevicesRoleForCapturePreset(
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002420 audio_source_t audioSource, device_role_t role, const AudioDeviceTypeAddrVector& devices) {
Andy Hung264fa4c2024-02-21 15:52:12 -08002421 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Jiabin Huang3b98d322020-09-03 17:54:16 +00002422 if (aps == 0) {
2423 return PERMISSION_DENIED;
2424 }
Mikhail Naganovddceecc2021-09-03 13:58:56 -07002425 AudioSource audioSourceAidl = VALUE_OR_RETURN_STATUS(
2426 legacy2aidl_audio_source_t_AudioSource(audioSource));
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002427 media::DeviceRole roleAidl = VALUE_OR_RETURN_STATUS(legacy2aidl_device_role_t_DeviceRole(role));
Mikhail Naganovf4a75362021-09-16 00:02:54 +00002428 std::vector<AudioDevice> devicesAidl = VALUE_OR_RETURN_STATUS(
2429 convertContainer<std::vector<AudioDevice>>(devices,
2430 legacy2aidl_AudioDeviceTypeAddress));
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002431 return statusTFromBinderStatus(
2432 aps->removeDevicesRoleForCapturePreset(audioSourceAidl, roleAidl, devicesAidl));
Jiabin Huang3b98d322020-09-03 17:54:16 +00002433}
2434
2435status_t AudioSystem::clearDevicesRoleForCapturePreset(audio_source_t audioSource,
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002436 device_role_t role) {
Andy Hung264fa4c2024-02-21 15:52:12 -08002437 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Jiabin Huang3b98d322020-09-03 17:54:16 +00002438 if (aps == 0) {
2439 return PERMISSION_DENIED;
2440 }
Mikhail Naganovddceecc2021-09-03 13:58:56 -07002441 AudioSource audioSourceAidl = VALUE_OR_RETURN_STATUS(
2442 legacy2aidl_audio_source_t_AudioSource(audioSource));
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002443 media::DeviceRole roleAidl = VALUE_OR_RETURN_STATUS(legacy2aidl_device_role_t_DeviceRole(role));
2444 return statusTFromBinderStatus(
2445 aps->clearDevicesRoleForCapturePreset(audioSourceAidl, roleAidl));
Jiabin Huang3b98d322020-09-03 17:54:16 +00002446}
2447
2448status_t AudioSystem::getDevicesForRoleAndCapturePreset(audio_source_t audioSource,
2449 device_role_t role,
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002450 AudioDeviceTypeAddrVector& devices) {
Andy Hung264fa4c2024-02-21 15:52:12 -08002451 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Jiabin Huang3b98d322020-09-03 17:54:16 +00002452 if (aps == 0) {
2453 return PERMISSION_DENIED;
2454 }
Mikhail Naganovddceecc2021-09-03 13:58:56 -07002455 AudioSource audioSourceAidl = VALUE_OR_RETURN_STATUS(
2456 legacy2aidl_audio_source_t_AudioSource(audioSource));
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002457 media::DeviceRole roleAidl = VALUE_OR_RETURN_STATUS(legacy2aidl_device_role_t_DeviceRole(role));
Mikhail Naganovf4a75362021-09-16 00:02:54 +00002458 std::vector<AudioDevice> devicesAidl;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002459 RETURN_STATUS_IF_ERROR(statusTFromBinderStatus(
2460 aps->getDevicesForRoleAndCapturePreset(audioSourceAidl, roleAidl, &devicesAidl)));
2461 devices = VALUE_OR_RETURN_STATUS(
2462 convertContainer<AudioDeviceTypeAddrVector>(devicesAidl,
2463 aidl2legacy_AudioDeviceTypeAddress));
2464 return OK;
Jiabin Huang3b98d322020-09-03 17:54:16 +00002465}
2466
Eric Laurent81dd0f52021-07-05 11:54:40 +02002467status_t AudioSystem::getSpatializer(const sp<media::INativeSpatializerCallback>& callback,
2468 sp<media::ISpatializer>* spatializer) {
Andy Hung264fa4c2024-02-21 15:52:12 -08002469 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Eric Laurent81dd0f52021-07-05 11:54:40 +02002470 if (spatializer == nullptr) {
2471 return BAD_VALUE;
2472 }
2473 if (aps == 0) {
2474 return PERMISSION_DENIED;
2475 }
2476 media::GetSpatializerResponse response;
2477 RETURN_STATUS_IF_ERROR(statusTFromBinderStatus(
2478 aps->getSpatializer(callback, &response)));
2479
2480 *spatializer = response.spatializer;
2481 return OK;
2482}
2483
2484status_t AudioSystem::canBeSpatialized(const audio_attributes_t *attr,
2485 const audio_config_t *config,
2486 const AudioDeviceTypeAddrVector &devices,
2487 bool *canBeSpatialized) {
Andy Hung264fa4c2024-02-21 15:52:12 -08002488 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Ram Mohan43297012022-08-30 16:11:49 +05302489 if (canBeSpatialized == nullptr) {
2490 return BAD_VALUE;
2491 }
Eric Laurent81dd0f52021-07-05 11:54:40 +02002492 if (aps == 0) {
2493 return PERMISSION_DENIED;
2494 }
2495 audio_attributes_t attributes = attr != nullptr ? *attr : AUDIO_ATTRIBUTES_INITIALIZER;
2496 audio_config_t configuration = config != nullptr ? *config : AUDIO_CONFIG_INITIALIZER;
2497
Mikhail Naganov1c400902023-05-17 11:48:43 -07002498 std::optional<media::audio::common::AudioAttributes> attrAidl = VALUE_OR_RETURN_STATUS(
2499 legacy2aidl_audio_attributes_t_AudioAttributes(attributes));
Mikhail Naganovdbf03642021-08-25 18:15:32 -07002500 std::optional<AudioConfig> configAidl = VALUE_OR_RETURN_STATUS(
Eric Laurent81dd0f52021-07-05 11:54:40 +02002501 legacy2aidl_audio_config_t_AudioConfig(configuration, false /*isInput*/));
Mikhail Naganovf4a75362021-09-16 00:02:54 +00002502 std::vector<AudioDevice> devicesAidl = VALUE_OR_RETURN_STATUS(
2503 convertContainer<std::vector<AudioDevice>>(devices,
2504 legacy2aidl_AudioDeviceTypeAddress));
Eric Laurent81dd0f52021-07-05 11:54:40 +02002505 RETURN_STATUS_IF_ERROR(statusTFromBinderStatus(
2506 aps->canBeSpatialized(attrAidl, configAidl, devicesAidl, canBeSpatialized)));
2507 return OK;
2508}
2509
Vlad Popae3fd1c22022-11-07 21:03:18 +01002510status_t AudioSystem::getSoundDoseInterface(const sp<media::ISoundDoseCallback>& callback,
2511 sp<media::ISoundDose>* soundDose) {
Andy Hung264fa4c2024-02-21 15:52:12 -08002512 const sp<IAudioFlinger> af = get_audio_flinger();
Vlad Popa63f047e2022-11-05 14:09:19 +01002513 if (af == nullptr) {
2514 return PERMISSION_DENIED;
2515 }
Vlad Popae3fd1c22022-11-07 21:03:18 +01002516 if (soundDose == nullptr) {
2517 return BAD_VALUE;
2518 }
Vlad Popa63f047e2022-11-05 14:09:19 +01002519
Vlad Popae3fd1c22022-11-07 21:03:18 +01002520 RETURN_STATUS_IF_ERROR(af->getSoundDoseInterface(callback, soundDose));
2521 return OK;
Vlad Popa63f047e2022-11-05 14:09:19 +01002522}
2523
jiabin2b9d5a12021-12-10 01:06:29 +00002524status_t AudioSystem::getDirectPlaybackSupport(const audio_attributes_t *attr,
2525 const audio_config_t *config,
2526 audio_direct_mode_t* directMode) {
2527 if (attr == nullptr || config == nullptr || directMode == nullptr) {
2528 return BAD_VALUE;
2529 }
2530
Andy Hung264fa4c2024-02-21 15:52:12 -08002531 const sp<IAudioPolicyService> aps = get_audio_policy_service();
jiabin2b9d5a12021-12-10 01:06:29 +00002532 if (aps == 0) {
2533 return PERMISSION_DENIED;
2534 }
2535
Mikhail Naganov1c400902023-05-17 11:48:43 -07002536 media::audio::common::AudioAttributes attrAidl = VALUE_OR_RETURN_STATUS(
2537 legacy2aidl_audio_attributes_t_AudioAttributes(*attr));
jiabin2b9d5a12021-12-10 01:06:29 +00002538 AudioConfig configAidl = VALUE_OR_RETURN_STATUS(
2539 legacy2aidl_audio_config_t_AudioConfig(*config, false /*isInput*/));
2540
2541 media::AudioDirectMode retAidl;
2542 RETURN_STATUS_IF_ERROR(statusTFromBinderStatus(
2543 aps->getDirectPlaybackSupport(attrAidl, configAidl, &retAidl)));
2544 *directMode = VALUE_OR_RETURN_STATUS(aidl2legacy_int32_t_audio_direct_mode_t_mask(
2545 static_cast<int32_t>(retAidl)));
2546 return NO_ERROR;
2547}
2548
Dorin Drimusf2196d82022-01-03 12:11:18 +01002549status_t AudioSystem::getDirectProfilesForAttributes(const audio_attributes_t* attr,
2550 std::vector<audio_profile>* audioProfiles) {
Mikhail Naganovefc504b2022-06-22 03:24:59 +00002551 if (attr == nullptr || audioProfiles == nullptr) {
Dorin Drimusf2196d82022-01-03 12:11:18 +01002552 return BAD_VALUE;
2553 }
2554
Andy Hung264fa4c2024-02-21 15:52:12 -08002555 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Dorin Drimusf2196d82022-01-03 12:11:18 +01002556 if (aps == 0) {
2557 return PERMISSION_DENIED;
2558 }
2559
Mikhail Naganov1c400902023-05-17 11:48:43 -07002560 media::audio::common::AudioAttributes attrAidl = VALUE_OR_RETURN_STATUS(
2561 legacy2aidl_audio_attributes_t_AudioAttributes(*attr));
Dorin Drimusf2196d82022-01-03 12:11:18 +01002562
2563 std::vector<media::audio::common::AudioProfile> audioProfilesAidl;
2564 RETURN_STATUS_IF_ERROR(statusTFromBinderStatus(
2565 aps->getDirectProfilesForAttributes(attrAidl, &audioProfilesAidl)));
2566 *audioProfiles = VALUE_OR_RETURN_STATUS(convertContainer<std::vector<audio_profile>>(
2567 audioProfilesAidl, aidl2legacy_AudioProfile_audio_profile, false /*isInput*/));
2568
2569 return NO_ERROR;
2570}
Eric Laurent81dd0f52021-07-05 11:54:40 +02002571
Eric Laurent076e7c72022-05-03 18:12:28 +02002572status_t AudioSystem::setRequestedLatencyMode(
2573 audio_io_handle_t output, audio_latency_mode_t mode) {
Andy Hung264fa4c2024-02-21 15:52:12 -08002574 const sp<IAudioFlinger> af = get_audio_flinger();
Eric Laurent076e7c72022-05-03 18:12:28 +02002575 if (af == nullptr) {
2576 return PERMISSION_DENIED;
2577 }
2578 return af->setRequestedLatencyMode(output, mode);
2579}
2580
2581status_t AudioSystem::getSupportedLatencyModes(audio_io_handle_t output,
2582 std::vector<audio_latency_mode_t>* modes) {
Andy Hung264fa4c2024-02-21 15:52:12 -08002583 const sp<IAudioFlinger> af = get_audio_flinger();
Eric Laurent076e7c72022-05-03 18:12:28 +02002584 if (af == nullptr) {
2585 return PERMISSION_DENIED;
2586 }
2587 return af->getSupportedLatencyModes(output, modes);
2588}
2589
Eric Laurent50d72582022-12-20 20:20:23 +01002590status_t AudioSystem::setBluetoothVariableLatencyEnabled(bool enabled) {
Andy Hung264fa4c2024-02-21 15:52:12 -08002591 const sp<IAudioFlinger> af = get_audio_flinger();
Eric Laurent52057642022-12-16 11:45:07 +01002592 if (af == nullptr) {
2593 return PERMISSION_DENIED;
2594 }
Eric Laurent50d72582022-12-20 20:20:23 +01002595 return af->setBluetoothVariableLatencyEnabled(enabled);
Eric Laurent52057642022-12-16 11:45:07 +01002596}
2597
Eric Laurent50d72582022-12-20 20:20:23 +01002598status_t AudioSystem::isBluetoothVariableLatencyEnabled(
2599 bool *enabled) {
Andy Hung264fa4c2024-02-21 15:52:12 -08002600 const sp<IAudioFlinger> af = get_audio_flinger();
Eric Laurent50d72582022-12-20 20:20:23 +01002601 if (af == nullptr) {
2602 return PERMISSION_DENIED;
2603 }
2604 return af->isBluetoothVariableLatencyEnabled(enabled);
2605}
2606
2607status_t AudioSystem::supportsBluetoothVariableLatency(
Eric Laurent52057642022-12-16 11:45:07 +01002608 bool *support) {
Andy Hung264fa4c2024-02-21 15:52:12 -08002609 const sp<IAudioFlinger> af = get_audio_flinger();
Eric Laurent52057642022-12-16 11:45:07 +01002610 if (af == nullptr) {
2611 return PERMISSION_DENIED;
2612 }
Eric Laurent50d72582022-12-20 20:20:23 +01002613 return af->supportsBluetoothVariableLatency(support);
Eric Laurent52057642022-12-16 11:45:07 +01002614}
2615
Mikhail Naganovffd97712023-05-03 17:45:36 -07002616status_t AudioSystem::getAudioPolicyConfig(media::AudioPolicyConfig *config) {
Andy Hung264fa4c2024-02-21 15:52:12 -08002617 const sp<IAudioFlinger> af = get_audio_flinger();
Mikhail Naganovffd97712023-05-03 17:45:36 -07002618 if (af == nullptr) {
2619 return PERMISSION_DENIED;
2620 }
2621 return af->getAudioPolicyConfig(config);
2622}
2623
Ytai Ben-Tsvi5120eda2020-03-26 09:41:15 -07002624class CaptureStateListenerImpl : public media::BnCaptureStateListener,
2625 public IBinder::DeathRecipient {
2626public:
Ytai Ben-Tsvid9f82832020-08-27 09:31:10 -07002627 CaptureStateListenerImpl(
2628 const sp<IAudioPolicyService>& aps,
2629 const sp<AudioSystem::CaptureStateListener>& listener)
Ytai Ben-Tsvia46b6d32020-08-31 13:29:11 -07002630 : mAps(aps), mListener(listener) {}
2631
2632 void init() {
Ytai Ben-Tsvid9f82832020-08-27 09:31:10 -07002633 bool active;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002634 status_t status = statusTFromBinderStatus(
2635 mAps->registerSoundTriggerCaptureStateListener(this, &active));
Ytai Ben-Tsvid9f82832020-08-27 09:31:10 -07002636 if (status != NO_ERROR) {
2637 mListener->onServiceDied();
2638 return;
2639 }
2640 mListener->onStateChanged(active);
Ytai Ben-Tsvia46b6d32020-08-31 13:29:11 -07002641 IInterface::asBinder(mAps)->linkToDeath(this);
Ytai Ben-Tsvid9f82832020-08-27 09:31:10 -07002642 }
2643
Ytai Ben-Tsvi5120eda2020-03-26 09:41:15 -07002644 binder::Status setCaptureState(bool active) override {
Andy Hungacd955a2024-02-16 17:14:36 -08002645 std::lock_guard _l(AudioSystem::gSoundTriggerMutex);
Ytai Ben-Tsvid9f82832020-08-27 09:31:10 -07002646 mListener->onStateChanged(active);
Ytai Ben-Tsvi5120eda2020-03-26 09:41:15 -07002647 return binder::Status::ok();
2648 }
2649
2650 void binderDied(const wp<IBinder>&) override {
Andy Hungacd955a2024-02-16 17:14:36 -08002651 std::lock_guard _l(AudioSystem::gSoundTriggerMutex);
Ytai Ben-Tsvid9f82832020-08-27 09:31:10 -07002652 mListener->onServiceDied();
Andy Hungacd955a2024-02-16 17:14:36 -08002653 AudioSystem::gSoundTriggerCaptureStateListener = nullptr;
Ytai Ben-Tsvi5120eda2020-03-26 09:41:15 -07002654 }
Ytai Ben-Tsvid9f82832020-08-27 09:31:10 -07002655
2656private:
2657 // Need this in order to keep the death receipent alive.
2658 sp<IAudioPolicyService> mAps;
2659 sp<AudioSystem::CaptureStateListener> mListener;
Ytai Ben-Tsvi5120eda2020-03-26 09:41:15 -07002660};
2661
2662status_t AudioSystem::registerSoundTriggerCaptureStateListener(
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002663 const sp<CaptureStateListener>& listener) {
Ytai Ben-Tsvid9f82832020-08-27 09:31:10 -07002664 LOG_ALWAYS_FATAL_IF(listener == nullptr);
2665
Andy Hung264fa4c2024-02-21 15:52:12 -08002666 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Ytai Ben-Tsvi5120eda2020-03-26 09:41:15 -07002667 if (aps == 0) {
2668 return PERMISSION_DENIED;
2669 }
2670
Andy Hungacd955a2024-02-16 17:14:36 -08002671 std::lock_guard _l(AudioSystem::gSoundTriggerMutex);
Ytai Ben-Tsvid9f82832020-08-27 09:31:10 -07002672 gSoundTriggerCaptureStateListener = new CaptureStateListenerImpl(aps, listener);
Ytai Ben-Tsvia46b6d32020-08-31 13:29:11 -07002673 gSoundTriggerCaptureStateListener->init();
Ytai Ben-Tsvi5120eda2020-03-26 09:41:15 -07002674
Ytai Ben-Tsvi5120eda2020-03-26 09:41:15 -07002675 return NO_ERROR;
2676}
2677
jiabin1319f5a2021-03-30 22:21:24 +00002678status_t AudioSystem::setVibratorInfos(
2679 const std::vector<media::AudioVibratorInfo>& vibratorInfos) {
Andy Hung264fa4c2024-02-21 15:52:12 -08002680 const sp<IAudioFlinger> af = get_audio_flinger();
jiabin1319f5a2021-03-30 22:21:24 +00002681 if (af == nullptr) {
2682 return PERMISSION_DENIED;
2683 }
2684 return af->setVibratorInfos(vibratorInfos);
2685}
2686
Jiabin Huangebe64102021-09-07 20:01:07 +00002687status_t AudioSystem::getMmapPolicyInfo(
jiabine99d0882021-09-17 05:21:25 +00002688 AudioMMapPolicyType policyType, std::vector<AudioMMapPolicyInfo> *policyInfos) {
Andy Hung264fa4c2024-02-21 15:52:12 -08002689 const sp<IAudioFlinger> af = get_audio_flinger();
Jiabin Huangebe64102021-09-07 20:01:07 +00002690 if (af == nullptr) {
2691 return PERMISSION_DENIED;
2692 }
2693 return af->getMmapPolicyInfos(policyType, policyInfos);
2694}
2695
jiabine504e7b2021-09-18 00:27:08 +00002696int32_t AudioSystem::getAAudioMixerBurstCount() {
Andy Hung264fa4c2024-02-21 15:52:12 -08002697 const sp<IAudioFlinger> af = get_audio_flinger();
jiabine504e7b2021-09-18 00:27:08 +00002698 if (af == nullptr) {
2699 return PERMISSION_DENIED;
2700 }
2701 return af->getAAudioMixerBurstCount();
2702}
2703
2704int32_t AudioSystem::getAAudioHardwareBurstMinUsec() {
Andy Hung264fa4c2024-02-21 15:52:12 -08002705 const sp<IAudioFlinger> af = get_audio_flinger();
jiabine504e7b2021-09-18 00:27:08 +00002706 if (af == nullptr) {
2707 return PERMISSION_DENIED;
2708 }
2709 return af->getAAudioHardwareBurstMinUsec();
2710}
2711
jiabina84c3d32022-12-02 18:59:55 +00002712status_t AudioSystem::getSupportedMixerAttributes(
2713 audio_port_handle_t portId, std::vector<audio_mixer_attributes_t> *mixerAttrs) {
Andy Hung264fa4c2024-02-21 15:52:12 -08002714 const sp<IAudioPolicyService> aps = get_audio_policy_service();
jiabina84c3d32022-12-02 18:59:55 +00002715 if (aps == nullptr) {
2716 return PERMISSION_DENIED;
2717 }
2718
2719 int32_t portIdAidl = VALUE_OR_RETURN_STATUS(legacy2aidl_audio_port_handle_t_int32_t(portId));
2720 std::vector<media::AudioMixerAttributesInternal> _aidlReturn;
2721 RETURN_STATUS_IF_ERROR(statusTFromBinderStatus(
2722 aps->getSupportedMixerAttributes(portIdAidl, &_aidlReturn)));
2723 *mixerAttrs = VALUE_OR_RETURN_STATUS(
2724 convertContainer<std::vector<audio_mixer_attributes_t>>(
2725 _aidlReturn,
2726 aidl2legacy_AudioMixerAttributesInternal_audio_mixer_attributes_t));
2727 return OK;
2728}
2729
2730status_t AudioSystem::setPreferredMixerAttributes(const audio_attributes_t *attr,
2731 audio_port_handle_t portId,
2732 uid_t uid,
2733 const audio_mixer_attributes_t *mixerAttr) {
Andy Hung264fa4c2024-02-21 15:52:12 -08002734 const sp<IAudioPolicyService> aps = get_audio_policy_service();
jiabina84c3d32022-12-02 18:59:55 +00002735 if (aps == nullptr) {
2736 return PERMISSION_DENIED;
2737 }
2738
Mikhail Naganov1c400902023-05-17 11:48:43 -07002739 media::audio::common::AudioAttributes attrAidl = VALUE_OR_RETURN_STATUS(
2740 legacy2aidl_audio_attributes_t_AudioAttributes(*attr));
jiabina84c3d32022-12-02 18:59:55 +00002741 media::AudioMixerAttributesInternal mixerAttrAidl = VALUE_OR_RETURN_STATUS(
2742 legacy2aidl_audio_mixer_attributes_t_AudioMixerAttributesInternal(*mixerAttr));
2743 int32_t uidAidl = VALUE_OR_RETURN_STATUS(legacy2aidl_uid_t_int32_t(uid));
2744 int32_t portIdAidl = VALUE_OR_RETURN_STATUS(legacy2aidl_audio_port_handle_t_int32_t(portId));
2745
2746 return statusTFromBinderStatus(
2747 aps->setPreferredMixerAttributes(attrAidl, portIdAidl, uidAidl, mixerAttrAidl));
2748}
2749
2750status_t AudioSystem::getPreferredMixerAttributes(
2751 const audio_attributes_t *attr,
2752 audio_port_handle_t portId,
2753 std::optional<audio_mixer_attributes_t> *mixerAttr) {
Andy Hung264fa4c2024-02-21 15:52:12 -08002754 const sp<IAudioPolicyService> aps = get_audio_policy_service();
jiabina84c3d32022-12-02 18:59:55 +00002755 if (aps == nullptr) {
2756 return PERMISSION_DENIED;
2757 }
2758
Mikhail Naganov1c400902023-05-17 11:48:43 -07002759 media::audio::common::AudioAttributes attrAidl = VALUE_OR_RETURN_STATUS(
2760 legacy2aidl_audio_attributes_t_AudioAttributes(*attr));
jiabina84c3d32022-12-02 18:59:55 +00002761 int32_t portIdAidl = VALUE_OR_RETURN_STATUS(legacy2aidl_audio_port_handle_t_int32_t(portId));
2762 std::optional<media::AudioMixerAttributesInternal> _aidlReturn;
2763 RETURN_STATUS_IF_ERROR(statusTFromBinderStatus(
2764 aps->getPreferredMixerAttributes(attrAidl, portIdAidl, &_aidlReturn)));
2765
2766 if (_aidlReturn.has_value()) {
2767 *mixerAttr = VALUE_OR_RETURN_STATUS(
2768 aidl2legacy_AudioMixerAttributesInternal_audio_mixer_attributes_t(
2769 _aidlReturn.value()));
2770 }
2771 return NO_ERROR;
2772}
2773
2774status_t AudioSystem::clearPreferredMixerAttributes(const audio_attributes_t *attr,
2775 audio_port_handle_t portId,
2776 uid_t uid) {
Andy Hung264fa4c2024-02-21 15:52:12 -08002777 const sp<IAudioPolicyService> aps = get_audio_policy_service();
jiabina84c3d32022-12-02 18:59:55 +00002778 if (aps == nullptr) {
2779 return PERMISSION_DENIED;
2780 }
2781
Mikhail Naganov1c400902023-05-17 11:48:43 -07002782 media::audio::common::AudioAttributes attrAidl = VALUE_OR_RETURN_STATUS(
2783 legacy2aidl_audio_attributes_t_AudioAttributes(*attr));
jiabina84c3d32022-12-02 18:59:55 +00002784 int32_t uidAidl = VALUE_OR_RETURN_STATUS(legacy2aidl_uid_t_int32_t(uid));
2785 int32_t portIdAidl = VALUE_OR_RETURN_STATUS(legacy2aidl_audio_port_handle_t_int32_t(portId));
2786 return statusTFromBinderStatus(
2787 aps->clearPreferredMixerAttributes(attrAidl, portIdAidl, uidAidl));
2788}
2789
Eric Laurentc2f1f072009-07-17 12:17:14 -07002790// ---------------------------------------------------------------------------
2791
Eric Laurente8726fe2015-06-26 09:39:24 -07002792int AudioSystem::AudioPolicyServiceClient::addAudioPortCallback(
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002793 const sp<AudioPortCallback>& callback) {
Andy Hungacd955a2024-02-16 17:14:36 -08002794 std::lock_guard _l(mMutex);
Andy Hungd211ce02024-02-16 15:34:07 -08002795 return mAudioPortCallbacks.insert(callback).second ? mAudioPortCallbacks.size() : -1;
Eric Laurentb28753e2015-04-01 13:06:28 -07002796}
2797
Eric Laurente8726fe2015-06-26 09:39:24 -07002798int AudioSystem::AudioPolicyServiceClient::removeAudioPortCallback(
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002799 const sp<AudioPortCallback>& callback) {
Andy Hungacd955a2024-02-16 17:14:36 -08002800 std::lock_guard _l(mMutex);
Andy Hungd211ce02024-02-16 15:34:07 -08002801 return mAudioPortCallbacks.erase(callback) > 0 ? mAudioPortCallbacks.size() : -1;
Eric Laurentb28753e2015-04-01 13:06:28 -07002802}
2803
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002804Status AudioSystem::AudioPolicyServiceClient::onAudioPortListUpdate() {
Andy Hungacd955a2024-02-16 17:14:36 -08002805 std::lock_guard _l(mMutex);
Andy Hungd211ce02024-02-16 15:34:07 -08002806 for (const auto& callback : mAudioPortCallbacks) {
2807 callback->onAudioPortListUpdate();
Eric Laurentb28753e2015-04-01 13:06:28 -07002808 }
Ytai Ben-Tsvi7e7a79d2020-12-15 16:48:16 -08002809 return Status::ok();
Eric Laurentb28753e2015-04-01 13:06:28 -07002810}
2811
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002812Status AudioSystem::AudioPolicyServiceClient::onAudioPatchListUpdate() {
Andy Hungacd955a2024-02-16 17:14:36 -08002813 std::lock_guard _l(mMutex);
Andy Hungd211ce02024-02-16 15:34:07 -08002814 for (const auto& callback : mAudioPortCallbacks) {
2815 callback->onAudioPatchListUpdate();
Eric Laurentb28753e2015-04-01 13:06:28 -07002816 }
Ytai Ben-Tsvi7e7a79d2020-12-15 16:48:16 -08002817 return Status::ok();
Eric Laurentb28753e2015-04-01 13:06:28 -07002818}
2819
François Gaffiecfe17322018-11-07 13:41:29 +01002820// ----------------------------------------------------------------------------
2821int AudioSystem::AudioPolicyServiceClient::addAudioVolumeGroupCallback(
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002822 const sp<AudioVolumeGroupCallback>& callback) {
Andy Hungacd955a2024-02-16 17:14:36 -08002823 std::lock_guard _l(mMutex);
Andy Hungd211ce02024-02-16 15:34:07 -08002824 return mAudioVolumeGroupCallbacks.insert(callback).second
2825 ? mAudioVolumeGroupCallbacks.size() : -1;
François Gaffiecfe17322018-11-07 13:41:29 +01002826}
2827
2828int AudioSystem::AudioPolicyServiceClient::removeAudioVolumeGroupCallback(
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002829 const sp<AudioVolumeGroupCallback>& callback) {
Andy Hungacd955a2024-02-16 17:14:36 -08002830 std::lock_guard _l(mMutex);
Andy Hungd211ce02024-02-16 15:34:07 -08002831 return mAudioVolumeGroupCallbacks.erase(callback) > 0
2832 ? mAudioVolumeGroupCallbacks.size() : -1;
François Gaffiecfe17322018-11-07 13:41:29 +01002833}
2834
Ytai Ben-Tsvi7e7a79d2020-12-15 16:48:16 -08002835Status AudioSystem::AudioPolicyServiceClient::onAudioVolumeGroupChanged(int32_t group,
2836 int32_t flags) {
2837 volume_group_t groupLegacy = VALUE_OR_RETURN_BINDER_STATUS(
2838 aidl2legacy_int32_t_volume_group_t(group));
2839 int flagsLegacy = VALUE_OR_RETURN_BINDER_STATUS(convertReinterpret<int>(flags));
2840
Andy Hungacd955a2024-02-16 17:14:36 -08002841 std::lock_guard _l(mMutex);
Andy Hungd211ce02024-02-16 15:34:07 -08002842 for (const auto& callback : mAudioVolumeGroupCallbacks) {
2843 callback->onAudioVolumeGroupChanged(groupLegacy, flagsLegacy);
François Gaffiecfe17322018-11-07 13:41:29 +01002844 }
Ytai Ben-Tsvi7e7a79d2020-12-15 16:48:16 -08002845 return Status::ok();
François Gaffiecfe17322018-11-07 13:41:29 +01002846}
2847// ----------------------------------------------------------------------------
2848
Ytai Ben-Tsvi7e7a79d2020-12-15 16:48:16 -08002849Status AudioSystem::AudioPolicyServiceClient::onDynamicPolicyMixStateUpdate(
2850 const ::std::string& regId, int32_t state) {
2851 ALOGV("AudioPolicyServiceClient::onDynamicPolicyMixStateUpdate(%s, %d)", regId.c_str(), state);
2852
2853 String8 regIdLegacy = VALUE_OR_RETURN_BINDER_STATUS(aidl2legacy_string_view_String8(regId));
2854 int stateLegacy = VALUE_OR_RETURN_BINDER_STATUS(convertReinterpret<int>(state));
Jean-Michel Trivif613d422015-04-23 18:41:29 -07002855 dynamic_policy_callback cb = NULL;
2856 {
Andy Hungacd955a2024-02-16 17:14:36 -08002857 std::lock_guard _l(AudioSystem::gMutex);
Jean-Michel Trivif613d422015-04-23 18:41:29 -07002858 cb = gDynPolicyCallback;
2859 }
2860
2861 if (cb != NULL) {
Ytai Ben-Tsvi7e7a79d2020-12-15 16:48:16 -08002862 cb(DYNAMIC_POLICY_EVENT_MIX_STATE_UPDATE, regIdLegacy, stateLegacy);
Jean-Michel Trivif613d422015-04-23 18:41:29 -07002863 }
Ytai Ben-Tsvi7e7a79d2020-12-15 16:48:16 -08002864 return Status::ok();
Jean-Michel Trivide801052015-04-14 19:10:14 -07002865}
2866
Ytai Ben-Tsvi7e7a79d2020-12-15 16:48:16 -08002867Status AudioSystem::AudioPolicyServiceClient::onRecordingConfigurationUpdate(
2868 int32_t event,
2869 const media::RecordClientInfo& clientInfo,
Mikhail Naganovdbf03642021-08-25 18:15:32 -07002870 const AudioConfigBase& clientConfig,
Ytai Ben-Tsvi7e7a79d2020-12-15 16:48:16 -08002871 const std::vector<media::EffectDescriptor>& clientEffects,
Mikhail Naganovdbf03642021-08-25 18:15:32 -07002872 const AudioConfigBase& deviceConfig,
Ytai Ben-Tsvi7e7a79d2020-12-15 16:48:16 -08002873 const std::vector<media::EffectDescriptor>& effects,
2874 int32_t patchHandle,
Mikhail Naganovddceecc2021-09-03 13:58:56 -07002875 AudioSource source) {
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -08002876 record_config_callback cb = NULL;
2877 {
Andy Hungacd955a2024-02-16 17:14:36 -08002878 std::lock_guard _l(AudioSystem::gMutex);
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -08002879 cb = gRecordConfigCallback;
2880 }
2881
2882 if (cb != NULL) {
Ytai Ben-Tsvi7e7a79d2020-12-15 16:48:16 -08002883 int eventLegacy = VALUE_OR_RETURN_BINDER_STATUS(convertReinterpret<int>(event));
2884 record_client_info_t clientInfoLegacy = VALUE_OR_RETURN_BINDER_STATUS(
2885 aidl2legacy_RecordClientInfo_record_client_info_t(clientInfo));
2886 audio_config_base_t clientConfigLegacy = VALUE_OR_RETURN_BINDER_STATUS(
Mikhail Naganovde3fa182021-07-30 15:06:42 -07002887 aidl2legacy_AudioConfigBase_audio_config_base_t(clientConfig, true /*isInput*/));
Ytai Ben-Tsvi7e7a79d2020-12-15 16:48:16 -08002888 std::vector<effect_descriptor_t> clientEffectsLegacy = VALUE_OR_RETURN_BINDER_STATUS(
2889 convertContainer<std::vector<effect_descriptor_t>>(
2890 clientEffects,
2891 aidl2legacy_EffectDescriptor_effect_descriptor_t));
2892 audio_config_base_t deviceConfigLegacy = VALUE_OR_RETURN_BINDER_STATUS(
Mikhail Naganovde3fa182021-07-30 15:06:42 -07002893 aidl2legacy_AudioConfigBase_audio_config_base_t(deviceConfig, true /*isInput*/));
Ytai Ben-Tsvi7e7a79d2020-12-15 16:48:16 -08002894 std::vector<effect_descriptor_t> effectsLegacy = VALUE_OR_RETURN_BINDER_STATUS(
2895 convertContainer<std::vector<effect_descriptor_t>>(
2896 effects,
2897 aidl2legacy_EffectDescriptor_effect_descriptor_t));
2898 audio_patch_handle_t patchHandleLegacy = VALUE_OR_RETURN_BINDER_STATUS(
2899 aidl2legacy_int32_t_audio_patch_handle_t(patchHandle));
2900 audio_source_t sourceLegacy = VALUE_OR_RETURN_BINDER_STATUS(
Mikhail Naganovddceecc2021-09-03 13:58:56 -07002901 aidl2legacy_AudioSource_audio_source_t(source));
Ytai Ben-Tsvi7e7a79d2020-12-15 16:48:16 -08002902 cb(eventLegacy, &clientInfoLegacy, &clientConfigLegacy, clientEffectsLegacy,
2903 &deviceConfigLegacy, effectsLegacy, patchHandleLegacy, sourceLegacy);
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -08002904 }
Ytai Ben-Tsvi7e7a79d2020-12-15 16:48:16 -08002905 return Status::ok();
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -08002906}
2907
Jean-Michel Trivi9a6b9ad2020-10-22 16:46:43 -07002908Status AudioSystem::AudioPolicyServiceClient::onRoutingUpdated() {
2909 routing_callback cb = NULL;
2910 {
Andy Hungacd955a2024-02-16 17:14:36 -08002911 std::lock_guard _l(AudioSystem::gMutex);
Jean-Michel Trivi9a6b9ad2020-10-22 16:46:43 -07002912 cb = gRoutingCallback;
2913 }
2914
2915 if (cb != NULL) {
2916 cb();
2917 }
2918 return Status::ok();
2919}
2920
Jean-Michel Trivi78f2b302022-04-15 18:18:41 +00002921Status AudioSystem::AudioPolicyServiceClient::onVolumeRangeInitRequest() {
2922 vol_range_init_req_callback cb = NULL;
2923 {
Andy Hungacd955a2024-02-16 17:14:36 -08002924 std::lock_guard _l(AudioSystem::gMutex);
Jean-Michel Trivi78f2b302022-04-15 18:18:41 +00002925 cb = gVolRangeInitReqCallback;
2926 }
2927
2928 if (cb != NULL) {
2929 cb();
2930 }
2931 return Status::ok();
2932}
2933
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002934void AudioSystem::AudioPolicyServiceClient::binderDied(const wp<IBinder>& who __unused) {
Glenn Kastend2d089f2014-11-05 11:48:12 -08002935 {
Andy Hungacd955a2024-02-16 17:14:36 -08002936 std::lock_guard _l(mMutex);
Andy Hungd211ce02024-02-16 15:34:07 -08002937 for (const auto& callback : mAudioPortCallbacks) {
2938 callback->onServiceDied();
Glenn Kastend2d089f2014-11-05 11:48:12 -08002939 }
Andy Hungd211ce02024-02-16 15:34:07 -08002940 for (const auto& callback : mAudioVolumeGroupCallbacks) {
2941 callback->onServiceDied();
François Gaffiecfe17322018-11-07 13:41:29 +01002942 }
Eric Laurentb52c1522014-05-20 11:27:36 -07002943 }
Mikhail Naganovec3d5792022-05-06 00:19:55 +00002944 AudioSystem::clearAudioPolicyService();
Eric Laurentc2f1f072009-07-17 12:17:14 -07002945
Steve Block5ff1dd52012-01-05 23:22:43 +00002946 ALOGW("AudioPolicyService server died!");
Eric Laurentc2f1f072009-07-17 12:17:14 -07002947}
2948
Ytai Ben-Tsvi7e7a79d2020-12-15 16:48:16 -08002949ConversionResult<record_client_info_t>
2950aidl2legacy_RecordClientInfo_record_client_info_t(const media::RecordClientInfo& aidl) {
2951 record_client_info_t legacy;
2952 legacy.riid = VALUE_OR_RETURN(aidl2legacy_int32_t_audio_unique_id_t(aidl.riid));
2953 legacy.uid = VALUE_OR_RETURN(aidl2legacy_int32_t_uid_t(aidl.uid));
2954 legacy.session = VALUE_OR_RETURN(aidl2legacy_int32_t_audio_session_t(aidl.session));
Mikhail Naganovddceecc2021-09-03 13:58:56 -07002955 legacy.source = VALUE_OR_RETURN(aidl2legacy_AudioSource_audio_source_t(aidl.source));
Ytai Ben-Tsvi7e7a79d2020-12-15 16:48:16 -08002956 legacy.port_id = VALUE_OR_RETURN(aidl2legacy_int32_t_audio_port_handle_t(aidl.portId));
2957 legacy.silenced = aidl.silenced;
2958 return legacy;
2959}
2960
2961ConversionResult<media::RecordClientInfo>
2962legacy2aidl_record_client_info_t_RecordClientInfo(const record_client_info_t& legacy) {
2963 media::RecordClientInfo aidl;
2964 aidl.riid = VALUE_OR_RETURN(legacy2aidl_audio_unique_id_t_int32_t(legacy.riid));
2965 aidl.uid = VALUE_OR_RETURN(legacy2aidl_uid_t_int32_t(legacy.uid));
2966 aidl.session = VALUE_OR_RETURN(legacy2aidl_audio_session_t_int32_t(legacy.session));
Mikhail Naganovddceecc2021-09-03 13:58:56 -07002967 aidl.source = VALUE_OR_RETURN(legacy2aidl_audio_source_t_AudioSource(legacy.source));
Ytai Ben-Tsvi7e7a79d2020-12-15 16:48:16 -08002968 aidl.portId = VALUE_OR_RETURN(legacy2aidl_audio_port_handle_t_int32_t(legacy.port_id));
2969 aidl.silenced = legacy.silenced;
2970 return aidl;
2971}
2972
Glenn Kasten40bc9062015-03-20 09:09:33 -07002973} // namespace android