blob: 8d5196bdab680611f039c99cfd6993a784092b64 [file] [log] [blame]
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001/*
2 * Copyright (C) 2006-2007 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#define LOG_TAG "AudioSystem"
18//#define LOG_NDEBUG 0
19
20#include <utils/Log.h>
Ytai Ben-Tsvi5120eda2020-03-26 09:41:15 -070021
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -080022#include <android/media/IAudioPolicyService.h>
Jan Sebechlebsky0af8e872023-08-11 14:45:08 +020023#include <android/media/AudioMixUpdate.h>
Ytai Ben-Tsvi5120eda2020-03-26 09:41:15 -070024#include <android/media/BnCaptureStateListener.h>
Marvin Raminbdefaf02023-11-01 09:10:32 +010025#include <android_media_audiopolicy.h>
Mathias Agopian75624082009-05-19 19:08:10 -070026#include <binder/IServiceManager.h>
Eric Laurentfb00fc72017-05-25 18:17:12 -070027#include <binder/ProcessState.h>
François Gaffie24437602018-04-23 15:08:59 +020028#include <binder/IPCThreadState.h>
Ytai Ben-Tsvi10dc0a62020-09-18 11:31:55 -070029#include <media/AidlConversion.h>
Eric Laurent21da6472017-11-09 16:29:26 -080030#include <media/AudioResamplerPublic.h>
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080031#include <media/AudioSystem.h>
Glenn Kasten1ab85ec2013-05-31 09:18:43 -070032#include <media/IAudioFlinger.h>
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -080033#include <media/PolicyAidlConversion.h>
François Gaffied0ba9ed2018-11-05 11:50:42 +010034#include <media/TypeConverter.h>
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080035#include <math.h>
36
Dima Zavin64760242011-05-11 14:15:23 -070037#include <system/audio.h>
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -080038#include <android/media/GetInputForAttrResponse.h>
jiabina84c3d32022-12-02 18:59:55 +000039#include <android/media/AudioMixerAttributesInternal.h>
Dima Zavinfce7a472011-04-19 22:30:36 -070040
Ytai Ben-Tsvi1ff75692020-11-06 12:16:12 -080041#define VALUE_OR_RETURN_BINDER_STATUS(x) \
Ytai Ben-Tsvi10dc0a62020-09-18 11:31:55 -070042 ({ auto _tmp = (x); \
Andy Hung1131b6e2020-12-08 20:47:45 -080043 if (!_tmp.ok()) return aidl_utils::binderStatusFromStatusT(_tmp.error()); \
Ytai Ben-Tsvia3815202020-10-28 14:58:08 -070044 std::move(_tmp.value()); })
Ytai Ben-Tsvi10dc0a62020-09-18 11:31:55 -070045
Eric Laurentc2f1f072009-07-17 12:17:14 -070046// ----------------------------------------------------------------------------
Eric Laurentc2f1f072009-07-17 12:17:14 -070047
Marvin Raminbdefaf02023-11-01 09:10:32 +010048namespace audio_flags = android::media::audiopolicy;
49
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080050namespace android {
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -080051using aidl_utils::statusTFromBinderStatus;
Ytai Ben-Tsvi10dc0a62020-09-18 11:31:55 -070052using binder::Status;
Mikhail Naganov57bd06f2021-08-10 16:41:54 -070053using content::AttributionSourceState;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -080054using media::IAudioPolicyService;
Mikhail Naganovdbf03642021-08-25 18:15:32 -070055using media::audio::common::AudioConfig;
56using media::audio::common::AudioConfigBase;
Mikhail Naganovf4a75362021-09-16 00:02:54 +000057using media::audio::common::AudioDevice;
58using media::audio::common::AudioDeviceDescription;
Mikhail Naganov57bd06f2021-08-10 16:41:54 -070059using media::audio::common::AudioFormatDescription;
jiabine99d0882021-09-17 05:21:25 +000060using media::audio::common::AudioMMapPolicyInfo;
61using media::audio::common::AudioMMapPolicyType;
Mikhail Naganovdbf03642021-08-25 18:15:32 -070062using media::audio::common::AudioOffloadInfo;
Mikhail Naganovddceecc2021-09-03 13:58:56 -070063using media::audio::common::AudioSource;
Mikhail Naganovdbf03642021-08-25 18:15:32 -070064using media::audio::common::AudioStreamType;
65using media::audio::common::AudioUsage;
Mikhail Naganov0078ee52021-09-30 23:06:20 +000066using media::audio::common::Int;
Ytai Ben-Tsvi10dc0a62020-09-18 11:31:55 -070067
Andy Hungacd955a2024-02-16 17:14:36 -080068std::mutex AudioSystem::gMutex;
Jean-Michel Trivif613d422015-04-23 18:41:29 -070069dynamic_policy_callback AudioSystem::gDynPolicyCallback = NULL;
Svet Ganovf4ddfef2018-01-16 07:37:58 -080070record_config_callback AudioSystem::gRecordConfigCallback = NULL;
Jean-Michel Trivi9a6b9ad2020-10-22 16:46:43 -070071routing_callback AudioSystem::gRoutingCallback = NULL;
Jean-Michel Trivi78f2b302022-04-15 18:18:41 +000072vol_range_init_req_callback AudioSystem::gVolRangeInitReqCallback = NULL;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080073
Andy Hungacd955a2024-02-16 17:14:36 -080074std::mutex AudioSystem::gErrorCallbacksMutex;
75std::set<audio_error_callback> AudioSystem::gAudioErrorCallbacks;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -080076
Andy Hungacd955a2024-02-16 17:14:36 -080077std::mutex AudioSystem::gSoundTriggerMutex;
78sp<CaptureStateListenerImpl> AudioSystem::gSoundTriggerCaptureStateListener;
Ytai Ben-Tsvi5120eda2020-03-26 09:41:15 -070079
Andy Hungacd955a2024-02-16 17:14:36 -080080std::mutex AudioSystem::gAPSMutex;
81sp<IAudioPolicyService> AudioSystem::gAudioPolicyService;
82sp<AudioSystem::AudioPolicyServiceClient> AudioSystem::gAudioPolicyServiceClient;
83
84// Sets the Binder for the AudioFlinger service, passed to this client process
85// from the system server.
Ahaan Ugale058f94b2021-05-21 02:23:50 -070086// This allows specific isolated processes to access the audio system. Currently used only for the
87// HotwordDetectionService.
Andy Hungda1fb072024-02-20 19:08:08 -080088template <typename ServiceInterface, typename Client, typename AidlInterface,
89 typename ServiceTraits>
90class ServiceHandler {
91public:
92 sp<ServiceInterface> getService(bool canStartThreadPool = true)
93 EXCLUDES(mMutex) NO_THREAD_SAFETY_ANALYSIS { // std::unique_ptr
94 sp<ServiceInterface> service;
95 sp<Client> client;
Ahaan Ugale058f94b2021-05-21 02:23:50 -070096
Andy Hungda1fb072024-02-20 19:08:08 -080097 bool reportNoError = false;
98 {
99 std::lock_guard _l(mMutex);
100 if (mService != nullptr) {
101 return mService;
102 }
Andy Hung66fc0bd2023-01-04 11:45:50 -0800103 }
104
Andy Hungda1fb072024-02-20 19:08:08 -0800105 std::unique_lock ul_only1thread(mSingleGetter);
106 std::unique_lock ul(mMutex);
107 if (mService != nullptr) {
108 return mService;
109 }
110 if (mClient == nullptr) {
111 mClient = sp<Client>::make();
Andy Hung66fc0bd2023-01-04 11:45:50 -0800112 } else {
113 reportNoError = true;
114 }
Andy Hungda1fb072024-02-20 19:08:08 -0800115 while (true) {
116 mService = mLocalService;
117 if (mService != nullptr) break;
Andy Hung66fc0bd2023-01-04 11:45:50 -0800118
Andy Hungda1fb072024-02-20 19:08:08 -0800119 sp<IBinder> binder = mBinder;
120 if (binder == nullptr) {
121 sp <IServiceManager> sm = defaultServiceManager();
122 binder = sm->checkService(String16(ServiceTraits::SERVICE_NAME));
Paul Wangbcf95f62023-02-28 15:38:16 +0000123 if (binder == nullptr) {
Andy Hungda1fb072024-02-20 19:08:08 -0800124 ALOGD("%s: waiting for %s", __func__, ServiceTraits::SERVICE_NAME);
125
126 // if the condition variable is present, setLocalService() and
127 // setBinder() is allowed to use it to notify us.
128 if (mCvGetter == nullptr) {
129 mCvGetter = std::make_shared<std::condition_variable>();
130 }
131 mCvGetter->wait_for(ul, std::chrono::seconds(1));
132 continue;
Paul Wangbcf95f62023-02-28 15:38:16 +0000133 }
Ahaan Ugale058f94b2021-05-21 02:23:50 -0700134 }
Andy Hungda1fb072024-02-20 19:08:08 -0800135 binder->linkToDeath(mClient);
136 auto aidlInterface = interface_cast<AidlInterface>(binder);
137 LOG_ALWAYS_FATAL_IF(aidlInterface == nullptr);
138 if constexpr (std::is_same_v<ServiceInterface, AidlInterface>) {
139 mService = std::move(aidlInterface);
140 } else /* constexpr */ {
141 mService = ServiceTraits::createServiceAdapter(aidlInterface);
142 }
143 break;
Glenn Kastene53b9ea2012-03-12 16:29:55 -0700144 }
Andy Hungda1fb072024-02-20 19:08:08 -0800145 if (mCvGetter) mCvGetter.reset(); // remove condition variable.
146 client = mClient;
147 service = mService;
148 // Make sure callbacks can be received by the client
149 if (canStartThreadPool) {
Akshata Kadam1d486b72023-08-07 05:49:48 +0000150 ProcessState::self()->startThreadPool();
151 }
Andy Hungda1fb072024-02-20 19:08:08 -0800152 ul.unlock();
153 ul_only1thread.unlock();
154 ServiceTraits::onServiceCreate(service, client);
155 if (reportNoError) AudioSystem::reportError(NO_ERROR);
156 return service;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800157 }
Andy Hungda1fb072024-02-20 19:08:08 -0800158
159 status_t setLocalService(const sp<ServiceInterface>& service) EXCLUDES(mMutex) {
160 std::lock_guard _l(mMutex);
161 // we allow clearing once set, but not a double non-null set.
162 if (mService != nullptr && service != nullptr) return INVALID_OPERATION;
163 mLocalService = service;
164 if (mCvGetter) mCvGetter->notify_one();
165 return OK;
166 }
167
168 sp<Client> getClient() EXCLUDES(mMutex) {
169 const auto service = getService();
170 if (service == nullptr) return nullptr;
171 std::lock_guard _l(mMutex);
172 return mClient;
173 }
174
175 void setBinder(const sp<IBinder>& binder) EXCLUDES(mMutex) {
176 std::lock_guard _l(mMutex);
177 if (mService != nullptr) {
178 ALOGW("%s: ignoring; %s connection already established.",
179 __func__, ServiceTraits::SERVICE_NAME);
180 return;
181 }
182 mBinder = binder;
183 if (mCvGetter) mCvGetter->notify_one();
184 }
185
186 void clearService() EXCLUDES(mMutex) {
187 std::lock_guard _l(mMutex);
188 mService.clear();
189 if (mClient) ServiceTraits::onClearService(mClient);
190 }
191
192private:
193 std::mutex mSingleGetter;
194 std::mutex mMutex;
195 std::shared_ptr<std::condition_variable> mCvGetter GUARDED_BY(mMutex);
196 sp<IBinder> mBinder GUARDED_BY(mMutex);
197 sp<ServiceInterface> mLocalService GUARDED_BY(mMutex);
198 sp<ServiceInterface> mService GUARDED_BY(mMutex);
199 sp<Client> mClient GUARDED_BY(mMutex);
200};
201
202struct AudioFlingerTraits {
203 static void onServiceCreate(
204 const sp<IAudioFlinger>& af, const sp<AudioSystem::AudioFlingerClient>& afc) {
205 const int64_t token = IPCThreadState::self()->clearCallingIdentity();
206 af->registerClient(afc);
207 IPCThreadState::self()->restoreCallingIdentity(token);
208 }
209
210 static sp<IAudioFlinger> createServiceAdapter(
211 const sp<media::IAudioFlingerService>& aidlInterface) {
212 return sp<AudioFlingerClientAdapter>::make(aidlInterface);
213 }
214
215 static void onClearService(const sp<AudioSystem::AudioFlingerClient>& afc) {
216 afc->clearIoCache();
217 }
218
219 static constexpr const char* SERVICE_NAME = IAudioFlinger::DEFAULT_SERVICE_NAME;
220};
221
222[[clang::no_destroy]] static constinit ServiceHandler<IAudioFlinger,
223 AudioSystem::AudioFlingerClient, media::IAudioFlingerService,
224 AudioFlingerTraits> gAudioFlingerServiceHandler;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800225
Andy Hung264fa4c2024-02-21 15:52:12 -0800226sp<IAudioFlinger> AudioSystem::get_audio_flinger() {
Andy Hungda1fb072024-02-20 19:08:08 -0800227 return gAudioFlingerServiceHandler.getService();
Akshata Kadam1d486b72023-08-07 05:49:48 +0000228}
229
Andy Hung264fa4c2024-02-21 15:52:12 -0800230sp<IAudioFlinger> AudioSystem::get_audio_flinger_for_fuzzer() {
Andy Hungda1fb072024-02-20 19:08:08 -0800231 return gAudioFlingerServiceHandler.getService(false /* canStartThreadPool */);
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
Glenn Kasten18a6d902012-09-24 11:27:56 -0700932// establish binder interface to AudioPolicy service
Andy Hung264fa4c2024-02-21 15:52:12 -0800933sp<IAudioPolicyService> AudioSystem::get_audio_policy_service() {
Eric Laurent0ebd5f92014-11-19 19:04:52 -0800934 sp<IAudioPolicyService> ap;
935 sp<AudioPolicyServiceClient> apc;
936 {
Andy Hungacd955a2024-02-16 17:14:36 -0800937 std::lock_guard _l(gAPSMutex);
Eric Laurent0ebd5f92014-11-19 19:04:52 -0800938 if (gAudioPolicyService == 0) {
939 sp<IServiceManager> sm = defaultServiceManager();
Paul Wangbcf95f62023-02-28 15:38:16 +0000940 sp<IBinder> binder = sm->waitForService(String16("media.audio_policy"));
941 if (binder == nullptr) {
942 return nullptr;
943 }
Eric Laurent0ebd5f92014-11-19 19:04:52 -0800944 if (gAudioPolicyServiceClient == NULL) {
945 gAudioPolicyServiceClient = new AudioPolicyServiceClient();
946 }
947 binder->linkToDeath(gAudioPolicyServiceClient);
948 gAudioPolicyService = interface_cast<IAudioPolicyService>(binder);
949 LOG_ALWAYS_FATAL_IF(gAudioPolicyService == 0);
950 apc = gAudioPolicyServiceClient;
Eric Laurentfb00fc72017-05-25 18:17:12 -0700951 // Make sure callbacks can be received by gAudioPolicyServiceClient
952 ProcessState::self()->startThreadPool();
Eric Laurentc2f1f072009-07-17 12:17:14 -0700953 }
Eric Laurent0ebd5f92014-11-19 19:04:52 -0800954 ap = gAudioPolicyService;
955 }
956 if (apc != 0) {
François Gaffie24437602018-04-23 15:08:59 +0200957 int64_t token = IPCThreadState::self()->clearCallingIdentity();
Eric Laurent0ebd5f92014-11-19 19:04:52 -0800958 ap->registerClient(apc);
François Gaffie24437602018-04-23 15:08:59 +0200959 ap->setAudioPortCallbacksEnabled(apc->isAudioPortCbEnabled());
François Gaffiecfe17322018-11-07 13:41:29 +0100960 ap->setAudioVolumeGroupCallbacksEnabled(apc->isAudioVolumeGroupCbEnabled());
François Gaffie24437602018-04-23 15:08:59 +0200961 IPCThreadState::self()->restoreCallingIdentity(token);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700962 }
Glenn Kastend2d089f2014-11-05 11:48:12 -0800963
Eric Laurent0ebd5f92014-11-19 19:04:52 -0800964 return ap;
Eric Laurentc2f1f072009-07-17 12:17:14 -0700965}
966
Mikhail Naganovec3d5792022-05-06 00:19:55 +0000967void AudioSystem::clearAudioPolicyService() {
Andy Hungacd955a2024-02-16 17:14:36 -0800968 std::lock_guard _l(gAPSMutex);
Mikhail Naganovec3d5792022-05-06 00:19:55 +0000969 gAudioPolicyService.clear();
970}
971
Glenn Kastenfb1fdc92013-07-10 17:03:19 -0700972// ---------------------------------------------------------------------------
973
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800974void AudioSystem::onNewAudioModulesAvailable() {
Andy Hung264fa4c2024-02-21 15:52:12 -0800975 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Mikhail Naganov88b30d22020-03-09 19:43:13 +0000976 if (aps == 0) return;
977 aps->onNewAudioModulesAvailable();
978}
979
Nathalie Le Clair88fa2752021-11-23 13:03:41 +0100980status_t AudioSystem::setDeviceConnectionState(audio_policy_dev_state_t state,
981 const android::media::audio::common::AudioPort& port,
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800982 audio_format_t encodedFormat) {
Andy Hung264fa4c2024-02-21 15:52:12 -0800983 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Eric Laurent71b63e32011-09-02 14:20:56 -0700984
Eric Laurentc2f1f072009-07-17 12:17:14 -0700985 if (aps == 0) return PERMISSION_DENIED;
986
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800987 return statusTFromBinderStatus(
988 aps->setDeviceConnectionState(
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800989 VALUE_OR_RETURN_STATUS(
990 legacy2aidl_audio_policy_dev_state_t_AudioPolicyDeviceState(state)),
Nathalie Le Clair88fa2752021-11-23 13:03:41 +0100991 port,
Mikhail Naganovb60bd1b2021-07-15 17:31:43 -0700992 VALUE_OR_RETURN_STATUS(
993 legacy2aidl_audio_format_t_AudioFormatDescription(encodedFormat))));
Eric Laurentc2f1f072009-07-17 12:17:14 -0700994}
995
Dima Zavinfce7a472011-04-19 22:30:36 -0700996audio_policy_dev_state_t AudioSystem::getDeviceConnectionState(audio_devices_t device,
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800997 const char* device_address) {
Andy Hung264fa4c2024-02-21 15:52:12 -0800998 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Dima Zavinfce7a472011-04-19 22:30:36 -0700999 if (aps == 0) return AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE;
Eric Laurentc2f1f072009-07-17 12:17:14 -07001000
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001001 auto result = [&]() -> ConversionResult<audio_policy_dev_state_t> {
Mikhail Naganov932cb962021-09-16 01:05:27 +00001002 AudioDevice deviceAidl = VALUE_OR_RETURN(
1003 legacy2aidl_audio_device_AudioDevice(device, device_address));
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001004
1005 media::AudioPolicyDeviceState result;
1006 RETURN_IF_ERROR(statusTFromBinderStatus(
1007 aps->getDeviceConnectionState(deviceAidl, &result)));
1008
1009 return aidl2legacy_AudioPolicyDeviceState_audio_policy_dev_state_t(result);
1010 }();
1011 return result.value_or(AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE);
Eric Laurentc2f1f072009-07-17 12:17:14 -07001012}
1013
Pavlin Radoslavovf862bc62016-12-26 18:57:22 -08001014status_t AudioSystem::handleDeviceConfigChange(audio_devices_t device,
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001015 const char* device_address,
1016 const char* device_name,
1017 audio_format_t encodedFormat) {
Andy Hung264fa4c2024-02-21 15:52:12 -08001018 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001019 const char* address = "";
1020 const char* name = "";
Pavlin Radoslavovf862bc62016-12-26 18:57:22 -08001021
1022 if (aps == 0) return PERMISSION_DENIED;
1023
1024 if (device_address != NULL) {
1025 address = device_address;
1026 }
1027 if (device_name != NULL) {
1028 name = device_name;
1029 }
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001030
Mikhail Naganov932cb962021-09-16 01:05:27 +00001031 AudioDevice deviceAidl = VALUE_OR_RETURN_STATUS(
1032 legacy2aidl_audio_device_AudioDevice(device, address));
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001033
1034 return statusTFromBinderStatus(
1035 aps->handleDeviceConfigChange(deviceAidl, name, VALUE_OR_RETURN_STATUS(
Mikhail Naganovb60bd1b2021-07-15 17:31:43 -07001036 legacy2aidl_audio_format_t_AudioFormatDescription(encodedFormat))));
Pavlin Radoslavovf862bc62016-12-26 18:57:22 -08001037}
1038
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001039status_t AudioSystem::setPhoneState(audio_mode_t state, uid_t uid) {
Glenn Kasten347966c2012-01-18 14:58:32 -08001040 if (uint32_t(state) >= AUDIO_MODE_CNT) return BAD_VALUE;
Andy Hung264fa4c2024-02-21 15:52:12 -08001041 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Eric Laurentc2f1f072009-07-17 12:17:14 -07001042 if (aps == 0) return PERMISSION_DENIED;
1043
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001044 return statusTFromBinderStatus(aps->setPhoneState(
1045 VALUE_OR_RETURN_STATUS(legacy2aidl_audio_mode_t_AudioMode(state)),
1046 VALUE_OR_RETURN_STATUS(legacy2aidl_uid_t_int32_t(uid))));
Eric Laurentc2f1f072009-07-17 12:17:14 -07001047}
1048
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001049status_t
1050AudioSystem::setForceUse(audio_policy_force_use_t usage, audio_policy_forced_cfg_t config) {
Andy Hung264fa4c2024-02-21 15:52:12 -08001051 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Eric Laurentc2f1f072009-07-17 12:17:14 -07001052 if (aps == 0) return PERMISSION_DENIED;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001053
1054 return statusTFromBinderStatus(
1055 aps->setForceUse(
1056 VALUE_OR_RETURN_STATUS(
1057 legacy2aidl_audio_policy_force_use_t_AudioPolicyForceUse(usage)),
1058 VALUE_OR_RETURN_STATUS(
1059 legacy2aidl_audio_policy_forced_cfg_t_AudioPolicyForcedConfig(
1060 config))));
Eric Laurentc2f1f072009-07-17 12:17:14 -07001061}
1062
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001063audio_policy_forced_cfg_t AudioSystem::getForceUse(audio_policy_force_use_t usage) {
Andy Hung264fa4c2024-02-21 15:52:12 -08001064 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Dima Zavinfce7a472011-04-19 22:30:36 -07001065 if (aps == 0) return AUDIO_POLICY_FORCE_NONE;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001066
1067 auto result = [&]() -> ConversionResult<audio_policy_forced_cfg_t> {
1068 media::AudioPolicyForceUse usageAidl = VALUE_OR_RETURN(
1069 legacy2aidl_audio_policy_force_use_t_AudioPolicyForceUse(usage));
1070 media::AudioPolicyForcedConfig configAidl;
1071 RETURN_IF_ERROR(statusTFromBinderStatus(
1072 aps->getForceUse(usageAidl, &configAidl)));
1073 return aidl2legacy_AudioPolicyForcedConfig_audio_policy_forced_cfg_t(configAidl);
1074 }();
1075
1076 return result.value_or(AUDIO_POLICY_FORCE_NONE);
Eric Laurentc2f1f072009-07-17 12:17:14 -07001077}
1078
1079
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001080audio_io_handle_t AudioSystem::getOutput(audio_stream_type_t stream) {
Andy Hung264fa4c2024-02-21 15:52:12 -08001081 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001082 if (aps == 0) return AUDIO_IO_HANDLE_NONE;
1083
1084 auto result = [&]() -> ConversionResult<audio_io_handle_t> {
Mikhail Naganovdbf03642021-08-25 18:15:32 -07001085 AudioStreamType streamAidl = VALUE_OR_RETURN(
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001086 legacy2aidl_audio_stream_type_t_AudioStreamType(stream));
1087 int32_t outputAidl;
1088 RETURN_IF_ERROR(
1089 statusTFromBinderStatus(aps->getOutput(streamAidl, &outputAidl)));
1090 return aidl2legacy_int32_t_audio_io_handle_t(outputAidl);
1091 }();
1092
1093 return result.value_or(AUDIO_IO_HANDLE_NONE);
Eric Laurentc2f1f072009-07-17 12:17:14 -07001094}
1095
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001096status_t AudioSystem::getOutputForAttr(audio_attributes_t* attr,
1097 audio_io_handle_t* output,
1098 audio_session_t session,
1099 audio_stream_type_t* stream,
Svet Ganov3e5f14f2021-05-13 22:51:08 +00001100 const AttributionSourceState& attributionSource,
jiabinf1c73972022-04-14 16:28:52 -07001101 audio_config_t* config,
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001102 audio_output_flags_t flags,
1103 audio_port_handle_t* selectedDeviceId,
1104 audio_port_handle_t* portId,
Eric Laurentb0a7bc92022-04-05 15:06:08 +02001105 std::vector<audio_io_handle_t>* secondaryOutputs,
jiabinc658e452022-10-21 20:52:21 +00001106 bool *isSpatialized,
1107 bool *isBitPerfect) {
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001108 if (attr == nullptr) {
1109 ALOGE("%s NULL audio attributes", __func__);
1110 return BAD_VALUE;
1111 }
1112 if (output == nullptr) {
1113 ALOGE("%s NULL output - shouldn't happen", __func__);
1114 return BAD_VALUE;
1115 }
1116 if (selectedDeviceId == nullptr) {
1117 ALOGE("%s NULL selectedDeviceId - shouldn't happen", __func__);
1118 return BAD_VALUE;
1119 }
1120 if (portId == nullptr) {
1121 ALOGE("%s NULL portId - shouldn't happen", __func__);
1122 return BAD_VALUE;
1123 }
1124 if (secondaryOutputs == nullptr) {
1125 ALOGE("%s NULL secondaryOutputs - shouldn't happen", __func__);
1126 return BAD_VALUE;
1127 }
1128
Andy Hung264fa4c2024-02-21 15:52:12 -08001129 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Eric Laurente83b55d2014-11-14 10:06:21 -08001130 if (aps == 0) return NO_INIT;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001131
Mikhail Naganov1c400902023-05-17 11:48:43 -07001132 media::audio::common::AudioAttributes attrAidl = VALUE_OR_RETURN_STATUS(
1133 legacy2aidl_audio_attributes_t_AudioAttributes(*attr));
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001134 int32_t sessionAidl = VALUE_OR_RETURN_STATUS(legacy2aidl_audio_session_t_int32_t(session));
Mikhail Naganovdbf03642021-08-25 18:15:32 -07001135 AudioConfig configAidl = VALUE_OR_RETURN_STATUS(
Mikhail Naganovde3fa182021-07-30 15:06:42 -07001136 legacy2aidl_audio_config_t_AudioConfig(*config, false /*isInput*/));
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001137 int32_t flagsAidl = VALUE_OR_RETURN_STATUS(
1138 legacy2aidl_audio_output_flags_t_int32_t_mask(flags));
Eric Laurentf99edd32021-02-01 15:57:33 +01001139 int32_t selectedDeviceIdAidl = VALUE_OR_RETURN_STATUS(
1140 legacy2aidl_audio_port_handle_t_int32_t(*selectedDeviceId));
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001141
1142 media::GetOutputForAttrResponse responseAidl;
1143
jiabinf1c73972022-04-14 16:28:52 -07001144 status_t status = statusTFromBinderStatus(
Svet Ganov3e5f14f2021-05-13 22:51:08 +00001145 aps->getOutputForAttr(attrAidl, sessionAidl, attributionSource, configAidl, flagsAidl,
jiabinf1c73972022-04-14 16:28:52 -07001146 selectedDeviceIdAidl, &responseAidl));
1147 if (status != NO_ERROR) {
1148 config->format = VALUE_OR_RETURN_STATUS(
1149 aidl2legacy_AudioFormatDescription_audio_format_t(responseAidl.configBase.format));
1150 config->channel_mask = VALUE_OR_RETURN_STATUS(
1151 aidl2legacy_AudioChannelLayout_audio_channel_mask_t(
1152 responseAidl.configBase.channelMask, false /*isInput*/));
1153 config->sample_rate = responseAidl.configBase.sampleRate;
1154 return status;
1155 }
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001156
1157 *output = VALUE_OR_RETURN_STATUS(
1158 aidl2legacy_int32_t_audio_io_handle_t(responseAidl.output));
1159
1160 if (stream != nullptr) {
1161 *stream = VALUE_OR_RETURN_STATUS(
1162 aidl2legacy_AudioStreamType_audio_stream_type_t(responseAidl.stream));
1163 }
1164 *selectedDeviceId = VALUE_OR_RETURN_STATUS(
1165 aidl2legacy_int32_t_audio_port_handle_t(responseAidl.selectedDeviceId));
1166 *portId = VALUE_OR_RETURN_STATUS(aidl2legacy_int32_t_audio_port_handle_t(responseAidl.portId));
1167 *secondaryOutputs = VALUE_OR_RETURN_STATUS(convertContainer<std::vector<audio_io_handle_t>>(
1168 responseAidl.secondaryOutputs, aidl2legacy_int32_t_audio_io_handle_t));
Eric Laurentb0a7bc92022-04-05 15:06:08 +02001169 *isSpatialized = responseAidl.isSpatialized;
jiabinc658e452022-10-21 20:52:21 +00001170 *isBitPerfect = responseAidl.isBitPerfect;
Eric Laurent0d13fea2022-11-04 17:12:08 +01001171 *attr = VALUE_OR_RETURN_STATUS(
Mikhail Naganov1c400902023-05-17 11:48:43 -07001172 aidl2legacy_AudioAttributes_audio_attributes_t(responseAidl.attr));
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001173
1174 return OK;
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -07001175}
1176
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001177status_t AudioSystem::startOutput(audio_port_handle_t portId) {
Andy Hung264fa4c2024-02-21 15:52:12 -08001178 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Eric Laurentc2f1f072009-07-17 12:17:14 -07001179 if (aps == 0) return PERMISSION_DENIED;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001180
1181 int32_t portIdAidl = VALUE_OR_RETURN_STATUS(legacy2aidl_audio_port_handle_t_int32_t(portId));
1182 return statusTFromBinderStatus(aps->startOutput(portIdAidl));
Eric Laurentc2f1f072009-07-17 12:17:14 -07001183}
1184
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001185status_t AudioSystem::stopOutput(audio_port_handle_t portId) {
Andy Hung264fa4c2024-02-21 15:52:12 -08001186 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Eric Laurentc2f1f072009-07-17 12:17:14 -07001187 if (aps == 0) return PERMISSION_DENIED;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001188
1189 int32_t portIdAidl = VALUE_OR_RETURN_STATUS(legacy2aidl_audio_port_handle_t_int32_t(portId));
1190 return statusTFromBinderStatus(aps->stopOutput(portIdAidl));
Eric Laurentc2f1f072009-07-17 12:17:14 -07001191}
1192
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001193void AudioSystem::releaseOutput(audio_port_handle_t portId) {
Andy Hung264fa4c2024-02-21 15:52:12 -08001194 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Eric Laurentc2f1f072009-07-17 12:17:14 -07001195 if (aps == 0) return;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001196
1197 auto status = [&]() -> status_t {
1198 int32_t portIdAidl = VALUE_OR_RETURN_STATUS(
1199 legacy2aidl_audio_port_handle_t_int32_t(portId));
1200 RETURN_STATUS_IF_ERROR(statusTFromBinderStatus(aps->releaseOutput(portIdAidl)));
1201 return OK;
1202 }();
1203
1204 // Ignore status.
1205 (void) status;
Eric Laurentc2f1f072009-07-17 12:17:14 -07001206}
1207
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001208status_t AudioSystem::getInputForAttr(const audio_attributes_t* attr,
1209 audio_io_handle_t* input,
1210 audio_unique_id_t riid,
1211 audio_session_t session,
Svet Ganov3e5f14f2021-05-13 22:51:08 +00001212 const AttributionSourceState &attributionSource,
jiabinf1c73972022-04-14 16:28:52 -07001213 audio_config_base_t* config,
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001214 audio_input_flags_t flags,
1215 audio_port_handle_t* selectedDeviceId,
1216 audio_port_handle_t* portId) {
1217 if (attr == NULL) {
1218 ALOGE("getInputForAttr NULL attr - shouldn't happen");
1219 return BAD_VALUE;
1220 }
1221 if (input == NULL) {
1222 ALOGE("getInputForAttr NULL input - shouldn't happen");
1223 return BAD_VALUE;
1224 }
1225 if (selectedDeviceId == NULL) {
1226 ALOGE("getInputForAttr NULL selectedDeviceId - shouldn't happen");
1227 return BAD_VALUE;
1228 }
1229 if (portId == NULL) {
1230 ALOGE("getInputForAttr NULL portId - shouldn't happen");
1231 return BAD_VALUE;
1232 }
1233
Andy Hung264fa4c2024-02-21 15:52:12 -08001234 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Eric Laurentcaf7f482014-11-25 17:50:47 -08001235 if (aps == 0) return NO_INIT;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001236
Mikhail Naganov1c400902023-05-17 11:48:43 -07001237 media::audio::common::AudioAttributes attrAidl = VALUE_OR_RETURN_STATUS(
1238 legacy2aidl_audio_attributes_t_AudioAttributes(*attr));
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001239 int32_t inputAidl = VALUE_OR_RETURN_STATUS(legacy2aidl_audio_io_handle_t_int32_t(*input));
1240 int32_t riidAidl = VALUE_OR_RETURN_STATUS(legacy2aidl_audio_unique_id_t_int32_t(riid));
1241 int32_t sessionAidl = VALUE_OR_RETURN_STATUS(legacy2aidl_audio_session_t_int32_t(session));
Mikhail Naganovdbf03642021-08-25 18:15:32 -07001242 AudioConfigBase configAidl = VALUE_OR_RETURN_STATUS(
Mikhail Naganovde3fa182021-07-30 15:06:42 -07001243 legacy2aidl_audio_config_base_t_AudioConfigBase(*config, true /*isInput*/));
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001244 int32_t flagsAidl = VALUE_OR_RETURN_STATUS(legacy2aidl_audio_input_flags_t_int32_t_mask(flags));
Eric Laurentf99edd32021-02-01 15:57:33 +01001245 int32_t selectedDeviceIdAidl = VALUE_OR_RETURN_STATUS(
1246 legacy2aidl_audio_port_handle_t_int32_t(*selectedDeviceId));
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001247
1248 media::GetInputForAttrResponse response;
1249
jiabinf1c73972022-04-14 16:28:52 -07001250 status_t status = statusTFromBinderStatus(
Svet Ganov3e5f14f2021-05-13 22:51:08 +00001251 aps->getInputForAttr(attrAidl, inputAidl, riidAidl, sessionAidl, attributionSource,
jiabinf1c73972022-04-14 16:28:52 -07001252 configAidl, flagsAidl, selectedDeviceIdAidl, &response));
1253 if (status != NO_ERROR) {
1254 *config = VALUE_OR_RETURN_STATUS(
1255 aidl2legacy_AudioConfigBase_audio_config_base_t(response.config, true /*isInput*/));
1256 return status;
1257 }
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001258
1259 *input = VALUE_OR_RETURN_STATUS(aidl2legacy_int32_t_audio_io_handle_t(response.input));
1260 *selectedDeviceId = VALUE_OR_RETURN_STATUS(
1261 aidl2legacy_int32_t_audio_port_handle_t(response.selectedDeviceId));
1262 *portId = VALUE_OR_RETURN_STATUS(aidl2legacy_int32_t_audio_port_handle_t(response.portId));
1263
1264 return OK;
Eric Laurentc2f1f072009-07-17 12:17:14 -07001265}
1266
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001267status_t AudioSystem::startInput(audio_port_handle_t portId) {
Andy Hung264fa4c2024-02-21 15:52:12 -08001268 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Eric Laurentc2f1f072009-07-17 12:17:14 -07001269 if (aps == 0) return PERMISSION_DENIED;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001270
1271 int32_t portIdAidl = VALUE_OR_RETURN_STATUS(legacy2aidl_audio_port_handle_t_int32_t(portId));
1272 return statusTFromBinderStatus(aps->startInput(portIdAidl));
Eric Laurentc2f1f072009-07-17 12:17:14 -07001273}
1274
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001275status_t AudioSystem::stopInput(audio_port_handle_t portId) {
Andy Hung264fa4c2024-02-21 15:52:12 -08001276 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Eric Laurentc2f1f072009-07-17 12:17:14 -07001277 if (aps == 0) return PERMISSION_DENIED;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001278
1279 int32_t portIdAidl = VALUE_OR_RETURN_STATUS(legacy2aidl_audio_port_handle_t_int32_t(portId));
1280 return statusTFromBinderStatus(aps->stopInput(portIdAidl));
Eric Laurentc2f1f072009-07-17 12:17:14 -07001281}
1282
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001283void AudioSystem::releaseInput(audio_port_handle_t portId) {
Andy Hung264fa4c2024-02-21 15:52:12 -08001284 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Eric Laurentc2f1f072009-07-17 12:17:14 -07001285 if (aps == 0) return;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001286
1287 auto status = [&]() -> status_t {
1288 int32_t portIdAidl = VALUE_OR_RETURN_STATUS(
1289 legacy2aidl_audio_port_handle_t_int32_t(portId));
1290 RETURN_STATUS_IF_ERROR(statusTFromBinderStatus(aps->releaseInput(portIdAidl)));
1291 return OK;
1292 }();
1293
1294 // Ignore status.
1295 (void) status;
Eric Laurentc2f1f072009-07-17 12:17:14 -07001296}
1297
Dima Zavinfce7a472011-04-19 22:30:36 -07001298status_t AudioSystem::initStreamVolume(audio_stream_type_t stream,
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001299 int indexMin,
1300 int indexMax) {
Andy Hung264fa4c2024-02-21 15:52:12 -08001301 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Eric Laurentc2f1f072009-07-17 12:17:14 -07001302 if (aps == 0) return PERMISSION_DENIED;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001303
Mikhail Naganovdbf03642021-08-25 18:15:32 -07001304 AudioStreamType streamAidl = VALUE_OR_RETURN_STATUS(
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001305 legacy2aidl_audio_stream_type_t_AudioStreamType(stream));
1306 int32_t indexMinAidl = VALUE_OR_RETURN_STATUS(convertIntegral<int32_t>(indexMin));
1307 int32_t indexMaxAidl = VALUE_OR_RETURN_STATUS(convertIntegral<int32_t>(indexMax));
Mikhail Naganovec3d5792022-05-06 00:19:55 +00001308 status_t status = statusTFromBinderStatus(
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001309 aps->initStreamVolume(streamAidl, indexMinAidl, indexMaxAidl));
Mikhail Naganovec3d5792022-05-06 00:19:55 +00001310 if (status == DEAD_OBJECT) {
1311 // This is a critical operation since w/o proper stream volumes no audio
1312 // will be heard. Make sure we recover from a failure in any case.
1313 ALOGE("Received DEAD_OBJECT from APS, clearing the client");
1314 clearAudioPolicyService();
1315 }
1316 return status;
Eric Laurentc2f1f072009-07-17 12:17:14 -07001317}
1318
Eric Laurent83844cc2011-11-18 16:43:31 -08001319status_t AudioSystem::setStreamVolumeIndex(audio_stream_type_t stream,
1320 int index,
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001321 audio_devices_t device) {
Andy Hung264fa4c2024-02-21 15:52:12 -08001322 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Eric Laurentc2f1f072009-07-17 12:17:14 -07001323 if (aps == 0) return PERMISSION_DENIED;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001324
Mikhail Naganovdbf03642021-08-25 18:15:32 -07001325 AudioStreamType streamAidl = VALUE_OR_RETURN_STATUS(
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001326 legacy2aidl_audio_stream_type_t_AudioStreamType(stream));
1327 int32_t indexAidl = VALUE_OR_RETURN_STATUS(convertIntegral<int32_t>(index));
Mikhail Naganovf4a75362021-09-16 00:02:54 +00001328 AudioDeviceDescription deviceAidl = VALUE_OR_RETURN_STATUS(
Mikhail Naganov21a32ec2021-07-08 14:40:12 -07001329 legacy2aidl_audio_devices_t_AudioDeviceDescription(device));
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001330 return statusTFromBinderStatus(
1331 aps->setStreamVolumeIndex(streamAidl, deviceAidl, indexAidl));
Eric Laurentc2f1f072009-07-17 12:17:14 -07001332}
1333
Eric Laurent83844cc2011-11-18 16:43:31 -08001334status_t AudioSystem::getStreamVolumeIndex(audio_stream_type_t stream,
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001335 int* index,
1336 audio_devices_t device) {
Andy Hung264fa4c2024-02-21 15:52:12 -08001337 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Eric Laurentc2f1f072009-07-17 12:17:14 -07001338 if (aps == 0) return PERMISSION_DENIED;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001339
Mikhail Naganovdbf03642021-08-25 18:15:32 -07001340 AudioStreamType streamAidl = VALUE_OR_RETURN_STATUS(
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001341 legacy2aidl_audio_stream_type_t_AudioStreamType(stream));
Mikhail Naganovf4a75362021-09-16 00:02:54 +00001342 AudioDeviceDescription deviceAidl = VALUE_OR_RETURN_STATUS(
Mikhail Naganov21a32ec2021-07-08 14:40:12 -07001343 legacy2aidl_audio_devices_t_AudioDeviceDescription(device));
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001344 int32_t indexAidl;
1345 RETURN_STATUS_IF_ERROR(statusTFromBinderStatus(
1346 aps->getStreamVolumeIndex(streamAidl, deviceAidl, &indexAidl)));
1347 if (index != nullptr) {
1348 *index = VALUE_OR_RETURN_STATUS(convertIntegral<int>(indexAidl));
1349 }
1350 return OK;
Eric Laurentc2f1f072009-07-17 12:17:14 -07001351}
1352
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001353status_t AudioSystem::setVolumeIndexForAttributes(const audio_attributes_t& attr,
François Gaffiecfe17322018-11-07 13:41:29 +01001354 int index,
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001355 audio_devices_t device) {
Andy Hung264fa4c2024-02-21 15:52:12 -08001356 const sp<IAudioPolicyService> aps = get_audio_policy_service();
François Gaffiecfe17322018-11-07 13:41:29 +01001357 if (aps == 0) return PERMISSION_DENIED;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001358
Mikhail Naganov1c400902023-05-17 11:48:43 -07001359 media::audio::common::AudioAttributes attrAidl = VALUE_OR_RETURN_STATUS(
1360 legacy2aidl_audio_attributes_t_AudioAttributes(attr));
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001361 int32_t indexAidl = VALUE_OR_RETURN_STATUS(convertIntegral<int32_t>(index));
Mikhail Naganovf4a75362021-09-16 00:02:54 +00001362 AudioDeviceDescription deviceAidl = VALUE_OR_RETURN_STATUS(
Mikhail Naganov21a32ec2021-07-08 14:40:12 -07001363 legacy2aidl_audio_devices_t_AudioDeviceDescription(device));
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001364 return statusTFromBinderStatus(
1365 aps->setVolumeIndexForAttributes(attrAidl, deviceAidl, indexAidl));
François Gaffiecfe17322018-11-07 13:41:29 +01001366}
1367
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001368status_t AudioSystem::getVolumeIndexForAttributes(const audio_attributes_t& attr,
1369 int& index,
1370 audio_devices_t device) {
Andy Hung264fa4c2024-02-21 15:52:12 -08001371 const sp<IAudioPolicyService> aps = get_audio_policy_service();
François Gaffiecfe17322018-11-07 13:41:29 +01001372 if (aps == 0) return PERMISSION_DENIED;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001373
Mikhail Naganov1c400902023-05-17 11:48:43 -07001374 media::audio::common::AudioAttributes attrAidl = VALUE_OR_RETURN_STATUS(
1375 legacy2aidl_audio_attributes_t_AudioAttributes(attr));
Mikhail Naganovf4a75362021-09-16 00:02:54 +00001376 AudioDeviceDescription deviceAidl = VALUE_OR_RETURN_STATUS(
Mikhail Naganov21a32ec2021-07-08 14:40:12 -07001377 legacy2aidl_audio_devices_t_AudioDeviceDescription(device));
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001378 int32_t indexAidl;
1379 RETURN_STATUS_IF_ERROR(statusTFromBinderStatus(
1380 aps->getVolumeIndexForAttributes(attrAidl, deviceAidl, &indexAidl)));
1381 index = VALUE_OR_RETURN_STATUS(convertIntegral<int>(indexAidl));
1382 return OK;
François Gaffiecfe17322018-11-07 13:41:29 +01001383}
1384
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001385status_t AudioSystem::getMaxVolumeIndexForAttributes(const audio_attributes_t& attr, int& index) {
Andy Hung264fa4c2024-02-21 15:52:12 -08001386 const sp<IAudioPolicyService> aps = get_audio_policy_service();
François Gaffiecfe17322018-11-07 13:41:29 +01001387 if (aps == 0) return PERMISSION_DENIED;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001388
Mikhail Naganov1c400902023-05-17 11:48:43 -07001389 media::audio::common::AudioAttributes attrAidl = VALUE_OR_RETURN_STATUS(
1390 legacy2aidl_audio_attributes_t_AudioAttributes(attr));
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001391 int32_t indexAidl;
1392 RETURN_STATUS_IF_ERROR(statusTFromBinderStatus(
1393 aps->getMaxVolumeIndexForAttributes(attrAidl, &indexAidl)));
1394 index = VALUE_OR_RETURN_STATUS(convertIntegral<int>(indexAidl));
1395 return OK;
François Gaffiecfe17322018-11-07 13:41:29 +01001396}
1397
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001398status_t AudioSystem::getMinVolumeIndexForAttributes(const audio_attributes_t& attr, int& index) {
Andy Hung264fa4c2024-02-21 15:52:12 -08001399 const sp<IAudioPolicyService> aps = get_audio_policy_service();
François Gaffiecfe17322018-11-07 13:41:29 +01001400 if (aps == 0) return PERMISSION_DENIED;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001401
Mikhail Naganov1c400902023-05-17 11:48:43 -07001402 media::audio::common::AudioAttributes attrAidl = VALUE_OR_RETURN_STATUS(
1403 legacy2aidl_audio_attributes_t_AudioAttributes(attr));
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001404 int32_t indexAidl;
1405 RETURN_STATUS_IF_ERROR(statusTFromBinderStatus(
1406 aps->getMinVolumeIndexForAttributes(attrAidl, &indexAidl)));
1407 index = VALUE_OR_RETURN_STATUS(convertIntegral<int>(indexAidl));
1408 return OK;
François Gaffiecfe17322018-11-07 13:41:29 +01001409}
1410
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001411product_strategy_t AudioSystem::getStrategyForStream(audio_stream_type_t stream) {
Andy Hung264fa4c2024-02-21 15:52:12 -08001412 const sp<IAudioPolicyService> aps = get_audio_policy_service();
François Gaffied0ba9ed2018-11-05 11:50:42 +01001413 if (aps == 0) return PRODUCT_STRATEGY_NONE;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001414
1415 auto result = [&]() -> ConversionResult<product_strategy_t> {
Mikhail Naganovdbf03642021-08-25 18:15:32 -07001416 AudioStreamType streamAidl = VALUE_OR_RETURN(
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001417 legacy2aidl_audio_stream_type_t_AudioStreamType(stream));
1418 int32_t resultAidl;
1419 RETURN_IF_ERROR(statusTFromBinderStatus(
1420 aps->getStrategyForStream(streamAidl, &resultAidl)));
1421 return aidl2legacy_int32_t_product_strategy_t(resultAidl);
1422 }();
1423 return result.value_or(PRODUCT_STRATEGY_NONE);
Eric Laurentde070132010-07-13 04:45:46 -07001424}
1425
François Gaffie1e2b56f2022-04-01 14:34:29 +02001426status_t AudioSystem::getDevicesForAttributes(const audio_attributes_t& aa,
Andy Hung6d23c0f2022-02-16 09:37:15 -08001427 AudioDeviceTypeAddrVector* devices,
1428 bool forVolume) {
Jean-Michel Trivif41599b2020-01-07 14:22:08 -08001429 if (devices == nullptr) {
1430 return BAD_VALUE;
1431 }
Andy Hung264fa4c2024-02-21 15:52:12 -08001432 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Jean-Michel Trivif41599b2020-01-07 14:22:08 -08001433 if (aps == 0) return PERMISSION_DENIED;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001434
Mikhail Naganov1c400902023-05-17 11:48:43 -07001435 media::audio::common::AudioAttributes aaAidl = VALUE_OR_RETURN_STATUS(
1436 legacy2aidl_audio_attributes_t_AudioAttributes(aa));
Mikhail Naganovf4a75362021-09-16 00:02:54 +00001437 std::vector<AudioDevice> retAidl;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001438 RETURN_STATUS_IF_ERROR(
Andy Hung6d23c0f2022-02-16 09:37:15 -08001439 statusTFromBinderStatus(aps->getDevicesForAttributes(aaAidl, forVolume, &retAidl)));
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001440 *devices = VALUE_OR_RETURN_STATUS(
1441 convertContainer<AudioDeviceTypeAddrVector>(
1442 retAidl,
1443 aidl2legacy_AudioDeviceTypeAddress));
1444 return OK;
Jean-Michel Trivif41599b2020-01-07 14:22:08 -08001445}
1446
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001447audio_io_handle_t AudioSystem::getOutputForEffect(const effect_descriptor_t* desc) {
Andy Hung264fa4c2024-02-21 15:52:12 -08001448 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Glenn Kastenefa6ea92014-01-08 09:10:43 -08001449 // FIXME change return type to status_t, and return PERMISSION_DENIED here
Glenn Kasten142f5192014-03-25 17:44:59 -07001450 if (aps == 0) return AUDIO_IO_HANDLE_NONE;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001451
1452 auto result = [&]() -> ConversionResult<audio_io_handle_t> {
1453 media::EffectDescriptor descAidl = VALUE_OR_RETURN(
1454 legacy2aidl_effect_descriptor_t_EffectDescriptor(*desc));
1455 int32_t retAidl;
1456 RETURN_IF_ERROR(
1457 statusTFromBinderStatus(aps->getOutputForEffect(descAidl, &retAidl)));
1458 return aidl2legacy_int32_t_audio_io_handle_t(retAidl);
1459 }();
1460
1461 return result.value_or(AUDIO_IO_HANDLE_NONE);
Eric Laurentde070132010-07-13 04:45:46 -07001462}
1463
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001464status_t AudioSystem::registerEffect(const effect_descriptor_t* desc,
1465 audio_io_handle_t io,
1466 product_strategy_t strategy,
1467 audio_session_t session,
1468 int id) {
Andy Hung264fa4c2024-02-21 15:52:12 -08001469 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Eric Laurentde070132010-07-13 04:45:46 -07001470 if (aps == 0) return PERMISSION_DENIED;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001471
1472 media::EffectDescriptor descAidl = VALUE_OR_RETURN_STATUS(
1473 legacy2aidl_effect_descriptor_t_EffectDescriptor(*desc));
1474 int32_t ioAidl = VALUE_OR_RETURN_STATUS(legacy2aidl_audio_io_handle_t_int32_t(io));
1475 int32_t strategyAidl = VALUE_OR_RETURN_STATUS(aidl2legacy_int32_t_product_strategy_t(strategy));
1476 int32_t sessionAidl = VALUE_OR_RETURN_STATUS(legacy2aidl_audio_session_t_int32_t(session));
1477 int32_t idAidl = VALUE_OR_RETURN_STATUS(convertReinterpret<int32_t>(id));
1478 return statusTFromBinderStatus(
1479 aps->registerEffect(descAidl, ioAidl, strategyAidl, sessionAidl, idAidl));
Eric Laurentde070132010-07-13 04:45:46 -07001480}
1481
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001482status_t AudioSystem::unregisterEffect(int id) {
Andy Hung264fa4c2024-02-21 15:52:12 -08001483 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Eric Laurentde070132010-07-13 04:45:46 -07001484 if (aps == 0) return PERMISSION_DENIED;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001485
1486 int32_t idAidl = VALUE_OR_RETURN_STATUS(convertReinterpret<int32_t>(id));
1487 return statusTFromBinderStatus(
1488 aps->unregisterEffect(idAidl));
Eric Laurentde070132010-07-13 04:45:46 -07001489}
1490
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001491status_t AudioSystem::setEffectEnabled(int id, bool enabled) {
Andy Hung264fa4c2024-02-21 15:52:12 -08001492 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Eric Laurentdb7c0792011-08-10 10:37:50 -07001493 if (aps == 0) return PERMISSION_DENIED;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001494
1495 int32_t idAidl = VALUE_OR_RETURN_STATUS(convertReinterpret<int32_t>(id));
1496 return statusTFromBinderStatus(
1497 aps->setEffectEnabled(idAidl, enabled));
Eric Laurentdb7c0792011-08-10 10:37:50 -07001498}
1499
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001500status_t AudioSystem::moveEffectsToIo(const std::vector<int>& ids, audio_io_handle_t io) {
Andy Hung264fa4c2024-02-21 15:52:12 -08001501 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Eric Laurent6c796322019-04-09 14:13:17 -07001502 if (aps == 0) return PERMISSION_DENIED;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001503
1504 std::vector<int32_t> idsAidl = VALUE_OR_RETURN_STATUS(
1505 convertContainer<std::vector<int32_t>>(ids, convertReinterpret<int32_t, int>));
1506 int32_t ioAidl = VALUE_OR_RETURN_STATUS(legacy2aidl_audio_io_handle_t_int32_t(io));
1507 return statusTFromBinderStatus(aps->moveEffectsToIo(idsAidl, ioAidl));
Eric Laurent6c796322019-04-09 14:13:17 -07001508}
1509
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001510status_t AudioSystem::isStreamActive(audio_stream_type_t stream, bool* state, uint32_t inPastMs) {
Andy Hung264fa4c2024-02-21 15:52:12 -08001511 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Eric Laurenteda6c362011-02-02 09:33:30 -08001512 if (aps == 0) return PERMISSION_DENIED;
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001513 if (state == NULL) return BAD_VALUE;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001514
Mikhail Naganovdbf03642021-08-25 18:15:32 -07001515 AudioStreamType streamAidl = VALUE_OR_RETURN_STATUS(
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001516 legacy2aidl_audio_stream_type_t_AudioStreamType(stream));
1517 int32_t inPastMsAidl = VALUE_OR_RETURN_STATUS(convertIntegral<int32_t>(inPastMs));
1518 RETURN_STATUS_IF_ERROR(statusTFromBinderStatus(
1519 aps->isStreamActive(streamAidl, inPastMsAidl, state)));
1520 return OK;
Eric Laurenteda6c362011-02-02 09:33:30 -08001521}
1522
Jean-Michel Trivi272ab542013-02-04 16:26:02 -08001523status_t AudioSystem::isStreamActiveRemotely(audio_stream_type_t stream, bool* state,
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001524 uint32_t inPastMs) {
Andy Hung264fa4c2024-02-21 15:52:12 -08001525 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Jean-Michel Trivi272ab542013-02-04 16:26:02 -08001526 if (aps == 0) return PERMISSION_DENIED;
1527 if (state == NULL) return BAD_VALUE;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001528
Mikhail Naganovdbf03642021-08-25 18:15:32 -07001529 AudioStreamType streamAidl = VALUE_OR_RETURN_STATUS(
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001530 legacy2aidl_audio_stream_type_t_AudioStreamType(stream));
1531 int32_t inPastMsAidl = VALUE_OR_RETURN_STATUS(convertIntegral<int32_t>(inPastMs));
1532 RETURN_STATUS_IF_ERROR(statusTFromBinderStatus(
1533 aps->isStreamActiveRemotely(streamAidl, inPastMsAidl, state)));
1534 return OK;
Jean-Michel Trivi272ab542013-02-04 16:26:02 -08001535}
1536
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001537status_t AudioSystem::isSourceActive(audio_source_t stream, bool* state) {
Andy Hung264fa4c2024-02-21 15:52:12 -08001538 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Jean-Michel Trivid7086032012-10-10 12:11:16 -07001539 if (aps == 0) return PERMISSION_DENIED;
1540 if (state == NULL) return BAD_VALUE;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001541
Mikhail Naganovddceecc2021-09-03 13:58:56 -07001542 AudioSource streamAidl = VALUE_OR_RETURN_STATUS(
1543 legacy2aidl_audio_source_t_AudioSource(stream));
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001544 RETURN_STATUS_IF_ERROR(statusTFromBinderStatus(
1545 aps->isSourceActive(streamAidl, state)));
1546 return OK;
Jean-Michel Trivid7086032012-10-10 12:11:16 -07001547}
1548
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001549uint32_t AudioSystem::getPrimaryOutputSamplingRate() {
Andy Hung264fa4c2024-02-21 15:52:12 -08001550 const sp<IAudioFlinger> af = get_audio_flinger();
Glenn Kastencc0f1cf2012-09-24 11:27:18 -07001551 if (af == 0) return 0;
1552 return af->getPrimaryOutputSamplingRate();
1553}
1554
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001555size_t AudioSystem::getPrimaryOutputFrameCount() {
Andy Hung264fa4c2024-02-21 15:52:12 -08001556 const sp<IAudioFlinger> af = get_audio_flinger();
Glenn Kastencc0f1cf2012-09-24 11:27:18 -07001557 if (af == 0) return 0;
1558 return af->getPrimaryOutputFrameCount();
1559}
Eric Laurenteda6c362011-02-02 09:33:30 -08001560
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001561status_t AudioSystem::setLowRamDevice(bool isLowRamDevice, int64_t totalMemory) {
Andy Hung264fa4c2024-02-21 15:52:12 -08001562 const sp<IAudioFlinger> af = get_audio_flinger();
Glenn Kasten4182c4e2013-07-15 14:45:07 -07001563 if (af == 0) return PERMISSION_DENIED;
Andy Hung6f248bb2018-01-23 14:04:37 -08001564 return af->setLowRamDevice(isLowRamDevice, totalMemory);
Glenn Kasten4182c4e2013-07-15 14:45:07 -07001565}
1566
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001567void AudioSystem::clearAudioConfigCache() {
Glenn Kastend2d089f2014-11-05 11:48:12 -08001568 // called by restoreTrack_l(), which needs new IAudioFlinger and IAudioPolicyService instances
Steve Block3856b092011-10-20 11:56:00 +01001569 ALOGV("clearAudioConfigCache()");
Andy Hungda1fb072024-02-20 19:08:08 -08001570 gAudioFlingerServiceHandler.clearService();
Mikhail Naganovec3d5792022-05-06 00:19:55 +00001571 clearAudioPolicyService();
Eric Laurent9f6530f2011-08-30 10:18:54 -07001572}
1573
Hayden Gomes524159d2019-12-23 14:41:47 -08001574status_t AudioSystem::setSupportedSystemUsages(const std::vector<audio_usage_t>& systemUsages) {
Andy Hung264fa4c2024-02-21 15:52:12 -08001575 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Hayden Gomes524159d2019-12-23 14:41:47 -08001576 if (aps == nullptr) return PERMISSION_DENIED;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001577
Mikhail Naganovdbf03642021-08-25 18:15:32 -07001578 std::vector<AudioUsage> systemUsagesAidl = VALUE_OR_RETURN_STATUS(
1579 convertContainer<std::vector<AudioUsage>>(systemUsages,
1580 legacy2aidl_audio_usage_t_AudioUsage));
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001581 return statusTFromBinderStatus(aps->setSupportedSystemUsages(systemUsagesAidl));
Hayden Gomes524159d2019-12-23 14:41:47 -08001582}
1583
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001584status_t AudioSystem::setAllowedCapturePolicy(uid_t uid, audio_flags_mask_t capturePolicy) {
Andy Hung264fa4c2024-02-21 15:52:12 -08001585 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Kevin Rocardb99cc752019-03-21 20:52:24 -07001586 if (aps == nullptr) return PERMISSION_DENIED;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001587
1588 int32_t uidAidl = VALUE_OR_RETURN_STATUS(legacy2aidl_uid_t_int32_t(uid));
1589 int32_t capturePolicyAidl = VALUE_OR_RETURN_STATUS(
1590 legacy2aidl_audio_flags_mask_t_int32_t_mask(capturePolicy));
1591 return statusTFromBinderStatus(aps->setAllowedCapturePolicy(uidAidl, capturePolicyAidl));
Kevin Rocardb99cc752019-03-21 20:52:24 -07001592}
1593
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001594audio_offload_mode_t AudioSystem::getOffloadSupport(const audio_offload_info_t& info) {
Eric Laurent90fe31c2020-11-26 20:06:35 +01001595 ALOGV("%s", __func__);
Andy Hung264fa4c2024-02-21 15:52:12 -08001596 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Eric Laurent90fe31c2020-11-26 20:06:35 +01001597 if (aps == 0) return AUDIO_OFFLOAD_NOT_SUPPORTED;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001598
1599 auto result = [&]() -> ConversionResult<audio_offload_mode_t> {
Mikhail Naganovdbf03642021-08-25 18:15:32 -07001600 AudioOffloadInfo infoAidl = VALUE_OR_RETURN(
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001601 legacy2aidl_audio_offload_info_t_AudioOffloadInfo(info));
1602 media::AudioOffloadMode retAidl;
1603 RETURN_IF_ERROR(
1604 statusTFromBinderStatus(aps->getOffloadSupport(infoAidl, &retAidl)));
1605 return aidl2legacy_AudioOffloadMode_audio_offload_mode_t(retAidl);
1606 }();
1607
1608 return result.value_or(static_cast<audio_offload_mode_t>(0));
Richard Fitzgeraldad3af332013-03-25 16:54:37 +00001609}
1610
Eric Laurent203b1a12014-04-01 10:34:16 -07001611status_t AudioSystem::listAudioPorts(audio_port_role_t role,
1612 audio_port_type_t type,
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001613 unsigned int* num_ports,
1614 struct audio_port_v7* ports,
1615 unsigned int* generation) {
1616 if (num_ports == nullptr || (*num_ports != 0 && ports == nullptr) ||
1617 generation == nullptr) {
1618 return BAD_VALUE;
1619 }
1620
Andy Hung264fa4c2024-02-21 15:52:12 -08001621 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Eric Laurent203b1a12014-04-01 10:34:16 -07001622 if (aps == 0) return PERMISSION_DENIED;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001623
1624 media::AudioPortRole roleAidl = VALUE_OR_RETURN_STATUS(
1625 legacy2aidl_audio_port_role_t_AudioPortRole(role));
1626 media::AudioPortType typeAidl = VALUE_OR_RETURN_STATUS(
1627 legacy2aidl_audio_port_type_t_AudioPortType(type));
Mikhail Naganov0078ee52021-09-30 23:06:20 +00001628 Int numPortsAidl;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001629 numPortsAidl.value = VALUE_OR_RETURN_STATUS(convertIntegral<int32_t>(*num_ports));
Atneya Nair638a6e42022-12-18 16:45:15 -08001630 std::vector<media::AudioPortFw> portsAidl;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001631 int32_t generationAidl;
1632
1633 RETURN_STATUS_IF_ERROR(statusTFromBinderStatus(
1634 aps->listAudioPorts(roleAidl, typeAidl, &numPortsAidl, &portsAidl, &generationAidl)));
1635 *num_ports = VALUE_OR_RETURN_STATUS(convertIntegral<unsigned int>(numPortsAidl.value));
1636 *generation = VALUE_OR_RETURN_STATUS(convertIntegral<unsigned int>(generationAidl));
1637 RETURN_STATUS_IF_ERROR(convertRange(portsAidl.begin(), portsAidl.end(), ports,
Mikhail Naganov87227252023-01-13 17:38:10 -08001638 aidl2legacy_AudioPortFw_audio_port_v7));
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001639 return OK;
Eric Laurent203b1a12014-04-01 10:34:16 -07001640}
1641
Mikhail Naganov5edc5ed2023-03-23 14:52:15 -07001642status_t AudioSystem::listDeclaredDevicePorts(media::AudioPortRole role,
1643 std::vector<media::AudioPortFw>* result) {
1644 if (result == nullptr) return BAD_VALUE;
Andy Hung264fa4c2024-02-21 15:52:12 -08001645 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Mikhail Naganov5edc5ed2023-03-23 14:52:15 -07001646 if (aps == 0) return PERMISSION_DENIED;
1647 RETURN_STATUS_IF_ERROR(statusTFromBinderStatus(aps->listDeclaredDevicePorts(role, result)));
1648 return OK;
1649}
1650
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001651status_t AudioSystem::getAudioPort(struct audio_port_v7* port) {
1652 if (port == nullptr) {
1653 return BAD_VALUE;
1654 }
Andy Hung264fa4c2024-02-21 15:52:12 -08001655 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Eric Laurent203b1a12014-04-01 10:34:16 -07001656 if (aps == 0) return PERMISSION_DENIED;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001657
Atneya Nair638a6e42022-12-18 16:45:15 -08001658 media::AudioPortFw portAidl;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001659 RETURN_STATUS_IF_ERROR(
Mikhail Naganov17031562022-02-23 23:00:27 +00001660 statusTFromBinderStatus(aps->getAudioPort(port->id, &portAidl)));
Mikhail Naganov87227252023-01-13 17:38:10 -08001661 *port = VALUE_OR_RETURN_STATUS(aidl2legacy_AudioPortFw_audio_port_v7(portAidl));
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001662 return OK;
Eric Laurent203b1a12014-04-01 10:34:16 -07001663}
1664
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001665status_t AudioSystem::createAudioPatch(const struct audio_patch* patch,
1666 audio_patch_handle_t* handle) {
1667 if (patch == nullptr || handle == nullptr) {
1668 return BAD_VALUE;
1669 }
1670
Andy Hung264fa4c2024-02-21 15:52:12 -08001671 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Eric Laurent203b1a12014-04-01 10:34:16 -07001672 if (aps == 0) return PERMISSION_DENIED;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001673
Atneya Nair3afdbd12022-12-18 16:14:18 -08001674 media::AudioPatchFw patchAidl = VALUE_OR_RETURN_STATUS(
Mikhail Naganov87227252023-01-13 17:38:10 -08001675 legacy2aidl_audio_patch_AudioPatchFw(*patch));
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001676 int32_t handleAidl = VALUE_OR_RETURN_STATUS(legacy2aidl_audio_patch_handle_t_int32_t(*handle));
1677 RETURN_STATUS_IF_ERROR(
1678 statusTFromBinderStatus(aps->createAudioPatch(patchAidl, handleAidl, &handleAidl)));
1679 *handle = VALUE_OR_RETURN_STATUS(aidl2legacy_int32_t_audio_patch_handle_t(handleAidl));
1680 return OK;
Eric Laurent203b1a12014-04-01 10:34:16 -07001681}
1682
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001683status_t AudioSystem::releaseAudioPatch(audio_patch_handle_t handle) {
Andy Hung264fa4c2024-02-21 15:52:12 -08001684 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Eric Laurent203b1a12014-04-01 10:34:16 -07001685 if (aps == 0) return PERMISSION_DENIED;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001686
1687 int32_t handleAidl = VALUE_OR_RETURN_STATUS(legacy2aidl_audio_patch_handle_t_int32_t(handle));
1688 return statusTFromBinderStatus(aps->releaseAudioPatch(handleAidl));
Eric Laurent203b1a12014-04-01 10:34:16 -07001689}
1690
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001691status_t AudioSystem::listAudioPatches(unsigned int* num_patches,
1692 struct audio_patch* patches,
1693 unsigned int* generation) {
1694 if (num_patches == nullptr || (*num_patches != 0 && patches == nullptr) ||
1695 generation == nullptr) {
1696 return BAD_VALUE;
1697 }
1698
Andy Hung264fa4c2024-02-21 15:52:12 -08001699 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Eric Laurent203b1a12014-04-01 10:34:16 -07001700 if (aps == 0) return PERMISSION_DENIED;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001701
1702
Mikhail Naganov0078ee52021-09-30 23:06:20 +00001703 Int numPatchesAidl;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001704 numPatchesAidl.value = VALUE_OR_RETURN_STATUS(convertIntegral<int32_t>(*num_patches));
Atneya Nair3afdbd12022-12-18 16:14:18 -08001705 std::vector<media::AudioPatchFw> patchesAidl;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001706 int32_t generationAidl;
1707
1708 RETURN_STATUS_IF_ERROR(statusTFromBinderStatus(
1709 aps->listAudioPatches(&numPatchesAidl, &patchesAidl, &generationAidl)));
1710 *num_patches = VALUE_OR_RETURN_STATUS(convertIntegral<unsigned int>(numPatchesAidl.value));
1711 *generation = VALUE_OR_RETURN_STATUS(convertIntegral<unsigned int>(generationAidl));
1712 RETURN_STATUS_IF_ERROR(convertRange(patchesAidl.begin(), patchesAidl.end(), patches,
Mikhail Naganov87227252023-01-13 17:38:10 -08001713 aidl2legacy_AudioPatchFw_audio_patch));
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001714 return OK;
Eric Laurent203b1a12014-04-01 10:34:16 -07001715}
1716
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001717status_t AudioSystem::setAudioPortConfig(const struct audio_port_config* config) {
1718 if (config == nullptr) {
1719 return BAD_VALUE;
1720 }
1721
Andy Hung264fa4c2024-02-21 15:52:12 -08001722 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Eric Laurent203b1a12014-04-01 10:34:16 -07001723 if (aps == 0) return PERMISSION_DENIED;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001724
Atneya Nair7a9594f2022-12-18 17:26:26 -08001725 media::AudioPortConfigFw configAidl = VALUE_OR_RETURN_STATUS(
Mikhail Naganov87227252023-01-13 17:38:10 -08001726 legacy2aidl_audio_port_config_AudioPortConfigFw(*config));
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001727 return statusTFromBinderStatus(aps->setAudioPortConfig(configAidl));
Eric Laurent203b1a12014-04-01 10:34:16 -07001728}
1729
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001730status_t AudioSystem::addAudioPortCallback(const sp<AudioPortCallback>& callback) {
Andy Hung264fa4c2024-02-21 15:52:12 -08001731 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Eric Laurentb28753e2015-04-01 13:06:28 -07001732 if (aps == 0) return PERMISSION_DENIED;
1733
Andy Hungacd955a2024-02-16 17:14:36 -08001734 std::lock_guard _l(gAPSMutex);
Eric Laurentb28753e2015-04-01 13:06:28 -07001735 if (gAudioPolicyServiceClient == 0) {
1736 return NO_INIT;
1737 }
Eric Laurente8726fe2015-06-26 09:39:24 -07001738 int ret = gAudioPolicyServiceClient->addAudioPortCallback(callback);
1739 if (ret == 1) {
1740 aps->setAudioPortCallbacksEnabled(true);
1741 }
1742 return (ret < 0) ? INVALID_OPERATION : NO_ERROR;
Eric Laurentb52c1522014-05-20 11:27:36 -07001743}
1744
Jean-Michel Trivif613d422015-04-23 18:41:29 -07001745/*static*/
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001746status_t AudioSystem::removeAudioPortCallback(const sp<AudioPortCallback>& callback) {
Andy Hung264fa4c2024-02-21 15:52:12 -08001747 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Eric Laurentb28753e2015-04-01 13:06:28 -07001748 if (aps == 0) return PERMISSION_DENIED;
1749
Andy Hungacd955a2024-02-16 17:14:36 -08001750 std::lock_guard _l(gAPSMutex);
Eric Laurentb28753e2015-04-01 13:06:28 -07001751 if (gAudioPolicyServiceClient == 0) {
1752 return NO_INIT;
1753 }
Eric Laurente8726fe2015-06-26 09:39:24 -07001754 int ret = gAudioPolicyServiceClient->removeAudioPortCallback(callback);
1755 if (ret == 0) {
1756 aps->setAudioPortCallbacksEnabled(false);
1757 }
1758 return (ret < 0) ? INVALID_OPERATION : NO_ERROR;
Eric Laurent296fb132015-05-01 11:38:42 -07001759}
1760
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001761status_t AudioSystem::addAudioVolumeGroupCallback(const sp<AudioVolumeGroupCallback>& callback) {
Andy Hung264fa4c2024-02-21 15:52:12 -08001762 const sp<IAudioPolicyService> aps = get_audio_policy_service();
François Gaffiecfe17322018-11-07 13:41:29 +01001763 if (aps == 0) return PERMISSION_DENIED;
1764
Andy Hungacd955a2024-02-16 17:14:36 -08001765 std::lock_guard _l(gAPSMutex);
François Gaffiecfe17322018-11-07 13:41:29 +01001766 if (gAudioPolicyServiceClient == 0) {
1767 return NO_INIT;
1768 }
1769 int ret = gAudioPolicyServiceClient->addAudioVolumeGroupCallback(callback);
1770 if (ret == 1) {
1771 aps->setAudioVolumeGroupCallbacksEnabled(true);
1772 }
1773 return (ret < 0) ? INVALID_OPERATION : NO_ERROR;
1774}
1775
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001776status_t AudioSystem::removeAudioVolumeGroupCallback(const sp<AudioVolumeGroupCallback>& callback) {
Andy Hung264fa4c2024-02-21 15:52:12 -08001777 const sp<IAudioPolicyService> aps = get_audio_policy_service();
François Gaffiecfe17322018-11-07 13:41:29 +01001778 if (aps == 0) return PERMISSION_DENIED;
1779
Andy Hungacd955a2024-02-16 17:14:36 -08001780 std::lock_guard _l(gAPSMutex);
François Gaffiecfe17322018-11-07 13:41:29 +01001781 if (gAudioPolicyServiceClient == 0) {
1782 return NO_INIT;
1783 }
1784 int ret = gAudioPolicyServiceClient->removeAudioVolumeGroupCallback(callback);
1785 if (ret == 0) {
1786 aps->setAudioVolumeGroupCallbacksEnabled(false);
1787 }
1788 return (ret < 0) ? INVALID_OPERATION : NO_ERROR;
1789}
1790
Eric Laurent296fb132015-05-01 11:38:42 -07001791status_t AudioSystem::addAudioDeviceCallback(
Eric Laurent09f1ed22019-04-24 17:45:17 -07001792 const wp<AudioDeviceCallback>& callback, audio_io_handle_t audioIo,
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001793 audio_port_handle_t portId) {
Eric Laurent296fb132015-05-01 11:38:42 -07001794 const sp<AudioFlingerClient> afc = getAudioFlingerClient();
1795 if (afc == 0) {
1796 return NO_INIT;
1797 }
Eric Laurent09f1ed22019-04-24 17:45:17 -07001798 status_t status = afc->addAudioDeviceCallback(callback, audioIo, portId);
Eric Laurent7c1ec5f2015-07-09 14:52:47 -07001799 if (status == NO_ERROR) {
Andy Hung264fa4c2024-02-21 15:52:12 -08001800 const sp<IAudioFlinger> af = get_audio_flinger();
Eric Laurent7c1ec5f2015-07-09 14:52:47 -07001801 if (af != 0) {
1802 af->registerClient(afc);
1803 }
1804 }
1805 return status;
Eric Laurent296fb132015-05-01 11:38:42 -07001806}
1807
1808status_t AudioSystem::removeAudioDeviceCallback(
Eric Laurent09f1ed22019-04-24 17:45:17 -07001809 const wp<AudioDeviceCallback>& callback, audio_io_handle_t audioIo,
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001810 audio_port_handle_t portId) {
Eric Laurent296fb132015-05-01 11:38:42 -07001811 const sp<AudioFlingerClient> afc = getAudioFlingerClient();
1812 if (afc == 0) {
1813 return NO_INIT;
1814 }
Eric Laurent09f1ed22019-04-24 17:45:17 -07001815 return afc->removeAudioDeviceCallback(callback, audioIo, portId);
Eric Laurent296fb132015-05-01 11:38:42 -07001816}
1817
Eric Laurent076e7c72022-05-03 18:12:28 +02001818status_t AudioSystem::addSupportedLatencyModesCallback(
1819 const sp<SupportedLatencyModesCallback>& callback) {
1820 const sp<AudioFlingerClient> afc = getAudioFlingerClient();
1821 if (afc == 0) {
1822 return NO_INIT;
1823 }
1824 return afc->addSupportedLatencyModesCallback(callback);
1825}
1826
1827status_t AudioSystem::removeSupportedLatencyModesCallback(
1828 const sp<SupportedLatencyModesCallback>& callback) {
1829 const sp<AudioFlingerClient> afc = getAudioFlingerClient();
1830 if (afc == 0) {
1831 return NO_INIT;
1832 }
1833 return afc->removeSupportedLatencyModesCallback(callback);
1834}
1835
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001836audio_port_handle_t AudioSystem::getDeviceIdForIo(audio_io_handle_t audioIo) {
Andy Hung264fa4c2024-02-21 15:52:12 -08001837 const sp<IAudioFlinger> af = get_audio_flinger();
Eric Laurent296fb132015-05-01 11:38:42 -07001838 if (af == 0) return PERMISSION_DENIED;
1839 const sp<AudioIoDescriptor> desc = getIoDescriptor(audioIo);
1840 if (desc == 0) {
1841 return AUDIO_PORT_HANDLE_NONE;
1842 }
1843 return desc->getDeviceId();
Eric Laurentb28753e2015-04-01 13:06:28 -07001844}
1845
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001846status_t AudioSystem::acquireSoundTriggerSession(audio_session_t* session,
1847 audio_io_handle_t* ioHandle,
1848 audio_devices_t* device) {
1849 if (session == nullptr || ioHandle == nullptr || device == nullptr) {
1850 return BAD_VALUE;
1851 }
Andy Hung264fa4c2024-02-21 15:52:12 -08001852 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Eric Laurentdf3dc7e2014-07-27 18:39:40 -07001853 if (aps == 0) return PERMISSION_DENIED;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001854
1855 media::SoundTriggerSession retAidl;
1856 RETURN_STATUS_IF_ERROR(
1857 statusTFromBinderStatus(aps->acquireSoundTriggerSession(&retAidl)));
1858 *session = VALUE_OR_RETURN_STATUS(aidl2legacy_int32_t_audio_session_t(retAidl.session));
1859 *ioHandle = VALUE_OR_RETURN_STATUS(aidl2legacy_int32_t_audio_io_handle_t(retAidl.ioHandle));
Mikhail Naganov21a32ec2021-07-08 14:40:12 -07001860 *device = VALUE_OR_RETURN_STATUS(
1861 aidl2legacy_AudioDeviceDescription_audio_devices_t(retAidl.device));
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001862 return OK;
Eric Laurentdf3dc7e2014-07-27 18:39:40 -07001863}
1864
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001865status_t AudioSystem::releaseSoundTriggerSession(audio_session_t session) {
Andy Hung264fa4c2024-02-21 15:52:12 -08001866 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Eric Laurentdf3dc7e2014-07-27 18:39:40 -07001867 if (aps == 0) return PERMISSION_DENIED;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001868
1869 int32_t sessionAidl = VALUE_OR_RETURN_STATUS(legacy2aidl_audio_session_t_int32_t(session));
1870 return statusTFromBinderStatus(aps->releaseSoundTriggerSession(sessionAidl));
Eric Laurentdf3dc7e2014-07-27 18:39:40 -07001871}
Eric Laurentbb6c9a02014-09-25 14:11:47 -07001872
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001873audio_mode_t AudioSystem::getPhoneState() {
Andy Hung264fa4c2024-02-21 15:52:12 -08001874 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Eric Laurentbb6c9a02014-09-25 14:11:47 -07001875 if (aps == 0) return AUDIO_MODE_INVALID;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001876
1877 auto result = [&]() -> ConversionResult<audio_mode_t> {
Mikhail Naganovddceecc2021-09-03 13:58:56 -07001878 media::audio::common::AudioMode retAidl;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001879 RETURN_IF_ERROR(statusTFromBinderStatus(aps->getPhoneState(&retAidl)));
1880 return aidl2legacy_AudioMode_audio_mode_t(retAidl);
1881 }();
1882
1883 return result.value_or(AUDIO_MODE_INVALID);
Eric Laurentbb6c9a02014-09-25 14:11:47 -07001884}
1885
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001886status_t AudioSystem::registerPolicyMixes(const Vector<AudioMix>& mixes, bool registration) {
Andy Hung264fa4c2024-02-21 15:52:12 -08001887 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Eric Laurentbaac1832014-12-01 17:52:59 -08001888 if (aps == 0) return PERMISSION_DENIED;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001889
1890 size_t mixesSize = std::min(mixes.size(), size_t{MAX_MIXES_PER_POLICY});
1891 std::vector<media::AudioMix> mixesAidl;
1892 RETURN_STATUS_IF_ERROR(
1893 convertRange(mixes.begin(), mixes.begin() + mixesSize, std::back_inserter(mixesAidl),
1894 legacy2aidl_AudioMix));
1895 return statusTFromBinderStatus(aps->registerPolicyMixes(mixesAidl, registration));
Eric Laurentbaac1832014-12-01 17:52:59 -08001896}
Eric Laurentbb6c9a02014-09-25 14:11:47 -07001897
Marvin Raminbdefaf02023-11-01 09:10:32 +01001898status_t AudioSystem::getRegisteredPolicyMixes(std::vector<AudioMix>& mixes) {
1899 if (!audio_flags::audio_mix_test_api()) {
1900 return INVALID_OPERATION;
1901 }
1902
1903 const sp<IAudioPolicyService> aps = AudioSystem::get_audio_policy_service();
1904 if (aps == nullptr) return PERMISSION_DENIED;
1905
1906 std::vector<::android::media::AudioMix> aidlMixes;
1907 Status status = aps->getRegisteredPolicyMixes(&aidlMixes);
1908
1909 for (const auto& aidlMix : aidlMixes) {
1910 AudioMix mix = VALUE_OR_RETURN_STATUS(aidl2legacy_AudioMix(aidlMix));
1911 mixes.push_back(mix);
1912 }
1913
1914 return statusTFromBinderStatus(status);
1915}
1916
Jan Sebechlebsky0af8e872023-08-11 14:45:08 +02001917status_t AudioSystem::updatePolicyMixes(
1918 const std::vector<std::pair<AudioMix, std::vector<AudioMixMatchCriterion>>>&
1919 mixesWithUpdates) {
Andy Hung264fa4c2024-02-21 15:52:12 -08001920 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Jan Sebechlebsky0af8e872023-08-11 14:45:08 +02001921 if (aps == 0) return PERMISSION_DENIED;
1922
1923 std::vector<media::AudioMixUpdate> updatesAidl;
1924 updatesAidl.reserve(mixesWithUpdates.size());
1925
1926 for (const auto& update : mixesWithUpdates) {
1927 media::AudioMixUpdate updateAidl;
1928 updateAidl.audioMix = VALUE_OR_RETURN_STATUS(legacy2aidl_AudioMix(update.first));
1929 RETURN_STATUS_IF_ERROR(convertRange(update.second.begin(), update.second.end(),
1930 std::back_inserter(updateAidl.newCriteria),
1931 legacy2aidl_AudioMixMatchCriterion));
1932 updatesAidl.emplace_back(updateAidl);
1933 }
1934
1935 return statusTFromBinderStatus(aps->updatePolicyMixes(updatesAidl));
1936}
1937
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001938status_t AudioSystem::setUidDeviceAffinities(uid_t uid, const AudioDeviceTypeAddrVector& devices) {
Andy Hung264fa4c2024-02-21 15:52:12 -08001939 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Jean-Michel Trivibda70da2018-12-19 07:30:15 -08001940 if (aps == 0) return PERMISSION_DENIED;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001941
1942 int32_t uidAidl = VALUE_OR_RETURN_STATUS(legacy2aidl_uid_t_int32_t(uid));
Mikhail Naganovf4a75362021-09-16 00:02:54 +00001943 std::vector<AudioDevice> devicesAidl = VALUE_OR_RETURN_STATUS(
1944 convertContainer<std::vector<AudioDevice>>(devices,
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001945 legacy2aidl_AudioDeviceTypeAddress));
1946 return statusTFromBinderStatus(aps->setUidDeviceAffinities(uidAidl, devicesAidl));
Jean-Michel Trivibda70da2018-12-19 07:30:15 -08001947}
1948
1949status_t AudioSystem::removeUidDeviceAffinities(uid_t uid) {
Andy Hung264fa4c2024-02-21 15:52:12 -08001950 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Jean-Michel Trivibda70da2018-12-19 07:30:15 -08001951 if (aps == 0) return PERMISSION_DENIED;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001952
1953 int32_t uidAidl = VALUE_OR_RETURN_STATUS(legacy2aidl_uid_t_int32_t(uid));
1954 return statusTFromBinderStatus(aps->removeUidDeviceAffinities(uidAidl));
Jean-Michel Trivibda70da2018-12-19 07:30:15 -08001955}
1956
Oscar Azucena90e77632019-11-27 17:12:28 -08001957status_t AudioSystem::setUserIdDeviceAffinities(int userId,
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001958 const AudioDeviceTypeAddrVector& devices) {
Andy Hung264fa4c2024-02-21 15:52:12 -08001959 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Oscar Azucena90e77632019-11-27 17:12:28 -08001960 if (aps == 0) return PERMISSION_DENIED;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001961
1962 int32_t userIdAidl = VALUE_OR_RETURN_STATUS(convertReinterpret<int32_t>(userId));
Mikhail Naganovf4a75362021-09-16 00:02:54 +00001963 std::vector<AudioDevice> devicesAidl = VALUE_OR_RETURN_STATUS(
1964 convertContainer<std::vector<AudioDevice>>(devices,
1965 legacy2aidl_AudioDeviceTypeAddress));
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001966 return statusTFromBinderStatus(
1967 aps->setUserIdDeviceAffinities(userIdAidl, devicesAidl));
Oscar Azucena90e77632019-11-27 17:12:28 -08001968}
1969
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001970status_t AudioSystem::removeUserIdDeviceAffinities(int userId) {
Andy Hung264fa4c2024-02-21 15:52:12 -08001971 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Oscar Azucena90e77632019-11-27 17:12:28 -08001972 if (aps == 0) return PERMISSION_DENIED;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001973 int32_t userIdAidl = VALUE_OR_RETURN_STATUS(convertReinterpret<int32_t>(userId));
1974 return statusTFromBinderStatus(aps->removeUserIdDeviceAffinities(userIdAidl));
Oscar Azucena90e77632019-11-27 17:12:28 -08001975}
1976
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001977status_t AudioSystem::startAudioSource(const struct audio_port_config* source,
1978 const audio_attributes_t* attributes,
1979 audio_port_handle_t* portId) {
1980 if (source == nullptr || attributes == nullptr || portId == nullptr) {
1981 return BAD_VALUE;
1982 }
Andy Hung264fa4c2024-02-21 15:52:12 -08001983 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Eric Laurent554a2772015-04-10 11:29:24 -07001984 if (aps == 0) return PERMISSION_DENIED;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001985
Atneya Nair7a9594f2022-12-18 17:26:26 -08001986 media::AudioPortConfigFw sourceAidl = VALUE_OR_RETURN_STATUS(
Mikhail Naganov87227252023-01-13 17:38:10 -08001987 legacy2aidl_audio_port_config_AudioPortConfigFw(*source));
Mikhail Naganov1c400902023-05-17 11:48:43 -07001988 media::audio::common::AudioAttributes attributesAidl = VALUE_OR_RETURN_STATUS(
1989 legacy2aidl_audio_attributes_t_AudioAttributes(*attributes));
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001990 int32_t portIdAidl;
1991 RETURN_STATUS_IF_ERROR(statusTFromBinderStatus(
1992 aps->startAudioSource(sourceAidl, attributesAidl, &portIdAidl)));
1993 *portId = VALUE_OR_RETURN_STATUS(aidl2legacy_int32_t_audio_port_handle_t(portIdAidl));
1994 return OK;
Eric Laurent554a2772015-04-10 11:29:24 -07001995}
1996
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001997status_t AudioSystem::stopAudioSource(audio_port_handle_t portId) {
Andy Hung264fa4c2024-02-21 15:52:12 -08001998 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Eric Laurent554a2772015-04-10 11:29:24 -07001999 if (aps == 0) return PERMISSION_DENIED;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002000
2001 int32_t portIdAidl = VALUE_OR_RETURN_STATUS(legacy2aidl_audio_port_handle_t_int32_t(portId));
2002 return statusTFromBinderStatus(aps->stopAudioSource(portIdAidl));
Eric Laurent554a2772015-04-10 11:29:24 -07002003}
2004
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002005status_t AudioSystem::setMasterMono(bool mono) {
Andy Hung264fa4c2024-02-21 15:52:12 -08002006 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Andy Hung2ddee192015-12-18 17:34:44 -08002007 if (aps == 0) return PERMISSION_DENIED;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002008 return statusTFromBinderStatus(aps->setMasterMono(mono));
Andy Hung2ddee192015-12-18 17:34:44 -08002009}
2010
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002011status_t AudioSystem::getMasterMono(bool* mono) {
2012 if (mono == nullptr) {
2013 return BAD_VALUE;
2014 }
Andy Hung264fa4c2024-02-21 15:52:12 -08002015 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Andy Hung2ddee192015-12-18 17:34:44 -08002016 if (aps == 0) return PERMISSION_DENIED;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002017 return statusTFromBinderStatus(aps->getMasterMono(mono));
Andy Hung2ddee192015-12-18 17:34:44 -08002018}
2019
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002020status_t AudioSystem::setMasterBalance(float balance) {
Andy Hung264fa4c2024-02-21 15:52:12 -08002021 const sp<IAudioFlinger> af = get_audio_flinger();
Richard Folke Tullberg3fae0372017-01-13 09:04:25 +01002022 if (af == 0) return PERMISSION_DENIED;
2023 return af->setMasterBalance(balance);
2024}
2025
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002026status_t AudioSystem::getMasterBalance(float* balance) {
Andy Hung264fa4c2024-02-21 15:52:12 -08002027 const sp<IAudioFlinger> af = get_audio_flinger();
Richard Folke Tullberg3fae0372017-01-13 09:04:25 +01002028 if (af == 0) return PERMISSION_DENIED;
2029 return af->getMasterBalance(balance);
2030}
2031
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002032float
2033AudioSystem::getStreamVolumeDB(audio_stream_type_t stream, int index, audio_devices_t device) {
Andy Hung264fa4c2024-02-21 15:52:12 -08002034 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Eric Laurentac9cef52017-06-09 15:46:26 -07002035 if (aps == 0) return NAN;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002036
2037 auto result = [&]() -> ConversionResult<float> {
Mikhail Naganovdbf03642021-08-25 18:15:32 -07002038 AudioStreamType streamAidl = VALUE_OR_RETURN(
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002039 legacy2aidl_audio_stream_type_t_AudioStreamType(stream));
2040 int32_t indexAidl = VALUE_OR_RETURN(convertIntegral<int32_t>(index));
Mikhail Naganovf4a75362021-09-16 00:02:54 +00002041 AudioDeviceDescription deviceAidl = VALUE_OR_RETURN(
Mikhail Naganov21a32ec2021-07-08 14:40:12 -07002042 legacy2aidl_audio_devices_t_AudioDeviceDescription(device));
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002043 float retAidl;
2044 RETURN_IF_ERROR(statusTFromBinderStatus(
2045 aps->getStreamVolumeDB(streamAidl, indexAidl, deviceAidl, &retAidl)));
2046 return retAidl;
2047 }();
2048 return result.value_or(NAN);
Eric Laurentac9cef52017-06-09 15:46:26 -07002049}
2050
Mikhail Naganovd5d9de72023-02-13 11:45:03 -08002051status_t AudioSystem::getMicrophones(std::vector<media::MicrophoneInfoFw>* microphones) {
Andy Hung264fa4c2024-02-21 15:52:12 -08002052 const sp<IAudioFlinger> af = get_audio_flinger();
jiabin46a76fa2018-01-05 10:18:21 -08002053 if (af == 0) return PERMISSION_DENIED;
2054 return af->getMicrophones(microphones);
2055}
2056
Eric Laurent42896a02019-09-27 15:40:33 -07002057status_t AudioSystem::setAudioHalPids(const std::vector<pid_t>& pids) {
Andy Hung264fa4c2024-02-21 15:52:12 -08002058 const sp<IAudioFlinger> af = get_audio_flinger();
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002059 if (af == nullptr) return PERMISSION_DENIED;
2060 return af->setAudioHalPids(pids);
Eric Laurent42896a02019-09-27 15:40:33 -07002061}
2062
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002063status_t AudioSystem::getSurroundFormats(unsigned int* numSurroundFormats,
2064 audio_format_t* surroundFormats,
Kriti Dang6537def2021-03-02 13:46:59 +01002065 bool* surroundFormatsEnabled) {
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002066 if (numSurroundFormats == nullptr || (*numSurroundFormats != 0 &&
2067 (surroundFormats == nullptr ||
2068 surroundFormatsEnabled == nullptr))) {
2069 return BAD_VALUE;
2070 }
2071
Andy Hung264fa4c2024-02-21 15:52:12 -08002072 const sp<IAudioPolicyService> aps = get_audio_policy_service();
jiabin81772902018-04-02 17:52:27 -07002073 if (aps == 0) return PERMISSION_DENIED;
Mikhail Naganov0078ee52021-09-30 23:06:20 +00002074 Int numSurroundFormatsAidl;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002075 numSurroundFormatsAidl.value =
2076 VALUE_OR_RETURN_STATUS(convertIntegral<int32_t>(*numSurroundFormats));
Mikhail Naganov57bd06f2021-08-10 16:41:54 -07002077 std::vector<AudioFormatDescription> surroundFormatsAidl;
Kriti Dang877b27e2021-02-02 12:10:40 +01002078 std::vector<bool> surroundFormatsEnabledAidl;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002079 RETURN_STATUS_IF_ERROR(statusTFromBinderStatus(
Kriti Dang6537def2021-03-02 13:46:59 +01002080 aps->getSurroundFormats(&numSurroundFormatsAidl, &surroundFormatsAidl,
2081 &surroundFormatsEnabledAidl)));
Kriti Dang877b27e2021-02-02 12:10:40 +01002082
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002083 *numSurroundFormats = VALUE_OR_RETURN_STATUS(
2084 convertIntegral<unsigned int>(numSurroundFormatsAidl.value));
2085 RETURN_STATUS_IF_ERROR(
2086 convertRange(surroundFormatsAidl.begin(), surroundFormatsAidl.end(), surroundFormats,
Mikhail Naganovb60bd1b2021-07-15 17:31:43 -07002087 aidl2legacy_AudioFormatDescription_audio_format_t));
Kriti Dang877b27e2021-02-02 12:10:40 +01002088 std::copy(surroundFormatsEnabledAidl.begin(), surroundFormatsEnabledAidl.end(),
2089 surroundFormatsEnabled);
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002090 return OK;
jiabin81772902018-04-02 17:52:27 -07002091}
2092
Kriti Dang6537def2021-03-02 13:46:59 +01002093status_t AudioSystem::getReportedSurroundFormats(unsigned int* numSurroundFormats,
2094 audio_format_t* surroundFormats) {
2095 if (numSurroundFormats == nullptr || (*numSurroundFormats != 0 && surroundFormats == nullptr)) {
2096 return BAD_VALUE;
2097 }
2098
Andy Hung264fa4c2024-02-21 15:52:12 -08002099 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Kriti Dang6537def2021-03-02 13:46:59 +01002100 if (aps == 0) return PERMISSION_DENIED;
Mikhail Naganov0078ee52021-09-30 23:06:20 +00002101 Int numSurroundFormatsAidl;
Kriti Dang6537def2021-03-02 13:46:59 +01002102 numSurroundFormatsAidl.value =
2103 VALUE_OR_RETURN_STATUS(convertIntegral<int32_t>(*numSurroundFormats));
Mikhail Naganov57bd06f2021-08-10 16:41:54 -07002104 std::vector<AudioFormatDescription> surroundFormatsAidl;
Kriti Dang6537def2021-03-02 13:46:59 +01002105 RETURN_STATUS_IF_ERROR(statusTFromBinderStatus(
2106 aps->getReportedSurroundFormats(&numSurroundFormatsAidl, &surroundFormatsAidl)));
2107
2108 *numSurroundFormats = VALUE_OR_RETURN_STATUS(
2109 convertIntegral<unsigned int>(numSurroundFormatsAidl.value));
2110 RETURN_STATUS_IF_ERROR(
2111 convertRange(surroundFormatsAidl.begin(), surroundFormatsAidl.end(), surroundFormats,
Mikhail Naganovb60bd1b2021-07-15 17:31:43 -07002112 aidl2legacy_AudioFormatDescription_audio_format_t));
Kriti Dang6537def2021-03-02 13:46:59 +01002113 return OK;
2114}
2115
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002116status_t AudioSystem::setSurroundFormatEnabled(audio_format_t audioFormat, bool enabled) {
Andy Hung264fa4c2024-02-21 15:52:12 -08002117 const sp<IAudioPolicyService> aps = get_audio_policy_service();
jiabin81772902018-04-02 17:52:27 -07002118 if (aps == 0) return PERMISSION_DENIED;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002119
Mikhail Naganov57bd06f2021-08-10 16:41:54 -07002120 AudioFormatDescription audioFormatAidl = VALUE_OR_RETURN_STATUS(
Mikhail Naganovb60bd1b2021-07-15 17:31:43 -07002121 legacy2aidl_audio_format_t_AudioFormatDescription(audioFormat));
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002122 return statusTFromBinderStatus(
2123 aps->setSurroundFormatEnabled(audioFormatAidl, enabled));
jiabin81772902018-04-02 17:52:27 -07002124}
2125
Oscar Azucena829d90d2022-01-28 17:17:56 -08002126status_t AudioSystem::setAssistantServicesUids(const std::vector<uid_t>& uids) {
Andy Hung264fa4c2024-02-21 15:52:12 -08002127 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Eric Laurentb78763e2018-10-17 10:08:02 -07002128 if (aps == 0) return PERMISSION_DENIED;
2129
Oscar Azucena829d90d2022-01-28 17:17:56 -08002130 std::vector<int32_t> uidsAidl = VALUE_OR_RETURN_STATUS(
2131 convertContainer<std::vector<int32_t>>(uids, legacy2aidl_uid_t_int32_t));
2132 return statusTFromBinderStatus(aps->setAssistantServicesUids(uidsAidl));
Ahaan Ugalef51ce002021-08-04 16:34:20 -07002133}
2134
Oscar Azucenac2cdda32022-01-31 19:10:39 -08002135status_t AudioSystem::setActiveAssistantServicesUids(const std::vector<uid_t>& activeUids) {
Andy Hung264fa4c2024-02-21 15:52:12 -08002136 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Oscar Azucenac2cdda32022-01-31 19:10:39 -08002137 if (aps == 0) return PERMISSION_DENIED;
2138
2139 std::vector<int32_t> activeUidsAidl = VALUE_OR_RETURN_STATUS(
2140 convertContainer<std::vector<int32_t>>(activeUids, legacy2aidl_uid_t_int32_t));
2141 return statusTFromBinderStatus(aps->setActiveAssistantServicesUids(activeUidsAidl));
2142}
2143
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002144status_t AudioSystem::setA11yServicesUids(const std::vector<uid_t>& uids) {
Andy Hung264fa4c2024-02-21 15:52:12 -08002145 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Eric Laurentb78763e2018-10-17 10:08:02 -07002146 if (aps == 0) return PERMISSION_DENIED;
2147
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002148 std::vector<int32_t> uidsAidl = VALUE_OR_RETURN_STATUS(
2149 convertContainer<std::vector<int32_t>>(uids, legacy2aidl_uid_t_int32_t));
2150 return statusTFromBinderStatus(aps->setA11yServicesUids(uidsAidl));
Eric Laurentb78763e2018-10-17 10:08:02 -07002151}
2152
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002153status_t AudioSystem::setCurrentImeUid(uid_t uid) {
Andy Hung264fa4c2024-02-21 15:52:12 -08002154 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Kohsuke Yatoha623a132020-03-24 20:10:26 -07002155 if (aps == 0) return PERMISSION_DENIED;
2156
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002157 int32_t uidAidl = VALUE_OR_RETURN_STATUS(legacy2aidl_uid_t_int32_t(uid));
2158 return statusTFromBinderStatus(aps->setCurrentImeUid(uidAidl));
Kohsuke Yatoha623a132020-03-24 20:10:26 -07002159}
2160
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002161bool AudioSystem::isHapticPlaybackSupported() {
Andy Hung264fa4c2024-02-21 15:52:12 -08002162 const sp<IAudioPolicyService> aps = get_audio_policy_service();
jiabin6012f912018-11-02 17:06:30 -07002163 if (aps == 0) return false;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002164
2165 auto result = [&]() -> ConversionResult<bool> {
2166 bool retVal;
2167 RETURN_IF_ERROR(
2168 statusTFromBinderStatus(aps->isHapticPlaybackSupported(&retVal)));
2169 return retVal;
2170 }();
2171 return result.value_or(false);
jiabin6012f912018-11-02 17:06:30 -07002172}
2173
Carter Hsu325a8eb2022-01-19 19:56:51 +08002174bool AudioSystem::isUltrasoundSupported() {
Andy Hung264fa4c2024-02-21 15:52:12 -08002175 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Carter Hsu325a8eb2022-01-19 19:56:51 +08002176 if (aps == 0) return false;
2177
2178 auto result = [&]() -> ConversionResult<bool> {
2179 bool retVal;
2180 RETURN_IF_ERROR(
2181 statusTFromBinderStatus(aps->isUltrasoundSupported(&retVal)));
2182 return retVal;
2183 }();
2184 return result.value_or(false);
2185}
2186
Pattydd807582021-11-04 21:01:03 +08002187status_t AudioSystem::getHwOffloadFormatsSupportedForBluetoothMedia(
2188 audio_devices_t device, std::vector<audio_format_t>* formats) {
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002189 if (formats == nullptr) {
2190 return BAD_VALUE;
2191 }
2192
Andy Hung264fa4c2024-02-21 15:52:12 -08002193 const sp<IAudioPolicyService> aps = get_audio_policy_service();
François Gaffied0ba9ed2018-11-05 11:50:42 +01002194 if (aps == 0) return PERMISSION_DENIED;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002195
Mikhail Naganov57bd06f2021-08-10 16:41:54 -07002196 std::vector<AudioFormatDescription> formatsAidl;
Pattydd807582021-11-04 21:01:03 +08002197 AudioDeviceDescription deviceAidl = VALUE_OR_RETURN_STATUS(
2198 legacy2aidl_audio_devices_t_AudioDeviceDescription(device));
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002199 RETURN_STATUS_IF_ERROR(statusTFromBinderStatus(
Pattydd807582021-11-04 21:01:03 +08002200 aps->getHwOffloadFormatsSupportedForBluetoothMedia(deviceAidl, &formatsAidl)));
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002201 *formats = VALUE_OR_RETURN_STATUS(
Mikhail Naganovb60bd1b2021-07-15 17:31:43 -07002202 convertContainer<std::vector<audio_format_t>>(
2203 formatsAidl,
2204 aidl2legacy_AudioFormatDescription_audio_format_t));
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002205 return OK;
François Gaffied0ba9ed2018-11-05 11:50:42 +01002206}
2207
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002208status_t AudioSystem::listAudioProductStrategies(AudioProductStrategyVector& strategies) {
Andy Hung264fa4c2024-02-21 15:52:12 -08002209 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Arun Mirpuri11029ad2018-12-19 20:45:19 -08002210 if (aps == 0) return PERMISSION_DENIED;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002211
2212 std::vector<media::AudioProductStrategy> strategiesAidl;
2213 RETURN_STATUS_IF_ERROR(statusTFromBinderStatus(
2214 aps->listAudioProductStrategies(&strategiesAidl)));
2215 strategies = VALUE_OR_RETURN_STATUS(
2216 convertContainer<AudioProductStrategyVector>(strategiesAidl,
2217 aidl2legacy_AudioProductStrategy));
2218 return OK;
François Gaffied0ba9ed2018-11-05 11:50:42 +01002219}
2220
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002221audio_attributes_t AudioSystem::streamTypeToAttributes(audio_stream_type_t stream) {
François Gaffied0ba9ed2018-11-05 11:50:42 +01002222 AudioProductStrategyVector strategies;
2223 listAudioProductStrategies(strategies);
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002224 for (const auto& strategy : strategies) {
François Gaffie1e2b56f2022-04-01 14:34:29 +02002225 auto attrVect = strategy.getVolumeGroupAttributes();
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002226 auto iter = std::find_if(begin(attrVect), end(attrVect), [&stream](const auto& attributes) {
2227 return attributes.getStreamType() == stream;
2228 });
François Gaffied0ba9ed2018-11-05 11:50:42 +01002229 if (iter != end(attrVect)) {
2230 return iter->getAttributes();
2231 }
2232 }
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002233 ALOGE("invalid stream type %s when converting to attributes", toString(stream).c_str());
François Gaffied0ba9ed2018-11-05 11:50:42 +01002234 return AUDIO_ATTRIBUTES_INITIALIZER;
2235}
2236
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002237audio_stream_type_t AudioSystem::attributesToStreamType(const audio_attributes_t& attr) {
François Gaffie4b2018b2018-11-07 11:18:59 +01002238 product_strategy_t psId;
François Gaffie1e2b56f2022-04-01 14:34:29 +02002239 status_t ret = AudioSystem::getProductStrategyFromAudioAttributes(attr, psId);
François Gaffie4b2018b2018-11-07 11:18:59 +01002240 if (ret != NO_ERROR) {
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002241 ALOGE("no strategy found for attributes %s", toString(attr).c_str());
François Gaffie4b2018b2018-11-07 11:18:59 +01002242 return AUDIO_STREAM_MUSIC;
2243 }
François Gaffied0ba9ed2018-11-05 11:50:42 +01002244 AudioProductStrategyVector strategies;
2245 listAudioProductStrategies(strategies);
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002246 for (const auto& strategy : strategies) {
François Gaffie4b2018b2018-11-07 11:18:59 +01002247 if (strategy.getId() == psId) {
François Gaffie1e2b56f2022-04-01 14:34:29 +02002248 auto attrVect = strategy.getVolumeGroupAttributes();
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002249 auto iter = std::find_if(begin(attrVect), end(attrVect), [&attr](const auto& refAttr) {
François Gaffie8d7fd5a2023-01-17 17:28:09 +01002250 return refAttr.matchesScore(attr) > 0;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002251 });
François Gaffied0ba9ed2018-11-05 11:50:42 +01002252 if (iter != end(attrVect)) {
2253 return iter->getStreamType();
2254 }
2255 }
2256 }
Jean-Michel Trivied678652019-12-19 13:39:30 -08002257 switch (attr.usage) {
2258 case AUDIO_USAGE_VIRTUAL_SOURCE:
2259 // virtual source is not expected to have an associated product strategy
2260 break;
2261 default:
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002262 ALOGE("invalid attributes %s when converting to stream", toString(attr).c_str());
Jean-Michel Trivied678652019-12-19 13:39:30 -08002263 break;
2264 }
François Gaffied0ba9ed2018-11-05 11:50:42 +01002265 return AUDIO_STREAM_MUSIC;
2266}
2267
François Gaffie1e2b56f2022-04-01 14:34:29 +02002268status_t AudioSystem::getProductStrategyFromAudioAttributes(const audio_attributes_t& aa,
Francois Gaffie11b65922020-09-24 16:59:08 +02002269 product_strategy_t& productStrategy,
2270 bool fallbackOnDefault) {
Andy Hung264fa4c2024-02-21 15:52:12 -08002271 const sp<IAudioPolicyService> aps = get_audio_policy_service();
François Gaffie4b2018b2018-11-07 11:18:59 +01002272 if (aps == 0) return PERMISSION_DENIED;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002273
Mikhail Naganov1c400902023-05-17 11:48:43 -07002274 media::audio::common::AudioAttributes aaAidl = VALUE_OR_RETURN_STATUS(
2275 legacy2aidl_audio_attributes_t_AudioAttributes(aa));
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002276 int32_t productStrategyAidl;
2277
2278 RETURN_STATUS_IF_ERROR(statusTFromBinderStatus(
Francois Gaffie11b65922020-09-24 16:59:08 +02002279 aps->getProductStrategyFromAudioAttributes(aaAidl, fallbackOnDefault,
2280 &productStrategyAidl)));
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002281 productStrategy = VALUE_OR_RETURN_STATUS(
2282 aidl2legacy_int32_t_product_strategy_t(productStrategyAidl));
2283 return OK;
François Gaffie4b2018b2018-11-07 11:18:59 +01002284}
2285
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002286status_t AudioSystem::listAudioVolumeGroups(AudioVolumeGroupVector& groups) {
Andy Hung264fa4c2024-02-21 15:52:12 -08002287 const sp<IAudioPolicyService> aps = get_audio_policy_service();
François Gaffie4b2018b2018-11-07 11:18:59 +01002288 if (aps == 0) return PERMISSION_DENIED;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002289
2290 std::vector<media::AudioVolumeGroup> groupsAidl;
2291 RETURN_STATUS_IF_ERROR(
2292 statusTFromBinderStatus(aps->listAudioVolumeGroups(&groupsAidl)));
2293 groups = VALUE_OR_RETURN_STATUS(
2294 convertContainer<AudioVolumeGroupVector>(groupsAidl, aidl2legacy_AudioVolumeGroup));
2295 return OK;
François Gaffie4b2018b2018-11-07 11:18:59 +01002296}
2297
François Gaffie1e2b56f2022-04-01 14:34:29 +02002298status_t AudioSystem::getVolumeGroupFromAudioAttributes(const audio_attributes_t &aa,
Francois Gaffie11b65922020-09-24 16:59:08 +02002299 volume_group_t& volumeGroup,
2300 bool fallbackOnDefault) {
Andy Hung264fa4c2024-02-21 15:52:12 -08002301 const sp<IAudioPolicyService> aps = get_audio_policy_service();
François Gaffie4b2018b2018-11-07 11:18:59 +01002302 if (aps == 0) return PERMISSION_DENIED;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002303
Mikhail Naganov1c400902023-05-17 11:48:43 -07002304 media::audio::common::AudioAttributes aaAidl = VALUE_OR_RETURN_STATUS(
2305 legacy2aidl_audio_attributes_t_AudioAttributes(aa));
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002306 int32_t volumeGroupAidl;
2307 RETURN_STATUS_IF_ERROR(statusTFromBinderStatus(
Francois Gaffie11b65922020-09-24 16:59:08 +02002308 aps->getVolumeGroupFromAudioAttributes(aaAidl, fallbackOnDefault, &volumeGroupAidl)));
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002309 volumeGroup = VALUE_OR_RETURN_STATUS(aidl2legacy_int32_t_volume_group_t(volumeGroupAidl));
2310 return OK;
Arun Mirpuri11029ad2018-12-19 20:45:19 -08002311}
Eric Laurentb78763e2018-10-17 10:08:02 -07002312
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002313status_t AudioSystem::setRttEnabled(bool enabled) {
Andy Hung264fa4c2024-02-21 15:52:12 -08002314 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Eric Laurent6ede98f2019-06-11 14:50:30 -07002315 if (aps == 0) return PERMISSION_DENIED;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002316 return statusTFromBinderStatus(aps->setRttEnabled(enabled));
Eric Laurent6ede98f2019-06-11 14:50:30 -07002317}
2318
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002319bool AudioSystem::isCallScreenModeSupported() {
Andy Hung264fa4c2024-02-21 15:52:12 -08002320 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Eric Laurent8340e672019-11-06 11:01:08 -08002321 if (aps == 0) return false;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002322
2323 auto result = [&]() -> ConversionResult<bool> {
2324 bool retAidl;
2325 RETURN_IF_ERROR(
2326 statusTFromBinderStatus(aps->isCallScreenModeSupported(&retAidl)));
2327 return retAidl;
2328 }();
2329 return result.value_or(false);
Eric Laurent8340e672019-11-06 11:01:08 -08002330}
2331
jiabin0a488932020-08-07 17:32:40 -07002332status_t AudioSystem::setDevicesRoleForStrategy(product_strategy_t strategy,
2333 device_role_t role,
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002334 const AudioDeviceTypeAddrVector& devices) {
Andy Hung264fa4c2024-02-21 15:52:12 -08002335 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Jean-Michel Trivi30857152019-11-01 11:04:15 -07002336 if (aps == 0) {
2337 return PERMISSION_DENIED;
2338 }
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002339
2340 int32_t strategyAidl = VALUE_OR_RETURN_STATUS(legacy2aidl_product_strategy_t_int32_t(strategy));
2341 media::DeviceRole roleAidl = VALUE_OR_RETURN_STATUS(legacy2aidl_device_role_t_DeviceRole(role));
Mikhail Naganovf4a75362021-09-16 00:02:54 +00002342 std::vector<AudioDevice> devicesAidl = VALUE_OR_RETURN_STATUS(
2343 convertContainer<std::vector<AudioDevice>>(devices,
2344 legacy2aidl_AudioDeviceTypeAddress));
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002345 return statusTFromBinderStatus(
2346 aps->setDevicesRoleForStrategy(strategyAidl, roleAidl, devicesAidl));
Jean-Michel Trivi30857152019-11-01 11:04:15 -07002347}
2348
Paul Wang5d7cdb52022-11-22 09:45:06 +00002349status_t AudioSystem::removeDevicesRoleForStrategy(product_strategy_t strategy,
2350 device_role_t role,
2351 const AudioDeviceTypeAddrVector& devices) {
Andy Hung264fa4c2024-02-21 15:52:12 -08002352 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Paul Wang5d7cdb52022-11-22 09:45:06 +00002353 if (aps == 0) {
2354 return PERMISSION_DENIED;
2355 }
2356
2357 int32_t strategyAidl = VALUE_OR_RETURN_STATUS(legacy2aidl_product_strategy_t_int32_t(strategy));
2358 media::DeviceRole roleAidl = VALUE_OR_RETURN_STATUS(legacy2aidl_device_role_t_DeviceRole(role));
2359 std::vector<AudioDevice> devicesAidl = VALUE_OR_RETURN_STATUS(
2360 convertContainer<std::vector<AudioDevice>>(devices,
2361 legacy2aidl_AudioDeviceTypeAddress));
2362 return statusTFromBinderStatus(
2363 aps->removeDevicesRoleForStrategy(strategyAidl, roleAidl, devicesAidl));
2364}
2365
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002366status_t
Paul Wang5d7cdb52022-11-22 09:45:06 +00002367AudioSystem::clearDevicesRoleForStrategy(product_strategy_t strategy, device_role_t role) {
Andy Hung264fa4c2024-02-21 15:52:12 -08002368 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Jean-Michel Trivi30857152019-11-01 11:04:15 -07002369 if (aps == 0) {
2370 return PERMISSION_DENIED;
2371 }
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002372 int32_t strategyAidl = VALUE_OR_RETURN_STATUS(legacy2aidl_product_strategy_t_int32_t(strategy));
2373 media::DeviceRole roleAidl = VALUE_OR_RETURN_STATUS(legacy2aidl_device_role_t_DeviceRole(role));
2374 return statusTFromBinderStatus(
Paul Wang5d7cdb52022-11-22 09:45:06 +00002375 aps->clearDevicesRoleForStrategy(strategyAidl, roleAidl));
Jean-Michel Trivi30857152019-11-01 11:04:15 -07002376}
2377
jiabin0a488932020-08-07 17:32:40 -07002378status_t AudioSystem::getDevicesForRoleAndStrategy(product_strategy_t strategy,
2379 device_role_t role,
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002380 AudioDeviceTypeAddrVector& devices) {
Andy Hung264fa4c2024-02-21 15:52:12 -08002381 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Jean-Michel Trivi30857152019-11-01 11:04:15 -07002382 if (aps == 0) {
2383 return PERMISSION_DENIED;
2384 }
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002385 int32_t strategyAidl = VALUE_OR_RETURN_STATUS(legacy2aidl_product_strategy_t_int32_t(strategy));
2386 media::DeviceRole roleAidl = VALUE_OR_RETURN_STATUS(legacy2aidl_device_role_t_DeviceRole(role));
Mikhail Naganovf4a75362021-09-16 00:02:54 +00002387 std::vector<AudioDevice> devicesAidl;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002388 RETURN_STATUS_IF_ERROR(statusTFromBinderStatus(
2389 aps->getDevicesForRoleAndStrategy(strategyAidl, roleAidl, &devicesAidl)));
2390 devices = VALUE_OR_RETURN_STATUS(
2391 convertContainer<AudioDeviceTypeAddrVector>(devicesAidl,
2392 aidl2legacy_AudioDeviceTypeAddress));
2393 return OK;
Jean-Michel Trivi30857152019-11-01 11:04:15 -07002394}
2395
Jiabin Huang3b98d322020-09-03 17:54:16 +00002396status_t AudioSystem::setDevicesRoleForCapturePreset(audio_source_t audioSource,
2397 device_role_t role,
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002398 const AudioDeviceTypeAddrVector& devices) {
Andy Hung264fa4c2024-02-21 15:52:12 -08002399 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Jiabin Huang3b98d322020-09-03 17:54:16 +00002400 if (aps == 0) {
2401 return PERMISSION_DENIED;
2402 }
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002403
Mikhail Naganovddceecc2021-09-03 13:58:56 -07002404 AudioSource audioSourceAidl = VALUE_OR_RETURN_STATUS(
2405 legacy2aidl_audio_source_t_AudioSource(audioSource));
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002406 media::DeviceRole roleAidl = VALUE_OR_RETURN_STATUS(legacy2aidl_device_role_t_DeviceRole(role));
Mikhail Naganovf4a75362021-09-16 00:02:54 +00002407 std::vector<AudioDevice> devicesAidl = VALUE_OR_RETURN_STATUS(
2408 convertContainer<std::vector<AudioDevice>>(devices,
2409 legacy2aidl_AudioDeviceTypeAddress));
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002410 return statusTFromBinderStatus(
2411 aps->setDevicesRoleForCapturePreset(audioSourceAidl, roleAidl, devicesAidl));
Jiabin Huang3b98d322020-09-03 17:54:16 +00002412}
2413
2414status_t AudioSystem::addDevicesRoleForCapturePreset(audio_source_t audioSource,
2415 device_role_t role,
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002416 const AudioDeviceTypeAddrVector& devices) {
Andy Hung264fa4c2024-02-21 15:52:12 -08002417 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Jiabin Huang3b98d322020-09-03 17:54:16 +00002418 if (aps == 0) {
2419 return PERMISSION_DENIED;
2420 }
Mikhail Naganovddceecc2021-09-03 13:58:56 -07002421 AudioSource audioSourceAidl = VALUE_OR_RETURN_STATUS(
2422 legacy2aidl_audio_source_t_AudioSource(audioSource));
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002423 media::DeviceRole roleAidl = VALUE_OR_RETURN_STATUS(legacy2aidl_device_role_t_DeviceRole(role));
Mikhail Naganovf4a75362021-09-16 00:02:54 +00002424 std::vector<AudioDevice> devicesAidl = VALUE_OR_RETURN_STATUS(
2425 convertContainer<std::vector<AudioDevice>>(devices,
2426 legacy2aidl_AudioDeviceTypeAddress));
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002427 return statusTFromBinderStatus(
2428 aps->addDevicesRoleForCapturePreset(audioSourceAidl, roleAidl, devicesAidl));
Jiabin Huang3b98d322020-09-03 17:54:16 +00002429}
2430
2431status_t AudioSystem::removeDevicesRoleForCapturePreset(
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002432 audio_source_t audioSource, device_role_t role, const AudioDeviceTypeAddrVector& devices) {
Andy Hung264fa4c2024-02-21 15:52:12 -08002433 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Jiabin Huang3b98d322020-09-03 17:54:16 +00002434 if (aps == 0) {
2435 return PERMISSION_DENIED;
2436 }
Mikhail Naganovddceecc2021-09-03 13:58:56 -07002437 AudioSource audioSourceAidl = VALUE_OR_RETURN_STATUS(
2438 legacy2aidl_audio_source_t_AudioSource(audioSource));
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002439 media::DeviceRole roleAidl = VALUE_OR_RETURN_STATUS(legacy2aidl_device_role_t_DeviceRole(role));
Mikhail Naganovf4a75362021-09-16 00:02:54 +00002440 std::vector<AudioDevice> devicesAidl = VALUE_OR_RETURN_STATUS(
2441 convertContainer<std::vector<AudioDevice>>(devices,
2442 legacy2aidl_AudioDeviceTypeAddress));
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002443 return statusTFromBinderStatus(
2444 aps->removeDevicesRoleForCapturePreset(audioSourceAidl, roleAidl, devicesAidl));
Jiabin Huang3b98d322020-09-03 17:54:16 +00002445}
2446
2447status_t AudioSystem::clearDevicesRoleForCapturePreset(audio_source_t audioSource,
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002448 device_role_t role) {
Andy Hung264fa4c2024-02-21 15:52:12 -08002449 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Jiabin Huang3b98d322020-09-03 17:54:16 +00002450 if (aps == 0) {
2451 return PERMISSION_DENIED;
2452 }
Mikhail Naganovddceecc2021-09-03 13:58:56 -07002453 AudioSource audioSourceAidl = VALUE_OR_RETURN_STATUS(
2454 legacy2aidl_audio_source_t_AudioSource(audioSource));
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002455 media::DeviceRole roleAidl = VALUE_OR_RETURN_STATUS(legacy2aidl_device_role_t_DeviceRole(role));
2456 return statusTFromBinderStatus(
2457 aps->clearDevicesRoleForCapturePreset(audioSourceAidl, roleAidl));
Jiabin Huang3b98d322020-09-03 17:54:16 +00002458}
2459
2460status_t AudioSystem::getDevicesForRoleAndCapturePreset(audio_source_t audioSource,
2461 device_role_t role,
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002462 AudioDeviceTypeAddrVector& devices) {
Andy Hung264fa4c2024-02-21 15:52:12 -08002463 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Jiabin Huang3b98d322020-09-03 17:54:16 +00002464 if (aps == 0) {
2465 return PERMISSION_DENIED;
2466 }
Mikhail Naganovddceecc2021-09-03 13:58:56 -07002467 AudioSource audioSourceAidl = VALUE_OR_RETURN_STATUS(
2468 legacy2aidl_audio_source_t_AudioSource(audioSource));
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002469 media::DeviceRole roleAidl = VALUE_OR_RETURN_STATUS(legacy2aidl_device_role_t_DeviceRole(role));
Mikhail Naganovf4a75362021-09-16 00:02:54 +00002470 std::vector<AudioDevice> devicesAidl;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002471 RETURN_STATUS_IF_ERROR(statusTFromBinderStatus(
2472 aps->getDevicesForRoleAndCapturePreset(audioSourceAidl, roleAidl, &devicesAidl)));
2473 devices = VALUE_OR_RETURN_STATUS(
2474 convertContainer<AudioDeviceTypeAddrVector>(devicesAidl,
2475 aidl2legacy_AudioDeviceTypeAddress));
2476 return OK;
Jiabin Huang3b98d322020-09-03 17:54:16 +00002477}
2478
Eric Laurent81dd0f52021-07-05 11:54:40 +02002479status_t AudioSystem::getSpatializer(const sp<media::INativeSpatializerCallback>& callback,
2480 sp<media::ISpatializer>* spatializer) {
Andy Hung264fa4c2024-02-21 15:52:12 -08002481 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Eric Laurent81dd0f52021-07-05 11:54:40 +02002482 if (spatializer == nullptr) {
2483 return BAD_VALUE;
2484 }
2485 if (aps == 0) {
2486 return PERMISSION_DENIED;
2487 }
2488 media::GetSpatializerResponse response;
2489 RETURN_STATUS_IF_ERROR(statusTFromBinderStatus(
2490 aps->getSpatializer(callback, &response)));
2491
2492 *spatializer = response.spatializer;
2493 return OK;
2494}
2495
2496status_t AudioSystem::canBeSpatialized(const audio_attributes_t *attr,
2497 const audio_config_t *config,
2498 const AudioDeviceTypeAddrVector &devices,
2499 bool *canBeSpatialized) {
Andy Hung264fa4c2024-02-21 15:52:12 -08002500 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Ram Mohan43297012022-08-30 16:11:49 +05302501 if (canBeSpatialized == nullptr) {
2502 return BAD_VALUE;
2503 }
Eric Laurent81dd0f52021-07-05 11:54:40 +02002504 if (aps == 0) {
2505 return PERMISSION_DENIED;
2506 }
2507 audio_attributes_t attributes = attr != nullptr ? *attr : AUDIO_ATTRIBUTES_INITIALIZER;
2508 audio_config_t configuration = config != nullptr ? *config : AUDIO_CONFIG_INITIALIZER;
2509
Mikhail Naganov1c400902023-05-17 11:48:43 -07002510 std::optional<media::audio::common::AudioAttributes> attrAidl = VALUE_OR_RETURN_STATUS(
2511 legacy2aidl_audio_attributes_t_AudioAttributes(attributes));
Mikhail Naganovdbf03642021-08-25 18:15:32 -07002512 std::optional<AudioConfig> configAidl = VALUE_OR_RETURN_STATUS(
Eric Laurent81dd0f52021-07-05 11:54:40 +02002513 legacy2aidl_audio_config_t_AudioConfig(configuration, false /*isInput*/));
Mikhail Naganovf4a75362021-09-16 00:02:54 +00002514 std::vector<AudioDevice> devicesAidl = VALUE_OR_RETURN_STATUS(
2515 convertContainer<std::vector<AudioDevice>>(devices,
2516 legacy2aidl_AudioDeviceTypeAddress));
Eric Laurent81dd0f52021-07-05 11:54:40 +02002517 RETURN_STATUS_IF_ERROR(statusTFromBinderStatus(
2518 aps->canBeSpatialized(attrAidl, configAidl, devicesAidl, canBeSpatialized)));
2519 return OK;
2520}
2521
Vlad Popae3fd1c22022-11-07 21:03:18 +01002522status_t AudioSystem::getSoundDoseInterface(const sp<media::ISoundDoseCallback>& callback,
2523 sp<media::ISoundDose>* soundDose) {
Andy Hung264fa4c2024-02-21 15:52:12 -08002524 const sp<IAudioFlinger> af = get_audio_flinger();
Vlad Popa63f047e2022-11-05 14:09:19 +01002525 if (af == nullptr) {
2526 return PERMISSION_DENIED;
2527 }
Vlad Popae3fd1c22022-11-07 21:03:18 +01002528 if (soundDose == nullptr) {
2529 return BAD_VALUE;
2530 }
Vlad Popa63f047e2022-11-05 14:09:19 +01002531
Vlad Popae3fd1c22022-11-07 21:03:18 +01002532 RETURN_STATUS_IF_ERROR(af->getSoundDoseInterface(callback, soundDose));
2533 return OK;
Vlad Popa63f047e2022-11-05 14:09:19 +01002534}
2535
jiabin2b9d5a12021-12-10 01:06:29 +00002536status_t AudioSystem::getDirectPlaybackSupport(const audio_attributes_t *attr,
2537 const audio_config_t *config,
2538 audio_direct_mode_t* directMode) {
2539 if (attr == nullptr || config == nullptr || directMode == nullptr) {
2540 return BAD_VALUE;
2541 }
2542
Andy Hung264fa4c2024-02-21 15:52:12 -08002543 const sp<IAudioPolicyService> aps = get_audio_policy_service();
jiabin2b9d5a12021-12-10 01:06:29 +00002544 if (aps == 0) {
2545 return PERMISSION_DENIED;
2546 }
2547
Mikhail Naganov1c400902023-05-17 11:48:43 -07002548 media::audio::common::AudioAttributes attrAidl = VALUE_OR_RETURN_STATUS(
2549 legacy2aidl_audio_attributes_t_AudioAttributes(*attr));
jiabin2b9d5a12021-12-10 01:06:29 +00002550 AudioConfig configAidl = VALUE_OR_RETURN_STATUS(
2551 legacy2aidl_audio_config_t_AudioConfig(*config, false /*isInput*/));
2552
2553 media::AudioDirectMode retAidl;
2554 RETURN_STATUS_IF_ERROR(statusTFromBinderStatus(
2555 aps->getDirectPlaybackSupport(attrAidl, configAidl, &retAidl)));
2556 *directMode = VALUE_OR_RETURN_STATUS(aidl2legacy_int32_t_audio_direct_mode_t_mask(
2557 static_cast<int32_t>(retAidl)));
2558 return NO_ERROR;
2559}
2560
Dorin Drimusf2196d82022-01-03 12:11:18 +01002561status_t AudioSystem::getDirectProfilesForAttributes(const audio_attributes_t* attr,
2562 std::vector<audio_profile>* audioProfiles) {
Mikhail Naganovefc504b2022-06-22 03:24:59 +00002563 if (attr == nullptr || audioProfiles == nullptr) {
Dorin Drimusf2196d82022-01-03 12:11:18 +01002564 return BAD_VALUE;
2565 }
2566
Andy Hung264fa4c2024-02-21 15:52:12 -08002567 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Dorin Drimusf2196d82022-01-03 12:11:18 +01002568 if (aps == 0) {
2569 return PERMISSION_DENIED;
2570 }
2571
Mikhail Naganov1c400902023-05-17 11:48:43 -07002572 media::audio::common::AudioAttributes attrAidl = VALUE_OR_RETURN_STATUS(
2573 legacy2aidl_audio_attributes_t_AudioAttributes(*attr));
Dorin Drimusf2196d82022-01-03 12:11:18 +01002574
2575 std::vector<media::audio::common::AudioProfile> audioProfilesAidl;
2576 RETURN_STATUS_IF_ERROR(statusTFromBinderStatus(
2577 aps->getDirectProfilesForAttributes(attrAidl, &audioProfilesAidl)));
2578 *audioProfiles = VALUE_OR_RETURN_STATUS(convertContainer<std::vector<audio_profile>>(
2579 audioProfilesAidl, aidl2legacy_AudioProfile_audio_profile, false /*isInput*/));
2580
2581 return NO_ERROR;
2582}
Eric Laurent81dd0f52021-07-05 11:54:40 +02002583
Eric Laurent076e7c72022-05-03 18:12:28 +02002584status_t AudioSystem::setRequestedLatencyMode(
2585 audio_io_handle_t output, audio_latency_mode_t mode) {
Andy Hung264fa4c2024-02-21 15:52:12 -08002586 const sp<IAudioFlinger> af = get_audio_flinger();
Eric Laurent076e7c72022-05-03 18:12:28 +02002587 if (af == nullptr) {
2588 return PERMISSION_DENIED;
2589 }
2590 return af->setRequestedLatencyMode(output, mode);
2591}
2592
2593status_t AudioSystem::getSupportedLatencyModes(audio_io_handle_t output,
2594 std::vector<audio_latency_mode_t>* modes) {
Andy Hung264fa4c2024-02-21 15:52:12 -08002595 const sp<IAudioFlinger> af = get_audio_flinger();
Eric Laurent076e7c72022-05-03 18:12:28 +02002596 if (af == nullptr) {
2597 return PERMISSION_DENIED;
2598 }
2599 return af->getSupportedLatencyModes(output, modes);
2600}
2601
Eric Laurent50d72582022-12-20 20:20:23 +01002602status_t AudioSystem::setBluetoothVariableLatencyEnabled(bool enabled) {
Andy Hung264fa4c2024-02-21 15:52:12 -08002603 const sp<IAudioFlinger> af = get_audio_flinger();
Eric Laurent52057642022-12-16 11:45:07 +01002604 if (af == nullptr) {
2605 return PERMISSION_DENIED;
2606 }
Eric Laurent50d72582022-12-20 20:20:23 +01002607 return af->setBluetoothVariableLatencyEnabled(enabled);
Eric Laurent52057642022-12-16 11:45:07 +01002608}
2609
Eric Laurent50d72582022-12-20 20:20:23 +01002610status_t AudioSystem::isBluetoothVariableLatencyEnabled(
2611 bool *enabled) {
Andy Hung264fa4c2024-02-21 15:52:12 -08002612 const sp<IAudioFlinger> af = get_audio_flinger();
Eric Laurent50d72582022-12-20 20:20:23 +01002613 if (af == nullptr) {
2614 return PERMISSION_DENIED;
2615 }
2616 return af->isBluetoothVariableLatencyEnabled(enabled);
2617}
2618
2619status_t AudioSystem::supportsBluetoothVariableLatency(
Eric Laurent52057642022-12-16 11:45:07 +01002620 bool *support) {
Andy Hung264fa4c2024-02-21 15:52:12 -08002621 const sp<IAudioFlinger> af = get_audio_flinger();
Eric Laurent52057642022-12-16 11:45:07 +01002622 if (af == nullptr) {
2623 return PERMISSION_DENIED;
2624 }
Eric Laurent50d72582022-12-20 20:20:23 +01002625 return af->supportsBluetoothVariableLatency(support);
Eric Laurent52057642022-12-16 11:45:07 +01002626}
2627
Mikhail Naganovffd97712023-05-03 17:45:36 -07002628status_t AudioSystem::getAudioPolicyConfig(media::AudioPolicyConfig *config) {
Andy Hung264fa4c2024-02-21 15:52:12 -08002629 const sp<IAudioFlinger> af = get_audio_flinger();
Mikhail Naganovffd97712023-05-03 17:45:36 -07002630 if (af == nullptr) {
2631 return PERMISSION_DENIED;
2632 }
2633 return af->getAudioPolicyConfig(config);
2634}
2635
Ytai Ben-Tsvi5120eda2020-03-26 09:41:15 -07002636class CaptureStateListenerImpl : public media::BnCaptureStateListener,
2637 public IBinder::DeathRecipient {
2638public:
Ytai Ben-Tsvid9f82832020-08-27 09:31:10 -07002639 CaptureStateListenerImpl(
2640 const sp<IAudioPolicyService>& aps,
2641 const sp<AudioSystem::CaptureStateListener>& listener)
Ytai Ben-Tsvia46b6d32020-08-31 13:29:11 -07002642 : mAps(aps), mListener(listener) {}
2643
2644 void init() {
Ytai Ben-Tsvid9f82832020-08-27 09:31:10 -07002645 bool active;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002646 status_t status = statusTFromBinderStatus(
2647 mAps->registerSoundTriggerCaptureStateListener(this, &active));
Ytai Ben-Tsvid9f82832020-08-27 09:31:10 -07002648 if (status != NO_ERROR) {
2649 mListener->onServiceDied();
2650 return;
2651 }
2652 mListener->onStateChanged(active);
Ytai Ben-Tsvia46b6d32020-08-31 13:29:11 -07002653 IInterface::asBinder(mAps)->linkToDeath(this);
Ytai Ben-Tsvid9f82832020-08-27 09:31:10 -07002654 }
2655
Ytai Ben-Tsvi5120eda2020-03-26 09:41:15 -07002656 binder::Status setCaptureState(bool active) override {
Andy Hungacd955a2024-02-16 17:14:36 -08002657 std::lock_guard _l(AudioSystem::gSoundTriggerMutex);
Ytai Ben-Tsvid9f82832020-08-27 09:31:10 -07002658 mListener->onStateChanged(active);
Ytai Ben-Tsvi5120eda2020-03-26 09:41:15 -07002659 return binder::Status::ok();
2660 }
2661
2662 void binderDied(const wp<IBinder>&) override {
Andy Hungacd955a2024-02-16 17:14:36 -08002663 std::lock_guard _l(AudioSystem::gSoundTriggerMutex);
Ytai Ben-Tsvid9f82832020-08-27 09:31:10 -07002664 mListener->onServiceDied();
Andy Hungacd955a2024-02-16 17:14:36 -08002665 AudioSystem::gSoundTriggerCaptureStateListener = nullptr;
Ytai Ben-Tsvi5120eda2020-03-26 09:41:15 -07002666 }
Ytai Ben-Tsvid9f82832020-08-27 09:31:10 -07002667
2668private:
2669 // Need this in order to keep the death receipent alive.
2670 sp<IAudioPolicyService> mAps;
2671 sp<AudioSystem::CaptureStateListener> mListener;
Ytai Ben-Tsvi5120eda2020-03-26 09:41:15 -07002672};
2673
2674status_t AudioSystem::registerSoundTriggerCaptureStateListener(
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002675 const sp<CaptureStateListener>& listener) {
Ytai Ben-Tsvid9f82832020-08-27 09:31:10 -07002676 LOG_ALWAYS_FATAL_IF(listener == nullptr);
2677
Andy Hung264fa4c2024-02-21 15:52:12 -08002678 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Ytai Ben-Tsvi5120eda2020-03-26 09:41:15 -07002679 if (aps == 0) {
2680 return PERMISSION_DENIED;
2681 }
2682
Andy Hungacd955a2024-02-16 17:14:36 -08002683 std::lock_guard _l(AudioSystem::gSoundTriggerMutex);
Ytai Ben-Tsvid9f82832020-08-27 09:31:10 -07002684 gSoundTriggerCaptureStateListener = new CaptureStateListenerImpl(aps, listener);
Ytai Ben-Tsvia46b6d32020-08-31 13:29:11 -07002685 gSoundTriggerCaptureStateListener->init();
Ytai Ben-Tsvi5120eda2020-03-26 09:41:15 -07002686
Ytai Ben-Tsvi5120eda2020-03-26 09:41:15 -07002687 return NO_ERROR;
2688}
2689
jiabin1319f5a2021-03-30 22:21:24 +00002690status_t AudioSystem::setVibratorInfos(
2691 const std::vector<media::AudioVibratorInfo>& vibratorInfos) {
Andy Hung264fa4c2024-02-21 15:52:12 -08002692 const sp<IAudioFlinger> af = get_audio_flinger();
jiabin1319f5a2021-03-30 22:21:24 +00002693 if (af == nullptr) {
2694 return PERMISSION_DENIED;
2695 }
2696 return af->setVibratorInfos(vibratorInfos);
2697}
2698
Jiabin Huangebe64102021-09-07 20:01:07 +00002699status_t AudioSystem::getMmapPolicyInfo(
jiabine99d0882021-09-17 05:21:25 +00002700 AudioMMapPolicyType policyType, std::vector<AudioMMapPolicyInfo> *policyInfos) {
Andy Hung264fa4c2024-02-21 15:52:12 -08002701 const sp<IAudioFlinger> af = get_audio_flinger();
Jiabin Huangebe64102021-09-07 20:01:07 +00002702 if (af == nullptr) {
2703 return PERMISSION_DENIED;
2704 }
2705 return af->getMmapPolicyInfos(policyType, policyInfos);
2706}
2707
jiabine504e7b2021-09-18 00:27:08 +00002708int32_t AudioSystem::getAAudioMixerBurstCount() {
Andy Hung264fa4c2024-02-21 15:52:12 -08002709 const sp<IAudioFlinger> af = get_audio_flinger();
jiabine504e7b2021-09-18 00:27:08 +00002710 if (af == nullptr) {
2711 return PERMISSION_DENIED;
2712 }
2713 return af->getAAudioMixerBurstCount();
2714}
2715
2716int32_t AudioSystem::getAAudioHardwareBurstMinUsec() {
Andy Hung264fa4c2024-02-21 15:52:12 -08002717 const sp<IAudioFlinger> af = get_audio_flinger();
jiabine504e7b2021-09-18 00:27:08 +00002718 if (af == nullptr) {
2719 return PERMISSION_DENIED;
2720 }
2721 return af->getAAudioHardwareBurstMinUsec();
2722}
2723
jiabina84c3d32022-12-02 18:59:55 +00002724status_t AudioSystem::getSupportedMixerAttributes(
2725 audio_port_handle_t portId, std::vector<audio_mixer_attributes_t> *mixerAttrs) {
Andy Hung264fa4c2024-02-21 15:52:12 -08002726 const sp<IAudioPolicyService> aps = get_audio_policy_service();
jiabina84c3d32022-12-02 18:59:55 +00002727 if (aps == nullptr) {
2728 return PERMISSION_DENIED;
2729 }
2730
2731 int32_t portIdAidl = VALUE_OR_RETURN_STATUS(legacy2aidl_audio_port_handle_t_int32_t(portId));
2732 std::vector<media::AudioMixerAttributesInternal> _aidlReturn;
2733 RETURN_STATUS_IF_ERROR(statusTFromBinderStatus(
2734 aps->getSupportedMixerAttributes(portIdAidl, &_aidlReturn)));
2735 *mixerAttrs = VALUE_OR_RETURN_STATUS(
2736 convertContainer<std::vector<audio_mixer_attributes_t>>(
2737 _aidlReturn,
2738 aidl2legacy_AudioMixerAttributesInternal_audio_mixer_attributes_t));
2739 return OK;
2740}
2741
2742status_t AudioSystem::setPreferredMixerAttributes(const audio_attributes_t *attr,
2743 audio_port_handle_t portId,
2744 uid_t uid,
2745 const audio_mixer_attributes_t *mixerAttr) {
Andy Hung264fa4c2024-02-21 15:52:12 -08002746 const sp<IAudioPolicyService> aps = get_audio_policy_service();
jiabina84c3d32022-12-02 18:59:55 +00002747 if (aps == nullptr) {
2748 return PERMISSION_DENIED;
2749 }
2750
Mikhail Naganov1c400902023-05-17 11:48:43 -07002751 media::audio::common::AudioAttributes attrAidl = VALUE_OR_RETURN_STATUS(
2752 legacy2aidl_audio_attributes_t_AudioAttributes(*attr));
jiabina84c3d32022-12-02 18:59:55 +00002753 media::AudioMixerAttributesInternal mixerAttrAidl = VALUE_OR_RETURN_STATUS(
2754 legacy2aidl_audio_mixer_attributes_t_AudioMixerAttributesInternal(*mixerAttr));
2755 int32_t uidAidl = VALUE_OR_RETURN_STATUS(legacy2aidl_uid_t_int32_t(uid));
2756 int32_t portIdAidl = VALUE_OR_RETURN_STATUS(legacy2aidl_audio_port_handle_t_int32_t(portId));
2757
2758 return statusTFromBinderStatus(
2759 aps->setPreferredMixerAttributes(attrAidl, portIdAidl, uidAidl, mixerAttrAidl));
2760}
2761
2762status_t AudioSystem::getPreferredMixerAttributes(
2763 const audio_attributes_t *attr,
2764 audio_port_handle_t portId,
2765 std::optional<audio_mixer_attributes_t> *mixerAttr) {
Andy Hung264fa4c2024-02-21 15:52:12 -08002766 const sp<IAudioPolicyService> aps = get_audio_policy_service();
jiabina84c3d32022-12-02 18:59:55 +00002767 if (aps == nullptr) {
2768 return PERMISSION_DENIED;
2769 }
2770
Mikhail Naganov1c400902023-05-17 11:48:43 -07002771 media::audio::common::AudioAttributes attrAidl = VALUE_OR_RETURN_STATUS(
2772 legacy2aidl_audio_attributes_t_AudioAttributes(*attr));
jiabina84c3d32022-12-02 18:59:55 +00002773 int32_t portIdAidl = VALUE_OR_RETURN_STATUS(legacy2aidl_audio_port_handle_t_int32_t(portId));
2774 std::optional<media::AudioMixerAttributesInternal> _aidlReturn;
2775 RETURN_STATUS_IF_ERROR(statusTFromBinderStatus(
2776 aps->getPreferredMixerAttributes(attrAidl, portIdAidl, &_aidlReturn)));
2777
2778 if (_aidlReturn.has_value()) {
2779 *mixerAttr = VALUE_OR_RETURN_STATUS(
2780 aidl2legacy_AudioMixerAttributesInternal_audio_mixer_attributes_t(
2781 _aidlReturn.value()));
2782 }
2783 return NO_ERROR;
2784}
2785
2786status_t AudioSystem::clearPreferredMixerAttributes(const audio_attributes_t *attr,
2787 audio_port_handle_t portId,
2788 uid_t uid) {
Andy Hung264fa4c2024-02-21 15:52:12 -08002789 const sp<IAudioPolicyService> aps = get_audio_policy_service();
jiabina84c3d32022-12-02 18:59:55 +00002790 if (aps == nullptr) {
2791 return PERMISSION_DENIED;
2792 }
2793
Mikhail Naganov1c400902023-05-17 11:48:43 -07002794 media::audio::common::AudioAttributes attrAidl = VALUE_OR_RETURN_STATUS(
2795 legacy2aidl_audio_attributes_t_AudioAttributes(*attr));
jiabina84c3d32022-12-02 18:59:55 +00002796 int32_t uidAidl = VALUE_OR_RETURN_STATUS(legacy2aidl_uid_t_int32_t(uid));
2797 int32_t portIdAidl = VALUE_OR_RETURN_STATUS(legacy2aidl_audio_port_handle_t_int32_t(portId));
2798 return statusTFromBinderStatus(
2799 aps->clearPreferredMixerAttributes(attrAidl, portIdAidl, uidAidl));
2800}
2801
Eric Laurentc2f1f072009-07-17 12:17:14 -07002802// ---------------------------------------------------------------------------
2803
Eric Laurente8726fe2015-06-26 09:39:24 -07002804int AudioSystem::AudioPolicyServiceClient::addAudioPortCallback(
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002805 const sp<AudioPortCallback>& callback) {
Andy Hungacd955a2024-02-16 17:14:36 -08002806 std::lock_guard _l(mMutex);
Andy Hungd211ce02024-02-16 15:34:07 -08002807 return mAudioPortCallbacks.insert(callback).second ? mAudioPortCallbacks.size() : -1;
Eric Laurentb28753e2015-04-01 13:06:28 -07002808}
2809
Eric Laurente8726fe2015-06-26 09:39:24 -07002810int AudioSystem::AudioPolicyServiceClient::removeAudioPortCallback(
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002811 const sp<AudioPortCallback>& callback) {
Andy Hungacd955a2024-02-16 17:14:36 -08002812 std::lock_guard _l(mMutex);
Andy Hungd211ce02024-02-16 15:34:07 -08002813 return mAudioPortCallbacks.erase(callback) > 0 ? mAudioPortCallbacks.size() : -1;
Eric Laurentb28753e2015-04-01 13:06:28 -07002814}
2815
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002816Status AudioSystem::AudioPolicyServiceClient::onAudioPortListUpdate() {
Andy Hungacd955a2024-02-16 17:14:36 -08002817 std::lock_guard _l(mMutex);
Andy Hungd211ce02024-02-16 15:34:07 -08002818 for (const auto& callback : mAudioPortCallbacks) {
2819 callback->onAudioPortListUpdate();
Eric Laurentb28753e2015-04-01 13:06:28 -07002820 }
Ytai Ben-Tsvi7e7a79d2020-12-15 16:48:16 -08002821 return Status::ok();
Eric Laurentb28753e2015-04-01 13:06:28 -07002822}
2823
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002824Status AudioSystem::AudioPolicyServiceClient::onAudioPatchListUpdate() {
Andy Hungacd955a2024-02-16 17:14:36 -08002825 std::lock_guard _l(mMutex);
Andy Hungd211ce02024-02-16 15:34:07 -08002826 for (const auto& callback : mAudioPortCallbacks) {
2827 callback->onAudioPatchListUpdate();
Eric Laurentb28753e2015-04-01 13:06:28 -07002828 }
Ytai Ben-Tsvi7e7a79d2020-12-15 16:48:16 -08002829 return Status::ok();
Eric Laurentb28753e2015-04-01 13:06:28 -07002830}
2831
François Gaffiecfe17322018-11-07 13:41:29 +01002832// ----------------------------------------------------------------------------
2833int AudioSystem::AudioPolicyServiceClient::addAudioVolumeGroupCallback(
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002834 const sp<AudioVolumeGroupCallback>& callback) {
Andy Hungacd955a2024-02-16 17:14:36 -08002835 std::lock_guard _l(mMutex);
Andy Hungd211ce02024-02-16 15:34:07 -08002836 return mAudioVolumeGroupCallbacks.insert(callback).second
2837 ? mAudioVolumeGroupCallbacks.size() : -1;
François Gaffiecfe17322018-11-07 13:41:29 +01002838}
2839
2840int AudioSystem::AudioPolicyServiceClient::removeAudioVolumeGroupCallback(
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.erase(callback) > 0
2844 ? mAudioVolumeGroupCallbacks.size() : -1;
François Gaffiecfe17322018-11-07 13:41:29 +01002845}
2846
Ytai Ben-Tsvi7e7a79d2020-12-15 16:48:16 -08002847Status AudioSystem::AudioPolicyServiceClient::onAudioVolumeGroupChanged(int32_t group,
2848 int32_t flags) {
2849 volume_group_t groupLegacy = VALUE_OR_RETURN_BINDER_STATUS(
2850 aidl2legacy_int32_t_volume_group_t(group));
2851 int flagsLegacy = VALUE_OR_RETURN_BINDER_STATUS(convertReinterpret<int>(flags));
2852
Andy Hungacd955a2024-02-16 17:14:36 -08002853 std::lock_guard _l(mMutex);
Andy Hungd211ce02024-02-16 15:34:07 -08002854 for (const auto& callback : mAudioVolumeGroupCallbacks) {
2855 callback->onAudioVolumeGroupChanged(groupLegacy, flagsLegacy);
François Gaffiecfe17322018-11-07 13:41:29 +01002856 }
Ytai Ben-Tsvi7e7a79d2020-12-15 16:48:16 -08002857 return Status::ok();
François Gaffiecfe17322018-11-07 13:41:29 +01002858}
2859// ----------------------------------------------------------------------------
2860
Ytai Ben-Tsvi7e7a79d2020-12-15 16:48:16 -08002861Status AudioSystem::AudioPolicyServiceClient::onDynamicPolicyMixStateUpdate(
2862 const ::std::string& regId, int32_t state) {
2863 ALOGV("AudioPolicyServiceClient::onDynamicPolicyMixStateUpdate(%s, %d)", regId.c_str(), state);
2864
2865 String8 regIdLegacy = VALUE_OR_RETURN_BINDER_STATUS(aidl2legacy_string_view_String8(regId));
2866 int stateLegacy = VALUE_OR_RETURN_BINDER_STATUS(convertReinterpret<int>(state));
Jean-Michel Trivif613d422015-04-23 18:41:29 -07002867 dynamic_policy_callback cb = NULL;
2868 {
Andy Hungacd955a2024-02-16 17:14:36 -08002869 std::lock_guard _l(AudioSystem::gMutex);
Jean-Michel Trivif613d422015-04-23 18:41:29 -07002870 cb = gDynPolicyCallback;
2871 }
2872
2873 if (cb != NULL) {
Ytai Ben-Tsvi7e7a79d2020-12-15 16:48:16 -08002874 cb(DYNAMIC_POLICY_EVENT_MIX_STATE_UPDATE, regIdLegacy, stateLegacy);
Jean-Michel Trivif613d422015-04-23 18:41:29 -07002875 }
Ytai Ben-Tsvi7e7a79d2020-12-15 16:48:16 -08002876 return Status::ok();
Jean-Michel Trivide801052015-04-14 19:10:14 -07002877}
2878
Ytai Ben-Tsvi7e7a79d2020-12-15 16:48:16 -08002879Status AudioSystem::AudioPolicyServiceClient::onRecordingConfigurationUpdate(
2880 int32_t event,
2881 const media::RecordClientInfo& clientInfo,
Mikhail Naganovdbf03642021-08-25 18:15:32 -07002882 const AudioConfigBase& clientConfig,
Ytai Ben-Tsvi7e7a79d2020-12-15 16:48:16 -08002883 const std::vector<media::EffectDescriptor>& clientEffects,
Mikhail Naganovdbf03642021-08-25 18:15:32 -07002884 const AudioConfigBase& deviceConfig,
Ytai Ben-Tsvi7e7a79d2020-12-15 16:48:16 -08002885 const std::vector<media::EffectDescriptor>& effects,
2886 int32_t patchHandle,
Mikhail Naganovddceecc2021-09-03 13:58:56 -07002887 AudioSource source) {
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -08002888 record_config_callback cb = NULL;
2889 {
Andy Hungacd955a2024-02-16 17:14:36 -08002890 std::lock_guard _l(AudioSystem::gMutex);
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -08002891 cb = gRecordConfigCallback;
2892 }
2893
2894 if (cb != NULL) {
Ytai Ben-Tsvi7e7a79d2020-12-15 16:48:16 -08002895 int eventLegacy = VALUE_OR_RETURN_BINDER_STATUS(convertReinterpret<int>(event));
2896 record_client_info_t clientInfoLegacy = VALUE_OR_RETURN_BINDER_STATUS(
2897 aidl2legacy_RecordClientInfo_record_client_info_t(clientInfo));
2898 audio_config_base_t clientConfigLegacy = VALUE_OR_RETURN_BINDER_STATUS(
Mikhail Naganovde3fa182021-07-30 15:06:42 -07002899 aidl2legacy_AudioConfigBase_audio_config_base_t(clientConfig, true /*isInput*/));
Ytai Ben-Tsvi7e7a79d2020-12-15 16:48:16 -08002900 std::vector<effect_descriptor_t> clientEffectsLegacy = VALUE_OR_RETURN_BINDER_STATUS(
2901 convertContainer<std::vector<effect_descriptor_t>>(
2902 clientEffects,
2903 aidl2legacy_EffectDescriptor_effect_descriptor_t));
2904 audio_config_base_t deviceConfigLegacy = VALUE_OR_RETURN_BINDER_STATUS(
Mikhail Naganovde3fa182021-07-30 15:06:42 -07002905 aidl2legacy_AudioConfigBase_audio_config_base_t(deviceConfig, true /*isInput*/));
Ytai Ben-Tsvi7e7a79d2020-12-15 16:48:16 -08002906 std::vector<effect_descriptor_t> effectsLegacy = VALUE_OR_RETURN_BINDER_STATUS(
2907 convertContainer<std::vector<effect_descriptor_t>>(
2908 effects,
2909 aidl2legacy_EffectDescriptor_effect_descriptor_t));
2910 audio_patch_handle_t patchHandleLegacy = VALUE_OR_RETURN_BINDER_STATUS(
2911 aidl2legacy_int32_t_audio_patch_handle_t(patchHandle));
2912 audio_source_t sourceLegacy = VALUE_OR_RETURN_BINDER_STATUS(
Mikhail Naganovddceecc2021-09-03 13:58:56 -07002913 aidl2legacy_AudioSource_audio_source_t(source));
Ytai Ben-Tsvi7e7a79d2020-12-15 16:48:16 -08002914 cb(eventLegacy, &clientInfoLegacy, &clientConfigLegacy, clientEffectsLegacy,
2915 &deviceConfigLegacy, effectsLegacy, patchHandleLegacy, sourceLegacy);
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -08002916 }
Ytai Ben-Tsvi7e7a79d2020-12-15 16:48:16 -08002917 return Status::ok();
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -08002918}
2919
Jean-Michel Trivi9a6b9ad2020-10-22 16:46:43 -07002920Status AudioSystem::AudioPolicyServiceClient::onRoutingUpdated() {
2921 routing_callback cb = NULL;
2922 {
Andy Hungacd955a2024-02-16 17:14:36 -08002923 std::lock_guard _l(AudioSystem::gMutex);
Jean-Michel Trivi9a6b9ad2020-10-22 16:46:43 -07002924 cb = gRoutingCallback;
2925 }
2926
2927 if (cb != NULL) {
2928 cb();
2929 }
2930 return Status::ok();
2931}
2932
Jean-Michel Trivi78f2b302022-04-15 18:18:41 +00002933Status AudioSystem::AudioPolicyServiceClient::onVolumeRangeInitRequest() {
2934 vol_range_init_req_callback cb = NULL;
2935 {
Andy Hungacd955a2024-02-16 17:14:36 -08002936 std::lock_guard _l(AudioSystem::gMutex);
Jean-Michel Trivi78f2b302022-04-15 18:18:41 +00002937 cb = gVolRangeInitReqCallback;
2938 }
2939
2940 if (cb != NULL) {
2941 cb();
2942 }
2943 return Status::ok();
2944}
2945
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002946void AudioSystem::AudioPolicyServiceClient::binderDied(const wp<IBinder>& who __unused) {
Glenn Kastend2d089f2014-11-05 11:48:12 -08002947 {
Andy Hungacd955a2024-02-16 17:14:36 -08002948 std::lock_guard _l(mMutex);
Andy Hungd211ce02024-02-16 15:34:07 -08002949 for (const auto& callback : mAudioPortCallbacks) {
2950 callback->onServiceDied();
Glenn Kastend2d089f2014-11-05 11:48:12 -08002951 }
Andy Hungd211ce02024-02-16 15:34:07 -08002952 for (const auto& callback : mAudioVolumeGroupCallbacks) {
2953 callback->onServiceDied();
François Gaffiecfe17322018-11-07 13:41:29 +01002954 }
Eric Laurentb52c1522014-05-20 11:27:36 -07002955 }
Mikhail Naganovec3d5792022-05-06 00:19:55 +00002956 AudioSystem::clearAudioPolicyService();
Eric Laurentc2f1f072009-07-17 12:17:14 -07002957
Steve Block5ff1dd52012-01-05 23:22:43 +00002958 ALOGW("AudioPolicyService server died!");
Eric Laurentc2f1f072009-07-17 12:17:14 -07002959}
2960
Ytai Ben-Tsvi7e7a79d2020-12-15 16:48:16 -08002961ConversionResult<record_client_info_t>
2962aidl2legacy_RecordClientInfo_record_client_info_t(const media::RecordClientInfo& aidl) {
2963 record_client_info_t legacy;
2964 legacy.riid = VALUE_OR_RETURN(aidl2legacy_int32_t_audio_unique_id_t(aidl.riid));
2965 legacy.uid = VALUE_OR_RETURN(aidl2legacy_int32_t_uid_t(aidl.uid));
2966 legacy.session = VALUE_OR_RETURN(aidl2legacy_int32_t_audio_session_t(aidl.session));
Mikhail Naganovddceecc2021-09-03 13:58:56 -07002967 legacy.source = VALUE_OR_RETURN(aidl2legacy_AudioSource_audio_source_t(aidl.source));
Ytai Ben-Tsvi7e7a79d2020-12-15 16:48:16 -08002968 legacy.port_id = VALUE_OR_RETURN(aidl2legacy_int32_t_audio_port_handle_t(aidl.portId));
2969 legacy.silenced = aidl.silenced;
2970 return legacy;
2971}
2972
2973ConversionResult<media::RecordClientInfo>
2974legacy2aidl_record_client_info_t_RecordClientInfo(const record_client_info_t& legacy) {
2975 media::RecordClientInfo aidl;
2976 aidl.riid = VALUE_OR_RETURN(legacy2aidl_audio_unique_id_t_int32_t(legacy.riid));
2977 aidl.uid = VALUE_OR_RETURN(legacy2aidl_uid_t_int32_t(legacy.uid));
2978 aidl.session = VALUE_OR_RETURN(legacy2aidl_audio_session_t_int32_t(legacy.session));
Mikhail Naganovddceecc2021-09-03 13:58:56 -07002979 aidl.source = VALUE_OR_RETURN(legacy2aidl_audio_source_t_AudioSource(legacy.source));
Ytai Ben-Tsvi7e7a79d2020-12-15 16:48:16 -08002980 aidl.portId = VALUE_OR_RETURN(legacy2aidl_audio_port_handle_t_int32_t(legacy.port_id));
2981 aidl.silenced = legacy.silenced;
2982 return aidl;
2983}
2984
Glenn Kasten40bc9062015-03-20 09:09:33 -07002985} // namespace android