blob: a15970af60a866733999ecfb724797366ad1ccdb [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>
Mikhail Naganov959e2d02019-03-28 11:08:19 -070026
27#include <audio_utils/clock.h>
Mathias Agopian65ab4712010-07-14 17:59:35 -070028#include <binder/IServiceManager.h>
29#include <utils/Log.h>
30#include <cutils/properties.h>
31#include <binder/IPCThreadState.h>
Svet Ganovf4ddfef2018-01-16 07:37:58 -080032#include <binder/PermissionController.h>
33#include <binder/IResultReceiver.h>
Mathias Agopian65ab4712010-07-14 17:59:35 -070034#include <utils/String16.h>
35#include <utils/threads.h>
36#include "AudioPolicyService.h"
Mathias Agopian65ab4712010-07-14 17:59:35 -070037#include <hardware_legacy/power.h>
Eric Laurent7c7f10b2011-06-17 21:29:58 -070038#include <media/AudioEffect.h>
Chih-Hung Hsiehc84d9d22014-11-14 13:33:34 -080039#include <media/AudioParameter.h>
Andy Hungab7ef302018-05-15 19:35:29 -070040#include <mediautils/ServiceUtilities.h>
Michael Groovercfd28302018-12-11 19:16:46 -080041#include <sensorprivacy/SensorPrivacyManager.h>
Mathias Agopian65ab4712010-07-14 17:59:35 -070042
Dima Zavin64760242011-05-11 14:15:23 -070043#include <system/audio.h>
Dima Zavin7394a4f2011-06-13 18:16:26 -070044#include <system/audio_policy.h>
Mikhail Naganov61a4fac2016-10-13 14:44:18 -070045
Mathias Agopian65ab4712010-07-14 17:59:35 -070046namespace android {
47
Glenn Kasten8dad0e32012-01-09 08:41:22 -080048static const char kDeadlockedString[] = "AudioPolicyService may be deadlocked\n";
49static const char kCmdDeadlockedString[] = "AudioPolicyService command thread may be deadlocked\n";
Mathias Agopian65ab4712010-07-14 17:59:35 -070050
Mikhail Naganov959e2d02019-03-28 11:08:19 -070051static const int kDumpLockTimeoutNs = 1 * NANOS_PER_SECOND;
Mathias Agopian65ab4712010-07-14 17:59:35 -070052
Eric Laurent0ede8922014-05-09 18:04:42 -070053static const nsecs_t kAudioCommandTimeoutNs = seconds(3); // 3 seconds
Christer Fletcher5fa8c4b2013-01-18 15:27:03 +010054
Svet Ganovf4ddfef2018-01-16 07:37:58 -080055static const String16 sManageAudioPolicyPermission("android.permission.MANAGE_AUDIO_POLICY");
Dima Zavinfce7a472011-04-19 22:30:36 -070056
Mathias Agopian65ab4712010-07-14 17:59:35 -070057// ----------------------------------------------------------------------------
58
59AudioPolicyService::AudioPolicyService()
Ytai Ben-Tsvi85093d52020-03-26 09:41:15 -070060 : BnAudioPolicyService(),
61 mpAudioPolicyDev(NULL),
62 mpAudioPolicy(NULL),
63 mAudioPolicyManager(NULL),
64 mAudioPolicyClient(NULL),
65 mPhoneState(AUDIO_MODE_INVALID),
66 mCaptureStateNotifier(false) {
Eric Laurentf5ada6e2014-10-09 17:49:00 -070067}
68
69void AudioPolicyService::onFirstRef()
70{
Eric Laurent8b1e80b2014-10-07 09:08:47 -070071 {
72 Mutex::Autolock _l(mLock);
Eric Laurent93575202011-01-18 18:39:02 -080073
Eric Laurent8b1e80b2014-10-07 09:08:47 -070074 // start audio commands thread
75 mAudioCommandThread = new AudioCommandThread(String8("ApmAudio"), this);
76 // start output activity command thread
77 mOutputCommandThread = new AudioCommandThread(String8("ApmOutput"), this);
Eric Laurentdce54a12014-03-10 12:19:46 -070078
Eric Laurent8b1e80b2014-10-07 09:08:47 -070079 mAudioPolicyClient = new AudioPolicyClient(this);
80 mAudioPolicyManager = createAudioPolicyManager(mAudioPolicyClient);
Hayden Gomes524159d2019-12-23 14:41:47 -080081
82 mSupportedSystemUsages = std::vector<audio_usage_t> {};
Eric Laurent8b1e80b2014-10-07 09:08:47 -070083 }
bryant_liuba2b4392014-06-11 16:49:30 +080084 // load audio processing modules
Eric Laurent8b1e80b2014-10-07 09:08:47 -070085 sp<AudioPolicyEffects>audioPolicyEffects = new AudioPolicyEffects();
86 {
87 Mutex::Autolock _l(mLock);
88 mAudioPolicyEffects = audioPolicyEffects;
89 }
Svet Ganovf4ddfef2018-01-16 07:37:58 -080090
91 mUidPolicy = new UidPolicy(this);
92 mUidPolicy->registerSelf();
Michael Groovercfd28302018-12-11 19:16:46 -080093
94 mSensorPrivacyPolicy = new SensorPrivacyPolicy(this);
95 mSensorPrivacyPolicy->registerSelf();
Mathias Agopian65ab4712010-07-14 17:59:35 -070096}
97
98AudioPolicyService::~AudioPolicyService()
99{
Mathias Agopian65ab4712010-07-14 17:59:35 -0700100 mAudioCommandThread->exit();
Eric Laurent657ff612014-05-07 11:58:24 -0700101 mOutputCommandThread->exit();
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700102
Eric Laurentf269b8e2014-06-09 20:01:29 -0700103 destroyAudioPolicyManager(mAudioPolicyManager);
Eric Laurentdce54a12014-03-10 12:19:46 -0700104 delete mAudioPolicyClient;
Eric Laurentb52c1522014-05-20 11:27:36 -0700105
106 mNotificationClients.clear();
bryant_liuba2b4392014-06-11 16:49:30 +0800107 mAudioPolicyEffects.clear();
Svet Ganovf4ddfef2018-01-16 07:37:58 -0800108
109 mUidPolicy->unregisterSelf();
110 mUidPolicy.clear();
Michael Groovercfd28302018-12-11 19:16:46 -0800111
112 mSensorPrivacyPolicy->unregisterSelf();
113 mSensorPrivacyPolicy.clear();
Eric Laurentb52c1522014-05-20 11:27:36 -0700114}
115
116// A notification client is always registered by AudioSystem when the client process
117// connects to AudioPolicyService.
118void AudioPolicyService::registerClient(const sp<IAudioPolicyServiceClient>& client)
119{
Eric Laurent12590252015-08-21 18:40:20 -0700120 if (client == 0) {
121 ALOGW("%s got NULL client", __FUNCTION__);
122 return;
123 }
Eric Laurent0ebd5f92014-11-19 19:04:52 -0800124 Mutex::Autolock _l(mNotificationClientsLock);
Eric Laurentb52c1522014-05-20 11:27:36 -0700125
126 uid_t uid = IPCThreadState::self()->getCallingUid();
luochaojiang908c7d72018-06-21 14:58:04 +0800127 pid_t pid = IPCThreadState::self()->getCallingPid();
128 int64_t token = ((int64_t)uid<<32) | pid;
129
130 if (mNotificationClients.indexOfKey(token) < 0) {
Eric Laurentb52c1522014-05-20 11:27:36 -0700131 sp<NotificationClient> notificationClient = new NotificationClient(this,
132 client,
luochaojiang908c7d72018-06-21 14:58:04 +0800133 uid,
134 pid);
135 ALOGV("registerClient() client %p, uid %d pid %d", client.get(), uid, pid);
Eric Laurentb52c1522014-05-20 11:27:36 -0700136
luochaojiang908c7d72018-06-21 14:58:04 +0800137 mNotificationClients.add(token, notificationClient);
Eric Laurentb52c1522014-05-20 11:27:36 -0700138
Marco Nelissenf8880202014-11-14 07:58:25 -0800139 sp<IBinder> binder = IInterface::asBinder(client);
Eric Laurentb52c1522014-05-20 11:27:36 -0700140 binder->linkToDeath(notificationClient);
141 }
142}
143
Eric Laurente8726fe2015-06-26 09:39:24 -0700144void AudioPolicyService::setAudioPortCallbacksEnabled(bool enabled)
145{
146 Mutex::Autolock _l(mNotificationClientsLock);
147
148 uid_t uid = IPCThreadState::self()->getCallingUid();
luochaojiang908c7d72018-06-21 14:58:04 +0800149 pid_t pid = IPCThreadState::self()->getCallingPid();
150 int64_t token = ((int64_t)uid<<32) | pid;
151
152 if (mNotificationClients.indexOfKey(token) < 0) {
Eric Laurente8726fe2015-06-26 09:39:24 -0700153 return;
154 }
luochaojiang908c7d72018-06-21 14:58:04 +0800155 mNotificationClients.valueFor(token)->setAudioPortCallbacksEnabled(enabled);
Eric Laurente8726fe2015-06-26 09:39:24 -0700156}
157
François Gaffiecfe17322018-11-07 13:41:29 +0100158void AudioPolicyService::setAudioVolumeGroupCallbacksEnabled(bool enabled)
159{
160 Mutex::Autolock _l(mNotificationClientsLock);
161
162 uid_t uid = IPCThreadState::self()->getCallingUid();
163 pid_t pid = IPCThreadState::self()->getCallingPid();
164 int64_t token = ((int64_t)uid<<32) | pid;
165
166 if (mNotificationClients.indexOfKey(token) < 0) {
167 return;
168 }
169 mNotificationClients.valueFor(token)->setAudioVolumeGroupCallbacksEnabled(enabled);
170}
171
Eric Laurentb52c1522014-05-20 11:27:36 -0700172// removeNotificationClient() is called when the client process dies.
luochaojiang908c7d72018-06-21 14:58:04 +0800173void AudioPolicyService::removeNotificationClient(uid_t uid, pid_t pid)
Eric Laurentb52c1522014-05-20 11:27:36 -0700174{
Eric Laurent0ebd5f92014-11-19 19:04:52 -0800175 {
176 Mutex::Autolock _l(mNotificationClientsLock);
luochaojiang908c7d72018-06-21 14:58:04 +0800177 int64_t token = ((int64_t)uid<<32) | pid;
178 mNotificationClients.removeItem(token);
Eric Laurent0ebd5f92014-11-19 19:04:52 -0800179 }
Eric Laurent0ebd5f92014-11-19 19:04:52 -0800180 {
181 Mutex::Autolock _l(mLock);
luochaojiang908c7d72018-06-21 14:58:04 +0800182 bool hasSameUid = false;
183 for (size_t i = 0; i < mNotificationClients.size(); i++) {
184 if (mNotificationClients.valueAt(i)->uid() == uid) {
185 hasSameUid = true;
186 break;
187 }
188 }
189 if (mAudioPolicyManager && !hasSameUid) {
Eric Laurent10b71232018-04-13 18:14:44 -0700190 // called from binder death notification: no need to clear caller identity
Eric Laurent8c7e6da2015-04-21 17:37:00 -0700191 mAudioPolicyManager->releaseResourcesForUid(uid);
Eric Laurentb52c1522014-05-20 11:27:36 -0700192 }
Eric Laurent0ebd5f92014-11-19 19:04:52 -0800193 }
Eric Laurentb52c1522014-05-20 11:27:36 -0700194}
195
196void AudioPolicyService::onAudioPortListUpdate()
197{
198 mOutputCommandThread->updateAudioPortListCommand();
199}
200
201void AudioPolicyService::doOnAudioPortListUpdate()
202{
Eric Laurent0ebd5f92014-11-19 19:04:52 -0800203 Mutex::Autolock _l(mNotificationClientsLock);
Eric Laurentb52c1522014-05-20 11:27:36 -0700204 for (size_t i = 0; i < mNotificationClients.size(); i++) {
205 mNotificationClients.valueAt(i)->onAudioPortListUpdate();
206 }
207}
208
209void AudioPolicyService::onAudioPatchListUpdate()
210{
211 mOutputCommandThread->updateAudioPatchListCommand();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700212}
213
Eric Laurentb52c1522014-05-20 11:27:36 -0700214void AudioPolicyService::doOnAudioPatchListUpdate()
215{
Eric Laurent0ebd5f92014-11-19 19:04:52 -0800216 Mutex::Autolock _l(mNotificationClientsLock);
Eric Laurentb52c1522014-05-20 11:27:36 -0700217 for (size_t i = 0; i < mNotificationClients.size(); i++) {
218 mNotificationClients.valueAt(i)->onAudioPatchListUpdate();
219 }
220}
221
François Gaffiecfe17322018-11-07 13:41:29 +0100222void AudioPolicyService::onAudioVolumeGroupChanged(volume_group_t group, int flags)
223{
224 mOutputCommandThread->changeAudioVolumeGroupCommand(group, flags);
225}
226
227void AudioPolicyService::doOnAudioVolumeGroupChanged(volume_group_t group, int flags)
228{
229 Mutex::Autolock _l(mNotificationClientsLock);
230 for (size_t i = 0; i < mNotificationClients.size(); i++) {
231 mNotificationClients.valueAt(i)->onAudioVolumeGroupChanged(group, flags);
232 }
233}
234
Chih-Hung Hsiehe964d4e2016-08-09 14:31:32 -0700235void AudioPolicyService::onDynamicPolicyMixStateUpdate(const String8& regId, int32_t state)
Jean-Michel Trivide801052015-04-14 19:10:14 -0700236{
237 ALOGV("AudioPolicyService::onDynamicPolicyMixStateUpdate(%s, %d)",
238 regId.string(), state);
239 mOutputCommandThread->dynamicPolicyMixStateUpdateCommand(regId, state);
240}
241
Chih-Hung Hsiehe964d4e2016-08-09 14:31:32 -0700242void AudioPolicyService::doOnDynamicPolicyMixStateUpdate(const String8& regId, int32_t state)
Jean-Michel Trivide801052015-04-14 19:10:14 -0700243{
244 Mutex::Autolock _l(mNotificationClientsLock);
245 for (size_t i = 0; i < mNotificationClients.size(); i++) {
246 mNotificationClients.valueAt(i)->onDynamicPolicyMixStateUpdate(regId, state);
247 }
248}
249
Eric Laurenta9f86652018-11-28 17:23:11 -0800250void AudioPolicyService::onRecordingConfigurationUpdate(
251 int event,
252 const record_client_info_t *clientInfo,
253 const audio_config_base_t *clientConfig,
254 std::vector<effect_descriptor_t> clientEffects,
255 const audio_config_base_t *deviceConfig,
256 std::vector<effect_descriptor_t> effects,
257 audio_patch_handle_t patchHandle,
258 audio_source_t source)
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -0800259{
Jean-Michel Triviac4e4292016-12-22 11:39:31 -0800260 mOutputCommandThread->recordingConfigurationUpdateCommand(event, clientInfo,
Eric Laurenta9f86652018-11-28 17:23:11 -0800261 clientConfig, clientEffects, deviceConfig, effects, patchHandle, source);
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -0800262}
263
Eric Laurenta9f86652018-11-28 17:23:11 -0800264void AudioPolicyService::doOnRecordingConfigurationUpdate(
265 int event,
266 const record_client_info_t *clientInfo,
267 const audio_config_base_t *clientConfig,
268 std::vector<effect_descriptor_t> clientEffects,
269 const audio_config_base_t *deviceConfig,
270 std::vector<effect_descriptor_t> effects,
271 audio_patch_handle_t patchHandle,
272 audio_source_t source)
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -0800273{
274 Mutex::Autolock _l(mNotificationClientsLock);
275 for (size_t i = 0; i < mNotificationClients.size(); i++) {
Jean-Michel Triviac4e4292016-12-22 11:39:31 -0800276 mNotificationClients.valueAt(i)->onRecordingConfigurationUpdate(event, clientInfo,
Eric Laurenta9f86652018-11-28 17:23:11 -0800277 clientConfig, clientEffects, deviceConfig, effects, patchHandle, source);
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -0800278 }
279}
280
281status_t AudioPolicyService::clientCreateAudioPatch(const struct audio_patch *patch,
282 audio_patch_handle_t *handle,
283 int delayMs)
284{
285 return mAudioCommandThread->createAudioPatchCommand(patch, handle, delayMs);
286}
287
288status_t AudioPolicyService::clientReleaseAudioPatch(audio_patch_handle_t handle,
289 int delayMs)
290{
291 return mAudioCommandThread->releaseAudioPatchCommand(handle, delayMs);
292}
293
Eric Laurente1715a42014-05-20 11:30:42 -0700294status_t AudioPolicyService::clientSetAudioPortConfig(const struct audio_port_config *config,
295 int delayMs)
296{
297 return mAudioCommandThread->setAudioPortConfigCommand(config, delayMs);
298}
299
Eric Laurentb52c1522014-05-20 11:27:36 -0700300AudioPolicyService::NotificationClient::NotificationClient(const sp<AudioPolicyService>& service,
301 const sp<IAudioPolicyServiceClient>& client,
luochaojiang908c7d72018-06-21 14:58:04 +0800302 uid_t uid,
303 pid_t pid)
304 : mService(service), mUid(uid), mPid(pid), mAudioPolicyServiceClient(client),
François Gaffiecfe17322018-11-07 13:41:29 +0100305 mAudioPortCallbacksEnabled(false), mAudioVolumeGroupCallbacksEnabled(false)
Eric Laurentb52c1522014-05-20 11:27:36 -0700306{
307}
308
309AudioPolicyService::NotificationClient::~NotificationClient()
310{
311}
312
313void AudioPolicyService::NotificationClient::binderDied(const wp<IBinder>& who __unused)
314{
315 sp<NotificationClient> keep(this);
316 sp<AudioPolicyService> service = mService.promote();
317 if (service != 0) {
luochaojiang908c7d72018-06-21 14:58:04 +0800318 service->removeNotificationClient(mUid, mPid);
Eric Laurentb52c1522014-05-20 11:27:36 -0700319 }
320}
321
322void AudioPolicyService::NotificationClient::onAudioPortListUpdate()
323{
Eric Laurente8726fe2015-06-26 09:39:24 -0700324 if (mAudioPolicyServiceClient != 0 && mAudioPortCallbacksEnabled) {
Eric Laurentb52c1522014-05-20 11:27:36 -0700325 mAudioPolicyServiceClient->onAudioPortListUpdate();
326 }
327}
328
329void AudioPolicyService::NotificationClient::onAudioPatchListUpdate()
330{
Eric Laurente8726fe2015-06-26 09:39:24 -0700331 if (mAudioPolicyServiceClient != 0 && mAudioPortCallbacksEnabled) {
Eric Laurentb52c1522014-05-20 11:27:36 -0700332 mAudioPolicyServiceClient->onAudioPatchListUpdate();
333 }
334}
Eric Laurent57dae992011-07-24 13:36:09 -0700335
François Gaffiecfe17322018-11-07 13:41:29 +0100336void AudioPolicyService::NotificationClient::onAudioVolumeGroupChanged(volume_group_t group,
337 int flags)
338{
339 if (mAudioPolicyServiceClient != 0 && mAudioVolumeGroupCallbacksEnabled) {
340 mAudioPolicyServiceClient->onAudioVolumeGroupChanged(group, flags);
341 }
342}
343
344
Jean-Michel Trivide801052015-04-14 19:10:14 -0700345void AudioPolicyService::NotificationClient::onDynamicPolicyMixStateUpdate(
Chih-Hung Hsiehe964d4e2016-08-09 14:31:32 -0700346 const String8& regId, int32_t state)
Jean-Michel Trivide801052015-04-14 19:10:14 -0700347{
Andy Hung4ef19fa2018-05-15 19:35:29 -0700348 if (mAudioPolicyServiceClient != 0 && isServiceUid(mUid)) {
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -0800349 mAudioPolicyServiceClient->onDynamicPolicyMixStateUpdate(regId, state);
350 }
351}
352
353void AudioPolicyService::NotificationClient::onRecordingConfigurationUpdate(
Eric Laurenta9f86652018-11-28 17:23:11 -0800354 int event,
355 const record_client_info_t *clientInfo,
356 const audio_config_base_t *clientConfig,
357 std::vector<effect_descriptor_t> clientEffects,
358 const audio_config_base_t *deviceConfig,
359 std::vector<effect_descriptor_t> effects,
360 audio_patch_handle_t patchHandle,
361 audio_source_t source)
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -0800362{
Andy Hung4ef19fa2018-05-15 19:35:29 -0700363 if (mAudioPolicyServiceClient != 0 && isServiceUid(mUid)) {
Jean-Michel Triviac4e4292016-12-22 11:39:31 -0800364 mAudioPolicyServiceClient->onRecordingConfigurationUpdate(event, clientInfo,
Eric Laurenta9f86652018-11-28 17:23:11 -0800365 clientConfig, clientEffects, deviceConfig, effects, patchHandle, source);
Jean-Michel Trivide801052015-04-14 19:10:14 -0700366 }
367}
368
Eric Laurente8726fe2015-06-26 09:39:24 -0700369void AudioPolicyService::NotificationClient::setAudioPortCallbacksEnabled(bool enabled)
370{
371 mAudioPortCallbacksEnabled = enabled;
372}
373
François Gaffiecfe17322018-11-07 13:41:29 +0100374void AudioPolicyService::NotificationClient::setAudioVolumeGroupCallbacksEnabled(bool enabled)
375{
376 mAudioVolumeGroupCallbacksEnabled = enabled;
377}
Eric Laurente8726fe2015-06-26 09:39:24 -0700378
Mathias Agopian65ab4712010-07-14 17:59:35 -0700379void AudioPolicyService::binderDied(const wp<IBinder>& who) {
Glenn Kasten411e4472012-11-02 10:00:06 -0700380 ALOGW("binderDied() %p, calling pid %d", who.unsafe_get(),
Eric Laurentde070132010-07-13 04:45:46 -0700381 IPCThreadState::self()->getCallingPid());
Mathias Agopian65ab4712010-07-14 17:59:35 -0700382}
383
Mikhail Naganov959e2d02019-03-28 11:08:19 -0700384static bool dumpTryLock(Mutex& mutex)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700385{
Mikhail Naganov959e2d02019-03-28 11:08:19 -0700386 status_t err = mutex.timedLock(kDumpLockTimeoutNs);
387 return err == NO_ERROR;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700388}
389
390status_t AudioPolicyService::dumpInternals(int fd)
391{
392 const size_t SIZE = 256;
393 char buffer[SIZE];
394 String8 result;
395
Eric Laurentdce54a12014-03-10 12:19:46 -0700396 snprintf(buffer, SIZE, "AudioPolicyManager: %p\n", mAudioPolicyManager);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700397 result.append(buffer);
398 snprintf(buffer, SIZE, "Command Thread: %p\n", mAudioCommandThread.get());
399 result.append(buffer);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700400
Hayden Gomes524159d2019-12-23 14:41:47 -0800401 snprintf(buffer, SIZE, "Supported System Usages:\n");
402 result.append(buffer);
403 for (std::vector<audio_usage_t>::iterator it = mSupportedSystemUsages.begin();
404 it != mSupportedSystemUsages.end(); ++it) {
405 snprintf(buffer, SIZE, "\t%d\n", *it);
406 result.append(buffer);
407 }
408
Mathias Agopian65ab4712010-07-14 17:59:35 -0700409 write(fd, result.string(), result.size());
410 return NO_ERROR;
411}
412
Eric Laurente8c8b432018-10-17 10:08:02 -0700413void AudioPolicyService::updateUidStates()
Svet Ganovf4ddfef2018-01-16 07:37:58 -0800414{
Eric Laurente8c8b432018-10-17 10:08:02 -0700415 Mutex::Autolock _l(mLock);
416 updateUidStates_l();
417}
418
419void AudioPolicyService::updateUidStates_l()
420{
Eric Laurent4eb58f12018-12-07 16:41:02 -0800421// Go over all active clients and allow capture (does not force silence) in the
422// following cases:
Eric Laurent8ed73c12019-08-30 17:25:19 -0700423// The client is the assistant
Eric Laurent6ede98f2019-06-11 14:50:30 -0700424// AND an accessibility service is on TOP or a RTT call is active
Eric Laurent589171c2019-07-25 18:04:29 -0700425// AND the source is VOICE_RECOGNITION or HOTWORD
426// OR uses VOICE_RECOGNITION AND is on TOP
427// OR uses HOTWORD
Eric Laurent1ff16a72019-03-14 18:35:04 -0700428// AND there is no active privacy sensitive capture or call
429// OR client has CAPTURE_AUDIO_OUTPUT privileged permission
Eric Laurenta46bedb2018-12-07 18:01:26 -0800430// OR The client is an accessibility service
Eric Laurent589171c2019-07-25 18:04:29 -0700431// AND Is on TOP
432// AND the source is VOICE_RECOGNITION or HOTWORD
433// OR The assistant is not on TOP
434// AND there is no active privacy sensitive capture or call
435// OR client has CAPTURE_AUDIO_OUTPUT privileged permission
Eric Laurenta171e352019-05-07 13:04:45 -0700436// AND is on TOP
Eric Laurenta46bedb2018-12-07 18:01:26 -0800437// AND the source is VOICE_RECOGNITION or HOTWORD
Eric Laurent1ff16a72019-03-14 18:35:04 -0700438// OR the client source is virtual (remote submix, call audio TX or RX...)
Eric Laurent4e947da2019-10-17 15:24:06 -0700439// OR the client source is HOTWORD
440// AND is on TOP
441// OR all active clients are using HOTWORD source
442// AND no call is active
443// OR client has CAPTURE_AUDIO_OUTPUT privileged permission
Kohsuke Yatoha623a132020-03-24 20:10:26 -0700444// OR the client is the current InputMethodService
445// AND a RTT call is active AND the source is VOICE_RECOGNITION
Eric Laurenta171e352019-05-07 13:04:45 -0700446// OR Any client
Eric Laurenta46bedb2018-12-07 18:01:26 -0800447// AND The assistant is not on TOP
Eric Laurenta171e352019-05-07 13:04:45 -0700448// AND is on TOP or latest started
Eric Laurent1ff16a72019-03-14 18:35:04 -0700449// AND there is no active privacy sensitive capture or call
Eric Laurent589171c2019-07-25 18:04:29 -0700450// OR client has CAPTURE_AUDIO_OUTPUT privileged permission
Eric Laurent4eb58f12018-12-07 16:41:02 -0800451
Eric Laurent4e947da2019-10-17 15:24:06 -0700452
Eric Laurent4eb58f12018-12-07 16:41:02 -0800453 sp<AudioRecordClient> topActive;
454 sp<AudioRecordClient> latestActive;
Eric Laurentc21d5692020-02-25 10:24:36 -0800455 sp<AudioRecordClient> topSensitiveActive;
Eric Laurent4eb58f12018-12-07 16:41:02 -0800456 sp<AudioRecordClient> latestSensitiveActive;
Eric Laurent1ff16a72019-03-14 18:35:04 -0700457
Eric Laurenta46bedb2018-12-07 18:01:26 -0800458 nsecs_t topStartNs = 0;
459 nsecs_t latestStartNs = 0;
Eric Laurentc21d5692020-02-25 10:24:36 -0800460 nsecs_t topSensitiveStartNs = 0;
Eric Laurent4eb58f12018-12-07 16:41:02 -0800461 nsecs_t latestSensitiveStartNs = 0;
462 bool isA11yOnTop = mUidPolicy->isA11yOnTop();
463 bool isAssistantOnTop = false;
464 bool isSensitiveActive = false;
Eric Laurent1ff16a72019-03-14 18:35:04 -0700465 bool isInCall = mPhoneState == AUDIO_MODE_IN_CALL;
Eric Laurentc21d5692020-02-25 10:24:36 -0800466 bool isInCommunication = mPhoneState == AUDIO_MODE_IN_COMMUNICATION;
467 bool rttCallActive = (isInCall || isInCommunication)
Eric Laurent6ede98f2019-06-11 14:50:30 -0700468 && mUidPolicy->isRttEnabled();
Eric Laurent4e947da2019-10-17 15:24:06 -0700469 bool onlyHotwordActive = true;
Eric Laurent4eb58f12018-12-07 16:41:02 -0800470
Michael Groovercfd28302018-12-11 19:16:46 -0800471 // if Sensor Privacy is enabled then all recordings should be silenced.
472 if (mSensorPrivacyPolicy->isSensorPrivacyEnabled()) {
473 silenceAllRecordings_l();
474 return;
475 }
476
Eric Laurente8c8b432018-10-17 10:08:02 -0700477 for (size_t i =0; i < mAudioRecordClients.size(); i++) {
478 sp<AudioRecordClient> current = mAudioRecordClients[i];
Eric Laurent1ff16a72019-03-14 18:35:04 -0700479 if (!current->active) {
480 continue;
Eric Laurent4eb58f12018-12-07 16:41:02 -0800481 }
Eric Laurent1ff16a72019-03-14 18:35:04 -0700482
483 app_state_t appState = apmStatFromAmState(mUidPolicy->getUidState(current->uid));
484 // clients which app is in IDLE state are not eligible for top active or
485 // latest active
486 if (appState == APP_STATE_IDLE) {
487 continue;
488 }
489
Eric Laurent589171c2019-07-25 18:04:29 -0700490 bool isAccessibility = mUidPolicy->isA11yUid(current->uid);
Eric Laurentc21d5692020-02-25 10:24:36 -0800491 // Clients capturing for Accessibility services are not considered
492 // for top or latest active to avoid masking regular clients started before
493 if (!isAccessibility) {
494 bool isAssistant = mUidPolicy->isAssistantUid(current->uid);
495 bool isPrivacySensitive =
496 (current->attributes.flags & AUDIO_FLAG_CAPTURE_PRIVATE) != 0;
497 if (appState == APP_STATE_TOP) {
498 if (isPrivacySensitive) {
499 if (current->startTimeNs > topSensitiveStartNs) {
500 topSensitiveActive = current;
501 topSensitiveStartNs = current->startTimeNs;
502 }
503 } else {
504 if (current->startTimeNs > topStartNs) {
505 topActive = current;
506 topStartNs = current->startTimeNs;
507 }
508 }
509 if (isAssistant) {
510 isAssistantOnTop = true;
511 }
Eric Laurenta46bedb2018-12-07 18:01:26 -0800512 }
Eric Laurentc21d5692020-02-25 10:24:36 -0800513 // Clients capturing for HOTWORD are not considered
514 // for latest active to avoid masking regular clients started before
515 if (!(current->attributes.source == AUDIO_SOURCE_HOTWORD
516 || ((isA11yOnTop || rttCallActive) && isAssistant))) {
517 if (isPrivacySensitive) {
518 if (current->startTimeNs > latestSensitiveStartNs) {
519 latestSensitiveActive = current;
520 latestSensitiveStartNs = current->startTimeNs;
521 }
522 isSensitiveActive = true;
523 } else {
524 if (current->startTimeNs > latestStartNs) {
525 latestActive = current;
526 latestStartNs = current->startTimeNs;
527 }
528 }
Eric Laurent4eb58f12018-12-07 16:41:02 -0800529 }
530 }
Eric Laurent4e947da2019-10-17 15:24:06 -0700531 if (current->attributes.source != AUDIO_SOURCE_HOTWORD) {
532 onlyHotwordActive = false;
533 }
Eric Laurent4eb58f12018-12-07 16:41:02 -0800534 }
535
Eric Laurent1ff16a72019-03-14 18:35:04 -0700536 // if no active client with UI on Top, consider latest active as top
537 if (topActive == nullptr) {
538 topActive = latestActive;
Eric Laurentc21d5692020-02-25 10:24:36 -0800539 topStartNs = latestStartNs;
540 }
541 if (topSensitiveActive == nullptr) {
542 topSensitiveActive = latestSensitiveActive;
543 topSensitiveStartNs = latestSensitiveStartNs;
544 }
545
546 // If both privacy sensitive and regular capture are active:
547 // if the regular capture is privileged
548 // allow concurrency
549 // else
550 // favor the privacy sensitive case
551 if (topActive != nullptr && topSensitiveActive != nullptr
Ricardo Correa57a37692020-03-23 17:27:25 -0700552 && !topActive->canCaptureOutput) {
Eric Laurentc21d5692020-02-25 10:24:36 -0800553 topActive = nullptr;
Eric Laurent4eb58f12018-12-07 16:41:02 -0800554 }
555
556 for (size_t i =0; i < mAudioRecordClients.size(); i++) {
557 sp<AudioRecordClient> current = mAudioRecordClients[i];
Eric Laurent1ff16a72019-03-14 18:35:04 -0700558 if (!current->active) {
559 continue;
560 }
561
Eric Laurent4eb58f12018-12-07 16:41:02 -0800562 audio_source_t source = current->attributes.source;
Eric Laurent1ff16a72019-03-14 18:35:04 -0700563 bool isTopOrLatestActive = topActive == nullptr ? false : current->uid == topActive->uid;
Eric Laurentc21d5692020-02-25 10:24:36 -0800564 bool isTopOrLatestSensitive = topSensitiveActive == nullptr ?
565 false : current->uid == topSensitiveActive->uid;
566
567 auto canCaptureIfInCallOrCommunication = [&](const auto &recordClient) {
Ricardo Correa57a37692020-03-23 17:27:25 -0700568 bool canCaptureCall = recordClient->canCaptureOutput;
569 bool canCaptureCommunication = recordClient->canCaptureOutput
Eric Laurentc21d5692020-02-25 10:24:36 -0800570 || recordClient->uid == mPhoneStateOwnerUid
571 || isServiceUid(mPhoneStateOwnerUid);
572 return !(isInCall && !canCaptureCall)
573 && !(isInCommunication && !canCaptureCommunication);
574 };
Eric Laurent1ff16a72019-03-14 18:35:04 -0700575
576 // By default allow capture if:
577 // The assistant is not on TOP
Eric Laurenta171e352019-05-07 13:04:45 -0700578 // AND is on TOP or latest started
Eric Laurent1ff16a72019-03-14 18:35:04 -0700579 // AND there is no active privacy sensitive capture or call
580 // OR client has CAPTURE_AUDIO_OUTPUT privileged permission
581 bool allowCapture = !isAssistantOnTop
Eric Laurentc21d5692020-02-25 10:24:36 -0800582 && (isTopOrLatestActive || isTopOrLatestSensitive)
583 && !(isSensitiveActive
Ricardo Correa57a37692020-03-23 17:27:25 -0700584 && !(isTopOrLatestSensitive || current->canCaptureOutput))
Eric Laurentc21d5692020-02-25 10:24:36 -0800585 && canCaptureIfInCallOrCommunication(current);
Eric Laurent2dc962b2019-03-01 08:25:25 -0800586
587 if (isVirtualSource(source)) {
Eric Laurent1ff16a72019-03-14 18:35:04 -0700588 // Allow capture for virtual (remote submix, call audio TX or RX...) sources
589 allowCapture = true;
Eric Laurent2dc962b2019-03-01 08:25:25 -0800590 } else if (mUidPolicy->isAssistantUid(current->uid)) {
Eric Laurent1ff16a72019-03-14 18:35:04 -0700591 // For assistant allow capture if:
Eric Laurent6ede98f2019-06-11 14:50:30 -0700592 // An accessibility service is on TOP or a RTT call is active
Eric Laurent1ff16a72019-03-14 18:35:04 -0700593 // AND the source is VOICE_RECOGNITION or HOTWORD
Eric Laurenta171e352019-05-07 13:04:45 -0700594 // OR is on TOP AND uses VOICE_RECOGNITION
Eric Laurent1ff16a72019-03-14 18:35:04 -0700595 // OR uses HOTWORD
596 // AND there is no active privacy sensitive capture or call
597 // OR client has CAPTURE_AUDIO_OUTPUT privileged permission
Eric Laurent6ede98f2019-06-11 14:50:30 -0700598 if (isA11yOnTop || rttCallActive) {
Eric Laurent4eb58f12018-12-07 16:41:02 -0800599 if (source == AUDIO_SOURCE_HOTWORD || source == AUDIO_SOURCE_VOICE_RECOGNITION) {
Eric Laurent1ff16a72019-03-14 18:35:04 -0700600 allowCapture = true;
Eric Laurent4eb58f12018-12-07 16:41:02 -0800601 }
602 } else {
Eric Laurenta171e352019-05-07 13:04:45 -0700603 if (((isAssistantOnTop && source == AUDIO_SOURCE_VOICE_RECOGNITION) ||
Eric Laurentc21d5692020-02-25 10:24:36 -0800604 source == AUDIO_SOURCE_HOTWORD)
Ricardo Correa57a37692020-03-23 17:27:25 -0700605 && !(isSensitiveActive && !current->canCaptureOutput)
Eric Laurentc21d5692020-02-25 10:24:36 -0800606 && canCaptureIfInCallOrCommunication(current)) {
Eric Laurent1ff16a72019-03-14 18:35:04 -0700607 allowCapture = true;
Eric Laurent4eb58f12018-12-07 16:41:02 -0800608 }
609 }
610 } else if (mUidPolicy->isA11yUid(current->uid)) {
Eric Laurent1ff16a72019-03-14 18:35:04 -0700611 // For accessibility service allow capture if:
Eric Laurent47670c92019-08-28 16:59:05 -0700612 // The assistant is not on TOP
613 // AND there is no active privacy sensitive capture or call
Eric Laurent589171c2019-07-25 18:04:29 -0700614 // OR client has CAPTURE_AUDIO_OUTPUT privileged permission
Eric Laurent47670c92019-08-28 16:59:05 -0700615 // OR
616 // Is on TOP AND the source is VOICE_RECOGNITION or HOTWORD
617 if (!isAssistantOnTop
Ricardo Correa57a37692020-03-23 17:27:25 -0700618 && !(isSensitiveActive && !current->canCaptureOutput)
Eric Laurentc21d5692020-02-25 10:24:36 -0800619 && canCaptureIfInCallOrCommunication(current)) {
Eric Laurent47670c92019-08-28 16:59:05 -0700620 allowCapture = true;
621 }
Eric Laurent589171c2019-07-25 18:04:29 -0700622 if (isA11yOnTop) {
623 if (source == AUDIO_SOURCE_VOICE_RECOGNITION || source == AUDIO_SOURCE_HOTWORD) {
624 allowCapture = true;
625 }
Eric Laurent4eb58f12018-12-07 16:41:02 -0800626 }
Eric Laurent4e947da2019-10-17 15:24:06 -0700627 } else if (source == AUDIO_SOURCE_HOTWORD) {
628 // For HOTWORD source allow capture when not on TOP if:
629 // All active clients are using HOTWORD source
630 // AND no call is active
631 // OR client has CAPTURE_AUDIO_OUTPUT privileged permission
Eric Laurentc21d5692020-02-25 10:24:36 -0800632 if (onlyHotwordActive
633 && canCaptureIfInCallOrCommunication(current)) {
Eric Laurent4e947da2019-10-17 15:24:06 -0700634 allowCapture = true;
635 }
Kohsuke Yatoha623a132020-03-24 20:10:26 -0700636 } else if (mUidPolicy->isCurrentImeUid(current->uid)) {
637 // For current InputMethodService allow capture if:
638 // A RTT call is active AND the source is VOICE_RECOGNITION
639 if (rttCallActive && source == AUDIO_SOURCE_VOICE_RECOGNITION) {
640 allowCapture = true;
641 }
Eric Laurent4eb58f12018-12-07 16:41:02 -0800642 }
Eric Laurent5ada82e2019-08-29 17:53:54 -0700643 setAppState_l(current->portId,
Eric Laurent1ff16a72019-03-14 18:35:04 -0700644 allowCapture ? apmStatFromAmState(mUidPolicy->getUidState(current->uid)) :
645 APP_STATE_IDLE);
Eric Laurente8c8b432018-10-17 10:08:02 -0700646 }
647}
648
Michael Groovercfd28302018-12-11 19:16:46 -0800649void AudioPolicyService::silenceAllRecordings_l() {
650 for (size_t i = 0; i < mAudioRecordClients.size(); i++) {
651 sp<AudioRecordClient> current = mAudioRecordClients[i];
Eric Laurent1ff16a72019-03-14 18:35:04 -0700652 if (!isVirtualSource(current->attributes.source)) {
Eric Laurent5ada82e2019-08-29 17:53:54 -0700653 setAppState_l(current->portId, APP_STATE_IDLE);
Eric Laurent1ff16a72019-03-14 18:35:04 -0700654 }
Michael Groovercfd28302018-12-11 19:16:46 -0800655 }
656}
657
Eric Laurente8c8b432018-10-17 10:08:02 -0700658/* static */
659app_state_t AudioPolicyService::apmStatFromAmState(int amState) {
Eric Laurent1ff16a72019-03-14 18:35:04 -0700660
661 if (amState == ActivityManager::PROCESS_STATE_UNKNOWN) {
Eric Laurente8c8b432018-10-17 10:08:02 -0700662 return APP_STATE_IDLE;
Eric Laurent1ff16a72019-03-14 18:35:04 -0700663 } else if (amState <= ActivityManager::PROCESS_STATE_TOP) {
664 // include persistent services
665 return APP_STATE_TOP;
Eric Laurente8c8b432018-10-17 10:08:02 -0700666 }
667 return APP_STATE_FOREGROUND;
668}
669
Eric Laurent4eb58f12018-12-07 16:41:02 -0800670/* static */
Eric Laurent2dc962b2019-03-01 08:25:25 -0800671bool AudioPolicyService::isVirtualSource(audio_source_t source)
Eric Laurent4eb58f12018-12-07 16:41:02 -0800672{
673 switch (source) {
674 case AUDIO_SOURCE_VOICE_UPLINK:
675 case AUDIO_SOURCE_VOICE_DOWNLINK:
676 case AUDIO_SOURCE_VOICE_CALL:
Eric Laurent2dc962b2019-03-01 08:25:25 -0800677 case AUDIO_SOURCE_REMOTE_SUBMIX:
678 case AUDIO_SOURCE_FM_TUNER:
Eric Laurent68eb2122020-04-30 17:40:57 -0700679 case AUDIO_SOURCE_ECHO_REFERENCE:
Eric Laurent4eb58f12018-12-07 16:41:02 -0800680 return true;
681 default:
682 break;
683 }
684 return false;
685}
686
Eric Laurent5ada82e2019-08-29 17:53:54 -0700687void AudioPolicyService::setAppState_l(audio_port_handle_t portId, app_state_t state)
Eric Laurente8c8b432018-10-17 10:08:02 -0700688{
689 AutoCallerClear acc;
690
691 if (mAudioPolicyManager) {
Eric Laurent5ada82e2019-08-29 17:53:54 -0700692 mAudioPolicyManager->setAppState(portId, state);
Mikhail Naganoveae73eb2018-04-03 16:57:36 -0700693 }
694 sp<IAudioFlinger> af = AudioSystem::get_audio_flinger();
695 if (af) {
Eric Laurentf32108e2018-10-04 17:22:04 -0700696 bool silenced = state == APP_STATE_IDLE;
Eric Laurent5ada82e2019-08-29 17:53:54 -0700697 af->setRecordSilenced(portId, silenced);
Mikhail Naganoveae73eb2018-04-03 16:57:36 -0700698 }
Svet Ganovf4ddfef2018-01-16 07:37:58 -0800699}
700
Glenn Kasten0f11b512014-01-31 16:18:54 -0800701status_t AudioPolicyService::dump(int fd, const Vector<String16>& args __unused)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700702{
Glenn Kasten44deb052012-02-05 18:09:08 -0800703 if (!dumpAllowed()) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700704 dumpPermissionDenial(fd);
705 } else {
Mikhail Naganov959e2d02019-03-28 11:08:19 -0700706 bool locked = dumpTryLock(mLock);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700707 if (!locked) {
708 String8 result(kDeadlockedString);
709 write(fd, result.string(), result.size());
710 }
711
712 dumpInternals(fd);
Glenn Kasten9d1f02d2012-02-08 17:47:58 -0800713 if (mAudioCommandThread != 0) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700714 mAudioCommandThread->dump(fd);
715 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700716
Eric Laurentdce54a12014-03-10 12:19:46 -0700717 if (mAudioPolicyManager) {
718 mAudioPolicyManager->dump(fd);
719 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700720
Kevin Rocard8be94972019-02-22 13:26:25 -0800721 mPackageManager.dump(fd);
722
Mathias Agopian65ab4712010-07-14 17:59:35 -0700723 if (locked) mLock.unlock();
724 }
725 return NO_ERROR;
726}
727
728status_t AudioPolicyService::dumpPermissionDenial(int fd)
729{
730 const size_t SIZE = 256;
731 char buffer[SIZE];
732 String8 result;
733 snprintf(buffer, SIZE, "Permission Denial: "
734 "can't dump AudioPolicyService from pid=%d, uid=%d\n",
735 IPCThreadState::self()->getCallingPid(),
736 IPCThreadState::self()->getCallingUid());
737 result.append(buffer);
738 write(fd, result.string(), result.size());
739 return NO_ERROR;
740}
741
742status_t AudioPolicyService::onTransact(
Svet Ganovf4ddfef2018-01-16 07:37:58 -0800743 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags) {
744 switch (code) {
745 case SHELL_COMMAND_TRANSACTION: {
746 int in = data.readFileDescriptor();
747 int out = data.readFileDescriptor();
748 int err = data.readFileDescriptor();
749 int argc = data.readInt32();
750 Vector<String16> args;
751 for (int i = 0; i < argc && data.dataAvail() > 0; i++) {
752 args.add(data.readString16());
753 }
754 sp<IBinder> unusedCallback;
755 sp<IResultReceiver> resultReceiver;
756 status_t status;
757 if ((status = data.readNullableStrongBinder(&unusedCallback)) != NO_ERROR) {
758 return status;
759 }
760 if ((status = data.readNullableStrongBinder(&resultReceiver)) != NO_ERROR) {
761 return status;
762 }
763 status = shellCommand(in, out, err, args);
764 if (resultReceiver != nullptr) {
765 resultReceiver->send(status);
766 }
767 return NO_ERROR;
768 }
769 }
770
Mathias Agopian65ab4712010-07-14 17:59:35 -0700771 return BnAudioPolicyService::onTransact(code, data, reply, flags);
772}
773
Svet Ganovf4ddfef2018-01-16 07:37:58 -0800774// ------------------- Shell command implementation -------------------
775
776// NOTE: This is a remote API - make sure all args are validated
777status_t AudioPolicyService::shellCommand(int in, int out, int err, Vector<String16>& args) {
778 if (!checkCallingPermission(sManageAudioPolicyPermission, nullptr, nullptr)) {
779 return PERMISSION_DENIED;
780 }
781 if (in == BAD_TYPE || out == BAD_TYPE || err == BAD_TYPE) {
782 return BAD_VALUE;
783 }
jovanakbe066e12019-09-02 11:54:39 -0700784 if (args.size() >= 3 && args[0] == String16("set-uid-state")) {
Svet Ganovf4ddfef2018-01-16 07:37:58 -0800785 return handleSetUidState(args, err);
jovanakbe066e12019-09-02 11:54:39 -0700786 } else if (args.size() >= 2 && args[0] == String16("reset-uid-state")) {
Svet Ganovf4ddfef2018-01-16 07:37:58 -0800787 return handleResetUidState(args, err);
jovanakbe066e12019-09-02 11:54:39 -0700788 } else if (args.size() >= 2 && args[0] == String16("get-uid-state")) {
Svet Ganovf4ddfef2018-01-16 07:37:58 -0800789 return handleGetUidState(args, out, err);
790 } else if (args.size() == 1 && args[0] == String16("help")) {
791 printHelp(out);
792 return NO_ERROR;
793 }
794 printHelp(err);
795 return BAD_VALUE;
796}
797
jovanakbe066e12019-09-02 11:54:39 -0700798static status_t getUidForPackage(String16 packageName, int userId, /*inout*/uid_t& uid, int err) {
799 if (userId < 0) {
800 ALOGE("Invalid user: %d", userId);
801 dprintf(err, "Invalid user: %d\n", userId);
Svet Ganovf4ddfef2018-01-16 07:37:58 -0800802 return BAD_VALUE;
803 }
jovanakbe066e12019-09-02 11:54:39 -0700804
805 PermissionController pc;
806 uid = pc.getPackageUid(packageName, 0);
807 if (uid <= 0) {
808 ALOGE("Unknown package: '%s'", String8(packageName).string());
809 dprintf(err, "Unknown package: '%s'\n", String8(packageName).string());
810 return BAD_VALUE;
811 }
812
813 uid = multiuser_get_uid(userId, uid);
814 return NO_ERROR;
815}
816
817status_t AudioPolicyService::handleSetUidState(Vector<String16>& args, int err) {
818 // Valid arg.size() is 3 or 5, args.size() is 5 with --user option.
819 if (!(args.size() == 3 || args.size() == 5)) {
820 printHelp(err);
821 return BAD_VALUE;
822 }
823
Svet Ganovf4ddfef2018-01-16 07:37:58 -0800824 bool active = false;
825 if (args[2] == String16("active")) {
826 active = true;
827 } else if ((args[2] != String16("idle"))) {
828 ALOGE("Expected active or idle but got: '%s'", String8(args[2]).string());
829 return BAD_VALUE;
830 }
jovanakbe066e12019-09-02 11:54:39 -0700831
832 int userId = 0;
833 if (args.size() >= 5 && args[3] == String16("--user")) {
834 userId = atoi(String8(args[4]));
835 }
836
837 uid_t uid;
838 if (getUidForPackage(args[1], userId, uid, err) == BAD_VALUE) {
839 return BAD_VALUE;
840 }
841
Svet Ganovf4ddfef2018-01-16 07:37:58 -0800842 mUidPolicy->addOverrideUid(uid, active);
843 return NO_ERROR;
844}
845
846status_t AudioPolicyService::handleResetUidState(Vector<String16>& args, int err) {
jovanakbe066e12019-09-02 11:54:39 -0700847 // Valid arg.size() is 2 or 4, args.size() is 4 with --user option.
848 if (!(args.size() == 2 || args.size() == 4)) {
849 printHelp(err);
Svet Ganovf4ddfef2018-01-16 07:37:58 -0800850 return BAD_VALUE;
851 }
jovanakbe066e12019-09-02 11:54:39 -0700852
853 int userId = 0;
854 if (args.size() >= 4 && args[2] == String16("--user")) {
855 userId = atoi(String8(args[3]));
856 }
857
858 uid_t uid;
859 if (getUidForPackage(args[1], userId, uid, err) == BAD_VALUE) {
860 return BAD_VALUE;
861 }
862
Svet Ganovf4ddfef2018-01-16 07:37:58 -0800863 mUidPolicy->removeOverrideUid(uid);
864 return NO_ERROR;
865}
866
867status_t AudioPolicyService::handleGetUidState(Vector<String16>& args, int out, int err) {
jovanakbe066e12019-09-02 11:54:39 -0700868 // Valid arg.size() is 2 or 4, args.size() is 4 with --user option.
869 if (!(args.size() == 2 || args.size() == 4)) {
870 printHelp(err);
Svet Ganovf4ddfef2018-01-16 07:37:58 -0800871 return BAD_VALUE;
872 }
jovanakbe066e12019-09-02 11:54:39 -0700873
874 int userId = 0;
875 if (args.size() >= 4 && args[2] == String16("--user")) {
876 userId = atoi(String8(args[3]));
877 }
878
879 uid_t uid;
880 if (getUidForPackage(args[1], userId, uid, err) == BAD_VALUE) {
881 return BAD_VALUE;
882 }
883
Svet Ganovf4ddfef2018-01-16 07:37:58 -0800884 if (mUidPolicy->isUidActive(uid)) {
885 return dprintf(out, "active\n");
886 } else {
887 return dprintf(out, "idle\n");
888 }
889}
890
891status_t AudioPolicyService::printHelp(int out) {
892 return dprintf(out, "Audio policy service commands:\n"
jovanakbe066e12019-09-02 11:54:39 -0700893 " get-uid-state <PACKAGE> [--user USER_ID] gets the uid state\n"
894 " set-uid-state <PACKAGE> <active|idle> [--user USER_ID] overrides the uid state\n"
895 " reset-uid-state <PACKAGE> [--user USER_ID] clears the uid state override\n"
Svet Ganovf4ddfef2018-01-16 07:37:58 -0800896 " help print this message\n");
897}
898
899// ----------- AudioPolicyService::UidPolicy implementation ----------
900
901void AudioPolicyService::UidPolicy::registerSelf() {
Steven Moreland2f348142019-07-02 15:59:07 -0700902 status_t res = mAm.linkToDeath(this);
903 mAm.registerUidObserver(this, ActivityManager::UID_OBSERVER_GONE
Svet Ganovf4ddfef2018-01-16 07:37:58 -0800904 | ActivityManager::UID_OBSERVER_IDLE
Eric Laurente8c8b432018-10-17 10:08:02 -0700905 | ActivityManager::UID_OBSERVER_ACTIVE
906 | ActivityManager::UID_OBSERVER_PROCSTATE,
Svet Ganovf4ddfef2018-01-16 07:37:58 -0800907 ActivityManager::PROCESS_STATE_UNKNOWN,
908 String16("audioserver"));
Mikhail Naganoveae73eb2018-04-03 16:57:36 -0700909 if (!res) {
910 Mutex::Autolock _l(mLock);
911 mObserverRegistered = true;
912 } else {
913 ALOGE("UidPolicy::registerSelf linkToDeath failed: %d", res);
Eric Laurent4eb58f12018-12-07 16:41:02 -0800914
Steven Moreland2f348142019-07-02 15:59:07 -0700915 mAm.unregisterUidObserver(this);
Mikhail Naganoveae73eb2018-04-03 16:57:36 -0700916 }
Svet Ganovf4ddfef2018-01-16 07:37:58 -0800917}
918
919void AudioPolicyService::UidPolicy::unregisterSelf() {
Steven Moreland2f348142019-07-02 15:59:07 -0700920 mAm.unlinkToDeath(this);
921 mAm.unregisterUidObserver(this);
Mikhail Naganoveae73eb2018-04-03 16:57:36 -0700922 Mutex::Autolock _l(mLock);
923 mObserverRegistered = false;
Svet Ganovf4ddfef2018-01-16 07:37:58 -0800924}
925
Mikhail Naganoveae73eb2018-04-03 16:57:36 -0700926void AudioPolicyService::UidPolicy::binderDied(__unused const wp<IBinder> &who) {
927 Mutex::Autolock _l(mLock);
928 mCachedUids.clear();
929 mObserverRegistered = false;
Svet Ganovf4ddfef2018-01-16 07:37:58 -0800930}
931
Eric Laurente8c8b432018-10-17 10:08:02 -0700932void AudioPolicyService::UidPolicy::checkRegistered() {
Mikhail Naganoveae73eb2018-04-03 16:57:36 -0700933 bool needToReregister = false;
934 {
935 Mutex::Autolock _l(mLock);
936 needToReregister = !mObserverRegistered;
Svet Ganovf4ddfef2018-01-16 07:37:58 -0800937 }
Mikhail Naganoveae73eb2018-04-03 16:57:36 -0700938 if (needToReregister) {
939 // Looks like ActivityManager has died previously, attempt to re-register.
940 registerSelf();
941 }
Eric Laurente8c8b432018-10-17 10:08:02 -0700942}
943
944bool AudioPolicyService::UidPolicy::isUidActive(uid_t uid) {
945 if (isServiceUid(uid)) return true;
946 checkRegistered();
Mikhail Naganoveae73eb2018-04-03 16:57:36 -0700947 {
948 Mutex::Autolock _l(mLock);
949 auto overrideIter = mOverrideUids.find(uid);
950 if (overrideIter != mOverrideUids.end()) {
Eric Laurente8c8b432018-10-17 10:08:02 -0700951 return overrideIter->second.first;
Mikhail Naganoveae73eb2018-04-03 16:57:36 -0700952 }
953 // In an absense of the ActivityManager, assume everything to be active.
954 if (!mObserverRegistered) return true;
955 auto cacheIter = mCachedUids.find(uid);
Mikhail Naganoveba668a2018-04-05 08:13:15 -0700956 if (cacheIter != mCachedUids.end()) {
Eric Laurente8c8b432018-10-17 10:08:02 -0700957 return cacheIter->second.first;
Mikhail Naganoveae73eb2018-04-03 16:57:36 -0700958 }
959 }
960 ActivityManager am;
Hui Yuffffffc2020-03-24 16:13:59 -0700961 bool active = am.isUidActiveOrForeground(uid, String16("audioserver"));
Mikhail Naganoveae73eb2018-04-03 16:57:36 -0700962 {
963 Mutex::Autolock _l(mLock);
Eric Laurente8c8b432018-10-17 10:08:02 -0700964 mCachedUids.insert(std::pair<uid_t,
965 std::pair<bool, int>>(uid, std::pair<bool, int>(active,
966 ActivityManager::PROCESS_STATE_UNKNOWN)));
Mikhail Naganoveae73eb2018-04-03 16:57:36 -0700967 }
968 return active;
Svet Ganovf4ddfef2018-01-16 07:37:58 -0800969}
970
Eric Laurente8c8b432018-10-17 10:08:02 -0700971int AudioPolicyService::UidPolicy::getUidState(uid_t uid) {
972 if (isServiceUid(uid)) {
973 return ActivityManager::PROCESS_STATE_TOP;
974 }
975 checkRegistered();
976 {
977 Mutex::Autolock _l(mLock);
978 auto overrideIter = mOverrideUids.find(uid);
979 if (overrideIter != mOverrideUids.end()) {
980 if (overrideIter->second.first) {
981 if (overrideIter->second.second != ActivityManager::PROCESS_STATE_UNKNOWN) {
982 return overrideIter->second.second;
983 } else {
984 auto cacheIter = mCachedUids.find(uid);
985 if (cacheIter != mCachedUids.end()) {
986 return cacheIter->second.second;
987 }
988 }
989 }
990 return ActivityManager::PROCESS_STATE_UNKNOWN;
991 }
992 // In an absense of the ActivityManager, assume everything to be active.
993 if (!mObserverRegistered) {
994 return ActivityManager::PROCESS_STATE_TOP;
995 }
996 auto cacheIter = mCachedUids.find(uid);
997 if (cacheIter != mCachedUids.end()) {
998 if (cacheIter->second.first) {
999 return cacheIter->second.second;
1000 } else {
1001 return ActivityManager::PROCESS_STATE_UNKNOWN;
1002 }
1003 }
1004 }
1005 ActivityManager am;
Hui Yuffffffc2020-03-24 16:13:59 -07001006 bool active = am.isUidActiveOrForeground(uid, String16("audioserver"));
Eric Laurente8c8b432018-10-17 10:08:02 -07001007 int state = ActivityManager::PROCESS_STATE_UNKNOWN;
1008 if (active) {
1009 state = am.getUidProcessState(uid, String16("audioserver"));
1010 }
1011 {
1012 Mutex::Autolock _l(mLock);
1013 mCachedUids.insert(std::pair<uid_t,
1014 std::pair<bool, int>>(uid, std::pair<bool, int>(active, state)));
1015 }
Eric Laurent4eb58f12018-12-07 16:41:02 -08001016
Eric Laurente8c8b432018-10-17 10:08:02 -07001017 return state;
1018}
1019
Mikhail Naganoveae73eb2018-04-03 16:57:36 -07001020void AudioPolicyService::UidPolicy::onUidActive(uid_t uid) {
Eric Laurente8c8b432018-10-17 10:08:02 -07001021 updateUid(&mCachedUids, uid, true, ActivityManager::PROCESS_STATE_UNKNOWN, true);
Mikhail Naganoveae73eb2018-04-03 16:57:36 -07001022}
1023
1024void AudioPolicyService::UidPolicy::onUidGone(uid_t uid, __unused bool disabled) {
Eric Laurente8c8b432018-10-17 10:08:02 -07001025 updateUid(&mCachedUids, uid, false, ActivityManager::PROCESS_STATE_UNKNOWN, false);
Mikhail Naganoveae73eb2018-04-03 16:57:36 -07001026}
1027
1028void AudioPolicyService::UidPolicy::onUidIdle(uid_t uid, __unused bool disabled) {
Eric Laurente8c8b432018-10-17 10:08:02 -07001029 updateUid(&mCachedUids, uid, false, ActivityManager::PROCESS_STATE_UNKNOWN, true);
Mikhail Naganoveae73eb2018-04-03 16:57:36 -07001030}
1031
Eric Laurente8c8b432018-10-17 10:08:02 -07001032void AudioPolicyService::UidPolicy::onUidStateChanged(uid_t uid,
1033 int32_t procState,
Hui Yu13ad0eb2019-09-09 10:27:07 -07001034 int64_t procStateSeq __unused,
1035 int32_t capability __unused) {
Eric Laurente8c8b432018-10-17 10:08:02 -07001036 if (procState != ActivityManager::PROCESS_STATE_UNKNOWN) {
1037 updateUid(&mCachedUids, uid, true, procState, true);
Mikhail Naganoveae73eb2018-04-03 16:57:36 -07001038 }
1039}
1040
1041void AudioPolicyService::UidPolicy::updateOverrideUid(uid_t uid, bool active, bool insert) {
Eric Laurente8c8b432018-10-17 10:08:02 -07001042 updateUid(&mOverrideUids, uid, active, ActivityManager::PROCESS_STATE_UNKNOWN, insert);
1043}
1044
1045void AudioPolicyService::UidPolicy::notifyService() {
1046 sp<AudioPolicyService> service = mService.promote();
1047 if (service != nullptr) {
1048 service->updateUidStates();
Mikhail Naganoveae73eb2018-04-03 16:57:36 -07001049 }
1050}
1051
Eric Laurente8c8b432018-10-17 10:08:02 -07001052void AudioPolicyService::UidPolicy::updateUid(std::unordered_map<uid_t,
1053 std::pair<bool, int>> *uids,
1054 uid_t uid,
1055 bool active,
1056 int state,
1057 bool insert) {
1058 if (isServiceUid(uid)) {
1059 return;
1060 }
1061 bool wasActive = isUidActive(uid);
1062 int previousState = getUidState(uid);
Mikhail Naganoveae73eb2018-04-03 16:57:36 -07001063 {
1064 Mutex::Autolock _l(mLock);
Eric Laurente8c8b432018-10-17 10:08:02 -07001065 updateUidLocked(uids, uid, active, state, insert);
Mikhail Naganoveae73eb2018-04-03 16:57:36 -07001066 }
Eric Laurente8c8b432018-10-17 10:08:02 -07001067 if (wasActive != isUidActive(uid) || state != previousState) {
1068 notifyService();
1069 }
Mikhail Naganoveae73eb2018-04-03 16:57:36 -07001070}
1071
Eric Laurente8c8b432018-10-17 10:08:02 -07001072void AudioPolicyService::UidPolicy::updateUidLocked(std::unordered_map<uid_t,
1073 std::pair<bool, int>> *uids,
1074 uid_t uid,
1075 bool active,
1076 int state,
1077 bool insert) {
Mikhail Naganoveae73eb2018-04-03 16:57:36 -07001078 auto it = uids->find(uid);
1079 if (it != uids->end()) {
Mikhail Naganoveae73eb2018-04-03 16:57:36 -07001080 if (insert) {
Eric Laurente8c8b432018-10-17 10:08:02 -07001081 if (state == ActivityManager::PROCESS_STATE_UNKNOWN) {
1082 it->second.first = active;
1083 }
1084 if (it->second.first) {
1085 it->second.second = state;
1086 } else {
1087 it->second.second = ActivityManager::PROCESS_STATE_UNKNOWN;
1088 }
Mikhail Naganoveae73eb2018-04-03 16:57:36 -07001089 } else {
1090 uids->erase(it);
1091 }
Eric Laurente8c8b432018-10-17 10:08:02 -07001092 } else if (insert && (state == ActivityManager::PROCESS_STATE_UNKNOWN)) {
1093 uids->insert(std::pair<uid_t, std::pair<bool, int>>(uid,
1094 std::pair<bool, int>(active, state)));
Mikhail Naganoveae73eb2018-04-03 16:57:36 -07001095 }
Svet Ganovf4ddfef2018-01-16 07:37:58 -08001096}
Mathias Agopian65ab4712010-07-14 17:59:35 -07001097
Eric Laurent4eb58f12018-12-07 16:41:02 -08001098bool AudioPolicyService::UidPolicy::isA11yOnTop() {
1099 for (const auto &uid : mCachedUids) {
Eric Laurent47670c92019-08-28 16:59:05 -07001100 if (!isA11yUid(uid.first)) {
Eric Laurent4eb58f12018-12-07 16:41:02 -08001101 continue;
1102 }
Amith Yamasanibcbb3002019-01-23 13:53:33 -08001103 if (uid.second.second >= ActivityManager::PROCESS_STATE_TOP
1104 && uid.second.second <= ActivityManager::PROCESS_STATE_BOUND_FOREGROUND_SERVICE) {
Eric Laurent4eb58f12018-12-07 16:41:02 -08001105 return true;
1106 }
1107 }
1108 return false;
1109}
1110
Eric Laurentb78763e2018-10-17 10:08:02 -07001111bool AudioPolicyService::UidPolicy::isA11yUid(uid_t uid)
1112{
1113 std::vector<uid_t>::iterator it = find(mA11yUids.begin(), mA11yUids.end(), uid);
1114 return it != mA11yUids.end();
1115}
1116
Michael Groovercfd28302018-12-11 19:16:46 -08001117// ----------- AudioPolicyService::SensorPrivacyService implementation ----------
1118void AudioPolicyService::SensorPrivacyPolicy::registerSelf() {
1119 SensorPrivacyManager spm;
1120 mSensorPrivacyEnabled = spm.isSensorPrivacyEnabled();
1121 spm.addSensorPrivacyListener(this);
1122}
1123
1124void AudioPolicyService::SensorPrivacyPolicy::unregisterSelf() {
1125 SensorPrivacyManager spm;
1126 spm.removeSensorPrivacyListener(this);
1127}
1128
1129bool AudioPolicyService::SensorPrivacyPolicy::isSensorPrivacyEnabled() {
1130 return mSensorPrivacyEnabled;
1131}
1132
1133binder::Status AudioPolicyService::SensorPrivacyPolicy::onSensorPrivacyChanged(bool enabled) {
1134 mSensorPrivacyEnabled = enabled;
1135 sp<AudioPolicyService> service = mService.promote();
1136 if (service != nullptr) {
1137 service->updateUidStates();
1138 }
1139 return binder::Status::ok();
1140}
1141
Mathias Agopian65ab4712010-07-14 17:59:35 -07001142// ----------- AudioPolicyService::AudioCommandThread implementation ----------
1143
Eric Laurentbfb1b832013-01-07 09:53:42 -08001144AudioPolicyService::AudioCommandThread::AudioCommandThread(String8 name,
1145 const wp<AudioPolicyService>& service)
1146 : Thread(false), mName(name), mService(service)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001147{
Mathias Agopian65ab4712010-07-14 17:59:35 -07001148}
1149
1150
1151AudioPolicyService::AudioCommandThread::~AudioCommandThread()
1152{
Eric Laurentbfb1b832013-01-07 09:53:42 -08001153 if (!mAudioCommands.isEmpty()) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07001154 release_wake_lock(mName.string());
1155 }
1156 mAudioCommands.clear();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001157}
1158
1159void AudioPolicyService::AudioCommandThread::onFirstRef()
1160{
Eric Laurentbfb1b832013-01-07 09:53:42 -08001161 run(mName.string(), ANDROID_PRIORITY_AUDIO);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001162}
1163
1164bool AudioPolicyService::AudioCommandThread::threadLoop()
1165{
Eric Laurentd7eda8d2016-02-02 17:18:39 -08001166 nsecs_t waitTime = -1;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001167
1168 mLock.lock();
1169 while (!exitPending())
1170 {
Eric Laurent59a89232014-06-08 14:14:17 -07001171 sp<AudioPolicyService> svc;
1172 while (!mAudioCommands.isEmpty() && !exitPending()) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07001173 nsecs_t curTime = systemTime();
1174 // commands are sorted by increasing time stamp: execute them from index 0 and up
1175 if (mAudioCommands[0]->mTime <= curTime) {
Eric Laurent0ede8922014-05-09 18:04:42 -07001176 sp<AudioCommand> command = mAudioCommands[0];
Mathias Agopian65ab4712010-07-14 17:59:35 -07001177 mAudioCommands.removeAt(0);
Eric Laurent0ede8922014-05-09 18:04:42 -07001178 mLastCommand = command;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001179
1180 switch (command->mCommand) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07001181 case SET_VOLUME: {
Eric Laurent0ede8922014-05-09 18:04:42 -07001182 VolumeData *data = (VolumeData *)command->mParam.get();
Steve Block3856b092011-10-20 11:56:00 +01001183 ALOGV("AudioCommandThread() processing set volume stream %d, \
Eric Laurentde070132010-07-13 04:45:46 -07001184 volume %f, output %d", data->mStream, data->mVolume, data->mIO);
Andy Hungfe726a62018-09-27 15:17:25 -07001185 mLock.unlock();
Eric Laurentde070132010-07-13 04:45:46 -07001186 command->mStatus = AudioSystem::setStreamVolume(data->mStream,
1187 data->mVolume,
1188 data->mIO);
Andy Hungfe726a62018-09-27 15:17:25 -07001189 mLock.lock();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001190 }break;
1191 case SET_PARAMETERS: {
Eric Laurent0ede8922014-05-09 18:04:42 -07001192 ParametersData *data = (ParametersData *)command->mParam.get();
Glenn Kastene53b9ea2012-03-12 16:29:55 -07001193 ALOGV("AudioCommandThread() processing set parameters string %s, io %d",
1194 data->mKeyValuePairs.string(), data->mIO);
Andy Hungfe726a62018-09-27 15:17:25 -07001195 mLock.unlock();
Glenn Kastene53b9ea2012-03-12 16:29:55 -07001196 command->mStatus = AudioSystem::setParameters(data->mIO, data->mKeyValuePairs);
Andy Hungfe726a62018-09-27 15:17:25 -07001197 mLock.lock();
Glenn Kastene53b9ea2012-03-12 16:29:55 -07001198 }break;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001199 case SET_VOICE_VOLUME: {
Eric Laurent0ede8922014-05-09 18:04:42 -07001200 VoiceVolumeData *data = (VoiceVolumeData *)command->mParam.get();
Steve Block3856b092011-10-20 11:56:00 +01001201 ALOGV("AudioCommandThread() processing set voice volume volume %f",
Eric Laurentde070132010-07-13 04:45:46 -07001202 data->mVolume);
Andy Hungfe726a62018-09-27 15:17:25 -07001203 mLock.unlock();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001204 command->mStatus = AudioSystem::setVoiceVolume(data->mVolume);
Andy Hungfe726a62018-09-27 15:17:25 -07001205 mLock.lock();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001206 }break;
Eric Laurentbfb1b832013-01-07 09:53:42 -08001207 case STOP_OUTPUT: {
Eric Laurent0ede8922014-05-09 18:04:42 -07001208 StopOutputData *data = (StopOutputData *)command->mParam.get();
Eric Laurentd7fe0862018-07-14 16:48:01 -07001209 ALOGV("AudioCommandThread() processing stop output portId %d",
1210 data->mPortId);
Eric Laurent59a89232014-06-08 14:14:17 -07001211 svc = mService.promote();
Eric Laurentbfb1b832013-01-07 09:53:42 -08001212 if (svc == 0) {
1213 break;
1214 }
1215 mLock.unlock();
Eric Laurentd7fe0862018-07-14 16:48:01 -07001216 svc->doStopOutput(data->mPortId);
Eric Laurentbfb1b832013-01-07 09:53:42 -08001217 mLock.lock();
Eric Laurentbfb1b832013-01-07 09:53:42 -08001218 }break;
1219 case RELEASE_OUTPUT: {
Eric Laurent0ede8922014-05-09 18:04:42 -07001220 ReleaseOutputData *data = (ReleaseOutputData *)command->mParam.get();
Eric Laurentd7fe0862018-07-14 16:48:01 -07001221 ALOGV("AudioCommandThread() processing release output portId %d",
1222 data->mPortId);
Eric Laurent59a89232014-06-08 14:14:17 -07001223 svc = mService.promote();
Eric Laurentbfb1b832013-01-07 09:53:42 -08001224 if (svc == 0) {
1225 break;
1226 }
1227 mLock.unlock();
Eric Laurentd7fe0862018-07-14 16:48:01 -07001228 svc->doReleaseOutput(data->mPortId);
Eric Laurentbfb1b832013-01-07 09:53:42 -08001229 mLock.lock();
Eric Laurentbfb1b832013-01-07 09:53:42 -08001230 }break;
Eric Laurent951f4552014-05-20 10:48:17 -07001231 case CREATE_AUDIO_PATCH: {
1232 CreateAudioPatchData *data = (CreateAudioPatchData *)command->mParam.get();
1233 ALOGV("AudioCommandThread() processing create audio patch");
1234 sp<IAudioFlinger> af = AudioSystem::get_audio_flinger();
1235 if (af == 0) {
1236 command->mStatus = PERMISSION_DENIED;
1237 } else {
Andy Hungfe726a62018-09-27 15:17:25 -07001238 mLock.unlock();
Eric Laurent951f4552014-05-20 10:48:17 -07001239 command->mStatus = af->createAudioPatch(&data->mPatch, &data->mHandle);
Andy Hungfe726a62018-09-27 15:17:25 -07001240 mLock.lock();
Eric Laurent951f4552014-05-20 10:48:17 -07001241 }
1242 } break;
1243 case RELEASE_AUDIO_PATCH: {
1244 ReleaseAudioPatchData *data = (ReleaseAudioPatchData *)command->mParam.get();
1245 ALOGV("AudioCommandThread() processing release audio patch");
1246 sp<IAudioFlinger> af = AudioSystem::get_audio_flinger();
1247 if (af == 0) {
1248 command->mStatus = PERMISSION_DENIED;
1249 } else {
Andy Hungfe726a62018-09-27 15:17:25 -07001250 mLock.unlock();
Eric Laurent951f4552014-05-20 10:48:17 -07001251 command->mStatus = af->releaseAudioPatch(data->mHandle);
Andy Hungfe726a62018-09-27 15:17:25 -07001252 mLock.lock();
Eric Laurent951f4552014-05-20 10:48:17 -07001253 }
1254 } break;
Eric Laurentb52c1522014-05-20 11:27:36 -07001255 case UPDATE_AUDIOPORT_LIST: {
1256 ALOGV("AudioCommandThread() processing update audio port list");
Eric Laurent59a89232014-06-08 14:14:17 -07001257 svc = mService.promote();
Eric Laurentb52c1522014-05-20 11:27:36 -07001258 if (svc == 0) {
1259 break;
1260 }
1261 mLock.unlock();
1262 svc->doOnAudioPortListUpdate();
1263 mLock.lock();
1264 }break;
1265 case UPDATE_AUDIOPATCH_LIST: {
1266 ALOGV("AudioCommandThread() processing update audio patch list");
Eric Laurent59a89232014-06-08 14:14:17 -07001267 svc = mService.promote();
Eric Laurentb52c1522014-05-20 11:27:36 -07001268 if (svc == 0) {
1269 break;
1270 }
1271 mLock.unlock();
1272 svc->doOnAudioPatchListUpdate();
1273 mLock.lock();
1274 }break;
François Gaffiecfe17322018-11-07 13:41:29 +01001275 case CHANGED_AUDIOVOLUMEGROUP: {
1276 AudioVolumeGroupData *data =
1277 static_cast<AudioVolumeGroupData *>(command->mParam.get());
1278 ALOGV("AudioCommandThread() processing update audio volume group");
1279 svc = mService.promote();
1280 if (svc == 0) {
1281 break;
1282 }
1283 mLock.unlock();
1284 svc->doOnAudioVolumeGroupChanged(data->mGroup, data->mFlags);
1285 mLock.lock();
1286 }break;
Eric Laurente1715a42014-05-20 11:30:42 -07001287 case SET_AUDIOPORT_CONFIG: {
1288 SetAudioPortConfigData *data = (SetAudioPortConfigData *)command->mParam.get();
1289 ALOGV("AudioCommandThread() processing set port config");
1290 sp<IAudioFlinger> af = AudioSystem::get_audio_flinger();
1291 if (af == 0) {
1292 command->mStatus = PERMISSION_DENIED;
1293 } else {
Andy Hungfe726a62018-09-27 15:17:25 -07001294 mLock.unlock();
Eric Laurente1715a42014-05-20 11:30:42 -07001295 command->mStatus = af->setAudioPortConfig(&data->mConfig);
Andy Hungfe726a62018-09-27 15:17:25 -07001296 mLock.lock();
Eric Laurente1715a42014-05-20 11:30:42 -07001297 }
1298 } break;
Jean-Michel Trivide801052015-04-14 19:10:14 -07001299 case DYN_POLICY_MIX_STATE_UPDATE: {
1300 DynPolicyMixStateUpdateData *data =
1301 (DynPolicyMixStateUpdateData *)command->mParam.get();
Jean-Michel Trivide801052015-04-14 19:10:14 -07001302 ALOGV("AudioCommandThread() processing dyn policy mix state update %s %d",
1303 data->mRegId.string(), data->mState);
1304 svc = mService.promote();
1305 if (svc == 0) {
1306 break;
1307 }
1308 mLock.unlock();
1309 svc->doOnDynamicPolicyMixStateUpdate(data->mRegId, data->mState);
1310 mLock.lock();
1311 } break;
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -08001312 case RECORDING_CONFIGURATION_UPDATE: {
1313 RecordingConfigurationUpdateData *data =
1314 (RecordingConfigurationUpdateData *)command->mParam.get();
1315 ALOGV("AudioCommandThread() processing recording configuration update");
1316 svc = mService.promote();
1317 if (svc == 0) {
1318 break;
1319 }
1320 mLock.unlock();
Jean-Michel Triviac4e4292016-12-22 11:39:31 -08001321 svc->doOnRecordingConfigurationUpdate(data->mEvent, &data->mClientInfo,
Eric Laurenta9f86652018-11-28 17:23:11 -08001322 &data->mClientConfig, data->mClientEffects,
1323 &data->mDeviceConfig, data->mEffects,
1324 data->mPatchHandle, data->mSource);
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -08001325 mLock.lock();
1326 } break;
Eric Laurentb20cf7d2019-04-05 19:37:34 -07001327 case SET_EFFECT_SUSPENDED: {
1328 SetEffectSuspendedData *data = (SetEffectSuspendedData *)command->mParam.get();
1329 ALOGV("AudioCommandThread() processing set effect suspended");
1330 sp<IAudioFlinger> af = AudioSystem::get_audio_flinger();
1331 if (af != 0) {
1332 mLock.unlock();
1333 af->setEffectSuspended(data->mEffectId, data->mSessionId, data->mSuspended);
1334 mLock.lock();
1335 }
1336 } break;
Mikhail Naganov88b30d22020-03-09 19:43:13 +00001337 case AUDIO_MODULES_UPDATE: {
1338 ALOGV("AudioCommandThread() processing audio modules update");
1339 svc = mService.promote();
1340 if (svc == 0) {
1341 break;
1342 }
1343 mLock.unlock();
1344 svc->doOnNewAudioModulesAvailable();
1345 mLock.lock();
1346 } break;
Eric Laurentb20cf7d2019-04-05 19:37:34 -07001347
Mathias Agopian65ab4712010-07-14 17:59:35 -07001348 default:
Steve Block5ff1dd52012-01-05 23:22:43 +00001349 ALOGW("AudioCommandThread() unknown command %d", command->mCommand);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001350 }
Eric Laurent0ede8922014-05-09 18:04:42 -07001351 {
1352 Mutex::Autolock _l(command->mLock);
1353 if (command->mWaitStatus) {
1354 command->mWaitStatus = false;
1355 command->mCond.signal();
1356 }
1357 }
Eric Laurentd7eda8d2016-02-02 17:18:39 -08001358 waitTime = -1;
Zach Janga754b4f2015-10-27 01:29:34 +00001359 // release mLock before releasing strong reference on the service as
1360 // AudioPolicyService destructor calls AudioCommandThread::exit() which
1361 // acquires mLock.
1362 mLock.unlock();
1363 svc.clear();
1364 mLock.lock();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001365 } else {
1366 waitTime = mAudioCommands[0]->mTime - curTime;
1367 break;
1368 }
1369 }
Zach Janga754b4f2015-10-27 01:29:34 +00001370
1371 // release delayed commands wake lock if the queue is empty
1372 if (mAudioCommands.isEmpty()) {
Ricardo Garcia05f2fdc2014-07-24 15:48:24 -07001373 release_wake_lock(mName.string());
Zach Janga754b4f2015-10-27 01:29:34 +00001374 }
1375
1376 // At this stage we have either an empty command queue or the first command in the queue
1377 // has a finite delay. So unless we are exiting it is safe to wait.
1378 if (!exitPending()) {
Eric Laurent59a89232014-06-08 14:14:17 -07001379 ALOGV("AudioCommandThread() going to sleep");
Eric Laurentd7eda8d2016-02-02 17:18:39 -08001380 if (waitTime == -1) {
1381 mWaitWorkCV.wait(mLock);
1382 } else {
1383 mWaitWorkCV.waitRelative(mLock, waitTime);
1384 }
Eric Laurent59a89232014-06-08 14:14:17 -07001385 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001386 }
Ricardo Garcia05f2fdc2014-07-24 15:48:24 -07001387 // release delayed commands wake lock before quitting
1388 if (!mAudioCommands.isEmpty()) {
1389 release_wake_lock(mName.string());
1390 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001391 mLock.unlock();
1392 return false;
1393}
1394
1395status_t AudioPolicyService::AudioCommandThread::dump(int fd)
1396{
1397 const size_t SIZE = 256;
1398 char buffer[SIZE];
1399 String8 result;
1400
1401 snprintf(buffer, SIZE, "AudioCommandThread %p Dump\n", this);
1402 result.append(buffer);
1403 write(fd, result.string(), result.size());
1404
Mikhail Naganov959e2d02019-03-28 11:08:19 -07001405 bool locked = dumpTryLock(mLock);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001406 if (!locked) {
1407 String8 result2(kCmdDeadlockedString);
1408 write(fd, result2.string(), result2.size());
1409 }
1410
1411 snprintf(buffer, SIZE, "- Commands:\n");
1412 result = String8(buffer);
1413 result.append(" Command Time Wait pParam\n");
Glenn Kasten8d6a2442012-02-08 14:04:28 -08001414 for (size_t i = 0; i < mAudioCommands.size(); i++) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07001415 mAudioCommands[i]->dump(buffer, SIZE);
1416 result.append(buffer);
1417 }
1418 result.append(" Last Command\n");
Eric Laurent0ede8922014-05-09 18:04:42 -07001419 if (mLastCommand != 0) {
1420 mLastCommand->dump(buffer, SIZE);
1421 result.append(buffer);
1422 } else {
1423 result.append(" none\n");
1424 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001425
1426 write(fd, result.string(), result.size());
1427
1428 if (locked) mLock.unlock();
1429
1430 return NO_ERROR;
1431}
1432
Glenn Kastenfff6d712012-01-12 16:38:12 -08001433status_t AudioPolicyService::AudioCommandThread::volumeCommand(audio_stream_type_t stream,
Eric Laurentde070132010-07-13 04:45:46 -07001434 float volume,
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001435 audio_io_handle_t output,
Eric Laurentde070132010-07-13 04:45:46 -07001436 int delayMs)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001437{
Eric Laurent0ede8922014-05-09 18:04:42 -07001438 sp<AudioCommand> command = new AudioCommand();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001439 command->mCommand = SET_VOLUME;
Eric Laurent0ede8922014-05-09 18:04:42 -07001440 sp<VolumeData> data = new VolumeData();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001441 data->mStream = stream;
1442 data->mVolume = volume;
1443 data->mIO = output;
1444 command->mParam = data;
Eric Laurent0ede8922014-05-09 18:04:42 -07001445 command->mWaitStatus = true;
Steve Block3856b092011-10-20 11:56:00 +01001446 ALOGV("AudioCommandThread() adding set volume stream %d, volume %f, output %d",
Eric Laurentde070132010-07-13 04:45:46 -07001447 stream, volume, output);
Eric Laurent0ede8922014-05-09 18:04:42 -07001448 return sendCommand(command, delayMs);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001449}
1450
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001451status_t AudioPolicyService::AudioCommandThread::parametersCommand(audio_io_handle_t ioHandle,
Dima Zavinfce7a472011-04-19 22:30:36 -07001452 const char *keyValuePairs,
Eric Laurentde070132010-07-13 04:45:46 -07001453 int delayMs)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001454{
Eric Laurent0ede8922014-05-09 18:04:42 -07001455 sp<AudioCommand> command = new AudioCommand();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001456 command->mCommand = SET_PARAMETERS;
Eric Laurent0ede8922014-05-09 18:04:42 -07001457 sp<ParametersData> data = new ParametersData();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001458 data->mIO = ioHandle;
Dima Zavinfce7a472011-04-19 22:30:36 -07001459 data->mKeyValuePairs = String8(keyValuePairs);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001460 command->mParam = data;
Eric Laurent0ede8922014-05-09 18:04:42 -07001461 command->mWaitStatus = true;
Steve Block3856b092011-10-20 11:56:00 +01001462 ALOGV("AudioCommandThread() adding set parameter string %s, io %d ,delay %d",
Dima Zavinfce7a472011-04-19 22:30:36 -07001463 keyValuePairs, ioHandle, delayMs);
Eric Laurent0ede8922014-05-09 18:04:42 -07001464 return sendCommand(command, delayMs);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001465}
1466
1467status_t AudioPolicyService::AudioCommandThread::voiceVolumeCommand(float volume, int delayMs)
1468{
Eric Laurent0ede8922014-05-09 18:04:42 -07001469 sp<AudioCommand> command = new AudioCommand();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001470 command->mCommand = SET_VOICE_VOLUME;
Eric Laurent0ede8922014-05-09 18:04:42 -07001471 sp<VoiceVolumeData> data = new VoiceVolumeData();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001472 data->mVolume = volume;
1473 command->mParam = data;
Eric Laurent0ede8922014-05-09 18:04:42 -07001474 command->mWaitStatus = true;
Steve Block3856b092011-10-20 11:56:00 +01001475 ALOGV("AudioCommandThread() adding set voice volume volume %f", volume);
Eric Laurent0ede8922014-05-09 18:04:42 -07001476 return sendCommand(command, delayMs);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001477}
1478
Eric Laurentb20cf7d2019-04-05 19:37:34 -07001479void AudioPolicyService::AudioCommandThread::setEffectSuspendedCommand(int effectId,
1480 audio_session_t sessionId,
1481 bool suspended)
1482{
1483 sp<AudioCommand> command = new AudioCommand();
1484 command->mCommand = SET_EFFECT_SUSPENDED;
1485 sp<SetEffectSuspendedData> data = new SetEffectSuspendedData();
1486 data->mEffectId = effectId;
1487 data->mSessionId = sessionId;
1488 data->mSuspended = suspended;
1489 command->mParam = data;
1490 ALOGV("AudioCommandThread() adding set suspended effectId %d sessionId %d suspended %d",
1491 effectId, sessionId, suspended);
1492 sendCommand(command);
1493}
1494
1495
Eric Laurentd7fe0862018-07-14 16:48:01 -07001496void AudioPolicyService::AudioCommandThread::stopOutputCommand(audio_port_handle_t portId)
Eric Laurentbfb1b832013-01-07 09:53:42 -08001497{
Eric Laurent0ede8922014-05-09 18:04:42 -07001498 sp<AudioCommand> command = new AudioCommand();
Eric Laurentbfb1b832013-01-07 09:53:42 -08001499 command->mCommand = STOP_OUTPUT;
Eric Laurent0ede8922014-05-09 18:04:42 -07001500 sp<StopOutputData> data = new StopOutputData();
Eric Laurentd7fe0862018-07-14 16:48:01 -07001501 data->mPortId = portId;
Jesper Tragardh48412dc2014-03-24 14:12:43 +01001502 command->mParam = data;
Eric Laurentd7fe0862018-07-14 16:48:01 -07001503 ALOGV("AudioCommandThread() adding stop output portId %d", portId);
Eric Laurent0ede8922014-05-09 18:04:42 -07001504 sendCommand(command);
Eric Laurentbfb1b832013-01-07 09:53:42 -08001505}
1506
Eric Laurentd7fe0862018-07-14 16:48:01 -07001507void AudioPolicyService::AudioCommandThread::releaseOutputCommand(audio_port_handle_t portId)
Eric Laurentbfb1b832013-01-07 09:53:42 -08001508{
Eric Laurent0ede8922014-05-09 18:04:42 -07001509 sp<AudioCommand> command = new AudioCommand();
Eric Laurentbfb1b832013-01-07 09:53:42 -08001510 command->mCommand = RELEASE_OUTPUT;
Eric Laurent0ede8922014-05-09 18:04:42 -07001511 sp<ReleaseOutputData> data = new ReleaseOutputData();
Eric Laurentd7fe0862018-07-14 16:48:01 -07001512 data->mPortId = portId;
Jesper Tragardh48412dc2014-03-24 14:12:43 +01001513 command->mParam = data;
Eric Laurentd7fe0862018-07-14 16:48:01 -07001514 ALOGV("AudioCommandThread() adding release output portId %d", portId);
Eric Laurent0ede8922014-05-09 18:04:42 -07001515 sendCommand(command);
1516}
1517
Eric Laurent951f4552014-05-20 10:48:17 -07001518status_t AudioPolicyService::AudioCommandThread::createAudioPatchCommand(
1519 const struct audio_patch *patch,
1520 audio_patch_handle_t *handle,
1521 int delayMs)
1522{
1523 status_t status = NO_ERROR;
1524
1525 sp<AudioCommand> command = new AudioCommand();
1526 command->mCommand = CREATE_AUDIO_PATCH;
1527 CreateAudioPatchData *data = new CreateAudioPatchData();
1528 data->mPatch = *patch;
1529 data->mHandle = *handle;
1530 command->mParam = data;
1531 command->mWaitStatus = true;
1532 ALOGV("AudioCommandThread() adding create patch delay %d", delayMs);
1533 status = sendCommand(command, delayMs);
1534 if (status == NO_ERROR) {
1535 *handle = data->mHandle;
1536 }
1537 return status;
1538}
1539
1540status_t AudioPolicyService::AudioCommandThread::releaseAudioPatchCommand(audio_patch_handle_t handle,
1541 int delayMs)
1542{
1543 sp<AudioCommand> command = new AudioCommand();
1544 command->mCommand = RELEASE_AUDIO_PATCH;
1545 ReleaseAudioPatchData *data = new ReleaseAudioPatchData();
1546 data->mHandle = handle;
1547 command->mParam = data;
1548 command->mWaitStatus = true;
1549 ALOGV("AudioCommandThread() adding release patch delay %d", delayMs);
1550 return sendCommand(command, delayMs);
1551}
1552
Eric Laurentb52c1522014-05-20 11:27:36 -07001553void AudioPolicyService::AudioCommandThread::updateAudioPortListCommand()
1554{
1555 sp<AudioCommand> command = new AudioCommand();
1556 command->mCommand = UPDATE_AUDIOPORT_LIST;
1557 ALOGV("AudioCommandThread() adding update audio port list");
1558 sendCommand(command);
1559}
1560
1561void AudioPolicyService::AudioCommandThread::updateAudioPatchListCommand()
1562{
1563 sp<AudioCommand>command = new AudioCommand();
1564 command->mCommand = UPDATE_AUDIOPATCH_LIST;
1565 ALOGV("AudioCommandThread() adding update audio patch list");
1566 sendCommand(command);
1567}
1568
François Gaffiecfe17322018-11-07 13:41:29 +01001569void AudioPolicyService::AudioCommandThread::changeAudioVolumeGroupCommand(volume_group_t group,
1570 int flags)
1571{
1572 sp<AudioCommand>command = new AudioCommand();
1573 command->mCommand = CHANGED_AUDIOVOLUMEGROUP;
1574 AudioVolumeGroupData *data= new AudioVolumeGroupData();
1575 data->mGroup = group;
1576 data->mFlags = flags;
1577 command->mParam = data;
1578 ALOGV("AudioCommandThread() adding audio volume group changed");
1579 sendCommand(command);
1580}
1581
Eric Laurente1715a42014-05-20 11:30:42 -07001582status_t AudioPolicyService::AudioCommandThread::setAudioPortConfigCommand(
1583 const struct audio_port_config *config, int delayMs)
1584{
1585 sp<AudioCommand> command = new AudioCommand();
1586 command->mCommand = SET_AUDIOPORT_CONFIG;
1587 SetAudioPortConfigData *data = new SetAudioPortConfigData();
1588 data->mConfig = *config;
1589 command->mParam = data;
1590 command->mWaitStatus = true;
1591 ALOGV("AudioCommandThread() adding set port config delay %d", delayMs);
1592 return sendCommand(command, delayMs);
1593}
1594
Jean-Michel Trivide801052015-04-14 19:10:14 -07001595void AudioPolicyService::AudioCommandThread::dynamicPolicyMixStateUpdateCommand(
Chih-Hung Hsiehe964d4e2016-08-09 14:31:32 -07001596 const String8& regId, int32_t state)
Jean-Michel Trivide801052015-04-14 19:10:14 -07001597{
1598 sp<AudioCommand> command = new AudioCommand();
1599 command->mCommand = DYN_POLICY_MIX_STATE_UPDATE;
1600 DynPolicyMixStateUpdateData *data = new DynPolicyMixStateUpdateData();
1601 data->mRegId = regId;
1602 data->mState = state;
1603 command->mParam = data;
1604 ALOGV("AudioCommandThread() sending dynamic policy mix (id=%s) state update to %d",
1605 regId.string(), state);
1606 sendCommand(command);
1607}
1608
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -08001609void AudioPolicyService::AudioCommandThread::recordingConfigurationUpdateCommand(
Eric Laurenta9f86652018-11-28 17:23:11 -08001610 int event,
1611 const record_client_info_t *clientInfo,
1612 const audio_config_base_t *clientConfig,
1613 std::vector<effect_descriptor_t> clientEffects,
1614 const audio_config_base_t *deviceConfig,
1615 std::vector<effect_descriptor_t> effects,
1616 audio_patch_handle_t patchHandle,
1617 audio_source_t source)
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -08001618{
1619 sp<AudioCommand>command = new AudioCommand();
1620 command->mCommand = RECORDING_CONFIGURATION_UPDATE;
1621 RecordingConfigurationUpdateData *data = new RecordingConfigurationUpdateData();
1622 data->mEvent = event;
Jean-Michel Triviac4e4292016-12-22 11:39:31 -08001623 data->mClientInfo = *clientInfo;
Jean-Michel Trivi7281aa92016-02-17 15:33:40 -08001624 data->mClientConfig = *clientConfig;
Eric Laurenta9f86652018-11-28 17:23:11 -08001625 data->mClientEffects = clientEffects;
Jean-Michel Trivi7281aa92016-02-17 15:33:40 -08001626 data->mDeviceConfig = *deviceConfig;
Eric Laurenta9f86652018-11-28 17:23:11 -08001627 data->mEffects = effects;
Jean-Michel Trivi8c7cf3b2016-02-25 17:08:24 -08001628 data->mPatchHandle = patchHandle;
Eric Laurenta9f86652018-11-28 17:23:11 -08001629 data->mSource = source;
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -08001630 command->mParam = data;
Jean-Michel Triviac4e4292016-12-22 11:39:31 -08001631 ALOGV("AudioCommandThread() adding recording configuration update event %d, source %d uid %u",
1632 event, clientInfo->source, clientInfo->uid);
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -08001633 sendCommand(command);
1634}
1635
Mikhail Naganov88b30d22020-03-09 19:43:13 +00001636void AudioPolicyService::AudioCommandThread::audioModulesUpdateCommand()
1637{
1638 sp<AudioCommand> command = new AudioCommand();
1639 command->mCommand = AUDIO_MODULES_UPDATE;
1640 sendCommand(command);
1641}
1642
Eric Laurent0ede8922014-05-09 18:04:42 -07001643status_t AudioPolicyService::AudioCommandThread::sendCommand(sp<AudioCommand>& command, int delayMs)
1644{
1645 {
1646 Mutex::Autolock _l(mLock);
1647 insertCommand_l(command, delayMs);
1648 mWaitWorkCV.signal();
1649 }
1650 Mutex::Autolock _l(command->mLock);
1651 while (command->mWaitStatus) {
1652 nsecs_t timeOutNs = kAudioCommandTimeoutNs + milliseconds(delayMs);
1653 if (command->mCond.waitRelative(command->mLock, timeOutNs) != NO_ERROR) {
1654 command->mStatus = TIMED_OUT;
1655 command->mWaitStatus = false;
1656 }
1657 }
1658 return command->mStatus;
Eric Laurentbfb1b832013-01-07 09:53:42 -08001659}
1660
Mathias Agopian65ab4712010-07-14 17:59:35 -07001661// insertCommand_l() must be called with mLock held
Eric Laurent0ede8922014-05-09 18:04:42 -07001662void AudioPolicyService::AudioCommandThread::insertCommand_l(sp<AudioCommand>& command, int delayMs)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001663{
Glenn Kasten8d6a2442012-02-08 14:04:28 -08001664 ssize_t i; // not size_t because i will count down to -1
Eric Laurent0ede8922014-05-09 18:04:42 -07001665 Vector < sp<AudioCommand> > removedCommands;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001666 command->mTime = systemTime() + milliseconds(delayMs);
1667
1668 // acquire wake lock to make sure delayed commands are processed
Eric Laurentbfb1b832013-01-07 09:53:42 -08001669 if (mAudioCommands.isEmpty()) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07001670 acquire_wake_lock(PARTIAL_WAKE_LOCK, mName.string());
1671 }
1672
1673 // check same pending commands with later time stamps and eliminate them
Ivan Lozano5ff158f2017-10-30 09:06:24 -07001674 for (i = (ssize_t)mAudioCommands.size()-1; i >= 0; i--) {
Eric Laurent0ede8922014-05-09 18:04:42 -07001675 sp<AudioCommand> command2 = mAudioCommands[i];
Mathias Agopian65ab4712010-07-14 17:59:35 -07001676 // commands are sorted by increasing time stamp: no need to scan the rest of mAudioCommands
1677 if (command2->mTime <= command->mTime) break;
Eric Laurente45b48a2014-09-04 16:40:57 -07001678
1679 // create audio patch or release audio patch commands are equivalent
1680 // with regard to filtering
1681 if ((command->mCommand == CREATE_AUDIO_PATCH) ||
1682 (command->mCommand == RELEASE_AUDIO_PATCH)) {
1683 if ((command2->mCommand != CREATE_AUDIO_PATCH) &&
1684 (command2->mCommand != RELEASE_AUDIO_PATCH)) {
1685 continue;
1686 }
1687 } else if (command2->mCommand != command->mCommand) continue;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001688
1689 switch (command->mCommand) {
1690 case SET_PARAMETERS: {
Eric Laurent0ede8922014-05-09 18:04:42 -07001691 ParametersData *data = (ParametersData *)command->mParam.get();
1692 ParametersData *data2 = (ParametersData *)command2->mParam.get();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001693 if (data->mIO != data2->mIO) break;
Steve Block3856b092011-10-20 11:56:00 +01001694 ALOGV("Comparing parameter command %s to new command %s",
Eric Laurentde070132010-07-13 04:45:46 -07001695 data2->mKeyValuePairs.string(), data->mKeyValuePairs.string());
Mathias Agopian65ab4712010-07-14 17:59:35 -07001696 AudioParameter param = AudioParameter(data->mKeyValuePairs);
1697 AudioParameter param2 = AudioParameter(data2->mKeyValuePairs);
1698 for (size_t j = 0; j < param.size(); j++) {
Glenn Kastene53b9ea2012-03-12 16:29:55 -07001699 String8 key;
1700 String8 value;
1701 param.getAt(j, key, value);
1702 for (size_t k = 0; k < param2.size(); k++) {
1703 String8 key2;
1704 String8 value2;
1705 param2.getAt(k, key2, value2);
1706 if (key2 == key) {
1707 param2.remove(key2);
1708 ALOGV("Filtering out parameter %s", key2.string());
1709 break;
1710 }
1711 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001712 }
1713 // if all keys have been filtered out, remove the command.
1714 // otherwise, update the key value pairs
1715 if (param2.size() == 0) {
1716 removedCommands.add(command2);
1717 } else {
1718 data2->mKeyValuePairs = param2.toString();
1719 }
Eric Laurent21e54562013-09-23 12:08:05 -07001720 command->mTime = command2->mTime;
1721 // force delayMs to non 0 so that code below does not request to wait for
1722 // command status as the command is now delayed
1723 delayMs = 1;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001724 } break;
1725
1726 case SET_VOLUME: {
Eric Laurent0ede8922014-05-09 18:04:42 -07001727 VolumeData *data = (VolumeData *)command->mParam.get();
1728 VolumeData *data2 = (VolumeData *)command2->mParam.get();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001729 if (data->mIO != data2->mIO) break;
1730 if (data->mStream != data2->mStream) break;
Steve Block3856b092011-10-20 11:56:00 +01001731 ALOGV("Filtering out volume command on output %d for stream %d",
Eric Laurentde070132010-07-13 04:45:46 -07001732 data->mIO, data->mStream);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001733 removedCommands.add(command2);
Eric Laurent21e54562013-09-23 12:08:05 -07001734 command->mTime = command2->mTime;
1735 // force delayMs to non 0 so that code below does not request to wait for
1736 // command status as the command is now delayed
1737 delayMs = 1;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001738 } break;
Eric Laurente45b48a2014-09-04 16:40:57 -07001739
Eric Laurentbaf35fe2016-07-27 15:36:53 -07001740 case SET_VOICE_VOLUME: {
1741 VoiceVolumeData *data = (VoiceVolumeData *)command->mParam.get();
1742 VoiceVolumeData *data2 = (VoiceVolumeData *)command2->mParam.get();
1743 ALOGV("Filtering out voice volume command value %f replaced by %f",
1744 data2->mVolume, data->mVolume);
1745 removedCommands.add(command2);
1746 command->mTime = command2->mTime;
1747 // force delayMs to non 0 so that code below does not request to wait for
1748 // command status as the command is now delayed
1749 delayMs = 1;
1750 } break;
1751
Eric Laurente45b48a2014-09-04 16:40:57 -07001752 case CREATE_AUDIO_PATCH:
1753 case RELEASE_AUDIO_PATCH: {
1754 audio_patch_handle_t handle;
Haynes Mathew Georgea2d4a6d2014-10-13 13:05:22 -07001755 struct audio_patch patch;
Eric Laurente45b48a2014-09-04 16:40:57 -07001756 if (command->mCommand == CREATE_AUDIO_PATCH) {
1757 handle = ((CreateAudioPatchData *)command->mParam.get())->mHandle;
Haynes Mathew Georgea2d4a6d2014-10-13 13:05:22 -07001758 patch = ((CreateAudioPatchData *)command->mParam.get())->mPatch;
Eric Laurente45b48a2014-09-04 16:40:57 -07001759 } else {
1760 handle = ((ReleaseAudioPatchData *)command->mParam.get())->mHandle;
Mikhail Naganov7be71d22018-05-23 16:51:46 -07001761 memset(&patch, 0, sizeof(patch));
Eric Laurente45b48a2014-09-04 16:40:57 -07001762 }
1763 audio_patch_handle_t handle2;
Haynes Mathew Georgea2d4a6d2014-10-13 13:05:22 -07001764 struct audio_patch patch2;
Eric Laurente45b48a2014-09-04 16:40:57 -07001765 if (command2->mCommand == CREATE_AUDIO_PATCH) {
1766 handle2 = ((CreateAudioPatchData *)command2->mParam.get())->mHandle;
Haynes Mathew Georgea2d4a6d2014-10-13 13:05:22 -07001767 patch2 = ((CreateAudioPatchData *)command2->mParam.get())->mPatch;
Eric Laurente45b48a2014-09-04 16:40:57 -07001768 } else {
1769 handle2 = ((ReleaseAudioPatchData *)command2->mParam.get())->mHandle;
Glenn Kastenf60b6b62015-07-06 10:53:26 -07001770 memset(&patch2, 0, sizeof(patch2));
Eric Laurente45b48a2014-09-04 16:40:57 -07001771 }
1772 if (handle != handle2) break;
Haynes Mathew Georgea2d4a6d2014-10-13 13:05:22 -07001773 /* Filter CREATE_AUDIO_PATCH commands only when they are issued for
1774 same output. */
1775 if( (command->mCommand == CREATE_AUDIO_PATCH) &&
1776 (command2->mCommand == CREATE_AUDIO_PATCH) ) {
1777 bool isOutputDiff = false;
1778 if (patch.num_sources == patch2.num_sources) {
1779 for (unsigned count = 0; count < patch.num_sources; count++) {
1780 if (patch.sources[count].id != patch2.sources[count].id) {
1781 isOutputDiff = true;
1782 break;
1783 }
1784 }
1785 if (isOutputDiff)
1786 break;
1787 }
1788 }
Eric Laurente45b48a2014-09-04 16:40:57 -07001789 ALOGV("Filtering out %s audio patch command for handle %d",
1790 (command->mCommand == CREATE_AUDIO_PATCH) ? "create" : "release", handle);
1791 removedCommands.add(command2);
1792 command->mTime = command2->mTime;
1793 // force delayMs to non 0 so that code below does not request to wait for
1794 // command status as the command is now delayed
1795 delayMs = 1;
1796 } break;
1797
Jean-Michel Trivide801052015-04-14 19:10:14 -07001798 case DYN_POLICY_MIX_STATE_UPDATE: {
1799
1800 } break;
1801
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -08001802 case RECORDING_CONFIGURATION_UPDATE: {
1803
1804 } break;
1805
Mathias Agopian65ab4712010-07-14 17:59:35 -07001806 default:
1807 break;
1808 }
1809 }
1810
1811 // remove filtered commands
1812 for (size_t j = 0; j < removedCommands.size(); j++) {
1813 // removed commands always have time stamps greater than current command
1814 for (size_t k = i + 1; k < mAudioCommands.size(); k++) {
Eric Laurent0ede8922014-05-09 18:04:42 -07001815 if (mAudioCommands[k].get() == removedCommands[j].get()) {
Steve Block3856b092011-10-20 11:56:00 +01001816 ALOGV("suppressing command: %d", mAudioCommands[k]->mCommand);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001817 mAudioCommands.removeAt(k);
1818 break;
1819 }
1820 }
1821 }
1822 removedCommands.clear();
1823
Eric Laurentaa79bef2015-01-15 14:33:51 -08001824 // Disable wait for status if delay is not 0.
1825 // Except for create audio patch command because the returned patch handle
1826 // is needed by audio policy manager
1827 if (delayMs != 0 && command->mCommand != CREATE_AUDIO_PATCH) {
Eric Laurentcec4abb2012-07-03 12:23:02 -07001828 command->mWaitStatus = false;
1829 }
Eric Laurentcec4abb2012-07-03 12:23:02 -07001830
Mathias Agopian65ab4712010-07-14 17:59:35 -07001831 // insert command at the right place according to its time stamp
Eric Laurent1e693b52014-07-09 15:03:28 -07001832 ALOGV("inserting command: %d at index %zd, num commands %zu",
1833 command->mCommand, i+1, mAudioCommands.size());
Mathias Agopian65ab4712010-07-14 17:59:35 -07001834 mAudioCommands.insertAt(command, i + 1);
1835}
1836
1837void AudioPolicyService::AudioCommandThread::exit()
1838{
Steve Block3856b092011-10-20 11:56:00 +01001839 ALOGV("AudioCommandThread::exit");
Mathias Agopian65ab4712010-07-14 17:59:35 -07001840 {
1841 AutoMutex _l(mLock);
1842 requestExit();
1843 mWaitWorkCV.signal();
1844 }
Zach Janga754b4f2015-10-27 01:29:34 +00001845 // Note that we can call it from the thread loop if all other references have been released
1846 // but it will safely return WOULD_BLOCK in this case
Mathias Agopian65ab4712010-07-14 17:59:35 -07001847 requestExitAndWait();
1848}
1849
1850void AudioPolicyService::AudioCommandThread::AudioCommand::dump(char* buffer, size_t size)
1851{
1852 snprintf(buffer, size, " %02d %06d.%03d %01u %p\n",
1853 mCommand,
1854 (int)ns2s(mTime),
1855 (int)ns2ms(mTime)%1000,
1856 mWaitStatus,
Eric Laurent0ede8922014-05-09 18:04:42 -07001857 mParam.get());
Mathias Agopian65ab4712010-07-14 17:59:35 -07001858}
1859
Dima Zavinfce7a472011-04-19 22:30:36 -07001860/******* helpers for the service_ops callbacks defined below *********/
1861void AudioPolicyService::setParameters(audio_io_handle_t ioHandle,
1862 const char *keyValuePairs,
1863 int delayMs)
1864{
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001865 mAudioCommandThread->parametersCommand(ioHandle, keyValuePairs,
Dima Zavinfce7a472011-04-19 22:30:36 -07001866 delayMs);
1867}
1868
1869int AudioPolicyService::setStreamVolume(audio_stream_type_t stream,
1870 float volume,
1871 audio_io_handle_t output,
1872 int delayMs)
1873{
Glenn Kastenfff6d712012-01-12 16:38:12 -08001874 return (int)mAudioCommandThread->volumeCommand(stream, volume,
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001875 output, delayMs);
Dima Zavinfce7a472011-04-19 22:30:36 -07001876}
1877
Dima Zavinfce7a472011-04-19 22:30:36 -07001878int AudioPolicyService::setVoiceVolume(float volume, int delayMs)
1879{
1880 return (int)mAudioCommandThread->voiceVolumeCommand(volume, delayMs);
1881}
1882
Eric Laurentb20cf7d2019-04-05 19:37:34 -07001883void AudioPolicyService::setEffectSuspended(int effectId,
1884 audio_session_t sessionId,
1885 bool suspended)
1886{
1887 mAudioCommandThread->setEffectSuspendedCommand(effectId, sessionId, suspended);
1888}
1889
Mikhail Naganov88b30d22020-03-09 19:43:13 +00001890void AudioPolicyService::onNewAudioModulesAvailable()
1891{
Mikhail Naganovd0e2c742020-03-25 15:59:59 -07001892 mOutputCommandThread->audioModulesUpdateCommand();
Mikhail Naganov88b30d22020-03-09 19:43:13 +00001893}
1894
Eric Laurentb20cf7d2019-04-05 19:37:34 -07001895
Dima Zavinfce7a472011-04-19 22:30:36 -07001896extern "C" {
Eric Laurent2d388ec2014-03-07 13:25:54 -08001897audio_module_handle_t aps_load_hw_module(void *service __unused,
1898 const char *name);
1899audio_io_handle_t aps_open_output(void *service __unused,
Eric Laurenta4c5a552012-03-29 10:12:40 -07001900 audio_devices_t *pDevices,
1901 uint32_t *pSamplingRate,
1902 audio_format_t *pFormat,
1903 audio_channel_mask_t *pChannelMask,
1904 uint32_t *pLatencyMs,
Eric Laurent2d388ec2014-03-07 13:25:54 -08001905 audio_output_flags_t flags);
Eric Laurenta4c5a552012-03-29 10:12:40 -07001906
Eric Laurent2d388ec2014-03-07 13:25:54 -08001907audio_io_handle_t aps_open_output_on_module(void *service __unused,
Eric Laurenta4c5a552012-03-29 10:12:40 -07001908 audio_module_handle_t module,
1909 audio_devices_t *pDevices,
1910 uint32_t *pSamplingRate,
1911 audio_format_t *pFormat,
1912 audio_channel_mask_t *pChannelMask,
1913 uint32_t *pLatencyMs,
Richard Fitzgeraldad3af332013-03-25 16:54:37 +00001914 audio_output_flags_t flags,
Eric Laurent2d388ec2014-03-07 13:25:54 -08001915 const audio_offload_info_t *offloadInfo);
1916audio_io_handle_t aps_open_dup_output(void *service __unused,
Dima Zavinfce7a472011-04-19 22:30:36 -07001917 audio_io_handle_t output1,
Eric Laurent2d388ec2014-03-07 13:25:54 -08001918 audio_io_handle_t output2);
1919int aps_close_output(void *service __unused, audio_io_handle_t output);
1920int aps_suspend_output(void *service __unused, audio_io_handle_t output);
1921int aps_restore_output(void *service __unused, audio_io_handle_t output);
1922audio_io_handle_t aps_open_input(void *service __unused,
Eric Laurenta4c5a552012-03-29 10:12:40 -07001923 audio_devices_t *pDevices,
1924 uint32_t *pSamplingRate,
1925 audio_format_t *pFormat,
1926 audio_channel_mask_t *pChannelMask,
Eric Laurent2d388ec2014-03-07 13:25:54 -08001927 audio_in_acoustics_t acoustics __unused);
1928audio_io_handle_t aps_open_input_on_module(void *service __unused,
Eric Laurenta4c5a552012-03-29 10:12:40 -07001929 audio_module_handle_t module,
1930 audio_devices_t *pDevices,
1931 uint32_t *pSamplingRate,
1932 audio_format_t *pFormat,
Eric Laurent2d388ec2014-03-07 13:25:54 -08001933 audio_channel_mask_t *pChannelMask);
1934int aps_close_input(void *service __unused, audio_io_handle_t input);
1935int aps_invalidate_stream(void *service __unused, audio_stream_type_t stream);
Glenn Kastend848eb42016-03-08 13:42:11 -08001936int aps_move_effects(void *service __unused, audio_session_t session,
Dima Zavinfce7a472011-04-19 22:30:36 -07001937 audio_io_handle_t src_output,
Eric Laurent2d388ec2014-03-07 13:25:54 -08001938 audio_io_handle_t dst_output);
1939char * aps_get_parameters(void *service __unused, audio_io_handle_t io_handle,
1940 const char *keys);
1941void aps_set_parameters(void *service, audio_io_handle_t io_handle,
1942 const char *kv_pairs, int delay_ms);
1943int aps_set_stream_volume(void *service, audio_stream_type_t stream,
Dima Zavinfce7a472011-04-19 22:30:36 -07001944 float volume, audio_io_handle_t output,
Eric Laurent2d388ec2014-03-07 13:25:54 -08001945 int delay_ms);
Eric Laurent2d388ec2014-03-07 13:25:54 -08001946int aps_set_voice_volume(void *service, float volume, int delay_ms);
1947};
Dima Zavinfce7a472011-04-19 22:30:36 -07001948
Mikhail Naganov1b2a7942017-12-08 10:18:09 -08001949} // namespace android