blob: a39477dd30f9d06d4db16083a8578724080a879e [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
386// OR Any other client
387// AND The assistant is not on TOP
388// AND is on TOP OR latest started
389// AND there is no privacy sensitive active capture
Eric Laurent4eb58f12018-12-07 16:41:02 -0800390//TODO: mamanage pre processing effects according to use case priority
391
392 sp<AudioRecordClient> topActive;
393 sp<AudioRecordClient> latestActive;
Eric Laurent4eb58f12018-12-07 16:41:02 -0800394 sp<AudioRecordClient> latestSensitiveActive;
Eric Laurenta46bedb2018-12-07 18:01:26 -0800395 nsecs_t topStartNs = 0;
396 nsecs_t latestStartNs = 0;
Eric Laurent4eb58f12018-12-07 16:41:02 -0800397 nsecs_t latestSensitiveStartNs = 0;
398 bool isA11yOnTop = mUidPolicy->isA11yOnTop();
399 bool isAssistantOnTop = false;
400 bool isSensitiveActive = false;
401
Michael Groovercfd28302018-12-11 19:16:46 -0800402 // if Sensor Privacy is enabled then all recordings should be silenced.
403 if (mSensorPrivacyPolicy->isSensorPrivacyEnabled()) {
404 silenceAllRecordings_l();
405 return;
406 }
407
Eric Laurente8c8b432018-10-17 10:08:02 -0700408 for (size_t i =0; i < mAudioRecordClients.size(); i++) {
409 sp<AudioRecordClient> current = mAudioRecordClients[i];
410 if (!current->active) continue;
Eric Laurent4eb58f12018-12-07 16:41:02 -0800411 if (isPrivacySensitive(current->attributes.source)) {
412 if (current->startTimeNs > latestSensitiveStartNs) {
413 latestSensitiveActive = current;
414 latestSensitiveStartNs = current->startTimeNs;
415 }
416 isSensitiveActive = true;
417 }
418 if (mUidPolicy->getUidState(current->uid) == ActivityManager::PROCESS_STATE_TOP) {
Eric Laurenta46bedb2018-12-07 18:01:26 -0800419 if (current->startTimeNs > topStartNs) {
420 topActive = current;
421 topStartNs = current->startTimeNs;
422 }
Eric Laurent4eb58f12018-12-07 16:41:02 -0800423 if (mUidPolicy->isAssistantUid(current->uid)) {
424 isAssistantOnTop = true;
425 }
426 }
427 if (current->startTimeNs > latestStartNs) {
428 latestActive = current;
429 latestStartNs = current->startTimeNs;
430 }
431 }
432
433 if (topActive == nullptr && latestActive == nullptr) {
434 return;
435 }
436
Eric Laurenta46bedb2018-12-07 18:01:26 -0800437 if (topActive != nullptr) {
438 latestActive = nullptr;
439 }
440
Eric Laurent4eb58f12018-12-07 16:41:02 -0800441 for (size_t i =0; i < mAudioRecordClients.size(); i++) {
442 sp<AudioRecordClient> current = mAudioRecordClients[i];
443 if (!current->active) continue;
444
445 audio_source_t source = current->attributes.source;
Eric Laurenta46bedb2018-12-07 18:01:26 -0800446 bool isOnTop = current == topActive;
Eric Laurent4eb58f12018-12-07 16:41:02 -0800447 bool isLatest = current == latestActive;
448 bool isLatestSensitive = current == latestSensitiveActive;
449 bool forceIdle = true;
450 if (mUidPolicy->isAssistantUid(current->uid)) {
451 if (isA11yOnTop) {
452 if (source == AUDIO_SOURCE_HOTWORD || source == AUDIO_SOURCE_VOICE_RECOGNITION) {
453 forceIdle = false;
454 }
455 } else {
Eric Laurenta46bedb2018-12-07 18:01:26 -0800456 if ((((isOnTop || isLatest) && source == AUDIO_SOURCE_VOICE_RECOGNITION) ||
Eric Laurent4eb58f12018-12-07 16:41:02 -0800457 source == AUDIO_SOURCE_HOTWORD) && !isSensitiveActive) {
458 forceIdle = false;
459 }
460 }
461 } else if (mUidPolicy->isA11yUid(current->uid)) {
Eric Laurenta46bedb2018-12-07 18:01:26 -0800462 if ((isOnTop || isLatest) &&
Eric Laurent4eb58f12018-12-07 16:41:02 -0800463 (source == AUDIO_SOURCE_VOICE_RECOGNITION || source == AUDIO_SOURCE_HOTWORD)) {
464 forceIdle = false;
465 }
466 } else {
Eric Laurenta46bedb2018-12-07 18:01:26 -0800467 if (!isAssistantOnTop && (isOnTop || isLatest) &&
Eric Laurent4eb58f12018-12-07 16:41:02 -0800468 (!isSensitiveActive || isLatestSensitive)) {
469 forceIdle = false;
470 }
471 }
472 setAppState_l(current->uid,
473 forceIdle ? APP_STATE_IDLE :
474 apmStatFromAmState(mUidPolicy->getUidState(current->uid)));
Eric Laurente8c8b432018-10-17 10:08:02 -0700475 }
476}
477
Michael Groovercfd28302018-12-11 19:16:46 -0800478void AudioPolicyService::silenceAllRecordings_l() {
479 for (size_t i = 0; i < mAudioRecordClients.size(); i++) {
480 sp<AudioRecordClient> current = mAudioRecordClients[i];
481 setAppState_l(current->uid, APP_STATE_IDLE);
482 }
483}
484
Eric Laurente8c8b432018-10-17 10:08:02 -0700485/* static */
486app_state_t AudioPolicyService::apmStatFromAmState(int amState) {
487 switch (amState) {
488 case ActivityManager::PROCESS_STATE_UNKNOWN:
489 return APP_STATE_IDLE;
490 case ActivityManager::PROCESS_STATE_TOP:
491 return APP_STATE_TOP;
492 default:
493 break;
494 }
495 return APP_STATE_FOREGROUND;
496}
497
Eric Laurent4eb58f12018-12-07 16:41:02 -0800498/* static */
499bool AudioPolicyService::isPrivacySensitive(audio_source_t source)
500{
501 switch (source) {
502 case AUDIO_SOURCE_VOICE_UPLINK:
503 case AUDIO_SOURCE_VOICE_DOWNLINK:
504 case AUDIO_SOURCE_VOICE_CALL:
505 case AUDIO_SOURCE_CAMCORDER:
506 case AUDIO_SOURCE_VOICE_COMMUNICATION:
507 return true;
508 default:
509 break;
510 }
511 return false;
512}
513
Eric Laurente8c8b432018-10-17 10:08:02 -0700514void AudioPolicyService::setAppState_l(uid_t uid, app_state_t state)
515{
516 AutoCallerClear acc;
517
518 if (mAudioPolicyManager) {
519 mAudioPolicyManager->setAppState(uid, state);
Mikhail Naganoveae73eb2018-04-03 16:57:36 -0700520 }
521 sp<IAudioFlinger> af = AudioSystem::get_audio_flinger();
522 if (af) {
Eric Laurentf32108e2018-10-04 17:22:04 -0700523 bool silenced = state == APP_STATE_IDLE;
Mikhail Naganoveae73eb2018-04-03 16:57:36 -0700524 af->setRecordSilenced(uid, silenced);
525 }
Svet Ganovf4ddfef2018-01-16 07:37:58 -0800526}
527
Glenn Kasten0f11b512014-01-31 16:18:54 -0800528status_t AudioPolicyService::dump(int fd, const Vector<String16>& args __unused)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700529{
Glenn Kasten44deb052012-02-05 18:09:08 -0800530 if (!dumpAllowed()) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700531 dumpPermissionDenial(fd);
532 } else {
533 bool locked = tryLock(mLock);
534 if (!locked) {
535 String8 result(kDeadlockedString);
536 write(fd, result.string(), result.size());
537 }
538
539 dumpInternals(fd);
Glenn Kasten9d1f02d2012-02-08 17:47:58 -0800540 if (mAudioCommandThread != 0) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700541 mAudioCommandThread->dump(fd);
542 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700543
Eric Laurentdce54a12014-03-10 12:19:46 -0700544 if (mAudioPolicyManager) {
545 mAudioPolicyManager->dump(fd);
546 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700547
548 if (locked) mLock.unlock();
549 }
550 return NO_ERROR;
551}
552
553status_t AudioPolicyService::dumpPermissionDenial(int fd)
554{
555 const size_t SIZE = 256;
556 char buffer[SIZE];
557 String8 result;
558 snprintf(buffer, SIZE, "Permission Denial: "
559 "can't dump AudioPolicyService from pid=%d, uid=%d\n",
560 IPCThreadState::self()->getCallingPid(),
561 IPCThreadState::self()->getCallingUid());
562 result.append(buffer);
563 write(fd, result.string(), result.size());
564 return NO_ERROR;
565}
566
567status_t AudioPolicyService::onTransact(
Svet Ganovf4ddfef2018-01-16 07:37:58 -0800568 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags) {
569 switch (code) {
570 case SHELL_COMMAND_TRANSACTION: {
571 int in = data.readFileDescriptor();
572 int out = data.readFileDescriptor();
573 int err = data.readFileDescriptor();
574 int argc = data.readInt32();
575 Vector<String16> args;
576 for (int i = 0; i < argc && data.dataAvail() > 0; i++) {
577 args.add(data.readString16());
578 }
579 sp<IBinder> unusedCallback;
580 sp<IResultReceiver> resultReceiver;
581 status_t status;
582 if ((status = data.readNullableStrongBinder(&unusedCallback)) != NO_ERROR) {
583 return status;
584 }
585 if ((status = data.readNullableStrongBinder(&resultReceiver)) != NO_ERROR) {
586 return status;
587 }
588 status = shellCommand(in, out, err, args);
589 if (resultReceiver != nullptr) {
590 resultReceiver->send(status);
591 }
592 return NO_ERROR;
593 }
594 }
595
Mathias Agopian65ab4712010-07-14 17:59:35 -0700596 return BnAudioPolicyService::onTransact(code, data, reply, flags);
597}
598
Svet Ganovf4ddfef2018-01-16 07:37:58 -0800599// ------------------- Shell command implementation -------------------
600
601// NOTE: This is a remote API - make sure all args are validated
602status_t AudioPolicyService::shellCommand(int in, int out, int err, Vector<String16>& args) {
603 if (!checkCallingPermission(sManageAudioPolicyPermission, nullptr, nullptr)) {
604 return PERMISSION_DENIED;
605 }
606 if (in == BAD_TYPE || out == BAD_TYPE || err == BAD_TYPE) {
607 return BAD_VALUE;
608 }
609 if (args.size() == 3 && args[0] == String16("set-uid-state")) {
610 return handleSetUidState(args, err);
611 } else if (args.size() == 2 && args[0] == String16("reset-uid-state")) {
612 return handleResetUidState(args, err);
613 } else if (args.size() == 2 && args[0] == String16("get-uid-state")) {
614 return handleGetUidState(args, out, err);
615 } else if (args.size() == 1 && args[0] == String16("help")) {
616 printHelp(out);
617 return NO_ERROR;
618 }
619 printHelp(err);
620 return BAD_VALUE;
621}
622
623status_t AudioPolicyService::handleSetUidState(Vector<String16>& args, int err) {
624 PermissionController pc;
625 int uid = pc.getPackageUid(args[1], 0);
626 if (uid <= 0) {
627 ALOGE("Unknown package: '%s'", String8(args[1]).string());
628 dprintf(err, "Unknown package: '%s'\n", String8(args[1]).string());
629 return BAD_VALUE;
630 }
631 bool active = false;
632 if (args[2] == String16("active")) {
633 active = true;
634 } else if ((args[2] != String16("idle"))) {
635 ALOGE("Expected active or idle but got: '%s'", String8(args[2]).string());
636 return BAD_VALUE;
637 }
638 mUidPolicy->addOverrideUid(uid, active);
639 return NO_ERROR;
640}
641
642status_t AudioPolicyService::handleResetUidState(Vector<String16>& args, int err) {
643 PermissionController pc;
644 int uid = pc.getPackageUid(args[1], 0);
645 if (uid < 0) {
646 ALOGE("Unknown package: '%s'", String8(args[1]).string());
647 dprintf(err, "Unknown package: '%s'\n", String8(args[1]).string());
648 return BAD_VALUE;
649 }
650 mUidPolicy->removeOverrideUid(uid);
651 return NO_ERROR;
652}
653
654status_t AudioPolicyService::handleGetUidState(Vector<String16>& args, int out, int err) {
655 PermissionController pc;
656 int uid = pc.getPackageUid(args[1], 0);
657 if (uid < 0) {
658 ALOGE("Unknown package: '%s'", String8(args[1]).string());
659 dprintf(err, "Unknown package: '%s'\n", String8(args[1]).string());
660 return BAD_VALUE;
661 }
662 if (mUidPolicy->isUidActive(uid)) {
663 return dprintf(out, "active\n");
664 } else {
665 return dprintf(out, "idle\n");
666 }
667}
668
669status_t AudioPolicyService::printHelp(int out) {
670 return dprintf(out, "Audio policy service commands:\n"
671 " get-uid-state <PACKAGE> gets the uid state\n"
672 " set-uid-state <PACKAGE> <active|idle> overrides the uid state\n"
673 " reset-uid-state <PACKAGE> clears the uid state override\n"
674 " help print this message\n");
675}
676
677// ----------- AudioPolicyService::UidPolicy implementation ----------
678
679void AudioPolicyService::UidPolicy::registerSelf() {
680 ActivityManager am;
681 am.registerUidObserver(this, ActivityManager::UID_OBSERVER_GONE
682 | ActivityManager::UID_OBSERVER_IDLE
Eric Laurente8c8b432018-10-17 10:08:02 -0700683 | ActivityManager::UID_OBSERVER_ACTIVE
684 | ActivityManager::UID_OBSERVER_PROCSTATE,
Svet Ganovf4ddfef2018-01-16 07:37:58 -0800685 ActivityManager::PROCESS_STATE_UNKNOWN,
686 String16("audioserver"));
Mikhail Naganoveae73eb2018-04-03 16:57:36 -0700687 status_t res = am.linkToDeath(this);
688 if (!res) {
689 Mutex::Autolock _l(mLock);
690 mObserverRegistered = true;
691 } else {
692 ALOGE("UidPolicy::registerSelf linkToDeath failed: %d", res);
Eric Laurent4eb58f12018-12-07 16:41:02 -0800693
Mikhail Naganoveae73eb2018-04-03 16:57:36 -0700694 am.unregisterUidObserver(this);
695 }
Svet Ganovf4ddfef2018-01-16 07:37:58 -0800696}
697
698void AudioPolicyService::UidPolicy::unregisterSelf() {
699 ActivityManager am;
Mikhail Naganoveae73eb2018-04-03 16:57:36 -0700700 am.unlinkToDeath(this);
Svet Ganovf4ddfef2018-01-16 07:37:58 -0800701 am.unregisterUidObserver(this);
Mikhail Naganoveae73eb2018-04-03 16:57:36 -0700702 Mutex::Autolock _l(mLock);
703 mObserverRegistered = false;
Svet Ganovf4ddfef2018-01-16 07:37:58 -0800704}
705
Mikhail Naganoveae73eb2018-04-03 16:57:36 -0700706void AudioPolicyService::UidPolicy::binderDied(__unused const wp<IBinder> &who) {
707 Mutex::Autolock _l(mLock);
708 mCachedUids.clear();
709 mObserverRegistered = false;
Svet Ganovf4ddfef2018-01-16 07:37:58 -0800710}
711
Eric Laurente8c8b432018-10-17 10:08:02 -0700712void AudioPolicyService::UidPolicy::checkRegistered() {
Mikhail Naganoveae73eb2018-04-03 16:57:36 -0700713 bool needToReregister = false;
714 {
715 Mutex::Autolock _l(mLock);
716 needToReregister = !mObserverRegistered;
Svet Ganovf4ddfef2018-01-16 07:37:58 -0800717 }
Mikhail Naganoveae73eb2018-04-03 16:57:36 -0700718 if (needToReregister) {
719 // Looks like ActivityManager has died previously, attempt to re-register.
720 registerSelf();
721 }
Eric Laurente8c8b432018-10-17 10:08:02 -0700722}
723
724bool AudioPolicyService::UidPolicy::isUidActive(uid_t uid) {
725 if (isServiceUid(uid)) return true;
726 checkRegistered();
Mikhail Naganoveae73eb2018-04-03 16:57:36 -0700727 {
728 Mutex::Autolock _l(mLock);
729 auto overrideIter = mOverrideUids.find(uid);
730 if (overrideIter != mOverrideUids.end()) {
Eric Laurente8c8b432018-10-17 10:08:02 -0700731 return overrideIter->second.first;
Mikhail Naganoveae73eb2018-04-03 16:57:36 -0700732 }
733 // In an absense of the ActivityManager, assume everything to be active.
734 if (!mObserverRegistered) return true;
735 auto cacheIter = mCachedUids.find(uid);
Mikhail Naganoveba668a2018-04-05 08:13:15 -0700736 if (cacheIter != mCachedUids.end()) {
Eric Laurente8c8b432018-10-17 10:08:02 -0700737 return cacheIter->second.first;
Mikhail Naganoveae73eb2018-04-03 16:57:36 -0700738 }
739 }
740 ActivityManager am;
741 bool active = am.isUidActive(uid, String16("audioserver"));
742 {
743 Mutex::Autolock _l(mLock);
Eric Laurente8c8b432018-10-17 10:08:02 -0700744 mCachedUids.insert(std::pair<uid_t,
745 std::pair<bool, int>>(uid, std::pair<bool, int>(active,
746 ActivityManager::PROCESS_STATE_UNKNOWN)));
Mikhail Naganoveae73eb2018-04-03 16:57:36 -0700747 }
748 return active;
Svet Ganovf4ddfef2018-01-16 07:37:58 -0800749}
750
Eric Laurente8c8b432018-10-17 10:08:02 -0700751int AudioPolicyService::UidPolicy::getUidState(uid_t uid) {
752 if (isServiceUid(uid)) {
753 return ActivityManager::PROCESS_STATE_TOP;
754 }
755 checkRegistered();
756 {
757 Mutex::Autolock _l(mLock);
758 auto overrideIter = mOverrideUids.find(uid);
759 if (overrideIter != mOverrideUids.end()) {
760 if (overrideIter->second.first) {
761 if (overrideIter->second.second != ActivityManager::PROCESS_STATE_UNKNOWN) {
762 return overrideIter->second.second;
763 } else {
764 auto cacheIter = mCachedUids.find(uid);
765 if (cacheIter != mCachedUids.end()) {
766 return cacheIter->second.second;
767 }
768 }
769 }
770 return ActivityManager::PROCESS_STATE_UNKNOWN;
771 }
772 // In an absense of the ActivityManager, assume everything to be active.
773 if (!mObserverRegistered) {
774 return ActivityManager::PROCESS_STATE_TOP;
775 }
776 auto cacheIter = mCachedUids.find(uid);
777 if (cacheIter != mCachedUids.end()) {
778 if (cacheIter->second.first) {
779 return cacheIter->second.second;
780 } else {
781 return ActivityManager::PROCESS_STATE_UNKNOWN;
782 }
783 }
784 }
785 ActivityManager am;
786 bool active = am.isUidActive(uid, String16("audioserver"));
787 int state = ActivityManager::PROCESS_STATE_UNKNOWN;
788 if (active) {
789 state = am.getUidProcessState(uid, String16("audioserver"));
790 }
791 {
792 Mutex::Autolock _l(mLock);
793 mCachedUids.insert(std::pair<uid_t,
794 std::pair<bool, int>>(uid, std::pair<bool, int>(active, state)));
795 }
Eric Laurent4eb58f12018-12-07 16:41:02 -0800796
Eric Laurente8c8b432018-10-17 10:08:02 -0700797 return state;
798}
799
Mikhail Naganoveae73eb2018-04-03 16:57:36 -0700800void AudioPolicyService::UidPolicy::onUidActive(uid_t uid) {
Eric Laurente8c8b432018-10-17 10:08:02 -0700801 updateUid(&mCachedUids, uid, true, ActivityManager::PROCESS_STATE_UNKNOWN, true);
Mikhail Naganoveae73eb2018-04-03 16:57:36 -0700802}
803
804void AudioPolicyService::UidPolicy::onUidGone(uid_t uid, __unused bool disabled) {
Eric Laurente8c8b432018-10-17 10:08:02 -0700805 updateUid(&mCachedUids, uid, false, ActivityManager::PROCESS_STATE_UNKNOWN, false);
Mikhail Naganoveae73eb2018-04-03 16:57:36 -0700806}
807
808void AudioPolicyService::UidPolicy::onUidIdle(uid_t uid, __unused bool disabled) {
Eric Laurente8c8b432018-10-17 10:08:02 -0700809 updateUid(&mCachedUids, uid, false, ActivityManager::PROCESS_STATE_UNKNOWN, true);
Mikhail Naganoveae73eb2018-04-03 16:57:36 -0700810}
811
Eric Laurente8c8b432018-10-17 10:08:02 -0700812void AudioPolicyService::UidPolicy::onUidStateChanged(uid_t uid,
813 int32_t procState,
814 int64_t procStateSeq __unused) {
815 if (procState != ActivityManager::PROCESS_STATE_UNKNOWN) {
816 updateUid(&mCachedUids, uid, true, procState, true);
Mikhail Naganoveae73eb2018-04-03 16:57:36 -0700817 }
818}
819
820void AudioPolicyService::UidPolicy::updateOverrideUid(uid_t uid, bool active, bool insert) {
Eric Laurente8c8b432018-10-17 10:08:02 -0700821 updateUid(&mOverrideUids, uid, active, ActivityManager::PROCESS_STATE_UNKNOWN, insert);
822}
823
824void AudioPolicyService::UidPolicy::notifyService() {
825 sp<AudioPolicyService> service = mService.promote();
826 if (service != nullptr) {
827 service->updateUidStates();
Mikhail Naganoveae73eb2018-04-03 16:57:36 -0700828 }
829}
830
Eric Laurente8c8b432018-10-17 10:08:02 -0700831void AudioPolicyService::UidPolicy::updateUid(std::unordered_map<uid_t,
832 std::pair<bool, int>> *uids,
833 uid_t uid,
834 bool active,
835 int state,
836 bool insert) {
837 if (isServiceUid(uid)) {
838 return;
839 }
840 bool wasActive = isUidActive(uid);
841 int previousState = getUidState(uid);
Mikhail Naganoveae73eb2018-04-03 16:57:36 -0700842 {
843 Mutex::Autolock _l(mLock);
Eric Laurente8c8b432018-10-17 10:08:02 -0700844 updateUidLocked(uids, uid, active, state, insert);
Mikhail Naganoveae73eb2018-04-03 16:57:36 -0700845 }
Eric Laurente8c8b432018-10-17 10:08:02 -0700846 if (wasActive != isUidActive(uid) || state != previousState) {
847 notifyService();
848 }
Mikhail Naganoveae73eb2018-04-03 16:57:36 -0700849}
850
Eric Laurente8c8b432018-10-17 10:08:02 -0700851void AudioPolicyService::UidPolicy::updateUidLocked(std::unordered_map<uid_t,
852 std::pair<bool, int>> *uids,
853 uid_t uid,
854 bool active,
855 int state,
856 bool insert) {
Mikhail Naganoveae73eb2018-04-03 16:57:36 -0700857 auto it = uids->find(uid);
858 if (it != uids->end()) {
Mikhail Naganoveae73eb2018-04-03 16:57:36 -0700859 if (insert) {
Eric Laurente8c8b432018-10-17 10:08:02 -0700860 if (state == ActivityManager::PROCESS_STATE_UNKNOWN) {
861 it->second.first = active;
862 }
863 if (it->second.first) {
864 it->second.second = state;
865 } else {
866 it->second.second = ActivityManager::PROCESS_STATE_UNKNOWN;
867 }
Mikhail Naganoveae73eb2018-04-03 16:57:36 -0700868 } else {
869 uids->erase(it);
870 }
Eric Laurente8c8b432018-10-17 10:08:02 -0700871 } else if (insert && (state == ActivityManager::PROCESS_STATE_UNKNOWN)) {
872 uids->insert(std::pair<uid_t, std::pair<bool, int>>(uid,
873 std::pair<bool, int>(active, state)));
Mikhail Naganoveae73eb2018-04-03 16:57:36 -0700874 }
Svet Ganovf4ddfef2018-01-16 07:37:58 -0800875}
Mathias Agopian65ab4712010-07-14 17:59:35 -0700876
Eric Laurent4eb58f12018-12-07 16:41:02 -0800877bool AudioPolicyService::UidPolicy::isA11yOnTop() {
878 for (const auto &uid : mCachedUids) {
879 std::vector<uid_t>::iterator it = find(mA11yUids.begin(), mA11yUids.end(), uid.first);
880 if (it == mA11yUids.end()) {
881 continue;
882 }
Amith Yamasanibcbb3002019-01-23 13:53:33 -0800883 if (uid.second.second >= ActivityManager::PROCESS_STATE_TOP
884 && uid.second.second <= ActivityManager::PROCESS_STATE_BOUND_FOREGROUND_SERVICE) {
Eric Laurent4eb58f12018-12-07 16:41:02 -0800885 return true;
886 }
887 }
888 return false;
889}
890
Eric Laurentb78763e2018-10-17 10:08:02 -0700891bool AudioPolicyService::UidPolicy::isA11yUid(uid_t uid)
892{
893 std::vector<uid_t>::iterator it = find(mA11yUids.begin(), mA11yUids.end(), uid);
894 return it != mA11yUids.end();
895}
896
Michael Groovercfd28302018-12-11 19:16:46 -0800897// ----------- AudioPolicyService::SensorPrivacyService implementation ----------
898void AudioPolicyService::SensorPrivacyPolicy::registerSelf() {
899 SensorPrivacyManager spm;
900 mSensorPrivacyEnabled = spm.isSensorPrivacyEnabled();
901 spm.addSensorPrivacyListener(this);
902}
903
904void AudioPolicyService::SensorPrivacyPolicy::unregisterSelf() {
905 SensorPrivacyManager spm;
906 spm.removeSensorPrivacyListener(this);
907}
908
909bool AudioPolicyService::SensorPrivacyPolicy::isSensorPrivacyEnabled() {
910 return mSensorPrivacyEnabled;
911}
912
913binder::Status AudioPolicyService::SensorPrivacyPolicy::onSensorPrivacyChanged(bool enabled) {
914 mSensorPrivacyEnabled = enabled;
915 sp<AudioPolicyService> service = mService.promote();
916 if (service != nullptr) {
917 service->updateUidStates();
918 }
919 return binder::Status::ok();
920}
921
Mathias Agopian65ab4712010-07-14 17:59:35 -0700922// ----------- AudioPolicyService::AudioCommandThread implementation ----------
923
Eric Laurentbfb1b832013-01-07 09:53:42 -0800924AudioPolicyService::AudioCommandThread::AudioCommandThread(String8 name,
925 const wp<AudioPolicyService>& service)
926 : Thread(false), mName(name), mService(service)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700927{
Mathias Agopian65ab4712010-07-14 17:59:35 -0700928}
929
930
931AudioPolicyService::AudioCommandThread::~AudioCommandThread()
932{
Eric Laurentbfb1b832013-01-07 09:53:42 -0800933 if (!mAudioCommands.isEmpty()) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700934 release_wake_lock(mName.string());
935 }
936 mAudioCommands.clear();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700937}
938
939void AudioPolicyService::AudioCommandThread::onFirstRef()
940{
Eric Laurentbfb1b832013-01-07 09:53:42 -0800941 run(mName.string(), ANDROID_PRIORITY_AUDIO);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700942}
943
944bool AudioPolicyService::AudioCommandThread::threadLoop()
945{
Eric Laurentd7eda8d2016-02-02 17:18:39 -0800946 nsecs_t waitTime = -1;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700947
948 mLock.lock();
949 while (!exitPending())
950 {
Eric Laurent59a89232014-06-08 14:14:17 -0700951 sp<AudioPolicyService> svc;
952 while (!mAudioCommands.isEmpty() && !exitPending()) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700953 nsecs_t curTime = systemTime();
954 // commands are sorted by increasing time stamp: execute them from index 0 and up
955 if (mAudioCommands[0]->mTime <= curTime) {
Eric Laurent0ede8922014-05-09 18:04:42 -0700956 sp<AudioCommand> command = mAudioCommands[0];
Mathias Agopian65ab4712010-07-14 17:59:35 -0700957 mAudioCommands.removeAt(0);
Eric Laurent0ede8922014-05-09 18:04:42 -0700958 mLastCommand = command;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700959
960 switch (command->mCommand) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700961 case SET_VOLUME: {
Eric Laurent0ede8922014-05-09 18:04:42 -0700962 VolumeData *data = (VolumeData *)command->mParam.get();
Steve Block3856b092011-10-20 11:56:00 +0100963 ALOGV("AudioCommandThread() processing set volume stream %d, \
Eric Laurentde070132010-07-13 04:45:46 -0700964 volume %f, output %d", data->mStream, data->mVolume, data->mIO);
Andy Hungfe726a62018-09-27 15:17:25 -0700965 mLock.unlock();
Eric Laurentde070132010-07-13 04:45:46 -0700966 command->mStatus = AudioSystem::setStreamVolume(data->mStream,
967 data->mVolume,
968 data->mIO);
Andy Hungfe726a62018-09-27 15:17:25 -0700969 mLock.lock();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700970 }break;
971 case SET_PARAMETERS: {
Eric Laurent0ede8922014-05-09 18:04:42 -0700972 ParametersData *data = (ParametersData *)command->mParam.get();
Glenn Kastene53b9ea2012-03-12 16:29:55 -0700973 ALOGV("AudioCommandThread() processing set parameters string %s, io %d",
974 data->mKeyValuePairs.string(), data->mIO);
Andy Hungfe726a62018-09-27 15:17:25 -0700975 mLock.unlock();
Glenn Kastene53b9ea2012-03-12 16:29:55 -0700976 command->mStatus = AudioSystem::setParameters(data->mIO, data->mKeyValuePairs);
Andy Hungfe726a62018-09-27 15:17:25 -0700977 mLock.lock();
Glenn Kastene53b9ea2012-03-12 16:29:55 -0700978 }break;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700979 case SET_VOICE_VOLUME: {
Eric Laurent0ede8922014-05-09 18:04:42 -0700980 VoiceVolumeData *data = (VoiceVolumeData *)command->mParam.get();
Steve Block3856b092011-10-20 11:56:00 +0100981 ALOGV("AudioCommandThread() processing set voice volume volume %f",
Eric Laurentde070132010-07-13 04:45:46 -0700982 data->mVolume);
Andy Hungfe726a62018-09-27 15:17:25 -0700983 mLock.unlock();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700984 command->mStatus = AudioSystem::setVoiceVolume(data->mVolume);
Andy Hungfe726a62018-09-27 15:17:25 -0700985 mLock.lock();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700986 }break;
Eric Laurentbfb1b832013-01-07 09:53:42 -0800987 case STOP_OUTPUT: {
Eric Laurent0ede8922014-05-09 18:04:42 -0700988 StopOutputData *data = (StopOutputData *)command->mParam.get();
Eric Laurentd7fe0862018-07-14 16:48:01 -0700989 ALOGV("AudioCommandThread() processing stop output portId %d",
990 data->mPortId);
Eric Laurent59a89232014-06-08 14:14:17 -0700991 svc = mService.promote();
Eric Laurentbfb1b832013-01-07 09:53:42 -0800992 if (svc == 0) {
993 break;
994 }
995 mLock.unlock();
Eric Laurentd7fe0862018-07-14 16:48:01 -0700996 svc->doStopOutput(data->mPortId);
Eric Laurentbfb1b832013-01-07 09:53:42 -0800997 mLock.lock();
Eric Laurentbfb1b832013-01-07 09:53:42 -0800998 }break;
999 case RELEASE_OUTPUT: {
Eric Laurent0ede8922014-05-09 18:04:42 -07001000 ReleaseOutputData *data = (ReleaseOutputData *)command->mParam.get();
Eric Laurentd7fe0862018-07-14 16:48:01 -07001001 ALOGV("AudioCommandThread() processing release output portId %d",
1002 data->mPortId);
Eric Laurent59a89232014-06-08 14:14:17 -07001003 svc = mService.promote();
Eric Laurentbfb1b832013-01-07 09:53:42 -08001004 if (svc == 0) {
1005 break;
1006 }
1007 mLock.unlock();
Eric Laurentd7fe0862018-07-14 16:48:01 -07001008 svc->doReleaseOutput(data->mPortId);
Eric Laurentbfb1b832013-01-07 09:53:42 -08001009 mLock.lock();
Eric Laurentbfb1b832013-01-07 09:53:42 -08001010 }break;
Eric Laurent951f4552014-05-20 10:48:17 -07001011 case CREATE_AUDIO_PATCH: {
1012 CreateAudioPatchData *data = (CreateAudioPatchData *)command->mParam.get();
1013 ALOGV("AudioCommandThread() processing create audio patch");
1014 sp<IAudioFlinger> af = AudioSystem::get_audio_flinger();
1015 if (af == 0) {
1016 command->mStatus = PERMISSION_DENIED;
1017 } else {
Andy Hungfe726a62018-09-27 15:17:25 -07001018 mLock.unlock();
Eric Laurent951f4552014-05-20 10:48:17 -07001019 command->mStatus = af->createAudioPatch(&data->mPatch, &data->mHandle);
Andy Hungfe726a62018-09-27 15:17:25 -07001020 mLock.lock();
Eric Laurent951f4552014-05-20 10:48:17 -07001021 }
1022 } break;
1023 case RELEASE_AUDIO_PATCH: {
1024 ReleaseAudioPatchData *data = (ReleaseAudioPatchData *)command->mParam.get();
1025 ALOGV("AudioCommandThread() processing release audio patch");
1026 sp<IAudioFlinger> af = AudioSystem::get_audio_flinger();
1027 if (af == 0) {
1028 command->mStatus = PERMISSION_DENIED;
1029 } else {
Andy Hungfe726a62018-09-27 15:17:25 -07001030 mLock.unlock();
Eric Laurent951f4552014-05-20 10:48:17 -07001031 command->mStatus = af->releaseAudioPatch(data->mHandle);
Andy Hungfe726a62018-09-27 15:17:25 -07001032 mLock.lock();
Eric Laurent951f4552014-05-20 10:48:17 -07001033 }
1034 } break;
Eric Laurentb52c1522014-05-20 11:27:36 -07001035 case UPDATE_AUDIOPORT_LIST: {
1036 ALOGV("AudioCommandThread() processing update audio port list");
Eric Laurent59a89232014-06-08 14:14:17 -07001037 svc = mService.promote();
Eric Laurentb52c1522014-05-20 11:27:36 -07001038 if (svc == 0) {
1039 break;
1040 }
1041 mLock.unlock();
1042 svc->doOnAudioPortListUpdate();
1043 mLock.lock();
1044 }break;
1045 case UPDATE_AUDIOPATCH_LIST: {
1046 ALOGV("AudioCommandThread() processing update audio patch list");
Eric Laurent59a89232014-06-08 14:14:17 -07001047 svc = mService.promote();
Eric Laurentb52c1522014-05-20 11:27:36 -07001048 if (svc == 0) {
1049 break;
1050 }
1051 mLock.unlock();
1052 svc->doOnAudioPatchListUpdate();
1053 mLock.lock();
1054 }break;
Eric Laurente1715a42014-05-20 11:30:42 -07001055 case SET_AUDIOPORT_CONFIG: {
1056 SetAudioPortConfigData *data = (SetAudioPortConfigData *)command->mParam.get();
1057 ALOGV("AudioCommandThread() processing set port config");
1058 sp<IAudioFlinger> af = AudioSystem::get_audio_flinger();
1059 if (af == 0) {
1060 command->mStatus = PERMISSION_DENIED;
1061 } else {
Andy Hungfe726a62018-09-27 15:17:25 -07001062 mLock.unlock();
Eric Laurente1715a42014-05-20 11:30:42 -07001063 command->mStatus = af->setAudioPortConfig(&data->mConfig);
Andy Hungfe726a62018-09-27 15:17:25 -07001064 mLock.lock();
Eric Laurente1715a42014-05-20 11:30:42 -07001065 }
1066 } break;
Jean-Michel Trivide801052015-04-14 19:10:14 -07001067 case DYN_POLICY_MIX_STATE_UPDATE: {
1068 DynPolicyMixStateUpdateData *data =
1069 (DynPolicyMixStateUpdateData *)command->mParam.get();
Jean-Michel Trivide801052015-04-14 19:10:14 -07001070 ALOGV("AudioCommandThread() processing dyn policy mix state update %s %d",
1071 data->mRegId.string(), data->mState);
1072 svc = mService.promote();
1073 if (svc == 0) {
1074 break;
1075 }
1076 mLock.unlock();
1077 svc->doOnDynamicPolicyMixStateUpdate(data->mRegId, data->mState);
1078 mLock.lock();
1079 } break;
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -08001080 case RECORDING_CONFIGURATION_UPDATE: {
1081 RecordingConfigurationUpdateData *data =
1082 (RecordingConfigurationUpdateData *)command->mParam.get();
1083 ALOGV("AudioCommandThread() processing recording configuration update");
1084 svc = mService.promote();
1085 if (svc == 0) {
1086 break;
1087 }
1088 mLock.unlock();
Jean-Michel Triviac4e4292016-12-22 11:39:31 -08001089 svc->doOnRecordingConfigurationUpdate(data->mEvent, &data->mClientInfo,
Eric Laurenta9f86652018-11-28 17:23:11 -08001090 &data->mClientConfig, data->mClientEffects,
1091 &data->mDeviceConfig, data->mEffects,
1092 data->mPatchHandle, data->mSource);
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -08001093 mLock.lock();
1094 } break;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001095 default:
Steve Block5ff1dd52012-01-05 23:22:43 +00001096 ALOGW("AudioCommandThread() unknown command %d", command->mCommand);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001097 }
Eric Laurent0ede8922014-05-09 18:04:42 -07001098 {
1099 Mutex::Autolock _l(command->mLock);
1100 if (command->mWaitStatus) {
1101 command->mWaitStatus = false;
1102 command->mCond.signal();
1103 }
1104 }
Eric Laurentd7eda8d2016-02-02 17:18:39 -08001105 waitTime = -1;
Zach Janga754b4f2015-10-27 01:29:34 +00001106 // release mLock before releasing strong reference on the service as
1107 // AudioPolicyService destructor calls AudioCommandThread::exit() which
1108 // acquires mLock.
1109 mLock.unlock();
1110 svc.clear();
1111 mLock.lock();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001112 } else {
1113 waitTime = mAudioCommands[0]->mTime - curTime;
1114 break;
1115 }
1116 }
Zach Janga754b4f2015-10-27 01:29:34 +00001117
1118 // release delayed commands wake lock if the queue is empty
1119 if (mAudioCommands.isEmpty()) {
Ricardo Garcia05f2fdc2014-07-24 15:48:24 -07001120 release_wake_lock(mName.string());
Zach Janga754b4f2015-10-27 01:29:34 +00001121 }
1122
1123 // At this stage we have either an empty command queue or the first command in the queue
1124 // has a finite delay. So unless we are exiting it is safe to wait.
1125 if (!exitPending()) {
Eric Laurent59a89232014-06-08 14:14:17 -07001126 ALOGV("AudioCommandThread() going to sleep");
Eric Laurentd7eda8d2016-02-02 17:18:39 -08001127 if (waitTime == -1) {
1128 mWaitWorkCV.wait(mLock);
1129 } else {
1130 mWaitWorkCV.waitRelative(mLock, waitTime);
1131 }
Eric Laurent59a89232014-06-08 14:14:17 -07001132 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001133 }
Ricardo Garcia05f2fdc2014-07-24 15:48:24 -07001134 // release delayed commands wake lock before quitting
1135 if (!mAudioCommands.isEmpty()) {
1136 release_wake_lock(mName.string());
1137 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001138 mLock.unlock();
1139 return false;
1140}
1141
1142status_t AudioPolicyService::AudioCommandThread::dump(int fd)
1143{
1144 const size_t SIZE = 256;
1145 char buffer[SIZE];
1146 String8 result;
1147
1148 snprintf(buffer, SIZE, "AudioCommandThread %p Dump\n", this);
1149 result.append(buffer);
1150 write(fd, result.string(), result.size());
1151
1152 bool locked = tryLock(mLock);
1153 if (!locked) {
1154 String8 result2(kCmdDeadlockedString);
1155 write(fd, result2.string(), result2.size());
1156 }
1157
1158 snprintf(buffer, SIZE, "- Commands:\n");
1159 result = String8(buffer);
1160 result.append(" Command Time Wait pParam\n");
Glenn Kasten8d6a2442012-02-08 14:04:28 -08001161 for (size_t i = 0; i < mAudioCommands.size(); i++) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07001162 mAudioCommands[i]->dump(buffer, SIZE);
1163 result.append(buffer);
1164 }
1165 result.append(" Last Command\n");
Eric Laurent0ede8922014-05-09 18:04:42 -07001166 if (mLastCommand != 0) {
1167 mLastCommand->dump(buffer, SIZE);
1168 result.append(buffer);
1169 } else {
1170 result.append(" none\n");
1171 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001172
1173 write(fd, result.string(), result.size());
1174
1175 if (locked) mLock.unlock();
1176
1177 return NO_ERROR;
1178}
1179
Glenn Kastenfff6d712012-01-12 16:38:12 -08001180status_t AudioPolicyService::AudioCommandThread::volumeCommand(audio_stream_type_t stream,
Eric Laurentde070132010-07-13 04:45:46 -07001181 float volume,
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001182 audio_io_handle_t output,
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_VOLUME;
Eric Laurent0ede8922014-05-09 18:04:42 -07001187 sp<VolumeData> data = new VolumeData();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001188 data->mStream = stream;
1189 data->mVolume = volume;
1190 data->mIO = output;
1191 command->mParam = data;
Eric Laurent0ede8922014-05-09 18:04:42 -07001192 command->mWaitStatus = true;
Steve Block3856b092011-10-20 11:56:00 +01001193 ALOGV("AudioCommandThread() adding set volume stream %d, volume %f, output %d",
Eric Laurentde070132010-07-13 04:45:46 -07001194 stream, volume, output);
Eric Laurent0ede8922014-05-09 18:04:42 -07001195 return sendCommand(command, delayMs);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001196}
1197
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001198status_t AudioPolicyService::AudioCommandThread::parametersCommand(audio_io_handle_t ioHandle,
Dima Zavinfce7a472011-04-19 22:30:36 -07001199 const char *keyValuePairs,
Eric Laurentde070132010-07-13 04:45:46 -07001200 int delayMs)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001201{
Eric Laurent0ede8922014-05-09 18:04:42 -07001202 sp<AudioCommand> command = new AudioCommand();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001203 command->mCommand = SET_PARAMETERS;
Eric Laurent0ede8922014-05-09 18:04:42 -07001204 sp<ParametersData> data = new ParametersData();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001205 data->mIO = ioHandle;
Dima Zavinfce7a472011-04-19 22:30:36 -07001206 data->mKeyValuePairs = String8(keyValuePairs);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001207 command->mParam = data;
Eric Laurent0ede8922014-05-09 18:04:42 -07001208 command->mWaitStatus = true;
Steve Block3856b092011-10-20 11:56:00 +01001209 ALOGV("AudioCommandThread() adding set parameter string %s, io %d ,delay %d",
Dima Zavinfce7a472011-04-19 22:30:36 -07001210 keyValuePairs, ioHandle, delayMs);
Eric Laurent0ede8922014-05-09 18:04:42 -07001211 return sendCommand(command, delayMs);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001212}
1213
1214status_t AudioPolicyService::AudioCommandThread::voiceVolumeCommand(float volume, int delayMs)
1215{
Eric Laurent0ede8922014-05-09 18:04:42 -07001216 sp<AudioCommand> command = new AudioCommand();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001217 command->mCommand = SET_VOICE_VOLUME;
Eric Laurent0ede8922014-05-09 18:04:42 -07001218 sp<VoiceVolumeData> data = new VoiceVolumeData();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001219 data->mVolume = volume;
1220 command->mParam = data;
Eric Laurent0ede8922014-05-09 18:04:42 -07001221 command->mWaitStatus = true;
Steve Block3856b092011-10-20 11:56:00 +01001222 ALOGV("AudioCommandThread() adding set voice volume volume %f", volume);
Eric Laurent0ede8922014-05-09 18:04:42 -07001223 return sendCommand(command, delayMs);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001224}
1225
Eric Laurentd7fe0862018-07-14 16:48:01 -07001226void AudioPolicyService::AudioCommandThread::stopOutputCommand(audio_port_handle_t portId)
Eric Laurentbfb1b832013-01-07 09:53:42 -08001227{
Eric Laurent0ede8922014-05-09 18:04:42 -07001228 sp<AudioCommand> command = new AudioCommand();
Eric Laurentbfb1b832013-01-07 09:53:42 -08001229 command->mCommand = STOP_OUTPUT;
Eric Laurent0ede8922014-05-09 18:04:42 -07001230 sp<StopOutputData> data = new StopOutputData();
Eric Laurentd7fe0862018-07-14 16:48:01 -07001231 data->mPortId = portId;
Jesper Tragardh48412dc2014-03-24 14:12:43 +01001232 command->mParam = data;
Eric Laurentd7fe0862018-07-14 16:48:01 -07001233 ALOGV("AudioCommandThread() adding stop output portId %d", portId);
Eric Laurent0ede8922014-05-09 18:04:42 -07001234 sendCommand(command);
Eric Laurentbfb1b832013-01-07 09:53:42 -08001235}
1236
Eric Laurentd7fe0862018-07-14 16:48:01 -07001237void AudioPolicyService::AudioCommandThread::releaseOutputCommand(audio_port_handle_t portId)
Eric Laurentbfb1b832013-01-07 09:53:42 -08001238{
Eric Laurent0ede8922014-05-09 18:04:42 -07001239 sp<AudioCommand> command = new AudioCommand();
Eric Laurentbfb1b832013-01-07 09:53:42 -08001240 command->mCommand = RELEASE_OUTPUT;
Eric Laurent0ede8922014-05-09 18:04:42 -07001241 sp<ReleaseOutputData> data = new ReleaseOutputData();
Eric Laurentd7fe0862018-07-14 16:48:01 -07001242 data->mPortId = portId;
Jesper Tragardh48412dc2014-03-24 14:12:43 +01001243 command->mParam = data;
Eric Laurentd7fe0862018-07-14 16:48:01 -07001244 ALOGV("AudioCommandThread() adding release output portId %d", portId);
Eric Laurent0ede8922014-05-09 18:04:42 -07001245 sendCommand(command);
1246}
1247
Eric Laurent951f4552014-05-20 10:48:17 -07001248status_t AudioPolicyService::AudioCommandThread::createAudioPatchCommand(
1249 const struct audio_patch *patch,
1250 audio_patch_handle_t *handle,
1251 int delayMs)
1252{
1253 status_t status = NO_ERROR;
1254
1255 sp<AudioCommand> command = new AudioCommand();
1256 command->mCommand = CREATE_AUDIO_PATCH;
1257 CreateAudioPatchData *data = new CreateAudioPatchData();
1258 data->mPatch = *patch;
1259 data->mHandle = *handle;
1260 command->mParam = data;
1261 command->mWaitStatus = true;
1262 ALOGV("AudioCommandThread() adding create patch delay %d", delayMs);
1263 status = sendCommand(command, delayMs);
1264 if (status == NO_ERROR) {
1265 *handle = data->mHandle;
1266 }
1267 return status;
1268}
1269
1270status_t AudioPolicyService::AudioCommandThread::releaseAudioPatchCommand(audio_patch_handle_t handle,
1271 int delayMs)
1272{
1273 sp<AudioCommand> command = new AudioCommand();
1274 command->mCommand = RELEASE_AUDIO_PATCH;
1275 ReleaseAudioPatchData *data = new ReleaseAudioPatchData();
1276 data->mHandle = handle;
1277 command->mParam = data;
1278 command->mWaitStatus = true;
1279 ALOGV("AudioCommandThread() adding release patch delay %d", delayMs);
1280 return sendCommand(command, delayMs);
1281}
1282
Eric Laurentb52c1522014-05-20 11:27:36 -07001283void AudioPolicyService::AudioCommandThread::updateAudioPortListCommand()
1284{
1285 sp<AudioCommand> command = new AudioCommand();
1286 command->mCommand = UPDATE_AUDIOPORT_LIST;
1287 ALOGV("AudioCommandThread() adding update audio port list");
1288 sendCommand(command);
1289}
1290
1291void AudioPolicyService::AudioCommandThread::updateAudioPatchListCommand()
1292{
1293 sp<AudioCommand>command = new AudioCommand();
1294 command->mCommand = UPDATE_AUDIOPATCH_LIST;
1295 ALOGV("AudioCommandThread() adding update audio patch list");
1296 sendCommand(command);
1297}
1298
Eric Laurente1715a42014-05-20 11:30:42 -07001299status_t AudioPolicyService::AudioCommandThread::setAudioPortConfigCommand(
1300 const struct audio_port_config *config, int delayMs)
1301{
1302 sp<AudioCommand> command = new AudioCommand();
1303 command->mCommand = SET_AUDIOPORT_CONFIG;
1304 SetAudioPortConfigData *data = new SetAudioPortConfigData();
1305 data->mConfig = *config;
1306 command->mParam = data;
1307 command->mWaitStatus = true;
1308 ALOGV("AudioCommandThread() adding set port config delay %d", delayMs);
1309 return sendCommand(command, delayMs);
1310}
1311
Jean-Michel Trivide801052015-04-14 19:10:14 -07001312void AudioPolicyService::AudioCommandThread::dynamicPolicyMixStateUpdateCommand(
Chih-Hung Hsiehe964d4e2016-08-09 14:31:32 -07001313 const String8& regId, int32_t state)
Jean-Michel Trivide801052015-04-14 19:10:14 -07001314{
1315 sp<AudioCommand> command = new AudioCommand();
1316 command->mCommand = DYN_POLICY_MIX_STATE_UPDATE;
1317 DynPolicyMixStateUpdateData *data = new DynPolicyMixStateUpdateData();
1318 data->mRegId = regId;
1319 data->mState = state;
1320 command->mParam = data;
1321 ALOGV("AudioCommandThread() sending dynamic policy mix (id=%s) state update to %d",
1322 regId.string(), state);
1323 sendCommand(command);
1324}
1325
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -08001326void AudioPolicyService::AudioCommandThread::recordingConfigurationUpdateCommand(
Eric Laurenta9f86652018-11-28 17:23:11 -08001327 int event,
1328 const record_client_info_t *clientInfo,
1329 const audio_config_base_t *clientConfig,
1330 std::vector<effect_descriptor_t> clientEffects,
1331 const audio_config_base_t *deviceConfig,
1332 std::vector<effect_descriptor_t> effects,
1333 audio_patch_handle_t patchHandle,
1334 audio_source_t source)
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -08001335{
1336 sp<AudioCommand>command = new AudioCommand();
1337 command->mCommand = RECORDING_CONFIGURATION_UPDATE;
1338 RecordingConfigurationUpdateData *data = new RecordingConfigurationUpdateData();
1339 data->mEvent = event;
Jean-Michel Triviac4e4292016-12-22 11:39:31 -08001340 data->mClientInfo = *clientInfo;
Jean-Michel Trivi7281aa92016-02-17 15:33:40 -08001341 data->mClientConfig = *clientConfig;
Eric Laurenta9f86652018-11-28 17:23:11 -08001342 data->mClientEffects = clientEffects;
Jean-Michel Trivi7281aa92016-02-17 15:33:40 -08001343 data->mDeviceConfig = *deviceConfig;
Eric Laurenta9f86652018-11-28 17:23:11 -08001344 data->mEffects = effects;
Jean-Michel Trivi8c7cf3b2016-02-25 17:08:24 -08001345 data->mPatchHandle = patchHandle;
Eric Laurenta9f86652018-11-28 17:23:11 -08001346 data->mSource = source;
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -08001347 command->mParam = data;
Jean-Michel Triviac4e4292016-12-22 11:39:31 -08001348 ALOGV("AudioCommandThread() adding recording configuration update event %d, source %d uid %u",
1349 event, clientInfo->source, clientInfo->uid);
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -08001350 sendCommand(command);
1351}
1352
Eric Laurent0ede8922014-05-09 18:04:42 -07001353status_t AudioPolicyService::AudioCommandThread::sendCommand(sp<AudioCommand>& command, int delayMs)
1354{
1355 {
1356 Mutex::Autolock _l(mLock);
1357 insertCommand_l(command, delayMs);
1358 mWaitWorkCV.signal();
1359 }
1360 Mutex::Autolock _l(command->mLock);
1361 while (command->mWaitStatus) {
1362 nsecs_t timeOutNs = kAudioCommandTimeoutNs + milliseconds(delayMs);
1363 if (command->mCond.waitRelative(command->mLock, timeOutNs) != NO_ERROR) {
1364 command->mStatus = TIMED_OUT;
1365 command->mWaitStatus = false;
1366 }
1367 }
1368 return command->mStatus;
Eric Laurentbfb1b832013-01-07 09:53:42 -08001369}
1370
Mathias Agopian65ab4712010-07-14 17:59:35 -07001371// insertCommand_l() must be called with mLock held
Eric Laurent0ede8922014-05-09 18:04:42 -07001372void AudioPolicyService::AudioCommandThread::insertCommand_l(sp<AudioCommand>& command, int delayMs)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001373{
Glenn Kasten8d6a2442012-02-08 14:04:28 -08001374 ssize_t i; // not size_t because i will count down to -1
Eric Laurent0ede8922014-05-09 18:04:42 -07001375 Vector < sp<AudioCommand> > removedCommands;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001376 command->mTime = systemTime() + milliseconds(delayMs);
1377
1378 // acquire wake lock to make sure delayed commands are processed
Eric Laurentbfb1b832013-01-07 09:53:42 -08001379 if (mAudioCommands.isEmpty()) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07001380 acquire_wake_lock(PARTIAL_WAKE_LOCK, mName.string());
1381 }
1382
1383 // check same pending commands with later time stamps and eliminate them
Ivan Lozano5ff158f2017-10-30 09:06:24 -07001384 for (i = (ssize_t)mAudioCommands.size()-1; i >= 0; i--) {
Eric Laurent0ede8922014-05-09 18:04:42 -07001385 sp<AudioCommand> command2 = mAudioCommands[i];
Mathias Agopian65ab4712010-07-14 17:59:35 -07001386 // commands are sorted by increasing time stamp: no need to scan the rest of mAudioCommands
1387 if (command2->mTime <= command->mTime) break;
Eric Laurente45b48a2014-09-04 16:40:57 -07001388
1389 // create audio patch or release audio patch commands are equivalent
1390 // with regard to filtering
1391 if ((command->mCommand == CREATE_AUDIO_PATCH) ||
1392 (command->mCommand == RELEASE_AUDIO_PATCH)) {
1393 if ((command2->mCommand != CREATE_AUDIO_PATCH) &&
1394 (command2->mCommand != RELEASE_AUDIO_PATCH)) {
1395 continue;
1396 }
1397 } else if (command2->mCommand != command->mCommand) continue;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001398
1399 switch (command->mCommand) {
1400 case SET_PARAMETERS: {
Eric Laurent0ede8922014-05-09 18:04:42 -07001401 ParametersData *data = (ParametersData *)command->mParam.get();
1402 ParametersData *data2 = (ParametersData *)command2->mParam.get();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001403 if (data->mIO != data2->mIO) break;
Steve Block3856b092011-10-20 11:56:00 +01001404 ALOGV("Comparing parameter command %s to new command %s",
Eric Laurentde070132010-07-13 04:45:46 -07001405 data2->mKeyValuePairs.string(), data->mKeyValuePairs.string());
Mathias Agopian65ab4712010-07-14 17:59:35 -07001406 AudioParameter param = AudioParameter(data->mKeyValuePairs);
1407 AudioParameter param2 = AudioParameter(data2->mKeyValuePairs);
1408 for (size_t j = 0; j < param.size(); j++) {
Glenn Kastene53b9ea2012-03-12 16:29:55 -07001409 String8 key;
1410 String8 value;
1411 param.getAt(j, key, value);
1412 for (size_t k = 0; k < param2.size(); k++) {
1413 String8 key2;
1414 String8 value2;
1415 param2.getAt(k, key2, value2);
1416 if (key2 == key) {
1417 param2.remove(key2);
1418 ALOGV("Filtering out parameter %s", key2.string());
1419 break;
1420 }
1421 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001422 }
1423 // if all keys have been filtered out, remove the command.
1424 // otherwise, update the key value pairs
1425 if (param2.size() == 0) {
1426 removedCommands.add(command2);
1427 } else {
1428 data2->mKeyValuePairs = param2.toString();
1429 }
Eric Laurent21e54562013-09-23 12:08:05 -07001430 command->mTime = command2->mTime;
1431 // force delayMs to non 0 so that code below does not request to wait for
1432 // command status as the command is now delayed
1433 delayMs = 1;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001434 } break;
1435
1436 case SET_VOLUME: {
Eric Laurent0ede8922014-05-09 18:04:42 -07001437 VolumeData *data = (VolumeData *)command->mParam.get();
1438 VolumeData *data2 = (VolumeData *)command2->mParam.get();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001439 if (data->mIO != data2->mIO) break;
1440 if (data->mStream != data2->mStream) break;
Steve Block3856b092011-10-20 11:56:00 +01001441 ALOGV("Filtering out volume command on output %d for stream %d",
Eric Laurentde070132010-07-13 04:45:46 -07001442 data->mIO, data->mStream);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001443 removedCommands.add(command2);
Eric Laurent21e54562013-09-23 12:08:05 -07001444 command->mTime = command2->mTime;
1445 // force delayMs to non 0 so that code below does not request to wait for
1446 // command status as the command is now delayed
1447 delayMs = 1;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001448 } break;
Eric Laurente45b48a2014-09-04 16:40:57 -07001449
Eric Laurentbaf35fe2016-07-27 15:36:53 -07001450 case SET_VOICE_VOLUME: {
1451 VoiceVolumeData *data = (VoiceVolumeData *)command->mParam.get();
1452 VoiceVolumeData *data2 = (VoiceVolumeData *)command2->mParam.get();
1453 ALOGV("Filtering out voice volume command value %f replaced by %f",
1454 data2->mVolume, data->mVolume);
1455 removedCommands.add(command2);
1456 command->mTime = command2->mTime;
1457 // force delayMs to non 0 so that code below does not request to wait for
1458 // command status as the command is now delayed
1459 delayMs = 1;
1460 } break;
1461
Eric Laurente45b48a2014-09-04 16:40:57 -07001462 case CREATE_AUDIO_PATCH:
1463 case RELEASE_AUDIO_PATCH: {
1464 audio_patch_handle_t handle;
Haynes Mathew Georgea2d4a6d2014-10-13 13:05:22 -07001465 struct audio_patch patch;
Eric Laurente45b48a2014-09-04 16:40:57 -07001466 if (command->mCommand == CREATE_AUDIO_PATCH) {
1467 handle = ((CreateAudioPatchData *)command->mParam.get())->mHandle;
Haynes Mathew Georgea2d4a6d2014-10-13 13:05:22 -07001468 patch = ((CreateAudioPatchData *)command->mParam.get())->mPatch;
Eric Laurente45b48a2014-09-04 16:40:57 -07001469 } else {
1470 handle = ((ReleaseAudioPatchData *)command->mParam.get())->mHandle;
Mikhail Naganov7be71d22018-05-23 16:51:46 -07001471 memset(&patch, 0, sizeof(patch));
Eric Laurente45b48a2014-09-04 16:40:57 -07001472 }
1473 audio_patch_handle_t handle2;
Haynes Mathew Georgea2d4a6d2014-10-13 13:05:22 -07001474 struct audio_patch patch2;
Eric Laurente45b48a2014-09-04 16:40:57 -07001475 if (command2->mCommand == CREATE_AUDIO_PATCH) {
1476 handle2 = ((CreateAudioPatchData *)command2->mParam.get())->mHandle;
Haynes Mathew Georgea2d4a6d2014-10-13 13:05:22 -07001477 patch2 = ((CreateAudioPatchData *)command2->mParam.get())->mPatch;
Eric Laurente45b48a2014-09-04 16:40:57 -07001478 } else {
1479 handle2 = ((ReleaseAudioPatchData *)command2->mParam.get())->mHandle;
Glenn Kastenf60b6b62015-07-06 10:53:26 -07001480 memset(&patch2, 0, sizeof(patch2));
Eric Laurente45b48a2014-09-04 16:40:57 -07001481 }
1482 if (handle != handle2) break;
Haynes Mathew Georgea2d4a6d2014-10-13 13:05:22 -07001483 /* Filter CREATE_AUDIO_PATCH commands only when they are issued for
1484 same output. */
1485 if( (command->mCommand == CREATE_AUDIO_PATCH) &&
1486 (command2->mCommand == CREATE_AUDIO_PATCH) ) {
1487 bool isOutputDiff = false;
1488 if (patch.num_sources == patch2.num_sources) {
1489 for (unsigned count = 0; count < patch.num_sources; count++) {
1490 if (patch.sources[count].id != patch2.sources[count].id) {
1491 isOutputDiff = true;
1492 break;
1493 }
1494 }
1495 if (isOutputDiff)
1496 break;
1497 }
1498 }
Eric Laurente45b48a2014-09-04 16:40:57 -07001499 ALOGV("Filtering out %s audio patch command for handle %d",
1500 (command->mCommand == CREATE_AUDIO_PATCH) ? "create" : "release", handle);
1501 removedCommands.add(command2);
1502 command->mTime = command2->mTime;
1503 // force delayMs to non 0 so that code below does not request to wait for
1504 // command status as the command is now delayed
1505 delayMs = 1;
1506 } break;
1507
Jean-Michel Trivide801052015-04-14 19:10:14 -07001508 case DYN_POLICY_MIX_STATE_UPDATE: {
1509
1510 } break;
1511
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -08001512 case RECORDING_CONFIGURATION_UPDATE: {
1513
1514 } break;
1515
Mathias Agopian65ab4712010-07-14 17:59:35 -07001516 default:
1517 break;
1518 }
1519 }
1520
1521 // remove filtered commands
1522 for (size_t j = 0; j < removedCommands.size(); j++) {
1523 // removed commands always have time stamps greater than current command
1524 for (size_t k = i + 1; k < mAudioCommands.size(); k++) {
Eric Laurent0ede8922014-05-09 18:04:42 -07001525 if (mAudioCommands[k].get() == removedCommands[j].get()) {
Steve Block3856b092011-10-20 11:56:00 +01001526 ALOGV("suppressing command: %d", mAudioCommands[k]->mCommand);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001527 mAudioCommands.removeAt(k);
1528 break;
1529 }
1530 }
1531 }
1532 removedCommands.clear();
1533
Eric Laurentaa79bef2015-01-15 14:33:51 -08001534 // Disable wait for status if delay is not 0.
1535 // Except for create audio patch command because the returned patch handle
1536 // is needed by audio policy manager
1537 if (delayMs != 0 && command->mCommand != CREATE_AUDIO_PATCH) {
Eric Laurentcec4abb2012-07-03 12:23:02 -07001538 command->mWaitStatus = false;
1539 }
Eric Laurentcec4abb2012-07-03 12:23:02 -07001540
Mathias Agopian65ab4712010-07-14 17:59:35 -07001541 // insert command at the right place according to its time stamp
Eric Laurent1e693b52014-07-09 15:03:28 -07001542 ALOGV("inserting command: %d at index %zd, num commands %zu",
1543 command->mCommand, i+1, mAudioCommands.size());
Mathias Agopian65ab4712010-07-14 17:59:35 -07001544 mAudioCommands.insertAt(command, i + 1);
1545}
1546
1547void AudioPolicyService::AudioCommandThread::exit()
1548{
Steve Block3856b092011-10-20 11:56:00 +01001549 ALOGV("AudioCommandThread::exit");
Mathias Agopian65ab4712010-07-14 17:59:35 -07001550 {
1551 AutoMutex _l(mLock);
1552 requestExit();
1553 mWaitWorkCV.signal();
1554 }
Zach Janga754b4f2015-10-27 01:29:34 +00001555 // Note that we can call it from the thread loop if all other references have been released
1556 // but it will safely return WOULD_BLOCK in this case
Mathias Agopian65ab4712010-07-14 17:59:35 -07001557 requestExitAndWait();
1558}
1559
1560void AudioPolicyService::AudioCommandThread::AudioCommand::dump(char* buffer, size_t size)
1561{
1562 snprintf(buffer, size, " %02d %06d.%03d %01u %p\n",
1563 mCommand,
1564 (int)ns2s(mTime),
1565 (int)ns2ms(mTime)%1000,
1566 mWaitStatus,
Eric Laurent0ede8922014-05-09 18:04:42 -07001567 mParam.get());
Mathias Agopian65ab4712010-07-14 17:59:35 -07001568}
1569
Dima Zavinfce7a472011-04-19 22:30:36 -07001570/******* helpers for the service_ops callbacks defined below *********/
1571void AudioPolicyService::setParameters(audio_io_handle_t ioHandle,
1572 const char *keyValuePairs,
1573 int delayMs)
1574{
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001575 mAudioCommandThread->parametersCommand(ioHandle, keyValuePairs,
Dima Zavinfce7a472011-04-19 22:30:36 -07001576 delayMs);
1577}
1578
1579int AudioPolicyService::setStreamVolume(audio_stream_type_t stream,
1580 float volume,
1581 audio_io_handle_t output,
1582 int delayMs)
1583{
Glenn Kastenfff6d712012-01-12 16:38:12 -08001584 return (int)mAudioCommandThread->volumeCommand(stream, volume,
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001585 output, delayMs);
Dima Zavinfce7a472011-04-19 22:30:36 -07001586}
1587
Dima Zavinfce7a472011-04-19 22:30:36 -07001588int AudioPolicyService::setVoiceVolume(float volume, int delayMs)
1589{
1590 return (int)mAudioCommandThread->voiceVolumeCommand(volume, delayMs);
1591}
1592
Dima Zavinfce7a472011-04-19 22:30:36 -07001593extern "C" {
Eric Laurent2d388ec2014-03-07 13:25:54 -08001594audio_module_handle_t aps_load_hw_module(void *service __unused,
1595 const char *name);
1596audio_io_handle_t aps_open_output(void *service __unused,
Eric Laurenta4c5a552012-03-29 10:12:40 -07001597 audio_devices_t *pDevices,
1598 uint32_t *pSamplingRate,
1599 audio_format_t *pFormat,
1600 audio_channel_mask_t *pChannelMask,
1601 uint32_t *pLatencyMs,
Eric Laurent2d388ec2014-03-07 13:25:54 -08001602 audio_output_flags_t flags);
Eric Laurenta4c5a552012-03-29 10:12:40 -07001603
Eric Laurent2d388ec2014-03-07 13:25:54 -08001604audio_io_handle_t aps_open_output_on_module(void *service __unused,
Eric Laurenta4c5a552012-03-29 10:12:40 -07001605 audio_module_handle_t module,
1606 audio_devices_t *pDevices,
1607 uint32_t *pSamplingRate,
1608 audio_format_t *pFormat,
1609 audio_channel_mask_t *pChannelMask,
1610 uint32_t *pLatencyMs,
Richard Fitzgeraldad3af332013-03-25 16:54:37 +00001611 audio_output_flags_t flags,
Eric Laurent2d388ec2014-03-07 13:25:54 -08001612 const audio_offload_info_t *offloadInfo);
1613audio_io_handle_t aps_open_dup_output(void *service __unused,
Dima Zavinfce7a472011-04-19 22:30:36 -07001614 audio_io_handle_t output1,
Eric Laurent2d388ec2014-03-07 13:25:54 -08001615 audio_io_handle_t output2);
1616int aps_close_output(void *service __unused, audio_io_handle_t output);
1617int aps_suspend_output(void *service __unused, audio_io_handle_t output);
1618int aps_restore_output(void *service __unused, audio_io_handle_t output);
1619audio_io_handle_t aps_open_input(void *service __unused,
Eric Laurenta4c5a552012-03-29 10:12:40 -07001620 audio_devices_t *pDevices,
1621 uint32_t *pSamplingRate,
1622 audio_format_t *pFormat,
1623 audio_channel_mask_t *pChannelMask,
Eric Laurent2d388ec2014-03-07 13:25:54 -08001624 audio_in_acoustics_t acoustics __unused);
1625audio_io_handle_t aps_open_input_on_module(void *service __unused,
Eric Laurenta4c5a552012-03-29 10:12:40 -07001626 audio_module_handle_t module,
1627 audio_devices_t *pDevices,
1628 uint32_t *pSamplingRate,
1629 audio_format_t *pFormat,
Eric Laurent2d388ec2014-03-07 13:25:54 -08001630 audio_channel_mask_t *pChannelMask);
1631int aps_close_input(void *service __unused, audio_io_handle_t input);
1632int aps_invalidate_stream(void *service __unused, audio_stream_type_t stream);
Glenn Kastend848eb42016-03-08 13:42:11 -08001633int aps_move_effects(void *service __unused, audio_session_t session,
Dima Zavinfce7a472011-04-19 22:30:36 -07001634 audio_io_handle_t src_output,
Eric Laurent2d388ec2014-03-07 13:25:54 -08001635 audio_io_handle_t dst_output);
1636char * aps_get_parameters(void *service __unused, audio_io_handle_t io_handle,
1637 const char *keys);
1638void aps_set_parameters(void *service, audio_io_handle_t io_handle,
1639 const char *kv_pairs, int delay_ms);
1640int aps_set_stream_volume(void *service, audio_stream_type_t stream,
Dima Zavinfce7a472011-04-19 22:30:36 -07001641 float volume, audio_io_handle_t output,
Eric Laurent2d388ec2014-03-07 13:25:54 -08001642 int delay_ms);
Eric Laurent2d388ec2014-03-07 13:25:54 -08001643int aps_set_voice_volume(void *service, float volume, int delay_ms);
1644};
Dima Zavinfce7a472011-04-19 22:30:36 -07001645
Mikhail Naganov1b2a7942017-12-08 10:18:09 -08001646} // namespace android