blob: de71a005a58be0bd35d65c39c3bdd95be259007c [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 Laurent6d607012021-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 Laurent6d607012021-07-05 11:54:40 +0200366void AudioPolicyService::onCheckSpatializer()
367{
368 Mutex::Autolock _l(mLock);
Eric Laurent39095982021-08-24 18:29:27 +0200369 onCheckSpatializer_l();
370}
371
372void AudioPolicyService::onCheckSpatializer_l()
373{
374 if (mSpatializer != nullptr) {
375 mOutputCommandThread->checkSpatializerCommand();
376 }
Eric Laurent6d607012021-07-05 11:54:40 +0200377}
378
379void AudioPolicyService::doOnCheckSpatializer()
380{
Eric Laurent39095982021-08-24 18:29:27 +0200381 Mutex::Autolock _l(mLock);
Eric Laurent6d607012021-07-05 11:54:40 +0200382
Eric Laurent39095982021-08-24 18:29:27 +0200383 if (mSpatializer != nullptr) {
384 if (mSpatializer->getLevel() != media::SpatializationLevel::NONE) {
385 audio_io_handle_t currentOutput = mSpatializer->getOutput();
386 audio_io_handle_t newOutput;
387 const audio_attributes_t attr = attributes_initializer(AUDIO_USAGE_MEDIA);
388 audio_config_base_t config = mSpatializer->getAudioInConfig();
389 status_t status =
390 mAudioPolicyManager->getSpatializerOutput(&config, &attr, &newOutput);
391
392 if (status == NO_ERROR && currentOutput == newOutput) {
393 return;
394 }
395 mLock.unlock();
396 // It is OK to call detachOutput() is none is already attached.
397 mSpatializer->detachOutput();
398 if (status != NO_ERROR || newOutput == AUDIO_IO_HANDLE_NONE) {
Eric Laurent6d607012021-07-05 11:54:40 +0200399 mLock.lock();
Eric Laurent39095982021-08-24 18:29:27 +0200400 return;
401 }
402 status = mSpatializer->attachOutput(newOutput);
403 mLock.lock();
404 if (status != NO_ERROR) {
405 mAudioPolicyManager->releaseSpatializerOutput(newOutput);
406 }
407 } else if (mSpatializer->getLevel() == media::SpatializationLevel::NONE
408 && mSpatializer->getOutput() != AUDIO_IO_HANDLE_NONE) {
409 mLock.unlock();
410 audio_io_handle_t output = mSpatializer->detachOutput();
411 mLock.lock();
412 if (output != AUDIO_IO_HANDLE_NONE) {
413 mAudioPolicyManager->releaseSpatializerOutput(output);
Eric Laurent6d607012021-07-05 11:54:40 +0200414 }
415 }
416 }
417}
418
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -0800419status_t AudioPolicyService::clientCreateAudioPatch(const struct audio_patch *patch,
420 audio_patch_handle_t *handle,
421 int delayMs)
422{
423 return mAudioCommandThread->createAudioPatchCommand(patch, handle, delayMs);
424}
425
426status_t AudioPolicyService::clientReleaseAudioPatch(audio_patch_handle_t handle,
427 int delayMs)
428{
429 return mAudioCommandThread->releaseAudioPatchCommand(handle, delayMs);
430}
431
Eric Laurente1715a42014-05-20 11:30:42 -0700432status_t AudioPolicyService::clientSetAudioPortConfig(const struct audio_port_config *config,
433 int delayMs)
434{
435 return mAudioCommandThread->setAudioPortConfigCommand(config, delayMs);
436}
437
Ytai Ben-Tsvi7e7a79d2020-12-15 16:48:16 -0800438AudioPolicyService::NotificationClient::NotificationClient(
439 const sp<AudioPolicyService>& service,
440 const sp<media::IAudioPolicyServiceClient>& client,
441 uid_t uid,
442 pid_t pid)
luochaojiang908c7d72018-06-21 14:58:04 +0800443 : mService(service), mUid(uid), mPid(pid), mAudioPolicyServiceClient(client),
François Gaffiecfe17322018-11-07 13:41:29 +0100444 mAudioPortCallbacksEnabled(false), mAudioVolumeGroupCallbacksEnabled(false)
Eric Laurentb52c1522014-05-20 11:27:36 -0700445{
446}
447
448AudioPolicyService::NotificationClient::~NotificationClient()
449{
450}
451
452void AudioPolicyService::NotificationClient::binderDied(const wp<IBinder>& who __unused)
453{
454 sp<NotificationClient> keep(this);
455 sp<AudioPolicyService> service = mService.promote();
456 if (service != 0) {
luochaojiang908c7d72018-06-21 14:58:04 +0800457 service->removeNotificationClient(mUid, mPid);
Eric Laurentb52c1522014-05-20 11:27:36 -0700458 }
459}
460
461void AudioPolicyService::NotificationClient::onAudioPortListUpdate()
462{
Eric Laurente8726fe2015-06-26 09:39:24 -0700463 if (mAudioPolicyServiceClient != 0 && mAudioPortCallbacksEnabled) {
Eric Laurentb52c1522014-05-20 11:27:36 -0700464 mAudioPolicyServiceClient->onAudioPortListUpdate();
465 }
466}
467
468void AudioPolicyService::NotificationClient::onAudioPatchListUpdate()
469{
Eric Laurente8726fe2015-06-26 09:39:24 -0700470 if (mAudioPolicyServiceClient != 0 && mAudioPortCallbacksEnabled) {
Eric Laurentb52c1522014-05-20 11:27:36 -0700471 mAudioPolicyServiceClient->onAudioPatchListUpdate();
472 }
473}
Eric Laurent57dae992011-07-24 13:36:09 -0700474
François Gaffiecfe17322018-11-07 13:41:29 +0100475void AudioPolicyService::NotificationClient::onAudioVolumeGroupChanged(volume_group_t group,
476 int flags)
477{
478 if (mAudioPolicyServiceClient != 0 && mAudioVolumeGroupCallbacksEnabled) {
479 mAudioPolicyServiceClient->onAudioVolumeGroupChanged(group, flags);
480 }
481}
482
483
Jean-Michel Trivide801052015-04-14 19:10:14 -0700484void AudioPolicyService::NotificationClient::onDynamicPolicyMixStateUpdate(
Chih-Hung Hsiehe964d4e2016-08-09 14:31:32 -0700485 const String8& regId, int32_t state)
Jean-Michel Trivide801052015-04-14 19:10:14 -0700486{
Andy Hung4ef19fa2018-05-15 19:35:29 -0700487 if (mAudioPolicyServiceClient != 0 && isServiceUid(mUid)) {
Ytai Ben-Tsvi7e7a79d2020-12-15 16:48:16 -0800488 mAudioPolicyServiceClient->onDynamicPolicyMixStateUpdate(
489 legacy2aidl_String8_string(regId).value(), state);
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -0800490 }
491}
492
493void AudioPolicyService::NotificationClient::onRecordingConfigurationUpdate(
Eric Laurenta9f86652018-11-28 17:23:11 -0800494 int event,
495 const record_client_info_t *clientInfo,
496 const audio_config_base_t *clientConfig,
497 std::vector<effect_descriptor_t> clientEffects,
498 const audio_config_base_t *deviceConfig,
499 std::vector<effect_descriptor_t> effects,
500 audio_patch_handle_t patchHandle,
501 audio_source_t source)
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -0800502{
Andy Hung4ef19fa2018-05-15 19:35:29 -0700503 if (mAudioPolicyServiceClient != 0 && isServiceUid(mUid)) {
Ytai Ben-Tsvi7e7a79d2020-12-15 16:48:16 -0800504 status_t status = [&]() -> status_t {
505 int32_t eventAidl = VALUE_OR_RETURN_STATUS(convertIntegral<int32_t>(event));
506 media::RecordClientInfo clientInfoAidl = VALUE_OR_RETURN_STATUS(
507 legacy2aidl_record_client_info_t_RecordClientInfo(*clientInfo));
508 media::AudioConfigBase clientConfigAidl = VALUE_OR_RETURN_STATUS(
509 legacy2aidl_audio_config_base_t_AudioConfigBase(*clientConfig));
510 std::vector<media::EffectDescriptor> clientEffectsAidl = VALUE_OR_RETURN_STATUS(
511 convertContainer<std::vector<media::EffectDescriptor>>(
512 clientEffects,
513 legacy2aidl_effect_descriptor_t_EffectDescriptor));
514 media::AudioConfigBase deviceConfigAidl = VALUE_OR_RETURN_STATUS(
515 legacy2aidl_audio_config_base_t_AudioConfigBase(*deviceConfig));
516 std::vector<media::EffectDescriptor> effectsAidl = VALUE_OR_RETURN_STATUS(
517 convertContainer<std::vector<media::EffectDescriptor>>(
518 effects,
519 legacy2aidl_effect_descriptor_t_EffectDescriptor));
520 int32_t patchHandleAidl = VALUE_OR_RETURN_STATUS(
521 legacy2aidl_audio_patch_handle_t_int32_t(patchHandle));
522 media::AudioSourceType sourceAidl = VALUE_OR_RETURN_STATUS(
523 legacy2aidl_audio_source_t_AudioSourceType(source));
524 return aidl_utils::statusTFromBinderStatus(
525 mAudioPolicyServiceClient->onRecordingConfigurationUpdate(eventAidl,
526 clientInfoAidl,
527 clientConfigAidl,
528 clientEffectsAidl,
529 deviceConfigAidl,
530 effectsAidl,
531 patchHandleAidl,
532 sourceAidl));
533 }();
534 ALOGW_IF(status != OK, "onRecordingConfigurationUpdate() failed: %d", status);
Jean-Michel Trivide801052015-04-14 19:10:14 -0700535 }
536}
537
Eric Laurente8726fe2015-06-26 09:39:24 -0700538void AudioPolicyService::NotificationClient::setAudioPortCallbacksEnabled(bool enabled)
539{
540 mAudioPortCallbacksEnabled = enabled;
541}
542
François Gaffiecfe17322018-11-07 13:41:29 +0100543void AudioPolicyService::NotificationClient::setAudioVolumeGroupCallbacksEnabled(bool enabled)
544{
545 mAudioVolumeGroupCallbacksEnabled = enabled;
546}
Eric Laurente8726fe2015-06-26 09:39:24 -0700547
Jean-Michel Trivi9a6b9ad2020-10-22 16:46:43 -0700548void AudioPolicyService::NotificationClient::onRoutingUpdated()
549{
550 if (mAudioPolicyServiceClient != 0 && isServiceUid(mUid)) {
551 mAudioPolicyServiceClient->onRoutingUpdated();
552 }
553}
554
Mathias Agopian65ab4712010-07-14 17:59:35 -0700555void AudioPolicyService::binderDied(const wp<IBinder>& who) {
Glenn Kasten411e4472012-11-02 10:00:06 -0700556 ALOGW("binderDied() %p, calling pid %d", who.unsafe_get(),
Eric Laurentde070132010-07-13 04:45:46 -0700557 IPCThreadState::self()->getCallingPid());
Mathias Agopian65ab4712010-07-14 17:59:35 -0700558}
559
Mikhail Naganov12b716c2020-04-30 22:37:43 +0000560static bool dumpTryLock(Mutex& mutex) ACQUIRE(mutex) NO_THREAD_SAFETY_ANALYSIS
Mathias Agopian65ab4712010-07-14 17:59:35 -0700561{
Mikhail Naganov12b716c2020-04-30 22:37:43 +0000562 return mutex.timedLock(kDumpLockTimeoutNs) == NO_ERROR;
563}
564
565static void dumpReleaseLock(Mutex& mutex, bool locked) RELEASE(mutex) NO_THREAD_SAFETY_ANALYSIS
566{
567 if (locked) mutex.unlock();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700568}
569
570status_t AudioPolicyService::dumpInternals(int fd)
571{
572 const size_t SIZE = 256;
573 char buffer[SIZE];
574 String8 result;
575
Eric Laurentdce54a12014-03-10 12:19:46 -0700576 snprintf(buffer, SIZE, "AudioPolicyManager: %p\n", mAudioPolicyManager);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700577 result.append(buffer);
578 snprintf(buffer, SIZE, "Command Thread: %p\n", mAudioCommandThread.get());
579 result.append(buffer);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700580
Hayden Gomes524159d2019-12-23 14:41:47 -0800581 snprintf(buffer, SIZE, "Supported System Usages:\n");
582 result.append(buffer);
583 for (std::vector<audio_usage_t>::iterator it = mSupportedSystemUsages.begin();
584 it != mSupportedSystemUsages.end(); ++it) {
585 snprintf(buffer, SIZE, "\t%d\n", *it);
586 result.append(buffer);
587 }
588
Mathias Agopian65ab4712010-07-14 17:59:35 -0700589 write(fd, result.string(), result.size());
590 return NO_ERROR;
591}
592
Eric Laurente8c8b432018-10-17 10:08:02 -0700593void AudioPolicyService::updateUidStates()
Svet Ganovf4ddfef2018-01-16 07:37:58 -0800594{
Eric Laurente8c8b432018-10-17 10:08:02 -0700595 Mutex::Autolock _l(mLock);
596 updateUidStates_l();
597}
598
599void AudioPolicyService::updateUidStates_l()
600{
Eric Laurent4eb58f12018-12-07 16:41:02 -0800601// Go over all active clients and allow capture (does not force silence) in the
602// following cases:
Evan Severson1f700cd2021-02-10 13:10:37 -0800603// The client is the assistant
604// AND an accessibility service is on TOP or a RTT call is active
Eric Laurent589171c2019-07-25 18:04:29 -0700605// AND the source is VOICE_RECOGNITION or HOTWORD
Evan Severson1f700cd2021-02-10 13:10:37 -0800606// OR uses VOICE_RECOGNITION AND is on TOP
607// OR uses HOTWORD
608// AND there is no active privacy sensitive capture or call
609// OR client has CAPTURE_AUDIO_OUTPUT privileged permission
610// OR The client is an accessibility service
611// AND Is on TOP
612// AND the source is VOICE_RECOGNITION or HOTWORD
613// OR The assistant is not on TOP
Eric Laurent589171c2019-07-25 18:04:29 -0700614// AND there is no active privacy sensitive capture or call
615// OR client has CAPTURE_AUDIO_OUTPUT privileged permission
Evan Severson1f700cd2021-02-10 13:10:37 -0800616// AND is on TOP
617// AND the source is VOICE_RECOGNITION or HOTWORD
618// OR the client source is virtual (remote submix, call audio TX or RX...)
619// OR the client source is HOTWORD
620// AND is on TOP
621// OR all active clients are using HOTWORD source
622// AND no call is active
623// OR client has CAPTURE_AUDIO_OUTPUT privileged permission
624// OR the client is the current InputMethodService
625// AND a RTT call is active AND the source is VOICE_RECOGNITION
626// OR Any client
627// AND The assistant is not on TOP
628// AND is on TOP or latest started
629// AND there is no active privacy sensitive capture or call
630// OR client has CAPTURE_AUDIO_OUTPUT privileged permission
Eric Laurent4eb58f12018-12-07 16:41:02 -0800631
Eric Laurent4e947da2019-10-17 15:24:06 -0700632
Eric Laurent4eb58f12018-12-07 16:41:02 -0800633 sp<AudioRecordClient> topActive;
634 sp<AudioRecordClient> latestActive;
Eric Laurentc21d5692020-02-25 10:24:36 -0800635 sp<AudioRecordClient> topSensitiveActive;
Eric Laurentb809a752020-06-29 09:53:13 -0700636 sp<AudioRecordClient> latestSensitiveActiveOrComm;
Eric Laurent1ff16a72019-03-14 18:35:04 -0700637
Eric Laurenta46bedb2018-12-07 18:01:26 -0800638 nsecs_t topStartNs = 0;
639 nsecs_t latestStartNs = 0;
Eric Laurentc21d5692020-02-25 10:24:36 -0800640 nsecs_t topSensitiveStartNs = 0;
Eric Laurent4eb58f12018-12-07 16:41:02 -0800641 nsecs_t latestSensitiveStartNs = 0;
642 bool isA11yOnTop = mUidPolicy->isA11yOnTop();
643 bool isAssistantOnTop = false;
644 bool isSensitiveActive = false;
Eric Laurent1ff16a72019-03-14 18:35:04 -0700645 bool isInCall = mPhoneState == AUDIO_MODE_IN_CALL;
Eric Laurentc21d5692020-02-25 10:24:36 -0800646 bool isInCommunication = mPhoneState == AUDIO_MODE_IN_COMMUNICATION;
647 bool rttCallActive = (isInCall || isInCommunication)
Eric Laurent6ede98f2019-06-11 14:50:30 -0700648 && mUidPolicy->isRttEnabled();
Eric Laurent4e947da2019-10-17 15:24:06 -0700649 bool onlyHotwordActive = true;
Eric Laurentb809a752020-06-29 09:53:13 -0700650 bool isPhoneStateOwnerActive = false;
Eric Laurent4eb58f12018-12-07 16:41:02 -0800651
Michael Groovercfd28302018-12-11 19:16:46 -0800652 // if Sensor Privacy is enabled then all recordings should be silenced.
653 if (mSensorPrivacyPolicy->isSensorPrivacyEnabled()) {
654 silenceAllRecordings_l();
655 return;
656 }
657
Eric Laurente8c8b432018-10-17 10:08:02 -0700658 for (size_t i =0; i < mAudioRecordClients.size(); i++) {
659 sp<AudioRecordClient> current = mAudioRecordClients[i];
Svet Ganov33761132021-05-13 22:51:08 +0000660 uid_t currentUid = VALUE_OR_FATAL(aidl2legacy_int32_t_uid_t(
661 current->attributionSource.uid));
Evan Severson1f700cd2021-02-10 13:10:37 -0800662 if (!current->active) {
Eric Laurent1ff16a72019-03-14 18:35:04 -0700663 continue;
Eric Laurent4eb58f12018-12-07 16:41:02 -0800664 }
Eric Laurent1ff16a72019-03-14 18:35:04 -0700665
Philip P. Moltmannbda45752020-07-17 16:41:18 -0700666 app_state_t appState = apmStatFromAmState(mUidPolicy->getUidState(currentUid));
Eric Laurent1ff16a72019-03-14 18:35:04 -0700667 // clients which app is in IDLE state are not eligible for top active or
668 // latest active
669 if (appState == APP_STATE_IDLE) {
670 continue;
671 }
672
Philip P. Moltmannbda45752020-07-17 16:41:18 -0700673 bool isAccessibility = mUidPolicy->isA11yUid(currentUid);
Eric Laurent14a88632020-07-16 12:28:30 -0700674 // Clients capturing for Accessibility services or virtual sources are not considered
Eric Laurentc21d5692020-02-25 10:24:36 -0800675 // for top or latest active to avoid masking regular clients started before
Eric Laurent14a88632020-07-16 12:28:30 -0700676 if (!isAccessibility && !isVirtualSource(current->attributes.source)) {
Philip P. Moltmannbda45752020-07-17 16:41:18 -0700677 bool isAssistant = mUidPolicy->isAssistantUid(currentUid);
Eric Laurentc21d5692020-02-25 10:24:36 -0800678 bool isPrivacySensitive =
679 (current->attributes.flags & AUDIO_FLAG_CAPTURE_PRIVATE) != 0;
Eric Laurentb809a752020-06-29 09:53:13 -0700680
Eric Laurentc21d5692020-02-25 10:24:36 -0800681 if (appState == APP_STATE_TOP) {
682 if (isPrivacySensitive) {
683 if (current->startTimeNs > topSensitiveStartNs) {
684 topSensitiveActive = current;
685 topSensitiveStartNs = current->startTimeNs;
686 }
687 } else {
688 if (current->startTimeNs > topStartNs) {
689 topActive = current;
690 topStartNs = current->startTimeNs;
691 }
692 }
693 if (isAssistant) {
694 isAssistantOnTop = true;
695 }
Eric Laurenta46bedb2018-12-07 18:01:26 -0800696 }
Eric Laurentc21d5692020-02-25 10:24:36 -0800697 // Clients capturing for HOTWORD are not considered
698 // for latest active to avoid masking regular clients started before
699 if (!(current->attributes.source == AUDIO_SOURCE_HOTWORD
700 || ((isA11yOnTop || rttCallActive) && isAssistant))) {
701 if (isPrivacySensitive) {
Eric Laurentb809a752020-06-29 09:53:13 -0700702 // if audio mode is IN_COMMUNICATION, make sure the audio mode owner
703 // is marked latest sensitive active even if another app qualifies.
704 if (current->startTimeNs > latestSensitiveStartNs
Philip P. Moltmannbda45752020-07-17 16:41:18 -0700705 || (isInCommunication && currentUid == mPhoneStateOwnerUid)) {
Eric Laurentb809a752020-06-29 09:53:13 -0700706 if (!isInCommunication || latestSensitiveActiveOrComm == nullptr
Philip P. Moltmannbda45752020-07-17 16:41:18 -0700707 || VALUE_OR_FATAL(aidl2legacy_int32_t_uid_t(
Svet Ganov33761132021-05-13 22:51:08 +0000708 latestSensitiveActiveOrComm->attributionSource.uid))
Philip P. Moltmannbda45752020-07-17 16:41:18 -0700709 != mPhoneStateOwnerUid) {
Eric Laurentb809a752020-06-29 09:53:13 -0700710 latestSensitiveActiveOrComm = current;
711 latestSensitiveStartNs = current->startTimeNs;
712 }
Eric Laurentc21d5692020-02-25 10:24:36 -0800713 }
714 isSensitiveActive = true;
715 } else {
716 if (current->startTimeNs > latestStartNs) {
717 latestActive = current;
718 latestStartNs = current->startTimeNs;
719 }
720 }
Eric Laurent4eb58f12018-12-07 16:41:02 -0800721 }
722 }
Eric Laurent4e947da2019-10-17 15:24:06 -0700723 if (current->attributes.source != AUDIO_SOURCE_HOTWORD) {
724 onlyHotwordActive = false;
725 }
Philip P. Moltmannbda45752020-07-17 16:41:18 -0700726 if (currentUid == mPhoneStateOwnerUid) {
Eric Laurentb809a752020-06-29 09:53:13 -0700727 isPhoneStateOwnerActive = true;
728 }
Eric Laurent4eb58f12018-12-07 16:41:02 -0800729 }
730
Eric Laurent1ff16a72019-03-14 18:35:04 -0700731 // if no active client with UI on Top, consider latest active as top
732 if (topActive == nullptr) {
733 topActive = latestActive;
Eric Laurentc21d5692020-02-25 10:24:36 -0800734 topStartNs = latestStartNs;
735 }
736 if (topSensitiveActive == nullptr) {
Eric Laurentb809a752020-06-29 09:53:13 -0700737 topSensitiveActive = latestSensitiveActiveOrComm;
Eric Laurentc21d5692020-02-25 10:24:36 -0800738 topSensitiveStartNs = latestSensitiveStartNs;
Eric Laurentb809a752020-06-29 09:53:13 -0700739 } else if (latestSensitiveActiveOrComm != nullptr) {
740 // if audio mode is IN_COMMUNICATION, favor audio mode owner over an app with
741 // foreground UI in case both are capturing with privacy sensitive flag.
Philip P. Moltmannbda45752020-07-17 16:41:18 -0700742 uid_t latestActiveUid = VALUE_OR_FATAL(
Svet Ganov33761132021-05-13 22:51:08 +0000743 aidl2legacy_int32_t_uid_t(latestSensitiveActiveOrComm->attributionSource.uid));
Philip P. Moltmannbda45752020-07-17 16:41:18 -0700744 if (isInCommunication && latestActiveUid == mPhoneStateOwnerUid) {
Eric Laurentb809a752020-06-29 09:53:13 -0700745 topSensitiveActive = latestSensitiveActiveOrComm;
746 topSensitiveStartNs = latestSensitiveStartNs;
747 }
Eric Laurentc21d5692020-02-25 10:24:36 -0800748 }
749
750 // If both privacy sensitive and regular capture are active:
751 // if the regular capture is privileged
752 // allow concurrency
753 // else
754 // favor the privacy sensitive case
755 if (topActive != nullptr && topSensitiveActive != nullptr
Ricardo Correa57a37692020-03-23 17:27:25 -0700756 && !topActive->canCaptureOutput) {
Eric Laurentc21d5692020-02-25 10:24:36 -0800757 topActive = nullptr;
Eric Laurent4eb58f12018-12-07 16:41:02 -0800758 }
759
760 for (size_t i =0; i < mAudioRecordClients.size(); i++) {
761 sp<AudioRecordClient> current = mAudioRecordClients[i];
Philip P. Moltmannbda45752020-07-17 16:41:18 -0700762 uid_t currentUid = VALUE_OR_FATAL(aidl2legacy_int32_t_uid_t(
Svet Ganov33761132021-05-13 22:51:08 +0000763 current->attributionSource.uid));
Eric Laurent1ff16a72019-03-14 18:35:04 -0700764 if (!current->active) {
765 continue;
766 }
767
Eric Laurent4eb58f12018-12-07 16:41:02 -0800768 audio_source_t source = current->attributes.source;
Philip P. Moltmannbda45752020-07-17 16:41:18 -0700769 bool isTopOrLatestActive = topActive == nullptr ? false :
Svet Ganov33761132021-05-13 22:51:08 +0000770 current->attributionSource.uid == topActive->attributionSource.uid;
Philip P. Moltmannbda45752020-07-17 16:41:18 -0700771 bool isTopOrLatestSensitive = topSensitiveActive == nullptr ? false :
Svet Ganov33761132021-05-13 22:51:08 +0000772 current->attributionSource.uid == topSensitiveActive->attributionSource.uid;
Eric Laurentc21d5692020-02-25 10:24:36 -0800773
Mikhail Naganov12b716c2020-04-30 22:37:43 +0000774 auto canCaptureIfInCallOrCommunication = [&](const auto &recordClient) REQUIRES(mLock) {
Philip P. Moltmannbda45752020-07-17 16:41:18 -0700775 uid_t recordUid = VALUE_OR_FATAL(aidl2legacy_int32_t_uid_t(
Svet Ganov33761132021-05-13 22:51:08 +0000776 recordClient->attributionSource.uid));
Ricardo Correa57a37692020-03-23 17:27:25 -0700777 bool canCaptureCall = recordClient->canCaptureOutput;
Eric Laurentb809a752020-06-29 09:53:13 -0700778 bool canCaptureCommunication = recordClient->canCaptureOutput
779 || !isPhoneStateOwnerActive
Philip P. Moltmannbda45752020-07-17 16:41:18 -0700780 || recordUid == mPhoneStateOwnerUid;
Eric Laurentb809a752020-06-29 09:53:13 -0700781 return !(isInCall && !canCaptureCall)
782 && !(isInCommunication && !canCaptureCommunication);
Eric Laurentc21d5692020-02-25 10:24:36 -0800783 };
Eric Laurent1ff16a72019-03-14 18:35:04 -0700784
785 // By default allow capture if:
786 // The assistant is not on TOP
Eric Laurenta171e352019-05-07 13:04:45 -0700787 // AND is on TOP or latest started
Eric Laurent1ff16a72019-03-14 18:35:04 -0700788 // AND there is no active privacy sensitive capture or call
789 // OR client has CAPTURE_AUDIO_OUTPUT privileged permission
790 bool allowCapture = !isAssistantOnTop
Eric Laurentc21d5692020-02-25 10:24:36 -0800791 && (isTopOrLatestActive || isTopOrLatestSensitive)
792 && !(isSensitiveActive
Ricardo Correa57a37692020-03-23 17:27:25 -0700793 && !(isTopOrLatestSensitive || current->canCaptureOutput))
Eric Laurentc21d5692020-02-25 10:24:36 -0800794 && canCaptureIfInCallOrCommunication(current);
Eric Laurent2dc962b2019-03-01 08:25:25 -0800795
Eric Laurented726cc2021-07-01 14:26:41 +0200796 if (!current->hasOp()) {
797 // Never allow capture if app op is denied
798 allowCapture = false;
799 } else if (isVirtualSource(source)) {
Eric Laurent1ff16a72019-03-14 18:35:04 -0700800 // Allow capture for virtual (remote submix, call audio TX or RX...) sources
801 allowCapture = true;
Philip P. Moltmannbda45752020-07-17 16:41:18 -0700802 } else if (mUidPolicy->isAssistantUid(currentUid)) {
Eric Laurent1ff16a72019-03-14 18:35:04 -0700803 // For assistant allow capture if:
Eric Laurent6ede98f2019-06-11 14:50:30 -0700804 // An accessibility service is on TOP or a RTT call is active
Eric Laurent1ff16a72019-03-14 18:35:04 -0700805 // AND the source is VOICE_RECOGNITION or HOTWORD
Eric Laurenta171e352019-05-07 13:04:45 -0700806 // OR is on TOP AND uses VOICE_RECOGNITION
Eric Laurent1ff16a72019-03-14 18:35:04 -0700807 // OR uses HOTWORD
808 // AND there is no active privacy sensitive capture or call
809 // OR client has CAPTURE_AUDIO_OUTPUT privileged permission
Eric Laurent6ede98f2019-06-11 14:50:30 -0700810 if (isA11yOnTop || rttCallActive) {
Eric Laurent4eb58f12018-12-07 16:41:02 -0800811 if (source == AUDIO_SOURCE_HOTWORD || source == AUDIO_SOURCE_VOICE_RECOGNITION) {
Eric Laurent1ff16a72019-03-14 18:35:04 -0700812 allowCapture = true;
Eric Laurent4eb58f12018-12-07 16:41:02 -0800813 }
814 } else {
Eric Laurenta171e352019-05-07 13:04:45 -0700815 if (((isAssistantOnTop && source == AUDIO_SOURCE_VOICE_RECOGNITION) ||
Eric Laurentc21d5692020-02-25 10:24:36 -0800816 source == AUDIO_SOURCE_HOTWORD)
Ricardo Correa57a37692020-03-23 17:27:25 -0700817 && !(isSensitiveActive && !current->canCaptureOutput)
Eric Laurentc21d5692020-02-25 10:24:36 -0800818 && canCaptureIfInCallOrCommunication(current)) {
Eric Laurent1ff16a72019-03-14 18:35:04 -0700819 allowCapture = true;
Eric Laurent4eb58f12018-12-07 16:41:02 -0800820 }
821 }
Philip P. Moltmannbda45752020-07-17 16:41:18 -0700822 } else if (mUidPolicy->isA11yUid(currentUid)) {
Eric Laurent1ff16a72019-03-14 18:35:04 -0700823 // For accessibility service allow capture if:
Eric Laurent47670c92019-08-28 16:59:05 -0700824 // The assistant is not on TOP
825 // AND there is no active privacy sensitive capture or call
Eric Laurent589171c2019-07-25 18:04:29 -0700826 // OR client has CAPTURE_AUDIO_OUTPUT privileged permission
Eric Laurent47670c92019-08-28 16:59:05 -0700827 // OR
828 // Is on TOP AND the source is VOICE_RECOGNITION or HOTWORD
829 if (!isAssistantOnTop
Ricardo Correa57a37692020-03-23 17:27:25 -0700830 && !(isSensitiveActive && !current->canCaptureOutput)
Eric Laurentc21d5692020-02-25 10:24:36 -0800831 && canCaptureIfInCallOrCommunication(current)) {
Eric Laurent47670c92019-08-28 16:59:05 -0700832 allowCapture = true;
833 }
Eric Laurent589171c2019-07-25 18:04:29 -0700834 if (isA11yOnTop) {
835 if (source == AUDIO_SOURCE_VOICE_RECOGNITION || source == AUDIO_SOURCE_HOTWORD) {
836 allowCapture = true;
837 }
Eric Laurent4eb58f12018-12-07 16:41:02 -0800838 }
Eric Laurent4e947da2019-10-17 15:24:06 -0700839 } else if (source == AUDIO_SOURCE_HOTWORD) {
840 // For HOTWORD source allow capture when not on TOP if:
841 // All active clients are using HOTWORD source
842 // AND no call is active
843 // OR client has CAPTURE_AUDIO_OUTPUT privileged permission
Eric Laurentc21d5692020-02-25 10:24:36 -0800844 if (onlyHotwordActive
845 && canCaptureIfInCallOrCommunication(current)) {
Eric Laurent4e947da2019-10-17 15:24:06 -0700846 allowCapture = true;
847 }
Philip P. Moltmannbda45752020-07-17 16:41:18 -0700848 } else if (mUidPolicy->isCurrentImeUid(currentUid)) {
Kohsuke Yatoha623a132020-03-24 20:10:26 -0700849 // For current InputMethodService allow capture if:
850 // A RTT call is active AND the source is VOICE_RECOGNITION
851 if (rttCallActive && source == AUDIO_SOURCE_VOICE_RECOGNITION) {
852 allowCapture = true;
853 }
Eric Laurent4eb58f12018-12-07 16:41:02 -0800854 }
Eric Laurent8c7ef892021-06-10 13:32:16 +0200855 setAppState_l(current,
Philip P. Moltmannbda45752020-07-17 16:41:18 -0700856 allowCapture ? apmStatFromAmState(mUidPolicy->getUidState(currentUid)) :
Eric Laurent1ff16a72019-03-14 18:35:04 -0700857 APP_STATE_IDLE);
Eric Laurente8c8b432018-10-17 10:08:02 -0700858 }
859}
860
Michael Groovercfd28302018-12-11 19:16:46 -0800861void AudioPolicyService::silenceAllRecordings_l() {
862 for (size_t i = 0; i < mAudioRecordClients.size(); i++) {
863 sp<AudioRecordClient> current = mAudioRecordClients[i];
Eric Laurent1ff16a72019-03-14 18:35:04 -0700864 if (!isVirtualSource(current->attributes.source)) {
Eric Laurent8c7ef892021-06-10 13:32:16 +0200865 setAppState_l(current, APP_STATE_IDLE);
Eric Laurent1ff16a72019-03-14 18:35:04 -0700866 }
Michael Groovercfd28302018-12-11 19:16:46 -0800867 }
868}
869
Eric Laurente8c8b432018-10-17 10:08:02 -0700870/* static */
871app_state_t AudioPolicyService::apmStatFromAmState(int amState) {
Eric Laurent1ff16a72019-03-14 18:35:04 -0700872
873 if (amState == ActivityManager::PROCESS_STATE_UNKNOWN) {
Eric Laurente8c8b432018-10-17 10:08:02 -0700874 return APP_STATE_IDLE;
Eric Laurent1ff16a72019-03-14 18:35:04 -0700875 } else if (amState <= ActivityManager::PROCESS_STATE_TOP) {
876 // include persistent services
877 return APP_STATE_TOP;
Eric Laurente8c8b432018-10-17 10:08:02 -0700878 }
879 return APP_STATE_FOREGROUND;
880}
881
Eric Laurent4eb58f12018-12-07 16:41:02 -0800882/* static */
Eric Laurent2dc962b2019-03-01 08:25:25 -0800883bool AudioPolicyService::isVirtualSource(audio_source_t source)
Eric Laurent4eb58f12018-12-07 16:41:02 -0800884{
885 switch (source) {
886 case AUDIO_SOURCE_VOICE_UPLINK:
887 case AUDIO_SOURCE_VOICE_DOWNLINK:
888 case AUDIO_SOURCE_VOICE_CALL:
Eric Laurent2dc962b2019-03-01 08:25:25 -0800889 case AUDIO_SOURCE_REMOTE_SUBMIX:
890 case AUDIO_SOURCE_FM_TUNER:
Eric Laurent68eb2122020-04-30 17:40:57 -0700891 case AUDIO_SOURCE_ECHO_REFERENCE:
Eric Laurent4eb58f12018-12-07 16:41:02 -0800892 return true;
893 default:
894 break;
895 }
896 return false;
897}
898
Eric Laurented726cc2021-07-01 14:26:41 +0200899/* static */
900bool AudioPolicyService::isAppOpSource(audio_source_t source)
901{
902 switch (source) {
903 case AUDIO_SOURCE_FM_TUNER:
904 case AUDIO_SOURCE_ECHO_REFERENCE:
905 return false;
906 default:
907 break;
908 }
909 return true;
910}
911
Eric Laurent8c7ef892021-06-10 13:32:16 +0200912void AudioPolicyService::setAppState_l(sp<AudioRecordClient> client, app_state_t state)
Eric Laurente8c8b432018-10-17 10:08:02 -0700913{
914 AutoCallerClear acc;
915
916 if (mAudioPolicyManager) {
Eric Laurent8c7ef892021-06-10 13:32:16 +0200917 mAudioPolicyManager->setAppState(client->portId, state);
Mikhail Naganoveae73eb2018-04-03 16:57:36 -0700918 }
919 sp<IAudioFlinger> af = AudioSystem::get_audio_flinger();
920 if (af) {
Eric Laurentf32108e2018-10-04 17:22:04 -0700921 bool silenced = state == APP_STATE_IDLE;
Eric Laurent8c7ef892021-06-10 13:32:16 +0200922 if (client->silenced != silenced) {
923 if (client->active) {
924 if (silenced) {
925 finishRecording(client->attributionSource, client->attributes.source);
926 } else {
927 std::stringstream msg;
928 msg << "Audio recording un-silenced on session " << client->session;
929 if (!startRecording(client->attributionSource, String16(msg.str().c_str()),
930 client->attributes.source)) {
931 silenced = true;
932 }
933 }
934 }
935 af->setRecordSilenced(client->portId, silenced);
936 client->silenced = silenced;
937 }
Mikhail Naganoveae73eb2018-04-03 16:57:36 -0700938 }
Svet Ganovf4ddfef2018-01-16 07:37:58 -0800939}
940
Glenn Kasten0f11b512014-01-31 16:18:54 -0800941status_t AudioPolicyService::dump(int fd, const Vector<String16>& args __unused)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700942{
Glenn Kasten44deb052012-02-05 18:09:08 -0800943 if (!dumpAllowed()) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700944 dumpPermissionDenial(fd);
945 } else {
Mikhail Naganov12b716c2020-04-30 22:37:43 +0000946 const bool locked = dumpTryLock(mLock);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700947 if (!locked) {
948 String8 result(kDeadlockedString);
949 write(fd, result.string(), result.size());
950 }
951
952 dumpInternals(fd);
Glenn Kasten9d1f02d2012-02-08 17:47:58 -0800953 if (mAudioCommandThread != 0) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700954 mAudioCommandThread->dump(fd);
955 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700956
Eric Laurentdce54a12014-03-10 12:19:46 -0700957 if (mAudioPolicyManager) {
958 mAudioPolicyManager->dump(fd);
959 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700960
Kevin Rocard8be94972019-02-22 13:26:25 -0800961 mPackageManager.dump(fd);
962
Mikhail Naganov12b716c2020-04-30 22:37:43 +0000963 dumpReleaseLock(mLock, locked);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700964 }
965 return NO_ERROR;
966}
967
968status_t AudioPolicyService::dumpPermissionDenial(int fd)
969{
970 const size_t SIZE = 256;
971 char buffer[SIZE];
972 String8 result;
973 snprintf(buffer, SIZE, "Permission Denial: "
974 "can't dump AudioPolicyService from pid=%d, uid=%d\n",
975 IPCThreadState::self()->getCallingPid(),
976 IPCThreadState::self()->getCallingUid());
977 result.append(buffer);
978 write(fd, result.string(), result.size());
979 return NO_ERROR;
980}
981
982status_t AudioPolicyService::onTransact(
Svet Ganovf4ddfef2018-01-16 07:37:58 -0800983 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags) {
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800984 // make sure transactions reserved to AudioFlinger do not come from other processes
985 switch (code) {
986 case TRANSACTION_startOutput:
987 case TRANSACTION_stopOutput:
988 case TRANSACTION_releaseOutput:
989 case TRANSACTION_getInputForAttr:
990 case TRANSACTION_startInput:
991 case TRANSACTION_stopInput:
992 case TRANSACTION_releaseInput:
993 case TRANSACTION_getOutputForEffect:
994 case TRANSACTION_registerEffect:
995 case TRANSACTION_unregisterEffect:
996 case TRANSACTION_setEffectEnabled:
997 case TRANSACTION_getStrategyForStream:
998 case TRANSACTION_getOutputForAttr:
999 case TRANSACTION_moveEffectsToIo:
1000 ALOGW("%s: transaction %d received from PID %d",
1001 __func__, code, IPCThreadState::self()->getCallingPid());
1002 return INVALID_OPERATION;
1003 default:
1004 break;
1005 }
1006
1007 // make sure the following transactions come from system components
1008 switch (code) {
1009 case TRANSACTION_setDeviceConnectionState:
1010 case TRANSACTION_handleDeviceConfigChange:
1011 case TRANSACTION_setPhoneState:
1012//FIXME: Allow setForceUse calls from system apps until a better use case routing API is available
1013// case TRANSACTION_setForceUse:
1014 case TRANSACTION_initStreamVolume:
1015 case TRANSACTION_setStreamVolumeIndex:
1016 case TRANSACTION_setVolumeIndexForAttributes:
1017 case TRANSACTION_getStreamVolumeIndex:
1018 case TRANSACTION_getVolumeIndexForAttributes:
1019 case TRANSACTION_getMinVolumeIndexForAttributes:
1020 case TRANSACTION_getMaxVolumeIndexForAttributes:
1021 case TRANSACTION_isStreamActive:
1022 case TRANSACTION_isStreamActiveRemotely:
1023 case TRANSACTION_isSourceActive:
1024 case TRANSACTION_getDevicesForStream:
1025 case TRANSACTION_registerPolicyMixes:
1026 case TRANSACTION_setMasterMono:
1027 case TRANSACTION_getSurroundFormats:
Kriti Dang6537def2021-03-02 13:46:59 +01001028 case TRANSACTION_getReportedSurroundFormats:
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001029 case TRANSACTION_setSurroundFormatEnabled:
1030 case TRANSACTION_setAssistantUid:
1031 case TRANSACTION_setA11yServicesUids:
1032 case TRANSACTION_setUidDeviceAffinities:
1033 case TRANSACTION_removeUidDeviceAffinities:
1034 case TRANSACTION_setUserIdDeviceAffinities:
1035 case TRANSACTION_removeUserIdDeviceAffinities:
1036 case TRANSACTION_getHwOffloadEncodingFormatsSupportedForA2DP:
1037 case TRANSACTION_listAudioVolumeGroups:
1038 case TRANSACTION_getVolumeGroupFromAudioAttributes:
1039 case TRANSACTION_acquireSoundTriggerSession:
1040 case TRANSACTION_releaseSoundTriggerSession:
1041 case TRANSACTION_setRttEnabled:
1042 case TRANSACTION_isCallScreenModeSupported:
1043 case TRANSACTION_setDevicesRoleForStrategy:
1044 case TRANSACTION_setSupportedSystemUsages:
1045 case TRANSACTION_removeDevicesRoleForStrategy:
1046 case TRANSACTION_getDevicesForRoleAndStrategy:
1047 case TRANSACTION_getDevicesForAttributes:
1048 case TRANSACTION_setAllowedCapturePolicy:
1049 case TRANSACTION_onNewAudioModulesAvailable:
1050 case TRANSACTION_setCurrentImeUid:
1051 case TRANSACTION_registerSoundTriggerCaptureStateListener:
1052 case TRANSACTION_setDevicesRoleForCapturePreset:
1053 case TRANSACTION_addDevicesRoleForCapturePreset:
1054 case TRANSACTION_removeDevicesRoleForCapturePreset:
1055 case TRANSACTION_clearDevicesRoleForCapturePreset:
Eric Laurent6d607012021-07-05 11:54:40 +02001056 case TRANSACTION_getDevicesForRoleAndCapturePreset:
1057 case TRANSACTION_getSpatializer: {
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001058 if (!isServiceUid(IPCThreadState::self()->getCallingUid())) {
1059 ALOGW("%s: transaction %d received from PID %d unauthorized UID %d",
1060 __func__, code, IPCThreadState::self()->getCallingPid(),
1061 IPCThreadState::self()->getCallingUid());
1062 return INVALID_OPERATION;
1063 }
1064 } break;
1065 default:
1066 break;
1067 }
1068
1069 std::string tag("IAudioPolicyService command " + std::to_string(code));
1070 TimeCheck check(tag.c_str());
1071
Svet Ganovf4ddfef2018-01-16 07:37:58 -08001072 switch (code) {
1073 case SHELL_COMMAND_TRANSACTION: {
1074 int in = data.readFileDescriptor();
1075 int out = data.readFileDescriptor();
1076 int err = data.readFileDescriptor();
1077 int argc = data.readInt32();
1078 Vector<String16> args;
1079 for (int i = 0; i < argc && data.dataAvail() > 0; i++) {
1080 args.add(data.readString16());
1081 }
1082 sp<IBinder> unusedCallback;
1083 sp<IResultReceiver> resultReceiver;
1084 status_t status;
1085 if ((status = data.readNullableStrongBinder(&unusedCallback)) != NO_ERROR) {
1086 return status;
1087 }
1088 if ((status = data.readNullableStrongBinder(&resultReceiver)) != NO_ERROR) {
1089 return status;
1090 }
1091 status = shellCommand(in, out, err, args);
1092 if (resultReceiver != nullptr) {
1093 resultReceiver->send(status);
1094 }
1095 return NO_ERROR;
1096 }
1097 }
1098
Mathias Agopian65ab4712010-07-14 17:59:35 -07001099 return BnAudioPolicyService::onTransact(code, data, reply, flags);
1100}
1101
Svet Ganovf4ddfef2018-01-16 07:37:58 -08001102// ------------------- Shell command implementation -------------------
1103
1104// NOTE: This is a remote API - make sure all args are validated
1105status_t AudioPolicyService::shellCommand(int in, int out, int err, Vector<String16>& args) {
1106 if (!checkCallingPermission(sManageAudioPolicyPermission, nullptr, nullptr)) {
1107 return PERMISSION_DENIED;
1108 }
1109 if (in == BAD_TYPE || out == BAD_TYPE || err == BAD_TYPE) {
1110 return BAD_VALUE;
1111 }
jovanakbe066e12019-09-02 11:54:39 -07001112 if (args.size() >= 3 && args[0] == String16("set-uid-state")) {
Svet Ganovf4ddfef2018-01-16 07:37:58 -08001113 return handleSetUidState(args, err);
jovanakbe066e12019-09-02 11:54:39 -07001114 } else if (args.size() >= 2 && args[0] == String16("reset-uid-state")) {
Svet Ganovf4ddfef2018-01-16 07:37:58 -08001115 return handleResetUidState(args, err);
jovanakbe066e12019-09-02 11:54:39 -07001116 } else if (args.size() >= 2 && args[0] == String16("get-uid-state")) {
Svet Ganovf4ddfef2018-01-16 07:37:58 -08001117 return handleGetUidState(args, out, err);
Eric Laurent269acb42021-04-23 16:53:22 +02001118 } else if (args.size() >= 1 && args[0] == String16("purge_permission-cache")) {
1119 purgePermissionCache();
1120 return NO_ERROR;
Svet Ganovf4ddfef2018-01-16 07:37:58 -08001121 } else if (args.size() == 1 && args[0] == String16("help")) {
1122 printHelp(out);
1123 return NO_ERROR;
1124 }
1125 printHelp(err);
1126 return BAD_VALUE;
1127}
1128
jovanakbe066e12019-09-02 11:54:39 -07001129static status_t getUidForPackage(String16 packageName, int userId, /*inout*/uid_t& uid, int err) {
1130 if (userId < 0) {
1131 ALOGE("Invalid user: %d", userId);
1132 dprintf(err, "Invalid user: %d\n", userId);
Svet Ganovf4ddfef2018-01-16 07:37:58 -08001133 return BAD_VALUE;
1134 }
jovanakbe066e12019-09-02 11:54:39 -07001135
1136 PermissionController pc;
1137 uid = pc.getPackageUid(packageName, 0);
1138 if (uid <= 0) {
1139 ALOGE("Unknown package: '%s'", String8(packageName).string());
1140 dprintf(err, "Unknown package: '%s'\n", String8(packageName).string());
1141 return BAD_VALUE;
1142 }
1143
1144 uid = multiuser_get_uid(userId, uid);
1145 return NO_ERROR;
1146}
1147
1148status_t AudioPolicyService::handleSetUidState(Vector<String16>& args, int err) {
1149 // Valid arg.size() is 3 or 5, args.size() is 5 with --user option.
1150 if (!(args.size() == 3 || args.size() == 5)) {
1151 printHelp(err);
1152 return BAD_VALUE;
1153 }
1154
Svet Ganovf4ddfef2018-01-16 07:37:58 -08001155 bool active = false;
1156 if (args[2] == String16("active")) {
1157 active = true;
1158 } else if ((args[2] != String16("idle"))) {
1159 ALOGE("Expected active or idle but got: '%s'", String8(args[2]).string());
1160 return BAD_VALUE;
1161 }
jovanakbe066e12019-09-02 11:54:39 -07001162
1163 int userId = 0;
1164 if (args.size() >= 5 && args[3] == String16("--user")) {
1165 userId = atoi(String8(args[4]));
1166 }
1167
1168 uid_t uid;
1169 if (getUidForPackage(args[1], userId, uid, err) == BAD_VALUE) {
1170 return BAD_VALUE;
1171 }
1172
Mikhail Naganov12b716c2020-04-30 22:37:43 +00001173 sp<UidPolicy> uidPolicy;
1174 {
1175 Mutex::Autolock _l(mLock);
1176 uidPolicy = mUidPolicy;
1177 }
1178 if (uidPolicy) {
1179 uidPolicy->addOverrideUid(uid, active);
1180 return NO_ERROR;
1181 }
1182 return NO_INIT;
Svet Ganovf4ddfef2018-01-16 07:37:58 -08001183}
1184
1185status_t AudioPolicyService::handleResetUidState(Vector<String16>& args, int err) {
jovanakbe066e12019-09-02 11:54:39 -07001186 // Valid arg.size() is 2 or 4, args.size() is 4 with --user option.
1187 if (!(args.size() == 2 || args.size() == 4)) {
1188 printHelp(err);
Svet Ganovf4ddfef2018-01-16 07:37:58 -08001189 return BAD_VALUE;
1190 }
jovanakbe066e12019-09-02 11:54:39 -07001191
1192 int userId = 0;
1193 if (args.size() >= 4 && args[2] == String16("--user")) {
1194 userId = atoi(String8(args[3]));
1195 }
1196
1197 uid_t uid;
1198 if (getUidForPackage(args[1], userId, uid, err) == BAD_VALUE) {
1199 return BAD_VALUE;
1200 }
1201
Mikhail Naganov12b716c2020-04-30 22:37:43 +00001202 sp<UidPolicy> uidPolicy;
1203 {
1204 Mutex::Autolock _l(mLock);
1205 uidPolicy = mUidPolicy;
1206 }
1207 if (uidPolicy) {
1208 uidPolicy->removeOverrideUid(uid);
1209 return NO_ERROR;
1210 }
1211 return NO_INIT;
Svet Ganovf4ddfef2018-01-16 07:37:58 -08001212}
1213
1214status_t AudioPolicyService::handleGetUidState(Vector<String16>& args, int out, int err) {
jovanakbe066e12019-09-02 11:54:39 -07001215 // Valid arg.size() is 2 or 4, args.size() is 4 with --user option.
1216 if (!(args.size() == 2 || args.size() == 4)) {
1217 printHelp(err);
Svet Ganovf4ddfef2018-01-16 07:37:58 -08001218 return BAD_VALUE;
1219 }
jovanakbe066e12019-09-02 11:54:39 -07001220
1221 int userId = 0;
1222 if (args.size() >= 4 && args[2] == String16("--user")) {
1223 userId = atoi(String8(args[3]));
1224 }
1225
1226 uid_t uid;
1227 if (getUidForPackage(args[1], userId, uid, err) == BAD_VALUE) {
1228 return BAD_VALUE;
1229 }
1230
Mikhail Naganov12b716c2020-04-30 22:37:43 +00001231 sp<UidPolicy> uidPolicy;
1232 {
1233 Mutex::Autolock _l(mLock);
1234 uidPolicy = mUidPolicy;
Svet Ganovf4ddfef2018-01-16 07:37:58 -08001235 }
Mikhail Naganov12b716c2020-04-30 22:37:43 +00001236 if (uidPolicy) {
1237 return dprintf(out, uidPolicy->isUidActive(uid) ? "active\n" : "idle\n");
1238 }
1239 return NO_INIT;
Svet Ganovf4ddfef2018-01-16 07:37:58 -08001240}
1241
1242status_t AudioPolicyService::printHelp(int out) {
1243 return dprintf(out, "Audio policy service commands:\n"
jovanakbe066e12019-09-02 11:54:39 -07001244 " get-uid-state <PACKAGE> [--user USER_ID] gets the uid state\n"
1245 " set-uid-state <PACKAGE> <active|idle> [--user USER_ID] overrides the uid state\n"
1246 " reset-uid-state <PACKAGE> [--user USER_ID] clears the uid state override\n"
Svet Ganovf4ddfef2018-01-16 07:37:58 -08001247 " help print this message\n");
1248}
1249
1250// ----------- AudioPolicyService::UidPolicy implementation ----------
1251
1252void AudioPolicyService::UidPolicy::registerSelf() {
Steven Moreland2f348142019-07-02 15:59:07 -07001253 status_t res = mAm.linkToDeath(this);
1254 mAm.registerUidObserver(this, ActivityManager::UID_OBSERVER_GONE
Svet Ganovf4ddfef2018-01-16 07:37:58 -08001255 | ActivityManager::UID_OBSERVER_IDLE
Eric Laurente8c8b432018-10-17 10:08:02 -07001256 | ActivityManager::UID_OBSERVER_ACTIVE
1257 | ActivityManager::UID_OBSERVER_PROCSTATE,
Svet Ganovf4ddfef2018-01-16 07:37:58 -08001258 ActivityManager::PROCESS_STATE_UNKNOWN,
1259 String16("audioserver"));
Mikhail Naganoveae73eb2018-04-03 16:57:36 -07001260 if (!res) {
1261 Mutex::Autolock _l(mLock);
1262 mObserverRegistered = true;
1263 } else {
1264 ALOGE("UidPolicy::registerSelf linkToDeath failed: %d", res);
Eric Laurent4eb58f12018-12-07 16:41:02 -08001265
Steven Moreland2f348142019-07-02 15:59:07 -07001266 mAm.unregisterUidObserver(this);
Mikhail Naganoveae73eb2018-04-03 16:57:36 -07001267 }
Svet Ganovf4ddfef2018-01-16 07:37:58 -08001268}
1269
1270void AudioPolicyService::UidPolicy::unregisterSelf() {
Steven Moreland2f348142019-07-02 15:59:07 -07001271 mAm.unlinkToDeath(this);
1272 mAm.unregisterUidObserver(this);
Mikhail Naganoveae73eb2018-04-03 16:57:36 -07001273 Mutex::Autolock _l(mLock);
1274 mObserverRegistered = false;
Svet Ganovf4ddfef2018-01-16 07:37:58 -08001275}
1276
Mikhail Naganoveae73eb2018-04-03 16:57:36 -07001277void AudioPolicyService::UidPolicy::binderDied(__unused const wp<IBinder> &who) {
1278 Mutex::Autolock _l(mLock);
1279 mCachedUids.clear();
1280 mObserverRegistered = false;
Svet Ganovf4ddfef2018-01-16 07:37:58 -08001281}
1282
Eric Laurente8c8b432018-10-17 10:08:02 -07001283void AudioPolicyService::UidPolicy::checkRegistered() {
Mikhail Naganoveae73eb2018-04-03 16:57:36 -07001284 bool needToReregister = false;
1285 {
1286 Mutex::Autolock _l(mLock);
1287 needToReregister = !mObserverRegistered;
Svet Ganovf4ddfef2018-01-16 07:37:58 -08001288 }
Mikhail Naganoveae73eb2018-04-03 16:57:36 -07001289 if (needToReregister) {
1290 // Looks like ActivityManager has died previously, attempt to re-register.
1291 registerSelf();
1292 }
Eric Laurente8c8b432018-10-17 10:08:02 -07001293}
1294
1295bool AudioPolicyService::UidPolicy::isUidActive(uid_t uid) {
1296 if (isServiceUid(uid)) return true;
1297 checkRegistered();
Mikhail Naganoveae73eb2018-04-03 16:57:36 -07001298 {
1299 Mutex::Autolock _l(mLock);
1300 auto overrideIter = mOverrideUids.find(uid);
1301 if (overrideIter != mOverrideUids.end()) {
Eric Laurente8c8b432018-10-17 10:08:02 -07001302 return overrideIter->second.first;
Mikhail Naganoveae73eb2018-04-03 16:57:36 -07001303 }
1304 // In an absense of the ActivityManager, assume everything to be active.
1305 if (!mObserverRegistered) return true;
1306 auto cacheIter = mCachedUids.find(uid);
Mikhail Naganoveba668a2018-04-05 08:13:15 -07001307 if (cacheIter != mCachedUids.end()) {
Eric Laurente8c8b432018-10-17 10:08:02 -07001308 return cacheIter->second.first;
Mikhail Naganoveae73eb2018-04-03 16:57:36 -07001309 }
1310 }
1311 ActivityManager am;
Hui Yu12c7ec72020-05-04 17:40:52 +00001312 bool active = am.isUidActive(uid, String16("audioserver"));
Mikhail Naganoveae73eb2018-04-03 16:57:36 -07001313 {
1314 Mutex::Autolock _l(mLock);
Eric Laurente8c8b432018-10-17 10:08:02 -07001315 mCachedUids.insert(std::pair<uid_t,
1316 std::pair<bool, int>>(uid, std::pair<bool, int>(active,
1317 ActivityManager::PROCESS_STATE_UNKNOWN)));
Mikhail Naganoveae73eb2018-04-03 16:57:36 -07001318 }
1319 return active;
Svet Ganovf4ddfef2018-01-16 07:37:58 -08001320}
1321
Eric Laurente8c8b432018-10-17 10:08:02 -07001322int AudioPolicyService::UidPolicy::getUidState(uid_t uid) {
1323 if (isServiceUid(uid)) {
1324 return ActivityManager::PROCESS_STATE_TOP;
1325 }
1326 checkRegistered();
1327 {
1328 Mutex::Autolock _l(mLock);
1329 auto overrideIter = mOverrideUids.find(uid);
1330 if (overrideIter != mOverrideUids.end()) {
1331 if (overrideIter->second.first) {
1332 if (overrideIter->second.second != ActivityManager::PROCESS_STATE_UNKNOWN) {
1333 return overrideIter->second.second;
1334 } else {
1335 auto cacheIter = mCachedUids.find(uid);
1336 if (cacheIter != mCachedUids.end()) {
1337 return cacheIter->second.second;
1338 }
1339 }
1340 }
1341 return ActivityManager::PROCESS_STATE_UNKNOWN;
1342 }
1343 // In an absense of the ActivityManager, assume everything to be active.
1344 if (!mObserverRegistered) {
1345 return ActivityManager::PROCESS_STATE_TOP;
1346 }
1347 auto cacheIter = mCachedUids.find(uid);
1348 if (cacheIter != mCachedUids.end()) {
1349 if (cacheIter->second.first) {
1350 return cacheIter->second.second;
1351 } else {
1352 return ActivityManager::PROCESS_STATE_UNKNOWN;
1353 }
1354 }
1355 }
1356 ActivityManager am;
Hui Yu12c7ec72020-05-04 17:40:52 +00001357 bool active = am.isUidActive(uid, String16("audioserver"));
Eric Laurente8c8b432018-10-17 10:08:02 -07001358 int state = ActivityManager::PROCESS_STATE_UNKNOWN;
1359 if (active) {
1360 state = am.getUidProcessState(uid, String16("audioserver"));
1361 }
1362 {
1363 Mutex::Autolock _l(mLock);
1364 mCachedUids.insert(std::pair<uid_t,
1365 std::pair<bool, int>>(uid, std::pair<bool, int>(active, state)));
1366 }
Eric Laurent4eb58f12018-12-07 16:41:02 -08001367
Eric Laurente8c8b432018-10-17 10:08:02 -07001368 return state;
1369}
1370
Mikhail Naganoveae73eb2018-04-03 16:57:36 -07001371void AudioPolicyService::UidPolicy::onUidActive(uid_t uid) {
Eric Laurente8c8b432018-10-17 10:08:02 -07001372 updateUid(&mCachedUids, uid, true, ActivityManager::PROCESS_STATE_UNKNOWN, true);
Mikhail Naganoveae73eb2018-04-03 16:57:36 -07001373}
1374
1375void AudioPolicyService::UidPolicy::onUidGone(uid_t uid, __unused bool disabled) {
Eric Laurente8c8b432018-10-17 10:08:02 -07001376 updateUid(&mCachedUids, uid, false, ActivityManager::PROCESS_STATE_UNKNOWN, false);
Mikhail Naganoveae73eb2018-04-03 16:57:36 -07001377}
1378
1379void AudioPolicyService::UidPolicy::onUidIdle(uid_t uid, __unused bool disabled) {
Eric Laurente8c8b432018-10-17 10:08:02 -07001380 updateUid(&mCachedUids, uid, false, ActivityManager::PROCESS_STATE_UNKNOWN, true);
Mikhail Naganoveae73eb2018-04-03 16:57:36 -07001381}
1382
Eric Laurente8c8b432018-10-17 10:08:02 -07001383void AudioPolicyService::UidPolicy::onUidStateChanged(uid_t uid,
1384 int32_t procState,
Hui Yu13ad0eb2019-09-09 10:27:07 -07001385 int64_t procStateSeq __unused,
1386 int32_t capability __unused) {
Eric Laurente8c8b432018-10-17 10:08:02 -07001387 if (procState != ActivityManager::PROCESS_STATE_UNKNOWN) {
1388 updateUid(&mCachedUids, uid, true, procState, true);
Mikhail Naganoveae73eb2018-04-03 16:57:36 -07001389 }
1390}
1391
1392void AudioPolicyService::UidPolicy::updateOverrideUid(uid_t uid, bool active, bool insert) {
Eric Laurente8c8b432018-10-17 10:08:02 -07001393 updateUid(&mOverrideUids, uid, active, ActivityManager::PROCESS_STATE_UNKNOWN, insert);
1394}
1395
1396void AudioPolicyService::UidPolicy::notifyService() {
1397 sp<AudioPolicyService> service = mService.promote();
1398 if (service != nullptr) {
1399 service->updateUidStates();
Mikhail Naganoveae73eb2018-04-03 16:57:36 -07001400 }
1401}
1402
Eric Laurente8c8b432018-10-17 10:08:02 -07001403void AudioPolicyService::UidPolicy::updateUid(std::unordered_map<uid_t,
1404 std::pair<bool, int>> *uids,
1405 uid_t uid,
1406 bool active,
1407 int state,
1408 bool insert) {
1409 if (isServiceUid(uid)) {
1410 return;
1411 }
1412 bool wasActive = isUidActive(uid);
1413 int previousState = getUidState(uid);
Mikhail Naganoveae73eb2018-04-03 16:57:36 -07001414 {
1415 Mutex::Autolock _l(mLock);
Eric Laurente8c8b432018-10-17 10:08:02 -07001416 updateUidLocked(uids, uid, active, state, insert);
Mikhail Naganoveae73eb2018-04-03 16:57:36 -07001417 }
Eric Laurente8c8b432018-10-17 10:08:02 -07001418 if (wasActive != isUidActive(uid) || state != previousState) {
1419 notifyService();
1420 }
Mikhail Naganoveae73eb2018-04-03 16:57:36 -07001421}
1422
Eric Laurente8c8b432018-10-17 10:08:02 -07001423void AudioPolicyService::UidPolicy::updateUidLocked(std::unordered_map<uid_t,
1424 std::pair<bool, int>> *uids,
1425 uid_t uid,
1426 bool active,
1427 int state,
1428 bool insert) {
Mikhail Naganoveae73eb2018-04-03 16:57:36 -07001429 auto it = uids->find(uid);
1430 if (it != uids->end()) {
Mikhail Naganoveae73eb2018-04-03 16:57:36 -07001431 if (insert) {
Eric Laurente8c8b432018-10-17 10:08:02 -07001432 if (state == ActivityManager::PROCESS_STATE_UNKNOWN) {
1433 it->second.first = active;
1434 }
1435 if (it->second.first) {
1436 it->second.second = state;
1437 } else {
1438 it->second.second = ActivityManager::PROCESS_STATE_UNKNOWN;
1439 }
Mikhail Naganoveae73eb2018-04-03 16:57:36 -07001440 } else {
1441 uids->erase(it);
1442 }
Eric Laurente8c8b432018-10-17 10:08:02 -07001443 } else if (insert && (state == ActivityManager::PROCESS_STATE_UNKNOWN)) {
1444 uids->insert(std::pair<uid_t, std::pair<bool, int>>(uid,
1445 std::pair<bool, int>(active, state)));
Mikhail Naganoveae73eb2018-04-03 16:57:36 -07001446 }
Svet Ganovf4ddfef2018-01-16 07:37:58 -08001447}
Mathias Agopian65ab4712010-07-14 17:59:35 -07001448
Eric Laurent4eb58f12018-12-07 16:41:02 -08001449bool AudioPolicyService::UidPolicy::isA11yOnTop() {
1450 for (const auto &uid : mCachedUids) {
Eric Laurent47670c92019-08-28 16:59:05 -07001451 if (!isA11yUid(uid.first)) {
Eric Laurent4eb58f12018-12-07 16:41:02 -08001452 continue;
1453 }
Amith Yamasanibcbb3002019-01-23 13:53:33 -08001454 if (uid.second.second >= ActivityManager::PROCESS_STATE_TOP
1455 && uid.second.second <= ActivityManager::PROCESS_STATE_BOUND_FOREGROUND_SERVICE) {
Eric Laurent4eb58f12018-12-07 16:41:02 -08001456 return true;
1457 }
1458 }
1459 return false;
1460}
1461
Eric Laurentb78763e2018-10-17 10:08:02 -07001462bool AudioPolicyService::UidPolicy::isA11yUid(uid_t uid)
1463{
1464 std::vector<uid_t>::iterator it = find(mA11yUids.begin(), mA11yUids.end(), uid);
1465 return it != mA11yUids.end();
1466}
1467
Michael Groovercfd28302018-12-11 19:16:46 -08001468// ----------- AudioPolicyService::SensorPrivacyService implementation ----------
1469void AudioPolicyService::SensorPrivacyPolicy::registerSelf() {
1470 SensorPrivacyManager spm;
1471 mSensorPrivacyEnabled = spm.isSensorPrivacyEnabled();
1472 spm.addSensorPrivacyListener(this);
1473}
1474
Evan Severson241d9592021-01-08 12:16:02 -08001475void AudioPolicyService::SensorPrivacyPolicy::registerSelfForMicrophoneOnly(int userId) {
1476 SensorPrivacyManager spm;
1477 mSensorPrivacyEnabled = spm.isIndividualSensorPrivacyEnabled(userId,
1478 SensorPrivacyManager::INDIVIDUAL_SENSOR_MICROPHONE);
1479 spm.addIndividualSensorPrivacyListener(userId,
1480 SensorPrivacyManager::INDIVIDUAL_SENSOR_MICROPHONE, this);
1481}
1482
Michael Groovercfd28302018-12-11 19:16:46 -08001483void AudioPolicyService::SensorPrivacyPolicy::unregisterSelf() {
1484 SensorPrivacyManager spm;
1485 spm.removeSensorPrivacyListener(this);
1486}
1487
1488bool AudioPolicyService::SensorPrivacyPolicy::isSensorPrivacyEnabled() {
1489 return mSensorPrivacyEnabled;
1490}
1491
1492binder::Status AudioPolicyService::SensorPrivacyPolicy::onSensorPrivacyChanged(bool enabled) {
1493 mSensorPrivacyEnabled = enabled;
1494 sp<AudioPolicyService> service = mService.promote();
1495 if (service != nullptr) {
1496 service->updateUidStates();
1497 }
1498 return binder::Status::ok();
1499}
1500
Eric Laurented726cc2021-07-01 14:26:41 +02001501// ----------- AudioPolicyService::OpRecordAudioMonitor implementation ----------
1502
1503// static
1504sp<AudioPolicyService::OpRecordAudioMonitor>
1505AudioPolicyService::OpRecordAudioMonitor::createIfNeeded(
1506 const AttributionSourceState& attributionSource, const audio_attributes_t& attr,
1507 wp<AudioCommandThread> commandThread)
1508{
Eric Laurent987ce102021-07-05 12:11:51 +02001509 if (isAudioServerOrRootUid(attributionSource.uid)) {
1510 ALOGV("not silencing record for audio or root source %s",
Eric Laurented726cc2021-07-01 14:26:41 +02001511 attributionSource.toString().c_str());
1512 return nullptr;
1513 }
1514
1515 if (!AudioPolicyService::isAppOpSource(attr.source)) {
1516 ALOGD("not monitoring app op for uid %d and source %d",
1517 attributionSource.uid, attr.source);
1518 return nullptr;
1519 }
1520
1521 if (!attributionSource.packageName.has_value()
1522 || attributionSource.packageName.value().size() == 0) {
1523 return nullptr;
1524 }
1525 return new OpRecordAudioMonitor(attributionSource, getOpForSource(attr.source), commandThread);
1526}
1527
1528AudioPolicyService::OpRecordAudioMonitor::OpRecordAudioMonitor(
1529 const AttributionSourceState& attributionSource, int32_t appOp,
1530 wp<AudioCommandThread> commandThread) :
1531 mHasOp(true), mAttributionSource(attributionSource), mAppOp(appOp),
1532 mCommandThread(commandThread)
1533{
1534}
1535
1536AudioPolicyService::OpRecordAudioMonitor::~OpRecordAudioMonitor()
1537{
1538 if (mOpCallback != 0) {
1539 mAppOpsManager.stopWatchingMode(mOpCallback);
1540 }
1541 mOpCallback.clear();
1542}
1543
1544void AudioPolicyService::OpRecordAudioMonitor::onFirstRef()
1545{
1546 checkOp();
1547 mOpCallback = new RecordAudioOpCallback(this);
1548 ALOGV("start watching op %d for %s", mAppOp, mAttributionSource.toString().c_str());
1549 // TODO: We need to always watch AppOpsManager::OP_RECORD_AUDIO too
1550 // since it controls the mic permission for legacy apps.
1551 mAppOpsManager.startWatchingMode(mAppOp, VALUE_OR_FATAL(aidl2legacy_string_view_String16(
1552 mAttributionSource.packageName.value_or(""))),
1553 mOpCallback);
1554}
1555
1556bool AudioPolicyService::OpRecordAudioMonitor::hasOp() const {
1557 return mHasOp.load();
1558}
1559
1560// Called by RecordAudioOpCallback when the app op corresponding to this OpRecordAudioMonitor
1561// is updated in AppOp callback and in onFirstRef()
1562// Note this method is never called (and never to be) for audio server / root track
1563// due to the UID in createIfNeeded(). As a result for those record track, it's:
1564// - not called from constructor,
1565// - not called from RecordAudioOpCallback because the callback is not installed in this case
1566void AudioPolicyService::OpRecordAudioMonitor::checkOp(bool updateUidStates)
1567{
1568 // TODO: We need to always check AppOpsManager::OP_RECORD_AUDIO too
1569 // since it controls the mic permission for legacy apps.
1570 const int32_t mode = mAppOpsManager.checkOp(mAppOp,
1571 mAttributionSource.uid, VALUE_OR_FATAL(aidl2legacy_string_view_String16(
1572 mAttributionSource.packageName.value_or(""))));
1573 const bool hasIt = (mode == AppOpsManager::MODE_ALLOWED);
1574 // verbose logging only log when appOp changed
1575 ALOGI_IF(hasIt != mHasOp.load(),
1576 "App op %d missing, %ssilencing record %s",
1577 mAppOp, hasIt ? "un" : "", mAttributionSource.toString().c_str());
1578 mHasOp.store(hasIt);
1579
1580 if (updateUidStates) {
1581 sp<AudioCommandThread> commandThread = mCommandThread.promote();
1582 if (commandThread != nullptr) {
1583 commandThread->updateUidStatesCommand();
1584 }
1585 }
1586}
1587
1588AudioPolicyService::OpRecordAudioMonitor::RecordAudioOpCallback::RecordAudioOpCallback(
1589 const wp<OpRecordAudioMonitor>& monitor) : mMonitor(monitor)
1590{ }
1591
1592void AudioPolicyService::OpRecordAudioMonitor::RecordAudioOpCallback::opChanged(int32_t op,
1593 const String16& packageName __unused) {
1594 sp<OpRecordAudioMonitor> monitor = mMonitor.promote();
1595 if (monitor != NULL) {
1596 if (op != monitor->getOp()) {
1597 return;
1598 }
1599 monitor->checkOp(true);
1600 }
1601}
1602
1603
Mathias Agopian65ab4712010-07-14 17:59:35 -07001604// ----------- AudioPolicyService::AudioCommandThread implementation ----------
1605
Eric Laurentbfb1b832013-01-07 09:53:42 -08001606AudioPolicyService::AudioCommandThread::AudioCommandThread(String8 name,
1607 const wp<AudioPolicyService>& service)
1608 : Thread(false), mName(name), mService(service)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001609{
Mathias Agopian65ab4712010-07-14 17:59:35 -07001610}
1611
1612
1613AudioPolicyService::AudioCommandThread::~AudioCommandThread()
1614{
Eric Laurentbfb1b832013-01-07 09:53:42 -08001615 if (!mAudioCommands.isEmpty()) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07001616 release_wake_lock(mName.string());
1617 }
1618 mAudioCommands.clear();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001619}
1620
1621void AudioPolicyService::AudioCommandThread::onFirstRef()
1622{
Eric Laurentbfb1b832013-01-07 09:53:42 -08001623 run(mName.string(), ANDROID_PRIORITY_AUDIO);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001624}
1625
1626bool AudioPolicyService::AudioCommandThread::threadLoop()
1627{
Eric Laurentd7eda8d2016-02-02 17:18:39 -08001628 nsecs_t waitTime = -1;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001629
1630 mLock.lock();
1631 while (!exitPending())
1632 {
Eric Laurent59a89232014-06-08 14:14:17 -07001633 sp<AudioPolicyService> svc;
1634 while (!mAudioCommands.isEmpty() && !exitPending()) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07001635 nsecs_t curTime = systemTime();
1636 // commands are sorted by increasing time stamp: execute them from index 0 and up
1637 if (mAudioCommands[0]->mTime <= curTime) {
Eric Laurent0ede8922014-05-09 18:04:42 -07001638 sp<AudioCommand> command = mAudioCommands[0];
Mathias Agopian65ab4712010-07-14 17:59:35 -07001639 mAudioCommands.removeAt(0);
Eric Laurent0ede8922014-05-09 18:04:42 -07001640 mLastCommand = command;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001641
1642 switch (command->mCommand) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07001643 case SET_VOLUME: {
Eric Laurent0ede8922014-05-09 18:04:42 -07001644 VolumeData *data = (VolumeData *)command->mParam.get();
Steve Block3856b092011-10-20 11:56:00 +01001645 ALOGV("AudioCommandThread() processing set volume stream %d, \
Eric Laurentde070132010-07-13 04:45:46 -07001646 volume %f, output %d", data->mStream, data->mVolume, data->mIO);
Andy Hungfe726a62018-09-27 15:17:25 -07001647 mLock.unlock();
Eric Laurentde070132010-07-13 04:45:46 -07001648 command->mStatus = AudioSystem::setStreamVolume(data->mStream,
1649 data->mVolume,
1650 data->mIO);
Andy Hungfe726a62018-09-27 15:17:25 -07001651 mLock.lock();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001652 }break;
1653 case SET_PARAMETERS: {
Eric Laurent0ede8922014-05-09 18:04:42 -07001654 ParametersData *data = (ParametersData *)command->mParam.get();
Glenn Kastene53b9ea2012-03-12 16:29:55 -07001655 ALOGV("AudioCommandThread() processing set parameters string %s, io %d",
1656 data->mKeyValuePairs.string(), data->mIO);
Andy Hungfe726a62018-09-27 15:17:25 -07001657 mLock.unlock();
Glenn Kastene53b9ea2012-03-12 16:29:55 -07001658 command->mStatus = AudioSystem::setParameters(data->mIO, data->mKeyValuePairs);
Andy Hungfe726a62018-09-27 15:17:25 -07001659 mLock.lock();
Glenn Kastene53b9ea2012-03-12 16:29:55 -07001660 }break;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001661 case SET_VOICE_VOLUME: {
Eric Laurent0ede8922014-05-09 18:04:42 -07001662 VoiceVolumeData *data = (VoiceVolumeData *)command->mParam.get();
Steve Block3856b092011-10-20 11:56:00 +01001663 ALOGV("AudioCommandThread() processing set voice volume volume %f",
Eric Laurentde070132010-07-13 04:45:46 -07001664 data->mVolume);
Andy Hungfe726a62018-09-27 15:17:25 -07001665 mLock.unlock();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001666 command->mStatus = AudioSystem::setVoiceVolume(data->mVolume);
Andy Hungfe726a62018-09-27 15:17:25 -07001667 mLock.lock();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001668 }break;
Eric Laurentbfb1b832013-01-07 09:53:42 -08001669 case STOP_OUTPUT: {
Eric Laurent0ede8922014-05-09 18:04:42 -07001670 StopOutputData *data = (StopOutputData *)command->mParam.get();
Eric Laurentd7fe0862018-07-14 16:48:01 -07001671 ALOGV("AudioCommandThread() processing stop output portId %d",
1672 data->mPortId);
Eric Laurent59a89232014-06-08 14:14:17 -07001673 svc = mService.promote();
Eric Laurentbfb1b832013-01-07 09:53:42 -08001674 if (svc == 0) {
1675 break;
1676 }
1677 mLock.unlock();
Eric Laurentd7fe0862018-07-14 16:48:01 -07001678 svc->doStopOutput(data->mPortId);
Eric Laurentbfb1b832013-01-07 09:53:42 -08001679 mLock.lock();
Eric Laurentbfb1b832013-01-07 09:53:42 -08001680 }break;
1681 case RELEASE_OUTPUT: {
Eric Laurent0ede8922014-05-09 18:04:42 -07001682 ReleaseOutputData *data = (ReleaseOutputData *)command->mParam.get();
Eric Laurentd7fe0862018-07-14 16:48:01 -07001683 ALOGV("AudioCommandThread() processing release output portId %d",
1684 data->mPortId);
Eric Laurent59a89232014-06-08 14:14:17 -07001685 svc = mService.promote();
Eric Laurentbfb1b832013-01-07 09:53:42 -08001686 if (svc == 0) {
1687 break;
1688 }
1689 mLock.unlock();
Eric Laurentd7fe0862018-07-14 16:48:01 -07001690 svc->doReleaseOutput(data->mPortId);
Eric Laurentbfb1b832013-01-07 09:53:42 -08001691 mLock.lock();
Eric Laurentbfb1b832013-01-07 09:53:42 -08001692 }break;
Eric Laurent951f4552014-05-20 10:48:17 -07001693 case CREATE_AUDIO_PATCH: {
1694 CreateAudioPatchData *data = (CreateAudioPatchData *)command->mParam.get();
1695 ALOGV("AudioCommandThread() processing create audio patch");
1696 sp<IAudioFlinger> af = AudioSystem::get_audio_flinger();
1697 if (af == 0) {
1698 command->mStatus = PERMISSION_DENIED;
1699 } else {
Andy Hungfe726a62018-09-27 15:17:25 -07001700 mLock.unlock();
Eric Laurent951f4552014-05-20 10:48:17 -07001701 command->mStatus = af->createAudioPatch(&data->mPatch, &data->mHandle);
Andy Hungfe726a62018-09-27 15:17:25 -07001702 mLock.lock();
Eric Laurent951f4552014-05-20 10:48:17 -07001703 }
1704 } break;
1705 case RELEASE_AUDIO_PATCH: {
1706 ReleaseAudioPatchData *data = (ReleaseAudioPatchData *)command->mParam.get();
1707 ALOGV("AudioCommandThread() processing release audio patch");
1708 sp<IAudioFlinger> af = AudioSystem::get_audio_flinger();
1709 if (af == 0) {
1710 command->mStatus = PERMISSION_DENIED;
1711 } else {
Andy Hungfe726a62018-09-27 15:17:25 -07001712 mLock.unlock();
Eric Laurent951f4552014-05-20 10:48:17 -07001713 command->mStatus = af->releaseAudioPatch(data->mHandle);
Andy Hungfe726a62018-09-27 15:17:25 -07001714 mLock.lock();
Eric Laurent951f4552014-05-20 10:48:17 -07001715 }
1716 } break;
Eric Laurentb52c1522014-05-20 11:27:36 -07001717 case UPDATE_AUDIOPORT_LIST: {
1718 ALOGV("AudioCommandThread() processing update audio port list");
Eric Laurent59a89232014-06-08 14:14:17 -07001719 svc = mService.promote();
Eric Laurentb52c1522014-05-20 11:27:36 -07001720 if (svc == 0) {
1721 break;
1722 }
1723 mLock.unlock();
1724 svc->doOnAudioPortListUpdate();
1725 mLock.lock();
1726 }break;
1727 case UPDATE_AUDIOPATCH_LIST: {
1728 ALOGV("AudioCommandThread() processing update audio patch list");
Eric Laurent59a89232014-06-08 14:14:17 -07001729 svc = mService.promote();
Eric Laurentb52c1522014-05-20 11:27:36 -07001730 if (svc == 0) {
1731 break;
1732 }
1733 mLock.unlock();
1734 svc->doOnAudioPatchListUpdate();
1735 mLock.lock();
1736 }break;
François Gaffiecfe17322018-11-07 13:41:29 +01001737 case CHANGED_AUDIOVOLUMEGROUP: {
1738 AudioVolumeGroupData *data =
1739 static_cast<AudioVolumeGroupData *>(command->mParam.get());
1740 ALOGV("AudioCommandThread() processing update audio volume group");
1741 svc = mService.promote();
1742 if (svc == 0) {
1743 break;
1744 }
1745 mLock.unlock();
1746 svc->doOnAudioVolumeGroupChanged(data->mGroup, data->mFlags);
1747 mLock.lock();
1748 }break;
Eric Laurente1715a42014-05-20 11:30:42 -07001749 case SET_AUDIOPORT_CONFIG: {
1750 SetAudioPortConfigData *data = (SetAudioPortConfigData *)command->mParam.get();
1751 ALOGV("AudioCommandThread() processing set port config");
1752 sp<IAudioFlinger> af = AudioSystem::get_audio_flinger();
1753 if (af == 0) {
1754 command->mStatus = PERMISSION_DENIED;
1755 } else {
Andy Hungfe726a62018-09-27 15:17:25 -07001756 mLock.unlock();
Eric Laurente1715a42014-05-20 11:30:42 -07001757 command->mStatus = af->setAudioPortConfig(&data->mConfig);
Andy Hungfe726a62018-09-27 15:17:25 -07001758 mLock.lock();
Eric Laurente1715a42014-05-20 11:30:42 -07001759 }
1760 } break;
Jean-Michel Trivide801052015-04-14 19:10:14 -07001761 case DYN_POLICY_MIX_STATE_UPDATE: {
1762 DynPolicyMixStateUpdateData *data =
1763 (DynPolicyMixStateUpdateData *)command->mParam.get();
Jean-Michel Trivide801052015-04-14 19:10:14 -07001764 ALOGV("AudioCommandThread() processing dyn policy mix state update %s %d",
1765 data->mRegId.string(), data->mState);
1766 svc = mService.promote();
1767 if (svc == 0) {
1768 break;
1769 }
1770 mLock.unlock();
1771 svc->doOnDynamicPolicyMixStateUpdate(data->mRegId, data->mState);
1772 mLock.lock();
1773 } break;
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -08001774 case RECORDING_CONFIGURATION_UPDATE: {
1775 RecordingConfigurationUpdateData *data =
1776 (RecordingConfigurationUpdateData *)command->mParam.get();
1777 ALOGV("AudioCommandThread() processing recording configuration update");
1778 svc = mService.promote();
1779 if (svc == 0) {
1780 break;
1781 }
1782 mLock.unlock();
Jean-Michel Triviac4e4292016-12-22 11:39:31 -08001783 svc->doOnRecordingConfigurationUpdate(data->mEvent, &data->mClientInfo,
Eric Laurenta9f86652018-11-28 17:23:11 -08001784 &data->mClientConfig, data->mClientEffects,
1785 &data->mDeviceConfig, data->mEffects,
1786 data->mPatchHandle, data->mSource);
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -08001787 mLock.lock();
1788 } break;
Eric Laurentb20cf7d2019-04-05 19:37:34 -07001789 case SET_EFFECT_SUSPENDED: {
1790 SetEffectSuspendedData *data = (SetEffectSuspendedData *)command->mParam.get();
1791 ALOGV("AudioCommandThread() processing set effect suspended");
1792 sp<IAudioFlinger> af = AudioSystem::get_audio_flinger();
1793 if (af != 0) {
1794 mLock.unlock();
1795 af->setEffectSuspended(data->mEffectId, data->mSessionId, data->mSuspended);
1796 mLock.lock();
1797 }
1798 } break;
Mikhail Naganov88b30d22020-03-09 19:43:13 +00001799 case AUDIO_MODULES_UPDATE: {
1800 ALOGV("AudioCommandThread() processing audio modules update");
1801 svc = mService.promote();
1802 if (svc == 0) {
1803 break;
1804 }
1805 mLock.unlock();
1806 svc->doOnNewAudioModulesAvailable();
1807 mLock.lock();
1808 } break;
Jean-Michel Trivi9a6b9ad2020-10-22 16:46:43 -07001809 case ROUTING_UPDATED: {
1810 ALOGV("AudioCommandThread() processing routing update");
1811 svc = mService.promote();
1812 if (svc == 0) {
1813 break;
1814 }
1815 mLock.unlock();
1816 svc->doOnRoutingUpdated();
1817 mLock.lock();
1818 } break;
Eric Laurentb20cf7d2019-04-05 19:37:34 -07001819
Eric Laurented726cc2021-07-01 14:26:41 +02001820 case UPDATE_UID_STATES: {
1821 ALOGV("AudioCommandThread() processing updateUID states");
1822 svc = mService.promote();
1823 if (svc == 0) {
1824 break;
1825 }
1826 mLock.unlock();
1827 svc->updateUidStates();
1828 mLock.lock();
1829 } break;
1830
Eric Laurent6d607012021-07-05 11:54:40 +02001831 case CHECK_SPATIALIZER: {
1832 ALOGV("AudioCommandThread() processing updateUID states");
1833 svc = mService.promote();
1834 if (svc == 0) {
1835 break;
1836 }
1837 mLock.unlock();
1838 svc->doOnCheckSpatializer();
1839 mLock.lock();
1840 } break;
1841
Mathias Agopian65ab4712010-07-14 17:59:35 -07001842 default:
Steve Block5ff1dd52012-01-05 23:22:43 +00001843 ALOGW("AudioCommandThread() unknown command %d", command->mCommand);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001844 }
Eric Laurent0ede8922014-05-09 18:04:42 -07001845 {
1846 Mutex::Autolock _l(command->mLock);
1847 if (command->mWaitStatus) {
1848 command->mWaitStatus = false;
1849 command->mCond.signal();
1850 }
1851 }
Eric Laurentd7eda8d2016-02-02 17:18:39 -08001852 waitTime = -1;
Zach Janga754b4f2015-10-27 01:29:34 +00001853 // release mLock before releasing strong reference on the service as
1854 // AudioPolicyService destructor calls AudioCommandThread::exit() which
1855 // acquires mLock.
1856 mLock.unlock();
1857 svc.clear();
1858 mLock.lock();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001859 } else {
1860 waitTime = mAudioCommands[0]->mTime - curTime;
1861 break;
1862 }
1863 }
Zach Janga754b4f2015-10-27 01:29:34 +00001864
1865 // release delayed commands wake lock if the queue is empty
1866 if (mAudioCommands.isEmpty()) {
Ricardo Garcia05f2fdc2014-07-24 15:48:24 -07001867 release_wake_lock(mName.string());
Zach Janga754b4f2015-10-27 01:29:34 +00001868 }
1869
1870 // At this stage we have either an empty command queue or the first command in the queue
1871 // has a finite delay. So unless we are exiting it is safe to wait.
1872 if (!exitPending()) {
Eric Laurent59a89232014-06-08 14:14:17 -07001873 ALOGV("AudioCommandThread() going to sleep");
Eric Laurentd7eda8d2016-02-02 17:18:39 -08001874 if (waitTime == -1) {
1875 mWaitWorkCV.wait(mLock);
1876 } else {
1877 mWaitWorkCV.waitRelative(mLock, waitTime);
1878 }
Eric Laurent59a89232014-06-08 14:14:17 -07001879 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001880 }
Ricardo Garcia05f2fdc2014-07-24 15:48:24 -07001881 // release delayed commands wake lock before quitting
1882 if (!mAudioCommands.isEmpty()) {
1883 release_wake_lock(mName.string());
1884 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001885 mLock.unlock();
1886 return false;
1887}
1888
1889status_t AudioPolicyService::AudioCommandThread::dump(int fd)
1890{
1891 const size_t SIZE = 256;
1892 char buffer[SIZE];
1893 String8 result;
1894
1895 snprintf(buffer, SIZE, "AudioCommandThread %p Dump\n", this);
1896 result.append(buffer);
1897 write(fd, result.string(), result.size());
1898
Mikhail Naganov12b716c2020-04-30 22:37:43 +00001899 const bool locked = dumpTryLock(mLock);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001900 if (!locked) {
1901 String8 result2(kCmdDeadlockedString);
1902 write(fd, result2.string(), result2.size());
1903 }
1904
1905 snprintf(buffer, SIZE, "- Commands:\n");
1906 result = String8(buffer);
1907 result.append(" Command Time Wait pParam\n");
Glenn Kasten8d6a2442012-02-08 14:04:28 -08001908 for (size_t i = 0; i < mAudioCommands.size(); i++) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07001909 mAudioCommands[i]->dump(buffer, SIZE);
1910 result.append(buffer);
1911 }
1912 result.append(" Last Command\n");
Eric Laurent0ede8922014-05-09 18:04:42 -07001913 if (mLastCommand != 0) {
1914 mLastCommand->dump(buffer, SIZE);
1915 result.append(buffer);
1916 } else {
1917 result.append(" none\n");
1918 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001919
1920 write(fd, result.string(), result.size());
1921
Mikhail Naganov12b716c2020-04-30 22:37:43 +00001922 dumpReleaseLock(mLock, locked);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001923
1924 return NO_ERROR;
1925}
1926
Glenn Kastenfff6d712012-01-12 16:38:12 -08001927status_t AudioPolicyService::AudioCommandThread::volumeCommand(audio_stream_type_t stream,
Eric Laurentde070132010-07-13 04:45:46 -07001928 float volume,
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001929 audio_io_handle_t output,
Eric Laurentde070132010-07-13 04:45:46 -07001930 int delayMs)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001931{
Eric Laurent0ede8922014-05-09 18:04:42 -07001932 sp<AudioCommand> command = new AudioCommand();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001933 command->mCommand = SET_VOLUME;
Eric Laurent0ede8922014-05-09 18:04:42 -07001934 sp<VolumeData> data = new VolumeData();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001935 data->mStream = stream;
1936 data->mVolume = volume;
1937 data->mIO = output;
1938 command->mParam = data;
Eric Laurent0ede8922014-05-09 18:04:42 -07001939 command->mWaitStatus = true;
Steve Block3856b092011-10-20 11:56:00 +01001940 ALOGV("AudioCommandThread() adding set volume stream %d, volume %f, output %d",
Eric Laurentde070132010-07-13 04:45:46 -07001941 stream, volume, output);
Eric Laurent0ede8922014-05-09 18:04:42 -07001942 return sendCommand(command, delayMs);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001943}
1944
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001945status_t AudioPolicyService::AudioCommandThread::parametersCommand(audio_io_handle_t ioHandle,
Dima Zavinfce7a472011-04-19 22:30:36 -07001946 const char *keyValuePairs,
Eric Laurentde070132010-07-13 04:45:46 -07001947 int delayMs)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001948{
Eric Laurent0ede8922014-05-09 18:04:42 -07001949 sp<AudioCommand> command = new AudioCommand();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001950 command->mCommand = SET_PARAMETERS;
Eric Laurent0ede8922014-05-09 18:04:42 -07001951 sp<ParametersData> data = new ParametersData();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001952 data->mIO = ioHandle;
Dima Zavinfce7a472011-04-19 22:30:36 -07001953 data->mKeyValuePairs = String8(keyValuePairs);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001954 command->mParam = data;
Eric Laurent0ede8922014-05-09 18:04:42 -07001955 command->mWaitStatus = true;
Steve Block3856b092011-10-20 11:56:00 +01001956 ALOGV("AudioCommandThread() adding set parameter string %s, io %d ,delay %d",
Dima Zavinfce7a472011-04-19 22:30:36 -07001957 keyValuePairs, ioHandle, delayMs);
Eric Laurent0ede8922014-05-09 18:04:42 -07001958 return sendCommand(command, delayMs);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001959}
1960
1961status_t AudioPolicyService::AudioCommandThread::voiceVolumeCommand(float volume, int delayMs)
1962{
Eric Laurent0ede8922014-05-09 18:04:42 -07001963 sp<AudioCommand> command = new AudioCommand();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001964 command->mCommand = SET_VOICE_VOLUME;
Eric Laurent0ede8922014-05-09 18:04:42 -07001965 sp<VoiceVolumeData> data = new VoiceVolumeData();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001966 data->mVolume = volume;
1967 command->mParam = data;
Eric Laurent0ede8922014-05-09 18:04:42 -07001968 command->mWaitStatus = true;
Steve Block3856b092011-10-20 11:56:00 +01001969 ALOGV("AudioCommandThread() adding set voice volume volume %f", volume);
Eric Laurent0ede8922014-05-09 18:04:42 -07001970 return sendCommand(command, delayMs);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001971}
1972
Eric Laurentb20cf7d2019-04-05 19:37:34 -07001973void AudioPolicyService::AudioCommandThread::setEffectSuspendedCommand(int effectId,
1974 audio_session_t sessionId,
1975 bool suspended)
1976{
1977 sp<AudioCommand> command = new AudioCommand();
1978 command->mCommand = SET_EFFECT_SUSPENDED;
1979 sp<SetEffectSuspendedData> data = new SetEffectSuspendedData();
1980 data->mEffectId = effectId;
1981 data->mSessionId = sessionId;
1982 data->mSuspended = suspended;
1983 command->mParam = data;
1984 ALOGV("AudioCommandThread() adding set suspended effectId %d sessionId %d suspended %d",
1985 effectId, sessionId, suspended);
1986 sendCommand(command);
1987}
1988
1989
Eric Laurentd7fe0862018-07-14 16:48:01 -07001990void AudioPolicyService::AudioCommandThread::stopOutputCommand(audio_port_handle_t portId)
Eric Laurentbfb1b832013-01-07 09:53:42 -08001991{
Eric Laurent0ede8922014-05-09 18:04:42 -07001992 sp<AudioCommand> command = new AudioCommand();
Eric Laurentbfb1b832013-01-07 09:53:42 -08001993 command->mCommand = STOP_OUTPUT;
Eric Laurent0ede8922014-05-09 18:04:42 -07001994 sp<StopOutputData> data = new StopOutputData();
Eric Laurentd7fe0862018-07-14 16:48:01 -07001995 data->mPortId = portId;
Jesper Tragardh48412dc2014-03-24 14:12:43 +01001996 command->mParam = data;
Eric Laurentd7fe0862018-07-14 16:48:01 -07001997 ALOGV("AudioCommandThread() adding stop output portId %d", portId);
Eric Laurent0ede8922014-05-09 18:04:42 -07001998 sendCommand(command);
Eric Laurentbfb1b832013-01-07 09:53:42 -08001999}
2000
Eric Laurentd7fe0862018-07-14 16:48:01 -07002001void AudioPolicyService::AudioCommandThread::releaseOutputCommand(audio_port_handle_t portId)
Eric Laurentbfb1b832013-01-07 09:53:42 -08002002{
Eric Laurent0ede8922014-05-09 18:04:42 -07002003 sp<AudioCommand> command = new AudioCommand();
Eric Laurentbfb1b832013-01-07 09:53:42 -08002004 command->mCommand = RELEASE_OUTPUT;
Eric Laurent0ede8922014-05-09 18:04:42 -07002005 sp<ReleaseOutputData> data = new ReleaseOutputData();
Eric Laurentd7fe0862018-07-14 16:48:01 -07002006 data->mPortId = portId;
Jesper Tragardh48412dc2014-03-24 14:12:43 +01002007 command->mParam = data;
Eric Laurentd7fe0862018-07-14 16:48:01 -07002008 ALOGV("AudioCommandThread() adding release output portId %d", portId);
Eric Laurent0ede8922014-05-09 18:04:42 -07002009 sendCommand(command);
2010}
2011
Eric Laurent951f4552014-05-20 10:48:17 -07002012status_t AudioPolicyService::AudioCommandThread::createAudioPatchCommand(
2013 const struct audio_patch *patch,
2014 audio_patch_handle_t *handle,
2015 int delayMs)
2016{
2017 status_t status = NO_ERROR;
2018
2019 sp<AudioCommand> command = new AudioCommand();
2020 command->mCommand = CREATE_AUDIO_PATCH;
2021 CreateAudioPatchData *data = new CreateAudioPatchData();
2022 data->mPatch = *patch;
2023 data->mHandle = *handle;
2024 command->mParam = data;
2025 command->mWaitStatus = true;
2026 ALOGV("AudioCommandThread() adding create patch delay %d", delayMs);
2027 status = sendCommand(command, delayMs);
2028 if (status == NO_ERROR) {
2029 *handle = data->mHandle;
2030 }
2031 return status;
2032}
2033
2034status_t AudioPolicyService::AudioCommandThread::releaseAudioPatchCommand(audio_patch_handle_t handle,
2035 int delayMs)
2036{
2037 sp<AudioCommand> command = new AudioCommand();
2038 command->mCommand = RELEASE_AUDIO_PATCH;
2039 ReleaseAudioPatchData *data = new ReleaseAudioPatchData();
2040 data->mHandle = handle;
2041 command->mParam = data;
2042 command->mWaitStatus = true;
2043 ALOGV("AudioCommandThread() adding release patch delay %d", delayMs);
2044 return sendCommand(command, delayMs);
2045}
2046
Eric Laurentb52c1522014-05-20 11:27:36 -07002047void AudioPolicyService::AudioCommandThread::updateAudioPortListCommand()
2048{
2049 sp<AudioCommand> command = new AudioCommand();
2050 command->mCommand = UPDATE_AUDIOPORT_LIST;
2051 ALOGV("AudioCommandThread() adding update audio port list");
2052 sendCommand(command);
2053}
2054
Eric Laurented726cc2021-07-01 14:26:41 +02002055void AudioPolicyService::AudioCommandThread::updateUidStatesCommand()
2056{
2057 sp<AudioCommand> command = new AudioCommand();
2058 command->mCommand = UPDATE_UID_STATES;
2059 ALOGV("AudioCommandThread() adding update UID states");
2060 sendCommand(command);
2061}
2062
Eric Laurentb52c1522014-05-20 11:27:36 -07002063void AudioPolicyService::AudioCommandThread::updateAudioPatchListCommand()
2064{
2065 sp<AudioCommand>command = new AudioCommand();
2066 command->mCommand = UPDATE_AUDIOPATCH_LIST;
2067 ALOGV("AudioCommandThread() adding update audio patch list");
2068 sendCommand(command);
2069}
2070
François Gaffiecfe17322018-11-07 13:41:29 +01002071void AudioPolicyService::AudioCommandThread::changeAudioVolumeGroupCommand(volume_group_t group,
2072 int flags)
2073{
2074 sp<AudioCommand>command = new AudioCommand();
2075 command->mCommand = CHANGED_AUDIOVOLUMEGROUP;
2076 AudioVolumeGroupData *data= new AudioVolumeGroupData();
2077 data->mGroup = group;
2078 data->mFlags = flags;
2079 command->mParam = data;
2080 ALOGV("AudioCommandThread() adding audio volume group changed");
2081 sendCommand(command);
2082}
2083
Eric Laurente1715a42014-05-20 11:30:42 -07002084status_t AudioPolicyService::AudioCommandThread::setAudioPortConfigCommand(
2085 const struct audio_port_config *config, int delayMs)
2086{
2087 sp<AudioCommand> command = new AudioCommand();
2088 command->mCommand = SET_AUDIOPORT_CONFIG;
2089 SetAudioPortConfigData *data = new SetAudioPortConfigData();
2090 data->mConfig = *config;
2091 command->mParam = data;
2092 command->mWaitStatus = true;
2093 ALOGV("AudioCommandThread() adding set port config delay %d", delayMs);
2094 return sendCommand(command, delayMs);
2095}
2096
Jean-Michel Trivide801052015-04-14 19:10:14 -07002097void AudioPolicyService::AudioCommandThread::dynamicPolicyMixStateUpdateCommand(
Chih-Hung Hsiehe964d4e2016-08-09 14:31:32 -07002098 const String8& regId, int32_t state)
Jean-Michel Trivide801052015-04-14 19:10:14 -07002099{
2100 sp<AudioCommand> command = new AudioCommand();
2101 command->mCommand = DYN_POLICY_MIX_STATE_UPDATE;
2102 DynPolicyMixStateUpdateData *data = new DynPolicyMixStateUpdateData();
2103 data->mRegId = regId;
2104 data->mState = state;
2105 command->mParam = data;
2106 ALOGV("AudioCommandThread() sending dynamic policy mix (id=%s) state update to %d",
2107 regId.string(), state);
2108 sendCommand(command);
2109}
2110
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -08002111void AudioPolicyService::AudioCommandThread::recordingConfigurationUpdateCommand(
Eric Laurenta9f86652018-11-28 17:23:11 -08002112 int event,
2113 const record_client_info_t *clientInfo,
2114 const audio_config_base_t *clientConfig,
2115 std::vector<effect_descriptor_t> clientEffects,
2116 const audio_config_base_t *deviceConfig,
2117 std::vector<effect_descriptor_t> effects,
2118 audio_patch_handle_t patchHandle,
2119 audio_source_t source)
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -08002120{
2121 sp<AudioCommand>command = new AudioCommand();
2122 command->mCommand = RECORDING_CONFIGURATION_UPDATE;
2123 RecordingConfigurationUpdateData *data = new RecordingConfigurationUpdateData();
2124 data->mEvent = event;
Jean-Michel Triviac4e4292016-12-22 11:39:31 -08002125 data->mClientInfo = *clientInfo;
Jean-Michel Trivi7281aa92016-02-17 15:33:40 -08002126 data->mClientConfig = *clientConfig;
Eric Laurenta9f86652018-11-28 17:23:11 -08002127 data->mClientEffects = clientEffects;
Jean-Michel Trivi7281aa92016-02-17 15:33:40 -08002128 data->mDeviceConfig = *deviceConfig;
Eric Laurenta9f86652018-11-28 17:23:11 -08002129 data->mEffects = effects;
Jean-Michel Trivi8c7cf3b2016-02-25 17:08:24 -08002130 data->mPatchHandle = patchHandle;
Eric Laurenta9f86652018-11-28 17:23:11 -08002131 data->mSource = source;
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -08002132 command->mParam = data;
Jean-Michel Triviac4e4292016-12-22 11:39:31 -08002133 ALOGV("AudioCommandThread() adding recording configuration update event %d, source %d uid %u",
2134 event, clientInfo->source, clientInfo->uid);
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -08002135 sendCommand(command);
2136}
2137
Mikhail Naganov88b30d22020-03-09 19:43:13 +00002138void AudioPolicyService::AudioCommandThread::audioModulesUpdateCommand()
2139{
2140 sp<AudioCommand> command = new AudioCommand();
2141 command->mCommand = AUDIO_MODULES_UPDATE;
2142 sendCommand(command);
2143}
2144
Jean-Michel Trivi9a6b9ad2020-10-22 16:46:43 -07002145void AudioPolicyService::AudioCommandThread::routingChangedCommand()
2146{
2147 sp<AudioCommand>command = new AudioCommand();
2148 command->mCommand = ROUTING_UPDATED;
2149 ALOGV("AudioCommandThread() adding routing update");
2150 sendCommand(command);
2151}
2152
Eric Laurent6d607012021-07-05 11:54:40 +02002153void AudioPolicyService::AudioCommandThread::checkSpatializerCommand()
2154{
2155 sp<AudioCommand>command = new AudioCommand();
2156 command->mCommand = CHECK_SPATIALIZER;
2157 ALOGV("AudioCommandThread() adding check spatializer");
2158 sendCommand(command);
2159}
2160
Eric Laurent0ede8922014-05-09 18:04:42 -07002161status_t AudioPolicyService::AudioCommandThread::sendCommand(sp<AudioCommand>& command, int delayMs)
2162{
2163 {
2164 Mutex::Autolock _l(mLock);
2165 insertCommand_l(command, delayMs);
2166 mWaitWorkCV.signal();
2167 }
2168 Mutex::Autolock _l(command->mLock);
2169 while (command->mWaitStatus) {
2170 nsecs_t timeOutNs = kAudioCommandTimeoutNs + milliseconds(delayMs);
2171 if (command->mCond.waitRelative(command->mLock, timeOutNs) != NO_ERROR) {
2172 command->mStatus = TIMED_OUT;
2173 command->mWaitStatus = false;
2174 }
2175 }
2176 return command->mStatus;
Eric Laurentbfb1b832013-01-07 09:53:42 -08002177}
2178
Mathias Agopian65ab4712010-07-14 17:59:35 -07002179// insertCommand_l() must be called with mLock held
Eric Laurent0ede8922014-05-09 18:04:42 -07002180void AudioPolicyService::AudioCommandThread::insertCommand_l(sp<AudioCommand>& command, int delayMs)
Mathias Agopian65ab4712010-07-14 17:59:35 -07002181{
Glenn Kasten8d6a2442012-02-08 14:04:28 -08002182 ssize_t i; // not size_t because i will count down to -1
Eric Laurent0ede8922014-05-09 18:04:42 -07002183 Vector < sp<AudioCommand> > removedCommands;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002184 command->mTime = systemTime() + milliseconds(delayMs);
2185
2186 // acquire wake lock to make sure delayed commands are processed
Eric Laurentbfb1b832013-01-07 09:53:42 -08002187 if (mAudioCommands.isEmpty()) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07002188 acquire_wake_lock(PARTIAL_WAKE_LOCK, mName.string());
2189 }
2190
2191 // check same pending commands with later time stamps and eliminate them
Ivan Lozano5ff158f2017-10-30 09:06:24 -07002192 for (i = (ssize_t)mAudioCommands.size()-1; i >= 0; i--) {
Eric Laurent0ede8922014-05-09 18:04:42 -07002193 sp<AudioCommand> command2 = mAudioCommands[i];
Mathias Agopian65ab4712010-07-14 17:59:35 -07002194 // commands are sorted by increasing time stamp: no need to scan the rest of mAudioCommands
2195 if (command2->mTime <= command->mTime) break;
Eric Laurente45b48a2014-09-04 16:40:57 -07002196
2197 // create audio patch or release audio patch commands are equivalent
2198 // with regard to filtering
2199 if ((command->mCommand == CREATE_AUDIO_PATCH) ||
2200 (command->mCommand == RELEASE_AUDIO_PATCH)) {
2201 if ((command2->mCommand != CREATE_AUDIO_PATCH) &&
2202 (command2->mCommand != RELEASE_AUDIO_PATCH)) {
2203 continue;
2204 }
2205 } else if (command2->mCommand != command->mCommand) continue;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002206
2207 switch (command->mCommand) {
2208 case SET_PARAMETERS: {
Eric Laurent0ede8922014-05-09 18:04:42 -07002209 ParametersData *data = (ParametersData *)command->mParam.get();
2210 ParametersData *data2 = (ParametersData *)command2->mParam.get();
Mathias Agopian65ab4712010-07-14 17:59:35 -07002211 if (data->mIO != data2->mIO) break;
Steve Block3856b092011-10-20 11:56:00 +01002212 ALOGV("Comparing parameter command %s to new command %s",
Eric Laurentde070132010-07-13 04:45:46 -07002213 data2->mKeyValuePairs.string(), data->mKeyValuePairs.string());
Mathias Agopian65ab4712010-07-14 17:59:35 -07002214 AudioParameter param = AudioParameter(data->mKeyValuePairs);
2215 AudioParameter param2 = AudioParameter(data2->mKeyValuePairs);
2216 for (size_t j = 0; j < param.size(); j++) {
Glenn Kastene53b9ea2012-03-12 16:29:55 -07002217 String8 key;
2218 String8 value;
2219 param.getAt(j, key, value);
2220 for (size_t k = 0; k < param2.size(); k++) {
2221 String8 key2;
2222 String8 value2;
2223 param2.getAt(k, key2, value2);
2224 if (key2 == key) {
2225 param2.remove(key2);
2226 ALOGV("Filtering out parameter %s", key2.string());
2227 break;
2228 }
2229 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07002230 }
2231 // if all keys have been filtered out, remove the command.
2232 // otherwise, update the key value pairs
2233 if (param2.size() == 0) {
2234 removedCommands.add(command2);
2235 } else {
2236 data2->mKeyValuePairs = param2.toString();
2237 }
Eric Laurent21e54562013-09-23 12:08:05 -07002238 command->mTime = command2->mTime;
2239 // force delayMs to non 0 so that code below does not request to wait for
2240 // command status as the command is now delayed
2241 delayMs = 1;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002242 } break;
2243
2244 case SET_VOLUME: {
Eric Laurent0ede8922014-05-09 18:04:42 -07002245 VolumeData *data = (VolumeData *)command->mParam.get();
2246 VolumeData *data2 = (VolumeData *)command2->mParam.get();
Mathias Agopian65ab4712010-07-14 17:59:35 -07002247 if (data->mIO != data2->mIO) break;
2248 if (data->mStream != data2->mStream) break;
Steve Block3856b092011-10-20 11:56:00 +01002249 ALOGV("Filtering out volume command on output %d for stream %d",
Eric Laurentde070132010-07-13 04:45:46 -07002250 data->mIO, data->mStream);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002251 removedCommands.add(command2);
Eric Laurent21e54562013-09-23 12:08:05 -07002252 command->mTime = command2->mTime;
2253 // force delayMs to non 0 so that code below does not request to wait for
2254 // command status as the command is now delayed
2255 delayMs = 1;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002256 } break;
Eric Laurente45b48a2014-09-04 16:40:57 -07002257
Eric Laurentbaf35fe2016-07-27 15:36:53 -07002258 case SET_VOICE_VOLUME: {
2259 VoiceVolumeData *data = (VoiceVolumeData *)command->mParam.get();
2260 VoiceVolumeData *data2 = (VoiceVolumeData *)command2->mParam.get();
2261 ALOGV("Filtering out voice volume command value %f replaced by %f",
2262 data2->mVolume, data->mVolume);
2263 removedCommands.add(command2);
2264 command->mTime = command2->mTime;
2265 // force delayMs to non 0 so that code below does not request to wait for
2266 // command status as the command is now delayed
2267 delayMs = 1;
2268 } break;
2269
Eric Laurente45b48a2014-09-04 16:40:57 -07002270 case CREATE_AUDIO_PATCH:
2271 case RELEASE_AUDIO_PATCH: {
2272 audio_patch_handle_t handle;
Haynes Mathew Georgea2d4a6d2014-10-13 13:05:22 -07002273 struct audio_patch patch;
Eric Laurente45b48a2014-09-04 16:40:57 -07002274 if (command->mCommand == CREATE_AUDIO_PATCH) {
2275 handle = ((CreateAudioPatchData *)command->mParam.get())->mHandle;
Haynes Mathew Georgea2d4a6d2014-10-13 13:05:22 -07002276 patch = ((CreateAudioPatchData *)command->mParam.get())->mPatch;
Eric Laurente45b48a2014-09-04 16:40:57 -07002277 } else {
2278 handle = ((ReleaseAudioPatchData *)command->mParam.get())->mHandle;
Mikhail Naganov7be71d22018-05-23 16:51:46 -07002279 memset(&patch, 0, sizeof(patch));
Eric Laurente45b48a2014-09-04 16:40:57 -07002280 }
2281 audio_patch_handle_t handle2;
Haynes Mathew Georgea2d4a6d2014-10-13 13:05:22 -07002282 struct audio_patch patch2;
Eric Laurente45b48a2014-09-04 16:40:57 -07002283 if (command2->mCommand == CREATE_AUDIO_PATCH) {
2284 handle2 = ((CreateAudioPatchData *)command2->mParam.get())->mHandle;
Haynes Mathew Georgea2d4a6d2014-10-13 13:05:22 -07002285 patch2 = ((CreateAudioPatchData *)command2->mParam.get())->mPatch;
Eric Laurente45b48a2014-09-04 16:40:57 -07002286 } else {
2287 handle2 = ((ReleaseAudioPatchData *)command2->mParam.get())->mHandle;
Glenn Kastenf60b6b62015-07-06 10:53:26 -07002288 memset(&patch2, 0, sizeof(patch2));
Eric Laurente45b48a2014-09-04 16:40:57 -07002289 }
2290 if (handle != handle2) break;
Haynes Mathew Georgea2d4a6d2014-10-13 13:05:22 -07002291 /* Filter CREATE_AUDIO_PATCH commands only when they are issued for
2292 same output. */
2293 if( (command->mCommand == CREATE_AUDIO_PATCH) &&
2294 (command2->mCommand == CREATE_AUDIO_PATCH) ) {
2295 bool isOutputDiff = false;
2296 if (patch.num_sources == patch2.num_sources) {
2297 for (unsigned count = 0; count < patch.num_sources; count++) {
2298 if (patch.sources[count].id != patch2.sources[count].id) {
2299 isOutputDiff = true;
2300 break;
2301 }
2302 }
2303 if (isOutputDiff)
2304 break;
2305 }
2306 }
Eric Laurente45b48a2014-09-04 16:40:57 -07002307 ALOGV("Filtering out %s audio patch command for handle %d",
2308 (command->mCommand == CREATE_AUDIO_PATCH) ? "create" : "release", handle);
2309 removedCommands.add(command2);
2310 command->mTime = command2->mTime;
2311 // force delayMs to non 0 so that code below does not request to wait for
2312 // command status as the command is now delayed
2313 delayMs = 1;
2314 } break;
2315
Jean-Michel Trivide801052015-04-14 19:10:14 -07002316 case DYN_POLICY_MIX_STATE_UPDATE: {
2317
2318 } break;
2319
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -08002320 case RECORDING_CONFIGURATION_UPDATE: {
2321
2322 } break;
2323
Jean-Michel Trivi9a6b9ad2020-10-22 16:46:43 -07002324 case ROUTING_UPDATED: {
2325
2326 } break;
2327
Mathias Agopian65ab4712010-07-14 17:59:35 -07002328 default:
2329 break;
2330 }
2331 }
2332
2333 // remove filtered commands
2334 for (size_t j = 0; j < removedCommands.size(); j++) {
2335 // removed commands always have time stamps greater than current command
2336 for (size_t k = i + 1; k < mAudioCommands.size(); k++) {
Eric Laurent0ede8922014-05-09 18:04:42 -07002337 if (mAudioCommands[k].get() == removedCommands[j].get()) {
Steve Block3856b092011-10-20 11:56:00 +01002338 ALOGV("suppressing command: %d", mAudioCommands[k]->mCommand);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002339 mAudioCommands.removeAt(k);
2340 break;
2341 }
2342 }
2343 }
2344 removedCommands.clear();
2345
Eric Laurentaa79bef2015-01-15 14:33:51 -08002346 // Disable wait for status if delay is not 0.
2347 // Except for create audio patch command because the returned patch handle
2348 // is needed by audio policy manager
2349 if (delayMs != 0 && command->mCommand != CREATE_AUDIO_PATCH) {
Eric Laurentcec4abb2012-07-03 12:23:02 -07002350 command->mWaitStatus = false;
2351 }
Eric Laurentcec4abb2012-07-03 12:23:02 -07002352
Mathias Agopian65ab4712010-07-14 17:59:35 -07002353 // insert command at the right place according to its time stamp
Eric Laurent1e693b52014-07-09 15:03:28 -07002354 ALOGV("inserting command: %d at index %zd, num commands %zu",
2355 command->mCommand, i+1, mAudioCommands.size());
Mathias Agopian65ab4712010-07-14 17:59:35 -07002356 mAudioCommands.insertAt(command, i + 1);
2357}
2358
2359void AudioPolicyService::AudioCommandThread::exit()
2360{
Steve Block3856b092011-10-20 11:56:00 +01002361 ALOGV("AudioCommandThread::exit");
Mathias Agopian65ab4712010-07-14 17:59:35 -07002362 {
2363 AutoMutex _l(mLock);
2364 requestExit();
2365 mWaitWorkCV.signal();
2366 }
Zach Janga754b4f2015-10-27 01:29:34 +00002367 // Note that we can call it from the thread loop if all other references have been released
2368 // but it will safely return WOULD_BLOCK in this case
Mathias Agopian65ab4712010-07-14 17:59:35 -07002369 requestExitAndWait();
2370}
2371
2372void AudioPolicyService::AudioCommandThread::AudioCommand::dump(char* buffer, size_t size)
2373{
2374 snprintf(buffer, size, " %02d %06d.%03d %01u %p\n",
2375 mCommand,
2376 (int)ns2s(mTime),
2377 (int)ns2ms(mTime)%1000,
2378 mWaitStatus,
Eric Laurent0ede8922014-05-09 18:04:42 -07002379 mParam.get());
Mathias Agopian65ab4712010-07-14 17:59:35 -07002380}
2381
Dima Zavinfce7a472011-04-19 22:30:36 -07002382/******* helpers for the service_ops callbacks defined below *********/
2383void AudioPolicyService::setParameters(audio_io_handle_t ioHandle,
2384 const char *keyValuePairs,
2385 int delayMs)
2386{
Glenn Kasten72ef00d2012-01-17 11:09:42 -08002387 mAudioCommandThread->parametersCommand(ioHandle, keyValuePairs,
Dima Zavinfce7a472011-04-19 22:30:36 -07002388 delayMs);
2389}
2390
2391int AudioPolicyService::setStreamVolume(audio_stream_type_t stream,
2392 float volume,
2393 audio_io_handle_t output,
2394 int delayMs)
2395{
Glenn Kastenfff6d712012-01-12 16:38:12 -08002396 return (int)mAudioCommandThread->volumeCommand(stream, volume,
Glenn Kasten72ef00d2012-01-17 11:09:42 -08002397 output, delayMs);
Dima Zavinfce7a472011-04-19 22:30:36 -07002398}
2399
Dima Zavinfce7a472011-04-19 22:30:36 -07002400int AudioPolicyService::setVoiceVolume(float volume, int delayMs)
2401{
2402 return (int)mAudioCommandThread->voiceVolumeCommand(volume, delayMs);
2403}
2404
Eric Laurentb20cf7d2019-04-05 19:37:34 -07002405void AudioPolicyService::setEffectSuspended(int effectId,
2406 audio_session_t sessionId,
2407 bool suspended)
2408{
2409 mAudioCommandThread->setEffectSuspendedCommand(effectId, sessionId, suspended);
2410}
2411
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002412Status AudioPolicyService::onNewAudioModulesAvailable()
Mikhail Naganov88b30d22020-03-09 19:43:13 +00002413{
Mikhail Naganovd0e2c742020-03-25 15:59:59 -07002414 mOutputCommandThread->audioModulesUpdateCommand();
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002415 return Status::ok();
Mikhail Naganov88b30d22020-03-09 19:43:13 +00002416}
2417
Eric Laurentb20cf7d2019-04-05 19:37:34 -07002418
Dima Zavinfce7a472011-04-19 22:30:36 -07002419extern "C" {
Eric Laurent2d388ec2014-03-07 13:25:54 -08002420audio_module_handle_t aps_load_hw_module(void *service __unused,
2421 const char *name);
2422audio_io_handle_t aps_open_output(void *service __unused,
Eric Laurenta4c5a552012-03-29 10:12:40 -07002423 audio_devices_t *pDevices,
2424 uint32_t *pSamplingRate,
2425 audio_format_t *pFormat,
2426 audio_channel_mask_t *pChannelMask,
2427 uint32_t *pLatencyMs,
Eric Laurent2d388ec2014-03-07 13:25:54 -08002428 audio_output_flags_t flags);
Eric Laurenta4c5a552012-03-29 10:12:40 -07002429
Eric Laurent2d388ec2014-03-07 13:25:54 -08002430audio_io_handle_t aps_open_output_on_module(void *service __unused,
Eric Laurenta4c5a552012-03-29 10:12:40 -07002431 audio_module_handle_t module,
2432 audio_devices_t *pDevices,
2433 uint32_t *pSamplingRate,
2434 audio_format_t *pFormat,
2435 audio_channel_mask_t *pChannelMask,
2436 uint32_t *pLatencyMs,
Richard Fitzgeraldad3af332013-03-25 16:54:37 +00002437 audio_output_flags_t flags,
Eric Laurent2d388ec2014-03-07 13:25:54 -08002438 const audio_offload_info_t *offloadInfo);
2439audio_io_handle_t aps_open_dup_output(void *service __unused,
Dima Zavinfce7a472011-04-19 22:30:36 -07002440 audio_io_handle_t output1,
Eric Laurent2d388ec2014-03-07 13:25:54 -08002441 audio_io_handle_t output2);
2442int aps_close_output(void *service __unused, audio_io_handle_t output);
2443int aps_suspend_output(void *service __unused, audio_io_handle_t output);
2444int aps_restore_output(void *service __unused, audio_io_handle_t output);
2445audio_io_handle_t aps_open_input(void *service __unused,
Eric Laurenta4c5a552012-03-29 10:12:40 -07002446 audio_devices_t *pDevices,
2447 uint32_t *pSamplingRate,
2448 audio_format_t *pFormat,
2449 audio_channel_mask_t *pChannelMask,
Eric Laurent2d388ec2014-03-07 13:25:54 -08002450 audio_in_acoustics_t acoustics __unused);
2451audio_io_handle_t aps_open_input_on_module(void *service __unused,
Eric Laurenta4c5a552012-03-29 10:12:40 -07002452 audio_module_handle_t module,
2453 audio_devices_t *pDevices,
2454 uint32_t *pSamplingRate,
2455 audio_format_t *pFormat,
Eric Laurent2d388ec2014-03-07 13:25:54 -08002456 audio_channel_mask_t *pChannelMask);
2457int aps_close_input(void *service __unused, audio_io_handle_t input);
2458int aps_invalidate_stream(void *service __unused, audio_stream_type_t stream);
Glenn Kastend848eb42016-03-08 13:42:11 -08002459int aps_move_effects(void *service __unused, audio_session_t session,
Dima Zavinfce7a472011-04-19 22:30:36 -07002460 audio_io_handle_t src_output,
Eric Laurent2d388ec2014-03-07 13:25:54 -08002461 audio_io_handle_t dst_output);
2462char * aps_get_parameters(void *service __unused, audio_io_handle_t io_handle,
2463 const char *keys);
2464void aps_set_parameters(void *service, audio_io_handle_t io_handle,
2465 const char *kv_pairs, int delay_ms);
2466int aps_set_stream_volume(void *service, audio_stream_type_t stream,
Dima Zavinfce7a472011-04-19 22:30:36 -07002467 float volume, audio_io_handle_t output,
Eric Laurent2d388ec2014-03-07 13:25:54 -08002468 int delay_ms);
Eric Laurent2d388ec2014-03-07 13:25:54 -08002469int aps_set_voice_volume(void *service, float volume, int delay_ms);
2470};
Dima Zavinfce7a472011-04-19 22:30:36 -07002471
Mikhail Naganov1b2a7942017-12-08 10:18:09 -08002472} // namespace android