blob: ee5d6ffee9da87033a622ee81d74ed9cab158a3f [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>
Mathias Agopian65ab4712010-07-14 17:59:35 -070041
Dima Zavin64760242011-05-11 14:15:23 -070042#include <system/audio.h>
Dima Zavin7394a4f2011-06-13 18:16:26 -070043#include <system/audio_policy.h>
Mikhail Naganov61a4fac2016-10-13 14:44:18 -070044
Mathias Agopian65ab4712010-07-14 17:59:35 -070045namespace android {
46
Glenn Kasten8dad0e32012-01-09 08:41:22 -080047static const char kDeadlockedString[] = "AudioPolicyService may be deadlocked\n";
48static const char kCmdDeadlockedString[] = "AudioPolicyService command thread may be deadlocked\n";
Mathias Agopian65ab4712010-07-14 17:59:35 -070049
50static const int kDumpLockRetries = 50;
Glenn Kasten22ecc912012-01-09 08:33:38 -080051static const int kDumpLockSleepUs = 20000;
Mathias Agopian65ab4712010-07-14 17:59:35 -070052
Eric Laurent0ede8922014-05-09 18:04:42 -070053static const nsecs_t kAudioCommandTimeoutNs = seconds(3); // 3 seconds
Christer Fletcher5fa8c4b2013-01-18 15:27:03 +010054
Svet Ganovf4ddfef2018-01-16 07:37:58 -080055static const String16 sManageAudioPolicyPermission("android.permission.MANAGE_AUDIO_POLICY");
Dima Zavinfce7a472011-04-19 22:30:36 -070056
Mathias Agopian65ab4712010-07-14 17:59:35 -070057// ----------------------------------------------------------------------------
58
59AudioPolicyService::AudioPolicyService()
Eric Laurentdce54a12014-03-10 12:19:46 -070060 : BnAudioPolicyService(), mpAudioPolicyDev(NULL), mpAudioPolicy(NULL),
Eric Laurentbb6c9a02014-09-25 14:11:47 -070061 mAudioPolicyManager(NULL), mAudioPolicyClient(NULL), mPhoneState(AUDIO_MODE_INVALID)
Mathias Agopian65ab4712010-07-14 17:59:35 -070062{
Eric Laurentf5ada6e2014-10-09 17:49:00 -070063}
64
65void AudioPolicyService::onFirstRef()
66{
Eric Laurent8b1e80b2014-10-07 09:08:47 -070067 {
68 Mutex::Autolock _l(mLock);
Eric Laurent93575202011-01-18 18:39:02 -080069
Eric Laurent8b1e80b2014-10-07 09:08:47 -070070 // start audio commands thread
71 mAudioCommandThread = new AudioCommandThread(String8("ApmAudio"), this);
72 // start output activity command thread
73 mOutputCommandThread = new AudioCommandThread(String8("ApmOutput"), this);
Eric Laurentdce54a12014-03-10 12:19:46 -070074
Eric Laurent8b1e80b2014-10-07 09:08:47 -070075 mAudioPolicyClient = new AudioPolicyClient(this);
76 mAudioPolicyManager = createAudioPolicyManager(mAudioPolicyClient);
Eric Laurent8b1e80b2014-10-07 09:08:47 -070077 }
bryant_liuba2b4392014-06-11 16:49:30 +080078 // load audio processing modules
Eric Laurent8b1e80b2014-10-07 09:08:47 -070079 sp<AudioPolicyEffects>audioPolicyEffects = new AudioPolicyEffects();
80 {
81 Mutex::Autolock _l(mLock);
82 mAudioPolicyEffects = audioPolicyEffects;
83 }
Svet Ganovf4ddfef2018-01-16 07:37:58 -080084
85 mUidPolicy = new UidPolicy(this);
86 mUidPolicy->registerSelf();
Mathias Agopian65ab4712010-07-14 17:59:35 -070087}
88
89AudioPolicyService::~AudioPolicyService()
90{
Mathias Agopian65ab4712010-07-14 17:59:35 -070091 mAudioCommandThread->exit();
Eric Laurent657ff612014-05-07 11:58:24 -070092 mOutputCommandThread->exit();
Eric Laurent7c7f10b2011-06-17 21:29:58 -070093
Eric Laurentf269b8e2014-06-09 20:01:29 -070094 destroyAudioPolicyManager(mAudioPolicyManager);
Eric Laurentdce54a12014-03-10 12:19:46 -070095 delete mAudioPolicyClient;
Eric Laurentb52c1522014-05-20 11:27:36 -070096
97 mNotificationClients.clear();
bryant_liuba2b4392014-06-11 16:49:30 +080098 mAudioPolicyEffects.clear();
Svet Ganovf4ddfef2018-01-16 07:37:58 -080099
100 mUidPolicy->unregisterSelf();
101 mUidPolicy.clear();
Eric Laurentb52c1522014-05-20 11:27:36 -0700102}
103
104// A notification client is always registered by AudioSystem when the client process
105// connects to AudioPolicyService.
106void AudioPolicyService::registerClient(const sp<IAudioPolicyServiceClient>& client)
107{
Eric Laurent12590252015-08-21 18:40:20 -0700108 if (client == 0) {
109 ALOGW("%s got NULL client", __FUNCTION__);
110 return;
111 }
Eric Laurent0ebd5f92014-11-19 19:04:52 -0800112 Mutex::Autolock _l(mNotificationClientsLock);
Eric Laurentb52c1522014-05-20 11:27:36 -0700113
114 uid_t uid = IPCThreadState::self()->getCallingUid();
luochaojiang908c7d72018-06-21 14:58:04 +0800115 pid_t pid = IPCThreadState::self()->getCallingPid();
116 int64_t token = ((int64_t)uid<<32) | pid;
117
118 if (mNotificationClients.indexOfKey(token) < 0) {
Eric Laurentb52c1522014-05-20 11:27:36 -0700119 sp<NotificationClient> notificationClient = new NotificationClient(this,
120 client,
luochaojiang908c7d72018-06-21 14:58:04 +0800121 uid,
122 pid);
123 ALOGV("registerClient() client %p, uid %d pid %d", client.get(), uid, pid);
Eric Laurentb52c1522014-05-20 11:27:36 -0700124
luochaojiang908c7d72018-06-21 14:58:04 +0800125 mNotificationClients.add(token, notificationClient);
Eric Laurentb52c1522014-05-20 11:27:36 -0700126
Marco Nelissenf8880202014-11-14 07:58:25 -0800127 sp<IBinder> binder = IInterface::asBinder(client);
Eric Laurentb52c1522014-05-20 11:27:36 -0700128 binder->linkToDeath(notificationClient);
129 }
130}
131
Eric Laurente8726fe2015-06-26 09:39:24 -0700132void AudioPolicyService::setAudioPortCallbacksEnabled(bool enabled)
133{
134 Mutex::Autolock _l(mNotificationClientsLock);
135
136 uid_t uid = IPCThreadState::self()->getCallingUid();
luochaojiang908c7d72018-06-21 14:58:04 +0800137 pid_t pid = IPCThreadState::self()->getCallingPid();
138 int64_t token = ((int64_t)uid<<32) | pid;
139
140 if (mNotificationClients.indexOfKey(token) < 0) {
Eric Laurente8726fe2015-06-26 09:39:24 -0700141 return;
142 }
luochaojiang908c7d72018-06-21 14:58:04 +0800143 mNotificationClients.valueFor(token)->setAudioPortCallbacksEnabled(enabled);
Eric Laurente8726fe2015-06-26 09:39:24 -0700144}
145
Eric Laurentb52c1522014-05-20 11:27:36 -0700146// removeNotificationClient() is called when the client process dies.
luochaojiang908c7d72018-06-21 14:58:04 +0800147void AudioPolicyService::removeNotificationClient(uid_t uid, pid_t pid)
Eric Laurentb52c1522014-05-20 11:27:36 -0700148{
Eric Laurent0ebd5f92014-11-19 19:04:52 -0800149 {
150 Mutex::Autolock _l(mNotificationClientsLock);
luochaojiang908c7d72018-06-21 14:58:04 +0800151 int64_t token = ((int64_t)uid<<32) | pid;
152 mNotificationClients.removeItem(token);
Eric Laurent0ebd5f92014-11-19 19:04:52 -0800153 }
Eric Laurent0ebd5f92014-11-19 19:04:52 -0800154 {
155 Mutex::Autolock _l(mLock);
luochaojiang908c7d72018-06-21 14:58:04 +0800156 bool hasSameUid = false;
157 for (size_t i = 0; i < mNotificationClients.size(); i++) {
158 if (mNotificationClients.valueAt(i)->uid() == uid) {
159 hasSameUid = true;
160 break;
161 }
162 }
163 if (mAudioPolicyManager && !hasSameUid) {
Eric Laurent10b71232018-04-13 18:14:44 -0700164 // called from binder death notification: no need to clear caller identity
Eric Laurent8c7e6da2015-04-21 17:37:00 -0700165 mAudioPolicyManager->releaseResourcesForUid(uid);
Eric Laurentb52c1522014-05-20 11:27:36 -0700166 }
Eric Laurent0ebd5f92014-11-19 19:04:52 -0800167 }
Eric Laurentb52c1522014-05-20 11:27:36 -0700168}
169
170void AudioPolicyService::onAudioPortListUpdate()
171{
172 mOutputCommandThread->updateAudioPortListCommand();
173}
174
175void AudioPolicyService::doOnAudioPortListUpdate()
176{
Eric Laurent0ebd5f92014-11-19 19:04:52 -0800177 Mutex::Autolock _l(mNotificationClientsLock);
Eric Laurentb52c1522014-05-20 11:27:36 -0700178 for (size_t i = 0; i < mNotificationClients.size(); i++) {
179 mNotificationClients.valueAt(i)->onAudioPortListUpdate();
180 }
181}
182
183void AudioPolicyService::onAudioPatchListUpdate()
184{
185 mOutputCommandThread->updateAudioPatchListCommand();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700186}
187
Eric Laurentb52c1522014-05-20 11:27:36 -0700188void AudioPolicyService::doOnAudioPatchListUpdate()
189{
Eric Laurent0ebd5f92014-11-19 19:04:52 -0800190 Mutex::Autolock _l(mNotificationClientsLock);
Eric Laurentb52c1522014-05-20 11:27:36 -0700191 for (size_t i = 0; i < mNotificationClients.size(); i++) {
192 mNotificationClients.valueAt(i)->onAudioPatchListUpdate();
193 }
194}
195
Chih-Hung Hsiehe964d4e2016-08-09 14:31:32 -0700196void AudioPolicyService::onDynamicPolicyMixStateUpdate(const String8& regId, int32_t state)
Jean-Michel Trivide801052015-04-14 19:10:14 -0700197{
198 ALOGV("AudioPolicyService::onDynamicPolicyMixStateUpdate(%s, %d)",
199 regId.string(), state);
200 mOutputCommandThread->dynamicPolicyMixStateUpdateCommand(regId, state);
201}
202
Chih-Hung Hsiehe964d4e2016-08-09 14:31:32 -0700203void AudioPolicyService::doOnDynamicPolicyMixStateUpdate(const String8& regId, int32_t state)
Jean-Michel Trivide801052015-04-14 19:10:14 -0700204{
205 Mutex::Autolock _l(mNotificationClientsLock);
206 for (size_t i = 0; i < mNotificationClients.size(); i++) {
207 mNotificationClients.valueAt(i)->onDynamicPolicyMixStateUpdate(regId, state);
208 }
209}
210
Jean-Michel Triviac4e4292016-12-22 11:39:31 -0800211void AudioPolicyService::onRecordingConfigurationUpdate(int event,
212 const record_client_info_t *clientInfo, const audio_config_base_t *clientConfig,
Jean-Michel Trivi8c7cf3b2016-02-25 17:08:24 -0800213 const audio_config_base_t *deviceConfig, audio_patch_handle_t patchHandle)
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -0800214{
Jean-Michel Triviac4e4292016-12-22 11:39:31 -0800215 mOutputCommandThread->recordingConfigurationUpdateCommand(event, clientInfo,
Jean-Michel Trivi8c7cf3b2016-02-25 17:08:24 -0800216 clientConfig, deviceConfig, patchHandle);
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -0800217}
218
Jean-Michel Triviac4e4292016-12-22 11:39:31 -0800219void AudioPolicyService::doOnRecordingConfigurationUpdate(int event,
220 const record_client_info_t *clientInfo, const audio_config_base_t *clientConfig,
Jean-Michel Trivi8c7cf3b2016-02-25 17:08:24 -0800221 const audio_config_base_t *deviceConfig, audio_patch_handle_t patchHandle)
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -0800222{
223 Mutex::Autolock _l(mNotificationClientsLock);
224 for (size_t i = 0; i < mNotificationClients.size(); i++) {
Jean-Michel Triviac4e4292016-12-22 11:39:31 -0800225 mNotificationClients.valueAt(i)->onRecordingConfigurationUpdate(event, clientInfo,
Jean-Michel Trivi8c7cf3b2016-02-25 17:08:24 -0800226 clientConfig, deviceConfig, patchHandle);
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -0800227 }
228}
229
230status_t AudioPolicyService::clientCreateAudioPatch(const struct audio_patch *patch,
231 audio_patch_handle_t *handle,
232 int delayMs)
233{
234 return mAudioCommandThread->createAudioPatchCommand(patch, handle, delayMs);
235}
236
237status_t AudioPolicyService::clientReleaseAudioPatch(audio_patch_handle_t handle,
238 int delayMs)
239{
240 return mAudioCommandThread->releaseAudioPatchCommand(handle, delayMs);
241}
242
Eric Laurente1715a42014-05-20 11:30:42 -0700243status_t AudioPolicyService::clientSetAudioPortConfig(const struct audio_port_config *config,
244 int delayMs)
245{
246 return mAudioCommandThread->setAudioPortConfigCommand(config, delayMs);
247}
248
Eric Laurentb52c1522014-05-20 11:27:36 -0700249AudioPolicyService::NotificationClient::NotificationClient(const sp<AudioPolicyService>& service,
250 const sp<IAudioPolicyServiceClient>& client,
luochaojiang908c7d72018-06-21 14:58:04 +0800251 uid_t uid,
252 pid_t pid)
253 : mService(service), mUid(uid), mPid(pid), mAudioPolicyServiceClient(client),
Eric Laurente8726fe2015-06-26 09:39:24 -0700254 mAudioPortCallbacksEnabled(false)
Eric Laurentb52c1522014-05-20 11:27:36 -0700255{
256}
257
258AudioPolicyService::NotificationClient::~NotificationClient()
259{
260}
261
262void AudioPolicyService::NotificationClient::binderDied(const wp<IBinder>& who __unused)
263{
264 sp<NotificationClient> keep(this);
265 sp<AudioPolicyService> service = mService.promote();
266 if (service != 0) {
luochaojiang908c7d72018-06-21 14:58:04 +0800267 service->removeNotificationClient(mUid, mPid);
Eric Laurentb52c1522014-05-20 11:27:36 -0700268 }
269}
270
271void AudioPolicyService::NotificationClient::onAudioPortListUpdate()
272{
Eric Laurente8726fe2015-06-26 09:39:24 -0700273 if (mAudioPolicyServiceClient != 0 && mAudioPortCallbacksEnabled) {
Eric Laurentb52c1522014-05-20 11:27:36 -0700274 mAudioPolicyServiceClient->onAudioPortListUpdate();
275 }
276}
277
278void AudioPolicyService::NotificationClient::onAudioPatchListUpdate()
279{
Eric Laurente8726fe2015-06-26 09:39:24 -0700280 if (mAudioPolicyServiceClient != 0 && mAudioPortCallbacksEnabled) {
Eric Laurentb52c1522014-05-20 11:27:36 -0700281 mAudioPolicyServiceClient->onAudioPatchListUpdate();
282 }
283}
Eric Laurent57dae992011-07-24 13:36:09 -0700284
Jean-Michel Trivide801052015-04-14 19:10:14 -0700285void AudioPolicyService::NotificationClient::onDynamicPolicyMixStateUpdate(
Chih-Hung Hsiehe964d4e2016-08-09 14:31:32 -0700286 const String8& regId, int32_t state)
Jean-Michel Trivide801052015-04-14 19:10:14 -0700287{
Andy Hung4ef19fa2018-05-15 19:35:29 -0700288 if (mAudioPolicyServiceClient != 0 && isServiceUid(mUid)) {
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -0800289 mAudioPolicyServiceClient->onDynamicPolicyMixStateUpdate(regId, state);
290 }
291}
292
293void AudioPolicyService::NotificationClient::onRecordingConfigurationUpdate(
Jean-Michel Triviac4e4292016-12-22 11:39:31 -0800294 int event, const record_client_info_t *clientInfo,
Jean-Michel Trivi8c7cf3b2016-02-25 17:08:24 -0800295 const audio_config_base_t *clientConfig, const audio_config_base_t *deviceConfig,
296 audio_patch_handle_t patchHandle)
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -0800297{
Andy Hung4ef19fa2018-05-15 19:35:29 -0700298 if (mAudioPolicyServiceClient != 0 && isServiceUid(mUid)) {
Jean-Michel Triviac4e4292016-12-22 11:39:31 -0800299 mAudioPolicyServiceClient->onRecordingConfigurationUpdate(event, clientInfo,
Jean-Michel Trivi8c7cf3b2016-02-25 17:08:24 -0800300 clientConfig, deviceConfig, patchHandle);
Jean-Michel Trivide801052015-04-14 19:10:14 -0700301 }
302}
303
Eric Laurente8726fe2015-06-26 09:39:24 -0700304void AudioPolicyService::NotificationClient::setAudioPortCallbacksEnabled(bool enabled)
305{
306 mAudioPortCallbacksEnabled = enabled;
307}
308
309
Mathias Agopian65ab4712010-07-14 17:59:35 -0700310void AudioPolicyService::binderDied(const wp<IBinder>& who) {
Glenn Kasten411e4472012-11-02 10:00:06 -0700311 ALOGW("binderDied() %p, calling pid %d", who.unsafe_get(),
Eric Laurentde070132010-07-13 04:45:46 -0700312 IPCThreadState::self()->getCallingPid());
Mathias Agopian65ab4712010-07-14 17:59:35 -0700313}
314
315static bool tryLock(Mutex& mutex)
316{
317 bool locked = false;
318 for (int i = 0; i < kDumpLockRetries; ++i) {
319 if (mutex.tryLock() == NO_ERROR) {
320 locked = true;
321 break;
322 }
Glenn Kasten22ecc912012-01-09 08:33:38 -0800323 usleep(kDumpLockSleepUs);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700324 }
325 return locked;
326}
327
328status_t AudioPolicyService::dumpInternals(int fd)
329{
330 const size_t SIZE = 256;
331 char buffer[SIZE];
332 String8 result;
333
Eric Laurentdce54a12014-03-10 12:19:46 -0700334 snprintf(buffer, SIZE, "AudioPolicyManager: %p\n", mAudioPolicyManager);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700335 result.append(buffer);
336 snprintf(buffer, SIZE, "Command Thread: %p\n", mAudioCommandThread.get());
337 result.append(buffer);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700338
339 write(fd, result.string(), result.size());
340 return NO_ERROR;
341}
342
Eric Laurente8c8b432018-10-17 10:08:02 -0700343void AudioPolicyService::updateUidStates()
Svet Ganovf4ddfef2018-01-16 07:37:58 -0800344{
Eric Laurente8c8b432018-10-17 10:08:02 -0700345 Mutex::Autolock _l(mLock);
346 updateUidStates_l();
347}
348
349void AudioPolicyService::updateUidStates_l()
350{
Eric Laurent4eb58f12018-12-07 16:41:02 -0800351// Go over all active clients and allow capture (does not force silence) in the
352// following cases:
Eric Laurenta46bedb2018-12-07 18:01:26 -0800353// The client is the assistant
354// AND an accessibility service is on TOP
355// AND the source is VOICE_RECOGNITION or HOTWORD
356// OR uses VOICE_RECOGNITION AND is on TOP OR latest started
357// OR uses HOTWORD
358// AND there is no privacy sensitive active capture
359// OR The client is an accessibility service
360// AND is on TOP OR latest started
361// AND the source is VOICE_RECOGNITION or HOTWORD
362// OR Any other client
363// AND The assistant is not on TOP
364// AND is on TOP OR latest started
365// AND there is no privacy sensitive active capture
Eric Laurent4eb58f12018-12-07 16:41:02 -0800366//TODO: mamanage pre processing effects according to use case priority
367
368 sp<AudioRecordClient> topActive;
369 sp<AudioRecordClient> latestActive;
Eric Laurent4eb58f12018-12-07 16:41:02 -0800370 sp<AudioRecordClient> latestSensitiveActive;
Eric Laurenta46bedb2018-12-07 18:01:26 -0800371 nsecs_t topStartNs = 0;
372 nsecs_t latestStartNs = 0;
Eric Laurent4eb58f12018-12-07 16:41:02 -0800373 nsecs_t latestSensitiveStartNs = 0;
374 bool isA11yOnTop = mUidPolicy->isA11yOnTop();
375 bool isAssistantOnTop = false;
376 bool isSensitiveActive = false;
377
Eric Laurente8c8b432018-10-17 10:08:02 -0700378 for (size_t i =0; i < mAudioRecordClients.size(); i++) {
379 sp<AudioRecordClient> current = mAudioRecordClients[i];
380 if (!current->active) continue;
Eric Laurent4eb58f12018-12-07 16:41:02 -0800381 if (isPrivacySensitive(current->attributes.source)) {
382 if (current->startTimeNs > latestSensitiveStartNs) {
383 latestSensitiveActive = current;
384 latestSensitiveStartNs = current->startTimeNs;
385 }
386 isSensitiveActive = true;
387 }
388 if (mUidPolicy->getUidState(current->uid) == ActivityManager::PROCESS_STATE_TOP) {
Eric Laurenta46bedb2018-12-07 18:01:26 -0800389 if (current->startTimeNs > topStartNs) {
390 topActive = current;
391 topStartNs = current->startTimeNs;
392 }
Eric Laurent4eb58f12018-12-07 16:41:02 -0800393 if (mUidPolicy->isAssistantUid(current->uid)) {
394 isAssistantOnTop = true;
395 }
396 }
397 if (current->startTimeNs > latestStartNs) {
398 latestActive = current;
399 latestStartNs = current->startTimeNs;
400 }
401 }
402
403 if (topActive == nullptr && latestActive == nullptr) {
404 return;
405 }
406
Eric Laurenta46bedb2018-12-07 18:01:26 -0800407 if (topActive != nullptr) {
408 latestActive = nullptr;
409 }
410
Eric Laurent4eb58f12018-12-07 16:41:02 -0800411 for (size_t i =0; i < mAudioRecordClients.size(); i++) {
412 sp<AudioRecordClient> current = mAudioRecordClients[i];
413 if (!current->active) continue;
414
415 audio_source_t source = current->attributes.source;
Eric Laurenta46bedb2018-12-07 18:01:26 -0800416 bool isOnTop = current == topActive;
Eric Laurent4eb58f12018-12-07 16:41:02 -0800417 bool isLatest = current == latestActive;
418 bool isLatestSensitive = current == latestSensitiveActive;
419 bool forceIdle = true;
420 if (mUidPolicy->isAssistantUid(current->uid)) {
421 if (isA11yOnTop) {
422 if (source == AUDIO_SOURCE_HOTWORD || source == AUDIO_SOURCE_VOICE_RECOGNITION) {
423 forceIdle = false;
424 }
425 } else {
Eric Laurenta46bedb2018-12-07 18:01:26 -0800426 if ((((isOnTop || isLatest) && source == AUDIO_SOURCE_VOICE_RECOGNITION) ||
Eric Laurent4eb58f12018-12-07 16:41:02 -0800427 source == AUDIO_SOURCE_HOTWORD) && !isSensitiveActive) {
428 forceIdle = false;
429 }
430 }
431 } else if (mUidPolicy->isA11yUid(current->uid)) {
Eric Laurenta46bedb2018-12-07 18:01:26 -0800432 if ((isOnTop || isLatest) &&
Eric Laurent4eb58f12018-12-07 16:41:02 -0800433 (source == AUDIO_SOURCE_VOICE_RECOGNITION || source == AUDIO_SOURCE_HOTWORD)) {
434 forceIdle = false;
435 }
436 } else {
Eric Laurenta46bedb2018-12-07 18:01:26 -0800437 if (!isAssistantOnTop && (isOnTop || isLatest) &&
Eric Laurent4eb58f12018-12-07 16:41:02 -0800438 (!isSensitiveActive || isLatestSensitive)) {
439 forceIdle = false;
440 }
441 }
442 setAppState_l(current->uid,
443 forceIdle ? APP_STATE_IDLE :
444 apmStatFromAmState(mUidPolicy->getUidState(current->uid)));
Eric Laurente8c8b432018-10-17 10:08:02 -0700445 }
446}
447
448/* static */
449app_state_t AudioPolicyService::apmStatFromAmState(int amState) {
450 switch (amState) {
451 case ActivityManager::PROCESS_STATE_UNKNOWN:
452 return APP_STATE_IDLE;
453 case ActivityManager::PROCESS_STATE_TOP:
454 return APP_STATE_TOP;
455 default:
456 break;
457 }
458 return APP_STATE_FOREGROUND;
459}
460
Eric Laurent4eb58f12018-12-07 16:41:02 -0800461/* static */
462bool AudioPolicyService::isPrivacySensitive(audio_source_t source)
463{
464 switch (source) {
465 case AUDIO_SOURCE_VOICE_UPLINK:
466 case AUDIO_SOURCE_VOICE_DOWNLINK:
467 case AUDIO_SOURCE_VOICE_CALL:
468 case AUDIO_SOURCE_CAMCORDER:
469 case AUDIO_SOURCE_VOICE_COMMUNICATION:
470 return true;
471 default:
472 break;
473 }
474 return false;
475}
476
Eric Laurente8c8b432018-10-17 10:08:02 -0700477void AudioPolicyService::setAppState_l(uid_t uid, app_state_t state)
478{
479 AutoCallerClear acc;
480
481 if (mAudioPolicyManager) {
482 mAudioPolicyManager->setAppState(uid, state);
Mikhail Naganoveae73eb2018-04-03 16:57:36 -0700483 }
484 sp<IAudioFlinger> af = AudioSystem::get_audio_flinger();
485 if (af) {
Eric Laurentf32108e2018-10-04 17:22:04 -0700486 bool silenced = state == APP_STATE_IDLE;
Mikhail Naganoveae73eb2018-04-03 16:57:36 -0700487 af->setRecordSilenced(uid, silenced);
488 }
Svet Ganovf4ddfef2018-01-16 07:37:58 -0800489}
490
Glenn Kasten0f11b512014-01-31 16:18:54 -0800491status_t AudioPolicyService::dump(int fd, const Vector<String16>& args __unused)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700492{
Glenn Kasten44deb052012-02-05 18:09:08 -0800493 if (!dumpAllowed()) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700494 dumpPermissionDenial(fd);
495 } else {
496 bool locked = tryLock(mLock);
497 if (!locked) {
498 String8 result(kDeadlockedString);
499 write(fd, result.string(), result.size());
500 }
501
502 dumpInternals(fd);
Glenn Kasten9d1f02d2012-02-08 17:47:58 -0800503 if (mAudioCommandThread != 0) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700504 mAudioCommandThread->dump(fd);
505 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700506
Eric Laurentdce54a12014-03-10 12:19:46 -0700507 if (mAudioPolicyManager) {
508 mAudioPolicyManager->dump(fd);
509 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700510
511 if (locked) mLock.unlock();
512 }
513 return NO_ERROR;
514}
515
516status_t AudioPolicyService::dumpPermissionDenial(int fd)
517{
518 const size_t SIZE = 256;
519 char buffer[SIZE];
520 String8 result;
521 snprintf(buffer, SIZE, "Permission Denial: "
522 "can't dump AudioPolicyService from pid=%d, uid=%d\n",
523 IPCThreadState::self()->getCallingPid(),
524 IPCThreadState::self()->getCallingUid());
525 result.append(buffer);
526 write(fd, result.string(), result.size());
527 return NO_ERROR;
528}
529
530status_t AudioPolicyService::onTransact(
Svet Ganovf4ddfef2018-01-16 07:37:58 -0800531 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags) {
532 switch (code) {
533 case SHELL_COMMAND_TRANSACTION: {
534 int in = data.readFileDescriptor();
535 int out = data.readFileDescriptor();
536 int err = data.readFileDescriptor();
537 int argc = data.readInt32();
538 Vector<String16> args;
539 for (int i = 0; i < argc && data.dataAvail() > 0; i++) {
540 args.add(data.readString16());
541 }
542 sp<IBinder> unusedCallback;
543 sp<IResultReceiver> resultReceiver;
544 status_t status;
545 if ((status = data.readNullableStrongBinder(&unusedCallback)) != NO_ERROR) {
546 return status;
547 }
548 if ((status = data.readNullableStrongBinder(&resultReceiver)) != NO_ERROR) {
549 return status;
550 }
551 status = shellCommand(in, out, err, args);
552 if (resultReceiver != nullptr) {
553 resultReceiver->send(status);
554 }
555 return NO_ERROR;
556 }
557 }
558
Mathias Agopian65ab4712010-07-14 17:59:35 -0700559 return BnAudioPolicyService::onTransact(code, data, reply, flags);
560}
561
Svet Ganovf4ddfef2018-01-16 07:37:58 -0800562// ------------------- Shell command implementation -------------------
563
564// NOTE: This is a remote API - make sure all args are validated
565status_t AudioPolicyService::shellCommand(int in, int out, int err, Vector<String16>& args) {
566 if (!checkCallingPermission(sManageAudioPolicyPermission, nullptr, nullptr)) {
567 return PERMISSION_DENIED;
568 }
569 if (in == BAD_TYPE || out == BAD_TYPE || err == BAD_TYPE) {
570 return BAD_VALUE;
571 }
572 if (args.size() == 3 && args[0] == String16("set-uid-state")) {
573 return handleSetUidState(args, err);
574 } else if (args.size() == 2 && args[0] == String16("reset-uid-state")) {
575 return handleResetUidState(args, err);
576 } else if (args.size() == 2 && args[0] == String16("get-uid-state")) {
577 return handleGetUidState(args, out, err);
578 } else if (args.size() == 1 && args[0] == String16("help")) {
579 printHelp(out);
580 return NO_ERROR;
581 }
582 printHelp(err);
583 return BAD_VALUE;
584}
585
586status_t AudioPolicyService::handleSetUidState(Vector<String16>& args, int err) {
587 PermissionController pc;
588 int uid = pc.getPackageUid(args[1], 0);
589 if (uid <= 0) {
590 ALOGE("Unknown package: '%s'", String8(args[1]).string());
591 dprintf(err, "Unknown package: '%s'\n", String8(args[1]).string());
592 return BAD_VALUE;
593 }
594 bool active = false;
595 if (args[2] == String16("active")) {
596 active = true;
597 } else if ((args[2] != String16("idle"))) {
598 ALOGE("Expected active or idle but got: '%s'", String8(args[2]).string());
599 return BAD_VALUE;
600 }
601 mUidPolicy->addOverrideUid(uid, active);
602 return NO_ERROR;
603}
604
605status_t AudioPolicyService::handleResetUidState(Vector<String16>& args, int err) {
606 PermissionController pc;
607 int uid = pc.getPackageUid(args[1], 0);
608 if (uid < 0) {
609 ALOGE("Unknown package: '%s'", String8(args[1]).string());
610 dprintf(err, "Unknown package: '%s'\n", String8(args[1]).string());
611 return BAD_VALUE;
612 }
613 mUidPolicy->removeOverrideUid(uid);
614 return NO_ERROR;
615}
616
617status_t AudioPolicyService::handleGetUidState(Vector<String16>& args, int out, int err) {
618 PermissionController pc;
619 int uid = pc.getPackageUid(args[1], 0);
620 if (uid < 0) {
621 ALOGE("Unknown package: '%s'", String8(args[1]).string());
622 dprintf(err, "Unknown package: '%s'\n", String8(args[1]).string());
623 return BAD_VALUE;
624 }
625 if (mUidPolicy->isUidActive(uid)) {
626 return dprintf(out, "active\n");
627 } else {
628 return dprintf(out, "idle\n");
629 }
630}
631
632status_t AudioPolicyService::printHelp(int out) {
633 return dprintf(out, "Audio policy service commands:\n"
634 " get-uid-state <PACKAGE> gets the uid state\n"
635 " set-uid-state <PACKAGE> <active|idle> overrides the uid state\n"
636 " reset-uid-state <PACKAGE> clears the uid state override\n"
637 " help print this message\n");
638}
639
640// ----------- AudioPolicyService::UidPolicy implementation ----------
641
642void AudioPolicyService::UidPolicy::registerSelf() {
643 ActivityManager am;
644 am.registerUidObserver(this, ActivityManager::UID_OBSERVER_GONE
645 | ActivityManager::UID_OBSERVER_IDLE
Eric Laurente8c8b432018-10-17 10:08:02 -0700646 | ActivityManager::UID_OBSERVER_ACTIVE
647 | ActivityManager::UID_OBSERVER_PROCSTATE,
Svet Ganovf4ddfef2018-01-16 07:37:58 -0800648 ActivityManager::PROCESS_STATE_UNKNOWN,
649 String16("audioserver"));
Mikhail Naganoveae73eb2018-04-03 16:57:36 -0700650 status_t res = am.linkToDeath(this);
651 if (!res) {
652 Mutex::Autolock _l(mLock);
653 mObserverRegistered = true;
654 } else {
655 ALOGE("UidPolicy::registerSelf linkToDeath failed: %d", res);
Eric Laurent4eb58f12018-12-07 16:41:02 -0800656
Mikhail Naganoveae73eb2018-04-03 16:57:36 -0700657 am.unregisterUidObserver(this);
658 }
Svet Ganovf4ddfef2018-01-16 07:37:58 -0800659}
660
661void AudioPolicyService::UidPolicy::unregisterSelf() {
662 ActivityManager am;
Mikhail Naganoveae73eb2018-04-03 16:57:36 -0700663 am.unlinkToDeath(this);
Svet Ganovf4ddfef2018-01-16 07:37:58 -0800664 am.unregisterUidObserver(this);
Mikhail Naganoveae73eb2018-04-03 16:57:36 -0700665 Mutex::Autolock _l(mLock);
666 mObserverRegistered = false;
Svet Ganovf4ddfef2018-01-16 07:37:58 -0800667}
668
Mikhail Naganoveae73eb2018-04-03 16:57:36 -0700669void AudioPolicyService::UidPolicy::binderDied(__unused const wp<IBinder> &who) {
670 Mutex::Autolock _l(mLock);
671 mCachedUids.clear();
672 mObserverRegistered = false;
Svet Ganovf4ddfef2018-01-16 07:37:58 -0800673}
674
Eric Laurente8c8b432018-10-17 10:08:02 -0700675void AudioPolicyService::UidPolicy::checkRegistered() {
Mikhail Naganoveae73eb2018-04-03 16:57:36 -0700676 bool needToReregister = false;
677 {
678 Mutex::Autolock _l(mLock);
679 needToReregister = !mObserverRegistered;
Svet Ganovf4ddfef2018-01-16 07:37:58 -0800680 }
Mikhail Naganoveae73eb2018-04-03 16:57:36 -0700681 if (needToReregister) {
682 // Looks like ActivityManager has died previously, attempt to re-register.
683 registerSelf();
684 }
Eric Laurente8c8b432018-10-17 10:08:02 -0700685}
686
687bool AudioPolicyService::UidPolicy::isUidActive(uid_t uid) {
688 if (isServiceUid(uid)) return true;
689 checkRegistered();
Mikhail Naganoveae73eb2018-04-03 16:57:36 -0700690 {
691 Mutex::Autolock _l(mLock);
692 auto overrideIter = mOverrideUids.find(uid);
693 if (overrideIter != mOverrideUids.end()) {
Eric Laurente8c8b432018-10-17 10:08:02 -0700694 return overrideIter->second.first;
Mikhail Naganoveae73eb2018-04-03 16:57:36 -0700695 }
696 // In an absense of the ActivityManager, assume everything to be active.
697 if (!mObserverRegistered) return true;
698 auto cacheIter = mCachedUids.find(uid);
Mikhail Naganoveba668a2018-04-05 08:13:15 -0700699 if (cacheIter != mCachedUids.end()) {
Eric Laurente8c8b432018-10-17 10:08:02 -0700700 return cacheIter->second.first;
Mikhail Naganoveae73eb2018-04-03 16:57:36 -0700701 }
702 }
703 ActivityManager am;
704 bool active = am.isUidActive(uid, String16("audioserver"));
705 {
706 Mutex::Autolock _l(mLock);
Eric Laurente8c8b432018-10-17 10:08:02 -0700707 mCachedUids.insert(std::pair<uid_t,
708 std::pair<bool, int>>(uid, std::pair<bool, int>(active,
709 ActivityManager::PROCESS_STATE_UNKNOWN)));
Mikhail Naganoveae73eb2018-04-03 16:57:36 -0700710 }
711 return active;
Svet Ganovf4ddfef2018-01-16 07:37:58 -0800712}
713
Eric Laurente8c8b432018-10-17 10:08:02 -0700714int AudioPolicyService::UidPolicy::getUidState(uid_t uid) {
715 if (isServiceUid(uid)) {
716 return ActivityManager::PROCESS_STATE_TOP;
717 }
718 checkRegistered();
719 {
720 Mutex::Autolock _l(mLock);
721 auto overrideIter = mOverrideUids.find(uid);
722 if (overrideIter != mOverrideUids.end()) {
723 if (overrideIter->second.first) {
724 if (overrideIter->second.second != ActivityManager::PROCESS_STATE_UNKNOWN) {
725 return overrideIter->second.second;
726 } else {
727 auto cacheIter = mCachedUids.find(uid);
728 if (cacheIter != mCachedUids.end()) {
729 return cacheIter->second.second;
730 }
731 }
732 }
733 return ActivityManager::PROCESS_STATE_UNKNOWN;
734 }
735 // In an absense of the ActivityManager, assume everything to be active.
736 if (!mObserverRegistered) {
737 return ActivityManager::PROCESS_STATE_TOP;
738 }
739 auto cacheIter = mCachedUids.find(uid);
740 if (cacheIter != mCachedUids.end()) {
741 if (cacheIter->second.first) {
742 return cacheIter->second.second;
743 } else {
744 return ActivityManager::PROCESS_STATE_UNKNOWN;
745 }
746 }
747 }
748 ActivityManager am;
749 bool active = am.isUidActive(uid, String16("audioserver"));
750 int state = ActivityManager::PROCESS_STATE_UNKNOWN;
751 if (active) {
752 state = am.getUidProcessState(uid, String16("audioserver"));
753 }
754 {
755 Mutex::Autolock _l(mLock);
756 mCachedUids.insert(std::pair<uid_t,
757 std::pair<bool, int>>(uid, std::pair<bool, int>(active, state)));
758 }
Eric Laurent4eb58f12018-12-07 16:41:02 -0800759
Eric Laurente8c8b432018-10-17 10:08:02 -0700760 return state;
761}
762
Mikhail Naganoveae73eb2018-04-03 16:57:36 -0700763void AudioPolicyService::UidPolicy::onUidActive(uid_t uid) {
Eric Laurente8c8b432018-10-17 10:08:02 -0700764 updateUid(&mCachedUids, uid, true, ActivityManager::PROCESS_STATE_UNKNOWN, true);
Mikhail Naganoveae73eb2018-04-03 16:57:36 -0700765}
766
767void AudioPolicyService::UidPolicy::onUidGone(uid_t uid, __unused bool disabled) {
Eric Laurente8c8b432018-10-17 10:08:02 -0700768 updateUid(&mCachedUids, uid, false, ActivityManager::PROCESS_STATE_UNKNOWN, false);
Mikhail Naganoveae73eb2018-04-03 16:57:36 -0700769}
770
771void AudioPolicyService::UidPolicy::onUidIdle(uid_t uid, __unused bool disabled) {
Eric Laurente8c8b432018-10-17 10:08:02 -0700772 updateUid(&mCachedUids, uid, false, ActivityManager::PROCESS_STATE_UNKNOWN, true);
Mikhail Naganoveae73eb2018-04-03 16:57:36 -0700773}
774
Eric Laurente8c8b432018-10-17 10:08:02 -0700775void AudioPolicyService::UidPolicy::onUidStateChanged(uid_t uid,
776 int32_t procState,
777 int64_t procStateSeq __unused) {
778 if (procState != ActivityManager::PROCESS_STATE_UNKNOWN) {
779 updateUid(&mCachedUids, uid, true, procState, true);
Mikhail Naganoveae73eb2018-04-03 16:57:36 -0700780 }
781}
782
783void AudioPolicyService::UidPolicy::updateOverrideUid(uid_t uid, bool active, bool insert) {
Eric Laurente8c8b432018-10-17 10:08:02 -0700784 updateUid(&mOverrideUids, uid, active, ActivityManager::PROCESS_STATE_UNKNOWN, insert);
785}
786
787void AudioPolicyService::UidPolicy::notifyService() {
788 sp<AudioPolicyService> service = mService.promote();
789 if (service != nullptr) {
790 service->updateUidStates();
Mikhail Naganoveae73eb2018-04-03 16:57:36 -0700791 }
792}
793
Eric Laurente8c8b432018-10-17 10:08:02 -0700794void AudioPolicyService::UidPolicy::updateUid(std::unordered_map<uid_t,
795 std::pair<bool, int>> *uids,
796 uid_t uid,
797 bool active,
798 int state,
799 bool insert) {
800 if (isServiceUid(uid)) {
801 return;
802 }
803 bool wasActive = isUidActive(uid);
804 int previousState = getUidState(uid);
Mikhail Naganoveae73eb2018-04-03 16:57:36 -0700805 {
806 Mutex::Autolock _l(mLock);
Eric Laurente8c8b432018-10-17 10:08:02 -0700807 updateUidLocked(uids, uid, active, state, insert);
Mikhail Naganoveae73eb2018-04-03 16:57:36 -0700808 }
Eric Laurente8c8b432018-10-17 10:08:02 -0700809 if (wasActive != isUidActive(uid) || state != previousState) {
810 notifyService();
811 }
Mikhail Naganoveae73eb2018-04-03 16:57:36 -0700812}
813
Eric Laurente8c8b432018-10-17 10:08:02 -0700814void AudioPolicyService::UidPolicy::updateUidLocked(std::unordered_map<uid_t,
815 std::pair<bool, int>> *uids,
816 uid_t uid,
817 bool active,
818 int state,
819 bool insert) {
Mikhail Naganoveae73eb2018-04-03 16:57:36 -0700820 auto it = uids->find(uid);
821 if (it != uids->end()) {
Mikhail Naganoveae73eb2018-04-03 16:57:36 -0700822 if (insert) {
Eric Laurente8c8b432018-10-17 10:08:02 -0700823 if (state == ActivityManager::PROCESS_STATE_UNKNOWN) {
824 it->second.first = active;
825 }
826 if (it->second.first) {
827 it->second.second = state;
828 } else {
829 it->second.second = ActivityManager::PROCESS_STATE_UNKNOWN;
830 }
Mikhail Naganoveae73eb2018-04-03 16:57:36 -0700831 } else {
832 uids->erase(it);
833 }
Eric Laurente8c8b432018-10-17 10:08:02 -0700834 } else if (insert && (state == ActivityManager::PROCESS_STATE_UNKNOWN)) {
835 uids->insert(std::pair<uid_t, std::pair<bool, int>>(uid,
836 std::pair<bool, int>(active, state)));
Mikhail Naganoveae73eb2018-04-03 16:57:36 -0700837 }
Svet Ganovf4ddfef2018-01-16 07:37:58 -0800838}
Mathias Agopian65ab4712010-07-14 17:59:35 -0700839
Eric Laurent4eb58f12018-12-07 16:41:02 -0800840bool AudioPolicyService::UidPolicy::isA11yOnTop() {
841 for (const auto &uid : mCachedUids) {
842 std::vector<uid_t>::iterator it = find(mA11yUids.begin(), mA11yUids.end(), uid.first);
843 if (it == mA11yUids.end()) {
844 continue;
845 }
846 if (uid.second.second == ActivityManager::PROCESS_STATE_TOP ||
847 uid.second.second == ActivityManager::PROCESS_STATE_FOREGROUND_SERVICE ||
848 uid.second.second == ActivityManager::PROCESS_STATE_BOUND_FOREGROUND_SERVICE) {
849 return true;
850 }
851 }
852 return false;
853}
854
Eric Laurentb78763e2018-10-17 10:08:02 -0700855bool AudioPolicyService::UidPolicy::isA11yUid(uid_t uid)
856{
857 std::vector<uid_t>::iterator it = find(mA11yUids.begin(), mA11yUids.end(), uid);
858 return it != mA11yUids.end();
859}
860
Mathias Agopian65ab4712010-07-14 17:59:35 -0700861// ----------- AudioPolicyService::AudioCommandThread implementation ----------
862
Eric Laurentbfb1b832013-01-07 09:53:42 -0800863AudioPolicyService::AudioCommandThread::AudioCommandThread(String8 name,
864 const wp<AudioPolicyService>& service)
865 : Thread(false), mName(name), mService(service)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700866{
Mathias Agopian65ab4712010-07-14 17:59:35 -0700867}
868
869
870AudioPolicyService::AudioCommandThread::~AudioCommandThread()
871{
Eric Laurentbfb1b832013-01-07 09:53:42 -0800872 if (!mAudioCommands.isEmpty()) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700873 release_wake_lock(mName.string());
874 }
875 mAudioCommands.clear();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700876}
877
878void AudioPolicyService::AudioCommandThread::onFirstRef()
879{
Eric Laurentbfb1b832013-01-07 09:53:42 -0800880 run(mName.string(), ANDROID_PRIORITY_AUDIO);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700881}
882
883bool AudioPolicyService::AudioCommandThread::threadLoop()
884{
Eric Laurentd7eda8d2016-02-02 17:18:39 -0800885 nsecs_t waitTime = -1;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700886
887 mLock.lock();
888 while (!exitPending())
889 {
Eric Laurent59a89232014-06-08 14:14:17 -0700890 sp<AudioPolicyService> svc;
891 while (!mAudioCommands.isEmpty() && !exitPending()) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700892 nsecs_t curTime = systemTime();
893 // commands are sorted by increasing time stamp: execute them from index 0 and up
894 if (mAudioCommands[0]->mTime <= curTime) {
Eric Laurent0ede8922014-05-09 18:04:42 -0700895 sp<AudioCommand> command = mAudioCommands[0];
Mathias Agopian65ab4712010-07-14 17:59:35 -0700896 mAudioCommands.removeAt(0);
Eric Laurent0ede8922014-05-09 18:04:42 -0700897 mLastCommand = command;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700898
899 switch (command->mCommand) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700900 case SET_VOLUME: {
Eric Laurent0ede8922014-05-09 18:04:42 -0700901 VolumeData *data = (VolumeData *)command->mParam.get();
Steve Block3856b092011-10-20 11:56:00 +0100902 ALOGV("AudioCommandThread() processing set volume stream %d, \
Eric Laurentde070132010-07-13 04:45:46 -0700903 volume %f, output %d", data->mStream, data->mVolume, data->mIO);
Andy Hungfe726a62018-09-27 15:17:25 -0700904 mLock.unlock();
Eric Laurentde070132010-07-13 04:45:46 -0700905 command->mStatus = AudioSystem::setStreamVolume(data->mStream,
906 data->mVolume,
907 data->mIO);
Andy Hungfe726a62018-09-27 15:17:25 -0700908 mLock.lock();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700909 }break;
910 case SET_PARAMETERS: {
Eric Laurent0ede8922014-05-09 18:04:42 -0700911 ParametersData *data = (ParametersData *)command->mParam.get();
Glenn Kastene53b9ea2012-03-12 16:29:55 -0700912 ALOGV("AudioCommandThread() processing set parameters string %s, io %d",
913 data->mKeyValuePairs.string(), data->mIO);
Andy Hungfe726a62018-09-27 15:17:25 -0700914 mLock.unlock();
Glenn Kastene53b9ea2012-03-12 16:29:55 -0700915 command->mStatus = AudioSystem::setParameters(data->mIO, data->mKeyValuePairs);
Andy Hungfe726a62018-09-27 15:17:25 -0700916 mLock.lock();
Glenn Kastene53b9ea2012-03-12 16:29:55 -0700917 }break;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700918 case SET_VOICE_VOLUME: {
Eric Laurent0ede8922014-05-09 18:04:42 -0700919 VoiceVolumeData *data = (VoiceVolumeData *)command->mParam.get();
Steve Block3856b092011-10-20 11:56:00 +0100920 ALOGV("AudioCommandThread() processing set voice volume volume %f",
Eric Laurentde070132010-07-13 04:45:46 -0700921 data->mVolume);
Andy Hungfe726a62018-09-27 15:17:25 -0700922 mLock.unlock();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700923 command->mStatus = AudioSystem::setVoiceVolume(data->mVolume);
Andy Hungfe726a62018-09-27 15:17:25 -0700924 mLock.lock();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700925 }break;
Eric Laurentbfb1b832013-01-07 09:53:42 -0800926 case STOP_OUTPUT: {
Eric Laurent0ede8922014-05-09 18:04:42 -0700927 StopOutputData *data = (StopOutputData *)command->mParam.get();
Eric Laurentd7fe0862018-07-14 16:48:01 -0700928 ALOGV("AudioCommandThread() processing stop output portId %d",
929 data->mPortId);
Eric Laurent59a89232014-06-08 14:14:17 -0700930 svc = mService.promote();
Eric Laurentbfb1b832013-01-07 09:53:42 -0800931 if (svc == 0) {
932 break;
933 }
934 mLock.unlock();
Eric Laurentd7fe0862018-07-14 16:48:01 -0700935 svc->doStopOutput(data->mPortId);
Eric Laurentbfb1b832013-01-07 09:53:42 -0800936 mLock.lock();
Eric Laurentbfb1b832013-01-07 09:53:42 -0800937 }break;
938 case RELEASE_OUTPUT: {
Eric Laurent0ede8922014-05-09 18:04:42 -0700939 ReleaseOutputData *data = (ReleaseOutputData *)command->mParam.get();
Eric Laurentd7fe0862018-07-14 16:48:01 -0700940 ALOGV("AudioCommandThread() processing release output portId %d",
941 data->mPortId);
Eric Laurent59a89232014-06-08 14:14:17 -0700942 svc = mService.promote();
Eric Laurentbfb1b832013-01-07 09:53:42 -0800943 if (svc == 0) {
944 break;
945 }
946 mLock.unlock();
Eric Laurentd7fe0862018-07-14 16:48:01 -0700947 svc->doReleaseOutput(data->mPortId);
Eric Laurentbfb1b832013-01-07 09:53:42 -0800948 mLock.lock();
Eric Laurentbfb1b832013-01-07 09:53:42 -0800949 }break;
Eric Laurent951f4552014-05-20 10:48:17 -0700950 case CREATE_AUDIO_PATCH: {
951 CreateAudioPatchData *data = (CreateAudioPatchData *)command->mParam.get();
952 ALOGV("AudioCommandThread() processing create audio patch");
953 sp<IAudioFlinger> af = AudioSystem::get_audio_flinger();
954 if (af == 0) {
955 command->mStatus = PERMISSION_DENIED;
956 } else {
Andy Hungfe726a62018-09-27 15:17:25 -0700957 mLock.unlock();
Eric Laurent951f4552014-05-20 10:48:17 -0700958 command->mStatus = af->createAudioPatch(&data->mPatch, &data->mHandle);
Andy Hungfe726a62018-09-27 15:17:25 -0700959 mLock.lock();
Eric Laurent951f4552014-05-20 10:48:17 -0700960 }
961 } break;
962 case RELEASE_AUDIO_PATCH: {
963 ReleaseAudioPatchData *data = (ReleaseAudioPatchData *)command->mParam.get();
964 ALOGV("AudioCommandThread() processing release audio patch");
965 sp<IAudioFlinger> af = AudioSystem::get_audio_flinger();
966 if (af == 0) {
967 command->mStatus = PERMISSION_DENIED;
968 } else {
Andy Hungfe726a62018-09-27 15:17:25 -0700969 mLock.unlock();
Eric Laurent951f4552014-05-20 10:48:17 -0700970 command->mStatus = af->releaseAudioPatch(data->mHandle);
Andy Hungfe726a62018-09-27 15:17:25 -0700971 mLock.lock();
Eric Laurent951f4552014-05-20 10:48:17 -0700972 }
973 } break;
Eric Laurentb52c1522014-05-20 11:27:36 -0700974 case UPDATE_AUDIOPORT_LIST: {
975 ALOGV("AudioCommandThread() processing update audio port list");
Eric Laurent59a89232014-06-08 14:14:17 -0700976 svc = mService.promote();
Eric Laurentb52c1522014-05-20 11:27:36 -0700977 if (svc == 0) {
978 break;
979 }
980 mLock.unlock();
981 svc->doOnAudioPortListUpdate();
982 mLock.lock();
983 }break;
984 case UPDATE_AUDIOPATCH_LIST: {
985 ALOGV("AudioCommandThread() processing update audio patch list");
Eric Laurent59a89232014-06-08 14:14:17 -0700986 svc = mService.promote();
Eric Laurentb52c1522014-05-20 11:27:36 -0700987 if (svc == 0) {
988 break;
989 }
990 mLock.unlock();
991 svc->doOnAudioPatchListUpdate();
992 mLock.lock();
993 }break;
Eric Laurente1715a42014-05-20 11:30:42 -0700994 case SET_AUDIOPORT_CONFIG: {
995 SetAudioPortConfigData *data = (SetAudioPortConfigData *)command->mParam.get();
996 ALOGV("AudioCommandThread() processing set port config");
997 sp<IAudioFlinger> af = AudioSystem::get_audio_flinger();
998 if (af == 0) {
999 command->mStatus = PERMISSION_DENIED;
1000 } else {
Andy Hungfe726a62018-09-27 15:17:25 -07001001 mLock.unlock();
Eric Laurente1715a42014-05-20 11:30:42 -07001002 command->mStatus = af->setAudioPortConfig(&data->mConfig);
Andy Hungfe726a62018-09-27 15:17:25 -07001003 mLock.lock();
Eric Laurente1715a42014-05-20 11:30:42 -07001004 }
1005 } break;
Jean-Michel Trivide801052015-04-14 19:10:14 -07001006 case DYN_POLICY_MIX_STATE_UPDATE: {
1007 DynPolicyMixStateUpdateData *data =
1008 (DynPolicyMixStateUpdateData *)command->mParam.get();
Jean-Michel Trivide801052015-04-14 19:10:14 -07001009 ALOGV("AudioCommandThread() processing dyn policy mix state update %s %d",
1010 data->mRegId.string(), data->mState);
1011 svc = mService.promote();
1012 if (svc == 0) {
1013 break;
1014 }
1015 mLock.unlock();
1016 svc->doOnDynamicPolicyMixStateUpdate(data->mRegId, data->mState);
1017 mLock.lock();
1018 } break;
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -08001019 case RECORDING_CONFIGURATION_UPDATE: {
1020 RecordingConfigurationUpdateData *data =
1021 (RecordingConfigurationUpdateData *)command->mParam.get();
1022 ALOGV("AudioCommandThread() processing recording configuration update");
1023 svc = mService.promote();
1024 if (svc == 0) {
1025 break;
1026 }
1027 mLock.unlock();
Jean-Michel Triviac4e4292016-12-22 11:39:31 -08001028 svc->doOnRecordingConfigurationUpdate(data->mEvent, &data->mClientInfo,
1029 &data->mClientConfig, &data->mDeviceConfig,
Jean-Michel Trivi8c7cf3b2016-02-25 17:08:24 -08001030 data->mPatchHandle);
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -08001031 mLock.lock();
1032 } break;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001033 default:
Steve Block5ff1dd52012-01-05 23:22:43 +00001034 ALOGW("AudioCommandThread() unknown command %d", command->mCommand);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001035 }
Eric Laurent0ede8922014-05-09 18:04:42 -07001036 {
1037 Mutex::Autolock _l(command->mLock);
1038 if (command->mWaitStatus) {
1039 command->mWaitStatus = false;
1040 command->mCond.signal();
1041 }
1042 }
Eric Laurentd7eda8d2016-02-02 17:18:39 -08001043 waitTime = -1;
Zach Janga754b4f2015-10-27 01:29:34 +00001044 // release mLock before releasing strong reference on the service as
1045 // AudioPolicyService destructor calls AudioCommandThread::exit() which
1046 // acquires mLock.
1047 mLock.unlock();
1048 svc.clear();
1049 mLock.lock();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001050 } else {
1051 waitTime = mAudioCommands[0]->mTime - curTime;
1052 break;
1053 }
1054 }
Zach Janga754b4f2015-10-27 01:29:34 +00001055
1056 // release delayed commands wake lock if the queue is empty
1057 if (mAudioCommands.isEmpty()) {
Ricardo Garcia05f2fdc2014-07-24 15:48:24 -07001058 release_wake_lock(mName.string());
Zach Janga754b4f2015-10-27 01:29:34 +00001059 }
1060
1061 // At this stage we have either an empty command queue or the first command in the queue
1062 // has a finite delay. So unless we are exiting it is safe to wait.
1063 if (!exitPending()) {
Eric Laurent59a89232014-06-08 14:14:17 -07001064 ALOGV("AudioCommandThread() going to sleep");
Eric Laurentd7eda8d2016-02-02 17:18:39 -08001065 if (waitTime == -1) {
1066 mWaitWorkCV.wait(mLock);
1067 } else {
1068 mWaitWorkCV.waitRelative(mLock, waitTime);
1069 }
Eric Laurent59a89232014-06-08 14:14:17 -07001070 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001071 }
Ricardo Garcia05f2fdc2014-07-24 15:48:24 -07001072 // release delayed commands wake lock before quitting
1073 if (!mAudioCommands.isEmpty()) {
1074 release_wake_lock(mName.string());
1075 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001076 mLock.unlock();
1077 return false;
1078}
1079
1080status_t AudioPolicyService::AudioCommandThread::dump(int fd)
1081{
1082 const size_t SIZE = 256;
1083 char buffer[SIZE];
1084 String8 result;
1085
1086 snprintf(buffer, SIZE, "AudioCommandThread %p Dump\n", this);
1087 result.append(buffer);
1088 write(fd, result.string(), result.size());
1089
1090 bool locked = tryLock(mLock);
1091 if (!locked) {
1092 String8 result2(kCmdDeadlockedString);
1093 write(fd, result2.string(), result2.size());
1094 }
1095
1096 snprintf(buffer, SIZE, "- Commands:\n");
1097 result = String8(buffer);
1098 result.append(" Command Time Wait pParam\n");
Glenn Kasten8d6a2442012-02-08 14:04:28 -08001099 for (size_t i = 0; i < mAudioCommands.size(); i++) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07001100 mAudioCommands[i]->dump(buffer, SIZE);
1101 result.append(buffer);
1102 }
1103 result.append(" Last Command\n");
Eric Laurent0ede8922014-05-09 18:04:42 -07001104 if (mLastCommand != 0) {
1105 mLastCommand->dump(buffer, SIZE);
1106 result.append(buffer);
1107 } else {
1108 result.append(" none\n");
1109 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001110
1111 write(fd, result.string(), result.size());
1112
1113 if (locked) mLock.unlock();
1114
1115 return NO_ERROR;
1116}
1117
Glenn Kastenfff6d712012-01-12 16:38:12 -08001118status_t AudioPolicyService::AudioCommandThread::volumeCommand(audio_stream_type_t stream,
Eric Laurentde070132010-07-13 04:45:46 -07001119 float volume,
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001120 audio_io_handle_t output,
Eric Laurentde070132010-07-13 04:45:46 -07001121 int delayMs)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001122{
Eric Laurent0ede8922014-05-09 18:04:42 -07001123 sp<AudioCommand> command = new AudioCommand();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001124 command->mCommand = SET_VOLUME;
Eric Laurent0ede8922014-05-09 18:04:42 -07001125 sp<VolumeData> data = new VolumeData();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001126 data->mStream = stream;
1127 data->mVolume = volume;
1128 data->mIO = output;
1129 command->mParam = data;
Eric Laurent0ede8922014-05-09 18:04:42 -07001130 command->mWaitStatus = true;
Steve Block3856b092011-10-20 11:56:00 +01001131 ALOGV("AudioCommandThread() adding set volume stream %d, volume %f, output %d",
Eric Laurentde070132010-07-13 04:45:46 -07001132 stream, volume, output);
Eric Laurent0ede8922014-05-09 18:04:42 -07001133 return sendCommand(command, delayMs);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001134}
1135
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001136status_t AudioPolicyService::AudioCommandThread::parametersCommand(audio_io_handle_t ioHandle,
Dima Zavinfce7a472011-04-19 22:30:36 -07001137 const char *keyValuePairs,
Eric Laurentde070132010-07-13 04:45:46 -07001138 int delayMs)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001139{
Eric Laurent0ede8922014-05-09 18:04:42 -07001140 sp<AudioCommand> command = new AudioCommand();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001141 command->mCommand = SET_PARAMETERS;
Eric Laurent0ede8922014-05-09 18:04:42 -07001142 sp<ParametersData> data = new ParametersData();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001143 data->mIO = ioHandle;
Dima Zavinfce7a472011-04-19 22:30:36 -07001144 data->mKeyValuePairs = String8(keyValuePairs);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001145 command->mParam = data;
Eric Laurent0ede8922014-05-09 18:04:42 -07001146 command->mWaitStatus = true;
Steve Block3856b092011-10-20 11:56:00 +01001147 ALOGV("AudioCommandThread() adding set parameter string %s, io %d ,delay %d",
Dima Zavinfce7a472011-04-19 22:30:36 -07001148 keyValuePairs, ioHandle, delayMs);
Eric Laurent0ede8922014-05-09 18:04:42 -07001149 return sendCommand(command, delayMs);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001150}
1151
1152status_t AudioPolicyService::AudioCommandThread::voiceVolumeCommand(float volume, int delayMs)
1153{
Eric Laurent0ede8922014-05-09 18:04:42 -07001154 sp<AudioCommand> command = new AudioCommand();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001155 command->mCommand = SET_VOICE_VOLUME;
Eric Laurent0ede8922014-05-09 18:04:42 -07001156 sp<VoiceVolumeData> data = new VoiceVolumeData();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001157 data->mVolume = volume;
1158 command->mParam = data;
Eric Laurent0ede8922014-05-09 18:04:42 -07001159 command->mWaitStatus = true;
Steve Block3856b092011-10-20 11:56:00 +01001160 ALOGV("AudioCommandThread() adding set voice volume volume %f", volume);
Eric Laurent0ede8922014-05-09 18:04:42 -07001161 return sendCommand(command, delayMs);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001162}
1163
Eric Laurentd7fe0862018-07-14 16:48:01 -07001164void AudioPolicyService::AudioCommandThread::stopOutputCommand(audio_port_handle_t portId)
Eric Laurentbfb1b832013-01-07 09:53:42 -08001165{
Eric Laurent0ede8922014-05-09 18:04:42 -07001166 sp<AudioCommand> command = new AudioCommand();
Eric Laurentbfb1b832013-01-07 09:53:42 -08001167 command->mCommand = STOP_OUTPUT;
Eric Laurent0ede8922014-05-09 18:04:42 -07001168 sp<StopOutputData> data = new StopOutputData();
Eric Laurentd7fe0862018-07-14 16:48:01 -07001169 data->mPortId = portId;
Jesper Tragardh48412dc2014-03-24 14:12:43 +01001170 command->mParam = data;
Eric Laurentd7fe0862018-07-14 16:48:01 -07001171 ALOGV("AudioCommandThread() adding stop output portId %d", portId);
Eric Laurent0ede8922014-05-09 18:04:42 -07001172 sendCommand(command);
Eric Laurentbfb1b832013-01-07 09:53:42 -08001173}
1174
Eric Laurentd7fe0862018-07-14 16:48:01 -07001175void AudioPolicyService::AudioCommandThread::releaseOutputCommand(audio_port_handle_t portId)
Eric Laurentbfb1b832013-01-07 09:53:42 -08001176{
Eric Laurent0ede8922014-05-09 18:04:42 -07001177 sp<AudioCommand> command = new AudioCommand();
Eric Laurentbfb1b832013-01-07 09:53:42 -08001178 command->mCommand = RELEASE_OUTPUT;
Eric Laurent0ede8922014-05-09 18:04:42 -07001179 sp<ReleaseOutputData> data = new ReleaseOutputData();
Eric Laurentd7fe0862018-07-14 16:48:01 -07001180 data->mPortId = portId;
Jesper Tragardh48412dc2014-03-24 14:12:43 +01001181 command->mParam = data;
Eric Laurentd7fe0862018-07-14 16:48:01 -07001182 ALOGV("AudioCommandThread() adding release output portId %d", portId);
Eric Laurent0ede8922014-05-09 18:04:42 -07001183 sendCommand(command);
1184}
1185
Eric Laurent951f4552014-05-20 10:48:17 -07001186status_t AudioPolicyService::AudioCommandThread::createAudioPatchCommand(
1187 const struct audio_patch *patch,
1188 audio_patch_handle_t *handle,
1189 int delayMs)
1190{
1191 status_t status = NO_ERROR;
1192
1193 sp<AudioCommand> command = new AudioCommand();
1194 command->mCommand = CREATE_AUDIO_PATCH;
1195 CreateAudioPatchData *data = new CreateAudioPatchData();
1196 data->mPatch = *patch;
1197 data->mHandle = *handle;
1198 command->mParam = data;
1199 command->mWaitStatus = true;
1200 ALOGV("AudioCommandThread() adding create patch delay %d", delayMs);
1201 status = sendCommand(command, delayMs);
1202 if (status == NO_ERROR) {
1203 *handle = data->mHandle;
1204 }
1205 return status;
1206}
1207
1208status_t AudioPolicyService::AudioCommandThread::releaseAudioPatchCommand(audio_patch_handle_t handle,
1209 int delayMs)
1210{
1211 sp<AudioCommand> command = new AudioCommand();
1212 command->mCommand = RELEASE_AUDIO_PATCH;
1213 ReleaseAudioPatchData *data = new ReleaseAudioPatchData();
1214 data->mHandle = handle;
1215 command->mParam = data;
1216 command->mWaitStatus = true;
1217 ALOGV("AudioCommandThread() adding release patch delay %d", delayMs);
1218 return sendCommand(command, delayMs);
1219}
1220
Eric Laurentb52c1522014-05-20 11:27:36 -07001221void AudioPolicyService::AudioCommandThread::updateAudioPortListCommand()
1222{
1223 sp<AudioCommand> command = new AudioCommand();
1224 command->mCommand = UPDATE_AUDIOPORT_LIST;
1225 ALOGV("AudioCommandThread() adding update audio port list");
1226 sendCommand(command);
1227}
1228
1229void AudioPolicyService::AudioCommandThread::updateAudioPatchListCommand()
1230{
1231 sp<AudioCommand>command = new AudioCommand();
1232 command->mCommand = UPDATE_AUDIOPATCH_LIST;
1233 ALOGV("AudioCommandThread() adding update audio patch list");
1234 sendCommand(command);
1235}
1236
Eric Laurente1715a42014-05-20 11:30:42 -07001237status_t AudioPolicyService::AudioCommandThread::setAudioPortConfigCommand(
1238 const struct audio_port_config *config, int delayMs)
1239{
1240 sp<AudioCommand> command = new AudioCommand();
1241 command->mCommand = SET_AUDIOPORT_CONFIG;
1242 SetAudioPortConfigData *data = new SetAudioPortConfigData();
1243 data->mConfig = *config;
1244 command->mParam = data;
1245 command->mWaitStatus = true;
1246 ALOGV("AudioCommandThread() adding set port config delay %d", delayMs);
1247 return sendCommand(command, delayMs);
1248}
1249
Jean-Michel Trivide801052015-04-14 19:10:14 -07001250void AudioPolicyService::AudioCommandThread::dynamicPolicyMixStateUpdateCommand(
Chih-Hung Hsiehe964d4e2016-08-09 14:31:32 -07001251 const String8& regId, int32_t state)
Jean-Michel Trivide801052015-04-14 19:10:14 -07001252{
1253 sp<AudioCommand> command = new AudioCommand();
1254 command->mCommand = DYN_POLICY_MIX_STATE_UPDATE;
1255 DynPolicyMixStateUpdateData *data = new DynPolicyMixStateUpdateData();
1256 data->mRegId = regId;
1257 data->mState = state;
1258 command->mParam = data;
1259 ALOGV("AudioCommandThread() sending dynamic policy mix (id=%s) state update to %d",
1260 regId.string(), state);
1261 sendCommand(command);
1262}
1263
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -08001264void AudioPolicyService::AudioCommandThread::recordingConfigurationUpdateCommand(
Jean-Michel Triviac4e4292016-12-22 11:39:31 -08001265 int event, const record_client_info_t *clientInfo,
Jean-Michel Trivi8c7cf3b2016-02-25 17:08:24 -08001266 const audio_config_base_t *clientConfig, const audio_config_base_t *deviceConfig,
1267 audio_patch_handle_t patchHandle)
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -08001268{
1269 sp<AudioCommand>command = new AudioCommand();
1270 command->mCommand = RECORDING_CONFIGURATION_UPDATE;
1271 RecordingConfigurationUpdateData *data = new RecordingConfigurationUpdateData();
1272 data->mEvent = event;
Jean-Michel Triviac4e4292016-12-22 11:39:31 -08001273 data->mClientInfo = *clientInfo;
Jean-Michel Trivi7281aa92016-02-17 15:33:40 -08001274 data->mClientConfig = *clientConfig;
1275 data->mDeviceConfig = *deviceConfig;
Jean-Michel Trivi8c7cf3b2016-02-25 17:08:24 -08001276 data->mPatchHandle = patchHandle;
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -08001277 command->mParam = data;
Jean-Michel Triviac4e4292016-12-22 11:39:31 -08001278 ALOGV("AudioCommandThread() adding recording configuration update event %d, source %d uid %u",
1279 event, clientInfo->source, clientInfo->uid);
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -08001280 sendCommand(command);
1281}
1282
Eric Laurent0ede8922014-05-09 18:04:42 -07001283status_t AudioPolicyService::AudioCommandThread::sendCommand(sp<AudioCommand>& command, int delayMs)
1284{
1285 {
1286 Mutex::Autolock _l(mLock);
1287 insertCommand_l(command, delayMs);
1288 mWaitWorkCV.signal();
1289 }
1290 Mutex::Autolock _l(command->mLock);
1291 while (command->mWaitStatus) {
1292 nsecs_t timeOutNs = kAudioCommandTimeoutNs + milliseconds(delayMs);
1293 if (command->mCond.waitRelative(command->mLock, timeOutNs) != NO_ERROR) {
1294 command->mStatus = TIMED_OUT;
1295 command->mWaitStatus = false;
1296 }
1297 }
1298 return command->mStatus;
Eric Laurentbfb1b832013-01-07 09:53:42 -08001299}
1300
Mathias Agopian65ab4712010-07-14 17:59:35 -07001301// insertCommand_l() must be called with mLock held
Eric Laurent0ede8922014-05-09 18:04:42 -07001302void AudioPolicyService::AudioCommandThread::insertCommand_l(sp<AudioCommand>& command, int delayMs)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001303{
Glenn Kasten8d6a2442012-02-08 14:04:28 -08001304 ssize_t i; // not size_t because i will count down to -1
Eric Laurent0ede8922014-05-09 18:04:42 -07001305 Vector < sp<AudioCommand> > removedCommands;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001306 command->mTime = systemTime() + milliseconds(delayMs);
1307
1308 // acquire wake lock to make sure delayed commands are processed
Eric Laurentbfb1b832013-01-07 09:53:42 -08001309 if (mAudioCommands.isEmpty()) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07001310 acquire_wake_lock(PARTIAL_WAKE_LOCK, mName.string());
1311 }
1312
1313 // check same pending commands with later time stamps and eliminate them
Ivan Lozano5ff158f2017-10-30 09:06:24 -07001314 for (i = (ssize_t)mAudioCommands.size()-1; i >= 0; i--) {
Eric Laurent0ede8922014-05-09 18:04:42 -07001315 sp<AudioCommand> command2 = mAudioCommands[i];
Mathias Agopian65ab4712010-07-14 17:59:35 -07001316 // commands are sorted by increasing time stamp: no need to scan the rest of mAudioCommands
1317 if (command2->mTime <= command->mTime) break;
Eric Laurente45b48a2014-09-04 16:40:57 -07001318
1319 // create audio patch or release audio patch commands are equivalent
1320 // with regard to filtering
1321 if ((command->mCommand == CREATE_AUDIO_PATCH) ||
1322 (command->mCommand == RELEASE_AUDIO_PATCH)) {
1323 if ((command2->mCommand != CREATE_AUDIO_PATCH) &&
1324 (command2->mCommand != RELEASE_AUDIO_PATCH)) {
1325 continue;
1326 }
1327 } else if (command2->mCommand != command->mCommand) continue;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001328
1329 switch (command->mCommand) {
1330 case SET_PARAMETERS: {
Eric Laurent0ede8922014-05-09 18:04:42 -07001331 ParametersData *data = (ParametersData *)command->mParam.get();
1332 ParametersData *data2 = (ParametersData *)command2->mParam.get();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001333 if (data->mIO != data2->mIO) break;
Steve Block3856b092011-10-20 11:56:00 +01001334 ALOGV("Comparing parameter command %s to new command %s",
Eric Laurentde070132010-07-13 04:45:46 -07001335 data2->mKeyValuePairs.string(), data->mKeyValuePairs.string());
Mathias Agopian65ab4712010-07-14 17:59:35 -07001336 AudioParameter param = AudioParameter(data->mKeyValuePairs);
1337 AudioParameter param2 = AudioParameter(data2->mKeyValuePairs);
1338 for (size_t j = 0; j < param.size(); j++) {
Glenn Kastene53b9ea2012-03-12 16:29:55 -07001339 String8 key;
1340 String8 value;
1341 param.getAt(j, key, value);
1342 for (size_t k = 0; k < param2.size(); k++) {
1343 String8 key2;
1344 String8 value2;
1345 param2.getAt(k, key2, value2);
1346 if (key2 == key) {
1347 param2.remove(key2);
1348 ALOGV("Filtering out parameter %s", key2.string());
1349 break;
1350 }
1351 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001352 }
1353 // if all keys have been filtered out, remove the command.
1354 // otherwise, update the key value pairs
1355 if (param2.size() == 0) {
1356 removedCommands.add(command2);
1357 } else {
1358 data2->mKeyValuePairs = param2.toString();
1359 }
Eric Laurent21e54562013-09-23 12:08:05 -07001360 command->mTime = command2->mTime;
1361 // force delayMs to non 0 so that code below does not request to wait for
1362 // command status as the command is now delayed
1363 delayMs = 1;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001364 } break;
1365
1366 case SET_VOLUME: {
Eric Laurent0ede8922014-05-09 18:04:42 -07001367 VolumeData *data = (VolumeData *)command->mParam.get();
1368 VolumeData *data2 = (VolumeData *)command2->mParam.get();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001369 if (data->mIO != data2->mIO) break;
1370 if (data->mStream != data2->mStream) break;
Steve Block3856b092011-10-20 11:56:00 +01001371 ALOGV("Filtering out volume command on output %d for stream %d",
Eric Laurentde070132010-07-13 04:45:46 -07001372 data->mIO, data->mStream);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001373 removedCommands.add(command2);
Eric Laurent21e54562013-09-23 12:08:05 -07001374 command->mTime = command2->mTime;
1375 // force delayMs to non 0 so that code below does not request to wait for
1376 // command status as the command is now delayed
1377 delayMs = 1;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001378 } break;
Eric Laurente45b48a2014-09-04 16:40:57 -07001379
Eric Laurentbaf35fe2016-07-27 15:36:53 -07001380 case SET_VOICE_VOLUME: {
1381 VoiceVolumeData *data = (VoiceVolumeData *)command->mParam.get();
1382 VoiceVolumeData *data2 = (VoiceVolumeData *)command2->mParam.get();
1383 ALOGV("Filtering out voice volume command value %f replaced by %f",
1384 data2->mVolume, data->mVolume);
1385 removedCommands.add(command2);
1386 command->mTime = command2->mTime;
1387 // force delayMs to non 0 so that code below does not request to wait for
1388 // command status as the command is now delayed
1389 delayMs = 1;
1390 } break;
1391
Eric Laurente45b48a2014-09-04 16:40:57 -07001392 case CREATE_AUDIO_PATCH:
1393 case RELEASE_AUDIO_PATCH: {
1394 audio_patch_handle_t handle;
Haynes Mathew Georgea2d4a6d2014-10-13 13:05:22 -07001395 struct audio_patch patch;
Eric Laurente45b48a2014-09-04 16:40:57 -07001396 if (command->mCommand == CREATE_AUDIO_PATCH) {
1397 handle = ((CreateAudioPatchData *)command->mParam.get())->mHandle;
Haynes Mathew Georgea2d4a6d2014-10-13 13:05:22 -07001398 patch = ((CreateAudioPatchData *)command->mParam.get())->mPatch;
Eric Laurente45b48a2014-09-04 16:40:57 -07001399 } else {
1400 handle = ((ReleaseAudioPatchData *)command->mParam.get())->mHandle;
Mikhail Naganov7be71d22018-05-23 16:51:46 -07001401 memset(&patch, 0, sizeof(patch));
Eric Laurente45b48a2014-09-04 16:40:57 -07001402 }
1403 audio_patch_handle_t handle2;
Haynes Mathew Georgea2d4a6d2014-10-13 13:05:22 -07001404 struct audio_patch patch2;
Eric Laurente45b48a2014-09-04 16:40:57 -07001405 if (command2->mCommand == CREATE_AUDIO_PATCH) {
1406 handle2 = ((CreateAudioPatchData *)command2->mParam.get())->mHandle;
Haynes Mathew Georgea2d4a6d2014-10-13 13:05:22 -07001407 patch2 = ((CreateAudioPatchData *)command2->mParam.get())->mPatch;
Eric Laurente45b48a2014-09-04 16:40:57 -07001408 } else {
1409 handle2 = ((ReleaseAudioPatchData *)command2->mParam.get())->mHandle;
Glenn Kastenf60b6b62015-07-06 10:53:26 -07001410 memset(&patch2, 0, sizeof(patch2));
Eric Laurente45b48a2014-09-04 16:40:57 -07001411 }
1412 if (handle != handle2) break;
Haynes Mathew Georgea2d4a6d2014-10-13 13:05:22 -07001413 /* Filter CREATE_AUDIO_PATCH commands only when they are issued for
1414 same output. */
1415 if( (command->mCommand == CREATE_AUDIO_PATCH) &&
1416 (command2->mCommand == CREATE_AUDIO_PATCH) ) {
1417 bool isOutputDiff = false;
1418 if (patch.num_sources == patch2.num_sources) {
1419 for (unsigned count = 0; count < patch.num_sources; count++) {
1420 if (patch.sources[count].id != patch2.sources[count].id) {
1421 isOutputDiff = true;
1422 break;
1423 }
1424 }
1425 if (isOutputDiff)
1426 break;
1427 }
1428 }
Eric Laurente45b48a2014-09-04 16:40:57 -07001429 ALOGV("Filtering out %s audio patch command for handle %d",
1430 (command->mCommand == CREATE_AUDIO_PATCH) ? "create" : "release", handle);
1431 removedCommands.add(command2);
1432 command->mTime = command2->mTime;
1433 // force delayMs to non 0 so that code below does not request to wait for
1434 // command status as the command is now delayed
1435 delayMs = 1;
1436 } break;
1437
Jean-Michel Trivide801052015-04-14 19:10:14 -07001438 case DYN_POLICY_MIX_STATE_UPDATE: {
1439
1440 } break;
1441
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -08001442 case RECORDING_CONFIGURATION_UPDATE: {
1443
1444 } break;
1445
Mathias Agopian65ab4712010-07-14 17:59:35 -07001446 default:
1447 break;
1448 }
1449 }
1450
1451 // remove filtered commands
1452 for (size_t j = 0; j < removedCommands.size(); j++) {
1453 // removed commands always have time stamps greater than current command
1454 for (size_t k = i + 1; k < mAudioCommands.size(); k++) {
Eric Laurent0ede8922014-05-09 18:04:42 -07001455 if (mAudioCommands[k].get() == removedCommands[j].get()) {
Steve Block3856b092011-10-20 11:56:00 +01001456 ALOGV("suppressing command: %d", mAudioCommands[k]->mCommand);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001457 mAudioCommands.removeAt(k);
1458 break;
1459 }
1460 }
1461 }
1462 removedCommands.clear();
1463
Eric Laurentaa79bef2015-01-15 14:33:51 -08001464 // Disable wait for status if delay is not 0.
1465 // Except for create audio patch command because the returned patch handle
1466 // is needed by audio policy manager
1467 if (delayMs != 0 && command->mCommand != CREATE_AUDIO_PATCH) {
Eric Laurentcec4abb2012-07-03 12:23:02 -07001468 command->mWaitStatus = false;
1469 }
Eric Laurentcec4abb2012-07-03 12:23:02 -07001470
Mathias Agopian65ab4712010-07-14 17:59:35 -07001471 // insert command at the right place according to its time stamp
Eric Laurent1e693b52014-07-09 15:03:28 -07001472 ALOGV("inserting command: %d at index %zd, num commands %zu",
1473 command->mCommand, i+1, mAudioCommands.size());
Mathias Agopian65ab4712010-07-14 17:59:35 -07001474 mAudioCommands.insertAt(command, i + 1);
1475}
1476
1477void AudioPolicyService::AudioCommandThread::exit()
1478{
Steve Block3856b092011-10-20 11:56:00 +01001479 ALOGV("AudioCommandThread::exit");
Mathias Agopian65ab4712010-07-14 17:59:35 -07001480 {
1481 AutoMutex _l(mLock);
1482 requestExit();
1483 mWaitWorkCV.signal();
1484 }
Zach Janga754b4f2015-10-27 01:29:34 +00001485 // Note that we can call it from the thread loop if all other references have been released
1486 // but it will safely return WOULD_BLOCK in this case
Mathias Agopian65ab4712010-07-14 17:59:35 -07001487 requestExitAndWait();
1488}
1489
1490void AudioPolicyService::AudioCommandThread::AudioCommand::dump(char* buffer, size_t size)
1491{
1492 snprintf(buffer, size, " %02d %06d.%03d %01u %p\n",
1493 mCommand,
1494 (int)ns2s(mTime),
1495 (int)ns2ms(mTime)%1000,
1496 mWaitStatus,
Eric Laurent0ede8922014-05-09 18:04:42 -07001497 mParam.get());
Mathias Agopian65ab4712010-07-14 17:59:35 -07001498}
1499
Dima Zavinfce7a472011-04-19 22:30:36 -07001500/******* helpers for the service_ops callbacks defined below *********/
1501void AudioPolicyService::setParameters(audio_io_handle_t ioHandle,
1502 const char *keyValuePairs,
1503 int delayMs)
1504{
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001505 mAudioCommandThread->parametersCommand(ioHandle, keyValuePairs,
Dima Zavinfce7a472011-04-19 22:30:36 -07001506 delayMs);
1507}
1508
1509int AudioPolicyService::setStreamVolume(audio_stream_type_t stream,
1510 float volume,
1511 audio_io_handle_t output,
1512 int delayMs)
1513{
Glenn Kastenfff6d712012-01-12 16:38:12 -08001514 return (int)mAudioCommandThread->volumeCommand(stream, volume,
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001515 output, delayMs);
Dima Zavinfce7a472011-04-19 22:30:36 -07001516}
1517
Dima Zavinfce7a472011-04-19 22:30:36 -07001518int AudioPolicyService::setVoiceVolume(float volume, int delayMs)
1519{
1520 return (int)mAudioCommandThread->voiceVolumeCommand(volume, delayMs);
1521}
1522
Dima Zavinfce7a472011-04-19 22:30:36 -07001523extern "C" {
Eric Laurent2d388ec2014-03-07 13:25:54 -08001524audio_module_handle_t aps_load_hw_module(void *service __unused,
1525 const char *name);
1526audio_io_handle_t aps_open_output(void *service __unused,
Eric Laurenta4c5a552012-03-29 10:12:40 -07001527 audio_devices_t *pDevices,
1528 uint32_t *pSamplingRate,
1529 audio_format_t *pFormat,
1530 audio_channel_mask_t *pChannelMask,
1531 uint32_t *pLatencyMs,
Eric Laurent2d388ec2014-03-07 13:25:54 -08001532 audio_output_flags_t flags);
Eric Laurenta4c5a552012-03-29 10:12:40 -07001533
Eric Laurent2d388ec2014-03-07 13:25:54 -08001534audio_io_handle_t aps_open_output_on_module(void *service __unused,
Eric Laurenta4c5a552012-03-29 10:12:40 -07001535 audio_module_handle_t module,
1536 audio_devices_t *pDevices,
1537 uint32_t *pSamplingRate,
1538 audio_format_t *pFormat,
1539 audio_channel_mask_t *pChannelMask,
1540 uint32_t *pLatencyMs,
Richard Fitzgeraldad3af332013-03-25 16:54:37 +00001541 audio_output_flags_t flags,
Eric Laurent2d388ec2014-03-07 13:25:54 -08001542 const audio_offload_info_t *offloadInfo);
1543audio_io_handle_t aps_open_dup_output(void *service __unused,
Dima Zavinfce7a472011-04-19 22:30:36 -07001544 audio_io_handle_t output1,
Eric Laurent2d388ec2014-03-07 13:25:54 -08001545 audio_io_handle_t output2);
1546int aps_close_output(void *service __unused, audio_io_handle_t output);
1547int aps_suspend_output(void *service __unused, audio_io_handle_t output);
1548int aps_restore_output(void *service __unused, audio_io_handle_t output);
1549audio_io_handle_t aps_open_input(void *service __unused,
Eric Laurenta4c5a552012-03-29 10:12:40 -07001550 audio_devices_t *pDevices,
1551 uint32_t *pSamplingRate,
1552 audio_format_t *pFormat,
1553 audio_channel_mask_t *pChannelMask,
Eric Laurent2d388ec2014-03-07 13:25:54 -08001554 audio_in_acoustics_t acoustics __unused);
1555audio_io_handle_t aps_open_input_on_module(void *service __unused,
Eric Laurenta4c5a552012-03-29 10:12:40 -07001556 audio_module_handle_t module,
1557 audio_devices_t *pDevices,
1558 uint32_t *pSamplingRate,
1559 audio_format_t *pFormat,
Eric Laurent2d388ec2014-03-07 13:25:54 -08001560 audio_channel_mask_t *pChannelMask);
1561int aps_close_input(void *service __unused, audio_io_handle_t input);
1562int aps_invalidate_stream(void *service __unused, audio_stream_type_t stream);
Glenn Kastend848eb42016-03-08 13:42:11 -08001563int aps_move_effects(void *service __unused, audio_session_t session,
Dima Zavinfce7a472011-04-19 22:30:36 -07001564 audio_io_handle_t src_output,
Eric Laurent2d388ec2014-03-07 13:25:54 -08001565 audio_io_handle_t dst_output);
1566char * aps_get_parameters(void *service __unused, audio_io_handle_t io_handle,
1567 const char *keys);
1568void aps_set_parameters(void *service, audio_io_handle_t io_handle,
1569 const char *kv_pairs, int delay_ms);
1570int aps_set_stream_volume(void *service, audio_stream_type_t stream,
Dima Zavinfce7a472011-04-19 22:30:36 -07001571 float volume, audio_io_handle_t output,
Eric Laurent2d388ec2014-03-07 13:25:54 -08001572 int delay_ms);
Eric Laurent2d388ec2014-03-07 13:25:54 -08001573int aps_set_voice_volume(void *service, float volume, int delay_ms);
1574};
Dima Zavinfce7a472011-04-19 22:30:36 -07001575
Mikhail Naganov1b2a7942017-12-08 10:18:09 -08001576} // namespace android