blob: 85f4a865c1cf706ff81e76a82fe28350f1aad93f [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;
Priyanka Advani (xWF)46555052024-11-20 18:24:09 +000063using media::audio::common::AudioPolicyForceUse;
64using media::audio::common::AudioPolicyForcedConfig;
Mikhail Naganovddceecc2021-09-03 13:58:56 -070065using media::audio::common::AudioSource;
Mikhail Naganovdbf03642021-08-25 18:15:32 -070066using media::audio::common::AudioStreamType;
67using media::audio::common::AudioUsage;
Mikhail Naganov0078ee52021-09-30 23:06:20 +000068using media::audio::common::Int;
Ytai Ben-Tsvi10dc0a62020-09-18 11:31:55 -070069
Andy Hungacd955a2024-02-16 17:14:36 -080070std::mutex AudioSystem::gMutex;
Jean-Michel Trivif613d422015-04-23 18:41:29 -070071dynamic_policy_callback AudioSystem::gDynPolicyCallback = NULL;
Svet Ganovf4ddfef2018-01-16 07:37:58 -080072record_config_callback AudioSystem::gRecordConfigCallback = NULL;
Jean-Michel Trivi9a6b9ad2020-10-22 16:46:43 -070073routing_callback AudioSystem::gRoutingCallback = NULL;
Jean-Michel Trivi78f2b302022-04-15 18:18:41 +000074vol_range_init_req_callback AudioSystem::gVolRangeInitReqCallback = NULL;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080075
Andy Hung88e71732024-02-21 16:44:27 -080076std::mutex AudioSystem::gApsCallbackMutex;
Andy Hungacd955a2024-02-16 17:14:36 -080077std::mutex AudioSystem::gErrorCallbacksMutex;
78std::set<audio_error_callback> AudioSystem::gAudioErrorCallbacks;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -080079
Andy Hungacd955a2024-02-16 17:14:36 -080080std::mutex AudioSystem::gSoundTriggerMutex;
81sp<CaptureStateListenerImpl> AudioSystem::gSoundTriggerCaptureStateListener;
Ytai Ben-Tsvi5120eda2020-03-26 09:41:15 -070082
Andy Hungacd955a2024-02-16 17:14:36 -080083// Sets the Binder for the AudioFlinger service, passed to this client process
84// from the system server.
Ahaan Ugale058f94b2021-05-21 02:23:50 -070085// This allows specific isolated processes to access the audio system. Currently used only for the
86// HotwordDetectionService.
Andy Hungda1fb072024-02-20 19:08:08 -080087template <typename ServiceInterface, typename Client, typename AidlInterface,
88 typename ServiceTraits>
89class ServiceHandler {
90public:
Shraddha Basantwanif2aa2502024-04-08 11:01:57 +000091 sp<ServiceInterface> getService()
Andy Hungda1fb072024-02-20 19:08:08 -080092 EXCLUDES(mMutex) NO_THREAD_SAFETY_ANALYSIS { // std::unique_ptr
93 sp<ServiceInterface> service;
94 sp<Client> client;
Ahaan Ugale058f94b2021-05-21 02:23:50 -070095
Andy Hungda1fb072024-02-20 19:08:08 -080096 bool reportNoError = false;
97 {
98 std::lock_guard _l(mMutex);
99 if (mService != nullptr) {
100 return mService;
101 }
Andy Hung66fc0bd2023-01-04 11:45:50 -0800102 }
103
Andy Hungda1fb072024-02-20 19:08:08 -0800104 std::unique_lock ul_only1thread(mSingleGetter);
105 std::unique_lock ul(mMutex);
106 if (mService != nullptr) {
107 return mService;
108 }
109 if (mClient == nullptr) {
110 mClient = sp<Client>::make();
Andy Hung66fc0bd2023-01-04 11:45:50 -0800111 } else {
112 reportNoError = true;
113 }
Andy Hungda1fb072024-02-20 19:08:08 -0800114 while (true) {
115 mService = mLocalService;
116 if (mService != nullptr) break;
Andy Hung66fc0bd2023-01-04 11:45:50 -0800117
Andy Hungda1fb072024-02-20 19:08:08 -0800118 sp<IBinder> binder = mBinder;
119 if (binder == nullptr) {
120 sp <IServiceManager> sm = defaultServiceManager();
121 binder = sm->checkService(String16(ServiceTraits::SERVICE_NAME));
Paul Wangbcf95f62023-02-28 15:38:16 +0000122 if (binder == nullptr) {
Andy Hungda1fb072024-02-20 19:08:08 -0800123 ALOGD("%s: waiting for %s", __func__, ServiceTraits::SERVICE_NAME);
124
125 // if the condition variable is present, setLocalService() and
126 // setBinder() is allowed to use it to notify us.
127 if (mCvGetter == nullptr) {
128 mCvGetter = std::make_shared<std::condition_variable>();
129 }
130 mCvGetter->wait_for(ul, std::chrono::seconds(1));
131 continue;
Paul Wangbcf95f62023-02-28 15:38:16 +0000132 }
Ahaan Ugale058f94b2021-05-21 02:23:50 -0700133 }
Andy Hungda1fb072024-02-20 19:08:08 -0800134 binder->linkToDeath(mClient);
135 auto aidlInterface = interface_cast<AidlInterface>(binder);
136 LOG_ALWAYS_FATAL_IF(aidlInterface == nullptr);
137 if constexpr (std::is_same_v<ServiceInterface, AidlInterface>) {
138 mService = std::move(aidlInterface);
139 } else /* constexpr */ {
140 mService = ServiceTraits::createServiceAdapter(aidlInterface);
141 }
142 break;
Glenn Kastene53b9ea2012-03-12 16:29:55 -0700143 }
Andy Hungda1fb072024-02-20 19:08:08 -0800144 if (mCvGetter) mCvGetter.reset(); // remove condition variable.
145 client = mClient;
146 service = mService;
147 // Make sure callbacks can be received by the client
Shraddha Basantwanif2aa2502024-04-08 11:01:57 +0000148 if (mCanStartThreadPool) {
Akshata Kadam1d486b72023-08-07 05:49:48 +0000149 ProcessState::self()->startThreadPool();
150 }
Andy Hungda1fb072024-02-20 19:08:08 -0800151 ul.unlock();
152 ul_only1thread.unlock();
153 ServiceTraits::onServiceCreate(service, client);
154 if (reportNoError) AudioSystem::reportError(NO_ERROR);
155 return service;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800156 }
Andy Hungda1fb072024-02-20 19:08:08 -0800157
158 status_t setLocalService(const sp<ServiceInterface>& service) EXCLUDES(mMutex) {
159 std::lock_guard _l(mMutex);
160 // we allow clearing once set, but not a double non-null set.
161 if (mService != nullptr && service != nullptr) return INVALID_OPERATION;
162 mLocalService = service;
163 if (mCvGetter) mCvGetter->notify_one();
164 return OK;
165 }
166
167 sp<Client> getClient() EXCLUDES(mMutex) {
168 const auto service = getService();
169 if (service == nullptr) return nullptr;
170 std::lock_guard _l(mMutex);
171 return mClient;
172 }
173
174 void setBinder(const sp<IBinder>& binder) EXCLUDES(mMutex) {
175 std::lock_guard _l(mMutex);
176 if (mService != nullptr) {
177 ALOGW("%s: ignoring; %s connection already established.",
178 __func__, ServiceTraits::SERVICE_NAME);
179 return;
180 }
181 mBinder = binder;
182 if (mCvGetter) mCvGetter->notify_one();
183 }
184
185 void clearService() EXCLUDES(mMutex) {
186 std::lock_guard _l(mMutex);
187 mService.clear();
188 if (mClient) ServiceTraits::onClearService(mClient);
189 }
190
Shraddha Basantwanif2aa2502024-04-08 11:01:57 +0000191 void disableThreadPool() {
192 mCanStartThreadPool = false;
193 }
194
Andy Hungda1fb072024-02-20 19:08:08 -0800195private:
196 std::mutex mSingleGetter;
197 std::mutex mMutex;
198 std::shared_ptr<std::condition_variable> mCvGetter GUARDED_BY(mMutex);
199 sp<IBinder> mBinder GUARDED_BY(mMutex);
200 sp<ServiceInterface> mLocalService GUARDED_BY(mMutex);
201 sp<ServiceInterface> mService GUARDED_BY(mMutex);
202 sp<Client> mClient GUARDED_BY(mMutex);
Shraddha Basantwanif2aa2502024-04-08 11:01:57 +0000203 std::atomic<bool> mCanStartThreadPool = true;
Andy Hungda1fb072024-02-20 19:08:08 -0800204};
205
206struct AudioFlingerTraits {
207 static void onServiceCreate(
208 const sp<IAudioFlinger>& af, const sp<AudioSystem::AudioFlingerClient>& afc) {
209 const int64_t token = IPCThreadState::self()->clearCallingIdentity();
210 af->registerClient(afc);
211 IPCThreadState::self()->restoreCallingIdentity(token);
212 }
213
214 static sp<IAudioFlinger> createServiceAdapter(
215 const sp<media::IAudioFlingerService>& aidlInterface) {
216 return sp<AudioFlingerClientAdapter>::make(aidlInterface);
217 }
218
219 static void onClearService(const sp<AudioSystem::AudioFlingerClient>& afc) {
220 afc->clearIoCache();
221 }
222
223 static constexpr const char* SERVICE_NAME = IAudioFlinger::DEFAULT_SERVICE_NAME;
224};
225
226[[clang::no_destroy]] static constinit ServiceHandler<IAudioFlinger,
227 AudioSystem::AudioFlingerClient, media::IAudioFlingerService,
228 AudioFlingerTraits> gAudioFlingerServiceHandler;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800229
Andy Hung264fa4c2024-02-21 15:52:12 -0800230sp<IAudioFlinger> AudioSystem::get_audio_flinger() {
Andy Hungda1fb072024-02-20 19:08:08 -0800231 return gAudioFlingerServiceHandler.getService();
Akshata Kadam1d486b72023-08-07 05:49:48 +0000232}
233
Andy Hungda1fb072024-02-20 19:08:08 -0800234sp<AudioSystem::AudioFlingerClient> AudioSystem::getAudioFlingerClient() {
235 return gAudioFlingerServiceHandler.getClient();
236}
237
238void AudioSystem::setAudioFlingerBinder(const sp<IBinder>& audioFlinger) {
239 if (audioFlinger->getInterfaceDescriptor() != media::IAudioFlingerService::descriptor) {
240 ALOGE("%s: received a binder of type %s",
241 __func__, String8(audioFlinger->getInterfaceDescriptor()).c_str());
242 return;
243 }
244 gAudioFlingerServiceHandler.setBinder(audioFlinger);
245}
246
247status_t AudioSystem::setLocalAudioFlinger(const sp<IAudioFlinger>& af) {
248 return gAudioFlingerServiceHandler.setLocalService(af);
Eric Laurent296fb132015-05-01 11:38:42 -0700249}
250
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800251sp<AudioIoDescriptor> AudioSystem::getIoDescriptor(audio_io_handle_t ioHandle) {
Eric Laurent296fb132015-05-01 11:38:42 -0700252 sp<AudioIoDescriptor> desc;
253 const sp<AudioFlingerClient> afc = getAudioFlingerClient();
254 if (afc != 0) {
255 desc = afc->getIoDescriptor(ioHandle);
256 }
257 return desc;
258}
259
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800260/* static */ status_t AudioSystem::checkAudioFlinger() {
Eric Laurent46291612013-07-18 14:38:44 -0700261 if (defaultServiceManager()->checkService(String16("media.audio_flinger")) != 0) {
262 return NO_ERROR;
263 }
264 return DEAD_OBJECT;
265}
266
Glenn Kasten4a8308b2016-04-18 14:10:01 -0700267// FIXME Declare in binder opcode order, similarly to IAudioFlinger.h and IAudioFlinger.cpp
268
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800269status_t AudioSystem::muteMicrophone(bool state) {
Andy Hung264fa4c2024-02-21 15:52:12 -0800270 const sp<IAudioFlinger> af = get_audio_flinger();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800271 if (af == 0) return PERMISSION_DENIED;
272 return af->setMicMute(state);
273}
274
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800275status_t AudioSystem::isMicrophoneMuted(bool* state) {
Andy Hung264fa4c2024-02-21 15:52:12 -0800276 const sp<IAudioFlinger> af = get_audio_flinger();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800277 if (af == 0) return PERMISSION_DENIED;
278 *state = af->getMicMute();
279 return NO_ERROR;
280}
281
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800282status_t AudioSystem::setMasterVolume(float value) {
Andy Hung264fa4c2024-02-21 15:52:12 -0800283 const sp<IAudioFlinger> af = get_audio_flinger();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800284 if (af == 0) return PERMISSION_DENIED;
285 af->setMasterVolume(value);
286 return NO_ERROR;
287}
288
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800289status_t AudioSystem::setMasterMute(bool mute) {
Andy Hung264fa4c2024-02-21 15:52:12 -0800290 const sp<IAudioFlinger> af = get_audio_flinger();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800291 if (af == 0) return PERMISSION_DENIED;
292 af->setMasterMute(mute);
293 return NO_ERROR;
294}
295
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800296status_t AudioSystem::getMasterVolume(float* volume) {
Andy Hung264fa4c2024-02-21 15:52:12 -0800297 const sp<IAudioFlinger> af = get_audio_flinger();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800298 if (af == 0) return PERMISSION_DENIED;
299 *volume = af->masterVolume();
300 return NO_ERROR;
301}
302
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800303status_t AudioSystem::getMasterMute(bool* mute) {
Andy Hung264fa4c2024-02-21 15:52:12 -0800304 const sp<IAudioFlinger> af = get_audio_flinger();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800305 if (af == 0) return PERMISSION_DENIED;
306 *mute = af->masterMute();
307 return NO_ERROR;
308}
309
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800310status_t AudioSystem::setStreamVolume(audio_stream_type_t stream, float value,
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800311 audio_io_handle_t output) {
Dima Zavinfce7a472011-04-19 22:30:36 -0700312 if (uint32_t(stream) >= AUDIO_STREAM_CNT) return BAD_VALUE;
Andy Hung264fa4c2024-02-21 15:52:12 -0800313 const sp<IAudioFlinger> af = get_audio_flinger();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800314 if (af == 0) return PERMISSION_DENIED;
Eric Laurentc2f1f072009-07-17 12:17:14 -0700315 af->setStreamVolume(stream, value, output);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800316 return NO_ERROR;
317}
318
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800319status_t AudioSystem::setStreamMute(audio_stream_type_t stream, bool mute) {
Dima Zavinfce7a472011-04-19 22:30:36 -0700320 if (uint32_t(stream) >= AUDIO_STREAM_CNT) return BAD_VALUE;
Andy Hung264fa4c2024-02-21 15:52:12 -0800321 const sp<IAudioFlinger> af = get_audio_flinger();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800322 if (af == 0) return PERMISSION_DENIED;
323 af->setStreamMute(stream, mute);
324 return NO_ERROR;
325}
326
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800327status_t AudioSystem::getStreamVolume(audio_stream_type_t stream, float* volume,
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800328 audio_io_handle_t output) {
Dima Zavinfce7a472011-04-19 22:30:36 -0700329 if (uint32_t(stream) >= AUDIO_STREAM_CNT) return BAD_VALUE;
Andy Hung264fa4c2024-02-21 15:52:12 -0800330 const sp<IAudioFlinger> af = get_audio_flinger();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800331 if (af == 0) return PERMISSION_DENIED;
Eric Laurentc2f1f072009-07-17 12:17:14 -0700332 *volume = af->streamVolume(stream, output);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800333 return NO_ERROR;
334}
335
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800336status_t AudioSystem::getStreamMute(audio_stream_type_t stream, bool* mute) {
Dima Zavinfce7a472011-04-19 22:30:36 -0700337 if (uint32_t(stream) >= AUDIO_STREAM_CNT) return BAD_VALUE;
Andy Hung264fa4c2024-02-21 15:52:12 -0800338 const sp<IAudioFlinger> af = get_audio_flinger();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800339 if (af == 0) return PERMISSION_DENIED;
340 *mute = af->streamMute(stream);
341 return NO_ERROR;
342}
343
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800344status_t AudioSystem::setMode(audio_mode_t mode) {
Glenn Kasten930f4ca2012-01-06 16:47:31 -0800345 if (uint32_t(mode) >= AUDIO_MODE_CNT) return BAD_VALUE;
Andy Hung264fa4c2024-02-21 15:52:12 -0800346 const sp<IAudioFlinger> af = get_audio_flinger();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800347 if (af == 0) return PERMISSION_DENIED;
348 return af->setMode(mode);
349}
350
Mikhail Naganovb1ddbb02023-03-15 17:06:59 -0700351status_t AudioSystem::setSimulateDeviceConnections(bool enabled) {
Andy Hung264fa4c2024-02-21 15:52:12 -0800352 const sp<IAudioFlinger> af = get_audio_flinger();
Mikhail Naganovb1ddbb02023-03-15 17:06:59 -0700353 if (af == 0) return PERMISSION_DENIED;
354 return af->setSimulateDeviceConnections(enabled);
355}
356
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800357status_t AudioSystem::setParameters(audio_io_handle_t ioHandle, const String8& keyValuePairs) {
Andy Hung264fa4c2024-02-21 15:52:12 -0800358 const sp<IAudioFlinger> af = get_audio_flinger();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800359 if (af == 0) return PERMISSION_DENIED;
Eric Laurentc2f1f072009-07-17 12:17:14 -0700360 return af->setParameters(ioHandle, keyValuePairs);
361}
362
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800363String8 AudioSystem::getParameters(audio_io_handle_t ioHandle, const String8& keys) {
Andy Hung264fa4c2024-02-21 15:52:12 -0800364 const sp<IAudioFlinger> af = get_audio_flinger();
Eric Laurentc2f1f072009-07-17 12:17:14 -0700365 String8 result = String8("");
366 if (af == 0) return result;
367
368 result = af->getParameters(ioHandle, keys);
369 return result;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800370}
371
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800372status_t AudioSystem::setParameters(const String8& keyValuePairs) {
Glenn Kasten142f5192014-03-25 17:44:59 -0700373 return setParameters(AUDIO_IO_HANDLE_NONE, keyValuePairs);
Glenn Kastenc23885e2013-12-19 16:35:18 -0800374}
375
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800376String8 AudioSystem::getParameters(const String8& keys) {
Glenn Kasten142f5192014-03-25 17:44:59 -0700377 return getParameters(AUDIO_IO_HANDLE_NONE, keys);
Glenn Kastenc23885e2013-12-19 16:35:18 -0800378}
379
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800380// convert volume steps to natural log scale
381
382// change this value to change volume scaling
Andy Hungacd955a2024-02-16 17:14:36 -0800383constexpr float kdbPerStep = 0.5f;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800384// shouldn't need to touch these
Andy Hungacd955a2024-02-16 17:14:36 -0800385constexpr float kdBConvert = -kdbPerStep * 2.302585093f / 20.0f;
386constexpr float kdBConvertInverse = 1.0f / kdBConvert;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800387
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800388float AudioSystem::linearToLog(int volume) {
Andy Hungacd955a2024-02-16 17:14:36 -0800389 // float v = volume ? exp(float(100 - volume) * kdBConvert) : 0;
Steve Blockb8a80522011-12-20 16:23:08 +0000390 // ALOGD("linearToLog(%d)=%f", volume, v);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800391 // return v;
Andy Hungacd955a2024-02-16 17:14:36 -0800392 return volume ? exp(float(100 - volume) * kdBConvert) : 0;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800393}
394
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800395int AudioSystem::logToLinear(float volume) {
Andy Hungacd955a2024-02-16 17:14:36 -0800396 // int v = volume ? 100 - int(kdBConvertInverse * log(volume) + 0.5) : 0;
Steve Blockb8a80522011-12-20 16:23:08 +0000397 // ALOGD("logTolinear(%d)=%f", v, volume);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800398 // return v;
Andy Hungacd955a2024-02-16 17:14:36 -0800399 return volume ? 100 - int(kdBConvertInverse * log(volume) + 0.5) : 0;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800400}
401
Eric Laurent21da6472017-11-09 16:29:26 -0800402/* static */ size_t AudioSystem::calculateMinFrameCount(
403 uint32_t afLatencyMs, uint32_t afFrameCount, uint32_t afSampleRate,
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800404 uint32_t sampleRate, float speed /*, uint32_t notificationsPerBufferReq*/) {
Eric Laurent21da6472017-11-09 16:29:26 -0800405 // Ensure that buffer depth covers at least audio hardware latency
406 uint32_t minBufCount = afLatencyMs / ((1000 * afFrameCount) / afSampleRate);
407 if (minBufCount < 2) {
408 minBufCount = 2;
409 }
410#if 0
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800411 // The notificationsPerBufferReq parameter is not yet used for non-fast tracks,
412 // but keeping the code here to make it easier to add later.
413 if (minBufCount < notificationsPerBufferReq) {
414 minBufCount = notificationsPerBufferReq;
415 }
Eric Laurent21da6472017-11-09 16:29:26 -0800416#endif
417 ALOGV("calculateMinFrameCount afLatency %u afFrameCount %u afSampleRate %u "
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800418 "sampleRate %u speed %f minBufCount: %u" /*" notificationsPerBufferReq %u"*/,
419 afLatencyMs, afFrameCount, afSampleRate, sampleRate, speed, minBufCount
420 /*, notificationsPerBufferReq*/);
Eric Laurent21da6472017-11-09 16:29:26 -0800421 return minBufCount * sourceFramesNeededWithTimestretch(
422 sampleRate, afFrameCount, afSampleRate, speed);
423}
424
425
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800426status_t
427AudioSystem::getOutputSamplingRate(uint32_t* samplingRate, audio_stream_type_t streamType) {
Eric Laurentc2f1f072009-07-17 12:17:14 -0700428 audio_io_handle_t output;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800429
Dima Zavinfce7a472011-04-19 22:30:36 -0700430 if (streamType == AUDIO_STREAM_DEFAULT) {
431 streamType = AUDIO_STREAM_MUSIC;
Eric Laurentc2f1f072009-07-17 12:17:14 -0700432 }
433
Glenn Kastenfff6d712012-01-12 16:38:12 -0800434 output = getOutput(streamType);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700435 if (output == 0) {
436 return PERMISSION_DENIED;
437 }
438
Jean-Michel Trivib7f24b12014-06-11 10:05:30 -0700439 return getSamplingRate(output, samplingRate);
Eric Laurent1a9ed112012-03-20 18:36:01 -0700440}
441
Glenn Kasten2c073da2016-02-26 09:14:08 -0800442status_t AudioSystem::getSamplingRate(audio_io_handle_t ioHandle,
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800443 uint32_t* samplingRate) {
Andy Hung264fa4c2024-02-21 15:52:12 -0800444 const sp<IAudioFlinger> af = get_audio_flinger();
Eric Laurentf6778fd2014-11-18 17:26:58 -0800445 if (af == 0) return PERMISSION_DENIED;
Glenn Kasten2c073da2016-02-26 09:14:08 -0800446 sp<AudioIoDescriptor> desc = getIoDescriptor(ioHandle);
447 if (desc == 0) {
448 *samplingRate = af->sampleRate(ioHandle);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700449 } else {
Mikhail Naganov88536df2021-07-26 17:30:29 -0700450 *samplingRate = desc->getSamplingRate();
Eric Laurentc2f1f072009-07-17 12:17:14 -0700451 }
Glenn Kastenf94006c2014-01-08 08:56:06 -0800452 if (*samplingRate == 0) {
Glenn Kasten2c073da2016-02-26 09:14:08 -0800453 ALOGE("AudioSystem::getSamplingRate failed for ioHandle %d", ioHandle);
Glenn Kastenf94006c2014-01-08 08:56:06 -0800454 return BAD_VALUE;
455 }
Eric Laurentc2f1f072009-07-17 12:17:14 -0700456
Glenn Kasten2c073da2016-02-26 09:14:08 -0800457 ALOGV("getSamplingRate() ioHandle %d, sampling rate %u", ioHandle, *samplingRate);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700458
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800459 return NO_ERROR;
460}
461
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800462status_t AudioSystem::getOutputFrameCount(size_t* frameCount, audio_stream_type_t streamType) {
Eric Laurentc2f1f072009-07-17 12:17:14 -0700463 audio_io_handle_t output;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800464
Dima Zavinfce7a472011-04-19 22:30:36 -0700465 if (streamType == AUDIO_STREAM_DEFAULT) {
466 streamType = AUDIO_STREAM_MUSIC;
Eric Laurentc2f1f072009-07-17 12:17:14 -0700467 }
Eric Laurent48f7f5e2009-04-02 09:32:43 -0700468
Glenn Kastenfff6d712012-01-12 16:38:12 -0800469 output = getOutput(streamType);
Glenn Kasten142f5192014-03-25 17:44:59 -0700470 if (output == AUDIO_IO_HANDLE_NONE) {
Eric Laurentc2f1f072009-07-17 12:17:14 -0700471 return PERMISSION_DENIED;
472 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800473
Jean-Michel Trivib7f24b12014-06-11 10:05:30 -0700474 return getFrameCount(output, frameCount);
Eric Laurent1a9ed112012-03-20 18:36:01 -0700475}
476
Glenn Kasten2c073da2016-02-26 09:14:08 -0800477status_t AudioSystem::getFrameCount(audio_io_handle_t ioHandle,
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800478 size_t* frameCount) {
Andy Hung264fa4c2024-02-21 15:52:12 -0800479 const sp<IAudioFlinger> af = get_audio_flinger();
Eric Laurentf6778fd2014-11-18 17:26:58 -0800480 if (af == 0) return PERMISSION_DENIED;
Glenn Kasten2c073da2016-02-26 09:14:08 -0800481 sp<AudioIoDescriptor> desc = getIoDescriptor(ioHandle);
482 if (desc == 0) {
483 *frameCount = af->frameCount(ioHandle);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700484 } else {
Mikhail Naganov88536df2021-07-26 17:30:29 -0700485 *frameCount = desc->getFrameCount();
Eric Laurentc2f1f072009-07-17 12:17:14 -0700486 }
Glenn Kastenf94006c2014-01-08 08:56:06 -0800487 if (*frameCount == 0) {
Glenn Kasten2c073da2016-02-26 09:14:08 -0800488 ALOGE("AudioSystem::getFrameCount failed for ioHandle %d", ioHandle);
Glenn Kastenf94006c2014-01-08 08:56:06 -0800489 return BAD_VALUE;
490 }
Eric Laurentc2f1f072009-07-17 12:17:14 -0700491
Glenn Kasten2c073da2016-02-26 09:14:08 -0800492 ALOGV("getFrameCount() ioHandle %d, frameCount %zu", ioHandle, *frameCount);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700493
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800494 return NO_ERROR;
495}
496
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800497status_t AudioSystem::getOutputLatency(uint32_t* latency, audio_stream_type_t streamType) {
Eric Laurentc2f1f072009-07-17 12:17:14 -0700498 audio_io_handle_t output;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800499
Dima Zavinfce7a472011-04-19 22:30:36 -0700500 if (streamType == AUDIO_STREAM_DEFAULT) {
501 streamType = AUDIO_STREAM_MUSIC;
Eric Laurentc2f1f072009-07-17 12:17:14 -0700502 }
Eric Laurent48f7f5e2009-04-02 09:32:43 -0700503
Glenn Kastenfff6d712012-01-12 16:38:12 -0800504 output = getOutput(streamType);
Glenn Kasten142f5192014-03-25 17:44:59 -0700505 if (output == AUDIO_IO_HANDLE_NONE) {
Eric Laurentc2f1f072009-07-17 12:17:14 -0700506 return PERMISSION_DENIED;
507 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800508
Glenn Kasten241618f2014-03-25 17:48:57 -0700509 return getLatency(output, latency);
Eric Laurent1a9ed112012-03-20 18:36:01 -0700510}
511
512status_t AudioSystem::getLatency(audio_io_handle_t output,
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800513 uint32_t* latency) {
Andy Hung264fa4c2024-02-21 15:52:12 -0800514 const sp<IAudioFlinger> af = get_audio_flinger();
Eric Laurentf6778fd2014-11-18 17:26:58 -0800515 if (af == 0) return PERMISSION_DENIED;
Eric Laurent296fb132015-05-01 11:38:42 -0700516 sp<AudioIoDescriptor> outputDesc = getIoDescriptor(output);
Eric Laurent73e26b62015-04-27 16:55:58 -0700517 if (outputDesc == 0) {
Eric Laurentc2f1f072009-07-17 12:17:14 -0700518 *latency = af->latency(output);
519 } else {
Mikhail Naganov88536df2021-07-26 17:30:29 -0700520 *latency = outputDesc->getLatency();
Eric Laurentc2f1f072009-07-17 12:17:14 -0700521 }
522
Glenn Kasten241618f2014-03-25 17:48:57 -0700523 ALOGV("getLatency() output %d, latency %d", output, *latency);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700524
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800525 return NO_ERROR;
526}
527
Glenn Kastendd8104c2012-07-02 12:42:44 -0700528status_t AudioSystem::getInputBufferSize(uint32_t sampleRate, audio_format_t format,
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800529 audio_channel_mask_t channelMask, size_t* buffSize) {
Eric Laurent296fb132015-05-01 11:38:42 -0700530 const sp<AudioFlingerClient> afc = getAudioFlingerClient();
531 if (afc == 0) {
532 return NO_INIT;
533 }
534 return afc->getInputBufferSize(sampleRate, format, channelMask, buffSize);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800535}
536
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800537status_t AudioSystem::setVoiceVolume(float value) {
Andy Hung264fa4c2024-02-21 15:52:12 -0800538 const sp<IAudioFlinger> af = get_audio_flinger();
Eric Laurentf0ee6f42009-10-21 08:14:22 -0700539 if (af == 0) return PERMISSION_DENIED;
540 return af->setVoiceVolume(value);
541}
542
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800543status_t AudioSystem::getRenderPosition(audio_io_handle_t output, uint32_t* halFrames,
544 uint32_t* dspFrames) {
Andy Hung264fa4c2024-02-21 15:52:12 -0800545 const sp<IAudioFlinger> af = get_audio_flinger();
Eric Laurent342e9cf2010-01-19 17:37:09 -0800546 if (af == 0) return PERMISSION_DENIED;
547
Richard Fitzgeraldad3af332013-03-25 16:54:37 +0000548 return af->getRenderPosition(halFrames, dspFrames, output);
Eric Laurent342e9cf2010-01-19 17:37:09 -0800549}
550
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800551uint32_t AudioSystem::getInputFramesLost(audio_io_handle_t ioHandle) {
Andy Hung264fa4c2024-02-21 15:52:12 -0800552 const sp<IAudioFlinger> af = get_audio_flinger();
Glenn Kasten5f972c02014-01-13 09:59:31 -0800553 uint32_t result = 0;
Eric Laurent05bca2f2010-02-26 02:47:27 -0800554 if (af == 0) return result;
Glenn Kasten142f5192014-03-25 17:44:59 -0700555 if (ioHandle == AUDIO_IO_HANDLE_NONE) return result;
Eric Laurent05bca2f2010-02-26 02:47:27 -0800556
557 result = af->getInputFramesLost(ioHandle);
558 return result;
559}
560
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800561audio_unique_id_t AudioSystem::newAudioUniqueId(audio_unique_id_use_t use) {
Mikhail Naganov2996f672019-04-18 12:29:59 -0700562 // Must not use AF as IDs will re-roll on audioserver restart, b/130369529.
Andy Hung264fa4c2024-02-21 15:52:12 -0800563 const sp<IAudioFlinger> af = get_audio_flinger();
Eric Laurentde3f8392014-07-27 18:38:22 -0700564 if (af == 0) return AUDIO_UNIQUE_ID_ALLOCATE;
Glenn Kasteneeecb982016-02-26 10:44:04 -0800565 return af->newAudioUniqueId(use);
Eric Laurentbe916aa2010-06-01 23:49:17 -0700566}
567
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800568void AudioSystem::acquireAudioSessionId(audio_session_t audioSession, pid_t pid, uid_t uid) {
Andy Hung264fa4c2024-02-21 15:52:12 -0800569 const sp<IAudioFlinger> af = get_audio_flinger();
Marco Nelissen3a34bef2011-08-02 13:33:41 -0700570 if (af != 0) {
Andy Hung8b0bfd92019-12-23 13:11:11 -0800571 af->acquireAudioSessionId(audioSession, pid, uid);
Marco Nelissen3a34bef2011-08-02 13:33:41 -0700572 }
573}
574
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800575void AudioSystem::releaseAudioSessionId(audio_session_t audioSession, pid_t pid) {
Andy Hung264fa4c2024-02-21 15:52:12 -0800576 const sp<IAudioFlinger> af = get_audio_flinger();
Marco Nelissen3a34bef2011-08-02 13:33:41 -0700577 if (af != 0) {
Marco Nelissend457c972014-02-11 08:47:07 -0800578 af->releaseAudioSessionId(audioSession, pid);
Marco Nelissen3a34bef2011-08-02 13:33:41 -0700579 }
580}
581
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800582audio_hw_sync_t AudioSystem::getAudioHwSyncForSession(audio_session_t sessionId) {
Andy Hung264fa4c2024-02-21 15:52:12 -0800583 const sp<IAudioFlinger> af = get_audio_flinger();
Eric Laurent93c3d412014-08-01 14:48:35 -0700584 if (af == 0) return AUDIO_HW_SYNC_INVALID;
585 return af->getAudioHwSyncForSession(sessionId);
586}
587
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800588status_t AudioSystem::systemReady() {
Andy Hung264fa4c2024-02-21 15:52:12 -0800589 const sp<IAudioFlinger> af = get_audio_flinger();
Eric Laurent72e3f392015-05-20 14:43:50 -0700590 if (af == 0) return NO_INIT;
591 return af->systemReady();
592}
593
Eric Laurentd66d7a12021-07-13 13:35:32 +0200594status_t AudioSystem::audioPolicyReady() {
Andy Hung264fa4c2024-02-21 15:52:12 -0800595 const sp<IAudioFlinger> af = get_audio_flinger();
Eric Laurentd66d7a12021-07-13 13:35:32 +0200596 if (af == 0) return NO_INIT;
597 return af->audioPolicyReady();
598}
599
Glenn Kasten4a8308b2016-04-18 14:10:01 -0700600status_t AudioSystem::getFrameCountHAL(audio_io_handle_t ioHandle,
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800601 size_t* frameCount) {
Andy Hung264fa4c2024-02-21 15:52:12 -0800602 const sp<IAudioFlinger> af = get_audio_flinger();
Glenn Kasten4a8308b2016-04-18 14:10:01 -0700603 if (af == 0) return PERMISSION_DENIED;
604 sp<AudioIoDescriptor> desc = getIoDescriptor(ioHandle);
605 if (desc == 0) {
606 *frameCount = af->frameCountHAL(ioHandle);
607 } else {
Mikhail Naganov88536df2021-07-26 17:30:29 -0700608 *frameCount = desc->getFrameCountHAL();
Glenn Kasten4a8308b2016-04-18 14:10:01 -0700609 }
610 if (*frameCount == 0) {
611 ALOGE("AudioSystem::getFrameCountHAL failed for ioHandle %d", ioHandle);
612 return BAD_VALUE;
613 }
614
615 ALOGV("getFrameCountHAL() ioHandle %d, frameCount %zu", ioHandle, *frameCount);
616
617 return NO_ERROR;
618}
619
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800620// ---------------------------------------------------------------------------
621
Eric Laurent73e26b62015-04-27 16:55:58 -0700622
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800623void AudioSystem::AudioFlingerClient::clearIoCache() {
Andy Hungacd955a2024-02-16 17:14:36 -0800624 std::lock_guard _l(mMutex);
Eric Laurent73e26b62015-04-27 16:55:58 -0700625 mIoDescriptors.clear();
626 mInBuffSize = 0;
627 mInSamplingRate = 0;
628 mInFormat = AUDIO_FORMAT_DEFAULT;
629 mInChannelMask = AUDIO_CHANNEL_NONE;
630}
631
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800632void AudioSystem::AudioFlingerClient::binderDied(const wp<IBinder>& who __unused) {
Andy Hungda1fb072024-02-20 19:08:08 -0800633 gAudioFlingerServiceHandler.clearService();
Ytai Ben-Tsvi000c3e42020-01-09 15:26:40 -0800634 reportError(DEAD_OBJECT);
635
Steve Block5ff1dd52012-01-05 23:22:43 +0000636 ALOGW("AudioFlinger server died!");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800637}
638
Ytai Ben-Tsvi10dc0a62020-09-18 11:31:55 -0700639Status AudioSystem::AudioFlingerClient::ioConfigChanged(
640 media::AudioIoConfigEvent _event,
641 const media::AudioIoDescriptor& _ioDesc) {
Mikhail Naganov88536df2021-07-26 17:30:29 -0700642 audio_io_config_event_t event = VALUE_OR_RETURN_BINDER_STATUS(
643 aidl2legacy_AudioIoConfigEvent_audio_io_config_event_t(_event));
Ytai Ben-Tsvi10dc0a62020-09-18 11:31:55 -0700644 sp<AudioIoDescriptor> ioDesc(
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800645 VALUE_OR_RETURN_BINDER_STATUS(
646 aidl2legacy_AudioIoDescriptor_AudioIoDescriptor(_ioDesc)));
Ytai Ben-Tsvi10dc0a62020-09-18 11:31:55 -0700647
Steve Block3856b092011-10-20 11:56:00 +0100648 ALOGV("ioConfigChanged() event %d", event);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700649
Mikhail Naganov88536df2021-07-26 17:30:29 -0700650 if (ioDesc->getIoHandle() == AUDIO_IO_HANDLE_NONE) return Status::ok();
Eric Laurentc2f1f072009-07-17 12:17:14 -0700651
Eric Laurent296fb132015-05-01 11:38:42 -0700652 audio_port_handle_t deviceId = AUDIO_PORT_HANDLE_NONE;
Eric Laurent09f1ed22019-04-24 17:45:17 -0700653 std::vector<sp<AudioDeviceCallback>> callbacksToCall;
Eric Laurent296fb132015-05-01 11:38:42 -0700654 {
Andy Hungacd955a2024-02-16 17:14:36 -0800655 std::lock_guard _l(mMutex);
Eric Laurent09f1ed22019-04-24 17:45:17 -0700656 auto callbacks = std::map<audio_port_handle_t, wp<AudioDeviceCallback>>();
Eric Laurent296fb132015-05-01 11:38:42 -0700657
658 switch (event) {
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800659 case AUDIO_OUTPUT_OPENED:
660 case AUDIO_OUTPUT_REGISTERED:
661 case AUDIO_INPUT_OPENED:
662 case AUDIO_INPUT_REGISTERED: {
Andy Hungd211ce02024-02-16 15:34:07 -0800663 if (sp<AudioIoDescriptor> oldDesc = getIoDescriptor_l(ioDesc->getIoHandle())) {
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800664 deviceId = oldDesc->getDeviceId();
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800665 }
Andy Hungd211ce02024-02-16 15:34:07 -0800666 mIoDescriptors[ioDesc->getIoHandle()] = ioDesc;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800667
668 if (ioDesc->getDeviceId() != AUDIO_PORT_HANDLE_NONE) {
669 deviceId = ioDesc->getDeviceId();
670 if (event == AUDIO_OUTPUT_OPENED || event == AUDIO_INPUT_OPENED) {
Mikhail Naganov88536df2021-07-26 17:30:29 -0700671 auto it = mAudioDeviceCallbacks.find(ioDesc->getIoHandle());
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800672 if (it != mAudioDeviceCallbacks.end()) {
673 callbacks = it->second;
674 }
675 }
676 }
Mikhail Naganov88536df2021-07-26 17:30:29 -0700677 ALOGV("ioConfigChanged() new %s %s %s",
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800678 event == AUDIO_OUTPUT_OPENED || event == AUDIO_OUTPUT_REGISTERED ?
679 "output" : "input",
680 event == AUDIO_OUTPUT_OPENED || event == AUDIO_INPUT_OPENED ?
681 "opened" : "registered",
Mikhail Naganov88536df2021-07-26 17:30:29 -0700682 ioDesc->toDebugString().c_str());
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800683 }
684 break;
685 case AUDIO_OUTPUT_CLOSED:
686 case AUDIO_INPUT_CLOSED: {
Mikhail Naganov88536df2021-07-26 17:30:29 -0700687 if (getIoDescriptor_l(ioDesc->getIoHandle()) == 0) {
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800688 ALOGW("ioConfigChanged() closing unknown %s %d",
Mikhail Naganov88536df2021-07-26 17:30:29 -0700689 event == AUDIO_OUTPUT_CLOSED ? "output" : "input", ioDesc->getIoHandle());
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800690 break;
691 }
692 ALOGV("ioConfigChanged() %s %d closed",
Mikhail Naganov88536df2021-07-26 17:30:29 -0700693 event == AUDIO_OUTPUT_CLOSED ? "output" : "input", ioDesc->getIoHandle());
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800694
Andy Hungd211ce02024-02-16 15:34:07 -0800695 mIoDescriptors.erase(ioDesc->getIoHandle());
Mikhail Naganov88536df2021-07-26 17:30:29 -0700696 mAudioDeviceCallbacks.erase(ioDesc->getIoHandle());
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800697 }
698 break;
699
700 case AUDIO_OUTPUT_CONFIG_CHANGED:
701 case AUDIO_INPUT_CONFIG_CHANGED: {
Mikhail Naganov88536df2021-07-26 17:30:29 -0700702 sp<AudioIoDescriptor> oldDesc = getIoDescriptor_l(ioDesc->getIoHandle());
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800703 if (oldDesc == 0) {
704 ALOGW("ioConfigChanged() modifying unknown %s! %d",
705 event == AUDIO_OUTPUT_CONFIG_CHANGED ? "output" : "input",
Mikhail Naganov88536df2021-07-26 17:30:29 -0700706 ioDesc->getIoHandle());
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800707 break;
708 }
709
Eric Laurent7c1ec5f2015-07-09 14:52:47 -0700710 deviceId = oldDesc->getDeviceId();
Andy Hungd211ce02024-02-16 15:34:07 -0800711 mIoDescriptors[ioDesc->getIoHandle()] = ioDesc;
Eric Laurent296fb132015-05-01 11:38:42 -0700712
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800713 if (deviceId != ioDesc->getDeviceId()) {
714 deviceId = ioDesc->getDeviceId();
Mikhail Naganov88536df2021-07-26 17:30:29 -0700715 auto it = mAudioDeviceCallbacks.find(ioDesc->getIoHandle());
Eric Laurent09f1ed22019-04-24 17:45:17 -0700716 if (it != mAudioDeviceCallbacks.end()) {
717 callbacks = it->second;
Francois Gaffie24a9fb02019-01-18 17:51:34 +0100718 }
719 }
Mikhail Naganov88536df2021-07-26 17:30:29 -0700720 ALOGV("ioConfigChanged() new config for %s %s",
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800721 event == AUDIO_OUTPUT_CONFIG_CHANGED ? "output" : "input",
Mikhail Naganov88536df2021-07-26 17:30:29 -0700722 ioDesc->toDebugString().c_str());
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800723
Eric Laurent296fb132015-05-01 11:38:42 -0700724 }
Eric Laurent296fb132015-05-01 11:38:42 -0700725 break;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800726 case AUDIO_CLIENT_STARTED: {
Mikhail Naganov88536df2021-07-26 17:30:29 -0700727 sp<AudioIoDescriptor> oldDesc = getIoDescriptor_l(ioDesc->getIoHandle());
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800728 if (oldDesc == 0) {
Mikhail Naganov88536df2021-07-26 17:30:29 -0700729 ALOGW("ioConfigChanged() start client on unknown io! %d",
730 ioDesc->getIoHandle());
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800731 break;
732 }
733 ALOGV("ioConfigChanged() AUDIO_CLIENT_STARTED io %d port %d num callbacks %zu",
Mikhail Naganov88536df2021-07-26 17:30:29 -0700734 ioDesc->getIoHandle(), ioDesc->getPortId(), mAudioDeviceCallbacks.size());
735 oldDesc->setPatch(ioDesc->getPatch());
736 auto it = mAudioDeviceCallbacks.find(ioDesc->getIoHandle());
Eric Laurent09f1ed22019-04-24 17:45:17 -0700737 if (it != mAudioDeviceCallbacks.end()) {
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800738 auto cbks = it->second;
Mikhail Naganov88536df2021-07-26 17:30:29 -0700739 auto it2 = cbks.find(ioDesc->getPortId());
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800740 if (it2 != cbks.end()) {
Mikhail Naganov88536df2021-07-26 17:30:29 -0700741 callbacks.emplace(ioDesc->getPortId(), it2->second);
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800742 deviceId = oldDesc->getDeviceId();
743 }
Eric Laurent09f1ed22019-04-24 17:45:17 -0700744 }
Eric Laurent296fb132015-05-01 11:38:42 -0700745 }
Eric Laurent09f1ed22019-04-24 17:45:17 -0700746 break;
Eric Laurent09f1ed22019-04-24 17:45:17 -0700747 }
748
749 for (auto wpCbk : callbacks) {
750 sp<AudioDeviceCallback> spCbk = wpCbk.second.promote();
751 if (spCbk != nullptr) {
752 callbacksToCall.push_back(spCbk);
753 }
Eric Laurentad2e7b92017-09-14 20:06:42 -0700754 }
Eric Laurent4463ff52019-02-07 13:56:09 -0800755 }
756
Andy Hungacd955a2024-02-16 17:14:36 -0800757 // Callbacks must be called without mMutex held. May lead to dead lock if calling for
758 // example getRoutedDevice that updates the device and tries to acquire mMutex.
Eric Laurent09f1ed22019-04-24 17:45:17 -0700759 for (auto cb : callbacksToCall) {
Mikhail Naganov88536df2021-07-26 17:30:29 -0700760 // If callbacksToCall is not empty, it implies ioDesc->getIoHandle() and deviceId are valid
761 cb->onAudioDeviceUpdate(ioDesc->getIoHandle(), deviceId);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700762 }
Ytai Ben-Tsvi10dc0a62020-09-18 11:31:55 -0700763
764 return Status::ok();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800765}
766
Eric Laurent076e7c72022-05-03 18:12:28 +0200767Status AudioSystem::AudioFlingerClient::onSupportedLatencyModesChanged(
Mikhail Naganovf53e1822022-12-18 02:48:14 +0000768 int output, const std::vector<media::audio::common::AudioLatencyMode>& latencyModes) {
Eric Laurent076e7c72022-05-03 18:12:28 +0200769 audio_io_handle_t outputLegacy = VALUE_OR_RETURN_BINDER_STATUS(
770 aidl2legacy_int32_t_audio_io_handle_t(output));
771 std::vector<audio_latency_mode_t> modesLegacy = VALUE_OR_RETURN_BINDER_STATUS(
772 convertContainer<std::vector<audio_latency_mode_t>>(
Mikhail Naganovf53e1822022-12-18 02:48:14 +0000773 latencyModes, aidl2legacy_AudioLatencyMode_audio_latency_mode_t));
Eric Laurent076e7c72022-05-03 18:12:28 +0200774
775 std::vector<sp<SupportedLatencyModesCallback>> callbacks;
776 {
Andy Hungacd955a2024-02-16 17:14:36 -0800777 std::lock_guard _l(mMutex);
Eric Laurent076e7c72022-05-03 18:12:28 +0200778 for (auto callback : mSupportedLatencyModesCallbacks) {
779 if (auto ref = callback.promote(); ref != nullptr) {
780 callbacks.push_back(ref);
781 }
782 }
783 }
784 for (const auto& callback : callbacks) {
785 callback->onSupportedLatencyModesChanged(outputLegacy, modesLegacy);
786 }
787
788 return Status::ok();
789}
790
Eric Laurent73e26b62015-04-27 16:55:58 -0700791status_t AudioSystem::AudioFlingerClient::getInputBufferSize(
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800792 uint32_t sampleRate, audio_format_t format,
793 audio_channel_mask_t channelMask, size_t* buffSize) {
Andy Hung264fa4c2024-02-21 15:52:12 -0800794 const sp<IAudioFlinger> af = get_audio_flinger();
Eric Laurent73e26b62015-04-27 16:55:58 -0700795 if (af == 0) {
796 return PERMISSION_DENIED;
797 }
Andy Hungacd955a2024-02-16 17:14:36 -0800798 std::lock_guard _l(mMutex);
Eric Laurent73e26b62015-04-27 16:55:58 -0700799 // Do we have a stale mInBuffSize or are we requesting the input buffer size for new values
800 if ((mInBuffSize == 0) || (sampleRate != mInSamplingRate) || (format != mInFormat)
801 || (channelMask != mInChannelMask)) {
802 size_t inBuffSize = af->getInputBufferSize(sampleRate, format, channelMask);
803 if (inBuffSize == 0) {
Glenn Kasten49f36ba2017-12-06 13:02:02 -0800804 ALOGE("AudioSystem::getInputBufferSize failed sampleRate %d format %#x channelMask %#x",
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800805 sampleRate, format, channelMask);
Eric Laurent73e26b62015-04-27 16:55:58 -0700806 return BAD_VALUE;
807 }
808 // A benign race is possible here: we could overwrite a fresher cache entry
809 // save the request params
810 mInSamplingRate = sampleRate;
811 mInFormat = format;
812 mInChannelMask = channelMask;
813
814 mInBuffSize = inBuffSize;
815 }
816
817 *buffSize = mInBuffSize;
818
819 return NO_ERROR;
820}
821
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800822sp<AudioIoDescriptor>
823AudioSystem::AudioFlingerClient::getIoDescriptor_l(audio_io_handle_t ioHandle) {
Andy Hungd211ce02024-02-16 15:34:07 -0800824 if (const auto it = mIoDescriptors.find(ioHandle);
825 it != mIoDescriptors.end()) {
826 return it->second;
Eric Laurent73e26b62015-04-27 16:55:58 -0700827 }
Andy Hungd211ce02024-02-16 15:34:07 -0800828 return {};
Eric Laurent73e26b62015-04-27 16:55:58 -0700829}
830
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800831sp<AudioIoDescriptor> AudioSystem::AudioFlingerClient::getIoDescriptor(audio_io_handle_t ioHandle) {
Andy Hungacd955a2024-02-16 17:14:36 -0800832 std::lock_guard _l(mMutex);
Praveen Chavan49fdeaf2015-09-29 02:25:47 -0700833 return getIoDescriptor_l(ioHandle);
834}
835
Eric Laurent296fb132015-05-01 11:38:42 -0700836status_t AudioSystem::AudioFlingerClient::addAudioDeviceCallback(
Eric Laurent09f1ed22019-04-24 17:45:17 -0700837 const wp<AudioDeviceCallback>& callback, audio_io_handle_t audioIo,
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800838 audio_port_handle_t portId) {
Eric Laurent09f1ed22019-04-24 17:45:17 -0700839 ALOGV("%s audioIo %d portId %d", __func__, audioIo, portId);
Andy Hungacd955a2024-02-16 17:14:36 -0800840 std::lock_guard _l(mMutex);
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800841 auto& callbacks = mAudioDeviceCallbacks.emplace(
842 audioIo,
843 std::map<audio_port_handle_t, wp<AudioDeviceCallback>>()).first->second;
Eric Laurent09f1ed22019-04-24 17:45:17 -0700844 auto result = callbacks.try_emplace(portId, callback);
845 if (!result.second) {
846 return INVALID_OPERATION;
Eric Laurent296fb132015-05-01 11:38:42 -0700847 }
Eric Laurent296fb132015-05-01 11:38:42 -0700848 return NO_ERROR;
849}
850
851status_t AudioSystem::AudioFlingerClient::removeAudioDeviceCallback(
Eric Laurent09f1ed22019-04-24 17:45:17 -0700852 const wp<AudioDeviceCallback>& callback __unused, audio_io_handle_t audioIo,
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800853 audio_port_handle_t portId) {
Eric Laurent09f1ed22019-04-24 17:45:17 -0700854 ALOGV("%s audioIo %d portId %d", __func__, audioIo, portId);
Andy Hungacd955a2024-02-16 17:14:36 -0800855 std::lock_guard _l(mMutex);
Eric Laurent09f1ed22019-04-24 17:45:17 -0700856 auto it = mAudioDeviceCallbacks.find(audioIo);
857 if (it == mAudioDeviceCallbacks.end()) {
Eric Laurent296fb132015-05-01 11:38:42 -0700858 return INVALID_OPERATION;
859 }
Eric Laurent09f1ed22019-04-24 17:45:17 -0700860 if (it->second.erase(portId) == 0) {
Eric Laurent296fb132015-05-01 11:38:42 -0700861 return INVALID_OPERATION;
862 }
Eric Laurent09f1ed22019-04-24 17:45:17 -0700863 if (it->second.size() == 0) {
864 mAudioDeviceCallbacks.erase(audioIo);
Eric Laurent296fb132015-05-01 11:38:42 -0700865 }
866 return NO_ERROR;
867}
868
Eric Laurent076e7c72022-05-03 18:12:28 +0200869status_t AudioSystem::AudioFlingerClient::addSupportedLatencyModesCallback(
870 const sp<SupportedLatencyModesCallback>& callback) {
Andy Hungacd955a2024-02-16 17:14:36 -0800871 std::lock_guard _l(mMutex);
Eric Laurent076e7c72022-05-03 18:12:28 +0200872 if (std::find(mSupportedLatencyModesCallbacks.begin(),
873 mSupportedLatencyModesCallbacks.end(),
874 callback) != mSupportedLatencyModesCallbacks.end()) {
875 return INVALID_OPERATION;
876 }
877 mSupportedLatencyModesCallbacks.push_back(callback);
878 return NO_ERROR;
879}
880
881status_t AudioSystem::AudioFlingerClient::removeSupportedLatencyModesCallback(
882 const sp<SupportedLatencyModesCallback>& callback) {
Andy Hungacd955a2024-02-16 17:14:36 -0800883 std::lock_guard _l(mMutex);
Eric Laurent076e7c72022-05-03 18:12:28 +0200884 auto it = std::find(mSupportedLatencyModesCallbacks.begin(),
885 mSupportedLatencyModesCallbacks.end(),
886 callback);
887 if (it == mSupportedLatencyModesCallbacks.end()) {
888 return INVALID_OPERATION;
889 }
890 mSupportedLatencyModesCallbacks.erase(it);
891 return NO_ERROR;
892}
893
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800894/* static */ uintptr_t AudioSystem::addErrorCallback(audio_error_callback cb) {
Andy Hungacd955a2024-02-16 17:14:36 -0800895 std::lock_guard _l(gErrorCallbacksMutex);
Ytai Ben-Tsvi000c3e42020-01-09 15:26:40 -0800896 gAudioErrorCallbacks.insert(cb);
897 return reinterpret_cast<uintptr_t>(cb);
898}
899
900/* static */ void AudioSystem::removeErrorCallback(uintptr_t cb) {
Andy Hungacd955a2024-02-16 17:14:36 -0800901 std::lock_guard _l(gErrorCallbacksMutex);
Ytai Ben-Tsvi000c3e42020-01-09 15:26:40 -0800902 gAudioErrorCallbacks.erase(reinterpret_cast<audio_error_callback>(cb));
903}
904
905/* static */ void AudioSystem::reportError(status_t err) {
Andy Hungacd955a2024-02-16 17:14:36 -0800906 std::lock_guard _l(gErrorCallbacksMutex);
Ytai Ben-Tsvi000c3e42020-01-09 15:26:40 -0800907 for (auto callback : gAudioErrorCallbacks) {
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800908 callback(err);
Ytai Ben-Tsvi000c3e42020-01-09 15:26:40 -0800909 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800910}
911
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800912/*static*/ void AudioSystem::setDynPolicyCallback(dynamic_policy_callback cb) {
Andy Hungacd955a2024-02-16 17:14:36 -0800913 std::lock_guard _l(gMutex);
Jean-Michel Trivif613d422015-04-23 18:41:29 -0700914 gDynPolicyCallback = cb;
915}
916
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800917/*static*/ void AudioSystem::setRecordConfigCallback(record_config_callback cb) {
Andy Hungacd955a2024-02-16 17:14:36 -0800918 std::lock_guard _l(gMutex);
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -0800919 gRecordConfigCallback = cb;
920}
921
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800922/*static*/ void AudioSystem::setRoutingCallback(routing_callback cb) {
Andy Hungacd955a2024-02-16 17:14:36 -0800923 std::lock_guard _l(gMutex);
Jean-Michel Trivi9a6b9ad2020-10-22 16:46:43 -0700924 gRoutingCallback = cb;
925}
926
Jean-Michel Trivi78f2b302022-04-15 18:18:41 +0000927/*static*/ void AudioSystem::setVolInitReqCallback(vol_range_init_req_callback cb) {
Andy Hungacd955a2024-02-16 17:14:36 -0800928 std::lock_guard _l(gMutex);
Jean-Michel Trivi78f2b302022-04-15 18:18:41 +0000929 gVolRangeInitReqCallback = cb;
930}
931
Andy Hung88e71732024-02-21 16:44:27 -0800932struct AudioPolicyTraits {
933 static void onServiceCreate(const sp<IAudioPolicyService>& ap,
934 const sp<AudioSystem::AudioPolicyServiceClient>& apc) {
935 const int64_t token = IPCThreadState::self()->clearCallingIdentity();
Eric Laurent0ebd5f92014-11-19 19:04:52 -0800936 ap->registerClient(apc);
François Gaffie24437602018-04-23 15:08:59 +0200937 ap->setAudioPortCallbacksEnabled(apc->isAudioPortCbEnabled());
François Gaffiecfe17322018-11-07 13:41:29 +0100938 ap->setAudioVolumeGroupCallbacksEnabled(apc->isAudioVolumeGroupCbEnabled());
François Gaffie24437602018-04-23 15:08:59 +0200939 IPCThreadState::self()->restoreCallingIdentity(token);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700940 }
Glenn Kastend2d089f2014-11-05 11:48:12 -0800941
Andy Hung88e71732024-02-21 16:44:27 -0800942 static void onClearService(const sp<AudioSystem::AudioPolicyServiceClient>&) {}
943
944 static constexpr const char *SERVICE_NAME = "media.audio_policy";
945};
946
947[[clang::no_destroy]] static constinit ServiceHandler<IAudioPolicyService,
948 AudioSystem::AudioPolicyServiceClient, IAudioPolicyService,
949 AudioPolicyTraits> gAudioPolicyServiceHandler;
950
951status_t AudioSystem::setLocalAudioPolicyService(const sp<IAudioPolicyService>& aps) {
952 return gAudioPolicyServiceHandler.setLocalService(aps);
953}
954
955sp<IAudioPolicyService> AudioSystem::get_audio_policy_service() {
956 return gAudioPolicyServiceHandler.getService();
Eric Laurentc2f1f072009-07-17 12:17:14 -0700957}
958
Mikhail Naganovec3d5792022-05-06 00:19:55 +0000959void AudioSystem::clearAudioPolicyService() {
Andy Hung88e71732024-02-21 16:44:27 -0800960 gAudioPolicyServiceHandler.clearService();
Mikhail Naganovec3d5792022-05-06 00:19:55 +0000961}
962
Shraddha Basantwanif2aa2502024-04-08 11:01:57 +0000963void AudioSystem::disableThreadPool() {
964 gAudioFlingerServiceHandler.disableThreadPool();
965 gAudioPolicyServiceHandler.disableThreadPool();
966}
967
Glenn Kastenfb1fdc92013-07-10 17:03:19 -0700968// ---------------------------------------------------------------------------
969
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800970void AudioSystem::onNewAudioModulesAvailable() {
Andy Hung264fa4c2024-02-21 15:52:12 -0800971 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Mikhail Naganov88b30d22020-03-09 19:43:13 +0000972 if (aps == 0) return;
973 aps->onNewAudioModulesAvailable();
974}
975
Nathalie Le Clair88fa2752021-11-23 13:03:41 +0100976status_t AudioSystem::setDeviceConnectionState(audio_policy_dev_state_t state,
977 const android::media::audio::common::AudioPort& port,
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800978 audio_format_t encodedFormat) {
Andy Hung264fa4c2024-02-21 15:52:12 -0800979 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Eric Laurent71b63e32011-09-02 14:20:56 -0700980
Eric Laurentc2f1f072009-07-17 12:17:14 -0700981 if (aps == 0) return PERMISSION_DENIED;
982
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800983 return statusTFromBinderStatus(
984 aps->setDeviceConnectionState(
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800985 VALUE_OR_RETURN_STATUS(
986 legacy2aidl_audio_policy_dev_state_t_AudioPolicyDeviceState(state)),
Nathalie Le Clair88fa2752021-11-23 13:03:41 +0100987 port,
Mikhail Naganovb60bd1b2021-07-15 17:31:43 -0700988 VALUE_OR_RETURN_STATUS(
989 legacy2aidl_audio_format_t_AudioFormatDescription(encodedFormat))));
Eric Laurentc2f1f072009-07-17 12:17:14 -0700990}
991
Dima Zavinfce7a472011-04-19 22:30:36 -0700992audio_policy_dev_state_t AudioSystem::getDeviceConnectionState(audio_devices_t device,
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800993 const char* device_address) {
Andy Hung264fa4c2024-02-21 15:52:12 -0800994 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Dima Zavinfce7a472011-04-19 22:30:36 -0700995 if (aps == 0) return AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE;
Eric Laurentc2f1f072009-07-17 12:17:14 -0700996
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800997 auto result = [&]() -> ConversionResult<audio_policy_dev_state_t> {
Mikhail Naganov932cb962021-09-16 01:05:27 +0000998 AudioDevice deviceAidl = VALUE_OR_RETURN(
999 legacy2aidl_audio_device_AudioDevice(device, device_address));
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001000
1001 media::AudioPolicyDeviceState result;
1002 RETURN_IF_ERROR(statusTFromBinderStatus(
1003 aps->getDeviceConnectionState(deviceAidl, &result)));
1004
1005 return aidl2legacy_AudioPolicyDeviceState_audio_policy_dev_state_t(result);
1006 }();
1007 return result.value_or(AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE);
Eric Laurentc2f1f072009-07-17 12:17:14 -07001008}
1009
Pavlin Radoslavovf862bc62016-12-26 18:57:22 -08001010status_t AudioSystem::handleDeviceConfigChange(audio_devices_t device,
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001011 const char* device_address,
1012 const char* device_name,
1013 audio_format_t encodedFormat) {
Andy Hung264fa4c2024-02-21 15:52:12 -08001014 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001015 const char* address = "";
1016 const char* name = "";
Pavlin Radoslavovf862bc62016-12-26 18:57:22 -08001017
1018 if (aps == 0) return PERMISSION_DENIED;
1019
1020 if (device_address != NULL) {
1021 address = device_address;
1022 }
1023 if (device_name != NULL) {
1024 name = device_name;
1025 }
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001026
Mikhail Naganov932cb962021-09-16 01:05:27 +00001027 AudioDevice deviceAidl = VALUE_OR_RETURN_STATUS(
1028 legacy2aidl_audio_device_AudioDevice(device, address));
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001029
1030 return statusTFromBinderStatus(
1031 aps->handleDeviceConfigChange(deviceAidl, name, VALUE_OR_RETURN_STATUS(
Mikhail Naganovb60bd1b2021-07-15 17:31:43 -07001032 legacy2aidl_audio_format_t_AudioFormatDescription(encodedFormat))));
Pavlin Radoslavovf862bc62016-12-26 18:57:22 -08001033}
1034
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001035status_t AudioSystem::setPhoneState(audio_mode_t state, uid_t uid) {
Glenn Kasten347966c2012-01-18 14:58:32 -08001036 if (uint32_t(state) >= AUDIO_MODE_CNT) return BAD_VALUE;
Andy Hung264fa4c2024-02-21 15:52:12 -08001037 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Eric Laurentc2f1f072009-07-17 12:17:14 -07001038 if (aps == 0) return PERMISSION_DENIED;
1039
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001040 return statusTFromBinderStatus(aps->setPhoneState(
1041 VALUE_OR_RETURN_STATUS(legacy2aidl_audio_mode_t_AudioMode(state)),
1042 VALUE_OR_RETURN_STATUS(legacy2aidl_uid_t_int32_t(uid))));
Eric Laurentc2f1f072009-07-17 12:17:14 -07001043}
1044
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001045status_t
1046AudioSystem::setForceUse(audio_policy_force_use_t usage, audio_policy_forced_cfg_t config) {
Andy Hung264fa4c2024-02-21 15:52:12 -08001047 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Eric Laurentc2f1f072009-07-17 12:17:14 -07001048 if (aps == 0) return PERMISSION_DENIED;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001049
1050 return statusTFromBinderStatus(
1051 aps->setForceUse(
1052 VALUE_OR_RETURN_STATUS(
1053 legacy2aidl_audio_policy_force_use_t_AudioPolicyForceUse(usage)),
1054 VALUE_OR_RETURN_STATUS(
1055 legacy2aidl_audio_policy_forced_cfg_t_AudioPolicyForcedConfig(
1056 config))));
Eric Laurentc2f1f072009-07-17 12:17:14 -07001057}
1058
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001059audio_policy_forced_cfg_t AudioSystem::getForceUse(audio_policy_force_use_t usage) {
Andy Hung264fa4c2024-02-21 15:52:12 -08001060 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Dima Zavinfce7a472011-04-19 22:30:36 -07001061 if (aps == 0) return AUDIO_POLICY_FORCE_NONE;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001062
1063 auto result = [&]() -> ConversionResult<audio_policy_forced_cfg_t> {
Priyanka Advani (xWF)46555052024-11-20 18:24:09 +00001064 AudioPolicyForceUse usageAidl = VALUE_OR_RETURN(
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001065 legacy2aidl_audio_policy_force_use_t_AudioPolicyForceUse(usage));
Priyanka Advani (xWF)46555052024-11-20 18:24:09 +00001066 AudioPolicyForcedConfig configAidl;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001067 RETURN_IF_ERROR(statusTFromBinderStatus(
1068 aps->getForceUse(usageAidl, &configAidl)));
1069 return aidl2legacy_AudioPolicyForcedConfig_audio_policy_forced_cfg_t(configAidl);
1070 }();
1071
1072 return result.value_or(AUDIO_POLICY_FORCE_NONE);
Eric Laurentc2f1f072009-07-17 12:17:14 -07001073}
1074
1075
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001076audio_io_handle_t AudioSystem::getOutput(audio_stream_type_t stream) {
Andy Hung264fa4c2024-02-21 15:52:12 -08001077 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001078 if (aps == 0) return AUDIO_IO_HANDLE_NONE;
1079
1080 auto result = [&]() -> ConversionResult<audio_io_handle_t> {
Mikhail Naganovdbf03642021-08-25 18:15:32 -07001081 AudioStreamType streamAidl = VALUE_OR_RETURN(
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001082 legacy2aidl_audio_stream_type_t_AudioStreamType(stream));
1083 int32_t outputAidl;
1084 RETURN_IF_ERROR(
1085 statusTFromBinderStatus(aps->getOutput(streamAidl, &outputAidl)));
1086 return aidl2legacy_int32_t_audio_io_handle_t(outputAidl);
1087 }();
1088
1089 return result.value_or(AUDIO_IO_HANDLE_NONE);
Eric Laurentc2f1f072009-07-17 12:17:14 -07001090}
1091
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001092status_t AudioSystem::getOutputForAttr(audio_attributes_t* attr,
1093 audio_io_handle_t* output,
1094 audio_session_t session,
1095 audio_stream_type_t* stream,
Svet Ganov3e5f14f2021-05-13 22:51:08 +00001096 const AttributionSourceState& attributionSource,
jiabinf1c73972022-04-14 16:28:52 -07001097 audio_config_t* config,
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001098 audio_output_flags_t flags,
1099 audio_port_handle_t* selectedDeviceId,
1100 audio_port_handle_t* portId,
Eric Laurentb0a7bc92022-04-05 15:06:08 +02001101 std::vector<audio_io_handle_t>* secondaryOutputs,
jiabinc658e452022-10-21 20:52:21 +00001102 bool *isSpatialized,
1103 bool *isBitPerfect) {
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001104 if (attr == nullptr) {
1105 ALOGE("%s NULL audio attributes", __func__);
1106 return BAD_VALUE;
1107 }
1108 if (output == nullptr) {
1109 ALOGE("%s NULL output - shouldn't happen", __func__);
1110 return BAD_VALUE;
1111 }
1112 if (selectedDeviceId == nullptr) {
1113 ALOGE("%s NULL selectedDeviceId - shouldn't happen", __func__);
1114 return BAD_VALUE;
1115 }
1116 if (portId == nullptr) {
1117 ALOGE("%s NULL portId - shouldn't happen", __func__);
1118 return BAD_VALUE;
1119 }
1120 if (secondaryOutputs == nullptr) {
1121 ALOGE("%s NULL secondaryOutputs - shouldn't happen", __func__);
1122 return BAD_VALUE;
1123 }
1124
Andy Hung264fa4c2024-02-21 15:52:12 -08001125 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Eric Laurente83b55d2014-11-14 10:06:21 -08001126 if (aps == 0) return NO_INIT;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001127
Mikhail Naganov1c400902023-05-17 11:48:43 -07001128 media::audio::common::AudioAttributes attrAidl = VALUE_OR_RETURN_STATUS(
1129 legacy2aidl_audio_attributes_t_AudioAttributes(*attr));
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001130 int32_t sessionAidl = VALUE_OR_RETURN_STATUS(legacy2aidl_audio_session_t_int32_t(session));
Mikhail Naganovdbf03642021-08-25 18:15:32 -07001131 AudioConfig configAidl = VALUE_OR_RETURN_STATUS(
Mikhail Naganovde3fa182021-07-30 15:06:42 -07001132 legacy2aidl_audio_config_t_AudioConfig(*config, false /*isInput*/));
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001133 int32_t flagsAidl = VALUE_OR_RETURN_STATUS(
1134 legacy2aidl_audio_output_flags_t_int32_t_mask(flags));
Eric Laurentf99edd32021-02-01 15:57:33 +01001135 int32_t selectedDeviceIdAidl = VALUE_OR_RETURN_STATUS(
1136 legacy2aidl_audio_port_handle_t_int32_t(*selectedDeviceId));
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001137
1138 media::GetOutputForAttrResponse responseAidl;
1139
jiabinf1c73972022-04-14 16:28:52 -07001140 status_t status = statusTFromBinderStatus(
Svet Ganov3e5f14f2021-05-13 22:51:08 +00001141 aps->getOutputForAttr(attrAidl, sessionAidl, attributionSource, configAidl, flagsAidl,
jiabinf1c73972022-04-14 16:28:52 -07001142 selectedDeviceIdAidl, &responseAidl));
1143 if (status != NO_ERROR) {
1144 config->format = VALUE_OR_RETURN_STATUS(
1145 aidl2legacy_AudioFormatDescription_audio_format_t(responseAidl.configBase.format));
1146 config->channel_mask = VALUE_OR_RETURN_STATUS(
1147 aidl2legacy_AudioChannelLayout_audio_channel_mask_t(
1148 responseAidl.configBase.channelMask, false /*isInput*/));
1149 config->sample_rate = responseAidl.configBase.sampleRate;
1150 return status;
1151 }
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001152
1153 *output = VALUE_OR_RETURN_STATUS(
1154 aidl2legacy_int32_t_audio_io_handle_t(responseAidl.output));
1155
1156 if (stream != nullptr) {
1157 *stream = VALUE_OR_RETURN_STATUS(
1158 aidl2legacy_AudioStreamType_audio_stream_type_t(responseAidl.stream));
1159 }
1160 *selectedDeviceId = VALUE_OR_RETURN_STATUS(
1161 aidl2legacy_int32_t_audio_port_handle_t(responseAidl.selectedDeviceId));
1162 *portId = VALUE_OR_RETURN_STATUS(aidl2legacy_int32_t_audio_port_handle_t(responseAidl.portId));
1163 *secondaryOutputs = VALUE_OR_RETURN_STATUS(convertContainer<std::vector<audio_io_handle_t>>(
1164 responseAidl.secondaryOutputs, aidl2legacy_int32_t_audio_io_handle_t));
Eric Laurentb0a7bc92022-04-05 15:06:08 +02001165 *isSpatialized = responseAidl.isSpatialized;
jiabinc658e452022-10-21 20:52:21 +00001166 *isBitPerfect = responseAidl.isBitPerfect;
Eric Laurent0d13fea2022-11-04 17:12:08 +01001167 *attr = VALUE_OR_RETURN_STATUS(
Mikhail Naganov1c400902023-05-17 11:48:43 -07001168 aidl2legacy_AudioAttributes_audio_attributes_t(responseAidl.attr));
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001169
1170 return OK;
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -07001171}
1172
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001173status_t AudioSystem::startOutput(audio_port_handle_t portId) {
Andy Hung264fa4c2024-02-21 15:52:12 -08001174 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Eric Laurentc2f1f072009-07-17 12:17:14 -07001175 if (aps == 0) return PERMISSION_DENIED;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001176
1177 int32_t portIdAidl = VALUE_OR_RETURN_STATUS(legacy2aidl_audio_port_handle_t_int32_t(portId));
1178 return statusTFromBinderStatus(aps->startOutput(portIdAidl));
Eric Laurentc2f1f072009-07-17 12:17:14 -07001179}
1180
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001181status_t AudioSystem::stopOutput(audio_port_handle_t portId) {
Andy Hung264fa4c2024-02-21 15:52:12 -08001182 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Eric Laurentc2f1f072009-07-17 12:17:14 -07001183 if (aps == 0) return PERMISSION_DENIED;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001184
1185 int32_t portIdAidl = VALUE_OR_RETURN_STATUS(legacy2aidl_audio_port_handle_t_int32_t(portId));
1186 return statusTFromBinderStatus(aps->stopOutput(portIdAidl));
Eric Laurentc2f1f072009-07-17 12:17:14 -07001187}
1188
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001189void AudioSystem::releaseOutput(audio_port_handle_t portId) {
Andy Hung264fa4c2024-02-21 15:52:12 -08001190 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Eric Laurentc2f1f072009-07-17 12:17:14 -07001191 if (aps == 0) return;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001192
1193 auto status = [&]() -> status_t {
1194 int32_t portIdAidl = VALUE_OR_RETURN_STATUS(
1195 legacy2aidl_audio_port_handle_t_int32_t(portId));
1196 RETURN_STATUS_IF_ERROR(statusTFromBinderStatus(aps->releaseOutput(portIdAidl)));
1197 return OK;
1198 }();
1199
1200 // Ignore status.
1201 (void) status;
Eric Laurentc2f1f072009-07-17 12:17:14 -07001202}
1203
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001204status_t AudioSystem::getInputForAttr(const audio_attributes_t* attr,
1205 audio_io_handle_t* input,
1206 audio_unique_id_t riid,
1207 audio_session_t session,
Svet Ganov3e5f14f2021-05-13 22:51:08 +00001208 const AttributionSourceState &attributionSource,
jiabinf1c73972022-04-14 16:28:52 -07001209 audio_config_base_t* config,
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001210 audio_input_flags_t flags,
1211 audio_port_handle_t* selectedDeviceId,
1212 audio_port_handle_t* portId) {
1213 if (attr == NULL) {
1214 ALOGE("getInputForAttr NULL attr - shouldn't happen");
1215 return BAD_VALUE;
1216 }
1217 if (input == NULL) {
1218 ALOGE("getInputForAttr NULL input - shouldn't happen");
1219 return BAD_VALUE;
1220 }
1221 if (selectedDeviceId == NULL) {
1222 ALOGE("getInputForAttr NULL selectedDeviceId - shouldn't happen");
1223 return BAD_VALUE;
1224 }
1225 if (portId == NULL) {
1226 ALOGE("getInputForAttr NULL portId - shouldn't happen");
1227 return BAD_VALUE;
1228 }
1229
Andy Hung264fa4c2024-02-21 15:52:12 -08001230 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Eric Laurentcaf7f482014-11-25 17:50:47 -08001231 if (aps == 0) return NO_INIT;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001232
Mikhail Naganov1c400902023-05-17 11:48:43 -07001233 media::audio::common::AudioAttributes attrAidl = VALUE_OR_RETURN_STATUS(
1234 legacy2aidl_audio_attributes_t_AudioAttributes(*attr));
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001235 int32_t inputAidl = VALUE_OR_RETURN_STATUS(legacy2aidl_audio_io_handle_t_int32_t(*input));
1236 int32_t riidAidl = VALUE_OR_RETURN_STATUS(legacy2aidl_audio_unique_id_t_int32_t(riid));
1237 int32_t sessionAidl = VALUE_OR_RETURN_STATUS(legacy2aidl_audio_session_t_int32_t(session));
Mikhail Naganovdbf03642021-08-25 18:15:32 -07001238 AudioConfigBase configAidl = VALUE_OR_RETURN_STATUS(
Mikhail Naganovde3fa182021-07-30 15:06:42 -07001239 legacy2aidl_audio_config_base_t_AudioConfigBase(*config, true /*isInput*/));
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001240 int32_t flagsAidl = VALUE_OR_RETURN_STATUS(legacy2aidl_audio_input_flags_t_int32_t_mask(flags));
Eric Laurentf99edd32021-02-01 15:57:33 +01001241 int32_t selectedDeviceIdAidl = VALUE_OR_RETURN_STATUS(
1242 legacy2aidl_audio_port_handle_t_int32_t(*selectedDeviceId));
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001243
1244 media::GetInputForAttrResponse response;
1245
jiabinf1c73972022-04-14 16:28:52 -07001246 status_t status = statusTFromBinderStatus(
Svet Ganov3e5f14f2021-05-13 22:51:08 +00001247 aps->getInputForAttr(attrAidl, inputAidl, riidAidl, sessionAidl, attributionSource,
jiabinf1c73972022-04-14 16:28:52 -07001248 configAidl, flagsAidl, selectedDeviceIdAidl, &response));
1249 if (status != NO_ERROR) {
1250 *config = VALUE_OR_RETURN_STATUS(
1251 aidl2legacy_AudioConfigBase_audio_config_base_t(response.config, true /*isInput*/));
1252 return status;
1253 }
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001254
1255 *input = VALUE_OR_RETURN_STATUS(aidl2legacy_int32_t_audio_io_handle_t(response.input));
1256 *selectedDeviceId = VALUE_OR_RETURN_STATUS(
1257 aidl2legacy_int32_t_audio_port_handle_t(response.selectedDeviceId));
1258 *portId = VALUE_OR_RETURN_STATUS(aidl2legacy_int32_t_audio_port_handle_t(response.portId));
1259
1260 return OK;
Eric Laurentc2f1f072009-07-17 12:17:14 -07001261}
1262
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001263status_t AudioSystem::startInput(audio_port_handle_t portId) {
Andy Hung264fa4c2024-02-21 15:52:12 -08001264 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Eric Laurentc2f1f072009-07-17 12:17:14 -07001265 if (aps == 0) return PERMISSION_DENIED;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001266
1267 int32_t portIdAidl = VALUE_OR_RETURN_STATUS(legacy2aidl_audio_port_handle_t_int32_t(portId));
1268 return statusTFromBinderStatus(aps->startInput(portIdAidl));
Eric Laurentc2f1f072009-07-17 12:17:14 -07001269}
1270
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001271status_t AudioSystem::stopInput(audio_port_handle_t portId) {
Andy Hung264fa4c2024-02-21 15:52:12 -08001272 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Eric Laurentc2f1f072009-07-17 12:17:14 -07001273 if (aps == 0) return PERMISSION_DENIED;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001274
1275 int32_t portIdAidl = VALUE_OR_RETURN_STATUS(legacy2aidl_audio_port_handle_t_int32_t(portId));
1276 return statusTFromBinderStatus(aps->stopInput(portIdAidl));
Eric Laurentc2f1f072009-07-17 12:17:14 -07001277}
1278
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001279void AudioSystem::releaseInput(audio_port_handle_t portId) {
Andy Hung264fa4c2024-02-21 15:52:12 -08001280 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Eric Laurentc2f1f072009-07-17 12:17:14 -07001281 if (aps == 0) return;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001282
1283 auto status = [&]() -> status_t {
1284 int32_t portIdAidl = VALUE_OR_RETURN_STATUS(
1285 legacy2aidl_audio_port_handle_t_int32_t(portId));
1286 RETURN_STATUS_IF_ERROR(statusTFromBinderStatus(aps->releaseInput(portIdAidl)));
1287 return OK;
1288 }();
1289
1290 // Ignore status.
1291 (void) status;
Eric Laurentc2f1f072009-07-17 12:17:14 -07001292}
1293
Vlad Popa87e0e582024-05-20 18:49:20 -07001294status_t AudioSystem::setDeviceAbsoluteVolumeEnabled(audio_devices_t deviceType,
1295 const char *address,
1296 bool enabled,
1297 audio_stream_type_t streamToDriveAbs) {
1298 const sp<IAudioPolicyService> aps = get_audio_policy_service();
1299 if (aps == nullptr) return PERMISSION_DENIED;
1300
1301 AudioDevice deviceAidl = VALUE_OR_RETURN_STATUS(
1302 legacy2aidl_audio_device_AudioDevice(deviceType, address));
1303 AudioStreamType streamToDriveAbsAidl = VALUE_OR_RETURN_STATUS(
1304 legacy2aidl_audio_stream_type_t_AudioStreamType(streamToDriveAbs));
1305 return statusTFromBinderStatus(
1306 aps->setDeviceAbsoluteVolumeEnabled(deviceAidl, enabled, streamToDriveAbsAidl));
1307}
1308
Dima Zavinfce7a472011-04-19 22:30:36 -07001309status_t AudioSystem::initStreamVolume(audio_stream_type_t stream,
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001310 int indexMin,
1311 int indexMax) {
Andy Hung264fa4c2024-02-21 15:52:12 -08001312 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Eric Laurentc2f1f072009-07-17 12:17:14 -07001313 if (aps == 0) return PERMISSION_DENIED;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001314
Mikhail Naganovdbf03642021-08-25 18:15:32 -07001315 AudioStreamType streamAidl = VALUE_OR_RETURN_STATUS(
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001316 legacy2aidl_audio_stream_type_t_AudioStreamType(stream));
1317 int32_t indexMinAidl = VALUE_OR_RETURN_STATUS(convertIntegral<int32_t>(indexMin));
1318 int32_t indexMaxAidl = VALUE_OR_RETURN_STATUS(convertIntegral<int32_t>(indexMax));
Mikhail Naganovec3d5792022-05-06 00:19:55 +00001319 status_t status = statusTFromBinderStatus(
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001320 aps->initStreamVolume(streamAidl, indexMinAidl, indexMaxAidl));
Mikhail Naganovec3d5792022-05-06 00:19:55 +00001321 if (status == DEAD_OBJECT) {
1322 // This is a critical operation since w/o proper stream volumes no audio
1323 // will be heard. Make sure we recover from a failure in any case.
1324 ALOGE("Received DEAD_OBJECT from APS, clearing the client");
1325 clearAudioPolicyService();
1326 }
1327 return status;
Eric Laurentc2f1f072009-07-17 12:17:14 -07001328}
1329
Eric Laurent83844cc2011-11-18 16:43:31 -08001330status_t AudioSystem::setStreamVolumeIndex(audio_stream_type_t stream,
1331 int index,
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001332 audio_devices_t device) {
Andy Hung264fa4c2024-02-21 15:52:12 -08001333 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Eric Laurentc2f1f072009-07-17 12:17:14 -07001334 if (aps == 0) return PERMISSION_DENIED;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001335
Mikhail Naganovdbf03642021-08-25 18:15:32 -07001336 AudioStreamType streamAidl = VALUE_OR_RETURN_STATUS(
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001337 legacy2aidl_audio_stream_type_t_AudioStreamType(stream));
1338 int32_t indexAidl = VALUE_OR_RETURN_STATUS(convertIntegral<int32_t>(index));
Mikhail Naganovf4a75362021-09-16 00:02:54 +00001339 AudioDeviceDescription deviceAidl = VALUE_OR_RETURN_STATUS(
Mikhail Naganov21a32ec2021-07-08 14:40:12 -07001340 legacy2aidl_audio_devices_t_AudioDeviceDescription(device));
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001341 return statusTFromBinderStatus(
1342 aps->setStreamVolumeIndex(streamAidl, deviceAidl, indexAidl));
Eric Laurentc2f1f072009-07-17 12:17:14 -07001343}
1344
Eric Laurent83844cc2011-11-18 16:43:31 -08001345status_t AudioSystem::getStreamVolumeIndex(audio_stream_type_t stream,
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001346 int* index,
1347 audio_devices_t device) {
Andy Hung264fa4c2024-02-21 15:52:12 -08001348 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Eric Laurentc2f1f072009-07-17 12:17:14 -07001349 if (aps == 0) return PERMISSION_DENIED;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001350
Mikhail Naganovdbf03642021-08-25 18:15:32 -07001351 AudioStreamType streamAidl = VALUE_OR_RETURN_STATUS(
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001352 legacy2aidl_audio_stream_type_t_AudioStreamType(stream));
Mikhail Naganovf4a75362021-09-16 00:02:54 +00001353 AudioDeviceDescription deviceAidl = VALUE_OR_RETURN_STATUS(
Mikhail Naganov21a32ec2021-07-08 14:40:12 -07001354 legacy2aidl_audio_devices_t_AudioDeviceDescription(device));
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001355 int32_t indexAidl;
1356 RETURN_STATUS_IF_ERROR(statusTFromBinderStatus(
1357 aps->getStreamVolumeIndex(streamAidl, deviceAidl, &indexAidl)));
1358 if (index != nullptr) {
1359 *index = VALUE_OR_RETURN_STATUS(convertIntegral<int>(indexAidl));
1360 }
1361 return OK;
Eric Laurentc2f1f072009-07-17 12:17:14 -07001362}
1363
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001364status_t AudioSystem::setVolumeIndexForAttributes(const audio_attributes_t& attr,
François Gaffiecfe17322018-11-07 13:41:29 +01001365 int index,
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001366 audio_devices_t device) {
Andy Hung264fa4c2024-02-21 15:52:12 -08001367 const sp<IAudioPolicyService> aps = get_audio_policy_service();
François Gaffiecfe17322018-11-07 13:41:29 +01001368 if (aps == 0) return PERMISSION_DENIED;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001369
Mikhail Naganov1c400902023-05-17 11:48:43 -07001370 media::audio::common::AudioAttributes attrAidl = VALUE_OR_RETURN_STATUS(
1371 legacy2aidl_audio_attributes_t_AudioAttributes(attr));
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001372 int32_t indexAidl = VALUE_OR_RETURN_STATUS(convertIntegral<int32_t>(index));
Mikhail Naganovf4a75362021-09-16 00:02:54 +00001373 AudioDeviceDescription deviceAidl = VALUE_OR_RETURN_STATUS(
Mikhail Naganov21a32ec2021-07-08 14:40:12 -07001374 legacy2aidl_audio_devices_t_AudioDeviceDescription(device));
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001375 return statusTFromBinderStatus(
1376 aps->setVolumeIndexForAttributes(attrAidl, deviceAidl, indexAidl));
François Gaffiecfe17322018-11-07 13:41:29 +01001377}
1378
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001379status_t AudioSystem::getVolumeIndexForAttributes(const audio_attributes_t& attr,
1380 int& index,
1381 audio_devices_t device) {
Andy Hung264fa4c2024-02-21 15:52:12 -08001382 const sp<IAudioPolicyService> aps = get_audio_policy_service();
François Gaffiecfe17322018-11-07 13:41:29 +01001383 if (aps == 0) return PERMISSION_DENIED;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001384
Mikhail Naganov1c400902023-05-17 11:48:43 -07001385 media::audio::common::AudioAttributes attrAidl = VALUE_OR_RETURN_STATUS(
1386 legacy2aidl_audio_attributes_t_AudioAttributes(attr));
Mikhail Naganovf4a75362021-09-16 00:02:54 +00001387 AudioDeviceDescription deviceAidl = VALUE_OR_RETURN_STATUS(
Mikhail Naganov21a32ec2021-07-08 14:40:12 -07001388 legacy2aidl_audio_devices_t_AudioDeviceDescription(device));
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001389 int32_t indexAidl;
1390 RETURN_STATUS_IF_ERROR(statusTFromBinderStatus(
1391 aps->getVolumeIndexForAttributes(attrAidl, deviceAidl, &indexAidl)));
1392 index = VALUE_OR_RETURN_STATUS(convertIntegral<int>(indexAidl));
1393 return OK;
François Gaffiecfe17322018-11-07 13:41:29 +01001394}
1395
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001396status_t AudioSystem::getMaxVolumeIndexForAttributes(const audio_attributes_t& attr, int& index) {
Andy Hung264fa4c2024-02-21 15:52:12 -08001397 const sp<IAudioPolicyService> aps = get_audio_policy_service();
François Gaffiecfe17322018-11-07 13:41:29 +01001398 if (aps == 0) return PERMISSION_DENIED;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001399
Mikhail Naganov1c400902023-05-17 11:48:43 -07001400 media::audio::common::AudioAttributes attrAidl = VALUE_OR_RETURN_STATUS(
1401 legacy2aidl_audio_attributes_t_AudioAttributes(attr));
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001402 int32_t indexAidl;
1403 RETURN_STATUS_IF_ERROR(statusTFromBinderStatus(
1404 aps->getMaxVolumeIndexForAttributes(attrAidl, &indexAidl)));
1405 index = VALUE_OR_RETURN_STATUS(convertIntegral<int>(indexAidl));
1406 return OK;
François Gaffiecfe17322018-11-07 13:41:29 +01001407}
1408
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001409status_t AudioSystem::getMinVolumeIndexForAttributes(const audio_attributes_t& attr, int& index) {
Andy Hung264fa4c2024-02-21 15:52:12 -08001410 const sp<IAudioPolicyService> aps = get_audio_policy_service();
François Gaffiecfe17322018-11-07 13:41:29 +01001411 if (aps == 0) return PERMISSION_DENIED;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001412
Mikhail Naganov1c400902023-05-17 11:48:43 -07001413 media::audio::common::AudioAttributes attrAidl = VALUE_OR_RETURN_STATUS(
1414 legacy2aidl_audio_attributes_t_AudioAttributes(attr));
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001415 int32_t indexAidl;
1416 RETURN_STATUS_IF_ERROR(statusTFromBinderStatus(
1417 aps->getMinVolumeIndexForAttributes(attrAidl, &indexAidl)));
1418 index = VALUE_OR_RETURN_STATUS(convertIntegral<int>(indexAidl));
1419 return OK;
François Gaffiecfe17322018-11-07 13:41:29 +01001420}
1421
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001422product_strategy_t AudioSystem::getStrategyForStream(audio_stream_type_t stream) {
Andy Hung264fa4c2024-02-21 15:52:12 -08001423 const sp<IAudioPolicyService> aps = get_audio_policy_service();
François Gaffied0ba9ed2018-11-05 11:50:42 +01001424 if (aps == 0) return PRODUCT_STRATEGY_NONE;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001425
1426 auto result = [&]() -> ConversionResult<product_strategy_t> {
Mikhail Naganovdbf03642021-08-25 18:15:32 -07001427 AudioStreamType streamAidl = VALUE_OR_RETURN(
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001428 legacy2aidl_audio_stream_type_t_AudioStreamType(stream));
1429 int32_t resultAidl;
1430 RETURN_IF_ERROR(statusTFromBinderStatus(
1431 aps->getStrategyForStream(streamAidl, &resultAidl)));
1432 return aidl2legacy_int32_t_product_strategy_t(resultAidl);
1433 }();
1434 return result.value_or(PRODUCT_STRATEGY_NONE);
Eric Laurentde070132010-07-13 04:45:46 -07001435}
1436
François Gaffie1e2b56f2022-04-01 14:34:29 +02001437status_t AudioSystem::getDevicesForAttributes(const audio_attributes_t& aa,
Andy Hung6d23c0f2022-02-16 09:37:15 -08001438 AudioDeviceTypeAddrVector* devices,
1439 bool forVolume) {
Jean-Michel Trivif41599b2020-01-07 14:22:08 -08001440 if (devices == nullptr) {
1441 return BAD_VALUE;
1442 }
Andy Hung264fa4c2024-02-21 15:52:12 -08001443 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Jean-Michel Trivif41599b2020-01-07 14:22:08 -08001444 if (aps == 0) return PERMISSION_DENIED;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001445
Mikhail Naganov1c400902023-05-17 11:48:43 -07001446 media::audio::common::AudioAttributes aaAidl = VALUE_OR_RETURN_STATUS(
1447 legacy2aidl_audio_attributes_t_AudioAttributes(aa));
Mikhail Naganovf4a75362021-09-16 00:02:54 +00001448 std::vector<AudioDevice> retAidl;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001449 RETURN_STATUS_IF_ERROR(
Andy Hung6d23c0f2022-02-16 09:37:15 -08001450 statusTFromBinderStatus(aps->getDevicesForAttributes(aaAidl, forVolume, &retAidl)));
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001451 *devices = VALUE_OR_RETURN_STATUS(
1452 convertContainer<AudioDeviceTypeAddrVector>(
1453 retAidl,
1454 aidl2legacy_AudioDeviceTypeAddress));
1455 return OK;
Jean-Michel Trivif41599b2020-01-07 14:22:08 -08001456}
1457
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001458audio_io_handle_t AudioSystem::getOutputForEffect(const effect_descriptor_t* desc) {
Andy Hung264fa4c2024-02-21 15:52:12 -08001459 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Glenn Kastenefa6ea92014-01-08 09:10:43 -08001460 // FIXME change return type to status_t, and return PERMISSION_DENIED here
Glenn Kasten142f5192014-03-25 17:44:59 -07001461 if (aps == 0) return AUDIO_IO_HANDLE_NONE;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001462
1463 auto result = [&]() -> ConversionResult<audio_io_handle_t> {
1464 media::EffectDescriptor descAidl = VALUE_OR_RETURN(
1465 legacy2aidl_effect_descriptor_t_EffectDescriptor(*desc));
1466 int32_t retAidl;
1467 RETURN_IF_ERROR(
1468 statusTFromBinderStatus(aps->getOutputForEffect(descAidl, &retAidl)));
1469 return aidl2legacy_int32_t_audio_io_handle_t(retAidl);
1470 }();
1471
1472 return result.value_or(AUDIO_IO_HANDLE_NONE);
Eric Laurentde070132010-07-13 04:45:46 -07001473}
1474
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001475status_t AudioSystem::registerEffect(const effect_descriptor_t* desc,
1476 audio_io_handle_t io,
1477 product_strategy_t strategy,
1478 audio_session_t session,
1479 int id) {
Andy Hung264fa4c2024-02-21 15:52:12 -08001480 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Eric Laurentde070132010-07-13 04:45:46 -07001481 if (aps == 0) return PERMISSION_DENIED;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001482
1483 media::EffectDescriptor descAidl = VALUE_OR_RETURN_STATUS(
1484 legacy2aidl_effect_descriptor_t_EffectDescriptor(*desc));
1485 int32_t ioAidl = VALUE_OR_RETURN_STATUS(legacy2aidl_audio_io_handle_t_int32_t(io));
1486 int32_t strategyAidl = VALUE_OR_RETURN_STATUS(aidl2legacy_int32_t_product_strategy_t(strategy));
1487 int32_t sessionAidl = VALUE_OR_RETURN_STATUS(legacy2aidl_audio_session_t_int32_t(session));
1488 int32_t idAidl = VALUE_OR_RETURN_STATUS(convertReinterpret<int32_t>(id));
1489 return statusTFromBinderStatus(
1490 aps->registerEffect(descAidl, ioAidl, strategyAidl, sessionAidl, idAidl));
Eric Laurentde070132010-07-13 04:45:46 -07001491}
1492
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001493status_t AudioSystem::unregisterEffect(int id) {
Andy Hung264fa4c2024-02-21 15:52:12 -08001494 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Eric Laurentde070132010-07-13 04:45:46 -07001495 if (aps == 0) return PERMISSION_DENIED;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001496
1497 int32_t idAidl = VALUE_OR_RETURN_STATUS(convertReinterpret<int32_t>(id));
1498 return statusTFromBinderStatus(
1499 aps->unregisterEffect(idAidl));
Eric Laurentde070132010-07-13 04:45:46 -07001500}
1501
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001502status_t AudioSystem::setEffectEnabled(int id, bool enabled) {
Andy Hung264fa4c2024-02-21 15:52:12 -08001503 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Eric Laurentdb7c0792011-08-10 10:37:50 -07001504 if (aps == 0) return PERMISSION_DENIED;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001505
1506 int32_t idAidl = VALUE_OR_RETURN_STATUS(convertReinterpret<int32_t>(id));
1507 return statusTFromBinderStatus(
1508 aps->setEffectEnabled(idAidl, enabled));
Eric Laurentdb7c0792011-08-10 10:37:50 -07001509}
1510
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001511status_t AudioSystem::moveEffectsToIo(const std::vector<int>& ids, audio_io_handle_t io) {
Andy Hung264fa4c2024-02-21 15:52:12 -08001512 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Eric Laurent6c796322019-04-09 14:13:17 -07001513 if (aps == 0) return PERMISSION_DENIED;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001514
1515 std::vector<int32_t> idsAidl = VALUE_OR_RETURN_STATUS(
1516 convertContainer<std::vector<int32_t>>(ids, convertReinterpret<int32_t, int>));
1517 int32_t ioAidl = VALUE_OR_RETURN_STATUS(legacy2aidl_audio_io_handle_t_int32_t(io));
1518 return statusTFromBinderStatus(aps->moveEffectsToIo(idsAidl, ioAidl));
Eric Laurent6c796322019-04-09 14:13:17 -07001519}
1520
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001521status_t AudioSystem::isStreamActive(audio_stream_type_t stream, bool* state, uint32_t inPastMs) {
Andy Hung264fa4c2024-02-21 15:52:12 -08001522 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Eric Laurenteda6c362011-02-02 09:33:30 -08001523 if (aps == 0) return PERMISSION_DENIED;
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001524 if (state == NULL) return BAD_VALUE;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001525
Mikhail Naganovdbf03642021-08-25 18:15:32 -07001526 AudioStreamType streamAidl = VALUE_OR_RETURN_STATUS(
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001527 legacy2aidl_audio_stream_type_t_AudioStreamType(stream));
1528 int32_t inPastMsAidl = VALUE_OR_RETURN_STATUS(convertIntegral<int32_t>(inPastMs));
1529 RETURN_STATUS_IF_ERROR(statusTFromBinderStatus(
1530 aps->isStreamActive(streamAidl, inPastMsAidl, state)));
1531 return OK;
Eric Laurenteda6c362011-02-02 09:33:30 -08001532}
1533
Jean-Michel Trivi272ab542013-02-04 16:26:02 -08001534status_t AudioSystem::isStreamActiveRemotely(audio_stream_type_t stream, bool* state,
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001535 uint32_t inPastMs) {
Andy Hung264fa4c2024-02-21 15:52:12 -08001536 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Jean-Michel Trivi272ab542013-02-04 16:26:02 -08001537 if (aps == 0) return PERMISSION_DENIED;
1538 if (state == NULL) return BAD_VALUE;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001539
Mikhail Naganovdbf03642021-08-25 18:15:32 -07001540 AudioStreamType streamAidl = VALUE_OR_RETURN_STATUS(
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001541 legacy2aidl_audio_stream_type_t_AudioStreamType(stream));
1542 int32_t inPastMsAidl = VALUE_OR_RETURN_STATUS(convertIntegral<int32_t>(inPastMs));
1543 RETURN_STATUS_IF_ERROR(statusTFromBinderStatus(
1544 aps->isStreamActiveRemotely(streamAidl, inPastMsAidl, state)));
1545 return OK;
Jean-Michel Trivi272ab542013-02-04 16:26:02 -08001546}
1547
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001548status_t AudioSystem::isSourceActive(audio_source_t stream, bool* state) {
Andy Hung264fa4c2024-02-21 15:52:12 -08001549 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Jean-Michel Trivid7086032012-10-10 12:11:16 -07001550 if (aps == 0) return PERMISSION_DENIED;
1551 if (state == NULL) return BAD_VALUE;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001552
Mikhail Naganovddceecc2021-09-03 13:58:56 -07001553 AudioSource streamAidl = VALUE_OR_RETURN_STATUS(
1554 legacy2aidl_audio_source_t_AudioSource(stream));
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001555 RETURN_STATUS_IF_ERROR(statusTFromBinderStatus(
1556 aps->isSourceActive(streamAidl, state)));
1557 return OK;
Jean-Michel Trivid7086032012-10-10 12:11:16 -07001558}
1559
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001560uint32_t AudioSystem::getPrimaryOutputSamplingRate() {
Andy Hung264fa4c2024-02-21 15:52:12 -08001561 const sp<IAudioFlinger> af = get_audio_flinger();
Glenn Kastencc0f1cf2012-09-24 11:27:18 -07001562 if (af == 0) return 0;
1563 return af->getPrimaryOutputSamplingRate();
1564}
1565
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001566size_t AudioSystem::getPrimaryOutputFrameCount() {
Andy Hung264fa4c2024-02-21 15:52:12 -08001567 const sp<IAudioFlinger> af = get_audio_flinger();
Glenn Kastencc0f1cf2012-09-24 11:27:18 -07001568 if (af == 0) return 0;
1569 return af->getPrimaryOutputFrameCount();
1570}
Eric Laurenteda6c362011-02-02 09:33:30 -08001571
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001572status_t AudioSystem::setLowRamDevice(bool isLowRamDevice, int64_t totalMemory) {
Andy Hung264fa4c2024-02-21 15:52:12 -08001573 const sp<IAudioFlinger> af = get_audio_flinger();
Glenn Kasten4182c4e2013-07-15 14:45:07 -07001574 if (af == 0) return PERMISSION_DENIED;
Andy Hung6f248bb2018-01-23 14:04:37 -08001575 return af->setLowRamDevice(isLowRamDevice, totalMemory);
Glenn Kasten4182c4e2013-07-15 14:45:07 -07001576}
1577
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001578void AudioSystem::clearAudioConfigCache() {
Glenn Kastend2d089f2014-11-05 11:48:12 -08001579 // called by restoreTrack_l(), which needs new IAudioFlinger and IAudioPolicyService instances
Steve Block3856b092011-10-20 11:56:00 +01001580 ALOGV("clearAudioConfigCache()");
Andy Hungda1fb072024-02-20 19:08:08 -08001581 gAudioFlingerServiceHandler.clearService();
Mikhail Naganovec3d5792022-05-06 00:19:55 +00001582 clearAudioPolicyService();
Eric Laurent9f6530f2011-08-30 10:18:54 -07001583}
1584
Hayden Gomes524159d2019-12-23 14:41:47 -08001585status_t AudioSystem::setSupportedSystemUsages(const std::vector<audio_usage_t>& systemUsages) {
Andy Hung264fa4c2024-02-21 15:52:12 -08001586 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Hayden Gomes524159d2019-12-23 14:41:47 -08001587 if (aps == nullptr) return PERMISSION_DENIED;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001588
Mikhail Naganovdbf03642021-08-25 18:15:32 -07001589 std::vector<AudioUsage> systemUsagesAidl = VALUE_OR_RETURN_STATUS(
1590 convertContainer<std::vector<AudioUsage>>(systemUsages,
1591 legacy2aidl_audio_usage_t_AudioUsage));
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001592 return statusTFromBinderStatus(aps->setSupportedSystemUsages(systemUsagesAidl));
Hayden Gomes524159d2019-12-23 14:41:47 -08001593}
1594
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001595status_t AudioSystem::setAllowedCapturePolicy(uid_t uid, audio_flags_mask_t capturePolicy) {
Andy Hung264fa4c2024-02-21 15:52:12 -08001596 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Kevin Rocardb99cc752019-03-21 20:52:24 -07001597 if (aps == nullptr) return PERMISSION_DENIED;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001598
1599 int32_t uidAidl = VALUE_OR_RETURN_STATUS(legacy2aidl_uid_t_int32_t(uid));
1600 int32_t capturePolicyAidl = VALUE_OR_RETURN_STATUS(
1601 legacy2aidl_audio_flags_mask_t_int32_t_mask(capturePolicy));
1602 return statusTFromBinderStatus(aps->setAllowedCapturePolicy(uidAidl, capturePolicyAidl));
Kevin Rocardb99cc752019-03-21 20:52:24 -07001603}
1604
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001605audio_offload_mode_t AudioSystem::getOffloadSupport(const audio_offload_info_t& info) {
Eric Laurent90fe31c2020-11-26 20:06:35 +01001606 ALOGV("%s", __func__);
Andy Hung264fa4c2024-02-21 15:52:12 -08001607 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Eric Laurent90fe31c2020-11-26 20:06:35 +01001608 if (aps == 0) return AUDIO_OFFLOAD_NOT_SUPPORTED;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001609
1610 auto result = [&]() -> ConversionResult<audio_offload_mode_t> {
Mikhail Naganovdbf03642021-08-25 18:15:32 -07001611 AudioOffloadInfo infoAidl = VALUE_OR_RETURN(
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001612 legacy2aidl_audio_offload_info_t_AudioOffloadInfo(info));
1613 media::AudioOffloadMode retAidl;
1614 RETURN_IF_ERROR(
1615 statusTFromBinderStatus(aps->getOffloadSupport(infoAidl, &retAidl)));
1616 return aidl2legacy_AudioOffloadMode_audio_offload_mode_t(retAidl);
1617 }();
1618
1619 return result.value_or(static_cast<audio_offload_mode_t>(0));
Richard Fitzgeraldad3af332013-03-25 16:54:37 +00001620}
1621
Eric Laurent203b1a12014-04-01 10:34:16 -07001622status_t AudioSystem::listAudioPorts(audio_port_role_t role,
1623 audio_port_type_t type,
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001624 unsigned int* num_ports,
1625 struct audio_port_v7* ports,
1626 unsigned int* generation) {
1627 if (num_ports == nullptr || (*num_ports != 0 && ports == nullptr) ||
1628 generation == nullptr) {
1629 return BAD_VALUE;
1630 }
1631
Andy Hung264fa4c2024-02-21 15:52:12 -08001632 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Eric Laurent203b1a12014-04-01 10:34:16 -07001633 if (aps == 0) return PERMISSION_DENIED;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001634
1635 media::AudioPortRole roleAidl = VALUE_OR_RETURN_STATUS(
1636 legacy2aidl_audio_port_role_t_AudioPortRole(role));
1637 media::AudioPortType typeAidl = VALUE_OR_RETURN_STATUS(
1638 legacy2aidl_audio_port_type_t_AudioPortType(type));
Mikhail Naganov0078ee52021-09-30 23:06:20 +00001639 Int numPortsAidl;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001640 numPortsAidl.value = VALUE_OR_RETURN_STATUS(convertIntegral<int32_t>(*num_ports));
Atneya Nair638a6e42022-12-18 16:45:15 -08001641 std::vector<media::AudioPortFw> portsAidl;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001642 int32_t generationAidl;
1643
1644 RETURN_STATUS_IF_ERROR(statusTFromBinderStatus(
1645 aps->listAudioPorts(roleAidl, typeAidl, &numPortsAidl, &portsAidl, &generationAidl)));
1646 *num_ports = VALUE_OR_RETURN_STATUS(convertIntegral<unsigned int>(numPortsAidl.value));
1647 *generation = VALUE_OR_RETURN_STATUS(convertIntegral<unsigned int>(generationAidl));
1648 RETURN_STATUS_IF_ERROR(convertRange(portsAidl.begin(), portsAidl.end(), ports,
Mikhail Naganov87227252023-01-13 17:38:10 -08001649 aidl2legacy_AudioPortFw_audio_port_v7));
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001650 return OK;
Eric Laurent203b1a12014-04-01 10:34:16 -07001651}
1652
Mikhail Naganov5edc5ed2023-03-23 14:52:15 -07001653status_t AudioSystem::listDeclaredDevicePorts(media::AudioPortRole role,
1654 std::vector<media::AudioPortFw>* result) {
1655 if (result == nullptr) return BAD_VALUE;
Andy Hung264fa4c2024-02-21 15:52:12 -08001656 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Mikhail Naganov5edc5ed2023-03-23 14:52:15 -07001657 if (aps == 0) return PERMISSION_DENIED;
1658 RETURN_STATUS_IF_ERROR(statusTFromBinderStatus(aps->listDeclaredDevicePorts(role, result)));
1659 return OK;
1660}
1661
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001662status_t AudioSystem::getAudioPort(struct audio_port_v7* port) {
1663 if (port == nullptr) {
1664 return BAD_VALUE;
1665 }
Andy Hung264fa4c2024-02-21 15:52:12 -08001666 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Eric Laurent203b1a12014-04-01 10:34:16 -07001667 if (aps == 0) return PERMISSION_DENIED;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001668
Atneya Nair638a6e42022-12-18 16:45:15 -08001669 media::AudioPortFw portAidl;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001670 RETURN_STATUS_IF_ERROR(
Mikhail Naganov17031562022-02-23 23:00:27 +00001671 statusTFromBinderStatus(aps->getAudioPort(port->id, &portAidl)));
Mikhail Naganov87227252023-01-13 17:38:10 -08001672 *port = VALUE_OR_RETURN_STATUS(aidl2legacy_AudioPortFw_audio_port_v7(portAidl));
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001673 return OK;
Eric Laurent203b1a12014-04-01 10:34:16 -07001674}
1675
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001676status_t AudioSystem::createAudioPatch(const struct audio_patch* patch,
1677 audio_patch_handle_t* handle) {
1678 if (patch == nullptr || handle == nullptr) {
1679 return BAD_VALUE;
1680 }
1681
Andy Hung264fa4c2024-02-21 15:52:12 -08001682 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Eric Laurent203b1a12014-04-01 10:34:16 -07001683 if (aps == 0) return PERMISSION_DENIED;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001684
Atneya Nair3afdbd12022-12-18 16:14:18 -08001685 media::AudioPatchFw patchAidl = VALUE_OR_RETURN_STATUS(
Mikhail Naganov87227252023-01-13 17:38:10 -08001686 legacy2aidl_audio_patch_AudioPatchFw(*patch));
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001687 int32_t handleAidl = VALUE_OR_RETURN_STATUS(legacy2aidl_audio_patch_handle_t_int32_t(*handle));
1688 RETURN_STATUS_IF_ERROR(
1689 statusTFromBinderStatus(aps->createAudioPatch(patchAidl, handleAidl, &handleAidl)));
1690 *handle = VALUE_OR_RETURN_STATUS(aidl2legacy_int32_t_audio_patch_handle_t(handleAidl));
1691 return OK;
Eric Laurent203b1a12014-04-01 10:34:16 -07001692}
1693
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001694status_t AudioSystem::releaseAudioPatch(audio_patch_handle_t handle) {
Andy Hung264fa4c2024-02-21 15:52:12 -08001695 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Eric Laurent203b1a12014-04-01 10:34:16 -07001696 if (aps == 0) return PERMISSION_DENIED;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001697
1698 int32_t handleAidl = VALUE_OR_RETURN_STATUS(legacy2aidl_audio_patch_handle_t_int32_t(handle));
1699 return statusTFromBinderStatus(aps->releaseAudioPatch(handleAidl));
Eric Laurent203b1a12014-04-01 10:34:16 -07001700}
1701
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001702status_t AudioSystem::listAudioPatches(unsigned int* num_patches,
1703 struct audio_patch* patches,
1704 unsigned int* generation) {
1705 if (num_patches == nullptr || (*num_patches != 0 && patches == nullptr) ||
1706 generation == nullptr) {
1707 return BAD_VALUE;
1708 }
1709
Andy Hung264fa4c2024-02-21 15:52:12 -08001710 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Eric Laurent203b1a12014-04-01 10:34:16 -07001711 if (aps == 0) return PERMISSION_DENIED;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001712
1713
Mikhail Naganov0078ee52021-09-30 23:06:20 +00001714 Int numPatchesAidl;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001715 numPatchesAidl.value = VALUE_OR_RETURN_STATUS(convertIntegral<int32_t>(*num_patches));
Atneya Nair3afdbd12022-12-18 16:14:18 -08001716 std::vector<media::AudioPatchFw> patchesAidl;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001717 int32_t generationAidl;
1718
1719 RETURN_STATUS_IF_ERROR(statusTFromBinderStatus(
1720 aps->listAudioPatches(&numPatchesAidl, &patchesAidl, &generationAidl)));
1721 *num_patches = VALUE_OR_RETURN_STATUS(convertIntegral<unsigned int>(numPatchesAidl.value));
1722 *generation = VALUE_OR_RETURN_STATUS(convertIntegral<unsigned int>(generationAidl));
1723 RETURN_STATUS_IF_ERROR(convertRange(patchesAidl.begin(), patchesAidl.end(), patches,
Mikhail Naganov87227252023-01-13 17:38:10 -08001724 aidl2legacy_AudioPatchFw_audio_patch));
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001725 return OK;
Eric Laurent203b1a12014-04-01 10:34:16 -07001726}
1727
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001728status_t AudioSystem::setAudioPortConfig(const struct audio_port_config* config) {
1729 if (config == nullptr) {
1730 return BAD_VALUE;
1731 }
1732
Andy Hung264fa4c2024-02-21 15:52:12 -08001733 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Eric Laurent203b1a12014-04-01 10:34:16 -07001734 if (aps == 0) return PERMISSION_DENIED;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001735
Atneya Nair7a9594f2022-12-18 17:26:26 -08001736 media::AudioPortConfigFw configAidl = VALUE_OR_RETURN_STATUS(
Mikhail Naganov87227252023-01-13 17:38:10 -08001737 legacy2aidl_audio_port_config_AudioPortConfigFw(*config));
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001738 return statusTFromBinderStatus(aps->setAudioPortConfig(configAidl));
Eric Laurent203b1a12014-04-01 10:34:16 -07001739}
1740
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001741status_t AudioSystem::addAudioPortCallback(const sp<AudioPortCallback>& callback) {
Andy Hung264fa4c2024-02-21 15:52:12 -08001742 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Eric Laurentb28753e2015-04-01 13:06:28 -07001743 if (aps == 0) return PERMISSION_DENIED;
Andy Hung88e71732024-02-21 16:44:27 -08001744 const auto apc = gAudioPolicyServiceHandler.getClient();
1745 if (apc == nullptr) return NO_INIT;
Eric Laurentb28753e2015-04-01 13:06:28 -07001746
Andy Hung88e71732024-02-21 16:44:27 -08001747 std::lock_guard _l(gApsCallbackMutex);
1748 const int ret = apc->addAudioPortCallback(callback);
Eric Laurente8726fe2015-06-26 09:39:24 -07001749 if (ret == 1) {
1750 aps->setAudioPortCallbacksEnabled(true);
1751 }
1752 return (ret < 0) ? INVALID_OPERATION : NO_ERROR;
Eric Laurentb52c1522014-05-20 11:27:36 -07001753}
1754
Jean-Michel Trivif613d422015-04-23 18:41:29 -07001755/*static*/
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001756status_t AudioSystem::removeAudioPortCallback(const sp<AudioPortCallback>& callback) {
Andy Hung264fa4c2024-02-21 15:52:12 -08001757 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Eric Laurentb28753e2015-04-01 13:06:28 -07001758 if (aps == 0) return PERMISSION_DENIED;
Andy Hung88e71732024-02-21 16:44:27 -08001759 const auto apc = gAudioPolicyServiceHandler.getClient();
1760 if (apc == nullptr) return NO_INIT;
Eric Laurentb28753e2015-04-01 13:06:28 -07001761
Andy Hung88e71732024-02-21 16:44:27 -08001762 std::lock_guard _l(gApsCallbackMutex);
1763 const int ret = apc->removeAudioPortCallback(callback);
Eric Laurente8726fe2015-06-26 09:39:24 -07001764 if (ret == 0) {
1765 aps->setAudioPortCallbacksEnabled(false);
1766 }
1767 return (ret < 0) ? INVALID_OPERATION : NO_ERROR;
Eric Laurent296fb132015-05-01 11:38:42 -07001768}
1769
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001770status_t AudioSystem::addAudioVolumeGroupCallback(const sp<AudioVolumeGroupCallback>& callback) {
Andy Hung264fa4c2024-02-21 15:52:12 -08001771 const sp<IAudioPolicyService> aps = get_audio_policy_service();
François Gaffiecfe17322018-11-07 13:41:29 +01001772 if (aps == 0) return PERMISSION_DENIED;
Andy Hung88e71732024-02-21 16:44:27 -08001773 const auto apc = gAudioPolicyServiceHandler.getClient();
1774 if (apc == nullptr) return NO_INIT;
François Gaffiecfe17322018-11-07 13:41:29 +01001775
Andy Hung88e71732024-02-21 16:44:27 -08001776 std::lock_guard _l(gApsCallbackMutex);
1777 const int ret = apc->addAudioVolumeGroupCallback(callback);
François Gaffiecfe17322018-11-07 13:41:29 +01001778 if (ret == 1) {
1779 aps->setAudioVolumeGroupCallbacksEnabled(true);
1780 }
1781 return (ret < 0) ? INVALID_OPERATION : NO_ERROR;
1782}
1783
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001784status_t AudioSystem::removeAudioVolumeGroupCallback(const sp<AudioVolumeGroupCallback>& callback) {
Andy Hung264fa4c2024-02-21 15:52:12 -08001785 const sp<IAudioPolicyService> aps = get_audio_policy_service();
François Gaffiecfe17322018-11-07 13:41:29 +01001786 if (aps == 0) return PERMISSION_DENIED;
Andy Hung88e71732024-02-21 16:44:27 -08001787 const auto apc = gAudioPolicyServiceHandler.getClient();
1788 if (apc == nullptr) return NO_INIT;
François Gaffiecfe17322018-11-07 13:41:29 +01001789
Andy Hung88e71732024-02-21 16:44:27 -08001790 std::lock_guard _l(gApsCallbackMutex);
1791 const int ret = apc->removeAudioVolumeGroupCallback(callback);
François Gaffiecfe17322018-11-07 13:41:29 +01001792 if (ret == 0) {
1793 aps->setAudioVolumeGroupCallbacksEnabled(false);
1794 }
1795 return (ret < 0) ? INVALID_OPERATION : NO_ERROR;
1796}
1797
Eric Laurent296fb132015-05-01 11:38:42 -07001798status_t AudioSystem::addAudioDeviceCallback(
Eric Laurent09f1ed22019-04-24 17:45:17 -07001799 const wp<AudioDeviceCallback>& callback, audio_io_handle_t audioIo,
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001800 audio_port_handle_t portId) {
Eric Laurent296fb132015-05-01 11:38:42 -07001801 const sp<AudioFlingerClient> afc = getAudioFlingerClient();
1802 if (afc == 0) {
1803 return NO_INIT;
1804 }
Eric Laurent09f1ed22019-04-24 17:45:17 -07001805 status_t status = afc->addAudioDeviceCallback(callback, audioIo, portId);
Eric Laurent7c1ec5f2015-07-09 14:52:47 -07001806 if (status == NO_ERROR) {
Andy Hung264fa4c2024-02-21 15:52:12 -08001807 const sp<IAudioFlinger> af = get_audio_flinger();
Eric Laurent7c1ec5f2015-07-09 14:52:47 -07001808 if (af != 0) {
1809 af->registerClient(afc);
1810 }
1811 }
1812 return status;
Eric Laurent296fb132015-05-01 11:38:42 -07001813}
1814
1815status_t AudioSystem::removeAudioDeviceCallback(
Eric Laurent09f1ed22019-04-24 17:45:17 -07001816 const wp<AudioDeviceCallback>& callback, audio_io_handle_t audioIo,
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001817 audio_port_handle_t portId) {
Eric Laurent296fb132015-05-01 11:38:42 -07001818 const sp<AudioFlingerClient> afc = getAudioFlingerClient();
1819 if (afc == 0) {
1820 return NO_INIT;
1821 }
Eric Laurent09f1ed22019-04-24 17:45:17 -07001822 return afc->removeAudioDeviceCallback(callback, audioIo, portId);
Eric Laurent296fb132015-05-01 11:38:42 -07001823}
1824
Eric Laurent076e7c72022-05-03 18:12:28 +02001825status_t AudioSystem::addSupportedLatencyModesCallback(
1826 const sp<SupportedLatencyModesCallback>& callback) {
1827 const sp<AudioFlingerClient> afc = getAudioFlingerClient();
1828 if (afc == 0) {
1829 return NO_INIT;
1830 }
1831 return afc->addSupportedLatencyModesCallback(callback);
1832}
1833
1834status_t AudioSystem::removeSupportedLatencyModesCallback(
1835 const sp<SupportedLatencyModesCallback>& callback) {
1836 const sp<AudioFlingerClient> afc = getAudioFlingerClient();
1837 if (afc == 0) {
1838 return NO_INIT;
1839 }
1840 return afc->removeSupportedLatencyModesCallback(callback);
1841}
1842
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001843audio_port_handle_t AudioSystem::getDeviceIdForIo(audio_io_handle_t audioIo) {
Andy Hung264fa4c2024-02-21 15:52:12 -08001844 const sp<IAudioFlinger> af = get_audio_flinger();
Eric Laurent296fb132015-05-01 11:38:42 -07001845 if (af == 0) return PERMISSION_DENIED;
1846 const sp<AudioIoDescriptor> desc = getIoDescriptor(audioIo);
1847 if (desc == 0) {
1848 return AUDIO_PORT_HANDLE_NONE;
1849 }
1850 return desc->getDeviceId();
Eric Laurentb28753e2015-04-01 13:06:28 -07001851}
1852
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001853status_t AudioSystem::acquireSoundTriggerSession(audio_session_t* session,
1854 audio_io_handle_t* ioHandle,
1855 audio_devices_t* device) {
1856 if (session == nullptr || ioHandle == nullptr || device == nullptr) {
1857 return BAD_VALUE;
1858 }
Andy Hung264fa4c2024-02-21 15:52:12 -08001859 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Eric Laurentdf3dc7e2014-07-27 18:39:40 -07001860 if (aps == 0) return PERMISSION_DENIED;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001861
1862 media::SoundTriggerSession retAidl;
1863 RETURN_STATUS_IF_ERROR(
1864 statusTFromBinderStatus(aps->acquireSoundTriggerSession(&retAidl)));
1865 *session = VALUE_OR_RETURN_STATUS(aidl2legacy_int32_t_audio_session_t(retAidl.session));
1866 *ioHandle = VALUE_OR_RETURN_STATUS(aidl2legacy_int32_t_audio_io_handle_t(retAidl.ioHandle));
Mikhail Naganov21a32ec2021-07-08 14:40:12 -07001867 *device = VALUE_OR_RETURN_STATUS(
1868 aidl2legacy_AudioDeviceDescription_audio_devices_t(retAidl.device));
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001869 return OK;
Eric Laurentdf3dc7e2014-07-27 18:39:40 -07001870}
1871
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001872status_t AudioSystem::releaseSoundTriggerSession(audio_session_t session) {
Andy Hung264fa4c2024-02-21 15:52:12 -08001873 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Eric Laurentdf3dc7e2014-07-27 18:39:40 -07001874 if (aps == 0) return PERMISSION_DENIED;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001875
1876 int32_t sessionAidl = VALUE_OR_RETURN_STATUS(legacy2aidl_audio_session_t_int32_t(session));
1877 return statusTFromBinderStatus(aps->releaseSoundTriggerSession(sessionAidl));
Eric Laurentdf3dc7e2014-07-27 18:39:40 -07001878}
Eric Laurentbb6c9a02014-09-25 14:11:47 -07001879
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001880audio_mode_t AudioSystem::getPhoneState() {
Andy Hung264fa4c2024-02-21 15:52:12 -08001881 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Eric Laurentbb6c9a02014-09-25 14:11:47 -07001882 if (aps == 0) return AUDIO_MODE_INVALID;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001883
1884 auto result = [&]() -> ConversionResult<audio_mode_t> {
Mikhail Naganovddceecc2021-09-03 13:58:56 -07001885 media::audio::common::AudioMode retAidl;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001886 RETURN_IF_ERROR(statusTFromBinderStatus(aps->getPhoneState(&retAidl)));
1887 return aidl2legacy_AudioMode_audio_mode_t(retAidl);
1888 }();
1889
1890 return result.value_or(AUDIO_MODE_INVALID);
Eric Laurentbb6c9a02014-09-25 14:11:47 -07001891}
1892
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001893status_t AudioSystem::registerPolicyMixes(const Vector<AudioMix>& mixes, bool registration) {
Andy Hung264fa4c2024-02-21 15:52:12 -08001894 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Eric Laurentbaac1832014-12-01 17:52:59 -08001895 if (aps == 0) return PERMISSION_DENIED;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001896
1897 size_t mixesSize = std::min(mixes.size(), size_t{MAX_MIXES_PER_POLICY});
1898 std::vector<media::AudioMix> mixesAidl;
1899 RETURN_STATUS_IF_ERROR(
1900 convertRange(mixes.begin(), mixes.begin() + mixesSize, std::back_inserter(mixesAidl),
1901 legacy2aidl_AudioMix));
1902 return statusTFromBinderStatus(aps->registerPolicyMixes(mixesAidl, registration));
Eric Laurentbaac1832014-12-01 17:52:59 -08001903}
Eric Laurentbb6c9a02014-09-25 14:11:47 -07001904
Marvin Raminbdefaf02023-11-01 09:10:32 +01001905status_t AudioSystem::getRegisteredPolicyMixes(std::vector<AudioMix>& mixes) {
1906 if (!audio_flags::audio_mix_test_api()) {
1907 return INVALID_OPERATION;
1908 }
1909
1910 const sp<IAudioPolicyService> aps = AudioSystem::get_audio_policy_service();
1911 if (aps == nullptr) return PERMISSION_DENIED;
1912
1913 std::vector<::android::media::AudioMix> aidlMixes;
1914 Status status = aps->getRegisteredPolicyMixes(&aidlMixes);
1915
1916 for (const auto& aidlMix : aidlMixes) {
1917 AudioMix mix = VALUE_OR_RETURN_STATUS(aidl2legacy_AudioMix(aidlMix));
1918 mixes.push_back(mix);
1919 }
1920
1921 return statusTFromBinderStatus(status);
1922}
1923
Jan Sebechlebsky0af8e872023-08-11 14:45:08 +02001924status_t AudioSystem::updatePolicyMixes(
1925 const std::vector<std::pair<AudioMix, std::vector<AudioMixMatchCriterion>>>&
1926 mixesWithUpdates) {
Andy Hung264fa4c2024-02-21 15:52:12 -08001927 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Jan Sebechlebsky0af8e872023-08-11 14:45:08 +02001928 if (aps == 0) return PERMISSION_DENIED;
1929
1930 std::vector<media::AudioMixUpdate> updatesAidl;
1931 updatesAidl.reserve(mixesWithUpdates.size());
1932
1933 for (const auto& update : mixesWithUpdates) {
1934 media::AudioMixUpdate updateAidl;
1935 updateAidl.audioMix = VALUE_OR_RETURN_STATUS(legacy2aidl_AudioMix(update.first));
1936 RETURN_STATUS_IF_ERROR(convertRange(update.second.begin(), update.second.end(),
1937 std::back_inserter(updateAidl.newCriteria),
1938 legacy2aidl_AudioMixMatchCriterion));
1939 updatesAidl.emplace_back(updateAidl);
1940 }
1941
1942 return statusTFromBinderStatus(aps->updatePolicyMixes(updatesAidl));
1943}
1944
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001945status_t AudioSystem::setUidDeviceAffinities(uid_t uid, const AudioDeviceTypeAddrVector& devices) {
Andy Hung264fa4c2024-02-21 15:52:12 -08001946 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Jean-Michel Trivibda70da2018-12-19 07:30:15 -08001947 if (aps == 0) return PERMISSION_DENIED;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001948
1949 int32_t uidAidl = VALUE_OR_RETURN_STATUS(legacy2aidl_uid_t_int32_t(uid));
Mikhail Naganovf4a75362021-09-16 00:02:54 +00001950 std::vector<AudioDevice> devicesAidl = VALUE_OR_RETURN_STATUS(
1951 convertContainer<std::vector<AudioDevice>>(devices,
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001952 legacy2aidl_AudioDeviceTypeAddress));
1953 return statusTFromBinderStatus(aps->setUidDeviceAffinities(uidAidl, devicesAidl));
Jean-Michel Trivibda70da2018-12-19 07:30:15 -08001954}
1955
1956status_t AudioSystem::removeUidDeviceAffinities(uid_t uid) {
Andy Hung264fa4c2024-02-21 15:52:12 -08001957 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Jean-Michel Trivibda70da2018-12-19 07:30:15 -08001958 if (aps == 0) return PERMISSION_DENIED;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001959
1960 int32_t uidAidl = VALUE_OR_RETURN_STATUS(legacy2aidl_uid_t_int32_t(uid));
1961 return statusTFromBinderStatus(aps->removeUidDeviceAffinities(uidAidl));
Jean-Michel Trivibda70da2018-12-19 07:30:15 -08001962}
1963
Oscar Azucena90e77632019-11-27 17:12:28 -08001964status_t AudioSystem::setUserIdDeviceAffinities(int userId,
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001965 const AudioDeviceTypeAddrVector& devices) {
Andy Hung264fa4c2024-02-21 15:52:12 -08001966 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Oscar Azucena90e77632019-11-27 17:12:28 -08001967 if (aps == 0) return PERMISSION_DENIED;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001968
1969 int32_t userIdAidl = VALUE_OR_RETURN_STATUS(convertReinterpret<int32_t>(userId));
Mikhail Naganovf4a75362021-09-16 00:02:54 +00001970 std::vector<AudioDevice> devicesAidl = VALUE_OR_RETURN_STATUS(
1971 convertContainer<std::vector<AudioDevice>>(devices,
1972 legacy2aidl_AudioDeviceTypeAddress));
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001973 return statusTFromBinderStatus(
1974 aps->setUserIdDeviceAffinities(userIdAidl, devicesAidl));
Oscar Azucena90e77632019-11-27 17:12:28 -08001975}
1976
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001977status_t AudioSystem::removeUserIdDeviceAffinities(int userId) {
Andy Hung264fa4c2024-02-21 15:52:12 -08001978 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Oscar Azucena90e77632019-11-27 17:12:28 -08001979 if (aps == 0) return PERMISSION_DENIED;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001980 int32_t userIdAidl = VALUE_OR_RETURN_STATUS(convertReinterpret<int32_t>(userId));
1981 return statusTFromBinderStatus(aps->removeUserIdDeviceAffinities(userIdAidl));
Oscar Azucena90e77632019-11-27 17:12:28 -08001982}
1983
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001984status_t AudioSystem::startAudioSource(const struct audio_port_config* source,
1985 const audio_attributes_t* attributes,
1986 audio_port_handle_t* portId) {
1987 if (source == nullptr || attributes == nullptr || portId == nullptr) {
1988 return BAD_VALUE;
1989 }
Andy Hung264fa4c2024-02-21 15:52:12 -08001990 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Eric Laurent554a2772015-04-10 11:29:24 -07001991 if (aps == 0) return PERMISSION_DENIED;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001992
Atneya Nair7a9594f2022-12-18 17:26:26 -08001993 media::AudioPortConfigFw sourceAidl = VALUE_OR_RETURN_STATUS(
Mikhail Naganov87227252023-01-13 17:38:10 -08001994 legacy2aidl_audio_port_config_AudioPortConfigFw(*source));
Mikhail Naganov1c400902023-05-17 11:48:43 -07001995 media::audio::common::AudioAttributes attributesAidl = VALUE_OR_RETURN_STATUS(
1996 legacy2aidl_audio_attributes_t_AudioAttributes(*attributes));
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001997 int32_t portIdAidl;
1998 RETURN_STATUS_IF_ERROR(statusTFromBinderStatus(
1999 aps->startAudioSource(sourceAidl, attributesAidl, &portIdAidl)));
2000 *portId = VALUE_OR_RETURN_STATUS(aidl2legacy_int32_t_audio_port_handle_t(portIdAidl));
2001 return OK;
Eric Laurent554a2772015-04-10 11:29:24 -07002002}
2003
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002004status_t AudioSystem::stopAudioSource(audio_port_handle_t portId) {
Andy Hung264fa4c2024-02-21 15:52:12 -08002005 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Eric Laurent554a2772015-04-10 11:29:24 -07002006 if (aps == 0) return PERMISSION_DENIED;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002007
2008 int32_t portIdAidl = VALUE_OR_RETURN_STATUS(legacy2aidl_audio_port_handle_t_int32_t(portId));
2009 return statusTFromBinderStatus(aps->stopAudioSource(portIdAidl));
Eric Laurent554a2772015-04-10 11:29:24 -07002010}
2011
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002012status_t AudioSystem::setMasterMono(bool mono) {
Andy Hung264fa4c2024-02-21 15:52:12 -08002013 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Andy Hung2ddee192015-12-18 17:34:44 -08002014 if (aps == 0) return PERMISSION_DENIED;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002015 return statusTFromBinderStatus(aps->setMasterMono(mono));
Andy Hung2ddee192015-12-18 17:34:44 -08002016}
2017
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002018status_t AudioSystem::getMasterMono(bool* mono) {
2019 if (mono == nullptr) {
2020 return BAD_VALUE;
2021 }
Andy Hung264fa4c2024-02-21 15:52:12 -08002022 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Andy Hung2ddee192015-12-18 17:34:44 -08002023 if (aps == 0) return PERMISSION_DENIED;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002024 return statusTFromBinderStatus(aps->getMasterMono(mono));
Andy Hung2ddee192015-12-18 17:34:44 -08002025}
2026
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002027status_t AudioSystem::setMasterBalance(float balance) {
Andy Hung264fa4c2024-02-21 15:52:12 -08002028 const sp<IAudioFlinger> af = get_audio_flinger();
Richard Folke Tullberg3fae0372017-01-13 09:04:25 +01002029 if (af == 0) return PERMISSION_DENIED;
2030 return af->setMasterBalance(balance);
2031}
2032
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002033status_t AudioSystem::getMasterBalance(float* balance) {
Andy Hung264fa4c2024-02-21 15:52:12 -08002034 const sp<IAudioFlinger> af = get_audio_flinger();
Richard Folke Tullberg3fae0372017-01-13 09:04:25 +01002035 if (af == 0) return PERMISSION_DENIED;
2036 return af->getMasterBalance(balance);
2037}
2038
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002039float
2040AudioSystem::getStreamVolumeDB(audio_stream_type_t stream, int index, audio_devices_t device) {
Andy Hung264fa4c2024-02-21 15:52:12 -08002041 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Eric Laurentac9cef52017-06-09 15:46:26 -07002042 if (aps == 0) return NAN;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002043
2044 auto result = [&]() -> ConversionResult<float> {
Mikhail Naganovdbf03642021-08-25 18:15:32 -07002045 AudioStreamType streamAidl = VALUE_OR_RETURN(
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002046 legacy2aidl_audio_stream_type_t_AudioStreamType(stream));
2047 int32_t indexAidl = VALUE_OR_RETURN(convertIntegral<int32_t>(index));
Mikhail Naganovf4a75362021-09-16 00:02:54 +00002048 AudioDeviceDescription deviceAidl = VALUE_OR_RETURN(
Mikhail Naganov21a32ec2021-07-08 14:40:12 -07002049 legacy2aidl_audio_devices_t_AudioDeviceDescription(device));
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002050 float retAidl;
2051 RETURN_IF_ERROR(statusTFromBinderStatus(
2052 aps->getStreamVolumeDB(streamAidl, indexAidl, deviceAidl, &retAidl)));
2053 return retAidl;
2054 }();
2055 return result.value_or(NAN);
Eric Laurentac9cef52017-06-09 15:46:26 -07002056}
2057
Mikhail Naganovd5d9de72023-02-13 11:45:03 -08002058status_t AudioSystem::getMicrophones(std::vector<media::MicrophoneInfoFw>* microphones) {
Andy Hung264fa4c2024-02-21 15:52:12 -08002059 const sp<IAudioFlinger> af = get_audio_flinger();
jiabin46a76fa2018-01-05 10:18:21 -08002060 if (af == 0) return PERMISSION_DENIED;
2061 return af->getMicrophones(microphones);
2062}
2063
Eric Laurent42896a02019-09-27 15:40:33 -07002064status_t AudioSystem::setAudioHalPids(const std::vector<pid_t>& pids) {
Andy Hung264fa4c2024-02-21 15:52:12 -08002065 const sp<IAudioFlinger> af = get_audio_flinger();
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002066 if (af == nullptr) return PERMISSION_DENIED;
2067 return af->setAudioHalPids(pids);
Eric Laurent42896a02019-09-27 15:40:33 -07002068}
2069
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002070status_t AudioSystem::getSurroundFormats(unsigned int* numSurroundFormats,
2071 audio_format_t* surroundFormats,
Kriti Dang6537def2021-03-02 13:46:59 +01002072 bool* surroundFormatsEnabled) {
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002073 if (numSurroundFormats == nullptr || (*numSurroundFormats != 0 &&
2074 (surroundFormats == nullptr ||
2075 surroundFormatsEnabled == nullptr))) {
2076 return BAD_VALUE;
2077 }
2078
Andy Hung264fa4c2024-02-21 15:52:12 -08002079 const sp<IAudioPolicyService> aps = get_audio_policy_service();
jiabin81772902018-04-02 17:52:27 -07002080 if (aps == 0) return PERMISSION_DENIED;
Mikhail Naganov0078ee52021-09-30 23:06:20 +00002081 Int numSurroundFormatsAidl;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002082 numSurroundFormatsAidl.value =
2083 VALUE_OR_RETURN_STATUS(convertIntegral<int32_t>(*numSurroundFormats));
Mikhail Naganov57bd06f2021-08-10 16:41:54 -07002084 std::vector<AudioFormatDescription> surroundFormatsAidl;
Kriti Dang877b27e2021-02-02 12:10:40 +01002085 std::vector<bool> surroundFormatsEnabledAidl;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002086 RETURN_STATUS_IF_ERROR(statusTFromBinderStatus(
Kriti Dang6537def2021-03-02 13:46:59 +01002087 aps->getSurroundFormats(&numSurroundFormatsAidl, &surroundFormatsAidl,
2088 &surroundFormatsEnabledAidl)));
Kriti Dang877b27e2021-02-02 12:10:40 +01002089
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002090 *numSurroundFormats = VALUE_OR_RETURN_STATUS(
2091 convertIntegral<unsigned int>(numSurroundFormatsAidl.value));
2092 RETURN_STATUS_IF_ERROR(
2093 convertRange(surroundFormatsAidl.begin(), surroundFormatsAidl.end(), surroundFormats,
Mikhail Naganovb60bd1b2021-07-15 17:31:43 -07002094 aidl2legacy_AudioFormatDescription_audio_format_t));
Kriti Dang877b27e2021-02-02 12:10:40 +01002095 std::copy(surroundFormatsEnabledAidl.begin(), surroundFormatsEnabledAidl.end(),
2096 surroundFormatsEnabled);
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002097 return OK;
jiabin81772902018-04-02 17:52:27 -07002098}
2099
Kriti Dang6537def2021-03-02 13:46:59 +01002100status_t AudioSystem::getReportedSurroundFormats(unsigned int* numSurroundFormats,
2101 audio_format_t* surroundFormats) {
2102 if (numSurroundFormats == nullptr || (*numSurroundFormats != 0 && surroundFormats == nullptr)) {
2103 return BAD_VALUE;
2104 }
2105
Andy Hung264fa4c2024-02-21 15:52:12 -08002106 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Kriti Dang6537def2021-03-02 13:46:59 +01002107 if (aps == 0) return PERMISSION_DENIED;
Mikhail Naganov0078ee52021-09-30 23:06:20 +00002108 Int numSurroundFormatsAidl;
Kriti Dang6537def2021-03-02 13:46:59 +01002109 numSurroundFormatsAidl.value =
2110 VALUE_OR_RETURN_STATUS(convertIntegral<int32_t>(*numSurroundFormats));
Mikhail Naganov57bd06f2021-08-10 16:41:54 -07002111 std::vector<AudioFormatDescription> surroundFormatsAidl;
Kriti Dang6537def2021-03-02 13:46:59 +01002112 RETURN_STATUS_IF_ERROR(statusTFromBinderStatus(
2113 aps->getReportedSurroundFormats(&numSurroundFormatsAidl, &surroundFormatsAidl)));
2114
2115 *numSurroundFormats = VALUE_OR_RETURN_STATUS(
2116 convertIntegral<unsigned int>(numSurroundFormatsAidl.value));
2117 RETURN_STATUS_IF_ERROR(
2118 convertRange(surroundFormatsAidl.begin(), surroundFormatsAidl.end(), surroundFormats,
Mikhail Naganovb60bd1b2021-07-15 17:31:43 -07002119 aidl2legacy_AudioFormatDescription_audio_format_t));
Kriti Dang6537def2021-03-02 13:46:59 +01002120 return OK;
2121}
2122
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002123status_t AudioSystem::setSurroundFormatEnabled(audio_format_t audioFormat, bool enabled) {
Andy Hung264fa4c2024-02-21 15:52:12 -08002124 const sp<IAudioPolicyService> aps = get_audio_policy_service();
jiabin81772902018-04-02 17:52:27 -07002125 if (aps == 0) return PERMISSION_DENIED;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002126
Mikhail Naganov57bd06f2021-08-10 16:41:54 -07002127 AudioFormatDescription audioFormatAidl = VALUE_OR_RETURN_STATUS(
Mikhail Naganovb60bd1b2021-07-15 17:31:43 -07002128 legacy2aidl_audio_format_t_AudioFormatDescription(audioFormat));
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002129 return statusTFromBinderStatus(
2130 aps->setSurroundFormatEnabled(audioFormatAidl, enabled));
jiabin81772902018-04-02 17:52:27 -07002131}
2132
Oscar Azucena829d90d2022-01-28 17:17:56 -08002133status_t AudioSystem::setAssistantServicesUids(const std::vector<uid_t>& uids) {
Andy Hung264fa4c2024-02-21 15:52:12 -08002134 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Eric Laurentb78763e2018-10-17 10:08:02 -07002135 if (aps == 0) return PERMISSION_DENIED;
2136
Oscar Azucena829d90d2022-01-28 17:17:56 -08002137 std::vector<int32_t> uidsAidl = VALUE_OR_RETURN_STATUS(
2138 convertContainer<std::vector<int32_t>>(uids, legacy2aidl_uid_t_int32_t));
2139 return statusTFromBinderStatus(aps->setAssistantServicesUids(uidsAidl));
Ahaan Ugalef51ce002021-08-04 16:34:20 -07002140}
2141
Oscar Azucenac2cdda32022-01-31 19:10:39 -08002142status_t AudioSystem::setActiveAssistantServicesUids(const std::vector<uid_t>& activeUids) {
Andy Hung264fa4c2024-02-21 15:52:12 -08002143 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Oscar Azucenac2cdda32022-01-31 19:10:39 -08002144 if (aps == 0) return PERMISSION_DENIED;
2145
2146 std::vector<int32_t> activeUidsAidl = VALUE_OR_RETURN_STATUS(
2147 convertContainer<std::vector<int32_t>>(activeUids, legacy2aidl_uid_t_int32_t));
2148 return statusTFromBinderStatus(aps->setActiveAssistantServicesUids(activeUidsAidl));
2149}
2150
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002151status_t AudioSystem::setA11yServicesUids(const std::vector<uid_t>& uids) {
Andy Hung264fa4c2024-02-21 15:52:12 -08002152 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Eric Laurentb78763e2018-10-17 10:08:02 -07002153 if (aps == 0) return PERMISSION_DENIED;
2154
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002155 std::vector<int32_t> uidsAidl = VALUE_OR_RETURN_STATUS(
2156 convertContainer<std::vector<int32_t>>(uids, legacy2aidl_uid_t_int32_t));
2157 return statusTFromBinderStatus(aps->setA11yServicesUids(uidsAidl));
Eric Laurentb78763e2018-10-17 10:08:02 -07002158}
2159
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002160status_t AudioSystem::setCurrentImeUid(uid_t uid) {
Andy Hung264fa4c2024-02-21 15:52:12 -08002161 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Kohsuke Yatoha623a132020-03-24 20:10:26 -07002162 if (aps == 0) return PERMISSION_DENIED;
2163
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002164 int32_t uidAidl = VALUE_OR_RETURN_STATUS(legacy2aidl_uid_t_int32_t(uid));
2165 return statusTFromBinderStatus(aps->setCurrentImeUid(uidAidl));
Kohsuke Yatoha623a132020-03-24 20:10:26 -07002166}
2167
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002168bool AudioSystem::isHapticPlaybackSupported() {
Andy Hung264fa4c2024-02-21 15:52:12 -08002169 const sp<IAudioPolicyService> aps = get_audio_policy_service();
jiabin6012f912018-11-02 17:06:30 -07002170 if (aps == 0) return false;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002171
2172 auto result = [&]() -> ConversionResult<bool> {
2173 bool retVal;
2174 RETURN_IF_ERROR(
2175 statusTFromBinderStatus(aps->isHapticPlaybackSupported(&retVal)));
2176 return retVal;
2177 }();
2178 return result.value_or(false);
jiabin6012f912018-11-02 17:06:30 -07002179}
2180
Carter Hsu325a8eb2022-01-19 19:56:51 +08002181bool AudioSystem::isUltrasoundSupported() {
Andy Hung264fa4c2024-02-21 15:52:12 -08002182 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Carter Hsu325a8eb2022-01-19 19:56:51 +08002183 if (aps == 0) return false;
2184
2185 auto result = [&]() -> ConversionResult<bool> {
2186 bool retVal;
2187 RETURN_IF_ERROR(
2188 statusTFromBinderStatus(aps->isUltrasoundSupported(&retVal)));
2189 return retVal;
2190 }();
2191 return result.value_or(false);
2192}
2193
Pattydd807582021-11-04 21:01:03 +08002194status_t AudioSystem::getHwOffloadFormatsSupportedForBluetoothMedia(
2195 audio_devices_t device, std::vector<audio_format_t>* formats) {
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002196 if (formats == nullptr) {
2197 return BAD_VALUE;
2198 }
2199
Andy Hung264fa4c2024-02-21 15:52:12 -08002200 const sp<IAudioPolicyService> aps = get_audio_policy_service();
François Gaffied0ba9ed2018-11-05 11:50:42 +01002201 if (aps == 0) return PERMISSION_DENIED;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002202
Mikhail Naganov57bd06f2021-08-10 16:41:54 -07002203 std::vector<AudioFormatDescription> formatsAidl;
Pattydd807582021-11-04 21:01:03 +08002204 AudioDeviceDescription deviceAidl = VALUE_OR_RETURN_STATUS(
2205 legacy2aidl_audio_devices_t_AudioDeviceDescription(device));
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002206 RETURN_STATUS_IF_ERROR(statusTFromBinderStatus(
Pattydd807582021-11-04 21:01:03 +08002207 aps->getHwOffloadFormatsSupportedForBluetoothMedia(deviceAidl, &formatsAidl)));
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002208 *formats = VALUE_OR_RETURN_STATUS(
Mikhail Naganovb60bd1b2021-07-15 17:31:43 -07002209 convertContainer<std::vector<audio_format_t>>(
2210 formatsAidl,
2211 aidl2legacy_AudioFormatDescription_audio_format_t));
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002212 return OK;
François Gaffied0ba9ed2018-11-05 11:50:42 +01002213}
2214
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002215status_t AudioSystem::listAudioProductStrategies(AudioProductStrategyVector& strategies) {
Andy Hung264fa4c2024-02-21 15:52:12 -08002216 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Arun Mirpuri11029ad2018-12-19 20:45:19 -08002217 if (aps == 0) return PERMISSION_DENIED;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002218
2219 std::vector<media::AudioProductStrategy> strategiesAidl;
2220 RETURN_STATUS_IF_ERROR(statusTFromBinderStatus(
2221 aps->listAudioProductStrategies(&strategiesAidl)));
2222 strategies = VALUE_OR_RETURN_STATUS(
2223 convertContainer<AudioProductStrategyVector>(strategiesAidl,
2224 aidl2legacy_AudioProductStrategy));
2225 return OK;
François Gaffied0ba9ed2018-11-05 11:50:42 +01002226}
2227
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002228audio_attributes_t AudioSystem::streamTypeToAttributes(audio_stream_type_t stream) {
François Gaffied0ba9ed2018-11-05 11:50:42 +01002229 AudioProductStrategyVector strategies;
2230 listAudioProductStrategies(strategies);
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002231 for (const auto& strategy : strategies) {
François Gaffie1e2b56f2022-04-01 14:34:29 +02002232 auto attrVect = strategy.getVolumeGroupAttributes();
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002233 auto iter = std::find_if(begin(attrVect), end(attrVect), [&stream](const auto& attributes) {
2234 return attributes.getStreamType() == stream;
2235 });
François Gaffied0ba9ed2018-11-05 11:50:42 +01002236 if (iter != end(attrVect)) {
2237 return iter->getAttributes();
2238 }
2239 }
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002240 ALOGE("invalid stream type %s when converting to attributes", toString(stream).c_str());
François Gaffied0ba9ed2018-11-05 11:50:42 +01002241 return AUDIO_ATTRIBUTES_INITIALIZER;
2242}
2243
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002244audio_stream_type_t AudioSystem::attributesToStreamType(const audio_attributes_t& attr) {
François Gaffie4b2018b2018-11-07 11:18:59 +01002245 product_strategy_t psId;
François Gaffie1e2b56f2022-04-01 14:34:29 +02002246 status_t ret = AudioSystem::getProductStrategyFromAudioAttributes(attr, psId);
François Gaffie4b2018b2018-11-07 11:18:59 +01002247 if (ret != NO_ERROR) {
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002248 ALOGE("no strategy found for attributes %s", toString(attr).c_str());
François Gaffie4b2018b2018-11-07 11:18:59 +01002249 return AUDIO_STREAM_MUSIC;
2250 }
François Gaffied0ba9ed2018-11-05 11:50:42 +01002251 AudioProductStrategyVector strategies;
2252 listAudioProductStrategies(strategies);
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002253 for (const auto& strategy : strategies) {
François Gaffie4b2018b2018-11-07 11:18:59 +01002254 if (strategy.getId() == psId) {
François Gaffie1e2b56f2022-04-01 14:34:29 +02002255 auto attrVect = strategy.getVolumeGroupAttributes();
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002256 auto iter = std::find_if(begin(attrVect), end(attrVect), [&attr](const auto& refAttr) {
François Gaffie8d7fd5a2023-01-17 17:28:09 +01002257 return refAttr.matchesScore(attr) > 0;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002258 });
François Gaffied0ba9ed2018-11-05 11:50:42 +01002259 if (iter != end(attrVect)) {
2260 return iter->getStreamType();
2261 }
2262 }
2263 }
Jean-Michel Trivied678652019-12-19 13:39:30 -08002264 switch (attr.usage) {
2265 case AUDIO_USAGE_VIRTUAL_SOURCE:
2266 // virtual source is not expected to have an associated product strategy
2267 break;
2268 default:
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002269 ALOGE("invalid attributes %s when converting to stream", toString(attr).c_str());
Jean-Michel Trivied678652019-12-19 13:39:30 -08002270 break;
2271 }
François Gaffied0ba9ed2018-11-05 11:50:42 +01002272 return AUDIO_STREAM_MUSIC;
2273}
2274
François Gaffie1e2b56f2022-04-01 14:34:29 +02002275status_t AudioSystem::getProductStrategyFromAudioAttributes(const audio_attributes_t& aa,
Francois Gaffie11b65922020-09-24 16:59:08 +02002276 product_strategy_t& productStrategy,
2277 bool fallbackOnDefault) {
Andy Hung264fa4c2024-02-21 15:52:12 -08002278 const sp<IAudioPolicyService> aps = get_audio_policy_service();
François Gaffie4b2018b2018-11-07 11:18:59 +01002279 if (aps == 0) return PERMISSION_DENIED;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002280
Mikhail Naganov1c400902023-05-17 11:48:43 -07002281 media::audio::common::AudioAttributes aaAidl = VALUE_OR_RETURN_STATUS(
2282 legacy2aidl_audio_attributes_t_AudioAttributes(aa));
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002283 int32_t productStrategyAidl;
2284
2285 RETURN_STATUS_IF_ERROR(statusTFromBinderStatus(
Francois Gaffie11b65922020-09-24 16:59:08 +02002286 aps->getProductStrategyFromAudioAttributes(aaAidl, fallbackOnDefault,
2287 &productStrategyAidl)));
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002288 productStrategy = VALUE_OR_RETURN_STATUS(
2289 aidl2legacy_int32_t_product_strategy_t(productStrategyAidl));
2290 return OK;
François Gaffie4b2018b2018-11-07 11:18:59 +01002291}
2292
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002293status_t AudioSystem::listAudioVolumeGroups(AudioVolumeGroupVector& groups) {
Andy Hung264fa4c2024-02-21 15:52:12 -08002294 const sp<IAudioPolicyService> aps = get_audio_policy_service();
François Gaffie4b2018b2018-11-07 11:18:59 +01002295 if (aps == 0) return PERMISSION_DENIED;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002296
2297 std::vector<media::AudioVolumeGroup> groupsAidl;
2298 RETURN_STATUS_IF_ERROR(
2299 statusTFromBinderStatus(aps->listAudioVolumeGroups(&groupsAidl)));
2300 groups = VALUE_OR_RETURN_STATUS(
2301 convertContainer<AudioVolumeGroupVector>(groupsAidl, aidl2legacy_AudioVolumeGroup));
2302 return OK;
François Gaffie4b2018b2018-11-07 11:18:59 +01002303}
2304
François Gaffie1e2b56f2022-04-01 14:34:29 +02002305status_t AudioSystem::getVolumeGroupFromAudioAttributes(const audio_attributes_t &aa,
Francois Gaffie11b65922020-09-24 16:59:08 +02002306 volume_group_t& volumeGroup,
2307 bool fallbackOnDefault) {
Andy Hung264fa4c2024-02-21 15:52:12 -08002308 const sp<IAudioPolicyService> aps = get_audio_policy_service();
François Gaffie4b2018b2018-11-07 11:18:59 +01002309 if (aps == 0) return PERMISSION_DENIED;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002310
Mikhail Naganov1c400902023-05-17 11:48:43 -07002311 media::audio::common::AudioAttributes aaAidl = VALUE_OR_RETURN_STATUS(
2312 legacy2aidl_audio_attributes_t_AudioAttributes(aa));
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002313 int32_t volumeGroupAidl;
2314 RETURN_STATUS_IF_ERROR(statusTFromBinderStatus(
Francois Gaffie11b65922020-09-24 16:59:08 +02002315 aps->getVolumeGroupFromAudioAttributes(aaAidl, fallbackOnDefault, &volumeGroupAidl)));
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002316 volumeGroup = VALUE_OR_RETURN_STATUS(aidl2legacy_int32_t_volume_group_t(volumeGroupAidl));
2317 return OK;
Arun Mirpuri11029ad2018-12-19 20:45:19 -08002318}
Eric Laurentb78763e2018-10-17 10:08:02 -07002319
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002320status_t AudioSystem::setRttEnabled(bool enabled) {
Andy Hung264fa4c2024-02-21 15:52:12 -08002321 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Eric Laurent6ede98f2019-06-11 14:50:30 -07002322 if (aps == 0) return PERMISSION_DENIED;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002323 return statusTFromBinderStatus(aps->setRttEnabled(enabled));
Eric Laurent6ede98f2019-06-11 14:50:30 -07002324}
2325
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002326bool AudioSystem::isCallScreenModeSupported() {
Andy Hung264fa4c2024-02-21 15:52:12 -08002327 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Eric Laurent8340e672019-11-06 11:01:08 -08002328 if (aps == 0) return false;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002329
2330 auto result = [&]() -> ConversionResult<bool> {
2331 bool retAidl;
2332 RETURN_IF_ERROR(
2333 statusTFromBinderStatus(aps->isCallScreenModeSupported(&retAidl)));
2334 return retAidl;
2335 }();
2336 return result.value_or(false);
Eric Laurent8340e672019-11-06 11:01:08 -08002337}
2338
jiabin0a488932020-08-07 17:32:40 -07002339status_t AudioSystem::setDevicesRoleForStrategy(product_strategy_t strategy,
2340 device_role_t role,
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002341 const AudioDeviceTypeAddrVector& devices) {
Andy Hung264fa4c2024-02-21 15:52:12 -08002342 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Jean-Michel Trivi30857152019-11-01 11:04:15 -07002343 if (aps == 0) {
2344 return PERMISSION_DENIED;
2345 }
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002346
2347 int32_t strategyAidl = VALUE_OR_RETURN_STATUS(legacy2aidl_product_strategy_t_int32_t(strategy));
2348 media::DeviceRole roleAidl = VALUE_OR_RETURN_STATUS(legacy2aidl_device_role_t_DeviceRole(role));
Mikhail Naganovf4a75362021-09-16 00:02:54 +00002349 std::vector<AudioDevice> devicesAidl = VALUE_OR_RETURN_STATUS(
2350 convertContainer<std::vector<AudioDevice>>(devices,
2351 legacy2aidl_AudioDeviceTypeAddress));
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002352 return statusTFromBinderStatus(
2353 aps->setDevicesRoleForStrategy(strategyAidl, roleAidl, devicesAidl));
Jean-Michel Trivi30857152019-11-01 11:04:15 -07002354}
2355
Paul Wang5d7cdb52022-11-22 09:45:06 +00002356status_t AudioSystem::removeDevicesRoleForStrategy(product_strategy_t strategy,
2357 device_role_t role,
2358 const AudioDeviceTypeAddrVector& devices) {
Andy Hung264fa4c2024-02-21 15:52:12 -08002359 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Paul Wang5d7cdb52022-11-22 09:45:06 +00002360 if (aps == 0) {
2361 return PERMISSION_DENIED;
2362 }
2363
2364 int32_t strategyAidl = VALUE_OR_RETURN_STATUS(legacy2aidl_product_strategy_t_int32_t(strategy));
2365 media::DeviceRole roleAidl = VALUE_OR_RETURN_STATUS(legacy2aidl_device_role_t_DeviceRole(role));
2366 std::vector<AudioDevice> devicesAidl = VALUE_OR_RETURN_STATUS(
2367 convertContainer<std::vector<AudioDevice>>(devices,
2368 legacy2aidl_AudioDeviceTypeAddress));
2369 return statusTFromBinderStatus(
2370 aps->removeDevicesRoleForStrategy(strategyAidl, roleAidl, devicesAidl));
2371}
2372
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002373status_t
Paul Wang5d7cdb52022-11-22 09:45:06 +00002374AudioSystem::clearDevicesRoleForStrategy(product_strategy_t strategy, device_role_t role) {
Andy Hung264fa4c2024-02-21 15:52:12 -08002375 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Jean-Michel Trivi30857152019-11-01 11:04:15 -07002376 if (aps == 0) {
2377 return PERMISSION_DENIED;
2378 }
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002379 int32_t strategyAidl = VALUE_OR_RETURN_STATUS(legacy2aidl_product_strategy_t_int32_t(strategy));
2380 media::DeviceRole roleAidl = VALUE_OR_RETURN_STATUS(legacy2aidl_device_role_t_DeviceRole(role));
2381 return statusTFromBinderStatus(
Paul Wang5d7cdb52022-11-22 09:45:06 +00002382 aps->clearDevicesRoleForStrategy(strategyAidl, roleAidl));
Jean-Michel Trivi30857152019-11-01 11:04:15 -07002383}
2384
jiabin0a488932020-08-07 17:32:40 -07002385status_t AudioSystem::getDevicesForRoleAndStrategy(product_strategy_t strategy,
2386 device_role_t role,
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002387 AudioDeviceTypeAddrVector& devices) {
Andy Hung264fa4c2024-02-21 15:52:12 -08002388 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Jean-Michel Trivi30857152019-11-01 11:04:15 -07002389 if (aps == 0) {
2390 return PERMISSION_DENIED;
2391 }
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002392 int32_t strategyAidl = VALUE_OR_RETURN_STATUS(legacy2aidl_product_strategy_t_int32_t(strategy));
2393 media::DeviceRole roleAidl = VALUE_OR_RETURN_STATUS(legacy2aidl_device_role_t_DeviceRole(role));
Mikhail Naganovf4a75362021-09-16 00:02:54 +00002394 std::vector<AudioDevice> devicesAidl;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002395 RETURN_STATUS_IF_ERROR(statusTFromBinderStatus(
2396 aps->getDevicesForRoleAndStrategy(strategyAidl, roleAidl, &devicesAidl)));
2397 devices = VALUE_OR_RETURN_STATUS(
2398 convertContainer<AudioDeviceTypeAddrVector>(devicesAidl,
2399 aidl2legacy_AudioDeviceTypeAddress));
2400 return OK;
Jean-Michel Trivi30857152019-11-01 11:04:15 -07002401}
2402
Jiabin Huang3b98d322020-09-03 17:54:16 +00002403status_t AudioSystem::setDevicesRoleForCapturePreset(audio_source_t audioSource,
2404 device_role_t role,
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002405 const AudioDeviceTypeAddrVector& devices) {
Andy Hung264fa4c2024-02-21 15:52:12 -08002406 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Jiabin Huang3b98d322020-09-03 17:54:16 +00002407 if (aps == 0) {
2408 return PERMISSION_DENIED;
2409 }
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002410
Mikhail Naganovddceecc2021-09-03 13:58:56 -07002411 AudioSource audioSourceAidl = VALUE_OR_RETURN_STATUS(
2412 legacy2aidl_audio_source_t_AudioSource(audioSource));
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002413 media::DeviceRole roleAidl = VALUE_OR_RETURN_STATUS(legacy2aidl_device_role_t_DeviceRole(role));
Mikhail Naganovf4a75362021-09-16 00:02:54 +00002414 std::vector<AudioDevice> devicesAidl = VALUE_OR_RETURN_STATUS(
2415 convertContainer<std::vector<AudioDevice>>(devices,
2416 legacy2aidl_AudioDeviceTypeAddress));
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002417 return statusTFromBinderStatus(
2418 aps->setDevicesRoleForCapturePreset(audioSourceAidl, roleAidl, devicesAidl));
Jiabin Huang3b98d322020-09-03 17:54:16 +00002419}
2420
2421status_t AudioSystem::addDevicesRoleForCapturePreset(audio_source_t audioSource,
2422 device_role_t role,
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002423 const AudioDeviceTypeAddrVector& devices) {
Andy Hung264fa4c2024-02-21 15:52:12 -08002424 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Jiabin Huang3b98d322020-09-03 17:54:16 +00002425 if (aps == 0) {
2426 return PERMISSION_DENIED;
2427 }
Mikhail Naganovddceecc2021-09-03 13:58:56 -07002428 AudioSource audioSourceAidl = VALUE_OR_RETURN_STATUS(
2429 legacy2aidl_audio_source_t_AudioSource(audioSource));
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002430 media::DeviceRole roleAidl = VALUE_OR_RETURN_STATUS(legacy2aidl_device_role_t_DeviceRole(role));
Mikhail Naganovf4a75362021-09-16 00:02:54 +00002431 std::vector<AudioDevice> devicesAidl = VALUE_OR_RETURN_STATUS(
2432 convertContainer<std::vector<AudioDevice>>(devices,
2433 legacy2aidl_AudioDeviceTypeAddress));
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002434 return statusTFromBinderStatus(
2435 aps->addDevicesRoleForCapturePreset(audioSourceAidl, roleAidl, devicesAidl));
Jiabin Huang3b98d322020-09-03 17:54:16 +00002436}
2437
2438status_t AudioSystem::removeDevicesRoleForCapturePreset(
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002439 audio_source_t audioSource, device_role_t role, const AudioDeviceTypeAddrVector& devices) {
Andy Hung264fa4c2024-02-21 15:52:12 -08002440 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Jiabin Huang3b98d322020-09-03 17:54:16 +00002441 if (aps == 0) {
2442 return PERMISSION_DENIED;
2443 }
Mikhail Naganovddceecc2021-09-03 13:58:56 -07002444 AudioSource audioSourceAidl = VALUE_OR_RETURN_STATUS(
2445 legacy2aidl_audio_source_t_AudioSource(audioSource));
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002446 media::DeviceRole roleAidl = VALUE_OR_RETURN_STATUS(legacy2aidl_device_role_t_DeviceRole(role));
Mikhail Naganovf4a75362021-09-16 00:02:54 +00002447 std::vector<AudioDevice> devicesAidl = VALUE_OR_RETURN_STATUS(
2448 convertContainer<std::vector<AudioDevice>>(devices,
2449 legacy2aidl_AudioDeviceTypeAddress));
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002450 return statusTFromBinderStatus(
2451 aps->removeDevicesRoleForCapturePreset(audioSourceAidl, roleAidl, devicesAidl));
Jiabin Huang3b98d322020-09-03 17:54:16 +00002452}
2453
2454status_t AudioSystem::clearDevicesRoleForCapturePreset(audio_source_t audioSource,
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002455 device_role_t role) {
Andy Hung264fa4c2024-02-21 15:52:12 -08002456 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Jiabin Huang3b98d322020-09-03 17:54:16 +00002457 if (aps == 0) {
2458 return PERMISSION_DENIED;
2459 }
Mikhail Naganovddceecc2021-09-03 13:58:56 -07002460 AudioSource audioSourceAidl = VALUE_OR_RETURN_STATUS(
2461 legacy2aidl_audio_source_t_AudioSource(audioSource));
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002462 media::DeviceRole roleAidl = VALUE_OR_RETURN_STATUS(legacy2aidl_device_role_t_DeviceRole(role));
2463 return statusTFromBinderStatus(
2464 aps->clearDevicesRoleForCapturePreset(audioSourceAidl, roleAidl));
Jiabin Huang3b98d322020-09-03 17:54:16 +00002465}
2466
2467status_t AudioSystem::getDevicesForRoleAndCapturePreset(audio_source_t audioSource,
2468 device_role_t role,
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002469 AudioDeviceTypeAddrVector& devices) {
Andy Hung264fa4c2024-02-21 15:52:12 -08002470 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Jiabin Huang3b98d322020-09-03 17:54:16 +00002471 if (aps == 0) {
2472 return PERMISSION_DENIED;
2473 }
Mikhail Naganovddceecc2021-09-03 13:58:56 -07002474 AudioSource audioSourceAidl = VALUE_OR_RETURN_STATUS(
2475 legacy2aidl_audio_source_t_AudioSource(audioSource));
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002476 media::DeviceRole roleAidl = VALUE_OR_RETURN_STATUS(legacy2aidl_device_role_t_DeviceRole(role));
Mikhail Naganovf4a75362021-09-16 00:02:54 +00002477 std::vector<AudioDevice> devicesAidl;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002478 RETURN_STATUS_IF_ERROR(statusTFromBinderStatus(
2479 aps->getDevicesForRoleAndCapturePreset(audioSourceAidl, roleAidl, &devicesAidl)));
2480 devices = VALUE_OR_RETURN_STATUS(
2481 convertContainer<AudioDeviceTypeAddrVector>(devicesAidl,
2482 aidl2legacy_AudioDeviceTypeAddress));
2483 return OK;
Jiabin Huang3b98d322020-09-03 17:54:16 +00002484}
2485
Eric Laurent81dd0f52021-07-05 11:54:40 +02002486status_t AudioSystem::getSpatializer(const sp<media::INativeSpatializerCallback>& callback,
2487 sp<media::ISpatializer>* spatializer) {
Andy Hung264fa4c2024-02-21 15:52:12 -08002488 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Eric Laurent81dd0f52021-07-05 11:54:40 +02002489 if (spatializer == nullptr) {
2490 return BAD_VALUE;
2491 }
2492 if (aps == 0) {
2493 return PERMISSION_DENIED;
2494 }
2495 media::GetSpatializerResponse response;
2496 RETURN_STATUS_IF_ERROR(statusTFromBinderStatus(
2497 aps->getSpatializer(callback, &response)));
2498
2499 *spatializer = response.spatializer;
2500 return OK;
2501}
2502
2503status_t AudioSystem::canBeSpatialized(const audio_attributes_t *attr,
2504 const audio_config_t *config,
2505 const AudioDeviceTypeAddrVector &devices,
2506 bool *canBeSpatialized) {
Andy Hung264fa4c2024-02-21 15:52:12 -08002507 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Ram Mohan43297012022-08-30 16:11:49 +05302508 if (canBeSpatialized == nullptr) {
2509 return BAD_VALUE;
2510 }
Eric Laurent81dd0f52021-07-05 11:54:40 +02002511 if (aps == 0) {
2512 return PERMISSION_DENIED;
2513 }
2514 audio_attributes_t attributes = attr != nullptr ? *attr : AUDIO_ATTRIBUTES_INITIALIZER;
2515 audio_config_t configuration = config != nullptr ? *config : AUDIO_CONFIG_INITIALIZER;
2516
Mikhail Naganov1c400902023-05-17 11:48:43 -07002517 std::optional<media::audio::common::AudioAttributes> attrAidl = VALUE_OR_RETURN_STATUS(
2518 legacy2aidl_audio_attributes_t_AudioAttributes(attributes));
Mikhail Naganovdbf03642021-08-25 18:15:32 -07002519 std::optional<AudioConfig> configAidl = VALUE_OR_RETURN_STATUS(
Eric Laurent81dd0f52021-07-05 11:54:40 +02002520 legacy2aidl_audio_config_t_AudioConfig(configuration, false /*isInput*/));
Mikhail Naganovf4a75362021-09-16 00:02:54 +00002521 std::vector<AudioDevice> devicesAidl = VALUE_OR_RETURN_STATUS(
2522 convertContainer<std::vector<AudioDevice>>(devices,
2523 legacy2aidl_AudioDeviceTypeAddress));
Eric Laurent81dd0f52021-07-05 11:54:40 +02002524 RETURN_STATUS_IF_ERROR(statusTFromBinderStatus(
2525 aps->canBeSpatialized(attrAidl, configAidl, devicesAidl, canBeSpatialized)));
2526 return OK;
2527}
2528
Vlad Popae3fd1c22022-11-07 21:03:18 +01002529status_t AudioSystem::getSoundDoseInterface(const sp<media::ISoundDoseCallback>& callback,
2530 sp<media::ISoundDose>* soundDose) {
Andy Hung264fa4c2024-02-21 15:52:12 -08002531 const sp<IAudioFlinger> af = get_audio_flinger();
Vlad Popa63f047e2022-11-05 14:09:19 +01002532 if (af == nullptr) {
2533 return PERMISSION_DENIED;
2534 }
Vlad Popae3fd1c22022-11-07 21:03:18 +01002535 if (soundDose == nullptr) {
2536 return BAD_VALUE;
2537 }
Vlad Popa63f047e2022-11-05 14:09:19 +01002538
Vlad Popae3fd1c22022-11-07 21:03:18 +01002539 RETURN_STATUS_IF_ERROR(af->getSoundDoseInterface(callback, soundDose));
2540 return OK;
Vlad Popa63f047e2022-11-05 14:09:19 +01002541}
2542
jiabin2b9d5a12021-12-10 01:06:29 +00002543status_t AudioSystem::getDirectPlaybackSupport(const audio_attributes_t *attr,
2544 const audio_config_t *config,
2545 audio_direct_mode_t* directMode) {
2546 if (attr == nullptr || config == nullptr || directMode == nullptr) {
2547 return BAD_VALUE;
2548 }
2549
Andy Hung264fa4c2024-02-21 15:52:12 -08002550 const sp<IAudioPolicyService> aps = get_audio_policy_service();
jiabin2b9d5a12021-12-10 01:06:29 +00002551 if (aps == 0) {
2552 return PERMISSION_DENIED;
2553 }
2554
Mikhail Naganov1c400902023-05-17 11:48:43 -07002555 media::audio::common::AudioAttributes attrAidl = VALUE_OR_RETURN_STATUS(
2556 legacy2aidl_audio_attributes_t_AudioAttributes(*attr));
jiabin2b9d5a12021-12-10 01:06:29 +00002557 AudioConfig configAidl = VALUE_OR_RETURN_STATUS(
2558 legacy2aidl_audio_config_t_AudioConfig(*config, false /*isInput*/));
2559
2560 media::AudioDirectMode retAidl;
2561 RETURN_STATUS_IF_ERROR(statusTFromBinderStatus(
2562 aps->getDirectPlaybackSupport(attrAidl, configAidl, &retAidl)));
2563 *directMode = VALUE_OR_RETURN_STATUS(aidl2legacy_int32_t_audio_direct_mode_t_mask(
2564 static_cast<int32_t>(retAidl)));
2565 return NO_ERROR;
2566}
2567
Dorin Drimusf2196d82022-01-03 12:11:18 +01002568status_t AudioSystem::getDirectProfilesForAttributes(const audio_attributes_t* attr,
2569 std::vector<audio_profile>* audioProfiles) {
Mikhail Naganovefc504b2022-06-22 03:24:59 +00002570 if (attr == nullptr || audioProfiles == nullptr) {
Dorin Drimusf2196d82022-01-03 12:11:18 +01002571 return BAD_VALUE;
2572 }
2573
Andy Hung264fa4c2024-02-21 15:52:12 -08002574 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Dorin Drimusf2196d82022-01-03 12:11:18 +01002575 if (aps == 0) {
2576 return PERMISSION_DENIED;
2577 }
2578
Mikhail Naganov1c400902023-05-17 11:48:43 -07002579 media::audio::common::AudioAttributes attrAidl = VALUE_OR_RETURN_STATUS(
2580 legacy2aidl_audio_attributes_t_AudioAttributes(*attr));
Dorin Drimusf2196d82022-01-03 12:11:18 +01002581
2582 std::vector<media::audio::common::AudioProfile> audioProfilesAidl;
2583 RETURN_STATUS_IF_ERROR(statusTFromBinderStatus(
2584 aps->getDirectProfilesForAttributes(attrAidl, &audioProfilesAidl)));
2585 *audioProfiles = VALUE_OR_RETURN_STATUS(convertContainer<std::vector<audio_profile>>(
2586 audioProfilesAidl, aidl2legacy_AudioProfile_audio_profile, false /*isInput*/));
2587
2588 return NO_ERROR;
2589}
Eric Laurent81dd0f52021-07-05 11:54:40 +02002590
Eric Laurent076e7c72022-05-03 18:12:28 +02002591status_t AudioSystem::setRequestedLatencyMode(
2592 audio_io_handle_t output, audio_latency_mode_t mode) {
Andy Hung264fa4c2024-02-21 15:52:12 -08002593 const sp<IAudioFlinger> af = get_audio_flinger();
Eric Laurent076e7c72022-05-03 18:12:28 +02002594 if (af == nullptr) {
2595 return PERMISSION_DENIED;
2596 }
2597 return af->setRequestedLatencyMode(output, mode);
2598}
2599
2600status_t AudioSystem::getSupportedLatencyModes(audio_io_handle_t output,
2601 std::vector<audio_latency_mode_t>* modes) {
Andy Hung264fa4c2024-02-21 15:52:12 -08002602 const sp<IAudioFlinger> af = get_audio_flinger();
Eric Laurent076e7c72022-05-03 18:12:28 +02002603 if (af == nullptr) {
2604 return PERMISSION_DENIED;
2605 }
2606 return af->getSupportedLatencyModes(output, modes);
2607}
2608
Eric Laurent50d72582022-12-20 20:20:23 +01002609status_t AudioSystem::setBluetoothVariableLatencyEnabled(bool enabled) {
Andy Hung264fa4c2024-02-21 15:52:12 -08002610 const sp<IAudioFlinger> af = get_audio_flinger();
Eric Laurent52057642022-12-16 11:45:07 +01002611 if (af == nullptr) {
2612 return PERMISSION_DENIED;
2613 }
Eric Laurent50d72582022-12-20 20:20:23 +01002614 return af->setBluetoothVariableLatencyEnabled(enabled);
Eric Laurent52057642022-12-16 11:45:07 +01002615}
2616
Eric Laurent50d72582022-12-20 20:20:23 +01002617status_t AudioSystem::isBluetoothVariableLatencyEnabled(
2618 bool *enabled) {
Andy Hung264fa4c2024-02-21 15:52:12 -08002619 const sp<IAudioFlinger> af = get_audio_flinger();
Eric Laurent50d72582022-12-20 20:20:23 +01002620 if (af == nullptr) {
2621 return PERMISSION_DENIED;
2622 }
2623 return af->isBluetoothVariableLatencyEnabled(enabled);
2624}
2625
2626status_t AudioSystem::supportsBluetoothVariableLatency(
Eric Laurent52057642022-12-16 11:45:07 +01002627 bool *support) {
Andy Hung264fa4c2024-02-21 15:52:12 -08002628 const sp<IAudioFlinger> af = get_audio_flinger();
Eric Laurent52057642022-12-16 11:45:07 +01002629 if (af == nullptr) {
2630 return PERMISSION_DENIED;
2631 }
Eric Laurent50d72582022-12-20 20:20:23 +01002632 return af->supportsBluetoothVariableLatency(support);
Eric Laurent52057642022-12-16 11:45:07 +01002633}
2634
Mikhail Naganovffd97712023-05-03 17:45:36 -07002635status_t AudioSystem::getAudioPolicyConfig(media::AudioPolicyConfig *config) {
Andy Hung264fa4c2024-02-21 15:52:12 -08002636 const sp<IAudioFlinger> af = get_audio_flinger();
Mikhail Naganovffd97712023-05-03 17:45:36 -07002637 if (af == nullptr) {
2638 return PERMISSION_DENIED;
2639 }
2640 return af->getAudioPolicyConfig(config);
2641}
2642
Ytai Ben-Tsvi5120eda2020-03-26 09:41:15 -07002643class CaptureStateListenerImpl : public media::BnCaptureStateListener,
2644 public IBinder::DeathRecipient {
2645public:
Ytai Ben-Tsvid9f82832020-08-27 09:31:10 -07002646 CaptureStateListenerImpl(
2647 const sp<IAudioPolicyService>& aps,
2648 const sp<AudioSystem::CaptureStateListener>& listener)
Ytai Ben-Tsvia46b6d32020-08-31 13:29:11 -07002649 : mAps(aps), mListener(listener) {}
2650
2651 void init() {
Ytai Ben-Tsvid9f82832020-08-27 09:31:10 -07002652 bool active;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002653 status_t status = statusTFromBinderStatus(
2654 mAps->registerSoundTriggerCaptureStateListener(this, &active));
Ytai Ben-Tsvid9f82832020-08-27 09:31:10 -07002655 if (status != NO_ERROR) {
2656 mListener->onServiceDied();
2657 return;
2658 }
2659 mListener->onStateChanged(active);
Ytai Ben-Tsvia46b6d32020-08-31 13:29:11 -07002660 IInterface::asBinder(mAps)->linkToDeath(this);
Ytai Ben-Tsvid9f82832020-08-27 09:31:10 -07002661 }
2662
Ytai Ben-Tsvi5120eda2020-03-26 09:41:15 -07002663 binder::Status setCaptureState(bool active) override {
Andy Hungacd955a2024-02-16 17:14:36 -08002664 std::lock_guard _l(AudioSystem::gSoundTriggerMutex);
Ytai Ben-Tsvid9f82832020-08-27 09:31:10 -07002665 mListener->onStateChanged(active);
Ytai Ben-Tsvi5120eda2020-03-26 09:41:15 -07002666 return binder::Status::ok();
2667 }
2668
2669 void binderDied(const wp<IBinder>&) override {
Andy Hungacd955a2024-02-16 17:14:36 -08002670 std::lock_guard _l(AudioSystem::gSoundTriggerMutex);
Ytai Ben-Tsvid9f82832020-08-27 09:31:10 -07002671 mListener->onServiceDied();
Andy Hungacd955a2024-02-16 17:14:36 -08002672 AudioSystem::gSoundTriggerCaptureStateListener = nullptr;
Ytai Ben-Tsvi5120eda2020-03-26 09:41:15 -07002673 }
Ytai Ben-Tsvid9f82832020-08-27 09:31:10 -07002674
2675private:
2676 // Need this in order to keep the death receipent alive.
2677 sp<IAudioPolicyService> mAps;
2678 sp<AudioSystem::CaptureStateListener> mListener;
Ytai Ben-Tsvi5120eda2020-03-26 09:41:15 -07002679};
2680
2681status_t AudioSystem::registerSoundTriggerCaptureStateListener(
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002682 const sp<CaptureStateListener>& listener) {
Ytai Ben-Tsvid9f82832020-08-27 09:31:10 -07002683 LOG_ALWAYS_FATAL_IF(listener == nullptr);
2684
Andy Hung264fa4c2024-02-21 15:52:12 -08002685 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Ytai Ben-Tsvi5120eda2020-03-26 09:41:15 -07002686 if (aps == 0) {
2687 return PERMISSION_DENIED;
2688 }
2689
Andy Hungacd955a2024-02-16 17:14:36 -08002690 std::lock_guard _l(AudioSystem::gSoundTriggerMutex);
Ytai Ben-Tsvid9f82832020-08-27 09:31:10 -07002691 gSoundTriggerCaptureStateListener = new CaptureStateListenerImpl(aps, listener);
Ytai Ben-Tsvia46b6d32020-08-31 13:29:11 -07002692 gSoundTriggerCaptureStateListener->init();
Ytai Ben-Tsvi5120eda2020-03-26 09:41:15 -07002693
Ytai Ben-Tsvi5120eda2020-03-26 09:41:15 -07002694 return NO_ERROR;
2695}
2696
jiabin1319f5a2021-03-30 22:21:24 +00002697status_t AudioSystem::setVibratorInfos(
2698 const std::vector<media::AudioVibratorInfo>& vibratorInfos) {
Andy Hung264fa4c2024-02-21 15:52:12 -08002699 const sp<IAudioFlinger> af = get_audio_flinger();
jiabin1319f5a2021-03-30 22:21:24 +00002700 if (af == nullptr) {
2701 return PERMISSION_DENIED;
2702 }
2703 return af->setVibratorInfos(vibratorInfos);
2704}
2705
Jiabin Huangebe64102021-09-07 20:01:07 +00002706status_t AudioSystem::getMmapPolicyInfo(
jiabine99d0882021-09-17 05:21:25 +00002707 AudioMMapPolicyType policyType, std::vector<AudioMMapPolicyInfo> *policyInfos) {
Andy Hung264fa4c2024-02-21 15:52:12 -08002708 const sp<IAudioFlinger> af = get_audio_flinger();
Jiabin Huangebe64102021-09-07 20:01:07 +00002709 if (af == nullptr) {
2710 return PERMISSION_DENIED;
2711 }
2712 return af->getMmapPolicyInfos(policyType, policyInfos);
2713}
2714
jiabine504e7b2021-09-18 00:27:08 +00002715int32_t AudioSystem::getAAudioMixerBurstCount() {
Andy Hung264fa4c2024-02-21 15:52:12 -08002716 const sp<IAudioFlinger> af = get_audio_flinger();
jiabine504e7b2021-09-18 00:27:08 +00002717 if (af == nullptr) {
2718 return PERMISSION_DENIED;
2719 }
2720 return af->getAAudioMixerBurstCount();
2721}
2722
2723int32_t AudioSystem::getAAudioHardwareBurstMinUsec() {
Andy Hung264fa4c2024-02-21 15:52:12 -08002724 const sp<IAudioFlinger> af = get_audio_flinger();
jiabine504e7b2021-09-18 00:27:08 +00002725 if (af == nullptr) {
2726 return PERMISSION_DENIED;
2727 }
2728 return af->getAAudioHardwareBurstMinUsec();
2729}
2730
jiabina84c3d32022-12-02 18:59:55 +00002731status_t AudioSystem::getSupportedMixerAttributes(
2732 audio_port_handle_t portId, std::vector<audio_mixer_attributes_t> *mixerAttrs) {
Andy Hung264fa4c2024-02-21 15:52:12 -08002733 const sp<IAudioPolicyService> aps = get_audio_policy_service();
jiabina84c3d32022-12-02 18:59:55 +00002734 if (aps == nullptr) {
2735 return PERMISSION_DENIED;
2736 }
2737
2738 int32_t portIdAidl = VALUE_OR_RETURN_STATUS(legacy2aidl_audio_port_handle_t_int32_t(portId));
2739 std::vector<media::AudioMixerAttributesInternal> _aidlReturn;
2740 RETURN_STATUS_IF_ERROR(statusTFromBinderStatus(
2741 aps->getSupportedMixerAttributes(portIdAidl, &_aidlReturn)));
2742 *mixerAttrs = VALUE_OR_RETURN_STATUS(
2743 convertContainer<std::vector<audio_mixer_attributes_t>>(
2744 _aidlReturn,
2745 aidl2legacy_AudioMixerAttributesInternal_audio_mixer_attributes_t));
2746 return OK;
2747}
2748
2749status_t AudioSystem::setPreferredMixerAttributes(const audio_attributes_t *attr,
2750 audio_port_handle_t portId,
2751 uid_t uid,
2752 const audio_mixer_attributes_t *mixerAttr) {
Andy Hung264fa4c2024-02-21 15:52:12 -08002753 const sp<IAudioPolicyService> aps = get_audio_policy_service();
jiabina84c3d32022-12-02 18:59:55 +00002754 if (aps == nullptr) {
2755 return PERMISSION_DENIED;
2756 }
2757
Mikhail Naganov1c400902023-05-17 11:48:43 -07002758 media::audio::common::AudioAttributes attrAidl = VALUE_OR_RETURN_STATUS(
2759 legacy2aidl_audio_attributes_t_AudioAttributes(*attr));
jiabina84c3d32022-12-02 18:59:55 +00002760 media::AudioMixerAttributesInternal mixerAttrAidl = VALUE_OR_RETURN_STATUS(
2761 legacy2aidl_audio_mixer_attributes_t_AudioMixerAttributesInternal(*mixerAttr));
2762 int32_t uidAidl = VALUE_OR_RETURN_STATUS(legacy2aidl_uid_t_int32_t(uid));
2763 int32_t portIdAidl = VALUE_OR_RETURN_STATUS(legacy2aidl_audio_port_handle_t_int32_t(portId));
2764
2765 return statusTFromBinderStatus(
2766 aps->setPreferredMixerAttributes(attrAidl, portIdAidl, uidAidl, mixerAttrAidl));
2767}
2768
2769status_t AudioSystem::getPreferredMixerAttributes(
2770 const audio_attributes_t *attr,
2771 audio_port_handle_t portId,
2772 std::optional<audio_mixer_attributes_t> *mixerAttr) {
Andy Hung264fa4c2024-02-21 15:52:12 -08002773 const sp<IAudioPolicyService> aps = get_audio_policy_service();
jiabina84c3d32022-12-02 18:59:55 +00002774 if (aps == nullptr) {
2775 return PERMISSION_DENIED;
2776 }
2777
Mikhail Naganov1c400902023-05-17 11:48:43 -07002778 media::audio::common::AudioAttributes attrAidl = VALUE_OR_RETURN_STATUS(
2779 legacy2aidl_audio_attributes_t_AudioAttributes(*attr));
jiabina84c3d32022-12-02 18:59:55 +00002780 int32_t portIdAidl = VALUE_OR_RETURN_STATUS(legacy2aidl_audio_port_handle_t_int32_t(portId));
2781 std::optional<media::AudioMixerAttributesInternal> _aidlReturn;
2782 RETURN_STATUS_IF_ERROR(statusTFromBinderStatus(
2783 aps->getPreferredMixerAttributes(attrAidl, portIdAidl, &_aidlReturn)));
2784
2785 if (_aidlReturn.has_value()) {
2786 *mixerAttr = VALUE_OR_RETURN_STATUS(
2787 aidl2legacy_AudioMixerAttributesInternal_audio_mixer_attributes_t(
2788 _aidlReturn.value()));
2789 }
2790 return NO_ERROR;
2791}
2792
2793status_t AudioSystem::clearPreferredMixerAttributes(const audio_attributes_t *attr,
2794 audio_port_handle_t portId,
2795 uid_t uid) {
Andy Hung264fa4c2024-02-21 15:52:12 -08002796 const sp<IAudioPolicyService> aps = get_audio_policy_service();
jiabina84c3d32022-12-02 18:59:55 +00002797 if (aps == nullptr) {
2798 return PERMISSION_DENIED;
2799 }
2800
Mikhail Naganov1c400902023-05-17 11:48:43 -07002801 media::audio::common::AudioAttributes attrAidl = VALUE_OR_RETURN_STATUS(
2802 legacy2aidl_audio_attributes_t_AudioAttributes(*attr));
jiabina84c3d32022-12-02 18:59:55 +00002803 int32_t uidAidl = VALUE_OR_RETURN_STATUS(legacy2aidl_uid_t_int32_t(uid));
2804 int32_t portIdAidl = VALUE_OR_RETURN_STATUS(legacy2aidl_audio_port_handle_t_int32_t(portId));
2805 return statusTFromBinderStatus(
2806 aps->clearPreferredMixerAttributes(attrAidl, portIdAidl, uidAidl));
2807}
2808
Eric Laurentc2f1f072009-07-17 12:17:14 -07002809// ---------------------------------------------------------------------------
2810
Eric Laurente8726fe2015-06-26 09:39:24 -07002811int AudioSystem::AudioPolicyServiceClient::addAudioPortCallback(
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002812 const sp<AudioPortCallback>& callback) {
Andy Hungacd955a2024-02-16 17:14:36 -08002813 std::lock_guard _l(mMutex);
Andy Hungd211ce02024-02-16 15:34:07 -08002814 return mAudioPortCallbacks.insert(callback).second ? mAudioPortCallbacks.size() : -1;
Eric Laurentb28753e2015-04-01 13:06:28 -07002815}
2816
Eric Laurente8726fe2015-06-26 09:39:24 -07002817int AudioSystem::AudioPolicyServiceClient::removeAudioPortCallback(
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002818 const sp<AudioPortCallback>& callback) {
Andy Hungacd955a2024-02-16 17:14:36 -08002819 std::lock_guard _l(mMutex);
Andy Hungd211ce02024-02-16 15:34:07 -08002820 return mAudioPortCallbacks.erase(callback) > 0 ? mAudioPortCallbacks.size() : -1;
Eric Laurentb28753e2015-04-01 13:06:28 -07002821}
2822
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002823Status AudioSystem::AudioPolicyServiceClient::onAudioPortListUpdate() {
Andy Hungacd955a2024-02-16 17:14:36 -08002824 std::lock_guard _l(mMutex);
Andy Hungd211ce02024-02-16 15:34:07 -08002825 for (const auto& callback : mAudioPortCallbacks) {
2826 callback->onAudioPortListUpdate();
Eric Laurentb28753e2015-04-01 13:06:28 -07002827 }
Ytai Ben-Tsvi7e7a79d2020-12-15 16:48:16 -08002828 return Status::ok();
Eric Laurentb28753e2015-04-01 13:06:28 -07002829}
2830
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002831Status AudioSystem::AudioPolicyServiceClient::onAudioPatchListUpdate() {
Andy Hungacd955a2024-02-16 17:14:36 -08002832 std::lock_guard _l(mMutex);
Andy Hungd211ce02024-02-16 15:34:07 -08002833 for (const auto& callback : mAudioPortCallbacks) {
2834 callback->onAudioPatchListUpdate();
Eric Laurentb28753e2015-04-01 13:06:28 -07002835 }
Ytai Ben-Tsvi7e7a79d2020-12-15 16:48:16 -08002836 return Status::ok();
Eric Laurentb28753e2015-04-01 13:06:28 -07002837}
2838
François Gaffiecfe17322018-11-07 13:41:29 +01002839// ----------------------------------------------------------------------------
2840int AudioSystem::AudioPolicyServiceClient::addAudioVolumeGroupCallback(
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002841 const sp<AudioVolumeGroupCallback>& callback) {
Andy Hungacd955a2024-02-16 17:14:36 -08002842 std::lock_guard _l(mMutex);
Andy Hungd211ce02024-02-16 15:34:07 -08002843 return mAudioVolumeGroupCallbacks.insert(callback).second
2844 ? mAudioVolumeGroupCallbacks.size() : -1;
François Gaffiecfe17322018-11-07 13:41:29 +01002845}
2846
2847int AudioSystem::AudioPolicyServiceClient::removeAudioVolumeGroupCallback(
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002848 const sp<AudioVolumeGroupCallback>& callback) {
Andy Hungacd955a2024-02-16 17:14:36 -08002849 std::lock_guard _l(mMutex);
Andy Hungd211ce02024-02-16 15:34:07 -08002850 return mAudioVolumeGroupCallbacks.erase(callback) > 0
2851 ? mAudioVolumeGroupCallbacks.size() : -1;
François Gaffiecfe17322018-11-07 13:41:29 +01002852}
2853
Ytai Ben-Tsvi7e7a79d2020-12-15 16:48:16 -08002854Status AudioSystem::AudioPolicyServiceClient::onAudioVolumeGroupChanged(int32_t group,
2855 int32_t flags) {
2856 volume_group_t groupLegacy = VALUE_OR_RETURN_BINDER_STATUS(
2857 aidl2legacy_int32_t_volume_group_t(group));
2858 int flagsLegacy = VALUE_OR_RETURN_BINDER_STATUS(convertReinterpret<int>(flags));
2859
Andy Hungacd955a2024-02-16 17:14:36 -08002860 std::lock_guard _l(mMutex);
Andy Hungd211ce02024-02-16 15:34:07 -08002861 for (const auto& callback : mAudioVolumeGroupCallbacks) {
2862 callback->onAudioVolumeGroupChanged(groupLegacy, flagsLegacy);
François Gaffiecfe17322018-11-07 13:41:29 +01002863 }
Ytai Ben-Tsvi7e7a79d2020-12-15 16:48:16 -08002864 return Status::ok();
François Gaffiecfe17322018-11-07 13:41:29 +01002865}
2866// ----------------------------------------------------------------------------
2867
Ytai Ben-Tsvi7e7a79d2020-12-15 16:48:16 -08002868Status AudioSystem::AudioPolicyServiceClient::onDynamicPolicyMixStateUpdate(
2869 const ::std::string& regId, int32_t state) {
2870 ALOGV("AudioPolicyServiceClient::onDynamicPolicyMixStateUpdate(%s, %d)", regId.c_str(), state);
2871
2872 String8 regIdLegacy = VALUE_OR_RETURN_BINDER_STATUS(aidl2legacy_string_view_String8(regId));
2873 int stateLegacy = VALUE_OR_RETURN_BINDER_STATUS(convertReinterpret<int>(state));
Jean-Michel Trivif613d422015-04-23 18:41:29 -07002874 dynamic_policy_callback cb = NULL;
2875 {
Andy Hungacd955a2024-02-16 17:14:36 -08002876 std::lock_guard _l(AudioSystem::gMutex);
Jean-Michel Trivif613d422015-04-23 18:41:29 -07002877 cb = gDynPolicyCallback;
2878 }
2879
2880 if (cb != NULL) {
Ytai Ben-Tsvi7e7a79d2020-12-15 16:48:16 -08002881 cb(DYNAMIC_POLICY_EVENT_MIX_STATE_UPDATE, regIdLegacy, stateLegacy);
Jean-Michel Trivif613d422015-04-23 18:41:29 -07002882 }
Ytai Ben-Tsvi7e7a79d2020-12-15 16:48:16 -08002883 return Status::ok();
Jean-Michel Trivide801052015-04-14 19:10:14 -07002884}
2885
Ytai Ben-Tsvi7e7a79d2020-12-15 16:48:16 -08002886Status AudioSystem::AudioPolicyServiceClient::onRecordingConfigurationUpdate(
2887 int32_t event,
2888 const media::RecordClientInfo& clientInfo,
Mikhail Naganovdbf03642021-08-25 18:15:32 -07002889 const AudioConfigBase& clientConfig,
Ytai Ben-Tsvi7e7a79d2020-12-15 16:48:16 -08002890 const std::vector<media::EffectDescriptor>& clientEffects,
Mikhail Naganovdbf03642021-08-25 18:15:32 -07002891 const AudioConfigBase& deviceConfig,
Ytai Ben-Tsvi7e7a79d2020-12-15 16:48:16 -08002892 const std::vector<media::EffectDescriptor>& effects,
2893 int32_t patchHandle,
Mikhail Naganovddceecc2021-09-03 13:58:56 -07002894 AudioSource source) {
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -08002895 record_config_callback cb = NULL;
2896 {
Andy Hungacd955a2024-02-16 17:14:36 -08002897 std::lock_guard _l(AudioSystem::gMutex);
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -08002898 cb = gRecordConfigCallback;
2899 }
2900
2901 if (cb != NULL) {
Ytai Ben-Tsvi7e7a79d2020-12-15 16:48:16 -08002902 int eventLegacy = VALUE_OR_RETURN_BINDER_STATUS(convertReinterpret<int>(event));
2903 record_client_info_t clientInfoLegacy = VALUE_OR_RETURN_BINDER_STATUS(
2904 aidl2legacy_RecordClientInfo_record_client_info_t(clientInfo));
2905 audio_config_base_t clientConfigLegacy = VALUE_OR_RETURN_BINDER_STATUS(
Mikhail Naganovde3fa182021-07-30 15:06:42 -07002906 aidl2legacy_AudioConfigBase_audio_config_base_t(clientConfig, true /*isInput*/));
Ytai Ben-Tsvi7e7a79d2020-12-15 16:48:16 -08002907 std::vector<effect_descriptor_t> clientEffectsLegacy = VALUE_OR_RETURN_BINDER_STATUS(
2908 convertContainer<std::vector<effect_descriptor_t>>(
2909 clientEffects,
2910 aidl2legacy_EffectDescriptor_effect_descriptor_t));
2911 audio_config_base_t deviceConfigLegacy = VALUE_OR_RETURN_BINDER_STATUS(
Mikhail Naganovde3fa182021-07-30 15:06:42 -07002912 aidl2legacy_AudioConfigBase_audio_config_base_t(deviceConfig, true /*isInput*/));
Ytai Ben-Tsvi7e7a79d2020-12-15 16:48:16 -08002913 std::vector<effect_descriptor_t> effectsLegacy = VALUE_OR_RETURN_BINDER_STATUS(
2914 convertContainer<std::vector<effect_descriptor_t>>(
2915 effects,
2916 aidl2legacy_EffectDescriptor_effect_descriptor_t));
2917 audio_patch_handle_t patchHandleLegacy = VALUE_OR_RETURN_BINDER_STATUS(
2918 aidl2legacy_int32_t_audio_patch_handle_t(patchHandle));
2919 audio_source_t sourceLegacy = VALUE_OR_RETURN_BINDER_STATUS(
Mikhail Naganovddceecc2021-09-03 13:58:56 -07002920 aidl2legacy_AudioSource_audio_source_t(source));
Ytai Ben-Tsvi7e7a79d2020-12-15 16:48:16 -08002921 cb(eventLegacy, &clientInfoLegacy, &clientConfigLegacy, clientEffectsLegacy,
2922 &deviceConfigLegacy, effectsLegacy, patchHandleLegacy, sourceLegacy);
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -08002923 }
Ytai Ben-Tsvi7e7a79d2020-12-15 16:48:16 -08002924 return Status::ok();
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -08002925}
2926
Jean-Michel Trivi9a6b9ad2020-10-22 16:46:43 -07002927Status AudioSystem::AudioPolicyServiceClient::onRoutingUpdated() {
2928 routing_callback cb = NULL;
2929 {
Andy Hungacd955a2024-02-16 17:14:36 -08002930 std::lock_guard _l(AudioSystem::gMutex);
Jean-Michel Trivi9a6b9ad2020-10-22 16:46:43 -07002931 cb = gRoutingCallback;
2932 }
2933
2934 if (cb != NULL) {
2935 cb();
2936 }
2937 return Status::ok();
2938}
2939
Jean-Michel Trivi78f2b302022-04-15 18:18:41 +00002940Status AudioSystem::AudioPolicyServiceClient::onVolumeRangeInitRequest() {
2941 vol_range_init_req_callback cb = NULL;
2942 {
Andy Hungacd955a2024-02-16 17:14:36 -08002943 std::lock_guard _l(AudioSystem::gMutex);
Jean-Michel Trivi78f2b302022-04-15 18:18:41 +00002944 cb = gVolRangeInitReqCallback;
2945 }
2946
2947 if (cb != NULL) {
2948 cb();
2949 }
2950 return Status::ok();
2951}
2952
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002953void AudioSystem::AudioPolicyServiceClient::binderDied(const wp<IBinder>& who __unused) {
Glenn Kastend2d089f2014-11-05 11:48:12 -08002954 {
Andy Hungacd955a2024-02-16 17:14:36 -08002955 std::lock_guard _l(mMutex);
Andy Hungd211ce02024-02-16 15:34:07 -08002956 for (const auto& callback : mAudioPortCallbacks) {
2957 callback->onServiceDied();
Glenn Kastend2d089f2014-11-05 11:48:12 -08002958 }
Andy Hungd211ce02024-02-16 15:34:07 -08002959 for (const auto& callback : mAudioVolumeGroupCallbacks) {
2960 callback->onServiceDied();
François Gaffiecfe17322018-11-07 13:41:29 +01002961 }
Eric Laurentb52c1522014-05-20 11:27:36 -07002962 }
Mikhail Naganovec3d5792022-05-06 00:19:55 +00002963 AudioSystem::clearAudioPolicyService();
Eric Laurentc2f1f072009-07-17 12:17:14 -07002964
Steve Block5ff1dd52012-01-05 23:22:43 +00002965 ALOGW("AudioPolicyService server died!");
Eric Laurentc2f1f072009-07-17 12:17:14 -07002966}
2967
Ytai Ben-Tsvi7e7a79d2020-12-15 16:48:16 -08002968ConversionResult<record_client_info_t>
2969aidl2legacy_RecordClientInfo_record_client_info_t(const media::RecordClientInfo& aidl) {
2970 record_client_info_t legacy;
2971 legacy.riid = VALUE_OR_RETURN(aidl2legacy_int32_t_audio_unique_id_t(aidl.riid));
2972 legacy.uid = VALUE_OR_RETURN(aidl2legacy_int32_t_uid_t(aidl.uid));
2973 legacy.session = VALUE_OR_RETURN(aidl2legacy_int32_t_audio_session_t(aidl.session));
Mikhail Naganovddceecc2021-09-03 13:58:56 -07002974 legacy.source = VALUE_OR_RETURN(aidl2legacy_AudioSource_audio_source_t(aidl.source));
Ytai Ben-Tsvi7e7a79d2020-12-15 16:48:16 -08002975 legacy.port_id = VALUE_OR_RETURN(aidl2legacy_int32_t_audio_port_handle_t(aidl.portId));
2976 legacy.silenced = aidl.silenced;
2977 return legacy;
2978}
2979
2980ConversionResult<media::RecordClientInfo>
2981legacy2aidl_record_client_info_t_RecordClientInfo(const record_client_info_t& legacy) {
2982 media::RecordClientInfo aidl;
2983 aidl.riid = VALUE_OR_RETURN(legacy2aidl_audio_unique_id_t_int32_t(legacy.riid));
2984 aidl.uid = VALUE_OR_RETURN(legacy2aidl_uid_t_int32_t(legacy.uid));
2985 aidl.session = VALUE_OR_RETURN(legacy2aidl_audio_session_t_int32_t(legacy.session));
Mikhail Naganovddceecc2021-09-03 13:58:56 -07002986 aidl.source = VALUE_OR_RETURN(legacy2aidl_audio_source_t_AudioSource(legacy.source));
Ytai Ben-Tsvi7e7a79d2020-12-15 16:48:16 -08002987 aidl.portId = VALUE_OR_RETURN(legacy2aidl_audio_port_handle_t_int32_t(legacy.port_id));
2988 aidl.silenced = legacy.silenced;
2989 return aidl;
2990}
2991
Glenn Kasten40bc9062015-03-20 09:09:33 -07002992} // namespace android