blob: 0b4a05968d7ca4bf94472699148f50d8131e964e [file] [log] [blame]
Mathias Agopian65ab4712010-07-14 17:59:35 -07001/*
2 * Copyright (C) 2009 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#define LOG_TAG "AudioPolicyService"
18//#define LOG_NDEBUG 0
19
Glenn Kasten153b9fe2013-07-15 11:23:36 -070020#include "Configuration.h"
Mathias Agopian65ab4712010-07-14 17:59:35 -070021#undef __STRICT_ANSI__
22#define __STDINT_LIMITS
23#define __STDC_LIMIT_MACROS
24#include <stdint.h>
Mathias Agopian65ab4712010-07-14 17:59:35 -070025#include <sys/time.h>
Jaideep Sharmaba9053b2021-01-25 21:24:26 +053026#include <dlfcn.h>
Mikhail Naganov959e2d02019-03-28 11:08:19 -070027
28#include <audio_utils/clock.h>
Mathias Agopian65ab4712010-07-14 17:59:35 -070029#include <binder/IServiceManager.h>
30#include <utils/Log.h>
31#include <cutils/properties.h>
32#include <binder/IPCThreadState.h>
Svet Ganovf4ddfef2018-01-16 07:37:58 -080033#include <binder/PermissionController.h>
34#include <binder/IResultReceiver.h>
Mathias Agopian65ab4712010-07-14 17:59:35 -070035#include <utils/String16.h>
36#include <utils/threads.h>
37#include "AudioPolicyService.h"
Mathias Agopian65ab4712010-07-14 17:59:35 -070038#include <hardware_legacy/power.h>
Ytai Ben-Tsvi7e7a79d2020-12-15 16:48:16 -080039#include <media/AidlConversion.h>
Eric Laurent7c7f10b2011-06-17 21:29:58 -070040#include <media/AudioEffect.h>
Chih-Hung Hsiehc84d9d22014-11-14 13:33:34 -080041#include <media/AudioParameter.h>
Andy Hungab7ef302018-05-15 19:35:29 -070042#include <mediautils/ServiceUtilities.h>
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -080043#include <mediautils/TimeCheck.h>
Michael Groovercfd28302018-12-11 19:16:46 -080044#include <sensorprivacy/SensorPrivacyManager.h>
Mathias Agopian65ab4712010-07-14 17:59:35 -070045
Dima Zavin64760242011-05-11 14:15:23 -070046#include <system/audio.h>
Dima Zavin7394a4f2011-06-13 18:16:26 -070047#include <system/audio_policy.h>
Jaideep Sharmaba9053b2021-01-25 21:24:26 +053048#include <AudioPolicyManager.h>
Mikhail Naganov61a4fac2016-10-13 14:44:18 -070049
Mathias Agopian65ab4712010-07-14 17:59:35 -070050namespace android {
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -080051using binder::Status;
Mathias Agopian65ab4712010-07-14 17:59:35 -070052
Glenn Kasten8dad0e32012-01-09 08:41:22 -080053static const char kDeadlockedString[] = "AudioPolicyService may be deadlocked\n";
54static const char kCmdDeadlockedString[] = "AudioPolicyService command thread may be deadlocked\n";
Jaideep Sharmaba9053b2021-01-25 21:24:26 +053055static const char kAudioPolicyManagerCustomPath[] = "libaudiopolicymanagercustom.so";
Mathias Agopian65ab4712010-07-14 17:59:35 -070056
Mikhail Naganov959e2d02019-03-28 11:08:19 -070057static const int kDumpLockTimeoutNs = 1 * NANOS_PER_SECOND;
Mathias Agopian65ab4712010-07-14 17:59:35 -070058
Eric Laurent0ede8922014-05-09 18:04:42 -070059static const nsecs_t kAudioCommandTimeoutNs = seconds(3); // 3 seconds
Christer Fletcher5fa8c4b2013-01-18 15:27:03 +010060
Svet Ganovf4ddfef2018-01-16 07:37:58 -080061static const String16 sManageAudioPolicyPermission("android.permission.MANAGE_AUDIO_POLICY");
Dima Zavinfce7a472011-04-19 22:30:36 -070062
Mathias Agopian65ab4712010-07-14 17:59:35 -070063// ----------------------------------------------------------------------------
64
Jaideep Sharmaba9053b2021-01-25 21:24:26 +053065static AudioPolicyInterface* createAudioPolicyManager(AudioPolicyClientInterface *clientInterface)
66{
67 AudioPolicyManager *apm = new AudioPolicyManager(clientInterface);
68 status_t status = apm->initialize();
69 if (status != NO_ERROR) {
70 delete apm;
71 apm = nullptr;
72 }
73 return apm;
74}
75
76static void destroyAudioPolicyManager(AudioPolicyInterface *interface)
77{
78 delete interface;
79}
80// ----------------------------------------------------------------------------
81
Mathias Agopian65ab4712010-07-14 17:59:35 -070082AudioPolicyService::AudioPolicyService()
Ytai Ben-Tsvi85093d52020-03-26 09:41:15 -070083 : BnAudioPolicyService(),
Ytai Ben-Tsvi85093d52020-03-26 09:41:15 -070084 mAudioPolicyManager(NULL),
85 mAudioPolicyClient(NULL),
86 mPhoneState(AUDIO_MODE_INVALID),
Jaideep Sharmaba9053b2021-01-25 21:24:26 +053087 mCaptureStateNotifier(false),
88 mCreateAudioPolicyManager(createAudioPolicyManager),
89 mDestroyAudioPolicyManager(destroyAudioPolicyManager) {
90}
91
92void AudioPolicyService::loadAudioPolicyManager()
93{
94 mLibraryHandle = dlopen(kAudioPolicyManagerCustomPath, RTLD_NOW);
95 if (mLibraryHandle != nullptr) {
96 ALOGI("%s loading %s", __func__, kAudioPolicyManagerCustomPath);
97 mCreateAudioPolicyManager = reinterpret_cast<CreateAudioPolicyManagerInstance>
98 (dlsym(mLibraryHandle, "createAudioPolicyManager"));
99 const char *lastError = dlerror();
100 ALOGW_IF(mCreateAudioPolicyManager == nullptr, "%s createAudioPolicyManager is null %s",
101 __func__, lastError != nullptr ? lastError : "no error");
102
103 mDestroyAudioPolicyManager = reinterpret_cast<DestroyAudioPolicyManagerInstance>(
104 dlsym(mLibraryHandle, "destroyAudioPolicyManager"));
105 lastError = dlerror();
106 ALOGW_IF(mDestroyAudioPolicyManager == nullptr, "%s destroyAudioPolicyManager is null %s",
107 __func__, lastError != nullptr ? lastError : "no error");
108 if (mCreateAudioPolicyManager == nullptr || mDestroyAudioPolicyManager == nullptr){
109 unloadAudioPolicyManager();
110 LOG_ALWAYS_FATAL("could not find audiopolicymanager interface methods");
111 }
112 }
Eric Laurentf5ada6e2014-10-09 17:49:00 -0700113}
114
115void AudioPolicyService::onFirstRef()
116{
Eric Laurent8b1e80b2014-10-07 09:08:47 -0700117 {
118 Mutex::Autolock _l(mLock);
Eric Laurent93575202011-01-18 18:39:02 -0800119
Eric Laurent8b1e80b2014-10-07 09:08:47 -0700120 // start audio commands thread
121 mAudioCommandThread = new AudioCommandThread(String8("ApmAudio"), this);
122 // start output activity command thread
123 mOutputCommandThread = new AudioCommandThread(String8("ApmOutput"), this);
Eric Laurentdce54a12014-03-10 12:19:46 -0700124
Eric Laurent8b1e80b2014-10-07 09:08:47 -0700125 mAudioPolicyClient = new AudioPolicyClient(this);
Jaideep Sharmaba9053b2021-01-25 21:24:26 +0530126
127 loadAudioPolicyManager();
128 mAudioPolicyManager = mCreateAudioPolicyManager(mAudioPolicyClient);
Eric Laurent8b1e80b2014-10-07 09:08:47 -0700129 }
Eric Laurentd66d7a12021-07-13 13:35:32 +0200130
bryant_liuba2b4392014-06-11 16:49:30 +0800131 // load audio processing modules
Mikhail Naganov12b716c2020-04-30 22:37:43 +0000132 sp<AudioPolicyEffects> audioPolicyEffects = new AudioPolicyEffects();
133 sp<UidPolicy> uidPolicy = new UidPolicy(this);
134 sp<SensorPrivacyPolicy> sensorPrivacyPolicy = new SensorPrivacyPolicy(this);
Eric Laurent8b1e80b2014-10-07 09:08:47 -0700135 {
136 Mutex::Autolock _l(mLock);
137 mAudioPolicyEffects = audioPolicyEffects;
Mikhail Naganov12b716c2020-04-30 22:37:43 +0000138 mUidPolicy = uidPolicy;
139 mSensorPrivacyPolicy = sensorPrivacyPolicy;
Eric Laurent8b1e80b2014-10-07 09:08:47 -0700140 }
Mikhail Naganov12b716c2020-04-30 22:37:43 +0000141 uidPolicy->registerSelf();
142 sensorPrivacyPolicy->registerSelf();
Eric Laurentd66d7a12021-07-13 13:35:32 +0200143
Eric Laurent81dd0f52021-07-05 11:54:40 +0200144 // Create spatializer if supported
Eric Laurent52b0bd52021-09-27 15:25:40 +0200145 if (mAudioPolicyManager != nullptr) {
146 Mutex::Autolock _l(mLock);
147 const audio_attributes_t attr = attributes_initializer(AUDIO_USAGE_MEDIA);
148 AudioDeviceTypeAddrVector devices;
149 bool hasSpatializer = mAudioPolicyManager->canBeSpatialized(&attr, nullptr, devices);
150 if (hasSpatializer) {
151 mSpatializer = Spatializer::create(this);
152 }
Eric Laurent81dd0f52021-07-05 11:54:40 +0200153 }
Eric Laurentd66d7a12021-07-13 13:35:32 +0200154 AudioSystem::audioPolicyReady();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700155}
156
Jaideep Sharmaba9053b2021-01-25 21:24:26 +0530157void AudioPolicyService::unloadAudioPolicyManager()
158{
159 ALOGV("%s ", __func__);
160 if (mLibraryHandle != nullptr) {
161 dlclose(mLibraryHandle);
162 }
163 mLibraryHandle = nullptr;
164 mCreateAudioPolicyManager = nullptr;
165 mDestroyAudioPolicyManager = nullptr;
166}
167
Mathias Agopian65ab4712010-07-14 17:59:35 -0700168AudioPolicyService::~AudioPolicyService()
169{
Mathias Agopian65ab4712010-07-14 17:59:35 -0700170 mAudioCommandThread->exit();
Eric Laurent657ff612014-05-07 11:58:24 -0700171 mOutputCommandThread->exit();
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700172
Jaideep Sharmaba9053b2021-01-25 21:24:26 +0530173 mDestroyAudioPolicyManager(mAudioPolicyManager);
174 unloadAudioPolicyManager();
175
Eric Laurentdce54a12014-03-10 12:19:46 -0700176 delete mAudioPolicyClient;
Eric Laurentb52c1522014-05-20 11:27:36 -0700177
178 mNotificationClients.clear();
bryant_liuba2b4392014-06-11 16:49:30 +0800179 mAudioPolicyEffects.clear();
Svet Ganovf4ddfef2018-01-16 07:37:58 -0800180
181 mUidPolicy->unregisterSelf();
Michael Groovercfd28302018-12-11 19:16:46 -0800182 mSensorPrivacyPolicy->unregisterSelf();
Mikhail Naganov12b716c2020-04-30 22:37:43 +0000183
184 mUidPolicy.clear();
Michael Groovercfd28302018-12-11 19:16:46 -0800185 mSensorPrivacyPolicy.clear();
Eric Laurentb52c1522014-05-20 11:27:36 -0700186}
187
188// A notification client is always registered by AudioSystem when the client process
189// connects to AudioPolicyService.
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800190Status AudioPolicyService::registerClient(const sp<media::IAudioPolicyServiceClient>& client)
Eric Laurentb52c1522014-05-20 11:27:36 -0700191{
Eric Laurent12590252015-08-21 18:40:20 -0700192 if (client == 0) {
193 ALOGW("%s got NULL client", __FUNCTION__);
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800194 return Status::ok();
Eric Laurent12590252015-08-21 18:40:20 -0700195 }
Eric Laurent0ebd5f92014-11-19 19:04:52 -0800196 Mutex::Autolock _l(mNotificationClientsLock);
Eric Laurentb52c1522014-05-20 11:27:36 -0700197
198 uid_t uid = IPCThreadState::self()->getCallingUid();
luochaojiang908c7d72018-06-21 14:58:04 +0800199 pid_t pid = IPCThreadState::self()->getCallingPid();
200 int64_t token = ((int64_t)uid<<32) | pid;
201
202 if (mNotificationClients.indexOfKey(token) < 0) {
Eric Laurentb52c1522014-05-20 11:27:36 -0700203 sp<NotificationClient> notificationClient = new NotificationClient(this,
204 client,
luochaojiang908c7d72018-06-21 14:58:04 +0800205 uid,
206 pid);
207 ALOGV("registerClient() client %p, uid %d pid %d", client.get(), uid, pid);
Eric Laurentb52c1522014-05-20 11:27:36 -0700208
luochaojiang908c7d72018-06-21 14:58:04 +0800209 mNotificationClients.add(token, notificationClient);
Eric Laurentb52c1522014-05-20 11:27:36 -0700210
Marco Nelissenf8880202014-11-14 07:58:25 -0800211 sp<IBinder> binder = IInterface::asBinder(client);
Eric Laurentb52c1522014-05-20 11:27:36 -0700212 binder->linkToDeath(notificationClient);
213 }
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800214 return Status::ok();
Eric Laurentb52c1522014-05-20 11:27:36 -0700215}
216
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800217Status AudioPolicyService::setAudioPortCallbacksEnabled(bool enabled)
Eric Laurente8726fe2015-06-26 09:39:24 -0700218{
219 Mutex::Autolock _l(mNotificationClientsLock);
220
221 uid_t uid = IPCThreadState::self()->getCallingUid();
luochaojiang908c7d72018-06-21 14:58:04 +0800222 pid_t pid = IPCThreadState::self()->getCallingPid();
223 int64_t token = ((int64_t)uid<<32) | pid;
224
225 if (mNotificationClients.indexOfKey(token) < 0) {
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800226 return Status::ok();
Eric Laurente8726fe2015-06-26 09:39:24 -0700227 }
luochaojiang908c7d72018-06-21 14:58:04 +0800228 mNotificationClients.valueFor(token)->setAudioPortCallbacksEnabled(enabled);
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800229 return Status::ok();
Eric Laurente8726fe2015-06-26 09:39:24 -0700230}
231
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800232Status AudioPolicyService::setAudioVolumeGroupCallbacksEnabled(bool enabled)
François Gaffiecfe17322018-11-07 13:41:29 +0100233{
234 Mutex::Autolock _l(mNotificationClientsLock);
235
236 uid_t uid = IPCThreadState::self()->getCallingUid();
237 pid_t pid = IPCThreadState::self()->getCallingPid();
238 int64_t token = ((int64_t)uid<<32) | pid;
239
240 if (mNotificationClients.indexOfKey(token) < 0) {
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800241 return Status::ok();
François Gaffiecfe17322018-11-07 13:41:29 +0100242 }
243 mNotificationClients.valueFor(token)->setAudioVolumeGroupCallbacksEnabled(enabled);
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800244 return Status::ok();
François Gaffiecfe17322018-11-07 13:41:29 +0100245}
246
Eric Laurentb52c1522014-05-20 11:27:36 -0700247// removeNotificationClient() is called when the client process dies.
luochaojiang908c7d72018-06-21 14:58:04 +0800248void AudioPolicyService::removeNotificationClient(uid_t uid, pid_t pid)
Eric Laurentb52c1522014-05-20 11:27:36 -0700249{
Mikhail Naganov12b716c2020-04-30 22:37:43 +0000250 bool hasSameUid = false;
Eric Laurent0ebd5f92014-11-19 19:04:52 -0800251 {
252 Mutex::Autolock _l(mNotificationClientsLock);
luochaojiang908c7d72018-06-21 14:58:04 +0800253 int64_t token = ((int64_t)uid<<32) | pid;
254 mNotificationClients.removeItem(token);
luochaojiang908c7d72018-06-21 14:58:04 +0800255 for (size_t i = 0; i < mNotificationClients.size(); i++) {
256 if (mNotificationClients.valueAt(i)->uid() == uid) {
257 hasSameUid = true;
258 break;
259 }
260 }
Mikhail Naganov12b716c2020-04-30 22:37:43 +0000261 }
262 {
263 Mutex::Autolock _l(mLock);
luochaojiang908c7d72018-06-21 14:58:04 +0800264 if (mAudioPolicyManager && !hasSameUid) {
Eric Laurent10b71232018-04-13 18:14:44 -0700265 // called from binder death notification: no need to clear caller identity
Eric Laurent8c7e6da2015-04-21 17:37:00 -0700266 mAudioPolicyManager->releaseResourcesForUid(uid);
Eric Laurentb52c1522014-05-20 11:27:36 -0700267 }
Eric Laurent0ebd5f92014-11-19 19:04:52 -0800268 }
Eric Laurentb52c1522014-05-20 11:27:36 -0700269}
270
271void AudioPolicyService::onAudioPortListUpdate()
272{
273 mOutputCommandThread->updateAudioPortListCommand();
274}
275
276void AudioPolicyService::doOnAudioPortListUpdate()
277{
Eric Laurent0ebd5f92014-11-19 19:04:52 -0800278 Mutex::Autolock _l(mNotificationClientsLock);
Eric Laurentb52c1522014-05-20 11:27:36 -0700279 for (size_t i = 0; i < mNotificationClients.size(); i++) {
280 mNotificationClients.valueAt(i)->onAudioPortListUpdate();
281 }
282}
283
284void AudioPolicyService::onAudioPatchListUpdate()
285{
286 mOutputCommandThread->updateAudioPatchListCommand();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700287}
288
Eric Laurentb52c1522014-05-20 11:27:36 -0700289void AudioPolicyService::doOnAudioPatchListUpdate()
290{
Eric Laurent0ebd5f92014-11-19 19:04:52 -0800291 Mutex::Autolock _l(mNotificationClientsLock);
Eric Laurentb52c1522014-05-20 11:27:36 -0700292 for (size_t i = 0; i < mNotificationClients.size(); i++) {
293 mNotificationClients.valueAt(i)->onAudioPatchListUpdate();
294 }
295}
296
François Gaffiecfe17322018-11-07 13:41:29 +0100297void AudioPolicyService::onAudioVolumeGroupChanged(volume_group_t group, int flags)
298{
299 mOutputCommandThread->changeAudioVolumeGroupCommand(group, flags);
300}
301
302void AudioPolicyService::doOnAudioVolumeGroupChanged(volume_group_t group, int flags)
303{
304 Mutex::Autolock _l(mNotificationClientsLock);
305 for (size_t i = 0; i < mNotificationClients.size(); i++) {
306 mNotificationClients.valueAt(i)->onAudioVolumeGroupChanged(group, flags);
307 }
308}
309
Chih-Hung Hsiehe964d4e2016-08-09 14:31:32 -0700310void AudioPolicyService::onDynamicPolicyMixStateUpdate(const String8& regId, int32_t state)
Jean-Michel Trivide801052015-04-14 19:10:14 -0700311{
312 ALOGV("AudioPolicyService::onDynamicPolicyMixStateUpdate(%s, %d)",
313 regId.string(), state);
314 mOutputCommandThread->dynamicPolicyMixStateUpdateCommand(regId, state);
315}
316
Chih-Hung Hsiehe964d4e2016-08-09 14:31:32 -0700317void AudioPolicyService::doOnDynamicPolicyMixStateUpdate(const String8& regId, int32_t state)
Jean-Michel Trivide801052015-04-14 19:10:14 -0700318{
319 Mutex::Autolock _l(mNotificationClientsLock);
320 for (size_t i = 0; i < mNotificationClients.size(); i++) {
321 mNotificationClients.valueAt(i)->onDynamicPolicyMixStateUpdate(regId, state);
322 }
323}
324
Eric Laurenta9f86652018-11-28 17:23:11 -0800325void AudioPolicyService::onRecordingConfigurationUpdate(
326 int event,
327 const record_client_info_t *clientInfo,
328 const audio_config_base_t *clientConfig,
329 std::vector<effect_descriptor_t> clientEffects,
330 const audio_config_base_t *deviceConfig,
331 std::vector<effect_descriptor_t> effects,
332 audio_patch_handle_t patchHandle,
333 audio_source_t source)
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -0800334{
Jean-Michel Triviac4e4292016-12-22 11:39:31 -0800335 mOutputCommandThread->recordingConfigurationUpdateCommand(event, clientInfo,
Eric Laurenta9f86652018-11-28 17:23:11 -0800336 clientConfig, clientEffects, deviceConfig, effects, patchHandle, source);
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -0800337}
338
Eric Laurenta9f86652018-11-28 17:23:11 -0800339void AudioPolicyService::doOnRecordingConfigurationUpdate(
340 int event,
341 const record_client_info_t *clientInfo,
342 const audio_config_base_t *clientConfig,
343 std::vector<effect_descriptor_t> clientEffects,
344 const audio_config_base_t *deviceConfig,
345 std::vector<effect_descriptor_t> effects,
346 audio_patch_handle_t patchHandle,
347 audio_source_t source)
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -0800348{
349 Mutex::Autolock _l(mNotificationClientsLock);
350 for (size_t i = 0; i < mNotificationClients.size(); i++) {
Jean-Michel Triviac4e4292016-12-22 11:39:31 -0800351 mNotificationClients.valueAt(i)->onRecordingConfigurationUpdate(event, clientInfo,
Eric Laurenta9f86652018-11-28 17:23:11 -0800352 clientConfig, clientEffects, deviceConfig, effects, patchHandle, source);
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -0800353 }
354}
355
Jean-Michel Trivi9a6b9ad2020-10-22 16:46:43 -0700356void AudioPolicyService::onRoutingUpdated()
357{
358 mOutputCommandThread->routingChangedCommand();
359}
360
361void AudioPolicyService::doOnRoutingUpdated()
362{
363 Mutex::Autolock _l(mNotificationClientsLock);
364 for (size_t i = 0; i < mNotificationClients.size(); i++) {
365 mNotificationClients.valueAt(i)->onRoutingUpdated();
366 }
367}
368
Eric Laurent81dd0f52021-07-05 11:54:40 +0200369void AudioPolicyService::onCheckSpatializer()
370{
371 Mutex::Autolock _l(mLock);
Eric Laurent39095982021-08-24 18:29:27 +0200372 onCheckSpatializer_l();
373}
374
375void AudioPolicyService::onCheckSpatializer_l()
376{
377 if (mSpatializer != nullptr) {
378 mOutputCommandThread->checkSpatializerCommand();
379 }
Eric Laurent81dd0f52021-07-05 11:54:40 +0200380}
381
382void AudioPolicyService::doOnCheckSpatializer()
383{
Eric Laurent39095982021-08-24 18:29:27 +0200384 Mutex::Autolock _l(mLock);
Eric Laurent81dd0f52021-07-05 11:54:40 +0200385
Eric Laurent39095982021-08-24 18:29:27 +0200386 if (mSpatializer != nullptr) {
Eric Laurent52b0bd52021-09-27 15:25:40 +0200387 // Note: mSpatializer != nullptr => mAudioPolicyManager != nullptr
Eric Laurent39095982021-08-24 18:29:27 +0200388 if (mSpatializer->getLevel() != media::SpatializationLevel::NONE) {
389 audio_io_handle_t currentOutput = mSpatializer->getOutput();
390 audio_io_handle_t newOutput;
391 const audio_attributes_t attr = attributes_initializer(AUDIO_USAGE_MEDIA);
392 audio_config_base_t config = mSpatializer->getAudioInConfig();
393 status_t status =
394 mAudioPolicyManager->getSpatializerOutput(&config, &attr, &newOutput);
395
396 if (status == NO_ERROR && currentOutput == newOutput) {
397 return;
398 }
399 mLock.unlock();
400 // It is OK to call detachOutput() is none is already attached.
401 mSpatializer->detachOutput();
402 if (status != NO_ERROR || newOutput == AUDIO_IO_HANDLE_NONE) {
Eric Laurent81dd0f52021-07-05 11:54:40 +0200403 mLock.lock();
Eric Laurent39095982021-08-24 18:29:27 +0200404 return;
405 }
406 status = mSpatializer->attachOutput(newOutput);
407 mLock.lock();
408 if (status != NO_ERROR) {
409 mAudioPolicyManager->releaseSpatializerOutput(newOutput);
410 }
411 } else if (mSpatializer->getLevel() == media::SpatializationLevel::NONE
412 && mSpatializer->getOutput() != AUDIO_IO_HANDLE_NONE) {
413 mLock.unlock();
414 audio_io_handle_t output = mSpatializer->detachOutput();
415 mLock.lock();
416 if (output != AUDIO_IO_HANDLE_NONE) {
417 mAudioPolicyManager->releaseSpatializerOutput(output);
Eric Laurent81dd0f52021-07-05 11:54:40 +0200418 }
419 }
420 }
421}
422
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -0800423status_t AudioPolicyService::clientCreateAudioPatch(const struct audio_patch *patch,
424 audio_patch_handle_t *handle,
425 int delayMs)
426{
427 return mAudioCommandThread->createAudioPatchCommand(patch, handle, delayMs);
428}
429
430status_t AudioPolicyService::clientReleaseAudioPatch(audio_patch_handle_t handle,
431 int delayMs)
432{
433 return mAudioCommandThread->releaseAudioPatchCommand(handle, delayMs);
434}
435
Eric Laurente1715a42014-05-20 11:30:42 -0700436status_t AudioPolicyService::clientSetAudioPortConfig(const struct audio_port_config *config,
437 int delayMs)
438{
439 return mAudioCommandThread->setAudioPortConfigCommand(config, delayMs);
440}
441
Ytai Ben-Tsvi7e7a79d2020-12-15 16:48:16 -0800442AudioPolicyService::NotificationClient::NotificationClient(
443 const sp<AudioPolicyService>& service,
444 const sp<media::IAudioPolicyServiceClient>& client,
445 uid_t uid,
446 pid_t pid)
luochaojiang908c7d72018-06-21 14:58:04 +0800447 : mService(service), mUid(uid), mPid(pid), mAudioPolicyServiceClient(client),
François Gaffiecfe17322018-11-07 13:41:29 +0100448 mAudioPortCallbacksEnabled(false), mAudioVolumeGroupCallbacksEnabled(false)
Eric Laurentb52c1522014-05-20 11:27:36 -0700449{
450}
451
452AudioPolicyService::NotificationClient::~NotificationClient()
453{
454}
455
456void AudioPolicyService::NotificationClient::binderDied(const wp<IBinder>& who __unused)
457{
458 sp<NotificationClient> keep(this);
459 sp<AudioPolicyService> service = mService.promote();
460 if (service != 0) {
luochaojiang908c7d72018-06-21 14:58:04 +0800461 service->removeNotificationClient(mUid, mPid);
Eric Laurentb52c1522014-05-20 11:27:36 -0700462 }
463}
464
465void AudioPolicyService::NotificationClient::onAudioPortListUpdate()
466{
Eric Laurente8726fe2015-06-26 09:39:24 -0700467 if (mAudioPolicyServiceClient != 0 && mAudioPortCallbacksEnabled) {
Eric Laurentb52c1522014-05-20 11:27:36 -0700468 mAudioPolicyServiceClient->onAudioPortListUpdate();
469 }
470}
471
472void AudioPolicyService::NotificationClient::onAudioPatchListUpdate()
473{
Eric Laurente8726fe2015-06-26 09:39:24 -0700474 if (mAudioPolicyServiceClient != 0 && mAudioPortCallbacksEnabled) {
Eric Laurentb52c1522014-05-20 11:27:36 -0700475 mAudioPolicyServiceClient->onAudioPatchListUpdate();
476 }
477}
Eric Laurent57dae992011-07-24 13:36:09 -0700478
François Gaffiecfe17322018-11-07 13:41:29 +0100479void AudioPolicyService::NotificationClient::onAudioVolumeGroupChanged(volume_group_t group,
480 int flags)
481{
482 if (mAudioPolicyServiceClient != 0 && mAudioVolumeGroupCallbacksEnabled) {
483 mAudioPolicyServiceClient->onAudioVolumeGroupChanged(group, flags);
484 }
485}
486
487
Jean-Michel Trivide801052015-04-14 19:10:14 -0700488void AudioPolicyService::NotificationClient::onDynamicPolicyMixStateUpdate(
Chih-Hung Hsiehe964d4e2016-08-09 14:31:32 -0700489 const String8& regId, int32_t state)
Jean-Michel Trivide801052015-04-14 19:10:14 -0700490{
Andy Hung4ef19fa2018-05-15 19:35:29 -0700491 if (mAudioPolicyServiceClient != 0 && isServiceUid(mUid)) {
Ytai Ben-Tsvi7e7a79d2020-12-15 16:48:16 -0800492 mAudioPolicyServiceClient->onDynamicPolicyMixStateUpdate(
493 legacy2aidl_String8_string(regId).value(), state);
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -0800494 }
495}
496
497void AudioPolicyService::NotificationClient::onRecordingConfigurationUpdate(
Eric Laurenta9f86652018-11-28 17:23:11 -0800498 int event,
499 const record_client_info_t *clientInfo,
500 const audio_config_base_t *clientConfig,
501 std::vector<effect_descriptor_t> clientEffects,
502 const audio_config_base_t *deviceConfig,
503 std::vector<effect_descriptor_t> effects,
504 audio_patch_handle_t patchHandle,
505 audio_source_t source)
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -0800506{
Andy Hung4ef19fa2018-05-15 19:35:29 -0700507 if (mAudioPolicyServiceClient != 0 && isServiceUid(mUid)) {
Ytai Ben-Tsvi7e7a79d2020-12-15 16:48:16 -0800508 status_t status = [&]() -> status_t {
509 int32_t eventAidl = VALUE_OR_RETURN_STATUS(convertIntegral<int32_t>(event));
510 media::RecordClientInfo clientInfoAidl = VALUE_OR_RETURN_STATUS(
511 legacy2aidl_record_client_info_t_RecordClientInfo(*clientInfo));
Mikhail Naganovdbf03642021-08-25 18:15:32 -0700512 AudioConfigBase clientConfigAidl = VALUE_OR_RETURN_STATUS(
Mikhail Naganovde3fa182021-07-30 15:06:42 -0700513 legacy2aidl_audio_config_base_t_AudioConfigBase(
514 *clientConfig, true /*isInput*/));
Ytai Ben-Tsvi7e7a79d2020-12-15 16:48:16 -0800515 std::vector<media::EffectDescriptor> clientEffectsAidl = VALUE_OR_RETURN_STATUS(
516 convertContainer<std::vector<media::EffectDescriptor>>(
517 clientEffects,
518 legacy2aidl_effect_descriptor_t_EffectDescriptor));
Mikhail Naganovdbf03642021-08-25 18:15:32 -0700519 AudioConfigBase deviceConfigAidl = VALUE_OR_RETURN_STATUS(
Mikhail Naganovde3fa182021-07-30 15:06:42 -0700520 legacy2aidl_audio_config_base_t_AudioConfigBase(
521 *deviceConfig, true /*isInput*/));
Ytai Ben-Tsvi7e7a79d2020-12-15 16:48:16 -0800522 std::vector<media::EffectDescriptor> effectsAidl = VALUE_OR_RETURN_STATUS(
523 convertContainer<std::vector<media::EffectDescriptor>>(
524 effects,
525 legacy2aidl_effect_descriptor_t_EffectDescriptor));
526 int32_t patchHandleAidl = VALUE_OR_RETURN_STATUS(
527 legacy2aidl_audio_patch_handle_t_int32_t(patchHandle));
Mikhail Naganovddceecc2021-09-03 13:58:56 -0700528 media::audio::common::AudioSource sourceAidl = VALUE_OR_RETURN_STATUS(
529 legacy2aidl_audio_source_t_AudioSource(source));
Ytai Ben-Tsvi7e7a79d2020-12-15 16:48:16 -0800530 return aidl_utils::statusTFromBinderStatus(
531 mAudioPolicyServiceClient->onRecordingConfigurationUpdate(eventAidl,
532 clientInfoAidl,
533 clientConfigAidl,
534 clientEffectsAidl,
535 deviceConfigAidl,
536 effectsAidl,
537 patchHandleAidl,
538 sourceAidl));
539 }();
540 ALOGW_IF(status != OK, "onRecordingConfigurationUpdate() failed: %d", status);
Jean-Michel Trivide801052015-04-14 19:10:14 -0700541 }
542}
543
Eric Laurente8726fe2015-06-26 09:39:24 -0700544void AudioPolicyService::NotificationClient::setAudioPortCallbacksEnabled(bool enabled)
545{
546 mAudioPortCallbacksEnabled = enabled;
547}
548
François Gaffiecfe17322018-11-07 13:41:29 +0100549void AudioPolicyService::NotificationClient::setAudioVolumeGroupCallbacksEnabled(bool enabled)
550{
551 mAudioVolumeGroupCallbacksEnabled = enabled;
552}
Eric Laurente8726fe2015-06-26 09:39:24 -0700553
Jean-Michel Trivi9a6b9ad2020-10-22 16:46:43 -0700554void AudioPolicyService::NotificationClient::onRoutingUpdated()
555{
556 if (mAudioPolicyServiceClient != 0 && isServiceUid(mUid)) {
557 mAudioPolicyServiceClient->onRoutingUpdated();
558 }
559}
560
Mathias Agopian65ab4712010-07-14 17:59:35 -0700561void AudioPolicyService::binderDied(const wp<IBinder>& who) {
Glenn Kasten411e4472012-11-02 10:00:06 -0700562 ALOGW("binderDied() %p, calling pid %d", who.unsafe_get(),
Eric Laurentde070132010-07-13 04:45:46 -0700563 IPCThreadState::self()->getCallingPid());
Mathias Agopian65ab4712010-07-14 17:59:35 -0700564}
565
Mikhail Naganov12b716c2020-04-30 22:37:43 +0000566static bool dumpTryLock(Mutex& mutex) ACQUIRE(mutex) NO_THREAD_SAFETY_ANALYSIS
Mathias Agopian65ab4712010-07-14 17:59:35 -0700567{
Mikhail Naganov12b716c2020-04-30 22:37:43 +0000568 return mutex.timedLock(kDumpLockTimeoutNs) == NO_ERROR;
569}
570
571static void dumpReleaseLock(Mutex& mutex, bool locked) RELEASE(mutex) NO_THREAD_SAFETY_ANALYSIS
572{
573 if (locked) mutex.unlock();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700574}
575
576status_t AudioPolicyService::dumpInternals(int fd)
577{
578 const size_t SIZE = 256;
579 char buffer[SIZE];
580 String8 result;
581
Eric Laurentdce54a12014-03-10 12:19:46 -0700582 snprintf(buffer, SIZE, "AudioPolicyManager: %p\n", mAudioPolicyManager);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700583 result.append(buffer);
584 snprintf(buffer, SIZE, "Command Thread: %p\n", mAudioCommandThread.get());
585 result.append(buffer);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700586
Hayden Gomes524159d2019-12-23 14:41:47 -0800587 snprintf(buffer, SIZE, "Supported System Usages:\n");
588 result.append(buffer);
589 for (std::vector<audio_usage_t>::iterator it = mSupportedSystemUsages.begin();
590 it != mSupportedSystemUsages.end(); ++it) {
591 snprintf(buffer, SIZE, "\t%d\n", *it);
592 result.append(buffer);
593 }
594
Mathias Agopian65ab4712010-07-14 17:59:35 -0700595 write(fd, result.string(), result.size());
596 return NO_ERROR;
597}
598
Eric Laurente8c8b432018-10-17 10:08:02 -0700599void AudioPolicyService::updateUidStates()
Svet Ganovf4ddfef2018-01-16 07:37:58 -0800600{
Eric Laurente8c8b432018-10-17 10:08:02 -0700601 Mutex::Autolock _l(mLock);
602 updateUidStates_l();
603}
604
605void AudioPolicyService::updateUidStates_l()
606{
Eric Laurent4eb58f12018-12-07 16:41:02 -0800607// Go over all active clients and allow capture (does not force silence) in the
608// following cases:
Evan Severson1f700cd2021-02-10 13:10:37 -0800609// The client is the assistant
610// AND an accessibility service is on TOP or a RTT call is active
Eric Laurent589171c2019-07-25 18:04:29 -0700611// AND the source is VOICE_RECOGNITION or HOTWORD
Evan Severson1f700cd2021-02-10 13:10:37 -0800612// OR uses VOICE_RECOGNITION AND is on TOP
613// OR uses HOTWORD
614// AND there is no active privacy sensitive capture or call
615// OR client has CAPTURE_AUDIO_OUTPUT privileged permission
616// OR The client is an accessibility service
617// AND Is on TOP
618// AND the source is VOICE_RECOGNITION or HOTWORD
619// OR The assistant is not on TOP
Eric Laurent589171c2019-07-25 18:04:29 -0700620// AND there is no active privacy sensitive capture or call
621// OR client has CAPTURE_AUDIO_OUTPUT privileged permission
Evan Severson1f700cd2021-02-10 13:10:37 -0800622// AND is on TOP
623// AND the source is VOICE_RECOGNITION or HOTWORD
624// OR the client source is virtual (remote submix, call audio TX or RX...)
625// OR the client source is HOTWORD
626// AND is on TOP
627// OR all active clients are using HOTWORD source
628// AND no call is active
629// OR client has CAPTURE_AUDIO_OUTPUT privileged permission
630// OR the client is the current InputMethodService
631// AND a RTT call is active AND the source is VOICE_RECOGNITION
632// OR Any client
633// AND The assistant is not on TOP
634// AND is on TOP or latest started
635// AND there is no active privacy sensitive capture or call
636// OR client has CAPTURE_AUDIO_OUTPUT privileged permission
Eric Laurent4eb58f12018-12-07 16:41:02 -0800637
Eric Laurent4e947da2019-10-17 15:24:06 -0700638
Eric Laurent4eb58f12018-12-07 16:41:02 -0800639 sp<AudioRecordClient> topActive;
640 sp<AudioRecordClient> latestActive;
Eric Laurentc21d5692020-02-25 10:24:36 -0800641 sp<AudioRecordClient> topSensitiveActive;
Eric Laurentb809a752020-06-29 09:53:13 -0700642 sp<AudioRecordClient> latestSensitiveActiveOrComm;
Eric Laurent1ff16a72019-03-14 18:35:04 -0700643
Eric Laurenta46bedb2018-12-07 18:01:26 -0800644 nsecs_t topStartNs = 0;
645 nsecs_t latestStartNs = 0;
Eric Laurentc21d5692020-02-25 10:24:36 -0800646 nsecs_t topSensitiveStartNs = 0;
Eric Laurent4eb58f12018-12-07 16:41:02 -0800647 nsecs_t latestSensitiveStartNs = 0;
648 bool isA11yOnTop = mUidPolicy->isA11yOnTop();
649 bool isAssistantOnTop = false;
650 bool isSensitiveActive = false;
Eric Laurent1ff16a72019-03-14 18:35:04 -0700651 bool isInCall = mPhoneState == AUDIO_MODE_IN_CALL;
Eric Laurentc21d5692020-02-25 10:24:36 -0800652 bool isInCommunication = mPhoneState == AUDIO_MODE_IN_COMMUNICATION;
653 bool rttCallActive = (isInCall || isInCommunication)
Eric Laurent6ede98f2019-06-11 14:50:30 -0700654 && mUidPolicy->isRttEnabled();
Eric Laurent4e947da2019-10-17 15:24:06 -0700655 bool onlyHotwordActive = true;
Eric Laurentb809a752020-06-29 09:53:13 -0700656 bool isPhoneStateOwnerActive = false;
Eric Laurent4eb58f12018-12-07 16:41:02 -0800657
Michael Groovercfd28302018-12-11 19:16:46 -0800658 // if Sensor Privacy is enabled then all recordings should be silenced.
659 if (mSensorPrivacyPolicy->isSensorPrivacyEnabled()) {
660 silenceAllRecordings_l();
661 return;
662 }
663
Eric Laurente8c8b432018-10-17 10:08:02 -0700664 for (size_t i =0; i < mAudioRecordClients.size(); i++) {
665 sp<AudioRecordClient> current = mAudioRecordClients[i];
Svet Ganov33761132021-05-13 22:51:08 +0000666 uid_t currentUid = VALUE_OR_FATAL(aidl2legacy_int32_t_uid_t(
667 current->attributionSource.uid));
Evan Severson1f700cd2021-02-10 13:10:37 -0800668 if (!current->active) {
Eric Laurent1ff16a72019-03-14 18:35:04 -0700669 continue;
Eric Laurent4eb58f12018-12-07 16:41:02 -0800670 }
Eric Laurent1ff16a72019-03-14 18:35:04 -0700671
Philip P. Moltmannbda45752020-07-17 16:41:18 -0700672 app_state_t appState = apmStatFromAmState(mUidPolicy->getUidState(currentUid));
Eric Laurent1ff16a72019-03-14 18:35:04 -0700673 // clients which app is in IDLE state are not eligible for top active or
674 // latest active
675 if (appState == APP_STATE_IDLE) {
676 continue;
677 }
678
Philip P. Moltmannbda45752020-07-17 16:41:18 -0700679 bool isAccessibility = mUidPolicy->isA11yUid(currentUid);
Eric Laurent14a88632020-07-16 12:28:30 -0700680 // Clients capturing for Accessibility services or virtual sources are not considered
Eric Laurentc21d5692020-02-25 10:24:36 -0800681 // for top or latest active to avoid masking regular clients started before
Eric Laurent14a88632020-07-16 12:28:30 -0700682 if (!isAccessibility && !isVirtualSource(current->attributes.source)) {
Philip P. Moltmannbda45752020-07-17 16:41:18 -0700683 bool isAssistant = mUidPolicy->isAssistantUid(currentUid);
Eric Laurentc21d5692020-02-25 10:24:36 -0800684 bool isPrivacySensitive =
685 (current->attributes.flags & AUDIO_FLAG_CAPTURE_PRIVATE) != 0;
Eric Laurentb809a752020-06-29 09:53:13 -0700686
Eric Laurentc21d5692020-02-25 10:24:36 -0800687 if (appState == APP_STATE_TOP) {
688 if (isPrivacySensitive) {
689 if (current->startTimeNs > topSensitiveStartNs) {
690 topSensitiveActive = current;
691 topSensitiveStartNs = current->startTimeNs;
692 }
693 } else {
694 if (current->startTimeNs > topStartNs) {
695 topActive = current;
696 topStartNs = current->startTimeNs;
697 }
698 }
699 if (isAssistant) {
700 isAssistantOnTop = true;
701 }
Eric Laurenta46bedb2018-12-07 18:01:26 -0800702 }
Eric Laurentc21d5692020-02-25 10:24:36 -0800703 // Clients capturing for HOTWORD are not considered
704 // for latest active to avoid masking regular clients started before
705 if (!(current->attributes.source == AUDIO_SOURCE_HOTWORD
706 || ((isA11yOnTop || rttCallActive) && isAssistant))) {
707 if (isPrivacySensitive) {
Eric Laurentb809a752020-06-29 09:53:13 -0700708 // if audio mode is IN_COMMUNICATION, make sure the audio mode owner
709 // is marked latest sensitive active even if another app qualifies.
710 if (current->startTimeNs > latestSensitiveStartNs
Philip P. Moltmannbda45752020-07-17 16:41:18 -0700711 || (isInCommunication && currentUid == mPhoneStateOwnerUid)) {
Eric Laurentb809a752020-06-29 09:53:13 -0700712 if (!isInCommunication || latestSensitiveActiveOrComm == nullptr
Philip P. Moltmannbda45752020-07-17 16:41:18 -0700713 || VALUE_OR_FATAL(aidl2legacy_int32_t_uid_t(
Svet Ganov33761132021-05-13 22:51:08 +0000714 latestSensitiveActiveOrComm->attributionSource.uid))
Philip P. Moltmannbda45752020-07-17 16:41:18 -0700715 != mPhoneStateOwnerUid) {
Eric Laurentb809a752020-06-29 09:53:13 -0700716 latestSensitiveActiveOrComm = current;
717 latestSensitiveStartNs = current->startTimeNs;
718 }
Eric Laurentc21d5692020-02-25 10:24:36 -0800719 }
720 isSensitiveActive = true;
721 } else {
722 if (current->startTimeNs > latestStartNs) {
723 latestActive = current;
724 latestStartNs = current->startTimeNs;
725 }
726 }
Eric Laurent4eb58f12018-12-07 16:41:02 -0800727 }
728 }
Eric Laurent4e947da2019-10-17 15:24:06 -0700729 if (current->attributes.source != AUDIO_SOURCE_HOTWORD) {
730 onlyHotwordActive = false;
731 }
Philip P. Moltmannbda45752020-07-17 16:41:18 -0700732 if (currentUid == mPhoneStateOwnerUid) {
Eric Laurentb809a752020-06-29 09:53:13 -0700733 isPhoneStateOwnerActive = true;
734 }
Eric Laurent4eb58f12018-12-07 16:41:02 -0800735 }
736
Eric Laurent1ff16a72019-03-14 18:35:04 -0700737 // if no active client with UI on Top, consider latest active as top
738 if (topActive == nullptr) {
739 topActive = latestActive;
Eric Laurentc21d5692020-02-25 10:24:36 -0800740 topStartNs = latestStartNs;
741 }
742 if (topSensitiveActive == nullptr) {
Eric Laurentb809a752020-06-29 09:53:13 -0700743 topSensitiveActive = latestSensitiveActiveOrComm;
Eric Laurentc21d5692020-02-25 10:24:36 -0800744 topSensitiveStartNs = latestSensitiveStartNs;
Eric Laurentb809a752020-06-29 09:53:13 -0700745 } else if (latestSensitiveActiveOrComm != nullptr) {
746 // if audio mode is IN_COMMUNICATION, favor audio mode owner over an app with
747 // foreground UI in case both are capturing with privacy sensitive flag.
Philip P. Moltmannbda45752020-07-17 16:41:18 -0700748 uid_t latestActiveUid = VALUE_OR_FATAL(
Svet Ganov33761132021-05-13 22:51:08 +0000749 aidl2legacy_int32_t_uid_t(latestSensitiveActiveOrComm->attributionSource.uid));
Philip P. Moltmannbda45752020-07-17 16:41:18 -0700750 if (isInCommunication && latestActiveUid == mPhoneStateOwnerUid) {
Eric Laurentb809a752020-06-29 09:53:13 -0700751 topSensitiveActive = latestSensitiveActiveOrComm;
752 topSensitiveStartNs = latestSensitiveStartNs;
753 }
Eric Laurentc21d5692020-02-25 10:24:36 -0800754 }
755
756 // If both privacy sensitive and regular capture are active:
757 // if the regular capture is privileged
758 // allow concurrency
759 // else
760 // favor the privacy sensitive case
761 if (topActive != nullptr && topSensitiveActive != nullptr
Ricardo Correa57a37692020-03-23 17:27:25 -0700762 && !topActive->canCaptureOutput) {
Eric Laurentc21d5692020-02-25 10:24:36 -0800763 topActive = nullptr;
Eric Laurent4eb58f12018-12-07 16:41:02 -0800764 }
765
766 for (size_t i =0; i < mAudioRecordClients.size(); i++) {
767 sp<AudioRecordClient> current = mAudioRecordClients[i];
Philip P. Moltmannbda45752020-07-17 16:41:18 -0700768 uid_t currentUid = VALUE_OR_FATAL(aidl2legacy_int32_t_uid_t(
Svet Ganov33761132021-05-13 22:51:08 +0000769 current->attributionSource.uid));
Eric Laurent1ff16a72019-03-14 18:35:04 -0700770 if (!current->active) {
771 continue;
772 }
773
Eric Laurent4eb58f12018-12-07 16:41:02 -0800774 audio_source_t source = current->attributes.source;
Philip P. Moltmannbda45752020-07-17 16:41:18 -0700775 bool isTopOrLatestActive = topActive == nullptr ? false :
Svet Ganov33761132021-05-13 22:51:08 +0000776 current->attributionSource.uid == topActive->attributionSource.uid;
Philip P. Moltmannbda45752020-07-17 16:41:18 -0700777 bool isTopOrLatestSensitive = topSensitiveActive == nullptr ? false :
Svet Ganov33761132021-05-13 22:51:08 +0000778 current->attributionSource.uid == topSensitiveActive->attributionSource.uid;
Eric Laurentc21d5692020-02-25 10:24:36 -0800779
Mikhail Naganov12b716c2020-04-30 22:37:43 +0000780 auto canCaptureIfInCallOrCommunication = [&](const auto &recordClient) REQUIRES(mLock) {
Philip P. Moltmannbda45752020-07-17 16:41:18 -0700781 uid_t recordUid = VALUE_OR_FATAL(aidl2legacy_int32_t_uid_t(
Svet Ganov33761132021-05-13 22:51:08 +0000782 recordClient->attributionSource.uid));
Ricardo Correa57a37692020-03-23 17:27:25 -0700783 bool canCaptureCall = recordClient->canCaptureOutput;
Eric Laurentb809a752020-06-29 09:53:13 -0700784 bool canCaptureCommunication = recordClient->canCaptureOutput
785 || !isPhoneStateOwnerActive
Philip P. Moltmannbda45752020-07-17 16:41:18 -0700786 || recordUid == mPhoneStateOwnerUid;
Eric Laurentb809a752020-06-29 09:53:13 -0700787 return !(isInCall && !canCaptureCall)
788 && !(isInCommunication && !canCaptureCommunication);
Eric Laurentc21d5692020-02-25 10:24:36 -0800789 };
Eric Laurent1ff16a72019-03-14 18:35:04 -0700790
791 // By default allow capture if:
792 // The assistant is not on TOP
Eric Laurenta171e352019-05-07 13:04:45 -0700793 // AND is on TOP or latest started
Eric Laurent1ff16a72019-03-14 18:35:04 -0700794 // AND there is no active privacy sensitive capture or call
795 // OR client has CAPTURE_AUDIO_OUTPUT privileged permission
796 bool allowCapture = !isAssistantOnTop
Eric Laurentc21d5692020-02-25 10:24:36 -0800797 && (isTopOrLatestActive || isTopOrLatestSensitive)
798 && !(isSensitiveActive
Ricardo Correa57a37692020-03-23 17:27:25 -0700799 && !(isTopOrLatestSensitive || current->canCaptureOutput))
Eric Laurentc21d5692020-02-25 10:24:36 -0800800 && canCaptureIfInCallOrCommunication(current);
Eric Laurent2dc962b2019-03-01 08:25:25 -0800801
Eric Laurented726cc2021-07-01 14:26:41 +0200802 if (!current->hasOp()) {
803 // Never allow capture if app op is denied
804 allowCapture = false;
805 } else if (isVirtualSource(source)) {
Eric Laurent1ff16a72019-03-14 18:35:04 -0700806 // Allow capture for virtual (remote submix, call audio TX or RX...) sources
807 allowCapture = true;
Philip P. Moltmannbda45752020-07-17 16:41:18 -0700808 } else if (mUidPolicy->isAssistantUid(currentUid)) {
Eric Laurent1ff16a72019-03-14 18:35:04 -0700809 // For assistant allow capture if:
Eric Laurent6ede98f2019-06-11 14:50:30 -0700810 // An accessibility service is on TOP or a RTT call is active
Eric Laurent1ff16a72019-03-14 18:35:04 -0700811 // AND the source is VOICE_RECOGNITION or HOTWORD
Eric Laurenta171e352019-05-07 13:04:45 -0700812 // OR is on TOP AND uses VOICE_RECOGNITION
Eric Laurent1ff16a72019-03-14 18:35:04 -0700813 // OR uses HOTWORD
814 // AND there is no active privacy sensitive capture or call
815 // OR client has CAPTURE_AUDIO_OUTPUT privileged permission
Eric Laurent6ede98f2019-06-11 14:50:30 -0700816 if (isA11yOnTop || rttCallActive) {
Eric Laurent4eb58f12018-12-07 16:41:02 -0800817 if (source == AUDIO_SOURCE_HOTWORD || source == AUDIO_SOURCE_VOICE_RECOGNITION) {
Eric Laurent1ff16a72019-03-14 18:35:04 -0700818 allowCapture = true;
Eric Laurent4eb58f12018-12-07 16:41:02 -0800819 }
820 } else {
Eric Laurenta171e352019-05-07 13:04:45 -0700821 if (((isAssistantOnTop && source == AUDIO_SOURCE_VOICE_RECOGNITION) ||
Eric Laurentc21d5692020-02-25 10:24:36 -0800822 source == AUDIO_SOURCE_HOTWORD)
Ricardo Correa57a37692020-03-23 17:27:25 -0700823 && !(isSensitiveActive && !current->canCaptureOutput)
Eric Laurentc21d5692020-02-25 10:24:36 -0800824 && canCaptureIfInCallOrCommunication(current)) {
Eric Laurent1ff16a72019-03-14 18:35:04 -0700825 allowCapture = true;
Eric Laurent4eb58f12018-12-07 16:41:02 -0800826 }
827 }
Philip P. Moltmannbda45752020-07-17 16:41:18 -0700828 } else if (mUidPolicy->isA11yUid(currentUid)) {
Eric Laurent1ff16a72019-03-14 18:35:04 -0700829 // For accessibility service allow capture if:
Eric Laurent47670c92019-08-28 16:59:05 -0700830 // The assistant is not on TOP
831 // AND there is no active privacy sensitive capture or call
Eric Laurent589171c2019-07-25 18:04:29 -0700832 // OR client has CAPTURE_AUDIO_OUTPUT privileged permission
Eric Laurent47670c92019-08-28 16:59:05 -0700833 // OR
834 // Is on TOP AND the source is VOICE_RECOGNITION or HOTWORD
835 if (!isAssistantOnTop
Ricardo Correa57a37692020-03-23 17:27:25 -0700836 && !(isSensitiveActive && !current->canCaptureOutput)
Eric Laurentc21d5692020-02-25 10:24:36 -0800837 && canCaptureIfInCallOrCommunication(current)) {
Eric Laurent47670c92019-08-28 16:59:05 -0700838 allowCapture = true;
839 }
Eric Laurent589171c2019-07-25 18:04:29 -0700840 if (isA11yOnTop) {
841 if (source == AUDIO_SOURCE_VOICE_RECOGNITION || source == AUDIO_SOURCE_HOTWORD) {
842 allowCapture = true;
843 }
Eric Laurent4eb58f12018-12-07 16:41:02 -0800844 }
Eric Laurent4e947da2019-10-17 15:24:06 -0700845 } else if (source == AUDIO_SOURCE_HOTWORD) {
846 // For HOTWORD source allow capture when not on TOP if:
847 // All active clients are using HOTWORD source
848 // AND no call is active
849 // OR client has CAPTURE_AUDIO_OUTPUT privileged permission
Eric Laurentc21d5692020-02-25 10:24:36 -0800850 if (onlyHotwordActive
851 && canCaptureIfInCallOrCommunication(current)) {
Eric Laurent4e947da2019-10-17 15:24:06 -0700852 allowCapture = true;
853 }
Philip P. Moltmannbda45752020-07-17 16:41:18 -0700854 } else if (mUidPolicy->isCurrentImeUid(currentUid)) {
Kohsuke Yatoha623a132020-03-24 20:10:26 -0700855 // For current InputMethodService allow capture if:
856 // A RTT call is active AND the source is VOICE_RECOGNITION
857 if (rttCallActive && source == AUDIO_SOURCE_VOICE_RECOGNITION) {
858 allowCapture = true;
859 }
Eric Laurent4eb58f12018-12-07 16:41:02 -0800860 }
Eric Laurent8c7ef892021-06-10 13:32:16 +0200861 setAppState_l(current,
Philip P. Moltmannbda45752020-07-17 16:41:18 -0700862 allowCapture ? apmStatFromAmState(mUidPolicy->getUidState(currentUid)) :
Eric Laurent1ff16a72019-03-14 18:35:04 -0700863 APP_STATE_IDLE);
Eric Laurente8c8b432018-10-17 10:08:02 -0700864 }
865}
866
Michael Groovercfd28302018-12-11 19:16:46 -0800867void AudioPolicyService::silenceAllRecordings_l() {
868 for (size_t i = 0; i < mAudioRecordClients.size(); i++) {
869 sp<AudioRecordClient> current = mAudioRecordClients[i];
Eric Laurent1ff16a72019-03-14 18:35:04 -0700870 if (!isVirtualSource(current->attributes.source)) {
Eric Laurent8c7ef892021-06-10 13:32:16 +0200871 setAppState_l(current, APP_STATE_IDLE);
Eric Laurent1ff16a72019-03-14 18:35:04 -0700872 }
Michael Groovercfd28302018-12-11 19:16:46 -0800873 }
874}
875
Eric Laurente8c8b432018-10-17 10:08:02 -0700876/* static */
877app_state_t AudioPolicyService::apmStatFromAmState(int amState) {
Eric Laurent1ff16a72019-03-14 18:35:04 -0700878
879 if (amState == ActivityManager::PROCESS_STATE_UNKNOWN) {
Eric Laurente8c8b432018-10-17 10:08:02 -0700880 return APP_STATE_IDLE;
Eric Laurent1ff16a72019-03-14 18:35:04 -0700881 } else if (amState <= ActivityManager::PROCESS_STATE_TOP) {
882 // include persistent services
883 return APP_STATE_TOP;
Eric Laurente8c8b432018-10-17 10:08:02 -0700884 }
885 return APP_STATE_FOREGROUND;
886}
887
Eric Laurent4eb58f12018-12-07 16:41:02 -0800888/* static */
Eric Laurent2dc962b2019-03-01 08:25:25 -0800889bool AudioPolicyService::isVirtualSource(audio_source_t source)
Eric Laurent4eb58f12018-12-07 16:41:02 -0800890{
891 switch (source) {
892 case AUDIO_SOURCE_VOICE_UPLINK:
893 case AUDIO_SOURCE_VOICE_DOWNLINK:
894 case AUDIO_SOURCE_VOICE_CALL:
Eric Laurent2dc962b2019-03-01 08:25:25 -0800895 case AUDIO_SOURCE_REMOTE_SUBMIX:
896 case AUDIO_SOURCE_FM_TUNER:
Eric Laurent68eb2122020-04-30 17:40:57 -0700897 case AUDIO_SOURCE_ECHO_REFERENCE:
Eric Laurent4eb58f12018-12-07 16:41:02 -0800898 return true;
899 default:
900 break;
901 }
902 return false;
903}
904
Eric Laurented726cc2021-07-01 14:26:41 +0200905/* static */
906bool AudioPolicyService::isAppOpSource(audio_source_t source)
907{
908 switch (source) {
909 case AUDIO_SOURCE_FM_TUNER:
910 case AUDIO_SOURCE_ECHO_REFERENCE:
Eric Laurent637bd202021-09-22 11:17:11 +0200911 case AUDIO_SOURCE_REMOTE_SUBMIX:
Eric Laurented726cc2021-07-01 14:26:41 +0200912 return false;
913 default:
914 break;
915 }
916 return true;
917}
918
Eric Laurent8c7ef892021-06-10 13:32:16 +0200919void AudioPolicyService::setAppState_l(sp<AudioRecordClient> client, app_state_t state)
Eric Laurente8c8b432018-10-17 10:08:02 -0700920{
921 AutoCallerClear acc;
922
923 if (mAudioPolicyManager) {
Eric Laurent8c7ef892021-06-10 13:32:16 +0200924 mAudioPolicyManager->setAppState(client->portId, state);
Mikhail Naganoveae73eb2018-04-03 16:57:36 -0700925 }
926 sp<IAudioFlinger> af = AudioSystem::get_audio_flinger();
927 if (af) {
Eric Laurentf32108e2018-10-04 17:22:04 -0700928 bool silenced = state == APP_STATE_IDLE;
Eric Laurent8c7ef892021-06-10 13:32:16 +0200929 if (client->silenced != silenced) {
930 if (client->active) {
931 if (silenced) {
932 finishRecording(client->attributionSource, client->attributes.source);
933 } else {
934 std::stringstream msg;
935 msg << "Audio recording un-silenced on session " << client->session;
936 if (!startRecording(client->attributionSource, String16(msg.str().c_str()),
937 client->attributes.source)) {
938 silenced = true;
939 }
940 }
941 }
942 af->setRecordSilenced(client->portId, silenced);
943 client->silenced = silenced;
944 }
Mikhail Naganoveae73eb2018-04-03 16:57:36 -0700945 }
Svet Ganovf4ddfef2018-01-16 07:37:58 -0800946}
947
Glenn Kasten0f11b512014-01-31 16:18:54 -0800948status_t AudioPolicyService::dump(int fd, const Vector<String16>& args __unused)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700949{
Glenn Kasten44deb052012-02-05 18:09:08 -0800950 if (!dumpAllowed()) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700951 dumpPermissionDenial(fd);
952 } else {
Mikhail Naganov12b716c2020-04-30 22:37:43 +0000953 const bool locked = dumpTryLock(mLock);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700954 if (!locked) {
955 String8 result(kDeadlockedString);
956 write(fd, result.string(), result.size());
957 }
958
959 dumpInternals(fd);
Glenn Kasten9d1f02d2012-02-08 17:47:58 -0800960 if (mAudioCommandThread != 0) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700961 mAudioCommandThread->dump(fd);
962 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700963
Eric Laurentdce54a12014-03-10 12:19:46 -0700964 if (mAudioPolicyManager) {
965 mAudioPolicyManager->dump(fd);
966 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700967
Kevin Rocard8be94972019-02-22 13:26:25 -0800968 mPackageManager.dump(fd);
969
Mikhail Naganov12b716c2020-04-30 22:37:43 +0000970 dumpReleaseLock(mLock, locked);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700971 }
972 return NO_ERROR;
973}
974
975status_t AudioPolicyService::dumpPermissionDenial(int fd)
976{
977 const size_t SIZE = 256;
978 char buffer[SIZE];
979 String8 result;
980 snprintf(buffer, SIZE, "Permission Denial: "
981 "can't dump AudioPolicyService from pid=%d, uid=%d\n",
982 IPCThreadState::self()->getCallingPid(),
983 IPCThreadState::self()->getCallingUid());
984 result.append(buffer);
985 write(fd, result.string(), result.size());
986 return NO_ERROR;
987}
988
989status_t AudioPolicyService::onTransact(
Svet Ganovf4ddfef2018-01-16 07:37:58 -0800990 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags) {
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800991 // make sure transactions reserved to AudioFlinger do not come from other processes
992 switch (code) {
993 case TRANSACTION_startOutput:
994 case TRANSACTION_stopOutput:
995 case TRANSACTION_releaseOutput:
996 case TRANSACTION_getInputForAttr:
997 case TRANSACTION_startInput:
998 case TRANSACTION_stopInput:
999 case TRANSACTION_releaseInput:
1000 case TRANSACTION_getOutputForEffect:
1001 case TRANSACTION_registerEffect:
1002 case TRANSACTION_unregisterEffect:
1003 case TRANSACTION_setEffectEnabled:
1004 case TRANSACTION_getStrategyForStream:
1005 case TRANSACTION_getOutputForAttr:
1006 case TRANSACTION_moveEffectsToIo:
1007 ALOGW("%s: transaction %d received from PID %d",
1008 __func__, code, IPCThreadState::self()->getCallingPid());
1009 return INVALID_OPERATION;
1010 default:
1011 break;
1012 }
1013
1014 // make sure the following transactions come from system components
1015 switch (code) {
1016 case TRANSACTION_setDeviceConnectionState:
1017 case TRANSACTION_handleDeviceConfigChange:
1018 case TRANSACTION_setPhoneState:
1019//FIXME: Allow setForceUse calls from system apps until a better use case routing API is available
1020// case TRANSACTION_setForceUse:
1021 case TRANSACTION_initStreamVolume:
1022 case TRANSACTION_setStreamVolumeIndex:
1023 case TRANSACTION_setVolumeIndexForAttributes:
1024 case TRANSACTION_getStreamVolumeIndex:
1025 case TRANSACTION_getVolumeIndexForAttributes:
1026 case TRANSACTION_getMinVolumeIndexForAttributes:
1027 case TRANSACTION_getMaxVolumeIndexForAttributes:
1028 case TRANSACTION_isStreamActive:
1029 case TRANSACTION_isStreamActiveRemotely:
1030 case TRANSACTION_isSourceActive:
1031 case TRANSACTION_getDevicesForStream:
1032 case TRANSACTION_registerPolicyMixes:
1033 case TRANSACTION_setMasterMono:
1034 case TRANSACTION_getSurroundFormats:
Kriti Dang6537def2021-03-02 13:46:59 +01001035 case TRANSACTION_getReportedSurroundFormats:
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001036 case TRANSACTION_setSurroundFormatEnabled:
1037 case TRANSACTION_setAssistantUid:
1038 case TRANSACTION_setA11yServicesUids:
1039 case TRANSACTION_setUidDeviceAffinities:
1040 case TRANSACTION_removeUidDeviceAffinities:
1041 case TRANSACTION_setUserIdDeviceAffinities:
1042 case TRANSACTION_removeUserIdDeviceAffinities:
1043 case TRANSACTION_getHwOffloadEncodingFormatsSupportedForA2DP:
1044 case TRANSACTION_listAudioVolumeGroups:
1045 case TRANSACTION_getVolumeGroupFromAudioAttributes:
1046 case TRANSACTION_acquireSoundTriggerSession:
1047 case TRANSACTION_releaseSoundTriggerSession:
1048 case TRANSACTION_setRttEnabled:
1049 case TRANSACTION_isCallScreenModeSupported:
1050 case TRANSACTION_setDevicesRoleForStrategy:
1051 case TRANSACTION_setSupportedSystemUsages:
1052 case TRANSACTION_removeDevicesRoleForStrategy:
1053 case TRANSACTION_getDevicesForRoleAndStrategy:
1054 case TRANSACTION_getDevicesForAttributes:
1055 case TRANSACTION_setAllowedCapturePolicy:
1056 case TRANSACTION_onNewAudioModulesAvailable:
1057 case TRANSACTION_setCurrentImeUid:
1058 case TRANSACTION_registerSoundTriggerCaptureStateListener:
1059 case TRANSACTION_setDevicesRoleForCapturePreset:
1060 case TRANSACTION_addDevicesRoleForCapturePreset:
1061 case TRANSACTION_removeDevicesRoleForCapturePreset:
1062 case TRANSACTION_clearDevicesRoleForCapturePreset:
Eric Laurent81dd0f52021-07-05 11:54:40 +02001063 case TRANSACTION_getDevicesForRoleAndCapturePreset:
1064 case TRANSACTION_getSpatializer: {
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001065 if (!isServiceUid(IPCThreadState::self()->getCallingUid())) {
1066 ALOGW("%s: transaction %d received from PID %d unauthorized UID %d",
1067 __func__, code, IPCThreadState::self()->getCallingPid(),
1068 IPCThreadState::self()->getCallingUid());
1069 return INVALID_OPERATION;
1070 }
1071 } break;
1072 default:
1073 break;
1074 }
1075
1076 std::string tag("IAudioPolicyService command " + std::to_string(code));
1077 TimeCheck check(tag.c_str());
1078
Svet Ganovf4ddfef2018-01-16 07:37:58 -08001079 switch (code) {
1080 case SHELL_COMMAND_TRANSACTION: {
1081 int in = data.readFileDescriptor();
1082 int out = data.readFileDescriptor();
1083 int err = data.readFileDescriptor();
1084 int argc = data.readInt32();
1085 Vector<String16> args;
1086 for (int i = 0; i < argc && data.dataAvail() > 0; i++) {
1087 args.add(data.readString16());
1088 }
1089 sp<IBinder> unusedCallback;
1090 sp<IResultReceiver> resultReceiver;
1091 status_t status;
1092 if ((status = data.readNullableStrongBinder(&unusedCallback)) != NO_ERROR) {
1093 return status;
1094 }
1095 if ((status = data.readNullableStrongBinder(&resultReceiver)) != NO_ERROR) {
1096 return status;
1097 }
1098 status = shellCommand(in, out, err, args);
1099 if (resultReceiver != nullptr) {
1100 resultReceiver->send(status);
1101 }
1102 return NO_ERROR;
1103 }
1104 }
1105
Mathias Agopian65ab4712010-07-14 17:59:35 -07001106 return BnAudioPolicyService::onTransact(code, data, reply, flags);
1107}
1108
Svet Ganovf4ddfef2018-01-16 07:37:58 -08001109// ------------------- Shell command implementation -------------------
1110
1111// NOTE: This is a remote API - make sure all args are validated
1112status_t AudioPolicyService::shellCommand(int in, int out, int err, Vector<String16>& args) {
1113 if (!checkCallingPermission(sManageAudioPolicyPermission, nullptr, nullptr)) {
1114 return PERMISSION_DENIED;
1115 }
1116 if (in == BAD_TYPE || out == BAD_TYPE || err == BAD_TYPE) {
1117 return BAD_VALUE;
1118 }
jovanakbe066e12019-09-02 11:54:39 -07001119 if (args.size() >= 3 && args[0] == String16("set-uid-state")) {
Svet Ganovf4ddfef2018-01-16 07:37:58 -08001120 return handleSetUidState(args, err);
jovanakbe066e12019-09-02 11:54:39 -07001121 } else if (args.size() >= 2 && args[0] == String16("reset-uid-state")) {
Svet Ganovf4ddfef2018-01-16 07:37:58 -08001122 return handleResetUidState(args, err);
jovanakbe066e12019-09-02 11:54:39 -07001123 } else if (args.size() >= 2 && args[0] == String16("get-uid-state")) {
Svet Ganovf4ddfef2018-01-16 07:37:58 -08001124 return handleGetUidState(args, out, err);
Eric Laurent269acb42021-04-23 16:53:22 +02001125 } else if (args.size() >= 1 && args[0] == String16("purge_permission-cache")) {
1126 purgePermissionCache();
1127 return NO_ERROR;
Svet Ganovf4ddfef2018-01-16 07:37:58 -08001128 } else if (args.size() == 1 && args[0] == String16("help")) {
1129 printHelp(out);
1130 return NO_ERROR;
1131 }
1132 printHelp(err);
1133 return BAD_VALUE;
1134}
1135
jovanakbe066e12019-09-02 11:54:39 -07001136static status_t getUidForPackage(String16 packageName, int userId, /*inout*/uid_t& uid, int err) {
1137 if (userId < 0) {
1138 ALOGE("Invalid user: %d", userId);
1139 dprintf(err, "Invalid user: %d\n", userId);
Svet Ganovf4ddfef2018-01-16 07:37:58 -08001140 return BAD_VALUE;
1141 }
jovanakbe066e12019-09-02 11:54:39 -07001142
1143 PermissionController pc;
1144 uid = pc.getPackageUid(packageName, 0);
1145 if (uid <= 0) {
1146 ALOGE("Unknown package: '%s'", String8(packageName).string());
1147 dprintf(err, "Unknown package: '%s'\n", String8(packageName).string());
1148 return BAD_VALUE;
1149 }
1150
1151 uid = multiuser_get_uid(userId, uid);
1152 return NO_ERROR;
1153}
1154
1155status_t AudioPolicyService::handleSetUidState(Vector<String16>& args, int err) {
1156 // Valid arg.size() is 3 or 5, args.size() is 5 with --user option.
1157 if (!(args.size() == 3 || args.size() == 5)) {
1158 printHelp(err);
1159 return BAD_VALUE;
1160 }
1161
Svet Ganovf4ddfef2018-01-16 07:37:58 -08001162 bool active = false;
1163 if (args[2] == String16("active")) {
1164 active = true;
1165 } else if ((args[2] != String16("idle"))) {
1166 ALOGE("Expected active or idle but got: '%s'", String8(args[2]).string());
1167 return BAD_VALUE;
1168 }
jovanakbe066e12019-09-02 11:54:39 -07001169
1170 int userId = 0;
1171 if (args.size() >= 5 && args[3] == String16("--user")) {
1172 userId = atoi(String8(args[4]));
1173 }
1174
1175 uid_t uid;
1176 if (getUidForPackage(args[1], userId, uid, err) == BAD_VALUE) {
1177 return BAD_VALUE;
1178 }
1179
Mikhail Naganov12b716c2020-04-30 22:37:43 +00001180 sp<UidPolicy> uidPolicy;
1181 {
1182 Mutex::Autolock _l(mLock);
1183 uidPolicy = mUidPolicy;
1184 }
1185 if (uidPolicy) {
1186 uidPolicy->addOverrideUid(uid, active);
1187 return NO_ERROR;
1188 }
1189 return NO_INIT;
Svet Ganovf4ddfef2018-01-16 07:37:58 -08001190}
1191
1192status_t AudioPolicyService::handleResetUidState(Vector<String16>& args, int err) {
jovanakbe066e12019-09-02 11:54:39 -07001193 // Valid arg.size() is 2 or 4, args.size() is 4 with --user option.
1194 if (!(args.size() == 2 || args.size() == 4)) {
1195 printHelp(err);
Svet Ganovf4ddfef2018-01-16 07:37:58 -08001196 return BAD_VALUE;
1197 }
jovanakbe066e12019-09-02 11:54:39 -07001198
1199 int userId = 0;
1200 if (args.size() >= 4 && args[2] == String16("--user")) {
1201 userId = atoi(String8(args[3]));
1202 }
1203
1204 uid_t uid;
1205 if (getUidForPackage(args[1], userId, uid, err) == BAD_VALUE) {
1206 return BAD_VALUE;
1207 }
1208
Mikhail Naganov12b716c2020-04-30 22:37:43 +00001209 sp<UidPolicy> uidPolicy;
1210 {
1211 Mutex::Autolock _l(mLock);
1212 uidPolicy = mUidPolicy;
1213 }
1214 if (uidPolicy) {
1215 uidPolicy->removeOverrideUid(uid);
1216 return NO_ERROR;
1217 }
1218 return NO_INIT;
Svet Ganovf4ddfef2018-01-16 07:37:58 -08001219}
1220
1221status_t AudioPolicyService::handleGetUidState(Vector<String16>& args, int out, int err) {
jovanakbe066e12019-09-02 11:54:39 -07001222 // Valid arg.size() is 2 or 4, args.size() is 4 with --user option.
1223 if (!(args.size() == 2 || args.size() == 4)) {
1224 printHelp(err);
Svet Ganovf4ddfef2018-01-16 07:37:58 -08001225 return BAD_VALUE;
1226 }
jovanakbe066e12019-09-02 11:54:39 -07001227
1228 int userId = 0;
1229 if (args.size() >= 4 && args[2] == String16("--user")) {
1230 userId = atoi(String8(args[3]));
1231 }
1232
1233 uid_t uid;
1234 if (getUidForPackage(args[1], userId, uid, err) == BAD_VALUE) {
1235 return BAD_VALUE;
1236 }
1237
Mikhail Naganov12b716c2020-04-30 22:37:43 +00001238 sp<UidPolicy> uidPolicy;
1239 {
1240 Mutex::Autolock _l(mLock);
1241 uidPolicy = mUidPolicy;
Svet Ganovf4ddfef2018-01-16 07:37:58 -08001242 }
Mikhail Naganov12b716c2020-04-30 22:37:43 +00001243 if (uidPolicy) {
1244 return dprintf(out, uidPolicy->isUidActive(uid) ? "active\n" : "idle\n");
1245 }
1246 return NO_INIT;
Svet Ganovf4ddfef2018-01-16 07:37:58 -08001247}
1248
1249status_t AudioPolicyService::printHelp(int out) {
1250 return dprintf(out, "Audio policy service commands:\n"
jovanakbe066e12019-09-02 11:54:39 -07001251 " get-uid-state <PACKAGE> [--user USER_ID] gets the uid state\n"
1252 " set-uid-state <PACKAGE> <active|idle> [--user USER_ID] overrides the uid state\n"
1253 " reset-uid-state <PACKAGE> [--user USER_ID] clears the uid state override\n"
Svet Ganovf4ddfef2018-01-16 07:37:58 -08001254 " help print this message\n");
1255}
1256
1257// ----------- AudioPolicyService::UidPolicy implementation ----------
1258
1259void AudioPolicyService::UidPolicy::registerSelf() {
Steven Moreland2f348142019-07-02 15:59:07 -07001260 status_t res = mAm.linkToDeath(this);
1261 mAm.registerUidObserver(this, ActivityManager::UID_OBSERVER_GONE
Svet Ganovf4ddfef2018-01-16 07:37:58 -08001262 | ActivityManager::UID_OBSERVER_IDLE
Eric Laurente8c8b432018-10-17 10:08:02 -07001263 | ActivityManager::UID_OBSERVER_ACTIVE
1264 | ActivityManager::UID_OBSERVER_PROCSTATE,
Svet Ganovf4ddfef2018-01-16 07:37:58 -08001265 ActivityManager::PROCESS_STATE_UNKNOWN,
1266 String16("audioserver"));
Mikhail Naganoveae73eb2018-04-03 16:57:36 -07001267 if (!res) {
1268 Mutex::Autolock _l(mLock);
1269 mObserverRegistered = true;
1270 } else {
1271 ALOGE("UidPolicy::registerSelf linkToDeath failed: %d", res);
Eric Laurent4eb58f12018-12-07 16:41:02 -08001272
Steven Moreland2f348142019-07-02 15:59:07 -07001273 mAm.unregisterUidObserver(this);
Mikhail Naganoveae73eb2018-04-03 16:57:36 -07001274 }
Svet Ganovf4ddfef2018-01-16 07:37:58 -08001275}
1276
1277void AudioPolicyService::UidPolicy::unregisterSelf() {
Steven Moreland2f348142019-07-02 15:59:07 -07001278 mAm.unlinkToDeath(this);
1279 mAm.unregisterUidObserver(this);
Mikhail Naganoveae73eb2018-04-03 16:57:36 -07001280 Mutex::Autolock _l(mLock);
1281 mObserverRegistered = false;
Svet Ganovf4ddfef2018-01-16 07:37:58 -08001282}
1283
Mikhail Naganoveae73eb2018-04-03 16:57:36 -07001284void AudioPolicyService::UidPolicy::binderDied(__unused const wp<IBinder> &who) {
1285 Mutex::Autolock _l(mLock);
1286 mCachedUids.clear();
1287 mObserverRegistered = false;
Svet Ganovf4ddfef2018-01-16 07:37:58 -08001288}
1289
Eric Laurente8c8b432018-10-17 10:08:02 -07001290void AudioPolicyService::UidPolicy::checkRegistered() {
Mikhail Naganoveae73eb2018-04-03 16:57:36 -07001291 bool needToReregister = false;
1292 {
1293 Mutex::Autolock _l(mLock);
1294 needToReregister = !mObserverRegistered;
Svet Ganovf4ddfef2018-01-16 07:37:58 -08001295 }
Mikhail Naganoveae73eb2018-04-03 16:57:36 -07001296 if (needToReregister) {
1297 // Looks like ActivityManager has died previously, attempt to re-register.
1298 registerSelf();
1299 }
Eric Laurente8c8b432018-10-17 10:08:02 -07001300}
1301
1302bool AudioPolicyService::UidPolicy::isUidActive(uid_t uid) {
1303 if (isServiceUid(uid)) return true;
1304 checkRegistered();
Mikhail Naganoveae73eb2018-04-03 16:57:36 -07001305 {
1306 Mutex::Autolock _l(mLock);
1307 auto overrideIter = mOverrideUids.find(uid);
1308 if (overrideIter != mOverrideUids.end()) {
Eric Laurente8c8b432018-10-17 10:08:02 -07001309 return overrideIter->second.first;
Mikhail Naganoveae73eb2018-04-03 16:57:36 -07001310 }
1311 // In an absense of the ActivityManager, assume everything to be active.
1312 if (!mObserverRegistered) return true;
1313 auto cacheIter = mCachedUids.find(uid);
Mikhail Naganoveba668a2018-04-05 08:13:15 -07001314 if (cacheIter != mCachedUids.end()) {
Eric Laurente8c8b432018-10-17 10:08:02 -07001315 return cacheIter->second.first;
Mikhail Naganoveae73eb2018-04-03 16:57:36 -07001316 }
1317 }
1318 ActivityManager am;
Hui Yu12c7ec72020-05-04 17:40:52 +00001319 bool active = am.isUidActive(uid, String16("audioserver"));
Mikhail Naganoveae73eb2018-04-03 16:57:36 -07001320 {
1321 Mutex::Autolock _l(mLock);
Eric Laurente8c8b432018-10-17 10:08:02 -07001322 mCachedUids.insert(std::pair<uid_t,
1323 std::pair<bool, int>>(uid, std::pair<bool, int>(active,
1324 ActivityManager::PROCESS_STATE_UNKNOWN)));
Mikhail Naganoveae73eb2018-04-03 16:57:36 -07001325 }
1326 return active;
Svet Ganovf4ddfef2018-01-16 07:37:58 -08001327}
1328
Eric Laurente8c8b432018-10-17 10:08:02 -07001329int AudioPolicyService::UidPolicy::getUidState(uid_t uid) {
1330 if (isServiceUid(uid)) {
1331 return ActivityManager::PROCESS_STATE_TOP;
1332 }
1333 checkRegistered();
1334 {
1335 Mutex::Autolock _l(mLock);
1336 auto overrideIter = mOverrideUids.find(uid);
1337 if (overrideIter != mOverrideUids.end()) {
1338 if (overrideIter->second.first) {
1339 if (overrideIter->second.second != ActivityManager::PROCESS_STATE_UNKNOWN) {
1340 return overrideIter->second.second;
1341 } else {
1342 auto cacheIter = mCachedUids.find(uid);
1343 if (cacheIter != mCachedUids.end()) {
1344 return cacheIter->second.second;
1345 }
1346 }
1347 }
1348 return ActivityManager::PROCESS_STATE_UNKNOWN;
1349 }
1350 // In an absense of the ActivityManager, assume everything to be active.
1351 if (!mObserverRegistered) {
1352 return ActivityManager::PROCESS_STATE_TOP;
1353 }
1354 auto cacheIter = mCachedUids.find(uid);
1355 if (cacheIter != mCachedUids.end()) {
1356 if (cacheIter->second.first) {
1357 return cacheIter->second.second;
1358 } else {
1359 return ActivityManager::PROCESS_STATE_UNKNOWN;
1360 }
1361 }
1362 }
1363 ActivityManager am;
Hui Yu12c7ec72020-05-04 17:40:52 +00001364 bool active = am.isUidActive(uid, String16("audioserver"));
Eric Laurente8c8b432018-10-17 10:08:02 -07001365 int state = ActivityManager::PROCESS_STATE_UNKNOWN;
1366 if (active) {
1367 state = am.getUidProcessState(uid, String16("audioserver"));
1368 }
1369 {
1370 Mutex::Autolock _l(mLock);
1371 mCachedUids.insert(std::pair<uid_t,
1372 std::pair<bool, int>>(uid, std::pair<bool, int>(active, state)));
1373 }
Eric Laurent4eb58f12018-12-07 16:41:02 -08001374
Eric Laurente8c8b432018-10-17 10:08:02 -07001375 return state;
1376}
1377
Mikhail Naganoveae73eb2018-04-03 16:57:36 -07001378void AudioPolicyService::UidPolicy::onUidActive(uid_t uid) {
Eric Laurente8c8b432018-10-17 10:08:02 -07001379 updateUid(&mCachedUids, uid, true, ActivityManager::PROCESS_STATE_UNKNOWN, true);
Mikhail Naganoveae73eb2018-04-03 16:57:36 -07001380}
1381
1382void AudioPolicyService::UidPolicy::onUidGone(uid_t uid, __unused bool disabled) {
Eric Laurente8c8b432018-10-17 10:08:02 -07001383 updateUid(&mCachedUids, uid, false, ActivityManager::PROCESS_STATE_UNKNOWN, false);
Mikhail Naganoveae73eb2018-04-03 16:57:36 -07001384}
1385
1386void AudioPolicyService::UidPolicy::onUidIdle(uid_t uid, __unused bool disabled) {
Eric Laurente8c8b432018-10-17 10:08:02 -07001387 updateUid(&mCachedUids, uid, false, ActivityManager::PROCESS_STATE_UNKNOWN, true);
Mikhail Naganoveae73eb2018-04-03 16:57:36 -07001388}
1389
Eric Laurente8c8b432018-10-17 10:08:02 -07001390void AudioPolicyService::UidPolicy::onUidStateChanged(uid_t uid,
1391 int32_t procState,
Hui Yu13ad0eb2019-09-09 10:27:07 -07001392 int64_t procStateSeq __unused,
1393 int32_t capability __unused) {
Eric Laurente8c8b432018-10-17 10:08:02 -07001394 if (procState != ActivityManager::PROCESS_STATE_UNKNOWN) {
1395 updateUid(&mCachedUids, uid, true, procState, true);
Mikhail Naganoveae73eb2018-04-03 16:57:36 -07001396 }
1397}
1398
1399void AudioPolicyService::UidPolicy::updateOverrideUid(uid_t uid, bool active, bool insert) {
Eric Laurente8c8b432018-10-17 10:08:02 -07001400 updateUid(&mOverrideUids, uid, active, ActivityManager::PROCESS_STATE_UNKNOWN, insert);
1401}
1402
1403void AudioPolicyService::UidPolicy::notifyService() {
1404 sp<AudioPolicyService> service = mService.promote();
1405 if (service != nullptr) {
1406 service->updateUidStates();
Mikhail Naganoveae73eb2018-04-03 16:57:36 -07001407 }
1408}
1409
Eric Laurente8c8b432018-10-17 10:08:02 -07001410void AudioPolicyService::UidPolicy::updateUid(std::unordered_map<uid_t,
1411 std::pair<bool, int>> *uids,
1412 uid_t uid,
1413 bool active,
1414 int state,
1415 bool insert) {
1416 if (isServiceUid(uid)) {
1417 return;
1418 }
1419 bool wasActive = isUidActive(uid);
1420 int previousState = getUidState(uid);
Mikhail Naganoveae73eb2018-04-03 16:57:36 -07001421 {
1422 Mutex::Autolock _l(mLock);
Eric Laurente8c8b432018-10-17 10:08:02 -07001423 updateUidLocked(uids, uid, active, state, insert);
Mikhail Naganoveae73eb2018-04-03 16:57:36 -07001424 }
Eric Laurente8c8b432018-10-17 10:08:02 -07001425 if (wasActive != isUidActive(uid) || state != previousState) {
1426 notifyService();
1427 }
Mikhail Naganoveae73eb2018-04-03 16:57:36 -07001428}
1429
Eric Laurente8c8b432018-10-17 10:08:02 -07001430void AudioPolicyService::UidPolicy::updateUidLocked(std::unordered_map<uid_t,
1431 std::pair<bool, int>> *uids,
1432 uid_t uid,
1433 bool active,
1434 int state,
1435 bool insert) {
Mikhail Naganoveae73eb2018-04-03 16:57:36 -07001436 auto it = uids->find(uid);
1437 if (it != uids->end()) {
Mikhail Naganoveae73eb2018-04-03 16:57:36 -07001438 if (insert) {
Eric Laurente8c8b432018-10-17 10:08:02 -07001439 if (state == ActivityManager::PROCESS_STATE_UNKNOWN) {
1440 it->second.first = active;
1441 }
1442 if (it->second.first) {
1443 it->second.second = state;
1444 } else {
1445 it->second.second = ActivityManager::PROCESS_STATE_UNKNOWN;
1446 }
Mikhail Naganoveae73eb2018-04-03 16:57:36 -07001447 } else {
1448 uids->erase(it);
1449 }
Eric Laurente8c8b432018-10-17 10:08:02 -07001450 } else if (insert && (state == ActivityManager::PROCESS_STATE_UNKNOWN)) {
1451 uids->insert(std::pair<uid_t, std::pair<bool, int>>(uid,
1452 std::pair<bool, int>(active, state)));
Mikhail Naganoveae73eb2018-04-03 16:57:36 -07001453 }
Svet Ganovf4ddfef2018-01-16 07:37:58 -08001454}
Mathias Agopian65ab4712010-07-14 17:59:35 -07001455
Eric Laurent4eb58f12018-12-07 16:41:02 -08001456bool AudioPolicyService::UidPolicy::isA11yOnTop() {
1457 for (const auto &uid : mCachedUids) {
Eric Laurent47670c92019-08-28 16:59:05 -07001458 if (!isA11yUid(uid.first)) {
Eric Laurent4eb58f12018-12-07 16:41:02 -08001459 continue;
1460 }
Amith Yamasanibcbb3002019-01-23 13:53:33 -08001461 if (uid.second.second >= ActivityManager::PROCESS_STATE_TOP
1462 && uid.second.second <= ActivityManager::PROCESS_STATE_BOUND_FOREGROUND_SERVICE) {
Eric Laurent4eb58f12018-12-07 16:41:02 -08001463 return true;
1464 }
1465 }
1466 return false;
1467}
1468
Eric Laurentb78763e2018-10-17 10:08:02 -07001469bool AudioPolicyService::UidPolicy::isA11yUid(uid_t uid)
1470{
1471 std::vector<uid_t>::iterator it = find(mA11yUids.begin(), mA11yUids.end(), uid);
1472 return it != mA11yUids.end();
1473}
1474
Michael Groovercfd28302018-12-11 19:16:46 -08001475// ----------- AudioPolicyService::SensorPrivacyService implementation ----------
1476void AudioPolicyService::SensorPrivacyPolicy::registerSelf() {
1477 SensorPrivacyManager spm;
1478 mSensorPrivacyEnabled = spm.isSensorPrivacyEnabled();
1479 spm.addSensorPrivacyListener(this);
1480}
1481
Evan Severson241d9592021-01-08 12:16:02 -08001482void AudioPolicyService::SensorPrivacyPolicy::registerSelfForMicrophoneOnly(int userId) {
1483 SensorPrivacyManager spm;
1484 mSensorPrivacyEnabled = spm.isIndividualSensorPrivacyEnabled(userId,
1485 SensorPrivacyManager::INDIVIDUAL_SENSOR_MICROPHONE);
1486 spm.addIndividualSensorPrivacyListener(userId,
1487 SensorPrivacyManager::INDIVIDUAL_SENSOR_MICROPHONE, this);
1488}
1489
Michael Groovercfd28302018-12-11 19:16:46 -08001490void AudioPolicyService::SensorPrivacyPolicy::unregisterSelf() {
1491 SensorPrivacyManager spm;
1492 spm.removeSensorPrivacyListener(this);
1493}
1494
1495bool AudioPolicyService::SensorPrivacyPolicy::isSensorPrivacyEnabled() {
1496 return mSensorPrivacyEnabled;
1497}
1498
1499binder::Status AudioPolicyService::SensorPrivacyPolicy::onSensorPrivacyChanged(bool enabled) {
1500 mSensorPrivacyEnabled = enabled;
1501 sp<AudioPolicyService> service = mService.promote();
1502 if (service != nullptr) {
1503 service->updateUidStates();
1504 }
1505 return binder::Status::ok();
1506}
1507
Eric Laurented726cc2021-07-01 14:26:41 +02001508// ----------- AudioPolicyService::OpRecordAudioMonitor implementation ----------
1509
1510// static
1511sp<AudioPolicyService::OpRecordAudioMonitor>
1512AudioPolicyService::OpRecordAudioMonitor::createIfNeeded(
1513 const AttributionSourceState& attributionSource, const audio_attributes_t& attr,
1514 wp<AudioCommandThread> commandThread)
1515{
Eric Laurent987ce102021-07-05 12:11:51 +02001516 if (isAudioServerOrRootUid(attributionSource.uid)) {
1517 ALOGV("not silencing record for audio or root source %s",
Eric Laurented726cc2021-07-01 14:26:41 +02001518 attributionSource.toString().c_str());
1519 return nullptr;
1520 }
1521
1522 if (!AudioPolicyService::isAppOpSource(attr.source)) {
1523 ALOGD("not monitoring app op for uid %d and source %d",
1524 attributionSource.uid, attr.source);
1525 return nullptr;
1526 }
1527
1528 if (!attributionSource.packageName.has_value()
1529 || attributionSource.packageName.value().size() == 0) {
1530 return nullptr;
1531 }
1532 return new OpRecordAudioMonitor(attributionSource, getOpForSource(attr.source), commandThread);
1533}
1534
1535AudioPolicyService::OpRecordAudioMonitor::OpRecordAudioMonitor(
1536 const AttributionSourceState& attributionSource, int32_t appOp,
1537 wp<AudioCommandThread> commandThread) :
1538 mHasOp(true), mAttributionSource(attributionSource), mAppOp(appOp),
1539 mCommandThread(commandThread)
1540{
1541}
1542
1543AudioPolicyService::OpRecordAudioMonitor::~OpRecordAudioMonitor()
1544{
1545 if (mOpCallback != 0) {
1546 mAppOpsManager.stopWatchingMode(mOpCallback);
1547 }
1548 mOpCallback.clear();
1549}
1550
1551void AudioPolicyService::OpRecordAudioMonitor::onFirstRef()
1552{
1553 checkOp();
1554 mOpCallback = new RecordAudioOpCallback(this);
1555 ALOGV("start watching op %d for %s", mAppOp, mAttributionSource.toString().c_str());
1556 // TODO: We need to always watch AppOpsManager::OP_RECORD_AUDIO too
1557 // since it controls the mic permission for legacy apps.
1558 mAppOpsManager.startWatchingMode(mAppOp, VALUE_OR_FATAL(aidl2legacy_string_view_String16(
1559 mAttributionSource.packageName.value_or(""))),
1560 mOpCallback);
1561}
1562
1563bool AudioPolicyService::OpRecordAudioMonitor::hasOp() const {
1564 return mHasOp.load();
1565}
1566
1567// Called by RecordAudioOpCallback when the app op corresponding to this OpRecordAudioMonitor
1568// is updated in AppOp callback and in onFirstRef()
1569// Note this method is never called (and never to be) for audio server / root track
1570// due to the UID in createIfNeeded(). As a result for those record track, it's:
1571// - not called from constructor,
1572// - not called from RecordAudioOpCallback because the callback is not installed in this case
1573void AudioPolicyService::OpRecordAudioMonitor::checkOp(bool updateUidStates)
1574{
1575 // TODO: We need to always check AppOpsManager::OP_RECORD_AUDIO too
1576 // since it controls the mic permission for legacy apps.
1577 const int32_t mode = mAppOpsManager.checkOp(mAppOp,
1578 mAttributionSource.uid, VALUE_OR_FATAL(aidl2legacy_string_view_String16(
1579 mAttributionSource.packageName.value_or(""))));
1580 const bool hasIt = (mode == AppOpsManager::MODE_ALLOWED);
1581 // verbose logging only log when appOp changed
1582 ALOGI_IF(hasIt != mHasOp.load(),
1583 "App op %d missing, %ssilencing record %s",
1584 mAppOp, hasIt ? "un" : "", mAttributionSource.toString().c_str());
1585 mHasOp.store(hasIt);
1586
1587 if (updateUidStates) {
1588 sp<AudioCommandThread> commandThread = mCommandThread.promote();
1589 if (commandThread != nullptr) {
1590 commandThread->updateUidStatesCommand();
1591 }
1592 }
1593}
1594
1595AudioPolicyService::OpRecordAudioMonitor::RecordAudioOpCallback::RecordAudioOpCallback(
1596 const wp<OpRecordAudioMonitor>& monitor) : mMonitor(monitor)
1597{ }
1598
1599void AudioPolicyService::OpRecordAudioMonitor::RecordAudioOpCallback::opChanged(int32_t op,
1600 const String16& packageName __unused) {
1601 sp<OpRecordAudioMonitor> monitor = mMonitor.promote();
1602 if (monitor != NULL) {
1603 if (op != monitor->getOp()) {
1604 return;
1605 }
1606 monitor->checkOp(true);
1607 }
1608}
1609
1610
Mathias Agopian65ab4712010-07-14 17:59:35 -07001611// ----------- AudioPolicyService::AudioCommandThread implementation ----------
1612
Eric Laurentbfb1b832013-01-07 09:53:42 -08001613AudioPolicyService::AudioCommandThread::AudioCommandThread(String8 name,
1614 const wp<AudioPolicyService>& service)
1615 : Thread(false), mName(name), mService(service)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001616{
Mathias Agopian65ab4712010-07-14 17:59:35 -07001617}
1618
1619
1620AudioPolicyService::AudioCommandThread::~AudioCommandThread()
1621{
Eric Laurentbfb1b832013-01-07 09:53:42 -08001622 if (!mAudioCommands.isEmpty()) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07001623 release_wake_lock(mName.string());
1624 }
1625 mAudioCommands.clear();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001626}
1627
1628void AudioPolicyService::AudioCommandThread::onFirstRef()
1629{
Eric Laurentbfb1b832013-01-07 09:53:42 -08001630 run(mName.string(), ANDROID_PRIORITY_AUDIO);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001631}
1632
1633bool AudioPolicyService::AudioCommandThread::threadLoop()
1634{
Eric Laurentd7eda8d2016-02-02 17:18:39 -08001635 nsecs_t waitTime = -1;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001636
1637 mLock.lock();
1638 while (!exitPending())
1639 {
Eric Laurent59a89232014-06-08 14:14:17 -07001640 sp<AudioPolicyService> svc;
1641 while (!mAudioCommands.isEmpty() && !exitPending()) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07001642 nsecs_t curTime = systemTime();
1643 // commands are sorted by increasing time stamp: execute them from index 0 and up
1644 if (mAudioCommands[0]->mTime <= curTime) {
Eric Laurent0ede8922014-05-09 18:04:42 -07001645 sp<AudioCommand> command = mAudioCommands[0];
Mathias Agopian65ab4712010-07-14 17:59:35 -07001646 mAudioCommands.removeAt(0);
Eric Laurent0ede8922014-05-09 18:04:42 -07001647 mLastCommand = command;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001648
1649 switch (command->mCommand) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07001650 case SET_VOLUME: {
Eric Laurent0ede8922014-05-09 18:04:42 -07001651 VolumeData *data = (VolumeData *)command->mParam.get();
Steve Block3856b092011-10-20 11:56:00 +01001652 ALOGV("AudioCommandThread() processing set volume stream %d, \
Eric Laurentde070132010-07-13 04:45:46 -07001653 volume %f, output %d", data->mStream, data->mVolume, data->mIO);
Andy Hungfe726a62018-09-27 15:17:25 -07001654 mLock.unlock();
Eric Laurentde070132010-07-13 04:45:46 -07001655 command->mStatus = AudioSystem::setStreamVolume(data->mStream,
1656 data->mVolume,
1657 data->mIO);
Andy Hungfe726a62018-09-27 15:17:25 -07001658 mLock.lock();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001659 }break;
1660 case SET_PARAMETERS: {
Eric Laurent0ede8922014-05-09 18:04:42 -07001661 ParametersData *data = (ParametersData *)command->mParam.get();
Glenn Kastene53b9ea2012-03-12 16:29:55 -07001662 ALOGV("AudioCommandThread() processing set parameters string %s, io %d",
1663 data->mKeyValuePairs.string(), data->mIO);
Andy Hungfe726a62018-09-27 15:17:25 -07001664 mLock.unlock();
Glenn Kastene53b9ea2012-03-12 16:29:55 -07001665 command->mStatus = AudioSystem::setParameters(data->mIO, data->mKeyValuePairs);
Andy Hungfe726a62018-09-27 15:17:25 -07001666 mLock.lock();
Glenn Kastene53b9ea2012-03-12 16:29:55 -07001667 }break;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001668 case SET_VOICE_VOLUME: {
Eric Laurent0ede8922014-05-09 18:04:42 -07001669 VoiceVolumeData *data = (VoiceVolumeData *)command->mParam.get();
Steve Block3856b092011-10-20 11:56:00 +01001670 ALOGV("AudioCommandThread() processing set voice volume volume %f",
Eric Laurentde070132010-07-13 04:45:46 -07001671 data->mVolume);
Andy Hungfe726a62018-09-27 15:17:25 -07001672 mLock.unlock();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001673 command->mStatus = AudioSystem::setVoiceVolume(data->mVolume);
Andy Hungfe726a62018-09-27 15:17:25 -07001674 mLock.lock();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001675 }break;
Eric Laurentbfb1b832013-01-07 09:53:42 -08001676 case STOP_OUTPUT: {
Eric Laurent0ede8922014-05-09 18:04:42 -07001677 StopOutputData *data = (StopOutputData *)command->mParam.get();
Eric Laurentd7fe0862018-07-14 16:48:01 -07001678 ALOGV("AudioCommandThread() processing stop output portId %d",
1679 data->mPortId);
Eric Laurent59a89232014-06-08 14:14:17 -07001680 svc = mService.promote();
Eric Laurentbfb1b832013-01-07 09:53:42 -08001681 if (svc == 0) {
1682 break;
1683 }
1684 mLock.unlock();
Eric Laurentd7fe0862018-07-14 16:48:01 -07001685 svc->doStopOutput(data->mPortId);
Eric Laurentbfb1b832013-01-07 09:53:42 -08001686 mLock.lock();
Eric Laurentbfb1b832013-01-07 09:53:42 -08001687 }break;
1688 case RELEASE_OUTPUT: {
Eric Laurent0ede8922014-05-09 18:04:42 -07001689 ReleaseOutputData *data = (ReleaseOutputData *)command->mParam.get();
Eric Laurentd7fe0862018-07-14 16:48:01 -07001690 ALOGV("AudioCommandThread() processing release output portId %d",
1691 data->mPortId);
Eric Laurent59a89232014-06-08 14:14:17 -07001692 svc = mService.promote();
Eric Laurentbfb1b832013-01-07 09:53:42 -08001693 if (svc == 0) {
1694 break;
1695 }
1696 mLock.unlock();
Eric Laurentd7fe0862018-07-14 16:48:01 -07001697 svc->doReleaseOutput(data->mPortId);
Eric Laurentbfb1b832013-01-07 09:53:42 -08001698 mLock.lock();
Eric Laurentbfb1b832013-01-07 09:53:42 -08001699 }break;
Eric Laurent951f4552014-05-20 10:48:17 -07001700 case CREATE_AUDIO_PATCH: {
1701 CreateAudioPatchData *data = (CreateAudioPatchData *)command->mParam.get();
1702 ALOGV("AudioCommandThread() processing create audio patch");
1703 sp<IAudioFlinger> af = AudioSystem::get_audio_flinger();
1704 if (af == 0) {
1705 command->mStatus = PERMISSION_DENIED;
1706 } else {
Andy Hungfe726a62018-09-27 15:17:25 -07001707 mLock.unlock();
Eric Laurent951f4552014-05-20 10:48:17 -07001708 command->mStatus = af->createAudioPatch(&data->mPatch, &data->mHandle);
Andy Hungfe726a62018-09-27 15:17:25 -07001709 mLock.lock();
Eric Laurent951f4552014-05-20 10:48:17 -07001710 }
1711 } break;
1712 case RELEASE_AUDIO_PATCH: {
1713 ReleaseAudioPatchData *data = (ReleaseAudioPatchData *)command->mParam.get();
1714 ALOGV("AudioCommandThread() processing release audio patch");
1715 sp<IAudioFlinger> af = AudioSystem::get_audio_flinger();
1716 if (af == 0) {
1717 command->mStatus = PERMISSION_DENIED;
1718 } else {
Andy Hungfe726a62018-09-27 15:17:25 -07001719 mLock.unlock();
Eric Laurent951f4552014-05-20 10:48:17 -07001720 command->mStatus = af->releaseAudioPatch(data->mHandle);
Andy Hungfe726a62018-09-27 15:17:25 -07001721 mLock.lock();
Eric Laurent951f4552014-05-20 10:48:17 -07001722 }
1723 } break;
Eric Laurentb52c1522014-05-20 11:27:36 -07001724 case UPDATE_AUDIOPORT_LIST: {
1725 ALOGV("AudioCommandThread() processing update audio port list");
Eric Laurent59a89232014-06-08 14:14:17 -07001726 svc = mService.promote();
Eric Laurentb52c1522014-05-20 11:27:36 -07001727 if (svc == 0) {
1728 break;
1729 }
1730 mLock.unlock();
1731 svc->doOnAudioPortListUpdate();
1732 mLock.lock();
1733 }break;
1734 case UPDATE_AUDIOPATCH_LIST: {
1735 ALOGV("AudioCommandThread() processing update audio patch list");
Eric Laurent59a89232014-06-08 14:14:17 -07001736 svc = mService.promote();
Eric Laurentb52c1522014-05-20 11:27:36 -07001737 if (svc == 0) {
1738 break;
1739 }
1740 mLock.unlock();
1741 svc->doOnAudioPatchListUpdate();
1742 mLock.lock();
1743 }break;
François Gaffiecfe17322018-11-07 13:41:29 +01001744 case CHANGED_AUDIOVOLUMEGROUP: {
1745 AudioVolumeGroupData *data =
1746 static_cast<AudioVolumeGroupData *>(command->mParam.get());
1747 ALOGV("AudioCommandThread() processing update audio volume group");
1748 svc = mService.promote();
1749 if (svc == 0) {
1750 break;
1751 }
1752 mLock.unlock();
1753 svc->doOnAudioVolumeGroupChanged(data->mGroup, data->mFlags);
1754 mLock.lock();
1755 }break;
Eric Laurente1715a42014-05-20 11:30:42 -07001756 case SET_AUDIOPORT_CONFIG: {
1757 SetAudioPortConfigData *data = (SetAudioPortConfigData *)command->mParam.get();
1758 ALOGV("AudioCommandThread() processing set port config");
1759 sp<IAudioFlinger> af = AudioSystem::get_audio_flinger();
1760 if (af == 0) {
1761 command->mStatus = PERMISSION_DENIED;
1762 } else {
Andy Hungfe726a62018-09-27 15:17:25 -07001763 mLock.unlock();
Eric Laurente1715a42014-05-20 11:30:42 -07001764 command->mStatus = af->setAudioPortConfig(&data->mConfig);
Andy Hungfe726a62018-09-27 15:17:25 -07001765 mLock.lock();
Eric Laurente1715a42014-05-20 11:30:42 -07001766 }
1767 } break;
Jean-Michel Trivide801052015-04-14 19:10:14 -07001768 case DYN_POLICY_MIX_STATE_UPDATE: {
1769 DynPolicyMixStateUpdateData *data =
1770 (DynPolicyMixStateUpdateData *)command->mParam.get();
Jean-Michel Trivide801052015-04-14 19:10:14 -07001771 ALOGV("AudioCommandThread() processing dyn policy mix state update %s %d",
1772 data->mRegId.string(), data->mState);
1773 svc = mService.promote();
1774 if (svc == 0) {
1775 break;
1776 }
1777 mLock.unlock();
1778 svc->doOnDynamicPolicyMixStateUpdate(data->mRegId, data->mState);
1779 mLock.lock();
1780 } break;
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -08001781 case RECORDING_CONFIGURATION_UPDATE: {
1782 RecordingConfigurationUpdateData *data =
1783 (RecordingConfigurationUpdateData *)command->mParam.get();
1784 ALOGV("AudioCommandThread() processing recording configuration update");
1785 svc = mService.promote();
1786 if (svc == 0) {
1787 break;
1788 }
1789 mLock.unlock();
Jean-Michel Triviac4e4292016-12-22 11:39:31 -08001790 svc->doOnRecordingConfigurationUpdate(data->mEvent, &data->mClientInfo,
Eric Laurenta9f86652018-11-28 17:23:11 -08001791 &data->mClientConfig, data->mClientEffects,
1792 &data->mDeviceConfig, data->mEffects,
1793 data->mPatchHandle, data->mSource);
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -08001794 mLock.lock();
1795 } break;
Eric Laurentb20cf7d2019-04-05 19:37:34 -07001796 case SET_EFFECT_SUSPENDED: {
1797 SetEffectSuspendedData *data = (SetEffectSuspendedData *)command->mParam.get();
1798 ALOGV("AudioCommandThread() processing set effect suspended");
1799 sp<IAudioFlinger> af = AudioSystem::get_audio_flinger();
1800 if (af != 0) {
1801 mLock.unlock();
1802 af->setEffectSuspended(data->mEffectId, data->mSessionId, data->mSuspended);
1803 mLock.lock();
1804 }
1805 } break;
Mikhail Naganov88b30d22020-03-09 19:43:13 +00001806 case AUDIO_MODULES_UPDATE: {
1807 ALOGV("AudioCommandThread() processing audio modules update");
1808 svc = mService.promote();
1809 if (svc == 0) {
1810 break;
1811 }
1812 mLock.unlock();
1813 svc->doOnNewAudioModulesAvailable();
1814 mLock.lock();
1815 } break;
Jean-Michel Trivi9a6b9ad2020-10-22 16:46:43 -07001816 case ROUTING_UPDATED: {
1817 ALOGV("AudioCommandThread() processing routing update");
1818 svc = mService.promote();
1819 if (svc == 0) {
1820 break;
1821 }
1822 mLock.unlock();
1823 svc->doOnRoutingUpdated();
1824 mLock.lock();
1825 } break;
Eric Laurentb20cf7d2019-04-05 19:37:34 -07001826
Eric Laurented726cc2021-07-01 14:26:41 +02001827 case UPDATE_UID_STATES: {
1828 ALOGV("AudioCommandThread() processing updateUID states");
1829 svc = mService.promote();
1830 if (svc == 0) {
1831 break;
1832 }
1833 mLock.unlock();
1834 svc->updateUidStates();
1835 mLock.lock();
1836 } break;
1837
Eric Laurent81dd0f52021-07-05 11:54:40 +02001838 case CHECK_SPATIALIZER: {
1839 ALOGV("AudioCommandThread() processing updateUID states");
1840 svc = mService.promote();
1841 if (svc == 0) {
1842 break;
1843 }
1844 mLock.unlock();
1845 svc->doOnCheckSpatializer();
1846 mLock.lock();
1847 } break;
1848
Mathias Agopian65ab4712010-07-14 17:59:35 -07001849 default:
Steve Block5ff1dd52012-01-05 23:22:43 +00001850 ALOGW("AudioCommandThread() unknown command %d", command->mCommand);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001851 }
Eric Laurent0ede8922014-05-09 18:04:42 -07001852 {
1853 Mutex::Autolock _l(command->mLock);
1854 if (command->mWaitStatus) {
1855 command->mWaitStatus = false;
1856 command->mCond.signal();
1857 }
1858 }
Eric Laurentd7eda8d2016-02-02 17:18:39 -08001859 waitTime = -1;
Zach Janga754b4f2015-10-27 01:29:34 +00001860 // release mLock before releasing strong reference on the service as
1861 // AudioPolicyService destructor calls AudioCommandThread::exit() which
1862 // acquires mLock.
1863 mLock.unlock();
1864 svc.clear();
1865 mLock.lock();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001866 } else {
1867 waitTime = mAudioCommands[0]->mTime - curTime;
1868 break;
1869 }
1870 }
Zach Janga754b4f2015-10-27 01:29:34 +00001871
1872 // release delayed commands wake lock if the queue is empty
1873 if (mAudioCommands.isEmpty()) {
Ricardo Garcia05f2fdc2014-07-24 15:48:24 -07001874 release_wake_lock(mName.string());
Zach Janga754b4f2015-10-27 01:29:34 +00001875 }
1876
1877 // At this stage we have either an empty command queue or the first command in the queue
1878 // has a finite delay. So unless we are exiting it is safe to wait.
1879 if (!exitPending()) {
Eric Laurent59a89232014-06-08 14:14:17 -07001880 ALOGV("AudioCommandThread() going to sleep");
Eric Laurentd7eda8d2016-02-02 17:18:39 -08001881 if (waitTime == -1) {
1882 mWaitWorkCV.wait(mLock);
1883 } else {
1884 mWaitWorkCV.waitRelative(mLock, waitTime);
1885 }
Eric Laurent59a89232014-06-08 14:14:17 -07001886 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001887 }
Ricardo Garcia05f2fdc2014-07-24 15:48:24 -07001888 // release delayed commands wake lock before quitting
1889 if (!mAudioCommands.isEmpty()) {
1890 release_wake_lock(mName.string());
1891 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001892 mLock.unlock();
1893 return false;
1894}
1895
1896status_t AudioPolicyService::AudioCommandThread::dump(int fd)
1897{
1898 const size_t SIZE = 256;
1899 char buffer[SIZE];
1900 String8 result;
1901
1902 snprintf(buffer, SIZE, "AudioCommandThread %p Dump\n", this);
1903 result.append(buffer);
1904 write(fd, result.string(), result.size());
1905
Mikhail Naganov12b716c2020-04-30 22:37:43 +00001906 const bool locked = dumpTryLock(mLock);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001907 if (!locked) {
1908 String8 result2(kCmdDeadlockedString);
1909 write(fd, result2.string(), result2.size());
1910 }
1911
1912 snprintf(buffer, SIZE, "- Commands:\n");
1913 result = String8(buffer);
1914 result.append(" Command Time Wait pParam\n");
Glenn Kasten8d6a2442012-02-08 14:04:28 -08001915 for (size_t i = 0; i < mAudioCommands.size(); i++) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07001916 mAudioCommands[i]->dump(buffer, SIZE);
1917 result.append(buffer);
1918 }
1919 result.append(" Last Command\n");
Eric Laurent0ede8922014-05-09 18:04:42 -07001920 if (mLastCommand != 0) {
1921 mLastCommand->dump(buffer, SIZE);
1922 result.append(buffer);
1923 } else {
1924 result.append(" none\n");
1925 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001926
1927 write(fd, result.string(), result.size());
1928
Mikhail Naganov12b716c2020-04-30 22:37:43 +00001929 dumpReleaseLock(mLock, locked);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001930
1931 return NO_ERROR;
1932}
1933
Glenn Kastenfff6d712012-01-12 16:38:12 -08001934status_t AudioPolicyService::AudioCommandThread::volumeCommand(audio_stream_type_t stream,
Eric Laurentde070132010-07-13 04:45:46 -07001935 float volume,
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001936 audio_io_handle_t output,
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_VOLUME;
Eric Laurent0ede8922014-05-09 18:04:42 -07001941 sp<VolumeData> data = new VolumeData();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001942 data->mStream = stream;
1943 data->mVolume = volume;
1944 data->mIO = output;
1945 command->mParam = data;
Eric Laurent0ede8922014-05-09 18:04:42 -07001946 command->mWaitStatus = true;
Steve Block3856b092011-10-20 11:56:00 +01001947 ALOGV("AudioCommandThread() adding set volume stream %d, volume %f, output %d",
Eric Laurentde070132010-07-13 04:45:46 -07001948 stream, volume, output);
Eric Laurent0ede8922014-05-09 18:04:42 -07001949 return sendCommand(command, delayMs);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001950}
1951
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001952status_t AudioPolicyService::AudioCommandThread::parametersCommand(audio_io_handle_t ioHandle,
Dima Zavinfce7a472011-04-19 22:30:36 -07001953 const char *keyValuePairs,
Eric Laurentde070132010-07-13 04:45:46 -07001954 int delayMs)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001955{
Eric Laurent0ede8922014-05-09 18:04:42 -07001956 sp<AudioCommand> command = new AudioCommand();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001957 command->mCommand = SET_PARAMETERS;
Eric Laurent0ede8922014-05-09 18:04:42 -07001958 sp<ParametersData> data = new ParametersData();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001959 data->mIO = ioHandle;
Dima Zavinfce7a472011-04-19 22:30:36 -07001960 data->mKeyValuePairs = String8(keyValuePairs);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001961 command->mParam = data;
Eric Laurent0ede8922014-05-09 18:04:42 -07001962 command->mWaitStatus = true;
Steve Block3856b092011-10-20 11:56:00 +01001963 ALOGV("AudioCommandThread() adding set parameter string %s, io %d ,delay %d",
Dima Zavinfce7a472011-04-19 22:30:36 -07001964 keyValuePairs, ioHandle, delayMs);
Eric Laurent0ede8922014-05-09 18:04:42 -07001965 return sendCommand(command, delayMs);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001966}
1967
1968status_t AudioPolicyService::AudioCommandThread::voiceVolumeCommand(float volume, int delayMs)
1969{
Eric Laurent0ede8922014-05-09 18:04:42 -07001970 sp<AudioCommand> command = new AudioCommand();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001971 command->mCommand = SET_VOICE_VOLUME;
Eric Laurent0ede8922014-05-09 18:04:42 -07001972 sp<VoiceVolumeData> data = new VoiceVolumeData();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001973 data->mVolume = volume;
1974 command->mParam = data;
Eric Laurent0ede8922014-05-09 18:04:42 -07001975 command->mWaitStatus = true;
Steve Block3856b092011-10-20 11:56:00 +01001976 ALOGV("AudioCommandThread() adding set voice volume volume %f", volume);
Eric Laurent0ede8922014-05-09 18:04:42 -07001977 return sendCommand(command, delayMs);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001978}
1979
Eric Laurentb20cf7d2019-04-05 19:37:34 -07001980void AudioPolicyService::AudioCommandThread::setEffectSuspendedCommand(int effectId,
1981 audio_session_t sessionId,
1982 bool suspended)
1983{
1984 sp<AudioCommand> command = new AudioCommand();
1985 command->mCommand = SET_EFFECT_SUSPENDED;
1986 sp<SetEffectSuspendedData> data = new SetEffectSuspendedData();
1987 data->mEffectId = effectId;
1988 data->mSessionId = sessionId;
1989 data->mSuspended = suspended;
1990 command->mParam = data;
1991 ALOGV("AudioCommandThread() adding set suspended effectId %d sessionId %d suspended %d",
1992 effectId, sessionId, suspended);
1993 sendCommand(command);
1994}
1995
1996
Eric Laurentd7fe0862018-07-14 16:48:01 -07001997void AudioPolicyService::AudioCommandThread::stopOutputCommand(audio_port_handle_t portId)
Eric Laurentbfb1b832013-01-07 09:53:42 -08001998{
Eric Laurent0ede8922014-05-09 18:04:42 -07001999 sp<AudioCommand> command = new AudioCommand();
Eric Laurentbfb1b832013-01-07 09:53:42 -08002000 command->mCommand = STOP_OUTPUT;
Eric Laurent0ede8922014-05-09 18:04:42 -07002001 sp<StopOutputData> data = new StopOutputData();
Eric Laurentd7fe0862018-07-14 16:48:01 -07002002 data->mPortId = portId;
Jesper Tragardh48412dc2014-03-24 14:12:43 +01002003 command->mParam = data;
Eric Laurentd7fe0862018-07-14 16:48:01 -07002004 ALOGV("AudioCommandThread() adding stop output portId %d", portId);
Eric Laurent0ede8922014-05-09 18:04:42 -07002005 sendCommand(command);
Eric Laurentbfb1b832013-01-07 09:53:42 -08002006}
2007
Eric Laurentd7fe0862018-07-14 16:48:01 -07002008void AudioPolicyService::AudioCommandThread::releaseOutputCommand(audio_port_handle_t portId)
Eric Laurentbfb1b832013-01-07 09:53:42 -08002009{
Eric Laurent0ede8922014-05-09 18:04:42 -07002010 sp<AudioCommand> command = new AudioCommand();
Eric Laurentbfb1b832013-01-07 09:53:42 -08002011 command->mCommand = RELEASE_OUTPUT;
Eric Laurent0ede8922014-05-09 18:04:42 -07002012 sp<ReleaseOutputData> data = new ReleaseOutputData();
Eric Laurentd7fe0862018-07-14 16:48:01 -07002013 data->mPortId = portId;
Jesper Tragardh48412dc2014-03-24 14:12:43 +01002014 command->mParam = data;
Eric Laurentd7fe0862018-07-14 16:48:01 -07002015 ALOGV("AudioCommandThread() adding release output portId %d", portId);
Eric Laurent0ede8922014-05-09 18:04:42 -07002016 sendCommand(command);
2017}
2018
Eric Laurent951f4552014-05-20 10:48:17 -07002019status_t AudioPolicyService::AudioCommandThread::createAudioPatchCommand(
2020 const struct audio_patch *patch,
2021 audio_patch_handle_t *handle,
2022 int delayMs)
2023{
2024 status_t status = NO_ERROR;
2025
2026 sp<AudioCommand> command = new AudioCommand();
2027 command->mCommand = CREATE_AUDIO_PATCH;
2028 CreateAudioPatchData *data = new CreateAudioPatchData();
2029 data->mPatch = *patch;
2030 data->mHandle = *handle;
2031 command->mParam = data;
2032 command->mWaitStatus = true;
2033 ALOGV("AudioCommandThread() adding create patch delay %d", delayMs);
2034 status = sendCommand(command, delayMs);
2035 if (status == NO_ERROR) {
2036 *handle = data->mHandle;
2037 }
2038 return status;
2039}
2040
2041status_t AudioPolicyService::AudioCommandThread::releaseAudioPatchCommand(audio_patch_handle_t handle,
2042 int delayMs)
2043{
2044 sp<AudioCommand> command = new AudioCommand();
2045 command->mCommand = RELEASE_AUDIO_PATCH;
2046 ReleaseAudioPatchData *data = new ReleaseAudioPatchData();
2047 data->mHandle = handle;
2048 command->mParam = data;
2049 command->mWaitStatus = true;
2050 ALOGV("AudioCommandThread() adding release patch delay %d", delayMs);
2051 return sendCommand(command, delayMs);
2052}
2053
Eric Laurentb52c1522014-05-20 11:27:36 -07002054void AudioPolicyService::AudioCommandThread::updateAudioPortListCommand()
2055{
2056 sp<AudioCommand> command = new AudioCommand();
2057 command->mCommand = UPDATE_AUDIOPORT_LIST;
2058 ALOGV("AudioCommandThread() adding update audio port list");
2059 sendCommand(command);
2060}
2061
Eric Laurented726cc2021-07-01 14:26:41 +02002062void AudioPolicyService::AudioCommandThread::updateUidStatesCommand()
2063{
2064 sp<AudioCommand> command = new AudioCommand();
2065 command->mCommand = UPDATE_UID_STATES;
2066 ALOGV("AudioCommandThread() adding update UID states");
2067 sendCommand(command);
2068}
2069
Eric Laurentb52c1522014-05-20 11:27:36 -07002070void AudioPolicyService::AudioCommandThread::updateAudioPatchListCommand()
2071{
2072 sp<AudioCommand>command = new AudioCommand();
2073 command->mCommand = UPDATE_AUDIOPATCH_LIST;
2074 ALOGV("AudioCommandThread() adding update audio patch list");
2075 sendCommand(command);
2076}
2077
François Gaffiecfe17322018-11-07 13:41:29 +01002078void AudioPolicyService::AudioCommandThread::changeAudioVolumeGroupCommand(volume_group_t group,
2079 int flags)
2080{
2081 sp<AudioCommand>command = new AudioCommand();
2082 command->mCommand = CHANGED_AUDIOVOLUMEGROUP;
2083 AudioVolumeGroupData *data= new AudioVolumeGroupData();
2084 data->mGroup = group;
2085 data->mFlags = flags;
2086 command->mParam = data;
2087 ALOGV("AudioCommandThread() adding audio volume group changed");
2088 sendCommand(command);
2089}
2090
Eric Laurente1715a42014-05-20 11:30:42 -07002091status_t AudioPolicyService::AudioCommandThread::setAudioPortConfigCommand(
2092 const struct audio_port_config *config, int delayMs)
2093{
2094 sp<AudioCommand> command = new AudioCommand();
2095 command->mCommand = SET_AUDIOPORT_CONFIG;
2096 SetAudioPortConfigData *data = new SetAudioPortConfigData();
2097 data->mConfig = *config;
2098 command->mParam = data;
2099 command->mWaitStatus = true;
2100 ALOGV("AudioCommandThread() adding set port config delay %d", delayMs);
2101 return sendCommand(command, delayMs);
2102}
2103
Jean-Michel Trivide801052015-04-14 19:10:14 -07002104void AudioPolicyService::AudioCommandThread::dynamicPolicyMixStateUpdateCommand(
Chih-Hung Hsiehe964d4e2016-08-09 14:31:32 -07002105 const String8& regId, int32_t state)
Jean-Michel Trivide801052015-04-14 19:10:14 -07002106{
2107 sp<AudioCommand> command = new AudioCommand();
2108 command->mCommand = DYN_POLICY_MIX_STATE_UPDATE;
2109 DynPolicyMixStateUpdateData *data = new DynPolicyMixStateUpdateData();
2110 data->mRegId = regId;
2111 data->mState = state;
2112 command->mParam = data;
2113 ALOGV("AudioCommandThread() sending dynamic policy mix (id=%s) state update to %d",
2114 regId.string(), state);
2115 sendCommand(command);
2116}
2117
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -08002118void AudioPolicyService::AudioCommandThread::recordingConfigurationUpdateCommand(
Eric Laurenta9f86652018-11-28 17:23:11 -08002119 int event,
2120 const record_client_info_t *clientInfo,
2121 const audio_config_base_t *clientConfig,
2122 std::vector<effect_descriptor_t> clientEffects,
2123 const audio_config_base_t *deviceConfig,
2124 std::vector<effect_descriptor_t> effects,
2125 audio_patch_handle_t patchHandle,
2126 audio_source_t source)
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -08002127{
2128 sp<AudioCommand>command = new AudioCommand();
2129 command->mCommand = RECORDING_CONFIGURATION_UPDATE;
2130 RecordingConfigurationUpdateData *data = new RecordingConfigurationUpdateData();
2131 data->mEvent = event;
Jean-Michel Triviac4e4292016-12-22 11:39:31 -08002132 data->mClientInfo = *clientInfo;
Jean-Michel Trivi7281aa92016-02-17 15:33:40 -08002133 data->mClientConfig = *clientConfig;
Eric Laurenta9f86652018-11-28 17:23:11 -08002134 data->mClientEffects = clientEffects;
Jean-Michel Trivi7281aa92016-02-17 15:33:40 -08002135 data->mDeviceConfig = *deviceConfig;
Eric Laurenta9f86652018-11-28 17:23:11 -08002136 data->mEffects = effects;
Jean-Michel Trivi8c7cf3b2016-02-25 17:08:24 -08002137 data->mPatchHandle = patchHandle;
Eric Laurenta9f86652018-11-28 17:23:11 -08002138 data->mSource = source;
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -08002139 command->mParam = data;
Jean-Michel Triviac4e4292016-12-22 11:39:31 -08002140 ALOGV("AudioCommandThread() adding recording configuration update event %d, source %d uid %u",
2141 event, clientInfo->source, clientInfo->uid);
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -08002142 sendCommand(command);
2143}
2144
Mikhail Naganov88b30d22020-03-09 19:43:13 +00002145void AudioPolicyService::AudioCommandThread::audioModulesUpdateCommand()
2146{
2147 sp<AudioCommand> command = new AudioCommand();
2148 command->mCommand = AUDIO_MODULES_UPDATE;
2149 sendCommand(command);
2150}
2151
Jean-Michel Trivi9a6b9ad2020-10-22 16:46:43 -07002152void AudioPolicyService::AudioCommandThread::routingChangedCommand()
2153{
2154 sp<AudioCommand>command = new AudioCommand();
2155 command->mCommand = ROUTING_UPDATED;
2156 ALOGV("AudioCommandThread() adding routing update");
2157 sendCommand(command);
2158}
2159
Eric Laurent81dd0f52021-07-05 11:54:40 +02002160void AudioPolicyService::AudioCommandThread::checkSpatializerCommand()
2161{
2162 sp<AudioCommand>command = new AudioCommand();
2163 command->mCommand = CHECK_SPATIALIZER;
2164 ALOGV("AudioCommandThread() adding check spatializer");
2165 sendCommand(command);
2166}
2167
Eric Laurent0ede8922014-05-09 18:04:42 -07002168status_t AudioPolicyService::AudioCommandThread::sendCommand(sp<AudioCommand>& command, int delayMs)
2169{
2170 {
2171 Mutex::Autolock _l(mLock);
2172 insertCommand_l(command, delayMs);
2173 mWaitWorkCV.signal();
2174 }
2175 Mutex::Autolock _l(command->mLock);
2176 while (command->mWaitStatus) {
2177 nsecs_t timeOutNs = kAudioCommandTimeoutNs + milliseconds(delayMs);
2178 if (command->mCond.waitRelative(command->mLock, timeOutNs) != NO_ERROR) {
2179 command->mStatus = TIMED_OUT;
2180 command->mWaitStatus = false;
2181 }
2182 }
2183 return command->mStatus;
Eric Laurentbfb1b832013-01-07 09:53:42 -08002184}
2185
Mathias Agopian65ab4712010-07-14 17:59:35 -07002186// insertCommand_l() must be called with mLock held
Eric Laurent0ede8922014-05-09 18:04:42 -07002187void AudioPolicyService::AudioCommandThread::insertCommand_l(sp<AudioCommand>& command, int delayMs)
Mathias Agopian65ab4712010-07-14 17:59:35 -07002188{
Glenn Kasten8d6a2442012-02-08 14:04:28 -08002189 ssize_t i; // not size_t because i will count down to -1
Eric Laurent0ede8922014-05-09 18:04:42 -07002190 Vector < sp<AudioCommand> > removedCommands;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002191 command->mTime = systemTime() + milliseconds(delayMs);
2192
2193 // acquire wake lock to make sure delayed commands are processed
Eric Laurentbfb1b832013-01-07 09:53:42 -08002194 if (mAudioCommands.isEmpty()) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07002195 acquire_wake_lock(PARTIAL_WAKE_LOCK, mName.string());
2196 }
2197
2198 // check same pending commands with later time stamps and eliminate them
Ivan Lozano5ff158f2017-10-30 09:06:24 -07002199 for (i = (ssize_t)mAudioCommands.size()-1; i >= 0; i--) {
Eric Laurent0ede8922014-05-09 18:04:42 -07002200 sp<AudioCommand> command2 = mAudioCommands[i];
Mathias Agopian65ab4712010-07-14 17:59:35 -07002201 // commands are sorted by increasing time stamp: no need to scan the rest of mAudioCommands
2202 if (command2->mTime <= command->mTime) break;
Eric Laurente45b48a2014-09-04 16:40:57 -07002203
2204 // create audio patch or release audio patch commands are equivalent
2205 // with regard to filtering
2206 if ((command->mCommand == CREATE_AUDIO_PATCH) ||
2207 (command->mCommand == RELEASE_AUDIO_PATCH)) {
2208 if ((command2->mCommand != CREATE_AUDIO_PATCH) &&
2209 (command2->mCommand != RELEASE_AUDIO_PATCH)) {
2210 continue;
2211 }
2212 } else if (command2->mCommand != command->mCommand) continue;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002213
2214 switch (command->mCommand) {
2215 case SET_PARAMETERS: {
Eric Laurent0ede8922014-05-09 18:04:42 -07002216 ParametersData *data = (ParametersData *)command->mParam.get();
2217 ParametersData *data2 = (ParametersData *)command2->mParam.get();
Mathias Agopian65ab4712010-07-14 17:59:35 -07002218 if (data->mIO != data2->mIO) break;
Steve Block3856b092011-10-20 11:56:00 +01002219 ALOGV("Comparing parameter command %s to new command %s",
Eric Laurentde070132010-07-13 04:45:46 -07002220 data2->mKeyValuePairs.string(), data->mKeyValuePairs.string());
Mathias Agopian65ab4712010-07-14 17:59:35 -07002221 AudioParameter param = AudioParameter(data->mKeyValuePairs);
2222 AudioParameter param2 = AudioParameter(data2->mKeyValuePairs);
2223 for (size_t j = 0; j < param.size(); j++) {
Glenn Kastene53b9ea2012-03-12 16:29:55 -07002224 String8 key;
2225 String8 value;
2226 param.getAt(j, key, value);
2227 for (size_t k = 0; k < param2.size(); k++) {
2228 String8 key2;
2229 String8 value2;
2230 param2.getAt(k, key2, value2);
2231 if (key2 == key) {
2232 param2.remove(key2);
2233 ALOGV("Filtering out parameter %s", key2.string());
2234 break;
2235 }
2236 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07002237 }
2238 // if all keys have been filtered out, remove the command.
2239 // otherwise, update the key value pairs
2240 if (param2.size() == 0) {
2241 removedCommands.add(command2);
2242 } else {
2243 data2->mKeyValuePairs = param2.toString();
2244 }
Eric Laurent21e54562013-09-23 12:08:05 -07002245 command->mTime = command2->mTime;
2246 // force delayMs to non 0 so that code below does not request to wait for
2247 // command status as the command is now delayed
2248 delayMs = 1;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002249 } break;
2250
2251 case SET_VOLUME: {
Eric Laurent0ede8922014-05-09 18:04:42 -07002252 VolumeData *data = (VolumeData *)command->mParam.get();
2253 VolumeData *data2 = (VolumeData *)command2->mParam.get();
Mathias Agopian65ab4712010-07-14 17:59:35 -07002254 if (data->mIO != data2->mIO) break;
2255 if (data->mStream != data2->mStream) break;
Steve Block3856b092011-10-20 11:56:00 +01002256 ALOGV("Filtering out volume command on output %d for stream %d",
Eric Laurentde070132010-07-13 04:45:46 -07002257 data->mIO, data->mStream);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002258 removedCommands.add(command2);
Eric Laurent21e54562013-09-23 12:08:05 -07002259 command->mTime = command2->mTime;
2260 // force delayMs to non 0 so that code below does not request to wait for
2261 // command status as the command is now delayed
2262 delayMs = 1;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002263 } break;
Eric Laurente45b48a2014-09-04 16:40:57 -07002264
Eric Laurentbaf35fe2016-07-27 15:36:53 -07002265 case SET_VOICE_VOLUME: {
2266 VoiceVolumeData *data = (VoiceVolumeData *)command->mParam.get();
2267 VoiceVolumeData *data2 = (VoiceVolumeData *)command2->mParam.get();
2268 ALOGV("Filtering out voice volume command value %f replaced by %f",
2269 data2->mVolume, data->mVolume);
2270 removedCommands.add(command2);
2271 command->mTime = command2->mTime;
2272 // force delayMs to non 0 so that code below does not request to wait for
2273 // command status as the command is now delayed
2274 delayMs = 1;
2275 } break;
2276
Eric Laurente45b48a2014-09-04 16:40:57 -07002277 case CREATE_AUDIO_PATCH:
2278 case RELEASE_AUDIO_PATCH: {
2279 audio_patch_handle_t handle;
Haynes Mathew Georgea2d4a6d2014-10-13 13:05:22 -07002280 struct audio_patch patch;
Eric Laurente45b48a2014-09-04 16:40:57 -07002281 if (command->mCommand == CREATE_AUDIO_PATCH) {
2282 handle = ((CreateAudioPatchData *)command->mParam.get())->mHandle;
Haynes Mathew Georgea2d4a6d2014-10-13 13:05:22 -07002283 patch = ((CreateAudioPatchData *)command->mParam.get())->mPatch;
Eric Laurente45b48a2014-09-04 16:40:57 -07002284 } else {
2285 handle = ((ReleaseAudioPatchData *)command->mParam.get())->mHandle;
Mikhail Naganov7be71d22018-05-23 16:51:46 -07002286 memset(&patch, 0, sizeof(patch));
Eric Laurente45b48a2014-09-04 16:40:57 -07002287 }
2288 audio_patch_handle_t handle2;
Haynes Mathew Georgea2d4a6d2014-10-13 13:05:22 -07002289 struct audio_patch patch2;
Eric Laurente45b48a2014-09-04 16:40:57 -07002290 if (command2->mCommand == CREATE_AUDIO_PATCH) {
2291 handle2 = ((CreateAudioPatchData *)command2->mParam.get())->mHandle;
Haynes Mathew Georgea2d4a6d2014-10-13 13:05:22 -07002292 patch2 = ((CreateAudioPatchData *)command2->mParam.get())->mPatch;
Eric Laurente45b48a2014-09-04 16:40:57 -07002293 } else {
2294 handle2 = ((ReleaseAudioPatchData *)command2->mParam.get())->mHandle;
Glenn Kastenf60b6b62015-07-06 10:53:26 -07002295 memset(&patch2, 0, sizeof(patch2));
Eric Laurente45b48a2014-09-04 16:40:57 -07002296 }
2297 if (handle != handle2) break;
Haynes Mathew Georgea2d4a6d2014-10-13 13:05:22 -07002298 /* Filter CREATE_AUDIO_PATCH commands only when they are issued for
2299 same output. */
2300 if( (command->mCommand == CREATE_AUDIO_PATCH) &&
2301 (command2->mCommand == CREATE_AUDIO_PATCH) ) {
2302 bool isOutputDiff = false;
2303 if (patch.num_sources == patch2.num_sources) {
2304 for (unsigned count = 0; count < patch.num_sources; count++) {
2305 if (patch.sources[count].id != patch2.sources[count].id) {
2306 isOutputDiff = true;
2307 break;
2308 }
2309 }
2310 if (isOutputDiff)
2311 break;
2312 }
2313 }
Eric Laurente45b48a2014-09-04 16:40:57 -07002314 ALOGV("Filtering out %s audio patch command for handle %d",
2315 (command->mCommand == CREATE_AUDIO_PATCH) ? "create" : "release", handle);
2316 removedCommands.add(command2);
2317 command->mTime = command2->mTime;
2318 // force delayMs to non 0 so that code below does not request to wait for
2319 // command status as the command is now delayed
2320 delayMs = 1;
2321 } break;
2322
Jean-Michel Trivide801052015-04-14 19:10:14 -07002323 case DYN_POLICY_MIX_STATE_UPDATE: {
2324
2325 } break;
2326
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -08002327 case RECORDING_CONFIGURATION_UPDATE: {
2328
2329 } break;
2330
Jean-Michel Trivi9a6b9ad2020-10-22 16:46:43 -07002331 case ROUTING_UPDATED: {
2332
2333 } break;
2334
Mathias Agopian65ab4712010-07-14 17:59:35 -07002335 default:
2336 break;
2337 }
2338 }
2339
2340 // remove filtered commands
2341 for (size_t j = 0; j < removedCommands.size(); j++) {
2342 // removed commands always have time stamps greater than current command
2343 for (size_t k = i + 1; k < mAudioCommands.size(); k++) {
Eric Laurent0ede8922014-05-09 18:04:42 -07002344 if (mAudioCommands[k].get() == removedCommands[j].get()) {
Steve Block3856b092011-10-20 11:56:00 +01002345 ALOGV("suppressing command: %d", mAudioCommands[k]->mCommand);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002346 mAudioCommands.removeAt(k);
2347 break;
2348 }
2349 }
2350 }
2351 removedCommands.clear();
2352
Eric Laurentaa79bef2015-01-15 14:33:51 -08002353 // Disable wait for status if delay is not 0.
2354 // Except for create audio patch command because the returned patch handle
2355 // is needed by audio policy manager
2356 if (delayMs != 0 && command->mCommand != CREATE_AUDIO_PATCH) {
Eric Laurentcec4abb2012-07-03 12:23:02 -07002357 command->mWaitStatus = false;
2358 }
Eric Laurentcec4abb2012-07-03 12:23:02 -07002359
Mathias Agopian65ab4712010-07-14 17:59:35 -07002360 // insert command at the right place according to its time stamp
Eric Laurent1e693b52014-07-09 15:03:28 -07002361 ALOGV("inserting command: %d at index %zd, num commands %zu",
2362 command->mCommand, i+1, mAudioCommands.size());
Mathias Agopian65ab4712010-07-14 17:59:35 -07002363 mAudioCommands.insertAt(command, i + 1);
2364}
2365
2366void AudioPolicyService::AudioCommandThread::exit()
2367{
Steve Block3856b092011-10-20 11:56:00 +01002368 ALOGV("AudioCommandThread::exit");
Mathias Agopian65ab4712010-07-14 17:59:35 -07002369 {
2370 AutoMutex _l(mLock);
2371 requestExit();
2372 mWaitWorkCV.signal();
2373 }
Zach Janga754b4f2015-10-27 01:29:34 +00002374 // Note that we can call it from the thread loop if all other references have been released
2375 // but it will safely return WOULD_BLOCK in this case
Mathias Agopian65ab4712010-07-14 17:59:35 -07002376 requestExitAndWait();
2377}
2378
2379void AudioPolicyService::AudioCommandThread::AudioCommand::dump(char* buffer, size_t size)
2380{
2381 snprintf(buffer, size, " %02d %06d.%03d %01u %p\n",
2382 mCommand,
2383 (int)ns2s(mTime),
2384 (int)ns2ms(mTime)%1000,
2385 mWaitStatus,
Eric Laurent0ede8922014-05-09 18:04:42 -07002386 mParam.get());
Mathias Agopian65ab4712010-07-14 17:59:35 -07002387}
2388
Dima Zavinfce7a472011-04-19 22:30:36 -07002389/******* helpers for the service_ops callbacks defined below *********/
2390void AudioPolicyService::setParameters(audio_io_handle_t ioHandle,
2391 const char *keyValuePairs,
2392 int delayMs)
2393{
Glenn Kasten72ef00d2012-01-17 11:09:42 -08002394 mAudioCommandThread->parametersCommand(ioHandle, keyValuePairs,
Dima Zavinfce7a472011-04-19 22:30:36 -07002395 delayMs);
2396}
2397
2398int AudioPolicyService::setStreamVolume(audio_stream_type_t stream,
2399 float volume,
2400 audio_io_handle_t output,
2401 int delayMs)
2402{
Glenn Kastenfff6d712012-01-12 16:38:12 -08002403 return (int)mAudioCommandThread->volumeCommand(stream, volume,
Glenn Kasten72ef00d2012-01-17 11:09:42 -08002404 output, delayMs);
Dima Zavinfce7a472011-04-19 22:30:36 -07002405}
2406
Dima Zavinfce7a472011-04-19 22:30:36 -07002407int AudioPolicyService::setVoiceVolume(float volume, int delayMs)
2408{
2409 return (int)mAudioCommandThread->voiceVolumeCommand(volume, delayMs);
2410}
2411
Eric Laurentb20cf7d2019-04-05 19:37:34 -07002412void AudioPolicyService::setEffectSuspended(int effectId,
2413 audio_session_t sessionId,
2414 bool suspended)
2415{
2416 mAudioCommandThread->setEffectSuspendedCommand(effectId, sessionId, suspended);
2417}
2418
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002419Status AudioPolicyService::onNewAudioModulesAvailable()
Mikhail Naganov88b30d22020-03-09 19:43:13 +00002420{
Mikhail Naganovd0e2c742020-03-25 15:59:59 -07002421 mOutputCommandThread->audioModulesUpdateCommand();
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002422 return Status::ok();
Mikhail Naganov88b30d22020-03-09 19:43:13 +00002423}
2424
Eric Laurentb20cf7d2019-04-05 19:37:34 -07002425
Dima Zavinfce7a472011-04-19 22:30:36 -07002426extern "C" {
Eric Laurent2d388ec2014-03-07 13:25:54 -08002427audio_module_handle_t aps_load_hw_module(void *service __unused,
2428 const char *name);
2429audio_io_handle_t aps_open_output(void *service __unused,
Eric Laurenta4c5a552012-03-29 10:12:40 -07002430 audio_devices_t *pDevices,
2431 uint32_t *pSamplingRate,
2432 audio_format_t *pFormat,
2433 audio_channel_mask_t *pChannelMask,
2434 uint32_t *pLatencyMs,
Eric Laurent2d388ec2014-03-07 13:25:54 -08002435 audio_output_flags_t flags);
Eric Laurenta4c5a552012-03-29 10:12:40 -07002436
Eric Laurent2d388ec2014-03-07 13:25:54 -08002437audio_io_handle_t aps_open_output_on_module(void *service __unused,
Eric Laurenta4c5a552012-03-29 10:12:40 -07002438 audio_module_handle_t module,
2439 audio_devices_t *pDevices,
2440 uint32_t *pSamplingRate,
2441 audio_format_t *pFormat,
2442 audio_channel_mask_t *pChannelMask,
2443 uint32_t *pLatencyMs,
Richard Fitzgeraldad3af332013-03-25 16:54:37 +00002444 audio_output_flags_t flags,
Eric Laurent2d388ec2014-03-07 13:25:54 -08002445 const audio_offload_info_t *offloadInfo);
2446audio_io_handle_t aps_open_dup_output(void *service __unused,
Dima Zavinfce7a472011-04-19 22:30:36 -07002447 audio_io_handle_t output1,
Eric Laurent2d388ec2014-03-07 13:25:54 -08002448 audio_io_handle_t output2);
2449int aps_close_output(void *service __unused, audio_io_handle_t output);
2450int aps_suspend_output(void *service __unused, audio_io_handle_t output);
2451int aps_restore_output(void *service __unused, audio_io_handle_t output);
2452audio_io_handle_t aps_open_input(void *service __unused,
Eric Laurenta4c5a552012-03-29 10:12:40 -07002453 audio_devices_t *pDevices,
2454 uint32_t *pSamplingRate,
2455 audio_format_t *pFormat,
2456 audio_channel_mask_t *pChannelMask,
Eric Laurent2d388ec2014-03-07 13:25:54 -08002457 audio_in_acoustics_t acoustics __unused);
2458audio_io_handle_t aps_open_input_on_module(void *service __unused,
Eric Laurenta4c5a552012-03-29 10:12:40 -07002459 audio_module_handle_t module,
2460 audio_devices_t *pDevices,
2461 uint32_t *pSamplingRate,
2462 audio_format_t *pFormat,
Eric Laurent2d388ec2014-03-07 13:25:54 -08002463 audio_channel_mask_t *pChannelMask);
2464int aps_close_input(void *service __unused, audio_io_handle_t input);
2465int aps_invalidate_stream(void *service __unused, audio_stream_type_t stream);
Glenn Kastend848eb42016-03-08 13:42:11 -08002466int aps_move_effects(void *service __unused, audio_session_t session,
Dima Zavinfce7a472011-04-19 22:30:36 -07002467 audio_io_handle_t src_output,
Eric Laurent2d388ec2014-03-07 13:25:54 -08002468 audio_io_handle_t dst_output);
2469char * aps_get_parameters(void *service __unused, audio_io_handle_t io_handle,
2470 const char *keys);
2471void aps_set_parameters(void *service, audio_io_handle_t io_handle,
2472 const char *kv_pairs, int delay_ms);
2473int aps_set_stream_volume(void *service, audio_stream_type_t stream,
Dima Zavinfce7a472011-04-19 22:30:36 -07002474 float volume, audio_io_handle_t output,
Eric Laurent2d388ec2014-03-07 13:25:54 -08002475 int delay_ms);
Eric Laurent2d388ec2014-03-07 13:25:54 -08002476int aps_set_voice_volume(void *service, float volume, int delay_ms);
2477};
Dima Zavinfce7a472011-04-19 22:30:36 -07002478
Mikhail Naganov1b2a7942017-12-08 10:18:09 -08002479} // namespace android