blob: d71a317492c6f2d48320e347e7a8066bee6ad544 [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 Laurentb809a752020-06-29 09:53:13 -0700488 sp<AudioRecordClient> latestSensitiveActiveOrComm;
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 Laurentb809a752020-06-29 09:53:13 -0700502 bool isPhoneStateOwnerActive = false;
Eric Laurent4eb58f12018-12-07 16:41:02 -0800503
Michael Groovercfd28302018-12-11 19:16:46 -0800504 // if Sensor Privacy is enabled then all recordings should be silenced.
505 if (mSensorPrivacyPolicy->isSensorPrivacyEnabled()) {
506 silenceAllRecordings_l();
507 return;
508 }
509
Eric Laurente8c8b432018-10-17 10:08:02 -0700510 for (size_t i =0; i < mAudioRecordClients.size(); i++) {
511 sp<AudioRecordClient> current = mAudioRecordClients[i];
Eric Laurent1ff16a72019-03-14 18:35:04 -0700512 if (!current->active) {
513 continue;
Eric Laurent4eb58f12018-12-07 16:41:02 -0800514 }
Eric Laurent1ff16a72019-03-14 18:35:04 -0700515
516 app_state_t appState = apmStatFromAmState(mUidPolicy->getUidState(current->uid));
517 // clients which app is in IDLE state are not eligible for top active or
518 // latest active
519 if (appState == APP_STATE_IDLE) {
520 continue;
521 }
522
Eric Laurent589171c2019-07-25 18:04:29 -0700523 bool isAccessibility = mUidPolicy->isA11yUid(current->uid);
Eric Laurent14a88632020-07-16 12:28:30 -0700524 // Clients capturing for Accessibility services or virtual sources are not considered
Eric Laurentc21d5692020-02-25 10:24:36 -0800525 // for top or latest active to avoid masking regular clients started before
Eric Laurent14a88632020-07-16 12:28:30 -0700526 if (!isAccessibility && !isVirtualSource(current->attributes.source)) {
Eric Laurentc21d5692020-02-25 10:24:36 -0800527 bool isAssistant = mUidPolicy->isAssistantUid(current->uid);
528 bool isPrivacySensitive =
529 (current->attributes.flags & AUDIO_FLAG_CAPTURE_PRIVATE) != 0;
Eric Laurentb809a752020-06-29 09:53:13 -0700530
Eric Laurentc21d5692020-02-25 10:24:36 -0800531 if (appState == APP_STATE_TOP) {
532 if (isPrivacySensitive) {
533 if (current->startTimeNs > topSensitiveStartNs) {
534 topSensitiveActive = current;
535 topSensitiveStartNs = current->startTimeNs;
536 }
537 } else {
538 if (current->startTimeNs > topStartNs) {
539 topActive = current;
540 topStartNs = current->startTimeNs;
541 }
542 }
543 if (isAssistant) {
544 isAssistantOnTop = true;
545 }
Eric Laurenta46bedb2018-12-07 18:01:26 -0800546 }
Eric Laurentc21d5692020-02-25 10:24:36 -0800547 // Clients capturing for HOTWORD are not considered
548 // for latest active to avoid masking regular clients started before
549 if (!(current->attributes.source == AUDIO_SOURCE_HOTWORD
550 || ((isA11yOnTop || rttCallActive) && isAssistant))) {
551 if (isPrivacySensitive) {
Eric Laurentb809a752020-06-29 09:53:13 -0700552 // if audio mode is IN_COMMUNICATION, make sure the audio mode owner
553 // is marked latest sensitive active even if another app qualifies.
554 if (current->startTimeNs > latestSensitiveStartNs
555 || (isInCommunication && current->uid == mPhoneStateOwnerUid)) {
556 if (!isInCommunication || latestSensitiveActiveOrComm == nullptr
557 || latestSensitiveActiveOrComm->uid != mPhoneStateOwnerUid) {
558 latestSensitiveActiveOrComm = current;
559 latestSensitiveStartNs = current->startTimeNs;
560 }
Eric Laurentc21d5692020-02-25 10:24:36 -0800561 }
562 isSensitiveActive = true;
563 } else {
564 if (current->startTimeNs > latestStartNs) {
565 latestActive = current;
566 latestStartNs = current->startTimeNs;
567 }
568 }
Eric Laurent4eb58f12018-12-07 16:41:02 -0800569 }
570 }
Eric Laurent4e947da2019-10-17 15:24:06 -0700571 if (current->attributes.source != AUDIO_SOURCE_HOTWORD) {
572 onlyHotwordActive = false;
573 }
Eric Laurentb809a752020-06-29 09:53:13 -0700574 if (current->uid == mPhoneStateOwnerUid) {
575 isPhoneStateOwnerActive = true;
576 }
Eric Laurent4eb58f12018-12-07 16:41:02 -0800577 }
578
Eric Laurent1ff16a72019-03-14 18:35:04 -0700579 // if no active client with UI on Top, consider latest active as top
580 if (topActive == nullptr) {
581 topActive = latestActive;
Eric Laurentc21d5692020-02-25 10:24:36 -0800582 topStartNs = latestStartNs;
583 }
584 if (topSensitiveActive == nullptr) {
Eric Laurentb809a752020-06-29 09:53:13 -0700585 topSensitiveActive = latestSensitiveActiveOrComm;
Eric Laurentc21d5692020-02-25 10:24:36 -0800586 topSensitiveStartNs = latestSensitiveStartNs;
Eric Laurentb809a752020-06-29 09:53:13 -0700587 } else if (latestSensitiveActiveOrComm != nullptr) {
588 // if audio mode is IN_COMMUNICATION, favor audio mode owner over an app with
589 // foreground UI in case both are capturing with privacy sensitive flag.
590 if (isInCommunication && latestSensitiveActiveOrComm->uid == mPhoneStateOwnerUid) {
591 topSensitiveActive = latestSensitiveActiveOrComm;
592 topSensitiveStartNs = latestSensitiveStartNs;
593 }
Eric Laurentc21d5692020-02-25 10:24:36 -0800594 }
595
596 // If both privacy sensitive and regular capture are active:
597 // if the regular capture is privileged
598 // allow concurrency
599 // else
600 // favor the privacy sensitive case
601 if (topActive != nullptr && topSensitiveActive != nullptr
Ricardo Correa57a37692020-03-23 17:27:25 -0700602 && !topActive->canCaptureOutput) {
Eric Laurentc21d5692020-02-25 10:24:36 -0800603 topActive = nullptr;
Eric Laurent4eb58f12018-12-07 16:41:02 -0800604 }
605
606 for (size_t i =0; i < mAudioRecordClients.size(); i++) {
607 sp<AudioRecordClient> current = mAudioRecordClients[i];
Eric Laurent1ff16a72019-03-14 18:35:04 -0700608 if (!current->active) {
609 continue;
610 }
611
Eric Laurent4eb58f12018-12-07 16:41:02 -0800612 audio_source_t source = current->attributes.source;
Eric Laurent1ff16a72019-03-14 18:35:04 -0700613 bool isTopOrLatestActive = topActive == nullptr ? false : current->uid == topActive->uid;
Eric Laurentc21d5692020-02-25 10:24:36 -0800614 bool isTopOrLatestSensitive = topSensitiveActive == nullptr ?
615 false : current->uid == topSensitiveActive->uid;
616
Mikhail Naganov12b716c2020-04-30 22:37:43 +0000617 auto canCaptureIfInCallOrCommunication = [&](const auto &recordClient) REQUIRES(mLock) {
Ricardo Correa57a37692020-03-23 17:27:25 -0700618 bool canCaptureCall = recordClient->canCaptureOutput;
Eric Laurentb809a752020-06-29 09:53:13 -0700619 bool canCaptureCommunication = recordClient->canCaptureOutput
620 || !isPhoneStateOwnerActive
621 || recordClient->uid == mPhoneStateOwnerUid;
622 return !(isInCall && !canCaptureCall)
623 && !(isInCommunication && !canCaptureCommunication);
Eric Laurentc21d5692020-02-25 10:24:36 -0800624 };
Eric Laurent1ff16a72019-03-14 18:35:04 -0700625
626 // By default allow capture if:
627 // The assistant is not on TOP
Eric Laurenta171e352019-05-07 13:04:45 -0700628 // AND is on TOP or latest started
Eric Laurent1ff16a72019-03-14 18:35:04 -0700629 // AND there is no active privacy sensitive capture or call
630 // OR client has CAPTURE_AUDIO_OUTPUT privileged permission
631 bool allowCapture = !isAssistantOnTop
Eric Laurentc21d5692020-02-25 10:24:36 -0800632 && (isTopOrLatestActive || isTopOrLatestSensitive)
633 && !(isSensitiveActive
Ricardo Correa57a37692020-03-23 17:27:25 -0700634 && !(isTopOrLatestSensitive || current->canCaptureOutput))
Eric Laurentc21d5692020-02-25 10:24:36 -0800635 && canCaptureIfInCallOrCommunication(current);
Eric Laurent2dc962b2019-03-01 08:25:25 -0800636
637 if (isVirtualSource(source)) {
Eric Laurent1ff16a72019-03-14 18:35:04 -0700638 // Allow capture for virtual (remote submix, call audio TX or RX...) sources
639 allowCapture = true;
Eric Laurent2dc962b2019-03-01 08:25:25 -0800640 } else if (mUidPolicy->isAssistantUid(current->uid)) {
Eric Laurent1ff16a72019-03-14 18:35:04 -0700641 // For assistant allow capture if:
Eric Laurent6ede98f2019-06-11 14:50:30 -0700642 // An accessibility service is on TOP or a RTT call is active
Eric Laurent1ff16a72019-03-14 18:35:04 -0700643 // AND the source is VOICE_RECOGNITION or HOTWORD
Eric Laurenta171e352019-05-07 13:04:45 -0700644 // OR is on TOP AND uses VOICE_RECOGNITION
Eric Laurent1ff16a72019-03-14 18:35:04 -0700645 // OR uses HOTWORD
646 // AND there is no active privacy sensitive capture or call
647 // OR client has CAPTURE_AUDIO_OUTPUT privileged permission
Eric Laurent6ede98f2019-06-11 14:50:30 -0700648 if (isA11yOnTop || rttCallActive) {
Eric Laurent4eb58f12018-12-07 16:41:02 -0800649 if (source == AUDIO_SOURCE_HOTWORD || source == AUDIO_SOURCE_VOICE_RECOGNITION) {
Eric Laurent1ff16a72019-03-14 18:35:04 -0700650 allowCapture = true;
Eric Laurent4eb58f12018-12-07 16:41:02 -0800651 }
652 } else {
Eric Laurenta171e352019-05-07 13:04:45 -0700653 if (((isAssistantOnTop && source == AUDIO_SOURCE_VOICE_RECOGNITION) ||
Eric Laurentc21d5692020-02-25 10:24:36 -0800654 source == AUDIO_SOURCE_HOTWORD)
Ricardo Correa57a37692020-03-23 17:27:25 -0700655 && !(isSensitiveActive && !current->canCaptureOutput)
Eric Laurentc21d5692020-02-25 10:24:36 -0800656 && canCaptureIfInCallOrCommunication(current)) {
Eric Laurent1ff16a72019-03-14 18:35:04 -0700657 allowCapture = true;
Eric Laurent4eb58f12018-12-07 16:41:02 -0800658 }
659 }
660 } else if (mUidPolicy->isA11yUid(current->uid)) {
Eric Laurent1ff16a72019-03-14 18:35:04 -0700661 // For accessibility service allow capture if:
Eric Laurent47670c92019-08-28 16:59:05 -0700662 // The assistant is not on TOP
663 // AND there is no active privacy sensitive capture or call
Eric Laurent589171c2019-07-25 18:04:29 -0700664 // OR client has CAPTURE_AUDIO_OUTPUT privileged permission
Eric Laurent47670c92019-08-28 16:59:05 -0700665 // OR
666 // Is on TOP AND the source is VOICE_RECOGNITION or HOTWORD
667 if (!isAssistantOnTop
Ricardo Correa57a37692020-03-23 17:27:25 -0700668 && !(isSensitiveActive && !current->canCaptureOutput)
Eric Laurentc21d5692020-02-25 10:24:36 -0800669 && canCaptureIfInCallOrCommunication(current)) {
Eric Laurent47670c92019-08-28 16:59:05 -0700670 allowCapture = true;
671 }
Eric Laurent589171c2019-07-25 18:04:29 -0700672 if (isA11yOnTop) {
673 if (source == AUDIO_SOURCE_VOICE_RECOGNITION || source == AUDIO_SOURCE_HOTWORD) {
674 allowCapture = true;
675 }
Eric Laurent4eb58f12018-12-07 16:41:02 -0800676 }
Eric Laurent4e947da2019-10-17 15:24:06 -0700677 } else if (source == AUDIO_SOURCE_HOTWORD) {
678 // For HOTWORD source allow capture when not on TOP if:
679 // All active clients are using HOTWORD source
680 // AND no call is active
681 // OR client has CAPTURE_AUDIO_OUTPUT privileged permission
Eric Laurentc21d5692020-02-25 10:24:36 -0800682 if (onlyHotwordActive
683 && canCaptureIfInCallOrCommunication(current)) {
Eric Laurent4e947da2019-10-17 15:24:06 -0700684 allowCapture = true;
685 }
Kohsuke Yatoha623a132020-03-24 20:10:26 -0700686 } else if (mUidPolicy->isCurrentImeUid(current->uid)) {
687 // For current InputMethodService allow capture if:
688 // A RTT call is active AND the source is VOICE_RECOGNITION
689 if (rttCallActive && source == AUDIO_SOURCE_VOICE_RECOGNITION) {
690 allowCapture = true;
691 }
Eric Laurent4eb58f12018-12-07 16:41:02 -0800692 }
Eric Laurent5ada82e2019-08-29 17:53:54 -0700693 setAppState_l(current->portId,
Eric Laurent1ff16a72019-03-14 18:35:04 -0700694 allowCapture ? apmStatFromAmState(mUidPolicy->getUidState(current->uid)) :
695 APP_STATE_IDLE);
Eric Laurente8c8b432018-10-17 10:08:02 -0700696 }
697}
698
Michael Groovercfd28302018-12-11 19:16:46 -0800699void AudioPolicyService::silenceAllRecordings_l() {
700 for (size_t i = 0; i < mAudioRecordClients.size(); i++) {
701 sp<AudioRecordClient> current = mAudioRecordClients[i];
Eric Laurent1ff16a72019-03-14 18:35:04 -0700702 if (!isVirtualSource(current->attributes.source)) {
Eric Laurent5ada82e2019-08-29 17:53:54 -0700703 setAppState_l(current->portId, APP_STATE_IDLE);
Eric Laurent1ff16a72019-03-14 18:35:04 -0700704 }
Michael Groovercfd28302018-12-11 19:16:46 -0800705 }
706}
707
Eric Laurente8c8b432018-10-17 10:08:02 -0700708/* static */
709app_state_t AudioPolicyService::apmStatFromAmState(int amState) {
Eric Laurent1ff16a72019-03-14 18:35:04 -0700710
711 if (amState == ActivityManager::PROCESS_STATE_UNKNOWN) {
Eric Laurente8c8b432018-10-17 10:08:02 -0700712 return APP_STATE_IDLE;
Eric Laurent1ff16a72019-03-14 18:35:04 -0700713 } else if (amState <= ActivityManager::PROCESS_STATE_TOP) {
714 // include persistent services
715 return APP_STATE_TOP;
Eric Laurente8c8b432018-10-17 10:08:02 -0700716 }
717 return APP_STATE_FOREGROUND;
718}
719
Eric Laurent4eb58f12018-12-07 16:41:02 -0800720/* static */
Eric Laurent2dc962b2019-03-01 08:25:25 -0800721bool AudioPolicyService::isVirtualSource(audio_source_t source)
Eric Laurent4eb58f12018-12-07 16:41:02 -0800722{
723 switch (source) {
724 case AUDIO_SOURCE_VOICE_UPLINK:
725 case AUDIO_SOURCE_VOICE_DOWNLINK:
726 case AUDIO_SOURCE_VOICE_CALL:
Eric Laurent2dc962b2019-03-01 08:25:25 -0800727 case AUDIO_SOURCE_REMOTE_SUBMIX:
728 case AUDIO_SOURCE_FM_TUNER:
Eric Laurent68eb2122020-04-30 17:40:57 -0700729 case AUDIO_SOURCE_ECHO_REFERENCE:
Eric Laurent4eb58f12018-12-07 16:41:02 -0800730 return true;
731 default:
732 break;
733 }
734 return false;
735}
736
Eric Laurent5ada82e2019-08-29 17:53:54 -0700737void AudioPolicyService::setAppState_l(audio_port_handle_t portId, app_state_t state)
Eric Laurente8c8b432018-10-17 10:08:02 -0700738{
739 AutoCallerClear acc;
740
741 if (mAudioPolicyManager) {
Eric Laurent5ada82e2019-08-29 17:53:54 -0700742 mAudioPolicyManager->setAppState(portId, state);
Mikhail Naganoveae73eb2018-04-03 16:57:36 -0700743 }
744 sp<IAudioFlinger> af = AudioSystem::get_audio_flinger();
745 if (af) {
Eric Laurentf32108e2018-10-04 17:22:04 -0700746 bool silenced = state == APP_STATE_IDLE;
Eric Laurent5ada82e2019-08-29 17:53:54 -0700747 af->setRecordSilenced(portId, silenced);
Mikhail Naganoveae73eb2018-04-03 16:57:36 -0700748 }
Svet Ganovf4ddfef2018-01-16 07:37:58 -0800749}
750
Glenn Kasten0f11b512014-01-31 16:18:54 -0800751status_t AudioPolicyService::dump(int fd, const Vector<String16>& args __unused)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700752{
Glenn Kasten44deb052012-02-05 18:09:08 -0800753 if (!dumpAllowed()) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700754 dumpPermissionDenial(fd);
755 } else {
Mikhail Naganov12b716c2020-04-30 22:37:43 +0000756 const bool locked = dumpTryLock(mLock);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700757 if (!locked) {
758 String8 result(kDeadlockedString);
759 write(fd, result.string(), result.size());
760 }
761
762 dumpInternals(fd);
Glenn Kasten9d1f02d2012-02-08 17:47:58 -0800763 if (mAudioCommandThread != 0) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700764 mAudioCommandThread->dump(fd);
765 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700766
Eric Laurentdce54a12014-03-10 12:19:46 -0700767 if (mAudioPolicyManager) {
768 mAudioPolicyManager->dump(fd);
769 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700770
Kevin Rocard8be94972019-02-22 13:26:25 -0800771 mPackageManager.dump(fd);
772
Mikhail Naganov12b716c2020-04-30 22:37:43 +0000773 dumpReleaseLock(mLock, locked);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700774 }
775 return NO_ERROR;
776}
777
778status_t AudioPolicyService::dumpPermissionDenial(int fd)
779{
780 const size_t SIZE = 256;
781 char buffer[SIZE];
782 String8 result;
783 snprintf(buffer, SIZE, "Permission Denial: "
784 "can't dump AudioPolicyService from pid=%d, uid=%d\n",
785 IPCThreadState::self()->getCallingPid(),
786 IPCThreadState::self()->getCallingUid());
787 result.append(buffer);
788 write(fd, result.string(), result.size());
789 return NO_ERROR;
790}
791
792status_t AudioPolicyService::onTransact(
Svet Ganovf4ddfef2018-01-16 07:37:58 -0800793 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags) {
794 switch (code) {
795 case SHELL_COMMAND_TRANSACTION: {
796 int in = data.readFileDescriptor();
797 int out = data.readFileDescriptor();
798 int err = data.readFileDescriptor();
799 int argc = data.readInt32();
800 Vector<String16> args;
801 for (int i = 0; i < argc && data.dataAvail() > 0; i++) {
802 args.add(data.readString16());
803 }
804 sp<IBinder> unusedCallback;
805 sp<IResultReceiver> resultReceiver;
806 status_t status;
807 if ((status = data.readNullableStrongBinder(&unusedCallback)) != NO_ERROR) {
808 return status;
809 }
810 if ((status = data.readNullableStrongBinder(&resultReceiver)) != NO_ERROR) {
811 return status;
812 }
813 status = shellCommand(in, out, err, args);
814 if (resultReceiver != nullptr) {
815 resultReceiver->send(status);
816 }
817 return NO_ERROR;
818 }
819 }
820
Mathias Agopian65ab4712010-07-14 17:59:35 -0700821 return BnAudioPolicyService::onTransact(code, data, reply, flags);
822}
823
Svet Ganovf4ddfef2018-01-16 07:37:58 -0800824// ------------------- Shell command implementation -------------------
825
826// NOTE: This is a remote API - make sure all args are validated
827status_t AudioPolicyService::shellCommand(int in, int out, int err, Vector<String16>& args) {
828 if (!checkCallingPermission(sManageAudioPolicyPermission, nullptr, nullptr)) {
829 return PERMISSION_DENIED;
830 }
831 if (in == BAD_TYPE || out == BAD_TYPE || err == BAD_TYPE) {
832 return BAD_VALUE;
833 }
jovanakbe066e12019-09-02 11:54:39 -0700834 if (args.size() >= 3 && args[0] == String16("set-uid-state")) {
Svet Ganovf4ddfef2018-01-16 07:37:58 -0800835 return handleSetUidState(args, err);
jovanakbe066e12019-09-02 11:54:39 -0700836 } else if (args.size() >= 2 && args[0] == String16("reset-uid-state")) {
Svet Ganovf4ddfef2018-01-16 07:37:58 -0800837 return handleResetUidState(args, err);
jovanakbe066e12019-09-02 11:54:39 -0700838 } else if (args.size() >= 2 && args[0] == String16("get-uid-state")) {
Svet Ganovf4ddfef2018-01-16 07:37:58 -0800839 return handleGetUidState(args, out, err);
840 } else if (args.size() == 1 && args[0] == String16("help")) {
841 printHelp(out);
842 return NO_ERROR;
843 }
844 printHelp(err);
845 return BAD_VALUE;
846}
847
jovanakbe066e12019-09-02 11:54:39 -0700848static status_t getUidForPackage(String16 packageName, int userId, /*inout*/uid_t& uid, int err) {
849 if (userId < 0) {
850 ALOGE("Invalid user: %d", userId);
851 dprintf(err, "Invalid user: %d\n", userId);
Svet Ganovf4ddfef2018-01-16 07:37:58 -0800852 return BAD_VALUE;
853 }
jovanakbe066e12019-09-02 11:54:39 -0700854
855 PermissionController pc;
856 uid = pc.getPackageUid(packageName, 0);
857 if (uid <= 0) {
858 ALOGE("Unknown package: '%s'", String8(packageName).string());
859 dprintf(err, "Unknown package: '%s'\n", String8(packageName).string());
860 return BAD_VALUE;
861 }
862
863 uid = multiuser_get_uid(userId, uid);
864 return NO_ERROR;
865}
866
867status_t AudioPolicyService::handleSetUidState(Vector<String16>& args, int err) {
868 // Valid arg.size() is 3 or 5, args.size() is 5 with --user option.
869 if (!(args.size() == 3 || args.size() == 5)) {
870 printHelp(err);
871 return BAD_VALUE;
872 }
873
Svet Ganovf4ddfef2018-01-16 07:37:58 -0800874 bool active = false;
875 if (args[2] == String16("active")) {
876 active = true;
877 } else if ((args[2] != String16("idle"))) {
878 ALOGE("Expected active or idle but got: '%s'", String8(args[2]).string());
879 return BAD_VALUE;
880 }
jovanakbe066e12019-09-02 11:54:39 -0700881
882 int userId = 0;
883 if (args.size() >= 5 && args[3] == String16("--user")) {
884 userId = atoi(String8(args[4]));
885 }
886
887 uid_t uid;
888 if (getUidForPackage(args[1], userId, uid, err) == BAD_VALUE) {
889 return BAD_VALUE;
890 }
891
Mikhail Naganov12b716c2020-04-30 22:37:43 +0000892 sp<UidPolicy> uidPolicy;
893 {
894 Mutex::Autolock _l(mLock);
895 uidPolicy = mUidPolicy;
896 }
897 if (uidPolicy) {
898 uidPolicy->addOverrideUid(uid, active);
899 return NO_ERROR;
900 }
901 return NO_INIT;
Svet Ganovf4ddfef2018-01-16 07:37:58 -0800902}
903
904status_t AudioPolicyService::handleResetUidState(Vector<String16>& args, int err) {
jovanakbe066e12019-09-02 11:54:39 -0700905 // Valid arg.size() is 2 or 4, args.size() is 4 with --user option.
906 if (!(args.size() == 2 || args.size() == 4)) {
907 printHelp(err);
Svet Ganovf4ddfef2018-01-16 07:37:58 -0800908 return BAD_VALUE;
909 }
jovanakbe066e12019-09-02 11:54:39 -0700910
911 int userId = 0;
912 if (args.size() >= 4 && args[2] == String16("--user")) {
913 userId = atoi(String8(args[3]));
914 }
915
916 uid_t uid;
917 if (getUidForPackage(args[1], userId, uid, err) == BAD_VALUE) {
918 return BAD_VALUE;
919 }
920
Mikhail Naganov12b716c2020-04-30 22:37:43 +0000921 sp<UidPolicy> uidPolicy;
922 {
923 Mutex::Autolock _l(mLock);
924 uidPolicy = mUidPolicy;
925 }
926 if (uidPolicy) {
927 uidPolicy->removeOverrideUid(uid);
928 return NO_ERROR;
929 }
930 return NO_INIT;
Svet Ganovf4ddfef2018-01-16 07:37:58 -0800931}
932
933status_t AudioPolicyService::handleGetUidState(Vector<String16>& args, int out, int err) {
jovanakbe066e12019-09-02 11:54:39 -0700934 // Valid arg.size() is 2 or 4, args.size() is 4 with --user option.
935 if (!(args.size() == 2 || args.size() == 4)) {
936 printHelp(err);
Svet Ganovf4ddfef2018-01-16 07:37:58 -0800937 return BAD_VALUE;
938 }
jovanakbe066e12019-09-02 11:54:39 -0700939
940 int userId = 0;
941 if (args.size() >= 4 && args[2] == String16("--user")) {
942 userId = atoi(String8(args[3]));
943 }
944
945 uid_t uid;
946 if (getUidForPackage(args[1], userId, uid, err) == BAD_VALUE) {
947 return BAD_VALUE;
948 }
949
Mikhail Naganov12b716c2020-04-30 22:37:43 +0000950 sp<UidPolicy> uidPolicy;
951 {
952 Mutex::Autolock _l(mLock);
953 uidPolicy = mUidPolicy;
Svet Ganovf4ddfef2018-01-16 07:37:58 -0800954 }
Mikhail Naganov12b716c2020-04-30 22:37:43 +0000955 if (uidPolicy) {
956 return dprintf(out, uidPolicy->isUidActive(uid) ? "active\n" : "idle\n");
957 }
958 return NO_INIT;
Svet Ganovf4ddfef2018-01-16 07:37:58 -0800959}
960
961status_t AudioPolicyService::printHelp(int out) {
962 return dprintf(out, "Audio policy service commands:\n"
jovanakbe066e12019-09-02 11:54:39 -0700963 " get-uid-state <PACKAGE> [--user USER_ID] gets the uid state\n"
964 " set-uid-state <PACKAGE> <active|idle> [--user USER_ID] overrides the uid state\n"
965 " reset-uid-state <PACKAGE> [--user USER_ID] clears the uid state override\n"
Svet Ganovf4ddfef2018-01-16 07:37:58 -0800966 " help print this message\n");
967}
968
969// ----------- AudioPolicyService::UidPolicy implementation ----------
970
971void AudioPolicyService::UidPolicy::registerSelf() {
Steven Moreland2f348142019-07-02 15:59:07 -0700972 status_t res = mAm.linkToDeath(this);
973 mAm.registerUidObserver(this, ActivityManager::UID_OBSERVER_GONE
Svet Ganovf4ddfef2018-01-16 07:37:58 -0800974 | ActivityManager::UID_OBSERVER_IDLE
Eric Laurente8c8b432018-10-17 10:08:02 -0700975 | ActivityManager::UID_OBSERVER_ACTIVE
976 | ActivityManager::UID_OBSERVER_PROCSTATE,
Svet Ganovf4ddfef2018-01-16 07:37:58 -0800977 ActivityManager::PROCESS_STATE_UNKNOWN,
978 String16("audioserver"));
Mikhail Naganoveae73eb2018-04-03 16:57:36 -0700979 if (!res) {
980 Mutex::Autolock _l(mLock);
981 mObserverRegistered = true;
982 } else {
983 ALOGE("UidPolicy::registerSelf linkToDeath failed: %d", res);
Eric Laurent4eb58f12018-12-07 16:41:02 -0800984
Steven Moreland2f348142019-07-02 15:59:07 -0700985 mAm.unregisterUidObserver(this);
Mikhail Naganoveae73eb2018-04-03 16:57:36 -0700986 }
Svet Ganovf4ddfef2018-01-16 07:37:58 -0800987}
988
989void AudioPolicyService::UidPolicy::unregisterSelf() {
Steven Moreland2f348142019-07-02 15:59:07 -0700990 mAm.unlinkToDeath(this);
991 mAm.unregisterUidObserver(this);
Mikhail Naganoveae73eb2018-04-03 16:57:36 -0700992 Mutex::Autolock _l(mLock);
993 mObserverRegistered = false;
Svet Ganovf4ddfef2018-01-16 07:37:58 -0800994}
995
Mikhail Naganoveae73eb2018-04-03 16:57:36 -0700996void AudioPolicyService::UidPolicy::binderDied(__unused const wp<IBinder> &who) {
997 Mutex::Autolock _l(mLock);
998 mCachedUids.clear();
999 mObserverRegistered = false;
Svet Ganovf4ddfef2018-01-16 07:37:58 -08001000}
1001
Eric Laurente8c8b432018-10-17 10:08:02 -07001002void AudioPolicyService::UidPolicy::checkRegistered() {
Mikhail Naganoveae73eb2018-04-03 16:57:36 -07001003 bool needToReregister = false;
1004 {
1005 Mutex::Autolock _l(mLock);
1006 needToReregister = !mObserverRegistered;
Svet Ganovf4ddfef2018-01-16 07:37:58 -08001007 }
Mikhail Naganoveae73eb2018-04-03 16:57:36 -07001008 if (needToReregister) {
1009 // Looks like ActivityManager has died previously, attempt to re-register.
1010 registerSelf();
1011 }
Eric Laurente8c8b432018-10-17 10:08:02 -07001012}
1013
1014bool AudioPolicyService::UidPolicy::isUidActive(uid_t uid) {
1015 if (isServiceUid(uid)) return true;
1016 checkRegistered();
Mikhail Naganoveae73eb2018-04-03 16:57:36 -07001017 {
1018 Mutex::Autolock _l(mLock);
1019 auto overrideIter = mOverrideUids.find(uid);
1020 if (overrideIter != mOverrideUids.end()) {
Eric Laurente8c8b432018-10-17 10:08:02 -07001021 return overrideIter->second.first;
Mikhail Naganoveae73eb2018-04-03 16:57:36 -07001022 }
1023 // In an absense of the ActivityManager, assume everything to be active.
1024 if (!mObserverRegistered) return true;
1025 auto cacheIter = mCachedUids.find(uid);
Mikhail Naganoveba668a2018-04-05 08:13:15 -07001026 if (cacheIter != mCachedUids.end()) {
Eric Laurente8c8b432018-10-17 10:08:02 -07001027 return cacheIter->second.first;
Mikhail Naganoveae73eb2018-04-03 16:57:36 -07001028 }
1029 }
1030 ActivityManager am;
Hui Yu12c7ec72020-05-04 17:40:52 +00001031 bool active = am.isUidActive(uid, String16("audioserver"));
Mikhail Naganoveae73eb2018-04-03 16:57:36 -07001032 {
1033 Mutex::Autolock _l(mLock);
Eric Laurente8c8b432018-10-17 10:08:02 -07001034 mCachedUids.insert(std::pair<uid_t,
1035 std::pair<bool, int>>(uid, std::pair<bool, int>(active,
1036 ActivityManager::PROCESS_STATE_UNKNOWN)));
Mikhail Naganoveae73eb2018-04-03 16:57:36 -07001037 }
1038 return active;
Svet Ganovf4ddfef2018-01-16 07:37:58 -08001039}
1040
Eric Laurente8c8b432018-10-17 10:08:02 -07001041int AudioPolicyService::UidPolicy::getUidState(uid_t uid) {
1042 if (isServiceUid(uid)) {
1043 return ActivityManager::PROCESS_STATE_TOP;
1044 }
1045 checkRegistered();
1046 {
1047 Mutex::Autolock _l(mLock);
1048 auto overrideIter = mOverrideUids.find(uid);
1049 if (overrideIter != mOverrideUids.end()) {
1050 if (overrideIter->second.first) {
1051 if (overrideIter->second.second != ActivityManager::PROCESS_STATE_UNKNOWN) {
1052 return overrideIter->second.second;
1053 } else {
1054 auto cacheIter = mCachedUids.find(uid);
1055 if (cacheIter != mCachedUids.end()) {
1056 return cacheIter->second.second;
1057 }
1058 }
1059 }
1060 return ActivityManager::PROCESS_STATE_UNKNOWN;
1061 }
1062 // In an absense of the ActivityManager, assume everything to be active.
1063 if (!mObserverRegistered) {
1064 return ActivityManager::PROCESS_STATE_TOP;
1065 }
1066 auto cacheIter = mCachedUids.find(uid);
1067 if (cacheIter != mCachedUids.end()) {
1068 if (cacheIter->second.first) {
1069 return cacheIter->second.second;
1070 } else {
1071 return ActivityManager::PROCESS_STATE_UNKNOWN;
1072 }
1073 }
1074 }
1075 ActivityManager am;
Hui Yu12c7ec72020-05-04 17:40:52 +00001076 bool active = am.isUidActive(uid, String16("audioserver"));
Eric Laurente8c8b432018-10-17 10:08:02 -07001077 int state = ActivityManager::PROCESS_STATE_UNKNOWN;
1078 if (active) {
1079 state = am.getUidProcessState(uid, String16("audioserver"));
1080 }
1081 {
1082 Mutex::Autolock _l(mLock);
1083 mCachedUids.insert(std::pair<uid_t,
1084 std::pair<bool, int>>(uid, std::pair<bool, int>(active, state)));
1085 }
Eric Laurent4eb58f12018-12-07 16:41:02 -08001086
Eric Laurente8c8b432018-10-17 10:08:02 -07001087 return state;
1088}
1089
Mikhail Naganoveae73eb2018-04-03 16:57:36 -07001090void AudioPolicyService::UidPolicy::onUidActive(uid_t uid) {
Eric Laurente8c8b432018-10-17 10:08:02 -07001091 updateUid(&mCachedUids, uid, true, ActivityManager::PROCESS_STATE_UNKNOWN, true);
Mikhail Naganoveae73eb2018-04-03 16:57:36 -07001092}
1093
1094void AudioPolicyService::UidPolicy::onUidGone(uid_t uid, __unused bool disabled) {
Eric Laurente8c8b432018-10-17 10:08:02 -07001095 updateUid(&mCachedUids, uid, false, ActivityManager::PROCESS_STATE_UNKNOWN, false);
Mikhail Naganoveae73eb2018-04-03 16:57:36 -07001096}
1097
1098void AudioPolicyService::UidPolicy::onUidIdle(uid_t uid, __unused bool disabled) {
Eric Laurente8c8b432018-10-17 10:08:02 -07001099 updateUid(&mCachedUids, uid, false, ActivityManager::PROCESS_STATE_UNKNOWN, true);
Mikhail Naganoveae73eb2018-04-03 16:57:36 -07001100}
1101
Eric Laurente8c8b432018-10-17 10:08:02 -07001102void AudioPolicyService::UidPolicy::onUidStateChanged(uid_t uid,
1103 int32_t procState,
Hui Yu13ad0eb2019-09-09 10:27:07 -07001104 int64_t procStateSeq __unused,
1105 int32_t capability __unused) {
Eric Laurente8c8b432018-10-17 10:08:02 -07001106 if (procState != ActivityManager::PROCESS_STATE_UNKNOWN) {
1107 updateUid(&mCachedUids, uid, true, procState, true);
Mikhail Naganoveae73eb2018-04-03 16:57:36 -07001108 }
1109}
1110
1111void AudioPolicyService::UidPolicy::updateOverrideUid(uid_t uid, bool active, bool insert) {
Eric Laurente8c8b432018-10-17 10:08:02 -07001112 updateUid(&mOverrideUids, uid, active, ActivityManager::PROCESS_STATE_UNKNOWN, insert);
1113}
1114
1115void AudioPolicyService::UidPolicy::notifyService() {
1116 sp<AudioPolicyService> service = mService.promote();
1117 if (service != nullptr) {
1118 service->updateUidStates();
Mikhail Naganoveae73eb2018-04-03 16:57:36 -07001119 }
1120}
1121
Eric Laurente8c8b432018-10-17 10:08:02 -07001122void AudioPolicyService::UidPolicy::updateUid(std::unordered_map<uid_t,
1123 std::pair<bool, int>> *uids,
1124 uid_t uid,
1125 bool active,
1126 int state,
1127 bool insert) {
1128 if (isServiceUid(uid)) {
1129 return;
1130 }
1131 bool wasActive = isUidActive(uid);
1132 int previousState = getUidState(uid);
Mikhail Naganoveae73eb2018-04-03 16:57:36 -07001133 {
1134 Mutex::Autolock _l(mLock);
Eric Laurente8c8b432018-10-17 10:08:02 -07001135 updateUidLocked(uids, uid, active, state, insert);
Mikhail Naganoveae73eb2018-04-03 16:57:36 -07001136 }
Eric Laurente8c8b432018-10-17 10:08:02 -07001137 if (wasActive != isUidActive(uid) || state != previousState) {
1138 notifyService();
1139 }
Mikhail Naganoveae73eb2018-04-03 16:57:36 -07001140}
1141
Eric Laurente8c8b432018-10-17 10:08:02 -07001142void AudioPolicyService::UidPolicy::updateUidLocked(std::unordered_map<uid_t,
1143 std::pair<bool, int>> *uids,
1144 uid_t uid,
1145 bool active,
1146 int state,
1147 bool insert) {
Mikhail Naganoveae73eb2018-04-03 16:57:36 -07001148 auto it = uids->find(uid);
1149 if (it != uids->end()) {
Mikhail Naganoveae73eb2018-04-03 16:57:36 -07001150 if (insert) {
Eric Laurente8c8b432018-10-17 10:08:02 -07001151 if (state == ActivityManager::PROCESS_STATE_UNKNOWN) {
1152 it->second.first = active;
1153 }
1154 if (it->second.first) {
1155 it->second.second = state;
1156 } else {
1157 it->second.second = ActivityManager::PROCESS_STATE_UNKNOWN;
1158 }
Mikhail Naganoveae73eb2018-04-03 16:57:36 -07001159 } else {
1160 uids->erase(it);
1161 }
Eric Laurente8c8b432018-10-17 10:08:02 -07001162 } else if (insert && (state == ActivityManager::PROCESS_STATE_UNKNOWN)) {
1163 uids->insert(std::pair<uid_t, std::pair<bool, int>>(uid,
1164 std::pair<bool, int>(active, state)));
Mikhail Naganoveae73eb2018-04-03 16:57:36 -07001165 }
Svet Ganovf4ddfef2018-01-16 07:37:58 -08001166}
Mathias Agopian65ab4712010-07-14 17:59:35 -07001167
Eric Laurent4eb58f12018-12-07 16:41:02 -08001168bool AudioPolicyService::UidPolicy::isA11yOnTop() {
1169 for (const auto &uid : mCachedUids) {
Eric Laurent47670c92019-08-28 16:59:05 -07001170 if (!isA11yUid(uid.first)) {
Eric Laurent4eb58f12018-12-07 16:41:02 -08001171 continue;
1172 }
Amith Yamasanibcbb3002019-01-23 13:53:33 -08001173 if (uid.second.second >= ActivityManager::PROCESS_STATE_TOP
1174 && uid.second.second <= ActivityManager::PROCESS_STATE_BOUND_FOREGROUND_SERVICE) {
Eric Laurent4eb58f12018-12-07 16:41:02 -08001175 return true;
1176 }
1177 }
1178 return false;
1179}
1180
Eric Laurentb78763e2018-10-17 10:08:02 -07001181bool AudioPolicyService::UidPolicy::isA11yUid(uid_t uid)
1182{
1183 std::vector<uid_t>::iterator it = find(mA11yUids.begin(), mA11yUids.end(), uid);
1184 return it != mA11yUids.end();
1185}
1186
Michael Groovercfd28302018-12-11 19:16:46 -08001187// ----------- AudioPolicyService::SensorPrivacyService implementation ----------
1188void AudioPolicyService::SensorPrivacyPolicy::registerSelf() {
1189 SensorPrivacyManager spm;
1190 mSensorPrivacyEnabled = spm.isSensorPrivacyEnabled();
1191 spm.addSensorPrivacyListener(this);
1192}
1193
1194void AudioPolicyService::SensorPrivacyPolicy::unregisterSelf() {
1195 SensorPrivacyManager spm;
1196 spm.removeSensorPrivacyListener(this);
1197}
1198
1199bool AudioPolicyService::SensorPrivacyPolicy::isSensorPrivacyEnabled() {
1200 return mSensorPrivacyEnabled;
1201}
1202
1203binder::Status AudioPolicyService::SensorPrivacyPolicy::onSensorPrivacyChanged(bool enabled) {
1204 mSensorPrivacyEnabled = enabled;
1205 sp<AudioPolicyService> service = mService.promote();
1206 if (service != nullptr) {
1207 service->updateUidStates();
1208 }
1209 return binder::Status::ok();
1210}
1211
Mathias Agopian65ab4712010-07-14 17:59:35 -07001212// ----------- AudioPolicyService::AudioCommandThread implementation ----------
1213
Eric Laurentbfb1b832013-01-07 09:53:42 -08001214AudioPolicyService::AudioCommandThread::AudioCommandThread(String8 name,
1215 const wp<AudioPolicyService>& service)
1216 : Thread(false), mName(name), mService(service)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001217{
Mathias Agopian65ab4712010-07-14 17:59:35 -07001218}
1219
1220
1221AudioPolicyService::AudioCommandThread::~AudioCommandThread()
1222{
Eric Laurentbfb1b832013-01-07 09:53:42 -08001223 if (!mAudioCommands.isEmpty()) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07001224 release_wake_lock(mName.string());
1225 }
1226 mAudioCommands.clear();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001227}
1228
1229void AudioPolicyService::AudioCommandThread::onFirstRef()
1230{
Eric Laurentbfb1b832013-01-07 09:53:42 -08001231 run(mName.string(), ANDROID_PRIORITY_AUDIO);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001232}
1233
1234bool AudioPolicyService::AudioCommandThread::threadLoop()
1235{
Eric Laurentd7eda8d2016-02-02 17:18:39 -08001236 nsecs_t waitTime = -1;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001237
1238 mLock.lock();
1239 while (!exitPending())
1240 {
Eric Laurent59a89232014-06-08 14:14:17 -07001241 sp<AudioPolicyService> svc;
1242 while (!mAudioCommands.isEmpty() && !exitPending()) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07001243 nsecs_t curTime = systemTime();
1244 // commands are sorted by increasing time stamp: execute them from index 0 and up
1245 if (mAudioCommands[0]->mTime <= curTime) {
Eric Laurent0ede8922014-05-09 18:04:42 -07001246 sp<AudioCommand> command = mAudioCommands[0];
Mathias Agopian65ab4712010-07-14 17:59:35 -07001247 mAudioCommands.removeAt(0);
Eric Laurent0ede8922014-05-09 18:04:42 -07001248 mLastCommand = command;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001249
1250 switch (command->mCommand) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07001251 case SET_VOLUME: {
Eric Laurent0ede8922014-05-09 18:04:42 -07001252 VolumeData *data = (VolumeData *)command->mParam.get();
Steve Block3856b092011-10-20 11:56:00 +01001253 ALOGV("AudioCommandThread() processing set volume stream %d, \
Eric Laurentde070132010-07-13 04:45:46 -07001254 volume %f, output %d", data->mStream, data->mVolume, data->mIO);
Andy Hungfe726a62018-09-27 15:17:25 -07001255 mLock.unlock();
Eric Laurentde070132010-07-13 04:45:46 -07001256 command->mStatus = AudioSystem::setStreamVolume(data->mStream,
1257 data->mVolume,
1258 data->mIO);
Andy Hungfe726a62018-09-27 15:17:25 -07001259 mLock.lock();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001260 }break;
1261 case SET_PARAMETERS: {
Eric Laurent0ede8922014-05-09 18:04:42 -07001262 ParametersData *data = (ParametersData *)command->mParam.get();
Glenn Kastene53b9ea2012-03-12 16:29:55 -07001263 ALOGV("AudioCommandThread() processing set parameters string %s, io %d",
1264 data->mKeyValuePairs.string(), data->mIO);
Andy Hungfe726a62018-09-27 15:17:25 -07001265 mLock.unlock();
Glenn Kastene53b9ea2012-03-12 16:29:55 -07001266 command->mStatus = AudioSystem::setParameters(data->mIO, data->mKeyValuePairs);
Andy Hungfe726a62018-09-27 15:17:25 -07001267 mLock.lock();
Glenn Kastene53b9ea2012-03-12 16:29:55 -07001268 }break;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001269 case SET_VOICE_VOLUME: {
Eric Laurent0ede8922014-05-09 18:04:42 -07001270 VoiceVolumeData *data = (VoiceVolumeData *)command->mParam.get();
Steve Block3856b092011-10-20 11:56:00 +01001271 ALOGV("AudioCommandThread() processing set voice volume volume %f",
Eric Laurentde070132010-07-13 04:45:46 -07001272 data->mVolume);
Andy Hungfe726a62018-09-27 15:17:25 -07001273 mLock.unlock();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001274 command->mStatus = AudioSystem::setVoiceVolume(data->mVolume);
Andy Hungfe726a62018-09-27 15:17:25 -07001275 mLock.lock();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001276 }break;
Eric Laurentbfb1b832013-01-07 09:53:42 -08001277 case STOP_OUTPUT: {
Eric Laurent0ede8922014-05-09 18:04:42 -07001278 StopOutputData *data = (StopOutputData *)command->mParam.get();
Eric Laurentd7fe0862018-07-14 16:48:01 -07001279 ALOGV("AudioCommandThread() processing stop output portId %d",
1280 data->mPortId);
Eric Laurent59a89232014-06-08 14:14:17 -07001281 svc = mService.promote();
Eric Laurentbfb1b832013-01-07 09:53:42 -08001282 if (svc == 0) {
1283 break;
1284 }
1285 mLock.unlock();
Eric Laurentd7fe0862018-07-14 16:48:01 -07001286 svc->doStopOutput(data->mPortId);
Eric Laurentbfb1b832013-01-07 09:53:42 -08001287 mLock.lock();
Eric Laurentbfb1b832013-01-07 09:53:42 -08001288 }break;
1289 case RELEASE_OUTPUT: {
Eric Laurent0ede8922014-05-09 18:04:42 -07001290 ReleaseOutputData *data = (ReleaseOutputData *)command->mParam.get();
Eric Laurentd7fe0862018-07-14 16:48:01 -07001291 ALOGV("AudioCommandThread() processing release output portId %d",
1292 data->mPortId);
Eric Laurent59a89232014-06-08 14:14:17 -07001293 svc = mService.promote();
Eric Laurentbfb1b832013-01-07 09:53:42 -08001294 if (svc == 0) {
1295 break;
1296 }
1297 mLock.unlock();
Eric Laurentd7fe0862018-07-14 16:48:01 -07001298 svc->doReleaseOutput(data->mPortId);
Eric Laurentbfb1b832013-01-07 09:53:42 -08001299 mLock.lock();
Eric Laurentbfb1b832013-01-07 09:53:42 -08001300 }break;
Eric Laurent951f4552014-05-20 10:48:17 -07001301 case CREATE_AUDIO_PATCH: {
1302 CreateAudioPatchData *data = (CreateAudioPatchData *)command->mParam.get();
1303 ALOGV("AudioCommandThread() processing create audio patch");
1304 sp<IAudioFlinger> af = AudioSystem::get_audio_flinger();
1305 if (af == 0) {
1306 command->mStatus = PERMISSION_DENIED;
1307 } else {
Andy Hungfe726a62018-09-27 15:17:25 -07001308 mLock.unlock();
Eric Laurent951f4552014-05-20 10:48:17 -07001309 command->mStatus = af->createAudioPatch(&data->mPatch, &data->mHandle);
Andy Hungfe726a62018-09-27 15:17:25 -07001310 mLock.lock();
Eric Laurent951f4552014-05-20 10:48:17 -07001311 }
1312 } break;
1313 case RELEASE_AUDIO_PATCH: {
1314 ReleaseAudioPatchData *data = (ReleaseAudioPatchData *)command->mParam.get();
1315 ALOGV("AudioCommandThread() processing release audio patch");
1316 sp<IAudioFlinger> af = AudioSystem::get_audio_flinger();
1317 if (af == 0) {
1318 command->mStatus = PERMISSION_DENIED;
1319 } else {
Andy Hungfe726a62018-09-27 15:17:25 -07001320 mLock.unlock();
Eric Laurent951f4552014-05-20 10:48:17 -07001321 command->mStatus = af->releaseAudioPatch(data->mHandle);
Andy Hungfe726a62018-09-27 15:17:25 -07001322 mLock.lock();
Eric Laurent951f4552014-05-20 10:48:17 -07001323 }
1324 } break;
Eric Laurentb52c1522014-05-20 11:27:36 -07001325 case UPDATE_AUDIOPORT_LIST: {
1326 ALOGV("AudioCommandThread() processing update audio port list");
Eric Laurent59a89232014-06-08 14:14:17 -07001327 svc = mService.promote();
Eric Laurentb52c1522014-05-20 11:27:36 -07001328 if (svc == 0) {
1329 break;
1330 }
1331 mLock.unlock();
1332 svc->doOnAudioPortListUpdate();
1333 mLock.lock();
1334 }break;
1335 case UPDATE_AUDIOPATCH_LIST: {
1336 ALOGV("AudioCommandThread() processing update audio patch list");
Eric Laurent59a89232014-06-08 14:14:17 -07001337 svc = mService.promote();
Eric Laurentb52c1522014-05-20 11:27:36 -07001338 if (svc == 0) {
1339 break;
1340 }
1341 mLock.unlock();
1342 svc->doOnAudioPatchListUpdate();
1343 mLock.lock();
1344 }break;
François Gaffiecfe17322018-11-07 13:41:29 +01001345 case CHANGED_AUDIOVOLUMEGROUP: {
1346 AudioVolumeGroupData *data =
1347 static_cast<AudioVolumeGroupData *>(command->mParam.get());
1348 ALOGV("AudioCommandThread() processing update audio volume group");
1349 svc = mService.promote();
1350 if (svc == 0) {
1351 break;
1352 }
1353 mLock.unlock();
1354 svc->doOnAudioVolumeGroupChanged(data->mGroup, data->mFlags);
1355 mLock.lock();
1356 }break;
Eric Laurente1715a42014-05-20 11:30:42 -07001357 case SET_AUDIOPORT_CONFIG: {
1358 SetAudioPortConfigData *data = (SetAudioPortConfigData *)command->mParam.get();
1359 ALOGV("AudioCommandThread() processing set port config");
1360 sp<IAudioFlinger> af = AudioSystem::get_audio_flinger();
1361 if (af == 0) {
1362 command->mStatus = PERMISSION_DENIED;
1363 } else {
Andy Hungfe726a62018-09-27 15:17:25 -07001364 mLock.unlock();
Eric Laurente1715a42014-05-20 11:30:42 -07001365 command->mStatus = af->setAudioPortConfig(&data->mConfig);
Andy Hungfe726a62018-09-27 15:17:25 -07001366 mLock.lock();
Eric Laurente1715a42014-05-20 11:30:42 -07001367 }
1368 } break;
Jean-Michel Trivide801052015-04-14 19:10:14 -07001369 case DYN_POLICY_MIX_STATE_UPDATE: {
1370 DynPolicyMixStateUpdateData *data =
1371 (DynPolicyMixStateUpdateData *)command->mParam.get();
Jean-Michel Trivide801052015-04-14 19:10:14 -07001372 ALOGV("AudioCommandThread() processing dyn policy mix state update %s %d",
1373 data->mRegId.string(), data->mState);
1374 svc = mService.promote();
1375 if (svc == 0) {
1376 break;
1377 }
1378 mLock.unlock();
1379 svc->doOnDynamicPolicyMixStateUpdate(data->mRegId, data->mState);
1380 mLock.lock();
1381 } break;
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -08001382 case RECORDING_CONFIGURATION_UPDATE: {
1383 RecordingConfigurationUpdateData *data =
1384 (RecordingConfigurationUpdateData *)command->mParam.get();
1385 ALOGV("AudioCommandThread() processing recording configuration update");
1386 svc = mService.promote();
1387 if (svc == 0) {
1388 break;
1389 }
1390 mLock.unlock();
Jean-Michel Triviac4e4292016-12-22 11:39:31 -08001391 svc->doOnRecordingConfigurationUpdate(data->mEvent, &data->mClientInfo,
Eric Laurenta9f86652018-11-28 17:23:11 -08001392 &data->mClientConfig, data->mClientEffects,
1393 &data->mDeviceConfig, data->mEffects,
1394 data->mPatchHandle, data->mSource);
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -08001395 mLock.lock();
1396 } break;
Eric Laurentb20cf7d2019-04-05 19:37:34 -07001397 case SET_EFFECT_SUSPENDED: {
1398 SetEffectSuspendedData *data = (SetEffectSuspendedData *)command->mParam.get();
1399 ALOGV("AudioCommandThread() processing set effect suspended");
1400 sp<IAudioFlinger> af = AudioSystem::get_audio_flinger();
1401 if (af != 0) {
1402 mLock.unlock();
1403 af->setEffectSuspended(data->mEffectId, data->mSessionId, data->mSuspended);
1404 mLock.lock();
1405 }
1406 } break;
Mikhail Naganov88b30d22020-03-09 19:43:13 +00001407 case AUDIO_MODULES_UPDATE: {
1408 ALOGV("AudioCommandThread() processing audio modules update");
1409 svc = mService.promote();
1410 if (svc == 0) {
1411 break;
1412 }
1413 mLock.unlock();
1414 svc->doOnNewAudioModulesAvailable();
1415 mLock.lock();
1416 } break;
Eric Laurentb20cf7d2019-04-05 19:37:34 -07001417
Mathias Agopian65ab4712010-07-14 17:59:35 -07001418 default:
Steve Block5ff1dd52012-01-05 23:22:43 +00001419 ALOGW("AudioCommandThread() unknown command %d", command->mCommand);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001420 }
Eric Laurent0ede8922014-05-09 18:04:42 -07001421 {
1422 Mutex::Autolock _l(command->mLock);
1423 if (command->mWaitStatus) {
1424 command->mWaitStatus = false;
1425 command->mCond.signal();
1426 }
1427 }
Eric Laurentd7eda8d2016-02-02 17:18:39 -08001428 waitTime = -1;
Zach Janga754b4f2015-10-27 01:29:34 +00001429 // release mLock before releasing strong reference on the service as
1430 // AudioPolicyService destructor calls AudioCommandThread::exit() which
1431 // acquires mLock.
1432 mLock.unlock();
1433 svc.clear();
1434 mLock.lock();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001435 } else {
1436 waitTime = mAudioCommands[0]->mTime - curTime;
1437 break;
1438 }
1439 }
Zach Janga754b4f2015-10-27 01:29:34 +00001440
1441 // release delayed commands wake lock if the queue is empty
1442 if (mAudioCommands.isEmpty()) {
Ricardo Garcia05f2fdc2014-07-24 15:48:24 -07001443 release_wake_lock(mName.string());
Zach Janga754b4f2015-10-27 01:29:34 +00001444 }
1445
1446 // At this stage we have either an empty command queue or the first command in the queue
1447 // has a finite delay. So unless we are exiting it is safe to wait.
1448 if (!exitPending()) {
Eric Laurent59a89232014-06-08 14:14:17 -07001449 ALOGV("AudioCommandThread() going to sleep");
Eric Laurentd7eda8d2016-02-02 17:18:39 -08001450 if (waitTime == -1) {
1451 mWaitWorkCV.wait(mLock);
1452 } else {
1453 mWaitWorkCV.waitRelative(mLock, waitTime);
1454 }
Eric Laurent59a89232014-06-08 14:14:17 -07001455 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001456 }
Ricardo Garcia05f2fdc2014-07-24 15:48:24 -07001457 // release delayed commands wake lock before quitting
1458 if (!mAudioCommands.isEmpty()) {
1459 release_wake_lock(mName.string());
1460 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001461 mLock.unlock();
1462 return false;
1463}
1464
1465status_t AudioPolicyService::AudioCommandThread::dump(int fd)
1466{
1467 const size_t SIZE = 256;
1468 char buffer[SIZE];
1469 String8 result;
1470
1471 snprintf(buffer, SIZE, "AudioCommandThread %p Dump\n", this);
1472 result.append(buffer);
1473 write(fd, result.string(), result.size());
1474
Mikhail Naganov12b716c2020-04-30 22:37:43 +00001475 const bool locked = dumpTryLock(mLock);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001476 if (!locked) {
1477 String8 result2(kCmdDeadlockedString);
1478 write(fd, result2.string(), result2.size());
1479 }
1480
1481 snprintf(buffer, SIZE, "- Commands:\n");
1482 result = String8(buffer);
1483 result.append(" Command Time Wait pParam\n");
Glenn Kasten8d6a2442012-02-08 14:04:28 -08001484 for (size_t i = 0; i < mAudioCommands.size(); i++) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07001485 mAudioCommands[i]->dump(buffer, SIZE);
1486 result.append(buffer);
1487 }
1488 result.append(" Last Command\n");
Eric Laurent0ede8922014-05-09 18:04:42 -07001489 if (mLastCommand != 0) {
1490 mLastCommand->dump(buffer, SIZE);
1491 result.append(buffer);
1492 } else {
1493 result.append(" none\n");
1494 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001495
1496 write(fd, result.string(), result.size());
1497
Mikhail Naganov12b716c2020-04-30 22:37:43 +00001498 dumpReleaseLock(mLock, locked);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001499
1500 return NO_ERROR;
1501}
1502
Glenn Kastenfff6d712012-01-12 16:38:12 -08001503status_t AudioPolicyService::AudioCommandThread::volumeCommand(audio_stream_type_t stream,
Eric Laurentde070132010-07-13 04:45:46 -07001504 float volume,
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001505 audio_io_handle_t output,
Eric Laurentde070132010-07-13 04:45:46 -07001506 int delayMs)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001507{
Eric Laurent0ede8922014-05-09 18:04:42 -07001508 sp<AudioCommand> command = new AudioCommand();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001509 command->mCommand = SET_VOLUME;
Eric Laurent0ede8922014-05-09 18:04:42 -07001510 sp<VolumeData> data = new VolumeData();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001511 data->mStream = stream;
1512 data->mVolume = volume;
1513 data->mIO = output;
1514 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 volume stream %d, volume %f, output %d",
Eric Laurentde070132010-07-13 04:45:46 -07001517 stream, volume, output);
Eric Laurent0ede8922014-05-09 18:04:42 -07001518 return sendCommand(command, delayMs);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001519}
1520
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001521status_t AudioPolicyService::AudioCommandThread::parametersCommand(audio_io_handle_t ioHandle,
Dima Zavinfce7a472011-04-19 22:30:36 -07001522 const char *keyValuePairs,
Eric Laurentde070132010-07-13 04:45:46 -07001523 int delayMs)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001524{
Eric Laurent0ede8922014-05-09 18:04:42 -07001525 sp<AudioCommand> command = new AudioCommand();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001526 command->mCommand = SET_PARAMETERS;
Eric Laurent0ede8922014-05-09 18:04:42 -07001527 sp<ParametersData> data = new ParametersData();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001528 data->mIO = ioHandle;
Dima Zavinfce7a472011-04-19 22:30:36 -07001529 data->mKeyValuePairs = String8(keyValuePairs);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001530 command->mParam = data;
Eric Laurent0ede8922014-05-09 18:04:42 -07001531 command->mWaitStatus = true;
Steve Block3856b092011-10-20 11:56:00 +01001532 ALOGV("AudioCommandThread() adding set parameter string %s, io %d ,delay %d",
Dima Zavinfce7a472011-04-19 22:30:36 -07001533 keyValuePairs, ioHandle, delayMs);
Eric Laurent0ede8922014-05-09 18:04:42 -07001534 return sendCommand(command, delayMs);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001535}
1536
1537status_t AudioPolicyService::AudioCommandThread::voiceVolumeCommand(float volume, int delayMs)
1538{
Eric Laurent0ede8922014-05-09 18:04:42 -07001539 sp<AudioCommand> command = new AudioCommand();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001540 command->mCommand = SET_VOICE_VOLUME;
Eric Laurent0ede8922014-05-09 18:04:42 -07001541 sp<VoiceVolumeData> data = new VoiceVolumeData();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001542 data->mVolume = volume;
1543 command->mParam = data;
Eric Laurent0ede8922014-05-09 18:04:42 -07001544 command->mWaitStatus = true;
Steve Block3856b092011-10-20 11:56:00 +01001545 ALOGV("AudioCommandThread() adding set voice volume volume %f", volume);
Eric Laurent0ede8922014-05-09 18:04:42 -07001546 return sendCommand(command, delayMs);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001547}
1548
Eric Laurentb20cf7d2019-04-05 19:37:34 -07001549void AudioPolicyService::AudioCommandThread::setEffectSuspendedCommand(int effectId,
1550 audio_session_t sessionId,
1551 bool suspended)
1552{
1553 sp<AudioCommand> command = new AudioCommand();
1554 command->mCommand = SET_EFFECT_SUSPENDED;
1555 sp<SetEffectSuspendedData> data = new SetEffectSuspendedData();
1556 data->mEffectId = effectId;
1557 data->mSessionId = sessionId;
1558 data->mSuspended = suspended;
1559 command->mParam = data;
1560 ALOGV("AudioCommandThread() adding set suspended effectId %d sessionId %d suspended %d",
1561 effectId, sessionId, suspended);
1562 sendCommand(command);
1563}
1564
1565
Eric Laurentd7fe0862018-07-14 16:48:01 -07001566void AudioPolicyService::AudioCommandThread::stopOutputCommand(audio_port_handle_t portId)
Eric Laurentbfb1b832013-01-07 09:53:42 -08001567{
Eric Laurent0ede8922014-05-09 18:04:42 -07001568 sp<AudioCommand> command = new AudioCommand();
Eric Laurentbfb1b832013-01-07 09:53:42 -08001569 command->mCommand = STOP_OUTPUT;
Eric Laurent0ede8922014-05-09 18:04:42 -07001570 sp<StopOutputData> data = new StopOutputData();
Eric Laurentd7fe0862018-07-14 16:48:01 -07001571 data->mPortId = portId;
Jesper Tragardh48412dc2014-03-24 14:12:43 +01001572 command->mParam = data;
Eric Laurentd7fe0862018-07-14 16:48:01 -07001573 ALOGV("AudioCommandThread() adding stop output portId %d", portId);
Eric Laurent0ede8922014-05-09 18:04:42 -07001574 sendCommand(command);
Eric Laurentbfb1b832013-01-07 09:53:42 -08001575}
1576
Eric Laurentd7fe0862018-07-14 16:48:01 -07001577void AudioPolicyService::AudioCommandThread::releaseOutputCommand(audio_port_handle_t portId)
Eric Laurentbfb1b832013-01-07 09:53:42 -08001578{
Eric Laurent0ede8922014-05-09 18:04:42 -07001579 sp<AudioCommand> command = new AudioCommand();
Eric Laurentbfb1b832013-01-07 09:53:42 -08001580 command->mCommand = RELEASE_OUTPUT;
Eric Laurent0ede8922014-05-09 18:04:42 -07001581 sp<ReleaseOutputData> data = new ReleaseOutputData();
Eric Laurentd7fe0862018-07-14 16:48:01 -07001582 data->mPortId = portId;
Jesper Tragardh48412dc2014-03-24 14:12:43 +01001583 command->mParam = data;
Eric Laurentd7fe0862018-07-14 16:48:01 -07001584 ALOGV("AudioCommandThread() adding release output portId %d", portId);
Eric Laurent0ede8922014-05-09 18:04:42 -07001585 sendCommand(command);
1586}
1587
Eric Laurent951f4552014-05-20 10:48:17 -07001588status_t AudioPolicyService::AudioCommandThread::createAudioPatchCommand(
1589 const struct audio_patch *patch,
1590 audio_patch_handle_t *handle,
1591 int delayMs)
1592{
1593 status_t status = NO_ERROR;
1594
1595 sp<AudioCommand> command = new AudioCommand();
1596 command->mCommand = CREATE_AUDIO_PATCH;
1597 CreateAudioPatchData *data = new CreateAudioPatchData();
1598 data->mPatch = *patch;
1599 data->mHandle = *handle;
1600 command->mParam = data;
1601 command->mWaitStatus = true;
1602 ALOGV("AudioCommandThread() adding create patch delay %d", delayMs);
1603 status = sendCommand(command, delayMs);
1604 if (status == NO_ERROR) {
1605 *handle = data->mHandle;
1606 }
1607 return status;
1608}
1609
1610status_t AudioPolicyService::AudioCommandThread::releaseAudioPatchCommand(audio_patch_handle_t handle,
1611 int delayMs)
1612{
1613 sp<AudioCommand> command = new AudioCommand();
1614 command->mCommand = RELEASE_AUDIO_PATCH;
1615 ReleaseAudioPatchData *data = new ReleaseAudioPatchData();
1616 data->mHandle = handle;
1617 command->mParam = data;
1618 command->mWaitStatus = true;
1619 ALOGV("AudioCommandThread() adding release patch delay %d", delayMs);
1620 return sendCommand(command, delayMs);
1621}
1622
Eric Laurentb52c1522014-05-20 11:27:36 -07001623void AudioPolicyService::AudioCommandThread::updateAudioPortListCommand()
1624{
1625 sp<AudioCommand> command = new AudioCommand();
1626 command->mCommand = UPDATE_AUDIOPORT_LIST;
1627 ALOGV("AudioCommandThread() adding update audio port list");
1628 sendCommand(command);
1629}
1630
1631void AudioPolicyService::AudioCommandThread::updateAudioPatchListCommand()
1632{
1633 sp<AudioCommand>command = new AudioCommand();
1634 command->mCommand = UPDATE_AUDIOPATCH_LIST;
1635 ALOGV("AudioCommandThread() adding update audio patch list");
1636 sendCommand(command);
1637}
1638
François Gaffiecfe17322018-11-07 13:41:29 +01001639void AudioPolicyService::AudioCommandThread::changeAudioVolumeGroupCommand(volume_group_t group,
1640 int flags)
1641{
1642 sp<AudioCommand>command = new AudioCommand();
1643 command->mCommand = CHANGED_AUDIOVOLUMEGROUP;
1644 AudioVolumeGroupData *data= new AudioVolumeGroupData();
1645 data->mGroup = group;
1646 data->mFlags = flags;
1647 command->mParam = data;
1648 ALOGV("AudioCommandThread() adding audio volume group changed");
1649 sendCommand(command);
1650}
1651
Eric Laurente1715a42014-05-20 11:30:42 -07001652status_t AudioPolicyService::AudioCommandThread::setAudioPortConfigCommand(
1653 const struct audio_port_config *config, int delayMs)
1654{
1655 sp<AudioCommand> command = new AudioCommand();
1656 command->mCommand = SET_AUDIOPORT_CONFIG;
1657 SetAudioPortConfigData *data = new SetAudioPortConfigData();
1658 data->mConfig = *config;
1659 command->mParam = data;
1660 command->mWaitStatus = true;
1661 ALOGV("AudioCommandThread() adding set port config delay %d", delayMs);
1662 return sendCommand(command, delayMs);
1663}
1664
Jean-Michel Trivide801052015-04-14 19:10:14 -07001665void AudioPolicyService::AudioCommandThread::dynamicPolicyMixStateUpdateCommand(
Chih-Hung Hsiehe964d4e2016-08-09 14:31:32 -07001666 const String8& regId, int32_t state)
Jean-Michel Trivide801052015-04-14 19:10:14 -07001667{
1668 sp<AudioCommand> command = new AudioCommand();
1669 command->mCommand = DYN_POLICY_MIX_STATE_UPDATE;
1670 DynPolicyMixStateUpdateData *data = new DynPolicyMixStateUpdateData();
1671 data->mRegId = regId;
1672 data->mState = state;
1673 command->mParam = data;
1674 ALOGV("AudioCommandThread() sending dynamic policy mix (id=%s) state update to %d",
1675 regId.string(), state);
1676 sendCommand(command);
1677}
1678
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -08001679void AudioPolicyService::AudioCommandThread::recordingConfigurationUpdateCommand(
Eric Laurenta9f86652018-11-28 17:23:11 -08001680 int event,
1681 const record_client_info_t *clientInfo,
1682 const audio_config_base_t *clientConfig,
1683 std::vector<effect_descriptor_t> clientEffects,
1684 const audio_config_base_t *deviceConfig,
1685 std::vector<effect_descriptor_t> effects,
1686 audio_patch_handle_t patchHandle,
1687 audio_source_t source)
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -08001688{
1689 sp<AudioCommand>command = new AudioCommand();
1690 command->mCommand = RECORDING_CONFIGURATION_UPDATE;
1691 RecordingConfigurationUpdateData *data = new RecordingConfigurationUpdateData();
1692 data->mEvent = event;
Jean-Michel Triviac4e4292016-12-22 11:39:31 -08001693 data->mClientInfo = *clientInfo;
Jean-Michel Trivi7281aa92016-02-17 15:33:40 -08001694 data->mClientConfig = *clientConfig;
Eric Laurenta9f86652018-11-28 17:23:11 -08001695 data->mClientEffects = clientEffects;
Jean-Michel Trivi7281aa92016-02-17 15:33:40 -08001696 data->mDeviceConfig = *deviceConfig;
Eric Laurenta9f86652018-11-28 17:23:11 -08001697 data->mEffects = effects;
Jean-Michel Trivi8c7cf3b2016-02-25 17:08:24 -08001698 data->mPatchHandle = patchHandle;
Eric Laurenta9f86652018-11-28 17:23:11 -08001699 data->mSource = source;
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -08001700 command->mParam = data;
Jean-Michel Triviac4e4292016-12-22 11:39:31 -08001701 ALOGV("AudioCommandThread() adding recording configuration update event %d, source %d uid %u",
1702 event, clientInfo->source, clientInfo->uid);
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -08001703 sendCommand(command);
1704}
1705
Mikhail Naganov88b30d22020-03-09 19:43:13 +00001706void AudioPolicyService::AudioCommandThread::audioModulesUpdateCommand()
1707{
1708 sp<AudioCommand> command = new AudioCommand();
1709 command->mCommand = AUDIO_MODULES_UPDATE;
1710 sendCommand(command);
1711}
1712
Eric Laurent0ede8922014-05-09 18:04:42 -07001713status_t AudioPolicyService::AudioCommandThread::sendCommand(sp<AudioCommand>& command, int delayMs)
1714{
1715 {
1716 Mutex::Autolock _l(mLock);
1717 insertCommand_l(command, delayMs);
1718 mWaitWorkCV.signal();
1719 }
1720 Mutex::Autolock _l(command->mLock);
1721 while (command->mWaitStatus) {
1722 nsecs_t timeOutNs = kAudioCommandTimeoutNs + milliseconds(delayMs);
1723 if (command->mCond.waitRelative(command->mLock, timeOutNs) != NO_ERROR) {
1724 command->mStatus = TIMED_OUT;
1725 command->mWaitStatus = false;
1726 }
1727 }
1728 return command->mStatus;
Eric Laurentbfb1b832013-01-07 09:53:42 -08001729}
1730
Mathias Agopian65ab4712010-07-14 17:59:35 -07001731// insertCommand_l() must be called with mLock held
Eric Laurent0ede8922014-05-09 18:04:42 -07001732void AudioPolicyService::AudioCommandThread::insertCommand_l(sp<AudioCommand>& command, int delayMs)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001733{
Glenn Kasten8d6a2442012-02-08 14:04:28 -08001734 ssize_t i; // not size_t because i will count down to -1
Eric Laurent0ede8922014-05-09 18:04:42 -07001735 Vector < sp<AudioCommand> > removedCommands;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001736 command->mTime = systemTime() + milliseconds(delayMs);
1737
1738 // acquire wake lock to make sure delayed commands are processed
Eric Laurentbfb1b832013-01-07 09:53:42 -08001739 if (mAudioCommands.isEmpty()) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07001740 acquire_wake_lock(PARTIAL_WAKE_LOCK, mName.string());
1741 }
1742
1743 // check same pending commands with later time stamps and eliminate them
Ivan Lozano5ff158f2017-10-30 09:06:24 -07001744 for (i = (ssize_t)mAudioCommands.size()-1; i >= 0; i--) {
Eric Laurent0ede8922014-05-09 18:04:42 -07001745 sp<AudioCommand> command2 = mAudioCommands[i];
Mathias Agopian65ab4712010-07-14 17:59:35 -07001746 // commands are sorted by increasing time stamp: no need to scan the rest of mAudioCommands
1747 if (command2->mTime <= command->mTime) break;
Eric Laurente45b48a2014-09-04 16:40:57 -07001748
1749 // create audio patch or release audio patch commands are equivalent
1750 // with regard to filtering
1751 if ((command->mCommand == CREATE_AUDIO_PATCH) ||
1752 (command->mCommand == RELEASE_AUDIO_PATCH)) {
1753 if ((command2->mCommand != CREATE_AUDIO_PATCH) &&
1754 (command2->mCommand != RELEASE_AUDIO_PATCH)) {
1755 continue;
1756 }
1757 } else if (command2->mCommand != command->mCommand) continue;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001758
1759 switch (command->mCommand) {
1760 case SET_PARAMETERS: {
Eric Laurent0ede8922014-05-09 18:04:42 -07001761 ParametersData *data = (ParametersData *)command->mParam.get();
1762 ParametersData *data2 = (ParametersData *)command2->mParam.get();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001763 if (data->mIO != data2->mIO) break;
Steve Block3856b092011-10-20 11:56:00 +01001764 ALOGV("Comparing parameter command %s to new command %s",
Eric Laurentde070132010-07-13 04:45:46 -07001765 data2->mKeyValuePairs.string(), data->mKeyValuePairs.string());
Mathias Agopian65ab4712010-07-14 17:59:35 -07001766 AudioParameter param = AudioParameter(data->mKeyValuePairs);
1767 AudioParameter param2 = AudioParameter(data2->mKeyValuePairs);
1768 for (size_t j = 0; j < param.size(); j++) {
Glenn Kastene53b9ea2012-03-12 16:29:55 -07001769 String8 key;
1770 String8 value;
1771 param.getAt(j, key, value);
1772 for (size_t k = 0; k < param2.size(); k++) {
1773 String8 key2;
1774 String8 value2;
1775 param2.getAt(k, key2, value2);
1776 if (key2 == key) {
1777 param2.remove(key2);
1778 ALOGV("Filtering out parameter %s", key2.string());
1779 break;
1780 }
1781 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001782 }
1783 // if all keys have been filtered out, remove the command.
1784 // otherwise, update the key value pairs
1785 if (param2.size() == 0) {
1786 removedCommands.add(command2);
1787 } else {
1788 data2->mKeyValuePairs = param2.toString();
1789 }
Eric Laurent21e54562013-09-23 12:08:05 -07001790 command->mTime = command2->mTime;
1791 // force delayMs to non 0 so that code below does not request to wait for
1792 // command status as the command is now delayed
1793 delayMs = 1;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001794 } break;
1795
1796 case SET_VOLUME: {
Eric Laurent0ede8922014-05-09 18:04:42 -07001797 VolumeData *data = (VolumeData *)command->mParam.get();
1798 VolumeData *data2 = (VolumeData *)command2->mParam.get();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001799 if (data->mIO != data2->mIO) break;
1800 if (data->mStream != data2->mStream) break;
Steve Block3856b092011-10-20 11:56:00 +01001801 ALOGV("Filtering out volume command on output %d for stream %d",
Eric Laurentde070132010-07-13 04:45:46 -07001802 data->mIO, data->mStream);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001803 removedCommands.add(command2);
Eric Laurent21e54562013-09-23 12:08:05 -07001804 command->mTime = command2->mTime;
1805 // force delayMs to non 0 so that code below does not request to wait for
1806 // command status as the command is now delayed
1807 delayMs = 1;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001808 } break;
Eric Laurente45b48a2014-09-04 16:40:57 -07001809
Eric Laurentbaf35fe2016-07-27 15:36:53 -07001810 case SET_VOICE_VOLUME: {
1811 VoiceVolumeData *data = (VoiceVolumeData *)command->mParam.get();
1812 VoiceVolumeData *data2 = (VoiceVolumeData *)command2->mParam.get();
1813 ALOGV("Filtering out voice volume command value %f replaced by %f",
1814 data2->mVolume, data->mVolume);
1815 removedCommands.add(command2);
1816 command->mTime = command2->mTime;
1817 // force delayMs to non 0 so that code below does not request to wait for
1818 // command status as the command is now delayed
1819 delayMs = 1;
1820 } break;
1821
Eric Laurente45b48a2014-09-04 16:40:57 -07001822 case CREATE_AUDIO_PATCH:
1823 case RELEASE_AUDIO_PATCH: {
1824 audio_patch_handle_t handle;
Haynes Mathew Georgea2d4a6d2014-10-13 13:05:22 -07001825 struct audio_patch patch;
Eric Laurente45b48a2014-09-04 16:40:57 -07001826 if (command->mCommand == CREATE_AUDIO_PATCH) {
1827 handle = ((CreateAudioPatchData *)command->mParam.get())->mHandle;
Haynes Mathew Georgea2d4a6d2014-10-13 13:05:22 -07001828 patch = ((CreateAudioPatchData *)command->mParam.get())->mPatch;
Eric Laurente45b48a2014-09-04 16:40:57 -07001829 } else {
1830 handle = ((ReleaseAudioPatchData *)command->mParam.get())->mHandle;
Mikhail Naganov7be71d22018-05-23 16:51:46 -07001831 memset(&patch, 0, sizeof(patch));
Eric Laurente45b48a2014-09-04 16:40:57 -07001832 }
1833 audio_patch_handle_t handle2;
Haynes Mathew Georgea2d4a6d2014-10-13 13:05:22 -07001834 struct audio_patch patch2;
Eric Laurente45b48a2014-09-04 16:40:57 -07001835 if (command2->mCommand == CREATE_AUDIO_PATCH) {
1836 handle2 = ((CreateAudioPatchData *)command2->mParam.get())->mHandle;
Haynes Mathew Georgea2d4a6d2014-10-13 13:05:22 -07001837 patch2 = ((CreateAudioPatchData *)command2->mParam.get())->mPatch;
Eric Laurente45b48a2014-09-04 16:40:57 -07001838 } else {
1839 handle2 = ((ReleaseAudioPatchData *)command2->mParam.get())->mHandle;
Glenn Kastenf60b6b62015-07-06 10:53:26 -07001840 memset(&patch2, 0, sizeof(patch2));
Eric Laurente45b48a2014-09-04 16:40:57 -07001841 }
1842 if (handle != handle2) break;
Haynes Mathew Georgea2d4a6d2014-10-13 13:05:22 -07001843 /* Filter CREATE_AUDIO_PATCH commands only when they are issued for
1844 same output. */
1845 if( (command->mCommand == CREATE_AUDIO_PATCH) &&
1846 (command2->mCommand == CREATE_AUDIO_PATCH) ) {
1847 bool isOutputDiff = false;
1848 if (patch.num_sources == patch2.num_sources) {
1849 for (unsigned count = 0; count < patch.num_sources; count++) {
1850 if (patch.sources[count].id != patch2.sources[count].id) {
1851 isOutputDiff = true;
1852 break;
1853 }
1854 }
1855 if (isOutputDiff)
1856 break;
1857 }
1858 }
Eric Laurente45b48a2014-09-04 16:40:57 -07001859 ALOGV("Filtering out %s audio patch command for handle %d",
1860 (command->mCommand == CREATE_AUDIO_PATCH) ? "create" : "release", handle);
1861 removedCommands.add(command2);
1862 command->mTime = command2->mTime;
1863 // force delayMs to non 0 so that code below does not request to wait for
1864 // command status as the command is now delayed
1865 delayMs = 1;
1866 } break;
1867
Jean-Michel Trivide801052015-04-14 19:10:14 -07001868 case DYN_POLICY_MIX_STATE_UPDATE: {
1869
1870 } break;
1871
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -08001872 case RECORDING_CONFIGURATION_UPDATE: {
1873
1874 } break;
1875
Mathias Agopian65ab4712010-07-14 17:59:35 -07001876 default:
1877 break;
1878 }
1879 }
1880
1881 // remove filtered commands
1882 for (size_t j = 0; j < removedCommands.size(); j++) {
1883 // removed commands always have time stamps greater than current command
1884 for (size_t k = i + 1; k < mAudioCommands.size(); k++) {
Eric Laurent0ede8922014-05-09 18:04:42 -07001885 if (mAudioCommands[k].get() == removedCommands[j].get()) {
Steve Block3856b092011-10-20 11:56:00 +01001886 ALOGV("suppressing command: %d", mAudioCommands[k]->mCommand);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001887 mAudioCommands.removeAt(k);
1888 break;
1889 }
1890 }
1891 }
1892 removedCommands.clear();
1893
Eric Laurentaa79bef2015-01-15 14:33:51 -08001894 // Disable wait for status if delay is not 0.
1895 // Except for create audio patch command because the returned patch handle
1896 // is needed by audio policy manager
1897 if (delayMs != 0 && command->mCommand != CREATE_AUDIO_PATCH) {
Eric Laurentcec4abb2012-07-03 12:23:02 -07001898 command->mWaitStatus = false;
1899 }
Eric Laurentcec4abb2012-07-03 12:23:02 -07001900
Mathias Agopian65ab4712010-07-14 17:59:35 -07001901 // insert command at the right place according to its time stamp
Eric Laurent1e693b52014-07-09 15:03:28 -07001902 ALOGV("inserting command: %d at index %zd, num commands %zu",
1903 command->mCommand, i+1, mAudioCommands.size());
Mathias Agopian65ab4712010-07-14 17:59:35 -07001904 mAudioCommands.insertAt(command, i + 1);
1905}
1906
1907void AudioPolicyService::AudioCommandThread::exit()
1908{
Steve Block3856b092011-10-20 11:56:00 +01001909 ALOGV("AudioCommandThread::exit");
Mathias Agopian65ab4712010-07-14 17:59:35 -07001910 {
1911 AutoMutex _l(mLock);
1912 requestExit();
1913 mWaitWorkCV.signal();
1914 }
Zach Janga754b4f2015-10-27 01:29:34 +00001915 // Note that we can call it from the thread loop if all other references have been released
1916 // but it will safely return WOULD_BLOCK in this case
Mathias Agopian65ab4712010-07-14 17:59:35 -07001917 requestExitAndWait();
1918}
1919
1920void AudioPolicyService::AudioCommandThread::AudioCommand::dump(char* buffer, size_t size)
1921{
1922 snprintf(buffer, size, " %02d %06d.%03d %01u %p\n",
1923 mCommand,
1924 (int)ns2s(mTime),
1925 (int)ns2ms(mTime)%1000,
1926 mWaitStatus,
Eric Laurent0ede8922014-05-09 18:04:42 -07001927 mParam.get());
Mathias Agopian65ab4712010-07-14 17:59:35 -07001928}
1929
Dima Zavinfce7a472011-04-19 22:30:36 -07001930/******* helpers for the service_ops callbacks defined below *********/
1931void AudioPolicyService::setParameters(audio_io_handle_t ioHandle,
1932 const char *keyValuePairs,
1933 int delayMs)
1934{
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001935 mAudioCommandThread->parametersCommand(ioHandle, keyValuePairs,
Dima Zavinfce7a472011-04-19 22:30:36 -07001936 delayMs);
1937}
1938
1939int AudioPolicyService::setStreamVolume(audio_stream_type_t stream,
1940 float volume,
1941 audio_io_handle_t output,
1942 int delayMs)
1943{
Glenn Kastenfff6d712012-01-12 16:38:12 -08001944 return (int)mAudioCommandThread->volumeCommand(stream, volume,
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001945 output, delayMs);
Dima Zavinfce7a472011-04-19 22:30:36 -07001946}
1947
Dima Zavinfce7a472011-04-19 22:30:36 -07001948int AudioPolicyService::setVoiceVolume(float volume, int delayMs)
1949{
1950 return (int)mAudioCommandThread->voiceVolumeCommand(volume, delayMs);
1951}
1952
Eric Laurentb20cf7d2019-04-05 19:37:34 -07001953void AudioPolicyService::setEffectSuspended(int effectId,
1954 audio_session_t sessionId,
1955 bool suspended)
1956{
1957 mAudioCommandThread->setEffectSuspendedCommand(effectId, sessionId, suspended);
1958}
1959
Mikhail Naganov88b30d22020-03-09 19:43:13 +00001960void AudioPolicyService::onNewAudioModulesAvailable()
1961{
Mikhail Naganovd0e2c742020-03-25 15:59:59 -07001962 mOutputCommandThread->audioModulesUpdateCommand();
Mikhail Naganov88b30d22020-03-09 19:43:13 +00001963}
1964
Eric Laurentb20cf7d2019-04-05 19:37:34 -07001965
Dima Zavinfce7a472011-04-19 22:30:36 -07001966extern "C" {
Eric Laurent2d388ec2014-03-07 13:25:54 -08001967audio_module_handle_t aps_load_hw_module(void *service __unused,
1968 const char *name);
1969audio_io_handle_t aps_open_output(void *service __unused,
Eric Laurenta4c5a552012-03-29 10:12:40 -07001970 audio_devices_t *pDevices,
1971 uint32_t *pSamplingRate,
1972 audio_format_t *pFormat,
1973 audio_channel_mask_t *pChannelMask,
1974 uint32_t *pLatencyMs,
Eric Laurent2d388ec2014-03-07 13:25:54 -08001975 audio_output_flags_t flags);
Eric Laurenta4c5a552012-03-29 10:12:40 -07001976
Eric Laurent2d388ec2014-03-07 13:25:54 -08001977audio_io_handle_t aps_open_output_on_module(void *service __unused,
Eric Laurenta4c5a552012-03-29 10:12:40 -07001978 audio_module_handle_t module,
1979 audio_devices_t *pDevices,
1980 uint32_t *pSamplingRate,
1981 audio_format_t *pFormat,
1982 audio_channel_mask_t *pChannelMask,
1983 uint32_t *pLatencyMs,
Richard Fitzgeraldad3af332013-03-25 16:54:37 +00001984 audio_output_flags_t flags,
Eric Laurent2d388ec2014-03-07 13:25:54 -08001985 const audio_offload_info_t *offloadInfo);
1986audio_io_handle_t aps_open_dup_output(void *service __unused,
Dima Zavinfce7a472011-04-19 22:30:36 -07001987 audio_io_handle_t output1,
Eric Laurent2d388ec2014-03-07 13:25:54 -08001988 audio_io_handle_t output2);
1989int aps_close_output(void *service __unused, audio_io_handle_t output);
1990int aps_suspend_output(void *service __unused, audio_io_handle_t output);
1991int aps_restore_output(void *service __unused, audio_io_handle_t output);
1992audio_io_handle_t aps_open_input(void *service __unused,
Eric Laurenta4c5a552012-03-29 10:12:40 -07001993 audio_devices_t *pDevices,
1994 uint32_t *pSamplingRate,
1995 audio_format_t *pFormat,
1996 audio_channel_mask_t *pChannelMask,
Eric Laurent2d388ec2014-03-07 13:25:54 -08001997 audio_in_acoustics_t acoustics __unused);
1998audio_io_handle_t aps_open_input_on_module(void *service __unused,
Eric Laurenta4c5a552012-03-29 10:12:40 -07001999 audio_module_handle_t module,
2000 audio_devices_t *pDevices,
2001 uint32_t *pSamplingRate,
2002 audio_format_t *pFormat,
Eric Laurent2d388ec2014-03-07 13:25:54 -08002003 audio_channel_mask_t *pChannelMask);
2004int aps_close_input(void *service __unused, audio_io_handle_t input);
2005int aps_invalidate_stream(void *service __unused, audio_stream_type_t stream);
Glenn Kastend848eb42016-03-08 13:42:11 -08002006int aps_move_effects(void *service __unused, audio_session_t session,
Dima Zavinfce7a472011-04-19 22:30:36 -07002007 audio_io_handle_t src_output,
Eric Laurent2d388ec2014-03-07 13:25:54 -08002008 audio_io_handle_t dst_output);
2009char * aps_get_parameters(void *service __unused, audio_io_handle_t io_handle,
2010 const char *keys);
2011void aps_set_parameters(void *service, audio_io_handle_t io_handle,
2012 const char *kv_pairs, int delay_ms);
2013int aps_set_stream_volume(void *service, audio_stream_type_t stream,
Dima Zavinfce7a472011-04-19 22:30:36 -07002014 float volume, audio_io_handle_t output,
Eric Laurent2d388ec2014-03-07 13:25:54 -08002015 int delay_ms);
Eric Laurent2d388ec2014-03-07 13:25:54 -08002016int aps_set_voice_volume(void *service, float volume, int delay_ms);
2017};
Dima Zavinfce7a472011-04-19 22:30:36 -07002018
Mikhail Naganov1b2a7942017-12-08 10:18:09 -08002019} // namespace android