blob: a5f323b6ef333fb2a66c24e055371a3d8875567c [file] [log] [blame]
Mathias Agopian65ab4712010-07-14 17:59:35 -07001/*
2 * Copyright (C) 2009 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 "AudioPolicyService"
18//#define LOG_NDEBUG 0
19
Glenn Kasten153b9fe2013-07-15 11:23:36 -070020#include "Configuration.h"
Mathias Agopian65ab4712010-07-14 17:59:35 -070021#undef __STRICT_ANSI__
22#define __STDINT_LIMITS
23#define __STDC_LIMIT_MACROS
24#include <stdint.h>
Mathias Agopian65ab4712010-07-14 17:59:35 -070025#include <sys/time.h>
Mikhail Naganov959e2d02019-03-28 11:08:19 -070026
27#include <audio_utils/clock.h>
Mathias Agopian65ab4712010-07-14 17:59:35 -070028#include <binder/IServiceManager.h>
29#include <utils/Log.h>
30#include <cutils/properties.h>
31#include <binder/IPCThreadState.h>
Svet Ganovf4ddfef2018-01-16 07:37:58 -080032#include <binder/PermissionController.h>
33#include <binder/IResultReceiver.h>
Mathias Agopian65ab4712010-07-14 17:59:35 -070034#include <utils/String16.h>
35#include <utils/threads.h>
36#include "AudioPolicyService.h"
Mathias Agopian65ab4712010-07-14 17:59:35 -070037#include <hardware_legacy/power.h>
Ytai Ben-Tsvi7e7a79d2020-12-15 16:48:16 -080038#include <media/AidlConversion.h>
Eric Laurent7c7f10b2011-06-17 21:29:58 -070039#include <media/AudioEffect.h>
Chih-Hung Hsiehc84d9d22014-11-14 13:33:34 -080040#include <media/AudioParameter.h>
Andy Hungab7ef302018-05-15 19:35:29 -070041#include <mediautils/ServiceUtilities.h>
Michael Groovercfd28302018-12-11 19:16:46 -080042#include <sensorprivacy/SensorPrivacyManager.h>
Mathias Agopian65ab4712010-07-14 17:59:35 -070043
Dima Zavin64760242011-05-11 14:15:23 -070044#include <system/audio.h>
Dima Zavin7394a4f2011-06-13 18:16:26 -070045#include <system/audio_policy.h>
Mikhail Naganov61a4fac2016-10-13 14:44:18 -070046
Mathias Agopian65ab4712010-07-14 17:59:35 -070047namespace android {
48
Glenn Kasten8dad0e32012-01-09 08:41:22 -080049static const char kDeadlockedString[] = "AudioPolicyService may be deadlocked\n";
50static const char kCmdDeadlockedString[] = "AudioPolicyService command thread may be deadlocked\n";
Mathias Agopian65ab4712010-07-14 17:59:35 -070051
Mikhail Naganov959e2d02019-03-28 11:08:19 -070052static const int kDumpLockTimeoutNs = 1 * NANOS_PER_SECOND;
Mathias Agopian65ab4712010-07-14 17:59:35 -070053
Eric Laurent0ede8922014-05-09 18:04:42 -070054static const nsecs_t kAudioCommandTimeoutNs = seconds(3); // 3 seconds
Christer Fletcher5fa8c4b2013-01-18 15:27:03 +010055
Svet Ganovf4ddfef2018-01-16 07:37:58 -080056static const String16 sManageAudioPolicyPermission("android.permission.MANAGE_AUDIO_POLICY");
Dima Zavinfce7a472011-04-19 22:30:36 -070057
Mathias Agopian65ab4712010-07-14 17:59:35 -070058// ----------------------------------------------------------------------------
59
60AudioPolicyService::AudioPolicyService()
Ytai Ben-Tsvi85093d52020-03-26 09:41:15 -070061 : BnAudioPolicyService(),
Ytai Ben-Tsvi85093d52020-03-26 09:41:15 -070062 mAudioPolicyManager(NULL),
63 mAudioPolicyClient(NULL),
64 mPhoneState(AUDIO_MODE_INVALID),
65 mCaptureStateNotifier(false) {
Eric Laurentf5ada6e2014-10-09 17:49:00 -070066}
67
68void AudioPolicyService::onFirstRef()
69{
Eric Laurent8b1e80b2014-10-07 09:08:47 -070070 {
71 Mutex::Autolock _l(mLock);
Eric Laurent93575202011-01-18 18:39:02 -080072
Eric Laurent8b1e80b2014-10-07 09:08:47 -070073 // start audio commands thread
74 mAudioCommandThread = new AudioCommandThread(String8("ApmAudio"), this);
75 // start output activity command thread
76 mOutputCommandThread = new AudioCommandThread(String8("ApmOutput"), this);
Eric Laurentdce54a12014-03-10 12:19:46 -070077
Eric Laurent8b1e80b2014-10-07 09:08:47 -070078 mAudioPolicyClient = new AudioPolicyClient(this);
79 mAudioPolicyManager = createAudioPolicyManager(mAudioPolicyClient);
Eric Laurent8b1e80b2014-10-07 09:08:47 -070080 }
bryant_liuba2b4392014-06-11 16:49:30 +080081 // load audio processing modules
Mikhail Naganov12b716c2020-04-30 22:37:43 +000082 sp<AudioPolicyEffects> audioPolicyEffects = new AudioPolicyEffects();
83 sp<UidPolicy> uidPolicy = new UidPolicy(this);
84 sp<SensorPrivacyPolicy> sensorPrivacyPolicy = new SensorPrivacyPolicy(this);
Eric Laurent8b1e80b2014-10-07 09:08:47 -070085 {
86 Mutex::Autolock _l(mLock);
87 mAudioPolicyEffects = audioPolicyEffects;
Mikhail Naganov12b716c2020-04-30 22:37:43 +000088 mUidPolicy = uidPolicy;
89 mSensorPrivacyPolicy = sensorPrivacyPolicy;
Eric Laurent8b1e80b2014-10-07 09:08:47 -070090 }
Mikhail Naganov12b716c2020-04-30 22:37:43 +000091 uidPolicy->registerSelf();
92 sensorPrivacyPolicy->registerSelf();
Mathias Agopian65ab4712010-07-14 17:59:35 -070093}
94
95AudioPolicyService::~AudioPolicyService()
96{
Mathias Agopian65ab4712010-07-14 17:59:35 -070097 mAudioCommandThread->exit();
Eric Laurent657ff612014-05-07 11:58:24 -070098 mOutputCommandThread->exit();
Eric Laurent7c7f10b2011-06-17 21:29:58 -070099
Eric Laurentf269b8e2014-06-09 20:01:29 -0700100 destroyAudioPolicyManager(mAudioPolicyManager);
Eric Laurentdce54a12014-03-10 12:19:46 -0700101 delete mAudioPolicyClient;
Eric Laurentb52c1522014-05-20 11:27:36 -0700102
103 mNotificationClients.clear();
bryant_liuba2b4392014-06-11 16:49:30 +0800104 mAudioPolicyEffects.clear();
Svet Ganovf4ddfef2018-01-16 07:37:58 -0800105
106 mUidPolicy->unregisterSelf();
Michael Groovercfd28302018-12-11 19:16:46 -0800107 mSensorPrivacyPolicy->unregisterSelf();
Mikhail Naganov12b716c2020-04-30 22:37:43 +0000108
109 mUidPolicy.clear();
Michael Groovercfd28302018-12-11 19:16:46 -0800110 mSensorPrivacyPolicy.clear();
Eric Laurentb52c1522014-05-20 11:27:36 -0700111}
112
113// A notification client is always registered by AudioSystem when the client process
114// connects to AudioPolicyService.
Ytai Ben-Tsvi7e7a79d2020-12-15 16:48:16 -0800115void AudioPolicyService::registerClient(const sp<media::IAudioPolicyServiceClient>& client)
Eric Laurentb52c1522014-05-20 11:27:36 -0700116{
Eric Laurent12590252015-08-21 18:40:20 -0700117 if (client == 0) {
118 ALOGW("%s got NULL client", __FUNCTION__);
119 return;
120 }
Eric Laurent0ebd5f92014-11-19 19:04:52 -0800121 Mutex::Autolock _l(mNotificationClientsLock);
Eric Laurentb52c1522014-05-20 11:27:36 -0700122
123 uid_t uid = IPCThreadState::self()->getCallingUid();
luochaojiang908c7d72018-06-21 14:58:04 +0800124 pid_t pid = IPCThreadState::self()->getCallingPid();
125 int64_t token = ((int64_t)uid<<32) | pid;
126
127 if (mNotificationClients.indexOfKey(token) < 0) {
Eric Laurentb52c1522014-05-20 11:27:36 -0700128 sp<NotificationClient> notificationClient = new NotificationClient(this,
129 client,
luochaojiang908c7d72018-06-21 14:58:04 +0800130 uid,
131 pid);
132 ALOGV("registerClient() client %p, uid %d pid %d", client.get(), uid, pid);
Eric Laurentb52c1522014-05-20 11:27:36 -0700133
luochaojiang908c7d72018-06-21 14:58:04 +0800134 mNotificationClients.add(token, notificationClient);
Eric Laurentb52c1522014-05-20 11:27:36 -0700135
Marco Nelissenf8880202014-11-14 07:58:25 -0800136 sp<IBinder> binder = IInterface::asBinder(client);
Eric Laurentb52c1522014-05-20 11:27:36 -0700137 binder->linkToDeath(notificationClient);
138 }
139}
140
Eric Laurente8726fe2015-06-26 09:39:24 -0700141void AudioPolicyService::setAudioPortCallbacksEnabled(bool enabled)
142{
143 Mutex::Autolock _l(mNotificationClientsLock);
144
145 uid_t uid = IPCThreadState::self()->getCallingUid();
luochaojiang908c7d72018-06-21 14:58:04 +0800146 pid_t pid = IPCThreadState::self()->getCallingPid();
147 int64_t token = ((int64_t)uid<<32) | pid;
148
149 if (mNotificationClients.indexOfKey(token) < 0) {
Eric Laurente8726fe2015-06-26 09:39:24 -0700150 return;
151 }
luochaojiang908c7d72018-06-21 14:58:04 +0800152 mNotificationClients.valueFor(token)->setAudioPortCallbacksEnabled(enabled);
Eric Laurente8726fe2015-06-26 09:39:24 -0700153}
154
François Gaffiecfe17322018-11-07 13:41:29 +0100155void AudioPolicyService::setAudioVolumeGroupCallbacksEnabled(bool enabled)
156{
157 Mutex::Autolock _l(mNotificationClientsLock);
158
159 uid_t uid = IPCThreadState::self()->getCallingUid();
160 pid_t pid = IPCThreadState::self()->getCallingPid();
161 int64_t token = ((int64_t)uid<<32) | pid;
162
163 if (mNotificationClients.indexOfKey(token) < 0) {
164 return;
165 }
166 mNotificationClients.valueFor(token)->setAudioVolumeGroupCallbacksEnabled(enabled);
167}
168
Eric Laurentb52c1522014-05-20 11:27:36 -0700169// removeNotificationClient() is called when the client process dies.
luochaojiang908c7d72018-06-21 14:58:04 +0800170void AudioPolicyService::removeNotificationClient(uid_t uid, pid_t pid)
Eric Laurentb52c1522014-05-20 11:27:36 -0700171{
Mikhail Naganov12b716c2020-04-30 22:37:43 +0000172 bool hasSameUid = false;
Eric Laurent0ebd5f92014-11-19 19:04:52 -0800173 {
174 Mutex::Autolock _l(mNotificationClientsLock);
luochaojiang908c7d72018-06-21 14:58:04 +0800175 int64_t token = ((int64_t)uid<<32) | pid;
176 mNotificationClients.removeItem(token);
luochaojiang908c7d72018-06-21 14:58:04 +0800177 for (size_t i = 0; i < mNotificationClients.size(); i++) {
178 if (mNotificationClients.valueAt(i)->uid() == uid) {
179 hasSameUid = true;
180 break;
181 }
182 }
Mikhail Naganov12b716c2020-04-30 22:37:43 +0000183 }
184 {
185 Mutex::Autolock _l(mLock);
luochaojiang908c7d72018-06-21 14:58:04 +0800186 if (mAudioPolicyManager && !hasSameUid) {
Eric Laurent10b71232018-04-13 18:14:44 -0700187 // called from binder death notification: no need to clear caller identity
Eric Laurent8c7e6da2015-04-21 17:37:00 -0700188 mAudioPolicyManager->releaseResourcesForUid(uid);
Eric Laurentb52c1522014-05-20 11:27:36 -0700189 }
Eric Laurent0ebd5f92014-11-19 19:04:52 -0800190 }
Eric Laurentb52c1522014-05-20 11:27:36 -0700191}
192
193void AudioPolicyService::onAudioPortListUpdate()
194{
195 mOutputCommandThread->updateAudioPortListCommand();
196}
197
198void AudioPolicyService::doOnAudioPortListUpdate()
199{
Eric Laurent0ebd5f92014-11-19 19:04:52 -0800200 Mutex::Autolock _l(mNotificationClientsLock);
Eric Laurentb52c1522014-05-20 11:27:36 -0700201 for (size_t i = 0; i < mNotificationClients.size(); i++) {
202 mNotificationClients.valueAt(i)->onAudioPortListUpdate();
203 }
204}
205
206void AudioPolicyService::onAudioPatchListUpdate()
207{
208 mOutputCommandThread->updateAudioPatchListCommand();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700209}
210
Eric Laurentb52c1522014-05-20 11:27:36 -0700211void AudioPolicyService::doOnAudioPatchListUpdate()
212{
Eric Laurent0ebd5f92014-11-19 19:04:52 -0800213 Mutex::Autolock _l(mNotificationClientsLock);
Eric Laurentb52c1522014-05-20 11:27:36 -0700214 for (size_t i = 0; i < mNotificationClients.size(); i++) {
215 mNotificationClients.valueAt(i)->onAudioPatchListUpdate();
216 }
217}
218
François Gaffiecfe17322018-11-07 13:41:29 +0100219void AudioPolicyService::onAudioVolumeGroupChanged(volume_group_t group, int flags)
220{
221 mOutputCommandThread->changeAudioVolumeGroupCommand(group, flags);
222}
223
224void AudioPolicyService::doOnAudioVolumeGroupChanged(volume_group_t group, int flags)
225{
226 Mutex::Autolock _l(mNotificationClientsLock);
227 for (size_t i = 0; i < mNotificationClients.size(); i++) {
228 mNotificationClients.valueAt(i)->onAudioVolumeGroupChanged(group, flags);
229 }
230}
231
Chih-Hung Hsiehe964d4e2016-08-09 14:31:32 -0700232void AudioPolicyService::onDynamicPolicyMixStateUpdate(const String8& regId, int32_t state)
Jean-Michel Trivide801052015-04-14 19:10:14 -0700233{
234 ALOGV("AudioPolicyService::onDynamicPolicyMixStateUpdate(%s, %d)",
235 regId.string(), state);
236 mOutputCommandThread->dynamicPolicyMixStateUpdateCommand(regId, state);
237}
238
Chih-Hung Hsiehe964d4e2016-08-09 14:31:32 -0700239void AudioPolicyService::doOnDynamicPolicyMixStateUpdate(const String8& regId, int32_t state)
Jean-Michel Trivide801052015-04-14 19:10:14 -0700240{
241 Mutex::Autolock _l(mNotificationClientsLock);
242 for (size_t i = 0; i < mNotificationClients.size(); i++) {
243 mNotificationClients.valueAt(i)->onDynamicPolicyMixStateUpdate(regId, state);
244 }
245}
246
Eric Laurenta9f86652018-11-28 17:23:11 -0800247void AudioPolicyService::onRecordingConfigurationUpdate(
248 int event,
249 const record_client_info_t *clientInfo,
250 const audio_config_base_t *clientConfig,
251 std::vector<effect_descriptor_t> clientEffects,
252 const audio_config_base_t *deviceConfig,
253 std::vector<effect_descriptor_t> effects,
254 audio_patch_handle_t patchHandle,
255 audio_source_t source)
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -0800256{
Jean-Michel Triviac4e4292016-12-22 11:39:31 -0800257 mOutputCommandThread->recordingConfigurationUpdateCommand(event, clientInfo,
Eric Laurenta9f86652018-11-28 17:23:11 -0800258 clientConfig, clientEffects, deviceConfig, effects, patchHandle, source);
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -0800259}
260
Eric Laurenta9f86652018-11-28 17:23:11 -0800261void AudioPolicyService::doOnRecordingConfigurationUpdate(
262 int event,
263 const record_client_info_t *clientInfo,
264 const audio_config_base_t *clientConfig,
265 std::vector<effect_descriptor_t> clientEffects,
266 const audio_config_base_t *deviceConfig,
267 std::vector<effect_descriptor_t> effects,
268 audio_patch_handle_t patchHandle,
269 audio_source_t source)
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -0800270{
271 Mutex::Autolock _l(mNotificationClientsLock);
272 for (size_t i = 0; i < mNotificationClients.size(); i++) {
Jean-Michel Triviac4e4292016-12-22 11:39:31 -0800273 mNotificationClients.valueAt(i)->onRecordingConfigurationUpdate(event, clientInfo,
Eric Laurenta9f86652018-11-28 17:23:11 -0800274 clientConfig, clientEffects, deviceConfig, effects, patchHandle, source);
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -0800275 }
276}
277
278status_t AudioPolicyService::clientCreateAudioPatch(const struct audio_patch *patch,
279 audio_patch_handle_t *handle,
280 int delayMs)
281{
282 return mAudioCommandThread->createAudioPatchCommand(patch, handle, delayMs);
283}
284
285status_t AudioPolicyService::clientReleaseAudioPatch(audio_patch_handle_t handle,
286 int delayMs)
287{
288 return mAudioCommandThread->releaseAudioPatchCommand(handle, delayMs);
289}
290
Eric Laurente1715a42014-05-20 11:30:42 -0700291status_t AudioPolicyService::clientSetAudioPortConfig(const struct audio_port_config *config,
292 int delayMs)
293{
294 return mAudioCommandThread->setAudioPortConfigCommand(config, delayMs);
295}
296
Ytai Ben-Tsvi7e7a79d2020-12-15 16:48:16 -0800297AudioPolicyService::NotificationClient::NotificationClient(
298 const sp<AudioPolicyService>& service,
299 const sp<media::IAudioPolicyServiceClient>& client,
300 uid_t uid,
301 pid_t pid)
luochaojiang908c7d72018-06-21 14:58:04 +0800302 : mService(service), mUid(uid), mPid(pid), mAudioPolicyServiceClient(client),
François Gaffiecfe17322018-11-07 13:41:29 +0100303 mAudioPortCallbacksEnabled(false), mAudioVolumeGroupCallbacksEnabled(false)
Eric Laurentb52c1522014-05-20 11:27:36 -0700304{
305}
306
307AudioPolicyService::NotificationClient::~NotificationClient()
308{
309}
310
311void AudioPolicyService::NotificationClient::binderDied(const wp<IBinder>& who __unused)
312{
313 sp<NotificationClient> keep(this);
314 sp<AudioPolicyService> service = mService.promote();
315 if (service != 0) {
luochaojiang908c7d72018-06-21 14:58:04 +0800316 service->removeNotificationClient(mUid, mPid);
Eric Laurentb52c1522014-05-20 11:27:36 -0700317 }
318}
319
320void AudioPolicyService::NotificationClient::onAudioPortListUpdate()
321{
Eric Laurente8726fe2015-06-26 09:39:24 -0700322 if (mAudioPolicyServiceClient != 0 && mAudioPortCallbacksEnabled) {
Eric Laurentb52c1522014-05-20 11:27:36 -0700323 mAudioPolicyServiceClient->onAudioPortListUpdate();
324 }
325}
326
327void AudioPolicyService::NotificationClient::onAudioPatchListUpdate()
328{
Eric Laurente8726fe2015-06-26 09:39:24 -0700329 if (mAudioPolicyServiceClient != 0 && mAudioPortCallbacksEnabled) {
Eric Laurentb52c1522014-05-20 11:27:36 -0700330 mAudioPolicyServiceClient->onAudioPatchListUpdate();
331 }
332}
Eric Laurent57dae992011-07-24 13:36:09 -0700333
François Gaffiecfe17322018-11-07 13:41:29 +0100334void AudioPolicyService::NotificationClient::onAudioVolumeGroupChanged(volume_group_t group,
335 int flags)
336{
337 if (mAudioPolicyServiceClient != 0 && mAudioVolumeGroupCallbacksEnabled) {
338 mAudioPolicyServiceClient->onAudioVolumeGroupChanged(group, flags);
339 }
340}
341
342
Jean-Michel Trivide801052015-04-14 19:10:14 -0700343void AudioPolicyService::NotificationClient::onDynamicPolicyMixStateUpdate(
Chih-Hung Hsiehe964d4e2016-08-09 14:31:32 -0700344 const String8& regId, int32_t state)
Jean-Michel Trivide801052015-04-14 19:10:14 -0700345{
Andy Hung4ef19fa2018-05-15 19:35:29 -0700346 if (mAudioPolicyServiceClient != 0 && isServiceUid(mUid)) {
Ytai Ben-Tsvi7e7a79d2020-12-15 16:48:16 -0800347 mAudioPolicyServiceClient->onDynamicPolicyMixStateUpdate(
348 legacy2aidl_String8_string(regId).value(), state);
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -0800349 }
350}
351
352void AudioPolicyService::NotificationClient::onRecordingConfigurationUpdate(
Eric Laurenta9f86652018-11-28 17:23:11 -0800353 int event,
354 const record_client_info_t *clientInfo,
355 const audio_config_base_t *clientConfig,
356 std::vector<effect_descriptor_t> clientEffects,
357 const audio_config_base_t *deviceConfig,
358 std::vector<effect_descriptor_t> effects,
359 audio_patch_handle_t patchHandle,
360 audio_source_t source)
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -0800361{
Andy Hung4ef19fa2018-05-15 19:35:29 -0700362 if (mAudioPolicyServiceClient != 0 && isServiceUid(mUid)) {
Ytai Ben-Tsvi7e7a79d2020-12-15 16:48:16 -0800363 status_t status = [&]() -> status_t {
364 int32_t eventAidl = VALUE_OR_RETURN_STATUS(convertIntegral<int32_t>(event));
365 media::RecordClientInfo clientInfoAidl = VALUE_OR_RETURN_STATUS(
366 legacy2aidl_record_client_info_t_RecordClientInfo(*clientInfo));
367 media::AudioConfigBase clientConfigAidl = VALUE_OR_RETURN_STATUS(
368 legacy2aidl_audio_config_base_t_AudioConfigBase(*clientConfig));
369 std::vector<media::EffectDescriptor> clientEffectsAidl = VALUE_OR_RETURN_STATUS(
370 convertContainer<std::vector<media::EffectDescriptor>>(
371 clientEffects,
372 legacy2aidl_effect_descriptor_t_EffectDescriptor));
373 media::AudioConfigBase deviceConfigAidl = VALUE_OR_RETURN_STATUS(
374 legacy2aidl_audio_config_base_t_AudioConfigBase(*deviceConfig));
375 std::vector<media::EffectDescriptor> effectsAidl = VALUE_OR_RETURN_STATUS(
376 convertContainer<std::vector<media::EffectDescriptor>>(
377 effects,
378 legacy2aidl_effect_descriptor_t_EffectDescriptor));
379 int32_t patchHandleAidl = VALUE_OR_RETURN_STATUS(
380 legacy2aidl_audio_patch_handle_t_int32_t(patchHandle));
381 media::AudioSourceType sourceAidl = VALUE_OR_RETURN_STATUS(
382 legacy2aidl_audio_source_t_AudioSourceType(source));
383 return aidl_utils::statusTFromBinderStatus(
384 mAudioPolicyServiceClient->onRecordingConfigurationUpdate(eventAidl,
385 clientInfoAidl,
386 clientConfigAidl,
387 clientEffectsAidl,
388 deviceConfigAidl,
389 effectsAidl,
390 patchHandleAidl,
391 sourceAidl));
392 }();
393 ALOGW_IF(status != OK, "onRecordingConfigurationUpdate() failed: %d", status);
Jean-Michel Trivide801052015-04-14 19:10:14 -0700394 }
395}
396
Eric Laurente8726fe2015-06-26 09:39:24 -0700397void AudioPolicyService::NotificationClient::setAudioPortCallbacksEnabled(bool enabled)
398{
399 mAudioPortCallbacksEnabled = enabled;
400}
401
François Gaffiecfe17322018-11-07 13:41:29 +0100402void AudioPolicyService::NotificationClient::setAudioVolumeGroupCallbacksEnabled(bool enabled)
403{
404 mAudioVolumeGroupCallbacksEnabled = enabled;
405}
Eric Laurente8726fe2015-06-26 09:39:24 -0700406
Mathias Agopian65ab4712010-07-14 17:59:35 -0700407void AudioPolicyService::binderDied(const wp<IBinder>& who) {
Glenn Kasten411e4472012-11-02 10:00:06 -0700408 ALOGW("binderDied() %p, calling pid %d", who.unsafe_get(),
Eric Laurentde070132010-07-13 04:45:46 -0700409 IPCThreadState::self()->getCallingPid());
Mathias Agopian65ab4712010-07-14 17:59:35 -0700410}
411
Mikhail Naganov12b716c2020-04-30 22:37:43 +0000412static bool dumpTryLock(Mutex& mutex) ACQUIRE(mutex) NO_THREAD_SAFETY_ANALYSIS
Mathias Agopian65ab4712010-07-14 17:59:35 -0700413{
Mikhail Naganov12b716c2020-04-30 22:37:43 +0000414 return mutex.timedLock(kDumpLockTimeoutNs) == NO_ERROR;
415}
416
417static void dumpReleaseLock(Mutex& mutex, bool locked) RELEASE(mutex) NO_THREAD_SAFETY_ANALYSIS
418{
419 if (locked) mutex.unlock();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700420}
421
422status_t AudioPolicyService::dumpInternals(int fd)
423{
424 const size_t SIZE = 256;
425 char buffer[SIZE];
426 String8 result;
427
Eric Laurentdce54a12014-03-10 12:19:46 -0700428 snprintf(buffer, SIZE, "AudioPolicyManager: %p\n", mAudioPolicyManager);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700429 result.append(buffer);
430 snprintf(buffer, SIZE, "Command Thread: %p\n", mAudioCommandThread.get());
431 result.append(buffer);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700432
Hayden Gomes524159d2019-12-23 14:41:47 -0800433 snprintf(buffer, SIZE, "Supported System Usages:\n");
434 result.append(buffer);
435 for (std::vector<audio_usage_t>::iterator it = mSupportedSystemUsages.begin();
436 it != mSupportedSystemUsages.end(); ++it) {
437 snprintf(buffer, SIZE, "\t%d\n", *it);
438 result.append(buffer);
439 }
440
Mathias Agopian65ab4712010-07-14 17:59:35 -0700441 write(fd, result.string(), result.size());
442 return NO_ERROR;
443}
444
Eric Laurente8c8b432018-10-17 10:08:02 -0700445void AudioPolicyService::updateUidStates()
Svet Ganovf4ddfef2018-01-16 07:37:58 -0800446{
Eric Laurente8c8b432018-10-17 10:08:02 -0700447 Mutex::Autolock _l(mLock);
448 updateUidStates_l();
449}
450
451void AudioPolicyService::updateUidStates_l()
452{
Eric Laurent4eb58f12018-12-07 16:41:02 -0800453// Go over all active clients and allow capture (does not force silence) in the
454// following cases:
Eric Laurent8ed73c12019-08-30 17:25:19 -0700455// The client is the assistant
Eric Laurent6ede98f2019-06-11 14:50:30 -0700456// AND an accessibility service is on TOP or a RTT call is active
Eric Laurent589171c2019-07-25 18:04:29 -0700457// AND the source is VOICE_RECOGNITION or HOTWORD
458// OR uses VOICE_RECOGNITION AND is on TOP
459// OR uses HOTWORD
Eric Laurent1ff16a72019-03-14 18:35:04 -0700460// AND there is no active privacy sensitive capture or call
461// OR client has CAPTURE_AUDIO_OUTPUT privileged permission
Eric Laurenta46bedb2018-12-07 18:01:26 -0800462// OR The client is an accessibility service
Eric Laurent589171c2019-07-25 18:04:29 -0700463// AND Is on TOP
464// AND the source is VOICE_RECOGNITION or HOTWORD
465// OR The assistant is not on TOP
466// AND there is no active privacy sensitive capture or call
467// OR client has CAPTURE_AUDIO_OUTPUT privileged permission
Eric Laurenta171e352019-05-07 13:04:45 -0700468// AND is on TOP
Eric Laurenta46bedb2018-12-07 18:01:26 -0800469// AND the source is VOICE_RECOGNITION or HOTWORD
Eric Laurent1ff16a72019-03-14 18:35:04 -0700470// OR the client source is virtual (remote submix, call audio TX or RX...)
Eric Laurent4e947da2019-10-17 15:24:06 -0700471// OR the client source is HOTWORD
472// AND is on TOP
473// OR all active clients are using HOTWORD source
474// AND no call is active
475// OR client has CAPTURE_AUDIO_OUTPUT privileged permission
Kohsuke Yatoha623a132020-03-24 20:10:26 -0700476// OR the client is the current InputMethodService
477// AND a RTT call is active AND the source is VOICE_RECOGNITION
Eric Laurenta171e352019-05-07 13:04:45 -0700478// OR Any client
Eric Laurenta46bedb2018-12-07 18:01:26 -0800479// AND The assistant is not on TOP
Eric Laurenta171e352019-05-07 13:04:45 -0700480// AND is on TOP or latest started
Eric Laurent1ff16a72019-03-14 18:35:04 -0700481// AND there is no active privacy sensitive capture or call
Eric Laurent589171c2019-07-25 18:04:29 -0700482// OR client has CAPTURE_AUDIO_OUTPUT privileged permission
Eric Laurent4eb58f12018-12-07 16:41:02 -0800483
Eric Laurent4e947da2019-10-17 15:24:06 -0700484
Eric Laurent4eb58f12018-12-07 16:41:02 -0800485 sp<AudioRecordClient> topActive;
486 sp<AudioRecordClient> latestActive;
Eric Laurentc21d5692020-02-25 10:24:36 -0800487 sp<AudioRecordClient> topSensitiveActive;
Eric Laurent4eb58f12018-12-07 16:41:02 -0800488 sp<AudioRecordClient> latestSensitiveActive;
Eric Laurent1ff16a72019-03-14 18:35:04 -0700489
Eric Laurenta46bedb2018-12-07 18:01:26 -0800490 nsecs_t topStartNs = 0;
491 nsecs_t latestStartNs = 0;
Eric Laurentc21d5692020-02-25 10:24:36 -0800492 nsecs_t topSensitiveStartNs = 0;
Eric Laurent4eb58f12018-12-07 16:41:02 -0800493 nsecs_t latestSensitiveStartNs = 0;
494 bool isA11yOnTop = mUidPolicy->isA11yOnTop();
495 bool isAssistantOnTop = false;
496 bool isSensitiveActive = false;
Eric Laurent1ff16a72019-03-14 18:35:04 -0700497 bool isInCall = mPhoneState == AUDIO_MODE_IN_CALL;
Eric Laurentc21d5692020-02-25 10:24:36 -0800498 bool isInCommunication = mPhoneState == AUDIO_MODE_IN_COMMUNICATION;
499 bool rttCallActive = (isInCall || isInCommunication)
Eric Laurent6ede98f2019-06-11 14:50:30 -0700500 && mUidPolicy->isRttEnabled();
Eric Laurent4e947da2019-10-17 15:24:06 -0700501 bool onlyHotwordActive = true;
Eric Laurent4eb58f12018-12-07 16:41:02 -0800502
Michael Groovercfd28302018-12-11 19:16:46 -0800503 // if Sensor Privacy is enabled then all recordings should be silenced.
504 if (mSensorPrivacyPolicy->isSensorPrivacyEnabled()) {
505 silenceAllRecordings_l();
506 return;
507 }
508
Eric Laurente8c8b432018-10-17 10:08:02 -0700509 for (size_t i =0; i < mAudioRecordClients.size(); i++) {
510 sp<AudioRecordClient> current = mAudioRecordClients[i];
Eric Laurent1ff16a72019-03-14 18:35:04 -0700511 if (!current->active) {
512 continue;
Eric Laurent4eb58f12018-12-07 16:41:02 -0800513 }
Eric Laurent1ff16a72019-03-14 18:35:04 -0700514
515 app_state_t appState = apmStatFromAmState(mUidPolicy->getUidState(current->uid));
516 // clients which app is in IDLE state are not eligible for top active or
517 // latest active
518 if (appState == APP_STATE_IDLE) {
519 continue;
520 }
521
Eric Laurent589171c2019-07-25 18:04:29 -0700522 bool isAccessibility = mUidPolicy->isA11yUid(current->uid);
Eric Laurent14a88632020-07-16 12:28:30 -0700523 // Clients capturing for Accessibility services or virtual sources are not considered
Eric Laurentc21d5692020-02-25 10:24:36 -0800524 // for top or latest active to avoid masking regular clients started before
Eric Laurent14a88632020-07-16 12:28:30 -0700525 if (!isAccessibility && !isVirtualSource(current->attributes.source)) {
Eric Laurentc21d5692020-02-25 10:24:36 -0800526 bool isAssistant = mUidPolicy->isAssistantUid(current->uid);
527 bool isPrivacySensitive =
528 (current->attributes.flags & AUDIO_FLAG_CAPTURE_PRIVATE) != 0;
529 if (appState == APP_STATE_TOP) {
530 if (isPrivacySensitive) {
531 if (current->startTimeNs > topSensitiveStartNs) {
532 topSensitiveActive = current;
533 topSensitiveStartNs = current->startTimeNs;
534 }
535 } else {
536 if (current->startTimeNs > topStartNs) {
537 topActive = current;
538 topStartNs = current->startTimeNs;
539 }
540 }
541 if (isAssistant) {
542 isAssistantOnTop = true;
543 }
Eric Laurenta46bedb2018-12-07 18:01:26 -0800544 }
Eric Laurentc21d5692020-02-25 10:24:36 -0800545 // Clients capturing for HOTWORD are not considered
546 // for latest active to avoid masking regular clients started before
547 if (!(current->attributes.source == AUDIO_SOURCE_HOTWORD
548 || ((isA11yOnTop || rttCallActive) && isAssistant))) {
549 if (isPrivacySensitive) {
550 if (current->startTimeNs > latestSensitiveStartNs) {
551 latestSensitiveActive = current;
552 latestSensitiveStartNs = current->startTimeNs;
553 }
554 isSensitiveActive = true;
555 } else {
556 if (current->startTimeNs > latestStartNs) {
557 latestActive = current;
558 latestStartNs = current->startTimeNs;
559 }
560 }
Eric Laurent4eb58f12018-12-07 16:41:02 -0800561 }
562 }
Eric Laurent4e947da2019-10-17 15:24:06 -0700563 if (current->attributes.source != AUDIO_SOURCE_HOTWORD) {
564 onlyHotwordActive = false;
565 }
Eric Laurent4eb58f12018-12-07 16:41:02 -0800566 }
567
Eric Laurent1ff16a72019-03-14 18:35:04 -0700568 // if no active client with UI on Top, consider latest active as top
569 if (topActive == nullptr) {
570 topActive = latestActive;
Eric Laurentc21d5692020-02-25 10:24:36 -0800571 topStartNs = latestStartNs;
572 }
573 if (topSensitiveActive == nullptr) {
574 topSensitiveActive = latestSensitiveActive;
575 topSensitiveStartNs = latestSensitiveStartNs;
576 }
577
578 // If both privacy sensitive and regular capture are active:
579 // if the regular capture is privileged
580 // allow concurrency
581 // else
582 // favor the privacy sensitive case
583 if (topActive != nullptr && topSensitiveActive != nullptr
Ricardo Correa57a37692020-03-23 17:27:25 -0700584 && !topActive->canCaptureOutput) {
Eric Laurentc21d5692020-02-25 10:24:36 -0800585 topActive = nullptr;
Eric Laurent4eb58f12018-12-07 16:41:02 -0800586 }
587
588 for (size_t i =0; i < mAudioRecordClients.size(); i++) {
589 sp<AudioRecordClient> current = mAudioRecordClients[i];
Eric Laurent1ff16a72019-03-14 18:35:04 -0700590 if (!current->active) {
591 continue;
592 }
593
Eric Laurent4eb58f12018-12-07 16:41:02 -0800594 audio_source_t source = current->attributes.source;
Eric Laurent1ff16a72019-03-14 18:35:04 -0700595 bool isTopOrLatestActive = topActive == nullptr ? false : current->uid == topActive->uid;
Eric Laurentc21d5692020-02-25 10:24:36 -0800596 bool isTopOrLatestSensitive = topSensitiveActive == nullptr ?
597 false : current->uid == topSensitiveActive->uid;
598
Mikhail Naganov12b716c2020-04-30 22:37:43 +0000599 auto canCaptureIfInCallOrCommunication = [&](const auto &recordClient) REQUIRES(mLock) {
Ricardo Correa57a37692020-03-23 17:27:25 -0700600 bool canCaptureCall = recordClient->canCaptureOutput;
Eric Laurentffabb492020-06-30 14:06:37 -0700601 return !(isInCall && !canCaptureCall);
602//TODO(b/160260850): restore restriction to mode owner once fix for misbehaving apps is merged
603// bool canCaptureCommunication = recordClient->canCaptureOutput
604// || recordClient->uid == mPhoneStateOwnerUid
605// || isServiceUid(mPhoneStateOwnerUid);
606// return !(isInCall && !canCaptureCall)
607// && !(isInCommunication && !canCaptureCommunication);
Eric Laurentc21d5692020-02-25 10:24:36 -0800608 };
Eric Laurent1ff16a72019-03-14 18:35:04 -0700609
610 // By default allow capture if:
611 // The assistant is not on TOP
Eric Laurenta171e352019-05-07 13:04:45 -0700612 // AND is on TOP or latest started
Eric Laurent1ff16a72019-03-14 18:35:04 -0700613 // AND there is no active privacy sensitive capture or call
614 // OR client has CAPTURE_AUDIO_OUTPUT privileged permission
615 bool allowCapture = !isAssistantOnTop
Eric Laurentc21d5692020-02-25 10:24:36 -0800616 && (isTopOrLatestActive || isTopOrLatestSensitive)
617 && !(isSensitiveActive
Ricardo Correa57a37692020-03-23 17:27:25 -0700618 && !(isTopOrLatestSensitive || current->canCaptureOutput))
Eric Laurentc21d5692020-02-25 10:24:36 -0800619 && canCaptureIfInCallOrCommunication(current);
Eric Laurent2dc962b2019-03-01 08:25:25 -0800620
621 if (isVirtualSource(source)) {
Eric Laurent1ff16a72019-03-14 18:35:04 -0700622 // Allow capture for virtual (remote submix, call audio TX or RX...) sources
623 allowCapture = true;
Eric Laurent2dc962b2019-03-01 08:25:25 -0800624 } else if (mUidPolicy->isAssistantUid(current->uid)) {
Eric Laurent1ff16a72019-03-14 18:35:04 -0700625 // For assistant allow capture if:
Eric Laurent6ede98f2019-06-11 14:50:30 -0700626 // An accessibility service is on TOP or a RTT call is active
Eric Laurent1ff16a72019-03-14 18:35:04 -0700627 // AND the source is VOICE_RECOGNITION or HOTWORD
Eric Laurenta171e352019-05-07 13:04:45 -0700628 // OR is on TOP AND uses VOICE_RECOGNITION
Eric Laurent1ff16a72019-03-14 18:35:04 -0700629 // OR uses HOTWORD
630 // AND there is no active privacy sensitive capture or call
631 // OR client has CAPTURE_AUDIO_OUTPUT privileged permission
Eric Laurent6ede98f2019-06-11 14:50:30 -0700632 if (isA11yOnTop || rttCallActive) {
Eric Laurent4eb58f12018-12-07 16:41:02 -0800633 if (source == AUDIO_SOURCE_HOTWORD || source == AUDIO_SOURCE_VOICE_RECOGNITION) {
Eric Laurent1ff16a72019-03-14 18:35:04 -0700634 allowCapture = true;
Eric Laurent4eb58f12018-12-07 16:41:02 -0800635 }
636 } else {
Eric Laurenta171e352019-05-07 13:04:45 -0700637 if (((isAssistantOnTop && source == AUDIO_SOURCE_VOICE_RECOGNITION) ||
Eric Laurentc21d5692020-02-25 10:24:36 -0800638 source == AUDIO_SOURCE_HOTWORD)
Ricardo Correa57a37692020-03-23 17:27:25 -0700639 && !(isSensitiveActive && !current->canCaptureOutput)
Eric Laurentc21d5692020-02-25 10:24:36 -0800640 && canCaptureIfInCallOrCommunication(current)) {
Eric Laurent1ff16a72019-03-14 18:35:04 -0700641 allowCapture = true;
Eric Laurent4eb58f12018-12-07 16:41:02 -0800642 }
643 }
644 } else if (mUidPolicy->isA11yUid(current->uid)) {
Eric Laurent1ff16a72019-03-14 18:35:04 -0700645 // For accessibility service allow capture if:
Eric Laurent47670c92019-08-28 16:59:05 -0700646 // The assistant is not on TOP
647 // AND there is no active privacy sensitive capture or call
Eric Laurent589171c2019-07-25 18:04:29 -0700648 // OR client has CAPTURE_AUDIO_OUTPUT privileged permission
Eric Laurent47670c92019-08-28 16:59:05 -0700649 // OR
650 // Is on TOP AND the source is VOICE_RECOGNITION or HOTWORD
651 if (!isAssistantOnTop
Ricardo Correa57a37692020-03-23 17:27:25 -0700652 && !(isSensitiveActive && !current->canCaptureOutput)
Eric Laurentc21d5692020-02-25 10:24:36 -0800653 && canCaptureIfInCallOrCommunication(current)) {
Eric Laurent47670c92019-08-28 16:59:05 -0700654 allowCapture = true;
655 }
Eric Laurent589171c2019-07-25 18:04:29 -0700656 if (isA11yOnTop) {
657 if (source == AUDIO_SOURCE_VOICE_RECOGNITION || source == AUDIO_SOURCE_HOTWORD) {
658 allowCapture = true;
659 }
Eric Laurent4eb58f12018-12-07 16:41:02 -0800660 }
Eric Laurent4e947da2019-10-17 15:24:06 -0700661 } else if (source == AUDIO_SOURCE_HOTWORD) {
662 // For HOTWORD source allow capture when not on TOP if:
663 // All active clients are using HOTWORD source
664 // AND no call is active
665 // OR client has CAPTURE_AUDIO_OUTPUT privileged permission
Eric Laurentc21d5692020-02-25 10:24:36 -0800666 if (onlyHotwordActive
667 && canCaptureIfInCallOrCommunication(current)) {
Eric Laurent4e947da2019-10-17 15:24:06 -0700668 allowCapture = true;
669 }
Kohsuke Yatoha623a132020-03-24 20:10:26 -0700670 } else if (mUidPolicy->isCurrentImeUid(current->uid)) {
671 // For current InputMethodService allow capture if:
672 // A RTT call is active AND the source is VOICE_RECOGNITION
673 if (rttCallActive && source == AUDIO_SOURCE_VOICE_RECOGNITION) {
674 allowCapture = true;
675 }
Eric Laurent4eb58f12018-12-07 16:41:02 -0800676 }
Eric Laurent5ada82e2019-08-29 17:53:54 -0700677 setAppState_l(current->portId,
Eric Laurent1ff16a72019-03-14 18:35:04 -0700678 allowCapture ? apmStatFromAmState(mUidPolicy->getUidState(current->uid)) :
679 APP_STATE_IDLE);
Eric Laurente8c8b432018-10-17 10:08:02 -0700680 }
681}
682
Michael Groovercfd28302018-12-11 19:16:46 -0800683void AudioPolicyService::silenceAllRecordings_l() {
684 for (size_t i = 0; i < mAudioRecordClients.size(); i++) {
685 sp<AudioRecordClient> current = mAudioRecordClients[i];
Eric Laurent1ff16a72019-03-14 18:35:04 -0700686 if (!isVirtualSource(current->attributes.source)) {
Eric Laurent5ada82e2019-08-29 17:53:54 -0700687 setAppState_l(current->portId, APP_STATE_IDLE);
Eric Laurent1ff16a72019-03-14 18:35:04 -0700688 }
Michael Groovercfd28302018-12-11 19:16:46 -0800689 }
690}
691
Eric Laurente8c8b432018-10-17 10:08:02 -0700692/* static */
693app_state_t AudioPolicyService::apmStatFromAmState(int amState) {
Eric Laurent1ff16a72019-03-14 18:35:04 -0700694
695 if (amState == ActivityManager::PROCESS_STATE_UNKNOWN) {
Eric Laurente8c8b432018-10-17 10:08:02 -0700696 return APP_STATE_IDLE;
Eric Laurent1ff16a72019-03-14 18:35:04 -0700697 } else if (amState <= ActivityManager::PROCESS_STATE_TOP) {
698 // include persistent services
699 return APP_STATE_TOP;
Eric Laurente8c8b432018-10-17 10:08:02 -0700700 }
701 return APP_STATE_FOREGROUND;
702}
703
Eric Laurent4eb58f12018-12-07 16:41:02 -0800704/* static */
Eric Laurent2dc962b2019-03-01 08:25:25 -0800705bool AudioPolicyService::isVirtualSource(audio_source_t source)
Eric Laurent4eb58f12018-12-07 16:41:02 -0800706{
707 switch (source) {
708 case AUDIO_SOURCE_VOICE_UPLINK:
709 case AUDIO_SOURCE_VOICE_DOWNLINK:
710 case AUDIO_SOURCE_VOICE_CALL:
Eric Laurent2dc962b2019-03-01 08:25:25 -0800711 case AUDIO_SOURCE_REMOTE_SUBMIX:
712 case AUDIO_SOURCE_FM_TUNER:
Eric Laurent68eb2122020-04-30 17:40:57 -0700713 case AUDIO_SOURCE_ECHO_REFERENCE:
Eric Laurent4eb58f12018-12-07 16:41:02 -0800714 return true;
715 default:
716 break;
717 }
718 return false;
719}
720
Eric Laurent5ada82e2019-08-29 17:53:54 -0700721void AudioPolicyService::setAppState_l(audio_port_handle_t portId, app_state_t state)
Eric Laurente8c8b432018-10-17 10:08:02 -0700722{
723 AutoCallerClear acc;
724
725 if (mAudioPolicyManager) {
Eric Laurent5ada82e2019-08-29 17:53:54 -0700726 mAudioPolicyManager->setAppState(portId, state);
Mikhail Naganoveae73eb2018-04-03 16:57:36 -0700727 }
728 sp<IAudioFlinger> af = AudioSystem::get_audio_flinger();
729 if (af) {
Eric Laurentf32108e2018-10-04 17:22:04 -0700730 bool silenced = state == APP_STATE_IDLE;
Eric Laurent5ada82e2019-08-29 17:53:54 -0700731 af->setRecordSilenced(portId, silenced);
Mikhail Naganoveae73eb2018-04-03 16:57:36 -0700732 }
Svet Ganovf4ddfef2018-01-16 07:37:58 -0800733}
734
Glenn Kasten0f11b512014-01-31 16:18:54 -0800735status_t AudioPolicyService::dump(int fd, const Vector<String16>& args __unused)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700736{
Glenn Kasten44deb052012-02-05 18:09:08 -0800737 if (!dumpAllowed()) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700738 dumpPermissionDenial(fd);
739 } else {
Mikhail Naganov12b716c2020-04-30 22:37:43 +0000740 const bool locked = dumpTryLock(mLock);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700741 if (!locked) {
742 String8 result(kDeadlockedString);
743 write(fd, result.string(), result.size());
744 }
745
746 dumpInternals(fd);
Glenn Kasten9d1f02d2012-02-08 17:47:58 -0800747 if (mAudioCommandThread != 0) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700748 mAudioCommandThread->dump(fd);
749 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700750
Eric Laurentdce54a12014-03-10 12:19:46 -0700751 if (mAudioPolicyManager) {
752 mAudioPolicyManager->dump(fd);
753 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700754
Kevin Rocard8be94972019-02-22 13:26:25 -0800755 mPackageManager.dump(fd);
756
Mikhail Naganov12b716c2020-04-30 22:37:43 +0000757 dumpReleaseLock(mLock, locked);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700758 }
759 return NO_ERROR;
760}
761
762status_t AudioPolicyService::dumpPermissionDenial(int fd)
763{
764 const size_t SIZE = 256;
765 char buffer[SIZE];
766 String8 result;
767 snprintf(buffer, SIZE, "Permission Denial: "
768 "can't dump AudioPolicyService from pid=%d, uid=%d\n",
769 IPCThreadState::self()->getCallingPid(),
770 IPCThreadState::self()->getCallingUid());
771 result.append(buffer);
772 write(fd, result.string(), result.size());
773 return NO_ERROR;
774}
775
776status_t AudioPolicyService::onTransact(
Svet Ganovf4ddfef2018-01-16 07:37:58 -0800777 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags) {
778 switch (code) {
779 case SHELL_COMMAND_TRANSACTION: {
780 int in = data.readFileDescriptor();
781 int out = data.readFileDescriptor();
782 int err = data.readFileDescriptor();
783 int argc = data.readInt32();
784 Vector<String16> args;
785 for (int i = 0; i < argc && data.dataAvail() > 0; i++) {
786 args.add(data.readString16());
787 }
788 sp<IBinder> unusedCallback;
789 sp<IResultReceiver> resultReceiver;
790 status_t status;
791 if ((status = data.readNullableStrongBinder(&unusedCallback)) != NO_ERROR) {
792 return status;
793 }
794 if ((status = data.readNullableStrongBinder(&resultReceiver)) != NO_ERROR) {
795 return status;
796 }
797 status = shellCommand(in, out, err, args);
798 if (resultReceiver != nullptr) {
799 resultReceiver->send(status);
800 }
801 return NO_ERROR;
802 }
803 }
804
Mathias Agopian65ab4712010-07-14 17:59:35 -0700805 return BnAudioPolicyService::onTransact(code, data, reply, flags);
806}
807
Svet Ganovf4ddfef2018-01-16 07:37:58 -0800808// ------------------- Shell command implementation -------------------
809
810// NOTE: This is a remote API - make sure all args are validated
811status_t AudioPolicyService::shellCommand(int in, int out, int err, Vector<String16>& args) {
812 if (!checkCallingPermission(sManageAudioPolicyPermission, nullptr, nullptr)) {
813 return PERMISSION_DENIED;
814 }
815 if (in == BAD_TYPE || out == BAD_TYPE || err == BAD_TYPE) {
816 return BAD_VALUE;
817 }
jovanakbe066e12019-09-02 11:54:39 -0700818 if (args.size() >= 3 && args[0] == String16("set-uid-state")) {
Svet Ganovf4ddfef2018-01-16 07:37:58 -0800819 return handleSetUidState(args, err);
jovanakbe066e12019-09-02 11:54:39 -0700820 } else if (args.size() >= 2 && args[0] == String16("reset-uid-state")) {
Svet Ganovf4ddfef2018-01-16 07:37:58 -0800821 return handleResetUidState(args, err);
jovanakbe066e12019-09-02 11:54:39 -0700822 } else if (args.size() >= 2 && args[0] == String16("get-uid-state")) {
Svet Ganovf4ddfef2018-01-16 07:37:58 -0800823 return handleGetUidState(args, out, err);
824 } else if (args.size() == 1 && args[0] == String16("help")) {
825 printHelp(out);
826 return NO_ERROR;
827 }
828 printHelp(err);
829 return BAD_VALUE;
830}
831
jovanakbe066e12019-09-02 11:54:39 -0700832static status_t getUidForPackage(String16 packageName, int userId, /*inout*/uid_t& uid, int err) {
833 if (userId < 0) {
834 ALOGE("Invalid user: %d", userId);
835 dprintf(err, "Invalid user: %d\n", userId);
Svet Ganovf4ddfef2018-01-16 07:37:58 -0800836 return BAD_VALUE;
837 }
jovanakbe066e12019-09-02 11:54:39 -0700838
839 PermissionController pc;
840 uid = pc.getPackageUid(packageName, 0);
841 if (uid <= 0) {
842 ALOGE("Unknown package: '%s'", String8(packageName).string());
843 dprintf(err, "Unknown package: '%s'\n", String8(packageName).string());
844 return BAD_VALUE;
845 }
846
847 uid = multiuser_get_uid(userId, uid);
848 return NO_ERROR;
849}
850
851status_t AudioPolicyService::handleSetUidState(Vector<String16>& args, int err) {
852 // Valid arg.size() is 3 or 5, args.size() is 5 with --user option.
853 if (!(args.size() == 3 || args.size() == 5)) {
854 printHelp(err);
855 return BAD_VALUE;
856 }
857
Svet Ganovf4ddfef2018-01-16 07:37:58 -0800858 bool active = false;
859 if (args[2] == String16("active")) {
860 active = true;
861 } else if ((args[2] != String16("idle"))) {
862 ALOGE("Expected active or idle but got: '%s'", String8(args[2]).string());
863 return BAD_VALUE;
864 }
jovanakbe066e12019-09-02 11:54:39 -0700865
866 int userId = 0;
867 if (args.size() >= 5 && args[3] == String16("--user")) {
868 userId = atoi(String8(args[4]));
869 }
870
871 uid_t uid;
872 if (getUidForPackage(args[1], userId, uid, err) == BAD_VALUE) {
873 return BAD_VALUE;
874 }
875
Mikhail Naganov12b716c2020-04-30 22:37:43 +0000876 sp<UidPolicy> uidPolicy;
877 {
878 Mutex::Autolock _l(mLock);
879 uidPolicy = mUidPolicy;
880 }
881 if (uidPolicy) {
882 uidPolicy->addOverrideUid(uid, active);
883 return NO_ERROR;
884 }
885 return NO_INIT;
Svet Ganovf4ddfef2018-01-16 07:37:58 -0800886}
887
888status_t AudioPolicyService::handleResetUidState(Vector<String16>& args, int err) {
jovanakbe066e12019-09-02 11:54:39 -0700889 // Valid arg.size() is 2 or 4, args.size() is 4 with --user option.
890 if (!(args.size() == 2 || args.size() == 4)) {
891 printHelp(err);
Svet Ganovf4ddfef2018-01-16 07:37:58 -0800892 return BAD_VALUE;
893 }
jovanakbe066e12019-09-02 11:54:39 -0700894
895 int userId = 0;
896 if (args.size() >= 4 && args[2] == String16("--user")) {
897 userId = atoi(String8(args[3]));
898 }
899
900 uid_t uid;
901 if (getUidForPackage(args[1], userId, uid, err) == BAD_VALUE) {
902 return BAD_VALUE;
903 }
904
Mikhail Naganov12b716c2020-04-30 22:37:43 +0000905 sp<UidPolicy> uidPolicy;
906 {
907 Mutex::Autolock _l(mLock);
908 uidPolicy = mUidPolicy;
909 }
910 if (uidPolicy) {
911 uidPolicy->removeOverrideUid(uid);
912 return NO_ERROR;
913 }
914 return NO_INIT;
Svet Ganovf4ddfef2018-01-16 07:37:58 -0800915}
916
917status_t AudioPolicyService::handleGetUidState(Vector<String16>& args, int out, int err) {
jovanakbe066e12019-09-02 11:54:39 -0700918 // Valid arg.size() is 2 or 4, args.size() is 4 with --user option.
919 if (!(args.size() == 2 || args.size() == 4)) {
920 printHelp(err);
Svet Ganovf4ddfef2018-01-16 07:37:58 -0800921 return BAD_VALUE;
922 }
jovanakbe066e12019-09-02 11:54:39 -0700923
924 int userId = 0;
925 if (args.size() >= 4 && args[2] == String16("--user")) {
926 userId = atoi(String8(args[3]));
927 }
928
929 uid_t uid;
930 if (getUidForPackage(args[1], userId, uid, err) == BAD_VALUE) {
931 return BAD_VALUE;
932 }
933
Mikhail Naganov12b716c2020-04-30 22:37:43 +0000934 sp<UidPolicy> uidPolicy;
935 {
936 Mutex::Autolock _l(mLock);
937 uidPolicy = mUidPolicy;
Svet Ganovf4ddfef2018-01-16 07:37:58 -0800938 }
Mikhail Naganov12b716c2020-04-30 22:37:43 +0000939 if (uidPolicy) {
940 return dprintf(out, uidPolicy->isUidActive(uid) ? "active\n" : "idle\n");
941 }
942 return NO_INIT;
Svet Ganovf4ddfef2018-01-16 07:37:58 -0800943}
944
945status_t AudioPolicyService::printHelp(int out) {
946 return dprintf(out, "Audio policy service commands:\n"
jovanakbe066e12019-09-02 11:54:39 -0700947 " get-uid-state <PACKAGE> [--user USER_ID] gets the uid state\n"
948 " set-uid-state <PACKAGE> <active|idle> [--user USER_ID] overrides the uid state\n"
949 " reset-uid-state <PACKAGE> [--user USER_ID] clears the uid state override\n"
Svet Ganovf4ddfef2018-01-16 07:37:58 -0800950 " help print this message\n");
951}
952
953// ----------- AudioPolicyService::UidPolicy implementation ----------
954
955void AudioPolicyService::UidPolicy::registerSelf() {
Steven Moreland2f348142019-07-02 15:59:07 -0700956 status_t res = mAm.linkToDeath(this);
957 mAm.registerUidObserver(this, ActivityManager::UID_OBSERVER_GONE
Svet Ganovf4ddfef2018-01-16 07:37:58 -0800958 | ActivityManager::UID_OBSERVER_IDLE
Eric Laurente8c8b432018-10-17 10:08:02 -0700959 | ActivityManager::UID_OBSERVER_ACTIVE
960 | ActivityManager::UID_OBSERVER_PROCSTATE,
Svet Ganovf4ddfef2018-01-16 07:37:58 -0800961 ActivityManager::PROCESS_STATE_UNKNOWN,
962 String16("audioserver"));
Mikhail Naganoveae73eb2018-04-03 16:57:36 -0700963 if (!res) {
964 Mutex::Autolock _l(mLock);
965 mObserverRegistered = true;
966 } else {
967 ALOGE("UidPolicy::registerSelf linkToDeath failed: %d", res);
Eric Laurent4eb58f12018-12-07 16:41:02 -0800968
Steven Moreland2f348142019-07-02 15:59:07 -0700969 mAm.unregisterUidObserver(this);
Mikhail Naganoveae73eb2018-04-03 16:57:36 -0700970 }
Svet Ganovf4ddfef2018-01-16 07:37:58 -0800971}
972
973void AudioPolicyService::UidPolicy::unregisterSelf() {
Steven Moreland2f348142019-07-02 15:59:07 -0700974 mAm.unlinkToDeath(this);
975 mAm.unregisterUidObserver(this);
Mikhail Naganoveae73eb2018-04-03 16:57:36 -0700976 Mutex::Autolock _l(mLock);
977 mObserverRegistered = false;
Svet Ganovf4ddfef2018-01-16 07:37:58 -0800978}
979
Mikhail Naganoveae73eb2018-04-03 16:57:36 -0700980void AudioPolicyService::UidPolicy::binderDied(__unused const wp<IBinder> &who) {
981 Mutex::Autolock _l(mLock);
982 mCachedUids.clear();
983 mObserverRegistered = false;
Svet Ganovf4ddfef2018-01-16 07:37:58 -0800984}
985
Eric Laurente8c8b432018-10-17 10:08:02 -0700986void AudioPolicyService::UidPolicy::checkRegistered() {
Mikhail Naganoveae73eb2018-04-03 16:57:36 -0700987 bool needToReregister = false;
988 {
989 Mutex::Autolock _l(mLock);
990 needToReregister = !mObserverRegistered;
Svet Ganovf4ddfef2018-01-16 07:37:58 -0800991 }
Mikhail Naganoveae73eb2018-04-03 16:57:36 -0700992 if (needToReregister) {
993 // Looks like ActivityManager has died previously, attempt to re-register.
994 registerSelf();
995 }
Eric Laurente8c8b432018-10-17 10:08:02 -0700996}
997
998bool AudioPolicyService::UidPolicy::isUidActive(uid_t uid) {
999 if (isServiceUid(uid)) return true;
1000 checkRegistered();
Mikhail Naganoveae73eb2018-04-03 16:57:36 -07001001 {
1002 Mutex::Autolock _l(mLock);
1003 auto overrideIter = mOverrideUids.find(uid);
1004 if (overrideIter != mOverrideUids.end()) {
Eric Laurente8c8b432018-10-17 10:08:02 -07001005 return overrideIter->second.first;
Mikhail Naganoveae73eb2018-04-03 16:57:36 -07001006 }
1007 // In an absense of the ActivityManager, assume everything to be active.
1008 if (!mObserverRegistered) return true;
1009 auto cacheIter = mCachedUids.find(uid);
Mikhail Naganoveba668a2018-04-05 08:13:15 -07001010 if (cacheIter != mCachedUids.end()) {
Eric Laurente8c8b432018-10-17 10:08:02 -07001011 return cacheIter->second.first;
Mikhail Naganoveae73eb2018-04-03 16:57:36 -07001012 }
1013 }
1014 ActivityManager am;
Hui Yu12c7ec72020-05-04 17:40:52 +00001015 bool active = am.isUidActive(uid, String16("audioserver"));
Mikhail Naganoveae73eb2018-04-03 16:57:36 -07001016 {
1017 Mutex::Autolock _l(mLock);
Eric Laurente8c8b432018-10-17 10:08:02 -07001018 mCachedUids.insert(std::pair<uid_t,
1019 std::pair<bool, int>>(uid, std::pair<bool, int>(active,
1020 ActivityManager::PROCESS_STATE_UNKNOWN)));
Mikhail Naganoveae73eb2018-04-03 16:57:36 -07001021 }
1022 return active;
Svet Ganovf4ddfef2018-01-16 07:37:58 -08001023}
1024
Eric Laurente8c8b432018-10-17 10:08:02 -07001025int AudioPolicyService::UidPolicy::getUidState(uid_t uid) {
1026 if (isServiceUid(uid)) {
1027 return ActivityManager::PROCESS_STATE_TOP;
1028 }
1029 checkRegistered();
1030 {
1031 Mutex::Autolock _l(mLock);
1032 auto overrideIter = mOverrideUids.find(uid);
1033 if (overrideIter != mOverrideUids.end()) {
1034 if (overrideIter->second.first) {
1035 if (overrideIter->second.second != ActivityManager::PROCESS_STATE_UNKNOWN) {
1036 return overrideIter->second.second;
1037 } else {
1038 auto cacheIter = mCachedUids.find(uid);
1039 if (cacheIter != mCachedUids.end()) {
1040 return cacheIter->second.second;
1041 }
1042 }
1043 }
1044 return ActivityManager::PROCESS_STATE_UNKNOWN;
1045 }
1046 // In an absense of the ActivityManager, assume everything to be active.
1047 if (!mObserverRegistered) {
1048 return ActivityManager::PROCESS_STATE_TOP;
1049 }
1050 auto cacheIter = mCachedUids.find(uid);
1051 if (cacheIter != mCachedUids.end()) {
1052 if (cacheIter->second.first) {
1053 return cacheIter->second.second;
1054 } else {
1055 return ActivityManager::PROCESS_STATE_UNKNOWN;
1056 }
1057 }
1058 }
1059 ActivityManager am;
Hui Yu12c7ec72020-05-04 17:40:52 +00001060 bool active = am.isUidActive(uid, String16("audioserver"));
Eric Laurente8c8b432018-10-17 10:08:02 -07001061 int state = ActivityManager::PROCESS_STATE_UNKNOWN;
1062 if (active) {
1063 state = am.getUidProcessState(uid, String16("audioserver"));
1064 }
1065 {
1066 Mutex::Autolock _l(mLock);
1067 mCachedUids.insert(std::pair<uid_t,
1068 std::pair<bool, int>>(uid, std::pair<bool, int>(active, state)));
1069 }
Eric Laurent4eb58f12018-12-07 16:41:02 -08001070
Eric Laurente8c8b432018-10-17 10:08:02 -07001071 return state;
1072}
1073
Mikhail Naganoveae73eb2018-04-03 16:57:36 -07001074void AudioPolicyService::UidPolicy::onUidActive(uid_t uid) {
Eric Laurente8c8b432018-10-17 10:08:02 -07001075 updateUid(&mCachedUids, uid, true, ActivityManager::PROCESS_STATE_UNKNOWN, true);
Mikhail Naganoveae73eb2018-04-03 16:57:36 -07001076}
1077
1078void AudioPolicyService::UidPolicy::onUidGone(uid_t uid, __unused bool disabled) {
Eric Laurente8c8b432018-10-17 10:08:02 -07001079 updateUid(&mCachedUids, uid, false, ActivityManager::PROCESS_STATE_UNKNOWN, false);
Mikhail Naganoveae73eb2018-04-03 16:57:36 -07001080}
1081
1082void AudioPolicyService::UidPolicy::onUidIdle(uid_t uid, __unused bool disabled) {
Eric Laurente8c8b432018-10-17 10:08:02 -07001083 updateUid(&mCachedUids, uid, false, ActivityManager::PROCESS_STATE_UNKNOWN, true);
Mikhail Naganoveae73eb2018-04-03 16:57:36 -07001084}
1085
Eric Laurente8c8b432018-10-17 10:08:02 -07001086void AudioPolicyService::UidPolicy::onUidStateChanged(uid_t uid,
1087 int32_t procState,
Hui Yu13ad0eb2019-09-09 10:27:07 -07001088 int64_t procStateSeq __unused,
1089 int32_t capability __unused) {
Eric Laurente8c8b432018-10-17 10:08:02 -07001090 if (procState != ActivityManager::PROCESS_STATE_UNKNOWN) {
1091 updateUid(&mCachedUids, uid, true, procState, true);
Mikhail Naganoveae73eb2018-04-03 16:57:36 -07001092 }
1093}
1094
1095void AudioPolicyService::UidPolicy::updateOverrideUid(uid_t uid, bool active, bool insert) {
Eric Laurente8c8b432018-10-17 10:08:02 -07001096 updateUid(&mOverrideUids, uid, active, ActivityManager::PROCESS_STATE_UNKNOWN, insert);
1097}
1098
1099void AudioPolicyService::UidPolicy::notifyService() {
1100 sp<AudioPolicyService> service = mService.promote();
1101 if (service != nullptr) {
1102 service->updateUidStates();
Mikhail Naganoveae73eb2018-04-03 16:57:36 -07001103 }
1104}
1105
Eric Laurente8c8b432018-10-17 10:08:02 -07001106void AudioPolicyService::UidPolicy::updateUid(std::unordered_map<uid_t,
1107 std::pair<bool, int>> *uids,
1108 uid_t uid,
1109 bool active,
1110 int state,
1111 bool insert) {
1112 if (isServiceUid(uid)) {
1113 return;
1114 }
1115 bool wasActive = isUidActive(uid);
1116 int previousState = getUidState(uid);
Mikhail Naganoveae73eb2018-04-03 16:57:36 -07001117 {
1118 Mutex::Autolock _l(mLock);
Eric Laurente8c8b432018-10-17 10:08:02 -07001119 updateUidLocked(uids, uid, active, state, insert);
Mikhail Naganoveae73eb2018-04-03 16:57:36 -07001120 }
Eric Laurente8c8b432018-10-17 10:08:02 -07001121 if (wasActive != isUidActive(uid) || state != previousState) {
1122 notifyService();
1123 }
Mikhail Naganoveae73eb2018-04-03 16:57:36 -07001124}
1125
Eric Laurente8c8b432018-10-17 10:08:02 -07001126void AudioPolicyService::UidPolicy::updateUidLocked(std::unordered_map<uid_t,
1127 std::pair<bool, int>> *uids,
1128 uid_t uid,
1129 bool active,
1130 int state,
1131 bool insert) {
Mikhail Naganoveae73eb2018-04-03 16:57:36 -07001132 auto it = uids->find(uid);
1133 if (it != uids->end()) {
Mikhail Naganoveae73eb2018-04-03 16:57:36 -07001134 if (insert) {
Eric Laurente8c8b432018-10-17 10:08:02 -07001135 if (state == ActivityManager::PROCESS_STATE_UNKNOWN) {
1136 it->second.first = active;
1137 }
1138 if (it->second.first) {
1139 it->second.second = state;
1140 } else {
1141 it->second.second = ActivityManager::PROCESS_STATE_UNKNOWN;
1142 }
Mikhail Naganoveae73eb2018-04-03 16:57:36 -07001143 } else {
1144 uids->erase(it);
1145 }
Eric Laurente8c8b432018-10-17 10:08:02 -07001146 } else if (insert && (state == ActivityManager::PROCESS_STATE_UNKNOWN)) {
1147 uids->insert(std::pair<uid_t, std::pair<bool, int>>(uid,
1148 std::pair<bool, int>(active, state)));
Mikhail Naganoveae73eb2018-04-03 16:57:36 -07001149 }
Svet Ganovf4ddfef2018-01-16 07:37:58 -08001150}
Mathias Agopian65ab4712010-07-14 17:59:35 -07001151
Eric Laurent4eb58f12018-12-07 16:41:02 -08001152bool AudioPolicyService::UidPolicy::isA11yOnTop() {
1153 for (const auto &uid : mCachedUids) {
Eric Laurent47670c92019-08-28 16:59:05 -07001154 if (!isA11yUid(uid.first)) {
Eric Laurent4eb58f12018-12-07 16:41:02 -08001155 continue;
1156 }
Amith Yamasanibcbb3002019-01-23 13:53:33 -08001157 if (uid.second.second >= ActivityManager::PROCESS_STATE_TOP
1158 && uid.second.second <= ActivityManager::PROCESS_STATE_BOUND_FOREGROUND_SERVICE) {
Eric Laurent4eb58f12018-12-07 16:41:02 -08001159 return true;
1160 }
1161 }
1162 return false;
1163}
1164
Eric Laurentb78763e2018-10-17 10:08:02 -07001165bool AudioPolicyService::UidPolicy::isA11yUid(uid_t uid)
1166{
1167 std::vector<uid_t>::iterator it = find(mA11yUids.begin(), mA11yUids.end(), uid);
1168 return it != mA11yUids.end();
1169}
1170
Michael Groovercfd28302018-12-11 19:16:46 -08001171// ----------- AudioPolicyService::SensorPrivacyService implementation ----------
1172void AudioPolicyService::SensorPrivacyPolicy::registerSelf() {
1173 SensorPrivacyManager spm;
1174 mSensorPrivacyEnabled = spm.isSensorPrivacyEnabled();
1175 spm.addSensorPrivacyListener(this);
1176}
1177
1178void AudioPolicyService::SensorPrivacyPolicy::unregisterSelf() {
1179 SensorPrivacyManager spm;
1180 spm.removeSensorPrivacyListener(this);
1181}
1182
1183bool AudioPolicyService::SensorPrivacyPolicy::isSensorPrivacyEnabled() {
1184 return mSensorPrivacyEnabled;
1185}
1186
1187binder::Status AudioPolicyService::SensorPrivacyPolicy::onSensorPrivacyChanged(bool enabled) {
1188 mSensorPrivacyEnabled = enabled;
1189 sp<AudioPolicyService> service = mService.promote();
1190 if (service != nullptr) {
1191 service->updateUidStates();
1192 }
1193 return binder::Status::ok();
1194}
1195
Mathias Agopian65ab4712010-07-14 17:59:35 -07001196// ----------- AudioPolicyService::AudioCommandThread implementation ----------
1197
Eric Laurentbfb1b832013-01-07 09:53:42 -08001198AudioPolicyService::AudioCommandThread::AudioCommandThread(String8 name,
1199 const wp<AudioPolicyService>& service)
1200 : Thread(false), mName(name), mService(service)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001201{
Mathias Agopian65ab4712010-07-14 17:59:35 -07001202}
1203
1204
1205AudioPolicyService::AudioCommandThread::~AudioCommandThread()
1206{
Eric Laurentbfb1b832013-01-07 09:53:42 -08001207 if (!mAudioCommands.isEmpty()) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07001208 release_wake_lock(mName.string());
1209 }
1210 mAudioCommands.clear();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001211}
1212
1213void AudioPolicyService::AudioCommandThread::onFirstRef()
1214{
Eric Laurentbfb1b832013-01-07 09:53:42 -08001215 run(mName.string(), ANDROID_PRIORITY_AUDIO);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001216}
1217
1218bool AudioPolicyService::AudioCommandThread::threadLoop()
1219{
Eric Laurentd7eda8d2016-02-02 17:18:39 -08001220 nsecs_t waitTime = -1;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001221
1222 mLock.lock();
1223 while (!exitPending())
1224 {
Eric Laurent59a89232014-06-08 14:14:17 -07001225 sp<AudioPolicyService> svc;
1226 while (!mAudioCommands.isEmpty() && !exitPending()) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07001227 nsecs_t curTime = systemTime();
1228 // commands are sorted by increasing time stamp: execute them from index 0 and up
1229 if (mAudioCommands[0]->mTime <= curTime) {
Eric Laurent0ede8922014-05-09 18:04:42 -07001230 sp<AudioCommand> command = mAudioCommands[0];
Mathias Agopian65ab4712010-07-14 17:59:35 -07001231 mAudioCommands.removeAt(0);
Eric Laurent0ede8922014-05-09 18:04:42 -07001232 mLastCommand = command;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001233
1234 switch (command->mCommand) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07001235 case SET_VOLUME: {
Eric Laurent0ede8922014-05-09 18:04:42 -07001236 VolumeData *data = (VolumeData *)command->mParam.get();
Steve Block3856b092011-10-20 11:56:00 +01001237 ALOGV("AudioCommandThread() processing set volume stream %d, \
Eric Laurentde070132010-07-13 04:45:46 -07001238 volume %f, output %d", data->mStream, data->mVolume, data->mIO);
Andy Hungfe726a62018-09-27 15:17:25 -07001239 mLock.unlock();
Eric Laurentde070132010-07-13 04:45:46 -07001240 command->mStatus = AudioSystem::setStreamVolume(data->mStream,
1241 data->mVolume,
1242 data->mIO);
Andy Hungfe726a62018-09-27 15:17:25 -07001243 mLock.lock();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001244 }break;
1245 case SET_PARAMETERS: {
Eric Laurent0ede8922014-05-09 18:04:42 -07001246 ParametersData *data = (ParametersData *)command->mParam.get();
Glenn Kastene53b9ea2012-03-12 16:29:55 -07001247 ALOGV("AudioCommandThread() processing set parameters string %s, io %d",
1248 data->mKeyValuePairs.string(), data->mIO);
Andy Hungfe726a62018-09-27 15:17:25 -07001249 mLock.unlock();
Glenn Kastene53b9ea2012-03-12 16:29:55 -07001250 command->mStatus = AudioSystem::setParameters(data->mIO, data->mKeyValuePairs);
Andy Hungfe726a62018-09-27 15:17:25 -07001251 mLock.lock();
Glenn Kastene53b9ea2012-03-12 16:29:55 -07001252 }break;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001253 case SET_VOICE_VOLUME: {
Eric Laurent0ede8922014-05-09 18:04:42 -07001254 VoiceVolumeData *data = (VoiceVolumeData *)command->mParam.get();
Steve Block3856b092011-10-20 11:56:00 +01001255 ALOGV("AudioCommandThread() processing set voice volume volume %f",
Eric Laurentde070132010-07-13 04:45:46 -07001256 data->mVolume);
Andy Hungfe726a62018-09-27 15:17:25 -07001257 mLock.unlock();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001258 command->mStatus = AudioSystem::setVoiceVolume(data->mVolume);
Andy Hungfe726a62018-09-27 15:17:25 -07001259 mLock.lock();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001260 }break;
Eric Laurentbfb1b832013-01-07 09:53:42 -08001261 case STOP_OUTPUT: {
Eric Laurent0ede8922014-05-09 18:04:42 -07001262 StopOutputData *data = (StopOutputData *)command->mParam.get();
Eric Laurentd7fe0862018-07-14 16:48:01 -07001263 ALOGV("AudioCommandThread() processing stop output portId %d",
1264 data->mPortId);
Eric Laurent59a89232014-06-08 14:14:17 -07001265 svc = mService.promote();
Eric Laurentbfb1b832013-01-07 09:53:42 -08001266 if (svc == 0) {
1267 break;
1268 }
1269 mLock.unlock();
Eric Laurentd7fe0862018-07-14 16:48:01 -07001270 svc->doStopOutput(data->mPortId);
Eric Laurentbfb1b832013-01-07 09:53:42 -08001271 mLock.lock();
Eric Laurentbfb1b832013-01-07 09:53:42 -08001272 }break;
1273 case RELEASE_OUTPUT: {
Eric Laurent0ede8922014-05-09 18:04:42 -07001274 ReleaseOutputData *data = (ReleaseOutputData *)command->mParam.get();
Eric Laurentd7fe0862018-07-14 16:48:01 -07001275 ALOGV("AudioCommandThread() processing release output portId %d",
1276 data->mPortId);
Eric Laurent59a89232014-06-08 14:14:17 -07001277 svc = mService.promote();
Eric Laurentbfb1b832013-01-07 09:53:42 -08001278 if (svc == 0) {
1279 break;
1280 }
1281 mLock.unlock();
Eric Laurentd7fe0862018-07-14 16:48:01 -07001282 svc->doReleaseOutput(data->mPortId);
Eric Laurentbfb1b832013-01-07 09:53:42 -08001283 mLock.lock();
Eric Laurentbfb1b832013-01-07 09:53:42 -08001284 }break;
Eric Laurent951f4552014-05-20 10:48:17 -07001285 case CREATE_AUDIO_PATCH: {
1286 CreateAudioPatchData *data = (CreateAudioPatchData *)command->mParam.get();
1287 ALOGV("AudioCommandThread() processing create audio patch");
1288 sp<IAudioFlinger> af = AudioSystem::get_audio_flinger();
1289 if (af == 0) {
1290 command->mStatus = PERMISSION_DENIED;
1291 } else {
Andy Hungfe726a62018-09-27 15:17:25 -07001292 mLock.unlock();
Eric Laurent951f4552014-05-20 10:48:17 -07001293 command->mStatus = af->createAudioPatch(&data->mPatch, &data->mHandle);
Andy Hungfe726a62018-09-27 15:17:25 -07001294 mLock.lock();
Eric Laurent951f4552014-05-20 10:48:17 -07001295 }
1296 } break;
1297 case RELEASE_AUDIO_PATCH: {
1298 ReleaseAudioPatchData *data = (ReleaseAudioPatchData *)command->mParam.get();
1299 ALOGV("AudioCommandThread() processing release audio patch");
1300 sp<IAudioFlinger> af = AudioSystem::get_audio_flinger();
1301 if (af == 0) {
1302 command->mStatus = PERMISSION_DENIED;
1303 } else {
Andy Hungfe726a62018-09-27 15:17:25 -07001304 mLock.unlock();
Eric Laurent951f4552014-05-20 10:48:17 -07001305 command->mStatus = af->releaseAudioPatch(data->mHandle);
Andy Hungfe726a62018-09-27 15:17:25 -07001306 mLock.lock();
Eric Laurent951f4552014-05-20 10:48:17 -07001307 }
1308 } break;
Eric Laurentb52c1522014-05-20 11:27:36 -07001309 case UPDATE_AUDIOPORT_LIST: {
1310 ALOGV("AudioCommandThread() processing update audio port list");
Eric Laurent59a89232014-06-08 14:14:17 -07001311 svc = mService.promote();
Eric Laurentb52c1522014-05-20 11:27:36 -07001312 if (svc == 0) {
1313 break;
1314 }
1315 mLock.unlock();
1316 svc->doOnAudioPortListUpdate();
1317 mLock.lock();
1318 }break;
1319 case UPDATE_AUDIOPATCH_LIST: {
1320 ALOGV("AudioCommandThread() processing update audio patch list");
Eric Laurent59a89232014-06-08 14:14:17 -07001321 svc = mService.promote();
Eric Laurentb52c1522014-05-20 11:27:36 -07001322 if (svc == 0) {
1323 break;
1324 }
1325 mLock.unlock();
1326 svc->doOnAudioPatchListUpdate();
1327 mLock.lock();
1328 }break;
François Gaffiecfe17322018-11-07 13:41:29 +01001329 case CHANGED_AUDIOVOLUMEGROUP: {
1330 AudioVolumeGroupData *data =
1331 static_cast<AudioVolumeGroupData *>(command->mParam.get());
1332 ALOGV("AudioCommandThread() processing update audio volume group");
1333 svc = mService.promote();
1334 if (svc == 0) {
1335 break;
1336 }
1337 mLock.unlock();
1338 svc->doOnAudioVolumeGroupChanged(data->mGroup, data->mFlags);
1339 mLock.lock();
1340 }break;
Eric Laurente1715a42014-05-20 11:30:42 -07001341 case SET_AUDIOPORT_CONFIG: {
1342 SetAudioPortConfigData *data = (SetAudioPortConfigData *)command->mParam.get();
1343 ALOGV("AudioCommandThread() processing set port config");
1344 sp<IAudioFlinger> af = AudioSystem::get_audio_flinger();
1345 if (af == 0) {
1346 command->mStatus = PERMISSION_DENIED;
1347 } else {
Andy Hungfe726a62018-09-27 15:17:25 -07001348 mLock.unlock();
Eric Laurente1715a42014-05-20 11:30:42 -07001349 command->mStatus = af->setAudioPortConfig(&data->mConfig);
Andy Hungfe726a62018-09-27 15:17:25 -07001350 mLock.lock();
Eric Laurente1715a42014-05-20 11:30:42 -07001351 }
1352 } break;
Jean-Michel Trivide801052015-04-14 19:10:14 -07001353 case DYN_POLICY_MIX_STATE_UPDATE: {
1354 DynPolicyMixStateUpdateData *data =
1355 (DynPolicyMixStateUpdateData *)command->mParam.get();
Jean-Michel Trivide801052015-04-14 19:10:14 -07001356 ALOGV("AudioCommandThread() processing dyn policy mix state update %s %d",
1357 data->mRegId.string(), data->mState);
1358 svc = mService.promote();
1359 if (svc == 0) {
1360 break;
1361 }
1362 mLock.unlock();
1363 svc->doOnDynamicPolicyMixStateUpdate(data->mRegId, data->mState);
1364 mLock.lock();
1365 } break;
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -08001366 case RECORDING_CONFIGURATION_UPDATE: {
1367 RecordingConfigurationUpdateData *data =
1368 (RecordingConfigurationUpdateData *)command->mParam.get();
1369 ALOGV("AudioCommandThread() processing recording configuration update");
1370 svc = mService.promote();
1371 if (svc == 0) {
1372 break;
1373 }
1374 mLock.unlock();
Jean-Michel Triviac4e4292016-12-22 11:39:31 -08001375 svc->doOnRecordingConfigurationUpdate(data->mEvent, &data->mClientInfo,
Eric Laurenta9f86652018-11-28 17:23:11 -08001376 &data->mClientConfig, data->mClientEffects,
1377 &data->mDeviceConfig, data->mEffects,
1378 data->mPatchHandle, data->mSource);
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -08001379 mLock.lock();
1380 } break;
Eric Laurentb20cf7d2019-04-05 19:37:34 -07001381 case SET_EFFECT_SUSPENDED: {
1382 SetEffectSuspendedData *data = (SetEffectSuspendedData *)command->mParam.get();
1383 ALOGV("AudioCommandThread() processing set effect suspended");
1384 sp<IAudioFlinger> af = AudioSystem::get_audio_flinger();
1385 if (af != 0) {
1386 mLock.unlock();
1387 af->setEffectSuspended(data->mEffectId, data->mSessionId, data->mSuspended);
1388 mLock.lock();
1389 }
1390 } break;
Mikhail Naganov88b30d22020-03-09 19:43:13 +00001391 case AUDIO_MODULES_UPDATE: {
1392 ALOGV("AudioCommandThread() processing audio modules update");
1393 svc = mService.promote();
1394 if (svc == 0) {
1395 break;
1396 }
1397 mLock.unlock();
1398 svc->doOnNewAudioModulesAvailable();
1399 mLock.lock();
1400 } break;
Eric Laurentb20cf7d2019-04-05 19:37:34 -07001401
Mathias Agopian65ab4712010-07-14 17:59:35 -07001402 default:
Steve Block5ff1dd52012-01-05 23:22:43 +00001403 ALOGW("AudioCommandThread() unknown command %d", command->mCommand);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001404 }
Eric Laurent0ede8922014-05-09 18:04:42 -07001405 {
1406 Mutex::Autolock _l(command->mLock);
1407 if (command->mWaitStatus) {
1408 command->mWaitStatus = false;
1409 command->mCond.signal();
1410 }
1411 }
Eric Laurentd7eda8d2016-02-02 17:18:39 -08001412 waitTime = -1;
Zach Janga754b4f2015-10-27 01:29:34 +00001413 // release mLock before releasing strong reference on the service as
1414 // AudioPolicyService destructor calls AudioCommandThread::exit() which
1415 // acquires mLock.
1416 mLock.unlock();
1417 svc.clear();
1418 mLock.lock();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001419 } else {
1420 waitTime = mAudioCommands[0]->mTime - curTime;
1421 break;
1422 }
1423 }
Zach Janga754b4f2015-10-27 01:29:34 +00001424
1425 // release delayed commands wake lock if the queue is empty
1426 if (mAudioCommands.isEmpty()) {
Ricardo Garcia05f2fdc2014-07-24 15:48:24 -07001427 release_wake_lock(mName.string());
Zach Janga754b4f2015-10-27 01:29:34 +00001428 }
1429
1430 // At this stage we have either an empty command queue or the first command in the queue
1431 // has a finite delay. So unless we are exiting it is safe to wait.
1432 if (!exitPending()) {
Eric Laurent59a89232014-06-08 14:14:17 -07001433 ALOGV("AudioCommandThread() going to sleep");
Eric Laurentd7eda8d2016-02-02 17:18:39 -08001434 if (waitTime == -1) {
1435 mWaitWorkCV.wait(mLock);
1436 } else {
1437 mWaitWorkCV.waitRelative(mLock, waitTime);
1438 }
Eric Laurent59a89232014-06-08 14:14:17 -07001439 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001440 }
Ricardo Garcia05f2fdc2014-07-24 15:48:24 -07001441 // release delayed commands wake lock before quitting
1442 if (!mAudioCommands.isEmpty()) {
1443 release_wake_lock(mName.string());
1444 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001445 mLock.unlock();
1446 return false;
1447}
1448
1449status_t AudioPolicyService::AudioCommandThread::dump(int fd)
1450{
1451 const size_t SIZE = 256;
1452 char buffer[SIZE];
1453 String8 result;
1454
1455 snprintf(buffer, SIZE, "AudioCommandThread %p Dump\n", this);
1456 result.append(buffer);
1457 write(fd, result.string(), result.size());
1458
Mikhail Naganov12b716c2020-04-30 22:37:43 +00001459 const bool locked = dumpTryLock(mLock);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001460 if (!locked) {
1461 String8 result2(kCmdDeadlockedString);
1462 write(fd, result2.string(), result2.size());
1463 }
1464
1465 snprintf(buffer, SIZE, "- Commands:\n");
1466 result = String8(buffer);
1467 result.append(" Command Time Wait pParam\n");
Glenn Kasten8d6a2442012-02-08 14:04:28 -08001468 for (size_t i = 0; i < mAudioCommands.size(); i++) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07001469 mAudioCommands[i]->dump(buffer, SIZE);
1470 result.append(buffer);
1471 }
1472 result.append(" Last Command\n");
Eric Laurent0ede8922014-05-09 18:04:42 -07001473 if (mLastCommand != 0) {
1474 mLastCommand->dump(buffer, SIZE);
1475 result.append(buffer);
1476 } else {
1477 result.append(" none\n");
1478 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001479
1480 write(fd, result.string(), result.size());
1481
Mikhail Naganov12b716c2020-04-30 22:37:43 +00001482 dumpReleaseLock(mLock, locked);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001483
1484 return NO_ERROR;
1485}
1486
Glenn Kastenfff6d712012-01-12 16:38:12 -08001487status_t AudioPolicyService::AudioCommandThread::volumeCommand(audio_stream_type_t stream,
Eric Laurentde070132010-07-13 04:45:46 -07001488 float volume,
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001489 audio_io_handle_t output,
Eric Laurentde070132010-07-13 04:45:46 -07001490 int delayMs)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001491{
Eric Laurent0ede8922014-05-09 18:04:42 -07001492 sp<AudioCommand> command = new AudioCommand();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001493 command->mCommand = SET_VOLUME;
Eric Laurent0ede8922014-05-09 18:04:42 -07001494 sp<VolumeData> data = new VolumeData();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001495 data->mStream = stream;
1496 data->mVolume = volume;
1497 data->mIO = output;
1498 command->mParam = data;
Eric Laurent0ede8922014-05-09 18:04:42 -07001499 command->mWaitStatus = true;
Steve Block3856b092011-10-20 11:56:00 +01001500 ALOGV("AudioCommandThread() adding set volume stream %d, volume %f, output %d",
Eric Laurentde070132010-07-13 04:45:46 -07001501 stream, volume, output);
Eric Laurent0ede8922014-05-09 18:04:42 -07001502 return sendCommand(command, delayMs);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001503}
1504
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001505status_t AudioPolicyService::AudioCommandThread::parametersCommand(audio_io_handle_t ioHandle,
Dima Zavinfce7a472011-04-19 22:30:36 -07001506 const char *keyValuePairs,
Eric Laurentde070132010-07-13 04:45:46 -07001507 int delayMs)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001508{
Eric Laurent0ede8922014-05-09 18:04:42 -07001509 sp<AudioCommand> command = new AudioCommand();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001510 command->mCommand = SET_PARAMETERS;
Eric Laurent0ede8922014-05-09 18:04:42 -07001511 sp<ParametersData> data = new ParametersData();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001512 data->mIO = ioHandle;
Dima Zavinfce7a472011-04-19 22:30:36 -07001513 data->mKeyValuePairs = String8(keyValuePairs);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001514 command->mParam = data;
Eric Laurent0ede8922014-05-09 18:04:42 -07001515 command->mWaitStatus = true;
Steve Block3856b092011-10-20 11:56:00 +01001516 ALOGV("AudioCommandThread() adding set parameter string %s, io %d ,delay %d",
Dima Zavinfce7a472011-04-19 22:30:36 -07001517 keyValuePairs, ioHandle, delayMs);
Eric Laurent0ede8922014-05-09 18:04:42 -07001518 return sendCommand(command, delayMs);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001519}
1520
1521status_t AudioPolicyService::AudioCommandThread::voiceVolumeCommand(float volume, int delayMs)
1522{
Eric Laurent0ede8922014-05-09 18:04:42 -07001523 sp<AudioCommand> command = new AudioCommand();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001524 command->mCommand = SET_VOICE_VOLUME;
Eric Laurent0ede8922014-05-09 18:04:42 -07001525 sp<VoiceVolumeData> data = new VoiceVolumeData();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001526 data->mVolume = volume;
1527 command->mParam = data;
Eric Laurent0ede8922014-05-09 18:04:42 -07001528 command->mWaitStatus = true;
Steve Block3856b092011-10-20 11:56:00 +01001529 ALOGV("AudioCommandThread() adding set voice volume volume %f", volume);
Eric Laurent0ede8922014-05-09 18:04:42 -07001530 return sendCommand(command, delayMs);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001531}
1532
Eric Laurentb20cf7d2019-04-05 19:37:34 -07001533void AudioPolicyService::AudioCommandThread::setEffectSuspendedCommand(int effectId,
1534 audio_session_t sessionId,
1535 bool suspended)
1536{
1537 sp<AudioCommand> command = new AudioCommand();
1538 command->mCommand = SET_EFFECT_SUSPENDED;
1539 sp<SetEffectSuspendedData> data = new SetEffectSuspendedData();
1540 data->mEffectId = effectId;
1541 data->mSessionId = sessionId;
1542 data->mSuspended = suspended;
1543 command->mParam = data;
1544 ALOGV("AudioCommandThread() adding set suspended effectId %d sessionId %d suspended %d",
1545 effectId, sessionId, suspended);
1546 sendCommand(command);
1547}
1548
1549
Eric Laurentd7fe0862018-07-14 16:48:01 -07001550void AudioPolicyService::AudioCommandThread::stopOutputCommand(audio_port_handle_t portId)
Eric Laurentbfb1b832013-01-07 09:53:42 -08001551{
Eric Laurent0ede8922014-05-09 18:04:42 -07001552 sp<AudioCommand> command = new AudioCommand();
Eric Laurentbfb1b832013-01-07 09:53:42 -08001553 command->mCommand = STOP_OUTPUT;
Eric Laurent0ede8922014-05-09 18:04:42 -07001554 sp<StopOutputData> data = new StopOutputData();
Eric Laurentd7fe0862018-07-14 16:48:01 -07001555 data->mPortId = portId;
Jesper Tragardh48412dc2014-03-24 14:12:43 +01001556 command->mParam = data;
Eric Laurentd7fe0862018-07-14 16:48:01 -07001557 ALOGV("AudioCommandThread() adding stop output portId %d", portId);
Eric Laurent0ede8922014-05-09 18:04:42 -07001558 sendCommand(command);
Eric Laurentbfb1b832013-01-07 09:53:42 -08001559}
1560
Eric Laurentd7fe0862018-07-14 16:48:01 -07001561void AudioPolicyService::AudioCommandThread::releaseOutputCommand(audio_port_handle_t portId)
Eric Laurentbfb1b832013-01-07 09:53:42 -08001562{
Eric Laurent0ede8922014-05-09 18:04:42 -07001563 sp<AudioCommand> command = new AudioCommand();
Eric Laurentbfb1b832013-01-07 09:53:42 -08001564 command->mCommand = RELEASE_OUTPUT;
Eric Laurent0ede8922014-05-09 18:04:42 -07001565 sp<ReleaseOutputData> data = new ReleaseOutputData();
Eric Laurentd7fe0862018-07-14 16:48:01 -07001566 data->mPortId = portId;
Jesper Tragardh48412dc2014-03-24 14:12:43 +01001567 command->mParam = data;
Eric Laurentd7fe0862018-07-14 16:48:01 -07001568 ALOGV("AudioCommandThread() adding release output portId %d", portId);
Eric Laurent0ede8922014-05-09 18:04:42 -07001569 sendCommand(command);
1570}
1571
Eric Laurent951f4552014-05-20 10:48:17 -07001572status_t AudioPolicyService::AudioCommandThread::createAudioPatchCommand(
1573 const struct audio_patch *patch,
1574 audio_patch_handle_t *handle,
1575 int delayMs)
1576{
1577 status_t status = NO_ERROR;
1578
1579 sp<AudioCommand> command = new AudioCommand();
1580 command->mCommand = CREATE_AUDIO_PATCH;
1581 CreateAudioPatchData *data = new CreateAudioPatchData();
1582 data->mPatch = *patch;
1583 data->mHandle = *handle;
1584 command->mParam = data;
1585 command->mWaitStatus = true;
1586 ALOGV("AudioCommandThread() adding create patch delay %d", delayMs);
1587 status = sendCommand(command, delayMs);
1588 if (status == NO_ERROR) {
1589 *handle = data->mHandle;
1590 }
1591 return status;
1592}
1593
1594status_t AudioPolicyService::AudioCommandThread::releaseAudioPatchCommand(audio_patch_handle_t handle,
1595 int delayMs)
1596{
1597 sp<AudioCommand> command = new AudioCommand();
1598 command->mCommand = RELEASE_AUDIO_PATCH;
1599 ReleaseAudioPatchData *data = new ReleaseAudioPatchData();
1600 data->mHandle = handle;
1601 command->mParam = data;
1602 command->mWaitStatus = true;
1603 ALOGV("AudioCommandThread() adding release patch delay %d", delayMs);
1604 return sendCommand(command, delayMs);
1605}
1606
Eric Laurentb52c1522014-05-20 11:27:36 -07001607void AudioPolicyService::AudioCommandThread::updateAudioPortListCommand()
1608{
1609 sp<AudioCommand> command = new AudioCommand();
1610 command->mCommand = UPDATE_AUDIOPORT_LIST;
1611 ALOGV("AudioCommandThread() adding update audio port list");
1612 sendCommand(command);
1613}
1614
1615void AudioPolicyService::AudioCommandThread::updateAudioPatchListCommand()
1616{
1617 sp<AudioCommand>command = new AudioCommand();
1618 command->mCommand = UPDATE_AUDIOPATCH_LIST;
1619 ALOGV("AudioCommandThread() adding update audio patch list");
1620 sendCommand(command);
1621}
1622
François Gaffiecfe17322018-11-07 13:41:29 +01001623void AudioPolicyService::AudioCommandThread::changeAudioVolumeGroupCommand(volume_group_t group,
1624 int flags)
1625{
1626 sp<AudioCommand>command = new AudioCommand();
1627 command->mCommand = CHANGED_AUDIOVOLUMEGROUP;
1628 AudioVolumeGroupData *data= new AudioVolumeGroupData();
1629 data->mGroup = group;
1630 data->mFlags = flags;
1631 command->mParam = data;
1632 ALOGV("AudioCommandThread() adding audio volume group changed");
1633 sendCommand(command);
1634}
1635
Eric Laurente1715a42014-05-20 11:30:42 -07001636status_t AudioPolicyService::AudioCommandThread::setAudioPortConfigCommand(
1637 const struct audio_port_config *config, int delayMs)
1638{
1639 sp<AudioCommand> command = new AudioCommand();
1640 command->mCommand = SET_AUDIOPORT_CONFIG;
1641 SetAudioPortConfigData *data = new SetAudioPortConfigData();
1642 data->mConfig = *config;
1643 command->mParam = data;
1644 command->mWaitStatus = true;
1645 ALOGV("AudioCommandThread() adding set port config delay %d", delayMs);
1646 return sendCommand(command, delayMs);
1647}
1648
Jean-Michel Trivide801052015-04-14 19:10:14 -07001649void AudioPolicyService::AudioCommandThread::dynamicPolicyMixStateUpdateCommand(
Chih-Hung Hsiehe964d4e2016-08-09 14:31:32 -07001650 const String8& regId, int32_t state)
Jean-Michel Trivide801052015-04-14 19:10:14 -07001651{
1652 sp<AudioCommand> command = new AudioCommand();
1653 command->mCommand = DYN_POLICY_MIX_STATE_UPDATE;
1654 DynPolicyMixStateUpdateData *data = new DynPolicyMixStateUpdateData();
1655 data->mRegId = regId;
1656 data->mState = state;
1657 command->mParam = data;
1658 ALOGV("AudioCommandThread() sending dynamic policy mix (id=%s) state update to %d",
1659 regId.string(), state);
1660 sendCommand(command);
1661}
1662
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -08001663void AudioPolicyService::AudioCommandThread::recordingConfigurationUpdateCommand(
Eric Laurenta9f86652018-11-28 17:23:11 -08001664 int event,
1665 const record_client_info_t *clientInfo,
1666 const audio_config_base_t *clientConfig,
1667 std::vector<effect_descriptor_t> clientEffects,
1668 const audio_config_base_t *deviceConfig,
1669 std::vector<effect_descriptor_t> effects,
1670 audio_patch_handle_t patchHandle,
1671 audio_source_t source)
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -08001672{
1673 sp<AudioCommand>command = new AudioCommand();
1674 command->mCommand = RECORDING_CONFIGURATION_UPDATE;
1675 RecordingConfigurationUpdateData *data = new RecordingConfigurationUpdateData();
1676 data->mEvent = event;
Jean-Michel Triviac4e4292016-12-22 11:39:31 -08001677 data->mClientInfo = *clientInfo;
Jean-Michel Trivi7281aa92016-02-17 15:33:40 -08001678 data->mClientConfig = *clientConfig;
Eric Laurenta9f86652018-11-28 17:23:11 -08001679 data->mClientEffects = clientEffects;
Jean-Michel Trivi7281aa92016-02-17 15:33:40 -08001680 data->mDeviceConfig = *deviceConfig;
Eric Laurenta9f86652018-11-28 17:23:11 -08001681 data->mEffects = effects;
Jean-Michel Trivi8c7cf3b2016-02-25 17:08:24 -08001682 data->mPatchHandle = patchHandle;
Eric Laurenta9f86652018-11-28 17:23:11 -08001683 data->mSource = source;
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -08001684 command->mParam = data;
Jean-Michel Triviac4e4292016-12-22 11:39:31 -08001685 ALOGV("AudioCommandThread() adding recording configuration update event %d, source %d uid %u",
1686 event, clientInfo->source, clientInfo->uid);
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -08001687 sendCommand(command);
1688}
1689
Mikhail Naganov88b30d22020-03-09 19:43:13 +00001690void AudioPolicyService::AudioCommandThread::audioModulesUpdateCommand()
1691{
1692 sp<AudioCommand> command = new AudioCommand();
1693 command->mCommand = AUDIO_MODULES_UPDATE;
1694 sendCommand(command);
1695}
1696
Eric Laurent0ede8922014-05-09 18:04:42 -07001697status_t AudioPolicyService::AudioCommandThread::sendCommand(sp<AudioCommand>& command, int delayMs)
1698{
1699 {
1700 Mutex::Autolock _l(mLock);
1701 insertCommand_l(command, delayMs);
1702 mWaitWorkCV.signal();
1703 }
1704 Mutex::Autolock _l(command->mLock);
1705 while (command->mWaitStatus) {
1706 nsecs_t timeOutNs = kAudioCommandTimeoutNs + milliseconds(delayMs);
1707 if (command->mCond.waitRelative(command->mLock, timeOutNs) != NO_ERROR) {
1708 command->mStatus = TIMED_OUT;
1709 command->mWaitStatus = false;
1710 }
1711 }
1712 return command->mStatus;
Eric Laurentbfb1b832013-01-07 09:53:42 -08001713}
1714
Mathias Agopian65ab4712010-07-14 17:59:35 -07001715// insertCommand_l() must be called with mLock held
Eric Laurent0ede8922014-05-09 18:04:42 -07001716void AudioPolicyService::AudioCommandThread::insertCommand_l(sp<AudioCommand>& command, int delayMs)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001717{
Glenn Kasten8d6a2442012-02-08 14:04:28 -08001718 ssize_t i; // not size_t because i will count down to -1
Eric Laurent0ede8922014-05-09 18:04:42 -07001719 Vector < sp<AudioCommand> > removedCommands;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001720 command->mTime = systemTime() + milliseconds(delayMs);
1721
1722 // acquire wake lock to make sure delayed commands are processed
Eric Laurentbfb1b832013-01-07 09:53:42 -08001723 if (mAudioCommands.isEmpty()) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07001724 acquire_wake_lock(PARTIAL_WAKE_LOCK, mName.string());
1725 }
1726
1727 // check same pending commands with later time stamps and eliminate them
Ivan Lozano5ff158f2017-10-30 09:06:24 -07001728 for (i = (ssize_t)mAudioCommands.size()-1; i >= 0; i--) {
Eric Laurent0ede8922014-05-09 18:04:42 -07001729 sp<AudioCommand> command2 = mAudioCommands[i];
Mathias Agopian65ab4712010-07-14 17:59:35 -07001730 // commands are sorted by increasing time stamp: no need to scan the rest of mAudioCommands
1731 if (command2->mTime <= command->mTime) break;
Eric Laurente45b48a2014-09-04 16:40:57 -07001732
1733 // create audio patch or release audio patch commands are equivalent
1734 // with regard to filtering
1735 if ((command->mCommand == CREATE_AUDIO_PATCH) ||
1736 (command->mCommand == RELEASE_AUDIO_PATCH)) {
1737 if ((command2->mCommand != CREATE_AUDIO_PATCH) &&
1738 (command2->mCommand != RELEASE_AUDIO_PATCH)) {
1739 continue;
1740 }
1741 } else if (command2->mCommand != command->mCommand) continue;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001742
1743 switch (command->mCommand) {
1744 case SET_PARAMETERS: {
Eric Laurent0ede8922014-05-09 18:04:42 -07001745 ParametersData *data = (ParametersData *)command->mParam.get();
1746 ParametersData *data2 = (ParametersData *)command2->mParam.get();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001747 if (data->mIO != data2->mIO) break;
Steve Block3856b092011-10-20 11:56:00 +01001748 ALOGV("Comparing parameter command %s to new command %s",
Eric Laurentde070132010-07-13 04:45:46 -07001749 data2->mKeyValuePairs.string(), data->mKeyValuePairs.string());
Mathias Agopian65ab4712010-07-14 17:59:35 -07001750 AudioParameter param = AudioParameter(data->mKeyValuePairs);
1751 AudioParameter param2 = AudioParameter(data2->mKeyValuePairs);
1752 for (size_t j = 0; j < param.size(); j++) {
Glenn Kastene53b9ea2012-03-12 16:29:55 -07001753 String8 key;
1754 String8 value;
1755 param.getAt(j, key, value);
1756 for (size_t k = 0; k < param2.size(); k++) {
1757 String8 key2;
1758 String8 value2;
1759 param2.getAt(k, key2, value2);
1760 if (key2 == key) {
1761 param2.remove(key2);
1762 ALOGV("Filtering out parameter %s", key2.string());
1763 break;
1764 }
1765 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001766 }
1767 // if all keys have been filtered out, remove the command.
1768 // otherwise, update the key value pairs
1769 if (param2.size() == 0) {
1770 removedCommands.add(command2);
1771 } else {
1772 data2->mKeyValuePairs = param2.toString();
1773 }
Eric Laurent21e54562013-09-23 12:08:05 -07001774 command->mTime = command2->mTime;
1775 // force delayMs to non 0 so that code below does not request to wait for
1776 // command status as the command is now delayed
1777 delayMs = 1;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001778 } break;
1779
1780 case SET_VOLUME: {
Eric Laurent0ede8922014-05-09 18:04:42 -07001781 VolumeData *data = (VolumeData *)command->mParam.get();
1782 VolumeData *data2 = (VolumeData *)command2->mParam.get();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001783 if (data->mIO != data2->mIO) break;
1784 if (data->mStream != data2->mStream) break;
Steve Block3856b092011-10-20 11:56:00 +01001785 ALOGV("Filtering out volume command on output %d for stream %d",
Eric Laurentde070132010-07-13 04:45:46 -07001786 data->mIO, data->mStream);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001787 removedCommands.add(command2);
Eric Laurent21e54562013-09-23 12:08:05 -07001788 command->mTime = command2->mTime;
1789 // force delayMs to non 0 so that code below does not request to wait for
1790 // command status as the command is now delayed
1791 delayMs = 1;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001792 } break;
Eric Laurente45b48a2014-09-04 16:40:57 -07001793
Eric Laurentbaf35fe2016-07-27 15:36:53 -07001794 case SET_VOICE_VOLUME: {
1795 VoiceVolumeData *data = (VoiceVolumeData *)command->mParam.get();
1796 VoiceVolumeData *data2 = (VoiceVolumeData *)command2->mParam.get();
1797 ALOGV("Filtering out voice volume command value %f replaced by %f",
1798 data2->mVolume, data->mVolume);
1799 removedCommands.add(command2);
1800 command->mTime = command2->mTime;
1801 // force delayMs to non 0 so that code below does not request to wait for
1802 // command status as the command is now delayed
1803 delayMs = 1;
1804 } break;
1805
Eric Laurente45b48a2014-09-04 16:40:57 -07001806 case CREATE_AUDIO_PATCH:
1807 case RELEASE_AUDIO_PATCH: {
1808 audio_patch_handle_t handle;
Haynes Mathew Georgea2d4a6d2014-10-13 13:05:22 -07001809 struct audio_patch patch;
Eric Laurente45b48a2014-09-04 16:40:57 -07001810 if (command->mCommand == CREATE_AUDIO_PATCH) {
1811 handle = ((CreateAudioPatchData *)command->mParam.get())->mHandle;
Haynes Mathew Georgea2d4a6d2014-10-13 13:05:22 -07001812 patch = ((CreateAudioPatchData *)command->mParam.get())->mPatch;
Eric Laurente45b48a2014-09-04 16:40:57 -07001813 } else {
1814 handle = ((ReleaseAudioPatchData *)command->mParam.get())->mHandle;
Mikhail Naganov7be71d22018-05-23 16:51:46 -07001815 memset(&patch, 0, sizeof(patch));
Eric Laurente45b48a2014-09-04 16:40:57 -07001816 }
1817 audio_patch_handle_t handle2;
Haynes Mathew Georgea2d4a6d2014-10-13 13:05:22 -07001818 struct audio_patch patch2;
Eric Laurente45b48a2014-09-04 16:40:57 -07001819 if (command2->mCommand == CREATE_AUDIO_PATCH) {
1820 handle2 = ((CreateAudioPatchData *)command2->mParam.get())->mHandle;
Haynes Mathew Georgea2d4a6d2014-10-13 13:05:22 -07001821 patch2 = ((CreateAudioPatchData *)command2->mParam.get())->mPatch;
Eric Laurente45b48a2014-09-04 16:40:57 -07001822 } else {
1823 handle2 = ((ReleaseAudioPatchData *)command2->mParam.get())->mHandle;
Glenn Kastenf60b6b62015-07-06 10:53:26 -07001824 memset(&patch2, 0, sizeof(patch2));
Eric Laurente45b48a2014-09-04 16:40:57 -07001825 }
1826 if (handle != handle2) break;
Haynes Mathew Georgea2d4a6d2014-10-13 13:05:22 -07001827 /* Filter CREATE_AUDIO_PATCH commands only when they are issued for
1828 same output. */
1829 if( (command->mCommand == CREATE_AUDIO_PATCH) &&
1830 (command2->mCommand == CREATE_AUDIO_PATCH) ) {
1831 bool isOutputDiff = false;
1832 if (patch.num_sources == patch2.num_sources) {
1833 for (unsigned count = 0; count < patch.num_sources; count++) {
1834 if (patch.sources[count].id != patch2.sources[count].id) {
1835 isOutputDiff = true;
1836 break;
1837 }
1838 }
1839 if (isOutputDiff)
1840 break;
1841 }
1842 }
Eric Laurente45b48a2014-09-04 16:40:57 -07001843 ALOGV("Filtering out %s audio patch command for handle %d",
1844 (command->mCommand == CREATE_AUDIO_PATCH) ? "create" : "release", handle);
1845 removedCommands.add(command2);
1846 command->mTime = command2->mTime;
1847 // force delayMs to non 0 so that code below does not request to wait for
1848 // command status as the command is now delayed
1849 delayMs = 1;
1850 } break;
1851
Jean-Michel Trivide801052015-04-14 19:10:14 -07001852 case DYN_POLICY_MIX_STATE_UPDATE: {
1853
1854 } break;
1855
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -08001856 case RECORDING_CONFIGURATION_UPDATE: {
1857
1858 } break;
1859
Mathias Agopian65ab4712010-07-14 17:59:35 -07001860 default:
1861 break;
1862 }
1863 }
1864
1865 // remove filtered commands
1866 for (size_t j = 0; j < removedCommands.size(); j++) {
1867 // removed commands always have time stamps greater than current command
1868 for (size_t k = i + 1; k < mAudioCommands.size(); k++) {
Eric Laurent0ede8922014-05-09 18:04:42 -07001869 if (mAudioCommands[k].get() == removedCommands[j].get()) {
Steve Block3856b092011-10-20 11:56:00 +01001870 ALOGV("suppressing command: %d", mAudioCommands[k]->mCommand);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001871 mAudioCommands.removeAt(k);
1872 break;
1873 }
1874 }
1875 }
1876 removedCommands.clear();
1877
Eric Laurentaa79bef2015-01-15 14:33:51 -08001878 // Disable wait for status if delay is not 0.
1879 // Except for create audio patch command because the returned patch handle
1880 // is needed by audio policy manager
1881 if (delayMs != 0 && command->mCommand != CREATE_AUDIO_PATCH) {
Eric Laurentcec4abb2012-07-03 12:23:02 -07001882 command->mWaitStatus = false;
1883 }
Eric Laurentcec4abb2012-07-03 12:23:02 -07001884
Mathias Agopian65ab4712010-07-14 17:59:35 -07001885 // insert command at the right place according to its time stamp
Eric Laurent1e693b52014-07-09 15:03:28 -07001886 ALOGV("inserting command: %d at index %zd, num commands %zu",
1887 command->mCommand, i+1, mAudioCommands.size());
Mathias Agopian65ab4712010-07-14 17:59:35 -07001888 mAudioCommands.insertAt(command, i + 1);
1889}
1890
1891void AudioPolicyService::AudioCommandThread::exit()
1892{
Steve Block3856b092011-10-20 11:56:00 +01001893 ALOGV("AudioCommandThread::exit");
Mathias Agopian65ab4712010-07-14 17:59:35 -07001894 {
1895 AutoMutex _l(mLock);
1896 requestExit();
1897 mWaitWorkCV.signal();
1898 }
Zach Janga754b4f2015-10-27 01:29:34 +00001899 // Note that we can call it from the thread loop if all other references have been released
1900 // but it will safely return WOULD_BLOCK in this case
Mathias Agopian65ab4712010-07-14 17:59:35 -07001901 requestExitAndWait();
1902}
1903
1904void AudioPolicyService::AudioCommandThread::AudioCommand::dump(char* buffer, size_t size)
1905{
1906 snprintf(buffer, size, " %02d %06d.%03d %01u %p\n",
1907 mCommand,
1908 (int)ns2s(mTime),
1909 (int)ns2ms(mTime)%1000,
1910 mWaitStatus,
Eric Laurent0ede8922014-05-09 18:04:42 -07001911 mParam.get());
Mathias Agopian65ab4712010-07-14 17:59:35 -07001912}
1913
Dima Zavinfce7a472011-04-19 22:30:36 -07001914/******* helpers for the service_ops callbacks defined below *********/
1915void AudioPolicyService::setParameters(audio_io_handle_t ioHandle,
1916 const char *keyValuePairs,
1917 int delayMs)
1918{
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001919 mAudioCommandThread->parametersCommand(ioHandle, keyValuePairs,
Dima Zavinfce7a472011-04-19 22:30:36 -07001920 delayMs);
1921}
1922
1923int AudioPolicyService::setStreamVolume(audio_stream_type_t stream,
1924 float volume,
1925 audio_io_handle_t output,
1926 int delayMs)
1927{
Glenn Kastenfff6d712012-01-12 16:38:12 -08001928 return (int)mAudioCommandThread->volumeCommand(stream, volume,
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001929 output, delayMs);
Dima Zavinfce7a472011-04-19 22:30:36 -07001930}
1931
Dima Zavinfce7a472011-04-19 22:30:36 -07001932int AudioPolicyService::setVoiceVolume(float volume, int delayMs)
1933{
1934 return (int)mAudioCommandThread->voiceVolumeCommand(volume, delayMs);
1935}
1936
Eric Laurentb20cf7d2019-04-05 19:37:34 -07001937void AudioPolicyService::setEffectSuspended(int effectId,
1938 audio_session_t sessionId,
1939 bool suspended)
1940{
1941 mAudioCommandThread->setEffectSuspendedCommand(effectId, sessionId, suspended);
1942}
1943
Mikhail Naganov88b30d22020-03-09 19:43:13 +00001944void AudioPolicyService::onNewAudioModulesAvailable()
1945{
Mikhail Naganovd0e2c742020-03-25 15:59:59 -07001946 mOutputCommandThread->audioModulesUpdateCommand();
Mikhail Naganov88b30d22020-03-09 19:43:13 +00001947}
1948
Eric Laurentb20cf7d2019-04-05 19:37:34 -07001949
Dima Zavinfce7a472011-04-19 22:30:36 -07001950extern "C" {
Eric Laurent2d388ec2014-03-07 13:25:54 -08001951audio_module_handle_t aps_load_hw_module(void *service __unused,
1952 const char *name);
1953audio_io_handle_t aps_open_output(void *service __unused,
Eric Laurenta4c5a552012-03-29 10:12:40 -07001954 audio_devices_t *pDevices,
1955 uint32_t *pSamplingRate,
1956 audio_format_t *pFormat,
1957 audio_channel_mask_t *pChannelMask,
1958 uint32_t *pLatencyMs,
Eric Laurent2d388ec2014-03-07 13:25:54 -08001959 audio_output_flags_t flags);
Eric Laurenta4c5a552012-03-29 10:12:40 -07001960
Eric Laurent2d388ec2014-03-07 13:25:54 -08001961audio_io_handle_t aps_open_output_on_module(void *service __unused,
Eric Laurenta4c5a552012-03-29 10:12:40 -07001962 audio_module_handle_t module,
1963 audio_devices_t *pDevices,
1964 uint32_t *pSamplingRate,
1965 audio_format_t *pFormat,
1966 audio_channel_mask_t *pChannelMask,
1967 uint32_t *pLatencyMs,
Richard Fitzgeraldad3af332013-03-25 16:54:37 +00001968 audio_output_flags_t flags,
Eric Laurent2d388ec2014-03-07 13:25:54 -08001969 const audio_offload_info_t *offloadInfo);
1970audio_io_handle_t aps_open_dup_output(void *service __unused,
Dima Zavinfce7a472011-04-19 22:30:36 -07001971 audio_io_handle_t output1,
Eric Laurent2d388ec2014-03-07 13:25:54 -08001972 audio_io_handle_t output2);
1973int aps_close_output(void *service __unused, audio_io_handle_t output);
1974int aps_suspend_output(void *service __unused, audio_io_handle_t output);
1975int aps_restore_output(void *service __unused, audio_io_handle_t output);
1976audio_io_handle_t aps_open_input(void *service __unused,
Eric Laurenta4c5a552012-03-29 10:12:40 -07001977 audio_devices_t *pDevices,
1978 uint32_t *pSamplingRate,
1979 audio_format_t *pFormat,
1980 audio_channel_mask_t *pChannelMask,
Eric Laurent2d388ec2014-03-07 13:25:54 -08001981 audio_in_acoustics_t acoustics __unused);
1982audio_io_handle_t aps_open_input_on_module(void *service __unused,
Eric Laurenta4c5a552012-03-29 10:12:40 -07001983 audio_module_handle_t module,
1984 audio_devices_t *pDevices,
1985 uint32_t *pSamplingRate,
1986 audio_format_t *pFormat,
Eric Laurent2d388ec2014-03-07 13:25:54 -08001987 audio_channel_mask_t *pChannelMask);
1988int aps_close_input(void *service __unused, audio_io_handle_t input);
1989int aps_invalidate_stream(void *service __unused, audio_stream_type_t stream);
Glenn Kastend848eb42016-03-08 13:42:11 -08001990int aps_move_effects(void *service __unused, audio_session_t session,
Dima Zavinfce7a472011-04-19 22:30:36 -07001991 audio_io_handle_t src_output,
Eric Laurent2d388ec2014-03-07 13:25:54 -08001992 audio_io_handle_t dst_output);
1993char * aps_get_parameters(void *service __unused, audio_io_handle_t io_handle,
1994 const char *keys);
1995void aps_set_parameters(void *service, audio_io_handle_t io_handle,
1996 const char *kv_pairs, int delay_ms);
1997int aps_set_stream_volume(void *service, audio_stream_type_t stream,
Dima Zavinfce7a472011-04-19 22:30:36 -07001998 float volume, audio_io_handle_t output,
Eric Laurent2d388ec2014-03-07 13:25:54 -08001999 int delay_ms);
Eric Laurent2d388ec2014-03-07 13:25:54 -08002000int aps_set_voice_volume(void *service, float volume, int delay_ms);
2001};
Dima Zavinfce7a472011-04-19 22:30:36 -07002002
Mikhail Naganov1b2a7942017-12-08 10:18:09 -08002003} // namespace android