blob: 9db156956e36467d412f45713af1cf8182623409 [file] [log] [blame]
Mathias Agopian65ab4712010-07-14 17:59:35 -07001/*
2 * Copyright (C) 2009 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#define LOG_TAG "AudioPolicyService"
18//#define LOG_NDEBUG 0
19
Glenn Kasten153b9fe2013-07-15 11:23:36 -070020#include "Configuration.h"
Mathias Agopian65ab4712010-07-14 17:59:35 -070021#undef __STRICT_ANSI__
22#define __STDINT_LIMITS
23#define __STDC_LIMIT_MACROS
24#include <stdint.h>
Mathias Agopian65ab4712010-07-14 17:59:35 -070025#include <sys/time.h>
Jaideep Sharmaba9053b2021-01-25 21:24:26 +053026#include <dlfcn.h>
Mikhail Naganov959e2d02019-03-28 11:08:19 -070027
28#include <audio_utils/clock.h>
Mathias Agopian65ab4712010-07-14 17:59:35 -070029#include <binder/IServiceManager.h>
30#include <utils/Log.h>
31#include <cutils/properties.h>
32#include <binder/IPCThreadState.h>
Svet Ganovf4ddfef2018-01-16 07:37:58 -080033#include <binder/PermissionController.h>
34#include <binder/IResultReceiver.h>
Mathias Agopian65ab4712010-07-14 17:59:35 -070035#include <utils/String16.h>
36#include <utils/threads.h>
37#include "AudioPolicyService.h"
Mathias Agopian65ab4712010-07-14 17:59:35 -070038#include <hardware_legacy/power.h>
Ytai Ben-Tsvi7e7a79d2020-12-15 16:48:16 -080039#include <media/AidlConversion.h>
Eric Laurent7c7f10b2011-06-17 21:29:58 -070040#include <media/AudioEffect.h>
Chih-Hung Hsiehc84d9d22014-11-14 13:33:34 -080041#include <media/AudioParameter.h>
Andy Hungab7ef302018-05-15 19:35:29 -070042#include <mediautils/ServiceUtilities.h>
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -080043#include <mediautils/TimeCheck.h>
Michael Groovercfd28302018-12-11 19:16:46 -080044#include <sensorprivacy/SensorPrivacyManager.h>
Mathias Agopian65ab4712010-07-14 17:59:35 -070045
Dima Zavin64760242011-05-11 14:15:23 -070046#include <system/audio.h>
Dima Zavin7394a4f2011-06-13 18:16:26 -070047#include <system/audio_policy.h>
Jaideep Sharmaba9053b2021-01-25 21:24:26 +053048#include <AudioPolicyManager.h>
Mikhail Naganov61a4fac2016-10-13 14:44:18 -070049
Mathias Agopian65ab4712010-07-14 17:59:35 -070050namespace android {
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -080051using binder::Status;
Mathias Agopian65ab4712010-07-14 17:59:35 -070052
Glenn Kasten8dad0e32012-01-09 08:41:22 -080053static const char kDeadlockedString[] = "AudioPolicyService may be deadlocked\n";
54static const char kCmdDeadlockedString[] = "AudioPolicyService command thread may be deadlocked\n";
Jaideep Sharmaba9053b2021-01-25 21:24:26 +053055static const char kAudioPolicyManagerCustomPath[] = "libaudiopolicymanagercustom.so";
Mathias Agopian65ab4712010-07-14 17:59:35 -070056
Mikhail Naganov959e2d02019-03-28 11:08:19 -070057static const int kDumpLockTimeoutNs = 1 * NANOS_PER_SECOND;
Mathias Agopian65ab4712010-07-14 17:59:35 -070058
Eric Laurent0ede8922014-05-09 18:04:42 -070059static const nsecs_t kAudioCommandTimeoutNs = seconds(3); // 3 seconds
Christer Fletcher5fa8c4b2013-01-18 15:27:03 +010060
Svet Ganovf4ddfef2018-01-16 07:37:58 -080061static const String16 sManageAudioPolicyPermission("android.permission.MANAGE_AUDIO_POLICY");
Dima Zavinfce7a472011-04-19 22:30:36 -070062
Mathias Agopian65ab4712010-07-14 17:59:35 -070063// ----------------------------------------------------------------------------
64
Jaideep Sharmaba9053b2021-01-25 21:24:26 +053065static AudioPolicyInterface* createAudioPolicyManager(AudioPolicyClientInterface *clientInterface)
66{
67 AudioPolicyManager *apm = new AudioPolicyManager(clientInterface);
68 status_t status = apm->initialize();
69 if (status != NO_ERROR) {
70 delete apm;
71 apm = nullptr;
72 }
73 return apm;
74}
75
76static void destroyAudioPolicyManager(AudioPolicyInterface *interface)
77{
78 delete interface;
79}
80// ----------------------------------------------------------------------------
81
Mathias Agopian65ab4712010-07-14 17:59:35 -070082AudioPolicyService::AudioPolicyService()
Ytai Ben-Tsvi85093d52020-03-26 09:41:15 -070083 : BnAudioPolicyService(),
Ytai Ben-Tsvi85093d52020-03-26 09:41:15 -070084 mAudioPolicyManager(NULL),
85 mAudioPolicyClient(NULL),
86 mPhoneState(AUDIO_MODE_INVALID),
Jaideep Sharmaba9053b2021-01-25 21:24:26 +053087 mCaptureStateNotifier(false),
88 mCreateAudioPolicyManager(createAudioPolicyManager),
89 mDestroyAudioPolicyManager(destroyAudioPolicyManager) {
90}
91
92void AudioPolicyService::loadAudioPolicyManager()
93{
94 mLibraryHandle = dlopen(kAudioPolicyManagerCustomPath, RTLD_NOW);
95 if (mLibraryHandle != nullptr) {
96 ALOGI("%s loading %s", __func__, kAudioPolicyManagerCustomPath);
97 mCreateAudioPolicyManager = reinterpret_cast<CreateAudioPolicyManagerInstance>
98 (dlsym(mLibraryHandle, "createAudioPolicyManager"));
99 const char *lastError = dlerror();
100 ALOGW_IF(mCreateAudioPolicyManager == nullptr, "%s createAudioPolicyManager is null %s",
101 __func__, lastError != nullptr ? lastError : "no error");
102
103 mDestroyAudioPolicyManager = reinterpret_cast<DestroyAudioPolicyManagerInstance>(
104 dlsym(mLibraryHandle, "destroyAudioPolicyManager"));
105 lastError = dlerror();
106 ALOGW_IF(mDestroyAudioPolicyManager == nullptr, "%s destroyAudioPolicyManager is null %s",
107 __func__, lastError != nullptr ? lastError : "no error");
108 if (mCreateAudioPolicyManager == nullptr || mDestroyAudioPolicyManager == nullptr){
109 unloadAudioPolicyManager();
110 LOG_ALWAYS_FATAL("could not find audiopolicymanager interface methods");
111 }
112 }
Eric Laurentf5ada6e2014-10-09 17:49:00 -0700113}
114
115void AudioPolicyService::onFirstRef()
116{
Eric Laurent8b1e80b2014-10-07 09:08:47 -0700117 {
118 Mutex::Autolock _l(mLock);
Eric Laurent93575202011-01-18 18:39:02 -0800119
Eric Laurent8b1e80b2014-10-07 09:08:47 -0700120 // start audio commands thread
121 mAudioCommandThread = new AudioCommandThread(String8("ApmAudio"), this);
122 // start output activity command thread
123 mOutputCommandThread = new AudioCommandThread(String8("ApmOutput"), this);
Eric Laurentdce54a12014-03-10 12:19:46 -0700124
Eric Laurent8b1e80b2014-10-07 09:08:47 -0700125 mAudioPolicyClient = new AudioPolicyClient(this);
Jaideep Sharmaba9053b2021-01-25 21:24:26 +0530126
127 loadAudioPolicyManager();
128 mAudioPolicyManager = mCreateAudioPolicyManager(mAudioPolicyClient);
Eric Laurent8b1e80b2014-10-07 09:08:47 -0700129 }
Eric Laurentd66d7a12021-07-13 13:35:32 +0200130
bryant_liuba2b4392014-06-11 16:49:30 +0800131 // load audio processing modules
Mikhail Naganov12b716c2020-04-30 22:37:43 +0000132 sp<AudioPolicyEffects> audioPolicyEffects = new AudioPolicyEffects();
133 sp<UidPolicy> uidPolicy = new UidPolicy(this);
134 sp<SensorPrivacyPolicy> sensorPrivacyPolicy = new SensorPrivacyPolicy(this);
Eric Laurent8b1e80b2014-10-07 09:08:47 -0700135 {
136 Mutex::Autolock _l(mLock);
137 mAudioPolicyEffects = audioPolicyEffects;
Mikhail Naganov12b716c2020-04-30 22:37:43 +0000138 mUidPolicy = uidPolicy;
139 mSensorPrivacyPolicy = sensorPrivacyPolicy;
Eric Laurent8b1e80b2014-10-07 09:08:47 -0700140 }
Mikhail Naganov12b716c2020-04-30 22:37:43 +0000141 uidPolicy->registerSelf();
142 sensorPrivacyPolicy->registerSelf();
Eric Laurentd66d7a12021-07-13 13:35:32 +0200143
Eric Laurent81dd0f52021-07-05 11:54:40 +0200144 // Create spatializer if supported
Eric Laurent52b0bd52021-09-27 15:25:40 +0200145 if (mAudioPolicyManager != nullptr) {
146 Mutex::Autolock _l(mLock);
147 const audio_attributes_t attr = attributes_initializer(AUDIO_USAGE_MEDIA);
148 AudioDeviceTypeAddrVector devices;
149 bool hasSpatializer = mAudioPolicyManager->canBeSpatialized(&attr, nullptr, devices);
150 if (hasSpatializer) {
151 mSpatializer = Spatializer::create(this);
152 }
Eric Laurent81dd0f52021-07-05 11:54:40 +0200153 }
Eric Laurentd66d7a12021-07-13 13:35:32 +0200154 AudioSystem::audioPolicyReady();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700155}
156
Jaideep Sharmaba9053b2021-01-25 21:24:26 +0530157void AudioPolicyService::unloadAudioPolicyManager()
158{
159 ALOGV("%s ", __func__);
160 if (mLibraryHandle != nullptr) {
161 dlclose(mLibraryHandle);
162 }
163 mLibraryHandle = nullptr;
164 mCreateAudioPolicyManager = nullptr;
165 mDestroyAudioPolicyManager = nullptr;
166}
167
Mathias Agopian65ab4712010-07-14 17:59:35 -0700168AudioPolicyService::~AudioPolicyService()
169{
Mathias Agopian65ab4712010-07-14 17:59:35 -0700170 mAudioCommandThread->exit();
Eric Laurent657ff612014-05-07 11:58:24 -0700171 mOutputCommandThread->exit();
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700172
Jaideep Sharmaba9053b2021-01-25 21:24:26 +0530173 mDestroyAudioPolicyManager(mAudioPolicyManager);
174 unloadAudioPolicyManager();
175
Eric Laurentdce54a12014-03-10 12:19:46 -0700176 delete mAudioPolicyClient;
Eric Laurentb52c1522014-05-20 11:27:36 -0700177
178 mNotificationClients.clear();
bryant_liuba2b4392014-06-11 16:49:30 +0800179 mAudioPolicyEffects.clear();
Svet Ganovf4ddfef2018-01-16 07:37:58 -0800180
181 mUidPolicy->unregisterSelf();
Michael Groovercfd28302018-12-11 19:16:46 -0800182 mSensorPrivacyPolicy->unregisterSelf();
Mikhail Naganov12b716c2020-04-30 22:37:43 +0000183
184 mUidPolicy.clear();
Michael Groovercfd28302018-12-11 19:16:46 -0800185 mSensorPrivacyPolicy.clear();
Eric Laurentb52c1522014-05-20 11:27:36 -0700186}
187
188// A notification client is always registered by AudioSystem when the client process
189// connects to AudioPolicyService.
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800190Status AudioPolicyService::registerClient(const sp<media::IAudioPolicyServiceClient>& client)
Eric Laurentb52c1522014-05-20 11:27:36 -0700191{
Eric Laurent12590252015-08-21 18:40:20 -0700192 if (client == 0) {
193 ALOGW("%s got NULL client", __FUNCTION__);
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800194 return Status::ok();
Eric Laurent12590252015-08-21 18:40:20 -0700195 }
Eric Laurent0ebd5f92014-11-19 19:04:52 -0800196 Mutex::Autolock _l(mNotificationClientsLock);
Eric Laurentb52c1522014-05-20 11:27:36 -0700197
198 uid_t uid = IPCThreadState::self()->getCallingUid();
luochaojiang908c7d72018-06-21 14:58:04 +0800199 pid_t pid = IPCThreadState::self()->getCallingPid();
200 int64_t token = ((int64_t)uid<<32) | pid;
201
202 if (mNotificationClients.indexOfKey(token) < 0) {
Eric Laurentb52c1522014-05-20 11:27:36 -0700203 sp<NotificationClient> notificationClient = new NotificationClient(this,
204 client,
luochaojiang908c7d72018-06-21 14:58:04 +0800205 uid,
206 pid);
207 ALOGV("registerClient() client %p, uid %d pid %d", client.get(), uid, pid);
Eric Laurentb52c1522014-05-20 11:27:36 -0700208
luochaojiang908c7d72018-06-21 14:58:04 +0800209 mNotificationClients.add(token, notificationClient);
Eric Laurentb52c1522014-05-20 11:27:36 -0700210
Marco Nelissenf8880202014-11-14 07:58:25 -0800211 sp<IBinder> binder = IInterface::asBinder(client);
Eric Laurentb52c1522014-05-20 11:27:36 -0700212 binder->linkToDeath(notificationClient);
213 }
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800214 return Status::ok();
Eric Laurentb52c1522014-05-20 11:27:36 -0700215}
216
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800217Status AudioPolicyService::setAudioPortCallbacksEnabled(bool enabled)
Eric Laurente8726fe2015-06-26 09:39:24 -0700218{
219 Mutex::Autolock _l(mNotificationClientsLock);
220
221 uid_t uid = IPCThreadState::self()->getCallingUid();
luochaojiang908c7d72018-06-21 14:58:04 +0800222 pid_t pid = IPCThreadState::self()->getCallingPid();
223 int64_t token = ((int64_t)uid<<32) | pid;
224
225 if (mNotificationClients.indexOfKey(token) < 0) {
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800226 return Status::ok();
Eric Laurente8726fe2015-06-26 09:39:24 -0700227 }
luochaojiang908c7d72018-06-21 14:58:04 +0800228 mNotificationClients.valueFor(token)->setAudioPortCallbacksEnabled(enabled);
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800229 return Status::ok();
Eric Laurente8726fe2015-06-26 09:39:24 -0700230}
231
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800232Status AudioPolicyService::setAudioVolumeGroupCallbacksEnabled(bool enabled)
François Gaffiecfe17322018-11-07 13:41:29 +0100233{
234 Mutex::Autolock _l(mNotificationClientsLock);
235
236 uid_t uid = IPCThreadState::self()->getCallingUid();
237 pid_t pid = IPCThreadState::self()->getCallingPid();
238 int64_t token = ((int64_t)uid<<32) | pid;
239
240 if (mNotificationClients.indexOfKey(token) < 0) {
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800241 return Status::ok();
François Gaffiecfe17322018-11-07 13:41:29 +0100242 }
243 mNotificationClients.valueFor(token)->setAudioVolumeGroupCallbacksEnabled(enabled);
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800244 return Status::ok();
François Gaffiecfe17322018-11-07 13:41:29 +0100245}
246
Eric Laurentb52c1522014-05-20 11:27:36 -0700247// removeNotificationClient() is called when the client process dies.
luochaojiang908c7d72018-06-21 14:58:04 +0800248void AudioPolicyService::removeNotificationClient(uid_t uid, pid_t pid)
Eric Laurentb52c1522014-05-20 11:27:36 -0700249{
Mikhail Naganov12b716c2020-04-30 22:37:43 +0000250 bool hasSameUid = false;
Eric Laurent0ebd5f92014-11-19 19:04:52 -0800251 {
252 Mutex::Autolock _l(mNotificationClientsLock);
luochaojiang908c7d72018-06-21 14:58:04 +0800253 int64_t token = ((int64_t)uid<<32) | pid;
254 mNotificationClients.removeItem(token);
luochaojiang908c7d72018-06-21 14:58:04 +0800255 for (size_t i = 0; i < mNotificationClients.size(); i++) {
256 if (mNotificationClients.valueAt(i)->uid() == uid) {
257 hasSameUid = true;
258 break;
259 }
260 }
Mikhail Naganov12b716c2020-04-30 22:37:43 +0000261 }
262 {
263 Mutex::Autolock _l(mLock);
luochaojiang908c7d72018-06-21 14:58:04 +0800264 if (mAudioPolicyManager && !hasSameUid) {
Eric Laurent10b71232018-04-13 18:14:44 -0700265 // called from binder death notification: no need to clear caller identity
Eric Laurent8c7e6da2015-04-21 17:37:00 -0700266 mAudioPolicyManager->releaseResourcesForUid(uid);
Eric Laurentb52c1522014-05-20 11:27:36 -0700267 }
Eric Laurent0ebd5f92014-11-19 19:04:52 -0800268 }
Eric Laurentb52c1522014-05-20 11:27:36 -0700269}
270
271void AudioPolicyService::onAudioPortListUpdate()
272{
273 mOutputCommandThread->updateAudioPortListCommand();
274}
275
276void AudioPolicyService::doOnAudioPortListUpdate()
277{
Eric Laurent0ebd5f92014-11-19 19:04:52 -0800278 Mutex::Autolock _l(mNotificationClientsLock);
Eric Laurentb52c1522014-05-20 11:27:36 -0700279 for (size_t i = 0; i < mNotificationClients.size(); i++) {
280 mNotificationClients.valueAt(i)->onAudioPortListUpdate();
281 }
282}
283
284void AudioPolicyService::onAudioPatchListUpdate()
285{
286 mOutputCommandThread->updateAudioPatchListCommand();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700287}
288
Eric Laurentb52c1522014-05-20 11:27:36 -0700289void AudioPolicyService::doOnAudioPatchListUpdate()
290{
Eric Laurent0ebd5f92014-11-19 19:04:52 -0800291 Mutex::Autolock _l(mNotificationClientsLock);
Eric Laurentb52c1522014-05-20 11:27:36 -0700292 for (size_t i = 0; i < mNotificationClients.size(); i++) {
293 mNotificationClients.valueAt(i)->onAudioPatchListUpdate();
294 }
295}
296
François Gaffiecfe17322018-11-07 13:41:29 +0100297void AudioPolicyService::onAudioVolumeGroupChanged(volume_group_t group, int flags)
298{
299 mOutputCommandThread->changeAudioVolumeGroupCommand(group, flags);
300}
301
302void AudioPolicyService::doOnAudioVolumeGroupChanged(volume_group_t group, int flags)
303{
304 Mutex::Autolock _l(mNotificationClientsLock);
305 for (size_t i = 0; i < mNotificationClients.size(); i++) {
306 mNotificationClients.valueAt(i)->onAudioVolumeGroupChanged(group, flags);
307 }
308}
309
Chih-Hung Hsiehe964d4e2016-08-09 14:31:32 -0700310void AudioPolicyService::onDynamicPolicyMixStateUpdate(const String8& regId, int32_t state)
Jean-Michel Trivide801052015-04-14 19:10:14 -0700311{
312 ALOGV("AudioPolicyService::onDynamicPolicyMixStateUpdate(%s, %d)",
313 regId.string(), state);
314 mOutputCommandThread->dynamicPolicyMixStateUpdateCommand(regId, state);
315}
316
Chih-Hung Hsiehe964d4e2016-08-09 14:31:32 -0700317void AudioPolicyService::doOnDynamicPolicyMixStateUpdate(const String8& regId, int32_t state)
Jean-Michel Trivide801052015-04-14 19:10:14 -0700318{
319 Mutex::Autolock _l(mNotificationClientsLock);
320 for (size_t i = 0; i < mNotificationClients.size(); i++) {
321 mNotificationClients.valueAt(i)->onDynamicPolicyMixStateUpdate(regId, state);
322 }
323}
324
Eric Laurenta9f86652018-11-28 17:23:11 -0800325void AudioPolicyService::onRecordingConfigurationUpdate(
326 int event,
327 const record_client_info_t *clientInfo,
328 const audio_config_base_t *clientConfig,
329 std::vector<effect_descriptor_t> clientEffects,
330 const audio_config_base_t *deviceConfig,
331 std::vector<effect_descriptor_t> effects,
332 audio_patch_handle_t patchHandle,
333 audio_source_t source)
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -0800334{
Jean-Michel Triviac4e4292016-12-22 11:39:31 -0800335 mOutputCommandThread->recordingConfigurationUpdateCommand(event, clientInfo,
Eric Laurenta9f86652018-11-28 17:23:11 -0800336 clientConfig, clientEffects, deviceConfig, effects, patchHandle, source);
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -0800337}
338
Eric Laurenta9f86652018-11-28 17:23:11 -0800339void AudioPolicyService::doOnRecordingConfigurationUpdate(
340 int event,
341 const record_client_info_t *clientInfo,
342 const audio_config_base_t *clientConfig,
343 std::vector<effect_descriptor_t> clientEffects,
344 const audio_config_base_t *deviceConfig,
345 std::vector<effect_descriptor_t> effects,
346 audio_patch_handle_t patchHandle,
347 audio_source_t source)
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -0800348{
349 Mutex::Autolock _l(mNotificationClientsLock);
350 for (size_t i = 0; i < mNotificationClients.size(); i++) {
Jean-Michel Triviac4e4292016-12-22 11:39:31 -0800351 mNotificationClients.valueAt(i)->onRecordingConfigurationUpdate(event, clientInfo,
Eric Laurenta9f86652018-11-28 17:23:11 -0800352 clientConfig, clientEffects, deviceConfig, effects, patchHandle, source);
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -0800353 }
354}
355
Jean-Michel Trivi9a6b9ad2020-10-22 16:46:43 -0700356void AudioPolicyService::onRoutingUpdated()
357{
358 mOutputCommandThread->routingChangedCommand();
359}
360
361void AudioPolicyService::doOnRoutingUpdated()
362{
363 Mutex::Autolock _l(mNotificationClientsLock);
364 for (size_t i = 0; i < mNotificationClients.size(); i++) {
365 mNotificationClients.valueAt(i)->onRoutingUpdated();
366 }
367}
368
Eric Laurent81dd0f52021-07-05 11:54:40 +0200369void AudioPolicyService::onCheckSpatializer()
370{
371 Mutex::Autolock _l(mLock);
Eric Laurent39095982021-08-24 18:29:27 +0200372 onCheckSpatializer_l();
373}
374
375void AudioPolicyService::onCheckSpatializer_l()
376{
377 if (mSpatializer != nullptr) {
378 mOutputCommandThread->checkSpatializerCommand();
379 }
Eric Laurent81dd0f52021-07-05 11:54:40 +0200380}
381
382void AudioPolicyService::doOnCheckSpatializer()
383{
Eric Laurent39095982021-08-24 18:29:27 +0200384 Mutex::Autolock _l(mLock);
Eric Laurent81dd0f52021-07-05 11:54:40 +0200385
Eric Laurent39095982021-08-24 18:29:27 +0200386 if (mSpatializer != nullptr) {
Eric Laurent52b0bd52021-09-27 15:25:40 +0200387 // Note: mSpatializer != nullptr => mAudioPolicyManager != nullptr
Eric Laurent39095982021-08-24 18:29:27 +0200388 if (mSpatializer->getLevel() != media::SpatializationLevel::NONE) {
389 audio_io_handle_t currentOutput = mSpatializer->getOutput();
390 audio_io_handle_t newOutput;
391 const audio_attributes_t attr = attributes_initializer(AUDIO_USAGE_MEDIA);
392 audio_config_base_t config = mSpatializer->getAudioInConfig();
393 status_t status =
394 mAudioPolicyManager->getSpatializerOutput(&config, &attr, &newOutput);
395
396 if (status == NO_ERROR && currentOutput == newOutput) {
397 return;
398 }
399 mLock.unlock();
400 // It is OK to call detachOutput() is none is already attached.
401 mSpatializer->detachOutput();
402 if (status != NO_ERROR || newOutput == AUDIO_IO_HANDLE_NONE) {
Eric Laurent81dd0f52021-07-05 11:54:40 +0200403 mLock.lock();
Eric Laurent39095982021-08-24 18:29:27 +0200404 return;
405 }
406 status = mSpatializer->attachOutput(newOutput);
407 mLock.lock();
408 if (status != NO_ERROR) {
409 mAudioPolicyManager->releaseSpatializerOutput(newOutput);
410 }
411 } else if (mSpatializer->getLevel() == media::SpatializationLevel::NONE
412 && mSpatializer->getOutput() != AUDIO_IO_HANDLE_NONE) {
413 mLock.unlock();
414 audio_io_handle_t output = mSpatializer->detachOutput();
415 mLock.lock();
416 if (output != AUDIO_IO_HANDLE_NONE) {
417 mAudioPolicyManager->releaseSpatializerOutput(output);
Eric Laurent81dd0f52021-07-05 11:54:40 +0200418 }
419 }
420 }
421}
422
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -0800423status_t AudioPolicyService::clientCreateAudioPatch(const struct audio_patch *patch,
424 audio_patch_handle_t *handle,
425 int delayMs)
426{
427 return mAudioCommandThread->createAudioPatchCommand(patch, handle, delayMs);
428}
429
430status_t AudioPolicyService::clientReleaseAudioPatch(audio_patch_handle_t handle,
431 int delayMs)
432{
433 return mAudioCommandThread->releaseAudioPatchCommand(handle, delayMs);
434}
435
Eric Laurente1715a42014-05-20 11:30:42 -0700436status_t AudioPolicyService::clientSetAudioPortConfig(const struct audio_port_config *config,
437 int delayMs)
438{
439 return mAudioCommandThread->setAudioPortConfigCommand(config, delayMs);
440}
441
Ytai Ben-Tsvi7e7a79d2020-12-15 16:48:16 -0800442AudioPolicyService::NotificationClient::NotificationClient(
443 const sp<AudioPolicyService>& service,
444 const sp<media::IAudioPolicyServiceClient>& client,
445 uid_t uid,
446 pid_t pid)
luochaojiang908c7d72018-06-21 14:58:04 +0800447 : mService(service), mUid(uid), mPid(pid), mAudioPolicyServiceClient(client),
François Gaffiecfe17322018-11-07 13:41:29 +0100448 mAudioPortCallbacksEnabled(false), mAudioVolumeGroupCallbacksEnabled(false)
Eric Laurentb52c1522014-05-20 11:27:36 -0700449{
450}
451
452AudioPolicyService::NotificationClient::~NotificationClient()
453{
454}
455
456void AudioPolicyService::NotificationClient::binderDied(const wp<IBinder>& who __unused)
457{
458 sp<NotificationClient> keep(this);
459 sp<AudioPolicyService> service = mService.promote();
460 if (service != 0) {
luochaojiang908c7d72018-06-21 14:58:04 +0800461 service->removeNotificationClient(mUid, mPid);
Eric Laurentb52c1522014-05-20 11:27:36 -0700462 }
463}
464
465void AudioPolicyService::NotificationClient::onAudioPortListUpdate()
466{
Eric Laurente8726fe2015-06-26 09:39:24 -0700467 if (mAudioPolicyServiceClient != 0 && mAudioPortCallbacksEnabled) {
Eric Laurentb52c1522014-05-20 11:27:36 -0700468 mAudioPolicyServiceClient->onAudioPortListUpdate();
469 }
470}
471
472void AudioPolicyService::NotificationClient::onAudioPatchListUpdate()
473{
Eric Laurente8726fe2015-06-26 09:39:24 -0700474 if (mAudioPolicyServiceClient != 0 && mAudioPortCallbacksEnabled) {
Eric Laurentb52c1522014-05-20 11:27:36 -0700475 mAudioPolicyServiceClient->onAudioPatchListUpdate();
476 }
477}
Eric Laurent57dae992011-07-24 13:36:09 -0700478
François Gaffiecfe17322018-11-07 13:41:29 +0100479void AudioPolicyService::NotificationClient::onAudioVolumeGroupChanged(volume_group_t group,
480 int flags)
481{
482 if (mAudioPolicyServiceClient != 0 && mAudioVolumeGroupCallbacksEnabled) {
483 mAudioPolicyServiceClient->onAudioVolumeGroupChanged(group, flags);
484 }
485}
486
487
Jean-Michel Trivide801052015-04-14 19:10:14 -0700488void AudioPolicyService::NotificationClient::onDynamicPolicyMixStateUpdate(
Chih-Hung Hsiehe964d4e2016-08-09 14:31:32 -0700489 const String8& regId, int32_t state)
Jean-Michel Trivide801052015-04-14 19:10:14 -0700490{
Andy Hung4ef19fa2018-05-15 19:35:29 -0700491 if (mAudioPolicyServiceClient != 0 && isServiceUid(mUid)) {
Ytai Ben-Tsvi7e7a79d2020-12-15 16:48:16 -0800492 mAudioPolicyServiceClient->onDynamicPolicyMixStateUpdate(
493 legacy2aidl_String8_string(regId).value(), state);
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -0800494 }
495}
496
497void AudioPolicyService::NotificationClient::onRecordingConfigurationUpdate(
Eric Laurenta9f86652018-11-28 17:23:11 -0800498 int event,
499 const record_client_info_t *clientInfo,
500 const audio_config_base_t *clientConfig,
501 std::vector<effect_descriptor_t> clientEffects,
502 const audio_config_base_t *deviceConfig,
503 std::vector<effect_descriptor_t> effects,
504 audio_patch_handle_t patchHandle,
505 audio_source_t source)
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -0800506{
Andy Hung4ef19fa2018-05-15 19:35:29 -0700507 if (mAudioPolicyServiceClient != 0 && isServiceUid(mUid)) {
Ytai Ben-Tsvi7e7a79d2020-12-15 16:48:16 -0800508 status_t status = [&]() -> status_t {
509 int32_t eventAidl = VALUE_OR_RETURN_STATUS(convertIntegral<int32_t>(event));
510 media::RecordClientInfo clientInfoAidl = VALUE_OR_RETURN_STATUS(
511 legacy2aidl_record_client_info_t_RecordClientInfo(*clientInfo));
Mikhail Naganovdbf03642021-08-25 18:15:32 -0700512 AudioConfigBase clientConfigAidl = VALUE_OR_RETURN_STATUS(
Mikhail Naganovde3fa182021-07-30 15:06:42 -0700513 legacy2aidl_audio_config_base_t_AudioConfigBase(
514 *clientConfig, true /*isInput*/));
Ytai Ben-Tsvi7e7a79d2020-12-15 16:48:16 -0800515 std::vector<media::EffectDescriptor> clientEffectsAidl = VALUE_OR_RETURN_STATUS(
516 convertContainer<std::vector<media::EffectDescriptor>>(
517 clientEffects,
518 legacy2aidl_effect_descriptor_t_EffectDescriptor));
Mikhail Naganovdbf03642021-08-25 18:15:32 -0700519 AudioConfigBase deviceConfigAidl = VALUE_OR_RETURN_STATUS(
Mikhail Naganovde3fa182021-07-30 15:06:42 -0700520 legacy2aidl_audio_config_base_t_AudioConfigBase(
521 *deviceConfig, true /*isInput*/));
Ytai Ben-Tsvi7e7a79d2020-12-15 16:48:16 -0800522 std::vector<media::EffectDescriptor> effectsAidl = VALUE_OR_RETURN_STATUS(
523 convertContainer<std::vector<media::EffectDescriptor>>(
524 effects,
525 legacy2aidl_effect_descriptor_t_EffectDescriptor));
526 int32_t patchHandleAidl = VALUE_OR_RETURN_STATUS(
527 legacy2aidl_audio_patch_handle_t_int32_t(patchHandle));
Mikhail Naganovddceecc2021-09-03 13:58:56 -0700528 media::audio::common::AudioSource sourceAidl = VALUE_OR_RETURN_STATUS(
529 legacy2aidl_audio_source_t_AudioSource(source));
Ytai Ben-Tsvi7e7a79d2020-12-15 16:48:16 -0800530 return aidl_utils::statusTFromBinderStatus(
531 mAudioPolicyServiceClient->onRecordingConfigurationUpdate(eventAidl,
532 clientInfoAidl,
533 clientConfigAidl,
534 clientEffectsAidl,
535 deviceConfigAidl,
536 effectsAidl,
537 patchHandleAidl,
538 sourceAidl));
539 }();
540 ALOGW_IF(status != OK, "onRecordingConfigurationUpdate() failed: %d", status);
Jean-Michel Trivide801052015-04-14 19:10:14 -0700541 }
542}
543
Eric Laurente8726fe2015-06-26 09:39:24 -0700544void AudioPolicyService::NotificationClient::setAudioPortCallbacksEnabled(bool enabled)
545{
546 mAudioPortCallbacksEnabled = enabled;
547}
548
François Gaffiecfe17322018-11-07 13:41:29 +0100549void AudioPolicyService::NotificationClient::setAudioVolumeGroupCallbacksEnabled(bool enabled)
550{
551 mAudioVolumeGroupCallbacksEnabled = enabled;
552}
Eric Laurente8726fe2015-06-26 09:39:24 -0700553
Jean-Michel Trivi9a6b9ad2020-10-22 16:46:43 -0700554void AudioPolicyService::NotificationClient::onRoutingUpdated()
555{
556 if (mAudioPolicyServiceClient != 0 && isServiceUid(mUid)) {
557 mAudioPolicyServiceClient->onRoutingUpdated();
558 }
559}
560
Mathias Agopian65ab4712010-07-14 17:59:35 -0700561void AudioPolicyService::binderDied(const wp<IBinder>& who) {
Glenn Kasten411e4472012-11-02 10:00:06 -0700562 ALOGW("binderDied() %p, calling pid %d", who.unsafe_get(),
Eric Laurentde070132010-07-13 04:45:46 -0700563 IPCThreadState::self()->getCallingPid());
Mathias Agopian65ab4712010-07-14 17:59:35 -0700564}
565
Mikhail Naganov12b716c2020-04-30 22:37:43 +0000566static bool dumpTryLock(Mutex& mutex) ACQUIRE(mutex) NO_THREAD_SAFETY_ANALYSIS
Mathias Agopian65ab4712010-07-14 17:59:35 -0700567{
Mikhail Naganov12b716c2020-04-30 22:37:43 +0000568 return mutex.timedLock(kDumpLockTimeoutNs) == NO_ERROR;
569}
570
571static void dumpReleaseLock(Mutex& mutex, bool locked) RELEASE(mutex) NO_THREAD_SAFETY_ANALYSIS
572{
573 if (locked) mutex.unlock();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700574}
575
576status_t AudioPolicyService::dumpInternals(int fd)
577{
578 const size_t SIZE = 256;
579 char buffer[SIZE];
580 String8 result;
581
Eric Laurentdce54a12014-03-10 12:19:46 -0700582 snprintf(buffer, SIZE, "AudioPolicyManager: %p\n", mAudioPolicyManager);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700583 result.append(buffer);
584 snprintf(buffer, SIZE, "Command Thread: %p\n", mAudioCommandThread.get());
585 result.append(buffer);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700586
Hayden Gomes524159d2019-12-23 14:41:47 -0800587 snprintf(buffer, SIZE, "Supported System Usages:\n");
588 result.append(buffer);
589 for (std::vector<audio_usage_t>::iterator it = mSupportedSystemUsages.begin();
590 it != mSupportedSystemUsages.end(); ++it) {
591 snprintf(buffer, SIZE, "\t%d\n", *it);
592 result.append(buffer);
593 }
594
Mathias Agopian65ab4712010-07-14 17:59:35 -0700595 write(fd, result.string(), result.size());
596 return NO_ERROR;
597}
598
Eric Laurente8c8b432018-10-17 10:08:02 -0700599void AudioPolicyService::updateUidStates()
Svet Ganovf4ddfef2018-01-16 07:37:58 -0800600{
Eric Laurente8c8b432018-10-17 10:08:02 -0700601 Mutex::Autolock _l(mLock);
602 updateUidStates_l();
603}
604
605void AudioPolicyService::updateUidStates_l()
606{
Eric Laurent4eb58f12018-12-07 16:41:02 -0800607// Go over all active clients and allow capture (does not force silence) in the
608// following cases:
Evan Severson1f700cd2021-02-10 13:10:37 -0800609// The client is the assistant
610// AND an accessibility service is on TOP or a RTT call is active
Eric Laurent589171c2019-07-25 18:04:29 -0700611// AND the source is VOICE_RECOGNITION or HOTWORD
Evan Severson1f700cd2021-02-10 13:10:37 -0800612// OR uses VOICE_RECOGNITION AND is on TOP
613// OR uses HOTWORD
614// AND there is no active privacy sensitive capture or call
615// OR client has CAPTURE_AUDIO_OUTPUT privileged permission
616// OR The client is an accessibility service
617// AND Is on TOP
618// AND the source is VOICE_RECOGNITION or HOTWORD
619// OR The assistant is not on TOP
Eric Laurent589171c2019-07-25 18:04:29 -0700620// AND there is no active privacy sensitive capture or call
621// OR client has CAPTURE_AUDIO_OUTPUT privileged permission
Evan Severson1f700cd2021-02-10 13:10:37 -0800622// AND is on TOP
623// AND the source is VOICE_RECOGNITION or HOTWORD
624// OR the client source is virtual (remote submix, call audio TX or RX...)
625// OR the client source is HOTWORD
626// AND is on TOP
627// OR all active clients are using HOTWORD source
628// AND no call is active
629// OR client has CAPTURE_AUDIO_OUTPUT privileged permission
630// OR the client is the current InputMethodService
631// AND a RTT call is active AND the source is VOICE_RECOGNITION
632// OR Any client
633// AND The assistant is not on TOP
634// AND is on TOP or latest started
635// AND there is no active privacy sensitive capture or call
636// OR client has CAPTURE_AUDIO_OUTPUT privileged permission
Eric Laurent4eb58f12018-12-07 16:41:02 -0800637
Eric Laurent4e947da2019-10-17 15:24:06 -0700638
Eric Laurent4eb58f12018-12-07 16:41:02 -0800639 sp<AudioRecordClient> topActive;
640 sp<AudioRecordClient> latestActive;
Eric Laurentc21d5692020-02-25 10:24:36 -0800641 sp<AudioRecordClient> topSensitiveActive;
Eric Laurentb809a752020-06-29 09:53:13 -0700642 sp<AudioRecordClient> latestSensitiveActiveOrComm;
Eric Laurent1ff16a72019-03-14 18:35:04 -0700643
Eric Laurenta46bedb2018-12-07 18:01:26 -0800644 nsecs_t topStartNs = 0;
645 nsecs_t latestStartNs = 0;
Eric Laurentc21d5692020-02-25 10:24:36 -0800646 nsecs_t topSensitiveStartNs = 0;
Eric Laurent4eb58f12018-12-07 16:41:02 -0800647 nsecs_t latestSensitiveStartNs = 0;
648 bool isA11yOnTop = mUidPolicy->isA11yOnTop();
649 bool isAssistantOnTop = false;
650 bool isSensitiveActive = false;
Eric Laurent1ff16a72019-03-14 18:35:04 -0700651 bool isInCall = mPhoneState == AUDIO_MODE_IN_CALL;
Eric Laurentc21d5692020-02-25 10:24:36 -0800652 bool isInCommunication = mPhoneState == AUDIO_MODE_IN_COMMUNICATION;
653 bool rttCallActive = (isInCall || isInCommunication)
Eric Laurent6ede98f2019-06-11 14:50:30 -0700654 && mUidPolicy->isRttEnabled();
Eric Laurent4e947da2019-10-17 15:24:06 -0700655 bool onlyHotwordActive = true;
Eric Laurentb809a752020-06-29 09:53:13 -0700656 bool isPhoneStateOwnerActive = false;
Eric Laurent4eb58f12018-12-07 16:41:02 -0800657
Michael Groovercfd28302018-12-11 19:16:46 -0800658 // if Sensor Privacy is enabled then all recordings should be silenced.
659 if (mSensorPrivacyPolicy->isSensorPrivacyEnabled()) {
660 silenceAllRecordings_l();
661 return;
662 }
663
Eric Laurente8c8b432018-10-17 10:08:02 -0700664 for (size_t i =0; i < mAudioRecordClients.size(); i++) {
665 sp<AudioRecordClient> current = mAudioRecordClients[i];
Svet Ganov33761132021-05-13 22:51:08 +0000666 uid_t currentUid = VALUE_OR_FATAL(aidl2legacy_int32_t_uid_t(
667 current->attributionSource.uid));
Evan Severson1f700cd2021-02-10 13:10:37 -0800668 if (!current->active) {
Eric Laurent1ff16a72019-03-14 18:35:04 -0700669 continue;
Eric Laurent4eb58f12018-12-07 16:41:02 -0800670 }
Eric Laurent1ff16a72019-03-14 18:35:04 -0700671
Philip P. Moltmannbda45752020-07-17 16:41:18 -0700672 app_state_t appState = apmStatFromAmState(mUidPolicy->getUidState(currentUid));
Eric Laurent1ff16a72019-03-14 18:35:04 -0700673 // clients which app is in IDLE state are not eligible for top active or
674 // latest active
675 if (appState == APP_STATE_IDLE) {
676 continue;
677 }
678
Philip P. Moltmannbda45752020-07-17 16:41:18 -0700679 bool isAccessibility = mUidPolicy->isA11yUid(currentUid);
Eric Laurent14a88632020-07-16 12:28:30 -0700680 // Clients capturing for Accessibility services or virtual sources are not considered
Eric Laurentc21d5692020-02-25 10:24:36 -0800681 // for top or latest active to avoid masking regular clients started before
Eric Laurent14a88632020-07-16 12:28:30 -0700682 if (!isAccessibility && !isVirtualSource(current->attributes.source)) {
Philip P. Moltmannbda45752020-07-17 16:41:18 -0700683 bool isAssistant = mUidPolicy->isAssistantUid(currentUid);
Eric Laurentc21d5692020-02-25 10:24:36 -0800684 bool isPrivacySensitive =
685 (current->attributes.flags & AUDIO_FLAG_CAPTURE_PRIVATE) != 0;
Eric Laurentb809a752020-06-29 09:53:13 -0700686
Eric Laurentc21d5692020-02-25 10:24:36 -0800687 if (appState == APP_STATE_TOP) {
688 if (isPrivacySensitive) {
689 if (current->startTimeNs > topSensitiveStartNs) {
690 topSensitiveActive = current;
691 topSensitiveStartNs = current->startTimeNs;
692 }
693 } else {
694 if (current->startTimeNs > topStartNs) {
695 topActive = current;
696 topStartNs = current->startTimeNs;
697 }
698 }
699 if (isAssistant) {
700 isAssistantOnTop = true;
701 }
Eric Laurenta46bedb2018-12-07 18:01:26 -0800702 }
Eric Laurentc21d5692020-02-25 10:24:36 -0800703 // Clients capturing for HOTWORD are not considered
704 // for latest active to avoid masking regular clients started before
705 if (!(current->attributes.source == AUDIO_SOURCE_HOTWORD
706 || ((isA11yOnTop || rttCallActive) && isAssistant))) {
707 if (isPrivacySensitive) {
Eric Laurentb809a752020-06-29 09:53:13 -0700708 // if audio mode is IN_COMMUNICATION, make sure the audio mode owner
709 // is marked latest sensitive active even if another app qualifies.
710 if (current->startTimeNs > latestSensitiveStartNs
Philip P. Moltmannbda45752020-07-17 16:41:18 -0700711 || (isInCommunication && currentUid == mPhoneStateOwnerUid)) {
Eric Laurentb809a752020-06-29 09:53:13 -0700712 if (!isInCommunication || latestSensitiveActiveOrComm == nullptr
Philip P. Moltmannbda45752020-07-17 16:41:18 -0700713 || VALUE_OR_FATAL(aidl2legacy_int32_t_uid_t(
Svet Ganov33761132021-05-13 22:51:08 +0000714 latestSensitiveActiveOrComm->attributionSource.uid))
Philip P. Moltmannbda45752020-07-17 16:41:18 -0700715 != mPhoneStateOwnerUid) {
Eric Laurentb809a752020-06-29 09:53:13 -0700716 latestSensitiveActiveOrComm = current;
717 latestSensitiveStartNs = current->startTimeNs;
718 }
Eric Laurentc21d5692020-02-25 10:24:36 -0800719 }
720 isSensitiveActive = true;
721 } else {
722 if (current->startTimeNs > latestStartNs) {
723 latestActive = current;
724 latestStartNs = current->startTimeNs;
725 }
726 }
Eric Laurent4eb58f12018-12-07 16:41:02 -0800727 }
728 }
Eric Laurent4e947da2019-10-17 15:24:06 -0700729 if (current->attributes.source != AUDIO_SOURCE_HOTWORD) {
730 onlyHotwordActive = false;
731 }
Eric Laurentb0eff0f2021-11-09 16:05:49 +0100732 if (currentUid == mPhoneStateOwnerUid &&
733 !isVirtualSource(current->attributes.source)) {
Eric Laurentb809a752020-06-29 09:53:13 -0700734 isPhoneStateOwnerActive = true;
735 }
Eric Laurent4eb58f12018-12-07 16:41:02 -0800736 }
737
Eric Laurent1ff16a72019-03-14 18:35:04 -0700738 // if no active client with UI on Top, consider latest active as top
739 if (topActive == nullptr) {
740 topActive = latestActive;
Eric Laurentc21d5692020-02-25 10:24:36 -0800741 topStartNs = latestStartNs;
742 }
743 if (topSensitiveActive == nullptr) {
Eric Laurentb809a752020-06-29 09:53:13 -0700744 topSensitiveActive = latestSensitiveActiveOrComm;
Eric Laurentc21d5692020-02-25 10:24:36 -0800745 topSensitiveStartNs = latestSensitiveStartNs;
Eric Laurentb809a752020-06-29 09:53:13 -0700746 } else if (latestSensitiveActiveOrComm != nullptr) {
747 // if audio mode is IN_COMMUNICATION, favor audio mode owner over an app with
748 // foreground UI in case both are capturing with privacy sensitive flag.
Philip P. Moltmannbda45752020-07-17 16:41:18 -0700749 uid_t latestActiveUid = VALUE_OR_FATAL(
Svet Ganov33761132021-05-13 22:51:08 +0000750 aidl2legacy_int32_t_uid_t(latestSensitiveActiveOrComm->attributionSource.uid));
Philip P. Moltmannbda45752020-07-17 16:41:18 -0700751 if (isInCommunication && latestActiveUid == mPhoneStateOwnerUid) {
Eric Laurentb809a752020-06-29 09:53:13 -0700752 topSensitiveActive = latestSensitiveActiveOrComm;
753 topSensitiveStartNs = latestSensitiveStartNs;
754 }
Eric Laurentc21d5692020-02-25 10:24:36 -0800755 }
756
757 // If both privacy sensitive and regular capture are active:
758 // if the regular capture is privileged
759 // allow concurrency
760 // else
761 // favor the privacy sensitive case
762 if (topActive != nullptr && topSensitiveActive != nullptr
Ricardo Correa57a37692020-03-23 17:27:25 -0700763 && !topActive->canCaptureOutput) {
Eric Laurentc21d5692020-02-25 10:24:36 -0800764 topActive = nullptr;
Eric Laurent4eb58f12018-12-07 16:41:02 -0800765 }
766
767 for (size_t i =0; i < mAudioRecordClients.size(); i++) {
768 sp<AudioRecordClient> current = mAudioRecordClients[i];
Philip P. Moltmannbda45752020-07-17 16:41:18 -0700769 uid_t currentUid = VALUE_OR_FATAL(aidl2legacy_int32_t_uid_t(
Svet Ganov33761132021-05-13 22:51:08 +0000770 current->attributionSource.uid));
Eric Laurent1ff16a72019-03-14 18:35:04 -0700771 if (!current->active) {
772 continue;
773 }
774
Eric Laurent4eb58f12018-12-07 16:41:02 -0800775 audio_source_t source = current->attributes.source;
Philip P. Moltmannbda45752020-07-17 16:41:18 -0700776 bool isTopOrLatestActive = topActive == nullptr ? false :
Svet Ganov33761132021-05-13 22:51:08 +0000777 current->attributionSource.uid == topActive->attributionSource.uid;
Philip P. Moltmannbda45752020-07-17 16:41:18 -0700778 bool isTopOrLatestSensitive = topSensitiveActive == nullptr ? false :
Svet Ganov33761132021-05-13 22:51:08 +0000779 current->attributionSource.uid == topSensitiveActive->attributionSource.uid;
Eric Laurentc21d5692020-02-25 10:24:36 -0800780
Mikhail Naganov12b716c2020-04-30 22:37:43 +0000781 auto canCaptureIfInCallOrCommunication = [&](const auto &recordClient) REQUIRES(mLock) {
Philip P. Moltmannbda45752020-07-17 16:41:18 -0700782 uid_t recordUid = VALUE_OR_FATAL(aidl2legacy_int32_t_uid_t(
Svet Ganov33761132021-05-13 22:51:08 +0000783 recordClient->attributionSource.uid));
Ricardo Correa57a37692020-03-23 17:27:25 -0700784 bool canCaptureCall = recordClient->canCaptureOutput;
Eric Laurentb809a752020-06-29 09:53:13 -0700785 bool canCaptureCommunication = recordClient->canCaptureOutput
786 || !isPhoneStateOwnerActive
Philip P. Moltmannbda45752020-07-17 16:41:18 -0700787 || recordUid == mPhoneStateOwnerUid;
Eric Laurentb809a752020-06-29 09:53:13 -0700788 return !(isInCall && !canCaptureCall)
789 && !(isInCommunication && !canCaptureCommunication);
Eric Laurentc21d5692020-02-25 10:24:36 -0800790 };
Eric Laurent1ff16a72019-03-14 18:35:04 -0700791
792 // By default allow capture if:
793 // The assistant is not on TOP
Eric Laurenta171e352019-05-07 13:04:45 -0700794 // AND is on TOP or latest started
Eric Laurent1ff16a72019-03-14 18:35:04 -0700795 // AND there is no active privacy sensitive capture or call
796 // OR client has CAPTURE_AUDIO_OUTPUT privileged permission
797 bool allowCapture = !isAssistantOnTop
Eric Laurentc21d5692020-02-25 10:24:36 -0800798 && (isTopOrLatestActive || isTopOrLatestSensitive)
799 && !(isSensitiveActive
Ricardo Correa57a37692020-03-23 17:27:25 -0700800 && !(isTopOrLatestSensitive || current->canCaptureOutput))
Eric Laurentc21d5692020-02-25 10:24:36 -0800801 && canCaptureIfInCallOrCommunication(current);
Eric Laurent2dc962b2019-03-01 08:25:25 -0800802
Eric Laurented726cc2021-07-01 14:26:41 +0200803 if (!current->hasOp()) {
804 // Never allow capture if app op is denied
805 allowCapture = false;
806 } else if (isVirtualSource(source)) {
Eric Laurent1ff16a72019-03-14 18:35:04 -0700807 // Allow capture for virtual (remote submix, call audio TX or RX...) sources
808 allowCapture = true;
Philip P. Moltmannbda45752020-07-17 16:41:18 -0700809 } else if (mUidPolicy->isAssistantUid(currentUid)) {
Eric Laurent1ff16a72019-03-14 18:35:04 -0700810 // For assistant allow capture if:
Eric Laurent6ede98f2019-06-11 14:50:30 -0700811 // An accessibility service is on TOP or a RTT call is active
Eric Laurent1ff16a72019-03-14 18:35:04 -0700812 // AND the source is VOICE_RECOGNITION or HOTWORD
Eric Laurenta171e352019-05-07 13:04:45 -0700813 // OR is on TOP AND uses VOICE_RECOGNITION
Eric Laurent1ff16a72019-03-14 18:35:04 -0700814 // OR uses HOTWORD
815 // AND there is no active privacy sensitive capture or call
816 // OR client has CAPTURE_AUDIO_OUTPUT privileged permission
Eric Laurent6ede98f2019-06-11 14:50:30 -0700817 if (isA11yOnTop || rttCallActive) {
Eric Laurent4eb58f12018-12-07 16:41:02 -0800818 if (source == AUDIO_SOURCE_HOTWORD || source == AUDIO_SOURCE_VOICE_RECOGNITION) {
Eric Laurent1ff16a72019-03-14 18:35:04 -0700819 allowCapture = true;
Eric Laurent4eb58f12018-12-07 16:41:02 -0800820 }
821 } else {
Eric Laurenta171e352019-05-07 13:04:45 -0700822 if (((isAssistantOnTop && source == AUDIO_SOURCE_VOICE_RECOGNITION) ||
Eric Laurentc21d5692020-02-25 10:24:36 -0800823 source == AUDIO_SOURCE_HOTWORD)
Ricardo Correa57a37692020-03-23 17:27:25 -0700824 && !(isSensitiveActive && !current->canCaptureOutput)
Eric Laurentc21d5692020-02-25 10:24:36 -0800825 && canCaptureIfInCallOrCommunication(current)) {
Eric Laurent1ff16a72019-03-14 18:35:04 -0700826 allowCapture = true;
Eric Laurent4eb58f12018-12-07 16:41:02 -0800827 }
828 }
Philip P. Moltmannbda45752020-07-17 16:41:18 -0700829 } else if (mUidPolicy->isA11yUid(currentUid)) {
Eric Laurent1ff16a72019-03-14 18:35:04 -0700830 // For accessibility service allow capture if:
Eric Laurent47670c92019-08-28 16:59:05 -0700831 // The assistant is not on TOP
832 // AND there is no active privacy sensitive capture or call
Eric Laurent589171c2019-07-25 18:04:29 -0700833 // OR client has CAPTURE_AUDIO_OUTPUT privileged permission
Eric Laurent47670c92019-08-28 16:59:05 -0700834 // OR
835 // Is on TOP AND the source is VOICE_RECOGNITION or HOTWORD
836 if (!isAssistantOnTop
Ricardo Correa57a37692020-03-23 17:27:25 -0700837 && !(isSensitiveActive && !current->canCaptureOutput)
Eric Laurentc21d5692020-02-25 10:24:36 -0800838 && canCaptureIfInCallOrCommunication(current)) {
Eric Laurent47670c92019-08-28 16:59:05 -0700839 allowCapture = true;
840 }
Eric Laurent589171c2019-07-25 18:04:29 -0700841 if (isA11yOnTop) {
842 if (source == AUDIO_SOURCE_VOICE_RECOGNITION || source == AUDIO_SOURCE_HOTWORD) {
843 allowCapture = true;
844 }
Eric Laurent4eb58f12018-12-07 16:41:02 -0800845 }
Eric Laurent4e947da2019-10-17 15:24:06 -0700846 } else if (source == AUDIO_SOURCE_HOTWORD) {
847 // For HOTWORD source allow capture when not on TOP if:
848 // All active clients are using HOTWORD source
849 // AND no call is active
850 // OR client has CAPTURE_AUDIO_OUTPUT privileged permission
Eric Laurentc21d5692020-02-25 10:24:36 -0800851 if (onlyHotwordActive
852 && canCaptureIfInCallOrCommunication(current)) {
Eric Laurent4e947da2019-10-17 15:24:06 -0700853 allowCapture = true;
854 }
Philip P. Moltmannbda45752020-07-17 16:41:18 -0700855 } else if (mUidPolicy->isCurrentImeUid(currentUid)) {
Kohsuke Yatoha623a132020-03-24 20:10:26 -0700856 // For current InputMethodService allow capture if:
857 // A RTT call is active AND the source is VOICE_RECOGNITION
858 if (rttCallActive && source == AUDIO_SOURCE_VOICE_RECOGNITION) {
859 allowCapture = true;
860 }
Eric Laurent4eb58f12018-12-07 16:41:02 -0800861 }
Eric Laurent8c7ef892021-06-10 13:32:16 +0200862 setAppState_l(current,
Philip P. Moltmannbda45752020-07-17 16:41:18 -0700863 allowCapture ? apmStatFromAmState(mUidPolicy->getUidState(currentUid)) :
Eric Laurent1ff16a72019-03-14 18:35:04 -0700864 APP_STATE_IDLE);
Eric Laurente8c8b432018-10-17 10:08:02 -0700865 }
866}
867
Michael Groovercfd28302018-12-11 19:16:46 -0800868void AudioPolicyService::silenceAllRecordings_l() {
869 for (size_t i = 0; i < mAudioRecordClients.size(); i++) {
870 sp<AudioRecordClient> current = mAudioRecordClients[i];
Eric Laurent1ff16a72019-03-14 18:35:04 -0700871 if (!isVirtualSource(current->attributes.source)) {
Eric Laurent8c7ef892021-06-10 13:32:16 +0200872 setAppState_l(current, APP_STATE_IDLE);
Eric Laurent1ff16a72019-03-14 18:35:04 -0700873 }
Michael Groovercfd28302018-12-11 19:16:46 -0800874 }
875}
876
Eric Laurente8c8b432018-10-17 10:08:02 -0700877/* static */
878app_state_t AudioPolicyService::apmStatFromAmState(int amState) {
Eric Laurent1ff16a72019-03-14 18:35:04 -0700879
880 if (amState == ActivityManager::PROCESS_STATE_UNKNOWN) {
Eric Laurente8c8b432018-10-17 10:08:02 -0700881 return APP_STATE_IDLE;
Eric Laurent1ff16a72019-03-14 18:35:04 -0700882 } else if (amState <= ActivityManager::PROCESS_STATE_TOP) {
883 // include persistent services
884 return APP_STATE_TOP;
Eric Laurente8c8b432018-10-17 10:08:02 -0700885 }
886 return APP_STATE_FOREGROUND;
887}
888
Eric Laurent4eb58f12018-12-07 16:41:02 -0800889/* static */
Eric Laurent2dc962b2019-03-01 08:25:25 -0800890bool AudioPolicyService::isVirtualSource(audio_source_t source)
Eric Laurent4eb58f12018-12-07 16:41:02 -0800891{
892 switch (source) {
893 case AUDIO_SOURCE_VOICE_UPLINK:
894 case AUDIO_SOURCE_VOICE_DOWNLINK:
895 case AUDIO_SOURCE_VOICE_CALL:
Eric Laurent2dc962b2019-03-01 08:25:25 -0800896 case AUDIO_SOURCE_REMOTE_SUBMIX:
897 case AUDIO_SOURCE_FM_TUNER:
Eric Laurent68eb2122020-04-30 17:40:57 -0700898 case AUDIO_SOURCE_ECHO_REFERENCE:
Eric Laurent4eb58f12018-12-07 16:41:02 -0800899 return true;
900 default:
901 break;
902 }
903 return false;
904}
905
Eric Laurented726cc2021-07-01 14:26:41 +0200906/* static */
907bool AudioPolicyService::isAppOpSource(audio_source_t source)
908{
909 switch (source) {
910 case AUDIO_SOURCE_FM_TUNER:
911 case AUDIO_SOURCE_ECHO_REFERENCE:
Eric Laurent637bd202021-09-22 11:17:11 +0200912 case AUDIO_SOURCE_REMOTE_SUBMIX:
Eric Laurented726cc2021-07-01 14:26:41 +0200913 return false;
914 default:
915 break;
916 }
917 return true;
918}
919
Eric Laurent8c7ef892021-06-10 13:32:16 +0200920void AudioPolicyService::setAppState_l(sp<AudioRecordClient> client, app_state_t state)
Eric Laurente8c8b432018-10-17 10:08:02 -0700921{
922 AutoCallerClear acc;
923
924 if (mAudioPolicyManager) {
Eric Laurent8c7ef892021-06-10 13:32:16 +0200925 mAudioPolicyManager->setAppState(client->portId, state);
Mikhail Naganoveae73eb2018-04-03 16:57:36 -0700926 }
927 sp<IAudioFlinger> af = AudioSystem::get_audio_flinger();
928 if (af) {
Eric Laurentf32108e2018-10-04 17:22:04 -0700929 bool silenced = state == APP_STATE_IDLE;
Eric Laurent8c7ef892021-06-10 13:32:16 +0200930 if (client->silenced != silenced) {
931 if (client->active) {
932 if (silenced) {
933 finishRecording(client->attributionSource, client->attributes.source);
934 } else {
935 std::stringstream msg;
936 msg << "Audio recording un-silenced on session " << client->session;
937 if (!startRecording(client->attributionSource, String16(msg.str().c_str()),
938 client->attributes.source)) {
939 silenced = true;
940 }
941 }
942 }
943 af->setRecordSilenced(client->portId, silenced);
944 client->silenced = silenced;
945 }
Mikhail Naganoveae73eb2018-04-03 16:57:36 -0700946 }
Svet Ganovf4ddfef2018-01-16 07:37:58 -0800947}
948
Glenn Kasten0f11b512014-01-31 16:18:54 -0800949status_t AudioPolicyService::dump(int fd, const Vector<String16>& args __unused)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700950{
Glenn Kasten44deb052012-02-05 18:09:08 -0800951 if (!dumpAllowed()) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700952 dumpPermissionDenial(fd);
953 } else {
Mikhail Naganov12b716c2020-04-30 22:37:43 +0000954 const bool locked = dumpTryLock(mLock);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700955 if (!locked) {
956 String8 result(kDeadlockedString);
957 write(fd, result.string(), result.size());
958 }
959
960 dumpInternals(fd);
Glenn Kasten9d1f02d2012-02-08 17:47:58 -0800961 if (mAudioCommandThread != 0) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700962 mAudioCommandThread->dump(fd);
963 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700964
Eric Laurentdce54a12014-03-10 12:19:46 -0700965 if (mAudioPolicyManager) {
966 mAudioPolicyManager->dump(fd);
967 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700968
Kevin Rocard8be94972019-02-22 13:26:25 -0800969 mPackageManager.dump(fd);
970
Mikhail Naganov12b716c2020-04-30 22:37:43 +0000971 dumpReleaseLock(mLock, locked);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700972 }
973 return NO_ERROR;
974}
975
976status_t AudioPolicyService::dumpPermissionDenial(int fd)
977{
978 const size_t SIZE = 256;
979 char buffer[SIZE];
980 String8 result;
981 snprintf(buffer, SIZE, "Permission Denial: "
982 "can't dump AudioPolicyService from pid=%d, uid=%d\n",
983 IPCThreadState::self()->getCallingPid(),
984 IPCThreadState::self()->getCallingUid());
985 result.append(buffer);
986 write(fd, result.string(), result.size());
987 return NO_ERROR;
988}
989
990status_t AudioPolicyService::onTransact(
Svet Ganovf4ddfef2018-01-16 07:37:58 -0800991 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags) {
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800992 // make sure transactions reserved to AudioFlinger do not come from other processes
993 switch (code) {
994 case TRANSACTION_startOutput:
995 case TRANSACTION_stopOutput:
996 case TRANSACTION_releaseOutput:
997 case TRANSACTION_getInputForAttr:
998 case TRANSACTION_startInput:
999 case TRANSACTION_stopInput:
1000 case TRANSACTION_releaseInput:
1001 case TRANSACTION_getOutputForEffect:
1002 case TRANSACTION_registerEffect:
1003 case TRANSACTION_unregisterEffect:
1004 case TRANSACTION_setEffectEnabled:
1005 case TRANSACTION_getStrategyForStream:
1006 case TRANSACTION_getOutputForAttr:
1007 case TRANSACTION_moveEffectsToIo:
1008 ALOGW("%s: transaction %d received from PID %d",
1009 __func__, code, IPCThreadState::self()->getCallingPid());
1010 return INVALID_OPERATION;
1011 default:
1012 break;
1013 }
1014
1015 // make sure the following transactions come from system components
1016 switch (code) {
1017 case TRANSACTION_setDeviceConnectionState:
1018 case TRANSACTION_handleDeviceConfigChange:
1019 case TRANSACTION_setPhoneState:
1020//FIXME: Allow setForceUse calls from system apps until a better use case routing API is available
1021// case TRANSACTION_setForceUse:
1022 case TRANSACTION_initStreamVolume:
1023 case TRANSACTION_setStreamVolumeIndex:
1024 case TRANSACTION_setVolumeIndexForAttributes:
1025 case TRANSACTION_getStreamVolumeIndex:
1026 case TRANSACTION_getVolumeIndexForAttributes:
1027 case TRANSACTION_getMinVolumeIndexForAttributes:
1028 case TRANSACTION_getMaxVolumeIndexForAttributes:
1029 case TRANSACTION_isStreamActive:
1030 case TRANSACTION_isStreamActiveRemotely:
1031 case TRANSACTION_isSourceActive:
1032 case TRANSACTION_getDevicesForStream:
1033 case TRANSACTION_registerPolicyMixes:
1034 case TRANSACTION_setMasterMono:
1035 case TRANSACTION_getSurroundFormats:
Kriti Dang6537def2021-03-02 13:46:59 +01001036 case TRANSACTION_getReportedSurroundFormats:
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001037 case TRANSACTION_setSurroundFormatEnabled:
1038 case TRANSACTION_setAssistantUid:
1039 case TRANSACTION_setA11yServicesUids:
1040 case TRANSACTION_setUidDeviceAffinities:
1041 case TRANSACTION_removeUidDeviceAffinities:
1042 case TRANSACTION_setUserIdDeviceAffinities:
1043 case TRANSACTION_removeUserIdDeviceAffinities:
1044 case TRANSACTION_getHwOffloadEncodingFormatsSupportedForA2DP:
1045 case TRANSACTION_listAudioVolumeGroups:
1046 case TRANSACTION_getVolumeGroupFromAudioAttributes:
1047 case TRANSACTION_acquireSoundTriggerSession:
1048 case TRANSACTION_releaseSoundTriggerSession:
1049 case TRANSACTION_setRttEnabled:
1050 case TRANSACTION_isCallScreenModeSupported:
1051 case TRANSACTION_setDevicesRoleForStrategy:
1052 case TRANSACTION_setSupportedSystemUsages:
1053 case TRANSACTION_removeDevicesRoleForStrategy:
1054 case TRANSACTION_getDevicesForRoleAndStrategy:
1055 case TRANSACTION_getDevicesForAttributes:
1056 case TRANSACTION_setAllowedCapturePolicy:
1057 case TRANSACTION_onNewAudioModulesAvailable:
1058 case TRANSACTION_setCurrentImeUid:
1059 case TRANSACTION_registerSoundTriggerCaptureStateListener:
1060 case TRANSACTION_setDevicesRoleForCapturePreset:
1061 case TRANSACTION_addDevicesRoleForCapturePreset:
1062 case TRANSACTION_removeDevicesRoleForCapturePreset:
1063 case TRANSACTION_clearDevicesRoleForCapturePreset:
Eric Laurent81dd0f52021-07-05 11:54:40 +02001064 case TRANSACTION_getDevicesForRoleAndCapturePreset:
1065 case TRANSACTION_getSpatializer: {
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001066 if (!isServiceUid(IPCThreadState::self()->getCallingUid())) {
1067 ALOGW("%s: transaction %d received from PID %d unauthorized UID %d",
1068 __func__, code, IPCThreadState::self()->getCallingPid(),
1069 IPCThreadState::self()->getCallingUid());
1070 return INVALID_OPERATION;
1071 }
1072 } break;
1073 default:
1074 break;
1075 }
1076
1077 std::string tag("IAudioPolicyService command " + std::to_string(code));
1078 TimeCheck check(tag.c_str());
1079
Svet Ganovf4ddfef2018-01-16 07:37:58 -08001080 switch (code) {
1081 case SHELL_COMMAND_TRANSACTION: {
1082 int in = data.readFileDescriptor();
1083 int out = data.readFileDescriptor();
1084 int err = data.readFileDescriptor();
1085 int argc = data.readInt32();
1086 Vector<String16> args;
1087 for (int i = 0; i < argc && data.dataAvail() > 0; i++) {
1088 args.add(data.readString16());
1089 }
1090 sp<IBinder> unusedCallback;
1091 sp<IResultReceiver> resultReceiver;
1092 status_t status;
1093 if ((status = data.readNullableStrongBinder(&unusedCallback)) != NO_ERROR) {
1094 return status;
1095 }
1096 if ((status = data.readNullableStrongBinder(&resultReceiver)) != NO_ERROR) {
1097 return status;
1098 }
1099 status = shellCommand(in, out, err, args);
1100 if (resultReceiver != nullptr) {
1101 resultReceiver->send(status);
1102 }
1103 return NO_ERROR;
1104 }
1105 }
1106
Mathias Agopian65ab4712010-07-14 17:59:35 -07001107 return BnAudioPolicyService::onTransact(code, data, reply, flags);
1108}
1109
Svet Ganovf4ddfef2018-01-16 07:37:58 -08001110// ------------------- Shell command implementation -------------------
1111
1112// NOTE: This is a remote API - make sure all args are validated
1113status_t AudioPolicyService::shellCommand(int in, int out, int err, Vector<String16>& args) {
1114 if (!checkCallingPermission(sManageAudioPolicyPermission, nullptr, nullptr)) {
1115 return PERMISSION_DENIED;
1116 }
1117 if (in == BAD_TYPE || out == BAD_TYPE || err == BAD_TYPE) {
1118 return BAD_VALUE;
1119 }
jovanakbe066e12019-09-02 11:54:39 -07001120 if (args.size() >= 3 && args[0] == String16("set-uid-state")) {
Svet Ganovf4ddfef2018-01-16 07:37:58 -08001121 return handleSetUidState(args, err);
jovanakbe066e12019-09-02 11:54:39 -07001122 } else if (args.size() >= 2 && args[0] == String16("reset-uid-state")) {
Svet Ganovf4ddfef2018-01-16 07:37:58 -08001123 return handleResetUidState(args, err);
jovanakbe066e12019-09-02 11:54:39 -07001124 } else if (args.size() >= 2 && args[0] == String16("get-uid-state")) {
Svet Ganovf4ddfef2018-01-16 07:37:58 -08001125 return handleGetUidState(args, out, err);
Eric Laurent269acb42021-04-23 16:53:22 +02001126 } else if (args.size() >= 1 && args[0] == String16("purge_permission-cache")) {
1127 purgePermissionCache();
1128 return NO_ERROR;
Svet Ganovf4ddfef2018-01-16 07:37:58 -08001129 } else if (args.size() == 1 && args[0] == String16("help")) {
1130 printHelp(out);
1131 return NO_ERROR;
1132 }
1133 printHelp(err);
1134 return BAD_VALUE;
1135}
1136
jovanakbe066e12019-09-02 11:54:39 -07001137static status_t getUidForPackage(String16 packageName, int userId, /*inout*/uid_t& uid, int err) {
1138 if (userId < 0) {
1139 ALOGE("Invalid user: %d", userId);
1140 dprintf(err, "Invalid user: %d\n", userId);
Svet Ganovf4ddfef2018-01-16 07:37:58 -08001141 return BAD_VALUE;
1142 }
jovanakbe066e12019-09-02 11:54:39 -07001143
1144 PermissionController pc;
1145 uid = pc.getPackageUid(packageName, 0);
1146 if (uid <= 0) {
1147 ALOGE("Unknown package: '%s'", String8(packageName).string());
1148 dprintf(err, "Unknown package: '%s'\n", String8(packageName).string());
1149 return BAD_VALUE;
1150 }
1151
1152 uid = multiuser_get_uid(userId, uid);
1153 return NO_ERROR;
1154}
1155
1156status_t AudioPolicyService::handleSetUidState(Vector<String16>& args, int err) {
1157 // Valid arg.size() is 3 or 5, args.size() is 5 with --user option.
1158 if (!(args.size() == 3 || args.size() == 5)) {
1159 printHelp(err);
1160 return BAD_VALUE;
1161 }
1162
Svet Ganovf4ddfef2018-01-16 07:37:58 -08001163 bool active = false;
1164 if (args[2] == String16("active")) {
1165 active = true;
1166 } else if ((args[2] != String16("idle"))) {
1167 ALOGE("Expected active or idle but got: '%s'", String8(args[2]).string());
1168 return BAD_VALUE;
1169 }
jovanakbe066e12019-09-02 11:54:39 -07001170
1171 int userId = 0;
1172 if (args.size() >= 5 && args[3] == String16("--user")) {
1173 userId = atoi(String8(args[4]));
1174 }
1175
1176 uid_t uid;
1177 if (getUidForPackage(args[1], userId, uid, err) == BAD_VALUE) {
1178 return BAD_VALUE;
1179 }
1180
Mikhail Naganov12b716c2020-04-30 22:37:43 +00001181 sp<UidPolicy> uidPolicy;
1182 {
1183 Mutex::Autolock _l(mLock);
1184 uidPolicy = mUidPolicy;
1185 }
1186 if (uidPolicy) {
1187 uidPolicy->addOverrideUid(uid, active);
1188 return NO_ERROR;
1189 }
1190 return NO_INIT;
Svet Ganovf4ddfef2018-01-16 07:37:58 -08001191}
1192
1193status_t AudioPolicyService::handleResetUidState(Vector<String16>& args, int err) {
jovanakbe066e12019-09-02 11:54:39 -07001194 // Valid arg.size() is 2 or 4, args.size() is 4 with --user option.
1195 if (!(args.size() == 2 || args.size() == 4)) {
1196 printHelp(err);
Svet Ganovf4ddfef2018-01-16 07:37:58 -08001197 return BAD_VALUE;
1198 }
jovanakbe066e12019-09-02 11:54:39 -07001199
1200 int userId = 0;
1201 if (args.size() >= 4 && args[2] == String16("--user")) {
1202 userId = atoi(String8(args[3]));
1203 }
1204
1205 uid_t uid;
1206 if (getUidForPackage(args[1], userId, uid, err) == BAD_VALUE) {
1207 return BAD_VALUE;
1208 }
1209
Mikhail Naganov12b716c2020-04-30 22:37:43 +00001210 sp<UidPolicy> uidPolicy;
1211 {
1212 Mutex::Autolock _l(mLock);
1213 uidPolicy = mUidPolicy;
1214 }
1215 if (uidPolicy) {
1216 uidPolicy->removeOverrideUid(uid);
1217 return NO_ERROR;
1218 }
1219 return NO_INIT;
Svet Ganovf4ddfef2018-01-16 07:37:58 -08001220}
1221
1222status_t AudioPolicyService::handleGetUidState(Vector<String16>& args, int out, int err) {
jovanakbe066e12019-09-02 11:54:39 -07001223 // Valid arg.size() is 2 or 4, args.size() is 4 with --user option.
1224 if (!(args.size() == 2 || args.size() == 4)) {
1225 printHelp(err);
Svet Ganovf4ddfef2018-01-16 07:37:58 -08001226 return BAD_VALUE;
1227 }
jovanakbe066e12019-09-02 11:54:39 -07001228
1229 int userId = 0;
1230 if (args.size() >= 4 && args[2] == String16("--user")) {
1231 userId = atoi(String8(args[3]));
1232 }
1233
1234 uid_t uid;
1235 if (getUidForPackage(args[1], userId, uid, err) == BAD_VALUE) {
1236 return BAD_VALUE;
1237 }
1238
Mikhail Naganov12b716c2020-04-30 22:37:43 +00001239 sp<UidPolicy> uidPolicy;
1240 {
1241 Mutex::Autolock _l(mLock);
1242 uidPolicy = mUidPolicy;
Svet Ganovf4ddfef2018-01-16 07:37:58 -08001243 }
Mikhail Naganov12b716c2020-04-30 22:37:43 +00001244 if (uidPolicy) {
1245 return dprintf(out, uidPolicy->isUidActive(uid) ? "active\n" : "idle\n");
1246 }
1247 return NO_INIT;
Svet Ganovf4ddfef2018-01-16 07:37:58 -08001248}
1249
1250status_t AudioPolicyService::printHelp(int out) {
1251 return dprintf(out, "Audio policy service commands:\n"
jovanakbe066e12019-09-02 11:54:39 -07001252 " get-uid-state <PACKAGE> [--user USER_ID] gets the uid state\n"
1253 " set-uid-state <PACKAGE> <active|idle> [--user USER_ID] overrides the uid state\n"
1254 " reset-uid-state <PACKAGE> [--user USER_ID] clears the uid state override\n"
Svet Ganovf4ddfef2018-01-16 07:37:58 -08001255 " help print this message\n");
1256}
1257
1258// ----------- AudioPolicyService::UidPolicy implementation ----------
1259
1260void AudioPolicyService::UidPolicy::registerSelf() {
Steven Moreland2f348142019-07-02 15:59:07 -07001261 status_t res = mAm.linkToDeath(this);
1262 mAm.registerUidObserver(this, ActivityManager::UID_OBSERVER_GONE
Svet Ganovf4ddfef2018-01-16 07:37:58 -08001263 | ActivityManager::UID_OBSERVER_IDLE
Eric Laurente8c8b432018-10-17 10:08:02 -07001264 | ActivityManager::UID_OBSERVER_ACTIVE
1265 | ActivityManager::UID_OBSERVER_PROCSTATE,
Svet Ganovf4ddfef2018-01-16 07:37:58 -08001266 ActivityManager::PROCESS_STATE_UNKNOWN,
1267 String16("audioserver"));
Mikhail Naganoveae73eb2018-04-03 16:57:36 -07001268 if (!res) {
1269 Mutex::Autolock _l(mLock);
1270 mObserverRegistered = true;
1271 } else {
1272 ALOGE("UidPolicy::registerSelf linkToDeath failed: %d", res);
Eric Laurent4eb58f12018-12-07 16:41:02 -08001273
Steven Moreland2f348142019-07-02 15:59:07 -07001274 mAm.unregisterUidObserver(this);
Mikhail Naganoveae73eb2018-04-03 16:57:36 -07001275 }
Svet Ganovf4ddfef2018-01-16 07:37:58 -08001276}
1277
1278void AudioPolicyService::UidPolicy::unregisterSelf() {
Steven Moreland2f348142019-07-02 15:59:07 -07001279 mAm.unlinkToDeath(this);
1280 mAm.unregisterUidObserver(this);
Mikhail Naganoveae73eb2018-04-03 16:57:36 -07001281 Mutex::Autolock _l(mLock);
1282 mObserverRegistered = false;
Svet Ganovf4ddfef2018-01-16 07:37:58 -08001283}
1284
Mikhail Naganoveae73eb2018-04-03 16:57:36 -07001285void AudioPolicyService::UidPolicy::binderDied(__unused const wp<IBinder> &who) {
1286 Mutex::Autolock _l(mLock);
1287 mCachedUids.clear();
1288 mObserverRegistered = false;
Svet Ganovf4ddfef2018-01-16 07:37:58 -08001289}
1290
Eric Laurente8c8b432018-10-17 10:08:02 -07001291void AudioPolicyService::UidPolicy::checkRegistered() {
Mikhail Naganoveae73eb2018-04-03 16:57:36 -07001292 bool needToReregister = false;
1293 {
1294 Mutex::Autolock _l(mLock);
1295 needToReregister = !mObserverRegistered;
Svet Ganovf4ddfef2018-01-16 07:37:58 -08001296 }
Mikhail Naganoveae73eb2018-04-03 16:57:36 -07001297 if (needToReregister) {
1298 // Looks like ActivityManager has died previously, attempt to re-register.
1299 registerSelf();
1300 }
Eric Laurente8c8b432018-10-17 10:08:02 -07001301}
1302
1303bool AudioPolicyService::UidPolicy::isUidActive(uid_t uid) {
1304 if (isServiceUid(uid)) return true;
1305 checkRegistered();
Mikhail Naganoveae73eb2018-04-03 16:57:36 -07001306 {
1307 Mutex::Autolock _l(mLock);
1308 auto overrideIter = mOverrideUids.find(uid);
1309 if (overrideIter != mOverrideUids.end()) {
Eric Laurente8c8b432018-10-17 10:08:02 -07001310 return overrideIter->second.first;
Mikhail Naganoveae73eb2018-04-03 16:57:36 -07001311 }
1312 // In an absense of the ActivityManager, assume everything to be active.
1313 if (!mObserverRegistered) return true;
1314 auto cacheIter = mCachedUids.find(uid);
Mikhail Naganoveba668a2018-04-05 08:13:15 -07001315 if (cacheIter != mCachedUids.end()) {
Eric Laurente8c8b432018-10-17 10:08:02 -07001316 return cacheIter->second.first;
Mikhail Naganoveae73eb2018-04-03 16:57:36 -07001317 }
1318 }
1319 ActivityManager am;
Hui Yu12c7ec72020-05-04 17:40:52 +00001320 bool active = am.isUidActive(uid, String16("audioserver"));
Mikhail Naganoveae73eb2018-04-03 16:57:36 -07001321 {
1322 Mutex::Autolock _l(mLock);
Eric Laurente8c8b432018-10-17 10:08:02 -07001323 mCachedUids.insert(std::pair<uid_t,
1324 std::pair<bool, int>>(uid, std::pair<bool, int>(active,
1325 ActivityManager::PROCESS_STATE_UNKNOWN)));
Mikhail Naganoveae73eb2018-04-03 16:57:36 -07001326 }
1327 return active;
Svet Ganovf4ddfef2018-01-16 07:37:58 -08001328}
1329
Eric Laurente8c8b432018-10-17 10:08:02 -07001330int AudioPolicyService::UidPolicy::getUidState(uid_t uid) {
1331 if (isServiceUid(uid)) {
1332 return ActivityManager::PROCESS_STATE_TOP;
1333 }
1334 checkRegistered();
1335 {
1336 Mutex::Autolock _l(mLock);
1337 auto overrideIter = mOverrideUids.find(uid);
1338 if (overrideIter != mOverrideUids.end()) {
1339 if (overrideIter->second.first) {
1340 if (overrideIter->second.second != ActivityManager::PROCESS_STATE_UNKNOWN) {
1341 return overrideIter->second.second;
1342 } else {
1343 auto cacheIter = mCachedUids.find(uid);
1344 if (cacheIter != mCachedUids.end()) {
1345 return cacheIter->second.second;
1346 }
1347 }
1348 }
1349 return ActivityManager::PROCESS_STATE_UNKNOWN;
1350 }
1351 // In an absense of the ActivityManager, assume everything to be active.
1352 if (!mObserverRegistered) {
1353 return ActivityManager::PROCESS_STATE_TOP;
1354 }
1355 auto cacheIter = mCachedUids.find(uid);
1356 if (cacheIter != mCachedUids.end()) {
1357 if (cacheIter->second.first) {
1358 return cacheIter->second.second;
1359 } else {
1360 return ActivityManager::PROCESS_STATE_UNKNOWN;
1361 }
1362 }
1363 }
1364 ActivityManager am;
Hui Yu12c7ec72020-05-04 17:40:52 +00001365 bool active = am.isUidActive(uid, String16("audioserver"));
Eric Laurente8c8b432018-10-17 10:08:02 -07001366 int state = ActivityManager::PROCESS_STATE_UNKNOWN;
1367 if (active) {
1368 state = am.getUidProcessState(uid, String16("audioserver"));
1369 }
1370 {
1371 Mutex::Autolock _l(mLock);
1372 mCachedUids.insert(std::pair<uid_t,
1373 std::pair<bool, int>>(uid, std::pair<bool, int>(active, state)));
1374 }
Eric Laurent4eb58f12018-12-07 16:41:02 -08001375
Eric Laurente8c8b432018-10-17 10:08:02 -07001376 return state;
1377}
1378
Mikhail Naganoveae73eb2018-04-03 16:57:36 -07001379void AudioPolicyService::UidPolicy::onUidActive(uid_t uid) {
Eric Laurente8c8b432018-10-17 10:08:02 -07001380 updateUid(&mCachedUids, uid, true, ActivityManager::PROCESS_STATE_UNKNOWN, true);
Mikhail Naganoveae73eb2018-04-03 16:57:36 -07001381}
1382
1383void AudioPolicyService::UidPolicy::onUidGone(uid_t uid, __unused bool disabled) {
Eric Laurente8c8b432018-10-17 10:08:02 -07001384 updateUid(&mCachedUids, uid, false, ActivityManager::PROCESS_STATE_UNKNOWN, false);
Mikhail Naganoveae73eb2018-04-03 16:57:36 -07001385}
1386
1387void AudioPolicyService::UidPolicy::onUidIdle(uid_t uid, __unused bool disabled) {
Eric Laurente8c8b432018-10-17 10:08:02 -07001388 updateUid(&mCachedUids, uid, false, ActivityManager::PROCESS_STATE_UNKNOWN, true);
Mikhail Naganoveae73eb2018-04-03 16:57:36 -07001389}
1390
Eric Laurente8c8b432018-10-17 10:08:02 -07001391void AudioPolicyService::UidPolicy::onUidStateChanged(uid_t uid,
1392 int32_t procState,
Hui Yu13ad0eb2019-09-09 10:27:07 -07001393 int64_t procStateSeq __unused,
1394 int32_t capability __unused) {
Eric Laurente8c8b432018-10-17 10:08:02 -07001395 if (procState != ActivityManager::PROCESS_STATE_UNKNOWN) {
1396 updateUid(&mCachedUids, uid, true, procState, true);
Mikhail Naganoveae73eb2018-04-03 16:57:36 -07001397 }
1398}
1399
1400void AudioPolicyService::UidPolicy::updateOverrideUid(uid_t uid, bool active, bool insert) {
Eric Laurente8c8b432018-10-17 10:08:02 -07001401 updateUid(&mOverrideUids, uid, active, ActivityManager::PROCESS_STATE_UNKNOWN, insert);
1402}
1403
1404void AudioPolicyService::UidPolicy::notifyService() {
1405 sp<AudioPolicyService> service = mService.promote();
1406 if (service != nullptr) {
1407 service->updateUidStates();
Mikhail Naganoveae73eb2018-04-03 16:57:36 -07001408 }
1409}
1410
Eric Laurente8c8b432018-10-17 10:08:02 -07001411void AudioPolicyService::UidPolicy::updateUid(std::unordered_map<uid_t,
1412 std::pair<bool, int>> *uids,
1413 uid_t uid,
1414 bool active,
1415 int state,
1416 bool insert) {
1417 if (isServiceUid(uid)) {
1418 return;
1419 }
1420 bool wasActive = isUidActive(uid);
1421 int previousState = getUidState(uid);
Mikhail Naganoveae73eb2018-04-03 16:57:36 -07001422 {
1423 Mutex::Autolock _l(mLock);
Eric Laurente8c8b432018-10-17 10:08:02 -07001424 updateUidLocked(uids, uid, active, state, insert);
Mikhail Naganoveae73eb2018-04-03 16:57:36 -07001425 }
Eric Laurente8c8b432018-10-17 10:08:02 -07001426 if (wasActive != isUidActive(uid) || state != previousState) {
1427 notifyService();
1428 }
Mikhail Naganoveae73eb2018-04-03 16:57:36 -07001429}
1430
Eric Laurente8c8b432018-10-17 10:08:02 -07001431void AudioPolicyService::UidPolicy::updateUidLocked(std::unordered_map<uid_t,
1432 std::pair<bool, int>> *uids,
1433 uid_t uid,
1434 bool active,
1435 int state,
1436 bool insert) {
Mikhail Naganoveae73eb2018-04-03 16:57:36 -07001437 auto it = uids->find(uid);
1438 if (it != uids->end()) {
Mikhail Naganoveae73eb2018-04-03 16:57:36 -07001439 if (insert) {
Eric Laurente8c8b432018-10-17 10:08:02 -07001440 if (state == ActivityManager::PROCESS_STATE_UNKNOWN) {
1441 it->second.first = active;
1442 }
1443 if (it->second.first) {
1444 it->second.second = state;
1445 } else {
1446 it->second.second = ActivityManager::PROCESS_STATE_UNKNOWN;
1447 }
Mikhail Naganoveae73eb2018-04-03 16:57:36 -07001448 } else {
1449 uids->erase(it);
1450 }
Eric Laurente8c8b432018-10-17 10:08:02 -07001451 } else if (insert && (state == ActivityManager::PROCESS_STATE_UNKNOWN)) {
1452 uids->insert(std::pair<uid_t, std::pair<bool, int>>(uid,
1453 std::pair<bool, int>(active, state)));
Mikhail Naganoveae73eb2018-04-03 16:57:36 -07001454 }
Svet Ganovf4ddfef2018-01-16 07:37:58 -08001455}
Mathias Agopian65ab4712010-07-14 17:59:35 -07001456
Eric Laurent4eb58f12018-12-07 16:41:02 -08001457bool AudioPolicyService::UidPolicy::isA11yOnTop() {
1458 for (const auto &uid : mCachedUids) {
Eric Laurent47670c92019-08-28 16:59:05 -07001459 if (!isA11yUid(uid.first)) {
Eric Laurent4eb58f12018-12-07 16:41:02 -08001460 continue;
1461 }
Amith Yamasanibcbb3002019-01-23 13:53:33 -08001462 if (uid.second.second >= ActivityManager::PROCESS_STATE_TOP
1463 && uid.second.second <= ActivityManager::PROCESS_STATE_BOUND_FOREGROUND_SERVICE) {
Eric Laurent4eb58f12018-12-07 16:41:02 -08001464 return true;
1465 }
1466 }
1467 return false;
1468}
1469
Eric Laurentb78763e2018-10-17 10:08:02 -07001470bool AudioPolicyService::UidPolicy::isA11yUid(uid_t uid)
1471{
1472 std::vector<uid_t>::iterator it = find(mA11yUids.begin(), mA11yUids.end(), uid);
1473 return it != mA11yUids.end();
1474}
1475
Michael Groovercfd28302018-12-11 19:16:46 -08001476// ----------- AudioPolicyService::SensorPrivacyService implementation ----------
1477void AudioPolicyService::SensorPrivacyPolicy::registerSelf() {
1478 SensorPrivacyManager spm;
1479 mSensorPrivacyEnabled = spm.isSensorPrivacyEnabled();
1480 spm.addSensorPrivacyListener(this);
1481}
1482
Evan Severson241d9592021-01-08 12:16:02 -08001483void AudioPolicyService::SensorPrivacyPolicy::registerSelfForMicrophoneOnly(int userId) {
1484 SensorPrivacyManager spm;
1485 mSensorPrivacyEnabled = spm.isIndividualSensorPrivacyEnabled(userId,
1486 SensorPrivacyManager::INDIVIDUAL_SENSOR_MICROPHONE);
1487 spm.addIndividualSensorPrivacyListener(userId,
1488 SensorPrivacyManager::INDIVIDUAL_SENSOR_MICROPHONE, this);
1489}
1490
Michael Groovercfd28302018-12-11 19:16:46 -08001491void AudioPolicyService::SensorPrivacyPolicy::unregisterSelf() {
1492 SensorPrivacyManager spm;
1493 spm.removeSensorPrivacyListener(this);
1494}
1495
1496bool AudioPolicyService::SensorPrivacyPolicy::isSensorPrivacyEnabled() {
1497 return mSensorPrivacyEnabled;
1498}
1499
1500binder::Status AudioPolicyService::SensorPrivacyPolicy::onSensorPrivacyChanged(bool enabled) {
1501 mSensorPrivacyEnabled = enabled;
1502 sp<AudioPolicyService> service = mService.promote();
1503 if (service != nullptr) {
1504 service->updateUidStates();
1505 }
1506 return binder::Status::ok();
1507}
1508
Eric Laurented726cc2021-07-01 14:26:41 +02001509// ----------- AudioPolicyService::OpRecordAudioMonitor implementation ----------
1510
1511// static
1512sp<AudioPolicyService::OpRecordAudioMonitor>
1513AudioPolicyService::OpRecordAudioMonitor::createIfNeeded(
1514 const AttributionSourceState& attributionSource, const audio_attributes_t& attr,
1515 wp<AudioCommandThread> commandThread)
1516{
Eric Laurent987ce102021-07-05 12:11:51 +02001517 if (isAudioServerOrRootUid(attributionSource.uid)) {
1518 ALOGV("not silencing record for audio or root source %s",
Eric Laurented726cc2021-07-01 14:26:41 +02001519 attributionSource.toString().c_str());
1520 return nullptr;
1521 }
1522
1523 if (!AudioPolicyService::isAppOpSource(attr.source)) {
1524 ALOGD("not monitoring app op for uid %d and source %d",
1525 attributionSource.uid, attr.source);
1526 return nullptr;
1527 }
1528
1529 if (!attributionSource.packageName.has_value()
1530 || attributionSource.packageName.value().size() == 0) {
1531 return nullptr;
1532 }
1533 return new OpRecordAudioMonitor(attributionSource, getOpForSource(attr.source), commandThread);
1534}
1535
1536AudioPolicyService::OpRecordAudioMonitor::OpRecordAudioMonitor(
1537 const AttributionSourceState& attributionSource, int32_t appOp,
1538 wp<AudioCommandThread> commandThread) :
1539 mHasOp(true), mAttributionSource(attributionSource), mAppOp(appOp),
1540 mCommandThread(commandThread)
1541{
1542}
1543
1544AudioPolicyService::OpRecordAudioMonitor::~OpRecordAudioMonitor()
1545{
1546 if (mOpCallback != 0) {
1547 mAppOpsManager.stopWatchingMode(mOpCallback);
1548 }
1549 mOpCallback.clear();
1550}
1551
1552void AudioPolicyService::OpRecordAudioMonitor::onFirstRef()
1553{
1554 checkOp();
1555 mOpCallback = new RecordAudioOpCallback(this);
1556 ALOGV("start watching op %d for %s", mAppOp, mAttributionSource.toString().c_str());
1557 // TODO: We need to always watch AppOpsManager::OP_RECORD_AUDIO too
1558 // since it controls the mic permission for legacy apps.
1559 mAppOpsManager.startWatchingMode(mAppOp, VALUE_OR_FATAL(aidl2legacy_string_view_String16(
1560 mAttributionSource.packageName.value_or(""))),
1561 mOpCallback);
1562}
1563
1564bool AudioPolicyService::OpRecordAudioMonitor::hasOp() const {
1565 return mHasOp.load();
1566}
1567
1568// Called by RecordAudioOpCallback when the app op corresponding to this OpRecordAudioMonitor
1569// is updated in AppOp callback and in onFirstRef()
1570// Note this method is never called (and never to be) for audio server / root track
1571// due to the UID in createIfNeeded(). As a result for those record track, it's:
1572// - not called from constructor,
1573// - not called from RecordAudioOpCallback because the callback is not installed in this case
1574void AudioPolicyService::OpRecordAudioMonitor::checkOp(bool updateUidStates)
1575{
1576 // TODO: We need to always check AppOpsManager::OP_RECORD_AUDIO too
1577 // since it controls the mic permission for legacy apps.
1578 const int32_t mode = mAppOpsManager.checkOp(mAppOp,
1579 mAttributionSource.uid, VALUE_OR_FATAL(aidl2legacy_string_view_String16(
1580 mAttributionSource.packageName.value_or(""))));
1581 const bool hasIt = (mode == AppOpsManager::MODE_ALLOWED);
1582 // verbose logging only log when appOp changed
1583 ALOGI_IF(hasIt != mHasOp.load(),
1584 "App op %d missing, %ssilencing record %s",
1585 mAppOp, hasIt ? "un" : "", mAttributionSource.toString().c_str());
1586 mHasOp.store(hasIt);
1587
1588 if (updateUidStates) {
1589 sp<AudioCommandThread> commandThread = mCommandThread.promote();
1590 if (commandThread != nullptr) {
1591 commandThread->updateUidStatesCommand();
1592 }
1593 }
1594}
1595
1596AudioPolicyService::OpRecordAudioMonitor::RecordAudioOpCallback::RecordAudioOpCallback(
1597 const wp<OpRecordAudioMonitor>& monitor) : mMonitor(monitor)
1598{ }
1599
1600void AudioPolicyService::OpRecordAudioMonitor::RecordAudioOpCallback::opChanged(int32_t op,
1601 const String16& packageName __unused) {
1602 sp<OpRecordAudioMonitor> monitor = mMonitor.promote();
1603 if (monitor != NULL) {
1604 if (op != monitor->getOp()) {
1605 return;
1606 }
1607 monitor->checkOp(true);
1608 }
1609}
1610
1611
Mathias Agopian65ab4712010-07-14 17:59:35 -07001612// ----------- AudioPolicyService::AudioCommandThread implementation ----------
1613
Eric Laurentbfb1b832013-01-07 09:53:42 -08001614AudioPolicyService::AudioCommandThread::AudioCommandThread(String8 name,
1615 const wp<AudioPolicyService>& service)
1616 : Thread(false), mName(name), mService(service)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001617{
Mathias Agopian65ab4712010-07-14 17:59:35 -07001618}
1619
1620
1621AudioPolicyService::AudioCommandThread::~AudioCommandThread()
1622{
Eric Laurentbfb1b832013-01-07 09:53:42 -08001623 if (!mAudioCommands.isEmpty()) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07001624 release_wake_lock(mName.string());
1625 }
1626 mAudioCommands.clear();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001627}
1628
1629void AudioPolicyService::AudioCommandThread::onFirstRef()
1630{
Eric Laurentbfb1b832013-01-07 09:53:42 -08001631 run(mName.string(), ANDROID_PRIORITY_AUDIO);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001632}
1633
1634bool AudioPolicyService::AudioCommandThread::threadLoop()
1635{
Eric Laurentd7eda8d2016-02-02 17:18:39 -08001636 nsecs_t waitTime = -1;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001637
1638 mLock.lock();
1639 while (!exitPending())
1640 {
Eric Laurent59a89232014-06-08 14:14:17 -07001641 sp<AudioPolicyService> svc;
1642 while (!mAudioCommands.isEmpty() && !exitPending()) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07001643 nsecs_t curTime = systemTime();
1644 // commands are sorted by increasing time stamp: execute them from index 0 and up
1645 if (mAudioCommands[0]->mTime <= curTime) {
Eric Laurent0ede8922014-05-09 18:04:42 -07001646 sp<AudioCommand> command = mAudioCommands[0];
Mathias Agopian65ab4712010-07-14 17:59:35 -07001647 mAudioCommands.removeAt(0);
Eric Laurent0ede8922014-05-09 18:04:42 -07001648 mLastCommand = command;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001649
1650 switch (command->mCommand) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07001651 case SET_VOLUME: {
Eric Laurent0ede8922014-05-09 18:04:42 -07001652 VolumeData *data = (VolumeData *)command->mParam.get();
Steve Block3856b092011-10-20 11:56:00 +01001653 ALOGV("AudioCommandThread() processing set volume stream %d, \
Eric Laurentde070132010-07-13 04:45:46 -07001654 volume %f, output %d", data->mStream, data->mVolume, data->mIO);
Andy Hungfe726a62018-09-27 15:17:25 -07001655 mLock.unlock();
Eric Laurentde070132010-07-13 04:45:46 -07001656 command->mStatus = AudioSystem::setStreamVolume(data->mStream,
1657 data->mVolume,
1658 data->mIO);
Andy Hungfe726a62018-09-27 15:17:25 -07001659 mLock.lock();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001660 }break;
1661 case SET_PARAMETERS: {
Eric Laurent0ede8922014-05-09 18:04:42 -07001662 ParametersData *data = (ParametersData *)command->mParam.get();
Glenn Kastene53b9ea2012-03-12 16:29:55 -07001663 ALOGV("AudioCommandThread() processing set parameters string %s, io %d",
1664 data->mKeyValuePairs.string(), data->mIO);
Andy Hungfe726a62018-09-27 15:17:25 -07001665 mLock.unlock();
Glenn Kastene53b9ea2012-03-12 16:29:55 -07001666 command->mStatus = AudioSystem::setParameters(data->mIO, data->mKeyValuePairs);
Andy Hungfe726a62018-09-27 15:17:25 -07001667 mLock.lock();
Glenn Kastene53b9ea2012-03-12 16:29:55 -07001668 }break;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001669 case SET_VOICE_VOLUME: {
Eric Laurent0ede8922014-05-09 18:04:42 -07001670 VoiceVolumeData *data = (VoiceVolumeData *)command->mParam.get();
Steve Block3856b092011-10-20 11:56:00 +01001671 ALOGV("AudioCommandThread() processing set voice volume volume %f",
Eric Laurentde070132010-07-13 04:45:46 -07001672 data->mVolume);
Andy Hungfe726a62018-09-27 15:17:25 -07001673 mLock.unlock();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001674 command->mStatus = AudioSystem::setVoiceVolume(data->mVolume);
Andy Hungfe726a62018-09-27 15:17:25 -07001675 mLock.lock();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001676 }break;
Eric Laurentbfb1b832013-01-07 09:53:42 -08001677 case STOP_OUTPUT: {
Eric Laurent0ede8922014-05-09 18:04:42 -07001678 StopOutputData *data = (StopOutputData *)command->mParam.get();
Eric Laurentd7fe0862018-07-14 16:48:01 -07001679 ALOGV("AudioCommandThread() processing stop output portId %d",
1680 data->mPortId);
Eric Laurent59a89232014-06-08 14:14:17 -07001681 svc = mService.promote();
Eric Laurentbfb1b832013-01-07 09:53:42 -08001682 if (svc == 0) {
1683 break;
1684 }
1685 mLock.unlock();
Eric Laurentd7fe0862018-07-14 16:48:01 -07001686 svc->doStopOutput(data->mPortId);
Eric Laurentbfb1b832013-01-07 09:53:42 -08001687 mLock.lock();
Eric Laurentbfb1b832013-01-07 09:53:42 -08001688 }break;
1689 case RELEASE_OUTPUT: {
Eric Laurent0ede8922014-05-09 18:04:42 -07001690 ReleaseOutputData *data = (ReleaseOutputData *)command->mParam.get();
Eric Laurentd7fe0862018-07-14 16:48:01 -07001691 ALOGV("AudioCommandThread() processing release output portId %d",
1692 data->mPortId);
Eric Laurent59a89232014-06-08 14:14:17 -07001693 svc = mService.promote();
Eric Laurentbfb1b832013-01-07 09:53:42 -08001694 if (svc == 0) {
1695 break;
1696 }
1697 mLock.unlock();
Eric Laurentd7fe0862018-07-14 16:48:01 -07001698 svc->doReleaseOutput(data->mPortId);
Eric Laurentbfb1b832013-01-07 09:53:42 -08001699 mLock.lock();
Eric Laurentbfb1b832013-01-07 09:53:42 -08001700 }break;
Eric Laurent951f4552014-05-20 10:48:17 -07001701 case CREATE_AUDIO_PATCH: {
1702 CreateAudioPatchData *data = (CreateAudioPatchData *)command->mParam.get();
1703 ALOGV("AudioCommandThread() processing create audio patch");
1704 sp<IAudioFlinger> af = AudioSystem::get_audio_flinger();
1705 if (af == 0) {
1706 command->mStatus = PERMISSION_DENIED;
1707 } else {
Andy Hungfe726a62018-09-27 15:17:25 -07001708 mLock.unlock();
Eric Laurent951f4552014-05-20 10:48:17 -07001709 command->mStatus = af->createAudioPatch(&data->mPatch, &data->mHandle);
Andy Hungfe726a62018-09-27 15:17:25 -07001710 mLock.lock();
Eric Laurent951f4552014-05-20 10:48:17 -07001711 }
1712 } break;
1713 case RELEASE_AUDIO_PATCH: {
1714 ReleaseAudioPatchData *data = (ReleaseAudioPatchData *)command->mParam.get();
1715 ALOGV("AudioCommandThread() processing release audio patch");
1716 sp<IAudioFlinger> af = AudioSystem::get_audio_flinger();
1717 if (af == 0) {
1718 command->mStatus = PERMISSION_DENIED;
1719 } else {
Andy Hungfe726a62018-09-27 15:17:25 -07001720 mLock.unlock();
Eric Laurent951f4552014-05-20 10:48:17 -07001721 command->mStatus = af->releaseAudioPatch(data->mHandle);
Andy Hungfe726a62018-09-27 15:17:25 -07001722 mLock.lock();
Eric Laurent951f4552014-05-20 10:48:17 -07001723 }
1724 } break;
Eric Laurentb52c1522014-05-20 11:27:36 -07001725 case UPDATE_AUDIOPORT_LIST: {
1726 ALOGV("AudioCommandThread() processing update audio port list");
Eric Laurent59a89232014-06-08 14:14:17 -07001727 svc = mService.promote();
Eric Laurentb52c1522014-05-20 11:27:36 -07001728 if (svc == 0) {
1729 break;
1730 }
1731 mLock.unlock();
1732 svc->doOnAudioPortListUpdate();
1733 mLock.lock();
1734 }break;
1735 case UPDATE_AUDIOPATCH_LIST: {
1736 ALOGV("AudioCommandThread() processing update audio patch list");
Eric Laurent59a89232014-06-08 14:14:17 -07001737 svc = mService.promote();
Eric Laurentb52c1522014-05-20 11:27:36 -07001738 if (svc == 0) {
1739 break;
1740 }
1741 mLock.unlock();
1742 svc->doOnAudioPatchListUpdate();
1743 mLock.lock();
1744 }break;
François Gaffiecfe17322018-11-07 13:41:29 +01001745 case CHANGED_AUDIOVOLUMEGROUP: {
1746 AudioVolumeGroupData *data =
1747 static_cast<AudioVolumeGroupData *>(command->mParam.get());
1748 ALOGV("AudioCommandThread() processing update audio volume group");
1749 svc = mService.promote();
1750 if (svc == 0) {
1751 break;
1752 }
1753 mLock.unlock();
1754 svc->doOnAudioVolumeGroupChanged(data->mGroup, data->mFlags);
1755 mLock.lock();
1756 }break;
Eric Laurente1715a42014-05-20 11:30:42 -07001757 case SET_AUDIOPORT_CONFIG: {
1758 SetAudioPortConfigData *data = (SetAudioPortConfigData *)command->mParam.get();
1759 ALOGV("AudioCommandThread() processing set port config");
1760 sp<IAudioFlinger> af = AudioSystem::get_audio_flinger();
1761 if (af == 0) {
1762 command->mStatus = PERMISSION_DENIED;
1763 } else {
Andy Hungfe726a62018-09-27 15:17:25 -07001764 mLock.unlock();
Eric Laurente1715a42014-05-20 11:30:42 -07001765 command->mStatus = af->setAudioPortConfig(&data->mConfig);
Andy Hungfe726a62018-09-27 15:17:25 -07001766 mLock.lock();
Eric Laurente1715a42014-05-20 11:30:42 -07001767 }
1768 } break;
Jean-Michel Trivide801052015-04-14 19:10:14 -07001769 case DYN_POLICY_MIX_STATE_UPDATE: {
1770 DynPolicyMixStateUpdateData *data =
1771 (DynPolicyMixStateUpdateData *)command->mParam.get();
Jean-Michel Trivide801052015-04-14 19:10:14 -07001772 ALOGV("AudioCommandThread() processing dyn policy mix state update %s %d",
1773 data->mRegId.string(), data->mState);
1774 svc = mService.promote();
1775 if (svc == 0) {
1776 break;
1777 }
1778 mLock.unlock();
1779 svc->doOnDynamicPolicyMixStateUpdate(data->mRegId, data->mState);
1780 mLock.lock();
1781 } break;
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -08001782 case RECORDING_CONFIGURATION_UPDATE: {
1783 RecordingConfigurationUpdateData *data =
1784 (RecordingConfigurationUpdateData *)command->mParam.get();
1785 ALOGV("AudioCommandThread() processing recording configuration update");
1786 svc = mService.promote();
1787 if (svc == 0) {
1788 break;
1789 }
1790 mLock.unlock();
Jean-Michel Triviac4e4292016-12-22 11:39:31 -08001791 svc->doOnRecordingConfigurationUpdate(data->mEvent, &data->mClientInfo,
Eric Laurenta9f86652018-11-28 17:23:11 -08001792 &data->mClientConfig, data->mClientEffects,
1793 &data->mDeviceConfig, data->mEffects,
1794 data->mPatchHandle, data->mSource);
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -08001795 mLock.lock();
1796 } break;
Eric Laurentb20cf7d2019-04-05 19:37:34 -07001797 case SET_EFFECT_SUSPENDED: {
1798 SetEffectSuspendedData *data = (SetEffectSuspendedData *)command->mParam.get();
1799 ALOGV("AudioCommandThread() processing set effect suspended");
1800 sp<IAudioFlinger> af = AudioSystem::get_audio_flinger();
1801 if (af != 0) {
1802 mLock.unlock();
1803 af->setEffectSuspended(data->mEffectId, data->mSessionId, data->mSuspended);
1804 mLock.lock();
1805 }
1806 } break;
Mikhail Naganov88b30d22020-03-09 19:43:13 +00001807 case AUDIO_MODULES_UPDATE: {
1808 ALOGV("AudioCommandThread() processing audio modules update");
1809 svc = mService.promote();
1810 if (svc == 0) {
1811 break;
1812 }
1813 mLock.unlock();
1814 svc->doOnNewAudioModulesAvailable();
1815 mLock.lock();
1816 } break;
Jean-Michel Trivi9a6b9ad2020-10-22 16:46:43 -07001817 case ROUTING_UPDATED: {
1818 ALOGV("AudioCommandThread() processing routing update");
1819 svc = mService.promote();
1820 if (svc == 0) {
1821 break;
1822 }
1823 mLock.unlock();
1824 svc->doOnRoutingUpdated();
1825 mLock.lock();
1826 } break;
Eric Laurentb20cf7d2019-04-05 19:37:34 -07001827
Eric Laurented726cc2021-07-01 14:26:41 +02001828 case UPDATE_UID_STATES: {
1829 ALOGV("AudioCommandThread() processing updateUID states");
1830 svc = mService.promote();
1831 if (svc == 0) {
1832 break;
1833 }
1834 mLock.unlock();
1835 svc->updateUidStates();
1836 mLock.lock();
1837 } break;
1838
Eric Laurent81dd0f52021-07-05 11:54:40 +02001839 case CHECK_SPATIALIZER: {
1840 ALOGV("AudioCommandThread() processing updateUID states");
1841 svc = mService.promote();
1842 if (svc == 0) {
1843 break;
1844 }
1845 mLock.unlock();
1846 svc->doOnCheckSpatializer();
1847 mLock.lock();
1848 } break;
1849
Mathias Agopian65ab4712010-07-14 17:59:35 -07001850 default:
Steve Block5ff1dd52012-01-05 23:22:43 +00001851 ALOGW("AudioCommandThread() unknown command %d", command->mCommand);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001852 }
Eric Laurent0ede8922014-05-09 18:04:42 -07001853 {
1854 Mutex::Autolock _l(command->mLock);
1855 if (command->mWaitStatus) {
1856 command->mWaitStatus = false;
1857 command->mCond.signal();
1858 }
1859 }
Eric Laurentd7eda8d2016-02-02 17:18:39 -08001860 waitTime = -1;
Zach Janga754b4f2015-10-27 01:29:34 +00001861 // release mLock before releasing strong reference on the service as
1862 // AudioPolicyService destructor calls AudioCommandThread::exit() which
1863 // acquires mLock.
1864 mLock.unlock();
1865 svc.clear();
1866 mLock.lock();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001867 } else {
1868 waitTime = mAudioCommands[0]->mTime - curTime;
1869 break;
1870 }
1871 }
Zach Janga754b4f2015-10-27 01:29:34 +00001872
1873 // release delayed commands wake lock if the queue is empty
1874 if (mAudioCommands.isEmpty()) {
Ricardo Garcia05f2fdc2014-07-24 15:48:24 -07001875 release_wake_lock(mName.string());
Zach Janga754b4f2015-10-27 01:29:34 +00001876 }
1877
1878 // At this stage we have either an empty command queue or the first command in the queue
1879 // has a finite delay. So unless we are exiting it is safe to wait.
1880 if (!exitPending()) {
Eric Laurent59a89232014-06-08 14:14:17 -07001881 ALOGV("AudioCommandThread() going to sleep");
Eric Laurentd7eda8d2016-02-02 17:18:39 -08001882 if (waitTime == -1) {
1883 mWaitWorkCV.wait(mLock);
1884 } else {
1885 mWaitWorkCV.waitRelative(mLock, waitTime);
1886 }
Eric Laurent59a89232014-06-08 14:14:17 -07001887 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001888 }
Ricardo Garcia05f2fdc2014-07-24 15:48:24 -07001889 // release delayed commands wake lock before quitting
1890 if (!mAudioCommands.isEmpty()) {
1891 release_wake_lock(mName.string());
1892 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001893 mLock.unlock();
1894 return false;
1895}
1896
1897status_t AudioPolicyService::AudioCommandThread::dump(int fd)
1898{
1899 const size_t SIZE = 256;
1900 char buffer[SIZE];
1901 String8 result;
1902
1903 snprintf(buffer, SIZE, "AudioCommandThread %p Dump\n", this);
1904 result.append(buffer);
1905 write(fd, result.string(), result.size());
1906
Mikhail Naganov12b716c2020-04-30 22:37:43 +00001907 const bool locked = dumpTryLock(mLock);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001908 if (!locked) {
1909 String8 result2(kCmdDeadlockedString);
1910 write(fd, result2.string(), result2.size());
1911 }
1912
1913 snprintf(buffer, SIZE, "- Commands:\n");
1914 result = String8(buffer);
1915 result.append(" Command Time Wait pParam\n");
Glenn Kasten8d6a2442012-02-08 14:04:28 -08001916 for (size_t i = 0; i < mAudioCommands.size(); i++) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07001917 mAudioCommands[i]->dump(buffer, SIZE);
1918 result.append(buffer);
1919 }
1920 result.append(" Last Command\n");
Eric Laurent0ede8922014-05-09 18:04:42 -07001921 if (mLastCommand != 0) {
1922 mLastCommand->dump(buffer, SIZE);
1923 result.append(buffer);
1924 } else {
1925 result.append(" none\n");
1926 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001927
1928 write(fd, result.string(), result.size());
1929
Mikhail Naganov12b716c2020-04-30 22:37:43 +00001930 dumpReleaseLock(mLock, locked);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001931
1932 return NO_ERROR;
1933}
1934
Glenn Kastenfff6d712012-01-12 16:38:12 -08001935status_t AudioPolicyService::AudioCommandThread::volumeCommand(audio_stream_type_t stream,
Eric Laurentde070132010-07-13 04:45:46 -07001936 float volume,
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001937 audio_io_handle_t output,
Eric Laurentde070132010-07-13 04:45:46 -07001938 int delayMs)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001939{
Eric Laurent0ede8922014-05-09 18:04:42 -07001940 sp<AudioCommand> command = new AudioCommand();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001941 command->mCommand = SET_VOLUME;
Eric Laurent0ede8922014-05-09 18:04:42 -07001942 sp<VolumeData> data = new VolumeData();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001943 data->mStream = stream;
1944 data->mVolume = volume;
1945 data->mIO = output;
1946 command->mParam = data;
Eric Laurent0ede8922014-05-09 18:04:42 -07001947 command->mWaitStatus = true;
Steve Block3856b092011-10-20 11:56:00 +01001948 ALOGV("AudioCommandThread() adding set volume stream %d, volume %f, output %d",
Eric Laurentde070132010-07-13 04:45:46 -07001949 stream, volume, output);
Eric Laurent0ede8922014-05-09 18:04:42 -07001950 return sendCommand(command, delayMs);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001951}
1952
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001953status_t AudioPolicyService::AudioCommandThread::parametersCommand(audio_io_handle_t ioHandle,
Dima Zavinfce7a472011-04-19 22:30:36 -07001954 const char *keyValuePairs,
Eric Laurentde070132010-07-13 04:45:46 -07001955 int delayMs)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001956{
Eric Laurent0ede8922014-05-09 18:04:42 -07001957 sp<AudioCommand> command = new AudioCommand();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001958 command->mCommand = SET_PARAMETERS;
Eric Laurent0ede8922014-05-09 18:04:42 -07001959 sp<ParametersData> data = new ParametersData();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001960 data->mIO = ioHandle;
Dima Zavinfce7a472011-04-19 22:30:36 -07001961 data->mKeyValuePairs = String8(keyValuePairs);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001962 command->mParam = data;
Eric Laurent0ede8922014-05-09 18:04:42 -07001963 command->mWaitStatus = true;
Steve Block3856b092011-10-20 11:56:00 +01001964 ALOGV("AudioCommandThread() adding set parameter string %s, io %d ,delay %d",
Dima Zavinfce7a472011-04-19 22:30:36 -07001965 keyValuePairs, ioHandle, delayMs);
Eric Laurent0ede8922014-05-09 18:04:42 -07001966 return sendCommand(command, delayMs);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001967}
1968
1969status_t AudioPolicyService::AudioCommandThread::voiceVolumeCommand(float volume, int delayMs)
1970{
Eric Laurent0ede8922014-05-09 18:04:42 -07001971 sp<AudioCommand> command = new AudioCommand();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001972 command->mCommand = SET_VOICE_VOLUME;
Eric Laurent0ede8922014-05-09 18:04:42 -07001973 sp<VoiceVolumeData> data = new VoiceVolumeData();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001974 data->mVolume = volume;
1975 command->mParam = data;
Eric Laurent0ede8922014-05-09 18:04:42 -07001976 command->mWaitStatus = true;
Steve Block3856b092011-10-20 11:56:00 +01001977 ALOGV("AudioCommandThread() adding set voice volume volume %f", volume);
Eric Laurent0ede8922014-05-09 18:04:42 -07001978 return sendCommand(command, delayMs);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001979}
1980
Eric Laurentb20cf7d2019-04-05 19:37:34 -07001981void AudioPolicyService::AudioCommandThread::setEffectSuspendedCommand(int effectId,
1982 audio_session_t sessionId,
1983 bool suspended)
1984{
1985 sp<AudioCommand> command = new AudioCommand();
1986 command->mCommand = SET_EFFECT_SUSPENDED;
1987 sp<SetEffectSuspendedData> data = new SetEffectSuspendedData();
1988 data->mEffectId = effectId;
1989 data->mSessionId = sessionId;
1990 data->mSuspended = suspended;
1991 command->mParam = data;
1992 ALOGV("AudioCommandThread() adding set suspended effectId %d sessionId %d suspended %d",
1993 effectId, sessionId, suspended);
1994 sendCommand(command);
1995}
1996
1997
Eric Laurentd7fe0862018-07-14 16:48:01 -07001998void AudioPolicyService::AudioCommandThread::stopOutputCommand(audio_port_handle_t portId)
Eric Laurentbfb1b832013-01-07 09:53:42 -08001999{
Eric Laurent0ede8922014-05-09 18:04:42 -07002000 sp<AudioCommand> command = new AudioCommand();
Eric Laurentbfb1b832013-01-07 09:53:42 -08002001 command->mCommand = STOP_OUTPUT;
Eric Laurent0ede8922014-05-09 18:04:42 -07002002 sp<StopOutputData> data = new StopOutputData();
Eric Laurentd7fe0862018-07-14 16:48:01 -07002003 data->mPortId = portId;
Jesper Tragardh48412dc2014-03-24 14:12:43 +01002004 command->mParam = data;
Eric Laurentd7fe0862018-07-14 16:48:01 -07002005 ALOGV("AudioCommandThread() adding stop output portId %d", portId);
Eric Laurent0ede8922014-05-09 18:04:42 -07002006 sendCommand(command);
Eric Laurentbfb1b832013-01-07 09:53:42 -08002007}
2008
Eric Laurentd7fe0862018-07-14 16:48:01 -07002009void AudioPolicyService::AudioCommandThread::releaseOutputCommand(audio_port_handle_t portId)
Eric Laurentbfb1b832013-01-07 09:53:42 -08002010{
Eric Laurent0ede8922014-05-09 18:04:42 -07002011 sp<AudioCommand> command = new AudioCommand();
Eric Laurentbfb1b832013-01-07 09:53:42 -08002012 command->mCommand = RELEASE_OUTPUT;
Eric Laurent0ede8922014-05-09 18:04:42 -07002013 sp<ReleaseOutputData> data = new ReleaseOutputData();
Eric Laurentd7fe0862018-07-14 16:48:01 -07002014 data->mPortId = portId;
Jesper Tragardh48412dc2014-03-24 14:12:43 +01002015 command->mParam = data;
Eric Laurentd7fe0862018-07-14 16:48:01 -07002016 ALOGV("AudioCommandThread() adding release output portId %d", portId);
Eric Laurent0ede8922014-05-09 18:04:42 -07002017 sendCommand(command);
2018}
2019
Eric Laurent951f4552014-05-20 10:48:17 -07002020status_t AudioPolicyService::AudioCommandThread::createAudioPatchCommand(
2021 const struct audio_patch *patch,
2022 audio_patch_handle_t *handle,
2023 int delayMs)
2024{
2025 status_t status = NO_ERROR;
2026
2027 sp<AudioCommand> command = new AudioCommand();
2028 command->mCommand = CREATE_AUDIO_PATCH;
2029 CreateAudioPatchData *data = new CreateAudioPatchData();
2030 data->mPatch = *patch;
2031 data->mHandle = *handle;
2032 command->mParam = data;
2033 command->mWaitStatus = true;
2034 ALOGV("AudioCommandThread() adding create patch delay %d", delayMs);
2035 status = sendCommand(command, delayMs);
2036 if (status == NO_ERROR) {
2037 *handle = data->mHandle;
2038 }
2039 return status;
2040}
2041
2042status_t AudioPolicyService::AudioCommandThread::releaseAudioPatchCommand(audio_patch_handle_t handle,
2043 int delayMs)
2044{
2045 sp<AudioCommand> command = new AudioCommand();
2046 command->mCommand = RELEASE_AUDIO_PATCH;
2047 ReleaseAudioPatchData *data = new ReleaseAudioPatchData();
2048 data->mHandle = handle;
2049 command->mParam = data;
2050 command->mWaitStatus = true;
2051 ALOGV("AudioCommandThread() adding release patch delay %d", delayMs);
2052 return sendCommand(command, delayMs);
2053}
2054
Eric Laurentb52c1522014-05-20 11:27:36 -07002055void AudioPolicyService::AudioCommandThread::updateAudioPortListCommand()
2056{
2057 sp<AudioCommand> command = new AudioCommand();
2058 command->mCommand = UPDATE_AUDIOPORT_LIST;
2059 ALOGV("AudioCommandThread() adding update audio port list");
2060 sendCommand(command);
2061}
2062
Eric Laurented726cc2021-07-01 14:26:41 +02002063void AudioPolicyService::AudioCommandThread::updateUidStatesCommand()
2064{
2065 sp<AudioCommand> command = new AudioCommand();
2066 command->mCommand = UPDATE_UID_STATES;
2067 ALOGV("AudioCommandThread() adding update UID states");
2068 sendCommand(command);
2069}
2070
Eric Laurentb52c1522014-05-20 11:27:36 -07002071void AudioPolicyService::AudioCommandThread::updateAudioPatchListCommand()
2072{
2073 sp<AudioCommand>command = new AudioCommand();
2074 command->mCommand = UPDATE_AUDIOPATCH_LIST;
2075 ALOGV("AudioCommandThread() adding update audio patch list");
2076 sendCommand(command);
2077}
2078
François Gaffiecfe17322018-11-07 13:41:29 +01002079void AudioPolicyService::AudioCommandThread::changeAudioVolumeGroupCommand(volume_group_t group,
2080 int flags)
2081{
2082 sp<AudioCommand>command = new AudioCommand();
2083 command->mCommand = CHANGED_AUDIOVOLUMEGROUP;
2084 AudioVolumeGroupData *data= new AudioVolumeGroupData();
2085 data->mGroup = group;
2086 data->mFlags = flags;
2087 command->mParam = data;
2088 ALOGV("AudioCommandThread() adding audio volume group changed");
2089 sendCommand(command);
2090}
2091
Eric Laurente1715a42014-05-20 11:30:42 -07002092status_t AudioPolicyService::AudioCommandThread::setAudioPortConfigCommand(
2093 const struct audio_port_config *config, int delayMs)
2094{
2095 sp<AudioCommand> command = new AudioCommand();
2096 command->mCommand = SET_AUDIOPORT_CONFIG;
2097 SetAudioPortConfigData *data = new SetAudioPortConfigData();
2098 data->mConfig = *config;
2099 command->mParam = data;
2100 command->mWaitStatus = true;
2101 ALOGV("AudioCommandThread() adding set port config delay %d", delayMs);
2102 return sendCommand(command, delayMs);
2103}
2104
Jean-Michel Trivide801052015-04-14 19:10:14 -07002105void AudioPolicyService::AudioCommandThread::dynamicPolicyMixStateUpdateCommand(
Chih-Hung Hsiehe964d4e2016-08-09 14:31:32 -07002106 const String8& regId, int32_t state)
Jean-Michel Trivide801052015-04-14 19:10:14 -07002107{
2108 sp<AudioCommand> command = new AudioCommand();
2109 command->mCommand = DYN_POLICY_MIX_STATE_UPDATE;
2110 DynPolicyMixStateUpdateData *data = new DynPolicyMixStateUpdateData();
2111 data->mRegId = regId;
2112 data->mState = state;
2113 command->mParam = data;
2114 ALOGV("AudioCommandThread() sending dynamic policy mix (id=%s) state update to %d",
2115 regId.string(), state);
2116 sendCommand(command);
2117}
2118
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -08002119void AudioPolicyService::AudioCommandThread::recordingConfigurationUpdateCommand(
Eric Laurenta9f86652018-11-28 17:23:11 -08002120 int event,
2121 const record_client_info_t *clientInfo,
2122 const audio_config_base_t *clientConfig,
2123 std::vector<effect_descriptor_t> clientEffects,
2124 const audio_config_base_t *deviceConfig,
2125 std::vector<effect_descriptor_t> effects,
2126 audio_patch_handle_t patchHandle,
2127 audio_source_t source)
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -08002128{
2129 sp<AudioCommand>command = new AudioCommand();
2130 command->mCommand = RECORDING_CONFIGURATION_UPDATE;
2131 RecordingConfigurationUpdateData *data = new RecordingConfigurationUpdateData();
2132 data->mEvent = event;
Jean-Michel Triviac4e4292016-12-22 11:39:31 -08002133 data->mClientInfo = *clientInfo;
Jean-Michel Trivi7281aa92016-02-17 15:33:40 -08002134 data->mClientConfig = *clientConfig;
Eric Laurenta9f86652018-11-28 17:23:11 -08002135 data->mClientEffects = clientEffects;
Jean-Michel Trivi7281aa92016-02-17 15:33:40 -08002136 data->mDeviceConfig = *deviceConfig;
Eric Laurenta9f86652018-11-28 17:23:11 -08002137 data->mEffects = effects;
Jean-Michel Trivi8c7cf3b2016-02-25 17:08:24 -08002138 data->mPatchHandle = patchHandle;
Eric Laurenta9f86652018-11-28 17:23:11 -08002139 data->mSource = source;
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -08002140 command->mParam = data;
Jean-Michel Triviac4e4292016-12-22 11:39:31 -08002141 ALOGV("AudioCommandThread() adding recording configuration update event %d, source %d uid %u",
2142 event, clientInfo->source, clientInfo->uid);
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -08002143 sendCommand(command);
2144}
2145
Mikhail Naganov88b30d22020-03-09 19:43:13 +00002146void AudioPolicyService::AudioCommandThread::audioModulesUpdateCommand()
2147{
2148 sp<AudioCommand> command = new AudioCommand();
2149 command->mCommand = AUDIO_MODULES_UPDATE;
2150 sendCommand(command);
2151}
2152
Jean-Michel Trivi9a6b9ad2020-10-22 16:46:43 -07002153void AudioPolicyService::AudioCommandThread::routingChangedCommand()
2154{
2155 sp<AudioCommand>command = new AudioCommand();
2156 command->mCommand = ROUTING_UPDATED;
2157 ALOGV("AudioCommandThread() adding routing update");
2158 sendCommand(command);
2159}
2160
Eric Laurent81dd0f52021-07-05 11:54:40 +02002161void AudioPolicyService::AudioCommandThread::checkSpatializerCommand()
2162{
2163 sp<AudioCommand>command = new AudioCommand();
2164 command->mCommand = CHECK_SPATIALIZER;
2165 ALOGV("AudioCommandThread() adding check spatializer");
2166 sendCommand(command);
2167}
2168
Eric Laurent0ede8922014-05-09 18:04:42 -07002169status_t AudioPolicyService::AudioCommandThread::sendCommand(sp<AudioCommand>& command, int delayMs)
2170{
2171 {
2172 Mutex::Autolock _l(mLock);
2173 insertCommand_l(command, delayMs);
2174 mWaitWorkCV.signal();
2175 }
2176 Mutex::Autolock _l(command->mLock);
2177 while (command->mWaitStatus) {
2178 nsecs_t timeOutNs = kAudioCommandTimeoutNs + milliseconds(delayMs);
2179 if (command->mCond.waitRelative(command->mLock, timeOutNs) != NO_ERROR) {
2180 command->mStatus = TIMED_OUT;
2181 command->mWaitStatus = false;
2182 }
2183 }
2184 return command->mStatus;
Eric Laurentbfb1b832013-01-07 09:53:42 -08002185}
2186
Mathias Agopian65ab4712010-07-14 17:59:35 -07002187// insertCommand_l() must be called with mLock held
Eric Laurent0ede8922014-05-09 18:04:42 -07002188void AudioPolicyService::AudioCommandThread::insertCommand_l(sp<AudioCommand>& command, int delayMs)
Mathias Agopian65ab4712010-07-14 17:59:35 -07002189{
Glenn Kasten8d6a2442012-02-08 14:04:28 -08002190 ssize_t i; // not size_t because i will count down to -1
Eric Laurent0ede8922014-05-09 18:04:42 -07002191 Vector < sp<AudioCommand> > removedCommands;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002192 command->mTime = systemTime() + milliseconds(delayMs);
2193
2194 // acquire wake lock to make sure delayed commands are processed
Eric Laurentbfb1b832013-01-07 09:53:42 -08002195 if (mAudioCommands.isEmpty()) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07002196 acquire_wake_lock(PARTIAL_WAKE_LOCK, mName.string());
2197 }
2198
2199 // check same pending commands with later time stamps and eliminate them
Ivan Lozano5ff158f2017-10-30 09:06:24 -07002200 for (i = (ssize_t)mAudioCommands.size()-1; i >= 0; i--) {
Eric Laurent0ede8922014-05-09 18:04:42 -07002201 sp<AudioCommand> command2 = mAudioCommands[i];
Mathias Agopian65ab4712010-07-14 17:59:35 -07002202 // commands are sorted by increasing time stamp: no need to scan the rest of mAudioCommands
2203 if (command2->mTime <= command->mTime) break;
Eric Laurente45b48a2014-09-04 16:40:57 -07002204
2205 // create audio patch or release audio patch commands are equivalent
2206 // with regard to filtering
2207 if ((command->mCommand == CREATE_AUDIO_PATCH) ||
2208 (command->mCommand == RELEASE_AUDIO_PATCH)) {
2209 if ((command2->mCommand != CREATE_AUDIO_PATCH) &&
2210 (command2->mCommand != RELEASE_AUDIO_PATCH)) {
2211 continue;
2212 }
2213 } else if (command2->mCommand != command->mCommand) continue;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002214
2215 switch (command->mCommand) {
2216 case SET_PARAMETERS: {
Eric Laurent0ede8922014-05-09 18:04:42 -07002217 ParametersData *data = (ParametersData *)command->mParam.get();
2218 ParametersData *data2 = (ParametersData *)command2->mParam.get();
Mathias Agopian65ab4712010-07-14 17:59:35 -07002219 if (data->mIO != data2->mIO) break;
Steve Block3856b092011-10-20 11:56:00 +01002220 ALOGV("Comparing parameter command %s to new command %s",
Eric Laurentde070132010-07-13 04:45:46 -07002221 data2->mKeyValuePairs.string(), data->mKeyValuePairs.string());
Mathias Agopian65ab4712010-07-14 17:59:35 -07002222 AudioParameter param = AudioParameter(data->mKeyValuePairs);
2223 AudioParameter param2 = AudioParameter(data2->mKeyValuePairs);
2224 for (size_t j = 0; j < param.size(); j++) {
Glenn Kastene53b9ea2012-03-12 16:29:55 -07002225 String8 key;
2226 String8 value;
2227 param.getAt(j, key, value);
2228 for (size_t k = 0; k < param2.size(); k++) {
2229 String8 key2;
2230 String8 value2;
2231 param2.getAt(k, key2, value2);
2232 if (key2 == key) {
2233 param2.remove(key2);
2234 ALOGV("Filtering out parameter %s", key2.string());
2235 break;
2236 }
2237 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07002238 }
2239 // if all keys have been filtered out, remove the command.
2240 // otherwise, update the key value pairs
2241 if (param2.size() == 0) {
2242 removedCommands.add(command2);
2243 } else {
2244 data2->mKeyValuePairs = param2.toString();
2245 }
Eric Laurent21e54562013-09-23 12:08:05 -07002246 command->mTime = command2->mTime;
2247 // force delayMs to non 0 so that code below does not request to wait for
2248 // command status as the command is now delayed
2249 delayMs = 1;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002250 } break;
2251
2252 case SET_VOLUME: {
Eric Laurent0ede8922014-05-09 18:04:42 -07002253 VolumeData *data = (VolumeData *)command->mParam.get();
2254 VolumeData *data2 = (VolumeData *)command2->mParam.get();
Mathias Agopian65ab4712010-07-14 17:59:35 -07002255 if (data->mIO != data2->mIO) break;
2256 if (data->mStream != data2->mStream) break;
Steve Block3856b092011-10-20 11:56:00 +01002257 ALOGV("Filtering out volume command on output %d for stream %d",
Eric Laurentde070132010-07-13 04:45:46 -07002258 data->mIO, data->mStream);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002259 removedCommands.add(command2);
Eric Laurent21e54562013-09-23 12:08:05 -07002260 command->mTime = command2->mTime;
2261 // force delayMs to non 0 so that code below does not request to wait for
2262 // command status as the command is now delayed
2263 delayMs = 1;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002264 } break;
Eric Laurente45b48a2014-09-04 16:40:57 -07002265
Eric Laurentbaf35fe2016-07-27 15:36:53 -07002266 case SET_VOICE_VOLUME: {
2267 VoiceVolumeData *data = (VoiceVolumeData *)command->mParam.get();
2268 VoiceVolumeData *data2 = (VoiceVolumeData *)command2->mParam.get();
2269 ALOGV("Filtering out voice volume command value %f replaced by %f",
2270 data2->mVolume, data->mVolume);
2271 removedCommands.add(command2);
2272 command->mTime = command2->mTime;
2273 // force delayMs to non 0 so that code below does not request to wait for
2274 // command status as the command is now delayed
2275 delayMs = 1;
2276 } break;
2277
Eric Laurente45b48a2014-09-04 16:40:57 -07002278 case CREATE_AUDIO_PATCH:
2279 case RELEASE_AUDIO_PATCH: {
2280 audio_patch_handle_t handle;
Haynes Mathew Georgea2d4a6d2014-10-13 13:05:22 -07002281 struct audio_patch patch;
Eric Laurente45b48a2014-09-04 16:40:57 -07002282 if (command->mCommand == CREATE_AUDIO_PATCH) {
2283 handle = ((CreateAudioPatchData *)command->mParam.get())->mHandle;
Haynes Mathew Georgea2d4a6d2014-10-13 13:05:22 -07002284 patch = ((CreateAudioPatchData *)command->mParam.get())->mPatch;
Eric Laurente45b48a2014-09-04 16:40:57 -07002285 } else {
2286 handle = ((ReleaseAudioPatchData *)command->mParam.get())->mHandle;
Mikhail Naganov7be71d22018-05-23 16:51:46 -07002287 memset(&patch, 0, sizeof(patch));
Eric Laurente45b48a2014-09-04 16:40:57 -07002288 }
2289 audio_patch_handle_t handle2;
Haynes Mathew Georgea2d4a6d2014-10-13 13:05:22 -07002290 struct audio_patch patch2;
Eric Laurente45b48a2014-09-04 16:40:57 -07002291 if (command2->mCommand == CREATE_AUDIO_PATCH) {
2292 handle2 = ((CreateAudioPatchData *)command2->mParam.get())->mHandle;
Haynes Mathew Georgea2d4a6d2014-10-13 13:05:22 -07002293 patch2 = ((CreateAudioPatchData *)command2->mParam.get())->mPatch;
Eric Laurente45b48a2014-09-04 16:40:57 -07002294 } else {
2295 handle2 = ((ReleaseAudioPatchData *)command2->mParam.get())->mHandle;
Glenn Kastenf60b6b62015-07-06 10:53:26 -07002296 memset(&patch2, 0, sizeof(patch2));
Eric Laurente45b48a2014-09-04 16:40:57 -07002297 }
2298 if (handle != handle2) break;
Haynes Mathew Georgea2d4a6d2014-10-13 13:05:22 -07002299 /* Filter CREATE_AUDIO_PATCH commands only when they are issued for
2300 same output. */
2301 if( (command->mCommand == CREATE_AUDIO_PATCH) &&
2302 (command2->mCommand == CREATE_AUDIO_PATCH) ) {
2303 bool isOutputDiff = false;
2304 if (patch.num_sources == patch2.num_sources) {
2305 for (unsigned count = 0; count < patch.num_sources; count++) {
2306 if (patch.sources[count].id != patch2.sources[count].id) {
2307 isOutputDiff = true;
2308 break;
2309 }
2310 }
2311 if (isOutputDiff)
2312 break;
2313 }
2314 }
Eric Laurente45b48a2014-09-04 16:40:57 -07002315 ALOGV("Filtering out %s audio patch command for handle %d",
2316 (command->mCommand == CREATE_AUDIO_PATCH) ? "create" : "release", handle);
2317 removedCommands.add(command2);
2318 command->mTime = command2->mTime;
2319 // force delayMs to non 0 so that code below does not request to wait for
2320 // command status as the command is now delayed
2321 delayMs = 1;
2322 } break;
2323
Jean-Michel Trivide801052015-04-14 19:10:14 -07002324 case DYN_POLICY_MIX_STATE_UPDATE: {
2325
2326 } break;
2327
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -08002328 case RECORDING_CONFIGURATION_UPDATE: {
2329
2330 } break;
2331
Jean-Michel Trivi9a6b9ad2020-10-22 16:46:43 -07002332 case ROUTING_UPDATED: {
2333
2334 } break;
2335
Mathias Agopian65ab4712010-07-14 17:59:35 -07002336 default:
2337 break;
2338 }
2339 }
2340
2341 // remove filtered commands
2342 for (size_t j = 0; j < removedCommands.size(); j++) {
2343 // removed commands always have time stamps greater than current command
2344 for (size_t k = i + 1; k < mAudioCommands.size(); k++) {
Eric Laurent0ede8922014-05-09 18:04:42 -07002345 if (mAudioCommands[k].get() == removedCommands[j].get()) {
Steve Block3856b092011-10-20 11:56:00 +01002346 ALOGV("suppressing command: %d", mAudioCommands[k]->mCommand);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002347 mAudioCommands.removeAt(k);
2348 break;
2349 }
2350 }
2351 }
2352 removedCommands.clear();
2353
Eric Laurentaa79bef2015-01-15 14:33:51 -08002354 // Disable wait for status if delay is not 0.
2355 // Except for create audio patch command because the returned patch handle
2356 // is needed by audio policy manager
2357 if (delayMs != 0 && command->mCommand != CREATE_AUDIO_PATCH) {
Eric Laurentcec4abb2012-07-03 12:23:02 -07002358 command->mWaitStatus = false;
2359 }
Eric Laurentcec4abb2012-07-03 12:23:02 -07002360
Mathias Agopian65ab4712010-07-14 17:59:35 -07002361 // insert command at the right place according to its time stamp
Eric Laurent1e693b52014-07-09 15:03:28 -07002362 ALOGV("inserting command: %d at index %zd, num commands %zu",
2363 command->mCommand, i+1, mAudioCommands.size());
Mathias Agopian65ab4712010-07-14 17:59:35 -07002364 mAudioCommands.insertAt(command, i + 1);
2365}
2366
2367void AudioPolicyService::AudioCommandThread::exit()
2368{
Steve Block3856b092011-10-20 11:56:00 +01002369 ALOGV("AudioCommandThread::exit");
Mathias Agopian65ab4712010-07-14 17:59:35 -07002370 {
2371 AutoMutex _l(mLock);
2372 requestExit();
2373 mWaitWorkCV.signal();
2374 }
Zach Janga754b4f2015-10-27 01:29:34 +00002375 // Note that we can call it from the thread loop if all other references have been released
2376 // but it will safely return WOULD_BLOCK in this case
Mathias Agopian65ab4712010-07-14 17:59:35 -07002377 requestExitAndWait();
2378}
2379
2380void AudioPolicyService::AudioCommandThread::AudioCommand::dump(char* buffer, size_t size)
2381{
2382 snprintf(buffer, size, " %02d %06d.%03d %01u %p\n",
2383 mCommand,
2384 (int)ns2s(mTime),
2385 (int)ns2ms(mTime)%1000,
2386 mWaitStatus,
Eric Laurent0ede8922014-05-09 18:04:42 -07002387 mParam.get());
Mathias Agopian65ab4712010-07-14 17:59:35 -07002388}
2389
Dima Zavinfce7a472011-04-19 22:30:36 -07002390/******* helpers for the service_ops callbacks defined below *********/
2391void AudioPolicyService::setParameters(audio_io_handle_t ioHandle,
2392 const char *keyValuePairs,
2393 int delayMs)
2394{
Glenn Kasten72ef00d2012-01-17 11:09:42 -08002395 mAudioCommandThread->parametersCommand(ioHandle, keyValuePairs,
Dima Zavinfce7a472011-04-19 22:30:36 -07002396 delayMs);
2397}
2398
2399int AudioPolicyService::setStreamVolume(audio_stream_type_t stream,
2400 float volume,
2401 audio_io_handle_t output,
2402 int delayMs)
2403{
Glenn Kastenfff6d712012-01-12 16:38:12 -08002404 return (int)mAudioCommandThread->volumeCommand(stream, volume,
Glenn Kasten72ef00d2012-01-17 11:09:42 -08002405 output, delayMs);
Dima Zavinfce7a472011-04-19 22:30:36 -07002406}
2407
Dima Zavinfce7a472011-04-19 22:30:36 -07002408int AudioPolicyService::setVoiceVolume(float volume, int delayMs)
2409{
2410 return (int)mAudioCommandThread->voiceVolumeCommand(volume, delayMs);
2411}
2412
Eric Laurentb20cf7d2019-04-05 19:37:34 -07002413void AudioPolicyService::setEffectSuspended(int effectId,
2414 audio_session_t sessionId,
2415 bool suspended)
2416{
2417 mAudioCommandThread->setEffectSuspendedCommand(effectId, sessionId, suspended);
2418}
2419
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002420Status AudioPolicyService::onNewAudioModulesAvailable()
Mikhail Naganov88b30d22020-03-09 19:43:13 +00002421{
Mikhail Naganovd0e2c742020-03-25 15:59:59 -07002422 mOutputCommandThread->audioModulesUpdateCommand();
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002423 return Status::ok();
Mikhail Naganov88b30d22020-03-09 19:43:13 +00002424}
2425
Eric Laurentb20cf7d2019-04-05 19:37:34 -07002426
Dima Zavinfce7a472011-04-19 22:30:36 -07002427extern "C" {
Eric Laurent2d388ec2014-03-07 13:25:54 -08002428audio_module_handle_t aps_load_hw_module(void *service __unused,
2429 const char *name);
2430audio_io_handle_t aps_open_output(void *service __unused,
Eric Laurenta4c5a552012-03-29 10:12:40 -07002431 audio_devices_t *pDevices,
2432 uint32_t *pSamplingRate,
2433 audio_format_t *pFormat,
2434 audio_channel_mask_t *pChannelMask,
2435 uint32_t *pLatencyMs,
Eric Laurent2d388ec2014-03-07 13:25:54 -08002436 audio_output_flags_t flags);
Eric Laurenta4c5a552012-03-29 10:12:40 -07002437
Eric Laurent2d388ec2014-03-07 13:25:54 -08002438audio_io_handle_t aps_open_output_on_module(void *service __unused,
Eric Laurenta4c5a552012-03-29 10:12:40 -07002439 audio_module_handle_t module,
2440 audio_devices_t *pDevices,
2441 uint32_t *pSamplingRate,
2442 audio_format_t *pFormat,
2443 audio_channel_mask_t *pChannelMask,
2444 uint32_t *pLatencyMs,
Richard Fitzgeraldad3af332013-03-25 16:54:37 +00002445 audio_output_flags_t flags,
Eric Laurent2d388ec2014-03-07 13:25:54 -08002446 const audio_offload_info_t *offloadInfo);
2447audio_io_handle_t aps_open_dup_output(void *service __unused,
Dima Zavinfce7a472011-04-19 22:30:36 -07002448 audio_io_handle_t output1,
Eric Laurent2d388ec2014-03-07 13:25:54 -08002449 audio_io_handle_t output2);
2450int aps_close_output(void *service __unused, audio_io_handle_t output);
2451int aps_suspend_output(void *service __unused, audio_io_handle_t output);
2452int aps_restore_output(void *service __unused, audio_io_handle_t output);
2453audio_io_handle_t aps_open_input(void *service __unused,
Eric Laurenta4c5a552012-03-29 10:12:40 -07002454 audio_devices_t *pDevices,
2455 uint32_t *pSamplingRate,
2456 audio_format_t *pFormat,
2457 audio_channel_mask_t *pChannelMask,
Eric Laurent2d388ec2014-03-07 13:25:54 -08002458 audio_in_acoustics_t acoustics __unused);
2459audio_io_handle_t aps_open_input_on_module(void *service __unused,
Eric Laurenta4c5a552012-03-29 10:12:40 -07002460 audio_module_handle_t module,
2461 audio_devices_t *pDevices,
2462 uint32_t *pSamplingRate,
2463 audio_format_t *pFormat,
Eric Laurent2d388ec2014-03-07 13:25:54 -08002464 audio_channel_mask_t *pChannelMask);
2465int aps_close_input(void *service __unused, audio_io_handle_t input);
2466int aps_invalidate_stream(void *service __unused, audio_stream_type_t stream);
Glenn Kastend848eb42016-03-08 13:42:11 -08002467int aps_move_effects(void *service __unused, audio_session_t session,
Dima Zavinfce7a472011-04-19 22:30:36 -07002468 audio_io_handle_t src_output,
Eric Laurent2d388ec2014-03-07 13:25:54 -08002469 audio_io_handle_t dst_output);
2470char * aps_get_parameters(void *service __unused, audio_io_handle_t io_handle,
2471 const char *keys);
2472void aps_set_parameters(void *service, audio_io_handle_t io_handle,
2473 const char *kv_pairs, int delay_ms);
2474int aps_set_stream_volume(void *service, audio_stream_type_t stream,
Dima Zavinfce7a472011-04-19 22:30:36 -07002475 float volume, audio_io_handle_t output,
Eric Laurent2d388ec2014-03-07 13:25:54 -08002476 int delay_ms);
Eric Laurent2d388ec2014-03-07 13:25:54 -08002477int aps_set_voice_volume(void *service, float volume, int delay_ms);
2478};
Dima Zavinfce7a472011-04-19 22:30:36 -07002479
Mikhail Naganov1b2a7942017-12-08 10:18:09 -08002480} // namespace android