blob: 86994edff3de7e8196bb1bf59e8cbf6a1dd7d00d [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>
Jaideep Sharmaba9053b2021-01-25 21:24:26 +053026#include <dlfcn.h>
Mikhail Naganov959e2d02019-03-28 11:08:19 -070027
28#include <audio_utils/clock.h>
Mathias Agopian65ab4712010-07-14 17:59:35 -070029#include <binder/IServiceManager.h>
30#include <utils/Log.h>
31#include <cutils/properties.h>
32#include <binder/IPCThreadState.h>
Svet Ganovf4ddfef2018-01-16 07:37:58 -080033#include <binder/PermissionController.h>
34#include <binder/IResultReceiver.h>
Mathias Agopian65ab4712010-07-14 17:59:35 -070035#include <utils/String16.h>
36#include <utils/threads.h>
37#include "AudioPolicyService.h"
Mathias Agopian65ab4712010-07-14 17:59:35 -070038#include <hardware_legacy/power.h>
Ytai Ben-Tsvi7e7a79d2020-12-15 16:48:16 -080039#include <media/AidlConversion.h>
Eric Laurent7c7f10b2011-06-17 21:29:58 -070040#include <media/AudioEffect.h>
Chih-Hung Hsiehc84d9d22014-11-14 13:33:34 -080041#include <media/AudioParameter.h>
Andy Hungab7ef302018-05-15 19:35:29 -070042#include <mediautils/ServiceUtilities.h>
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -080043#include <mediautils/TimeCheck.h>
Michael Groovercfd28302018-12-11 19:16:46 -080044#include <sensorprivacy/SensorPrivacyManager.h>
Mathias Agopian65ab4712010-07-14 17:59:35 -070045
Dima Zavin64760242011-05-11 14:15:23 -070046#include <system/audio.h>
Dima Zavin7394a4f2011-06-13 18:16:26 -070047#include <system/audio_policy.h>
Jaideep Sharmaba9053b2021-01-25 21:24:26 +053048#include <AudioPolicyManager.h>
Mikhail Naganov61a4fac2016-10-13 14:44:18 -070049
Mathias Agopian65ab4712010-07-14 17:59:35 -070050namespace android {
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -080051using binder::Status;
Mathias Agopian65ab4712010-07-14 17:59:35 -070052
Glenn Kasten8dad0e32012-01-09 08:41:22 -080053static const char kDeadlockedString[] = "AudioPolicyService may be deadlocked\n";
54static const char kCmdDeadlockedString[] = "AudioPolicyService command thread may be deadlocked\n";
Jaideep Sharmaba9053b2021-01-25 21:24:26 +053055static const char kAudioPolicyManagerCustomPath[] = "libaudiopolicymanagercustom.so";
Mathias Agopian65ab4712010-07-14 17:59:35 -070056
Mikhail Naganov959e2d02019-03-28 11:08:19 -070057static const int kDumpLockTimeoutNs = 1 * NANOS_PER_SECOND;
Mathias Agopian65ab4712010-07-14 17:59:35 -070058
Eric Laurent0ede8922014-05-09 18:04:42 -070059static const nsecs_t kAudioCommandTimeoutNs = seconds(3); // 3 seconds
Christer Fletcher5fa8c4b2013-01-18 15:27:03 +010060
Svet Ganovf4ddfef2018-01-16 07:37:58 -080061static const String16 sManageAudioPolicyPermission("android.permission.MANAGE_AUDIO_POLICY");
Dima Zavinfce7a472011-04-19 22:30:36 -070062
Mathias Agopian65ab4712010-07-14 17:59:35 -070063// ----------------------------------------------------------------------------
64
Jaideep Sharmaba9053b2021-01-25 21:24:26 +053065static AudioPolicyInterface* createAudioPolicyManager(AudioPolicyClientInterface *clientInterface)
66{
67 AudioPolicyManager *apm = new AudioPolicyManager(clientInterface);
68 status_t status = apm->initialize();
69 if (status != NO_ERROR) {
70 delete apm;
71 apm = nullptr;
72 }
73 return apm;
74}
75
76static void destroyAudioPolicyManager(AudioPolicyInterface *interface)
77{
78 delete interface;
79}
80// ----------------------------------------------------------------------------
81
Mathias Agopian65ab4712010-07-14 17:59:35 -070082AudioPolicyService::AudioPolicyService()
Ytai Ben-Tsvi85093d52020-03-26 09:41:15 -070083 : BnAudioPolicyService(),
Ytai Ben-Tsvi85093d52020-03-26 09:41:15 -070084 mAudioPolicyManager(NULL),
85 mAudioPolicyClient(NULL),
86 mPhoneState(AUDIO_MODE_INVALID),
Jaideep Sharmaba9053b2021-01-25 21:24:26 +053087 mCaptureStateNotifier(false),
88 mCreateAudioPolicyManager(createAudioPolicyManager),
89 mDestroyAudioPolicyManager(destroyAudioPolicyManager) {
90}
91
92void AudioPolicyService::loadAudioPolicyManager()
93{
94 mLibraryHandle = dlopen(kAudioPolicyManagerCustomPath, RTLD_NOW);
95 if (mLibraryHandle != nullptr) {
96 ALOGI("%s loading %s", __func__, kAudioPolicyManagerCustomPath);
97 mCreateAudioPolicyManager = reinterpret_cast<CreateAudioPolicyManagerInstance>
98 (dlsym(mLibraryHandle, "createAudioPolicyManager"));
99 const char *lastError = dlerror();
100 ALOGW_IF(mCreateAudioPolicyManager == nullptr, "%s createAudioPolicyManager is null %s",
101 __func__, lastError != nullptr ? lastError : "no error");
102
103 mDestroyAudioPolicyManager = reinterpret_cast<DestroyAudioPolicyManagerInstance>(
104 dlsym(mLibraryHandle, "destroyAudioPolicyManager"));
105 lastError = dlerror();
106 ALOGW_IF(mDestroyAudioPolicyManager == nullptr, "%s destroyAudioPolicyManager is null %s",
107 __func__, lastError != nullptr ? lastError : "no error");
108 if (mCreateAudioPolicyManager == nullptr || mDestroyAudioPolicyManager == nullptr){
109 unloadAudioPolicyManager();
110 LOG_ALWAYS_FATAL("could not find audiopolicymanager interface methods");
111 }
112 }
Eric Laurentf5ada6e2014-10-09 17:49:00 -0700113}
114
115void AudioPolicyService::onFirstRef()
116{
Eric Laurent8b1e80b2014-10-07 09:08:47 -0700117 {
118 Mutex::Autolock _l(mLock);
Eric Laurent93575202011-01-18 18:39:02 -0800119
Eric Laurent8b1e80b2014-10-07 09:08:47 -0700120 // start audio commands thread
121 mAudioCommandThread = new AudioCommandThread(String8("ApmAudio"), this);
122 // start output activity command thread
123 mOutputCommandThread = new AudioCommandThread(String8("ApmOutput"), this);
Eric Laurentdce54a12014-03-10 12:19:46 -0700124
Eric Laurent8b1e80b2014-10-07 09:08:47 -0700125 mAudioPolicyClient = new AudioPolicyClient(this);
Jaideep Sharmaba9053b2021-01-25 21:24:26 +0530126
127 loadAudioPolicyManager();
128 mAudioPolicyManager = mCreateAudioPolicyManager(mAudioPolicyClient);
Eric Laurent8b1e80b2014-10-07 09:08:47 -0700129 }
Eric Laurentd66d7a12021-07-13 13:35:32 +0200130
bryant_liuba2b4392014-06-11 16:49:30 +0800131 // load audio processing modules
Mikhail Naganov12b716c2020-04-30 22:37:43 +0000132 sp<AudioPolicyEffects> audioPolicyEffects = new AudioPolicyEffects();
133 sp<UidPolicy> uidPolicy = new UidPolicy(this);
134 sp<SensorPrivacyPolicy> sensorPrivacyPolicy = new SensorPrivacyPolicy(this);
Eric Laurent8b1e80b2014-10-07 09:08:47 -0700135 {
136 Mutex::Autolock _l(mLock);
137 mAudioPolicyEffects = audioPolicyEffects;
Mikhail Naganov12b716c2020-04-30 22:37:43 +0000138 mUidPolicy = uidPolicy;
139 mSensorPrivacyPolicy = sensorPrivacyPolicy;
Eric Laurent8b1e80b2014-10-07 09:08:47 -0700140 }
Mikhail Naganov12b716c2020-04-30 22:37:43 +0000141 uidPolicy->registerSelf();
142 sensorPrivacyPolicy->registerSelf();
Eric Laurentd66d7a12021-07-13 13:35:32 +0200143
Eric Laurent81dd0f52021-07-05 11:54:40 +0200144 // Create spatializer if supported
145 const audio_attributes_t attr = attributes_initializer(AUDIO_USAGE_MEDIA);
146 AudioDeviceTypeAddrVector devices;
147 bool hasSpatializer = mAudioPolicyManager->canBeSpatialized(&attr, nullptr, devices);
148 if (hasSpatializer) {
149 mSpatializer = Spatializer::create(this);
150 }
Eric Laurentd66d7a12021-07-13 13:35:32 +0200151 AudioSystem::audioPolicyReady();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700152}
153
Jaideep Sharmaba9053b2021-01-25 21:24:26 +0530154void AudioPolicyService::unloadAudioPolicyManager()
155{
156 ALOGV("%s ", __func__);
157 if (mLibraryHandle != nullptr) {
158 dlclose(mLibraryHandle);
159 }
160 mLibraryHandle = nullptr;
161 mCreateAudioPolicyManager = nullptr;
162 mDestroyAudioPolicyManager = nullptr;
163}
164
Mathias Agopian65ab4712010-07-14 17:59:35 -0700165AudioPolicyService::~AudioPolicyService()
166{
Mathias Agopian65ab4712010-07-14 17:59:35 -0700167 mAudioCommandThread->exit();
Eric Laurent657ff612014-05-07 11:58:24 -0700168 mOutputCommandThread->exit();
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700169
Jaideep Sharmaba9053b2021-01-25 21:24:26 +0530170 mDestroyAudioPolicyManager(mAudioPolicyManager);
171 unloadAudioPolicyManager();
172
Eric Laurentdce54a12014-03-10 12:19:46 -0700173 delete mAudioPolicyClient;
Eric Laurentb52c1522014-05-20 11:27:36 -0700174
175 mNotificationClients.clear();
bryant_liuba2b4392014-06-11 16:49:30 +0800176 mAudioPolicyEffects.clear();
Svet Ganovf4ddfef2018-01-16 07:37:58 -0800177
178 mUidPolicy->unregisterSelf();
Michael Groovercfd28302018-12-11 19:16:46 -0800179 mSensorPrivacyPolicy->unregisterSelf();
Mikhail Naganov12b716c2020-04-30 22:37:43 +0000180
181 mUidPolicy.clear();
Michael Groovercfd28302018-12-11 19:16:46 -0800182 mSensorPrivacyPolicy.clear();
Eric Laurentb52c1522014-05-20 11:27:36 -0700183}
184
185// A notification client is always registered by AudioSystem when the client process
186// connects to AudioPolicyService.
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800187Status AudioPolicyService::registerClient(const sp<media::IAudioPolicyServiceClient>& client)
Eric Laurentb52c1522014-05-20 11:27:36 -0700188{
Eric Laurent12590252015-08-21 18:40:20 -0700189 if (client == 0) {
190 ALOGW("%s got NULL client", __FUNCTION__);
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800191 return Status::ok();
Eric Laurent12590252015-08-21 18:40:20 -0700192 }
Eric Laurent0ebd5f92014-11-19 19:04:52 -0800193 Mutex::Autolock _l(mNotificationClientsLock);
Eric Laurentb52c1522014-05-20 11:27:36 -0700194
195 uid_t uid = IPCThreadState::self()->getCallingUid();
luochaojiang908c7d72018-06-21 14:58:04 +0800196 pid_t pid = IPCThreadState::self()->getCallingPid();
197 int64_t token = ((int64_t)uid<<32) | pid;
198
199 if (mNotificationClients.indexOfKey(token) < 0) {
Eric Laurentb52c1522014-05-20 11:27:36 -0700200 sp<NotificationClient> notificationClient = new NotificationClient(this,
201 client,
luochaojiang908c7d72018-06-21 14:58:04 +0800202 uid,
203 pid);
204 ALOGV("registerClient() client %p, uid %d pid %d", client.get(), uid, pid);
Eric Laurentb52c1522014-05-20 11:27:36 -0700205
luochaojiang908c7d72018-06-21 14:58:04 +0800206 mNotificationClients.add(token, notificationClient);
Eric Laurentb52c1522014-05-20 11:27:36 -0700207
Marco Nelissenf8880202014-11-14 07:58:25 -0800208 sp<IBinder> binder = IInterface::asBinder(client);
Eric Laurentb52c1522014-05-20 11:27:36 -0700209 binder->linkToDeath(notificationClient);
210 }
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800211 return Status::ok();
Eric Laurentb52c1522014-05-20 11:27:36 -0700212}
213
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800214Status AudioPolicyService::setAudioPortCallbacksEnabled(bool enabled)
Eric Laurente8726fe2015-06-26 09:39:24 -0700215{
216 Mutex::Autolock _l(mNotificationClientsLock);
217
218 uid_t uid = IPCThreadState::self()->getCallingUid();
luochaojiang908c7d72018-06-21 14:58:04 +0800219 pid_t pid = IPCThreadState::self()->getCallingPid();
220 int64_t token = ((int64_t)uid<<32) | pid;
221
222 if (mNotificationClients.indexOfKey(token) < 0) {
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800223 return Status::ok();
Eric Laurente8726fe2015-06-26 09:39:24 -0700224 }
luochaojiang908c7d72018-06-21 14:58:04 +0800225 mNotificationClients.valueFor(token)->setAudioPortCallbacksEnabled(enabled);
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800226 return Status::ok();
Eric Laurente8726fe2015-06-26 09:39:24 -0700227}
228
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800229Status AudioPolicyService::setAudioVolumeGroupCallbacksEnabled(bool enabled)
François Gaffiecfe17322018-11-07 13:41:29 +0100230{
231 Mutex::Autolock _l(mNotificationClientsLock);
232
233 uid_t uid = IPCThreadState::self()->getCallingUid();
234 pid_t pid = IPCThreadState::self()->getCallingPid();
235 int64_t token = ((int64_t)uid<<32) | pid;
236
237 if (mNotificationClients.indexOfKey(token) < 0) {
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800238 return Status::ok();
François Gaffiecfe17322018-11-07 13:41:29 +0100239 }
240 mNotificationClients.valueFor(token)->setAudioVolumeGroupCallbacksEnabled(enabled);
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800241 return Status::ok();
François Gaffiecfe17322018-11-07 13:41:29 +0100242}
243
Eric Laurentb52c1522014-05-20 11:27:36 -0700244// removeNotificationClient() is called when the client process dies.
luochaojiang908c7d72018-06-21 14:58:04 +0800245void AudioPolicyService::removeNotificationClient(uid_t uid, pid_t pid)
Eric Laurentb52c1522014-05-20 11:27:36 -0700246{
Mikhail Naganov12b716c2020-04-30 22:37:43 +0000247 bool hasSameUid = false;
Eric Laurent0ebd5f92014-11-19 19:04:52 -0800248 {
249 Mutex::Autolock _l(mNotificationClientsLock);
luochaojiang908c7d72018-06-21 14:58:04 +0800250 int64_t token = ((int64_t)uid<<32) | pid;
251 mNotificationClients.removeItem(token);
luochaojiang908c7d72018-06-21 14:58:04 +0800252 for (size_t i = 0; i < mNotificationClients.size(); i++) {
253 if (mNotificationClients.valueAt(i)->uid() == uid) {
254 hasSameUid = true;
255 break;
256 }
257 }
Mikhail Naganov12b716c2020-04-30 22:37:43 +0000258 }
259 {
260 Mutex::Autolock _l(mLock);
luochaojiang908c7d72018-06-21 14:58:04 +0800261 if (mAudioPolicyManager && !hasSameUid) {
Eric Laurent10b71232018-04-13 18:14:44 -0700262 // called from binder death notification: no need to clear caller identity
Eric Laurent8c7e6da2015-04-21 17:37:00 -0700263 mAudioPolicyManager->releaseResourcesForUid(uid);
Eric Laurentb52c1522014-05-20 11:27:36 -0700264 }
Eric Laurent0ebd5f92014-11-19 19:04:52 -0800265 }
Eric Laurentb52c1522014-05-20 11:27:36 -0700266}
267
268void AudioPolicyService::onAudioPortListUpdate()
269{
270 mOutputCommandThread->updateAudioPortListCommand();
271}
272
273void AudioPolicyService::doOnAudioPortListUpdate()
274{
Eric Laurent0ebd5f92014-11-19 19:04:52 -0800275 Mutex::Autolock _l(mNotificationClientsLock);
Eric Laurentb52c1522014-05-20 11:27:36 -0700276 for (size_t i = 0; i < mNotificationClients.size(); i++) {
277 mNotificationClients.valueAt(i)->onAudioPortListUpdate();
278 }
279}
280
281void AudioPolicyService::onAudioPatchListUpdate()
282{
283 mOutputCommandThread->updateAudioPatchListCommand();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700284}
285
Eric Laurentb52c1522014-05-20 11:27:36 -0700286void AudioPolicyService::doOnAudioPatchListUpdate()
287{
Eric Laurent0ebd5f92014-11-19 19:04:52 -0800288 Mutex::Autolock _l(mNotificationClientsLock);
Eric Laurentb52c1522014-05-20 11:27:36 -0700289 for (size_t i = 0; i < mNotificationClients.size(); i++) {
290 mNotificationClients.valueAt(i)->onAudioPatchListUpdate();
291 }
292}
293
François Gaffiecfe17322018-11-07 13:41:29 +0100294void AudioPolicyService::onAudioVolumeGroupChanged(volume_group_t group, int flags)
295{
296 mOutputCommandThread->changeAudioVolumeGroupCommand(group, flags);
297}
298
299void AudioPolicyService::doOnAudioVolumeGroupChanged(volume_group_t group, int flags)
300{
301 Mutex::Autolock _l(mNotificationClientsLock);
302 for (size_t i = 0; i < mNotificationClients.size(); i++) {
303 mNotificationClients.valueAt(i)->onAudioVolumeGroupChanged(group, flags);
304 }
305}
306
Chih-Hung Hsiehe964d4e2016-08-09 14:31:32 -0700307void AudioPolicyService::onDynamicPolicyMixStateUpdate(const String8& regId, int32_t state)
Jean-Michel Trivide801052015-04-14 19:10:14 -0700308{
309 ALOGV("AudioPolicyService::onDynamicPolicyMixStateUpdate(%s, %d)",
310 regId.string(), state);
311 mOutputCommandThread->dynamicPolicyMixStateUpdateCommand(regId, state);
312}
313
Chih-Hung Hsiehe964d4e2016-08-09 14:31:32 -0700314void AudioPolicyService::doOnDynamicPolicyMixStateUpdate(const String8& regId, int32_t state)
Jean-Michel Trivide801052015-04-14 19:10:14 -0700315{
316 Mutex::Autolock _l(mNotificationClientsLock);
317 for (size_t i = 0; i < mNotificationClients.size(); i++) {
318 mNotificationClients.valueAt(i)->onDynamicPolicyMixStateUpdate(regId, state);
319 }
320}
321
Eric Laurenta9f86652018-11-28 17:23:11 -0800322void AudioPolicyService::onRecordingConfigurationUpdate(
323 int event,
324 const record_client_info_t *clientInfo,
325 const audio_config_base_t *clientConfig,
326 std::vector<effect_descriptor_t> clientEffects,
327 const audio_config_base_t *deviceConfig,
328 std::vector<effect_descriptor_t> effects,
329 audio_patch_handle_t patchHandle,
330 audio_source_t source)
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -0800331{
Jean-Michel Triviac4e4292016-12-22 11:39:31 -0800332 mOutputCommandThread->recordingConfigurationUpdateCommand(event, clientInfo,
Eric Laurenta9f86652018-11-28 17:23:11 -0800333 clientConfig, clientEffects, deviceConfig, effects, patchHandle, source);
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -0800334}
335
Eric Laurenta9f86652018-11-28 17:23:11 -0800336void AudioPolicyService::doOnRecordingConfigurationUpdate(
337 int event,
338 const record_client_info_t *clientInfo,
339 const audio_config_base_t *clientConfig,
340 std::vector<effect_descriptor_t> clientEffects,
341 const audio_config_base_t *deviceConfig,
342 std::vector<effect_descriptor_t> effects,
343 audio_patch_handle_t patchHandle,
344 audio_source_t source)
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -0800345{
346 Mutex::Autolock _l(mNotificationClientsLock);
347 for (size_t i = 0; i < mNotificationClients.size(); i++) {
Jean-Michel Triviac4e4292016-12-22 11:39:31 -0800348 mNotificationClients.valueAt(i)->onRecordingConfigurationUpdate(event, clientInfo,
Eric Laurenta9f86652018-11-28 17:23:11 -0800349 clientConfig, clientEffects, deviceConfig, effects, patchHandle, source);
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -0800350 }
351}
352
Jean-Michel Trivi9a6b9ad2020-10-22 16:46:43 -0700353void AudioPolicyService::onRoutingUpdated()
354{
355 mOutputCommandThread->routingChangedCommand();
356}
357
358void AudioPolicyService::doOnRoutingUpdated()
359{
360 Mutex::Autolock _l(mNotificationClientsLock);
361 for (size_t i = 0; i < mNotificationClients.size(); i++) {
362 mNotificationClients.valueAt(i)->onRoutingUpdated();
363 }
364}
365
Eric Laurent81dd0f52021-07-05 11:54:40 +0200366void AudioPolicyService::onCheckSpatializer()
367{
368 Mutex::Autolock _l(mLock);
369 mOutputCommandThread->checkSpatializerCommand();
370}
371
372void AudioPolicyService::doOnCheckSpatializer()
373{
374 sp<Spatializer> spatializer;
375 {
376 Mutex::Autolock _l(mLock);
377 spatializer = mSpatializer;
378
379 if (spatializer != nullptr) {
380 audio_io_handle_t output = AUDIO_IO_HANDLE_NONE;
381 if (spatializer->getLevel() != media::SpatializationLevel::NONE
382 && spatializer->getOutput() == AUDIO_IO_HANDLE_NONE) {
383 const audio_attributes_t attr = attributes_initializer(AUDIO_USAGE_MEDIA);
384 audio_config_base_t config = spatializer->getAudioInConfig();
385 status_t status =
386 mAudioPolicyManager->getSpatializerOutput(&config, &attr, &output);
387 if (status != NO_ERROR || output == AUDIO_IO_HANDLE_NONE) {
388 return;
389 }
390 mLock.unlock();
391 status = spatializer->attachOutput(output);
392 mLock.lock();
393 if (status != NO_ERROR) {
394 mAudioPolicyManager->releaseSpatializerOutput(output);
395 }
396 } else if (spatializer->getLevel() == media::SpatializationLevel::NONE
397 && spatializer->getOutput() != AUDIO_IO_HANDLE_NONE) {
398 mLock.unlock();
399 output = spatializer->detachOutput();
400 mLock.lock();
401 if (output != AUDIO_IO_HANDLE_NONE) {
402 mAudioPolicyManager->releaseSpatializerOutput(output);
403 }
404 }
405 }
406 }
407}
408
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -0800409status_t AudioPolicyService::clientCreateAudioPatch(const struct audio_patch *patch,
410 audio_patch_handle_t *handle,
411 int delayMs)
412{
413 return mAudioCommandThread->createAudioPatchCommand(patch, handle, delayMs);
414}
415
416status_t AudioPolicyService::clientReleaseAudioPatch(audio_patch_handle_t handle,
417 int delayMs)
418{
419 return mAudioCommandThread->releaseAudioPatchCommand(handle, delayMs);
420}
421
Eric Laurente1715a42014-05-20 11:30:42 -0700422status_t AudioPolicyService::clientSetAudioPortConfig(const struct audio_port_config *config,
423 int delayMs)
424{
425 return mAudioCommandThread->setAudioPortConfigCommand(config, delayMs);
426}
427
Ytai Ben-Tsvi7e7a79d2020-12-15 16:48:16 -0800428AudioPolicyService::NotificationClient::NotificationClient(
429 const sp<AudioPolicyService>& service,
430 const sp<media::IAudioPolicyServiceClient>& client,
431 uid_t uid,
432 pid_t pid)
luochaojiang908c7d72018-06-21 14:58:04 +0800433 : mService(service), mUid(uid), mPid(pid), mAudioPolicyServiceClient(client),
François Gaffiecfe17322018-11-07 13:41:29 +0100434 mAudioPortCallbacksEnabled(false), mAudioVolumeGroupCallbacksEnabled(false)
Eric Laurentb52c1522014-05-20 11:27:36 -0700435{
436}
437
438AudioPolicyService::NotificationClient::~NotificationClient()
439{
440}
441
442void AudioPolicyService::NotificationClient::binderDied(const wp<IBinder>& who __unused)
443{
444 sp<NotificationClient> keep(this);
445 sp<AudioPolicyService> service = mService.promote();
446 if (service != 0) {
luochaojiang908c7d72018-06-21 14:58:04 +0800447 service->removeNotificationClient(mUid, mPid);
Eric Laurentb52c1522014-05-20 11:27:36 -0700448 }
449}
450
451void AudioPolicyService::NotificationClient::onAudioPortListUpdate()
452{
Eric Laurente8726fe2015-06-26 09:39:24 -0700453 if (mAudioPolicyServiceClient != 0 && mAudioPortCallbacksEnabled) {
Eric Laurentb52c1522014-05-20 11:27:36 -0700454 mAudioPolicyServiceClient->onAudioPortListUpdate();
455 }
456}
457
458void AudioPolicyService::NotificationClient::onAudioPatchListUpdate()
459{
Eric Laurente8726fe2015-06-26 09:39:24 -0700460 if (mAudioPolicyServiceClient != 0 && mAudioPortCallbacksEnabled) {
Eric Laurentb52c1522014-05-20 11:27:36 -0700461 mAudioPolicyServiceClient->onAudioPatchListUpdate();
462 }
463}
Eric Laurent57dae992011-07-24 13:36:09 -0700464
François Gaffiecfe17322018-11-07 13:41:29 +0100465void AudioPolicyService::NotificationClient::onAudioVolumeGroupChanged(volume_group_t group,
466 int flags)
467{
468 if (mAudioPolicyServiceClient != 0 && mAudioVolumeGroupCallbacksEnabled) {
469 mAudioPolicyServiceClient->onAudioVolumeGroupChanged(group, flags);
470 }
471}
472
473
Jean-Michel Trivide801052015-04-14 19:10:14 -0700474void AudioPolicyService::NotificationClient::onDynamicPolicyMixStateUpdate(
Chih-Hung Hsiehe964d4e2016-08-09 14:31:32 -0700475 const String8& regId, int32_t state)
Jean-Michel Trivide801052015-04-14 19:10:14 -0700476{
Andy Hung4ef19fa2018-05-15 19:35:29 -0700477 if (mAudioPolicyServiceClient != 0 && isServiceUid(mUid)) {
Ytai Ben-Tsvi7e7a79d2020-12-15 16:48:16 -0800478 mAudioPolicyServiceClient->onDynamicPolicyMixStateUpdate(
479 legacy2aidl_String8_string(regId).value(), state);
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -0800480 }
481}
482
483void AudioPolicyService::NotificationClient::onRecordingConfigurationUpdate(
Eric Laurenta9f86652018-11-28 17:23:11 -0800484 int event,
485 const record_client_info_t *clientInfo,
486 const audio_config_base_t *clientConfig,
487 std::vector<effect_descriptor_t> clientEffects,
488 const audio_config_base_t *deviceConfig,
489 std::vector<effect_descriptor_t> effects,
490 audio_patch_handle_t patchHandle,
491 audio_source_t source)
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -0800492{
Andy Hung4ef19fa2018-05-15 19:35:29 -0700493 if (mAudioPolicyServiceClient != 0 && isServiceUid(mUid)) {
Ytai Ben-Tsvi7e7a79d2020-12-15 16:48:16 -0800494 status_t status = [&]() -> status_t {
495 int32_t eventAidl = VALUE_OR_RETURN_STATUS(convertIntegral<int32_t>(event));
496 media::RecordClientInfo clientInfoAidl = VALUE_OR_RETURN_STATUS(
497 legacy2aidl_record_client_info_t_RecordClientInfo(*clientInfo));
498 media::AudioConfigBase clientConfigAidl = VALUE_OR_RETURN_STATUS(
Mikhail Naganovde3fa182021-07-30 15:06:42 -0700499 legacy2aidl_audio_config_base_t_AudioConfigBase(
500 *clientConfig, true /*isInput*/));
Ytai Ben-Tsvi7e7a79d2020-12-15 16:48:16 -0800501 std::vector<media::EffectDescriptor> clientEffectsAidl = VALUE_OR_RETURN_STATUS(
502 convertContainer<std::vector<media::EffectDescriptor>>(
503 clientEffects,
504 legacy2aidl_effect_descriptor_t_EffectDescriptor));
505 media::AudioConfigBase deviceConfigAidl = VALUE_OR_RETURN_STATUS(
Mikhail Naganovde3fa182021-07-30 15:06:42 -0700506 legacy2aidl_audio_config_base_t_AudioConfigBase(
507 *deviceConfig, true /*isInput*/));
Ytai Ben-Tsvi7e7a79d2020-12-15 16:48:16 -0800508 std::vector<media::EffectDescriptor> effectsAidl = VALUE_OR_RETURN_STATUS(
509 convertContainer<std::vector<media::EffectDescriptor>>(
510 effects,
511 legacy2aidl_effect_descriptor_t_EffectDescriptor));
512 int32_t patchHandleAidl = VALUE_OR_RETURN_STATUS(
513 legacy2aidl_audio_patch_handle_t_int32_t(patchHandle));
514 media::AudioSourceType sourceAidl = VALUE_OR_RETURN_STATUS(
515 legacy2aidl_audio_source_t_AudioSourceType(source));
516 return aidl_utils::statusTFromBinderStatus(
517 mAudioPolicyServiceClient->onRecordingConfigurationUpdate(eventAidl,
518 clientInfoAidl,
519 clientConfigAidl,
520 clientEffectsAidl,
521 deviceConfigAidl,
522 effectsAidl,
523 patchHandleAidl,
524 sourceAidl));
525 }();
526 ALOGW_IF(status != OK, "onRecordingConfigurationUpdate() failed: %d", status);
Jean-Michel Trivide801052015-04-14 19:10:14 -0700527 }
528}
529
Eric Laurente8726fe2015-06-26 09:39:24 -0700530void AudioPolicyService::NotificationClient::setAudioPortCallbacksEnabled(bool enabled)
531{
532 mAudioPortCallbacksEnabled = enabled;
533}
534
François Gaffiecfe17322018-11-07 13:41:29 +0100535void AudioPolicyService::NotificationClient::setAudioVolumeGroupCallbacksEnabled(bool enabled)
536{
537 mAudioVolumeGroupCallbacksEnabled = enabled;
538}
Eric Laurente8726fe2015-06-26 09:39:24 -0700539
Jean-Michel Trivi9a6b9ad2020-10-22 16:46:43 -0700540void AudioPolicyService::NotificationClient::onRoutingUpdated()
541{
542 if (mAudioPolicyServiceClient != 0 && isServiceUid(mUid)) {
543 mAudioPolicyServiceClient->onRoutingUpdated();
544 }
545}
546
Mathias Agopian65ab4712010-07-14 17:59:35 -0700547void AudioPolicyService::binderDied(const wp<IBinder>& who) {
Glenn Kasten411e4472012-11-02 10:00:06 -0700548 ALOGW("binderDied() %p, calling pid %d", who.unsafe_get(),
Eric Laurentde070132010-07-13 04:45:46 -0700549 IPCThreadState::self()->getCallingPid());
Mathias Agopian65ab4712010-07-14 17:59:35 -0700550}
551
Mikhail Naganov12b716c2020-04-30 22:37:43 +0000552static bool dumpTryLock(Mutex& mutex) ACQUIRE(mutex) NO_THREAD_SAFETY_ANALYSIS
Mathias Agopian65ab4712010-07-14 17:59:35 -0700553{
Mikhail Naganov12b716c2020-04-30 22:37:43 +0000554 return mutex.timedLock(kDumpLockTimeoutNs) == NO_ERROR;
555}
556
557static void dumpReleaseLock(Mutex& mutex, bool locked) RELEASE(mutex) NO_THREAD_SAFETY_ANALYSIS
558{
559 if (locked) mutex.unlock();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700560}
561
562status_t AudioPolicyService::dumpInternals(int fd)
563{
564 const size_t SIZE = 256;
565 char buffer[SIZE];
566 String8 result;
567
Eric Laurentdce54a12014-03-10 12:19:46 -0700568 snprintf(buffer, SIZE, "AudioPolicyManager: %p\n", mAudioPolicyManager);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700569 result.append(buffer);
570 snprintf(buffer, SIZE, "Command Thread: %p\n", mAudioCommandThread.get());
571 result.append(buffer);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700572
Hayden Gomes524159d2019-12-23 14:41:47 -0800573 snprintf(buffer, SIZE, "Supported System Usages:\n");
574 result.append(buffer);
575 for (std::vector<audio_usage_t>::iterator it = mSupportedSystemUsages.begin();
576 it != mSupportedSystemUsages.end(); ++it) {
577 snprintf(buffer, SIZE, "\t%d\n", *it);
578 result.append(buffer);
579 }
580
Mathias Agopian65ab4712010-07-14 17:59:35 -0700581 write(fd, result.string(), result.size());
582 return NO_ERROR;
583}
584
Eric Laurente8c8b432018-10-17 10:08:02 -0700585void AudioPolicyService::updateUidStates()
Svet Ganovf4ddfef2018-01-16 07:37:58 -0800586{
Eric Laurente8c8b432018-10-17 10:08:02 -0700587 Mutex::Autolock _l(mLock);
588 updateUidStates_l();
589}
590
591void AudioPolicyService::updateUidStates_l()
592{
Eric Laurent4eb58f12018-12-07 16:41:02 -0800593// Go over all active clients and allow capture (does not force silence) in the
594// following cases:
Evan Severson1f700cd2021-02-10 13:10:37 -0800595// The client is the assistant
596// AND an accessibility service is on TOP or a RTT call is active
Eric Laurent589171c2019-07-25 18:04:29 -0700597// AND the source is VOICE_RECOGNITION or HOTWORD
Evan Severson1f700cd2021-02-10 13:10:37 -0800598// OR uses VOICE_RECOGNITION AND is on TOP
599// OR uses HOTWORD
600// AND there is no active privacy sensitive capture or call
601// OR client has CAPTURE_AUDIO_OUTPUT privileged permission
602// OR The client is an accessibility service
603// AND Is on TOP
604// AND the source is VOICE_RECOGNITION or HOTWORD
605// OR The assistant is not on TOP
Eric Laurent589171c2019-07-25 18:04:29 -0700606// AND there is no active privacy sensitive capture or call
607// OR client has CAPTURE_AUDIO_OUTPUT privileged permission
Evan Severson1f700cd2021-02-10 13:10:37 -0800608// AND is on TOP
609// AND the source is VOICE_RECOGNITION or HOTWORD
610// OR the client source is virtual (remote submix, call audio TX or RX...)
611// OR the client source is HOTWORD
612// AND is on TOP
613// OR all active clients are using HOTWORD source
614// AND no call is active
615// OR client has CAPTURE_AUDIO_OUTPUT privileged permission
616// OR the client is the current InputMethodService
617// AND a RTT call is active AND the source is VOICE_RECOGNITION
618// OR Any client
619// AND The assistant is not on TOP
620// AND is on TOP or latest started
621// AND there is no active privacy sensitive capture or call
622// OR client has CAPTURE_AUDIO_OUTPUT privileged permission
Eric Laurent4eb58f12018-12-07 16:41:02 -0800623
Eric Laurent4e947da2019-10-17 15:24:06 -0700624
Eric Laurent4eb58f12018-12-07 16:41:02 -0800625 sp<AudioRecordClient> topActive;
626 sp<AudioRecordClient> latestActive;
Eric Laurentc21d5692020-02-25 10:24:36 -0800627 sp<AudioRecordClient> topSensitiveActive;
Eric Laurentb809a752020-06-29 09:53:13 -0700628 sp<AudioRecordClient> latestSensitiveActiveOrComm;
Eric Laurent1ff16a72019-03-14 18:35:04 -0700629
Eric Laurenta46bedb2018-12-07 18:01:26 -0800630 nsecs_t topStartNs = 0;
631 nsecs_t latestStartNs = 0;
Eric Laurentc21d5692020-02-25 10:24:36 -0800632 nsecs_t topSensitiveStartNs = 0;
Eric Laurent4eb58f12018-12-07 16:41:02 -0800633 nsecs_t latestSensitiveStartNs = 0;
634 bool isA11yOnTop = mUidPolicy->isA11yOnTop();
635 bool isAssistantOnTop = false;
636 bool isSensitiveActive = false;
Eric Laurent1ff16a72019-03-14 18:35:04 -0700637 bool isInCall = mPhoneState == AUDIO_MODE_IN_CALL;
Eric Laurentc21d5692020-02-25 10:24:36 -0800638 bool isInCommunication = mPhoneState == AUDIO_MODE_IN_COMMUNICATION;
639 bool rttCallActive = (isInCall || isInCommunication)
Eric Laurent6ede98f2019-06-11 14:50:30 -0700640 && mUidPolicy->isRttEnabled();
Eric Laurent4e947da2019-10-17 15:24:06 -0700641 bool onlyHotwordActive = true;
Eric Laurentb809a752020-06-29 09:53:13 -0700642 bool isPhoneStateOwnerActive = false;
Eric Laurent4eb58f12018-12-07 16:41:02 -0800643
Michael Groovercfd28302018-12-11 19:16:46 -0800644 // if Sensor Privacy is enabled then all recordings should be silenced.
645 if (mSensorPrivacyPolicy->isSensorPrivacyEnabled()) {
646 silenceAllRecordings_l();
647 return;
648 }
649
Eric Laurente8c8b432018-10-17 10:08:02 -0700650 for (size_t i =0; i < mAudioRecordClients.size(); i++) {
651 sp<AudioRecordClient> current = mAudioRecordClients[i];
Svet Ganov33761132021-05-13 22:51:08 +0000652 uid_t currentUid = VALUE_OR_FATAL(aidl2legacy_int32_t_uid_t(
653 current->attributionSource.uid));
Evan Severson1f700cd2021-02-10 13:10:37 -0800654 if (!current->active) {
Eric Laurent1ff16a72019-03-14 18:35:04 -0700655 continue;
Eric Laurent4eb58f12018-12-07 16:41:02 -0800656 }
Eric Laurent1ff16a72019-03-14 18:35:04 -0700657
Philip P. Moltmannbda45752020-07-17 16:41:18 -0700658 app_state_t appState = apmStatFromAmState(mUidPolicy->getUidState(currentUid));
Eric Laurent1ff16a72019-03-14 18:35:04 -0700659 // clients which app is in IDLE state are not eligible for top active or
660 // latest active
661 if (appState == APP_STATE_IDLE) {
662 continue;
663 }
664
Philip P. Moltmannbda45752020-07-17 16:41:18 -0700665 bool isAccessibility = mUidPolicy->isA11yUid(currentUid);
Eric Laurent14a88632020-07-16 12:28:30 -0700666 // Clients capturing for Accessibility services or virtual sources are not considered
Eric Laurentc21d5692020-02-25 10:24:36 -0800667 // for top or latest active to avoid masking regular clients started before
Eric Laurent14a88632020-07-16 12:28:30 -0700668 if (!isAccessibility && !isVirtualSource(current->attributes.source)) {
Philip P. Moltmannbda45752020-07-17 16:41:18 -0700669 bool isAssistant = mUidPolicy->isAssistantUid(currentUid);
Eric Laurentc21d5692020-02-25 10:24:36 -0800670 bool isPrivacySensitive =
671 (current->attributes.flags & AUDIO_FLAG_CAPTURE_PRIVATE) != 0;
Eric Laurentb809a752020-06-29 09:53:13 -0700672
Eric Laurentc21d5692020-02-25 10:24:36 -0800673 if (appState == APP_STATE_TOP) {
674 if (isPrivacySensitive) {
675 if (current->startTimeNs > topSensitiveStartNs) {
676 topSensitiveActive = current;
677 topSensitiveStartNs = current->startTimeNs;
678 }
679 } else {
680 if (current->startTimeNs > topStartNs) {
681 topActive = current;
682 topStartNs = current->startTimeNs;
683 }
684 }
685 if (isAssistant) {
686 isAssistantOnTop = true;
687 }
Eric Laurenta46bedb2018-12-07 18:01:26 -0800688 }
Eric Laurentc21d5692020-02-25 10:24:36 -0800689 // Clients capturing for HOTWORD are not considered
690 // for latest active to avoid masking regular clients started before
691 if (!(current->attributes.source == AUDIO_SOURCE_HOTWORD
692 || ((isA11yOnTop || rttCallActive) && isAssistant))) {
693 if (isPrivacySensitive) {
Eric Laurentb809a752020-06-29 09:53:13 -0700694 // if audio mode is IN_COMMUNICATION, make sure the audio mode owner
695 // is marked latest sensitive active even if another app qualifies.
696 if (current->startTimeNs > latestSensitiveStartNs
Philip P. Moltmannbda45752020-07-17 16:41:18 -0700697 || (isInCommunication && currentUid == mPhoneStateOwnerUid)) {
Eric Laurentb809a752020-06-29 09:53:13 -0700698 if (!isInCommunication || latestSensitiveActiveOrComm == nullptr
Philip P. Moltmannbda45752020-07-17 16:41:18 -0700699 || VALUE_OR_FATAL(aidl2legacy_int32_t_uid_t(
Svet Ganov33761132021-05-13 22:51:08 +0000700 latestSensitiveActiveOrComm->attributionSource.uid))
Philip P. Moltmannbda45752020-07-17 16:41:18 -0700701 != mPhoneStateOwnerUid) {
Eric Laurentb809a752020-06-29 09:53:13 -0700702 latestSensitiveActiveOrComm = current;
703 latestSensitiveStartNs = current->startTimeNs;
704 }
Eric Laurentc21d5692020-02-25 10:24:36 -0800705 }
706 isSensitiveActive = true;
707 } else {
708 if (current->startTimeNs > latestStartNs) {
709 latestActive = current;
710 latestStartNs = current->startTimeNs;
711 }
712 }
Eric Laurent4eb58f12018-12-07 16:41:02 -0800713 }
714 }
Eric Laurent4e947da2019-10-17 15:24:06 -0700715 if (current->attributes.source != AUDIO_SOURCE_HOTWORD) {
716 onlyHotwordActive = false;
717 }
Philip P. Moltmannbda45752020-07-17 16:41:18 -0700718 if (currentUid == mPhoneStateOwnerUid) {
Eric Laurentb809a752020-06-29 09:53:13 -0700719 isPhoneStateOwnerActive = true;
720 }
Eric Laurent4eb58f12018-12-07 16:41:02 -0800721 }
722
Eric Laurent1ff16a72019-03-14 18:35:04 -0700723 // if no active client with UI on Top, consider latest active as top
724 if (topActive == nullptr) {
725 topActive = latestActive;
Eric Laurentc21d5692020-02-25 10:24:36 -0800726 topStartNs = latestStartNs;
727 }
728 if (topSensitiveActive == nullptr) {
Eric Laurentb809a752020-06-29 09:53:13 -0700729 topSensitiveActive = latestSensitiveActiveOrComm;
Eric Laurentc21d5692020-02-25 10:24:36 -0800730 topSensitiveStartNs = latestSensitiveStartNs;
Eric Laurentb809a752020-06-29 09:53:13 -0700731 } else if (latestSensitiveActiveOrComm != nullptr) {
732 // if audio mode is IN_COMMUNICATION, favor audio mode owner over an app with
733 // foreground UI in case both are capturing with privacy sensitive flag.
Philip P. Moltmannbda45752020-07-17 16:41:18 -0700734 uid_t latestActiveUid = VALUE_OR_FATAL(
Svet Ganov33761132021-05-13 22:51:08 +0000735 aidl2legacy_int32_t_uid_t(latestSensitiveActiveOrComm->attributionSource.uid));
Philip P. Moltmannbda45752020-07-17 16:41:18 -0700736 if (isInCommunication && latestActiveUid == mPhoneStateOwnerUid) {
Eric Laurentb809a752020-06-29 09:53:13 -0700737 topSensitiveActive = latestSensitiveActiveOrComm;
738 topSensitiveStartNs = latestSensitiveStartNs;
739 }
Eric Laurentc21d5692020-02-25 10:24:36 -0800740 }
741
742 // If both privacy sensitive and regular capture are active:
743 // if the regular capture is privileged
744 // allow concurrency
745 // else
746 // favor the privacy sensitive case
747 if (topActive != nullptr && topSensitiveActive != nullptr
Ricardo Correa57a37692020-03-23 17:27:25 -0700748 && !topActive->canCaptureOutput) {
Eric Laurentc21d5692020-02-25 10:24:36 -0800749 topActive = nullptr;
Eric Laurent4eb58f12018-12-07 16:41:02 -0800750 }
751
752 for (size_t i =0; i < mAudioRecordClients.size(); i++) {
753 sp<AudioRecordClient> current = mAudioRecordClients[i];
Philip P. Moltmannbda45752020-07-17 16:41:18 -0700754 uid_t currentUid = VALUE_OR_FATAL(aidl2legacy_int32_t_uid_t(
Svet Ganov33761132021-05-13 22:51:08 +0000755 current->attributionSource.uid));
Eric Laurent1ff16a72019-03-14 18:35:04 -0700756 if (!current->active) {
757 continue;
758 }
759
Eric Laurent4eb58f12018-12-07 16:41:02 -0800760 audio_source_t source = current->attributes.source;
Philip P. Moltmannbda45752020-07-17 16:41:18 -0700761 bool isTopOrLatestActive = topActive == nullptr ? false :
Svet Ganov33761132021-05-13 22:51:08 +0000762 current->attributionSource.uid == topActive->attributionSource.uid;
Philip P. Moltmannbda45752020-07-17 16:41:18 -0700763 bool isTopOrLatestSensitive = topSensitiveActive == nullptr ? false :
Svet Ganov33761132021-05-13 22:51:08 +0000764 current->attributionSource.uid == topSensitiveActive->attributionSource.uid;
Eric Laurentc21d5692020-02-25 10:24:36 -0800765
Mikhail Naganov12b716c2020-04-30 22:37:43 +0000766 auto canCaptureIfInCallOrCommunication = [&](const auto &recordClient) REQUIRES(mLock) {
Philip P. Moltmannbda45752020-07-17 16:41:18 -0700767 uid_t recordUid = VALUE_OR_FATAL(aidl2legacy_int32_t_uid_t(
Svet Ganov33761132021-05-13 22:51:08 +0000768 recordClient->attributionSource.uid));
Ricardo Correa57a37692020-03-23 17:27:25 -0700769 bool canCaptureCall = recordClient->canCaptureOutput;
Eric Laurentb809a752020-06-29 09:53:13 -0700770 bool canCaptureCommunication = recordClient->canCaptureOutput
771 || !isPhoneStateOwnerActive
Philip P. Moltmannbda45752020-07-17 16:41:18 -0700772 || recordUid == mPhoneStateOwnerUid;
Eric Laurentb809a752020-06-29 09:53:13 -0700773 return !(isInCall && !canCaptureCall)
774 && !(isInCommunication && !canCaptureCommunication);
Eric Laurentc21d5692020-02-25 10:24:36 -0800775 };
Eric Laurent1ff16a72019-03-14 18:35:04 -0700776
777 // By default allow capture if:
778 // The assistant is not on TOP
Eric Laurenta171e352019-05-07 13:04:45 -0700779 // AND is on TOP or latest started
Eric Laurent1ff16a72019-03-14 18:35:04 -0700780 // AND there is no active privacy sensitive capture or call
781 // OR client has CAPTURE_AUDIO_OUTPUT privileged permission
782 bool allowCapture = !isAssistantOnTop
Eric Laurentc21d5692020-02-25 10:24:36 -0800783 && (isTopOrLatestActive || isTopOrLatestSensitive)
784 && !(isSensitiveActive
Ricardo Correa57a37692020-03-23 17:27:25 -0700785 && !(isTopOrLatestSensitive || current->canCaptureOutput))
Eric Laurentc21d5692020-02-25 10:24:36 -0800786 && canCaptureIfInCallOrCommunication(current);
Eric Laurent2dc962b2019-03-01 08:25:25 -0800787
Eric Laurented726cc2021-07-01 14:26:41 +0200788 if (!current->hasOp()) {
789 // Never allow capture if app op is denied
790 allowCapture = false;
791 } else if (isVirtualSource(source)) {
Eric Laurent1ff16a72019-03-14 18:35:04 -0700792 // Allow capture for virtual (remote submix, call audio TX or RX...) sources
793 allowCapture = true;
Philip P. Moltmannbda45752020-07-17 16:41:18 -0700794 } else if (mUidPolicy->isAssistantUid(currentUid)) {
Eric Laurent1ff16a72019-03-14 18:35:04 -0700795 // For assistant allow capture if:
Eric Laurent6ede98f2019-06-11 14:50:30 -0700796 // An accessibility service is on TOP or a RTT call is active
Eric Laurent1ff16a72019-03-14 18:35:04 -0700797 // AND the source is VOICE_RECOGNITION or HOTWORD
Eric Laurenta171e352019-05-07 13:04:45 -0700798 // OR is on TOP AND uses VOICE_RECOGNITION
Eric Laurent1ff16a72019-03-14 18:35:04 -0700799 // OR uses HOTWORD
800 // AND there is no active privacy sensitive capture or call
801 // OR client has CAPTURE_AUDIO_OUTPUT privileged permission
Eric Laurent6ede98f2019-06-11 14:50:30 -0700802 if (isA11yOnTop || rttCallActive) {
Eric Laurent4eb58f12018-12-07 16:41:02 -0800803 if (source == AUDIO_SOURCE_HOTWORD || source == AUDIO_SOURCE_VOICE_RECOGNITION) {
Eric Laurent1ff16a72019-03-14 18:35:04 -0700804 allowCapture = true;
Eric Laurent4eb58f12018-12-07 16:41:02 -0800805 }
806 } else {
Eric Laurenta171e352019-05-07 13:04:45 -0700807 if (((isAssistantOnTop && source == AUDIO_SOURCE_VOICE_RECOGNITION) ||
Eric Laurentc21d5692020-02-25 10:24:36 -0800808 source == AUDIO_SOURCE_HOTWORD)
Ricardo Correa57a37692020-03-23 17:27:25 -0700809 && !(isSensitiveActive && !current->canCaptureOutput)
Eric Laurentc21d5692020-02-25 10:24:36 -0800810 && canCaptureIfInCallOrCommunication(current)) {
Eric Laurent1ff16a72019-03-14 18:35:04 -0700811 allowCapture = true;
Eric Laurent4eb58f12018-12-07 16:41:02 -0800812 }
813 }
Philip P. Moltmannbda45752020-07-17 16:41:18 -0700814 } else if (mUidPolicy->isA11yUid(currentUid)) {
Eric Laurent1ff16a72019-03-14 18:35:04 -0700815 // For accessibility service allow capture if:
Eric Laurent47670c92019-08-28 16:59:05 -0700816 // The assistant is not on TOP
817 // AND there is no active privacy sensitive capture or call
Eric Laurent589171c2019-07-25 18:04:29 -0700818 // OR client has CAPTURE_AUDIO_OUTPUT privileged permission
Eric Laurent47670c92019-08-28 16:59:05 -0700819 // OR
820 // Is on TOP AND the source is VOICE_RECOGNITION or HOTWORD
821 if (!isAssistantOnTop
Ricardo Correa57a37692020-03-23 17:27:25 -0700822 && !(isSensitiveActive && !current->canCaptureOutput)
Eric Laurentc21d5692020-02-25 10:24:36 -0800823 && canCaptureIfInCallOrCommunication(current)) {
Eric Laurent47670c92019-08-28 16:59:05 -0700824 allowCapture = true;
825 }
Eric Laurent589171c2019-07-25 18:04:29 -0700826 if (isA11yOnTop) {
827 if (source == AUDIO_SOURCE_VOICE_RECOGNITION || source == AUDIO_SOURCE_HOTWORD) {
828 allowCapture = true;
829 }
Eric Laurent4eb58f12018-12-07 16:41:02 -0800830 }
Eric Laurent4e947da2019-10-17 15:24:06 -0700831 } else if (source == AUDIO_SOURCE_HOTWORD) {
832 // For HOTWORD source allow capture when not on TOP if:
833 // All active clients are using HOTWORD source
834 // AND no call is active
835 // OR client has CAPTURE_AUDIO_OUTPUT privileged permission
Eric Laurentc21d5692020-02-25 10:24:36 -0800836 if (onlyHotwordActive
837 && canCaptureIfInCallOrCommunication(current)) {
Eric Laurent4e947da2019-10-17 15:24:06 -0700838 allowCapture = true;
839 }
Philip P. Moltmannbda45752020-07-17 16:41:18 -0700840 } else if (mUidPolicy->isCurrentImeUid(currentUid)) {
Kohsuke Yatoha623a132020-03-24 20:10:26 -0700841 // For current InputMethodService allow capture if:
842 // A RTT call is active AND the source is VOICE_RECOGNITION
843 if (rttCallActive && source == AUDIO_SOURCE_VOICE_RECOGNITION) {
844 allowCapture = true;
845 }
Eric Laurent4eb58f12018-12-07 16:41:02 -0800846 }
Eric Laurent8c7ef892021-06-10 13:32:16 +0200847 setAppState_l(current,
Philip P. Moltmannbda45752020-07-17 16:41:18 -0700848 allowCapture ? apmStatFromAmState(mUidPolicy->getUidState(currentUid)) :
Eric Laurent1ff16a72019-03-14 18:35:04 -0700849 APP_STATE_IDLE);
Eric Laurente8c8b432018-10-17 10:08:02 -0700850 }
851}
852
Michael Groovercfd28302018-12-11 19:16:46 -0800853void AudioPolicyService::silenceAllRecordings_l() {
854 for (size_t i = 0; i < mAudioRecordClients.size(); i++) {
855 sp<AudioRecordClient> current = mAudioRecordClients[i];
Eric Laurent1ff16a72019-03-14 18:35:04 -0700856 if (!isVirtualSource(current->attributes.source)) {
Eric Laurent8c7ef892021-06-10 13:32:16 +0200857 setAppState_l(current, APP_STATE_IDLE);
Eric Laurent1ff16a72019-03-14 18:35:04 -0700858 }
Michael Groovercfd28302018-12-11 19:16:46 -0800859 }
860}
861
Eric Laurente8c8b432018-10-17 10:08:02 -0700862/* static */
863app_state_t AudioPolicyService::apmStatFromAmState(int amState) {
Eric Laurent1ff16a72019-03-14 18:35:04 -0700864
865 if (amState == ActivityManager::PROCESS_STATE_UNKNOWN) {
Eric Laurente8c8b432018-10-17 10:08:02 -0700866 return APP_STATE_IDLE;
Eric Laurent1ff16a72019-03-14 18:35:04 -0700867 } else if (amState <= ActivityManager::PROCESS_STATE_TOP) {
868 // include persistent services
869 return APP_STATE_TOP;
Eric Laurente8c8b432018-10-17 10:08:02 -0700870 }
871 return APP_STATE_FOREGROUND;
872}
873
Eric Laurent4eb58f12018-12-07 16:41:02 -0800874/* static */
Eric Laurent2dc962b2019-03-01 08:25:25 -0800875bool AudioPolicyService::isVirtualSource(audio_source_t source)
Eric Laurent4eb58f12018-12-07 16:41:02 -0800876{
877 switch (source) {
878 case AUDIO_SOURCE_VOICE_UPLINK:
879 case AUDIO_SOURCE_VOICE_DOWNLINK:
880 case AUDIO_SOURCE_VOICE_CALL:
Eric Laurent2dc962b2019-03-01 08:25:25 -0800881 case AUDIO_SOURCE_REMOTE_SUBMIX:
882 case AUDIO_SOURCE_FM_TUNER:
Eric Laurent68eb2122020-04-30 17:40:57 -0700883 case AUDIO_SOURCE_ECHO_REFERENCE:
Eric Laurent4eb58f12018-12-07 16:41:02 -0800884 return true;
885 default:
886 break;
887 }
888 return false;
889}
890
Eric Laurented726cc2021-07-01 14:26:41 +0200891/* static */
892bool AudioPolicyService::isAppOpSource(audio_source_t source)
893{
894 switch (source) {
895 case AUDIO_SOURCE_FM_TUNER:
896 case AUDIO_SOURCE_ECHO_REFERENCE:
897 return false;
898 default:
899 break;
900 }
901 return true;
902}
903
Eric Laurent8c7ef892021-06-10 13:32:16 +0200904void AudioPolicyService::setAppState_l(sp<AudioRecordClient> client, app_state_t state)
Eric Laurente8c8b432018-10-17 10:08:02 -0700905{
906 AutoCallerClear acc;
907
908 if (mAudioPolicyManager) {
Eric Laurent8c7ef892021-06-10 13:32:16 +0200909 mAudioPolicyManager->setAppState(client->portId, state);
Mikhail Naganoveae73eb2018-04-03 16:57:36 -0700910 }
911 sp<IAudioFlinger> af = AudioSystem::get_audio_flinger();
912 if (af) {
Eric Laurentf32108e2018-10-04 17:22:04 -0700913 bool silenced = state == APP_STATE_IDLE;
Eric Laurent8c7ef892021-06-10 13:32:16 +0200914 if (client->silenced != silenced) {
915 if (client->active) {
916 if (silenced) {
917 finishRecording(client->attributionSource, client->attributes.source);
918 } else {
919 std::stringstream msg;
920 msg << "Audio recording un-silenced on session " << client->session;
921 if (!startRecording(client->attributionSource, String16(msg.str().c_str()),
922 client->attributes.source)) {
923 silenced = true;
924 }
925 }
926 }
927 af->setRecordSilenced(client->portId, silenced);
928 client->silenced = silenced;
929 }
Mikhail Naganoveae73eb2018-04-03 16:57:36 -0700930 }
Svet Ganovf4ddfef2018-01-16 07:37:58 -0800931}
932
Glenn Kasten0f11b512014-01-31 16:18:54 -0800933status_t AudioPolicyService::dump(int fd, const Vector<String16>& args __unused)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700934{
Glenn Kasten44deb052012-02-05 18:09:08 -0800935 if (!dumpAllowed()) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700936 dumpPermissionDenial(fd);
937 } else {
Mikhail Naganov12b716c2020-04-30 22:37:43 +0000938 const bool locked = dumpTryLock(mLock);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700939 if (!locked) {
940 String8 result(kDeadlockedString);
941 write(fd, result.string(), result.size());
942 }
943
944 dumpInternals(fd);
Glenn Kasten9d1f02d2012-02-08 17:47:58 -0800945 if (mAudioCommandThread != 0) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700946 mAudioCommandThread->dump(fd);
947 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700948
Eric Laurentdce54a12014-03-10 12:19:46 -0700949 if (mAudioPolicyManager) {
950 mAudioPolicyManager->dump(fd);
951 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700952
Kevin Rocard8be94972019-02-22 13:26:25 -0800953 mPackageManager.dump(fd);
954
Mikhail Naganov12b716c2020-04-30 22:37:43 +0000955 dumpReleaseLock(mLock, locked);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700956 }
957 return NO_ERROR;
958}
959
960status_t AudioPolicyService::dumpPermissionDenial(int fd)
961{
962 const size_t SIZE = 256;
963 char buffer[SIZE];
964 String8 result;
965 snprintf(buffer, SIZE, "Permission Denial: "
966 "can't dump AudioPolicyService from pid=%d, uid=%d\n",
967 IPCThreadState::self()->getCallingPid(),
968 IPCThreadState::self()->getCallingUid());
969 result.append(buffer);
970 write(fd, result.string(), result.size());
971 return NO_ERROR;
972}
973
974status_t AudioPolicyService::onTransact(
Svet Ganovf4ddfef2018-01-16 07:37:58 -0800975 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags) {
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800976 // make sure transactions reserved to AudioFlinger do not come from other processes
977 switch (code) {
978 case TRANSACTION_startOutput:
979 case TRANSACTION_stopOutput:
980 case TRANSACTION_releaseOutput:
981 case TRANSACTION_getInputForAttr:
982 case TRANSACTION_startInput:
983 case TRANSACTION_stopInput:
984 case TRANSACTION_releaseInput:
985 case TRANSACTION_getOutputForEffect:
986 case TRANSACTION_registerEffect:
987 case TRANSACTION_unregisterEffect:
988 case TRANSACTION_setEffectEnabled:
989 case TRANSACTION_getStrategyForStream:
990 case TRANSACTION_getOutputForAttr:
991 case TRANSACTION_moveEffectsToIo:
992 ALOGW("%s: transaction %d received from PID %d",
993 __func__, code, IPCThreadState::self()->getCallingPid());
994 return INVALID_OPERATION;
995 default:
996 break;
997 }
998
999 // make sure the following transactions come from system components
1000 switch (code) {
1001 case TRANSACTION_setDeviceConnectionState:
1002 case TRANSACTION_handleDeviceConfigChange:
1003 case TRANSACTION_setPhoneState:
1004//FIXME: Allow setForceUse calls from system apps until a better use case routing API is available
1005// case TRANSACTION_setForceUse:
1006 case TRANSACTION_initStreamVolume:
1007 case TRANSACTION_setStreamVolumeIndex:
1008 case TRANSACTION_setVolumeIndexForAttributes:
1009 case TRANSACTION_getStreamVolumeIndex:
1010 case TRANSACTION_getVolumeIndexForAttributes:
1011 case TRANSACTION_getMinVolumeIndexForAttributes:
1012 case TRANSACTION_getMaxVolumeIndexForAttributes:
1013 case TRANSACTION_isStreamActive:
1014 case TRANSACTION_isStreamActiveRemotely:
1015 case TRANSACTION_isSourceActive:
1016 case TRANSACTION_getDevicesForStream:
1017 case TRANSACTION_registerPolicyMixes:
1018 case TRANSACTION_setMasterMono:
1019 case TRANSACTION_getSurroundFormats:
Kriti Dang6537def2021-03-02 13:46:59 +01001020 case TRANSACTION_getReportedSurroundFormats:
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001021 case TRANSACTION_setSurroundFormatEnabled:
1022 case TRANSACTION_setAssistantUid:
1023 case TRANSACTION_setA11yServicesUids:
1024 case TRANSACTION_setUidDeviceAffinities:
1025 case TRANSACTION_removeUidDeviceAffinities:
1026 case TRANSACTION_setUserIdDeviceAffinities:
1027 case TRANSACTION_removeUserIdDeviceAffinities:
1028 case TRANSACTION_getHwOffloadEncodingFormatsSupportedForA2DP:
1029 case TRANSACTION_listAudioVolumeGroups:
1030 case TRANSACTION_getVolumeGroupFromAudioAttributes:
1031 case TRANSACTION_acquireSoundTriggerSession:
1032 case TRANSACTION_releaseSoundTriggerSession:
1033 case TRANSACTION_setRttEnabled:
1034 case TRANSACTION_isCallScreenModeSupported:
1035 case TRANSACTION_setDevicesRoleForStrategy:
1036 case TRANSACTION_setSupportedSystemUsages:
1037 case TRANSACTION_removeDevicesRoleForStrategy:
1038 case TRANSACTION_getDevicesForRoleAndStrategy:
1039 case TRANSACTION_getDevicesForAttributes:
1040 case TRANSACTION_setAllowedCapturePolicy:
1041 case TRANSACTION_onNewAudioModulesAvailable:
1042 case TRANSACTION_setCurrentImeUid:
1043 case TRANSACTION_registerSoundTriggerCaptureStateListener:
1044 case TRANSACTION_setDevicesRoleForCapturePreset:
1045 case TRANSACTION_addDevicesRoleForCapturePreset:
1046 case TRANSACTION_removeDevicesRoleForCapturePreset:
1047 case TRANSACTION_clearDevicesRoleForCapturePreset:
Eric Laurent81dd0f52021-07-05 11:54:40 +02001048 case TRANSACTION_getDevicesForRoleAndCapturePreset:
1049 case TRANSACTION_getSpatializer: {
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001050 if (!isServiceUid(IPCThreadState::self()->getCallingUid())) {
1051 ALOGW("%s: transaction %d received from PID %d unauthorized UID %d",
1052 __func__, code, IPCThreadState::self()->getCallingPid(),
1053 IPCThreadState::self()->getCallingUid());
1054 return INVALID_OPERATION;
1055 }
1056 } break;
1057 default:
1058 break;
1059 }
1060
1061 std::string tag("IAudioPolicyService command " + std::to_string(code));
1062 TimeCheck check(tag.c_str());
1063
Svet Ganovf4ddfef2018-01-16 07:37:58 -08001064 switch (code) {
1065 case SHELL_COMMAND_TRANSACTION: {
1066 int in = data.readFileDescriptor();
1067 int out = data.readFileDescriptor();
1068 int err = data.readFileDescriptor();
1069 int argc = data.readInt32();
1070 Vector<String16> args;
1071 for (int i = 0; i < argc && data.dataAvail() > 0; i++) {
1072 args.add(data.readString16());
1073 }
1074 sp<IBinder> unusedCallback;
1075 sp<IResultReceiver> resultReceiver;
1076 status_t status;
1077 if ((status = data.readNullableStrongBinder(&unusedCallback)) != NO_ERROR) {
1078 return status;
1079 }
1080 if ((status = data.readNullableStrongBinder(&resultReceiver)) != NO_ERROR) {
1081 return status;
1082 }
1083 status = shellCommand(in, out, err, args);
1084 if (resultReceiver != nullptr) {
1085 resultReceiver->send(status);
1086 }
1087 return NO_ERROR;
1088 }
1089 }
1090
Mathias Agopian65ab4712010-07-14 17:59:35 -07001091 return BnAudioPolicyService::onTransact(code, data, reply, flags);
1092}
1093
Svet Ganovf4ddfef2018-01-16 07:37:58 -08001094// ------------------- Shell command implementation -------------------
1095
1096// NOTE: This is a remote API - make sure all args are validated
1097status_t AudioPolicyService::shellCommand(int in, int out, int err, Vector<String16>& args) {
1098 if (!checkCallingPermission(sManageAudioPolicyPermission, nullptr, nullptr)) {
1099 return PERMISSION_DENIED;
1100 }
1101 if (in == BAD_TYPE || out == BAD_TYPE || err == BAD_TYPE) {
1102 return BAD_VALUE;
1103 }
jovanakbe066e12019-09-02 11:54:39 -07001104 if (args.size() >= 3 && args[0] == String16("set-uid-state")) {
Svet Ganovf4ddfef2018-01-16 07:37:58 -08001105 return handleSetUidState(args, err);
jovanakbe066e12019-09-02 11:54:39 -07001106 } else if (args.size() >= 2 && args[0] == String16("reset-uid-state")) {
Svet Ganovf4ddfef2018-01-16 07:37:58 -08001107 return handleResetUidState(args, err);
jovanakbe066e12019-09-02 11:54:39 -07001108 } else if (args.size() >= 2 && args[0] == String16("get-uid-state")) {
Svet Ganovf4ddfef2018-01-16 07:37:58 -08001109 return handleGetUidState(args, out, err);
Eric Laurent269acb42021-04-23 16:53:22 +02001110 } else if (args.size() >= 1 && args[0] == String16("purge_permission-cache")) {
1111 purgePermissionCache();
1112 return NO_ERROR;
Svet Ganovf4ddfef2018-01-16 07:37:58 -08001113 } else if (args.size() == 1 && args[0] == String16("help")) {
1114 printHelp(out);
1115 return NO_ERROR;
1116 }
1117 printHelp(err);
1118 return BAD_VALUE;
1119}
1120
jovanakbe066e12019-09-02 11:54:39 -07001121static status_t getUidForPackage(String16 packageName, int userId, /*inout*/uid_t& uid, int err) {
1122 if (userId < 0) {
1123 ALOGE("Invalid user: %d", userId);
1124 dprintf(err, "Invalid user: %d\n", userId);
Svet Ganovf4ddfef2018-01-16 07:37:58 -08001125 return BAD_VALUE;
1126 }
jovanakbe066e12019-09-02 11:54:39 -07001127
1128 PermissionController pc;
1129 uid = pc.getPackageUid(packageName, 0);
1130 if (uid <= 0) {
1131 ALOGE("Unknown package: '%s'", String8(packageName).string());
1132 dprintf(err, "Unknown package: '%s'\n", String8(packageName).string());
1133 return BAD_VALUE;
1134 }
1135
1136 uid = multiuser_get_uid(userId, uid);
1137 return NO_ERROR;
1138}
1139
1140status_t AudioPolicyService::handleSetUidState(Vector<String16>& args, int err) {
1141 // Valid arg.size() is 3 or 5, args.size() is 5 with --user option.
1142 if (!(args.size() == 3 || args.size() == 5)) {
1143 printHelp(err);
1144 return BAD_VALUE;
1145 }
1146
Svet Ganovf4ddfef2018-01-16 07:37:58 -08001147 bool active = false;
1148 if (args[2] == String16("active")) {
1149 active = true;
1150 } else if ((args[2] != String16("idle"))) {
1151 ALOGE("Expected active or idle but got: '%s'", String8(args[2]).string());
1152 return BAD_VALUE;
1153 }
jovanakbe066e12019-09-02 11:54:39 -07001154
1155 int userId = 0;
1156 if (args.size() >= 5 && args[3] == String16("--user")) {
1157 userId = atoi(String8(args[4]));
1158 }
1159
1160 uid_t uid;
1161 if (getUidForPackage(args[1], userId, uid, err) == BAD_VALUE) {
1162 return BAD_VALUE;
1163 }
1164
Mikhail Naganov12b716c2020-04-30 22:37:43 +00001165 sp<UidPolicy> uidPolicy;
1166 {
1167 Mutex::Autolock _l(mLock);
1168 uidPolicy = mUidPolicy;
1169 }
1170 if (uidPolicy) {
1171 uidPolicy->addOverrideUid(uid, active);
1172 return NO_ERROR;
1173 }
1174 return NO_INIT;
Svet Ganovf4ddfef2018-01-16 07:37:58 -08001175}
1176
1177status_t AudioPolicyService::handleResetUidState(Vector<String16>& args, int err) {
jovanakbe066e12019-09-02 11:54:39 -07001178 // Valid arg.size() is 2 or 4, args.size() is 4 with --user option.
1179 if (!(args.size() == 2 || args.size() == 4)) {
1180 printHelp(err);
Svet Ganovf4ddfef2018-01-16 07:37:58 -08001181 return BAD_VALUE;
1182 }
jovanakbe066e12019-09-02 11:54:39 -07001183
1184 int userId = 0;
1185 if (args.size() >= 4 && args[2] == String16("--user")) {
1186 userId = atoi(String8(args[3]));
1187 }
1188
1189 uid_t uid;
1190 if (getUidForPackage(args[1], userId, uid, err) == BAD_VALUE) {
1191 return BAD_VALUE;
1192 }
1193
Mikhail Naganov12b716c2020-04-30 22:37:43 +00001194 sp<UidPolicy> uidPolicy;
1195 {
1196 Mutex::Autolock _l(mLock);
1197 uidPolicy = mUidPolicy;
1198 }
1199 if (uidPolicy) {
1200 uidPolicy->removeOverrideUid(uid);
1201 return NO_ERROR;
1202 }
1203 return NO_INIT;
Svet Ganovf4ddfef2018-01-16 07:37:58 -08001204}
1205
1206status_t AudioPolicyService::handleGetUidState(Vector<String16>& args, int out, int err) {
jovanakbe066e12019-09-02 11:54:39 -07001207 // Valid arg.size() is 2 or 4, args.size() is 4 with --user option.
1208 if (!(args.size() == 2 || args.size() == 4)) {
1209 printHelp(err);
Svet Ganovf4ddfef2018-01-16 07:37:58 -08001210 return BAD_VALUE;
1211 }
jovanakbe066e12019-09-02 11:54:39 -07001212
1213 int userId = 0;
1214 if (args.size() >= 4 && args[2] == String16("--user")) {
1215 userId = atoi(String8(args[3]));
1216 }
1217
1218 uid_t uid;
1219 if (getUidForPackage(args[1], userId, uid, err) == BAD_VALUE) {
1220 return BAD_VALUE;
1221 }
1222
Mikhail Naganov12b716c2020-04-30 22:37:43 +00001223 sp<UidPolicy> uidPolicy;
1224 {
1225 Mutex::Autolock _l(mLock);
1226 uidPolicy = mUidPolicy;
Svet Ganovf4ddfef2018-01-16 07:37:58 -08001227 }
Mikhail Naganov12b716c2020-04-30 22:37:43 +00001228 if (uidPolicy) {
1229 return dprintf(out, uidPolicy->isUidActive(uid) ? "active\n" : "idle\n");
1230 }
1231 return NO_INIT;
Svet Ganovf4ddfef2018-01-16 07:37:58 -08001232}
1233
1234status_t AudioPolicyService::printHelp(int out) {
1235 return dprintf(out, "Audio policy service commands:\n"
jovanakbe066e12019-09-02 11:54:39 -07001236 " get-uid-state <PACKAGE> [--user USER_ID] gets the uid state\n"
1237 " set-uid-state <PACKAGE> <active|idle> [--user USER_ID] overrides the uid state\n"
1238 " reset-uid-state <PACKAGE> [--user USER_ID] clears the uid state override\n"
Svet Ganovf4ddfef2018-01-16 07:37:58 -08001239 " help print this message\n");
1240}
1241
1242// ----------- AudioPolicyService::UidPolicy implementation ----------
1243
1244void AudioPolicyService::UidPolicy::registerSelf() {
Steven Moreland2f348142019-07-02 15:59:07 -07001245 status_t res = mAm.linkToDeath(this);
1246 mAm.registerUidObserver(this, ActivityManager::UID_OBSERVER_GONE
Svet Ganovf4ddfef2018-01-16 07:37:58 -08001247 | ActivityManager::UID_OBSERVER_IDLE
Eric Laurente8c8b432018-10-17 10:08:02 -07001248 | ActivityManager::UID_OBSERVER_ACTIVE
1249 | ActivityManager::UID_OBSERVER_PROCSTATE,
Svet Ganovf4ddfef2018-01-16 07:37:58 -08001250 ActivityManager::PROCESS_STATE_UNKNOWN,
1251 String16("audioserver"));
Mikhail Naganoveae73eb2018-04-03 16:57:36 -07001252 if (!res) {
1253 Mutex::Autolock _l(mLock);
1254 mObserverRegistered = true;
1255 } else {
1256 ALOGE("UidPolicy::registerSelf linkToDeath failed: %d", res);
Eric Laurent4eb58f12018-12-07 16:41:02 -08001257
Steven Moreland2f348142019-07-02 15:59:07 -07001258 mAm.unregisterUidObserver(this);
Mikhail Naganoveae73eb2018-04-03 16:57:36 -07001259 }
Svet Ganovf4ddfef2018-01-16 07:37:58 -08001260}
1261
1262void AudioPolicyService::UidPolicy::unregisterSelf() {
Steven Moreland2f348142019-07-02 15:59:07 -07001263 mAm.unlinkToDeath(this);
1264 mAm.unregisterUidObserver(this);
Mikhail Naganoveae73eb2018-04-03 16:57:36 -07001265 Mutex::Autolock _l(mLock);
1266 mObserverRegistered = false;
Svet Ganovf4ddfef2018-01-16 07:37:58 -08001267}
1268
Mikhail Naganoveae73eb2018-04-03 16:57:36 -07001269void AudioPolicyService::UidPolicy::binderDied(__unused const wp<IBinder> &who) {
1270 Mutex::Autolock _l(mLock);
1271 mCachedUids.clear();
1272 mObserverRegistered = false;
Svet Ganovf4ddfef2018-01-16 07:37:58 -08001273}
1274
Eric Laurente8c8b432018-10-17 10:08:02 -07001275void AudioPolicyService::UidPolicy::checkRegistered() {
Mikhail Naganoveae73eb2018-04-03 16:57:36 -07001276 bool needToReregister = false;
1277 {
1278 Mutex::Autolock _l(mLock);
1279 needToReregister = !mObserverRegistered;
Svet Ganovf4ddfef2018-01-16 07:37:58 -08001280 }
Mikhail Naganoveae73eb2018-04-03 16:57:36 -07001281 if (needToReregister) {
1282 // Looks like ActivityManager has died previously, attempt to re-register.
1283 registerSelf();
1284 }
Eric Laurente8c8b432018-10-17 10:08:02 -07001285}
1286
1287bool AudioPolicyService::UidPolicy::isUidActive(uid_t uid) {
1288 if (isServiceUid(uid)) return true;
1289 checkRegistered();
Mikhail Naganoveae73eb2018-04-03 16:57:36 -07001290 {
1291 Mutex::Autolock _l(mLock);
1292 auto overrideIter = mOverrideUids.find(uid);
1293 if (overrideIter != mOverrideUids.end()) {
Eric Laurente8c8b432018-10-17 10:08:02 -07001294 return overrideIter->second.first;
Mikhail Naganoveae73eb2018-04-03 16:57:36 -07001295 }
1296 // In an absense of the ActivityManager, assume everything to be active.
1297 if (!mObserverRegistered) return true;
1298 auto cacheIter = mCachedUids.find(uid);
Mikhail Naganoveba668a2018-04-05 08:13:15 -07001299 if (cacheIter != mCachedUids.end()) {
Eric Laurente8c8b432018-10-17 10:08:02 -07001300 return cacheIter->second.first;
Mikhail Naganoveae73eb2018-04-03 16:57:36 -07001301 }
1302 }
1303 ActivityManager am;
Hui Yu12c7ec72020-05-04 17:40:52 +00001304 bool active = am.isUidActive(uid, String16("audioserver"));
Mikhail Naganoveae73eb2018-04-03 16:57:36 -07001305 {
1306 Mutex::Autolock _l(mLock);
Eric Laurente8c8b432018-10-17 10:08:02 -07001307 mCachedUids.insert(std::pair<uid_t,
1308 std::pair<bool, int>>(uid, std::pair<bool, int>(active,
1309 ActivityManager::PROCESS_STATE_UNKNOWN)));
Mikhail Naganoveae73eb2018-04-03 16:57:36 -07001310 }
1311 return active;
Svet Ganovf4ddfef2018-01-16 07:37:58 -08001312}
1313
Eric Laurente8c8b432018-10-17 10:08:02 -07001314int AudioPolicyService::UidPolicy::getUidState(uid_t uid) {
1315 if (isServiceUid(uid)) {
1316 return ActivityManager::PROCESS_STATE_TOP;
1317 }
1318 checkRegistered();
1319 {
1320 Mutex::Autolock _l(mLock);
1321 auto overrideIter = mOverrideUids.find(uid);
1322 if (overrideIter != mOverrideUids.end()) {
1323 if (overrideIter->second.first) {
1324 if (overrideIter->second.second != ActivityManager::PROCESS_STATE_UNKNOWN) {
1325 return overrideIter->second.second;
1326 } else {
1327 auto cacheIter = mCachedUids.find(uid);
1328 if (cacheIter != mCachedUids.end()) {
1329 return cacheIter->second.second;
1330 }
1331 }
1332 }
1333 return ActivityManager::PROCESS_STATE_UNKNOWN;
1334 }
1335 // In an absense of the ActivityManager, assume everything to be active.
1336 if (!mObserverRegistered) {
1337 return ActivityManager::PROCESS_STATE_TOP;
1338 }
1339 auto cacheIter = mCachedUids.find(uid);
1340 if (cacheIter != mCachedUids.end()) {
1341 if (cacheIter->second.first) {
1342 return cacheIter->second.second;
1343 } else {
1344 return ActivityManager::PROCESS_STATE_UNKNOWN;
1345 }
1346 }
1347 }
1348 ActivityManager am;
Hui Yu12c7ec72020-05-04 17:40:52 +00001349 bool active = am.isUidActive(uid, String16("audioserver"));
Eric Laurente8c8b432018-10-17 10:08:02 -07001350 int state = ActivityManager::PROCESS_STATE_UNKNOWN;
1351 if (active) {
1352 state = am.getUidProcessState(uid, String16("audioserver"));
1353 }
1354 {
1355 Mutex::Autolock _l(mLock);
1356 mCachedUids.insert(std::pair<uid_t,
1357 std::pair<bool, int>>(uid, std::pair<bool, int>(active, state)));
1358 }
Eric Laurent4eb58f12018-12-07 16:41:02 -08001359
Eric Laurente8c8b432018-10-17 10:08:02 -07001360 return state;
1361}
1362
Mikhail Naganoveae73eb2018-04-03 16:57:36 -07001363void AudioPolicyService::UidPolicy::onUidActive(uid_t uid) {
Eric Laurente8c8b432018-10-17 10:08:02 -07001364 updateUid(&mCachedUids, uid, true, ActivityManager::PROCESS_STATE_UNKNOWN, true);
Mikhail Naganoveae73eb2018-04-03 16:57:36 -07001365}
1366
1367void AudioPolicyService::UidPolicy::onUidGone(uid_t uid, __unused bool disabled) {
Eric Laurente8c8b432018-10-17 10:08:02 -07001368 updateUid(&mCachedUids, uid, false, ActivityManager::PROCESS_STATE_UNKNOWN, false);
Mikhail Naganoveae73eb2018-04-03 16:57:36 -07001369}
1370
1371void AudioPolicyService::UidPolicy::onUidIdle(uid_t uid, __unused bool disabled) {
Eric Laurente8c8b432018-10-17 10:08:02 -07001372 updateUid(&mCachedUids, uid, false, ActivityManager::PROCESS_STATE_UNKNOWN, true);
Mikhail Naganoveae73eb2018-04-03 16:57:36 -07001373}
1374
Eric Laurente8c8b432018-10-17 10:08:02 -07001375void AudioPolicyService::UidPolicy::onUidStateChanged(uid_t uid,
1376 int32_t procState,
Hui Yu13ad0eb2019-09-09 10:27:07 -07001377 int64_t procStateSeq __unused,
1378 int32_t capability __unused) {
Eric Laurente8c8b432018-10-17 10:08:02 -07001379 if (procState != ActivityManager::PROCESS_STATE_UNKNOWN) {
1380 updateUid(&mCachedUids, uid, true, procState, true);
Mikhail Naganoveae73eb2018-04-03 16:57:36 -07001381 }
1382}
1383
1384void AudioPolicyService::UidPolicy::updateOverrideUid(uid_t uid, bool active, bool insert) {
Eric Laurente8c8b432018-10-17 10:08:02 -07001385 updateUid(&mOverrideUids, uid, active, ActivityManager::PROCESS_STATE_UNKNOWN, insert);
1386}
1387
1388void AudioPolicyService::UidPolicy::notifyService() {
1389 sp<AudioPolicyService> service = mService.promote();
1390 if (service != nullptr) {
1391 service->updateUidStates();
Mikhail Naganoveae73eb2018-04-03 16:57:36 -07001392 }
1393}
1394
Eric Laurente8c8b432018-10-17 10:08:02 -07001395void AudioPolicyService::UidPolicy::updateUid(std::unordered_map<uid_t,
1396 std::pair<bool, int>> *uids,
1397 uid_t uid,
1398 bool active,
1399 int state,
1400 bool insert) {
1401 if (isServiceUid(uid)) {
1402 return;
1403 }
1404 bool wasActive = isUidActive(uid);
1405 int previousState = getUidState(uid);
Mikhail Naganoveae73eb2018-04-03 16:57:36 -07001406 {
1407 Mutex::Autolock _l(mLock);
Eric Laurente8c8b432018-10-17 10:08:02 -07001408 updateUidLocked(uids, uid, active, state, insert);
Mikhail Naganoveae73eb2018-04-03 16:57:36 -07001409 }
Eric Laurente8c8b432018-10-17 10:08:02 -07001410 if (wasActive != isUidActive(uid) || state != previousState) {
1411 notifyService();
1412 }
Mikhail Naganoveae73eb2018-04-03 16:57:36 -07001413}
1414
Eric Laurente8c8b432018-10-17 10:08:02 -07001415void AudioPolicyService::UidPolicy::updateUidLocked(std::unordered_map<uid_t,
1416 std::pair<bool, int>> *uids,
1417 uid_t uid,
1418 bool active,
1419 int state,
1420 bool insert) {
Mikhail Naganoveae73eb2018-04-03 16:57:36 -07001421 auto it = uids->find(uid);
1422 if (it != uids->end()) {
Mikhail Naganoveae73eb2018-04-03 16:57:36 -07001423 if (insert) {
Eric Laurente8c8b432018-10-17 10:08:02 -07001424 if (state == ActivityManager::PROCESS_STATE_UNKNOWN) {
1425 it->second.first = active;
1426 }
1427 if (it->second.first) {
1428 it->second.second = state;
1429 } else {
1430 it->second.second = ActivityManager::PROCESS_STATE_UNKNOWN;
1431 }
Mikhail Naganoveae73eb2018-04-03 16:57:36 -07001432 } else {
1433 uids->erase(it);
1434 }
Eric Laurente8c8b432018-10-17 10:08:02 -07001435 } else if (insert && (state == ActivityManager::PROCESS_STATE_UNKNOWN)) {
1436 uids->insert(std::pair<uid_t, std::pair<bool, int>>(uid,
1437 std::pair<bool, int>(active, state)));
Mikhail Naganoveae73eb2018-04-03 16:57:36 -07001438 }
Svet Ganovf4ddfef2018-01-16 07:37:58 -08001439}
Mathias Agopian65ab4712010-07-14 17:59:35 -07001440
Eric Laurent4eb58f12018-12-07 16:41:02 -08001441bool AudioPolicyService::UidPolicy::isA11yOnTop() {
1442 for (const auto &uid : mCachedUids) {
Eric Laurent47670c92019-08-28 16:59:05 -07001443 if (!isA11yUid(uid.first)) {
Eric Laurent4eb58f12018-12-07 16:41:02 -08001444 continue;
1445 }
Amith Yamasanibcbb3002019-01-23 13:53:33 -08001446 if (uid.second.second >= ActivityManager::PROCESS_STATE_TOP
1447 && uid.second.second <= ActivityManager::PROCESS_STATE_BOUND_FOREGROUND_SERVICE) {
Eric Laurent4eb58f12018-12-07 16:41:02 -08001448 return true;
1449 }
1450 }
1451 return false;
1452}
1453
Eric Laurentb78763e2018-10-17 10:08:02 -07001454bool AudioPolicyService::UidPolicy::isA11yUid(uid_t uid)
1455{
1456 std::vector<uid_t>::iterator it = find(mA11yUids.begin(), mA11yUids.end(), uid);
1457 return it != mA11yUids.end();
1458}
1459
Michael Groovercfd28302018-12-11 19:16:46 -08001460// ----------- AudioPolicyService::SensorPrivacyService implementation ----------
1461void AudioPolicyService::SensorPrivacyPolicy::registerSelf() {
1462 SensorPrivacyManager spm;
1463 mSensorPrivacyEnabled = spm.isSensorPrivacyEnabled();
1464 spm.addSensorPrivacyListener(this);
1465}
1466
Evan Severson241d9592021-01-08 12:16:02 -08001467void AudioPolicyService::SensorPrivacyPolicy::registerSelfForMicrophoneOnly(int userId) {
1468 SensorPrivacyManager spm;
1469 mSensorPrivacyEnabled = spm.isIndividualSensorPrivacyEnabled(userId,
1470 SensorPrivacyManager::INDIVIDUAL_SENSOR_MICROPHONE);
1471 spm.addIndividualSensorPrivacyListener(userId,
1472 SensorPrivacyManager::INDIVIDUAL_SENSOR_MICROPHONE, this);
1473}
1474
Michael Groovercfd28302018-12-11 19:16:46 -08001475void AudioPolicyService::SensorPrivacyPolicy::unregisterSelf() {
1476 SensorPrivacyManager spm;
1477 spm.removeSensorPrivacyListener(this);
1478}
1479
1480bool AudioPolicyService::SensorPrivacyPolicy::isSensorPrivacyEnabled() {
1481 return mSensorPrivacyEnabled;
1482}
1483
1484binder::Status AudioPolicyService::SensorPrivacyPolicy::onSensorPrivacyChanged(bool enabled) {
1485 mSensorPrivacyEnabled = enabled;
1486 sp<AudioPolicyService> service = mService.promote();
1487 if (service != nullptr) {
1488 service->updateUidStates();
1489 }
1490 return binder::Status::ok();
1491}
1492
Eric Laurented726cc2021-07-01 14:26:41 +02001493// ----------- AudioPolicyService::OpRecordAudioMonitor implementation ----------
1494
1495// static
1496sp<AudioPolicyService::OpRecordAudioMonitor>
1497AudioPolicyService::OpRecordAudioMonitor::createIfNeeded(
1498 const AttributionSourceState& attributionSource, const audio_attributes_t& attr,
1499 wp<AudioCommandThread> commandThread)
1500{
Eric Laurent987ce102021-07-05 12:11:51 +02001501 if (isAudioServerOrRootUid(attributionSource.uid)) {
1502 ALOGV("not silencing record for audio or root source %s",
Eric Laurented726cc2021-07-01 14:26:41 +02001503 attributionSource.toString().c_str());
1504 return nullptr;
1505 }
1506
1507 if (!AudioPolicyService::isAppOpSource(attr.source)) {
1508 ALOGD("not monitoring app op for uid %d and source %d",
1509 attributionSource.uid, attr.source);
1510 return nullptr;
1511 }
1512
1513 if (!attributionSource.packageName.has_value()
1514 || attributionSource.packageName.value().size() == 0) {
1515 return nullptr;
1516 }
1517 return new OpRecordAudioMonitor(attributionSource, getOpForSource(attr.source), commandThread);
1518}
1519
1520AudioPolicyService::OpRecordAudioMonitor::OpRecordAudioMonitor(
1521 const AttributionSourceState& attributionSource, int32_t appOp,
1522 wp<AudioCommandThread> commandThread) :
1523 mHasOp(true), mAttributionSource(attributionSource), mAppOp(appOp),
1524 mCommandThread(commandThread)
1525{
1526}
1527
1528AudioPolicyService::OpRecordAudioMonitor::~OpRecordAudioMonitor()
1529{
1530 if (mOpCallback != 0) {
1531 mAppOpsManager.stopWatchingMode(mOpCallback);
1532 }
1533 mOpCallback.clear();
1534}
1535
1536void AudioPolicyService::OpRecordAudioMonitor::onFirstRef()
1537{
1538 checkOp();
1539 mOpCallback = new RecordAudioOpCallback(this);
1540 ALOGV("start watching op %d for %s", mAppOp, mAttributionSource.toString().c_str());
1541 // TODO: We need to always watch AppOpsManager::OP_RECORD_AUDIO too
1542 // since it controls the mic permission for legacy apps.
1543 mAppOpsManager.startWatchingMode(mAppOp, VALUE_OR_FATAL(aidl2legacy_string_view_String16(
1544 mAttributionSource.packageName.value_or(""))),
1545 mOpCallback);
1546}
1547
1548bool AudioPolicyService::OpRecordAudioMonitor::hasOp() const {
1549 return mHasOp.load();
1550}
1551
1552// Called by RecordAudioOpCallback when the app op corresponding to this OpRecordAudioMonitor
1553// is updated in AppOp callback and in onFirstRef()
1554// Note this method is never called (and never to be) for audio server / root track
1555// due to the UID in createIfNeeded(). As a result for those record track, it's:
1556// - not called from constructor,
1557// - not called from RecordAudioOpCallback because the callback is not installed in this case
1558void AudioPolicyService::OpRecordAudioMonitor::checkOp(bool updateUidStates)
1559{
1560 // TODO: We need to always check AppOpsManager::OP_RECORD_AUDIO too
1561 // since it controls the mic permission for legacy apps.
1562 const int32_t mode = mAppOpsManager.checkOp(mAppOp,
1563 mAttributionSource.uid, VALUE_OR_FATAL(aidl2legacy_string_view_String16(
1564 mAttributionSource.packageName.value_or(""))));
1565 const bool hasIt = (mode == AppOpsManager::MODE_ALLOWED);
1566 // verbose logging only log when appOp changed
1567 ALOGI_IF(hasIt != mHasOp.load(),
1568 "App op %d missing, %ssilencing record %s",
1569 mAppOp, hasIt ? "un" : "", mAttributionSource.toString().c_str());
1570 mHasOp.store(hasIt);
1571
1572 if (updateUidStates) {
1573 sp<AudioCommandThread> commandThread = mCommandThread.promote();
1574 if (commandThread != nullptr) {
1575 commandThread->updateUidStatesCommand();
1576 }
1577 }
1578}
1579
1580AudioPolicyService::OpRecordAudioMonitor::RecordAudioOpCallback::RecordAudioOpCallback(
1581 const wp<OpRecordAudioMonitor>& monitor) : mMonitor(monitor)
1582{ }
1583
1584void AudioPolicyService::OpRecordAudioMonitor::RecordAudioOpCallback::opChanged(int32_t op,
1585 const String16& packageName __unused) {
1586 sp<OpRecordAudioMonitor> monitor = mMonitor.promote();
1587 if (monitor != NULL) {
1588 if (op != monitor->getOp()) {
1589 return;
1590 }
1591 monitor->checkOp(true);
1592 }
1593}
1594
1595
Mathias Agopian65ab4712010-07-14 17:59:35 -07001596// ----------- AudioPolicyService::AudioCommandThread implementation ----------
1597
Eric Laurentbfb1b832013-01-07 09:53:42 -08001598AudioPolicyService::AudioCommandThread::AudioCommandThread(String8 name,
1599 const wp<AudioPolicyService>& service)
1600 : Thread(false), mName(name), mService(service)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001601{
Mathias Agopian65ab4712010-07-14 17:59:35 -07001602}
1603
1604
1605AudioPolicyService::AudioCommandThread::~AudioCommandThread()
1606{
Eric Laurentbfb1b832013-01-07 09:53:42 -08001607 if (!mAudioCommands.isEmpty()) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07001608 release_wake_lock(mName.string());
1609 }
1610 mAudioCommands.clear();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001611}
1612
1613void AudioPolicyService::AudioCommandThread::onFirstRef()
1614{
Eric Laurentbfb1b832013-01-07 09:53:42 -08001615 run(mName.string(), ANDROID_PRIORITY_AUDIO);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001616}
1617
1618bool AudioPolicyService::AudioCommandThread::threadLoop()
1619{
Eric Laurentd7eda8d2016-02-02 17:18:39 -08001620 nsecs_t waitTime = -1;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001621
1622 mLock.lock();
1623 while (!exitPending())
1624 {
Eric Laurent59a89232014-06-08 14:14:17 -07001625 sp<AudioPolicyService> svc;
1626 while (!mAudioCommands.isEmpty() && !exitPending()) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07001627 nsecs_t curTime = systemTime();
1628 // commands are sorted by increasing time stamp: execute them from index 0 and up
1629 if (mAudioCommands[0]->mTime <= curTime) {
Eric Laurent0ede8922014-05-09 18:04:42 -07001630 sp<AudioCommand> command = mAudioCommands[0];
Mathias Agopian65ab4712010-07-14 17:59:35 -07001631 mAudioCommands.removeAt(0);
Eric Laurent0ede8922014-05-09 18:04:42 -07001632 mLastCommand = command;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001633
1634 switch (command->mCommand) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07001635 case SET_VOLUME: {
Eric Laurent0ede8922014-05-09 18:04:42 -07001636 VolumeData *data = (VolumeData *)command->mParam.get();
Steve Block3856b092011-10-20 11:56:00 +01001637 ALOGV("AudioCommandThread() processing set volume stream %d, \
Eric Laurentde070132010-07-13 04:45:46 -07001638 volume %f, output %d", data->mStream, data->mVolume, data->mIO);
Andy Hungfe726a62018-09-27 15:17:25 -07001639 mLock.unlock();
Eric Laurentde070132010-07-13 04:45:46 -07001640 command->mStatus = AudioSystem::setStreamVolume(data->mStream,
1641 data->mVolume,
1642 data->mIO);
Andy Hungfe726a62018-09-27 15:17:25 -07001643 mLock.lock();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001644 }break;
1645 case SET_PARAMETERS: {
Eric Laurent0ede8922014-05-09 18:04:42 -07001646 ParametersData *data = (ParametersData *)command->mParam.get();
Glenn Kastene53b9ea2012-03-12 16:29:55 -07001647 ALOGV("AudioCommandThread() processing set parameters string %s, io %d",
1648 data->mKeyValuePairs.string(), data->mIO);
Andy Hungfe726a62018-09-27 15:17:25 -07001649 mLock.unlock();
Glenn Kastene53b9ea2012-03-12 16:29:55 -07001650 command->mStatus = AudioSystem::setParameters(data->mIO, data->mKeyValuePairs);
Andy Hungfe726a62018-09-27 15:17:25 -07001651 mLock.lock();
Glenn Kastene53b9ea2012-03-12 16:29:55 -07001652 }break;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001653 case SET_VOICE_VOLUME: {
Eric Laurent0ede8922014-05-09 18:04:42 -07001654 VoiceVolumeData *data = (VoiceVolumeData *)command->mParam.get();
Steve Block3856b092011-10-20 11:56:00 +01001655 ALOGV("AudioCommandThread() processing set voice volume volume %f",
Eric Laurentde070132010-07-13 04:45:46 -07001656 data->mVolume);
Andy Hungfe726a62018-09-27 15:17:25 -07001657 mLock.unlock();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001658 command->mStatus = AudioSystem::setVoiceVolume(data->mVolume);
Andy Hungfe726a62018-09-27 15:17:25 -07001659 mLock.lock();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001660 }break;
Eric Laurentbfb1b832013-01-07 09:53:42 -08001661 case STOP_OUTPUT: {
Eric Laurent0ede8922014-05-09 18:04:42 -07001662 StopOutputData *data = (StopOutputData *)command->mParam.get();
Eric Laurentd7fe0862018-07-14 16:48:01 -07001663 ALOGV("AudioCommandThread() processing stop output portId %d",
1664 data->mPortId);
Eric Laurent59a89232014-06-08 14:14:17 -07001665 svc = mService.promote();
Eric Laurentbfb1b832013-01-07 09:53:42 -08001666 if (svc == 0) {
1667 break;
1668 }
1669 mLock.unlock();
Eric Laurentd7fe0862018-07-14 16:48:01 -07001670 svc->doStopOutput(data->mPortId);
Eric Laurentbfb1b832013-01-07 09:53:42 -08001671 mLock.lock();
Eric Laurentbfb1b832013-01-07 09:53:42 -08001672 }break;
1673 case RELEASE_OUTPUT: {
Eric Laurent0ede8922014-05-09 18:04:42 -07001674 ReleaseOutputData *data = (ReleaseOutputData *)command->mParam.get();
Eric Laurentd7fe0862018-07-14 16:48:01 -07001675 ALOGV("AudioCommandThread() processing release output portId %d",
1676 data->mPortId);
Eric Laurent59a89232014-06-08 14:14:17 -07001677 svc = mService.promote();
Eric Laurentbfb1b832013-01-07 09:53:42 -08001678 if (svc == 0) {
1679 break;
1680 }
1681 mLock.unlock();
Eric Laurentd7fe0862018-07-14 16:48:01 -07001682 svc->doReleaseOutput(data->mPortId);
Eric Laurentbfb1b832013-01-07 09:53:42 -08001683 mLock.lock();
Eric Laurentbfb1b832013-01-07 09:53:42 -08001684 }break;
Eric Laurent951f4552014-05-20 10:48:17 -07001685 case CREATE_AUDIO_PATCH: {
1686 CreateAudioPatchData *data = (CreateAudioPatchData *)command->mParam.get();
1687 ALOGV("AudioCommandThread() processing create audio patch");
1688 sp<IAudioFlinger> af = AudioSystem::get_audio_flinger();
1689 if (af == 0) {
1690 command->mStatus = PERMISSION_DENIED;
1691 } else {
Andy Hungfe726a62018-09-27 15:17:25 -07001692 mLock.unlock();
Eric Laurent951f4552014-05-20 10:48:17 -07001693 command->mStatus = af->createAudioPatch(&data->mPatch, &data->mHandle);
Andy Hungfe726a62018-09-27 15:17:25 -07001694 mLock.lock();
Eric Laurent951f4552014-05-20 10:48:17 -07001695 }
1696 } break;
1697 case RELEASE_AUDIO_PATCH: {
1698 ReleaseAudioPatchData *data = (ReleaseAudioPatchData *)command->mParam.get();
1699 ALOGV("AudioCommandThread() processing release audio patch");
1700 sp<IAudioFlinger> af = AudioSystem::get_audio_flinger();
1701 if (af == 0) {
1702 command->mStatus = PERMISSION_DENIED;
1703 } else {
Andy Hungfe726a62018-09-27 15:17:25 -07001704 mLock.unlock();
Eric Laurent951f4552014-05-20 10:48:17 -07001705 command->mStatus = af->releaseAudioPatch(data->mHandle);
Andy Hungfe726a62018-09-27 15:17:25 -07001706 mLock.lock();
Eric Laurent951f4552014-05-20 10:48:17 -07001707 }
1708 } break;
Eric Laurentb52c1522014-05-20 11:27:36 -07001709 case UPDATE_AUDIOPORT_LIST: {
1710 ALOGV("AudioCommandThread() processing update audio port list");
Eric Laurent59a89232014-06-08 14:14:17 -07001711 svc = mService.promote();
Eric Laurentb52c1522014-05-20 11:27:36 -07001712 if (svc == 0) {
1713 break;
1714 }
1715 mLock.unlock();
1716 svc->doOnAudioPortListUpdate();
1717 mLock.lock();
1718 }break;
1719 case UPDATE_AUDIOPATCH_LIST: {
1720 ALOGV("AudioCommandThread() processing update audio patch list");
Eric Laurent59a89232014-06-08 14:14:17 -07001721 svc = mService.promote();
Eric Laurentb52c1522014-05-20 11:27:36 -07001722 if (svc == 0) {
1723 break;
1724 }
1725 mLock.unlock();
1726 svc->doOnAudioPatchListUpdate();
1727 mLock.lock();
1728 }break;
François Gaffiecfe17322018-11-07 13:41:29 +01001729 case CHANGED_AUDIOVOLUMEGROUP: {
1730 AudioVolumeGroupData *data =
1731 static_cast<AudioVolumeGroupData *>(command->mParam.get());
1732 ALOGV("AudioCommandThread() processing update audio volume group");
1733 svc = mService.promote();
1734 if (svc == 0) {
1735 break;
1736 }
1737 mLock.unlock();
1738 svc->doOnAudioVolumeGroupChanged(data->mGroup, data->mFlags);
1739 mLock.lock();
1740 }break;
Eric Laurente1715a42014-05-20 11:30:42 -07001741 case SET_AUDIOPORT_CONFIG: {
1742 SetAudioPortConfigData *data = (SetAudioPortConfigData *)command->mParam.get();
1743 ALOGV("AudioCommandThread() processing set port config");
1744 sp<IAudioFlinger> af = AudioSystem::get_audio_flinger();
1745 if (af == 0) {
1746 command->mStatus = PERMISSION_DENIED;
1747 } else {
Andy Hungfe726a62018-09-27 15:17:25 -07001748 mLock.unlock();
Eric Laurente1715a42014-05-20 11:30:42 -07001749 command->mStatus = af->setAudioPortConfig(&data->mConfig);
Andy Hungfe726a62018-09-27 15:17:25 -07001750 mLock.lock();
Eric Laurente1715a42014-05-20 11:30:42 -07001751 }
1752 } break;
Jean-Michel Trivide801052015-04-14 19:10:14 -07001753 case DYN_POLICY_MIX_STATE_UPDATE: {
1754 DynPolicyMixStateUpdateData *data =
1755 (DynPolicyMixStateUpdateData *)command->mParam.get();
Jean-Michel Trivide801052015-04-14 19:10:14 -07001756 ALOGV("AudioCommandThread() processing dyn policy mix state update %s %d",
1757 data->mRegId.string(), data->mState);
1758 svc = mService.promote();
1759 if (svc == 0) {
1760 break;
1761 }
1762 mLock.unlock();
1763 svc->doOnDynamicPolicyMixStateUpdate(data->mRegId, data->mState);
1764 mLock.lock();
1765 } break;
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -08001766 case RECORDING_CONFIGURATION_UPDATE: {
1767 RecordingConfigurationUpdateData *data =
1768 (RecordingConfigurationUpdateData *)command->mParam.get();
1769 ALOGV("AudioCommandThread() processing recording configuration update");
1770 svc = mService.promote();
1771 if (svc == 0) {
1772 break;
1773 }
1774 mLock.unlock();
Jean-Michel Triviac4e4292016-12-22 11:39:31 -08001775 svc->doOnRecordingConfigurationUpdate(data->mEvent, &data->mClientInfo,
Eric Laurenta9f86652018-11-28 17:23:11 -08001776 &data->mClientConfig, data->mClientEffects,
1777 &data->mDeviceConfig, data->mEffects,
1778 data->mPatchHandle, data->mSource);
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -08001779 mLock.lock();
1780 } break;
Eric Laurentb20cf7d2019-04-05 19:37:34 -07001781 case SET_EFFECT_SUSPENDED: {
1782 SetEffectSuspendedData *data = (SetEffectSuspendedData *)command->mParam.get();
1783 ALOGV("AudioCommandThread() processing set effect suspended");
1784 sp<IAudioFlinger> af = AudioSystem::get_audio_flinger();
1785 if (af != 0) {
1786 mLock.unlock();
1787 af->setEffectSuspended(data->mEffectId, data->mSessionId, data->mSuspended);
1788 mLock.lock();
1789 }
1790 } break;
Mikhail Naganov88b30d22020-03-09 19:43:13 +00001791 case AUDIO_MODULES_UPDATE: {
1792 ALOGV("AudioCommandThread() processing audio modules update");
1793 svc = mService.promote();
1794 if (svc == 0) {
1795 break;
1796 }
1797 mLock.unlock();
1798 svc->doOnNewAudioModulesAvailable();
1799 mLock.lock();
1800 } break;
Jean-Michel Trivi9a6b9ad2020-10-22 16:46:43 -07001801 case ROUTING_UPDATED: {
1802 ALOGV("AudioCommandThread() processing routing update");
1803 svc = mService.promote();
1804 if (svc == 0) {
1805 break;
1806 }
1807 mLock.unlock();
1808 svc->doOnRoutingUpdated();
1809 mLock.lock();
1810 } break;
Eric Laurentb20cf7d2019-04-05 19:37:34 -07001811
Eric Laurented726cc2021-07-01 14:26:41 +02001812 case UPDATE_UID_STATES: {
1813 ALOGV("AudioCommandThread() processing updateUID states");
1814 svc = mService.promote();
1815 if (svc == 0) {
1816 break;
1817 }
1818 mLock.unlock();
1819 svc->updateUidStates();
1820 mLock.lock();
1821 } break;
1822
Eric Laurent81dd0f52021-07-05 11:54:40 +02001823 case CHECK_SPATIALIZER: {
1824 ALOGV("AudioCommandThread() processing updateUID states");
1825 svc = mService.promote();
1826 if (svc == 0) {
1827 break;
1828 }
1829 mLock.unlock();
1830 svc->doOnCheckSpatializer();
1831 mLock.lock();
1832 } break;
1833
Mathias Agopian65ab4712010-07-14 17:59:35 -07001834 default:
Steve Block5ff1dd52012-01-05 23:22:43 +00001835 ALOGW("AudioCommandThread() unknown command %d", command->mCommand);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001836 }
Eric Laurent0ede8922014-05-09 18:04:42 -07001837 {
1838 Mutex::Autolock _l(command->mLock);
1839 if (command->mWaitStatus) {
1840 command->mWaitStatus = false;
1841 command->mCond.signal();
1842 }
1843 }
Eric Laurentd7eda8d2016-02-02 17:18:39 -08001844 waitTime = -1;
Zach Janga754b4f2015-10-27 01:29:34 +00001845 // release mLock before releasing strong reference on the service as
1846 // AudioPolicyService destructor calls AudioCommandThread::exit() which
1847 // acquires mLock.
1848 mLock.unlock();
1849 svc.clear();
1850 mLock.lock();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001851 } else {
1852 waitTime = mAudioCommands[0]->mTime - curTime;
1853 break;
1854 }
1855 }
Zach Janga754b4f2015-10-27 01:29:34 +00001856
1857 // release delayed commands wake lock if the queue is empty
1858 if (mAudioCommands.isEmpty()) {
Ricardo Garcia05f2fdc2014-07-24 15:48:24 -07001859 release_wake_lock(mName.string());
Zach Janga754b4f2015-10-27 01:29:34 +00001860 }
1861
1862 // At this stage we have either an empty command queue or the first command in the queue
1863 // has a finite delay. So unless we are exiting it is safe to wait.
1864 if (!exitPending()) {
Eric Laurent59a89232014-06-08 14:14:17 -07001865 ALOGV("AudioCommandThread() going to sleep");
Eric Laurentd7eda8d2016-02-02 17:18:39 -08001866 if (waitTime == -1) {
1867 mWaitWorkCV.wait(mLock);
1868 } else {
1869 mWaitWorkCV.waitRelative(mLock, waitTime);
1870 }
Eric Laurent59a89232014-06-08 14:14:17 -07001871 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001872 }
Ricardo Garcia05f2fdc2014-07-24 15:48:24 -07001873 // release delayed commands wake lock before quitting
1874 if (!mAudioCommands.isEmpty()) {
1875 release_wake_lock(mName.string());
1876 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001877 mLock.unlock();
1878 return false;
1879}
1880
1881status_t AudioPolicyService::AudioCommandThread::dump(int fd)
1882{
1883 const size_t SIZE = 256;
1884 char buffer[SIZE];
1885 String8 result;
1886
1887 snprintf(buffer, SIZE, "AudioCommandThread %p Dump\n", this);
1888 result.append(buffer);
1889 write(fd, result.string(), result.size());
1890
Mikhail Naganov12b716c2020-04-30 22:37:43 +00001891 const bool locked = dumpTryLock(mLock);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001892 if (!locked) {
1893 String8 result2(kCmdDeadlockedString);
1894 write(fd, result2.string(), result2.size());
1895 }
1896
1897 snprintf(buffer, SIZE, "- Commands:\n");
1898 result = String8(buffer);
1899 result.append(" Command Time Wait pParam\n");
Glenn Kasten8d6a2442012-02-08 14:04:28 -08001900 for (size_t i = 0; i < mAudioCommands.size(); i++) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07001901 mAudioCommands[i]->dump(buffer, SIZE);
1902 result.append(buffer);
1903 }
1904 result.append(" Last Command\n");
Eric Laurent0ede8922014-05-09 18:04:42 -07001905 if (mLastCommand != 0) {
1906 mLastCommand->dump(buffer, SIZE);
1907 result.append(buffer);
1908 } else {
1909 result.append(" none\n");
1910 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001911
1912 write(fd, result.string(), result.size());
1913
Mikhail Naganov12b716c2020-04-30 22:37:43 +00001914 dumpReleaseLock(mLock, locked);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001915
1916 return NO_ERROR;
1917}
1918
Glenn Kastenfff6d712012-01-12 16:38:12 -08001919status_t AudioPolicyService::AudioCommandThread::volumeCommand(audio_stream_type_t stream,
Eric Laurentde070132010-07-13 04:45:46 -07001920 float volume,
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001921 audio_io_handle_t output,
Eric Laurentde070132010-07-13 04:45:46 -07001922 int delayMs)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001923{
Eric Laurent0ede8922014-05-09 18:04:42 -07001924 sp<AudioCommand> command = new AudioCommand();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001925 command->mCommand = SET_VOLUME;
Eric Laurent0ede8922014-05-09 18:04:42 -07001926 sp<VolumeData> data = new VolumeData();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001927 data->mStream = stream;
1928 data->mVolume = volume;
1929 data->mIO = output;
1930 command->mParam = data;
Eric Laurent0ede8922014-05-09 18:04:42 -07001931 command->mWaitStatus = true;
Steve Block3856b092011-10-20 11:56:00 +01001932 ALOGV("AudioCommandThread() adding set volume stream %d, volume %f, output %d",
Eric Laurentde070132010-07-13 04:45:46 -07001933 stream, volume, output);
Eric Laurent0ede8922014-05-09 18:04:42 -07001934 return sendCommand(command, delayMs);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001935}
1936
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001937status_t AudioPolicyService::AudioCommandThread::parametersCommand(audio_io_handle_t ioHandle,
Dima Zavinfce7a472011-04-19 22:30:36 -07001938 const char *keyValuePairs,
Eric Laurentde070132010-07-13 04:45:46 -07001939 int delayMs)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001940{
Eric Laurent0ede8922014-05-09 18:04:42 -07001941 sp<AudioCommand> command = new AudioCommand();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001942 command->mCommand = SET_PARAMETERS;
Eric Laurent0ede8922014-05-09 18:04:42 -07001943 sp<ParametersData> data = new ParametersData();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001944 data->mIO = ioHandle;
Dima Zavinfce7a472011-04-19 22:30:36 -07001945 data->mKeyValuePairs = String8(keyValuePairs);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001946 command->mParam = data;
Eric Laurent0ede8922014-05-09 18:04:42 -07001947 command->mWaitStatus = true;
Steve Block3856b092011-10-20 11:56:00 +01001948 ALOGV("AudioCommandThread() adding set parameter string %s, io %d ,delay %d",
Dima Zavinfce7a472011-04-19 22:30:36 -07001949 keyValuePairs, ioHandle, delayMs);
Eric Laurent0ede8922014-05-09 18:04:42 -07001950 return sendCommand(command, delayMs);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001951}
1952
1953status_t AudioPolicyService::AudioCommandThread::voiceVolumeCommand(float volume, int delayMs)
1954{
Eric Laurent0ede8922014-05-09 18:04:42 -07001955 sp<AudioCommand> command = new AudioCommand();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001956 command->mCommand = SET_VOICE_VOLUME;
Eric Laurent0ede8922014-05-09 18:04:42 -07001957 sp<VoiceVolumeData> data = new VoiceVolumeData();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001958 data->mVolume = volume;
1959 command->mParam = data;
Eric Laurent0ede8922014-05-09 18:04:42 -07001960 command->mWaitStatus = true;
Steve Block3856b092011-10-20 11:56:00 +01001961 ALOGV("AudioCommandThread() adding set voice volume volume %f", volume);
Eric Laurent0ede8922014-05-09 18:04:42 -07001962 return sendCommand(command, delayMs);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001963}
1964
Eric Laurentb20cf7d2019-04-05 19:37:34 -07001965void AudioPolicyService::AudioCommandThread::setEffectSuspendedCommand(int effectId,
1966 audio_session_t sessionId,
1967 bool suspended)
1968{
1969 sp<AudioCommand> command = new AudioCommand();
1970 command->mCommand = SET_EFFECT_SUSPENDED;
1971 sp<SetEffectSuspendedData> data = new SetEffectSuspendedData();
1972 data->mEffectId = effectId;
1973 data->mSessionId = sessionId;
1974 data->mSuspended = suspended;
1975 command->mParam = data;
1976 ALOGV("AudioCommandThread() adding set suspended effectId %d sessionId %d suspended %d",
1977 effectId, sessionId, suspended);
1978 sendCommand(command);
1979}
1980
1981
Eric Laurentd7fe0862018-07-14 16:48:01 -07001982void AudioPolicyService::AudioCommandThread::stopOutputCommand(audio_port_handle_t portId)
Eric Laurentbfb1b832013-01-07 09:53:42 -08001983{
Eric Laurent0ede8922014-05-09 18:04:42 -07001984 sp<AudioCommand> command = new AudioCommand();
Eric Laurentbfb1b832013-01-07 09:53:42 -08001985 command->mCommand = STOP_OUTPUT;
Eric Laurent0ede8922014-05-09 18:04:42 -07001986 sp<StopOutputData> data = new StopOutputData();
Eric Laurentd7fe0862018-07-14 16:48:01 -07001987 data->mPortId = portId;
Jesper Tragardh48412dc2014-03-24 14:12:43 +01001988 command->mParam = data;
Eric Laurentd7fe0862018-07-14 16:48:01 -07001989 ALOGV("AudioCommandThread() adding stop output portId %d", portId);
Eric Laurent0ede8922014-05-09 18:04:42 -07001990 sendCommand(command);
Eric Laurentbfb1b832013-01-07 09:53:42 -08001991}
1992
Eric Laurentd7fe0862018-07-14 16:48:01 -07001993void AudioPolicyService::AudioCommandThread::releaseOutputCommand(audio_port_handle_t portId)
Eric Laurentbfb1b832013-01-07 09:53:42 -08001994{
Eric Laurent0ede8922014-05-09 18:04:42 -07001995 sp<AudioCommand> command = new AudioCommand();
Eric Laurentbfb1b832013-01-07 09:53:42 -08001996 command->mCommand = RELEASE_OUTPUT;
Eric Laurent0ede8922014-05-09 18:04:42 -07001997 sp<ReleaseOutputData> data = new ReleaseOutputData();
Eric Laurentd7fe0862018-07-14 16:48:01 -07001998 data->mPortId = portId;
Jesper Tragardh48412dc2014-03-24 14:12:43 +01001999 command->mParam = data;
Eric Laurentd7fe0862018-07-14 16:48:01 -07002000 ALOGV("AudioCommandThread() adding release output portId %d", portId);
Eric Laurent0ede8922014-05-09 18:04:42 -07002001 sendCommand(command);
2002}
2003
Eric Laurent951f4552014-05-20 10:48:17 -07002004status_t AudioPolicyService::AudioCommandThread::createAudioPatchCommand(
2005 const struct audio_patch *patch,
2006 audio_patch_handle_t *handle,
2007 int delayMs)
2008{
2009 status_t status = NO_ERROR;
2010
2011 sp<AudioCommand> command = new AudioCommand();
2012 command->mCommand = CREATE_AUDIO_PATCH;
2013 CreateAudioPatchData *data = new CreateAudioPatchData();
2014 data->mPatch = *patch;
2015 data->mHandle = *handle;
2016 command->mParam = data;
2017 command->mWaitStatus = true;
2018 ALOGV("AudioCommandThread() adding create patch delay %d", delayMs);
2019 status = sendCommand(command, delayMs);
2020 if (status == NO_ERROR) {
2021 *handle = data->mHandle;
2022 }
2023 return status;
2024}
2025
2026status_t AudioPolicyService::AudioCommandThread::releaseAudioPatchCommand(audio_patch_handle_t handle,
2027 int delayMs)
2028{
2029 sp<AudioCommand> command = new AudioCommand();
2030 command->mCommand = RELEASE_AUDIO_PATCH;
2031 ReleaseAudioPatchData *data = new ReleaseAudioPatchData();
2032 data->mHandle = handle;
2033 command->mParam = data;
2034 command->mWaitStatus = true;
2035 ALOGV("AudioCommandThread() adding release patch delay %d", delayMs);
2036 return sendCommand(command, delayMs);
2037}
2038
Eric Laurentb52c1522014-05-20 11:27:36 -07002039void AudioPolicyService::AudioCommandThread::updateAudioPortListCommand()
2040{
2041 sp<AudioCommand> command = new AudioCommand();
2042 command->mCommand = UPDATE_AUDIOPORT_LIST;
2043 ALOGV("AudioCommandThread() adding update audio port list");
2044 sendCommand(command);
2045}
2046
Eric Laurented726cc2021-07-01 14:26:41 +02002047void AudioPolicyService::AudioCommandThread::updateUidStatesCommand()
2048{
2049 sp<AudioCommand> command = new AudioCommand();
2050 command->mCommand = UPDATE_UID_STATES;
2051 ALOGV("AudioCommandThread() adding update UID states");
2052 sendCommand(command);
2053}
2054
Eric Laurentb52c1522014-05-20 11:27:36 -07002055void AudioPolicyService::AudioCommandThread::updateAudioPatchListCommand()
2056{
2057 sp<AudioCommand>command = new AudioCommand();
2058 command->mCommand = UPDATE_AUDIOPATCH_LIST;
2059 ALOGV("AudioCommandThread() adding update audio patch list");
2060 sendCommand(command);
2061}
2062
François Gaffiecfe17322018-11-07 13:41:29 +01002063void AudioPolicyService::AudioCommandThread::changeAudioVolumeGroupCommand(volume_group_t group,
2064 int flags)
2065{
2066 sp<AudioCommand>command = new AudioCommand();
2067 command->mCommand = CHANGED_AUDIOVOLUMEGROUP;
2068 AudioVolumeGroupData *data= new AudioVolumeGroupData();
2069 data->mGroup = group;
2070 data->mFlags = flags;
2071 command->mParam = data;
2072 ALOGV("AudioCommandThread() adding audio volume group changed");
2073 sendCommand(command);
2074}
2075
Eric Laurente1715a42014-05-20 11:30:42 -07002076status_t AudioPolicyService::AudioCommandThread::setAudioPortConfigCommand(
2077 const struct audio_port_config *config, int delayMs)
2078{
2079 sp<AudioCommand> command = new AudioCommand();
2080 command->mCommand = SET_AUDIOPORT_CONFIG;
2081 SetAudioPortConfigData *data = new SetAudioPortConfigData();
2082 data->mConfig = *config;
2083 command->mParam = data;
2084 command->mWaitStatus = true;
2085 ALOGV("AudioCommandThread() adding set port config delay %d", delayMs);
2086 return sendCommand(command, delayMs);
2087}
2088
Jean-Michel Trivide801052015-04-14 19:10:14 -07002089void AudioPolicyService::AudioCommandThread::dynamicPolicyMixStateUpdateCommand(
Chih-Hung Hsiehe964d4e2016-08-09 14:31:32 -07002090 const String8& regId, int32_t state)
Jean-Michel Trivide801052015-04-14 19:10:14 -07002091{
2092 sp<AudioCommand> command = new AudioCommand();
2093 command->mCommand = DYN_POLICY_MIX_STATE_UPDATE;
2094 DynPolicyMixStateUpdateData *data = new DynPolicyMixStateUpdateData();
2095 data->mRegId = regId;
2096 data->mState = state;
2097 command->mParam = data;
2098 ALOGV("AudioCommandThread() sending dynamic policy mix (id=%s) state update to %d",
2099 regId.string(), state);
2100 sendCommand(command);
2101}
2102
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -08002103void AudioPolicyService::AudioCommandThread::recordingConfigurationUpdateCommand(
Eric Laurenta9f86652018-11-28 17:23:11 -08002104 int event,
2105 const record_client_info_t *clientInfo,
2106 const audio_config_base_t *clientConfig,
2107 std::vector<effect_descriptor_t> clientEffects,
2108 const audio_config_base_t *deviceConfig,
2109 std::vector<effect_descriptor_t> effects,
2110 audio_patch_handle_t patchHandle,
2111 audio_source_t source)
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -08002112{
2113 sp<AudioCommand>command = new AudioCommand();
2114 command->mCommand = RECORDING_CONFIGURATION_UPDATE;
2115 RecordingConfigurationUpdateData *data = new RecordingConfigurationUpdateData();
2116 data->mEvent = event;
Jean-Michel Triviac4e4292016-12-22 11:39:31 -08002117 data->mClientInfo = *clientInfo;
Jean-Michel Trivi7281aa92016-02-17 15:33:40 -08002118 data->mClientConfig = *clientConfig;
Eric Laurenta9f86652018-11-28 17:23:11 -08002119 data->mClientEffects = clientEffects;
Jean-Michel Trivi7281aa92016-02-17 15:33:40 -08002120 data->mDeviceConfig = *deviceConfig;
Eric Laurenta9f86652018-11-28 17:23:11 -08002121 data->mEffects = effects;
Jean-Michel Trivi8c7cf3b2016-02-25 17:08:24 -08002122 data->mPatchHandle = patchHandle;
Eric Laurenta9f86652018-11-28 17:23:11 -08002123 data->mSource = source;
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -08002124 command->mParam = data;
Jean-Michel Triviac4e4292016-12-22 11:39:31 -08002125 ALOGV("AudioCommandThread() adding recording configuration update event %d, source %d uid %u",
2126 event, clientInfo->source, clientInfo->uid);
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -08002127 sendCommand(command);
2128}
2129
Mikhail Naganov88b30d22020-03-09 19:43:13 +00002130void AudioPolicyService::AudioCommandThread::audioModulesUpdateCommand()
2131{
2132 sp<AudioCommand> command = new AudioCommand();
2133 command->mCommand = AUDIO_MODULES_UPDATE;
2134 sendCommand(command);
2135}
2136
Jean-Michel Trivi9a6b9ad2020-10-22 16:46:43 -07002137void AudioPolicyService::AudioCommandThread::routingChangedCommand()
2138{
2139 sp<AudioCommand>command = new AudioCommand();
2140 command->mCommand = ROUTING_UPDATED;
2141 ALOGV("AudioCommandThread() adding routing update");
2142 sendCommand(command);
2143}
2144
Eric Laurent81dd0f52021-07-05 11:54:40 +02002145void AudioPolicyService::AudioCommandThread::checkSpatializerCommand()
2146{
2147 sp<AudioCommand>command = new AudioCommand();
2148 command->mCommand = CHECK_SPATIALIZER;
2149 ALOGV("AudioCommandThread() adding check spatializer");
2150 sendCommand(command);
2151}
2152
Eric Laurent0ede8922014-05-09 18:04:42 -07002153status_t AudioPolicyService::AudioCommandThread::sendCommand(sp<AudioCommand>& command, int delayMs)
2154{
2155 {
2156 Mutex::Autolock _l(mLock);
2157 insertCommand_l(command, delayMs);
2158 mWaitWorkCV.signal();
2159 }
2160 Mutex::Autolock _l(command->mLock);
2161 while (command->mWaitStatus) {
2162 nsecs_t timeOutNs = kAudioCommandTimeoutNs + milliseconds(delayMs);
2163 if (command->mCond.waitRelative(command->mLock, timeOutNs) != NO_ERROR) {
2164 command->mStatus = TIMED_OUT;
2165 command->mWaitStatus = false;
2166 }
2167 }
2168 return command->mStatus;
Eric Laurentbfb1b832013-01-07 09:53:42 -08002169}
2170
Mathias Agopian65ab4712010-07-14 17:59:35 -07002171// insertCommand_l() must be called with mLock held
Eric Laurent0ede8922014-05-09 18:04:42 -07002172void AudioPolicyService::AudioCommandThread::insertCommand_l(sp<AudioCommand>& command, int delayMs)
Mathias Agopian65ab4712010-07-14 17:59:35 -07002173{
Glenn Kasten8d6a2442012-02-08 14:04:28 -08002174 ssize_t i; // not size_t because i will count down to -1
Eric Laurent0ede8922014-05-09 18:04:42 -07002175 Vector < sp<AudioCommand> > removedCommands;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002176 command->mTime = systemTime() + milliseconds(delayMs);
2177
2178 // acquire wake lock to make sure delayed commands are processed
Eric Laurentbfb1b832013-01-07 09:53:42 -08002179 if (mAudioCommands.isEmpty()) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07002180 acquire_wake_lock(PARTIAL_WAKE_LOCK, mName.string());
2181 }
2182
2183 // check same pending commands with later time stamps and eliminate them
Ivan Lozano5ff158f2017-10-30 09:06:24 -07002184 for (i = (ssize_t)mAudioCommands.size()-1; i >= 0; i--) {
Eric Laurent0ede8922014-05-09 18:04:42 -07002185 sp<AudioCommand> command2 = mAudioCommands[i];
Mathias Agopian65ab4712010-07-14 17:59:35 -07002186 // commands are sorted by increasing time stamp: no need to scan the rest of mAudioCommands
2187 if (command2->mTime <= command->mTime) break;
Eric Laurente45b48a2014-09-04 16:40:57 -07002188
2189 // create audio patch or release audio patch commands are equivalent
2190 // with regard to filtering
2191 if ((command->mCommand == CREATE_AUDIO_PATCH) ||
2192 (command->mCommand == RELEASE_AUDIO_PATCH)) {
2193 if ((command2->mCommand != CREATE_AUDIO_PATCH) &&
2194 (command2->mCommand != RELEASE_AUDIO_PATCH)) {
2195 continue;
2196 }
2197 } else if (command2->mCommand != command->mCommand) continue;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002198
2199 switch (command->mCommand) {
2200 case SET_PARAMETERS: {
Eric Laurent0ede8922014-05-09 18:04:42 -07002201 ParametersData *data = (ParametersData *)command->mParam.get();
2202 ParametersData *data2 = (ParametersData *)command2->mParam.get();
Mathias Agopian65ab4712010-07-14 17:59:35 -07002203 if (data->mIO != data2->mIO) break;
Steve Block3856b092011-10-20 11:56:00 +01002204 ALOGV("Comparing parameter command %s to new command %s",
Eric Laurentde070132010-07-13 04:45:46 -07002205 data2->mKeyValuePairs.string(), data->mKeyValuePairs.string());
Mathias Agopian65ab4712010-07-14 17:59:35 -07002206 AudioParameter param = AudioParameter(data->mKeyValuePairs);
2207 AudioParameter param2 = AudioParameter(data2->mKeyValuePairs);
2208 for (size_t j = 0; j < param.size(); j++) {
Glenn Kastene53b9ea2012-03-12 16:29:55 -07002209 String8 key;
2210 String8 value;
2211 param.getAt(j, key, value);
2212 for (size_t k = 0; k < param2.size(); k++) {
2213 String8 key2;
2214 String8 value2;
2215 param2.getAt(k, key2, value2);
2216 if (key2 == key) {
2217 param2.remove(key2);
2218 ALOGV("Filtering out parameter %s", key2.string());
2219 break;
2220 }
2221 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07002222 }
2223 // if all keys have been filtered out, remove the command.
2224 // otherwise, update the key value pairs
2225 if (param2.size() == 0) {
2226 removedCommands.add(command2);
2227 } else {
2228 data2->mKeyValuePairs = param2.toString();
2229 }
Eric Laurent21e54562013-09-23 12:08:05 -07002230 command->mTime = command2->mTime;
2231 // force delayMs to non 0 so that code below does not request to wait for
2232 // command status as the command is now delayed
2233 delayMs = 1;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002234 } break;
2235
2236 case SET_VOLUME: {
Eric Laurent0ede8922014-05-09 18:04:42 -07002237 VolumeData *data = (VolumeData *)command->mParam.get();
2238 VolumeData *data2 = (VolumeData *)command2->mParam.get();
Mathias Agopian65ab4712010-07-14 17:59:35 -07002239 if (data->mIO != data2->mIO) break;
2240 if (data->mStream != data2->mStream) break;
Steve Block3856b092011-10-20 11:56:00 +01002241 ALOGV("Filtering out volume command on output %d for stream %d",
Eric Laurentde070132010-07-13 04:45:46 -07002242 data->mIO, data->mStream);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002243 removedCommands.add(command2);
Eric Laurent21e54562013-09-23 12:08:05 -07002244 command->mTime = command2->mTime;
2245 // force delayMs to non 0 so that code below does not request to wait for
2246 // command status as the command is now delayed
2247 delayMs = 1;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002248 } break;
Eric Laurente45b48a2014-09-04 16:40:57 -07002249
Eric Laurentbaf35fe2016-07-27 15:36:53 -07002250 case SET_VOICE_VOLUME: {
2251 VoiceVolumeData *data = (VoiceVolumeData *)command->mParam.get();
2252 VoiceVolumeData *data2 = (VoiceVolumeData *)command2->mParam.get();
2253 ALOGV("Filtering out voice volume command value %f replaced by %f",
2254 data2->mVolume, data->mVolume);
2255 removedCommands.add(command2);
2256 command->mTime = command2->mTime;
2257 // force delayMs to non 0 so that code below does not request to wait for
2258 // command status as the command is now delayed
2259 delayMs = 1;
2260 } break;
2261
Eric Laurente45b48a2014-09-04 16:40:57 -07002262 case CREATE_AUDIO_PATCH:
2263 case RELEASE_AUDIO_PATCH: {
2264 audio_patch_handle_t handle;
Haynes Mathew Georgea2d4a6d2014-10-13 13:05:22 -07002265 struct audio_patch patch;
Eric Laurente45b48a2014-09-04 16:40:57 -07002266 if (command->mCommand == CREATE_AUDIO_PATCH) {
2267 handle = ((CreateAudioPatchData *)command->mParam.get())->mHandle;
Haynes Mathew Georgea2d4a6d2014-10-13 13:05:22 -07002268 patch = ((CreateAudioPatchData *)command->mParam.get())->mPatch;
Eric Laurente45b48a2014-09-04 16:40:57 -07002269 } else {
2270 handle = ((ReleaseAudioPatchData *)command->mParam.get())->mHandle;
Mikhail Naganov7be71d22018-05-23 16:51:46 -07002271 memset(&patch, 0, sizeof(patch));
Eric Laurente45b48a2014-09-04 16:40:57 -07002272 }
2273 audio_patch_handle_t handle2;
Haynes Mathew Georgea2d4a6d2014-10-13 13:05:22 -07002274 struct audio_patch patch2;
Eric Laurente45b48a2014-09-04 16:40:57 -07002275 if (command2->mCommand == CREATE_AUDIO_PATCH) {
2276 handle2 = ((CreateAudioPatchData *)command2->mParam.get())->mHandle;
Haynes Mathew Georgea2d4a6d2014-10-13 13:05:22 -07002277 patch2 = ((CreateAudioPatchData *)command2->mParam.get())->mPatch;
Eric Laurente45b48a2014-09-04 16:40:57 -07002278 } else {
2279 handle2 = ((ReleaseAudioPatchData *)command2->mParam.get())->mHandle;
Glenn Kastenf60b6b62015-07-06 10:53:26 -07002280 memset(&patch2, 0, sizeof(patch2));
Eric Laurente45b48a2014-09-04 16:40:57 -07002281 }
2282 if (handle != handle2) break;
Haynes Mathew Georgea2d4a6d2014-10-13 13:05:22 -07002283 /* Filter CREATE_AUDIO_PATCH commands only when they are issued for
2284 same output. */
2285 if( (command->mCommand == CREATE_AUDIO_PATCH) &&
2286 (command2->mCommand == CREATE_AUDIO_PATCH) ) {
2287 bool isOutputDiff = false;
2288 if (patch.num_sources == patch2.num_sources) {
2289 for (unsigned count = 0; count < patch.num_sources; count++) {
2290 if (patch.sources[count].id != patch2.sources[count].id) {
2291 isOutputDiff = true;
2292 break;
2293 }
2294 }
2295 if (isOutputDiff)
2296 break;
2297 }
2298 }
Eric Laurente45b48a2014-09-04 16:40:57 -07002299 ALOGV("Filtering out %s audio patch command for handle %d",
2300 (command->mCommand == CREATE_AUDIO_PATCH) ? "create" : "release", handle);
2301 removedCommands.add(command2);
2302 command->mTime = command2->mTime;
2303 // force delayMs to non 0 so that code below does not request to wait for
2304 // command status as the command is now delayed
2305 delayMs = 1;
2306 } break;
2307
Jean-Michel Trivide801052015-04-14 19:10:14 -07002308 case DYN_POLICY_MIX_STATE_UPDATE: {
2309
2310 } break;
2311
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -08002312 case RECORDING_CONFIGURATION_UPDATE: {
2313
2314 } break;
2315
Jean-Michel Trivi9a6b9ad2020-10-22 16:46:43 -07002316 case ROUTING_UPDATED: {
2317
2318 } break;
2319
Mathias Agopian65ab4712010-07-14 17:59:35 -07002320 default:
2321 break;
2322 }
2323 }
2324
2325 // remove filtered commands
2326 for (size_t j = 0; j < removedCommands.size(); j++) {
2327 // removed commands always have time stamps greater than current command
2328 for (size_t k = i + 1; k < mAudioCommands.size(); k++) {
Eric Laurent0ede8922014-05-09 18:04:42 -07002329 if (mAudioCommands[k].get() == removedCommands[j].get()) {
Steve Block3856b092011-10-20 11:56:00 +01002330 ALOGV("suppressing command: %d", mAudioCommands[k]->mCommand);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002331 mAudioCommands.removeAt(k);
2332 break;
2333 }
2334 }
2335 }
2336 removedCommands.clear();
2337
Eric Laurentaa79bef2015-01-15 14:33:51 -08002338 // Disable wait for status if delay is not 0.
2339 // Except for create audio patch command because the returned patch handle
2340 // is needed by audio policy manager
2341 if (delayMs != 0 && command->mCommand != CREATE_AUDIO_PATCH) {
Eric Laurentcec4abb2012-07-03 12:23:02 -07002342 command->mWaitStatus = false;
2343 }
Eric Laurentcec4abb2012-07-03 12:23:02 -07002344
Mathias Agopian65ab4712010-07-14 17:59:35 -07002345 // insert command at the right place according to its time stamp
Eric Laurent1e693b52014-07-09 15:03:28 -07002346 ALOGV("inserting command: %d at index %zd, num commands %zu",
2347 command->mCommand, i+1, mAudioCommands.size());
Mathias Agopian65ab4712010-07-14 17:59:35 -07002348 mAudioCommands.insertAt(command, i + 1);
2349}
2350
2351void AudioPolicyService::AudioCommandThread::exit()
2352{
Steve Block3856b092011-10-20 11:56:00 +01002353 ALOGV("AudioCommandThread::exit");
Mathias Agopian65ab4712010-07-14 17:59:35 -07002354 {
2355 AutoMutex _l(mLock);
2356 requestExit();
2357 mWaitWorkCV.signal();
2358 }
Zach Janga754b4f2015-10-27 01:29:34 +00002359 // Note that we can call it from the thread loop if all other references have been released
2360 // but it will safely return WOULD_BLOCK in this case
Mathias Agopian65ab4712010-07-14 17:59:35 -07002361 requestExitAndWait();
2362}
2363
2364void AudioPolicyService::AudioCommandThread::AudioCommand::dump(char* buffer, size_t size)
2365{
2366 snprintf(buffer, size, " %02d %06d.%03d %01u %p\n",
2367 mCommand,
2368 (int)ns2s(mTime),
2369 (int)ns2ms(mTime)%1000,
2370 mWaitStatus,
Eric Laurent0ede8922014-05-09 18:04:42 -07002371 mParam.get());
Mathias Agopian65ab4712010-07-14 17:59:35 -07002372}
2373
Dima Zavinfce7a472011-04-19 22:30:36 -07002374/******* helpers for the service_ops callbacks defined below *********/
2375void AudioPolicyService::setParameters(audio_io_handle_t ioHandle,
2376 const char *keyValuePairs,
2377 int delayMs)
2378{
Glenn Kasten72ef00d2012-01-17 11:09:42 -08002379 mAudioCommandThread->parametersCommand(ioHandle, keyValuePairs,
Dima Zavinfce7a472011-04-19 22:30:36 -07002380 delayMs);
2381}
2382
2383int AudioPolicyService::setStreamVolume(audio_stream_type_t stream,
2384 float volume,
2385 audio_io_handle_t output,
2386 int delayMs)
2387{
Glenn Kastenfff6d712012-01-12 16:38:12 -08002388 return (int)mAudioCommandThread->volumeCommand(stream, volume,
Glenn Kasten72ef00d2012-01-17 11:09:42 -08002389 output, delayMs);
Dima Zavinfce7a472011-04-19 22:30:36 -07002390}
2391
Dima Zavinfce7a472011-04-19 22:30:36 -07002392int AudioPolicyService::setVoiceVolume(float volume, int delayMs)
2393{
2394 return (int)mAudioCommandThread->voiceVolumeCommand(volume, delayMs);
2395}
2396
Eric Laurentb20cf7d2019-04-05 19:37:34 -07002397void AudioPolicyService::setEffectSuspended(int effectId,
2398 audio_session_t sessionId,
2399 bool suspended)
2400{
2401 mAudioCommandThread->setEffectSuspendedCommand(effectId, sessionId, suspended);
2402}
2403
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002404Status AudioPolicyService::onNewAudioModulesAvailable()
Mikhail Naganov88b30d22020-03-09 19:43:13 +00002405{
Mikhail Naganovd0e2c742020-03-25 15:59:59 -07002406 mOutputCommandThread->audioModulesUpdateCommand();
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002407 return Status::ok();
Mikhail Naganov88b30d22020-03-09 19:43:13 +00002408}
2409
Eric Laurentb20cf7d2019-04-05 19:37:34 -07002410
Dima Zavinfce7a472011-04-19 22:30:36 -07002411extern "C" {
Eric Laurent2d388ec2014-03-07 13:25:54 -08002412audio_module_handle_t aps_load_hw_module(void *service __unused,
2413 const char *name);
2414audio_io_handle_t aps_open_output(void *service __unused,
Eric Laurenta4c5a552012-03-29 10:12:40 -07002415 audio_devices_t *pDevices,
2416 uint32_t *pSamplingRate,
2417 audio_format_t *pFormat,
2418 audio_channel_mask_t *pChannelMask,
2419 uint32_t *pLatencyMs,
Eric Laurent2d388ec2014-03-07 13:25:54 -08002420 audio_output_flags_t flags);
Eric Laurenta4c5a552012-03-29 10:12:40 -07002421
Eric Laurent2d388ec2014-03-07 13:25:54 -08002422audio_io_handle_t aps_open_output_on_module(void *service __unused,
Eric Laurenta4c5a552012-03-29 10:12:40 -07002423 audio_module_handle_t module,
2424 audio_devices_t *pDevices,
2425 uint32_t *pSamplingRate,
2426 audio_format_t *pFormat,
2427 audio_channel_mask_t *pChannelMask,
2428 uint32_t *pLatencyMs,
Richard Fitzgeraldad3af332013-03-25 16:54:37 +00002429 audio_output_flags_t flags,
Eric Laurent2d388ec2014-03-07 13:25:54 -08002430 const audio_offload_info_t *offloadInfo);
2431audio_io_handle_t aps_open_dup_output(void *service __unused,
Dima Zavinfce7a472011-04-19 22:30:36 -07002432 audio_io_handle_t output1,
Eric Laurent2d388ec2014-03-07 13:25:54 -08002433 audio_io_handle_t output2);
2434int aps_close_output(void *service __unused, audio_io_handle_t output);
2435int aps_suspend_output(void *service __unused, audio_io_handle_t output);
2436int aps_restore_output(void *service __unused, audio_io_handle_t output);
2437audio_io_handle_t aps_open_input(void *service __unused,
Eric Laurenta4c5a552012-03-29 10:12:40 -07002438 audio_devices_t *pDevices,
2439 uint32_t *pSamplingRate,
2440 audio_format_t *pFormat,
2441 audio_channel_mask_t *pChannelMask,
Eric Laurent2d388ec2014-03-07 13:25:54 -08002442 audio_in_acoustics_t acoustics __unused);
2443audio_io_handle_t aps_open_input_on_module(void *service __unused,
Eric Laurenta4c5a552012-03-29 10:12:40 -07002444 audio_module_handle_t module,
2445 audio_devices_t *pDevices,
2446 uint32_t *pSamplingRate,
2447 audio_format_t *pFormat,
Eric Laurent2d388ec2014-03-07 13:25:54 -08002448 audio_channel_mask_t *pChannelMask);
2449int aps_close_input(void *service __unused, audio_io_handle_t input);
2450int aps_invalidate_stream(void *service __unused, audio_stream_type_t stream);
Glenn Kastend848eb42016-03-08 13:42:11 -08002451int aps_move_effects(void *service __unused, audio_session_t session,
Dima Zavinfce7a472011-04-19 22:30:36 -07002452 audio_io_handle_t src_output,
Eric Laurent2d388ec2014-03-07 13:25:54 -08002453 audio_io_handle_t dst_output);
2454char * aps_get_parameters(void *service __unused, audio_io_handle_t io_handle,
2455 const char *keys);
2456void aps_set_parameters(void *service, audio_io_handle_t io_handle,
2457 const char *kv_pairs, int delay_ms);
2458int aps_set_stream_volume(void *service, audio_stream_type_t stream,
Dima Zavinfce7a472011-04-19 22:30:36 -07002459 float volume, audio_io_handle_t output,
Eric Laurent2d388ec2014-03-07 13:25:54 -08002460 int delay_ms);
Eric Laurent2d388ec2014-03-07 13:25:54 -08002461int aps_set_voice_volume(void *service, float volume, int delay_ms);
2462};
Dima Zavinfce7a472011-04-19 22:30:36 -07002463
Mikhail Naganov1b2a7942017-12-08 10:18:09 -08002464} // namespace android