blob: 416817ff1e409c397881b6a8015ed610ea044207 [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 }
883 if (uid.second.second == ActivityManager::PROCESS_STATE_TOP ||
884 uid.second.second == ActivityManager::PROCESS_STATE_FOREGROUND_SERVICE ||
885 uid.second.second == ActivityManager::PROCESS_STATE_BOUND_FOREGROUND_SERVICE) {
886 return true;
887 }
888 }
889 return false;
890}
891
Eric Laurentb78763e2018-10-17 10:08:02 -0700892bool AudioPolicyService::UidPolicy::isA11yUid(uid_t uid)
893{
894 std::vector<uid_t>::iterator it = find(mA11yUids.begin(), mA11yUids.end(), uid);
895 return it != mA11yUids.end();
896}
897
Michael Groovercfd28302018-12-11 19:16:46 -0800898// ----------- AudioPolicyService::SensorPrivacyService implementation ----------
899void AudioPolicyService::SensorPrivacyPolicy::registerSelf() {
900 SensorPrivacyManager spm;
901 mSensorPrivacyEnabled = spm.isSensorPrivacyEnabled();
902 spm.addSensorPrivacyListener(this);
903}
904
905void AudioPolicyService::SensorPrivacyPolicy::unregisterSelf() {
906 SensorPrivacyManager spm;
907 spm.removeSensorPrivacyListener(this);
908}
909
910bool AudioPolicyService::SensorPrivacyPolicy::isSensorPrivacyEnabled() {
911 return mSensorPrivacyEnabled;
912}
913
914binder::Status AudioPolicyService::SensorPrivacyPolicy::onSensorPrivacyChanged(bool enabled) {
915 mSensorPrivacyEnabled = enabled;
916 sp<AudioPolicyService> service = mService.promote();
917 if (service != nullptr) {
918 service->updateUidStates();
919 }
920 return binder::Status::ok();
921}
922
Mathias Agopian65ab4712010-07-14 17:59:35 -0700923// ----------- AudioPolicyService::AudioCommandThread implementation ----------
924
Eric Laurentbfb1b832013-01-07 09:53:42 -0800925AudioPolicyService::AudioCommandThread::AudioCommandThread(String8 name,
926 const wp<AudioPolicyService>& service)
927 : Thread(false), mName(name), mService(service)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700928{
Mathias Agopian65ab4712010-07-14 17:59:35 -0700929}
930
931
932AudioPolicyService::AudioCommandThread::~AudioCommandThread()
933{
Eric Laurentbfb1b832013-01-07 09:53:42 -0800934 if (!mAudioCommands.isEmpty()) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700935 release_wake_lock(mName.string());
936 }
937 mAudioCommands.clear();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700938}
939
940void AudioPolicyService::AudioCommandThread::onFirstRef()
941{
Eric Laurentbfb1b832013-01-07 09:53:42 -0800942 run(mName.string(), ANDROID_PRIORITY_AUDIO);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700943}
944
945bool AudioPolicyService::AudioCommandThread::threadLoop()
946{
Eric Laurentd7eda8d2016-02-02 17:18:39 -0800947 nsecs_t waitTime = -1;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700948
949 mLock.lock();
950 while (!exitPending())
951 {
Eric Laurent59a89232014-06-08 14:14:17 -0700952 sp<AudioPolicyService> svc;
953 while (!mAudioCommands.isEmpty() && !exitPending()) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700954 nsecs_t curTime = systemTime();
955 // commands are sorted by increasing time stamp: execute them from index 0 and up
956 if (mAudioCommands[0]->mTime <= curTime) {
Eric Laurent0ede8922014-05-09 18:04:42 -0700957 sp<AudioCommand> command = mAudioCommands[0];
Mathias Agopian65ab4712010-07-14 17:59:35 -0700958 mAudioCommands.removeAt(0);
Eric Laurent0ede8922014-05-09 18:04:42 -0700959 mLastCommand = command;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700960
961 switch (command->mCommand) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700962 case SET_VOLUME: {
Eric Laurent0ede8922014-05-09 18:04:42 -0700963 VolumeData *data = (VolumeData *)command->mParam.get();
Steve Block3856b092011-10-20 11:56:00 +0100964 ALOGV("AudioCommandThread() processing set volume stream %d, \
Eric Laurentde070132010-07-13 04:45:46 -0700965 volume %f, output %d", data->mStream, data->mVolume, data->mIO);
Andy Hungfe726a62018-09-27 15:17:25 -0700966 mLock.unlock();
Eric Laurentde070132010-07-13 04:45:46 -0700967 command->mStatus = AudioSystem::setStreamVolume(data->mStream,
968 data->mVolume,
969 data->mIO);
Andy Hungfe726a62018-09-27 15:17:25 -0700970 mLock.lock();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700971 }break;
972 case SET_PARAMETERS: {
Eric Laurent0ede8922014-05-09 18:04:42 -0700973 ParametersData *data = (ParametersData *)command->mParam.get();
Glenn Kastene53b9ea2012-03-12 16:29:55 -0700974 ALOGV("AudioCommandThread() processing set parameters string %s, io %d",
975 data->mKeyValuePairs.string(), data->mIO);
Andy Hungfe726a62018-09-27 15:17:25 -0700976 mLock.unlock();
Glenn Kastene53b9ea2012-03-12 16:29:55 -0700977 command->mStatus = AudioSystem::setParameters(data->mIO, data->mKeyValuePairs);
Andy Hungfe726a62018-09-27 15:17:25 -0700978 mLock.lock();
Glenn Kastene53b9ea2012-03-12 16:29:55 -0700979 }break;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700980 case SET_VOICE_VOLUME: {
Eric Laurent0ede8922014-05-09 18:04:42 -0700981 VoiceVolumeData *data = (VoiceVolumeData *)command->mParam.get();
Steve Block3856b092011-10-20 11:56:00 +0100982 ALOGV("AudioCommandThread() processing set voice volume volume %f",
Eric Laurentde070132010-07-13 04:45:46 -0700983 data->mVolume);
Andy Hungfe726a62018-09-27 15:17:25 -0700984 mLock.unlock();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700985 command->mStatus = AudioSystem::setVoiceVolume(data->mVolume);
Andy Hungfe726a62018-09-27 15:17:25 -0700986 mLock.lock();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700987 }break;
Eric Laurentbfb1b832013-01-07 09:53:42 -0800988 case STOP_OUTPUT: {
Eric Laurent0ede8922014-05-09 18:04:42 -0700989 StopOutputData *data = (StopOutputData *)command->mParam.get();
Eric Laurentd7fe0862018-07-14 16:48:01 -0700990 ALOGV("AudioCommandThread() processing stop output portId %d",
991 data->mPortId);
Eric Laurent59a89232014-06-08 14:14:17 -0700992 svc = mService.promote();
Eric Laurentbfb1b832013-01-07 09:53:42 -0800993 if (svc == 0) {
994 break;
995 }
996 mLock.unlock();
Eric Laurentd7fe0862018-07-14 16:48:01 -0700997 svc->doStopOutput(data->mPortId);
Eric Laurentbfb1b832013-01-07 09:53:42 -0800998 mLock.lock();
Eric Laurentbfb1b832013-01-07 09:53:42 -0800999 }break;
1000 case RELEASE_OUTPUT: {
Eric Laurent0ede8922014-05-09 18:04:42 -07001001 ReleaseOutputData *data = (ReleaseOutputData *)command->mParam.get();
Eric Laurentd7fe0862018-07-14 16:48:01 -07001002 ALOGV("AudioCommandThread() processing release output portId %d",
1003 data->mPortId);
Eric Laurent59a89232014-06-08 14:14:17 -07001004 svc = mService.promote();
Eric Laurentbfb1b832013-01-07 09:53:42 -08001005 if (svc == 0) {
1006 break;
1007 }
1008 mLock.unlock();
Eric Laurentd7fe0862018-07-14 16:48:01 -07001009 svc->doReleaseOutput(data->mPortId);
Eric Laurentbfb1b832013-01-07 09:53:42 -08001010 mLock.lock();
Eric Laurentbfb1b832013-01-07 09:53:42 -08001011 }break;
Eric Laurent951f4552014-05-20 10:48:17 -07001012 case CREATE_AUDIO_PATCH: {
1013 CreateAudioPatchData *data = (CreateAudioPatchData *)command->mParam.get();
1014 ALOGV("AudioCommandThread() processing create audio patch");
1015 sp<IAudioFlinger> af = AudioSystem::get_audio_flinger();
1016 if (af == 0) {
1017 command->mStatus = PERMISSION_DENIED;
1018 } else {
Andy Hungfe726a62018-09-27 15:17:25 -07001019 mLock.unlock();
Eric Laurent951f4552014-05-20 10:48:17 -07001020 command->mStatus = af->createAudioPatch(&data->mPatch, &data->mHandle);
Andy Hungfe726a62018-09-27 15:17:25 -07001021 mLock.lock();
Eric Laurent951f4552014-05-20 10:48:17 -07001022 }
1023 } break;
1024 case RELEASE_AUDIO_PATCH: {
1025 ReleaseAudioPatchData *data = (ReleaseAudioPatchData *)command->mParam.get();
1026 ALOGV("AudioCommandThread() processing release audio patch");
1027 sp<IAudioFlinger> af = AudioSystem::get_audio_flinger();
1028 if (af == 0) {
1029 command->mStatus = PERMISSION_DENIED;
1030 } else {
Andy Hungfe726a62018-09-27 15:17:25 -07001031 mLock.unlock();
Eric Laurent951f4552014-05-20 10:48:17 -07001032 command->mStatus = af->releaseAudioPatch(data->mHandle);
Andy Hungfe726a62018-09-27 15:17:25 -07001033 mLock.lock();
Eric Laurent951f4552014-05-20 10:48:17 -07001034 }
1035 } break;
Eric Laurentb52c1522014-05-20 11:27:36 -07001036 case UPDATE_AUDIOPORT_LIST: {
1037 ALOGV("AudioCommandThread() processing update audio port list");
Eric Laurent59a89232014-06-08 14:14:17 -07001038 svc = mService.promote();
Eric Laurentb52c1522014-05-20 11:27:36 -07001039 if (svc == 0) {
1040 break;
1041 }
1042 mLock.unlock();
1043 svc->doOnAudioPortListUpdate();
1044 mLock.lock();
1045 }break;
1046 case UPDATE_AUDIOPATCH_LIST: {
1047 ALOGV("AudioCommandThread() processing update audio patch list");
Eric Laurent59a89232014-06-08 14:14:17 -07001048 svc = mService.promote();
Eric Laurentb52c1522014-05-20 11:27:36 -07001049 if (svc == 0) {
1050 break;
1051 }
1052 mLock.unlock();
1053 svc->doOnAudioPatchListUpdate();
1054 mLock.lock();
1055 }break;
Eric Laurente1715a42014-05-20 11:30:42 -07001056 case SET_AUDIOPORT_CONFIG: {
1057 SetAudioPortConfigData *data = (SetAudioPortConfigData *)command->mParam.get();
1058 ALOGV("AudioCommandThread() processing set port config");
1059 sp<IAudioFlinger> af = AudioSystem::get_audio_flinger();
1060 if (af == 0) {
1061 command->mStatus = PERMISSION_DENIED;
1062 } else {
Andy Hungfe726a62018-09-27 15:17:25 -07001063 mLock.unlock();
Eric Laurente1715a42014-05-20 11:30:42 -07001064 command->mStatus = af->setAudioPortConfig(&data->mConfig);
Andy Hungfe726a62018-09-27 15:17:25 -07001065 mLock.lock();
Eric Laurente1715a42014-05-20 11:30:42 -07001066 }
1067 } break;
Jean-Michel Trivide801052015-04-14 19:10:14 -07001068 case DYN_POLICY_MIX_STATE_UPDATE: {
1069 DynPolicyMixStateUpdateData *data =
1070 (DynPolicyMixStateUpdateData *)command->mParam.get();
Jean-Michel Trivide801052015-04-14 19:10:14 -07001071 ALOGV("AudioCommandThread() processing dyn policy mix state update %s %d",
1072 data->mRegId.string(), data->mState);
1073 svc = mService.promote();
1074 if (svc == 0) {
1075 break;
1076 }
1077 mLock.unlock();
1078 svc->doOnDynamicPolicyMixStateUpdate(data->mRegId, data->mState);
1079 mLock.lock();
1080 } break;
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -08001081 case RECORDING_CONFIGURATION_UPDATE: {
1082 RecordingConfigurationUpdateData *data =
1083 (RecordingConfigurationUpdateData *)command->mParam.get();
1084 ALOGV("AudioCommandThread() processing recording configuration update");
1085 svc = mService.promote();
1086 if (svc == 0) {
1087 break;
1088 }
1089 mLock.unlock();
Jean-Michel Triviac4e4292016-12-22 11:39:31 -08001090 svc->doOnRecordingConfigurationUpdate(data->mEvent, &data->mClientInfo,
Eric Laurenta9f86652018-11-28 17:23:11 -08001091 &data->mClientConfig, data->mClientEffects,
1092 &data->mDeviceConfig, data->mEffects,
1093 data->mPatchHandle, data->mSource);
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -08001094 mLock.lock();
1095 } break;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001096 default:
Steve Block5ff1dd52012-01-05 23:22:43 +00001097 ALOGW("AudioCommandThread() unknown command %d", command->mCommand);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001098 }
Eric Laurent0ede8922014-05-09 18:04:42 -07001099 {
1100 Mutex::Autolock _l(command->mLock);
1101 if (command->mWaitStatus) {
1102 command->mWaitStatus = false;
1103 command->mCond.signal();
1104 }
1105 }
Eric Laurentd7eda8d2016-02-02 17:18:39 -08001106 waitTime = -1;
Zach Janga754b4f2015-10-27 01:29:34 +00001107 // release mLock before releasing strong reference on the service as
1108 // AudioPolicyService destructor calls AudioCommandThread::exit() which
1109 // acquires mLock.
1110 mLock.unlock();
1111 svc.clear();
1112 mLock.lock();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001113 } else {
1114 waitTime = mAudioCommands[0]->mTime - curTime;
1115 break;
1116 }
1117 }
Zach Janga754b4f2015-10-27 01:29:34 +00001118
1119 // release delayed commands wake lock if the queue is empty
1120 if (mAudioCommands.isEmpty()) {
Ricardo Garcia05f2fdc2014-07-24 15:48:24 -07001121 release_wake_lock(mName.string());
Zach Janga754b4f2015-10-27 01:29:34 +00001122 }
1123
1124 // At this stage we have either an empty command queue or the first command in the queue
1125 // has a finite delay. So unless we are exiting it is safe to wait.
1126 if (!exitPending()) {
Eric Laurent59a89232014-06-08 14:14:17 -07001127 ALOGV("AudioCommandThread() going to sleep");
Eric Laurentd7eda8d2016-02-02 17:18:39 -08001128 if (waitTime == -1) {
1129 mWaitWorkCV.wait(mLock);
1130 } else {
1131 mWaitWorkCV.waitRelative(mLock, waitTime);
1132 }
Eric Laurent59a89232014-06-08 14:14:17 -07001133 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001134 }
Ricardo Garcia05f2fdc2014-07-24 15:48:24 -07001135 // release delayed commands wake lock before quitting
1136 if (!mAudioCommands.isEmpty()) {
1137 release_wake_lock(mName.string());
1138 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001139 mLock.unlock();
1140 return false;
1141}
1142
1143status_t AudioPolicyService::AudioCommandThread::dump(int fd)
1144{
1145 const size_t SIZE = 256;
1146 char buffer[SIZE];
1147 String8 result;
1148
1149 snprintf(buffer, SIZE, "AudioCommandThread %p Dump\n", this);
1150 result.append(buffer);
1151 write(fd, result.string(), result.size());
1152
1153 bool locked = tryLock(mLock);
1154 if (!locked) {
1155 String8 result2(kCmdDeadlockedString);
1156 write(fd, result2.string(), result2.size());
1157 }
1158
1159 snprintf(buffer, SIZE, "- Commands:\n");
1160 result = String8(buffer);
1161 result.append(" Command Time Wait pParam\n");
Glenn Kasten8d6a2442012-02-08 14:04:28 -08001162 for (size_t i = 0; i < mAudioCommands.size(); i++) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07001163 mAudioCommands[i]->dump(buffer, SIZE);
1164 result.append(buffer);
1165 }
1166 result.append(" Last Command\n");
Eric Laurent0ede8922014-05-09 18:04:42 -07001167 if (mLastCommand != 0) {
1168 mLastCommand->dump(buffer, SIZE);
1169 result.append(buffer);
1170 } else {
1171 result.append(" none\n");
1172 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001173
1174 write(fd, result.string(), result.size());
1175
1176 if (locked) mLock.unlock();
1177
1178 return NO_ERROR;
1179}
1180
Glenn Kastenfff6d712012-01-12 16:38:12 -08001181status_t AudioPolicyService::AudioCommandThread::volumeCommand(audio_stream_type_t stream,
Eric Laurentde070132010-07-13 04:45:46 -07001182 float volume,
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001183 audio_io_handle_t output,
Eric Laurentde070132010-07-13 04:45:46 -07001184 int delayMs)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001185{
Eric Laurent0ede8922014-05-09 18:04:42 -07001186 sp<AudioCommand> command = new AudioCommand();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001187 command->mCommand = SET_VOLUME;
Eric Laurent0ede8922014-05-09 18:04:42 -07001188 sp<VolumeData> data = new VolumeData();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001189 data->mStream = stream;
1190 data->mVolume = volume;
1191 data->mIO = output;
1192 command->mParam = data;
Eric Laurent0ede8922014-05-09 18:04:42 -07001193 command->mWaitStatus = true;
Steve Block3856b092011-10-20 11:56:00 +01001194 ALOGV("AudioCommandThread() adding set volume stream %d, volume %f, output %d",
Eric Laurentde070132010-07-13 04:45:46 -07001195 stream, volume, output);
Eric Laurent0ede8922014-05-09 18:04:42 -07001196 return sendCommand(command, delayMs);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001197}
1198
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001199status_t AudioPolicyService::AudioCommandThread::parametersCommand(audio_io_handle_t ioHandle,
Dima Zavinfce7a472011-04-19 22:30:36 -07001200 const char *keyValuePairs,
Eric Laurentde070132010-07-13 04:45:46 -07001201 int delayMs)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001202{
Eric Laurent0ede8922014-05-09 18:04:42 -07001203 sp<AudioCommand> command = new AudioCommand();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001204 command->mCommand = SET_PARAMETERS;
Eric Laurent0ede8922014-05-09 18:04:42 -07001205 sp<ParametersData> data = new ParametersData();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001206 data->mIO = ioHandle;
Dima Zavinfce7a472011-04-19 22:30:36 -07001207 data->mKeyValuePairs = String8(keyValuePairs);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001208 command->mParam = data;
Eric Laurent0ede8922014-05-09 18:04:42 -07001209 command->mWaitStatus = true;
Steve Block3856b092011-10-20 11:56:00 +01001210 ALOGV("AudioCommandThread() adding set parameter string %s, io %d ,delay %d",
Dima Zavinfce7a472011-04-19 22:30:36 -07001211 keyValuePairs, ioHandle, delayMs);
Eric Laurent0ede8922014-05-09 18:04:42 -07001212 return sendCommand(command, delayMs);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001213}
1214
1215status_t AudioPolicyService::AudioCommandThread::voiceVolumeCommand(float volume, int delayMs)
1216{
Eric Laurent0ede8922014-05-09 18:04:42 -07001217 sp<AudioCommand> command = new AudioCommand();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001218 command->mCommand = SET_VOICE_VOLUME;
Eric Laurent0ede8922014-05-09 18:04:42 -07001219 sp<VoiceVolumeData> data = new VoiceVolumeData();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001220 data->mVolume = volume;
1221 command->mParam = data;
Eric Laurent0ede8922014-05-09 18:04:42 -07001222 command->mWaitStatus = true;
Steve Block3856b092011-10-20 11:56:00 +01001223 ALOGV("AudioCommandThread() adding set voice volume volume %f", volume);
Eric Laurent0ede8922014-05-09 18:04:42 -07001224 return sendCommand(command, delayMs);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001225}
1226
Eric Laurentd7fe0862018-07-14 16:48:01 -07001227void AudioPolicyService::AudioCommandThread::stopOutputCommand(audio_port_handle_t portId)
Eric Laurentbfb1b832013-01-07 09:53:42 -08001228{
Eric Laurent0ede8922014-05-09 18:04:42 -07001229 sp<AudioCommand> command = new AudioCommand();
Eric Laurentbfb1b832013-01-07 09:53:42 -08001230 command->mCommand = STOP_OUTPUT;
Eric Laurent0ede8922014-05-09 18:04:42 -07001231 sp<StopOutputData> data = new StopOutputData();
Eric Laurentd7fe0862018-07-14 16:48:01 -07001232 data->mPortId = portId;
Jesper Tragardh48412dc2014-03-24 14:12:43 +01001233 command->mParam = data;
Eric Laurentd7fe0862018-07-14 16:48:01 -07001234 ALOGV("AudioCommandThread() adding stop output portId %d", portId);
Eric Laurent0ede8922014-05-09 18:04:42 -07001235 sendCommand(command);
Eric Laurentbfb1b832013-01-07 09:53:42 -08001236}
1237
Eric Laurentd7fe0862018-07-14 16:48:01 -07001238void AudioPolicyService::AudioCommandThread::releaseOutputCommand(audio_port_handle_t portId)
Eric Laurentbfb1b832013-01-07 09:53:42 -08001239{
Eric Laurent0ede8922014-05-09 18:04:42 -07001240 sp<AudioCommand> command = new AudioCommand();
Eric Laurentbfb1b832013-01-07 09:53:42 -08001241 command->mCommand = RELEASE_OUTPUT;
Eric Laurent0ede8922014-05-09 18:04:42 -07001242 sp<ReleaseOutputData> data = new ReleaseOutputData();
Eric Laurentd7fe0862018-07-14 16:48:01 -07001243 data->mPortId = portId;
Jesper Tragardh48412dc2014-03-24 14:12:43 +01001244 command->mParam = data;
Eric Laurentd7fe0862018-07-14 16:48:01 -07001245 ALOGV("AudioCommandThread() adding release output portId %d", portId);
Eric Laurent0ede8922014-05-09 18:04:42 -07001246 sendCommand(command);
1247}
1248
Eric Laurent951f4552014-05-20 10:48:17 -07001249status_t AudioPolicyService::AudioCommandThread::createAudioPatchCommand(
1250 const struct audio_patch *patch,
1251 audio_patch_handle_t *handle,
1252 int delayMs)
1253{
1254 status_t status = NO_ERROR;
1255
1256 sp<AudioCommand> command = new AudioCommand();
1257 command->mCommand = CREATE_AUDIO_PATCH;
1258 CreateAudioPatchData *data = new CreateAudioPatchData();
1259 data->mPatch = *patch;
1260 data->mHandle = *handle;
1261 command->mParam = data;
1262 command->mWaitStatus = true;
1263 ALOGV("AudioCommandThread() adding create patch delay %d", delayMs);
1264 status = sendCommand(command, delayMs);
1265 if (status == NO_ERROR) {
1266 *handle = data->mHandle;
1267 }
1268 return status;
1269}
1270
1271status_t AudioPolicyService::AudioCommandThread::releaseAudioPatchCommand(audio_patch_handle_t handle,
1272 int delayMs)
1273{
1274 sp<AudioCommand> command = new AudioCommand();
1275 command->mCommand = RELEASE_AUDIO_PATCH;
1276 ReleaseAudioPatchData *data = new ReleaseAudioPatchData();
1277 data->mHandle = handle;
1278 command->mParam = data;
1279 command->mWaitStatus = true;
1280 ALOGV("AudioCommandThread() adding release patch delay %d", delayMs);
1281 return sendCommand(command, delayMs);
1282}
1283
Eric Laurentb52c1522014-05-20 11:27:36 -07001284void AudioPolicyService::AudioCommandThread::updateAudioPortListCommand()
1285{
1286 sp<AudioCommand> command = new AudioCommand();
1287 command->mCommand = UPDATE_AUDIOPORT_LIST;
1288 ALOGV("AudioCommandThread() adding update audio port list");
1289 sendCommand(command);
1290}
1291
1292void AudioPolicyService::AudioCommandThread::updateAudioPatchListCommand()
1293{
1294 sp<AudioCommand>command = new AudioCommand();
1295 command->mCommand = UPDATE_AUDIOPATCH_LIST;
1296 ALOGV("AudioCommandThread() adding update audio patch list");
1297 sendCommand(command);
1298}
1299
Eric Laurente1715a42014-05-20 11:30:42 -07001300status_t AudioPolicyService::AudioCommandThread::setAudioPortConfigCommand(
1301 const struct audio_port_config *config, int delayMs)
1302{
1303 sp<AudioCommand> command = new AudioCommand();
1304 command->mCommand = SET_AUDIOPORT_CONFIG;
1305 SetAudioPortConfigData *data = new SetAudioPortConfigData();
1306 data->mConfig = *config;
1307 command->mParam = data;
1308 command->mWaitStatus = true;
1309 ALOGV("AudioCommandThread() adding set port config delay %d", delayMs);
1310 return sendCommand(command, delayMs);
1311}
1312
Jean-Michel Trivide801052015-04-14 19:10:14 -07001313void AudioPolicyService::AudioCommandThread::dynamicPolicyMixStateUpdateCommand(
Chih-Hung Hsiehe964d4e2016-08-09 14:31:32 -07001314 const String8& regId, int32_t state)
Jean-Michel Trivide801052015-04-14 19:10:14 -07001315{
1316 sp<AudioCommand> command = new AudioCommand();
1317 command->mCommand = DYN_POLICY_MIX_STATE_UPDATE;
1318 DynPolicyMixStateUpdateData *data = new DynPolicyMixStateUpdateData();
1319 data->mRegId = regId;
1320 data->mState = state;
1321 command->mParam = data;
1322 ALOGV("AudioCommandThread() sending dynamic policy mix (id=%s) state update to %d",
1323 regId.string(), state);
1324 sendCommand(command);
1325}
1326
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -08001327void AudioPolicyService::AudioCommandThread::recordingConfigurationUpdateCommand(
Eric Laurenta9f86652018-11-28 17:23:11 -08001328 int event,
1329 const record_client_info_t *clientInfo,
1330 const audio_config_base_t *clientConfig,
1331 std::vector<effect_descriptor_t> clientEffects,
1332 const audio_config_base_t *deviceConfig,
1333 std::vector<effect_descriptor_t> effects,
1334 audio_patch_handle_t patchHandle,
1335 audio_source_t source)
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -08001336{
1337 sp<AudioCommand>command = new AudioCommand();
1338 command->mCommand = RECORDING_CONFIGURATION_UPDATE;
1339 RecordingConfigurationUpdateData *data = new RecordingConfigurationUpdateData();
1340 data->mEvent = event;
Jean-Michel Triviac4e4292016-12-22 11:39:31 -08001341 data->mClientInfo = *clientInfo;
Jean-Michel Trivi7281aa92016-02-17 15:33:40 -08001342 data->mClientConfig = *clientConfig;
Eric Laurenta9f86652018-11-28 17:23:11 -08001343 data->mClientEffects = clientEffects;
Jean-Michel Trivi7281aa92016-02-17 15:33:40 -08001344 data->mDeviceConfig = *deviceConfig;
Eric Laurenta9f86652018-11-28 17:23:11 -08001345 data->mEffects = effects;
Jean-Michel Trivi8c7cf3b2016-02-25 17:08:24 -08001346 data->mPatchHandle = patchHandle;
Eric Laurenta9f86652018-11-28 17:23:11 -08001347 data->mSource = source;
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -08001348 command->mParam = data;
Jean-Michel Triviac4e4292016-12-22 11:39:31 -08001349 ALOGV("AudioCommandThread() adding recording configuration update event %d, source %d uid %u",
1350 event, clientInfo->source, clientInfo->uid);
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -08001351 sendCommand(command);
1352}
1353
Eric Laurent0ede8922014-05-09 18:04:42 -07001354status_t AudioPolicyService::AudioCommandThread::sendCommand(sp<AudioCommand>& command, int delayMs)
1355{
1356 {
1357 Mutex::Autolock _l(mLock);
1358 insertCommand_l(command, delayMs);
1359 mWaitWorkCV.signal();
1360 }
1361 Mutex::Autolock _l(command->mLock);
1362 while (command->mWaitStatus) {
1363 nsecs_t timeOutNs = kAudioCommandTimeoutNs + milliseconds(delayMs);
1364 if (command->mCond.waitRelative(command->mLock, timeOutNs) != NO_ERROR) {
1365 command->mStatus = TIMED_OUT;
1366 command->mWaitStatus = false;
1367 }
1368 }
1369 return command->mStatus;
Eric Laurentbfb1b832013-01-07 09:53:42 -08001370}
1371
Mathias Agopian65ab4712010-07-14 17:59:35 -07001372// insertCommand_l() must be called with mLock held
Eric Laurent0ede8922014-05-09 18:04:42 -07001373void AudioPolicyService::AudioCommandThread::insertCommand_l(sp<AudioCommand>& command, int delayMs)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001374{
Glenn Kasten8d6a2442012-02-08 14:04:28 -08001375 ssize_t i; // not size_t because i will count down to -1
Eric Laurent0ede8922014-05-09 18:04:42 -07001376 Vector < sp<AudioCommand> > removedCommands;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001377 command->mTime = systemTime() + milliseconds(delayMs);
1378
1379 // acquire wake lock to make sure delayed commands are processed
Eric Laurentbfb1b832013-01-07 09:53:42 -08001380 if (mAudioCommands.isEmpty()) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07001381 acquire_wake_lock(PARTIAL_WAKE_LOCK, mName.string());
1382 }
1383
1384 // check same pending commands with later time stamps and eliminate them
Ivan Lozano5ff158f2017-10-30 09:06:24 -07001385 for (i = (ssize_t)mAudioCommands.size()-1; i >= 0; i--) {
Eric Laurent0ede8922014-05-09 18:04:42 -07001386 sp<AudioCommand> command2 = mAudioCommands[i];
Mathias Agopian65ab4712010-07-14 17:59:35 -07001387 // commands are sorted by increasing time stamp: no need to scan the rest of mAudioCommands
1388 if (command2->mTime <= command->mTime) break;
Eric Laurente45b48a2014-09-04 16:40:57 -07001389
1390 // create audio patch or release audio patch commands are equivalent
1391 // with regard to filtering
1392 if ((command->mCommand == CREATE_AUDIO_PATCH) ||
1393 (command->mCommand == RELEASE_AUDIO_PATCH)) {
1394 if ((command2->mCommand != CREATE_AUDIO_PATCH) &&
1395 (command2->mCommand != RELEASE_AUDIO_PATCH)) {
1396 continue;
1397 }
1398 } else if (command2->mCommand != command->mCommand) continue;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001399
1400 switch (command->mCommand) {
1401 case SET_PARAMETERS: {
Eric Laurent0ede8922014-05-09 18:04:42 -07001402 ParametersData *data = (ParametersData *)command->mParam.get();
1403 ParametersData *data2 = (ParametersData *)command2->mParam.get();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001404 if (data->mIO != data2->mIO) break;
Steve Block3856b092011-10-20 11:56:00 +01001405 ALOGV("Comparing parameter command %s to new command %s",
Eric Laurentde070132010-07-13 04:45:46 -07001406 data2->mKeyValuePairs.string(), data->mKeyValuePairs.string());
Mathias Agopian65ab4712010-07-14 17:59:35 -07001407 AudioParameter param = AudioParameter(data->mKeyValuePairs);
1408 AudioParameter param2 = AudioParameter(data2->mKeyValuePairs);
1409 for (size_t j = 0; j < param.size(); j++) {
Glenn Kastene53b9ea2012-03-12 16:29:55 -07001410 String8 key;
1411 String8 value;
1412 param.getAt(j, key, value);
1413 for (size_t k = 0; k < param2.size(); k++) {
1414 String8 key2;
1415 String8 value2;
1416 param2.getAt(k, key2, value2);
1417 if (key2 == key) {
1418 param2.remove(key2);
1419 ALOGV("Filtering out parameter %s", key2.string());
1420 break;
1421 }
1422 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001423 }
1424 // if all keys have been filtered out, remove the command.
1425 // otherwise, update the key value pairs
1426 if (param2.size() == 0) {
1427 removedCommands.add(command2);
1428 } else {
1429 data2->mKeyValuePairs = param2.toString();
1430 }
Eric Laurent21e54562013-09-23 12:08:05 -07001431 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;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001435 } break;
1436
1437 case SET_VOLUME: {
Eric Laurent0ede8922014-05-09 18:04:42 -07001438 VolumeData *data = (VolumeData *)command->mParam.get();
1439 VolumeData *data2 = (VolumeData *)command2->mParam.get();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001440 if (data->mIO != data2->mIO) break;
1441 if (data->mStream != data2->mStream) break;
Steve Block3856b092011-10-20 11:56:00 +01001442 ALOGV("Filtering out volume command on output %d for stream %d",
Eric Laurentde070132010-07-13 04:45:46 -07001443 data->mIO, data->mStream);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001444 removedCommands.add(command2);
Eric Laurent21e54562013-09-23 12:08:05 -07001445 command->mTime = command2->mTime;
1446 // force delayMs to non 0 so that code below does not request to wait for
1447 // command status as the command is now delayed
1448 delayMs = 1;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001449 } break;
Eric Laurente45b48a2014-09-04 16:40:57 -07001450
Eric Laurentbaf35fe2016-07-27 15:36:53 -07001451 case SET_VOICE_VOLUME: {
1452 VoiceVolumeData *data = (VoiceVolumeData *)command->mParam.get();
1453 VoiceVolumeData *data2 = (VoiceVolumeData *)command2->mParam.get();
1454 ALOGV("Filtering out voice volume command value %f replaced by %f",
1455 data2->mVolume, data->mVolume);
1456 removedCommands.add(command2);
1457 command->mTime = command2->mTime;
1458 // force delayMs to non 0 so that code below does not request to wait for
1459 // command status as the command is now delayed
1460 delayMs = 1;
1461 } break;
1462
Eric Laurente45b48a2014-09-04 16:40:57 -07001463 case CREATE_AUDIO_PATCH:
1464 case RELEASE_AUDIO_PATCH: {
1465 audio_patch_handle_t handle;
Haynes Mathew Georgea2d4a6d2014-10-13 13:05:22 -07001466 struct audio_patch patch;
Eric Laurente45b48a2014-09-04 16:40:57 -07001467 if (command->mCommand == CREATE_AUDIO_PATCH) {
1468 handle = ((CreateAudioPatchData *)command->mParam.get())->mHandle;
Haynes Mathew Georgea2d4a6d2014-10-13 13:05:22 -07001469 patch = ((CreateAudioPatchData *)command->mParam.get())->mPatch;
Eric Laurente45b48a2014-09-04 16:40:57 -07001470 } else {
1471 handle = ((ReleaseAudioPatchData *)command->mParam.get())->mHandle;
Mikhail Naganov7be71d22018-05-23 16:51:46 -07001472 memset(&patch, 0, sizeof(patch));
Eric Laurente45b48a2014-09-04 16:40:57 -07001473 }
1474 audio_patch_handle_t handle2;
Haynes Mathew Georgea2d4a6d2014-10-13 13:05:22 -07001475 struct audio_patch patch2;
Eric Laurente45b48a2014-09-04 16:40:57 -07001476 if (command2->mCommand == CREATE_AUDIO_PATCH) {
1477 handle2 = ((CreateAudioPatchData *)command2->mParam.get())->mHandle;
Haynes Mathew Georgea2d4a6d2014-10-13 13:05:22 -07001478 patch2 = ((CreateAudioPatchData *)command2->mParam.get())->mPatch;
Eric Laurente45b48a2014-09-04 16:40:57 -07001479 } else {
1480 handle2 = ((ReleaseAudioPatchData *)command2->mParam.get())->mHandle;
Glenn Kastenf60b6b62015-07-06 10:53:26 -07001481 memset(&patch2, 0, sizeof(patch2));
Eric Laurente45b48a2014-09-04 16:40:57 -07001482 }
1483 if (handle != handle2) break;
Haynes Mathew Georgea2d4a6d2014-10-13 13:05:22 -07001484 /* Filter CREATE_AUDIO_PATCH commands only when they are issued for
1485 same output. */
1486 if( (command->mCommand == CREATE_AUDIO_PATCH) &&
1487 (command2->mCommand == CREATE_AUDIO_PATCH) ) {
1488 bool isOutputDiff = false;
1489 if (patch.num_sources == patch2.num_sources) {
1490 for (unsigned count = 0; count < patch.num_sources; count++) {
1491 if (patch.sources[count].id != patch2.sources[count].id) {
1492 isOutputDiff = true;
1493 break;
1494 }
1495 }
1496 if (isOutputDiff)
1497 break;
1498 }
1499 }
Eric Laurente45b48a2014-09-04 16:40:57 -07001500 ALOGV("Filtering out %s audio patch command for handle %d",
1501 (command->mCommand == CREATE_AUDIO_PATCH) ? "create" : "release", handle);
1502 removedCommands.add(command2);
1503 command->mTime = command2->mTime;
1504 // force delayMs to non 0 so that code below does not request to wait for
1505 // command status as the command is now delayed
1506 delayMs = 1;
1507 } break;
1508
Jean-Michel Trivide801052015-04-14 19:10:14 -07001509 case DYN_POLICY_MIX_STATE_UPDATE: {
1510
1511 } break;
1512
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -08001513 case RECORDING_CONFIGURATION_UPDATE: {
1514
1515 } break;
1516
Mathias Agopian65ab4712010-07-14 17:59:35 -07001517 default:
1518 break;
1519 }
1520 }
1521
1522 // remove filtered commands
1523 for (size_t j = 0; j < removedCommands.size(); j++) {
1524 // removed commands always have time stamps greater than current command
1525 for (size_t k = i + 1; k < mAudioCommands.size(); k++) {
Eric Laurent0ede8922014-05-09 18:04:42 -07001526 if (mAudioCommands[k].get() == removedCommands[j].get()) {
Steve Block3856b092011-10-20 11:56:00 +01001527 ALOGV("suppressing command: %d", mAudioCommands[k]->mCommand);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001528 mAudioCommands.removeAt(k);
1529 break;
1530 }
1531 }
1532 }
1533 removedCommands.clear();
1534
Eric Laurentaa79bef2015-01-15 14:33:51 -08001535 // Disable wait for status if delay is not 0.
1536 // Except for create audio patch command because the returned patch handle
1537 // is needed by audio policy manager
1538 if (delayMs != 0 && command->mCommand != CREATE_AUDIO_PATCH) {
Eric Laurentcec4abb2012-07-03 12:23:02 -07001539 command->mWaitStatus = false;
1540 }
Eric Laurentcec4abb2012-07-03 12:23:02 -07001541
Mathias Agopian65ab4712010-07-14 17:59:35 -07001542 // insert command at the right place according to its time stamp
Eric Laurent1e693b52014-07-09 15:03:28 -07001543 ALOGV("inserting command: %d at index %zd, num commands %zu",
1544 command->mCommand, i+1, mAudioCommands.size());
Mathias Agopian65ab4712010-07-14 17:59:35 -07001545 mAudioCommands.insertAt(command, i + 1);
1546}
1547
1548void AudioPolicyService::AudioCommandThread::exit()
1549{
Steve Block3856b092011-10-20 11:56:00 +01001550 ALOGV("AudioCommandThread::exit");
Mathias Agopian65ab4712010-07-14 17:59:35 -07001551 {
1552 AutoMutex _l(mLock);
1553 requestExit();
1554 mWaitWorkCV.signal();
1555 }
Zach Janga754b4f2015-10-27 01:29:34 +00001556 // Note that we can call it from the thread loop if all other references have been released
1557 // but it will safely return WOULD_BLOCK in this case
Mathias Agopian65ab4712010-07-14 17:59:35 -07001558 requestExitAndWait();
1559}
1560
1561void AudioPolicyService::AudioCommandThread::AudioCommand::dump(char* buffer, size_t size)
1562{
1563 snprintf(buffer, size, " %02d %06d.%03d %01u %p\n",
1564 mCommand,
1565 (int)ns2s(mTime),
1566 (int)ns2ms(mTime)%1000,
1567 mWaitStatus,
Eric Laurent0ede8922014-05-09 18:04:42 -07001568 mParam.get());
Mathias Agopian65ab4712010-07-14 17:59:35 -07001569}
1570
Dima Zavinfce7a472011-04-19 22:30:36 -07001571/******* helpers for the service_ops callbacks defined below *********/
1572void AudioPolicyService::setParameters(audio_io_handle_t ioHandle,
1573 const char *keyValuePairs,
1574 int delayMs)
1575{
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001576 mAudioCommandThread->parametersCommand(ioHandle, keyValuePairs,
Dima Zavinfce7a472011-04-19 22:30:36 -07001577 delayMs);
1578}
1579
1580int AudioPolicyService::setStreamVolume(audio_stream_type_t stream,
1581 float volume,
1582 audio_io_handle_t output,
1583 int delayMs)
1584{
Glenn Kastenfff6d712012-01-12 16:38:12 -08001585 return (int)mAudioCommandThread->volumeCommand(stream, volume,
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001586 output, delayMs);
Dima Zavinfce7a472011-04-19 22:30:36 -07001587}
1588
Dima Zavinfce7a472011-04-19 22:30:36 -07001589int AudioPolicyService::setVoiceVolume(float volume, int delayMs)
1590{
1591 return (int)mAudioCommandThread->voiceVolumeCommand(volume, delayMs);
1592}
1593
Dima Zavinfce7a472011-04-19 22:30:36 -07001594extern "C" {
Eric Laurent2d388ec2014-03-07 13:25:54 -08001595audio_module_handle_t aps_load_hw_module(void *service __unused,
1596 const char *name);
1597audio_io_handle_t aps_open_output(void *service __unused,
Eric Laurenta4c5a552012-03-29 10:12:40 -07001598 audio_devices_t *pDevices,
1599 uint32_t *pSamplingRate,
1600 audio_format_t *pFormat,
1601 audio_channel_mask_t *pChannelMask,
1602 uint32_t *pLatencyMs,
Eric Laurent2d388ec2014-03-07 13:25:54 -08001603 audio_output_flags_t flags);
Eric Laurenta4c5a552012-03-29 10:12:40 -07001604
Eric Laurent2d388ec2014-03-07 13:25:54 -08001605audio_io_handle_t aps_open_output_on_module(void *service __unused,
Eric Laurenta4c5a552012-03-29 10:12:40 -07001606 audio_module_handle_t module,
1607 audio_devices_t *pDevices,
1608 uint32_t *pSamplingRate,
1609 audio_format_t *pFormat,
1610 audio_channel_mask_t *pChannelMask,
1611 uint32_t *pLatencyMs,
Richard Fitzgeraldad3af332013-03-25 16:54:37 +00001612 audio_output_flags_t flags,
Eric Laurent2d388ec2014-03-07 13:25:54 -08001613 const audio_offload_info_t *offloadInfo);
1614audio_io_handle_t aps_open_dup_output(void *service __unused,
Dima Zavinfce7a472011-04-19 22:30:36 -07001615 audio_io_handle_t output1,
Eric Laurent2d388ec2014-03-07 13:25:54 -08001616 audio_io_handle_t output2);
1617int aps_close_output(void *service __unused, audio_io_handle_t output);
1618int aps_suspend_output(void *service __unused, audio_io_handle_t output);
1619int aps_restore_output(void *service __unused, audio_io_handle_t output);
1620audio_io_handle_t aps_open_input(void *service __unused,
Eric Laurenta4c5a552012-03-29 10:12:40 -07001621 audio_devices_t *pDevices,
1622 uint32_t *pSamplingRate,
1623 audio_format_t *pFormat,
1624 audio_channel_mask_t *pChannelMask,
Eric Laurent2d388ec2014-03-07 13:25:54 -08001625 audio_in_acoustics_t acoustics __unused);
1626audio_io_handle_t aps_open_input_on_module(void *service __unused,
Eric Laurenta4c5a552012-03-29 10:12:40 -07001627 audio_module_handle_t module,
1628 audio_devices_t *pDevices,
1629 uint32_t *pSamplingRate,
1630 audio_format_t *pFormat,
Eric Laurent2d388ec2014-03-07 13:25:54 -08001631 audio_channel_mask_t *pChannelMask);
1632int aps_close_input(void *service __unused, audio_io_handle_t input);
1633int aps_invalidate_stream(void *service __unused, audio_stream_type_t stream);
Glenn Kastend848eb42016-03-08 13:42:11 -08001634int aps_move_effects(void *service __unused, audio_session_t session,
Dima Zavinfce7a472011-04-19 22:30:36 -07001635 audio_io_handle_t src_output,
Eric Laurent2d388ec2014-03-07 13:25:54 -08001636 audio_io_handle_t dst_output);
1637char * aps_get_parameters(void *service __unused, audio_io_handle_t io_handle,
1638 const char *keys);
1639void aps_set_parameters(void *service, audio_io_handle_t io_handle,
1640 const char *kv_pairs, int delay_ms);
1641int aps_set_stream_volume(void *service, audio_stream_type_t stream,
Dima Zavinfce7a472011-04-19 22:30:36 -07001642 float volume, audio_io_handle_t output,
Eric Laurent2d388ec2014-03-07 13:25:54 -08001643 int delay_ms);
Eric Laurent2d388ec2014-03-07 13:25:54 -08001644int aps_set_voice_volume(void *service, float volume, int delay_ms);
1645};
Dima Zavinfce7a472011-04-19 22:30:36 -07001646
Mikhail Naganov1b2a7942017-12-08 10:18:09 -08001647} // namespace android