blob: d0efac0fba2515eb2da8752f2de531ec9384394a [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
145 const audio_attributes_t attr = attributes_initializer(AUDIO_USAGE_MEDIA);
146 AudioDeviceTypeAddrVector devices;
147 bool hasSpatializer = mAudioPolicyManager->canBeSpatialized(&attr, nullptr, devices);
148 if (hasSpatializer) {
149 mSpatializer = Spatializer::create(this);
150 }
Eric Laurentd66d7a12021-07-13 13:35:32 +0200151 AudioSystem::audioPolicyReady();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700152}
153
Jaideep Sharmaba9053b2021-01-25 21:24:26 +0530154void AudioPolicyService::unloadAudioPolicyManager()
155{
156 ALOGV("%s ", __func__);
157 if (mLibraryHandle != nullptr) {
158 dlclose(mLibraryHandle);
159 }
160 mLibraryHandle = nullptr;
161 mCreateAudioPolicyManager = nullptr;
162 mDestroyAudioPolicyManager = nullptr;
163}
164
Mathias Agopian65ab4712010-07-14 17:59:35 -0700165AudioPolicyService::~AudioPolicyService()
166{
Mathias Agopian65ab4712010-07-14 17:59:35 -0700167 mAudioCommandThread->exit();
Eric Laurent657ff612014-05-07 11:58:24 -0700168 mOutputCommandThread->exit();
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700169
Jaideep Sharmaba9053b2021-01-25 21:24:26 +0530170 mDestroyAudioPolicyManager(mAudioPolicyManager);
171 unloadAudioPolicyManager();
172
Eric Laurentdce54a12014-03-10 12:19:46 -0700173 delete mAudioPolicyClient;
Eric Laurentb52c1522014-05-20 11:27:36 -0700174
175 mNotificationClients.clear();
bryant_liuba2b4392014-06-11 16:49:30 +0800176 mAudioPolicyEffects.clear();
Svet Ganovf4ddfef2018-01-16 07:37:58 -0800177
178 mUidPolicy->unregisterSelf();
Michael Groovercfd28302018-12-11 19:16:46 -0800179 mSensorPrivacyPolicy->unregisterSelf();
Mikhail Naganov12b716c2020-04-30 22:37:43 +0000180
181 mUidPolicy.clear();
Michael Groovercfd28302018-12-11 19:16:46 -0800182 mSensorPrivacyPolicy.clear();
Eric Laurentb52c1522014-05-20 11:27:36 -0700183}
184
185// A notification client is always registered by AudioSystem when the client process
186// connects to AudioPolicyService.
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800187Status AudioPolicyService::registerClient(const sp<media::IAudioPolicyServiceClient>& client)
Eric Laurentb52c1522014-05-20 11:27:36 -0700188{
Eric Laurent12590252015-08-21 18:40:20 -0700189 if (client == 0) {
190 ALOGW("%s got NULL client", __FUNCTION__);
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800191 return Status::ok();
Eric Laurent12590252015-08-21 18:40:20 -0700192 }
Eric Laurent0ebd5f92014-11-19 19:04:52 -0800193 Mutex::Autolock _l(mNotificationClientsLock);
Eric Laurentb52c1522014-05-20 11:27:36 -0700194
195 uid_t uid = IPCThreadState::self()->getCallingUid();
luochaojiang908c7d72018-06-21 14:58:04 +0800196 pid_t pid = IPCThreadState::self()->getCallingPid();
197 int64_t token = ((int64_t)uid<<32) | pid;
198
199 if (mNotificationClients.indexOfKey(token) < 0) {
Eric Laurentb52c1522014-05-20 11:27:36 -0700200 sp<NotificationClient> notificationClient = new NotificationClient(this,
201 client,
luochaojiang908c7d72018-06-21 14:58:04 +0800202 uid,
203 pid);
204 ALOGV("registerClient() client %p, uid %d pid %d", client.get(), uid, pid);
Eric Laurentb52c1522014-05-20 11:27:36 -0700205
luochaojiang908c7d72018-06-21 14:58:04 +0800206 mNotificationClients.add(token, notificationClient);
Eric Laurentb52c1522014-05-20 11:27:36 -0700207
Marco Nelissenf8880202014-11-14 07:58:25 -0800208 sp<IBinder> binder = IInterface::asBinder(client);
Eric Laurentb52c1522014-05-20 11:27:36 -0700209 binder->linkToDeath(notificationClient);
210 }
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800211 return Status::ok();
Eric Laurentb52c1522014-05-20 11:27:36 -0700212}
213
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800214Status AudioPolicyService::setAudioPortCallbacksEnabled(bool enabled)
Eric Laurente8726fe2015-06-26 09:39:24 -0700215{
216 Mutex::Autolock _l(mNotificationClientsLock);
217
218 uid_t uid = IPCThreadState::self()->getCallingUid();
luochaojiang908c7d72018-06-21 14:58:04 +0800219 pid_t pid = IPCThreadState::self()->getCallingPid();
220 int64_t token = ((int64_t)uid<<32) | pid;
221
222 if (mNotificationClients.indexOfKey(token) < 0) {
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800223 return Status::ok();
Eric Laurente8726fe2015-06-26 09:39:24 -0700224 }
luochaojiang908c7d72018-06-21 14:58:04 +0800225 mNotificationClients.valueFor(token)->setAudioPortCallbacksEnabled(enabled);
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800226 return Status::ok();
Eric Laurente8726fe2015-06-26 09:39:24 -0700227}
228
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800229Status AudioPolicyService::setAudioVolumeGroupCallbacksEnabled(bool enabled)
François Gaffiecfe17322018-11-07 13:41:29 +0100230{
231 Mutex::Autolock _l(mNotificationClientsLock);
232
233 uid_t uid = IPCThreadState::self()->getCallingUid();
234 pid_t pid = IPCThreadState::self()->getCallingPid();
235 int64_t token = ((int64_t)uid<<32) | pid;
236
237 if (mNotificationClients.indexOfKey(token) < 0) {
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800238 return Status::ok();
François Gaffiecfe17322018-11-07 13:41:29 +0100239 }
240 mNotificationClients.valueFor(token)->setAudioVolumeGroupCallbacksEnabled(enabled);
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800241 return Status::ok();
François Gaffiecfe17322018-11-07 13:41:29 +0100242}
243
Eric Laurentb52c1522014-05-20 11:27:36 -0700244// removeNotificationClient() is called when the client process dies.
luochaojiang908c7d72018-06-21 14:58:04 +0800245void AudioPolicyService::removeNotificationClient(uid_t uid, pid_t pid)
Eric Laurentb52c1522014-05-20 11:27:36 -0700246{
Mikhail Naganov12b716c2020-04-30 22:37:43 +0000247 bool hasSameUid = false;
Eric Laurent0ebd5f92014-11-19 19:04:52 -0800248 {
249 Mutex::Autolock _l(mNotificationClientsLock);
luochaojiang908c7d72018-06-21 14:58:04 +0800250 int64_t token = ((int64_t)uid<<32) | pid;
251 mNotificationClients.removeItem(token);
luochaojiang908c7d72018-06-21 14:58:04 +0800252 for (size_t i = 0; i < mNotificationClients.size(); i++) {
253 if (mNotificationClients.valueAt(i)->uid() == uid) {
254 hasSameUid = true;
255 break;
256 }
257 }
Mikhail Naganov12b716c2020-04-30 22:37:43 +0000258 }
259 {
260 Mutex::Autolock _l(mLock);
luochaojiang908c7d72018-06-21 14:58:04 +0800261 if (mAudioPolicyManager && !hasSameUid) {
Eric Laurent10b71232018-04-13 18:14:44 -0700262 // called from binder death notification: no need to clear caller identity
Eric Laurent8c7e6da2015-04-21 17:37:00 -0700263 mAudioPolicyManager->releaseResourcesForUid(uid);
Eric Laurentb52c1522014-05-20 11:27:36 -0700264 }
Eric Laurent0ebd5f92014-11-19 19:04:52 -0800265 }
Eric Laurentb52c1522014-05-20 11:27:36 -0700266}
267
268void AudioPolicyService::onAudioPortListUpdate()
269{
270 mOutputCommandThread->updateAudioPortListCommand();
271}
272
273void AudioPolicyService::doOnAudioPortListUpdate()
274{
Eric Laurent0ebd5f92014-11-19 19:04:52 -0800275 Mutex::Autolock _l(mNotificationClientsLock);
Eric Laurentb52c1522014-05-20 11:27:36 -0700276 for (size_t i = 0; i < mNotificationClients.size(); i++) {
277 mNotificationClients.valueAt(i)->onAudioPortListUpdate();
278 }
279}
280
281void AudioPolicyService::onAudioPatchListUpdate()
282{
283 mOutputCommandThread->updateAudioPatchListCommand();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700284}
285
Eric Laurentb52c1522014-05-20 11:27:36 -0700286void AudioPolicyService::doOnAudioPatchListUpdate()
287{
Eric Laurent0ebd5f92014-11-19 19:04:52 -0800288 Mutex::Autolock _l(mNotificationClientsLock);
Eric Laurentb52c1522014-05-20 11:27:36 -0700289 for (size_t i = 0; i < mNotificationClients.size(); i++) {
290 mNotificationClients.valueAt(i)->onAudioPatchListUpdate();
291 }
292}
293
François Gaffiecfe17322018-11-07 13:41:29 +0100294void AudioPolicyService::onAudioVolumeGroupChanged(volume_group_t group, int flags)
295{
296 mOutputCommandThread->changeAudioVolumeGroupCommand(group, flags);
297}
298
299void AudioPolicyService::doOnAudioVolumeGroupChanged(volume_group_t group, int flags)
300{
301 Mutex::Autolock _l(mNotificationClientsLock);
302 for (size_t i = 0; i < mNotificationClients.size(); i++) {
303 mNotificationClients.valueAt(i)->onAudioVolumeGroupChanged(group, flags);
304 }
305}
306
Chih-Hung Hsiehe964d4e2016-08-09 14:31:32 -0700307void AudioPolicyService::onDynamicPolicyMixStateUpdate(const String8& regId, int32_t state)
Jean-Michel Trivide801052015-04-14 19:10:14 -0700308{
309 ALOGV("AudioPolicyService::onDynamicPolicyMixStateUpdate(%s, %d)",
310 regId.string(), state);
311 mOutputCommandThread->dynamicPolicyMixStateUpdateCommand(regId, state);
312}
313
Chih-Hung Hsiehe964d4e2016-08-09 14:31:32 -0700314void AudioPolicyService::doOnDynamicPolicyMixStateUpdate(const String8& regId, int32_t state)
Jean-Michel Trivide801052015-04-14 19:10:14 -0700315{
316 Mutex::Autolock _l(mNotificationClientsLock);
317 for (size_t i = 0; i < mNotificationClients.size(); i++) {
318 mNotificationClients.valueAt(i)->onDynamicPolicyMixStateUpdate(regId, state);
319 }
320}
321
Eric Laurenta9f86652018-11-28 17:23:11 -0800322void AudioPolicyService::onRecordingConfigurationUpdate(
323 int event,
324 const record_client_info_t *clientInfo,
325 const audio_config_base_t *clientConfig,
326 std::vector<effect_descriptor_t> clientEffects,
327 const audio_config_base_t *deviceConfig,
328 std::vector<effect_descriptor_t> effects,
329 audio_patch_handle_t patchHandle,
330 audio_source_t source)
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -0800331{
Jean-Michel Triviac4e4292016-12-22 11:39:31 -0800332 mOutputCommandThread->recordingConfigurationUpdateCommand(event, clientInfo,
Eric Laurenta9f86652018-11-28 17:23:11 -0800333 clientConfig, clientEffects, deviceConfig, effects, patchHandle, source);
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -0800334}
335
Eric Laurenta9f86652018-11-28 17:23:11 -0800336void AudioPolicyService::doOnRecordingConfigurationUpdate(
337 int event,
338 const record_client_info_t *clientInfo,
339 const audio_config_base_t *clientConfig,
340 std::vector<effect_descriptor_t> clientEffects,
341 const audio_config_base_t *deviceConfig,
342 std::vector<effect_descriptor_t> effects,
343 audio_patch_handle_t patchHandle,
344 audio_source_t source)
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -0800345{
346 Mutex::Autolock _l(mNotificationClientsLock);
347 for (size_t i = 0; i < mNotificationClients.size(); i++) {
Jean-Michel Triviac4e4292016-12-22 11:39:31 -0800348 mNotificationClients.valueAt(i)->onRecordingConfigurationUpdate(event, clientInfo,
Eric Laurenta9f86652018-11-28 17:23:11 -0800349 clientConfig, clientEffects, deviceConfig, effects, patchHandle, source);
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -0800350 }
351}
352
Jean-Michel Trivi9a6b9ad2020-10-22 16:46:43 -0700353void AudioPolicyService::onRoutingUpdated()
354{
355 mOutputCommandThread->routingChangedCommand();
356}
357
358void AudioPolicyService::doOnRoutingUpdated()
359{
360 Mutex::Autolock _l(mNotificationClientsLock);
361 for (size_t i = 0; i < mNotificationClients.size(); i++) {
362 mNotificationClients.valueAt(i)->onRoutingUpdated();
363 }
364}
365
Eric Laurent81dd0f52021-07-05 11:54:40 +0200366void AudioPolicyService::onCheckSpatializer()
367{
368 Mutex::Autolock _l(mLock);
Eric Laurent39095982021-08-24 18:29:27 +0200369 onCheckSpatializer_l();
370}
371
372void AudioPolicyService::onCheckSpatializer_l()
373{
374 if (mSpatializer != nullptr) {
375 mOutputCommandThread->checkSpatializerCommand();
376 }
Eric Laurent81dd0f52021-07-05 11:54:40 +0200377}
378
379void AudioPolicyService::doOnCheckSpatializer()
380{
Eric Laurent39095982021-08-24 18:29:27 +0200381 Mutex::Autolock _l(mLock);
Eric Laurent81dd0f52021-07-05 11:54:40 +0200382
Eric Laurent39095982021-08-24 18:29:27 +0200383 if (mSpatializer != nullptr) {
384 if (mSpatializer->getLevel() != media::SpatializationLevel::NONE) {
385 audio_io_handle_t currentOutput = mSpatializer->getOutput();
386 audio_io_handle_t newOutput;
387 const audio_attributes_t attr = attributes_initializer(AUDIO_USAGE_MEDIA);
388 audio_config_base_t config = mSpatializer->getAudioInConfig();
389 status_t status =
390 mAudioPolicyManager->getSpatializerOutput(&config, &attr, &newOutput);
391
392 if (status == NO_ERROR && currentOutput == newOutput) {
393 return;
394 }
395 mLock.unlock();
396 // It is OK to call detachOutput() is none is already attached.
397 mSpatializer->detachOutput();
398 if (status != NO_ERROR || newOutput == AUDIO_IO_HANDLE_NONE) {
Eric Laurent81dd0f52021-07-05 11:54:40 +0200399 mLock.lock();
Eric Laurent39095982021-08-24 18:29:27 +0200400 return;
401 }
402 status = mSpatializer->attachOutput(newOutput);
403 mLock.lock();
404 if (status != NO_ERROR) {
405 mAudioPolicyManager->releaseSpatializerOutput(newOutput);
406 }
407 } else if (mSpatializer->getLevel() == media::SpatializationLevel::NONE
408 && mSpatializer->getOutput() != AUDIO_IO_HANDLE_NONE) {
409 mLock.unlock();
410 audio_io_handle_t output = mSpatializer->detachOutput();
411 mLock.lock();
412 if (output != AUDIO_IO_HANDLE_NONE) {
413 mAudioPolicyManager->releaseSpatializerOutput(output);
Eric Laurent81dd0f52021-07-05 11:54:40 +0200414 }
415 }
416 }
417}
418
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -0800419status_t AudioPolicyService::clientCreateAudioPatch(const struct audio_patch *patch,
420 audio_patch_handle_t *handle,
421 int delayMs)
422{
423 return mAudioCommandThread->createAudioPatchCommand(patch, handle, delayMs);
424}
425
426status_t AudioPolicyService::clientReleaseAudioPatch(audio_patch_handle_t handle,
427 int delayMs)
428{
429 return mAudioCommandThread->releaseAudioPatchCommand(handle, delayMs);
430}
431
Eric Laurente1715a42014-05-20 11:30:42 -0700432status_t AudioPolicyService::clientSetAudioPortConfig(const struct audio_port_config *config,
433 int delayMs)
434{
435 return mAudioCommandThread->setAudioPortConfigCommand(config, delayMs);
436}
437
Ytai Ben-Tsvi7e7a79d2020-12-15 16:48:16 -0800438AudioPolicyService::NotificationClient::NotificationClient(
439 const sp<AudioPolicyService>& service,
440 const sp<media::IAudioPolicyServiceClient>& client,
441 uid_t uid,
442 pid_t pid)
luochaojiang908c7d72018-06-21 14:58:04 +0800443 : mService(service), mUid(uid), mPid(pid), mAudioPolicyServiceClient(client),
François Gaffiecfe17322018-11-07 13:41:29 +0100444 mAudioPortCallbacksEnabled(false), mAudioVolumeGroupCallbacksEnabled(false)
Eric Laurentb52c1522014-05-20 11:27:36 -0700445{
446}
447
448AudioPolicyService::NotificationClient::~NotificationClient()
449{
450}
451
452void AudioPolicyService::NotificationClient::binderDied(const wp<IBinder>& who __unused)
453{
454 sp<NotificationClient> keep(this);
455 sp<AudioPolicyService> service = mService.promote();
456 if (service != 0) {
luochaojiang908c7d72018-06-21 14:58:04 +0800457 service->removeNotificationClient(mUid, mPid);
Eric Laurentb52c1522014-05-20 11:27:36 -0700458 }
459}
460
461void AudioPolicyService::NotificationClient::onAudioPortListUpdate()
462{
Eric Laurente8726fe2015-06-26 09:39:24 -0700463 if (mAudioPolicyServiceClient != 0 && mAudioPortCallbacksEnabled) {
Eric Laurentb52c1522014-05-20 11:27:36 -0700464 mAudioPolicyServiceClient->onAudioPortListUpdate();
465 }
466}
467
468void AudioPolicyService::NotificationClient::onAudioPatchListUpdate()
469{
Eric Laurente8726fe2015-06-26 09:39:24 -0700470 if (mAudioPolicyServiceClient != 0 && mAudioPortCallbacksEnabled) {
Eric Laurentb52c1522014-05-20 11:27:36 -0700471 mAudioPolicyServiceClient->onAudioPatchListUpdate();
472 }
473}
Eric Laurent57dae992011-07-24 13:36:09 -0700474
François Gaffiecfe17322018-11-07 13:41:29 +0100475void AudioPolicyService::NotificationClient::onAudioVolumeGroupChanged(volume_group_t group,
476 int flags)
477{
478 if (mAudioPolicyServiceClient != 0 && mAudioVolumeGroupCallbacksEnabled) {
479 mAudioPolicyServiceClient->onAudioVolumeGroupChanged(group, flags);
480 }
481}
482
483
Jean-Michel Trivide801052015-04-14 19:10:14 -0700484void AudioPolicyService::NotificationClient::onDynamicPolicyMixStateUpdate(
Chih-Hung Hsiehe964d4e2016-08-09 14:31:32 -0700485 const String8& regId, int32_t state)
Jean-Michel Trivide801052015-04-14 19:10:14 -0700486{
Andy Hung4ef19fa2018-05-15 19:35:29 -0700487 if (mAudioPolicyServiceClient != 0 && isServiceUid(mUid)) {
Ytai Ben-Tsvi7e7a79d2020-12-15 16:48:16 -0800488 mAudioPolicyServiceClient->onDynamicPolicyMixStateUpdate(
489 legacy2aidl_String8_string(regId).value(), state);
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -0800490 }
491}
492
493void AudioPolicyService::NotificationClient::onRecordingConfigurationUpdate(
Eric Laurenta9f86652018-11-28 17:23:11 -0800494 int event,
495 const record_client_info_t *clientInfo,
496 const audio_config_base_t *clientConfig,
497 std::vector<effect_descriptor_t> clientEffects,
498 const audio_config_base_t *deviceConfig,
499 std::vector<effect_descriptor_t> effects,
500 audio_patch_handle_t patchHandle,
501 audio_source_t source)
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -0800502{
Andy Hung4ef19fa2018-05-15 19:35:29 -0700503 if (mAudioPolicyServiceClient != 0 && isServiceUid(mUid)) {
Ytai Ben-Tsvi7e7a79d2020-12-15 16:48:16 -0800504 status_t status = [&]() -> status_t {
505 int32_t eventAidl = VALUE_OR_RETURN_STATUS(convertIntegral<int32_t>(event));
506 media::RecordClientInfo clientInfoAidl = VALUE_OR_RETURN_STATUS(
507 legacy2aidl_record_client_info_t_RecordClientInfo(*clientInfo));
Mikhail Naganovdbf03642021-08-25 18:15:32 -0700508 AudioConfigBase clientConfigAidl = VALUE_OR_RETURN_STATUS(
Mikhail Naganovde3fa182021-07-30 15:06:42 -0700509 legacy2aidl_audio_config_base_t_AudioConfigBase(
510 *clientConfig, true /*isInput*/));
Ytai Ben-Tsvi7e7a79d2020-12-15 16:48:16 -0800511 std::vector<media::EffectDescriptor> clientEffectsAidl = VALUE_OR_RETURN_STATUS(
512 convertContainer<std::vector<media::EffectDescriptor>>(
513 clientEffects,
514 legacy2aidl_effect_descriptor_t_EffectDescriptor));
Mikhail Naganovdbf03642021-08-25 18:15:32 -0700515 AudioConfigBase deviceConfigAidl = VALUE_OR_RETURN_STATUS(
Mikhail Naganovde3fa182021-07-30 15:06:42 -0700516 legacy2aidl_audio_config_base_t_AudioConfigBase(
517 *deviceConfig, true /*isInput*/));
Ytai Ben-Tsvi7e7a79d2020-12-15 16:48:16 -0800518 std::vector<media::EffectDescriptor> effectsAidl = VALUE_OR_RETURN_STATUS(
519 convertContainer<std::vector<media::EffectDescriptor>>(
520 effects,
521 legacy2aidl_effect_descriptor_t_EffectDescriptor));
522 int32_t patchHandleAidl = VALUE_OR_RETURN_STATUS(
523 legacy2aidl_audio_patch_handle_t_int32_t(patchHandle));
Mikhail Naganovddceecc2021-09-03 13:58:56 -0700524 media::audio::common::AudioSource sourceAidl = VALUE_OR_RETURN_STATUS(
525 legacy2aidl_audio_source_t_AudioSource(source));
Ytai Ben-Tsvi7e7a79d2020-12-15 16:48:16 -0800526 return aidl_utils::statusTFromBinderStatus(
527 mAudioPolicyServiceClient->onRecordingConfigurationUpdate(eventAidl,
528 clientInfoAidl,
529 clientConfigAidl,
530 clientEffectsAidl,
531 deviceConfigAidl,
532 effectsAidl,
533 patchHandleAidl,
534 sourceAidl));
535 }();
536 ALOGW_IF(status != OK, "onRecordingConfigurationUpdate() failed: %d", status);
Jean-Michel Trivide801052015-04-14 19:10:14 -0700537 }
538}
539
Eric Laurente8726fe2015-06-26 09:39:24 -0700540void AudioPolicyService::NotificationClient::setAudioPortCallbacksEnabled(bool enabled)
541{
542 mAudioPortCallbacksEnabled = enabled;
543}
544
François Gaffiecfe17322018-11-07 13:41:29 +0100545void AudioPolicyService::NotificationClient::setAudioVolumeGroupCallbacksEnabled(bool enabled)
546{
547 mAudioVolumeGroupCallbacksEnabled = enabled;
548}
Eric Laurente8726fe2015-06-26 09:39:24 -0700549
Jean-Michel Trivi9a6b9ad2020-10-22 16:46:43 -0700550void AudioPolicyService::NotificationClient::onRoutingUpdated()
551{
552 if (mAudioPolicyServiceClient != 0 && isServiceUid(mUid)) {
553 mAudioPolicyServiceClient->onRoutingUpdated();
554 }
555}
556
Mathias Agopian65ab4712010-07-14 17:59:35 -0700557void AudioPolicyService::binderDied(const wp<IBinder>& who) {
Glenn Kasten411e4472012-11-02 10:00:06 -0700558 ALOGW("binderDied() %p, calling pid %d", who.unsafe_get(),
Eric Laurentde070132010-07-13 04:45:46 -0700559 IPCThreadState::self()->getCallingPid());
Mathias Agopian65ab4712010-07-14 17:59:35 -0700560}
561
Mikhail Naganov12b716c2020-04-30 22:37:43 +0000562static bool dumpTryLock(Mutex& mutex) ACQUIRE(mutex) NO_THREAD_SAFETY_ANALYSIS
Mathias Agopian65ab4712010-07-14 17:59:35 -0700563{
Mikhail Naganov12b716c2020-04-30 22:37:43 +0000564 return mutex.timedLock(kDumpLockTimeoutNs) == NO_ERROR;
565}
566
567static void dumpReleaseLock(Mutex& mutex, bool locked) RELEASE(mutex) NO_THREAD_SAFETY_ANALYSIS
568{
569 if (locked) mutex.unlock();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700570}
571
572status_t AudioPolicyService::dumpInternals(int fd)
573{
574 const size_t SIZE = 256;
575 char buffer[SIZE];
576 String8 result;
577
Eric Laurentdce54a12014-03-10 12:19:46 -0700578 snprintf(buffer, SIZE, "AudioPolicyManager: %p\n", mAudioPolicyManager);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700579 result.append(buffer);
580 snprintf(buffer, SIZE, "Command Thread: %p\n", mAudioCommandThread.get());
581 result.append(buffer);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700582
Hayden Gomes524159d2019-12-23 14:41:47 -0800583 snprintf(buffer, SIZE, "Supported System Usages:\n");
584 result.append(buffer);
585 for (std::vector<audio_usage_t>::iterator it = mSupportedSystemUsages.begin();
586 it != mSupportedSystemUsages.end(); ++it) {
587 snprintf(buffer, SIZE, "\t%d\n", *it);
588 result.append(buffer);
589 }
590
Mathias Agopian65ab4712010-07-14 17:59:35 -0700591 write(fd, result.string(), result.size());
592 return NO_ERROR;
593}
594
Eric Laurente8c8b432018-10-17 10:08:02 -0700595void AudioPolicyService::updateUidStates()
Svet Ganovf4ddfef2018-01-16 07:37:58 -0800596{
Eric Laurente8c8b432018-10-17 10:08:02 -0700597 Mutex::Autolock _l(mLock);
598 updateUidStates_l();
599}
600
601void AudioPolicyService::updateUidStates_l()
602{
Eric Laurent4eb58f12018-12-07 16:41:02 -0800603// Go over all active clients and allow capture (does not force silence) in the
604// following cases:
Evan Severson1f700cd2021-02-10 13:10:37 -0800605// The client is the assistant
606// AND an accessibility service is on TOP or a RTT call is active
Eric Laurent589171c2019-07-25 18:04:29 -0700607// AND the source is VOICE_RECOGNITION or HOTWORD
Evan Severson1f700cd2021-02-10 13:10:37 -0800608// OR uses VOICE_RECOGNITION AND is on TOP
609// OR uses HOTWORD
610// AND there is no active privacy sensitive capture or call
611// OR client has CAPTURE_AUDIO_OUTPUT privileged permission
612// OR The client is an accessibility service
613// AND Is on TOP
614// AND the source is VOICE_RECOGNITION or HOTWORD
615// OR The assistant is not on TOP
Eric Laurent589171c2019-07-25 18:04:29 -0700616// AND there is no active privacy sensitive capture or call
617// OR client has CAPTURE_AUDIO_OUTPUT privileged permission
Evan Severson1f700cd2021-02-10 13:10:37 -0800618// AND is on TOP
619// AND the source is VOICE_RECOGNITION or HOTWORD
620// OR the client source is virtual (remote submix, call audio TX or RX...)
621// OR the client source is HOTWORD
622// AND is on TOP
623// OR all active clients are using HOTWORD source
624// AND no call is active
625// OR client has CAPTURE_AUDIO_OUTPUT privileged permission
626// OR the client is the current InputMethodService
627// AND a RTT call is active AND the source is VOICE_RECOGNITION
628// OR Any client
629// AND The assistant is not on TOP
630// AND is on TOP or latest started
631// AND there is no active privacy sensitive capture or call
632// OR client has CAPTURE_AUDIO_OUTPUT privileged permission
Eric Laurent4eb58f12018-12-07 16:41:02 -0800633
Eric Laurent4e947da2019-10-17 15:24:06 -0700634
Eric Laurent4eb58f12018-12-07 16:41:02 -0800635 sp<AudioRecordClient> topActive;
636 sp<AudioRecordClient> latestActive;
Eric Laurentc21d5692020-02-25 10:24:36 -0800637 sp<AudioRecordClient> topSensitiveActive;
Eric Laurentb809a752020-06-29 09:53:13 -0700638 sp<AudioRecordClient> latestSensitiveActiveOrComm;
Eric Laurent1ff16a72019-03-14 18:35:04 -0700639
Eric Laurenta46bedb2018-12-07 18:01:26 -0800640 nsecs_t topStartNs = 0;
641 nsecs_t latestStartNs = 0;
Eric Laurentc21d5692020-02-25 10:24:36 -0800642 nsecs_t topSensitiveStartNs = 0;
Eric Laurent4eb58f12018-12-07 16:41:02 -0800643 nsecs_t latestSensitiveStartNs = 0;
644 bool isA11yOnTop = mUidPolicy->isA11yOnTop();
645 bool isAssistantOnTop = false;
646 bool isSensitiveActive = false;
Eric Laurent1ff16a72019-03-14 18:35:04 -0700647 bool isInCall = mPhoneState == AUDIO_MODE_IN_CALL;
Eric Laurentc21d5692020-02-25 10:24:36 -0800648 bool isInCommunication = mPhoneState == AUDIO_MODE_IN_COMMUNICATION;
649 bool rttCallActive = (isInCall || isInCommunication)
Eric Laurent6ede98f2019-06-11 14:50:30 -0700650 && mUidPolicy->isRttEnabled();
Eric Laurent4e947da2019-10-17 15:24:06 -0700651 bool onlyHotwordActive = true;
Eric Laurentb809a752020-06-29 09:53:13 -0700652 bool isPhoneStateOwnerActive = false;
Eric Laurent4eb58f12018-12-07 16:41:02 -0800653
Michael Groovercfd28302018-12-11 19:16:46 -0800654 // if Sensor Privacy is enabled then all recordings should be silenced.
655 if (mSensorPrivacyPolicy->isSensorPrivacyEnabled()) {
656 silenceAllRecordings_l();
657 return;
658 }
659
Eric Laurente8c8b432018-10-17 10:08:02 -0700660 for (size_t i =0; i < mAudioRecordClients.size(); i++) {
661 sp<AudioRecordClient> current = mAudioRecordClients[i];
Svet Ganov33761132021-05-13 22:51:08 +0000662 uid_t currentUid = VALUE_OR_FATAL(aidl2legacy_int32_t_uid_t(
663 current->attributionSource.uid));
Evan Severson1f700cd2021-02-10 13:10:37 -0800664 if (!current->active) {
Eric Laurent1ff16a72019-03-14 18:35:04 -0700665 continue;
Eric Laurent4eb58f12018-12-07 16:41:02 -0800666 }
Eric Laurent1ff16a72019-03-14 18:35:04 -0700667
Philip P. Moltmannbda45752020-07-17 16:41:18 -0700668 app_state_t appState = apmStatFromAmState(mUidPolicy->getUidState(currentUid));
Eric Laurent1ff16a72019-03-14 18:35:04 -0700669 // clients which app is in IDLE state are not eligible for top active or
670 // latest active
671 if (appState == APP_STATE_IDLE) {
672 continue;
673 }
674
Philip P. Moltmannbda45752020-07-17 16:41:18 -0700675 bool isAccessibility = mUidPolicy->isA11yUid(currentUid);
Eric Laurent14a88632020-07-16 12:28:30 -0700676 // Clients capturing for Accessibility services or virtual sources are not considered
Eric Laurentc21d5692020-02-25 10:24:36 -0800677 // for top or latest active to avoid masking regular clients started before
Eric Laurent14a88632020-07-16 12:28:30 -0700678 if (!isAccessibility && !isVirtualSource(current->attributes.source)) {
Philip P. Moltmannbda45752020-07-17 16:41:18 -0700679 bool isAssistant = mUidPolicy->isAssistantUid(currentUid);
Eric Laurentc21d5692020-02-25 10:24:36 -0800680 bool isPrivacySensitive =
681 (current->attributes.flags & AUDIO_FLAG_CAPTURE_PRIVATE) != 0;
Eric Laurentb809a752020-06-29 09:53:13 -0700682
Eric Laurentc21d5692020-02-25 10:24:36 -0800683 if (appState == APP_STATE_TOP) {
684 if (isPrivacySensitive) {
685 if (current->startTimeNs > topSensitiveStartNs) {
686 topSensitiveActive = current;
687 topSensitiveStartNs = current->startTimeNs;
688 }
689 } else {
690 if (current->startTimeNs > topStartNs) {
691 topActive = current;
692 topStartNs = current->startTimeNs;
693 }
694 }
695 if (isAssistant) {
696 isAssistantOnTop = true;
697 }
Eric Laurenta46bedb2018-12-07 18:01:26 -0800698 }
Eric Laurentc21d5692020-02-25 10:24:36 -0800699 // Clients capturing for HOTWORD are not considered
700 // for latest active to avoid masking regular clients started before
701 if (!(current->attributes.source == AUDIO_SOURCE_HOTWORD
702 || ((isA11yOnTop || rttCallActive) && isAssistant))) {
703 if (isPrivacySensitive) {
Eric Laurentb809a752020-06-29 09:53:13 -0700704 // if audio mode is IN_COMMUNICATION, make sure the audio mode owner
705 // is marked latest sensitive active even if another app qualifies.
706 if (current->startTimeNs > latestSensitiveStartNs
Philip P. Moltmannbda45752020-07-17 16:41:18 -0700707 || (isInCommunication && currentUid == mPhoneStateOwnerUid)) {
Eric Laurentb809a752020-06-29 09:53:13 -0700708 if (!isInCommunication || latestSensitiveActiveOrComm == nullptr
Philip P. Moltmannbda45752020-07-17 16:41:18 -0700709 || VALUE_OR_FATAL(aidl2legacy_int32_t_uid_t(
Svet Ganov33761132021-05-13 22:51:08 +0000710 latestSensitiveActiveOrComm->attributionSource.uid))
Philip P. Moltmannbda45752020-07-17 16:41:18 -0700711 != mPhoneStateOwnerUid) {
Eric Laurentb809a752020-06-29 09:53:13 -0700712 latestSensitiveActiveOrComm = current;
713 latestSensitiveStartNs = current->startTimeNs;
714 }
Eric Laurentc21d5692020-02-25 10:24:36 -0800715 }
716 isSensitiveActive = true;
717 } else {
718 if (current->startTimeNs > latestStartNs) {
719 latestActive = current;
720 latestStartNs = current->startTimeNs;
721 }
722 }
Eric Laurent4eb58f12018-12-07 16:41:02 -0800723 }
724 }
Eric Laurent4e947da2019-10-17 15:24:06 -0700725 if (current->attributes.source != AUDIO_SOURCE_HOTWORD) {
726 onlyHotwordActive = false;
727 }
Philip P. Moltmannbda45752020-07-17 16:41:18 -0700728 if (currentUid == mPhoneStateOwnerUid) {
Eric Laurentb809a752020-06-29 09:53:13 -0700729 isPhoneStateOwnerActive = true;
730 }
Eric Laurent4eb58f12018-12-07 16:41:02 -0800731 }
732
Eric Laurent1ff16a72019-03-14 18:35:04 -0700733 // if no active client with UI on Top, consider latest active as top
734 if (topActive == nullptr) {
735 topActive = latestActive;
Eric Laurentc21d5692020-02-25 10:24:36 -0800736 topStartNs = latestStartNs;
737 }
738 if (topSensitiveActive == nullptr) {
Eric Laurentb809a752020-06-29 09:53:13 -0700739 topSensitiveActive = latestSensitiveActiveOrComm;
Eric Laurentc21d5692020-02-25 10:24:36 -0800740 topSensitiveStartNs = latestSensitiveStartNs;
Eric Laurentb809a752020-06-29 09:53:13 -0700741 } else if (latestSensitiveActiveOrComm != nullptr) {
742 // if audio mode is IN_COMMUNICATION, favor audio mode owner over an app with
743 // foreground UI in case both are capturing with privacy sensitive flag.
Philip P. Moltmannbda45752020-07-17 16:41:18 -0700744 uid_t latestActiveUid = VALUE_OR_FATAL(
Svet Ganov33761132021-05-13 22:51:08 +0000745 aidl2legacy_int32_t_uid_t(latestSensitiveActiveOrComm->attributionSource.uid));
Philip P. Moltmannbda45752020-07-17 16:41:18 -0700746 if (isInCommunication && latestActiveUid == mPhoneStateOwnerUid) {
Eric Laurentb809a752020-06-29 09:53:13 -0700747 topSensitiveActive = latestSensitiveActiveOrComm;
748 topSensitiveStartNs = latestSensitiveStartNs;
749 }
Eric Laurentc21d5692020-02-25 10:24:36 -0800750 }
751
752 // If both privacy sensitive and regular capture are active:
753 // if the regular capture is privileged
754 // allow concurrency
755 // else
756 // favor the privacy sensitive case
757 if (topActive != nullptr && topSensitiveActive != nullptr
Ricardo Correa57a37692020-03-23 17:27:25 -0700758 && !topActive->canCaptureOutput) {
Eric Laurentc21d5692020-02-25 10:24:36 -0800759 topActive = nullptr;
Eric Laurent4eb58f12018-12-07 16:41:02 -0800760 }
761
762 for (size_t i =0; i < mAudioRecordClients.size(); i++) {
763 sp<AudioRecordClient> current = mAudioRecordClients[i];
Philip P. Moltmannbda45752020-07-17 16:41:18 -0700764 uid_t currentUid = VALUE_OR_FATAL(aidl2legacy_int32_t_uid_t(
Svet Ganov33761132021-05-13 22:51:08 +0000765 current->attributionSource.uid));
Eric Laurent1ff16a72019-03-14 18:35:04 -0700766 if (!current->active) {
767 continue;
768 }
769
Eric Laurent4eb58f12018-12-07 16:41:02 -0800770 audio_source_t source = current->attributes.source;
Philip P. Moltmannbda45752020-07-17 16:41:18 -0700771 bool isTopOrLatestActive = topActive == nullptr ? false :
Svet Ganov33761132021-05-13 22:51:08 +0000772 current->attributionSource.uid == topActive->attributionSource.uid;
Philip P. Moltmannbda45752020-07-17 16:41:18 -0700773 bool isTopOrLatestSensitive = topSensitiveActive == nullptr ? false :
Svet Ganov33761132021-05-13 22:51:08 +0000774 current->attributionSource.uid == topSensitiveActive->attributionSource.uid;
Eric Laurentc21d5692020-02-25 10:24:36 -0800775
Mikhail Naganov12b716c2020-04-30 22:37:43 +0000776 auto canCaptureIfInCallOrCommunication = [&](const auto &recordClient) REQUIRES(mLock) {
Philip P. Moltmannbda45752020-07-17 16:41:18 -0700777 uid_t recordUid = VALUE_OR_FATAL(aidl2legacy_int32_t_uid_t(
Svet Ganov33761132021-05-13 22:51:08 +0000778 recordClient->attributionSource.uid));
Ricardo Correa57a37692020-03-23 17:27:25 -0700779 bool canCaptureCall = recordClient->canCaptureOutput;
Eric Laurentb809a752020-06-29 09:53:13 -0700780 bool canCaptureCommunication = recordClient->canCaptureOutput
781 || !isPhoneStateOwnerActive
Philip P. Moltmannbda45752020-07-17 16:41:18 -0700782 || recordUid == mPhoneStateOwnerUid;
Eric Laurentb809a752020-06-29 09:53:13 -0700783 return !(isInCall && !canCaptureCall)
784 && !(isInCommunication && !canCaptureCommunication);
Eric Laurentc21d5692020-02-25 10:24:36 -0800785 };
Eric Laurent1ff16a72019-03-14 18:35:04 -0700786
787 // By default allow capture if:
788 // The assistant is not on TOP
Eric Laurenta171e352019-05-07 13:04:45 -0700789 // AND is on TOP or latest started
Eric Laurent1ff16a72019-03-14 18:35:04 -0700790 // AND there is no active privacy sensitive capture or call
791 // OR client has CAPTURE_AUDIO_OUTPUT privileged permission
792 bool allowCapture = !isAssistantOnTop
Eric Laurentc21d5692020-02-25 10:24:36 -0800793 && (isTopOrLatestActive || isTopOrLatestSensitive)
794 && !(isSensitiveActive
Ricardo Correa57a37692020-03-23 17:27:25 -0700795 && !(isTopOrLatestSensitive || current->canCaptureOutput))
Eric Laurentc21d5692020-02-25 10:24:36 -0800796 && canCaptureIfInCallOrCommunication(current);
Eric Laurent2dc962b2019-03-01 08:25:25 -0800797
Eric Laurented726cc2021-07-01 14:26:41 +0200798 if (!current->hasOp()) {
799 // Never allow capture if app op is denied
800 allowCapture = false;
801 } else if (isVirtualSource(source)) {
Eric Laurent1ff16a72019-03-14 18:35:04 -0700802 // Allow capture for virtual (remote submix, call audio TX or RX...) sources
803 allowCapture = true;
Philip P. Moltmannbda45752020-07-17 16:41:18 -0700804 } else if (mUidPolicy->isAssistantUid(currentUid)) {
Eric Laurent1ff16a72019-03-14 18:35:04 -0700805 // For assistant allow capture if:
Eric Laurent6ede98f2019-06-11 14:50:30 -0700806 // An accessibility service is on TOP or a RTT call is active
Eric Laurent1ff16a72019-03-14 18:35:04 -0700807 // AND the source is VOICE_RECOGNITION or HOTWORD
Eric Laurenta171e352019-05-07 13:04:45 -0700808 // OR is on TOP AND uses VOICE_RECOGNITION
Eric Laurent1ff16a72019-03-14 18:35:04 -0700809 // OR uses HOTWORD
810 // AND there is no active privacy sensitive capture or call
811 // OR client has CAPTURE_AUDIO_OUTPUT privileged permission
Eric Laurent6ede98f2019-06-11 14:50:30 -0700812 if (isA11yOnTop || rttCallActive) {
Eric Laurent4eb58f12018-12-07 16:41:02 -0800813 if (source == AUDIO_SOURCE_HOTWORD || source == AUDIO_SOURCE_VOICE_RECOGNITION) {
Eric Laurent1ff16a72019-03-14 18:35:04 -0700814 allowCapture = true;
Eric Laurent4eb58f12018-12-07 16:41:02 -0800815 }
816 } else {
Eric Laurenta171e352019-05-07 13:04:45 -0700817 if (((isAssistantOnTop && source == AUDIO_SOURCE_VOICE_RECOGNITION) ||
Eric Laurentc21d5692020-02-25 10:24:36 -0800818 source == AUDIO_SOURCE_HOTWORD)
Ricardo Correa57a37692020-03-23 17:27:25 -0700819 && !(isSensitiveActive && !current->canCaptureOutput)
Eric Laurentc21d5692020-02-25 10:24:36 -0800820 && canCaptureIfInCallOrCommunication(current)) {
Eric Laurent1ff16a72019-03-14 18:35:04 -0700821 allowCapture = true;
Eric Laurent4eb58f12018-12-07 16:41:02 -0800822 }
823 }
Philip P. Moltmannbda45752020-07-17 16:41:18 -0700824 } else if (mUidPolicy->isA11yUid(currentUid)) {
Eric Laurent1ff16a72019-03-14 18:35:04 -0700825 // For accessibility service allow capture if:
Eric Laurent47670c92019-08-28 16:59:05 -0700826 // The assistant is not on TOP
827 // AND there is no active privacy sensitive capture or call
Eric Laurent589171c2019-07-25 18:04:29 -0700828 // OR client has CAPTURE_AUDIO_OUTPUT privileged permission
Eric Laurent47670c92019-08-28 16:59:05 -0700829 // OR
830 // Is on TOP AND the source is VOICE_RECOGNITION or HOTWORD
831 if (!isAssistantOnTop
Ricardo Correa57a37692020-03-23 17:27:25 -0700832 && !(isSensitiveActive && !current->canCaptureOutput)
Eric Laurentc21d5692020-02-25 10:24:36 -0800833 && canCaptureIfInCallOrCommunication(current)) {
Eric Laurent47670c92019-08-28 16:59:05 -0700834 allowCapture = true;
835 }
Eric Laurent589171c2019-07-25 18:04:29 -0700836 if (isA11yOnTop) {
837 if (source == AUDIO_SOURCE_VOICE_RECOGNITION || source == AUDIO_SOURCE_HOTWORD) {
838 allowCapture = true;
839 }
Eric Laurent4eb58f12018-12-07 16:41:02 -0800840 }
Eric Laurent4e947da2019-10-17 15:24:06 -0700841 } else if (source == AUDIO_SOURCE_HOTWORD) {
842 // For HOTWORD source allow capture when not on TOP if:
843 // All active clients are using HOTWORD source
844 // AND no call is active
845 // OR client has CAPTURE_AUDIO_OUTPUT privileged permission
Eric Laurentc21d5692020-02-25 10:24:36 -0800846 if (onlyHotwordActive
847 && canCaptureIfInCallOrCommunication(current)) {
Eric Laurent4e947da2019-10-17 15:24:06 -0700848 allowCapture = true;
849 }
Philip P. Moltmannbda45752020-07-17 16:41:18 -0700850 } else if (mUidPolicy->isCurrentImeUid(currentUid)) {
Kohsuke Yatoha623a132020-03-24 20:10:26 -0700851 // For current InputMethodService allow capture if:
852 // A RTT call is active AND the source is VOICE_RECOGNITION
853 if (rttCallActive && source == AUDIO_SOURCE_VOICE_RECOGNITION) {
854 allowCapture = true;
855 }
Eric Laurent4eb58f12018-12-07 16:41:02 -0800856 }
Eric Laurent8c7ef892021-06-10 13:32:16 +0200857 setAppState_l(current,
Philip P. Moltmannbda45752020-07-17 16:41:18 -0700858 allowCapture ? apmStatFromAmState(mUidPolicy->getUidState(currentUid)) :
Eric Laurent1ff16a72019-03-14 18:35:04 -0700859 APP_STATE_IDLE);
Eric Laurente8c8b432018-10-17 10:08:02 -0700860 }
861}
862
Michael Groovercfd28302018-12-11 19:16:46 -0800863void AudioPolicyService::silenceAllRecordings_l() {
864 for (size_t i = 0; i < mAudioRecordClients.size(); i++) {
865 sp<AudioRecordClient> current = mAudioRecordClients[i];
Eric Laurent1ff16a72019-03-14 18:35:04 -0700866 if (!isVirtualSource(current->attributes.source)) {
Eric Laurent8c7ef892021-06-10 13:32:16 +0200867 setAppState_l(current, APP_STATE_IDLE);
Eric Laurent1ff16a72019-03-14 18:35:04 -0700868 }
Michael Groovercfd28302018-12-11 19:16:46 -0800869 }
870}
871
Eric Laurente8c8b432018-10-17 10:08:02 -0700872/* static */
873app_state_t AudioPolicyService::apmStatFromAmState(int amState) {
Eric Laurent1ff16a72019-03-14 18:35:04 -0700874
875 if (amState == ActivityManager::PROCESS_STATE_UNKNOWN) {
Eric Laurente8c8b432018-10-17 10:08:02 -0700876 return APP_STATE_IDLE;
Eric Laurent1ff16a72019-03-14 18:35:04 -0700877 } else if (amState <= ActivityManager::PROCESS_STATE_TOP) {
878 // include persistent services
879 return APP_STATE_TOP;
Eric Laurente8c8b432018-10-17 10:08:02 -0700880 }
881 return APP_STATE_FOREGROUND;
882}
883
Eric Laurent4eb58f12018-12-07 16:41:02 -0800884/* static */
Eric Laurent2dc962b2019-03-01 08:25:25 -0800885bool AudioPolicyService::isVirtualSource(audio_source_t source)
Eric Laurent4eb58f12018-12-07 16:41:02 -0800886{
887 switch (source) {
888 case AUDIO_SOURCE_VOICE_UPLINK:
889 case AUDIO_SOURCE_VOICE_DOWNLINK:
890 case AUDIO_SOURCE_VOICE_CALL:
Eric Laurent2dc962b2019-03-01 08:25:25 -0800891 case AUDIO_SOURCE_REMOTE_SUBMIX:
892 case AUDIO_SOURCE_FM_TUNER:
Eric Laurent68eb2122020-04-30 17:40:57 -0700893 case AUDIO_SOURCE_ECHO_REFERENCE:
Eric Laurent4eb58f12018-12-07 16:41:02 -0800894 return true;
895 default:
896 break;
897 }
898 return false;
899}
900
Eric Laurented726cc2021-07-01 14:26:41 +0200901/* static */
902bool AudioPolicyService::isAppOpSource(audio_source_t source)
903{
904 switch (source) {
905 case AUDIO_SOURCE_FM_TUNER:
906 case AUDIO_SOURCE_ECHO_REFERENCE:
Eric Laurent637bd202021-09-22 11:17:11 +0200907 case AUDIO_SOURCE_REMOTE_SUBMIX:
Eric Laurented726cc2021-07-01 14:26:41 +0200908 return false;
909 default:
910 break;
911 }
912 return true;
913}
914
Eric Laurent8c7ef892021-06-10 13:32:16 +0200915void AudioPolicyService::setAppState_l(sp<AudioRecordClient> client, app_state_t state)
Eric Laurente8c8b432018-10-17 10:08:02 -0700916{
917 AutoCallerClear acc;
918
919 if (mAudioPolicyManager) {
Eric Laurent8c7ef892021-06-10 13:32:16 +0200920 mAudioPolicyManager->setAppState(client->portId, state);
Mikhail Naganoveae73eb2018-04-03 16:57:36 -0700921 }
922 sp<IAudioFlinger> af = AudioSystem::get_audio_flinger();
923 if (af) {
Eric Laurentf32108e2018-10-04 17:22:04 -0700924 bool silenced = state == APP_STATE_IDLE;
Eric Laurent8c7ef892021-06-10 13:32:16 +0200925 if (client->silenced != silenced) {
926 if (client->active) {
927 if (silenced) {
928 finishRecording(client->attributionSource, client->attributes.source);
929 } else {
930 std::stringstream msg;
931 msg << "Audio recording un-silenced on session " << client->session;
932 if (!startRecording(client->attributionSource, String16(msg.str().c_str()),
933 client->attributes.source)) {
934 silenced = true;
935 }
936 }
937 }
938 af->setRecordSilenced(client->portId, silenced);
939 client->silenced = silenced;
940 }
Mikhail Naganoveae73eb2018-04-03 16:57:36 -0700941 }
Svet Ganovf4ddfef2018-01-16 07:37:58 -0800942}
943
Glenn Kasten0f11b512014-01-31 16:18:54 -0800944status_t AudioPolicyService::dump(int fd, const Vector<String16>& args __unused)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700945{
Glenn Kasten44deb052012-02-05 18:09:08 -0800946 if (!dumpAllowed()) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700947 dumpPermissionDenial(fd);
948 } else {
Mikhail Naganov12b716c2020-04-30 22:37:43 +0000949 const bool locked = dumpTryLock(mLock);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700950 if (!locked) {
951 String8 result(kDeadlockedString);
952 write(fd, result.string(), result.size());
953 }
954
955 dumpInternals(fd);
Glenn Kasten9d1f02d2012-02-08 17:47:58 -0800956 if (mAudioCommandThread != 0) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700957 mAudioCommandThread->dump(fd);
958 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700959
Eric Laurentdce54a12014-03-10 12:19:46 -0700960 if (mAudioPolicyManager) {
961 mAudioPolicyManager->dump(fd);
962 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700963
Kevin Rocard8be94972019-02-22 13:26:25 -0800964 mPackageManager.dump(fd);
965
Mikhail Naganov12b716c2020-04-30 22:37:43 +0000966 dumpReleaseLock(mLock, locked);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700967 }
968 return NO_ERROR;
969}
970
971status_t AudioPolicyService::dumpPermissionDenial(int fd)
972{
973 const size_t SIZE = 256;
974 char buffer[SIZE];
975 String8 result;
976 snprintf(buffer, SIZE, "Permission Denial: "
977 "can't dump AudioPolicyService from pid=%d, uid=%d\n",
978 IPCThreadState::self()->getCallingPid(),
979 IPCThreadState::self()->getCallingUid());
980 result.append(buffer);
981 write(fd, result.string(), result.size());
982 return NO_ERROR;
983}
984
985status_t AudioPolicyService::onTransact(
Svet Ganovf4ddfef2018-01-16 07:37:58 -0800986 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags) {
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800987 // make sure transactions reserved to AudioFlinger do not come from other processes
988 switch (code) {
989 case TRANSACTION_startOutput:
990 case TRANSACTION_stopOutput:
991 case TRANSACTION_releaseOutput:
992 case TRANSACTION_getInputForAttr:
993 case TRANSACTION_startInput:
994 case TRANSACTION_stopInput:
995 case TRANSACTION_releaseInput:
996 case TRANSACTION_getOutputForEffect:
997 case TRANSACTION_registerEffect:
998 case TRANSACTION_unregisterEffect:
999 case TRANSACTION_setEffectEnabled:
1000 case TRANSACTION_getStrategyForStream:
1001 case TRANSACTION_getOutputForAttr:
1002 case TRANSACTION_moveEffectsToIo:
1003 ALOGW("%s: transaction %d received from PID %d",
1004 __func__, code, IPCThreadState::self()->getCallingPid());
1005 return INVALID_OPERATION;
1006 default:
1007 break;
1008 }
1009
1010 // make sure the following transactions come from system components
1011 switch (code) {
1012 case TRANSACTION_setDeviceConnectionState:
1013 case TRANSACTION_handleDeviceConfigChange:
1014 case TRANSACTION_setPhoneState:
1015//FIXME: Allow setForceUse calls from system apps until a better use case routing API is available
1016// case TRANSACTION_setForceUse:
1017 case TRANSACTION_initStreamVolume:
1018 case TRANSACTION_setStreamVolumeIndex:
1019 case TRANSACTION_setVolumeIndexForAttributes:
1020 case TRANSACTION_getStreamVolumeIndex:
1021 case TRANSACTION_getVolumeIndexForAttributes:
1022 case TRANSACTION_getMinVolumeIndexForAttributes:
1023 case TRANSACTION_getMaxVolumeIndexForAttributes:
1024 case TRANSACTION_isStreamActive:
1025 case TRANSACTION_isStreamActiveRemotely:
1026 case TRANSACTION_isSourceActive:
1027 case TRANSACTION_getDevicesForStream:
1028 case TRANSACTION_registerPolicyMixes:
1029 case TRANSACTION_setMasterMono:
1030 case TRANSACTION_getSurroundFormats:
Kriti Dang6537def2021-03-02 13:46:59 +01001031 case TRANSACTION_getReportedSurroundFormats:
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001032 case TRANSACTION_setSurroundFormatEnabled:
1033 case TRANSACTION_setAssistantUid:
1034 case TRANSACTION_setA11yServicesUids:
1035 case TRANSACTION_setUidDeviceAffinities:
1036 case TRANSACTION_removeUidDeviceAffinities:
1037 case TRANSACTION_setUserIdDeviceAffinities:
1038 case TRANSACTION_removeUserIdDeviceAffinities:
1039 case TRANSACTION_getHwOffloadEncodingFormatsSupportedForA2DP:
1040 case TRANSACTION_listAudioVolumeGroups:
1041 case TRANSACTION_getVolumeGroupFromAudioAttributes:
1042 case TRANSACTION_acquireSoundTriggerSession:
1043 case TRANSACTION_releaseSoundTriggerSession:
1044 case TRANSACTION_setRttEnabled:
1045 case TRANSACTION_isCallScreenModeSupported:
1046 case TRANSACTION_setDevicesRoleForStrategy:
1047 case TRANSACTION_setSupportedSystemUsages:
1048 case TRANSACTION_removeDevicesRoleForStrategy:
1049 case TRANSACTION_getDevicesForRoleAndStrategy:
1050 case TRANSACTION_getDevicesForAttributes:
1051 case TRANSACTION_setAllowedCapturePolicy:
1052 case TRANSACTION_onNewAudioModulesAvailable:
1053 case TRANSACTION_setCurrentImeUid:
1054 case TRANSACTION_registerSoundTriggerCaptureStateListener:
1055 case TRANSACTION_setDevicesRoleForCapturePreset:
1056 case TRANSACTION_addDevicesRoleForCapturePreset:
1057 case TRANSACTION_removeDevicesRoleForCapturePreset:
1058 case TRANSACTION_clearDevicesRoleForCapturePreset:
Eric Laurent81dd0f52021-07-05 11:54:40 +02001059 case TRANSACTION_getDevicesForRoleAndCapturePreset:
1060 case TRANSACTION_getSpatializer: {
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001061 if (!isServiceUid(IPCThreadState::self()->getCallingUid())) {
1062 ALOGW("%s: transaction %d received from PID %d unauthorized UID %d",
1063 __func__, code, IPCThreadState::self()->getCallingPid(),
1064 IPCThreadState::self()->getCallingUid());
1065 return INVALID_OPERATION;
1066 }
1067 } break;
1068 default:
1069 break;
1070 }
1071
1072 std::string tag("IAudioPolicyService command " + std::to_string(code));
1073 TimeCheck check(tag.c_str());
1074
Svet Ganovf4ddfef2018-01-16 07:37:58 -08001075 switch (code) {
1076 case SHELL_COMMAND_TRANSACTION: {
1077 int in = data.readFileDescriptor();
1078 int out = data.readFileDescriptor();
1079 int err = data.readFileDescriptor();
1080 int argc = data.readInt32();
1081 Vector<String16> args;
1082 for (int i = 0; i < argc && data.dataAvail() > 0; i++) {
1083 args.add(data.readString16());
1084 }
1085 sp<IBinder> unusedCallback;
1086 sp<IResultReceiver> resultReceiver;
1087 status_t status;
1088 if ((status = data.readNullableStrongBinder(&unusedCallback)) != NO_ERROR) {
1089 return status;
1090 }
1091 if ((status = data.readNullableStrongBinder(&resultReceiver)) != NO_ERROR) {
1092 return status;
1093 }
1094 status = shellCommand(in, out, err, args);
1095 if (resultReceiver != nullptr) {
1096 resultReceiver->send(status);
1097 }
1098 return NO_ERROR;
1099 }
1100 }
1101
Mathias Agopian65ab4712010-07-14 17:59:35 -07001102 return BnAudioPolicyService::onTransact(code, data, reply, flags);
1103}
1104
Svet Ganovf4ddfef2018-01-16 07:37:58 -08001105// ------------------- Shell command implementation -------------------
1106
1107// NOTE: This is a remote API - make sure all args are validated
1108status_t AudioPolicyService::shellCommand(int in, int out, int err, Vector<String16>& args) {
1109 if (!checkCallingPermission(sManageAudioPolicyPermission, nullptr, nullptr)) {
1110 return PERMISSION_DENIED;
1111 }
1112 if (in == BAD_TYPE || out == BAD_TYPE || err == BAD_TYPE) {
1113 return BAD_VALUE;
1114 }
jovanakbe066e12019-09-02 11:54:39 -07001115 if (args.size() >= 3 && args[0] == String16("set-uid-state")) {
Svet Ganovf4ddfef2018-01-16 07:37:58 -08001116 return handleSetUidState(args, err);
jovanakbe066e12019-09-02 11:54:39 -07001117 } else if (args.size() >= 2 && args[0] == String16("reset-uid-state")) {
Svet Ganovf4ddfef2018-01-16 07:37:58 -08001118 return handleResetUidState(args, err);
jovanakbe066e12019-09-02 11:54:39 -07001119 } else if (args.size() >= 2 && args[0] == String16("get-uid-state")) {
Svet Ganovf4ddfef2018-01-16 07:37:58 -08001120 return handleGetUidState(args, out, err);
Eric Laurent269acb42021-04-23 16:53:22 +02001121 } else if (args.size() >= 1 && args[0] == String16("purge_permission-cache")) {
1122 purgePermissionCache();
1123 return NO_ERROR;
Svet Ganovf4ddfef2018-01-16 07:37:58 -08001124 } else if (args.size() == 1 && args[0] == String16("help")) {
1125 printHelp(out);
1126 return NO_ERROR;
1127 }
1128 printHelp(err);
1129 return BAD_VALUE;
1130}
1131
jovanakbe066e12019-09-02 11:54:39 -07001132static status_t getUidForPackage(String16 packageName, int userId, /*inout*/uid_t& uid, int err) {
1133 if (userId < 0) {
1134 ALOGE("Invalid user: %d", userId);
1135 dprintf(err, "Invalid user: %d\n", userId);
Svet Ganovf4ddfef2018-01-16 07:37:58 -08001136 return BAD_VALUE;
1137 }
jovanakbe066e12019-09-02 11:54:39 -07001138
1139 PermissionController pc;
1140 uid = pc.getPackageUid(packageName, 0);
1141 if (uid <= 0) {
1142 ALOGE("Unknown package: '%s'", String8(packageName).string());
1143 dprintf(err, "Unknown package: '%s'\n", String8(packageName).string());
1144 return BAD_VALUE;
1145 }
1146
1147 uid = multiuser_get_uid(userId, uid);
1148 return NO_ERROR;
1149}
1150
1151status_t AudioPolicyService::handleSetUidState(Vector<String16>& args, int err) {
1152 // Valid arg.size() is 3 or 5, args.size() is 5 with --user option.
1153 if (!(args.size() == 3 || args.size() == 5)) {
1154 printHelp(err);
1155 return BAD_VALUE;
1156 }
1157
Svet Ganovf4ddfef2018-01-16 07:37:58 -08001158 bool active = false;
1159 if (args[2] == String16("active")) {
1160 active = true;
1161 } else if ((args[2] != String16("idle"))) {
1162 ALOGE("Expected active or idle but got: '%s'", String8(args[2]).string());
1163 return BAD_VALUE;
1164 }
jovanakbe066e12019-09-02 11:54:39 -07001165
1166 int userId = 0;
1167 if (args.size() >= 5 && args[3] == String16("--user")) {
1168 userId = atoi(String8(args[4]));
1169 }
1170
1171 uid_t uid;
1172 if (getUidForPackage(args[1], userId, uid, err) == BAD_VALUE) {
1173 return BAD_VALUE;
1174 }
1175
Mikhail Naganov12b716c2020-04-30 22:37:43 +00001176 sp<UidPolicy> uidPolicy;
1177 {
1178 Mutex::Autolock _l(mLock);
1179 uidPolicy = mUidPolicy;
1180 }
1181 if (uidPolicy) {
1182 uidPolicy->addOverrideUid(uid, active);
1183 return NO_ERROR;
1184 }
1185 return NO_INIT;
Svet Ganovf4ddfef2018-01-16 07:37:58 -08001186}
1187
1188status_t AudioPolicyService::handleResetUidState(Vector<String16>& args, int err) {
jovanakbe066e12019-09-02 11:54:39 -07001189 // Valid arg.size() is 2 or 4, args.size() is 4 with --user option.
1190 if (!(args.size() == 2 || args.size() == 4)) {
1191 printHelp(err);
Svet Ganovf4ddfef2018-01-16 07:37:58 -08001192 return BAD_VALUE;
1193 }
jovanakbe066e12019-09-02 11:54:39 -07001194
1195 int userId = 0;
1196 if (args.size() >= 4 && args[2] == String16("--user")) {
1197 userId = atoi(String8(args[3]));
1198 }
1199
1200 uid_t uid;
1201 if (getUidForPackage(args[1], userId, uid, err) == BAD_VALUE) {
1202 return BAD_VALUE;
1203 }
1204
Mikhail Naganov12b716c2020-04-30 22:37:43 +00001205 sp<UidPolicy> uidPolicy;
1206 {
1207 Mutex::Autolock _l(mLock);
1208 uidPolicy = mUidPolicy;
1209 }
1210 if (uidPolicy) {
1211 uidPolicy->removeOverrideUid(uid);
1212 return NO_ERROR;
1213 }
1214 return NO_INIT;
Svet Ganovf4ddfef2018-01-16 07:37:58 -08001215}
1216
1217status_t AudioPolicyService::handleGetUidState(Vector<String16>& args, int out, int err) {
jovanakbe066e12019-09-02 11:54:39 -07001218 // Valid arg.size() is 2 or 4, args.size() is 4 with --user option.
1219 if (!(args.size() == 2 || args.size() == 4)) {
1220 printHelp(err);
Svet Ganovf4ddfef2018-01-16 07:37:58 -08001221 return BAD_VALUE;
1222 }
jovanakbe066e12019-09-02 11:54:39 -07001223
1224 int userId = 0;
1225 if (args.size() >= 4 && args[2] == String16("--user")) {
1226 userId = atoi(String8(args[3]));
1227 }
1228
1229 uid_t uid;
1230 if (getUidForPackage(args[1], userId, uid, err) == BAD_VALUE) {
1231 return BAD_VALUE;
1232 }
1233
Mikhail Naganov12b716c2020-04-30 22:37:43 +00001234 sp<UidPolicy> uidPolicy;
1235 {
1236 Mutex::Autolock _l(mLock);
1237 uidPolicy = mUidPolicy;
Svet Ganovf4ddfef2018-01-16 07:37:58 -08001238 }
Mikhail Naganov12b716c2020-04-30 22:37:43 +00001239 if (uidPolicy) {
1240 return dprintf(out, uidPolicy->isUidActive(uid) ? "active\n" : "idle\n");
1241 }
1242 return NO_INIT;
Svet Ganovf4ddfef2018-01-16 07:37:58 -08001243}
1244
1245status_t AudioPolicyService::printHelp(int out) {
1246 return dprintf(out, "Audio policy service commands:\n"
jovanakbe066e12019-09-02 11:54:39 -07001247 " get-uid-state <PACKAGE> [--user USER_ID] gets the uid state\n"
1248 " set-uid-state <PACKAGE> <active|idle> [--user USER_ID] overrides the uid state\n"
1249 " reset-uid-state <PACKAGE> [--user USER_ID] clears the uid state override\n"
Svet Ganovf4ddfef2018-01-16 07:37:58 -08001250 " help print this message\n");
1251}
1252
1253// ----------- AudioPolicyService::UidPolicy implementation ----------
1254
1255void AudioPolicyService::UidPolicy::registerSelf() {
Steven Moreland2f348142019-07-02 15:59:07 -07001256 status_t res = mAm.linkToDeath(this);
1257 mAm.registerUidObserver(this, ActivityManager::UID_OBSERVER_GONE
Svet Ganovf4ddfef2018-01-16 07:37:58 -08001258 | ActivityManager::UID_OBSERVER_IDLE
Eric Laurente8c8b432018-10-17 10:08:02 -07001259 | ActivityManager::UID_OBSERVER_ACTIVE
1260 | ActivityManager::UID_OBSERVER_PROCSTATE,
Svet Ganovf4ddfef2018-01-16 07:37:58 -08001261 ActivityManager::PROCESS_STATE_UNKNOWN,
1262 String16("audioserver"));
Mikhail Naganoveae73eb2018-04-03 16:57:36 -07001263 if (!res) {
1264 Mutex::Autolock _l(mLock);
1265 mObserverRegistered = true;
1266 } else {
1267 ALOGE("UidPolicy::registerSelf linkToDeath failed: %d", res);
Eric Laurent4eb58f12018-12-07 16:41:02 -08001268
Steven Moreland2f348142019-07-02 15:59:07 -07001269 mAm.unregisterUidObserver(this);
Mikhail Naganoveae73eb2018-04-03 16:57:36 -07001270 }
Svet Ganovf4ddfef2018-01-16 07:37:58 -08001271}
1272
1273void AudioPolicyService::UidPolicy::unregisterSelf() {
Steven Moreland2f348142019-07-02 15:59:07 -07001274 mAm.unlinkToDeath(this);
1275 mAm.unregisterUidObserver(this);
Mikhail Naganoveae73eb2018-04-03 16:57:36 -07001276 Mutex::Autolock _l(mLock);
1277 mObserverRegistered = false;
Svet Ganovf4ddfef2018-01-16 07:37:58 -08001278}
1279
Mikhail Naganoveae73eb2018-04-03 16:57:36 -07001280void AudioPolicyService::UidPolicy::binderDied(__unused const wp<IBinder> &who) {
1281 Mutex::Autolock _l(mLock);
1282 mCachedUids.clear();
1283 mObserverRegistered = false;
Svet Ganovf4ddfef2018-01-16 07:37:58 -08001284}
1285
Eric Laurente8c8b432018-10-17 10:08:02 -07001286void AudioPolicyService::UidPolicy::checkRegistered() {
Mikhail Naganoveae73eb2018-04-03 16:57:36 -07001287 bool needToReregister = false;
1288 {
1289 Mutex::Autolock _l(mLock);
1290 needToReregister = !mObserverRegistered;
Svet Ganovf4ddfef2018-01-16 07:37:58 -08001291 }
Mikhail Naganoveae73eb2018-04-03 16:57:36 -07001292 if (needToReregister) {
1293 // Looks like ActivityManager has died previously, attempt to re-register.
1294 registerSelf();
1295 }
Eric Laurente8c8b432018-10-17 10:08:02 -07001296}
1297
1298bool AudioPolicyService::UidPolicy::isUidActive(uid_t uid) {
1299 if (isServiceUid(uid)) return true;
1300 checkRegistered();
Mikhail Naganoveae73eb2018-04-03 16:57:36 -07001301 {
1302 Mutex::Autolock _l(mLock);
1303 auto overrideIter = mOverrideUids.find(uid);
1304 if (overrideIter != mOverrideUids.end()) {
Eric Laurente8c8b432018-10-17 10:08:02 -07001305 return overrideIter->second.first;
Mikhail Naganoveae73eb2018-04-03 16:57:36 -07001306 }
1307 // In an absense of the ActivityManager, assume everything to be active.
1308 if (!mObserverRegistered) return true;
1309 auto cacheIter = mCachedUids.find(uid);
Mikhail Naganoveba668a2018-04-05 08:13:15 -07001310 if (cacheIter != mCachedUids.end()) {
Eric Laurente8c8b432018-10-17 10:08:02 -07001311 return cacheIter->second.first;
Mikhail Naganoveae73eb2018-04-03 16:57:36 -07001312 }
1313 }
1314 ActivityManager am;
Hui Yu12c7ec72020-05-04 17:40:52 +00001315 bool active = am.isUidActive(uid, String16("audioserver"));
Mikhail Naganoveae73eb2018-04-03 16:57:36 -07001316 {
1317 Mutex::Autolock _l(mLock);
Eric Laurente8c8b432018-10-17 10:08:02 -07001318 mCachedUids.insert(std::pair<uid_t,
1319 std::pair<bool, int>>(uid, std::pair<bool, int>(active,
1320 ActivityManager::PROCESS_STATE_UNKNOWN)));
Mikhail Naganoveae73eb2018-04-03 16:57:36 -07001321 }
1322 return active;
Svet Ganovf4ddfef2018-01-16 07:37:58 -08001323}
1324
Eric Laurente8c8b432018-10-17 10:08:02 -07001325int AudioPolicyService::UidPolicy::getUidState(uid_t uid) {
1326 if (isServiceUid(uid)) {
1327 return ActivityManager::PROCESS_STATE_TOP;
1328 }
1329 checkRegistered();
1330 {
1331 Mutex::Autolock _l(mLock);
1332 auto overrideIter = mOverrideUids.find(uid);
1333 if (overrideIter != mOverrideUids.end()) {
1334 if (overrideIter->second.first) {
1335 if (overrideIter->second.second != ActivityManager::PROCESS_STATE_UNKNOWN) {
1336 return overrideIter->second.second;
1337 } else {
1338 auto cacheIter = mCachedUids.find(uid);
1339 if (cacheIter != mCachedUids.end()) {
1340 return cacheIter->second.second;
1341 }
1342 }
1343 }
1344 return ActivityManager::PROCESS_STATE_UNKNOWN;
1345 }
1346 // In an absense of the ActivityManager, assume everything to be active.
1347 if (!mObserverRegistered) {
1348 return ActivityManager::PROCESS_STATE_TOP;
1349 }
1350 auto cacheIter = mCachedUids.find(uid);
1351 if (cacheIter != mCachedUids.end()) {
1352 if (cacheIter->second.first) {
1353 return cacheIter->second.second;
1354 } else {
1355 return ActivityManager::PROCESS_STATE_UNKNOWN;
1356 }
1357 }
1358 }
1359 ActivityManager am;
Hui Yu12c7ec72020-05-04 17:40:52 +00001360 bool active = am.isUidActive(uid, String16("audioserver"));
Eric Laurente8c8b432018-10-17 10:08:02 -07001361 int state = ActivityManager::PROCESS_STATE_UNKNOWN;
1362 if (active) {
1363 state = am.getUidProcessState(uid, String16("audioserver"));
1364 }
1365 {
1366 Mutex::Autolock _l(mLock);
1367 mCachedUids.insert(std::pair<uid_t,
1368 std::pair<bool, int>>(uid, std::pair<bool, int>(active, state)));
1369 }
Eric Laurent4eb58f12018-12-07 16:41:02 -08001370
Eric Laurente8c8b432018-10-17 10:08:02 -07001371 return state;
1372}
1373
Mikhail Naganoveae73eb2018-04-03 16:57:36 -07001374void AudioPolicyService::UidPolicy::onUidActive(uid_t uid) {
Eric Laurente8c8b432018-10-17 10:08:02 -07001375 updateUid(&mCachedUids, uid, true, ActivityManager::PROCESS_STATE_UNKNOWN, true);
Mikhail Naganoveae73eb2018-04-03 16:57:36 -07001376}
1377
1378void AudioPolicyService::UidPolicy::onUidGone(uid_t uid, __unused bool disabled) {
Eric Laurente8c8b432018-10-17 10:08:02 -07001379 updateUid(&mCachedUids, uid, false, ActivityManager::PROCESS_STATE_UNKNOWN, false);
Mikhail Naganoveae73eb2018-04-03 16:57:36 -07001380}
1381
1382void AudioPolicyService::UidPolicy::onUidIdle(uid_t uid, __unused bool disabled) {
Eric Laurente8c8b432018-10-17 10:08:02 -07001383 updateUid(&mCachedUids, uid, false, ActivityManager::PROCESS_STATE_UNKNOWN, true);
Mikhail Naganoveae73eb2018-04-03 16:57:36 -07001384}
1385
Eric Laurente8c8b432018-10-17 10:08:02 -07001386void AudioPolicyService::UidPolicy::onUidStateChanged(uid_t uid,
1387 int32_t procState,
Hui Yu13ad0eb2019-09-09 10:27:07 -07001388 int64_t procStateSeq __unused,
1389 int32_t capability __unused) {
Eric Laurente8c8b432018-10-17 10:08:02 -07001390 if (procState != ActivityManager::PROCESS_STATE_UNKNOWN) {
1391 updateUid(&mCachedUids, uid, true, procState, true);
Mikhail Naganoveae73eb2018-04-03 16:57:36 -07001392 }
1393}
1394
1395void AudioPolicyService::UidPolicy::updateOverrideUid(uid_t uid, bool active, bool insert) {
Eric Laurente8c8b432018-10-17 10:08:02 -07001396 updateUid(&mOverrideUids, uid, active, ActivityManager::PROCESS_STATE_UNKNOWN, insert);
1397}
1398
1399void AudioPolicyService::UidPolicy::notifyService() {
1400 sp<AudioPolicyService> service = mService.promote();
1401 if (service != nullptr) {
1402 service->updateUidStates();
Mikhail Naganoveae73eb2018-04-03 16:57:36 -07001403 }
1404}
1405
Eric Laurente8c8b432018-10-17 10:08:02 -07001406void AudioPolicyService::UidPolicy::updateUid(std::unordered_map<uid_t,
1407 std::pair<bool, int>> *uids,
1408 uid_t uid,
1409 bool active,
1410 int state,
1411 bool insert) {
1412 if (isServiceUid(uid)) {
1413 return;
1414 }
1415 bool wasActive = isUidActive(uid);
1416 int previousState = getUidState(uid);
Mikhail Naganoveae73eb2018-04-03 16:57:36 -07001417 {
1418 Mutex::Autolock _l(mLock);
Eric Laurente8c8b432018-10-17 10:08:02 -07001419 updateUidLocked(uids, uid, active, state, insert);
Mikhail Naganoveae73eb2018-04-03 16:57:36 -07001420 }
Eric Laurente8c8b432018-10-17 10:08:02 -07001421 if (wasActive != isUidActive(uid) || state != previousState) {
1422 notifyService();
1423 }
Mikhail Naganoveae73eb2018-04-03 16:57:36 -07001424}
1425
Eric Laurente8c8b432018-10-17 10:08:02 -07001426void AudioPolicyService::UidPolicy::updateUidLocked(std::unordered_map<uid_t,
1427 std::pair<bool, int>> *uids,
1428 uid_t uid,
1429 bool active,
1430 int state,
1431 bool insert) {
Mikhail Naganoveae73eb2018-04-03 16:57:36 -07001432 auto it = uids->find(uid);
1433 if (it != uids->end()) {
Mikhail Naganoveae73eb2018-04-03 16:57:36 -07001434 if (insert) {
Eric Laurente8c8b432018-10-17 10:08:02 -07001435 if (state == ActivityManager::PROCESS_STATE_UNKNOWN) {
1436 it->second.first = active;
1437 }
1438 if (it->second.first) {
1439 it->second.second = state;
1440 } else {
1441 it->second.second = ActivityManager::PROCESS_STATE_UNKNOWN;
1442 }
Mikhail Naganoveae73eb2018-04-03 16:57:36 -07001443 } else {
1444 uids->erase(it);
1445 }
Eric Laurente8c8b432018-10-17 10:08:02 -07001446 } else if (insert && (state == ActivityManager::PROCESS_STATE_UNKNOWN)) {
1447 uids->insert(std::pair<uid_t, std::pair<bool, int>>(uid,
1448 std::pair<bool, int>(active, state)));
Mikhail Naganoveae73eb2018-04-03 16:57:36 -07001449 }
Svet Ganovf4ddfef2018-01-16 07:37:58 -08001450}
Mathias Agopian65ab4712010-07-14 17:59:35 -07001451
Eric Laurent4eb58f12018-12-07 16:41:02 -08001452bool AudioPolicyService::UidPolicy::isA11yOnTop() {
1453 for (const auto &uid : mCachedUids) {
Eric Laurent47670c92019-08-28 16:59:05 -07001454 if (!isA11yUid(uid.first)) {
Eric Laurent4eb58f12018-12-07 16:41:02 -08001455 continue;
1456 }
Amith Yamasanibcbb3002019-01-23 13:53:33 -08001457 if (uid.second.second >= ActivityManager::PROCESS_STATE_TOP
1458 && uid.second.second <= ActivityManager::PROCESS_STATE_BOUND_FOREGROUND_SERVICE) {
Eric Laurent4eb58f12018-12-07 16:41:02 -08001459 return true;
1460 }
1461 }
1462 return false;
1463}
1464
Eric Laurentb78763e2018-10-17 10:08:02 -07001465bool AudioPolicyService::UidPolicy::isA11yUid(uid_t uid)
1466{
1467 std::vector<uid_t>::iterator it = find(mA11yUids.begin(), mA11yUids.end(), uid);
1468 return it != mA11yUids.end();
1469}
1470
Michael Groovercfd28302018-12-11 19:16:46 -08001471// ----------- AudioPolicyService::SensorPrivacyService implementation ----------
1472void AudioPolicyService::SensorPrivacyPolicy::registerSelf() {
1473 SensorPrivacyManager spm;
1474 mSensorPrivacyEnabled = spm.isSensorPrivacyEnabled();
1475 spm.addSensorPrivacyListener(this);
1476}
1477
Evan Severson241d9592021-01-08 12:16:02 -08001478void AudioPolicyService::SensorPrivacyPolicy::registerSelfForMicrophoneOnly(int userId) {
1479 SensorPrivacyManager spm;
1480 mSensorPrivacyEnabled = spm.isIndividualSensorPrivacyEnabled(userId,
1481 SensorPrivacyManager::INDIVIDUAL_SENSOR_MICROPHONE);
1482 spm.addIndividualSensorPrivacyListener(userId,
1483 SensorPrivacyManager::INDIVIDUAL_SENSOR_MICROPHONE, this);
1484}
1485
Michael Groovercfd28302018-12-11 19:16:46 -08001486void AudioPolicyService::SensorPrivacyPolicy::unregisterSelf() {
1487 SensorPrivacyManager spm;
1488 spm.removeSensorPrivacyListener(this);
1489}
1490
1491bool AudioPolicyService::SensorPrivacyPolicy::isSensorPrivacyEnabled() {
1492 return mSensorPrivacyEnabled;
1493}
1494
1495binder::Status AudioPolicyService::SensorPrivacyPolicy::onSensorPrivacyChanged(bool enabled) {
1496 mSensorPrivacyEnabled = enabled;
1497 sp<AudioPolicyService> service = mService.promote();
1498 if (service != nullptr) {
1499 service->updateUidStates();
1500 }
1501 return binder::Status::ok();
1502}
1503
Eric Laurented726cc2021-07-01 14:26:41 +02001504// ----------- AudioPolicyService::OpRecordAudioMonitor implementation ----------
1505
1506// static
1507sp<AudioPolicyService::OpRecordAudioMonitor>
1508AudioPolicyService::OpRecordAudioMonitor::createIfNeeded(
1509 const AttributionSourceState& attributionSource, const audio_attributes_t& attr,
1510 wp<AudioCommandThread> commandThread)
1511{
Eric Laurent987ce102021-07-05 12:11:51 +02001512 if (isAudioServerOrRootUid(attributionSource.uid)) {
1513 ALOGV("not silencing record for audio or root source %s",
Eric Laurented726cc2021-07-01 14:26:41 +02001514 attributionSource.toString().c_str());
1515 return nullptr;
1516 }
1517
1518 if (!AudioPolicyService::isAppOpSource(attr.source)) {
1519 ALOGD("not monitoring app op for uid %d and source %d",
1520 attributionSource.uid, attr.source);
1521 return nullptr;
1522 }
1523
1524 if (!attributionSource.packageName.has_value()
1525 || attributionSource.packageName.value().size() == 0) {
1526 return nullptr;
1527 }
1528 return new OpRecordAudioMonitor(attributionSource, getOpForSource(attr.source), commandThread);
1529}
1530
1531AudioPolicyService::OpRecordAudioMonitor::OpRecordAudioMonitor(
1532 const AttributionSourceState& attributionSource, int32_t appOp,
1533 wp<AudioCommandThread> commandThread) :
1534 mHasOp(true), mAttributionSource(attributionSource), mAppOp(appOp),
1535 mCommandThread(commandThread)
1536{
1537}
1538
1539AudioPolicyService::OpRecordAudioMonitor::~OpRecordAudioMonitor()
1540{
1541 if (mOpCallback != 0) {
1542 mAppOpsManager.stopWatchingMode(mOpCallback);
1543 }
1544 mOpCallback.clear();
1545}
1546
1547void AudioPolicyService::OpRecordAudioMonitor::onFirstRef()
1548{
1549 checkOp();
1550 mOpCallback = new RecordAudioOpCallback(this);
1551 ALOGV("start watching op %d for %s", mAppOp, mAttributionSource.toString().c_str());
1552 // TODO: We need to always watch AppOpsManager::OP_RECORD_AUDIO too
1553 // since it controls the mic permission for legacy apps.
1554 mAppOpsManager.startWatchingMode(mAppOp, VALUE_OR_FATAL(aidl2legacy_string_view_String16(
1555 mAttributionSource.packageName.value_or(""))),
1556 mOpCallback);
1557}
1558
1559bool AudioPolicyService::OpRecordAudioMonitor::hasOp() const {
1560 return mHasOp.load();
1561}
1562
1563// Called by RecordAudioOpCallback when the app op corresponding to this OpRecordAudioMonitor
1564// is updated in AppOp callback and in onFirstRef()
1565// Note this method is never called (and never to be) for audio server / root track
1566// due to the UID in createIfNeeded(). As a result for those record track, it's:
1567// - not called from constructor,
1568// - not called from RecordAudioOpCallback because the callback is not installed in this case
1569void AudioPolicyService::OpRecordAudioMonitor::checkOp(bool updateUidStates)
1570{
1571 // TODO: We need to always check AppOpsManager::OP_RECORD_AUDIO too
1572 // since it controls the mic permission for legacy apps.
1573 const int32_t mode = mAppOpsManager.checkOp(mAppOp,
1574 mAttributionSource.uid, VALUE_OR_FATAL(aidl2legacy_string_view_String16(
1575 mAttributionSource.packageName.value_or(""))));
1576 const bool hasIt = (mode == AppOpsManager::MODE_ALLOWED);
1577 // verbose logging only log when appOp changed
1578 ALOGI_IF(hasIt != mHasOp.load(),
1579 "App op %d missing, %ssilencing record %s",
1580 mAppOp, hasIt ? "un" : "", mAttributionSource.toString().c_str());
1581 mHasOp.store(hasIt);
1582
1583 if (updateUidStates) {
1584 sp<AudioCommandThread> commandThread = mCommandThread.promote();
1585 if (commandThread != nullptr) {
1586 commandThread->updateUidStatesCommand();
1587 }
1588 }
1589}
1590
1591AudioPolicyService::OpRecordAudioMonitor::RecordAudioOpCallback::RecordAudioOpCallback(
1592 const wp<OpRecordAudioMonitor>& monitor) : mMonitor(monitor)
1593{ }
1594
1595void AudioPolicyService::OpRecordAudioMonitor::RecordAudioOpCallback::opChanged(int32_t op,
1596 const String16& packageName __unused) {
1597 sp<OpRecordAudioMonitor> monitor = mMonitor.promote();
1598 if (monitor != NULL) {
1599 if (op != monitor->getOp()) {
1600 return;
1601 }
1602 monitor->checkOp(true);
1603 }
1604}
1605
1606
Mathias Agopian65ab4712010-07-14 17:59:35 -07001607// ----------- AudioPolicyService::AudioCommandThread implementation ----------
1608
Eric Laurentbfb1b832013-01-07 09:53:42 -08001609AudioPolicyService::AudioCommandThread::AudioCommandThread(String8 name,
1610 const wp<AudioPolicyService>& service)
1611 : Thread(false), mName(name), mService(service)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001612{
Mathias Agopian65ab4712010-07-14 17:59:35 -07001613}
1614
1615
1616AudioPolicyService::AudioCommandThread::~AudioCommandThread()
1617{
Eric Laurentbfb1b832013-01-07 09:53:42 -08001618 if (!mAudioCommands.isEmpty()) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07001619 release_wake_lock(mName.string());
1620 }
1621 mAudioCommands.clear();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001622}
1623
1624void AudioPolicyService::AudioCommandThread::onFirstRef()
1625{
Eric Laurentbfb1b832013-01-07 09:53:42 -08001626 run(mName.string(), ANDROID_PRIORITY_AUDIO);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001627}
1628
1629bool AudioPolicyService::AudioCommandThread::threadLoop()
1630{
Eric Laurentd7eda8d2016-02-02 17:18:39 -08001631 nsecs_t waitTime = -1;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001632
1633 mLock.lock();
1634 while (!exitPending())
1635 {
Eric Laurent59a89232014-06-08 14:14:17 -07001636 sp<AudioPolicyService> svc;
1637 while (!mAudioCommands.isEmpty() && !exitPending()) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07001638 nsecs_t curTime = systemTime();
1639 // commands are sorted by increasing time stamp: execute them from index 0 and up
1640 if (mAudioCommands[0]->mTime <= curTime) {
Eric Laurent0ede8922014-05-09 18:04:42 -07001641 sp<AudioCommand> command = mAudioCommands[0];
Mathias Agopian65ab4712010-07-14 17:59:35 -07001642 mAudioCommands.removeAt(0);
Eric Laurent0ede8922014-05-09 18:04:42 -07001643 mLastCommand = command;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001644
1645 switch (command->mCommand) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07001646 case SET_VOLUME: {
Eric Laurent0ede8922014-05-09 18:04:42 -07001647 VolumeData *data = (VolumeData *)command->mParam.get();
Steve Block3856b092011-10-20 11:56:00 +01001648 ALOGV("AudioCommandThread() processing set volume stream %d, \
Eric Laurentde070132010-07-13 04:45:46 -07001649 volume %f, output %d", data->mStream, data->mVolume, data->mIO);
Andy Hungfe726a62018-09-27 15:17:25 -07001650 mLock.unlock();
Eric Laurentde070132010-07-13 04:45:46 -07001651 command->mStatus = AudioSystem::setStreamVolume(data->mStream,
1652 data->mVolume,
1653 data->mIO);
Andy Hungfe726a62018-09-27 15:17:25 -07001654 mLock.lock();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001655 }break;
1656 case SET_PARAMETERS: {
Eric Laurent0ede8922014-05-09 18:04:42 -07001657 ParametersData *data = (ParametersData *)command->mParam.get();
Glenn Kastene53b9ea2012-03-12 16:29:55 -07001658 ALOGV("AudioCommandThread() processing set parameters string %s, io %d",
1659 data->mKeyValuePairs.string(), data->mIO);
Andy Hungfe726a62018-09-27 15:17:25 -07001660 mLock.unlock();
Glenn Kastene53b9ea2012-03-12 16:29:55 -07001661 command->mStatus = AudioSystem::setParameters(data->mIO, data->mKeyValuePairs);
Andy Hungfe726a62018-09-27 15:17:25 -07001662 mLock.lock();
Glenn Kastene53b9ea2012-03-12 16:29:55 -07001663 }break;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001664 case SET_VOICE_VOLUME: {
Eric Laurent0ede8922014-05-09 18:04:42 -07001665 VoiceVolumeData *data = (VoiceVolumeData *)command->mParam.get();
Steve Block3856b092011-10-20 11:56:00 +01001666 ALOGV("AudioCommandThread() processing set voice volume volume %f",
Eric Laurentde070132010-07-13 04:45:46 -07001667 data->mVolume);
Andy Hungfe726a62018-09-27 15:17:25 -07001668 mLock.unlock();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001669 command->mStatus = AudioSystem::setVoiceVolume(data->mVolume);
Andy Hungfe726a62018-09-27 15:17:25 -07001670 mLock.lock();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001671 }break;
Eric Laurentbfb1b832013-01-07 09:53:42 -08001672 case STOP_OUTPUT: {
Eric Laurent0ede8922014-05-09 18:04:42 -07001673 StopOutputData *data = (StopOutputData *)command->mParam.get();
Eric Laurentd7fe0862018-07-14 16:48:01 -07001674 ALOGV("AudioCommandThread() processing stop output portId %d",
1675 data->mPortId);
Eric Laurent59a89232014-06-08 14:14:17 -07001676 svc = mService.promote();
Eric Laurentbfb1b832013-01-07 09:53:42 -08001677 if (svc == 0) {
1678 break;
1679 }
1680 mLock.unlock();
Eric Laurentd7fe0862018-07-14 16:48:01 -07001681 svc->doStopOutput(data->mPortId);
Eric Laurentbfb1b832013-01-07 09:53:42 -08001682 mLock.lock();
Eric Laurentbfb1b832013-01-07 09:53:42 -08001683 }break;
1684 case RELEASE_OUTPUT: {
Eric Laurent0ede8922014-05-09 18:04:42 -07001685 ReleaseOutputData *data = (ReleaseOutputData *)command->mParam.get();
Eric Laurentd7fe0862018-07-14 16:48:01 -07001686 ALOGV("AudioCommandThread() processing release output portId %d",
1687 data->mPortId);
Eric Laurent59a89232014-06-08 14:14:17 -07001688 svc = mService.promote();
Eric Laurentbfb1b832013-01-07 09:53:42 -08001689 if (svc == 0) {
1690 break;
1691 }
1692 mLock.unlock();
Eric Laurentd7fe0862018-07-14 16:48:01 -07001693 svc->doReleaseOutput(data->mPortId);
Eric Laurentbfb1b832013-01-07 09:53:42 -08001694 mLock.lock();
Eric Laurentbfb1b832013-01-07 09:53:42 -08001695 }break;
Eric Laurent951f4552014-05-20 10:48:17 -07001696 case CREATE_AUDIO_PATCH: {
1697 CreateAudioPatchData *data = (CreateAudioPatchData *)command->mParam.get();
1698 ALOGV("AudioCommandThread() processing create audio patch");
1699 sp<IAudioFlinger> af = AudioSystem::get_audio_flinger();
1700 if (af == 0) {
1701 command->mStatus = PERMISSION_DENIED;
1702 } else {
Andy Hungfe726a62018-09-27 15:17:25 -07001703 mLock.unlock();
Eric Laurent951f4552014-05-20 10:48:17 -07001704 command->mStatus = af->createAudioPatch(&data->mPatch, &data->mHandle);
Andy Hungfe726a62018-09-27 15:17:25 -07001705 mLock.lock();
Eric Laurent951f4552014-05-20 10:48:17 -07001706 }
1707 } break;
1708 case RELEASE_AUDIO_PATCH: {
1709 ReleaseAudioPatchData *data = (ReleaseAudioPatchData *)command->mParam.get();
1710 ALOGV("AudioCommandThread() processing release audio patch");
1711 sp<IAudioFlinger> af = AudioSystem::get_audio_flinger();
1712 if (af == 0) {
1713 command->mStatus = PERMISSION_DENIED;
1714 } else {
Andy Hungfe726a62018-09-27 15:17:25 -07001715 mLock.unlock();
Eric Laurent951f4552014-05-20 10:48:17 -07001716 command->mStatus = af->releaseAudioPatch(data->mHandle);
Andy Hungfe726a62018-09-27 15:17:25 -07001717 mLock.lock();
Eric Laurent951f4552014-05-20 10:48:17 -07001718 }
1719 } break;
Eric Laurentb52c1522014-05-20 11:27:36 -07001720 case UPDATE_AUDIOPORT_LIST: {
1721 ALOGV("AudioCommandThread() processing update audio port list");
Eric Laurent59a89232014-06-08 14:14:17 -07001722 svc = mService.promote();
Eric Laurentb52c1522014-05-20 11:27:36 -07001723 if (svc == 0) {
1724 break;
1725 }
1726 mLock.unlock();
1727 svc->doOnAudioPortListUpdate();
1728 mLock.lock();
1729 }break;
1730 case UPDATE_AUDIOPATCH_LIST: {
1731 ALOGV("AudioCommandThread() processing update audio patch list");
Eric Laurent59a89232014-06-08 14:14:17 -07001732 svc = mService.promote();
Eric Laurentb52c1522014-05-20 11:27:36 -07001733 if (svc == 0) {
1734 break;
1735 }
1736 mLock.unlock();
1737 svc->doOnAudioPatchListUpdate();
1738 mLock.lock();
1739 }break;
François Gaffiecfe17322018-11-07 13:41:29 +01001740 case CHANGED_AUDIOVOLUMEGROUP: {
1741 AudioVolumeGroupData *data =
1742 static_cast<AudioVolumeGroupData *>(command->mParam.get());
1743 ALOGV("AudioCommandThread() processing update audio volume group");
1744 svc = mService.promote();
1745 if (svc == 0) {
1746 break;
1747 }
1748 mLock.unlock();
1749 svc->doOnAudioVolumeGroupChanged(data->mGroup, data->mFlags);
1750 mLock.lock();
1751 }break;
Eric Laurente1715a42014-05-20 11:30:42 -07001752 case SET_AUDIOPORT_CONFIG: {
1753 SetAudioPortConfigData *data = (SetAudioPortConfigData *)command->mParam.get();
1754 ALOGV("AudioCommandThread() processing set port config");
1755 sp<IAudioFlinger> af = AudioSystem::get_audio_flinger();
1756 if (af == 0) {
1757 command->mStatus = PERMISSION_DENIED;
1758 } else {
Andy Hungfe726a62018-09-27 15:17:25 -07001759 mLock.unlock();
Eric Laurente1715a42014-05-20 11:30:42 -07001760 command->mStatus = af->setAudioPortConfig(&data->mConfig);
Andy Hungfe726a62018-09-27 15:17:25 -07001761 mLock.lock();
Eric Laurente1715a42014-05-20 11:30:42 -07001762 }
1763 } break;
Jean-Michel Trivide801052015-04-14 19:10:14 -07001764 case DYN_POLICY_MIX_STATE_UPDATE: {
1765 DynPolicyMixStateUpdateData *data =
1766 (DynPolicyMixStateUpdateData *)command->mParam.get();
Jean-Michel Trivide801052015-04-14 19:10:14 -07001767 ALOGV("AudioCommandThread() processing dyn policy mix state update %s %d",
1768 data->mRegId.string(), data->mState);
1769 svc = mService.promote();
1770 if (svc == 0) {
1771 break;
1772 }
1773 mLock.unlock();
1774 svc->doOnDynamicPolicyMixStateUpdate(data->mRegId, data->mState);
1775 mLock.lock();
1776 } break;
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -08001777 case RECORDING_CONFIGURATION_UPDATE: {
1778 RecordingConfigurationUpdateData *data =
1779 (RecordingConfigurationUpdateData *)command->mParam.get();
1780 ALOGV("AudioCommandThread() processing recording configuration update");
1781 svc = mService.promote();
1782 if (svc == 0) {
1783 break;
1784 }
1785 mLock.unlock();
Jean-Michel Triviac4e4292016-12-22 11:39:31 -08001786 svc->doOnRecordingConfigurationUpdate(data->mEvent, &data->mClientInfo,
Eric Laurenta9f86652018-11-28 17:23:11 -08001787 &data->mClientConfig, data->mClientEffects,
1788 &data->mDeviceConfig, data->mEffects,
1789 data->mPatchHandle, data->mSource);
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -08001790 mLock.lock();
1791 } break;
Eric Laurentb20cf7d2019-04-05 19:37:34 -07001792 case SET_EFFECT_SUSPENDED: {
1793 SetEffectSuspendedData *data = (SetEffectSuspendedData *)command->mParam.get();
1794 ALOGV("AudioCommandThread() processing set effect suspended");
1795 sp<IAudioFlinger> af = AudioSystem::get_audio_flinger();
1796 if (af != 0) {
1797 mLock.unlock();
1798 af->setEffectSuspended(data->mEffectId, data->mSessionId, data->mSuspended);
1799 mLock.lock();
1800 }
1801 } break;
Mikhail Naganov88b30d22020-03-09 19:43:13 +00001802 case AUDIO_MODULES_UPDATE: {
1803 ALOGV("AudioCommandThread() processing audio modules update");
1804 svc = mService.promote();
1805 if (svc == 0) {
1806 break;
1807 }
1808 mLock.unlock();
1809 svc->doOnNewAudioModulesAvailable();
1810 mLock.lock();
1811 } break;
Jean-Michel Trivi9a6b9ad2020-10-22 16:46:43 -07001812 case ROUTING_UPDATED: {
1813 ALOGV("AudioCommandThread() processing routing update");
1814 svc = mService.promote();
1815 if (svc == 0) {
1816 break;
1817 }
1818 mLock.unlock();
1819 svc->doOnRoutingUpdated();
1820 mLock.lock();
1821 } break;
Eric Laurentb20cf7d2019-04-05 19:37:34 -07001822
Eric Laurented726cc2021-07-01 14:26:41 +02001823 case UPDATE_UID_STATES: {
1824 ALOGV("AudioCommandThread() processing updateUID states");
1825 svc = mService.promote();
1826 if (svc == 0) {
1827 break;
1828 }
1829 mLock.unlock();
1830 svc->updateUidStates();
1831 mLock.lock();
1832 } break;
1833
Eric Laurent81dd0f52021-07-05 11:54:40 +02001834 case CHECK_SPATIALIZER: {
1835 ALOGV("AudioCommandThread() processing updateUID states");
1836 svc = mService.promote();
1837 if (svc == 0) {
1838 break;
1839 }
1840 mLock.unlock();
1841 svc->doOnCheckSpatializer();
1842 mLock.lock();
1843 } break;
1844
Mathias Agopian65ab4712010-07-14 17:59:35 -07001845 default:
Steve Block5ff1dd52012-01-05 23:22:43 +00001846 ALOGW("AudioCommandThread() unknown command %d", command->mCommand);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001847 }
Eric Laurent0ede8922014-05-09 18:04:42 -07001848 {
1849 Mutex::Autolock _l(command->mLock);
1850 if (command->mWaitStatus) {
1851 command->mWaitStatus = false;
1852 command->mCond.signal();
1853 }
1854 }
Eric Laurentd7eda8d2016-02-02 17:18:39 -08001855 waitTime = -1;
Zach Janga754b4f2015-10-27 01:29:34 +00001856 // release mLock before releasing strong reference on the service as
1857 // AudioPolicyService destructor calls AudioCommandThread::exit() which
1858 // acquires mLock.
1859 mLock.unlock();
1860 svc.clear();
1861 mLock.lock();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001862 } else {
1863 waitTime = mAudioCommands[0]->mTime - curTime;
1864 break;
1865 }
1866 }
Zach Janga754b4f2015-10-27 01:29:34 +00001867
1868 // release delayed commands wake lock if the queue is empty
1869 if (mAudioCommands.isEmpty()) {
Ricardo Garcia05f2fdc2014-07-24 15:48:24 -07001870 release_wake_lock(mName.string());
Zach Janga754b4f2015-10-27 01:29:34 +00001871 }
1872
1873 // At this stage we have either an empty command queue or the first command in the queue
1874 // has a finite delay. So unless we are exiting it is safe to wait.
1875 if (!exitPending()) {
Eric Laurent59a89232014-06-08 14:14:17 -07001876 ALOGV("AudioCommandThread() going to sleep");
Eric Laurentd7eda8d2016-02-02 17:18:39 -08001877 if (waitTime == -1) {
1878 mWaitWorkCV.wait(mLock);
1879 } else {
1880 mWaitWorkCV.waitRelative(mLock, waitTime);
1881 }
Eric Laurent59a89232014-06-08 14:14:17 -07001882 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001883 }
Ricardo Garcia05f2fdc2014-07-24 15:48:24 -07001884 // release delayed commands wake lock before quitting
1885 if (!mAudioCommands.isEmpty()) {
1886 release_wake_lock(mName.string());
1887 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001888 mLock.unlock();
1889 return false;
1890}
1891
1892status_t AudioPolicyService::AudioCommandThread::dump(int fd)
1893{
1894 const size_t SIZE = 256;
1895 char buffer[SIZE];
1896 String8 result;
1897
1898 snprintf(buffer, SIZE, "AudioCommandThread %p Dump\n", this);
1899 result.append(buffer);
1900 write(fd, result.string(), result.size());
1901
Mikhail Naganov12b716c2020-04-30 22:37:43 +00001902 const bool locked = dumpTryLock(mLock);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001903 if (!locked) {
1904 String8 result2(kCmdDeadlockedString);
1905 write(fd, result2.string(), result2.size());
1906 }
1907
1908 snprintf(buffer, SIZE, "- Commands:\n");
1909 result = String8(buffer);
1910 result.append(" Command Time Wait pParam\n");
Glenn Kasten8d6a2442012-02-08 14:04:28 -08001911 for (size_t i = 0; i < mAudioCommands.size(); i++) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07001912 mAudioCommands[i]->dump(buffer, SIZE);
1913 result.append(buffer);
1914 }
1915 result.append(" Last Command\n");
Eric Laurent0ede8922014-05-09 18:04:42 -07001916 if (mLastCommand != 0) {
1917 mLastCommand->dump(buffer, SIZE);
1918 result.append(buffer);
1919 } else {
1920 result.append(" none\n");
1921 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001922
1923 write(fd, result.string(), result.size());
1924
Mikhail Naganov12b716c2020-04-30 22:37:43 +00001925 dumpReleaseLock(mLock, locked);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001926
1927 return NO_ERROR;
1928}
1929
Glenn Kastenfff6d712012-01-12 16:38:12 -08001930status_t AudioPolicyService::AudioCommandThread::volumeCommand(audio_stream_type_t stream,
Eric Laurentde070132010-07-13 04:45:46 -07001931 float volume,
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001932 audio_io_handle_t output,
Eric Laurentde070132010-07-13 04:45:46 -07001933 int delayMs)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001934{
Eric Laurent0ede8922014-05-09 18:04:42 -07001935 sp<AudioCommand> command = new AudioCommand();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001936 command->mCommand = SET_VOLUME;
Eric Laurent0ede8922014-05-09 18:04:42 -07001937 sp<VolumeData> data = new VolumeData();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001938 data->mStream = stream;
1939 data->mVolume = volume;
1940 data->mIO = output;
1941 command->mParam = data;
Eric Laurent0ede8922014-05-09 18:04:42 -07001942 command->mWaitStatus = true;
Steve Block3856b092011-10-20 11:56:00 +01001943 ALOGV("AudioCommandThread() adding set volume stream %d, volume %f, output %d",
Eric Laurentde070132010-07-13 04:45:46 -07001944 stream, volume, output);
Eric Laurent0ede8922014-05-09 18:04:42 -07001945 return sendCommand(command, delayMs);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001946}
1947
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001948status_t AudioPolicyService::AudioCommandThread::parametersCommand(audio_io_handle_t ioHandle,
Dima Zavinfce7a472011-04-19 22:30:36 -07001949 const char *keyValuePairs,
Eric Laurentde070132010-07-13 04:45:46 -07001950 int delayMs)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001951{
Eric Laurent0ede8922014-05-09 18:04:42 -07001952 sp<AudioCommand> command = new AudioCommand();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001953 command->mCommand = SET_PARAMETERS;
Eric Laurent0ede8922014-05-09 18:04:42 -07001954 sp<ParametersData> data = new ParametersData();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001955 data->mIO = ioHandle;
Dima Zavinfce7a472011-04-19 22:30:36 -07001956 data->mKeyValuePairs = String8(keyValuePairs);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001957 command->mParam = data;
Eric Laurent0ede8922014-05-09 18:04:42 -07001958 command->mWaitStatus = true;
Steve Block3856b092011-10-20 11:56:00 +01001959 ALOGV("AudioCommandThread() adding set parameter string %s, io %d ,delay %d",
Dima Zavinfce7a472011-04-19 22:30:36 -07001960 keyValuePairs, ioHandle, delayMs);
Eric Laurent0ede8922014-05-09 18:04:42 -07001961 return sendCommand(command, delayMs);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001962}
1963
1964status_t AudioPolicyService::AudioCommandThread::voiceVolumeCommand(float volume, int delayMs)
1965{
Eric Laurent0ede8922014-05-09 18:04:42 -07001966 sp<AudioCommand> command = new AudioCommand();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001967 command->mCommand = SET_VOICE_VOLUME;
Eric Laurent0ede8922014-05-09 18:04:42 -07001968 sp<VoiceVolumeData> data = new VoiceVolumeData();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001969 data->mVolume = volume;
1970 command->mParam = data;
Eric Laurent0ede8922014-05-09 18:04:42 -07001971 command->mWaitStatus = true;
Steve Block3856b092011-10-20 11:56:00 +01001972 ALOGV("AudioCommandThread() adding set voice volume volume %f", volume);
Eric Laurent0ede8922014-05-09 18:04:42 -07001973 return sendCommand(command, delayMs);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001974}
1975
Eric Laurentb20cf7d2019-04-05 19:37:34 -07001976void AudioPolicyService::AudioCommandThread::setEffectSuspendedCommand(int effectId,
1977 audio_session_t sessionId,
1978 bool suspended)
1979{
1980 sp<AudioCommand> command = new AudioCommand();
1981 command->mCommand = SET_EFFECT_SUSPENDED;
1982 sp<SetEffectSuspendedData> data = new SetEffectSuspendedData();
1983 data->mEffectId = effectId;
1984 data->mSessionId = sessionId;
1985 data->mSuspended = suspended;
1986 command->mParam = data;
1987 ALOGV("AudioCommandThread() adding set suspended effectId %d sessionId %d suspended %d",
1988 effectId, sessionId, suspended);
1989 sendCommand(command);
1990}
1991
1992
Eric Laurentd7fe0862018-07-14 16:48:01 -07001993void AudioPolicyService::AudioCommandThread::stopOutputCommand(audio_port_handle_t portId)
Eric Laurentbfb1b832013-01-07 09:53:42 -08001994{
Eric Laurent0ede8922014-05-09 18:04:42 -07001995 sp<AudioCommand> command = new AudioCommand();
Eric Laurentbfb1b832013-01-07 09:53:42 -08001996 command->mCommand = STOP_OUTPUT;
Eric Laurent0ede8922014-05-09 18:04:42 -07001997 sp<StopOutputData> data = new StopOutputData();
Eric Laurentd7fe0862018-07-14 16:48:01 -07001998 data->mPortId = portId;
Jesper Tragardh48412dc2014-03-24 14:12:43 +01001999 command->mParam = data;
Eric Laurentd7fe0862018-07-14 16:48:01 -07002000 ALOGV("AudioCommandThread() adding stop output portId %d", portId);
Eric Laurent0ede8922014-05-09 18:04:42 -07002001 sendCommand(command);
Eric Laurentbfb1b832013-01-07 09:53:42 -08002002}
2003
Eric Laurentd7fe0862018-07-14 16:48:01 -07002004void AudioPolicyService::AudioCommandThread::releaseOutputCommand(audio_port_handle_t portId)
Eric Laurentbfb1b832013-01-07 09:53:42 -08002005{
Eric Laurent0ede8922014-05-09 18:04:42 -07002006 sp<AudioCommand> command = new AudioCommand();
Eric Laurentbfb1b832013-01-07 09:53:42 -08002007 command->mCommand = RELEASE_OUTPUT;
Eric Laurent0ede8922014-05-09 18:04:42 -07002008 sp<ReleaseOutputData> data = new ReleaseOutputData();
Eric Laurentd7fe0862018-07-14 16:48:01 -07002009 data->mPortId = portId;
Jesper Tragardh48412dc2014-03-24 14:12:43 +01002010 command->mParam = data;
Eric Laurentd7fe0862018-07-14 16:48:01 -07002011 ALOGV("AudioCommandThread() adding release output portId %d", portId);
Eric Laurent0ede8922014-05-09 18:04:42 -07002012 sendCommand(command);
2013}
2014
Eric Laurent951f4552014-05-20 10:48:17 -07002015status_t AudioPolicyService::AudioCommandThread::createAudioPatchCommand(
2016 const struct audio_patch *patch,
2017 audio_patch_handle_t *handle,
2018 int delayMs)
2019{
2020 status_t status = NO_ERROR;
2021
2022 sp<AudioCommand> command = new AudioCommand();
2023 command->mCommand = CREATE_AUDIO_PATCH;
2024 CreateAudioPatchData *data = new CreateAudioPatchData();
2025 data->mPatch = *patch;
2026 data->mHandle = *handle;
2027 command->mParam = data;
2028 command->mWaitStatus = true;
2029 ALOGV("AudioCommandThread() adding create patch delay %d", delayMs);
2030 status = sendCommand(command, delayMs);
2031 if (status == NO_ERROR) {
2032 *handle = data->mHandle;
2033 }
2034 return status;
2035}
2036
2037status_t AudioPolicyService::AudioCommandThread::releaseAudioPatchCommand(audio_patch_handle_t handle,
2038 int delayMs)
2039{
2040 sp<AudioCommand> command = new AudioCommand();
2041 command->mCommand = RELEASE_AUDIO_PATCH;
2042 ReleaseAudioPatchData *data = new ReleaseAudioPatchData();
2043 data->mHandle = handle;
2044 command->mParam = data;
2045 command->mWaitStatus = true;
2046 ALOGV("AudioCommandThread() adding release patch delay %d", delayMs);
2047 return sendCommand(command, delayMs);
2048}
2049
Eric Laurentb52c1522014-05-20 11:27:36 -07002050void AudioPolicyService::AudioCommandThread::updateAudioPortListCommand()
2051{
2052 sp<AudioCommand> command = new AudioCommand();
2053 command->mCommand = UPDATE_AUDIOPORT_LIST;
2054 ALOGV("AudioCommandThread() adding update audio port list");
2055 sendCommand(command);
2056}
2057
Eric Laurented726cc2021-07-01 14:26:41 +02002058void AudioPolicyService::AudioCommandThread::updateUidStatesCommand()
2059{
2060 sp<AudioCommand> command = new AudioCommand();
2061 command->mCommand = UPDATE_UID_STATES;
2062 ALOGV("AudioCommandThread() adding update UID states");
2063 sendCommand(command);
2064}
2065
Eric Laurentb52c1522014-05-20 11:27:36 -07002066void AudioPolicyService::AudioCommandThread::updateAudioPatchListCommand()
2067{
2068 sp<AudioCommand>command = new AudioCommand();
2069 command->mCommand = UPDATE_AUDIOPATCH_LIST;
2070 ALOGV("AudioCommandThread() adding update audio patch list");
2071 sendCommand(command);
2072}
2073
François Gaffiecfe17322018-11-07 13:41:29 +01002074void AudioPolicyService::AudioCommandThread::changeAudioVolumeGroupCommand(volume_group_t group,
2075 int flags)
2076{
2077 sp<AudioCommand>command = new AudioCommand();
2078 command->mCommand = CHANGED_AUDIOVOLUMEGROUP;
2079 AudioVolumeGroupData *data= new AudioVolumeGroupData();
2080 data->mGroup = group;
2081 data->mFlags = flags;
2082 command->mParam = data;
2083 ALOGV("AudioCommandThread() adding audio volume group changed");
2084 sendCommand(command);
2085}
2086
Eric Laurente1715a42014-05-20 11:30:42 -07002087status_t AudioPolicyService::AudioCommandThread::setAudioPortConfigCommand(
2088 const struct audio_port_config *config, int delayMs)
2089{
2090 sp<AudioCommand> command = new AudioCommand();
2091 command->mCommand = SET_AUDIOPORT_CONFIG;
2092 SetAudioPortConfigData *data = new SetAudioPortConfigData();
2093 data->mConfig = *config;
2094 command->mParam = data;
2095 command->mWaitStatus = true;
2096 ALOGV("AudioCommandThread() adding set port config delay %d", delayMs);
2097 return sendCommand(command, delayMs);
2098}
2099
Jean-Michel Trivide801052015-04-14 19:10:14 -07002100void AudioPolicyService::AudioCommandThread::dynamicPolicyMixStateUpdateCommand(
Chih-Hung Hsiehe964d4e2016-08-09 14:31:32 -07002101 const String8& regId, int32_t state)
Jean-Michel Trivide801052015-04-14 19:10:14 -07002102{
2103 sp<AudioCommand> command = new AudioCommand();
2104 command->mCommand = DYN_POLICY_MIX_STATE_UPDATE;
2105 DynPolicyMixStateUpdateData *data = new DynPolicyMixStateUpdateData();
2106 data->mRegId = regId;
2107 data->mState = state;
2108 command->mParam = data;
2109 ALOGV("AudioCommandThread() sending dynamic policy mix (id=%s) state update to %d",
2110 regId.string(), state);
2111 sendCommand(command);
2112}
2113
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -08002114void AudioPolicyService::AudioCommandThread::recordingConfigurationUpdateCommand(
Eric Laurenta9f86652018-11-28 17:23:11 -08002115 int event,
2116 const record_client_info_t *clientInfo,
2117 const audio_config_base_t *clientConfig,
2118 std::vector<effect_descriptor_t> clientEffects,
2119 const audio_config_base_t *deviceConfig,
2120 std::vector<effect_descriptor_t> effects,
2121 audio_patch_handle_t patchHandle,
2122 audio_source_t source)
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -08002123{
2124 sp<AudioCommand>command = new AudioCommand();
2125 command->mCommand = RECORDING_CONFIGURATION_UPDATE;
2126 RecordingConfigurationUpdateData *data = new RecordingConfigurationUpdateData();
2127 data->mEvent = event;
Jean-Michel Triviac4e4292016-12-22 11:39:31 -08002128 data->mClientInfo = *clientInfo;
Jean-Michel Trivi7281aa92016-02-17 15:33:40 -08002129 data->mClientConfig = *clientConfig;
Eric Laurenta9f86652018-11-28 17:23:11 -08002130 data->mClientEffects = clientEffects;
Jean-Michel Trivi7281aa92016-02-17 15:33:40 -08002131 data->mDeviceConfig = *deviceConfig;
Eric Laurenta9f86652018-11-28 17:23:11 -08002132 data->mEffects = effects;
Jean-Michel Trivi8c7cf3b2016-02-25 17:08:24 -08002133 data->mPatchHandle = patchHandle;
Eric Laurenta9f86652018-11-28 17:23:11 -08002134 data->mSource = source;
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -08002135 command->mParam = data;
Jean-Michel Triviac4e4292016-12-22 11:39:31 -08002136 ALOGV("AudioCommandThread() adding recording configuration update event %d, source %d uid %u",
2137 event, clientInfo->source, clientInfo->uid);
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -08002138 sendCommand(command);
2139}
2140
Mikhail Naganov88b30d22020-03-09 19:43:13 +00002141void AudioPolicyService::AudioCommandThread::audioModulesUpdateCommand()
2142{
2143 sp<AudioCommand> command = new AudioCommand();
2144 command->mCommand = AUDIO_MODULES_UPDATE;
2145 sendCommand(command);
2146}
2147
Jean-Michel Trivi9a6b9ad2020-10-22 16:46:43 -07002148void AudioPolicyService::AudioCommandThread::routingChangedCommand()
2149{
2150 sp<AudioCommand>command = new AudioCommand();
2151 command->mCommand = ROUTING_UPDATED;
2152 ALOGV("AudioCommandThread() adding routing update");
2153 sendCommand(command);
2154}
2155
Eric Laurent81dd0f52021-07-05 11:54:40 +02002156void AudioPolicyService::AudioCommandThread::checkSpatializerCommand()
2157{
2158 sp<AudioCommand>command = new AudioCommand();
2159 command->mCommand = CHECK_SPATIALIZER;
2160 ALOGV("AudioCommandThread() adding check spatializer");
2161 sendCommand(command);
2162}
2163
Eric Laurent0ede8922014-05-09 18:04:42 -07002164status_t AudioPolicyService::AudioCommandThread::sendCommand(sp<AudioCommand>& command, int delayMs)
2165{
2166 {
2167 Mutex::Autolock _l(mLock);
2168 insertCommand_l(command, delayMs);
2169 mWaitWorkCV.signal();
2170 }
2171 Mutex::Autolock _l(command->mLock);
2172 while (command->mWaitStatus) {
2173 nsecs_t timeOutNs = kAudioCommandTimeoutNs + milliseconds(delayMs);
2174 if (command->mCond.waitRelative(command->mLock, timeOutNs) != NO_ERROR) {
2175 command->mStatus = TIMED_OUT;
2176 command->mWaitStatus = false;
2177 }
2178 }
2179 return command->mStatus;
Eric Laurentbfb1b832013-01-07 09:53:42 -08002180}
2181
Mathias Agopian65ab4712010-07-14 17:59:35 -07002182// insertCommand_l() must be called with mLock held
Eric Laurent0ede8922014-05-09 18:04:42 -07002183void AudioPolicyService::AudioCommandThread::insertCommand_l(sp<AudioCommand>& command, int delayMs)
Mathias Agopian65ab4712010-07-14 17:59:35 -07002184{
Glenn Kasten8d6a2442012-02-08 14:04:28 -08002185 ssize_t i; // not size_t because i will count down to -1
Eric Laurent0ede8922014-05-09 18:04:42 -07002186 Vector < sp<AudioCommand> > removedCommands;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002187 command->mTime = systemTime() + milliseconds(delayMs);
2188
2189 // acquire wake lock to make sure delayed commands are processed
Eric Laurentbfb1b832013-01-07 09:53:42 -08002190 if (mAudioCommands.isEmpty()) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07002191 acquire_wake_lock(PARTIAL_WAKE_LOCK, mName.string());
2192 }
2193
2194 // check same pending commands with later time stamps and eliminate them
Ivan Lozano5ff158f2017-10-30 09:06:24 -07002195 for (i = (ssize_t)mAudioCommands.size()-1; i >= 0; i--) {
Eric Laurent0ede8922014-05-09 18:04:42 -07002196 sp<AudioCommand> command2 = mAudioCommands[i];
Mathias Agopian65ab4712010-07-14 17:59:35 -07002197 // commands are sorted by increasing time stamp: no need to scan the rest of mAudioCommands
2198 if (command2->mTime <= command->mTime) break;
Eric Laurente45b48a2014-09-04 16:40:57 -07002199
2200 // create audio patch or release audio patch commands are equivalent
2201 // with regard to filtering
2202 if ((command->mCommand == CREATE_AUDIO_PATCH) ||
2203 (command->mCommand == RELEASE_AUDIO_PATCH)) {
2204 if ((command2->mCommand != CREATE_AUDIO_PATCH) &&
2205 (command2->mCommand != RELEASE_AUDIO_PATCH)) {
2206 continue;
2207 }
2208 } else if (command2->mCommand != command->mCommand) continue;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002209
2210 switch (command->mCommand) {
2211 case SET_PARAMETERS: {
Eric Laurent0ede8922014-05-09 18:04:42 -07002212 ParametersData *data = (ParametersData *)command->mParam.get();
2213 ParametersData *data2 = (ParametersData *)command2->mParam.get();
Mathias Agopian65ab4712010-07-14 17:59:35 -07002214 if (data->mIO != data2->mIO) break;
Steve Block3856b092011-10-20 11:56:00 +01002215 ALOGV("Comparing parameter command %s to new command %s",
Eric Laurentde070132010-07-13 04:45:46 -07002216 data2->mKeyValuePairs.string(), data->mKeyValuePairs.string());
Mathias Agopian65ab4712010-07-14 17:59:35 -07002217 AudioParameter param = AudioParameter(data->mKeyValuePairs);
2218 AudioParameter param2 = AudioParameter(data2->mKeyValuePairs);
2219 for (size_t j = 0; j < param.size(); j++) {
Glenn Kastene53b9ea2012-03-12 16:29:55 -07002220 String8 key;
2221 String8 value;
2222 param.getAt(j, key, value);
2223 for (size_t k = 0; k < param2.size(); k++) {
2224 String8 key2;
2225 String8 value2;
2226 param2.getAt(k, key2, value2);
2227 if (key2 == key) {
2228 param2.remove(key2);
2229 ALOGV("Filtering out parameter %s", key2.string());
2230 break;
2231 }
2232 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07002233 }
2234 // if all keys have been filtered out, remove the command.
2235 // otherwise, update the key value pairs
2236 if (param2.size() == 0) {
2237 removedCommands.add(command2);
2238 } else {
2239 data2->mKeyValuePairs = param2.toString();
2240 }
Eric Laurent21e54562013-09-23 12:08:05 -07002241 command->mTime = command2->mTime;
2242 // force delayMs to non 0 so that code below does not request to wait for
2243 // command status as the command is now delayed
2244 delayMs = 1;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002245 } break;
2246
2247 case SET_VOLUME: {
Eric Laurent0ede8922014-05-09 18:04:42 -07002248 VolumeData *data = (VolumeData *)command->mParam.get();
2249 VolumeData *data2 = (VolumeData *)command2->mParam.get();
Mathias Agopian65ab4712010-07-14 17:59:35 -07002250 if (data->mIO != data2->mIO) break;
2251 if (data->mStream != data2->mStream) break;
Steve Block3856b092011-10-20 11:56:00 +01002252 ALOGV("Filtering out volume command on output %d for stream %d",
Eric Laurentde070132010-07-13 04:45:46 -07002253 data->mIO, data->mStream);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002254 removedCommands.add(command2);
Eric Laurent21e54562013-09-23 12:08:05 -07002255 command->mTime = command2->mTime;
2256 // force delayMs to non 0 so that code below does not request to wait for
2257 // command status as the command is now delayed
2258 delayMs = 1;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002259 } break;
Eric Laurente45b48a2014-09-04 16:40:57 -07002260
Eric Laurentbaf35fe2016-07-27 15:36:53 -07002261 case SET_VOICE_VOLUME: {
2262 VoiceVolumeData *data = (VoiceVolumeData *)command->mParam.get();
2263 VoiceVolumeData *data2 = (VoiceVolumeData *)command2->mParam.get();
2264 ALOGV("Filtering out voice volume command value %f replaced by %f",
2265 data2->mVolume, data->mVolume);
2266 removedCommands.add(command2);
2267 command->mTime = command2->mTime;
2268 // force delayMs to non 0 so that code below does not request to wait for
2269 // command status as the command is now delayed
2270 delayMs = 1;
2271 } break;
2272
Eric Laurente45b48a2014-09-04 16:40:57 -07002273 case CREATE_AUDIO_PATCH:
2274 case RELEASE_AUDIO_PATCH: {
2275 audio_patch_handle_t handle;
Haynes Mathew Georgea2d4a6d2014-10-13 13:05:22 -07002276 struct audio_patch patch;
Eric Laurente45b48a2014-09-04 16:40:57 -07002277 if (command->mCommand == CREATE_AUDIO_PATCH) {
2278 handle = ((CreateAudioPatchData *)command->mParam.get())->mHandle;
Haynes Mathew Georgea2d4a6d2014-10-13 13:05:22 -07002279 patch = ((CreateAudioPatchData *)command->mParam.get())->mPatch;
Eric Laurente45b48a2014-09-04 16:40:57 -07002280 } else {
2281 handle = ((ReleaseAudioPatchData *)command->mParam.get())->mHandle;
Mikhail Naganov7be71d22018-05-23 16:51:46 -07002282 memset(&patch, 0, sizeof(patch));
Eric Laurente45b48a2014-09-04 16:40:57 -07002283 }
2284 audio_patch_handle_t handle2;
Haynes Mathew Georgea2d4a6d2014-10-13 13:05:22 -07002285 struct audio_patch patch2;
Eric Laurente45b48a2014-09-04 16:40:57 -07002286 if (command2->mCommand == CREATE_AUDIO_PATCH) {
2287 handle2 = ((CreateAudioPatchData *)command2->mParam.get())->mHandle;
Haynes Mathew Georgea2d4a6d2014-10-13 13:05:22 -07002288 patch2 = ((CreateAudioPatchData *)command2->mParam.get())->mPatch;
Eric Laurente45b48a2014-09-04 16:40:57 -07002289 } else {
2290 handle2 = ((ReleaseAudioPatchData *)command2->mParam.get())->mHandle;
Glenn Kastenf60b6b62015-07-06 10:53:26 -07002291 memset(&patch2, 0, sizeof(patch2));
Eric Laurente45b48a2014-09-04 16:40:57 -07002292 }
2293 if (handle != handle2) break;
Haynes Mathew Georgea2d4a6d2014-10-13 13:05:22 -07002294 /* Filter CREATE_AUDIO_PATCH commands only when they are issued for
2295 same output. */
2296 if( (command->mCommand == CREATE_AUDIO_PATCH) &&
2297 (command2->mCommand == CREATE_AUDIO_PATCH) ) {
2298 bool isOutputDiff = false;
2299 if (patch.num_sources == patch2.num_sources) {
2300 for (unsigned count = 0; count < patch.num_sources; count++) {
2301 if (patch.sources[count].id != patch2.sources[count].id) {
2302 isOutputDiff = true;
2303 break;
2304 }
2305 }
2306 if (isOutputDiff)
2307 break;
2308 }
2309 }
Eric Laurente45b48a2014-09-04 16:40:57 -07002310 ALOGV("Filtering out %s audio patch command for handle %d",
2311 (command->mCommand == CREATE_AUDIO_PATCH) ? "create" : "release", handle);
2312 removedCommands.add(command2);
2313 command->mTime = command2->mTime;
2314 // force delayMs to non 0 so that code below does not request to wait for
2315 // command status as the command is now delayed
2316 delayMs = 1;
2317 } break;
2318
Jean-Michel Trivide801052015-04-14 19:10:14 -07002319 case DYN_POLICY_MIX_STATE_UPDATE: {
2320
2321 } break;
2322
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -08002323 case RECORDING_CONFIGURATION_UPDATE: {
2324
2325 } break;
2326
Jean-Michel Trivi9a6b9ad2020-10-22 16:46:43 -07002327 case ROUTING_UPDATED: {
2328
2329 } break;
2330
Mathias Agopian65ab4712010-07-14 17:59:35 -07002331 default:
2332 break;
2333 }
2334 }
2335
2336 // remove filtered commands
2337 for (size_t j = 0; j < removedCommands.size(); j++) {
2338 // removed commands always have time stamps greater than current command
2339 for (size_t k = i + 1; k < mAudioCommands.size(); k++) {
Eric Laurent0ede8922014-05-09 18:04:42 -07002340 if (mAudioCommands[k].get() == removedCommands[j].get()) {
Steve Block3856b092011-10-20 11:56:00 +01002341 ALOGV("suppressing command: %d", mAudioCommands[k]->mCommand);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002342 mAudioCommands.removeAt(k);
2343 break;
2344 }
2345 }
2346 }
2347 removedCommands.clear();
2348
Eric Laurentaa79bef2015-01-15 14:33:51 -08002349 // Disable wait for status if delay is not 0.
2350 // Except for create audio patch command because the returned patch handle
2351 // is needed by audio policy manager
2352 if (delayMs != 0 && command->mCommand != CREATE_AUDIO_PATCH) {
Eric Laurentcec4abb2012-07-03 12:23:02 -07002353 command->mWaitStatus = false;
2354 }
Eric Laurentcec4abb2012-07-03 12:23:02 -07002355
Mathias Agopian65ab4712010-07-14 17:59:35 -07002356 // insert command at the right place according to its time stamp
Eric Laurent1e693b52014-07-09 15:03:28 -07002357 ALOGV("inserting command: %d at index %zd, num commands %zu",
2358 command->mCommand, i+1, mAudioCommands.size());
Mathias Agopian65ab4712010-07-14 17:59:35 -07002359 mAudioCommands.insertAt(command, i + 1);
2360}
2361
2362void AudioPolicyService::AudioCommandThread::exit()
2363{
Steve Block3856b092011-10-20 11:56:00 +01002364 ALOGV("AudioCommandThread::exit");
Mathias Agopian65ab4712010-07-14 17:59:35 -07002365 {
2366 AutoMutex _l(mLock);
2367 requestExit();
2368 mWaitWorkCV.signal();
2369 }
Zach Janga754b4f2015-10-27 01:29:34 +00002370 // Note that we can call it from the thread loop if all other references have been released
2371 // but it will safely return WOULD_BLOCK in this case
Mathias Agopian65ab4712010-07-14 17:59:35 -07002372 requestExitAndWait();
2373}
2374
2375void AudioPolicyService::AudioCommandThread::AudioCommand::dump(char* buffer, size_t size)
2376{
2377 snprintf(buffer, size, " %02d %06d.%03d %01u %p\n",
2378 mCommand,
2379 (int)ns2s(mTime),
2380 (int)ns2ms(mTime)%1000,
2381 mWaitStatus,
Eric Laurent0ede8922014-05-09 18:04:42 -07002382 mParam.get());
Mathias Agopian65ab4712010-07-14 17:59:35 -07002383}
2384
Dima Zavinfce7a472011-04-19 22:30:36 -07002385/******* helpers for the service_ops callbacks defined below *********/
2386void AudioPolicyService::setParameters(audio_io_handle_t ioHandle,
2387 const char *keyValuePairs,
2388 int delayMs)
2389{
Glenn Kasten72ef00d2012-01-17 11:09:42 -08002390 mAudioCommandThread->parametersCommand(ioHandle, keyValuePairs,
Dima Zavinfce7a472011-04-19 22:30:36 -07002391 delayMs);
2392}
2393
2394int AudioPolicyService::setStreamVolume(audio_stream_type_t stream,
2395 float volume,
2396 audio_io_handle_t output,
2397 int delayMs)
2398{
Glenn Kastenfff6d712012-01-12 16:38:12 -08002399 return (int)mAudioCommandThread->volumeCommand(stream, volume,
Glenn Kasten72ef00d2012-01-17 11:09:42 -08002400 output, delayMs);
Dima Zavinfce7a472011-04-19 22:30:36 -07002401}
2402
Dima Zavinfce7a472011-04-19 22:30:36 -07002403int AudioPolicyService::setVoiceVolume(float volume, int delayMs)
2404{
2405 return (int)mAudioCommandThread->voiceVolumeCommand(volume, delayMs);
2406}
2407
Eric Laurentb20cf7d2019-04-05 19:37:34 -07002408void AudioPolicyService::setEffectSuspended(int effectId,
2409 audio_session_t sessionId,
2410 bool suspended)
2411{
2412 mAudioCommandThread->setEffectSuspendedCommand(effectId, sessionId, suspended);
2413}
2414
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002415Status AudioPolicyService::onNewAudioModulesAvailable()
Mikhail Naganov88b30d22020-03-09 19:43:13 +00002416{
Mikhail Naganovd0e2c742020-03-25 15:59:59 -07002417 mOutputCommandThread->audioModulesUpdateCommand();
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002418 return Status::ok();
Mikhail Naganov88b30d22020-03-09 19:43:13 +00002419}
2420
Eric Laurentb20cf7d2019-04-05 19:37:34 -07002421
Dima Zavinfce7a472011-04-19 22:30:36 -07002422extern "C" {
Eric Laurent2d388ec2014-03-07 13:25:54 -08002423audio_module_handle_t aps_load_hw_module(void *service __unused,
2424 const char *name);
2425audio_io_handle_t aps_open_output(void *service __unused,
Eric Laurenta4c5a552012-03-29 10:12:40 -07002426 audio_devices_t *pDevices,
2427 uint32_t *pSamplingRate,
2428 audio_format_t *pFormat,
2429 audio_channel_mask_t *pChannelMask,
2430 uint32_t *pLatencyMs,
Eric Laurent2d388ec2014-03-07 13:25:54 -08002431 audio_output_flags_t flags);
Eric Laurenta4c5a552012-03-29 10:12:40 -07002432
Eric Laurent2d388ec2014-03-07 13:25:54 -08002433audio_io_handle_t aps_open_output_on_module(void *service __unused,
Eric Laurenta4c5a552012-03-29 10:12:40 -07002434 audio_module_handle_t module,
2435 audio_devices_t *pDevices,
2436 uint32_t *pSamplingRate,
2437 audio_format_t *pFormat,
2438 audio_channel_mask_t *pChannelMask,
2439 uint32_t *pLatencyMs,
Richard Fitzgeraldad3af332013-03-25 16:54:37 +00002440 audio_output_flags_t flags,
Eric Laurent2d388ec2014-03-07 13:25:54 -08002441 const audio_offload_info_t *offloadInfo);
2442audio_io_handle_t aps_open_dup_output(void *service __unused,
Dima Zavinfce7a472011-04-19 22:30:36 -07002443 audio_io_handle_t output1,
Eric Laurent2d388ec2014-03-07 13:25:54 -08002444 audio_io_handle_t output2);
2445int aps_close_output(void *service __unused, audio_io_handle_t output);
2446int aps_suspend_output(void *service __unused, audio_io_handle_t output);
2447int aps_restore_output(void *service __unused, audio_io_handle_t output);
2448audio_io_handle_t aps_open_input(void *service __unused,
Eric Laurenta4c5a552012-03-29 10:12:40 -07002449 audio_devices_t *pDevices,
2450 uint32_t *pSamplingRate,
2451 audio_format_t *pFormat,
2452 audio_channel_mask_t *pChannelMask,
Eric Laurent2d388ec2014-03-07 13:25:54 -08002453 audio_in_acoustics_t acoustics __unused);
2454audio_io_handle_t aps_open_input_on_module(void *service __unused,
Eric Laurenta4c5a552012-03-29 10:12:40 -07002455 audio_module_handle_t module,
2456 audio_devices_t *pDevices,
2457 uint32_t *pSamplingRate,
2458 audio_format_t *pFormat,
Eric Laurent2d388ec2014-03-07 13:25:54 -08002459 audio_channel_mask_t *pChannelMask);
2460int aps_close_input(void *service __unused, audio_io_handle_t input);
2461int aps_invalidate_stream(void *service __unused, audio_stream_type_t stream);
Glenn Kastend848eb42016-03-08 13:42:11 -08002462int aps_move_effects(void *service __unused, audio_session_t session,
Dima Zavinfce7a472011-04-19 22:30:36 -07002463 audio_io_handle_t src_output,
Eric Laurent2d388ec2014-03-07 13:25:54 -08002464 audio_io_handle_t dst_output);
2465char * aps_get_parameters(void *service __unused, audio_io_handle_t io_handle,
2466 const char *keys);
2467void aps_set_parameters(void *service, audio_io_handle_t io_handle,
2468 const char *kv_pairs, int delay_ms);
2469int aps_set_stream_volume(void *service, audio_stream_type_t stream,
Dima Zavinfce7a472011-04-19 22:30:36 -07002470 float volume, audio_io_handle_t output,
Eric Laurent2d388ec2014-03-07 13:25:54 -08002471 int delay_ms);
Eric Laurent2d388ec2014-03-07 13:25:54 -08002472int aps_set_voice_volume(void *service, float volume, int delay_ms);
2473};
Dima Zavinfce7a472011-04-19 22:30:36 -07002474
Mikhail Naganov1b2a7942017-12-08 10:18:09 -08002475} // namespace android