blob: f233971cf052a3c202633590bc5dba1d57097ecc [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
Jean-Michel Triviac4e4292016-12-22 11:39:31 -0800218void AudioPolicyService::onRecordingConfigurationUpdate(int event,
219 const record_client_info_t *clientInfo, const audio_config_base_t *clientConfig,
Jean-Michel Trivi8c7cf3b2016-02-25 17:08:24 -0800220 const audio_config_base_t *deviceConfig, audio_patch_handle_t patchHandle)
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -0800221{
Jean-Michel Triviac4e4292016-12-22 11:39:31 -0800222 mOutputCommandThread->recordingConfigurationUpdateCommand(event, clientInfo,
Jean-Michel Trivi8c7cf3b2016-02-25 17:08:24 -0800223 clientConfig, deviceConfig, patchHandle);
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -0800224}
225
Jean-Michel Triviac4e4292016-12-22 11:39:31 -0800226void AudioPolicyService::doOnRecordingConfigurationUpdate(int event,
227 const record_client_info_t *clientInfo, const audio_config_base_t *clientConfig,
Jean-Michel Trivi8c7cf3b2016-02-25 17:08:24 -0800228 const audio_config_base_t *deviceConfig, audio_patch_handle_t patchHandle)
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -0800229{
230 Mutex::Autolock _l(mNotificationClientsLock);
231 for (size_t i = 0; i < mNotificationClients.size(); i++) {
Jean-Michel Triviac4e4292016-12-22 11:39:31 -0800232 mNotificationClients.valueAt(i)->onRecordingConfigurationUpdate(event, clientInfo,
Jean-Michel Trivi8c7cf3b2016-02-25 17:08:24 -0800233 clientConfig, deviceConfig, patchHandle);
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -0800234 }
235}
236
237status_t AudioPolicyService::clientCreateAudioPatch(const struct audio_patch *patch,
238 audio_patch_handle_t *handle,
239 int delayMs)
240{
241 return mAudioCommandThread->createAudioPatchCommand(patch, handle, delayMs);
242}
243
244status_t AudioPolicyService::clientReleaseAudioPatch(audio_patch_handle_t handle,
245 int delayMs)
246{
247 return mAudioCommandThread->releaseAudioPatchCommand(handle, delayMs);
248}
249
Eric Laurente1715a42014-05-20 11:30:42 -0700250status_t AudioPolicyService::clientSetAudioPortConfig(const struct audio_port_config *config,
251 int delayMs)
252{
253 return mAudioCommandThread->setAudioPortConfigCommand(config, delayMs);
254}
255
Eric Laurentb52c1522014-05-20 11:27:36 -0700256AudioPolicyService::NotificationClient::NotificationClient(const sp<AudioPolicyService>& service,
257 const sp<IAudioPolicyServiceClient>& client,
luochaojiang908c7d72018-06-21 14:58:04 +0800258 uid_t uid,
259 pid_t pid)
260 : mService(service), mUid(uid), mPid(pid), mAudioPolicyServiceClient(client),
Eric Laurente8726fe2015-06-26 09:39:24 -0700261 mAudioPortCallbacksEnabled(false)
Eric Laurentb52c1522014-05-20 11:27:36 -0700262{
263}
264
265AudioPolicyService::NotificationClient::~NotificationClient()
266{
267}
268
269void AudioPolicyService::NotificationClient::binderDied(const wp<IBinder>& who __unused)
270{
271 sp<NotificationClient> keep(this);
272 sp<AudioPolicyService> service = mService.promote();
273 if (service != 0) {
luochaojiang908c7d72018-06-21 14:58:04 +0800274 service->removeNotificationClient(mUid, mPid);
Eric Laurentb52c1522014-05-20 11:27:36 -0700275 }
276}
277
278void AudioPolicyService::NotificationClient::onAudioPortListUpdate()
279{
Eric Laurente8726fe2015-06-26 09:39:24 -0700280 if (mAudioPolicyServiceClient != 0 && mAudioPortCallbacksEnabled) {
Eric Laurentb52c1522014-05-20 11:27:36 -0700281 mAudioPolicyServiceClient->onAudioPortListUpdate();
282 }
283}
284
285void AudioPolicyService::NotificationClient::onAudioPatchListUpdate()
286{
Eric Laurente8726fe2015-06-26 09:39:24 -0700287 if (mAudioPolicyServiceClient != 0 && mAudioPortCallbacksEnabled) {
Eric Laurentb52c1522014-05-20 11:27:36 -0700288 mAudioPolicyServiceClient->onAudioPatchListUpdate();
289 }
290}
Eric Laurent57dae992011-07-24 13:36:09 -0700291
Jean-Michel Trivide801052015-04-14 19:10:14 -0700292void AudioPolicyService::NotificationClient::onDynamicPolicyMixStateUpdate(
Chih-Hung Hsiehe964d4e2016-08-09 14:31:32 -0700293 const String8& regId, int32_t state)
Jean-Michel Trivide801052015-04-14 19:10:14 -0700294{
Andy Hung4ef19fa2018-05-15 19:35:29 -0700295 if (mAudioPolicyServiceClient != 0 && isServiceUid(mUid)) {
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -0800296 mAudioPolicyServiceClient->onDynamicPolicyMixStateUpdate(regId, state);
297 }
298}
299
300void AudioPolicyService::NotificationClient::onRecordingConfigurationUpdate(
Jean-Michel Triviac4e4292016-12-22 11:39:31 -0800301 int event, const record_client_info_t *clientInfo,
Jean-Michel Trivi8c7cf3b2016-02-25 17:08:24 -0800302 const audio_config_base_t *clientConfig, const audio_config_base_t *deviceConfig,
303 audio_patch_handle_t patchHandle)
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -0800304{
Andy Hung4ef19fa2018-05-15 19:35:29 -0700305 if (mAudioPolicyServiceClient != 0 && isServiceUid(mUid)) {
Jean-Michel Triviac4e4292016-12-22 11:39:31 -0800306 mAudioPolicyServiceClient->onRecordingConfigurationUpdate(event, clientInfo,
Jean-Michel Trivi8c7cf3b2016-02-25 17:08:24 -0800307 clientConfig, deviceConfig, patchHandle);
Jean-Michel Trivide801052015-04-14 19:10:14 -0700308 }
309}
310
Eric Laurente8726fe2015-06-26 09:39:24 -0700311void AudioPolicyService::NotificationClient::setAudioPortCallbacksEnabled(bool enabled)
312{
313 mAudioPortCallbacksEnabled = enabled;
314}
315
316
Mathias Agopian65ab4712010-07-14 17:59:35 -0700317void AudioPolicyService::binderDied(const wp<IBinder>& who) {
Glenn Kasten411e4472012-11-02 10:00:06 -0700318 ALOGW("binderDied() %p, calling pid %d", who.unsafe_get(),
Eric Laurentde070132010-07-13 04:45:46 -0700319 IPCThreadState::self()->getCallingPid());
Mathias Agopian65ab4712010-07-14 17:59:35 -0700320}
321
322static bool tryLock(Mutex& mutex)
323{
324 bool locked = false;
325 for (int i = 0; i < kDumpLockRetries; ++i) {
326 if (mutex.tryLock() == NO_ERROR) {
327 locked = true;
328 break;
329 }
Glenn Kasten22ecc912012-01-09 08:33:38 -0800330 usleep(kDumpLockSleepUs);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700331 }
332 return locked;
333}
334
335status_t AudioPolicyService::dumpInternals(int fd)
336{
337 const size_t SIZE = 256;
338 char buffer[SIZE];
339 String8 result;
340
Eric Laurentdce54a12014-03-10 12:19:46 -0700341 snprintf(buffer, SIZE, "AudioPolicyManager: %p\n", mAudioPolicyManager);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700342 result.append(buffer);
343 snprintf(buffer, SIZE, "Command Thread: %p\n", mAudioCommandThread.get());
344 result.append(buffer);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700345
346 write(fd, result.string(), result.size());
347 return NO_ERROR;
348}
349
Eric Laurente8c8b432018-10-17 10:08:02 -0700350void AudioPolicyService::updateUidStates()
Svet Ganovf4ddfef2018-01-16 07:37:58 -0800351{
Eric Laurente8c8b432018-10-17 10:08:02 -0700352 Mutex::Autolock _l(mLock);
353 updateUidStates_l();
354}
355
356void AudioPolicyService::updateUidStates_l()
357{
Eric Laurent4eb58f12018-12-07 16:41:02 -0800358// Go over all active clients and allow capture (does not force silence) in the
359// following cases:
Eric Laurenta46bedb2018-12-07 18:01:26 -0800360// The client is the assistant
361// AND an accessibility service is on TOP
362// AND the source is VOICE_RECOGNITION or HOTWORD
363// OR uses VOICE_RECOGNITION AND is on TOP OR latest started
364// OR uses HOTWORD
365// AND there is no privacy sensitive active capture
366// OR The client is an accessibility service
367// AND is on TOP OR latest started
368// AND the source is VOICE_RECOGNITION or HOTWORD
369// OR Any other client
370// AND The assistant is not on TOP
371// AND is on TOP OR latest started
372// AND there is no privacy sensitive active capture
Eric Laurent4eb58f12018-12-07 16:41:02 -0800373//TODO: mamanage pre processing effects according to use case priority
374
375 sp<AudioRecordClient> topActive;
376 sp<AudioRecordClient> latestActive;
Eric Laurent4eb58f12018-12-07 16:41:02 -0800377 sp<AudioRecordClient> latestSensitiveActive;
Eric Laurenta46bedb2018-12-07 18:01:26 -0800378 nsecs_t topStartNs = 0;
379 nsecs_t latestStartNs = 0;
Eric Laurent4eb58f12018-12-07 16:41:02 -0800380 nsecs_t latestSensitiveStartNs = 0;
381 bool isA11yOnTop = mUidPolicy->isA11yOnTop();
382 bool isAssistantOnTop = false;
383 bool isSensitiveActive = false;
384
Michael Groovercfd28302018-12-11 19:16:46 -0800385 // if Sensor Privacy is enabled then all recordings should be silenced.
386 if (mSensorPrivacyPolicy->isSensorPrivacyEnabled()) {
387 silenceAllRecordings_l();
388 return;
389 }
390
Eric Laurente8c8b432018-10-17 10:08:02 -0700391 for (size_t i =0; i < mAudioRecordClients.size(); i++) {
392 sp<AudioRecordClient> current = mAudioRecordClients[i];
393 if (!current->active) continue;
Eric Laurent4eb58f12018-12-07 16:41:02 -0800394 if (isPrivacySensitive(current->attributes.source)) {
395 if (current->startTimeNs > latestSensitiveStartNs) {
396 latestSensitiveActive = current;
397 latestSensitiveStartNs = current->startTimeNs;
398 }
399 isSensitiveActive = true;
400 }
401 if (mUidPolicy->getUidState(current->uid) == ActivityManager::PROCESS_STATE_TOP) {
Eric Laurenta46bedb2018-12-07 18:01:26 -0800402 if (current->startTimeNs > topStartNs) {
403 topActive = current;
404 topStartNs = current->startTimeNs;
405 }
Eric Laurent4eb58f12018-12-07 16:41:02 -0800406 if (mUidPolicy->isAssistantUid(current->uid)) {
407 isAssistantOnTop = true;
408 }
409 }
410 if (current->startTimeNs > latestStartNs) {
411 latestActive = current;
412 latestStartNs = current->startTimeNs;
413 }
414 }
415
416 if (topActive == nullptr && latestActive == nullptr) {
417 return;
418 }
419
Eric Laurenta46bedb2018-12-07 18:01:26 -0800420 if (topActive != nullptr) {
421 latestActive = nullptr;
422 }
423
Eric Laurent4eb58f12018-12-07 16:41:02 -0800424 for (size_t i =0; i < mAudioRecordClients.size(); i++) {
425 sp<AudioRecordClient> current = mAudioRecordClients[i];
426 if (!current->active) continue;
427
428 audio_source_t source = current->attributes.source;
Eric Laurenta46bedb2018-12-07 18:01:26 -0800429 bool isOnTop = current == topActive;
Eric Laurent4eb58f12018-12-07 16:41:02 -0800430 bool isLatest = current == latestActive;
431 bool isLatestSensitive = current == latestSensitiveActive;
432 bool forceIdle = true;
433 if (mUidPolicy->isAssistantUid(current->uid)) {
434 if (isA11yOnTop) {
435 if (source == AUDIO_SOURCE_HOTWORD || source == AUDIO_SOURCE_VOICE_RECOGNITION) {
436 forceIdle = false;
437 }
438 } else {
Eric Laurenta46bedb2018-12-07 18:01:26 -0800439 if ((((isOnTop || isLatest) && source == AUDIO_SOURCE_VOICE_RECOGNITION) ||
Eric Laurent4eb58f12018-12-07 16:41:02 -0800440 source == AUDIO_SOURCE_HOTWORD) && !isSensitiveActive) {
441 forceIdle = false;
442 }
443 }
444 } else if (mUidPolicy->isA11yUid(current->uid)) {
Eric Laurenta46bedb2018-12-07 18:01:26 -0800445 if ((isOnTop || isLatest) &&
Eric Laurent4eb58f12018-12-07 16:41:02 -0800446 (source == AUDIO_SOURCE_VOICE_RECOGNITION || source == AUDIO_SOURCE_HOTWORD)) {
447 forceIdle = false;
448 }
449 } else {
Eric Laurenta46bedb2018-12-07 18:01:26 -0800450 if (!isAssistantOnTop && (isOnTop || isLatest) &&
Eric Laurent4eb58f12018-12-07 16:41:02 -0800451 (!isSensitiveActive || isLatestSensitive)) {
452 forceIdle = false;
453 }
454 }
455 setAppState_l(current->uid,
456 forceIdle ? APP_STATE_IDLE :
457 apmStatFromAmState(mUidPolicy->getUidState(current->uid)));
Eric Laurente8c8b432018-10-17 10:08:02 -0700458 }
459}
460
Michael Groovercfd28302018-12-11 19:16:46 -0800461void AudioPolicyService::silenceAllRecordings_l() {
462 for (size_t i = 0; i < mAudioRecordClients.size(); i++) {
463 sp<AudioRecordClient> current = mAudioRecordClients[i];
464 setAppState_l(current->uid, APP_STATE_IDLE);
465 }
466}
467
Eric Laurente8c8b432018-10-17 10:08:02 -0700468/* static */
469app_state_t AudioPolicyService::apmStatFromAmState(int amState) {
470 switch (amState) {
471 case ActivityManager::PROCESS_STATE_UNKNOWN:
472 return APP_STATE_IDLE;
473 case ActivityManager::PROCESS_STATE_TOP:
474 return APP_STATE_TOP;
475 default:
476 break;
477 }
478 return APP_STATE_FOREGROUND;
479}
480
Eric Laurent4eb58f12018-12-07 16:41:02 -0800481/* static */
482bool AudioPolicyService::isPrivacySensitive(audio_source_t source)
483{
484 switch (source) {
485 case AUDIO_SOURCE_VOICE_UPLINK:
486 case AUDIO_SOURCE_VOICE_DOWNLINK:
487 case AUDIO_SOURCE_VOICE_CALL:
488 case AUDIO_SOURCE_CAMCORDER:
489 case AUDIO_SOURCE_VOICE_COMMUNICATION:
490 return true;
491 default:
492 break;
493 }
494 return false;
495}
496
Eric Laurente8c8b432018-10-17 10:08:02 -0700497void AudioPolicyService::setAppState_l(uid_t uid, app_state_t state)
498{
499 AutoCallerClear acc;
500
501 if (mAudioPolicyManager) {
502 mAudioPolicyManager->setAppState(uid, state);
Mikhail Naganoveae73eb2018-04-03 16:57:36 -0700503 }
504 sp<IAudioFlinger> af = AudioSystem::get_audio_flinger();
505 if (af) {
Eric Laurentf32108e2018-10-04 17:22:04 -0700506 bool silenced = state == APP_STATE_IDLE;
Mikhail Naganoveae73eb2018-04-03 16:57:36 -0700507 af->setRecordSilenced(uid, silenced);
508 }
Svet Ganovf4ddfef2018-01-16 07:37:58 -0800509}
510
Glenn Kasten0f11b512014-01-31 16:18:54 -0800511status_t AudioPolicyService::dump(int fd, const Vector<String16>& args __unused)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700512{
Glenn Kasten44deb052012-02-05 18:09:08 -0800513 if (!dumpAllowed()) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700514 dumpPermissionDenial(fd);
515 } else {
516 bool locked = tryLock(mLock);
517 if (!locked) {
518 String8 result(kDeadlockedString);
519 write(fd, result.string(), result.size());
520 }
521
522 dumpInternals(fd);
Glenn Kasten9d1f02d2012-02-08 17:47:58 -0800523 if (mAudioCommandThread != 0) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700524 mAudioCommandThread->dump(fd);
525 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700526
Eric Laurentdce54a12014-03-10 12:19:46 -0700527 if (mAudioPolicyManager) {
528 mAudioPolicyManager->dump(fd);
529 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700530
531 if (locked) mLock.unlock();
532 }
533 return NO_ERROR;
534}
535
536status_t AudioPolicyService::dumpPermissionDenial(int fd)
537{
538 const size_t SIZE = 256;
539 char buffer[SIZE];
540 String8 result;
541 snprintf(buffer, SIZE, "Permission Denial: "
542 "can't dump AudioPolicyService from pid=%d, uid=%d\n",
543 IPCThreadState::self()->getCallingPid(),
544 IPCThreadState::self()->getCallingUid());
545 result.append(buffer);
546 write(fd, result.string(), result.size());
547 return NO_ERROR;
548}
549
550status_t AudioPolicyService::onTransact(
Svet Ganovf4ddfef2018-01-16 07:37:58 -0800551 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags) {
552 switch (code) {
553 case SHELL_COMMAND_TRANSACTION: {
554 int in = data.readFileDescriptor();
555 int out = data.readFileDescriptor();
556 int err = data.readFileDescriptor();
557 int argc = data.readInt32();
558 Vector<String16> args;
559 for (int i = 0; i < argc && data.dataAvail() > 0; i++) {
560 args.add(data.readString16());
561 }
562 sp<IBinder> unusedCallback;
563 sp<IResultReceiver> resultReceiver;
564 status_t status;
565 if ((status = data.readNullableStrongBinder(&unusedCallback)) != NO_ERROR) {
566 return status;
567 }
568 if ((status = data.readNullableStrongBinder(&resultReceiver)) != NO_ERROR) {
569 return status;
570 }
571 status = shellCommand(in, out, err, args);
572 if (resultReceiver != nullptr) {
573 resultReceiver->send(status);
574 }
575 return NO_ERROR;
576 }
577 }
578
Mathias Agopian65ab4712010-07-14 17:59:35 -0700579 return BnAudioPolicyService::onTransact(code, data, reply, flags);
580}
581
Svet Ganovf4ddfef2018-01-16 07:37:58 -0800582// ------------------- Shell command implementation -------------------
583
584// NOTE: This is a remote API - make sure all args are validated
585status_t AudioPolicyService::shellCommand(int in, int out, int err, Vector<String16>& args) {
586 if (!checkCallingPermission(sManageAudioPolicyPermission, nullptr, nullptr)) {
587 return PERMISSION_DENIED;
588 }
589 if (in == BAD_TYPE || out == BAD_TYPE || err == BAD_TYPE) {
590 return BAD_VALUE;
591 }
592 if (args.size() == 3 && args[0] == String16("set-uid-state")) {
593 return handleSetUidState(args, err);
594 } else if (args.size() == 2 && args[0] == String16("reset-uid-state")) {
595 return handleResetUidState(args, err);
596 } else if (args.size() == 2 && args[0] == String16("get-uid-state")) {
597 return handleGetUidState(args, out, err);
598 } else if (args.size() == 1 && args[0] == String16("help")) {
599 printHelp(out);
600 return NO_ERROR;
601 }
602 printHelp(err);
603 return BAD_VALUE;
604}
605
606status_t AudioPolicyService::handleSetUidState(Vector<String16>& args, int err) {
607 PermissionController pc;
608 int uid = pc.getPackageUid(args[1], 0);
609 if (uid <= 0) {
610 ALOGE("Unknown package: '%s'", String8(args[1]).string());
611 dprintf(err, "Unknown package: '%s'\n", String8(args[1]).string());
612 return BAD_VALUE;
613 }
614 bool active = false;
615 if (args[2] == String16("active")) {
616 active = true;
617 } else if ((args[2] != String16("idle"))) {
618 ALOGE("Expected active or idle but got: '%s'", String8(args[2]).string());
619 return BAD_VALUE;
620 }
621 mUidPolicy->addOverrideUid(uid, active);
622 return NO_ERROR;
623}
624
625status_t AudioPolicyService::handleResetUidState(Vector<String16>& args, int err) {
626 PermissionController pc;
627 int uid = pc.getPackageUid(args[1], 0);
628 if (uid < 0) {
629 ALOGE("Unknown package: '%s'", String8(args[1]).string());
630 dprintf(err, "Unknown package: '%s'\n", String8(args[1]).string());
631 return BAD_VALUE;
632 }
633 mUidPolicy->removeOverrideUid(uid);
634 return NO_ERROR;
635}
636
637status_t AudioPolicyService::handleGetUidState(Vector<String16>& args, int out, int err) {
638 PermissionController pc;
639 int uid = pc.getPackageUid(args[1], 0);
640 if (uid < 0) {
641 ALOGE("Unknown package: '%s'", String8(args[1]).string());
642 dprintf(err, "Unknown package: '%s'\n", String8(args[1]).string());
643 return BAD_VALUE;
644 }
645 if (mUidPolicy->isUidActive(uid)) {
646 return dprintf(out, "active\n");
647 } else {
648 return dprintf(out, "idle\n");
649 }
650}
651
652status_t AudioPolicyService::printHelp(int out) {
653 return dprintf(out, "Audio policy service commands:\n"
654 " get-uid-state <PACKAGE> gets the uid state\n"
655 " set-uid-state <PACKAGE> <active|idle> overrides the uid state\n"
656 " reset-uid-state <PACKAGE> clears the uid state override\n"
657 " help print this message\n");
658}
659
660// ----------- AudioPolicyService::UidPolicy implementation ----------
661
662void AudioPolicyService::UidPolicy::registerSelf() {
663 ActivityManager am;
664 am.registerUidObserver(this, ActivityManager::UID_OBSERVER_GONE
665 | ActivityManager::UID_OBSERVER_IDLE
Eric Laurente8c8b432018-10-17 10:08:02 -0700666 | ActivityManager::UID_OBSERVER_ACTIVE
667 | ActivityManager::UID_OBSERVER_PROCSTATE,
Svet Ganovf4ddfef2018-01-16 07:37:58 -0800668 ActivityManager::PROCESS_STATE_UNKNOWN,
669 String16("audioserver"));
Mikhail Naganoveae73eb2018-04-03 16:57:36 -0700670 status_t res = am.linkToDeath(this);
671 if (!res) {
672 Mutex::Autolock _l(mLock);
673 mObserverRegistered = true;
674 } else {
675 ALOGE("UidPolicy::registerSelf linkToDeath failed: %d", res);
Eric Laurent4eb58f12018-12-07 16:41:02 -0800676
Mikhail Naganoveae73eb2018-04-03 16:57:36 -0700677 am.unregisterUidObserver(this);
678 }
Svet Ganovf4ddfef2018-01-16 07:37:58 -0800679}
680
681void AudioPolicyService::UidPolicy::unregisterSelf() {
682 ActivityManager am;
Mikhail Naganoveae73eb2018-04-03 16:57:36 -0700683 am.unlinkToDeath(this);
Svet Ganovf4ddfef2018-01-16 07:37:58 -0800684 am.unregisterUidObserver(this);
Mikhail Naganoveae73eb2018-04-03 16:57:36 -0700685 Mutex::Autolock _l(mLock);
686 mObserverRegistered = false;
Svet Ganovf4ddfef2018-01-16 07:37:58 -0800687}
688
Mikhail Naganoveae73eb2018-04-03 16:57:36 -0700689void AudioPolicyService::UidPolicy::binderDied(__unused const wp<IBinder> &who) {
690 Mutex::Autolock _l(mLock);
691 mCachedUids.clear();
692 mObserverRegistered = false;
Svet Ganovf4ddfef2018-01-16 07:37:58 -0800693}
694
Eric Laurente8c8b432018-10-17 10:08:02 -0700695void AudioPolicyService::UidPolicy::checkRegistered() {
Mikhail Naganoveae73eb2018-04-03 16:57:36 -0700696 bool needToReregister = false;
697 {
698 Mutex::Autolock _l(mLock);
699 needToReregister = !mObserverRegistered;
Svet Ganovf4ddfef2018-01-16 07:37:58 -0800700 }
Mikhail Naganoveae73eb2018-04-03 16:57:36 -0700701 if (needToReregister) {
702 // Looks like ActivityManager has died previously, attempt to re-register.
703 registerSelf();
704 }
Eric Laurente8c8b432018-10-17 10:08:02 -0700705}
706
707bool AudioPolicyService::UidPolicy::isUidActive(uid_t uid) {
708 if (isServiceUid(uid)) return true;
709 checkRegistered();
Mikhail Naganoveae73eb2018-04-03 16:57:36 -0700710 {
711 Mutex::Autolock _l(mLock);
712 auto overrideIter = mOverrideUids.find(uid);
713 if (overrideIter != mOverrideUids.end()) {
Eric Laurente8c8b432018-10-17 10:08:02 -0700714 return overrideIter->second.first;
Mikhail Naganoveae73eb2018-04-03 16:57:36 -0700715 }
716 // In an absense of the ActivityManager, assume everything to be active.
717 if (!mObserverRegistered) return true;
718 auto cacheIter = mCachedUids.find(uid);
Mikhail Naganoveba668a2018-04-05 08:13:15 -0700719 if (cacheIter != mCachedUids.end()) {
Eric Laurente8c8b432018-10-17 10:08:02 -0700720 return cacheIter->second.first;
Mikhail Naganoveae73eb2018-04-03 16:57:36 -0700721 }
722 }
723 ActivityManager am;
724 bool active = am.isUidActive(uid, String16("audioserver"));
725 {
726 Mutex::Autolock _l(mLock);
Eric Laurente8c8b432018-10-17 10:08:02 -0700727 mCachedUids.insert(std::pair<uid_t,
728 std::pair<bool, int>>(uid, std::pair<bool, int>(active,
729 ActivityManager::PROCESS_STATE_UNKNOWN)));
Mikhail Naganoveae73eb2018-04-03 16:57:36 -0700730 }
731 return active;
Svet Ganovf4ddfef2018-01-16 07:37:58 -0800732}
733
Eric Laurente8c8b432018-10-17 10:08:02 -0700734int AudioPolicyService::UidPolicy::getUidState(uid_t uid) {
735 if (isServiceUid(uid)) {
736 return ActivityManager::PROCESS_STATE_TOP;
737 }
738 checkRegistered();
739 {
740 Mutex::Autolock _l(mLock);
741 auto overrideIter = mOverrideUids.find(uid);
742 if (overrideIter != mOverrideUids.end()) {
743 if (overrideIter->second.first) {
744 if (overrideIter->second.second != ActivityManager::PROCESS_STATE_UNKNOWN) {
745 return overrideIter->second.second;
746 } else {
747 auto cacheIter = mCachedUids.find(uid);
748 if (cacheIter != mCachedUids.end()) {
749 return cacheIter->second.second;
750 }
751 }
752 }
753 return ActivityManager::PROCESS_STATE_UNKNOWN;
754 }
755 // In an absense of the ActivityManager, assume everything to be active.
756 if (!mObserverRegistered) {
757 return ActivityManager::PROCESS_STATE_TOP;
758 }
759 auto cacheIter = mCachedUids.find(uid);
760 if (cacheIter != mCachedUids.end()) {
761 if (cacheIter->second.first) {
762 return cacheIter->second.second;
763 } else {
764 return ActivityManager::PROCESS_STATE_UNKNOWN;
765 }
766 }
767 }
768 ActivityManager am;
769 bool active = am.isUidActive(uid, String16("audioserver"));
770 int state = ActivityManager::PROCESS_STATE_UNKNOWN;
771 if (active) {
772 state = am.getUidProcessState(uid, String16("audioserver"));
773 }
774 {
775 Mutex::Autolock _l(mLock);
776 mCachedUids.insert(std::pair<uid_t,
777 std::pair<bool, int>>(uid, std::pair<bool, int>(active, state)));
778 }
Eric Laurent4eb58f12018-12-07 16:41:02 -0800779
Eric Laurente8c8b432018-10-17 10:08:02 -0700780 return state;
781}
782
Mikhail Naganoveae73eb2018-04-03 16:57:36 -0700783void AudioPolicyService::UidPolicy::onUidActive(uid_t uid) {
Eric Laurente8c8b432018-10-17 10:08:02 -0700784 updateUid(&mCachedUids, uid, true, ActivityManager::PROCESS_STATE_UNKNOWN, true);
Mikhail Naganoveae73eb2018-04-03 16:57:36 -0700785}
786
787void AudioPolicyService::UidPolicy::onUidGone(uid_t uid, __unused bool disabled) {
Eric Laurente8c8b432018-10-17 10:08:02 -0700788 updateUid(&mCachedUids, uid, false, ActivityManager::PROCESS_STATE_UNKNOWN, false);
Mikhail Naganoveae73eb2018-04-03 16:57:36 -0700789}
790
791void AudioPolicyService::UidPolicy::onUidIdle(uid_t uid, __unused bool disabled) {
Eric Laurente8c8b432018-10-17 10:08:02 -0700792 updateUid(&mCachedUids, uid, false, ActivityManager::PROCESS_STATE_UNKNOWN, true);
Mikhail Naganoveae73eb2018-04-03 16:57:36 -0700793}
794
Eric Laurente8c8b432018-10-17 10:08:02 -0700795void AudioPolicyService::UidPolicy::onUidStateChanged(uid_t uid,
796 int32_t procState,
797 int64_t procStateSeq __unused) {
798 if (procState != ActivityManager::PROCESS_STATE_UNKNOWN) {
799 updateUid(&mCachedUids, uid, true, procState, true);
Mikhail Naganoveae73eb2018-04-03 16:57:36 -0700800 }
801}
802
803void AudioPolicyService::UidPolicy::updateOverrideUid(uid_t uid, bool active, bool insert) {
Eric Laurente8c8b432018-10-17 10:08:02 -0700804 updateUid(&mOverrideUids, uid, active, ActivityManager::PROCESS_STATE_UNKNOWN, insert);
805}
806
807void AudioPolicyService::UidPolicy::notifyService() {
808 sp<AudioPolicyService> service = mService.promote();
809 if (service != nullptr) {
810 service->updateUidStates();
Mikhail Naganoveae73eb2018-04-03 16:57:36 -0700811 }
812}
813
Eric Laurente8c8b432018-10-17 10:08:02 -0700814void AudioPolicyService::UidPolicy::updateUid(std::unordered_map<uid_t,
815 std::pair<bool, int>> *uids,
816 uid_t uid,
817 bool active,
818 int state,
819 bool insert) {
820 if (isServiceUid(uid)) {
821 return;
822 }
823 bool wasActive = isUidActive(uid);
824 int previousState = getUidState(uid);
Mikhail Naganoveae73eb2018-04-03 16:57:36 -0700825 {
826 Mutex::Autolock _l(mLock);
Eric Laurente8c8b432018-10-17 10:08:02 -0700827 updateUidLocked(uids, uid, active, state, insert);
Mikhail Naganoveae73eb2018-04-03 16:57:36 -0700828 }
Eric Laurente8c8b432018-10-17 10:08:02 -0700829 if (wasActive != isUidActive(uid) || state != previousState) {
830 notifyService();
831 }
Mikhail Naganoveae73eb2018-04-03 16:57:36 -0700832}
833
Eric Laurente8c8b432018-10-17 10:08:02 -0700834void AudioPolicyService::UidPolicy::updateUidLocked(std::unordered_map<uid_t,
835 std::pair<bool, int>> *uids,
836 uid_t uid,
837 bool active,
838 int state,
839 bool insert) {
Mikhail Naganoveae73eb2018-04-03 16:57:36 -0700840 auto it = uids->find(uid);
841 if (it != uids->end()) {
Mikhail Naganoveae73eb2018-04-03 16:57:36 -0700842 if (insert) {
Eric Laurente8c8b432018-10-17 10:08:02 -0700843 if (state == ActivityManager::PROCESS_STATE_UNKNOWN) {
844 it->second.first = active;
845 }
846 if (it->second.first) {
847 it->second.second = state;
848 } else {
849 it->second.second = ActivityManager::PROCESS_STATE_UNKNOWN;
850 }
Mikhail Naganoveae73eb2018-04-03 16:57:36 -0700851 } else {
852 uids->erase(it);
853 }
Eric Laurente8c8b432018-10-17 10:08:02 -0700854 } else if (insert && (state == ActivityManager::PROCESS_STATE_UNKNOWN)) {
855 uids->insert(std::pair<uid_t, std::pair<bool, int>>(uid,
856 std::pair<bool, int>(active, state)));
Mikhail Naganoveae73eb2018-04-03 16:57:36 -0700857 }
Svet Ganovf4ddfef2018-01-16 07:37:58 -0800858}
Mathias Agopian65ab4712010-07-14 17:59:35 -0700859
Eric Laurent4eb58f12018-12-07 16:41:02 -0800860bool AudioPolicyService::UidPolicy::isA11yOnTop() {
861 for (const auto &uid : mCachedUids) {
862 std::vector<uid_t>::iterator it = find(mA11yUids.begin(), mA11yUids.end(), uid.first);
863 if (it == mA11yUids.end()) {
864 continue;
865 }
866 if (uid.second.second == ActivityManager::PROCESS_STATE_TOP ||
867 uid.second.second == ActivityManager::PROCESS_STATE_FOREGROUND_SERVICE ||
868 uid.second.second == ActivityManager::PROCESS_STATE_BOUND_FOREGROUND_SERVICE) {
869 return true;
870 }
871 }
872 return false;
873}
874
Eric Laurentb78763e2018-10-17 10:08:02 -0700875bool AudioPolicyService::UidPolicy::isA11yUid(uid_t uid)
876{
877 std::vector<uid_t>::iterator it = find(mA11yUids.begin(), mA11yUids.end(), uid);
878 return it != mA11yUids.end();
879}
880
Michael Groovercfd28302018-12-11 19:16:46 -0800881// ----------- AudioPolicyService::SensorPrivacyService implementation ----------
882void AudioPolicyService::SensorPrivacyPolicy::registerSelf() {
883 SensorPrivacyManager spm;
884 mSensorPrivacyEnabled = spm.isSensorPrivacyEnabled();
885 spm.addSensorPrivacyListener(this);
886}
887
888void AudioPolicyService::SensorPrivacyPolicy::unregisterSelf() {
889 SensorPrivacyManager spm;
890 spm.removeSensorPrivacyListener(this);
891}
892
893bool AudioPolicyService::SensorPrivacyPolicy::isSensorPrivacyEnabled() {
894 return mSensorPrivacyEnabled;
895}
896
897binder::Status AudioPolicyService::SensorPrivacyPolicy::onSensorPrivacyChanged(bool enabled) {
898 mSensorPrivacyEnabled = enabled;
899 sp<AudioPolicyService> service = mService.promote();
900 if (service != nullptr) {
901 service->updateUidStates();
902 }
903 return binder::Status::ok();
904}
905
Mathias Agopian65ab4712010-07-14 17:59:35 -0700906// ----------- AudioPolicyService::AudioCommandThread implementation ----------
907
Eric Laurentbfb1b832013-01-07 09:53:42 -0800908AudioPolicyService::AudioCommandThread::AudioCommandThread(String8 name,
909 const wp<AudioPolicyService>& service)
910 : Thread(false), mName(name), mService(service)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700911{
Mathias Agopian65ab4712010-07-14 17:59:35 -0700912}
913
914
915AudioPolicyService::AudioCommandThread::~AudioCommandThread()
916{
Eric Laurentbfb1b832013-01-07 09:53:42 -0800917 if (!mAudioCommands.isEmpty()) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700918 release_wake_lock(mName.string());
919 }
920 mAudioCommands.clear();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700921}
922
923void AudioPolicyService::AudioCommandThread::onFirstRef()
924{
Eric Laurentbfb1b832013-01-07 09:53:42 -0800925 run(mName.string(), ANDROID_PRIORITY_AUDIO);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700926}
927
928bool AudioPolicyService::AudioCommandThread::threadLoop()
929{
Eric Laurentd7eda8d2016-02-02 17:18:39 -0800930 nsecs_t waitTime = -1;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700931
932 mLock.lock();
933 while (!exitPending())
934 {
Eric Laurent59a89232014-06-08 14:14:17 -0700935 sp<AudioPolicyService> svc;
936 while (!mAudioCommands.isEmpty() && !exitPending()) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700937 nsecs_t curTime = systemTime();
938 // commands are sorted by increasing time stamp: execute them from index 0 and up
939 if (mAudioCommands[0]->mTime <= curTime) {
Eric Laurent0ede8922014-05-09 18:04:42 -0700940 sp<AudioCommand> command = mAudioCommands[0];
Mathias Agopian65ab4712010-07-14 17:59:35 -0700941 mAudioCommands.removeAt(0);
Eric Laurent0ede8922014-05-09 18:04:42 -0700942 mLastCommand = command;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700943
944 switch (command->mCommand) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700945 case SET_VOLUME: {
Eric Laurent0ede8922014-05-09 18:04:42 -0700946 VolumeData *data = (VolumeData *)command->mParam.get();
Steve Block3856b092011-10-20 11:56:00 +0100947 ALOGV("AudioCommandThread() processing set volume stream %d, \
Eric Laurentde070132010-07-13 04:45:46 -0700948 volume %f, output %d", data->mStream, data->mVolume, data->mIO);
Andy Hungfe726a62018-09-27 15:17:25 -0700949 mLock.unlock();
Eric Laurentde070132010-07-13 04:45:46 -0700950 command->mStatus = AudioSystem::setStreamVolume(data->mStream,
951 data->mVolume,
952 data->mIO);
Andy Hungfe726a62018-09-27 15:17:25 -0700953 mLock.lock();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700954 }break;
955 case SET_PARAMETERS: {
Eric Laurent0ede8922014-05-09 18:04:42 -0700956 ParametersData *data = (ParametersData *)command->mParam.get();
Glenn Kastene53b9ea2012-03-12 16:29:55 -0700957 ALOGV("AudioCommandThread() processing set parameters string %s, io %d",
958 data->mKeyValuePairs.string(), data->mIO);
Andy Hungfe726a62018-09-27 15:17:25 -0700959 mLock.unlock();
Glenn Kastene53b9ea2012-03-12 16:29:55 -0700960 command->mStatus = AudioSystem::setParameters(data->mIO, data->mKeyValuePairs);
Andy Hungfe726a62018-09-27 15:17:25 -0700961 mLock.lock();
Glenn Kastene53b9ea2012-03-12 16:29:55 -0700962 }break;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700963 case SET_VOICE_VOLUME: {
Eric Laurent0ede8922014-05-09 18:04:42 -0700964 VoiceVolumeData *data = (VoiceVolumeData *)command->mParam.get();
Steve Block3856b092011-10-20 11:56:00 +0100965 ALOGV("AudioCommandThread() processing set voice volume volume %f",
Eric Laurentde070132010-07-13 04:45:46 -0700966 data->mVolume);
Andy Hungfe726a62018-09-27 15:17:25 -0700967 mLock.unlock();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700968 command->mStatus = AudioSystem::setVoiceVolume(data->mVolume);
Andy Hungfe726a62018-09-27 15:17:25 -0700969 mLock.lock();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700970 }break;
Eric Laurentbfb1b832013-01-07 09:53:42 -0800971 case STOP_OUTPUT: {
Eric Laurent0ede8922014-05-09 18:04:42 -0700972 StopOutputData *data = (StopOutputData *)command->mParam.get();
Eric Laurentd7fe0862018-07-14 16:48:01 -0700973 ALOGV("AudioCommandThread() processing stop output portId %d",
974 data->mPortId);
Eric Laurent59a89232014-06-08 14:14:17 -0700975 svc = mService.promote();
Eric Laurentbfb1b832013-01-07 09:53:42 -0800976 if (svc == 0) {
977 break;
978 }
979 mLock.unlock();
Eric Laurentd7fe0862018-07-14 16:48:01 -0700980 svc->doStopOutput(data->mPortId);
Eric Laurentbfb1b832013-01-07 09:53:42 -0800981 mLock.lock();
Eric Laurentbfb1b832013-01-07 09:53:42 -0800982 }break;
983 case RELEASE_OUTPUT: {
Eric Laurent0ede8922014-05-09 18:04:42 -0700984 ReleaseOutputData *data = (ReleaseOutputData *)command->mParam.get();
Eric Laurentd7fe0862018-07-14 16:48:01 -0700985 ALOGV("AudioCommandThread() processing release output portId %d",
986 data->mPortId);
Eric Laurent59a89232014-06-08 14:14:17 -0700987 svc = mService.promote();
Eric Laurentbfb1b832013-01-07 09:53:42 -0800988 if (svc == 0) {
989 break;
990 }
991 mLock.unlock();
Eric Laurentd7fe0862018-07-14 16:48:01 -0700992 svc->doReleaseOutput(data->mPortId);
Eric Laurentbfb1b832013-01-07 09:53:42 -0800993 mLock.lock();
Eric Laurentbfb1b832013-01-07 09:53:42 -0800994 }break;
Eric Laurent951f4552014-05-20 10:48:17 -0700995 case CREATE_AUDIO_PATCH: {
996 CreateAudioPatchData *data = (CreateAudioPatchData *)command->mParam.get();
997 ALOGV("AudioCommandThread() processing create audio patch");
998 sp<IAudioFlinger> af = AudioSystem::get_audio_flinger();
999 if (af == 0) {
1000 command->mStatus = PERMISSION_DENIED;
1001 } else {
Andy Hungfe726a62018-09-27 15:17:25 -07001002 mLock.unlock();
Eric Laurent951f4552014-05-20 10:48:17 -07001003 command->mStatus = af->createAudioPatch(&data->mPatch, &data->mHandle);
Andy Hungfe726a62018-09-27 15:17:25 -07001004 mLock.lock();
Eric Laurent951f4552014-05-20 10:48:17 -07001005 }
1006 } break;
1007 case RELEASE_AUDIO_PATCH: {
1008 ReleaseAudioPatchData *data = (ReleaseAudioPatchData *)command->mParam.get();
1009 ALOGV("AudioCommandThread() processing release audio patch");
1010 sp<IAudioFlinger> af = AudioSystem::get_audio_flinger();
1011 if (af == 0) {
1012 command->mStatus = PERMISSION_DENIED;
1013 } else {
Andy Hungfe726a62018-09-27 15:17:25 -07001014 mLock.unlock();
Eric Laurent951f4552014-05-20 10:48:17 -07001015 command->mStatus = af->releaseAudioPatch(data->mHandle);
Andy Hungfe726a62018-09-27 15:17:25 -07001016 mLock.lock();
Eric Laurent951f4552014-05-20 10:48:17 -07001017 }
1018 } break;
Eric Laurentb52c1522014-05-20 11:27:36 -07001019 case UPDATE_AUDIOPORT_LIST: {
1020 ALOGV("AudioCommandThread() processing update audio port list");
Eric Laurent59a89232014-06-08 14:14:17 -07001021 svc = mService.promote();
Eric Laurentb52c1522014-05-20 11:27:36 -07001022 if (svc == 0) {
1023 break;
1024 }
1025 mLock.unlock();
1026 svc->doOnAudioPortListUpdate();
1027 mLock.lock();
1028 }break;
1029 case UPDATE_AUDIOPATCH_LIST: {
1030 ALOGV("AudioCommandThread() processing update audio patch list");
Eric Laurent59a89232014-06-08 14:14:17 -07001031 svc = mService.promote();
Eric Laurentb52c1522014-05-20 11:27:36 -07001032 if (svc == 0) {
1033 break;
1034 }
1035 mLock.unlock();
1036 svc->doOnAudioPatchListUpdate();
1037 mLock.lock();
1038 }break;
Eric Laurente1715a42014-05-20 11:30:42 -07001039 case SET_AUDIOPORT_CONFIG: {
1040 SetAudioPortConfigData *data = (SetAudioPortConfigData *)command->mParam.get();
1041 ALOGV("AudioCommandThread() processing set port config");
1042 sp<IAudioFlinger> af = AudioSystem::get_audio_flinger();
1043 if (af == 0) {
1044 command->mStatus = PERMISSION_DENIED;
1045 } else {
Andy Hungfe726a62018-09-27 15:17:25 -07001046 mLock.unlock();
Eric Laurente1715a42014-05-20 11:30:42 -07001047 command->mStatus = af->setAudioPortConfig(&data->mConfig);
Andy Hungfe726a62018-09-27 15:17:25 -07001048 mLock.lock();
Eric Laurente1715a42014-05-20 11:30:42 -07001049 }
1050 } break;
Jean-Michel Trivide801052015-04-14 19:10:14 -07001051 case DYN_POLICY_MIX_STATE_UPDATE: {
1052 DynPolicyMixStateUpdateData *data =
1053 (DynPolicyMixStateUpdateData *)command->mParam.get();
Jean-Michel Trivide801052015-04-14 19:10:14 -07001054 ALOGV("AudioCommandThread() processing dyn policy mix state update %s %d",
1055 data->mRegId.string(), data->mState);
1056 svc = mService.promote();
1057 if (svc == 0) {
1058 break;
1059 }
1060 mLock.unlock();
1061 svc->doOnDynamicPolicyMixStateUpdate(data->mRegId, data->mState);
1062 mLock.lock();
1063 } break;
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -08001064 case RECORDING_CONFIGURATION_UPDATE: {
1065 RecordingConfigurationUpdateData *data =
1066 (RecordingConfigurationUpdateData *)command->mParam.get();
1067 ALOGV("AudioCommandThread() processing recording configuration update");
1068 svc = mService.promote();
1069 if (svc == 0) {
1070 break;
1071 }
1072 mLock.unlock();
Jean-Michel Triviac4e4292016-12-22 11:39:31 -08001073 svc->doOnRecordingConfigurationUpdate(data->mEvent, &data->mClientInfo,
1074 &data->mClientConfig, &data->mDeviceConfig,
Jean-Michel Trivi8c7cf3b2016-02-25 17:08:24 -08001075 data->mPatchHandle);
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -08001076 mLock.lock();
1077 } break;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001078 default:
Steve Block5ff1dd52012-01-05 23:22:43 +00001079 ALOGW("AudioCommandThread() unknown command %d", command->mCommand);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001080 }
Eric Laurent0ede8922014-05-09 18:04:42 -07001081 {
1082 Mutex::Autolock _l(command->mLock);
1083 if (command->mWaitStatus) {
1084 command->mWaitStatus = false;
1085 command->mCond.signal();
1086 }
1087 }
Eric Laurentd7eda8d2016-02-02 17:18:39 -08001088 waitTime = -1;
Zach Janga754b4f2015-10-27 01:29:34 +00001089 // release mLock before releasing strong reference on the service as
1090 // AudioPolicyService destructor calls AudioCommandThread::exit() which
1091 // acquires mLock.
1092 mLock.unlock();
1093 svc.clear();
1094 mLock.lock();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001095 } else {
1096 waitTime = mAudioCommands[0]->mTime - curTime;
1097 break;
1098 }
1099 }
Zach Janga754b4f2015-10-27 01:29:34 +00001100
1101 // release delayed commands wake lock if the queue is empty
1102 if (mAudioCommands.isEmpty()) {
Ricardo Garcia05f2fdc2014-07-24 15:48:24 -07001103 release_wake_lock(mName.string());
Zach Janga754b4f2015-10-27 01:29:34 +00001104 }
1105
1106 // At this stage we have either an empty command queue or the first command in the queue
1107 // has a finite delay. So unless we are exiting it is safe to wait.
1108 if (!exitPending()) {
Eric Laurent59a89232014-06-08 14:14:17 -07001109 ALOGV("AudioCommandThread() going to sleep");
Eric Laurentd7eda8d2016-02-02 17:18:39 -08001110 if (waitTime == -1) {
1111 mWaitWorkCV.wait(mLock);
1112 } else {
1113 mWaitWorkCV.waitRelative(mLock, waitTime);
1114 }
Eric Laurent59a89232014-06-08 14:14:17 -07001115 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001116 }
Ricardo Garcia05f2fdc2014-07-24 15:48:24 -07001117 // release delayed commands wake lock before quitting
1118 if (!mAudioCommands.isEmpty()) {
1119 release_wake_lock(mName.string());
1120 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001121 mLock.unlock();
1122 return false;
1123}
1124
1125status_t AudioPolicyService::AudioCommandThread::dump(int fd)
1126{
1127 const size_t SIZE = 256;
1128 char buffer[SIZE];
1129 String8 result;
1130
1131 snprintf(buffer, SIZE, "AudioCommandThread %p Dump\n", this);
1132 result.append(buffer);
1133 write(fd, result.string(), result.size());
1134
1135 bool locked = tryLock(mLock);
1136 if (!locked) {
1137 String8 result2(kCmdDeadlockedString);
1138 write(fd, result2.string(), result2.size());
1139 }
1140
1141 snprintf(buffer, SIZE, "- Commands:\n");
1142 result = String8(buffer);
1143 result.append(" Command Time Wait pParam\n");
Glenn Kasten8d6a2442012-02-08 14:04:28 -08001144 for (size_t i = 0; i < mAudioCommands.size(); i++) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07001145 mAudioCommands[i]->dump(buffer, SIZE);
1146 result.append(buffer);
1147 }
1148 result.append(" Last Command\n");
Eric Laurent0ede8922014-05-09 18:04:42 -07001149 if (mLastCommand != 0) {
1150 mLastCommand->dump(buffer, SIZE);
1151 result.append(buffer);
1152 } else {
1153 result.append(" none\n");
1154 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001155
1156 write(fd, result.string(), result.size());
1157
1158 if (locked) mLock.unlock();
1159
1160 return NO_ERROR;
1161}
1162
Glenn Kastenfff6d712012-01-12 16:38:12 -08001163status_t AudioPolicyService::AudioCommandThread::volumeCommand(audio_stream_type_t stream,
Eric Laurentde070132010-07-13 04:45:46 -07001164 float volume,
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001165 audio_io_handle_t output,
Eric Laurentde070132010-07-13 04:45:46 -07001166 int delayMs)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001167{
Eric Laurent0ede8922014-05-09 18:04:42 -07001168 sp<AudioCommand> command = new AudioCommand();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001169 command->mCommand = SET_VOLUME;
Eric Laurent0ede8922014-05-09 18:04:42 -07001170 sp<VolumeData> data = new VolumeData();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001171 data->mStream = stream;
1172 data->mVolume = volume;
1173 data->mIO = output;
1174 command->mParam = data;
Eric Laurent0ede8922014-05-09 18:04:42 -07001175 command->mWaitStatus = true;
Steve Block3856b092011-10-20 11:56:00 +01001176 ALOGV("AudioCommandThread() adding set volume stream %d, volume %f, output %d",
Eric Laurentde070132010-07-13 04:45:46 -07001177 stream, volume, output);
Eric Laurent0ede8922014-05-09 18:04:42 -07001178 return sendCommand(command, delayMs);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001179}
1180
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001181status_t AudioPolicyService::AudioCommandThread::parametersCommand(audio_io_handle_t ioHandle,
Dima Zavinfce7a472011-04-19 22:30:36 -07001182 const char *keyValuePairs,
Eric Laurentde070132010-07-13 04:45:46 -07001183 int delayMs)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001184{
Eric Laurent0ede8922014-05-09 18:04:42 -07001185 sp<AudioCommand> command = new AudioCommand();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001186 command->mCommand = SET_PARAMETERS;
Eric Laurent0ede8922014-05-09 18:04:42 -07001187 sp<ParametersData> data = new ParametersData();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001188 data->mIO = ioHandle;
Dima Zavinfce7a472011-04-19 22:30:36 -07001189 data->mKeyValuePairs = String8(keyValuePairs);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001190 command->mParam = data;
Eric Laurent0ede8922014-05-09 18:04:42 -07001191 command->mWaitStatus = true;
Steve Block3856b092011-10-20 11:56:00 +01001192 ALOGV("AudioCommandThread() adding set parameter string %s, io %d ,delay %d",
Dima Zavinfce7a472011-04-19 22:30:36 -07001193 keyValuePairs, ioHandle, delayMs);
Eric Laurent0ede8922014-05-09 18:04:42 -07001194 return sendCommand(command, delayMs);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001195}
1196
1197status_t AudioPolicyService::AudioCommandThread::voiceVolumeCommand(float volume, int delayMs)
1198{
Eric Laurent0ede8922014-05-09 18:04:42 -07001199 sp<AudioCommand> command = new AudioCommand();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001200 command->mCommand = SET_VOICE_VOLUME;
Eric Laurent0ede8922014-05-09 18:04:42 -07001201 sp<VoiceVolumeData> data = new VoiceVolumeData();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001202 data->mVolume = volume;
1203 command->mParam = data;
Eric Laurent0ede8922014-05-09 18:04:42 -07001204 command->mWaitStatus = true;
Steve Block3856b092011-10-20 11:56:00 +01001205 ALOGV("AudioCommandThread() adding set voice volume volume %f", volume);
Eric Laurent0ede8922014-05-09 18:04:42 -07001206 return sendCommand(command, delayMs);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001207}
1208
Eric Laurentd7fe0862018-07-14 16:48:01 -07001209void AudioPolicyService::AudioCommandThread::stopOutputCommand(audio_port_handle_t portId)
Eric Laurentbfb1b832013-01-07 09:53:42 -08001210{
Eric Laurent0ede8922014-05-09 18:04:42 -07001211 sp<AudioCommand> command = new AudioCommand();
Eric Laurentbfb1b832013-01-07 09:53:42 -08001212 command->mCommand = STOP_OUTPUT;
Eric Laurent0ede8922014-05-09 18:04:42 -07001213 sp<StopOutputData> data = new StopOutputData();
Eric Laurentd7fe0862018-07-14 16:48:01 -07001214 data->mPortId = portId;
Jesper Tragardh48412dc2014-03-24 14:12:43 +01001215 command->mParam = data;
Eric Laurentd7fe0862018-07-14 16:48:01 -07001216 ALOGV("AudioCommandThread() adding stop output portId %d", portId);
Eric Laurent0ede8922014-05-09 18:04:42 -07001217 sendCommand(command);
Eric Laurentbfb1b832013-01-07 09:53:42 -08001218}
1219
Eric Laurentd7fe0862018-07-14 16:48:01 -07001220void AudioPolicyService::AudioCommandThread::releaseOutputCommand(audio_port_handle_t portId)
Eric Laurentbfb1b832013-01-07 09:53:42 -08001221{
Eric Laurent0ede8922014-05-09 18:04:42 -07001222 sp<AudioCommand> command = new AudioCommand();
Eric Laurentbfb1b832013-01-07 09:53:42 -08001223 command->mCommand = RELEASE_OUTPUT;
Eric Laurent0ede8922014-05-09 18:04:42 -07001224 sp<ReleaseOutputData> data = new ReleaseOutputData();
Eric Laurentd7fe0862018-07-14 16:48:01 -07001225 data->mPortId = portId;
Jesper Tragardh48412dc2014-03-24 14:12:43 +01001226 command->mParam = data;
Eric Laurentd7fe0862018-07-14 16:48:01 -07001227 ALOGV("AudioCommandThread() adding release output portId %d", portId);
Eric Laurent0ede8922014-05-09 18:04:42 -07001228 sendCommand(command);
1229}
1230
Eric Laurent951f4552014-05-20 10:48:17 -07001231status_t AudioPolicyService::AudioCommandThread::createAudioPatchCommand(
1232 const struct audio_patch *patch,
1233 audio_patch_handle_t *handle,
1234 int delayMs)
1235{
1236 status_t status = NO_ERROR;
1237
1238 sp<AudioCommand> command = new AudioCommand();
1239 command->mCommand = CREATE_AUDIO_PATCH;
1240 CreateAudioPatchData *data = new CreateAudioPatchData();
1241 data->mPatch = *patch;
1242 data->mHandle = *handle;
1243 command->mParam = data;
1244 command->mWaitStatus = true;
1245 ALOGV("AudioCommandThread() adding create patch delay %d", delayMs);
1246 status = sendCommand(command, delayMs);
1247 if (status == NO_ERROR) {
1248 *handle = data->mHandle;
1249 }
1250 return status;
1251}
1252
1253status_t AudioPolicyService::AudioCommandThread::releaseAudioPatchCommand(audio_patch_handle_t handle,
1254 int delayMs)
1255{
1256 sp<AudioCommand> command = new AudioCommand();
1257 command->mCommand = RELEASE_AUDIO_PATCH;
1258 ReleaseAudioPatchData *data = new ReleaseAudioPatchData();
1259 data->mHandle = handle;
1260 command->mParam = data;
1261 command->mWaitStatus = true;
1262 ALOGV("AudioCommandThread() adding release patch delay %d", delayMs);
1263 return sendCommand(command, delayMs);
1264}
1265
Eric Laurentb52c1522014-05-20 11:27:36 -07001266void AudioPolicyService::AudioCommandThread::updateAudioPortListCommand()
1267{
1268 sp<AudioCommand> command = new AudioCommand();
1269 command->mCommand = UPDATE_AUDIOPORT_LIST;
1270 ALOGV("AudioCommandThread() adding update audio port list");
1271 sendCommand(command);
1272}
1273
1274void AudioPolicyService::AudioCommandThread::updateAudioPatchListCommand()
1275{
1276 sp<AudioCommand>command = new AudioCommand();
1277 command->mCommand = UPDATE_AUDIOPATCH_LIST;
1278 ALOGV("AudioCommandThread() adding update audio patch list");
1279 sendCommand(command);
1280}
1281
Eric Laurente1715a42014-05-20 11:30:42 -07001282status_t AudioPolicyService::AudioCommandThread::setAudioPortConfigCommand(
1283 const struct audio_port_config *config, int delayMs)
1284{
1285 sp<AudioCommand> command = new AudioCommand();
1286 command->mCommand = SET_AUDIOPORT_CONFIG;
1287 SetAudioPortConfigData *data = new SetAudioPortConfigData();
1288 data->mConfig = *config;
1289 command->mParam = data;
1290 command->mWaitStatus = true;
1291 ALOGV("AudioCommandThread() adding set port config delay %d", delayMs);
1292 return sendCommand(command, delayMs);
1293}
1294
Jean-Michel Trivide801052015-04-14 19:10:14 -07001295void AudioPolicyService::AudioCommandThread::dynamicPolicyMixStateUpdateCommand(
Chih-Hung Hsiehe964d4e2016-08-09 14:31:32 -07001296 const String8& regId, int32_t state)
Jean-Michel Trivide801052015-04-14 19:10:14 -07001297{
1298 sp<AudioCommand> command = new AudioCommand();
1299 command->mCommand = DYN_POLICY_MIX_STATE_UPDATE;
1300 DynPolicyMixStateUpdateData *data = new DynPolicyMixStateUpdateData();
1301 data->mRegId = regId;
1302 data->mState = state;
1303 command->mParam = data;
1304 ALOGV("AudioCommandThread() sending dynamic policy mix (id=%s) state update to %d",
1305 regId.string(), state);
1306 sendCommand(command);
1307}
1308
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -08001309void AudioPolicyService::AudioCommandThread::recordingConfigurationUpdateCommand(
Jean-Michel Triviac4e4292016-12-22 11:39:31 -08001310 int event, const record_client_info_t *clientInfo,
Jean-Michel Trivi8c7cf3b2016-02-25 17:08:24 -08001311 const audio_config_base_t *clientConfig, const audio_config_base_t *deviceConfig,
1312 audio_patch_handle_t patchHandle)
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -08001313{
1314 sp<AudioCommand>command = new AudioCommand();
1315 command->mCommand = RECORDING_CONFIGURATION_UPDATE;
1316 RecordingConfigurationUpdateData *data = new RecordingConfigurationUpdateData();
1317 data->mEvent = event;
Jean-Michel Triviac4e4292016-12-22 11:39:31 -08001318 data->mClientInfo = *clientInfo;
Jean-Michel Trivi7281aa92016-02-17 15:33:40 -08001319 data->mClientConfig = *clientConfig;
1320 data->mDeviceConfig = *deviceConfig;
Jean-Michel Trivi8c7cf3b2016-02-25 17:08:24 -08001321 data->mPatchHandle = patchHandle;
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -08001322 command->mParam = data;
Jean-Michel Triviac4e4292016-12-22 11:39:31 -08001323 ALOGV("AudioCommandThread() adding recording configuration update event %d, source %d uid %u",
1324 event, clientInfo->source, clientInfo->uid);
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -08001325 sendCommand(command);
1326}
1327
Eric Laurent0ede8922014-05-09 18:04:42 -07001328status_t AudioPolicyService::AudioCommandThread::sendCommand(sp<AudioCommand>& command, int delayMs)
1329{
1330 {
1331 Mutex::Autolock _l(mLock);
1332 insertCommand_l(command, delayMs);
1333 mWaitWorkCV.signal();
1334 }
1335 Mutex::Autolock _l(command->mLock);
1336 while (command->mWaitStatus) {
1337 nsecs_t timeOutNs = kAudioCommandTimeoutNs + milliseconds(delayMs);
1338 if (command->mCond.waitRelative(command->mLock, timeOutNs) != NO_ERROR) {
1339 command->mStatus = TIMED_OUT;
1340 command->mWaitStatus = false;
1341 }
1342 }
1343 return command->mStatus;
Eric Laurentbfb1b832013-01-07 09:53:42 -08001344}
1345
Mathias Agopian65ab4712010-07-14 17:59:35 -07001346// insertCommand_l() must be called with mLock held
Eric Laurent0ede8922014-05-09 18:04:42 -07001347void AudioPolicyService::AudioCommandThread::insertCommand_l(sp<AudioCommand>& command, int delayMs)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001348{
Glenn Kasten8d6a2442012-02-08 14:04:28 -08001349 ssize_t i; // not size_t because i will count down to -1
Eric Laurent0ede8922014-05-09 18:04:42 -07001350 Vector < sp<AudioCommand> > removedCommands;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001351 command->mTime = systemTime() + milliseconds(delayMs);
1352
1353 // acquire wake lock to make sure delayed commands are processed
Eric Laurentbfb1b832013-01-07 09:53:42 -08001354 if (mAudioCommands.isEmpty()) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07001355 acquire_wake_lock(PARTIAL_WAKE_LOCK, mName.string());
1356 }
1357
1358 // check same pending commands with later time stamps and eliminate them
Ivan Lozano5ff158f2017-10-30 09:06:24 -07001359 for (i = (ssize_t)mAudioCommands.size()-1; i >= 0; i--) {
Eric Laurent0ede8922014-05-09 18:04:42 -07001360 sp<AudioCommand> command2 = mAudioCommands[i];
Mathias Agopian65ab4712010-07-14 17:59:35 -07001361 // commands are sorted by increasing time stamp: no need to scan the rest of mAudioCommands
1362 if (command2->mTime <= command->mTime) break;
Eric Laurente45b48a2014-09-04 16:40:57 -07001363
1364 // create audio patch or release audio patch commands are equivalent
1365 // with regard to filtering
1366 if ((command->mCommand == CREATE_AUDIO_PATCH) ||
1367 (command->mCommand == RELEASE_AUDIO_PATCH)) {
1368 if ((command2->mCommand != CREATE_AUDIO_PATCH) &&
1369 (command2->mCommand != RELEASE_AUDIO_PATCH)) {
1370 continue;
1371 }
1372 } else if (command2->mCommand != command->mCommand) continue;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001373
1374 switch (command->mCommand) {
1375 case SET_PARAMETERS: {
Eric Laurent0ede8922014-05-09 18:04:42 -07001376 ParametersData *data = (ParametersData *)command->mParam.get();
1377 ParametersData *data2 = (ParametersData *)command2->mParam.get();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001378 if (data->mIO != data2->mIO) break;
Steve Block3856b092011-10-20 11:56:00 +01001379 ALOGV("Comparing parameter command %s to new command %s",
Eric Laurentde070132010-07-13 04:45:46 -07001380 data2->mKeyValuePairs.string(), data->mKeyValuePairs.string());
Mathias Agopian65ab4712010-07-14 17:59:35 -07001381 AudioParameter param = AudioParameter(data->mKeyValuePairs);
1382 AudioParameter param2 = AudioParameter(data2->mKeyValuePairs);
1383 for (size_t j = 0; j < param.size(); j++) {
Glenn Kastene53b9ea2012-03-12 16:29:55 -07001384 String8 key;
1385 String8 value;
1386 param.getAt(j, key, value);
1387 for (size_t k = 0; k < param2.size(); k++) {
1388 String8 key2;
1389 String8 value2;
1390 param2.getAt(k, key2, value2);
1391 if (key2 == key) {
1392 param2.remove(key2);
1393 ALOGV("Filtering out parameter %s", key2.string());
1394 break;
1395 }
1396 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001397 }
1398 // if all keys have been filtered out, remove the command.
1399 // otherwise, update the key value pairs
1400 if (param2.size() == 0) {
1401 removedCommands.add(command2);
1402 } else {
1403 data2->mKeyValuePairs = param2.toString();
1404 }
Eric Laurent21e54562013-09-23 12:08:05 -07001405 command->mTime = command2->mTime;
1406 // force delayMs to non 0 so that code below does not request to wait for
1407 // command status as the command is now delayed
1408 delayMs = 1;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001409 } break;
1410
1411 case SET_VOLUME: {
Eric Laurent0ede8922014-05-09 18:04:42 -07001412 VolumeData *data = (VolumeData *)command->mParam.get();
1413 VolumeData *data2 = (VolumeData *)command2->mParam.get();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001414 if (data->mIO != data2->mIO) break;
1415 if (data->mStream != data2->mStream) break;
Steve Block3856b092011-10-20 11:56:00 +01001416 ALOGV("Filtering out volume command on output %d for stream %d",
Eric Laurentde070132010-07-13 04:45:46 -07001417 data->mIO, data->mStream);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001418 removedCommands.add(command2);
Eric Laurent21e54562013-09-23 12:08:05 -07001419 command->mTime = command2->mTime;
1420 // force delayMs to non 0 so that code below does not request to wait for
1421 // command status as the command is now delayed
1422 delayMs = 1;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001423 } break;
Eric Laurente45b48a2014-09-04 16:40:57 -07001424
Eric Laurentbaf35fe2016-07-27 15:36:53 -07001425 case SET_VOICE_VOLUME: {
1426 VoiceVolumeData *data = (VoiceVolumeData *)command->mParam.get();
1427 VoiceVolumeData *data2 = (VoiceVolumeData *)command2->mParam.get();
1428 ALOGV("Filtering out voice volume command value %f replaced by %f",
1429 data2->mVolume, data->mVolume);
1430 removedCommands.add(command2);
1431 command->mTime = command2->mTime;
1432 // force delayMs to non 0 so that code below does not request to wait for
1433 // command status as the command is now delayed
1434 delayMs = 1;
1435 } break;
1436
Eric Laurente45b48a2014-09-04 16:40:57 -07001437 case CREATE_AUDIO_PATCH:
1438 case RELEASE_AUDIO_PATCH: {
1439 audio_patch_handle_t handle;
Haynes Mathew Georgea2d4a6d2014-10-13 13:05:22 -07001440 struct audio_patch patch;
Eric Laurente45b48a2014-09-04 16:40:57 -07001441 if (command->mCommand == CREATE_AUDIO_PATCH) {
1442 handle = ((CreateAudioPatchData *)command->mParam.get())->mHandle;
Haynes Mathew Georgea2d4a6d2014-10-13 13:05:22 -07001443 patch = ((CreateAudioPatchData *)command->mParam.get())->mPatch;
Eric Laurente45b48a2014-09-04 16:40:57 -07001444 } else {
1445 handle = ((ReleaseAudioPatchData *)command->mParam.get())->mHandle;
Mikhail Naganov7be71d22018-05-23 16:51:46 -07001446 memset(&patch, 0, sizeof(patch));
Eric Laurente45b48a2014-09-04 16:40:57 -07001447 }
1448 audio_patch_handle_t handle2;
Haynes Mathew Georgea2d4a6d2014-10-13 13:05:22 -07001449 struct audio_patch patch2;
Eric Laurente45b48a2014-09-04 16:40:57 -07001450 if (command2->mCommand == CREATE_AUDIO_PATCH) {
1451 handle2 = ((CreateAudioPatchData *)command2->mParam.get())->mHandle;
Haynes Mathew Georgea2d4a6d2014-10-13 13:05:22 -07001452 patch2 = ((CreateAudioPatchData *)command2->mParam.get())->mPatch;
Eric Laurente45b48a2014-09-04 16:40:57 -07001453 } else {
1454 handle2 = ((ReleaseAudioPatchData *)command2->mParam.get())->mHandle;
Glenn Kastenf60b6b62015-07-06 10:53:26 -07001455 memset(&patch2, 0, sizeof(patch2));
Eric Laurente45b48a2014-09-04 16:40:57 -07001456 }
1457 if (handle != handle2) break;
Haynes Mathew Georgea2d4a6d2014-10-13 13:05:22 -07001458 /* Filter CREATE_AUDIO_PATCH commands only when they are issued for
1459 same output. */
1460 if( (command->mCommand == CREATE_AUDIO_PATCH) &&
1461 (command2->mCommand == CREATE_AUDIO_PATCH) ) {
1462 bool isOutputDiff = false;
1463 if (patch.num_sources == patch2.num_sources) {
1464 for (unsigned count = 0; count < patch.num_sources; count++) {
1465 if (patch.sources[count].id != patch2.sources[count].id) {
1466 isOutputDiff = true;
1467 break;
1468 }
1469 }
1470 if (isOutputDiff)
1471 break;
1472 }
1473 }
Eric Laurente45b48a2014-09-04 16:40:57 -07001474 ALOGV("Filtering out %s audio patch command for handle %d",
1475 (command->mCommand == CREATE_AUDIO_PATCH) ? "create" : "release", handle);
1476 removedCommands.add(command2);
1477 command->mTime = command2->mTime;
1478 // force delayMs to non 0 so that code below does not request to wait for
1479 // command status as the command is now delayed
1480 delayMs = 1;
1481 } break;
1482
Jean-Michel Trivide801052015-04-14 19:10:14 -07001483 case DYN_POLICY_MIX_STATE_UPDATE: {
1484
1485 } break;
1486
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -08001487 case RECORDING_CONFIGURATION_UPDATE: {
1488
1489 } break;
1490
Mathias Agopian65ab4712010-07-14 17:59:35 -07001491 default:
1492 break;
1493 }
1494 }
1495
1496 // remove filtered commands
1497 for (size_t j = 0; j < removedCommands.size(); j++) {
1498 // removed commands always have time stamps greater than current command
1499 for (size_t k = i + 1; k < mAudioCommands.size(); k++) {
Eric Laurent0ede8922014-05-09 18:04:42 -07001500 if (mAudioCommands[k].get() == removedCommands[j].get()) {
Steve Block3856b092011-10-20 11:56:00 +01001501 ALOGV("suppressing command: %d", mAudioCommands[k]->mCommand);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001502 mAudioCommands.removeAt(k);
1503 break;
1504 }
1505 }
1506 }
1507 removedCommands.clear();
1508
Eric Laurentaa79bef2015-01-15 14:33:51 -08001509 // Disable wait for status if delay is not 0.
1510 // Except for create audio patch command because the returned patch handle
1511 // is needed by audio policy manager
1512 if (delayMs != 0 && command->mCommand != CREATE_AUDIO_PATCH) {
Eric Laurentcec4abb2012-07-03 12:23:02 -07001513 command->mWaitStatus = false;
1514 }
Eric Laurentcec4abb2012-07-03 12:23:02 -07001515
Mathias Agopian65ab4712010-07-14 17:59:35 -07001516 // insert command at the right place according to its time stamp
Eric Laurent1e693b52014-07-09 15:03:28 -07001517 ALOGV("inserting command: %d at index %zd, num commands %zu",
1518 command->mCommand, i+1, mAudioCommands.size());
Mathias Agopian65ab4712010-07-14 17:59:35 -07001519 mAudioCommands.insertAt(command, i + 1);
1520}
1521
1522void AudioPolicyService::AudioCommandThread::exit()
1523{
Steve Block3856b092011-10-20 11:56:00 +01001524 ALOGV("AudioCommandThread::exit");
Mathias Agopian65ab4712010-07-14 17:59:35 -07001525 {
1526 AutoMutex _l(mLock);
1527 requestExit();
1528 mWaitWorkCV.signal();
1529 }
Zach Janga754b4f2015-10-27 01:29:34 +00001530 // Note that we can call it from the thread loop if all other references have been released
1531 // but it will safely return WOULD_BLOCK in this case
Mathias Agopian65ab4712010-07-14 17:59:35 -07001532 requestExitAndWait();
1533}
1534
1535void AudioPolicyService::AudioCommandThread::AudioCommand::dump(char* buffer, size_t size)
1536{
1537 snprintf(buffer, size, " %02d %06d.%03d %01u %p\n",
1538 mCommand,
1539 (int)ns2s(mTime),
1540 (int)ns2ms(mTime)%1000,
1541 mWaitStatus,
Eric Laurent0ede8922014-05-09 18:04:42 -07001542 mParam.get());
Mathias Agopian65ab4712010-07-14 17:59:35 -07001543}
1544
Dima Zavinfce7a472011-04-19 22:30:36 -07001545/******* helpers for the service_ops callbacks defined below *********/
1546void AudioPolicyService::setParameters(audio_io_handle_t ioHandle,
1547 const char *keyValuePairs,
1548 int delayMs)
1549{
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001550 mAudioCommandThread->parametersCommand(ioHandle, keyValuePairs,
Dima Zavinfce7a472011-04-19 22:30:36 -07001551 delayMs);
1552}
1553
1554int AudioPolicyService::setStreamVolume(audio_stream_type_t stream,
1555 float volume,
1556 audio_io_handle_t output,
1557 int delayMs)
1558{
Glenn Kastenfff6d712012-01-12 16:38:12 -08001559 return (int)mAudioCommandThread->volumeCommand(stream, volume,
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001560 output, delayMs);
Dima Zavinfce7a472011-04-19 22:30:36 -07001561}
1562
Dima Zavinfce7a472011-04-19 22:30:36 -07001563int AudioPolicyService::setVoiceVolume(float volume, int delayMs)
1564{
1565 return (int)mAudioCommandThread->voiceVolumeCommand(volume, delayMs);
1566}
1567
Dima Zavinfce7a472011-04-19 22:30:36 -07001568extern "C" {
Eric Laurent2d388ec2014-03-07 13:25:54 -08001569audio_module_handle_t aps_load_hw_module(void *service __unused,
1570 const char *name);
1571audio_io_handle_t aps_open_output(void *service __unused,
Eric Laurenta4c5a552012-03-29 10:12:40 -07001572 audio_devices_t *pDevices,
1573 uint32_t *pSamplingRate,
1574 audio_format_t *pFormat,
1575 audio_channel_mask_t *pChannelMask,
1576 uint32_t *pLatencyMs,
Eric Laurent2d388ec2014-03-07 13:25:54 -08001577 audio_output_flags_t flags);
Eric Laurenta4c5a552012-03-29 10:12:40 -07001578
Eric Laurent2d388ec2014-03-07 13:25:54 -08001579audio_io_handle_t aps_open_output_on_module(void *service __unused,
Eric Laurenta4c5a552012-03-29 10:12:40 -07001580 audio_module_handle_t module,
1581 audio_devices_t *pDevices,
1582 uint32_t *pSamplingRate,
1583 audio_format_t *pFormat,
1584 audio_channel_mask_t *pChannelMask,
1585 uint32_t *pLatencyMs,
Richard Fitzgeraldad3af332013-03-25 16:54:37 +00001586 audio_output_flags_t flags,
Eric Laurent2d388ec2014-03-07 13:25:54 -08001587 const audio_offload_info_t *offloadInfo);
1588audio_io_handle_t aps_open_dup_output(void *service __unused,
Dima Zavinfce7a472011-04-19 22:30:36 -07001589 audio_io_handle_t output1,
Eric Laurent2d388ec2014-03-07 13:25:54 -08001590 audio_io_handle_t output2);
1591int aps_close_output(void *service __unused, audio_io_handle_t output);
1592int aps_suspend_output(void *service __unused, audio_io_handle_t output);
1593int aps_restore_output(void *service __unused, audio_io_handle_t output);
1594audio_io_handle_t aps_open_input(void *service __unused,
Eric Laurenta4c5a552012-03-29 10:12:40 -07001595 audio_devices_t *pDevices,
1596 uint32_t *pSamplingRate,
1597 audio_format_t *pFormat,
1598 audio_channel_mask_t *pChannelMask,
Eric Laurent2d388ec2014-03-07 13:25:54 -08001599 audio_in_acoustics_t acoustics __unused);
1600audio_io_handle_t aps_open_input_on_module(void *service __unused,
Eric Laurenta4c5a552012-03-29 10:12:40 -07001601 audio_module_handle_t module,
1602 audio_devices_t *pDevices,
1603 uint32_t *pSamplingRate,
1604 audio_format_t *pFormat,
Eric Laurent2d388ec2014-03-07 13:25:54 -08001605 audio_channel_mask_t *pChannelMask);
1606int aps_close_input(void *service __unused, audio_io_handle_t input);
1607int aps_invalidate_stream(void *service __unused, audio_stream_type_t stream);
Glenn Kastend848eb42016-03-08 13:42:11 -08001608int aps_move_effects(void *service __unused, audio_session_t session,
Dima Zavinfce7a472011-04-19 22:30:36 -07001609 audio_io_handle_t src_output,
Eric Laurent2d388ec2014-03-07 13:25:54 -08001610 audio_io_handle_t dst_output);
1611char * aps_get_parameters(void *service __unused, audio_io_handle_t io_handle,
1612 const char *keys);
1613void aps_set_parameters(void *service, audio_io_handle_t io_handle,
1614 const char *kv_pairs, int delay_ms);
1615int aps_set_stream_volume(void *service, audio_stream_type_t stream,
Dima Zavinfce7a472011-04-19 22:30:36 -07001616 float volume, audio_io_handle_t output,
Eric Laurent2d388ec2014-03-07 13:25:54 -08001617 int delay_ms);
Eric Laurent2d388ec2014-03-07 13:25:54 -08001618int aps_set_voice_volume(void *service, float volume, int delay_ms);
1619};
Dima Zavinfce7a472011-04-19 22:30:36 -07001620
Mikhail Naganov1b2a7942017-12-08 10:18:09 -08001621} // namespace android