blob: 8add137075ee8dab08ea910fcdf8aa5320b1b3c4 [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);
Eric Laurentb4f42a92022-01-17 17:37:31 +0100395 ALOGV("%s currentOutput %d newOutput %d channel_mask %#x",
396 __func__, currentOutput, newOutput, config.channel_mask);
Eric Laurent39095982021-08-24 18:29:27 +0200397 if (status == NO_ERROR && currentOutput == newOutput) {
398 return;
399 }
400 mLock.unlock();
401 // It is OK to call detachOutput() is none is already attached.
402 mSpatializer->detachOutput();
403 if (status != NO_ERROR || newOutput == AUDIO_IO_HANDLE_NONE) {
Eric Laurent81dd0f52021-07-05 11:54:40 +0200404 mLock.lock();
Eric Laurent39095982021-08-24 18:29:27 +0200405 return;
406 }
407 status = mSpatializer->attachOutput(newOutput);
408 mLock.lock();
409 if (status != NO_ERROR) {
410 mAudioPolicyManager->releaseSpatializerOutput(newOutput);
411 }
412 } else if (mSpatializer->getLevel() == media::SpatializationLevel::NONE
413 && mSpatializer->getOutput() != AUDIO_IO_HANDLE_NONE) {
414 mLock.unlock();
415 audio_io_handle_t output = mSpatializer->detachOutput();
416 mLock.lock();
417 if (output != AUDIO_IO_HANDLE_NONE) {
418 mAudioPolicyManager->releaseSpatializerOutput(output);
Eric Laurent81dd0f52021-07-05 11:54:40 +0200419 }
420 }
421 }
422}
423
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -0800424status_t AudioPolicyService::clientCreateAudioPatch(const struct audio_patch *patch,
425 audio_patch_handle_t *handle,
426 int delayMs)
427{
428 return mAudioCommandThread->createAudioPatchCommand(patch, handle, delayMs);
429}
430
431status_t AudioPolicyService::clientReleaseAudioPatch(audio_patch_handle_t handle,
432 int delayMs)
433{
434 return mAudioCommandThread->releaseAudioPatchCommand(handle, delayMs);
435}
436
Eric Laurente1715a42014-05-20 11:30:42 -0700437status_t AudioPolicyService::clientSetAudioPortConfig(const struct audio_port_config *config,
438 int delayMs)
439{
440 return mAudioCommandThread->setAudioPortConfigCommand(config, delayMs);
441}
442
Ytai Ben-Tsvi7e7a79d2020-12-15 16:48:16 -0800443AudioPolicyService::NotificationClient::NotificationClient(
444 const sp<AudioPolicyService>& service,
445 const sp<media::IAudioPolicyServiceClient>& client,
446 uid_t uid,
447 pid_t pid)
luochaojiang908c7d72018-06-21 14:58:04 +0800448 : mService(service), mUid(uid), mPid(pid), mAudioPolicyServiceClient(client),
François Gaffiecfe17322018-11-07 13:41:29 +0100449 mAudioPortCallbacksEnabled(false), mAudioVolumeGroupCallbacksEnabled(false)
Eric Laurentb52c1522014-05-20 11:27:36 -0700450{
451}
452
453AudioPolicyService::NotificationClient::~NotificationClient()
454{
455}
456
457void AudioPolicyService::NotificationClient::binderDied(const wp<IBinder>& who __unused)
458{
459 sp<NotificationClient> keep(this);
460 sp<AudioPolicyService> service = mService.promote();
461 if (service != 0) {
luochaojiang908c7d72018-06-21 14:58:04 +0800462 service->removeNotificationClient(mUid, mPid);
Eric Laurentb52c1522014-05-20 11:27:36 -0700463 }
464}
465
466void AudioPolicyService::NotificationClient::onAudioPortListUpdate()
467{
Eric Laurente8726fe2015-06-26 09:39:24 -0700468 if (mAudioPolicyServiceClient != 0 && mAudioPortCallbacksEnabled) {
Eric Laurentb52c1522014-05-20 11:27:36 -0700469 mAudioPolicyServiceClient->onAudioPortListUpdate();
470 }
471}
472
473void AudioPolicyService::NotificationClient::onAudioPatchListUpdate()
474{
Eric Laurente8726fe2015-06-26 09:39:24 -0700475 if (mAudioPolicyServiceClient != 0 && mAudioPortCallbacksEnabled) {
Eric Laurentb52c1522014-05-20 11:27:36 -0700476 mAudioPolicyServiceClient->onAudioPatchListUpdate();
477 }
478}
Eric Laurent57dae992011-07-24 13:36:09 -0700479
Pattydd807582021-11-04 21:01:03 +0800480void AudioPolicyService::NotificationClient::onAudioVolumeGroupChanged(volume_group_t group,
François Gaffiecfe17322018-11-07 13:41:29 +0100481 int flags)
482{
483 if (mAudioPolicyServiceClient != 0 && mAudioVolumeGroupCallbacksEnabled) {
484 mAudioPolicyServiceClient->onAudioVolumeGroupChanged(group, flags);
485 }
486}
487
488
Jean-Michel Trivide801052015-04-14 19:10:14 -0700489void AudioPolicyService::NotificationClient::onDynamicPolicyMixStateUpdate(
Chih-Hung Hsiehe964d4e2016-08-09 14:31:32 -0700490 const String8& regId, int32_t state)
Jean-Michel Trivide801052015-04-14 19:10:14 -0700491{
Andy Hung4ef19fa2018-05-15 19:35:29 -0700492 if (mAudioPolicyServiceClient != 0 && isServiceUid(mUid)) {
Ytai Ben-Tsvi7e7a79d2020-12-15 16:48:16 -0800493 mAudioPolicyServiceClient->onDynamicPolicyMixStateUpdate(
494 legacy2aidl_String8_string(regId).value(), state);
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -0800495 }
496}
497
498void AudioPolicyService::NotificationClient::onRecordingConfigurationUpdate(
Eric Laurenta9f86652018-11-28 17:23:11 -0800499 int event,
500 const record_client_info_t *clientInfo,
501 const audio_config_base_t *clientConfig,
502 std::vector<effect_descriptor_t> clientEffects,
503 const audio_config_base_t *deviceConfig,
504 std::vector<effect_descriptor_t> effects,
505 audio_patch_handle_t patchHandle,
506 audio_source_t source)
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -0800507{
Andy Hung4ef19fa2018-05-15 19:35:29 -0700508 if (mAudioPolicyServiceClient != 0 && isServiceUid(mUid)) {
Ytai Ben-Tsvi7e7a79d2020-12-15 16:48:16 -0800509 status_t status = [&]() -> status_t {
510 int32_t eventAidl = VALUE_OR_RETURN_STATUS(convertIntegral<int32_t>(event));
511 media::RecordClientInfo clientInfoAidl = VALUE_OR_RETURN_STATUS(
512 legacy2aidl_record_client_info_t_RecordClientInfo(*clientInfo));
Mikhail Naganovdbf03642021-08-25 18:15:32 -0700513 AudioConfigBase clientConfigAidl = VALUE_OR_RETURN_STATUS(
Mikhail Naganovde3fa182021-07-30 15:06:42 -0700514 legacy2aidl_audio_config_base_t_AudioConfigBase(
515 *clientConfig, true /*isInput*/));
Ytai Ben-Tsvi7e7a79d2020-12-15 16:48:16 -0800516 std::vector<media::EffectDescriptor> clientEffectsAidl = VALUE_OR_RETURN_STATUS(
517 convertContainer<std::vector<media::EffectDescriptor>>(
518 clientEffects,
519 legacy2aidl_effect_descriptor_t_EffectDescriptor));
Mikhail Naganovdbf03642021-08-25 18:15:32 -0700520 AudioConfigBase deviceConfigAidl = VALUE_OR_RETURN_STATUS(
Mikhail Naganovde3fa182021-07-30 15:06:42 -0700521 legacy2aidl_audio_config_base_t_AudioConfigBase(
522 *deviceConfig, true /*isInput*/));
Ytai Ben-Tsvi7e7a79d2020-12-15 16:48:16 -0800523 std::vector<media::EffectDescriptor> effectsAidl = VALUE_OR_RETURN_STATUS(
524 convertContainer<std::vector<media::EffectDescriptor>>(
525 effects,
526 legacy2aidl_effect_descriptor_t_EffectDescriptor));
527 int32_t patchHandleAidl = VALUE_OR_RETURN_STATUS(
528 legacy2aidl_audio_patch_handle_t_int32_t(patchHandle));
Mikhail Naganovddceecc2021-09-03 13:58:56 -0700529 media::audio::common::AudioSource sourceAidl = VALUE_OR_RETURN_STATUS(
530 legacy2aidl_audio_source_t_AudioSource(source));
Ytai Ben-Tsvi7e7a79d2020-12-15 16:48:16 -0800531 return aidl_utils::statusTFromBinderStatus(
532 mAudioPolicyServiceClient->onRecordingConfigurationUpdate(eventAidl,
533 clientInfoAidl,
534 clientConfigAidl,
535 clientEffectsAidl,
536 deviceConfigAidl,
537 effectsAidl,
538 patchHandleAidl,
539 sourceAidl));
540 }();
541 ALOGW_IF(status != OK, "onRecordingConfigurationUpdate() failed: %d", status);
Jean-Michel Trivide801052015-04-14 19:10:14 -0700542 }
543}
544
Eric Laurente8726fe2015-06-26 09:39:24 -0700545void AudioPolicyService::NotificationClient::setAudioPortCallbacksEnabled(bool enabled)
546{
547 mAudioPortCallbacksEnabled = enabled;
548}
549
François Gaffiecfe17322018-11-07 13:41:29 +0100550void AudioPolicyService::NotificationClient::setAudioVolumeGroupCallbacksEnabled(bool enabled)
551{
552 mAudioVolumeGroupCallbacksEnabled = enabled;
553}
Eric Laurente8726fe2015-06-26 09:39:24 -0700554
Jean-Michel Trivi9a6b9ad2020-10-22 16:46:43 -0700555void AudioPolicyService::NotificationClient::onRoutingUpdated()
556{
557 if (mAudioPolicyServiceClient != 0 && isServiceUid(mUid)) {
558 mAudioPolicyServiceClient->onRoutingUpdated();
559 }
560}
561
Mathias Agopian65ab4712010-07-14 17:59:35 -0700562void AudioPolicyService::binderDied(const wp<IBinder>& who) {
Glenn Kasten411e4472012-11-02 10:00:06 -0700563 ALOGW("binderDied() %p, calling pid %d", who.unsafe_get(),
Eric Laurentde070132010-07-13 04:45:46 -0700564 IPCThreadState::self()->getCallingPid());
Mathias Agopian65ab4712010-07-14 17:59:35 -0700565}
566
Mikhail Naganov12b716c2020-04-30 22:37:43 +0000567static bool dumpTryLock(Mutex& mutex) ACQUIRE(mutex) NO_THREAD_SAFETY_ANALYSIS
Mathias Agopian65ab4712010-07-14 17:59:35 -0700568{
Mikhail Naganov12b716c2020-04-30 22:37:43 +0000569 return mutex.timedLock(kDumpLockTimeoutNs) == NO_ERROR;
570}
571
572static void dumpReleaseLock(Mutex& mutex, bool locked) RELEASE(mutex) NO_THREAD_SAFETY_ANALYSIS
573{
574 if (locked) mutex.unlock();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700575}
576
577status_t AudioPolicyService::dumpInternals(int fd)
578{
579 const size_t SIZE = 256;
580 char buffer[SIZE];
581 String8 result;
582
Eric Laurentdce54a12014-03-10 12:19:46 -0700583 snprintf(buffer, SIZE, "AudioPolicyManager: %p\n", mAudioPolicyManager);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700584 result.append(buffer);
585 snprintf(buffer, SIZE, "Command Thread: %p\n", mAudioCommandThread.get());
586 result.append(buffer);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700587
Hayden Gomes524159d2019-12-23 14:41:47 -0800588 snprintf(buffer, SIZE, "Supported System Usages:\n");
589 result.append(buffer);
590 for (std::vector<audio_usage_t>::iterator it = mSupportedSystemUsages.begin();
591 it != mSupportedSystemUsages.end(); ++it) {
592 snprintf(buffer, SIZE, "\t%d\n", *it);
593 result.append(buffer);
594 }
595
Mathias Agopian65ab4712010-07-14 17:59:35 -0700596 write(fd, result.string(), result.size());
597 return NO_ERROR;
598}
599
Eric Laurente8c8b432018-10-17 10:08:02 -0700600void AudioPolicyService::updateUidStates()
Svet Ganovf4ddfef2018-01-16 07:37:58 -0800601{
Eric Laurente8c8b432018-10-17 10:08:02 -0700602 Mutex::Autolock _l(mLock);
603 updateUidStates_l();
604}
605
606void AudioPolicyService::updateUidStates_l()
607{
Eric Laurent4eb58f12018-12-07 16:41:02 -0800608// Go over all active clients and allow capture (does not force silence) in the
609// following cases:
Evan Severson1f700cd2021-02-10 13:10:37 -0800610// The client is the assistant
611// AND an accessibility service is on TOP or a RTT call is active
Eric Laurent589171c2019-07-25 18:04:29 -0700612// AND the source is VOICE_RECOGNITION or HOTWORD
Evan Severson1f700cd2021-02-10 13:10:37 -0800613// OR uses VOICE_RECOGNITION AND is on TOP
614// OR uses HOTWORD
615// AND there is no active privacy sensitive capture or call
616// OR client has CAPTURE_AUDIO_OUTPUT privileged permission
617// OR The client is an accessibility service
618// AND Is on TOP
619// AND the source is VOICE_RECOGNITION or HOTWORD
620// OR The assistant is not on TOP
Eric Laurent589171c2019-07-25 18:04:29 -0700621// AND there is no active privacy sensitive capture or call
622// OR client has CAPTURE_AUDIO_OUTPUT privileged permission
Evan Severson1f700cd2021-02-10 13:10:37 -0800623// AND is on TOP
624// AND the source is VOICE_RECOGNITION or HOTWORD
625// OR the client source is virtual (remote submix, call audio TX or RX...)
626// OR the client source is HOTWORD
627// AND is on TOP
628// OR all active clients are using HOTWORD source
629// AND no call is active
630// OR client has CAPTURE_AUDIO_OUTPUT privileged permission
631// OR the client is the current InputMethodService
632// AND a RTT call is active AND the source is VOICE_RECOGNITION
633// OR Any client
634// AND The assistant is not on TOP
635// AND is on TOP or latest started
636// AND there is no active privacy sensitive capture or call
637// OR client has CAPTURE_AUDIO_OUTPUT privileged permission
Eric Laurent4eb58f12018-12-07 16:41:02 -0800638
Eric Laurent4e947da2019-10-17 15:24:06 -0700639
Eric Laurent4eb58f12018-12-07 16:41:02 -0800640 sp<AudioRecordClient> topActive;
641 sp<AudioRecordClient> latestActive;
Eric Laurentc21d5692020-02-25 10:24:36 -0800642 sp<AudioRecordClient> topSensitiveActive;
Eric Laurentb809a752020-06-29 09:53:13 -0700643 sp<AudioRecordClient> latestSensitiveActiveOrComm;
Eric Laurent1ff16a72019-03-14 18:35:04 -0700644
Eric Laurenta46bedb2018-12-07 18:01:26 -0800645 nsecs_t topStartNs = 0;
646 nsecs_t latestStartNs = 0;
Eric Laurentc21d5692020-02-25 10:24:36 -0800647 nsecs_t topSensitiveStartNs = 0;
Eric Laurent4eb58f12018-12-07 16:41:02 -0800648 nsecs_t latestSensitiveStartNs = 0;
649 bool isA11yOnTop = mUidPolicy->isA11yOnTop();
650 bool isAssistantOnTop = false;
651 bool isSensitiveActive = false;
Eric Laurent1ff16a72019-03-14 18:35:04 -0700652 bool isInCall = mPhoneState == AUDIO_MODE_IN_CALL;
Eric Laurentc21d5692020-02-25 10:24:36 -0800653 bool isInCommunication = mPhoneState == AUDIO_MODE_IN_COMMUNICATION;
654 bool rttCallActive = (isInCall || isInCommunication)
Eric Laurent6ede98f2019-06-11 14:50:30 -0700655 && mUidPolicy->isRttEnabled();
Eric Laurent4e947da2019-10-17 15:24:06 -0700656 bool onlyHotwordActive = true;
Eric Laurentb809a752020-06-29 09:53:13 -0700657 bool isPhoneStateOwnerActive = false;
Eric Laurent4eb58f12018-12-07 16:41:02 -0800658
Michael Groovercfd28302018-12-11 19:16:46 -0800659 // if Sensor Privacy is enabled then all recordings should be silenced.
660 if (mSensorPrivacyPolicy->isSensorPrivacyEnabled()) {
661 silenceAllRecordings_l();
662 return;
663 }
664
Eric Laurente8c8b432018-10-17 10:08:02 -0700665 for (size_t i =0; i < mAudioRecordClients.size(); i++) {
666 sp<AudioRecordClient> current = mAudioRecordClients[i];
Svet Ganov33761132021-05-13 22:51:08 +0000667 uid_t currentUid = VALUE_OR_FATAL(aidl2legacy_int32_t_uid_t(
668 current->attributionSource.uid));
Evan Severson1f700cd2021-02-10 13:10:37 -0800669 if (!current->active) {
Eric Laurent1ff16a72019-03-14 18:35:04 -0700670 continue;
Eric Laurent4eb58f12018-12-07 16:41:02 -0800671 }
Eric Laurent1ff16a72019-03-14 18:35:04 -0700672
Philip P. Moltmannbda45752020-07-17 16:41:18 -0700673 app_state_t appState = apmStatFromAmState(mUidPolicy->getUidState(currentUid));
Eric Laurent1ff16a72019-03-14 18:35:04 -0700674 // clients which app is in IDLE state are not eligible for top active or
675 // latest active
676 if (appState == APP_STATE_IDLE) {
677 continue;
678 }
679
Philip P. Moltmannbda45752020-07-17 16:41:18 -0700680 bool isAccessibility = mUidPolicy->isA11yUid(currentUid);
Eric Laurent14a88632020-07-16 12:28:30 -0700681 // Clients capturing for Accessibility services or virtual sources are not considered
Eric Laurentc21d5692020-02-25 10:24:36 -0800682 // for top or latest active to avoid masking regular clients started before
Eric Laurent14a88632020-07-16 12:28:30 -0700683 if (!isAccessibility && !isVirtualSource(current->attributes.source)) {
Philip P. Moltmannbda45752020-07-17 16:41:18 -0700684 bool isAssistant = mUidPolicy->isAssistantUid(currentUid);
Eric Laurentc21d5692020-02-25 10:24:36 -0800685 bool isPrivacySensitive =
686 (current->attributes.flags & AUDIO_FLAG_CAPTURE_PRIVATE) != 0;
Eric Laurentb809a752020-06-29 09:53:13 -0700687
Eric Laurentc21d5692020-02-25 10:24:36 -0800688 if (appState == APP_STATE_TOP) {
689 if (isPrivacySensitive) {
690 if (current->startTimeNs > topSensitiveStartNs) {
691 topSensitiveActive = current;
692 topSensitiveStartNs = current->startTimeNs;
693 }
694 } else {
695 if (current->startTimeNs > topStartNs) {
696 topActive = current;
697 topStartNs = current->startTimeNs;
698 }
699 }
700 if (isAssistant) {
701 isAssistantOnTop = true;
702 }
Eric Laurenta46bedb2018-12-07 18:01:26 -0800703 }
Eric Laurentc21d5692020-02-25 10:24:36 -0800704 // Clients capturing for HOTWORD are not considered
705 // for latest active to avoid masking regular clients started before
706 if (!(current->attributes.source == AUDIO_SOURCE_HOTWORD
707 || ((isA11yOnTop || rttCallActive) && isAssistant))) {
708 if (isPrivacySensitive) {
Eric Laurentb809a752020-06-29 09:53:13 -0700709 // if audio mode is IN_COMMUNICATION, make sure the audio mode owner
710 // is marked latest sensitive active even if another app qualifies.
711 if (current->startTimeNs > latestSensitiveStartNs
Philip P. Moltmannbda45752020-07-17 16:41:18 -0700712 || (isInCommunication && currentUid == mPhoneStateOwnerUid)) {
Eric Laurentb809a752020-06-29 09:53:13 -0700713 if (!isInCommunication || latestSensitiveActiveOrComm == nullptr
Philip P. Moltmannbda45752020-07-17 16:41:18 -0700714 || VALUE_OR_FATAL(aidl2legacy_int32_t_uid_t(
Svet Ganov33761132021-05-13 22:51:08 +0000715 latestSensitiveActiveOrComm->attributionSource.uid))
Philip P. Moltmannbda45752020-07-17 16:41:18 -0700716 != mPhoneStateOwnerUid) {
Eric Laurentb809a752020-06-29 09:53:13 -0700717 latestSensitiveActiveOrComm = current;
718 latestSensitiveStartNs = current->startTimeNs;
719 }
Eric Laurentc21d5692020-02-25 10:24:36 -0800720 }
721 isSensitiveActive = true;
722 } else {
723 if (current->startTimeNs > latestStartNs) {
724 latestActive = current;
725 latestStartNs = current->startTimeNs;
726 }
727 }
Eric Laurent4eb58f12018-12-07 16:41:02 -0800728 }
729 }
Eric Laurent4e947da2019-10-17 15:24:06 -0700730 if (current->attributes.source != AUDIO_SOURCE_HOTWORD) {
731 onlyHotwordActive = false;
732 }
Eric Laurentb0eff0f2021-11-09 16:05:49 +0100733 if (currentUid == mPhoneStateOwnerUid &&
734 !isVirtualSource(current->attributes.source)) {
Eric Laurentb809a752020-06-29 09:53:13 -0700735 isPhoneStateOwnerActive = true;
736 }
Eric Laurent4eb58f12018-12-07 16:41:02 -0800737 }
738
Eric Laurent1ff16a72019-03-14 18:35:04 -0700739 // if no active client with UI on Top, consider latest active as top
740 if (topActive == nullptr) {
741 topActive = latestActive;
Eric Laurentc21d5692020-02-25 10:24:36 -0800742 topStartNs = latestStartNs;
743 }
744 if (topSensitiveActive == nullptr) {
Eric Laurentb809a752020-06-29 09:53:13 -0700745 topSensitiveActive = latestSensitiveActiveOrComm;
Eric Laurentc21d5692020-02-25 10:24:36 -0800746 topSensitiveStartNs = latestSensitiveStartNs;
Eric Laurentb809a752020-06-29 09:53:13 -0700747 } else if (latestSensitiveActiveOrComm != nullptr) {
748 // if audio mode is IN_COMMUNICATION, favor audio mode owner over an app with
749 // foreground UI in case both are capturing with privacy sensitive flag.
Philip P. Moltmannbda45752020-07-17 16:41:18 -0700750 uid_t latestActiveUid = VALUE_OR_FATAL(
Svet Ganov33761132021-05-13 22:51:08 +0000751 aidl2legacy_int32_t_uid_t(latestSensitiveActiveOrComm->attributionSource.uid));
Philip P. Moltmannbda45752020-07-17 16:41:18 -0700752 if (isInCommunication && latestActiveUid == mPhoneStateOwnerUid) {
Eric Laurentb809a752020-06-29 09:53:13 -0700753 topSensitiveActive = latestSensitiveActiveOrComm;
754 topSensitiveStartNs = latestSensitiveStartNs;
755 }
Eric Laurentc21d5692020-02-25 10:24:36 -0800756 }
757
758 // If both privacy sensitive and regular capture are active:
759 // if the regular capture is privileged
760 // allow concurrency
761 // else
762 // favor the privacy sensitive case
763 if (topActive != nullptr && topSensitiveActive != nullptr
Ricardo Correa57a37692020-03-23 17:27:25 -0700764 && !topActive->canCaptureOutput) {
Eric Laurentc21d5692020-02-25 10:24:36 -0800765 topActive = nullptr;
Eric Laurent4eb58f12018-12-07 16:41:02 -0800766 }
767
768 for (size_t i =0; i < mAudioRecordClients.size(); i++) {
769 sp<AudioRecordClient> current = mAudioRecordClients[i];
Philip P. Moltmannbda45752020-07-17 16:41:18 -0700770 uid_t currentUid = VALUE_OR_FATAL(aidl2legacy_int32_t_uid_t(
Svet Ganov33761132021-05-13 22:51:08 +0000771 current->attributionSource.uid));
Eric Laurent1ff16a72019-03-14 18:35:04 -0700772 if (!current->active) {
773 continue;
774 }
775
Eric Laurent4eb58f12018-12-07 16:41:02 -0800776 audio_source_t source = current->attributes.source;
Philip P. Moltmannbda45752020-07-17 16:41:18 -0700777 bool isTopOrLatestActive = topActive == nullptr ? false :
Svet Ganov33761132021-05-13 22:51:08 +0000778 current->attributionSource.uid == topActive->attributionSource.uid;
Philip P. Moltmannbda45752020-07-17 16:41:18 -0700779 bool isTopOrLatestSensitive = topSensitiveActive == nullptr ? false :
Svet Ganov33761132021-05-13 22:51:08 +0000780 current->attributionSource.uid == topSensitiveActive->attributionSource.uid;
Eric Laurentc21d5692020-02-25 10:24:36 -0800781
Mikhail Naganov12b716c2020-04-30 22:37:43 +0000782 auto canCaptureIfInCallOrCommunication = [&](const auto &recordClient) REQUIRES(mLock) {
Philip P. Moltmannbda45752020-07-17 16:41:18 -0700783 uid_t recordUid = VALUE_OR_FATAL(aidl2legacy_int32_t_uid_t(
Svet Ganov33761132021-05-13 22:51:08 +0000784 recordClient->attributionSource.uid));
Ricardo Correa57a37692020-03-23 17:27:25 -0700785 bool canCaptureCall = recordClient->canCaptureOutput;
Eric Laurentb809a752020-06-29 09:53:13 -0700786 bool canCaptureCommunication = recordClient->canCaptureOutput
787 || !isPhoneStateOwnerActive
Philip P. Moltmannbda45752020-07-17 16:41:18 -0700788 || recordUid == mPhoneStateOwnerUid;
Eric Laurentb809a752020-06-29 09:53:13 -0700789 return !(isInCall && !canCaptureCall)
790 && !(isInCommunication && !canCaptureCommunication);
Eric Laurentc21d5692020-02-25 10:24:36 -0800791 };
Eric Laurent1ff16a72019-03-14 18:35:04 -0700792
793 // By default allow capture if:
794 // The assistant is not on TOP
Eric Laurenta171e352019-05-07 13:04:45 -0700795 // AND is on TOP or latest started
Eric Laurent1ff16a72019-03-14 18:35:04 -0700796 // AND there is no active privacy sensitive capture or call
797 // OR client has CAPTURE_AUDIO_OUTPUT privileged permission
798 bool allowCapture = !isAssistantOnTop
Eric Laurentc21d5692020-02-25 10:24:36 -0800799 && (isTopOrLatestActive || isTopOrLatestSensitive)
800 && !(isSensitiveActive
Ricardo Correa57a37692020-03-23 17:27:25 -0700801 && !(isTopOrLatestSensitive || current->canCaptureOutput))
Eric Laurentc21d5692020-02-25 10:24:36 -0800802 && canCaptureIfInCallOrCommunication(current);
Eric Laurent2dc962b2019-03-01 08:25:25 -0800803
Eric Laurented726cc2021-07-01 14:26:41 +0200804 if (!current->hasOp()) {
805 // Never allow capture if app op is denied
806 allowCapture = false;
807 } else if (isVirtualSource(source)) {
Eric Laurent1ff16a72019-03-14 18:35:04 -0700808 // Allow capture for virtual (remote submix, call audio TX or RX...) sources
809 allowCapture = true;
Philip P. Moltmannbda45752020-07-17 16:41:18 -0700810 } else if (mUidPolicy->isAssistantUid(currentUid)) {
Eric Laurent1ff16a72019-03-14 18:35:04 -0700811 // For assistant allow capture if:
Eric Laurent6ede98f2019-06-11 14:50:30 -0700812 // An accessibility service is on TOP or a RTT call is active
Eric Laurent1ff16a72019-03-14 18:35:04 -0700813 // AND the source is VOICE_RECOGNITION or HOTWORD
Eric Laurenta171e352019-05-07 13:04:45 -0700814 // OR is on TOP AND uses VOICE_RECOGNITION
Eric Laurent1ff16a72019-03-14 18:35:04 -0700815 // OR uses HOTWORD
816 // AND there is no active privacy sensitive capture or call
817 // OR client has CAPTURE_AUDIO_OUTPUT privileged permission
Eric Laurent6ede98f2019-06-11 14:50:30 -0700818 if (isA11yOnTop || rttCallActive) {
Eric Laurent4eb58f12018-12-07 16:41:02 -0800819 if (source == AUDIO_SOURCE_HOTWORD || source == AUDIO_SOURCE_VOICE_RECOGNITION) {
Eric Laurent1ff16a72019-03-14 18:35:04 -0700820 allowCapture = true;
Eric Laurent4eb58f12018-12-07 16:41:02 -0800821 }
822 } else {
Eric Laurenta171e352019-05-07 13:04:45 -0700823 if (((isAssistantOnTop && source == AUDIO_SOURCE_VOICE_RECOGNITION) ||
Eric Laurentc21d5692020-02-25 10:24:36 -0800824 source == AUDIO_SOURCE_HOTWORD)
Ricardo Correa57a37692020-03-23 17:27:25 -0700825 && !(isSensitiveActive && !current->canCaptureOutput)
Eric Laurentc21d5692020-02-25 10:24:36 -0800826 && canCaptureIfInCallOrCommunication(current)) {
Eric Laurent1ff16a72019-03-14 18:35:04 -0700827 allowCapture = true;
Eric Laurent4eb58f12018-12-07 16:41:02 -0800828 }
829 }
Philip P. Moltmannbda45752020-07-17 16:41:18 -0700830 } else if (mUidPolicy->isA11yUid(currentUid)) {
Eric Laurent1ff16a72019-03-14 18:35:04 -0700831 // For accessibility service allow capture if:
Eric Laurent47670c92019-08-28 16:59:05 -0700832 // The assistant is not on TOP
833 // AND there is no active privacy sensitive capture or call
Eric Laurent589171c2019-07-25 18:04:29 -0700834 // OR client has CAPTURE_AUDIO_OUTPUT privileged permission
Eric Laurent47670c92019-08-28 16:59:05 -0700835 // OR
836 // Is on TOP AND the source is VOICE_RECOGNITION or HOTWORD
837 if (!isAssistantOnTop
Ricardo Correa57a37692020-03-23 17:27:25 -0700838 && !(isSensitiveActive && !current->canCaptureOutput)
Eric Laurentc21d5692020-02-25 10:24:36 -0800839 && canCaptureIfInCallOrCommunication(current)) {
Eric Laurent47670c92019-08-28 16:59:05 -0700840 allowCapture = true;
841 }
Eric Laurent589171c2019-07-25 18:04:29 -0700842 if (isA11yOnTop) {
843 if (source == AUDIO_SOURCE_VOICE_RECOGNITION || source == AUDIO_SOURCE_HOTWORD) {
844 allowCapture = true;
845 }
Eric Laurent4eb58f12018-12-07 16:41:02 -0800846 }
Eric Laurent4e947da2019-10-17 15:24:06 -0700847 } else if (source == AUDIO_SOURCE_HOTWORD) {
848 // For HOTWORD source allow capture when not on TOP if:
849 // All active clients are using HOTWORD source
850 // AND no call is active
851 // OR client has CAPTURE_AUDIO_OUTPUT privileged permission
Eric Laurentc21d5692020-02-25 10:24:36 -0800852 if (onlyHotwordActive
853 && canCaptureIfInCallOrCommunication(current)) {
Eric Laurent4e947da2019-10-17 15:24:06 -0700854 allowCapture = true;
855 }
Philip P. Moltmannbda45752020-07-17 16:41:18 -0700856 } else if (mUidPolicy->isCurrentImeUid(currentUid)) {
Kohsuke Yatoha623a132020-03-24 20:10:26 -0700857 // For current InputMethodService allow capture if:
858 // A RTT call is active AND the source is VOICE_RECOGNITION
859 if (rttCallActive && source == AUDIO_SOURCE_VOICE_RECOGNITION) {
860 allowCapture = true;
861 }
Eric Laurent4eb58f12018-12-07 16:41:02 -0800862 }
Eric Laurent8c7ef892021-06-10 13:32:16 +0200863 setAppState_l(current,
Philip P. Moltmannbda45752020-07-17 16:41:18 -0700864 allowCapture ? apmStatFromAmState(mUidPolicy->getUidState(currentUid)) :
Eric Laurent1ff16a72019-03-14 18:35:04 -0700865 APP_STATE_IDLE);
Eric Laurente8c8b432018-10-17 10:08:02 -0700866 }
867}
868
Michael Groovercfd28302018-12-11 19:16:46 -0800869void AudioPolicyService::silenceAllRecordings_l() {
870 for (size_t i = 0; i < mAudioRecordClients.size(); i++) {
871 sp<AudioRecordClient> current = mAudioRecordClients[i];
Eric Laurent1ff16a72019-03-14 18:35:04 -0700872 if (!isVirtualSource(current->attributes.source)) {
Eric Laurent8c7ef892021-06-10 13:32:16 +0200873 setAppState_l(current, APP_STATE_IDLE);
Eric Laurent1ff16a72019-03-14 18:35:04 -0700874 }
Michael Groovercfd28302018-12-11 19:16:46 -0800875 }
876}
877
Eric Laurente8c8b432018-10-17 10:08:02 -0700878/* static */
879app_state_t AudioPolicyService::apmStatFromAmState(int amState) {
Eric Laurent1ff16a72019-03-14 18:35:04 -0700880
881 if (amState == ActivityManager::PROCESS_STATE_UNKNOWN) {
Eric Laurente8c8b432018-10-17 10:08:02 -0700882 return APP_STATE_IDLE;
Eric Laurent1ff16a72019-03-14 18:35:04 -0700883 } else if (amState <= ActivityManager::PROCESS_STATE_TOP) {
884 // include persistent services
885 return APP_STATE_TOP;
Eric Laurente8c8b432018-10-17 10:08:02 -0700886 }
887 return APP_STATE_FOREGROUND;
888}
889
Eric Laurent4eb58f12018-12-07 16:41:02 -0800890/* static */
Eric Laurent2dc962b2019-03-01 08:25:25 -0800891bool AudioPolicyService::isVirtualSource(audio_source_t source)
Eric Laurent4eb58f12018-12-07 16:41:02 -0800892{
893 switch (source) {
894 case AUDIO_SOURCE_VOICE_UPLINK:
895 case AUDIO_SOURCE_VOICE_DOWNLINK:
896 case AUDIO_SOURCE_VOICE_CALL:
Eric Laurent2dc962b2019-03-01 08:25:25 -0800897 case AUDIO_SOURCE_REMOTE_SUBMIX:
898 case AUDIO_SOURCE_FM_TUNER:
Eric Laurent68eb2122020-04-30 17:40:57 -0700899 case AUDIO_SOURCE_ECHO_REFERENCE:
Eric Laurent4eb58f12018-12-07 16:41:02 -0800900 return true;
901 default:
902 break;
903 }
904 return false;
905}
906
Eric Laurented726cc2021-07-01 14:26:41 +0200907/* static */
908bool AudioPolicyService::isAppOpSource(audio_source_t source)
909{
910 switch (source) {
911 case AUDIO_SOURCE_FM_TUNER:
912 case AUDIO_SOURCE_ECHO_REFERENCE:
Eric Laurent637bd202021-09-22 11:17:11 +0200913 case AUDIO_SOURCE_REMOTE_SUBMIX:
Eric Laurented726cc2021-07-01 14:26:41 +0200914 return false;
915 default:
916 break;
917 }
918 return true;
919}
920
Eric Laurent8c7ef892021-06-10 13:32:16 +0200921void AudioPolicyService::setAppState_l(sp<AudioRecordClient> client, app_state_t state)
Eric Laurente8c8b432018-10-17 10:08:02 -0700922{
923 AutoCallerClear acc;
924
925 if (mAudioPolicyManager) {
Eric Laurent8c7ef892021-06-10 13:32:16 +0200926 mAudioPolicyManager->setAppState(client->portId, state);
Mikhail Naganoveae73eb2018-04-03 16:57:36 -0700927 }
928 sp<IAudioFlinger> af = AudioSystem::get_audio_flinger();
929 if (af) {
Eric Laurentf32108e2018-10-04 17:22:04 -0700930 bool silenced = state == APP_STATE_IDLE;
Eric Laurent8c7ef892021-06-10 13:32:16 +0200931 if (client->silenced != silenced) {
932 if (client->active) {
933 if (silenced) {
934 finishRecording(client->attributionSource, client->attributes.source);
935 } else {
936 std::stringstream msg;
937 msg << "Audio recording un-silenced on session " << client->session;
938 if (!startRecording(client->attributionSource, String16(msg.str().c_str()),
939 client->attributes.source)) {
940 silenced = true;
941 }
942 }
943 }
944 af->setRecordSilenced(client->portId, silenced);
945 client->silenced = silenced;
946 }
Mikhail Naganoveae73eb2018-04-03 16:57:36 -0700947 }
Svet Ganovf4ddfef2018-01-16 07:37:58 -0800948}
949
Glenn Kasten0f11b512014-01-31 16:18:54 -0800950status_t AudioPolicyService::dump(int fd, const Vector<String16>& args __unused)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700951{
Glenn Kasten44deb052012-02-05 18:09:08 -0800952 if (!dumpAllowed()) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700953 dumpPermissionDenial(fd);
954 } else {
Mikhail Naganov12b716c2020-04-30 22:37:43 +0000955 const bool locked = dumpTryLock(mLock);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700956 if (!locked) {
957 String8 result(kDeadlockedString);
958 write(fd, result.string(), result.size());
959 }
960
961 dumpInternals(fd);
Glenn Kasten9d1f02d2012-02-08 17:47:58 -0800962 if (mAudioCommandThread != 0) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700963 mAudioCommandThread->dump(fd);
964 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700965
Eric Laurentdce54a12014-03-10 12:19:46 -0700966 if (mAudioPolicyManager) {
967 mAudioPolicyManager->dump(fd);
968 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700969
Kevin Rocard8be94972019-02-22 13:26:25 -0800970 mPackageManager.dump(fd);
971
Mikhail Naganov12b716c2020-04-30 22:37:43 +0000972 dumpReleaseLock(mLock, locked);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700973 }
974 return NO_ERROR;
975}
976
977status_t AudioPolicyService::dumpPermissionDenial(int fd)
978{
979 const size_t SIZE = 256;
980 char buffer[SIZE];
981 String8 result;
982 snprintf(buffer, SIZE, "Permission Denial: "
983 "can't dump AudioPolicyService from pid=%d, uid=%d\n",
984 IPCThreadState::self()->getCallingPid(),
985 IPCThreadState::self()->getCallingUid());
986 result.append(buffer);
987 write(fd, result.string(), result.size());
988 return NO_ERROR;
989}
990
991status_t AudioPolicyService::onTransact(
Svet Ganovf4ddfef2018-01-16 07:37:58 -0800992 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags) {
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800993 // make sure transactions reserved to AudioFlinger do not come from other processes
994 switch (code) {
995 case TRANSACTION_startOutput:
996 case TRANSACTION_stopOutput:
997 case TRANSACTION_releaseOutput:
998 case TRANSACTION_getInputForAttr:
999 case TRANSACTION_startInput:
1000 case TRANSACTION_stopInput:
1001 case TRANSACTION_releaseInput:
1002 case TRANSACTION_getOutputForEffect:
1003 case TRANSACTION_registerEffect:
1004 case TRANSACTION_unregisterEffect:
1005 case TRANSACTION_setEffectEnabled:
1006 case TRANSACTION_getStrategyForStream:
1007 case TRANSACTION_getOutputForAttr:
1008 case TRANSACTION_moveEffectsToIo:
1009 ALOGW("%s: transaction %d received from PID %d",
1010 __func__, code, IPCThreadState::self()->getCallingPid());
1011 return INVALID_OPERATION;
1012 default:
1013 break;
1014 }
1015
1016 // make sure the following transactions come from system components
1017 switch (code) {
1018 case TRANSACTION_setDeviceConnectionState:
1019 case TRANSACTION_handleDeviceConfigChange:
1020 case TRANSACTION_setPhoneState:
1021//FIXME: Allow setForceUse calls from system apps until a better use case routing API is available
1022// case TRANSACTION_setForceUse:
1023 case TRANSACTION_initStreamVolume:
1024 case TRANSACTION_setStreamVolumeIndex:
1025 case TRANSACTION_setVolumeIndexForAttributes:
1026 case TRANSACTION_getStreamVolumeIndex:
1027 case TRANSACTION_getVolumeIndexForAttributes:
1028 case TRANSACTION_getMinVolumeIndexForAttributes:
1029 case TRANSACTION_getMaxVolumeIndexForAttributes:
1030 case TRANSACTION_isStreamActive:
1031 case TRANSACTION_isStreamActiveRemotely:
1032 case TRANSACTION_isSourceActive:
1033 case TRANSACTION_getDevicesForStream:
1034 case TRANSACTION_registerPolicyMixes:
1035 case TRANSACTION_setMasterMono:
1036 case TRANSACTION_getSurroundFormats:
Kriti Dang6537def2021-03-02 13:46:59 +01001037 case TRANSACTION_getReportedSurroundFormats:
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001038 case TRANSACTION_setSurroundFormatEnabled:
1039 case TRANSACTION_setAssistantUid:
1040 case TRANSACTION_setA11yServicesUids:
1041 case TRANSACTION_setUidDeviceAffinities:
1042 case TRANSACTION_removeUidDeviceAffinities:
1043 case TRANSACTION_setUserIdDeviceAffinities:
1044 case TRANSACTION_removeUserIdDeviceAffinities:
Pattydd807582021-11-04 21:01:03 +08001045 case TRANSACTION_getHwOffloadFormatsSupportedForBluetoothMedia:
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001046 case TRANSACTION_listAudioVolumeGroups:
1047 case TRANSACTION_getVolumeGroupFromAudioAttributes:
1048 case TRANSACTION_acquireSoundTriggerSession:
1049 case TRANSACTION_releaseSoundTriggerSession:
1050 case TRANSACTION_setRttEnabled:
1051 case TRANSACTION_isCallScreenModeSupported:
1052 case TRANSACTION_setDevicesRoleForStrategy:
1053 case TRANSACTION_setSupportedSystemUsages:
1054 case TRANSACTION_removeDevicesRoleForStrategy:
1055 case TRANSACTION_getDevicesForRoleAndStrategy:
1056 case TRANSACTION_getDevicesForAttributes:
1057 case TRANSACTION_setAllowedCapturePolicy:
1058 case TRANSACTION_onNewAudioModulesAvailable:
1059 case TRANSACTION_setCurrentImeUid:
1060 case TRANSACTION_registerSoundTriggerCaptureStateListener:
1061 case TRANSACTION_setDevicesRoleForCapturePreset:
1062 case TRANSACTION_addDevicesRoleForCapturePreset:
1063 case TRANSACTION_removeDevicesRoleForCapturePreset:
1064 case TRANSACTION_clearDevicesRoleForCapturePreset:
Eric Laurent81dd0f52021-07-05 11:54:40 +02001065 case TRANSACTION_getDevicesForRoleAndCapturePreset:
1066 case TRANSACTION_getSpatializer: {
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001067 if (!isServiceUid(IPCThreadState::self()->getCallingUid())) {
1068 ALOGW("%s: transaction %d received from PID %d unauthorized UID %d",
1069 __func__, code, IPCThreadState::self()->getCallingPid(),
1070 IPCThreadState::self()->getCallingUid());
1071 return INVALID_OPERATION;
1072 }
1073 } break;
1074 default:
1075 break;
1076 }
1077
1078 std::string tag("IAudioPolicyService command " + std::to_string(code));
1079 TimeCheck check(tag.c_str());
1080
Svet Ganovf4ddfef2018-01-16 07:37:58 -08001081 switch (code) {
1082 case SHELL_COMMAND_TRANSACTION: {
1083 int in = data.readFileDescriptor();
1084 int out = data.readFileDescriptor();
1085 int err = data.readFileDescriptor();
1086 int argc = data.readInt32();
1087 Vector<String16> args;
1088 for (int i = 0; i < argc && data.dataAvail() > 0; i++) {
1089 args.add(data.readString16());
1090 }
1091 sp<IBinder> unusedCallback;
1092 sp<IResultReceiver> resultReceiver;
1093 status_t status;
1094 if ((status = data.readNullableStrongBinder(&unusedCallback)) != NO_ERROR) {
1095 return status;
1096 }
1097 if ((status = data.readNullableStrongBinder(&resultReceiver)) != NO_ERROR) {
1098 return status;
1099 }
1100 status = shellCommand(in, out, err, args);
1101 if (resultReceiver != nullptr) {
1102 resultReceiver->send(status);
1103 }
1104 return NO_ERROR;
1105 }
1106 }
1107
Mathias Agopian65ab4712010-07-14 17:59:35 -07001108 return BnAudioPolicyService::onTransact(code, data, reply, flags);
1109}
1110
Svet Ganovf4ddfef2018-01-16 07:37:58 -08001111// ------------------- Shell command implementation -------------------
1112
1113// NOTE: This is a remote API - make sure all args are validated
1114status_t AudioPolicyService::shellCommand(int in, int out, int err, Vector<String16>& args) {
1115 if (!checkCallingPermission(sManageAudioPolicyPermission, nullptr, nullptr)) {
1116 return PERMISSION_DENIED;
1117 }
1118 if (in == BAD_TYPE || out == BAD_TYPE || err == BAD_TYPE) {
1119 return BAD_VALUE;
1120 }
jovanakbe066e12019-09-02 11:54:39 -07001121 if (args.size() >= 3 && args[0] == String16("set-uid-state")) {
Svet Ganovf4ddfef2018-01-16 07:37:58 -08001122 return handleSetUidState(args, err);
jovanakbe066e12019-09-02 11:54:39 -07001123 } else if (args.size() >= 2 && args[0] == String16("reset-uid-state")) {
Svet Ganovf4ddfef2018-01-16 07:37:58 -08001124 return handleResetUidState(args, err);
jovanakbe066e12019-09-02 11:54:39 -07001125 } else if (args.size() >= 2 && args[0] == String16("get-uid-state")) {
Svet Ganovf4ddfef2018-01-16 07:37:58 -08001126 return handleGetUidState(args, out, err);
Eric Laurent269acb42021-04-23 16:53:22 +02001127 } else if (args.size() >= 1 && args[0] == String16("purge_permission-cache")) {
1128 purgePermissionCache();
1129 return NO_ERROR;
Svet Ganovf4ddfef2018-01-16 07:37:58 -08001130 } else if (args.size() == 1 && args[0] == String16("help")) {
1131 printHelp(out);
1132 return NO_ERROR;
1133 }
1134 printHelp(err);
1135 return BAD_VALUE;
1136}
1137
jovanakbe066e12019-09-02 11:54:39 -07001138static status_t getUidForPackage(String16 packageName, int userId, /*inout*/uid_t& uid, int err) {
1139 if (userId < 0) {
1140 ALOGE("Invalid user: %d", userId);
1141 dprintf(err, "Invalid user: %d\n", userId);
Svet Ganovf4ddfef2018-01-16 07:37:58 -08001142 return BAD_VALUE;
1143 }
jovanakbe066e12019-09-02 11:54:39 -07001144
1145 PermissionController pc;
1146 uid = pc.getPackageUid(packageName, 0);
1147 if (uid <= 0) {
1148 ALOGE("Unknown package: '%s'", String8(packageName).string());
1149 dprintf(err, "Unknown package: '%s'\n", String8(packageName).string());
1150 return BAD_VALUE;
1151 }
1152
1153 uid = multiuser_get_uid(userId, uid);
1154 return NO_ERROR;
1155}
1156
1157status_t AudioPolicyService::handleSetUidState(Vector<String16>& args, int err) {
1158 // Valid arg.size() is 3 or 5, args.size() is 5 with --user option.
1159 if (!(args.size() == 3 || args.size() == 5)) {
1160 printHelp(err);
1161 return BAD_VALUE;
1162 }
1163
Svet Ganovf4ddfef2018-01-16 07:37:58 -08001164 bool active = false;
1165 if (args[2] == String16("active")) {
1166 active = true;
1167 } else if ((args[2] != String16("idle"))) {
1168 ALOGE("Expected active or idle but got: '%s'", String8(args[2]).string());
1169 return BAD_VALUE;
1170 }
jovanakbe066e12019-09-02 11:54:39 -07001171
1172 int userId = 0;
1173 if (args.size() >= 5 && args[3] == String16("--user")) {
1174 userId = atoi(String8(args[4]));
1175 }
1176
1177 uid_t uid;
1178 if (getUidForPackage(args[1], userId, uid, err) == BAD_VALUE) {
1179 return BAD_VALUE;
1180 }
1181
Mikhail Naganov12b716c2020-04-30 22:37:43 +00001182 sp<UidPolicy> uidPolicy;
1183 {
1184 Mutex::Autolock _l(mLock);
1185 uidPolicy = mUidPolicy;
1186 }
1187 if (uidPolicy) {
1188 uidPolicy->addOverrideUid(uid, active);
1189 return NO_ERROR;
1190 }
1191 return NO_INIT;
Svet Ganovf4ddfef2018-01-16 07:37:58 -08001192}
1193
1194status_t AudioPolicyService::handleResetUidState(Vector<String16>& args, int err) {
jovanakbe066e12019-09-02 11:54:39 -07001195 // Valid arg.size() is 2 or 4, args.size() is 4 with --user option.
1196 if (!(args.size() == 2 || args.size() == 4)) {
1197 printHelp(err);
Svet Ganovf4ddfef2018-01-16 07:37:58 -08001198 return BAD_VALUE;
1199 }
jovanakbe066e12019-09-02 11:54:39 -07001200
1201 int userId = 0;
1202 if (args.size() >= 4 && args[2] == String16("--user")) {
1203 userId = atoi(String8(args[3]));
1204 }
1205
1206 uid_t uid;
1207 if (getUidForPackage(args[1], userId, uid, err) == BAD_VALUE) {
1208 return BAD_VALUE;
1209 }
1210
Mikhail Naganov12b716c2020-04-30 22:37:43 +00001211 sp<UidPolicy> uidPolicy;
1212 {
1213 Mutex::Autolock _l(mLock);
1214 uidPolicy = mUidPolicy;
1215 }
1216 if (uidPolicy) {
1217 uidPolicy->removeOverrideUid(uid);
1218 return NO_ERROR;
1219 }
1220 return NO_INIT;
Svet Ganovf4ddfef2018-01-16 07:37:58 -08001221}
1222
1223status_t AudioPolicyService::handleGetUidState(Vector<String16>& args, int out, int err) {
jovanakbe066e12019-09-02 11:54:39 -07001224 // Valid arg.size() is 2 or 4, args.size() is 4 with --user option.
1225 if (!(args.size() == 2 || args.size() == 4)) {
1226 printHelp(err);
Svet Ganovf4ddfef2018-01-16 07:37:58 -08001227 return BAD_VALUE;
1228 }
jovanakbe066e12019-09-02 11:54:39 -07001229
1230 int userId = 0;
1231 if (args.size() >= 4 && args[2] == String16("--user")) {
1232 userId = atoi(String8(args[3]));
1233 }
1234
1235 uid_t uid;
1236 if (getUidForPackage(args[1], userId, uid, err) == BAD_VALUE) {
1237 return BAD_VALUE;
1238 }
1239
Mikhail Naganov12b716c2020-04-30 22:37:43 +00001240 sp<UidPolicy> uidPolicy;
1241 {
1242 Mutex::Autolock _l(mLock);
1243 uidPolicy = mUidPolicy;
Svet Ganovf4ddfef2018-01-16 07:37:58 -08001244 }
Mikhail Naganov12b716c2020-04-30 22:37:43 +00001245 if (uidPolicy) {
1246 return dprintf(out, uidPolicy->isUidActive(uid) ? "active\n" : "idle\n");
1247 }
1248 return NO_INIT;
Svet Ganovf4ddfef2018-01-16 07:37:58 -08001249}
1250
1251status_t AudioPolicyService::printHelp(int out) {
1252 return dprintf(out, "Audio policy service commands:\n"
jovanakbe066e12019-09-02 11:54:39 -07001253 " get-uid-state <PACKAGE> [--user USER_ID] gets the uid state\n"
1254 " set-uid-state <PACKAGE> <active|idle> [--user USER_ID] overrides the uid state\n"
1255 " reset-uid-state <PACKAGE> [--user USER_ID] clears the uid state override\n"
Svet Ganovf4ddfef2018-01-16 07:37:58 -08001256 " help print this message\n");
1257}
1258
1259// ----------- AudioPolicyService::UidPolicy implementation ----------
1260
1261void AudioPolicyService::UidPolicy::registerSelf() {
Steven Moreland2f348142019-07-02 15:59:07 -07001262 status_t res = mAm.linkToDeath(this);
1263 mAm.registerUidObserver(this, ActivityManager::UID_OBSERVER_GONE
Svet Ganovf4ddfef2018-01-16 07:37:58 -08001264 | ActivityManager::UID_OBSERVER_IDLE
Eric Laurente8c8b432018-10-17 10:08:02 -07001265 | ActivityManager::UID_OBSERVER_ACTIVE
1266 | ActivityManager::UID_OBSERVER_PROCSTATE,
Svet Ganovf4ddfef2018-01-16 07:37:58 -08001267 ActivityManager::PROCESS_STATE_UNKNOWN,
1268 String16("audioserver"));
Mikhail Naganoveae73eb2018-04-03 16:57:36 -07001269 if (!res) {
1270 Mutex::Autolock _l(mLock);
1271 mObserverRegistered = true;
1272 } else {
1273 ALOGE("UidPolicy::registerSelf linkToDeath failed: %d", res);
Eric Laurent4eb58f12018-12-07 16:41:02 -08001274
Steven Moreland2f348142019-07-02 15:59:07 -07001275 mAm.unregisterUidObserver(this);
Mikhail Naganoveae73eb2018-04-03 16:57:36 -07001276 }
Svet Ganovf4ddfef2018-01-16 07:37:58 -08001277}
1278
1279void AudioPolicyService::UidPolicy::unregisterSelf() {
Steven Moreland2f348142019-07-02 15:59:07 -07001280 mAm.unlinkToDeath(this);
1281 mAm.unregisterUidObserver(this);
Mikhail Naganoveae73eb2018-04-03 16:57:36 -07001282 Mutex::Autolock _l(mLock);
1283 mObserverRegistered = false;
Svet Ganovf4ddfef2018-01-16 07:37:58 -08001284}
1285
Mikhail Naganoveae73eb2018-04-03 16:57:36 -07001286void AudioPolicyService::UidPolicy::binderDied(__unused const wp<IBinder> &who) {
1287 Mutex::Autolock _l(mLock);
1288 mCachedUids.clear();
1289 mObserverRegistered = false;
Svet Ganovf4ddfef2018-01-16 07:37:58 -08001290}
1291
Eric Laurente8c8b432018-10-17 10:08:02 -07001292void AudioPolicyService::UidPolicy::checkRegistered() {
Mikhail Naganoveae73eb2018-04-03 16:57:36 -07001293 bool needToReregister = false;
1294 {
1295 Mutex::Autolock _l(mLock);
1296 needToReregister = !mObserverRegistered;
Svet Ganovf4ddfef2018-01-16 07:37:58 -08001297 }
Mikhail Naganoveae73eb2018-04-03 16:57:36 -07001298 if (needToReregister) {
1299 // Looks like ActivityManager has died previously, attempt to re-register.
1300 registerSelf();
1301 }
Eric Laurente8c8b432018-10-17 10:08:02 -07001302}
1303
1304bool AudioPolicyService::UidPolicy::isUidActive(uid_t uid) {
1305 if (isServiceUid(uid)) return true;
1306 checkRegistered();
Mikhail Naganoveae73eb2018-04-03 16:57:36 -07001307 {
1308 Mutex::Autolock _l(mLock);
1309 auto overrideIter = mOverrideUids.find(uid);
1310 if (overrideIter != mOverrideUids.end()) {
Eric Laurente8c8b432018-10-17 10:08:02 -07001311 return overrideIter->second.first;
Mikhail Naganoveae73eb2018-04-03 16:57:36 -07001312 }
1313 // In an absense of the ActivityManager, assume everything to be active.
1314 if (!mObserverRegistered) return true;
1315 auto cacheIter = mCachedUids.find(uid);
Mikhail Naganoveba668a2018-04-05 08:13:15 -07001316 if (cacheIter != mCachedUids.end()) {
Eric Laurente8c8b432018-10-17 10:08:02 -07001317 return cacheIter->second.first;
Mikhail Naganoveae73eb2018-04-03 16:57:36 -07001318 }
1319 }
1320 ActivityManager am;
Hui Yu12c7ec72020-05-04 17:40:52 +00001321 bool active = am.isUidActive(uid, String16("audioserver"));
Mikhail Naganoveae73eb2018-04-03 16:57:36 -07001322 {
1323 Mutex::Autolock _l(mLock);
Eric Laurente8c8b432018-10-17 10:08:02 -07001324 mCachedUids.insert(std::pair<uid_t,
1325 std::pair<bool, int>>(uid, std::pair<bool, int>(active,
1326 ActivityManager::PROCESS_STATE_UNKNOWN)));
Mikhail Naganoveae73eb2018-04-03 16:57:36 -07001327 }
1328 return active;
Svet Ganovf4ddfef2018-01-16 07:37:58 -08001329}
1330
Eric Laurente8c8b432018-10-17 10:08:02 -07001331int AudioPolicyService::UidPolicy::getUidState(uid_t uid) {
1332 if (isServiceUid(uid)) {
1333 return ActivityManager::PROCESS_STATE_TOP;
1334 }
1335 checkRegistered();
1336 {
1337 Mutex::Autolock _l(mLock);
1338 auto overrideIter = mOverrideUids.find(uid);
1339 if (overrideIter != mOverrideUids.end()) {
1340 if (overrideIter->second.first) {
1341 if (overrideIter->second.second != ActivityManager::PROCESS_STATE_UNKNOWN) {
1342 return overrideIter->second.second;
1343 } else {
1344 auto cacheIter = mCachedUids.find(uid);
1345 if (cacheIter != mCachedUids.end()) {
1346 return cacheIter->second.second;
1347 }
1348 }
1349 }
1350 return ActivityManager::PROCESS_STATE_UNKNOWN;
1351 }
1352 // In an absense of the ActivityManager, assume everything to be active.
1353 if (!mObserverRegistered) {
1354 return ActivityManager::PROCESS_STATE_TOP;
1355 }
1356 auto cacheIter = mCachedUids.find(uid);
1357 if (cacheIter != mCachedUids.end()) {
1358 if (cacheIter->second.first) {
1359 return cacheIter->second.second;
1360 } else {
1361 return ActivityManager::PROCESS_STATE_UNKNOWN;
1362 }
1363 }
1364 }
1365 ActivityManager am;
Hui Yu12c7ec72020-05-04 17:40:52 +00001366 bool active = am.isUidActive(uid, String16("audioserver"));
Eric Laurente8c8b432018-10-17 10:08:02 -07001367 int state = ActivityManager::PROCESS_STATE_UNKNOWN;
1368 if (active) {
1369 state = am.getUidProcessState(uid, String16("audioserver"));
1370 }
1371 {
1372 Mutex::Autolock _l(mLock);
1373 mCachedUids.insert(std::pair<uid_t,
1374 std::pair<bool, int>>(uid, std::pair<bool, int>(active, state)));
1375 }
Eric Laurent4eb58f12018-12-07 16:41:02 -08001376
Eric Laurente8c8b432018-10-17 10:08:02 -07001377 return state;
1378}
1379
Mikhail Naganoveae73eb2018-04-03 16:57:36 -07001380void AudioPolicyService::UidPolicy::onUidActive(uid_t uid) {
Eric Laurente8c8b432018-10-17 10:08:02 -07001381 updateUid(&mCachedUids, uid, true, ActivityManager::PROCESS_STATE_UNKNOWN, true);
Mikhail Naganoveae73eb2018-04-03 16:57:36 -07001382}
1383
1384void AudioPolicyService::UidPolicy::onUidGone(uid_t uid, __unused bool disabled) {
Eric Laurente8c8b432018-10-17 10:08:02 -07001385 updateUid(&mCachedUids, uid, false, ActivityManager::PROCESS_STATE_UNKNOWN, false);
Mikhail Naganoveae73eb2018-04-03 16:57:36 -07001386}
1387
1388void AudioPolicyService::UidPolicy::onUidIdle(uid_t uid, __unused bool disabled) {
Eric Laurente8c8b432018-10-17 10:08:02 -07001389 updateUid(&mCachedUids, uid, false, ActivityManager::PROCESS_STATE_UNKNOWN, true);
Mikhail Naganoveae73eb2018-04-03 16:57:36 -07001390}
1391
Eric Laurente8c8b432018-10-17 10:08:02 -07001392void AudioPolicyService::UidPolicy::onUidStateChanged(uid_t uid,
1393 int32_t procState,
Hui Yu13ad0eb2019-09-09 10:27:07 -07001394 int64_t procStateSeq __unused,
1395 int32_t capability __unused) {
Eric Laurente8c8b432018-10-17 10:08:02 -07001396 if (procState != ActivityManager::PROCESS_STATE_UNKNOWN) {
1397 updateUid(&mCachedUids, uid, true, procState, true);
Mikhail Naganoveae73eb2018-04-03 16:57:36 -07001398 }
1399}
1400
1401void AudioPolicyService::UidPolicy::updateOverrideUid(uid_t uid, bool active, bool insert) {
Eric Laurente8c8b432018-10-17 10:08:02 -07001402 updateUid(&mOverrideUids, uid, active, ActivityManager::PROCESS_STATE_UNKNOWN, insert);
1403}
1404
1405void AudioPolicyService::UidPolicy::notifyService() {
1406 sp<AudioPolicyService> service = mService.promote();
1407 if (service != nullptr) {
1408 service->updateUidStates();
Mikhail Naganoveae73eb2018-04-03 16:57:36 -07001409 }
1410}
1411
Eric Laurente8c8b432018-10-17 10:08:02 -07001412void AudioPolicyService::UidPolicy::updateUid(std::unordered_map<uid_t,
1413 std::pair<bool, int>> *uids,
1414 uid_t uid,
1415 bool active,
1416 int state,
1417 bool insert) {
1418 if (isServiceUid(uid)) {
1419 return;
1420 }
1421 bool wasActive = isUidActive(uid);
1422 int previousState = getUidState(uid);
Mikhail Naganoveae73eb2018-04-03 16:57:36 -07001423 {
1424 Mutex::Autolock _l(mLock);
Eric Laurente8c8b432018-10-17 10:08:02 -07001425 updateUidLocked(uids, uid, active, state, insert);
Mikhail Naganoveae73eb2018-04-03 16:57:36 -07001426 }
Eric Laurente8c8b432018-10-17 10:08:02 -07001427 if (wasActive != isUidActive(uid) || state != previousState) {
1428 notifyService();
1429 }
Mikhail Naganoveae73eb2018-04-03 16:57:36 -07001430}
1431
Eric Laurente8c8b432018-10-17 10:08:02 -07001432void AudioPolicyService::UidPolicy::updateUidLocked(std::unordered_map<uid_t,
1433 std::pair<bool, int>> *uids,
1434 uid_t uid,
1435 bool active,
1436 int state,
1437 bool insert) {
Mikhail Naganoveae73eb2018-04-03 16:57:36 -07001438 auto it = uids->find(uid);
1439 if (it != uids->end()) {
Mikhail Naganoveae73eb2018-04-03 16:57:36 -07001440 if (insert) {
Eric Laurente8c8b432018-10-17 10:08:02 -07001441 if (state == ActivityManager::PROCESS_STATE_UNKNOWN) {
1442 it->second.first = active;
1443 }
1444 if (it->second.first) {
1445 it->second.second = state;
1446 } else {
1447 it->second.second = ActivityManager::PROCESS_STATE_UNKNOWN;
1448 }
Mikhail Naganoveae73eb2018-04-03 16:57:36 -07001449 } else {
1450 uids->erase(it);
1451 }
Eric Laurente8c8b432018-10-17 10:08:02 -07001452 } else if (insert && (state == ActivityManager::PROCESS_STATE_UNKNOWN)) {
1453 uids->insert(std::pair<uid_t, std::pair<bool, int>>(uid,
1454 std::pair<bool, int>(active, state)));
Mikhail Naganoveae73eb2018-04-03 16:57:36 -07001455 }
Svet Ganovf4ddfef2018-01-16 07:37:58 -08001456}
Mathias Agopian65ab4712010-07-14 17:59:35 -07001457
Eric Laurent4eb58f12018-12-07 16:41:02 -08001458bool AudioPolicyService::UidPolicy::isA11yOnTop() {
1459 for (const auto &uid : mCachedUids) {
Eric Laurent47670c92019-08-28 16:59:05 -07001460 if (!isA11yUid(uid.first)) {
Eric Laurent4eb58f12018-12-07 16:41:02 -08001461 continue;
1462 }
Amith Yamasanibcbb3002019-01-23 13:53:33 -08001463 if (uid.second.second >= ActivityManager::PROCESS_STATE_TOP
1464 && uid.second.second <= ActivityManager::PROCESS_STATE_BOUND_FOREGROUND_SERVICE) {
Eric Laurent4eb58f12018-12-07 16:41:02 -08001465 return true;
1466 }
1467 }
1468 return false;
1469}
1470
Eric Laurentb78763e2018-10-17 10:08:02 -07001471bool AudioPolicyService::UidPolicy::isA11yUid(uid_t uid)
1472{
1473 std::vector<uid_t>::iterator it = find(mA11yUids.begin(), mA11yUids.end(), uid);
1474 return it != mA11yUids.end();
1475}
1476
Michael Groovercfd28302018-12-11 19:16:46 -08001477// ----------- AudioPolicyService::SensorPrivacyService implementation ----------
1478void AudioPolicyService::SensorPrivacyPolicy::registerSelf() {
1479 SensorPrivacyManager spm;
1480 mSensorPrivacyEnabled = spm.isSensorPrivacyEnabled();
1481 spm.addSensorPrivacyListener(this);
1482}
1483
Evan Severson241d9592021-01-08 12:16:02 -08001484void AudioPolicyService::SensorPrivacyPolicy::registerSelfForMicrophoneOnly(int userId) {
1485 SensorPrivacyManager spm;
1486 mSensorPrivacyEnabled = spm.isIndividualSensorPrivacyEnabled(userId,
1487 SensorPrivacyManager::INDIVIDUAL_SENSOR_MICROPHONE);
1488 spm.addIndividualSensorPrivacyListener(userId,
1489 SensorPrivacyManager::INDIVIDUAL_SENSOR_MICROPHONE, this);
1490}
1491
Michael Groovercfd28302018-12-11 19:16:46 -08001492void AudioPolicyService::SensorPrivacyPolicy::unregisterSelf() {
1493 SensorPrivacyManager spm;
1494 spm.removeSensorPrivacyListener(this);
1495}
1496
1497bool AudioPolicyService::SensorPrivacyPolicy::isSensorPrivacyEnabled() {
1498 return mSensorPrivacyEnabled;
1499}
1500
1501binder::Status AudioPolicyService::SensorPrivacyPolicy::onSensorPrivacyChanged(bool enabled) {
1502 mSensorPrivacyEnabled = enabled;
1503 sp<AudioPolicyService> service = mService.promote();
1504 if (service != nullptr) {
1505 service->updateUidStates();
1506 }
1507 return binder::Status::ok();
1508}
1509
Eric Laurented726cc2021-07-01 14:26:41 +02001510// ----------- AudioPolicyService::OpRecordAudioMonitor implementation ----------
1511
1512// static
1513sp<AudioPolicyService::OpRecordAudioMonitor>
1514AudioPolicyService::OpRecordAudioMonitor::createIfNeeded(
1515 const AttributionSourceState& attributionSource, const audio_attributes_t& attr,
1516 wp<AudioCommandThread> commandThread)
1517{
Eric Laurent987ce102021-07-05 12:11:51 +02001518 if (isAudioServerOrRootUid(attributionSource.uid)) {
1519 ALOGV("not silencing record for audio or root source %s",
Eric Laurented726cc2021-07-01 14:26:41 +02001520 attributionSource.toString().c_str());
1521 return nullptr;
1522 }
1523
1524 if (!AudioPolicyService::isAppOpSource(attr.source)) {
1525 ALOGD("not monitoring app op for uid %d and source %d",
1526 attributionSource.uid, attr.source);
1527 return nullptr;
1528 }
1529
1530 if (!attributionSource.packageName.has_value()
1531 || attributionSource.packageName.value().size() == 0) {
1532 return nullptr;
1533 }
1534 return new OpRecordAudioMonitor(attributionSource, getOpForSource(attr.source), commandThread);
1535}
1536
1537AudioPolicyService::OpRecordAudioMonitor::OpRecordAudioMonitor(
1538 const AttributionSourceState& attributionSource, int32_t appOp,
1539 wp<AudioCommandThread> commandThread) :
1540 mHasOp(true), mAttributionSource(attributionSource), mAppOp(appOp),
1541 mCommandThread(commandThread)
1542{
1543}
1544
1545AudioPolicyService::OpRecordAudioMonitor::~OpRecordAudioMonitor()
1546{
1547 if (mOpCallback != 0) {
1548 mAppOpsManager.stopWatchingMode(mOpCallback);
1549 }
1550 mOpCallback.clear();
1551}
1552
1553void AudioPolicyService::OpRecordAudioMonitor::onFirstRef()
1554{
1555 checkOp();
1556 mOpCallback = new RecordAudioOpCallback(this);
1557 ALOGV("start watching op %d for %s", mAppOp, mAttributionSource.toString().c_str());
1558 // TODO: We need to always watch AppOpsManager::OP_RECORD_AUDIO too
1559 // since it controls the mic permission for legacy apps.
1560 mAppOpsManager.startWatchingMode(mAppOp, VALUE_OR_FATAL(aidl2legacy_string_view_String16(
1561 mAttributionSource.packageName.value_or(""))),
1562 mOpCallback);
1563}
1564
1565bool AudioPolicyService::OpRecordAudioMonitor::hasOp() const {
1566 return mHasOp.load();
1567}
1568
1569// Called by RecordAudioOpCallback when the app op corresponding to this OpRecordAudioMonitor
1570// is updated in AppOp callback and in onFirstRef()
1571// Note this method is never called (and never to be) for audio server / root track
1572// due to the UID in createIfNeeded(). As a result for those record track, it's:
1573// - not called from constructor,
1574// - not called from RecordAudioOpCallback because the callback is not installed in this case
1575void AudioPolicyService::OpRecordAudioMonitor::checkOp(bool updateUidStates)
1576{
1577 // TODO: We need to always check AppOpsManager::OP_RECORD_AUDIO too
1578 // since it controls the mic permission for legacy apps.
1579 const int32_t mode = mAppOpsManager.checkOp(mAppOp,
1580 mAttributionSource.uid, VALUE_OR_FATAL(aidl2legacy_string_view_String16(
1581 mAttributionSource.packageName.value_or(""))));
1582 const bool hasIt = (mode == AppOpsManager::MODE_ALLOWED);
1583 // verbose logging only log when appOp changed
1584 ALOGI_IF(hasIt != mHasOp.load(),
1585 "App op %d missing, %ssilencing record %s",
1586 mAppOp, hasIt ? "un" : "", mAttributionSource.toString().c_str());
1587 mHasOp.store(hasIt);
1588
1589 if (updateUidStates) {
1590 sp<AudioCommandThread> commandThread = mCommandThread.promote();
1591 if (commandThread != nullptr) {
1592 commandThread->updateUidStatesCommand();
1593 }
1594 }
1595}
1596
1597AudioPolicyService::OpRecordAudioMonitor::RecordAudioOpCallback::RecordAudioOpCallback(
1598 const wp<OpRecordAudioMonitor>& monitor) : mMonitor(monitor)
1599{ }
1600
1601void AudioPolicyService::OpRecordAudioMonitor::RecordAudioOpCallback::opChanged(int32_t op,
1602 const String16& packageName __unused) {
1603 sp<OpRecordAudioMonitor> monitor = mMonitor.promote();
1604 if (monitor != NULL) {
1605 if (op != monitor->getOp()) {
1606 return;
1607 }
1608 monitor->checkOp(true);
1609 }
1610}
1611
1612
Mathias Agopian65ab4712010-07-14 17:59:35 -07001613// ----------- AudioPolicyService::AudioCommandThread implementation ----------
1614
Eric Laurentbfb1b832013-01-07 09:53:42 -08001615AudioPolicyService::AudioCommandThread::AudioCommandThread(String8 name,
1616 const wp<AudioPolicyService>& service)
1617 : Thread(false), mName(name), mService(service)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001618{
Mathias Agopian65ab4712010-07-14 17:59:35 -07001619}
1620
1621
1622AudioPolicyService::AudioCommandThread::~AudioCommandThread()
1623{
Eric Laurentbfb1b832013-01-07 09:53:42 -08001624 if (!mAudioCommands.isEmpty()) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07001625 release_wake_lock(mName.string());
1626 }
1627 mAudioCommands.clear();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001628}
1629
1630void AudioPolicyService::AudioCommandThread::onFirstRef()
1631{
Eric Laurentbfb1b832013-01-07 09:53:42 -08001632 run(mName.string(), ANDROID_PRIORITY_AUDIO);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001633}
1634
1635bool AudioPolicyService::AudioCommandThread::threadLoop()
1636{
Eric Laurentd7eda8d2016-02-02 17:18:39 -08001637 nsecs_t waitTime = -1;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001638
1639 mLock.lock();
1640 while (!exitPending())
1641 {
Eric Laurent59a89232014-06-08 14:14:17 -07001642 sp<AudioPolicyService> svc;
1643 while (!mAudioCommands.isEmpty() && !exitPending()) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07001644 nsecs_t curTime = systemTime();
1645 // commands are sorted by increasing time stamp: execute them from index 0 and up
1646 if (mAudioCommands[0]->mTime <= curTime) {
Eric Laurent0ede8922014-05-09 18:04:42 -07001647 sp<AudioCommand> command = mAudioCommands[0];
Mathias Agopian65ab4712010-07-14 17:59:35 -07001648 mAudioCommands.removeAt(0);
Eric Laurent0ede8922014-05-09 18:04:42 -07001649 mLastCommand = command;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001650
1651 switch (command->mCommand) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07001652 case SET_VOLUME: {
Eric Laurent0ede8922014-05-09 18:04:42 -07001653 VolumeData *data = (VolumeData *)command->mParam.get();
Steve Block3856b092011-10-20 11:56:00 +01001654 ALOGV("AudioCommandThread() processing set volume stream %d, \
Eric Laurentde070132010-07-13 04:45:46 -07001655 volume %f, output %d", data->mStream, data->mVolume, data->mIO);
Andy Hungfe726a62018-09-27 15:17:25 -07001656 mLock.unlock();
Eric Laurentde070132010-07-13 04:45:46 -07001657 command->mStatus = AudioSystem::setStreamVolume(data->mStream,
1658 data->mVolume,
1659 data->mIO);
Andy Hungfe726a62018-09-27 15:17:25 -07001660 mLock.lock();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001661 }break;
1662 case SET_PARAMETERS: {
Eric Laurent0ede8922014-05-09 18:04:42 -07001663 ParametersData *data = (ParametersData *)command->mParam.get();
Glenn Kastene53b9ea2012-03-12 16:29:55 -07001664 ALOGV("AudioCommandThread() processing set parameters string %s, io %d",
1665 data->mKeyValuePairs.string(), data->mIO);
Andy Hungfe726a62018-09-27 15:17:25 -07001666 mLock.unlock();
Glenn Kastene53b9ea2012-03-12 16:29:55 -07001667 command->mStatus = AudioSystem::setParameters(data->mIO, data->mKeyValuePairs);
Andy Hungfe726a62018-09-27 15:17:25 -07001668 mLock.lock();
Glenn Kastene53b9ea2012-03-12 16:29:55 -07001669 }break;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001670 case SET_VOICE_VOLUME: {
Eric Laurent0ede8922014-05-09 18:04:42 -07001671 VoiceVolumeData *data = (VoiceVolumeData *)command->mParam.get();
Steve Block3856b092011-10-20 11:56:00 +01001672 ALOGV("AudioCommandThread() processing set voice volume volume %f",
Eric Laurentde070132010-07-13 04:45:46 -07001673 data->mVolume);
Andy Hungfe726a62018-09-27 15:17:25 -07001674 mLock.unlock();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001675 command->mStatus = AudioSystem::setVoiceVolume(data->mVolume);
Andy Hungfe726a62018-09-27 15:17:25 -07001676 mLock.lock();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001677 }break;
Eric Laurentbfb1b832013-01-07 09:53:42 -08001678 case STOP_OUTPUT: {
Eric Laurent0ede8922014-05-09 18:04:42 -07001679 StopOutputData *data = (StopOutputData *)command->mParam.get();
Eric Laurentd7fe0862018-07-14 16:48:01 -07001680 ALOGV("AudioCommandThread() processing stop output portId %d",
1681 data->mPortId);
Eric Laurent59a89232014-06-08 14:14:17 -07001682 svc = mService.promote();
Eric Laurentbfb1b832013-01-07 09:53:42 -08001683 if (svc == 0) {
1684 break;
1685 }
1686 mLock.unlock();
Eric Laurentd7fe0862018-07-14 16:48:01 -07001687 svc->doStopOutput(data->mPortId);
Eric Laurentbfb1b832013-01-07 09:53:42 -08001688 mLock.lock();
Eric Laurentbfb1b832013-01-07 09:53:42 -08001689 }break;
1690 case RELEASE_OUTPUT: {
Eric Laurent0ede8922014-05-09 18:04:42 -07001691 ReleaseOutputData *data = (ReleaseOutputData *)command->mParam.get();
Eric Laurentd7fe0862018-07-14 16:48:01 -07001692 ALOGV("AudioCommandThread() processing release output portId %d",
1693 data->mPortId);
Eric Laurent59a89232014-06-08 14:14:17 -07001694 svc = mService.promote();
Eric Laurentbfb1b832013-01-07 09:53:42 -08001695 if (svc == 0) {
1696 break;
1697 }
1698 mLock.unlock();
Eric Laurentd7fe0862018-07-14 16:48:01 -07001699 svc->doReleaseOutput(data->mPortId);
Eric Laurentbfb1b832013-01-07 09:53:42 -08001700 mLock.lock();
Eric Laurentbfb1b832013-01-07 09:53:42 -08001701 }break;
Eric Laurent951f4552014-05-20 10:48:17 -07001702 case CREATE_AUDIO_PATCH: {
1703 CreateAudioPatchData *data = (CreateAudioPatchData *)command->mParam.get();
1704 ALOGV("AudioCommandThread() processing create audio patch");
1705 sp<IAudioFlinger> af = AudioSystem::get_audio_flinger();
1706 if (af == 0) {
1707 command->mStatus = PERMISSION_DENIED;
1708 } else {
Andy Hungfe726a62018-09-27 15:17:25 -07001709 mLock.unlock();
Eric Laurent951f4552014-05-20 10:48:17 -07001710 command->mStatus = af->createAudioPatch(&data->mPatch, &data->mHandle);
Andy Hungfe726a62018-09-27 15:17:25 -07001711 mLock.lock();
Eric Laurent951f4552014-05-20 10:48:17 -07001712 }
1713 } break;
1714 case RELEASE_AUDIO_PATCH: {
1715 ReleaseAudioPatchData *data = (ReleaseAudioPatchData *)command->mParam.get();
1716 ALOGV("AudioCommandThread() processing release audio patch");
1717 sp<IAudioFlinger> af = AudioSystem::get_audio_flinger();
1718 if (af == 0) {
1719 command->mStatus = PERMISSION_DENIED;
1720 } else {
Andy Hungfe726a62018-09-27 15:17:25 -07001721 mLock.unlock();
Eric Laurent951f4552014-05-20 10:48:17 -07001722 command->mStatus = af->releaseAudioPatch(data->mHandle);
Andy Hungfe726a62018-09-27 15:17:25 -07001723 mLock.lock();
Eric Laurent951f4552014-05-20 10:48:17 -07001724 }
1725 } break;
Eric Laurentb52c1522014-05-20 11:27:36 -07001726 case UPDATE_AUDIOPORT_LIST: {
1727 ALOGV("AudioCommandThread() processing update audio port list");
Eric Laurent59a89232014-06-08 14:14:17 -07001728 svc = mService.promote();
Eric Laurentb52c1522014-05-20 11:27:36 -07001729 if (svc == 0) {
1730 break;
1731 }
1732 mLock.unlock();
1733 svc->doOnAudioPortListUpdate();
1734 mLock.lock();
1735 }break;
1736 case UPDATE_AUDIOPATCH_LIST: {
1737 ALOGV("AudioCommandThread() processing update audio patch list");
Eric Laurent59a89232014-06-08 14:14:17 -07001738 svc = mService.promote();
Eric Laurentb52c1522014-05-20 11:27:36 -07001739 if (svc == 0) {
1740 break;
1741 }
1742 mLock.unlock();
1743 svc->doOnAudioPatchListUpdate();
1744 mLock.lock();
1745 }break;
François Gaffiecfe17322018-11-07 13:41:29 +01001746 case CHANGED_AUDIOVOLUMEGROUP: {
1747 AudioVolumeGroupData *data =
1748 static_cast<AudioVolumeGroupData *>(command->mParam.get());
1749 ALOGV("AudioCommandThread() processing update audio volume group");
1750 svc = mService.promote();
1751 if (svc == 0) {
1752 break;
1753 }
1754 mLock.unlock();
1755 svc->doOnAudioVolumeGroupChanged(data->mGroup, data->mFlags);
1756 mLock.lock();
1757 }break;
Eric Laurente1715a42014-05-20 11:30:42 -07001758 case SET_AUDIOPORT_CONFIG: {
1759 SetAudioPortConfigData *data = (SetAudioPortConfigData *)command->mParam.get();
1760 ALOGV("AudioCommandThread() processing set port config");
1761 sp<IAudioFlinger> af = AudioSystem::get_audio_flinger();
1762 if (af == 0) {
1763 command->mStatus = PERMISSION_DENIED;
1764 } else {
Andy Hungfe726a62018-09-27 15:17:25 -07001765 mLock.unlock();
Eric Laurente1715a42014-05-20 11:30:42 -07001766 command->mStatus = af->setAudioPortConfig(&data->mConfig);
Andy Hungfe726a62018-09-27 15:17:25 -07001767 mLock.lock();
Eric Laurente1715a42014-05-20 11:30:42 -07001768 }
1769 } break;
Jean-Michel Trivide801052015-04-14 19:10:14 -07001770 case DYN_POLICY_MIX_STATE_UPDATE: {
1771 DynPolicyMixStateUpdateData *data =
1772 (DynPolicyMixStateUpdateData *)command->mParam.get();
Jean-Michel Trivide801052015-04-14 19:10:14 -07001773 ALOGV("AudioCommandThread() processing dyn policy mix state update %s %d",
1774 data->mRegId.string(), data->mState);
1775 svc = mService.promote();
1776 if (svc == 0) {
1777 break;
1778 }
1779 mLock.unlock();
1780 svc->doOnDynamicPolicyMixStateUpdate(data->mRegId, data->mState);
1781 mLock.lock();
1782 } break;
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -08001783 case RECORDING_CONFIGURATION_UPDATE: {
1784 RecordingConfigurationUpdateData *data =
1785 (RecordingConfigurationUpdateData *)command->mParam.get();
1786 ALOGV("AudioCommandThread() processing recording configuration update");
1787 svc = mService.promote();
1788 if (svc == 0) {
1789 break;
1790 }
1791 mLock.unlock();
Jean-Michel Triviac4e4292016-12-22 11:39:31 -08001792 svc->doOnRecordingConfigurationUpdate(data->mEvent, &data->mClientInfo,
Eric Laurenta9f86652018-11-28 17:23:11 -08001793 &data->mClientConfig, data->mClientEffects,
1794 &data->mDeviceConfig, data->mEffects,
1795 data->mPatchHandle, data->mSource);
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -08001796 mLock.lock();
1797 } break;
Eric Laurentb20cf7d2019-04-05 19:37:34 -07001798 case SET_EFFECT_SUSPENDED: {
1799 SetEffectSuspendedData *data = (SetEffectSuspendedData *)command->mParam.get();
1800 ALOGV("AudioCommandThread() processing set effect suspended");
1801 sp<IAudioFlinger> af = AudioSystem::get_audio_flinger();
1802 if (af != 0) {
1803 mLock.unlock();
1804 af->setEffectSuspended(data->mEffectId, data->mSessionId, data->mSuspended);
1805 mLock.lock();
1806 }
1807 } break;
Mikhail Naganov88b30d22020-03-09 19:43:13 +00001808 case AUDIO_MODULES_UPDATE: {
1809 ALOGV("AudioCommandThread() processing audio modules update");
1810 svc = mService.promote();
1811 if (svc == 0) {
1812 break;
1813 }
1814 mLock.unlock();
1815 svc->doOnNewAudioModulesAvailable();
1816 mLock.lock();
1817 } break;
Jean-Michel Trivi9a6b9ad2020-10-22 16:46:43 -07001818 case ROUTING_UPDATED: {
1819 ALOGV("AudioCommandThread() processing routing update");
1820 svc = mService.promote();
1821 if (svc == 0) {
1822 break;
1823 }
1824 mLock.unlock();
1825 svc->doOnRoutingUpdated();
1826 mLock.lock();
1827 } break;
Eric Laurentb20cf7d2019-04-05 19:37:34 -07001828
Eric Laurented726cc2021-07-01 14:26:41 +02001829 case UPDATE_UID_STATES: {
1830 ALOGV("AudioCommandThread() processing updateUID states");
1831 svc = mService.promote();
1832 if (svc == 0) {
1833 break;
1834 }
1835 mLock.unlock();
1836 svc->updateUidStates();
1837 mLock.lock();
1838 } break;
1839
Eric Laurent81dd0f52021-07-05 11:54:40 +02001840 case CHECK_SPATIALIZER: {
1841 ALOGV("AudioCommandThread() processing updateUID states");
1842 svc = mService.promote();
1843 if (svc == 0) {
1844 break;
1845 }
1846 mLock.unlock();
1847 svc->doOnCheckSpatializer();
1848 mLock.lock();
1849 } break;
1850
Mathias Agopian65ab4712010-07-14 17:59:35 -07001851 default:
Steve Block5ff1dd52012-01-05 23:22:43 +00001852 ALOGW("AudioCommandThread() unknown command %d", command->mCommand);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001853 }
Eric Laurent0ede8922014-05-09 18:04:42 -07001854 {
1855 Mutex::Autolock _l(command->mLock);
1856 if (command->mWaitStatus) {
1857 command->mWaitStatus = false;
1858 command->mCond.signal();
1859 }
1860 }
Eric Laurentd7eda8d2016-02-02 17:18:39 -08001861 waitTime = -1;
Zach Janga754b4f2015-10-27 01:29:34 +00001862 // release mLock before releasing strong reference on the service as
1863 // AudioPolicyService destructor calls AudioCommandThread::exit() which
1864 // acquires mLock.
1865 mLock.unlock();
1866 svc.clear();
1867 mLock.lock();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001868 } else {
1869 waitTime = mAudioCommands[0]->mTime - curTime;
1870 break;
1871 }
1872 }
Zach Janga754b4f2015-10-27 01:29:34 +00001873
1874 // release delayed commands wake lock if the queue is empty
1875 if (mAudioCommands.isEmpty()) {
Ricardo Garcia05f2fdc2014-07-24 15:48:24 -07001876 release_wake_lock(mName.string());
Zach Janga754b4f2015-10-27 01:29:34 +00001877 }
1878
1879 // At this stage we have either an empty command queue or the first command in the queue
1880 // has a finite delay. So unless we are exiting it is safe to wait.
1881 if (!exitPending()) {
Eric Laurent59a89232014-06-08 14:14:17 -07001882 ALOGV("AudioCommandThread() going to sleep");
Eric Laurentd7eda8d2016-02-02 17:18:39 -08001883 if (waitTime == -1) {
1884 mWaitWorkCV.wait(mLock);
1885 } else {
1886 mWaitWorkCV.waitRelative(mLock, waitTime);
1887 }
Eric Laurent59a89232014-06-08 14:14:17 -07001888 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001889 }
Ricardo Garcia05f2fdc2014-07-24 15:48:24 -07001890 // release delayed commands wake lock before quitting
1891 if (!mAudioCommands.isEmpty()) {
1892 release_wake_lock(mName.string());
1893 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001894 mLock.unlock();
1895 return false;
1896}
1897
1898status_t AudioPolicyService::AudioCommandThread::dump(int fd)
1899{
1900 const size_t SIZE = 256;
1901 char buffer[SIZE];
1902 String8 result;
1903
1904 snprintf(buffer, SIZE, "AudioCommandThread %p Dump\n", this);
1905 result.append(buffer);
1906 write(fd, result.string(), result.size());
1907
Mikhail Naganov12b716c2020-04-30 22:37:43 +00001908 const bool locked = dumpTryLock(mLock);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001909 if (!locked) {
1910 String8 result2(kCmdDeadlockedString);
1911 write(fd, result2.string(), result2.size());
1912 }
1913
1914 snprintf(buffer, SIZE, "- Commands:\n");
1915 result = String8(buffer);
1916 result.append(" Command Time Wait pParam\n");
Glenn Kasten8d6a2442012-02-08 14:04:28 -08001917 for (size_t i = 0; i < mAudioCommands.size(); i++) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07001918 mAudioCommands[i]->dump(buffer, SIZE);
1919 result.append(buffer);
1920 }
1921 result.append(" Last Command\n");
Eric Laurent0ede8922014-05-09 18:04:42 -07001922 if (mLastCommand != 0) {
1923 mLastCommand->dump(buffer, SIZE);
1924 result.append(buffer);
1925 } else {
1926 result.append(" none\n");
1927 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001928
1929 write(fd, result.string(), result.size());
1930
Mikhail Naganov12b716c2020-04-30 22:37:43 +00001931 dumpReleaseLock(mLock, locked);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001932
1933 return NO_ERROR;
1934}
1935
Glenn Kastenfff6d712012-01-12 16:38:12 -08001936status_t AudioPolicyService::AudioCommandThread::volumeCommand(audio_stream_type_t stream,
Eric Laurentde070132010-07-13 04:45:46 -07001937 float volume,
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001938 audio_io_handle_t output,
Eric Laurentde070132010-07-13 04:45:46 -07001939 int delayMs)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001940{
Eric Laurent0ede8922014-05-09 18:04:42 -07001941 sp<AudioCommand> command = new AudioCommand();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001942 command->mCommand = SET_VOLUME;
Eric Laurent0ede8922014-05-09 18:04:42 -07001943 sp<VolumeData> data = new VolumeData();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001944 data->mStream = stream;
1945 data->mVolume = volume;
1946 data->mIO = output;
1947 command->mParam = data;
Eric Laurent0ede8922014-05-09 18:04:42 -07001948 command->mWaitStatus = true;
Steve Block3856b092011-10-20 11:56:00 +01001949 ALOGV("AudioCommandThread() adding set volume stream %d, volume %f, output %d",
Eric Laurentde070132010-07-13 04:45:46 -07001950 stream, volume, output);
Eric Laurent0ede8922014-05-09 18:04:42 -07001951 return sendCommand(command, delayMs);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001952}
1953
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001954status_t AudioPolicyService::AudioCommandThread::parametersCommand(audio_io_handle_t ioHandle,
Dima Zavinfce7a472011-04-19 22:30:36 -07001955 const char *keyValuePairs,
Eric Laurentde070132010-07-13 04:45:46 -07001956 int delayMs)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001957{
Eric Laurent0ede8922014-05-09 18:04:42 -07001958 sp<AudioCommand> command = new AudioCommand();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001959 command->mCommand = SET_PARAMETERS;
Eric Laurent0ede8922014-05-09 18:04:42 -07001960 sp<ParametersData> data = new ParametersData();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001961 data->mIO = ioHandle;
Dima Zavinfce7a472011-04-19 22:30:36 -07001962 data->mKeyValuePairs = String8(keyValuePairs);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001963 command->mParam = data;
Eric Laurent0ede8922014-05-09 18:04:42 -07001964 command->mWaitStatus = true;
Steve Block3856b092011-10-20 11:56:00 +01001965 ALOGV("AudioCommandThread() adding set parameter string %s, io %d ,delay %d",
Dima Zavinfce7a472011-04-19 22:30:36 -07001966 keyValuePairs, ioHandle, delayMs);
Eric Laurent0ede8922014-05-09 18:04:42 -07001967 return sendCommand(command, delayMs);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001968}
1969
1970status_t AudioPolicyService::AudioCommandThread::voiceVolumeCommand(float volume, int delayMs)
1971{
Eric Laurent0ede8922014-05-09 18:04:42 -07001972 sp<AudioCommand> command = new AudioCommand();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001973 command->mCommand = SET_VOICE_VOLUME;
Eric Laurent0ede8922014-05-09 18:04:42 -07001974 sp<VoiceVolumeData> data = new VoiceVolumeData();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001975 data->mVolume = volume;
1976 command->mParam = data;
Eric Laurent0ede8922014-05-09 18:04:42 -07001977 command->mWaitStatus = true;
Steve Block3856b092011-10-20 11:56:00 +01001978 ALOGV("AudioCommandThread() adding set voice volume volume %f", volume);
Eric Laurent0ede8922014-05-09 18:04:42 -07001979 return sendCommand(command, delayMs);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001980}
1981
Eric Laurentb20cf7d2019-04-05 19:37:34 -07001982void AudioPolicyService::AudioCommandThread::setEffectSuspendedCommand(int effectId,
1983 audio_session_t sessionId,
1984 bool suspended)
1985{
1986 sp<AudioCommand> command = new AudioCommand();
1987 command->mCommand = SET_EFFECT_SUSPENDED;
1988 sp<SetEffectSuspendedData> data = new SetEffectSuspendedData();
1989 data->mEffectId = effectId;
1990 data->mSessionId = sessionId;
1991 data->mSuspended = suspended;
1992 command->mParam = data;
1993 ALOGV("AudioCommandThread() adding set suspended effectId %d sessionId %d suspended %d",
1994 effectId, sessionId, suspended);
1995 sendCommand(command);
1996}
1997
1998
Eric Laurentd7fe0862018-07-14 16:48:01 -07001999void AudioPolicyService::AudioCommandThread::stopOutputCommand(audio_port_handle_t portId)
Eric Laurentbfb1b832013-01-07 09:53:42 -08002000{
Eric Laurent0ede8922014-05-09 18:04:42 -07002001 sp<AudioCommand> command = new AudioCommand();
Eric Laurentbfb1b832013-01-07 09:53:42 -08002002 command->mCommand = STOP_OUTPUT;
Eric Laurent0ede8922014-05-09 18:04:42 -07002003 sp<StopOutputData> data = new StopOutputData();
Eric Laurentd7fe0862018-07-14 16:48:01 -07002004 data->mPortId = portId;
Jesper Tragardh48412dc2014-03-24 14:12:43 +01002005 command->mParam = data;
Eric Laurentd7fe0862018-07-14 16:48:01 -07002006 ALOGV("AudioCommandThread() adding stop output portId %d", portId);
Eric Laurent0ede8922014-05-09 18:04:42 -07002007 sendCommand(command);
Eric Laurentbfb1b832013-01-07 09:53:42 -08002008}
2009
Eric Laurentd7fe0862018-07-14 16:48:01 -07002010void AudioPolicyService::AudioCommandThread::releaseOutputCommand(audio_port_handle_t portId)
Eric Laurentbfb1b832013-01-07 09:53:42 -08002011{
Eric Laurent0ede8922014-05-09 18:04:42 -07002012 sp<AudioCommand> command = new AudioCommand();
Eric Laurentbfb1b832013-01-07 09:53:42 -08002013 command->mCommand = RELEASE_OUTPUT;
Eric Laurent0ede8922014-05-09 18:04:42 -07002014 sp<ReleaseOutputData> data = new ReleaseOutputData();
Eric Laurentd7fe0862018-07-14 16:48:01 -07002015 data->mPortId = portId;
Jesper Tragardh48412dc2014-03-24 14:12:43 +01002016 command->mParam = data;
Eric Laurentd7fe0862018-07-14 16:48:01 -07002017 ALOGV("AudioCommandThread() adding release output portId %d", portId);
Eric Laurent0ede8922014-05-09 18:04:42 -07002018 sendCommand(command);
2019}
2020
Eric Laurent951f4552014-05-20 10:48:17 -07002021status_t AudioPolicyService::AudioCommandThread::createAudioPatchCommand(
2022 const struct audio_patch *patch,
2023 audio_patch_handle_t *handle,
2024 int delayMs)
2025{
2026 status_t status = NO_ERROR;
2027
2028 sp<AudioCommand> command = new AudioCommand();
2029 command->mCommand = CREATE_AUDIO_PATCH;
2030 CreateAudioPatchData *data = new CreateAudioPatchData();
2031 data->mPatch = *patch;
2032 data->mHandle = *handle;
2033 command->mParam = data;
2034 command->mWaitStatus = true;
2035 ALOGV("AudioCommandThread() adding create patch delay %d", delayMs);
2036 status = sendCommand(command, delayMs);
2037 if (status == NO_ERROR) {
2038 *handle = data->mHandle;
2039 }
2040 return status;
2041}
2042
2043status_t AudioPolicyService::AudioCommandThread::releaseAudioPatchCommand(audio_patch_handle_t handle,
2044 int delayMs)
2045{
2046 sp<AudioCommand> command = new AudioCommand();
2047 command->mCommand = RELEASE_AUDIO_PATCH;
2048 ReleaseAudioPatchData *data = new ReleaseAudioPatchData();
2049 data->mHandle = handle;
2050 command->mParam = data;
2051 command->mWaitStatus = true;
2052 ALOGV("AudioCommandThread() adding release patch delay %d", delayMs);
2053 return sendCommand(command, delayMs);
2054}
2055
Eric Laurentb52c1522014-05-20 11:27:36 -07002056void AudioPolicyService::AudioCommandThread::updateAudioPortListCommand()
2057{
2058 sp<AudioCommand> command = new AudioCommand();
2059 command->mCommand = UPDATE_AUDIOPORT_LIST;
2060 ALOGV("AudioCommandThread() adding update audio port list");
2061 sendCommand(command);
2062}
2063
Eric Laurented726cc2021-07-01 14:26:41 +02002064void AudioPolicyService::AudioCommandThread::updateUidStatesCommand()
2065{
2066 sp<AudioCommand> command = new AudioCommand();
2067 command->mCommand = UPDATE_UID_STATES;
2068 ALOGV("AudioCommandThread() adding update UID states");
2069 sendCommand(command);
2070}
2071
Eric Laurentb52c1522014-05-20 11:27:36 -07002072void AudioPolicyService::AudioCommandThread::updateAudioPatchListCommand()
2073{
2074 sp<AudioCommand>command = new AudioCommand();
2075 command->mCommand = UPDATE_AUDIOPATCH_LIST;
2076 ALOGV("AudioCommandThread() adding update audio patch list");
2077 sendCommand(command);
2078}
2079
François Gaffiecfe17322018-11-07 13:41:29 +01002080void AudioPolicyService::AudioCommandThread::changeAudioVolumeGroupCommand(volume_group_t group,
2081 int flags)
2082{
2083 sp<AudioCommand>command = new AudioCommand();
2084 command->mCommand = CHANGED_AUDIOVOLUMEGROUP;
2085 AudioVolumeGroupData *data= new AudioVolumeGroupData();
2086 data->mGroup = group;
2087 data->mFlags = flags;
2088 command->mParam = data;
2089 ALOGV("AudioCommandThread() adding audio volume group changed");
2090 sendCommand(command);
2091}
2092
Eric Laurente1715a42014-05-20 11:30:42 -07002093status_t AudioPolicyService::AudioCommandThread::setAudioPortConfigCommand(
2094 const struct audio_port_config *config, int delayMs)
2095{
2096 sp<AudioCommand> command = new AudioCommand();
2097 command->mCommand = SET_AUDIOPORT_CONFIG;
2098 SetAudioPortConfigData *data = new SetAudioPortConfigData();
2099 data->mConfig = *config;
2100 command->mParam = data;
2101 command->mWaitStatus = true;
2102 ALOGV("AudioCommandThread() adding set port config delay %d", delayMs);
2103 return sendCommand(command, delayMs);
2104}
2105
Jean-Michel Trivide801052015-04-14 19:10:14 -07002106void AudioPolicyService::AudioCommandThread::dynamicPolicyMixStateUpdateCommand(
Chih-Hung Hsiehe964d4e2016-08-09 14:31:32 -07002107 const String8& regId, int32_t state)
Jean-Michel Trivide801052015-04-14 19:10:14 -07002108{
2109 sp<AudioCommand> command = new AudioCommand();
2110 command->mCommand = DYN_POLICY_MIX_STATE_UPDATE;
2111 DynPolicyMixStateUpdateData *data = new DynPolicyMixStateUpdateData();
2112 data->mRegId = regId;
2113 data->mState = state;
2114 command->mParam = data;
2115 ALOGV("AudioCommandThread() sending dynamic policy mix (id=%s) state update to %d",
2116 regId.string(), state);
2117 sendCommand(command);
2118}
2119
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -08002120void AudioPolicyService::AudioCommandThread::recordingConfigurationUpdateCommand(
Eric Laurenta9f86652018-11-28 17:23:11 -08002121 int event,
2122 const record_client_info_t *clientInfo,
2123 const audio_config_base_t *clientConfig,
2124 std::vector<effect_descriptor_t> clientEffects,
2125 const audio_config_base_t *deviceConfig,
2126 std::vector<effect_descriptor_t> effects,
2127 audio_patch_handle_t patchHandle,
2128 audio_source_t source)
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -08002129{
2130 sp<AudioCommand>command = new AudioCommand();
2131 command->mCommand = RECORDING_CONFIGURATION_UPDATE;
2132 RecordingConfigurationUpdateData *data = new RecordingConfigurationUpdateData();
2133 data->mEvent = event;
Jean-Michel Triviac4e4292016-12-22 11:39:31 -08002134 data->mClientInfo = *clientInfo;
Jean-Michel Trivi7281aa92016-02-17 15:33:40 -08002135 data->mClientConfig = *clientConfig;
Eric Laurenta9f86652018-11-28 17:23:11 -08002136 data->mClientEffects = clientEffects;
Jean-Michel Trivi7281aa92016-02-17 15:33:40 -08002137 data->mDeviceConfig = *deviceConfig;
Eric Laurenta9f86652018-11-28 17:23:11 -08002138 data->mEffects = effects;
Jean-Michel Trivi8c7cf3b2016-02-25 17:08:24 -08002139 data->mPatchHandle = patchHandle;
Eric Laurenta9f86652018-11-28 17:23:11 -08002140 data->mSource = source;
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -08002141 command->mParam = data;
Jean-Michel Triviac4e4292016-12-22 11:39:31 -08002142 ALOGV("AudioCommandThread() adding recording configuration update event %d, source %d uid %u",
2143 event, clientInfo->source, clientInfo->uid);
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -08002144 sendCommand(command);
2145}
2146
Mikhail Naganov88b30d22020-03-09 19:43:13 +00002147void AudioPolicyService::AudioCommandThread::audioModulesUpdateCommand()
2148{
2149 sp<AudioCommand> command = new AudioCommand();
2150 command->mCommand = AUDIO_MODULES_UPDATE;
2151 sendCommand(command);
2152}
2153
Jean-Michel Trivi9a6b9ad2020-10-22 16:46:43 -07002154void AudioPolicyService::AudioCommandThread::routingChangedCommand()
2155{
2156 sp<AudioCommand>command = new AudioCommand();
2157 command->mCommand = ROUTING_UPDATED;
2158 ALOGV("AudioCommandThread() adding routing update");
2159 sendCommand(command);
2160}
2161
Eric Laurent81dd0f52021-07-05 11:54:40 +02002162void AudioPolicyService::AudioCommandThread::checkSpatializerCommand()
2163{
2164 sp<AudioCommand>command = new AudioCommand();
2165 command->mCommand = CHECK_SPATIALIZER;
2166 ALOGV("AudioCommandThread() adding check spatializer");
2167 sendCommand(command);
2168}
2169
Eric Laurent0ede8922014-05-09 18:04:42 -07002170status_t AudioPolicyService::AudioCommandThread::sendCommand(sp<AudioCommand>& command, int delayMs)
2171{
2172 {
2173 Mutex::Autolock _l(mLock);
2174 insertCommand_l(command, delayMs);
2175 mWaitWorkCV.signal();
2176 }
2177 Mutex::Autolock _l(command->mLock);
2178 while (command->mWaitStatus) {
2179 nsecs_t timeOutNs = kAudioCommandTimeoutNs + milliseconds(delayMs);
2180 if (command->mCond.waitRelative(command->mLock, timeOutNs) != NO_ERROR) {
2181 command->mStatus = TIMED_OUT;
2182 command->mWaitStatus = false;
2183 }
2184 }
2185 return command->mStatus;
Eric Laurentbfb1b832013-01-07 09:53:42 -08002186}
2187
Mathias Agopian65ab4712010-07-14 17:59:35 -07002188// insertCommand_l() must be called with mLock held
Eric Laurent0ede8922014-05-09 18:04:42 -07002189void AudioPolicyService::AudioCommandThread::insertCommand_l(sp<AudioCommand>& command, int delayMs)
Mathias Agopian65ab4712010-07-14 17:59:35 -07002190{
Glenn Kasten8d6a2442012-02-08 14:04:28 -08002191 ssize_t i; // not size_t because i will count down to -1
Eric Laurent0ede8922014-05-09 18:04:42 -07002192 Vector < sp<AudioCommand> > removedCommands;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002193 command->mTime = systemTime() + milliseconds(delayMs);
2194
2195 // acquire wake lock to make sure delayed commands are processed
Eric Laurentbfb1b832013-01-07 09:53:42 -08002196 if (mAudioCommands.isEmpty()) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07002197 acquire_wake_lock(PARTIAL_WAKE_LOCK, mName.string());
2198 }
2199
2200 // check same pending commands with later time stamps and eliminate them
Ivan Lozano5ff158f2017-10-30 09:06:24 -07002201 for (i = (ssize_t)mAudioCommands.size()-1; i >= 0; i--) {
Eric Laurent0ede8922014-05-09 18:04:42 -07002202 sp<AudioCommand> command2 = mAudioCommands[i];
Mathias Agopian65ab4712010-07-14 17:59:35 -07002203 // commands are sorted by increasing time stamp: no need to scan the rest of mAudioCommands
2204 if (command2->mTime <= command->mTime) break;
Eric Laurente45b48a2014-09-04 16:40:57 -07002205
2206 // create audio patch or release audio patch commands are equivalent
2207 // with regard to filtering
2208 if ((command->mCommand == CREATE_AUDIO_PATCH) ||
2209 (command->mCommand == RELEASE_AUDIO_PATCH)) {
2210 if ((command2->mCommand != CREATE_AUDIO_PATCH) &&
2211 (command2->mCommand != RELEASE_AUDIO_PATCH)) {
2212 continue;
2213 }
2214 } else if (command2->mCommand != command->mCommand) continue;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002215
2216 switch (command->mCommand) {
2217 case SET_PARAMETERS: {
Eric Laurent0ede8922014-05-09 18:04:42 -07002218 ParametersData *data = (ParametersData *)command->mParam.get();
2219 ParametersData *data2 = (ParametersData *)command2->mParam.get();
Mathias Agopian65ab4712010-07-14 17:59:35 -07002220 if (data->mIO != data2->mIO) break;
Steve Block3856b092011-10-20 11:56:00 +01002221 ALOGV("Comparing parameter command %s to new command %s",
Eric Laurentde070132010-07-13 04:45:46 -07002222 data2->mKeyValuePairs.string(), data->mKeyValuePairs.string());
Mathias Agopian65ab4712010-07-14 17:59:35 -07002223 AudioParameter param = AudioParameter(data->mKeyValuePairs);
2224 AudioParameter param2 = AudioParameter(data2->mKeyValuePairs);
2225 for (size_t j = 0; j < param.size(); j++) {
Glenn Kastene53b9ea2012-03-12 16:29:55 -07002226 String8 key;
2227 String8 value;
2228 param.getAt(j, key, value);
2229 for (size_t k = 0; k < param2.size(); k++) {
2230 String8 key2;
2231 String8 value2;
2232 param2.getAt(k, key2, value2);
2233 if (key2 == key) {
2234 param2.remove(key2);
2235 ALOGV("Filtering out parameter %s", key2.string());
2236 break;
2237 }
2238 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07002239 }
2240 // if all keys have been filtered out, remove the command.
2241 // otherwise, update the key value pairs
2242 if (param2.size() == 0) {
2243 removedCommands.add(command2);
2244 } else {
2245 data2->mKeyValuePairs = param2.toString();
2246 }
Eric Laurent21e54562013-09-23 12:08:05 -07002247 command->mTime = command2->mTime;
2248 // force delayMs to non 0 so that code below does not request to wait for
2249 // command status as the command is now delayed
2250 delayMs = 1;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002251 } break;
2252
2253 case SET_VOLUME: {
Eric Laurent0ede8922014-05-09 18:04:42 -07002254 VolumeData *data = (VolumeData *)command->mParam.get();
2255 VolumeData *data2 = (VolumeData *)command2->mParam.get();
Mathias Agopian65ab4712010-07-14 17:59:35 -07002256 if (data->mIO != data2->mIO) break;
2257 if (data->mStream != data2->mStream) break;
Steve Block3856b092011-10-20 11:56:00 +01002258 ALOGV("Filtering out volume command on output %d for stream %d",
Eric Laurentde070132010-07-13 04:45:46 -07002259 data->mIO, data->mStream);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002260 removedCommands.add(command2);
Eric Laurent21e54562013-09-23 12:08:05 -07002261 command->mTime = command2->mTime;
2262 // force delayMs to non 0 so that code below does not request to wait for
2263 // command status as the command is now delayed
2264 delayMs = 1;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002265 } break;
Eric Laurente45b48a2014-09-04 16:40:57 -07002266
Eric Laurentbaf35fe2016-07-27 15:36:53 -07002267 case SET_VOICE_VOLUME: {
2268 VoiceVolumeData *data = (VoiceVolumeData *)command->mParam.get();
2269 VoiceVolumeData *data2 = (VoiceVolumeData *)command2->mParam.get();
2270 ALOGV("Filtering out voice volume command value %f replaced by %f",
2271 data2->mVolume, data->mVolume);
2272 removedCommands.add(command2);
2273 command->mTime = command2->mTime;
2274 // force delayMs to non 0 so that code below does not request to wait for
2275 // command status as the command is now delayed
2276 delayMs = 1;
2277 } break;
2278
Eric Laurente45b48a2014-09-04 16:40:57 -07002279 case CREATE_AUDIO_PATCH:
2280 case RELEASE_AUDIO_PATCH: {
2281 audio_patch_handle_t handle;
Haynes Mathew Georgea2d4a6d2014-10-13 13:05:22 -07002282 struct audio_patch patch;
Eric Laurente45b48a2014-09-04 16:40:57 -07002283 if (command->mCommand == CREATE_AUDIO_PATCH) {
2284 handle = ((CreateAudioPatchData *)command->mParam.get())->mHandle;
Haynes Mathew Georgea2d4a6d2014-10-13 13:05:22 -07002285 patch = ((CreateAudioPatchData *)command->mParam.get())->mPatch;
Eric Laurente45b48a2014-09-04 16:40:57 -07002286 } else {
2287 handle = ((ReleaseAudioPatchData *)command->mParam.get())->mHandle;
Mikhail Naganov7be71d22018-05-23 16:51:46 -07002288 memset(&patch, 0, sizeof(patch));
Eric Laurente45b48a2014-09-04 16:40:57 -07002289 }
2290 audio_patch_handle_t handle2;
Haynes Mathew Georgea2d4a6d2014-10-13 13:05:22 -07002291 struct audio_patch patch2;
Eric Laurente45b48a2014-09-04 16:40:57 -07002292 if (command2->mCommand == CREATE_AUDIO_PATCH) {
2293 handle2 = ((CreateAudioPatchData *)command2->mParam.get())->mHandle;
Haynes Mathew Georgea2d4a6d2014-10-13 13:05:22 -07002294 patch2 = ((CreateAudioPatchData *)command2->mParam.get())->mPatch;
Eric Laurente45b48a2014-09-04 16:40:57 -07002295 } else {
2296 handle2 = ((ReleaseAudioPatchData *)command2->mParam.get())->mHandle;
Glenn Kastenf60b6b62015-07-06 10:53:26 -07002297 memset(&patch2, 0, sizeof(patch2));
Eric Laurente45b48a2014-09-04 16:40:57 -07002298 }
2299 if (handle != handle2) break;
Haynes Mathew Georgea2d4a6d2014-10-13 13:05:22 -07002300 /* Filter CREATE_AUDIO_PATCH commands only when they are issued for
2301 same output. */
2302 if( (command->mCommand == CREATE_AUDIO_PATCH) &&
2303 (command2->mCommand == CREATE_AUDIO_PATCH) ) {
2304 bool isOutputDiff = false;
2305 if (patch.num_sources == patch2.num_sources) {
2306 for (unsigned count = 0; count < patch.num_sources; count++) {
2307 if (patch.sources[count].id != patch2.sources[count].id) {
2308 isOutputDiff = true;
2309 break;
2310 }
2311 }
2312 if (isOutputDiff)
2313 break;
2314 }
2315 }
Eric Laurente45b48a2014-09-04 16:40:57 -07002316 ALOGV("Filtering out %s audio patch command for handle %d",
2317 (command->mCommand == CREATE_AUDIO_PATCH) ? "create" : "release", handle);
2318 removedCommands.add(command2);
2319 command->mTime = command2->mTime;
2320 // force delayMs to non 0 so that code below does not request to wait for
2321 // command status as the command is now delayed
2322 delayMs = 1;
2323 } break;
2324
Jean-Michel Trivide801052015-04-14 19:10:14 -07002325 case DYN_POLICY_MIX_STATE_UPDATE: {
2326
2327 } break;
2328
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -08002329 case RECORDING_CONFIGURATION_UPDATE: {
2330
2331 } break;
2332
Jean-Michel Trivi9a6b9ad2020-10-22 16:46:43 -07002333 case ROUTING_UPDATED: {
2334
2335 } break;
2336
Mathias Agopian65ab4712010-07-14 17:59:35 -07002337 default:
2338 break;
2339 }
2340 }
2341
2342 // remove filtered commands
2343 for (size_t j = 0; j < removedCommands.size(); j++) {
2344 // removed commands always have time stamps greater than current command
2345 for (size_t k = i + 1; k < mAudioCommands.size(); k++) {
Eric Laurent0ede8922014-05-09 18:04:42 -07002346 if (mAudioCommands[k].get() == removedCommands[j].get()) {
Steve Block3856b092011-10-20 11:56:00 +01002347 ALOGV("suppressing command: %d", mAudioCommands[k]->mCommand);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002348 mAudioCommands.removeAt(k);
2349 break;
2350 }
2351 }
2352 }
2353 removedCommands.clear();
2354
Eric Laurentaa79bef2015-01-15 14:33:51 -08002355 // Disable wait for status if delay is not 0.
2356 // Except for create audio patch command because the returned patch handle
2357 // is needed by audio policy manager
2358 if (delayMs != 0 && command->mCommand != CREATE_AUDIO_PATCH) {
Eric Laurentcec4abb2012-07-03 12:23:02 -07002359 command->mWaitStatus = false;
2360 }
Eric Laurentcec4abb2012-07-03 12:23:02 -07002361
Mathias Agopian65ab4712010-07-14 17:59:35 -07002362 // insert command at the right place according to its time stamp
Eric Laurent1e693b52014-07-09 15:03:28 -07002363 ALOGV("inserting command: %d at index %zd, num commands %zu",
2364 command->mCommand, i+1, mAudioCommands.size());
Mathias Agopian65ab4712010-07-14 17:59:35 -07002365 mAudioCommands.insertAt(command, i + 1);
2366}
2367
2368void AudioPolicyService::AudioCommandThread::exit()
2369{
Steve Block3856b092011-10-20 11:56:00 +01002370 ALOGV("AudioCommandThread::exit");
Mathias Agopian65ab4712010-07-14 17:59:35 -07002371 {
2372 AutoMutex _l(mLock);
2373 requestExit();
2374 mWaitWorkCV.signal();
2375 }
Zach Janga754b4f2015-10-27 01:29:34 +00002376 // Note that we can call it from the thread loop if all other references have been released
2377 // but it will safely return WOULD_BLOCK in this case
Mathias Agopian65ab4712010-07-14 17:59:35 -07002378 requestExitAndWait();
2379}
2380
2381void AudioPolicyService::AudioCommandThread::AudioCommand::dump(char* buffer, size_t size)
2382{
2383 snprintf(buffer, size, " %02d %06d.%03d %01u %p\n",
2384 mCommand,
2385 (int)ns2s(mTime),
2386 (int)ns2ms(mTime)%1000,
2387 mWaitStatus,
Eric Laurent0ede8922014-05-09 18:04:42 -07002388 mParam.get());
Mathias Agopian65ab4712010-07-14 17:59:35 -07002389}
2390
Dima Zavinfce7a472011-04-19 22:30:36 -07002391/******* helpers for the service_ops callbacks defined below *********/
2392void AudioPolicyService::setParameters(audio_io_handle_t ioHandle,
2393 const char *keyValuePairs,
2394 int delayMs)
2395{
Glenn Kasten72ef00d2012-01-17 11:09:42 -08002396 mAudioCommandThread->parametersCommand(ioHandle, keyValuePairs,
Dima Zavinfce7a472011-04-19 22:30:36 -07002397 delayMs);
2398}
2399
2400int AudioPolicyService::setStreamVolume(audio_stream_type_t stream,
2401 float volume,
2402 audio_io_handle_t output,
2403 int delayMs)
2404{
Glenn Kastenfff6d712012-01-12 16:38:12 -08002405 return (int)mAudioCommandThread->volumeCommand(stream, volume,
Glenn Kasten72ef00d2012-01-17 11:09:42 -08002406 output, delayMs);
Dima Zavinfce7a472011-04-19 22:30:36 -07002407}
2408
Dima Zavinfce7a472011-04-19 22:30:36 -07002409int AudioPolicyService::setVoiceVolume(float volume, int delayMs)
2410{
2411 return (int)mAudioCommandThread->voiceVolumeCommand(volume, delayMs);
2412}
2413
Eric Laurentb20cf7d2019-04-05 19:37:34 -07002414void AudioPolicyService::setEffectSuspended(int effectId,
2415 audio_session_t sessionId,
2416 bool suspended)
2417{
2418 mAudioCommandThread->setEffectSuspendedCommand(effectId, sessionId, suspended);
2419}
2420
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002421Status AudioPolicyService::onNewAudioModulesAvailable()
Mikhail Naganov88b30d22020-03-09 19:43:13 +00002422{
Mikhail Naganovd0e2c742020-03-25 15:59:59 -07002423 mOutputCommandThread->audioModulesUpdateCommand();
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002424 return Status::ok();
Mikhail Naganov88b30d22020-03-09 19:43:13 +00002425}
2426
Eric Laurentb20cf7d2019-04-05 19:37:34 -07002427
Dima Zavinfce7a472011-04-19 22:30:36 -07002428extern "C" {
Eric Laurent2d388ec2014-03-07 13:25:54 -08002429audio_module_handle_t aps_load_hw_module(void *service __unused,
2430 const char *name);
2431audio_io_handle_t aps_open_output(void *service __unused,
Eric Laurenta4c5a552012-03-29 10:12:40 -07002432 audio_devices_t *pDevices,
2433 uint32_t *pSamplingRate,
2434 audio_format_t *pFormat,
2435 audio_channel_mask_t *pChannelMask,
2436 uint32_t *pLatencyMs,
Eric Laurent2d388ec2014-03-07 13:25:54 -08002437 audio_output_flags_t flags);
Eric Laurenta4c5a552012-03-29 10:12:40 -07002438
Eric Laurent2d388ec2014-03-07 13:25:54 -08002439audio_io_handle_t aps_open_output_on_module(void *service __unused,
Eric Laurenta4c5a552012-03-29 10:12:40 -07002440 audio_module_handle_t module,
2441 audio_devices_t *pDevices,
2442 uint32_t *pSamplingRate,
2443 audio_format_t *pFormat,
2444 audio_channel_mask_t *pChannelMask,
2445 uint32_t *pLatencyMs,
Richard Fitzgeraldad3af332013-03-25 16:54:37 +00002446 audio_output_flags_t flags,
Eric Laurent2d388ec2014-03-07 13:25:54 -08002447 const audio_offload_info_t *offloadInfo);
2448audio_io_handle_t aps_open_dup_output(void *service __unused,
Dima Zavinfce7a472011-04-19 22:30:36 -07002449 audio_io_handle_t output1,
Eric Laurent2d388ec2014-03-07 13:25:54 -08002450 audio_io_handle_t output2);
2451int aps_close_output(void *service __unused, audio_io_handle_t output);
2452int aps_suspend_output(void *service __unused, audio_io_handle_t output);
2453int aps_restore_output(void *service __unused, audio_io_handle_t output);
2454audio_io_handle_t aps_open_input(void *service __unused,
Eric Laurenta4c5a552012-03-29 10:12:40 -07002455 audio_devices_t *pDevices,
2456 uint32_t *pSamplingRate,
2457 audio_format_t *pFormat,
2458 audio_channel_mask_t *pChannelMask,
Eric Laurent2d388ec2014-03-07 13:25:54 -08002459 audio_in_acoustics_t acoustics __unused);
2460audio_io_handle_t aps_open_input_on_module(void *service __unused,
Eric Laurenta4c5a552012-03-29 10:12:40 -07002461 audio_module_handle_t module,
2462 audio_devices_t *pDevices,
2463 uint32_t *pSamplingRate,
2464 audio_format_t *pFormat,
Eric Laurent2d388ec2014-03-07 13:25:54 -08002465 audio_channel_mask_t *pChannelMask);
2466int aps_close_input(void *service __unused, audio_io_handle_t input);
2467int aps_invalidate_stream(void *service __unused, audio_stream_type_t stream);
Glenn Kastend848eb42016-03-08 13:42:11 -08002468int aps_move_effects(void *service __unused, audio_session_t session,
Dima Zavinfce7a472011-04-19 22:30:36 -07002469 audio_io_handle_t src_output,
Eric Laurent2d388ec2014-03-07 13:25:54 -08002470 audio_io_handle_t dst_output);
2471char * aps_get_parameters(void *service __unused, audio_io_handle_t io_handle,
2472 const char *keys);
2473void aps_set_parameters(void *service, audio_io_handle_t io_handle,
2474 const char *kv_pairs, int delay_ms);
2475int aps_set_stream_volume(void *service, audio_stream_type_t stream,
Dima Zavinfce7a472011-04-19 22:30:36 -07002476 float volume, audio_io_handle_t output,
Eric Laurent2d388ec2014-03-07 13:25:54 -08002477 int delay_ms);
Eric Laurent2d388ec2014-03-07 13:25:54 -08002478int aps_set_voice_volume(void *service, float volume, int delay_ms);
2479};
Dima Zavinfce7a472011-04-19 22:30:36 -07002480
Mikhail Naganov1b2a7942017-12-08 10:18:09 -08002481} // namespace android