blob: 55f74e179c61c079062f3f11572132168dd90e82 [file] [log] [blame]
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001/*
2 * Copyright (C) 2006-2007 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#define LOG_TAG "AudioSystem"
18//#define LOG_NDEBUG 0
19
20#include <utils/Log.h>
Ytai Ben-Tsvi5120eda2020-03-26 09:41:15 -070021
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -080022#include <android/media/IAudioPolicyService.h>
Jan Sebechlebsky0af8e872023-08-11 14:45:08 +020023#include <android/media/AudioMixUpdate.h>
Ytai Ben-Tsvi5120eda2020-03-26 09:41:15 -070024#include <android/media/BnCaptureStateListener.h>
Marvin Raminbdefaf02023-11-01 09:10:32 +010025#include <android_media_audiopolicy.h>
Mathias Agopian75624082009-05-19 19:08:10 -070026#include <binder/IServiceManager.h>
Eric Laurentfb00fc72017-05-25 18:17:12 -070027#include <binder/ProcessState.h>
François Gaffie24437602018-04-23 15:08:59 +020028#include <binder/IPCThreadState.h>
Ytai Ben-Tsvi10dc0a62020-09-18 11:31:55 -070029#include <media/AidlConversion.h>
Eric Laurent21da6472017-11-09 16:29:26 -080030#include <media/AudioResamplerPublic.h>
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080031#include <media/AudioSystem.h>
Glenn Kasten1ab85ec2013-05-31 09:18:43 -070032#include <media/IAudioFlinger.h>
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -080033#include <media/PolicyAidlConversion.h>
François Gaffied0ba9ed2018-11-05 11:50:42 +010034#include <media/TypeConverter.h>
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080035#include <math.h>
36
Dima Zavin64760242011-05-11 14:15:23 -070037#include <system/audio.h>
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -080038#include <android/media/GetInputForAttrResponse.h>
jiabina84c3d32022-12-02 18:59:55 +000039#include <android/media/AudioMixerAttributesInternal.h>
Dima Zavinfce7a472011-04-19 22:30:36 -070040
Ytai Ben-Tsvi1ff75692020-11-06 12:16:12 -080041#define VALUE_OR_RETURN_BINDER_STATUS(x) \
Ytai Ben-Tsvi10dc0a62020-09-18 11:31:55 -070042 ({ auto _tmp = (x); \
Andy Hung1131b6e2020-12-08 20:47:45 -080043 if (!_tmp.ok()) return aidl_utils::binderStatusFromStatusT(_tmp.error()); \
Ytai Ben-Tsvia3815202020-10-28 14:58:08 -070044 std::move(_tmp.value()); })
Ytai Ben-Tsvi10dc0a62020-09-18 11:31:55 -070045
Eric Laurentc2f1f072009-07-17 12:17:14 -070046// ----------------------------------------------------------------------------
Eric Laurentc2f1f072009-07-17 12:17:14 -070047
Marvin Raminbdefaf02023-11-01 09:10:32 +010048namespace audio_flags = android::media::audiopolicy;
49
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080050namespace android {
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -080051using aidl_utils::statusTFromBinderStatus;
Ytai Ben-Tsvi10dc0a62020-09-18 11:31:55 -070052using binder::Status;
Mikhail Naganov57bd06f2021-08-10 16:41:54 -070053using content::AttributionSourceState;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -080054using media::IAudioPolicyService;
Mikhail Naganovdbf03642021-08-25 18:15:32 -070055using media::audio::common::AudioConfig;
56using media::audio::common::AudioConfigBase;
Mikhail Naganovf4a75362021-09-16 00:02:54 +000057using media::audio::common::AudioDevice;
58using media::audio::common::AudioDeviceDescription;
Mikhail Naganov57bd06f2021-08-10 16:41:54 -070059using media::audio::common::AudioFormatDescription;
jiabine99d0882021-09-17 05:21:25 +000060using media::audio::common::AudioMMapPolicyInfo;
61using media::audio::common::AudioMMapPolicyType;
Mikhail Naganovdbf03642021-08-25 18:15:32 -070062using media::audio::common::AudioOffloadInfo;
Mikhail Naganovddceecc2021-09-03 13:58:56 -070063using media::audio::common::AudioSource;
Mikhail Naganovdbf03642021-08-25 18:15:32 -070064using media::audio::common::AudioStreamType;
65using media::audio::common::AudioUsage;
Mikhail Naganov0078ee52021-09-30 23:06:20 +000066using media::audio::common::Int;
Ytai Ben-Tsvi10dc0a62020-09-18 11:31:55 -070067
Andy Hungacd955a2024-02-16 17:14:36 -080068std::mutex AudioSystem::gMutex;
Jean-Michel Trivif613d422015-04-23 18:41:29 -070069dynamic_policy_callback AudioSystem::gDynPolicyCallback = NULL;
Svet Ganovf4ddfef2018-01-16 07:37:58 -080070record_config_callback AudioSystem::gRecordConfigCallback = NULL;
Jean-Michel Trivi9a6b9ad2020-10-22 16:46:43 -070071routing_callback AudioSystem::gRoutingCallback = NULL;
Jean-Michel Trivi78f2b302022-04-15 18:18:41 +000072vol_range_init_req_callback AudioSystem::gVolRangeInitReqCallback = NULL;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080073
Andy Hung88e71732024-02-21 16:44:27 -080074std::mutex AudioSystem::gApsCallbackMutex;
Andy Hungacd955a2024-02-16 17:14:36 -080075std::mutex AudioSystem::gErrorCallbacksMutex;
76std::set<audio_error_callback> AudioSystem::gAudioErrorCallbacks;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -080077
Andy Hungacd955a2024-02-16 17:14:36 -080078std::mutex AudioSystem::gSoundTriggerMutex;
79sp<CaptureStateListenerImpl> AudioSystem::gSoundTriggerCaptureStateListener;
Ytai Ben-Tsvi5120eda2020-03-26 09:41:15 -070080
Andy Hungacd955a2024-02-16 17:14:36 -080081// Sets the Binder for the AudioFlinger service, passed to this client process
82// from the system server.
Ahaan Ugale058f94b2021-05-21 02:23:50 -070083// This allows specific isolated processes to access the audio system. Currently used only for the
84// HotwordDetectionService.
Andy Hungda1fb072024-02-20 19:08:08 -080085template <typename ServiceInterface, typename Client, typename AidlInterface,
86 typename ServiceTraits>
87class ServiceHandler {
88public:
Shraddha Basantwanif2aa2502024-04-08 11:01:57 +000089 sp<ServiceInterface> getService()
Andy Hungda1fb072024-02-20 19:08:08 -080090 EXCLUDES(mMutex) NO_THREAD_SAFETY_ANALYSIS { // std::unique_ptr
91 sp<ServiceInterface> service;
92 sp<Client> client;
Ahaan Ugale058f94b2021-05-21 02:23:50 -070093
Andy Hungda1fb072024-02-20 19:08:08 -080094 bool reportNoError = false;
95 {
96 std::lock_guard _l(mMutex);
97 if (mService != nullptr) {
98 return mService;
99 }
Andy Hung66fc0bd2023-01-04 11:45:50 -0800100 }
101
Andy Hungda1fb072024-02-20 19:08:08 -0800102 std::unique_lock ul_only1thread(mSingleGetter);
103 std::unique_lock ul(mMutex);
104 if (mService != nullptr) {
105 return mService;
106 }
107 if (mClient == nullptr) {
108 mClient = sp<Client>::make();
Andy Hung66fc0bd2023-01-04 11:45:50 -0800109 } else {
110 reportNoError = true;
111 }
Andy Hungda1fb072024-02-20 19:08:08 -0800112 while (true) {
113 mService = mLocalService;
114 if (mService != nullptr) break;
Andy Hung66fc0bd2023-01-04 11:45:50 -0800115
Andy Hungda1fb072024-02-20 19:08:08 -0800116 sp<IBinder> binder = mBinder;
117 if (binder == nullptr) {
118 sp <IServiceManager> sm = defaultServiceManager();
119 binder = sm->checkService(String16(ServiceTraits::SERVICE_NAME));
Paul Wangbcf95f62023-02-28 15:38:16 +0000120 if (binder == nullptr) {
Andy Hungda1fb072024-02-20 19:08:08 -0800121 ALOGD("%s: waiting for %s", __func__, ServiceTraits::SERVICE_NAME);
122
123 // if the condition variable is present, setLocalService() and
124 // setBinder() is allowed to use it to notify us.
125 if (mCvGetter == nullptr) {
126 mCvGetter = std::make_shared<std::condition_variable>();
127 }
128 mCvGetter->wait_for(ul, std::chrono::seconds(1));
129 continue;
Paul Wangbcf95f62023-02-28 15:38:16 +0000130 }
Ahaan Ugale058f94b2021-05-21 02:23:50 -0700131 }
Andy Hungda1fb072024-02-20 19:08:08 -0800132 binder->linkToDeath(mClient);
133 auto aidlInterface = interface_cast<AidlInterface>(binder);
134 LOG_ALWAYS_FATAL_IF(aidlInterface == nullptr);
135 if constexpr (std::is_same_v<ServiceInterface, AidlInterface>) {
136 mService = std::move(aidlInterface);
137 } else /* constexpr */ {
138 mService = ServiceTraits::createServiceAdapter(aidlInterface);
139 }
140 break;
Glenn Kastene53b9ea2012-03-12 16:29:55 -0700141 }
Andy Hungda1fb072024-02-20 19:08:08 -0800142 if (mCvGetter) mCvGetter.reset(); // remove condition variable.
143 client = mClient;
144 service = mService;
145 // Make sure callbacks can be received by the client
Shraddha Basantwanif2aa2502024-04-08 11:01:57 +0000146 if (mCanStartThreadPool) {
Akshata Kadam1d486b72023-08-07 05:49:48 +0000147 ProcessState::self()->startThreadPool();
148 }
Andy Hungda1fb072024-02-20 19:08:08 -0800149 ul.unlock();
150 ul_only1thread.unlock();
151 ServiceTraits::onServiceCreate(service, client);
152 if (reportNoError) AudioSystem::reportError(NO_ERROR);
153 return service;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800154 }
Andy Hungda1fb072024-02-20 19:08:08 -0800155
156 status_t setLocalService(const sp<ServiceInterface>& service) EXCLUDES(mMutex) {
157 std::lock_guard _l(mMutex);
158 // we allow clearing once set, but not a double non-null set.
159 if (mService != nullptr && service != nullptr) return INVALID_OPERATION;
160 mLocalService = service;
161 if (mCvGetter) mCvGetter->notify_one();
162 return OK;
163 }
164
165 sp<Client> getClient() EXCLUDES(mMutex) {
166 const auto service = getService();
167 if (service == nullptr) return nullptr;
168 std::lock_guard _l(mMutex);
169 return mClient;
170 }
171
172 void setBinder(const sp<IBinder>& binder) EXCLUDES(mMutex) {
173 std::lock_guard _l(mMutex);
174 if (mService != nullptr) {
175 ALOGW("%s: ignoring; %s connection already established.",
176 __func__, ServiceTraits::SERVICE_NAME);
177 return;
178 }
179 mBinder = binder;
180 if (mCvGetter) mCvGetter->notify_one();
181 }
182
183 void clearService() EXCLUDES(mMutex) {
184 std::lock_guard _l(mMutex);
185 mService.clear();
186 if (mClient) ServiceTraits::onClearService(mClient);
187 }
188
Shraddha Basantwanif2aa2502024-04-08 11:01:57 +0000189 void disableThreadPool() {
190 mCanStartThreadPool = false;
191 }
192
Andy Hungda1fb072024-02-20 19:08:08 -0800193private:
194 std::mutex mSingleGetter;
195 std::mutex mMutex;
196 std::shared_ptr<std::condition_variable> mCvGetter GUARDED_BY(mMutex);
197 sp<IBinder> mBinder GUARDED_BY(mMutex);
198 sp<ServiceInterface> mLocalService GUARDED_BY(mMutex);
199 sp<ServiceInterface> mService GUARDED_BY(mMutex);
200 sp<Client> mClient GUARDED_BY(mMutex);
Shraddha Basantwanif2aa2502024-04-08 11:01:57 +0000201 std::atomic<bool> mCanStartThreadPool = true;
Andy Hungda1fb072024-02-20 19:08:08 -0800202};
203
204struct AudioFlingerTraits {
205 static void onServiceCreate(
206 const sp<IAudioFlinger>& af, const sp<AudioSystem::AudioFlingerClient>& afc) {
207 const int64_t token = IPCThreadState::self()->clearCallingIdentity();
208 af->registerClient(afc);
209 IPCThreadState::self()->restoreCallingIdentity(token);
210 }
211
212 static sp<IAudioFlinger> createServiceAdapter(
213 const sp<media::IAudioFlingerService>& aidlInterface) {
214 return sp<AudioFlingerClientAdapter>::make(aidlInterface);
215 }
216
217 static void onClearService(const sp<AudioSystem::AudioFlingerClient>& afc) {
218 afc->clearIoCache();
219 }
220
221 static constexpr const char* SERVICE_NAME = IAudioFlinger::DEFAULT_SERVICE_NAME;
222};
223
224[[clang::no_destroy]] static constinit ServiceHandler<IAudioFlinger,
225 AudioSystem::AudioFlingerClient, media::IAudioFlingerService,
226 AudioFlingerTraits> gAudioFlingerServiceHandler;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800227
Andy Hung264fa4c2024-02-21 15:52:12 -0800228sp<IAudioFlinger> AudioSystem::get_audio_flinger() {
Andy Hungda1fb072024-02-20 19:08:08 -0800229 return gAudioFlingerServiceHandler.getService();
Akshata Kadam1d486b72023-08-07 05:49:48 +0000230}
231
Andy Hungda1fb072024-02-20 19:08:08 -0800232sp<AudioSystem::AudioFlingerClient> AudioSystem::getAudioFlingerClient() {
233 return gAudioFlingerServiceHandler.getClient();
234}
235
236void AudioSystem::setAudioFlingerBinder(const sp<IBinder>& audioFlinger) {
237 if (audioFlinger->getInterfaceDescriptor() != media::IAudioFlingerService::descriptor) {
238 ALOGE("%s: received a binder of type %s",
239 __func__, String8(audioFlinger->getInterfaceDescriptor()).c_str());
240 return;
241 }
242 gAudioFlingerServiceHandler.setBinder(audioFlinger);
243}
244
245status_t AudioSystem::setLocalAudioFlinger(const sp<IAudioFlinger>& af) {
246 return gAudioFlingerServiceHandler.setLocalService(af);
Eric Laurent296fb132015-05-01 11:38:42 -0700247}
248
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800249sp<AudioIoDescriptor> AudioSystem::getIoDescriptor(audio_io_handle_t ioHandle) {
Eric Laurent296fb132015-05-01 11:38:42 -0700250 sp<AudioIoDescriptor> desc;
251 const sp<AudioFlingerClient> afc = getAudioFlingerClient();
252 if (afc != 0) {
253 desc = afc->getIoDescriptor(ioHandle);
254 }
255 return desc;
256}
257
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800258/* static */ status_t AudioSystem::checkAudioFlinger() {
Eric Laurent46291612013-07-18 14:38:44 -0700259 if (defaultServiceManager()->checkService(String16("media.audio_flinger")) != 0) {
260 return NO_ERROR;
261 }
262 return DEAD_OBJECT;
263}
264
Glenn Kasten4a8308b2016-04-18 14:10:01 -0700265// FIXME Declare in binder opcode order, similarly to IAudioFlinger.h and IAudioFlinger.cpp
266
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800267status_t AudioSystem::muteMicrophone(bool state) {
Andy Hung264fa4c2024-02-21 15:52:12 -0800268 const sp<IAudioFlinger> af = get_audio_flinger();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800269 if (af == 0) return PERMISSION_DENIED;
270 return af->setMicMute(state);
271}
272
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800273status_t AudioSystem::isMicrophoneMuted(bool* state) {
Andy Hung264fa4c2024-02-21 15:52:12 -0800274 const sp<IAudioFlinger> af = get_audio_flinger();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800275 if (af == 0) return PERMISSION_DENIED;
276 *state = af->getMicMute();
277 return NO_ERROR;
278}
279
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800280status_t AudioSystem::setMasterVolume(float value) {
Andy Hung264fa4c2024-02-21 15:52:12 -0800281 const sp<IAudioFlinger> af = get_audio_flinger();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800282 if (af == 0) return PERMISSION_DENIED;
283 af->setMasterVolume(value);
284 return NO_ERROR;
285}
286
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800287status_t AudioSystem::setMasterMute(bool mute) {
Andy Hung264fa4c2024-02-21 15:52:12 -0800288 const sp<IAudioFlinger> af = get_audio_flinger();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800289 if (af == 0) return PERMISSION_DENIED;
290 af->setMasterMute(mute);
291 return NO_ERROR;
292}
293
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800294status_t AudioSystem::getMasterVolume(float* volume) {
Andy Hung264fa4c2024-02-21 15:52:12 -0800295 const sp<IAudioFlinger> af = get_audio_flinger();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800296 if (af == 0) return PERMISSION_DENIED;
297 *volume = af->masterVolume();
298 return NO_ERROR;
299}
300
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800301status_t AudioSystem::getMasterMute(bool* mute) {
Andy Hung264fa4c2024-02-21 15:52:12 -0800302 const sp<IAudioFlinger> af = get_audio_flinger();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800303 if (af == 0) return PERMISSION_DENIED;
304 *mute = af->masterMute();
305 return NO_ERROR;
306}
307
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800308status_t AudioSystem::setStreamVolume(audio_stream_type_t stream, float value,
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800309 audio_io_handle_t output) {
Dima Zavinfce7a472011-04-19 22:30:36 -0700310 if (uint32_t(stream) >= AUDIO_STREAM_CNT) return BAD_VALUE;
Andy Hung264fa4c2024-02-21 15:52:12 -0800311 const sp<IAudioFlinger> af = get_audio_flinger();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800312 if (af == 0) return PERMISSION_DENIED;
Eric Laurentc2f1f072009-07-17 12:17:14 -0700313 af->setStreamVolume(stream, value, output);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800314 return NO_ERROR;
315}
316
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800317status_t AudioSystem::setStreamMute(audio_stream_type_t stream, bool mute) {
Dima Zavinfce7a472011-04-19 22:30:36 -0700318 if (uint32_t(stream) >= AUDIO_STREAM_CNT) return BAD_VALUE;
Andy Hung264fa4c2024-02-21 15:52:12 -0800319 const sp<IAudioFlinger> af = get_audio_flinger();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800320 if (af == 0) return PERMISSION_DENIED;
321 af->setStreamMute(stream, mute);
322 return NO_ERROR;
323}
324
Andy Hung6b137d12024-08-27 22:35:17 +0000325status_t AudioSystem::setPortsVolume(
326 const std::vector<audio_port_handle_t>& portIds, float volume, audio_io_handle_t output) {
327 const sp<IAudioFlinger> af = get_audio_flinger();
328 if (af == 0) return PERMISSION_DENIED;
329 std::vector<int32_t> portIdsAidl = VALUE_OR_RETURN_STATUS(
330 convertContainer<std::vector<int32_t>>(
331 portIds, legacy2aidl_audio_port_handle_t_int32_t));
332 int32_t outputAidl = VALUE_OR_RETURN_STATUS(legacy2aidl_audio_io_handle_t_int32_t(output));
333 af->setPortsVolume(portIdsAidl, volume, outputAidl);
334 return NO_ERROR;
335}
336
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800337status_t AudioSystem::setMode(audio_mode_t mode) {
Glenn Kasten930f4ca2012-01-06 16:47:31 -0800338 if (uint32_t(mode) >= AUDIO_MODE_CNT) return BAD_VALUE;
Andy Hung264fa4c2024-02-21 15:52:12 -0800339 const sp<IAudioFlinger> af = get_audio_flinger();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800340 if (af == 0) return PERMISSION_DENIED;
341 return af->setMode(mode);
342}
343
Mikhail Naganovb1ddbb02023-03-15 17:06:59 -0700344status_t AudioSystem::setSimulateDeviceConnections(bool enabled) {
Andy Hung264fa4c2024-02-21 15:52:12 -0800345 const sp<IAudioFlinger> af = get_audio_flinger();
Mikhail Naganovb1ddbb02023-03-15 17:06:59 -0700346 if (af == 0) return PERMISSION_DENIED;
347 return af->setSimulateDeviceConnections(enabled);
348}
349
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800350status_t AudioSystem::setParameters(audio_io_handle_t ioHandle, const String8& keyValuePairs) {
Andy Hung264fa4c2024-02-21 15:52:12 -0800351 const sp<IAudioFlinger> af = get_audio_flinger();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800352 if (af == 0) return PERMISSION_DENIED;
Eric Laurentc2f1f072009-07-17 12:17:14 -0700353 return af->setParameters(ioHandle, keyValuePairs);
354}
355
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800356String8 AudioSystem::getParameters(audio_io_handle_t ioHandle, const String8& keys) {
Andy Hung264fa4c2024-02-21 15:52:12 -0800357 const sp<IAudioFlinger> af = get_audio_flinger();
Eric Laurentc2f1f072009-07-17 12:17:14 -0700358 String8 result = String8("");
359 if (af == 0) return result;
360
361 result = af->getParameters(ioHandle, keys);
362 return result;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800363}
364
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800365status_t AudioSystem::setParameters(const String8& keyValuePairs) {
Glenn Kasten142f5192014-03-25 17:44:59 -0700366 return setParameters(AUDIO_IO_HANDLE_NONE, keyValuePairs);
Glenn Kastenc23885e2013-12-19 16:35:18 -0800367}
368
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800369String8 AudioSystem::getParameters(const String8& keys) {
Glenn Kasten142f5192014-03-25 17:44:59 -0700370 return getParameters(AUDIO_IO_HANDLE_NONE, keys);
Glenn Kastenc23885e2013-12-19 16:35:18 -0800371}
372
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800373// convert volume steps to natural log scale
374
375// change this value to change volume scaling
Andy Hungacd955a2024-02-16 17:14:36 -0800376constexpr float kdbPerStep = 0.5f;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800377// shouldn't need to touch these
Andy Hungacd955a2024-02-16 17:14:36 -0800378constexpr float kdBConvert = -kdbPerStep * 2.302585093f / 20.0f;
379constexpr float kdBConvertInverse = 1.0f / kdBConvert;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800380
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800381float AudioSystem::linearToLog(int volume) {
Andy Hungacd955a2024-02-16 17:14:36 -0800382 // float v = volume ? exp(float(100 - volume) * kdBConvert) : 0;
Steve Blockb8a80522011-12-20 16:23:08 +0000383 // ALOGD("linearToLog(%d)=%f", volume, v);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800384 // return v;
Andy Hungacd955a2024-02-16 17:14:36 -0800385 return volume ? exp(float(100 - volume) * kdBConvert) : 0;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800386}
387
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800388int AudioSystem::logToLinear(float volume) {
Andy Hungacd955a2024-02-16 17:14:36 -0800389 // int v = volume ? 100 - int(kdBConvertInverse * log(volume) + 0.5) : 0;
Steve Blockb8a80522011-12-20 16:23:08 +0000390 // ALOGD("logTolinear(%d)=%f", v, volume);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800391 // return v;
Andy Hungacd955a2024-02-16 17:14:36 -0800392 return volume ? 100 - int(kdBConvertInverse * log(volume) + 0.5) : 0;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800393}
394
Eric Laurent21da6472017-11-09 16:29:26 -0800395/* static */ size_t AudioSystem::calculateMinFrameCount(
396 uint32_t afLatencyMs, uint32_t afFrameCount, uint32_t afSampleRate,
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800397 uint32_t sampleRate, float speed /*, uint32_t notificationsPerBufferReq*/) {
Eric Laurent21da6472017-11-09 16:29:26 -0800398 // Ensure that buffer depth covers at least audio hardware latency
399 uint32_t minBufCount = afLatencyMs / ((1000 * afFrameCount) / afSampleRate);
400 if (minBufCount < 2) {
401 minBufCount = 2;
402 }
403#if 0
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800404 // The notificationsPerBufferReq parameter is not yet used for non-fast tracks,
405 // but keeping the code here to make it easier to add later.
406 if (minBufCount < notificationsPerBufferReq) {
407 minBufCount = notificationsPerBufferReq;
408 }
Eric Laurent21da6472017-11-09 16:29:26 -0800409#endif
410 ALOGV("calculateMinFrameCount afLatency %u afFrameCount %u afSampleRate %u "
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800411 "sampleRate %u speed %f minBufCount: %u" /*" notificationsPerBufferReq %u"*/,
412 afLatencyMs, afFrameCount, afSampleRate, sampleRate, speed, minBufCount
413 /*, notificationsPerBufferReq*/);
Eric Laurent21da6472017-11-09 16:29:26 -0800414 return minBufCount * sourceFramesNeededWithTimestretch(
415 sampleRate, afFrameCount, afSampleRate, speed);
416}
417
418
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800419status_t
420AudioSystem::getOutputSamplingRate(uint32_t* samplingRate, audio_stream_type_t streamType) {
Eric Laurentc2f1f072009-07-17 12:17:14 -0700421 audio_io_handle_t output;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800422
Dima Zavinfce7a472011-04-19 22:30:36 -0700423 if (streamType == AUDIO_STREAM_DEFAULT) {
424 streamType = AUDIO_STREAM_MUSIC;
Eric Laurentc2f1f072009-07-17 12:17:14 -0700425 }
426
Glenn Kastenfff6d712012-01-12 16:38:12 -0800427 output = getOutput(streamType);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700428 if (output == 0) {
429 return PERMISSION_DENIED;
430 }
431
Jean-Michel Trivib7f24b12014-06-11 10:05:30 -0700432 return getSamplingRate(output, samplingRate);
Eric Laurent1a9ed112012-03-20 18:36:01 -0700433}
434
Glenn Kasten2c073da2016-02-26 09:14:08 -0800435status_t AudioSystem::getSamplingRate(audio_io_handle_t ioHandle,
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800436 uint32_t* samplingRate) {
Andy Hung264fa4c2024-02-21 15:52:12 -0800437 const sp<IAudioFlinger> af = get_audio_flinger();
Eric Laurentf6778fd2014-11-18 17:26:58 -0800438 if (af == 0) return PERMISSION_DENIED;
Glenn Kasten2c073da2016-02-26 09:14:08 -0800439 sp<AudioIoDescriptor> desc = getIoDescriptor(ioHandle);
440 if (desc == 0) {
441 *samplingRate = af->sampleRate(ioHandle);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700442 } else {
Mikhail Naganov88536df2021-07-26 17:30:29 -0700443 *samplingRate = desc->getSamplingRate();
Eric Laurentc2f1f072009-07-17 12:17:14 -0700444 }
Glenn Kastenf94006c2014-01-08 08:56:06 -0800445 if (*samplingRate == 0) {
Glenn Kasten2c073da2016-02-26 09:14:08 -0800446 ALOGE("AudioSystem::getSamplingRate failed for ioHandle %d", ioHandle);
Glenn Kastenf94006c2014-01-08 08:56:06 -0800447 return BAD_VALUE;
448 }
Eric Laurentc2f1f072009-07-17 12:17:14 -0700449
Glenn Kasten2c073da2016-02-26 09:14:08 -0800450 ALOGV("getSamplingRate() ioHandle %d, sampling rate %u", ioHandle, *samplingRate);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700451
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800452 return NO_ERROR;
453}
454
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800455status_t AudioSystem::getOutputFrameCount(size_t* frameCount, audio_stream_type_t streamType) {
Eric Laurentc2f1f072009-07-17 12:17:14 -0700456 audio_io_handle_t output;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800457
Dima Zavinfce7a472011-04-19 22:30:36 -0700458 if (streamType == AUDIO_STREAM_DEFAULT) {
459 streamType = AUDIO_STREAM_MUSIC;
Eric Laurentc2f1f072009-07-17 12:17:14 -0700460 }
Eric Laurent48f7f5e2009-04-02 09:32:43 -0700461
Glenn Kastenfff6d712012-01-12 16:38:12 -0800462 output = getOutput(streamType);
Glenn Kasten142f5192014-03-25 17:44:59 -0700463 if (output == AUDIO_IO_HANDLE_NONE) {
Eric Laurentc2f1f072009-07-17 12:17:14 -0700464 return PERMISSION_DENIED;
465 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800466
Jean-Michel Trivib7f24b12014-06-11 10:05:30 -0700467 return getFrameCount(output, frameCount);
Eric Laurent1a9ed112012-03-20 18:36:01 -0700468}
469
Glenn Kasten2c073da2016-02-26 09:14:08 -0800470status_t AudioSystem::getFrameCount(audio_io_handle_t ioHandle,
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800471 size_t* frameCount) {
Andy Hung264fa4c2024-02-21 15:52:12 -0800472 const sp<IAudioFlinger> af = get_audio_flinger();
Eric Laurentf6778fd2014-11-18 17:26:58 -0800473 if (af == 0) return PERMISSION_DENIED;
Glenn Kasten2c073da2016-02-26 09:14:08 -0800474 sp<AudioIoDescriptor> desc = getIoDescriptor(ioHandle);
475 if (desc == 0) {
476 *frameCount = af->frameCount(ioHandle);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700477 } else {
Mikhail Naganov88536df2021-07-26 17:30:29 -0700478 *frameCount = desc->getFrameCount();
Eric Laurentc2f1f072009-07-17 12:17:14 -0700479 }
Glenn Kastenf94006c2014-01-08 08:56:06 -0800480 if (*frameCount == 0) {
Glenn Kasten2c073da2016-02-26 09:14:08 -0800481 ALOGE("AudioSystem::getFrameCount failed for ioHandle %d", ioHandle);
Glenn Kastenf94006c2014-01-08 08:56:06 -0800482 return BAD_VALUE;
483 }
Eric Laurentc2f1f072009-07-17 12:17:14 -0700484
Glenn Kasten2c073da2016-02-26 09:14:08 -0800485 ALOGV("getFrameCount() ioHandle %d, frameCount %zu", ioHandle, *frameCount);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700486
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800487 return NO_ERROR;
488}
489
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800490status_t AudioSystem::getOutputLatency(uint32_t* latency, audio_stream_type_t streamType) {
Eric Laurentc2f1f072009-07-17 12:17:14 -0700491 audio_io_handle_t output;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800492
Dima Zavinfce7a472011-04-19 22:30:36 -0700493 if (streamType == AUDIO_STREAM_DEFAULT) {
494 streamType = AUDIO_STREAM_MUSIC;
Eric Laurentc2f1f072009-07-17 12:17:14 -0700495 }
Eric Laurent48f7f5e2009-04-02 09:32:43 -0700496
Glenn Kastenfff6d712012-01-12 16:38:12 -0800497 output = getOutput(streamType);
Glenn Kasten142f5192014-03-25 17:44:59 -0700498 if (output == AUDIO_IO_HANDLE_NONE) {
Eric Laurentc2f1f072009-07-17 12:17:14 -0700499 return PERMISSION_DENIED;
500 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800501
Glenn Kasten241618f2014-03-25 17:48:57 -0700502 return getLatency(output, latency);
Eric Laurent1a9ed112012-03-20 18:36:01 -0700503}
504
505status_t AudioSystem::getLatency(audio_io_handle_t output,
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800506 uint32_t* latency) {
Andy Hung264fa4c2024-02-21 15:52:12 -0800507 const sp<IAudioFlinger> af = get_audio_flinger();
Eric Laurentf6778fd2014-11-18 17:26:58 -0800508 if (af == 0) return PERMISSION_DENIED;
Eric Laurent296fb132015-05-01 11:38:42 -0700509 sp<AudioIoDescriptor> outputDesc = getIoDescriptor(output);
Eric Laurent73e26b62015-04-27 16:55:58 -0700510 if (outputDesc == 0) {
Eric Laurentc2f1f072009-07-17 12:17:14 -0700511 *latency = af->latency(output);
512 } else {
Mikhail Naganov88536df2021-07-26 17:30:29 -0700513 *latency = outputDesc->getLatency();
Eric Laurentc2f1f072009-07-17 12:17:14 -0700514 }
515
Glenn Kasten241618f2014-03-25 17:48:57 -0700516 ALOGV("getLatency() output %d, latency %d", output, *latency);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700517
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800518 return NO_ERROR;
519}
520
Glenn Kastendd8104c2012-07-02 12:42:44 -0700521status_t AudioSystem::getInputBufferSize(uint32_t sampleRate, audio_format_t format,
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800522 audio_channel_mask_t channelMask, size_t* buffSize) {
Eric Laurent296fb132015-05-01 11:38:42 -0700523 const sp<AudioFlingerClient> afc = getAudioFlingerClient();
524 if (afc == 0) {
525 return NO_INIT;
526 }
527 return afc->getInputBufferSize(sampleRate, format, channelMask, buffSize);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800528}
529
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800530status_t AudioSystem::setVoiceVolume(float value) {
Andy Hung264fa4c2024-02-21 15:52:12 -0800531 const sp<IAudioFlinger> af = get_audio_flinger();
Eric Laurentf0ee6f42009-10-21 08:14:22 -0700532 if (af == 0) return PERMISSION_DENIED;
533 return af->setVoiceVolume(value);
534}
535
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800536status_t AudioSystem::getRenderPosition(audio_io_handle_t output, uint32_t* halFrames,
537 uint32_t* dspFrames) {
Andy Hung264fa4c2024-02-21 15:52:12 -0800538 const sp<IAudioFlinger> af = get_audio_flinger();
Eric Laurent342e9cf2010-01-19 17:37:09 -0800539 if (af == 0) return PERMISSION_DENIED;
540
Richard Fitzgeraldad3af332013-03-25 16:54:37 +0000541 return af->getRenderPosition(halFrames, dspFrames, output);
Eric Laurent342e9cf2010-01-19 17:37:09 -0800542}
543
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800544uint32_t AudioSystem::getInputFramesLost(audio_io_handle_t ioHandle) {
Andy Hung264fa4c2024-02-21 15:52:12 -0800545 const sp<IAudioFlinger> af = get_audio_flinger();
Glenn Kasten5f972c02014-01-13 09:59:31 -0800546 uint32_t result = 0;
Eric Laurent05bca2f2010-02-26 02:47:27 -0800547 if (af == 0) return result;
Glenn Kasten142f5192014-03-25 17:44:59 -0700548 if (ioHandle == AUDIO_IO_HANDLE_NONE) return result;
Eric Laurent05bca2f2010-02-26 02:47:27 -0800549
550 result = af->getInputFramesLost(ioHandle);
551 return result;
552}
553
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800554audio_unique_id_t AudioSystem::newAudioUniqueId(audio_unique_id_use_t use) {
Mikhail Naganov2996f672019-04-18 12:29:59 -0700555 // Must not use AF as IDs will re-roll on audioserver restart, b/130369529.
Andy Hung264fa4c2024-02-21 15:52:12 -0800556 const sp<IAudioFlinger> af = get_audio_flinger();
Eric Laurentde3f8392014-07-27 18:38:22 -0700557 if (af == 0) return AUDIO_UNIQUE_ID_ALLOCATE;
Glenn Kasteneeecb982016-02-26 10:44:04 -0800558 return af->newAudioUniqueId(use);
Eric Laurentbe916aa2010-06-01 23:49:17 -0700559}
560
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800561void AudioSystem::acquireAudioSessionId(audio_session_t audioSession, pid_t pid, uid_t uid) {
Andy Hung264fa4c2024-02-21 15:52:12 -0800562 const sp<IAudioFlinger> af = get_audio_flinger();
Marco Nelissen3a34bef2011-08-02 13:33:41 -0700563 if (af != 0) {
Andy Hung8b0bfd92019-12-23 13:11:11 -0800564 af->acquireAudioSessionId(audioSession, pid, uid);
Marco Nelissen3a34bef2011-08-02 13:33:41 -0700565 }
566}
567
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800568void AudioSystem::releaseAudioSessionId(audio_session_t audioSession, pid_t pid) {
Andy Hung264fa4c2024-02-21 15:52:12 -0800569 const sp<IAudioFlinger> af = get_audio_flinger();
Marco Nelissen3a34bef2011-08-02 13:33:41 -0700570 if (af != 0) {
Marco Nelissend457c972014-02-11 08:47:07 -0800571 af->releaseAudioSessionId(audioSession, pid);
Marco Nelissen3a34bef2011-08-02 13:33:41 -0700572 }
573}
574
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800575audio_hw_sync_t AudioSystem::getAudioHwSyncForSession(audio_session_t sessionId) {
Andy Hung264fa4c2024-02-21 15:52:12 -0800576 const sp<IAudioFlinger> af = get_audio_flinger();
Eric Laurent93c3d412014-08-01 14:48:35 -0700577 if (af == 0) return AUDIO_HW_SYNC_INVALID;
578 return af->getAudioHwSyncForSession(sessionId);
579}
580
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800581status_t AudioSystem::systemReady() {
Andy Hung264fa4c2024-02-21 15:52:12 -0800582 const sp<IAudioFlinger> af = get_audio_flinger();
Eric Laurent72e3f392015-05-20 14:43:50 -0700583 if (af == 0) return NO_INIT;
584 return af->systemReady();
585}
586
Eric Laurentd66d7a12021-07-13 13:35:32 +0200587status_t AudioSystem::audioPolicyReady() {
Andy Hung264fa4c2024-02-21 15:52:12 -0800588 const sp<IAudioFlinger> af = get_audio_flinger();
Eric Laurentd66d7a12021-07-13 13:35:32 +0200589 if (af == 0) return NO_INIT;
590 return af->audioPolicyReady();
591}
592
Glenn Kasten4a8308b2016-04-18 14:10:01 -0700593status_t AudioSystem::getFrameCountHAL(audio_io_handle_t ioHandle,
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800594 size_t* frameCount) {
Andy Hung264fa4c2024-02-21 15:52:12 -0800595 const sp<IAudioFlinger> af = get_audio_flinger();
Glenn Kasten4a8308b2016-04-18 14:10:01 -0700596 if (af == 0) return PERMISSION_DENIED;
597 sp<AudioIoDescriptor> desc = getIoDescriptor(ioHandle);
598 if (desc == 0) {
599 *frameCount = af->frameCountHAL(ioHandle);
600 } else {
Mikhail Naganov88536df2021-07-26 17:30:29 -0700601 *frameCount = desc->getFrameCountHAL();
Glenn Kasten4a8308b2016-04-18 14:10:01 -0700602 }
603 if (*frameCount == 0) {
604 ALOGE("AudioSystem::getFrameCountHAL failed for ioHandle %d", ioHandle);
605 return BAD_VALUE;
606 }
607
608 ALOGV("getFrameCountHAL() ioHandle %d, frameCount %zu", ioHandle, *frameCount);
609
610 return NO_ERROR;
611}
612
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800613// ---------------------------------------------------------------------------
614
Eric Laurent73e26b62015-04-27 16:55:58 -0700615
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800616void AudioSystem::AudioFlingerClient::clearIoCache() {
Andy Hungacd955a2024-02-16 17:14:36 -0800617 std::lock_guard _l(mMutex);
Eric Laurent73e26b62015-04-27 16:55:58 -0700618 mIoDescriptors.clear();
619 mInBuffSize = 0;
620 mInSamplingRate = 0;
621 mInFormat = AUDIO_FORMAT_DEFAULT;
622 mInChannelMask = AUDIO_CHANNEL_NONE;
623}
624
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800625void AudioSystem::AudioFlingerClient::binderDied(const wp<IBinder>& who __unused) {
Andy Hungda1fb072024-02-20 19:08:08 -0800626 gAudioFlingerServiceHandler.clearService();
Ytai Ben-Tsvi000c3e42020-01-09 15:26:40 -0800627 reportError(DEAD_OBJECT);
628
Steve Block5ff1dd52012-01-05 23:22:43 +0000629 ALOGW("AudioFlinger server died!");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800630}
631
Ytai Ben-Tsvi10dc0a62020-09-18 11:31:55 -0700632Status AudioSystem::AudioFlingerClient::ioConfigChanged(
633 media::AudioIoConfigEvent _event,
634 const media::AudioIoDescriptor& _ioDesc) {
Mikhail Naganov88536df2021-07-26 17:30:29 -0700635 audio_io_config_event_t event = VALUE_OR_RETURN_BINDER_STATUS(
636 aidl2legacy_AudioIoConfigEvent_audio_io_config_event_t(_event));
Ytai Ben-Tsvi10dc0a62020-09-18 11:31:55 -0700637 sp<AudioIoDescriptor> ioDesc(
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800638 VALUE_OR_RETURN_BINDER_STATUS(
639 aidl2legacy_AudioIoDescriptor_AudioIoDescriptor(_ioDesc)));
Ytai Ben-Tsvi10dc0a62020-09-18 11:31:55 -0700640
Steve Block3856b092011-10-20 11:56:00 +0100641 ALOGV("ioConfigChanged() event %d", event);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700642
Mikhail Naganov88536df2021-07-26 17:30:29 -0700643 if (ioDesc->getIoHandle() == AUDIO_IO_HANDLE_NONE) return Status::ok();
Eric Laurentc2f1f072009-07-17 12:17:14 -0700644
Eric Laurent296fb132015-05-01 11:38:42 -0700645 audio_port_handle_t deviceId = AUDIO_PORT_HANDLE_NONE;
Eric Laurent09f1ed22019-04-24 17:45:17 -0700646 std::vector<sp<AudioDeviceCallback>> callbacksToCall;
Eric Laurent296fb132015-05-01 11:38:42 -0700647 {
Andy Hungacd955a2024-02-16 17:14:36 -0800648 std::lock_guard _l(mMutex);
Eric Laurent09f1ed22019-04-24 17:45:17 -0700649 auto callbacks = std::map<audio_port_handle_t, wp<AudioDeviceCallback>>();
Eric Laurent296fb132015-05-01 11:38:42 -0700650
651 switch (event) {
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800652 case AUDIO_OUTPUT_OPENED:
653 case AUDIO_OUTPUT_REGISTERED:
654 case AUDIO_INPUT_OPENED:
655 case AUDIO_INPUT_REGISTERED: {
Andy Hungd211ce02024-02-16 15:34:07 -0800656 if (sp<AudioIoDescriptor> oldDesc = getIoDescriptor_l(ioDesc->getIoHandle())) {
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800657 deviceId = oldDesc->getDeviceId();
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800658 }
Andy Hungd211ce02024-02-16 15:34:07 -0800659 mIoDescriptors[ioDesc->getIoHandle()] = ioDesc;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800660
661 if (ioDesc->getDeviceId() != AUDIO_PORT_HANDLE_NONE) {
662 deviceId = ioDesc->getDeviceId();
663 if (event == AUDIO_OUTPUT_OPENED || event == AUDIO_INPUT_OPENED) {
Mikhail Naganov88536df2021-07-26 17:30:29 -0700664 auto it = mAudioDeviceCallbacks.find(ioDesc->getIoHandle());
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800665 if (it != mAudioDeviceCallbacks.end()) {
666 callbacks = it->second;
667 }
668 }
669 }
Mikhail Naganov88536df2021-07-26 17:30:29 -0700670 ALOGV("ioConfigChanged() new %s %s %s",
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800671 event == AUDIO_OUTPUT_OPENED || event == AUDIO_OUTPUT_REGISTERED ?
672 "output" : "input",
673 event == AUDIO_OUTPUT_OPENED || event == AUDIO_INPUT_OPENED ?
674 "opened" : "registered",
Mikhail Naganov88536df2021-07-26 17:30:29 -0700675 ioDesc->toDebugString().c_str());
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800676 }
677 break;
678 case AUDIO_OUTPUT_CLOSED:
679 case AUDIO_INPUT_CLOSED: {
Mikhail Naganov88536df2021-07-26 17:30:29 -0700680 if (getIoDescriptor_l(ioDesc->getIoHandle()) == 0) {
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800681 ALOGW("ioConfigChanged() closing unknown %s %d",
Mikhail Naganov88536df2021-07-26 17:30:29 -0700682 event == AUDIO_OUTPUT_CLOSED ? "output" : "input", ioDesc->getIoHandle());
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800683 break;
684 }
685 ALOGV("ioConfigChanged() %s %d closed",
Mikhail Naganov88536df2021-07-26 17:30:29 -0700686 event == AUDIO_OUTPUT_CLOSED ? "output" : "input", ioDesc->getIoHandle());
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800687
Andy Hungd211ce02024-02-16 15:34:07 -0800688 mIoDescriptors.erase(ioDesc->getIoHandle());
Mikhail Naganov88536df2021-07-26 17:30:29 -0700689 mAudioDeviceCallbacks.erase(ioDesc->getIoHandle());
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800690 }
691 break;
692
693 case AUDIO_OUTPUT_CONFIG_CHANGED:
694 case AUDIO_INPUT_CONFIG_CHANGED: {
Mikhail Naganov88536df2021-07-26 17:30:29 -0700695 sp<AudioIoDescriptor> oldDesc = getIoDescriptor_l(ioDesc->getIoHandle());
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800696 if (oldDesc == 0) {
697 ALOGW("ioConfigChanged() modifying unknown %s! %d",
698 event == AUDIO_OUTPUT_CONFIG_CHANGED ? "output" : "input",
Mikhail Naganov88536df2021-07-26 17:30:29 -0700699 ioDesc->getIoHandle());
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800700 break;
701 }
702
Eric Laurent7c1ec5f2015-07-09 14:52:47 -0700703 deviceId = oldDesc->getDeviceId();
Andy Hungd211ce02024-02-16 15:34:07 -0800704 mIoDescriptors[ioDesc->getIoHandle()] = ioDesc;
Eric Laurent296fb132015-05-01 11:38:42 -0700705
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800706 if (deviceId != ioDesc->getDeviceId()) {
707 deviceId = ioDesc->getDeviceId();
Mikhail Naganov88536df2021-07-26 17:30:29 -0700708 auto it = mAudioDeviceCallbacks.find(ioDesc->getIoHandle());
Eric Laurent09f1ed22019-04-24 17:45:17 -0700709 if (it != mAudioDeviceCallbacks.end()) {
710 callbacks = it->second;
Francois Gaffie24a9fb02019-01-18 17:51:34 +0100711 }
712 }
Mikhail Naganov88536df2021-07-26 17:30:29 -0700713 ALOGV("ioConfigChanged() new config for %s %s",
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800714 event == AUDIO_OUTPUT_CONFIG_CHANGED ? "output" : "input",
Mikhail Naganov88536df2021-07-26 17:30:29 -0700715 ioDesc->toDebugString().c_str());
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800716
Eric Laurent296fb132015-05-01 11:38:42 -0700717 }
Eric Laurent296fb132015-05-01 11:38:42 -0700718 break;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800719 case AUDIO_CLIENT_STARTED: {
Mikhail Naganov88536df2021-07-26 17:30:29 -0700720 sp<AudioIoDescriptor> oldDesc = getIoDescriptor_l(ioDesc->getIoHandle());
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800721 if (oldDesc == 0) {
Mikhail Naganov88536df2021-07-26 17:30:29 -0700722 ALOGW("ioConfigChanged() start client on unknown io! %d",
723 ioDesc->getIoHandle());
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800724 break;
725 }
726 ALOGV("ioConfigChanged() AUDIO_CLIENT_STARTED io %d port %d num callbacks %zu",
Mikhail Naganov88536df2021-07-26 17:30:29 -0700727 ioDesc->getIoHandle(), ioDesc->getPortId(), mAudioDeviceCallbacks.size());
728 oldDesc->setPatch(ioDesc->getPatch());
729 auto it = mAudioDeviceCallbacks.find(ioDesc->getIoHandle());
Eric Laurent09f1ed22019-04-24 17:45:17 -0700730 if (it != mAudioDeviceCallbacks.end()) {
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800731 auto cbks = it->second;
Mikhail Naganov88536df2021-07-26 17:30:29 -0700732 auto it2 = cbks.find(ioDesc->getPortId());
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800733 if (it2 != cbks.end()) {
Mikhail Naganov88536df2021-07-26 17:30:29 -0700734 callbacks.emplace(ioDesc->getPortId(), it2->second);
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800735 deviceId = oldDesc->getDeviceId();
736 }
Eric Laurent09f1ed22019-04-24 17:45:17 -0700737 }
Eric Laurent296fb132015-05-01 11:38:42 -0700738 }
Eric Laurent09f1ed22019-04-24 17:45:17 -0700739 break;
Eric Laurent09f1ed22019-04-24 17:45:17 -0700740 }
741
742 for (auto wpCbk : callbacks) {
743 sp<AudioDeviceCallback> spCbk = wpCbk.second.promote();
744 if (spCbk != nullptr) {
745 callbacksToCall.push_back(spCbk);
746 }
Eric Laurentad2e7b92017-09-14 20:06:42 -0700747 }
Eric Laurent4463ff52019-02-07 13:56:09 -0800748 }
749
Andy Hungacd955a2024-02-16 17:14:36 -0800750 // Callbacks must be called without mMutex held. May lead to dead lock if calling for
751 // example getRoutedDevice that updates the device and tries to acquire mMutex.
Eric Laurent09f1ed22019-04-24 17:45:17 -0700752 for (auto cb : callbacksToCall) {
Mikhail Naganov88536df2021-07-26 17:30:29 -0700753 // If callbacksToCall is not empty, it implies ioDesc->getIoHandle() and deviceId are valid
754 cb->onAudioDeviceUpdate(ioDesc->getIoHandle(), deviceId);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700755 }
Ytai Ben-Tsvi10dc0a62020-09-18 11:31:55 -0700756
757 return Status::ok();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800758}
759
Eric Laurent076e7c72022-05-03 18:12:28 +0200760Status AudioSystem::AudioFlingerClient::onSupportedLatencyModesChanged(
Mikhail Naganovf53e1822022-12-18 02:48:14 +0000761 int output, const std::vector<media::audio::common::AudioLatencyMode>& latencyModes) {
Eric Laurent076e7c72022-05-03 18:12:28 +0200762 audio_io_handle_t outputLegacy = VALUE_OR_RETURN_BINDER_STATUS(
763 aidl2legacy_int32_t_audio_io_handle_t(output));
764 std::vector<audio_latency_mode_t> modesLegacy = VALUE_OR_RETURN_BINDER_STATUS(
765 convertContainer<std::vector<audio_latency_mode_t>>(
Mikhail Naganovf53e1822022-12-18 02:48:14 +0000766 latencyModes, aidl2legacy_AudioLatencyMode_audio_latency_mode_t));
Eric Laurent076e7c72022-05-03 18:12:28 +0200767
768 std::vector<sp<SupportedLatencyModesCallback>> callbacks;
769 {
Andy Hungacd955a2024-02-16 17:14:36 -0800770 std::lock_guard _l(mMutex);
Eric Laurent076e7c72022-05-03 18:12:28 +0200771 for (auto callback : mSupportedLatencyModesCallbacks) {
772 if (auto ref = callback.promote(); ref != nullptr) {
773 callbacks.push_back(ref);
774 }
775 }
776 }
777 for (const auto& callback : callbacks) {
778 callback->onSupportedLatencyModesChanged(outputLegacy, modesLegacy);
779 }
780
781 return Status::ok();
782}
783
Eric Laurent73e26b62015-04-27 16:55:58 -0700784status_t AudioSystem::AudioFlingerClient::getInputBufferSize(
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800785 uint32_t sampleRate, audio_format_t format,
786 audio_channel_mask_t channelMask, size_t* buffSize) {
Andy Hung264fa4c2024-02-21 15:52:12 -0800787 const sp<IAudioFlinger> af = get_audio_flinger();
Eric Laurent73e26b62015-04-27 16:55:58 -0700788 if (af == 0) {
789 return PERMISSION_DENIED;
790 }
Andy Hungacd955a2024-02-16 17:14:36 -0800791 std::lock_guard _l(mMutex);
Eric Laurent73e26b62015-04-27 16:55:58 -0700792 // Do we have a stale mInBuffSize or are we requesting the input buffer size for new values
793 if ((mInBuffSize == 0) || (sampleRate != mInSamplingRate) || (format != mInFormat)
794 || (channelMask != mInChannelMask)) {
795 size_t inBuffSize = af->getInputBufferSize(sampleRate, format, channelMask);
796 if (inBuffSize == 0) {
Glenn Kasten49f36ba2017-12-06 13:02:02 -0800797 ALOGE("AudioSystem::getInputBufferSize failed sampleRate %d format %#x channelMask %#x",
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800798 sampleRate, format, channelMask);
Eric Laurent73e26b62015-04-27 16:55:58 -0700799 return BAD_VALUE;
800 }
801 // A benign race is possible here: we could overwrite a fresher cache entry
802 // save the request params
803 mInSamplingRate = sampleRate;
804 mInFormat = format;
805 mInChannelMask = channelMask;
806
807 mInBuffSize = inBuffSize;
808 }
809
810 *buffSize = mInBuffSize;
811
812 return NO_ERROR;
813}
814
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800815sp<AudioIoDescriptor>
816AudioSystem::AudioFlingerClient::getIoDescriptor_l(audio_io_handle_t ioHandle) {
Andy Hungd211ce02024-02-16 15:34:07 -0800817 if (const auto it = mIoDescriptors.find(ioHandle);
818 it != mIoDescriptors.end()) {
819 return it->second;
Eric Laurent73e26b62015-04-27 16:55:58 -0700820 }
Andy Hungd211ce02024-02-16 15:34:07 -0800821 return {};
Eric Laurent73e26b62015-04-27 16:55:58 -0700822}
823
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800824sp<AudioIoDescriptor> AudioSystem::AudioFlingerClient::getIoDescriptor(audio_io_handle_t ioHandle) {
Andy Hungacd955a2024-02-16 17:14:36 -0800825 std::lock_guard _l(mMutex);
Praveen Chavan49fdeaf2015-09-29 02:25:47 -0700826 return getIoDescriptor_l(ioHandle);
827}
828
Eric Laurent296fb132015-05-01 11:38:42 -0700829status_t AudioSystem::AudioFlingerClient::addAudioDeviceCallback(
Eric Laurent09f1ed22019-04-24 17:45:17 -0700830 const wp<AudioDeviceCallback>& callback, audio_io_handle_t audioIo,
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800831 audio_port_handle_t portId) {
Eric Laurent09f1ed22019-04-24 17:45:17 -0700832 ALOGV("%s audioIo %d portId %d", __func__, audioIo, portId);
Andy Hungacd955a2024-02-16 17:14:36 -0800833 std::lock_guard _l(mMutex);
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800834 auto& callbacks = mAudioDeviceCallbacks.emplace(
835 audioIo,
836 std::map<audio_port_handle_t, wp<AudioDeviceCallback>>()).first->second;
Eric Laurent09f1ed22019-04-24 17:45:17 -0700837 auto result = callbacks.try_emplace(portId, callback);
838 if (!result.second) {
839 return INVALID_OPERATION;
Eric Laurent296fb132015-05-01 11:38:42 -0700840 }
Eric Laurent296fb132015-05-01 11:38:42 -0700841 return NO_ERROR;
842}
843
844status_t AudioSystem::AudioFlingerClient::removeAudioDeviceCallback(
Eric Laurent09f1ed22019-04-24 17:45:17 -0700845 const wp<AudioDeviceCallback>& callback __unused, audio_io_handle_t audioIo,
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800846 audio_port_handle_t portId) {
Eric Laurent09f1ed22019-04-24 17:45:17 -0700847 ALOGV("%s audioIo %d portId %d", __func__, audioIo, portId);
Andy Hungacd955a2024-02-16 17:14:36 -0800848 std::lock_guard _l(mMutex);
Eric Laurent09f1ed22019-04-24 17:45:17 -0700849 auto it = mAudioDeviceCallbacks.find(audioIo);
850 if (it == mAudioDeviceCallbacks.end()) {
Eric Laurent296fb132015-05-01 11:38:42 -0700851 return INVALID_OPERATION;
852 }
Eric Laurent09f1ed22019-04-24 17:45:17 -0700853 if (it->second.erase(portId) == 0) {
Eric Laurent296fb132015-05-01 11:38:42 -0700854 return INVALID_OPERATION;
855 }
Eric Laurent09f1ed22019-04-24 17:45:17 -0700856 if (it->second.size() == 0) {
857 mAudioDeviceCallbacks.erase(audioIo);
Eric Laurent296fb132015-05-01 11:38:42 -0700858 }
859 return NO_ERROR;
860}
861
Eric Laurent076e7c72022-05-03 18:12:28 +0200862status_t AudioSystem::AudioFlingerClient::addSupportedLatencyModesCallback(
863 const sp<SupportedLatencyModesCallback>& callback) {
Andy Hungacd955a2024-02-16 17:14:36 -0800864 std::lock_guard _l(mMutex);
Eric Laurent076e7c72022-05-03 18:12:28 +0200865 if (std::find(mSupportedLatencyModesCallbacks.begin(),
866 mSupportedLatencyModesCallbacks.end(),
867 callback) != mSupportedLatencyModesCallbacks.end()) {
868 return INVALID_OPERATION;
869 }
870 mSupportedLatencyModesCallbacks.push_back(callback);
871 return NO_ERROR;
872}
873
874status_t AudioSystem::AudioFlingerClient::removeSupportedLatencyModesCallback(
875 const sp<SupportedLatencyModesCallback>& callback) {
Andy Hungacd955a2024-02-16 17:14:36 -0800876 std::lock_guard _l(mMutex);
Eric Laurent076e7c72022-05-03 18:12:28 +0200877 auto it = std::find(mSupportedLatencyModesCallbacks.begin(),
878 mSupportedLatencyModesCallbacks.end(),
879 callback);
880 if (it == mSupportedLatencyModesCallbacks.end()) {
881 return INVALID_OPERATION;
882 }
883 mSupportedLatencyModesCallbacks.erase(it);
884 return NO_ERROR;
885}
886
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800887/* static */ uintptr_t AudioSystem::addErrorCallback(audio_error_callback cb) {
Andy Hungacd955a2024-02-16 17:14:36 -0800888 std::lock_guard _l(gErrorCallbacksMutex);
Ytai Ben-Tsvi000c3e42020-01-09 15:26:40 -0800889 gAudioErrorCallbacks.insert(cb);
890 return reinterpret_cast<uintptr_t>(cb);
891}
892
893/* static */ void AudioSystem::removeErrorCallback(uintptr_t cb) {
Andy Hungacd955a2024-02-16 17:14:36 -0800894 std::lock_guard _l(gErrorCallbacksMutex);
Ytai Ben-Tsvi000c3e42020-01-09 15:26:40 -0800895 gAudioErrorCallbacks.erase(reinterpret_cast<audio_error_callback>(cb));
896}
897
898/* static */ void AudioSystem::reportError(status_t err) {
Andy Hungacd955a2024-02-16 17:14:36 -0800899 std::lock_guard _l(gErrorCallbacksMutex);
Ytai Ben-Tsvi000c3e42020-01-09 15:26:40 -0800900 for (auto callback : gAudioErrorCallbacks) {
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800901 callback(err);
Ytai Ben-Tsvi000c3e42020-01-09 15:26:40 -0800902 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800903}
904
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800905/*static*/ void AudioSystem::setDynPolicyCallback(dynamic_policy_callback cb) {
Andy Hungacd955a2024-02-16 17:14:36 -0800906 std::lock_guard _l(gMutex);
Jean-Michel Trivif613d422015-04-23 18:41:29 -0700907 gDynPolicyCallback = cb;
908}
909
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800910/*static*/ void AudioSystem::setRecordConfigCallback(record_config_callback cb) {
Andy Hungacd955a2024-02-16 17:14:36 -0800911 std::lock_guard _l(gMutex);
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -0800912 gRecordConfigCallback = cb;
913}
914
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800915/*static*/ void AudioSystem::setRoutingCallback(routing_callback cb) {
Andy Hungacd955a2024-02-16 17:14:36 -0800916 std::lock_guard _l(gMutex);
Jean-Michel Trivi9a6b9ad2020-10-22 16:46:43 -0700917 gRoutingCallback = cb;
918}
919
Jean-Michel Trivi78f2b302022-04-15 18:18:41 +0000920/*static*/ void AudioSystem::setVolInitReqCallback(vol_range_init_req_callback cb) {
Andy Hungacd955a2024-02-16 17:14:36 -0800921 std::lock_guard _l(gMutex);
Jean-Michel Trivi78f2b302022-04-15 18:18:41 +0000922 gVolRangeInitReqCallback = cb;
923}
924
Andy Hung88e71732024-02-21 16:44:27 -0800925struct AudioPolicyTraits {
926 static void onServiceCreate(const sp<IAudioPolicyService>& ap,
927 const sp<AudioSystem::AudioPolicyServiceClient>& apc) {
928 const int64_t token = IPCThreadState::self()->clearCallingIdentity();
Eric Laurent0ebd5f92014-11-19 19:04:52 -0800929 ap->registerClient(apc);
François Gaffie24437602018-04-23 15:08:59 +0200930 ap->setAudioPortCallbacksEnabled(apc->isAudioPortCbEnabled());
François Gaffiecfe17322018-11-07 13:41:29 +0100931 ap->setAudioVolumeGroupCallbacksEnabled(apc->isAudioVolumeGroupCbEnabled());
François Gaffie24437602018-04-23 15:08:59 +0200932 IPCThreadState::self()->restoreCallingIdentity(token);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700933 }
Glenn Kastend2d089f2014-11-05 11:48:12 -0800934
Andy Hung88e71732024-02-21 16:44:27 -0800935 static void onClearService(const sp<AudioSystem::AudioPolicyServiceClient>&) {}
936
937 static constexpr const char *SERVICE_NAME = "media.audio_policy";
938};
939
940[[clang::no_destroy]] static constinit ServiceHandler<IAudioPolicyService,
941 AudioSystem::AudioPolicyServiceClient, IAudioPolicyService,
942 AudioPolicyTraits> gAudioPolicyServiceHandler;
943
944status_t AudioSystem::setLocalAudioPolicyService(const sp<IAudioPolicyService>& aps) {
945 return gAudioPolicyServiceHandler.setLocalService(aps);
946}
947
948sp<IAudioPolicyService> AudioSystem::get_audio_policy_service() {
949 return gAudioPolicyServiceHandler.getService();
Eric Laurentc2f1f072009-07-17 12:17:14 -0700950}
951
Mikhail Naganovec3d5792022-05-06 00:19:55 +0000952void AudioSystem::clearAudioPolicyService() {
Andy Hung88e71732024-02-21 16:44:27 -0800953 gAudioPolicyServiceHandler.clearService();
Mikhail Naganovec3d5792022-05-06 00:19:55 +0000954}
955
Shraddha Basantwanif2aa2502024-04-08 11:01:57 +0000956void AudioSystem::disableThreadPool() {
957 gAudioFlingerServiceHandler.disableThreadPool();
958 gAudioPolicyServiceHandler.disableThreadPool();
959}
960
Glenn Kastenfb1fdc92013-07-10 17:03:19 -0700961// ---------------------------------------------------------------------------
962
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800963void AudioSystem::onNewAudioModulesAvailable() {
Andy Hung264fa4c2024-02-21 15:52:12 -0800964 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Mikhail Naganov88b30d22020-03-09 19:43:13 +0000965 if (aps == 0) return;
966 aps->onNewAudioModulesAvailable();
967}
968
Nathalie Le Clair88fa2752021-11-23 13:03:41 +0100969status_t AudioSystem::setDeviceConnectionState(audio_policy_dev_state_t state,
970 const android::media::audio::common::AudioPort& port,
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800971 audio_format_t encodedFormat) {
Andy Hung264fa4c2024-02-21 15:52:12 -0800972 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Eric Laurent71b63e32011-09-02 14:20:56 -0700973
Eric Laurentc2f1f072009-07-17 12:17:14 -0700974 if (aps == 0) return PERMISSION_DENIED;
975
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800976 return statusTFromBinderStatus(
977 aps->setDeviceConnectionState(
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800978 VALUE_OR_RETURN_STATUS(
979 legacy2aidl_audio_policy_dev_state_t_AudioPolicyDeviceState(state)),
Nathalie Le Clair88fa2752021-11-23 13:03:41 +0100980 port,
Mikhail Naganovb60bd1b2021-07-15 17:31:43 -0700981 VALUE_OR_RETURN_STATUS(
982 legacy2aidl_audio_format_t_AudioFormatDescription(encodedFormat))));
Eric Laurentc2f1f072009-07-17 12:17:14 -0700983}
984
Dima Zavinfce7a472011-04-19 22:30:36 -0700985audio_policy_dev_state_t AudioSystem::getDeviceConnectionState(audio_devices_t device,
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800986 const char* device_address) {
Andy Hung264fa4c2024-02-21 15:52:12 -0800987 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Dima Zavinfce7a472011-04-19 22:30:36 -0700988 if (aps == 0) return AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE;
Eric Laurentc2f1f072009-07-17 12:17:14 -0700989
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800990 auto result = [&]() -> ConversionResult<audio_policy_dev_state_t> {
Mikhail Naganov932cb962021-09-16 01:05:27 +0000991 AudioDevice deviceAidl = VALUE_OR_RETURN(
992 legacy2aidl_audio_device_AudioDevice(device, device_address));
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800993
994 media::AudioPolicyDeviceState result;
995 RETURN_IF_ERROR(statusTFromBinderStatus(
996 aps->getDeviceConnectionState(deviceAidl, &result)));
997
998 return aidl2legacy_AudioPolicyDeviceState_audio_policy_dev_state_t(result);
999 }();
1000 return result.value_or(AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE);
Eric Laurentc2f1f072009-07-17 12:17:14 -07001001}
1002
Pavlin Radoslavovf862bc62016-12-26 18:57:22 -08001003status_t AudioSystem::handleDeviceConfigChange(audio_devices_t device,
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001004 const char* device_address,
1005 const char* device_name,
1006 audio_format_t encodedFormat) {
Andy Hung264fa4c2024-02-21 15:52:12 -08001007 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001008 const char* address = "";
1009 const char* name = "";
Pavlin Radoslavovf862bc62016-12-26 18:57:22 -08001010
1011 if (aps == 0) return PERMISSION_DENIED;
1012
1013 if (device_address != NULL) {
1014 address = device_address;
1015 }
1016 if (device_name != NULL) {
1017 name = device_name;
1018 }
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001019
Mikhail Naganov932cb962021-09-16 01:05:27 +00001020 AudioDevice deviceAidl = VALUE_OR_RETURN_STATUS(
1021 legacy2aidl_audio_device_AudioDevice(device, address));
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001022
1023 return statusTFromBinderStatus(
1024 aps->handleDeviceConfigChange(deviceAidl, name, VALUE_OR_RETURN_STATUS(
Mikhail Naganovb60bd1b2021-07-15 17:31:43 -07001025 legacy2aidl_audio_format_t_AudioFormatDescription(encodedFormat))));
Pavlin Radoslavovf862bc62016-12-26 18:57:22 -08001026}
1027
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001028status_t AudioSystem::setPhoneState(audio_mode_t state, uid_t uid) {
Glenn Kasten347966c2012-01-18 14:58:32 -08001029 if (uint32_t(state) >= AUDIO_MODE_CNT) return BAD_VALUE;
Andy Hung264fa4c2024-02-21 15:52:12 -08001030 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Eric Laurentc2f1f072009-07-17 12:17:14 -07001031 if (aps == 0) return PERMISSION_DENIED;
1032
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001033 return statusTFromBinderStatus(aps->setPhoneState(
1034 VALUE_OR_RETURN_STATUS(legacy2aidl_audio_mode_t_AudioMode(state)),
1035 VALUE_OR_RETURN_STATUS(legacy2aidl_uid_t_int32_t(uid))));
Eric Laurentc2f1f072009-07-17 12:17:14 -07001036}
1037
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001038status_t
1039AudioSystem::setForceUse(audio_policy_force_use_t usage, audio_policy_forced_cfg_t config) {
Andy Hung264fa4c2024-02-21 15:52:12 -08001040 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Eric Laurentc2f1f072009-07-17 12:17:14 -07001041 if (aps == 0) return PERMISSION_DENIED;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001042
1043 return statusTFromBinderStatus(
1044 aps->setForceUse(
1045 VALUE_OR_RETURN_STATUS(
1046 legacy2aidl_audio_policy_force_use_t_AudioPolicyForceUse(usage)),
1047 VALUE_OR_RETURN_STATUS(
1048 legacy2aidl_audio_policy_forced_cfg_t_AudioPolicyForcedConfig(
1049 config))));
Eric Laurentc2f1f072009-07-17 12:17:14 -07001050}
1051
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001052audio_policy_forced_cfg_t AudioSystem::getForceUse(audio_policy_force_use_t usage) {
Andy Hung264fa4c2024-02-21 15:52:12 -08001053 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Dima Zavinfce7a472011-04-19 22:30:36 -07001054 if (aps == 0) return AUDIO_POLICY_FORCE_NONE;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001055
1056 auto result = [&]() -> ConversionResult<audio_policy_forced_cfg_t> {
1057 media::AudioPolicyForceUse usageAidl = VALUE_OR_RETURN(
1058 legacy2aidl_audio_policy_force_use_t_AudioPolicyForceUse(usage));
1059 media::AudioPolicyForcedConfig configAidl;
1060 RETURN_IF_ERROR(statusTFromBinderStatus(
1061 aps->getForceUse(usageAidl, &configAidl)));
1062 return aidl2legacy_AudioPolicyForcedConfig_audio_policy_forced_cfg_t(configAidl);
1063 }();
1064
1065 return result.value_or(AUDIO_POLICY_FORCE_NONE);
Eric Laurentc2f1f072009-07-17 12:17:14 -07001066}
1067
1068
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001069audio_io_handle_t AudioSystem::getOutput(audio_stream_type_t stream) {
Andy Hung264fa4c2024-02-21 15:52:12 -08001070 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001071 if (aps == 0) return AUDIO_IO_HANDLE_NONE;
1072
1073 auto result = [&]() -> ConversionResult<audio_io_handle_t> {
Mikhail Naganovdbf03642021-08-25 18:15:32 -07001074 AudioStreamType streamAidl = VALUE_OR_RETURN(
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001075 legacy2aidl_audio_stream_type_t_AudioStreamType(stream));
1076 int32_t outputAidl;
1077 RETURN_IF_ERROR(
1078 statusTFromBinderStatus(aps->getOutput(streamAidl, &outputAidl)));
1079 return aidl2legacy_int32_t_audio_io_handle_t(outputAidl);
1080 }();
1081
1082 return result.value_or(AUDIO_IO_HANDLE_NONE);
Eric Laurentc2f1f072009-07-17 12:17:14 -07001083}
1084
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001085status_t AudioSystem::getOutputForAttr(audio_attributes_t* attr,
1086 audio_io_handle_t* output,
1087 audio_session_t session,
1088 audio_stream_type_t* stream,
Svet Ganov3e5f14f2021-05-13 22:51:08 +00001089 const AttributionSourceState& attributionSource,
jiabinf1c73972022-04-14 16:28:52 -07001090 audio_config_t* config,
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001091 audio_output_flags_t flags,
1092 audio_port_handle_t* selectedDeviceId,
1093 audio_port_handle_t* portId,
Eric Laurentb0a7bc92022-04-05 15:06:08 +02001094 std::vector<audio_io_handle_t>* secondaryOutputs,
jiabinc658e452022-10-21 20:52:21 +00001095 bool *isSpatialized,
Andy Hung6b137d12024-08-27 22:35:17 +00001096 bool *isBitPerfect,
1097 float *volume) {
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001098 if (attr == nullptr) {
1099 ALOGE("%s NULL audio attributes", __func__);
1100 return BAD_VALUE;
1101 }
1102 if (output == nullptr) {
1103 ALOGE("%s NULL output - shouldn't happen", __func__);
1104 return BAD_VALUE;
1105 }
1106 if (selectedDeviceId == nullptr) {
1107 ALOGE("%s NULL selectedDeviceId - shouldn't happen", __func__);
1108 return BAD_VALUE;
1109 }
1110 if (portId == nullptr) {
1111 ALOGE("%s NULL portId - shouldn't happen", __func__);
1112 return BAD_VALUE;
1113 }
1114 if (secondaryOutputs == nullptr) {
1115 ALOGE("%s NULL secondaryOutputs - shouldn't happen", __func__);
1116 return BAD_VALUE;
1117 }
1118
Andy Hung264fa4c2024-02-21 15:52:12 -08001119 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Eric Laurente83b55d2014-11-14 10:06:21 -08001120 if (aps == 0) return NO_INIT;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001121
Mikhail Naganov1c400902023-05-17 11:48:43 -07001122 media::audio::common::AudioAttributes attrAidl = VALUE_OR_RETURN_STATUS(
1123 legacy2aidl_audio_attributes_t_AudioAttributes(*attr));
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001124 int32_t sessionAidl = VALUE_OR_RETURN_STATUS(legacy2aidl_audio_session_t_int32_t(session));
Mikhail Naganovdbf03642021-08-25 18:15:32 -07001125 AudioConfig configAidl = VALUE_OR_RETURN_STATUS(
Mikhail Naganovde3fa182021-07-30 15:06:42 -07001126 legacy2aidl_audio_config_t_AudioConfig(*config, false /*isInput*/));
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001127 int32_t flagsAidl = VALUE_OR_RETURN_STATUS(
1128 legacy2aidl_audio_output_flags_t_int32_t_mask(flags));
Eric Laurentf99edd32021-02-01 15:57:33 +01001129 int32_t selectedDeviceIdAidl = VALUE_OR_RETURN_STATUS(
1130 legacy2aidl_audio_port_handle_t_int32_t(*selectedDeviceId));
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001131
1132 media::GetOutputForAttrResponse responseAidl;
1133
jiabinf1c73972022-04-14 16:28:52 -07001134 status_t status = statusTFromBinderStatus(
Svet Ganov3e5f14f2021-05-13 22:51:08 +00001135 aps->getOutputForAttr(attrAidl, sessionAidl, attributionSource, configAidl, flagsAidl,
jiabinf1c73972022-04-14 16:28:52 -07001136 selectedDeviceIdAidl, &responseAidl));
1137 if (status != NO_ERROR) {
1138 config->format = VALUE_OR_RETURN_STATUS(
1139 aidl2legacy_AudioFormatDescription_audio_format_t(responseAidl.configBase.format));
1140 config->channel_mask = VALUE_OR_RETURN_STATUS(
1141 aidl2legacy_AudioChannelLayout_audio_channel_mask_t(
1142 responseAidl.configBase.channelMask, false /*isInput*/));
1143 config->sample_rate = responseAidl.configBase.sampleRate;
1144 return status;
1145 }
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001146
1147 *output = VALUE_OR_RETURN_STATUS(
1148 aidl2legacy_int32_t_audio_io_handle_t(responseAidl.output));
1149
1150 if (stream != nullptr) {
1151 *stream = VALUE_OR_RETURN_STATUS(
1152 aidl2legacy_AudioStreamType_audio_stream_type_t(responseAidl.stream));
1153 }
1154 *selectedDeviceId = VALUE_OR_RETURN_STATUS(
1155 aidl2legacy_int32_t_audio_port_handle_t(responseAidl.selectedDeviceId));
1156 *portId = VALUE_OR_RETURN_STATUS(aidl2legacy_int32_t_audio_port_handle_t(responseAidl.portId));
1157 *secondaryOutputs = VALUE_OR_RETURN_STATUS(convertContainer<std::vector<audio_io_handle_t>>(
1158 responseAidl.secondaryOutputs, aidl2legacy_int32_t_audio_io_handle_t));
Eric Laurentb0a7bc92022-04-05 15:06:08 +02001159 *isSpatialized = responseAidl.isSpatialized;
jiabinc658e452022-10-21 20:52:21 +00001160 *isBitPerfect = responseAidl.isBitPerfect;
Eric Laurent0d13fea2022-11-04 17:12:08 +01001161 *attr = VALUE_OR_RETURN_STATUS(
Mikhail Naganov1c400902023-05-17 11:48:43 -07001162 aidl2legacy_AudioAttributes_audio_attributes_t(responseAidl.attr));
Andy Hung6b137d12024-08-27 22:35:17 +00001163 *volume = responseAidl.volume;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001164
1165 return OK;
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -07001166}
1167
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001168status_t AudioSystem::startOutput(audio_port_handle_t portId) {
Andy Hung264fa4c2024-02-21 15:52:12 -08001169 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Eric Laurentc2f1f072009-07-17 12:17:14 -07001170 if (aps == 0) return PERMISSION_DENIED;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001171
1172 int32_t portIdAidl = VALUE_OR_RETURN_STATUS(legacy2aidl_audio_port_handle_t_int32_t(portId));
1173 return statusTFromBinderStatus(aps->startOutput(portIdAidl));
Eric Laurentc2f1f072009-07-17 12:17:14 -07001174}
1175
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001176status_t AudioSystem::stopOutput(audio_port_handle_t portId) {
Andy Hung264fa4c2024-02-21 15:52:12 -08001177 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Eric Laurentc2f1f072009-07-17 12:17:14 -07001178 if (aps == 0) return PERMISSION_DENIED;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001179
1180 int32_t portIdAidl = VALUE_OR_RETURN_STATUS(legacy2aidl_audio_port_handle_t_int32_t(portId));
1181 return statusTFromBinderStatus(aps->stopOutput(portIdAidl));
Eric Laurentc2f1f072009-07-17 12:17:14 -07001182}
1183
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001184void AudioSystem::releaseOutput(audio_port_handle_t portId) {
Andy Hung264fa4c2024-02-21 15:52:12 -08001185 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Eric Laurentc2f1f072009-07-17 12:17:14 -07001186 if (aps == 0) return;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001187
1188 auto status = [&]() -> status_t {
1189 int32_t portIdAidl = VALUE_OR_RETURN_STATUS(
1190 legacy2aidl_audio_port_handle_t_int32_t(portId));
1191 RETURN_STATUS_IF_ERROR(statusTFromBinderStatus(aps->releaseOutput(portIdAidl)));
1192 return OK;
1193 }();
1194
1195 // Ignore status.
1196 (void) status;
Eric Laurentc2f1f072009-07-17 12:17:14 -07001197}
1198
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001199status_t AudioSystem::getInputForAttr(const audio_attributes_t* attr,
1200 audio_io_handle_t* input,
1201 audio_unique_id_t riid,
1202 audio_session_t session,
Svet Ganov3e5f14f2021-05-13 22:51:08 +00001203 const AttributionSourceState &attributionSource,
jiabinf1c73972022-04-14 16:28:52 -07001204 audio_config_base_t* config,
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001205 audio_input_flags_t flags,
1206 audio_port_handle_t* selectedDeviceId,
1207 audio_port_handle_t* portId) {
1208 if (attr == NULL) {
1209 ALOGE("getInputForAttr NULL attr - shouldn't happen");
1210 return BAD_VALUE;
1211 }
1212 if (input == NULL) {
1213 ALOGE("getInputForAttr NULL input - shouldn't happen");
1214 return BAD_VALUE;
1215 }
1216 if (selectedDeviceId == NULL) {
1217 ALOGE("getInputForAttr NULL selectedDeviceId - shouldn't happen");
1218 return BAD_VALUE;
1219 }
1220 if (portId == NULL) {
1221 ALOGE("getInputForAttr NULL portId - shouldn't happen");
1222 return BAD_VALUE;
1223 }
1224
Andy Hung264fa4c2024-02-21 15:52:12 -08001225 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Eric Laurentcaf7f482014-11-25 17:50:47 -08001226 if (aps == 0) return NO_INIT;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001227
Mikhail Naganov1c400902023-05-17 11:48:43 -07001228 media::audio::common::AudioAttributes attrAidl = VALUE_OR_RETURN_STATUS(
1229 legacy2aidl_audio_attributes_t_AudioAttributes(*attr));
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001230 int32_t inputAidl = VALUE_OR_RETURN_STATUS(legacy2aidl_audio_io_handle_t_int32_t(*input));
1231 int32_t riidAidl = VALUE_OR_RETURN_STATUS(legacy2aidl_audio_unique_id_t_int32_t(riid));
1232 int32_t sessionAidl = VALUE_OR_RETURN_STATUS(legacy2aidl_audio_session_t_int32_t(session));
Mikhail Naganovdbf03642021-08-25 18:15:32 -07001233 AudioConfigBase configAidl = VALUE_OR_RETURN_STATUS(
Mikhail Naganovde3fa182021-07-30 15:06:42 -07001234 legacy2aidl_audio_config_base_t_AudioConfigBase(*config, true /*isInput*/));
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001235 int32_t flagsAidl = VALUE_OR_RETURN_STATUS(legacy2aidl_audio_input_flags_t_int32_t_mask(flags));
Eric Laurentf99edd32021-02-01 15:57:33 +01001236 int32_t selectedDeviceIdAidl = VALUE_OR_RETURN_STATUS(
1237 legacy2aidl_audio_port_handle_t_int32_t(*selectedDeviceId));
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001238
1239 media::GetInputForAttrResponse response;
1240
jiabinf1c73972022-04-14 16:28:52 -07001241 status_t status = statusTFromBinderStatus(
Svet Ganov3e5f14f2021-05-13 22:51:08 +00001242 aps->getInputForAttr(attrAidl, inputAidl, riidAidl, sessionAidl, attributionSource,
jiabinf1c73972022-04-14 16:28:52 -07001243 configAidl, flagsAidl, selectedDeviceIdAidl, &response));
1244 if (status != NO_ERROR) {
1245 *config = VALUE_OR_RETURN_STATUS(
1246 aidl2legacy_AudioConfigBase_audio_config_base_t(response.config, true /*isInput*/));
1247 return status;
1248 }
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001249
1250 *input = VALUE_OR_RETURN_STATUS(aidl2legacy_int32_t_audio_io_handle_t(response.input));
1251 *selectedDeviceId = VALUE_OR_RETURN_STATUS(
1252 aidl2legacy_int32_t_audio_port_handle_t(response.selectedDeviceId));
1253 *portId = VALUE_OR_RETURN_STATUS(aidl2legacy_int32_t_audio_port_handle_t(response.portId));
1254
1255 return OK;
Eric Laurentc2f1f072009-07-17 12:17:14 -07001256}
1257
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001258status_t AudioSystem::startInput(audio_port_handle_t portId) {
Andy Hung264fa4c2024-02-21 15:52:12 -08001259 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Eric Laurentc2f1f072009-07-17 12:17:14 -07001260 if (aps == 0) return PERMISSION_DENIED;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001261
1262 int32_t portIdAidl = VALUE_OR_RETURN_STATUS(legacy2aidl_audio_port_handle_t_int32_t(portId));
1263 return statusTFromBinderStatus(aps->startInput(portIdAidl));
Eric Laurentc2f1f072009-07-17 12:17:14 -07001264}
1265
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001266status_t AudioSystem::stopInput(audio_port_handle_t portId) {
Andy Hung264fa4c2024-02-21 15:52:12 -08001267 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Eric Laurentc2f1f072009-07-17 12:17:14 -07001268 if (aps == 0) return PERMISSION_DENIED;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001269
1270 int32_t portIdAidl = VALUE_OR_RETURN_STATUS(legacy2aidl_audio_port_handle_t_int32_t(portId));
1271 return statusTFromBinderStatus(aps->stopInput(portIdAidl));
Eric Laurentc2f1f072009-07-17 12:17:14 -07001272}
1273
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001274void AudioSystem::releaseInput(audio_port_handle_t portId) {
Andy Hung264fa4c2024-02-21 15:52:12 -08001275 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Eric Laurentc2f1f072009-07-17 12:17:14 -07001276 if (aps == 0) return;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001277
1278 auto status = [&]() -> status_t {
1279 int32_t portIdAidl = VALUE_OR_RETURN_STATUS(
1280 legacy2aidl_audio_port_handle_t_int32_t(portId));
1281 RETURN_STATUS_IF_ERROR(statusTFromBinderStatus(aps->releaseInput(portIdAidl)));
1282 return OK;
1283 }();
1284
1285 // Ignore status.
1286 (void) status;
Eric Laurentc2f1f072009-07-17 12:17:14 -07001287}
1288
Vlad Popa87e0e582024-05-20 18:49:20 -07001289status_t AudioSystem::setDeviceAbsoluteVolumeEnabled(audio_devices_t deviceType,
1290 const char *address,
1291 bool enabled,
1292 audio_stream_type_t streamToDriveAbs) {
1293 const sp<IAudioPolicyService> aps = get_audio_policy_service();
1294 if (aps == nullptr) return PERMISSION_DENIED;
1295
1296 AudioDevice deviceAidl = VALUE_OR_RETURN_STATUS(
1297 legacy2aidl_audio_device_AudioDevice(deviceType, address));
1298 AudioStreamType streamToDriveAbsAidl = VALUE_OR_RETURN_STATUS(
1299 legacy2aidl_audio_stream_type_t_AudioStreamType(streamToDriveAbs));
1300 return statusTFromBinderStatus(
1301 aps->setDeviceAbsoluteVolumeEnabled(deviceAidl, enabled, streamToDriveAbsAidl));
1302}
1303
Dima Zavinfce7a472011-04-19 22:30:36 -07001304status_t AudioSystem::initStreamVolume(audio_stream_type_t stream,
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001305 int indexMin,
1306 int indexMax) {
Andy Hung264fa4c2024-02-21 15:52:12 -08001307 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Eric Laurentc2f1f072009-07-17 12:17:14 -07001308 if (aps == 0) return PERMISSION_DENIED;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001309
Mikhail Naganovdbf03642021-08-25 18:15:32 -07001310 AudioStreamType streamAidl = VALUE_OR_RETURN_STATUS(
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001311 legacy2aidl_audio_stream_type_t_AudioStreamType(stream));
1312 int32_t indexMinAidl = VALUE_OR_RETURN_STATUS(convertIntegral<int32_t>(indexMin));
1313 int32_t indexMaxAidl = VALUE_OR_RETURN_STATUS(convertIntegral<int32_t>(indexMax));
Mikhail Naganovec3d5792022-05-06 00:19:55 +00001314 status_t status = statusTFromBinderStatus(
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001315 aps->initStreamVolume(streamAidl, indexMinAidl, indexMaxAidl));
Mikhail Naganovec3d5792022-05-06 00:19:55 +00001316 if (status == DEAD_OBJECT) {
1317 // This is a critical operation since w/o proper stream volumes no audio
1318 // will be heard. Make sure we recover from a failure in any case.
1319 ALOGE("Received DEAD_OBJECT from APS, clearing the client");
1320 clearAudioPolicyService();
1321 }
1322 return status;
Eric Laurentc2f1f072009-07-17 12:17:14 -07001323}
1324
Eric Laurent83844cc2011-11-18 16:43:31 -08001325status_t AudioSystem::setStreamVolumeIndex(audio_stream_type_t stream,
1326 int index,
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001327 audio_devices_t device) {
Andy Hung264fa4c2024-02-21 15:52:12 -08001328 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Eric Laurentc2f1f072009-07-17 12:17:14 -07001329 if (aps == 0) return PERMISSION_DENIED;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001330
Mikhail Naganovdbf03642021-08-25 18:15:32 -07001331 AudioStreamType streamAidl = VALUE_OR_RETURN_STATUS(
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001332 legacy2aidl_audio_stream_type_t_AudioStreamType(stream));
1333 int32_t indexAidl = VALUE_OR_RETURN_STATUS(convertIntegral<int32_t>(index));
Mikhail Naganovf4a75362021-09-16 00:02:54 +00001334 AudioDeviceDescription deviceAidl = VALUE_OR_RETURN_STATUS(
Mikhail Naganov21a32ec2021-07-08 14:40:12 -07001335 legacy2aidl_audio_devices_t_AudioDeviceDescription(device));
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001336 return statusTFromBinderStatus(
1337 aps->setStreamVolumeIndex(streamAidl, deviceAidl, indexAidl));
Eric Laurentc2f1f072009-07-17 12:17:14 -07001338}
1339
Eric Laurent83844cc2011-11-18 16:43:31 -08001340status_t AudioSystem::getStreamVolumeIndex(audio_stream_type_t stream,
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001341 int* index,
1342 audio_devices_t device) {
Andy Hung264fa4c2024-02-21 15:52:12 -08001343 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Eric Laurentc2f1f072009-07-17 12:17:14 -07001344 if (aps == 0) return PERMISSION_DENIED;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001345
Mikhail Naganovdbf03642021-08-25 18:15:32 -07001346 AudioStreamType streamAidl = VALUE_OR_RETURN_STATUS(
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001347 legacy2aidl_audio_stream_type_t_AudioStreamType(stream));
Mikhail Naganovf4a75362021-09-16 00:02:54 +00001348 AudioDeviceDescription deviceAidl = VALUE_OR_RETURN_STATUS(
Mikhail Naganov21a32ec2021-07-08 14:40:12 -07001349 legacy2aidl_audio_devices_t_AudioDeviceDescription(device));
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001350 int32_t indexAidl;
1351 RETURN_STATUS_IF_ERROR(statusTFromBinderStatus(
1352 aps->getStreamVolumeIndex(streamAidl, deviceAidl, &indexAidl)));
1353 if (index != nullptr) {
1354 *index = VALUE_OR_RETURN_STATUS(convertIntegral<int>(indexAidl));
1355 }
1356 return OK;
Eric Laurentc2f1f072009-07-17 12:17:14 -07001357}
1358
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001359status_t AudioSystem::setVolumeIndexForAttributes(const audio_attributes_t& attr,
François Gaffiecfe17322018-11-07 13:41:29 +01001360 int index,
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001361 audio_devices_t device) {
Andy Hung264fa4c2024-02-21 15:52:12 -08001362 const sp<IAudioPolicyService> aps = get_audio_policy_service();
François Gaffiecfe17322018-11-07 13:41:29 +01001363 if (aps == 0) return PERMISSION_DENIED;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001364
Mikhail Naganov1c400902023-05-17 11:48:43 -07001365 media::audio::common::AudioAttributes attrAidl = VALUE_OR_RETURN_STATUS(
1366 legacy2aidl_audio_attributes_t_AudioAttributes(attr));
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001367 int32_t indexAidl = VALUE_OR_RETURN_STATUS(convertIntegral<int32_t>(index));
Mikhail Naganovf4a75362021-09-16 00:02:54 +00001368 AudioDeviceDescription deviceAidl = VALUE_OR_RETURN_STATUS(
Mikhail Naganov21a32ec2021-07-08 14:40:12 -07001369 legacy2aidl_audio_devices_t_AudioDeviceDescription(device));
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001370 return statusTFromBinderStatus(
1371 aps->setVolumeIndexForAttributes(attrAidl, deviceAidl, indexAidl));
François Gaffiecfe17322018-11-07 13:41:29 +01001372}
1373
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001374status_t AudioSystem::getVolumeIndexForAttributes(const audio_attributes_t& attr,
1375 int& index,
1376 audio_devices_t device) {
Andy Hung264fa4c2024-02-21 15:52:12 -08001377 const sp<IAudioPolicyService> aps = get_audio_policy_service();
François Gaffiecfe17322018-11-07 13:41:29 +01001378 if (aps == 0) return PERMISSION_DENIED;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001379
Mikhail Naganov1c400902023-05-17 11:48:43 -07001380 media::audio::common::AudioAttributes attrAidl = VALUE_OR_RETURN_STATUS(
1381 legacy2aidl_audio_attributes_t_AudioAttributes(attr));
Mikhail Naganovf4a75362021-09-16 00:02:54 +00001382 AudioDeviceDescription deviceAidl = VALUE_OR_RETURN_STATUS(
Mikhail Naganov21a32ec2021-07-08 14:40:12 -07001383 legacy2aidl_audio_devices_t_AudioDeviceDescription(device));
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001384 int32_t indexAidl;
1385 RETURN_STATUS_IF_ERROR(statusTFromBinderStatus(
1386 aps->getVolumeIndexForAttributes(attrAidl, deviceAidl, &indexAidl)));
1387 index = VALUE_OR_RETURN_STATUS(convertIntegral<int>(indexAidl));
1388 return OK;
François Gaffiecfe17322018-11-07 13:41:29 +01001389}
1390
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001391status_t AudioSystem::getMaxVolumeIndexForAttributes(const audio_attributes_t& attr, int& index) {
Andy Hung264fa4c2024-02-21 15:52:12 -08001392 const sp<IAudioPolicyService> aps = get_audio_policy_service();
François Gaffiecfe17322018-11-07 13:41:29 +01001393 if (aps == 0) return PERMISSION_DENIED;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001394
Mikhail Naganov1c400902023-05-17 11:48:43 -07001395 media::audio::common::AudioAttributes attrAidl = VALUE_OR_RETURN_STATUS(
1396 legacy2aidl_audio_attributes_t_AudioAttributes(attr));
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001397 int32_t indexAidl;
1398 RETURN_STATUS_IF_ERROR(statusTFromBinderStatus(
1399 aps->getMaxVolumeIndexForAttributes(attrAidl, &indexAidl)));
1400 index = VALUE_OR_RETURN_STATUS(convertIntegral<int>(indexAidl));
1401 return OK;
François Gaffiecfe17322018-11-07 13:41:29 +01001402}
1403
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001404status_t AudioSystem::getMinVolumeIndexForAttributes(const audio_attributes_t& attr, int& index) {
Andy Hung264fa4c2024-02-21 15:52:12 -08001405 const sp<IAudioPolicyService> aps = get_audio_policy_service();
François Gaffiecfe17322018-11-07 13:41:29 +01001406 if (aps == 0) return PERMISSION_DENIED;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001407
Mikhail Naganov1c400902023-05-17 11:48:43 -07001408 media::audio::common::AudioAttributes attrAidl = VALUE_OR_RETURN_STATUS(
1409 legacy2aidl_audio_attributes_t_AudioAttributes(attr));
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001410 int32_t indexAidl;
1411 RETURN_STATUS_IF_ERROR(statusTFromBinderStatus(
1412 aps->getMinVolumeIndexForAttributes(attrAidl, &indexAidl)));
1413 index = VALUE_OR_RETURN_STATUS(convertIntegral<int>(indexAidl));
1414 return OK;
François Gaffiecfe17322018-11-07 13:41:29 +01001415}
1416
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001417product_strategy_t AudioSystem::getStrategyForStream(audio_stream_type_t stream) {
Andy Hung264fa4c2024-02-21 15:52:12 -08001418 const sp<IAudioPolicyService> aps = get_audio_policy_service();
François Gaffied0ba9ed2018-11-05 11:50:42 +01001419 if (aps == 0) return PRODUCT_STRATEGY_NONE;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001420
1421 auto result = [&]() -> ConversionResult<product_strategy_t> {
Mikhail Naganovdbf03642021-08-25 18:15:32 -07001422 AudioStreamType streamAidl = VALUE_OR_RETURN(
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001423 legacy2aidl_audio_stream_type_t_AudioStreamType(stream));
1424 int32_t resultAidl;
1425 RETURN_IF_ERROR(statusTFromBinderStatus(
1426 aps->getStrategyForStream(streamAidl, &resultAidl)));
1427 return aidl2legacy_int32_t_product_strategy_t(resultAidl);
1428 }();
1429 return result.value_or(PRODUCT_STRATEGY_NONE);
Eric Laurentde070132010-07-13 04:45:46 -07001430}
1431
François Gaffie1e2b56f2022-04-01 14:34:29 +02001432status_t AudioSystem::getDevicesForAttributes(const audio_attributes_t& aa,
Andy Hung6d23c0f2022-02-16 09:37:15 -08001433 AudioDeviceTypeAddrVector* devices,
1434 bool forVolume) {
Jean-Michel Trivif41599b2020-01-07 14:22:08 -08001435 if (devices == nullptr) {
1436 return BAD_VALUE;
1437 }
Andy Hung264fa4c2024-02-21 15:52:12 -08001438 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Jean-Michel Trivif41599b2020-01-07 14:22:08 -08001439 if (aps == 0) return PERMISSION_DENIED;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001440
Mikhail Naganov1c400902023-05-17 11:48:43 -07001441 media::audio::common::AudioAttributes aaAidl = VALUE_OR_RETURN_STATUS(
1442 legacy2aidl_audio_attributes_t_AudioAttributes(aa));
Mikhail Naganovf4a75362021-09-16 00:02:54 +00001443 std::vector<AudioDevice> retAidl;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001444 RETURN_STATUS_IF_ERROR(
Andy Hung6d23c0f2022-02-16 09:37:15 -08001445 statusTFromBinderStatus(aps->getDevicesForAttributes(aaAidl, forVolume, &retAidl)));
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001446 *devices = VALUE_OR_RETURN_STATUS(
1447 convertContainer<AudioDeviceTypeAddrVector>(
1448 retAidl,
1449 aidl2legacy_AudioDeviceTypeAddress));
1450 return OK;
Jean-Michel Trivif41599b2020-01-07 14:22:08 -08001451}
1452
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001453audio_io_handle_t AudioSystem::getOutputForEffect(const effect_descriptor_t* desc) {
Andy Hung264fa4c2024-02-21 15:52:12 -08001454 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Glenn Kastenefa6ea92014-01-08 09:10:43 -08001455 // FIXME change return type to status_t, and return PERMISSION_DENIED here
Glenn Kasten142f5192014-03-25 17:44:59 -07001456 if (aps == 0) return AUDIO_IO_HANDLE_NONE;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001457
1458 auto result = [&]() -> ConversionResult<audio_io_handle_t> {
1459 media::EffectDescriptor descAidl = VALUE_OR_RETURN(
1460 legacy2aidl_effect_descriptor_t_EffectDescriptor(*desc));
1461 int32_t retAidl;
1462 RETURN_IF_ERROR(
1463 statusTFromBinderStatus(aps->getOutputForEffect(descAidl, &retAidl)));
1464 return aidl2legacy_int32_t_audio_io_handle_t(retAidl);
1465 }();
1466
1467 return result.value_or(AUDIO_IO_HANDLE_NONE);
Eric Laurentde070132010-07-13 04:45:46 -07001468}
1469
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001470status_t AudioSystem::registerEffect(const effect_descriptor_t* desc,
1471 audio_io_handle_t io,
1472 product_strategy_t strategy,
1473 audio_session_t session,
1474 int id) {
Andy Hung264fa4c2024-02-21 15:52:12 -08001475 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Eric Laurentde070132010-07-13 04:45:46 -07001476 if (aps == 0) return PERMISSION_DENIED;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001477
1478 media::EffectDescriptor descAidl = VALUE_OR_RETURN_STATUS(
1479 legacy2aidl_effect_descriptor_t_EffectDescriptor(*desc));
1480 int32_t ioAidl = VALUE_OR_RETURN_STATUS(legacy2aidl_audio_io_handle_t_int32_t(io));
1481 int32_t strategyAidl = VALUE_OR_RETURN_STATUS(aidl2legacy_int32_t_product_strategy_t(strategy));
1482 int32_t sessionAidl = VALUE_OR_RETURN_STATUS(legacy2aidl_audio_session_t_int32_t(session));
1483 int32_t idAidl = VALUE_OR_RETURN_STATUS(convertReinterpret<int32_t>(id));
1484 return statusTFromBinderStatus(
1485 aps->registerEffect(descAidl, ioAidl, strategyAidl, sessionAidl, idAidl));
Eric Laurentde070132010-07-13 04:45:46 -07001486}
1487
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001488status_t AudioSystem::unregisterEffect(int id) {
Andy Hung264fa4c2024-02-21 15:52:12 -08001489 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Eric Laurentde070132010-07-13 04:45:46 -07001490 if (aps == 0) return PERMISSION_DENIED;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001491
1492 int32_t idAidl = VALUE_OR_RETURN_STATUS(convertReinterpret<int32_t>(id));
1493 return statusTFromBinderStatus(
1494 aps->unregisterEffect(idAidl));
Eric Laurentde070132010-07-13 04:45:46 -07001495}
1496
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001497status_t AudioSystem::setEffectEnabled(int id, bool enabled) {
Andy Hung264fa4c2024-02-21 15:52:12 -08001498 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Eric Laurentdb7c0792011-08-10 10:37:50 -07001499 if (aps == 0) return PERMISSION_DENIED;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001500
1501 int32_t idAidl = VALUE_OR_RETURN_STATUS(convertReinterpret<int32_t>(id));
1502 return statusTFromBinderStatus(
1503 aps->setEffectEnabled(idAidl, enabled));
Eric Laurentdb7c0792011-08-10 10:37:50 -07001504}
1505
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001506status_t AudioSystem::moveEffectsToIo(const std::vector<int>& ids, audio_io_handle_t io) {
Andy Hung264fa4c2024-02-21 15:52:12 -08001507 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Eric Laurent6c796322019-04-09 14:13:17 -07001508 if (aps == 0) return PERMISSION_DENIED;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001509
1510 std::vector<int32_t> idsAidl = VALUE_OR_RETURN_STATUS(
1511 convertContainer<std::vector<int32_t>>(ids, convertReinterpret<int32_t, int>));
1512 int32_t ioAidl = VALUE_OR_RETURN_STATUS(legacy2aidl_audio_io_handle_t_int32_t(io));
1513 return statusTFromBinderStatus(aps->moveEffectsToIo(idsAidl, ioAidl));
Eric Laurent6c796322019-04-09 14:13:17 -07001514}
1515
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001516status_t AudioSystem::isStreamActive(audio_stream_type_t stream, bool* state, uint32_t inPastMs) {
Andy Hung264fa4c2024-02-21 15:52:12 -08001517 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Eric Laurenteda6c362011-02-02 09:33:30 -08001518 if (aps == 0) return PERMISSION_DENIED;
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001519 if (state == NULL) return BAD_VALUE;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001520
Mikhail Naganovdbf03642021-08-25 18:15:32 -07001521 AudioStreamType streamAidl = VALUE_OR_RETURN_STATUS(
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001522 legacy2aidl_audio_stream_type_t_AudioStreamType(stream));
1523 int32_t inPastMsAidl = VALUE_OR_RETURN_STATUS(convertIntegral<int32_t>(inPastMs));
1524 RETURN_STATUS_IF_ERROR(statusTFromBinderStatus(
1525 aps->isStreamActive(streamAidl, inPastMsAidl, state)));
1526 return OK;
Eric Laurenteda6c362011-02-02 09:33:30 -08001527}
1528
Jean-Michel Trivi272ab542013-02-04 16:26:02 -08001529status_t AudioSystem::isStreamActiveRemotely(audio_stream_type_t stream, bool* state,
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001530 uint32_t inPastMs) {
Andy Hung264fa4c2024-02-21 15:52:12 -08001531 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Jean-Michel Trivi272ab542013-02-04 16:26:02 -08001532 if (aps == 0) return PERMISSION_DENIED;
1533 if (state == NULL) return BAD_VALUE;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001534
Mikhail Naganovdbf03642021-08-25 18:15:32 -07001535 AudioStreamType streamAidl = VALUE_OR_RETURN_STATUS(
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001536 legacy2aidl_audio_stream_type_t_AudioStreamType(stream));
1537 int32_t inPastMsAidl = VALUE_OR_RETURN_STATUS(convertIntegral<int32_t>(inPastMs));
1538 RETURN_STATUS_IF_ERROR(statusTFromBinderStatus(
1539 aps->isStreamActiveRemotely(streamAidl, inPastMsAidl, state)));
1540 return OK;
Jean-Michel Trivi272ab542013-02-04 16:26:02 -08001541}
1542
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001543status_t AudioSystem::isSourceActive(audio_source_t stream, bool* state) {
Andy Hung264fa4c2024-02-21 15:52:12 -08001544 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Jean-Michel Trivid7086032012-10-10 12:11:16 -07001545 if (aps == 0) return PERMISSION_DENIED;
1546 if (state == NULL) return BAD_VALUE;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001547
Mikhail Naganovddceecc2021-09-03 13:58:56 -07001548 AudioSource streamAidl = VALUE_OR_RETURN_STATUS(
1549 legacy2aidl_audio_source_t_AudioSource(stream));
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001550 RETURN_STATUS_IF_ERROR(statusTFromBinderStatus(
1551 aps->isSourceActive(streamAidl, state)));
1552 return OK;
Jean-Michel Trivid7086032012-10-10 12:11:16 -07001553}
1554
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001555uint32_t AudioSystem::getPrimaryOutputSamplingRate() {
Andy Hung264fa4c2024-02-21 15:52:12 -08001556 const sp<IAudioFlinger> af = get_audio_flinger();
Glenn Kastencc0f1cf2012-09-24 11:27:18 -07001557 if (af == 0) return 0;
1558 return af->getPrimaryOutputSamplingRate();
1559}
1560
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001561size_t AudioSystem::getPrimaryOutputFrameCount() {
Andy Hung264fa4c2024-02-21 15:52:12 -08001562 const sp<IAudioFlinger> af = get_audio_flinger();
Glenn Kastencc0f1cf2012-09-24 11:27:18 -07001563 if (af == 0) return 0;
1564 return af->getPrimaryOutputFrameCount();
1565}
Eric Laurenteda6c362011-02-02 09:33:30 -08001566
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001567status_t AudioSystem::setLowRamDevice(bool isLowRamDevice, int64_t totalMemory) {
Andy Hung264fa4c2024-02-21 15:52:12 -08001568 const sp<IAudioFlinger> af = get_audio_flinger();
Glenn Kasten4182c4e2013-07-15 14:45:07 -07001569 if (af == 0) return PERMISSION_DENIED;
Andy Hung6f248bb2018-01-23 14:04:37 -08001570 return af->setLowRamDevice(isLowRamDevice, totalMemory);
Glenn Kasten4182c4e2013-07-15 14:45:07 -07001571}
1572
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001573void AudioSystem::clearAudioConfigCache() {
Glenn Kastend2d089f2014-11-05 11:48:12 -08001574 // called by restoreTrack_l(), which needs new IAudioFlinger and IAudioPolicyService instances
Steve Block3856b092011-10-20 11:56:00 +01001575 ALOGV("clearAudioConfigCache()");
Andy Hungda1fb072024-02-20 19:08:08 -08001576 gAudioFlingerServiceHandler.clearService();
Mikhail Naganovec3d5792022-05-06 00:19:55 +00001577 clearAudioPolicyService();
Eric Laurent9f6530f2011-08-30 10:18:54 -07001578}
1579
Hayden Gomes524159d2019-12-23 14:41:47 -08001580status_t AudioSystem::setSupportedSystemUsages(const std::vector<audio_usage_t>& systemUsages) {
Andy Hung264fa4c2024-02-21 15:52:12 -08001581 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Hayden Gomes524159d2019-12-23 14:41:47 -08001582 if (aps == nullptr) return PERMISSION_DENIED;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001583
Mikhail Naganovdbf03642021-08-25 18:15:32 -07001584 std::vector<AudioUsage> systemUsagesAidl = VALUE_OR_RETURN_STATUS(
1585 convertContainer<std::vector<AudioUsage>>(systemUsages,
1586 legacy2aidl_audio_usage_t_AudioUsage));
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001587 return statusTFromBinderStatus(aps->setSupportedSystemUsages(systemUsagesAidl));
Hayden Gomes524159d2019-12-23 14:41:47 -08001588}
1589
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001590status_t AudioSystem::setAllowedCapturePolicy(uid_t uid, audio_flags_mask_t capturePolicy) {
Andy Hung264fa4c2024-02-21 15:52:12 -08001591 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Kevin Rocardb99cc752019-03-21 20:52:24 -07001592 if (aps == nullptr) return PERMISSION_DENIED;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001593
1594 int32_t uidAidl = VALUE_OR_RETURN_STATUS(legacy2aidl_uid_t_int32_t(uid));
1595 int32_t capturePolicyAidl = VALUE_OR_RETURN_STATUS(
1596 legacy2aidl_audio_flags_mask_t_int32_t_mask(capturePolicy));
1597 return statusTFromBinderStatus(aps->setAllowedCapturePolicy(uidAidl, capturePolicyAidl));
Kevin Rocardb99cc752019-03-21 20:52:24 -07001598}
1599
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001600audio_offload_mode_t AudioSystem::getOffloadSupport(const audio_offload_info_t& info) {
Eric Laurent90fe31c2020-11-26 20:06:35 +01001601 ALOGV("%s", __func__);
Andy Hung264fa4c2024-02-21 15:52:12 -08001602 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Eric Laurent90fe31c2020-11-26 20:06:35 +01001603 if (aps == 0) return AUDIO_OFFLOAD_NOT_SUPPORTED;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001604
1605 auto result = [&]() -> ConversionResult<audio_offload_mode_t> {
Mikhail Naganovdbf03642021-08-25 18:15:32 -07001606 AudioOffloadInfo infoAidl = VALUE_OR_RETURN(
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001607 legacy2aidl_audio_offload_info_t_AudioOffloadInfo(info));
1608 media::AudioOffloadMode retAidl;
1609 RETURN_IF_ERROR(
1610 statusTFromBinderStatus(aps->getOffloadSupport(infoAidl, &retAidl)));
1611 return aidl2legacy_AudioOffloadMode_audio_offload_mode_t(retAidl);
1612 }();
1613
1614 return result.value_or(static_cast<audio_offload_mode_t>(0));
Richard Fitzgeraldad3af332013-03-25 16:54:37 +00001615}
1616
Eric Laurent203b1a12014-04-01 10:34:16 -07001617status_t AudioSystem::listAudioPorts(audio_port_role_t role,
1618 audio_port_type_t type,
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001619 unsigned int* num_ports,
1620 struct audio_port_v7* ports,
1621 unsigned int* generation) {
1622 if (num_ports == nullptr || (*num_ports != 0 && ports == nullptr) ||
1623 generation == nullptr) {
1624 return BAD_VALUE;
1625 }
1626
Andy Hung264fa4c2024-02-21 15:52:12 -08001627 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Eric Laurent203b1a12014-04-01 10:34:16 -07001628 if (aps == 0) return PERMISSION_DENIED;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001629
1630 media::AudioPortRole roleAidl = VALUE_OR_RETURN_STATUS(
1631 legacy2aidl_audio_port_role_t_AudioPortRole(role));
1632 media::AudioPortType typeAidl = VALUE_OR_RETURN_STATUS(
1633 legacy2aidl_audio_port_type_t_AudioPortType(type));
Mikhail Naganov0078ee52021-09-30 23:06:20 +00001634 Int numPortsAidl;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001635 numPortsAidl.value = VALUE_OR_RETURN_STATUS(convertIntegral<int32_t>(*num_ports));
Atneya Nair638a6e42022-12-18 16:45:15 -08001636 std::vector<media::AudioPortFw> portsAidl;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001637 int32_t generationAidl;
1638
1639 RETURN_STATUS_IF_ERROR(statusTFromBinderStatus(
1640 aps->listAudioPorts(roleAidl, typeAidl, &numPortsAidl, &portsAidl, &generationAidl)));
1641 *num_ports = VALUE_OR_RETURN_STATUS(convertIntegral<unsigned int>(numPortsAidl.value));
1642 *generation = VALUE_OR_RETURN_STATUS(convertIntegral<unsigned int>(generationAidl));
1643 RETURN_STATUS_IF_ERROR(convertRange(portsAidl.begin(), portsAidl.end(), ports,
Mikhail Naganov87227252023-01-13 17:38:10 -08001644 aidl2legacy_AudioPortFw_audio_port_v7));
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001645 return OK;
Eric Laurent203b1a12014-04-01 10:34:16 -07001646}
1647
Mikhail Naganov5edc5ed2023-03-23 14:52:15 -07001648status_t AudioSystem::listDeclaredDevicePorts(media::AudioPortRole role,
1649 std::vector<media::AudioPortFw>* result) {
1650 if (result == nullptr) return BAD_VALUE;
Andy Hung264fa4c2024-02-21 15:52:12 -08001651 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Mikhail Naganov5edc5ed2023-03-23 14:52:15 -07001652 if (aps == 0) return PERMISSION_DENIED;
1653 RETURN_STATUS_IF_ERROR(statusTFromBinderStatus(aps->listDeclaredDevicePorts(role, result)));
1654 return OK;
1655}
1656
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001657status_t AudioSystem::getAudioPort(struct audio_port_v7* port) {
1658 if (port == nullptr) {
1659 return BAD_VALUE;
1660 }
Andy Hung264fa4c2024-02-21 15:52:12 -08001661 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Eric Laurent203b1a12014-04-01 10:34:16 -07001662 if (aps == 0) return PERMISSION_DENIED;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001663
Atneya Nair638a6e42022-12-18 16:45:15 -08001664 media::AudioPortFw portAidl;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001665 RETURN_STATUS_IF_ERROR(
Mikhail Naganov17031562022-02-23 23:00:27 +00001666 statusTFromBinderStatus(aps->getAudioPort(port->id, &portAidl)));
Mikhail Naganov87227252023-01-13 17:38:10 -08001667 *port = VALUE_OR_RETURN_STATUS(aidl2legacy_AudioPortFw_audio_port_v7(portAidl));
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001668 return OK;
Eric Laurent203b1a12014-04-01 10:34:16 -07001669}
1670
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001671status_t AudioSystem::createAudioPatch(const struct audio_patch* patch,
1672 audio_patch_handle_t* handle) {
1673 if (patch == nullptr || handle == nullptr) {
1674 return BAD_VALUE;
1675 }
1676
Andy Hung264fa4c2024-02-21 15:52:12 -08001677 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Eric Laurent203b1a12014-04-01 10:34:16 -07001678 if (aps == 0) return PERMISSION_DENIED;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001679
Atneya Nair3afdbd12022-12-18 16:14:18 -08001680 media::AudioPatchFw patchAidl = VALUE_OR_RETURN_STATUS(
Mikhail Naganov87227252023-01-13 17:38:10 -08001681 legacy2aidl_audio_patch_AudioPatchFw(*patch));
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001682 int32_t handleAidl = VALUE_OR_RETURN_STATUS(legacy2aidl_audio_patch_handle_t_int32_t(*handle));
1683 RETURN_STATUS_IF_ERROR(
1684 statusTFromBinderStatus(aps->createAudioPatch(patchAidl, handleAidl, &handleAidl)));
1685 *handle = VALUE_OR_RETURN_STATUS(aidl2legacy_int32_t_audio_patch_handle_t(handleAidl));
1686 return OK;
Eric Laurent203b1a12014-04-01 10:34:16 -07001687}
1688
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001689status_t AudioSystem::releaseAudioPatch(audio_patch_handle_t handle) {
Andy Hung264fa4c2024-02-21 15:52:12 -08001690 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Eric Laurent203b1a12014-04-01 10:34:16 -07001691 if (aps == 0) return PERMISSION_DENIED;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001692
1693 int32_t handleAidl = VALUE_OR_RETURN_STATUS(legacy2aidl_audio_patch_handle_t_int32_t(handle));
1694 return statusTFromBinderStatus(aps->releaseAudioPatch(handleAidl));
Eric Laurent203b1a12014-04-01 10:34:16 -07001695}
1696
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001697status_t AudioSystem::listAudioPatches(unsigned int* num_patches,
1698 struct audio_patch* patches,
1699 unsigned int* generation) {
1700 if (num_patches == nullptr || (*num_patches != 0 && patches == nullptr) ||
1701 generation == nullptr) {
1702 return BAD_VALUE;
1703 }
1704
Andy Hung264fa4c2024-02-21 15:52:12 -08001705 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Eric Laurent203b1a12014-04-01 10:34:16 -07001706 if (aps == 0) return PERMISSION_DENIED;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001707
1708
Mikhail Naganov0078ee52021-09-30 23:06:20 +00001709 Int numPatchesAidl;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001710 numPatchesAidl.value = VALUE_OR_RETURN_STATUS(convertIntegral<int32_t>(*num_patches));
Atneya Nair3afdbd12022-12-18 16:14:18 -08001711 std::vector<media::AudioPatchFw> patchesAidl;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001712 int32_t generationAidl;
1713
1714 RETURN_STATUS_IF_ERROR(statusTFromBinderStatus(
1715 aps->listAudioPatches(&numPatchesAidl, &patchesAidl, &generationAidl)));
1716 *num_patches = VALUE_OR_RETURN_STATUS(convertIntegral<unsigned int>(numPatchesAidl.value));
1717 *generation = VALUE_OR_RETURN_STATUS(convertIntegral<unsigned int>(generationAidl));
1718 RETURN_STATUS_IF_ERROR(convertRange(patchesAidl.begin(), patchesAidl.end(), patches,
Mikhail Naganov87227252023-01-13 17:38:10 -08001719 aidl2legacy_AudioPatchFw_audio_patch));
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001720 return OK;
Eric Laurent203b1a12014-04-01 10:34:16 -07001721}
1722
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001723status_t AudioSystem::setAudioPortConfig(const struct audio_port_config* config) {
1724 if (config == nullptr) {
1725 return BAD_VALUE;
1726 }
1727
Andy Hung264fa4c2024-02-21 15:52:12 -08001728 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Eric Laurent203b1a12014-04-01 10:34:16 -07001729 if (aps == 0) return PERMISSION_DENIED;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001730
Atneya Nair7a9594f2022-12-18 17:26:26 -08001731 media::AudioPortConfigFw configAidl = VALUE_OR_RETURN_STATUS(
Mikhail Naganov87227252023-01-13 17:38:10 -08001732 legacy2aidl_audio_port_config_AudioPortConfigFw(*config));
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001733 return statusTFromBinderStatus(aps->setAudioPortConfig(configAidl));
Eric Laurent203b1a12014-04-01 10:34:16 -07001734}
1735
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001736status_t AudioSystem::addAudioPortCallback(const sp<AudioPortCallback>& callback) {
Andy Hung264fa4c2024-02-21 15:52:12 -08001737 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Eric Laurentb28753e2015-04-01 13:06:28 -07001738 if (aps == 0) return PERMISSION_DENIED;
Andy Hung88e71732024-02-21 16:44:27 -08001739 const auto apc = gAudioPolicyServiceHandler.getClient();
1740 if (apc == nullptr) return NO_INIT;
Eric Laurentb28753e2015-04-01 13:06:28 -07001741
Andy Hung88e71732024-02-21 16:44:27 -08001742 std::lock_guard _l(gApsCallbackMutex);
1743 const int ret = apc->addAudioPortCallback(callback);
Eric Laurente8726fe2015-06-26 09:39:24 -07001744 if (ret == 1) {
1745 aps->setAudioPortCallbacksEnabled(true);
1746 }
1747 return (ret < 0) ? INVALID_OPERATION : NO_ERROR;
Eric Laurentb52c1522014-05-20 11:27:36 -07001748}
1749
Jean-Michel Trivif613d422015-04-23 18:41:29 -07001750/*static*/
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001751status_t AudioSystem::removeAudioPortCallback(const sp<AudioPortCallback>& callback) {
Andy Hung264fa4c2024-02-21 15:52:12 -08001752 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Eric Laurentb28753e2015-04-01 13:06:28 -07001753 if (aps == 0) return PERMISSION_DENIED;
Andy Hung88e71732024-02-21 16:44:27 -08001754 const auto apc = gAudioPolicyServiceHandler.getClient();
1755 if (apc == nullptr) return NO_INIT;
Eric Laurentb28753e2015-04-01 13:06:28 -07001756
Andy Hung88e71732024-02-21 16:44:27 -08001757 std::lock_guard _l(gApsCallbackMutex);
1758 const int ret = apc->removeAudioPortCallback(callback);
Eric Laurente8726fe2015-06-26 09:39:24 -07001759 if (ret == 0) {
1760 aps->setAudioPortCallbacksEnabled(false);
1761 }
1762 return (ret < 0) ? INVALID_OPERATION : NO_ERROR;
Eric Laurent296fb132015-05-01 11:38:42 -07001763}
1764
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001765status_t AudioSystem::addAudioVolumeGroupCallback(const sp<AudioVolumeGroupCallback>& callback) {
Andy Hung264fa4c2024-02-21 15:52:12 -08001766 const sp<IAudioPolicyService> aps = get_audio_policy_service();
François Gaffiecfe17322018-11-07 13:41:29 +01001767 if (aps == 0) return PERMISSION_DENIED;
Andy Hung88e71732024-02-21 16:44:27 -08001768 const auto apc = gAudioPolicyServiceHandler.getClient();
1769 if (apc == nullptr) return NO_INIT;
François Gaffiecfe17322018-11-07 13:41:29 +01001770
Andy Hung88e71732024-02-21 16:44:27 -08001771 std::lock_guard _l(gApsCallbackMutex);
1772 const int ret = apc->addAudioVolumeGroupCallback(callback);
François Gaffiecfe17322018-11-07 13:41:29 +01001773 if (ret == 1) {
1774 aps->setAudioVolumeGroupCallbacksEnabled(true);
1775 }
1776 return (ret < 0) ? INVALID_OPERATION : NO_ERROR;
1777}
1778
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001779status_t AudioSystem::removeAudioVolumeGroupCallback(const sp<AudioVolumeGroupCallback>& callback) {
Andy Hung264fa4c2024-02-21 15:52:12 -08001780 const sp<IAudioPolicyService> aps = get_audio_policy_service();
François Gaffiecfe17322018-11-07 13:41:29 +01001781 if (aps == 0) return PERMISSION_DENIED;
Andy Hung88e71732024-02-21 16:44:27 -08001782 const auto apc = gAudioPolicyServiceHandler.getClient();
1783 if (apc == nullptr) return NO_INIT;
François Gaffiecfe17322018-11-07 13:41:29 +01001784
Andy Hung88e71732024-02-21 16:44:27 -08001785 std::lock_guard _l(gApsCallbackMutex);
1786 const int ret = apc->removeAudioVolumeGroupCallback(callback);
François Gaffiecfe17322018-11-07 13:41:29 +01001787 if (ret == 0) {
1788 aps->setAudioVolumeGroupCallbacksEnabled(false);
1789 }
1790 return (ret < 0) ? INVALID_OPERATION : NO_ERROR;
1791}
1792
Eric Laurent296fb132015-05-01 11:38:42 -07001793status_t AudioSystem::addAudioDeviceCallback(
Eric Laurent09f1ed22019-04-24 17:45:17 -07001794 const wp<AudioDeviceCallback>& callback, audio_io_handle_t audioIo,
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001795 audio_port_handle_t portId) {
Eric Laurent296fb132015-05-01 11:38:42 -07001796 const sp<AudioFlingerClient> afc = getAudioFlingerClient();
1797 if (afc == 0) {
1798 return NO_INIT;
1799 }
Eric Laurent09f1ed22019-04-24 17:45:17 -07001800 status_t status = afc->addAudioDeviceCallback(callback, audioIo, portId);
Eric Laurent7c1ec5f2015-07-09 14:52:47 -07001801 if (status == NO_ERROR) {
Andy Hung264fa4c2024-02-21 15:52:12 -08001802 const sp<IAudioFlinger> af = get_audio_flinger();
Eric Laurent7c1ec5f2015-07-09 14:52:47 -07001803 if (af != 0) {
1804 af->registerClient(afc);
1805 }
1806 }
1807 return status;
Eric Laurent296fb132015-05-01 11:38:42 -07001808}
1809
1810status_t AudioSystem::removeAudioDeviceCallback(
Eric Laurent09f1ed22019-04-24 17:45:17 -07001811 const wp<AudioDeviceCallback>& callback, audio_io_handle_t audioIo,
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001812 audio_port_handle_t portId) {
Eric Laurent296fb132015-05-01 11:38:42 -07001813 const sp<AudioFlingerClient> afc = getAudioFlingerClient();
1814 if (afc == 0) {
1815 return NO_INIT;
1816 }
Eric Laurent09f1ed22019-04-24 17:45:17 -07001817 return afc->removeAudioDeviceCallback(callback, audioIo, portId);
Eric Laurent296fb132015-05-01 11:38:42 -07001818}
1819
Eric Laurent076e7c72022-05-03 18:12:28 +02001820status_t AudioSystem::addSupportedLatencyModesCallback(
1821 const sp<SupportedLatencyModesCallback>& callback) {
1822 const sp<AudioFlingerClient> afc = getAudioFlingerClient();
1823 if (afc == 0) {
1824 return NO_INIT;
1825 }
1826 return afc->addSupportedLatencyModesCallback(callback);
1827}
1828
1829status_t AudioSystem::removeSupportedLatencyModesCallback(
1830 const sp<SupportedLatencyModesCallback>& callback) {
1831 const sp<AudioFlingerClient> afc = getAudioFlingerClient();
1832 if (afc == 0) {
1833 return NO_INIT;
1834 }
1835 return afc->removeSupportedLatencyModesCallback(callback);
1836}
1837
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001838audio_port_handle_t AudioSystem::getDeviceIdForIo(audio_io_handle_t audioIo) {
Andy Hung264fa4c2024-02-21 15:52:12 -08001839 const sp<IAudioFlinger> af = get_audio_flinger();
Eric Laurent296fb132015-05-01 11:38:42 -07001840 if (af == 0) return PERMISSION_DENIED;
1841 const sp<AudioIoDescriptor> desc = getIoDescriptor(audioIo);
1842 if (desc == 0) {
1843 return AUDIO_PORT_HANDLE_NONE;
1844 }
1845 return desc->getDeviceId();
Eric Laurentb28753e2015-04-01 13:06:28 -07001846}
1847
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001848status_t AudioSystem::acquireSoundTriggerSession(audio_session_t* session,
1849 audio_io_handle_t* ioHandle,
1850 audio_devices_t* device) {
1851 if (session == nullptr || ioHandle == nullptr || device == nullptr) {
1852 return BAD_VALUE;
1853 }
Andy Hung264fa4c2024-02-21 15:52:12 -08001854 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Eric Laurentdf3dc7e2014-07-27 18:39:40 -07001855 if (aps == 0) return PERMISSION_DENIED;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001856
1857 media::SoundTriggerSession retAidl;
1858 RETURN_STATUS_IF_ERROR(
1859 statusTFromBinderStatus(aps->acquireSoundTriggerSession(&retAidl)));
1860 *session = VALUE_OR_RETURN_STATUS(aidl2legacy_int32_t_audio_session_t(retAidl.session));
1861 *ioHandle = VALUE_OR_RETURN_STATUS(aidl2legacy_int32_t_audio_io_handle_t(retAidl.ioHandle));
Mikhail Naganov21a32ec2021-07-08 14:40:12 -07001862 *device = VALUE_OR_RETURN_STATUS(
1863 aidl2legacy_AudioDeviceDescription_audio_devices_t(retAidl.device));
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001864 return OK;
Eric Laurentdf3dc7e2014-07-27 18:39:40 -07001865}
1866
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001867status_t AudioSystem::releaseSoundTriggerSession(audio_session_t session) {
Andy Hung264fa4c2024-02-21 15:52:12 -08001868 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Eric Laurentdf3dc7e2014-07-27 18:39:40 -07001869 if (aps == 0) return PERMISSION_DENIED;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001870
1871 int32_t sessionAidl = VALUE_OR_RETURN_STATUS(legacy2aidl_audio_session_t_int32_t(session));
1872 return statusTFromBinderStatus(aps->releaseSoundTriggerSession(sessionAidl));
Eric Laurentdf3dc7e2014-07-27 18:39:40 -07001873}
Eric Laurentbb6c9a02014-09-25 14:11:47 -07001874
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001875audio_mode_t AudioSystem::getPhoneState() {
Andy Hung264fa4c2024-02-21 15:52:12 -08001876 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Eric Laurentbb6c9a02014-09-25 14:11:47 -07001877 if (aps == 0) return AUDIO_MODE_INVALID;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001878
1879 auto result = [&]() -> ConversionResult<audio_mode_t> {
Mikhail Naganovddceecc2021-09-03 13:58:56 -07001880 media::audio::common::AudioMode retAidl;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001881 RETURN_IF_ERROR(statusTFromBinderStatus(aps->getPhoneState(&retAidl)));
1882 return aidl2legacy_AudioMode_audio_mode_t(retAidl);
1883 }();
1884
1885 return result.value_or(AUDIO_MODE_INVALID);
Eric Laurentbb6c9a02014-09-25 14:11:47 -07001886}
1887
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001888status_t AudioSystem::registerPolicyMixes(const Vector<AudioMix>& mixes, bool registration) {
Andy Hung264fa4c2024-02-21 15:52:12 -08001889 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Eric Laurentbaac1832014-12-01 17:52:59 -08001890 if (aps == 0) return PERMISSION_DENIED;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001891
1892 size_t mixesSize = std::min(mixes.size(), size_t{MAX_MIXES_PER_POLICY});
1893 std::vector<media::AudioMix> mixesAidl;
1894 RETURN_STATUS_IF_ERROR(
1895 convertRange(mixes.begin(), mixes.begin() + mixesSize, std::back_inserter(mixesAidl),
1896 legacy2aidl_AudioMix));
1897 return statusTFromBinderStatus(aps->registerPolicyMixes(mixesAidl, registration));
Eric Laurentbaac1832014-12-01 17:52:59 -08001898}
Eric Laurentbb6c9a02014-09-25 14:11:47 -07001899
Marvin Raminbdefaf02023-11-01 09:10:32 +01001900status_t AudioSystem::getRegisteredPolicyMixes(std::vector<AudioMix>& mixes) {
1901 if (!audio_flags::audio_mix_test_api()) {
1902 return INVALID_OPERATION;
1903 }
1904
1905 const sp<IAudioPolicyService> aps = AudioSystem::get_audio_policy_service();
1906 if (aps == nullptr) return PERMISSION_DENIED;
1907
1908 std::vector<::android::media::AudioMix> aidlMixes;
1909 Status status = aps->getRegisteredPolicyMixes(&aidlMixes);
1910
1911 for (const auto& aidlMix : aidlMixes) {
1912 AudioMix mix = VALUE_OR_RETURN_STATUS(aidl2legacy_AudioMix(aidlMix));
1913 mixes.push_back(mix);
1914 }
1915
1916 return statusTFromBinderStatus(status);
1917}
1918
Jan Sebechlebsky0af8e872023-08-11 14:45:08 +02001919status_t AudioSystem::updatePolicyMixes(
1920 const std::vector<std::pair<AudioMix, std::vector<AudioMixMatchCriterion>>>&
1921 mixesWithUpdates) {
Andy Hung264fa4c2024-02-21 15:52:12 -08001922 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Jan Sebechlebsky0af8e872023-08-11 14:45:08 +02001923 if (aps == 0) return PERMISSION_DENIED;
1924
1925 std::vector<media::AudioMixUpdate> updatesAidl;
1926 updatesAidl.reserve(mixesWithUpdates.size());
1927
1928 for (const auto& update : mixesWithUpdates) {
1929 media::AudioMixUpdate updateAidl;
1930 updateAidl.audioMix = VALUE_OR_RETURN_STATUS(legacy2aidl_AudioMix(update.first));
1931 RETURN_STATUS_IF_ERROR(convertRange(update.second.begin(), update.second.end(),
1932 std::back_inserter(updateAidl.newCriteria),
1933 legacy2aidl_AudioMixMatchCriterion));
1934 updatesAidl.emplace_back(updateAidl);
1935 }
1936
1937 return statusTFromBinderStatus(aps->updatePolicyMixes(updatesAidl));
1938}
1939
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001940status_t AudioSystem::setUidDeviceAffinities(uid_t uid, const AudioDeviceTypeAddrVector& devices) {
Andy Hung264fa4c2024-02-21 15:52:12 -08001941 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Jean-Michel Trivibda70da2018-12-19 07:30:15 -08001942 if (aps == 0) return PERMISSION_DENIED;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001943
1944 int32_t uidAidl = VALUE_OR_RETURN_STATUS(legacy2aidl_uid_t_int32_t(uid));
Mikhail Naganovf4a75362021-09-16 00:02:54 +00001945 std::vector<AudioDevice> devicesAidl = VALUE_OR_RETURN_STATUS(
1946 convertContainer<std::vector<AudioDevice>>(devices,
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001947 legacy2aidl_AudioDeviceTypeAddress));
1948 return statusTFromBinderStatus(aps->setUidDeviceAffinities(uidAidl, devicesAidl));
Jean-Michel Trivibda70da2018-12-19 07:30:15 -08001949}
1950
1951status_t AudioSystem::removeUidDeviceAffinities(uid_t uid) {
Andy Hung264fa4c2024-02-21 15:52:12 -08001952 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Jean-Michel Trivibda70da2018-12-19 07:30:15 -08001953 if (aps == 0) return PERMISSION_DENIED;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001954
1955 int32_t uidAidl = VALUE_OR_RETURN_STATUS(legacy2aidl_uid_t_int32_t(uid));
1956 return statusTFromBinderStatus(aps->removeUidDeviceAffinities(uidAidl));
Jean-Michel Trivibda70da2018-12-19 07:30:15 -08001957}
1958
Oscar Azucena90e77632019-11-27 17:12:28 -08001959status_t AudioSystem::setUserIdDeviceAffinities(int userId,
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001960 const AudioDeviceTypeAddrVector& devices) {
Andy Hung264fa4c2024-02-21 15:52:12 -08001961 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Oscar Azucena90e77632019-11-27 17:12:28 -08001962 if (aps == 0) return PERMISSION_DENIED;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001963
1964 int32_t userIdAidl = VALUE_OR_RETURN_STATUS(convertReinterpret<int32_t>(userId));
Mikhail Naganovf4a75362021-09-16 00:02:54 +00001965 std::vector<AudioDevice> devicesAidl = VALUE_OR_RETURN_STATUS(
1966 convertContainer<std::vector<AudioDevice>>(devices,
1967 legacy2aidl_AudioDeviceTypeAddress));
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001968 return statusTFromBinderStatus(
1969 aps->setUserIdDeviceAffinities(userIdAidl, devicesAidl));
Oscar Azucena90e77632019-11-27 17:12:28 -08001970}
1971
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001972status_t AudioSystem::removeUserIdDeviceAffinities(int userId) {
Andy Hung264fa4c2024-02-21 15:52:12 -08001973 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Oscar Azucena90e77632019-11-27 17:12:28 -08001974 if (aps == 0) return PERMISSION_DENIED;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001975 int32_t userIdAidl = VALUE_OR_RETURN_STATUS(convertReinterpret<int32_t>(userId));
1976 return statusTFromBinderStatus(aps->removeUserIdDeviceAffinities(userIdAidl));
Oscar Azucena90e77632019-11-27 17:12:28 -08001977}
1978
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001979status_t AudioSystem::startAudioSource(const struct audio_port_config* source,
1980 const audio_attributes_t* attributes,
1981 audio_port_handle_t* portId) {
1982 if (source == nullptr || attributes == nullptr || portId == nullptr) {
1983 return BAD_VALUE;
1984 }
Andy Hung264fa4c2024-02-21 15:52:12 -08001985 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Eric Laurent554a2772015-04-10 11:29:24 -07001986 if (aps == 0) return PERMISSION_DENIED;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001987
Atneya Nair7a9594f2022-12-18 17:26:26 -08001988 media::AudioPortConfigFw sourceAidl = VALUE_OR_RETURN_STATUS(
Mikhail Naganov87227252023-01-13 17:38:10 -08001989 legacy2aidl_audio_port_config_AudioPortConfigFw(*source));
Mikhail Naganov1c400902023-05-17 11:48:43 -07001990 media::audio::common::AudioAttributes attributesAidl = VALUE_OR_RETURN_STATUS(
1991 legacy2aidl_audio_attributes_t_AudioAttributes(*attributes));
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001992 int32_t portIdAidl;
1993 RETURN_STATUS_IF_ERROR(statusTFromBinderStatus(
1994 aps->startAudioSource(sourceAidl, attributesAidl, &portIdAidl)));
1995 *portId = VALUE_OR_RETURN_STATUS(aidl2legacy_int32_t_audio_port_handle_t(portIdAidl));
1996 return OK;
Eric Laurent554a2772015-04-10 11:29:24 -07001997}
1998
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001999status_t AudioSystem::stopAudioSource(audio_port_handle_t portId) {
Andy Hung264fa4c2024-02-21 15:52:12 -08002000 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Eric Laurent554a2772015-04-10 11:29:24 -07002001 if (aps == 0) return PERMISSION_DENIED;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002002
2003 int32_t portIdAidl = VALUE_OR_RETURN_STATUS(legacy2aidl_audio_port_handle_t_int32_t(portId));
2004 return statusTFromBinderStatus(aps->stopAudioSource(portIdAidl));
Eric Laurent554a2772015-04-10 11:29:24 -07002005}
2006
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002007status_t AudioSystem::setMasterMono(bool mono) {
Andy Hung264fa4c2024-02-21 15:52:12 -08002008 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Andy Hung2ddee192015-12-18 17:34:44 -08002009 if (aps == 0) return PERMISSION_DENIED;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002010 return statusTFromBinderStatus(aps->setMasterMono(mono));
Andy Hung2ddee192015-12-18 17:34:44 -08002011}
2012
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002013status_t AudioSystem::getMasterMono(bool* mono) {
2014 if (mono == nullptr) {
2015 return BAD_VALUE;
2016 }
Andy Hung264fa4c2024-02-21 15:52:12 -08002017 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Andy Hung2ddee192015-12-18 17:34:44 -08002018 if (aps == 0) return PERMISSION_DENIED;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002019 return statusTFromBinderStatus(aps->getMasterMono(mono));
Andy Hung2ddee192015-12-18 17:34:44 -08002020}
2021
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002022status_t AudioSystem::setMasterBalance(float balance) {
Andy Hung264fa4c2024-02-21 15:52:12 -08002023 const sp<IAudioFlinger> af = get_audio_flinger();
Richard Folke Tullberg3fae0372017-01-13 09:04:25 +01002024 if (af == 0) return PERMISSION_DENIED;
2025 return af->setMasterBalance(balance);
2026}
2027
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002028status_t AudioSystem::getMasterBalance(float* balance) {
Andy Hung264fa4c2024-02-21 15:52:12 -08002029 const sp<IAudioFlinger> af = get_audio_flinger();
Richard Folke Tullberg3fae0372017-01-13 09:04:25 +01002030 if (af == 0) return PERMISSION_DENIED;
2031 return af->getMasterBalance(balance);
2032}
2033
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002034float
2035AudioSystem::getStreamVolumeDB(audio_stream_type_t stream, int index, audio_devices_t device) {
Andy Hung264fa4c2024-02-21 15:52:12 -08002036 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Eric Laurentac9cef52017-06-09 15:46:26 -07002037 if (aps == 0) return NAN;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002038
2039 auto result = [&]() -> ConversionResult<float> {
Mikhail Naganovdbf03642021-08-25 18:15:32 -07002040 AudioStreamType streamAidl = VALUE_OR_RETURN(
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002041 legacy2aidl_audio_stream_type_t_AudioStreamType(stream));
2042 int32_t indexAidl = VALUE_OR_RETURN(convertIntegral<int32_t>(index));
Mikhail Naganovf4a75362021-09-16 00:02:54 +00002043 AudioDeviceDescription deviceAidl = VALUE_OR_RETURN(
Mikhail Naganov21a32ec2021-07-08 14:40:12 -07002044 legacy2aidl_audio_devices_t_AudioDeviceDescription(device));
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002045 float retAidl;
2046 RETURN_IF_ERROR(statusTFromBinderStatus(
2047 aps->getStreamVolumeDB(streamAidl, indexAidl, deviceAidl, &retAidl)));
2048 return retAidl;
2049 }();
2050 return result.value_or(NAN);
Eric Laurentac9cef52017-06-09 15:46:26 -07002051}
2052
Mikhail Naganovd5d9de72023-02-13 11:45:03 -08002053status_t AudioSystem::getMicrophones(std::vector<media::MicrophoneInfoFw>* microphones) {
Andy Hung264fa4c2024-02-21 15:52:12 -08002054 const sp<IAudioFlinger> af = get_audio_flinger();
jiabin46a76fa2018-01-05 10:18:21 -08002055 if (af == 0) return PERMISSION_DENIED;
2056 return af->getMicrophones(microphones);
2057}
2058
Eric Laurent42896a02019-09-27 15:40:33 -07002059status_t AudioSystem::setAudioHalPids(const std::vector<pid_t>& pids) {
Andy Hung264fa4c2024-02-21 15:52:12 -08002060 const sp<IAudioFlinger> af = get_audio_flinger();
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002061 if (af == nullptr) return PERMISSION_DENIED;
2062 return af->setAudioHalPids(pids);
Eric Laurent42896a02019-09-27 15:40:33 -07002063}
2064
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002065status_t AudioSystem::getSurroundFormats(unsigned int* numSurroundFormats,
2066 audio_format_t* surroundFormats,
Kriti Dang6537def2021-03-02 13:46:59 +01002067 bool* surroundFormatsEnabled) {
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002068 if (numSurroundFormats == nullptr || (*numSurroundFormats != 0 &&
2069 (surroundFormats == nullptr ||
2070 surroundFormatsEnabled == nullptr))) {
2071 return BAD_VALUE;
2072 }
2073
Andy Hung264fa4c2024-02-21 15:52:12 -08002074 const sp<IAudioPolicyService> aps = get_audio_policy_service();
jiabin81772902018-04-02 17:52:27 -07002075 if (aps == 0) return PERMISSION_DENIED;
Mikhail Naganov0078ee52021-09-30 23:06:20 +00002076 Int numSurroundFormatsAidl;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002077 numSurroundFormatsAidl.value =
2078 VALUE_OR_RETURN_STATUS(convertIntegral<int32_t>(*numSurroundFormats));
Mikhail Naganov57bd06f2021-08-10 16:41:54 -07002079 std::vector<AudioFormatDescription> surroundFormatsAidl;
Kriti Dang877b27e2021-02-02 12:10:40 +01002080 std::vector<bool> surroundFormatsEnabledAidl;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002081 RETURN_STATUS_IF_ERROR(statusTFromBinderStatus(
Kriti Dang6537def2021-03-02 13:46:59 +01002082 aps->getSurroundFormats(&numSurroundFormatsAidl, &surroundFormatsAidl,
2083 &surroundFormatsEnabledAidl)));
Kriti Dang877b27e2021-02-02 12:10:40 +01002084
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002085 *numSurroundFormats = VALUE_OR_RETURN_STATUS(
2086 convertIntegral<unsigned int>(numSurroundFormatsAidl.value));
2087 RETURN_STATUS_IF_ERROR(
2088 convertRange(surroundFormatsAidl.begin(), surroundFormatsAidl.end(), surroundFormats,
Mikhail Naganovb60bd1b2021-07-15 17:31:43 -07002089 aidl2legacy_AudioFormatDescription_audio_format_t));
Kriti Dang877b27e2021-02-02 12:10:40 +01002090 std::copy(surroundFormatsEnabledAidl.begin(), surroundFormatsEnabledAidl.end(),
2091 surroundFormatsEnabled);
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002092 return OK;
jiabin81772902018-04-02 17:52:27 -07002093}
2094
Kriti Dang6537def2021-03-02 13:46:59 +01002095status_t AudioSystem::getReportedSurroundFormats(unsigned int* numSurroundFormats,
2096 audio_format_t* surroundFormats) {
2097 if (numSurroundFormats == nullptr || (*numSurroundFormats != 0 && surroundFormats == nullptr)) {
2098 return BAD_VALUE;
2099 }
2100
Andy Hung264fa4c2024-02-21 15:52:12 -08002101 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Kriti Dang6537def2021-03-02 13:46:59 +01002102 if (aps == 0) return PERMISSION_DENIED;
Mikhail Naganov0078ee52021-09-30 23:06:20 +00002103 Int numSurroundFormatsAidl;
Kriti Dang6537def2021-03-02 13:46:59 +01002104 numSurroundFormatsAidl.value =
2105 VALUE_OR_RETURN_STATUS(convertIntegral<int32_t>(*numSurroundFormats));
Mikhail Naganov57bd06f2021-08-10 16:41:54 -07002106 std::vector<AudioFormatDescription> surroundFormatsAidl;
Kriti Dang6537def2021-03-02 13:46:59 +01002107 RETURN_STATUS_IF_ERROR(statusTFromBinderStatus(
2108 aps->getReportedSurroundFormats(&numSurroundFormatsAidl, &surroundFormatsAidl)));
2109
2110 *numSurroundFormats = VALUE_OR_RETURN_STATUS(
2111 convertIntegral<unsigned int>(numSurroundFormatsAidl.value));
2112 RETURN_STATUS_IF_ERROR(
2113 convertRange(surroundFormatsAidl.begin(), surroundFormatsAidl.end(), surroundFormats,
Mikhail Naganovb60bd1b2021-07-15 17:31:43 -07002114 aidl2legacy_AudioFormatDescription_audio_format_t));
Kriti Dang6537def2021-03-02 13:46:59 +01002115 return OK;
2116}
2117
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002118status_t AudioSystem::setSurroundFormatEnabled(audio_format_t audioFormat, bool enabled) {
Andy Hung264fa4c2024-02-21 15:52:12 -08002119 const sp<IAudioPolicyService> aps = get_audio_policy_service();
jiabin81772902018-04-02 17:52:27 -07002120 if (aps == 0) return PERMISSION_DENIED;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002121
Mikhail Naganov57bd06f2021-08-10 16:41:54 -07002122 AudioFormatDescription audioFormatAidl = VALUE_OR_RETURN_STATUS(
Mikhail Naganovb60bd1b2021-07-15 17:31:43 -07002123 legacy2aidl_audio_format_t_AudioFormatDescription(audioFormat));
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002124 return statusTFromBinderStatus(
2125 aps->setSurroundFormatEnabled(audioFormatAidl, enabled));
jiabin81772902018-04-02 17:52:27 -07002126}
2127
Oscar Azucena829d90d2022-01-28 17:17:56 -08002128status_t AudioSystem::setAssistantServicesUids(const std::vector<uid_t>& uids) {
Andy Hung264fa4c2024-02-21 15:52:12 -08002129 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Eric Laurentb78763e2018-10-17 10:08:02 -07002130 if (aps == 0) return PERMISSION_DENIED;
2131
Oscar Azucena829d90d2022-01-28 17:17:56 -08002132 std::vector<int32_t> uidsAidl = VALUE_OR_RETURN_STATUS(
2133 convertContainer<std::vector<int32_t>>(uids, legacy2aidl_uid_t_int32_t));
2134 return statusTFromBinderStatus(aps->setAssistantServicesUids(uidsAidl));
Ahaan Ugalef51ce002021-08-04 16:34:20 -07002135}
2136
Oscar Azucenac2cdda32022-01-31 19:10:39 -08002137status_t AudioSystem::setActiveAssistantServicesUids(const std::vector<uid_t>& activeUids) {
Andy Hung264fa4c2024-02-21 15:52:12 -08002138 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Oscar Azucenac2cdda32022-01-31 19:10:39 -08002139 if (aps == 0) return PERMISSION_DENIED;
2140
2141 std::vector<int32_t> activeUidsAidl = VALUE_OR_RETURN_STATUS(
2142 convertContainer<std::vector<int32_t>>(activeUids, legacy2aidl_uid_t_int32_t));
2143 return statusTFromBinderStatus(aps->setActiveAssistantServicesUids(activeUidsAidl));
2144}
2145
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002146status_t AudioSystem::setA11yServicesUids(const std::vector<uid_t>& uids) {
Andy Hung264fa4c2024-02-21 15:52:12 -08002147 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Eric Laurentb78763e2018-10-17 10:08:02 -07002148 if (aps == 0) return PERMISSION_DENIED;
2149
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002150 std::vector<int32_t> uidsAidl = VALUE_OR_RETURN_STATUS(
2151 convertContainer<std::vector<int32_t>>(uids, legacy2aidl_uid_t_int32_t));
2152 return statusTFromBinderStatus(aps->setA11yServicesUids(uidsAidl));
Eric Laurentb78763e2018-10-17 10:08:02 -07002153}
2154
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002155status_t AudioSystem::setCurrentImeUid(uid_t uid) {
Andy Hung264fa4c2024-02-21 15:52:12 -08002156 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Kohsuke Yatoha623a132020-03-24 20:10:26 -07002157 if (aps == 0) return PERMISSION_DENIED;
2158
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002159 int32_t uidAidl = VALUE_OR_RETURN_STATUS(legacy2aidl_uid_t_int32_t(uid));
2160 return statusTFromBinderStatus(aps->setCurrentImeUid(uidAidl));
Kohsuke Yatoha623a132020-03-24 20:10:26 -07002161}
2162
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002163bool AudioSystem::isHapticPlaybackSupported() {
Andy Hung264fa4c2024-02-21 15:52:12 -08002164 const sp<IAudioPolicyService> aps = get_audio_policy_service();
jiabin6012f912018-11-02 17:06:30 -07002165 if (aps == 0) return false;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002166
2167 auto result = [&]() -> ConversionResult<bool> {
2168 bool retVal;
2169 RETURN_IF_ERROR(
2170 statusTFromBinderStatus(aps->isHapticPlaybackSupported(&retVal)));
2171 return retVal;
2172 }();
2173 return result.value_or(false);
jiabin6012f912018-11-02 17:06:30 -07002174}
2175
Carter Hsu325a8eb2022-01-19 19:56:51 +08002176bool AudioSystem::isUltrasoundSupported() {
Andy Hung264fa4c2024-02-21 15:52:12 -08002177 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Carter Hsu325a8eb2022-01-19 19:56:51 +08002178 if (aps == 0) return false;
2179
2180 auto result = [&]() -> ConversionResult<bool> {
2181 bool retVal;
2182 RETURN_IF_ERROR(
2183 statusTFromBinderStatus(aps->isUltrasoundSupported(&retVal)));
2184 return retVal;
2185 }();
2186 return result.value_or(false);
2187}
2188
Pattydd807582021-11-04 21:01:03 +08002189status_t AudioSystem::getHwOffloadFormatsSupportedForBluetoothMedia(
2190 audio_devices_t device, std::vector<audio_format_t>* formats) {
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002191 if (formats == nullptr) {
2192 return BAD_VALUE;
2193 }
2194
Andy Hung264fa4c2024-02-21 15:52:12 -08002195 const sp<IAudioPolicyService> aps = get_audio_policy_service();
François Gaffied0ba9ed2018-11-05 11:50:42 +01002196 if (aps == 0) return PERMISSION_DENIED;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002197
Mikhail Naganov57bd06f2021-08-10 16:41:54 -07002198 std::vector<AudioFormatDescription> formatsAidl;
Pattydd807582021-11-04 21:01:03 +08002199 AudioDeviceDescription deviceAidl = VALUE_OR_RETURN_STATUS(
2200 legacy2aidl_audio_devices_t_AudioDeviceDescription(device));
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002201 RETURN_STATUS_IF_ERROR(statusTFromBinderStatus(
Pattydd807582021-11-04 21:01:03 +08002202 aps->getHwOffloadFormatsSupportedForBluetoothMedia(deviceAidl, &formatsAidl)));
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002203 *formats = VALUE_OR_RETURN_STATUS(
Mikhail Naganovb60bd1b2021-07-15 17:31:43 -07002204 convertContainer<std::vector<audio_format_t>>(
2205 formatsAidl,
2206 aidl2legacy_AudioFormatDescription_audio_format_t));
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002207 return OK;
François Gaffied0ba9ed2018-11-05 11:50:42 +01002208}
2209
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002210status_t AudioSystem::listAudioProductStrategies(AudioProductStrategyVector& strategies) {
Andy Hung264fa4c2024-02-21 15:52:12 -08002211 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Arun Mirpuri11029ad2018-12-19 20:45:19 -08002212 if (aps == 0) return PERMISSION_DENIED;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002213
2214 std::vector<media::AudioProductStrategy> strategiesAidl;
2215 RETURN_STATUS_IF_ERROR(statusTFromBinderStatus(
2216 aps->listAudioProductStrategies(&strategiesAidl)));
2217 strategies = VALUE_OR_RETURN_STATUS(
2218 convertContainer<AudioProductStrategyVector>(strategiesAidl,
2219 aidl2legacy_AudioProductStrategy));
2220 return OK;
François Gaffied0ba9ed2018-11-05 11:50:42 +01002221}
2222
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002223audio_attributes_t AudioSystem::streamTypeToAttributes(audio_stream_type_t stream) {
François Gaffied0ba9ed2018-11-05 11:50:42 +01002224 AudioProductStrategyVector strategies;
2225 listAudioProductStrategies(strategies);
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002226 for (const auto& strategy : strategies) {
François Gaffie1e2b56f2022-04-01 14:34:29 +02002227 auto attrVect = strategy.getVolumeGroupAttributes();
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002228 auto iter = std::find_if(begin(attrVect), end(attrVect), [&stream](const auto& attributes) {
2229 return attributes.getStreamType() == stream;
2230 });
François Gaffied0ba9ed2018-11-05 11:50:42 +01002231 if (iter != end(attrVect)) {
2232 return iter->getAttributes();
2233 }
2234 }
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002235 ALOGE("invalid stream type %s when converting to attributes", toString(stream).c_str());
François Gaffied0ba9ed2018-11-05 11:50:42 +01002236 return AUDIO_ATTRIBUTES_INITIALIZER;
2237}
2238
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002239audio_stream_type_t AudioSystem::attributesToStreamType(const audio_attributes_t& attr) {
François Gaffie4b2018b2018-11-07 11:18:59 +01002240 product_strategy_t psId;
François Gaffie1e2b56f2022-04-01 14:34:29 +02002241 status_t ret = AudioSystem::getProductStrategyFromAudioAttributes(attr, psId);
François Gaffie4b2018b2018-11-07 11:18:59 +01002242 if (ret != NO_ERROR) {
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002243 ALOGE("no strategy found for attributes %s", toString(attr).c_str());
François Gaffie4b2018b2018-11-07 11:18:59 +01002244 return AUDIO_STREAM_MUSIC;
2245 }
François Gaffied0ba9ed2018-11-05 11:50:42 +01002246 AudioProductStrategyVector strategies;
2247 listAudioProductStrategies(strategies);
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002248 for (const auto& strategy : strategies) {
François Gaffie4b2018b2018-11-07 11:18:59 +01002249 if (strategy.getId() == psId) {
François Gaffie1e2b56f2022-04-01 14:34:29 +02002250 auto attrVect = strategy.getVolumeGroupAttributes();
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002251 auto iter = std::find_if(begin(attrVect), end(attrVect), [&attr](const auto& refAttr) {
François Gaffie8d7fd5a2023-01-17 17:28:09 +01002252 return refAttr.matchesScore(attr) > 0;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002253 });
François Gaffied0ba9ed2018-11-05 11:50:42 +01002254 if (iter != end(attrVect)) {
2255 return iter->getStreamType();
2256 }
2257 }
2258 }
Jean-Michel Trivied678652019-12-19 13:39:30 -08002259 switch (attr.usage) {
2260 case AUDIO_USAGE_VIRTUAL_SOURCE:
2261 // virtual source is not expected to have an associated product strategy
2262 break;
2263 default:
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002264 ALOGE("invalid attributes %s when converting to stream", toString(attr).c_str());
Jean-Michel Trivied678652019-12-19 13:39:30 -08002265 break;
2266 }
François Gaffied0ba9ed2018-11-05 11:50:42 +01002267 return AUDIO_STREAM_MUSIC;
2268}
2269
François Gaffie1e2b56f2022-04-01 14:34:29 +02002270status_t AudioSystem::getProductStrategyFromAudioAttributes(const audio_attributes_t& aa,
Francois Gaffie11b65922020-09-24 16:59:08 +02002271 product_strategy_t& productStrategy,
2272 bool fallbackOnDefault) {
Andy Hung264fa4c2024-02-21 15:52:12 -08002273 const sp<IAudioPolicyService> aps = get_audio_policy_service();
François Gaffie4b2018b2018-11-07 11:18:59 +01002274 if (aps == 0) return PERMISSION_DENIED;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002275
Mikhail Naganov1c400902023-05-17 11:48:43 -07002276 media::audio::common::AudioAttributes aaAidl = VALUE_OR_RETURN_STATUS(
2277 legacy2aidl_audio_attributes_t_AudioAttributes(aa));
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002278 int32_t productStrategyAidl;
2279
2280 RETURN_STATUS_IF_ERROR(statusTFromBinderStatus(
Francois Gaffie11b65922020-09-24 16:59:08 +02002281 aps->getProductStrategyFromAudioAttributes(aaAidl, fallbackOnDefault,
2282 &productStrategyAidl)));
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002283 productStrategy = VALUE_OR_RETURN_STATUS(
2284 aidl2legacy_int32_t_product_strategy_t(productStrategyAidl));
2285 return OK;
François Gaffie4b2018b2018-11-07 11:18:59 +01002286}
2287
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002288status_t AudioSystem::listAudioVolumeGroups(AudioVolumeGroupVector& groups) {
Andy Hung264fa4c2024-02-21 15:52:12 -08002289 const sp<IAudioPolicyService> aps = get_audio_policy_service();
François Gaffie4b2018b2018-11-07 11:18:59 +01002290 if (aps == 0) return PERMISSION_DENIED;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002291
2292 std::vector<media::AudioVolumeGroup> groupsAidl;
2293 RETURN_STATUS_IF_ERROR(
2294 statusTFromBinderStatus(aps->listAudioVolumeGroups(&groupsAidl)));
2295 groups = VALUE_OR_RETURN_STATUS(
2296 convertContainer<AudioVolumeGroupVector>(groupsAidl, aidl2legacy_AudioVolumeGroup));
2297 return OK;
François Gaffie4b2018b2018-11-07 11:18:59 +01002298}
2299
François Gaffie1e2b56f2022-04-01 14:34:29 +02002300status_t AudioSystem::getVolumeGroupFromAudioAttributes(const audio_attributes_t &aa,
Francois Gaffie11b65922020-09-24 16:59:08 +02002301 volume_group_t& volumeGroup,
2302 bool fallbackOnDefault) {
Andy Hung264fa4c2024-02-21 15:52:12 -08002303 const sp<IAudioPolicyService> aps = get_audio_policy_service();
François Gaffie4b2018b2018-11-07 11:18:59 +01002304 if (aps == 0) return PERMISSION_DENIED;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002305
Mikhail Naganov1c400902023-05-17 11:48:43 -07002306 media::audio::common::AudioAttributes aaAidl = VALUE_OR_RETURN_STATUS(
2307 legacy2aidl_audio_attributes_t_AudioAttributes(aa));
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002308 int32_t volumeGroupAidl;
2309 RETURN_STATUS_IF_ERROR(statusTFromBinderStatus(
Francois Gaffie11b65922020-09-24 16:59:08 +02002310 aps->getVolumeGroupFromAudioAttributes(aaAidl, fallbackOnDefault, &volumeGroupAidl)));
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002311 volumeGroup = VALUE_OR_RETURN_STATUS(aidl2legacy_int32_t_volume_group_t(volumeGroupAidl));
2312 return OK;
Arun Mirpuri11029ad2018-12-19 20:45:19 -08002313}
Eric Laurentb78763e2018-10-17 10:08:02 -07002314
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002315status_t AudioSystem::setRttEnabled(bool enabled) {
Andy Hung264fa4c2024-02-21 15:52:12 -08002316 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Eric Laurent6ede98f2019-06-11 14:50:30 -07002317 if (aps == 0) return PERMISSION_DENIED;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002318 return statusTFromBinderStatus(aps->setRttEnabled(enabled));
Eric Laurent6ede98f2019-06-11 14:50:30 -07002319}
2320
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002321bool AudioSystem::isCallScreenModeSupported() {
Andy Hung264fa4c2024-02-21 15:52:12 -08002322 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Eric Laurent8340e672019-11-06 11:01:08 -08002323 if (aps == 0) return false;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002324
2325 auto result = [&]() -> ConversionResult<bool> {
2326 bool retAidl;
2327 RETURN_IF_ERROR(
2328 statusTFromBinderStatus(aps->isCallScreenModeSupported(&retAidl)));
2329 return retAidl;
2330 }();
2331 return result.value_or(false);
Eric Laurent8340e672019-11-06 11:01:08 -08002332}
2333
jiabin0a488932020-08-07 17:32:40 -07002334status_t AudioSystem::setDevicesRoleForStrategy(product_strategy_t strategy,
2335 device_role_t role,
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002336 const AudioDeviceTypeAddrVector& devices) {
Andy Hung264fa4c2024-02-21 15:52:12 -08002337 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Jean-Michel Trivi30857152019-11-01 11:04:15 -07002338 if (aps == 0) {
2339 return PERMISSION_DENIED;
2340 }
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002341
2342 int32_t strategyAidl = VALUE_OR_RETURN_STATUS(legacy2aidl_product_strategy_t_int32_t(strategy));
2343 media::DeviceRole roleAidl = VALUE_OR_RETURN_STATUS(legacy2aidl_device_role_t_DeviceRole(role));
Mikhail Naganovf4a75362021-09-16 00:02:54 +00002344 std::vector<AudioDevice> devicesAidl = VALUE_OR_RETURN_STATUS(
2345 convertContainer<std::vector<AudioDevice>>(devices,
2346 legacy2aidl_AudioDeviceTypeAddress));
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002347 return statusTFromBinderStatus(
2348 aps->setDevicesRoleForStrategy(strategyAidl, roleAidl, devicesAidl));
Jean-Michel Trivi30857152019-11-01 11:04:15 -07002349}
2350
Paul Wang5d7cdb52022-11-22 09:45:06 +00002351status_t AudioSystem::removeDevicesRoleForStrategy(product_strategy_t strategy,
2352 device_role_t role,
2353 const AudioDeviceTypeAddrVector& devices) {
Andy Hung264fa4c2024-02-21 15:52:12 -08002354 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Paul Wang5d7cdb52022-11-22 09:45:06 +00002355 if (aps == 0) {
2356 return PERMISSION_DENIED;
2357 }
2358
2359 int32_t strategyAidl = VALUE_OR_RETURN_STATUS(legacy2aidl_product_strategy_t_int32_t(strategy));
2360 media::DeviceRole roleAidl = VALUE_OR_RETURN_STATUS(legacy2aidl_device_role_t_DeviceRole(role));
2361 std::vector<AudioDevice> devicesAidl = VALUE_OR_RETURN_STATUS(
2362 convertContainer<std::vector<AudioDevice>>(devices,
2363 legacy2aidl_AudioDeviceTypeAddress));
2364 return statusTFromBinderStatus(
2365 aps->removeDevicesRoleForStrategy(strategyAidl, roleAidl, devicesAidl));
2366}
2367
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002368status_t
Paul Wang5d7cdb52022-11-22 09:45:06 +00002369AudioSystem::clearDevicesRoleForStrategy(product_strategy_t strategy, device_role_t role) {
Andy Hung264fa4c2024-02-21 15:52:12 -08002370 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Jean-Michel Trivi30857152019-11-01 11:04:15 -07002371 if (aps == 0) {
2372 return PERMISSION_DENIED;
2373 }
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002374 int32_t strategyAidl = VALUE_OR_RETURN_STATUS(legacy2aidl_product_strategy_t_int32_t(strategy));
2375 media::DeviceRole roleAidl = VALUE_OR_RETURN_STATUS(legacy2aidl_device_role_t_DeviceRole(role));
2376 return statusTFromBinderStatus(
Paul Wang5d7cdb52022-11-22 09:45:06 +00002377 aps->clearDevicesRoleForStrategy(strategyAidl, roleAidl));
Jean-Michel Trivi30857152019-11-01 11:04:15 -07002378}
2379
jiabin0a488932020-08-07 17:32:40 -07002380status_t AudioSystem::getDevicesForRoleAndStrategy(product_strategy_t strategy,
2381 device_role_t role,
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002382 AudioDeviceTypeAddrVector& devices) {
Andy Hung264fa4c2024-02-21 15:52:12 -08002383 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Jean-Michel Trivi30857152019-11-01 11:04:15 -07002384 if (aps == 0) {
2385 return PERMISSION_DENIED;
2386 }
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002387 int32_t strategyAidl = VALUE_OR_RETURN_STATUS(legacy2aidl_product_strategy_t_int32_t(strategy));
2388 media::DeviceRole roleAidl = VALUE_OR_RETURN_STATUS(legacy2aidl_device_role_t_DeviceRole(role));
Mikhail Naganovf4a75362021-09-16 00:02:54 +00002389 std::vector<AudioDevice> devicesAidl;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002390 RETURN_STATUS_IF_ERROR(statusTFromBinderStatus(
2391 aps->getDevicesForRoleAndStrategy(strategyAidl, roleAidl, &devicesAidl)));
2392 devices = VALUE_OR_RETURN_STATUS(
2393 convertContainer<AudioDeviceTypeAddrVector>(devicesAidl,
2394 aidl2legacy_AudioDeviceTypeAddress));
2395 return OK;
Jean-Michel Trivi30857152019-11-01 11:04:15 -07002396}
2397
Jiabin Huang3b98d322020-09-03 17:54:16 +00002398status_t AudioSystem::setDevicesRoleForCapturePreset(audio_source_t audioSource,
2399 device_role_t role,
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002400 const AudioDeviceTypeAddrVector& devices) {
Andy Hung264fa4c2024-02-21 15:52:12 -08002401 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Jiabin Huang3b98d322020-09-03 17:54:16 +00002402 if (aps == 0) {
2403 return PERMISSION_DENIED;
2404 }
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002405
Mikhail Naganovddceecc2021-09-03 13:58:56 -07002406 AudioSource audioSourceAidl = VALUE_OR_RETURN_STATUS(
2407 legacy2aidl_audio_source_t_AudioSource(audioSource));
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002408 media::DeviceRole roleAidl = VALUE_OR_RETURN_STATUS(legacy2aidl_device_role_t_DeviceRole(role));
Mikhail Naganovf4a75362021-09-16 00:02:54 +00002409 std::vector<AudioDevice> devicesAidl = VALUE_OR_RETURN_STATUS(
2410 convertContainer<std::vector<AudioDevice>>(devices,
2411 legacy2aidl_AudioDeviceTypeAddress));
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002412 return statusTFromBinderStatus(
2413 aps->setDevicesRoleForCapturePreset(audioSourceAidl, roleAidl, devicesAidl));
Jiabin Huang3b98d322020-09-03 17:54:16 +00002414}
2415
2416status_t AudioSystem::addDevicesRoleForCapturePreset(audio_source_t audioSource,
2417 device_role_t role,
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002418 const AudioDeviceTypeAddrVector& devices) {
Andy Hung264fa4c2024-02-21 15:52:12 -08002419 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Jiabin Huang3b98d322020-09-03 17:54:16 +00002420 if (aps == 0) {
2421 return PERMISSION_DENIED;
2422 }
Mikhail Naganovddceecc2021-09-03 13:58:56 -07002423 AudioSource audioSourceAidl = VALUE_OR_RETURN_STATUS(
2424 legacy2aidl_audio_source_t_AudioSource(audioSource));
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002425 media::DeviceRole roleAidl = VALUE_OR_RETURN_STATUS(legacy2aidl_device_role_t_DeviceRole(role));
Mikhail Naganovf4a75362021-09-16 00:02:54 +00002426 std::vector<AudioDevice> devicesAidl = VALUE_OR_RETURN_STATUS(
2427 convertContainer<std::vector<AudioDevice>>(devices,
2428 legacy2aidl_AudioDeviceTypeAddress));
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002429 return statusTFromBinderStatus(
2430 aps->addDevicesRoleForCapturePreset(audioSourceAidl, roleAidl, devicesAidl));
Jiabin Huang3b98d322020-09-03 17:54:16 +00002431}
2432
2433status_t AudioSystem::removeDevicesRoleForCapturePreset(
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002434 audio_source_t audioSource, device_role_t role, const AudioDeviceTypeAddrVector& devices) {
Andy Hung264fa4c2024-02-21 15:52:12 -08002435 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Jiabin Huang3b98d322020-09-03 17:54:16 +00002436 if (aps == 0) {
2437 return PERMISSION_DENIED;
2438 }
Mikhail Naganovddceecc2021-09-03 13:58:56 -07002439 AudioSource audioSourceAidl = VALUE_OR_RETURN_STATUS(
2440 legacy2aidl_audio_source_t_AudioSource(audioSource));
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002441 media::DeviceRole roleAidl = VALUE_OR_RETURN_STATUS(legacy2aidl_device_role_t_DeviceRole(role));
Mikhail Naganovf4a75362021-09-16 00:02:54 +00002442 std::vector<AudioDevice> devicesAidl = VALUE_OR_RETURN_STATUS(
2443 convertContainer<std::vector<AudioDevice>>(devices,
2444 legacy2aidl_AudioDeviceTypeAddress));
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002445 return statusTFromBinderStatus(
2446 aps->removeDevicesRoleForCapturePreset(audioSourceAidl, roleAidl, devicesAidl));
Jiabin Huang3b98d322020-09-03 17:54:16 +00002447}
2448
2449status_t AudioSystem::clearDevicesRoleForCapturePreset(audio_source_t audioSource,
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002450 device_role_t role) {
Andy Hung264fa4c2024-02-21 15:52:12 -08002451 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Jiabin Huang3b98d322020-09-03 17:54:16 +00002452 if (aps == 0) {
2453 return PERMISSION_DENIED;
2454 }
Mikhail Naganovddceecc2021-09-03 13:58:56 -07002455 AudioSource audioSourceAidl = VALUE_OR_RETURN_STATUS(
2456 legacy2aidl_audio_source_t_AudioSource(audioSource));
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002457 media::DeviceRole roleAidl = VALUE_OR_RETURN_STATUS(legacy2aidl_device_role_t_DeviceRole(role));
2458 return statusTFromBinderStatus(
2459 aps->clearDevicesRoleForCapturePreset(audioSourceAidl, roleAidl));
Jiabin Huang3b98d322020-09-03 17:54:16 +00002460}
2461
2462status_t AudioSystem::getDevicesForRoleAndCapturePreset(audio_source_t audioSource,
2463 device_role_t role,
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002464 AudioDeviceTypeAddrVector& devices) {
Andy Hung264fa4c2024-02-21 15:52:12 -08002465 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Jiabin Huang3b98d322020-09-03 17:54:16 +00002466 if (aps == 0) {
2467 return PERMISSION_DENIED;
2468 }
Mikhail Naganovddceecc2021-09-03 13:58:56 -07002469 AudioSource audioSourceAidl = VALUE_OR_RETURN_STATUS(
2470 legacy2aidl_audio_source_t_AudioSource(audioSource));
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002471 media::DeviceRole roleAidl = VALUE_OR_RETURN_STATUS(legacy2aidl_device_role_t_DeviceRole(role));
Mikhail Naganovf4a75362021-09-16 00:02:54 +00002472 std::vector<AudioDevice> devicesAidl;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002473 RETURN_STATUS_IF_ERROR(statusTFromBinderStatus(
2474 aps->getDevicesForRoleAndCapturePreset(audioSourceAidl, roleAidl, &devicesAidl)));
2475 devices = VALUE_OR_RETURN_STATUS(
2476 convertContainer<AudioDeviceTypeAddrVector>(devicesAidl,
2477 aidl2legacy_AudioDeviceTypeAddress));
2478 return OK;
Jiabin Huang3b98d322020-09-03 17:54:16 +00002479}
2480
Eric Laurent81dd0f52021-07-05 11:54:40 +02002481status_t AudioSystem::getSpatializer(const sp<media::INativeSpatializerCallback>& callback,
2482 sp<media::ISpatializer>* spatializer) {
Andy Hung264fa4c2024-02-21 15:52:12 -08002483 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Eric Laurent81dd0f52021-07-05 11:54:40 +02002484 if (spatializer == nullptr) {
2485 return BAD_VALUE;
2486 }
2487 if (aps == 0) {
2488 return PERMISSION_DENIED;
2489 }
2490 media::GetSpatializerResponse response;
2491 RETURN_STATUS_IF_ERROR(statusTFromBinderStatus(
2492 aps->getSpatializer(callback, &response)));
2493
2494 *spatializer = response.spatializer;
2495 return OK;
2496}
2497
2498status_t AudioSystem::canBeSpatialized(const audio_attributes_t *attr,
2499 const audio_config_t *config,
2500 const AudioDeviceTypeAddrVector &devices,
2501 bool *canBeSpatialized) {
Andy Hung264fa4c2024-02-21 15:52:12 -08002502 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Ram Mohan43297012022-08-30 16:11:49 +05302503 if (canBeSpatialized == nullptr) {
2504 return BAD_VALUE;
2505 }
Eric Laurent81dd0f52021-07-05 11:54:40 +02002506 if (aps == 0) {
2507 return PERMISSION_DENIED;
2508 }
2509 audio_attributes_t attributes = attr != nullptr ? *attr : AUDIO_ATTRIBUTES_INITIALIZER;
2510 audio_config_t configuration = config != nullptr ? *config : AUDIO_CONFIG_INITIALIZER;
2511
Mikhail Naganov1c400902023-05-17 11:48:43 -07002512 std::optional<media::audio::common::AudioAttributes> attrAidl = VALUE_OR_RETURN_STATUS(
2513 legacy2aidl_audio_attributes_t_AudioAttributes(attributes));
Mikhail Naganovdbf03642021-08-25 18:15:32 -07002514 std::optional<AudioConfig> configAidl = VALUE_OR_RETURN_STATUS(
Eric Laurent81dd0f52021-07-05 11:54:40 +02002515 legacy2aidl_audio_config_t_AudioConfig(configuration, false /*isInput*/));
Mikhail Naganovf4a75362021-09-16 00:02:54 +00002516 std::vector<AudioDevice> devicesAidl = VALUE_OR_RETURN_STATUS(
2517 convertContainer<std::vector<AudioDevice>>(devices,
2518 legacy2aidl_AudioDeviceTypeAddress));
Eric Laurent81dd0f52021-07-05 11:54:40 +02002519 RETURN_STATUS_IF_ERROR(statusTFromBinderStatus(
2520 aps->canBeSpatialized(attrAidl, configAidl, devicesAidl, canBeSpatialized)));
2521 return OK;
2522}
2523
Vlad Popae3fd1c22022-11-07 21:03:18 +01002524status_t AudioSystem::getSoundDoseInterface(const sp<media::ISoundDoseCallback>& callback,
2525 sp<media::ISoundDose>* soundDose) {
Andy Hung264fa4c2024-02-21 15:52:12 -08002526 const sp<IAudioFlinger> af = get_audio_flinger();
Vlad Popa63f047e2022-11-05 14:09:19 +01002527 if (af == nullptr) {
2528 return PERMISSION_DENIED;
2529 }
Vlad Popae3fd1c22022-11-07 21:03:18 +01002530 if (soundDose == nullptr) {
2531 return BAD_VALUE;
2532 }
Vlad Popa63f047e2022-11-05 14:09:19 +01002533
Vlad Popae3fd1c22022-11-07 21:03:18 +01002534 RETURN_STATUS_IF_ERROR(af->getSoundDoseInterface(callback, soundDose));
2535 return OK;
Vlad Popa63f047e2022-11-05 14:09:19 +01002536}
2537
jiabin2b9d5a12021-12-10 01:06:29 +00002538status_t AudioSystem::getDirectPlaybackSupport(const audio_attributes_t *attr,
2539 const audio_config_t *config,
2540 audio_direct_mode_t* directMode) {
2541 if (attr == nullptr || config == nullptr || directMode == nullptr) {
2542 return BAD_VALUE;
2543 }
2544
Andy Hung264fa4c2024-02-21 15:52:12 -08002545 const sp<IAudioPolicyService> aps = get_audio_policy_service();
jiabin2b9d5a12021-12-10 01:06:29 +00002546 if (aps == 0) {
2547 return PERMISSION_DENIED;
2548 }
2549
Mikhail Naganov1c400902023-05-17 11:48:43 -07002550 media::audio::common::AudioAttributes attrAidl = VALUE_OR_RETURN_STATUS(
2551 legacy2aidl_audio_attributes_t_AudioAttributes(*attr));
jiabin2b9d5a12021-12-10 01:06:29 +00002552 AudioConfig configAidl = VALUE_OR_RETURN_STATUS(
2553 legacy2aidl_audio_config_t_AudioConfig(*config, false /*isInput*/));
2554
2555 media::AudioDirectMode retAidl;
2556 RETURN_STATUS_IF_ERROR(statusTFromBinderStatus(
2557 aps->getDirectPlaybackSupport(attrAidl, configAidl, &retAidl)));
2558 *directMode = VALUE_OR_RETURN_STATUS(aidl2legacy_int32_t_audio_direct_mode_t_mask(
2559 static_cast<int32_t>(retAidl)));
2560 return NO_ERROR;
2561}
2562
Dorin Drimusf2196d82022-01-03 12:11:18 +01002563status_t AudioSystem::getDirectProfilesForAttributes(const audio_attributes_t* attr,
2564 std::vector<audio_profile>* audioProfiles) {
Mikhail Naganovefc504b2022-06-22 03:24:59 +00002565 if (attr == nullptr || audioProfiles == nullptr) {
Dorin Drimusf2196d82022-01-03 12:11:18 +01002566 return BAD_VALUE;
2567 }
2568
Andy Hung264fa4c2024-02-21 15:52:12 -08002569 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Dorin Drimusf2196d82022-01-03 12:11:18 +01002570 if (aps == 0) {
2571 return PERMISSION_DENIED;
2572 }
2573
Mikhail Naganov1c400902023-05-17 11:48:43 -07002574 media::audio::common::AudioAttributes attrAidl = VALUE_OR_RETURN_STATUS(
2575 legacy2aidl_audio_attributes_t_AudioAttributes(*attr));
Dorin Drimusf2196d82022-01-03 12:11:18 +01002576
2577 std::vector<media::audio::common::AudioProfile> audioProfilesAidl;
2578 RETURN_STATUS_IF_ERROR(statusTFromBinderStatus(
2579 aps->getDirectProfilesForAttributes(attrAidl, &audioProfilesAidl)));
2580 *audioProfiles = VALUE_OR_RETURN_STATUS(convertContainer<std::vector<audio_profile>>(
2581 audioProfilesAidl, aidl2legacy_AudioProfile_audio_profile, false /*isInput*/));
2582
2583 return NO_ERROR;
2584}
Eric Laurent81dd0f52021-07-05 11:54:40 +02002585
Eric Laurent076e7c72022-05-03 18:12:28 +02002586status_t AudioSystem::setRequestedLatencyMode(
2587 audio_io_handle_t output, audio_latency_mode_t mode) {
Andy Hung264fa4c2024-02-21 15:52:12 -08002588 const sp<IAudioFlinger> af = get_audio_flinger();
Eric Laurent076e7c72022-05-03 18:12:28 +02002589 if (af == nullptr) {
2590 return PERMISSION_DENIED;
2591 }
2592 return af->setRequestedLatencyMode(output, mode);
2593}
2594
2595status_t AudioSystem::getSupportedLatencyModes(audio_io_handle_t output,
2596 std::vector<audio_latency_mode_t>* modes) {
Andy Hung264fa4c2024-02-21 15:52:12 -08002597 const sp<IAudioFlinger> af = get_audio_flinger();
Eric Laurent076e7c72022-05-03 18:12:28 +02002598 if (af == nullptr) {
2599 return PERMISSION_DENIED;
2600 }
2601 return af->getSupportedLatencyModes(output, modes);
2602}
2603
Eric Laurent50d72582022-12-20 20:20:23 +01002604status_t AudioSystem::setBluetoothVariableLatencyEnabled(bool enabled) {
Andy Hung264fa4c2024-02-21 15:52:12 -08002605 const sp<IAudioFlinger> af = get_audio_flinger();
Eric Laurent52057642022-12-16 11:45:07 +01002606 if (af == nullptr) {
2607 return PERMISSION_DENIED;
2608 }
Eric Laurent50d72582022-12-20 20:20:23 +01002609 return af->setBluetoothVariableLatencyEnabled(enabled);
Eric Laurent52057642022-12-16 11:45:07 +01002610}
2611
Eric Laurent50d72582022-12-20 20:20:23 +01002612status_t AudioSystem::isBluetoothVariableLatencyEnabled(
2613 bool *enabled) {
Andy Hung264fa4c2024-02-21 15:52:12 -08002614 const sp<IAudioFlinger> af = get_audio_flinger();
Eric Laurent50d72582022-12-20 20:20:23 +01002615 if (af == nullptr) {
2616 return PERMISSION_DENIED;
2617 }
2618 return af->isBluetoothVariableLatencyEnabled(enabled);
2619}
2620
2621status_t AudioSystem::supportsBluetoothVariableLatency(
Eric Laurent52057642022-12-16 11:45:07 +01002622 bool *support) {
Andy Hung264fa4c2024-02-21 15:52:12 -08002623 const sp<IAudioFlinger> af = get_audio_flinger();
Eric Laurent52057642022-12-16 11:45:07 +01002624 if (af == nullptr) {
2625 return PERMISSION_DENIED;
2626 }
Eric Laurent50d72582022-12-20 20:20:23 +01002627 return af->supportsBluetoothVariableLatency(support);
Eric Laurent52057642022-12-16 11:45:07 +01002628}
2629
Mikhail Naganovffd97712023-05-03 17:45:36 -07002630status_t AudioSystem::getAudioPolicyConfig(media::AudioPolicyConfig *config) {
Andy Hung264fa4c2024-02-21 15:52:12 -08002631 const sp<IAudioFlinger> af = get_audio_flinger();
Mikhail Naganovffd97712023-05-03 17:45:36 -07002632 if (af == nullptr) {
2633 return PERMISSION_DENIED;
2634 }
2635 return af->getAudioPolicyConfig(config);
2636}
2637
Ytai Ben-Tsvi5120eda2020-03-26 09:41:15 -07002638class CaptureStateListenerImpl : public media::BnCaptureStateListener,
2639 public IBinder::DeathRecipient {
2640public:
Ytai Ben-Tsvid9f82832020-08-27 09:31:10 -07002641 CaptureStateListenerImpl(
2642 const sp<IAudioPolicyService>& aps,
2643 const sp<AudioSystem::CaptureStateListener>& listener)
Ytai Ben-Tsvia46b6d32020-08-31 13:29:11 -07002644 : mAps(aps), mListener(listener) {}
2645
2646 void init() {
Ytai Ben-Tsvid9f82832020-08-27 09:31:10 -07002647 bool active;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002648 status_t status = statusTFromBinderStatus(
2649 mAps->registerSoundTriggerCaptureStateListener(this, &active));
Ytai Ben-Tsvid9f82832020-08-27 09:31:10 -07002650 if (status != NO_ERROR) {
2651 mListener->onServiceDied();
2652 return;
2653 }
2654 mListener->onStateChanged(active);
Ytai Ben-Tsvia46b6d32020-08-31 13:29:11 -07002655 IInterface::asBinder(mAps)->linkToDeath(this);
Ytai Ben-Tsvid9f82832020-08-27 09:31:10 -07002656 }
2657
Ytai Ben-Tsvi5120eda2020-03-26 09:41:15 -07002658 binder::Status setCaptureState(bool active) override {
Andy Hungacd955a2024-02-16 17:14:36 -08002659 std::lock_guard _l(AudioSystem::gSoundTriggerMutex);
Ytai Ben-Tsvid9f82832020-08-27 09:31:10 -07002660 mListener->onStateChanged(active);
Ytai Ben-Tsvi5120eda2020-03-26 09:41:15 -07002661 return binder::Status::ok();
2662 }
2663
2664 void binderDied(const wp<IBinder>&) override {
Andy Hungacd955a2024-02-16 17:14:36 -08002665 std::lock_guard _l(AudioSystem::gSoundTriggerMutex);
Ytai Ben-Tsvid9f82832020-08-27 09:31:10 -07002666 mListener->onServiceDied();
Andy Hungacd955a2024-02-16 17:14:36 -08002667 AudioSystem::gSoundTriggerCaptureStateListener = nullptr;
Ytai Ben-Tsvi5120eda2020-03-26 09:41:15 -07002668 }
Ytai Ben-Tsvid9f82832020-08-27 09:31:10 -07002669
2670private:
2671 // Need this in order to keep the death receipent alive.
2672 sp<IAudioPolicyService> mAps;
2673 sp<AudioSystem::CaptureStateListener> mListener;
Ytai Ben-Tsvi5120eda2020-03-26 09:41:15 -07002674};
2675
2676status_t AudioSystem::registerSoundTriggerCaptureStateListener(
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002677 const sp<CaptureStateListener>& listener) {
Ytai Ben-Tsvid9f82832020-08-27 09:31:10 -07002678 LOG_ALWAYS_FATAL_IF(listener == nullptr);
2679
Andy Hung264fa4c2024-02-21 15:52:12 -08002680 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Ytai Ben-Tsvi5120eda2020-03-26 09:41:15 -07002681 if (aps == 0) {
2682 return PERMISSION_DENIED;
2683 }
2684
Andy Hungacd955a2024-02-16 17:14:36 -08002685 std::lock_guard _l(AudioSystem::gSoundTriggerMutex);
Ytai Ben-Tsvid9f82832020-08-27 09:31:10 -07002686 gSoundTriggerCaptureStateListener = new CaptureStateListenerImpl(aps, listener);
Ytai Ben-Tsvia46b6d32020-08-31 13:29:11 -07002687 gSoundTriggerCaptureStateListener->init();
Ytai Ben-Tsvi5120eda2020-03-26 09:41:15 -07002688
Ytai Ben-Tsvi5120eda2020-03-26 09:41:15 -07002689 return NO_ERROR;
2690}
2691
jiabin1319f5a2021-03-30 22:21:24 +00002692status_t AudioSystem::setVibratorInfos(
2693 const std::vector<media::AudioVibratorInfo>& vibratorInfos) {
Andy Hung264fa4c2024-02-21 15:52:12 -08002694 const sp<IAudioFlinger> af = get_audio_flinger();
jiabin1319f5a2021-03-30 22:21:24 +00002695 if (af == nullptr) {
2696 return PERMISSION_DENIED;
2697 }
2698 return af->setVibratorInfos(vibratorInfos);
2699}
2700
Jiabin Huangebe64102021-09-07 20:01:07 +00002701status_t AudioSystem::getMmapPolicyInfo(
jiabine99d0882021-09-17 05:21:25 +00002702 AudioMMapPolicyType policyType, std::vector<AudioMMapPolicyInfo> *policyInfos) {
Andy Hung264fa4c2024-02-21 15:52:12 -08002703 const sp<IAudioFlinger> af = get_audio_flinger();
Jiabin Huangebe64102021-09-07 20:01:07 +00002704 if (af == nullptr) {
2705 return PERMISSION_DENIED;
2706 }
2707 return af->getMmapPolicyInfos(policyType, policyInfos);
2708}
2709
jiabine504e7b2021-09-18 00:27:08 +00002710int32_t AudioSystem::getAAudioMixerBurstCount() {
Andy Hung264fa4c2024-02-21 15:52:12 -08002711 const sp<IAudioFlinger> af = get_audio_flinger();
jiabine504e7b2021-09-18 00:27:08 +00002712 if (af == nullptr) {
2713 return PERMISSION_DENIED;
2714 }
2715 return af->getAAudioMixerBurstCount();
2716}
2717
2718int32_t AudioSystem::getAAudioHardwareBurstMinUsec() {
Andy Hung264fa4c2024-02-21 15:52:12 -08002719 const sp<IAudioFlinger> af = get_audio_flinger();
jiabine504e7b2021-09-18 00:27:08 +00002720 if (af == nullptr) {
2721 return PERMISSION_DENIED;
2722 }
2723 return af->getAAudioHardwareBurstMinUsec();
2724}
2725
jiabina84c3d32022-12-02 18:59:55 +00002726status_t AudioSystem::getSupportedMixerAttributes(
2727 audio_port_handle_t portId, std::vector<audio_mixer_attributes_t> *mixerAttrs) {
Andy Hung264fa4c2024-02-21 15:52:12 -08002728 const sp<IAudioPolicyService> aps = get_audio_policy_service();
jiabina84c3d32022-12-02 18:59:55 +00002729 if (aps == nullptr) {
2730 return PERMISSION_DENIED;
2731 }
2732
2733 int32_t portIdAidl = VALUE_OR_RETURN_STATUS(legacy2aidl_audio_port_handle_t_int32_t(portId));
2734 std::vector<media::AudioMixerAttributesInternal> _aidlReturn;
2735 RETURN_STATUS_IF_ERROR(statusTFromBinderStatus(
2736 aps->getSupportedMixerAttributes(portIdAidl, &_aidlReturn)));
2737 *mixerAttrs = VALUE_OR_RETURN_STATUS(
2738 convertContainer<std::vector<audio_mixer_attributes_t>>(
2739 _aidlReturn,
2740 aidl2legacy_AudioMixerAttributesInternal_audio_mixer_attributes_t));
2741 return OK;
2742}
2743
2744status_t AudioSystem::setPreferredMixerAttributes(const audio_attributes_t *attr,
2745 audio_port_handle_t portId,
2746 uid_t uid,
2747 const audio_mixer_attributes_t *mixerAttr) {
Andy Hung264fa4c2024-02-21 15:52:12 -08002748 const sp<IAudioPolicyService> aps = get_audio_policy_service();
jiabina84c3d32022-12-02 18:59:55 +00002749 if (aps == nullptr) {
2750 return PERMISSION_DENIED;
2751 }
2752
Mikhail Naganov1c400902023-05-17 11:48:43 -07002753 media::audio::common::AudioAttributes attrAidl = VALUE_OR_RETURN_STATUS(
2754 legacy2aidl_audio_attributes_t_AudioAttributes(*attr));
jiabina84c3d32022-12-02 18:59:55 +00002755 media::AudioMixerAttributesInternal mixerAttrAidl = VALUE_OR_RETURN_STATUS(
2756 legacy2aidl_audio_mixer_attributes_t_AudioMixerAttributesInternal(*mixerAttr));
2757 int32_t uidAidl = VALUE_OR_RETURN_STATUS(legacy2aidl_uid_t_int32_t(uid));
2758 int32_t portIdAidl = VALUE_OR_RETURN_STATUS(legacy2aidl_audio_port_handle_t_int32_t(portId));
2759
2760 return statusTFromBinderStatus(
2761 aps->setPreferredMixerAttributes(attrAidl, portIdAidl, uidAidl, mixerAttrAidl));
2762}
2763
2764status_t AudioSystem::getPreferredMixerAttributes(
2765 const audio_attributes_t *attr,
2766 audio_port_handle_t portId,
2767 std::optional<audio_mixer_attributes_t> *mixerAttr) {
Andy Hung264fa4c2024-02-21 15:52:12 -08002768 const sp<IAudioPolicyService> aps = get_audio_policy_service();
jiabina84c3d32022-12-02 18:59:55 +00002769 if (aps == nullptr) {
2770 return PERMISSION_DENIED;
2771 }
2772
Mikhail Naganov1c400902023-05-17 11:48:43 -07002773 media::audio::common::AudioAttributes attrAidl = VALUE_OR_RETURN_STATUS(
2774 legacy2aidl_audio_attributes_t_AudioAttributes(*attr));
jiabina84c3d32022-12-02 18:59:55 +00002775 int32_t portIdAidl = VALUE_OR_RETURN_STATUS(legacy2aidl_audio_port_handle_t_int32_t(portId));
2776 std::optional<media::AudioMixerAttributesInternal> _aidlReturn;
2777 RETURN_STATUS_IF_ERROR(statusTFromBinderStatus(
2778 aps->getPreferredMixerAttributes(attrAidl, portIdAidl, &_aidlReturn)));
2779
2780 if (_aidlReturn.has_value()) {
2781 *mixerAttr = VALUE_OR_RETURN_STATUS(
2782 aidl2legacy_AudioMixerAttributesInternal_audio_mixer_attributes_t(
2783 _aidlReturn.value()));
2784 }
2785 return NO_ERROR;
2786}
2787
2788status_t AudioSystem::clearPreferredMixerAttributes(const audio_attributes_t *attr,
2789 audio_port_handle_t portId,
2790 uid_t uid) {
Andy Hung264fa4c2024-02-21 15:52:12 -08002791 const sp<IAudioPolicyService> aps = get_audio_policy_service();
jiabina84c3d32022-12-02 18:59:55 +00002792 if (aps == nullptr) {
2793 return PERMISSION_DENIED;
2794 }
2795
Mikhail Naganov1c400902023-05-17 11:48:43 -07002796 media::audio::common::AudioAttributes attrAidl = VALUE_OR_RETURN_STATUS(
2797 legacy2aidl_audio_attributes_t_AudioAttributes(*attr));
jiabina84c3d32022-12-02 18:59:55 +00002798 int32_t uidAidl = VALUE_OR_RETURN_STATUS(legacy2aidl_uid_t_int32_t(uid));
2799 int32_t portIdAidl = VALUE_OR_RETURN_STATUS(legacy2aidl_audio_port_handle_t_int32_t(portId));
2800 return statusTFromBinderStatus(
2801 aps->clearPreferredMixerAttributes(attrAidl, portIdAidl, uidAidl));
2802}
2803
Eric Laurentc2f1f072009-07-17 12:17:14 -07002804// ---------------------------------------------------------------------------
2805
Eric Laurente8726fe2015-06-26 09:39:24 -07002806int AudioSystem::AudioPolicyServiceClient::addAudioPortCallback(
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002807 const sp<AudioPortCallback>& callback) {
Andy Hungacd955a2024-02-16 17:14:36 -08002808 std::lock_guard _l(mMutex);
Andy Hungd211ce02024-02-16 15:34:07 -08002809 return mAudioPortCallbacks.insert(callback).second ? mAudioPortCallbacks.size() : -1;
Eric Laurentb28753e2015-04-01 13:06:28 -07002810}
2811
Eric Laurente8726fe2015-06-26 09:39:24 -07002812int AudioSystem::AudioPolicyServiceClient::removeAudioPortCallback(
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002813 const sp<AudioPortCallback>& callback) {
Andy Hungacd955a2024-02-16 17:14:36 -08002814 std::lock_guard _l(mMutex);
Andy Hungd211ce02024-02-16 15:34:07 -08002815 return mAudioPortCallbacks.erase(callback) > 0 ? mAudioPortCallbacks.size() : -1;
Eric Laurentb28753e2015-04-01 13:06:28 -07002816}
2817
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002818Status AudioSystem::AudioPolicyServiceClient::onAudioPortListUpdate() {
Andy Hungacd955a2024-02-16 17:14:36 -08002819 std::lock_guard _l(mMutex);
Andy Hungd211ce02024-02-16 15:34:07 -08002820 for (const auto& callback : mAudioPortCallbacks) {
2821 callback->onAudioPortListUpdate();
Eric Laurentb28753e2015-04-01 13:06:28 -07002822 }
Ytai Ben-Tsvi7e7a79d2020-12-15 16:48:16 -08002823 return Status::ok();
Eric Laurentb28753e2015-04-01 13:06:28 -07002824}
2825
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002826Status AudioSystem::AudioPolicyServiceClient::onAudioPatchListUpdate() {
Andy Hungacd955a2024-02-16 17:14:36 -08002827 std::lock_guard _l(mMutex);
Andy Hungd211ce02024-02-16 15:34:07 -08002828 for (const auto& callback : mAudioPortCallbacks) {
2829 callback->onAudioPatchListUpdate();
Eric Laurentb28753e2015-04-01 13:06:28 -07002830 }
Ytai Ben-Tsvi7e7a79d2020-12-15 16:48:16 -08002831 return Status::ok();
Eric Laurentb28753e2015-04-01 13:06:28 -07002832}
2833
François Gaffiecfe17322018-11-07 13:41:29 +01002834// ----------------------------------------------------------------------------
2835int AudioSystem::AudioPolicyServiceClient::addAudioVolumeGroupCallback(
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002836 const sp<AudioVolumeGroupCallback>& callback) {
Andy Hungacd955a2024-02-16 17:14:36 -08002837 std::lock_guard _l(mMutex);
Andy Hungd211ce02024-02-16 15:34:07 -08002838 return mAudioVolumeGroupCallbacks.insert(callback).second
2839 ? mAudioVolumeGroupCallbacks.size() : -1;
François Gaffiecfe17322018-11-07 13:41:29 +01002840}
2841
2842int AudioSystem::AudioPolicyServiceClient::removeAudioVolumeGroupCallback(
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002843 const sp<AudioVolumeGroupCallback>& callback) {
Andy Hungacd955a2024-02-16 17:14:36 -08002844 std::lock_guard _l(mMutex);
Andy Hungd211ce02024-02-16 15:34:07 -08002845 return mAudioVolumeGroupCallbacks.erase(callback) > 0
2846 ? mAudioVolumeGroupCallbacks.size() : -1;
François Gaffiecfe17322018-11-07 13:41:29 +01002847}
2848
Ytai Ben-Tsvi7e7a79d2020-12-15 16:48:16 -08002849Status AudioSystem::AudioPolicyServiceClient::onAudioVolumeGroupChanged(int32_t group,
2850 int32_t flags) {
2851 volume_group_t groupLegacy = VALUE_OR_RETURN_BINDER_STATUS(
2852 aidl2legacy_int32_t_volume_group_t(group));
2853 int flagsLegacy = VALUE_OR_RETURN_BINDER_STATUS(convertReinterpret<int>(flags));
2854
Andy Hungacd955a2024-02-16 17:14:36 -08002855 std::lock_guard _l(mMutex);
Andy Hungd211ce02024-02-16 15:34:07 -08002856 for (const auto& callback : mAudioVolumeGroupCallbacks) {
2857 callback->onAudioVolumeGroupChanged(groupLegacy, flagsLegacy);
François Gaffiecfe17322018-11-07 13:41:29 +01002858 }
Ytai Ben-Tsvi7e7a79d2020-12-15 16:48:16 -08002859 return Status::ok();
François Gaffiecfe17322018-11-07 13:41:29 +01002860}
2861// ----------------------------------------------------------------------------
2862
Ytai Ben-Tsvi7e7a79d2020-12-15 16:48:16 -08002863Status AudioSystem::AudioPolicyServiceClient::onDynamicPolicyMixStateUpdate(
2864 const ::std::string& regId, int32_t state) {
2865 ALOGV("AudioPolicyServiceClient::onDynamicPolicyMixStateUpdate(%s, %d)", regId.c_str(), state);
2866
2867 String8 regIdLegacy = VALUE_OR_RETURN_BINDER_STATUS(aidl2legacy_string_view_String8(regId));
2868 int stateLegacy = VALUE_OR_RETURN_BINDER_STATUS(convertReinterpret<int>(state));
Jean-Michel Trivif613d422015-04-23 18:41:29 -07002869 dynamic_policy_callback cb = NULL;
2870 {
Andy Hungacd955a2024-02-16 17:14:36 -08002871 std::lock_guard _l(AudioSystem::gMutex);
Jean-Michel Trivif613d422015-04-23 18:41:29 -07002872 cb = gDynPolicyCallback;
2873 }
2874
2875 if (cb != NULL) {
Ytai Ben-Tsvi7e7a79d2020-12-15 16:48:16 -08002876 cb(DYNAMIC_POLICY_EVENT_MIX_STATE_UPDATE, regIdLegacy, stateLegacy);
Jean-Michel Trivif613d422015-04-23 18:41:29 -07002877 }
Ytai Ben-Tsvi7e7a79d2020-12-15 16:48:16 -08002878 return Status::ok();
Jean-Michel Trivide801052015-04-14 19:10:14 -07002879}
2880
Ytai Ben-Tsvi7e7a79d2020-12-15 16:48:16 -08002881Status AudioSystem::AudioPolicyServiceClient::onRecordingConfigurationUpdate(
2882 int32_t event,
2883 const media::RecordClientInfo& clientInfo,
Mikhail Naganovdbf03642021-08-25 18:15:32 -07002884 const AudioConfigBase& clientConfig,
Ytai Ben-Tsvi7e7a79d2020-12-15 16:48:16 -08002885 const std::vector<media::EffectDescriptor>& clientEffects,
Mikhail Naganovdbf03642021-08-25 18:15:32 -07002886 const AudioConfigBase& deviceConfig,
Ytai Ben-Tsvi7e7a79d2020-12-15 16:48:16 -08002887 const std::vector<media::EffectDescriptor>& effects,
2888 int32_t patchHandle,
Mikhail Naganovddceecc2021-09-03 13:58:56 -07002889 AudioSource source) {
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -08002890 record_config_callback cb = NULL;
2891 {
Andy Hungacd955a2024-02-16 17:14:36 -08002892 std::lock_guard _l(AudioSystem::gMutex);
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -08002893 cb = gRecordConfigCallback;
2894 }
2895
2896 if (cb != NULL) {
Ytai Ben-Tsvi7e7a79d2020-12-15 16:48:16 -08002897 int eventLegacy = VALUE_OR_RETURN_BINDER_STATUS(convertReinterpret<int>(event));
2898 record_client_info_t clientInfoLegacy = VALUE_OR_RETURN_BINDER_STATUS(
2899 aidl2legacy_RecordClientInfo_record_client_info_t(clientInfo));
2900 audio_config_base_t clientConfigLegacy = VALUE_OR_RETURN_BINDER_STATUS(
Mikhail Naganovde3fa182021-07-30 15:06:42 -07002901 aidl2legacy_AudioConfigBase_audio_config_base_t(clientConfig, true /*isInput*/));
Ytai Ben-Tsvi7e7a79d2020-12-15 16:48:16 -08002902 std::vector<effect_descriptor_t> clientEffectsLegacy = VALUE_OR_RETURN_BINDER_STATUS(
2903 convertContainer<std::vector<effect_descriptor_t>>(
2904 clientEffects,
2905 aidl2legacy_EffectDescriptor_effect_descriptor_t));
2906 audio_config_base_t deviceConfigLegacy = VALUE_OR_RETURN_BINDER_STATUS(
Mikhail Naganovde3fa182021-07-30 15:06:42 -07002907 aidl2legacy_AudioConfigBase_audio_config_base_t(deviceConfig, true /*isInput*/));
Ytai Ben-Tsvi7e7a79d2020-12-15 16:48:16 -08002908 std::vector<effect_descriptor_t> effectsLegacy = VALUE_OR_RETURN_BINDER_STATUS(
2909 convertContainer<std::vector<effect_descriptor_t>>(
2910 effects,
2911 aidl2legacy_EffectDescriptor_effect_descriptor_t));
2912 audio_patch_handle_t patchHandleLegacy = VALUE_OR_RETURN_BINDER_STATUS(
2913 aidl2legacy_int32_t_audio_patch_handle_t(patchHandle));
2914 audio_source_t sourceLegacy = VALUE_OR_RETURN_BINDER_STATUS(
Mikhail Naganovddceecc2021-09-03 13:58:56 -07002915 aidl2legacy_AudioSource_audio_source_t(source));
Ytai Ben-Tsvi7e7a79d2020-12-15 16:48:16 -08002916 cb(eventLegacy, &clientInfoLegacy, &clientConfigLegacy, clientEffectsLegacy,
2917 &deviceConfigLegacy, effectsLegacy, patchHandleLegacy, sourceLegacy);
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -08002918 }
Ytai Ben-Tsvi7e7a79d2020-12-15 16:48:16 -08002919 return Status::ok();
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -08002920}
2921
Jean-Michel Trivi9a6b9ad2020-10-22 16:46:43 -07002922Status AudioSystem::AudioPolicyServiceClient::onRoutingUpdated() {
2923 routing_callback cb = NULL;
2924 {
Andy Hungacd955a2024-02-16 17:14:36 -08002925 std::lock_guard _l(AudioSystem::gMutex);
Jean-Michel Trivi9a6b9ad2020-10-22 16:46:43 -07002926 cb = gRoutingCallback;
2927 }
2928
2929 if (cb != NULL) {
2930 cb();
2931 }
2932 return Status::ok();
2933}
2934
Jean-Michel Trivi78f2b302022-04-15 18:18:41 +00002935Status AudioSystem::AudioPolicyServiceClient::onVolumeRangeInitRequest() {
2936 vol_range_init_req_callback cb = NULL;
2937 {
Andy Hungacd955a2024-02-16 17:14:36 -08002938 std::lock_guard _l(AudioSystem::gMutex);
Jean-Michel Trivi78f2b302022-04-15 18:18:41 +00002939 cb = gVolRangeInitReqCallback;
2940 }
2941
2942 if (cb != NULL) {
2943 cb();
2944 }
2945 return Status::ok();
2946}
2947
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002948void AudioSystem::AudioPolicyServiceClient::binderDied(const wp<IBinder>& who __unused) {
Glenn Kastend2d089f2014-11-05 11:48:12 -08002949 {
Andy Hungacd955a2024-02-16 17:14:36 -08002950 std::lock_guard _l(mMutex);
Andy Hungd211ce02024-02-16 15:34:07 -08002951 for (const auto& callback : mAudioPortCallbacks) {
2952 callback->onServiceDied();
Glenn Kastend2d089f2014-11-05 11:48:12 -08002953 }
Andy Hungd211ce02024-02-16 15:34:07 -08002954 for (const auto& callback : mAudioVolumeGroupCallbacks) {
2955 callback->onServiceDied();
François Gaffiecfe17322018-11-07 13:41:29 +01002956 }
Eric Laurentb52c1522014-05-20 11:27:36 -07002957 }
Mikhail Naganovec3d5792022-05-06 00:19:55 +00002958 AudioSystem::clearAudioPolicyService();
Eric Laurentc2f1f072009-07-17 12:17:14 -07002959
Steve Block5ff1dd52012-01-05 23:22:43 +00002960 ALOGW("AudioPolicyService server died!");
Eric Laurentc2f1f072009-07-17 12:17:14 -07002961}
2962
Ytai Ben-Tsvi7e7a79d2020-12-15 16:48:16 -08002963ConversionResult<record_client_info_t>
2964aidl2legacy_RecordClientInfo_record_client_info_t(const media::RecordClientInfo& aidl) {
2965 record_client_info_t legacy;
2966 legacy.riid = VALUE_OR_RETURN(aidl2legacy_int32_t_audio_unique_id_t(aidl.riid));
2967 legacy.uid = VALUE_OR_RETURN(aidl2legacy_int32_t_uid_t(aidl.uid));
2968 legacy.session = VALUE_OR_RETURN(aidl2legacy_int32_t_audio_session_t(aidl.session));
Mikhail Naganovddceecc2021-09-03 13:58:56 -07002969 legacy.source = VALUE_OR_RETURN(aidl2legacy_AudioSource_audio_source_t(aidl.source));
Ytai Ben-Tsvi7e7a79d2020-12-15 16:48:16 -08002970 legacy.port_id = VALUE_OR_RETURN(aidl2legacy_int32_t_audio_port_handle_t(aidl.portId));
2971 legacy.silenced = aidl.silenced;
2972 return legacy;
2973}
2974
2975ConversionResult<media::RecordClientInfo>
2976legacy2aidl_record_client_info_t_RecordClientInfo(const record_client_info_t& legacy) {
2977 media::RecordClientInfo aidl;
2978 aidl.riid = VALUE_OR_RETURN(legacy2aidl_audio_unique_id_t_int32_t(legacy.riid));
2979 aidl.uid = VALUE_OR_RETURN(legacy2aidl_uid_t_int32_t(legacy.uid));
2980 aidl.session = VALUE_OR_RETURN(legacy2aidl_audio_session_t_int32_t(legacy.session));
Mikhail Naganovddceecc2021-09-03 13:58:56 -07002981 aidl.source = VALUE_OR_RETURN(legacy2aidl_audio_source_t_AudioSource(legacy.source));
Ytai Ben-Tsvi7e7a79d2020-12-15 16:48:16 -08002982 aidl.portId = VALUE_OR_RETURN(legacy2aidl_audio_port_handle_t_int32_t(legacy.port_id));
2983 aidl.silenced = legacy.silenced;
2984 return aidl;
2985}
2986
Glenn Kasten40bc9062015-03-20 09:09:33 -07002987} // namespace android