blob: 56c472bde0cb9844f928bbc6dbab2551965b5e86 [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);
369 mOutputCommandThread->checkSpatializerCommand();
370}
371
372void AudioPolicyService::doOnCheckSpatializer()
373{
374 sp<Spatializer> spatializer;
375 {
376 Mutex::Autolock _l(mLock);
377 spatializer = mSpatializer;
378
379 if (spatializer != nullptr) {
380 audio_io_handle_t output = AUDIO_IO_HANDLE_NONE;
381 if (spatializer->getLevel() != media::SpatializationLevel::NONE
382 && spatializer->getOutput() == AUDIO_IO_HANDLE_NONE) {
383 const audio_attributes_t attr = attributes_initializer(AUDIO_USAGE_MEDIA);
384 audio_config_base_t config = spatializer->getAudioInConfig();
385 status_t status =
386 mAudioPolicyManager->getSpatializerOutput(&config, &attr, &output);
387 if (status != NO_ERROR || output == AUDIO_IO_HANDLE_NONE) {
388 return;
389 }
390 mLock.unlock();
391 status = spatializer->attachOutput(output);
392 mLock.lock();
393 if (status != NO_ERROR) {
394 mAudioPolicyManager->releaseSpatializerOutput(output);
395 }
396 } else if (spatializer->getLevel() == media::SpatializationLevel::NONE
397 && spatializer->getOutput() != AUDIO_IO_HANDLE_NONE) {
398 mLock.unlock();
399 output = spatializer->detachOutput();
400 mLock.lock();
401 if (output != AUDIO_IO_HANDLE_NONE) {
402 mAudioPolicyManager->releaseSpatializerOutput(output);
403 }
404 }
405 }
406 }
407}
408
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -0800409status_t AudioPolicyService::clientCreateAudioPatch(const struct audio_patch *patch,
410 audio_patch_handle_t *handle,
411 int delayMs)
412{
413 return mAudioCommandThread->createAudioPatchCommand(patch, handle, delayMs);
414}
415
416status_t AudioPolicyService::clientReleaseAudioPatch(audio_patch_handle_t handle,
417 int delayMs)
418{
419 return mAudioCommandThread->releaseAudioPatchCommand(handle, delayMs);
420}
421
Eric Laurente1715a42014-05-20 11:30:42 -0700422status_t AudioPolicyService::clientSetAudioPortConfig(const struct audio_port_config *config,
423 int delayMs)
424{
425 return mAudioCommandThread->setAudioPortConfigCommand(config, delayMs);
426}
427
Ytai Ben-Tsvi7e7a79d2020-12-15 16:48:16 -0800428AudioPolicyService::NotificationClient::NotificationClient(
429 const sp<AudioPolicyService>& service,
430 const sp<media::IAudioPolicyServiceClient>& client,
431 uid_t uid,
432 pid_t pid)
luochaojiang908c7d72018-06-21 14:58:04 +0800433 : mService(service), mUid(uid), mPid(pid), mAudioPolicyServiceClient(client),
François Gaffiecfe17322018-11-07 13:41:29 +0100434 mAudioPortCallbacksEnabled(false), mAudioVolumeGroupCallbacksEnabled(false)
Eric Laurentb52c1522014-05-20 11:27:36 -0700435{
436}
437
438AudioPolicyService::NotificationClient::~NotificationClient()
439{
440}
441
442void AudioPolicyService::NotificationClient::binderDied(const wp<IBinder>& who __unused)
443{
444 sp<NotificationClient> keep(this);
445 sp<AudioPolicyService> service = mService.promote();
446 if (service != 0) {
luochaojiang908c7d72018-06-21 14:58:04 +0800447 service->removeNotificationClient(mUid, mPid);
Eric Laurentb52c1522014-05-20 11:27:36 -0700448 }
449}
450
451void AudioPolicyService::NotificationClient::onAudioPortListUpdate()
452{
Eric Laurente8726fe2015-06-26 09:39:24 -0700453 if (mAudioPolicyServiceClient != 0 && mAudioPortCallbacksEnabled) {
Eric Laurentb52c1522014-05-20 11:27:36 -0700454 mAudioPolicyServiceClient->onAudioPortListUpdate();
455 }
456}
457
458void AudioPolicyService::NotificationClient::onAudioPatchListUpdate()
459{
Eric Laurente8726fe2015-06-26 09:39:24 -0700460 if (mAudioPolicyServiceClient != 0 && mAudioPortCallbacksEnabled) {
Eric Laurentb52c1522014-05-20 11:27:36 -0700461 mAudioPolicyServiceClient->onAudioPatchListUpdate();
462 }
463}
Eric Laurent57dae992011-07-24 13:36:09 -0700464
François Gaffiecfe17322018-11-07 13:41:29 +0100465void AudioPolicyService::NotificationClient::onAudioVolumeGroupChanged(volume_group_t group,
466 int flags)
467{
468 if (mAudioPolicyServiceClient != 0 && mAudioVolumeGroupCallbacksEnabled) {
469 mAudioPolicyServiceClient->onAudioVolumeGroupChanged(group, flags);
470 }
471}
472
473
Jean-Michel Trivide801052015-04-14 19:10:14 -0700474void AudioPolicyService::NotificationClient::onDynamicPolicyMixStateUpdate(
Chih-Hung Hsiehe964d4e2016-08-09 14:31:32 -0700475 const String8& regId, int32_t state)
Jean-Michel Trivide801052015-04-14 19:10:14 -0700476{
Andy Hung4ef19fa2018-05-15 19:35:29 -0700477 if (mAudioPolicyServiceClient != 0 && isServiceUid(mUid)) {
Ytai Ben-Tsvi7e7a79d2020-12-15 16:48:16 -0800478 mAudioPolicyServiceClient->onDynamicPolicyMixStateUpdate(
479 legacy2aidl_String8_string(regId).value(), state);
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -0800480 }
481}
482
483void AudioPolicyService::NotificationClient::onRecordingConfigurationUpdate(
Eric Laurenta9f86652018-11-28 17:23:11 -0800484 int event,
485 const record_client_info_t *clientInfo,
486 const audio_config_base_t *clientConfig,
487 std::vector<effect_descriptor_t> clientEffects,
488 const audio_config_base_t *deviceConfig,
489 std::vector<effect_descriptor_t> effects,
490 audio_patch_handle_t patchHandle,
491 audio_source_t source)
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -0800492{
Andy Hung4ef19fa2018-05-15 19:35:29 -0700493 if (mAudioPolicyServiceClient != 0 && isServiceUid(mUid)) {
Ytai Ben-Tsvi7e7a79d2020-12-15 16:48:16 -0800494 status_t status = [&]() -> status_t {
495 int32_t eventAidl = VALUE_OR_RETURN_STATUS(convertIntegral<int32_t>(event));
496 media::RecordClientInfo clientInfoAidl = VALUE_OR_RETURN_STATUS(
497 legacy2aidl_record_client_info_t_RecordClientInfo(*clientInfo));
498 media::AudioConfigBase clientConfigAidl = VALUE_OR_RETURN_STATUS(
499 legacy2aidl_audio_config_base_t_AudioConfigBase(*clientConfig));
500 std::vector<media::EffectDescriptor> clientEffectsAidl = VALUE_OR_RETURN_STATUS(
501 convertContainer<std::vector<media::EffectDescriptor>>(
502 clientEffects,
503 legacy2aidl_effect_descriptor_t_EffectDescriptor));
504 media::AudioConfigBase deviceConfigAidl = VALUE_OR_RETURN_STATUS(
505 legacy2aidl_audio_config_base_t_AudioConfigBase(*deviceConfig));
506 std::vector<media::EffectDescriptor> effectsAidl = VALUE_OR_RETURN_STATUS(
507 convertContainer<std::vector<media::EffectDescriptor>>(
508 effects,
509 legacy2aidl_effect_descriptor_t_EffectDescriptor));
510 int32_t patchHandleAidl = VALUE_OR_RETURN_STATUS(
511 legacy2aidl_audio_patch_handle_t_int32_t(patchHandle));
512 media::AudioSourceType sourceAidl = VALUE_OR_RETURN_STATUS(
513 legacy2aidl_audio_source_t_AudioSourceType(source));
514 return aidl_utils::statusTFromBinderStatus(
515 mAudioPolicyServiceClient->onRecordingConfigurationUpdate(eventAidl,
516 clientInfoAidl,
517 clientConfigAidl,
518 clientEffectsAidl,
519 deviceConfigAidl,
520 effectsAidl,
521 patchHandleAidl,
522 sourceAidl));
523 }();
524 ALOGW_IF(status != OK, "onRecordingConfigurationUpdate() failed: %d", status);
Jean-Michel Trivide801052015-04-14 19:10:14 -0700525 }
526}
527
Eric Laurente8726fe2015-06-26 09:39:24 -0700528void AudioPolicyService::NotificationClient::setAudioPortCallbacksEnabled(bool enabled)
529{
530 mAudioPortCallbacksEnabled = enabled;
531}
532
François Gaffiecfe17322018-11-07 13:41:29 +0100533void AudioPolicyService::NotificationClient::setAudioVolumeGroupCallbacksEnabled(bool enabled)
534{
535 mAudioVolumeGroupCallbacksEnabled = enabled;
536}
Eric Laurente8726fe2015-06-26 09:39:24 -0700537
Jean-Michel Trivi9a6b9ad2020-10-22 16:46:43 -0700538void AudioPolicyService::NotificationClient::onRoutingUpdated()
539{
540 if (mAudioPolicyServiceClient != 0 && isServiceUid(mUid)) {
541 mAudioPolicyServiceClient->onRoutingUpdated();
542 }
543}
544
Mathias Agopian65ab4712010-07-14 17:59:35 -0700545void AudioPolicyService::binderDied(const wp<IBinder>& who) {
Glenn Kasten411e4472012-11-02 10:00:06 -0700546 ALOGW("binderDied() %p, calling pid %d", who.unsafe_get(),
Eric Laurentde070132010-07-13 04:45:46 -0700547 IPCThreadState::self()->getCallingPid());
Mathias Agopian65ab4712010-07-14 17:59:35 -0700548}
549
Mikhail Naganov12b716c2020-04-30 22:37:43 +0000550static bool dumpTryLock(Mutex& mutex) ACQUIRE(mutex) NO_THREAD_SAFETY_ANALYSIS
Mathias Agopian65ab4712010-07-14 17:59:35 -0700551{
Mikhail Naganov12b716c2020-04-30 22:37:43 +0000552 return mutex.timedLock(kDumpLockTimeoutNs) == NO_ERROR;
553}
554
555static void dumpReleaseLock(Mutex& mutex, bool locked) RELEASE(mutex) NO_THREAD_SAFETY_ANALYSIS
556{
557 if (locked) mutex.unlock();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700558}
559
560status_t AudioPolicyService::dumpInternals(int fd)
561{
562 const size_t SIZE = 256;
563 char buffer[SIZE];
564 String8 result;
565
Eric Laurentdce54a12014-03-10 12:19:46 -0700566 snprintf(buffer, SIZE, "AudioPolicyManager: %p\n", mAudioPolicyManager);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700567 result.append(buffer);
568 snprintf(buffer, SIZE, "Command Thread: %p\n", mAudioCommandThread.get());
569 result.append(buffer);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700570
Hayden Gomes524159d2019-12-23 14:41:47 -0800571 snprintf(buffer, SIZE, "Supported System Usages:\n");
572 result.append(buffer);
573 for (std::vector<audio_usage_t>::iterator it = mSupportedSystemUsages.begin();
574 it != mSupportedSystemUsages.end(); ++it) {
575 snprintf(buffer, SIZE, "\t%d\n", *it);
576 result.append(buffer);
577 }
578
Mathias Agopian65ab4712010-07-14 17:59:35 -0700579 write(fd, result.string(), result.size());
580 return NO_ERROR;
581}
582
Eric Laurente8c8b432018-10-17 10:08:02 -0700583void AudioPolicyService::updateUidStates()
Svet Ganovf4ddfef2018-01-16 07:37:58 -0800584{
Eric Laurente8c8b432018-10-17 10:08:02 -0700585 Mutex::Autolock _l(mLock);
586 updateUidStates_l();
587}
588
589void AudioPolicyService::updateUidStates_l()
590{
Eric Laurent4eb58f12018-12-07 16:41:02 -0800591// Go over all active clients and allow capture (does not force silence) in the
592// following cases:
Evan Severson1f700cd2021-02-10 13:10:37 -0800593// The client is the assistant
594// AND an accessibility service is on TOP or a RTT call is active
Eric Laurent589171c2019-07-25 18:04:29 -0700595// AND the source is VOICE_RECOGNITION or HOTWORD
Evan Severson1f700cd2021-02-10 13:10:37 -0800596// OR uses VOICE_RECOGNITION AND is on TOP
597// OR uses HOTWORD
598// AND there is no active privacy sensitive capture or call
599// OR client has CAPTURE_AUDIO_OUTPUT privileged permission
600// OR The client is an accessibility service
601// AND Is on TOP
602// AND the source is VOICE_RECOGNITION or HOTWORD
603// OR The assistant is not on TOP
Eric Laurent589171c2019-07-25 18:04:29 -0700604// AND there is no active privacy sensitive capture or call
605// OR client has CAPTURE_AUDIO_OUTPUT privileged permission
Evan Severson1f700cd2021-02-10 13:10:37 -0800606// AND is on TOP
607// AND the source is VOICE_RECOGNITION or HOTWORD
608// OR the client source is virtual (remote submix, call audio TX or RX...)
609// OR the client source is HOTWORD
610// AND is on TOP
611// OR all active clients are using HOTWORD source
612// AND no call is active
613// OR client has CAPTURE_AUDIO_OUTPUT privileged permission
614// OR the client is the current InputMethodService
615// AND a RTT call is active AND the source is VOICE_RECOGNITION
616// OR Any client
617// AND The assistant is not on TOP
618// AND is on TOP or latest started
619// AND there is no active privacy sensitive capture or call
620// OR client has CAPTURE_AUDIO_OUTPUT privileged permission
Eric Laurent4eb58f12018-12-07 16:41:02 -0800621
Eric Laurent4e947da2019-10-17 15:24:06 -0700622
Eric Laurent4eb58f12018-12-07 16:41:02 -0800623 sp<AudioRecordClient> topActive;
624 sp<AudioRecordClient> latestActive;
Eric Laurentc21d5692020-02-25 10:24:36 -0800625 sp<AudioRecordClient> topSensitiveActive;
Eric Laurentb809a752020-06-29 09:53:13 -0700626 sp<AudioRecordClient> latestSensitiveActiveOrComm;
Eric Laurent1ff16a72019-03-14 18:35:04 -0700627
Eric Laurenta46bedb2018-12-07 18:01:26 -0800628 nsecs_t topStartNs = 0;
629 nsecs_t latestStartNs = 0;
Eric Laurentc21d5692020-02-25 10:24:36 -0800630 nsecs_t topSensitiveStartNs = 0;
Eric Laurent4eb58f12018-12-07 16:41:02 -0800631 nsecs_t latestSensitiveStartNs = 0;
632 bool isA11yOnTop = mUidPolicy->isA11yOnTop();
633 bool isAssistantOnTop = false;
634 bool isSensitiveActive = false;
Eric Laurent1ff16a72019-03-14 18:35:04 -0700635 bool isInCall = mPhoneState == AUDIO_MODE_IN_CALL;
Eric Laurentc21d5692020-02-25 10:24:36 -0800636 bool isInCommunication = mPhoneState == AUDIO_MODE_IN_COMMUNICATION;
637 bool rttCallActive = (isInCall || isInCommunication)
Eric Laurent6ede98f2019-06-11 14:50:30 -0700638 && mUidPolicy->isRttEnabled();
Eric Laurent4e947da2019-10-17 15:24:06 -0700639 bool onlyHotwordActive = true;
Eric Laurentb809a752020-06-29 09:53:13 -0700640 bool isPhoneStateOwnerActive = false;
Eric Laurent4eb58f12018-12-07 16:41:02 -0800641
Michael Groovercfd28302018-12-11 19:16:46 -0800642 // if Sensor Privacy is enabled then all recordings should be silenced.
643 if (mSensorPrivacyPolicy->isSensorPrivacyEnabled()) {
644 silenceAllRecordings_l();
645 return;
646 }
647
Eric Laurente8c8b432018-10-17 10:08:02 -0700648 for (size_t i =0; i < mAudioRecordClients.size(); i++) {
649 sp<AudioRecordClient> current = mAudioRecordClients[i];
Svet Ganov33761132021-05-13 22:51:08 +0000650 uid_t currentUid = VALUE_OR_FATAL(aidl2legacy_int32_t_uid_t(
651 current->attributionSource.uid));
Evan Severson1f700cd2021-02-10 13:10:37 -0800652 if (!current->active) {
Eric Laurent1ff16a72019-03-14 18:35:04 -0700653 continue;
Eric Laurent4eb58f12018-12-07 16:41:02 -0800654 }
Eric Laurent1ff16a72019-03-14 18:35:04 -0700655
Philip P. Moltmannbda45752020-07-17 16:41:18 -0700656 app_state_t appState = apmStatFromAmState(mUidPolicy->getUidState(currentUid));
Eric Laurent1ff16a72019-03-14 18:35:04 -0700657 // clients which app is in IDLE state are not eligible for top active or
658 // latest active
659 if (appState == APP_STATE_IDLE) {
660 continue;
661 }
662
Philip P. Moltmannbda45752020-07-17 16:41:18 -0700663 bool isAccessibility = mUidPolicy->isA11yUid(currentUid);
Eric Laurent14a88632020-07-16 12:28:30 -0700664 // Clients capturing for Accessibility services or virtual sources are not considered
Eric Laurentc21d5692020-02-25 10:24:36 -0800665 // for top or latest active to avoid masking regular clients started before
Eric Laurent14a88632020-07-16 12:28:30 -0700666 if (!isAccessibility && !isVirtualSource(current->attributes.source)) {
Philip P. Moltmannbda45752020-07-17 16:41:18 -0700667 bool isAssistant = mUidPolicy->isAssistantUid(currentUid);
Eric Laurentc21d5692020-02-25 10:24:36 -0800668 bool isPrivacySensitive =
669 (current->attributes.flags & AUDIO_FLAG_CAPTURE_PRIVATE) != 0;
Eric Laurentb809a752020-06-29 09:53:13 -0700670
Eric Laurentc21d5692020-02-25 10:24:36 -0800671 if (appState == APP_STATE_TOP) {
672 if (isPrivacySensitive) {
673 if (current->startTimeNs > topSensitiveStartNs) {
674 topSensitiveActive = current;
675 topSensitiveStartNs = current->startTimeNs;
676 }
677 } else {
678 if (current->startTimeNs > topStartNs) {
679 topActive = current;
680 topStartNs = current->startTimeNs;
681 }
682 }
683 if (isAssistant) {
684 isAssistantOnTop = true;
685 }
Eric Laurenta46bedb2018-12-07 18:01:26 -0800686 }
Eric Laurentc21d5692020-02-25 10:24:36 -0800687 // Clients capturing for HOTWORD are not considered
688 // for latest active to avoid masking regular clients started before
689 if (!(current->attributes.source == AUDIO_SOURCE_HOTWORD
690 || ((isA11yOnTop || rttCallActive) && isAssistant))) {
691 if (isPrivacySensitive) {
Eric Laurentb809a752020-06-29 09:53:13 -0700692 // if audio mode is IN_COMMUNICATION, make sure the audio mode owner
693 // is marked latest sensitive active even if another app qualifies.
694 if (current->startTimeNs > latestSensitiveStartNs
Philip P. Moltmannbda45752020-07-17 16:41:18 -0700695 || (isInCommunication && currentUid == mPhoneStateOwnerUid)) {
Eric Laurentb809a752020-06-29 09:53:13 -0700696 if (!isInCommunication || latestSensitiveActiveOrComm == nullptr
Philip P. Moltmannbda45752020-07-17 16:41:18 -0700697 || VALUE_OR_FATAL(aidl2legacy_int32_t_uid_t(
Svet Ganov33761132021-05-13 22:51:08 +0000698 latestSensitiveActiveOrComm->attributionSource.uid))
Philip P. Moltmannbda45752020-07-17 16:41:18 -0700699 != mPhoneStateOwnerUid) {
Eric Laurentb809a752020-06-29 09:53:13 -0700700 latestSensitiveActiveOrComm = current;
701 latestSensitiveStartNs = current->startTimeNs;
702 }
Eric Laurentc21d5692020-02-25 10:24:36 -0800703 }
704 isSensitiveActive = true;
705 } else {
706 if (current->startTimeNs > latestStartNs) {
707 latestActive = current;
708 latestStartNs = current->startTimeNs;
709 }
710 }
Eric Laurent4eb58f12018-12-07 16:41:02 -0800711 }
712 }
Eric Laurent4e947da2019-10-17 15:24:06 -0700713 if (current->attributes.source != AUDIO_SOURCE_HOTWORD) {
714 onlyHotwordActive = false;
715 }
Philip P. Moltmannbda45752020-07-17 16:41:18 -0700716 if (currentUid == mPhoneStateOwnerUid) {
Eric Laurentb809a752020-06-29 09:53:13 -0700717 isPhoneStateOwnerActive = true;
718 }
Eric Laurent4eb58f12018-12-07 16:41:02 -0800719 }
720
Eric Laurent1ff16a72019-03-14 18:35:04 -0700721 // if no active client with UI on Top, consider latest active as top
722 if (topActive == nullptr) {
723 topActive = latestActive;
Eric Laurentc21d5692020-02-25 10:24:36 -0800724 topStartNs = latestStartNs;
725 }
726 if (topSensitiveActive == nullptr) {
Eric Laurentb809a752020-06-29 09:53:13 -0700727 topSensitiveActive = latestSensitiveActiveOrComm;
Eric Laurentc21d5692020-02-25 10:24:36 -0800728 topSensitiveStartNs = latestSensitiveStartNs;
Eric Laurentb809a752020-06-29 09:53:13 -0700729 } else if (latestSensitiveActiveOrComm != nullptr) {
730 // if audio mode is IN_COMMUNICATION, favor audio mode owner over an app with
731 // foreground UI in case both are capturing with privacy sensitive flag.
Philip P. Moltmannbda45752020-07-17 16:41:18 -0700732 uid_t latestActiveUid = VALUE_OR_FATAL(
Svet Ganov33761132021-05-13 22:51:08 +0000733 aidl2legacy_int32_t_uid_t(latestSensitiveActiveOrComm->attributionSource.uid));
Philip P. Moltmannbda45752020-07-17 16:41:18 -0700734 if (isInCommunication && latestActiveUid == mPhoneStateOwnerUid) {
Eric Laurentb809a752020-06-29 09:53:13 -0700735 topSensitiveActive = latestSensitiveActiveOrComm;
736 topSensitiveStartNs = latestSensitiveStartNs;
737 }
Eric Laurentc21d5692020-02-25 10:24:36 -0800738 }
739
740 // If both privacy sensitive and regular capture are active:
741 // if the regular capture is privileged
742 // allow concurrency
743 // else
744 // favor the privacy sensitive case
745 if (topActive != nullptr && topSensitiveActive != nullptr
Ricardo Correa57a37692020-03-23 17:27:25 -0700746 && !topActive->canCaptureOutput) {
Eric Laurentc21d5692020-02-25 10:24:36 -0800747 topActive = nullptr;
Eric Laurent4eb58f12018-12-07 16:41:02 -0800748 }
749
750 for (size_t i =0; i < mAudioRecordClients.size(); i++) {
751 sp<AudioRecordClient> current = mAudioRecordClients[i];
Philip P. Moltmannbda45752020-07-17 16:41:18 -0700752 uid_t currentUid = VALUE_OR_FATAL(aidl2legacy_int32_t_uid_t(
Svet Ganov33761132021-05-13 22:51:08 +0000753 current->attributionSource.uid));
Eric Laurent1ff16a72019-03-14 18:35:04 -0700754 if (!current->active) {
755 continue;
756 }
757
Eric Laurent4eb58f12018-12-07 16:41:02 -0800758 audio_source_t source = current->attributes.source;
Philip P. Moltmannbda45752020-07-17 16:41:18 -0700759 bool isTopOrLatestActive = topActive == nullptr ? false :
Svet Ganov33761132021-05-13 22:51:08 +0000760 current->attributionSource.uid == topActive->attributionSource.uid;
Philip P. Moltmannbda45752020-07-17 16:41:18 -0700761 bool isTopOrLatestSensitive = topSensitiveActive == nullptr ? false :
Svet Ganov33761132021-05-13 22:51:08 +0000762 current->attributionSource.uid == topSensitiveActive->attributionSource.uid;
Eric Laurentc21d5692020-02-25 10:24:36 -0800763
Mikhail Naganov12b716c2020-04-30 22:37:43 +0000764 auto canCaptureIfInCallOrCommunication = [&](const auto &recordClient) REQUIRES(mLock) {
Philip P. Moltmannbda45752020-07-17 16:41:18 -0700765 uid_t recordUid = VALUE_OR_FATAL(aidl2legacy_int32_t_uid_t(
Svet Ganov33761132021-05-13 22:51:08 +0000766 recordClient->attributionSource.uid));
Ricardo Correa57a37692020-03-23 17:27:25 -0700767 bool canCaptureCall = recordClient->canCaptureOutput;
Eric Laurentb809a752020-06-29 09:53:13 -0700768 bool canCaptureCommunication = recordClient->canCaptureOutput
769 || !isPhoneStateOwnerActive
Philip P. Moltmannbda45752020-07-17 16:41:18 -0700770 || recordUid == mPhoneStateOwnerUid;
Eric Laurentb809a752020-06-29 09:53:13 -0700771 return !(isInCall && !canCaptureCall)
772 && !(isInCommunication && !canCaptureCommunication);
Eric Laurentc21d5692020-02-25 10:24:36 -0800773 };
Eric Laurent1ff16a72019-03-14 18:35:04 -0700774
775 // By default allow capture if:
776 // The assistant is not on TOP
Eric Laurenta171e352019-05-07 13:04:45 -0700777 // AND is on TOP or latest started
Eric Laurent1ff16a72019-03-14 18:35:04 -0700778 // AND there is no active privacy sensitive capture or call
779 // OR client has CAPTURE_AUDIO_OUTPUT privileged permission
780 bool allowCapture = !isAssistantOnTop
Eric Laurentc21d5692020-02-25 10:24:36 -0800781 && (isTopOrLatestActive || isTopOrLatestSensitive)
782 && !(isSensitiveActive
Ricardo Correa57a37692020-03-23 17:27:25 -0700783 && !(isTopOrLatestSensitive || current->canCaptureOutput))
Eric Laurentc21d5692020-02-25 10:24:36 -0800784 && canCaptureIfInCallOrCommunication(current);
Eric Laurent2dc962b2019-03-01 08:25:25 -0800785
Eric Laurented726cc2021-07-01 14:26:41 +0200786 if (!current->hasOp()) {
787 // Never allow capture if app op is denied
788 allowCapture = false;
789 } else if (isVirtualSource(source)) {
Eric Laurent1ff16a72019-03-14 18:35:04 -0700790 // Allow capture for virtual (remote submix, call audio TX or RX...) sources
791 allowCapture = true;
Philip P. Moltmannbda45752020-07-17 16:41:18 -0700792 } else if (mUidPolicy->isAssistantUid(currentUid)) {
Eric Laurent1ff16a72019-03-14 18:35:04 -0700793 // For assistant allow capture if:
Eric Laurent6ede98f2019-06-11 14:50:30 -0700794 // An accessibility service is on TOP or a RTT call is active
Eric Laurent1ff16a72019-03-14 18:35:04 -0700795 // AND the source is VOICE_RECOGNITION or HOTWORD
Eric Laurenta171e352019-05-07 13:04:45 -0700796 // OR is on TOP AND uses VOICE_RECOGNITION
Eric Laurent1ff16a72019-03-14 18:35:04 -0700797 // OR uses HOTWORD
798 // AND there is no active privacy sensitive capture or call
799 // OR client has CAPTURE_AUDIO_OUTPUT privileged permission
Eric Laurent6ede98f2019-06-11 14:50:30 -0700800 if (isA11yOnTop || rttCallActive) {
Eric Laurent4eb58f12018-12-07 16:41:02 -0800801 if (source == AUDIO_SOURCE_HOTWORD || source == AUDIO_SOURCE_VOICE_RECOGNITION) {
Eric Laurent1ff16a72019-03-14 18:35:04 -0700802 allowCapture = true;
Eric Laurent4eb58f12018-12-07 16:41:02 -0800803 }
804 } else {
Eric Laurenta171e352019-05-07 13:04:45 -0700805 if (((isAssistantOnTop && source == AUDIO_SOURCE_VOICE_RECOGNITION) ||
Eric Laurentc21d5692020-02-25 10:24:36 -0800806 source == AUDIO_SOURCE_HOTWORD)
Ricardo Correa57a37692020-03-23 17:27:25 -0700807 && !(isSensitiveActive && !current->canCaptureOutput)
Eric Laurentc21d5692020-02-25 10:24:36 -0800808 && canCaptureIfInCallOrCommunication(current)) {
Eric Laurent1ff16a72019-03-14 18:35:04 -0700809 allowCapture = true;
Eric Laurent4eb58f12018-12-07 16:41:02 -0800810 }
811 }
Philip P. Moltmannbda45752020-07-17 16:41:18 -0700812 } else if (mUidPolicy->isA11yUid(currentUid)) {
Eric Laurent1ff16a72019-03-14 18:35:04 -0700813 // For accessibility service allow capture if:
Eric Laurent47670c92019-08-28 16:59:05 -0700814 // The assistant is not on TOP
815 // AND there is no active privacy sensitive capture or call
Eric Laurent589171c2019-07-25 18:04:29 -0700816 // OR client has CAPTURE_AUDIO_OUTPUT privileged permission
Eric Laurent47670c92019-08-28 16:59:05 -0700817 // OR
818 // Is on TOP AND the source is VOICE_RECOGNITION or HOTWORD
819 if (!isAssistantOnTop
Ricardo Correa57a37692020-03-23 17:27:25 -0700820 && !(isSensitiveActive && !current->canCaptureOutput)
Eric Laurentc21d5692020-02-25 10:24:36 -0800821 && canCaptureIfInCallOrCommunication(current)) {
Eric Laurent47670c92019-08-28 16:59:05 -0700822 allowCapture = true;
823 }
Eric Laurent589171c2019-07-25 18:04:29 -0700824 if (isA11yOnTop) {
825 if (source == AUDIO_SOURCE_VOICE_RECOGNITION || source == AUDIO_SOURCE_HOTWORD) {
826 allowCapture = true;
827 }
Eric Laurent4eb58f12018-12-07 16:41:02 -0800828 }
Eric Laurent4e947da2019-10-17 15:24:06 -0700829 } else if (source == AUDIO_SOURCE_HOTWORD) {
830 // For HOTWORD source allow capture when not on TOP if:
831 // All active clients are using HOTWORD source
832 // AND no call is active
833 // OR client has CAPTURE_AUDIO_OUTPUT privileged permission
Eric Laurentc21d5692020-02-25 10:24:36 -0800834 if (onlyHotwordActive
835 && canCaptureIfInCallOrCommunication(current)) {
Eric Laurent4e947da2019-10-17 15:24:06 -0700836 allowCapture = true;
837 }
Philip P. Moltmannbda45752020-07-17 16:41:18 -0700838 } else if (mUidPolicy->isCurrentImeUid(currentUid)) {
Kohsuke Yatoha623a132020-03-24 20:10:26 -0700839 // For current InputMethodService allow capture if:
840 // A RTT call is active AND the source is VOICE_RECOGNITION
841 if (rttCallActive && source == AUDIO_SOURCE_VOICE_RECOGNITION) {
842 allowCapture = true;
843 }
Eric Laurent4eb58f12018-12-07 16:41:02 -0800844 }
Eric Laurent8c7ef892021-06-10 13:32:16 +0200845 setAppState_l(current,
Philip P. Moltmannbda45752020-07-17 16:41:18 -0700846 allowCapture ? apmStatFromAmState(mUidPolicy->getUidState(currentUid)) :
Eric Laurent1ff16a72019-03-14 18:35:04 -0700847 APP_STATE_IDLE);
Eric Laurente8c8b432018-10-17 10:08:02 -0700848 }
849}
850
Michael Groovercfd28302018-12-11 19:16:46 -0800851void AudioPolicyService::silenceAllRecordings_l() {
852 for (size_t i = 0; i < mAudioRecordClients.size(); i++) {
853 sp<AudioRecordClient> current = mAudioRecordClients[i];
Eric Laurent1ff16a72019-03-14 18:35:04 -0700854 if (!isVirtualSource(current->attributes.source)) {
Eric Laurent8c7ef892021-06-10 13:32:16 +0200855 setAppState_l(current, APP_STATE_IDLE);
Eric Laurent1ff16a72019-03-14 18:35:04 -0700856 }
Michael Groovercfd28302018-12-11 19:16:46 -0800857 }
858}
859
Eric Laurente8c8b432018-10-17 10:08:02 -0700860/* static */
861app_state_t AudioPolicyService::apmStatFromAmState(int amState) {
Eric Laurent1ff16a72019-03-14 18:35:04 -0700862
863 if (amState == ActivityManager::PROCESS_STATE_UNKNOWN) {
Eric Laurente8c8b432018-10-17 10:08:02 -0700864 return APP_STATE_IDLE;
Eric Laurent1ff16a72019-03-14 18:35:04 -0700865 } else if (amState <= ActivityManager::PROCESS_STATE_TOP) {
866 // include persistent services
867 return APP_STATE_TOP;
Eric Laurente8c8b432018-10-17 10:08:02 -0700868 }
869 return APP_STATE_FOREGROUND;
870}
871
Eric Laurent4eb58f12018-12-07 16:41:02 -0800872/* static */
Eric Laurent2dc962b2019-03-01 08:25:25 -0800873bool AudioPolicyService::isVirtualSource(audio_source_t source)
Eric Laurent4eb58f12018-12-07 16:41:02 -0800874{
875 switch (source) {
876 case AUDIO_SOURCE_VOICE_UPLINK:
877 case AUDIO_SOURCE_VOICE_DOWNLINK:
878 case AUDIO_SOURCE_VOICE_CALL:
Eric Laurent2dc962b2019-03-01 08:25:25 -0800879 case AUDIO_SOURCE_REMOTE_SUBMIX:
880 case AUDIO_SOURCE_FM_TUNER:
Eric Laurent68eb2122020-04-30 17:40:57 -0700881 case AUDIO_SOURCE_ECHO_REFERENCE:
Eric Laurent4eb58f12018-12-07 16:41:02 -0800882 return true;
883 default:
884 break;
885 }
886 return false;
887}
888
Eric Laurented726cc2021-07-01 14:26:41 +0200889/* static */
890bool AudioPolicyService::isAppOpSource(audio_source_t source)
891{
892 switch (source) {
893 case AUDIO_SOURCE_FM_TUNER:
894 case AUDIO_SOURCE_ECHO_REFERENCE:
895 return false;
896 default:
897 break;
898 }
899 return true;
900}
901
Eric Laurent8c7ef892021-06-10 13:32:16 +0200902void AudioPolicyService::setAppState_l(sp<AudioRecordClient> client, app_state_t state)
Eric Laurente8c8b432018-10-17 10:08:02 -0700903{
904 AutoCallerClear acc;
905
906 if (mAudioPolicyManager) {
Eric Laurent8c7ef892021-06-10 13:32:16 +0200907 mAudioPolicyManager->setAppState(client->portId, state);
Mikhail Naganoveae73eb2018-04-03 16:57:36 -0700908 }
909 sp<IAudioFlinger> af = AudioSystem::get_audio_flinger();
910 if (af) {
Eric Laurentf32108e2018-10-04 17:22:04 -0700911 bool silenced = state == APP_STATE_IDLE;
Eric Laurent8c7ef892021-06-10 13:32:16 +0200912 if (client->silenced != silenced) {
913 if (client->active) {
914 if (silenced) {
915 finishRecording(client->attributionSource, client->attributes.source);
916 } else {
917 std::stringstream msg;
918 msg << "Audio recording un-silenced on session " << client->session;
919 if (!startRecording(client->attributionSource, String16(msg.str().c_str()),
920 client->attributes.source)) {
921 silenced = true;
922 }
923 }
924 }
925 af->setRecordSilenced(client->portId, silenced);
926 client->silenced = silenced;
927 }
Mikhail Naganoveae73eb2018-04-03 16:57:36 -0700928 }
Svet Ganovf4ddfef2018-01-16 07:37:58 -0800929}
930
Glenn Kasten0f11b512014-01-31 16:18:54 -0800931status_t AudioPolicyService::dump(int fd, const Vector<String16>& args __unused)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700932{
Glenn Kasten44deb052012-02-05 18:09:08 -0800933 if (!dumpAllowed()) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700934 dumpPermissionDenial(fd);
935 } else {
Mikhail Naganov12b716c2020-04-30 22:37:43 +0000936 const bool locked = dumpTryLock(mLock);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700937 if (!locked) {
938 String8 result(kDeadlockedString);
939 write(fd, result.string(), result.size());
940 }
941
942 dumpInternals(fd);
Glenn Kasten9d1f02d2012-02-08 17:47:58 -0800943 if (mAudioCommandThread != 0) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700944 mAudioCommandThread->dump(fd);
945 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700946
Eric Laurentdce54a12014-03-10 12:19:46 -0700947 if (mAudioPolicyManager) {
948 mAudioPolicyManager->dump(fd);
949 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700950
Kevin Rocard8be94972019-02-22 13:26:25 -0800951 mPackageManager.dump(fd);
952
Mikhail Naganov12b716c2020-04-30 22:37:43 +0000953 dumpReleaseLock(mLock, locked);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700954 }
955 return NO_ERROR;
956}
957
958status_t AudioPolicyService::dumpPermissionDenial(int fd)
959{
960 const size_t SIZE = 256;
961 char buffer[SIZE];
962 String8 result;
963 snprintf(buffer, SIZE, "Permission Denial: "
964 "can't dump AudioPolicyService from pid=%d, uid=%d\n",
965 IPCThreadState::self()->getCallingPid(),
966 IPCThreadState::self()->getCallingUid());
967 result.append(buffer);
968 write(fd, result.string(), result.size());
969 return NO_ERROR;
970}
971
972status_t AudioPolicyService::onTransact(
Svet Ganovf4ddfef2018-01-16 07:37:58 -0800973 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags) {
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800974 // make sure transactions reserved to AudioFlinger do not come from other processes
975 switch (code) {
976 case TRANSACTION_startOutput:
977 case TRANSACTION_stopOutput:
978 case TRANSACTION_releaseOutput:
979 case TRANSACTION_getInputForAttr:
980 case TRANSACTION_startInput:
981 case TRANSACTION_stopInput:
982 case TRANSACTION_releaseInput:
983 case TRANSACTION_getOutputForEffect:
984 case TRANSACTION_registerEffect:
985 case TRANSACTION_unregisterEffect:
986 case TRANSACTION_setEffectEnabled:
987 case TRANSACTION_getStrategyForStream:
988 case TRANSACTION_getOutputForAttr:
989 case TRANSACTION_moveEffectsToIo:
990 ALOGW("%s: transaction %d received from PID %d",
991 __func__, code, IPCThreadState::self()->getCallingPid());
992 return INVALID_OPERATION;
993 default:
994 break;
995 }
996
997 // make sure the following transactions come from system components
998 switch (code) {
999 case TRANSACTION_setDeviceConnectionState:
1000 case TRANSACTION_handleDeviceConfigChange:
1001 case TRANSACTION_setPhoneState:
1002//FIXME: Allow setForceUse calls from system apps until a better use case routing API is available
1003// case TRANSACTION_setForceUse:
1004 case TRANSACTION_initStreamVolume:
1005 case TRANSACTION_setStreamVolumeIndex:
1006 case TRANSACTION_setVolumeIndexForAttributes:
1007 case TRANSACTION_getStreamVolumeIndex:
1008 case TRANSACTION_getVolumeIndexForAttributes:
1009 case TRANSACTION_getMinVolumeIndexForAttributes:
1010 case TRANSACTION_getMaxVolumeIndexForAttributes:
1011 case TRANSACTION_isStreamActive:
1012 case TRANSACTION_isStreamActiveRemotely:
1013 case TRANSACTION_isSourceActive:
1014 case TRANSACTION_getDevicesForStream:
1015 case TRANSACTION_registerPolicyMixes:
1016 case TRANSACTION_setMasterMono:
1017 case TRANSACTION_getSurroundFormats:
Kriti Dang6537def2021-03-02 13:46:59 +01001018 case TRANSACTION_getReportedSurroundFormats:
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001019 case TRANSACTION_setSurroundFormatEnabled:
1020 case TRANSACTION_setAssistantUid:
1021 case TRANSACTION_setA11yServicesUids:
1022 case TRANSACTION_setUidDeviceAffinities:
1023 case TRANSACTION_removeUidDeviceAffinities:
1024 case TRANSACTION_setUserIdDeviceAffinities:
1025 case TRANSACTION_removeUserIdDeviceAffinities:
1026 case TRANSACTION_getHwOffloadEncodingFormatsSupportedForA2DP:
1027 case TRANSACTION_listAudioVolumeGroups:
1028 case TRANSACTION_getVolumeGroupFromAudioAttributes:
1029 case TRANSACTION_acquireSoundTriggerSession:
1030 case TRANSACTION_releaseSoundTriggerSession:
1031 case TRANSACTION_setRttEnabled:
1032 case TRANSACTION_isCallScreenModeSupported:
1033 case TRANSACTION_setDevicesRoleForStrategy:
1034 case TRANSACTION_setSupportedSystemUsages:
1035 case TRANSACTION_removeDevicesRoleForStrategy:
1036 case TRANSACTION_getDevicesForRoleAndStrategy:
1037 case TRANSACTION_getDevicesForAttributes:
1038 case TRANSACTION_setAllowedCapturePolicy:
1039 case TRANSACTION_onNewAudioModulesAvailable:
1040 case TRANSACTION_setCurrentImeUid:
1041 case TRANSACTION_registerSoundTriggerCaptureStateListener:
1042 case TRANSACTION_setDevicesRoleForCapturePreset:
1043 case TRANSACTION_addDevicesRoleForCapturePreset:
1044 case TRANSACTION_removeDevicesRoleForCapturePreset:
1045 case TRANSACTION_clearDevicesRoleForCapturePreset:
Eric Laurent6d607012021-07-05 11:54:40 +02001046 case TRANSACTION_getDevicesForRoleAndCapturePreset:
1047 case TRANSACTION_getSpatializer: {
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001048 if (!isServiceUid(IPCThreadState::self()->getCallingUid())) {
1049 ALOGW("%s: transaction %d received from PID %d unauthorized UID %d",
1050 __func__, code, IPCThreadState::self()->getCallingPid(),
1051 IPCThreadState::self()->getCallingUid());
1052 return INVALID_OPERATION;
1053 }
1054 } break;
1055 default:
1056 break;
1057 }
1058
1059 std::string tag("IAudioPolicyService command " + std::to_string(code));
1060 TimeCheck check(tag.c_str());
1061
Svet Ganovf4ddfef2018-01-16 07:37:58 -08001062 switch (code) {
1063 case SHELL_COMMAND_TRANSACTION: {
1064 int in = data.readFileDescriptor();
1065 int out = data.readFileDescriptor();
1066 int err = data.readFileDescriptor();
1067 int argc = data.readInt32();
1068 Vector<String16> args;
1069 for (int i = 0; i < argc && data.dataAvail() > 0; i++) {
1070 args.add(data.readString16());
1071 }
1072 sp<IBinder> unusedCallback;
1073 sp<IResultReceiver> resultReceiver;
1074 status_t status;
1075 if ((status = data.readNullableStrongBinder(&unusedCallback)) != NO_ERROR) {
1076 return status;
1077 }
1078 if ((status = data.readNullableStrongBinder(&resultReceiver)) != NO_ERROR) {
1079 return status;
1080 }
1081 status = shellCommand(in, out, err, args);
1082 if (resultReceiver != nullptr) {
1083 resultReceiver->send(status);
1084 }
1085 return NO_ERROR;
1086 }
1087 }
1088
Mathias Agopian65ab4712010-07-14 17:59:35 -07001089 return BnAudioPolicyService::onTransact(code, data, reply, flags);
1090}
1091
Svet Ganovf4ddfef2018-01-16 07:37:58 -08001092// ------------------- Shell command implementation -------------------
1093
1094// NOTE: This is a remote API - make sure all args are validated
1095status_t AudioPolicyService::shellCommand(int in, int out, int err, Vector<String16>& args) {
1096 if (!checkCallingPermission(sManageAudioPolicyPermission, nullptr, nullptr)) {
1097 return PERMISSION_DENIED;
1098 }
1099 if (in == BAD_TYPE || out == BAD_TYPE || err == BAD_TYPE) {
1100 return BAD_VALUE;
1101 }
jovanakbe066e12019-09-02 11:54:39 -07001102 if (args.size() >= 3 && args[0] == String16("set-uid-state")) {
Svet Ganovf4ddfef2018-01-16 07:37:58 -08001103 return handleSetUidState(args, err);
jovanakbe066e12019-09-02 11:54:39 -07001104 } else if (args.size() >= 2 && args[0] == String16("reset-uid-state")) {
Svet Ganovf4ddfef2018-01-16 07:37:58 -08001105 return handleResetUidState(args, err);
jovanakbe066e12019-09-02 11:54:39 -07001106 } else if (args.size() >= 2 && args[0] == String16("get-uid-state")) {
Svet Ganovf4ddfef2018-01-16 07:37:58 -08001107 return handleGetUidState(args, out, err);
Eric Laurent269acb42021-04-23 16:53:22 +02001108 } else if (args.size() >= 1 && args[0] == String16("purge_permission-cache")) {
1109 purgePermissionCache();
1110 return NO_ERROR;
Svet Ganovf4ddfef2018-01-16 07:37:58 -08001111 } else if (args.size() == 1 && args[0] == String16("help")) {
1112 printHelp(out);
1113 return NO_ERROR;
1114 }
1115 printHelp(err);
1116 return BAD_VALUE;
1117}
1118
jovanakbe066e12019-09-02 11:54:39 -07001119static status_t getUidForPackage(String16 packageName, int userId, /*inout*/uid_t& uid, int err) {
1120 if (userId < 0) {
1121 ALOGE("Invalid user: %d", userId);
1122 dprintf(err, "Invalid user: %d\n", userId);
Svet Ganovf4ddfef2018-01-16 07:37:58 -08001123 return BAD_VALUE;
1124 }
jovanakbe066e12019-09-02 11:54:39 -07001125
1126 PermissionController pc;
1127 uid = pc.getPackageUid(packageName, 0);
1128 if (uid <= 0) {
1129 ALOGE("Unknown package: '%s'", String8(packageName).string());
1130 dprintf(err, "Unknown package: '%s'\n", String8(packageName).string());
1131 return BAD_VALUE;
1132 }
1133
1134 uid = multiuser_get_uid(userId, uid);
1135 return NO_ERROR;
1136}
1137
1138status_t AudioPolicyService::handleSetUidState(Vector<String16>& args, int err) {
1139 // Valid arg.size() is 3 or 5, args.size() is 5 with --user option.
1140 if (!(args.size() == 3 || args.size() == 5)) {
1141 printHelp(err);
1142 return BAD_VALUE;
1143 }
1144
Svet Ganovf4ddfef2018-01-16 07:37:58 -08001145 bool active = false;
1146 if (args[2] == String16("active")) {
1147 active = true;
1148 } else if ((args[2] != String16("idle"))) {
1149 ALOGE("Expected active or idle but got: '%s'", String8(args[2]).string());
1150 return BAD_VALUE;
1151 }
jovanakbe066e12019-09-02 11:54:39 -07001152
1153 int userId = 0;
1154 if (args.size() >= 5 && args[3] == String16("--user")) {
1155 userId = atoi(String8(args[4]));
1156 }
1157
1158 uid_t uid;
1159 if (getUidForPackage(args[1], userId, uid, err) == BAD_VALUE) {
1160 return BAD_VALUE;
1161 }
1162
Mikhail Naganov12b716c2020-04-30 22:37:43 +00001163 sp<UidPolicy> uidPolicy;
1164 {
1165 Mutex::Autolock _l(mLock);
1166 uidPolicy = mUidPolicy;
1167 }
1168 if (uidPolicy) {
1169 uidPolicy->addOverrideUid(uid, active);
1170 return NO_ERROR;
1171 }
1172 return NO_INIT;
Svet Ganovf4ddfef2018-01-16 07:37:58 -08001173}
1174
1175status_t AudioPolicyService::handleResetUidState(Vector<String16>& args, int err) {
jovanakbe066e12019-09-02 11:54:39 -07001176 // Valid arg.size() is 2 or 4, args.size() is 4 with --user option.
1177 if (!(args.size() == 2 || args.size() == 4)) {
1178 printHelp(err);
Svet Ganovf4ddfef2018-01-16 07:37:58 -08001179 return BAD_VALUE;
1180 }
jovanakbe066e12019-09-02 11:54:39 -07001181
1182 int userId = 0;
1183 if (args.size() >= 4 && args[2] == String16("--user")) {
1184 userId = atoi(String8(args[3]));
1185 }
1186
1187 uid_t uid;
1188 if (getUidForPackage(args[1], userId, uid, err) == BAD_VALUE) {
1189 return BAD_VALUE;
1190 }
1191
Mikhail Naganov12b716c2020-04-30 22:37:43 +00001192 sp<UidPolicy> uidPolicy;
1193 {
1194 Mutex::Autolock _l(mLock);
1195 uidPolicy = mUidPolicy;
1196 }
1197 if (uidPolicy) {
1198 uidPolicy->removeOverrideUid(uid);
1199 return NO_ERROR;
1200 }
1201 return NO_INIT;
Svet Ganovf4ddfef2018-01-16 07:37:58 -08001202}
1203
1204status_t AudioPolicyService::handleGetUidState(Vector<String16>& args, int out, int err) {
jovanakbe066e12019-09-02 11:54:39 -07001205 // Valid arg.size() is 2 or 4, args.size() is 4 with --user option.
1206 if (!(args.size() == 2 || args.size() == 4)) {
1207 printHelp(err);
Svet Ganovf4ddfef2018-01-16 07:37:58 -08001208 return BAD_VALUE;
1209 }
jovanakbe066e12019-09-02 11:54:39 -07001210
1211 int userId = 0;
1212 if (args.size() >= 4 && args[2] == String16("--user")) {
1213 userId = atoi(String8(args[3]));
1214 }
1215
1216 uid_t uid;
1217 if (getUidForPackage(args[1], userId, uid, err) == BAD_VALUE) {
1218 return BAD_VALUE;
1219 }
1220
Mikhail Naganov12b716c2020-04-30 22:37:43 +00001221 sp<UidPolicy> uidPolicy;
1222 {
1223 Mutex::Autolock _l(mLock);
1224 uidPolicy = mUidPolicy;
Svet Ganovf4ddfef2018-01-16 07:37:58 -08001225 }
Mikhail Naganov12b716c2020-04-30 22:37:43 +00001226 if (uidPolicy) {
1227 return dprintf(out, uidPolicy->isUidActive(uid) ? "active\n" : "idle\n");
1228 }
1229 return NO_INIT;
Svet Ganovf4ddfef2018-01-16 07:37:58 -08001230}
1231
1232status_t AudioPolicyService::printHelp(int out) {
1233 return dprintf(out, "Audio policy service commands:\n"
jovanakbe066e12019-09-02 11:54:39 -07001234 " get-uid-state <PACKAGE> [--user USER_ID] gets the uid state\n"
1235 " set-uid-state <PACKAGE> <active|idle> [--user USER_ID] overrides the uid state\n"
1236 " reset-uid-state <PACKAGE> [--user USER_ID] clears the uid state override\n"
Svet Ganovf4ddfef2018-01-16 07:37:58 -08001237 " help print this message\n");
1238}
1239
1240// ----------- AudioPolicyService::UidPolicy implementation ----------
1241
1242void AudioPolicyService::UidPolicy::registerSelf() {
Steven Moreland2f348142019-07-02 15:59:07 -07001243 status_t res = mAm.linkToDeath(this);
1244 mAm.registerUidObserver(this, ActivityManager::UID_OBSERVER_GONE
Svet Ganovf4ddfef2018-01-16 07:37:58 -08001245 | ActivityManager::UID_OBSERVER_IDLE
Eric Laurente8c8b432018-10-17 10:08:02 -07001246 | ActivityManager::UID_OBSERVER_ACTIVE
1247 | ActivityManager::UID_OBSERVER_PROCSTATE,
Svet Ganovf4ddfef2018-01-16 07:37:58 -08001248 ActivityManager::PROCESS_STATE_UNKNOWN,
1249 String16("audioserver"));
Mikhail Naganoveae73eb2018-04-03 16:57:36 -07001250 if (!res) {
1251 Mutex::Autolock _l(mLock);
1252 mObserverRegistered = true;
1253 } else {
1254 ALOGE("UidPolicy::registerSelf linkToDeath failed: %d", res);
Eric Laurent4eb58f12018-12-07 16:41:02 -08001255
Steven Moreland2f348142019-07-02 15:59:07 -07001256 mAm.unregisterUidObserver(this);
Mikhail Naganoveae73eb2018-04-03 16:57:36 -07001257 }
Svet Ganovf4ddfef2018-01-16 07:37:58 -08001258}
1259
1260void AudioPolicyService::UidPolicy::unregisterSelf() {
Steven Moreland2f348142019-07-02 15:59:07 -07001261 mAm.unlinkToDeath(this);
1262 mAm.unregisterUidObserver(this);
Mikhail Naganoveae73eb2018-04-03 16:57:36 -07001263 Mutex::Autolock _l(mLock);
1264 mObserverRegistered = false;
Svet Ganovf4ddfef2018-01-16 07:37:58 -08001265}
1266
Mikhail Naganoveae73eb2018-04-03 16:57:36 -07001267void AudioPolicyService::UidPolicy::binderDied(__unused const wp<IBinder> &who) {
1268 Mutex::Autolock _l(mLock);
1269 mCachedUids.clear();
1270 mObserverRegistered = false;
Svet Ganovf4ddfef2018-01-16 07:37:58 -08001271}
1272
Eric Laurente8c8b432018-10-17 10:08:02 -07001273void AudioPolicyService::UidPolicy::checkRegistered() {
Mikhail Naganoveae73eb2018-04-03 16:57:36 -07001274 bool needToReregister = false;
1275 {
1276 Mutex::Autolock _l(mLock);
1277 needToReregister = !mObserverRegistered;
Svet Ganovf4ddfef2018-01-16 07:37:58 -08001278 }
Mikhail Naganoveae73eb2018-04-03 16:57:36 -07001279 if (needToReregister) {
1280 // Looks like ActivityManager has died previously, attempt to re-register.
1281 registerSelf();
1282 }
Eric Laurente8c8b432018-10-17 10:08:02 -07001283}
1284
1285bool AudioPolicyService::UidPolicy::isUidActive(uid_t uid) {
1286 if (isServiceUid(uid)) return true;
1287 checkRegistered();
Mikhail Naganoveae73eb2018-04-03 16:57:36 -07001288 {
1289 Mutex::Autolock _l(mLock);
1290 auto overrideIter = mOverrideUids.find(uid);
1291 if (overrideIter != mOverrideUids.end()) {
Eric Laurente8c8b432018-10-17 10:08:02 -07001292 return overrideIter->second.first;
Mikhail Naganoveae73eb2018-04-03 16:57:36 -07001293 }
1294 // In an absense of the ActivityManager, assume everything to be active.
1295 if (!mObserverRegistered) return true;
1296 auto cacheIter = mCachedUids.find(uid);
Mikhail Naganoveba668a2018-04-05 08:13:15 -07001297 if (cacheIter != mCachedUids.end()) {
Eric Laurente8c8b432018-10-17 10:08:02 -07001298 return cacheIter->second.first;
Mikhail Naganoveae73eb2018-04-03 16:57:36 -07001299 }
1300 }
1301 ActivityManager am;
Hui Yu12c7ec72020-05-04 17:40:52 +00001302 bool active = am.isUidActive(uid, String16("audioserver"));
Mikhail Naganoveae73eb2018-04-03 16:57:36 -07001303 {
1304 Mutex::Autolock _l(mLock);
Eric Laurente8c8b432018-10-17 10:08:02 -07001305 mCachedUids.insert(std::pair<uid_t,
1306 std::pair<bool, int>>(uid, std::pair<bool, int>(active,
1307 ActivityManager::PROCESS_STATE_UNKNOWN)));
Mikhail Naganoveae73eb2018-04-03 16:57:36 -07001308 }
1309 return active;
Svet Ganovf4ddfef2018-01-16 07:37:58 -08001310}
1311
Eric Laurente8c8b432018-10-17 10:08:02 -07001312int AudioPolicyService::UidPolicy::getUidState(uid_t uid) {
1313 if (isServiceUid(uid)) {
1314 return ActivityManager::PROCESS_STATE_TOP;
1315 }
1316 checkRegistered();
1317 {
1318 Mutex::Autolock _l(mLock);
1319 auto overrideIter = mOverrideUids.find(uid);
1320 if (overrideIter != mOverrideUids.end()) {
1321 if (overrideIter->second.first) {
1322 if (overrideIter->second.second != ActivityManager::PROCESS_STATE_UNKNOWN) {
1323 return overrideIter->second.second;
1324 } else {
1325 auto cacheIter = mCachedUids.find(uid);
1326 if (cacheIter != mCachedUids.end()) {
1327 return cacheIter->second.second;
1328 }
1329 }
1330 }
1331 return ActivityManager::PROCESS_STATE_UNKNOWN;
1332 }
1333 // In an absense of the ActivityManager, assume everything to be active.
1334 if (!mObserverRegistered) {
1335 return ActivityManager::PROCESS_STATE_TOP;
1336 }
1337 auto cacheIter = mCachedUids.find(uid);
1338 if (cacheIter != mCachedUids.end()) {
1339 if (cacheIter->second.first) {
1340 return cacheIter->second.second;
1341 } else {
1342 return ActivityManager::PROCESS_STATE_UNKNOWN;
1343 }
1344 }
1345 }
1346 ActivityManager am;
Hui Yu12c7ec72020-05-04 17:40:52 +00001347 bool active = am.isUidActive(uid, String16("audioserver"));
Eric Laurente8c8b432018-10-17 10:08:02 -07001348 int state = ActivityManager::PROCESS_STATE_UNKNOWN;
1349 if (active) {
1350 state = am.getUidProcessState(uid, String16("audioserver"));
1351 }
1352 {
1353 Mutex::Autolock _l(mLock);
1354 mCachedUids.insert(std::pair<uid_t,
1355 std::pair<bool, int>>(uid, std::pair<bool, int>(active, state)));
1356 }
Eric Laurent4eb58f12018-12-07 16:41:02 -08001357
Eric Laurente8c8b432018-10-17 10:08:02 -07001358 return state;
1359}
1360
Mikhail Naganoveae73eb2018-04-03 16:57:36 -07001361void AudioPolicyService::UidPolicy::onUidActive(uid_t uid) {
Eric Laurente8c8b432018-10-17 10:08:02 -07001362 updateUid(&mCachedUids, uid, true, ActivityManager::PROCESS_STATE_UNKNOWN, true);
Mikhail Naganoveae73eb2018-04-03 16:57:36 -07001363}
1364
1365void AudioPolicyService::UidPolicy::onUidGone(uid_t uid, __unused bool disabled) {
Eric Laurente8c8b432018-10-17 10:08:02 -07001366 updateUid(&mCachedUids, uid, false, ActivityManager::PROCESS_STATE_UNKNOWN, false);
Mikhail Naganoveae73eb2018-04-03 16:57:36 -07001367}
1368
1369void AudioPolicyService::UidPolicy::onUidIdle(uid_t uid, __unused bool disabled) {
Eric Laurente8c8b432018-10-17 10:08:02 -07001370 updateUid(&mCachedUids, uid, false, ActivityManager::PROCESS_STATE_UNKNOWN, true);
Mikhail Naganoveae73eb2018-04-03 16:57:36 -07001371}
1372
Eric Laurente8c8b432018-10-17 10:08:02 -07001373void AudioPolicyService::UidPolicy::onUidStateChanged(uid_t uid,
1374 int32_t procState,
Hui Yu13ad0eb2019-09-09 10:27:07 -07001375 int64_t procStateSeq __unused,
1376 int32_t capability __unused) {
Eric Laurente8c8b432018-10-17 10:08:02 -07001377 if (procState != ActivityManager::PROCESS_STATE_UNKNOWN) {
1378 updateUid(&mCachedUids, uid, true, procState, true);
Mikhail Naganoveae73eb2018-04-03 16:57:36 -07001379 }
1380}
1381
1382void AudioPolicyService::UidPolicy::updateOverrideUid(uid_t uid, bool active, bool insert) {
Eric Laurente8c8b432018-10-17 10:08:02 -07001383 updateUid(&mOverrideUids, uid, active, ActivityManager::PROCESS_STATE_UNKNOWN, insert);
1384}
1385
1386void AudioPolicyService::UidPolicy::notifyService() {
1387 sp<AudioPolicyService> service = mService.promote();
1388 if (service != nullptr) {
1389 service->updateUidStates();
Mikhail Naganoveae73eb2018-04-03 16:57:36 -07001390 }
1391}
1392
Eric Laurente8c8b432018-10-17 10:08:02 -07001393void AudioPolicyService::UidPolicy::updateUid(std::unordered_map<uid_t,
1394 std::pair<bool, int>> *uids,
1395 uid_t uid,
1396 bool active,
1397 int state,
1398 bool insert) {
1399 if (isServiceUid(uid)) {
1400 return;
1401 }
1402 bool wasActive = isUidActive(uid);
1403 int previousState = getUidState(uid);
Mikhail Naganoveae73eb2018-04-03 16:57:36 -07001404 {
1405 Mutex::Autolock _l(mLock);
Eric Laurente8c8b432018-10-17 10:08:02 -07001406 updateUidLocked(uids, uid, active, state, insert);
Mikhail Naganoveae73eb2018-04-03 16:57:36 -07001407 }
Eric Laurente8c8b432018-10-17 10:08:02 -07001408 if (wasActive != isUidActive(uid) || state != previousState) {
1409 notifyService();
1410 }
Mikhail Naganoveae73eb2018-04-03 16:57:36 -07001411}
1412
Eric Laurente8c8b432018-10-17 10:08:02 -07001413void AudioPolicyService::UidPolicy::updateUidLocked(std::unordered_map<uid_t,
1414 std::pair<bool, int>> *uids,
1415 uid_t uid,
1416 bool active,
1417 int state,
1418 bool insert) {
Mikhail Naganoveae73eb2018-04-03 16:57:36 -07001419 auto it = uids->find(uid);
1420 if (it != uids->end()) {
Mikhail Naganoveae73eb2018-04-03 16:57:36 -07001421 if (insert) {
Eric Laurente8c8b432018-10-17 10:08:02 -07001422 if (state == ActivityManager::PROCESS_STATE_UNKNOWN) {
1423 it->second.first = active;
1424 }
1425 if (it->second.first) {
1426 it->second.second = state;
1427 } else {
1428 it->second.second = ActivityManager::PROCESS_STATE_UNKNOWN;
1429 }
Mikhail Naganoveae73eb2018-04-03 16:57:36 -07001430 } else {
1431 uids->erase(it);
1432 }
Eric Laurente8c8b432018-10-17 10:08:02 -07001433 } else if (insert && (state == ActivityManager::PROCESS_STATE_UNKNOWN)) {
1434 uids->insert(std::pair<uid_t, std::pair<bool, int>>(uid,
1435 std::pair<bool, int>(active, state)));
Mikhail Naganoveae73eb2018-04-03 16:57:36 -07001436 }
Svet Ganovf4ddfef2018-01-16 07:37:58 -08001437}
Mathias Agopian65ab4712010-07-14 17:59:35 -07001438
Eric Laurent4eb58f12018-12-07 16:41:02 -08001439bool AudioPolicyService::UidPolicy::isA11yOnTop() {
1440 for (const auto &uid : mCachedUids) {
Eric Laurent47670c92019-08-28 16:59:05 -07001441 if (!isA11yUid(uid.first)) {
Eric Laurent4eb58f12018-12-07 16:41:02 -08001442 continue;
1443 }
Amith Yamasanibcbb3002019-01-23 13:53:33 -08001444 if (uid.second.second >= ActivityManager::PROCESS_STATE_TOP
1445 && uid.second.second <= ActivityManager::PROCESS_STATE_BOUND_FOREGROUND_SERVICE) {
Eric Laurent4eb58f12018-12-07 16:41:02 -08001446 return true;
1447 }
1448 }
1449 return false;
1450}
1451
Eric Laurentb78763e2018-10-17 10:08:02 -07001452bool AudioPolicyService::UidPolicy::isA11yUid(uid_t uid)
1453{
1454 std::vector<uid_t>::iterator it = find(mA11yUids.begin(), mA11yUids.end(), uid);
1455 return it != mA11yUids.end();
1456}
1457
Michael Groovercfd28302018-12-11 19:16:46 -08001458// ----------- AudioPolicyService::SensorPrivacyService implementation ----------
1459void AudioPolicyService::SensorPrivacyPolicy::registerSelf() {
1460 SensorPrivacyManager spm;
1461 mSensorPrivacyEnabled = spm.isSensorPrivacyEnabled();
1462 spm.addSensorPrivacyListener(this);
1463}
1464
Evan Severson241d9592021-01-08 12:16:02 -08001465void AudioPolicyService::SensorPrivacyPolicy::registerSelfForMicrophoneOnly(int userId) {
1466 SensorPrivacyManager spm;
1467 mSensorPrivacyEnabled = spm.isIndividualSensorPrivacyEnabled(userId,
1468 SensorPrivacyManager::INDIVIDUAL_SENSOR_MICROPHONE);
1469 spm.addIndividualSensorPrivacyListener(userId,
1470 SensorPrivacyManager::INDIVIDUAL_SENSOR_MICROPHONE, this);
1471}
1472
Michael Groovercfd28302018-12-11 19:16:46 -08001473void AudioPolicyService::SensorPrivacyPolicy::unregisterSelf() {
1474 SensorPrivacyManager spm;
1475 spm.removeSensorPrivacyListener(this);
1476}
1477
1478bool AudioPolicyService::SensorPrivacyPolicy::isSensorPrivacyEnabled() {
1479 return mSensorPrivacyEnabled;
1480}
1481
1482binder::Status AudioPolicyService::SensorPrivacyPolicy::onSensorPrivacyChanged(bool enabled) {
1483 mSensorPrivacyEnabled = enabled;
1484 sp<AudioPolicyService> service = mService.promote();
1485 if (service != nullptr) {
1486 service->updateUidStates();
1487 }
1488 return binder::Status::ok();
1489}
1490
Eric Laurented726cc2021-07-01 14:26:41 +02001491// ----------- AudioPolicyService::OpRecordAudioMonitor implementation ----------
1492
1493// static
1494sp<AudioPolicyService::OpRecordAudioMonitor>
1495AudioPolicyService::OpRecordAudioMonitor::createIfNeeded(
1496 const AttributionSourceState& attributionSource, const audio_attributes_t& attr,
1497 wp<AudioCommandThread> commandThread)
1498{
Eric Laurent987ce102021-07-05 12:11:51 +02001499 if (isAudioServerOrRootUid(attributionSource.uid)) {
1500 ALOGV("not silencing record for audio or root source %s",
Eric Laurented726cc2021-07-01 14:26:41 +02001501 attributionSource.toString().c_str());
1502 return nullptr;
1503 }
1504
1505 if (!AudioPolicyService::isAppOpSource(attr.source)) {
1506 ALOGD("not monitoring app op for uid %d and source %d",
1507 attributionSource.uid, attr.source);
1508 return nullptr;
1509 }
1510
1511 if (!attributionSource.packageName.has_value()
1512 || attributionSource.packageName.value().size() == 0) {
1513 return nullptr;
1514 }
1515 return new OpRecordAudioMonitor(attributionSource, getOpForSource(attr.source), commandThread);
1516}
1517
1518AudioPolicyService::OpRecordAudioMonitor::OpRecordAudioMonitor(
1519 const AttributionSourceState& attributionSource, int32_t appOp,
1520 wp<AudioCommandThread> commandThread) :
1521 mHasOp(true), mAttributionSource(attributionSource), mAppOp(appOp),
1522 mCommandThread(commandThread)
1523{
1524}
1525
1526AudioPolicyService::OpRecordAudioMonitor::~OpRecordAudioMonitor()
1527{
1528 if (mOpCallback != 0) {
1529 mAppOpsManager.stopWatchingMode(mOpCallback);
1530 }
1531 mOpCallback.clear();
1532}
1533
1534void AudioPolicyService::OpRecordAudioMonitor::onFirstRef()
1535{
1536 checkOp();
1537 mOpCallback = new RecordAudioOpCallback(this);
1538 ALOGV("start watching op %d for %s", mAppOp, mAttributionSource.toString().c_str());
1539 // TODO: We need to always watch AppOpsManager::OP_RECORD_AUDIO too
1540 // since it controls the mic permission for legacy apps.
1541 mAppOpsManager.startWatchingMode(mAppOp, VALUE_OR_FATAL(aidl2legacy_string_view_String16(
1542 mAttributionSource.packageName.value_or(""))),
1543 mOpCallback);
1544}
1545
1546bool AudioPolicyService::OpRecordAudioMonitor::hasOp() const {
1547 return mHasOp.load();
1548}
1549
1550// Called by RecordAudioOpCallback when the app op corresponding to this OpRecordAudioMonitor
1551// is updated in AppOp callback and in onFirstRef()
1552// Note this method is never called (and never to be) for audio server / root track
1553// due to the UID in createIfNeeded(). As a result for those record track, it's:
1554// - not called from constructor,
1555// - not called from RecordAudioOpCallback because the callback is not installed in this case
1556void AudioPolicyService::OpRecordAudioMonitor::checkOp(bool updateUidStates)
1557{
1558 // TODO: We need to always check AppOpsManager::OP_RECORD_AUDIO too
1559 // since it controls the mic permission for legacy apps.
1560 const int32_t mode = mAppOpsManager.checkOp(mAppOp,
1561 mAttributionSource.uid, VALUE_OR_FATAL(aidl2legacy_string_view_String16(
1562 mAttributionSource.packageName.value_or(""))));
1563 const bool hasIt = (mode == AppOpsManager::MODE_ALLOWED);
1564 // verbose logging only log when appOp changed
1565 ALOGI_IF(hasIt != mHasOp.load(),
1566 "App op %d missing, %ssilencing record %s",
1567 mAppOp, hasIt ? "un" : "", mAttributionSource.toString().c_str());
1568 mHasOp.store(hasIt);
1569
1570 if (updateUidStates) {
1571 sp<AudioCommandThread> commandThread = mCommandThread.promote();
1572 if (commandThread != nullptr) {
1573 commandThread->updateUidStatesCommand();
1574 }
1575 }
1576}
1577
1578AudioPolicyService::OpRecordAudioMonitor::RecordAudioOpCallback::RecordAudioOpCallback(
1579 const wp<OpRecordAudioMonitor>& monitor) : mMonitor(monitor)
1580{ }
1581
1582void AudioPolicyService::OpRecordAudioMonitor::RecordAudioOpCallback::opChanged(int32_t op,
1583 const String16& packageName __unused) {
1584 sp<OpRecordAudioMonitor> monitor = mMonitor.promote();
1585 if (monitor != NULL) {
1586 if (op != monitor->getOp()) {
1587 return;
1588 }
1589 monitor->checkOp(true);
1590 }
1591}
1592
1593
Mathias Agopian65ab4712010-07-14 17:59:35 -07001594// ----------- AudioPolicyService::AudioCommandThread implementation ----------
1595
Eric Laurentbfb1b832013-01-07 09:53:42 -08001596AudioPolicyService::AudioCommandThread::AudioCommandThread(String8 name,
1597 const wp<AudioPolicyService>& service)
1598 : Thread(false), mName(name), mService(service)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001599{
Mathias Agopian65ab4712010-07-14 17:59:35 -07001600}
1601
1602
1603AudioPolicyService::AudioCommandThread::~AudioCommandThread()
1604{
Eric Laurentbfb1b832013-01-07 09:53:42 -08001605 if (!mAudioCommands.isEmpty()) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07001606 release_wake_lock(mName.string());
1607 }
1608 mAudioCommands.clear();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001609}
1610
1611void AudioPolicyService::AudioCommandThread::onFirstRef()
1612{
Eric Laurentbfb1b832013-01-07 09:53:42 -08001613 run(mName.string(), ANDROID_PRIORITY_AUDIO);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001614}
1615
1616bool AudioPolicyService::AudioCommandThread::threadLoop()
1617{
Eric Laurentd7eda8d2016-02-02 17:18:39 -08001618 nsecs_t waitTime = -1;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001619
1620 mLock.lock();
1621 while (!exitPending())
1622 {
Eric Laurent59a89232014-06-08 14:14:17 -07001623 sp<AudioPolicyService> svc;
1624 while (!mAudioCommands.isEmpty() && !exitPending()) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07001625 nsecs_t curTime = systemTime();
1626 // commands are sorted by increasing time stamp: execute them from index 0 and up
1627 if (mAudioCommands[0]->mTime <= curTime) {
Eric Laurent0ede8922014-05-09 18:04:42 -07001628 sp<AudioCommand> command = mAudioCommands[0];
Mathias Agopian65ab4712010-07-14 17:59:35 -07001629 mAudioCommands.removeAt(0);
Eric Laurent0ede8922014-05-09 18:04:42 -07001630 mLastCommand = command;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001631
1632 switch (command->mCommand) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07001633 case SET_VOLUME: {
Eric Laurent0ede8922014-05-09 18:04:42 -07001634 VolumeData *data = (VolumeData *)command->mParam.get();
Steve Block3856b092011-10-20 11:56:00 +01001635 ALOGV("AudioCommandThread() processing set volume stream %d, \
Eric Laurentde070132010-07-13 04:45:46 -07001636 volume %f, output %d", data->mStream, data->mVolume, data->mIO);
Andy Hungfe726a62018-09-27 15:17:25 -07001637 mLock.unlock();
Eric Laurentde070132010-07-13 04:45:46 -07001638 command->mStatus = AudioSystem::setStreamVolume(data->mStream,
1639 data->mVolume,
1640 data->mIO);
Andy Hungfe726a62018-09-27 15:17:25 -07001641 mLock.lock();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001642 }break;
1643 case SET_PARAMETERS: {
Eric Laurent0ede8922014-05-09 18:04:42 -07001644 ParametersData *data = (ParametersData *)command->mParam.get();
Glenn Kastene53b9ea2012-03-12 16:29:55 -07001645 ALOGV("AudioCommandThread() processing set parameters string %s, io %d",
1646 data->mKeyValuePairs.string(), data->mIO);
Andy Hungfe726a62018-09-27 15:17:25 -07001647 mLock.unlock();
Glenn Kastene53b9ea2012-03-12 16:29:55 -07001648 command->mStatus = AudioSystem::setParameters(data->mIO, data->mKeyValuePairs);
Andy Hungfe726a62018-09-27 15:17:25 -07001649 mLock.lock();
Glenn Kastene53b9ea2012-03-12 16:29:55 -07001650 }break;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001651 case SET_VOICE_VOLUME: {
Eric Laurent0ede8922014-05-09 18:04:42 -07001652 VoiceVolumeData *data = (VoiceVolumeData *)command->mParam.get();
Steve Block3856b092011-10-20 11:56:00 +01001653 ALOGV("AudioCommandThread() processing set voice volume volume %f",
Eric Laurentde070132010-07-13 04:45:46 -07001654 data->mVolume);
Andy Hungfe726a62018-09-27 15:17:25 -07001655 mLock.unlock();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001656 command->mStatus = AudioSystem::setVoiceVolume(data->mVolume);
Andy Hungfe726a62018-09-27 15:17:25 -07001657 mLock.lock();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001658 }break;
Eric Laurentbfb1b832013-01-07 09:53:42 -08001659 case STOP_OUTPUT: {
Eric Laurent0ede8922014-05-09 18:04:42 -07001660 StopOutputData *data = (StopOutputData *)command->mParam.get();
Eric Laurentd7fe0862018-07-14 16:48:01 -07001661 ALOGV("AudioCommandThread() processing stop output portId %d",
1662 data->mPortId);
Eric Laurent59a89232014-06-08 14:14:17 -07001663 svc = mService.promote();
Eric Laurentbfb1b832013-01-07 09:53:42 -08001664 if (svc == 0) {
1665 break;
1666 }
1667 mLock.unlock();
Eric Laurentd7fe0862018-07-14 16:48:01 -07001668 svc->doStopOutput(data->mPortId);
Eric Laurentbfb1b832013-01-07 09:53:42 -08001669 mLock.lock();
Eric Laurentbfb1b832013-01-07 09:53:42 -08001670 }break;
1671 case RELEASE_OUTPUT: {
Eric Laurent0ede8922014-05-09 18:04:42 -07001672 ReleaseOutputData *data = (ReleaseOutputData *)command->mParam.get();
Eric Laurentd7fe0862018-07-14 16:48:01 -07001673 ALOGV("AudioCommandThread() processing release output portId %d",
1674 data->mPortId);
Eric Laurent59a89232014-06-08 14:14:17 -07001675 svc = mService.promote();
Eric Laurentbfb1b832013-01-07 09:53:42 -08001676 if (svc == 0) {
1677 break;
1678 }
1679 mLock.unlock();
Eric Laurentd7fe0862018-07-14 16:48:01 -07001680 svc->doReleaseOutput(data->mPortId);
Eric Laurentbfb1b832013-01-07 09:53:42 -08001681 mLock.lock();
Eric Laurentbfb1b832013-01-07 09:53:42 -08001682 }break;
Eric Laurent951f4552014-05-20 10:48:17 -07001683 case CREATE_AUDIO_PATCH: {
1684 CreateAudioPatchData *data = (CreateAudioPatchData *)command->mParam.get();
1685 ALOGV("AudioCommandThread() processing create audio patch");
1686 sp<IAudioFlinger> af = AudioSystem::get_audio_flinger();
1687 if (af == 0) {
1688 command->mStatus = PERMISSION_DENIED;
1689 } else {
Andy Hungfe726a62018-09-27 15:17:25 -07001690 mLock.unlock();
Eric Laurent951f4552014-05-20 10:48:17 -07001691 command->mStatus = af->createAudioPatch(&data->mPatch, &data->mHandle);
Andy Hungfe726a62018-09-27 15:17:25 -07001692 mLock.lock();
Eric Laurent951f4552014-05-20 10:48:17 -07001693 }
1694 } break;
1695 case RELEASE_AUDIO_PATCH: {
1696 ReleaseAudioPatchData *data = (ReleaseAudioPatchData *)command->mParam.get();
1697 ALOGV("AudioCommandThread() processing release audio patch");
1698 sp<IAudioFlinger> af = AudioSystem::get_audio_flinger();
1699 if (af == 0) {
1700 command->mStatus = PERMISSION_DENIED;
1701 } else {
Andy Hungfe726a62018-09-27 15:17:25 -07001702 mLock.unlock();
Eric Laurent951f4552014-05-20 10:48:17 -07001703 command->mStatus = af->releaseAudioPatch(data->mHandle);
Andy Hungfe726a62018-09-27 15:17:25 -07001704 mLock.lock();
Eric Laurent951f4552014-05-20 10:48:17 -07001705 }
1706 } break;
Eric Laurentb52c1522014-05-20 11:27:36 -07001707 case UPDATE_AUDIOPORT_LIST: {
1708 ALOGV("AudioCommandThread() processing update audio port list");
Eric Laurent59a89232014-06-08 14:14:17 -07001709 svc = mService.promote();
Eric Laurentb52c1522014-05-20 11:27:36 -07001710 if (svc == 0) {
1711 break;
1712 }
1713 mLock.unlock();
1714 svc->doOnAudioPortListUpdate();
1715 mLock.lock();
1716 }break;
1717 case UPDATE_AUDIOPATCH_LIST: {
1718 ALOGV("AudioCommandThread() processing update audio patch 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->doOnAudioPatchListUpdate();
1725 mLock.lock();
1726 }break;
François Gaffiecfe17322018-11-07 13:41:29 +01001727 case CHANGED_AUDIOVOLUMEGROUP: {
1728 AudioVolumeGroupData *data =
1729 static_cast<AudioVolumeGroupData *>(command->mParam.get());
1730 ALOGV("AudioCommandThread() processing update audio volume group");
1731 svc = mService.promote();
1732 if (svc == 0) {
1733 break;
1734 }
1735 mLock.unlock();
1736 svc->doOnAudioVolumeGroupChanged(data->mGroup, data->mFlags);
1737 mLock.lock();
1738 }break;
Eric Laurente1715a42014-05-20 11:30:42 -07001739 case SET_AUDIOPORT_CONFIG: {
1740 SetAudioPortConfigData *data = (SetAudioPortConfigData *)command->mParam.get();
1741 ALOGV("AudioCommandThread() processing set port config");
1742 sp<IAudioFlinger> af = AudioSystem::get_audio_flinger();
1743 if (af == 0) {
1744 command->mStatus = PERMISSION_DENIED;
1745 } else {
Andy Hungfe726a62018-09-27 15:17:25 -07001746 mLock.unlock();
Eric Laurente1715a42014-05-20 11:30:42 -07001747 command->mStatus = af->setAudioPortConfig(&data->mConfig);
Andy Hungfe726a62018-09-27 15:17:25 -07001748 mLock.lock();
Eric Laurente1715a42014-05-20 11:30:42 -07001749 }
1750 } break;
Jean-Michel Trivide801052015-04-14 19:10:14 -07001751 case DYN_POLICY_MIX_STATE_UPDATE: {
1752 DynPolicyMixStateUpdateData *data =
1753 (DynPolicyMixStateUpdateData *)command->mParam.get();
Jean-Michel Trivide801052015-04-14 19:10:14 -07001754 ALOGV("AudioCommandThread() processing dyn policy mix state update %s %d",
1755 data->mRegId.string(), data->mState);
1756 svc = mService.promote();
1757 if (svc == 0) {
1758 break;
1759 }
1760 mLock.unlock();
1761 svc->doOnDynamicPolicyMixStateUpdate(data->mRegId, data->mState);
1762 mLock.lock();
1763 } break;
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -08001764 case RECORDING_CONFIGURATION_UPDATE: {
1765 RecordingConfigurationUpdateData *data =
1766 (RecordingConfigurationUpdateData *)command->mParam.get();
1767 ALOGV("AudioCommandThread() processing recording configuration update");
1768 svc = mService.promote();
1769 if (svc == 0) {
1770 break;
1771 }
1772 mLock.unlock();
Jean-Michel Triviac4e4292016-12-22 11:39:31 -08001773 svc->doOnRecordingConfigurationUpdate(data->mEvent, &data->mClientInfo,
Eric Laurenta9f86652018-11-28 17:23:11 -08001774 &data->mClientConfig, data->mClientEffects,
1775 &data->mDeviceConfig, data->mEffects,
1776 data->mPatchHandle, data->mSource);
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -08001777 mLock.lock();
1778 } break;
Eric Laurentb20cf7d2019-04-05 19:37:34 -07001779 case SET_EFFECT_SUSPENDED: {
1780 SetEffectSuspendedData *data = (SetEffectSuspendedData *)command->mParam.get();
1781 ALOGV("AudioCommandThread() processing set effect suspended");
1782 sp<IAudioFlinger> af = AudioSystem::get_audio_flinger();
1783 if (af != 0) {
1784 mLock.unlock();
1785 af->setEffectSuspended(data->mEffectId, data->mSessionId, data->mSuspended);
1786 mLock.lock();
1787 }
1788 } break;
Mikhail Naganov88b30d22020-03-09 19:43:13 +00001789 case AUDIO_MODULES_UPDATE: {
1790 ALOGV("AudioCommandThread() processing audio modules update");
1791 svc = mService.promote();
1792 if (svc == 0) {
1793 break;
1794 }
1795 mLock.unlock();
1796 svc->doOnNewAudioModulesAvailable();
1797 mLock.lock();
1798 } break;
Jean-Michel Trivi9a6b9ad2020-10-22 16:46:43 -07001799 case ROUTING_UPDATED: {
1800 ALOGV("AudioCommandThread() processing routing update");
1801 svc = mService.promote();
1802 if (svc == 0) {
1803 break;
1804 }
1805 mLock.unlock();
1806 svc->doOnRoutingUpdated();
1807 mLock.lock();
1808 } break;
Eric Laurentb20cf7d2019-04-05 19:37:34 -07001809
Eric Laurented726cc2021-07-01 14:26:41 +02001810 case UPDATE_UID_STATES: {
1811 ALOGV("AudioCommandThread() processing updateUID states");
1812 svc = mService.promote();
1813 if (svc == 0) {
1814 break;
1815 }
1816 mLock.unlock();
1817 svc->updateUidStates();
1818 mLock.lock();
1819 } break;
1820
Eric Laurent6d607012021-07-05 11:54:40 +02001821 case CHECK_SPATIALIZER: {
1822 ALOGV("AudioCommandThread() processing updateUID states");
1823 svc = mService.promote();
1824 if (svc == 0) {
1825 break;
1826 }
1827 mLock.unlock();
1828 svc->doOnCheckSpatializer();
1829 mLock.lock();
1830 } break;
1831
Mathias Agopian65ab4712010-07-14 17:59:35 -07001832 default:
Steve Block5ff1dd52012-01-05 23:22:43 +00001833 ALOGW("AudioCommandThread() unknown command %d", command->mCommand);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001834 }
Eric Laurent0ede8922014-05-09 18:04:42 -07001835 {
1836 Mutex::Autolock _l(command->mLock);
1837 if (command->mWaitStatus) {
1838 command->mWaitStatus = false;
1839 command->mCond.signal();
1840 }
1841 }
Eric Laurentd7eda8d2016-02-02 17:18:39 -08001842 waitTime = -1;
Zach Janga754b4f2015-10-27 01:29:34 +00001843 // release mLock before releasing strong reference on the service as
1844 // AudioPolicyService destructor calls AudioCommandThread::exit() which
1845 // acquires mLock.
1846 mLock.unlock();
1847 svc.clear();
1848 mLock.lock();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001849 } else {
1850 waitTime = mAudioCommands[0]->mTime - curTime;
1851 break;
1852 }
1853 }
Zach Janga754b4f2015-10-27 01:29:34 +00001854
1855 // release delayed commands wake lock if the queue is empty
1856 if (mAudioCommands.isEmpty()) {
Ricardo Garcia05f2fdc2014-07-24 15:48:24 -07001857 release_wake_lock(mName.string());
Zach Janga754b4f2015-10-27 01:29:34 +00001858 }
1859
1860 // At this stage we have either an empty command queue or the first command in the queue
1861 // has a finite delay. So unless we are exiting it is safe to wait.
1862 if (!exitPending()) {
Eric Laurent59a89232014-06-08 14:14:17 -07001863 ALOGV("AudioCommandThread() going to sleep");
Eric Laurentd7eda8d2016-02-02 17:18:39 -08001864 if (waitTime == -1) {
1865 mWaitWorkCV.wait(mLock);
1866 } else {
1867 mWaitWorkCV.waitRelative(mLock, waitTime);
1868 }
Eric Laurent59a89232014-06-08 14:14:17 -07001869 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001870 }
Ricardo Garcia05f2fdc2014-07-24 15:48:24 -07001871 // release delayed commands wake lock before quitting
1872 if (!mAudioCommands.isEmpty()) {
1873 release_wake_lock(mName.string());
1874 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001875 mLock.unlock();
1876 return false;
1877}
1878
1879status_t AudioPolicyService::AudioCommandThread::dump(int fd)
1880{
1881 const size_t SIZE = 256;
1882 char buffer[SIZE];
1883 String8 result;
1884
1885 snprintf(buffer, SIZE, "AudioCommandThread %p Dump\n", this);
1886 result.append(buffer);
1887 write(fd, result.string(), result.size());
1888
Mikhail Naganov12b716c2020-04-30 22:37:43 +00001889 const bool locked = dumpTryLock(mLock);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001890 if (!locked) {
1891 String8 result2(kCmdDeadlockedString);
1892 write(fd, result2.string(), result2.size());
1893 }
1894
1895 snprintf(buffer, SIZE, "- Commands:\n");
1896 result = String8(buffer);
1897 result.append(" Command Time Wait pParam\n");
Glenn Kasten8d6a2442012-02-08 14:04:28 -08001898 for (size_t i = 0; i < mAudioCommands.size(); i++) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07001899 mAudioCommands[i]->dump(buffer, SIZE);
1900 result.append(buffer);
1901 }
1902 result.append(" Last Command\n");
Eric Laurent0ede8922014-05-09 18:04:42 -07001903 if (mLastCommand != 0) {
1904 mLastCommand->dump(buffer, SIZE);
1905 result.append(buffer);
1906 } else {
1907 result.append(" none\n");
1908 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001909
1910 write(fd, result.string(), result.size());
1911
Mikhail Naganov12b716c2020-04-30 22:37:43 +00001912 dumpReleaseLock(mLock, locked);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001913
1914 return NO_ERROR;
1915}
1916
Glenn Kastenfff6d712012-01-12 16:38:12 -08001917status_t AudioPolicyService::AudioCommandThread::volumeCommand(audio_stream_type_t stream,
Eric Laurentde070132010-07-13 04:45:46 -07001918 float volume,
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001919 audio_io_handle_t output,
Eric Laurentde070132010-07-13 04:45:46 -07001920 int delayMs)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001921{
Eric Laurent0ede8922014-05-09 18:04:42 -07001922 sp<AudioCommand> command = new AudioCommand();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001923 command->mCommand = SET_VOLUME;
Eric Laurent0ede8922014-05-09 18:04:42 -07001924 sp<VolumeData> data = new VolumeData();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001925 data->mStream = stream;
1926 data->mVolume = volume;
1927 data->mIO = output;
1928 command->mParam = data;
Eric Laurent0ede8922014-05-09 18:04:42 -07001929 command->mWaitStatus = true;
Steve Block3856b092011-10-20 11:56:00 +01001930 ALOGV("AudioCommandThread() adding set volume stream %d, volume %f, output %d",
Eric Laurentde070132010-07-13 04:45:46 -07001931 stream, volume, output);
Eric Laurent0ede8922014-05-09 18:04:42 -07001932 return sendCommand(command, delayMs);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001933}
1934
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001935status_t AudioPolicyService::AudioCommandThread::parametersCommand(audio_io_handle_t ioHandle,
Dima Zavinfce7a472011-04-19 22:30:36 -07001936 const char *keyValuePairs,
Eric Laurentde070132010-07-13 04:45:46 -07001937 int delayMs)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001938{
Eric Laurent0ede8922014-05-09 18:04:42 -07001939 sp<AudioCommand> command = new AudioCommand();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001940 command->mCommand = SET_PARAMETERS;
Eric Laurent0ede8922014-05-09 18:04:42 -07001941 sp<ParametersData> data = new ParametersData();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001942 data->mIO = ioHandle;
Dima Zavinfce7a472011-04-19 22:30:36 -07001943 data->mKeyValuePairs = String8(keyValuePairs);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001944 command->mParam = data;
Eric Laurent0ede8922014-05-09 18:04:42 -07001945 command->mWaitStatus = true;
Steve Block3856b092011-10-20 11:56:00 +01001946 ALOGV("AudioCommandThread() adding set parameter string %s, io %d ,delay %d",
Dima Zavinfce7a472011-04-19 22:30:36 -07001947 keyValuePairs, ioHandle, delayMs);
Eric Laurent0ede8922014-05-09 18:04:42 -07001948 return sendCommand(command, delayMs);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001949}
1950
1951status_t AudioPolicyService::AudioCommandThread::voiceVolumeCommand(float volume, int delayMs)
1952{
Eric Laurent0ede8922014-05-09 18:04:42 -07001953 sp<AudioCommand> command = new AudioCommand();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001954 command->mCommand = SET_VOICE_VOLUME;
Eric Laurent0ede8922014-05-09 18:04:42 -07001955 sp<VoiceVolumeData> data = new VoiceVolumeData();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001956 data->mVolume = volume;
1957 command->mParam = data;
Eric Laurent0ede8922014-05-09 18:04:42 -07001958 command->mWaitStatus = true;
Steve Block3856b092011-10-20 11:56:00 +01001959 ALOGV("AudioCommandThread() adding set voice volume volume %f", volume);
Eric Laurent0ede8922014-05-09 18:04:42 -07001960 return sendCommand(command, delayMs);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001961}
1962
Eric Laurentb20cf7d2019-04-05 19:37:34 -07001963void AudioPolicyService::AudioCommandThread::setEffectSuspendedCommand(int effectId,
1964 audio_session_t sessionId,
1965 bool suspended)
1966{
1967 sp<AudioCommand> command = new AudioCommand();
1968 command->mCommand = SET_EFFECT_SUSPENDED;
1969 sp<SetEffectSuspendedData> data = new SetEffectSuspendedData();
1970 data->mEffectId = effectId;
1971 data->mSessionId = sessionId;
1972 data->mSuspended = suspended;
1973 command->mParam = data;
1974 ALOGV("AudioCommandThread() adding set suspended effectId %d sessionId %d suspended %d",
1975 effectId, sessionId, suspended);
1976 sendCommand(command);
1977}
1978
1979
Eric Laurentd7fe0862018-07-14 16:48:01 -07001980void AudioPolicyService::AudioCommandThread::stopOutputCommand(audio_port_handle_t portId)
Eric Laurentbfb1b832013-01-07 09:53:42 -08001981{
Eric Laurent0ede8922014-05-09 18:04:42 -07001982 sp<AudioCommand> command = new AudioCommand();
Eric Laurentbfb1b832013-01-07 09:53:42 -08001983 command->mCommand = STOP_OUTPUT;
Eric Laurent0ede8922014-05-09 18:04:42 -07001984 sp<StopOutputData> data = new StopOutputData();
Eric Laurentd7fe0862018-07-14 16:48:01 -07001985 data->mPortId = portId;
Jesper Tragardh48412dc2014-03-24 14:12:43 +01001986 command->mParam = data;
Eric Laurentd7fe0862018-07-14 16:48:01 -07001987 ALOGV("AudioCommandThread() adding stop output portId %d", portId);
Eric Laurent0ede8922014-05-09 18:04:42 -07001988 sendCommand(command);
Eric Laurentbfb1b832013-01-07 09:53:42 -08001989}
1990
Eric Laurentd7fe0862018-07-14 16:48:01 -07001991void AudioPolicyService::AudioCommandThread::releaseOutputCommand(audio_port_handle_t portId)
Eric Laurentbfb1b832013-01-07 09:53:42 -08001992{
Eric Laurent0ede8922014-05-09 18:04:42 -07001993 sp<AudioCommand> command = new AudioCommand();
Eric Laurentbfb1b832013-01-07 09:53:42 -08001994 command->mCommand = RELEASE_OUTPUT;
Eric Laurent0ede8922014-05-09 18:04:42 -07001995 sp<ReleaseOutputData> data = new ReleaseOutputData();
Eric Laurentd7fe0862018-07-14 16:48:01 -07001996 data->mPortId = portId;
Jesper Tragardh48412dc2014-03-24 14:12:43 +01001997 command->mParam = data;
Eric Laurentd7fe0862018-07-14 16:48:01 -07001998 ALOGV("AudioCommandThread() adding release output portId %d", portId);
Eric Laurent0ede8922014-05-09 18:04:42 -07001999 sendCommand(command);
2000}
2001
Eric Laurent951f4552014-05-20 10:48:17 -07002002status_t AudioPolicyService::AudioCommandThread::createAudioPatchCommand(
2003 const struct audio_patch *patch,
2004 audio_patch_handle_t *handle,
2005 int delayMs)
2006{
2007 status_t status = NO_ERROR;
2008
2009 sp<AudioCommand> command = new AudioCommand();
2010 command->mCommand = CREATE_AUDIO_PATCH;
2011 CreateAudioPatchData *data = new CreateAudioPatchData();
2012 data->mPatch = *patch;
2013 data->mHandle = *handle;
2014 command->mParam = data;
2015 command->mWaitStatus = true;
2016 ALOGV("AudioCommandThread() adding create patch delay %d", delayMs);
2017 status = sendCommand(command, delayMs);
2018 if (status == NO_ERROR) {
2019 *handle = data->mHandle;
2020 }
2021 return status;
2022}
2023
2024status_t AudioPolicyService::AudioCommandThread::releaseAudioPatchCommand(audio_patch_handle_t handle,
2025 int delayMs)
2026{
2027 sp<AudioCommand> command = new AudioCommand();
2028 command->mCommand = RELEASE_AUDIO_PATCH;
2029 ReleaseAudioPatchData *data = new ReleaseAudioPatchData();
2030 data->mHandle = handle;
2031 command->mParam = data;
2032 command->mWaitStatus = true;
2033 ALOGV("AudioCommandThread() adding release patch delay %d", delayMs);
2034 return sendCommand(command, delayMs);
2035}
2036
Eric Laurentb52c1522014-05-20 11:27:36 -07002037void AudioPolicyService::AudioCommandThread::updateAudioPortListCommand()
2038{
2039 sp<AudioCommand> command = new AudioCommand();
2040 command->mCommand = UPDATE_AUDIOPORT_LIST;
2041 ALOGV("AudioCommandThread() adding update audio port list");
2042 sendCommand(command);
2043}
2044
Eric Laurented726cc2021-07-01 14:26:41 +02002045void AudioPolicyService::AudioCommandThread::updateUidStatesCommand()
2046{
2047 sp<AudioCommand> command = new AudioCommand();
2048 command->mCommand = UPDATE_UID_STATES;
2049 ALOGV("AudioCommandThread() adding update UID states");
2050 sendCommand(command);
2051}
2052
Eric Laurentb52c1522014-05-20 11:27:36 -07002053void AudioPolicyService::AudioCommandThread::updateAudioPatchListCommand()
2054{
2055 sp<AudioCommand>command = new AudioCommand();
2056 command->mCommand = UPDATE_AUDIOPATCH_LIST;
2057 ALOGV("AudioCommandThread() adding update audio patch list");
2058 sendCommand(command);
2059}
2060
François Gaffiecfe17322018-11-07 13:41:29 +01002061void AudioPolicyService::AudioCommandThread::changeAudioVolumeGroupCommand(volume_group_t group,
2062 int flags)
2063{
2064 sp<AudioCommand>command = new AudioCommand();
2065 command->mCommand = CHANGED_AUDIOVOLUMEGROUP;
2066 AudioVolumeGroupData *data= new AudioVolumeGroupData();
2067 data->mGroup = group;
2068 data->mFlags = flags;
2069 command->mParam = data;
2070 ALOGV("AudioCommandThread() adding audio volume group changed");
2071 sendCommand(command);
2072}
2073
Eric Laurente1715a42014-05-20 11:30:42 -07002074status_t AudioPolicyService::AudioCommandThread::setAudioPortConfigCommand(
2075 const struct audio_port_config *config, int delayMs)
2076{
2077 sp<AudioCommand> command = new AudioCommand();
2078 command->mCommand = SET_AUDIOPORT_CONFIG;
2079 SetAudioPortConfigData *data = new SetAudioPortConfigData();
2080 data->mConfig = *config;
2081 command->mParam = data;
2082 command->mWaitStatus = true;
2083 ALOGV("AudioCommandThread() adding set port config delay %d", delayMs);
2084 return sendCommand(command, delayMs);
2085}
2086
Jean-Michel Trivide801052015-04-14 19:10:14 -07002087void AudioPolicyService::AudioCommandThread::dynamicPolicyMixStateUpdateCommand(
Chih-Hung Hsiehe964d4e2016-08-09 14:31:32 -07002088 const String8& regId, int32_t state)
Jean-Michel Trivide801052015-04-14 19:10:14 -07002089{
2090 sp<AudioCommand> command = new AudioCommand();
2091 command->mCommand = DYN_POLICY_MIX_STATE_UPDATE;
2092 DynPolicyMixStateUpdateData *data = new DynPolicyMixStateUpdateData();
2093 data->mRegId = regId;
2094 data->mState = state;
2095 command->mParam = data;
2096 ALOGV("AudioCommandThread() sending dynamic policy mix (id=%s) state update to %d",
2097 regId.string(), state);
2098 sendCommand(command);
2099}
2100
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -08002101void AudioPolicyService::AudioCommandThread::recordingConfigurationUpdateCommand(
Eric Laurenta9f86652018-11-28 17:23:11 -08002102 int event,
2103 const record_client_info_t *clientInfo,
2104 const audio_config_base_t *clientConfig,
2105 std::vector<effect_descriptor_t> clientEffects,
2106 const audio_config_base_t *deviceConfig,
2107 std::vector<effect_descriptor_t> effects,
2108 audio_patch_handle_t patchHandle,
2109 audio_source_t source)
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -08002110{
2111 sp<AudioCommand>command = new AudioCommand();
2112 command->mCommand = RECORDING_CONFIGURATION_UPDATE;
2113 RecordingConfigurationUpdateData *data = new RecordingConfigurationUpdateData();
2114 data->mEvent = event;
Jean-Michel Triviac4e4292016-12-22 11:39:31 -08002115 data->mClientInfo = *clientInfo;
Jean-Michel Trivi7281aa92016-02-17 15:33:40 -08002116 data->mClientConfig = *clientConfig;
Eric Laurenta9f86652018-11-28 17:23:11 -08002117 data->mClientEffects = clientEffects;
Jean-Michel Trivi7281aa92016-02-17 15:33:40 -08002118 data->mDeviceConfig = *deviceConfig;
Eric Laurenta9f86652018-11-28 17:23:11 -08002119 data->mEffects = effects;
Jean-Michel Trivi8c7cf3b2016-02-25 17:08:24 -08002120 data->mPatchHandle = patchHandle;
Eric Laurenta9f86652018-11-28 17:23:11 -08002121 data->mSource = source;
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -08002122 command->mParam = data;
Jean-Michel Triviac4e4292016-12-22 11:39:31 -08002123 ALOGV("AudioCommandThread() adding recording configuration update event %d, source %d uid %u",
2124 event, clientInfo->source, clientInfo->uid);
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -08002125 sendCommand(command);
2126}
2127
Mikhail Naganov88b30d22020-03-09 19:43:13 +00002128void AudioPolicyService::AudioCommandThread::audioModulesUpdateCommand()
2129{
2130 sp<AudioCommand> command = new AudioCommand();
2131 command->mCommand = AUDIO_MODULES_UPDATE;
2132 sendCommand(command);
2133}
2134
Jean-Michel Trivi9a6b9ad2020-10-22 16:46:43 -07002135void AudioPolicyService::AudioCommandThread::routingChangedCommand()
2136{
2137 sp<AudioCommand>command = new AudioCommand();
2138 command->mCommand = ROUTING_UPDATED;
2139 ALOGV("AudioCommandThread() adding routing update");
2140 sendCommand(command);
2141}
2142
Eric Laurent6d607012021-07-05 11:54:40 +02002143void AudioPolicyService::AudioCommandThread::checkSpatializerCommand()
2144{
2145 sp<AudioCommand>command = new AudioCommand();
2146 command->mCommand = CHECK_SPATIALIZER;
2147 ALOGV("AudioCommandThread() adding check spatializer");
2148 sendCommand(command);
2149}
2150
Eric Laurent0ede8922014-05-09 18:04:42 -07002151status_t AudioPolicyService::AudioCommandThread::sendCommand(sp<AudioCommand>& command, int delayMs)
2152{
2153 {
2154 Mutex::Autolock _l(mLock);
2155 insertCommand_l(command, delayMs);
2156 mWaitWorkCV.signal();
2157 }
2158 Mutex::Autolock _l(command->mLock);
2159 while (command->mWaitStatus) {
2160 nsecs_t timeOutNs = kAudioCommandTimeoutNs + milliseconds(delayMs);
2161 if (command->mCond.waitRelative(command->mLock, timeOutNs) != NO_ERROR) {
2162 command->mStatus = TIMED_OUT;
2163 command->mWaitStatus = false;
2164 }
2165 }
2166 return command->mStatus;
Eric Laurentbfb1b832013-01-07 09:53:42 -08002167}
2168
Mathias Agopian65ab4712010-07-14 17:59:35 -07002169// insertCommand_l() must be called with mLock held
Eric Laurent0ede8922014-05-09 18:04:42 -07002170void AudioPolicyService::AudioCommandThread::insertCommand_l(sp<AudioCommand>& command, int delayMs)
Mathias Agopian65ab4712010-07-14 17:59:35 -07002171{
Glenn Kasten8d6a2442012-02-08 14:04:28 -08002172 ssize_t i; // not size_t because i will count down to -1
Eric Laurent0ede8922014-05-09 18:04:42 -07002173 Vector < sp<AudioCommand> > removedCommands;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002174 command->mTime = systemTime() + milliseconds(delayMs);
2175
2176 // acquire wake lock to make sure delayed commands are processed
Eric Laurentbfb1b832013-01-07 09:53:42 -08002177 if (mAudioCommands.isEmpty()) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07002178 acquire_wake_lock(PARTIAL_WAKE_LOCK, mName.string());
2179 }
2180
2181 // check same pending commands with later time stamps and eliminate them
Ivan Lozano5ff158f2017-10-30 09:06:24 -07002182 for (i = (ssize_t)mAudioCommands.size()-1; i >= 0; i--) {
Eric Laurent0ede8922014-05-09 18:04:42 -07002183 sp<AudioCommand> command2 = mAudioCommands[i];
Mathias Agopian65ab4712010-07-14 17:59:35 -07002184 // commands are sorted by increasing time stamp: no need to scan the rest of mAudioCommands
2185 if (command2->mTime <= command->mTime) break;
Eric Laurente45b48a2014-09-04 16:40:57 -07002186
2187 // create audio patch or release audio patch commands are equivalent
2188 // with regard to filtering
2189 if ((command->mCommand == CREATE_AUDIO_PATCH) ||
2190 (command->mCommand == RELEASE_AUDIO_PATCH)) {
2191 if ((command2->mCommand != CREATE_AUDIO_PATCH) &&
2192 (command2->mCommand != RELEASE_AUDIO_PATCH)) {
2193 continue;
2194 }
2195 } else if (command2->mCommand != command->mCommand) continue;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002196
2197 switch (command->mCommand) {
2198 case SET_PARAMETERS: {
Eric Laurent0ede8922014-05-09 18:04:42 -07002199 ParametersData *data = (ParametersData *)command->mParam.get();
2200 ParametersData *data2 = (ParametersData *)command2->mParam.get();
Mathias Agopian65ab4712010-07-14 17:59:35 -07002201 if (data->mIO != data2->mIO) break;
Steve Block3856b092011-10-20 11:56:00 +01002202 ALOGV("Comparing parameter command %s to new command %s",
Eric Laurentde070132010-07-13 04:45:46 -07002203 data2->mKeyValuePairs.string(), data->mKeyValuePairs.string());
Mathias Agopian65ab4712010-07-14 17:59:35 -07002204 AudioParameter param = AudioParameter(data->mKeyValuePairs);
2205 AudioParameter param2 = AudioParameter(data2->mKeyValuePairs);
2206 for (size_t j = 0; j < param.size(); j++) {
Glenn Kastene53b9ea2012-03-12 16:29:55 -07002207 String8 key;
2208 String8 value;
2209 param.getAt(j, key, value);
2210 for (size_t k = 0; k < param2.size(); k++) {
2211 String8 key2;
2212 String8 value2;
2213 param2.getAt(k, key2, value2);
2214 if (key2 == key) {
2215 param2.remove(key2);
2216 ALOGV("Filtering out parameter %s", key2.string());
2217 break;
2218 }
2219 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07002220 }
2221 // if all keys have been filtered out, remove the command.
2222 // otherwise, update the key value pairs
2223 if (param2.size() == 0) {
2224 removedCommands.add(command2);
2225 } else {
2226 data2->mKeyValuePairs = param2.toString();
2227 }
Eric Laurent21e54562013-09-23 12:08:05 -07002228 command->mTime = command2->mTime;
2229 // force delayMs to non 0 so that code below does not request to wait for
2230 // command status as the command is now delayed
2231 delayMs = 1;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002232 } break;
2233
2234 case SET_VOLUME: {
Eric Laurent0ede8922014-05-09 18:04:42 -07002235 VolumeData *data = (VolumeData *)command->mParam.get();
2236 VolumeData *data2 = (VolumeData *)command2->mParam.get();
Mathias Agopian65ab4712010-07-14 17:59:35 -07002237 if (data->mIO != data2->mIO) break;
2238 if (data->mStream != data2->mStream) break;
Steve Block3856b092011-10-20 11:56:00 +01002239 ALOGV("Filtering out volume command on output %d for stream %d",
Eric Laurentde070132010-07-13 04:45:46 -07002240 data->mIO, data->mStream);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002241 removedCommands.add(command2);
Eric Laurent21e54562013-09-23 12:08:05 -07002242 command->mTime = command2->mTime;
2243 // force delayMs to non 0 so that code below does not request to wait for
2244 // command status as the command is now delayed
2245 delayMs = 1;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002246 } break;
Eric Laurente45b48a2014-09-04 16:40:57 -07002247
Eric Laurentbaf35fe2016-07-27 15:36:53 -07002248 case SET_VOICE_VOLUME: {
2249 VoiceVolumeData *data = (VoiceVolumeData *)command->mParam.get();
2250 VoiceVolumeData *data2 = (VoiceVolumeData *)command2->mParam.get();
2251 ALOGV("Filtering out voice volume command value %f replaced by %f",
2252 data2->mVolume, data->mVolume);
2253 removedCommands.add(command2);
2254 command->mTime = command2->mTime;
2255 // force delayMs to non 0 so that code below does not request to wait for
2256 // command status as the command is now delayed
2257 delayMs = 1;
2258 } break;
2259
Eric Laurente45b48a2014-09-04 16:40:57 -07002260 case CREATE_AUDIO_PATCH:
2261 case RELEASE_AUDIO_PATCH: {
2262 audio_patch_handle_t handle;
Haynes Mathew Georgea2d4a6d2014-10-13 13:05:22 -07002263 struct audio_patch patch;
Eric Laurente45b48a2014-09-04 16:40:57 -07002264 if (command->mCommand == CREATE_AUDIO_PATCH) {
2265 handle = ((CreateAudioPatchData *)command->mParam.get())->mHandle;
Haynes Mathew Georgea2d4a6d2014-10-13 13:05:22 -07002266 patch = ((CreateAudioPatchData *)command->mParam.get())->mPatch;
Eric Laurente45b48a2014-09-04 16:40:57 -07002267 } else {
2268 handle = ((ReleaseAudioPatchData *)command->mParam.get())->mHandle;
Mikhail Naganov7be71d22018-05-23 16:51:46 -07002269 memset(&patch, 0, sizeof(patch));
Eric Laurente45b48a2014-09-04 16:40:57 -07002270 }
2271 audio_patch_handle_t handle2;
Haynes Mathew Georgea2d4a6d2014-10-13 13:05:22 -07002272 struct audio_patch patch2;
Eric Laurente45b48a2014-09-04 16:40:57 -07002273 if (command2->mCommand == CREATE_AUDIO_PATCH) {
2274 handle2 = ((CreateAudioPatchData *)command2->mParam.get())->mHandle;
Haynes Mathew Georgea2d4a6d2014-10-13 13:05:22 -07002275 patch2 = ((CreateAudioPatchData *)command2->mParam.get())->mPatch;
Eric Laurente45b48a2014-09-04 16:40:57 -07002276 } else {
2277 handle2 = ((ReleaseAudioPatchData *)command2->mParam.get())->mHandle;
Glenn Kastenf60b6b62015-07-06 10:53:26 -07002278 memset(&patch2, 0, sizeof(patch2));
Eric Laurente45b48a2014-09-04 16:40:57 -07002279 }
2280 if (handle != handle2) break;
Haynes Mathew Georgea2d4a6d2014-10-13 13:05:22 -07002281 /* Filter CREATE_AUDIO_PATCH commands only when they are issued for
2282 same output. */
2283 if( (command->mCommand == CREATE_AUDIO_PATCH) &&
2284 (command2->mCommand == CREATE_AUDIO_PATCH) ) {
2285 bool isOutputDiff = false;
2286 if (patch.num_sources == patch2.num_sources) {
2287 for (unsigned count = 0; count < patch.num_sources; count++) {
2288 if (patch.sources[count].id != patch2.sources[count].id) {
2289 isOutputDiff = true;
2290 break;
2291 }
2292 }
2293 if (isOutputDiff)
2294 break;
2295 }
2296 }
Eric Laurente45b48a2014-09-04 16:40:57 -07002297 ALOGV("Filtering out %s audio patch command for handle %d",
2298 (command->mCommand == CREATE_AUDIO_PATCH) ? "create" : "release", handle);
2299 removedCommands.add(command2);
2300 command->mTime = command2->mTime;
2301 // force delayMs to non 0 so that code below does not request to wait for
2302 // command status as the command is now delayed
2303 delayMs = 1;
2304 } break;
2305
Jean-Michel Trivide801052015-04-14 19:10:14 -07002306 case DYN_POLICY_MIX_STATE_UPDATE: {
2307
2308 } break;
2309
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -08002310 case RECORDING_CONFIGURATION_UPDATE: {
2311
2312 } break;
2313
Jean-Michel Trivi9a6b9ad2020-10-22 16:46:43 -07002314 case ROUTING_UPDATED: {
2315
2316 } break;
2317
Mathias Agopian65ab4712010-07-14 17:59:35 -07002318 default:
2319 break;
2320 }
2321 }
2322
2323 // remove filtered commands
2324 for (size_t j = 0; j < removedCommands.size(); j++) {
2325 // removed commands always have time stamps greater than current command
2326 for (size_t k = i + 1; k < mAudioCommands.size(); k++) {
Eric Laurent0ede8922014-05-09 18:04:42 -07002327 if (mAudioCommands[k].get() == removedCommands[j].get()) {
Steve Block3856b092011-10-20 11:56:00 +01002328 ALOGV("suppressing command: %d", mAudioCommands[k]->mCommand);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002329 mAudioCommands.removeAt(k);
2330 break;
2331 }
2332 }
2333 }
2334 removedCommands.clear();
2335
Eric Laurentaa79bef2015-01-15 14:33:51 -08002336 // Disable wait for status if delay is not 0.
2337 // Except for create audio patch command because the returned patch handle
2338 // is needed by audio policy manager
2339 if (delayMs != 0 && command->mCommand != CREATE_AUDIO_PATCH) {
Eric Laurentcec4abb2012-07-03 12:23:02 -07002340 command->mWaitStatus = false;
2341 }
Eric Laurentcec4abb2012-07-03 12:23:02 -07002342
Mathias Agopian65ab4712010-07-14 17:59:35 -07002343 // insert command at the right place according to its time stamp
Eric Laurent1e693b52014-07-09 15:03:28 -07002344 ALOGV("inserting command: %d at index %zd, num commands %zu",
2345 command->mCommand, i+1, mAudioCommands.size());
Mathias Agopian65ab4712010-07-14 17:59:35 -07002346 mAudioCommands.insertAt(command, i + 1);
2347}
2348
2349void AudioPolicyService::AudioCommandThread::exit()
2350{
Steve Block3856b092011-10-20 11:56:00 +01002351 ALOGV("AudioCommandThread::exit");
Mathias Agopian65ab4712010-07-14 17:59:35 -07002352 {
2353 AutoMutex _l(mLock);
2354 requestExit();
2355 mWaitWorkCV.signal();
2356 }
Zach Janga754b4f2015-10-27 01:29:34 +00002357 // Note that we can call it from the thread loop if all other references have been released
2358 // but it will safely return WOULD_BLOCK in this case
Mathias Agopian65ab4712010-07-14 17:59:35 -07002359 requestExitAndWait();
2360}
2361
2362void AudioPolicyService::AudioCommandThread::AudioCommand::dump(char* buffer, size_t size)
2363{
2364 snprintf(buffer, size, " %02d %06d.%03d %01u %p\n",
2365 mCommand,
2366 (int)ns2s(mTime),
2367 (int)ns2ms(mTime)%1000,
2368 mWaitStatus,
Eric Laurent0ede8922014-05-09 18:04:42 -07002369 mParam.get());
Mathias Agopian65ab4712010-07-14 17:59:35 -07002370}
2371
Dima Zavinfce7a472011-04-19 22:30:36 -07002372/******* helpers for the service_ops callbacks defined below *********/
2373void AudioPolicyService::setParameters(audio_io_handle_t ioHandle,
2374 const char *keyValuePairs,
2375 int delayMs)
2376{
Glenn Kasten72ef00d2012-01-17 11:09:42 -08002377 mAudioCommandThread->parametersCommand(ioHandle, keyValuePairs,
Dima Zavinfce7a472011-04-19 22:30:36 -07002378 delayMs);
2379}
2380
2381int AudioPolicyService::setStreamVolume(audio_stream_type_t stream,
2382 float volume,
2383 audio_io_handle_t output,
2384 int delayMs)
2385{
Glenn Kastenfff6d712012-01-12 16:38:12 -08002386 return (int)mAudioCommandThread->volumeCommand(stream, volume,
Glenn Kasten72ef00d2012-01-17 11:09:42 -08002387 output, delayMs);
Dima Zavinfce7a472011-04-19 22:30:36 -07002388}
2389
Dima Zavinfce7a472011-04-19 22:30:36 -07002390int AudioPolicyService::setVoiceVolume(float volume, int delayMs)
2391{
2392 return (int)mAudioCommandThread->voiceVolumeCommand(volume, delayMs);
2393}
2394
Eric Laurentb20cf7d2019-04-05 19:37:34 -07002395void AudioPolicyService::setEffectSuspended(int effectId,
2396 audio_session_t sessionId,
2397 bool suspended)
2398{
2399 mAudioCommandThread->setEffectSuspendedCommand(effectId, sessionId, suspended);
2400}
2401
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002402Status AudioPolicyService::onNewAudioModulesAvailable()
Mikhail Naganov88b30d22020-03-09 19:43:13 +00002403{
Mikhail Naganovd0e2c742020-03-25 15:59:59 -07002404 mOutputCommandThread->audioModulesUpdateCommand();
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002405 return Status::ok();
Mikhail Naganov88b30d22020-03-09 19:43:13 +00002406}
2407
Eric Laurentb20cf7d2019-04-05 19:37:34 -07002408
Dima Zavinfce7a472011-04-19 22:30:36 -07002409extern "C" {
Eric Laurent2d388ec2014-03-07 13:25:54 -08002410audio_module_handle_t aps_load_hw_module(void *service __unused,
2411 const char *name);
2412audio_io_handle_t aps_open_output(void *service __unused,
Eric Laurenta4c5a552012-03-29 10:12:40 -07002413 audio_devices_t *pDevices,
2414 uint32_t *pSamplingRate,
2415 audio_format_t *pFormat,
2416 audio_channel_mask_t *pChannelMask,
2417 uint32_t *pLatencyMs,
Eric Laurent2d388ec2014-03-07 13:25:54 -08002418 audio_output_flags_t flags);
Eric Laurenta4c5a552012-03-29 10:12:40 -07002419
Eric Laurent2d388ec2014-03-07 13:25:54 -08002420audio_io_handle_t aps_open_output_on_module(void *service __unused,
Eric Laurenta4c5a552012-03-29 10:12:40 -07002421 audio_module_handle_t module,
2422 audio_devices_t *pDevices,
2423 uint32_t *pSamplingRate,
2424 audio_format_t *pFormat,
2425 audio_channel_mask_t *pChannelMask,
2426 uint32_t *pLatencyMs,
Richard Fitzgeraldad3af332013-03-25 16:54:37 +00002427 audio_output_flags_t flags,
Eric Laurent2d388ec2014-03-07 13:25:54 -08002428 const audio_offload_info_t *offloadInfo);
2429audio_io_handle_t aps_open_dup_output(void *service __unused,
Dima Zavinfce7a472011-04-19 22:30:36 -07002430 audio_io_handle_t output1,
Eric Laurent2d388ec2014-03-07 13:25:54 -08002431 audio_io_handle_t output2);
2432int aps_close_output(void *service __unused, audio_io_handle_t output);
2433int aps_suspend_output(void *service __unused, audio_io_handle_t output);
2434int aps_restore_output(void *service __unused, audio_io_handle_t output);
2435audio_io_handle_t aps_open_input(void *service __unused,
Eric Laurenta4c5a552012-03-29 10:12:40 -07002436 audio_devices_t *pDevices,
2437 uint32_t *pSamplingRate,
2438 audio_format_t *pFormat,
2439 audio_channel_mask_t *pChannelMask,
Eric Laurent2d388ec2014-03-07 13:25:54 -08002440 audio_in_acoustics_t acoustics __unused);
2441audio_io_handle_t aps_open_input_on_module(void *service __unused,
Eric Laurenta4c5a552012-03-29 10:12:40 -07002442 audio_module_handle_t module,
2443 audio_devices_t *pDevices,
2444 uint32_t *pSamplingRate,
2445 audio_format_t *pFormat,
Eric Laurent2d388ec2014-03-07 13:25:54 -08002446 audio_channel_mask_t *pChannelMask);
2447int aps_close_input(void *service __unused, audio_io_handle_t input);
2448int aps_invalidate_stream(void *service __unused, audio_stream_type_t stream);
Glenn Kastend848eb42016-03-08 13:42:11 -08002449int aps_move_effects(void *service __unused, audio_session_t session,
Dima Zavinfce7a472011-04-19 22:30:36 -07002450 audio_io_handle_t src_output,
Eric Laurent2d388ec2014-03-07 13:25:54 -08002451 audio_io_handle_t dst_output);
2452char * aps_get_parameters(void *service __unused, audio_io_handle_t io_handle,
2453 const char *keys);
2454void aps_set_parameters(void *service, audio_io_handle_t io_handle,
2455 const char *kv_pairs, int delay_ms);
2456int aps_set_stream_volume(void *service, audio_stream_type_t stream,
Dima Zavinfce7a472011-04-19 22:30:36 -07002457 float volume, audio_io_handle_t output,
Eric Laurent2d388ec2014-03-07 13:25:54 -08002458 int delay_ms);
Eric Laurent2d388ec2014-03-07 13:25:54 -08002459int aps_set_voice_volume(void *service, float volume, int delay_ms);
2460};
Dima Zavinfce7a472011-04-19 22:30:36 -07002461
Mikhail Naganov1b2a7942017-12-08 10:18:09 -08002462} // namespace android