blob: 102b376ded5ab33f68fd46200055c23334926c3f [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);
395
396 if (status == NO_ERROR && currentOutput == newOutput) {
397 return;
398 }
399 mLock.unlock();
400 // It is OK to call detachOutput() is none is already attached.
401 mSpatializer->detachOutput();
402 if (status != NO_ERROR || newOutput == AUDIO_IO_HANDLE_NONE) {
Eric Laurent6d607012021-07-05 11:54:40 +0200403 mLock.lock();
Eric Laurent39095982021-08-24 18:29:27 +0200404 return;
405 }
406 status = mSpatializer->attachOutput(newOutput);
407 mLock.lock();
408 if (status != NO_ERROR) {
409 mAudioPolicyManager->releaseSpatializerOutput(newOutput);
410 }
411 } else if (mSpatializer->getLevel() == media::SpatializationLevel::NONE
412 && mSpatializer->getOutput() != AUDIO_IO_HANDLE_NONE) {
413 mLock.unlock();
414 audio_io_handle_t output = mSpatializer->detachOutput();
415 mLock.lock();
416 if (output != AUDIO_IO_HANDLE_NONE) {
417 mAudioPolicyManager->releaseSpatializerOutput(output);
Eric Laurent6d607012021-07-05 11:54:40 +0200418 }
419 }
420 }
421}
422
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -0800423status_t AudioPolicyService::clientCreateAudioPatch(const struct audio_patch *patch,
424 audio_patch_handle_t *handle,
425 int delayMs)
426{
427 return mAudioCommandThread->createAudioPatchCommand(patch, handle, delayMs);
428}
429
430status_t AudioPolicyService::clientReleaseAudioPatch(audio_patch_handle_t handle,
431 int delayMs)
432{
433 return mAudioCommandThread->releaseAudioPatchCommand(handle, delayMs);
434}
435
Eric Laurente1715a42014-05-20 11:30:42 -0700436status_t AudioPolicyService::clientSetAudioPortConfig(const struct audio_port_config *config,
437 int delayMs)
438{
439 return mAudioCommandThread->setAudioPortConfigCommand(config, delayMs);
440}
441
Ytai Ben-Tsvi7e7a79d2020-12-15 16:48:16 -0800442AudioPolicyService::NotificationClient::NotificationClient(
443 const sp<AudioPolicyService>& service,
444 const sp<media::IAudioPolicyServiceClient>& client,
445 uid_t uid,
446 pid_t pid)
luochaojiang908c7d72018-06-21 14:58:04 +0800447 : mService(service), mUid(uid), mPid(pid), mAudioPolicyServiceClient(client),
François Gaffiecfe17322018-11-07 13:41:29 +0100448 mAudioPortCallbacksEnabled(false), mAudioVolumeGroupCallbacksEnabled(false)
Eric Laurentb52c1522014-05-20 11:27:36 -0700449{
450}
451
452AudioPolicyService::NotificationClient::~NotificationClient()
453{
454}
455
456void AudioPolicyService::NotificationClient::binderDied(const wp<IBinder>& who __unused)
457{
458 sp<NotificationClient> keep(this);
459 sp<AudioPolicyService> service = mService.promote();
460 if (service != 0) {
luochaojiang908c7d72018-06-21 14:58:04 +0800461 service->removeNotificationClient(mUid, mPid);
Eric Laurentb52c1522014-05-20 11:27:36 -0700462 }
463}
464
465void AudioPolicyService::NotificationClient::onAudioPortListUpdate()
466{
Eric Laurente8726fe2015-06-26 09:39:24 -0700467 if (mAudioPolicyServiceClient != 0 && mAudioPortCallbacksEnabled) {
Eric Laurentb52c1522014-05-20 11:27:36 -0700468 mAudioPolicyServiceClient->onAudioPortListUpdate();
469 }
470}
471
472void AudioPolicyService::NotificationClient::onAudioPatchListUpdate()
473{
Eric Laurente8726fe2015-06-26 09:39:24 -0700474 if (mAudioPolicyServiceClient != 0 && mAudioPortCallbacksEnabled) {
Eric Laurentb52c1522014-05-20 11:27:36 -0700475 mAudioPolicyServiceClient->onAudioPatchListUpdate();
476 }
477}
Eric Laurent57dae992011-07-24 13:36:09 -0700478
François Gaffiecfe17322018-11-07 13:41:29 +0100479void AudioPolicyService::NotificationClient::onAudioVolumeGroupChanged(volume_group_t group,
480 int flags)
481{
482 if (mAudioPolicyServiceClient != 0 && mAudioVolumeGroupCallbacksEnabled) {
483 mAudioPolicyServiceClient->onAudioVolumeGroupChanged(group, flags);
484 }
485}
486
487
Jean-Michel Trivide801052015-04-14 19:10:14 -0700488void AudioPolicyService::NotificationClient::onDynamicPolicyMixStateUpdate(
Chih-Hung Hsiehe964d4e2016-08-09 14:31:32 -0700489 const String8& regId, int32_t state)
Jean-Michel Trivide801052015-04-14 19:10:14 -0700490{
Andy Hung4ef19fa2018-05-15 19:35:29 -0700491 if (mAudioPolicyServiceClient != 0 && isServiceUid(mUid)) {
Ytai Ben-Tsvi7e7a79d2020-12-15 16:48:16 -0800492 mAudioPolicyServiceClient->onDynamicPolicyMixStateUpdate(
493 legacy2aidl_String8_string(regId).value(), state);
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -0800494 }
495}
496
497void AudioPolicyService::NotificationClient::onRecordingConfigurationUpdate(
Eric Laurenta9f86652018-11-28 17:23:11 -0800498 int event,
499 const record_client_info_t *clientInfo,
500 const audio_config_base_t *clientConfig,
501 std::vector<effect_descriptor_t> clientEffects,
502 const audio_config_base_t *deviceConfig,
503 std::vector<effect_descriptor_t> effects,
504 audio_patch_handle_t patchHandle,
505 audio_source_t source)
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -0800506{
Andy Hung4ef19fa2018-05-15 19:35:29 -0700507 if (mAudioPolicyServiceClient != 0 && isServiceUid(mUid)) {
Ytai Ben-Tsvi7e7a79d2020-12-15 16:48:16 -0800508 status_t status = [&]() -> status_t {
509 int32_t eventAidl = VALUE_OR_RETURN_STATUS(convertIntegral<int32_t>(event));
510 media::RecordClientInfo clientInfoAidl = VALUE_OR_RETURN_STATUS(
511 legacy2aidl_record_client_info_t_RecordClientInfo(*clientInfo));
512 media::AudioConfigBase clientConfigAidl = VALUE_OR_RETURN_STATUS(
513 legacy2aidl_audio_config_base_t_AudioConfigBase(*clientConfig));
514 std::vector<media::EffectDescriptor> clientEffectsAidl = VALUE_OR_RETURN_STATUS(
515 convertContainer<std::vector<media::EffectDescriptor>>(
516 clientEffects,
517 legacy2aidl_effect_descriptor_t_EffectDescriptor));
518 media::AudioConfigBase deviceConfigAidl = VALUE_OR_RETURN_STATUS(
519 legacy2aidl_audio_config_base_t_AudioConfigBase(*deviceConfig));
520 std::vector<media::EffectDescriptor> effectsAidl = VALUE_OR_RETURN_STATUS(
521 convertContainer<std::vector<media::EffectDescriptor>>(
522 effects,
523 legacy2aidl_effect_descriptor_t_EffectDescriptor));
524 int32_t patchHandleAidl = VALUE_OR_RETURN_STATUS(
525 legacy2aidl_audio_patch_handle_t_int32_t(patchHandle));
526 media::AudioSourceType sourceAidl = VALUE_OR_RETURN_STATUS(
527 legacy2aidl_audio_source_t_AudioSourceType(source));
528 return aidl_utils::statusTFromBinderStatus(
529 mAudioPolicyServiceClient->onRecordingConfigurationUpdate(eventAidl,
530 clientInfoAidl,
531 clientConfigAidl,
532 clientEffectsAidl,
533 deviceConfigAidl,
534 effectsAidl,
535 patchHandleAidl,
536 sourceAidl));
537 }();
538 ALOGW_IF(status != OK, "onRecordingConfigurationUpdate() failed: %d", status);
Jean-Michel Trivide801052015-04-14 19:10:14 -0700539 }
540}
541
Eric Laurente8726fe2015-06-26 09:39:24 -0700542void AudioPolicyService::NotificationClient::setAudioPortCallbacksEnabled(bool enabled)
543{
544 mAudioPortCallbacksEnabled = enabled;
545}
546
François Gaffiecfe17322018-11-07 13:41:29 +0100547void AudioPolicyService::NotificationClient::setAudioVolumeGroupCallbacksEnabled(bool enabled)
548{
549 mAudioVolumeGroupCallbacksEnabled = enabled;
550}
Eric Laurente8726fe2015-06-26 09:39:24 -0700551
Jean-Michel Trivi9a6b9ad2020-10-22 16:46:43 -0700552void AudioPolicyService::NotificationClient::onRoutingUpdated()
553{
554 if (mAudioPolicyServiceClient != 0 && isServiceUid(mUid)) {
555 mAudioPolicyServiceClient->onRoutingUpdated();
556 }
557}
558
Mathias Agopian65ab4712010-07-14 17:59:35 -0700559void AudioPolicyService::binderDied(const wp<IBinder>& who) {
Glenn Kasten411e4472012-11-02 10:00:06 -0700560 ALOGW("binderDied() %p, calling pid %d", who.unsafe_get(),
Eric Laurentde070132010-07-13 04:45:46 -0700561 IPCThreadState::self()->getCallingPid());
Mathias Agopian65ab4712010-07-14 17:59:35 -0700562}
563
Mikhail Naganov12b716c2020-04-30 22:37:43 +0000564static bool dumpTryLock(Mutex& mutex) ACQUIRE(mutex) NO_THREAD_SAFETY_ANALYSIS
Mathias Agopian65ab4712010-07-14 17:59:35 -0700565{
Mikhail Naganov12b716c2020-04-30 22:37:43 +0000566 return mutex.timedLock(kDumpLockTimeoutNs) == NO_ERROR;
567}
568
569static void dumpReleaseLock(Mutex& mutex, bool locked) RELEASE(mutex) NO_THREAD_SAFETY_ANALYSIS
570{
571 if (locked) mutex.unlock();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700572}
573
574status_t AudioPolicyService::dumpInternals(int fd)
575{
576 const size_t SIZE = 256;
577 char buffer[SIZE];
578 String8 result;
579
Eric Laurentdce54a12014-03-10 12:19:46 -0700580 snprintf(buffer, SIZE, "AudioPolicyManager: %p\n", mAudioPolicyManager);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700581 result.append(buffer);
582 snprintf(buffer, SIZE, "Command Thread: %p\n", mAudioCommandThread.get());
583 result.append(buffer);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700584
Hayden Gomes524159d2019-12-23 14:41:47 -0800585 snprintf(buffer, SIZE, "Supported System Usages:\n");
586 result.append(buffer);
587 for (std::vector<audio_usage_t>::iterator it = mSupportedSystemUsages.begin();
588 it != mSupportedSystemUsages.end(); ++it) {
589 snprintf(buffer, SIZE, "\t%d\n", *it);
590 result.append(buffer);
591 }
592
Mathias Agopian65ab4712010-07-14 17:59:35 -0700593 write(fd, result.string(), result.size());
594 return NO_ERROR;
595}
596
Eric Laurente8c8b432018-10-17 10:08:02 -0700597void AudioPolicyService::updateUidStates()
Svet Ganovf4ddfef2018-01-16 07:37:58 -0800598{
Eric Laurente8c8b432018-10-17 10:08:02 -0700599 Mutex::Autolock _l(mLock);
600 updateUidStates_l();
601}
602
603void AudioPolicyService::updateUidStates_l()
604{
Eric Laurent4eb58f12018-12-07 16:41:02 -0800605// Go over all active clients and allow capture (does not force silence) in the
606// following cases:
Evan Severson1f700cd2021-02-10 13:10:37 -0800607// The client is the assistant
608// AND an accessibility service is on TOP or a RTT call is active
Eric Laurent589171c2019-07-25 18:04:29 -0700609// AND the source is VOICE_RECOGNITION or HOTWORD
Evan Severson1f700cd2021-02-10 13:10:37 -0800610// OR uses VOICE_RECOGNITION AND is on TOP
611// OR uses HOTWORD
612// AND there is no active privacy sensitive capture or call
613// OR client has CAPTURE_AUDIO_OUTPUT privileged permission
614// OR The client is an accessibility service
615// AND Is on TOP
616// AND the source is VOICE_RECOGNITION or HOTWORD
617// OR The assistant is not on TOP
Eric Laurent589171c2019-07-25 18:04:29 -0700618// AND there is no active privacy sensitive capture or call
619// OR client has CAPTURE_AUDIO_OUTPUT privileged permission
Evan Severson1f700cd2021-02-10 13:10:37 -0800620// AND is on TOP
621// AND the source is VOICE_RECOGNITION or HOTWORD
622// OR the client source is virtual (remote submix, call audio TX or RX...)
623// OR the client source is HOTWORD
624// AND is on TOP
625// OR all active clients are using HOTWORD source
626// AND no call is active
627// OR client has CAPTURE_AUDIO_OUTPUT privileged permission
628// OR the client is the current InputMethodService
629// AND a RTT call is active AND the source is VOICE_RECOGNITION
630// OR Any client
631// AND The assistant is not on TOP
632// AND is on TOP or latest started
633// AND there is no active privacy sensitive capture or call
634// OR client has CAPTURE_AUDIO_OUTPUT privileged permission
Eric Laurent4eb58f12018-12-07 16:41:02 -0800635
Eric Laurent4e947da2019-10-17 15:24:06 -0700636
Eric Laurent4eb58f12018-12-07 16:41:02 -0800637 sp<AudioRecordClient> topActive;
638 sp<AudioRecordClient> latestActive;
Eric Laurentc21d5692020-02-25 10:24:36 -0800639 sp<AudioRecordClient> topSensitiveActive;
Eric Laurentb809a752020-06-29 09:53:13 -0700640 sp<AudioRecordClient> latestSensitiveActiveOrComm;
Eric Laurent1ff16a72019-03-14 18:35:04 -0700641
Eric Laurenta46bedb2018-12-07 18:01:26 -0800642 nsecs_t topStartNs = 0;
643 nsecs_t latestStartNs = 0;
Eric Laurentc21d5692020-02-25 10:24:36 -0800644 nsecs_t topSensitiveStartNs = 0;
Eric Laurent4eb58f12018-12-07 16:41:02 -0800645 nsecs_t latestSensitiveStartNs = 0;
646 bool isA11yOnTop = mUidPolicy->isA11yOnTop();
647 bool isAssistantOnTop = false;
648 bool isSensitiveActive = false;
Eric Laurent1ff16a72019-03-14 18:35:04 -0700649 bool isInCall = mPhoneState == AUDIO_MODE_IN_CALL;
Eric Laurentc21d5692020-02-25 10:24:36 -0800650 bool isInCommunication = mPhoneState == AUDIO_MODE_IN_COMMUNICATION;
651 bool rttCallActive = (isInCall || isInCommunication)
Eric Laurent6ede98f2019-06-11 14:50:30 -0700652 && mUidPolicy->isRttEnabled();
Eric Laurent4e947da2019-10-17 15:24:06 -0700653 bool onlyHotwordActive = true;
Eric Laurentb809a752020-06-29 09:53:13 -0700654 bool isPhoneStateOwnerActive = false;
Eric Laurent4eb58f12018-12-07 16:41:02 -0800655
Michael Groovercfd28302018-12-11 19:16:46 -0800656 // if Sensor Privacy is enabled then all recordings should be silenced.
657 if (mSensorPrivacyPolicy->isSensorPrivacyEnabled()) {
658 silenceAllRecordings_l();
659 return;
660 }
661
Eric Laurente8c8b432018-10-17 10:08:02 -0700662 for (size_t i =0; i < mAudioRecordClients.size(); i++) {
663 sp<AudioRecordClient> current = mAudioRecordClients[i];
Svet Ganov33761132021-05-13 22:51:08 +0000664 uid_t currentUid = VALUE_OR_FATAL(aidl2legacy_int32_t_uid_t(
665 current->attributionSource.uid));
Evan Severson1f700cd2021-02-10 13:10:37 -0800666 if (!current->active) {
Eric Laurent1ff16a72019-03-14 18:35:04 -0700667 continue;
Eric Laurent4eb58f12018-12-07 16:41:02 -0800668 }
Eric Laurent1ff16a72019-03-14 18:35:04 -0700669
Philip P. Moltmannbda45752020-07-17 16:41:18 -0700670 app_state_t appState = apmStatFromAmState(mUidPolicy->getUidState(currentUid));
Eric Laurent1ff16a72019-03-14 18:35:04 -0700671 // clients which app is in IDLE state are not eligible for top active or
672 // latest active
673 if (appState == APP_STATE_IDLE) {
674 continue;
675 }
676
Philip P. Moltmannbda45752020-07-17 16:41:18 -0700677 bool isAccessibility = mUidPolicy->isA11yUid(currentUid);
Eric Laurent14a88632020-07-16 12:28:30 -0700678 // Clients capturing for Accessibility services or virtual sources are not considered
Eric Laurentc21d5692020-02-25 10:24:36 -0800679 // for top or latest active to avoid masking regular clients started before
Eric Laurent14a88632020-07-16 12:28:30 -0700680 if (!isAccessibility && !isVirtualSource(current->attributes.source)) {
Philip P. Moltmannbda45752020-07-17 16:41:18 -0700681 bool isAssistant = mUidPolicy->isAssistantUid(currentUid);
Eric Laurentc21d5692020-02-25 10:24:36 -0800682 bool isPrivacySensitive =
683 (current->attributes.flags & AUDIO_FLAG_CAPTURE_PRIVATE) != 0;
Eric Laurentb809a752020-06-29 09:53:13 -0700684
Eric Laurentc21d5692020-02-25 10:24:36 -0800685 if (appState == APP_STATE_TOP) {
686 if (isPrivacySensitive) {
687 if (current->startTimeNs > topSensitiveStartNs) {
688 topSensitiveActive = current;
689 topSensitiveStartNs = current->startTimeNs;
690 }
691 } else {
692 if (current->startTimeNs > topStartNs) {
693 topActive = current;
694 topStartNs = current->startTimeNs;
695 }
696 }
697 if (isAssistant) {
698 isAssistantOnTop = true;
699 }
Eric Laurenta46bedb2018-12-07 18:01:26 -0800700 }
Eric Laurentc21d5692020-02-25 10:24:36 -0800701 // Clients capturing for HOTWORD are not considered
702 // for latest active to avoid masking regular clients started before
703 if (!(current->attributes.source == AUDIO_SOURCE_HOTWORD
704 || ((isA11yOnTop || rttCallActive) && isAssistant))) {
705 if (isPrivacySensitive) {
Eric Laurentb809a752020-06-29 09:53:13 -0700706 // if audio mode is IN_COMMUNICATION, make sure the audio mode owner
707 // is marked latest sensitive active even if another app qualifies.
708 if (current->startTimeNs > latestSensitiveStartNs
Philip P. Moltmannbda45752020-07-17 16:41:18 -0700709 || (isInCommunication && currentUid == mPhoneStateOwnerUid)) {
Eric Laurentb809a752020-06-29 09:53:13 -0700710 if (!isInCommunication || latestSensitiveActiveOrComm == nullptr
Philip P. Moltmannbda45752020-07-17 16:41:18 -0700711 || VALUE_OR_FATAL(aidl2legacy_int32_t_uid_t(
Svet Ganov33761132021-05-13 22:51:08 +0000712 latestSensitiveActiveOrComm->attributionSource.uid))
Philip P. Moltmannbda45752020-07-17 16:41:18 -0700713 != mPhoneStateOwnerUid) {
Eric Laurentb809a752020-06-29 09:53:13 -0700714 latestSensitiveActiveOrComm = current;
715 latestSensitiveStartNs = current->startTimeNs;
716 }
Eric Laurentc21d5692020-02-25 10:24:36 -0800717 }
718 isSensitiveActive = true;
719 } else {
720 if (current->startTimeNs > latestStartNs) {
721 latestActive = current;
722 latestStartNs = current->startTimeNs;
723 }
724 }
Eric Laurent4eb58f12018-12-07 16:41:02 -0800725 }
726 }
Eric Laurent4e947da2019-10-17 15:24:06 -0700727 if (current->attributes.source != AUDIO_SOURCE_HOTWORD) {
728 onlyHotwordActive = false;
729 }
Philip P. Moltmannbda45752020-07-17 16:41:18 -0700730 if (currentUid == mPhoneStateOwnerUid) {
Eric Laurentb809a752020-06-29 09:53:13 -0700731 isPhoneStateOwnerActive = true;
732 }
Eric Laurent4eb58f12018-12-07 16:41:02 -0800733 }
734
Eric Laurent1ff16a72019-03-14 18:35:04 -0700735 // if no active client with UI on Top, consider latest active as top
736 if (topActive == nullptr) {
737 topActive = latestActive;
Eric Laurentc21d5692020-02-25 10:24:36 -0800738 topStartNs = latestStartNs;
739 }
740 if (topSensitiveActive == nullptr) {
Eric Laurentb809a752020-06-29 09:53:13 -0700741 topSensitiveActive = latestSensitiveActiveOrComm;
Eric Laurentc21d5692020-02-25 10:24:36 -0800742 topSensitiveStartNs = latestSensitiveStartNs;
Eric Laurentb809a752020-06-29 09:53:13 -0700743 } else if (latestSensitiveActiveOrComm != nullptr) {
744 // if audio mode is IN_COMMUNICATION, favor audio mode owner over an app with
745 // foreground UI in case both are capturing with privacy sensitive flag.
Philip P. Moltmannbda45752020-07-17 16:41:18 -0700746 uid_t latestActiveUid = VALUE_OR_FATAL(
Svet Ganov33761132021-05-13 22:51:08 +0000747 aidl2legacy_int32_t_uid_t(latestSensitiveActiveOrComm->attributionSource.uid));
Philip P. Moltmannbda45752020-07-17 16:41:18 -0700748 if (isInCommunication && latestActiveUid == mPhoneStateOwnerUid) {
Eric Laurentb809a752020-06-29 09:53:13 -0700749 topSensitiveActive = latestSensitiveActiveOrComm;
750 topSensitiveStartNs = latestSensitiveStartNs;
751 }
Eric Laurentc21d5692020-02-25 10:24:36 -0800752 }
753
754 // If both privacy sensitive and regular capture are active:
755 // if the regular capture is privileged
756 // allow concurrency
757 // else
758 // favor the privacy sensitive case
759 if (topActive != nullptr && topSensitiveActive != nullptr
Ricardo Correa57a37692020-03-23 17:27:25 -0700760 && !topActive->canCaptureOutput) {
Eric Laurentc21d5692020-02-25 10:24:36 -0800761 topActive = nullptr;
Eric Laurent4eb58f12018-12-07 16:41:02 -0800762 }
763
764 for (size_t i =0; i < mAudioRecordClients.size(); i++) {
765 sp<AudioRecordClient> current = mAudioRecordClients[i];
Philip P. Moltmannbda45752020-07-17 16:41:18 -0700766 uid_t currentUid = VALUE_OR_FATAL(aidl2legacy_int32_t_uid_t(
Svet Ganov33761132021-05-13 22:51:08 +0000767 current->attributionSource.uid));
Eric Laurent1ff16a72019-03-14 18:35:04 -0700768 if (!current->active) {
769 continue;
770 }
771
Eric Laurent4eb58f12018-12-07 16:41:02 -0800772 audio_source_t source = current->attributes.source;
Philip P. Moltmannbda45752020-07-17 16:41:18 -0700773 bool isTopOrLatestActive = topActive == nullptr ? false :
Svet Ganov33761132021-05-13 22:51:08 +0000774 current->attributionSource.uid == topActive->attributionSource.uid;
Philip P. Moltmannbda45752020-07-17 16:41:18 -0700775 bool isTopOrLatestSensitive = topSensitiveActive == nullptr ? false :
Svet Ganov33761132021-05-13 22:51:08 +0000776 current->attributionSource.uid == topSensitiveActive->attributionSource.uid;
Eric Laurentc21d5692020-02-25 10:24:36 -0800777
Mikhail Naganov12b716c2020-04-30 22:37:43 +0000778 auto canCaptureIfInCallOrCommunication = [&](const auto &recordClient) REQUIRES(mLock) {
Philip P. Moltmannbda45752020-07-17 16:41:18 -0700779 uid_t recordUid = VALUE_OR_FATAL(aidl2legacy_int32_t_uid_t(
Svet Ganov33761132021-05-13 22:51:08 +0000780 recordClient->attributionSource.uid));
Ricardo Correa57a37692020-03-23 17:27:25 -0700781 bool canCaptureCall = recordClient->canCaptureOutput;
Eric Laurentb809a752020-06-29 09:53:13 -0700782 bool canCaptureCommunication = recordClient->canCaptureOutput
783 || !isPhoneStateOwnerActive
Philip P. Moltmannbda45752020-07-17 16:41:18 -0700784 || recordUid == mPhoneStateOwnerUid;
Eric Laurentb809a752020-06-29 09:53:13 -0700785 return !(isInCall && !canCaptureCall)
786 && !(isInCommunication && !canCaptureCommunication);
Eric Laurentc21d5692020-02-25 10:24:36 -0800787 };
Eric Laurent1ff16a72019-03-14 18:35:04 -0700788
789 // By default allow capture if:
790 // The assistant is not on TOP
Eric Laurenta171e352019-05-07 13:04:45 -0700791 // AND is on TOP or latest started
Eric Laurent1ff16a72019-03-14 18:35:04 -0700792 // AND there is no active privacy sensitive capture or call
793 // OR client has CAPTURE_AUDIO_OUTPUT privileged permission
794 bool allowCapture = !isAssistantOnTop
Eric Laurentc21d5692020-02-25 10:24:36 -0800795 && (isTopOrLatestActive || isTopOrLatestSensitive)
796 && !(isSensitiveActive
Ricardo Correa57a37692020-03-23 17:27:25 -0700797 && !(isTopOrLatestSensitive || current->canCaptureOutput))
Eric Laurentc21d5692020-02-25 10:24:36 -0800798 && canCaptureIfInCallOrCommunication(current);
Eric Laurent2dc962b2019-03-01 08:25:25 -0800799
Eric Laurented726cc2021-07-01 14:26:41 +0200800 if (!current->hasOp()) {
801 // Never allow capture if app op is denied
802 allowCapture = false;
803 } else if (isVirtualSource(source)) {
Eric Laurent1ff16a72019-03-14 18:35:04 -0700804 // Allow capture for virtual (remote submix, call audio TX or RX...) sources
805 allowCapture = true;
Philip P. Moltmannbda45752020-07-17 16:41:18 -0700806 } else if (mUidPolicy->isAssistantUid(currentUid)) {
Eric Laurent1ff16a72019-03-14 18:35:04 -0700807 // For assistant allow capture if:
Eric Laurent6ede98f2019-06-11 14:50:30 -0700808 // An accessibility service is on TOP or a RTT call is active
Eric Laurent1ff16a72019-03-14 18:35:04 -0700809 // AND the source is VOICE_RECOGNITION or HOTWORD
Eric Laurenta171e352019-05-07 13:04:45 -0700810 // OR is on TOP AND uses VOICE_RECOGNITION
Eric Laurent1ff16a72019-03-14 18:35:04 -0700811 // OR uses HOTWORD
812 // AND there is no active privacy sensitive capture or call
813 // OR client has CAPTURE_AUDIO_OUTPUT privileged permission
Eric Laurent6ede98f2019-06-11 14:50:30 -0700814 if (isA11yOnTop || rttCallActive) {
Eric Laurent4eb58f12018-12-07 16:41:02 -0800815 if (source == AUDIO_SOURCE_HOTWORD || source == AUDIO_SOURCE_VOICE_RECOGNITION) {
Eric Laurent1ff16a72019-03-14 18:35:04 -0700816 allowCapture = true;
Eric Laurent4eb58f12018-12-07 16:41:02 -0800817 }
818 } else {
Eric Laurenta171e352019-05-07 13:04:45 -0700819 if (((isAssistantOnTop && source == AUDIO_SOURCE_VOICE_RECOGNITION) ||
Eric Laurentc21d5692020-02-25 10:24:36 -0800820 source == AUDIO_SOURCE_HOTWORD)
Ricardo Correa57a37692020-03-23 17:27:25 -0700821 && !(isSensitiveActive && !current->canCaptureOutput)
Eric Laurentc21d5692020-02-25 10:24:36 -0800822 && canCaptureIfInCallOrCommunication(current)) {
Eric Laurent1ff16a72019-03-14 18:35:04 -0700823 allowCapture = true;
Eric Laurent4eb58f12018-12-07 16:41:02 -0800824 }
825 }
Philip P. Moltmannbda45752020-07-17 16:41:18 -0700826 } else if (mUidPolicy->isA11yUid(currentUid)) {
Eric Laurent1ff16a72019-03-14 18:35:04 -0700827 // For accessibility service allow capture if:
Eric Laurent47670c92019-08-28 16:59:05 -0700828 // The assistant is not on TOP
829 // AND there is no active privacy sensitive capture or call
Eric Laurent589171c2019-07-25 18:04:29 -0700830 // OR client has CAPTURE_AUDIO_OUTPUT privileged permission
Eric Laurent47670c92019-08-28 16:59:05 -0700831 // OR
832 // Is on TOP AND the source is VOICE_RECOGNITION or HOTWORD
833 if (!isAssistantOnTop
Ricardo Correa57a37692020-03-23 17:27:25 -0700834 && !(isSensitiveActive && !current->canCaptureOutput)
Eric Laurentc21d5692020-02-25 10:24:36 -0800835 && canCaptureIfInCallOrCommunication(current)) {
Eric Laurent47670c92019-08-28 16:59:05 -0700836 allowCapture = true;
837 }
Eric Laurent589171c2019-07-25 18:04:29 -0700838 if (isA11yOnTop) {
839 if (source == AUDIO_SOURCE_VOICE_RECOGNITION || source == AUDIO_SOURCE_HOTWORD) {
840 allowCapture = true;
841 }
Eric Laurent4eb58f12018-12-07 16:41:02 -0800842 }
Eric Laurent4e947da2019-10-17 15:24:06 -0700843 } else if (source == AUDIO_SOURCE_HOTWORD) {
844 // For HOTWORD source allow capture when not on TOP if:
845 // All active clients are using HOTWORD source
846 // AND no call is active
847 // OR client has CAPTURE_AUDIO_OUTPUT privileged permission
Eric Laurentc21d5692020-02-25 10:24:36 -0800848 if (onlyHotwordActive
849 && canCaptureIfInCallOrCommunication(current)) {
Eric Laurent4e947da2019-10-17 15:24:06 -0700850 allowCapture = true;
851 }
Philip P. Moltmannbda45752020-07-17 16:41:18 -0700852 } else if (mUidPolicy->isCurrentImeUid(currentUid)) {
Kohsuke Yatoha623a132020-03-24 20:10:26 -0700853 // For current InputMethodService allow capture if:
854 // A RTT call is active AND the source is VOICE_RECOGNITION
855 if (rttCallActive && source == AUDIO_SOURCE_VOICE_RECOGNITION) {
856 allowCapture = true;
857 }
Eric Laurent4eb58f12018-12-07 16:41:02 -0800858 }
Eric Laurent8c7ef892021-06-10 13:32:16 +0200859 setAppState_l(current,
Philip P. Moltmannbda45752020-07-17 16:41:18 -0700860 allowCapture ? apmStatFromAmState(mUidPolicy->getUidState(currentUid)) :
Eric Laurent1ff16a72019-03-14 18:35:04 -0700861 APP_STATE_IDLE);
Eric Laurente8c8b432018-10-17 10:08:02 -0700862 }
863}
864
Michael Groovercfd28302018-12-11 19:16:46 -0800865void AudioPolicyService::silenceAllRecordings_l() {
866 for (size_t i = 0; i < mAudioRecordClients.size(); i++) {
867 sp<AudioRecordClient> current = mAudioRecordClients[i];
Eric Laurent1ff16a72019-03-14 18:35:04 -0700868 if (!isVirtualSource(current->attributes.source)) {
Eric Laurent8c7ef892021-06-10 13:32:16 +0200869 setAppState_l(current, APP_STATE_IDLE);
Eric Laurent1ff16a72019-03-14 18:35:04 -0700870 }
Michael Groovercfd28302018-12-11 19:16:46 -0800871 }
872}
873
Eric Laurente8c8b432018-10-17 10:08:02 -0700874/* static */
875app_state_t AudioPolicyService::apmStatFromAmState(int amState) {
Eric Laurent1ff16a72019-03-14 18:35:04 -0700876
877 if (amState == ActivityManager::PROCESS_STATE_UNKNOWN) {
Eric Laurente8c8b432018-10-17 10:08:02 -0700878 return APP_STATE_IDLE;
Eric Laurent1ff16a72019-03-14 18:35:04 -0700879 } else if (amState <= ActivityManager::PROCESS_STATE_TOP) {
880 // include persistent services
881 return APP_STATE_TOP;
Eric Laurente8c8b432018-10-17 10:08:02 -0700882 }
883 return APP_STATE_FOREGROUND;
884}
885
Eric Laurent4eb58f12018-12-07 16:41:02 -0800886/* static */
Eric Laurent2dc962b2019-03-01 08:25:25 -0800887bool AudioPolicyService::isVirtualSource(audio_source_t source)
Eric Laurent4eb58f12018-12-07 16:41:02 -0800888{
889 switch (source) {
890 case AUDIO_SOURCE_VOICE_UPLINK:
891 case AUDIO_SOURCE_VOICE_DOWNLINK:
892 case AUDIO_SOURCE_VOICE_CALL:
Eric Laurent2dc962b2019-03-01 08:25:25 -0800893 case AUDIO_SOURCE_REMOTE_SUBMIX:
894 case AUDIO_SOURCE_FM_TUNER:
Eric Laurent68eb2122020-04-30 17:40:57 -0700895 case AUDIO_SOURCE_ECHO_REFERENCE:
Eric Laurent4eb58f12018-12-07 16:41:02 -0800896 return true;
897 default:
898 break;
899 }
900 return false;
901}
902
Eric Laurented726cc2021-07-01 14:26:41 +0200903/* static */
904bool AudioPolicyService::isAppOpSource(audio_source_t source)
905{
906 switch (source) {
907 case AUDIO_SOURCE_FM_TUNER:
908 case AUDIO_SOURCE_ECHO_REFERENCE:
909 return false;
910 default:
911 break;
912 }
913 return true;
914}
915
Eric Laurent8c7ef892021-06-10 13:32:16 +0200916void AudioPolicyService::setAppState_l(sp<AudioRecordClient> client, app_state_t state)
Eric Laurente8c8b432018-10-17 10:08:02 -0700917{
918 AutoCallerClear acc;
919
920 if (mAudioPolicyManager) {
Eric Laurent8c7ef892021-06-10 13:32:16 +0200921 mAudioPolicyManager->setAppState(client->portId, state);
Mikhail Naganoveae73eb2018-04-03 16:57:36 -0700922 }
923 sp<IAudioFlinger> af = AudioSystem::get_audio_flinger();
924 if (af) {
Eric Laurentf32108e2018-10-04 17:22:04 -0700925 bool silenced = state == APP_STATE_IDLE;
Eric Laurent8c7ef892021-06-10 13:32:16 +0200926 if (client->silenced != silenced) {
927 if (client->active) {
928 if (silenced) {
929 finishRecording(client->attributionSource, client->attributes.source);
930 } else {
931 std::stringstream msg;
932 msg << "Audio recording un-silenced on session " << client->session;
933 if (!startRecording(client->attributionSource, String16(msg.str().c_str()),
934 client->attributes.source)) {
935 silenced = true;
936 }
937 }
938 }
939 af->setRecordSilenced(client->portId, silenced);
940 client->silenced = silenced;
941 }
Mikhail Naganoveae73eb2018-04-03 16:57:36 -0700942 }
Svet Ganovf4ddfef2018-01-16 07:37:58 -0800943}
944
Glenn Kasten0f11b512014-01-31 16:18:54 -0800945status_t AudioPolicyService::dump(int fd, const Vector<String16>& args __unused)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700946{
Glenn Kasten44deb052012-02-05 18:09:08 -0800947 if (!dumpAllowed()) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700948 dumpPermissionDenial(fd);
949 } else {
Mikhail Naganov12b716c2020-04-30 22:37:43 +0000950 const bool locked = dumpTryLock(mLock);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700951 if (!locked) {
952 String8 result(kDeadlockedString);
953 write(fd, result.string(), result.size());
954 }
955
956 dumpInternals(fd);
Glenn Kasten9d1f02d2012-02-08 17:47:58 -0800957 if (mAudioCommandThread != 0) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700958 mAudioCommandThread->dump(fd);
959 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700960
Eric Laurentdce54a12014-03-10 12:19:46 -0700961 if (mAudioPolicyManager) {
962 mAudioPolicyManager->dump(fd);
963 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700964
Kevin Rocard8be94972019-02-22 13:26:25 -0800965 mPackageManager.dump(fd);
966
Mikhail Naganov12b716c2020-04-30 22:37:43 +0000967 dumpReleaseLock(mLock, locked);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700968 }
969 return NO_ERROR;
970}
971
972status_t AudioPolicyService::dumpPermissionDenial(int fd)
973{
974 const size_t SIZE = 256;
975 char buffer[SIZE];
976 String8 result;
977 snprintf(buffer, SIZE, "Permission Denial: "
978 "can't dump AudioPolicyService from pid=%d, uid=%d\n",
979 IPCThreadState::self()->getCallingPid(),
980 IPCThreadState::self()->getCallingUid());
981 result.append(buffer);
982 write(fd, result.string(), result.size());
983 return NO_ERROR;
984}
985
986status_t AudioPolicyService::onTransact(
Svet Ganovf4ddfef2018-01-16 07:37:58 -0800987 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags) {
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800988 // make sure transactions reserved to AudioFlinger do not come from other processes
989 switch (code) {
990 case TRANSACTION_startOutput:
991 case TRANSACTION_stopOutput:
992 case TRANSACTION_releaseOutput:
993 case TRANSACTION_getInputForAttr:
994 case TRANSACTION_startInput:
995 case TRANSACTION_stopInput:
996 case TRANSACTION_releaseInput:
997 case TRANSACTION_getOutputForEffect:
998 case TRANSACTION_registerEffect:
999 case TRANSACTION_unregisterEffect:
1000 case TRANSACTION_setEffectEnabled:
1001 case TRANSACTION_getStrategyForStream:
1002 case TRANSACTION_getOutputForAttr:
1003 case TRANSACTION_moveEffectsToIo:
1004 ALOGW("%s: transaction %d received from PID %d",
1005 __func__, code, IPCThreadState::self()->getCallingPid());
1006 return INVALID_OPERATION;
1007 default:
1008 break;
1009 }
1010
1011 // make sure the following transactions come from system components
1012 switch (code) {
1013 case TRANSACTION_setDeviceConnectionState:
1014 case TRANSACTION_handleDeviceConfigChange:
1015 case TRANSACTION_setPhoneState:
1016//FIXME: Allow setForceUse calls from system apps until a better use case routing API is available
1017// case TRANSACTION_setForceUse:
1018 case TRANSACTION_initStreamVolume:
1019 case TRANSACTION_setStreamVolumeIndex:
1020 case TRANSACTION_setVolumeIndexForAttributes:
1021 case TRANSACTION_getStreamVolumeIndex:
1022 case TRANSACTION_getVolumeIndexForAttributes:
1023 case TRANSACTION_getMinVolumeIndexForAttributes:
1024 case TRANSACTION_getMaxVolumeIndexForAttributes:
1025 case TRANSACTION_isStreamActive:
1026 case TRANSACTION_isStreamActiveRemotely:
1027 case TRANSACTION_isSourceActive:
1028 case TRANSACTION_getDevicesForStream:
1029 case TRANSACTION_registerPolicyMixes:
1030 case TRANSACTION_setMasterMono:
1031 case TRANSACTION_getSurroundFormats:
Kriti Dang6537def2021-03-02 13:46:59 +01001032 case TRANSACTION_getReportedSurroundFormats:
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001033 case TRANSACTION_setSurroundFormatEnabled:
1034 case TRANSACTION_setAssistantUid:
1035 case TRANSACTION_setA11yServicesUids:
1036 case TRANSACTION_setUidDeviceAffinities:
1037 case TRANSACTION_removeUidDeviceAffinities:
1038 case TRANSACTION_setUserIdDeviceAffinities:
1039 case TRANSACTION_removeUserIdDeviceAffinities:
1040 case TRANSACTION_getHwOffloadEncodingFormatsSupportedForA2DP:
1041 case TRANSACTION_listAudioVolumeGroups:
1042 case TRANSACTION_getVolumeGroupFromAudioAttributes:
1043 case TRANSACTION_acquireSoundTriggerSession:
1044 case TRANSACTION_releaseSoundTriggerSession:
1045 case TRANSACTION_setRttEnabled:
1046 case TRANSACTION_isCallScreenModeSupported:
1047 case TRANSACTION_setDevicesRoleForStrategy:
1048 case TRANSACTION_setSupportedSystemUsages:
1049 case TRANSACTION_removeDevicesRoleForStrategy:
1050 case TRANSACTION_getDevicesForRoleAndStrategy:
1051 case TRANSACTION_getDevicesForAttributes:
1052 case TRANSACTION_setAllowedCapturePolicy:
1053 case TRANSACTION_onNewAudioModulesAvailable:
1054 case TRANSACTION_setCurrentImeUid:
1055 case TRANSACTION_registerSoundTriggerCaptureStateListener:
1056 case TRANSACTION_setDevicesRoleForCapturePreset:
1057 case TRANSACTION_addDevicesRoleForCapturePreset:
1058 case TRANSACTION_removeDevicesRoleForCapturePreset:
1059 case TRANSACTION_clearDevicesRoleForCapturePreset:
Eric Laurent6d607012021-07-05 11:54:40 +02001060 case TRANSACTION_getDevicesForRoleAndCapturePreset:
1061 case TRANSACTION_getSpatializer: {
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08001062 if (!isServiceUid(IPCThreadState::self()->getCallingUid())) {
1063 ALOGW("%s: transaction %d received from PID %d unauthorized UID %d",
1064 __func__, code, IPCThreadState::self()->getCallingPid(),
1065 IPCThreadState::self()->getCallingUid());
1066 return INVALID_OPERATION;
1067 }
1068 } break;
1069 default:
1070 break;
1071 }
1072
1073 std::string tag("IAudioPolicyService command " + std::to_string(code));
1074 TimeCheck check(tag.c_str());
1075
Svet Ganovf4ddfef2018-01-16 07:37:58 -08001076 switch (code) {
1077 case SHELL_COMMAND_TRANSACTION: {
1078 int in = data.readFileDescriptor();
1079 int out = data.readFileDescriptor();
1080 int err = data.readFileDescriptor();
1081 int argc = data.readInt32();
1082 Vector<String16> args;
1083 for (int i = 0; i < argc && data.dataAvail() > 0; i++) {
1084 args.add(data.readString16());
1085 }
1086 sp<IBinder> unusedCallback;
1087 sp<IResultReceiver> resultReceiver;
1088 status_t status;
1089 if ((status = data.readNullableStrongBinder(&unusedCallback)) != NO_ERROR) {
1090 return status;
1091 }
1092 if ((status = data.readNullableStrongBinder(&resultReceiver)) != NO_ERROR) {
1093 return status;
1094 }
1095 status = shellCommand(in, out, err, args);
1096 if (resultReceiver != nullptr) {
1097 resultReceiver->send(status);
1098 }
1099 return NO_ERROR;
1100 }
1101 }
1102
Mathias Agopian65ab4712010-07-14 17:59:35 -07001103 return BnAudioPolicyService::onTransact(code, data, reply, flags);
1104}
1105
Svet Ganovf4ddfef2018-01-16 07:37:58 -08001106// ------------------- Shell command implementation -------------------
1107
1108// NOTE: This is a remote API - make sure all args are validated
1109status_t AudioPolicyService::shellCommand(int in, int out, int err, Vector<String16>& args) {
1110 if (!checkCallingPermission(sManageAudioPolicyPermission, nullptr, nullptr)) {
1111 return PERMISSION_DENIED;
1112 }
1113 if (in == BAD_TYPE || out == BAD_TYPE || err == BAD_TYPE) {
1114 return BAD_VALUE;
1115 }
jovanakbe066e12019-09-02 11:54:39 -07001116 if (args.size() >= 3 && args[0] == String16("set-uid-state")) {
Svet Ganovf4ddfef2018-01-16 07:37:58 -08001117 return handleSetUidState(args, err);
jovanakbe066e12019-09-02 11:54:39 -07001118 } else if (args.size() >= 2 && args[0] == String16("reset-uid-state")) {
Svet Ganovf4ddfef2018-01-16 07:37:58 -08001119 return handleResetUidState(args, err);
jovanakbe066e12019-09-02 11:54:39 -07001120 } else if (args.size() >= 2 && args[0] == String16("get-uid-state")) {
Svet Ganovf4ddfef2018-01-16 07:37:58 -08001121 return handleGetUidState(args, out, err);
Eric Laurent269acb42021-04-23 16:53:22 +02001122 } else if (args.size() >= 1 && args[0] == String16("purge_permission-cache")) {
1123 purgePermissionCache();
1124 return NO_ERROR;
Svet Ganovf4ddfef2018-01-16 07:37:58 -08001125 } else if (args.size() == 1 && args[0] == String16("help")) {
1126 printHelp(out);
1127 return NO_ERROR;
1128 }
1129 printHelp(err);
1130 return BAD_VALUE;
1131}
1132
jovanakbe066e12019-09-02 11:54:39 -07001133static status_t getUidForPackage(String16 packageName, int userId, /*inout*/uid_t& uid, int err) {
1134 if (userId < 0) {
1135 ALOGE("Invalid user: %d", userId);
1136 dprintf(err, "Invalid user: %d\n", userId);
Svet Ganovf4ddfef2018-01-16 07:37:58 -08001137 return BAD_VALUE;
1138 }
jovanakbe066e12019-09-02 11:54:39 -07001139
1140 PermissionController pc;
1141 uid = pc.getPackageUid(packageName, 0);
1142 if (uid <= 0) {
1143 ALOGE("Unknown package: '%s'", String8(packageName).string());
1144 dprintf(err, "Unknown package: '%s'\n", String8(packageName).string());
1145 return BAD_VALUE;
1146 }
1147
1148 uid = multiuser_get_uid(userId, uid);
1149 return NO_ERROR;
1150}
1151
1152status_t AudioPolicyService::handleSetUidState(Vector<String16>& args, int err) {
1153 // Valid arg.size() is 3 or 5, args.size() is 5 with --user option.
1154 if (!(args.size() == 3 || args.size() == 5)) {
1155 printHelp(err);
1156 return BAD_VALUE;
1157 }
1158
Svet Ganovf4ddfef2018-01-16 07:37:58 -08001159 bool active = false;
1160 if (args[2] == String16("active")) {
1161 active = true;
1162 } else if ((args[2] != String16("idle"))) {
1163 ALOGE("Expected active or idle but got: '%s'", String8(args[2]).string());
1164 return BAD_VALUE;
1165 }
jovanakbe066e12019-09-02 11:54:39 -07001166
1167 int userId = 0;
1168 if (args.size() >= 5 && args[3] == String16("--user")) {
1169 userId = atoi(String8(args[4]));
1170 }
1171
1172 uid_t uid;
1173 if (getUidForPackage(args[1], userId, uid, err) == BAD_VALUE) {
1174 return BAD_VALUE;
1175 }
1176
Mikhail Naganov12b716c2020-04-30 22:37:43 +00001177 sp<UidPolicy> uidPolicy;
1178 {
1179 Mutex::Autolock _l(mLock);
1180 uidPolicy = mUidPolicy;
1181 }
1182 if (uidPolicy) {
1183 uidPolicy->addOverrideUid(uid, active);
1184 return NO_ERROR;
1185 }
1186 return NO_INIT;
Svet Ganovf4ddfef2018-01-16 07:37:58 -08001187}
1188
1189status_t AudioPolicyService::handleResetUidState(Vector<String16>& args, int err) {
jovanakbe066e12019-09-02 11:54:39 -07001190 // Valid arg.size() is 2 or 4, args.size() is 4 with --user option.
1191 if (!(args.size() == 2 || args.size() == 4)) {
1192 printHelp(err);
Svet Ganovf4ddfef2018-01-16 07:37:58 -08001193 return BAD_VALUE;
1194 }
jovanakbe066e12019-09-02 11:54:39 -07001195
1196 int userId = 0;
1197 if (args.size() >= 4 && args[2] == String16("--user")) {
1198 userId = atoi(String8(args[3]));
1199 }
1200
1201 uid_t uid;
1202 if (getUidForPackage(args[1], userId, uid, err) == BAD_VALUE) {
1203 return BAD_VALUE;
1204 }
1205
Mikhail Naganov12b716c2020-04-30 22:37:43 +00001206 sp<UidPolicy> uidPolicy;
1207 {
1208 Mutex::Autolock _l(mLock);
1209 uidPolicy = mUidPolicy;
1210 }
1211 if (uidPolicy) {
1212 uidPolicy->removeOverrideUid(uid);
1213 return NO_ERROR;
1214 }
1215 return NO_INIT;
Svet Ganovf4ddfef2018-01-16 07:37:58 -08001216}
1217
1218status_t AudioPolicyService::handleGetUidState(Vector<String16>& args, int out, int err) {
jovanakbe066e12019-09-02 11:54:39 -07001219 // Valid arg.size() is 2 or 4, args.size() is 4 with --user option.
1220 if (!(args.size() == 2 || args.size() == 4)) {
1221 printHelp(err);
Svet Ganovf4ddfef2018-01-16 07:37:58 -08001222 return BAD_VALUE;
1223 }
jovanakbe066e12019-09-02 11:54:39 -07001224
1225 int userId = 0;
1226 if (args.size() >= 4 && args[2] == String16("--user")) {
1227 userId = atoi(String8(args[3]));
1228 }
1229
1230 uid_t uid;
1231 if (getUidForPackage(args[1], userId, uid, err) == BAD_VALUE) {
1232 return BAD_VALUE;
1233 }
1234
Mikhail Naganov12b716c2020-04-30 22:37:43 +00001235 sp<UidPolicy> uidPolicy;
1236 {
1237 Mutex::Autolock _l(mLock);
1238 uidPolicy = mUidPolicy;
Svet Ganovf4ddfef2018-01-16 07:37:58 -08001239 }
Mikhail Naganov12b716c2020-04-30 22:37:43 +00001240 if (uidPolicy) {
1241 return dprintf(out, uidPolicy->isUidActive(uid) ? "active\n" : "idle\n");
1242 }
1243 return NO_INIT;
Svet Ganovf4ddfef2018-01-16 07:37:58 -08001244}
1245
1246status_t AudioPolicyService::printHelp(int out) {
1247 return dprintf(out, "Audio policy service commands:\n"
jovanakbe066e12019-09-02 11:54:39 -07001248 " get-uid-state <PACKAGE> [--user USER_ID] gets the uid state\n"
1249 " set-uid-state <PACKAGE> <active|idle> [--user USER_ID] overrides the uid state\n"
1250 " reset-uid-state <PACKAGE> [--user USER_ID] clears the uid state override\n"
Svet Ganovf4ddfef2018-01-16 07:37:58 -08001251 " help print this message\n");
1252}
1253
1254// ----------- AudioPolicyService::UidPolicy implementation ----------
1255
1256void AudioPolicyService::UidPolicy::registerSelf() {
Steven Moreland2f348142019-07-02 15:59:07 -07001257 status_t res = mAm.linkToDeath(this);
1258 mAm.registerUidObserver(this, ActivityManager::UID_OBSERVER_GONE
Svet Ganovf4ddfef2018-01-16 07:37:58 -08001259 | ActivityManager::UID_OBSERVER_IDLE
Eric Laurente8c8b432018-10-17 10:08:02 -07001260 | ActivityManager::UID_OBSERVER_ACTIVE
1261 | ActivityManager::UID_OBSERVER_PROCSTATE,
Svet Ganovf4ddfef2018-01-16 07:37:58 -08001262 ActivityManager::PROCESS_STATE_UNKNOWN,
1263 String16("audioserver"));
Mikhail Naganoveae73eb2018-04-03 16:57:36 -07001264 if (!res) {
1265 Mutex::Autolock _l(mLock);
1266 mObserverRegistered = true;
1267 } else {
1268 ALOGE("UidPolicy::registerSelf linkToDeath failed: %d", res);
Eric Laurent4eb58f12018-12-07 16:41:02 -08001269
Steven Moreland2f348142019-07-02 15:59:07 -07001270 mAm.unregisterUidObserver(this);
Mikhail Naganoveae73eb2018-04-03 16:57:36 -07001271 }
Svet Ganovf4ddfef2018-01-16 07:37:58 -08001272}
1273
1274void AudioPolicyService::UidPolicy::unregisterSelf() {
Steven Moreland2f348142019-07-02 15:59:07 -07001275 mAm.unlinkToDeath(this);
1276 mAm.unregisterUidObserver(this);
Mikhail Naganoveae73eb2018-04-03 16:57:36 -07001277 Mutex::Autolock _l(mLock);
1278 mObserverRegistered = false;
Svet Ganovf4ddfef2018-01-16 07:37:58 -08001279}
1280
Mikhail Naganoveae73eb2018-04-03 16:57:36 -07001281void AudioPolicyService::UidPolicy::binderDied(__unused const wp<IBinder> &who) {
1282 Mutex::Autolock _l(mLock);
1283 mCachedUids.clear();
1284 mObserverRegistered = false;
Svet Ganovf4ddfef2018-01-16 07:37:58 -08001285}
1286
Eric Laurente8c8b432018-10-17 10:08:02 -07001287void AudioPolicyService::UidPolicy::checkRegistered() {
Mikhail Naganoveae73eb2018-04-03 16:57:36 -07001288 bool needToReregister = false;
1289 {
1290 Mutex::Autolock _l(mLock);
1291 needToReregister = !mObserverRegistered;
Svet Ganovf4ddfef2018-01-16 07:37:58 -08001292 }
Mikhail Naganoveae73eb2018-04-03 16:57:36 -07001293 if (needToReregister) {
1294 // Looks like ActivityManager has died previously, attempt to re-register.
1295 registerSelf();
1296 }
Eric Laurente8c8b432018-10-17 10:08:02 -07001297}
1298
1299bool AudioPolicyService::UidPolicy::isUidActive(uid_t uid) {
1300 if (isServiceUid(uid)) return true;
1301 checkRegistered();
Mikhail Naganoveae73eb2018-04-03 16:57:36 -07001302 {
1303 Mutex::Autolock _l(mLock);
1304 auto overrideIter = mOverrideUids.find(uid);
1305 if (overrideIter != mOverrideUids.end()) {
Eric Laurente8c8b432018-10-17 10:08:02 -07001306 return overrideIter->second.first;
Mikhail Naganoveae73eb2018-04-03 16:57:36 -07001307 }
1308 // In an absense of the ActivityManager, assume everything to be active.
1309 if (!mObserverRegistered) return true;
1310 auto cacheIter = mCachedUids.find(uid);
Mikhail Naganoveba668a2018-04-05 08:13:15 -07001311 if (cacheIter != mCachedUids.end()) {
Eric Laurente8c8b432018-10-17 10:08:02 -07001312 return cacheIter->second.first;
Mikhail Naganoveae73eb2018-04-03 16:57:36 -07001313 }
1314 }
1315 ActivityManager am;
Hui Yu12c7ec72020-05-04 17:40:52 +00001316 bool active = am.isUidActive(uid, String16("audioserver"));
Mikhail Naganoveae73eb2018-04-03 16:57:36 -07001317 {
1318 Mutex::Autolock _l(mLock);
Eric Laurente8c8b432018-10-17 10:08:02 -07001319 mCachedUids.insert(std::pair<uid_t,
1320 std::pair<bool, int>>(uid, std::pair<bool, int>(active,
1321 ActivityManager::PROCESS_STATE_UNKNOWN)));
Mikhail Naganoveae73eb2018-04-03 16:57:36 -07001322 }
1323 return active;
Svet Ganovf4ddfef2018-01-16 07:37:58 -08001324}
1325
Eric Laurente8c8b432018-10-17 10:08:02 -07001326int AudioPolicyService::UidPolicy::getUidState(uid_t uid) {
1327 if (isServiceUid(uid)) {
1328 return ActivityManager::PROCESS_STATE_TOP;
1329 }
1330 checkRegistered();
1331 {
1332 Mutex::Autolock _l(mLock);
1333 auto overrideIter = mOverrideUids.find(uid);
1334 if (overrideIter != mOverrideUids.end()) {
1335 if (overrideIter->second.first) {
1336 if (overrideIter->second.second != ActivityManager::PROCESS_STATE_UNKNOWN) {
1337 return overrideIter->second.second;
1338 } else {
1339 auto cacheIter = mCachedUids.find(uid);
1340 if (cacheIter != mCachedUids.end()) {
1341 return cacheIter->second.second;
1342 }
1343 }
1344 }
1345 return ActivityManager::PROCESS_STATE_UNKNOWN;
1346 }
1347 // In an absense of the ActivityManager, assume everything to be active.
1348 if (!mObserverRegistered) {
1349 return ActivityManager::PROCESS_STATE_TOP;
1350 }
1351 auto cacheIter = mCachedUids.find(uid);
1352 if (cacheIter != mCachedUids.end()) {
1353 if (cacheIter->second.first) {
1354 return cacheIter->second.second;
1355 } else {
1356 return ActivityManager::PROCESS_STATE_UNKNOWN;
1357 }
1358 }
1359 }
1360 ActivityManager am;
Hui Yu12c7ec72020-05-04 17:40:52 +00001361 bool active = am.isUidActive(uid, String16("audioserver"));
Eric Laurente8c8b432018-10-17 10:08:02 -07001362 int state = ActivityManager::PROCESS_STATE_UNKNOWN;
1363 if (active) {
1364 state = am.getUidProcessState(uid, String16("audioserver"));
1365 }
1366 {
1367 Mutex::Autolock _l(mLock);
1368 mCachedUids.insert(std::pair<uid_t,
1369 std::pair<bool, int>>(uid, std::pair<bool, int>(active, state)));
1370 }
Eric Laurent4eb58f12018-12-07 16:41:02 -08001371
Eric Laurente8c8b432018-10-17 10:08:02 -07001372 return state;
1373}
1374
Mikhail Naganoveae73eb2018-04-03 16:57:36 -07001375void AudioPolicyService::UidPolicy::onUidActive(uid_t uid) {
Eric Laurente8c8b432018-10-17 10:08:02 -07001376 updateUid(&mCachedUids, uid, true, ActivityManager::PROCESS_STATE_UNKNOWN, true);
Mikhail Naganoveae73eb2018-04-03 16:57:36 -07001377}
1378
1379void AudioPolicyService::UidPolicy::onUidGone(uid_t uid, __unused bool disabled) {
Eric Laurente8c8b432018-10-17 10:08:02 -07001380 updateUid(&mCachedUids, uid, false, ActivityManager::PROCESS_STATE_UNKNOWN, false);
Mikhail Naganoveae73eb2018-04-03 16:57:36 -07001381}
1382
1383void AudioPolicyService::UidPolicy::onUidIdle(uid_t uid, __unused bool disabled) {
Eric Laurente8c8b432018-10-17 10:08:02 -07001384 updateUid(&mCachedUids, uid, false, ActivityManager::PROCESS_STATE_UNKNOWN, true);
Mikhail Naganoveae73eb2018-04-03 16:57:36 -07001385}
1386
Eric Laurente8c8b432018-10-17 10:08:02 -07001387void AudioPolicyService::UidPolicy::onUidStateChanged(uid_t uid,
1388 int32_t procState,
Hui Yu13ad0eb2019-09-09 10:27:07 -07001389 int64_t procStateSeq __unused,
1390 int32_t capability __unused) {
Eric Laurente8c8b432018-10-17 10:08:02 -07001391 if (procState != ActivityManager::PROCESS_STATE_UNKNOWN) {
1392 updateUid(&mCachedUids, uid, true, procState, true);
Mikhail Naganoveae73eb2018-04-03 16:57:36 -07001393 }
1394}
1395
1396void AudioPolicyService::UidPolicy::updateOverrideUid(uid_t uid, bool active, bool insert) {
Eric Laurente8c8b432018-10-17 10:08:02 -07001397 updateUid(&mOverrideUids, uid, active, ActivityManager::PROCESS_STATE_UNKNOWN, insert);
1398}
1399
1400void AudioPolicyService::UidPolicy::notifyService() {
1401 sp<AudioPolicyService> service = mService.promote();
1402 if (service != nullptr) {
1403 service->updateUidStates();
Mikhail Naganoveae73eb2018-04-03 16:57:36 -07001404 }
1405}
1406
Eric Laurente8c8b432018-10-17 10:08:02 -07001407void AudioPolicyService::UidPolicy::updateUid(std::unordered_map<uid_t,
1408 std::pair<bool, int>> *uids,
1409 uid_t uid,
1410 bool active,
1411 int state,
1412 bool insert) {
1413 if (isServiceUid(uid)) {
1414 return;
1415 }
1416 bool wasActive = isUidActive(uid);
1417 int previousState = getUidState(uid);
Mikhail Naganoveae73eb2018-04-03 16:57:36 -07001418 {
1419 Mutex::Autolock _l(mLock);
Eric Laurente8c8b432018-10-17 10:08:02 -07001420 updateUidLocked(uids, uid, active, state, insert);
Mikhail Naganoveae73eb2018-04-03 16:57:36 -07001421 }
Eric Laurente8c8b432018-10-17 10:08:02 -07001422 if (wasActive != isUidActive(uid) || state != previousState) {
1423 notifyService();
1424 }
Mikhail Naganoveae73eb2018-04-03 16:57:36 -07001425}
1426
Eric Laurente8c8b432018-10-17 10:08:02 -07001427void AudioPolicyService::UidPolicy::updateUidLocked(std::unordered_map<uid_t,
1428 std::pair<bool, int>> *uids,
1429 uid_t uid,
1430 bool active,
1431 int state,
1432 bool insert) {
Mikhail Naganoveae73eb2018-04-03 16:57:36 -07001433 auto it = uids->find(uid);
1434 if (it != uids->end()) {
Mikhail Naganoveae73eb2018-04-03 16:57:36 -07001435 if (insert) {
Eric Laurente8c8b432018-10-17 10:08:02 -07001436 if (state == ActivityManager::PROCESS_STATE_UNKNOWN) {
1437 it->second.first = active;
1438 }
1439 if (it->second.first) {
1440 it->second.second = state;
1441 } else {
1442 it->second.second = ActivityManager::PROCESS_STATE_UNKNOWN;
1443 }
Mikhail Naganoveae73eb2018-04-03 16:57:36 -07001444 } else {
1445 uids->erase(it);
1446 }
Eric Laurente8c8b432018-10-17 10:08:02 -07001447 } else if (insert && (state == ActivityManager::PROCESS_STATE_UNKNOWN)) {
1448 uids->insert(std::pair<uid_t, std::pair<bool, int>>(uid,
1449 std::pair<bool, int>(active, state)));
Mikhail Naganoveae73eb2018-04-03 16:57:36 -07001450 }
Svet Ganovf4ddfef2018-01-16 07:37:58 -08001451}
Mathias Agopian65ab4712010-07-14 17:59:35 -07001452
Eric Laurent4eb58f12018-12-07 16:41:02 -08001453bool AudioPolicyService::UidPolicy::isA11yOnTop() {
1454 for (const auto &uid : mCachedUids) {
Eric Laurent47670c92019-08-28 16:59:05 -07001455 if (!isA11yUid(uid.first)) {
Eric Laurent4eb58f12018-12-07 16:41:02 -08001456 continue;
1457 }
Amith Yamasanibcbb3002019-01-23 13:53:33 -08001458 if (uid.second.second >= ActivityManager::PROCESS_STATE_TOP
1459 && uid.second.second <= ActivityManager::PROCESS_STATE_BOUND_FOREGROUND_SERVICE) {
Eric Laurent4eb58f12018-12-07 16:41:02 -08001460 return true;
1461 }
1462 }
1463 return false;
1464}
1465
Eric Laurentb78763e2018-10-17 10:08:02 -07001466bool AudioPolicyService::UidPolicy::isA11yUid(uid_t uid)
1467{
1468 std::vector<uid_t>::iterator it = find(mA11yUids.begin(), mA11yUids.end(), uid);
1469 return it != mA11yUids.end();
1470}
1471
Michael Groovercfd28302018-12-11 19:16:46 -08001472// ----------- AudioPolicyService::SensorPrivacyService implementation ----------
1473void AudioPolicyService::SensorPrivacyPolicy::registerSelf() {
1474 SensorPrivacyManager spm;
1475 mSensorPrivacyEnabled = spm.isSensorPrivacyEnabled();
1476 spm.addSensorPrivacyListener(this);
1477}
1478
Evan Severson241d9592021-01-08 12:16:02 -08001479void AudioPolicyService::SensorPrivacyPolicy::registerSelfForMicrophoneOnly(int userId) {
1480 SensorPrivacyManager spm;
1481 mSensorPrivacyEnabled = spm.isIndividualSensorPrivacyEnabled(userId,
1482 SensorPrivacyManager::INDIVIDUAL_SENSOR_MICROPHONE);
1483 spm.addIndividualSensorPrivacyListener(userId,
1484 SensorPrivacyManager::INDIVIDUAL_SENSOR_MICROPHONE, this);
1485}
1486
Michael Groovercfd28302018-12-11 19:16:46 -08001487void AudioPolicyService::SensorPrivacyPolicy::unregisterSelf() {
1488 SensorPrivacyManager spm;
1489 spm.removeSensorPrivacyListener(this);
1490}
1491
1492bool AudioPolicyService::SensorPrivacyPolicy::isSensorPrivacyEnabled() {
1493 return mSensorPrivacyEnabled;
1494}
1495
1496binder::Status AudioPolicyService::SensorPrivacyPolicy::onSensorPrivacyChanged(bool enabled) {
1497 mSensorPrivacyEnabled = enabled;
1498 sp<AudioPolicyService> service = mService.promote();
1499 if (service != nullptr) {
1500 service->updateUidStates();
1501 }
1502 return binder::Status::ok();
1503}
1504
Eric Laurented726cc2021-07-01 14:26:41 +02001505// ----------- AudioPolicyService::OpRecordAudioMonitor implementation ----------
1506
1507// static
1508sp<AudioPolicyService::OpRecordAudioMonitor>
1509AudioPolicyService::OpRecordAudioMonitor::createIfNeeded(
1510 const AttributionSourceState& attributionSource, const audio_attributes_t& attr,
1511 wp<AudioCommandThread> commandThread)
1512{
Eric Laurent987ce102021-07-05 12:11:51 +02001513 if (isAudioServerOrRootUid(attributionSource.uid)) {
1514 ALOGV("not silencing record for audio or root source %s",
Eric Laurented726cc2021-07-01 14:26:41 +02001515 attributionSource.toString().c_str());
1516 return nullptr;
1517 }
1518
1519 if (!AudioPolicyService::isAppOpSource(attr.source)) {
1520 ALOGD("not monitoring app op for uid %d and source %d",
1521 attributionSource.uid, attr.source);
1522 return nullptr;
1523 }
1524
1525 if (!attributionSource.packageName.has_value()
1526 || attributionSource.packageName.value().size() == 0) {
1527 return nullptr;
1528 }
1529 return new OpRecordAudioMonitor(attributionSource, getOpForSource(attr.source), commandThread);
1530}
1531
1532AudioPolicyService::OpRecordAudioMonitor::OpRecordAudioMonitor(
1533 const AttributionSourceState& attributionSource, int32_t appOp,
1534 wp<AudioCommandThread> commandThread) :
1535 mHasOp(true), mAttributionSource(attributionSource), mAppOp(appOp),
1536 mCommandThread(commandThread)
1537{
1538}
1539
1540AudioPolicyService::OpRecordAudioMonitor::~OpRecordAudioMonitor()
1541{
1542 if (mOpCallback != 0) {
1543 mAppOpsManager.stopWatchingMode(mOpCallback);
1544 }
1545 mOpCallback.clear();
1546}
1547
1548void AudioPolicyService::OpRecordAudioMonitor::onFirstRef()
1549{
1550 checkOp();
1551 mOpCallback = new RecordAudioOpCallback(this);
1552 ALOGV("start watching op %d for %s", mAppOp, mAttributionSource.toString().c_str());
1553 // TODO: We need to always watch AppOpsManager::OP_RECORD_AUDIO too
1554 // since it controls the mic permission for legacy apps.
1555 mAppOpsManager.startWatchingMode(mAppOp, VALUE_OR_FATAL(aidl2legacy_string_view_String16(
1556 mAttributionSource.packageName.value_or(""))),
1557 mOpCallback);
1558}
1559
1560bool AudioPolicyService::OpRecordAudioMonitor::hasOp() const {
1561 return mHasOp.load();
1562}
1563
1564// Called by RecordAudioOpCallback when the app op corresponding to this OpRecordAudioMonitor
1565// is updated in AppOp callback and in onFirstRef()
1566// Note this method is never called (and never to be) for audio server / root track
1567// due to the UID in createIfNeeded(). As a result for those record track, it's:
1568// - not called from constructor,
1569// - not called from RecordAudioOpCallback because the callback is not installed in this case
1570void AudioPolicyService::OpRecordAudioMonitor::checkOp(bool updateUidStates)
1571{
1572 // TODO: We need to always check AppOpsManager::OP_RECORD_AUDIO too
1573 // since it controls the mic permission for legacy apps.
1574 const int32_t mode = mAppOpsManager.checkOp(mAppOp,
1575 mAttributionSource.uid, VALUE_OR_FATAL(aidl2legacy_string_view_String16(
1576 mAttributionSource.packageName.value_or(""))));
1577 const bool hasIt = (mode == AppOpsManager::MODE_ALLOWED);
1578 // verbose logging only log when appOp changed
1579 ALOGI_IF(hasIt != mHasOp.load(),
1580 "App op %d missing, %ssilencing record %s",
1581 mAppOp, hasIt ? "un" : "", mAttributionSource.toString().c_str());
1582 mHasOp.store(hasIt);
1583
1584 if (updateUidStates) {
1585 sp<AudioCommandThread> commandThread = mCommandThread.promote();
1586 if (commandThread != nullptr) {
1587 commandThread->updateUidStatesCommand();
1588 }
1589 }
1590}
1591
1592AudioPolicyService::OpRecordAudioMonitor::RecordAudioOpCallback::RecordAudioOpCallback(
1593 const wp<OpRecordAudioMonitor>& monitor) : mMonitor(monitor)
1594{ }
1595
1596void AudioPolicyService::OpRecordAudioMonitor::RecordAudioOpCallback::opChanged(int32_t op,
1597 const String16& packageName __unused) {
1598 sp<OpRecordAudioMonitor> monitor = mMonitor.promote();
1599 if (monitor != NULL) {
1600 if (op != monitor->getOp()) {
1601 return;
1602 }
1603 monitor->checkOp(true);
1604 }
1605}
1606
1607
Mathias Agopian65ab4712010-07-14 17:59:35 -07001608// ----------- AudioPolicyService::AudioCommandThread implementation ----------
1609
Eric Laurentbfb1b832013-01-07 09:53:42 -08001610AudioPolicyService::AudioCommandThread::AudioCommandThread(String8 name,
1611 const wp<AudioPolicyService>& service)
1612 : Thread(false), mName(name), mService(service)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001613{
Mathias Agopian65ab4712010-07-14 17:59:35 -07001614}
1615
1616
1617AudioPolicyService::AudioCommandThread::~AudioCommandThread()
1618{
Eric Laurentbfb1b832013-01-07 09:53:42 -08001619 if (!mAudioCommands.isEmpty()) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07001620 release_wake_lock(mName.string());
1621 }
1622 mAudioCommands.clear();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001623}
1624
1625void AudioPolicyService::AudioCommandThread::onFirstRef()
1626{
Eric Laurentbfb1b832013-01-07 09:53:42 -08001627 run(mName.string(), ANDROID_PRIORITY_AUDIO);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001628}
1629
1630bool AudioPolicyService::AudioCommandThread::threadLoop()
1631{
Eric Laurentd7eda8d2016-02-02 17:18:39 -08001632 nsecs_t waitTime = -1;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001633
1634 mLock.lock();
1635 while (!exitPending())
1636 {
Eric Laurent59a89232014-06-08 14:14:17 -07001637 sp<AudioPolicyService> svc;
1638 while (!mAudioCommands.isEmpty() && !exitPending()) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07001639 nsecs_t curTime = systemTime();
1640 // commands are sorted by increasing time stamp: execute them from index 0 and up
1641 if (mAudioCommands[0]->mTime <= curTime) {
Eric Laurent0ede8922014-05-09 18:04:42 -07001642 sp<AudioCommand> command = mAudioCommands[0];
Mathias Agopian65ab4712010-07-14 17:59:35 -07001643 mAudioCommands.removeAt(0);
Eric Laurent0ede8922014-05-09 18:04:42 -07001644 mLastCommand = command;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001645
1646 switch (command->mCommand) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07001647 case SET_VOLUME: {
Eric Laurent0ede8922014-05-09 18:04:42 -07001648 VolumeData *data = (VolumeData *)command->mParam.get();
Steve Block3856b092011-10-20 11:56:00 +01001649 ALOGV("AudioCommandThread() processing set volume stream %d, \
Eric Laurentde070132010-07-13 04:45:46 -07001650 volume %f, output %d", data->mStream, data->mVolume, data->mIO);
Andy Hungfe726a62018-09-27 15:17:25 -07001651 mLock.unlock();
Eric Laurentde070132010-07-13 04:45:46 -07001652 command->mStatus = AudioSystem::setStreamVolume(data->mStream,
1653 data->mVolume,
1654 data->mIO);
Andy Hungfe726a62018-09-27 15:17:25 -07001655 mLock.lock();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001656 }break;
1657 case SET_PARAMETERS: {
Eric Laurent0ede8922014-05-09 18:04:42 -07001658 ParametersData *data = (ParametersData *)command->mParam.get();
Glenn Kastene53b9ea2012-03-12 16:29:55 -07001659 ALOGV("AudioCommandThread() processing set parameters string %s, io %d",
1660 data->mKeyValuePairs.string(), data->mIO);
Andy Hungfe726a62018-09-27 15:17:25 -07001661 mLock.unlock();
Glenn Kastene53b9ea2012-03-12 16:29:55 -07001662 command->mStatus = AudioSystem::setParameters(data->mIO, data->mKeyValuePairs);
Andy Hungfe726a62018-09-27 15:17:25 -07001663 mLock.lock();
Glenn Kastene53b9ea2012-03-12 16:29:55 -07001664 }break;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001665 case SET_VOICE_VOLUME: {
Eric Laurent0ede8922014-05-09 18:04:42 -07001666 VoiceVolumeData *data = (VoiceVolumeData *)command->mParam.get();
Steve Block3856b092011-10-20 11:56:00 +01001667 ALOGV("AudioCommandThread() processing set voice volume volume %f",
Eric Laurentde070132010-07-13 04:45:46 -07001668 data->mVolume);
Andy Hungfe726a62018-09-27 15:17:25 -07001669 mLock.unlock();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001670 command->mStatus = AudioSystem::setVoiceVolume(data->mVolume);
Andy Hungfe726a62018-09-27 15:17:25 -07001671 mLock.lock();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001672 }break;
Eric Laurentbfb1b832013-01-07 09:53:42 -08001673 case STOP_OUTPUT: {
Eric Laurent0ede8922014-05-09 18:04:42 -07001674 StopOutputData *data = (StopOutputData *)command->mParam.get();
Eric Laurentd7fe0862018-07-14 16:48:01 -07001675 ALOGV("AudioCommandThread() processing stop output portId %d",
1676 data->mPortId);
Eric Laurent59a89232014-06-08 14:14:17 -07001677 svc = mService.promote();
Eric Laurentbfb1b832013-01-07 09:53:42 -08001678 if (svc == 0) {
1679 break;
1680 }
1681 mLock.unlock();
Eric Laurentd7fe0862018-07-14 16:48:01 -07001682 svc->doStopOutput(data->mPortId);
Eric Laurentbfb1b832013-01-07 09:53:42 -08001683 mLock.lock();
Eric Laurentbfb1b832013-01-07 09:53:42 -08001684 }break;
1685 case RELEASE_OUTPUT: {
Eric Laurent0ede8922014-05-09 18:04:42 -07001686 ReleaseOutputData *data = (ReleaseOutputData *)command->mParam.get();
Eric Laurentd7fe0862018-07-14 16:48:01 -07001687 ALOGV("AudioCommandThread() processing release output portId %d",
1688 data->mPortId);
Eric Laurent59a89232014-06-08 14:14:17 -07001689 svc = mService.promote();
Eric Laurentbfb1b832013-01-07 09:53:42 -08001690 if (svc == 0) {
1691 break;
1692 }
1693 mLock.unlock();
Eric Laurentd7fe0862018-07-14 16:48:01 -07001694 svc->doReleaseOutput(data->mPortId);
Eric Laurentbfb1b832013-01-07 09:53:42 -08001695 mLock.lock();
Eric Laurentbfb1b832013-01-07 09:53:42 -08001696 }break;
Eric Laurent951f4552014-05-20 10:48:17 -07001697 case CREATE_AUDIO_PATCH: {
1698 CreateAudioPatchData *data = (CreateAudioPatchData *)command->mParam.get();
1699 ALOGV("AudioCommandThread() processing create audio patch");
1700 sp<IAudioFlinger> af = AudioSystem::get_audio_flinger();
1701 if (af == 0) {
1702 command->mStatus = PERMISSION_DENIED;
1703 } else {
Andy Hungfe726a62018-09-27 15:17:25 -07001704 mLock.unlock();
Eric Laurent951f4552014-05-20 10:48:17 -07001705 command->mStatus = af->createAudioPatch(&data->mPatch, &data->mHandle);
Andy Hungfe726a62018-09-27 15:17:25 -07001706 mLock.lock();
Eric Laurent951f4552014-05-20 10:48:17 -07001707 }
1708 } break;
1709 case RELEASE_AUDIO_PATCH: {
1710 ReleaseAudioPatchData *data = (ReleaseAudioPatchData *)command->mParam.get();
1711 ALOGV("AudioCommandThread() processing release audio patch");
1712 sp<IAudioFlinger> af = AudioSystem::get_audio_flinger();
1713 if (af == 0) {
1714 command->mStatus = PERMISSION_DENIED;
1715 } else {
Andy Hungfe726a62018-09-27 15:17:25 -07001716 mLock.unlock();
Eric Laurent951f4552014-05-20 10:48:17 -07001717 command->mStatus = af->releaseAudioPatch(data->mHandle);
Andy Hungfe726a62018-09-27 15:17:25 -07001718 mLock.lock();
Eric Laurent951f4552014-05-20 10:48:17 -07001719 }
1720 } break;
Eric Laurentb52c1522014-05-20 11:27:36 -07001721 case UPDATE_AUDIOPORT_LIST: {
1722 ALOGV("AudioCommandThread() processing update audio port list");
Eric Laurent59a89232014-06-08 14:14:17 -07001723 svc = mService.promote();
Eric Laurentb52c1522014-05-20 11:27:36 -07001724 if (svc == 0) {
1725 break;
1726 }
1727 mLock.unlock();
1728 svc->doOnAudioPortListUpdate();
1729 mLock.lock();
1730 }break;
1731 case UPDATE_AUDIOPATCH_LIST: {
1732 ALOGV("AudioCommandThread() processing update audio patch list");
Eric Laurent59a89232014-06-08 14:14:17 -07001733 svc = mService.promote();
Eric Laurentb52c1522014-05-20 11:27:36 -07001734 if (svc == 0) {
1735 break;
1736 }
1737 mLock.unlock();
1738 svc->doOnAudioPatchListUpdate();
1739 mLock.lock();
1740 }break;
François Gaffiecfe17322018-11-07 13:41:29 +01001741 case CHANGED_AUDIOVOLUMEGROUP: {
1742 AudioVolumeGroupData *data =
1743 static_cast<AudioVolumeGroupData *>(command->mParam.get());
1744 ALOGV("AudioCommandThread() processing update audio volume group");
1745 svc = mService.promote();
1746 if (svc == 0) {
1747 break;
1748 }
1749 mLock.unlock();
1750 svc->doOnAudioVolumeGroupChanged(data->mGroup, data->mFlags);
1751 mLock.lock();
1752 }break;
Eric Laurente1715a42014-05-20 11:30:42 -07001753 case SET_AUDIOPORT_CONFIG: {
1754 SetAudioPortConfigData *data = (SetAudioPortConfigData *)command->mParam.get();
1755 ALOGV("AudioCommandThread() processing set port config");
1756 sp<IAudioFlinger> af = AudioSystem::get_audio_flinger();
1757 if (af == 0) {
1758 command->mStatus = PERMISSION_DENIED;
1759 } else {
Andy Hungfe726a62018-09-27 15:17:25 -07001760 mLock.unlock();
Eric Laurente1715a42014-05-20 11:30:42 -07001761 command->mStatus = af->setAudioPortConfig(&data->mConfig);
Andy Hungfe726a62018-09-27 15:17:25 -07001762 mLock.lock();
Eric Laurente1715a42014-05-20 11:30:42 -07001763 }
1764 } break;
Jean-Michel Trivide801052015-04-14 19:10:14 -07001765 case DYN_POLICY_MIX_STATE_UPDATE: {
1766 DynPolicyMixStateUpdateData *data =
1767 (DynPolicyMixStateUpdateData *)command->mParam.get();
Jean-Michel Trivide801052015-04-14 19:10:14 -07001768 ALOGV("AudioCommandThread() processing dyn policy mix state update %s %d",
1769 data->mRegId.string(), data->mState);
1770 svc = mService.promote();
1771 if (svc == 0) {
1772 break;
1773 }
1774 mLock.unlock();
1775 svc->doOnDynamicPolicyMixStateUpdate(data->mRegId, data->mState);
1776 mLock.lock();
1777 } break;
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -08001778 case RECORDING_CONFIGURATION_UPDATE: {
1779 RecordingConfigurationUpdateData *data =
1780 (RecordingConfigurationUpdateData *)command->mParam.get();
1781 ALOGV("AudioCommandThread() processing recording configuration update");
1782 svc = mService.promote();
1783 if (svc == 0) {
1784 break;
1785 }
1786 mLock.unlock();
Jean-Michel Triviac4e4292016-12-22 11:39:31 -08001787 svc->doOnRecordingConfigurationUpdate(data->mEvent, &data->mClientInfo,
Eric Laurenta9f86652018-11-28 17:23:11 -08001788 &data->mClientConfig, data->mClientEffects,
1789 &data->mDeviceConfig, data->mEffects,
1790 data->mPatchHandle, data->mSource);
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -08001791 mLock.lock();
1792 } break;
Eric Laurentb20cf7d2019-04-05 19:37:34 -07001793 case SET_EFFECT_SUSPENDED: {
1794 SetEffectSuspendedData *data = (SetEffectSuspendedData *)command->mParam.get();
1795 ALOGV("AudioCommandThread() processing set effect suspended");
1796 sp<IAudioFlinger> af = AudioSystem::get_audio_flinger();
1797 if (af != 0) {
1798 mLock.unlock();
1799 af->setEffectSuspended(data->mEffectId, data->mSessionId, data->mSuspended);
1800 mLock.lock();
1801 }
1802 } break;
Mikhail Naganov88b30d22020-03-09 19:43:13 +00001803 case AUDIO_MODULES_UPDATE: {
1804 ALOGV("AudioCommandThread() processing audio modules update");
1805 svc = mService.promote();
1806 if (svc == 0) {
1807 break;
1808 }
1809 mLock.unlock();
1810 svc->doOnNewAudioModulesAvailable();
1811 mLock.lock();
1812 } break;
Jean-Michel Trivi9a6b9ad2020-10-22 16:46:43 -07001813 case ROUTING_UPDATED: {
1814 ALOGV("AudioCommandThread() processing routing update");
1815 svc = mService.promote();
1816 if (svc == 0) {
1817 break;
1818 }
1819 mLock.unlock();
1820 svc->doOnRoutingUpdated();
1821 mLock.lock();
1822 } break;
Eric Laurentb20cf7d2019-04-05 19:37:34 -07001823
Eric Laurented726cc2021-07-01 14:26:41 +02001824 case UPDATE_UID_STATES: {
1825 ALOGV("AudioCommandThread() processing updateUID states");
1826 svc = mService.promote();
1827 if (svc == 0) {
1828 break;
1829 }
1830 mLock.unlock();
1831 svc->updateUidStates();
1832 mLock.lock();
1833 } break;
1834
Eric Laurent6d607012021-07-05 11:54:40 +02001835 case CHECK_SPATIALIZER: {
1836 ALOGV("AudioCommandThread() processing updateUID states");
1837 svc = mService.promote();
1838 if (svc == 0) {
1839 break;
1840 }
1841 mLock.unlock();
1842 svc->doOnCheckSpatializer();
1843 mLock.lock();
1844 } break;
1845
Mathias Agopian65ab4712010-07-14 17:59:35 -07001846 default:
Steve Block5ff1dd52012-01-05 23:22:43 +00001847 ALOGW("AudioCommandThread() unknown command %d", command->mCommand);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001848 }
Eric Laurent0ede8922014-05-09 18:04:42 -07001849 {
1850 Mutex::Autolock _l(command->mLock);
1851 if (command->mWaitStatus) {
1852 command->mWaitStatus = false;
1853 command->mCond.signal();
1854 }
1855 }
Eric Laurentd7eda8d2016-02-02 17:18:39 -08001856 waitTime = -1;
Zach Janga754b4f2015-10-27 01:29:34 +00001857 // release mLock before releasing strong reference on the service as
1858 // AudioPolicyService destructor calls AudioCommandThread::exit() which
1859 // acquires mLock.
1860 mLock.unlock();
1861 svc.clear();
1862 mLock.lock();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001863 } else {
1864 waitTime = mAudioCommands[0]->mTime - curTime;
1865 break;
1866 }
1867 }
Zach Janga754b4f2015-10-27 01:29:34 +00001868
1869 // release delayed commands wake lock if the queue is empty
1870 if (mAudioCommands.isEmpty()) {
Ricardo Garcia05f2fdc2014-07-24 15:48:24 -07001871 release_wake_lock(mName.string());
Zach Janga754b4f2015-10-27 01:29:34 +00001872 }
1873
1874 // At this stage we have either an empty command queue or the first command in the queue
1875 // has a finite delay. So unless we are exiting it is safe to wait.
1876 if (!exitPending()) {
Eric Laurent59a89232014-06-08 14:14:17 -07001877 ALOGV("AudioCommandThread() going to sleep");
Eric Laurentd7eda8d2016-02-02 17:18:39 -08001878 if (waitTime == -1) {
1879 mWaitWorkCV.wait(mLock);
1880 } else {
1881 mWaitWorkCV.waitRelative(mLock, waitTime);
1882 }
Eric Laurent59a89232014-06-08 14:14:17 -07001883 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001884 }
Ricardo Garcia05f2fdc2014-07-24 15:48:24 -07001885 // release delayed commands wake lock before quitting
1886 if (!mAudioCommands.isEmpty()) {
1887 release_wake_lock(mName.string());
1888 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001889 mLock.unlock();
1890 return false;
1891}
1892
1893status_t AudioPolicyService::AudioCommandThread::dump(int fd)
1894{
1895 const size_t SIZE = 256;
1896 char buffer[SIZE];
1897 String8 result;
1898
1899 snprintf(buffer, SIZE, "AudioCommandThread %p Dump\n", this);
1900 result.append(buffer);
1901 write(fd, result.string(), result.size());
1902
Mikhail Naganov12b716c2020-04-30 22:37:43 +00001903 const bool locked = dumpTryLock(mLock);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001904 if (!locked) {
1905 String8 result2(kCmdDeadlockedString);
1906 write(fd, result2.string(), result2.size());
1907 }
1908
1909 snprintf(buffer, SIZE, "- Commands:\n");
1910 result = String8(buffer);
1911 result.append(" Command Time Wait pParam\n");
Glenn Kasten8d6a2442012-02-08 14:04:28 -08001912 for (size_t i = 0; i < mAudioCommands.size(); i++) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07001913 mAudioCommands[i]->dump(buffer, SIZE);
1914 result.append(buffer);
1915 }
1916 result.append(" Last Command\n");
Eric Laurent0ede8922014-05-09 18:04:42 -07001917 if (mLastCommand != 0) {
1918 mLastCommand->dump(buffer, SIZE);
1919 result.append(buffer);
1920 } else {
1921 result.append(" none\n");
1922 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001923
1924 write(fd, result.string(), result.size());
1925
Mikhail Naganov12b716c2020-04-30 22:37:43 +00001926 dumpReleaseLock(mLock, locked);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001927
1928 return NO_ERROR;
1929}
1930
Glenn Kastenfff6d712012-01-12 16:38:12 -08001931status_t AudioPolicyService::AudioCommandThread::volumeCommand(audio_stream_type_t stream,
Eric Laurentde070132010-07-13 04:45:46 -07001932 float volume,
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001933 audio_io_handle_t output,
Eric Laurentde070132010-07-13 04:45:46 -07001934 int delayMs)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001935{
Eric Laurent0ede8922014-05-09 18:04:42 -07001936 sp<AudioCommand> command = new AudioCommand();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001937 command->mCommand = SET_VOLUME;
Eric Laurent0ede8922014-05-09 18:04:42 -07001938 sp<VolumeData> data = new VolumeData();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001939 data->mStream = stream;
1940 data->mVolume = volume;
1941 data->mIO = output;
1942 command->mParam = data;
Eric Laurent0ede8922014-05-09 18:04:42 -07001943 command->mWaitStatus = true;
Steve Block3856b092011-10-20 11:56:00 +01001944 ALOGV("AudioCommandThread() adding set volume stream %d, volume %f, output %d",
Eric Laurentde070132010-07-13 04:45:46 -07001945 stream, volume, output);
Eric Laurent0ede8922014-05-09 18:04:42 -07001946 return sendCommand(command, delayMs);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001947}
1948
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001949status_t AudioPolicyService::AudioCommandThread::parametersCommand(audio_io_handle_t ioHandle,
Dima Zavinfce7a472011-04-19 22:30:36 -07001950 const char *keyValuePairs,
Eric Laurentde070132010-07-13 04:45:46 -07001951 int delayMs)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001952{
Eric Laurent0ede8922014-05-09 18:04:42 -07001953 sp<AudioCommand> command = new AudioCommand();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001954 command->mCommand = SET_PARAMETERS;
Eric Laurent0ede8922014-05-09 18:04:42 -07001955 sp<ParametersData> data = new ParametersData();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001956 data->mIO = ioHandle;
Dima Zavinfce7a472011-04-19 22:30:36 -07001957 data->mKeyValuePairs = String8(keyValuePairs);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001958 command->mParam = data;
Eric Laurent0ede8922014-05-09 18:04:42 -07001959 command->mWaitStatus = true;
Steve Block3856b092011-10-20 11:56:00 +01001960 ALOGV("AudioCommandThread() adding set parameter string %s, io %d ,delay %d",
Dima Zavinfce7a472011-04-19 22:30:36 -07001961 keyValuePairs, ioHandle, delayMs);
Eric Laurent0ede8922014-05-09 18:04:42 -07001962 return sendCommand(command, delayMs);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001963}
1964
1965status_t AudioPolicyService::AudioCommandThread::voiceVolumeCommand(float volume, int delayMs)
1966{
Eric Laurent0ede8922014-05-09 18:04:42 -07001967 sp<AudioCommand> command = new AudioCommand();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001968 command->mCommand = SET_VOICE_VOLUME;
Eric Laurent0ede8922014-05-09 18:04:42 -07001969 sp<VoiceVolumeData> data = new VoiceVolumeData();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001970 data->mVolume = volume;
1971 command->mParam = data;
Eric Laurent0ede8922014-05-09 18:04:42 -07001972 command->mWaitStatus = true;
Steve Block3856b092011-10-20 11:56:00 +01001973 ALOGV("AudioCommandThread() adding set voice volume volume %f", volume);
Eric Laurent0ede8922014-05-09 18:04:42 -07001974 return sendCommand(command, delayMs);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001975}
1976
Eric Laurentb20cf7d2019-04-05 19:37:34 -07001977void AudioPolicyService::AudioCommandThread::setEffectSuspendedCommand(int effectId,
1978 audio_session_t sessionId,
1979 bool suspended)
1980{
1981 sp<AudioCommand> command = new AudioCommand();
1982 command->mCommand = SET_EFFECT_SUSPENDED;
1983 sp<SetEffectSuspendedData> data = new SetEffectSuspendedData();
1984 data->mEffectId = effectId;
1985 data->mSessionId = sessionId;
1986 data->mSuspended = suspended;
1987 command->mParam = data;
1988 ALOGV("AudioCommandThread() adding set suspended effectId %d sessionId %d suspended %d",
1989 effectId, sessionId, suspended);
1990 sendCommand(command);
1991}
1992
1993
Eric Laurentd7fe0862018-07-14 16:48:01 -07001994void AudioPolicyService::AudioCommandThread::stopOutputCommand(audio_port_handle_t portId)
Eric Laurentbfb1b832013-01-07 09:53:42 -08001995{
Eric Laurent0ede8922014-05-09 18:04:42 -07001996 sp<AudioCommand> command = new AudioCommand();
Eric Laurentbfb1b832013-01-07 09:53:42 -08001997 command->mCommand = STOP_OUTPUT;
Eric Laurent0ede8922014-05-09 18:04:42 -07001998 sp<StopOutputData> data = new StopOutputData();
Eric Laurentd7fe0862018-07-14 16:48:01 -07001999 data->mPortId = portId;
Jesper Tragardh48412dc2014-03-24 14:12:43 +01002000 command->mParam = data;
Eric Laurentd7fe0862018-07-14 16:48:01 -07002001 ALOGV("AudioCommandThread() adding stop output portId %d", portId);
Eric Laurent0ede8922014-05-09 18:04:42 -07002002 sendCommand(command);
Eric Laurentbfb1b832013-01-07 09:53:42 -08002003}
2004
Eric Laurentd7fe0862018-07-14 16:48:01 -07002005void AudioPolicyService::AudioCommandThread::releaseOutputCommand(audio_port_handle_t portId)
Eric Laurentbfb1b832013-01-07 09:53:42 -08002006{
Eric Laurent0ede8922014-05-09 18:04:42 -07002007 sp<AudioCommand> command = new AudioCommand();
Eric Laurentbfb1b832013-01-07 09:53:42 -08002008 command->mCommand = RELEASE_OUTPUT;
Eric Laurent0ede8922014-05-09 18:04:42 -07002009 sp<ReleaseOutputData> data = new ReleaseOutputData();
Eric Laurentd7fe0862018-07-14 16:48:01 -07002010 data->mPortId = portId;
Jesper Tragardh48412dc2014-03-24 14:12:43 +01002011 command->mParam = data;
Eric Laurentd7fe0862018-07-14 16:48:01 -07002012 ALOGV("AudioCommandThread() adding release output portId %d", portId);
Eric Laurent0ede8922014-05-09 18:04:42 -07002013 sendCommand(command);
2014}
2015
Eric Laurent951f4552014-05-20 10:48:17 -07002016status_t AudioPolicyService::AudioCommandThread::createAudioPatchCommand(
2017 const struct audio_patch *patch,
2018 audio_patch_handle_t *handle,
2019 int delayMs)
2020{
2021 status_t status = NO_ERROR;
2022
2023 sp<AudioCommand> command = new AudioCommand();
2024 command->mCommand = CREATE_AUDIO_PATCH;
2025 CreateAudioPatchData *data = new CreateAudioPatchData();
2026 data->mPatch = *patch;
2027 data->mHandle = *handle;
2028 command->mParam = data;
2029 command->mWaitStatus = true;
2030 ALOGV("AudioCommandThread() adding create patch delay %d", delayMs);
2031 status = sendCommand(command, delayMs);
2032 if (status == NO_ERROR) {
2033 *handle = data->mHandle;
2034 }
2035 return status;
2036}
2037
2038status_t AudioPolicyService::AudioCommandThread::releaseAudioPatchCommand(audio_patch_handle_t handle,
2039 int delayMs)
2040{
2041 sp<AudioCommand> command = new AudioCommand();
2042 command->mCommand = RELEASE_AUDIO_PATCH;
2043 ReleaseAudioPatchData *data = new ReleaseAudioPatchData();
2044 data->mHandle = handle;
2045 command->mParam = data;
2046 command->mWaitStatus = true;
2047 ALOGV("AudioCommandThread() adding release patch delay %d", delayMs);
2048 return sendCommand(command, delayMs);
2049}
2050
Eric Laurentb52c1522014-05-20 11:27:36 -07002051void AudioPolicyService::AudioCommandThread::updateAudioPortListCommand()
2052{
2053 sp<AudioCommand> command = new AudioCommand();
2054 command->mCommand = UPDATE_AUDIOPORT_LIST;
2055 ALOGV("AudioCommandThread() adding update audio port list");
2056 sendCommand(command);
2057}
2058
Eric Laurented726cc2021-07-01 14:26:41 +02002059void AudioPolicyService::AudioCommandThread::updateUidStatesCommand()
2060{
2061 sp<AudioCommand> command = new AudioCommand();
2062 command->mCommand = UPDATE_UID_STATES;
2063 ALOGV("AudioCommandThread() adding update UID states");
2064 sendCommand(command);
2065}
2066
Eric Laurentb52c1522014-05-20 11:27:36 -07002067void AudioPolicyService::AudioCommandThread::updateAudioPatchListCommand()
2068{
2069 sp<AudioCommand>command = new AudioCommand();
2070 command->mCommand = UPDATE_AUDIOPATCH_LIST;
2071 ALOGV("AudioCommandThread() adding update audio patch list");
2072 sendCommand(command);
2073}
2074
François Gaffiecfe17322018-11-07 13:41:29 +01002075void AudioPolicyService::AudioCommandThread::changeAudioVolumeGroupCommand(volume_group_t group,
2076 int flags)
2077{
2078 sp<AudioCommand>command = new AudioCommand();
2079 command->mCommand = CHANGED_AUDIOVOLUMEGROUP;
2080 AudioVolumeGroupData *data= new AudioVolumeGroupData();
2081 data->mGroup = group;
2082 data->mFlags = flags;
2083 command->mParam = data;
2084 ALOGV("AudioCommandThread() adding audio volume group changed");
2085 sendCommand(command);
2086}
2087
Eric Laurente1715a42014-05-20 11:30:42 -07002088status_t AudioPolicyService::AudioCommandThread::setAudioPortConfigCommand(
2089 const struct audio_port_config *config, int delayMs)
2090{
2091 sp<AudioCommand> command = new AudioCommand();
2092 command->mCommand = SET_AUDIOPORT_CONFIG;
2093 SetAudioPortConfigData *data = new SetAudioPortConfigData();
2094 data->mConfig = *config;
2095 command->mParam = data;
2096 command->mWaitStatus = true;
2097 ALOGV("AudioCommandThread() adding set port config delay %d", delayMs);
2098 return sendCommand(command, delayMs);
2099}
2100
Jean-Michel Trivide801052015-04-14 19:10:14 -07002101void AudioPolicyService::AudioCommandThread::dynamicPolicyMixStateUpdateCommand(
Chih-Hung Hsiehe964d4e2016-08-09 14:31:32 -07002102 const String8& regId, int32_t state)
Jean-Michel Trivide801052015-04-14 19:10:14 -07002103{
2104 sp<AudioCommand> command = new AudioCommand();
2105 command->mCommand = DYN_POLICY_MIX_STATE_UPDATE;
2106 DynPolicyMixStateUpdateData *data = new DynPolicyMixStateUpdateData();
2107 data->mRegId = regId;
2108 data->mState = state;
2109 command->mParam = data;
2110 ALOGV("AudioCommandThread() sending dynamic policy mix (id=%s) state update to %d",
2111 regId.string(), state);
2112 sendCommand(command);
2113}
2114
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -08002115void AudioPolicyService::AudioCommandThread::recordingConfigurationUpdateCommand(
Eric Laurenta9f86652018-11-28 17:23:11 -08002116 int event,
2117 const record_client_info_t *clientInfo,
2118 const audio_config_base_t *clientConfig,
2119 std::vector<effect_descriptor_t> clientEffects,
2120 const audio_config_base_t *deviceConfig,
2121 std::vector<effect_descriptor_t> effects,
2122 audio_patch_handle_t patchHandle,
2123 audio_source_t source)
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -08002124{
2125 sp<AudioCommand>command = new AudioCommand();
2126 command->mCommand = RECORDING_CONFIGURATION_UPDATE;
2127 RecordingConfigurationUpdateData *data = new RecordingConfigurationUpdateData();
2128 data->mEvent = event;
Jean-Michel Triviac4e4292016-12-22 11:39:31 -08002129 data->mClientInfo = *clientInfo;
Jean-Michel Trivi7281aa92016-02-17 15:33:40 -08002130 data->mClientConfig = *clientConfig;
Eric Laurenta9f86652018-11-28 17:23:11 -08002131 data->mClientEffects = clientEffects;
Jean-Michel Trivi7281aa92016-02-17 15:33:40 -08002132 data->mDeviceConfig = *deviceConfig;
Eric Laurenta9f86652018-11-28 17:23:11 -08002133 data->mEffects = effects;
Jean-Michel Trivi8c7cf3b2016-02-25 17:08:24 -08002134 data->mPatchHandle = patchHandle;
Eric Laurenta9f86652018-11-28 17:23:11 -08002135 data->mSource = source;
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -08002136 command->mParam = data;
Jean-Michel Triviac4e4292016-12-22 11:39:31 -08002137 ALOGV("AudioCommandThread() adding recording configuration update event %d, source %d uid %u",
2138 event, clientInfo->source, clientInfo->uid);
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -08002139 sendCommand(command);
2140}
2141
Mikhail Naganov88b30d22020-03-09 19:43:13 +00002142void AudioPolicyService::AudioCommandThread::audioModulesUpdateCommand()
2143{
2144 sp<AudioCommand> command = new AudioCommand();
2145 command->mCommand = AUDIO_MODULES_UPDATE;
2146 sendCommand(command);
2147}
2148
Jean-Michel Trivi9a6b9ad2020-10-22 16:46:43 -07002149void AudioPolicyService::AudioCommandThread::routingChangedCommand()
2150{
2151 sp<AudioCommand>command = new AudioCommand();
2152 command->mCommand = ROUTING_UPDATED;
2153 ALOGV("AudioCommandThread() adding routing update");
2154 sendCommand(command);
2155}
2156
Eric Laurent6d607012021-07-05 11:54:40 +02002157void AudioPolicyService::AudioCommandThread::checkSpatializerCommand()
2158{
2159 sp<AudioCommand>command = new AudioCommand();
2160 command->mCommand = CHECK_SPATIALIZER;
2161 ALOGV("AudioCommandThread() adding check spatializer");
2162 sendCommand(command);
2163}
2164
Eric Laurent0ede8922014-05-09 18:04:42 -07002165status_t AudioPolicyService::AudioCommandThread::sendCommand(sp<AudioCommand>& command, int delayMs)
2166{
2167 {
2168 Mutex::Autolock _l(mLock);
2169 insertCommand_l(command, delayMs);
2170 mWaitWorkCV.signal();
2171 }
2172 Mutex::Autolock _l(command->mLock);
2173 while (command->mWaitStatus) {
2174 nsecs_t timeOutNs = kAudioCommandTimeoutNs + milliseconds(delayMs);
2175 if (command->mCond.waitRelative(command->mLock, timeOutNs) != NO_ERROR) {
2176 command->mStatus = TIMED_OUT;
2177 command->mWaitStatus = false;
2178 }
2179 }
2180 return command->mStatus;
Eric Laurentbfb1b832013-01-07 09:53:42 -08002181}
2182
Mathias Agopian65ab4712010-07-14 17:59:35 -07002183// insertCommand_l() must be called with mLock held
Eric Laurent0ede8922014-05-09 18:04:42 -07002184void AudioPolicyService::AudioCommandThread::insertCommand_l(sp<AudioCommand>& command, int delayMs)
Mathias Agopian65ab4712010-07-14 17:59:35 -07002185{
Glenn Kasten8d6a2442012-02-08 14:04:28 -08002186 ssize_t i; // not size_t because i will count down to -1
Eric Laurent0ede8922014-05-09 18:04:42 -07002187 Vector < sp<AudioCommand> > removedCommands;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002188 command->mTime = systemTime() + milliseconds(delayMs);
2189
2190 // acquire wake lock to make sure delayed commands are processed
Eric Laurentbfb1b832013-01-07 09:53:42 -08002191 if (mAudioCommands.isEmpty()) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07002192 acquire_wake_lock(PARTIAL_WAKE_LOCK, mName.string());
2193 }
2194
2195 // check same pending commands with later time stamps and eliminate them
Ivan Lozano5ff158f2017-10-30 09:06:24 -07002196 for (i = (ssize_t)mAudioCommands.size()-1; i >= 0; i--) {
Eric Laurent0ede8922014-05-09 18:04:42 -07002197 sp<AudioCommand> command2 = mAudioCommands[i];
Mathias Agopian65ab4712010-07-14 17:59:35 -07002198 // commands are sorted by increasing time stamp: no need to scan the rest of mAudioCommands
2199 if (command2->mTime <= command->mTime) break;
Eric Laurente45b48a2014-09-04 16:40:57 -07002200
2201 // create audio patch or release audio patch commands are equivalent
2202 // with regard to filtering
2203 if ((command->mCommand == CREATE_AUDIO_PATCH) ||
2204 (command->mCommand == RELEASE_AUDIO_PATCH)) {
2205 if ((command2->mCommand != CREATE_AUDIO_PATCH) &&
2206 (command2->mCommand != RELEASE_AUDIO_PATCH)) {
2207 continue;
2208 }
2209 } else if (command2->mCommand != command->mCommand) continue;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002210
2211 switch (command->mCommand) {
2212 case SET_PARAMETERS: {
Eric Laurent0ede8922014-05-09 18:04:42 -07002213 ParametersData *data = (ParametersData *)command->mParam.get();
2214 ParametersData *data2 = (ParametersData *)command2->mParam.get();
Mathias Agopian65ab4712010-07-14 17:59:35 -07002215 if (data->mIO != data2->mIO) break;
Steve Block3856b092011-10-20 11:56:00 +01002216 ALOGV("Comparing parameter command %s to new command %s",
Eric Laurentde070132010-07-13 04:45:46 -07002217 data2->mKeyValuePairs.string(), data->mKeyValuePairs.string());
Mathias Agopian65ab4712010-07-14 17:59:35 -07002218 AudioParameter param = AudioParameter(data->mKeyValuePairs);
2219 AudioParameter param2 = AudioParameter(data2->mKeyValuePairs);
2220 for (size_t j = 0; j < param.size(); j++) {
Glenn Kastene53b9ea2012-03-12 16:29:55 -07002221 String8 key;
2222 String8 value;
2223 param.getAt(j, key, value);
2224 for (size_t k = 0; k < param2.size(); k++) {
2225 String8 key2;
2226 String8 value2;
2227 param2.getAt(k, key2, value2);
2228 if (key2 == key) {
2229 param2.remove(key2);
2230 ALOGV("Filtering out parameter %s", key2.string());
2231 break;
2232 }
2233 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07002234 }
2235 // if all keys have been filtered out, remove the command.
2236 // otherwise, update the key value pairs
2237 if (param2.size() == 0) {
2238 removedCommands.add(command2);
2239 } else {
2240 data2->mKeyValuePairs = param2.toString();
2241 }
Eric Laurent21e54562013-09-23 12:08:05 -07002242 command->mTime = command2->mTime;
2243 // force delayMs to non 0 so that code below does not request to wait for
2244 // command status as the command is now delayed
2245 delayMs = 1;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002246 } break;
2247
2248 case SET_VOLUME: {
Eric Laurent0ede8922014-05-09 18:04:42 -07002249 VolumeData *data = (VolumeData *)command->mParam.get();
2250 VolumeData *data2 = (VolumeData *)command2->mParam.get();
Mathias Agopian65ab4712010-07-14 17:59:35 -07002251 if (data->mIO != data2->mIO) break;
2252 if (data->mStream != data2->mStream) break;
Steve Block3856b092011-10-20 11:56:00 +01002253 ALOGV("Filtering out volume command on output %d for stream %d",
Eric Laurentde070132010-07-13 04:45:46 -07002254 data->mIO, data->mStream);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002255 removedCommands.add(command2);
Eric Laurent21e54562013-09-23 12:08:05 -07002256 command->mTime = command2->mTime;
2257 // force delayMs to non 0 so that code below does not request to wait for
2258 // command status as the command is now delayed
2259 delayMs = 1;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002260 } break;
Eric Laurente45b48a2014-09-04 16:40:57 -07002261
Eric Laurentbaf35fe2016-07-27 15:36:53 -07002262 case SET_VOICE_VOLUME: {
2263 VoiceVolumeData *data = (VoiceVolumeData *)command->mParam.get();
2264 VoiceVolumeData *data2 = (VoiceVolumeData *)command2->mParam.get();
2265 ALOGV("Filtering out voice volume command value %f replaced by %f",
2266 data2->mVolume, data->mVolume);
2267 removedCommands.add(command2);
2268 command->mTime = command2->mTime;
2269 // force delayMs to non 0 so that code below does not request to wait for
2270 // command status as the command is now delayed
2271 delayMs = 1;
2272 } break;
2273
Eric Laurente45b48a2014-09-04 16:40:57 -07002274 case CREATE_AUDIO_PATCH:
2275 case RELEASE_AUDIO_PATCH: {
2276 audio_patch_handle_t handle;
Haynes Mathew Georgea2d4a6d2014-10-13 13:05:22 -07002277 struct audio_patch patch;
Eric Laurente45b48a2014-09-04 16:40:57 -07002278 if (command->mCommand == CREATE_AUDIO_PATCH) {
2279 handle = ((CreateAudioPatchData *)command->mParam.get())->mHandle;
Haynes Mathew Georgea2d4a6d2014-10-13 13:05:22 -07002280 patch = ((CreateAudioPatchData *)command->mParam.get())->mPatch;
Eric Laurente45b48a2014-09-04 16:40:57 -07002281 } else {
2282 handle = ((ReleaseAudioPatchData *)command->mParam.get())->mHandle;
Mikhail Naganov7be71d22018-05-23 16:51:46 -07002283 memset(&patch, 0, sizeof(patch));
Eric Laurente45b48a2014-09-04 16:40:57 -07002284 }
2285 audio_patch_handle_t handle2;
Haynes Mathew Georgea2d4a6d2014-10-13 13:05:22 -07002286 struct audio_patch patch2;
Eric Laurente45b48a2014-09-04 16:40:57 -07002287 if (command2->mCommand == CREATE_AUDIO_PATCH) {
2288 handle2 = ((CreateAudioPatchData *)command2->mParam.get())->mHandle;
Haynes Mathew Georgea2d4a6d2014-10-13 13:05:22 -07002289 patch2 = ((CreateAudioPatchData *)command2->mParam.get())->mPatch;
Eric Laurente45b48a2014-09-04 16:40:57 -07002290 } else {
2291 handle2 = ((ReleaseAudioPatchData *)command2->mParam.get())->mHandle;
Glenn Kastenf60b6b62015-07-06 10:53:26 -07002292 memset(&patch2, 0, sizeof(patch2));
Eric Laurente45b48a2014-09-04 16:40:57 -07002293 }
2294 if (handle != handle2) break;
Haynes Mathew Georgea2d4a6d2014-10-13 13:05:22 -07002295 /* Filter CREATE_AUDIO_PATCH commands only when they are issued for
2296 same output. */
2297 if( (command->mCommand == CREATE_AUDIO_PATCH) &&
2298 (command2->mCommand == CREATE_AUDIO_PATCH) ) {
2299 bool isOutputDiff = false;
2300 if (patch.num_sources == patch2.num_sources) {
2301 for (unsigned count = 0; count < patch.num_sources; count++) {
2302 if (patch.sources[count].id != patch2.sources[count].id) {
2303 isOutputDiff = true;
2304 break;
2305 }
2306 }
2307 if (isOutputDiff)
2308 break;
2309 }
2310 }
Eric Laurente45b48a2014-09-04 16:40:57 -07002311 ALOGV("Filtering out %s audio patch command for handle %d",
2312 (command->mCommand == CREATE_AUDIO_PATCH) ? "create" : "release", handle);
2313 removedCommands.add(command2);
2314 command->mTime = command2->mTime;
2315 // force delayMs to non 0 so that code below does not request to wait for
2316 // command status as the command is now delayed
2317 delayMs = 1;
2318 } break;
2319
Jean-Michel Trivide801052015-04-14 19:10:14 -07002320 case DYN_POLICY_MIX_STATE_UPDATE: {
2321
2322 } break;
2323
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -08002324 case RECORDING_CONFIGURATION_UPDATE: {
2325
2326 } break;
2327
Jean-Michel Trivi9a6b9ad2020-10-22 16:46:43 -07002328 case ROUTING_UPDATED: {
2329
2330 } break;
2331
Mathias Agopian65ab4712010-07-14 17:59:35 -07002332 default:
2333 break;
2334 }
2335 }
2336
2337 // remove filtered commands
2338 for (size_t j = 0; j < removedCommands.size(); j++) {
2339 // removed commands always have time stamps greater than current command
2340 for (size_t k = i + 1; k < mAudioCommands.size(); k++) {
Eric Laurent0ede8922014-05-09 18:04:42 -07002341 if (mAudioCommands[k].get() == removedCommands[j].get()) {
Steve Block3856b092011-10-20 11:56:00 +01002342 ALOGV("suppressing command: %d", mAudioCommands[k]->mCommand);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002343 mAudioCommands.removeAt(k);
2344 break;
2345 }
2346 }
2347 }
2348 removedCommands.clear();
2349
Eric Laurentaa79bef2015-01-15 14:33:51 -08002350 // Disable wait for status if delay is not 0.
2351 // Except for create audio patch command because the returned patch handle
2352 // is needed by audio policy manager
2353 if (delayMs != 0 && command->mCommand != CREATE_AUDIO_PATCH) {
Eric Laurentcec4abb2012-07-03 12:23:02 -07002354 command->mWaitStatus = false;
2355 }
Eric Laurentcec4abb2012-07-03 12:23:02 -07002356
Mathias Agopian65ab4712010-07-14 17:59:35 -07002357 // insert command at the right place according to its time stamp
Eric Laurent1e693b52014-07-09 15:03:28 -07002358 ALOGV("inserting command: %d at index %zd, num commands %zu",
2359 command->mCommand, i+1, mAudioCommands.size());
Mathias Agopian65ab4712010-07-14 17:59:35 -07002360 mAudioCommands.insertAt(command, i + 1);
2361}
2362
2363void AudioPolicyService::AudioCommandThread::exit()
2364{
Steve Block3856b092011-10-20 11:56:00 +01002365 ALOGV("AudioCommandThread::exit");
Mathias Agopian65ab4712010-07-14 17:59:35 -07002366 {
2367 AutoMutex _l(mLock);
2368 requestExit();
2369 mWaitWorkCV.signal();
2370 }
Zach Janga754b4f2015-10-27 01:29:34 +00002371 // Note that we can call it from the thread loop if all other references have been released
2372 // but it will safely return WOULD_BLOCK in this case
Mathias Agopian65ab4712010-07-14 17:59:35 -07002373 requestExitAndWait();
2374}
2375
2376void AudioPolicyService::AudioCommandThread::AudioCommand::dump(char* buffer, size_t size)
2377{
2378 snprintf(buffer, size, " %02d %06d.%03d %01u %p\n",
2379 mCommand,
2380 (int)ns2s(mTime),
2381 (int)ns2ms(mTime)%1000,
2382 mWaitStatus,
Eric Laurent0ede8922014-05-09 18:04:42 -07002383 mParam.get());
Mathias Agopian65ab4712010-07-14 17:59:35 -07002384}
2385
Dima Zavinfce7a472011-04-19 22:30:36 -07002386/******* helpers for the service_ops callbacks defined below *********/
2387void AudioPolicyService::setParameters(audio_io_handle_t ioHandle,
2388 const char *keyValuePairs,
2389 int delayMs)
2390{
Glenn Kasten72ef00d2012-01-17 11:09:42 -08002391 mAudioCommandThread->parametersCommand(ioHandle, keyValuePairs,
Dima Zavinfce7a472011-04-19 22:30:36 -07002392 delayMs);
2393}
2394
2395int AudioPolicyService::setStreamVolume(audio_stream_type_t stream,
2396 float volume,
2397 audio_io_handle_t output,
2398 int delayMs)
2399{
Glenn Kastenfff6d712012-01-12 16:38:12 -08002400 return (int)mAudioCommandThread->volumeCommand(stream, volume,
Glenn Kasten72ef00d2012-01-17 11:09:42 -08002401 output, delayMs);
Dima Zavinfce7a472011-04-19 22:30:36 -07002402}
2403
Dima Zavinfce7a472011-04-19 22:30:36 -07002404int AudioPolicyService::setVoiceVolume(float volume, int delayMs)
2405{
2406 return (int)mAudioCommandThread->voiceVolumeCommand(volume, delayMs);
2407}
2408
Eric Laurentb20cf7d2019-04-05 19:37:34 -07002409void AudioPolicyService::setEffectSuspended(int effectId,
2410 audio_session_t sessionId,
2411 bool suspended)
2412{
2413 mAudioCommandThread->setEffectSuspendedCommand(effectId, sessionId, suspended);
2414}
2415
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002416Status AudioPolicyService::onNewAudioModulesAvailable()
Mikhail Naganov88b30d22020-03-09 19:43:13 +00002417{
Mikhail Naganovd0e2c742020-03-25 15:59:59 -07002418 mOutputCommandThread->audioModulesUpdateCommand();
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002419 return Status::ok();
Mikhail Naganov88b30d22020-03-09 19:43:13 +00002420}
2421
Eric Laurentb20cf7d2019-04-05 19:37:34 -07002422
Dima Zavinfce7a472011-04-19 22:30:36 -07002423extern "C" {
Eric Laurent2d388ec2014-03-07 13:25:54 -08002424audio_module_handle_t aps_load_hw_module(void *service __unused,
2425 const char *name);
2426audio_io_handle_t aps_open_output(void *service __unused,
Eric Laurenta4c5a552012-03-29 10:12:40 -07002427 audio_devices_t *pDevices,
2428 uint32_t *pSamplingRate,
2429 audio_format_t *pFormat,
2430 audio_channel_mask_t *pChannelMask,
2431 uint32_t *pLatencyMs,
Eric Laurent2d388ec2014-03-07 13:25:54 -08002432 audio_output_flags_t flags);
Eric Laurenta4c5a552012-03-29 10:12:40 -07002433
Eric Laurent2d388ec2014-03-07 13:25:54 -08002434audio_io_handle_t aps_open_output_on_module(void *service __unused,
Eric Laurenta4c5a552012-03-29 10:12:40 -07002435 audio_module_handle_t module,
2436 audio_devices_t *pDevices,
2437 uint32_t *pSamplingRate,
2438 audio_format_t *pFormat,
2439 audio_channel_mask_t *pChannelMask,
2440 uint32_t *pLatencyMs,
Richard Fitzgeraldad3af332013-03-25 16:54:37 +00002441 audio_output_flags_t flags,
Eric Laurent2d388ec2014-03-07 13:25:54 -08002442 const audio_offload_info_t *offloadInfo);
2443audio_io_handle_t aps_open_dup_output(void *service __unused,
Dima Zavinfce7a472011-04-19 22:30:36 -07002444 audio_io_handle_t output1,
Eric Laurent2d388ec2014-03-07 13:25:54 -08002445 audio_io_handle_t output2);
2446int aps_close_output(void *service __unused, audio_io_handle_t output);
2447int aps_suspend_output(void *service __unused, audio_io_handle_t output);
2448int aps_restore_output(void *service __unused, audio_io_handle_t output);
2449audio_io_handle_t aps_open_input(void *service __unused,
Eric Laurenta4c5a552012-03-29 10:12:40 -07002450 audio_devices_t *pDevices,
2451 uint32_t *pSamplingRate,
2452 audio_format_t *pFormat,
2453 audio_channel_mask_t *pChannelMask,
Eric Laurent2d388ec2014-03-07 13:25:54 -08002454 audio_in_acoustics_t acoustics __unused);
2455audio_io_handle_t aps_open_input_on_module(void *service __unused,
Eric Laurenta4c5a552012-03-29 10:12:40 -07002456 audio_module_handle_t module,
2457 audio_devices_t *pDevices,
2458 uint32_t *pSamplingRate,
2459 audio_format_t *pFormat,
Eric Laurent2d388ec2014-03-07 13:25:54 -08002460 audio_channel_mask_t *pChannelMask);
2461int aps_close_input(void *service __unused, audio_io_handle_t input);
2462int aps_invalidate_stream(void *service __unused, audio_stream_type_t stream);
Glenn Kastend848eb42016-03-08 13:42:11 -08002463int aps_move_effects(void *service __unused, audio_session_t session,
Dima Zavinfce7a472011-04-19 22:30:36 -07002464 audio_io_handle_t src_output,
Eric Laurent2d388ec2014-03-07 13:25:54 -08002465 audio_io_handle_t dst_output);
2466char * aps_get_parameters(void *service __unused, audio_io_handle_t io_handle,
2467 const char *keys);
2468void aps_set_parameters(void *service, audio_io_handle_t io_handle,
2469 const char *kv_pairs, int delay_ms);
2470int aps_set_stream_volume(void *service, audio_stream_type_t stream,
Dima Zavinfce7a472011-04-19 22:30:36 -07002471 float volume, audio_io_handle_t output,
Eric Laurent2d388ec2014-03-07 13:25:54 -08002472 int delay_ms);
Eric Laurent2d388ec2014-03-07 13:25:54 -08002473int aps_set_voice_volume(void *service, float volume, int delay_ms);
2474};
Dima Zavinfce7a472011-04-19 22:30:36 -07002475
Mikhail Naganov1b2a7942017-12-08 10:18:09 -08002476} // namespace android