blob: 1d72931b7138a1ac19702940f446be12f2bb62e6 [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>
25
26#include <sys/time.h>
27#include <binder/IServiceManager.h>
28#include <utils/Log.h>
29#include <cutils/properties.h>
30#include <binder/IPCThreadState.h>
Svet Ganovf4ddfef2018-01-16 07:37:58 -080031#include <binder/ActivityManager.h>
32#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
51static const int kDumpLockRetries = 50;
Glenn Kasten22ecc912012-01-09 08:33:38 -080052static const int kDumpLockSleepUs = 20000;
Mathias Agopian65ab4712010-07-14 17:59:35 -070053
Eric Laurent0ede8922014-05-09 18:04:42 -070054static const nsecs_t kAudioCommandTimeoutNs = seconds(3); // 3 seconds
Christer Fletcher5fa8c4b2013-01-18 15:27:03 +010055
Svet Ganovf4ddfef2018-01-16 07:37:58 -080056static const String16 sManageAudioPolicyPermission("android.permission.MANAGE_AUDIO_POLICY");
Dima Zavinfce7a472011-04-19 22:30:36 -070057
Mathias Agopian65ab4712010-07-14 17:59:35 -070058// ----------------------------------------------------------------------------
59
60AudioPolicyService::AudioPolicyService()
Eric Laurentdce54a12014-03-10 12:19:46 -070061 : BnAudioPolicyService(), mpAudioPolicyDev(NULL), mpAudioPolicy(NULL),
Eric Laurentbb6c9a02014-09-25 14:11:47 -070062 mAudioPolicyManager(NULL), mAudioPolicyClient(NULL), mPhoneState(AUDIO_MODE_INVALID)
Mathias Agopian65ab4712010-07-14 17:59:35 -070063{
Eric Laurentf5ada6e2014-10-09 17:49:00 -070064}
65
66void AudioPolicyService::onFirstRef()
67{
Eric Laurent8b1e80b2014-10-07 09:08:47 -070068 {
69 Mutex::Autolock _l(mLock);
Eric Laurent93575202011-01-18 18:39:02 -080070
Eric Laurent8b1e80b2014-10-07 09:08:47 -070071 // start audio commands thread
72 mAudioCommandThread = new AudioCommandThread(String8("ApmAudio"), this);
73 // start output activity command thread
74 mOutputCommandThread = new AudioCommandThread(String8("ApmOutput"), this);
Eric Laurentdce54a12014-03-10 12:19:46 -070075
Eric Laurent8b1e80b2014-10-07 09:08:47 -070076 mAudioPolicyClient = new AudioPolicyClient(this);
77 mAudioPolicyManager = createAudioPolicyManager(mAudioPolicyClient);
Eric Laurent8b1e80b2014-10-07 09:08:47 -070078 }
bryant_liuba2b4392014-06-11 16:49:30 +080079 // load audio processing modules
Eric Laurent8b1e80b2014-10-07 09:08:47 -070080 sp<AudioPolicyEffects>audioPolicyEffects = new AudioPolicyEffects();
81 {
82 Mutex::Autolock _l(mLock);
83 mAudioPolicyEffects = audioPolicyEffects;
84 }
Svet Ganovf4ddfef2018-01-16 07:37:58 -080085
86 mUidPolicy = new UidPolicy(this);
87 mUidPolicy->registerSelf();
Michael Groovercfd28302018-12-11 19:16:46 -080088
89 mSensorPrivacyPolicy = new SensorPrivacyPolicy(this);
90 mSensorPrivacyPolicy->registerSelf();
Mathias Agopian65ab4712010-07-14 17:59:35 -070091}
92
93AudioPolicyService::~AudioPolicyService()
94{
Mathias Agopian65ab4712010-07-14 17:59:35 -070095 mAudioCommandThread->exit();
Eric Laurent657ff612014-05-07 11:58:24 -070096 mOutputCommandThread->exit();
Eric Laurent7c7f10b2011-06-17 21:29:58 -070097
Eric Laurentf269b8e2014-06-09 20:01:29 -070098 destroyAudioPolicyManager(mAudioPolicyManager);
Eric Laurentdce54a12014-03-10 12:19:46 -070099 delete mAudioPolicyClient;
Eric Laurentb52c1522014-05-20 11:27:36 -0700100
101 mNotificationClients.clear();
bryant_liuba2b4392014-06-11 16:49:30 +0800102 mAudioPolicyEffects.clear();
Svet Ganovf4ddfef2018-01-16 07:37:58 -0800103
104 mUidPolicy->unregisterSelf();
105 mUidPolicy.clear();
Michael Groovercfd28302018-12-11 19:16:46 -0800106
107 mSensorPrivacyPolicy->unregisterSelf();
108 mSensorPrivacyPolicy.clear();
Eric Laurentb52c1522014-05-20 11:27:36 -0700109}
110
111// A notification client is always registered by AudioSystem when the client process
112// connects to AudioPolicyService.
113void AudioPolicyService::registerClient(const sp<IAudioPolicyServiceClient>& client)
114{
Eric Laurent12590252015-08-21 18:40:20 -0700115 if (client == 0) {
116 ALOGW("%s got NULL client", __FUNCTION__);
117 return;
118 }
Eric Laurent0ebd5f92014-11-19 19:04:52 -0800119 Mutex::Autolock _l(mNotificationClientsLock);
Eric Laurentb52c1522014-05-20 11:27:36 -0700120
121 uid_t uid = IPCThreadState::self()->getCallingUid();
luochaojiang908c7d72018-06-21 14:58:04 +0800122 pid_t pid = IPCThreadState::self()->getCallingPid();
123 int64_t token = ((int64_t)uid<<32) | pid;
124
125 if (mNotificationClients.indexOfKey(token) < 0) {
Eric Laurentb52c1522014-05-20 11:27:36 -0700126 sp<NotificationClient> notificationClient = new NotificationClient(this,
127 client,
luochaojiang908c7d72018-06-21 14:58:04 +0800128 uid,
129 pid);
130 ALOGV("registerClient() client %p, uid %d pid %d", client.get(), uid, pid);
Eric Laurentb52c1522014-05-20 11:27:36 -0700131
luochaojiang908c7d72018-06-21 14:58:04 +0800132 mNotificationClients.add(token, notificationClient);
Eric Laurentb52c1522014-05-20 11:27:36 -0700133
Marco Nelissenf8880202014-11-14 07:58:25 -0800134 sp<IBinder> binder = IInterface::asBinder(client);
Eric Laurentb52c1522014-05-20 11:27:36 -0700135 binder->linkToDeath(notificationClient);
136 }
137}
138
Eric Laurente8726fe2015-06-26 09:39:24 -0700139void AudioPolicyService::setAudioPortCallbacksEnabled(bool enabled)
140{
141 Mutex::Autolock _l(mNotificationClientsLock);
142
143 uid_t uid = IPCThreadState::self()->getCallingUid();
luochaojiang908c7d72018-06-21 14:58:04 +0800144 pid_t pid = IPCThreadState::self()->getCallingPid();
145 int64_t token = ((int64_t)uid<<32) | pid;
146
147 if (mNotificationClients.indexOfKey(token) < 0) {
Eric Laurente8726fe2015-06-26 09:39:24 -0700148 return;
149 }
luochaojiang908c7d72018-06-21 14:58:04 +0800150 mNotificationClients.valueFor(token)->setAudioPortCallbacksEnabled(enabled);
Eric Laurente8726fe2015-06-26 09:39:24 -0700151}
152
Eric Laurentb52c1522014-05-20 11:27:36 -0700153// removeNotificationClient() is called when the client process dies.
luochaojiang908c7d72018-06-21 14:58:04 +0800154void AudioPolicyService::removeNotificationClient(uid_t uid, pid_t pid)
Eric Laurentb52c1522014-05-20 11:27:36 -0700155{
Eric Laurent0ebd5f92014-11-19 19:04:52 -0800156 {
157 Mutex::Autolock _l(mNotificationClientsLock);
luochaojiang908c7d72018-06-21 14:58:04 +0800158 int64_t token = ((int64_t)uid<<32) | pid;
159 mNotificationClients.removeItem(token);
Eric Laurent0ebd5f92014-11-19 19:04:52 -0800160 }
Eric Laurent0ebd5f92014-11-19 19:04:52 -0800161 {
162 Mutex::Autolock _l(mLock);
luochaojiang908c7d72018-06-21 14:58:04 +0800163 bool hasSameUid = false;
164 for (size_t i = 0; i < mNotificationClients.size(); i++) {
165 if (mNotificationClients.valueAt(i)->uid() == uid) {
166 hasSameUid = true;
167 break;
168 }
169 }
170 if (mAudioPolicyManager && !hasSameUid) {
Eric Laurent10b71232018-04-13 18:14:44 -0700171 // called from binder death notification: no need to clear caller identity
Eric Laurent8c7e6da2015-04-21 17:37:00 -0700172 mAudioPolicyManager->releaseResourcesForUid(uid);
Eric Laurentb52c1522014-05-20 11:27:36 -0700173 }
Eric Laurent0ebd5f92014-11-19 19:04:52 -0800174 }
Eric Laurentb52c1522014-05-20 11:27:36 -0700175}
176
177void AudioPolicyService::onAudioPortListUpdate()
178{
179 mOutputCommandThread->updateAudioPortListCommand();
180}
181
182void AudioPolicyService::doOnAudioPortListUpdate()
183{
Eric Laurent0ebd5f92014-11-19 19:04:52 -0800184 Mutex::Autolock _l(mNotificationClientsLock);
Eric Laurentb52c1522014-05-20 11:27:36 -0700185 for (size_t i = 0; i < mNotificationClients.size(); i++) {
186 mNotificationClients.valueAt(i)->onAudioPortListUpdate();
187 }
188}
189
190void AudioPolicyService::onAudioPatchListUpdate()
191{
192 mOutputCommandThread->updateAudioPatchListCommand();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700193}
194
Eric Laurentb52c1522014-05-20 11:27:36 -0700195void AudioPolicyService::doOnAudioPatchListUpdate()
196{
Eric Laurent0ebd5f92014-11-19 19:04:52 -0800197 Mutex::Autolock _l(mNotificationClientsLock);
Eric Laurentb52c1522014-05-20 11:27:36 -0700198 for (size_t i = 0; i < mNotificationClients.size(); i++) {
199 mNotificationClients.valueAt(i)->onAudioPatchListUpdate();
200 }
201}
202
Chih-Hung Hsiehe964d4e2016-08-09 14:31:32 -0700203void AudioPolicyService::onDynamicPolicyMixStateUpdate(const String8& regId, int32_t state)
Jean-Michel Trivide801052015-04-14 19:10:14 -0700204{
205 ALOGV("AudioPolicyService::onDynamicPolicyMixStateUpdate(%s, %d)",
206 regId.string(), state);
207 mOutputCommandThread->dynamicPolicyMixStateUpdateCommand(regId, state);
208}
209
Chih-Hung Hsiehe964d4e2016-08-09 14:31:32 -0700210void AudioPolicyService::doOnDynamicPolicyMixStateUpdate(const String8& regId, int32_t state)
Jean-Michel Trivide801052015-04-14 19:10:14 -0700211{
212 Mutex::Autolock _l(mNotificationClientsLock);
213 for (size_t i = 0; i < mNotificationClients.size(); i++) {
214 mNotificationClients.valueAt(i)->onDynamicPolicyMixStateUpdate(regId, state);
215 }
216}
217
Eric Laurenta9f86652018-11-28 17:23:11 -0800218void AudioPolicyService::onRecordingConfigurationUpdate(
219 int event,
220 const record_client_info_t *clientInfo,
221 const audio_config_base_t *clientConfig,
222 std::vector<effect_descriptor_t> clientEffects,
223 const audio_config_base_t *deviceConfig,
224 std::vector<effect_descriptor_t> effects,
225 audio_patch_handle_t patchHandle,
226 audio_source_t source)
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -0800227{
Jean-Michel Triviac4e4292016-12-22 11:39:31 -0800228 mOutputCommandThread->recordingConfigurationUpdateCommand(event, clientInfo,
Eric Laurenta9f86652018-11-28 17:23:11 -0800229 clientConfig, clientEffects, deviceConfig, effects, patchHandle, source);
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -0800230}
231
Eric Laurenta9f86652018-11-28 17:23:11 -0800232void AudioPolicyService::doOnRecordingConfigurationUpdate(
233 int event,
234 const record_client_info_t *clientInfo,
235 const audio_config_base_t *clientConfig,
236 std::vector<effect_descriptor_t> clientEffects,
237 const audio_config_base_t *deviceConfig,
238 std::vector<effect_descriptor_t> effects,
239 audio_patch_handle_t patchHandle,
240 audio_source_t source)
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -0800241{
242 Mutex::Autolock _l(mNotificationClientsLock);
243 for (size_t i = 0; i < mNotificationClients.size(); i++) {
Jean-Michel Triviac4e4292016-12-22 11:39:31 -0800244 mNotificationClients.valueAt(i)->onRecordingConfigurationUpdate(event, clientInfo,
Eric Laurenta9f86652018-11-28 17:23:11 -0800245 clientConfig, clientEffects, deviceConfig, effects, patchHandle, source);
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -0800246 }
247}
248
249status_t AudioPolicyService::clientCreateAudioPatch(const struct audio_patch *patch,
250 audio_patch_handle_t *handle,
251 int delayMs)
252{
253 return mAudioCommandThread->createAudioPatchCommand(patch, handle, delayMs);
254}
255
256status_t AudioPolicyService::clientReleaseAudioPatch(audio_patch_handle_t handle,
257 int delayMs)
258{
259 return mAudioCommandThread->releaseAudioPatchCommand(handle, delayMs);
260}
261
Eric Laurente1715a42014-05-20 11:30:42 -0700262status_t AudioPolicyService::clientSetAudioPortConfig(const struct audio_port_config *config,
263 int delayMs)
264{
265 return mAudioCommandThread->setAudioPortConfigCommand(config, delayMs);
266}
267
Eric Laurentb52c1522014-05-20 11:27:36 -0700268AudioPolicyService::NotificationClient::NotificationClient(const sp<AudioPolicyService>& service,
269 const sp<IAudioPolicyServiceClient>& client,
luochaojiang908c7d72018-06-21 14:58:04 +0800270 uid_t uid,
271 pid_t pid)
272 : mService(service), mUid(uid), mPid(pid), mAudioPolicyServiceClient(client),
Eric Laurente8726fe2015-06-26 09:39:24 -0700273 mAudioPortCallbacksEnabled(false)
Eric Laurentb52c1522014-05-20 11:27:36 -0700274{
275}
276
277AudioPolicyService::NotificationClient::~NotificationClient()
278{
279}
280
281void AudioPolicyService::NotificationClient::binderDied(const wp<IBinder>& who __unused)
282{
283 sp<NotificationClient> keep(this);
284 sp<AudioPolicyService> service = mService.promote();
285 if (service != 0) {
luochaojiang908c7d72018-06-21 14:58:04 +0800286 service->removeNotificationClient(mUid, mPid);
Eric Laurentb52c1522014-05-20 11:27:36 -0700287 }
288}
289
290void AudioPolicyService::NotificationClient::onAudioPortListUpdate()
291{
Eric Laurente8726fe2015-06-26 09:39:24 -0700292 if (mAudioPolicyServiceClient != 0 && mAudioPortCallbacksEnabled) {
Eric Laurentb52c1522014-05-20 11:27:36 -0700293 mAudioPolicyServiceClient->onAudioPortListUpdate();
294 }
295}
296
297void AudioPolicyService::NotificationClient::onAudioPatchListUpdate()
298{
Eric Laurente8726fe2015-06-26 09:39:24 -0700299 if (mAudioPolicyServiceClient != 0 && mAudioPortCallbacksEnabled) {
Eric Laurentb52c1522014-05-20 11:27:36 -0700300 mAudioPolicyServiceClient->onAudioPatchListUpdate();
301 }
302}
Eric Laurent57dae992011-07-24 13:36:09 -0700303
Jean-Michel Trivide801052015-04-14 19:10:14 -0700304void AudioPolicyService::NotificationClient::onDynamicPolicyMixStateUpdate(
Chih-Hung Hsiehe964d4e2016-08-09 14:31:32 -0700305 const String8& regId, int32_t state)
Jean-Michel Trivide801052015-04-14 19:10:14 -0700306{
Andy Hung4ef19fa2018-05-15 19:35:29 -0700307 if (mAudioPolicyServiceClient != 0 && isServiceUid(mUid)) {
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -0800308 mAudioPolicyServiceClient->onDynamicPolicyMixStateUpdate(regId, state);
309 }
310}
311
312void AudioPolicyService::NotificationClient::onRecordingConfigurationUpdate(
Eric Laurenta9f86652018-11-28 17:23:11 -0800313 int event,
314 const record_client_info_t *clientInfo,
315 const audio_config_base_t *clientConfig,
316 std::vector<effect_descriptor_t> clientEffects,
317 const audio_config_base_t *deviceConfig,
318 std::vector<effect_descriptor_t> effects,
319 audio_patch_handle_t patchHandle,
320 audio_source_t source)
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -0800321{
Andy Hung4ef19fa2018-05-15 19:35:29 -0700322 if (mAudioPolicyServiceClient != 0 && isServiceUid(mUid)) {
Jean-Michel Triviac4e4292016-12-22 11:39:31 -0800323 mAudioPolicyServiceClient->onRecordingConfigurationUpdate(event, clientInfo,
Eric Laurenta9f86652018-11-28 17:23:11 -0800324 clientConfig, clientEffects, deviceConfig, effects, patchHandle, source);
Jean-Michel Trivide801052015-04-14 19:10:14 -0700325 }
326}
327
Eric Laurente8726fe2015-06-26 09:39:24 -0700328void AudioPolicyService::NotificationClient::setAudioPortCallbacksEnabled(bool enabled)
329{
330 mAudioPortCallbacksEnabled = enabled;
331}
332
333
Mathias Agopian65ab4712010-07-14 17:59:35 -0700334void AudioPolicyService::binderDied(const wp<IBinder>& who) {
Glenn Kasten411e4472012-11-02 10:00:06 -0700335 ALOGW("binderDied() %p, calling pid %d", who.unsafe_get(),
Eric Laurentde070132010-07-13 04:45:46 -0700336 IPCThreadState::self()->getCallingPid());
Mathias Agopian65ab4712010-07-14 17:59:35 -0700337}
338
339static bool tryLock(Mutex& mutex)
340{
341 bool locked = false;
342 for (int i = 0; i < kDumpLockRetries; ++i) {
343 if (mutex.tryLock() == NO_ERROR) {
344 locked = true;
345 break;
346 }
Glenn Kasten22ecc912012-01-09 08:33:38 -0800347 usleep(kDumpLockSleepUs);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700348 }
349 return locked;
350}
351
352status_t AudioPolicyService::dumpInternals(int fd)
353{
354 const size_t SIZE = 256;
355 char buffer[SIZE];
356 String8 result;
357
Eric Laurentdce54a12014-03-10 12:19:46 -0700358 snprintf(buffer, SIZE, "AudioPolicyManager: %p\n", mAudioPolicyManager);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700359 result.append(buffer);
360 snprintf(buffer, SIZE, "Command Thread: %p\n", mAudioCommandThread.get());
361 result.append(buffer);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700362
363 write(fd, result.string(), result.size());
364 return NO_ERROR;
365}
366
Eric Laurente8c8b432018-10-17 10:08:02 -0700367void AudioPolicyService::updateUidStates()
Svet Ganovf4ddfef2018-01-16 07:37:58 -0800368{
Eric Laurente8c8b432018-10-17 10:08:02 -0700369 Mutex::Autolock _l(mLock);
370 updateUidStates_l();
371}
372
373void AudioPolicyService::updateUidStates_l()
374{
Eric Laurent4eb58f12018-12-07 16:41:02 -0800375// Go over all active clients and allow capture (does not force silence) in the
376// following cases:
Eric Laurenta46bedb2018-12-07 18:01:26 -0800377// The client is the assistant
378// AND an accessibility service is on TOP
379// AND the source is VOICE_RECOGNITION or HOTWORD
380// OR uses VOICE_RECOGNITION AND is on TOP OR latest started
381// OR uses HOTWORD
382// AND there is no privacy sensitive active capture
383// OR The client is an accessibility service
384// AND is on TOP OR latest started
385// AND the source is VOICE_RECOGNITION or HOTWORD
Sapir Caduri9a183af2019-02-14 22:23:47 +0200386// OR the source is one of: AUDIO_SOURCE_VOICE_DOWNLINK, AUDIO_SOURCE_VOICE_UPLINK,
387// AUDIO_SOURCE_VOICE_CALL
Eric Laurenta46bedb2018-12-07 18:01:26 -0800388// OR Any other client
389// AND The assistant is not on TOP
390// AND is on TOP OR latest started
391// AND there is no privacy sensitive active capture
Eric Laurent4eb58f12018-12-07 16:41:02 -0800392//TODO: mamanage pre processing effects according to use case priority
393
394 sp<AudioRecordClient> topActive;
395 sp<AudioRecordClient> latestActive;
Eric Laurent4eb58f12018-12-07 16:41:02 -0800396 sp<AudioRecordClient> latestSensitiveActive;
Eric Laurenta46bedb2018-12-07 18:01:26 -0800397 nsecs_t topStartNs = 0;
398 nsecs_t latestStartNs = 0;
Eric Laurent4eb58f12018-12-07 16:41:02 -0800399 nsecs_t latestSensitiveStartNs = 0;
400 bool isA11yOnTop = mUidPolicy->isA11yOnTop();
401 bool isAssistantOnTop = false;
402 bool isSensitiveActive = false;
403
Michael Groovercfd28302018-12-11 19:16:46 -0800404 // if Sensor Privacy is enabled then all recordings should be silenced.
405 if (mSensorPrivacyPolicy->isSensorPrivacyEnabled()) {
406 silenceAllRecordings_l();
407 return;
408 }
409
Eric Laurente8c8b432018-10-17 10:08:02 -0700410 for (size_t i =0; i < mAudioRecordClients.size(); i++) {
411 sp<AudioRecordClient> current = mAudioRecordClients[i];
412 if (!current->active) continue;
Eric Laurent4eb58f12018-12-07 16:41:02 -0800413 if (isPrivacySensitive(current->attributes.source)) {
414 if (current->startTimeNs > latestSensitiveStartNs) {
415 latestSensitiveActive = current;
416 latestSensitiveStartNs = current->startTimeNs;
417 }
418 isSensitiveActive = true;
419 }
420 if (mUidPolicy->getUidState(current->uid) == ActivityManager::PROCESS_STATE_TOP) {
Eric Laurenta46bedb2018-12-07 18:01:26 -0800421 if (current->startTimeNs > topStartNs) {
422 topActive = current;
423 topStartNs = current->startTimeNs;
424 }
Eric Laurent4eb58f12018-12-07 16:41:02 -0800425 if (mUidPolicy->isAssistantUid(current->uid)) {
426 isAssistantOnTop = true;
427 }
428 }
429 if (current->startTimeNs > latestStartNs) {
430 latestActive = current;
431 latestStartNs = current->startTimeNs;
432 }
433 }
434
435 if (topActive == nullptr && latestActive == nullptr) {
436 return;
437 }
438
Eric Laurenta46bedb2018-12-07 18:01:26 -0800439 if (topActive != nullptr) {
440 latestActive = nullptr;
441 }
442
Eric Laurent4eb58f12018-12-07 16:41:02 -0800443 for (size_t i =0; i < mAudioRecordClients.size(); i++) {
444 sp<AudioRecordClient> current = mAudioRecordClients[i];
445 if (!current->active) continue;
446
447 audio_source_t source = current->attributes.source;
Eric Laurenta46bedb2018-12-07 18:01:26 -0800448 bool isOnTop = current == topActive;
Eric Laurent4eb58f12018-12-07 16:41:02 -0800449 bool isLatest = current == latestActive;
450 bool isLatestSensitive = current == latestSensitiveActive;
451 bool forceIdle = true;
452 if (mUidPolicy->isAssistantUid(current->uid)) {
453 if (isA11yOnTop) {
454 if (source == AUDIO_SOURCE_HOTWORD || source == AUDIO_SOURCE_VOICE_RECOGNITION) {
455 forceIdle = false;
456 }
457 } else {
Eric Laurenta46bedb2018-12-07 18:01:26 -0800458 if ((((isOnTop || isLatest) && source == AUDIO_SOURCE_VOICE_RECOGNITION) ||
Eric Laurent4eb58f12018-12-07 16:41:02 -0800459 source == AUDIO_SOURCE_HOTWORD) && !isSensitiveActive) {
460 forceIdle = false;
461 }
462 }
463 } else if (mUidPolicy->isA11yUid(current->uid)) {
Eric Laurenta46bedb2018-12-07 18:01:26 -0800464 if ((isOnTop || isLatest) &&
Eric Laurent4eb58f12018-12-07 16:41:02 -0800465 (source == AUDIO_SOURCE_VOICE_RECOGNITION || source == AUDIO_SOURCE_HOTWORD)) {
466 forceIdle = false;
467 }
Sapir Caduri9a183af2019-02-14 22:23:47 +0200468 } else if (source == AUDIO_SOURCE_VOICE_DOWNLINK ||
469 source == AUDIO_SOURCE_VOICE_CALL ||
470 (source == AUDIO_SOURCE_VOICE_UPLINK)) {
471 forceIdle = false;
Eric Laurent4eb58f12018-12-07 16:41:02 -0800472 } else {
Eric Laurenta46bedb2018-12-07 18:01:26 -0800473 if (!isAssistantOnTop && (isOnTop || isLatest) &&
Eric Laurent4eb58f12018-12-07 16:41:02 -0800474 (!isSensitiveActive || isLatestSensitive)) {
475 forceIdle = false;
476 }
477 }
478 setAppState_l(current->uid,
479 forceIdle ? APP_STATE_IDLE :
480 apmStatFromAmState(mUidPolicy->getUidState(current->uid)));
Eric Laurente8c8b432018-10-17 10:08:02 -0700481 }
482}
483
Michael Groovercfd28302018-12-11 19:16:46 -0800484void AudioPolicyService::silenceAllRecordings_l() {
485 for (size_t i = 0; i < mAudioRecordClients.size(); i++) {
486 sp<AudioRecordClient> current = mAudioRecordClients[i];
487 setAppState_l(current->uid, APP_STATE_IDLE);
488 }
489}
490
Eric Laurente8c8b432018-10-17 10:08:02 -0700491/* static */
492app_state_t AudioPolicyService::apmStatFromAmState(int amState) {
493 switch (amState) {
494 case ActivityManager::PROCESS_STATE_UNKNOWN:
495 return APP_STATE_IDLE;
496 case ActivityManager::PROCESS_STATE_TOP:
497 return APP_STATE_TOP;
498 default:
499 break;
500 }
501 return APP_STATE_FOREGROUND;
502}
503
Eric Laurent4eb58f12018-12-07 16:41:02 -0800504/* static */
505bool AudioPolicyService::isPrivacySensitive(audio_source_t source)
506{
507 switch (source) {
508 case AUDIO_SOURCE_VOICE_UPLINK:
509 case AUDIO_SOURCE_VOICE_DOWNLINK:
510 case AUDIO_SOURCE_VOICE_CALL:
511 case AUDIO_SOURCE_CAMCORDER:
512 case AUDIO_SOURCE_VOICE_COMMUNICATION:
513 return true;
514 default:
515 break;
516 }
517 return false;
518}
519
Eric Laurente8c8b432018-10-17 10:08:02 -0700520void AudioPolicyService::setAppState_l(uid_t uid, app_state_t state)
521{
522 AutoCallerClear acc;
523
524 if (mAudioPolicyManager) {
525 mAudioPolicyManager->setAppState(uid, state);
Mikhail Naganoveae73eb2018-04-03 16:57:36 -0700526 }
527 sp<IAudioFlinger> af = AudioSystem::get_audio_flinger();
528 if (af) {
Eric Laurentf32108e2018-10-04 17:22:04 -0700529 bool silenced = state == APP_STATE_IDLE;
Mikhail Naganoveae73eb2018-04-03 16:57:36 -0700530 af->setRecordSilenced(uid, silenced);
531 }
Svet Ganovf4ddfef2018-01-16 07:37:58 -0800532}
533
Glenn Kasten0f11b512014-01-31 16:18:54 -0800534status_t AudioPolicyService::dump(int fd, const Vector<String16>& args __unused)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700535{
Glenn Kasten44deb052012-02-05 18:09:08 -0800536 if (!dumpAllowed()) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700537 dumpPermissionDenial(fd);
538 } else {
539 bool locked = tryLock(mLock);
540 if (!locked) {
541 String8 result(kDeadlockedString);
542 write(fd, result.string(), result.size());
543 }
544
545 dumpInternals(fd);
Glenn Kasten9d1f02d2012-02-08 17:47:58 -0800546 if (mAudioCommandThread != 0) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700547 mAudioCommandThread->dump(fd);
548 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700549
Eric Laurentdce54a12014-03-10 12:19:46 -0700550 if (mAudioPolicyManager) {
551 mAudioPolicyManager->dump(fd);
552 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700553
Kevin Rocard8be94972019-02-22 13:26:25 -0800554 mPackageManager.dump(fd);
555
Mathias Agopian65ab4712010-07-14 17:59:35 -0700556 if (locked) mLock.unlock();
557 }
558 return NO_ERROR;
559}
560
561status_t AudioPolicyService::dumpPermissionDenial(int fd)
562{
563 const size_t SIZE = 256;
564 char buffer[SIZE];
565 String8 result;
566 snprintf(buffer, SIZE, "Permission Denial: "
567 "can't dump AudioPolicyService from pid=%d, uid=%d\n",
568 IPCThreadState::self()->getCallingPid(),
569 IPCThreadState::self()->getCallingUid());
570 result.append(buffer);
571 write(fd, result.string(), result.size());
572 return NO_ERROR;
573}
574
575status_t AudioPolicyService::onTransact(
Svet Ganovf4ddfef2018-01-16 07:37:58 -0800576 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags) {
577 switch (code) {
578 case SHELL_COMMAND_TRANSACTION: {
579 int in = data.readFileDescriptor();
580 int out = data.readFileDescriptor();
581 int err = data.readFileDescriptor();
582 int argc = data.readInt32();
583 Vector<String16> args;
584 for (int i = 0; i < argc && data.dataAvail() > 0; i++) {
585 args.add(data.readString16());
586 }
587 sp<IBinder> unusedCallback;
588 sp<IResultReceiver> resultReceiver;
589 status_t status;
590 if ((status = data.readNullableStrongBinder(&unusedCallback)) != NO_ERROR) {
591 return status;
592 }
593 if ((status = data.readNullableStrongBinder(&resultReceiver)) != NO_ERROR) {
594 return status;
595 }
596 status = shellCommand(in, out, err, args);
597 if (resultReceiver != nullptr) {
598 resultReceiver->send(status);
599 }
600 return NO_ERROR;
601 }
602 }
603
Mathias Agopian65ab4712010-07-14 17:59:35 -0700604 return BnAudioPolicyService::onTransact(code, data, reply, flags);
605}
606
Svet Ganovf4ddfef2018-01-16 07:37:58 -0800607// ------------------- Shell command implementation -------------------
608
609// NOTE: This is a remote API - make sure all args are validated
610status_t AudioPolicyService::shellCommand(int in, int out, int err, Vector<String16>& args) {
611 if (!checkCallingPermission(sManageAudioPolicyPermission, nullptr, nullptr)) {
612 return PERMISSION_DENIED;
613 }
614 if (in == BAD_TYPE || out == BAD_TYPE || err == BAD_TYPE) {
615 return BAD_VALUE;
616 }
617 if (args.size() == 3 && args[0] == String16("set-uid-state")) {
618 return handleSetUidState(args, err);
619 } else if (args.size() == 2 && args[0] == String16("reset-uid-state")) {
620 return handleResetUidState(args, err);
621 } else if (args.size() == 2 && args[0] == String16("get-uid-state")) {
622 return handleGetUidState(args, out, err);
623 } else if (args.size() == 1 && args[0] == String16("help")) {
624 printHelp(out);
625 return NO_ERROR;
626 }
627 printHelp(err);
628 return BAD_VALUE;
629}
630
631status_t AudioPolicyService::handleSetUidState(Vector<String16>& args, int err) {
632 PermissionController pc;
633 int uid = pc.getPackageUid(args[1], 0);
634 if (uid <= 0) {
635 ALOGE("Unknown package: '%s'", String8(args[1]).string());
636 dprintf(err, "Unknown package: '%s'\n", String8(args[1]).string());
637 return BAD_VALUE;
638 }
639 bool active = false;
640 if (args[2] == String16("active")) {
641 active = true;
642 } else if ((args[2] != String16("idle"))) {
643 ALOGE("Expected active or idle but got: '%s'", String8(args[2]).string());
644 return BAD_VALUE;
645 }
646 mUidPolicy->addOverrideUid(uid, active);
647 return NO_ERROR;
648}
649
650status_t AudioPolicyService::handleResetUidState(Vector<String16>& args, int err) {
651 PermissionController pc;
652 int uid = pc.getPackageUid(args[1], 0);
653 if (uid < 0) {
654 ALOGE("Unknown package: '%s'", String8(args[1]).string());
655 dprintf(err, "Unknown package: '%s'\n", String8(args[1]).string());
656 return BAD_VALUE;
657 }
658 mUidPolicy->removeOverrideUid(uid);
659 return NO_ERROR;
660}
661
662status_t AudioPolicyService::handleGetUidState(Vector<String16>& args, int out, int err) {
663 PermissionController pc;
664 int uid = pc.getPackageUid(args[1], 0);
665 if (uid < 0) {
666 ALOGE("Unknown package: '%s'", String8(args[1]).string());
667 dprintf(err, "Unknown package: '%s'\n", String8(args[1]).string());
668 return BAD_VALUE;
669 }
670 if (mUidPolicy->isUidActive(uid)) {
671 return dprintf(out, "active\n");
672 } else {
673 return dprintf(out, "idle\n");
674 }
675}
676
677status_t AudioPolicyService::printHelp(int out) {
678 return dprintf(out, "Audio policy service commands:\n"
679 " get-uid-state <PACKAGE> gets the uid state\n"
680 " set-uid-state <PACKAGE> <active|idle> overrides the uid state\n"
681 " reset-uid-state <PACKAGE> clears the uid state override\n"
682 " help print this message\n");
683}
684
685// ----------- AudioPolicyService::UidPolicy implementation ----------
686
687void AudioPolicyService::UidPolicy::registerSelf() {
688 ActivityManager am;
689 am.registerUidObserver(this, ActivityManager::UID_OBSERVER_GONE
690 | ActivityManager::UID_OBSERVER_IDLE
Eric Laurente8c8b432018-10-17 10:08:02 -0700691 | ActivityManager::UID_OBSERVER_ACTIVE
692 | ActivityManager::UID_OBSERVER_PROCSTATE,
Svet Ganovf4ddfef2018-01-16 07:37:58 -0800693 ActivityManager::PROCESS_STATE_UNKNOWN,
694 String16("audioserver"));
Mikhail Naganoveae73eb2018-04-03 16:57:36 -0700695 status_t res = am.linkToDeath(this);
696 if (!res) {
697 Mutex::Autolock _l(mLock);
698 mObserverRegistered = true;
699 } else {
700 ALOGE("UidPolicy::registerSelf linkToDeath failed: %d", res);
Eric Laurent4eb58f12018-12-07 16:41:02 -0800701
Mikhail Naganoveae73eb2018-04-03 16:57:36 -0700702 am.unregisterUidObserver(this);
703 }
Svet Ganovf4ddfef2018-01-16 07:37:58 -0800704}
705
706void AudioPolicyService::UidPolicy::unregisterSelf() {
707 ActivityManager am;
Mikhail Naganoveae73eb2018-04-03 16:57:36 -0700708 am.unlinkToDeath(this);
Svet Ganovf4ddfef2018-01-16 07:37:58 -0800709 am.unregisterUidObserver(this);
Mikhail Naganoveae73eb2018-04-03 16:57:36 -0700710 Mutex::Autolock _l(mLock);
711 mObserverRegistered = false;
Svet Ganovf4ddfef2018-01-16 07:37:58 -0800712}
713
Mikhail Naganoveae73eb2018-04-03 16:57:36 -0700714void AudioPolicyService::UidPolicy::binderDied(__unused const wp<IBinder> &who) {
715 Mutex::Autolock _l(mLock);
716 mCachedUids.clear();
717 mObserverRegistered = false;
Svet Ganovf4ddfef2018-01-16 07:37:58 -0800718}
719
Eric Laurente8c8b432018-10-17 10:08:02 -0700720void AudioPolicyService::UidPolicy::checkRegistered() {
Mikhail Naganoveae73eb2018-04-03 16:57:36 -0700721 bool needToReregister = false;
722 {
723 Mutex::Autolock _l(mLock);
724 needToReregister = !mObserverRegistered;
Svet Ganovf4ddfef2018-01-16 07:37:58 -0800725 }
Mikhail Naganoveae73eb2018-04-03 16:57:36 -0700726 if (needToReregister) {
727 // Looks like ActivityManager has died previously, attempt to re-register.
728 registerSelf();
729 }
Eric Laurente8c8b432018-10-17 10:08:02 -0700730}
731
732bool AudioPolicyService::UidPolicy::isUidActive(uid_t uid) {
733 if (isServiceUid(uid)) return true;
734 checkRegistered();
Mikhail Naganoveae73eb2018-04-03 16:57:36 -0700735 {
736 Mutex::Autolock _l(mLock);
737 auto overrideIter = mOverrideUids.find(uid);
738 if (overrideIter != mOverrideUids.end()) {
Eric Laurente8c8b432018-10-17 10:08:02 -0700739 return overrideIter->second.first;
Mikhail Naganoveae73eb2018-04-03 16:57:36 -0700740 }
741 // In an absense of the ActivityManager, assume everything to be active.
742 if (!mObserverRegistered) return true;
743 auto cacheIter = mCachedUids.find(uid);
Mikhail Naganoveba668a2018-04-05 08:13:15 -0700744 if (cacheIter != mCachedUids.end()) {
Eric Laurente8c8b432018-10-17 10:08:02 -0700745 return cacheIter->second.first;
Mikhail Naganoveae73eb2018-04-03 16:57:36 -0700746 }
747 }
748 ActivityManager am;
749 bool active = am.isUidActive(uid, String16("audioserver"));
750 {
751 Mutex::Autolock _l(mLock);
Eric Laurente8c8b432018-10-17 10:08:02 -0700752 mCachedUids.insert(std::pair<uid_t,
753 std::pair<bool, int>>(uid, std::pair<bool, int>(active,
754 ActivityManager::PROCESS_STATE_UNKNOWN)));
Mikhail Naganoveae73eb2018-04-03 16:57:36 -0700755 }
756 return active;
Svet Ganovf4ddfef2018-01-16 07:37:58 -0800757}
758
Eric Laurente8c8b432018-10-17 10:08:02 -0700759int AudioPolicyService::UidPolicy::getUidState(uid_t uid) {
760 if (isServiceUid(uid)) {
761 return ActivityManager::PROCESS_STATE_TOP;
762 }
763 checkRegistered();
764 {
765 Mutex::Autolock _l(mLock);
766 auto overrideIter = mOverrideUids.find(uid);
767 if (overrideIter != mOverrideUids.end()) {
768 if (overrideIter->second.first) {
769 if (overrideIter->second.second != ActivityManager::PROCESS_STATE_UNKNOWN) {
770 return overrideIter->second.second;
771 } else {
772 auto cacheIter = mCachedUids.find(uid);
773 if (cacheIter != mCachedUids.end()) {
774 return cacheIter->second.second;
775 }
776 }
777 }
778 return ActivityManager::PROCESS_STATE_UNKNOWN;
779 }
780 // In an absense of the ActivityManager, assume everything to be active.
781 if (!mObserverRegistered) {
782 return ActivityManager::PROCESS_STATE_TOP;
783 }
784 auto cacheIter = mCachedUids.find(uid);
785 if (cacheIter != mCachedUids.end()) {
786 if (cacheIter->second.first) {
787 return cacheIter->second.second;
788 } else {
789 return ActivityManager::PROCESS_STATE_UNKNOWN;
790 }
791 }
792 }
793 ActivityManager am;
794 bool active = am.isUidActive(uid, String16("audioserver"));
795 int state = ActivityManager::PROCESS_STATE_UNKNOWN;
796 if (active) {
797 state = am.getUidProcessState(uid, String16("audioserver"));
798 }
799 {
800 Mutex::Autolock _l(mLock);
801 mCachedUids.insert(std::pair<uid_t,
802 std::pair<bool, int>>(uid, std::pair<bool, int>(active, state)));
803 }
Eric Laurent4eb58f12018-12-07 16:41:02 -0800804
Eric Laurente8c8b432018-10-17 10:08:02 -0700805 return state;
806}
807
Mikhail Naganoveae73eb2018-04-03 16:57:36 -0700808void AudioPolicyService::UidPolicy::onUidActive(uid_t uid) {
Eric Laurente8c8b432018-10-17 10:08:02 -0700809 updateUid(&mCachedUids, uid, true, ActivityManager::PROCESS_STATE_UNKNOWN, true);
Mikhail Naganoveae73eb2018-04-03 16:57:36 -0700810}
811
812void AudioPolicyService::UidPolicy::onUidGone(uid_t uid, __unused bool disabled) {
Eric Laurente8c8b432018-10-17 10:08:02 -0700813 updateUid(&mCachedUids, uid, false, ActivityManager::PROCESS_STATE_UNKNOWN, false);
Mikhail Naganoveae73eb2018-04-03 16:57:36 -0700814}
815
816void AudioPolicyService::UidPolicy::onUidIdle(uid_t uid, __unused bool disabled) {
Eric Laurente8c8b432018-10-17 10:08:02 -0700817 updateUid(&mCachedUids, uid, false, ActivityManager::PROCESS_STATE_UNKNOWN, true);
Mikhail Naganoveae73eb2018-04-03 16:57:36 -0700818}
819
Eric Laurente8c8b432018-10-17 10:08:02 -0700820void AudioPolicyService::UidPolicy::onUidStateChanged(uid_t uid,
821 int32_t procState,
822 int64_t procStateSeq __unused) {
823 if (procState != ActivityManager::PROCESS_STATE_UNKNOWN) {
824 updateUid(&mCachedUids, uid, true, procState, true);
Mikhail Naganoveae73eb2018-04-03 16:57:36 -0700825 }
826}
827
828void AudioPolicyService::UidPolicy::updateOverrideUid(uid_t uid, bool active, bool insert) {
Eric Laurente8c8b432018-10-17 10:08:02 -0700829 updateUid(&mOverrideUids, uid, active, ActivityManager::PROCESS_STATE_UNKNOWN, insert);
830}
831
832void AudioPolicyService::UidPolicy::notifyService() {
833 sp<AudioPolicyService> service = mService.promote();
834 if (service != nullptr) {
835 service->updateUidStates();
Mikhail Naganoveae73eb2018-04-03 16:57:36 -0700836 }
837}
838
Eric Laurente8c8b432018-10-17 10:08:02 -0700839void AudioPolicyService::UidPolicy::updateUid(std::unordered_map<uid_t,
840 std::pair<bool, int>> *uids,
841 uid_t uid,
842 bool active,
843 int state,
844 bool insert) {
845 if (isServiceUid(uid)) {
846 return;
847 }
848 bool wasActive = isUidActive(uid);
849 int previousState = getUidState(uid);
Mikhail Naganoveae73eb2018-04-03 16:57:36 -0700850 {
851 Mutex::Autolock _l(mLock);
Eric Laurente8c8b432018-10-17 10:08:02 -0700852 updateUidLocked(uids, uid, active, state, insert);
Mikhail Naganoveae73eb2018-04-03 16:57:36 -0700853 }
Eric Laurente8c8b432018-10-17 10:08:02 -0700854 if (wasActive != isUidActive(uid) || state != previousState) {
855 notifyService();
856 }
Mikhail Naganoveae73eb2018-04-03 16:57:36 -0700857}
858
Eric Laurente8c8b432018-10-17 10:08:02 -0700859void AudioPolicyService::UidPolicy::updateUidLocked(std::unordered_map<uid_t,
860 std::pair<bool, int>> *uids,
861 uid_t uid,
862 bool active,
863 int state,
864 bool insert) {
Mikhail Naganoveae73eb2018-04-03 16:57:36 -0700865 auto it = uids->find(uid);
866 if (it != uids->end()) {
Mikhail Naganoveae73eb2018-04-03 16:57:36 -0700867 if (insert) {
Eric Laurente8c8b432018-10-17 10:08:02 -0700868 if (state == ActivityManager::PROCESS_STATE_UNKNOWN) {
869 it->second.first = active;
870 }
871 if (it->second.first) {
872 it->second.second = state;
873 } else {
874 it->second.second = ActivityManager::PROCESS_STATE_UNKNOWN;
875 }
Mikhail Naganoveae73eb2018-04-03 16:57:36 -0700876 } else {
877 uids->erase(it);
878 }
Eric Laurente8c8b432018-10-17 10:08:02 -0700879 } else if (insert && (state == ActivityManager::PROCESS_STATE_UNKNOWN)) {
880 uids->insert(std::pair<uid_t, std::pair<bool, int>>(uid,
881 std::pair<bool, int>(active, state)));
Mikhail Naganoveae73eb2018-04-03 16:57:36 -0700882 }
Svet Ganovf4ddfef2018-01-16 07:37:58 -0800883}
Mathias Agopian65ab4712010-07-14 17:59:35 -0700884
Eric Laurent4eb58f12018-12-07 16:41:02 -0800885bool AudioPolicyService::UidPolicy::isA11yOnTop() {
886 for (const auto &uid : mCachedUids) {
887 std::vector<uid_t>::iterator it = find(mA11yUids.begin(), mA11yUids.end(), uid.first);
888 if (it == mA11yUids.end()) {
889 continue;
890 }
Amith Yamasanibcbb3002019-01-23 13:53:33 -0800891 if (uid.second.second >= ActivityManager::PROCESS_STATE_TOP
892 && uid.second.second <= ActivityManager::PROCESS_STATE_BOUND_FOREGROUND_SERVICE) {
Eric Laurent4eb58f12018-12-07 16:41:02 -0800893 return true;
894 }
895 }
896 return false;
897}
898
Eric Laurentb78763e2018-10-17 10:08:02 -0700899bool AudioPolicyService::UidPolicy::isA11yUid(uid_t uid)
900{
901 std::vector<uid_t>::iterator it = find(mA11yUids.begin(), mA11yUids.end(), uid);
902 return it != mA11yUids.end();
903}
904
Michael Groovercfd28302018-12-11 19:16:46 -0800905// ----------- AudioPolicyService::SensorPrivacyService implementation ----------
906void AudioPolicyService::SensorPrivacyPolicy::registerSelf() {
907 SensorPrivacyManager spm;
908 mSensorPrivacyEnabled = spm.isSensorPrivacyEnabled();
909 spm.addSensorPrivacyListener(this);
910}
911
912void AudioPolicyService::SensorPrivacyPolicy::unregisterSelf() {
913 SensorPrivacyManager spm;
914 spm.removeSensorPrivacyListener(this);
915}
916
917bool AudioPolicyService::SensorPrivacyPolicy::isSensorPrivacyEnabled() {
918 return mSensorPrivacyEnabled;
919}
920
921binder::Status AudioPolicyService::SensorPrivacyPolicy::onSensorPrivacyChanged(bool enabled) {
922 mSensorPrivacyEnabled = enabled;
923 sp<AudioPolicyService> service = mService.promote();
924 if (service != nullptr) {
925 service->updateUidStates();
926 }
927 return binder::Status::ok();
928}
929
Mathias Agopian65ab4712010-07-14 17:59:35 -0700930// ----------- AudioPolicyService::AudioCommandThread implementation ----------
931
Eric Laurentbfb1b832013-01-07 09:53:42 -0800932AudioPolicyService::AudioCommandThread::AudioCommandThread(String8 name,
933 const wp<AudioPolicyService>& service)
934 : Thread(false), mName(name), mService(service)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700935{
Mathias Agopian65ab4712010-07-14 17:59:35 -0700936}
937
938
939AudioPolicyService::AudioCommandThread::~AudioCommandThread()
940{
Eric Laurentbfb1b832013-01-07 09:53:42 -0800941 if (!mAudioCommands.isEmpty()) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700942 release_wake_lock(mName.string());
943 }
944 mAudioCommands.clear();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700945}
946
947void AudioPolicyService::AudioCommandThread::onFirstRef()
948{
Eric Laurentbfb1b832013-01-07 09:53:42 -0800949 run(mName.string(), ANDROID_PRIORITY_AUDIO);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700950}
951
952bool AudioPolicyService::AudioCommandThread::threadLoop()
953{
Eric Laurentd7eda8d2016-02-02 17:18:39 -0800954 nsecs_t waitTime = -1;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700955
956 mLock.lock();
957 while (!exitPending())
958 {
Eric Laurent59a89232014-06-08 14:14:17 -0700959 sp<AudioPolicyService> svc;
960 while (!mAudioCommands.isEmpty() && !exitPending()) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700961 nsecs_t curTime = systemTime();
962 // commands are sorted by increasing time stamp: execute them from index 0 and up
963 if (mAudioCommands[0]->mTime <= curTime) {
Eric Laurent0ede8922014-05-09 18:04:42 -0700964 sp<AudioCommand> command = mAudioCommands[0];
Mathias Agopian65ab4712010-07-14 17:59:35 -0700965 mAudioCommands.removeAt(0);
Eric Laurent0ede8922014-05-09 18:04:42 -0700966 mLastCommand = command;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700967
968 switch (command->mCommand) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700969 case SET_VOLUME: {
Eric Laurent0ede8922014-05-09 18:04:42 -0700970 VolumeData *data = (VolumeData *)command->mParam.get();
Steve Block3856b092011-10-20 11:56:00 +0100971 ALOGV("AudioCommandThread() processing set volume stream %d, \
Eric Laurentde070132010-07-13 04:45:46 -0700972 volume %f, output %d", data->mStream, data->mVolume, data->mIO);
Andy Hungfe726a62018-09-27 15:17:25 -0700973 mLock.unlock();
Eric Laurentde070132010-07-13 04:45:46 -0700974 command->mStatus = AudioSystem::setStreamVolume(data->mStream,
975 data->mVolume,
976 data->mIO);
Andy Hungfe726a62018-09-27 15:17:25 -0700977 mLock.lock();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700978 }break;
979 case SET_PARAMETERS: {
Eric Laurent0ede8922014-05-09 18:04:42 -0700980 ParametersData *data = (ParametersData *)command->mParam.get();
Glenn Kastene53b9ea2012-03-12 16:29:55 -0700981 ALOGV("AudioCommandThread() processing set parameters string %s, io %d",
982 data->mKeyValuePairs.string(), data->mIO);
Andy Hungfe726a62018-09-27 15:17:25 -0700983 mLock.unlock();
Glenn Kastene53b9ea2012-03-12 16:29:55 -0700984 command->mStatus = AudioSystem::setParameters(data->mIO, data->mKeyValuePairs);
Andy Hungfe726a62018-09-27 15:17:25 -0700985 mLock.lock();
Glenn Kastene53b9ea2012-03-12 16:29:55 -0700986 }break;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700987 case SET_VOICE_VOLUME: {
Eric Laurent0ede8922014-05-09 18:04:42 -0700988 VoiceVolumeData *data = (VoiceVolumeData *)command->mParam.get();
Steve Block3856b092011-10-20 11:56:00 +0100989 ALOGV("AudioCommandThread() processing set voice volume volume %f",
Eric Laurentde070132010-07-13 04:45:46 -0700990 data->mVolume);
Andy Hungfe726a62018-09-27 15:17:25 -0700991 mLock.unlock();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700992 command->mStatus = AudioSystem::setVoiceVolume(data->mVolume);
Andy Hungfe726a62018-09-27 15:17:25 -0700993 mLock.lock();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700994 }break;
Eric Laurentbfb1b832013-01-07 09:53:42 -0800995 case STOP_OUTPUT: {
Eric Laurent0ede8922014-05-09 18:04:42 -0700996 StopOutputData *data = (StopOutputData *)command->mParam.get();
Eric Laurentd7fe0862018-07-14 16:48:01 -0700997 ALOGV("AudioCommandThread() processing stop output portId %d",
998 data->mPortId);
Eric Laurent59a89232014-06-08 14:14:17 -0700999 svc = mService.promote();
Eric Laurentbfb1b832013-01-07 09:53:42 -08001000 if (svc == 0) {
1001 break;
1002 }
1003 mLock.unlock();
Eric Laurentd7fe0862018-07-14 16:48:01 -07001004 svc->doStopOutput(data->mPortId);
Eric Laurentbfb1b832013-01-07 09:53:42 -08001005 mLock.lock();
Eric Laurentbfb1b832013-01-07 09:53:42 -08001006 }break;
1007 case RELEASE_OUTPUT: {
Eric Laurent0ede8922014-05-09 18:04:42 -07001008 ReleaseOutputData *data = (ReleaseOutputData *)command->mParam.get();
Eric Laurentd7fe0862018-07-14 16:48:01 -07001009 ALOGV("AudioCommandThread() processing release output portId %d",
1010 data->mPortId);
Eric Laurent59a89232014-06-08 14:14:17 -07001011 svc = mService.promote();
Eric Laurentbfb1b832013-01-07 09:53:42 -08001012 if (svc == 0) {
1013 break;
1014 }
1015 mLock.unlock();
Eric Laurentd7fe0862018-07-14 16:48:01 -07001016 svc->doReleaseOutput(data->mPortId);
Eric Laurentbfb1b832013-01-07 09:53:42 -08001017 mLock.lock();
Eric Laurentbfb1b832013-01-07 09:53:42 -08001018 }break;
Eric Laurent951f4552014-05-20 10:48:17 -07001019 case CREATE_AUDIO_PATCH: {
1020 CreateAudioPatchData *data = (CreateAudioPatchData *)command->mParam.get();
1021 ALOGV("AudioCommandThread() processing create audio patch");
1022 sp<IAudioFlinger> af = AudioSystem::get_audio_flinger();
1023 if (af == 0) {
1024 command->mStatus = PERMISSION_DENIED;
1025 } else {
Andy Hungfe726a62018-09-27 15:17:25 -07001026 mLock.unlock();
Eric Laurent951f4552014-05-20 10:48:17 -07001027 command->mStatus = af->createAudioPatch(&data->mPatch, &data->mHandle);
Andy Hungfe726a62018-09-27 15:17:25 -07001028 mLock.lock();
Eric Laurent951f4552014-05-20 10:48:17 -07001029 }
1030 } break;
1031 case RELEASE_AUDIO_PATCH: {
1032 ReleaseAudioPatchData *data = (ReleaseAudioPatchData *)command->mParam.get();
1033 ALOGV("AudioCommandThread() processing release audio patch");
1034 sp<IAudioFlinger> af = AudioSystem::get_audio_flinger();
1035 if (af == 0) {
1036 command->mStatus = PERMISSION_DENIED;
1037 } else {
Andy Hungfe726a62018-09-27 15:17:25 -07001038 mLock.unlock();
Eric Laurent951f4552014-05-20 10:48:17 -07001039 command->mStatus = af->releaseAudioPatch(data->mHandle);
Andy Hungfe726a62018-09-27 15:17:25 -07001040 mLock.lock();
Eric Laurent951f4552014-05-20 10:48:17 -07001041 }
1042 } break;
Eric Laurentb52c1522014-05-20 11:27:36 -07001043 case UPDATE_AUDIOPORT_LIST: {
1044 ALOGV("AudioCommandThread() processing update audio port list");
Eric Laurent59a89232014-06-08 14:14:17 -07001045 svc = mService.promote();
Eric Laurentb52c1522014-05-20 11:27:36 -07001046 if (svc == 0) {
1047 break;
1048 }
1049 mLock.unlock();
1050 svc->doOnAudioPortListUpdate();
1051 mLock.lock();
1052 }break;
1053 case UPDATE_AUDIOPATCH_LIST: {
1054 ALOGV("AudioCommandThread() processing update audio patch list");
Eric Laurent59a89232014-06-08 14:14:17 -07001055 svc = mService.promote();
Eric Laurentb52c1522014-05-20 11:27:36 -07001056 if (svc == 0) {
1057 break;
1058 }
1059 mLock.unlock();
1060 svc->doOnAudioPatchListUpdate();
1061 mLock.lock();
1062 }break;
Eric Laurente1715a42014-05-20 11:30:42 -07001063 case SET_AUDIOPORT_CONFIG: {
1064 SetAudioPortConfigData *data = (SetAudioPortConfigData *)command->mParam.get();
1065 ALOGV("AudioCommandThread() processing set port config");
1066 sp<IAudioFlinger> af = AudioSystem::get_audio_flinger();
1067 if (af == 0) {
1068 command->mStatus = PERMISSION_DENIED;
1069 } else {
Andy Hungfe726a62018-09-27 15:17:25 -07001070 mLock.unlock();
Eric Laurente1715a42014-05-20 11:30:42 -07001071 command->mStatus = af->setAudioPortConfig(&data->mConfig);
Andy Hungfe726a62018-09-27 15:17:25 -07001072 mLock.lock();
Eric Laurente1715a42014-05-20 11:30:42 -07001073 }
1074 } break;
Jean-Michel Trivide801052015-04-14 19:10:14 -07001075 case DYN_POLICY_MIX_STATE_UPDATE: {
1076 DynPolicyMixStateUpdateData *data =
1077 (DynPolicyMixStateUpdateData *)command->mParam.get();
Jean-Michel Trivide801052015-04-14 19:10:14 -07001078 ALOGV("AudioCommandThread() processing dyn policy mix state update %s %d",
1079 data->mRegId.string(), data->mState);
1080 svc = mService.promote();
1081 if (svc == 0) {
1082 break;
1083 }
1084 mLock.unlock();
1085 svc->doOnDynamicPolicyMixStateUpdate(data->mRegId, data->mState);
1086 mLock.lock();
1087 } break;
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -08001088 case RECORDING_CONFIGURATION_UPDATE: {
1089 RecordingConfigurationUpdateData *data =
1090 (RecordingConfigurationUpdateData *)command->mParam.get();
1091 ALOGV("AudioCommandThread() processing recording configuration update");
1092 svc = mService.promote();
1093 if (svc == 0) {
1094 break;
1095 }
1096 mLock.unlock();
Jean-Michel Triviac4e4292016-12-22 11:39:31 -08001097 svc->doOnRecordingConfigurationUpdate(data->mEvent, &data->mClientInfo,
Eric Laurenta9f86652018-11-28 17:23:11 -08001098 &data->mClientConfig, data->mClientEffects,
1099 &data->mDeviceConfig, data->mEffects,
1100 data->mPatchHandle, data->mSource);
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -08001101 mLock.lock();
1102 } break;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001103 default:
Steve Block5ff1dd52012-01-05 23:22:43 +00001104 ALOGW("AudioCommandThread() unknown command %d", command->mCommand);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001105 }
Eric Laurent0ede8922014-05-09 18:04:42 -07001106 {
1107 Mutex::Autolock _l(command->mLock);
1108 if (command->mWaitStatus) {
1109 command->mWaitStatus = false;
1110 command->mCond.signal();
1111 }
1112 }
Eric Laurentd7eda8d2016-02-02 17:18:39 -08001113 waitTime = -1;
Zach Janga754b4f2015-10-27 01:29:34 +00001114 // release mLock before releasing strong reference on the service as
1115 // AudioPolicyService destructor calls AudioCommandThread::exit() which
1116 // acquires mLock.
1117 mLock.unlock();
1118 svc.clear();
1119 mLock.lock();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001120 } else {
1121 waitTime = mAudioCommands[0]->mTime - curTime;
1122 break;
1123 }
1124 }
Zach Janga754b4f2015-10-27 01:29:34 +00001125
1126 // release delayed commands wake lock if the queue is empty
1127 if (mAudioCommands.isEmpty()) {
Ricardo Garcia05f2fdc2014-07-24 15:48:24 -07001128 release_wake_lock(mName.string());
Zach Janga754b4f2015-10-27 01:29:34 +00001129 }
1130
1131 // At this stage we have either an empty command queue or the first command in the queue
1132 // has a finite delay. So unless we are exiting it is safe to wait.
1133 if (!exitPending()) {
Eric Laurent59a89232014-06-08 14:14:17 -07001134 ALOGV("AudioCommandThread() going to sleep");
Eric Laurentd7eda8d2016-02-02 17:18:39 -08001135 if (waitTime == -1) {
1136 mWaitWorkCV.wait(mLock);
1137 } else {
1138 mWaitWorkCV.waitRelative(mLock, waitTime);
1139 }
Eric Laurent59a89232014-06-08 14:14:17 -07001140 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001141 }
Ricardo Garcia05f2fdc2014-07-24 15:48:24 -07001142 // release delayed commands wake lock before quitting
1143 if (!mAudioCommands.isEmpty()) {
1144 release_wake_lock(mName.string());
1145 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001146 mLock.unlock();
1147 return false;
1148}
1149
1150status_t AudioPolicyService::AudioCommandThread::dump(int fd)
1151{
1152 const size_t SIZE = 256;
1153 char buffer[SIZE];
1154 String8 result;
1155
1156 snprintf(buffer, SIZE, "AudioCommandThread %p Dump\n", this);
1157 result.append(buffer);
1158 write(fd, result.string(), result.size());
1159
1160 bool locked = tryLock(mLock);
1161 if (!locked) {
1162 String8 result2(kCmdDeadlockedString);
1163 write(fd, result2.string(), result2.size());
1164 }
1165
1166 snprintf(buffer, SIZE, "- Commands:\n");
1167 result = String8(buffer);
1168 result.append(" Command Time Wait pParam\n");
Glenn Kasten8d6a2442012-02-08 14:04:28 -08001169 for (size_t i = 0; i < mAudioCommands.size(); i++) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07001170 mAudioCommands[i]->dump(buffer, SIZE);
1171 result.append(buffer);
1172 }
1173 result.append(" Last Command\n");
Eric Laurent0ede8922014-05-09 18:04:42 -07001174 if (mLastCommand != 0) {
1175 mLastCommand->dump(buffer, SIZE);
1176 result.append(buffer);
1177 } else {
1178 result.append(" none\n");
1179 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001180
1181 write(fd, result.string(), result.size());
1182
1183 if (locked) mLock.unlock();
1184
1185 return NO_ERROR;
1186}
1187
Glenn Kastenfff6d712012-01-12 16:38:12 -08001188status_t AudioPolicyService::AudioCommandThread::volumeCommand(audio_stream_type_t stream,
Eric Laurentde070132010-07-13 04:45:46 -07001189 float volume,
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001190 audio_io_handle_t output,
Eric Laurentde070132010-07-13 04:45:46 -07001191 int delayMs)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001192{
Eric Laurent0ede8922014-05-09 18:04:42 -07001193 sp<AudioCommand> command = new AudioCommand();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001194 command->mCommand = SET_VOLUME;
Eric Laurent0ede8922014-05-09 18:04:42 -07001195 sp<VolumeData> data = new VolumeData();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001196 data->mStream = stream;
1197 data->mVolume = volume;
1198 data->mIO = output;
1199 command->mParam = data;
Eric Laurent0ede8922014-05-09 18:04:42 -07001200 command->mWaitStatus = true;
Steve Block3856b092011-10-20 11:56:00 +01001201 ALOGV("AudioCommandThread() adding set volume stream %d, volume %f, output %d",
Eric Laurentde070132010-07-13 04:45:46 -07001202 stream, volume, output);
Eric Laurent0ede8922014-05-09 18:04:42 -07001203 return sendCommand(command, delayMs);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001204}
1205
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001206status_t AudioPolicyService::AudioCommandThread::parametersCommand(audio_io_handle_t ioHandle,
Dima Zavinfce7a472011-04-19 22:30:36 -07001207 const char *keyValuePairs,
Eric Laurentde070132010-07-13 04:45:46 -07001208 int delayMs)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001209{
Eric Laurent0ede8922014-05-09 18:04:42 -07001210 sp<AudioCommand> command = new AudioCommand();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001211 command->mCommand = SET_PARAMETERS;
Eric Laurent0ede8922014-05-09 18:04:42 -07001212 sp<ParametersData> data = new ParametersData();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001213 data->mIO = ioHandle;
Dima Zavinfce7a472011-04-19 22:30:36 -07001214 data->mKeyValuePairs = String8(keyValuePairs);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001215 command->mParam = data;
Eric Laurent0ede8922014-05-09 18:04:42 -07001216 command->mWaitStatus = true;
Steve Block3856b092011-10-20 11:56:00 +01001217 ALOGV("AudioCommandThread() adding set parameter string %s, io %d ,delay %d",
Dima Zavinfce7a472011-04-19 22:30:36 -07001218 keyValuePairs, ioHandle, delayMs);
Eric Laurent0ede8922014-05-09 18:04:42 -07001219 return sendCommand(command, delayMs);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001220}
1221
1222status_t AudioPolicyService::AudioCommandThread::voiceVolumeCommand(float volume, int delayMs)
1223{
Eric Laurent0ede8922014-05-09 18:04:42 -07001224 sp<AudioCommand> command = new AudioCommand();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001225 command->mCommand = SET_VOICE_VOLUME;
Eric Laurent0ede8922014-05-09 18:04:42 -07001226 sp<VoiceVolumeData> data = new VoiceVolumeData();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001227 data->mVolume = volume;
1228 command->mParam = data;
Eric Laurent0ede8922014-05-09 18:04:42 -07001229 command->mWaitStatus = true;
Steve Block3856b092011-10-20 11:56:00 +01001230 ALOGV("AudioCommandThread() adding set voice volume volume %f", volume);
Eric Laurent0ede8922014-05-09 18:04:42 -07001231 return sendCommand(command, delayMs);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001232}
1233
Eric Laurentd7fe0862018-07-14 16:48:01 -07001234void AudioPolicyService::AudioCommandThread::stopOutputCommand(audio_port_handle_t portId)
Eric Laurentbfb1b832013-01-07 09:53:42 -08001235{
Eric Laurent0ede8922014-05-09 18:04:42 -07001236 sp<AudioCommand> command = new AudioCommand();
Eric Laurentbfb1b832013-01-07 09:53:42 -08001237 command->mCommand = STOP_OUTPUT;
Eric Laurent0ede8922014-05-09 18:04:42 -07001238 sp<StopOutputData> data = new StopOutputData();
Eric Laurentd7fe0862018-07-14 16:48:01 -07001239 data->mPortId = portId;
Jesper Tragardh48412dc2014-03-24 14:12:43 +01001240 command->mParam = data;
Eric Laurentd7fe0862018-07-14 16:48:01 -07001241 ALOGV("AudioCommandThread() adding stop output portId %d", portId);
Eric Laurent0ede8922014-05-09 18:04:42 -07001242 sendCommand(command);
Eric Laurentbfb1b832013-01-07 09:53:42 -08001243}
1244
Eric Laurentd7fe0862018-07-14 16:48:01 -07001245void AudioPolicyService::AudioCommandThread::releaseOutputCommand(audio_port_handle_t portId)
Eric Laurentbfb1b832013-01-07 09:53:42 -08001246{
Eric Laurent0ede8922014-05-09 18:04:42 -07001247 sp<AudioCommand> command = new AudioCommand();
Eric Laurentbfb1b832013-01-07 09:53:42 -08001248 command->mCommand = RELEASE_OUTPUT;
Eric Laurent0ede8922014-05-09 18:04:42 -07001249 sp<ReleaseOutputData> data = new ReleaseOutputData();
Eric Laurentd7fe0862018-07-14 16:48:01 -07001250 data->mPortId = portId;
Jesper Tragardh48412dc2014-03-24 14:12:43 +01001251 command->mParam = data;
Eric Laurentd7fe0862018-07-14 16:48:01 -07001252 ALOGV("AudioCommandThread() adding release output portId %d", portId);
Eric Laurent0ede8922014-05-09 18:04:42 -07001253 sendCommand(command);
1254}
1255
Eric Laurent951f4552014-05-20 10:48:17 -07001256status_t AudioPolicyService::AudioCommandThread::createAudioPatchCommand(
1257 const struct audio_patch *patch,
1258 audio_patch_handle_t *handle,
1259 int delayMs)
1260{
1261 status_t status = NO_ERROR;
1262
1263 sp<AudioCommand> command = new AudioCommand();
1264 command->mCommand = CREATE_AUDIO_PATCH;
1265 CreateAudioPatchData *data = new CreateAudioPatchData();
1266 data->mPatch = *patch;
1267 data->mHandle = *handle;
1268 command->mParam = data;
1269 command->mWaitStatus = true;
1270 ALOGV("AudioCommandThread() adding create patch delay %d", delayMs);
1271 status = sendCommand(command, delayMs);
1272 if (status == NO_ERROR) {
1273 *handle = data->mHandle;
1274 }
1275 return status;
1276}
1277
1278status_t AudioPolicyService::AudioCommandThread::releaseAudioPatchCommand(audio_patch_handle_t handle,
1279 int delayMs)
1280{
1281 sp<AudioCommand> command = new AudioCommand();
1282 command->mCommand = RELEASE_AUDIO_PATCH;
1283 ReleaseAudioPatchData *data = new ReleaseAudioPatchData();
1284 data->mHandle = handle;
1285 command->mParam = data;
1286 command->mWaitStatus = true;
1287 ALOGV("AudioCommandThread() adding release patch delay %d", delayMs);
1288 return sendCommand(command, delayMs);
1289}
1290
Eric Laurentb52c1522014-05-20 11:27:36 -07001291void AudioPolicyService::AudioCommandThread::updateAudioPortListCommand()
1292{
1293 sp<AudioCommand> command = new AudioCommand();
1294 command->mCommand = UPDATE_AUDIOPORT_LIST;
1295 ALOGV("AudioCommandThread() adding update audio port list");
1296 sendCommand(command);
1297}
1298
1299void AudioPolicyService::AudioCommandThread::updateAudioPatchListCommand()
1300{
1301 sp<AudioCommand>command = new AudioCommand();
1302 command->mCommand = UPDATE_AUDIOPATCH_LIST;
1303 ALOGV("AudioCommandThread() adding update audio patch list");
1304 sendCommand(command);
1305}
1306
Eric Laurente1715a42014-05-20 11:30:42 -07001307status_t AudioPolicyService::AudioCommandThread::setAudioPortConfigCommand(
1308 const struct audio_port_config *config, int delayMs)
1309{
1310 sp<AudioCommand> command = new AudioCommand();
1311 command->mCommand = SET_AUDIOPORT_CONFIG;
1312 SetAudioPortConfigData *data = new SetAudioPortConfigData();
1313 data->mConfig = *config;
1314 command->mParam = data;
1315 command->mWaitStatus = true;
1316 ALOGV("AudioCommandThread() adding set port config delay %d", delayMs);
1317 return sendCommand(command, delayMs);
1318}
1319
Jean-Michel Trivide801052015-04-14 19:10:14 -07001320void AudioPolicyService::AudioCommandThread::dynamicPolicyMixStateUpdateCommand(
Chih-Hung Hsiehe964d4e2016-08-09 14:31:32 -07001321 const String8& regId, int32_t state)
Jean-Michel Trivide801052015-04-14 19:10:14 -07001322{
1323 sp<AudioCommand> command = new AudioCommand();
1324 command->mCommand = DYN_POLICY_MIX_STATE_UPDATE;
1325 DynPolicyMixStateUpdateData *data = new DynPolicyMixStateUpdateData();
1326 data->mRegId = regId;
1327 data->mState = state;
1328 command->mParam = data;
1329 ALOGV("AudioCommandThread() sending dynamic policy mix (id=%s) state update to %d",
1330 regId.string(), state);
1331 sendCommand(command);
1332}
1333
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -08001334void AudioPolicyService::AudioCommandThread::recordingConfigurationUpdateCommand(
Eric Laurenta9f86652018-11-28 17:23:11 -08001335 int event,
1336 const record_client_info_t *clientInfo,
1337 const audio_config_base_t *clientConfig,
1338 std::vector<effect_descriptor_t> clientEffects,
1339 const audio_config_base_t *deviceConfig,
1340 std::vector<effect_descriptor_t> effects,
1341 audio_patch_handle_t patchHandle,
1342 audio_source_t source)
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -08001343{
1344 sp<AudioCommand>command = new AudioCommand();
1345 command->mCommand = RECORDING_CONFIGURATION_UPDATE;
1346 RecordingConfigurationUpdateData *data = new RecordingConfigurationUpdateData();
1347 data->mEvent = event;
Jean-Michel Triviac4e4292016-12-22 11:39:31 -08001348 data->mClientInfo = *clientInfo;
Jean-Michel Trivi7281aa92016-02-17 15:33:40 -08001349 data->mClientConfig = *clientConfig;
Eric Laurenta9f86652018-11-28 17:23:11 -08001350 data->mClientEffects = clientEffects;
Jean-Michel Trivi7281aa92016-02-17 15:33:40 -08001351 data->mDeviceConfig = *deviceConfig;
Eric Laurenta9f86652018-11-28 17:23:11 -08001352 data->mEffects = effects;
Jean-Michel Trivi8c7cf3b2016-02-25 17:08:24 -08001353 data->mPatchHandle = patchHandle;
Eric Laurenta9f86652018-11-28 17:23:11 -08001354 data->mSource = source;
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -08001355 command->mParam = data;
Jean-Michel Triviac4e4292016-12-22 11:39:31 -08001356 ALOGV("AudioCommandThread() adding recording configuration update event %d, source %d uid %u",
1357 event, clientInfo->source, clientInfo->uid);
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -08001358 sendCommand(command);
1359}
1360
Eric Laurent0ede8922014-05-09 18:04:42 -07001361status_t AudioPolicyService::AudioCommandThread::sendCommand(sp<AudioCommand>& command, int delayMs)
1362{
1363 {
1364 Mutex::Autolock _l(mLock);
1365 insertCommand_l(command, delayMs);
1366 mWaitWorkCV.signal();
1367 }
1368 Mutex::Autolock _l(command->mLock);
1369 while (command->mWaitStatus) {
1370 nsecs_t timeOutNs = kAudioCommandTimeoutNs + milliseconds(delayMs);
1371 if (command->mCond.waitRelative(command->mLock, timeOutNs) != NO_ERROR) {
1372 command->mStatus = TIMED_OUT;
1373 command->mWaitStatus = false;
1374 }
1375 }
1376 return command->mStatus;
Eric Laurentbfb1b832013-01-07 09:53:42 -08001377}
1378
Mathias Agopian65ab4712010-07-14 17:59:35 -07001379// insertCommand_l() must be called with mLock held
Eric Laurent0ede8922014-05-09 18:04:42 -07001380void AudioPolicyService::AudioCommandThread::insertCommand_l(sp<AudioCommand>& command, int delayMs)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001381{
Glenn Kasten8d6a2442012-02-08 14:04:28 -08001382 ssize_t i; // not size_t because i will count down to -1
Eric Laurent0ede8922014-05-09 18:04:42 -07001383 Vector < sp<AudioCommand> > removedCommands;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001384 command->mTime = systemTime() + milliseconds(delayMs);
1385
1386 // acquire wake lock to make sure delayed commands are processed
Eric Laurentbfb1b832013-01-07 09:53:42 -08001387 if (mAudioCommands.isEmpty()) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07001388 acquire_wake_lock(PARTIAL_WAKE_LOCK, mName.string());
1389 }
1390
1391 // check same pending commands with later time stamps and eliminate them
Ivan Lozano5ff158f2017-10-30 09:06:24 -07001392 for (i = (ssize_t)mAudioCommands.size()-1; i >= 0; i--) {
Eric Laurent0ede8922014-05-09 18:04:42 -07001393 sp<AudioCommand> command2 = mAudioCommands[i];
Mathias Agopian65ab4712010-07-14 17:59:35 -07001394 // commands are sorted by increasing time stamp: no need to scan the rest of mAudioCommands
1395 if (command2->mTime <= command->mTime) break;
Eric Laurente45b48a2014-09-04 16:40:57 -07001396
1397 // create audio patch or release audio patch commands are equivalent
1398 // with regard to filtering
1399 if ((command->mCommand == CREATE_AUDIO_PATCH) ||
1400 (command->mCommand == RELEASE_AUDIO_PATCH)) {
1401 if ((command2->mCommand != CREATE_AUDIO_PATCH) &&
1402 (command2->mCommand != RELEASE_AUDIO_PATCH)) {
1403 continue;
1404 }
1405 } else if (command2->mCommand != command->mCommand) continue;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001406
1407 switch (command->mCommand) {
1408 case SET_PARAMETERS: {
Eric Laurent0ede8922014-05-09 18:04:42 -07001409 ParametersData *data = (ParametersData *)command->mParam.get();
1410 ParametersData *data2 = (ParametersData *)command2->mParam.get();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001411 if (data->mIO != data2->mIO) break;
Steve Block3856b092011-10-20 11:56:00 +01001412 ALOGV("Comparing parameter command %s to new command %s",
Eric Laurentde070132010-07-13 04:45:46 -07001413 data2->mKeyValuePairs.string(), data->mKeyValuePairs.string());
Mathias Agopian65ab4712010-07-14 17:59:35 -07001414 AudioParameter param = AudioParameter(data->mKeyValuePairs);
1415 AudioParameter param2 = AudioParameter(data2->mKeyValuePairs);
1416 for (size_t j = 0; j < param.size(); j++) {
Glenn Kastene53b9ea2012-03-12 16:29:55 -07001417 String8 key;
1418 String8 value;
1419 param.getAt(j, key, value);
1420 for (size_t k = 0; k < param2.size(); k++) {
1421 String8 key2;
1422 String8 value2;
1423 param2.getAt(k, key2, value2);
1424 if (key2 == key) {
1425 param2.remove(key2);
1426 ALOGV("Filtering out parameter %s", key2.string());
1427 break;
1428 }
1429 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001430 }
1431 // if all keys have been filtered out, remove the command.
1432 // otherwise, update the key value pairs
1433 if (param2.size() == 0) {
1434 removedCommands.add(command2);
1435 } else {
1436 data2->mKeyValuePairs = param2.toString();
1437 }
Eric Laurent21e54562013-09-23 12:08:05 -07001438 command->mTime = command2->mTime;
1439 // force delayMs to non 0 so that code below does not request to wait for
1440 // command status as the command is now delayed
1441 delayMs = 1;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001442 } break;
1443
1444 case SET_VOLUME: {
Eric Laurent0ede8922014-05-09 18:04:42 -07001445 VolumeData *data = (VolumeData *)command->mParam.get();
1446 VolumeData *data2 = (VolumeData *)command2->mParam.get();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001447 if (data->mIO != data2->mIO) break;
1448 if (data->mStream != data2->mStream) break;
Steve Block3856b092011-10-20 11:56:00 +01001449 ALOGV("Filtering out volume command on output %d for stream %d",
Eric Laurentde070132010-07-13 04:45:46 -07001450 data->mIO, data->mStream);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001451 removedCommands.add(command2);
Eric Laurent21e54562013-09-23 12:08:05 -07001452 command->mTime = command2->mTime;
1453 // force delayMs to non 0 so that code below does not request to wait for
1454 // command status as the command is now delayed
1455 delayMs = 1;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001456 } break;
Eric Laurente45b48a2014-09-04 16:40:57 -07001457
Eric Laurentbaf35fe2016-07-27 15:36:53 -07001458 case SET_VOICE_VOLUME: {
1459 VoiceVolumeData *data = (VoiceVolumeData *)command->mParam.get();
1460 VoiceVolumeData *data2 = (VoiceVolumeData *)command2->mParam.get();
1461 ALOGV("Filtering out voice volume command value %f replaced by %f",
1462 data2->mVolume, data->mVolume);
1463 removedCommands.add(command2);
1464 command->mTime = command2->mTime;
1465 // force delayMs to non 0 so that code below does not request to wait for
1466 // command status as the command is now delayed
1467 delayMs = 1;
1468 } break;
1469
Eric Laurente45b48a2014-09-04 16:40:57 -07001470 case CREATE_AUDIO_PATCH:
1471 case RELEASE_AUDIO_PATCH: {
1472 audio_patch_handle_t handle;
Haynes Mathew Georgea2d4a6d2014-10-13 13:05:22 -07001473 struct audio_patch patch;
Eric Laurente45b48a2014-09-04 16:40:57 -07001474 if (command->mCommand == CREATE_AUDIO_PATCH) {
1475 handle = ((CreateAudioPatchData *)command->mParam.get())->mHandle;
Haynes Mathew Georgea2d4a6d2014-10-13 13:05:22 -07001476 patch = ((CreateAudioPatchData *)command->mParam.get())->mPatch;
Eric Laurente45b48a2014-09-04 16:40:57 -07001477 } else {
1478 handle = ((ReleaseAudioPatchData *)command->mParam.get())->mHandle;
Mikhail Naganov7be71d22018-05-23 16:51:46 -07001479 memset(&patch, 0, sizeof(patch));
Eric Laurente45b48a2014-09-04 16:40:57 -07001480 }
1481 audio_patch_handle_t handle2;
Haynes Mathew Georgea2d4a6d2014-10-13 13:05:22 -07001482 struct audio_patch patch2;
Eric Laurente45b48a2014-09-04 16:40:57 -07001483 if (command2->mCommand == CREATE_AUDIO_PATCH) {
1484 handle2 = ((CreateAudioPatchData *)command2->mParam.get())->mHandle;
Haynes Mathew Georgea2d4a6d2014-10-13 13:05:22 -07001485 patch2 = ((CreateAudioPatchData *)command2->mParam.get())->mPatch;
Eric Laurente45b48a2014-09-04 16:40:57 -07001486 } else {
1487 handle2 = ((ReleaseAudioPatchData *)command2->mParam.get())->mHandle;
Glenn Kastenf60b6b62015-07-06 10:53:26 -07001488 memset(&patch2, 0, sizeof(patch2));
Eric Laurente45b48a2014-09-04 16:40:57 -07001489 }
1490 if (handle != handle2) break;
Haynes Mathew Georgea2d4a6d2014-10-13 13:05:22 -07001491 /* Filter CREATE_AUDIO_PATCH commands only when they are issued for
1492 same output. */
1493 if( (command->mCommand == CREATE_AUDIO_PATCH) &&
1494 (command2->mCommand == CREATE_AUDIO_PATCH) ) {
1495 bool isOutputDiff = false;
1496 if (patch.num_sources == patch2.num_sources) {
1497 for (unsigned count = 0; count < patch.num_sources; count++) {
1498 if (patch.sources[count].id != patch2.sources[count].id) {
1499 isOutputDiff = true;
1500 break;
1501 }
1502 }
1503 if (isOutputDiff)
1504 break;
1505 }
1506 }
Eric Laurente45b48a2014-09-04 16:40:57 -07001507 ALOGV("Filtering out %s audio patch command for handle %d",
1508 (command->mCommand == CREATE_AUDIO_PATCH) ? "create" : "release", handle);
1509 removedCommands.add(command2);
1510 command->mTime = command2->mTime;
1511 // force delayMs to non 0 so that code below does not request to wait for
1512 // command status as the command is now delayed
1513 delayMs = 1;
1514 } break;
1515
Jean-Michel Trivide801052015-04-14 19:10:14 -07001516 case DYN_POLICY_MIX_STATE_UPDATE: {
1517
1518 } break;
1519
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -08001520 case RECORDING_CONFIGURATION_UPDATE: {
1521
1522 } break;
1523
Mathias Agopian65ab4712010-07-14 17:59:35 -07001524 default:
1525 break;
1526 }
1527 }
1528
1529 // remove filtered commands
1530 for (size_t j = 0; j < removedCommands.size(); j++) {
1531 // removed commands always have time stamps greater than current command
1532 for (size_t k = i + 1; k < mAudioCommands.size(); k++) {
Eric Laurent0ede8922014-05-09 18:04:42 -07001533 if (mAudioCommands[k].get() == removedCommands[j].get()) {
Steve Block3856b092011-10-20 11:56:00 +01001534 ALOGV("suppressing command: %d", mAudioCommands[k]->mCommand);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001535 mAudioCommands.removeAt(k);
1536 break;
1537 }
1538 }
1539 }
1540 removedCommands.clear();
1541
Eric Laurentaa79bef2015-01-15 14:33:51 -08001542 // Disable wait for status if delay is not 0.
1543 // Except for create audio patch command because the returned patch handle
1544 // is needed by audio policy manager
1545 if (delayMs != 0 && command->mCommand != CREATE_AUDIO_PATCH) {
Eric Laurentcec4abb2012-07-03 12:23:02 -07001546 command->mWaitStatus = false;
1547 }
Eric Laurentcec4abb2012-07-03 12:23:02 -07001548
Mathias Agopian65ab4712010-07-14 17:59:35 -07001549 // insert command at the right place according to its time stamp
Eric Laurent1e693b52014-07-09 15:03:28 -07001550 ALOGV("inserting command: %d at index %zd, num commands %zu",
1551 command->mCommand, i+1, mAudioCommands.size());
Mathias Agopian65ab4712010-07-14 17:59:35 -07001552 mAudioCommands.insertAt(command, i + 1);
1553}
1554
1555void AudioPolicyService::AudioCommandThread::exit()
1556{
Steve Block3856b092011-10-20 11:56:00 +01001557 ALOGV("AudioCommandThread::exit");
Mathias Agopian65ab4712010-07-14 17:59:35 -07001558 {
1559 AutoMutex _l(mLock);
1560 requestExit();
1561 mWaitWorkCV.signal();
1562 }
Zach Janga754b4f2015-10-27 01:29:34 +00001563 // Note that we can call it from the thread loop if all other references have been released
1564 // but it will safely return WOULD_BLOCK in this case
Mathias Agopian65ab4712010-07-14 17:59:35 -07001565 requestExitAndWait();
1566}
1567
1568void AudioPolicyService::AudioCommandThread::AudioCommand::dump(char* buffer, size_t size)
1569{
1570 snprintf(buffer, size, " %02d %06d.%03d %01u %p\n",
1571 mCommand,
1572 (int)ns2s(mTime),
1573 (int)ns2ms(mTime)%1000,
1574 mWaitStatus,
Eric Laurent0ede8922014-05-09 18:04:42 -07001575 mParam.get());
Mathias Agopian65ab4712010-07-14 17:59:35 -07001576}
1577
Dima Zavinfce7a472011-04-19 22:30:36 -07001578/******* helpers for the service_ops callbacks defined below *********/
1579void AudioPolicyService::setParameters(audio_io_handle_t ioHandle,
1580 const char *keyValuePairs,
1581 int delayMs)
1582{
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001583 mAudioCommandThread->parametersCommand(ioHandle, keyValuePairs,
Dima Zavinfce7a472011-04-19 22:30:36 -07001584 delayMs);
1585}
1586
1587int AudioPolicyService::setStreamVolume(audio_stream_type_t stream,
1588 float volume,
1589 audio_io_handle_t output,
1590 int delayMs)
1591{
Glenn Kastenfff6d712012-01-12 16:38:12 -08001592 return (int)mAudioCommandThread->volumeCommand(stream, volume,
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001593 output, delayMs);
Dima Zavinfce7a472011-04-19 22:30:36 -07001594}
1595
Dima Zavinfce7a472011-04-19 22:30:36 -07001596int AudioPolicyService::setVoiceVolume(float volume, int delayMs)
1597{
1598 return (int)mAudioCommandThread->voiceVolumeCommand(volume, delayMs);
1599}
1600
Dima Zavinfce7a472011-04-19 22:30:36 -07001601extern "C" {
Eric Laurent2d388ec2014-03-07 13:25:54 -08001602audio_module_handle_t aps_load_hw_module(void *service __unused,
1603 const char *name);
1604audio_io_handle_t aps_open_output(void *service __unused,
Eric Laurenta4c5a552012-03-29 10:12:40 -07001605 audio_devices_t *pDevices,
1606 uint32_t *pSamplingRate,
1607 audio_format_t *pFormat,
1608 audio_channel_mask_t *pChannelMask,
1609 uint32_t *pLatencyMs,
Eric Laurent2d388ec2014-03-07 13:25:54 -08001610 audio_output_flags_t flags);
Eric Laurenta4c5a552012-03-29 10:12:40 -07001611
Eric Laurent2d388ec2014-03-07 13:25:54 -08001612audio_io_handle_t aps_open_output_on_module(void *service __unused,
Eric Laurenta4c5a552012-03-29 10:12:40 -07001613 audio_module_handle_t module,
1614 audio_devices_t *pDevices,
1615 uint32_t *pSamplingRate,
1616 audio_format_t *pFormat,
1617 audio_channel_mask_t *pChannelMask,
1618 uint32_t *pLatencyMs,
Richard Fitzgeraldad3af332013-03-25 16:54:37 +00001619 audio_output_flags_t flags,
Eric Laurent2d388ec2014-03-07 13:25:54 -08001620 const audio_offload_info_t *offloadInfo);
1621audio_io_handle_t aps_open_dup_output(void *service __unused,
Dima Zavinfce7a472011-04-19 22:30:36 -07001622 audio_io_handle_t output1,
Eric Laurent2d388ec2014-03-07 13:25:54 -08001623 audio_io_handle_t output2);
1624int aps_close_output(void *service __unused, audio_io_handle_t output);
1625int aps_suspend_output(void *service __unused, audio_io_handle_t output);
1626int aps_restore_output(void *service __unused, audio_io_handle_t output);
1627audio_io_handle_t aps_open_input(void *service __unused,
Eric Laurenta4c5a552012-03-29 10:12:40 -07001628 audio_devices_t *pDevices,
1629 uint32_t *pSamplingRate,
1630 audio_format_t *pFormat,
1631 audio_channel_mask_t *pChannelMask,
Eric Laurent2d388ec2014-03-07 13:25:54 -08001632 audio_in_acoustics_t acoustics __unused);
1633audio_io_handle_t aps_open_input_on_module(void *service __unused,
Eric Laurenta4c5a552012-03-29 10:12:40 -07001634 audio_module_handle_t module,
1635 audio_devices_t *pDevices,
1636 uint32_t *pSamplingRate,
1637 audio_format_t *pFormat,
Eric Laurent2d388ec2014-03-07 13:25:54 -08001638 audio_channel_mask_t *pChannelMask);
1639int aps_close_input(void *service __unused, audio_io_handle_t input);
1640int aps_invalidate_stream(void *service __unused, audio_stream_type_t stream);
Glenn Kastend848eb42016-03-08 13:42:11 -08001641int aps_move_effects(void *service __unused, audio_session_t session,
Dima Zavinfce7a472011-04-19 22:30:36 -07001642 audio_io_handle_t src_output,
Eric Laurent2d388ec2014-03-07 13:25:54 -08001643 audio_io_handle_t dst_output);
1644char * aps_get_parameters(void *service __unused, audio_io_handle_t io_handle,
1645 const char *keys);
1646void aps_set_parameters(void *service, audio_io_handle_t io_handle,
1647 const char *kv_pairs, int delay_ms);
1648int aps_set_stream_volume(void *service, audio_stream_type_t stream,
Dima Zavinfce7a472011-04-19 22:30:36 -07001649 float volume, audio_io_handle_t output,
Eric Laurent2d388ec2014-03-07 13:25:54 -08001650 int delay_ms);
Eric Laurent2d388ec2014-03-07 13:25:54 -08001651int aps_set_voice_volume(void *service, float volume, int delay_ms);
1652};
Dima Zavinfce7a472011-04-19 22:30:36 -07001653
Mikhail Naganov1b2a7942017-12-08 10:18:09 -08001654} // namespace android