blob: 3f4fcfdf35680098bc6333f149a104177d8b1f56 [file] [log] [blame]
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001/*
2 * Copyright (C) 2006-2007 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#define LOG_TAG "AudioSystem"
18//#define LOG_NDEBUG 0
19
20#include <utils/Log.h>
Ytai Ben-Tsvi5120eda2020-03-26 09:41:15 -070021
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -080022#include <android/media/IAudioPolicyService.h>
Jan Sebechlebsky0af8e872023-08-11 14:45:08 +020023#include <android/media/AudioMixUpdate.h>
Ytai Ben-Tsvi5120eda2020-03-26 09:41:15 -070024#include <android/media/BnCaptureStateListener.h>
Marvin Raminbdefaf02023-11-01 09:10:32 +010025#include <android_media_audiopolicy.h>
Mathias Agopian75624082009-05-19 19:08:10 -070026#include <binder/IServiceManager.h>
Eric Laurentfb00fc72017-05-25 18:17:12 -070027#include <binder/ProcessState.h>
François Gaffie24437602018-04-23 15:08:59 +020028#include <binder/IPCThreadState.h>
Ytai Ben-Tsvi10dc0a62020-09-18 11:31:55 -070029#include <media/AidlConversion.h>
Eric Laurent21da6472017-11-09 16:29:26 -080030#include <media/AudioResamplerPublic.h>
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080031#include <media/AudioSystem.h>
Glenn Kasten1ab85ec2013-05-31 09:18:43 -070032#include <media/IAudioFlinger.h>
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -080033#include <media/PolicyAidlConversion.h>
François Gaffied0ba9ed2018-11-05 11:50:42 +010034#include <media/TypeConverter.h>
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080035#include <math.h>
36
Dima Zavin64760242011-05-11 14:15:23 -070037#include <system/audio.h>
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -080038#include <android/media/GetInputForAttrResponse.h>
jiabina84c3d32022-12-02 18:59:55 +000039#include <android/media/AudioMixerAttributesInternal.h>
Dima Zavinfce7a472011-04-19 22:30:36 -070040
Ytai Ben-Tsvi1ff75692020-11-06 12:16:12 -080041#define VALUE_OR_RETURN_BINDER_STATUS(x) \
Ytai Ben-Tsvi10dc0a62020-09-18 11:31:55 -070042 ({ auto _tmp = (x); \
Andy Hung1131b6e2020-12-08 20:47:45 -080043 if (!_tmp.ok()) return aidl_utils::binderStatusFromStatusT(_tmp.error()); \
Ytai Ben-Tsvia3815202020-10-28 14:58:08 -070044 std::move(_tmp.value()); })
Ytai Ben-Tsvi10dc0a62020-09-18 11:31:55 -070045
Eric Laurentc2f1f072009-07-17 12:17:14 -070046// ----------------------------------------------------------------------------
Eric Laurentc2f1f072009-07-17 12:17:14 -070047
Marvin Raminbdefaf02023-11-01 09:10:32 +010048namespace audio_flags = android::media::audiopolicy;
49
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080050namespace android {
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -080051using aidl_utils::statusTFromBinderStatus;
Ytai Ben-Tsvi10dc0a62020-09-18 11:31:55 -070052using binder::Status;
Mikhail Naganov57bd06f2021-08-10 16:41:54 -070053using content::AttributionSourceState;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -080054using media::IAudioPolicyService;
Mikhail Naganovdbf03642021-08-25 18:15:32 -070055using media::audio::common::AudioConfig;
56using media::audio::common::AudioConfigBase;
Mikhail Naganovf4a75362021-09-16 00:02:54 +000057using media::audio::common::AudioDevice;
58using media::audio::common::AudioDeviceDescription;
Mikhail Naganov57bd06f2021-08-10 16:41:54 -070059using media::audio::common::AudioFormatDescription;
jiabine99d0882021-09-17 05:21:25 +000060using media::audio::common::AudioMMapPolicyInfo;
61using media::audio::common::AudioMMapPolicyType;
Mikhail Naganovdbf03642021-08-25 18:15:32 -070062using media::audio::common::AudioOffloadInfo;
Mikhail Naganovddceecc2021-09-03 13:58:56 -070063using media::audio::common::AudioSource;
Mikhail Naganovdbf03642021-08-25 18:15:32 -070064using media::audio::common::AudioStreamType;
65using media::audio::common::AudioUsage;
Mikhail Naganov0078ee52021-09-30 23:06:20 +000066using media::audio::common::Int;
Ytai Ben-Tsvi10dc0a62020-09-18 11:31:55 -070067
Andy Hungacd955a2024-02-16 17:14:36 -080068std::mutex AudioSystem::gMutex;
Jean-Michel Trivif613d422015-04-23 18:41:29 -070069dynamic_policy_callback AudioSystem::gDynPolicyCallback = NULL;
Svet Ganovf4ddfef2018-01-16 07:37:58 -080070record_config_callback AudioSystem::gRecordConfigCallback = NULL;
Jean-Michel Trivi9a6b9ad2020-10-22 16:46:43 -070071routing_callback AudioSystem::gRoutingCallback = NULL;
Jean-Michel Trivi78f2b302022-04-15 18:18:41 +000072vol_range_init_req_callback AudioSystem::gVolRangeInitReqCallback = NULL;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080073
Andy Hung88e71732024-02-21 16:44:27 -080074std::mutex AudioSystem::gApsCallbackMutex;
Andy Hungacd955a2024-02-16 17:14:36 -080075std::mutex AudioSystem::gErrorCallbacksMutex;
76std::set<audio_error_callback> AudioSystem::gAudioErrorCallbacks;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -080077
Andy Hungacd955a2024-02-16 17:14:36 -080078std::mutex AudioSystem::gSoundTriggerMutex;
79sp<CaptureStateListenerImpl> AudioSystem::gSoundTriggerCaptureStateListener;
Ytai Ben-Tsvi5120eda2020-03-26 09:41:15 -070080
Andy Hungacd955a2024-02-16 17:14:36 -080081// Sets the Binder for the AudioFlinger service, passed to this client process
82// from the system server.
Ahaan Ugale058f94b2021-05-21 02:23:50 -070083// This allows specific isolated processes to access the audio system. Currently used only for the
84// HotwordDetectionService.
Andy Hungda1fb072024-02-20 19:08:08 -080085template <typename ServiceInterface, typename Client, typename AidlInterface,
86 typename ServiceTraits>
87class ServiceHandler {
88public:
Shraddha Basantwanif2aa2502024-04-08 11:01:57 +000089 sp<ServiceInterface> getService()
Andy Hungda1fb072024-02-20 19:08:08 -080090 EXCLUDES(mMutex) NO_THREAD_SAFETY_ANALYSIS { // std::unique_ptr
91 sp<ServiceInterface> service;
92 sp<Client> client;
Ahaan Ugale058f94b2021-05-21 02:23:50 -070093
Andy Hungda1fb072024-02-20 19:08:08 -080094 bool reportNoError = false;
95 {
96 std::lock_guard _l(mMutex);
97 if (mService != nullptr) {
98 return mService;
99 }
Andy Hung66fc0bd2023-01-04 11:45:50 -0800100 }
101
Andy Hungda1fb072024-02-20 19:08:08 -0800102 std::unique_lock ul_only1thread(mSingleGetter);
103 std::unique_lock ul(mMutex);
104 if (mService != nullptr) {
105 return mService;
106 }
107 if (mClient == nullptr) {
108 mClient = sp<Client>::make();
Andy Hung66fc0bd2023-01-04 11:45:50 -0800109 } else {
110 reportNoError = true;
111 }
Andy Hungda1fb072024-02-20 19:08:08 -0800112 while (true) {
113 mService = mLocalService;
114 if (mService != nullptr) break;
Andy Hung66fc0bd2023-01-04 11:45:50 -0800115
Andy Hungda1fb072024-02-20 19:08:08 -0800116 sp<IBinder> binder = mBinder;
117 if (binder == nullptr) {
118 sp <IServiceManager> sm = defaultServiceManager();
119 binder = sm->checkService(String16(ServiceTraits::SERVICE_NAME));
Paul Wangbcf95f62023-02-28 15:38:16 +0000120 if (binder == nullptr) {
Andy Hungda1fb072024-02-20 19:08:08 -0800121 ALOGD("%s: waiting for %s", __func__, ServiceTraits::SERVICE_NAME);
122
123 // if the condition variable is present, setLocalService() and
124 // setBinder() is allowed to use it to notify us.
125 if (mCvGetter == nullptr) {
126 mCvGetter = std::make_shared<std::condition_variable>();
127 }
128 mCvGetter->wait_for(ul, std::chrono::seconds(1));
129 continue;
Paul Wangbcf95f62023-02-28 15:38:16 +0000130 }
Ahaan Ugale058f94b2021-05-21 02:23:50 -0700131 }
Andy Hungda1fb072024-02-20 19:08:08 -0800132 binder->linkToDeath(mClient);
133 auto aidlInterface = interface_cast<AidlInterface>(binder);
134 LOG_ALWAYS_FATAL_IF(aidlInterface == nullptr);
135 if constexpr (std::is_same_v<ServiceInterface, AidlInterface>) {
136 mService = std::move(aidlInterface);
137 } else /* constexpr */ {
138 mService = ServiceTraits::createServiceAdapter(aidlInterface);
139 }
140 break;
Glenn Kastene53b9ea2012-03-12 16:29:55 -0700141 }
Andy Hungda1fb072024-02-20 19:08:08 -0800142 if (mCvGetter) mCvGetter.reset(); // remove condition variable.
143 client = mClient;
144 service = mService;
145 // Make sure callbacks can be received by the client
Shraddha Basantwanif2aa2502024-04-08 11:01:57 +0000146 if (mCanStartThreadPool) {
Akshata Kadam1d486b72023-08-07 05:49:48 +0000147 ProcessState::self()->startThreadPool();
148 }
Andy Hungda1fb072024-02-20 19:08:08 -0800149 ul.unlock();
150 ul_only1thread.unlock();
151 ServiceTraits::onServiceCreate(service, client);
152 if (reportNoError) AudioSystem::reportError(NO_ERROR);
153 return service;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800154 }
Andy Hungda1fb072024-02-20 19:08:08 -0800155
156 status_t setLocalService(const sp<ServiceInterface>& service) EXCLUDES(mMutex) {
157 std::lock_guard _l(mMutex);
158 // we allow clearing once set, but not a double non-null set.
159 if (mService != nullptr && service != nullptr) return INVALID_OPERATION;
160 mLocalService = service;
161 if (mCvGetter) mCvGetter->notify_one();
162 return OK;
163 }
164
165 sp<Client> getClient() EXCLUDES(mMutex) {
166 const auto service = getService();
167 if (service == nullptr) return nullptr;
168 std::lock_guard _l(mMutex);
169 return mClient;
170 }
171
172 void setBinder(const sp<IBinder>& binder) EXCLUDES(mMutex) {
173 std::lock_guard _l(mMutex);
174 if (mService != nullptr) {
175 ALOGW("%s: ignoring; %s connection already established.",
176 __func__, ServiceTraits::SERVICE_NAME);
177 return;
178 }
179 mBinder = binder;
180 if (mCvGetter) mCvGetter->notify_one();
181 }
182
183 void clearService() EXCLUDES(mMutex) {
184 std::lock_guard _l(mMutex);
185 mService.clear();
186 if (mClient) ServiceTraits::onClearService(mClient);
187 }
188
Shraddha Basantwanif2aa2502024-04-08 11:01:57 +0000189 void disableThreadPool() {
190 mCanStartThreadPool = false;
191 }
192
Andy Hungda1fb072024-02-20 19:08:08 -0800193private:
194 std::mutex mSingleGetter;
195 std::mutex mMutex;
196 std::shared_ptr<std::condition_variable> mCvGetter GUARDED_BY(mMutex);
197 sp<IBinder> mBinder GUARDED_BY(mMutex);
198 sp<ServiceInterface> mLocalService GUARDED_BY(mMutex);
199 sp<ServiceInterface> mService GUARDED_BY(mMutex);
200 sp<Client> mClient GUARDED_BY(mMutex);
Shraddha Basantwanif2aa2502024-04-08 11:01:57 +0000201 std::atomic<bool> mCanStartThreadPool = true;
Andy Hungda1fb072024-02-20 19:08:08 -0800202};
203
204struct AudioFlingerTraits {
205 static void onServiceCreate(
206 const sp<IAudioFlinger>& af, const sp<AudioSystem::AudioFlingerClient>& afc) {
207 const int64_t token = IPCThreadState::self()->clearCallingIdentity();
208 af->registerClient(afc);
209 IPCThreadState::self()->restoreCallingIdentity(token);
210 }
211
212 static sp<IAudioFlinger> createServiceAdapter(
213 const sp<media::IAudioFlingerService>& aidlInterface) {
214 return sp<AudioFlingerClientAdapter>::make(aidlInterface);
215 }
216
217 static void onClearService(const sp<AudioSystem::AudioFlingerClient>& afc) {
218 afc->clearIoCache();
219 }
220
221 static constexpr const char* SERVICE_NAME = IAudioFlinger::DEFAULT_SERVICE_NAME;
222};
223
224[[clang::no_destroy]] static constinit ServiceHandler<IAudioFlinger,
225 AudioSystem::AudioFlingerClient, media::IAudioFlingerService,
226 AudioFlingerTraits> gAudioFlingerServiceHandler;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800227
Andy Hung264fa4c2024-02-21 15:52:12 -0800228sp<IAudioFlinger> AudioSystem::get_audio_flinger() {
Andy Hungda1fb072024-02-20 19:08:08 -0800229 return gAudioFlingerServiceHandler.getService();
Akshata Kadam1d486b72023-08-07 05:49:48 +0000230}
231
Andy Hungda1fb072024-02-20 19:08:08 -0800232sp<AudioSystem::AudioFlingerClient> AudioSystem::getAudioFlingerClient() {
233 return gAudioFlingerServiceHandler.getClient();
234}
235
236void AudioSystem::setAudioFlingerBinder(const sp<IBinder>& audioFlinger) {
237 if (audioFlinger->getInterfaceDescriptor() != media::IAudioFlingerService::descriptor) {
238 ALOGE("%s: received a binder of type %s",
239 __func__, String8(audioFlinger->getInterfaceDescriptor()).c_str());
240 return;
241 }
242 gAudioFlingerServiceHandler.setBinder(audioFlinger);
243}
244
245status_t AudioSystem::setLocalAudioFlinger(const sp<IAudioFlinger>& af) {
246 return gAudioFlingerServiceHandler.setLocalService(af);
Eric Laurent296fb132015-05-01 11:38:42 -0700247}
248
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800249sp<AudioIoDescriptor> AudioSystem::getIoDescriptor(audio_io_handle_t ioHandle) {
Eric Laurent296fb132015-05-01 11:38:42 -0700250 sp<AudioIoDescriptor> desc;
251 const sp<AudioFlingerClient> afc = getAudioFlingerClient();
252 if (afc != 0) {
253 desc = afc->getIoDescriptor(ioHandle);
254 }
255 return desc;
256}
257
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800258/* static */ status_t AudioSystem::checkAudioFlinger() {
Eric Laurent46291612013-07-18 14:38:44 -0700259 if (defaultServiceManager()->checkService(String16("media.audio_flinger")) != 0) {
260 return NO_ERROR;
261 }
262 return DEAD_OBJECT;
263}
264
Glenn Kasten4a8308b2016-04-18 14:10:01 -0700265// FIXME Declare in binder opcode order, similarly to IAudioFlinger.h and IAudioFlinger.cpp
266
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800267status_t AudioSystem::muteMicrophone(bool state) {
Andy Hung264fa4c2024-02-21 15:52:12 -0800268 const sp<IAudioFlinger> af = get_audio_flinger();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800269 if (af == 0) return PERMISSION_DENIED;
270 return af->setMicMute(state);
271}
272
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800273status_t AudioSystem::isMicrophoneMuted(bool* state) {
Andy Hung264fa4c2024-02-21 15:52:12 -0800274 const sp<IAudioFlinger> af = get_audio_flinger();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800275 if (af == 0) return PERMISSION_DENIED;
276 *state = af->getMicMute();
277 return NO_ERROR;
278}
279
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800280status_t AudioSystem::setMasterVolume(float value) {
Andy Hung264fa4c2024-02-21 15:52:12 -0800281 const sp<IAudioFlinger> af = get_audio_flinger();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800282 if (af == 0) return PERMISSION_DENIED;
283 af->setMasterVolume(value);
284 return NO_ERROR;
285}
286
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800287status_t AudioSystem::setMasterMute(bool mute) {
Andy Hung264fa4c2024-02-21 15:52:12 -0800288 const sp<IAudioFlinger> af = get_audio_flinger();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800289 if (af == 0) return PERMISSION_DENIED;
290 af->setMasterMute(mute);
291 return NO_ERROR;
292}
293
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800294status_t AudioSystem::getMasterVolume(float* volume) {
Andy Hung264fa4c2024-02-21 15:52:12 -0800295 const sp<IAudioFlinger> af = get_audio_flinger();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800296 if (af == 0) return PERMISSION_DENIED;
297 *volume = af->masterVolume();
298 return NO_ERROR;
299}
300
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800301status_t AudioSystem::getMasterMute(bool* mute) {
Andy Hung264fa4c2024-02-21 15:52:12 -0800302 const sp<IAudioFlinger> af = get_audio_flinger();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800303 if (af == 0) return PERMISSION_DENIED;
304 *mute = af->masterMute();
305 return NO_ERROR;
306}
307
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800308status_t AudioSystem::setStreamVolume(audio_stream_type_t stream, float value,
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800309 audio_io_handle_t output) {
Dima Zavinfce7a472011-04-19 22:30:36 -0700310 if (uint32_t(stream) >= AUDIO_STREAM_CNT) return BAD_VALUE;
Andy Hung264fa4c2024-02-21 15:52:12 -0800311 const sp<IAudioFlinger> af = get_audio_flinger();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800312 if (af == 0) return PERMISSION_DENIED;
Eric Laurentc2f1f072009-07-17 12:17:14 -0700313 af->setStreamVolume(stream, value, output);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800314 return NO_ERROR;
315}
316
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800317status_t AudioSystem::setStreamMute(audio_stream_type_t stream, bool mute) {
Dima Zavinfce7a472011-04-19 22:30:36 -0700318 if (uint32_t(stream) >= AUDIO_STREAM_CNT) return BAD_VALUE;
Andy Hung264fa4c2024-02-21 15:52:12 -0800319 const sp<IAudioFlinger> af = get_audio_flinger();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800320 if (af == 0) return PERMISSION_DENIED;
321 af->setStreamMute(stream, mute);
322 return NO_ERROR;
323}
324
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800325status_t AudioSystem::getStreamVolume(audio_stream_type_t stream, float* volume,
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800326 audio_io_handle_t output) {
Dima Zavinfce7a472011-04-19 22:30:36 -0700327 if (uint32_t(stream) >= AUDIO_STREAM_CNT) return BAD_VALUE;
Andy Hung264fa4c2024-02-21 15:52:12 -0800328 const sp<IAudioFlinger> af = get_audio_flinger();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800329 if (af == 0) return PERMISSION_DENIED;
Eric Laurentc2f1f072009-07-17 12:17:14 -0700330 *volume = af->streamVolume(stream, output);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800331 return NO_ERROR;
332}
333
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800334status_t AudioSystem::getStreamMute(audio_stream_type_t stream, bool* mute) {
Dima Zavinfce7a472011-04-19 22:30:36 -0700335 if (uint32_t(stream) >= AUDIO_STREAM_CNT) return BAD_VALUE;
Andy Hung264fa4c2024-02-21 15:52:12 -0800336 const sp<IAudioFlinger> af = get_audio_flinger();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800337 if (af == 0) return PERMISSION_DENIED;
338 *mute = af->streamMute(stream);
339 return NO_ERROR;
340}
341
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800342status_t AudioSystem::setMode(audio_mode_t mode) {
Glenn Kasten930f4ca2012-01-06 16:47:31 -0800343 if (uint32_t(mode) >= AUDIO_MODE_CNT) return BAD_VALUE;
Andy Hung264fa4c2024-02-21 15:52:12 -0800344 const sp<IAudioFlinger> af = get_audio_flinger();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800345 if (af == 0) return PERMISSION_DENIED;
346 return af->setMode(mode);
347}
348
Mikhail Naganovb1ddbb02023-03-15 17:06:59 -0700349status_t AudioSystem::setSimulateDeviceConnections(bool enabled) {
Andy Hung264fa4c2024-02-21 15:52:12 -0800350 const sp<IAudioFlinger> af = get_audio_flinger();
Mikhail Naganovb1ddbb02023-03-15 17:06:59 -0700351 if (af == 0) return PERMISSION_DENIED;
352 return af->setSimulateDeviceConnections(enabled);
353}
354
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800355status_t AudioSystem::setParameters(audio_io_handle_t ioHandle, const String8& keyValuePairs) {
Andy Hung264fa4c2024-02-21 15:52:12 -0800356 const sp<IAudioFlinger> af = get_audio_flinger();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800357 if (af == 0) return PERMISSION_DENIED;
Eric Laurentc2f1f072009-07-17 12:17:14 -0700358 return af->setParameters(ioHandle, keyValuePairs);
359}
360
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800361String8 AudioSystem::getParameters(audio_io_handle_t ioHandle, const String8& keys) {
Andy Hung264fa4c2024-02-21 15:52:12 -0800362 const sp<IAudioFlinger> af = get_audio_flinger();
Eric Laurentc2f1f072009-07-17 12:17:14 -0700363 String8 result = String8("");
364 if (af == 0) return result;
365
366 result = af->getParameters(ioHandle, keys);
367 return result;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800368}
369
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800370status_t AudioSystem::setParameters(const String8& keyValuePairs) {
Glenn Kasten142f5192014-03-25 17:44:59 -0700371 return setParameters(AUDIO_IO_HANDLE_NONE, keyValuePairs);
Glenn Kastenc23885e2013-12-19 16:35:18 -0800372}
373
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800374String8 AudioSystem::getParameters(const String8& keys) {
Glenn Kasten142f5192014-03-25 17:44:59 -0700375 return getParameters(AUDIO_IO_HANDLE_NONE, keys);
Glenn Kastenc23885e2013-12-19 16:35:18 -0800376}
377
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800378// convert volume steps to natural log scale
379
380// change this value to change volume scaling
Andy Hungacd955a2024-02-16 17:14:36 -0800381constexpr float kdbPerStep = 0.5f;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800382// shouldn't need to touch these
Andy Hungacd955a2024-02-16 17:14:36 -0800383constexpr float kdBConvert = -kdbPerStep * 2.302585093f / 20.0f;
384constexpr float kdBConvertInverse = 1.0f / kdBConvert;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800385
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800386float AudioSystem::linearToLog(int volume) {
Andy Hungacd955a2024-02-16 17:14:36 -0800387 // float v = volume ? exp(float(100 - volume) * kdBConvert) : 0;
Steve Blockb8a80522011-12-20 16:23:08 +0000388 // ALOGD("linearToLog(%d)=%f", volume, v);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800389 // return v;
Andy Hungacd955a2024-02-16 17:14:36 -0800390 return volume ? exp(float(100 - volume) * kdBConvert) : 0;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800391}
392
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800393int AudioSystem::logToLinear(float volume) {
Andy Hungacd955a2024-02-16 17:14:36 -0800394 // int v = volume ? 100 - int(kdBConvertInverse * log(volume) + 0.5) : 0;
Steve Blockb8a80522011-12-20 16:23:08 +0000395 // ALOGD("logTolinear(%d)=%f", v, volume);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800396 // return v;
Andy Hungacd955a2024-02-16 17:14:36 -0800397 return volume ? 100 - int(kdBConvertInverse * log(volume) + 0.5) : 0;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800398}
399
Eric Laurent21da6472017-11-09 16:29:26 -0800400/* static */ size_t AudioSystem::calculateMinFrameCount(
401 uint32_t afLatencyMs, uint32_t afFrameCount, uint32_t afSampleRate,
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800402 uint32_t sampleRate, float speed /*, uint32_t notificationsPerBufferReq*/) {
Eric Laurent21da6472017-11-09 16:29:26 -0800403 // Ensure that buffer depth covers at least audio hardware latency
404 uint32_t minBufCount = afLatencyMs / ((1000 * afFrameCount) / afSampleRate);
405 if (minBufCount < 2) {
406 minBufCount = 2;
407 }
408#if 0
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800409 // The notificationsPerBufferReq parameter is not yet used for non-fast tracks,
410 // but keeping the code here to make it easier to add later.
411 if (minBufCount < notificationsPerBufferReq) {
412 minBufCount = notificationsPerBufferReq;
413 }
Eric Laurent21da6472017-11-09 16:29:26 -0800414#endif
415 ALOGV("calculateMinFrameCount afLatency %u afFrameCount %u afSampleRate %u "
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800416 "sampleRate %u speed %f minBufCount: %u" /*" notificationsPerBufferReq %u"*/,
417 afLatencyMs, afFrameCount, afSampleRate, sampleRate, speed, minBufCount
418 /*, notificationsPerBufferReq*/);
Eric Laurent21da6472017-11-09 16:29:26 -0800419 return minBufCount * sourceFramesNeededWithTimestretch(
420 sampleRate, afFrameCount, afSampleRate, speed);
421}
422
423
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800424status_t
425AudioSystem::getOutputSamplingRate(uint32_t* samplingRate, audio_stream_type_t streamType) {
Eric Laurentc2f1f072009-07-17 12:17:14 -0700426 audio_io_handle_t output;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800427
Dima Zavinfce7a472011-04-19 22:30:36 -0700428 if (streamType == AUDIO_STREAM_DEFAULT) {
429 streamType = AUDIO_STREAM_MUSIC;
Eric Laurentc2f1f072009-07-17 12:17:14 -0700430 }
431
Glenn Kastenfff6d712012-01-12 16:38:12 -0800432 output = getOutput(streamType);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700433 if (output == 0) {
434 return PERMISSION_DENIED;
435 }
436
Jean-Michel Trivib7f24b12014-06-11 10:05:30 -0700437 return getSamplingRate(output, samplingRate);
Eric Laurent1a9ed112012-03-20 18:36:01 -0700438}
439
Glenn Kasten2c073da2016-02-26 09:14:08 -0800440status_t AudioSystem::getSamplingRate(audio_io_handle_t ioHandle,
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800441 uint32_t* samplingRate) {
Andy Hung264fa4c2024-02-21 15:52:12 -0800442 const sp<IAudioFlinger> af = get_audio_flinger();
Eric Laurentf6778fd2014-11-18 17:26:58 -0800443 if (af == 0) return PERMISSION_DENIED;
Glenn Kasten2c073da2016-02-26 09:14:08 -0800444 sp<AudioIoDescriptor> desc = getIoDescriptor(ioHandle);
445 if (desc == 0) {
446 *samplingRate = af->sampleRate(ioHandle);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700447 } else {
Mikhail Naganov88536df2021-07-26 17:30:29 -0700448 *samplingRate = desc->getSamplingRate();
Eric Laurentc2f1f072009-07-17 12:17:14 -0700449 }
Glenn Kastenf94006c2014-01-08 08:56:06 -0800450 if (*samplingRate == 0) {
Glenn Kasten2c073da2016-02-26 09:14:08 -0800451 ALOGE("AudioSystem::getSamplingRate failed for ioHandle %d", ioHandle);
Glenn Kastenf94006c2014-01-08 08:56:06 -0800452 return BAD_VALUE;
453 }
Eric Laurentc2f1f072009-07-17 12:17:14 -0700454
Glenn Kasten2c073da2016-02-26 09:14:08 -0800455 ALOGV("getSamplingRate() ioHandle %d, sampling rate %u", ioHandle, *samplingRate);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700456
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800457 return NO_ERROR;
458}
459
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800460status_t AudioSystem::getOutputFrameCount(size_t* frameCount, audio_stream_type_t streamType) {
Eric Laurentc2f1f072009-07-17 12:17:14 -0700461 audio_io_handle_t output;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800462
Dima Zavinfce7a472011-04-19 22:30:36 -0700463 if (streamType == AUDIO_STREAM_DEFAULT) {
464 streamType = AUDIO_STREAM_MUSIC;
Eric Laurentc2f1f072009-07-17 12:17:14 -0700465 }
Eric Laurent48f7f5e2009-04-02 09:32:43 -0700466
Glenn Kastenfff6d712012-01-12 16:38:12 -0800467 output = getOutput(streamType);
Glenn Kasten142f5192014-03-25 17:44:59 -0700468 if (output == AUDIO_IO_HANDLE_NONE) {
Eric Laurentc2f1f072009-07-17 12:17:14 -0700469 return PERMISSION_DENIED;
470 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800471
Jean-Michel Trivib7f24b12014-06-11 10:05:30 -0700472 return getFrameCount(output, frameCount);
Eric Laurent1a9ed112012-03-20 18:36:01 -0700473}
474
Glenn Kasten2c073da2016-02-26 09:14:08 -0800475status_t AudioSystem::getFrameCount(audio_io_handle_t ioHandle,
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800476 size_t* frameCount) {
Andy Hung264fa4c2024-02-21 15:52:12 -0800477 const sp<IAudioFlinger> af = get_audio_flinger();
Eric Laurentf6778fd2014-11-18 17:26:58 -0800478 if (af == 0) return PERMISSION_DENIED;
Glenn Kasten2c073da2016-02-26 09:14:08 -0800479 sp<AudioIoDescriptor> desc = getIoDescriptor(ioHandle);
480 if (desc == 0) {
481 *frameCount = af->frameCount(ioHandle);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700482 } else {
Mikhail Naganov88536df2021-07-26 17:30:29 -0700483 *frameCount = desc->getFrameCount();
Eric Laurentc2f1f072009-07-17 12:17:14 -0700484 }
Glenn Kastenf94006c2014-01-08 08:56:06 -0800485 if (*frameCount == 0) {
Glenn Kasten2c073da2016-02-26 09:14:08 -0800486 ALOGE("AudioSystem::getFrameCount failed for ioHandle %d", ioHandle);
Glenn Kastenf94006c2014-01-08 08:56:06 -0800487 return BAD_VALUE;
488 }
Eric Laurentc2f1f072009-07-17 12:17:14 -0700489
Glenn Kasten2c073da2016-02-26 09:14:08 -0800490 ALOGV("getFrameCount() ioHandle %d, frameCount %zu", ioHandle, *frameCount);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700491
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800492 return NO_ERROR;
493}
494
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800495status_t AudioSystem::getOutputLatency(uint32_t* latency, audio_stream_type_t streamType) {
Eric Laurentc2f1f072009-07-17 12:17:14 -0700496 audio_io_handle_t output;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800497
Dima Zavinfce7a472011-04-19 22:30:36 -0700498 if (streamType == AUDIO_STREAM_DEFAULT) {
499 streamType = AUDIO_STREAM_MUSIC;
Eric Laurentc2f1f072009-07-17 12:17:14 -0700500 }
Eric Laurent48f7f5e2009-04-02 09:32:43 -0700501
Glenn Kastenfff6d712012-01-12 16:38:12 -0800502 output = getOutput(streamType);
Glenn Kasten142f5192014-03-25 17:44:59 -0700503 if (output == AUDIO_IO_HANDLE_NONE) {
Eric Laurentc2f1f072009-07-17 12:17:14 -0700504 return PERMISSION_DENIED;
505 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800506
Glenn Kasten241618f2014-03-25 17:48:57 -0700507 return getLatency(output, latency);
Eric Laurent1a9ed112012-03-20 18:36:01 -0700508}
509
510status_t AudioSystem::getLatency(audio_io_handle_t output,
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800511 uint32_t* latency) {
Andy Hung264fa4c2024-02-21 15:52:12 -0800512 const sp<IAudioFlinger> af = get_audio_flinger();
Eric Laurentf6778fd2014-11-18 17:26:58 -0800513 if (af == 0) return PERMISSION_DENIED;
Eric Laurent296fb132015-05-01 11:38:42 -0700514 sp<AudioIoDescriptor> outputDesc = getIoDescriptor(output);
Eric Laurent73e26b62015-04-27 16:55:58 -0700515 if (outputDesc == 0) {
Eric Laurentc2f1f072009-07-17 12:17:14 -0700516 *latency = af->latency(output);
517 } else {
Mikhail Naganov88536df2021-07-26 17:30:29 -0700518 *latency = outputDesc->getLatency();
Eric Laurentc2f1f072009-07-17 12:17:14 -0700519 }
520
Glenn Kasten241618f2014-03-25 17:48:57 -0700521 ALOGV("getLatency() output %d, latency %d", output, *latency);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700522
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800523 return NO_ERROR;
524}
525
Glenn Kastendd8104c2012-07-02 12:42:44 -0700526status_t AudioSystem::getInputBufferSize(uint32_t sampleRate, audio_format_t format,
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800527 audio_channel_mask_t channelMask, size_t* buffSize) {
Eric Laurent296fb132015-05-01 11:38:42 -0700528 const sp<AudioFlingerClient> afc = getAudioFlingerClient();
529 if (afc == 0) {
530 return NO_INIT;
531 }
532 return afc->getInputBufferSize(sampleRate, format, channelMask, buffSize);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800533}
534
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800535status_t AudioSystem::setVoiceVolume(float value) {
Andy Hung264fa4c2024-02-21 15:52:12 -0800536 const sp<IAudioFlinger> af = get_audio_flinger();
Eric Laurentf0ee6f42009-10-21 08:14:22 -0700537 if (af == 0) return PERMISSION_DENIED;
538 return af->setVoiceVolume(value);
539}
540
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800541status_t AudioSystem::getRenderPosition(audio_io_handle_t output, uint32_t* halFrames,
542 uint32_t* dspFrames) {
Andy Hung264fa4c2024-02-21 15:52:12 -0800543 const sp<IAudioFlinger> af = get_audio_flinger();
Eric Laurent342e9cf2010-01-19 17:37:09 -0800544 if (af == 0) return PERMISSION_DENIED;
545
Richard Fitzgeraldad3af332013-03-25 16:54:37 +0000546 return af->getRenderPosition(halFrames, dspFrames, output);
Eric Laurent342e9cf2010-01-19 17:37:09 -0800547}
548
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800549uint32_t AudioSystem::getInputFramesLost(audio_io_handle_t ioHandle) {
Andy Hung264fa4c2024-02-21 15:52:12 -0800550 const sp<IAudioFlinger> af = get_audio_flinger();
Glenn Kasten5f972c02014-01-13 09:59:31 -0800551 uint32_t result = 0;
Eric Laurent05bca2f2010-02-26 02:47:27 -0800552 if (af == 0) return result;
Glenn Kasten142f5192014-03-25 17:44:59 -0700553 if (ioHandle == AUDIO_IO_HANDLE_NONE) return result;
Eric Laurent05bca2f2010-02-26 02:47:27 -0800554
555 result = af->getInputFramesLost(ioHandle);
556 return result;
557}
558
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800559audio_unique_id_t AudioSystem::newAudioUniqueId(audio_unique_id_use_t use) {
Mikhail Naganov2996f672019-04-18 12:29:59 -0700560 // Must not use AF as IDs will re-roll on audioserver restart, b/130369529.
Andy Hung264fa4c2024-02-21 15:52:12 -0800561 const sp<IAudioFlinger> af = get_audio_flinger();
Eric Laurentde3f8392014-07-27 18:38:22 -0700562 if (af == 0) return AUDIO_UNIQUE_ID_ALLOCATE;
Glenn Kasteneeecb982016-02-26 10:44:04 -0800563 return af->newAudioUniqueId(use);
Eric Laurentbe916aa2010-06-01 23:49:17 -0700564}
565
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800566void AudioSystem::acquireAudioSessionId(audio_session_t audioSession, pid_t pid, uid_t uid) {
Andy Hung264fa4c2024-02-21 15:52:12 -0800567 const sp<IAudioFlinger> af = get_audio_flinger();
Marco Nelissen3a34bef2011-08-02 13:33:41 -0700568 if (af != 0) {
Andy Hung8b0bfd92019-12-23 13:11:11 -0800569 af->acquireAudioSessionId(audioSession, pid, uid);
Marco Nelissen3a34bef2011-08-02 13:33:41 -0700570 }
571}
572
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800573void AudioSystem::releaseAudioSessionId(audio_session_t audioSession, pid_t pid) {
Andy Hung264fa4c2024-02-21 15:52:12 -0800574 const sp<IAudioFlinger> af = get_audio_flinger();
Marco Nelissen3a34bef2011-08-02 13:33:41 -0700575 if (af != 0) {
Marco Nelissend457c972014-02-11 08:47:07 -0800576 af->releaseAudioSessionId(audioSession, pid);
Marco Nelissen3a34bef2011-08-02 13:33:41 -0700577 }
578}
579
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800580audio_hw_sync_t AudioSystem::getAudioHwSyncForSession(audio_session_t sessionId) {
Andy Hung264fa4c2024-02-21 15:52:12 -0800581 const sp<IAudioFlinger> af = get_audio_flinger();
Eric Laurent93c3d412014-08-01 14:48:35 -0700582 if (af == 0) return AUDIO_HW_SYNC_INVALID;
583 return af->getAudioHwSyncForSession(sessionId);
584}
585
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800586status_t AudioSystem::systemReady() {
Andy Hung264fa4c2024-02-21 15:52:12 -0800587 const sp<IAudioFlinger> af = get_audio_flinger();
Eric Laurent72e3f392015-05-20 14:43:50 -0700588 if (af == 0) return NO_INIT;
589 return af->systemReady();
590}
591
Eric Laurentd66d7a12021-07-13 13:35:32 +0200592status_t AudioSystem::audioPolicyReady() {
Andy Hung264fa4c2024-02-21 15:52:12 -0800593 const sp<IAudioFlinger> af = get_audio_flinger();
Eric Laurentd66d7a12021-07-13 13:35:32 +0200594 if (af == 0) return NO_INIT;
595 return af->audioPolicyReady();
596}
597
Glenn Kasten4a8308b2016-04-18 14:10:01 -0700598status_t AudioSystem::getFrameCountHAL(audio_io_handle_t ioHandle,
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800599 size_t* frameCount) {
Andy Hung264fa4c2024-02-21 15:52:12 -0800600 const sp<IAudioFlinger> af = get_audio_flinger();
Glenn Kasten4a8308b2016-04-18 14:10:01 -0700601 if (af == 0) return PERMISSION_DENIED;
602 sp<AudioIoDescriptor> desc = getIoDescriptor(ioHandle);
603 if (desc == 0) {
604 *frameCount = af->frameCountHAL(ioHandle);
605 } else {
Mikhail Naganov88536df2021-07-26 17:30:29 -0700606 *frameCount = desc->getFrameCountHAL();
Glenn Kasten4a8308b2016-04-18 14:10:01 -0700607 }
608 if (*frameCount == 0) {
609 ALOGE("AudioSystem::getFrameCountHAL failed for ioHandle %d", ioHandle);
610 return BAD_VALUE;
611 }
612
613 ALOGV("getFrameCountHAL() ioHandle %d, frameCount %zu", ioHandle, *frameCount);
614
615 return NO_ERROR;
616}
617
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800618// ---------------------------------------------------------------------------
619
Eric Laurent73e26b62015-04-27 16:55:58 -0700620
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800621void AudioSystem::AudioFlingerClient::clearIoCache() {
Andy Hungacd955a2024-02-16 17:14:36 -0800622 std::lock_guard _l(mMutex);
Eric Laurent73e26b62015-04-27 16:55:58 -0700623 mIoDescriptors.clear();
624 mInBuffSize = 0;
625 mInSamplingRate = 0;
626 mInFormat = AUDIO_FORMAT_DEFAULT;
627 mInChannelMask = AUDIO_CHANNEL_NONE;
628}
629
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800630void AudioSystem::AudioFlingerClient::binderDied(const wp<IBinder>& who __unused) {
Andy Hungda1fb072024-02-20 19:08:08 -0800631 gAudioFlingerServiceHandler.clearService();
Ytai Ben-Tsvi000c3e42020-01-09 15:26:40 -0800632 reportError(DEAD_OBJECT);
633
Steve Block5ff1dd52012-01-05 23:22:43 +0000634 ALOGW("AudioFlinger server died!");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800635}
636
Ytai Ben-Tsvi10dc0a62020-09-18 11:31:55 -0700637Status AudioSystem::AudioFlingerClient::ioConfigChanged(
638 media::AudioIoConfigEvent _event,
639 const media::AudioIoDescriptor& _ioDesc) {
Mikhail Naganov88536df2021-07-26 17:30:29 -0700640 audio_io_config_event_t event = VALUE_OR_RETURN_BINDER_STATUS(
641 aidl2legacy_AudioIoConfigEvent_audio_io_config_event_t(_event));
Ytai Ben-Tsvi10dc0a62020-09-18 11:31:55 -0700642 sp<AudioIoDescriptor> ioDesc(
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800643 VALUE_OR_RETURN_BINDER_STATUS(
644 aidl2legacy_AudioIoDescriptor_AudioIoDescriptor(_ioDesc)));
Ytai Ben-Tsvi10dc0a62020-09-18 11:31:55 -0700645
Steve Block3856b092011-10-20 11:56:00 +0100646 ALOGV("ioConfigChanged() event %d", event);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700647
Mikhail Naganov88536df2021-07-26 17:30:29 -0700648 if (ioDesc->getIoHandle() == AUDIO_IO_HANDLE_NONE) return Status::ok();
Eric Laurentc2f1f072009-07-17 12:17:14 -0700649
Eric Laurent296fb132015-05-01 11:38:42 -0700650 audio_port_handle_t deviceId = AUDIO_PORT_HANDLE_NONE;
Eric Laurent09f1ed22019-04-24 17:45:17 -0700651 std::vector<sp<AudioDeviceCallback>> callbacksToCall;
Eric Laurent296fb132015-05-01 11:38:42 -0700652 {
Andy Hungacd955a2024-02-16 17:14:36 -0800653 std::lock_guard _l(mMutex);
Eric Laurent09f1ed22019-04-24 17:45:17 -0700654 auto callbacks = std::map<audio_port_handle_t, wp<AudioDeviceCallback>>();
Eric Laurent296fb132015-05-01 11:38:42 -0700655
656 switch (event) {
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800657 case AUDIO_OUTPUT_OPENED:
658 case AUDIO_OUTPUT_REGISTERED:
659 case AUDIO_INPUT_OPENED:
660 case AUDIO_INPUT_REGISTERED: {
Andy Hungd211ce02024-02-16 15:34:07 -0800661 if (sp<AudioIoDescriptor> oldDesc = getIoDescriptor_l(ioDesc->getIoHandle())) {
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800662 deviceId = oldDesc->getDeviceId();
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800663 }
Andy Hungd211ce02024-02-16 15:34:07 -0800664 mIoDescriptors[ioDesc->getIoHandle()] = ioDesc;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800665
666 if (ioDesc->getDeviceId() != AUDIO_PORT_HANDLE_NONE) {
667 deviceId = ioDesc->getDeviceId();
668 if (event == AUDIO_OUTPUT_OPENED || event == AUDIO_INPUT_OPENED) {
Mikhail Naganov88536df2021-07-26 17:30:29 -0700669 auto it = mAudioDeviceCallbacks.find(ioDesc->getIoHandle());
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800670 if (it != mAudioDeviceCallbacks.end()) {
671 callbacks = it->second;
672 }
673 }
674 }
Mikhail Naganov88536df2021-07-26 17:30:29 -0700675 ALOGV("ioConfigChanged() new %s %s %s",
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800676 event == AUDIO_OUTPUT_OPENED || event == AUDIO_OUTPUT_REGISTERED ?
677 "output" : "input",
678 event == AUDIO_OUTPUT_OPENED || event == AUDIO_INPUT_OPENED ?
679 "opened" : "registered",
Mikhail Naganov88536df2021-07-26 17:30:29 -0700680 ioDesc->toDebugString().c_str());
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800681 }
682 break;
683 case AUDIO_OUTPUT_CLOSED:
684 case AUDIO_INPUT_CLOSED: {
Mikhail Naganov88536df2021-07-26 17:30:29 -0700685 if (getIoDescriptor_l(ioDesc->getIoHandle()) == 0) {
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800686 ALOGW("ioConfigChanged() closing unknown %s %d",
Mikhail Naganov88536df2021-07-26 17:30:29 -0700687 event == AUDIO_OUTPUT_CLOSED ? "output" : "input", ioDesc->getIoHandle());
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800688 break;
689 }
690 ALOGV("ioConfigChanged() %s %d closed",
Mikhail Naganov88536df2021-07-26 17:30:29 -0700691 event == AUDIO_OUTPUT_CLOSED ? "output" : "input", ioDesc->getIoHandle());
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800692
Andy Hungd211ce02024-02-16 15:34:07 -0800693 mIoDescriptors.erase(ioDesc->getIoHandle());
Mikhail Naganov88536df2021-07-26 17:30:29 -0700694 mAudioDeviceCallbacks.erase(ioDesc->getIoHandle());
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800695 }
696 break;
697
698 case AUDIO_OUTPUT_CONFIG_CHANGED:
699 case AUDIO_INPUT_CONFIG_CHANGED: {
Mikhail Naganov88536df2021-07-26 17:30:29 -0700700 sp<AudioIoDescriptor> oldDesc = getIoDescriptor_l(ioDesc->getIoHandle());
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800701 if (oldDesc == 0) {
702 ALOGW("ioConfigChanged() modifying unknown %s! %d",
703 event == AUDIO_OUTPUT_CONFIG_CHANGED ? "output" : "input",
Mikhail Naganov88536df2021-07-26 17:30:29 -0700704 ioDesc->getIoHandle());
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800705 break;
706 }
707
Eric Laurent7c1ec5f2015-07-09 14:52:47 -0700708 deviceId = oldDesc->getDeviceId();
Andy Hungd211ce02024-02-16 15:34:07 -0800709 mIoDescriptors[ioDesc->getIoHandle()] = ioDesc;
Eric Laurent296fb132015-05-01 11:38:42 -0700710
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800711 if (deviceId != ioDesc->getDeviceId()) {
712 deviceId = ioDesc->getDeviceId();
Mikhail Naganov88536df2021-07-26 17:30:29 -0700713 auto it = mAudioDeviceCallbacks.find(ioDesc->getIoHandle());
Eric Laurent09f1ed22019-04-24 17:45:17 -0700714 if (it != mAudioDeviceCallbacks.end()) {
715 callbacks = it->second;
Francois Gaffie24a9fb02019-01-18 17:51:34 +0100716 }
717 }
Mikhail Naganov88536df2021-07-26 17:30:29 -0700718 ALOGV("ioConfigChanged() new config for %s %s",
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800719 event == AUDIO_OUTPUT_CONFIG_CHANGED ? "output" : "input",
Mikhail Naganov88536df2021-07-26 17:30:29 -0700720 ioDesc->toDebugString().c_str());
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800721
Eric Laurent296fb132015-05-01 11:38:42 -0700722 }
Eric Laurent296fb132015-05-01 11:38:42 -0700723 break;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800724 case AUDIO_CLIENT_STARTED: {
Mikhail Naganov88536df2021-07-26 17:30:29 -0700725 sp<AudioIoDescriptor> oldDesc = getIoDescriptor_l(ioDesc->getIoHandle());
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800726 if (oldDesc == 0) {
Mikhail Naganov88536df2021-07-26 17:30:29 -0700727 ALOGW("ioConfigChanged() start client on unknown io! %d",
728 ioDesc->getIoHandle());
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800729 break;
730 }
731 ALOGV("ioConfigChanged() AUDIO_CLIENT_STARTED io %d port %d num callbacks %zu",
Mikhail Naganov88536df2021-07-26 17:30:29 -0700732 ioDesc->getIoHandle(), ioDesc->getPortId(), mAudioDeviceCallbacks.size());
733 oldDesc->setPatch(ioDesc->getPatch());
734 auto it = mAudioDeviceCallbacks.find(ioDesc->getIoHandle());
Eric Laurent09f1ed22019-04-24 17:45:17 -0700735 if (it != mAudioDeviceCallbacks.end()) {
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800736 auto cbks = it->second;
Mikhail Naganov88536df2021-07-26 17:30:29 -0700737 auto it2 = cbks.find(ioDesc->getPortId());
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800738 if (it2 != cbks.end()) {
Mikhail Naganov88536df2021-07-26 17:30:29 -0700739 callbacks.emplace(ioDesc->getPortId(), it2->second);
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800740 deviceId = oldDesc->getDeviceId();
741 }
Eric Laurent09f1ed22019-04-24 17:45:17 -0700742 }
Eric Laurent296fb132015-05-01 11:38:42 -0700743 }
Eric Laurent09f1ed22019-04-24 17:45:17 -0700744 break;
Eric Laurent09f1ed22019-04-24 17:45:17 -0700745 }
746
747 for (auto wpCbk : callbacks) {
748 sp<AudioDeviceCallback> spCbk = wpCbk.second.promote();
749 if (spCbk != nullptr) {
750 callbacksToCall.push_back(spCbk);
751 }
Eric Laurentad2e7b92017-09-14 20:06:42 -0700752 }
Eric Laurent4463ff52019-02-07 13:56:09 -0800753 }
754
Andy Hungacd955a2024-02-16 17:14:36 -0800755 // Callbacks must be called without mMutex held. May lead to dead lock if calling for
756 // example getRoutedDevice that updates the device and tries to acquire mMutex.
Eric Laurent09f1ed22019-04-24 17:45:17 -0700757 for (auto cb : callbacksToCall) {
Mikhail Naganov88536df2021-07-26 17:30:29 -0700758 // If callbacksToCall is not empty, it implies ioDesc->getIoHandle() and deviceId are valid
759 cb->onAudioDeviceUpdate(ioDesc->getIoHandle(), deviceId);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700760 }
Ytai Ben-Tsvi10dc0a62020-09-18 11:31:55 -0700761
762 return Status::ok();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800763}
764
Eric Laurent076e7c72022-05-03 18:12:28 +0200765Status AudioSystem::AudioFlingerClient::onSupportedLatencyModesChanged(
Mikhail Naganovf53e1822022-12-18 02:48:14 +0000766 int output, const std::vector<media::audio::common::AudioLatencyMode>& latencyModes) {
Eric Laurent076e7c72022-05-03 18:12:28 +0200767 audio_io_handle_t outputLegacy = VALUE_OR_RETURN_BINDER_STATUS(
768 aidl2legacy_int32_t_audio_io_handle_t(output));
769 std::vector<audio_latency_mode_t> modesLegacy = VALUE_OR_RETURN_BINDER_STATUS(
770 convertContainer<std::vector<audio_latency_mode_t>>(
Mikhail Naganovf53e1822022-12-18 02:48:14 +0000771 latencyModes, aidl2legacy_AudioLatencyMode_audio_latency_mode_t));
Eric Laurent076e7c72022-05-03 18:12:28 +0200772
773 std::vector<sp<SupportedLatencyModesCallback>> callbacks;
774 {
Andy Hungacd955a2024-02-16 17:14:36 -0800775 std::lock_guard _l(mMutex);
Eric Laurent076e7c72022-05-03 18:12:28 +0200776 for (auto callback : mSupportedLatencyModesCallbacks) {
777 if (auto ref = callback.promote(); ref != nullptr) {
778 callbacks.push_back(ref);
779 }
780 }
781 }
782 for (const auto& callback : callbacks) {
783 callback->onSupportedLatencyModesChanged(outputLegacy, modesLegacy);
784 }
785
786 return Status::ok();
787}
788
Eric Laurent73e26b62015-04-27 16:55:58 -0700789status_t AudioSystem::AudioFlingerClient::getInputBufferSize(
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800790 uint32_t sampleRate, audio_format_t format,
791 audio_channel_mask_t channelMask, size_t* buffSize) {
Andy Hung264fa4c2024-02-21 15:52:12 -0800792 const sp<IAudioFlinger> af = get_audio_flinger();
Eric Laurent73e26b62015-04-27 16:55:58 -0700793 if (af == 0) {
794 return PERMISSION_DENIED;
795 }
Andy Hungacd955a2024-02-16 17:14:36 -0800796 std::lock_guard _l(mMutex);
Eric Laurent73e26b62015-04-27 16:55:58 -0700797 // Do we have a stale mInBuffSize or are we requesting the input buffer size for new values
798 if ((mInBuffSize == 0) || (sampleRate != mInSamplingRate) || (format != mInFormat)
799 || (channelMask != mInChannelMask)) {
800 size_t inBuffSize = af->getInputBufferSize(sampleRate, format, channelMask);
801 if (inBuffSize == 0) {
Glenn Kasten49f36ba2017-12-06 13:02:02 -0800802 ALOGE("AudioSystem::getInputBufferSize failed sampleRate %d format %#x channelMask %#x",
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800803 sampleRate, format, channelMask);
Eric Laurent73e26b62015-04-27 16:55:58 -0700804 return BAD_VALUE;
805 }
806 // A benign race is possible here: we could overwrite a fresher cache entry
807 // save the request params
808 mInSamplingRate = sampleRate;
809 mInFormat = format;
810 mInChannelMask = channelMask;
811
812 mInBuffSize = inBuffSize;
813 }
814
815 *buffSize = mInBuffSize;
816
817 return NO_ERROR;
818}
819
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800820sp<AudioIoDescriptor>
821AudioSystem::AudioFlingerClient::getIoDescriptor_l(audio_io_handle_t ioHandle) {
Andy Hungd211ce02024-02-16 15:34:07 -0800822 if (const auto it = mIoDescriptors.find(ioHandle);
823 it != mIoDescriptors.end()) {
824 return it->second;
Eric Laurent73e26b62015-04-27 16:55:58 -0700825 }
Andy Hungd211ce02024-02-16 15:34:07 -0800826 return {};
Eric Laurent73e26b62015-04-27 16:55:58 -0700827}
828
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800829sp<AudioIoDescriptor> AudioSystem::AudioFlingerClient::getIoDescriptor(audio_io_handle_t ioHandle) {
Andy Hungacd955a2024-02-16 17:14:36 -0800830 std::lock_guard _l(mMutex);
Praveen Chavan49fdeaf2015-09-29 02:25:47 -0700831 return getIoDescriptor_l(ioHandle);
832}
833
Eric Laurent296fb132015-05-01 11:38:42 -0700834status_t AudioSystem::AudioFlingerClient::addAudioDeviceCallback(
Eric Laurent09f1ed22019-04-24 17:45:17 -0700835 const wp<AudioDeviceCallback>& callback, audio_io_handle_t audioIo,
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800836 audio_port_handle_t portId) {
Eric Laurent09f1ed22019-04-24 17:45:17 -0700837 ALOGV("%s audioIo %d portId %d", __func__, audioIo, portId);
Andy Hungacd955a2024-02-16 17:14:36 -0800838 std::lock_guard _l(mMutex);
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800839 auto& callbacks = mAudioDeviceCallbacks.emplace(
840 audioIo,
841 std::map<audio_port_handle_t, wp<AudioDeviceCallback>>()).first->second;
Eric Laurent09f1ed22019-04-24 17:45:17 -0700842 auto result = callbacks.try_emplace(portId, callback);
843 if (!result.second) {
844 return INVALID_OPERATION;
Eric Laurent296fb132015-05-01 11:38:42 -0700845 }
Eric Laurent296fb132015-05-01 11:38:42 -0700846 return NO_ERROR;
847}
848
849status_t AudioSystem::AudioFlingerClient::removeAudioDeviceCallback(
Eric Laurent09f1ed22019-04-24 17:45:17 -0700850 const wp<AudioDeviceCallback>& callback __unused, audio_io_handle_t audioIo,
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800851 audio_port_handle_t portId) {
Eric Laurent09f1ed22019-04-24 17:45:17 -0700852 ALOGV("%s audioIo %d portId %d", __func__, audioIo, portId);
Andy Hungacd955a2024-02-16 17:14:36 -0800853 std::lock_guard _l(mMutex);
Eric Laurent09f1ed22019-04-24 17:45:17 -0700854 auto it = mAudioDeviceCallbacks.find(audioIo);
855 if (it == mAudioDeviceCallbacks.end()) {
Eric Laurent296fb132015-05-01 11:38:42 -0700856 return INVALID_OPERATION;
857 }
Eric Laurent09f1ed22019-04-24 17:45:17 -0700858 if (it->second.erase(portId) == 0) {
Eric Laurent296fb132015-05-01 11:38:42 -0700859 return INVALID_OPERATION;
860 }
Eric Laurent09f1ed22019-04-24 17:45:17 -0700861 if (it->second.size() == 0) {
862 mAudioDeviceCallbacks.erase(audioIo);
Eric Laurent296fb132015-05-01 11:38:42 -0700863 }
864 return NO_ERROR;
865}
866
Eric Laurent076e7c72022-05-03 18:12:28 +0200867status_t AudioSystem::AudioFlingerClient::addSupportedLatencyModesCallback(
868 const sp<SupportedLatencyModesCallback>& callback) {
Andy Hungacd955a2024-02-16 17:14:36 -0800869 std::lock_guard _l(mMutex);
Eric Laurent076e7c72022-05-03 18:12:28 +0200870 if (std::find(mSupportedLatencyModesCallbacks.begin(),
871 mSupportedLatencyModesCallbacks.end(),
872 callback) != mSupportedLatencyModesCallbacks.end()) {
873 return INVALID_OPERATION;
874 }
875 mSupportedLatencyModesCallbacks.push_back(callback);
876 return NO_ERROR;
877}
878
879status_t AudioSystem::AudioFlingerClient::removeSupportedLatencyModesCallback(
880 const sp<SupportedLatencyModesCallback>& callback) {
Andy Hungacd955a2024-02-16 17:14:36 -0800881 std::lock_guard _l(mMutex);
Eric Laurent076e7c72022-05-03 18:12:28 +0200882 auto it = std::find(mSupportedLatencyModesCallbacks.begin(),
883 mSupportedLatencyModesCallbacks.end(),
884 callback);
885 if (it == mSupportedLatencyModesCallbacks.end()) {
886 return INVALID_OPERATION;
887 }
888 mSupportedLatencyModesCallbacks.erase(it);
889 return NO_ERROR;
890}
891
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800892/* static */ uintptr_t AudioSystem::addErrorCallback(audio_error_callback cb) {
Andy Hungacd955a2024-02-16 17:14:36 -0800893 std::lock_guard _l(gErrorCallbacksMutex);
Ytai Ben-Tsvi000c3e42020-01-09 15:26:40 -0800894 gAudioErrorCallbacks.insert(cb);
895 return reinterpret_cast<uintptr_t>(cb);
896}
897
898/* static */ void AudioSystem::removeErrorCallback(uintptr_t cb) {
Andy Hungacd955a2024-02-16 17:14:36 -0800899 std::lock_guard _l(gErrorCallbacksMutex);
Ytai Ben-Tsvi000c3e42020-01-09 15:26:40 -0800900 gAudioErrorCallbacks.erase(reinterpret_cast<audio_error_callback>(cb));
901}
902
903/* static */ void AudioSystem::reportError(status_t err) {
Andy Hungacd955a2024-02-16 17:14:36 -0800904 std::lock_guard _l(gErrorCallbacksMutex);
Ytai Ben-Tsvi000c3e42020-01-09 15:26:40 -0800905 for (auto callback : gAudioErrorCallbacks) {
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800906 callback(err);
Ytai Ben-Tsvi000c3e42020-01-09 15:26:40 -0800907 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800908}
909
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800910/*static*/ void AudioSystem::setDynPolicyCallback(dynamic_policy_callback cb) {
Andy Hungacd955a2024-02-16 17:14:36 -0800911 std::lock_guard _l(gMutex);
Jean-Michel Trivif613d422015-04-23 18:41:29 -0700912 gDynPolicyCallback = cb;
913}
914
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800915/*static*/ void AudioSystem::setRecordConfigCallback(record_config_callback cb) {
Andy Hungacd955a2024-02-16 17:14:36 -0800916 std::lock_guard _l(gMutex);
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -0800917 gRecordConfigCallback = cb;
918}
919
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800920/*static*/ void AudioSystem::setRoutingCallback(routing_callback cb) {
Andy Hungacd955a2024-02-16 17:14:36 -0800921 std::lock_guard _l(gMutex);
Jean-Michel Trivi9a6b9ad2020-10-22 16:46:43 -0700922 gRoutingCallback = cb;
923}
924
Jean-Michel Trivi78f2b302022-04-15 18:18:41 +0000925/*static*/ void AudioSystem::setVolInitReqCallback(vol_range_init_req_callback cb) {
Andy Hungacd955a2024-02-16 17:14:36 -0800926 std::lock_guard _l(gMutex);
Jean-Michel Trivi78f2b302022-04-15 18:18:41 +0000927 gVolRangeInitReqCallback = cb;
928}
929
Andy Hung88e71732024-02-21 16:44:27 -0800930struct AudioPolicyTraits {
931 static void onServiceCreate(const sp<IAudioPolicyService>& ap,
932 const sp<AudioSystem::AudioPolicyServiceClient>& apc) {
933 const int64_t token = IPCThreadState::self()->clearCallingIdentity();
Eric Laurent0ebd5f92014-11-19 19:04:52 -0800934 ap->registerClient(apc);
François Gaffie24437602018-04-23 15:08:59 +0200935 ap->setAudioPortCallbacksEnabled(apc->isAudioPortCbEnabled());
François Gaffiecfe17322018-11-07 13:41:29 +0100936 ap->setAudioVolumeGroupCallbacksEnabled(apc->isAudioVolumeGroupCbEnabled());
François Gaffie24437602018-04-23 15:08:59 +0200937 IPCThreadState::self()->restoreCallingIdentity(token);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700938 }
Glenn Kastend2d089f2014-11-05 11:48:12 -0800939
Andy Hung88e71732024-02-21 16:44:27 -0800940 static void onClearService(const sp<AudioSystem::AudioPolicyServiceClient>&) {}
941
942 static constexpr const char *SERVICE_NAME = "media.audio_policy";
943};
944
945[[clang::no_destroy]] static constinit ServiceHandler<IAudioPolicyService,
946 AudioSystem::AudioPolicyServiceClient, IAudioPolicyService,
947 AudioPolicyTraits> gAudioPolicyServiceHandler;
948
949status_t AudioSystem::setLocalAudioPolicyService(const sp<IAudioPolicyService>& aps) {
950 return gAudioPolicyServiceHandler.setLocalService(aps);
951}
952
953sp<IAudioPolicyService> AudioSystem::get_audio_policy_service() {
954 return gAudioPolicyServiceHandler.getService();
Eric Laurentc2f1f072009-07-17 12:17:14 -0700955}
956
Mikhail Naganovec3d5792022-05-06 00:19:55 +0000957void AudioSystem::clearAudioPolicyService() {
Andy Hung88e71732024-02-21 16:44:27 -0800958 gAudioPolicyServiceHandler.clearService();
Mikhail Naganovec3d5792022-05-06 00:19:55 +0000959}
960
Shraddha Basantwanif2aa2502024-04-08 11:01:57 +0000961void AudioSystem::disableThreadPool() {
962 gAudioFlingerServiceHandler.disableThreadPool();
963 gAudioPolicyServiceHandler.disableThreadPool();
964}
965
Glenn Kastenfb1fdc92013-07-10 17:03:19 -0700966// ---------------------------------------------------------------------------
967
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800968void AudioSystem::onNewAudioModulesAvailable() {
Andy Hung264fa4c2024-02-21 15:52:12 -0800969 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Mikhail Naganov88b30d22020-03-09 19:43:13 +0000970 if (aps == 0) return;
971 aps->onNewAudioModulesAvailable();
972}
973
Nathalie Le Clair88fa2752021-11-23 13:03:41 +0100974status_t AudioSystem::setDeviceConnectionState(audio_policy_dev_state_t state,
975 const android::media::audio::common::AudioPort& port,
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800976 audio_format_t encodedFormat) {
Andy Hung264fa4c2024-02-21 15:52:12 -0800977 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Eric Laurent71b63e32011-09-02 14:20:56 -0700978
Eric Laurentc2f1f072009-07-17 12:17:14 -0700979 if (aps == 0) return PERMISSION_DENIED;
980
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800981 return statusTFromBinderStatus(
982 aps->setDeviceConnectionState(
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800983 VALUE_OR_RETURN_STATUS(
984 legacy2aidl_audio_policy_dev_state_t_AudioPolicyDeviceState(state)),
Nathalie Le Clair88fa2752021-11-23 13:03:41 +0100985 port,
Mikhail Naganovb60bd1b2021-07-15 17:31:43 -0700986 VALUE_OR_RETURN_STATUS(
987 legacy2aidl_audio_format_t_AudioFormatDescription(encodedFormat))));
Eric Laurentc2f1f072009-07-17 12:17:14 -0700988}
989
Dima Zavinfce7a472011-04-19 22:30:36 -0700990audio_policy_dev_state_t AudioSystem::getDeviceConnectionState(audio_devices_t device,
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800991 const char* device_address) {
Andy Hung264fa4c2024-02-21 15:52:12 -0800992 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Dima Zavinfce7a472011-04-19 22:30:36 -0700993 if (aps == 0) return AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE;
Eric Laurentc2f1f072009-07-17 12:17:14 -0700994
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800995 auto result = [&]() -> ConversionResult<audio_policy_dev_state_t> {
Mikhail Naganov932cb962021-09-16 01:05:27 +0000996 AudioDevice deviceAidl = VALUE_OR_RETURN(
997 legacy2aidl_audio_device_AudioDevice(device, device_address));
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800998
999 media::AudioPolicyDeviceState result;
1000 RETURN_IF_ERROR(statusTFromBinderStatus(
1001 aps->getDeviceConnectionState(deviceAidl, &result)));
1002
1003 return aidl2legacy_AudioPolicyDeviceState_audio_policy_dev_state_t(result);
1004 }();
1005 return result.value_or(AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE);
Eric Laurentc2f1f072009-07-17 12:17:14 -07001006}
1007
Pavlin Radoslavovf862bc62016-12-26 18:57:22 -08001008status_t AudioSystem::handleDeviceConfigChange(audio_devices_t device,
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001009 const char* device_address,
1010 const char* device_name,
1011 audio_format_t encodedFormat) {
Andy Hung264fa4c2024-02-21 15:52:12 -08001012 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001013 const char* address = "";
1014 const char* name = "";
Pavlin Radoslavovf862bc62016-12-26 18:57:22 -08001015
1016 if (aps == 0) return PERMISSION_DENIED;
1017
1018 if (device_address != NULL) {
1019 address = device_address;
1020 }
1021 if (device_name != NULL) {
1022 name = device_name;
1023 }
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001024
Mikhail Naganov932cb962021-09-16 01:05:27 +00001025 AudioDevice deviceAidl = VALUE_OR_RETURN_STATUS(
1026 legacy2aidl_audio_device_AudioDevice(device, address));
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001027
1028 return statusTFromBinderStatus(
1029 aps->handleDeviceConfigChange(deviceAidl, name, VALUE_OR_RETURN_STATUS(
Mikhail Naganovb60bd1b2021-07-15 17:31:43 -07001030 legacy2aidl_audio_format_t_AudioFormatDescription(encodedFormat))));
Pavlin Radoslavovf862bc62016-12-26 18:57:22 -08001031}
1032
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001033status_t AudioSystem::setPhoneState(audio_mode_t state, uid_t uid) {
Glenn Kasten347966c2012-01-18 14:58:32 -08001034 if (uint32_t(state) >= AUDIO_MODE_CNT) return BAD_VALUE;
Andy Hung264fa4c2024-02-21 15:52:12 -08001035 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Eric Laurentc2f1f072009-07-17 12:17:14 -07001036 if (aps == 0) return PERMISSION_DENIED;
1037
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001038 return statusTFromBinderStatus(aps->setPhoneState(
1039 VALUE_OR_RETURN_STATUS(legacy2aidl_audio_mode_t_AudioMode(state)),
1040 VALUE_OR_RETURN_STATUS(legacy2aidl_uid_t_int32_t(uid))));
Eric Laurentc2f1f072009-07-17 12:17:14 -07001041}
1042
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001043status_t
1044AudioSystem::setForceUse(audio_policy_force_use_t usage, audio_policy_forced_cfg_t config) {
Andy Hung264fa4c2024-02-21 15:52:12 -08001045 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Eric Laurentc2f1f072009-07-17 12:17:14 -07001046 if (aps == 0) return PERMISSION_DENIED;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001047
1048 return statusTFromBinderStatus(
1049 aps->setForceUse(
1050 VALUE_OR_RETURN_STATUS(
1051 legacy2aidl_audio_policy_force_use_t_AudioPolicyForceUse(usage)),
1052 VALUE_OR_RETURN_STATUS(
1053 legacy2aidl_audio_policy_forced_cfg_t_AudioPolicyForcedConfig(
1054 config))));
Eric Laurentc2f1f072009-07-17 12:17:14 -07001055}
1056
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001057audio_policy_forced_cfg_t AudioSystem::getForceUse(audio_policy_force_use_t usage) {
Andy Hung264fa4c2024-02-21 15:52:12 -08001058 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Dima Zavinfce7a472011-04-19 22:30:36 -07001059 if (aps == 0) return AUDIO_POLICY_FORCE_NONE;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001060
1061 auto result = [&]() -> ConversionResult<audio_policy_forced_cfg_t> {
Mikhail Naganovd8670042024-11-20 21:17:52 +00001062 media::AudioPolicyForceUse usageAidl = VALUE_OR_RETURN(
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001063 legacy2aidl_audio_policy_force_use_t_AudioPolicyForceUse(usage));
Mikhail Naganovd8670042024-11-20 21:17:52 +00001064 media::AudioPolicyForcedConfig configAidl;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001065 RETURN_IF_ERROR(statusTFromBinderStatus(
1066 aps->getForceUse(usageAidl, &configAidl)));
1067 return aidl2legacy_AudioPolicyForcedConfig_audio_policy_forced_cfg_t(configAidl);
1068 }();
1069
1070 return result.value_or(AUDIO_POLICY_FORCE_NONE);
Eric Laurentc2f1f072009-07-17 12:17:14 -07001071}
1072
1073
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001074audio_io_handle_t AudioSystem::getOutput(audio_stream_type_t stream) {
Andy Hung264fa4c2024-02-21 15:52:12 -08001075 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001076 if (aps == 0) return AUDIO_IO_HANDLE_NONE;
1077
1078 auto result = [&]() -> ConversionResult<audio_io_handle_t> {
Mikhail Naganovdbf03642021-08-25 18:15:32 -07001079 AudioStreamType streamAidl = VALUE_OR_RETURN(
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001080 legacy2aidl_audio_stream_type_t_AudioStreamType(stream));
1081 int32_t outputAidl;
1082 RETURN_IF_ERROR(
1083 statusTFromBinderStatus(aps->getOutput(streamAidl, &outputAidl)));
1084 return aidl2legacy_int32_t_audio_io_handle_t(outputAidl);
1085 }();
1086
1087 return result.value_or(AUDIO_IO_HANDLE_NONE);
Eric Laurentc2f1f072009-07-17 12:17:14 -07001088}
1089
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001090status_t AudioSystem::getOutputForAttr(audio_attributes_t* attr,
1091 audio_io_handle_t* output,
1092 audio_session_t session,
1093 audio_stream_type_t* stream,
Svet Ganov3e5f14f2021-05-13 22:51:08 +00001094 const AttributionSourceState& attributionSource,
jiabinf1c73972022-04-14 16:28:52 -07001095 audio_config_t* config,
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001096 audio_output_flags_t flags,
1097 audio_port_handle_t* selectedDeviceId,
1098 audio_port_handle_t* portId,
Eric Laurentb0a7bc92022-04-05 15:06:08 +02001099 std::vector<audio_io_handle_t>* secondaryOutputs,
jiabinc658e452022-10-21 20:52:21 +00001100 bool *isSpatialized,
1101 bool *isBitPerfect) {
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001102 if (attr == nullptr) {
1103 ALOGE("%s NULL audio attributes", __func__);
1104 return BAD_VALUE;
1105 }
1106 if (output == nullptr) {
1107 ALOGE("%s NULL output - shouldn't happen", __func__);
1108 return BAD_VALUE;
1109 }
1110 if (selectedDeviceId == nullptr) {
1111 ALOGE("%s NULL selectedDeviceId - shouldn't happen", __func__);
1112 return BAD_VALUE;
1113 }
1114 if (portId == nullptr) {
1115 ALOGE("%s NULL portId - shouldn't happen", __func__);
1116 return BAD_VALUE;
1117 }
1118 if (secondaryOutputs == nullptr) {
1119 ALOGE("%s NULL secondaryOutputs - shouldn't happen", __func__);
1120 return BAD_VALUE;
1121 }
1122
Andy Hung264fa4c2024-02-21 15:52:12 -08001123 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Eric Laurente83b55d2014-11-14 10:06:21 -08001124 if (aps == 0) return NO_INIT;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001125
Mikhail Naganov1c400902023-05-17 11:48:43 -07001126 media::audio::common::AudioAttributes attrAidl = VALUE_OR_RETURN_STATUS(
1127 legacy2aidl_audio_attributes_t_AudioAttributes(*attr));
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001128 int32_t sessionAidl = VALUE_OR_RETURN_STATUS(legacy2aidl_audio_session_t_int32_t(session));
Mikhail Naganovdbf03642021-08-25 18:15:32 -07001129 AudioConfig configAidl = VALUE_OR_RETURN_STATUS(
Mikhail Naganovde3fa182021-07-30 15:06:42 -07001130 legacy2aidl_audio_config_t_AudioConfig(*config, false /*isInput*/));
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001131 int32_t flagsAidl = VALUE_OR_RETURN_STATUS(
1132 legacy2aidl_audio_output_flags_t_int32_t_mask(flags));
Eric Laurentf99edd32021-02-01 15:57:33 +01001133 int32_t selectedDeviceIdAidl = VALUE_OR_RETURN_STATUS(
1134 legacy2aidl_audio_port_handle_t_int32_t(*selectedDeviceId));
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001135
1136 media::GetOutputForAttrResponse responseAidl;
1137
jiabinf1c73972022-04-14 16:28:52 -07001138 status_t status = statusTFromBinderStatus(
Svet Ganov3e5f14f2021-05-13 22:51:08 +00001139 aps->getOutputForAttr(attrAidl, sessionAidl, attributionSource, configAidl, flagsAidl,
jiabinf1c73972022-04-14 16:28:52 -07001140 selectedDeviceIdAidl, &responseAidl));
1141 if (status != NO_ERROR) {
1142 config->format = VALUE_OR_RETURN_STATUS(
1143 aidl2legacy_AudioFormatDescription_audio_format_t(responseAidl.configBase.format));
1144 config->channel_mask = VALUE_OR_RETURN_STATUS(
1145 aidl2legacy_AudioChannelLayout_audio_channel_mask_t(
1146 responseAidl.configBase.channelMask, false /*isInput*/));
1147 config->sample_rate = responseAidl.configBase.sampleRate;
1148 return status;
1149 }
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001150
1151 *output = VALUE_OR_RETURN_STATUS(
1152 aidl2legacy_int32_t_audio_io_handle_t(responseAidl.output));
1153
1154 if (stream != nullptr) {
1155 *stream = VALUE_OR_RETURN_STATUS(
1156 aidl2legacy_AudioStreamType_audio_stream_type_t(responseAidl.stream));
1157 }
1158 *selectedDeviceId = VALUE_OR_RETURN_STATUS(
1159 aidl2legacy_int32_t_audio_port_handle_t(responseAidl.selectedDeviceId));
1160 *portId = VALUE_OR_RETURN_STATUS(aidl2legacy_int32_t_audio_port_handle_t(responseAidl.portId));
1161 *secondaryOutputs = VALUE_OR_RETURN_STATUS(convertContainer<std::vector<audio_io_handle_t>>(
1162 responseAidl.secondaryOutputs, aidl2legacy_int32_t_audio_io_handle_t));
Eric Laurentb0a7bc92022-04-05 15:06:08 +02001163 *isSpatialized = responseAidl.isSpatialized;
jiabinc658e452022-10-21 20:52:21 +00001164 *isBitPerfect = responseAidl.isBitPerfect;
Eric Laurent0d13fea2022-11-04 17:12:08 +01001165 *attr = VALUE_OR_RETURN_STATUS(
Mikhail Naganov1c400902023-05-17 11:48:43 -07001166 aidl2legacy_AudioAttributes_audio_attributes_t(responseAidl.attr));
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001167
1168 return OK;
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -07001169}
1170
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001171status_t AudioSystem::startOutput(audio_port_handle_t portId) {
Andy Hung264fa4c2024-02-21 15:52:12 -08001172 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Eric Laurentc2f1f072009-07-17 12:17:14 -07001173 if (aps == 0) return PERMISSION_DENIED;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001174
1175 int32_t portIdAidl = VALUE_OR_RETURN_STATUS(legacy2aidl_audio_port_handle_t_int32_t(portId));
1176 return statusTFromBinderStatus(aps->startOutput(portIdAidl));
Eric Laurentc2f1f072009-07-17 12:17:14 -07001177}
1178
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001179status_t AudioSystem::stopOutput(audio_port_handle_t portId) {
Andy Hung264fa4c2024-02-21 15:52:12 -08001180 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Eric Laurentc2f1f072009-07-17 12:17:14 -07001181 if (aps == 0) return PERMISSION_DENIED;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001182
1183 int32_t portIdAidl = VALUE_OR_RETURN_STATUS(legacy2aidl_audio_port_handle_t_int32_t(portId));
1184 return statusTFromBinderStatus(aps->stopOutput(portIdAidl));
Eric Laurentc2f1f072009-07-17 12:17:14 -07001185}
1186
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001187void AudioSystem::releaseOutput(audio_port_handle_t portId) {
Andy Hung264fa4c2024-02-21 15:52:12 -08001188 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Eric Laurentc2f1f072009-07-17 12:17:14 -07001189 if (aps == 0) return;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001190
1191 auto status = [&]() -> status_t {
1192 int32_t portIdAidl = VALUE_OR_RETURN_STATUS(
1193 legacy2aidl_audio_port_handle_t_int32_t(portId));
1194 RETURN_STATUS_IF_ERROR(statusTFromBinderStatus(aps->releaseOutput(portIdAidl)));
1195 return OK;
1196 }();
1197
1198 // Ignore status.
1199 (void) status;
Eric Laurentc2f1f072009-07-17 12:17:14 -07001200}
1201
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001202status_t AudioSystem::getInputForAttr(const audio_attributes_t* attr,
1203 audio_io_handle_t* input,
1204 audio_unique_id_t riid,
1205 audio_session_t session,
Svet Ganov3e5f14f2021-05-13 22:51:08 +00001206 const AttributionSourceState &attributionSource,
jiabinf1c73972022-04-14 16:28:52 -07001207 audio_config_base_t* config,
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001208 audio_input_flags_t flags,
1209 audio_port_handle_t* selectedDeviceId,
1210 audio_port_handle_t* portId) {
1211 if (attr == NULL) {
1212 ALOGE("getInputForAttr NULL attr - shouldn't happen");
1213 return BAD_VALUE;
1214 }
1215 if (input == NULL) {
1216 ALOGE("getInputForAttr NULL input - shouldn't happen");
1217 return BAD_VALUE;
1218 }
1219 if (selectedDeviceId == NULL) {
1220 ALOGE("getInputForAttr NULL selectedDeviceId - shouldn't happen");
1221 return BAD_VALUE;
1222 }
1223 if (portId == NULL) {
1224 ALOGE("getInputForAttr NULL portId - shouldn't happen");
1225 return BAD_VALUE;
1226 }
1227
Andy Hung264fa4c2024-02-21 15:52:12 -08001228 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Eric Laurentcaf7f482014-11-25 17:50:47 -08001229 if (aps == 0) return NO_INIT;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001230
Mikhail Naganov1c400902023-05-17 11:48:43 -07001231 media::audio::common::AudioAttributes attrAidl = VALUE_OR_RETURN_STATUS(
1232 legacy2aidl_audio_attributes_t_AudioAttributes(*attr));
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001233 int32_t inputAidl = VALUE_OR_RETURN_STATUS(legacy2aidl_audio_io_handle_t_int32_t(*input));
1234 int32_t riidAidl = VALUE_OR_RETURN_STATUS(legacy2aidl_audio_unique_id_t_int32_t(riid));
1235 int32_t sessionAidl = VALUE_OR_RETURN_STATUS(legacy2aidl_audio_session_t_int32_t(session));
Mikhail Naganovdbf03642021-08-25 18:15:32 -07001236 AudioConfigBase configAidl = VALUE_OR_RETURN_STATUS(
Mikhail Naganovde3fa182021-07-30 15:06:42 -07001237 legacy2aidl_audio_config_base_t_AudioConfigBase(*config, true /*isInput*/));
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001238 int32_t flagsAidl = VALUE_OR_RETURN_STATUS(legacy2aidl_audio_input_flags_t_int32_t_mask(flags));
Eric Laurentf99edd32021-02-01 15:57:33 +01001239 int32_t selectedDeviceIdAidl = VALUE_OR_RETURN_STATUS(
1240 legacy2aidl_audio_port_handle_t_int32_t(*selectedDeviceId));
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001241
1242 media::GetInputForAttrResponse response;
1243
jiabinf1c73972022-04-14 16:28:52 -07001244 status_t status = statusTFromBinderStatus(
Svet Ganov3e5f14f2021-05-13 22:51:08 +00001245 aps->getInputForAttr(attrAidl, inputAidl, riidAidl, sessionAidl, attributionSource,
jiabinf1c73972022-04-14 16:28:52 -07001246 configAidl, flagsAidl, selectedDeviceIdAidl, &response));
1247 if (status != NO_ERROR) {
1248 *config = VALUE_OR_RETURN_STATUS(
1249 aidl2legacy_AudioConfigBase_audio_config_base_t(response.config, true /*isInput*/));
1250 return status;
1251 }
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001252
1253 *input = VALUE_OR_RETURN_STATUS(aidl2legacy_int32_t_audio_io_handle_t(response.input));
1254 *selectedDeviceId = VALUE_OR_RETURN_STATUS(
1255 aidl2legacy_int32_t_audio_port_handle_t(response.selectedDeviceId));
1256 *portId = VALUE_OR_RETURN_STATUS(aidl2legacy_int32_t_audio_port_handle_t(response.portId));
1257
1258 return OK;
Eric Laurentc2f1f072009-07-17 12:17:14 -07001259}
1260
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001261status_t AudioSystem::startInput(audio_port_handle_t portId) {
Andy Hung264fa4c2024-02-21 15:52:12 -08001262 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Eric Laurentc2f1f072009-07-17 12:17:14 -07001263 if (aps == 0) return PERMISSION_DENIED;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001264
1265 int32_t portIdAidl = VALUE_OR_RETURN_STATUS(legacy2aidl_audio_port_handle_t_int32_t(portId));
1266 return statusTFromBinderStatus(aps->startInput(portIdAidl));
Eric Laurentc2f1f072009-07-17 12:17:14 -07001267}
1268
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001269status_t AudioSystem::stopInput(audio_port_handle_t portId) {
Andy Hung264fa4c2024-02-21 15:52:12 -08001270 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Eric Laurentc2f1f072009-07-17 12:17:14 -07001271 if (aps == 0) return PERMISSION_DENIED;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001272
1273 int32_t portIdAidl = VALUE_OR_RETURN_STATUS(legacy2aidl_audio_port_handle_t_int32_t(portId));
1274 return statusTFromBinderStatus(aps->stopInput(portIdAidl));
Eric Laurentc2f1f072009-07-17 12:17:14 -07001275}
1276
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001277void AudioSystem::releaseInput(audio_port_handle_t portId) {
Andy Hung264fa4c2024-02-21 15:52:12 -08001278 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Eric Laurentc2f1f072009-07-17 12:17:14 -07001279 if (aps == 0) return;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001280
1281 auto status = [&]() -> status_t {
1282 int32_t portIdAidl = VALUE_OR_RETURN_STATUS(
1283 legacy2aidl_audio_port_handle_t_int32_t(portId));
1284 RETURN_STATUS_IF_ERROR(statusTFromBinderStatus(aps->releaseInput(portIdAidl)));
1285 return OK;
1286 }();
1287
1288 // Ignore status.
1289 (void) status;
Eric Laurentc2f1f072009-07-17 12:17:14 -07001290}
1291
Vlad Popa87e0e582024-05-20 18:49:20 -07001292status_t AudioSystem::setDeviceAbsoluteVolumeEnabled(audio_devices_t deviceType,
1293 const char *address,
1294 bool enabled,
1295 audio_stream_type_t streamToDriveAbs) {
1296 const sp<IAudioPolicyService> aps = get_audio_policy_service();
1297 if (aps == nullptr) return PERMISSION_DENIED;
1298
1299 AudioDevice deviceAidl = VALUE_OR_RETURN_STATUS(
1300 legacy2aidl_audio_device_AudioDevice(deviceType, address));
1301 AudioStreamType streamToDriveAbsAidl = VALUE_OR_RETURN_STATUS(
1302 legacy2aidl_audio_stream_type_t_AudioStreamType(streamToDriveAbs));
1303 return statusTFromBinderStatus(
1304 aps->setDeviceAbsoluteVolumeEnabled(deviceAidl, enabled, streamToDriveAbsAidl));
1305}
1306
Dima Zavinfce7a472011-04-19 22:30:36 -07001307status_t AudioSystem::initStreamVolume(audio_stream_type_t stream,
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001308 int indexMin,
1309 int indexMax) {
Andy Hung264fa4c2024-02-21 15:52:12 -08001310 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Eric Laurentc2f1f072009-07-17 12:17:14 -07001311 if (aps == 0) return PERMISSION_DENIED;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001312
Mikhail Naganovdbf03642021-08-25 18:15:32 -07001313 AudioStreamType streamAidl = VALUE_OR_RETURN_STATUS(
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001314 legacy2aidl_audio_stream_type_t_AudioStreamType(stream));
1315 int32_t indexMinAidl = VALUE_OR_RETURN_STATUS(convertIntegral<int32_t>(indexMin));
1316 int32_t indexMaxAidl = VALUE_OR_RETURN_STATUS(convertIntegral<int32_t>(indexMax));
Mikhail Naganovec3d5792022-05-06 00:19:55 +00001317 status_t status = statusTFromBinderStatus(
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001318 aps->initStreamVolume(streamAidl, indexMinAidl, indexMaxAidl));
Mikhail Naganovec3d5792022-05-06 00:19:55 +00001319 if (status == DEAD_OBJECT) {
1320 // This is a critical operation since w/o proper stream volumes no audio
1321 // will be heard. Make sure we recover from a failure in any case.
1322 ALOGE("Received DEAD_OBJECT from APS, clearing the client");
1323 clearAudioPolicyService();
1324 }
1325 return status;
Eric Laurentc2f1f072009-07-17 12:17:14 -07001326}
1327
Eric Laurent83844cc2011-11-18 16:43:31 -08001328status_t AudioSystem::setStreamVolumeIndex(audio_stream_type_t stream,
1329 int index,
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001330 audio_devices_t device) {
Andy Hung264fa4c2024-02-21 15:52:12 -08001331 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Eric Laurentc2f1f072009-07-17 12:17:14 -07001332 if (aps == 0) return PERMISSION_DENIED;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001333
Mikhail Naganovdbf03642021-08-25 18:15:32 -07001334 AudioStreamType streamAidl = VALUE_OR_RETURN_STATUS(
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001335 legacy2aidl_audio_stream_type_t_AudioStreamType(stream));
1336 int32_t indexAidl = VALUE_OR_RETURN_STATUS(convertIntegral<int32_t>(index));
Mikhail Naganovf4a75362021-09-16 00:02:54 +00001337 AudioDeviceDescription deviceAidl = VALUE_OR_RETURN_STATUS(
Mikhail Naganov21a32ec2021-07-08 14:40:12 -07001338 legacy2aidl_audio_devices_t_AudioDeviceDescription(device));
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001339 return statusTFromBinderStatus(
1340 aps->setStreamVolumeIndex(streamAidl, deviceAidl, indexAidl));
Eric Laurentc2f1f072009-07-17 12:17:14 -07001341}
1342
Eric Laurent83844cc2011-11-18 16:43:31 -08001343status_t AudioSystem::getStreamVolumeIndex(audio_stream_type_t stream,
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001344 int* index,
1345 audio_devices_t device) {
Andy Hung264fa4c2024-02-21 15:52:12 -08001346 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Eric Laurentc2f1f072009-07-17 12:17:14 -07001347 if (aps == 0) return PERMISSION_DENIED;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001348
Mikhail Naganovdbf03642021-08-25 18:15:32 -07001349 AudioStreamType streamAidl = VALUE_OR_RETURN_STATUS(
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001350 legacy2aidl_audio_stream_type_t_AudioStreamType(stream));
Mikhail Naganovf4a75362021-09-16 00:02:54 +00001351 AudioDeviceDescription deviceAidl = VALUE_OR_RETURN_STATUS(
Mikhail Naganov21a32ec2021-07-08 14:40:12 -07001352 legacy2aidl_audio_devices_t_AudioDeviceDescription(device));
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001353 int32_t indexAidl;
1354 RETURN_STATUS_IF_ERROR(statusTFromBinderStatus(
1355 aps->getStreamVolumeIndex(streamAidl, deviceAidl, &indexAidl)));
1356 if (index != nullptr) {
1357 *index = VALUE_OR_RETURN_STATUS(convertIntegral<int>(indexAidl));
1358 }
1359 return OK;
Eric Laurentc2f1f072009-07-17 12:17:14 -07001360}
1361
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001362status_t AudioSystem::setVolumeIndexForAttributes(const audio_attributes_t& attr,
François Gaffiecfe17322018-11-07 13:41:29 +01001363 int index,
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001364 audio_devices_t device) {
Andy Hung264fa4c2024-02-21 15:52:12 -08001365 const sp<IAudioPolicyService> aps = get_audio_policy_service();
François Gaffiecfe17322018-11-07 13:41:29 +01001366 if (aps == 0) return PERMISSION_DENIED;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001367
Mikhail Naganov1c400902023-05-17 11:48:43 -07001368 media::audio::common::AudioAttributes attrAidl = VALUE_OR_RETURN_STATUS(
1369 legacy2aidl_audio_attributes_t_AudioAttributes(attr));
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001370 int32_t indexAidl = VALUE_OR_RETURN_STATUS(convertIntegral<int32_t>(index));
Mikhail Naganovf4a75362021-09-16 00:02:54 +00001371 AudioDeviceDescription deviceAidl = VALUE_OR_RETURN_STATUS(
Mikhail Naganov21a32ec2021-07-08 14:40:12 -07001372 legacy2aidl_audio_devices_t_AudioDeviceDescription(device));
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001373 return statusTFromBinderStatus(
1374 aps->setVolumeIndexForAttributes(attrAidl, deviceAidl, indexAidl));
François Gaffiecfe17322018-11-07 13:41:29 +01001375}
1376
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001377status_t AudioSystem::getVolumeIndexForAttributes(const audio_attributes_t& attr,
1378 int& index,
1379 audio_devices_t device) {
Andy Hung264fa4c2024-02-21 15:52:12 -08001380 const sp<IAudioPolicyService> aps = get_audio_policy_service();
François Gaffiecfe17322018-11-07 13:41:29 +01001381 if (aps == 0) return PERMISSION_DENIED;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001382
Mikhail Naganov1c400902023-05-17 11:48:43 -07001383 media::audio::common::AudioAttributes attrAidl = VALUE_OR_RETURN_STATUS(
1384 legacy2aidl_audio_attributes_t_AudioAttributes(attr));
Mikhail Naganovf4a75362021-09-16 00:02:54 +00001385 AudioDeviceDescription deviceAidl = VALUE_OR_RETURN_STATUS(
Mikhail Naganov21a32ec2021-07-08 14:40:12 -07001386 legacy2aidl_audio_devices_t_AudioDeviceDescription(device));
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001387 int32_t indexAidl;
1388 RETURN_STATUS_IF_ERROR(statusTFromBinderStatus(
1389 aps->getVolumeIndexForAttributes(attrAidl, deviceAidl, &indexAidl)));
1390 index = VALUE_OR_RETURN_STATUS(convertIntegral<int>(indexAidl));
1391 return OK;
François Gaffiecfe17322018-11-07 13:41:29 +01001392}
1393
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001394status_t AudioSystem::getMaxVolumeIndexForAttributes(const audio_attributes_t& attr, int& index) {
Andy Hung264fa4c2024-02-21 15:52:12 -08001395 const sp<IAudioPolicyService> aps = get_audio_policy_service();
François Gaffiecfe17322018-11-07 13:41:29 +01001396 if (aps == 0) return PERMISSION_DENIED;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001397
Mikhail Naganov1c400902023-05-17 11:48:43 -07001398 media::audio::common::AudioAttributes attrAidl = VALUE_OR_RETURN_STATUS(
1399 legacy2aidl_audio_attributes_t_AudioAttributes(attr));
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001400 int32_t indexAidl;
1401 RETURN_STATUS_IF_ERROR(statusTFromBinderStatus(
1402 aps->getMaxVolumeIndexForAttributes(attrAidl, &indexAidl)));
1403 index = VALUE_OR_RETURN_STATUS(convertIntegral<int>(indexAidl));
1404 return OK;
François Gaffiecfe17322018-11-07 13:41:29 +01001405}
1406
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001407status_t AudioSystem::getMinVolumeIndexForAttributes(const audio_attributes_t& attr, int& index) {
Andy Hung264fa4c2024-02-21 15:52:12 -08001408 const sp<IAudioPolicyService> aps = get_audio_policy_service();
François Gaffiecfe17322018-11-07 13:41:29 +01001409 if (aps == 0) return PERMISSION_DENIED;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001410
Mikhail Naganov1c400902023-05-17 11:48:43 -07001411 media::audio::common::AudioAttributes attrAidl = VALUE_OR_RETURN_STATUS(
1412 legacy2aidl_audio_attributes_t_AudioAttributes(attr));
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001413 int32_t indexAidl;
1414 RETURN_STATUS_IF_ERROR(statusTFromBinderStatus(
1415 aps->getMinVolumeIndexForAttributes(attrAidl, &indexAidl)));
1416 index = VALUE_OR_RETURN_STATUS(convertIntegral<int>(indexAidl));
1417 return OK;
François Gaffiecfe17322018-11-07 13:41:29 +01001418}
1419
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001420product_strategy_t AudioSystem::getStrategyForStream(audio_stream_type_t stream) {
Andy Hung264fa4c2024-02-21 15:52:12 -08001421 const sp<IAudioPolicyService> aps = get_audio_policy_service();
François Gaffied0ba9ed2018-11-05 11:50:42 +01001422 if (aps == 0) return PRODUCT_STRATEGY_NONE;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001423
1424 auto result = [&]() -> ConversionResult<product_strategy_t> {
Mikhail Naganovdbf03642021-08-25 18:15:32 -07001425 AudioStreamType streamAidl = VALUE_OR_RETURN(
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001426 legacy2aidl_audio_stream_type_t_AudioStreamType(stream));
1427 int32_t resultAidl;
1428 RETURN_IF_ERROR(statusTFromBinderStatus(
1429 aps->getStrategyForStream(streamAidl, &resultAidl)));
1430 return aidl2legacy_int32_t_product_strategy_t(resultAidl);
1431 }();
1432 return result.value_or(PRODUCT_STRATEGY_NONE);
Eric Laurentde070132010-07-13 04:45:46 -07001433}
1434
François Gaffie1e2b56f2022-04-01 14:34:29 +02001435status_t AudioSystem::getDevicesForAttributes(const audio_attributes_t& aa,
Andy Hung6d23c0f2022-02-16 09:37:15 -08001436 AudioDeviceTypeAddrVector* devices,
1437 bool forVolume) {
Jean-Michel Trivif41599b2020-01-07 14:22:08 -08001438 if (devices == nullptr) {
1439 return BAD_VALUE;
1440 }
Andy Hung264fa4c2024-02-21 15:52:12 -08001441 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Jean-Michel Trivif41599b2020-01-07 14:22:08 -08001442 if (aps == 0) return PERMISSION_DENIED;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001443
Mikhail Naganov1c400902023-05-17 11:48:43 -07001444 media::audio::common::AudioAttributes aaAidl = VALUE_OR_RETURN_STATUS(
1445 legacy2aidl_audio_attributes_t_AudioAttributes(aa));
Mikhail Naganovf4a75362021-09-16 00:02:54 +00001446 std::vector<AudioDevice> retAidl;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001447 RETURN_STATUS_IF_ERROR(
Andy Hung6d23c0f2022-02-16 09:37:15 -08001448 statusTFromBinderStatus(aps->getDevicesForAttributes(aaAidl, forVolume, &retAidl)));
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001449 *devices = VALUE_OR_RETURN_STATUS(
1450 convertContainer<AudioDeviceTypeAddrVector>(
1451 retAidl,
1452 aidl2legacy_AudioDeviceTypeAddress));
1453 return OK;
Jean-Michel Trivif41599b2020-01-07 14:22:08 -08001454}
1455
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001456audio_io_handle_t AudioSystem::getOutputForEffect(const effect_descriptor_t* desc) {
Andy Hung264fa4c2024-02-21 15:52:12 -08001457 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Glenn Kastenefa6ea92014-01-08 09:10:43 -08001458 // FIXME change return type to status_t, and return PERMISSION_DENIED here
Glenn Kasten142f5192014-03-25 17:44:59 -07001459 if (aps == 0) return AUDIO_IO_HANDLE_NONE;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001460
1461 auto result = [&]() -> ConversionResult<audio_io_handle_t> {
1462 media::EffectDescriptor descAidl = VALUE_OR_RETURN(
1463 legacy2aidl_effect_descriptor_t_EffectDescriptor(*desc));
1464 int32_t retAidl;
1465 RETURN_IF_ERROR(
1466 statusTFromBinderStatus(aps->getOutputForEffect(descAidl, &retAidl)));
1467 return aidl2legacy_int32_t_audio_io_handle_t(retAidl);
1468 }();
1469
1470 return result.value_or(AUDIO_IO_HANDLE_NONE);
Eric Laurentde070132010-07-13 04:45:46 -07001471}
1472
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001473status_t AudioSystem::registerEffect(const effect_descriptor_t* desc,
1474 audio_io_handle_t io,
1475 product_strategy_t strategy,
1476 audio_session_t session,
1477 int id) {
Andy Hung264fa4c2024-02-21 15:52:12 -08001478 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Eric Laurentde070132010-07-13 04:45:46 -07001479 if (aps == 0) return PERMISSION_DENIED;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001480
1481 media::EffectDescriptor descAidl = VALUE_OR_RETURN_STATUS(
1482 legacy2aidl_effect_descriptor_t_EffectDescriptor(*desc));
1483 int32_t ioAidl = VALUE_OR_RETURN_STATUS(legacy2aidl_audio_io_handle_t_int32_t(io));
1484 int32_t strategyAidl = VALUE_OR_RETURN_STATUS(aidl2legacy_int32_t_product_strategy_t(strategy));
1485 int32_t sessionAidl = VALUE_OR_RETURN_STATUS(legacy2aidl_audio_session_t_int32_t(session));
1486 int32_t idAidl = VALUE_OR_RETURN_STATUS(convertReinterpret<int32_t>(id));
1487 return statusTFromBinderStatus(
1488 aps->registerEffect(descAidl, ioAidl, strategyAidl, sessionAidl, idAidl));
Eric Laurentde070132010-07-13 04:45:46 -07001489}
1490
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001491status_t AudioSystem::unregisterEffect(int id) {
Andy Hung264fa4c2024-02-21 15:52:12 -08001492 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Eric Laurentde070132010-07-13 04:45:46 -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->unregisterEffect(idAidl));
Eric Laurentde070132010-07-13 04:45:46 -07001498}
1499
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001500status_t AudioSystem::setEffectEnabled(int id, bool enabled) {
Andy Hung264fa4c2024-02-21 15:52:12 -08001501 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Eric Laurentdb7c0792011-08-10 10:37:50 -07001502 if (aps == 0) return PERMISSION_DENIED;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001503
1504 int32_t idAidl = VALUE_OR_RETURN_STATUS(convertReinterpret<int32_t>(id));
1505 return statusTFromBinderStatus(
1506 aps->setEffectEnabled(idAidl, enabled));
Eric Laurentdb7c0792011-08-10 10:37:50 -07001507}
1508
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001509status_t AudioSystem::moveEffectsToIo(const std::vector<int>& ids, audio_io_handle_t io) {
Andy Hung264fa4c2024-02-21 15:52:12 -08001510 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Eric Laurent6c796322019-04-09 14:13:17 -07001511 if (aps == 0) return PERMISSION_DENIED;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001512
1513 std::vector<int32_t> idsAidl = VALUE_OR_RETURN_STATUS(
1514 convertContainer<std::vector<int32_t>>(ids, convertReinterpret<int32_t, int>));
1515 int32_t ioAidl = VALUE_OR_RETURN_STATUS(legacy2aidl_audio_io_handle_t_int32_t(io));
1516 return statusTFromBinderStatus(aps->moveEffectsToIo(idsAidl, ioAidl));
Eric Laurent6c796322019-04-09 14:13:17 -07001517}
1518
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001519status_t AudioSystem::isStreamActive(audio_stream_type_t stream, bool* state, uint32_t inPastMs) {
Andy Hung264fa4c2024-02-21 15:52:12 -08001520 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Eric Laurenteda6c362011-02-02 09:33:30 -08001521 if (aps == 0) return PERMISSION_DENIED;
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001522 if (state == NULL) return BAD_VALUE;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001523
Mikhail Naganovdbf03642021-08-25 18:15:32 -07001524 AudioStreamType streamAidl = VALUE_OR_RETURN_STATUS(
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001525 legacy2aidl_audio_stream_type_t_AudioStreamType(stream));
1526 int32_t inPastMsAidl = VALUE_OR_RETURN_STATUS(convertIntegral<int32_t>(inPastMs));
1527 RETURN_STATUS_IF_ERROR(statusTFromBinderStatus(
1528 aps->isStreamActive(streamAidl, inPastMsAidl, state)));
1529 return OK;
Eric Laurenteda6c362011-02-02 09:33:30 -08001530}
1531
Jean-Michel Trivi272ab542013-02-04 16:26:02 -08001532status_t AudioSystem::isStreamActiveRemotely(audio_stream_type_t stream, bool* state,
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001533 uint32_t inPastMs) {
Andy Hung264fa4c2024-02-21 15:52:12 -08001534 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Jean-Michel Trivi272ab542013-02-04 16:26:02 -08001535 if (aps == 0) return PERMISSION_DENIED;
1536 if (state == NULL) return BAD_VALUE;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001537
Mikhail Naganovdbf03642021-08-25 18:15:32 -07001538 AudioStreamType streamAidl = VALUE_OR_RETURN_STATUS(
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001539 legacy2aidl_audio_stream_type_t_AudioStreamType(stream));
1540 int32_t inPastMsAidl = VALUE_OR_RETURN_STATUS(convertIntegral<int32_t>(inPastMs));
1541 RETURN_STATUS_IF_ERROR(statusTFromBinderStatus(
1542 aps->isStreamActiveRemotely(streamAidl, inPastMsAidl, state)));
1543 return OK;
Jean-Michel Trivi272ab542013-02-04 16:26:02 -08001544}
1545
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001546status_t AudioSystem::isSourceActive(audio_source_t stream, bool* state) {
Andy Hung264fa4c2024-02-21 15:52:12 -08001547 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Jean-Michel Trivid7086032012-10-10 12:11:16 -07001548 if (aps == 0) return PERMISSION_DENIED;
1549 if (state == NULL) return BAD_VALUE;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001550
Mikhail Naganovddceecc2021-09-03 13:58:56 -07001551 AudioSource streamAidl = VALUE_OR_RETURN_STATUS(
1552 legacy2aidl_audio_source_t_AudioSource(stream));
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001553 RETURN_STATUS_IF_ERROR(statusTFromBinderStatus(
1554 aps->isSourceActive(streamAidl, state)));
1555 return OK;
Jean-Michel Trivid7086032012-10-10 12:11:16 -07001556}
1557
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001558uint32_t AudioSystem::getPrimaryOutputSamplingRate() {
Andy Hung264fa4c2024-02-21 15:52:12 -08001559 const sp<IAudioFlinger> af = get_audio_flinger();
Glenn Kastencc0f1cf2012-09-24 11:27:18 -07001560 if (af == 0) return 0;
1561 return af->getPrimaryOutputSamplingRate();
1562}
1563
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001564size_t AudioSystem::getPrimaryOutputFrameCount() {
Andy Hung264fa4c2024-02-21 15:52:12 -08001565 const sp<IAudioFlinger> af = get_audio_flinger();
Glenn Kastencc0f1cf2012-09-24 11:27:18 -07001566 if (af == 0) return 0;
1567 return af->getPrimaryOutputFrameCount();
1568}
Eric Laurenteda6c362011-02-02 09:33:30 -08001569
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001570status_t AudioSystem::setLowRamDevice(bool isLowRamDevice, int64_t totalMemory) {
Andy Hung264fa4c2024-02-21 15:52:12 -08001571 const sp<IAudioFlinger> af = get_audio_flinger();
Glenn Kasten4182c4e2013-07-15 14:45:07 -07001572 if (af == 0) return PERMISSION_DENIED;
Andy Hung6f248bb2018-01-23 14:04:37 -08001573 return af->setLowRamDevice(isLowRamDevice, totalMemory);
Glenn Kasten4182c4e2013-07-15 14:45:07 -07001574}
1575
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001576void AudioSystem::clearAudioConfigCache() {
Glenn Kastend2d089f2014-11-05 11:48:12 -08001577 // called by restoreTrack_l(), which needs new IAudioFlinger and IAudioPolicyService instances
Steve Block3856b092011-10-20 11:56:00 +01001578 ALOGV("clearAudioConfigCache()");
Andy Hungda1fb072024-02-20 19:08:08 -08001579 gAudioFlingerServiceHandler.clearService();
Mikhail Naganovec3d5792022-05-06 00:19:55 +00001580 clearAudioPolicyService();
Eric Laurent9f6530f2011-08-30 10:18:54 -07001581}
1582
Hayden Gomes524159d2019-12-23 14:41:47 -08001583status_t AudioSystem::setSupportedSystemUsages(const std::vector<audio_usage_t>& systemUsages) {
Andy Hung264fa4c2024-02-21 15:52:12 -08001584 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Hayden Gomes524159d2019-12-23 14:41:47 -08001585 if (aps == nullptr) return PERMISSION_DENIED;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001586
Mikhail Naganovdbf03642021-08-25 18:15:32 -07001587 std::vector<AudioUsage> systemUsagesAidl = VALUE_OR_RETURN_STATUS(
1588 convertContainer<std::vector<AudioUsage>>(systemUsages,
1589 legacy2aidl_audio_usage_t_AudioUsage));
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001590 return statusTFromBinderStatus(aps->setSupportedSystemUsages(systemUsagesAidl));
Hayden Gomes524159d2019-12-23 14:41:47 -08001591}
1592
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001593status_t AudioSystem::setAllowedCapturePolicy(uid_t uid, audio_flags_mask_t capturePolicy) {
Andy Hung264fa4c2024-02-21 15:52:12 -08001594 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Kevin Rocardb99cc752019-03-21 20:52:24 -07001595 if (aps == nullptr) return PERMISSION_DENIED;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001596
1597 int32_t uidAidl = VALUE_OR_RETURN_STATUS(legacy2aidl_uid_t_int32_t(uid));
1598 int32_t capturePolicyAidl = VALUE_OR_RETURN_STATUS(
1599 legacy2aidl_audio_flags_mask_t_int32_t_mask(capturePolicy));
1600 return statusTFromBinderStatus(aps->setAllowedCapturePolicy(uidAidl, capturePolicyAidl));
Kevin Rocardb99cc752019-03-21 20:52:24 -07001601}
1602
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001603audio_offload_mode_t AudioSystem::getOffloadSupport(const audio_offload_info_t& info) {
Eric Laurent90fe31c2020-11-26 20:06:35 +01001604 ALOGV("%s", __func__);
Andy Hung264fa4c2024-02-21 15:52:12 -08001605 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Eric Laurent90fe31c2020-11-26 20:06:35 +01001606 if (aps == 0) return AUDIO_OFFLOAD_NOT_SUPPORTED;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001607
1608 auto result = [&]() -> ConversionResult<audio_offload_mode_t> {
Mikhail Naganovdbf03642021-08-25 18:15:32 -07001609 AudioOffloadInfo infoAidl = VALUE_OR_RETURN(
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001610 legacy2aidl_audio_offload_info_t_AudioOffloadInfo(info));
1611 media::AudioOffloadMode retAidl;
1612 RETURN_IF_ERROR(
1613 statusTFromBinderStatus(aps->getOffloadSupport(infoAidl, &retAidl)));
1614 return aidl2legacy_AudioOffloadMode_audio_offload_mode_t(retAidl);
1615 }();
1616
1617 return result.value_or(static_cast<audio_offload_mode_t>(0));
Richard Fitzgeraldad3af332013-03-25 16:54:37 +00001618}
1619
Eric Laurent203b1a12014-04-01 10:34:16 -07001620status_t AudioSystem::listAudioPorts(audio_port_role_t role,
1621 audio_port_type_t type,
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001622 unsigned int* num_ports,
1623 struct audio_port_v7* ports,
1624 unsigned int* generation) {
1625 if (num_ports == nullptr || (*num_ports != 0 && ports == nullptr) ||
1626 generation == nullptr) {
1627 return BAD_VALUE;
1628 }
1629
Andy Hung264fa4c2024-02-21 15:52:12 -08001630 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Eric Laurent203b1a12014-04-01 10:34:16 -07001631 if (aps == 0) return PERMISSION_DENIED;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001632
1633 media::AudioPortRole roleAidl = VALUE_OR_RETURN_STATUS(
1634 legacy2aidl_audio_port_role_t_AudioPortRole(role));
1635 media::AudioPortType typeAidl = VALUE_OR_RETURN_STATUS(
1636 legacy2aidl_audio_port_type_t_AudioPortType(type));
Mikhail Naganov0078ee52021-09-30 23:06:20 +00001637 Int numPortsAidl;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001638 numPortsAidl.value = VALUE_OR_RETURN_STATUS(convertIntegral<int32_t>(*num_ports));
Atneya Nair638a6e42022-12-18 16:45:15 -08001639 std::vector<media::AudioPortFw> portsAidl;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001640 int32_t generationAidl;
1641
1642 RETURN_STATUS_IF_ERROR(statusTFromBinderStatus(
1643 aps->listAudioPorts(roleAidl, typeAidl, &numPortsAidl, &portsAidl, &generationAidl)));
1644 *num_ports = VALUE_OR_RETURN_STATUS(convertIntegral<unsigned int>(numPortsAidl.value));
1645 *generation = VALUE_OR_RETURN_STATUS(convertIntegral<unsigned int>(generationAidl));
1646 RETURN_STATUS_IF_ERROR(convertRange(portsAidl.begin(), portsAidl.end(), ports,
Mikhail Naganov87227252023-01-13 17:38:10 -08001647 aidl2legacy_AudioPortFw_audio_port_v7));
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001648 return OK;
Eric Laurent203b1a12014-04-01 10:34:16 -07001649}
1650
Mikhail Naganov5edc5ed2023-03-23 14:52:15 -07001651status_t AudioSystem::listDeclaredDevicePorts(media::AudioPortRole role,
1652 std::vector<media::AudioPortFw>* result) {
1653 if (result == nullptr) return BAD_VALUE;
Andy Hung264fa4c2024-02-21 15:52:12 -08001654 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Mikhail Naganov5edc5ed2023-03-23 14:52:15 -07001655 if (aps == 0) return PERMISSION_DENIED;
1656 RETURN_STATUS_IF_ERROR(statusTFromBinderStatus(aps->listDeclaredDevicePorts(role, result)));
1657 return OK;
1658}
1659
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001660status_t AudioSystem::getAudioPort(struct audio_port_v7* port) {
1661 if (port == nullptr) {
1662 return BAD_VALUE;
1663 }
Andy Hung264fa4c2024-02-21 15:52:12 -08001664 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Eric Laurent203b1a12014-04-01 10:34:16 -07001665 if (aps == 0) return PERMISSION_DENIED;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001666
Atneya Nair638a6e42022-12-18 16:45:15 -08001667 media::AudioPortFw portAidl;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001668 RETURN_STATUS_IF_ERROR(
Mikhail Naganov17031562022-02-23 23:00:27 +00001669 statusTFromBinderStatus(aps->getAudioPort(port->id, &portAidl)));
Mikhail Naganov87227252023-01-13 17:38:10 -08001670 *port = VALUE_OR_RETURN_STATUS(aidl2legacy_AudioPortFw_audio_port_v7(portAidl));
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001671 return OK;
Eric Laurent203b1a12014-04-01 10:34:16 -07001672}
1673
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001674status_t AudioSystem::createAudioPatch(const struct audio_patch* patch,
1675 audio_patch_handle_t* handle) {
1676 if (patch == nullptr || handle == nullptr) {
1677 return BAD_VALUE;
1678 }
1679
Andy Hung264fa4c2024-02-21 15:52:12 -08001680 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Eric Laurent203b1a12014-04-01 10:34:16 -07001681 if (aps == 0) return PERMISSION_DENIED;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001682
Atneya Nair3afdbd12022-12-18 16:14:18 -08001683 media::AudioPatchFw patchAidl = VALUE_OR_RETURN_STATUS(
Mikhail Naganov87227252023-01-13 17:38:10 -08001684 legacy2aidl_audio_patch_AudioPatchFw(*patch));
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001685 int32_t handleAidl = VALUE_OR_RETURN_STATUS(legacy2aidl_audio_patch_handle_t_int32_t(*handle));
1686 RETURN_STATUS_IF_ERROR(
1687 statusTFromBinderStatus(aps->createAudioPatch(patchAidl, handleAidl, &handleAidl)));
1688 *handle = VALUE_OR_RETURN_STATUS(aidl2legacy_int32_t_audio_patch_handle_t(handleAidl));
1689 return OK;
Eric Laurent203b1a12014-04-01 10:34:16 -07001690}
1691
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001692status_t AudioSystem::releaseAudioPatch(audio_patch_handle_t handle) {
Andy Hung264fa4c2024-02-21 15:52:12 -08001693 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Eric Laurent203b1a12014-04-01 10:34:16 -07001694 if (aps == 0) return PERMISSION_DENIED;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001695
1696 int32_t handleAidl = VALUE_OR_RETURN_STATUS(legacy2aidl_audio_patch_handle_t_int32_t(handle));
1697 return statusTFromBinderStatus(aps->releaseAudioPatch(handleAidl));
Eric Laurent203b1a12014-04-01 10:34:16 -07001698}
1699
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001700status_t AudioSystem::listAudioPatches(unsigned int* num_patches,
1701 struct audio_patch* patches,
1702 unsigned int* generation) {
1703 if (num_patches == nullptr || (*num_patches != 0 && patches == nullptr) ||
1704 generation == nullptr) {
1705 return BAD_VALUE;
1706 }
1707
Andy Hung264fa4c2024-02-21 15:52:12 -08001708 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Eric Laurent203b1a12014-04-01 10:34:16 -07001709 if (aps == 0) return PERMISSION_DENIED;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001710
1711
Mikhail Naganov0078ee52021-09-30 23:06:20 +00001712 Int numPatchesAidl;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001713 numPatchesAidl.value = VALUE_OR_RETURN_STATUS(convertIntegral<int32_t>(*num_patches));
Atneya Nair3afdbd12022-12-18 16:14:18 -08001714 std::vector<media::AudioPatchFw> patchesAidl;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001715 int32_t generationAidl;
1716
1717 RETURN_STATUS_IF_ERROR(statusTFromBinderStatus(
1718 aps->listAudioPatches(&numPatchesAidl, &patchesAidl, &generationAidl)));
1719 *num_patches = VALUE_OR_RETURN_STATUS(convertIntegral<unsigned int>(numPatchesAidl.value));
1720 *generation = VALUE_OR_RETURN_STATUS(convertIntegral<unsigned int>(generationAidl));
1721 RETURN_STATUS_IF_ERROR(convertRange(patchesAidl.begin(), patchesAidl.end(), patches,
Mikhail Naganov87227252023-01-13 17:38:10 -08001722 aidl2legacy_AudioPatchFw_audio_patch));
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001723 return OK;
Eric Laurent203b1a12014-04-01 10:34:16 -07001724}
1725
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001726status_t AudioSystem::setAudioPortConfig(const struct audio_port_config* config) {
1727 if (config == nullptr) {
1728 return BAD_VALUE;
1729 }
1730
Andy Hung264fa4c2024-02-21 15:52:12 -08001731 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Eric Laurent203b1a12014-04-01 10:34:16 -07001732 if (aps == 0) return PERMISSION_DENIED;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001733
Atneya Nair7a9594f2022-12-18 17:26:26 -08001734 media::AudioPortConfigFw configAidl = VALUE_OR_RETURN_STATUS(
Mikhail Naganov87227252023-01-13 17:38:10 -08001735 legacy2aidl_audio_port_config_AudioPortConfigFw(*config));
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001736 return statusTFromBinderStatus(aps->setAudioPortConfig(configAidl));
Eric Laurent203b1a12014-04-01 10:34:16 -07001737}
1738
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001739status_t AudioSystem::addAudioPortCallback(const sp<AudioPortCallback>& callback) {
Andy Hung264fa4c2024-02-21 15:52:12 -08001740 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Eric Laurentb28753e2015-04-01 13:06:28 -07001741 if (aps == 0) return PERMISSION_DENIED;
Andy Hung88e71732024-02-21 16:44:27 -08001742 const auto apc = gAudioPolicyServiceHandler.getClient();
1743 if (apc == nullptr) return NO_INIT;
Eric Laurentb28753e2015-04-01 13:06:28 -07001744
Andy Hung88e71732024-02-21 16:44:27 -08001745 std::lock_guard _l(gApsCallbackMutex);
1746 const int ret = apc->addAudioPortCallback(callback);
Eric Laurente8726fe2015-06-26 09:39:24 -07001747 if (ret == 1) {
1748 aps->setAudioPortCallbacksEnabled(true);
1749 }
1750 return (ret < 0) ? INVALID_OPERATION : NO_ERROR;
Eric Laurentb52c1522014-05-20 11:27:36 -07001751}
1752
Jean-Michel Trivif613d422015-04-23 18:41:29 -07001753/*static*/
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001754status_t AudioSystem::removeAudioPortCallback(const sp<AudioPortCallback>& callback) {
Andy Hung264fa4c2024-02-21 15:52:12 -08001755 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Eric Laurentb28753e2015-04-01 13:06:28 -07001756 if (aps == 0) return PERMISSION_DENIED;
Andy Hung88e71732024-02-21 16:44:27 -08001757 const auto apc = gAudioPolicyServiceHandler.getClient();
1758 if (apc == nullptr) return NO_INIT;
Eric Laurentb28753e2015-04-01 13:06:28 -07001759
Andy Hung88e71732024-02-21 16:44:27 -08001760 std::lock_guard _l(gApsCallbackMutex);
1761 const int ret = apc->removeAudioPortCallback(callback);
Eric Laurente8726fe2015-06-26 09:39:24 -07001762 if (ret == 0) {
1763 aps->setAudioPortCallbacksEnabled(false);
1764 }
1765 return (ret < 0) ? INVALID_OPERATION : NO_ERROR;
Eric Laurent296fb132015-05-01 11:38:42 -07001766}
1767
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001768status_t AudioSystem::addAudioVolumeGroupCallback(const sp<AudioVolumeGroupCallback>& callback) {
Andy Hung264fa4c2024-02-21 15:52:12 -08001769 const sp<IAudioPolicyService> aps = get_audio_policy_service();
François Gaffiecfe17322018-11-07 13:41:29 +01001770 if (aps == 0) return PERMISSION_DENIED;
Andy Hung88e71732024-02-21 16:44:27 -08001771 const auto apc = gAudioPolicyServiceHandler.getClient();
1772 if (apc == nullptr) return NO_INIT;
François Gaffiecfe17322018-11-07 13:41:29 +01001773
Andy Hung88e71732024-02-21 16:44:27 -08001774 std::lock_guard _l(gApsCallbackMutex);
1775 const int ret = apc->addAudioVolumeGroupCallback(callback);
François Gaffiecfe17322018-11-07 13:41:29 +01001776 if (ret == 1) {
1777 aps->setAudioVolumeGroupCallbacksEnabled(true);
1778 }
1779 return (ret < 0) ? INVALID_OPERATION : NO_ERROR;
1780}
1781
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001782status_t AudioSystem::removeAudioVolumeGroupCallback(const sp<AudioVolumeGroupCallback>& callback) {
Andy Hung264fa4c2024-02-21 15:52:12 -08001783 const sp<IAudioPolicyService> aps = get_audio_policy_service();
François Gaffiecfe17322018-11-07 13:41:29 +01001784 if (aps == 0) return PERMISSION_DENIED;
Andy Hung88e71732024-02-21 16:44:27 -08001785 const auto apc = gAudioPolicyServiceHandler.getClient();
1786 if (apc == nullptr) return NO_INIT;
François Gaffiecfe17322018-11-07 13:41:29 +01001787
Andy Hung88e71732024-02-21 16:44:27 -08001788 std::lock_guard _l(gApsCallbackMutex);
1789 const int ret = apc->removeAudioVolumeGroupCallback(callback);
François Gaffiecfe17322018-11-07 13:41:29 +01001790 if (ret == 0) {
1791 aps->setAudioVolumeGroupCallbacksEnabled(false);
1792 }
1793 return (ret < 0) ? INVALID_OPERATION : NO_ERROR;
1794}
1795
Eric Laurent296fb132015-05-01 11:38:42 -07001796status_t AudioSystem::addAudioDeviceCallback(
Eric Laurent09f1ed22019-04-24 17:45:17 -07001797 const wp<AudioDeviceCallback>& callback, audio_io_handle_t audioIo,
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001798 audio_port_handle_t portId) {
Eric Laurent296fb132015-05-01 11:38:42 -07001799 const sp<AudioFlingerClient> afc = getAudioFlingerClient();
1800 if (afc == 0) {
1801 return NO_INIT;
1802 }
Eric Laurent09f1ed22019-04-24 17:45:17 -07001803 status_t status = afc->addAudioDeviceCallback(callback, audioIo, portId);
Eric Laurent7c1ec5f2015-07-09 14:52:47 -07001804 if (status == NO_ERROR) {
Andy Hung264fa4c2024-02-21 15:52:12 -08001805 const sp<IAudioFlinger> af = get_audio_flinger();
Eric Laurent7c1ec5f2015-07-09 14:52:47 -07001806 if (af != 0) {
1807 af->registerClient(afc);
1808 }
1809 }
1810 return status;
Eric Laurent296fb132015-05-01 11:38:42 -07001811}
1812
1813status_t AudioSystem::removeAudioDeviceCallback(
Eric Laurent09f1ed22019-04-24 17:45:17 -07001814 const wp<AudioDeviceCallback>& callback, audio_io_handle_t audioIo,
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001815 audio_port_handle_t portId) {
Eric Laurent296fb132015-05-01 11:38:42 -07001816 const sp<AudioFlingerClient> afc = getAudioFlingerClient();
1817 if (afc == 0) {
1818 return NO_INIT;
1819 }
Eric Laurent09f1ed22019-04-24 17:45:17 -07001820 return afc->removeAudioDeviceCallback(callback, audioIo, portId);
Eric Laurent296fb132015-05-01 11:38:42 -07001821}
1822
Eric Laurent076e7c72022-05-03 18:12:28 +02001823status_t AudioSystem::addSupportedLatencyModesCallback(
1824 const sp<SupportedLatencyModesCallback>& callback) {
1825 const sp<AudioFlingerClient> afc = getAudioFlingerClient();
1826 if (afc == 0) {
1827 return NO_INIT;
1828 }
1829 return afc->addSupportedLatencyModesCallback(callback);
1830}
1831
1832status_t AudioSystem::removeSupportedLatencyModesCallback(
1833 const sp<SupportedLatencyModesCallback>& callback) {
1834 const sp<AudioFlingerClient> afc = getAudioFlingerClient();
1835 if (afc == 0) {
1836 return NO_INIT;
1837 }
1838 return afc->removeSupportedLatencyModesCallback(callback);
1839}
1840
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001841audio_port_handle_t AudioSystem::getDeviceIdForIo(audio_io_handle_t audioIo) {
Andy Hung264fa4c2024-02-21 15:52:12 -08001842 const sp<IAudioFlinger> af = get_audio_flinger();
Eric Laurent296fb132015-05-01 11:38:42 -07001843 if (af == 0) return PERMISSION_DENIED;
1844 const sp<AudioIoDescriptor> desc = getIoDescriptor(audioIo);
1845 if (desc == 0) {
1846 return AUDIO_PORT_HANDLE_NONE;
1847 }
1848 return desc->getDeviceId();
Eric Laurentb28753e2015-04-01 13:06:28 -07001849}
1850
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001851status_t AudioSystem::acquireSoundTriggerSession(audio_session_t* session,
1852 audio_io_handle_t* ioHandle,
1853 audio_devices_t* device) {
1854 if (session == nullptr || ioHandle == nullptr || device == nullptr) {
1855 return BAD_VALUE;
1856 }
Andy Hung264fa4c2024-02-21 15:52:12 -08001857 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Eric Laurentdf3dc7e2014-07-27 18:39:40 -07001858 if (aps == 0) return PERMISSION_DENIED;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001859
1860 media::SoundTriggerSession retAidl;
1861 RETURN_STATUS_IF_ERROR(
1862 statusTFromBinderStatus(aps->acquireSoundTriggerSession(&retAidl)));
1863 *session = VALUE_OR_RETURN_STATUS(aidl2legacy_int32_t_audio_session_t(retAidl.session));
1864 *ioHandle = VALUE_OR_RETURN_STATUS(aidl2legacy_int32_t_audio_io_handle_t(retAidl.ioHandle));
Mikhail Naganov21a32ec2021-07-08 14:40:12 -07001865 *device = VALUE_OR_RETURN_STATUS(
1866 aidl2legacy_AudioDeviceDescription_audio_devices_t(retAidl.device));
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001867 return OK;
Eric Laurentdf3dc7e2014-07-27 18:39:40 -07001868}
1869
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001870status_t AudioSystem::releaseSoundTriggerSession(audio_session_t session) {
Andy Hung264fa4c2024-02-21 15:52:12 -08001871 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Eric Laurentdf3dc7e2014-07-27 18:39:40 -07001872 if (aps == 0) return PERMISSION_DENIED;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001873
1874 int32_t sessionAidl = VALUE_OR_RETURN_STATUS(legacy2aidl_audio_session_t_int32_t(session));
1875 return statusTFromBinderStatus(aps->releaseSoundTriggerSession(sessionAidl));
Eric Laurentdf3dc7e2014-07-27 18:39:40 -07001876}
Eric Laurentbb6c9a02014-09-25 14:11:47 -07001877
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001878audio_mode_t AudioSystem::getPhoneState() {
Andy Hung264fa4c2024-02-21 15:52:12 -08001879 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Eric Laurentbb6c9a02014-09-25 14:11:47 -07001880 if (aps == 0) return AUDIO_MODE_INVALID;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001881
1882 auto result = [&]() -> ConversionResult<audio_mode_t> {
Mikhail Naganovddceecc2021-09-03 13:58:56 -07001883 media::audio::common::AudioMode retAidl;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001884 RETURN_IF_ERROR(statusTFromBinderStatus(aps->getPhoneState(&retAidl)));
1885 return aidl2legacy_AudioMode_audio_mode_t(retAidl);
1886 }();
1887
1888 return result.value_or(AUDIO_MODE_INVALID);
Eric Laurentbb6c9a02014-09-25 14:11:47 -07001889}
1890
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001891status_t AudioSystem::registerPolicyMixes(const Vector<AudioMix>& mixes, bool registration) {
Andy Hung264fa4c2024-02-21 15:52:12 -08001892 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Eric Laurentbaac1832014-12-01 17:52:59 -08001893 if (aps == 0) return PERMISSION_DENIED;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001894
1895 size_t mixesSize = std::min(mixes.size(), size_t{MAX_MIXES_PER_POLICY});
1896 std::vector<media::AudioMix> mixesAidl;
1897 RETURN_STATUS_IF_ERROR(
1898 convertRange(mixes.begin(), mixes.begin() + mixesSize, std::back_inserter(mixesAidl),
1899 legacy2aidl_AudioMix));
1900 return statusTFromBinderStatus(aps->registerPolicyMixes(mixesAidl, registration));
Eric Laurentbaac1832014-12-01 17:52:59 -08001901}
Eric Laurentbb6c9a02014-09-25 14:11:47 -07001902
Marvin Raminbdefaf02023-11-01 09:10:32 +01001903status_t AudioSystem::getRegisteredPolicyMixes(std::vector<AudioMix>& mixes) {
1904 if (!audio_flags::audio_mix_test_api()) {
1905 return INVALID_OPERATION;
1906 }
1907
1908 const sp<IAudioPolicyService> aps = AudioSystem::get_audio_policy_service();
1909 if (aps == nullptr) return PERMISSION_DENIED;
1910
1911 std::vector<::android::media::AudioMix> aidlMixes;
1912 Status status = aps->getRegisteredPolicyMixes(&aidlMixes);
1913
1914 for (const auto& aidlMix : aidlMixes) {
1915 AudioMix mix = VALUE_OR_RETURN_STATUS(aidl2legacy_AudioMix(aidlMix));
1916 mixes.push_back(mix);
1917 }
1918
1919 return statusTFromBinderStatus(status);
1920}
1921
Jan Sebechlebsky0af8e872023-08-11 14:45:08 +02001922status_t AudioSystem::updatePolicyMixes(
1923 const std::vector<std::pair<AudioMix, std::vector<AudioMixMatchCriterion>>>&
1924 mixesWithUpdates) {
Andy Hung264fa4c2024-02-21 15:52:12 -08001925 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Jan Sebechlebsky0af8e872023-08-11 14:45:08 +02001926 if (aps == 0) return PERMISSION_DENIED;
1927
1928 std::vector<media::AudioMixUpdate> updatesAidl;
1929 updatesAidl.reserve(mixesWithUpdates.size());
1930
1931 for (const auto& update : mixesWithUpdates) {
1932 media::AudioMixUpdate updateAidl;
1933 updateAidl.audioMix = VALUE_OR_RETURN_STATUS(legacy2aidl_AudioMix(update.first));
1934 RETURN_STATUS_IF_ERROR(convertRange(update.second.begin(), update.second.end(),
1935 std::back_inserter(updateAidl.newCriteria),
1936 legacy2aidl_AudioMixMatchCriterion));
1937 updatesAidl.emplace_back(updateAidl);
1938 }
1939
1940 return statusTFromBinderStatus(aps->updatePolicyMixes(updatesAidl));
1941}
1942
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001943status_t AudioSystem::setUidDeviceAffinities(uid_t uid, const AudioDeviceTypeAddrVector& devices) {
Andy Hung264fa4c2024-02-21 15:52:12 -08001944 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Jean-Michel Trivibda70da2018-12-19 07:30:15 -08001945 if (aps == 0) return PERMISSION_DENIED;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001946
1947 int32_t uidAidl = VALUE_OR_RETURN_STATUS(legacy2aidl_uid_t_int32_t(uid));
Mikhail Naganovf4a75362021-09-16 00:02:54 +00001948 std::vector<AudioDevice> devicesAidl = VALUE_OR_RETURN_STATUS(
1949 convertContainer<std::vector<AudioDevice>>(devices,
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001950 legacy2aidl_AudioDeviceTypeAddress));
1951 return statusTFromBinderStatus(aps->setUidDeviceAffinities(uidAidl, devicesAidl));
Jean-Michel Trivibda70da2018-12-19 07:30:15 -08001952}
1953
1954status_t AudioSystem::removeUidDeviceAffinities(uid_t uid) {
Andy Hung264fa4c2024-02-21 15:52:12 -08001955 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Jean-Michel Trivibda70da2018-12-19 07:30:15 -08001956 if (aps == 0) return PERMISSION_DENIED;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001957
1958 int32_t uidAidl = VALUE_OR_RETURN_STATUS(legacy2aidl_uid_t_int32_t(uid));
1959 return statusTFromBinderStatus(aps->removeUidDeviceAffinities(uidAidl));
Jean-Michel Trivibda70da2018-12-19 07:30:15 -08001960}
1961
Oscar Azucena90e77632019-11-27 17:12:28 -08001962status_t AudioSystem::setUserIdDeviceAffinities(int userId,
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001963 const AudioDeviceTypeAddrVector& devices) {
Andy Hung264fa4c2024-02-21 15:52:12 -08001964 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Oscar Azucena90e77632019-11-27 17:12:28 -08001965 if (aps == 0) return PERMISSION_DENIED;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001966
1967 int32_t userIdAidl = VALUE_OR_RETURN_STATUS(convertReinterpret<int32_t>(userId));
Mikhail Naganovf4a75362021-09-16 00:02:54 +00001968 std::vector<AudioDevice> devicesAidl = VALUE_OR_RETURN_STATUS(
1969 convertContainer<std::vector<AudioDevice>>(devices,
1970 legacy2aidl_AudioDeviceTypeAddress));
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001971 return statusTFromBinderStatus(
1972 aps->setUserIdDeviceAffinities(userIdAidl, devicesAidl));
Oscar Azucena90e77632019-11-27 17:12:28 -08001973}
1974
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001975status_t AudioSystem::removeUserIdDeviceAffinities(int userId) {
Andy Hung264fa4c2024-02-21 15:52:12 -08001976 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Oscar Azucena90e77632019-11-27 17:12:28 -08001977 if (aps == 0) return PERMISSION_DENIED;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001978 int32_t userIdAidl = VALUE_OR_RETURN_STATUS(convertReinterpret<int32_t>(userId));
1979 return statusTFromBinderStatus(aps->removeUserIdDeviceAffinities(userIdAidl));
Oscar Azucena90e77632019-11-27 17:12:28 -08001980}
1981
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001982status_t AudioSystem::startAudioSource(const struct audio_port_config* source,
1983 const audio_attributes_t* attributes,
1984 audio_port_handle_t* portId) {
1985 if (source == nullptr || attributes == nullptr || portId == nullptr) {
1986 return BAD_VALUE;
1987 }
Andy Hung264fa4c2024-02-21 15:52:12 -08001988 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Eric Laurent554a2772015-04-10 11:29:24 -07001989 if (aps == 0) return PERMISSION_DENIED;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001990
Atneya Nair7a9594f2022-12-18 17:26:26 -08001991 media::AudioPortConfigFw sourceAidl = VALUE_OR_RETURN_STATUS(
Mikhail Naganov87227252023-01-13 17:38:10 -08001992 legacy2aidl_audio_port_config_AudioPortConfigFw(*source));
Mikhail Naganov1c400902023-05-17 11:48:43 -07001993 media::audio::common::AudioAttributes attributesAidl = VALUE_OR_RETURN_STATUS(
1994 legacy2aidl_audio_attributes_t_AudioAttributes(*attributes));
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001995 int32_t portIdAidl;
1996 RETURN_STATUS_IF_ERROR(statusTFromBinderStatus(
1997 aps->startAudioSource(sourceAidl, attributesAidl, &portIdAidl)));
1998 *portId = VALUE_OR_RETURN_STATUS(aidl2legacy_int32_t_audio_port_handle_t(portIdAidl));
1999 return OK;
Eric Laurent554a2772015-04-10 11:29:24 -07002000}
2001
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002002status_t AudioSystem::stopAudioSource(audio_port_handle_t portId) {
Andy Hung264fa4c2024-02-21 15:52:12 -08002003 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Eric Laurent554a2772015-04-10 11:29:24 -07002004 if (aps == 0) return PERMISSION_DENIED;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002005
2006 int32_t portIdAidl = VALUE_OR_RETURN_STATUS(legacy2aidl_audio_port_handle_t_int32_t(portId));
2007 return statusTFromBinderStatus(aps->stopAudioSource(portIdAidl));
Eric Laurent554a2772015-04-10 11:29:24 -07002008}
2009
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002010status_t AudioSystem::setMasterMono(bool mono) {
Andy Hung264fa4c2024-02-21 15:52:12 -08002011 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Andy Hung2ddee192015-12-18 17:34:44 -08002012 if (aps == 0) return PERMISSION_DENIED;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002013 return statusTFromBinderStatus(aps->setMasterMono(mono));
Andy Hung2ddee192015-12-18 17:34:44 -08002014}
2015
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002016status_t AudioSystem::getMasterMono(bool* mono) {
2017 if (mono == nullptr) {
2018 return BAD_VALUE;
2019 }
Andy Hung264fa4c2024-02-21 15:52:12 -08002020 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Andy Hung2ddee192015-12-18 17:34:44 -08002021 if (aps == 0) return PERMISSION_DENIED;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002022 return statusTFromBinderStatus(aps->getMasterMono(mono));
Andy Hung2ddee192015-12-18 17:34:44 -08002023}
2024
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002025status_t AudioSystem::setMasterBalance(float balance) {
Andy Hung264fa4c2024-02-21 15:52:12 -08002026 const sp<IAudioFlinger> af = get_audio_flinger();
Richard Folke Tullberg3fae0372017-01-13 09:04:25 +01002027 if (af == 0) return PERMISSION_DENIED;
2028 return af->setMasterBalance(balance);
2029}
2030
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002031status_t AudioSystem::getMasterBalance(float* balance) {
Andy Hung264fa4c2024-02-21 15:52:12 -08002032 const sp<IAudioFlinger> af = get_audio_flinger();
Richard Folke Tullberg3fae0372017-01-13 09:04:25 +01002033 if (af == 0) return PERMISSION_DENIED;
2034 return af->getMasterBalance(balance);
2035}
2036
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002037float
2038AudioSystem::getStreamVolumeDB(audio_stream_type_t stream, int index, audio_devices_t device) {
Andy Hung264fa4c2024-02-21 15:52:12 -08002039 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Eric Laurentac9cef52017-06-09 15:46:26 -07002040 if (aps == 0) return NAN;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002041
2042 auto result = [&]() -> ConversionResult<float> {
Mikhail Naganovdbf03642021-08-25 18:15:32 -07002043 AudioStreamType streamAidl = VALUE_OR_RETURN(
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002044 legacy2aidl_audio_stream_type_t_AudioStreamType(stream));
2045 int32_t indexAidl = VALUE_OR_RETURN(convertIntegral<int32_t>(index));
Mikhail Naganovf4a75362021-09-16 00:02:54 +00002046 AudioDeviceDescription deviceAidl = VALUE_OR_RETURN(
Mikhail Naganov21a32ec2021-07-08 14:40:12 -07002047 legacy2aidl_audio_devices_t_AudioDeviceDescription(device));
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002048 float retAidl;
2049 RETURN_IF_ERROR(statusTFromBinderStatus(
2050 aps->getStreamVolumeDB(streamAidl, indexAidl, deviceAidl, &retAidl)));
2051 return retAidl;
2052 }();
2053 return result.value_or(NAN);
Eric Laurentac9cef52017-06-09 15:46:26 -07002054}
2055
Mikhail Naganovd5d9de72023-02-13 11:45:03 -08002056status_t AudioSystem::getMicrophones(std::vector<media::MicrophoneInfoFw>* microphones) {
Andy Hung264fa4c2024-02-21 15:52:12 -08002057 const sp<IAudioFlinger> af = get_audio_flinger();
jiabin46a76fa2018-01-05 10:18:21 -08002058 if (af == 0) return PERMISSION_DENIED;
2059 return af->getMicrophones(microphones);
2060}
2061
Eric Laurent42896a02019-09-27 15:40:33 -07002062status_t AudioSystem::setAudioHalPids(const std::vector<pid_t>& pids) {
Andy Hung264fa4c2024-02-21 15:52:12 -08002063 const sp<IAudioFlinger> af = get_audio_flinger();
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002064 if (af == nullptr) return PERMISSION_DENIED;
2065 return af->setAudioHalPids(pids);
Eric Laurent42896a02019-09-27 15:40:33 -07002066}
2067
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002068status_t AudioSystem::getSurroundFormats(unsigned int* numSurroundFormats,
2069 audio_format_t* surroundFormats,
Kriti Dang6537def2021-03-02 13:46:59 +01002070 bool* surroundFormatsEnabled) {
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002071 if (numSurroundFormats == nullptr || (*numSurroundFormats != 0 &&
2072 (surroundFormats == nullptr ||
2073 surroundFormatsEnabled == nullptr))) {
2074 return BAD_VALUE;
2075 }
2076
Andy Hung264fa4c2024-02-21 15:52:12 -08002077 const sp<IAudioPolicyService> aps = get_audio_policy_service();
jiabin81772902018-04-02 17:52:27 -07002078 if (aps == 0) return PERMISSION_DENIED;
Mikhail Naganov0078ee52021-09-30 23:06:20 +00002079 Int numSurroundFormatsAidl;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002080 numSurroundFormatsAidl.value =
2081 VALUE_OR_RETURN_STATUS(convertIntegral<int32_t>(*numSurroundFormats));
Mikhail Naganov57bd06f2021-08-10 16:41:54 -07002082 std::vector<AudioFormatDescription> surroundFormatsAidl;
Kriti Dang877b27e2021-02-02 12:10:40 +01002083 std::vector<bool> surroundFormatsEnabledAidl;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002084 RETURN_STATUS_IF_ERROR(statusTFromBinderStatus(
Kriti Dang6537def2021-03-02 13:46:59 +01002085 aps->getSurroundFormats(&numSurroundFormatsAidl, &surroundFormatsAidl,
2086 &surroundFormatsEnabledAidl)));
Kriti Dang877b27e2021-02-02 12:10:40 +01002087
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002088 *numSurroundFormats = VALUE_OR_RETURN_STATUS(
2089 convertIntegral<unsigned int>(numSurroundFormatsAidl.value));
2090 RETURN_STATUS_IF_ERROR(
2091 convertRange(surroundFormatsAidl.begin(), surroundFormatsAidl.end(), surroundFormats,
Mikhail Naganovb60bd1b2021-07-15 17:31:43 -07002092 aidl2legacy_AudioFormatDescription_audio_format_t));
Kriti Dang877b27e2021-02-02 12:10:40 +01002093 std::copy(surroundFormatsEnabledAidl.begin(), surroundFormatsEnabledAidl.end(),
2094 surroundFormatsEnabled);
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002095 return OK;
jiabin81772902018-04-02 17:52:27 -07002096}
2097
Kriti Dang6537def2021-03-02 13:46:59 +01002098status_t AudioSystem::getReportedSurroundFormats(unsigned int* numSurroundFormats,
2099 audio_format_t* surroundFormats) {
2100 if (numSurroundFormats == nullptr || (*numSurroundFormats != 0 && surroundFormats == nullptr)) {
2101 return BAD_VALUE;
2102 }
2103
Andy Hung264fa4c2024-02-21 15:52:12 -08002104 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Kriti Dang6537def2021-03-02 13:46:59 +01002105 if (aps == 0) return PERMISSION_DENIED;
Mikhail Naganov0078ee52021-09-30 23:06:20 +00002106 Int numSurroundFormatsAidl;
Kriti Dang6537def2021-03-02 13:46:59 +01002107 numSurroundFormatsAidl.value =
2108 VALUE_OR_RETURN_STATUS(convertIntegral<int32_t>(*numSurroundFormats));
Mikhail Naganov57bd06f2021-08-10 16:41:54 -07002109 std::vector<AudioFormatDescription> surroundFormatsAidl;
Kriti Dang6537def2021-03-02 13:46:59 +01002110 RETURN_STATUS_IF_ERROR(statusTFromBinderStatus(
2111 aps->getReportedSurroundFormats(&numSurroundFormatsAidl, &surroundFormatsAidl)));
2112
2113 *numSurroundFormats = VALUE_OR_RETURN_STATUS(
2114 convertIntegral<unsigned int>(numSurroundFormatsAidl.value));
2115 RETURN_STATUS_IF_ERROR(
2116 convertRange(surroundFormatsAidl.begin(), surroundFormatsAidl.end(), surroundFormats,
Mikhail Naganovb60bd1b2021-07-15 17:31:43 -07002117 aidl2legacy_AudioFormatDescription_audio_format_t));
Kriti Dang6537def2021-03-02 13:46:59 +01002118 return OK;
2119}
2120
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002121status_t AudioSystem::setSurroundFormatEnabled(audio_format_t audioFormat, bool enabled) {
Andy Hung264fa4c2024-02-21 15:52:12 -08002122 const sp<IAudioPolicyService> aps = get_audio_policy_service();
jiabin81772902018-04-02 17:52:27 -07002123 if (aps == 0) return PERMISSION_DENIED;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002124
Mikhail Naganov57bd06f2021-08-10 16:41:54 -07002125 AudioFormatDescription audioFormatAidl = VALUE_OR_RETURN_STATUS(
Mikhail Naganovb60bd1b2021-07-15 17:31:43 -07002126 legacy2aidl_audio_format_t_AudioFormatDescription(audioFormat));
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002127 return statusTFromBinderStatus(
2128 aps->setSurroundFormatEnabled(audioFormatAidl, enabled));
jiabin81772902018-04-02 17:52:27 -07002129}
2130
Oscar Azucena829d90d2022-01-28 17:17:56 -08002131status_t AudioSystem::setAssistantServicesUids(const std::vector<uid_t>& uids) {
Andy Hung264fa4c2024-02-21 15:52:12 -08002132 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Eric Laurentb78763e2018-10-17 10:08:02 -07002133 if (aps == 0) return PERMISSION_DENIED;
2134
Oscar Azucena829d90d2022-01-28 17:17:56 -08002135 std::vector<int32_t> uidsAidl = VALUE_OR_RETURN_STATUS(
2136 convertContainer<std::vector<int32_t>>(uids, legacy2aidl_uid_t_int32_t));
2137 return statusTFromBinderStatus(aps->setAssistantServicesUids(uidsAidl));
Ahaan Ugalef51ce002021-08-04 16:34:20 -07002138}
2139
Oscar Azucenac2cdda32022-01-31 19:10:39 -08002140status_t AudioSystem::setActiveAssistantServicesUids(const std::vector<uid_t>& activeUids) {
Andy Hung264fa4c2024-02-21 15:52:12 -08002141 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Oscar Azucenac2cdda32022-01-31 19:10:39 -08002142 if (aps == 0) return PERMISSION_DENIED;
2143
2144 std::vector<int32_t> activeUidsAidl = VALUE_OR_RETURN_STATUS(
2145 convertContainer<std::vector<int32_t>>(activeUids, legacy2aidl_uid_t_int32_t));
2146 return statusTFromBinderStatus(aps->setActiveAssistantServicesUids(activeUidsAidl));
2147}
2148
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002149status_t AudioSystem::setA11yServicesUids(const std::vector<uid_t>& uids) {
Andy Hung264fa4c2024-02-21 15:52:12 -08002150 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Eric Laurentb78763e2018-10-17 10:08:02 -07002151 if (aps == 0) return PERMISSION_DENIED;
2152
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002153 std::vector<int32_t> uidsAidl = VALUE_OR_RETURN_STATUS(
2154 convertContainer<std::vector<int32_t>>(uids, legacy2aidl_uid_t_int32_t));
2155 return statusTFromBinderStatus(aps->setA11yServicesUids(uidsAidl));
Eric Laurentb78763e2018-10-17 10:08:02 -07002156}
2157
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002158status_t AudioSystem::setCurrentImeUid(uid_t uid) {
Andy Hung264fa4c2024-02-21 15:52:12 -08002159 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Kohsuke Yatoha623a132020-03-24 20:10:26 -07002160 if (aps == 0) return PERMISSION_DENIED;
2161
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002162 int32_t uidAidl = VALUE_OR_RETURN_STATUS(legacy2aidl_uid_t_int32_t(uid));
2163 return statusTFromBinderStatus(aps->setCurrentImeUid(uidAidl));
Kohsuke Yatoha623a132020-03-24 20:10:26 -07002164}
2165
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002166bool AudioSystem::isHapticPlaybackSupported() {
Andy Hung264fa4c2024-02-21 15:52:12 -08002167 const sp<IAudioPolicyService> aps = get_audio_policy_service();
jiabin6012f912018-11-02 17:06:30 -07002168 if (aps == 0) return false;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002169
2170 auto result = [&]() -> ConversionResult<bool> {
2171 bool retVal;
2172 RETURN_IF_ERROR(
2173 statusTFromBinderStatus(aps->isHapticPlaybackSupported(&retVal)));
2174 return retVal;
2175 }();
2176 return result.value_or(false);
jiabin6012f912018-11-02 17:06:30 -07002177}
2178
Carter Hsu325a8eb2022-01-19 19:56:51 +08002179bool AudioSystem::isUltrasoundSupported() {
Andy Hung264fa4c2024-02-21 15:52:12 -08002180 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Carter Hsu325a8eb2022-01-19 19:56:51 +08002181 if (aps == 0) return false;
2182
2183 auto result = [&]() -> ConversionResult<bool> {
2184 bool retVal;
2185 RETURN_IF_ERROR(
2186 statusTFromBinderStatus(aps->isUltrasoundSupported(&retVal)));
2187 return retVal;
2188 }();
2189 return result.value_or(false);
2190}
2191
Pattydd807582021-11-04 21:01:03 +08002192status_t AudioSystem::getHwOffloadFormatsSupportedForBluetoothMedia(
2193 audio_devices_t device, std::vector<audio_format_t>* formats) {
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002194 if (formats == nullptr) {
2195 return BAD_VALUE;
2196 }
2197
Andy Hung264fa4c2024-02-21 15:52:12 -08002198 const sp<IAudioPolicyService> aps = get_audio_policy_service();
François Gaffied0ba9ed2018-11-05 11:50:42 +01002199 if (aps == 0) return PERMISSION_DENIED;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002200
Mikhail Naganov57bd06f2021-08-10 16:41:54 -07002201 std::vector<AudioFormatDescription> formatsAidl;
Pattydd807582021-11-04 21:01:03 +08002202 AudioDeviceDescription deviceAidl = VALUE_OR_RETURN_STATUS(
2203 legacy2aidl_audio_devices_t_AudioDeviceDescription(device));
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002204 RETURN_STATUS_IF_ERROR(statusTFromBinderStatus(
Pattydd807582021-11-04 21:01:03 +08002205 aps->getHwOffloadFormatsSupportedForBluetoothMedia(deviceAidl, &formatsAidl)));
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002206 *formats = VALUE_OR_RETURN_STATUS(
Mikhail Naganovb60bd1b2021-07-15 17:31:43 -07002207 convertContainer<std::vector<audio_format_t>>(
2208 formatsAidl,
2209 aidl2legacy_AudioFormatDescription_audio_format_t));
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002210 return OK;
François Gaffied0ba9ed2018-11-05 11:50:42 +01002211}
2212
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002213status_t AudioSystem::listAudioProductStrategies(AudioProductStrategyVector& strategies) {
Andy Hung264fa4c2024-02-21 15:52:12 -08002214 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Arun Mirpuri11029ad2018-12-19 20:45:19 -08002215 if (aps == 0) return PERMISSION_DENIED;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002216
2217 std::vector<media::AudioProductStrategy> strategiesAidl;
2218 RETURN_STATUS_IF_ERROR(statusTFromBinderStatus(
2219 aps->listAudioProductStrategies(&strategiesAidl)));
2220 strategies = VALUE_OR_RETURN_STATUS(
2221 convertContainer<AudioProductStrategyVector>(strategiesAidl,
2222 aidl2legacy_AudioProductStrategy));
2223 return OK;
François Gaffied0ba9ed2018-11-05 11:50:42 +01002224}
2225
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002226audio_attributes_t AudioSystem::streamTypeToAttributes(audio_stream_type_t stream) {
François Gaffied0ba9ed2018-11-05 11:50:42 +01002227 AudioProductStrategyVector strategies;
2228 listAudioProductStrategies(strategies);
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002229 for (const auto& strategy : strategies) {
François Gaffie1e2b56f2022-04-01 14:34:29 +02002230 auto attrVect = strategy.getVolumeGroupAttributes();
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002231 auto iter = std::find_if(begin(attrVect), end(attrVect), [&stream](const auto& attributes) {
2232 return attributes.getStreamType() == stream;
2233 });
François Gaffied0ba9ed2018-11-05 11:50:42 +01002234 if (iter != end(attrVect)) {
2235 return iter->getAttributes();
2236 }
2237 }
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002238 ALOGE("invalid stream type %s when converting to attributes", toString(stream).c_str());
François Gaffied0ba9ed2018-11-05 11:50:42 +01002239 return AUDIO_ATTRIBUTES_INITIALIZER;
2240}
2241
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002242audio_stream_type_t AudioSystem::attributesToStreamType(const audio_attributes_t& attr) {
François Gaffie4b2018b2018-11-07 11:18:59 +01002243 product_strategy_t psId;
François Gaffie1e2b56f2022-04-01 14:34:29 +02002244 status_t ret = AudioSystem::getProductStrategyFromAudioAttributes(attr, psId);
François Gaffie4b2018b2018-11-07 11:18:59 +01002245 if (ret != NO_ERROR) {
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002246 ALOGE("no strategy found for attributes %s", toString(attr).c_str());
François Gaffie4b2018b2018-11-07 11:18:59 +01002247 return AUDIO_STREAM_MUSIC;
2248 }
François Gaffied0ba9ed2018-11-05 11:50:42 +01002249 AudioProductStrategyVector strategies;
2250 listAudioProductStrategies(strategies);
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002251 for (const auto& strategy : strategies) {
François Gaffie4b2018b2018-11-07 11:18:59 +01002252 if (strategy.getId() == psId) {
François Gaffie1e2b56f2022-04-01 14:34:29 +02002253 auto attrVect = strategy.getVolumeGroupAttributes();
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002254 auto iter = std::find_if(begin(attrVect), end(attrVect), [&attr](const auto& refAttr) {
François Gaffie8d7fd5a2023-01-17 17:28:09 +01002255 return refAttr.matchesScore(attr) > 0;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002256 });
François Gaffied0ba9ed2018-11-05 11:50:42 +01002257 if (iter != end(attrVect)) {
2258 return iter->getStreamType();
2259 }
2260 }
2261 }
Jean-Michel Trivied678652019-12-19 13:39:30 -08002262 switch (attr.usage) {
2263 case AUDIO_USAGE_VIRTUAL_SOURCE:
2264 // virtual source is not expected to have an associated product strategy
2265 break;
2266 default:
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002267 ALOGE("invalid attributes %s when converting to stream", toString(attr).c_str());
Jean-Michel Trivied678652019-12-19 13:39:30 -08002268 break;
2269 }
François Gaffied0ba9ed2018-11-05 11:50:42 +01002270 return AUDIO_STREAM_MUSIC;
2271}
2272
François Gaffie1e2b56f2022-04-01 14:34:29 +02002273status_t AudioSystem::getProductStrategyFromAudioAttributes(const audio_attributes_t& aa,
Francois Gaffie11b65922020-09-24 16:59:08 +02002274 product_strategy_t& productStrategy,
2275 bool fallbackOnDefault) {
Andy Hung264fa4c2024-02-21 15:52:12 -08002276 const sp<IAudioPolicyService> aps = get_audio_policy_service();
François Gaffie4b2018b2018-11-07 11:18:59 +01002277 if (aps == 0) return PERMISSION_DENIED;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002278
Mikhail Naganov1c400902023-05-17 11:48:43 -07002279 media::audio::common::AudioAttributes aaAidl = VALUE_OR_RETURN_STATUS(
2280 legacy2aidl_audio_attributes_t_AudioAttributes(aa));
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002281 int32_t productStrategyAidl;
2282
2283 RETURN_STATUS_IF_ERROR(statusTFromBinderStatus(
Francois Gaffie11b65922020-09-24 16:59:08 +02002284 aps->getProductStrategyFromAudioAttributes(aaAidl, fallbackOnDefault,
2285 &productStrategyAidl)));
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002286 productStrategy = VALUE_OR_RETURN_STATUS(
2287 aidl2legacy_int32_t_product_strategy_t(productStrategyAidl));
2288 return OK;
François Gaffie4b2018b2018-11-07 11:18:59 +01002289}
2290
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002291status_t AudioSystem::listAudioVolumeGroups(AudioVolumeGroupVector& groups) {
Andy Hung264fa4c2024-02-21 15:52:12 -08002292 const sp<IAudioPolicyService> aps = get_audio_policy_service();
François Gaffie4b2018b2018-11-07 11:18:59 +01002293 if (aps == 0) return PERMISSION_DENIED;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002294
2295 std::vector<media::AudioVolumeGroup> groupsAidl;
2296 RETURN_STATUS_IF_ERROR(
2297 statusTFromBinderStatus(aps->listAudioVolumeGroups(&groupsAidl)));
2298 groups = VALUE_OR_RETURN_STATUS(
2299 convertContainer<AudioVolumeGroupVector>(groupsAidl, aidl2legacy_AudioVolumeGroup));
2300 return OK;
François Gaffie4b2018b2018-11-07 11:18:59 +01002301}
2302
François Gaffie1e2b56f2022-04-01 14:34:29 +02002303status_t AudioSystem::getVolumeGroupFromAudioAttributes(const audio_attributes_t &aa,
Francois Gaffie11b65922020-09-24 16:59:08 +02002304 volume_group_t& volumeGroup,
2305 bool fallbackOnDefault) {
Andy Hung264fa4c2024-02-21 15:52:12 -08002306 const sp<IAudioPolicyService> aps = get_audio_policy_service();
François Gaffie4b2018b2018-11-07 11:18:59 +01002307 if (aps == 0) return PERMISSION_DENIED;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002308
Mikhail Naganov1c400902023-05-17 11:48:43 -07002309 media::audio::common::AudioAttributes aaAidl = VALUE_OR_RETURN_STATUS(
2310 legacy2aidl_audio_attributes_t_AudioAttributes(aa));
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002311 int32_t volumeGroupAidl;
2312 RETURN_STATUS_IF_ERROR(statusTFromBinderStatus(
Francois Gaffie11b65922020-09-24 16:59:08 +02002313 aps->getVolumeGroupFromAudioAttributes(aaAidl, fallbackOnDefault, &volumeGroupAidl)));
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002314 volumeGroup = VALUE_OR_RETURN_STATUS(aidl2legacy_int32_t_volume_group_t(volumeGroupAidl));
2315 return OK;
Arun Mirpuri11029ad2018-12-19 20:45:19 -08002316}
Eric Laurentb78763e2018-10-17 10:08:02 -07002317
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002318status_t AudioSystem::setRttEnabled(bool enabled) {
Andy Hung264fa4c2024-02-21 15:52:12 -08002319 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Eric Laurent6ede98f2019-06-11 14:50:30 -07002320 if (aps == 0) return PERMISSION_DENIED;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002321 return statusTFromBinderStatus(aps->setRttEnabled(enabled));
Eric Laurent6ede98f2019-06-11 14:50:30 -07002322}
2323
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002324bool AudioSystem::isCallScreenModeSupported() {
Andy Hung264fa4c2024-02-21 15:52:12 -08002325 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Eric Laurent8340e672019-11-06 11:01:08 -08002326 if (aps == 0) return false;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002327
2328 auto result = [&]() -> ConversionResult<bool> {
2329 bool retAidl;
2330 RETURN_IF_ERROR(
2331 statusTFromBinderStatus(aps->isCallScreenModeSupported(&retAidl)));
2332 return retAidl;
2333 }();
2334 return result.value_or(false);
Eric Laurent8340e672019-11-06 11:01:08 -08002335}
2336
jiabin0a488932020-08-07 17:32:40 -07002337status_t AudioSystem::setDevicesRoleForStrategy(product_strategy_t strategy,
2338 device_role_t role,
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002339 const AudioDeviceTypeAddrVector& devices) {
Andy Hung264fa4c2024-02-21 15:52:12 -08002340 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Jean-Michel Trivi30857152019-11-01 11:04:15 -07002341 if (aps == 0) {
2342 return PERMISSION_DENIED;
2343 }
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002344
2345 int32_t strategyAidl = VALUE_OR_RETURN_STATUS(legacy2aidl_product_strategy_t_int32_t(strategy));
2346 media::DeviceRole roleAidl = VALUE_OR_RETURN_STATUS(legacy2aidl_device_role_t_DeviceRole(role));
Mikhail Naganovf4a75362021-09-16 00:02:54 +00002347 std::vector<AudioDevice> devicesAidl = VALUE_OR_RETURN_STATUS(
2348 convertContainer<std::vector<AudioDevice>>(devices,
2349 legacy2aidl_AudioDeviceTypeAddress));
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002350 return statusTFromBinderStatus(
2351 aps->setDevicesRoleForStrategy(strategyAidl, roleAidl, devicesAidl));
Jean-Michel Trivi30857152019-11-01 11:04:15 -07002352}
2353
Paul Wang5d7cdb52022-11-22 09:45:06 +00002354status_t AudioSystem::removeDevicesRoleForStrategy(product_strategy_t strategy,
2355 device_role_t role,
2356 const AudioDeviceTypeAddrVector& devices) {
Andy Hung264fa4c2024-02-21 15:52:12 -08002357 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Paul Wang5d7cdb52022-11-22 09:45:06 +00002358 if (aps == 0) {
2359 return PERMISSION_DENIED;
2360 }
2361
2362 int32_t strategyAidl = VALUE_OR_RETURN_STATUS(legacy2aidl_product_strategy_t_int32_t(strategy));
2363 media::DeviceRole roleAidl = VALUE_OR_RETURN_STATUS(legacy2aidl_device_role_t_DeviceRole(role));
2364 std::vector<AudioDevice> devicesAidl = VALUE_OR_RETURN_STATUS(
2365 convertContainer<std::vector<AudioDevice>>(devices,
2366 legacy2aidl_AudioDeviceTypeAddress));
2367 return statusTFromBinderStatus(
2368 aps->removeDevicesRoleForStrategy(strategyAidl, roleAidl, devicesAidl));
2369}
2370
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002371status_t
Paul Wang5d7cdb52022-11-22 09:45:06 +00002372AudioSystem::clearDevicesRoleForStrategy(product_strategy_t strategy, device_role_t role) {
Andy Hung264fa4c2024-02-21 15:52:12 -08002373 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Jean-Michel Trivi30857152019-11-01 11:04:15 -07002374 if (aps == 0) {
2375 return PERMISSION_DENIED;
2376 }
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002377 int32_t strategyAidl = VALUE_OR_RETURN_STATUS(legacy2aidl_product_strategy_t_int32_t(strategy));
2378 media::DeviceRole roleAidl = VALUE_OR_RETURN_STATUS(legacy2aidl_device_role_t_DeviceRole(role));
2379 return statusTFromBinderStatus(
Paul Wang5d7cdb52022-11-22 09:45:06 +00002380 aps->clearDevicesRoleForStrategy(strategyAidl, roleAidl));
Jean-Michel Trivi30857152019-11-01 11:04:15 -07002381}
2382
jiabin0a488932020-08-07 17:32:40 -07002383status_t AudioSystem::getDevicesForRoleAndStrategy(product_strategy_t strategy,
2384 device_role_t role,
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002385 AudioDeviceTypeAddrVector& devices) {
Andy Hung264fa4c2024-02-21 15:52:12 -08002386 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Jean-Michel Trivi30857152019-11-01 11:04:15 -07002387 if (aps == 0) {
2388 return PERMISSION_DENIED;
2389 }
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002390 int32_t strategyAidl = VALUE_OR_RETURN_STATUS(legacy2aidl_product_strategy_t_int32_t(strategy));
2391 media::DeviceRole roleAidl = VALUE_OR_RETURN_STATUS(legacy2aidl_device_role_t_DeviceRole(role));
Mikhail Naganovf4a75362021-09-16 00:02:54 +00002392 std::vector<AudioDevice> devicesAidl;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002393 RETURN_STATUS_IF_ERROR(statusTFromBinderStatus(
2394 aps->getDevicesForRoleAndStrategy(strategyAidl, roleAidl, &devicesAidl)));
2395 devices = VALUE_OR_RETURN_STATUS(
2396 convertContainer<AudioDeviceTypeAddrVector>(devicesAidl,
2397 aidl2legacy_AudioDeviceTypeAddress));
2398 return OK;
Jean-Michel Trivi30857152019-11-01 11:04:15 -07002399}
2400
Jiabin Huang3b98d322020-09-03 17:54:16 +00002401status_t AudioSystem::setDevicesRoleForCapturePreset(audio_source_t audioSource,
2402 device_role_t role,
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002403 const AudioDeviceTypeAddrVector& devices) {
Andy Hung264fa4c2024-02-21 15:52:12 -08002404 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Jiabin Huang3b98d322020-09-03 17:54:16 +00002405 if (aps == 0) {
2406 return PERMISSION_DENIED;
2407 }
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002408
Mikhail Naganovddceecc2021-09-03 13:58:56 -07002409 AudioSource audioSourceAidl = VALUE_OR_RETURN_STATUS(
2410 legacy2aidl_audio_source_t_AudioSource(audioSource));
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002411 media::DeviceRole roleAidl = VALUE_OR_RETURN_STATUS(legacy2aidl_device_role_t_DeviceRole(role));
Mikhail Naganovf4a75362021-09-16 00:02:54 +00002412 std::vector<AudioDevice> devicesAidl = VALUE_OR_RETURN_STATUS(
2413 convertContainer<std::vector<AudioDevice>>(devices,
2414 legacy2aidl_AudioDeviceTypeAddress));
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002415 return statusTFromBinderStatus(
2416 aps->setDevicesRoleForCapturePreset(audioSourceAidl, roleAidl, devicesAidl));
Jiabin Huang3b98d322020-09-03 17:54:16 +00002417}
2418
2419status_t AudioSystem::addDevicesRoleForCapturePreset(audio_source_t audioSource,
2420 device_role_t role,
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002421 const AudioDeviceTypeAddrVector& devices) {
Andy Hung264fa4c2024-02-21 15:52:12 -08002422 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Jiabin Huang3b98d322020-09-03 17:54:16 +00002423 if (aps == 0) {
2424 return PERMISSION_DENIED;
2425 }
Mikhail Naganovddceecc2021-09-03 13:58:56 -07002426 AudioSource audioSourceAidl = VALUE_OR_RETURN_STATUS(
2427 legacy2aidl_audio_source_t_AudioSource(audioSource));
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002428 media::DeviceRole roleAidl = VALUE_OR_RETURN_STATUS(legacy2aidl_device_role_t_DeviceRole(role));
Mikhail Naganovf4a75362021-09-16 00:02:54 +00002429 std::vector<AudioDevice> devicesAidl = VALUE_OR_RETURN_STATUS(
2430 convertContainer<std::vector<AudioDevice>>(devices,
2431 legacy2aidl_AudioDeviceTypeAddress));
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002432 return statusTFromBinderStatus(
2433 aps->addDevicesRoleForCapturePreset(audioSourceAidl, roleAidl, devicesAidl));
Jiabin Huang3b98d322020-09-03 17:54:16 +00002434}
2435
2436status_t AudioSystem::removeDevicesRoleForCapturePreset(
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002437 audio_source_t audioSource, device_role_t role, const AudioDeviceTypeAddrVector& devices) {
Andy Hung264fa4c2024-02-21 15:52:12 -08002438 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Jiabin Huang3b98d322020-09-03 17:54:16 +00002439 if (aps == 0) {
2440 return PERMISSION_DENIED;
2441 }
Mikhail Naganovddceecc2021-09-03 13:58:56 -07002442 AudioSource audioSourceAidl = VALUE_OR_RETURN_STATUS(
2443 legacy2aidl_audio_source_t_AudioSource(audioSource));
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002444 media::DeviceRole roleAidl = VALUE_OR_RETURN_STATUS(legacy2aidl_device_role_t_DeviceRole(role));
Mikhail Naganovf4a75362021-09-16 00:02:54 +00002445 std::vector<AudioDevice> devicesAidl = VALUE_OR_RETURN_STATUS(
2446 convertContainer<std::vector<AudioDevice>>(devices,
2447 legacy2aidl_AudioDeviceTypeAddress));
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002448 return statusTFromBinderStatus(
2449 aps->removeDevicesRoleForCapturePreset(audioSourceAidl, roleAidl, devicesAidl));
Jiabin Huang3b98d322020-09-03 17:54:16 +00002450}
2451
2452status_t AudioSystem::clearDevicesRoleForCapturePreset(audio_source_t audioSource,
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002453 device_role_t role) {
Andy Hung264fa4c2024-02-21 15:52:12 -08002454 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Jiabin Huang3b98d322020-09-03 17:54:16 +00002455 if (aps == 0) {
2456 return PERMISSION_DENIED;
2457 }
Mikhail Naganovddceecc2021-09-03 13:58:56 -07002458 AudioSource audioSourceAidl = VALUE_OR_RETURN_STATUS(
2459 legacy2aidl_audio_source_t_AudioSource(audioSource));
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002460 media::DeviceRole roleAidl = VALUE_OR_RETURN_STATUS(legacy2aidl_device_role_t_DeviceRole(role));
2461 return statusTFromBinderStatus(
2462 aps->clearDevicesRoleForCapturePreset(audioSourceAidl, roleAidl));
Jiabin Huang3b98d322020-09-03 17:54:16 +00002463}
2464
2465status_t AudioSystem::getDevicesForRoleAndCapturePreset(audio_source_t audioSource,
2466 device_role_t role,
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002467 AudioDeviceTypeAddrVector& devices) {
Andy Hung264fa4c2024-02-21 15:52:12 -08002468 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Jiabin Huang3b98d322020-09-03 17:54:16 +00002469 if (aps == 0) {
2470 return PERMISSION_DENIED;
2471 }
Mikhail Naganovddceecc2021-09-03 13:58:56 -07002472 AudioSource audioSourceAidl = VALUE_OR_RETURN_STATUS(
2473 legacy2aidl_audio_source_t_AudioSource(audioSource));
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002474 media::DeviceRole roleAidl = VALUE_OR_RETURN_STATUS(legacy2aidl_device_role_t_DeviceRole(role));
Mikhail Naganovf4a75362021-09-16 00:02:54 +00002475 std::vector<AudioDevice> devicesAidl;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002476 RETURN_STATUS_IF_ERROR(statusTFromBinderStatus(
2477 aps->getDevicesForRoleAndCapturePreset(audioSourceAidl, roleAidl, &devicesAidl)));
2478 devices = VALUE_OR_RETURN_STATUS(
2479 convertContainer<AudioDeviceTypeAddrVector>(devicesAidl,
2480 aidl2legacy_AudioDeviceTypeAddress));
2481 return OK;
Jiabin Huang3b98d322020-09-03 17:54:16 +00002482}
2483
Eric Laurent81dd0f52021-07-05 11:54:40 +02002484status_t AudioSystem::getSpatializer(const sp<media::INativeSpatializerCallback>& callback,
2485 sp<media::ISpatializer>* spatializer) {
Andy Hung264fa4c2024-02-21 15:52:12 -08002486 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Eric Laurent81dd0f52021-07-05 11:54:40 +02002487 if (spatializer == nullptr) {
2488 return BAD_VALUE;
2489 }
2490 if (aps == 0) {
2491 return PERMISSION_DENIED;
2492 }
2493 media::GetSpatializerResponse response;
2494 RETURN_STATUS_IF_ERROR(statusTFromBinderStatus(
2495 aps->getSpatializer(callback, &response)));
2496
2497 *spatializer = response.spatializer;
2498 return OK;
2499}
2500
2501status_t AudioSystem::canBeSpatialized(const audio_attributes_t *attr,
2502 const audio_config_t *config,
2503 const AudioDeviceTypeAddrVector &devices,
2504 bool *canBeSpatialized) {
Andy Hung264fa4c2024-02-21 15:52:12 -08002505 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Ram Mohan43297012022-08-30 16:11:49 +05302506 if (canBeSpatialized == nullptr) {
2507 return BAD_VALUE;
2508 }
Eric Laurent81dd0f52021-07-05 11:54:40 +02002509 if (aps == 0) {
2510 return PERMISSION_DENIED;
2511 }
2512 audio_attributes_t attributes = attr != nullptr ? *attr : AUDIO_ATTRIBUTES_INITIALIZER;
2513 audio_config_t configuration = config != nullptr ? *config : AUDIO_CONFIG_INITIALIZER;
2514
Mikhail Naganov1c400902023-05-17 11:48:43 -07002515 std::optional<media::audio::common::AudioAttributes> attrAidl = VALUE_OR_RETURN_STATUS(
2516 legacy2aidl_audio_attributes_t_AudioAttributes(attributes));
Mikhail Naganovdbf03642021-08-25 18:15:32 -07002517 std::optional<AudioConfig> configAidl = VALUE_OR_RETURN_STATUS(
Eric Laurent81dd0f52021-07-05 11:54:40 +02002518 legacy2aidl_audio_config_t_AudioConfig(configuration, false /*isInput*/));
Mikhail Naganovf4a75362021-09-16 00:02:54 +00002519 std::vector<AudioDevice> devicesAidl = VALUE_OR_RETURN_STATUS(
2520 convertContainer<std::vector<AudioDevice>>(devices,
2521 legacy2aidl_AudioDeviceTypeAddress));
Eric Laurent81dd0f52021-07-05 11:54:40 +02002522 RETURN_STATUS_IF_ERROR(statusTFromBinderStatus(
2523 aps->canBeSpatialized(attrAidl, configAidl, devicesAidl, canBeSpatialized)));
2524 return OK;
2525}
2526
Vlad Popae3fd1c22022-11-07 21:03:18 +01002527status_t AudioSystem::getSoundDoseInterface(const sp<media::ISoundDoseCallback>& callback,
2528 sp<media::ISoundDose>* soundDose) {
Andy Hung264fa4c2024-02-21 15:52:12 -08002529 const sp<IAudioFlinger> af = get_audio_flinger();
Vlad Popa63f047e2022-11-05 14:09:19 +01002530 if (af == nullptr) {
2531 return PERMISSION_DENIED;
2532 }
Vlad Popae3fd1c22022-11-07 21:03:18 +01002533 if (soundDose == nullptr) {
2534 return BAD_VALUE;
2535 }
Vlad Popa63f047e2022-11-05 14:09:19 +01002536
Vlad Popae3fd1c22022-11-07 21:03:18 +01002537 RETURN_STATUS_IF_ERROR(af->getSoundDoseInterface(callback, soundDose));
2538 return OK;
Vlad Popa63f047e2022-11-05 14:09:19 +01002539}
2540
jiabin2b9d5a12021-12-10 01:06:29 +00002541status_t AudioSystem::getDirectPlaybackSupport(const audio_attributes_t *attr,
2542 const audio_config_t *config,
2543 audio_direct_mode_t* directMode) {
2544 if (attr == nullptr || config == nullptr || directMode == nullptr) {
2545 return BAD_VALUE;
2546 }
2547
Andy Hung264fa4c2024-02-21 15:52:12 -08002548 const sp<IAudioPolicyService> aps = get_audio_policy_service();
jiabin2b9d5a12021-12-10 01:06:29 +00002549 if (aps == 0) {
2550 return PERMISSION_DENIED;
2551 }
2552
Mikhail Naganov1c400902023-05-17 11:48:43 -07002553 media::audio::common::AudioAttributes attrAidl = VALUE_OR_RETURN_STATUS(
2554 legacy2aidl_audio_attributes_t_AudioAttributes(*attr));
jiabin2b9d5a12021-12-10 01:06:29 +00002555 AudioConfig configAidl = VALUE_OR_RETURN_STATUS(
2556 legacy2aidl_audio_config_t_AudioConfig(*config, false /*isInput*/));
2557
2558 media::AudioDirectMode retAidl;
2559 RETURN_STATUS_IF_ERROR(statusTFromBinderStatus(
2560 aps->getDirectPlaybackSupport(attrAidl, configAidl, &retAidl)));
2561 *directMode = VALUE_OR_RETURN_STATUS(aidl2legacy_int32_t_audio_direct_mode_t_mask(
2562 static_cast<int32_t>(retAidl)));
2563 return NO_ERROR;
2564}
2565
Dorin Drimusf2196d82022-01-03 12:11:18 +01002566status_t AudioSystem::getDirectProfilesForAttributes(const audio_attributes_t* attr,
2567 std::vector<audio_profile>* audioProfiles) {
Mikhail Naganovefc504b2022-06-22 03:24:59 +00002568 if (attr == nullptr || audioProfiles == nullptr) {
Dorin Drimusf2196d82022-01-03 12:11:18 +01002569 return BAD_VALUE;
2570 }
2571
Andy Hung264fa4c2024-02-21 15:52:12 -08002572 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Dorin Drimusf2196d82022-01-03 12:11:18 +01002573 if (aps == 0) {
2574 return PERMISSION_DENIED;
2575 }
2576
Mikhail Naganov1c400902023-05-17 11:48:43 -07002577 media::audio::common::AudioAttributes attrAidl = VALUE_OR_RETURN_STATUS(
2578 legacy2aidl_audio_attributes_t_AudioAttributes(*attr));
Dorin Drimusf2196d82022-01-03 12:11:18 +01002579
2580 std::vector<media::audio::common::AudioProfile> audioProfilesAidl;
2581 RETURN_STATUS_IF_ERROR(statusTFromBinderStatus(
2582 aps->getDirectProfilesForAttributes(attrAidl, &audioProfilesAidl)));
2583 *audioProfiles = VALUE_OR_RETURN_STATUS(convertContainer<std::vector<audio_profile>>(
2584 audioProfilesAidl, aidl2legacy_AudioProfile_audio_profile, false /*isInput*/));
2585
2586 return NO_ERROR;
2587}
Eric Laurent81dd0f52021-07-05 11:54:40 +02002588
Eric Laurent076e7c72022-05-03 18:12:28 +02002589status_t AudioSystem::setRequestedLatencyMode(
2590 audio_io_handle_t output, audio_latency_mode_t mode) {
Andy Hung264fa4c2024-02-21 15:52:12 -08002591 const sp<IAudioFlinger> af = get_audio_flinger();
Eric Laurent076e7c72022-05-03 18:12:28 +02002592 if (af == nullptr) {
2593 return PERMISSION_DENIED;
2594 }
2595 return af->setRequestedLatencyMode(output, mode);
2596}
2597
2598status_t AudioSystem::getSupportedLatencyModes(audio_io_handle_t output,
2599 std::vector<audio_latency_mode_t>* modes) {
Andy Hung264fa4c2024-02-21 15:52:12 -08002600 const sp<IAudioFlinger> af = get_audio_flinger();
Eric Laurent076e7c72022-05-03 18:12:28 +02002601 if (af == nullptr) {
2602 return PERMISSION_DENIED;
2603 }
2604 return af->getSupportedLatencyModes(output, modes);
2605}
2606
Eric Laurent50d72582022-12-20 20:20:23 +01002607status_t AudioSystem::setBluetoothVariableLatencyEnabled(bool enabled) {
Andy Hung264fa4c2024-02-21 15:52:12 -08002608 const sp<IAudioFlinger> af = get_audio_flinger();
Eric Laurent52057642022-12-16 11:45:07 +01002609 if (af == nullptr) {
2610 return PERMISSION_DENIED;
2611 }
Eric Laurent50d72582022-12-20 20:20:23 +01002612 return af->setBluetoothVariableLatencyEnabled(enabled);
Eric Laurent52057642022-12-16 11:45:07 +01002613}
2614
Eric Laurent50d72582022-12-20 20:20:23 +01002615status_t AudioSystem::isBluetoothVariableLatencyEnabled(
2616 bool *enabled) {
Andy Hung264fa4c2024-02-21 15:52:12 -08002617 const sp<IAudioFlinger> af = get_audio_flinger();
Eric Laurent50d72582022-12-20 20:20:23 +01002618 if (af == nullptr) {
2619 return PERMISSION_DENIED;
2620 }
2621 return af->isBluetoothVariableLatencyEnabled(enabled);
2622}
2623
2624status_t AudioSystem::supportsBluetoothVariableLatency(
Eric Laurent52057642022-12-16 11:45:07 +01002625 bool *support) {
Andy Hung264fa4c2024-02-21 15:52:12 -08002626 const sp<IAudioFlinger> af = get_audio_flinger();
Eric Laurent52057642022-12-16 11:45:07 +01002627 if (af == nullptr) {
2628 return PERMISSION_DENIED;
2629 }
Eric Laurent50d72582022-12-20 20:20:23 +01002630 return af->supportsBluetoothVariableLatency(support);
Eric Laurent52057642022-12-16 11:45:07 +01002631}
2632
Mikhail Naganovffd97712023-05-03 17:45:36 -07002633status_t AudioSystem::getAudioPolicyConfig(media::AudioPolicyConfig *config) {
Andy Hung264fa4c2024-02-21 15:52:12 -08002634 const sp<IAudioFlinger> af = get_audio_flinger();
Mikhail Naganovffd97712023-05-03 17:45:36 -07002635 if (af == nullptr) {
2636 return PERMISSION_DENIED;
2637 }
2638 return af->getAudioPolicyConfig(config);
2639}
2640
Ytai Ben-Tsvi5120eda2020-03-26 09:41:15 -07002641class CaptureStateListenerImpl : public media::BnCaptureStateListener,
2642 public IBinder::DeathRecipient {
2643public:
Ytai Ben-Tsvid9f82832020-08-27 09:31:10 -07002644 CaptureStateListenerImpl(
2645 const sp<IAudioPolicyService>& aps,
2646 const sp<AudioSystem::CaptureStateListener>& listener)
Ytai Ben-Tsvia46b6d32020-08-31 13:29:11 -07002647 : mAps(aps), mListener(listener) {}
2648
2649 void init() {
Ytai Ben-Tsvid9f82832020-08-27 09:31:10 -07002650 bool active;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002651 status_t status = statusTFromBinderStatus(
2652 mAps->registerSoundTriggerCaptureStateListener(this, &active));
Ytai Ben-Tsvid9f82832020-08-27 09:31:10 -07002653 if (status != NO_ERROR) {
2654 mListener->onServiceDied();
2655 return;
2656 }
2657 mListener->onStateChanged(active);
Ytai Ben-Tsvia46b6d32020-08-31 13:29:11 -07002658 IInterface::asBinder(mAps)->linkToDeath(this);
Ytai Ben-Tsvid9f82832020-08-27 09:31:10 -07002659 }
2660
Ytai Ben-Tsvi5120eda2020-03-26 09:41:15 -07002661 binder::Status setCaptureState(bool active) override {
Andy Hungacd955a2024-02-16 17:14:36 -08002662 std::lock_guard _l(AudioSystem::gSoundTriggerMutex);
Ytai Ben-Tsvid9f82832020-08-27 09:31:10 -07002663 mListener->onStateChanged(active);
Ytai Ben-Tsvi5120eda2020-03-26 09:41:15 -07002664 return binder::Status::ok();
2665 }
2666
2667 void binderDied(const wp<IBinder>&) override {
Andy Hungacd955a2024-02-16 17:14:36 -08002668 std::lock_guard _l(AudioSystem::gSoundTriggerMutex);
Ytai Ben-Tsvid9f82832020-08-27 09:31:10 -07002669 mListener->onServiceDied();
Andy Hungacd955a2024-02-16 17:14:36 -08002670 AudioSystem::gSoundTriggerCaptureStateListener = nullptr;
Ytai Ben-Tsvi5120eda2020-03-26 09:41:15 -07002671 }
Ytai Ben-Tsvid9f82832020-08-27 09:31:10 -07002672
2673private:
2674 // Need this in order to keep the death receipent alive.
2675 sp<IAudioPolicyService> mAps;
2676 sp<AudioSystem::CaptureStateListener> mListener;
Ytai Ben-Tsvi5120eda2020-03-26 09:41:15 -07002677};
2678
2679status_t AudioSystem::registerSoundTriggerCaptureStateListener(
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002680 const sp<CaptureStateListener>& listener) {
Ytai Ben-Tsvid9f82832020-08-27 09:31:10 -07002681 LOG_ALWAYS_FATAL_IF(listener == nullptr);
2682
Andy Hung264fa4c2024-02-21 15:52:12 -08002683 const sp<IAudioPolicyService> aps = get_audio_policy_service();
Ytai Ben-Tsvi5120eda2020-03-26 09:41:15 -07002684 if (aps == 0) {
2685 return PERMISSION_DENIED;
2686 }
2687
Andy Hungacd955a2024-02-16 17:14:36 -08002688 std::lock_guard _l(AudioSystem::gSoundTriggerMutex);
Ytai Ben-Tsvid9f82832020-08-27 09:31:10 -07002689 gSoundTriggerCaptureStateListener = new CaptureStateListenerImpl(aps, listener);
Ytai Ben-Tsvia46b6d32020-08-31 13:29:11 -07002690 gSoundTriggerCaptureStateListener->init();
Ytai Ben-Tsvi5120eda2020-03-26 09:41:15 -07002691
Ytai Ben-Tsvi5120eda2020-03-26 09:41:15 -07002692 return NO_ERROR;
2693}
2694
jiabin1319f5a2021-03-30 22:21:24 +00002695status_t AudioSystem::setVibratorInfos(
2696 const std::vector<media::AudioVibratorInfo>& vibratorInfos) {
Andy Hung264fa4c2024-02-21 15:52:12 -08002697 const sp<IAudioFlinger> af = get_audio_flinger();
jiabin1319f5a2021-03-30 22:21:24 +00002698 if (af == nullptr) {
2699 return PERMISSION_DENIED;
2700 }
2701 return af->setVibratorInfos(vibratorInfos);
2702}
2703
Jiabin Huangebe64102021-09-07 20:01:07 +00002704status_t AudioSystem::getMmapPolicyInfo(
jiabine99d0882021-09-17 05:21:25 +00002705 AudioMMapPolicyType policyType, std::vector<AudioMMapPolicyInfo> *policyInfos) {
Andy Hung264fa4c2024-02-21 15:52:12 -08002706 const sp<IAudioFlinger> af = get_audio_flinger();
Jiabin Huangebe64102021-09-07 20:01:07 +00002707 if (af == nullptr) {
2708 return PERMISSION_DENIED;
2709 }
2710 return af->getMmapPolicyInfos(policyType, policyInfos);
2711}
2712
jiabine504e7b2021-09-18 00:27:08 +00002713int32_t AudioSystem::getAAudioMixerBurstCount() {
Andy Hung264fa4c2024-02-21 15:52:12 -08002714 const sp<IAudioFlinger> af = get_audio_flinger();
jiabine504e7b2021-09-18 00:27:08 +00002715 if (af == nullptr) {
2716 return PERMISSION_DENIED;
2717 }
2718 return af->getAAudioMixerBurstCount();
2719}
2720
2721int32_t AudioSystem::getAAudioHardwareBurstMinUsec() {
Andy Hung264fa4c2024-02-21 15:52:12 -08002722 const sp<IAudioFlinger> af = get_audio_flinger();
jiabine504e7b2021-09-18 00:27:08 +00002723 if (af == nullptr) {
2724 return PERMISSION_DENIED;
2725 }
2726 return af->getAAudioHardwareBurstMinUsec();
2727}
2728
jiabina84c3d32022-12-02 18:59:55 +00002729status_t AudioSystem::getSupportedMixerAttributes(
2730 audio_port_handle_t portId, std::vector<audio_mixer_attributes_t> *mixerAttrs) {
Andy Hung264fa4c2024-02-21 15:52:12 -08002731 const sp<IAudioPolicyService> aps = get_audio_policy_service();
jiabina84c3d32022-12-02 18:59:55 +00002732 if (aps == nullptr) {
2733 return PERMISSION_DENIED;
2734 }
2735
2736 int32_t portIdAidl = VALUE_OR_RETURN_STATUS(legacy2aidl_audio_port_handle_t_int32_t(portId));
2737 std::vector<media::AudioMixerAttributesInternal> _aidlReturn;
2738 RETURN_STATUS_IF_ERROR(statusTFromBinderStatus(
2739 aps->getSupportedMixerAttributes(portIdAidl, &_aidlReturn)));
2740 *mixerAttrs = VALUE_OR_RETURN_STATUS(
2741 convertContainer<std::vector<audio_mixer_attributes_t>>(
2742 _aidlReturn,
2743 aidl2legacy_AudioMixerAttributesInternal_audio_mixer_attributes_t));
2744 return OK;
2745}
2746
2747status_t AudioSystem::setPreferredMixerAttributes(const audio_attributes_t *attr,
2748 audio_port_handle_t portId,
2749 uid_t uid,
2750 const audio_mixer_attributes_t *mixerAttr) {
Andy Hung264fa4c2024-02-21 15:52:12 -08002751 const sp<IAudioPolicyService> aps = get_audio_policy_service();
jiabina84c3d32022-12-02 18:59:55 +00002752 if (aps == nullptr) {
2753 return PERMISSION_DENIED;
2754 }
2755
Mikhail Naganov1c400902023-05-17 11:48:43 -07002756 media::audio::common::AudioAttributes attrAidl = VALUE_OR_RETURN_STATUS(
2757 legacy2aidl_audio_attributes_t_AudioAttributes(*attr));
jiabina84c3d32022-12-02 18:59:55 +00002758 media::AudioMixerAttributesInternal mixerAttrAidl = VALUE_OR_RETURN_STATUS(
2759 legacy2aidl_audio_mixer_attributes_t_AudioMixerAttributesInternal(*mixerAttr));
2760 int32_t uidAidl = VALUE_OR_RETURN_STATUS(legacy2aidl_uid_t_int32_t(uid));
2761 int32_t portIdAidl = VALUE_OR_RETURN_STATUS(legacy2aidl_audio_port_handle_t_int32_t(portId));
2762
2763 return statusTFromBinderStatus(
2764 aps->setPreferredMixerAttributes(attrAidl, portIdAidl, uidAidl, mixerAttrAidl));
2765}
2766
2767status_t AudioSystem::getPreferredMixerAttributes(
2768 const audio_attributes_t *attr,
2769 audio_port_handle_t portId,
2770 std::optional<audio_mixer_attributes_t> *mixerAttr) {
Andy Hung264fa4c2024-02-21 15:52:12 -08002771 const sp<IAudioPolicyService> aps = get_audio_policy_service();
jiabina84c3d32022-12-02 18:59:55 +00002772 if (aps == nullptr) {
2773 return PERMISSION_DENIED;
2774 }
2775
Mikhail Naganov1c400902023-05-17 11:48:43 -07002776 media::audio::common::AudioAttributes attrAidl = VALUE_OR_RETURN_STATUS(
2777 legacy2aidl_audio_attributes_t_AudioAttributes(*attr));
jiabina84c3d32022-12-02 18:59:55 +00002778 int32_t portIdAidl = VALUE_OR_RETURN_STATUS(legacy2aidl_audio_port_handle_t_int32_t(portId));
2779 std::optional<media::AudioMixerAttributesInternal> _aidlReturn;
2780 RETURN_STATUS_IF_ERROR(statusTFromBinderStatus(
2781 aps->getPreferredMixerAttributes(attrAidl, portIdAidl, &_aidlReturn)));
2782
2783 if (_aidlReturn.has_value()) {
2784 *mixerAttr = VALUE_OR_RETURN_STATUS(
2785 aidl2legacy_AudioMixerAttributesInternal_audio_mixer_attributes_t(
2786 _aidlReturn.value()));
2787 }
2788 return NO_ERROR;
2789}
2790
2791status_t AudioSystem::clearPreferredMixerAttributes(const audio_attributes_t *attr,
2792 audio_port_handle_t portId,
2793 uid_t uid) {
Andy Hung264fa4c2024-02-21 15:52:12 -08002794 const sp<IAudioPolicyService> aps = get_audio_policy_service();
jiabina84c3d32022-12-02 18:59:55 +00002795 if (aps == nullptr) {
2796 return PERMISSION_DENIED;
2797 }
2798
Mikhail Naganov1c400902023-05-17 11:48:43 -07002799 media::audio::common::AudioAttributes attrAidl = VALUE_OR_RETURN_STATUS(
2800 legacy2aidl_audio_attributes_t_AudioAttributes(*attr));
jiabina84c3d32022-12-02 18:59:55 +00002801 int32_t uidAidl = VALUE_OR_RETURN_STATUS(legacy2aidl_uid_t_int32_t(uid));
2802 int32_t portIdAidl = VALUE_OR_RETURN_STATUS(legacy2aidl_audio_port_handle_t_int32_t(portId));
2803 return statusTFromBinderStatus(
2804 aps->clearPreferredMixerAttributes(attrAidl, portIdAidl, uidAidl));
2805}
2806
Eric Laurentc2f1f072009-07-17 12:17:14 -07002807// ---------------------------------------------------------------------------
2808
Eric Laurente8726fe2015-06-26 09:39:24 -07002809int AudioSystem::AudioPolicyServiceClient::addAudioPortCallback(
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002810 const sp<AudioPortCallback>& callback) {
Andy Hungacd955a2024-02-16 17:14:36 -08002811 std::lock_guard _l(mMutex);
Andy Hungd211ce02024-02-16 15:34:07 -08002812 return mAudioPortCallbacks.insert(callback).second ? mAudioPortCallbacks.size() : -1;
Eric Laurentb28753e2015-04-01 13:06:28 -07002813}
2814
Eric Laurente8726fe2015-06-26 09:39:24 -07002815int AudioSystem::AudioPolicyServiceClient::removeAudioPortCallback(
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002816 const sp<AudioPortCallback>& callback) {
Andy Hungacd955a2024-02-16 17:14:36 -08002817 std::lock_guard _l(mMutex);
Andy Hungd211ce02024-02-16 15:34:07 -08002818 return mAudioPortCallbacks.erase(callback) > 0 ? mAudioPortCallbacks.size() : -1;
Eric Laurentb28753e2015-04-01 13:06:28 -07002819}
2820
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002821Status AudioSystem::AudioPolicyServiceClient::onAudioPortListUpdate() {
Andy Hungacd955a2024-02-16 17:14:36 -08002822 std::lock_guard _l(mMutex);
Andy Hungd211ce02024-02-16 15:34:07 -08002823 for (const auto& callback : mAudioPortCallbacks) {
2824 callback->onAudioPortListUpdate();
Eric Laurentb28753e2015-04-01 13:06:28 -07002825 }
Ytai Ben-Tsvi7e7a79d2020-12-15 16:48:16 -08002826 return Status::ok();
Eric Laurentb28753e2015-04-01 13:06:28 -07002827}
2828
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002829Status AudioSystem::AudioPolicyServiceClient::onAudioPatchListUpdate() {
Andy Hungacd955a2024-02-16 17:14:36 -08002830 std::lock_guard _l(mMutex);
Andy Hungd211ce02024-02-16 15:34:07 -08002831 for (const auto& callback : mAudioPortCallbacks) {
2832 callback->onAudioPatchListUpdate();
Eric Laurentb28753e2015-04-01 13:06:28 -07002833 }
Ytai Ben-Tsvi7e7a79d2020-12-15 16:48:16 -08002834 return Status::ok();
Eric Laurentb28753e2015-04-01 13:06:28 -07002835}
2836
François Gaffiecfe17322018-11-07 13:41:29 +01002837// ----------------------------------------------------------------------------
2838int AudioSystem::AudioPolicyServiceClient::addAudioVolumeGroupCallback(
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002839 const sp<AudioVolumeGroupCallback>& callback) {
Andy Hungacd955a2024-02-16 17:14:36 -08002840 std::lock_guard _l(mMutex);
Andy Hungd211ce02024-02-16 15:34:07 -08002841 return mAudioVolumeGroupCallbacks.insert(callback).second
2842 ? mAudioVolumeGroupCallbacks.size() : -1;
François Gaffiecfe17322018-11-07 13:41:29 +01002843}
2844
2845int AudioSystem::AudioPolicyServiceClient::removeAudioVolumeGroupCallback(
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002846 const sp<AudioVolumeGroupCallback>& callback) {
Andy Hungacd955a2024-02-16 17:14:36 -08002847 std::lock_guard _l(mMutex);
Andy Hungd211ce02024-02-16 15:34:07 -08002848 return mAudioVolumeGroupCallbacks.erase(callback) > 0
2849 ? mAudioVolumeGroupCallbacks.size() : -1;
François Gaffiecfe17322018-11-07 13:41:29 +01002850}
2851
Ytai Ben-Tsvi7e7a79d2020-12-15 16:48:16 -08002852Status AudioSystem::AudioPolicyServiceClient::onAudioVolumeGroupChanged(int32_t group,
2853 int32_t flags) {
2854 volume_group_t groupLegacy = VALUE_OR_RETURN_BINDER_STATUS(
2855 aidl2legacy_int32_t_volume_group_t(group));
2856 int flagsLegacy = VALUE_OR_RETURN_BINDER_STATUS(convertReinterpret<int>(flags));
2857
Andy Hungacd955a2024-02-16 17:14:36 -08002858 std::lock_guard _l(mMutex);
Andy Hungd211ce02024-02-16 15:34:07 -08002859 for (const auto& callback : mAudioVolumeGroupCallbacks) {
2860 callback->onAudioVolumeGroupChanged(groupLegacy, flagsLegacy);
François Gaffiecfe17322018-11-07 13:41:29 +01002861 }
Ytai Ben-Tsvi7e7a79d2020-12-15 16:48:16 -08002862 return Status::ok();
François Gaffiecfe17322018-11-07 13:41:29 +01002863}
2864// ----------------------------------------------------------------------------
2865
Ytai Ben-Tsvi7e7a79d2020-12-15 16:48:16 -08002866Status AudioSystem::AudioPolicyServiceClient::onDynamicPolicyMixStateUpdate(
2867 const ::std::string& regId, int32_t state) {
2868 ALOGV("AudioPolicyServiceClient::onDynamicPolicyMixStateUpdate(%s, %d)", regId.c_str(), state);
2869
2870 String8 regIdLegacy = VALUE_OR_RETURN_BINDER_STATUS(aidl2legacy_string_view_String8(regId));
2871 int stateLegacy = VALUE_OR_RETURN_BINDER_STATUS(convertReinterpret<int>(state));
Jean-Michel Trivif613d422015-04-23 18:41:29 -07002872 dynamic_policy_callback cb = NULL;
2873 {
Andy Hungacd955a2024-02-16 17:14:36 -08002874 std::lock_guard _l(AudioSystem::gMutex);
Jean-Michel Trivif613d422015-04-23 18:41:29 -07002875 cb = gDynPolicyCallback;
2876 }
2877
2878 if (cb != NULL) {
Ytai Ben-Tsvi7e7a79d2020-12-15 16:48:16 -08002879 cb(DYNAMIC_POLICY_EVENT_MIX_STATE_UPDATE, regIdLegacy, stateLegacy);
Jean-Michel Trivif613d422015-04-23 18:41:29 -07002880 }
Ytai Ben-Tsvi7e7a79d2020-12-15 16:48:16 -08002881 return Status::ok();
Jean-Michel Trivide801052015-04-14 19:10:14 -07002882}
2883
Ytai Ben-Tsvi7e7a79d2020-12-15 16:48:16 -08002884Status AudioSystem::AudioPolicyServiceClient::onRecordingConfigurationUpdate(
2885 int32_t event,
2886 const media::RecordClientInfo& clientInfo,
Mikhail Naganovdbf03642021-08-25 18:15:32 -07002887 const AudioConfigBase& clientConfig,
Ytai Ben-Tsvi7e7a79d2020-12-15 16:48:16 -08002888 const std::vector<media::EffectDescriptor>& clientEffects,
Mikhail Naganovdbf03642021-08-25 18:15:32 -07002889 const AudioConfigBase& deviceConfig,
Ytai Ben-Tsvi7e7a79d2020-12-15 16:48:16 -08002890 const std::vector<media::EffectDescriptor>& effects,
2891 int32_t patchHandle,
Mikhail Naganovddceecc2021-09-03 13:58:56 -07002892 AudioSource source) {
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -08002893 record_config_callback cb = NULL;
2894 {
Andy Hungacd955a2024-02-16 17:14:36 -08002895 std::lock_guard _l(AudioSystem::gMutex);
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -08002896 cb = gRecordConfigCallback;
2897 }
2898
2899 if (cb != NULL) {
Ytai Ben-Tsvi7e7a79d2020-12-15 16:48:16 -08002900 int eventLegacy = VALUE_OR_RETURN_BINDER_STATUS(convertReinterpret<int>(event));
2901 record_client_info_t clientInfoLegacy = VALUE_OR_RETURN_BINDER_STATUS(
2902 aidl2legacy_RecordClientInfo_record_client_info_t(clientInfo));
2903 audio_config_base_t clientConfigLegacy = VALUE_OR_RETURN_BINDER_STATUS(
Mikhail Naganovde3fa182021-07-30 15:06:42 -07002904 aidl2legacy_AudioConfigBase_audio_config_base_t(clientConfig, true /*isInput*/));
Ytai Ben-Tsvi7e7a79d2020-12-15 16:48:16 -08002905 std::vector<effect_descriptor_t> clientEffectsLegacy = VALUE_OR_RETURN_BINDER_STATUS(
2906 convertContainer<std::vector<effect_descriptor_t>>(
2907 clientEffects,
2908 aidl2legacy_EffectDescriptor_effect_descriptor_t));
2909 audio_config_base_t deviceConfigLegacy = VALUE_OR_RETURN_BINDER_STATUS(
Mikhail Naganovde3fa182021-07-30 15:06:42 -07002910 aidl2legacy_AudioConfigBase_audio_config_base_t(deviceConfig, true /*isInput*/));
Ytai Ben-Tsvi7e7a79d2020-12-15 16:48:16 -08002911 std::vector<effect_descriptor_t> effectsLegacy = VALUE_OR_RETURN_BINDER_STATUS(
2912 convertContainer<std::vector<effect_descriptor_t>>(
2913 effects,
2914 aidl2legacy_EffectDescriptor_effect_descriptor_t));
2915 audio_patch_handle_t patchHandleLegacy = VALUE_OR_RETURN_BINDER_STATUS(
2916 aidl2legacy_int32_t_audio_patch_handle_t(patchHandle));
2917 audio_source_t sourceLegacy = VALUE_OR_RETURN_BINDER_STATUS(
Mikhail Naganovddceecc2021-09-03 13:58:56 -07002918 aidl2legacy_AudioSource_audio_source_t(source));
Ytai Ben-Tsvi7e7a79d2020-12-15 16:48:16 -08002919 cb(eventLegacy, &clientInfoLegacy, &clientConfigLegacy, clientEffectsLegacy,
2920 &deviceConfigLegacy, effectsLegacy, patchHandleLegacy, sourceLegacy);
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -08002921 }
Ytai Ben-Tsvi7e7a79d2020-12-15 16:48:16 -08002922 return Status::ok();
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -08002923}
2924
Jean-Michel Trivi9a6b9ad2020-10-22 16:46:43 -07002925Status AudioSystem::AudioPolicyServiceClient::onRoutingUpdated() {
2926 routing_callback cb = NULL;
2927 {
Andy Hungacd955a2024-02-16 17:14:36 -08002928 std::lock_guard _l(AudioSystem::gMutex);
Jean-Michel Trivi9a6b9ad2020-10-22 16:46:43 -07002929 cb = gRoutingCallback;
2930 }
2931
2932 if (cb != NULL) {
2933 cb();
2934 }
2935 return Status::ok();
2936}
2937
Jean-Michel Trivi78f2b302022-04-15 18:18:41 +00002938Status AudioSystem::AudioPolicyServiceClient::onVolumeRangeInitRequest() {
2939 vol_range_init_req_callback cb = NULL;
2940 {
Andy Hungacd955a2024-02-16 17:14:36 -08002941 std::lock_guard _l(AudioSystem::gMutex);
Jean-Michel Trivi78f2b302022-04-15 18:18:41 +00002942 cb = gVolRangeInitReqCallback;
2943 }
2944
2945 if (cb != NULL) {
2946 cb();
2947 }
2948 return Status::ok();
2949}
2950
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002951void AudioSystem::AudioPolicyServiceClient::binderDied(const wp<IBinder>& who __unused) {
Glenn Kastend2d089f2014-11-05 11:48:12 -08002952 {
Andy Hungacd955a2024-02-16 17:14:36 -08002953 std::lock_guard _l(mMutex);
Andy Hungd211ce02024-02-16 15:34:07 -08002954 for (const auto& callback : mAudioPortCallbacks) {
2955 callback->onServiceDied();
Glenn Kastend2d089f2014-11-05 11:48:12 -08002956 }
Andy Hungd211ce02024-02-16 15:34:07 -08002957 for (const auto& callback : mAudioVolumeGroupCallbacks) {
2958 callback->onServiceDied();
François Gaffiecfe17322018-11-07 13:41:29 +01002959 }
Eric Laurentb52c1522014-05-20 11:27:36 -07002960 }
Mikhail Naganovec3d5792022-05-06 00:19:55 +00002961 AudioSystem::clearAudioPolicyService();
Eric Laurentc2f1f072009-07-17 12:17:14 -07002962
Steve Block5ff1dd52012-01-05 23:22:43 +00002963 ALOGW("AudioPolicyService server died!");
Eric Laurentc2f1f072009-07-17 12:17:14 -07002964}
2965
Ytai Ben-Tsvi7e7a79d2020-12-15 16:48:16 -08002966ConversionResult<record_client_info_t>
2967aidl2legacy_RecordClientInfo_record_client_info_t(const media::RecordClientInfo& aidl) {
2968 record_client_info_t legacy;
2969 legacy.riid = VALUE_OR_RETURN(aidl2legacy_int32_t_audio_unique_id_t(aidl.riid));
2970 legacy.uid = VALUE_OR_RETURN(aidl2legacy_int32_t_uid_t(aidl.uid));
2971 legacy.session = VALUE_OR_RETURN(aidl2legacy_int32_t_audio_session_t(aidl.session));
Mikhail Naganovddceecc2021-09-03 13:58:56 -07002972 legacy.source = VALUE_OR_RETURN(aidl2legacy_AudioSource_audio_source_t(aidl.source));
Ytai Ben-Tsvi7e7a79d2020-12-15 16:48:16 -08002973 legacy.port_id = VALUE_OR_RETURN(aidl2legacy_int32_t_audio_port_handle_t(aidl.portId));
2974 legacy.silenced = aidl.silenced;
2975 return legacy;
2976}
2977
2978ConversionResult<media::RecordClientInfo>
2979legacy2aidl_record_client_info_t_RecordClientInfo(const record_client_info_t& legacy) {
2980 media::RecordClientInfo aidl;
2981 aidl.riid = VALUE_OR_RETURN(legacy2aidl_audio_unique_id_t_int32_t(legacy.riid));
2982 aidl.uid = VALUE_OR_RETURN(legacy2aidl_uid_t_int32_t(legacy.uid));
2983 aidl.session = VALUE_OR_RETURN(legacy2aidl_audio_session_t_int32_t(legacy.session));
Mikhail Naganovddceecc2021-09-03 13:58:56 -07002984 aidl.source = VALUE_OR_RETURN(legacy2aidl_audio_source_t_AudioSource(legacy.source));
Ytai Ben-Tsvi7e7a79d2020-12-15 16:48:16 -08002985 aidl.portId = VALUE_OR_RETURN(legacy2aidl_audio_port_handle_t_int32_t(legacy.port_id));
2986 aidl.silenced = legacy.silenced;
2987 return aidl;
2988}
2989
Glenn Kasten40bc9062015-03-20 09:09:33 -07002990} // namespace android