blob: 12cd136db9d1ccd1bf473ca3b0504f278f09184d [file] [log] [blame]
Mathias Agopian65ab4712010-07-14 17:59:35 -07001/*
2 * Copyright (C) 2009 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#define LOG_TAG "AudioPolicyService"
18//#define LOG_NDEBUG 0
19
Glenn Kasten153b9fe2013-07-15 11:23:36 -070020#include "Configuration.h"
Mathias Agopian65ab4712010-07-14 17:59:35 -070021#undef __STRICT_ANSI__
22#define __STDINT_LIMITS
23#define __STDC_LIMIT_MACROS
24#include <stdint.h>
Mathias Agopian65ab4712010-07-14 17:59:35 -070025#include <sys/time.h>
Jaideep Sharmaba9053b2021-01-25 21:24:26 +053026#include <dlfcn.h>
Mikhail Naganov959e2d02019-03-28 11:08:19 -070027
28#include <audio_utils/clock.h>
Mathias Agopian65ab4712010-07-14 17:59:35 -070029#include <binder/IServiceManager.h>
30#include <utils/Log.h>
31#include <cutils/properties.h>
32#include <binder/IPCThreadState.h>
Svet Ganovf4ddfef2018-01-16 07:37:58 -080033#include <binder/PermissionController.h>
34#include <binder/IResultReceiver.h>
Mathias Agopian65ab4712010-07-14 17:59:35 -070035#include <utils/String16.h>
36#include <utils/threads.h>
37#include "AudioPolicyService.h"
Mathias Agopian65ab4712010-07-14 17:59:35 -070038#include <hardware_legacy/power.h>
Ytai Ben-Tsvi7e7a79d2020-12-15 16:48:16 -080039#include <media/AidlConversion.h>
Eric Laurent7c7f10b2011-06-17 21:29:58 -070040#include <media/AudioEffect.h>
Chih-Hung Hsiehc84d9d22014-11-14 13:33:34 -080041#include <media/AudioParameter.h>
Andy Hungab7ef302018-05-15 19:35:29 -070042#include <mediautils/ServiceUtilities.h>
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -080043#include <mediautils/TimeCheck.h>
Michael Groovercfd28302018-12-11 19:16:46 -080044#include <sensorprivacy/SensorPrivacyManager.h>
Mathias Agopian65ab4712010-07-14 17:59:35 -070045
Dima Zavin64760242011-05-11 14:15:23 -070046#include <system/audio.h>
Dima Zavin7394a4f2011-06-13 18:16:26 -070047#include <system/audio_policy.h>
Jaideep Sharmaba9053b2021-01-25 21:24:26 +053048#include <AudioPolicyManager.h>
Mikhail Naganov61a4fac2016-10-13 14:44:18 -070049
Mathias Agopian65ab4712010-07-14 17:59:35 -070050namespace android {
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -080051using binder::Status;
Mathias Agopian65ab4712010-07-14 17:59:35 -070052
Glenn Kasten8dad0e32012-01-09 08:41:22 -080053static const char kDeadlockedString[] = "AudioPolicyService may be deadlocked\n";
54static const char kCmdDeadlockedString[] = "AudioPolicyService command thread may be deadlocked\n";
Jaideep Sharmaba9053b2021-01-25 21:24:26 +053055static const char kAudioPolicyManagerCustomPath[] = "libaudiopolicymanagercustom.so";
Mathias Agopian65ab4712010-07-14 17:59:35 -070056
Mikhail Naganov959e2d02019-03-28 11:08:19 -070057static const int kDumpLockTimeoutNs = 1 * NANOS_PER_SECOND;
Mathias Agopian65ab4712010-07-14 17:59:35 -070058
Eric Laurent0ede8922014-05-09 18:04:42 -070059static const nsecs_t kAudioCommandTimeoutNs = seconds(3); // 3 seconds
Christer Fletcher5fa8c4b2013-01-18 15:27:03 +010060
Svet Ganovf4ddfef2018-01-16 07:37:58 -080061static const String16 sManageAudioPolicyPermission("android.permission.MANAGE_AUDIO_POLICY");
Dima Zavinfce7a472011-04-19 22:30:36 -070062
Mathias Agopian65ab4712010-07-14 17:59:35 -070063// ----------------------------------------------------------------------------
64
Jaideep Sharmaba9053b2021-01-25 21:24:26 +053065static AudioPolicyInterface* createAudioPolicyManager(AudioPolicyClientInterface *clientInterface)
66{
67 AudioPolicyManager *apm = new AudioPolicyManager(clientInterface);
68 status_t status = apm->initialize();
69 if (status != NO_ERROR) {
70 delete apm;
71 apm = nullptr;
72 }
73 return apm;
74}
75
76static void destroyAudioPolicyManager(AudioPolicyInterface *interface)
77{
78 delete interface;
79}
80// ----------------------------------------------------------------------------
81
Mathias Agopian65ab4712010-07-14 17:59:35 -070082AudioPolicyService::AudioPolicyService()
Ytai Ben-Tsvi85093d52020-03-26 09:41:15 -070083 : BnAudioPolicyService(),
Ytai Ben-Tsvi85093d52020-03-26 09:41:15 -070084 mAudioPolicyManager(NULL),
85 mAudioPolicyClient(NULL),
86 mPhoneState(AUDIO_MODE_INVALID),
Jaideep Sharmaba9053b2021-01-25 21:24:26 +053087 mCaptureStateNotifier(false),
88 mCreateAudioPolicyManager(createAudioPolicyManager),
89 mDestroyAudioPolicyManager(destroyAudioPolicyManager) {
90}
91
92void AudioPolicyService::loadAudioPolicyManager()
93{
94 mLibraryHandle = dlopen(kAudioPolicyManagerCustomPath, RTLD_NOW);
95 if (mLibraryHandle != nullptr) {
96 ALOGI("%s loading %s", __func__, kAudioPolicyManagerCustomPath);
97 mCreateAudioPolicyManager = reinterpret_cast<CreateAudioPolicyManagerInstance>
98 (dlsym(mLibraryHandle, "createAudioPolicyManager"));
99 const char *lastError = dlerror();
100 ALOGW_IF(mCreateAudioPolicyManager == nullptr, "%s createAudioPolicyManager is null %s",
101 __func__, lastError != nullptr ? lastError : "no error");
102
103 mDestroyAudioPolicyManager = reinterpret_cast<DestroyAudioPolicyManagerInstance>(
104 dlsym(mLibraryHandle, "destroyAudioPolicyManager"));
105 lastError = dlerror();
106 ALOGW_IF(mDestroyAudioPolicyManager == nullptr, "%s destroyAudioPolicyManager is null %s",
107 __func__, lastError != nullptr ? lastError : "no error");
108 if (mCreateAudioPolicyManager == nullptr || mDestroyAudioPolicyManager == nullptr){
109 unloadAudioPolicyManager();
110 LOG_ALWAYS_FATAL("could not find audiopolicymanager interface methods");
111 }
112 }
Eric Laurentf5ada6e2014-10-09 17:49:00 -0700113}
114
115void AudioPolicyService::onFirstRef()
116{
Eric Laurent8b1e80b2014-10-07 09:08:47 -0700117 {
118 Mutex::Autolock _l(mLock);
Eric Laurent93575202011-01-18 18:39:02 -0800119
Eric Laurent8b1e80b2014-10-07 09:08:47 -0700120 // start audio commands thread
121 mAudioCommandThread = new AudioCommandThread(String8("ApmAudio"), this);
122 // start output activity command thread
123 mOutputCommandThread = new AudioCommandThread(String8("ApmOutput"), this);
Eric Laurentdce54a12014-03-10 12:19:46 -0700124
Eric Laurent8b1e80b2014-10-07 09:08:47 -0700125 mAudioPolicyClient = new AudioPolicyClient(this);
Jaideep Sharmaba9053b2021-01-25 21:24:26 +0530126
127 loadAudioPolicyManager();
128 mAudioPolicyManager = mCreateAudioPolicyManager(mAudioPolicyClient);
Eric Laurent8b1e80b2014-10-07 09:08:47 -0700129 }
Eric Laurentd66d7a12021-07-13 13:35:32 +0200130
bryant_liuba2b4392014-06-11 16:49:30 +0800131 // load audio processing modules
Mikhail Naganov12b716c2020-04-30 22:37:43 +0000132 sp<AudioPolicyEffects> audioPolicyEffects = new AudioPolicyEffects();
133 sp<UidPolicy> uidPolicy = new UidPolicy(this);
134 sp<SensorPrivacyPolicy> sensorPrivacyPolicy = new SensorPrivacyPolicy(this);
Eric Laurent8b1e80b2014-10-07 09:08:47 -0700135 {
136 Mutex::Autolock _l(mLock);
137 mAudioPolicyEffects = audioPolicyEffects;
Mikhail Naganov12b716c2020-04-30 22:37:43 +0000138 mUidPolicy = uidPolicy;
139 mSensorPrivacyPolicy = sensorPrivacyPolicy;
Eric Laurent8b1e80b2014-10-07 09:08:47 -0700140 }
Mikhail Naganov12b716c2020-04-30 22:37:43 +0000141 uidPolicy->registerSelf();
142 sensorPrivacyPolicy->registerSelf();
Eric Laurentd66d7a12021-07-13 13:35:32 +0200143
Eric Laurent6d607012021-07-05 11:54:40 +0200144 // Create spatializer if supported
Eric Laurent52b0bd52021-09-27 15:25:40 +0200145 if (mAudioPolicyManager != nullptr) {
146 Mutex::Autolock _l(mLock);
147 const audio_attributes_t attr = attributes_initializer(AUDIO_USAGE_MEDIA);
148 AudioDeviceTypeAddrVector devices;
149 bool hasSpatializer = mAudioPolicyManager->canBeSpatialized(&attr, nullptr, devices);
150 if (hasSpatializer) {
151 mSpatializer = Spatializer::create(this);
152 }
Eric Laurent6d607012021-07-05 11:54:40 +0200153 }
Eric Laurentd66d7a12021-07-13 13:35:32 +0200154 AudioSystem::audioPolicyReady();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700155}
156
Jaideep Sharmaba9053b2021-01-25 21:24:26 +0530157void AudioPolicyService::unloadAudioPolicyManager()
158{
159 ALOGV("%s ", __func__);
160 if (mLibraryHandle != nullptr) {
161 dlclose(mLibraryHandle);
162 }
163 mLibraryHandle = nullptr;
164 mCreateAudioPolicyManager = nullptr;
165 mDestroyAudioPolicyManager = nullptr;
166}
167
Mathias Agopian65ab4712010-07-14 17:59:35 -0700168AudioPolicyService::~AudioPolicyService()
169{
Mathias Agopian65ab4712010-07-14 17:59:35 -0700170 mAudioCommandThread->exit();
Eric Laurent657ff612014-05-07 11:58:24 -0700171 mOutputCommandThread->exit();
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700172
Jaideep Sharmaba9053b2021-01-25 21:24:26 +0530173 mDestroyAudioPolicyManager(mAudioPolicyManager);
174 unloadAudioPolicyManager();
175
Eric Laurentdce54a12014-03-10 12:19:46 -0700176 delete mAudioPolicyClient;
Eric Laurentb52c1522014-05-20 11:27:36 -0700177
178 mNotificationClients.clear();
bryant_liuba2b4392014-06-11 16:49:30 +0800179 mAudioPolicyEffects.clear();
Svet Ganovf4ddfef2018-01-16 07:37:58 -0800180
181 mUidPolicy->unregisterSelf();
Michael Groovercfd28302018-12-11 19:16:46 -0800182 mSensorPrivacyPolicy->unregisterSelf();
Mikhail Naganov12b716c2020-04-30 22:37:43 +0000183
184 mUidPolicy.clear();
Michael Groovercfd28302018-12-11 19:16:46 -0800185 mSensorPrivacyPolicy.clear();
Eric Laurentb52c1522014-05-20 11:27:36 -0700186}
187
188// A notification client is always registered by AudioSystem when the client process
189// connects to AudioPolicyService.
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800190Status AudioPolicyService::registerClient(const sp<media::IAudioPolicyServiceClient>& client)
Eric Laurentb52c1522014-05-20 11:27:36 -0700191{
Eric Laurent12590252015-08-21 18:40:20 -0700192 if (client == 0) {
193 ALOGW("%s got NULL client", __FUNCTION__);
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800194 return Status::ok();
Eric Laurent12590252015-08-21 18:40:20 -0700195 }
Eric Laurent0ebd5f92014-11-19 19:04:52 -0800196 Mutex::Autolock _l(mNotificationClientsLock);
Eric Laurentb52c1522014-05-20 11:27:36 -0700197
198 uid_t uid = IPCThreadState::self()->getCallingUid();
luochaojiang908c7d72018-06-21 14:58:04 +0800199 pid_t pid = IPCThreadState::self()->getCallingPid();
200 int64_t token = ((int64_t)uid<<32) | pid;
201
202 if (mNotificationClients.indexOfKey(token) < 0) {
Eric Laurentb52c1522014-05-20 11:27:36 -0700203 sp<NotificationClient> notificationClient = new NotificationClient(this,
204 client,
luochaojiang908c7d72018-06-21 14:58:04 +0800205 uid,
206 pid);
207 ALOGV("registerClient() client %p, uid %d pid %d", client.get(), uid, pid);
Eric Laurentb52c1522014-05-20 11:27:36 -0700208
luochaojiang908c7d72018-06-21 14:58:04 +0800209 mNotificationClients.add(token, notificationClient);
Eric Laurentb52c1522014-05-20 11:27:36 -0700210
Marco Nelissenf8880202014-11-14 07:58:25 -0800211 sp<IBinder> binder = IInterface::asBinder(client);
Eric Laurentb52c1522014-05-20 11:27:36 -0700212 binder->linkToDeath(notificationClient);
213 }
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800214 return Status::ok();
Eric Laurentb52c1522014-05-20 11:27:36 -0700215}
216
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800217Status AudioPolicyService::setAudioPortCallbacksEnabled(bool enabled)
Eric Laurente8726fe2015-06-26 09:39:24 -0700218{
219 Mutex::Autolock _l(mNotificationClientsLock);
220
221 uid_t uid = IPCThreadState::self()->getCallingUid();
luochaojiang908c7d72018-06-21 14:58:04 +0800222 pid_t pid = IPCThreadState::self()->getCallingPid();
223 int64_t token = ((int64_t)uid<<32) | pid;
224
225 if (mNotificationClients.indexOfKey(token) < 0) {
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800226 return Status::ok();
Eric Laurente8726fe2015-06-26 09:39:24 -0700227 }
luochaojiang908c7d72018-06-21 14:58:04 +0800228 mNotificationClients.valueFor(token)->setAudioPortCallbacksEnabled(enabled);
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800229 return Status::ok();
Eric Laurente8726fe2015-06-26 09:39:24 -0700230}
231
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800232Status AudioPolicyService::setAudioVolumeGroupCallbacksEnabled(bool enabled)
François Gaffiecfe17322018-11-07 13:41:29 +0100233{
234 Mutex::Autolock _l(mNotificationClientsLock);
235
236 uid_t uid = IPCThreadState::self()->getCallingUid();
237 pid_t pid = IPCThreadState::self()->getCallingPid();
238 int64_t token = ((int64_t)uid<<32) | pid;
239
240 if (mNotificationClients.indexOfKey(token) < 0) {
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800241 return Status::ok();
François Gaffiecfe17322018-11-07 13:41:29 +0100242 }
243 mNotificationClients.valueFor(token)->setAudioVolumeGroupCallbacksEnabled(enabled);
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800244 return Status::ok();
François Gaffiecfe17322018-11-07 13:41:29 +0100245}
246
Eric Laurentb52c1522014-05-20 11:27:36 -0700247// removeNotificationClient() is called when the client process dies.
luochaojiang908c7d72018-06-21 14:58:04 +0800248void AudioPolicyService::removeNotificationClient(uid_t uid, pid_t pid)
Eric Laurentb52c1522014-05-20 11:27:36 -0700249{
Mikhail Naganov12b716c2020-04-30 22:37:43 +0000250 bool hasSameUid = false;
Eric Laurent0ebd5f92014-11-19 19:04:52 -0800251 {
252 Mutex::Autolock _l(mNotificationClientsLock);
luochaojiang908c7d72018-06-21 14:58:04 +0800253 int64_t token = ((int64_t)uid<<32) | pid;
254 mNotificationClients.removeItem(token);
luochaojiang908c7d72018-06-21 14:58:04 +0800255 for (size_t i = 0; i < mNotificationClients.size(); i++) {
256 if (mNotificationClients.valueAt(i)->uid() == uid) {
257 hasSameUid = true;
258 break;
259 }
260 }
Mikhail Naganov12b716c2020-04-30 22:37:43 +0000261 }
262 {
263 Mutex::Autolock _l(mLock);
luochaojiang908c7d72018-06-21 14:58:04 +0800264 if (mAudioPolicyManager && !hasSameUid) {
Eric Laurent10b71232018-04-13 18:14:44 -0700265 // called from binder death notification: no need to clear caller identity
Eric Laurent8c7e6da2015-04-21 17:37:00 -0700266 mAudioPolicyManager->releaseResourcesForUid(uid);
Eric Laurentb52c1522014-05-20 11:27:36 -0700267 }
Eric Laurent0ebd5f92014-11-19 19:04:52 -0800268 }
Eric Laurentb52c1522014-05-20 11:27:36 -0700269}
270
271void AudioPolicyService::onAudioPortListUpdate()
272{
273 mOutputCommandThread->updateAudioPortListCommand();
274}
275
276void AudioPolicyService::doOnAudioPortListUpdate()
277{
Eric Laurent0ebd5f92014-11-19 19:04:52 -0800278 Mutex::Autolock _l(mNotificationClientsLock);
Eric Laurentb52c1522014-05-20 11:27:36 -0700279 for (size_t i = 0; i < mNotificationClients.size(); i++) {
280 mNotificationClients.valueAt(i)->onAudioPortListUpdate();
281 }
282}
283
284void AudioPolicyService::onAudioPatchListUpdate()
285{
286 mOutputCommandThread->updateAudioPatchListCommand();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700287}
288
Eric Laurentb52c1522014-05-20 11:27:36 -0700289void AudioPolicyService::doOnAudioPatchListUpdate()
290{
Eric Laurent0ebd5f92014-11-19 19:04:52 -0800291 Mutex::Autolock _l(mNotificationClientsLock);
Eric Laurentb52c1522014-05-20 11:27:36 -0700292 for (size_t i = 0; i < mNotificationClients.size(); i++) {
293 mNotificationClients.valueAt(i)->onAudioPatchListUpdate();
294 }
295}
296
François Gaffiecfe17322018-11-07 13:41:29 +0100297void AudioPolicyService::onAudioVolumeGroupChanged(volume_group_t group, int flags)
298{
299 mOutputCommandThread->changeAudioVolumeGroupCommand(group, flags);
300}
301
302void AudioPolicyService::doOnAudioVolumeGroupChanged(volume_group_t group, int flags)
303{
304 Mutex::Autolock _l(mNotificationClientsLock);
305 for (size_t i = 0; i < mNotificationClients.size(); i++) {
306 mNotificationClients.valueAt(i)->onAudioVolumeGroupChanged(group, flags);
307 }
308}
309
Chih-Hung Hsiehe964d4e2016-08-09 14:31:32 -0700310void AudioPolicyService::onDynamicPolicyMixStateUpdate(const String8& regId, int32_t state)
Jean-Michel Trivide801052015-04-14 19:10:14 -0700311{
312 ALOGV("AudioPolicyService::onDynamicPolicyMixStateUpdate(%s, %d)",
313 regId.string(), state);
314 mOutputCommandThread->dynamicPolicyMixStateUpdateCommand(regId, state);
315}
316
Chih-Hung Hsiehe964d4e2016-08-09 14:31:32 -0700317void AudioPolicyService::doOnDynamicPolicyMixStateUpdate(const String8& regId, int32_t state)
Jean-Michel Trivide801052015-04-14 19:10:14 -0700318{
319 Mutex::Autolock _l(mNotificationClientsLock);
320 for (size_t i = 0; i < mNotificationClients.size(); i++) {
321 mNotificationClients.valueAt(i)->onDynamicPolicyMixStateUpdate(regId, state);
322 }
323}
324
Eric Laurenta9f86652018-11-28 17:23:11 -0800325void AudioPolicyService::onRecordingConfigurationUpdate(
326 int event,
327 const record_client_info_t *clientInfo,
328 const audio_config_base_t *clientConfig,
329 std::vector<effect_descriptor_t> clientEffects,
330 const audio_config_base_t *deviceConfig,
331 std::vector<effect_descriptor_t> effects,
332 audio_patch_handle_t patchHandle,
333 audio_source_t source)
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -0800334{
Jean-Michel Triviac4e4292016-12-22 11:39:31 -0800335 mOutputCommandThread->recordingConfigurationUpdateCommand(event, clientInfo,
Eric Laurenta9f86652018-11-28 17:23:11 -0800336 clientConfig, clientEffects, deviceConfig, effects, patchHandle, source);
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -0800337}
338
Eric Laurenta9f86652018-11-28 17:23:11 -0800339void AudioPolicyService::doOnRecordingConfigurationUpdate(
340 int event,
341 const record_client_info_t *clientInfo,
342 const audio_config_base_t *clientConfig,
343 std::vector<effect_descriptor_t> clientEffects,
344 const audio_config_base_t *deviceConfig,
345 std::vector<effect_descriptor_t> effects,
346 audio_patch_handle_t patchHandle,
347 audio_source_t source)
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -0800348{
349 Mutex::Autolock _l(mNotificationClientsLock);
350 for (size_t i = 0; i < mNotificationClients.size(); i++) {
Jean-Michel Triviac4e4292016-12-22 11:39:31 -0800351 mNotificationClients.valueAt(i)->onRecordingConfigurationUpdate(event, clientInfo,
Eric Laurenta9f86652018-11-28 17:23:11 -0800352 clientConfig, clientEffects, deviceConfig, effects, patchHandle, source);
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -0800353 }
354}
355
Jean-Michel Trivi9a6b9ad2020-10-22 16:46:43 -0700356void AudioPolicyService::onRoutingUpdated()
357{
358 mOutputCommandThread->routingChangedCommand();
359}
360
361void AudioPolicyService::doOnRoutingUpdated()
362{
363 Mutex::Autolock _l(mNotificationClientsLock);
364 for (size_t i = 0; i < mNotificationClients.size(); i++) {
365 mNotificationClients.valueAt(i)->onRoutingUpdated();
366 }
367}
368
Eric Laurent6d607012021-07-05 11:54:40 +0200369void AudioPolicyService::onCheckSpatializer()
370{
371 Mutex::Autolock _l(mLock);
Eric Laurent39095982021-08-24 18:29:27 +0200372 onCheckSpatializer_l();
373}
374
375void AudioPolicyService::onCheckSpatializer_l()
376{
377 if (mSpatializer != nullptr) {
378 mOutputCommandThread->checkSpatializerCommand();
379 }
Eric Laurent6d607012021-07-05 11:54:40 +0200380}
381
382void AudioPolicyService::doOnCheckSpatializer()
383{
Eric Laurent39095982021-08-24 18:29:27 +0200384 Mutex::Autolock _l(mLock);
Eric Laurent6d607012021-07-05 11:54:40 +0200385
Eric Laurent39095982021-08-24 18:29:27 +0200386 if (mSpatializer != nullptr) {
Eric Laurent52b0bd52021-09-27 15:25:40 +0200387 // Note: mSpatializer != nullptr => mAudioPolicyManager != nullptr
Eric Laurent39095982021-08-24 18:29:27 +0200388 if (mSpatializer->getLevel() != media::SpatializationLevel::NONE) {
389 audio_io_handle_t currentOutput = mSpatializer->getOutput();
390 audio_io_handle_t newOutput;
391 const audio_attributes_t attr = attributes_initializer(AUDIO_USAGE_MEDIA);
392 audio_config_base_t config = mSpatializer->getAudioInConfig();
393 status_t status =
394 mAudioPolicyManager->getSpatializerOutput(&config, &attr, &newOutput);
Eric Laurentd23aa162022-01-17 17:37:31 +0100395 ALOGV("%s currentOutput %d newOutput %d channel_mask %#x",
396 __func__, currentOutput, newOutput, config.channel_mask);
Eric Laurent39095982021-08-24 18:29:27 +0200397 if (status == NO_ERROR && currentOutput == newOutput) {
398 return;
399 }
400 mLock.unlock();
401 // It is OK to call detachOutput() is none is already attached.
402 mSpatializer->detachOutput();
403 if (status != NO_ERROR || newOutput == AUDIO_IO_HANDLE_NONE) {
Eric Laurent6d607012021-07-05 11:54:40 +0200404 mLock.lock();
Eric Laurent39095982021-08-24 18:29:27 +0200405 return;
406 }
407 status = mSpatializer->attachOutput(newOutput);
408 mLock.lock();
409 if (status != NO_ERROR) {
410 mAudioPolicyManager->releaseSpatializerOutput(newOutput);
411 }
412 } else if (mSpatializer->getLevel() == media::SpatializationLevel::NONE
413 && mSpatializer->getOutput() != AUDIO_IO_HANDLE_NONE) {
414 mLock.unlock();
415 audio_io_handle_t output = mSpatializer->detachOutput();
416 mLock.lock();
417 if (output != AUDIO_IO_HANDLE_NONE) {
418 mAudioPolicyManager->releaseSpatializerOutput(output);
Eric Laurent6d607012021-07-05 11:54:40 +0200419 }
420 }
421 }
422}
423
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -0800424status_t AudioPolicyService::clientCreateAudioPatch(const struct audio_patch *patch,
425 audio_patch_handle_t *handle,
426 int delayMs)
427{
428 return mAudioCommandThread->createAudioPatchCommand(patch, handle, delayMs);
429}
430
431status_t AudioPolicyService::clientReleaseAudioPatch(audio_patch_handle_t handle,
432 int delayMs)
433{
434 return mAudioCommandThread->releaseAudioPatchCommand(handle, delayMs);
435}
436
Eric Laurente1715a42014-05-20 11:30:42 -0700437status_t AudioPolicyService::clientSetAudioPortConfig(const struct audio_port_config *config,
438 int delayMs)
439{
440 return mAudioCommandThread->setAudioPortConfigCommand(config, delayMs);
441}
442
Ytai Ben-Tsvi7e7a79d2020-12-15 16:48:16 -0800443AudioPolicyService::NotificationClient::NotificationClient(
444 const sp<AudioPolicyService>& service,
445 const sp<media::IAudioPolicyServiceClient>& client,
446 uid_t uid,
447 pid_t pid)
luochaojiang908c7d72018-06-21 14:58:04 +0800448 : mService(service), mUid(uid), mPid(pid), mAudioPolicyServiceClient(client),
François Gaffiecfe17322018-11-07 13:41:29 +0100449 mAudioPortCallbacksEnabled(false), mAudioVolumeGroupCallbacksEnabled(false)
Eric Laurentb52c1522014-05-20 11:27:36 -0700450{
451}
452
453AudioPolicyService::NotificationClient::~NotificationClient()
454{
455}
456
457void AudioPolicyService::NotificationClient::binderDied(const wp<IBinder>& who __unused)
458{
459 sp<NotificationClient> keep(this);
460 sp<AudioPolicyService> service = mService.promote();
461 if (service != 0) {
luochaojiang908c7d72018-06-21 14:58:04 +0800462 service->removeNotificationClient(mUid, mPid);
Eric Laurentb52c1522014-05-20 11:27:36 -0700463 }
464}
465
466void AudioPolicyService::NotificationClient::onAudioPortListUpdate()
467{
Eric Laurente8726fe2015-06-26 09:39:24 -0700468 if (mAudioPolicyServiceClient != 0 && mAudioPortCallbacksEnabled) {
Eric Laurentb52c1522014-05-20 11:27:36 -0700469 mAudioPolicyServiceClient->onAudioPortListUpdate();
470 }
471}
472
473void AudioPolicyService::NotificationClient::onAudioPatchListUpdate()
474{
Eric Laurente8726fe2015-06-26 09:39:24 -0700475 if (mAudioPolicyServiceClient != 0 && mAudioPortCallbacksEnabled) {
Eric Laurentb52c1522014-05-20 11:27:36 -0700476 mAudioPolicyServiceClient->onAudioPatchListUpdate();
477 }
478}
Eric Laurent57dae992011-07-24 13:36:09 -0700479
François Gaffiecfe17322018-11-07 13:41:29 +0100480void AudioPolicyService::NotificationClient::onAudioVolumeGroupChanged(volume_group_t group,
481 int flags)
482{
483 if (mAudioPolicyServiceClient != 0 && mAudioVolumeGroupCallbacksEnabled) {
484 mAudioPolicyServiceClient->onAudioVolumeGroupChanged(group, flags);
485 }
486}
487
488
Jean-Michel Trivide801052015-04-14 19:10:14 -0700489void AudioPolicyService::NotificationClient::onDynamicPolicyMixStateUpdate(
Chih-Hung Hsiehe964d4e2016-08-09 14:31:32 -0700490 const String8& regId, int32_t state)
Jean-Michel Trivide801052015-04-14 19:10:14 -0700491{
Andy Hung4ef19fa2018-05-15 19:35:29 -0700492 if (mAudioPolicyServiceClient != 0 && isServiceUid(mUid)) {
Ytai Ben-Tsvi7e7a79d2020-12-15 16:48:16 -0800493 mAudioPolicyServiceClient->onDynamicPolicyMixStateUpdate(
494 legacy2aidl_String8_string(regId).value(), state);
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -0800495 }
496}
497
498void AudioPolicyService::NotificationClient::onRecordingConfigurationUpdate(
Eric Laurenta9f86652018-11-28 17:23:11 -0800499 int event,
500 const record_client_info_t *clientInfo,
501 const audio_config_base_t *clientConfig,
502 std::vector<effect_descriptor_t> clientEffects,
503 const audio_config_base_t *deviceConfig,
504 std::vector<effect_descriptor_t> effects,
505 audio_patch_handle_t patchHandle,
506 audio_source_t source)
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -0800507{
Andy Hung4ef19fa2018-05-15 19:35:29 -0700508 if (mAudioPolicyServiceClient != 0 && isServiceUid(mUid)) {
Ytai Ben-Tsvi7e7a79d2020-12-15 16:48:16 -0800509 status_t status = [&]() -> status_t {
510 int32_t eventAidl = VALUE_OR_RETURN_STATUS(convertIntegral<int32_t>(event));
511 media::RecordClientInfo clientInfoAidl = VALUE_OR_RETURN_STATUS(
512 legacy2aidl_record_client_info_t_RecordClientInfo(*clientInfo));
513 media::AudioConfigBase clientConfigAidl = VALUE_OR_RETURN_STATUS(
514 legacy2aidl_audio_config_base_t_AudioConfigBase(*clientConfig));
515 std::vector<media::EffectDescriptor> clientEffectsAidl = VALUE_OR_RETURN_STATUS(
516 convertContainer<std::vector<media::EffectDescriptor>>(
517 clientEffects,
518 legacy2aidl_effect_descriptor_t_EffectDescriptor));
519 media::AudioConfigBase deviceConfigAidl = VALUE_OR_RETURN_STATUS(
520 legacy2aidl_audio_config_base_t_AudioConfigBase(*deviceConfig));
521 std::vector<media::EffectDescriptor> effectsAidl = VALUE_OR_RETURN_STATUS(
522 convertContainer<std::vector<media::EffectDescriptor>>(
523 effects,
524 legacy2aidl_effect_descriptor_t_EffectDescriptor));
525 int32_t patchHandleAidl = VALUE_OR_RETURN_STATUS(
526 legacy2aidl_audio_patch_handle_t_int32_t(patchHandle));
527 media::AudioSourceType sourceAidl = VALUE_OR_RETURN_STATUS(
528 legacy2aidl_audio_source_t_AudioSourceType(source));
529 return aidl_utils::statusTFromBinderStatus(
530 mAudioPolicyServiceClient->onRecordingConfigurationUpdate(eventAidl,
531 clientInfoAidl,
532 clientConfigAidl,
533 clientEffectsAidl,
534 deviceConfigAidl,
535 effectsAidl,
536 patchHandleAidl,
537 sourceAidl));
538 }();
539 ALOGW_IF(status != OK, "onRecordingConfigurationUpdate() failed: %d", status);
Jean-Michel Trivide801052015-04-14 19:10:14 -0700540 }
541}
542
Eric Laurente8726fe2015-06-26 09:39:24 -0700543void AudioPolicyService::NotificationClient::setAudioPortCallbacksEnabled(bool enabled)
544{
545 mAudioPortCallbacksEnabled = enabled;
546}
547
François Gaffiecfe17322018-11-07 13:41:29 +0100548void AudioPolicyService::NotificationClient::setAudioVolumeGroupCallbacksEnabled(bool enabled)
549{
550 mAudioVolumeGroupCallbacksEnabled = enabled;
551}
Eric Laurente8726fe2015-06-26 09:39:24 -0700552
Jean-Michel Trivi9a6b9ad2020-10-22 16:46:43 -0700553void AudioPolicyService::NotificationClient::onRoutingUpdated()
554{
555 if (mAudioPolicyServiceClient != 0 && isServiceUid(mUid)) {
556 mAudioPolicyServiceClient->onRoutingUpdated();
557 }
558}
559
Mathias Agopian65ab4712010-07-14 17:59:35 -0700560void AudioPolicyService::binderDied(const wp<IBinder>& who) {
Glenn Kasten411e4472012-11-02 10:00:06 -0700561 ALOGW("binderDied() %p, calling pid %d", who.unsafe_get(),
Eric Laurentde070132010-07-13 04:45:46 -0700562 IPCThreadState::self()->getCallingPid());
Mathias Agopian65ab4712010-07-14 17:59:35 -0700563}
564
Mikhail Naganov12b716c2020-04-30 22:37:43 +0000565static bool dumpTryLock(Mutex& mutex) ACQUIRE(mutex) NO_THREAD_SAFETY_ANALYSIS
Mathias Agopian65ab4712010-07-14 17:59:35 -0700566{
Mikhail Naganov12b716c2020-04-30 22:37:43 +0000567 return mutex.timedLock(kDumpLockTimeoutNs) == NO_ERROR;
568}
569
570static void dumpReleaseLock(Mutex& mutex, bool locked) RELEASE(mutex) NO_THREAD_SAFETY_ANALYSIS
571{
572 if (locked) mutex.unlock();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700573}
574
575status_t AudioPolicyService::dumpInternals(int fd)
576{
577 const size_t SIZE = 256;
578 char buffer[SIZE];
579 String8 result;
580
Eric Laurentdce54a12014-03-10 12:19:46 -0700581 snprintf(buffer, SIZE, "AudioPolicyManager: %p\n", mAudioPolicyManager);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700582 result.append(buffer);
583 snprintf(buffer, SIZE, "Command Thread: %p\n", mAudioCommandThread.get());
584 result.append(buffer);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700585
Hayden Gomes524159d2019-12-23 14:41:47 -0800586 snprintf(buffer, SIZE, "Supported System Usages:\n");
587 result.append(buffer);
588 for (std::vector<audio_usage_t>::iterator it = mSupportedSystemUsages.begin();
589 it != mSupportedSystemUsages.end(); ++it) {
590 snprintf(buffer, SIZE, "\t%d\n", *it);
591 result.append(buffer);
592 }
593
Mathias Agopian65ab4712010-07-14 17:59:35 -0700594 write(fd, result.string(), result.size());
595 return NO_ERROR;
596}
597
Eric Laurente8c8b432018-10-17 10:08:02 -0700598void AudioPolicyService::updateUidStates()
Svet Ganovf4ddfef2018-01-16 07:37:58 -0800599{
Eric Laurente8c8b432018-10-17 10:08:02 -0700600 Mutex::Autolock _l(mLock);
601 updateUidStates_l();
602}
603
604void AudioPolicyService::updateUidStates_l()
605{
Eric Laurent4eb58f12018-12-07 16:41:02 -0800606// Go over all active clients and allow capture (does not force silence) in the
607// following cases:
Evan Severson1f700cd2021-02-10 13:10:37 -0800608// The client is the assistant
609// AND an accessibility service is on TOP or a RTT call is active
Eric Laurent589171c2019-07-25 18:04:29 -0700610// AND the source is VOICE_RECOGNITION or HOTWORD
Evan Severson1f700cd2021-02-10 13:10:37 -0800611// OR uses VOICE_RECOGNITION AND is on TOP
612// OR uses HOTWORD
613// AND there is no active privacy sensitive capture or call
614// OR client has CAPTURE_AUDIO_OUTPUT privileged permission
615// OR The client is an accessibility service
616// AND Is on TOP
617// AND the source is VOICE_RECOGNITION or HOTWORD
618// OR The assistant is not on TOP
Eric Laurent589171c2019-07-25 18:04:29 -0700619// AND there is no active privacy sensitive capture or call
620// OR client has CAPTURE_AUDIO_OUTPUT privileged permission
Evan Severson1f700cd2021-02-10 13:10:37 -0800621// AND is on TOP
622// AND the source is VOICE_RECOGNITION or HOTWORD
623// OR the client source is virtual (remote submix, call audio TX or RX...)
624// OR the client source is HOTWORD
625// AND is on TOP
626// OR all active clients are using HOTWORD source
627// AND no call is active
628// OR client has CAPTURE_AUDIO_OUTPUT privileged permission
629// OR the client is the current InputMethodService
630// AND a RTT call is active AND the source is VOICE_RECOGNITION
631// OR Any client
632// AND The assistant is not on TOP
633// AND is on TOP or latest started
634// AND there is no active privacy sensitive capture or call
635// OR client has CAPTURE_AUDIO_OUTPUT privileged permission
Eric Laurent4eb58f12018-12-07 16:41:02 -0800636
Eric Laurent4e947da2019-10-17 15:24:06 -0700637
Eric Laurent4eb58f12018-12-07 16:41:02 -0800638 sp<AudioRecordClient> topActive;
639 sp<AudioRecordClient> latestActive;
Eric Laurentc21d5692020-02-25 10:24:36 -0800640 sp<AudioRecordClient> topSensitiveActive;
Eric Laurentb809a752020-06-29 09:53:13 -0700641 sp<AudioRecordClient> latestSensitiveActiveOrComm;
Eric Laurent1ff16a72019-03-14 18:35:04 -0700642
Eric Laurenta46bedb2018-12-07 18:01:26 -0800643 nsecs_t topStartNs = 0;
644 nsecs_t latestStartNs = 0;
Eric Laurentc21d5692020-02-25 10:24:36 -0800645 nsecs_t topSensitiveStartNs = 0;
Eric Laurent4eb58f12018-12-07 16:41:02 -0800646 nsecs_t latestSensitiveStartNs = 0;
647 bool isA11yOnTop = mUidPolicy->isA11yOnTop();
648 bool isAssistantOnTop = false;
649 bool isSensitiveActive = false;
Eric Laurent1ff16a72019-03-14 18:35:04 -0700650 bool isInCall = mPhoneState == AUDIO_MODE_IN_CALL;
Eric Laurentc21d5692020-02-25 10:24:36 -0800651 bool isInCommunication = mPhoneState == AUDIO_MODE_IN_COMMUNICATION;
652 bool rttCallActive = (isInCall || isInCommunication)
Eric Laurent6ede98f2019-06-11 14:50:30 -0700653 && mUidPolicy->isRttEnabled();
Eric Laurent4e947da2019-10-17 15:24:06 -0700654 bool onlyHotwordActive = true;
Eric Laurentb809a752020-06-29 09:53:13 -0700655 bool isPhoneStateOwnerActive = false;
Eric Laurent4eb58f12018-12-07 16:41:02 -0800656
Michael Groovercfd28302018-12-11 19:16:46 -0800657 // if Sensor Privacy is enabled then all recordings should be silenced.
658 if (mSensorPrivacyPolicy->isSensorPrivacyEnabled()) {
659 silenceAllRecordings_l();
660 return;
661 }
662
Eric Laurente8c8b432018-10-17 10:08:02 -0700663 for (size_t i =0; i < mAudioRecordClients.size(); i++) {
664 sp<AudioRecordClient> current = mAudioRecordClients[i];
Svet Ganov33761132021-05-13 22:51:08 +0000665 uid_t currentUid = VALUE_OR_FATAL(aidl2legacy_int32_t_uid_t(
666 current->attributionSource.uid));
Evan Severson1f700cd2021-02-10 13:10:37 -0800667 if (!current->active) {
Eric Laurent1ff16a72019-03-14 18:35:04 -0700668 continue;
Eric Laurent4eb58f12018-12-07 16:41:02 -0800669 }
Eric Laurent1ff16a72019-03-14 18:35:04 -0700670
Philip P. Moltmannbda45752020-07-17 16:41:18 -0700671 app_state_t appState = apmStatFromAmState(mUidPolicy->getUidState(currentUid));
Eric Laurent1ff16a72019-03-14 18:35:04 -0700672 // clients which app is in IDLE state are not eligible for top active or
673 // latest active
674 if (appState == APP_STATE_IDLE) {
675 continue;
676 }
677
Philip P. Moltmannbda45752020-07-17 16:41:18 -0700678 bool isAccessibility = mUidPolicy->isA11yUid(currentUid);
Eric Laurent14a88632020-07-16 12:28:30 -0700679 // Clients capturing for Accessibility services or virtual sources are not considered
Eric Laurentc21d5692020-02-25 10:24:36 -0800680 // for top or latest active to avoid masking regular clients started before
Eric Laurent14a88632020-07-16 12:28:30 -0700681 if (!isAccessibility && !isVirtualSource(current->attributes.source)) {
Philip P. Moltmannbda45752020-07-17 16:41:18 -0700682 bool isAssistant = mUidPolicy->isAssistantUid(currentUid);
Eric Laurentc21d5692020-02-25 10:24:36 -0800683 bool isPrivacySensitive =
684 (current->attributes.flags & AUDIO_FLAG_CAPTURE_PRIVATE) != 0;
Eric Laurentb809a752020-06-29 09:53:13 -0700685
Eric Laurentc21d5692020-02-25 10:24:36 -0800686 if (appState == APP_STATE_TOP) {
687 if (isPrivacySensitive) {
688 if (current->startTimeNs > topSensitiveStartNs) {
689 topSensitiveActive = current;
690 topSensitiveStartNs = current->startTimeNs;
691 }
692 } else {
693 if (current->startTimeNs > topStartNs) {
694 topActive = current;
695 topStartNs = current->startTimeNs;
696 }
697 }
698 if (isAssistant) {
699 isAssistantOnTop = true;
700 }
Eric Laurenta46bedb2018-12-07 18:01:26 -0800701 }
Eric Laurentc21d5692020-02-25 10:24:36 -0800702 // Clients capturing for HOTWORD are not considered
703 // for latest active to avoid masking regular clients started before
704 if (!(current->attributes.source == AUDIO_SOURCE_HOTWORD
705 || ((isA11yOnTop || rttCallActive) && isAssistant))) {
706 if (isPrivacySensitive) {
Eric Laurentb809a752020-06-29 09:53:13 -0700707 // if audio mode is IN_COMMUNICATION, make sure the audio mode owner
708 // is marked latest sensitive active even if another app qualifies.
709 if (current->startTimeNs > latestSensitiveStartNs
Philip P. Moltmannbda45752020-07-17 16:41:18 -0700710 || (isInCommunication && currentUid == mPhoneStateOwnerUid)) {
Eric Laurentb809a752020-06-29 09:53:13 -0700711 if (!isInCommunication || latestSensitiveActiveOrComm == nullptr
Philip P. Moltmannbda45752020-07-17 16:41:18 -0700712 || VALUE_OR_FATAL(aidl2legacy_int32_t_uid_t(
Svet Ganov33761132021-05-13 22:51:08 +0000713 latestSensitiveActiveOrComm->attributionSource.uid))
Philip P. Moltmannbda45752020-07-17 16:41:18 -0700714 != mPhoneStateOwnerUid) {
Eric Laurentb809a752020-06-29 09:53:13 -0700715 latestSensitiveActiveOrComm = current;
716 latestSensitiveStartNs = current->startTimeNs;
717 }
Eric Laurentc21d5692020-02-25 10:24:36 -0800718 }
719 isSensitiveActive = true;
720 } else {
721 if (current->startTimeNs > latestStartNs) {
722 latestActive = current;
723 latestStartNs = current->startTimeNs;
724 }
725 }
Eric Laurent4eb58f12018-12-07 16:41:02 -0800726 }
727 }
Eric Laurent4e947da2019-10-17 15:24:06 -0700728 if (current->attributes.source != AUDIO_SOURCE_HOTWORD) {
729 onlyHotwordActive = false;
730 }
Philip P. Moltmannbda45752020-07-17 16:41:18 -0700731 if (currentUid == mPhoneStateOwnerUid) {
Eric Laurentb809a752020-06-29 09:53:13 -0700732 isPhoneStateOwnerActive = true;
733 }
Eric Laurent4eb58f12018-12-07 16:41:02 -0800734 }
735
Eric Laurent1ff16a72019-03-14 18:35:04 -0700736 // if no active client with UI on Top, consider latest active as top
737 if (topActive == nullptr) {
738 topActive = latestActive;
Eric Laurentc21d5692020-02-25 10:24:36 -0800739 topStartNs = latestStartNs;
740 }
741 if (topSensitiveActive == nullptr) {
Eric Laurentb809a752020-06-29 09:53:13 -0700742 topSensitiveActive = latestSensitiveActiveOrComm;
Eric Laurentc21d5692020-02-25 10:24:36 -0800743 topSensitiveStartNs = latestSensitiveStartNs;
Eric Laurentb809a752020-06-29 09:53:13 -0700744 } else if (latestSensitiveActiveOrComm != nullptr) {
745 // if audio mode is IN_COMMUNICATION, favor audio mode owner over an app with
746 // foreground UI in case both are capturing with privacy sensitive flag.
Philip P. Moltmannbda45752020-07-17 16:41:18 -0700747 uid_t latestActiveUid = VALUE_OR_FATAL(
Svet Ganov33761132021-05-13 22:51:08 +0000748 aidl2legacy_int32_t_uid_t(latestSensitiveActiveOrComm->attributionSource.uid));
Philip P. Moltmannbda45752020-07-17 16:41:18 -0700749 if (isInCommunication && latestActiveUid == mPhoneStateOwnerUid) {
Eric Laurentb809a752020-06-29 09:53:13 -0700750 topSensitiveActive = latestSensitiveActiveOrComm;
751 topSensitiveStartNs = latestSensitiveStartNs;
752 }
Eric Laurentc21d5692020-02-25 10:24:36 -0800753 }
754
755 // If both privacy sensitive and regular capture are active:
756 // if the regular capture is privileged
757 // allow concurrency
758 // else
759 // favor the privacy sensitive case
760 if (topActive != nullptr && topSensitiveActive != nullptr
Ricardo Correa57a37692020-03-23 17:27:25 -0700761 && !topActive->canCaptureOutput) {
Eric Laurentc21d5692020-02-25 10:24:36 -0800762 topActive = nullptr;
Eric Laurent4eb58f12018-12-07 16:41:02 -0800763 }
764
765 for (size_t i =0; i < mAudioRecordClients.size(); i++) {
766 sp<AudioRecordClient> current = mAudioRecordClients[i];
Philip P. Moltmannbda45752020-07-17 16:41:18 -0700767 uid_t currentUid = VALUE_OR_FATAL(aidl2legacy_int32_t_uid_t(
Svet Ganov33761132021-05-13 22:51:08 +0000768 current->attributionSource.uid));
Eric Laurent1ff16a72019-03-14 18:35:04 -0700769 if (!current->active) {
770 continue;
771 }
772
Eric Laurent4eb58f12018-12-07 16:41:02 -0800773 audio_source_t source = current->attributes.source;
Philip P. Moltmannbda45752020-07-17 16:41:18 -0700774 bool isTopOrLatestActive = topActive == nullptr ? false :
Svet Ganov33761132021-05-13 22:51:08 +0000775 current->attributionSource.uid == topActive->attributionSource.uid;
Philip P. Moltmannbda45752020-07-17 16:41:18 -0700776 bool isTopOrLatestSensitive = topSensitiveActive == nullptr ? false :
Svet Ganov33761132021-05-13 22:51:08 +0000777 current->attributionSource.uid == topSensitiveActive->attributionSource.uid;
Eric Laurentc21d5692020-02-25 10:24:36 -0800778
Mikhail Naganov12b716c2020-04-30 22:37:43 +0000779 auto canCaptureIfInCallOrCommunication = [&](const auto &recordClient) REQUIRES(mLock) {
Philip P. Moltmannbda45752020-07-17 16:41:18 -0700780 uid_t recordUid = VALUE_OR_FATAL(aidl2legacy_int32_t_uid_t(
Svet Ganov33761132021-05-13 22:51:08 +0000781 recordClient->attributionSource.uid));
Ricardo Correa57a37692020-03-23 17:27:25 -0700782 bool canCaptureCall = recordClient->canCaptureOutput;
Eric Laurentb809a752020-06-29 09:53:13 -0700783 bool canCaptureCommunication = recordClient->canCaptureOutput
784 || !isPhoneStateOwnerActive
Philip P. Moltmannbda45752020-07-17 16:41:18 -0700785 || recordUid == mPhoneStateOwnerUid;
Eric Laurentb809a752020-06-29 09:53:13 -0700786 return !(isInCall && !canCaptureCall)
787 && !(isInCommunication && !canCaptureCommunication);
Eric Laurentc21d5692020-02-25 10:24:36 -0800788 };
Eric Laurent1ff16a72019-03-14 18:35:04 -0700789
790 // By default allow capture if:
791 // The assistant is not on TOP
Eric Laurenta171e352019-05-07 13:04:45 -0700792 // AND is on TOP or latest started
Eric Laurent1ff16a72019-03-14 18:35:04 -0700793 // AND there is no active privacy sensitive capture or call
794 // OR client has CAPTURE_AUDIO_OUTPUT privileged permission
795 bool allowCapture = !isAssistantOnTop
Eric Laurentc21d5692020-02-25 10:24:36 -0800796 && (isTopOrLatestActive || isTopOrLatestSensitive)
797 && !(isSensitiveActive
Ricardo Correa57a37692020-03-23 17:27:25 -0700798 && !(isTopOrLatestSensitive || current->canCaptureOutput))
Eric Laurentc21d5692020-02-25 10:24:36 -0800799 && canCaptureIfInCallOrCommunication(current);
Eric Laurent2dc962b2019-03-01 08:25:25 -0800800
Eric Laurented726cc2021-07-01 14:26:41 +0200801 if (!current->hasOp()) {
802 // Never allow capture if app op is denied
803 allowCapture = false;
804 } else if (isVirtualSource(source)) {
Eric Laurent1ff16a72019-03-14 18:35:04 -0700805 // Allow capture for virtual (remote submix, call audio TX or RX...) sources
806 allowCapture = true;
Philip P. Moltmannbda45752020-07-17 16:41:18 -0700807 } else if (mUidPolicy->isAssistantUid(currentUid)) {
Eric Laurent1ff16a72019-03-14 18:35:04 -0700808 // For assistant allow capture if:
Eric Laurent6ede98f2019-06-11 14:50:30 -0700809 // An accessibility service is on TOP or a RTT call is active
Eric Laurent1ff16a72019-03-14 18:35:04 -0700810 // AND the source is VOICE_RECOGNITION or HOTWORD
Eric Laurenta171e352019-05-07 13:04:45 -0700811 // OR is on TOP AND uses VOICE_RECOGNITION
Eric Laurent1ff16a72019-03-14 18:35:04 -0700812 // OR uses HOTWORD
813 // AND there is no active privacy sensitive capture or call
814 // OR client has CAPTURE_AUDIO_OUTPUT privileged permission
Eric Laurent6ede98f2019-06-11 14:50:30 -0700815 if (isA11yOnTop || rttCallActive) {
Eric Laurent4eb58f12018-12-07 16:41:02 -0800816 if (source == AUDIO_SOURCE_HOTWORD || source == AUDIO_SOURCE_VOICE_RECOGNITION) {
Eric Laurent1ff16a72019-03-14 18:35:04 -0700817 allowCapture = true;
Eric Laurent4eb58f12018-12-07 16:41:02 -0800818 }
819 } else {
Eric Laurenta171e352019-05-07 13:04:45 -0700820 if (((isAssistantOnTop && source == AUDIO_SOURCE_VOICE_RECOGNITION) ||
Eric Laurentc21d5692020-02-25 10:24:36 -0800821 source == AUDIO_SOURCE_HOTWORD)
Ricardo Correa57a37692020-03-23 17:27:25 -0700822 && !(isSensitiveActive && !current->canCaptureOutput)
Eric Laurentc21d5692020-02-25 10:24:36 -0800823 && canCaptureIfInCallOrCommunication(current)) {
Eric Laurent1ff16a72019-03-14 18:35:04 -0700824 allowCapture = true;
Eric Laurent4eb58f12018-12-07 16:41:02 -0800825 }
826 }
Philip P. Moltmannbda45752020-07-17 16:41:18 -0700827 } else if (mUidPolicy->isA11yUid(currentUid)) {
Eric Laurent1ff16a72019-03-14 18:35:04 -0700828 // For accessibility service allow capture if:
Eric Laurent47670c92019-08-28 16:59:05 -0700829 // The assistant is not on TOP
830 // AND there is no active privacy sensitive capture or call
Eric Laurent589171c2019-07-25 18:04:29 -0700831 // OR client has CAPTURE_AUDIO_OUTPUT privileged permission
Eric Laurent47670c92019-08-28 16:59:05 -0700832 // OR
833 // Is on TOP AND the source is VOICE_RECOGNITION or HOTWORD
834 if (!isAssistantOnTop
Ricardo Correa57a37692020-03-23 17:27:25 -0700835 && !(isSensitiveActive && !current->canCaptureOutput)
Eric Laurentc21d5692020-02-25 10:24:36 -0800836 && canCaptureIfInCallOrCommunication(current)) {
Eric Laurent47670c92019-08-28 16:59:05 -0700837 allowCapture = true;
838 }
Eric Laurent589171c2019-07-25 18:04:29 -0700839 if (isA11yOnTop) {
840 if (source == AUDIO_SOURCE_VOICE_RECOGNITION || source == AUDIO_SOURCE_HOTWORD) {
841 allowCapture = true;
842 }
Eric Laurent4eb58f12018-12-07 16:41:02 -0800843 }
Eric Laurent4e947da2019-10-17 15:24:06 -0700844 } else if (source == AUDIO_SOURCE_HOTWORD) {
845 // For HOTWORD source allow capture when not on TOP if:
846 // All active clients are using HOTWORD source
847 // AND no call is active
848 // OR client has CAPTURE_AUDIO_OUTPUT privileged permission
Eric Laurentc21d5692020-02-25 10:24:36 -0800849 if (onlyHotwordActive
850 && canCaptureIfInCallOrCommunication(current)) {
Eric Laurent4e947da2019-10-17 15:24:06 -0700851 allowCapture = true;
852 }
Philip P. Moltmannbda45752020-07-17 16:41:18 -0700853 } else if (mUidPolicy->isCurrentImeUid(currentUid)) {
Kohsuke Yatoha623a132020-03-24 20:10:26 -0700854 // For current InputMethodService allow capture if:
855 // A RTT call is active AND the source is VOICE_RECOGNITION
856 if (rttCallActive && source == AUDIO_SOURCE_VOICE_RECOGNITION) {
857 allowCapture = true;
858 }
Eric Laurent4eb58f12018-12-07 16:41:02 -0800859 }
Eric Laurent8c7ef892021-06-10 13:32:16 +0200860 setAppState_l(current,
Philip P. Moltmannbda45752020-07-17 16:41:18 -0700861 allowCapture ? apmStatFromAmState(mUidPolicy->getUidState(currentUid)) :
Eric Laurent1ff16a72019-03-14 18:35:04 -0700862 APP_STATE_IDLE);
Eric Laurente8c8b432018-10-17 10:08:02 -0700863 }
864}
865
Michael Groovercfd28302018-12-11 19:16:46 -0800866void AudioPolicyService::silenceAllRecordings_l() {
867 for (size_t i = 0; i < mAudioRecordClients.size(); i++) {
868 sp<AudioRecordClient> current = mAudioRecordClients[i];
Eric Laurent1ff16a72019-03-14 18:35:04 -0700869 if (!isVirtualSource(current->attributes.source)) {
Eric Laurent8c7ef892021-06-10 13:32:16 +0200870 setAppState_l(current, APP_STATE_IDLE);
Eric Laurent1ff16a72019-03-14 18:35:04 -0700871 }
Michael Groovercfd28302018-12-11 19:16:46 -0800872 }
873}
874
Eric Laurente8c8b432018-10-17 10:08:02 -0700875/* static */
876app_state_t AudioPolicyService::apmStatFromAmState(int amState) {
Eric Laurent1ff16a72019-03-14 18:35:04 -0700877
878 if (amState == ActivityManager::PROCESS_STATE_UNKNOWN) {
Eric Laurente8c8b432018-10-17 10:08:02 -0700879 return APP_STATE_IDLE;
Eric Laurent1ff16a72019-03-14 18:35:04 -0700880 } else if (amState <= ActivityManager::PROCESS_STATE_TOP) {
881 // include persistent services
882 return APP_STATE_TOP;
Eric Laurente8c8b432018-10-17 10:08:02 -0700883 }
884 return APP_STATE_FOREGROUND;
885}
886
Eric Laurent4eb58f12018-12-07 16:41:02 -0800887/* static */
Eric Laurent2dc962b2019-03-01 08:25:25 -0800888bool AudioPolicyService::isVirtualSource(audio_source_t source)
Eric Laurent4eb58f12018-12-07 16:41:02 -0800889{
890 switch (source) {
891 case AUDIO_SOURCE_VOICE_UPLINK:
892 case AUDIO_SOURCE_VOICE_DOWNLINK:
893 case AUDIO_SOURCE_VOICE_CALL:
Eric Laurent2dc962b2019-03-01 08:25:25 -0800894 case AUDIO_SOURCE_REMOTE_SUBMIX:
895 case AUDIO_SOURCE_FM_TUNER:
Eric Laurent68eb2122020-04-30 17:40:57 -0700896 case AUDIO_SOURCE_ECHO_REFERENCE:
Eric Laurent4eb58f12018-12-07 16:41:02 -0800897 return true;
898 default:
899 break;
900 }
901 return false;
902}
903
Eric Laurented726cc2021-07-01 14:26:41 +0200904/* static */
905bool AudioPolicyService::isAppOpSource(audio_source_t source)
906{
907 switch (source) {
908 case AUDIO_SOURCE_FM_TUNER:
909 case AUDIO_SOURCE_ECHO_REFERENCE:
910 return false;
911 default:
912 break;
913 }
914 return true;
915}
916
Eric Laurent8c7ef892021-06-10 13:32:16 +0200917void AudioPolicyService::setAppState_l(sp<AudioRecordClient> client, app_state_t state)
Eric Laurente8c8b432018-10-17 10:08:02 -0700918{
919 AutoCallerClear acc;
920
921 if (mAudioPolicyManager) {
Eric Laurent8c7ef892021-06-10 13:32:16 +0200922 mAudioPolicyManager->setAppState(client->portId, state);
Mikhail Naganoveae73eb2018-04-03 16:57:36 -0700923 }
924 sp<IAudioFlinger> af = AudioSystem::get_audio_flinger();
925 if (af) {
Eric Laurentf32108e2018-10-04 17:22:04 -0700926 bool silenced = state == APP_STATE_IDLE;
Eric Laurent8c7ef892021-06-10 13:32:16 +0200927 if (client->silenced != silenced) {
928 if (client->active) {
929 if (silenced) {
930 finishRecording(client->attributionSource, client->attributes.source);
931 } else {
932 std::stringstream msg;
933 msg << "Audio recording un-silenced on session " << client->session;
934 if (!startRecording(client->attributionSource, String16(msg.str().c_str()),
935 client->attributes.source)) {
936 silenced = true;
937 }
938 }
939 }
940 af->setRecordSilenced(client->portId, silenced);
941 client->silenced = silenced;
942 }
Mikhail Naganoveae73eb2018-04-03 16:57:36 -0700943 }
Svet Ganovf4ddfef2018-01-16 07:37:58 -0800944}
945
Glenn Kasten0f11b512014-01-31 16:18:54 -0800946status_t AudioPolicyService::dump(int fd, const Vector<String16>& args __unused)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700947{
Glenn Kasten44deb052012-02-05 18:09:08 -0800948 if (!dumpAllowed()) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700949 dumpPermissionDenial(fd);
950 } else {
Mikhail Naganov12b716c2020-04-30 22:37:43 +0000951 const bool locked = dumpTryLock(mLock);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700952 if (!locked) {
953 String8 result(kDeadlockedString);
954 write(fd, result.string(), result.size());
955 }
956
957 dumpInternals(fd);
Glenn Kasten9d1f02d2012-02-08 17:47:58 -0800958 if (mAudioCommandThread != 0) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700959 mAudioCommandThread->dump(fd);
960 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700961
Eric Laurentdce54a12014-03-10 12:19:46 -0700962 if (mAudioPolicyManager) {
963 mAudioPolicyManager->dump(fd);
964 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700965
Kevin Rocard8be94972019-02-22 13:26:25 -0800966 mPackageManager.dump(fd);
967
Mikhail Naganov12b716c2020-04-30 22:37:43 +0000968 dumpReleaseLock(mLock, locked);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700969 }
970 return NO_ERROR;
971}
972
973status_t AudioPolicyService::dumpPermissionDenial(int fd)
974{
975 const size_t SIZE = 256;
976 char buffer[SIZE];
977 String8 result;
978 snprintf(buffer, SIZE, "Permission Denial: "
979 "can't dump AudioPolicyService from pid=%d, uid=%d\n",
980 IPCThreadState::self()->getCallingPid(),
981 IPCThreadState::self()->getCallingUid());
982 result.append(buffer);
983 write(fd, result.string(), result.size());
984 return NO_ERROR;
985}
986
987status_t AudioPolicyService::onTransact(
Svet Ganovf4ddfef2018-01-16 07:37:58 -0800988 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags) {
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800989 // make sure transactions reserved to AudioFlinger do not come from other processes
990 switch (code) {
991 case TRANSACTION_startOutput:
992 case TRANSACTION_stopOutput:
993 case TRANSACTION_releaseOutput:
994 case TRANSACTION_getInputForAttr:
995 case TRANSACTION_startInput:
996 case TRANSACTION_stopInput:
997 case TRANSACTION_releaseInput:
998 case TRANSACTION_getOutputForEffect:
999 case TRANSACTION_registerEffect:
1000 case TRANSACTION_unregisterEffect:
1001 case TRANSACTION_setEffectEnabled:
1002 case TRANSACTION_getStrategyForStream:
1003 case TRANSACTION_getOutputForAttr:
1004 case TRANSACTION_moveEffectsToIo:
1005 ALOGW("%s: transaction %d received from PID %d",
1006 __func__, code, IPCThreadState::self()->getCallingPid());
1007 return INVALID_OPERATION;
1008 default:
1009 break;
1010 }
1011
1012 // make sure the following transactions come from system components
1013 switch (code) {
1014 case TRANSACTION_setDeviceConnectionState:
1015 case TRANSACTION_handleDeviceConfigChange:
1016 case TRANSACTION_setPhoneState:
1017//FIXME: Allow setForceUse calls from system apps until a better use case routing API is available
1018// case TRANSACTION_setForceUse:
1019 case TRANSACTION_initStreamVolume:
1020 case TRANSACTION_setStreamVolumeIndex:
1021 case TRANSACTION_setVolumeIndexForAttributes:
1022 case TRANSACTION_getStreamVolumeIndex:
1023 case TRANSACTION_getVolumeIndexForAttributes:
1024 case TRANSACTION_getMinVolumeIndexForAttributes:
1025 case TRANSACTION_getMaxVolumeIndexForAttributes:
1026 case TRANSACTION_isStreamActive:
1027 case TRANSACTION_isStreamActiveRemotely:
1028 case TRANSACTION_isSourceActive:
1029 case TRANSACTION_getDevicesForStream:
1030 case TRANSACTION_registerPolicyMixes:
1031 case TRANSACTION_setMasterMono:
1032 case TRANSACTION_getSurroundFormats:
Kriti Dang6537def2021-03-02 13:46:59 +01001033 case TRANSACTION_getReportedSurroundFormats:
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001034 case TRANSACTION_setSurroundFormatEnabled:
1035 case TRANSACTION_setAssistantUid:
1036 case TRANSACTION_setA11yServicesUids:
1037 case TRANSACTION_setUidDeviceAffinities:
1038 case TRANSACTION_removeUidDeviceAffinities:
1039 case TRANSACTION_setUserIdDeviceAffinities:
1040 case TRANSACTION_removeUserIdDeviceAffinities:
1041 case TRANSACTION_getHwOffloadEncodingFormatsSupportedForA2DP:
1042 case TRANSACTION_listAudioVolumeGroups:
1043 case TRANSACTION_getVolumeGroupFromAudioAttributes:
1044 case TRANSACTION_acquireSoundTriggerSession:
1045 case TRANSACTION_releaseSoundTriggerSession:
1046 case TRANSACTION_setRttEnabled:
1047 case TRANSACTION_isCallScreenModeSupported:
1048 case TRANSACTION_setDevicesRoleForStrategy:
1049 case TRANSACTION_setSupportedSystemUsages:
1050 case TRANSACTION_removeDevicesRoleForStrategy:
1051 case TRANSACTION_getDevicesForRoleAndStrategy:
1052 case TRANSACTION_getDevicesForAttributes:
1053 case TRANSACTION_setAllowedCapturePolicy:
1054 case TRANSACTION_onNewAudioModulesAvailable:
1055 case TRANSACTION_setCurrentImeUid:
1056 case TRANSACTION_registerSoundTriggerCaptureStateListener:
1057 case TRANSACTION_setDevicesRoleForCapturePreset:
1058 case TRANSACTION_addDevicesRoleForCapturePreset:
1059 case TRANSACTION_removeDevicesRoleForCapturePreset:
1060 case TRANSACTION_clearDevicesRoleForCapturePreset:
Eric Laurent6d607012021-07-05 11:54:40 +02001061 case TRANSACTION_getDevicesForRoleAndCapturePreset:
1062 case TRANSACTION_getSpatializer: {
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001063 if (!isServiceUid(IPCThreadState::self()->getCallingUid())) {
1064 ALOGW("%s: transaction %d received from PID %d unauthorized UID %d",
1065 __func__, code, IPCThreadState::self()->getCallingPid(),
1066 IPCThreadState::self()->getCallingUid());
1067 return INVALID_OPERATION;
1068 }
1069 } break;
1070 default:
1071 break;
1072 }
1073
1074 std::string tag("IAudioPolicyService command " + std::to_string(code));
1075 TimeCheck check(tag.c_str());
1076
Svet Ganovf4ddfef2018-01-16 07:37:58 -08001077 switch (code) {
1078 case SHELL_COMMAND_TRANSACTION: {
1079 int in = data.readFileDescriptor();
1080 int out = data.readFileDescriptor();
1081 int err = data.readFileDescriptor();
1082 int argc = data.readInt32();
1083 Vector<String16> args;
1084 for (int i = 0; i < argc && data.dataAvail() > 0; i++) {
1085 args.add(data.readString16());
1086 }
1087 sp<IBinder> unusedCallback;
1088 sp<IResultReceiver> resultReceiver;
1089 status_t status;
1090 if ((status = data.readNullableStrongBinder(&unusedCallback)) != NO_ERROR) {
1091 return status;
1092 }
1093 if ((status = data.readNullableStrongBinder(&resultReceiver)) != NO_ERROR) {
1094 return status;
1095 }
1096 status = shellCommand(in, out, err, args);
1097 if (resultReceiver != nullptr) {
1098 resultReceiver->send(status);
1099 }
1100 return NO_ERROR;
1101 }
1102 }
1103
Mathias Agopian65ab4712010-07-14 17:59:35 -07001104 return BnAudioPolicyService::onTransact(code, data, reply, flags);
1105}
1106
Svet Ganovf4ddfef2018-01-16 07:37:58 -08001107// ------------------- Shell command implementation -------------------
1108
1109// NOTE: This is a remote API - make sure all args are validated
1110status_t AudioPolicyService::shellCommand(int in, int out, int err, Vector<String16>& args) {
1111 if (!checkCallingPermission(sManageAudioPolicyPermission, nullptr, nullptr)) {
1112 return PERMISSION_DENIED;
1113 }
1114 if (in == BAD_TYPE || out == BAD_TYPE || err == BAD_TYPE) {
1115 return BAD_VALUE;
1116 }
jovanakbe066e12019-09-02 11:54:39 -07001117 if (args.size() >= 3 && args[0] == String16("set-uid-state")) {
Svet Ganovf4ddfef2018-01-16 07:37:58 -08001118 return handleSetUidState(args, err);
jovanakbe066e12019-09-02 11:54:39 -07001119 } else if (args.size() >= 2 && args[0] == String16("reset-uid-state")) {
Svet Ganovf4ddfef2018-01-16 07:37:58 -08001120 return handleResetUidState(args, err);
jovanakbe066e12019-09-02 11:54:39 -07001121 } else if (args.size() >= 2 && args[0] == String16("get-uid-state")) {
Svet Ganovf4ddfef2018-01-16 07:37:58 -08001122 return handleGetUidState(args, out, err);
Eric Laurent269acb42021-04-23 16:53:22 +02001123 } else if (args.size() >= 1 && args[0] == String16("purge_permission-cache")) {
1124 purgePermissionCache();
1125 return NO_ERROR;
Svet Ganovf4ddfef2018-01-16 07:37:58 -08001126 } else if (args.size() == 1 && args[0] == String16("help")) {
1127 printHelp(out);
1128 return NO_ERROR;
1129 }
1130 printHelp(err);
1131 return BAD_VALUE;
1132}
1133
jovanakbe066e12019-09-02 11:54:39 -07001134static status_t getUidForPackage(String16 packageName, int userId, /*inout*/uid_t& uid, int err) {
1135 if (userId < 0) {
1136 ALOGE("Invalid user: %d", userId);
1137 dprintf(err, "Invalid user: %d\n", userId);
Svet Ganovf4ddfef2018-01-16 07:37:58 -08001138 return BAD_VALUE;
1139 }
jovanakbe066e12019-09-02 11:54:39 -07001140
1141 PermissionController pc;
1142 uid = pc.getPackageUid(packageName, 0);
1143 if (uid <= 0) {
1144 ALOGE("Unknown package: '%s'", String8(packageName).string());
1145 dprintf(err, "Unknown package: '%s'\n", String8(packageName).string());
1146 return BAD_VALUE;
1147 }
1148
1149 uid = multiuser_get_uid(userId, uid);
1150 return NO_ERROR;
1151}
1152
1153status_t AudioPolicyService::handleSetUidState(Vector<String16>& args, int err) {
1154 // Valid arg.size() is 3 or 5, args.size() is 5 with --user option.
1155 if (!(args.size() == 3 || args.size() == 5)) {
1156 printHelp(err);
1157 return BAD_VALUE;
1158 }
1159
Svet Ganovf4ddfef2018-01-16 07:37:58 -08001160 bool active = false;
1161 if (args[2] == String16("active")) {
1162 active = true;
1163 } else if ((args[2] != String16("idle"))) {
1164 ALOGE("Expected active or idle but got: '%s'", String8(args[2]).string());
1165 return BAD_VALUE;
1166 }
jovanakbe066e12019-09-02 11:54:39 -07001167
1168 int userId = 0;
1169 if (args.size() >= 5 && args[3] == String16("--user")) {
1170 userId = atoi(String8(args[4]));
1171 }
1172
1173 uid_t uid;
1174 if (getUidForPackage(args[1], userId, uid, err) == BAD_VALUE) {
1175 return BAD_VALUE;
1176 }
1177
Mikhail Naganov12b716c2020-04-30 22:37:43 +00001178 sp<UidPolicy> uidPolicy;
1179 {
1180 Mutex::Autolock _l(mLock);
1181 uidPolicy = mUidPolicy;
1182 }
1183 if (uidPolicy) {
1184 uidPolicy->addOverrideUid(uid, active);
1185 return NO_ERROR;
1186 }
1187 return NO_INIT;
Svet Ganovf4ddfef2018-01-16 07:37:58 -08001188}
1189
1190status_t AudioPolicyService::handleResetUidState(Vector<String16>& args, int err) {
jovanakbe066e12019-09-02 11:54:39 -07001191 // Valid arg.size() is 2 or 4, args.size() is 4 with --user option.
1192 if (!(args.size() == 2 || args.size() == 4)) {
1193 printHelp(err);
Svet Ganovf4ddfef2018-01-16 07:37:58 -08001194 return BAD_VALUE;
1195 }
jovanakbe066e12019-09-02 11:54:39 -07001196
1197 int userId = 0;
1198 if (args.size() >= 4 && args[2] == String16("--user")) {
1199 userId = atoi(String8(args[3]));
1200 }
1201
1202 uid_t uid;
1203 if (getUidForPackage(args[1], userId, uid, err) == BAD_VALUE) {
1204 return BAD_VALUE;
1205 }
1206
Mikhail Naganov12b716c2020-04-30 22:37:43 +00001207 sp<UidPolicy> uidPolicy;
1208 {
1209 Mutex::Autolock _l(mLock);
1210 uidPolicy = mUidPolicy;
1211 }
1212 if (uidPolicy) {
1213 uidPolicy->removeOverrideUid(uid);
1214 return NO_ERROR;
1215 }
1216 return NO_INIT;
Svet Ganovf4ddfef2018-01-16 07:37:58 -08001217}
1218
1219status_t AudioPolicyService::handleGetUidState(Vector<String16>& args, int out, int err) {
jovanakbe066e12019-09-02 11:54:39 -07001220 // Valid arg.size() is 2 or 4, args.size() is 4 with --user option.
1221 if (!(args.size() == 2 || args.size() == 4)) {
1222 printHelp(err);
Svet Ganovf4ddfef2018-01-16 07:37:58 -08001223 return BAD_VALUE;
1224 }
jovanakbe066e12019-09-02 11:54:39 -07001225
1226 int userId = 0;
1227 if (args.size() >= 4 && args[2] == String16("--user")) {
1228 userId = atoi(String8(args[3]));
1229 }
1230
1231 uid_t uid;
1232 if (getUidForPackage(args[1], userId, uid, err) == BAD_VALUE) {
1233 return BAD_VALUE;
1234 }
1235
Mikhail Naganov12b716c2020-04-30 22:37:43 +00001236 sp<UidPolicy> uidPolicy;
1237 {
1238 Mutex::Autolock _l(mLock);
1239 uidPolicy = mUidPolicy;
Svet Ganovf4ddfef2018-01-16 07:37:58 -08001240 }
Mikhail Naganov12b716c2020-04-30 22:37:43 +00001241 if (uidPolicy) {
1242 return dprintf(out, uidPolicy->isUidActive(uid) ? "active\n" : "idle\n");
1243 }
1244 return NO_INIT;
Svet Ganovf4ddfef2018-01-16 07:37:58 -08001245}
1246
1247status_t AudioPolicyService::printHelp(int out) {
1248 return dprintf(out, "Audio policy service commands:\n"
jovanakbe066e12019-09-02 11:54:39 -07001249 " get-uid-state <PACKAGE> [--user USER_ID] gets the uid state\n"
1250 " set-uid-state <PACKAGE> <active|idle> [--user USER_ID] overrides the uid state\n"
1251 " reset-uid-state <PACKAGE> [--user USER_ID] clears the uid state override\n"
Svet Ganovf4ddfef2018-01-16 07:37:58 -08001252 " help print this message\n");
1253}
1254
1255// ----------- AudioPolicyService::UidPolicy implementation ----------
1256
1257void AudioPolicyService::UidPolicy::registerSelf() {
Steven Moreland2f348142019-07-02 15:59:07 -07001258 status_t res = mAm.linkToDeath(this);
1259 mAm.registerUidObserver(this, ActivityManager::UID_OBSERVER_GONE
Svet Ganovf4ddfef2018-01-16 07:37:58 -08001260 | ActivityManager::UID_OBSERVER_IDLE
Eric Laurente8c8b432018-10-17 10:08:02 -07001261 | ActivityManager::UID_OBSERVER_ACTIVE
1262 | ActivityManager::UID_OBSERVER_PROCSTATE,
Svet Ganovf4ddfef2018-01-16 07:37:58 -08001263 ActivityManager::PROCESS_STATE_UNKNOWN,
1264 String16("audioserver"));
Mikhail Naganoveae73eb2018-04-03 16:57:36 -07001265 if (!res) {
1266 Mutex::Autolock _l(mLock);
1267 mObserverRegistered = true;
1268 } else {
1269 ALOGE("UidPolicy::registerSelf linkToDeath failed: %d", res);
Eric Laurent4eb58f12018-12-07 16:41:02 -08001270
Steven Moreland2f348142019-07-02 15:59:07 -07001271 mAm.unregisterUidObserver(this);
Mikhail Naganoveae73eb2018-04-03 16:57:36 -07001272 }
Svet Ganovf4ddfef2018-01-16 07:37:58 -08001273}
1274
1275void AudioPolicyService::UidPolicy::unregisterSelf() {
Steven Moreland2f348142019-07-02 15:59:07 -07001276 mAm.unlinkToDeath(this);
1277 mAm.unregisterUidObserver(this);
Mikhail Naganoveae73eb2018-04-03 16:57:36 -07001278 Mutex::Autolock _l(mLock);
1279 mObserverRegistered = false;
Svet Ganovf4ddfef2018-01-16 07:37:58 -08001280}
1281
Mikhail Naganoveae73eb2018-04-03 16:57:36 -07001282void AudioPolicyService::UidPolicy::binderDied(__unused const wp<IBinder> &who) {
1283 Mutex::Autolock _l(mLock);
1284 mCachedUids.clear();
1285 mObserverRegistered = false;
Svet Ganovf4ddfef2018-01-16 07:37:58 -08001286}
1287
Eric Laurente8c8b432018-10-17 10:08:02 -07001288void AudioPolicyService::UidPolicy::checkRegistered() {
Mikhail Naganoveae73eb2018-04-03 16:57:36 -07001289 bool needToReregister = false;
1290 {
1291 Mutex::Autolock _l(mLock);
1292 needToReregister = !mObserverRegistered;
Svet Ganovf4ddfef2018-01-16 07:37:58 -08001293 }
Mikhail Naganoveae73eb2018-04-03 16:57:36 -07001294 if (needToReregister) {
1295 // Looks like ActivityManager has died previously, attempt to re-register.
1296 registerSelf();
1297 }
Eric Laurente8c8b432018-10-17 10:08:02 -07001298}
1299
1300bool AudioPolicyService::UidPolicy::isUidActive(uid_t uid) {
1301 if (isServiceUid(uid)) return true;
1302 checkRegistered();
Mikhail Naganoveae73eb2018-04-03 16:57:36 -07001303 {
1304 Mutex::Autolock _l(mLock);
1305 auto overrideIter = mOverrideUids.find(uid);
1306 if (overrideIter != mOverrideUids.end()) {
Eric Laurente8c8b432018-10-17 10:08:02 -07001307 return overrideIter->second.first;
Mikhail Naganoveae73eb2018-04-03 16:57:36 -07001308 }
1309 // In an absense of the ActivityManager, assume everything to be active.
1310 if (!mObserverRegistered) return true;
1311 auto cacheIter = mCachedUids.find(uid);
Mikhail Naganoveba668a2018-04-05 08:13:15 -07001312 if (cacheIter != mCachedUids.end()) {
Eric Laurente8c8b432018-10-17 10:08:02 -07001313 return cacheIter->second.first;
Mikhail Naganoveae73eb2018-04-03 16:57:36 -07001314 }
1315 }
1316 ActivityManager am;
Hui Yu12c7ec72020-05-04 17:40:52 +00001317 bool active = am.isUidActive(uid, String16("audioserver"));
Mikhail Naganoveae73eb2018-04-03 16:57:36 -07001318 {
1319 Mutex::Autolock _l(mLock);
Eric Laurente8c8b432018-10-17 10:08:02 -07001320 mCachedUids.insert(std::pair<uid_t,
1321 std::pair<bool, int>>(uid, std::pair<bool, int>(active,
1322 ActivityManager::PROCESS_STATE_UNKNOWN)));
Mikhail Naganoveae73eb2018-04-03 16:57:36 -07001323 }
1324 return active;
Svet Ganovf4ddfef2018-01-16 07:37:58 -08001325}
1326
Eric Laurente8c8b432018-10-17 10:08:02 -07001327int AudioPolicyService::UidPolicy::getUidState(uid_t uid) {
1328 if (isServiceUid(uid)) {
1329 return ActivityManager::PROCESS_STATE_TOP;
1330 }
1331 checkRegistered();
1332 {
1333 Mutex::Autolock _l(mLock);
1334 auto overrideIter = mOverrideUids.find(uid);
1335 if (overrideIter != mOverrideUids.end()) {
1336 if (overrideIter->second.first) {
1337 if (overrideIter->second.second != ActivityManager::PROCESS_STATE_UNKNOWN) {
1338 return overrideIter->second.second;
1339 } else {
1340 auto cacheIter = mCachedUids.find(uid);
1341 if (cacheIter != mCachedUids.end()) {
1342 return cacheIter->second.second;
1343 }
1344 }
1345 }
1346 return ActivityManager::PROCESS_STATE_UNKNOWN;
1347 }
1348 // In an absense of the ActivityManager, assume everything to be active.
1349 if (!mObserverRegistered) {
1350 return ActivityManager::PROCESS_STATE_TOP;
1351 }
1352 auto cacheIter = mCachedUids.find(uid);
1353 if (cacheIter != mCachedUids.end()) {
1354 if (cacheIter->second.first) {
1355 return cacheIter->second.second;
1356 } else {
1357 return ActivityManager::PROCESS_STATE_UNKNOWN;
1358 }
1359 }
1360 }
1361 ActivityManager am;
Hui Yu12c7ec72020-05-04 17:40:52 +00001362 bool active = am.isUidActive(uid, String16("audioserver"));
Eric Laurente8c8b432018-10-17 10:08:02 -07001363 int state = ActivityManager::PROCESS_STATE_UNKNOWN;
1364 if (active) {
1365 state = am.getUidProcessState(uid, String16("audioserver"));
1366 }
1367 {
1368 Mutex::Autolock _l(mLock);
1369 mCachedUids.insert(std::pair<uid_t,
1370 std::pair<bool, int>>(uid, std::pair<bool, int>(active, state)));
1371 }
Eric Laurent4eb58f12018-12-07 16:41:02 -08001372
Eric Laurente8c8b432018-10-17 10:08:02 -07001373 return state;
1374}
1375
Mikhail Naganoveae73eb2018-04-03 16:57:36 -07001376void AudioPolicyService::UidPolicy::onUidActive(uid_t uid) {
Eric Laurente8c8b432018-10-17 10:08:02 -07001377 updateUid(&mCachedUids, uid, true, ActivityManager::PROCESS_STATE_UNKNOWN, true);
Mikhail Naganoveae73eb2018-04-03 16:57:36 -07001378}
1379
1380void AudioPolicyService::UidPolicy::onUidGone(uid_t uid, __unused bool disabled) {
Eric Laurente8c8b432018-10-17 10:08:02 -07001381 updateUid(&mCachedUids, uid, false, ActivityManager::PROCESS_STATE_UNKNOWN, false);
Mikhail Naganoveae73eb2018-04-03 16:57:36 -07001382}
1383
1384void AudioPolicyService::UidPolicy::onUidIdle(uid_t uid, __unused bool disabled) {
Eric Laurente8c8b432018-10-17 10:08:02 -07001385 updateUid(&mCachedUids, uid, false, ActivityManager::PROCESS_STATE_UNKNOWN, true);
Mikhail Naganoveae73eb2018-04-03 16:57:36 -07001386}
1387
Eric Laurente8c8b432018-10-17 10:08:02 -07001388void AudioPolicyService::UidPolicy::onUidStateChanged(uid_t uid,
1389 int32_t procState,
Hui Yu13ad0eb2019-09-09 10:27:07 -07001390 int64_t procStateSeq __unused,
1391 int32_t capability __unused) {
Eric Laurente8c8b432018-10-17 10:08:02 -07001392 if (procState != ActivityManager::PROCESS_STATE_UNKNOWN) {
1393 updateUid(&mCachedUids, uid, true, procState, true);
Mikhail Naganoveae73eb2018-04-03 16:57:36 -07001394 }
1395}
1396
1397void AudioPolicyService::UidPolicy::updateOverrideUid(uid_t uid, bool active, bool insert) {
Eric Laurente8c8b432018-10-17 10:08:02 -07001398 updateUid(&mOverrideUids, uid, active, ActivityManager::PROCESS_STATE_UNKNOWN, insert);
1399}
1400
1401void AudioPolicyService::UidPolicy::notifyService() {
1402 sp<AudioPolicyService> service = mService.promote();
1403 if (service != nullptr) {
1404 service->updateUidStates();
Mikhail Naganoveae73eb2018-04-03 16:57:36 -07001405 }
1406}
1407
Eric Laurente8c8b432018-10-17 10:08:02 -07001408void AudioPolicyService::UidPolicy::updateUid(std::unordered_map<uid_t,
1409 std::pair<bool, int>> *uids,
1410 uid_t uid,
1411 bool active,
1412 int state,
1413 bool insert) {
1414 if (isServiceUid(uid)) {
1415 return;
1416 }
1417 bool wasActive = isUidActive(uid);
1418 int previousState = getUidState(uid);
Mikhail Naganoveae73eb2018-04-03 16:57:36 -07001419 {
1420 Mutex::Autolock _l(mLock);
Eric Laurente8c8b432018-10-17 10:08:02 -07001421 updateUidLocked(uids, uid, active, state, insert);
Mikhail Naganoveae73eb2018-04-03 16:57:36 -07001422 }
Eric Laurente8c8b432018-10-17 10:08:02 -07001423 if (wasActive != isUidActive(uid) || state != previousState) {
1424 notifyService();
1425 }
Mikhail Naganoveae73eb2018-04-03 16:57:36 -07001426}
1427
Eric Laurente8c8b432018-10-17 10:08:02 -07001428void AudioPolicyService::UidPolicy::updateUidLocked(std::unordered_map<uid_t,
1429 std::pair<bool, int>> *uids,
1430 uid_t uid,
1431 bool active,
1432 int state,
1433 bool insert) {
Mikhail Naganoveae73eb2018-04-03 16:57:36 -07001434 auto it = uids->find(uid);
1435 if (it != uids->end()) {
Mikhail Naganoveae73eb2018-04-03 16:57:36 -07001436 if (insert) {
Eric Laurente8c8b432018-10-17 10:08:02 -07001437 if (state == ActivityManager::PROCESS_STATE_UNKNOWN) {
1438 it->second.first = active;
1439 }
1440 if (it->second.first) {
1441 it->second.second = state;
1442 } else {
1443 it->second.second = ActivityManager::PROCESS_STATE_UNKNOWN;
1444 }
Mikhail Naganoveae73eb2018-04-03 16:57:36 -07001445 } else {
1446 uids->erase(it);
1447 }
Eric Laurente8c8b432018-10-17 10:08:02 -07001448 } else if (insert && (state == ActivityManager::PROCESS_STATE_UNKNOWN)) {
1449 uids->insert(std::pair<uid_t, std::pair<bool, int>>(uid,
1450 std::pair<bool, int>(active, state)));
Mikhail Naganoveae73eb2018-04-03 16:57:36 -07001451 }
Svet Ganovf4ddfef2018-01-16 07:37:58 -08001452}
Mathias Agopian65ab4712010-07-14 17:59:35 -07001453
Eric Laurent4eb58f12018-12-07 16:41:02 -08001454bool AudioPolicyService::UidPolicy::isA11yOnTop() {
1455 for (const auto &uid : mCachedUids) {
Eric Laurent47670c92019-08-28 16:59:05 -07001456 if (!isA11yUid(uid.first)) {
Eric Laurent4eb58f12018-12-07 16:41:02 -08001457 continue;
1458 }
Amith Yamasanibcbb3002019-01-23 13:53:33 -08001459 if (uid.second.second >= ActivityManager::PROCESS_STATE_TOP
1460 && uid.second.second <= ActivityManager::PROCESS_STATE_BOUND_FOREGROUND_SERVICE) {
Eric Laurent4eb58f12018-12-07 16:41:02 -08001461 return true;
1462 }
1463 }
1464 return false;
1465}
1466
Eric Laurentb78763e2018-10-17 10:08:02 -07001467bool AudioPolicyService::UidPolicy::isA11yUid(uid_t uid)
1468{
1469 std::vector<uid_t>::iterator it = find(mA11yUids.begin(), mA11yUids.end(), uid);
1470 return it != mA11yUids.end();
1471}
1472
Michael Groovercfd28302018-12-11 19:16:46 -08001473// ----------- AudioPolicyService::SensorPrivacyService implementation ----------
1474void AudioPolicyService::SensorPrivacyPolicy::registerSelf() {
1475 SensorPrivacyManager spm;
1476 mSensorPrivacyEnabled = spm.isSensorPrivacyEnabled();
1477 spm.addSensorPrivacyListener(this);
1478}
1479
Evan Severson241d9592021-01-08 12:16:02 -08001480void AudioPolicyService::SensorPrivacyPolicy::registerSelfForMicrophoneOnly(int userId) {
1481 SensorPrivacyManager spm;
1482 mSensorPrivacyEnabled = spm.isIndividualSensorPrivacyEnabled(userId,
1483 SensorPrivacyManager::INDIVIDUAL_SENSOR_MICROPHONE);
1484 spm.addIndividualSensorPrivacyListener(userId,
1485 SensorPrivacyManager::INDIVIDUAL_SENSOR_MICROPHONE, this);
1486}
1487
Michael Groovercfd28302018-12-11 19:16:46 -08001488void AudioPolicyService::SensorPrivacyPolicy::unregisterSelf() {
1489 SensorPrivacyManager spm;
1490 spm.removeSensorPrivacyListener(this);
1491}
1492
1493bool AudioPolicyService::SensorPrivacyPolicy::isSensorPrivacyEnabled() {
1494 return mSensorPrivacyEnabled;
1495}
1496
1497binder::Status AudioPolicyService::SensorPrivacyPolicy::onSensorPrivacyChanged(bool enabled) {
1498 mSensorPrivacyEnabled = enabled;
1499 sp<AudioPolicyService> service = mService.promote();
1500 if (service != nullptr) {
1501 service->updateUidStates();
1502 }
1503 return binder::Status::ok();
1504}
1505
Eric Laurented726cc2021-07-01 14:26:41 +02001506// ----------- AudioPolicyService::OpRecordAudioMonitor implementation ----------
1507
1508// static
1509sp<AudioPolicyService::OpRecordAudioMonitor>
1510AudioPolicyService::OpRecordAudioMonitor::createIfNeeded(
1511 const AttributionSourceState& attributionSource, const audio_attributes_t& attr,
1512 wp<AudioCommandThread> commandThread)
1513{
Eric Laurent987ce102021-07-05 12:11:51 +02001514 if (isAudioServerOrRootUid(attributionSource.uid)) {
1515 ALOGV("not silencing record for audio or root source %s",
Eric Laurented726cc2021-07-01 14:26:41 +02001516 attributionSource.toString().c_str());
1517 return nullptr;
1518 }
1519
1520 if (!AudioPolicyService::isAppOpSource(attr.source)) {
1521 ALOGD("not monitoring app op for uid %d and source %d",
1522 attributionSource.uid, attr.source);
1523 return nullptr;
1524 }
1525
1526 if (!attributionSource.packageName.has_value()
1527 || attributionSource.packageName.value().size() == 0) {
1528 return nullptr;
1529 }
1530 return new OpRecordAudioMonitor(attributionSource, getOpForSource(attr.source), commandThread);
1531}
1532
1533AudioPolicyService::OpRecordAudioMonitor::OpRecordAudioMonitor(
1534 const AttributionSourceState& attributionSource, int32_t appOp,
1535 wp<AudioCommandThread> commandThread) :
1536 mHasOp(true), mAttributionSource(attributionSource), mAppOp(appOp),
1537 mCommandThread(commandThread)
1538{
1539}
1540
1541AudioPolicyService::OpRecordAudioMonitor::~OpRecordAudioMonitor()
1542{
1543 if (mOpCallback != 0) {
1544 mAppOpsManager.stopWatchingMode(mOpCallback);
1545 }
1546 mOpCallback.clear();
1547}
1548
1549void AudioPolicyService::OpRecordAudioMonitor::onFirstRef()
1550{
1551 checkOp();
1552 mOpCallback = new RecordAudioOpCallback(this);
1553 ALOGV("start watching op %d for %s", mAppOp, mAttributionSource.toString().c_str());
1554 // TODO: We need to always watch AppOpsManager::OP_RECORD_AUDIO too
1555 // since it controls the mic permission for legacy apps.
1556 mAppOpsManager.startWatchingMode(mAppOp, VALUE_OR_FATAL(aidl2legacy_string_view_String16(
1557 mAttributionSource.packageName.value_or(""))),
1558 mOpCallback);
1559}
1560
1561bool AudioPolicyService::OpRecordAudioMonitor::hasOp() const {
1562 return mHasOp.load();
1563}
1564
1565// Called by RecordAudioOpCallback when the app op corresponding to this OpRecordAudioMonitor
1566// is updated in AppOp callback and in onFirstRef()
1567// Note this method is never called (and never to be) for audio server / root track
1568// due to the UID in createIfNeeded(). As a result for those record track, it's:
1569// - not called from constructor,
1570// - not called from RecordAudioOpCallback because the callback is not installed in this case
1571void AudioPolicyService::OpRecordAudioMonitor::checkOp(bool updateUidStates)
1572{
1573 // TODO: We need to always check AppOpsManager::OP_RECORD_AUDIO too
1574 // since it controls the mic permission for legacy apps.
1575 const int32_t mode = mAppOpsManager.checkOp(mAppOp,
1576 mAttributionSource.uid, VALUE_OR_FATAL(aidl2legacy_string_view_String16(
1577 mAttributionSource.packageName.value_or(""))));
1578 const bool hasIt = (mode == AppOpsManager::MODE_ALLOWED);
1579 // verbose logging only log when appOp changed
1580 ALOGI_IF(hasIt != mHasOp.load(),
1581 "App op %d missing, %ssilencing record %s",
1582 mAppOp, hasIt ? "un" : "", mAttributionSource.toString().c_str());
1583 mHasOp.store(hasIt);
1584
1585 if (updateUidStates) {
1586 sp<AudioCommandThread> commandThread = mCommandThread.promote();
1587 if (commandThread != nullptr) {
1588 commandThread->updateUidStatesCommand();
1589 }
1590 }
1591}
1592
1593AudioPolicyService::OpRecordAudioMonitor::RecordAudioOpCallback::RecordAudioOpCallback(
1594 const wp<OpRecordAudioMonitor>& monitor) : mMonitor(monitor)
1595{ }
1596
1597void AudioPolicyService::OpRecordAudioMonitor::RecordAudioOpCallback::opChanged(int32_t op,
1598 const String16& packageName __unused) {
1599 sp<OpRecordAudioMonitor> monitor = mMonitor.promote();
1600 if (monitor != NULL) {
1601 if (op != monitor->getOp()) {
1602 return;
1603 }
1604 monitor->checkOp(true);
1605 }
1606}
1607
1608
Mathias Agopian65ab4712010-07-14 17:59:35 -07001609// ----------- AudioPolicyService::AudioCommandThread implementation ----------
1610
Eric Laurentbfb1b832013-01-07 09:53:42 -08001611AudioPolicyService::AudioCommandThread::AudioCommandThread(String8 name,
1612 const wp<AudioPolicyService>& service)
1613 : Thread(false), mName(name), mService(service)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001614{
Mathias Agopian65ab4712010-07-14 17:59:35 -07001615}
1616
1617
1618AudioPolicyService::AudioCommandThread::~AudioCommandThread()
1619{
Eric Laurentbfb1b832013-01-07 09:53:42 -08001620 if (!mAudioCommands.isEmpty()) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07001621 release_wake_lock(mName.string());
1622 }
1623 mAudioCommands.clear();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001624}
1625
1626void AudioPolicyService::AudioCommandThread::onFirstRef()
1627{
Eric Laurentbfb1b832013-01-07 09:53:42 -08001628 run(mName.string(), ANDROID_PRIORITY_AUDIO);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001629}
1630
1631bool AudioPolicyService::AudioCommandThread::threadLoop()
1632{
Eric Laurentd7eda8d2016-02-02 17:18:39 -08001633 nsecs_t waitTime = -1;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001634
1635 mLock.lock();
1636 while (!exitPending())
1637 {
Eric Laurent59a89232014-06-08 14:14:17 -07001638 sp<AudioPolicyService> svc;
1639 while (!mAudioCommands.isEmpty() && !exitPending()) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07001640 nsecs_t curTime = systemTime();
1641 // commands are sorted by increasing time stamp: execute them from index 0 and up
1642 if (mAudioCommands[0]->mTime <= curTime) {
Eric Laurent0ede8922014-05-09 18:04:42 -07001643 sp<AudioCommand> command = mAudioCommands[0];
Mathias Agopian65ab4712010-07-14 17:59:35 -07001644 mAudioCommands.removeAt(0);
Eric Laurent0ede8922014-05-09 18:04:42 -07001645 mLastCommand = command;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001646
1647 switch (command->mCommand) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07001648 case SET_VOLUME: {
Eric Laurent0ede8922014-05-09 18:04:42 -07001649 VolumeData *data = (VolumeData *)command->mParam.get();
Steve Block3856b092011-10-20 11:56:00 +01001650 ALOGV("AudioCommandThread() processing set volume stream %d, \
Eric Laurentde070132010-07-13 04:45:46 -07001651 volume %f, output %d", data->mStream, data->mVolume, data->mIO);
Andy Hungfe726a62018-09-27 15:17:25 -07001652 mLock.unlock();
Eric Laurentde070132010-07-13 04:45:46 -07001653 command->mStatus = AudioSystem::setStreamVolume(data->mStream,
1654 data->mVolume,
1655 data->mIO);
Andy Hungfe726a62018-09-27 15:17:25 -07001656 mLock.lock();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001657 }break;
1658 case SET_PARAMETERS: {
Eric Laurent0ede8922014-05-09 18:04:42 -07001659 ParametersData *data = (ParametersData *)command->mParam.get();
Glenn Kastene53b9ea2012-03-12 16:29:55 -07001660 ALOGV("AudioCommandThread() processing set parameters string %s, io %d",
1661 data->mKeyValuePairs.string(), data->mIO);
Andy Hungfe726a62018-09-27 15:17:25 -07001662 mLock.unlock();
Glenn Kastene53b9ea2012-03-12 16:29:55 -07001663 command->mStatus = AudioSystem::setParameters(data->mIO, data->mKeyValuePairs);
Andy Hungfe726a62018-09-27 15:17:25 -07001664 mLock.lock();
Glenn Kastene53b9ea2012-03-12 16:29:55 -07001665 }break;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001666 case SET_VOICE_VOLUME: {
Eric Laurent0ede8922014-05-09 18:04:42 -07001667 VoiceVolumeData *data = (VoiceVolumeData *)command->mParam.get();
Steve Block3856b092011-10-20 11:56:00 +01001668 ALOGV("AudioCommandThread() processing set voice volume volume %f",
Eric Laurentde070132010-07-13 04:45:46 -07001669 data->mVolume);
Andy Hungfe726a62018-09-27 15:17:25 -07001670 mLock.unlock();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001671 command->mStatus = AudioSystem::setVoiceVolume(data->mVolume);
Andy Hungfe726a62018-09-27 15:17:25 -07001672 mLock.lock();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001673 }break;
Eric Laurentbfb1b832013-01-07 09:53:42 -08001674 case STOP_OUTPUT: {
Eric Laurent0ede8922014-05-09 18:04:42 -07001675 StopOutputData *data = (StopOutputData *)command->mParam.get();
Eric Laurentd7fe0862018-07-14 16:48:01 -07001676 ALOGV("AudioCommandThread() processing stop output portId %d",
1677 data->mPortId);
Eric Laurent59a89232014-06-08 14:14:17 -07001678 svc = mService.promote();
Eric Laurentbfb1b832013-01-07 09:53:42 -08001679 if (svc == 0) {
1680 break;
1681 }
1682 mLock.unlock();
Eric Laurentd7fe0862018-07-14 16:48:01 -07001683 svc->doStopOutput(data->mPortId);
Eric Laurentbfb1b832013-01-07 09:53:42 -08001684 mLock.lock();
Eric Laurentbfb1b832013-01-07 09:53:42 -08001685 }break;
1686 case RELEASE_OUTPUT: {
Eric Laurent0ede8922014-05-09 18:04:42 -07001687 ReleaseOutputData *data = (ReleaseOutputData *)command->mParam.get();
Eric Laurentd7fe0862018-07-14 16:48:01 -07001688 ALOGV("AudioCommandThread() processing release output portId %d",
1689 data->mPortId);
Eric Laurent59a89232014-06-08 14:14:17 -07001690 svc = mService.promote();
Eric Laurentbfb1b832013-01-07 09:53:42 -08001691 if (svc == 0) {
1692 break;
1693 }
1694 mLock.unlock();
Eric Laurentd7fe0862018-07-14 16:48:01 -07001695 svc->doReleaseOutput(data->mPortId);
Eric Laurentbfb1b832013-01-07 09:53:42 -08001696 mLock.lock();
Eric Laurentbfb1b832013-01-07 09:53:42 -08001697 }break;
Eric Laurent951f4552014-05-20 10:48:17 -07001698 case CREATE_AUDIO_PATCH: {
1699 CreateAudioPatchData *data = (CreateAudioPatchData *)command->mParam.get();
1700 ALOGV("AudioCommandThread() processing create audio patch");
1701 sp<IAudioFlinger> af = AudioSystem::get_audio_flinger();
1702 if (af == 0) {
1703 command->mStatus = PERMISSION_DENIED;
1704 } else {
Andy Hungfe726a62018-09-27 15:17:25 -07001705 mLock.unlock();
Eric Laurent951f4552014-05-20 10:48:17 -07001706 command->mStatus = af->createAudioPatch(&data->mPatch, &data->mHandle);
Andy Hungfe726a62018-09-27 15:17:25 -07001707 mLock.lock();
Eric Laurent951f4552014-05-20 10:48:17 -07001708 }
1709 } break;
1710 case RELEASE_AUDIO_PATCH: {
1711 ReleaseAudioPatchData *data = (ReleaseAudioPatchData *)command->mParam.get();
1712 ALOGV("AudioCommandThread() processing release audio patch");
1713 sp<IAudioFlinger> af = AudioSystem::get_audio_flinger();
1714 if (af == 0) {
1715 command->mStatus = PERMISSION_DENIED;
1716 } else {
Andy Hungfe726a62018-09-27 15:17:25 -07001717 mLock.unlock();
Eric Laurent951f4552014-05-20 10:48:17 -07001718 command->mStatus = af->releaseAudioPatch(data->mHandle);
Andy Hungfe726a62018-09-27 15:17:25 -07001719 mLock.lock();
Eric Laurent951f4552014-05-20 10:48:17 -07001720 }
1721 } break;
Eric Laurentb52c1522014-05-20 11:27:36 -07001722 case UPDATE_AUDIOPORT_LIST: {
1723 ALOGV("AudioCommandThread() processing update audio port list");
Eric Laurent59a89232014-06-08 14:14:17 -07001724 svc = mService.promote();
Eric Laurentb52c1522014-05-20 11:27:36 -07001725 if (svc == 0) {
1726 break;
1727 }
1728 mLock.unlock();
1729 svc->doOnAudioPortListUpdate();
1730 mLock.lock();
1731 }break;
1732 case UPDATE_AUDIOPATCH_LIST: {
1733 ALOGV("AudioCommandThread() processing update audio patch list");
Eric Laurent59a89232014-06-08 14:14:17 -07001734 svc = mService.promote();
Eric Laurentb52c1522014-05-20 11:27:36 -07001735 if (svc == 0) {
1736 break;
1737 }
1738 mLock.unlock();
1739 svc->doOnAudioPatchListUpdate();
1740 mLock.lock();
1741 }break;
François Gaffiecfe17322018-11-07 13:41:29 +01001742 case CHANGED_AUDIOVOLUMEGROUP: {
1743 AudioVolumeGroupData *data =
1744 static_cast<AudioVolumeGroupData *>(command->mParam.get());
1745 ALOGV("AudioCommandThread() processing update audio volume group");
1746 svc = mService.promote();
1747 if (svc == 0) {
1748 break;
1749 }
1750 mLock.unlock();
1751 svc->doOnAudioVolumeGroupChanged(data->mGroup, data->mFlags);
1752 mLock.lock();
1753 }break;
Eric Laurente1715a42014-05-20 11:30:42 -07001754 case SET_AUDIOPORT_CONFIG: {
1755 SetAudioPortConfigData *data = (SetAudioPortConfigData *)command->mParam.get();
1756 ALOGV("AudioCommandThread() processing set port config");
1757 sp<IAudioFlinger> af = AudioSystem::get_audio_flinger();
1758 if (af == 0) {
1759 command->mStatus = PERMISSION_DENIED;
1760 } else {
Andy Hungfe726a62018-09-27 15:17:25 -07001761 mLock.unlock();
Eric Laurente1715a42014-05-20 11:30:42 -07001762 command->mStatus = af->setAudioPortConfig(&data->mConfig);
Andy Hungfe726a62018-09-27 15:17:25 -07001763 mLock.lock();
Eric Laurente1715a42014-05-20 11:30:42 -07001764 }
1765 } break;
Jean-Michel Trivide801052015-04-14 19:10:14 -07001766 case DYN_POLICY_MIX_STATE_UPDATE: {
1767 DynPolicyMixStateUpdateData *data =
1768 (DynPolicyMixStateUpdateData *)command->mParam.get();
Jean-Michel Trivide801052015-04-14 19:10:14 -07001769 ALOGV("AudioCommandThread() processing dyn policy mix state update %s %d",
1770 data->mRegId.string(), data->mState);
1771 svc = mService.promote();
1772 if (svc == 0) {
1773 break;
1774 }
1775 mLock.unlock();
1776 svc->doOnDynamicPolicyMixStateUpdate(data->mRegId, data->mState);
1777 mLock.lock();
1778 } break;
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -08001779 case RECORDING_CONFIGURATION_UPDATE: {
1780 RecordingConfigurationUpdateData *data =
1781 (RecordingConfigurationUpdateData *)command->mParam.get();
1782 ALOGV("AudioCommandThread() processing recording configuration update");
1783 svc = mService.promote();
1784 if (svc == 0) {
1785 break;
1786 }
1787 mLock.unlock();
Jean-Michel Triviac4e4292016-12-22 11:39:31 -08001788 svc->doOnRecordingConfigurationUpdate(data->mEvent, &data->mClientInfo,
Eric Laurenta9f86652018-11-28 17:23:11 -08001789 &data->mClientConfig, data->mClientEffects,
1790 &data->mDeviceConfig, data->mEffects,
1791 data->mPatchHandle, data->mSource);
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -08001792 mLock.lock();
1793 } break;
Eric Laurentb20cf7d2019-04-05 19:37:34 -07001794 case SET_EFFECT_SUSPENDED: {
1795 SetEffectSuspendedData *data = (SetEffectSuspendedData *)command->mParam.get();
1796 ALOGV("AudioCommandThread() processing set effect suspended");
1797 sp<IAudioFlinger> af = AudioSystem::get_audio_flinger();
1798 if (af != 0) {
1799 mLock.unlock();
1800 af->setEffectSuspended(data->mEffectId, data->mSessionId, data->mSuspended);
1801 mLock.lock();
1802 }
1803 } break;
Mikhail Naganov88b30d22020-03-09 19:43:13 +00001804 case AUDIO_MODULES_UPDATE: {
1805 ALOGV("AudioCommandThread() processing audio modules update");
1806 svc = mService.promote();
1807 if (svc == 0) {
1808 break;
1809 }
1810 mLock.unlock();
1811 svc->doOnNewAudioModulesAvailable();
1812 mLock.lock();
1813 } break;
Jean-Michel Trivi9a6b9ad2020-10-22 16:46:43 -07001814 case ROUTING_UPDATED: {
1815 ALOGV("AudioCommandThread() processing routing update");
1816 svc = mService.promote();
1817 if (svc == 0) {
1818 break;
1819 }
1820 mLock.unlock();
1821 svc->doOnRoutingUpdated();
1822 mLock.lock();
1823 } break;
Eric Laurentb20cf7d2019-04-05 19:37:34 -07001824
Eric Laurented726cc2021-07-01 14:26:41 +02001825 case UPDATE_UID_STATES: {
1826 ALOGV("AudioCommandThread() processing updateUID states");
1827 svc = mService.promote();
1828 if (svc == 0) {
1829 break;
1830 }
1831 mLock.unlock();
1832 svc->updateUidStates();
1833 mLock.lock();
1834 } break;
1835
Eric Laurent6d607012021-07-05 11:54:40 +02001836 case CHECK_SPATIALIZER: {
1837 ALOGV("AudioCommandThread() processing updateUID states");
1838 svc = mService.promote();
1839 if (svc == 0) {
1840 break;
1841 }
1842 mLock.unlock();
1843 svc->doOnCheckSpatializer();
1844 mLock.lock();
1845 } break;
1846
Mathias Agopian65ab4712010-07-14 17:59:35 -07001847 default:
Steve Block5ff1dd52012-01-05 23:22:43 +00001848 ALOGW("AudioCommandThread() unknown command %d", command->mCommand);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001849 }
Eric Laurent0ede8922014-05-09 18:04:42 -07001850 {
1851 Mutex::Autolock _l(command->mLock);
1852 if (command->mWaitStatus) {
1853 command->mWaitStatus = false;
1854 command->mCond.signal();
1855 }
1856 }
Eric Laurentd7eda8d2016-02-02 17:18:39 -08001857 waitTime = -1;
Zach Janga754b4f2015-10-27 01:29:34 +00001858 // release mLock before releasing strong reference on the service as
1859 // AudioPolicyService destructor calls AudioCommandThread::exit() which
1860 // acquires mLock.
1861 mLock.unlock();
1862 svc.clear();
1863 mLock.lock();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001864 } else {
1865 waitTime = mAudioCommands[0]->mTime - curTime;
1866 break;
1867 }
1868 }
Zach Janga754b4f2015-10-27 01:29:34 +00001869
1870 // release delayed commands wake lock if the queue is empty
1871 if (mAudioCommands.isEmpty()) {
Ricardo Garcia05f2fdc2014-07-24 15:48:24 -07001872 release_wake_lock(mName.string());
Zach Janga754b4f2015-10-27 01:29:34 +00001873 }
1874
1875 // At this stage we have either an empty command queue or the first command in the queue
1876 // has a finite delay. So unless we are exiting it is safe to wait.
1877 if (!exitPending()) {
Eric Laurent59a89232014-06-08 14:14:17 -07001878 ALOGV("AudioCommandThread() going to sleep");
Eric Laurentd7eda8d2016-02-02 17:18:39 -08001879 if (waitTime == -1) {
1880 mWaitWorkCV.wait(mLock);
1881 } else {
1882 mWaitWorkCV.waitRelative(mLock, waitTime);
1883 }
Eric Laurent59a89232014-06-08 14:14:17 -07001884 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001885 }
Ricardo Garcia05f2fdc2014-07-24 15:48:24 -07001886 // release delayed commands wake lock before quitting
1887 if (!mAudioCommands.isEmpty()) {
1888 release_wake_lock(mName.string());
1889 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001890 mLock.unlock();
1891 return false;
1892}
1893
1894status_t AudioPolicyService::AudioCommandThread::dump(int fd)
1895{
1896 const size_t SIZE = 256;
1897 char buffer[SIZE];
1898 String8 result;
1899
1900 snprintf(buffer, SIZE, "AudioCommandThread %p Dump\n", this);
1901 result.append(buffer);
1902 write(fd, result.string(), result.size());
1903
Mikhail Naganov12b716c2020-04-30 22:37:43 +00001904 const bool locked = dumpTryLock(mLock);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001905 if (!locked) {
1906 String8 result2(kCmdDeadlockedString);
1907 write(fd, result2.string(), result2.size());
1908 }
1909
1910 snprintf(buffer, SIZE, "- Commands:\n");
1911 result = String8(buffer);
1912 result.append(" Command Time Wait pParam\n");
Glenn Kasten8d6a2442012-02-08 14:04:28 -08001913 for (size_t i = 0; i < mAudioCommands.size(); i++) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07001914 mAudioCommands[i]->dump(buffer, SIZE);
1915 result.append(buffer);
1916 }
1917 result.append(" Last Command\n");
Eric Laurent0ede8922014-05-09 18:04:42 -07001918 if (mLastCommand != 0) {
1919 mLastCommand->dump(buffer, SIZE);
1920 result.append(buffer);
1921 } else {
1922 result.append(" none\n");
1923 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001924
1925 write(fd, result.string(), result.size());
1926
Mikhail Naganov12b716c2020-04-30 22:37:43 +00001927 dumpReleaseLock(mLock, locked);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001928
1929 return NO_ERROR;
1930}
1931
Glenn Kastenfff6d712012-01-12 16:38:12 -08001932status_t AudioPolicyService::AudioCommandThread::volumeCommand(audio_stream_type_t stream,
Eric Laurentde070132010-07-13 04:45:46 -07001933 float volume,
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001934 audio_io_handle_t output,
Eric Laurentde070132010-07-13 04:45:46 -07001935 int delayMs)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001936{
Eric Laurent0ede8922014-05-09 18:04:42 -07001937 sp<AudioCommand> command = new AudioCommand();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001938 command->mCommand = SET_VOLUME;
Eric Laurent0ede8922014-05-09 18:04:42 -07001939 sp<VolumeData> data = new VolumeData();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001940 data->mStream = stream;
1941 data->mVolume = volume;
1942 data->mIO = output;
1943 command->mParam = data;
Eric Laurent0ede8922014-05-09 18:04:42 -07001944 command->mWaitStatus = true;
Steve Block3856b092011-10-20 11:56:00 +01001945 ALOGV("AudioCommandThread() adding set volume stream %d, volume %f, output %d",
Eric Laurentde070132010-07-13 04:45:46 -07001946 stream, volume, output);
Eric Laurent0ede8922014-05-09 18:04:42 -07001947 return sendCommand(command, delayMs);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001948}
1949
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001950status_t AudioPolicyService::AudioCommandThread::parametersCommand(audio_io_handle_t ioHandle,
Dima Zavinfce7a472011-04-19 22:30:36 -07001951 const char *keyValuePairs,
Eric Laurentde070132010-07-13 04:45:46 -07001952 int delayMs)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001953{
Eric Laurent0ede8922014-05-09 18:04:42 -07001954 sp<AudioCommand> command = new AudioCommand();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001955 command->mCommand = SET_PARAMETERS;
Eric Laurent0ede8922014-05-09 18:04:42 -07001956 sp<ParametersData> data = new ParametersData();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001957 data->mIO = ioHandle;
Dima Zavinfce7a472011-04-19 22:30:36 -07001958 data->mKeyValuePairs = String8(keyValuePairs);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001959 command->mParam = data;
Eric Laurent0ede8922014-05-09 18:04:42 -07001960 command->mWaitStatus = true;
Steve Block3856b092011-10-20 11:56:00 +01001961 ALOGV("AudioCommandThread() adding set parameter string %s, io %d ,delay %d",
Dima Zavinfce7a472011-04-19 22:30:36 -07001962 keyValuePairs, ioHandle, delayMs);
Eric Laurent0ede8922014-05-09 18:04:42 -07001963 return sendCommand(command, delayMs);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001964}
1965
1966status_t AudioPolicyService::AudioCommandThread::voiceVolumeCommand(float volume, int delayMs)
1967{
Eric Laurent0ede8922014-05-09 18:04:42 -07001968 sp<AudioCommand> command = new AudioCommand();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001969 command->mCommand = SET_VOICE_VOLUME;
Eric Laurent0ede8922014-05-09 18:04:42 -07001970 sp<VoiceVolumeData> data = new VoiceVolumeData();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001971 data->mVolume = volume;
1972 command->mParam = data;
Eric Laurent0ede8922014-05-09 18:04:42 -07001973 command->mWaitStatus = true;
Steve Block3856b092011-10-20 11:56:00 +01001974 ALOGV("AudioCommandThread() adding set voice volume volume %f", volume);
Eric Laurent0ede8922014-05-09 18:04:42 -07001975 return sendCommand(command, delayMs);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001976}
1977
Eric Laurentb20cf7d2019-04-05 19:37:34 -07001978void AudioPolicyService::AudioCommandThread::setEffectSuspendedCommand(int effectId,
1979 audio_session_t sessionId,
1980 bool suspended)
1981{
1982 sp<AudioCommand> command = new AudioCommand();
1983 command->mCommand = SET_EFFECT_SUSPENDED;
1984 sp<SetEffectSuspendedData> data = new SetEffectSuspendedData();
1985 data->mEffectId = effectId;
1986 data->mSessionId = sessionId;
1987 data->mSuspended = suspended;
1988 command->mParam = data;
1989 ALOGV("AudioCommandThread() adding set suspended effectId %d sessionId %d suspended %d",
1990 effectId, sessionId, suspended);
1991 sendCommand(command);
1992}
1993
1994
Eric Laurentd7fe0862018-07-14 16:48:01 -07001995void AudioPolicyService::AudioCommandThread::stopOutputCommand(audio_port_handle_t portId)
Eric Laurentbfb1b832013-01-07 09:53:42 -08001996{
Eric Laurent0ede8922014-05-09 18:04:42 -07001997 sp<AudioCommand> command = new AudioCommand();
Eric Laurentbfb1b832013-01-07 09:53:42 -08001998 command->mCommand = STOP_OUTPUT;
Eric Laurent0ede8922014-05-09 18:04:42 -07001999 sp<StopOutputData> data = new StopOutputData();
Eric Laurentd7fe0862018-07-14 16:48:01 -07002000 data->mPortId = portId;
Jesper Tragardh48412dc2014-03-24 14:12:43 +01002001 command->mParam = data;
Eric Laurentd7fe0862018-07-14 16:48:01 -07002002 ALOGV("AudioCommandThread() adding stop output portId %d", portId);
Eric Laurent0ede8922014-05-09 18:04:42 -07002003 sendCommand(command);
Eric Laurentbfb1b832013-01-07 09:53:42 -08002004}
2005
Eric Laurentd7fe0862018-07-14 16:48:01 -07002006void AudioPolicyService::AudioCommandThread::releaseOutputCommand(audio_port_handle_t portId)
Eric Laurentbfb1b832013-01-07 09:53:42 -08002007{
Eric Laurent0ede8922014-05-09 18:04:42 -07002008 sp<AudioCommand> command = new AudioCommand();
Eric Laurentbfb1b832013-01-07 09:53:42 -08002009 command->mCommand = RELEASE_OUTPUT;
Eric Laurent0ede8922014-05-09 18:04:42 -07002010 sp<ReleaseOutputData> data = new ReleaseOutputData();
Eric Laurentd7fe0862018-07-14 16:48:01 -07002011 data->mPortId = portId;
Jesper Tragardh48412dc2014-03-24 14:12:43 +01002012 command->mParam = data;
Eric Laurentd7fe0862018-07-14 16:48:01 -07002013 ALOGV("AudioCommandThread() adding release output portId %d", portId);
Eric Laurent0ede8922014-05-09 18:04:42 -07002014 sendCommand(command);
2015}
2016
Eric Laurent951f4552014-05-20 10:48:17 -07002017status_t AudioPolicyService::AudioCommandThread::createAudioPatchCommand(
2018 const struct audio_patch *patch,
2019 audio_patch_handle_t *handle,
2020 int delayMs)
2021{
2022 status_t status = NO_ERROR;
2023
2024 sp<AudioCommand> command = new AudioCommand();
2025 command->mCommand = CREATE_AUDIO_PATCH;
2026 CreateAudioPatchData *data = new CreateAudioPatchData();
2027 data->mPatch = *patch;
2028 data->mHandle = *handle;
2029 command->mParam = data;
2030 command->mWaitStatus = true;
2031 ALOGV("AudioCommandThread() adding create patch delay %d", delayMs);
2032 status = sendCommand(command, delayMs);
2033 if (status == NO_ERROR) {
2034 *handle = data->mHandle;
2035 }
2036 return status;
2037}
2038
2039status_t AudioPolicyService::AudioCommandThread::releaseAudioPatchCommand(audio_patch_handle_t handle,
2040 int delayMs)
2041{
2042 sp<AudioCommand> command = new AudioCommand();
2043 command->mCommand = RELEASE_AUDIO_PATCH;
2044 ReleaseAudioPatchData *data = new ReleaseAudioPatchData();
2045 data->mHandle = handle;
2046 command->mParam = data;
2047 command->mWaitStatus = true;
2048 ALOGV("AudioCommandThread() adding release patch delay %d", delayMs);
2049 return sendCommand(command, delayMs);
2050}
2051
Eric Laurentb52c1522014-05-20 11:27:36 -07002052void AudioPolicyService::AudioCommandThread::updateAudioPortListCommand()
2053{
2054 sp<AudioCommand> command = new AudioCommand();
2055 command->mCommand = UPDATE_AUDIOPORT_LIST;
2056 ALOGV("AudioCommandThread() adding update audio port list");
2057 sendCommand(command);
2058}
2059
Eric Laurented726cc2021-07-01 14:26:41 +02002060void AudioPolicyService::AudioCommandThread::updateUidStatesCommand()
2061{
2062 sp<AudioCommand> command = new AudioCommand();
2063 command->mCommand = UPDATE_UID_STATES;
2064 ALOGV("AudioCommandThread() adding update UID states");
2065 sendCommand(command);
2066}
2067
Eric Laurentb52c1522014-05-20 11:27:36 -07002068void AudioPolicyService::AudioCommandThread::updateAudioPatchListCommand()
2069{
2070 sp<AudioCommand>command = new AudioCommand();
2071 command->mCommand = UPDATE_AUDIOPATCH_LIST;
2072 ALOGV("AudioCommandThread() adding update audio patch list");
2073 sendCommand(command);
2074}
2075
François Gaffiecfe17322018-11-07 13:41:29 +01002076void AudioPolicyService::AudioCommandThread::changeAudioVolumeGroupCommand(volume_group_t group,
2077 int flags)
2078{
2079 sp<AudioCommand>command = new AudioCommand();
2080 command->mCommand = CHANGED_AUDIOVOLUMEGROUP;
2081 AudioVolumeGroupData *data= new AudioVolumeGroupData();
2082 data->mGroup = group;
2083 data->mFlags = flags;
2084 command->mParam = data;
2085 ALOGV("AudioCommandThread() adding audio volume group changed");
2086 sendCommand(command);
2087}
2088
Eric Laurente1715a42014-05-20 11:30:42 -07002089status_t AudioPolicyService::AudioCommandThread::setAudioPortConfigCommand(
2090 const struct audio_port_config *config, int delayMs)
2091{
2092 sp<AudioCommand> command = new AudioCommand();
2093 command->mCommand = SET_AUDIOPORT_CONFIG;
2094 SetAudioPortConfigData *data = new SetAudioPortConfigData();
2095 data->mConfig = *config;
2096 command->mParam = data;
2097 command->mWaitStatus = true;
2098 ALOGV("AudioCommandThread() adding set port config delay %d", delayMs);
2099 return sendCommand(command, delayMs);
2100}
2101
Jean-Michel Trivide801052015-04-14 19:10:14 -07002102void AudioPolicyService::AudioCommandThread::dynamicPolicyMixStateUpdateCommand(
Chih-Hung Hsiehe964d4e2016-08-09 14:31:32 -07002103 const String8& regId, int32_t state)
Jean-Michel Trivide801052015-04-14 19:10:14 -07002104{
2105 sp<AudioCommand> command = new AudioCommand();
2106 command->mCommand = DYN_POLICY_MIX_STATE_UPDATE;
2107 DynPolicyMixStateUpdateData *data = new DynPolicyMixStateUpdateData();
2108 data->mRegId = regId;
2109 data->mState = state;
2110 command->mParam = data;
2111 ALOGV("AudioCommandThread() sending dynamic policy mix (id=%s) state update to %d",
2112 regId.string(), state);
2113 sendCommand(command);
2114}
2115
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -08002116void AudioPolicyService::AudioCommandThread::recordingConfigurationUpdateCommand(
Eric Laurenta9f86652018-11-28 17:23:11 -08002117 int event,
2118 const record_client_info_t *clientInfo,
2119 const audio_config_base_t *clientConfig,
2120 std::vector<effect_descriptor_t> clientEffects,
2121 const audio_config_base_t *deviceConfig,
2122 std::vector<effect_descriptor_t> effects,
2123 audio_patch_handle_t patchHandle,
2124 audio_source_t source)
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -08002125{
2126 sp<AudioCommand>command = new AudioCommand();
2127 command->mCommand = RECORDING_CONFIGURATION_UPDATE;
2128 RecordingConfigurationUpdateData *data = new RecordingConfigurationUpdateData();
2129 data->mEvent = event;
Jean-Michel Triviac4e4292016-12-22 11:39:31 -08002130 data->mClientInfo = *clientInfo;
Jean-Michel Trivi7281aa92016-02-17 15:33:40 -08002131 data->mClientConfig = *clientConfig;
Eric Laurenta9f86652018-11-28 17:23:11 -08002132 data->mClientEffects = clientEffects;
Jean-Michel Trivi7281aa92016-02-17 15:33:40 -08002133 data->mDeviceConfig = *deviceConfig;
Eric Laurenta9f86652018-11-28 17:23:11 -08002134 data->mEffects = effects;
Jean-Michel Trivi8c7cf3b2016-02-25 17:08:24 -08002135 data->mPatchHandle = patchHandle;
Eric Laurenta9f86652018-11-28 17:23:11 -08002136 data->mSource = source;
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -08002137 command->mParam = data;
Jean-Michel Triviac4e4292016-12-22 11:39:31 -08002138 ALOGV("AudioCommandThread() adding recording configuration update event %d, source %d uid %u",
2139 event, clientInfo->source, clientInfo->uid);
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -08002140 sendCommand(command);
2141}
2142
Mikhail Naganov88b30d22020-03-09 19:43:13 +00002143void AudioPolicyService::AudioCommandThread::audioModulesUpdateCommand()
2144{
2145 sp<AudioCommand> command = new AudioCommand();
2146 command->mCommand = AUDIO_MODULES_UPDATE;
2147 sendCommand(command);
2148}
2149
Jean-Michel Trivi9a6b9ad2020-10-22 16:46:43 -07002150void AudioPolicyService::AudioCommandThread::routingChangedCommand()
2151{
2152 sp<AudioCommand>command = new AudioCommand();
2153 command->mCommand = ROUTING_UPDATED;
2154 ALOGV("AudioCommandThread() adding routing update");
2155 sendCommand(command);
2156}
2157
Eric Laurent6d607012021-07-05 11:54:40 +02002158void AudioPolicyService::AudioCommandThread::checkSpatializerCommand()
2159{
2160 sp<AudioCommand>command = new AudioCommand();
2161 command->mCommand = CHECK_SPATIALIZER;
2162 ALOGV("AudioCommandThread() adding check spatializer");
2163 sendCommand(command);
2164}
2165
Eric Laurent0ede8922014-05-09 18:04:42 -07002166status_t AudioPolicyService::AudioCommandThread::sendCommand(sp<AudioCommand>& command, int delayMs)
2167{
2168 {
2169 Mutex::Autolock _l(mLock);
2170 insertCommand_l(command, delayMs);
2171 mWaitWorkCV.signal();
2172 }
2173 Mutex::Autolock _l(command->mLock);
2174 while (command->mWaitStatus) {
2175 nsecs_t timeOutNs = kAudioCommandTimeoutNs + milliseconds(delayMs);
2176 if (command->mCond.waitRelative(command->mLock, timeOutNs) != NO_ERROR) {
2177 command->mStatus = TIMED_OUT;
2178 command->mWaitStatus = false;
2179 }
2180 }
2181 return command->mStatus;
Eric Laurentbfb1b832013-01-07 09:53:42 -08002182}
2183
Mathias Agopian65ab4712010-07-14 17:59:35 -07002184// insertCommand_l() must be called with mLock held
Eric Laurent0ede8922014-05-09 18:04:42 -07002185void AudioPolicyService::AudioCommandThread::insertCommand_l(sp<AudioCommand>& command, int delayMs)
Mathias Agopian65ab4712010-07-14 17:59:35 -07002186{
Glenn Kasten8d6a2442012-02-08 14:04:28 -08002187 ssize_t i; // not size_t because i will count down to -1
Eric Laurent0ede8922014-05-09 18:04:42 -07002188 Vector < sp<AudioCommand> > removedCommands;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002189 command->mTime = systemTime() + milliseconds(delayMs);
2190
2191 // acquire wake lock to make sure delayed commands are processed
Eric Laurentbfb1b832013-01-07 09:53:42 -08002192 if (mAudioCommands.isEmpty()) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07002193 acquire_wake_lock(PARTIAL_WAKE_LOCK, mName.string());
2194 }
2195
2196 // check same pending commands with later time stamps and eliminate them
Ivan Lozano5ff158f2017-10-30 09:06:24 -07002197 for (i = (ssize_t)mAudioCommands.size()-1; i >= 0; i--) {
Eric Laurent0ede8922014-05-09 18:04:42 -07002198 sp<AudioCommand> command2 = mAudioCommands[i];
Mathias Agopian65ab4712010-07-14 17:59:35 -07002199 // commands are sorted by increasing time stamp: no need to scan the rest of mAudioCommands
2200 if (command2->mTime <= command->mTime) break;
Eric Laurente45b48a2014-09-04 16:40:57 -07002201
2202 // create audio patch or release audio patch commands are equivalent
2203 // with regard to filtering
2204 if ((command->mCommand == CREATE_AUDIO_PATCH) ||
2205 (command->mCommand == RELEASE_AUDIO_PATCH)) {
2206 if ((command2->mCommand != CREATE_AUDIO_PATCH) &&
2207 (command2->mCommand != RELEASE_AUDIO_PATCH)) {
2208 continue;
2209 }
2210 } else if (command2->mCommand != command->mCommand) continue;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002211
2212 switch (command->mCommand) {
2213 case SET_PARAMETERS: {
Eric Laurent0ede8922014-05-09 18:04:42 -07002214 ParametersData *data = (ParametersData *)command->mParam.get();
2215 ParametersData *data2 = (ParametersData *)command2->mParam.get();
Mathias Agopian65ab4712010-07-14 17:59:35 -07002216 if (data->mIO != data2->mIO) break;
Steve Block3856b092011-10-20 11:56:00 +01002217 ALOGV("Comparing parameter command %s to new command %s",
Eric Laurentde070132010-07-13 04:45:46 -07002218 data2->mKeyValuePairs.string(), data->mKeyValuePairs.string());
Mathias Agopian65ab4712010-07-14 17:59:35 -07002219 AudioParameter param = AudioParameter(data->mKeyValuePairs);
2220 AudioParameter param2 = AudioParameter(data2->mKeyValuePairs);
2221 for (size_t j = 0; j < param.size(); j++) {
Glenn Kastene53b9ea2012-03-12 16:29:55 -07002222 String8 key;
2223 String8 value;
2224 param.getAt(j, key, value);
2225 for (size_t k = 0; k < param2.size(); k++) {
2226 String8 key2;
2227 String8 value2;
2228 param2.getAt(k, key2, value2);
2229 if (key2 == key) {
2230 param2.remove(key2);
2231 ALOGV("Filtering out parameter %s", key2.string());
2232 break;
2233 }
2234 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07002235 }
2236 // if all keys have been filtered out, remove the command.
2237 // otherwise, update the key value pairs
2238 if (param2.size() == 0) {
2239 removedCommands.add(command2);
2240 } else {
2241 data2->mKeyValuePairs = param2.toString();
2242 }
Eric Laurent21e54562013-09-23 12:08:05 -07002243 command->mTime = command2->mTime;
2244 // force delayMs to non 0 so that code below does not request to wait for
2245 // command status as the command is now delayed
2246 delayMs = 1;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002247 } break;
2248
2249 case SET_VOLUME: {
Eric Laurent0ede8922014-05-09 18:04:42 -07002250 VolumeData *data = (VolumeData *)command->mParam.get();
2251 VolumeData *data2 = (VolumeData *)command2->mParam.get();
Mathias Agopian65ab4712010-07-14 17:59:35 -07002252 if (data->mIO != data2->mIO) break;
2253 if (data->mStream != data2->mStream) break;
Steve Block3856b092011-10-20 11:56:00 +01002254 ALOGV("Filtering out volume command on output %d for stream %d",
Eric Laurentde070132010-07-13 04:45:46 -07002255 data->mIO, data->mStream);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002256 removedCommands.add(command2);
Eric Laurent21e54562013-09-23 12:08:05 -07002257 command->mTime = command2->mTime;
2258 // force delayMs to non 0 so that code below does not request to wait for
2259 // command status as the command is now delayed
2260 delayMs = 1;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002261 } break;
Eric Laurente45b48a2014-09-04 16:40:57 -07002262
Eric Laurentbaf35fe2016-07-27 15:36:53 -07002263 case SET_VOICE_VOLUME: {
2264 VoiceVolumeData *data = (VoiceVolumeData *)command->mParam.get();
2265 VoiceVolumeData *data2 = (VoiceVolumeData *)command2->mParam.get();
2266 ALOGV("Filtering out voice volume command value %f replaced by %f",
2267 data2->mVolume, data->mVolume);
2268 removedCommands.add(command2);
2269 command->mTime = command2->mTime;
2270 // force delayMs to non 0 so that code below does not request to wait for
2271 // command status as the command is now delayed
2272 delayMs = 1;
2273 } break;
2274
Eric Laurente45b48a2014-09-04 16:40:57 -07002275 case CREATE_AUDIO_PATCH:
2276 case RELEASE_AUDIO_PATCH: {
2277 audio_patch_handle_t handle;
Haynes Mathew Georgea2d4a6d2014-10-13 13:05:22 -07002278 struct audio_patch patch;
Eric Laurente45b48a2014-09-04 16:40:57 -07002279 if (command->mCommand == CREATE_AUDIO_PATCH) {
2280 handle = ((CreateAudioPatchData *)command->mParam.get())->mHandle;
Haynes Mathew Georgea2d4a6d2014-10-13 13:05:22 -07002281 patch = ((CreateAudioPatchData *)command->mParam.get())->mPatch;
Eric Laurente45b48a2014-09-04 16:40:57 -07002282 } else {
2283 handle = ((ReleaseAudioPatchData *)command->mParam.get())->mHandle;
Mikhail Naganov7be71d22018-05-23 16:51:46 -07002284 memset(&patch, 0, sizeof(patch));
Eric Laurente45b48a2014-09-04 16:40:57 -07002285 }
2286 audio_patch_handle_t handle2;
Haynes Mathew Georgea2d4a6d2014-10-13 13:05:22 -07002287 struct audio_patch patch2;
Eric Laurente45b48a2014-09-04 16:40:57 -07002288 if (command2->mCommand == CREATE_AUDIO_PATCH) {
2289 handle2 = ((CreateAudioPatchData *)command2->mParam.get())->mHandle;
Haynes Mathew Georgea2d4a6d2014-10-13 13:05:22 -07002290 patch2 = ((CreateAudioPatchData *)command2->mParam.get())->mPatch;
Eric Laurente45b48a2014-09-04 16:40:57 -07002291 } else {
2292 handle2 = ((ReleaseAudioPatchData *)command2->mParam.get())->mHandle;
Glenn Kastenf60b6b62015-07-06 10:53:26 -07002293 memset(&patch2, 0, sizeof(patch2));
Eric Laurente45b48a2014-09-04 16:40:57 -07002294 }
2295 if (handle != handle2) break;
Haynes Mathew Georgea2d4a6d2014-10-13 13:05:22 -07002296 /* Filter CREATE_AUDIO_PATCH commands only when they are issued for
2297 same output. */
2298 if( (command->mCommand == CREATE_AUDIO_PATCH) &&
2299 (command2->mCommand == CREATE_AUDIO_PATCH) ) {
2300 bool isOutputDiff = false;
2301 if (patch.num_sources == patch2.num_sources) {
2302 for (unsigned count = 0; count < patch.num_sources; count++) {
2303 if (patch.sources[count].id != patch2.sources[count].id) {
2304 isOutputDiff = true;
2305 break;
2306 }
2307 }
2308 if (isOutputDiff)
2309 break;
2310 }
2311 }
Eric Laurente45b48a2014-09-04 16:40:57 -07002312 ALOGV("Filtering out %s audio patch command for handle %d",
2313 (command->mCommand == CREATE_AUDIO_PATCH) ? "create" : "release", handle);
2314 removedCommands.add(command2);
2315 command->mTime = command2->mTime;
2316 // force delayMs to non 0 so that code below does not request to wait for
2317 // command status as the command is now delayed
2318 delayMs = 1;
2319 } break;
2320
Jean-Michel Trivide801052015-04-14 19:10:14 -07002321 case DYN_POLICY_MIX_STATE_UPDATE: {
2322
2323 } break;
2324
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -08002325 case RECORDING_CONFIGURATION_UPDATE: {
2326
2327 } break;
2328
Jean-Michel Trivi9a6b9ad2020-10-22 16:46:43 -07002329 case ROUTING_UPDATED: {
2330
2331 } break;
2332
Mathias Agopian65ab4712010-07-14 17:59:35 -07002333 default:
2334 break;
2335 }
2336 }
2337
2338 // remove filtered commands
2339 for (size_t j = 0; j < removedCommands.size(); j++) {
2340 // removed commands always have time stamps greater than current command
2341 for (size_t k = i + 1; k < mAudioCommands.size(); k++) {
Eric Laurent0ede8922014-05-09 18:04:42 -07002342 if (mAudioCommands[k].get() == removedCommands[j].get()) {
Steve Block3856b092011-10-20 11:56:00 +01002343 ALOGV("suppressing command: %d", mAudioCommands[k]->mCommand);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002344 mAudioCommands.removeAt(k);
2345 break;
2346 }
2347 }
2348 }
2349 removedCommands.clear();
2350
Eric Laurentaa79bef2015-01-15 14:33:51 -08002351 // Disable wait for status if delay is not 0.
2352 // Except for create audio patch command because the returned patch handle
2353 // is needed by audio policy manager
2354 if (delayMs != 0 && command->mCommand != CREATE_AUDIO_PATCH) {
Eric Laurentcec4abb2012-07-03 12:23:02 -07002355 command->mWaitStatus = false;
2356 }
Eric Laurentcec4abb2012-07-03 12:23:02 -07002357
Mathias Agopian65ab4712010-07-14 17:59:35 -07002358 // insert command at the right place according to its time stamp
Eric Laurent1e693b52014-07-09 15:03:28 -07002359 ALOGV("inserting command: %d at index %zd, num commands %zu",
2360 command->mCommand, i+1, mAudioCommands.size());
Mathias Agopian65ab4712010-07-14 17:59:35 -07002361 mAudioCommands.insertAt(command, i + 1);
2362}
2363
2364void AudioPolicyService::AudioCommandThread::exit()
2365{
Steve Block3856b092011-10-20 11:56:00 +01002366 ALOGV("AudioCommandThread::exit");
Mathias Agopian65ab4712010-07-14 17:59:35 -07002367 {
2368 AutoMutex _l(mLock);
2369 requestExit();
2370 mWaitWorkCV.signal();
2371 }
Zach Janga754b4f2015-10-27 01:29:34 +00002372 // Note that we can call it from the thread loop if all other references have been released
2373 // but it will safely return WOULD_BLOCK in this case
Mathias Agopian65ab4712010-07-14 17:59:35 -07002374 requestExitAndWait();
2375}
2376
2377void AudioPolicyService::AudioCommandThread::AudioCommand::dump(char* buffer, size_t size)
2378{
2379 snprintf(buffer, size, " %02d %06d.%03d %01u %p\n",
2380 mCommand,
2381 (int)ns2s(mTime),
2382 (int)ns2ms(mTime)%1000,
2383 mWaitStatus,
Eric Laurent0ede8922014-05-09 18:04:42 -07002384 mParam.get());
Mathias Agopian65ab4712010-07-14 17:59:35 -07002385}
2386
Dima Zavinfce7a472011-04-19 22:30:36 -07002387/******* helpers for the service_ops callbacks defined below *********/
2388void AudioPolicyService::setParameters(audio_io_handle_t ioHandle,
2389 const char *keyValuePairs,
2390 int delayMs)
2391{
Glenn Kasten72ef00d2012-01-17 11:09:42 -08002392 mAudioCommandThread->parametersCommand(ioHandle, keyValuePairs,
Dima Zavinfce7a472011-04-19 22:30:36 -07002393 delayMs);
2394}
2395
2396int AudioPolicyService::setStreamVolume(audio_stream_type_t stream,
2397 float volume,
2398 audio_io_handle_t output,
2399 int delayMs)
2400{
Glenn Kastenfff6d712012-01-12 16:38:12 -08002401 return (int)mAudioCommandThread->volumeCommand(stream, volume,
Glenn Kasten72ef00d2012-01-17 11:09:42 -08002402 output, delayMs);
Dima Zavinfce7a472011-04-19 22:30:36 -07002403}
2404
Dima Zavinfce7a472011-04-19 22:30:36 -07002405int AudioPolicyService::setVoiceVolume(float volume, int delayMs)
2406{
2407 return (int)mAudioCommandThread->voiceVolumeCommand(volume, delayMs);
2408}
2409
Eric Laurentb20cf7d2019-04-05 19:37:34 -07002410void AudioPolicyService::setEffectSuspended(int effectId,
2411 audio_session_t sessionId,
2412 bool suspended)
2413{
2414 mAudioCommandThread->setEffectSuspendedCommand(effectId, sessionId, suspended);
2415}
2416
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002417Status AudioPolicyService::onNewAudioModulesAvailable()
Mikhail Naganov88b30d22020-03-09 19:43:13 +00002418{
Mikhail Naganovd0e2c742020-03-25 15:59:59 -07002419 mOutputCommandThread->audioModulesUpdateCommand();
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002420 return Status::ok();
Mikhail Naganov88b30d22020-03-09 19:43:13 +00002421}
2422
Eric Laurentb20cf7d2019-04-05 19:37:34 -07002423
Dima Zavinfce7a472011-04-19 22:30:36 -07002424extern "C" {
Eric Laurent2d388ec2014-03-07 13:25:54 -08002425audio_module_handle_t aps_load_hw_module(void *service __unused,
2426 const char *name);
2427audio_io_handle_t aps_open_output(void *service __unused,
Eric Laurenta4c5a552012-03-29 10:12:40 -07002428 audio_devices_t *pDevices,
2429 uint32_t *pSamplingRate,
2430 audio_format_t *pFormat,
2431 audio_channel_mask_t *pChannelMask,
2432 uint32_t *pLatencyMs,
Eric Laurent2d388ec2014-03-07 13:25:54 -08002433 audio_output_flags_t flags);
Eric Laurenta4c5a552012-03-29 10:12:40 -07002434
Eric Laurent2d388ec2014-03-07 13:25:54 -08002435audio_io_handle_t aps_open_output_on_module(void *service __unused,
Eric Laurenta4c5a552012-03-29 10:12:40 -07002436 audio_module_handle_t module,
2437 audio_devices_t *pDevices,
2438 uint32_t *pSamplingRate,
2439 audio_format_t *pFormat,
2440 audio_channel_mask_t *pChannelMask,
2441 uint32_t *pLatencyMs,
Richard Fitzgeraldad3af332013-03-25 16:54:37 +00002442 audio_output_flags_t flags,
Eric Laurent2d388ec2014-03-07 13:25:54 -08002443 const audio_offload_info_t *offloadInfo);
2444audio_io_handle_t aps_open_dup_output(void *service __unused,
Dima Zavinfce7a472011-04-19 22:30:36 -07002445 audio_io_handle_t output1,
Eric Laurent2d388ec2014-03-07 13:25:54 -08002446 audio_io_handle_t output2);
2447int aps_close_output(void *service __unused, audio_io_handle_t output);
2448int aps_suspend_output(void *service __unused, audio_io_handle_t output);
2449int aps_restore_output(void *service __unused, audio_io_handle_t output);
2450audio_io_handle_t aps_open_input(void *service __unused,
Eric Laurenta4c5a552012-03-29 10:12:40 -07002451 audio_devices_t *pDevices,
2452 uint32_t *pSamplingRate,
2453 audio_format_t *pFormat,
2454 audio_channel_mask_t *pChannelMask,
Eric Laurent2d388ec2014-03-07 13:25:54 -08002455 audio_in_acoustics_t acoustics __unused);
2456audio_io_handle_t aps_open_input_on_module(void *service __unused,
Eric Laurenta4c5a552012-03-29 10:12:40 -07002457 audio_module_handle_t module,
2458 audio_devices_t *pDevices,
2459 uint32_t *pSamplingRate,
2460 audio_format_t *pFormat,
Eric Laurent2d388ec2014-03-07 13:25:54 -08002461 audio_channel_mask_t *pChannelMask);
2462int aps_close_input(void *service __unused, audio_io_handle_t input);
2463int aps_invalidate_stream(void *service __unused, audio_stream_type_t stream);
Glenn Kastend848eb42016-03-08 13:42:11 -08002464int aps_move_effects(void *service __unused, audio_session_t session,
Dima Zavinfce7a472011-04-19 22:30:36 -07002465 audio_io_handle_t src_output,
Eric Laurent2d388ec2014-03-07 13:25:54 -08002466 audio_io_handle_t dst_output);
2467char * aps_get_parameters(void *service __unused, audio_io_handle_t io_handle,
2468 const char *keys);
2469void aps_set_parameters(void *service, audio_io_handle_t io_handle,
2470 const char *kv_pairs, int delay_ms);
2471int aps_set_stream_volume(void *service, audio_stream_type_t stream,
Dima Zavinfce7a472011-04-19 22:30:36 -07002472 float volume, audio_io_handle_t output,
Eric Laurent2d388ec2014-03-07 13:25:54 -08002473 int delay_ms);
Eric Laurent2d388ec2014-03-07 13:25:54 -08002474int aps_set_voice_volume(void *service, float volume, int delay_ms);
2475};
Dima Zavinfce7a472011-04-19 22:30:36 -07002476
Mikhail Naganov1b2a7942017-12-08 10:18:09 -08002477} // namespace android