blob: 1da61b877a19f5ab07da75641873e38509f58e86 [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
Francois Gaffie55b2a0f2021-06-24 15:58:37 +0200325status_t AudioSystem::setPortsVolume(
326 const std::vector<audio_port_handle_t>& portIds, float volume, audio_io_handle_t output) {
327 for (const auto& port : portIds) {
328 if (port == AUDIO_PORT_HANDLE_NONE) {
329 return BAD_VALUE;
330 }
331 }
332 if (isnan(volume) || volume > 1.0f || volume < 0.0f) {
333 return BAD_VALUE;
334 }
335 const sp<IAudioFlinger> af = get_audio_flinger();
336 if (af == 0) return PERMISSION_DENIED;
337 std::vector<int32_t> portIdsAidl = VALUE_OR_RETURN_STATUS(
338 convertContainer<std::vector<int32_t>>(
339 portIds, legacy2aidl_audio_port_handle_t_int32_t));
340 int32_t outputAidl = VALUE_OR_RETURN_STATUS(legacy2aidl_audio_io_handle_t_int32_t(output));
341 af->setPortsVolume(portIdsAidl, volume, outputAidl);
342 return NO_ERROR;
343}
344
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800345status_t AudioSystem::setMode(audio_mode_t mode) {
Glenn Kasten930f4ca2012-01-06 16:47:31 -0800346 if (uint32_t(mode) >= AUDIO_MODE_CNT) return BAD_VALUE;
Andy Hung264fa4c2024-02-21 15:52:12 -0800347 const sp<IAudioFlinger> af = get_audio_flinger();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800348 if (af == 0) return PERMISSION_DENIED;
349 return af->setMode(mode);
350}
351
Mikhail Naganovb1ddbb02023-03-15 17:06:59 -0700352status_t AudioSystem::setSimulateDeviceConnections(bool enabled) {
Andy Hung264fa4c2024-02-21 15:52:12 -0800353 const sp<IAudioFlinger> af = get_audio_flinger();
Mikhail Naganovb1ddbb02023-03-15 17:06:59 -0700354 if (af == 0) return PERMISSION_DENIED;
355 return af->setSimulateDeviceConnections(enabled);
356}
357
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800358status_t AudioSystem::setParameters(audio_io_handle_t ioHandle, const String8& keyValuePairs) {
Andy Hung264fa4c2024-02-21 15:52:12 -0800359 const sp<IAudioFlinger> af = get_audio_flinger();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800360 if (af == 0) return PERMISSION_DENIED;
Eric Laurentc2f1f072009-07-17 12:17:14 -0700361 return af->setParameters(ioHandle, keyValuePairs);
362}
363
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800364String8 AudioSystem::getParameters(audio_io_handle_t ioHandle, const String8& keys) {
Andy Hung264fa4c2024-02-21 15:52:12 -0800365 const sp<IAudioFlinger> af = get_audio_flinger();
Eric Laurentc2f1f072009-07-17 12:17:14 -0700366 String8 result = String8("");
367 if (af == 0) return result;
368
369 result = af->getParameters(ioHandle, keys);
370 return result;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800371}
372
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800373status_t AudioSystem::setParameters(const String8& keyValuePairs) {
Glenn Kasten142f5192014-03-25 17:44:59 -0700374 return setParameters(AUDIO_IO_HANDLE_NONE, keyValuePairs);
Glenn Kastenc23885e2013-12-19 16:35:18 -0800375}
376
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800377String8 AudioSystem::getParameters(const String8& keys) {
Glenn Kasten142f5192014-03-25 17:44:59 -0700378 return getParameters(AUDIO_IO_HANDLE_NONE, keys);
Glenn Kastenc23885e2013-12-19 16:35:18 -0800379}
380
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800381// convert volume steps to natural log scale
382
383// change this value to change volume scaling
Andy Hungacd955a2024-02-16 17:14:36 -0800384constexpr float kdbPerStep = 0.5f;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800385// shouldn't need to touch these
Andy Hungacd955a2024-02-16 17:14:36 -0800386constexpr float kdBConvert = -kdbPerStep * 2.302585093f / 20.0f;
387constexpr float kdBConvertInverse = 1.0f / kdBConvert;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800388
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800389float AudioSystem::linearToLog(int volume) {
Andy Hungacd955a2024-02-16 17:14:36 -0800390 // float v = volume ? exp(float(100 - volume) * kdBConvert) : 0;
Steve Blockb8a80522011-12-20 16:23:08 +0000391 // ALOGD("linearToLog(%d)=%f", volume, v);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800392 // return v;
Andy Hungacd955a2024-02-16 17:14:36 -0800393 return volume ? exp(float(100 - volume) * kdBConvert) : 0;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800394}
395
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800396int AudioSystem::logToLinear(float volume) {
Andy Hungacd955a2024-02-16 17:14:36 -0800397 // int v = volume ? 100 - int(kdBConvertInverse * log(volume) + 0.5) : 0;
Steve Blockb8a80522011-12-20 16:23:08 +0000398 // ALOGD("logTolinear(%d)=%f", v, volume);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800399 // return v;
Andy Hungacd955a2024-02-16 17:14:36 -0800400 return volume ? 100 - int(kdBConvertInverse * log(volume) + 0.5) : 0;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800401}
402
Eric Laurent21da6472017-11-09 16:29:26 -0800403/* static */ size_t AudioSystem::calculateMinFrameCount(
404 uint32_t afLatencyMs, uint32_t afFrameCount, uint32_t afSampleRate,
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800405 uint32_t sampleRate, float speed /*, uint32_t notificationsPerBufferReq*/) {
Eric Laurent21da6472017-11-09 16:29:26 -0800406 // Ensure that buffer depth covers at least audio hardware latency
407 uint32_t minBufCount = afLatencyMs / ((1000 * afFrameCount) / afSampleRate);
408 if (minBufCount < 2) {
409 minBufCount = 2;
410 }
411#if 0
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800412 // The notificationsPerBufferReq parameter is not yet used for non-fast tracks,
413 // but keeping the code here to make it easier to add later.
414 if (minBufCount < notificationsPerBufferReq) {
415 minBufCount = notificationsPerBufferReq;
416 }
Eric Laurent21da6472017-11-09 16:29:26 -0800417#endif
418 ALOGV("calculateMinFrameCount afLatency %u afFrameCount %u afSampleRate %u "
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800419 "sampleRate %u speed %f minBufCount: %u" /*" notificationsPerBufferReq %u"*/,
420 afLatencyMs, afFrameCount, afSampleRate, sampleRate, speed, minBufCount
421 /*, notificationsPerBufferReq*/);
Eric Laurent21da6472017-11-09 16:29:26 -0800422 return minBufCount * sourceFramesNeededWithTimestretch(
423 sampleRate, afFrameCount, afSampleRate, speed);
424}
425
426
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800427status_t
428AudioSystem::getOutputSamplingRate(uint32_t* samplingRate, audio_stream_type_t streamType) {
Eric Laurentc2f1f072009-07-17 12:17:14 -0700429 audio_io_handle_t output;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800430
Dima Zavinfce7a472011-04-19 22:30:36 -0700431 if (streamType == AUDIO_STREAM_DEFAULT) {
432 streamType = AUDIO_STREAM_MUSIC;
Eric Laurentc2f1f072009-07-17 12:17:14 -0700433 }
434
Glenn Kastenfff6d712012-01-12 16:38:12 -0800435 output = getOutput(streamType);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700436 if (output == 0) {
437 return PERMISSION_DENIED;
438 }
439
Jean-Michel Trivib7f24b12014-06-11 10:05:30 -0700440 return getSamplingRate(output, samplingRate);
Eric Laurent1a9ed112012-03-20 18:36:01 -0700441}
442
Glenn Kasten2c073da2016-02-26 09:14:08 -0800443status_t AudioSystem::getSamplingRate(audio_io_handle_t ioHandle,
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800444 uint32_t* samplingRate) {
Andy Hung264fa4c2024-02-21 15:52:12 -0800445 const sp<IAudioFlinger> af = get_audio_flinger();
Eric Laurentf6778fd2014-11-18 17:26:58 -0800446 if (af == 0) return PERMISSION_DENIED;
Glenn Kasten2c073da2016-02-26 09:14:08 -0800447 sp<AudioIoDescriptor> desc = getIoDescriptor(ioHandle);
448 if (desc == 0) {
449 *samplingRate = af->sampleRate(ioHandle);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700450 } else {
Mikhail Naganov88536df2021-07-26 17:30:29 -0700451 *samplingRate = desc->getSamplingRate();
Eric Laurentc2f1f072009-07-17 12:17:14 -0700452 }
Glenn Kastenf94006c2014-01-08 08:56:06 -0800453 if (*samplingRate == 0) {
Glenn Kasten2c073da2016-02-26 09:14:08 -0800454 ALOGE("AudioSystem::getSamplingRate failed for ioHandle %d", ioHandle);
Glenn Kastenf94006c2014-01-08 08:56:06 -0800455 return BAD_VALUE;
456 }
Eric Laurentc2f1f072009-07-17 12:17:14 -0700457
Glenn Kasten2c073da2016-02-26 09:14:08 -0800458 ALOGV("getSamplingRate() ioHandle %d, sampling rate %u", ioHandle, *samplingRate);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700459
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800460 return NO_ERROR;
461}
462
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800463status_t AudioSystem::getOutputFrameCount(size_t* frameCount, audio_stream_type_t streamType) {
Eric Laurentc2f1f072009-07-17 12:17:14 -0700464 audio_io_handle_t output;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800465
Dima Zavinfce7a472011-04-19 22:30:36 -0700466 if (streamType == AUDIO_STREAM_DEFAULT) {
467 streamType = AUDIO_STREAM_MUSIC;
Eric Laurentc2f1f072009-07-17 12:17:14 -0700468 }
Eric Laurent48f7f5e2009-04-02 09:32:43 -0700469
Glenn Kastenfff6d712012-01-12 16:38:12 -0800470 output = getOutput(streamType);
Glenn Kasten142f5192014-03-25 17:44:59 -0700471 if (output == AUDIO_IO_HANDLE_NONE) {
Eric Laurentc2f1f072009-07-17 12:17:14 -0700472 return PERMISSION_DENIED;
473 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800474
Jean-Michel Trivib7f24b12014-06-11 10:05:30 -0700475 return getFrameCount(output, frameCount);
Eric Laurent1a9ed112012-03-20 18:36:01 -0700476}
477
Glenn Kasten2c073da2016-02-26 09:14:08 -0800478status_t AudioSystem::getFrameCount(audio_io_handle_t ioHandle,
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800479 size_t* frameCount) {
Andy Hung264fa4c2024-02-21 15:52:12 -0800480 const sp<IAudioFlinger> af = get_audio_flinger();
Eric Laurentf6778fd2014-11-18 17:26:58 -0800481 if (af == 0) return PERMISSION_DENIED;
Glenn Kasten2c073da2016-02-26 09:14:08 -0800482 sp<AudioIoDescriptor> desc = getIoDescriptor(ioHandle);
483 if (desc == 0) {
484 *frameCount = af->frameCount(ioHandle);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700485 } else {
Mikhail Naganov88536df2021-07-26 17:30:29 -0700486 *frameCount = desc->getFrameCount();
Eric Laurentc2f1f072009-07-17 12:17:14 -0700487 }
Glenn Kastenf94006c2014-01-08 08:56:06 -0800488 if (*frameCount == 0) {
Glenn Kasten2c073da2016-02-26 09:14:08 -0800489 ALOGE("AudioSystem::getFrameCount failed for ioHandle %d", ioHandle);
Glenn Kastenf94006c2014-01-08 08:56:06 -0800490 return BAD_VALUE;
491 }
Eric Laurentc2f1f072009-07-17 12:17:14 -0700492
Glenn Kasten2c073da2016-02-26 09:14:08 -0800493 ALOGV("getFrameCount() ioHandle %d, frameCount %zu", ioHandle, *frameCount);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700494
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800495 return NO_ERROR;
496}
497
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800498status_t AudioSystem::getOutputLatency(uint32_t* latency, audio_stream_type_t streamType) {
Eric Laurentc2f1f072009-07-17 12:17:14 -0700499 audio_io_handle_t output;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800500
Dima Zavinfce7a472011-04-19 22:30:36 -0700501 if (streamType == AUDIO_STREAM_DEFAULT) {
502 streamType = AUDIO_STREAM_MUSIC;
Eric Laurentc2f1f072009-07-17 12:17:14 -0700503 }
Eric Laurent48f7f5e2009-04-02 09:32:43 -0700504
Glenn Kastenfff6d712012-01-12 16:38:12 -0800505 output = getOutput(streamType);
Glenn Kasten142f5192014-03-25 17:44:59 -0700506 if (output == AUDIO_IO_HANDLE_NONE) {
Eric Laurentc2f1f072009-07-17 12:17:14 -0700507 return PERMISSION_DENIED;
508 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800509
Glenn Kasten241618f2014-03-25 17:48:57 -0700510 return getLatency(output, latency);
Eric Laurent1a9ed112012-03-20 18:36:01 -0700511}
512
513status_t AudioSystem::getLatency(audio_io_handle_t output,
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800514 uint32_t* latency) {
Andy Hung264fa4c2024-02-21 15:52:12 -0800515 const sp<IAudioFlinger> af = get_audio_flinger();
Eric Laurentf6778fd2014-11-18 17:26:58 -0800516 if (af == 0) return PERMISSION_DENIED;
Eric Laurent296fb132015-05-01 11:38:42 -0700517 sp<AudioIoDescriptor> outputDesc = getIoDescriptor(output);
Eric Laurent73e26b62015-04-27 16:55:58 -0700518 if (outputDesc == 0) {
Eric Laurentc2f1f072009-07-17 12:17:14 -0700519 *latency = af->latency(output);
520 } else {
Mikhail Naganov88536df2021-07-26 17:30:29 -0700521 *latency = outputDesc->getLatency();
Eric Laurentc2f1f072009-07-17 12:17:14 -0700522 }
523
Glenn Kasten241618f2014-03-25 17:48:57 -0700524 ALOGV("getLatency() output %d, latency %d", output, *latency);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700525
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800526 return NO_ERROR;
527}
528
Glenn Kastendd8104c2012-07-02 12:42:44 -0700529status_t AudioSystem::getInputBufferSize(uint32_t sampleRate, audio_format_t format,
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800530 audio_channel_mask_t channelMask, size_t* buffSize) {
Eric Laurent296fb132015-05-01 11:38:42 -0700531 const sp<AudioFlingerClient> afc = getAudioFlingerClient();
532 if (afc == 0) {
533 return NO_INIT;
534 }
535 return afc->getInputBufferSize(sampleRate, format, channelMask, buffSize);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800536}
537
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800538status_t AudioSystem::setVoiceVolume(float value) {
Andy Hung264fa4c2024-02-21 15:52:12 -0800539 const sp<IAudioFlinger> af = get_audio_flinger();
Eric Laurentf0ee6f42009-10-21 08:14:22 -0700540 if (af == 0) return PERMISSION_DENIED;
541 return af->setVoiceVolume(value);
542}
543
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800544status_t AudioSystem::getRenderPosition(audio_io_handle_t output, uint32_t* halFrames,
545 uint32_t* dspFrames) {
Andy Hung264fa4c2024-02-21 15:52:12 -0800546 const sp<IAudioFlinger> af = get_audio_flinger();
Eric Laurent342e9cf2010-01-19 17:37:09 -0800547 if (af == 0) return PERMISSION_DENIED;
548
Richard Fitzgeraldad3af332013-03-25 16:54:37 +0000549 return af->getRenderPosition(halFrames, dspFrames, output);
Eric Laurent342e9cf2010-01-19 17:37:09 -0800550}
551
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800552uint32_t AudioSystem::getInputFramesLost(audio_io_handle_t ioHandle) {
Andy Hung264fa4c2024-02-21 15:52:12 -0800553 const sp<IAudioFlinger> af = get_audio_flinger();
Glenn Kasten5f972c02014-01-13 09:59:31 -0800554 uint32_t result = 0;
Eric Laurent05bca2f2010-02-26 02:47:27 -0800555 if (af == 0) return result;
Glenn Kasten142f5192014-03-25 17:44:59 -0700556 if (ioHandle == AUDIO_IO_HANDLE_NONE) return result;
Eric Laurent05bca2f2010-02-26 02:47:27 -0800557
558 result = af->getInputFramesLost(ioHandle);
559 return result;
560}
561
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800562audio_unique_id_t AudioSystem::newAudioUniqueId(audio_unique_id_use_t use) {
Mikhail Naganov2996f672019-04-18 12:29:59 -0700563 // Must not use AF as IDs will re-roll on audioserver restart, b/130369529.
Andy Hung264fa4c2024-02-21 15:52:12 -0800564 const sp<IAudioFlinger> af = get_audio_flinger();
Eric Laurentde3f8392014-07-27 18:38:22 -0700565 if (af == 0) return AUDIO_UNIQUE_ID_ALLOCATE;
Glenn Kasteneeecb982016-02-26 10:44:04 -0800566 return af->newAudioUniqueId(use);
Eric Laurentbe916aa2010-06-01 23:49:17 -0700567}
568
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800569void AudioSystem::acquireAudioSessionId(audio_session_t audioSession, pid_t pid, uid_t uid) {
Andy Hung264fa4c2024-02-21 15:52:12 -0800570 const sp<IAudioFlinger> af = get_audio_flinger();
Marco Nelissen3a34bef2011-08-02 13:33:41 -0700571 if (af != 0) {
Andy Hung8b0bfd92019-12-23 13:11:11 -0800572 af->acquireAudioSessionId(audioSession, pid, uid);
Marco Nelissen3a34bef2011-08-02 13:33:41 -0700573 }
574}
575
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800576void AudioSystem::releaseAudioSessionId(audio_session_t audioSession, pid_t pid) {
Andy Hung264fa4c2024-02-21 15:52:12 -0800577 const sp<IAudioFlinger> af = get_audio_flinger();
Marco Nelissen3a34bef2011-08-02 13:33:41 -0700578 if (af != 0) {
Marco Nelissend457c972014-02-11 08:47:07 -0800579 af->releaseAudioSessionId(audioSession, pid);
Marco Nelissen3a34bef2011-08-02 13:33:41 -0700580 }
581}
582
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800583audio_hw_sync_t AudioSystem::getAudioHwSyncForSession(audio_session_t sessionId) {
Andy Hung264fa4c2024-02-21 15:52:12 -0800584 const sp<IAudioFlinger> af = get_audio_flinger();
Eric Laurent93c3d412014-08-01 14:48:35 -0700585 if (af == 0) return AUDIO_HW_SYNC_INVALID;
586 return af->getAudioHwSyncForSession(sessionId);
587}
588
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800589status_t AudioSystem::systemReady() {
Andy Hung264fa4c2024-02-21 15:52:12 -0800590 const sp<IAudioFlinger> af = get_audio_flinger();
Eric Laurent72e3f392015-05-20 14:43:50 -0700591 if (af == 0) return NO_INIT;
592 return af->systemReady();
593}
594
Eric Laurentd66d7a12021-07-13 13:35:32 +0200595status_t AudioSystem::audioPolicyReady() {
Andy Hung264fa4c2024-02-21 15:52:12 -0800596 const sp<IAudioFlinger> af = get_audio_flinger();
Eric Laurentd66d7a12021-07-13 13:35:32 +0200597 if (af == 0) return NO_INIT;
598 return af->audioPolicyReady();
599}
600
Glenn Kasten4a8308b2016-04-18 14:10:01 -0700601status_t AudioSystem::getFrameCountHAL(audio_io_handle_t ioHandle,
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800602 size_t* frameCount) {
Andy Hung264fa4c2024-02-21 15:52:12 -0800603 const sp<IAudioFlinger> af = get_audio_flinger();
Glenn Kasten4a8308b2016-04-18 14:10:01 -0700604 if (af == 0) return PERMISSION_DENIED;
605 sp<AudioIoDescriptor> desc = getIoDescriptor(ioHandle);
606 if (desc == 0) {
607 *frameCount = af->frameCountHAL(ioHandle);
608 } else {
Mikhail Naganov88536df2021-07-26 17:30:29 -0700609 *frameCount = desc->getFrameCountHAL();
Glenn Kasten4a8308b2016-04-18 14:10:01 -0700610 }
611 if (*frameCount == 0) {
612 ALOGE("AudioSystem::getFrameCountHAL failed for ioHandle %d", ioHandle);
613 return BAD_VALUE;
614 }
615
616 ALOGV("getFrameCountHAL() ioHandle %d, frameCount %zu", ioHandle, *frameCount);
617
618 return NO_ERROR;
619}
620
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800621// ---------------------------------------------------------------------------
622
Eric Laurent73e26b62015-04-27 16:55:58 -0700623
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800624void AudioSystem::AudioFlingerClient::clearIoCache() {
Andy Hungacd955a2024-02-16 17:14:36 -0800625 std::lock_guard _l(mMutex);
Eric Laurent73e26b62015-04-27 16:55:58 -0700626 mIoDescriptors.clear();
627 mInBuffSize = 0;
628 mInSamplingRate = 0;
629 mInFormat = AUDIO_FORMAT_DEFAULT;
630 mInChannelMask = AUDIO_CHANNEL_NONE;
631}
632
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800633void AudioSystem::AudioFlingerClient::binderDied(const wp<IBinder>& who __unused) {
Andy Hungda1fb072024-02-20 19:08:08 -0800634 gAudioFlingerServiceHandler.clearService();
Ytai Ben-Tsvi000c3e42020-01-09 15:26:40 -0800635 reportError(DEAD_OBJECT);
636
Steve Block5ff1dd52012-01-05 23:22:43 +0000637 ALOGW("AudioFlinger server died!");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800638}
639
Ytai Ben-Tsvi10dc0a62020-09-18 11:31:55 -0700640Status AudioSystem::AudioFlingerClient::ioConfigChanged(
641 media::AudioIoConfigEvent _event,
642 const media::AudioIoDescriptor& _ioDesc) {
Mikhail Naganov88536df2021-07-26 17:30:29 -0700643 audio_io_config_event_t event = VALUE_OR_RETURN_BINDER_STATUS(
644 aidl2legacy_AudioIoConfigEvent_audio_io_config_event_t(_event));
Ytai Ben-Tsvi10dc0a62020-09-18 11:31:55 -0700645 sp<AudioIoDescriptor> ioDesc(
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800646 VALUE_OR_RETURN_BINDER_STATUS(
647 aidl2legacy_AudioIoDescriptor_AudioIoDescriptor(_ioDesc)));
Ytai Ben-Tsvi10dc0a62020-09-18 11:31:55 -0700648
Steve Block3856b092011-10-20 11:56:00 +0100649 ALOGV("ioConfigChanged() event %d", event);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700650
Mikhail Naganov88536df2021-07-26 17:30:29 -0700651 if (ioDesc->getIoHandle() == AUDIO_IO_HANDLE_NONE) return Status::ok();
Eric Laurentc2f1f072009-07-17 12:17:14 -0700652
Eric Laurent296fb132015-05-01 11:38:42 -0700653 audio_port_handle_t deviceId = AUDIO_PORT_HANDLE_NONE;
Eric Laurent09f1ed22019-04-24 17:45:17 -0700654 std::vector<sp<AudioDeviceCallback>> callbacksToCall;
Eric Laurent296fb132015-05-01 11:38:42 -0700655 {
Andy Hungacd955a2024-02-16 17:14:36 -0800656 std::lock_guard _l(mMutex);
Eric Laurent09f1ed22019-04-24 17:45:17 -0700657 auto callbacks = std::map<audio_port_handle_t, wp<AudioDeviceCallback>>();
Eric Laurent296fb132015-05-01 11:38:42 -0700658
659 switch (event) {
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800660 case AUDIO_OUTPUT_OPENED:
661 case AUDIO_OUTPUT_REGISTERED:
662 case AUDIO_INPUT_OPENED:
663 case AUDIO_INPUT_REGISTERED: {
Andy Hungd211ce02024-02-16 15:34:07 -0800664 if (sp<AudioIoDescriptor> oldDesc = getIoDescriptor_l(ioDesc->getIoHandle())) {
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800665 deviceId = oldDesc->getDeviceId();
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800666 }
Andy Hungd211ce02024-02-16 15:34:07 -0800667 mIoDescriptors[ioDesc->getIoHandle()] = ioDesc;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800668
669 if (ioDesc->getDeviceId() != AUDIO_PORT_HANDLE_NONE) {
670 deviceId = ioDesc->getDeviceId();
671 if (event == AUDIO_OUTPUT_OPENED || event == AUDIO_INPUT_OPENED) {
Mikhail Naganov88536df2021-07-26 17:30:29 -0700672 auto it = mAudioDeviceCallbacks.find(ioDesc->getIoHandle());
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800673 if (it != mAudioDeviceCallbacks.end()) {
674 callbacks = it->second;
675 }
676 }
677 }
Mikhail Naganov88536df2021-07-26 17:30:29 -0700678 ALOGV("ioConfigChanged() new %s %s %s",
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800679 event == AUDIO_OUTPUT_OPENED || event == AUDIO_OUTPUT_REGISTERED ?
680 "output" : "input",
681 event == AUDIO_OUTPUT_OPENED || event == AUDIO_INPUT_OPENED ?
682 "opened" : "registered",
Mikhail Naganov88536df2021-07-26 17:30:29 -0700683 ioDesc->toDebugString().c_str());
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800684 }
685 break;
686 case AUDIO_OUTPUT_CLOSED:
687 case AUDIO_INPUT_CLOSED: {
Mikhail Naganov88536df2021-07-26 17:30:29 -0700688 if (getIoDescriptor_l(ioDesc->getIoHandle()) == 0) {
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800689 ALOGW("ioConfigChanged() closing unknown %s %d",
Mikhail Naganov88536df2021-07-26 17:30:29 -0700690 event == AUDIO_OUTPUT_CLOSED ? "output" : "input", ioDesc->getIoHandle());
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800691 break;
692 }
693 ALOGV("ioConfigChanged() %s %d closed",
Mikhail Naganov88536df2021-07-26 17:30:29 -0700694 event == AUDIO_OUTPUT_CLOSED ? "output" : "input", ioDesc->getIoHandle());
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800695
Andy Hungd211ce02024-02-16 15:34:07 -0800696 mIoDescriptors.erase(ioDesc->getIoHandle());
Mikhail Naganov88536df2021-07-26 17:30:29 -0700697 mAudioDeviceCallbacks.erase(ioDesc->getIoHandle());
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800698 }
699 break;
700
701 case AUDIO_OUTPUT_CONFIG_CHANGED:
702 case AUDIO_INPUT_CONFIG_CHANGED: {
Mikhail Naganov88536df2021-07-26 17:30:29 -0700703 sp<AudioIoDescriptor> oldDesc = getIoDescriptor_l(ioDesc->getIoHandle());
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800704 if (oldDesc == 0) {
705 ALOGW("ioConfigChanged() modifying unknown %s! %d",
706 event == AUDIO_OUTPUT_CONFIG_CHANGED ? "output" : "input",
Mikhail Naganov88536df2021-07-26 17:30:29 -0700707 ioDesc->getIoHandle());
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800708 break;
709 }
710
Eric Laurent7c1ec5f2015-07-09 14:52:47 -0700711 deviceId = oldDesc->getDeviceId();
Andy Hungd211ce02024-02-16 15:34:07 -0800712 mIoDescriptors[ioDesc->getIoHandle()] = ioDesc;
Eric Laurent296fb132015-05-01 11:38:42 -0700713
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800714 if (deviceId != ioDesc->getDeviceId()) {
715 deviceId = ioDesc->getDeviceId();
Mikhail Naganov88536df2021-07-26 17:30:29 -0700716 auto it = mAudioDeviceCallbacks.find(ioDesc->getIoHandle());
Eric Laurent09f1ed22019-04-24 17:45:17 -0700717 if (it != mAudioDeviceCallbacks.end()) {
718 callbacks = it->second;
Francois Gaffie24a9fb02019-01-18 17:51:34 +0100719 }
720 }
Mikhail Naganov88536df2021-07-26 17:30:29 -0700721 ALOGV("ioConfigChanged() new config for %s %s",
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800722 event == AUDIO_OUTPUT_CONFIG_CHANGED ? "output" : "input",
Mikhail Naganov88536df2021-07-26 17:30:29 -0700723 ioDesc->toDebugString().c_str());
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800724
Eric Laurent296fb132015-05-01 11:38:42 -0700725 }
Eric Laurent296fb132015-05-01 11:38:42 -0700726 break;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800727 case AUDIO_CLIENT_STARTED: {
Mikhail Naganov88536df2021-07-26 17:30:29 -0700728 sp<AudioIoDescriptor> oldDesc = getIoDescriptor_l(ioDesc->getIoHandle());
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800729 if (oldDesc == 0) {
Mikhail Naganov88536df2021-07-26 17:30:29 -0700730 ALOGW("ioConfigChanged() start client on unknown io! %d",
731 ioDesc->getIoHandle());
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800732 break;
733 }
734 ALOGV("ioConfigChanged() AUDIO_CLIENT_STARTED io %d port %d num callbacks %zu",
Mikhail Naganov88536df2021-07-26 17:30:29 -0700735 ioDesc->getIoHandle(), ioDesc->getPortId(), mAudioDeviceCallbacks.size());
736 oldDesc->setPatch(ioDesc->getPatch());
737 auto it = mAudioDeviceCallbacks.find(ioDesc->getIoHandle());
Eric Laurent09f1ed22019-04-24 17:45:17 -0700738 if (it != mAudioDeviceCallbacks.end()) {
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800739 auto cbks = it->second;
Mikhail Naganov88536df2021-07-26 17:30:29 -0700740 auto it2 = cbks.find(ioDesc->getPortId());
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800741 if (it2 != cbks.end()) {
Mikhail Naganov88536df2021-07-26 17:30:29 -0700742 callbacks.emplace(ioDesc->getPortId(), it2->second);
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800743 deviceId = oldDesc->getDeviceId();
744 }
Eric Laurent09f1ed22019-04-24 17:45:17 -0700745 }
Eric Laurent296fb132015-05-01 11:38:42 -0700746 }
Eric Laurent09f1ed22019-04-24 17:45:17 -0700747 break;
Eric Laurent09f1ed22019-04-24 17:45:17 -0700748 }
749
750 for (auto wpCbk : callbacks) {
751 sp<AudioDeviceCallback> spCbk = wpCbk.second.promote();
752 if (spCbk != nullptr) {
753 callbacksToCall.push_back(spCbk);
754 }
Eric Laurentad2e7b92017-09-14 20:06:42 -0700755 }
Eric Laurent4463ff52019-02-07 13:56:09 -0800756 }
757
Andy Hungacd955a2024-02-16 17:14:36 -0800758 // Callbacks must be called without mMutex held. May lead to dead lock if calling for
759 // example getRoutedDevice that updates the device and tries to acquire mMutex.
Eric Laurent09f1ed22019-04-24 17:45:17 -0700760 for (auto cb : callbacksToCall) {
Mikhail Naganov88536df2021-07-26 17:30:29 -0700761 // If callbacksToCall is not empty, it implies ioDesc->getIoHandle() and deviceId are valid
762 cb->onAudioDeviceUpdate(ioDesc->getIoHandle(), deviceId);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700763 }
Ytai Ben-Tsvi10dc0a62020-09-18 11:31:55 -0700764
765 return Status::ok();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800766}
767
Eric Laurent076e7c72022-05-03 18:12:28 +0200768Status AudioSystem::AudioFlingerClient::onSupportedLatencyModesChanged(
Mikhail Naganovf53e1822022-12-18 02:48:14 +0000769 int output, const std::vector<media::audio::common::AudioLatencyMode>& latencyModes) {
Eric Laurent076e7c72022-05-03 18:12:28 +0200770 audio_io_handle_t outputLegacy = VALUE_OR_RETURN_BINDER_STATUS(
771 aidl2legacy_int32_t_audio_io_handle_t(output));
772 std::vector<audio_latency_mode_t> modesLegacy = VALUE_OR_RETURN_BINDER_STATUS(
773 convertContainer<std::vector<audio_latency_mode_t>>(
Mikhail Naganovf53e1822022-12-18 02:48:14 +0000774 latencyModes, aidl2legacy_AudioLatencyMode_audio_latency_mode_t));
Eric Laurent076e7c72022-05-03 18:12:28 +0200775
776 std::vector<sp<SupportedLatencyModesCallback>> callbacks;
777 {
Andy Hungacd955a2024-02-16 17:14:36 -0800778 std::lock_guard _l(mMutex);
Eric Laurent076e7c72022-05-03 18:12:28 +0200779 for (auto callback : mSupportedLatencyModesCallbacks) {
780 if (auto ref = callback.promote(); ref != nullptr) {
781 callbacks.push_back(ref);
782 }
783 }
784 }
785 for (const auto& callback : callbacks) {
786 callback->onSupportedLatencyModesChanged(outputLegacy, modesLegacy);
787 }
788
789 return Status::ok();
790}
791
Eric Laurent73e26b62015-04-27 16:55:58 -0700792status_t AudioSystem::AudioFlingerClient::getInputBufferSize(
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800793 uint32_t sampleRate, audio_format_t format,
794 audio_channel_mask_t channelMask, size_t* buffSize) {
Andy Hung264fa4c2024-02-21 15:52:12 -0800795 const sp<IAudioFlinger> af = get_audio_flinger();
Eric Laurent73e26b62015-04-27 16:55:58 -0700796 if (af == 0) {
797 return PERMISSION_DENIED;
798 }
Andy Hungacd955a2024-02-16 17:14:36 -0800799 std::lock_guard _l(mMutex);
Eric Laurent73e26b62015-04-27 16:55:58 -0700800 // Do we have a stale mInBuffSize or are we requesting the input buffer size for new values
801 if ((mInBuffSize == 0) || (sampleRate != mInSamplingRate) || (format != mInFormat)
802 || (channelMask != mInChannelMask)) {
803 size_t inBuffSize = af->getInputBufferSize(sampleRate, format, channelMask);
804 if (inBuffSize == 0) {
Glenn Kasten49f36ba2017-12-06 13:02:02 -0800805 ALOGE("AudioSystem::getInputBufferSize failed sampleRate %d format %#x channelMask %#x",
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800806 sampleRate, format, channelMask);
Eric Laurent73e26b62015-04-27 16:55:58 -0700807 return BAD_VALUE;
808 }
809 // A benign race is possible here: we could overwrite a fresher cache entry
810 // save the request params
811 mInSamplingRate = sampleRate;
812 mInFormat = format;
813 mInChannelMask = channelMask;
814
815 mInBuffSize = inBuffSize;
816 }
817
818 *buffSize = mInBuffSize;
819
820 return NO_ERROR;
821}
822
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800823sp<AudioIoDescriptor>
824AudioSystem::AudioFlingerClient::getIoDescriptor_l(audio_io_handle_t ioHandle) {
Andy Hungd211ce02024-02-16 15:34:07 -0800825 if (const auto it = mIoDescriptors.find(ioHandle);
826 it != mIoDescriptors.end()) {
827 return it->second;
Eric Laurent73e26b62015-04-27 16:55:58 -0700828 }
Andy Hungd211ce02024-02-16 15:34:07 -0800829 return {};
Eric Laurent73e26b62015-04-27 16:55:58 -0700830}
831
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800832sp<AudioIoDescriptor> AudioSystem::AudioFlingerClient::getIoDescriptor(audio_io_handle_t ioHandle) {
Andy Hungacd955a2024-02-16 17:14:36 -0800833 std::lock_guard _l(mMutex);
Praveen Chavan49fdeaf2015-09-29 02:25:47 -0700834 return getIoDescriptor_l(ioHandle);
835}
836
Eric Laurent296fb132015-05-01 11:38:42 -0700837status_t AudioSystem::AudioFlingerClient::addAudioDeviceCallback(
Eric Laurent09f1ed22019-04-24 17:45:17 -0700838 const wp<AudioDeviceCallback>& callback, audio_io_handle_t audioIo,
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800839 audio_port_handle_t portId) {
Eric Laurent09f1ed22019-04-24 17:45:17 -0700840 ALOGV("%s audioIo %d portId %d", __func__, audioIo, portId);
Andy Hungacd955a2024-02-16 17:14:36 -0800841 std::lock_guard _l(mMutex);
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800842 auto& callbacks = mAudioDeviceCallbacks.emplace(
843 audioIo,
844 std::map<audio_port_handle_t, wp<AudioDeviceCallback>>()).first->second;
Eric Laurent09f1ed22019-04-24 17:45:17 -0700845 auto result = callbacks.try_emplace(portId, callback);
846 if (!result.second) {
847 return INVALID_OPERATION;
Eric Laurent296fb132015-05-01 11:38:42 -0700848 }
Eric Laurent296fb132015-05-01 11:38:42 -0700849 return NO_ERROR;
850}
851
852status_t AudioSystem::AudioFlingerClient::removeAudioDeviceCallback(
Eric Laurent09f1ed22019-04-24 17:45:17 -0700853 const wp<AudioDeviceCallback>& callback __unused, audio_io_handle_t audioIo,
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800854 audio_port_handle_t portId) {
Eric Laurent09f1ed22019-04-24 17:45:17 -0700855 ALOGV("%s audioIo %d portId %d", __func__, audioIo, portId);
Andy Hungacd955a2024-02-16 17:14:36 -0800856 std::lock_guard _l(mMutex);
Eric Laurent09f1ed22019-04-24 17:45:17 -0700857 auto it = mAudioDeviceCallbacks.find(audioIo);
858 if (it == mAudioDeviceCallbacks.end()) {
Eric Laurent296fb132015-05-01 11:38:42 -0700859 return INVALID_OPERATION;
860 }
Eric Laurent09f1ed22019-04-24 17:45:17 -0700861 if (it->second.erase(portId) == 0) {
Eric Laurent296fb132015-05-01 11:38:42 -0700862 return INVALID_OPERATION;
863 }
Eric Laurent09f1ed22019-04-24 17:45:17 -0700864 if (it->second.size() == 0) {
865 mAudioDeviceCallbacks.erase(audioIo);
Eric Laurent296fb132015-05-01 11:38:42 -0700866 }
867 return NO_ERROR;
868}
869
Eric Laurent076e7c72022-05-03 18:12:28 +0200870status_t AudioSystem::AudioFlingerClient::addSupportedLatencyModesCallback(
871 const sp<SupportedLatencyModesCallback>& callback) {
Andy Hungacd955a2024-02-16 17:14:36 -0800872 std::lock_guard _l(mMutex);
Eric Laurent076e7c72022-05-03 18:12:28 +0200873 if (std::find(mSupportedLatencyModesCallbacks.begin(),
874 mSupportedLatencyModesCallbacks.end(),
875 callback) != mSupportedLatencyModesCallbacks.end()) {
876 return INVALID_OPERATION;
877 }
878 mSupportedLatencyModesCallbacks.push_back(callback);
879 return NO_ERROR;
880}
881
882status_t AudioSystem::AudioFlingerClient::removeSupportedLatencyModesCallback(
883 const sp<SupportedLatencyModesCallback>& callback) {
Andy Hungacd955a2024-02-16 17:14:36 -0800884 std::lock_guard _l(mMutex);
Eric Laurent076e7c72022-05-03 18:12:28 +0200885 auto it = std::find(mSupportedLatencyModesCallbacks.begin(),
886 mSupportedLatencyModesCallbacks.end(),
887 callback);
888 if (it == mSupportedLatencyModesCallbacks.end()) {
889 return INVALID_OPERATION;
890 }
891 mSupportedLatencyModesCallbacks.erase(it);
892 return NO_ERROR;
893}
894
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800895/* static */ uintptr_t AudioSystem::addErrorCallback(audio_error_callback cb) {
Andy Hungacd955a2024-02-16 17:14:36 -0800896 std::lock_guard _l(gErrorCallbacksMutex);
Ytai Ben-Tsvi000c3e42020-01-09 15:26:40 -0800897 gAudioErrorCallbacks.insert(cb);
898 return reinterpret_cast<uintptr_t>(cb);
899}
900
901/* static */ void AudioSystem::removeErrorCallback(uintptr_t cb) {
Andy Hungacd955a2024-02-16 17:14:36 -0800902 std::lock_guard _l(gErrorCallbacksMutex);
Ytai Ben-Tsvi000c3e42020-01-09 15:26:40 -0800903 gAudioErrorCallbacks.erase(reinterpret_cast<audio_error_callback>(cb));
904}
905
906/* static */ void AudioSystem::reportError(status_t err) {
Andy Hungacd955a2024-02-16 17:14:36 -0800907 std::lock_guard _l(gErrorCallbacksMutex);
Ytai Ben-Tsvi000c3e42020-01-09 15:26:40 -0800908 for (auto callback : gAudioErrorCallbacks) {
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800909 callback(err);
Ytai Ben-Tsvi000c3e42020-01-09 15:26:40 -0800910 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800911}
912
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800913/*static*/ void AudioSystem::setDynPolicyCallback(dynamic_policy_callback cb) {
Andy Hungacd955a2024-02-16 17:14:36 -0800914 std::lock_guard _l(gMutex);
Jean-Michel Trivif613d422015-04-23 18:41:29 -0700915 gDynPolicyCallback = cb;
916}
917
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800918/*static*/ void AudioSystem::setRecordConfigCallback(record_config_callback cb) {
Andy Hungacd955a2024-02-16 17:14:36 -0800919 std::lock_guard _l(gMutex);
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -0800920 gRecordConfigCallback = cb;
921}
922
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800923/*static*/ void AudioSystem::setRoutingCallback(routing_callback cb) {
Andy Hungacd955a2024-02-16 17:14:36 -0800924 std::lock_guard _l(gMutex);
Jean-Michel Trivi9a6b9ad2020-10-22 16:46:43 -0700925 gRoutingCallback = cb;
926}
927
Jean-Michel Trivi78f2b302022-04-15 18:18:41 +0000928/*static*/ void AudioSystem::setVolInitReqCallback(vol_range_init_req_callback cb) {
Andy Hungacd955a2024-02-16 17:14:36 -0800929 std::lock_guard _l(gMutex);
Jean-Michel Trivi78f2b302022-04-15 18:18:41 +0000930 gVolRangeInitReqCallback = cb;
931}
932
Andy Hung88e71732024-02-21 16:44:27 -0800933struct AudioPolicyTraits {
934 static void onServiceCreate(const sp<IAudioPolicyService>& ap,
935 const sp<AudioSystem::AudioPolicyServiceClient>& apc) {
936 const int64_t token = IPCThreadState::self()->clearCallingIdentity();
Eric Laurent0ebd5f92014-11-19 19:04:52 -0800937 ap->registerClient(apc);
François Gaffie24437602018-04-23 15:08:59 +0200938 ap->setAudioPortCallbacksEnabled(apc->isAudioPortCbEnabled());
François Gaffiecfe17322018-11-07 13:41:29 +0100939 ap->setAudioVolumeGroupCallbacksEnabled(apc->isAudioVolumeGroupCbEnabled());
François Gaffie24437602018-04-23 15:08:59 +0200940 IPCThreadState::self()->restoreCallingIdentity(token);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700941 }
Glenn Kastend2d089f2014-11-05 11:48:12 -0800942
Andy Hung88e71732024-02-21 16:44:27 -0800943 static void onClearService(const sp<AudioSystem::AudioPolicyServiceClient>&) {}
944
945 static constexpr const char *SERVICE_NAME = "media.audio_policy";
946};
947
948[[clang::no_destroy]] static constinit ServiceHandler<IAudioPolicyService,
949 AudioSystem::AudioPolicyServiceClient, IAudioPolicyService,
950 AudioPolicyTraits> gAudioPolicyServiceHandler;
951
952status_t AudioSystem::setLocalAudioPolicyService(const sp<IAudioPolicyService>& aps) {
953 return gAudioPolicyServiceHandler.setLocalService(aps);
954}
955
956sp<IAudioPolicyService> AudioSystem::get_audio_policy_service() {
957 return gAudioPolicyServiceHandler.getService();
Eric Laurentc2f1f072009-07-17 12:17:14 -0700958}
959
Mikhail Naganovec3d5792022-05-06 00:19:55 +0000960void AudioSystem::clearAudioPolicyService() {
Andy Hung88e71732024-02-21 16:44:27 -0800961 gAudioPolicyServiceHandler.clearService();
Mikhail Naganovec3d5792022-05-06 00:19:55 +0000962}
963
Shraddha Basantwanif2aa2502024-04-08 11:01:57 +0000964void AudioSystem::disableThreadPool() {
965 gAudioFlingerServiceHandler.disableThreadPool();
966 gAudioPolicyServiceHandler.disableThreadPool();
967}
968
Glenn Kastenfb1fdc92013-07-10 17:03:19 -0700969// ---------------------------------------------------------------------------
970
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800971void AudioSystem::onNewAudioModulesAvailable() {
Andy Hung264fa4c2024-02-21 15:52:12 -0800972 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Mikhail Naganov88b30d22020-03-09 19:43:13 +0000973 if (aps == 0) return;
974 aps->onNewAudioModulesAvailable();
975}
976
Nathalie Le Clair88fa2752021-11-23 13:03:41 +0100977status_t AudioSystem::setDeviceConnectionState(audio_policy_dev_state_t state,
978 const android::media::audio::common::AudioPort& port,
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800979 audio_format_t encodedFormat) {
Andy Hung264fa4c2024-02-21 15:52:12 -0800980 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Eric Laurent71b63e32011-09-02 14:20:56 -0700981
Eric Laurentc2f1f072009-07-17 12:17:14 -0700982 if (aps == 0) return PERMISSION_DENIED;
983
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800984 return statusTFromBinderStatus(
985 aps->setDeviceConnectionState(
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800986 VALUE_OR_RETURN_STATUS(
987 legacy2aidl_audio_policy_dev_state_t_AudioPolicyDeviceState(state)),
Nathalie Le Clair88fa2752021-11-23 13:03:41 +0100988 port,
Mikhail Naganovb60bd1b2021-07-15 17:31:43 -0700989 VALUE_OR_RETURN_STATUS(
990 legacy2aidl_audio_format_t_AudioFormatDescription(encodedFormat))));
Eric Laurentc2f1f072009-07-17 12:17:14 -0700991}
992
Dima Zavinfce7a472011-04-19 22:30:36 -0700993audio_policy_dev_state_t AudioSystem::getDeviceConnectionState(audio_devices_t device,
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800994 const char* device_address) {
Andy Hung264fa4c2024-02-21 15:52:12 -0800995 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Dima Zavinfce7a472011-04-19 22:30:36 -0700996 if (aps == 0) return AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE;
Eric Laurentc2f1f072009-07-17 12:17:14 -0700997
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800998 auto result = [&]() -> ConversionResult<audio_policy_dev_state_t> {
Mikhail Naganov932cb962021-09-16 01:05:27 +0000999 AudioDevice deviceAidl = VALUE_OR_RETURN(
1000 legacy2aidl_audio_device_AudioDevice(device, device_address));
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001001
1002 media::AudioPolicyDeviceState result;
1003 RETURN_IF_ERROR(statusTFromBinderStatus(
1004 aps->getDeviceConnectionState(deviceAidl, &result)));
1005
1006 return aidl2legacy_AudioPolicyDeviceState_audio_policy_dev_state_t(result);
1007 }();
1008 return result.value_or(AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE);
Eric Laurentc2f1f072009-07-17 12:17:14 -07001009}
1010
Pavlin Radoslavovf862bc62016-12-26 18:57:22 -08001011status_t AudioSystem::handleDeviceConfigChange(audio_devices_t device,
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001012 const char* device_address,
1013 const char* device_name,
1014 audio_format_t encodedFormat) {
Andy Hung264fa4c2024-02-21 15:52:12 -08001015 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001016 const char* address = "";
1017 const char* name = "";
Pavlin Radoslavovf862bc62016-12-26 18:57:22 -08001018
1019 if (aps == 0) return PERMISSION_DENIED;
1020
1021 if (device_address != NULL) {
1022 address = device_address;
1023 }
1024 if (device_name != NULL) {
1025 name = device_name;
1026 }
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001027
Mikhail Naganov932cb962021-09-16 01:05:27 +00001028 AudioDevice deviceAidl = VALUE_OR_RETURN_STATUS(
1029 legacy2aidl_audio_device_AudioDevice(device, address));
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001030
1031 return statusTFromBinderStatus(
1032 aps->handleDeviceConfigChange(deviceAidl, name, VALUE_OR_RETURN_STATUS(
Mikhail Naganovb60bd1b2021-07-15 17:31:43 -07001033 legacy2aidl_audio_format_t_AudioFormatDescription(encodedFormat))));
Pavlin Radoslavovf862bc62016-12-26 18:57:22 -08001034}
1035
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001036status_t AudioSystem::setPhoneState(audio_mode_t state, uid_t uid) {
Glenn Kasten347966c2012-01-18 14:58:32 -08001037 if (uint32_t(state) >= AUDIO_MODE_CNT) return BAD_VALUE;
Andy Hung264fa4c2024-02-21 15:52:12 -08001038 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Eric Laurentc2f1f072009-07-17 12:17:14 -07001039 if (aps == 0) return PERMISSION_DENIED;
1040
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001041 return statusTFromBinderStatus(aps->setPhoneState(
1042 VALUE_OR_RETURN_STATUS(legacy2aidl_audio_mode_t_AudioMode(state)),
1043 VALUE_OR_RETURN_STATUS(legacy2aidl_uid_t_int32_t(uid))));
Eric Laurentc2f1f072009-07-17 12:17:14 -07001044}
1045
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001046status_t
1047AudioSystem::setForceUse(audio_policy_force_use_t usage, audio_policy_forced_cfg_t config) {
Andy Hung264fa4c2024-02-21 15:52:12 -08001048 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Eric Laurentc2f1f072009-07-17 12:17:14 -07001049 if (aps == 0) return PERMISSION_DENIED;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001050
1051 return statusTFromBinderStatus(
1052 aps->setForceUse(
1053 VALUE_OR_RETURN_STATUS(
1054 legacy2aidl_audio_policy_force_use_t_AudioPolicyForceUse(usage)),
1055 VALUE_OR_RETURN_STATUS(
1056 legacy2aidl_audio_policy_forced_cfg_t_AudioPolicyForcedConfig(
1057 config))));
Eric Laurentc2f1f072009-07-17 12:17:14 -07001058}
1059
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001060audio_policy_forced_cfg_t AudioSystem::getForceUse(audio_policy_force_use_t usage) {
Andy Hung264fa4c2024-02-21 15:52:12 -08001061 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Dima Zavinfce7a472011-04-19 22:30:36 -07001062 if (aps == 0) return AUDIO_POLICY_FORCE_NONE;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001063
1064 auto result = [&]() -> ConversionResult<audio_policy_forced_cfg_t> {
1065 media::AudioPolicyForceUse usageAidl = VALUE_OR_RETURN(
1066 legacy2aidl_audio_policy_force_use_t_AudioPolicyForceUse(usage));
1067 media::AudioPolicyForcedConfig configAidl;
1068 RETURN_IF_ERROR(statusTFromBinderStatus(
1069 aps->getForceUse(usageAidl, &configAidl)));
1070 return aidl2legacy_AudioPolicyForcedConfig_audio_policy_forced_cfg_t(configAidl);
1071 }();
1072
1073 return result.value_or(AUDIO_POLICY_FORCE_NONE);
Eric Laurentc2f1f072009-07-17 12:17:14 -07001074}
1075
1076
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001077audio_io_handle_t AudioSystem::getOutput(audio_stream_type_t stream) {
Andy Hung264fa4c2024-02-21 15:52:12 -08001078 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001079 if (aps == 0) return AUDIO_IO_HANDLE_NONE;
1080
1081 auto result = [&]() -> ConversionResult<audio_io_handle_t> {
Mikhail Naganovdbf03642021-08-25 18:15:32 -07001082 AudioStreamType streamAidl = VALUE_OR_RETURN(
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001083 legacy2aidl_audio_stream_type_t_AudioStreamType(stream));
1084 int32_t outputAidl;
1085 RETURN_IF_ERROR(
1086 statusTFromBinderStatus(aps->getOutput(streamAidl, &outputAidl)));
1087 return aidl2legacy_int32_t_audio_io_handle_t(outputAidl);
1088 }();
1089
1090 return result.value_or(AUDIO_IO_HANDLE_NONE);
Eric Laurentc2f1f072009-07-17 12:17:14 -07001091}
1092
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001093status_t AudioSystem::getOutputForAttr(audio_attributes_t* attr,
1094 audio_io_handle_t* output,
1095 audio_session_t session,
1096 audio_stream_type_t* stream,
Svet Ganov3e5f14f2021-05-13 22:51:08 +00001097 const AttributionSourceState& attributionSource,
jiabinf1c73972022-04-14 16:28:52 -07001098 audio_config_t* config,
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001099 audio_output_flags_t flags,
1100 audio_port_handle_t* selectedDeviceId,
1101 audio_port_handle_t* portId,
Eric Laurentb0a7bc92022-04-05 15:06:08 +02001102 std::vector<audio_io_handle_t>* secondaryOutputs,
jiabinc658e452022-10-21 20:52:21 +00001103 bool *isSpatialized,
Francois Gaffie55b2a0f2021-06-24 15:58:37 +02001104 bool *isBitPerfect,
1105 float *volume) {
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001106 if (attr == nullptr) {
1107 ALOGE("%s NULL audio attributes", __func__);
1108 return BAD_VALUE;
1109 }
1110 if (output == nullptr) {
1111 ALOGE("%s NULL output - shouldn't happen", __func__);
1112 return BAD_VALUE;
1113 }
1114 if (selectedDeviceId == nullptr) {
1115 ALOGE("%s NULL selectedDeviceId - shouldn't happen", __func__);
1116 return BAD_VALUE;
1117 }
1118 if (portId == nullptr) {
1119 ALOGE("%s NULL portId - shouldn't happen", __func__);
1120 return BAD_VALUE;
1121 }
1122 if (secondaryOutputs == nullptr) {
1123 ALOGE("%s NULL secondaryOutputs - shouldn't happen", __func__);
1124 return BAD_VALUE;
1125 }
1126
Andy Hung264fa4c2024-02-21 15:52:12 -08001127 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Eric Laurente83b55d2014-11-14 10:06:21 -08001128 if (aps == 0) return NO_INIT;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001129
Mikhail Naganov1c400902023-05-17 11:48:43 -07001130 media::audio::common::AudioAttributes attrAidl = VALUE_OR_RETURN_STATUS(
1131 legacy2aidl_audio_attributes_t_AudioAttributes(*attr));
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001132 int32_t sessionAidl = VALUE_OR_RETURN_STATUS(legacy2aidl_audio_session_t_int32_t(session));
Mikhail Naganovdbf03642021-08-25 18:15:32 -07001133 AudioConfig configAidl = VALUE_OR_RETURN_STATUS(
Mikhail Naganovde3fa182021-07-30 15:06:42 -07001134 legacy2aidl_audio_config_t_AudioConfig(*config, false /*isInput*/));
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001135 int32_t flagsAidl = VALUE_OR_RETURN_STATUS(
1136 legacy2aidl_audio_output_flags_t_int32_t_mask(flags));
Eric Laurentf99edd32021-02-01 15:57:33 +01001137 int32_t selectedDeviceIdAidl = VALUE_OR_RETURN_STATUS(
1138 legacy2aidl_audio_port_handle_t_int32_t(*selectedDeviceId));
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001139
1140 media::GetOutputForAttrResponse responseAidl;
1141
jiabinf1c73972022-04-14 16:28:52 -07001142 status_t status = statusTFromBinderStatus(
Svet Ganov3e5f14f2021-05-13 22:51:08 +00001143 aps->getOutputForAttr(attrAidl, sessionAidl, attributionSource, configAidl, flagsAidl,
jiabinf1c73972022-04-14 16:28:52 -07001144 selectedDeviceIdAidl, &responseAidl));
1145 if (status != NO_ERROR) {
1146 config->format = VALUE_OR_RETURN_STATUS(
1147 aidl2legacy_AudioFormatDescription_audio_format_t(responseAidl.configBase.format));
1148 config->channel_mask = VALUE_OR_RETURN_STATUS(
1149 aidl2legacy_AudioChannelLayout_audio_channel_mask_t(
1150 responseAidl.configBase.channelMask, false /*isInput*/));
1151 config->sample_rate = responseAidl.configBase.sampleRate;
1152 return status;
1153 }
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001154
1155 *output = VALUE_OR_RETURN_STATUS(
1156 aidl2legacy_int32_t_audio_io_handle_t(responseAidl.output));
1157
1158 if (stream != nullptr) {
1159 *stream = VALUE_OR_RETURN_STATUS(
1160 aidl2legacy_AudioStreamType_audio_stream_type_t(responseAidl.stream));
1161 }
1162 *selectedDeviceId = VALUE_OR_RETURN_STATUS(
1163 aidl2legacy_int32_t_audio_port_handle_t(responseAidl.selectedDeviceId));
1164 *portId = VALUE_OR_RETURN_STATUS(aidl2legacy_int32_t_audio_port_handle_t(responseAidl.portId));
1165 *secondaryOutputs = VALUE_OR_RETURN_STATUS(convertContainer<std::vector<audio_io_handle_t>>(
1166 responseAidl.secondaryOutputs, aidl2legacy_int32_t_audio_io_handle_t));
Eric Laurentb0a7bc92022-04-05 15:06:08 +02001167 *isSpatialized = responseAidl.isSpatialized;
jiabinc658e452022-10-21 20:52:21 +00001168 *isBitPerfect = responseAidl.isBitPerfect;
Eric Laurent0d13fea2022-11-04 17:12:08 +01001169 *attr = VALUE_OR_RETURN_STATUS(
Mikhail Naganov1c400902023-05-17 11:48:43 -07001170 aidl2legacy_AudioAttributes_audio_attributes_t(responseAidl.attr));
Francois Gaffie55b2a0f2021-06-24 15:58:37 +02001171 *volume = responseAidl.volume;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001172
1173 return OK;
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -07001174}
1175
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001176status_t AudioSystem::startOutput(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->startOutput(portIdAidl));
Eric Laurentc2f1f072009-07-17 12:17:14 -07001182}
1183
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001184status_t AudioSystem::stopOutput(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 PERMISSION_DENIED;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001187
1188 int32_t portIdAidl = VALUE_OR_RETURN_STATUS(legacy2aidl_audio_port_handle_t_int32_t(portId));
1189 return statusTFromBinderStatus(aps->stopOutput(portIdAidl));
Eric Laurentc2f1f072009-07-17 12:17:14 -07001190}
1191
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001192void AudioSystem::releaseOutput(audio_port_handle_t portId) {
Andy Hung264fa4c2024-02-21 15:52:12 -08001193 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Eric Laurentc2f1f072009-07-17 12:17:14 -07001194 if (aps == 0) return;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001195
1196 auto status = [&]() -> status_t {
1197 int32_t portIdAidl = VALUE_OR_RETURN_STATUS(
1198 legacy2aidl_audio_port_handle_t_int32_t(portId));
1199 RETURN_STATUS_IF_ERROR(statusTFromBinderStatus(aps->releaseOutput(portIdAidl)));
1200 return OK;
1201 }();
1202
1203 // Ignore status.
1204 (void) status;
Eric Laurentc2f1f072009-07-17 12:17:14 -07001205}
1206
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001207status_t AudioSystem::getInputForAttr(const audio_attributes_t* attr,
1208 audio_io_handle_t* input,
1209 audio_unique_id_t riid,
1210 audio_session_t session,
Svet Ganov3e5f14f2021-05-13 22:51:08 +00001211 const AttributionSourceState &attributionSource,
jiabinf1c73972022-04-14 16:28:52 -07001212 audio_config_base_t* config,
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001213 audio_input_flags_t flags,
1214 audio_port_handle_t* selectedDeviceId,
1215 audio_port_handle_t* portId) {
1216 if (attr == NULL) {
1217 ALOGE("getInputForAttr NULL attr - shouldn't happen");
1218 return BAD_VALUE;
1219 }
1220 if (input == NULL) {
1221 ALOGE("getInputForAttr NULL input - shouldn't happen");
1222 return BAD_VALUE;
1223 }
1224 if (selectedDeviceId == NULL) {
1225 ALOGE("getInputForAttr NULL selectedDeviceId - shouldn't happen");
1226 return BAD_VALUE;
1227 }
1228 if (portId == NULL) {
1229 ALOGE("getInputForAttr NULL portId - shouldn't happen");
1230 return BAD_VALUE;
1231 }
1232
Andy Hung264fa4c2024-02-21 15:52:12 -08001233 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Eric Laurentcaf7f482014-11-25 17:50:47 -08001234 if (aps == 0) return NO_INIT;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001235
Mikhail Naganov1c400902023-05-17 11:48:43 -07001236 media::audio::common::AudioAttributes attrAidl = VALUE_OR_RETURN_STATUS(
1237 legacy2aidl_audio_attributes_t_AudioAttributes(*attr));
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001238 int32_t inputAidl = VALUE_OR_RETURN_STATUS(legacy2aidl_audio_io_handle_t_int32_t(*input));
1239 int32_t riidAidl = VALUE_OR_RETURN_STATUS(legacy2aidl_audio_unique_id_t_int32_t(riid));
1240 int32_t sessionAidl = VALUE_OR_RETURN_STATUS(legacy2aidl_audio_session_t_int32_t(session));
Mikhail Naganovdbf03642021-08-25 18:15:32 -07001241 AudioConfigBase configAidl = VALUE_OR_RETURN_STATUS(
Mikhail Naganovde3fa182021-07-30 15:06:42 -07001242 legacy2aidl_audio_config_base_t_AudioConfigBase(*config, true /*isInput*/));
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001243 int32_t flagsAidl = VALUE_OR_RETURN_STATUS(legacy2aidl_audio_input_flags_t_int32_t_mask(flags));
Eric Laurentf99edd32021-02-01 15:57:33 +01001244 int32_t selectedDeviceIdAidl = VALUE_OR_RETURN_STATUS(
1245 legacy2aidl_audio_port_handle_t_int32_t(*selectedDeviceId));
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001246
1247 media::GetInputForAttrResponse response;
1248
jiabinf1c73972022-04-14 16:28:52 -07001249 status_t status = statusTFromBinderStatus(
Svet Ganov3e5f14f2021-05-13 22:51:08 +00001250 aps->getInputForAttr(attrAidl, inputAidl, riidAidl, sessionAidl, attributionSource,
jiabinf1c73972022-04-14 16:28:52 -07001251 configAidl, flagsAidl, selectedDeviceIdAidl, &response));
1252 if (status != NO_ERROR) {
1253 *config = VALUE_OR_RETURN_STATUS(
1254 aidl2legacy_AudioConfigBase_audio_config_base_t(response.config, true /*isInput*/));
1255 return status;
1256 }
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001257
1258 *input = VALUE_OR_RETURN_STATUS(aidl2legacy_int32_t_audio_io_handle_t(response.input));
1259 *selectedDeviceId = VALUE_OR_RETURN_STATUS(
1260 aidl2legacy_int32_t_audio_port_handle_t(response.selectedDeviceId));
1261 *portId = VALUE_OR_RETURN_STATUS(aidl2legacy_int32_t_audio_port_handle_t(response.portId));
1262
1263 return OK;
Eric Laurentc2f1f072009-07-17 12:17:14 -07001264}
1265
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001266status_t AudioSystem::startInput(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->startInput(portIdAidl));
Eric Laurentc2f1f072009-07-17 12:17:14 -07001272}
1273
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001274status_t AudioSystem::stopInput(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 PERMISSION_DENIED;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001277
1278 int32_t portIdAidl = VALUE_OR_RETURN_STATUS(legacy2aidl_audio_port_handle_t_int32_t(portId));
1279 return statusTFromBinderStatus(aps->stopInput(portIdAidl));
Eric Laurentc2f1f072009-07-17 12:17:14 -07001280}
1281
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001282void AudioSystem::releaseInput(audio_port_handle_t portId) {
Andy Hung264fa4c2024-02-21 15:52:12 -08001283 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Eric Laurentc2f1f072009-07-17 12:17:14 -07001284 if (aps == 0) return;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001285
1286 auto status = [&]() -> status_t {
1287 int32_t portIdAidl = VALUE_OR_RETURN_STATUS(
1288 legacy2aidl_audio_port_handle_t_int32_t(portId));
1289 RETURN_STATUS_IF_ERROR(statusTFromBinderStatus(aps->releaseInput(portIdAidl)));
1290 return OK;
1291 }();
1292
1293 // Ignore status.
1294 (void) status;
Eric Laurentc2f1f072009-07-17 12:17:14 -07001295}
1296
Vlad Popa87e0e582024-05-20 18:49:20 -07001297status_t AudioSystem::setDeviceAbsoluteVolumeEnabled(audio_devices_t deviceType,
1298 const char *address,
1299 bool enabled,
1300 audio_stream_type_t streamToDriveAbs) {
1301 const sp<IAudioPolicyService> aps = get_audio_policy_service();
1302 if (aps == nullptr) return PERMISSION_DENIED;
1303
1304 AudioDevice deviceAidl = VALUE_OR_RETURN_STATUS(
1305 legacy2aidl_audio_device_AudioDevice(deviceType, address));
1306 AudioStreamType streamToDriveAbsAidl = VALUE_OR_RETURN_STATUS(
1307 legacy2aidl_audio_stream_type_t_AudioStreamType(streamToDriveAbs));
1308 return statusTFromBinderStatus(
1309 aps->setDeviceAbsoluteVolumeEnabled(deviceAidl, enabled, streamToDriveAbsAidl));
1310}
1311
Dima Zavinfce7a472011-04-19 22:30:36 -07001312status_t AudioSystem::initStreamVolume(audio_stream_type_t stream,
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001313 int indexMin,
1314 int indexMax) {
Andy Hung264fa4c2024-02-21 15:52:12 -08001315 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Eric Laurentc2f1f072009-07-17 12:17:14 -07001316 if (aps == 0) return PERMISSION_DENIED;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001317
Mikhail Naganovdbf03642021-08-25 18:15:32 -07001318 AudioStreamType streamAidl = VALUE_OR_RETURN_STATUS(
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001319 legacy2aidl_audio_stream_type_t_AudioStreamType(stream));
1320 int32_t indexMinAidl = VALUE_OR_RETURN_STATUS(convertIntegral<int32_t>(indexMin));
1321 int32_t indexMaxAidl = VALUE_OR_RETURN_STATUS(convertIntegral<int32_t>(indexMax));
Mikhail Naganovec3d5792022-05-06 00:19:55 +00001322 status_t status = statusTFromBinderStatus(
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001323 aps->initStreamVolume(streamAidl, indexMinAidl, indexMaxAidl));
Mikhail Naganovec3d5792022-05-06 00:19:55 +00001324 if (status == DEAD_OBJECT) {
1325 // This is a critical operation since w/o proper stream volumes no audio
1326 // will be heard. Make sure we recover from a failure in any case.
1327 ALOGE("Received DEAD_OBJECT from APS, clearing the client");
1328 clearAudioPolicyService();
1329 }
1330 return status;
Eric Laurentc2f1f072009-07-17 12:17:14 -07001331}
1332
Eric Laurent83844cc2011-11-18 16:43:31 -08001333status_t AudioSystem::setStreamVolumeIndex(audio_stream_type_t stream,
1334 int index,
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001335 audio_devices_t device) {
Andy Hung264fa4c2024-02-21 15:52:12 -08001336 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Eric Laurentc2f1f072009-07-17 12:17:14 -07001337 if (aps == 0) return PERMISSION_DENIED;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001338
Mikhail Naganovdbf03642021-08-25 18:15:32 -07001339 AudioStreamType streamAidl = VALUE_OR_RETURN_STATUS(
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001340 legacy2aidl_audio_stream_type_t_AudioStreamType(stream));
1341 int32_t indexAidl = VALUE_OR_RETURN_STATUS(convertIntegral<int32_t>(index));
Mikhail Naganovf4a75362021-09-16 00:02:54 +00001342 AudioDeviceDescription deviceAidl = VALUE_OR_RETURN_STATUS(
Mikhail Naganov21a32ec2021-07-08 14:40:12 -07001343 legacy2aidl_audio_devices_t_AudioDeviceDescription(device));
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001344 return statusTFromBinderStatus(
1345 aps->setStreamVolumeIndex(streamAidl, deviceAidl, indexAidl));
Eric Laurentc2f1f072009-07-17 12:17:14 -07001346}
1347
Eric Laurent83844cc2011-11-18 16:43:31 -08001348status_t AudioSystem::getStreamVolumeIndex(audio_stream_type_t stream,
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001349 int* index,
1350 audio_devices_t device) {
Andy Hung264fa4c2024-02-21 15:52:12 -08001351 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Eric Laurentc2f1f072009-07-17 12:17:14 -07001352 if (aps == 0) return PERMISSION_DENIED;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001353
Mikhail Naganovdbf03642021-08-25 18:15:32 -07001354 AudioStreamType streamAidl = VALUE_OR_RETURN_STATUS(
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001355 legacy2aidl_audio_stream_type_t_AudioStreamType(stream));
Mikhail Naganovf4a75362021-09-16 00:02:54 +00001356 AudioDeviceDescription deviceAidl = VALUE_OR_RETURN_STATUS(
Mikhail Naganov21a32ec2021-07-08 14:40:12 -07001357 legacy2aidl_audio_devices_t_AudioDeviceDescription(device));
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001358 int32_t indexAidl;
1359 RETURN_STATUS_IF_ERROR(statusTFromBinderStatus(
1360 aps->getStreamVolumeIndex(streamAidl, deviceAidl, &indexAidl)));
1361 if (index != nullptr) {
1362 *index = VALUE_OR_RETURN_STATUS(convertIntegral<int>(indexAidl));
1363 }
1364 return OK;
Eric Laurentc2f1f072009-07-17 12:17:14 -07001365}
1366
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001367status_t AudioSystem::setVolumeIndexForAttributes(const audio_attributes_t& attr,
François Gaffiecfe17322018-11-07 13:41:29 +01001368 int index,
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001369 audio_devices_t device) {
Andy Hung264fa4c2024-02-21 15:52:12 -08001370 const sp<IAudioPolicyService> aps = get_audio_policy_service();
François Gaffiecfe17322018-11-07 13:41:29 +01001371 if (aps == 0) return PERMISSION_DENIED;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001372
Mikhail Naganov1c400902023-05-17 11:48:43 -07001373 media::audio::common::AudioAttributes attrAidl = VALUE_OR_RETURN_STATUS(
1374 legacy2aidl_audio_attributes_t_AudioAttributes(attr));
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001375 int32_t indexAidl = VALUE_OR_RETURN_STATUS(convertIntegral<int32_t>(index));
Mikhail Naganovf4a75362021-09-16 00:02:54 +00001376 AudioDeviceDescription deviceAidl = VALUE_OR_RETURN_STATUS(
Mikhail Naganov21a32ec2021-07-08 14:40:12 -07001377 legacy2aidl_audio_devices_t_AudioDeviceDescription(device));
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001378 return statusTFromBinderStatus(
1379 aps->setVolumeIndexForAttributes(attrAidl, deviceAidl, indexAidl));
François Gaffiecfe17322018-11-07 13:41:29 +01001380}
1381
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001382status_t AudioSystem::getVolumeIndexForAttributes(const audio_attributes_t& attr,
1383 int& index,
1384 audio_devices_t device) {
Andy Hung264fa4c2024-02-21 15:52:12 -08001385 const sp<IAudioPolicyService> aps = get_audio_policy_service();
François Gaffiecfe17322018-11-07 13:41:29 +01001386 if (aps == 0) return PERMISSION_DENIED;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001387
Mikhail Naganov1c400902023-05-17 11:48:43 -07001388 media::audio::common::AudioAttributes attrAidl = VALUE_OR_RETURN_STATUS(
1389 legacy2aidl_audio_attributes_t_AudioAttributes(attr));
Mikhail Naganovf4a75362021-09-16 00:02:54 +00001390 AudioDeviceDescription deviceAidl = VALUE_OR_RETURN_STATUS(
Mikhail Naganov21a32ec2021-07-08 14:40:12 -07001391 legacy2aidl_audio_devices_t_AudioDeviceDescription(device));
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001392 int32_t indexAidl;
1393 RETURN_STATUS_IF_ERROR(statusTFromBinderStatus(
1394 aps->getVolumeIndexForAttributes(attrAidl, deviceAidl, &indexAidl)));
1395 index = VALUE_OR_RETURN_STATUS(convertIntegral<int>(indexAidl));
1396 return OK;
François Gaffiecfe17322018-11-07 13:41:29 +01001397}
1398
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001399status_t AudioSystem::getMaxVolumeIndexForAttributes(const audio_attributes_t& attr, int& index) {
Andy Hung264fa4c2024-02-21 15:52:12 -08001400 const sp<IAudioPolicyService> aps = get_audio_policy_service();
François Gaffiecfe17322018-11-07 13:41:29 +01001401 if (aps == 0) return PERMISSION_DENIED;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001402
Mikhail Naganov1c400902023-05-17 11:48:43 -07001403 media::audio::common::AudioAttributes attrAidl = VALUE_OR_RETURN_STATUS(
1404 legacy2aidl_audio_attributes_t_AudioAttributes(attr));
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001405 int32_t indexAidl;
1406 RETURN_STATUS_IF_ERROR(statusTFromBinderStatus(
1407 aps->getMaxVolumeIndexForAttributes(attrAidl, &indexAidl)));
1408 index = VALUE_OR_RETURN_STATUS(convertIntegral<int>(indexAidl));
1409 return OK;
François Gaffiecfe17322018-11-07 13:41:29 +01001410}
1411
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001412status_t AudioSystem::getMinVolumeIndexForAttributes(const audio_attributes_t& attr, int& index) {
Andy Hung264fa4c2024-02-21 15:52:12 -08001413 const sp<IAudioPolicyService> aps = get_audio_policy_service();
François Gaffiecfe17322018-11-07 13:41:29 +01001414 if (aps == 0) return PERMISSION_DENIED;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001415
Mikhail Naganov1c400902023-05-17 11:48:43 -07001416 media::audio::common::AudioAttributes attrAidl = VALUE_OR_RETURN_STATUS(
1417 legacy2aidl_audio_attributes_t_AudioAttributes(attr));
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001418 int32_t indexAidl;
1419 RETURN_STATUS_IF_ERROR(statusTFromBinderStatus(
1420 aps->getMinVolumeIndexForAttributes(attrAidl, &indexAidl)));
1421 index = VALUE_OR_RETURN_STATUS(convertIntegral<int>(indexAidl));
1422 return OK;
François Gaffiecfe17322018-11-07 13:41:29 +01001423}
1424
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001425product_strategy_t AudioSystem::getStrategyForStream(audio_stream_type_t stream) {
Andy Hung264fa4c2024-02-21 15:52:12 -08001426 const sp<IAudioPolicyService> aps = get_audio_policy_service();
François Gaffied0ba9ed2018-11-05 11:50:42 +01001427 if (aps == 0) return PRODUCT_STRATEGY_NONE;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001428
1429 auto result = [&]() -> ConversionResult<product_strategy_t> {
Mikhail Naganovdbf03642021-08-25 18:15:32 -07001430 AudioStreamType streamAidl = VALUE_OR_RETURN(
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001431 legacy2aidl_audio_stream_type_t_AudioStreamType(stream));
1432 int32_t resultAidl;
1433 RETURN_IF_ERROR(statusTFromBinderStatus(
1434 aps->getStrategyForStream(streamAidl, &resultAidl)));
1435 return aidl2legacy_int32_t_product_strategy_t(resultAidl);
1436 }();
1437 return result.value_or(PRODUCT_STRATEGY_NONE);
Eric Laurentde070132010-07-13 04:45:46 -07001438}
1439
François Gaffie1e2b56f2022-04-01 14:34:29 +02001440status_t AudioSystem::getDevicesForAttributes(const audio_attributes_t& aa,
Andy Hung6d23c0f2022-02-16 09:37:15 -08001441 AudioDeviceTypeAddrVector* devices,
1442 bool forVolume) {
Jean-Michel Trivif41599b2020-01-07 14:22:08 -08001443 if (devices == nullptr) {
1444 return BAD_VALUE;
1445 }
Andy Hung264fa4c2024-02-21 15:52:12 -08001446 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Jean-Michel Trivif41599b2020-01-07 14:22:08 -08001447 if (aps == 0) return PERMISSION_DENIED;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001448
Mikhail Naganov1c400902023-05-17 11:48:43 -07001449 media::audio::common::AudioAttributes aaAidl = VALUE_OR_RETURN_STATUS(
1450 legacy2aidl_audio_attributes_t_AudioAttributes(aa));
Mikhail Naganovf4a75362021-09-16 00:02:54 +00001451 std::vector<AudioDevice> retAidl;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001452 RETURN_STATUS_IF_ERROR(
Andy Hung6d23c0f2022-02-16 09:37:15 -08001453 statusTFromBinderStatus(aps->getDevicesForAttributes(aaAidl, forVolume, &retAidl)));
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001454 *devices = VALUE_OR_RETURN_STATUS(
1455 convertContainer<AudioDeviceTypeAddrVector>(
1456 retAidl,
1457 aidl2legacy_AudioDeviceTypeAddress));
1458 return OK;
Jean-Michel Trivif41599b2020-01-07 14:22:08 -08001459}
1460
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001461audio_io_handle_t AudioSystem::getOutputForEffect(const effect_descriptor_t* desc) {
Andy Hung264fa4c2024-02-21 15:52:12 -08001462 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Glenn Kastenefa6ea92014-01-08 09:10:43 -08001463 // FIXME change return type to status_t, and return PERMISSION_DENIED here
Glenn Kasten142f5192014-03-25 17:44:59 -07001464 if (aps == 0) return AUDIO_IO_HANDLE_NONE;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001465
1466 auto result = [&]() -> ConversionResult<audio_io_handle_t> {
1467 media::EffectDescriptor descAidl = VALUE_OR_RETURN(
1468 legacy2aidl_effect_descriptor_t_EffectDescriptor(*desc));
1469 int32_t retAidl;
1470 RETURN_IF_ERROR(
1471 statusTFromBinderStatus(aps->getOutputForEffect(descAidl, &retAidl)));
1472 return aidl2legacy_int32_t_audio_io_handle_t(retAidl);
1473 }();
1474
1475 return result.value_or(AUDIO_IO_HANDLE_NONE);
Eric Laurentde070132010-07-13 04:45:46 -07001476}
1477
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001478status_t AudioSystem::registerEffect(const effect_descriptor_t* desc,
1479 audio_io_handle_t io,
1480 product_strategy_t strategy,
1481 audio_session_t session,
1482 int id) {
Andy Hung264fa4c2024-02-21 15:52:12 -08001483 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Eric Laurentde070132010-07-13 04:45:46 -07001484 if (aps == 0) return PERMISSION_DENIED;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001485
1486 media::EffectDescriptor descAidl = VALUE_OR_RETURN_STATUS(
1487 legacy2aidl_effect_descriptor_t_EffectDescriptor(*desc));
1488 int32_t ioAidl = VALUE_OR_RETURN_STATUS(legacy2aidl_audio_io_handle_t_int32_t(io));
1489 int32_t strategyAidl = VALUE_OR_RETURN_STATUS(aidl2legacy_int32_t_product_strategy_t(strategy));
1490 int32_t sessionAidl = VALUE_OR_RETURN_STATUS(legacy2aidl_audio_session_t_int32_t(session));
1491 int32_t idAidl = VALUE_OR_RETURN_STATUS(convertReinterpret<int32_t>(id));
1492 return statusTFromBinderStatus(
1493 aps->registerEffect(descAidl, ioAidl, strategyAidl, sessionAidl, idAidl));
Eric Laurentde070132010-07-13 04:45:46 -07001494}
1495
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001496status_t AudioSystem::unregisterEffect(int id) {
Andy Hung264fa4c2024-02-21 15:52:12 -08001497 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Eric Laurentde070132010-07-13 04:45:46 -07001498 if (aps == 0) return PERMISSION_DENIED;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001499
1500 int32_t idAidl = VALUE_OR_RETURN_STATUS(convertReinterpret<int32_t>(id));
1501 return statusTFromBinderStatus(
1502 aps->unregisterEffect(idAidl));
Eric Laurentde070132010-07-13 04:45:46 -07001503}
1504
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001505status_t AudioSystem::setEffectEnabled(int id, bool enabled) {
Andy Hung264fa4c2024-02-21 15:52:12 -08001506 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Eric Laurentdb7c0792011-08-10 10:37:50 -07001507 if (aps == 0) return PERMISSION_DENIED;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001508
1509 int32_t idAidl = VALUE_OR_RETURN_STATUS(convertReinterpret<int32_t>(id));
1510 return statusTFromBinderStatus(
1511 aps->setEffectEnabled(idAidl, enabled));
Eric Laurentdb7c0792011-08-10 10:37:50 -07001512}
1513
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001514status_t AudioSystem::moveEffectsToIo(const std::vector<int>& ids, audio_io_handle_t io) {
Andy Hung264fa4c2024-02-21 15:52:12 -08001515 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Eric Laurent6c796322019-04-09 14:13:17 -07001516 if (aps == 0) return PERMISSION_DENIED;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001517
1518 std::vector<int32_t> idsAidl = VALUE_OR_RETURN_STATUS(
1519 convertContainer<std::vector<int32_t>>(ids, convertReinterpret<int32_t, int>));
1520 int32_t ioAidl = VALUE_OR_RETURN_STATUS(legacy2aidl_audio_io_handle_t_int32_t(io));
1521 return statusTFromBinderStatus(aps->moveEffectsToIo(idsAidl, ioAidl));
Eric Laurent6c796322019-04-09 14:13:17 -07001522}
1523
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001524status_t AudioSystem::isStreamActive(audio_stream_type_t stream, bool* state, uint32_t inPastMs) {
Andy Hung264fa4c2024-02-21 15:52:12 -08001525 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Eric Laurenteda6c362011-02-02 09:33:30 -08001526 if (aps == 0) return PERMISSION_DENIED;
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001527 if (state == NULL) return BAD_VALUE;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001528
Mikhail Naganovdbf03642021-08-25 18:15:32 -07001529 AudioStreamType streamAidl = VALUE_OR_RETURN_STATUS(
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001530 legacy2aidl_audio_stream_type_t_AudioStreamType(stream));
1531 int32_t inPastMsAidl = VALUE_OR_RETURN_STATUS(convertIntegral<int32_t>(inPastMs));
1532 RETURN_STATUS_IF_ERROR(statusTFromBinderStatus(
1533 aps->isStreamActive(streamAidl, inPastMsAidl, state)));
1534 return OK;
Eric Laurenteda6c362011-02-02 09:33:30 -08001535}
1536
Jean-Michel Trivi272ab542013-02-04 16:26:02 -08001537status_t AudioSystem::isStreamActiveRemotely(audio_stream_type_t stream, bool* state,
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001538 uint32_t inPastMs) {
Andy Hung264fa4c2024-02-21 15:52:12 -08001539 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Jean-Michel Trivi272ab542013-02-04 16:26:02 -08001540 if (aps == 0) return PERMISSION_DENIED;
1541 if (state == NULL) return BAD_VALUE;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001542
Mikhail Naganovdbf03642021-08-25 18:15:32 -07001543 AudioStreamType streamAidl = VALUE_OR_RETURN_STATUS(
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001544 legacy2aidl_audio_stream_type_t_AudioStreamType(stream));
1545 int32_t inPastMsAidl = VALUE_OR_RETURN_STATUS(convertIntegral<int32_t>(inPastMs));
1546 RETURN_STATUS_IF_ERROR(statusTFromBinderStatus(
1547 aps->isStreamActiveRemotely(streamAidl, inPastMsAidl, state)));
1548 return OK;
Jean-Michel Trivi272ab542013-02-04 16:26:02 -08001549}
1550
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001551status_t AudioSystem::isSourceActive(audio_source_t stream, bool* state) {
Andy Hung264fa4c2024-02-21 15:52:12 -08001552 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Jean-Michel Trivid7086032012-10-10 12:11:16 -07001553 if (aps == 0) return PERMISSION_DENIED;
1554 if (state == NULL) return BAD_VALUE;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001555
Mikhail Naganovddceecc2021-09-03 13:58:56 -07001556 AudioSource streamAidl = VALUE_OR_RETURN_STATUS(
1557 legacy2aidl_audio_source_t_AudioSource(stream));
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001558 RETURN_STATUS_IF_ERROR(statusTFromBinderStatus(
1559 aps->isSourceActive(streamAidl, state)));
1560 return OK;
Jean-Michel Trivid7086032012-10-10 12:11:16 -07001561}
1562
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001563uint32_t AudioSystem::getPrimaryOutputSamplingRate() {
Andy Hung264fa4c2024-02-21 15:52:12 -08001564 const sp<IAudioFlinger> af = get_audio_flinger();
Glenn Kastencc0f1cf2012-09-24 11:27:18 -07001565 if (af == 0) return 0;
1566 return af->getPrimaryOutputSamplingRate();
1567}
1568
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001569size_t AudioSystem::getPrimaryOutputFrameCount() {
Andy Hung264fa4c2024-02-21 15:52:12 -08001570 const sp<IAudioFlinger> af = get_audio_flinger();
Glenn Kastencc0f1cf2012-09-24 11:27:18 -07001571 if (af == 0) return 0;
1572 return af->getPrimaryOutputFrameCount();
1573}
Eric Laurenteda6c362011-02-02 09:33:30 -08001574
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001575status_t AudioSystem::setLowRamDevice(bool isLowRamDevice, int64_t totalMemory) {
Andy Hung264fa4c2024-02-21 15:52:12 -08001576 const sp<IAudioFlinger> af = get_audio_flinger();
Glenn Kasten4182c4e2013-07-15 14:45:07 -07001577 if (af == 0) return PERMISSION_DENIED;
Andy Hung6f248bb2018-01-23 14:04:37 -08001578 return af->setLowRamDevice(isLowRamDevice, totalMemory);
Glenn Kasten4182c4e2013-07-15 14:45:07 -07001579}
1580
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001581void AudioSystem::clearAudioConfigCache() {
Glenn Kastend2d089f2014-11-05 11:48:12 -08001582 // called by restoreTrack_l(), which needs new IAudioFlinger and IAudioPolicyService instances
Steve Block3856b092011-10-20 11:56:00 +01001583 ALOGV("clearAudioConfigCache()");
Andy Hungda1fb072024-02-20 19:08:08 -08001584 gAudioFlingerServiceHandler.clearService();
Mikhail Naganovec3d5792022-05-06 00:19:55 +00001585 clearAudioPolicyService();
Eric Laurent9f6530f2011-08-30 10:18:54 -07001586}
1587
Hayden Gomes524159d2019-12-23 14:41:47 -08001588status_t AudioSystem::setSupportedSystemUsages(const std::vector<audio_usage_t>& systemUsages) {
Andy Hung264fa4c2024-02-21 15:52:12 -08001589 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Hayden Gomes524159d2019-12-23 14:41:47 -08001590 if (aps == nullptr) return PERMISSION_DENIED;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001591
Mikhail Naganovdbf03642021-08-25 18:15:32 -07001592 std::vector<AudioUsage> systemUsagesAidl = VALUE_OR_RETURN_STATUS(
1593 convertContainer<std::vector<AudioUsage>>(systemUsages,
1594 legacy2aidl_audio_usage_t_AudioUsage));
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001595 return statusTFromBinderStatus(aps->setSupportedSystemUsages(systemUsagesAidl));
Hayden Gomes524159d2019-12-23 14:41:47 -08001596}
1597
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001598status_t AudioSystem::setAllowedCapturePolicy(uid_t uid, audio_flags_mask_t capturePolicy) {
Andy Hung264fa4c2024-02-21 15:52:12 -08001599 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Kevin Rocardb99cc752019-03-21 20:52:24 -07001600 if (aps == nullptr) return PERMISSION_DENIED;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001601
1602 int32_t uidAidl = VALUE_OR_RETURN_STATUS(legacy2aidl_uid_t_int32_t(uid));
1603 int32_t capturePolicyAidl = VALUE_OR_RETURN_STATUS(
1604 legacy2aidl_audio_flags_mask_t_int32_t_mask(capturePolicy));
1605 return statusTFromBinderStatus(aps->setAllowedCapturePolicy(uidAidl, capturePolicyAidl));
Kevin Rocardb99cc752019-03-21 20:52:24 -07001606}
1607
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001608audio_offload_mode_t AudioSystem::getOffloadSupport(const audio_offload_info_t& info) {
Eric Laurent90fe31c2020-11-26 20:06:35 +01001609 ALOGV("%s", __func__);
Andy Hung264fa4c2024-02-21 15:52:12 -08001610 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Eric Laurent90fe31c2020-11-26 20:06:35 +01001611 if (aps == 0) return AUDIO_OFFLOAD_NOT_SUPPORTED;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001612
1613 auto result = [&]() -> ConversionResult<audio_offload_mode_t> {
Mikhail Naganovdbf03642021-08-25 18:15:32 -07001614 AudioOffloadInfo infoAidl = VALUE_OR_RETURN(
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001615 legacy2aidl_audio_offload_info_t_AudioOffloadInfo(info));
1616 media::AudioOffloadMode retAidl;
1617 RETURN_IF_ERROR(
1618 statusTFromBinderStatus(aps->getOffloadSupport(infoAidl, &retAidl)));
1619 return aidl2legacy_AudioOffloadMode_audio_offload_mode_t(retAidl);
1620 }();
1621
1622 return result.value_or(static_cast<audio_offload_mode_t>(0));
Richard Fitzgeraldad3af332013-03-25 16:54:37 +00001623}
1624
Eric Laurent203b1a12014-04-01 10:34:16 -07001625status_t AudioSystem::listAudioPorts(audio_port_role_t role,
1626 audio_port_type_t type,
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001627 unsigned int* num_ports,
1628 struct audio_port_v7* ports,
1629 unsigned int* generation) {
1630 if (num_ports == nullptr || (*num_ports != 0 && ports == nullptr) ||
1631 generation == nullptr) {
1632 return BAD_VALUE;
1633 }
1634
Andy Hung264fa4c2024-02-21 15:52:12 -08001635 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Eric Laurent203b1a12014-04-01 10:34:16 -07001636 if (aps == 0) return PERMISSION_DENIED;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001637
1638 media::AudioPortRole roleAidl = VALUE_OR_RETURN_STATUS(
1639 legacy2aidl_audio_port_role_t_AudioPortRole(role));
1640 media::AudioPortType typeAidl = VALUE_OR_RETURN_STATUS(
1641 legacy2aidl_audio_port_type_t_AudioPortType(type));
Mikhail Naganov0078ee52021-09-30 23:06:20 +00001642 Int numPortsAidl;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001643 numPortsAidl.value = VALUE_OR_RETURN_STATUS(convertIntegral<int32_t>(*num_ports));
Atneya Nair638a6e42022-12-18 16:45:15 -08001644 std::vector<media::AudioPortFw> portsAidl;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001645 int32_t generationAidl;
1646
1647 RETURN_STATUS_IF_ERROR(statusTFromBinderStatus(
1648 aps->listAudioPorts(roleAidl, typeAidl, &numPortsAidl, &portsAidl, &generationAidl)));
1649 *num_ports = VALUE_OR_RETURN_STATUS(convertIntegral<unsigned int>(numPortsAidl.value));
1650 *generation = VALUE_OR_RETURN_STATUS(convertIntegral<unsigned int>(generationAidl));
1651 RETURN_STATUS_IF_ERROR(convertRange(portsAidl.begin(), portsAidl.end(), ports,
Mikhail Naganov87227252023-01-13 17:38:10 -08001652 aidl2legacy_AudioPortFw_audio_port_v7));
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001653 return OK;
Eric Laurent203b1a12014-04-01 10:34:16 -07001654}
1655
Mikhail Naganov5edc5ed2023-03-23 14:52:15 -07001656status_t AudioSystem::listDeclaredDevicePorts(media::AudioPortRole role,
1657 std::vector<media::AudioPortFw>* result) {
1658 if (result == nullptr) return BAD_VALUE;
Andy Hung264fa4c2024-02-21 15:52:12 -08001659 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Mikhail Naganov5edc5ed2023-03-23 14:52:15 -07001660 if (aps == 0) return PERMISSION_DENIED;
1661 RETURN_STATUS_IF_ERROR(statusTFromBinderStatus(aps->listDeclaredDevicePorts(role, result)));
1662 return OK;
1663}
1664
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001665status_t AudioSystem::getAudioPort(struct audio_port_v7* port) {
1666 if (port == nullptr) {
1667 return BAD_VALUE;
1668 }
Andy Hung264fa4c2024-02-21 15:52:12 -08001669 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Eric Laurent203b1a12014-04-01 10:34:16 -07001670 if (aps == 0) return PERMISSION_DENIED;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001671
Atneya Nair638a6e42022-12-18 16:45:15 -08001672 media::AudioPortFw portAidl;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001673 RETURN_STATUS_IF_ERROR(
Mikhail Naganov17031562022-02-23 23:00:27 +00001674 statusTFromBinderStatus(aps->getAudioPort(port->id, &portAidl)));
Mikhail Naganov87227252023-01-13 17:38:10 -08001675 *port = VALUE_OR_RETURN_STATUS(aidl2legacy_AudioPortFw_audio_port_v7(portAidl));
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001676 return OK;
Eric Laurent203b1a12014-04-01 10:34:16 -07001677}
1678
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001679status_t AudioSystem::createAudioPatch(const struct audio_patch* patch,
1680 audio_patch_handle_t* handle) {
1681 if (patch == nullptr || handle == nullptr) {
1682 return BAD_VALUE;
1683 }
1684
Andy Hung264fa4c2024-02-21 15:52:12 -08001685 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Eric Laurent203b1a12014-04-01 10:34:16 -07001686 if (aps == 0) return PERMISSION_DENIED;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001687
Atneya Nair3afdbd12022-12-18 16:14:18 -08001688 media::AudioPatchFw patchAidl = VALUE_OR_RETURN_STATUS(
Mikhail Naganov87227252023-01-13 17:38:10 -08001689 legacy2aidl_audio_patch_AudioPatchFw(*patch));
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001690 int32_t handleAidl = VALUE_OR_RETURN_STATUS(legacy2aidl_audio_patch_handle_t_int32_t(*handle));
1691 RETURN_STATUS_IF_ERROR(
1692 statusTFromBinderStatus(aps->createAudioPatch(patchAidl, handleAidl, &handleAidl)));
1693 *handle = VALUE_OR_RETURN_STATUS(aidl2legacy_int32_t_audio_patch_handle_t(handleAidl));
1694 return OK;
Eric Laurent203b1a12014-04-01 10:34:16 -07001695}
1696
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001697status_t AudioSystem::releaseAudioPatch(audio_patch_handle_t handle) {
Andy Hung264fa4c2024-02-21 15:52:12 -08001698 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Eric Laurent203b1a12014-04-01 10:34:16 -07001699 if (aps == 0) return PERMISSION_DENIED;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001700
1701 int32_t handleAidl = VALUE_OR_RETURN_STATUS(legacy2aidl_audio_patch_handle_t_int32_t(handle));
1702 return statusTFromBinderStatus(aps->releaseAudioPatch(handleAidl));
Eric Laurent203b1a12014-04-01 10:34:16 -07001703}
1704
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001705status_t AudioSystem::listAudioPatches(unsigned int* num_patches,
1706 struct audio_patch* patches,
1707 unsigned int* generation) {
1708 if (num_patches == nullptr || (*num_patches != 0 && patches == nullptr) ||
1709 generation == nullptr) {
1710 return BAD_VALUE;
1711 }
1712
Andy Hung264fa4c2024-02-21 15:52:12 -08001713 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Eric Laurent203b1a12014-04-01 10:34:16 -07001714 if (aps == 0) return PERMISSION_DENIED;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001715
1716
Mikhail Naganov0078ee52021-09-30 23:06:20 +00001717 Int numPatchesAidl;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001718 numPatchesAidl.value = VALUE_OR_RETURN_STATUS(convertIntegral<int32_t>(*num_patches));
Atneya Nair3afdbd12022-12-18 16:14:18 -08001719 std::vector<media::AudioPatchFw> patchesAidl;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001720 int32_t generationAidl;
1721
1722 RETURN_STATUS_IF_ERROR(statusTFromBinderStatus(
1723 aps->listAudioPatches(&numPatchesAidl, &patchesAidl, &generationAidl)));
1724 *num_patches = VALUE_OR_RETURN_STATUS(convertIntegral<unsigned int>(numPatchesAidl.value));
1725 *generation = VALUE_OR_RETURN_STATUS(convertIntegral<unsigned int>(generationAidl));
1726 RETURN_STATUS_IF_ERROR(convertRange(patchesAidl.begin(), patchesAidl.end(), patches,
Mikhail Naganov87227252023-01-13 17:38:10 -08001727 aidl2legacy_AudioPatchFw_audio_patch));
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001728 return OK;
Eric Laurent203b1a12014-04-01 10:34:16 -07001729}
1730
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001731status_t AudioSystem::setAudioPortConfig(const struct audio_port_config* config) {
1732 if (config == nullptr) {
1733 return BAD_VALUE;
1734 }
1735
Andy Hung264fa4c2024-02-21 15:52:12 -08001736 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Eric Laurent203b1a12014-04-01 10:34:16 -07001737 if (aps == 0) return PERMISSION_DENIED;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001738
Atneya Nair7a9594f2022-12-18 17:26:26 -08001739 media::AudioPortConfigFw configAidl = VALUE_OR_RETURN_STATUS(
Mikhail Naganov87227252023-01-13 17:38:10 -08001740 legacy2aidl_audio_port_config_AudioPortConfigFw(*config));
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001741 return statusTFromBinderStatus(aps->setAudioPortConfig(configAidl));
Eric Laurent203b1a12014-04-01 10:34:16 -07001742}
1743
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001744status_t AudioSystem::addAudioPortCallback(const sp<AudioPortCallback>& callback) {
Andy Hung264fa4c2024-02-21 15:52:12 -08001745 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Eric Laurentb28753e2015-04-01 13:06:28 -07001746 if (aps == 0) return PERMISSION_DENIED;
Andy Hung88e71732024-02-21 16:44:27 -08001747 const auto apc = gAudioPolicyServiceHandler.getClient();
1748 if (apc == nullptr) return NO_INIT;
Eric Laurentb28753e2015-04-01 13:06:28 -07001749
Andy Hung88e71732024-02-21 16:44:27 -08001750 std::lock_guard _l(gApsCallbackMutex);
1751 const int ret = apc->addAudioPortCallback(callback);
Eric Laurente8726fe2015-06-26 09:39:24 -07001752 if (ret == 1) {
1753 aps->setAudioPortCallbacksEnabled(true);
1754 }
1755 return (ret < 0) ? INVALID_OPERATION : NO_ERROR;
Eric Laurentb52c1522014-05-20 11:27:36 -07001756}
1757
Jean-Michel Trivif613d422015-04-23 18:41:29 -07001758/*static*/
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001759status_t AudioSystem::removeAudioPortCallback(const sp<AudioPortCallback>& callback) {
Andy Hung264fa4c2024-02-21 15:52:12 -08001760 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Eric Laurentb28753e2015-04-01 13:06:28 -07001761 if (aps == 0) return PERMISSION_DENIED;
Andy Hung88e71732024-02-21 16:44:27 -08001762 const auto apc = gAudioPolicyServiceHandler.getClient();
1763 if (apc == nullptr) return NO_INIT;
Eric Laurentb28753e2015-04-01 13:06:28 -07001764
Andy Hung88e71732024-02-21 16:44:27 -08001765 std::lock_guard _l(gApsCallbackMutex);
1766 const int ret = apc->removeAudioPortCallback(callback);
Eric Laurente8726fe2015-06-26 09:39:24 -07001767 if (ret == 0) {
1768 aps->setAudioPortCallbacksEnabled(false);
1769 }
1770 return (ret < 0) ? INVALID_OPERATION : NO_ERROR;
Eric Laurent296fb132015-05-01 11:38:42 -07001771}
1772
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001773status_t AudioSystem::addAudioVolumeGroupCallback(const sp<AudioVolumeGroupCallback>& callback) {
Andy Hung264fa4c2024-02-21 15:52:12 -08001774 const sp<IAudioPolicyService> aps = get_audio_policy_service();
François Gaffiecfe17322018-11-07 13:41:29 +01001775 if (aps == 0) return PERMISSION_DENIED;
Andy Hung88e71732024-02-21 16:44:27 -08001776 const auto apc = gAudioPolicyServiceHandler.getClient();
1777 if (apc == nullptr) return NO_INIT;
François Gaffiecfe17322018-11-07 13:41:29 +01001778
Andy Hung88e71732024-02-21 16:44:27 -08001779 std::lock_guard _l(gApsCallbackMutex);
1780 const int ret = apc->addAudioVolumeGroupCallback(callback);
François Gaffiecfe17322018-11-07 13:41:29 +01001781 if (ret == 1) {
1782 aps->setAudioVolumeGroupCallbacksEnabled(true);
1783 }
1784 return (ret < 0) ? INVALID_OPERATION : NO_ERROR;
1785}
1786
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001787status_t AudioSystem::removeAudioVolumeGroupCallback(const sp<AudioVolumeGroupCallback>& callback) {
Andy Hung264fa4c2024-02-21 15:52:12 -08001788 const sp<IAudioPolicyService> aps = get_audio_policy_service();
François Gaffiecfe17322018-11-07 13:41:29 +01001789 if (aps == 0) return PERMISSION_DENIED;
Andy Hung88e71732024-02-21 16:44:27 -08001790 const auto apc = gAudioPolicyServiceHandler.getClient();
1791 if (apc == nullptr) return NO_INIT;
François Gaffiecfe17322018-11-07 13:41:29 +01001792
Andy Hung88e71732024-02-21 16:44:27 -08001793 std::lock_guard _l(gApsCallbackMutex);
1794 const int ret = apc->removeAudioVolumeGroupCallback(callback);
François Gaffiecfe17322018-11-07 13:41:29 +01001795 if (ret == 0) {
1796 aps->setAudioVolumeGroupCallbacksEnabled(false);
1797 }
1798 return (ret < 0) ? INVALID_OPERATION : NO_ERROR;
1799}
1800
Eric Laurent296fb132015-05-01 11:38:42 -07001801status_t AudioSystem::addAudioDeviceCallback(
Eric Laurent09f1ed22019-04-24 17:45:17 -07001802 const wp<AudioDeviceCallback>& callback, audio_io_handle_t audioIo,
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001803 audio_port_handle_t portId) {
Eric Laurent296fb132015-05-01 11:38:42 -07001804 const sp<AudioFlingerClient> afc = getAudioFlingerClient();
1805 if (afc == 0) {
1806 return NO_INIT;
1807 }
Eric Laurent09f1ed22019-04-24 17:45:17 -07001808 status_t status = afc->addAudioDeviceCallback(callback, audioIo, portId);
Eric Laurent7c1ec5f2015-07-09 14:52:47 -07001809 if (status == NO_ERROR) {
Andy Hung264fa4c2024-02-21 15:52:12 -08001810 const sp<IAudioFlinger> af = get_audio_flinger();
Eric Laurent7c1ec5f2015-07-09 14:52:47 -07001811 if (af != 0) {
1812 af->registerClient(afc);
1813 }
1814 }
1815 return status;
Eric Laurent296fb132015-05-01 11:38:42 -07001816}
1817
1818status_t AudioSystem::removeAudioDeviceCallback(
Eric Laurent09f1ed22019-04-24 17:45:17 -07001819 const wp<AudioDeviceCallback>& callback, audio_io_handle_t audioIo,
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001820 audio_port_handle_t portId) {
Eric Laurent296fb132015-05-01 11:38:42 -07001821 const sp<AudioFlingerClient> afc = getAudioFlingerClient();
1822 if (afc == 0) {
1823 return NO_INIT;
1824 }
Eric Laurent09f1ed22019-04-24 17:45:17 -07001825 return afc->removeAudioDeviceCallback(callback, audioIo, portId);
Eric Laurent296fb132015-05-01 11:38:42 -07001826}
1827
Eric Laurent076e7c72022-05-03 18:12:28 +02001828status_t AudioSystem::addSupportedLatencyModesCallback(
1829 const sp<SupportedLatencyModesCallback>& callback) {
1830 const sp<AudioFlingerClient> afc = getAudioFlingerClient();
1831 if (afc == 0) {
1832 return NO_INIT;
1833 }
1834 return afc->addSupportedLatencyModesCallback(callback);
1835}
1836
1837status_t AudioSystem::removeSupportedLatencyModesCallback(
1838 const sp<SupportedLatencyModesCallback>& callback) {
1839 const sp<AudioFlingerClient> afc = getAudioFlingerClient();
1840 if (afc == 0) {
1841 return NO_INIT;
1842 }
1843 return afc->removeSupportedLatencyModesCallback(callback);
1844}
1845
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001846audio_port_handle_t AudioSystem::getDeviceIdForIo(audio_io_handle_t audioIo) {
Andy Hung264fa4c2024-02-21 15:52:12 -08001847 const sp<IAudioFlinger> af = get_audio_flinger();
Eric Laurent296fb132015-05-01 11:38:42 -07001848 if (af == 0) return PERMISSION_DENIED;
1849 const sp<AudioIoDescriptor> desc = getIoDescriptor(audioIo);
1850 if (desc == 0) {
1851 return AUDIO_PORT_HANDLE_NONE;
1852 }
1853 return desc->getDeviceId();
Eric Laurentb28753e2015-04-01 13:06:28 -07001854}
1855
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001856status_t AudioSystem::acquireSoundTriggerSession(audio_session_t* session,
1857 audio_io_handle_t* ioHandle,
1858 audio_devices_t* device) {
1859 if (session == nullptr || ioHandle == nullptr || device == nullptr) {
1860 return BAD_VALUE;
1861 }
Andy Hung264fa4c2024-02-21 15:52:12 -08001862 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Eric Laurentdf3dc7e2014-07-27 18:39:40 -07001863 if (aps == 0) return PERMISSION_DENIED;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001864
1865 media::SoundTriggerSession retAidl;
1866 RETURN_STATUS_IF_ERROR(
1867 statusTFromBinderStatus(aps->acquireSoundTriggerSession(&retAidl)));
1868 *session = VALUE_OR_RETURN_STATUS(aidl2legacy_int32_t_audio_session_t(retAidl.session));
1869 *ioHandle = VALUE_OR_RETURN_STATUS(aidl2legacy_int32_t_audio_io_handle_t(retAidl.ioHandle));
Mikhail Naganov21a32ec2021-07-08 14:40:12 -07001870 *device = VALUE_OR_RETURN_STATUS(
1871 aidl2legacy_AudioDeviceDescription_audio_devices_t(retAidl.device));
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001872 return OK;
Eric Laurentdf3dc7e2014-07-27 18:39:40 -07001873}
1874
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001875status_t AudioSystem::releaseSoundTriggerSession(audio_session_t session) {
Andy Hung264fa4c2024-02-21 15:52:12 -08001876 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Eric Laurentdf3dc7e2014-07-27 18:39:40 -07001877 if (aps == 0) return PERMISSION_DENIED;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001878
1879 int32_t sessionAidl = VALUE_OR_RETURN_STATUS(legacy2aidl_audio_session_t_int32_t(session));
1880 return statusTFromBinderStatus(aps->releaseSoundTriggerSession(sessionAidl));
Eric Laurentdf3dc7e2014-07-27 18:39:40 -07001881}
Eric Laurentbb6c9a02014-09-25 14:11:47 -07001882
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001883audio_mode_t AudioSystem::getPhoneState() {
Andy Hung264fa4c2024-02-21 15:52:12 -08001884 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Eric Laurentbb6c9a02014-09-25 14:11:47 -07001885 if (aps == 0) return AUDIO_MODE_INVALID;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001886
1887 auto result = [&]() -> ConversionResult<audio_mode_t> {
Mikhail Naganovddceecc2021-09-03 13:58:56 -07001888 media::audio::common::AudioMode retAidl;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001889 RETURN_IF_ERROR(statusTFromBinderStatus(aps->getPhoneState(&retAidl)));
1890 return aidl2legacy_AudioMode_audio_mode_t(retAidl);
1891 }();
1892
1893 return result.value_or(AUDIO_MODE_INVALID);
Eric Laurentbb6c9a02014-09-25 14:11:47 -07001894}
1895
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001896status_t AudioSystem::registerPolicyMixes(const Vector<AudioMix>& mixes, bool registration) {
Andy Hung264fa4c2024-02-21 15:52:12 -08001897 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Eric Laurentbaac1832014-12-01 17:52:59 -08001898 if (aps == 0) return PERMISSION_DENIED;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001899
1900 size_t mixesSize = std::min(mixes.size(), size_t{MAX_MIXES_PER_POLICY});
1901 std::vector<media::AudioMix> mixesAidl;
1902 RETURN_STATUS_IF_ERROR(
1903 convertRange(mixes.begin(), mixes.begin() + mixesSize, std::back_inserter(mixesAidl),
1904 legacy2aidl_AudioMix));
1905 return statusTFromBinderStatus(aps->registerPolicyMixes(mixesAidl, registration));
Eric Laurentbaac1832014-12-01 17:52:59 -08001906}
Eric Laurentbb6c9a02014-09-25 14:11:47 -07001907
Marvin Raminbdefaf02023-11-01 09:10:32 +01001908status_t AudioSystem::getRegisteredPolicyMixes(std::vector<AudioMix>& mixes) {
1909 if (!audio_flags::audio_mix_test_api()) {
1910 return INVALID_OPERATION;
1911 }
1912
1913 const sp<IAudioPolicyService> aps = AudioSystem::get_audio_policy_service();
1914 if (aps == nullptr) return PERMISSION_DENIED;
1915
1916 std::vector<::android::media::AudioMix> aidlMixes;
1917 Status status = aps->getRegisteredPolicyMixes(&aidlMixes);
1918
1919 for (const auto& aidlMix : aidlMixes) {
1920 AudioMix mix = VALUE_OR_RETURN_STATUS(aidl2legacy_AudioMix(aidlMix));
1921 mixes.push_back(mix);
1922 }
1923
1924 return statusTFromBinderStatus(status);
1925}
1926
Jan Sebechlebsky0af8e872023-08-11 14:45:08 +02001927status_t AudioSystem::updatePolicyMixes(
1928 const std::vector<std::pair<AudioMix, std::vector<AudioMixMatchCriterion>>>&
1929 mixesWithUpdates) {
Andy Hung264fa4c2024-02-21 15:52:12 -08001930 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Jan Sebechlebsky0af8e872023-08-11 14:45:08 +02001931 if (aps == 0) return PERMISSION_DENIED;
1932
1933 std::vector<media::AudioMixUpdate> updatesAidl;
1934 updatesAidl.reserve(mixesWithUpdates.size());
1935
1936 for (const auto& update : mixesWithUpdates) {
1937 media::AudioMixUpdate updateAidl;
1938 updateAidl.audioMix = VALUE_OR_RETURN_STATUS(legacy2aidl_AudioMix(update.first));
1939 RETURN_STATUS_IF_ERROR(convertRange(update.second.begin(), update.second.end(),
1940 std::back_inserter(updateAidl.newCriteria),
1941 legacy2aidl_AudioMixMatchCriterion));
1942 updatesAidl.emplace_back(updateAidl);
1943 }
1944
1945 return statusTFromBinderStatus(aps->updatePolicyMixes(updatesAidl));
1946}
1947
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001948status_t AudioSystem::setUidDeviceAffinities(uid_t uid, const AudioDeviceTypeAddrVector& devices) {
Andy Hung264fa4c2024-02-21 15:52:12 -08001949 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Jean-Michel Trivibda70da2018-12-19 07:30:15 -08001950 if (aps == 0) return PERMISSION_DENIED;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001951
1952 int32_t uidAidl = VALUE_OR_RETURN_STATUS(legacy2aidl_uid_t_int32_t(uid));
Mikhail Naganovf4a75362021-09-16 00:02:54 +00001953 std::vector<AudioDevice> devicesAidl = VALUE_OR_RETURN_STATUS(
1954 convertContainer<std::vector<AudioDevice>>(devices,
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001955 legacy2aidl_AudioDeviceTypeAddress));
1956 return statusTFromBinderStatus(aps->setUidDeviceAffinities(uidAidl, devicesAidl));
Jean-Michel Trivibda70da2018-12-19 07:30:15 -08001957}
1958
1959status_t AudioSystem::removeUidDeviceAffinities(uid_t uid) {
Andy Hung264fa4c2024-02-21 15:52:12 -08001960 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Jean-Michel Trivibda70da2018-12-19 07:30:15 -08001961 if (aps == 0) return PERMISSION_DENIED;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001962
1963 int32_t uidAidl = VALUE_OR_RETURN_STATUS(legacy2aidl_uid_t_int32_t(uid));
1964 return statusTFromBinderStatus(aps->removeUidDeviceAffinities(uidAidl));
Jean-Michel Trivibda70da2018-12-19 07:30:15 -08001965}
1966
Oscar Azucena90e77632019-11-27 17:12:28 -08001967status_t AudioSystem::setUserIdDeviceAffinities(int userId,
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001968 const AudioDeviceTypeAddrVector& devices) {
Andy Hung264fa4c2024-02-21 15:52:12 -08001969 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Oscar Azucena90e77632019-11-27 17:12:28 -08001970 if (aps == 0) return PERMISSION_DENIED;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001971
1972 int32_t userIdAidl = VALUE_OR_RETURN_STATUS(convertReinterpret<int32_t>(userId));
Mikhail Naganovf4a75362021-09-16 00:02:54 +00001973 std::vector<AudioDevice> devicesAidl = VALUE_OR_RETURN_STATUS(
1974 convertContainer<std::vector<AudioDevice>>(devices,
1975 legacy2aidl_AudioDeviceTypeAddress));
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001976 return statusTFromBinderStatus(
1977 aps->setUserIdDeviceAffinities(userIdAidl, devicesAidl));
Oscar Azucena90e77632019-11-27 17:12:28 -08001978}
1979
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001980status_t AudioSystem::removeUserIdDeviceAffinities(int userId) {
Andy Hung264fa4c2024-02-21 15:52:12 -08001981 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Oscar Azucena90e77632019-11-27 17:12:28 -08001982 if (aps == 0) return PERMISSION_DENIED;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001983 int32_t userIdAidl = VALUE_OR_RETURN_STATUS(convertReinterpret<int32_t>(userId));
1984 return statusTFromBinderStatus(aps->removeUserIdDeviceAffinities(userIdAidl));
Oscar Azucena90e77632019-11-27 17:12:28 -08001985}
1986
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001987status_t AudioSystem::startAudioSource(const struct audio_port_config* source,
1988 const audio_attributes_t* attributes,
1989 audio_port_handle_t* portId) {
1990 if (source == nullptr || attributes == nullptr || portId == nullptr) {
1991 return BAD_VALUE;
1992 }
Andy Hung264fa4c2024-02-21 15:52:12 -08001993 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Eric Laurent554a2772015-04-10 11:29:24 -07001994 if (aps == 0) return PERMISSION_DENIED;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001995
Atneya Nair7a9594f2022-12-18 17:26:26 -08001996 media::AudioPortConfigFw sourceAidl = VALUE_OR_RETURN_STATUS(
Mikhail Naganov87227252023-01-13 17:38:10 -08001997 legacy2aidl_audio_port_config_AudioPortConfigFw(*source));
Mikhail Naganov1c400902023-05-17 11:48:43 -07001998 media::audio::common::AudioAttributes attributesAidl = VALUE_OR_RETURN_STATUS(
1999 legacy2aidl_audio_attributes_t_AudioAttributes(*attributes));
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002000 int32_t portIdAidl;
2001 RETURN_STATUS_IF_ERROR(statusTFromBinderStatus(
2002 aps->startAudioSource(sourceAidl, attributesAidl, &portIdAidl)));
2003 *portId = VALUE_OR_RETURN_STATUS(aidl2legacy_int32_t_audio_port_handle_t(portIdAidl));
2004 return OK;
Eric Laurent554a2772015-04-10 11:29:24 -07002005}
2006
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002007status_t AudioSystem::stopAudioSource(audio_port_handle_t portId) {
Andy Hung264fa4c2024-02-21 15:52:12 -08002008 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Eric Laurent554a2772015-04-10 11:29:24 -07002009 if (aps == 0) return PERMISSION_DENIED;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002010
2011 int32_t portIdAidl = VALUE_OR_RETURN_STATUS(legacy2aidl_audio_port_handle_t_int32_t(portId));
2012 return statusTFromBinderStatus(aps->stopAudioSource(portIdAidl));
Eric Laurent554a2772015-04-10 11:29:24 -07002013}
2014
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002015status_t AudioSystem::setMasterMono(bool mono) {
Andy Hung264fa4c2024-02-21 15:52:12 -08002016 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Andy Hung2ddee192015-12-18 17:34:44 -08002017 if (aps == 0) return PERMISSION_DENIED;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002018 return statusTFromBinderStatus(aps->setMasterMono(mono));
Andy Hung2ddee192015-12-18 17:34:44 -08002019}
2020
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002021status_t AudioSystem::getMasterMono(bool* mono) {
2022 if (mono == nullptr) {
2023 return BAD_VALUE;
2024 }
Andy Hung264fa4c2024-02-21 15:52:12 -08002025 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Andy Hung2ddee192015-12-18 17:34:44 -08002026 if (aps == 0) return PERMISSION_DENIED;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002027 return statusTFromBinderStatus(aps->getMasterMono(mono));
Andy Hung2ddee192015-12-18 17:34:44 -08002028}
2029
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002030status_t AudioSystem::setMasterBalance(float balance) {
Andy Hung264fa4c2024-02-21 15:52:12 -08002031 const sp<IAudioFlinger> af = get_audio_flinger();
Richard Folke Tullberg3fae0372017-01-13 09:04:25 +01002032 if (af == 0) return PERMISSION_DENIED;
2033 return af->setMasterBalance(balance);
2034}
2035
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002036status_t AudioSystem::getMasterBalance(float* balance) {
Andy Hung264fa4c2024-02-21 15:52:12 -08002037 const sp<IAudioFlinger> af = get_audio_flinger();
Richard Folke Tullberg3fae0372017-01-13 09:04:25 +01002038 if (af == 0) return PERMISSION_DENIED;
2039 return af->getMasterBalance(balance);
2040}
2041
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002042float
2043AudioSystem::getStreamVolumeDB(audio_stream_type_t stream, int index, audio_devices_t device) {
Andy Hung264fa4c2024-02-21 15:52:12 -08002044 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Eric Laurentac9cef52017-06-09 15:46:26 -07002045 if (aps == 0) return NAN;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002046
2047 auto result = [&]() -> ConversionResult<float> {
Mikhail Naganovdbf03642021-08-25 18:15:32 -07002048 AudioStreamType streamAidl = VALUE_OR_RETURN(
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002049 legacy2aidl_audio_stream_type_t_AudioStreamType(stream));
2050 int32_t indexAidl = VALUE_OR_RETURN(convertIntegral<int32_t>(index));
Mikhail Naganovf4a75362021-09-16 00:02:54 +00002051 AudioDeviceDescription deviceAidl = VALUE_OR_RETURN(
Mikhail Naganov21a32ec2021-07-08 14:40:12 -07002052 legacy2aidl_audio_devices_t_AudioDeviceDescription(device));
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002053 float retAidl;
2054 RETURN_IF_ERROR(statusTFromBinderStatus(
2055 aps->getStreamVolumeDB(streamAidl, indexAidl, deviceAidl, &retAidl)));
2056 return retAidl;
2057 }();
2058 return result.value_or(NAN);
Eric Laurentac9cef52017-06-09 15:46:26 -07002059}
2060
Mikhail Naganovd5d9de72023-02-13 11:45:03 -08002061status_t AudioSystem::getMicrophones(std::vector<media::MicrophoneInfoFw>* microphones) {
Andy Hung264fa4c2024-02-21 15:52:12 -08002062 const sp<IAudioFlinger> af = get_audio_flinger();
jiabin46a76fa2018-01-05 10:18:21 -08002063 if (af == 0) return PERMISSION_DENIED;
2064 return af->getMicrophones(microphones);
2065}
2066
Eric Laurent42896a02019-09-27 15:40:33 -07002067status_t AudioSystem::setAudioHalPids(const std::vector<pid_t>& pids) {
Andy Hung264fa4c2024-02-21 15:52:12 -08002068 const sp<IAudioFlinger> af = get_audio_flinger();
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002069 if (af == nullptr) return PERMISSION_DENIED;
2070 return af->setAudioHalPids(pids);
Eric Laurent42896a02019-09-27 15:40:33 -07002071}
2072
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002073status_t AudioSystem::getSurroundFormats(unsigned int* numSurroundFormats,
2074 audio_format_t* surroundFormats,
Kriti Dang6537def2021-03-02 13:46:59 +01002075 bool* surroundFormatsEnabled) {
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002076 if (numSurroundFormats == nullptr || (*numSurroundFormats != 0 &&
2077 (surroundFormats == nullptr ||
2078 surroundFormatsEnabled == nullptr))) {
2079 return BAD_VALUE;
2080 }
2081
Andy Hung264fa4c2024-02-21 15:52:12 -08002082 const sp<IAudioPolicyService> aps = get_audio_policy_service();
jiabin81772902018-04-02 17:52:27 -07002083 if (aps == 0) return PERMISSION_DENIED;
Mikhail Naganov0078ee52021-09-30 23:06:20 +00002084 Int numSurroundFormatsAidl;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002085 numSurroundFormatsAidl.value =
2086 VALUE_OR_RETURN_STATUS(convertIntegral<int32_t>(*numSurroundFormats));
Mikhail Naganov57bd06f2021-08-10 16:41:54 -07002087 std::vector<AudioFormatDescription> surroundFormatsAidl;
Kriti Dang877b27e2021-02-02 12:10:40 +01002088 std::vector<bool> surroundFormatsEnabledAidl;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002089 RETURN_STATUS_IF_ERROR(statusTFromBinderStatus(
Kriti Dang6537def2021-03-02 13:46:59 +01002090 aps->getSurroundFormats(&numSurroundFormatsAidl, &surroundFormatsAidl,
2091 &surroundFormatsEnabledAidl)));
Kriti Dang877b27e2021-02-02 12:10:40 +01002092
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002093 *numSurroundFormats = VALUE_OR_RETURN_STATUS(
2094 convertIntegral<unsigned int>(numSurroundFormatsAidl.value));
2095 RETURN_STATUS_IF_ERROR(
2096 convertRange(surroundFormatsAidl.begin(), surroundFormatsAidl.end(), surroundFormats,
Mikhail Naganovb60bd1b2021-07-15 17:31:43 -07002097 aidl2legacy_AudioFormatDescription_audio_format_t));
Kriti Dang877b27e2021-02-02 12:10:40 +01002098 std::copy(surroundFormatsEnabledAidl.begin(), surroundFormatsEnabledAidl.end(),
2099 surroundFormatsEnabled);
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002100 return OK;
jiabin81772902018-04-02 17:52:27 -07002101}
2102
Kriti Dang6537def2021-03-02 13:46:59 +01002103status_t AudioSystem::getReportedSurroundFormats(unsigned int* numSurroundFormats,
2104 audio_format_t* surroundFormats) {
2105 if (numSurroundFormats == nullptr || (*numSurroundFormats != 0 && surroundFormats == nullptr)) {
2106 return BAD_VALUE;
2107 }
2108
Andy Hung264fa4c2024-02-21 15:52:12 -08002109 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Kriti Dang6537def2021-03-02 13:46:59 +01002110 if (aps == 0) return PERMISSION_DENIED;
Mikhail Naganov0078ee52021-09-30 23:06:20 +00002111 Int numSurroundFormatsAidl;
Kriti Dang6537def2021-03-02 13:46:59 +01002112 numSurroundFormatsAidl.value =
2113 VALUE_OR_RETURN_STATUS(convertIntegral<int32_t>(*numSurroundFormats));
Mikhail Naganov57bd06f2021-08-10 16:41:54 -07002114 std::vector<AudioFormatDescription> surroundFormatsAidl;
Kriti Dang6537def2021-03-02 13:46:59 +01002115 RETURN_STATUS_IF_ERROR(statusTFromBinderStatus(
2116 aps->getReportedSurroundFormats(&numSurroundFormatsAidl, &surroundFormatsAidl)));
2117
2118 *numSurroundFormats = VALUE_OR_RETURN_STATUS(
2119 convertIntegral<unsigned int>(numSurroundFormatsAidl.value));
2120 RETURN_STATUS_IF_ERROR(
2121 convertRange(surroundFormatsAidl.begin(), surroundFormatsAidl.end(), surroundFormats,
Mikhail Naganovb60bd1b2021-07-15 17:31:43 -07002122 aidl2legacy_AudioFormatDescription_audio_format_t));
Kriti Dang6537def2021-03-02 13:46:59 +01002123 return OK;
2124}
2125
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002126status_t AudioSystem::setSurroundFormatEnabled(audio_format_t audioFormat, bool enabled) {
Andy Hung264fa4c2024-02-21 15:52:12 -08002127 const sp<IAudioPolicyService> aps = get_audio_policy_service();
jiabin81772902018-04-02 17:52:27 -07002128 if (aps == 0) return PERMISSION_DENIED;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002129
Mikhail Naganov57bd06f2021-08-10 16:41:54 -07002130 AudioFormatDescription audioFormatAidl = VALUE_OR_RETURN_STATUS(
Mikhail Naganovb60bd1b2021-07-15 17:31:43 -07002131 legacy2aidl_audio_format_t_AudioFormatDescription(audioFormat));
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002132 return statusTFromBinderStatus(
2133 aps->setSurroundFormatEnabled(audioFormatAidl, enabled));
jiabin81772902018-04-02 17:52:27 -07002134}
2135
Oscar Azucena829d90d2022-01-28 17:17:56 -08002136status_t AudioSystem::setAssistantServicesUids(const std::vector<uid_t>& uids) {
Andy Hung264fa4c2024-02-21 15:52:12 -08002137 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Eric Laurentb78763e2018-10-17 10:08:02 -07002138 if (aps == 0) return PERMISSION_DENIED;
2139
Oscar Azucena829d90d2022-01-28 17:17:56 -08002140 std::vector<int32_t> uidsAidl = VALUE_OR_RETURN_STATUS(
2141 convertContainer<std::vector<int32_t>>(uids, legacy2aidl_uid_t_int32_t));
2142 return statusTFromBinderStatus(aps->setAssistantServicesUids(uidsAidl));
Ahaan Ugalef51ce002021-08-04 16:34:20 -07002143}
2144
Oscar Azucenac2cdda32022-01-31 19:10:39 -08002145status_t AudioSystem::setActiveAssistantServicesUids(const std::vector<uid_t>& activeUids) {
Andy Hung264fa4c2024-02-21 15:52:12 -08002146 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Oscar Azucenac2cdda32022-01-31 19:10:39 -08002147 if (aps == 0) return PERMISSION_DENIED;
2148
2149 std::vector<int32_t> activeUidsAidl = VALUE_OR_RETURN_STATUS(
2150 convertContainer<std::vector<int32_t>>(activeUids, legacy2aidl_uid_t_int32_t));
2151 return statusTFromBinderStatus(aps->setActiveAssistantServicesUids(activeUidsAidl));
2152}
2153
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002154status_t AudioSystem::setA11yServicesUids(const std::vector<uid_t>& uids) {
Andy Hung264fa4c2024-02-21 15:52:12 -08002155 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Eric Laurentb78763e2018-10-17 10:08:02 -07002156 if (aps == 0) return PERMISSION_DENIED;
2157
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002158 std::vector<int32_t> uidsAidl = VALUE_OR_RETURN_STATUS(
2159 convertContainer<std::vector<int32_t>>(uids, legacy2aidl_uid_t_int32_t));
2160 return statusTFromBinderStatus(aps->setA11yServicesUids(uidsAidl));
Eric Laurentb78763e2018-10-17 10:08:02 -07002161}
2162
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002163status_t AudioSystem::setCurrentImeUid(uid_t uid) {
Andy Hung264fa4c2024-02-21 15:52:12 -08002164 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Kohsuke Yatoha623a132020-03-24 20:10:26 -07002165 if (aps == 0) return PERMISSION_DENIED;
2166
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002167 int32_t uidAidl = VALUE_OR_RETURN_STATUS(legacy2aidl_uid_t_int32_t(uid));
2168 return statusTFromBinderStatus(aps->setCurrentImeUid(uidAidl));
Kohsuke Yatoha623a132020-03-24 20:10:26 -07002169}
2170
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002171bool AudioSystem::isHapticPlaybackSupported() {
Andy Hung264fa4c2024-02-21 15:52:12 -08002172 const sp<IAudioPolicyService> aps = get_audio_policy_service();
jiabin6012f912018-11-02 17:06:30 -07002173 if (aps == 0) return false;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002174
2175 auto result = [&]() -> ConversionResult<bool> {
2176 bool retVal;
2177 RETURN_IF_ERROR(
2178 statusTFromBinderStatus(aps->isHapticPlaybackSupported(&retVal)));
2179 return retVal;
2180 }();
2181 return result.value_or(false);
jiabin6012f912018-11-02 17:06:30 -07002182}
2183
Carter Hsu325a8eb2022-01-19 19:56:51 +08002184bool AudioSystem::isUltrasoundSupported() {
Andy Hung264fa4c2024-02-21 15:52:12 -08002185 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Carter Hsu325a8eb2022-01-19 19:56:51 +08002186 if (aps == 0) return false;
2187
2188 auto result = [&]() -> ConversionResult<bool> {
2189 bool retVal;
2190 RETURN_IF_ERROR(
2191 statusTFromBinderStatus(aps->isUltrasoundSupported(&retVal)));
2192 return retVal;
2193 }();
2194 return result.value_or(false);
2195}
2196
Pattydd807582021-11-04 21:01:03 +08002197status_t AudioSystem::getHwOffloadFormatsSupportedForBluetoothMedia(
2198 audio_devices_t device, std::vector<audio_format_t>* formats) {
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002199 if (formats == nullptr) {
2200 return BAD_VALUE;
2201 }
2202
Andy Hung264fa4c2024-02-21 15:52:12 -08002203 const sp<IAudioPolicyService> aps = get_audio_policy_service();
François Gaffied0ba9ed2018-11-05 11:50:42 +01002204 if (aps == 0) return PERMISSION_DENIED;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002205
Mikhail Naganov57bd06f2021-08-10 16:41:54 -07002206 std::vector<AudioFormatDescription> formatsAidl;
Pattydd807582021-11-04 21:01:03 +08002207 AudioDeviceDescription deviceAidl = VALUE_OR_RETURN_STATUS(
2208 legacy2aidl_audio_devices_t_AudioDeviceDescription(device));
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002209 RETURN_STATUS_IF_ERROR(statusTFromBinderStatus(
Pattydd807582021-11-04 21:01:03 +08002210 aps->getHwOffloadFormatsSupportedForBluetoothMedia(deviceAidl, &formatsAidl)));
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002211 *formats = VALUE_OR_RETURN_STATUS(
Mikhail Naganovb60bd1b2021-07-15 17:31:43 -07002212 convertContainer<std::vector<audio_format_t>>(
2213 formatsAidl,
2214 aidl2legacy_AudioFormatDescription_audio_format_t));
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002215 return OK;
François Gaffied0ba9ed2018-11-05 11:50:42 +01002216}
2217
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002218status_t AudioSystem::listAudioProductStrategies(AudioProductStrategyVector& strategies) {
Andy Hung264fa4c2024-02-21 15:52:12 -08002219 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Arun Mirpuri11029ad2018-12-19 20:45:19 -08002220 if (aps == 0) return PERMISSION_DENIED;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002221
2222 std::vector<media::AudioProductStrategy> strategiesAidl;
2223 RETURN_STATUS_IF_ERROR(statusTFromBinderStatus(
2224 aps->listAudioProductStrategies(&strategiesAidl)));
2225 strategies = VALUE_OR_RETURN_STATUS(
2226 convertContainer<AudioProductStrategyVector>(strategiesAidl,
2227 aidl2legacy_AudioProductStrategy));
2228 return OK;
François Gaffied0ba9ed2018-11-05 11:50:42 +01002229}
2230
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002231audio_attributes_t AudioSystem::streamTypeToAttributes(audio_stream_type_t stream) {
François Gaffied0ba9ed2018-11-05 11:50:42 +01002232 AudioProductStrategyVector strategies;
2233 listAudioProductStrategies(strategies);
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002234 for (const auto& strategy : strategies) {
François Gaffie1e2b56f2022-04-01 14:34:29 +02002235 auto attrVect = strategy.getVolumeGroupAttributes();
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002236 auto iter = std::find_if(begin(attrVect), end(attrVect), [&stream](const auto& attributes) {
2237 return attributes.getStreamType() == stream;
2238 });
François Gaffied0ba9ed2018-11-05 11:50:42 +01002239 if (iter != end(attrVect)) {
2240 return iter->getAttributes();
2241 }
2242 }
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002243 ALOGE("invalid stream type %s when converting to attributes", toString(stream).c_str());
François Gaffied0ba9ed2018-11-05 11:50:42 +01002244 return AUDIO_ATTRIBUTES_INITIALIZER;
2245}
2246
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002247audio_stream_type_t AudioSystem::attributesToStreamType(const audio_attributes_t& attr) {
François Gaffie4b2018b2018-11-07 11:18:59 +01002248 product_strategy_t psId;
François Gaffie1e2b56f2022-04-01 14:34:29 +02002249 status_t ret = AudioSystem::getProductStrategyFromAudioAttributes(attr, psId);
François Gaffie4b2018b2018-11-07 11:18:59 +01002250 if (ret != NO_ERROR) {
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002251 ALOGE("no strategy found for attributes %s", toString(attr).c_str());
François Gaffie4b2018b2018-11-07 11:18:59 +01002252 return AUDIO_STREAM_MUSIC;
2253 }
François Gaffied0ba9ed2018-11-05 11:50:42 +01002254 AudioProductStrategyVector strategies;
2255 listAudioProductStrategies(strategies);
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002256 for (const auto& strategy : strategies) {
François Gaffie4b2018b2018-11-07 11:18:59 +01002257 if (strategy.getId() == psId) {
François Gaffie1e2b56f2022-04-01 14:34:29 +02002258 auto attrVect = strategy.getVolumeGroupAttributes();
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002259 auto iter = std::find_if(begin(attrVect), end(attrVect), [&attr](const auto& refAttr) {
François Gaffie8d7fd5a2023-01-17 17:28:09 +01002260 return refAttr.matchesScore(attr) > 0;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002261 });
François Gaffied0ba9ed2018-11-05 11:50:42 +01002262 if (iter != end(attrVect)) {
2263 return iter->getStreamType();
2264 }
2265 }
2266 }
Jean-Michel Trivied678652019-12-19 13:39:30 -08002267 switch (attr.usage) {
2268 case AUDIO_USAGE_VIRTUAL_SOURCE:
2269 // virtual source is not expected to have an associated product strategy
2270 break;
2271 default:
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002272 ALOGE("invalid attributes %s when converting to stream", toString(attr).c_str());
Jean-Michel Trivied678652019-12-19 13:39:30 -08002273 break;
2274 }
François Gaffied0ba9ed2018-11-05 11:50:42 +01002275 return AUDIO_STREAM_MUSIC;
2276}
2277
François Gaffie1e2b56f2022-04-01 14:34:29 +02002278status_t AudioSystem::getProductStrategyFromAudioAttributes(const audio_attributes_t& aa,
Francois Gaffie11b65922020-09-24 16:59:08 +02002279 product_strategy_t& productStrategy,
2280 bool fallbackOnDefault) {
Andy Hung264fa4c2024-02-21 15:52:12 -08002281 const sp<IAudioPolicyService> aps = get_audio_policy_service();
François Gaffie4b2018b2018-11-07 11:18:59 +01002282 if (aps == 0) return PERMISSION_DENIED;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002283
Mikhail Naganov1c400902023-05-17 11:48:43 -07002284 media::audio::common::AudioAttributes aaAidl = VALUE_OR_RETURN_STATUS(
2285 legacy2aidl_audio_attributes_t_AudioAttributes(aa));
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002286 int32_t productStrategyAidl;
2287
2288 RETURN_STATUS_IF_ERROR(statusTFromBinderStatus(
Francois Gaffie11b65922020-09-24 16:59:08 +02002289 aps->getProductStrategyFromAudioAttributes(aaAidl, fallbackOnDefault,
2290 &productStrategyAidl)));
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002291 productStrategy = VALUE_OR_RETURN_STATUS(
2292 aidl2legacy_int32_t_product_strategy_t(productStrategyAidl));
2293 return OK;
François Gaffie4b2018b2018-11-07 11:18:59 +01002294}
2295
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002296status_t AudioSystem::listAudioVolumeGroups(AudioVolumeGroupVector& groups) {
Andy Hung264fa4c2024-02-21 15:52:12 -08002297 const sp<IAudioPolicyService> aps = get_audio_policy_service();
François Gaffie4b2018b2018-11-07 11:18:59 +01002298 if (aps == 0) return PERMISSION_DENIED;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002299
2300 std::vector<media::AudioVolumeGroup> groupsAidl;
2301 RETURN_STATUS_IF_ERROR(
2302 statusTFromBinderStatus(aps->listAudioVolumeGroups(&groupsAidl)));
2303 groups = VALUE_OR_RETURN_STATUS(
2304 convertContainer<AudioVolumeGroupVector>(groupsAidl, aidl2legacy_AudioVolumeGroup));
2305 return OK;
François Gaffie4b2018b2018-11-07 11:18:59 +01002306}
2307
François Gaffie1e2b56f2022-04-01 14:34:29 +02002308status_t AudioSystem::getVolumeGroupFromAudioAttributes(const audio_attributes_t &aa,
Francois Gaffie11b65922020-09-24 16:59:08 +02002309 volume_group_t& volumeGroup,
2310 bool fallbackOnDefault) {
Andy Hung264fa4c2024-02-21 15:52:12 -08002311 const sp<IAudioPolicyService> aps = get_audio_policy_service();
François Gaffie4b2018b2018-11-07 11:18:59 +01002312 if (aps == 0) return PERMISSION_DENIED;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002313
Mikhail Naganov1c400902023-05-17 11:48:43 -07002314 media::audio::common::AudioAttributes aaAidl = VALUE_OR_RETURN_STATUS(
2315 legacy2aidl_audio_attributes_t_AudioAttributes(aa));
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002316 int32_t volumeGroupAidl;
2317 RETURN_STATUS_IF_ERROR(statusTFromBinderStatus(
Francois Gaffie11b65922020-09-24 16:59:08 +02002318 aps->getVolumeGroupFromAudioAttributes(aaAidl, fallbackOnDefault, &volumeGroupAidl)));
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002319 volumeGroup = VALUE_OR_RETURN_STATUS(aidl2legacy_int32_t_volume_group_t(volumeGroupAidl));
2320 return OK;
Arun Mirpuri11029ad2018-12-19 20:45:19 -08002321}
Eric Laurentb78763e2018-10-17 10:08:02 -07002322
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002323status_t AudioSystem::setRttEnabled(bool enabled) {
Andy Hung264fa4c2024-02-21 15:52:12 -08002324 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Eric Laurent6ede98f2019-06-11 14:50:30 -07002325 if (aps == 0) return PERMISSION_DENIED;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002326 return statusTFromBinderStatus(aps->setRttEnabled(enabled));
Eric Laurent6ede98f2019-06-11 14:50:30 -07002327}
2328
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002329bool AudioSystem::isCallScreenModeSupported() {
Andy Hung264fa4c2024-02-21 15:52:12 -08002330 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Eric Laurent8340e672019-11-06 11:01:08 -08002331 if (aps == 0) return false;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002332
2333 auto result = [&]() -> ConversionResult<bool> {
2334 bool retAidl;
2335 RETURN_IF_ERROR(
2336 statusTFromBinderStatus(aps->isCallScreenModeSupported(&retAidl)));
2337 return retAidl;
2338 }();
2339 return result.value_or(false);
Eric Laurent8340e672019-11-06 11:01:08 -08002340}
2341
jiabin0a488932020-08-07 17:32:40 -07002342status_t AudioSystem::setDevicesRoleForStrategy(product_strategy_t strategy,
2343 device_role_t role,
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002344 const AudioDeviceTypeAddrVector& devices) {
Andy Hung264fa4c2024-02-21 15:52:12 -08002345 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Jean-Michel Trivi30857152019-11-01 11:04:15 -07002346 if (aps == 0) {
2347 return PERMISSION_DENIED;
2348 }
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002349
2350 int32_t strategyAidl = VALUE_OR_RETURN_STATUS(legacy2aidl_product_strategy_t_int32_t(strategy));
2351 media::DeviceRole roleAidl = VALUE_OR_RETURN_STATUS(legacy2aidl_device_role_t_DeviceRole(role));
Mikhail Naganovf4a75362021-09-16 00:02:54 +00002352 std::vector<AudioDevice> devicesAidl = VALUE_OR_RETURN_STATUS(
2353 convertContainer<std::vector<AudioDevice>>(devices,
2354 legacy2aidl_AudioDeviceTypeAddress));
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002355 return statusTFromBinderStatus(
2356 aps->setDevicesRoleForStrategy(strategyAidl, roleAidl, devicesAidl));
Jean-Michel Trivi30857152019-11-01 11:04:15 -07002357}
2358
Paul Wang5d7cdb52022-11-22 09:45:06 +00002359status_t AudioSystem::removeDevicesRoleForStrategy(product_strategy_t strategy,
2360 device_role_t role,
2361 const AudioDeviceTypeAddrVector& devices) {
Andy Hung264fa4c2024-02-21 15:52:12 -08002362 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Paul Wang5d7cdb52022-11-22 09:45:06 +00002363 if (aps == 0) {
2364 return PERMISSION_DENIED;
2365 }
2366
2367 int32_t strategyAidl = VALUE_OR_RETURN_STATUS(legacy2aidl_product_strategy_t_int32_t(strategy));
2368 media::DeviceRole roleAidl = VALUE_OR_RETURN_STATUS(legacy2aidl_device_role_t_DeviceRole(role));
2369 std::vector<AudioDevice> devicesAidl = VALUE_OR_RETURN_STATUS(
2370 convertContainer<std::vector<AudioDevice>>(devices,
2371 legacy2aidl_AudioDeviceTypeAddress));
2372 return statusTFromBinderStatus(
2373 aps->removeDevicesRoleForStrategy(strategyAidl, roleAidl, devicesAidl));
2374}
2375
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002376status_t
Paul Wang5d7cdb52022-11-22 09:45:06 +00002377AudioSystem::clearDevicesRoleForStrategy(product_strategy_t strategy, device_role_t role) {
Andy Hung264fa4c2024-02-21 15:52:12 -08002378 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Jean-Michel Trivi30857152019-11-01 11:04:15 -07002379 if (aps == 0) {
2380 return PERMISSION_DENIED;
2381 }
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002382 int32_t strategyAidl = VALUE_OR_RETURN_STATUS(legacy2aidl_product_strategy_t_int32_t(strategy));
2383 media::DeviceRole roleAidl = VALUE_OR_RETURN_STATUS(legacy2aidl_device_role_t_DeviceRole(role));
2384 return statusTFromBinderStatus(
Paul Wang5d7cdb52022-11-22 09:45:06 +00002385 aps->clearDevicesRoleForStrategy(strategyAidl, roleAidl));
Jean-Michel Trivi30857152019-11-01 11:04:15 -07002386}
2387
jiabin0a488932020-08-07 17:32:40 -07002388status_t AudioSystem::getDevicesForRoleAndStrategy(product_strategy_t strategy,
2389 device_role_t role,
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002390 AudioDeviceTypeAddrVector& devices) {
Andy Hung264fa4c2024-02-21 15:52:12 -08002391 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Jean-Michel Trivi30857152019-11-01 11:04:15 -07002392 if (aps == 0) {
2393 return PERMISSION_DENIED;
2394 }
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002395 int32_t strategyAidl = VALUE_OR_RETURN_STATUS(legacy2aidl_product_strategy_t_int32_t(strategy));
2396 media::DeviceRole roleAidl = VALUE_OR_RETURN_STATUS(legacy2aidl_device_role_t_DeviceRole(role));
Mikhail Naganovf4a75362021-09-16 00:02:54 +00002397 std::vector<AudioDevice> devicesAidl;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002398 RETURN_STATUS_IF_ERROR(statusTFromBinderStatus(
2399 aps->getDevicesForRoleAndStrategy(strategyAidl, roleAidl, &devicesAidl)));
2400 devices = VALUE_OR_RETURN_STATUS(
2401 convertContainer<AudioDeviceTypeAddrVector>(devicesAidl,
2402 aidl2legacy_AudioDeviceTypeAddress));
2403 return OK;
Jean-Michel Trivi30857152019-11-01 11:04:15 -07002404}
2405
Jiabin Huang3b98d322020-09-03 17:54:16 +00002406status_t AudioSystem::setDevicesRoleForCapturePreset(audio_source_t audioSource,
2407 device_role_t role,
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002408 const AudioDeviceTypeAddrVector& devices) {
Andy Hung264fa4c2024-02-21 15:52:12 -08002409 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Jiabin Huang3b98d322020-09-03 17:54:16 +00002410 if (aps == 0) {
2411 return PERMISSION_DENIED;
2412 }
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002413
Mikhail Naganovddceecc2021-09-03 13:58:56 -07002414 AudioSource audioSourceAidl = VALUE_OR_RETURN_STATUS(
2415 legacy2aidl_audio_source_t_AudioSource(audioSource));
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002416 media::DeviceRole roleAidl = VALUE_OR_RETURN_STATUS(legacy2aidl_device_role_t_DeviceRole(role));
Mikhail Naganovf4a75362021-09-16 00:02:54 +00002417 std::vector<AudioDevice> devicesAidl = VALUE_OR_RETURN_STATUS(
2418 convertContainer<std::vector<AudioDevice>>(devices,
2419 legacy2aidl_AudioDeviceTypeAddress));
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002420 return statusTFromBinderStatus(
2421 aps->setDevicesRoleForCapturePreset(audioSourceAidl, roleAidl, devicesAidl));
Jiabin Huang3b98d322020-09-03 17:54:16 +00002422}
2423
2424status_t AudioSystem::addDevicesRoleForCapturePreset(audio_source_t audioSource,
2425 device_role_t role,
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002426 const AudioDeviceTypeAddrVector& devices) {
Andy Hung264fa4c2024-02-21 15:52:12 -08002427 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Jiabin Huang3b98d322020-09-03 17:54:16 +00002428 if (aps == 0) {
2429 return PERMISSION_DENIED;
2430 }
Mikhail Naganovddceecc2021-09-03 13:58:56 -07002431 AudioSource audioSourceAidl = VALUE_OR_RETURN_STATUS(
2432 legacy2aidl_audio_source_t_AudioSource(audioSource));
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002433 media::DeviceRole roleAidl = VALUE_OR_RETURN_STATUS(legacy2aidl_device_role_t_DeviceRole(role));
Mikhail Naganovf4a75362021-09-16 00:02:54 +00002434 std::vector<AudioDevice> devicesAidl = VALUE_OR_RETURN_STATUS(
2435 convertContainer<std::vector<AudioDevice>>(devices,
2436 legacy2aidl_AudioDeviceTypeAddress));
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002437 return statusTFromBinderStatus(
2438 aps->addDevicesRoleForCapturePreset(audioSourceAidl, roleAidl, devicesAidl));
Jiabin Huang3b98d322020-09-03 17:54:16 +00002439}
2440
2441status_t AudioSystem::removeDevicesRoleForCapturePreset(
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002442 audio_source_t audioSource, device_role_t role, const AudioDeviceTypeAddrVector& devices) {
Andy Hung264fa4c2024-02-21 15:52:12 -08002443 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Jiabin Huang3b98d322020-09-03 17:54:16 +00002444 if (aps == 0) {
2445 return PERMISSION_DENIED;
2446 }
Mikhail Naganovddceecc2021-09-03 13:58:56 -07002447 AudioSource audioSourceAidl = VALUE_OR_RETURN_STATUS(
2448 legacy2aidl_audio_source_t_AudioSource(audioSource));
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002449 media::DeviceRole roleAidl = VALUE_OR_RETURN_STATUS(legacy2aidl_device_role_t_DeviceRole(role));
Mikhail Naganovf4a75362021-09-16 00:02:54 +00002450 std::vector<AudioDevice> devicesAidl = VALUE_OR_RETURN_STATUS(
2451 convertContainer<std::vector<AudioDevice>>(devices,
2452 legacy2aidl_AudioDeviceTypeAddress));
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002453 return statusTFromBinderStatus(
2454 aps->removeDevicesRoleForCapturePreset(audioSourceAidl, roleAidl, devicesAidl));
Jiabin Huang3b98d322020-09-03 17:54:16 +00002455}
2456
2457status_t AudioSystem::clearDevicesRoleForCapturePreset(audio_source_t audioSource,
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002458 device_role_t role) {
Andy Hung264fa4c2024-02-21 15:52:12 -08002459 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Jiabin Huang3b98d322020-09-03 17:54:16 +00002460 if (aps == 0) {
2461 return PERMISSION_DENIED;
2462 }
Mikhail Naganovddceecc2021-09-03 13:58:56 -07002463 AudioSource audioSourceAidl = VALUE_OR_RETURN_STATUS(
2464 legacy2aidl_audio_source_t_AudioSource(audioSource));
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002465 media::DeviceRole roleAidl = VALUE_OR_RETURN_STATUS(legacy2aidl_device_role_t_DeviceRole(role));
2466 return statusTFromBinderStatus(
2467 aps->clearDevicesRoleForCapturePreset(audioSourceAidl, roleAidl));
Jiabin Huang3b98d322020-09-03 17:54:16 +00002468}
2469
2470status_t AudioSystem::getDevicesForRoleAndCapturePreset(audio_source_t audioSource,
2471 device_role_t role,
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002472 AudioDeviceTypeAddrVector& devices) {
Andy Hung264fa4c2024-02-21 15:52:12 -08002473 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Jiabin Huang3b98d322020-09-03 17:54:16 +00002474 if (aps == 0) {
2475 return PERMISSION_DENIED;
2476 }
Mikhail Naganovddceecc2021-09-03 13:58:56 -07002477 AudioSource audioSourceAidl = VALUE_OR_RETURN_STATUS(
2478 legacy2aidl_audio_source_t_AudioSource(audioSource));
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002479 media::DeviceRole roleAidl = VALUE_OR_RETURN_STATUS(legacy2aidl_device_role_t_DeviceRole(role));
Mikhail Naganovf4a75362021-09-16 00:02:54 +00002480 std::vector<AudioDevice> devicesAidl;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002481 RETURN_STATUS_IF_ERROR(statusTFromBinderStatus(
2482 aps->getDevicesForRoleAndCapturePreset(audioSourceAidl, roleAidl, &devicesAidl)));
2483 devices = VALUE_OR_RETURN_STATUS(
2484 convertContainer<AudioDeviceTypeAddrVector>(devicesAidl,
2485 aidl2legacy_AudioDeviceTypeAddress));
2486 return OK;
Jiabin Huang3b98d322020-09-03 17:54:16 +00002487}
2488
Eric Laurent81dd0f52021-07-05 11:54:40 +02002489status_t AudioSystem::getSpatializer(const sp<media::INativeSpatializerCallback>& callback,
2490 sp<media::ISpatializer>* spatializer) {
Andy Hung264fa4c2024-02-21 15:52:12 -08002491 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Eric Laurent81dd0f52021-07-05 11:54:40 +02002492 if (spatializer == nullptr) {
2493 return BAD_VALUE;
2494 }
2495 if (aps == 0) {
2496 return PERMISSION_DENIED;
2497 }
2498 media::GetSpatializerResponse response;
2499 RETURN_STATUS_IF_ERROR(statusTFromBinderStatus(
2500 aps->getSpatializer(callback, &response)));
2501
2502 *spatializer = response.spatializer;
2503 return OK;
2504}
2505
2506status_t AudioSystem::canBeSpatialized(const audio_attributes_t *attr,
2507 const audio_config_t *config,
2508 const AudioDeviceTypeAddrVector &devices,
2509 bool *canBeSpatialized) {
Andy Hung264fa4c2024-02-21 15:52:12 -08002510 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Ram Mohan43297012022-08-30 16:11:49 +05302511 if (canBeSpatialized == nullptr) {
2512 return BAD_VALUE;
2513 }
Eric Laurent81dd0f52021-07-05 11:54:40 +02002514 if (aps == 0) {
2515 return PERMISSION_DENIED;
2516 }
2517 audio_attributes_t attributes = attr != nullptr ? *attr : AUDIO_ATTRIBUTES_INITIALIZER;
2518 audio_config_t configuration = config != nullptr ? *config : AUDIO_CONFIG_INITIALIZER;
2519
Mikhail Naganov1c400902023-05-17 11:48:43 -07002520 std::optional<media::audio::common::AudioAttributes> attrAidl = VALUE_OR_RETURN_STATUS(
2521 legacy2aidl_audio_attributes_t_AudioAttributes(attributes));
Mikhail Naganovdbf03642021-08-25 18:15:32 -07002522 std::optional<AudioConfig> configAidl = VALUE_OR_RETURN_STATUS(
Eric Laurent81dd0f52021-07-05 11:54:40 +02002523 legacy2aidl_audio_config_t_AudioConfig(configuration, false /*isInput*/));
Mikhail Naganovf4a75362021-09-16 00:02:54 +00002524 std::vector<AudioDevice> devicesAidl = VALUE_OR_RETURN_STATUS(
2525 convertContainer<std::vector<AudioDevice>>(devices,
2526 legacy2aidl_AudioDeviceTypeAddress));
Eric Laurent81dd0f52021-07-05 11:54:40 +02002527 RETURN_STATUS_IF_ERROR(statusTFromBinderStatus(
2528 aps->canBeSpatialized(attrAidl, configAidl, devicesAidl, canBeSpatialized)));
2529 return OK;
2530}
2531
Vlad Popae3fd1c22022-11-07 21:03:18 +01002532status_t AudioSystem::getSoundDoseInterface(const sp<media::ISoundDoseCallback>& callback,
2533 sp<media::ISoundDose>* soundDose) {
Andy Hung264fa4c2024-02-21 15:52:12 -08002534 const sp<IAudioFlinger> af = get_audio_flinger();
Vlad Popa63f047e2022-11-05 14:09:19 +01002535 if (af == nullptr) {
2536 return PERMISSION_DENIED;
2537 }
Vlad Popae3fd1c22022-11-07 21:03:18 +01002538 if (soundDose == nullptr) {
2539 return BAD_VALUE;
2540 }
Vlad Popa63f047e2022-11-05 14:09:19 +01002541
Vlad Popae3fd1c22022-11-07 21:03:18 +01002542 RETURN_STATUS_IF_ERROR(af->getSoundDoseInterface(callback, soundDose));
2543 return OK;
Vlad Popa63f047e2022-11-05 14:09:19 +01002544}
2545
jiabin2b9d5a12021-12-10 01:06:29 +00002546status_t AudioSystem::getDirectPlaybackSupport(const audio_attributes_t *attr,
2547 const audio_config_t *config,
2548 audio_direct_mode_t* directMode) {
2549 if (attr == nullptr || config == nullptr || directMode == nullptr) {
2550 return BAD_VALUE;
2551 }
2552
Andy Hung264fa4c2024-02-21 15:52:12 -08002553 const sp<IAudioPolicyService> aps = get_audio_policy_service();
jiabin2b9d5a12021-12-10 01:06:29 +00002554 if (aps == 0) {
2555 return PERMISSION_DENIED;
2556 }
2557
Mikhail Naganov1c400902023-05-17 11:48:43 -07002558 media::audio::common::AudioAttributes attrAidl = VALUE_OR_RETURN_STATUS(
2559 legacy2aidl_audio_attributes_t_AudioAttributes(*attr));
jiabin2b9d5a12021-12-10 01:06:29 +00002560 AudioConfig configAidl = VALUE_OR_RETURN_STATUS(
2561 legacy2aidl_audio_config_t_AudioConfig(*config, false /*isInput*/));
2562
2563 media::AudioDirectMode retAidl;
2564 RETURN_STATUS_IF_ERROR(statusTFromBinderStatus(
2565 aps->getDirectPlaybackSupport(attrAidl, configAidl, &retAidl)));
2566 *directMode = VALUE_OR_RETURN_STATUS(aidl2legacy_int32_t_audio_direct_mode_t_mask(
2567 static_cast<int32_t>(retAidl)));
2568 return NO_ERROR;
2569}
2570
Dorin Drimusf2196d82022-01-03 12:11:18 +01002571status_t AudioSystem::getDirectProfilesForAttributes(const audio_attributes_t* attr,
2572 std::vector<audio_profile>* audioProfiles) {
Mikhail Naganovefc504b2022-06-22 03:24:59 +00002573 if (attr == nullptr || audioProfiles == nullptr) {
Dorin Drimusf2196d82022-01-03 12:11:18 +01002574 return BAD_VALUE;
2575 }
2576
Andy Hung264fa4c2024-02-21 15:52:12 -08002577 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Dorin Drimusf2196d82022-01-03 12:11:18 +01002578 if (aps == 0) {
2579 return PERMISSION_DENIED;
2580 }
2581
Mikhail Naganov1c400902023-05-17 11:48:43 -07002582 media::audio::common::AudioAttributes attrAidl = VALUE_OR_RETURN_STATUS(
2583 legacy2aidl_audio_attributes_t_AudioAttributes(*attr));
Dorin Drimusf2196d82022-01-03 12:11:18 +01002584
2585 std::vector<media::audio::common::AudioProfile> audioProfilesAidl;
2586 RETURN_STATUS_IF_ERROR(statusTFromBinderStatus(
2587 aps->getDirectProfilesForAttributes(attrAidl, &audioProfilesAidl)));
2588 *audioProfiles = VALUE_OR_RETURN_STATUS(convertContainer<std::vector<audio_profile>>(
2589 audioProfilesAidl, aidl2legacy_AudioProfile_audio_profile, false /*isInput*/));
2590
2591 return NO_ERROR;
2592}
Eric Laurent81dd0f52021-07-05 11:54:40 +02002593
Eric Laurent076e7c72022-05-03 18:12:28 +02002594status_t AudioSystem::setRequestedLatencyMode(
2595 audio_io_handle_t output, audio_latency_mode_t mode) {
Andy Hung264fa4c2024-02-21 15:52:12 -08002596 const sp<IAudioFlinger> af = get_audio_flinger();
Eric Laurent076e7c72022-05-03 18:12:28 +02002597 if (af == nullptr) {
2598 return PERMISSION_DENIED;
2599 }
2600 return af->setRequestedLatencyMode(output, mode);
2601}
2602
2603status_t AudioSystem::getSupportedLatencyModes(audio_io_handle_t output,
2604 std::vector<audio_latency_mode_t>* modes) {
Andy Hung264fa4c2024-02-21 15:52:12 -08002605 const sp<IAudioFlinger> af = get_audio_flinger();
Eric Laurent076e7c72022-05-03 18:12:28 +02002606 if (af == nullptr) {
2607 return PERMISSION_DENIED;
2608 }
2609 return af->getSupportedLatencyModes(output, modes);
2610}
2611
Eric Laurent50d72582022-12-20 20:20:23 +01002612status_t AudioSystem::setBluetoothVariableLatencyEnabled(bool enabled) {
Andy Hung264fa4c2024-02-21 15:52:12 -08002613 const sp<IAudioFlinger> af = get_audio_flinger();
Eric Laurent52057642022-12-16 11:45:07 +01002614 if (af == nullptr) {
2615 return PERMISSION_DENIED;
2616 }
Eric Laurent50d72582022-12-20 20:20:23 +01002617 return af->setBluetoothVariableLatencyEnabled(enabled);
Eric Laurent52057642022-12-16 11:45:07 +01002618}
2619
Eric Laurent50d72582022-12-20 20:20:23 +01002620status_t AudioSystem::isBluetoothVariableLatencyEnabled(
2621 bool *enabled) {
Andy Hung264fa4c2024-02-21 15:52:12 -08002622 const sp<IAudioFlinger> af = get_audio_flinger();
Eric Laurent50d72582022-12-20 20:20:23 +01002623 if (af == nullptr) {
2624 return PERMISSION_DENIED;
2625 }
2626 return af->isBluetoothVariableLatencyEnabled(enabled);
2627}
2628
2629status_t AudioSystem::supportsBluetoothVariableLatency(
Eric Laurent52057642022-12-16 11:45:07 +01002630 bool *support) {
Andy Hung264fa4c2024-02-21 15:52:12 -08002631 const sp<IAudioFlinger> af = get_audio_flinger();
Eric Laurent52057642022-12-16 11:45:07 +01002632 if (af == nullptr) {
2633 return PERMISSION_DENIED;
2634 }
Eric Laurent50d72582022-12-20 20:20:23 +01002635 return af->supportsBluetoothVariableLatency(support);
Eric Laurent52057642022-12-16 11:45:07 +01002636}
2637
Mikhail Naganovffd97712023-05-03 17:45:36 -07002638status_t AudioSystem::getAudioPolicyConfig(media::AudioPolicyConfig *config) {
Andy Hung264fa4c2024-02-21 15:52:12 -08002639 const sp<IAudioFlinger> af = get_audio_flinger();
Mikhail Naganovffd97712023-05-03 17:45:36 -07002640 if (af == nullptr) {
2641 return PERMISSION_DENIED;
2642 }
2643 return af->getAudioPolicyConfig(config);
2644}
2645
Ytai Ben-Tsvi5120eda2020-03-26 09:41:15 -07002646class CaptureStateListenerImpl : public media::BnCaptureStateListener,
2647 public IBinder::DeathRecipient {
2648public:
Ytai Ben-Tsvid9f82832020-08-27 09:31:10 -07002649 CaptureStateListenerImpl(
2650 const sp<IAudioPolicyService>& aps,
2651 const sp<AudioSystem::CaptureStateListener>& listener)
Ytai Ben-Tsvia46b6d32020-08-31 13:29:11 -07002652 : mAps(aps), mListener(listener) {}
2653
2654 void init() {
Ytai Ben-Tsvid9f82832020-08-27 09:31:10 -07002655 bool active;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002656 status_t status = statusTFromBinderStatus(
2657 mAps->registerSoundTriggerCaptureStateListener(this, &active));
Ytai Ben-Tsvid9f82832020-08-27 09:31:10 -07002658 if (status != NO_ERROR) {
2659 mListener->onServiceDied();
2660 return;
2661 }
2662 mListener->onStateChanged(active);
Ytai Ben-Tsvia46b6d32020-08-31 13:29:11 -07002663 IInterface::asBinder(mAps)->linkToDeath(this);
Ytai Ben-Tsvid9f82832020-08-27 09:31:10 -07002664 }
2665
Ytai Ben-Tsvi5120eda2020-03-26 09:41:15 -07002666 binder::Status setCaptureState(bool active) override {
Andy Hungacd955a2024-02-16 17:14:36 -08002667 std::lock_guard _l(AudioSystem::gSoundTriggerMutex);
Ytai Ben-Tsvid9f82832020-08-27 09:31:10 -07002668 mListener->onStateChanged(active);
Ytai Ben-Tsvi5120eda2020-03-26 09:41:15 -07002669 return binder::Status::ok();
2670 }
2671
2672 void binderDied(const wp<IBinder>&) override {
Andy Hungacd955a2024-02-16 17:14:36 -08002673 std::lock_guard _l(AudioSystem::gSoundTriggerMutex);
Ytai Ben-Tsvid9f82832020-08-27 09:31:10 -07002674 mListener->onServiceDied();
Andy Hungacd955a2024-02-16 17:14:36 -08002675 AudioSystem::gSoundTriggerCaptureStateListener = nullptr;
Ytai Ben-Tsvi5120eda2020-03-26 09:41:15 -07002676 }
Ytai Ben-Tsvid9f82832020-08-27 09:31:10 -07002677
2678private:
2679 // Need this in order to keep the death receipent alive.
2680 sp<IAudioPolicyService> mAps;
2681 sp<AudioSystem::CaptureStateListener> mListener;
Ytai Ben-Tsvi5120eda2020-03-26 09:41:15 -07002682};
2683
2684status_t AudioSystem::registerSoundTriggerCaptureStateListener(
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002685 const sp<CaptureStateListener>& listener) {
Ytai Ben-Tsvid9f82832020-08-27 09:31:10 -07002686 LOG_ALWAYS_FATAL_IF(listener == nullptr);
2687
Andy Hung264fa4c2024-02-21 15:52:12 -08002688 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Ytai Ben-Tsvi5120eda2020-03-26 09:41:15 -07002689 if (aps == 0) {
2690 return PERMISSION_DENIED;
2691 }
2692
Andy Hungacd955a2024-02-16 17:14:36 -08002693 std::lock_guard _l(AudioSystem::gSoundTriggerMutex);
Ytai Ben-Tsvid9f82832020-08-27 09:31:10 -07002694 gSoundTriggerCaptureStateListener = new CaptureStateListenerImpl(aps, listener);
Ytai Ben-Tsvia46b6d32020-08-31 13:29:11 -07002695 gSoundTriggerCaptureStateListener->init();
Ytai Ben-Tsvi5120eda2020-03-26 09:41:15 -07002696
Ytai Ben-Tsvi5120eda2020-03-26 09:41:15 -07002697 return NO_ERROR;
2698}
2699
jiabin1319f5a2021-03-30 22:21:24 +00002700status_t AudioSystem::setVibratorInfos(
2701 const std::vector<media::AudioVibratorInfo>& vibratorInfos) {
Andy Hung264fa4c2024-02-21 15:52:12 -08002702 const sp<IAudioFlinger> af = get_audio_flinger();
jiabin1319f5a2021-03-30 22:21:24 +00002703 if (af == nullptr) {
2704 return PERMISSION_DENIED;
2705 }
2706 return af->setVibratorInfos(vibratorInfos);
2707}
2708
Jiabin Huangebe64102021-09-07 20:01:07 +00002709status_t AudioSystem::getMmapPolicyInfo(
jiabine99d0882021-09-17 05:21:25 +00002710 AudioMMapPolicyType policyType, std::vector<AudioMMapPolicyInfo> *policyInfos) {
Andy Hung264fa4c2024-02-21 15:52:12 -08002711 const sp<IAudioFlinger> af = get_audio_flinger();
Jiabin Huangebe64102021-09-07 20:01:07 +00002712 if (af == nullptr) {
2713 return PERMISSION_DENIED;
2714 }
2715 return af->getMmapPolicyInfos(policyType, policyInfos);
2716}
2717
jiabine504e7b2021-09-18 00:27:08 +00002718int32_t AudioSystem::getAAudioMixerBurstCount() {
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->getAAudioMixerBurstCount();
2724}
2725
2726int32_t AudioSystem::getAAudioHardwareBurstMinUsec() {
Andy Hung264fa4c2024-02-21 15:52:12 -08002727 const sp<IAudioFlinger> af = get_audio_flinger();
jiabine504e7b2021-09-18 00:27:08 +00002728 if (af == nullptr) {
2729 return PERMISSION_DENIED;
2730 }
2731 return af->getAAudioHardwareBurstMinUsec();
2732}
2733
jiabina84c3d32022-12-02 18:59:55 +00002734status_t AudioSystem::getSupportedMixerAttributes(
2735 audio_port_handle_t portId, std::vector<audio_mixer_attributes_t> *mixerAttrs) {
Andy Hung264fa4c2024-02-21 15:52:12 -08002736 const sp<IAudioPolicyService> aps = get_audio_policy_service();
jiabina84c3d32022-12-02 18:59:55 +00002737 if (aps == nullptr) {
2738 return PERMISSION_DENIED;
2739 }
2740
2741 int32_t portIdAidl = VALUE_OR_RETURN_STATUS(legacy2aidl_audio_port_handle_t_int32_t(portId));
2742 std::vector<media::AudioMixerAttributesInternal> _aidlReturn;
2743 RETURN_STATUS_IF_ERROR(statusTFromBinderStatus(
2744 aps->getSupportedMixerAttributes(portIdAidl, &_aidlReturn)));
2745 *mixerAttrs = VALUE_OR_RETURN_STATUS(
2746 convertContainer<std::vector<audio_mixer_attributes_t>>(
2747 _aidlReturn,
2748 aidl2legacy_AudioMixerAttributesInternal_audio_mixer_attributes_t));
2749 return OK;
2750}
2751
2752status_t AudioSystem::setPreferredMixerAttributes(const audio_attributes_t *attr,
2753 audio_port_handle_t portId,
2754 uid_t uid,
2755 const audio_mixer_attributes_t *mixerAttr) {
Andy Hung264fa4c2024-02-21 15:52:12 -08002756 const sp<IAudioPolicyService> aps = get_audio_policy_service();
jiabina84c3d32022-12-02 18:59:55 +00002757 if (aps == nullptr) {
2758 return PERMISSION_DENIED;
2759 }
2760
Mikhail Naganov1c400902023-05-17 11:48:43 -07002761 media::audio::common::AudioAttributes attrAidl = VALUE_OR_RETURN_STATUS(
2762 legacy2aidl_audio_attributes_t_AudioAttributes(*attr));
jiabina84c3d32022-12-02 18:59:55 +00002763 media::AudioMixerAttributesInternal mixerAttrAidl = VALUE_OR_RETURN_STATUS(
2764 legacy2aidl_audio_mixer_attributes_t_AudioMixerAttributesInternal(*mixerAttr));
2765 int32_t uidAidl = VALUE_OR_RETURN_STATUS(legacy2aidl_uid_t_int32_t(uid));
2766 int32_t portIdAidl = VALUE_OR_RETURN_STATUS(legacy2aidl_audio_port_handle_t_int32_t(portId));
2767
2768 return statusTFromBinderStatus(
2769 aps->setPreferredMixerAttributes(attrAidl, portIdAidl, uidAidl, mixerAttrAidl));
2770}
2771
2772status_t AudioSystem::getPreferredMixerAttributes(
2773 const audio_attributes_t *attr,
2774 audio_port_handle_t portId,
2775 std::optional<audio_mixer_attributes_t> *mixerAttr) {
Andy Hung264fa4c2024-02-21 15:52:12 -08002776 const sp<IAudioPolicyService> aps = get_audio_policy_service();
jiabina84c3d32022-12-02 18:59:55 +00002777 if (aps == nullptr) {
2778 return PERMISSION_DENIED;
2779 }
2780
Mikhail Naganov1c400902023-05-17 11:48:43 -07002781 media::audio::common::AudioAttributes attrAidl = VALUE_OR_RETURN_STATUS(
2782 legacy2aidl_audio_attributes_t_AudioAttributes(*attr));
jiabina84c3d32022-12-02 18:59:55 +00002783 int32_t portIdAidl = VALUE_OR_RETURN_STATUS(legacy2aidl_audio_port_handle_t_int32_t(portId));
2784 std::optional<media::AudioMixerAttributesInternal> _aidlReturn;
2785 RETURN_STATUS_IF_ERROR(statusTFromBinderStatus(
2786 aps->getPreferredMixerAttributes(attrAidl, portIdAidl, &_aidlReturn)));
2787
2788 if (_aidlReturn.has_value()) {
2789 *mixerAttr = VALUE_OR_RETURN_STATUS(
2790 aidl2legacy_AudioMixerAttributesInternal_audio_mixer_attributes_t(
2791 _aidlReturn.value()));
2792 }
2793 return NO_ERROR;
2794}
2795
2796status_t AudioSystem::clearPreferredMixerAttributes(const audio_attributes_t *attr,
2797 audio_port_handle_t portId,
2798 uid_t uid) {
Andy Hung264fa4c2024-02-21 15:52:12 -08002799 const sp<IAudioPolicyService> aps = get_audio_policy_service();
jiabina84c3d32022-12-02 18:59:55 +00002800 if (aps == nullptr) {
2801 return PERMISSION_DENIED;
2802 }
2803
Mikhail Naganov1c400902023-05-17 11:48:43 -07002804 media::audio::common::AudioAttributes attrAidl = VALUE_OR_RETURN_STATUS(
2805 legacy2aidl_audio_attributes_t_AudioAttributes(*attr));
jiabina84c3d32022-12-02 18:59:55 +00002806 int32_t uidAidl = VALUE_OR_RETURN_STATUS(legacy2aidl_uid_t_int32_t(uid));
2807 int32_t portIdAidl = VALUE_OR_RETURN_STATUS(legacy2aidl_audio_port_handle_t_int32_t(portId));
2808 return statusTFromBinderStatus(
2809 aps->clearPreferredMixerAttributes(attrAidl, portIdAidl, uidAidl));
2810}
2811
Eric Laurentc2f1f072009-07-17 12:17:14 -07002812// ---------------------------------------------------------------------------
2813
Eric Laurente8726fe2015-06-26 09:39:24 -07002814int AudioSystem::AudioPolicyServiceClient::addAudioPortCallback(
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002815 const sp<AudioPortCallback>& callback) {
Andy Hungacd955a2024-02-16 17:14:36 -08002816 std::lock_guard _l(mMutex);
Andy Hungd211ce02024-02-16 15:34:07 -08002817 return mAudioPortCallbacks.insert(callback).second ? mAudioPortCallbacks.size() : -1;
Eric Laurentb28753e2015-04-01 13:06:28 -07002818}
2819
Eric Laurente8726fe2015-06-26 09:39:24 -07002820int AudioSystem::AudioPolicyServiceClient::removeAudioPortCallback(
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002821 const sp<AudioPortCallback>& callback) {
Andy Hungacd955a2024-02-16 17:14:36 -08002822 std::lock_guard _l(mMutex);
Andy Hungd211ce02024-02-16 15:34:07 -08002823 return mAudioPortCallbacks.erase(callback) > 0 ? mAudioPortCallbacks.size() : -1;
Eric Laurentb28753e2015-04-01 13:06:28 -07002824}
2825
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002826Status AudioSystem::AudioPolicyServiceClient::onAudioPortListUpdate() {
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->onAudioPortListUpdate();
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
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002834Status AudioSystem::AudioPolicyServiceClient::onAudioPatchListUpdate() {
Andy Hungacd955a2024-02-16 17:14:36 -08002835 std::lock_guard _l(mMutex);
Andy Hungd211ce02024-02-16 15:34:07 -08002836 for (const auto& callback : mAudioPortCallbacks) {
2837 callback->onAudioPatchListUpdate();
Eric Laurentb28753e2015-04-01 13:06:28 -07002838 }
Ytai Ben-Tsvi7e7a79d2020-12-15 16:48:16 -08002839 return Status::ok();
Eric Laurentb28753e2015-04-01 13:06:28 -07002840}
2841
François Gaffiecfe17322018-11-07 13:41:29 +01002842// ----------------------------------------------------------------------------
2843int AudioSystem::AudioPolicyServiceClient::addAudioVolumeGroupCallback(
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002844 const sp<AudioVolumeGroupCallback>& callback) {
Andy Hungacd955a2024-02-16 17:14:36 -08002845 std::lock_guard _l(mMutex);
Andy Hungd211ce02024-02-16 15:34:07 -08002846 return mAudioVolumeGroupCallbacks.insert(callback).second
2847 ? mAudioVolumeGroupCallbacks.size() : -1;
François Gaffiecfe17322018-11-07 13:41:29 +01002848}
2849
2850int AudioSystem::AudioPolicyServiceClient::removeAudioVolumeGroupCallback(
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002851 const sp<AudioVolumeGroupCallback>& callback) {
Andy Hungacd955a2024-02-16 17:14:36 -08002852 std::lock_guard _l(mMutex);
Andy Hungd211ce02024-02-16 15:34:07 -08002853 return mAudioVolumeGroupCallbacks.erase(callback) > 0
2854 ? mAudioVolumeGroupCallbacks.size() : -1;
François Gaffiecfe17322018-11-07 13:41:29 +01002855}
2856
Ytai Ben-Tsvi7e7a79d2020-12-15 16:48:16 -08002857Status AudioSystem::AudioPolicyServiceClient::onAudioVolumeGroupChanged(int32_t group,
2858 int32_t flags) {
2859 volume_group_t groupLegacy = VALUE_OR_RETURN_BINDER_STATUS(
2860 aidl2legacy_int32_t_volume_group_t(group));
2861 int flagsLegacy = VALUE_OR_RETURN_BINDER_STATUS(convertReinterpret<int>(flags));
2862
Andy Hungacd955a2024-02-16 17:14:36 -08002863 std::lock_guard _l(mMutex);
Andy Hungd211ce02024-02-16 15:34:07 -08002864 for (const auto& callback : mAudioVolumeGroupCallbacks) {
2865 callback->onAudioVolumeGroupChanged(groupLegacy, flagsLegacy);
François Gaffiecfe17322018-11-07 13:41:29 +01002866 }
Ytai Ben-Tsvi7e7a79d2020-12-15 16:48:16 -08002867 return Status::ok();
François Gaffiecfe17322018-11-07 13:41:29 +01002868}
2869// ----------------------------------------------------------------------------
2870
Ytai Ben-Tsvi7e7a79d2020-12-15 16:48:16 -08002871Status AudioSystem::AudioPolicyServiceClient::onDynamicPolicyMixStateUpdate(
2872 const ::std::string& regId, int32_t state) {
2873 ALOGV("AudioPolicyServiceClient::onDynamicPolicyMixStateUpdate(%s, %d)", regId.c_str(), state);
2874
2875 String8 regIdLegacy = VALUE_OR_RETURN_BINDER_STATUS(aidl2legacy_string_view_String8(regId));
2876 int stateLegacy = VALUE_OR_RETURN_BINDER_STATUS(convertReinterpret<int>(state));
Jean-Michel Trivif613d422015-04-23 18:41:29 -07002877 dynamic_policy_callback cb = NULL;
2878 {
Andy Hungacd955a2024-02-16 17:14:36 -08002879 std::lock_guard _l(AudioSystem::gMutex);
Jean-Michel Trivif613d422015-04-23 18:41:29 -07002880 cb = gDynPolicyCallback;
2881 }
2882
2883 if (cb != NULL) {
Ytai Ben-Tsvi7e7a79d2020-12-15 16:48:16 -08002884 cb(DYNAMIC_POLICY_EVENT_MIX_STATE_UPDATE, regIdLegacy, stateLegacy);
Jean-Michel Trivif613d422015-04-23 18:41:29 -07002885 }
Ytai Ben-Tsvi7e7a79d2020-12-15 16:48:16 -08002886 return Status::ok();
Jean-Michel Trivide801052015-04-14 19:10:14 -07002887}
2888
Ytai Ben-Tsvi7e7a79d2020-12-15 16:48:16 -08002889Status AudioSystem::AudioPolicyServiceClient::onRecordingConfigurationUpdate(
2890 int32_t event,
2891 const media::RecordClientInfo& clientInfo,
Mikhail Naganovdbf03642021-08-25 18:15:32 -07002892 const AudioConfigBase& clientConfig,
Ytai Ben-Tsvi7e7a79d2020-12-15 16:48:16 -08002893 const std::vector<media::EffectDescriptor>& clientEffects,
Mikhail Naganovdbf03642021-08-25 18:15:32 -07002894 const AudioConfigBase& deviceConfig,
Ytai Ben-Tsvi7e7a79d2020-12-15 16:48:16 -08002895 const std::vector<media::EffectDescriptor>& effects,
2896 int32_t patchHandle,
Mikhail Naganovddceecc2021-09-03 13:58:56 -07002897 AudioSource source) {
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -08002898 record_config_callback cb = NULL;
2899 {
Andy Hungacd955a2024-02-16 17:14:36 -08002900 std::lock_guard _l(AudioSystem::gMutex);
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -08002901 cb = gRecordConfigCallback;
2902 }
2903
2904 if (cb != NULL) {
Ytai Ben-Tsvi7e7a79d2020-12-15 16:48:16 -08002905 int eventLegacy = VALUE_OR_RETURN_BINDER_STATUS(convertReinterpret<int>(event));
2906 record_client_info_t clientInfoLegacy = VALUE_OR_RETURN_BINDER_STATUS(
2907 aidl2legacy_RecordClientInfo_record_client_info_t(clientInfo));
2908 audio_config_base_t clientConfigLegacy = VALUE_OR_RETURN_BINDER_STATUS(
Mikhail Naganovde3fa182021-07-30 15:06:42 -07002909 aidl2legacy_AudioConfigBase_audio_config_base_t(clientConfig, true /*isInput*/));
Ytai Ben-Tsvi7e7a79d2020-12-15 16:48:16 -08002910 std::vector<effect_descriptor_t> clientEffectsLegacy = VALUE_OR_RETURN_BINDER_STATUS(
2911 convertContainer<std::vector<effect_descriptor_t>>(
2912 clientEffects,
2913 aidl2legacy_EffectDescriptor_effect_descriptor_t));
2914 audio_config_base_t deviceConfigLegacy = VALUE_OR_RETURN_BINDER_STATUS(
Mikhail Naganovde3fa182021-07-30 15:06:42 -07002915 aidl2legacy_AudioConfigBase_audio_config_base_t(deviceConfig, true /*isInput*/));
Ytai Ben-Tsvi7e7a79d2020-12-15 16:48:16 -08002916 std::vector<effect_descriptor_t> effectsLegacy = VALUE_OR_RETURN_BINDER_STATUS(
2917 convertContainer<std::vector<effect_descriptor_t>>(
2918 effects,
2919 aidl2legacy_EffectDescriptor_effect_descriptor_t));
2920 audio_patch_handle_t patchHandleLegacy = VALUE_OR_RETURN_BINDER_STATUS(
2921 aidl2legacy_int32_t_audio_patch_handle_t(patchHandle));
2922 audio_source_t sourceLegacy = VALUE_OR_RETURN_BINDER_STATUS(
Mikhail Naganovddceecc2021-09-03 13:58:56 -07002923 aidl2legacy_AudioSource_audio_source_t(source));
Ytai Ben-Tsvi7e7a79d2020-12-15 16:48:16 -08002924 cb(eventLegacy, &clientInfoLegacy, &clientConfigLegacy, clientEffectsLegacy,
2925 &deviceConfigLegacy, effectsLegacy, patchHandleLegacy, sourceLegacy);
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -08002926 }
Ytai Ben-Tsvi7e7a79d2020-12-15 16:48:16 -08002927 return Status::ok();
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -08002928}
2929
Jean-Michel Trivi9a6b9ad2020-10-22 16:46:43 -07002930Status AudioSystem::AudioPolicyServiceClient::onRoutingUpdated() {
2931 routing_callback cb = NULL;
2932 {
Andy Hungacd955a2024-02-16 17:14:36 -08002933 std::lock_guard _l(AudioSystem::gMutex);
Jean-Michel Trivi9a6b9ad2020-10-22 16:46:43 -07002934 cb = gRoutingCallback;
2935 }
2936
2937 if (cb != NULL) {
2938 cb();
2939 }
2940 return Status::ok();
2941}
2942
Jean-Michel Trivi78f2b302022-04-15 18:18:41 +00002943Status AudioSystem::AudioPolicyServiceClient::onVolumeRangeInitRequest() {
2944 vol_range_init_req_callback cb = NULL;
2945 {
Andy Hungacd955a2024-02-16 17:14:36 -08002946 std::lock_guard _l(AudioSystem::gMutex);
Jean-Michel Trivi78f2b302022-04-15 18:18:41 +00002947 cb = gVolRangeInitReqCallback;
2948 }
2949
2950 if (cb != NULL) {
2951 cb();
2952 }
2953 return Status::ok();
2954}
2955
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002956void AudioSystem::AudioPolicyServiceClient::binderDied(const wp<IBinder>& who __unused) {
Glenn Kastend2d089f2014-11-05 11:48:12 -08002957 {
Andy Hungacd955a2024-02-16 17:14:36 -08002958 std::lock_guard _l(mMutex);
Andy Hungd211ce02024-02-16 15:34:07 -08002959 for (const auto& callback : mAudioPortCallbacks) {
2960 callback->onServiceDied();
Glenn Kastend2d089f2014-11-05 11:48:12 -08002961 }
Andy Hungd211ce02024-02-16 15:34:07 -08002962 for (const auto& callback : mAudioVolumeGroupCallbacks) {
2963 callback->onServiceDied();
François Gaffiecfe17322018-11-07 13:41:29 +01002964 }
Eric Laurentb52c1522014-05-20 11:27:36 -07002965 }
Mikhail Naganovec3d5792022-05-06 00:19:55 +00002966 AudioSystem::clearAudioPolicyService();
Eric Laurentc2f1f072009-07-17 12:17:14 -07002967
Steve Block5ff1dd52012-01-05 23:22:43 +00002968 ALOGW("AudioPolicyService server died!");
Eric Laurentc2f1f072009-07-17 12:17:14 -07002969}
2970
Ytai Ben-Tsvi7e7a79d2020-12-15 16:48:16 -08002971ConversionResult<record_client_info_t>
2972aidl2legacy_RecordClientInfo_record_client_info_t(const media::RecordClientInfo& aidl) {
2973 record_client_info_t legacy;
2974 legacy.riid = VALUE_OR_RETURN(aidl2legacy_int32_t_audio_unique_id_t(aidl.riid));
2975 legacy.uid = VALUE_OR_RETURN(aidl2legacy_int32_t_uid_t(aidl.uid));
2976 legacy.session = VALUE_OR_RETURN(aidl2legacy_int32_t_audio_session_t(aidl.session));
Mikhail Naganovddceecc2021-09-03 13:58:56 -07002977 legacy.source = VALUE_OR_RETURN(aidl2legacy_AudioSource_audio_source_t(aidl.source));
Ytai Ben-Tsvi7e7a79d2020-12-15 16:48:16 -08002978 legacy.port_id = VALUE_OR_RETURN(aidl2legacy_int32_t_audio_port_handle_t(aidl.portId));
2979 legacy.silenced = aidl.silenced;
2980 return legacy;
2981}
2982
2983ConversionResult<media::RecordClientInfo>
2984legacy2aidl_record_client_info_t_RecordClientInfo(const record_client_info_t& legacy) {
2985 media::RecordClientInfo aidl;
2986 aidl.riid = VALUE_OR_RETURN(legacy2aidl_audio_unique_id_t_int32_t(legacy.riid));
2987 aidl.uid = VALUE_OR_RETURN(legacy2aidl_uid_t_int32_t(legacy.uid));
2988 aidl.session = VALUE_OR_RETURN(legacy2aidl_audio_session_t_int32_t(legacy.session));
Mikhail Naganovddceecc2021-09-03 13:58:56 -07002989 aidl.source = VALUE_OR_RETURN(legacy2aidl_audio_source_t_AudioSource(legacy.source));
Ytai Ben-Tsvi7e7a79d2020-12-15 16:48:16 -08002990 aidl.portId = VALUE_OR_RETURN(legacy2aidl_audio_port_handle_t_int32_t(legacy.port_id));
2991 aidl.silenced = legacy.silenced;
2992 return aidl;
2993}
2994
Glenn Kasten40bc9062015-03-20 09:09:33 -07002995} // namespace android