blob: a467d2c1751a12d576894ad54880c62ec0e9a553 [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;
François Gaffie873738c2024-08-02 17:09:21 +020063using media::audio::common::AudioPolicyForceUse;
64using media::audio::common::AudioPolicyForcedConfig;
Mikhail Naganovddceecc2021-09-03 13:58:56 -070065using media::audio::common::AudioSource;
Mikhail Naganovdbf03642021-08-25 18:15:32 -070066using media::audio::common::AudioStreamType;
67using media::audio::common::AudioUsage;
Mikhail Naganov0078ee52021-09-30 23:06:20 +000068using media::audio::common::Int;
Ytai Ben-Tsvi10dc0a62020-09-18 11:31:55 -070069
Andy Hungacd955a2024-02-16 17:14:36 -080070std::mutex AudioSystem::gMutex;
Jean-Michel Trivif613d422015-04-23 18:41:29 -070071dynamic_policy_callback AudioSystem::gDynPolicyCallback = NULL;
Svet Ganovf4ddfef2018-01-16 07:37:58 -080072record_config_callback AudioSystem::gRecordConfigCallback = NULL;
Jean-Michel Trivi9a6b9ad2020-10-22 16:46:43 -070073routing_callback AudioSystem::gRoutingCallback = NULL;
Jean-Michel Trivi78f2b302022-04-15 18:18:41 +000074vol_range_init_req_callback AudioSystem::gVolRangeInitReqCallback = NULL;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080075
Andy Hung88e71732024-02-21 16:44:27 -080076std::mutex AudioSystem::gApsCallbackMutex;
Andy Hungacd955a2024-02-16 17:14:36 -080077std::mutex AudioSystem::gErrorCallbacksMutex;
78std::set<audio_error_callback> AudioSystem::gAudioErrorCallbacks;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -080079
Andy Hungacd955a2024-02-16 17:14:36 -080080std::mutex AudioSystem::gSoundTriggerMutex;
81sp<CaptureStateListenerImpl> AudioSystem::gSoundTriggerCaptureStateListener;
Ytai Ben-Tsvi5120eda2020-03-26 09:41:15 -070082
Andy Hungacd955a2024-02-16 17:14:36 -080083// Sets the Binder for the AudioFlinger service, passed to this client process
84// from the system server.
Ahaan Ugale058f94b2021-05-21 02:23:50 -070085// This allows specific isolated processes to access the audio system. Currently used only for the
86// HotwordDetectionService.
Andy Hungda1fb072024-02-20 19:08:08 -080087template <typename ServiceInterface, typename Client, typename AidlInterface,
88 typename ServiceTraits>
89class ServiceHandler {
90public:
Shraddha Basantwanif2aa2502024-04-08 11:01:57 +000091 sp<ServiceInterface> getService()
Andy Hungda1fb072024-02-20 19:08:08 -080092 EXCLUDES(mMutex) NO_THREAD_SAFETY_ANALYSIS { // std::unique_ptr
93 sp<ServiceInterface> service;
94 sp<Client> client;
Ahaan Ugale058f94b2021-05-21 02:23:50 -070095
Andy Hungda1fb072024-02-20 19:08:08 -080096 bool reportNoError = false;
97 {
98 std::lock_guard _l(mMutex);
99 if (mService != nullptr) {
100 return mService;
101 }
Andy Hung66fc0bd2023-01-04 11:45:50 -0800102 }
103
Andy Hungda1fb072024-02-20 19:08:08 -0800104 std::unique_lock ul_only1thread(mSingleGetter);
105 std::unique_lock ul(mMutex);
106 if (mService != nullptr) {
107 return mService;
108 }
109 if (mClient == nullptr) {
110 mClient = sp<Client>::make();
Andy Hung66fc0bd2023-01-04 11:45:50 -0800111 } else {
112 reportNoError = true;
113 }
Andy Hungda1fb072024-02-20 19:08:08 -0800114 while (true) {
115 mService = mLocalService;
116 if (mService != nullptr) break;
Andy Hung66fc0bd2023-01-04 11:45:50 -0800117
Andy Hungda1fb072024-02-20 19:08:08 -0800118 sp<IBinder> binder = mBinder;
119 if (binder == nullptr) {
120 sp <IServiceManager> sm = defaultServiceManager();
121 binder = sm->checkService(String16(ServiceTraits::SERVICE_NAME));
Paul Wangbcf95f62023-02-28 15:38:16 +0000122 if (binder == nullptr) {
Andy Hungda1fb072024-02-20 19:08:08 -0800123 ALOGD("%s: waiting for %s", __func__, ServiceTraits::SERVICE_NAME);
124
125 // if the condition variable is present, setLocalService() and
126 // setBinder() is allowed to use it to notify us.
127 if (mCvGetter == nullptr) {
128 mCvGetter = std::make_shared<std::condition_variable>();
129 }
130 mCvGetter->wait_for(ul, std::chrono::seconds(1));
131 continue;
Paul Wangbcf95f62023-02-28 15:38:16 +0000132 }
Ahaan Ugale058f94b2021-05-21 02:23:50 -0700133 }
Andy Hungda1fb072024-02-20 19:08:08 -0800134 binder->linkToDeath(mClient);
135 auto aidlInterface = interface_cast<AidlInterface>(binder);
136 LOG_ALWAYS_FATAL_IF(aidlInterface == nullptr);
137 if constexpr (std::is_same_v<ServiceInterface, AidlInterface>) {
138 mService = std::move(aidlInterface);
139 } else /* constexpr */ {
140 mService = ServiceTraits::createServiceAdapter(aidlInterface);
141 }
142 break;
Glenn Kastene53b9ea2012-03-12 16:29:55 -0700143 }
Andy Hungda1fb072024-02-20 19:08:08 -0800144 if (mCvGetter) mCvGetter.reset(); // remove condition variable.
145 client = mClient;
146 service = mService;
147 // Make sure callbacks can be received by the client
Shraddha Basantwanif2aa2502024-04-08 11:01:57 +0000148 if (mCanStartThreadPool) {
Akshata Kadam1d486b72023-08-07 05:49:48 +0000149 ProcessState::self()->startThreadPool();
150 }
Andy Hungda1fb072024-02-20 19:08:08 -0800151 ul.unlock();
152 ul_only1thread.unlock();
153 ServiceTraits::onServiceCreate(service, client);
154 if (reportNoError) AudioSystem::reportError(NO_ERROR);
155 return service;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800156 }
Andy Hungda1fb072024-02-20 19:08:08 -0800157
158 status_t setLocalService(const sp<ServiceInterface>& service) EXCLUDES(mMutex) {
159 std::lock_guard _l(mMutex);
160 // we allow clearing once set, but not a double non-null set.
161 if (mService != nullptr && service != nullptr) return INVALID_OPERATION;
162 mLocalService = service;
163 if (mCvGetter) mCvGetter->notify_one();
164 return OK;
165 }
166
167 sp<Client> getClient() EXCLUDES(mMutex) {
168 const auto service = getService();
169 if (service == nullptr) return nullptr;
170 std::lock_guard _l(mMutex);
171 return mClient;
172 }
173
174 void setBinder(const sp<IBinder>& binder) EXCLUDES(mMutex) {
175 std::lock_guard _l(mMutex);
176 if (mService != nullptr) {
177 ALOGW("%s: ignoring; %s connection already established.",
178 __func__, ServiceTraits::SERVICE_NAME);
179 return;
180 }
181 mBinder = binder;
182 if (mCvGetter) mCvGetter->notify_one();
183 }
184
185 void clearService() EXCLUDES(mMutex) {
186 std::lock_guard _l(mMutex);
187 mService.clear();
188 if (mClient) ServiceTraits::onClearService(mClient);
189 }
190
Shraddha Basantwanif2aa2502024-04-08 11:01:57 +0000191 void disableThreadPool() {
192 mCanStartThreadPool = false;
193 }
194
Andy Hungda1fb072024-02-20 19:08:08 -0800195private:
196 std::mutex mSingleGetter;
197 std::mutex mMutex;
198 std::shared_ptr<std::condition_variable> mCvGetter GUARDED_BY(mMutex);
199 sp<IBinder> mBinder GUARDED_BY(mMutex);
200 sp<ServiceInterface> mLocalService GUARDED_BY(mMutex);
201 sp<ServiceInterface> mService GUARDED_BY(mMutex);
202 sp<Client> mClient GUARDED_BY(mMutex);
Shraddha Basantwanif2aa2502024-04-08 11:01:57 +0000203 std::atomic<bool> mCanStartThreadPool = true;
Andy Hungda1fb072024-02-20 19:08:08 -0800204};
205
206struct AudioFlingerTraits {
207 static void onServiceCreate(
208 const sp<IAudioFlinger>& af, const sp<AudioSystem::AudioFlingerClient>& afc) {
209 const int64_t token = IPCThreadState::self()->clearCallingIdentity();
210 af->registerClient(afc);
211 IPCThreadState::self()->restoreCallingIdentity(token);
212 }
213
214 static sp<IAudioFlinger> createServiceAdapter(
215 const sp<media::IAudioFlingerService>& aidlInterface) {
216 return sp<AudioFlingerClientAdapter>::make(aidlInterface);
217 }
218
219 static void onClearService(const sp<AudioSystem::AudioFlingerClient>& afc) {
220 afc->clearIoCache();
221 }
222
223 static constexpr const char* SERVICE_NAME = IAudioFlinger::DEFAULT_SERVICE_NAME;
224};
225
226[[clang::no_destroy]] static constinit ServiceHandler<IAudioFlinger,
227 AudioSystem::AudioFlingerClient, media::IAudioFlingerService,
228 AudioFlingerTraits> gAudioFlingerServiceHandler;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800229
Andy Hung264fa4c2024-02-21 15:52:12 -0800230sp<IAudioFlinger> AudioSystem::get_audio_flinger() {
Andy Hungda1fb072024-02-20 19:08:08 -0800231 return gAudioFlingerServiceHandler.getService();
Akshata Kadam1d486b72023-08-07 05:49:48 +0000232}
233
Andy Hungda1fb072024-02-20 19:08:08 -0800234sp<AudioSystem::AudioFlingerClient> AudioSystem::getAudioFlingerClient() {
235 return gAudioFlingerServiceHandler.getClient();
236}
237
238void AudioSystem::setAudioFlingerBinder(const sp<IBinder>& audioFlinger) {
239 if (audioFlinger->getInterfaceDescriptor() != media::IAudioFlingerService::descriptor) {
240 ALOGE("%s: received a binder of type %s",
241 __func__, String8(audioFlinger->getInterfaceDescriptor()).c_str());
242 return;
243 }
244 gAudioFlingerServiceHandler.setBinder(audioFlinger);
245}
246
247status_t AudioSystem::setLocalAudioFlinger(const sp<IAudioFlinger>& af) {
248 return gAudioFlingerServiceHandler.setLocalService(af);
Eric Laurent296fb132015-05-01 11:38:42 -0700249}
250
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800251sp<AudioIoDescriptor> AudioSystem::getIoDescriptor(audio_io_handle_t ioHandle) {
Eric Laurent296fb132015-05-01 11:38:42 -0700252 sp<AudioIoDescriptor> desc;
253 const sp<AudioFlingerClient> afc = getAudioFlingerClient();
254 if (afc != 0) {
255 desc = afc->getIoDescriptor(ioHandle);
256 }
257 return desc;
258}
259
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800260/* static */ status_t AudioSystem::checkAudioFlinger() {
Eric Laurent46291612013-07-18 14:38:44 -0700261 if (defaultServiceManager()->checkService(String16("media.audio_flinger")) != 0) {
262 return NO_ERROR;
263 }
264 return DEAD_OBJECT;
265}
266
Glenn Kasten4a8308b2016-04-18 14:10:01 -0700267// FIXME Declare in binder opcode order, similarly to IAudioFlinger.h and IAudioFlinger.cpp
268
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800269status_t AudioSystem::muteMicrophone(bool state) {
Andy Hung264fa4c2024-02-21 15:52:12 -0800270 const sp<IAudioFlinger> af = get_audio_flinger();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800271 if (af == 0) return PERMISSION_DENIED;
272 return af->setMicMute(state);
273}
274
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800275status_t AudioSystem::isMicrophoneMuted(bool* state) {
Andy Hung264fa4c2024-02-21 15:52:12 -0800276 const sp<IAudioFlinger> af = get_audio_flinger();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800277 if (af == 0) return PERMISSION_DENIED;
278 *state = af->getMicMute();
279 return NO_ERROR;
280}
281
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800282status_t AudioSystem::setMasterVolume(float value) {
Andy Hung264fa4c2024-02-21 15:52:12 -0800283 const sp<IAudioFlinger> af = get_audio_flinger();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800284 if (af == 0) return PERMISSION_DENIED;
285 af->setMasterVolume(value);
286 return NO_ERROR;
287}
288
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800289status_t AudioSystem::setMasterMute(bool mute) {
Andy Hung264fa4c2024-02-21 15:52:12 -0800290 const sp<IAudioFlinger> af = get_audio_flinger();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800291 if (af == 0) return PERMISSION_DENIED;
292 af->setMasterMute(mute);
293 return NO_ERROR;
294}
295
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800296status_t AudioSystem::getMasterVolume(float* volume) {
Andy Hung264fa4c2024-02-21 15:52:12 -0800297 const sp<IAudioFlinger> af = get_audio_flinger();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800298 if (af == 0) return PERMISSION_DENIED;
299 *volume = af->masterVolume();
300 return NO_ERROR;
301}
302
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800303status_t AudioSystem::getMasterMute(bool* mute) {
Andy Hung264fa4c2024-02-21 15:52:12 -0800304 const sp<IAudioFlinger> af = get_audio_flinger();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800305 if (af == 0) return PERMISSION_DENIED;
306 *mute = af->masterMute();
307 return NO_ERROR;
308}
309
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800310status_t AudioSystem::setStreamVolume(audio_stream_type_t stream, float value,
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800311 audio_io_handle_t output) {
Dima Zavinfce7a472011-04-19 22:30:36 -0700312 if (uint32_t(stream) >= AUDIO_STREAM_CNT) return BAD_VALUE;
Andy Hung264fa4c2024-02-21 15:52:12 -0800313 const sp<IAudioFlinger> af = get_audio_flinger();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800314 if (af == 0) return PERMISSION_DENIED;
Eric Laurentc2f1f072009-07-17 12:17:14 -0700315 af->setStreamVolume(stream, value, output);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800316 return NO_ERROR;
317}
318
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800319status_t AudioSystem::setStreamMute(audio_stream_type_t stream, bool mute) {
Dima Zavinfce7a472011-04-19 22:30:36 -0700320 if (uint32_t(stream) >= AUDIO_STREAM_CNT) return BAD_VALUE;
Andy Hung264fa4c2024-02-21 15:52:12 -0800321 const sp<IAudioFlinger> af = get_audio_flinger();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800322 if (af == 0) return PERMISSION_DENIED;
323 af->setStreamMute(stream, mute);
324 return NO_ERROR;
325}
326
Andy Hung6b137d12024-08-27 22:35:17 +0000327status_t AudioSystem::setPortsVolume(
328 const std::vector<audio_port_handle_t>& portIds, float volume, audio_io_handle_t output) {
329 const sp<IAudioFlinger> af = get_audio_flinger();
330 if (af == 0) return PERMISSION_DENIED;
331 std::vector<int32_t> portIdsAidl = VALUE_OR_RETURN_STATUS(
332 convertContainer<std::vector<int32_t>>(
333 portIds, legacy2aidl_audio_port_handle_t_int32_t));
334 int32_t outputAidl = VALUE_OR_RETURN_STATUS(legacy2aidl_audio_io_handle_t_int32_t(output));
335 af->setPortsVolume(portIdsAidl, volume, outputAidl);
336 return NO_ERROR;
337}
338
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800339status_t AudioSystem::setMode(audio_mode_t mode) {
Glenn Kasten930f4ca2012-01-06 16:47:31 -0800340 if (uint32_t(mode) >= AUDIO_MODE_CNT) return BAD_VALUE;
Andy Hung264fa4c2024-02-21 15:52:12 -0800341 const sp<IAudioFlinger> af = get_audio_flinger();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800342 if (af == 0) return PERMISSION_DENIED;
343 return af->setMode(mode);
344}
345
Mikhail Naganovb1ddbb02023-03-15 17:06:59 -0700346status_t AudioSystem::setSimulateDeviceConnections(bool enabled) {
Andy Hung264fa4c2024-02-21 15:52:12 -0800347 const sp<IAudioFlinger> af = get_audio_flinger();
Mikhail Naganovb1ddbb02023-03-15 17:06:59 -0700348 if (af == 0) return PERMISSION_DENIED;
349 return af->setSimulateDeviceConnections(enabled);
350}
351
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800352status_t AudioSystem::setParameters(audio_io_handle_t ioHandle, const String8& keyValuePairs) {
Andy Hung264fa4c2024-02-21 15:52:12 -0800353 const sp<IAudioFlinger> af = get_audio_flinger();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800354 if (af == 0) return PERMISSION_DENIED;
Eric Laurentc2f1f072009-07-17 12:17:14 -0700355 return af->setParameters(ioHandle, keyValuePairs);
356}
357
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800358String8 AudioSystem::getParameters(audio_io_handle_t ioHandle, const String8& keys) {
Andy Hung264fa4c2024-02-21 15:52:12 -0800359 const sp<IAudioFlinger> af = get_audio_flinger();
Eric Laurentc2f1f072009-07-17 12:17:14 -0700360 String8 result = String8("");
361 if (af == 0) return result;
362
363 result = af->getParameters(ioHandle, keys);
364 return result;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800365}
366
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800367status_t AudioSystem::setParameters(const String8& keyValuePairs) {
Glenn Kasten142f5192014-03-25 17:44:59 -0700368 return setParameters(AUDIO_IO_HANDLE_NONE, keyValuePairs);
Glenn Kastenc23885e2013-12-19 16:35:18 -0800369}
370
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800371String8 AudioSystem::getParameters(const String8& keys) {
Glenn Kasten142f5192014-03-25 17:44:59 -0700372 return getParameters(AUDIO_IO_HANDLE_NONE, keys);
Glenn Kastenc23885e2013-12-19 16:35:18 -0800373}
374
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800375// convert volume steps to natural log scale
376
377// change this value to change volume scaling
Andy Hungacd955a2024-02-16 17:14:36 -0800378constexpr float kdbPerStep = 0.5f;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800379// shouldn't need to touch these
Andy Hungacd955a2024-02-16 17:14:36 -0800380constexpr float kdBConvert = -kdbPerStep * 2.302585093f / 20.0f;
381constexpr float kdBConvertInverse = 1.0f / kdBConvert;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800382
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800383float AudioSystem::linearToLog(int volume) {
Andy Hungacd955a2024-02-16 17:14:36 -0800384 // float v = volume ? exp(float(100 - volume) * kdBConvert) : 0;
Steve Blockb8a80522011-12-20 16:23:08 +0000385 // ALOGD("linearToLog(%d)=%f", volume, v);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800386 // return v;
Andy Hungacd955a2024-02-16 17:14:36 -0800387 return volume ? exp(float(100 - volume) * kdBConvert) : 0;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800388}
389
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800390int AudioSystem::logToLinear(float volume) {
Andy Hungacd955a2024-02-16 17:14:36 -0800391 // int v = volume ? 100 - int(kdBConvertInverse * log(volume) + 0.5) : 0;
Steve Blockb8a80522011-12-20 16:23:08 +0000392 // ALOGD("logTolinear(%d)=%f", v, volume);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800393 // return v;
Andy Hungacd955a2024-02-16 17:14:36 -0800394 return volume ? 100 - int(kdBConvertInverse * log(volume) + 0.5) : 0;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800395}
396
Eric Laurent21da6472017-11-09 16:29:26 -0800397/* static */ size_t AudioSystem::calculateMinFrameCount(
398 uint32_t afLatencyMs, uint32_t afFrameCount, uint32_t afSampleRate,
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800399 uint32_t sampleRate, float speed /*, uint32_t notificationsPerBufferReq*/) {
Eric Laurent21da6472017-11-09 16:29:26 -0800400 // Ensure that buffer depth covers at least audio hardware latency
401 uint32_t minBufCount = afLatencyMs / ((1000 * afFrameCount) / afSampleRate);
402 if (minBufCount < 2) {
403 minBufCount = 2;
404 }
405#if 0
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800406 // The notificationsPerBufferReq parameter is not yet used for non-fast tracks,
407 // but keeping the code here to make it easier to add later.
408 if (minBufCount < notificationsPerBufferReq) {
409 minBufCount = notificationsPerBufferReq;
410 }
Eric Laurent21da6472017-11-09 16:29:26 -0800411#endif
412 ALOGV("calculateMinFrameCount afLatency %u afFrameCount %u afSampleRate %u "
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800413 "sampleRate %u speed %f minBufCount: %u" /*" notificationsPerBufferReq %u"*/,
414 afLatencyMs, afFrameCount, afSampleRate, sampleRate, speed, minBufCount
415 /*, notificationsPerBufferReq*/);
Eric Laurent21da6472017-11-09 16:29:26 -0800416 return minBufCount * sourceFramesNeededWithTimestretch(
417 sampleRate, afFrameCount, afSampleRate, speed);
418}
419
420
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800421status_t
422AudioSystem::getOutputSamplingRate(uint32_t* samplingRate, audio_stream_type_t streamType) {
Eric Laurentc2f1f072009-07-17 12:17:14 -0700423 audio_io_handle_t output;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800424
Dima Zavinfce7a472011-04-19 22:30:36 -0700425 if (streamType == AUDIO_STREAM_DEFAULT) {
426 streamType = AUDIO_STREAM_MUSIC;
Eric Laurentc2f1f072009-07-17 12:17:14 -0700427 }
428
Glenn Kastenfff6d712012-01-12 16:38:12 -0800429 output = getOutput(streamType);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700430 if (output == 0) {
431 return PERMISSION_DENIED;
432 }
433
Jean-Michel Trivib7f24b12014-06-11 10:05:30 -0700434 return getSamplingRate(output, samplingRate);
Eric Laurent1a9ed112012-03-20 18:36:01 -0700435}
436
Glenn Kasten2c073da2016-02-26 09:14:08 -0800437status_t AudioSystem::getSamplingRate(audio_io_handle_t ioHandle,
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800438 uint32_t* samplingRate) {
Andy Hung264fa4c2024-02-21 15:52:12 -0800439 const sp<IAudioFlinger> af = get_audio_flinger();
Eric Laurentf6778fd2014-11-18 17:26:58 -0800440 if (af == 0) return PERMISSION_DENIED;
Glenn Kasten2c073da2016-02-26 09:14:08 -0800441 sp<AudioIoDescriptor> desc = getIoDescriptor(ioHandle);
442 if (desc == 0) {
443 *samplingRate = af->sampleRate(ioHandle);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700444 } else {
Mikhail Naganov88536df2021-07-26 17:30:29 -0700445 *samplingRate = desc->getSamplingRate();
Eric Laurentc2f1f072009-07-17 12:17:14 -0700446 }
Glenn Kastenf94006c2014-01-08 08:56:06 -0800447 if (*samplingRate == 0) {
Glenn Kasten2c073da2016-02-26 09:14:08 -0800448 ALOGE("AudioSystem::getSamplingRate failed for ioHandle %d", ioHandle);
Glenn Kastenf94006c2014-01-08 08:56:06 -0800449 return BAD_VALUE;
450 }
Eric Laurentc2f1f072009-07-17 12:17:14 -0700451
Glenn Kasten2c073da2016-02-26 09:14:08 -0800452 ALOGV("getSamplingRate() ioHandle %d, sampling rate %u", ioHandle, *samplingRate);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700453
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800454 return NO_ERROR;
455}
456
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800457status_t AudioSystem::getOutputFrameCount(size_t* frameCount, audio_stream_type_t streamType) {
Eric Laurentc2f1f072009-07-17 12:17:14 -0700458 audio_io_handle_t output;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800459
Dima Zavinfce7a472011-04-19 22:30:36 -0700460 if (streamType == AUDIO_STREAM_DEFAULT) {
461 streamType = AUDIO_STREAM_MUSIC;
Eric Laurentc2f1f072009-07-17 12:17:14 -0700462 }
Eric Laurent48f7f5e2009-04-02 09:32:43 -0700463
Glenn Kastenfff6d712012-01-12 16:38:12 -0800464 output = getOutput(streamType);
Glenn Kasten142f5192014-03-25 17:44:59 -0700465 if (output == AUDIO_IO_HANDLE_NONE) {
Eric Laurentc2f1f072009-07-17 12:17:14 -0700466 return PERMISSION_DENIED;
467 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800468
Jean-Michel Trivib7f24b12014-06-11 10:05:30 -0700469 return getFrameCount(output, frameCount);
Eric Laurent1a9ed112012-03-20 18:36:01 -0700470}
471
Glenn Kasten2c073da2016-02-26 09:14:08 -0800472status_t AudioSystem::getFrameCount(audio_io_handle_t ioHandle,
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800473 size_t* frameCount) {
Andy Hung264fa4c2024-02-21 15:52:12 -0800474 const sp<IAudioFlinger> af = get_audio_flinger();
Eric Laurentf6778fd2014-11-18 17:26:58 -0800475 if (af == 0) return PERMISSION_DENIED;
Glenn Kasten2c073da2016-02-26 09:14:08 -0800476 sp<AudioIoDescriptor> desc = getIoDescriptor(ioHandle);
477 if (desc == 0) {
478 *frameCount = af->frameCount(ioHandle);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700479 } else {
Mikhail Naganov88536df2021-07-26 17:30:29 -0700480 *frameCount = desc->getFrameCount();
Eric Laurentc2f1f072009-07-17 12:17:14 -0700481 }
Glenn Kastenf94006c2014-01-08 08:56:06 -0800482 if (*frameCount == 0) {
Glenn Kasten2c073da2016-02-26 09:14:08 -0800483 ALOGE("AudioSystem::getFrameCount failed for ioHandle %d", ioHandle);
Glenn Kastenf94006c2014-01-08 08:56:06 -0800484 return BAD_VALUE;
485 }
Eric Laurentc2f1f072009-07-17 12:17:14 -0700486
Glenn Kasten2c073da2016-02-26 09:14:08 -0800487 ALOGV("getFrameCount() ioHandle %d, frameCount %zu", ioHandle, *frameCount);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700488
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800489 return NO_ERROR;
490}
491
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800492status_t AudioSystem::getOutputLatency(uint32_t* latency, audio_stream_type_t streamType) {
Eric Laurentc2f1f072009-07-17 12:17:14 -0700493 audio_io_handle_t output;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800494
Dima Zavinfce7a472011-04-19 22:30:36 -0700495 if (streamType == AUDIO_STREAM_DEFAULT) {
496 streamType = AUDIO_STREAM_MUSIC;
Eric Laurentc2f1f072009-07-17 12:17:14 -0700497 }
Eric Laurent48f7f5e2009-04-02 09:32:43 -0700498
Glenn Kastenfff6d712012-01-12 16:38:12 -0800499 output = getOutput(streamType);
Glenn Kasten142f5192014-03-25 17:44:59 -0700500 if (output == AUDIO_IO_HANDLE_NONE) {
Eric Laurentc2f1f072009-07-17 12:17:14 -0700501 return PERMISSION_DENIED;
502 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800503
Glenn Kasten241618f2014-03-25 17:48:57 -0700504 return getLatency(output, latency);
Eric Laurent1a9ed112012-03-20 18:36:01 -0700505}
506
507status_t AudioSystem::getLatency(audio_io_handle_t output,
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800508 uint32_t* latency) {
Andy Hung264fa4c2024-02-21 15:52:12 -0800509 const sp<IAudioFlinger> af = get_audio_flinger();
Eric Laurentf6778fd2014-11-18 17:26:58 -0800510 if (af == 0) return PERMISSION_DENIED;
Eric Laurent296fb132015-05-01 11:38:42 -0700511 sp<AudioIoDescriptor> outputDesc = getIoDescriptor(output);
Eric Laurent73e26b62015-04-27 16:55:58 -0700512 if (outputDesc == 0) {
Eric Laurentc2f1f072009-07-17 12:17:14 -0700513 *latency = af->latency(output);
514 } else {
Mikhail Naganov88536df2021-07-26 17:30:29 -0700515 *latency = outputDesc->getLatency();
Eric Laurentc2f1f072009-07-17 12:17:14 -0700516 }
517
Glenn Kasten241618f2014-03-25 17:48:57 -0700518 ALOGV("getLatency() output %d, latency %d", output, *latency);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700519
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800520 return NO_ERROR;
521}
522
Glenn Kastendd8104c2012-07-02 12:42:44 -0700523status_t AudioSystem::getInputBufferSize(uint32_t sampleRate, audio_format_t format,
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800524 audio_channel_mask_t channelMask, size_t* buffSize) {
Eric Laurent296fb132015-05-01 11:38:42 -0700525 const sp<AudioFlingerClient> afc = getAudioFlingerClient();
526 if (afc == 0) {
527 return NO_INIT;
528 }
529 return afc->getInputBufferSize(sampleRate, format, channelMask, buffSize);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800530}
531
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800532status_t AudioSystem::setVoiceVolume(float value) {
Andy Hung264fa4c2024-02-21 15:52:12 -0800533 const sp<IAudioFlinger> af = get_audio_flinger();
Eric Laurentf0ee6f42009-10-21 08:14:22 -0700534 if (af == 0) return PERMISSION_DENIED;
535 return af->setVoiceVolume(value);
536}
537
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800538status_t AudioSystem::getRenderPosition(audio_io_handle_t output, uint32_t* halFrames,
539 uint32_t* dspFrames) {
Andy Hung264fa4c2024-02-21 15:52:12 -0800540 const sp<IAudioFlinger> af = get_audio_flinger();
Eric Laurent342e9cf2010-01-19 17:37:09 -0800541 if (af == 0) return PERMISSION_DENIED;
542
Richard Fitzgeraldad3af332013-03-25 16:54:37 +0000543 return af->getRenderPosition(halFrames, dspFrames, output);
Eric Laurent342e9cf2010-01-19 17:37:09 -0800544}
545
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800546uint32_t AudioSystem::getInputFramesLost(audio_io_handle_t ioHandle) {
Andy Hung264fa4c2024-02-21 15:52:12 -0800547 const sp<IAudioFlinger> af = get_audio_flinger();
Glenn Kasten5f972c02014-01-13 09:59:31 -0800548 uint32_t result = 0;
Eric Laurent05bca2f2010-02-26 02:47:27 -0800549 if (af == 0) return result;
Glenn Kasten142f5192014-03-25 17:44:59 -0700550 if (ioHandle == AUDIO_IO_HANDLE_NONE) return result;
Eric Laurent05bca2f2010-02-26 02:47:27 -0800551
552 result = af->getInputFramesLost(ioHandle);
553 return result;
554}
555
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800556audio_unique_id_t AudioSystem::newAudioUniqueId(audio_unique_id_use_t use) {
Mikhail Naganov2996f672019-04-18 12:29:59 -0700557 // Must not use AF as IDs will re-roll on audioserver restart, b/130369529.
Andy Hung264fa4c2024-02-21 15:52:12 -0800558 const sp<IAudioFlinger> af = get_audio_flinger();
Eric Laurentde3f8392014-07-27 18:38:22 -0700559 if (af == 0) return AUDIO_UNIQUE_ID_ALLOCATE;
Glenn Kasteneeecb982016-02-26 10:44:04 -0800560 return af->newAudioUniqueId(use);
Eric Laurentbe916aa2010-06-01 23:49:17 -0700561}
562
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800563void AudioSystem::acquireAudioSessionId(audio_session_t audioSession, pid_t pid, uid_t uid) {
Andy Hung264fa4c2024-02-21 15:52:12 -0800564 const sp<IAudioFlinger> af = get_audio_flinger();
Marco Nelissen3a34bef2011-08-02 13:33:41 -0700565 if (af != 0) {
Andy Hung8b0bfd92019-12-23 13:11:11 -0800566 af->acquireAudioSessionId(audioSession, pid, uid);
Marco Nelissen3a34bef2011-08-02 13:33:41 -0700567 }
568}
569
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800570void AudioSystem::releaseAudioSessionId(audio_session_t audioSession, pid_t pid) {
Andy Hung264fa4c2024-02-21 15:52:12 -0800571 const sp<IAudioFlinger> af = get_audio_flinger();
Marco Nelissen3a34bef2011-08-02 13:33:41 -0700572 if (af != 0) {
Marco Nelissend457c972014-02-11 08:47:07 -0800573 af->releaseAudioSessionId(audioSession, pid);
Marco Nelissen3a34bef2011-08-02 13:33:41 -0700574 }
575}
576
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800577audio_hw_sync_t AudioSystem::getAudioHwSyncForSession(audio_session_t sessionId) {
Andy Hung264fa4c2024-02-21 15:52:12 -0800578 const sp<IAudioFlinger> af = get_audio_flinger();
Eric Laurent93c3d412014-08-01 14:48:35 -0700579 if (af == 0) return AUDIO_HW_SYNC_INVALID;
580 return af->getAudioHwSyncForSession(sessionId);
581}
582
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800583status_t AudioSystem::systemReady() {
Andy Hung264fa4c2024-02-21 15:52:12 -0800584 const sp<IAudioFlinger> af = get_audio_flinger();
Eric Laurent72e3f392015-05-20 14:43:50 -0700585 if (af == 0) return NO_INIT;
586 return af->systemReady();
587}
588
Eric Laurentd66d7a12021-07-13 13:35:32 +0200589status_t AudioSystem::audioPolicyReady() {
Andy Hung264fa4c2024-02-21 15:52:12 -0800590 const sp<IAudioFlinger> af = get_audio_flinger();
Eric Laurentd66d7a12021-07-13 13:35:32 +0200591 if (af == 0) return NO_INIT;
592 return af->audioPolicyReady();
593}
594
Glenn Kasten4a8308b2016-04-18 14:10:01 -0700595status_t AudioSystem::getFrameCountHAL(audio_io_handle_t ioHandle,
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800596 size_t* frameCount) {
Andy Hung264fa4c2024-02-21 15:52:12 -0800597 const sp<IAudioFlinger> af = get_audio_flinger();
Glenn Kasten4a8308b2016-04-18 14:10:01 -0700598 if (af == 0) return PERMISSION_DENIED;
599 sp<AudioIoDescriptor> desc = getIoDescriptor(ioHandle);
600 if (desc == 0) {
601 *frameCount = af->frameCountHAL(ioHandle);
602 } else {
Mikhail Naganov88536df2021-07-26 17:30:29 -0700603 *frameCount = desc->getFrameCountHAL();
Glenn Kasten4a8308b2016-04-18 14:10:01 -0700604 }
605 if (*frameCount == 0) {
606 ALOGE("AudioSystem::getFrameCountHAL failed for ioHandle %d", ioHandle);
607 return BAD_VALUE;
608 }
609
610 ALOGV("getFrameCountHAL() ioHandle %d, frameCount %zu", ioHandle, *frameCount);
611
612 return NO_ERROR;
613}
614
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800615// ---------------------------------------------------------------------------
616
Eric Laurent73e26b62015-04-27 16:55:58 -0700617
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800618void AudioSystem::AudioFlingerClient::clearIoCache() {
Andy Hungacd955a2024-02-16 17:14:36 -0800619 std::lock_guard _l(mMutex);
Eric Laurent73e26b62015-04-27 16:55:58 -0700620 mIoDescriptors.clear();
621 mInBuffSize = 0;
622 mInSamplingRate = 0;
623 mInFormat = AUDIO_FORMAT_DEFAULT;
624 mInChannelMask = AUDIO_CHANNEL_NONE;
625}
626
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800627void AudioSystem::AudioFlingerClient::binderDied(const wp<IBinder>& who __unused) {
Andy Hungda1fb072024-02-20 19:08:08 -0800628 gAudioFlingerServiceHandler.clearService();
Ytai Ben-Tsvi000c3e42020-01-09 15:26:40 -0800629 reportError(DEAD_OBJECT);
630
Steve Block5ff1dd52012-01-05 23:22:43 +0000631 ALOGW("AudioFlinger server died!");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800632}
633
Ytai Ben-Tsvi10dc0a62020-09-18 11:31:55 -0700634Status AudioSystem::AudioFlingerClient::ioConfigChanged(
635 media::AudioIoConfigEvent _event,
636 const media::AudioIoDescriptor& _ioDesc) {
Mikhail Naganov88536df2021-07-26 17:30:29 -0700637 audio_io_config_event_t event = VALUE_OR_RETURN_BINDER_STATUS(
638 aidl2legacy_AudioIoConfigEvent_audio_io_config_event_t(_event));
Ytai Ben-Tsvi10dc0a62020-09-18 11:31:55 -0700639 sp<AudioIoDescriptor> ioDesc(
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800640 VALUE_OR_RETURN_BINDER_STATUS(
641 aidl2legacy_AudioIoDescriptor_AudioIoDescriptor(_ioDesc)));
Ytai Ben-Tsvi10dc0a62020-09-18 11:31:55 -0700642
Steve Block3856b092011-10-20 11:56:00 +0100643 ALOGV("ioConfigChanged() event %d", event);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700644
Mikhail Naganov88536df2021-07-26 17:30:29 -0700645 if (ioDesc->getIoHandle() == AUDIO_IO_HANDLE_NONE) return Status::ok();
Eric Laurentc2f1f072009-07-17 12:17:14 -0700646
Eric Laurent296fb132015-05-01 11:38:42 -0700647 audio_port_handle_t deviceId = AUDIO_PORT_HANDLE_NONE;
Eric Laurent09f1ed22019-04-24 17:45:17 -0700648 std::vector<sp<AudioDeviceCallback>> callbacksToCall;
Eric Laurent296fb132015-05-01 11:38:42 -0700649 {
Andy Hungacd955a2024-02-16 17:14:36 -0800650 std::lock_guard _l(mMutex);
Eric Laurent09f1ed22019-04-24 17:45:17 -0700651 auto callbacks = std::map<audio_port_handle_t, wp<AudioDeviceCallback>>();
Eric Laurent296fb132015-05-01 11:38:42 -0700652
653 switch (event) {
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800654 case AUDIO_OUTPUT_OPENED:
655 case AUDIO_OUTPUT_REGISTERED:
656 case AUDIO_INPUT_OPENED:
657 case AUDIO_INPUT_REGISTERED: {
Andy Hungd211ce02024-02-16 15:34:07 -0800658 if (sp<AudioIoDescriptor> oldDesc = getIoDescriptor_l(ioDesc->getIoHandle())) {
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800659 deviceId = oldDesc->getDeviceId();
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800660 }
Andy Hungd211ce02024-02-16 15:34:07 -0800661 mIoDescriptors[ioDesc->getIoHandle()] = ioDesc;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800662
663 if (ioDesc->getDeviceId() != AUDIO_PORT_HANDLE_NONE) {
664 deviceId = ioDesc->getDeviceId();
665 if (event == AUDIO_OUTPUT_OPENED || event == AUDIO_INPUT_OPENED) {
Mikhail Naganov88536df2021-07-26 17:30:29 -0700666 auto it = mAudioDeviceCallbacks.find(ioDesc->getIoHandle());
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800667 if (it != mAudioDeviceCallbacks.end()) {
668 callbacks = it->second;
669 }
670 }
671 }
Mikhail Naganov88536df2021-07-26 17:30:29 -0700672 ALOGV("ioConfigChanged() new %s %s %s",
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800673 event == AUDIO_OUTPUT_OPENED || event == AUDIO_OUTPUT_REGISTERED ?
674 "output" : "input",
675 event == AUDIO_OUTPUT_OPENED || event == AUDIO_INPUT_OPENED ?
676 "opened" : "registered",
Mikhail Naganov88536df2021-07-26 17:30:29 -0700677 ioDesc->toDebugString().c_str());
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800678 }
679 break;
680 case AUDIO_OUTPUT_CLOSED:
681 case AUDIO_INPUT_CLOSED: {
Mikhail Naganov88536df2021-07-26 17:30:29 -0700682 if (getIoDescriptor_l(ioDesc->getIoHandle()) == 0) {
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800683 ALOGW("ioConfigChanged() closing unknown %s %d",
Mikhail Naganov88536df2021-07-26 17:30:29 -0700684 event == AUDIO_OUTPUT_CLOSED ? "output" : "input", ioDesc->getIoHandle());
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800685 break;
686 }
687 ALOGV("ioConfigChanged() %s %d closed",
Mikhail Naganov88536df2021-07-26 17:30:29 -0700688 event == AUDIO_OUTPUT_CLOSED ? "output" : "input", ioDesc->getIoHandle());
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800689
Andy Hungd211ce02024-02-16 15:34:07 -0800690 mIoDescriptors.erase(ioDesc->getIoHandle());
Mikhail Naganov88536df2021-07-26 17:30:29 -0700691 mAudioDeviceCallbacks.erase(ioDesc->getIoHandle());
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800692 }
693 break;
694
695 case AUDIO_OUTPUT_CONFIG_CHANGED:
696 case AUDIO_INPUT_CONFIG_CHANGED: {
Mikhail Naganov88536df2021-07-26 17:30:29 -0700697 sp<AudioIoDescriptor> oldDesc = getIoDescriptor_l(ioDesc->getIoHandle());
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800698 if (oldDesc == 0) {
699 ALOGW("ioConfigChanged() modifying unknown %s! %d",
700 event == AUDIO_OUTPUT_CONFIG_CHANGED ? "output" : "input",
Mikhail Naganov88536df2021-07-26 17:30:29 -0700701 ioDesc->getIoHandle());
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800702 break;
703 }
704
Eric Laurent7c1ec5f2015-07-09 14:52:47 -0700705 deviceId = oldDesc->getDeviceId();
Andy Hungd211ce02024-02-16 15:34:07 -0800706 mIoDescriptors[ioDesc->getIoHandle()] = ioDesc;
Eric Laurent296fb132015-05-01 11:38:42 -0700707
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800708 if (deviceId != ioDesc->getDeviceId()) {
709 deviceId = ioDesc->getDeviceId();
Mikhail Naganov88536df2021-07-26 17:30:29 -0700710 auto it = mAudioDeviceCallbacks.find(ioDesc->getIoHandle());
Eric Laurent09f1ed22019-04-24 17:45:17 -0700711 if (it != mAudioDeviceCallbacks.end()) {
712 callbacks = it->second;
Francois Gaffie24a9fb02019-01-18 17:51:34 +0100713 }
714 }
Mikhail Naganov88536df2021-07-26 17:30:29 -0700715 ALOGV("ioConfigChanged() new config for %s %s",
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800716 event == AUDIO_OUTPUT_CONFIG_CHANGED ? "output" : "input",
Mikhail Naganov88536df2021-07-26 17:30:29 -0700717 ioDesc->toDebugString().c_str());
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800718
Eric Laurent296fb132015-05-01 11:38:42 -0700719 }
Eric Laurent296fb132015-05-01 11:38:42 -0700720 break;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800721 case AUDIO_CLIENT_STARTED: {
Mikhail Naganov88536df2021-07-26 17:30:29 -0700722 sp<AudioIoDescriptor> oldDesc = getIoDescriptor_l(ioDesc->getIoHandle());
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800723 if (oldDesc == 0) {
Mikhail Naganov88536df2021-07-26 17:30:29 -0700724 ALOGW("ioConfigChanged() start client on unknown io! %d",
725 ioDesc->getIoHandle());
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800726 break;
727 }
728 ALOGV("ioConfigChanged() AUDIO_CLIENT_STARTED io %d port %d num callbacks %zu",
Mikhail Naganov88536df2021-07-26 17:30:29 -0700729 ioDesc->getIoHandle(), ioDesc->getPortId(), mAudioDeviceCallbacks.size());
730 oldDesc->setPatch(ioDesc->getPatch());
731 auto it = mAudioDeviceCallbacks.find(ioDesc->getIoHandle());
Eric Laurent09f1ed22019-04-24 17:45:17 -0700732 if (it != mAudioDeviceCallbacks.end()) {
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800733 auto cbks = it->second;
Mikhail Naganov88536df2021-07-26 17:30:29 -0700734 auto it2 = cbks.find(ioDesc->getPortId());
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800735 if (it2 != cbks.end()) {
Mikhail Naganov88536df2021-07-26 17:30:29 -0700736 callbacks.emplace(ioDesc->getPortId(), it2->second);
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800737 deviceId = oldDesc->getDeviceId();
738 }
Eric Laurent09f1ed22019-04-24 17:45:17 -0700739 }
Eric Laurent296fb132015-05-01 11:38:42 -0700740 }
Eric Laurent09f1ed22019-04-24 17:45:17 -0700741 break;
Eric Laurent09f1ed22019-04-24 17:45:17 -0700742 }
743
744 for (auto wpCbk : callbacks) {
745 sp<AudioDeviceCallback> spCbk = wpCbk.second.promote();
746 if (spCbk != nullptr) {
747 callbacksToCall.push_back(spCbk);
748 }
Eric Laurentad2e7b92017-09-14 20:06:42 -0700749 }
Eric Laurent4463ff52019-02-07 13:56:09 -0800750 }
751
Andy Hungacd955a2024-02-16 17:14:36 -0800752 // Callbacks must be called without mMutex held. May lead to dead lock if calling for
753 // example getRoutedDevice that updates the device and tries to acquire mMutex.
Eric Laurent09f1ed22019-04-24 17:45:17 -0700754 for (auto cb : callbacksToCall) {
Mikhail Naganov88536df2021-07-26 17:30:29 -0700755 // If callbacksToCall is not empty, it implies ioDesc->getIoHandle() and deviceId are valid
756 cb->onAudioDeviceUpdate(ioDesc->getIoHandle(), deviceId);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700757 }
Ytai Ben-Tsvi10dc0a62020-09-18 11:31:55 -0700758
759 return Status::ok();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800760}
761
Eric Laurent076e7c72022-05-03 18:12:28 +0200762Status AudioSystem::AudioFlingerClient::onSupportedLatencyModesChanged(
Mikhail Naganovf53e1822022-12-18 02:48:14 +0000763 int output, const std::vector<media::audio::common::AudioLatencyMode>& latencyModes) {
Eric Laurent076e7c72022-05-03 18:12:28 +0200764 audio_io_handle_t outputLegacy = VALUE_OR_RETURN_BINDER_STATUS(
765 aidl2legacy_int32_t_audio_io_handle_t(output));
766 std::vector<audio_latency_mode_t> modesLegacy = VALUE_OR_RETURN_BINDER_STATUS(
767 convertContainer<std::vector<audio_latency_mode_t>>(
Mikhail Naganovf53e1822022-12-18 02:48:14 +0000768 latencyModes, aidl2legacy_AudioLatencyMode_audio_latency_mode_t));
Eric Laurent076e7c72022-05-03 18:12:28 +0200769
770 std::vector<sp<SupportedLatencyModesCallback>> callbacks;
771 {
Andy Hungacd955a2024-02-16 17:14:36 -0800772 std::lock_guard _l(mMutex);
Eric Laurent076e7c72022-05-03 18:12:28 +0200773 for (auto callback : mSupportedLatencyModesCallbacks) {
774 if (auto ref = callback.promote(); ref != nullptr) {
775 callbacks.push_back(ref);
776 }
777 }
778 }
779 for (const auto& callback : callbacks) {
780 callback->onSupportedLatencyModesChanged(outputLegacy, modesLegacy);
781 }
782
783 return Status::ok();
784}
785
Eric Laurent73e26b62015-04-27 16:55:58 -0700786status_t AudioSystem::AudioFlingerClient::getInputBufferSize(
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800787 uint32_t sampleRate, audio_format_t format,
788 audio_channel_mask_t channelMask, size_t* buffSize) {
Andy Hung264fa4c2024-02-21 15:52:12 -0800789 const sp<IAudioFlinger> af = get_audio_flinger();
Eric Laurent73e26b62015-04-27 16:55:58 -0700790 if (af == 0) {
791 return PERMISSION_DENIED;
792 }
Andy Hungacd955a2024-02-16 17:14:36 -0800793 std::lock_guard _l(mMutex);
Eric Laurent73e26b62015-04-27 16:55:58 -0700794 // Do we have a stale mInBuffSize or are we requesting the input buffer size for new values
795 if ((mInBuffSize == 0) || (sampleRate != mInSamplingRate) || (format != mInFormat)
796 || (channelMask != mInChannelMask)) {
797 size_t inBuffSize = af->getInputBufferSize(sampleRate, format, channelMask);
798 if (inBuffSize == 0) {
Glenn Kasten49f36ba2017-12-06 13:02:02 -0800799 ALOGE("AudioSystem::getInputBufferSize failed sampleRate %d format %#x channelMask %#x",
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800800 sampleRate, format, channelMask);
Eric Laurent73e26b62015-04-27 16:55:58 -0700801 return BAD_VALUE;
802 }
803 // A benign race is possible here: we could overwrite a fresher cache entry
804 // save the request params
805 mInSamplingRate = sampleRate;
806 mInFormat = format;
807 mInChannelMask = channelMask;
808
809 mInBuffSize = inBuffSize;
810 }
811
812 *buffSize = mInBuffSize;
813
814 return NO_ERROR;
815}
816
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800817sp<AudioIoDescriptor>
818AudioSystem::AudioFlingerClient::getIoDescriptor_l(audio_io_handle_t ioHandle) {
Andy Hungd211ce02024-02-16 15:34:07 -0800819 if (const auto it = mIoDescriptors.find(ioHandle);
820 it != mIoDescriptors.end()) {
821 return it->second;
Eric Laurent73e26b62015-04-27 16:55:58 -0700822 }
Andy Hungd211ce02024-02-16 15:34:07 -0800823 return {};
Eric Laurent73e26b62015-04-27 16:55:58 -0700824}
825
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800826sp<AudioIoDescriptor> AudioSystem::AudioFlingerClient::getIoDescriptor(audio_io_handle_t ioHandle) {
Andy Hungacd955a2024-02-16 17:14:36 -0800827 std::lock_guard _l(mMutex);
Praveen Chavan49fdeaf2015-09-29 02:25:47 -0700828 return getIoDescriptor_l(ioHandle);
829}
830
Eric Laurent296fb132015-05-01 11:38:42 -0700831status_t AudioSystem::AudioFlingerClient::addAudioDeviceCallback(
Eric Laurent09f1ed22019-04-24 17:45:17 -0700832 const wp<AudioDeviceCallback>& callback, audio_io_handle_t audioIo,
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800833 audio_port_handle_t portId) {
Eric Laurent09f1ed22019-04-24 17:45:17 -0700834 ALOGV("%s audioIo %d portId %d", __func__, audioIo, portId);
Andy Hungacd955a2024-02-16 17:14:36 -0800835 std::lock_guard _l(mMutex);
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800836 auto& callbacks = mAudioDeviceCallbacks.emplace(
837 audioIo,
838 std::map<audio_port_handle_t, wp<AudioDeviceCallback>>()).first->second;
Eric Laurent09f1ed22019-04-24 17:45:17 -0700839 auto result = callbacks.try_emplace(portId, callback);
840 if (!result.second) {
841 return INVALID_OPERATION;
Eric Laurent296fb132015-05-01 11:38:42 -0700842 }
Eric Laurent296fb132015-05-01 11:38:42 -0700843 return NO_ERROR;
844}
845
846status_t AudioSystem::AudioFlingerClient::removeAudioDeviceCallback(
Eric Laurent09f1ed22019-04-24 17:45:17 -0700847 const wp<AudioDeviceCallback>& callback __unused, audio_io_handle_t audioIo,
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800848 audio_port_handle_t portId) {
Eric Laurent09f1ed22019-04-24 17:45:17 -0700849 ALOGV("%s audioIo %d portId %d", __func__, audioIo, portId);
Andy Hungacd955a2024-02-16 17:14:36 -0800850 std::lock_guard _l(mMutex);
Eric Laurent09f1ed22019-04-24 17:45:17 -0700851 auto it = mAudioDeviceCallbacks.find(audioIo);
852 if (it == mAudioDeviceCallbacks.end()) {
Eric Laurent296fb132015-05-01 11:38:42 -0700853 return INVALID_OPERATION;
854 }
Eric Laurent09f1ed22019-04-24 17:45:17 -0700855 if (it->second.erase(portId) == 0) {
Eric Laurent296fb132015-05-01 11:38:42 -0700856 return INVALID_OPERATION;
857 }
Eric Laurent09f1ed22019-04-24 17:45:17 -0700858 if (it->second.size() == 0) {
859 mAudioDeviceCallbacks.erase(audioIo);
Eric Laurent296fb132015-05-01 11:38:42 -0700860 }
861 return NO_ERROR;
862}
863
Eric Laurent076e7c72022-05-03 18:12:28 +0200864status_t AudioSystem::AudioFlingerClient::addSupportedLatencyModesCallback(
865 const sp<SupportedLatencyModesCallback>& callback) {
Andy Hungacd955a2024-02-16 17:14:36 -0800866 std::lock_guard _l(mMutex);
Eric Laurent076e7c72022-05-03 18:12:28 +0200867 if (std::find(mSupportedLatencyModesCallbacks.begin(),
868 mSupportedLatencyModesCallbacks.end(),
869 callback) != mSupportedLatencyModesCallbacks.end()) {
870 return INVALID_OPERATION;
871 }
872 mSupportedLatencyModesCallbacks.push_back(callback);
873 return NO_ERROR;
874}
875
876status_t AudioSystem::AudioFlingerClient::removeSupportedLatencyModesCallback(
877 const sp<SupportedLatencyModesCallback>& callback) {
Andy Hungacd955a2024-02-16 17:14:36 -0800878 std::lock_guard _l(mMutex);
Eric Laurent076e7c72022-05-03 18:12:28 +0200879 auto it = std::find(mSupportedLatencyModesCallbacks.begin(),
880 mSupportedLatencyModesCallbacks.end(),
881 callback);
882 if (it == mSupportedLatencyModesCallbacks.end()) {
883 return INVALID_OPERATION;
884 }
885 mSupportedLatencyModesCallbacks.erase(it);
886 return NO_ERROR;
887}
888
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800889/* static */ uintptr_t AudioSystem::addErrorCallback(audio_error_callback cb) {
Andy Hungacd955a2024-02-16 17:14:36 -0800890 std::lock_guard _l(gErrorCallbacksMutex);
Ytai Ben-Tsvi000c3e42020-01-09 15:26:40 -0800891 gAudioErrorCallbacks.insert(cb);
892 return reinterpret_cast<uintptr_t>(cb);
893}
894
895/* static */ void AudioSystem::removeErrorCallback(uintptr_t cb) {
Andy Hungacd955a2024-02-16 17:14:36 -0800896 std::lock_guard _l(gErrorCallbacksMutex);
Ytai Ben-Tsvi000c3e42020-01-09 15:26:40 -0800897 gAudioErrorCallbacks.erase(reinterpret_cast<audio_error_callback>(cb));
898}
899
900/* static */ void AudioSystem::reportError(status_t err) {
Andy Hungacd955a2024-02-16 17:14:36 -0800901 std::lock_guard _l(gErrorCallbacksMutex);
Ytai Ben-Tsvi000c3e42020-01-09 15:26:40 -0800902 for (auto callback : gAudioErrorCallbacks) {
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800903 callback(err);
Ytai Ben-Tsvi000c3e42020-01-09 15:26:40 -0800904 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800905}
906
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800907/*static*/ void AudioSystem::setDynPolicyCallback(dynamic_policy_callback cb) {
Andy Hungacd955a2024-02-16 17:14:36 -0800908 std::lock_guard _l(gMutex);
Jean-Michel Trivif613d422015-04-23 18:41:29 -0700909 gDynPolicyCallback = cb;
910}
911
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800912/*static*/ void AudioSystem::setRecordConfigCallback(record_config_callback cb) {
Andy Hungacd955a2024-02-16 17:14:36 -0800913 std::lock_guard _l(gMutex);
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -0800914 gRecordConfigCallback = cb;
915}
916
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800917/*static*/ void AudioSystem::setRoutingCallback(routing_callback cb) {
Andy Hungacd955a2024-02-16 17:14:36 -0800918 std::lock_guard _l(gMutex);
Jean-Michel Trivi9a6b9ad2020-10-22 16:46:43 -0700919 gRoutingCallback = cb;
920}
921
Jean-Michel Trivi78f2b302022-04-15 18:18:41 +0000922/*static*/ void AudioSystem::setVolInitReqCallback(vol_range_init_req_callback cb) {
Andy Hungacd955a2024-02-16 17:14:36 -0800923 std::lock_guard _l(gMutex);
Jean-Michel Trivi78f2b302022-04-15 18:18:41 +0000924 gVolRangeInitReqCallback = cb;
925}
926
Andy Hung88e71732024-02-21 16:44:27 -0800927struct AudioPolicyTraits {
928 static void onServiceCreate(const sp<IAudioPolicyService>& ap,
929 const sp<AudioSystem::AudioPolicyServiceClient>& apc) {
930 const int64_t token = IPCThreadState::self()->clearCallingIdentity();
Eric Laurent0ebd5f92014-11-19 19:04:52 -0800931 ap->registerClient(apc);
François Gaffie24437602018-04-23 15:08:59 +0200932 ap->setAudioPortCallbacksEnabled(apc->isAudioPortCbEnabled());
François Gaffiecfe17322018-11-07 13:41:29 +0100933 ap->setAudioVolumeGroupCallbacksEnabled(apc->isAudioVolumeGroupCbEnabled());
François Gaffie24437602018-04-23 15:08:59 +0200934 IPCThreadState::self()->restoreCallingIdentity(token);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700935 }
Glenn Kastend2d089f2014-11-05 11:48:12 -0800936
Andy Hung88e71732024-02-21 16:44:27 -0800937 static void onClearService(const sp<AudioSystem::AudioPolicyServiceClient>&) {}
938
939 static constexpr const char *SERVICE_NAME = "media.audio_policy";
940};
941
942[[clang::no_destroy]] static constinit ServiceHandler<IAudioPolicyService,
943 AudioSystem::AudioPolicyServiceClient, IAudioPolicyService,
944 AudioPolicyTraits> gAudioPolicyServiceHandler;
945
946status_t AudioSystem::setLocalAudioPolicyService(const sp<IAudioPolicyService>& aps) {
947 return gAudioPolicyServiceHandler.setLocalService(aps);
948}
949
950sp<IAudioPolicyService> AudioSystem::get_audio_policy_service() {
951 return gAudioPolicyServiceHandler.getService();
Eric Laurentc2f1f072009-07-17 12:17:14 -0700952}
953
Mikhail Naganovec3d5792022-05-06 00:19:55 +0000954void AudioSystem::clearAudioPolicyService() {
Andy Hung88e71732024-02-21 16:44:27 -0800955 gAudioPolicyServiceHandler.clearService();
Mikhail Naganovec3d5792022-05-06 00:19:55 +0000956}
957
Shraddha Basantwanif2aa2502024-04-08 11:01:57 +0000958void AudioSystem::disableThreadPool() {
959 gAudioFlingerServiceHandler.disableThreadPool();
960 gAudioPolicyServiceHandler.disableThreadPool();
961}
962
Glenn Kastenfb1fdc92013-07-10 17:03:19 -0700963// ---------------------------------------------------------------------------
964
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800965void AudioSystem::onNewAudioModulesAvailable() {
Andy Hung264fa4c2024-02-21 15:52:12 -0800966 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Mikhail Naganov88b30d22020-03-09 19:43:13 +0000967 if (aps == 0) return;
968 aps->onNewAudioModulesAvailable();
969}
970
Nathalie Le Clair88fa2752021-11-23 13:03:41 +0100971status_t AudioSystem::setDeviceConnectionState(audio_policy_dev_state_t state,
972 const android::media::audio::common::AudioPort& port,
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800973 audio_format_t encodedFormat) {
Andy Hung264fa4c2024-02-21 15:52:12 -0800974 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Eric Laurent71b63e32011-09-02 14:20:56 -0700975
Eric Laurentc2f1f072009-07-17 12:17:14 -0700976 if (aps == 0) return PERMISSION_DENIED;
977
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800978 return statusTFromBinderStatus(
979 aps->setDeviceConnectionState(
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800980 VALUE_OR_RETURN_STATUS(
981 legacy2aidl_audio_policy_dev_state_t_AudioPolicyDeviceState(state)),
Nathalie Le Clair88fa2752021-11-23 13:03:41 +0100982 port,
Mikhail Naganovb60bd1b2021-07-15 17:31:43 -0700983 VALUE_OR_RETURN_STATUS(
984 legacy2aidl_audio_format_t_AudioFormatDescription(encodedFormat))));
Eric Laurentc2f1f072009-07-17 12:17:14 -0700985}
986
Dima Zavinfce7a472011-04-19 22:30:36 -0700987audio_policy_dev_state_t AudioSystem::getDeviceConnectionState(audio_devices_t device,
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800988 const char* device_address) {
Andy Hung264fa4c2024-02-21 15:52:12 -0800989 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Dima Zavinfce7a472011-04-19 22:30:36 -0700990 if (aps == 0) return AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE;
Eric Laurentc2f1f072009-07-17 12:17:14 -0700991
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800992 auto result = [&]() -> ConversionResult<audio_policy_dev_state_t> {
Mikhail Naganov932cb962021-09-16 01:05:27 +0000993 AudioDevice deviceAidl = VALUE_OR_RETURN(
994 legacy2aidl_audio_device_AudioDevice(device, device_address));
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800995
996 media::AudioPolicyDeviceState result;
997 RETURN_IF_ERROR(statusTFromBinderStatus(
998 aps->getDeviceConnectionState(deviceAidl, &result)));
999
1000 return aidl2legacy_AudioPolicyDeviceState_audio_policy_dev_state_t(result);
1001 }();
1002 return result.value_or(AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE);
Eric Laurentc2f1f072009-07-17 12:17:14 -07001003}
1004
Pavlin Radoslavovf862bc62016-12-26 18:57:22 -08001005status_t AudioSystem::handleDeviceConfigChange(audio_devices_t device,
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001006 const char* device_address,
1007 const char* device_name,
1008 audio_format_t encodedFormat) {
Andy Hung264fa4c2024-02-21 15:52:12 -08001009 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001010 const char* address = "";
1011 const char* name = "";
Pavlin Radoslavovf862bc62016-12-26 18:57:22 -08001012
1013 if (aps == 0) return PERMISSION_DENIED;
1014
1015 if (device_address != NULL) {
1016 address = device_address;
1017 }
1018 if (device_name != NULL) {
1019 name = device_name;
1020 }
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001021
Mikhail Naganov932cb962021-09-16 01:05:27 +00001022 AudioDevice deviceAidl = VALUE_OR_RETURN_STATUS(
1023 legacy2aidl_audio_device_AudioDevice(device, address));
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001024
1025 return statusTFromBinderStatus(
1026 aps->handleDeviceConfigChange(deviceAidl, name, VALUE_OR_RETURN_STATUS(
Mikhail Naganovb60bd1b2021-07-15 17:31:43 -07001027 legacy2aidl_audio_format_t_AudioFormatDescription(encodedFormat))));
Pavlin Radoslavovf862bc62016-12-26 18:57:22 -08001028}
1029
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001030status_t AudioSystem::setPhoneState(audio_mode_t state, uid_t uid) {
Glenn Kasten347966c2012-01-18 14:58:32 -08001031 if (uint32_t(state) >= AUDIO_MODE_CNT) return BAD_VALUE;
Andy Hung264fa4c2024-02-21 15:52:12 -08001032 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Eric Laurentc2f1f072009-07-17 12:17:14 -07001033 if (aps == 0) return PERMISSION_DENIED;
1034
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001035 return statusTFromBinderStatus(aps->setPhoneState(
1036 VALUE_OR_RETURN_STATUS(legacy2aidl_audio_mode_t_AudioMode(state)),
1037 VALUE_OR_RETURN_STATUS(legacy2aidl_uid_t_int32_t(uid))));
Eric Laurentc2f1f072009-07-17 12:17:14 -07001038}
1039
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001040status_t
1041AudioSystem::setForceUse(audio_policy_force_use_t usage, audio_policy_forced_cfg_t config) {
Andy Hung264fa4c2024-02-21 15:52:12 -08001042 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Eric Laurentc2f1f072009-07-17 12:17:14 -07001043 if (aps == 0) return PERMISSION_DENIED;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001044
1045 return statusTFromBinderStatus(
1046 aps->setForceUse(
1047 VALUE_OR_RETURN_STATUS(
1048 legacy2aidl_audio_policy_force_use_t_AudioPolicyForceUse(usage)),
1049 VALUE_OR_RETURN_STATUS(
1050 legacy2aidl_audio_policy_forced_cfg_t_AudioPolicyForcedConfig(
1051 config))));
Eric Laurentc2f1f072009-07-17 12:17:14 -07001052}
1053
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001054audio_policy_forced_cfg_t AudioSystem::getForceUse(audio_policy_force_use_t usage) {
Andy Hung264fa4c2024-02-21 15:52:12 -08001055 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Dima Zavinfce7a472011-04-19 22:30:36 -07001056 if (aps == 0) return AUDIO_POLICY_FORCE_NONE;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001057
1058 auto result = [&]() -> ConversionResult<audio_policy_forced_cfg_t> {
François Gaffie873738c2024-08-02 17:09:21 +02001059 AudioPolicyForceUse usageAidl = VALUE_OR_RETURN(
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001060 legacy2aidl_audio_policy_force_use_t_AudioPolicyForceUse(usage));
François Gaffie873738c2024-08-02 17:09:21 +02001061 AudioPolicyForcedConfig configAidl;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001062 RETURN_IF_ERROR(statusTFromBinderStatus(
1063 aps->getForceUse(usageAidl, &configAidl)));
1064 return aidl2legacy_AudioPolicyForcedConfig_audio_policy_forced_cfg_t(configAidl);
1065 }();
1066
1067 return result.value_or(AUDIO_POLICY_FORCE_NONE);
Eric Laurentc2f1f072009-07-17 12:17:14 -07001068}
1069
1070
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001071audio_io_handle_t AudioSystem::getOutput(audio_stream_type_t stream) {
Andy Hung264fa4c2024-02-21 15:52:12 -08001072 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001073 if (aps == 0) return AUDIO_IO_HANDLE_NONE;
1074
1075 auto result = [&]() -> ConversionResult<audio_io_handle_t> {
Mikhail Naganovdbf03642021-08-25 18:15:32 -07001076 AudioStreamType streamAidl = VALUE_OR_RETURN(
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001077 legacy2aidl_audio_stream_type_t_AudioStreamType(stream));
1078 int32_t outputAidl;
1079 RETURN_IF_ERROR(
1080 statusTFromBinderStatus(aps->getOutput(streamAidl, &outputAidl)));
1081 return aidl2legacy_int32_t_audio_io_handle_t(outputAidl);
1082 }();
1083
1084 return result.value_or(AUDIO_IO_HANDLE_NONE);
Eric Laurentc2f1f072009-07-17 12:17:14 -07001085}
1086
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001087status_t AudioSystem::getOutputForAttr(audio_attributes_t* attr,
1088 audio_io_handle_t* output,
1089 audio_session_t session,
1090 audio_stream_type_t* stream,
Svet Ganov3e5f14f2021-05-13 22:51:08 +00001091 const AttributionSourceState& attributionSource,
jiabinf1c73972022-04-14 16:28:52 -07001092 audio_config_t* config,
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001093 audio_output_flags_t flags,
1094 audio_port_handle_t* selectedDeviceId,
1095 audio_port_handle_t* portId,
Eric Laurentb0a7bc92022-04-05 15:06:08 +02001096 std::vector<audio_io_handle_t>* secondaryOutputs,
jiabinc658e452022-10-21 20:52:21 +00001097 bool *isSpatialized,
Andy Hung6b137d12024-08-27 22:35:17 +00001098 bool *isBitPerfect,
1099 float *volume) {
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001100 if (attr == nullptr) {
1101 ALOGE("%s NULL audio attributes", __func__);
1102 return BAD_VALUE;
1103 }
1104 if (output == nullptr) {
1105 ALOGE("%s NULL output - shouldn't happen", __func__);
1106 return BAD_VALUE;
1107 }
1108 if (selectedDeviceId == nullptr) {
1109 ALOGE("%s NULL selectedDeviceId - shouldn't happen", __func__);
1110 return BAD_VALUE;
1111 }
1112 if (portId == nullptr) {
1113 ALOGE("%s NULL portId - shouldn't happen", __func__);
1114 return BAD_VALUE;
1115 }
1116 if (secondaryOutputs == nullptr) {
1117 ALOGE("%s NULL secondaryOutputs - shouldn't happen", __func__);
1118 return BAD_VALUE;
1119 }
1120
Andy Hung264fa4c2024-02-21 15:52:12 -08001121 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Eric Laurente83b55d2014-11-14 10:06:21 -08001122 if (aps == 0) return NO_INIT;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001123
Mikhail Naganov1c400902023-05-17 11:48:43 -07001124 media::audio::common::AudioAttributes attrAidl = VALUE_OR_RETURN_STATUS(
1125 legacy2aidl_audio_attributes_t_AudioAttributes(*attr));
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001126 int32_t sessionAidl = VALUE_OR_RETURN_STATUS(legacy2aidl_audio_session_t_int32_t(session));
Mikhail Naganovdbf03642021-08-25 18:15:32 -07001127 AudioConfig configAidl = VALUE_OR_RETURN_STATUS(
Mikhail Naganovde3fa182021-07-30 15:06:42 -07001128 legacy2aidl_audio_config_t_AudioConfig(*config, false /*isInput*/));
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001129 int32_t flagsAidl = VALUE_OR_RETURN_STATUS(
1130 legacy2aidl_audio_output_flags_t_int32_t_mask(flags));
Eric Laurentf99edd32021-02-01 15:57:33 +01001131 int32_t selectedDeviceIdAidl = VALUE_OR_RETURN_STATUS(
1132 legacy2aidl_audio_port_handle_t_int32_t(*selectedDeviceId));
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001133
1134 media::GetOutputForAttrResponse responseAidl;
1135
jiabinf1c73972022-04-14 16:28:52 -07001136 status_t status = statusTFromBinderStatus(
Svet Ganov3e5f14f2021-05-13 22:51:08 +00001137 aps->getOutputForAttr(attrAidl, sessionAidl, attributionSource, configAidl, flagsAidl,
jiabinf1c73972022-04-14 16:28:52 -07001138 selectedDeviceIdAidl, &responseAidl));
1139 if (status != NO_ERROR) {
1140 config->format = VALUE_OR_RETURN_STATUS(
1141 aidl2legacy_AudioFormatDescription_audio_format_t(responseAidl.configBase.format));
1142 config->channel_mask = VALUE_OR_RETURN_STATUS(
1143 aidl2legacy_AudioChannelLayout_audio_channel_mask_t(
1144 responseAidl.configBase.channelMask, false /*isInput*/));
1145 config->sample_rate = responseAidl.configBase.sampleRate;
1146 return status;
1147 }
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001148
1149 *output = VALUE_OR_RETURN_STATUS(
1150 aidl2legacy_int32_t_audio_io_handle_t(responseAidl.output));
1151
1152 if (stream != nullptr) {
1153 *stream = VALUE_OR_RETURN_STATUS(
1154 aidl2legacy_AudioStreamType_audio_stream_type_t(responseAidl.stream));
1155 }
1156 *selectedDeviceId = VALUE_OR_RETURN_STATUS(
1157 aidl2legacy_int32_t_audio_port_handle_t(responseAidl.selectedDeviceId));
1158 *portId = VALUE_OR_RETURN_STATUS(aidl2legacy_int32_t_audio_port_handle_t(responseAidl.portId));
1159 *secondaryOutputs = VALUE_OR_RETURN_STATUS(convertContainer<std::vector<audio_io_handle_t>>(
1160 responseAidl.secondaryOutputs, aidl2legacy_int32_t_audio_io_handle_t));
Eric Laurentb0a7bc92022-04-05 15:06:08 +02001161 *isSpatialized = responseAidl.isSpatialized;
jiabinc658e452022-10-21 20:52:21 +00001162 *isBitPerfect = responseAidl.isBitPerfect;
Eric Laurent0d13fea2022-11-04 17:12:08 +01001163 *attr = VALUE_OR_RETURN_STATUS(
Mikhail Naganov1c400902023-05-17 11:48:43 -07001164 aidl2legacy_AudioAttributes_audio_attributes_t(responseAidl.attr));
Andy Hung6b137d12024-08-27 22:35:17 +00001165 *volume = responseAidl.volume;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001166
1167 return OK;
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -07001168}
1169
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001170status_t AudioSystem::startOutput(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 PERMISSION_DENIED;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001173
1174 int32_t portIdAidl = VALUE_OR_RETURN_STATUS(legacy2aidl_audio_port_handle_t_int32_t(portId));
1175 return statusTFromBinderStatus(aps->startOutput(portIdAidl));
Eric Laurentc2f1f072009-07-17 12:17:14 -07001176}
1177
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001178status_t AudioSystem::stopOutput(audio_port_handle_t portId) {
Andy Hung264fa4c2024-02-21 15:52:12 -08001179 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Eric Laurentc2f1f072009-07-17 12:17:14 -07001180 if (aps == 0) return PERMISSION_DENIED;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001181
1182 int32_t portIdAidl = VALUE_OR_RETURN_STATUS(legacy2aidl_audio_port_handle_t_int32_t(portId));
1183 return statusTFromBinderStatus(aps->stopOutput(portIdAidl));
Eric Laurentc2f1f072009-07-17 12:17:14 -07001184}
1185
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001186void AudioSystem::releaseOutput(audio_port_handle_t portId) {
Andy Hung264fa4c2024-02-21 15:52:12 -08001187 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Eric Laurentc2f1f072009-07-17 12:17:14 -07001188 if (aps == 0) return;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001189
1190 auto status = [&]() -> status_t {
1191 int32_t portIdAidl = VALUE_OR_RETURN_STATUS(
1192 legacy2aidl_audio_port_handle_t_int32_t(portId));
1193 RETURN_STATUS_IF_ERROR(statusTFromBinderStatus(aps->releaseOutput(portIdAidl)));
1194 return OK;
1195 }();
1196
1197 // Ignore status.
1198 (void) status;
Eric Laurentc2f1f072009-07-17 12:17:14 -07001199}
1200
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001201status_t AudioSystem::getInputForAttr(const audio_attributes_t* attr,
1202 audio_io_handle_t* input,
1203 audio_unique_id_t riid,
1204 audio_session_t session,
Svet Ganov3e5f14f2021-05-13 22:51:08 +00001205 const AttributionSourceState &attributionSource,
jiabinf1c73972022-04-14 16:28:52 -07001206 audio_config_base_t* config,
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001207 audio_input_flags_t flags,
1208 audio_port_handle_t* selectedDeviceId,
1209 audio_port_handle_t* portId) {
1210 if (attr == NULL) {
1211 ALOGE("getInputForAttr NULL attr - shouldn't happen");
1212 return BAD_VALUE;
1213 }
1214 if (input == NULL) {
1215 ALOGE("getInputForAttr NULL input - shouldn't happen");
1216 return BAD_VALUE;
1217 }
1218 if (selectedDeviceId == NULL) {
1219 ALOGE("getInputForAttr NULL selectedDeviceId - shouldn't happen");
1220 return BAD_VALUE;
1221 }
1222 if (portId == NULL) {
1223 ALOGE("getInputForAttr NULL portId - shouldn't happen");
1224 return BAD_VALUE;
1225 }
1226
Andy Hung264fa4c2024-02-21 15:52:12 -08001227 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Eric Laurentcaf7f482014-11-25 17:50:47 -08001228 if (aps == 0) return NO_INIT;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001229
Mikhail Naganov1c400902023-05-17 11:48:43 -07001230 media::audio::common::AudioAttributes attrAidl = VALUE_OR_RETURN_STATUS(
1231 legacy2aidl_audio_attributes_t_AudioAttributes(*attr));
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001232 int32_t inputAidl = VALUE_OR_RETURN_STATUS(legacy2aidl_audio_io_handle_t_int32_t(*input));
1233 int32_t riidAidl = VALUE_OR_RETURN_STATUS(legacy2aidl_audio_unique_id_t_int32_t(riid));
1234 int32_t sessionAidl = VALUE_OR_RETURN_STATUS(legacy2aidl_audio_session_t_int32_t(session));
Mikhail Naganovdbf03642021-08-25 18:15:32 -07001235 AudioConfigBase configAidl = VALUE_OR_RETURN_STATUS(
Mikhail Naganovde3fa182021-07-30 15:06:42 -07001236 legacy2aidl_audio_config_base_t_AudioConfigBase(*config, true /*isInput*/));
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001237 int32_t flagsAidl = VALUE_OR_RETURN_STATUS(legacy2aidl_audio_input_flags_t_int32_t_mask(flags));
Eric Laurentf99edd32021-02-01 15:57:33 +01001238 int32_t selectedDeviceIdAidl = VALUE_OR_RETURN_STATUS(
1239 legacy2aidl_audio_port_handle_t_int32_t(*selectedDeviceId));
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001240
1241 media::GetInputForAttrResponse response;
1242
jiabinf1c73972022-04-14 16:28:52 -07001243 status_t status = statusTFromBinderStatus(
Svet Ganov3e5f14f2021-05-13 22:51:08 +00001244 aps->getInputForAttr(attrAidl, inputAidl, riidAidl, sessionAidl, attributionSource,
jiabinf1c73972022-04-14 16:28:52 -07001245 configAidl, flagsAidl, selectedDeviceIdAidl, &response));
1246 if (status != NO_ERROR) {
1247 *config = VALUE_OR_RETURN_STATUS(
1248 aidl2legacy_AudioConfigBase_audio_config_base_t(response.config, true /*isInput*/));
1249 return status;
1250 }
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001251
1252 *input = VALUE_OR_RETURN_STATUS(aidl2legacy_int32_t_audio_io_handle_t(response.input));
1253 *selectedDeviceId = VALUE_OR_RETURN_STATUS(
1254 aidl2legacy_int32_t_audio_port_handle_t(response.selectedDeviceId));
1255 *portId = VALUE_OR_RETURN_STATUS(aidl2legacy_int32_t_audio_port_handle_t(response.portId));
1256
1257 return OK;
Eric Laurentc2f1f072009-07-17 12:17:14 -07001258}
1259
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001260status_t AudioSystem::startInput(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 PERMISSION_DENIED;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001263
1264 int32_t portIdAidl = VALUE_OR_RETURN_STATUS(legacy2aidl_audio_port_handle_t_int32_t(portId));
1265 return statusTFromBinderStatus(aps->startInput(portIdAidl));
Eric Laurentc2f1f072009-07-17 12:17:14 -07001266}
1267
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001268status_t AudioSystem::stopInput(audio_port_handle_t portId) {
Andy Hung264fa4c2024-02-21 15:52:12 -08001269 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Eric Laurentc2f1f072009-07-17 12:17:14 -07001270 if (aps == 0) return PERMISSION_DENIED;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001271
1272 int32_t portIdAidl = VALUE_OR_RETURN_STATUS(legacy2aidl_audio_port_handle_t_int32_t(portId));
1273 return statusTFromBinderStatus(aps->stopInput(portIdAidl));
Eric Laurentc2f1f072009-07-17 12:17:14 -07001274}
1275
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001276void AudioSystem::releaseInput(audio_port_handle_t portId) {
Andy Hung264fa4c2024-02-21 15:52:12 -08001277 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Eric Laurentc2f1f072009-07-17 12:17:14 -07001278 if (aps == 0) return;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001279
1280 auto status = [&]() -> status_t {
1281 int32_t portIdAidl = VALUE_OR_RETURN_STATUS(
1282 legacy2aidl_audio_port_handle_t_int32_t(portId));
1283 RETURN_STATUS_IF_ERROR(statusTFromBinderStatus(aps->releaseInput(portIdAidl)));
1284 return OK;
1285 }();
1286
1287 // Ignore status.
1288 (void) status;
Eric Laurentc2f1f072009-07-17 12:17:14 -07001289}
1290
Vlad Popa87e0e582024-05-20 18:49:20 -07001291status_t AudioSystem::setDeviceAbsoluteVolumeEnabled(audio_devices_t deviceType,
1292 const char *address,
1293 bool enabled,
1294 audio_stream_type_t streamToDriveAbs) {
1295 const sp<IAudioPolicyService> aps = get_audio_policy_service();
1296 if (aps == nullptr) return PERMISSION_DENIED;
1297
1298 AudioDevice deviceAidl = VALUE_OR_RETURN_STATUS(
1299 legacy2aidl_audio_device_AudioDevice(deviceType, address));
1300 AudioStreamType streamToDriveAbsAidl = VALUE_OR_RETURN_STATUS(
1301 legacy2aidl_audio_stream_type_t_AudioStreamType(streamToDriveAbs));
1302 return statusTFromBinderStatus(
1303 aps->setDeviceAbsoluteVolumeEnabled(deviceAidl, enabled, streamToDriveAbsAidl));
1304}
1305
Dima Zavinfce7a472011-04-19 22:30:36 -07001306status_t AudioSystem::initStreamVolume(audio_stream_type_t stream,
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001307 int indexMin,
1308 int indexMax) {
Andy Hung264fa4c2024-02-21 15:52:12 -08001309 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Eric Laurentc2f1f072009-07-17 12:17:14 -07001310 if (aps == 0) return PERMISSION_DENIED;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001311
Mikhail Naganovdbf03642021-08-25 18:15:32 -07001312 AudioStreamType streamAidl = VALUE_OR_RETURN_STATUS(
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001313 legacy2aidl_audio_stream_type_t_AudioStreamType(stream));
1314 int32_t indexMinAidl = VALUE_OR_RETURN_STATUS(convertIntegral<int32_t>(indexMin));
1315 int32_t indexMaxAidl = VALUE_OR_RETURN_STATUS(convertIntegral<int32_t>(indexMax));
Mikhail Naganovec3d5792022-05-06 00:19:55 +00001316 status_t status = statusTFromBinderStatus(
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001317 aps->initStreamVolume(streamAidl, indexMinAidl, indexMaxAidl));
Mikhail Naganovec3d5792022-05-06 00:19:55 +00001318 if (status == DEAD_OBJECT) {
1319 // This is a critical operation since w/o proper stream volumes no audio
1320 // will be heard. Make sure we recover from a failure in any case.
1321 ALOGE("Received DEAD_OBJECT from APS, clearing the client");
1322 clearAudioPolicyService();
1323 }
1324 return status;
Eric Laurentc2f1f072009-07-17 12:17:14 -07001325}
1326
Eric Laurent83844cc2011-11-18 16:43:31 -08001327status_t AudioSystem::setStreamVolumeIndex(audio_stream_type_t stream,
1328 int index,
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001329 audio_devices_t device) {
Andy Hung264fa4c2024-02-21 15:52:12 -08001330 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Eric Laurentc2f1f072009-07-17 12:17:14 -07001331 if (aps == 0) return PERMISSION_DENIED;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001332
Mikhail Naganovdbf03642021-08-25 18:15:32 -07001333 AudioStreamType streamAidl = VALUE_OR_RETURN_STATUS(
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001334 legacy2aidl_audio_stream_type_t_AudioStreamType(stream));
1335 int32_t indexAidl = VALUE_OR_RETURN_STATUS(convertIntegral<int32_t>(index));
Mikhail Naganovf4a75362021-09-16 00:02:54 +00001336 AudioDeviceDescription deviceAidl = VALUE_OR_RETURN_STATUS(
Mikhail Naganov21a32ec2021-07-08 14:40:12 -07001337 legacy2aidl_audio_devices_t_AudioDeviceDescription(device));
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001338 return statusTFromBinderStatus(
1339 aps->setStreamVolumeIndex(streamAidl, deviceAidl, indexAidl));
Eric Laurentc2f1f072009-07-17 12:17:14 -07001340}
1341
Eric Laurent83844cc2011-11-18 16:43:31 -08001342status_t AudioSystem::getStreamVolumeIndex(audio_stream_type_t stream,
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001343 int* index,
1344 audio_devices_t device) {
Andy Hung264fa4c2024-02-21 15:52:12 -08001345 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Eric Laurentc2f1f072009-07-17 12:17:14 -07001346 if (aps == 0) return PERMISSION_DENIED;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001347
Mikhail Naganovdbf03642021-08-25 18:15:32 -07001348 AudioStreamType streamAidl = VALUE_OR_RETURN_STATUS(
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001349 legacy2aidl_audio_stream_type_t_AudioStreamType(stream));
Mikhail Naganovf4a75362021-09-16 00:02:54 +00001350 AudioDeviceDescription deviceAidl = VALUE_OR_RETURN_STATUS(
Mikhail Naganov21a32ec2021-07-08 14:40:12 -07001351 legacy2aidl_audio_devices_t_AudioDeviceDescription(device));
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001352 int32_t indexAidl;
1353 RETURN_STATUS_IF_ERROR(statusTFromBinderStatus(
1354 aps->getStreamVolumeIndex(streamAidl, deviceAidl, &indexAidl)));
1355 if (index != nullptr) {
1356 *index = VALUE_OR_RETURN_STATUS(convertIntegral<int>(indexAidl));
1357 }
1358 return OK;
Eric Laurentc2f1f072009-07-17 12:17:14 -07001359}
1360
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001361status_t AudioSystem::setVolumeIndexForAttributes(const audio_attributes_t& attr,
François Gaffiecfe17322018-11-07 13:41:29 +01001362 int index,
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001363 audio_devices_t device) {
Andy Hung264fa4c2024-02-21 15:52:12 -08001364 const sp<IAudioPolicyService> aps = get_audio_policy_service();
François Gaffiecfe17322018-11-07 13:41:29 +01001365 if (aps == 0) return PERMISSION_DENIED;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001366
Mikhail Naganov1c400902023-05-17 11:48:43 -07001367 media::audio::common::AudioAttributes attrAidl = VALUE_OR_RETURN_STATUS(
1368 legacy2aidl_audio_attributes_t_AudioAttributes(attr));
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001369 int32_t indexAidl = VALUE_OR_RETURN_STATUS(convertIntegral<int32_t>(index));
Mikhail Naganovf4a75362021-09-16 00:02:54 +00001370 AudioDeviceDescription deviceAidl = VALUE_OR_RETURN_STATUS(
Mikhail Naganov21a32ec2021-07-08 14:40:12 -07001371 legacy2aidl_audio_devices_t_AudioDeviceDescription(device));
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001372 return statusTFromBinderStatus(
1373 aps->setVolumeIndexForAttributes(attrAidl, deviceAidl, indexAidl));
François Gaffiecfe17322018-11-07 13:41:29 +01001374}
1375
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001376status_t AudioSystem::getVolumeIndexForAttributes(const audio_attributes_t& attr,
1377 int& index,
1378 audio_devices_t device) {
Andy Hung264fa4c2024-02-21 15:52:12 -08001379 const sp<IAudioPolicyService> aps = get_audio_policy_service();
François Gaffiecfe17322018-11-07 13:41:29 +01001380 if (aps == 0) return PERMISSION_DENIED;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001381
Mikhail Naganov1c400902023-05-17 11:48:43 -07001382 media::audio::common::AudioAttributes attrAidl = VALUE_OR_RETURN_STATUS(
1383 legacy2aidl_audio_attributes_t_AudioAttributes(attr));
Mikhail Naganovf4a75362021-09-16 00:02:54 +00001384 AudioDeviceDescription deviceAidl = VALUE_OR_RETURN_STATUS(
Mikhail Naganov21a32ec2021-07-08 14:40:12 -07001385 legacy2aidl_audio_devices_t_AudioDeviceDescription(device));
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001386 int32_t indexAidl;
1387 RETURN_STATUS_IF_ERROR(statusTFromBinderStatus(
1388 aps->getVolumeIndexForAttributes(attrAidl, deviceAidl, &indexAidl)));
1389 index = VALUE_OR_RETURN_STATUS(convertIntegral<int>(indexAidl));
1390 return OK;
François Gaffiecfe17322018-11-07 13:41:29 +01001391}
1392
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001393status_t AudioSystem::getMaxVolumeIndexForAttributes(const audio_attributes_t& attr, int& index) {
Andy Hung264fa4c2024-02-21 15:52:12 -08001394 const sp<IAudioPolicyService> aps = get_audio_policy_service();
François Gaffiecfe17322018-11-07 13:41:29 +01001395 if (aps == 0) return PERMISSION_DENIED;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001396
Mikhail Naganov1c400902023-05-17 11:48:43 -07001397 media::audio::common::AudioAttributes attrAidl = VALUE_OR_RETURN_STATUS(
1398 legacy2aidl_audio_attributes_t_AudioAttributes(attr));
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001399 int32_t indexAidl;
1400 RETURN_STATUS_IF_ERROR(statusTFromBinderStatus(
1401 aps->getMaxVolumeIndexForAttributes(attrAidl, &indexAidl)));
1402 index = VALUE_OR_RETURN_STATUS(convertIntegral<int>(indexAidl));
1403 return OK;
François Gaffiecfe17322018-11-07 13:41:29 +01001404}
1405
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001406status_t AudioSystem::getMinVolumeIndexForAttributes(const audio_attributes_t& attr, int& index) {
Andy Hung264fa4c2024-02-21 15:52:12 -08001407 const sp<IAudioPolicyService> aps = get_audio_policy_service();
François Gaffiecfe17322018-11-07 13:41:29 +01001408 if (aps == 0) return PERMISSION_DENIED;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001409
Mikhail Naganov1c400902023-05-17 11:48:43 -07001410 media::audio::common::AudioAttributes attrAidl = VALUE_OR_RETURN_STATUS(
1411 legacy2aidl_audio_attributes_t_AudioAttributes(attr));
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001412 int32_t indexAidl;
1413 RETURN_STATUS_IF_ERROR(statusTFromBinderStatus(
1414 aps->getMinVolumeIndexForAttributes(attrAidl, &indexAidl)));
1415 index = VALUE_OR_RETURN_STATUS(convertIntegral<int>(indexAidl));
1416 return OK;
François Gaffiecfe17322018-11-07 13:41:29 +01001417}
1418
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001419product_strategy_t AudioSystem::getStrategyForStream(audio_stream_type_t stream) {
Andy Hung264fa4c2024-02-21 15:52:12 -08001420 const sp<IAudioPolicyService> aps = get_audio_policy_service();
François Gaffied0ba9ed2018-11-05 11:50:42 +01001421 if (aps == 0) return PRODUCT_STRATEGY_NONE;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001422
1423 auto result = [&]() -> ConversionResult<product_strategy_t> {
Mikhail Naganovdbf03642021-08-25 18:15:32 -07001424 AudioStreamType streamAidl = VALUE_OR_RETURN(
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001425 legacy2aidl_audio_stream_type_t_AudioStreamType(stream));
1426 int32_t resultAidl;
1427 RETURN_IF_ERROR(statusTFromBinderStatus(
1428 aps->getStrategyForStream(streamAidl, &resultAidl)));
1429 return aidl2legacy_int32_t_product_strategy_t(resultAidl);
1430 }();
1431 return result.value_or(PRODUCT_STRATEGY_NONE);
Eric Laurentde070132010-07-13 04:45:46 -07001432}
1433
François Gaffie1e2b56f2022-04-01 14:34:29 +02001434status_t AudioSystem::getDevicesForAttributes(const audio_attributes_t& aa,
Andy Hung6d23c0f2022-02-16 09:37:15 -08001435 AudioDeviceTypeAddrVector* devices,
1436 bool forVolume) {
Jean-Michel Trivif41599b2020-01-07 14:22:08 -08001437 if (devices == nullptr) {
1438 return BAD_VALUE;
1439 }
Andy Hung264fa4c2024-02-21 15:52:12 -08001440 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Jean-Michel Trivif41599b2020-01-07 14:22:08 -08001441 if (aps == 0) return PERMISSION_DENIED;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001442
Mikhail Naganov1c400902023-05-17 11:48:43 -07001443 media::audio::common::AudioAttributes aaAidl = VALUE_OR_RETURN_STATUS(
1444 legacy2aidl_audio_attributes_t_AudioAttributes(aa));
Mikhail Naganovf4a75362021-09-16 00:02:54 +00001445 std::vector<AudioDevice> retAidl;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001446 RETURN_STATUS_IF_ERROR(
Andy Hung6d23c0f2022-02-16 09:37:15 -08001447 statusTFromBinderStatus(aps->getDevicesForAttributes(aaAidl, forVolume, &retAidl)));
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001448 *devices = VALUE_OR_RETURN_STATUS(
1449 convertContainer<AudioDeviceTypeAddrVector>(
1450 retAidl,
1451 aidl2legacy_AudioDeviceTypeAddress));
1452 return OK;
Jean-Michel Trivif41599b2020-01-07 14:22:08 -08001453}
1454
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001455audio_io_handle_t AudioSystem::getOutputForEffect(const effect_descriptor_t* desc) {
Andy Hung264fa4c2024-02-21 15:52:12 -08001456 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Glenn Kastenefa6ea92014-01-08 09:10:43 -08001457 // FIXME change return type to status_t, and return PERMISSION_DENIED here
Glenn Kasten142f5192014-03-25 17:44:59 -07001458 if (aps == 0) return AUDIO_IO_HANDLE_NONE;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001459
1460 auto result = [&]() -> ConversionResult<audio_io_handle_t> {
1461 media::EffectDescriptor descAidl = VALUE_OR_RETURN(
1462 legacy2aidl_effect_descriptor_t_EffectDescriptor(*desc));
1463 int32_t retAidl;
1464 RETURN_IF_ERROR(
1465 statusTFromBinderStatus(aps->getOutputForEffect(descAidl, &retAidl)));
1466 return aidl2legacy_int32_t_audio_io_handle_t(retAidl);
1467 }();
1468
1469 return result.value_or(AUDIO_IO_HANDLE_NONE);
Eric Laurentde070132010-07-13 04:45:46 -07001470}
1471
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001472status_t AudioSystem::registerEffect(const effect_descriptor_t* desc,
1473 audio_io_handle_t io,
1474 product_strategy_t strategy,
1475 audio_session_t session,
1476 int id) {
Andy Hung264fa4c2024-02-21 15:52:12 -08001477 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Eric Laurentde070132010-07-13 04:45:46 -07001478 if (aps == 0) return PERMISSION_DENIED;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001479
1480 media::EffectDescriptor descAidl = VALUE_OR_RETURN_STATUS(
1481 legacy2aidl_effect_descriptor_t_EffectDescriptor(*desc));
1482 int32_t ioAidl = VALUE_OR_RETURN_STATUS(legacy2aidl_audio_io_handle_t_int32_t(io));
1483 int32_t strategyAidl = VALUE_OR_RETURN_STATUS(aidl2legacy_int32_t_product_strategy_t(strategy));
1484 int32_t sessionAidl = VALUE_OR_RETURN_STATUS(legacy2aidl_audio_session_t_int32_t(session));
1485 int32_t idAidl = VALUE_OR_RETURN_STATUS(convertReinterpret<int32_t>(id));
1486 return statusTFromBinderStatus(
1487 aps->registerEffect(descAidl, ioAidl, strategyAidl, sessionAidl, idAidl));
Eric Laurentde070132010-07-13 04:45:46 -07001488}
1489
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001490status_t AudioSystem::unregisterEffect(int id) {
Andy Hung264fa4c2024-02-21 15:52:12 -08001491 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Eric Laurentde070132010-07-13 04:45:46 -07001492 if (aps == 0) return PERMISSION_DENIED;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001493
1494 int32_t idAidl = VALUE_OR_RETURN_STATUS(convertReinterpret<int32_t>(id));
1495 return statusTFromBinderStatus(
1496 aps->unregisterEffect(idAidl));
Eric Laurentde070132010-07-13 04:45:46 -07001497}
1498
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001499status_t AudioSystem::setEffectEnabled(int id, bool enabled) {
Andy Hung264fa4c2024-02-21 15:52:12 -08001500 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Eric Laurentdb7c0792011-08-10 10:37:50 -07001501 if (aps == 0) return PERMISSION_DENIED;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001502
1503 int32_t idAidl = VALUE_OR_RETURN_STATUS(convertReinterpret<int32_t>(id));
1504 return statusTFromBinderStatus(
1505 aps->setEffectEnabled(idAidl, enabled));
Eric Laurentdb7c0792011-08-10 10:37:50 -07001506}
1507
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001508status_t AudioSystem::moveEffectsToIo(const std::vector<int>& ids, audio_io_handle_t io) {
Andy Hung264fa4c2024-02-21 15:52:12 -08001509 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Eric Laurent6c796322019-04-09 14:13:17 -07001510 if (aps == 0) return PERMISSION_DENIED;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001511
1512 std::vector<int32_t> idsAidl = VALUE_OR_RETURN_STATUS(
1513 convertContainer<std::vector<int32_t>>(ids, convertReinterpret<int32_t, int>));
1514 int32_t ioAidl = VALUE_OR_RETURN_STATUS(legacy2aidl_audio_io_handle_t_int32_t(io));
1515 return statusTFromBinderStatus(aps->moveEffectsToIo(idsAidl, ioAidl));
Eric Laurent6c796322019-04-09 14:13:17 -07001516}
1517
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001518status_t AudioSystem::isStreamActive(audio_stream_type_t stream, bool* state, uint32_t inPastMs) {
Andy Hung264fa4c2024-02-21 15:52:12 -08001519 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Eric Laurenteda6c362011-02-02 09:33:30 -08001520 if (aps == 0) return PERMISSION_DENIED;
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001521 if (state == NULL) return BAD_VALUE;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001522
Mikhail Naganovdbf03642021-08-25 18:15:32 -07001523 AudioStreamType streamAidl = VALUE_OR_RETURN_STATUS(
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001524 legacy2aidl_audio_stream_type_t_AudioStreamType(stream));
1525 int32_t inPastMsAidl = VALUE_OR_RETURN_STATUS(convertIntegral<int32_t>(inPastMs));
1526 RETURN_STATUS_IF_ERROR(statusTFromBinderStatus(
1527 aps->isStreamActive(streamAidl, inPastMsAidl, state)));
1528 return OK;
Eric Laurenteda6c362011-02-02 09:33:30 -08001529}
1530
Jean-Michel Trivi272ab542013-02-04 16:26:02 -08001531status_t AudioSystem::isStreamActiveRemotely(audio_stream_type_t stream, bool* state,
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001532 uint32_t inPastMs) {
Andy Hung264fa4c2024-02-21 15:52:12 -08001533 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Jean-Michel Trivi272ab542013-02-04 16:26:02 -08001534 if (aps == 0) return PERMISSION_DENIED;
1535 if (state == NULL) return BAD_VALUE;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001536
Mikhail Naganovdbf03642021-08-25 18:15:32 -07001537 AudioStreamType streamAidl = VALUE_OR_RETURN_STATUS(
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001538 legacy2aidl_audio_stream_type_t_AudioStreamType(stream));
1539 int32_t inPastMsAidl = VALUE_OR_RETURN_STATUS(convertIntegral<int32_t>(inPastMs));
1540 RETURN_STATUS_IF_ERROR(statusTFromBinderStatus(
1541 aps->isStreamActiveRemotely(streamAidl, inPastMsAidl, state)));
1542 return OK;
Jean-Michel Trivi272ab542013-02-04 16:26:02 -08001543}
1544
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001545status_t AudioSystem::isSourceActive(audio_source_t stream, bool* state) {
Andy Hung264fa4c2024-02-21 15:52:12 -08001546 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Jean-Michel Trivid7086032012-10-10 12:11:16 -07001547 if (aps == 0) return PERMISSION_DENIED;
1548 if (state == NULL) return BAD_VALUE;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001549
Mikhail Naganovddceecc2021-09-03 13:58:56 -07001550 AudioSource streamAidl = VALUE_OR_RETURN_STATUS(
1551 legacy2aidl_audio_source_t_AudioSource(stream));
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001552 RETURN_STATUS_IF_ERROR(statusTFromBinderStatus(
1553 aps->isSourceActive(streamAidl, state)));
1554 return OK;
Jean-Michel Trivid7086032012-10-10 12:11:16 -07001555}
1556
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001557uint32_t AudioSystem::getPrimaryOutputSamplingRate() {
Andy Hung264fa4c2024-02-21 15:52:12 -08001558 const sp<IAudioFlinger> af = get_audio_flinger();
Glenn Kastencc0f1cf2012-09-24 11:27:18 -07001559 if (af == 0) return 0;
1560 return af->getPrimaryOutputSamplingRate();
1561}
1562
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001563size_t AudioSystem::getPrimaryOutputFrameCount() {
Andy Hung264fa4c2024-02-21 15:52:12 -08001564 const sp<IAudioFlinger> af = get_audio_flinger();
Glenn Kastencc0f1cf2012-09-24 11:27:18 -07001565 if (af == 0) return 0;
1566 return af->getPrimaryOutputFrameCount();
1567}
Eric Laurenteda6c362011-02-02 09:33:30 -08001568
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001569status_t AudioSystem::setLowRamDevice(bool isLowRamDevice, int64_t totalMemory) {
Andy Hung264fa4c2024-02-21 15:52:12 -08001570 const sp<IAudioFlinger> af = get_audio_flinger();
Glenn Kasten4182c4e2013-07-15 14:45:07 -07001571 if (af == 0) return PERMISSION_DENIED;
Andy Hung6f248bb2018-01-23 14:04:37 -08001572 return af->setLowRamDevice(isLowRamDevice, totalMemory);
Glenn Kasten4182c4e2013-07-15 14:45:07 -07001573}
1574
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001575void AudioSystem::clearAudioConfigCache() {
Glenn Kastend2d089f2014-11-05 11:48:12 -08001576 // called by restoreTrack_l(), which needs new IAudioFlinger and IAudioPolicyService instances
Steve Block3856b092011-10-20 11:56:00 +01001577 ALOGV("clearAudioConfigCache()");
Andy Hungda1fb072024-02-20 19:08:08 -08001578 gAudioFlingerServiceHandler.clearService();
Mikhail Naganovec3d5792022-05-06 00:19:55 +00001579 clearAudioPolicyService();
Eric Laurent9f6530f2011-08-30 10:18:54 -07001580}
1581
Hayden Gomes524159d2019-12-23 14:41:47 -08001582status_t AudioSystem::setSupportedSystemUsages(const std::vector<audio_usage_t>& systemUsages) {
Andy Hung264fa4c2024-02-21 15:52:12 -08001583 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Hayden Gomes524159d2019-12-23 14:41:47 -08001584 if (aps == nullptr) return PERMISSION_DENIED;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001585
Mikhail Naganovdbf03642021-08-25 18:15:32 -07001586 std::vector<AudioUsage> systemUsagesAidl = VALUE_OR_RETURN_STATUS(
1587 convertContainer<std::vector<AudioUsage>>(systemUsages,
1588 legacy2aidl_audio_usage_t_AudioUsage));
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001589 return statusTFromBinderStatus(aps->setSupportedSystemUsages(systemUsagesAidl));
Hayden Gomes524159d2019-12-23 14:41:47 -08001590}
1591
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001592status_t AudioSystem::setAllowedCapturePolicy(uid_t uid, audio_flags_mask_t capturePolicy) {
Andy Hung264fa4c2024-02-21 15:52:12 -08001593 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Kevin Rocardb99cc752019-03-21 20:52:24 -07001594 if (aps == nullptr) return PERMISSION_DENIED;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001595
1596 int32_t uidAidl = VALUE_OR_RETURN_STATUS(legacy2aidl_uid_t_int32_t(uid));
1597 int32_t capturePolicyAidl = VALUE_OR_RETURN_STATUS(
1598 legacy2aidl_audio_flags_mask_t_int32_t_mask(capturePolicy));
1599 return statusTFromBinderStatus(aps->setAllowedCapturePolicy(uidAidl, capturePolicyAidl));
Kevin Rocardb99cc752019-03-21 20:52:24 -07001600}
1601
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001602audio_offload_mode_t AudioSystem::getOffloadSupport(const audio_offload_info_t& info) {
Eric Laurent90fe31c2020-11-26 20:06:35 +01001603 ALOGV("%s", __func__);
Andy Hung264fa4c2024-02-21 15:52:12 -08001604 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Eric Laurent90fe31c2020-11-26 20:06:35 +01001605 if (aps == 0) return AUDIO_OFFLOAD_NOT_SUPPORTED;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001606
1607 auto result = [&]() -> ConversionResult<audio_offload_mode_t> {
Mikhail Naganovdbf03642021-08-25 18:15:32 -07001608 AudioOffloadInfo infoAidl = VALUE_OR_RETURN(
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001609 legacy2aidl_audio_offload_info_t_AudioOffloadInfo(info));
1610 media::AudioOffloadMode retAidl;
1611 RETURN_IF_ERROR(
1612 statusTFromBinderStatus(aps->getOffloadSupport(infoAidl, &retAidl)));
1613 return aidl2legacy_AudioOffloadMode_audio_offload_mode_t(retAidl);
1614 }();
1615
1616 return result.value_or(static_cast<audio_offload_mode_t>(0));
Richard Fitzgeraldad3af332013-03-25 16:54:37 +00001617}
1618
Eric Laurent203b1a12014-04-01 10:34:16 -07001619status_t AudioSystem::listAudioPorts(audio_port_role_t role,
1620 audio_port_type_t type,
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001621 unsigned int* num_ports,
1622 struct audio_port_v7* ports,
1623 unsigned int* generation) {
1624 if (num_ports == nullptr || (*num_ports != 0 && ports == nullptr) ||
1625 generation == nullptr) {
1626 return BAD_VALUE;
1627 }
1628
Andy Hung264fa4c2024-02-21 15:52:12 -08001629 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Eric Laurent203b1a12014-04-01 10:34:16 -07001630 if (aps == 0) return PERMISSION_DENIED;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001631
1632 media::AudioPortRole roleAidl = VALUE_OR_RETURN_STATUS(
1633 legacy2aidl_audio_port_role_t_AudioPortRole(role));
1634 media::AudioPortType typeAidl = VALUE_OR_RETURN_STATUS(
1635 legacy2aidl_audio_port_type_t_AudioPortType(type));
Mikhail Naganov0078ee52021-09-30 23:06:20 +00001636 Int numPortsAidl;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001637 numPortsAidl.value = VALUE_OR_RETURN_STATUS(convertIntegral<int32_t>(*num_ports));
Atneya Nair638a6e42022-12-18 16:45:15 -08001638 std::vector<media::AudioPortFw> portsAidl;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001639 int32_t generationAidl;
1640
1641 RETURN_STATUS_IF_ERROR(statusTFromBinderStatus(
1642 aps->listAudioPorts(roleAidl, typeAidl, &numPortsAidl, &portsAidl, &generationAidl)));
1643 *num_ports = VALUE_OR_RETURN_STATUS(convertIntegral<unsigned int>(numPortsAidl.value));
1644 *generation = VALUE_OR_RETURN_STATUS(convertIntegral<unsigned int>(generationAidl));
1645 RETURN_STATUS_IF_ERROR(convertRange(portsAidl.begin(), portsAidl.end(), ports,
Mikhail Naganov87227252023-01-13 17:38:10 -08001646 aidl2legacy_AudioPortFw_audio_port_v7));
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001647 return OK;
Eric Laurent203b1a12014-04-01 10:34:16 -07001648}
1649
Mikhail Naganov5edc5ed2023-03-23 14:52:15 -07001650status_t AudioSystem::listDeclaredDevicePorts(media::AudioPortRole role,
1651 std::vector<media::AudioPortFw>* result) {
1652 if (result == nullptr) return BAD_VALUE;
Andy Hung264fa4c2024-02-21 15:52:12 -08001653 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Mikhail Naganov5edc5ed2023-03-23 14:52:15 -07001654 if (aps == 0) return PERMISSION_DENIED;
1655 RETURN_STATUS_IF_ERROR(statusTFromBinderStatus(aps->listDeclaredDevicePorts(role, result)));
1656 return OK;
1657}
1658
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001659status_t AudioSystem::getAudioPort(struct audio_port_v7* port) {
1660 if (port == nullptr) {
1661 return BAD_VALUE;
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 Nair638a6e42022-12-18 16:45:15 -08001666 media::AudioPortFw portAidl;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001667 RETURN_STATUS_IF_ERROR(
Mikhail Naganov17031562022-02-23 23:00:27 +00001668 statusTFromBinderStatus(aps->getAudioPort(port->id, &portAidl)));
Mikhail Naganov87227252023-01-13 17:38:10 -08001669 *port = VALUE_OR_RETURN_STATUS(aidl2legacy_AudioPortFw_audio_port_v7(portAidl));
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001670 return OK;
Eric Laurent203b1a12014-04-01 10:34:16 -07001671}
1672
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001673status_t AudioSystem::createAudioPatch(const struct audio_patch* patch,
1674 audio_patch_handle_t* handle) {
1675 if (patch == nullptr || handle == nullptr) {
1676 return BAD_VALUE;
1677 }
1678
Andy Hung264fa4c2024-02-21 15:52:12 -08001679 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Eric Laurent203b1a12014-04-01 10:34:16 -07001680 if (aps == 0) return PERMISSION_DENIED;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001681
Atneya Nair3afdbd12022-12-18 16:14:18 -08001682 media::AudioPatchFw patchAidl = VALUE_OR_RETURN_STATUS(
Mikhail Naganov87227252023-01-13 17:38:10 -08001683 legacy2aidl_audio_patch_AudioPatchFw(*patch));
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001684 int32_t handleAidl = VALUE_OR_RETURN_STATUS(legacy2aidl_audio_patch_handle_t_int32_t(*handle));
1685 RETURN_STATUS_IF_ERROR(
1686 statusTFromBinderStatus(aps->createAudioPatch(patchAidl, handleAidl, &handleAidl)));
1687 *handle = VALUE_OR_RETURN_STATUS(aidl2legacy_int32_t_audio_patch_handle_t(handleAidl));
1688 return OK;
Eric Laurent203b1a12014-04-01 10:34:16 -07001689}
1690
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001691status_t AudioSystem::releaseAudioPatch(audio_patch_handle_t handle) {
Andy Hung264fa4c2024-02-21 15:52:12 -08001692 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Eric Laurent203b1a12014-04-01 10:34:16 -07001693 if (aps == 0) return PERMISSION_DENIED;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001694
1695 int32_t handleAidl = VALUE_OR_RETURN_STATUS(legacy2aidl_audio_patch_handle_t_int32_t(handle));
1696 return statusTFromBinderStatus(aps->releaseAudioPatch(handleAidl));
Eric Laurent203b1a12014-04-01 10:34:16 -07001697}
1698
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001699status_t AudioSystem::listAudioPatches(unsigned int* num_patches,
1700 struct audio_patch* patches,
1701 unsigned int* generation) {
1702 if (num_patches == nullptr || (*num_patches != 0 && patches == nullptr) ||
1703 generation == nullptr) {
1704 return BAD_VALUE;
1705 }
1706
Andy Hung264fa4c2024-02-21 15:52:12 -08001707 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Eric Laurent203b1a12014-04-01 10:34:16 -07001708 if (aps == 0) return PERMISSION_DENIED;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001709
1710
Mikhail Naganov0078ee52021-09-30 23:06:20 +00001711 Int numPatchesAidl;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001712 numPatchesAidl.value = VALUE_OR_RETURN_STATUS(convertIntegral<int32_t>(*num_patches));
Atneya Nair3afdbd12022-12-18 16:14:18 -08001713 std::vector<media::AudioPatchFw> patchesAidl;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001714 int32_t generationAidl;
1715
1716 RETURN_STATUS_IF_ERROR(statusTFromBinderStatus(
1717 aps->listAudioPatches(&numPatchesAidl, &patchesAidl, &generationAidl)));
1718 *num_patches = VALUE_OR_RETURN_STATUS(convertIntegral<unsigned int>(numPatchesAidl.value));
1719 *generation = VALUE_OR_RETURN_STATUS(convertIntegral<unsigned int>(generationAidl));
1720 RETURN_STATUS_IF_ERROR(convertRange(patchesAidl.begin(), patchesAidl.end(), patches,
Mikhail Naganov87227252023-01-13 17:38:10 -08001721 aidl2legacy_AudioPatchFw_audio_patch));
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001722 return OK;
Eric Laurent203b1a12014-04-01 10:34:16 -07001723}
1724
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001725status_t AudioSystem::setAudioPortConfig(const struct audio_port_config* config) {
1726 if (config == nullptr) {
1727 return BAD_VALUE;
1728 }
1729
Andy Hung264fa4c2024-02-21 15:52:12 -08001730 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Eric Laurent203b1a12014-04-01 10:34:16 -07001731 if (aps == 0) return PERMISSION_DENIED;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001732
Atneya Nair7a9594f2022-12-18 17:26:26 -08001733 media::AudioPortConfigFw configAidl = VALUE_OR_RETURN_STATUS(
Mikhail Naganov87227252023-01-13 17:38:10 -08001734 legacy2aidl_audio_port_config_AudioPortConfigFw(*config));
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001735 return statusTFromBinderStatus(aps->setAudioPortConfig(configAidl));
Eric Laurent203b1a12014-04-01 10:34:16 -07001736}
1737
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001738status_t AudioSystem::addAudioPortCallback(const sp<AudioPortCallback>& callback) {
Andy Hung264fa4c2024-02-21 15:52:12 -08001739 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Eric Laurentb28753e2015-04-01 13:06:28 -07001740 if (aps == 0) return PERMISSION_DENIED;
Andy Hung88e71732024-02-21 16:44:27 -08001741 const auto apc = gAudioPolicyServiceHandler.getClient();
1742 if (apc == nullptr) return NO_INIT;
Eric Laurentb28753e2015-04-01 13:06:28 -07001743
Andy Hung88e71732024-02-21 16:44:27 -08001744 std::lock_guard _l(gApsCallbackMutex);
1745 const int ret = apc->addAudioPortCallback(callback);
Eric Laurente8726fe2015-06-26 09:39:24 -07001746 if (ret == 1) {
1747 aps->setAudioPortCallbacksEnabled(true);
1748 }
1749 return (ret < 0) ? INVALID_OPERATION : NO_ERROR;
Eric Laurentb52c1522014-05-20 11:27:36 -07001750}
1751
Jean-Michel Trivif613d422015-04-23 18:41:29 -07001752/*static*/
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001753status_t AudioSystem::removeAudioPortCallback(const sp<AudioPortCallback>& callback) {
Andy Hung264fa4c2024-02-21 15:52:12 -08001754 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Eric Laurentb28753e2015-04-01 13:06:28 -07001755 if (aps == 0) return PERMISSION_DENIED;
Andy Hung88e71732024-02-21 16:44:27 -08001756 const auto apc = gAudioPolicyServiceHandler.getClient();
1757 if (apc == nullptr) return NO_INIT;
Eric Laurentb28753e2015-04-01 13:06:28 -07001758
Andy Hung88e71732024-02-21 16:44:27 -08001759 std::lock_guard _l(gApsCallbackMutex);
1760 const int ret = apc->removeAudioPortCallback(callback);
Eric Laurente8726fe2015-06-26 09:39:24 -07001761 if (ret == 0) {
1762 aps->setAudioPortCallbacksEnabled(false);
1763 }
1764 return (ret < 0) ? INVALID_OPERATION : NO_ERROR;
Eric Laurent296fb132015-05-01 11:38:42 -07001765}
1766
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001767status_t AudioSystem::addAudioVolumeGroupCallback(const sp<AudioVolumeGroupCallback>& callback) {
Andy Hung264fa4c2024-02-21 15:52:12 -08001768 const sp<IAudioPolicyService> aps = get_audio_policy_service();
François Gaffiecfe17322018-11-07 13:41:29 +01001769 if (aps == 0) return PERMISSION_DENIED;
Andy Hung88e71732024-02-21 16:44:27 -08001770 const auto apc = gAudioPolicyServiceHandler.getClient();
1771 if (apc == nullptr) return NO_INIT;
François Gaffiecfe17322018-11-07 13:41:29 +01001772
Andy Hung88e71732024-02-21 16:44:27 -08001773 std::lock_guard _l(gApsCallbackMutex);
1774 const int ret = apc->addAudioVolumeGroupCallback(callback);
François Gaffiecfe17322018-11-07 13:41:29 +01001775 if (ret == 1) {
1776 aps->setAudioVolumeGroupCallbacksEnabled(true);
1777 }
1778 return (ret < 0) ? INVALID_OPERATION : NO_ERROR;
1779}
1780
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001781status_t AudioSystem::removeAudioVolumeGroupCallback(const sp<AudioVolumeGroupCallback>& callback) {
Andy Hung264fa4c2024-02-21 15:52:12 -08001782 const sp<IAudioPolicyService> aps = get_audio_policy_service();
François Gaffiecfe17322018-11-07 13:41:29 +01001783 if (aps == 0) return PERMISSION_DENIED;
Andy Hung88e71732024-02-21 16:44:27 -08001784 const auto apc = gAudioPolicyServiceHandler.getClient();
1785 if (apc == nullptr) return NO_INIT;
François Gaffiecfe17322018-11-07 13:41:29 +01001786
Andy Hung88e71732024-02-21 16:44:27 -08001787 std::lock_guard _l(gApsCallbackMutex);
1788 const int ret = apc->removeAudioVolumeGroupCallback(callback);
François Gaffiecfe17322018-11-07 13:41:29 +01001789 if (ret == 0) {
1790 aps->setAudioVolumeGroupCallbacksEnabled(false);
1791 }
1792 return (ret < 0) ? INVALID_OPERATION : NO_ERROR;
1793}
1794
Eric Laurent296fb132015-05-01 11:38:42 -07001795status_t AudioSystem::addAudioDeviceCallback(
Eric Laurent09f1ed22019-04-24 17:45:17 -07001796 const wp<AudioDeviceCallback>& callback, audio_io_handle_t audioIo,
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001797 audio_port_handle_t portId) {
Eric Laurent296fb132015-05-01 11:38:42 -07001798 const sp<AudioFlingerClient> afc = getAudioFlingerClient();
1799 if (afc == 0) {
1800 return NO_INIT;
1801 }
Eric Laurent09f1ed22019-04-24 17:45:17 -07001802 status_t status = afc->addAudioDeviceCallback(callback, audioIo, portId);
Eric Laurent7c1ec5f2015-07-09 14:52:47 -07001803 if (status == NO_ERROR) {
Andy Hung264fa4c2024-02-21 15:52:12 -08001804 const sp<IAudioFlinger> af = get_audio_flinger();
Eric Laurent7c1ec5f2015-07-09 14:52:47 -07001805 if (af != 0) {
1806 af->registerClient(afc);
1807 }
1808 }
1809 return status;
Eric Laurent296fb132015-05-01 11:38:42 -07001810}
1811
1812status_t AudioSystem::removeAudioDeviceCallback(
Eric Laurent09f1ed22019-04-24 17:45:17 -07001813 const wp<AudioDeviceCallback>& callback, audio_io_handle_t audioIo,
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001814 audio_port_handle_t portId) {
Eric Laurent296fb132015-05-01 11:38:42 -07001815 const sp<AudioFlingerClient> afc = getAudioFlingerClient();
1816 if (afc == 0) {
1817 return NO_INIT;
1818 }
Eric Laurent09f1ed22019-04-24 17:45:17 -07001819 return afc->removeAudioDeviceCallback(callback, audioIo, portId);
Eric Laurent296fb132015-05-01 11:38:42 -07001820}
1821
Eric Laurent076e7c72022-05-03 18:12:28 +02001822status_t AudioSystem::addSupportedLatencyModesCallback(
1823 const sp<SupportedLatencyModesCallback>& callback) {
1824 const sp<AudioFlingerClient> afc = getAudioFlingerClient();
1825 if (afc == 0) {
1826 return NO_INIT;
1827 }
1828 return afc->addSupportedLatencyModesCallback(callback);
1829}
1830
1831status_t AudioSystem::removeSupportedLatencyModesCallback(
1832 const sp<SupportedLatencyModesCallback>& callback) {
1833 const sp<AudioFlingerClient> afc = getAudioFlingerClient();
1834 if (afc == 0) {
1835 return NO_INIT;
1836 }
1837 return afc->removeSupportedLatencyModesCallback(callback);
1838}
1839
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001840audio_port_handle_t AudioSystem::getDeviceIdForIo(audio_io_handle_t audioIo) {
Andy Hung264fa4c2024-02-21 15:52:12 -08001841 const sp<IAudioFlinger> af = get_audio_flinger();
Eric Laurent296fb132015-05-01 11:38:42 -07001842 if (af == 0) return PERMISSION_DENIED;
1843 const sp<AudioIoDescriptor> desc = getIoDescriptor(audioIo);
1844 if (desc == 0) {
1845 return AUDIO_PORT_HANDLE_NONE;
1846 }
1847 return desc->getDeviceId();
Eric Laurentb28753e2015-04-01 13:06:28 -07001848}
1849
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001850status_t AudioSystem::acquireSoundTriggerSession(audio_session_t* session,
1851 audio_io_handle_t* ioHandle,
1852 audio_devices_t* device) {
1853 if (session == nullptr || ioHandle == nullptr || device == nullptr) {
1854 return BAD_VALUE;
1855 }
Andy Hung264fa4c2024-02-21 15:52:12 -08001856 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Eric Laurentdf3dc7e2014-07-27 18:39:40 -07001857 if (aps == 0) return PERMISSION_DENIED;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001858
1859 media::SoundTriggerSession retAidl;
1860 RETURN_STATUS_IF_ERROR(
1861 statusTFromBinderStatus(aps->acquireSoundTriggerSession(&retAidl)));
1862 *session = VALUE_OR_RETURN_STATUS(aidl2legacy_int32_t_audio_session_t(retAidl.session));
1863 *ioHandle = VALUE_OR_RETURN_STATUS(aidl2legacy_int32_t_audio_io_handle_t(retAidl.ioHandle));
Mikhail Naganov21a32ec2021-07-08 14:40:12 -07001864 *device = VALUE_OR_RETURN_STATUS(
1865 aidl2legacy_AudioDeviceDescription_audio_devices_t(retAidl.device));
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001866 return OK;
Eric Laurentdf3dc7e2014-07-27 18:39:40 -07001867}
1868
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001869status_t AudioSystem::releaseSoundTriggerSession(audio_session_t session) {
Andy Hung264fa4c2024-02-21 15:52:12 -08001870 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Eric Laurentdf3dc7e2014-07-27 18:39:40 -07001871 if (aps == 0) return PERMISSION_DENIED;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001872
1873 int32_t sessionAidl = VALUE_OR_RETURN_STATUS(legacy2aidl_audio_session_t_int32_t(session));
1874 return statusTFromBinderStatus(aps->releaseSoundTriggerSession(sessionAidl));
Eric Laurentdf3dc7e2014-07-27 18:39:40 -07001875}
Eric Laurentbb6c9a02014-09-25 14:11:47 -07001876
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001877audio_mode_t AudioSystem::getPhoneState() {
Andy Hung264fa4c2024-02-21 15:52:12 -08001878 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Eric Laurentbb6c9a02014-09-25 14:11:47 -07001879 if (aps == 0) return AUDIO_MODE_INVALID;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001880
1881 auto result = [&]() -> ConversionResult<audio_mode_t> {
Mikhail Naganovddceecc2021-09-03 13:58:56 -07001882 media::audio::common::AudioMode retAidl;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001883 RETURN_IF_ERROR(statusTFromBinderStatus(aps->getPhoneState(&retAidl)));
1884 return aidl2legacy_AudioMode_audio_mode_t(retAidl);
1885 }();
1886
1887 return result.value_or(AUDIO_MODE_INVALID);
Eric Laurentbb6c9a02014-09-25 14:11:47 -07001888}
1889
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001890status_t AudioSystem::registerPolicyMixes(const Vector<AudioMix>& mixes, bool registration) {
Andy Hung264fa4c2024-02-21 15:52:12 -08001891 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Eric Laurentbaac1832014-12-01 17:52:59 -08001892 if (aps == 0) return PERMISSION_DENIED;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001893
1894 size_t mixesSize = std::min(mixes.size(), size_t{MAX_MIXES_PER_POLICY});
1895 std::vector<media::AudioMix> mixesAidl;
1896 RETURN_STATUS_IF_ERROR(
1897 convertRange(mixes.begin(), mixes.begin() + mixesSize, std::back_inserter(mixesAidl),
1898 legacy2aidl_AudioMix));
1899 return statusTFromBinderStatus(aps->registerPolicyMixes(mixesAidl, registration));
Eric Laurentbaac1832014-12-01 17:52:59 -08001900}
Eric Laurentbb6c9a02014-09-25 14:11:47 -07001901
Marvin Raminbdefaf02023-11-01 09:10:32 +01001902status_t AudioSystem::getRegisteredPolicyMixes(std::vector<AudioMix>& mixes) {
1903 if (!audio_flags::audio_mix_test_api()) {
1904 return INVALID_OPERATION;
1905 }
1906
1907 const sp<IAudioPolicyService> aps = AudioSystem::get_audio_policy_service();
1908 if (aps == nullptr) return PERMISSION_DENIED;
1909
1910 std::vector<::android::media::AudioMix> aidlMixes;
1911 Status status = aps->getRegisteredPolicyMixes(&aidlMixes);
1912
1913 for (const auto& aidlMix : aidlMixes) {
1914 AudioMix mix = VALUE_OR_RETURN_STATUS(aidl2legacy_AudioMix(aidlMix));
1915 mixes.push_back(mix);
1916 }
1917
1918 return statusTFromBinderStatus(status);
1919}
1920
Jan Sebechlebsky0af8e872023-08-11 14:45:08 +02001921status_t AudioSystem::updatePolicyMixes(
1922 const std::vector<std::pair<AudioMix, std::vector<AudioMixMatchCriterion>>>&
1923 mixesWithUpdates) {
Andy Hung264fa4c2024-02-21 15:52:12 -08001924 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Jan Sebechlebsky0af8e872023-08-11 14:45:08 +02001925 if (aps == 0) return PERMISSION_DENIED;
1926
1927 std::vector<media::AudioMixUpdate> updatesAidl;
1928 updatesAidl.reserve(mixesWithUpdates.size());
1929
1930 for (const auto& update : mixesWithUpdates) {
1931 media::AudioMixUpdate updateAidl;
1932 updateAidl.audioMix = VALUE_OR_RETURN_STATUS(legacy2aidl_AudioMix(update.first));
1933 RETURN_STATUS_IF_ERROR(convertRange(update.second.begin(), update.second.end(),
1934 std::back_inserter(updateAidl.newCriteria),
1935 legacy2aidl_AudioMixMatchCriterion));
1936 updatesAidl.emplace_back(updateAidl);
1937 }
1938
1939 return statusTFromBinderStatus(aps->updatePolicyMixes(updatesAidl));
1940}
1941
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001942status_t AudioSystem::setUidDeviceAffinities(uid_t uid, const AudioDeviceTypeAddrVector& devices) {
Andy Hung264fa4c2024-02-21 15:52:12 -08001943 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Jean-Michel Trivibda70da2018-12-19 07:30:15 -08001944 if (aps == 0) return PERMISSION_DENIED;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001945
1946 int32_t uidAidl = VALUE_OR_RETURN_STATUS(legacy2aidl_uid_t_int32_t(uid));
Mikhail Naganovf4a75362021-09-16 00:02:54 +00001947 std::vector<AudioDevice> devicesAidl = VALUE_OR_RETURN_STATUS(
1948 convertContainer<std::vector<AudioDevice>>(devices,
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001949 legacy2aidl_AudioDeviceTypeAddress));
1950 return statusTFromBinderStatus(aps->setUidDeviceAffinities(uidAidl, devicesAidl));
Jean-Michel Trivibda70da2018-12-19 07:30:15 -08001951}
1952
1953status_t AudioSystem::removeUidDeviceAffinities(uid_t uid) {
Andy Hung264fa4c2024-02-21 15:52:12 -08001954 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Jean-Michel Trivibda70da2018-12-19 07:30:15 -08001955 if (aps == 0) return PERMISSION_DENIED;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001956
1957 int32_t uidAidl = VALUE_OR_RETURN_STATUS(legacy2aidl_uid_t_int32_t(uid));
1958 return statusTFromBinderStatus(aps->removeUidDeviceAffinities(uidAidl));
Jean-Michel Trivibda70da2018-12-19 07:30:15 -08001959}
1960
Oscar Azucena90e77632019-11-27 17:12:28 -08001961status_t AudioSystem::setUserIdDeviceAffinities(int userId,
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001962 const AudioDeviceTypeAddrVector& devices) {
Andy Hung264fa4c2024-02-21 15:52:12 -08001963 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Oscar Azucena90e77632019-11-27 17:12:28 -08001964 if (aps == 0) return PERMISSION_DENIED;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001965
1966 int32_t userIdAidl = VALUE_OR_RETURN_STATUS(convertReinterpret<int32_t>(userId));
Mikhail Naganovf4a75362021-09-16 00:02:54 +00001967 std::vector<AudioDevice> devicesAidl = VALUE_OR_RETURN_STATUS(
1968 convertContainer<std::vector<AudioDevice>>(devices,
1969 legacy2aidl_AudioDeviceTypeAddress));
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001970 return statusTFromBinderStatus(
1971 aps->setUserIdDeviceAffinities(userIdAidl, devicesAidl));
Oscar Azucena90e77632019-11-27 17:12:28 -08001972}
1973
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001974status_t AudioSystem::removeUserIdDeviceAffinities(int userId) {
Andy Hung264fa4c2024-02-21 15:52:12 -08001975 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Oscar Azucena90e77632019-11-27 17:12:28 -08001976 if (aps == 0) return PERMISSION_DENIED;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001977 int32_t userIdAidl = VALUE_OR_RETURN_STATUS(convertReinterpret<int32_t>(userId));
1978 return statusTFromBinderStatus(aps->removeUserIdDeviceAffinities(userIdAidl));
Oscar Azucena90e77632019-11-27 17:12:28 -08001979}
1980
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001981status_t AudioSystem::startAudioSource(const struct audio_port_config* source,
1982 const audio_attributes_t* attributes,
1983 audio_port_handle_t* portId) {
1984 if (source == nullptr || attributes == nullptr || portId == nullptr) {
1985 return BAD_VALUE;
1986 }
Andy Hung264fa4c2024-02-21 15:52:12 -08001987 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Eric Laurent554a2772015-04-10 11:29:24 -07001988 if (aps == 0) return PERMISSION_DENIED;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001989
Atneya Nair7a9594f2022-12-18 17:26:26 -08001990 media::AudioPortConfigFw sourceAidl = VALUE_OR_RETURN_STATUS(
Mikhail Naganov87227252023-01-13 17:38:10 -08001991 legacy2aidl_audio_port_config_AudioPortConfigFw(*source));
Mikhail Naganov1c400902023-05-17 11:48:43 -07001992 media::audio::common::AudioAttributes attributesAidl = VALUE_OR_RETURN_STATUS(
1993 legacy2aidl_audio_attributes_t_AudioAttributes(*attributes));
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001994 int32_t portIdAidl;
1995 RETURN_STATUS_IF_ERROR(statusTFromBinderStatus(
1996 aps->startAudioSource(sourceAidl, attributesAidl, &portIdAidl)));
1997 *portId = VALUE_OR_RETURN_STATUS(aidl2legacy_int32_t_audio_port_handle_t(portIdAidl));
1998 return OK;
Eric Laurent554a2772015-04-10 11:29:24 -07001999}
2000
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002001status_t AudioSystem::stopAudioSource(audio_port_handle_t portId) {
Andy Hung264fa4c2024-02-21 15:52:12 -08002002 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Eric Laurent554a2772015-04-10 11:29:24 -07002003 if (aps == 0) return PERMISSION_DENIED;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002004
2005 int32_t portIdAidl = VALUE_OR_RETURN_STATUS(legacy2aidl_audio_port_handle_t_int32_t(portId));
2006 return statusTFromBinderStatus(aps->stopAudioSource(portIdAidl));
Eric Laurent554a2772015-04-10 11:29:24 -07002007}
2008
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002009status_t AudioSystem::setMasterMono(bool mono) {
Andy Hung264fa4c2024-02-21 15:52:12 -08002010 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Andy Hung2ddee192015-12-18 17:34:44 -08002011 if (aps == 0) return PERMISSION_DENIED;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002012 return statusTFromBinderStatus(aps->setMasterMono(mono));
Andy Hung2ddee192015-12-18 17:34:44 -08002013}
2014
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002015status_t AudioSystem::getMasterMono(bool* mono) {
2016 if (mono == nullptr) {
2017 return BAD_VALUE;
2018 }
Andy Hung264fa4c2024-02-21 15:52:12 -08002019 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Andy Hung2ddee192015-12-18 17:34:44 -08002020 if (aps == 0) return PERMISSION_DENIED;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002021 return statusTFromBinderStatus(aps->getMasterMono(mono));
Andy Hung2ddee192015-12-18 17:34:44 -08002022}
2023
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002024status_t AudioSystem::setMasterBalance(float balance) {
Andy Hung264fa4c2024-02-21 15:52:12 -08002025 const sp<IAudioFlinger> af = get_audio_flinger();
Richard Folke Tullberg3fae0372017-01-13 09:04:25 +01002026 if (af == 0) return PERMISSION_DENIED;
2027 return af->setMasterBalance(balance);
2028}
2029
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002030status_t AudioSystem::getMasterBalance(float* balance) {
Andy Hung264fa4c2024-02-21 15:52:12 -08002031 const sp<IAudioFlinger> af = get_audio_flinger();
Richard Folke Tullberg3fae0372017-01-13 09:04:25 +01002032 if (af == 0) return PERMISSION_DENIED;
2033 return af->getMasterBalance(balance);
2034}
2035
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002036float
2037AudioSystem::getStreamVolumeDB(audio_stream_type_t stream, int index, audio_devices_t device) {
Andy Hung264fa4c2024-02-21 15:52:12 -08002038 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Eric Laurentac9cef52017-06-09 15:46:26 -07002039 if (aps == 0) return NAN;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002040
2041 auto result = [&]() -> ConversionResult<float> {
Mikhail Naganovdbf03642021-08-25 18:15:32 -07002042 AudioStreamType streamAidl = VALUE_OR_RETURN(
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002043 legacy2aidl_audio_stream_type_t_AudioStreamType(stream));
2044 int32_t indexAidl = VALUE_OR_RETURN(convertIntegral<int32_t>(index));
Mikhail Naganovf4a75362021-09-16 00:02:54 +00002045 AudioDeviceDescription deviceAidl = VALUE_OR_RETURN(
Mikhail Naganov21a32ec2021-07-08 14:40:12 -07002046 legacy2aidl_audio_devices_t_AudioDeviceDescription(device));
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002047 float retAidl;
2048 RETURN_IF_ERROR(statusTFromBinderStatus(
2049 aps->getStreamVolumeDB(streamAidl, indexAidl, deviceAidl, &retAidl)));
2050 return retAidl;
2051 }();
2052 return result.value_or(NAN);
Eric Laurentac9cef52017-06-09 15:46:26 -07002053}
2054
Mikhail Naganovd5d9de72023-02-13 11:45:03 -08002055status_t AudioSystem::getMicrophones(std::vector<media::MicrophoneInfoFw>* microphones) {
Andy Hung264fa4c2024-02-21 15:52:12 -08002056 const sp<IAudioFlinger> af = get_audio_flinger();
jiabin46a76fa2018-01-05 10:18:21 -08002057 if (af == 0) return PERMISSION_DENIED;
2058 return af->getMicrophones(microphones);
2059}
2060
Eric Laurent42896a02019-09-27 15:40:33 -07002061status_t AudioSystem::setAudioHalPids(const std::vector<pid_t>& pids) {
Andy Hung264fa4c2024-02-21 15:52:12 -08002062 const sp<IAudioFlinger> af = get_audio_flinger();
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002063 if (af == nullptr) return PERMISSION_DENIED;
2064 return af->setAudioHalPids(pids);
Eric Laurent42896a02019-09-27 15:40:33 -07002065}
2066
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002067status_t AudioSystem::getSurroundFormats(unsigned int* numSurroundFormats,
2068 audio_format_t* surroundFormats,
Kriti Dang6537def2021-03-02 13:46:59 +01002069 bool* surroundFormatsEnabled) {
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002070 if (numSurroundFormats == nullptr || (*numSurroundFormats != 0 &&
2071 (surroundFormats == nullptr ||
2072 surroundFormatsEnabled == nullptr))) {
2073 return BAD_VALUE;
2074 }
2075
Andy Hung264fa4c2024-02-21 15:52:12 -08002076 const sp<IAudioPolicyService> aps = get_audio_policy_service();
jiabin81772902018-04-02 17:52:27 -07002077 if (aps == 0) return PERMISSION_DENIED;
Mikhail Naganov0078ee52021-09-30 23:06:20 +00002078 Int numSurroundFormatsAidl;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002079 numSurroundFormatsAidl.value =
2080 VALUE_OR_RETURN_STATUS(convertIntegral<int32_t>(*numSurroundFormats));
Mikhail Naganov57bd06f2021-08-10 16:41:54 -07002081 std::vector<AudioFormatDescription> surroundFormatsAidl;
Kriti Dang877b27e2021-02-02 12:10:40 +01002082 std::vector<bool> surroundFormatsEnabledAidl;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002083 RETURN_STATUS_IF_ERROR(statusTFromBinderStatus(
Kriti Dang6537def2021-03-02 13:46:59 +01002084 aps->getSurroundFormats(&numSurroundFormatsAidl, &surroundFormatsAidl,
2085 &surroundFormatsEnabledAidl)));
Kriti Dang877b27e2021-02-02 12:10:40 +01002086
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002087 *numSurroundFormats = VALUE_OR_RETURN_STATUS(
2088 convertIntegral<unsigned int>(numSurroundFormatsAidl.value));
2089 RETURN_STATUS_IF_ERROR(
2090 convertRange(surroundFormatsAidl.begin(), surroundFormatsAidl.end(), surroundFormats,
Mikhail Naganovb60bd1b2021-07-15 17:31:43 -07002091 aidl2legacy_AudioFormatDescription_audio_format_t));
Kriti Dang877b27e2021-02-02 12:10:40 +01002092 std::copy(surroundFormatsEnabledAidl.begin(), surroundFormatsEnabledAidl.end(),
2093 surroundFormatsEnabled);
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002094 return OK;
jiabin81772902018-04-02 17:52:27 -07002095}
2096
Kriti Dang6537def2021-03-02 13:46:59 +01002097status_t AudioSystem::getReportedSurroundFormats(unsigned int* numSurroundFormats,
2098 audio_format_t* surroundFormats) {
2099 if (numSurroundFormats == nullptr || (*numSurroundFormats != 0 && surroundFormats == nullptr)) {
2100 return BAD_VALUE;
2101 }
2102
Andy Hung264fa4c2024-02-21 15:52:12 -08002103 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Kriti Dang6537def2021-03-02 13:46:59 +01002104 if (aps == 0) return PERMISSION_DENIED;
Mikhail Naganov0078ee52021-09-30 23:06:20 +00002105 Int numSurroundFormatsAidl;
Kriti Dang6537def2021-03-02 13:46:59 +01002106 numSurroundFormatsAidl.value =
2107 VALUE_OR_RETURN_STATUS(convertIntegral<int32_t>(*numSurroundFormats));
Mikhail Naganov57bd06f2021-08-10 16:41:54 -07002108 std::vector<AudioFormatDescription> surroundFormatsAidl;
Kriti Dang6537def2021-03-02 13:46:59 +01002109 RETURN_STATUS_IF_ERROR(statusTFromBinderStatus(
2110 aps->getReportedSurroundFormats(&numSurroundFormatsAidl, &surroundFormatsAidl)));
2111
2112 *numSurroundFormats = VALUE_OR_RETURN_STATUS(
2113 convertIntegral<unsigned int>(numSurroundFormatsAidl.value));
2114 RETURN_STATUS_IF_ERROR(
2115 convertRange(surroundFormatsAidl.begin(), surroundFormatsAidl.end(), surroundFormats,
Mikhail Naganovb60bd1b2021-07-15 17:31:43 -07002116 aidl2legacy_AudioFormatDescription_audio_format_t));
Kriti Dang6537def2021-03-02 13:46:59 +01002117 return OK;
2118}
2119
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002120status_t AudioSystem::setSurroundFormatEnabled(audio_format_t audioFormat, bool enabled) {
Andy Hung264fa4c2024-02-21 15:52:12 -08002121 const sp<IAudioPolicyService> aps = get_audio_policy_service();
jiabin81772902018-04-02 17:52:27 -07002122 if (aps == 0) return PERMISSION_DENIED;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002123
Mikhail Naganov57bd06f2021-08-10 16:41:54 -07002124 AudioFormatDescription audioFormatAidl = VALUE_OR_RETURN_STATUS(
Mikhail Naganovb60bd1b2021-07-15 17:31:43 -07002125 legacy2aidl_audio_format_t_AudioFormatDescription(audioFormat));
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002126 return statusTFromBinderStatus(
2127 aps->setSurroundFormatEnabled(audioFormatAidl, enabled));
jiabin81772902018-04-02 17:52:27 -07002128}
2129
Oscar Azucena829d90d2022-01-28 17:17:56 -08002130status_t AudioSystem::setAssistantServicesUids(const std::vector<uid_t>& uids) {
Andy Hung264fa4c2024-02-21 15:52:12 -08002131 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Eric Laurentb78763e2018-10-17 10:08:02 -07002132 if (aps == 0) return PERMISSION_DENIED;
2133
Oscar Azucena829d90d2022-01-28 17:17:56 -08002134 std::vector<int32_t> uidsAidl = VALUE_OR_RETURN_STATUS(
2135 convertContainer<std::vector<int32_t>>(uids, legacy2aidl_uid_t_int32_t));
2136 return statusTFromBinderStatus(aps->setAssistantServicesUids(uidsAidl));
Ahaan Ugalef51ce002021-08-04 16:34:20 -07002137}
2138
Oscar Azucenac2cdda32022-01-31 19:10:39 -08002139status_t AudioSystem::setActiveAssistantServicesUids(const std::vector<uid_t>& activeUids) {
Andy Hung264fa4c2024-02-21 15:52:12 -08002140 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Oscar Azucenac2cdda32022-01-31 19:10:39 -08002141 if (aps == 0) return PERMISSION_DENIED;
2142
2143 std::vector<int32_t> activeUidsAidl = VALUE_OR_RETURN_STATUS(
2144 convertContainer<std::vector<int32_t>>(activeUids, legacy2aidl_uid_t_int32_t));
2145 return statusTFromBinderStatus(aps->setActiveAssistantServicesUids(activeUidsAidl));
2146}
2147
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002148status_t AudioSystem::setA11yServicesUids(const std::vector<uid_t>& uids) {
Andy Hung264fa4c2024-02-21 15:52:12 -08002149 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Eric Laurentb78763e2018-10-17 10:08:02 -07002150 if (aps == 0) return PERMISSION_DENIED;
2151
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002152 std::vector<int32_t> uidsAidl = VALUE_OR_RETURN_STATUS(
2153 convertContainer<std::vector<int32_t>>(uids, legacy2aidl_uid_t_int32_t));
2154 return statusTFromBinderStatus(aps->setA11yServicesUids(uidsAidl));
Eric Laurentb78763e2018-10-17 10:08:02 -07002155}
2156
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002157status_t AudioSystem::setCurrentImeUid(uid_t uid) {
Andy Hung264fa4c2024-02-21 15:52:12 -08002158 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Kohsuke Yatoha623a132020-03-24 20:10:26 -07002159 if (aps == 0) return PERMISSION_DENIED;
2160
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002161 int32_t uidAidl = VALUE_OR_RETURN_STATUS(legacy2aidl_uid_t_int32_t(uid));
2162 return statusTFromBinderStatus(aps->setCurrentImeUid(uidAidl));
Kohsuke Yatoha623a132020-03-24 20:10:26 -07002163}
2164
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002165bool AudioSystem::isHapticPlaybackSupported() {
Andy Hung264fa4c2024-02-21 15:52:12 -08002166 const sp<IAudioPolicyService> aps = get_audio_policy_service();
jiabin6012f912018-11-02 17:06:30 -07002167 if (aps == 0) return false;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002168
2169 auto result = [&]() -> ConversionResult<bool> {
2170 bool retVal;
2171 RETURN_IF_ERROR(
2172 statusTFromBinderStatus(aps->isHapticPlaybackSupported(&retVal)));
2173 return retVal;
2174 }();
2175 return result.value_or(false);
jiabin6012f912018-11-02 17:06:30 -07002176}
2177
Carter Hsu325a8eb2022-01-19 19:56:51 +08002178bool AudioSystem::isUltrasoundSupported() {
Andy Hung264fa4c2024-02-21 15:52:12 -08002179 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Carter Hsu325a8eb2022-01-19 19:56:51 +08002180 if (aps == 0) return false;
2181
2182 auto result = [&]() -> ConversionResult<bool> {
2183 bool retVal;
2184 RETURN_IF_ERROR(
2185 statusTFromBinderStatus(aps->isUltrasoundSupported(&retVal)));
2186 return retVal;
2187 }();
2188 return result.value_or(false);
2189}
2190
Pattydd807582021-11-04 21:01:03 +08002191status_t AudioSystem::getHwOffloadFormatsSupportedForBluetoothMedia(
2192 audio_devices_t device, std::vector<audio_format_t>* formats) {
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002193 if (formats == nullptr) {
2194 return BAD_VALUE;
2195 }
2196
Andy Hung264fa4c2024-02-21 15:52:12 -08002197 const sp<IAudioPolicyService> aps = get_audio_policy_service();
François Gaffied0ba9ed2018-11-05 11:50:42 +01002198 if (aps == 0) return PERMISSION_DENIED;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002199
Mikhail Naganov57bd06f2021-08-10 16:41:54 -07002200 std::vector<AudioFormatDescription> formatsAidl;
Pattydd807582021-11-04 21:01:03 +08002201 AudioDeviceDescription deviceAidl = VALUE_OR_RETURN_STATUS(
2202 legacy2aidl_audio_devices_t_AudioDeviceDescription(device));
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002203 RETURN_STATUS_IF_ERROR(statusTFromBinderStatus(
Pattydd807582021-11-04 21:01:03 +08002204 aps->getHwOffloadFormatsSupportedForBluetoothMedia(deviceAidl, &formatsAidl)));
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002205 *formats = VALUE_OR_RETURN_STATUS(
Mikhail Naganovb60bd1b2021-07-15 17:31:43 -07002206 convertContainer<std::vector<audio_format_t>>(
2207 formatsAidl,
2208 aidl2legacy_AudioFormatDescription_audio_format_t));
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002209 return OK;
François Gaffied0ba9ed2018-11-05 11:50:42 +01002210}
2211
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002212status_t AudioSystem::listAudioProductStrategies(AudioProductStrategyVector& strategies) {
Andy Hung264fa4c2024-02-21 15:52:12 -08002213 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Arun Mirpuri11029ad2018-12-19 20:45:19 -08002214 if (aps == 0) return PERMISSION_DENIED;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002215
2216 std::vector<media::AudioProductStrategy> strategiesAidl;
2217 RETURN_STATUS_IF_ERROR(statusTFromBinderStatus(
2218 aps->listAudioProductStrategies(&strategiesAidl)));
2219 strategies = VALUE_OR_RETURN_STATUS(
2220 convertContainer<AudioProductStrategyVector>(strategiesAidl,
2221 aidl2legacy_AudioProductStrategy));
2222 return OK;
François Gaffied0ba9ed2018-11-05 11:50:42 +01002223}
2224
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002225audio_attributes_t AudioSystem::streamTypeToAttributes(audio_stream_type_t stream) {
François Gaffied0ba9ed2018-11-05 11:50:42 +01002226 AudioProductStrategyVector strategies;
2227 listAudioProductStrategies(strategies);
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002228 for (const auto& strategy : strategies) {
François Gaffie1e2b56f2022-04-01 14:34:29 +02002229 auto attrVect = strategy.getVolumeGroupAttributes();
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002230 auto iter = std::find_if(begin(attrVect), end(attrVect), [&stream](const auto& attributes) {
2231 return attributes.getStreamType() == stream;
2232 });
François Gaffied0ba9ed2018-11-05 11:50:42 +01002233 if (iter != end(attrVect)) {
2234 return iter->getAttributes();
2235 }
2236 }
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002237 ALOGE("invalid stream type %s when converting to attributes", toString(stream).c_str());
François Gaffied0ba9ed2018-11-05 11:50:42 +01002238 return AUDIO_ATTRIBUTES_INITIALIZER;
2239}
2240
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002241audio_stream_type_t AudioSystem::attributesToStreamType(const audio_attributes_t& attr) {
François Gaffie4b2018b2018-11-07 11:18:59 +01002242 product_strategy_t psId;
François Gaffie1e2b56f2022-04-01 14:34:29 +02002243 status_t ret = AudioSystem::getProductStrategyFromAudioAttributes(attr, psId);
François Gaffie4b2018b2018-11-07 11:18:59 +01002244 if (ret != NO_ERROR) {
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002245 ALOGE("no strategy found for attributes %s", toString(attr).c_str());
François Gaffie4b2018b2018-11-07 11:18:59 +01002246 return AUDIO_STREAM_MUSIC;
2247 }
François Gaffied0ba9ed2018-11-05 11:50:42 +01002248 AudioProductStrategyVector strategies;
2249 listAudioProductStrategies(strategies);
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002250 for (const auto& strategy : strategies) {
François Gaffie4b2018b2018-11-07 11:18:59 +01002251 if (strategy.getId() == psId) {
François Gaffie1e2b56f2022-04-01 14:34:29 +02002252 auto attrVect = strategy.getVolumeGroupAttributes();
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002253 auto iter = std::find_if(begin(attrVect), end(attrVect), [&attr](const auto& refAttr) {
François Gaffie8d7fd5a2023-01-17 17:28:09 +01002254 return refAttr.matchesScore(attr) > 0;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002255 });
François Gaffied0ba9ed2018-11-05 11:50:42 +01002256 if (iter != end(attrVect)) {
2257 return iter->getStreamType();
2258 }
2259 }
2260 }
Jean-Michel Trivied678652019-12-19 13:39:30 -08002261 switch (attr.usage) {
2262 case AUDIO_USAGE_VIRTUAL_SOURCE:
2263 // virtual source is not expected to have an associated product strategy
2264 break;
2265 default:
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002266 ALOGE("invalid attributes %s when converting to stream", toString(attr).c_str());
Jean-Michel Trivied678652019-12-19 13:39:30 -08002267 break;
2268 }
François Gaffied0ba9ed2018-11-05 11:50:42 +01002269 return AUDIO_STREAM_MUSIC;
2270}
2271
François Gaffie1e2b56f2022-04-01 14:34:29 +02002272status_t AudioSystem::getProductStrategyFromAudioAttributes(const audio_attributes_t& aa,
Francois Gaffie11b65922020-09-24 16:59:08 +02002273 product_strategy_t& productStrategy,
2274 bool fallbackOnDefault) {
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
Mikhail Naganov1c400902023-05-17 11:48:43 -07002278 media::audio::common::AudioAttributes aaAidl = VALUE_OR_RETURN_STATUS(
2279 legacy2aidl_audio_attributes_t_AudioAttributes(aa));
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002280 int32_t productStrategyAidl;
2281
2282 RETURN_STATUS_IF_ERROR(statusTFromBinderStatus(
Francois Gaffie11b65922020-09-24 16:59:08 +02002283 aps->getProductStrategyFromAudioAttributes(aaAidl, fallbackOnDefault,
2284 &productStrategyAidl)));
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002285 productStrategy = VALUE_OR_RETURN_STATUS(
2286 aidl2legacy_int32_t_product_strategy_t(productStrategyAidl));
2287 return OK;
François Gaffie4b2018b2018-11-07 11:18:59 +01002288}
2289
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002290status_t AudioSystem::listAudioVolumeGroups(AudioVolumeGroupVector& groups) {
Andy Hung264fa4c2024-02-21 15:52:12 -08002291 const sp<IAudioPolicyService> aps = get_audio_policy_service();
François Gaffie4b2018b2018-11-07 11:18:59 +01002292 if (aps == 0) return PERMISSION_DENIED;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002293
2294 std::vector<media::AudioVolumeGroup> groupsAidl;
2295 RETURN_STATUS_IF_ERROR(
2296 statusTFromBinderStatus(aps->listAudioVolumeGroups(&groupsAidl)));
2297 groups = VALUE_OR_RETURN_STATUS(
2298 convertContainer<AudioVolumeGroupVector>(groupsAidl, aidl2legacy_AudioVolumeGroup));
2299 return OK;
François Gaffie4b2018b2018-11-07 11:18:59 +01002300}
2301
François Gaffie1e2b56f2022-04-01 14:34:29 +02002302status_t AudioSystem::getVolumeGroupFromAudioAttributes(const audio_attributes_t &aa,
Francois Gaffie11b65922020-09-24 16:59:08 +02002303 volume_group_t& volumeGroup,
2304 bool fallbackOnDefault) {
Andy Hung264fa4c2024-02-21 15:52:12 -08002305 const sp<IAudioPolicyService> aps = get_audio_policy_service();
François Gaffie4b2018b2018-11-07 11:18:59 +01002306 if (aps == 0) return PERMISSION_DENIED;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002307
Mikhail Naganov1c400902023-05-17 11:48:43 -07002308 media::audio::common::AudioAttributes aaAidl = VALUE_OR_RETURN_STATUS(
2309 legacy2aidl_audio_attributes_t_AudioAttributes(aa));
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002310 int32_t volumeGroupAidl;
2311 RETURN_STATUS_IF_ERROR(statusTFromBinderStatus(
Francois Gaffie11b65922020-09-24 16:59:08 +02002312 aps->getVolumeGroupFromAudioAttributes(aaAidl, fallbackOnDefault, &volumeGroupAidl)));
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002313 volumeGroup = VALUE_OR_RETURN_STATUS(aidl2legacy_int32_t_volume_group_t(volumeGroupAidl));
2314 return OK;
Arun Mirpuri11029ad2018-12-19 20:45:19 -08002315}
Eric Laurentb78763e2018-10-17 10:08:02 -07002316
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002317status_t AudioSystem::setRttEnabled(bool enabled) {
Andy Hung264fa4c2024-02-21 15:52:12 -08002318 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Eric Laurent6ede98f2019-06-11 14:50:30 -07002319 if (aps == 0) return PERMISSION_DENIED;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002320 return statusTFromBinderStatus(aps->setRttEnabled(enabled));
Eric Laurent6ede98f2019-06-11 14:50:30 -07002321}
2322
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002323bool AudioSystem::isCallScreenModeSupported() {
Andy Hung264fa4c2024-02-21 15:52:12 -08002324 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Eric Laurent8340e672019-11-06 11:01:08 -08002325 if (aps == 0) return false;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002326
2327 auto result = [&]() -> ConversionResult<bool> {
2328 bool retAidl;
2329 RETURN_IF_ERROR(
2330 statusTFromBinderStatus(aps->isCallScreenModeSupported(&retAidl)));
2331 return retAidl;
2332 }();
2333 return result.value_or(false);
Eric Laurent8340e672019-11-06 11:01:08 -08002334}
2335
jiabin0a488932020-08-07 17:32:40 -07002336status_t AudioSystem::setDevicesRoleForStrategy(product_strategy_t strategy,
2337 device_role_t role,
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002338 const AudioDeviceTypeAddrVector& devices) {
Andy Hung264fa4c2024-02-21 15:52:12 -08002339 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Jean-Michel Trivi30857152019-11-01 11:04:15 -07002340 if (aps == 0) {
2341 return PERMISSION_DENIED;
2342 }
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002343
2344 int32_t strategyAidl = VALUE_OR_RETURN_STATUS(legacy2aidl_product_strategy_t_int32_t(strategy));
2345 media::DeviceRole roleAidl = VALUE_OR_RETURN_STATUS(legacy2aidl_device_role_t_DeviceRole(role));
Mikhail Naganovf4a75362021-09-16 00:02:54 +00002346 std::vector<AudioDevice> devicesAidl = VALUE_OR_RETURN_STATUS(
2347 convertContainer<std::vector<AudioDevice>>(devices,
2348 legacy2aidl_AudioDeviceTypeAddress));
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002349 return statusTFromBinderStatus(
2350 aps->setDevicesRoleForStrategy(strategyAidl, roleAidl, devicesAidl));
Jean-Michel Trivi30857152019-11-01 11:04:15 -07002351}
2352
Paul Wang5d7cdb52022-11-22 09:45:06 +00002353status_t AudioSystem::removeDevicesRoleForStrategy(product_strategy_t strategy,
2354 device_role_t role,
2355 const AudioDeviceTypeAddrVector& devices) {
Andy Hung264fa4c2024-02-21 15:52:12 -08002356 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Paul Wang5d7cdb52022-11-22 09:45:06 +00002357 if (aps == 0) {
2358 return PERMISSION_DENIED;
2359 }
2360
2361 int32_t strategyAidl = VALUE_OR_RETURN_STATUS(legacy2aidl_product_strategy_t_int32_t(strategy));
2362 media::DeviceRole roleAidl = VALUE_OR_RETURN_STATUS(legacy2aidl_device_role_t_DeviceRole(role));
2363 std::vector<AudioDevice> devicesAidl = VALUE_OR_RETURN_STATUS(
2364 convertContainer<std::vector<AudioDevice>>(devices,
2365 legacy2aidl_AudioDeviceTypeAddress));
2366 return statusTFromBinderStatus(
2367 aps->removeDevicesRoleForStrategy(strategyAidl, roleAidl, devicesAidl));
2368}
2369
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002370status_t
Paul Wang5d7cdb52022-11-22 09:45:06 +00002371AudioSystem::clearDevicesRoleForStrategy(product_strategy_t strategy, device_role_t role) {
Andy Hung264fa4c2024-02-21 15:52:12 -08002372 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Jean-Michel Trivi30857152019-11-01 11:04:15 -07002373 if (aps == 0) {
2374 return PERMISSION_DENIED;
2375 }
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002376 int32_t strategyAidl = VALUE_OR_RETURN_STATUS(legacy2aidl_product_strategy_t_int32_t(strategy));
2377 media::DeviceRole roleAidl = VALUE_OR_RETURN_STATUS(legacy2aidl_device_role_t_DeviceRole(role));
2378 return statusTFromBinderStatus(
Paul Wang5d7cdb52022-11-22 09:45:06 +00002379 aps->clearDevicesRoleForStrategy(strategyAidl, roleAidl));
Jean-Michel Trivi30857152019-11-01 11:04:15 -07002380}
2381
jiabin0a488932020-08-07 17:32:40 -07002382status_t AudioSystem::getDevicesForRoleAndStrategy(product_strategy_t strategy,
2383 device_role_t role,
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002384 AudioDeviceTypeAddrVector& devices) {
Andy Hung264fa4c2024-02-21 15:52:12 -08002385 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Jean-Michel Trivi30857152019-11-01 11:04:15 -07002386 if (aps == 0) {
2387 return PERMISSION_DENIED;
2388 }
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002389 int32_t strategyAidl = VALUE_OR_RETURN_STATUS(legacy2aidl_product_strategy_t_int32_t(strategy));
2390 media::DeviceRole roleAidl = VALUE_OR_RETURN_STATUS(legacy2aidl_device_role_t_DeviceRole(role));
Mikhail Naganovf4a75362021-09-16 00:02:54 +00002391 std::vector<AudioDevice> devicesAidl;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002392 RETURN_STATUS_IF_ERROR(statusTFromBinderStatus(
2393 aps->getDevicesForRoleAndStrategy(strategyAidl, roleAidl, &devicesAidl)));
2394 devices = VALUE_OR_RETURN_STATUS(
2395 convertContainer<AudioDeviceTypeAddrVector>(devicesAidl,
2396 aidl2legacy_AudioDeviceTypeAddress));
2397 return OK;
Jean-Michel Trivi30857152019-11-01 11:04:15 -07002398}
2399
Jiabin Huang3b98d322020-09-03 17:54:16 +00002400status_t AudioSystem::setDevicesRoleForCapturePreset(audio_source_t audioSource,
2401 device_role_t role,
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002402 const AudioDeviceTypeAddrVector& devices) {
Andy Hung264fa4c2024-02-21 15:52:12 -08002403 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Jiabin Huang3b98d322020-09-03 17:54:16 +00002404 if (aps == 0) {
2405 return PERMISSION_DENIED;
2406 }
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002407
Mikhail Naganovddceecc2021-09-03 13:58:56 -07002408 AudioSource audioSourceAidl = VALUE_OR_RETURN_STATUS(
2409 legacy2aidl_audio_source_t_AudioSource(audioSource));
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002410 media::DeviceRole roleAidl = VALUE_OR_RETURN_STATUS(legacy2aidl_device_role_t_DeviceRole(role));
Mikhail Naganovf4a75362021-09-16 00:02:54 +00002411 std::vector<AudioDevice> devicesAidl = VALUE_OR_RETURN_STATUS(
2412 convertContainer<std::vector<AudioDevice>>(devices,
2413 legacy2aidl_AudioDeviceTypeAddress));
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002414 return statusTFromBinderStatus(
2415 aps->setDevicesRoleForCapturePreset(audioSourceAidl, roleAidl, devicesAidl));
Jiabin Huang3b98d322020-09-03 17:54:16 +00002416}
2417
2418status_t AudioSystem::addDevicesRoleForCapturePreset(audio_source_t audioSource,
2419 device_role_t role,
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002420 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->addDevicesRoleForCapturePreset(audioSourceAidl, roleAidl, devicesAidl));
Jiabin Huang3b98d322020-09-03 17:54:16 +00002433}
2434
2435status_t AudioSystem::removeDevicesRoleForCapturePreset(
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002436 audio_source_t audioSource, device_role_t role, const AudioDeviceTypeAddrVector& devices) {
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));
Mikhail Naganovf4a75362021-09-16 00:02:54 +00002444 std::vector<AudioDevice> devicesAidl = VALUE_OR_RETURN_STATUS(
2445 convertContainer<std::vector<AudioDevice>>(devices,
2446 legacy2aidl_AudioDeviceTypeAddress));
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002447 return statusTFromBinderStatus(
2448 aps->removeDevicesRoleForCapturePreset(audioSourceAidl, roleAidl, devicesAidl));
Jiabin Huang3b98d322020-09-03 17:54:16 +00002449}
2450
2451status_t AudioSystem::clearDevicesRoleForCapturePreset(audio_source_t audioSource,
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002452 device_role_t role) {
Andy Hung264fa4c2024-02-21 15:52:12 -08002453 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Jiabin Huang3b98d322020-09-03 17:54:16 +00002454 if (aps == 0) {
2455 return PERMISSION_DENIED;
2456 }
Mikhail Naganovddceecc2021-09-03 13:58:56 -07002457 AudioSource audioSourceAidl = VALUE_OR_RETURN_STATUS(
2458 legacy2aidl_audio_source_t_AudioSource(audioSource));
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002459 media::DeviceRole roleAidl = VALUE_OR_RETURN_STATUS(legacy2aidl_device_role_t_DeviceRole(role));
2460 return statusTFromBinderStatus(
2461 aps->clearDevicesRoleForCapturePreset(audioSourceAidl, roleAidl));
Jiabin Huang3b98d322020-09-03 17:54:16 +00002462}
2463
2464status_t AudioSystem::getDevicesForRoleAndCapturePreset(audio_source_t audioSource,
2465 device_role_t role,
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002466 AudioDeviceTypeAddrVector& devices) {
Andy Hung264fa4c2024-02-21 15:52:12 -08002467 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Jiabin Huang3b98d322020-09-03 17:54:16 +00002468 if (aps == 0) {
2469 return PERMISSION_DENIED;
2470 }
Mikhail Naganovddceecc2021-09-03 13:58:56 -07002471 AudioSource audioSourceAidl = VALUE_OR_RETURN_STATUS(
2472 legacy2aidl_audio_source_t_AudioSource(audioSource));
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002473 media::DeviceRole roleAidl = VALUE_OR_RETURN_STATUS(legacy2aidl_device_role_t_DeviceRole(role));
Mikhail Naganovf4a75362021-09-16 00:02:54 +00002474 std::vector<AudioDevice> devicesAidl;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002475 RETURN_STATUS_IF_ERROR(statusTFromBinderStatus(
2476 aps->getDevicesForRoleAndCapturePreset(audioSourceAidl, roleAidl, &devicesAidl)));
2477 devices = VALUE_OR_RETURN_STATUS(
2478 convertContainer<AudioDeviceTypeAddrVector>(devicesAidl,
2479 aidl2legacy_AudioDeviceTypeAddress));
2480 return OK;
Jiabin Huang3b98d322020-09-03 17:54:16 +00002481}
2482
Eric Laurent81dd0f52021-07-05 11:54:40 +02002483status_t AudioSystem::getSpatializer(const sp<media::INativeSpatializerCallback>& callback,
2484 sp<media::ISpatializer>* spatializer) {
Andy Hung264fa4c2024-02-21 15:52:12 -08002485 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Eric Laurent81dd0f52021-07-05 11:54:40 +02002486 if (spatializer == nullptr) {
2487 return BAD_VALUE;
2488 }
2489 if (aps == 0) {
2490 return PERMISSION_DENIED;
2491 }
2492 media::GetSpatializerResponse response;
2493 RETURN_STATUS_IF_ERROR(statusTFromBinderStatus(
2494 aps->getSpatializer(callback, &response)));
2495
2496 *spatializer = response.spatializer;
2497 return OK;
2498}
2499
2500status_t AudioSystem::canBeSpatialized(const audio_attributes_t *attr,
2501 const audio_config_t *config,
2502 const AudioDeviceTypeAddrVector &devices,
2503 bool *canBeSpatialized) {
Andy Hung264fa4c2024-02-21 15:52:12 -08002504 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Ram Mohan43297012022-08-30 16:11:49 +05302505 if (canBeSpatialized == nullptr) {
2506 return BAD_VALUE;
2507 }
Eric Laurent81dd0f52021-07-05 11:54:40 +02002508 if (aps == 0) {
2509 return PERMISSION_DENIED;
2510 }
2511 audio_attributes_t attributes = attr != nullptr ? *attr : AUDIO_ATTRIBUTES_INITIALIZER;
2512 audio_config_t configuration = config != nullptr ? *config : AUDIO_CONFIG_INITIALIZER;
2513
Mikhail Naganov1c400902023-05-17 11:48:43 -07002514 std::optional<media::audio::common::AudioAttributes> attrAidl = VALUE_OR_RETURN_STATUS(
2515 legacy2aidl_audio_attributes_t_AudioAttributes(attributes));
Mikhail Naganovdbf03642021-08-25 18:15:32 -07002516 std::optional<AudioConfig> configAidl = VALUE_OR_RETURN_STATUS(
Eric Laurent81dd0f52021-07-05 11:54:40 +02002517 legacy2aidl_audio_config_t_AudioConfig(configuration, false /*isInput*/));
Mikhail Naganovf4a75362021-09-16 00:02:54 +00002518 std::vector<AudioDevice> devicesAidl = VALUE_OR_RETURN_STATUS(
2519 convertContainer<std::vector<AudioDevice>>(devices,
2520 legacy2aidl_AudioDeviceTypeAddress));
Eric Laurent81dd0f52021-07-05 11:54:40 +02002521 RETURN_STATUS_IF_ERROR(statusTFromBinderStatus(
2522 aps->canBeSpatialized(attrAidl, configAidl, devicesAidl, canBeSpatialized)));
2523 return OK;
2524}
2525
Vlad Popae3fd1c22022-11-07 21:03:18 +01002526status_t AudioSystem::getSoundDoseInterface(const sp<media::ISoundDoseCallback>& callback,
2527 sp<media::ISoundDose>* soundDose) {
Andy Hung264fa4c2024-02-21 15:52:12 -08002528 const sp<IAudioFlinger> af = get_audio_flinger();
Vlad Popa63f047e2022-11-05 14:09:19 +01002529 if (af == nullptr) {
2530 return PERMISSION_DENIED;
2531 }
Vlad Popae3fd1c22022-11-07 21:03:18 +01002532 if (soundDose == nullptr) {
2533 return BAD_VALUE;
2534 }
Vlad Popa63f047e2022-11-05 14:09:19 +01002535
Vlad Popae3fd1c22022-11-07 21:03:18 +01002536 RETURN_STATUS_IF_ERROR(af->getSoundDoseInterface(callback, soundDose));
2537 return OK;
Vlad Popa63f047e2022-11-05 14:09:19 +01002538}
2539
jiabin2b9d5a12021-12-10 01:06:29 +00002540status_t AudioSystem::getDirectPlaybackSupport(const audio_attributes_t *attr,
2541 const audio_config_t *config,
2542 audio_direct_mode_t* directMode) {
2543 if (attr == nullptr || config == nullptr || directMode == nullptr) {
2544 return BAD_VALUE;
2545 }
2546
Andy Hung264fa4c2024-02-21 15:52:12 -08002547 const sp<IAudioPolicyService> aps = get_audio_policy_service();
jiabin2b9d5a12021-12-10 01:06:29 +00002548 if (aps == 0) {
2549 return PERMISSION_DENIED;
2550 }
2551
Mikhail Naganov1c400902023-05-17 11:48:43 -07002552 media::audio::common::AudioAttributes attrAidl = VALUE_OR_RETURN_STATUS(
2553 legacy2aidl_audio_attributes_t_AudioAttributes(*attr));
jiabin2b9d5a12021-12-10 01:06:29 +00002554 AudioConfig configAidl = VALUE_OR_RETURN_STATUS(
2555 legacy2aidl_audio_config_t_AudioConfig(*config, false /*isInput*/));
2556
2557 media::AudioDirectMode retAidl;
2558 RETURN_STATUS_IF_ERROR(statusTFromBinderStatus(
2559 aps->getDirectPlaybackSupport(attrAidl, configAidl, &retAidl)));
2560 *directMode = VALUE_OR_RETURN_STATUS(aidl2legacy_int32_t_audio_direct_mode_t_mask(
2561 static_cast<int32_t>(retAidl)));
2562 return NO_ERROR;
2563}
2564
Dorin Drimusf2196d82022-01-03 12:11:18 +01002565status_t AudioSystem::getDirectProfilesForAttributes(const audio_attributes_t* attr,
2566 std::vector<audio_profile>* audioProfiles) {
Mikhail Naganovefc504b2022-06-22 03:24:59 +00002567 if (attr == nullptr || audioProfiles == nullptr) {
Dorin Drimusf2196d82022-01-03 12:11:18 +01002568 return BAD_VALUE;
2569 }
2570
Andy Hung264fa4c2024-02-21 15:52:12 -08002571 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Dorin Drimusf2196d82022-01-03 12:11:18 +01002572 if (aps == 0) {
2573 return PERMISSION_DENIED;
2574 }
2575
Mikhail Naganov1c400902023-05-17 11:48:43 -07002576 media::audio::common::AudioAttributes attrAidl = VALUE_OR_RETURN_STATUS(
2577 legacy2aidl_audio_attributes_t_AudioAttributes(*attr));
Dorin Drimusf2196d82022-01-03 12:11:18 +01002578
2579 std::vector<media::audio::common::AudioProfile> audioProfilesAidl;
2580 RETURN_STATUS_IF_ERROR(statusTFromBinderStatus(
2581 aps->getDirectProfilesForAttributes(attrAidl, &audioProfilesAidl)));
2582 *audioProfiles = VALUE_OR_RETURN_STATUS(convertContainer<std::vector<audio_profile>>(
2583 audioProfilesAidl, aidl2legacy_AudioProfile_audio_profile, false /*isInput*/));
2584
2585 return NO_ERROR;
2586}
Eric Laurent81dd0f52021-07-05 11:54:40 +02002587
Eric Laurent076e7c72022-05-03 18:12:28 +02002588status_t AudioSystem::setRequestedLatencyMode(
2589 audio_io_handle_t output, audio_latency_mode_t mode) {
Andy Hung264fa4c2024-02-21 15:52:12 -08002590 const sp<IAudioFlinger> af = get_audio_flinger();
Eric Laurent076e7c72022-05-03 18:12:28 +02002591 if (af == nullptr) {
2592 return PERMISSION_DENIED;
2593 }
2594 return af->setRequestedLatencyMode(output, mode);
2595}
2596
2597status_t AudioSystem::getSupportedLatencyModes(audio_io_handle_t output,
2598 std::vector<audio_latency_mode_t>* modes) {
Andy Hung264fa4c2024-02-21 15:52:12 -08002599 const sp<IAudioFlinger> af = get_audio_flinger();
Eric Laurent076e7c72022-05-03 18:12:28 +02002600 if (af == nullptr) {
2601 return PERMISSION_DENIED;
2602 }
2603 return af->getSupportedLatencyModes(output, modes);
2604}
2605
Eric Laurent50d72582022-12-20 20:20:23 +01002606status_t AudioSystem::setBluetoothVariableLatencyEnabled(bool enabled) {
Andy Hung264fa4c2024-02-21 15:52:12 -08002607 const sp<IAudioFlinger> af = get_audio_flinger();
Eric Laurent52057642022-12-16 11:45:07 +01002608 if (af == nullptr) {
2609 return PERMISSION_DENIED;
2610 }
Eric Laurent50d72582022-12-20 20:20:23 +01002611 return af->setBluetoothVariableLatencyEnabled(enabled);
Eric Laurent52057642022-12-16 11:45:07 +01002612}
2613
Eric Laurent50d72582022-12-20 20:20:23 +01002614status_t AudioSystem::isBluetoothVariableLatencyEnabled(
2615 bool *enabled) {
Andy Hung264fa4c2024-02-21 15:52:12 -08002616 const sp<IAudioFlinger> af = get_audio_flinger();
Eric Laurent50d72582022-12-20 20:20:23 +01002617 if (af == nullptr) {
2618 return PERMISSION_DENIED;
2619 }
2620 return af->isBluetoothVariableLatencyEnabled(enabled);
2621}
2622
2623status_t AudioSystem::supportsBluetoothVariableLatency(
Eric Laurent52057642022-12-16 11:45:07 +01002624 bool *support) {
Andy Hung264fa4c2024-02-21 15:52:12 -08002625 const sp<IAudioFlinger> af = get_audio_flinger();
Eric Laurent52057642022-12-16 11:45:07 +01002626 if (af == nullptr) {
2627 return PERMISSION_DENIED;
2628 }
Eric Laurent50d72582022-12-20 20:20:23 +01002629 return af->supportsBluetoothVariableLatency(support);
Eric Laurent52057642022-12-16 11:45:07 +01002630}
2631
Mikhail Naganovffd97712023-05-03 17:45:36 -07002632status_t AudioSystem::getAudioPolicyConfig(media::AudioPolicyConfig *config) {
Andy Hung264fa4c2024-02-21 15:52:12 -08002633 const sp<IAudioFlinger> af = get_audio_flinger();
Mikhail Naganovffd97712023-05-03 17:45:36 -07002634 if (af == nullptr) {
2635 return PERMISSION_DENIED;
2636 }
2637 return af->getAudioPolicyConfig(config);
2638}
2639
Ytai Ben-Tsvi5120eda2020-03-26 09:41:15 -07002640class CaptureStateListenerImpl : public media::BnCaptureStateListener,
2641 public IBinder::DeathRecipient {
2642public:
Ytai Ben-Tsvid9f82832020-08-27 09:31:10 -07002643 CaptureStateListenerImpl(
2644 const sp<IAudioPolicyService>& aps,
2645 const sp<AudioSystem::CaptureStateListener>& listener)
Ytai Ben-Tsvia46b6d32020-08-31 13:29:11 -07002646 : mAps(aps), mListener(listener) {}
2647
2648 void init() {
Ytai Ben-Tsvid9f82832020-08-27 09:31:10 -07002649 bool active;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002650 status_t status = statusTFromBinderStatus(
2651 mAps->registerSoundTriggerCaptureStateListener(this, &active));
Ytai Ben-Tsvid9f82832020-08-27 09:31:10 -07002652 if (status != NO_ERROR) {
2653 mListener->onServiceDied();
2654 return;
2655 }
2656 mListener->onStateChanged(active);
Ytai Ben-Tsvia46b6d32020-08-31 13:29:11 -07002657 IInterface::asBinder(mAps)->linkToDeath(this);
Ytai Ben-Tsvid9f82832020-08-27 09:31:10 -07002658 }
2659
Ytai Ben-Tsvi5120eda2020-03-26 09:41:15 -07002660 binder::Status setCaptureState(bool active) override {
Andy Hungacd955a2024-02-16 17:14:36 -08002661 std::lock_guard _l(AudioSystem::gSoundTriggerMutex);
Ytai Ben-Tsvid9f82832020-08-27 09:31:10 -07002662 mListener->onStateChanged(active);
Ytai Ben-Tsvi5120eda2020-03-26 09:41:15 -07002663 return binder::Status::ok();
2664 }
2665
2666 void binderDied(const wp<IBinder>&) override {
Andy Hungacd955a2024-02-16 17:14:36 -08002667 std::lock_guard _l(AudioSystem::gSoundTriggerMutex);
Ytai Ben-Tsvid9f82832020-08-27 09:31:10 -07002668 mListener->onServiceDied();
Andy Hungacd955a2024-02-16 17:14:36 -08002669 AudioSystem::gSoundTriggerCaptureStateListener = nullptr;
Ytai Ben-Tsvi5120eda2020-03-26 09:41:15 -07002670 }
Ytai Ben-Tsvid9f82832020-08-27 09:31:10 -07002671
2672private:
2673 // Need this in order to keep the death receipent alive.
2674 sp<IAudioPolicyService> mAps;
2675 sp<AudioSystem::CaptureStateListener> mListener;
Ytai Ben-Tsvi5120eda2020-03-26 09:41:15 -07002676};
2677
2678status_t AudioSystem::registerSoundTriggerCaptureStateListener(
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002679 const sp<CaptureStateListener>& listener) {
Ytai Ben-Tsvid9f82832020-08-27 09:31:10 -07002680 LOG_ALWAYS_FATAL_IF(listener == nullptr);
2681
Andy Hung264fa4c2024-02-21 15:52:12 -08002682 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Ytai Ben-Tsvi5120eda2020-03-26 09:41:15 -07002683 if (aps == 0) {
2684 return PERMISSION_DENIED;
2685 }
2686
Andy Hungacd955a2024-02-16 17:14:36 -08002687 std::lock_guard _l(AudioSystem::gSoundTriggerMutex);
Ytai Ben-Tsvid9f82832020-08-27 09:31:10 -07002688 gSoundTriggerCaptureStateListener = new CaptureStateListenerImpl(aps, listener);
Ytai Ben-Tsvia46b6d32020-08-31 13:29:11 -07002689 gSoundTriggerCaptureStateListener->init();
Ytai Ben-Tsvi5120eda2020-03-26 09:41:15 -07002690
Ytai Ben-Tsvi5120eda2020-03-26 09:41:15 -07002691 return NO_ERROR;
2692}
2693
jiabin1319f5a2021-03-30 22:21:24 +00002694status_t AudioSystem::setVibratorInfos(
2695 const std::vector<media::AudioVibratorInfo>& vibratorInfos) {
Andy Hung264fa4c2024-02-21 15:52:12 -08002696 const sp<IAudioFlinger> af = get_audio_flinger();
jiabin1319f5a2021-03-30 22:21:24 +00002697 if (af == nullptr) {
2698 return PERMISSION_DENIED;
2699 }
2700 return af->setVibratorInfos(vibratorInfos);
2701}
2702
Jiabin Huangaa6e9e32024-10-21 17:19:28 +00002703status_t AudioSystem::getMmapPolicyInfos(
jiabine99d0882021-09-17 05:21:25 +00002704 AudioMMapPolicyType policyType, std::vector<AudioMMapPolicyInfo> *policyInfos) {
Jiabin Huangaa6e9e32024-10-21 17:19:28 +00002705 const sp<IAudioPolicyService> aps = get_audio_policy_service();
2706 if (aps == nullptr) {
Jiabin Huangebe64102021-09-07 20:01:07 +00002707 return PERMISSION_DENIED;
2708 }
Jiabin Huangaa6e9e32024-10-21 17:19:28 +00002709 return statusTFromBinderStatus(aps->getMmapPolicyInfos(policyType, policyInfos));
Jiabin Huangebe64102021-09-07 20:01:07 +00002710}
2711
jiabine504e7b2021-09-18 00:27:08 +00002712int32_t AudioSystem::getAAudioMixerBurstCount() {
Andy Hung264fa4c2024-02-21 15:52:12 -08002713 const sp<IAudioFlinger> af = get_audio_flinger();
jiabine504e7b2021-09-18 00:27:08 +00002714 if (af == nullptr) {
2715 return PERMISSION_DENIED;
2716 }
2717 return af->getAAudioMixerBurstCount();
2718}
2719
2720int32_t AudioSystem::getAAudioHardwareBurstMinUsec() {
Andy Hung264fa4c2024-02-21 15:52:12 -08002721 const sp<IAudioFlinger> af = get_audio_flinger();
jiabine504e7b2021-09-18 00:27:08 +00002722 if (af == nullptr) {
2723 return PERMISSION_DENIED;
2724 }
2725 return af->getAAudioHardwareBurstMinUsec();
2726}
2727
jiabina84c3d32022-12-02 18:59:55 +00002728status_t AudioSystem::getSupportedMixerAttributes(
2729 audio_port_handle_t portId, std::vector<audio_mixer_attributes_t> *mixerAttrs) {
Andy Hung264fa4c2024-02-21 15:52:12 -08002730 const sp<IAudioPolicyService> aps = get_audio_policy_service();
jiabina84c3d32022-12-02 18:59:55 +00002731 if (aps == nullptr) {
2732 return PERMISSION_DENIED;
2733 }
2734
2735 int32_t portIdAidl = VALUE_OR_RETURN_STATUS(legacy2aidl_audio_port_handle_t_int32_t(portId));
2736 std::vector<media::AudioMixerAttributesInternal> _aidlReturn;
2737 RETURN_STATUS_IF_ERROR(statusTFromBinderStatus(
2738 aps->getSupportedMixerAttributes(portIdAidl, &_aidlReturn)));
2739 *mixerAttrs = VALUE_OR_RETURN_STATUS(
2740 convertContainer<std::vector<audio_mixer_attributes_t>>(
2741 _aidlReturn,
2742 aidl2legacy_AudioMixerAttributesInternal_audio_mixer_attributes_t));
2743 return OK;
2744}
2745
2746status_t AudioSystem::setPreferredMixerAttributes(const audio_attributes_t *attr,
2747 audio_port_handle_t portId,
2748 uid_t uid,
2749 const audio_mixer_attributes_t *mixerAttr) {
Andy Hung264fa4c2024-02-21 15:52:12 -08002750 const sp<IAudioPolicyService> aps = get_audio_policy_service();
jiabina84c3d32022-12-02 18:59:55 +00002751 if (aps == nullptr) {
2752 return PERMISSION_DENIED;
2753 }
2754
Mikhail Naganov1c400902023-05-17 11:48:43 -07002755 media::audio::common::AudioAttributes attrAidl = VALUE_OR_RETURN_STATUS(
2756 legacy2aidl_audio_attributes_t_AudioAttributes(*attr));
jiabina84c3d32022-12-02 18:59:55 +00002757 media::AudioMixerAttributesInternal mixerAttrAidl = VALUE_OR_RETURN_STATUS(
2758 legacy2aidl_audio_mixer_attributes_t_AudioMixerAttributesInternal(*mixerAttr));
2759 int32_t uidAidl = VALUE_OR_RETURN_STATUS(legacy2aidl_uid_t_int32_t(uid));
2760 int32_t portIdAidl = VALUE_OR_RETURN_STATUS(legacy2aidl_audio_port_handle_t_int32_t(portId));
2761
2762 return statusTFromBinderStatus(
2763 aps->setPreferredMixerAttributes(attrAidl, portIdAidl, uidAidl, mixerAttrAidl));
2764}
2765
2766status_t AudioSystem::getPreferredMixerAttributes(
2767 const audio_attributes_t *attr,
2768 audio_port_handle_t portId,
2769 std::optional<audio_mixer_attributes_t> *mixerAttr) {
Andy Hung264fa4c2024-02-21 15:52:12 -08002770 const sp<IAudioPolicyService> aps = get_audio_policy_service();
jiabina84c3d32022-12-02 18:59:55 +00002771 if (aps == nullptr) {
2772 return PERMISSION_DENIED;
2773 }
2774
Mikhail Naganov1c400902023-05-17 11:48:43 -07002775 media::audio::common::AudioAttributes attrAidl = VALUE_OR_RETURN_STATUS(
2776 legacy2aidl_audio_attributes_t_AudioAttributes(*attr));
jiabina84c3d32022-12-02 18:59:55 +00002777 int32_t portIdAidl = VALUE_OR_RETURN_STATUS(legacy2aidl_audio_port_handle_t_int32_t(portId));
2778 std::optional<media::AudioMixerAttributesInternal> _aidlReturn;
2779 RETURN_STATUS_IF_ERROR(statusTFromBinderStatus(
2780 aps->getPreferredMixerAttributes(attrAidl, portIdAidl, &_aidlReturn)));
2781
2782 if (_aidlReturn.has_value()) {
2783 *mixerAttr = VALUE_OR_RETURN_STATUS(
2784 aidl2legacy_AudioMixerAttributesInternal_audio_mixer_attributes_t(
2785 _aidlReturn.value()));
2786 }
2787 return NO_ERROR;
2788}
2789
2790status_t AudioSystem::clearPreferredMixerAttributes(const audio_attributes_t *attr,
2791 audio_port_handle_t portId,
2792 uid_t uid) {
Andy Hung264fa4c2024-02-21 15:52:12 -08002793 const sp<IAudioPolicyService> aps = get_audio_policy_service();
jiabina84c3d32022-12-02 18:59:55 +00002794 if (aps == nullptr) {
2795 return PERMISSION_DENIED;
2796 }
2797
Mikhail Naganov1c400902023-05-17 11:48:43 -07002798 media::audio::common::AudioAttributes attrAidl = VALUE_OR_RETURN_STATUS(
2799 legacy2aidl_audio_attributes_t_AudioAttributes(*attr));
jiabina84c3d32022-12-02 18:59:55 +00002800 int32_t uidAidl = VALUE_OR_RETURN_STATUS(legacy2aidl_uid_t_int32_t(uid));
2801 int32_t portIdAidl = VALUE_OR_RETURN_STATUS(legacy2aidl_audio_port_handle_t_int32_t(portId));
2802 return statusTFromBinderStatus(
2803 aps->clearPreferredMixerAttributes(attrAidl, portIdAidl, uidAidl));
2804}
2805
Jiabin Huangaa6e9e32024-10-21 17:19:28 +00002806status_t AudioSystem::getMmapPolicyForDevice(AudioMMapPolicyType policyType,
2807 audio_devices_t device,
2808 AudioMMapPolicyInfo *policyInfo) {
2809 const sp<IAudioPolicyService> aps = get_audio_policy_service();
2810 if (aps == nullptr) {
2811 return PERMISSION_DENIED;
2812 }
2813 policyInfo->device.type = VALUE_OR_RETURN_STATUS(
2814 legacy2aidl_audio_devices_t_AudioDeviceDescription(device));
2815 return statusTFromBinderStatus(aps->getMmapPolicyForDevice(policyType, policyInfo));
2816}
2817
Eric Laurentc2f1f072009-07-17 12:17:14 -07002818// ---------------------------------------------------------------------------
2819
Eric Laurente8726fe2015-06-26 09:39:24 -07002820int AudioSystem::AudioPolicyServiceClient::addAudioPortCallback(
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002821 const sp<AudioPortCallback>& callback) {
Andy Hungacd955a2024-02-16 17:14:36 -08002822 std::lock_guard _l(mMutex);
Andy Hungd211ce02024-02-16 15:34:07 -08002823 return mAudioPortCallbacks.insert(callback).second ? mAudioPortCallbacks.size() : -1;
Eric Laurentb28753e2015-04-01 13:06:28 -07002824}
2825
Eric Laurente8726fe2015-06-26 09:39:24 -07002826int AudioSystem::AudioPolicyServiceClient::removeAudioPortCallback(
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002827 const sp<AudioPortCallback>& callback) {
Andy Hungacd955a2024-02-16 17:14:36 -08002828 std::lock_guard _l(mMutex);
Andy Hungd211ce02024-02-16 15:34:07 -08002829 return mAudioPortCallbacks.erase(callback) > 0 ? mAudioPortCallbacks.size() : -1;
Eric Laurentb28753e2015-04-01 13:06:28 -07002830}
2831
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002832Status AudioSystem::AudioPolicyServiceClient::onAudioPortListUpdate() {
Andy Hungacd955a2024-02-16 17:14:36 -08002833 std::lock_guard _l(mMutex);
Andy Hungd211ce02024-02-16 15:34:07 -08002834 for (const auto& callback : mAudioPortCallbacks) {
2835 callback->onAudioPortListUpdate();
Eric Laurentb28753e2015-04-01 13:06:28 -07002836 }
Ytai Ben-Tsvi7e7a79d2020-12-15 16:48:16 -08002837 return Status::ok();
Eric Laurentb28753e2015-04-01 13:06:28 -07002838}
2839
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002840Status AudioSystem::AudioPolicyServiceClient::onAudioPatchListUpdate() {
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 : mAudioPortCallbacks) {
2843 callback->onAudioPatchListUpdate();
Eric Laurentb28753e2015-04-01 13:06:28 -07002844 }
Ytai Ben-Tsvi7e7a79d2020-12-15 16:48:16 -08002845 return Status::ok();
Eric Laurentb28753e2015-04-01 13:06:28 -07002846}
2847
François Gaffiecfe17322018-11-07 13:41:29 +01002848// ----------------------------------------------------------------------------
2849int AudioSystem::AudioPolicyServiceClient::addAudioVolumeGroupCallback(
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002850 const sp<AudioVolumeGroupCallback>& callback) {
Andy Hungacd955a2024-02-16 17:14:36 -08002851 std::lock_guard _l(mMutex);
Andy Hungd211ce02024-02-16 15:34:07 -08002852 return mAudioVolumeGroupCallbacks.insert(callback).second
2853 ? mAudioVolumeGroupCallbacks.size() : -1;
François Gaffiecfe17322018-11-07 13:41:29 +01002854}
2855
2856int AudioSystem::AudioPolicyServiceClient::removeAudioVolumeGroupCallback(
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002857 const sp<AudioVolumeGroupCallback>& callback) {
Andy Hungacd955a2024-02-16 17:14:36 -08002858 std::lock_guard _l(mMutex);
Andy Hungd211ce02024-02-16 15:34:07 -08002859 return mAudioVolumeGroupCallbacks.erase(callback) > 0
2860 ? mAudioVolumeGroupCallbacks.size() : -1;
François Gaffiecfe17322018-11-07 13:41:29 +01002861}
2862
Ytai Ben-Tsvi7e7a79d2020-12-15 16:48:16 -08002863Status AudioSystem::AudioPolicyServiceClient::onAudioVolumeGroupChanged(int32_t group,
2864 int32_t flags) {
2865 volume_group_t groupLegacy = VALUE_OR_RETURN_BINDER_STATUS(
2866 aidl2legacy_int32_t_volume_group_t(group));
2867 int flagsLegacy = VALUE_OR_RETURN_BINDER_STATUS(convertReinterpret<int>(flags));
2868
Andy Hungacd955a2024-02-16 17:14:36 -08002869 std::lock_guard _l(mMutex);
Andy Hungd211ce02024-02-16 15:34:07 -08002870 for (const auto& callback : mAudioVolumeGroupCallbacks) {
2871 callback->onAudioVolumeGroupChanged(groupLegacy, flagsLegacy);
François Gaffiecfe17322018-11-07 13:41:29 +01002872 }
Ytai Ben-Tsvi7e7a79d2020-12-15 16:48:16 -08002873 return Status::ok();
François Gaffiecfe17322018-11-07 13:41:29 +01002874}
2875// ----------------------------------------------------------------------------
2876
Ytai Ben-Tsvi7e7a79d2020-12-15 16:48:16 -08002877Status AudioSystem::AudioPolicyServiceClient::onDynamicPolicyMixStateUpdate(
2878 const ::std::string& regId, int32_t state) {
2879 ALOGV("AudioPolicyServiceClient::onDynamicPolicyMixStateUpdate(%s, %d)", regId.c_str(), state);
2880
2881 String8 regIdLegacy = VALUE_OR_RETURN_BINDER_STATUS(aidl2legacy_string_view_String8(regId));
2882 int stateLegacy = VALUE_OR_RETURN_BINDER_STATUS(convertReinterpret<int>(state));
Jean-Michel Trivif613d422015-04-23 18:41:29 -07002883 dynamic_policy_callback cb = NULL;
2884 {
Andy Hungacd955a2024-02-16 17:14:36 -08002885 std::lock_guard _l(AudioSystem::gMutex);
Jean-Michel Trivif613d422015-04-23 18:41:29 -07002886 cb = gDynPolicyCallback;
2887 }
2888
2889 if (cb != NULL) {
Ytai Ben-Tsvi7e7a79d2020-12-15 16:48:16 -08002890 cb(DYNAMIC_POLICY_EVENT_MIX_STATE_UPDATE, regIdLegacy, stateLegacy);
Jean-Michel Trivif613d422015-04-23 18:41:29 -07002891 }
Ytai Ben-Tsvi7e7a79d2020-12-15 16:48:16 -08002892 return Status::ok();
Jean-Michel Trivide801052015-04-14 19:10:14 -07002893}
2894
Ytai Ben-Tsvi7e7a79d2020-12-15 16:48:16 -08002895Status AudioSystem::AudioPolicyServiceClient::onRecordingConfigurationUpdate(
2896 int32_t event,
2897 const media::RecordClientInfo& clientInfo,
Mikhail Naganovdbf03642021-08-25 18:15:32 -07002898 const AudioConfigBase& clientConfig,
Ytai Ben-Tsvi7e7a79d2020-12-15 16:48:16 -08002899 const std::vector<media::EffectDescriptor>& clientEffects,
Mikhail Naganovdbf03642021-08-25 18:15:32 -07002900 const AudioConfigBase& deviceConfig,
Ytai Ben-Tsvi7e7a79d2020-12-15 16:48:16 -08002901 const std::vector<media::EffectDescriptor>& effects,
2902 int32_t patchHandle,
Mikhail Naganovddceecc2021-09-03 13:58:56 -07002903 AudioSource source) {
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -08002904 record_config_callback cb = NULL;
2905 {
Andy Hungacd955a2024-02-16 17:14:36 -08002906 std::lock_guard _l(AudioSystem::gMutex);
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -08002907 cb = gRecordConfigCallback;
2908 }
2909
2910 if (cb != NULL) {
Ytai Ben-Tsvi7e7a79d2020-12-15 16:48:16 -08002911 int eventLegacy = VALUE_OR_RETURN_BINDER_STATUS(convertReinterpret<int>(event));
2912 record_client_info_t clientInfoLegacy = VALUE_OR_RETURN_BINDER_STATUS(
2913 aidl2legacy_RecordClientInfo_record_client_info_t(clientInfo));
2914 audio_config_base_t clientConfigLegacy = VALUE_OR_RETURN_BINDER_STATUS(
Mikhail Naganovde3fa182021-07-30 15:06:42 -07002915 aidl2legacy_AudioConfigBase_audio_config_base_t(clientConfig, true /*isInput*/));
Ytai Ben-Tsvi7e7a79d2020-12-15 16:48:16 -08002916 std::vector<effect_descriptor_t> clientEffectsLegacy = VALUE_OR_RETURN_BINDER_STATUS(
2917 convertContainer<std::vector<effect_descriptor_t>>(
2918 clientEffects,
2919 aidl2legacy_EffectDescriptor_effect_descriptor_t));
2920 audio_config_base_t deviceConfigLegacy = VALUE_OR_RETURN_BINDER_STATUS(
Mikhail Naganovde3fa182021-07-30 15:06:42 -07002921 aidl2legacy_AudioConfigBase_audio_config_base_t(deviceConfig, true /*isInput*/));
Ytai Ben-Tsvi7e7a79d2020-12-15 16:48:16 -08002922 std::vector<effect_descriptor_t> effectsLegacy = VALUE_OR_RETURN_BINDER_STATUS(
2923 convertContainer<std::vector<effect_descriptor_t>>(
2924 effects,
2925 aidl2legacy_EffectDescriptor_effect_descriptor_t));
2926 audio_patch_handle_t patchHandleLegacy = VALUE_OR_RETURN_BINDER_STATUS(
2927 aidl2legacy_int32_t_audio_patch_handle_t(patchHandle));
2928 audio_source_t sourceLegacy = VALUE_OR_RETURN_BINDER_STATUS(
Mikhail Naganovddceecc2021-09-03 13:58:56 -07002929 aidl2legacy_AudioSource_audio_source_t(source));
Ytai Ben-Tsvi7e7a79d2020-12-15 16:48:16 -08002930 cb(eventLegacy, &clientInfoLegacy, &clientConfigLegacy, clientEffectsLegacy,
2931 &deviceConfigLegacy, effectsLegacy, patchHandleLegacy, sourceLegacy);
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -08002932 }
Ytai Ben-Tsvi7e7a79d2020-12-15 16:48:16 -08002933 return Status::ok();
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -08002934}
2935
Jean-Michel Trivi9a6b9ad2020-10-22 16:46:43 -07002936Status AudioSystem::AudioPolicyServiceClient::onRoutingUpdated() {
2937 routing_callback cb = NULL;
2938 {
Andy Hungacd955a2024-02-16 17:14:36 -08002939 std::lock_guard _l(AudioSystem::gMutex);
Jean-Michel Trivi9a6b9ad2020-10-22 16:46:43 -07002940 cb = gRoutingCallback;
2941 }
2942
2943 if (cb != NULL) {
2944 cb();
2945 }
2946 return Status::ok();
2947}
2948
Jean-Michel Trivi78f2b302022-04-15 18:18:41 +00002949Status AudioSystem::AudioPolicyServiceClient::onVolumeRangeInitRequest() {
2950 vol_range_init_req_callback cb = NULL;
2951 {
Andy Hungacd955a2024-02-16 17:14:36 -08002952 std::lock_guard _l(AudioSystem::gMutex);
Jean-Michel Trivi78f2b302022-04-15 18:18:41 +00002953 cb = gVolRangeInitReqCallback;
2954 }
2955
2956 if (cb != NULL) {
2957 cb();
2958 }
2959 return Status::ok();
2960}
2961
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002962void AudioSystem::AudioPolicyServiceClient::binderDied(const wp<IBinder>& who __unused) {
Glenn Kastend2d089f2014-11-05 11:48:12 -08002963 {
Andy Hungacd955a2024-02-16 17:14:36 -08002964 std::lock_guard _l(mMutex);
Andy Hungd211ce02024-02-16 15:34:07 -08002965 for (const auto& callback : mAudioPortCallbacks) {
2966 callback->onServiceDied();
Glenn Kastend2d089f2014-11-05 11:48:12 -08002967 }
Andy Hungd211ce02024-02-16 15:34:07 -08002968 for (const auto& callback : mAudioVolumeGroupCallbacks) {
2969 callback->onServiceDied();
François Gaffiecfe17322018-11-07 13:41:29 +01002970 }
Eric Laurentb52c1522014-05-20 11:27:36 -07002971 }
Mikhail Naganovec3d5792022-05-06 00:19:55 +00002972 AudioSystem::clearAudioPolicyService();
Eric Laurentc2f1f072009-07-17 12:17:14 -07002973
Steve Block5ff1dd52012-01-05 23:22:43 +00002974 ALOGW("AudioPolicyService server died!");
Eric Laurentc2f1f072009-07-17 12:17:14 -07002975}
2976
Ytai Ben-Tsvi7e7a79d2020-12-15 16:48:16 -08002977ConversionResult<record_client_info_t>
2978aidl2legacy_RecordClientInfo_record_client_info_t(const media::RecordClientInfo& aidl) {
2979 record_client_info_t legacy;
2980 legacy.riid = VALUE_OR_RETURN(aidl2legacy_int32_t_audio_unique_id_t(aidl.riid));
2981 legacy.uid = VALUE_OR_RETURN(aidl2legacy_int32_t_uid_t(aidl.uid));
2982 legacy.session = VALUE_OR_RETURN(aidl2legacy_int32_t_audio_session_t(aidl.session));
Mikhail Naganovddceecc2021-09-03 13:58:56 -07002983 legacy.source = VALUE_OR_RETURN(aidl2legacy_AudioSource_audio_source_t(aidl.source));
Ytai Ben-Tsvi7e7a79d2020-12-15 16:48:16 -08002984 legacy.port_id = VALUE_OR_RETURN(aidl2legacy_int32_t_audio_port_handle_t(aidl.portId));
2985 legacy.silenced = aidl.silenced;
2986 return legacy;
2987}
2988
2989ConversionResult<media::RecordClientInfo>
2990legacy2aidl_record_client_info_t_RecordClientInfo(const record_client_info_t& legacy) {
2991 media::RecordClientInfo aidl;
2992 aidl.riid = VALUE_OR_RETURN(legacy2aidl_audio_unique_id_t_int32_t(legacy.riid));
2993 aidl.uid = VALUE_OR_RETURN(legacy2aidl_uid_t_int32_t(legacy.uid));
2994 aidl.session = VALUE_OR_RETURN(legacy2aidl_audio_session_t_int32_t(legacy.session));
Mikhail Naganovddceecc2021-09-03 13:58:56 -07002995 aidl.source = VALUE_OR_RETURN(legacy2aidl_audio_source_t_AudioSource(legacy.source));
Ytai Ben-Tsvi7e7a79d2020-12-15 16:48:16 -08002996 aidl.portId = VALUE_OR_RETURN(legacy2aidl_audio_port_handle_t_int32_t(legacy.port_id));
2997 aidl.silenced = legacy.silenced;
2998 return aidl;
2999}
3000
Glenn Kasten40bc9062015-03-20 09:09:33 -07003001} // namespace android