blob: 76ac191503aae914a760be863dd4f73317498725 [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
554 if (locked) mLock.unlock();
555 }
556 return NO_ERROR;
557}
558
559status_t AudioPolicyService::dumpPermissionDenial(int fd)
560{
561 const size_t SIZE = 256;
562 char buffer[SIZE];
563 String8 result;
564 snprintf(buffer, SIZE, "Permission Denial: "
565 "can't dump AudioPolicyService from pid=%d, uid=%d\n",
566 IPCThreadState::self()->getCallingPid(),
567 IPCThreadState::self()->getCallingUid());
568 result.append(buffer);
569 write(fd, result.string(), result.size());
570 return NO_ERROR;
571}
572
573status_t AudioPolicyService::onTransact(
Svet Ganovf4ddfef2018-01-16 07:37:58 -0800574 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags) {
575 switch (code) {
576 case SHELL_COMMAND_TRANSACTION: {
577 int in = data.readFileDescriptor();
578 int out = data.readFileDescriptor();
579 int err = data.readFileDescriptor();
580 int argc = data.readInt32();
581 Vector<String16> args;
582 for (int i = 0; i < argc && data.dataAvail() > 0; i++) {
583 args.add(data.readString16());
584 }
585 sp<IBinder> unusedCallback;
586 sp<IResultReceiver> resultReceiver;
587 status_t status;
588 if ((status = data.readNullableStrongBinder(&unusedCallback)) != NO_ERROR) {
589 return status;
590 }
591 if ((status = data.readNullableStrongBinder(&resultReceiver)) != NO_ERROR) {
592 return status;
593 }
594 status = shellCommand(in, out, err, args);
595 if (resultReceiver != nullptr) {
596 resultReceiver->send(status);
597 }
598 return NO_ERROR;
599 }
600 }
601
Mathias Agopian65ab4712010-07-14 17:59:35 -0700602 return BnAudioPolicyService::onTransact(code, data, reply, flags);
603}
604
Svet Ganovf4ddfef2018-01-16 07:37:58 -0800605// ------------------- Shell command implementation -------------------
606
607// NOTE: This is a remote API - make sure all args are validated
608status_t AudioPolicyService::shellCommand(int in, int out, int err, Vector<String16>& args) {
609 if (!checkCallingPermission(sManageAudioPolicyPermission, nullptr, nullptr)) {
610 return PERMISSION_DENIED;
611 }
612 if (in == BAD_TYPE || out == BAD_TYPE || err == BAD_TYPE) {
613 return BAD_VALUE;
614 }
615 if (args.size() == 3 && args[0] == String16("set-uid-state")) {
616 return handleSetUidState(args, err);
617 } else if (args.size() == 2 && args[0] == String16("reset-uid-state")) {
618 return handleResetUidState(args, err);
619 } else if (args.size() == 2 && args[0] == String16("get-uid-state")) {
620 return handleGetUidState(args, out, err);
621 } else if (args.size() == 1 && args[0] == String16("help")) {
622 printHelp(out);
623 return NO_ERROR;
624 }
625 printHelp(err);
626 return BAD_VALUE;
627}
628
629status_t AudioPolicyService::handleSetUidState(Vector<String16>& args, int err) {
630 PermissionController pc;
631 int uid = pc.getPackageUid(args[1], 0);
632 if (uid <= 0) {
633 ALOGE("Unknown package: '%s'", String8(args[1]).string());
634 dprintf(err, "Unknown package: '%s'\n", String8(args[1]).string());
635 return BAD_VALUE;
636 }
637 bool active = false;
638 if (args[2] == String16("active")) {
639 active = true;
640 } else if ((args[2] != String16("idle"))) {
641 ALOGE("Expected active or idle but got: '%s'", String8(args[2]).string());
642 return BAD_VALUE;
643 }
644 mUidPolicy->addOverrideUid(uid, active);
645 return NO_ERROR;
646}
647
648status_t AudioPolicyService::handleResetUidState(Vector<String16>& args, int err) {
649 PermissionController pc;
650 int uid = pc.getPackageUid(args[1], 0);
651 if (uid < 0) {
652 ALOGE("Unknown package: '%s'", String8(args[1]).string());
653 dprintf(err, "Unknown package: '%s'\n", String8(args[1]).string());
654 return BAD_VALUE;
655 }
656 mUidPolicy->removeOverrideUid(uid);
657 return NO_ERROR;
658}
659
660status_t AudioPolicyService::handleGetUidState(Vector<String16>& args, int out, int err) {
661 PermissionController pc;
662 int uid = pc.getPackageUid(args[1], 0);
663 if (uid < 0) {
664 ALOGE("Unknown package: '%s'", String8(args[1]).string());
665 dprintf(err, "Unknown package: '%s'\n", String8(args[1]).string());
666 return BAD_VALUE;
667 }
668 if (mUidPolicy->isUidActive(uid)) {
669 return dprintf(out, "active\n");
670 } else {
671 return dprintf(out, "idle\n");
672 }
673}
674
675status_t AudioPolicyService::printHelp(int out) {
676 return dprintf(out, "Audio policy service commands:\n"
677 " get-uid-state <PACKAGE> gets the uid state\n"
678 " set-uid-state <PACKAGE> <active|idle> overrides the uid state\n"
679 " reset-uid-state <PACKAGE> clears the uid state override\n"
680 " help print this message\n");
681}
682
683// ----------- AudioPolicyService::UidPolicy implementation ----------
684
685void AudioPolicyService::UidPolicy::registerSelf() {
686 ActivityManager am;
687 am.registerUidObserver(this, ActivityManager::UID_OBSERVER_GONE
688 | ActivityManager::UID_OBSERVER_IDLE
Eric Laurente8c8b432018-10-17 10:08:02 -0700689 | ActivityManager::UID_OBSERVER_ACTIVE
690 | ActivityManager::UID_OBSERVER_PROCSTATE,
Svet Ganovf4ddfef2018-01-16 07:37:58 -0800691 ActivityManager::PROCESS_STATE_UNKNOWN,
692 String16("audioserver"));
Mikhail Naganoveae73eb2018-04-03 16:57:36 -0700693 status_t res = am.linkToDeath(this);
694 if (!res) {
695 Mutex::Autolock _l(mLock);
696 mObserverRegistered = true;
697 } else {
698 ALOGE("UidPolicy::registerSelf linkToDeath failed: %d", res);
Eric Laurent4eb58f12018-12-07 16:41:02 -0800699
Mikhail Naganoveae73eb2018-04-03 16:57:36 -0700700 am.unregisterUidObserver(this);
701 }
Svet Ganovf4ddfef2018-01-16 07:37:58 -0800702}
703
704void AudioPolicyService::UidPolicy::unregisterSelf() {
705 ActivityManager am;
Mikhail Naganoveae73eb2018-04-03 16:57:36 -0700706 am.unlinkToDeath(this);
Svet Ganovf4ddfef2018-01-16 07:37:58 -0800707 am.unregisterUidObserver(this);
Mikhail Naganoveae73eb2018-04-03 16:57:36 -0700708 Mutex::Autolock _l(mLock);
709 mObserverRegistered = false;
Svet Ganovf4ddfef2018-01-16 07:37:58 -0800710}
711
Mikhail Naganoveae73eb2018-04-03 16:57:36 -0700712void AudioPolicyService::UidPolicy::binderDied(__unused const wp<IBinder> &who) {
713 Mutex::Autolock _l(mLock);
714 mCachedUids.clear();
715 mObserverRegistered = false;
Svet Ganovf4ddfef2018-01-16 07:37:58 -0800716}
717
Eric Laurente8c8b432018-10-17 10:08:02 -0700718void AudioPolicyService::UidPolicy::checkRegistered() {
Mikhail Naganoveae73eb2018-04-03 16:57:36 -0700719 bool needToReregister = false;
720 {
721 Mutex::Autolock _l(mLock);
722 needToReregister = !mObserverRegistered;
Svet Ganovf4ddfef2018-01-16 07:37:58 -0800723 }
Mikhail Naganoveae73eb2018-04-03 16:57:36 -0700724 if (needToReregister) {
725 // Looks like ActivityManager has died previously, attempt to re-register.
726 registerSelf();
727 }
Eric Laurente8c8b432018-10-17 10:08:02 -0700728}
729
730bool AudioPolicyService::UidPolicy::isUidActive(uid_t uid) {
731 if (isServiceUid(uid)) return true;
732 checkRegistered();
Mikhail Naganoveae73eb2018-04-03 16:57:36 -0700733 {
734 Mutex::Autolock _l(mLock);
735 auto overrideIter = mOverrideUids.find(uid);
736 if (overrideIter != mOverrideUids.end()) {
Eric Laurente8c8b432018-10-17 10:08:02 -0700737 return overrideIter->second.first;
Mikhail Naganoveae73eb2018-04-03 16:57:36 -0700738 }
739 // In an absense of the ActivityManager, assume everything to be active.
740 if (!mObserverRegistered) return true;
741 auto cacheIter = mCachedUids.find(uid);
Mikhail Naganoveba668a2018-04-05 08:13:15 -0700742 if (cacheIter != mCachedUids.end()) {
Eric Laurente8c8b432018-10-17 10:08:02 -0700743 return cacheIter->second.first;
Mikhail Naganoveae73eb2018-04-03 16:57:36 -0700744 }
745 }
746 ActivityManager am;
747 bool active = am.isUidActive(uid, String16("audioserver"));
748 {
749 Mutex::Autolock _l(mLock);
Eric Laurente8c8b432018-10-17 10:08:02 -0700750 mCachedUids.insert(std::pair<uid_t,
751 std::pair<bool, int>>(uid, std::pair<bool, int>(active,
752 ActivityManager::PROCESS_STATE_UNKNOWN)));
Mikhail Naganoveae73eb2018-04-03 16:57:36 -0700753 }
754 return active;
Svet Ganovf4ddfef2018-01-16 07:37:58 -0800755}
756
Eric Laurente8c8b432018-10-17 10:08:02 -0700757int AudioPolicyService::UidPolicy::getUidState(uid_t uid) {
758 if (isServiceUid(uid)) {
759 return ActivityManager::PROCESS_STATE_TOP;
760 }
761 checkRegistered();
762 {
763 Mutex::Autolock _l(mLock);
764 auto overrideIter = mOverrideUids.find(uid);
765 if (overrideIter != mOverrideUids.end()) {
766 if (overrideIter->second.first) {
767 if (overrideIter->second.second != ActivityManager::PROCESS_STATE_UNKNOWN) {
768 return overrideIter->second.second;
769 } else {
770 auto cacheIter = mCachedUids.find(uid);
771 if (cacheIter != mCachedUids.end()) {
772 return cacheIter->second.second;
773 }
774 }
775 }
776 return ActivityManager::PROCESS_STATE_UNKNOWN;
777 }
778 // In an absense of the ActivityManager, assume everything to be active.
779 if (!mObserverRegistered) {
780 return ActivityManager::PROCESS_STATE_TOP;
781 }
782 auto cacheIter = mCachedUids.find(uid);
783 if (cacheIter != mCachedUids.end()) {
784 if (cacheIter->second.first) {
785 return cacheIter->second.second;
786 } else {
787 return ActivityManager::PROCESS_STATE_UNKNOWN;
788 }
789 }
790 }
791 ActivityManager am;
792 bool active = am.isUidActive(uid, String16("audioserver"));
793 int state = ActivityManager::PROCESS_STATE_UNKNOWN;
794 if (active) {
795 state = am.getUidProcessState(uid, String16("audioserver"));
796 }
797 {
798 Mutex::Autolock _l(mLock);
799 mCachedUids.insert(std::pair<uid_t,
800 std::pair<bool, int>>(uid, std::pair<bool, int>(active, state)));
801 }
Eric Laurent4eb58f12018-12-07 16:41:02 -0800802
Eric Laurente8c8b432018-10-17 10:08:02 -0700803 return state;
804}
805
Mikhail Naganoveae73eb2018-04-03 16:57:36 -0700806void AudioPolicyService::UidPolicy::onUidActive(uid_t uid) {
Eric Laurente8c8b432018-10-17 10:08:02 -0700807 updateUid(&mCachedUids, uid, true, ActivityManager::PROCESS_STATE_UNKNOWN, true);
Mikhail Naganoveae73eb2018-04-03 16:57:36 -0700808}
809
810void AudioPolicyService::UidPolicy::onUidGone(uid_t uid, __unused bool disabled) {
Eric Laurente8c8b432018-10-17 10:08:02 -0700811 updateUid(&mCachedUids, uid, false, ActivityManager::PROCESS_STATE_UNKNOWN, false);
Mikhail Naganoveae73eb2018-04-03 16:57:36 -0700812}
813
814void AudioPolicyService::UidPolicy::onUidIdle(uid_t uid, __unused bool disabled) {
Eric Laurente8c8b432018-10-17 10:08:02 -0700815 updateUid(&mCachedUids, uid, false, ActivityManager::PROCESS_STATE_UNKNOWN, true);
Mikhail Naganoveae73eb2018-04-03 16:57:36 -0700816}
817
Eric Laurente8c8b432018-10-17 10:08:02 -0700818void AudioPolicyService::UidPolicy::onUidStateChanged(uid_t uid,
819 int32_t procState,
820 int64_t procStateSeq __unused) {
821 if (procState != ActivityManager::PROCESS_STATE_UNKNOWN) {
822 updateUid(&mCachedUids, uid, true, procState, true);
Mikhail Naganoveae73eb2018-04-03 16:57:36 -0700823 }
824}
825
826void AudioPolicyService::UidPolicy::updateOverrideUid(uid_t uid, bool active, bool insert) {
Eric Laurente8c8b432018-10-17 10:08:02 -0700827 updateUid(&mOverrideUids, uid, active, ActivityManager::PROCESS_STATE_UNKNOWN, insert);
828}
829
830void AudioPolicyService::UidPolicy::notifyService() {
831 sp<AudioPolicyService> service = mService.promote();
832 if (service != nullptr) {
833 service->updateUidStates();
Mikhail Naganoveae73eb2018-04-03 16:57:36 -0700834 }
835}
836
Eric Laurente8c8b432018-10-17 10:08:02 -0700837void AudioPolicyService::UidPolicy::updateUid(std::unordered_map<uid_t,
838 std::pair<bool, int>> *uids,
839 uid_t uid,
840 bool active,
841 int state,
842 bool insert) {
843 if (isServiceUid(uid)) {
844 return;
845 }
846 bool wasActive = isUidActive(uid);
847 int previousState = getUidState(uid);
Mikhail Naganoveae73eb2018-04-03 16:57:36 -0700848 {
849 Mutex::Autolock _l(mLock);
Eric Laurente8c8b432018-10-17 10:08:02 -0700850 updateUidLocked(uids, uid, active, state, insert);
Mikhail Naganoveae73eb2018-04-03 16:57:36 -0700851 }
Eric Laurente8c8b432018-10-17 10:08:02 -0700852 if (wasActive != isUidActive(uid) || state != previousState) {
853 notifyService();
854 }
Mikhail Naganoveae73eb2018-04-03 16:57:36 -0700855}
856
Eric Laurente8c8b432018-10-17 10:08:02 -0700857void AudioPolicyService::UidPolicy::updateUidLocked(std::unordered_map<uid_t,
858 std::pair<bool, int>> *uids,
859 uid_t uid,
860 bool active,
861 int state,
862 bool insert) {
Mikhail Naganoveae73eb2018-04-03 16:57:36 -0700863 auto it = uids->find(uid);
864 if (it != uids->end()) {
Mikhail Naganoveae73eb2018-04-03 16:57:36 -0700865 if (insert) {
Eric Laurente8c8b432018-10-17 10:08:02 -0700866 if (state == ActivityManager::PROCESS_STATE_UNKNOWN) {
867 it->second.first = active;
868 }
869 if (it->second.first) {
870 it->second.second = state;
871 } else {
872 it->second.second = ActivityManager::PROCESS_STATE_UNKNOWN;
873 }
Mikhail Naganoveae73eb2018-04-03 16:57:36 -0700874 } else {
875 uids->erase(it);
876 }
Eric Laurente8c8b432018-10-17 10:08:02 -0700877 } else if (insert && (state == ActivityManager::PROCESS_STATE_UNKNOWN)) {
878 uids->insert(std::pair<uid_t, std::pair<bool, int>>(uid,
879 std::pair<bool, int>(active, state)));
Mikhail Naganoveae73eb2018-04-03 16:57:36 -0700880 }
Svet Ganovf4ddfef2018-01-16 07:37:58 -0800881}
Mathias Agopian65ab4712010-07-14 17:59:35 -0700882
Eric Laurent4eb58f12018-12-07 16:41:02 -0800883bool AudioPolicyService::UidPolicy::isA11yOnTop() {
884 for (const auto &uid : mCachedUids) {
885 std::vector<uid_t>::iterator it = find(mA11yUids.begin(), mA11yUids.end(), uid.first);
886 if (it == mA11yUids.end()) {
887 continue;
888 }
Amith Yamasanibcbb3002019-01-23 13:53:33 -0800889 if (uid.second.second >= ActivityManager::PROCESS_STATE_TOP
890 && uid.second.second <= ActivityManager::PROCESS_STATE_BOUND_FOREGROUND_SERVICE) {
Eric Laurent4eb58f12018-12-07 16:41:02 -0800891 return true;
892 }
893 }
894 return false;
895}
896
Eric Laurentb78763e2018-10-17 10:08:02 -0700897bool AudioPolicyService::UidPolicy::isA11yUid(uid_t uid)
898{
899 std::vector<uid_t>::iterator it = find(mA11yUids.begin(), mA11yUids.end(), uid);
900 return it != mA11yUids.end();
901}
902
Michael Groovercfd28302018-12-11 19:16:46 -0800903// ----------- AudioPolicyService::SensorPrivacyService implementation ----------
904void AudioPolicyService::SensorPrivacyPolicy::registerSelf() {
905 SensorPrivacyManager spm;
906 mSensorPrivacyEnabled = spm.isSensorPrivacyEnabled();
907 spm.addSensorPrivacyListener(this);
908}
909
910void AudioPolicyService::SensorPrivacyPolicy::unregisterSelf() {
911 SensorPrivacyManager spm;
912 spm.removeSensorPrivacyListener(this);
913}
914
915bool AudioPolicyService::SensorPrivacyPolicy::isSensorPrivacyEnabled() {
916 return mSensorPrivacyEnabled;
917}
918
919binder::Status AudioPolicyService::SensorPrivacyPolicy::onSensorPrivacyChanged(bool enabled) {
920 mSensorPrivacyEnabled = enabled;
921 sp<AudioPolicyService> service = mService.promote();
922 if (service != nullptr) {
923 service->updateUidStates();
924 }
925 return binder::Status::ok();
926}
927
Mathias Agopian65ab4712010-07-14 17:59:35 -0700928// ----------- AudioPolicyService::AudioCommandThread implementation ----------
929
Eric Laurentbfb1b832013-01-07 09:53:42 -0800930AudioPolicyService::AudioCommandThread::AudioCommandThread(String8 name,
931 const wp<AudioPolicyService>& service)
932 : Thread(false), mName(name), mService(service)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700933{
Mathias Agopian65ab4712010-07-14 17:59:35 -0700934}
935
936
937AudioPolicyService::AudioCommandThread::~AudioCommandThread()
938{
Eric Laurentbfb1b832013-01-07 09:53:42 -0800939 if (!mAudioCommands.isEmpty()) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700940 release_wake_lock(mName.string());
941 }
942 mAudioCommands.clear();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700943}
944
945void AudioPolicyService::AudioCommandThread::onFirstRef()
946{
Eric Laurentbfb1b832013-01-07 09:53:42 -0800947 run(mName.string(), ANDROID_PRIORITY_AUDIO);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700948}
949
950bool AudioPolicyService::AudioCommandThread::threadLoop()
951{
Eric Laurentd7eda8d2016-02-02 17:18:39 -0800952 nsecs_t waitTime = -1;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700953
954 mLock.lock();
955 while (!exitPending())
956 {
Eric Laurent59a89232014-06-08 14:14:17 -0700957 sp<AudioPolicyService> svc;
958 while (!mAudioCommands.isEmpty() && !exitPending()) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700959 nsecs_t curTime = systemTime();
960 // commands are sorted by increasing time stamp: execute them from index 0 and up
961 if (mAudioCommands[0]->mTime <= curTime) {
Eric Laurent0ede8922014-05-09 18:04:42 -0700962 sp<AudioCommand> command = mAudioCommands[0];
Mathias Agopian65ab4712010-07-14 17:59:35 -0700963 mAudioCommands.removeAt(0);
Eric Laurent0ede8922014-05-09 18:04:42 -0700964 mLastCommand = command;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700965
966 switch (command->mCommand) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700967 case SET_VOLUME: {
Eric Laurent0ede8922014-05-09 18:04:42 -0700968 VolumeData *data = (VolumeData *)command->mParam.get();
Steve Block3856b092011-10-20 11:56:00 +0100969 ALOGV("AudioCommandThread() processing set volume stream %d, \
Eric Laurentde070132010-07-13 04:45:46 -0700970 volume %f, output %d", data->mStream, data->mVolume, data->mIO);
Andy Hungfe726a62018-09-27 15:17:25 -0700971 mLock.unlock();
Eric Laurentde070132010-07-13 04:45:46 -0700972 command->mStatus = AudioSystem::setStreamVolume(data->mStream,
973 data->mVolume,
974 data->mIO);
Andy Hungfe726a62018-09-27 15:17:25 -0700975 mLock.lock();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700976 }break;
977 case SET_PARAMETERS: {
Eric Laurent0ede8922014-05-09 18:04:42 -0700978 ParametersData *data = (ParametersData *)command->mParam.get();
Glenn Kastene53b9ea2012-03-12 16:29:55 -0700979 ALOGV("AudioCommandThread() processing set parameters string %s, io %d",
980 data->mKeyValuePairs.string(), data->mIO);
Andy Hungfe726a62018-09-27 15:17:25 -0700981 mLock.unlock();
Glenn Kastene53b9ea2012-03-12 16:29:55 -0700982 command->mStatus = AudioSystem::setParameters(data->mIO, data->mKeyValuePairs);
Andy Hungfe726a62018-09-27 15:17:25 -0700983 mLock.lock();
Glenn Kastene53b9ea2012-03-12 16:29:55 -0700984 }break;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700985 case SET_VOICE_VOLUME: {
Eric Laurent0ede8922014-05-09 18:04:42 -0700986 VoiceVolumeData *data = (VoiceVolumeData *)command->mParam.get();
Steve Block3856b092011-10-20 11:56:00 +0100987 ALOGV("AudioCommandThread() processing set voice volume volume %f",
Eric Laurentde070132010-07-13 04:45:46 -0700988 data->mVolume);
Andy Hungfe726a62018-09-27 15:17:25 -0700989 mLock.unlock();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700990 command->mStatus = AudioSystem::setVoiceVolume(data->mVolume);
Andy Hungfe726a62018-09-27 15:17:25 -0700991 mLock.lock();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700992 }break;
Eric Laurentbfb1b832013-01-07 09:53:42 -0800993 case STOP_OUTPUT: {
Eric Laurent0ede8922014-05-09 18:04:42 -0700994 StopOutputData *data = (StopOutputData *)command->mParam.get();
Eric Laurentd7fe0862018-07-14 16:48:01 -0700995 ALOGV("AudioCommandThread() processing stop output portId %d",
996 data->mPortId);
Eric Laurent59a89232014-06-08 14:14:17 -0700997 svc = mService.promote();
Eric Laurentbfb1b832013-01-07 09:53:42 -0800998 if (svc == 0) {
999 break;
1000 }
1001 mLock.unlock();
Eric Laurentd7fe0862018-07-14 16:48:01 -07001002 svc->doStopOutput(data->mPortId);
Eric Laurentbfb1b832013-01-07 09:53:42 -08001003 mLock.lock();
Eric Laurentbfb1b832013-01-07 09:53:42 -08001004 }break;
1005 case RELEASE_OUTPUT: {
Eric Laurent0ede8922014-05-09 18:04:42 -07001006 ReleaseOutputData *data = (ReleaseOutputData *)command->mParam.get();
Eric Laurentd7fe0862018-07-14 16:48:01 -07001007 ALOGV("AudioCommandThread() processing release output portId %d",
1008 data->mPortId);
Eric Laurent59a89232014-06-08 14:14:17 -07001009 svc = mService.promote();
Eric Laurentbfb1b832013-01-07 09:53:42 -08001010 if (svc == 0) {
1011 break;
1012 }
1013 mLock.unlock();
Eric Laurentd7fe0862018-07-14 16:48:01 -07001014 svc->doReleaseOutput(data->mPortId);
Eric Laurentbfb1b832013-01-07 09:53:42 -08001015 mLock.lock();
Eric Laurentbfb1b832013-01-07 09:53:42 -08001016 }break;
Eric Laurent951f4552014-05-20 10:48:17 -07001017 case CREATE_AUDIO_PATCH: {
1018 CreateAudioPatchData *data = (CreateAudioPatchData *)command->mParam.get();
1019 ALOGV("AudioCommandThread() processing create audio patch");
1020 sp<IAudioFlinger> af = AudioSystem::get_audio_flinger();
1021 if (af == 0) {
1022 command->mStatus = PERMISSION_DENIED;
1023 } else {
Andy Hungfe726a62018-09-27 15:17:25 -07001024 mLock.unlock();
Eric Laurent951f4552014-05-20 10:48:17 -07001025 command->mStatus = af->createAudioPatch(&data->mPatch, &data->mHandle);
Andy Hungfe726a62018-09-27 15:17:25 -07001026 mLock.lock();
Eric Laurent951f4552014-05-20 10:48:17 -07001027 }
1028 } break;
1029 case RELEASE_AUDIO_PATCH: {
1030 ReleaseAudioPatchData *data = (ReleaseAudioPatchData *)command->mParam.get();
1031 ALOGV("AudioCommandThread() processing release audio patch");
1032 sp<IAudioFlinger> af = AudioSystem::get_audio_flinger();
1033 if (af == 0) {
1034 command->mStatus = PERMISSION_DENIED;
1035 } else {
Andy Hungfe726a62018-09-27 15:17:25 -07001036 mLock.unlock();
Eric Laurent951f4552014-05-20 10:48:17 -07001037 command->mStatus = af->releaseAudioPatch(data->mHandle);
Andy Hungfe726a62018-09-27 15:17:25 -07001038 mLock.lock();
Eric Laurent951f4552014-05-20 10:48:17 -07001039 }
1040 } break;
Eric Laurentb52c1522014-05-20 11:27:36 -07001041 case UPDATE_AUDIOPORT_LIST: {
1042 ALOGV("AudioCommandThread() processing update audio port list");
Eric Laurent59a89232014-06-08 14:14:17 -07001043 svc = mService.promote();
Eric Laurentb52c1522014-05-20 11:27:36 -07001044 if (svc == 0) {
1045 break;
1046 }
1047 mLock.unlock();
1048 svc->doOnAudioPortListUpdate();
1049 mLock.lock();
1050 }break;
1051 case UPDATE_AUDIOPATCH_LIST: {
1052 ALOGV("AudioCommandThread() processing update audio patch list");
Eric Laurent59a89232014-06-08 14:14:17 -07001053 svc = mService.promote();
Eric Laurentb52c1522014-05-20 11:27:36 -07001054 if (svc == 0) {
1055 break;
1056 }
1057 mLock.unlock();
1058 svc->doOnAudioPatchListUpdate();
1059 mLock.lock();
1060 }break;
Eric Laurente1715a42014-05-20 11:30:42 -07001061 case SET_AUDIOPORT_CONFIG: {
1062 SetAudioPortConfigData *data = (SetAudioPortConfigData *)command->mParam.get();
1063 ALOGV("AudioCommandThread() processing set port config");
1064 sp<IAudioFlinger> af = AudioSystem::get_audio_flinger();
1065 if (af == 0) {
1066 command->mStatus = PERMISSION_DENIED;
1067 } else {
Andy Hungfe726a62018-09-27 15:17:25 -07001068 mLock.unlock();
Eric Laurente1715a42014-05-20 11:30:42 -07001069 command->mStatus = af->setAudioPortConfig(&data->mConfig);
Andy Hungfe726a62018-09-27 15:17:25 -07001070 mLock.lock();
Eric Laurente1715a42014-05-20 11:30:42 -07001071 }
1072 } break;
Jean-Michel Trivide801052015-04-14 19:10:14 -07001073 case DYN_POLICY_MIX_STATE_UPDATE: {
1074 DynPolicyMixStateUpdateData *data =
1075 (DynPolicyMixStateUpdateData *)command->mParam.get();
Jean-Michel Trivide801052015-04-14 19:10:14 -07001076 ALOGV("AudioCommandThread() processing dyn policy mix state update %s %d",
1077 data->mRegId.string(), data->mState);
1078 svc = mService.promote();
1079 if (svc == 0) {
1080 break;
1081 }
1082 mLock.unlock();
1083 svc->doOnDynamicPolicyMixStateUpdate(data->mRegId, data->mState);
1084 mLock.lock();
1085 } break;
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -08001086 case RECORDING_CONFIGURATION_UPDATE: {
1087 RecordingConfigurationUpdateData *data =
1088 (RecordingConfigurationUpdateData *)command->mParam.get();
1089 ALOGV("AudioCommandThread() processing recording configuration update");
1090 svc = mService.promote();
1091 if (svc == 0) {
1092 break;
1093 }
1094 mLock.unlock();
Jean-Michel Triviac4e4292016-12-22 11:39:31 -08001095 svc->doOnRecordingConfigurationUpdate(data->mEvent, &data->mClientInfo,
Eric Laurenta9f86652018-11-28 17:23:11 -08001096 &data->mClientConfig, data->mClientEffects,
1097 &data->mDeviceConfig, data->mEffects,
1098 data->mPatchHandle, data->mSource);
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -08001099 mLock.lock();
1100 } break;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001101 default:
Steve Block5ff1dd52012-01-05 23:22:43 +00001102 ALOGW("AudioCommandThread() unknown command %d", command->mCommand);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001103 }
Eric Laurent0ede8922014-05-09 18:04:42 -07001104 {
1105 Mutex::Autolock _l(command->mLock);
1106 if (command->mWaitStatus) {
1107 command->mWaitStatus = false;
1108 command->mCond.signal();
1109 }
1110 }
Eric Laurentd7eda8d2016-02-02 17:18:39 -08001111 waitTime = -1;
Zach Janga754b4f2015-10-27 01:29:34 +00001112 // release mLock before releasing strong reference on the service as
1113 // AudioPolicyService destructor calls AudioCommandThread::exit() which
1114 // acquires mLock.
1115 mLock.unlock();
1116 svc.clear();
1117 mLock.lock();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001118 } else {
1119 waitTime = mAudioCommands[0]->mTime - curTime;
1120 break;
1121 }
1122 }
Zach Janga754b4f2015-10-27 01:29:34 +00001123
1124 // release delayed commands wake lock if the queue is empty
1125 if (mAudioCommands.isEmpty()) {
Ricardo Garcia05f2fdc2014-07-24 15:48:24 -07001126 release_wake_lock(mName.string());
Zach Janga754b4f2015-10-27 01:29:34 +00001127 }
1128
1129 // At this stage we have either an empty command queue or the first command in the queue
1130 // has a finite delay. So unless we are exiting it is safe to wait.
1131 if (!exitPending()) {
Eric Laurent59a89232014-06-08 14:14:17 -07001132 ALOGV("AudioCommandThread() going to sleep");
Eric Laurentd7eda8d2016-02-02 17:18:39 -08001133 if (waitTime == -1) {
1134 mWaitWorkCV.wait(mLock);
1135 } else {
1136 mWaitWorkCV.waitRelative(mLock, waitTime);
1137 }
Eric Laurent59a89232014-06-08 14:14:17 -07001138 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001139 }
Ricardo Garcia05f2fdc2014-07-24 15:48:24 -07001140 // release delayed commands wake lock before quitting
1141 if (!mAudioCommands.isEmpty()) {
1142 release_wake_lock(mName.string());
1143 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001144 mLock.unlock();
1145 return false;
1146}
1147
1148status_t AudioPolicyService::AudioCommandThread::dump(int fd)
1149{
1150 const size_t SIZE = 256;
1151 char buffer[SIZE];
1152 String8 result;
1153
1154 snprintf(buffer, SIZE, "AudioCommandThread %p Dump\n", this);
1155 result.append(buffer);
1156 write(fd, result.string(), result.size());
1157
1158 bool locked = tryLock(mLock);
1159 if (!locked) {
1160 String8 result2(kCmdDeadlockedString);
1161 write(fd, result2.string(), result2.size());
1162 }
1163
1164 snprintf(buffer, SIZE, "- Commands:\n");
1165 result = String8(buffer);
1166 result.append(" Command Time Wait pParam\n");
Glenn Kasten8d6a2442012-02-08 14:04:28 -08001167 for (size_t i = 0; i < mAudioCommands.size(); i++) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07001168 mAudioCommands[i]->dump(buffer, SIZE);
1169 result.append(buffer);
1170 }
1171 result.append(" Last Command\n");
Eric Laurent0ede8922014-05-09 18:04:42 -07001172 if (mLastCommand != 0) {
1173 mLastCommand->dump(buffer, SIZE);
1174 result.append(buffer);
1175 } else {
1176 result.append(" none\n");
1177 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001178
1179 write(fd, result.string(), result.size());
1180
1181 if (locked) mLock.unlock();
1182
1183 return NO_ERROR;
1184}
1185
Glenn Kastenfff6d712012-01-12 16:38:12 -08001186status_t AudioPolicyService::AudioCommandThread::volumeCommand(audio_stream_type_t stream,
Eric Laurentde070132010-07-13 04:45:46 -07001187 float volume,
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001188 audio_io_handle_t output,
Eric Laurentde070132010-07-13 04:45:46 -07001189 int delayMs)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001190{
Eric Laurent0ede8922014-05-09 18:04:42 -07001191 sp<AudioCommand> command = new AudioCommand();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001192 command->mCommand = SET_VOLUME;
Eric Laurent0ede8922014-05-09 18:04:42 -07001193 sp<VolumeData> data = new VolumeData();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001194 data->mStream = stream;
1195 data->mVolume = volume;
1196 data->mIO = output;
1197 command->mParam = data;
Eric Laurent0ede8922014-05-09 18:04:42 -07001198 command->mWaitStatus = true;
Steve Block3856b092011-10-20 11:56:00 +01001199 ALOGV("AudioCommandThread() adding set volume stream %d, volume %f, output %d",
Eric Laurentde070132010-07-13 04:45:46 -07001200 stream, volume, output);
Eric Laurent0ede8922014-05-09 18:04:42 -07001201 return sendCommand(command, delayMs);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001202}
1203
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001204status_t AudioPolicyService::AudioCommandThread::parametersCommand(audio_io_handle_t ioHandle,
Dima Zavinfce7a472011-04-19 22:30:36 -07001205 const char *keyValuePairs,
Eric Laurentde070132010-07-13 04:45:46 -07001206 int delayMs)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001207{
Eric Laurent0ede8922014-05-09 18:04:42 -07001208 sp<AudioCommand> command = new AudioCommand();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001209 command->mCommand = SET_PARAMETERS;
Eric Laurent0ede8922014-05-09 18:04:42 -07001210 sp<ParametersData> data = new ParametersData();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001211 data->mIO = ioHandle;
Dima Zavinfce7a472011-04-19 22:30:36 -07001212 data->mKeyValuePairs = String8(keyValuePairs);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001213 command->mParam = data;
Eric Laurent0ede8922014-05-09 18:04:42 -07001214 command->mWaitStatus = true;
Steve Block3856b092011-10-20 11:56:00 +01001215 ALOGV("AudioCommandThread() adding set parameter string %s, io %d ,delay %d",
Dima Zavinfce7a472011-04-19 22:30:36 -07001216 keyValuePairs, ioHandle, delayMs);
Eric Laurent0ede8922014-05-09 18:04:42 -07001217 return sendCommand(command, delayMs);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001218}
1219
1220status_t AudioPolicyService::AudioCommandThread::voiceVolumeCommand(float volume, int delayMs)
1221{
Eric Laurent0ede8922014-05-09 18:04:42 -07001222 sp<AudioCommand> command = new AudioCommand();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001223 command->mCommand = SET_VOICE_VOLUME;
Eric Laurent0ede8922014-05-09 18:04:42 -07001224 sp<VoiceVolumeData> data = new VoiceVolumeData();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001225 data->mVolume = volume;
1226 command->mParam = data;
Eric Laurent0ede8922014-05-09 18:04:42 -07001227 command->mWaitStatus = true;
Steve Block3856b092011-10-20 11:56:00 +01001228 ALOGV("AudioCommandThread() adding set voice volume volume %f", volume);
Eric Laurent0ede8922014-05-09 18:04:42 -07001229 return sendCommand(command, delayMs);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001230}
1231
Eric Laurentd7fe0862018-07-14 16:48:01 -07001232void AudioPolicyService::AudioCommandThread::stopOutputCommand(audio_port_handle_t portId)
Eric Laurentbfb1b832013-01-07 09:53:42 -08001233{
Eric Laurent0ede8922014-05-09 18:04:42 -07001234 sp<AudioCommand> command = new AudioCommand();
Eric Laurentbfb1b832013-01-07 09:53:42 -08001235 command->mCommand = STOP_OUTPUT;
Eric Laurent0ede8922014-05-09 18:04:42 -07001236 sp<StopOutputData> data = new StopOutputData();
Eric Laurentd7fe0862018-07-14 16:48:01 -07001237 data->mPortId = portId;
Jesper Tragardh48412dc2014-03-24 14:12:43 +01001238 command->mParam = data;
Eric Laurentd7fe0862018-07-14 16:48:01 -07001239 ALOGV("AudioCommandThread() adding stop output portId %d", portId);
Eric Laurent0ede8922014-05-09 18:04:42 -07001240 sendCommand(command);
Eric Laurentbfb1b832013-01-07 09:53:42 -08001241}
1242
Eric Laurentd7fe0862018-07-14 16:48:01 -07001243void AudioPolicyService::AudioCommandThread::releaseOutputCommand(audio_port_handle_t portId)
Eric Laurentbfb1b832013-01-07 09:53:42 -08001244{
Eric Laurent0ede8922014-05-09 18:04:42 -07001245 sp<AudioCommand> command = new AudioCommand();
Eric Laurentbfb1b832013-01-07 09:53:42 -08001246 command->mCommand = RELEASE_OUTPUT;
Eric Laurent0ede8922014-05-09 18:04:42 -07001247 sp<ReleaseOutputData> data = new ReleaseOutputData();
Eric Laurentd7fe0862018-07-14 16:48:01 -07001248 data->mPortId = portId;
Jesper Tragardh48412dc2014-03-24 14:12:43 +01001249 command->mParam = data;
Eric Laurentd7fe0862018-07-14 16:48:01 -07001250 ALOGV("AudioCommandThread() adding release output portId %d", portId);
Eric Laurent0ede8922014-05-09 18:04:42 -07001251 sendCommand(command);
1252}
1253
Eric Laurent951f4552014-05-20 10:48:17 -07001254status_t AudioPolicyService::AudioCommandThread::createAudioPatchCommand(
1255 const struct audio_patch *patch,
1256 audio_patch_handle_t *handle,
1257 int delayMs)
1258{
1259 status_t status = NO_ERROR;
1260
1261 sp<AudioCommand> command = new AudioCommand();
1262 command->mCommand = CREATE_AUDIO_PATCH;
1263 CreateAudioPatchData *data = new CreateAudioPatchData();
1264 data->mPatch = *patch;
1265 data->mHandle = *handle;
1266 command->mParam = data;
1267 command->mWaitStatus = true;
1268 ALOGV("AudioCommandThread() adding create patch delay %d", delayMs);
1269 status = sendCommand(command, delayMs);
1270 if (status == NO_ERROR) {
1271 *handle = data->mHandle;
1272 }
1273 return status;
1274}
1275
1276status_t AudioPolicyService::AudioCommandThread::releaseAudioPatchCommand(audio_patch_handle_t handle,
1277 int delayMs)
1278{
1279 sp<AudioCommand> command = new AudioCommand();
1280 command->mCommand = RELEASE_AUDIO_PATCH;
1281 ReleaseAudioPatchData *data = new ReleaseAudioPatchData();
1282 data->mHandle = handle;
1283 command->mParam = data;
1284 command->mWaitStatus = true;
1285 ALOGV("AudioCommandThread() adding release patch delay %d", delayMs);
1286 return sendCommand(command, delayMs);
1287}
1288
Eric Laurentb52c1522014-05-20 11:27:36 -07001289void AudioPolicyService::AudioCommandThread::updateAudioPortListCommand()
1290{
1291 sp<AudioCommand> command = new AudioCommand();
1292 command->mCommand = UPDATE_AUDIOPORT_LIST;
1293 ALOGV("AudioCommandThread() adding update audio port list");
1294 sendCommand(command);
1295}
1296
1297void AudioPolicyService::AudioCommandThread::updateAudioPatchListCommand()
1298{
1299 sp<AudioCommand>command = new AudioCommand();
1300 command->mCommand = UPDATE_AUDIOPATCH_LIST;
1301 ALOGV("AudioCommandThread() adding update audio patch list");
1302 sendCommand(command);
1303}
1304
Eric Laurente1715a42014-05-20 11:30:42 -07001305status_t AudioPolicyService::AudioCommandThread::setAudioPortConfigCommand(
1306 const struct audio_port_config *config, int delayMs)
1307{
1308 sp<AudioCommand> command = new AudioCommand();
1309 command->mCommand = SET_AUDIOPORT_CONFIG;
1310 SetAudioPortConfigData *data = new SetAudioPortConfigData();
1311 data->mConfig = *config;
1312 command->mParam = data;
1313 command->mWaitStatus = true;
1314 ALOGV("AudioCommandThread() adding set port config delay %d", delayMs);
1315 return sendCommand(command, delayMs);
1316}
1317
Jean-Michel Trivide801052015-04-14 19:10:14 -07001318void AudioPolicyService::AudioCommandThread::dynamicPolicyMixStateUpdateCommand(
Chih-Hung Hsiehe964d4e2016-08-09 14:31:32 -07001319 const String8& regId, int32_t state)
Jean-Michel Trivide801052015-04-14 19:10:14 -07001320{
1321 sp<AudioCommand> command = new AudioCommand();
1322 command->mCommand = DYN_POLICY_MIX_STATE_UPDATE;
1323 DynPolicyMixStateUpdateData *data = new DynPolicyMixStateUpdateData();
1324 data->mRegId = regId;
1325 data->mState = state;
1326 command->mParam = data;
1327 ALOGV("AudioCommandThread() sending dynamic policy mix (id=%s) state update to %d",
1328 regId.string(), state);
1329 sendCommand(command);
1330}
1331
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -08001332void AudioPolicyService::AudioCommandThread::recordingConfigurationUpdateCommand(
Eric Laurenta9f86652018-11-28 17:23:11 -08001333 int event,
1334 const record_client_info_t *clientInfo,
1335 const audio_config_base_t *clientConfig,
1336 std::vector<effect_descriptor_t> clientEffects,
1337 const audio_config_base_t *deviceConfig,
1338 std::vector<effect_descriptor_t> effects,
1339 audio_patch_handle_t patchHandle,
1340 audio_source_t source)
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -08001341{
1342 sp<AudioCommand>command = new AudioCommand();
1343 command->mCommand = RECORDING_CONFIGURATION_UPDATE;
1344 RecordingConfigurationUpdateData *data = new RecordingConfigurationUpdateData();
1345 data->mEvent = event;
Jean-Michel Triviac4e4292016-12-22 11:39:31 -08001346 data->mClientInfo = *clientInfo;
Jean-Michel Trivi7281aa92016-02-17 15:33:40 -08001347 data->mClientConfig = *clientConfig;
Eric Laurenta9f86652018-11-28 17:23:11 -08001348 data->mClientEffects = clientEffects;
Jean-Michel Trivi7281aa92016-02-17 15:33:40 -08001349 data->mDeviceConfig = *deviceConfig;
Eric Laurenta9f86652018-11-28 17:23:11 -08001350 data->mEffects = effects;
Jean-Michel Trivi8c7cf3b2016-02-25 17:08:24 -08001351 data->mPatchHandle = patchHandle;
Eric Laurenta9f86652018-11-28 17:23:11 -08001352 data->mSource = source;
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -08001353 command->mParam = data;
Jean-Michel Triviac4e4292016-12-22 11:39:31 -08001354 ALOGV("AudioCommandThread() adding recording configuration update event %d, source %d uid %u",
1355 event, clientInfo->source, clientInfo->uid);
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -08001356 sendCommand(command);
1357}
1358
Eric Laurent0ede8922014-05-09 18:04:42 -07001359status_t AudioPolicyService::AudioCommandThread::sendCommand(sp<AudioCommand>& command, int delayMs)
1360{
1361 {
1362 Mutex::Autolock _l(mLock);
1363 insertCommand_l(command, delayMs);
1364 mWaitWorkCV.signal();
1365 }
1366 Mutex::Autolock _l(command->mLock);
1367 while (command->mWaitStatus) {
1368 nsecs_t timeOutNs = kAudioCommandTimeoutNs + milliseconds(delayMs);
1369 if (command->mCond.waitRelative(command->mLock, timeOutNs) != NO_ERROR) {
1370 command->mStatus = TIMED_OUT;
1371 command->mWaitStatus = false;
1372 }
1373 }
1374 return command->mStatus;
Eric Laurentbfb1b832013-01-07 09:53:42 -08001375}
1376
Mathias Agopian65ab4712010-07-14 17:59:35 -07001377// insertCommand_l() must be called with mLock held
Eric Laurent0ede8922014-05-09 18:04:42 -07001378void AudioPolicyService::AudioCommandThread::insertCommand_l(sp<AudioCommand>& command, int delayMs)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001379{
Glenn Kasten8d6a2442012-02-08 14:04:28 -08001380 ssize_t i; // not size_t because i will count down to -1
Eric Laurent0ede8922014-05-09 18:04:42 -07001381 Vector < sp<AudioCommand> > removedCommands;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001382 command->mTime = systemTime() + milliseconds(delayMs);
1383
1384 // acquire wake lock to make sure delayed commands are processed
Eric Laurentbfb1b832013-01-07 09:53:42 -08001385 if (mAudioCommands.isEmpty()) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07001386 acquire_wake_lock(PARTIAL_WAKE_LOCK, mName.string());
1387 }
1388
1389 // check same pending commands with later time stamps and eliminate them
Ivan Lozano5ff158f2017-10-30 09:06:24 -07001390 for (i = (ssize_t)mAudioCommands.size()-1; i >= 0; i--) {
Eric Laurent0ede8922014-05-09 18:04:42 -07001391 sp<AudioCommand> command2 = mAudioCommands[i];
Mathias Agopian65ab4712010-07-14 17:59:35 -07001392 // commands are sorted by increasing time stamp: no need to scan the rest of mAudioCommands
1393 if (command2->mTime <= command->mTime) break;
Eric Laurente45b48a2014-09-04 16:40:57 -07001394
1395 // create audio patch or release audio patch commands are equivalent
1396 // with regard to filtering
1397 if ((command->mCommand == CREATE_AUDIO_PATCH) ||
1398 (command->mCommand == RELEASE_AUDIO_PATCH)) {
1399 if ((command2->mCommand != CREATE_AUDIO_PATCH) &&
1400 (command2->mCommand != RELEASE_AUDIO_PATCH)) {
1401 continue;
1402 }
1403 } else if (command2->mCommand != command->mCommand) continue;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001404
1405 switch (command->mCommand) {
1406 case SET_PARAMETERS: {
Eric Laurent0ede8922014-05-09 18:04:42 -07001407 ParametersData *data = (ParametersData *)command->mParam.get();
1408 ParametersData *data2 = (ParametersData *)command2->mParam.get();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001409 if (data->mIO != data2->mIO) break;
Steve Block3856b092011-10-20 11:56:00 +01001410 ALOGV("Comparing parameter command %s to new command %s",
Eric Laurentde070132010-07-13 04:45:46 -07001411 data2->mKeyValuePairs.string(), data->mKeyValuePairs.string());
Mathias Agopian65ab4712010-07-14 17:59:35 -07001412 AudioParameter param = AudioParameter(data->mKeyValuePairs);
1413 AudioParameter param2 = AudioParameter(data2->mKeyValuePairs);
1414 for (size_t j = 0; j < param.size(); j++) {
Glenn Kastene53b9ea2012-03-12 16:29:55 -07001415 String8 key;
1416 String8 value;
1417 param.getAt(j, key, value);
1418 for (size_t k = 0; k < param2.size(); k++) {
1419 String8 key2;
1420 String8 value2;
1421 param2.getAt(k, key2, value2);
1422 if (key2 == key) {
1423 param2.remove(key2);
1424 ALOGV("Filtering out parameter %s", key2.string());
1425 break;
1426 }
1427 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001428 }
1429 // if all keys have been filtered out, remove the command.
1430 // otherwise, update the key value pairs
1431 if (param2.size() == 0) {
1432 removedCommands.add(command2);
1433 } else {
1434 data2->mKeyValuePairs = param2.toString();
1435 }
Eric Laurent21e54562013-09-23 12:08:05 -07001436 command->mTime = command2->mTime;
1437 // force delayMs to non 0 so that code below does not request to wait for
1438 // command status as the command is now delayed
1439 delayMs = 1;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001440 } break;
1441
1442 case SET_VOLUME: {
Eric Laurent0ede8922014-05-09 18:04:42 -07001443 VolumeData *data = (VolumeData *)command->mParam.get();
1444 VolumeData *data2 = (VolumeData *)command2->mParam.get();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001445 if (data->mIO != data2->mIO) break;
1446 if (data->mStream != data2->mStream) break;
Steve Block3856b092011-10-20 11:56:00 +01001447 ALOGV("Filtering out volume command on output %d for stream %d",
Eric Laurentde070132010-07-13 04:45:46 -07001448 data->mIO, data->mStream);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001449 removedCommands.add(command2);
Eric Laurent21e54562013-09-23 12:08:05 -07001450 command->mTime = command2->mTime;
1451 // force delayMs to non 0 so that code below does not request to wait for
1452 // command status as the command is now delayed
1453 delayMs = 1;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001454 } break;
Eric Laurente45b48a2014-09-04 16:40:57 -07001455
Eric Laurentbaf35fe2016-07-27 15:36:53 -07001456 case SET_VOICE_VOLUME: {
1457 VoiceVolumeData *data = (VoiceVolumeData *)command->mParam.get();
1458 VoiceVolumeData *data2 = (VoiceVolumeData *)command2->mParam.get();
1459 ALOGV("Filtering out voice volume command value %f replaced by %f",
1460 data2->mVolume, data->mVolume);
1461 removedCommands.add(command2);
1462 command->mTime = command2->mTime;
1463 // force delayMs to non 0 so that code below does not request to wait for
1464 // command status as the command is now delayed
1465 delayMs = 1;
1466 } break;
1467
Eric Laurente45b48a2014-09-04 16:40:57 -07001468 case CREATE_AUDIO_PATCH:
1469 case RELEASE_AUDIO_PATCH: {
1470 audio_patch_handle_t handle;
Haynes Mathew Georgea2d4a6d2014-10-13 13:05:22 -07001471 struct audio_patch patch;
Eric Laurente45b48a2014-09-04 16:40:57 -07001472 if (command->mCommand == CREATE_AUDIO_PATCH) {
1473 handle = ((CreateAudioPatchData *)command->mParam.get())->mHandle;
Haynes Mathew Georgea2d4a6d2014-10-13 13:05:22 -07001474 patch = ((CreateAudioPatchData *)command->mParam.get())->mPatch;
Eric Laurente45b48a2014-09-04 16:40:57 -07001475 } else {
1476 handle = ((ReleaseAudioPatchData *)command->mParam.get())->mHandle;
Mikhail Naganov7be71d22018-05-23 16:51:46 -07001477 memset(&patch, 0, sizeof(patch));
Eric Laurente45b48a2014-09-04 16:40:57 -07001478 }
1479 audio_patch_handle_t handle2;
Haynes Mathew Georgea2d4a6d2014-10-13 13:05:22 -07001480 struct audio_patch patch2;
Eric Laurente45b48a2014-09-04 16:40:57 -07001481 if (command2->mCommand == CREATE_AUDIO_PATCH) {
1482 handle2 = ((CreateAudioPatchData *)command2->mParam.get())->mHandle;
Haynes Mathew Georgea2d4a6d2014-10-13 13:05:22 -07001483 patch2 = ((CreateAudioPatchData *)command2->mParam.get())->mPatch;
Eric Laurente45b48a2014-09-04 16:40:57 -07001484 } else {
1485 handle2 = ((ReleaseAudioPatchData *)command2->mParam.get())->mHandle;
Glenn Kastenf60b6b62015-07-06 10:53:26 -07001486 memset(&patch2, 0, sizeof(patch2));
Eric Laurente45b48a2014-09-04 16:40:57 -07001487 }
1488 if (handle != handle2) break;
Haynes Mathew Georgea2d4a6d2014-10-13 13:05:22 -07001489 /* Filter CREATE_AUDIO_PATCH commands only when they are issued for
1490 same output. */
1491 if( (command->mCommand == CREATE_AUDIO_PATCH) &&
1492 (command2->mCommand == CREATE_AUDIO_PATCH) ) {
1493 bool isOutputDiff = false;
1494 if (patch.num_sources == patch2.num_sources) {
1495 for (unsigned count = 0; count < patch.num_sources; count++) {
1496 if (patch.sources[count].id != patch2.sources[count].id) {
1497 isOutputDiff = true;
1498 break;
1499 }
1500 }
1501 if (isOutputDiff)
1502 break;
1503 }
1504 }
Eric Laurente45b48a2014-09-04 16:40:57 -07001505 ALOGV("Filtering out %s audio patch command for handle %d",
1506 (command->mCommand == CREATE_AUDIO_PATCH) ? "create" : "release", handle);
1507 removedCommands.add(command2);
1508 command->mTime = command2->mTime;
1509 // force delayMs to non 0 so that code below does not request to wait for
1510 // command status as the command is now delayed
1511 delayMs = 1;
1512 } break;
1513
Jean-Michel Trivide801052015-04-14 19:10:14 -07001514 case DYN_POLICY_MIX_STATE_UPDATE: {
1515
1516 } break;
1517
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -08001518 case RECORDING_CONFIGURATION_UPDATE: {
1519
1520 } break;
1521
Mathias Agopian65ab4712010-07-14 17:59:35 -07001522 default:
1523 break;
1524 }
1525 }
1526
1527 // remove filtered commands
1528 for (size_t j = 0; j < removedCommands.size(); j++) {
1529 // removed commands always have time stamps greater than current command
1530 for (size_t k = i + 1; k < mAudioCommands.size(); k++) {
Eric Laurent0ede8922014-05-09 18:04:42 -07001531 if (mAudioCommands[k].get() == removedCommands[j].get()) {
Steve Block3856b092011-10-20 11:56:00 +01001532 ALOGV("suppressing command: %d", mAudioCommands[k]->mCommand);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001533 mAudioCommands.removeAt(k);
1534 break;
1535 }
1536 }
1537 }
1538 removedCommands.clear();
1539
Eric Laurentaa79bef2015-01-15 14:33:51 -08001540 // Disable wait for status if delay is not 0.
1541 // Except for create audio patch command because the returned patch handle
1542 // is needed by audio policy manager
1543 if (delayMs != 0 && command->mCommand != CREATE_AUDIO_PATCH) {
Eric Laurentcec4abb2012-07-03 12:23:02 -07001544 command->mWaitStatus = false;
1545 }
Eric Laurentcec4abb2012-07-03 12:23:02 -07001546
Mathias Agopian65ab4712010-07-14 17:59:35 -07001547 // insert command at the right place according to its time stamp
Eric Laurent1e693b52014-07-09 15:03:28 -07001548 ALOGV("inserting command: %d at index %zd, num commands %zu",
1549 command->mCommand, i+1, mAudioCommands.size());
Mathias Agopian65ab4712010-07-14 17:59:35 -07001550 mAudioCommands.insertAt(command, i + 1);
1551}
1552
1553void AudioPolicyService::AudioCommandThread::exit()
1554{
Steve Block3856b092011-10-20 11:56:00 +01001555 ALOGV("AudioCommandThread::exit");
Mathias Agopian65ab4712010-07-14 17:59:35 -07001556 {
1557 AutoMutex _l(mLock);
1558 requestExit();
1559 mWaitWorkCV.signal();
1560 }
Zach Janga754b4f2015-10-27 01:29:34 +00001561 // Note that we can call it from the thread loop if all other references have been released
1562 // but it will safely return WOULD_BLOCK in this case
Mathias Agopian65ab4712010-07-14 17:59:35 -07001563 requestExitAndWait();
1564}
1565
1566void AudioPolicyService::AudioCommandThread::AudioCommand::dump(char* buffer, size_t size)
1567{
1568 snprintf(buffer, size, " %02d %06d.%03d %01u %p\n",
1569 mCommand,
1570 (int)ns2s(mTime),
1571 (int)ns2ms(mTime)%1000,
1572 mWaitStatus,
Eric Laurent0ede8922014-05-09 18:04:42 -07001573 mParam.get());
Mathias Agopian65ab4712010-07-14 17:59:35 -07001574}
1575
Dima Zavinfce7a472011-04-19 22:30:36 -07001576/******* helpers for the service_ops callbacks defined below *********/
1577void AudioPolicyService::setParameters(audio_io_handle_t ioHandle,
1578 const char *keyValuePairs,
1579 int delayMs)
1580{
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001581 mAudioCommandThread->parametersCommand(ioHandle, keyValuePairs,
Dima Zavinfce7a472011-04-19 22:30:36 -07001582 delayMs);
1583}
1584
1585int AudioPolicyService::setStreamVolume(audio_stream_type_t stream,
1586 float volume,
1587 audio_io_handle_t output,
1588 int delayMs)
1589{
Glenn Kastenfff6d712012-01-12 16:38:12 -08001590 return (int)mAudioCommandThread->volumeCommand(stream, volume,
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001591 output, delayMs);
Dima Zavinfce7a472011-04-19 22:30:36 -07001592}
1593
Dima Zavinfce7a472011-04-19 22:30:36 -07001594int AudioPolicyService::setVoiceVolume(float volume, int delayMs)
1595{
1596 return (int)mAudioCommandThread->voiceVolumeCommand(volume, delayMs);
1597}
1598
Dima Zavinfce7a472011-04-19 22:30:36 -07001599extern "C" {
Eric Laurent2d388ec2014-03-07 13:25:54 -08001600audio_module_handle_t aps_load_hw_module(void *service __unused,
1601 const char *name);
1602audio_io_handle_t aps_open_output(void *service __unused,
Eric Laurenta4c5a552012-03-29 10:12:40 -07001603 audio_devices_t *pDevices,
1604 uint32_t *pSamplingRate,
1605 audio_format_t *pFormat,
1606 audio_channel_mask_t *pChannelMask,
1607 uint32_t *pLatencyMs,
Eric Laurent2d388ec2014-03-07 13:25:54 -08001608 audio_output_flags_t flags);
Eric Laurenta4c5a552012-03-29 10:12:40 -07001609
Eric Laurent2d388ec2014-03-07 13:25:54 -08001610audio_io_handle_t aps_open_output_on_module(void *service __unused,
Eric Laurenta4c5a552012-03-29 10:12:40 -07001611 audio_module_handle_t module,
1612 audio_devices_t *pDevices,
1613 uint32_t *pSamplingRate,
1614 audio_format_t *pFormat,
1615 audio_channel_mask_t *pChannelMask,
1616 uint32_t *pLatencyMs,
Richard Fitzgeraldad3af332013-03-25 16:54:37 +00001617 audio_output_flags_t flags,
Eric Laurent2d388ec2014-03-07 13:25:54 -08001618 const audio_offload_info_t *offloadInfo);
1619audio_io_handle_t aps_open_dup_output(void *service __unused,
Dima Zavinfce7a472011-04-19 22:30:36 -07001620 audio_io_handle_t output1,
Eric Laurent2d388ec2014-03-07 13:25:54 -08001621 audio_io_handle_t output2);
1622int aps_close_output(void *service __unused, audio_io_handle_t output);
1623int aps_suspend_output(void *service __unused, audio_io_handle_t output);
1624int aps_restore_output(void *service __unused, audio_io_handle_t output);
1625audio_io_handle_t aps_open_input(void *service __unused,
Eric Laurenta4c5a552012-03-29 10:12:40 -07001626 audio_devices_t *pDevices,
1627 uint32_t *pSamplingRate,
1628 audio_format_t *pFormat,
1629 audio_channel_mask_t *pChannelMask,
Eric Laurent2d388ec2014-03-07 13:25:54 -08001630 audio_in_acoustics_t acoustics __unused);
1631audio_io_handle_t aps_open_input_on_module(void *service __unused,
Eric Laurenta4c5a552012-03-29 10:12:40 -07001632 audio_module_handle_t module,
1633 audio_devices_t *pDevices,
1634 uint32_t *pSamplingRate,
1635 audio_format_t *pFormat,
Eric Laurent2d388ec2014-03-07 13:25:54 -08001636 audio_channel_mask_t *pChannelMask);
1637int aps_close_input(void *service __unused, audio_io_handle_t input);
1638int aps_invalidate_stream(void *service __unused, audio_stream_type_t stream);
Glenn Kastend848eb42016-03-08 13:42:11 -08001639int aps_move_effects(void *service __unused, audio_session_t session,
Dima Zavinfce7a472011-04-19 22:30:36 -07001640 audio_io_handle_t src_output,
Eric Laurent2d388ec2014-03-07 13:25:54 -08001641 audio_io_handle_t dst_output);
1642char * aps_get_parameters(void *service __unused, audio_io_handle_t io_handle,
1643 const char *keys);
1644void aps_set_parameters(void *service, audio_io_handle_t io_handle,
1645 const char *kv_pairs, int delay_ms);
1646int aps_set_stream_volume(void *service, audio_stream_type_t stream,
Dima Zavinfce7a472011-04-19 22:30:36 -07001647 float volume, audio_io_handle_t output,
Eric Laurent2d388ec2014-03-07 13:25:54 -08001648 int delay_ms);
Eric Laurent2d388ec2014-03-07 13:25:54 -08001649int aps_set_voice_volume(void *service, float volume, int delay_ms);
1650};
Dima Zavinfce7a472011-04-19 22:30:36 -07001651
Mikhail Naganov1b2a7942017-12-08 10:18:09 -08001652} // namespace android