blob: cbe6eb99c4d84a3ae03fe91d83311a219ad05966 [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>
Mathias Agopian65ab4712010-07-14 17:59:35 -070025#include <sys/time.h>
Mikhail Naganov959e2d02019-03-28 11:08:19 -070026
27#include <audio_utils/clock.h>
Mathias Agopian65ab4712010-07-14 17:59:35 -070028#include <binder/IServiceManager.h>
29#include <utils/Log.h>
30#include <cutils/properties.h>
31#include <binder/IPCThreadState.h>
Svet Ganovf4ddfef2018-01-16 07:37:58 -080032#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
Mikhail Naganov959e2d02019-03-28 11:08:19 -070051static const int kDumpLockTimeoutNs = 1 * NANOS_PER_SECOND;
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);
Hayden Gomes524159d2019-12-23 14:41:47 -080077
78 mSupportedSystemUsages = std::vector<audio_usage_t> {};
Eric Laurent8b1e80b2014-10-07 09:08:47 -070079 }
bryant_liuba2b4392014-06-11 16:49:30 +080080 // load audio processing modules
Eric Laurent8b1e80b2014-10-07 09:08:47 -070081 sp<AudioPolicyEffects>audioPolicyEffects = new AudioPolicyEffects();
82 {
83 Mutex::Autolock _l(mLock);
84 mAudioPolicyEffects = audioPolicyEffects;
85 }
Svet Ganovf4ddfef2018-01-16 07:37:58 -080086
87 mUidPolicy = new UidPolicy(this);
88 mUidPolicy->registerSelf();
Michael Groovercfd28302018-12-11 19:16:46 -080089
90 mSensorPrivacyPolicy = new SensorPrivacyPolicy(this);
91 mSensorPrivacyPolicy->registerSelf();
Mathias Agopian65ab4712010-07-14 17:59:35 -070092}
93
94AudioPolicyService::~AudioPolicyService()
95{
Mathias Agopian65ab4712010-07-14 17:59:35 -070096 mAudioCommandThread->exit();
Eric Laurent657ff612014-05-07 11:58:24 -070097 mOutputCommandThread->exit();
Eric Laurent7c7f10b2011-06-17 21:29:58 -070098
Eric Laurentf269b8e2014-06-09 20:01:29 -070099 destroyAudioPolicyManager(mAudioPolicyManager);
Eric Laurentdce54a12014-03-10 12:19:46 -0700100 delete mAudioPolicyClient;
Eric Laurentb52c1522014-05-20 11:27:36 -0700101
102 mNotificationClients.clear();
bryant_liuba2b4392014-06-11 16:49:30 +0800103 mAudioPolicyEffects.clear();
Svet Ganovf4ddfef2018-01-16 07:37:58 -0800104
105 mUidPolicy->unregisterSelf();
106 mUidPolicy.clear();
Michael Groovercfd28302018-12-11 19:16:46 -0800107
108 mSensorPrivacyPolicy->unregisterSelf();
109 mSensorPrivacyPolicy.clear();
Eric Laurentb52c1522014-05-20 11:27:36 -0700110}
111
112// A notification client is always registered by AudioSystem when the client process
113// connects to AudioPolicyService.
114void AudioPolicyService::registerClient(const sp<IAudioPolicyServiceClient>& client)
115{
Eric Laurent12590252015-08-21 18:40:20 -0700116 if (client == 0) {
117 ALOGW("%s got NULL client", __FUNCTION__);
118 return;
119 }
Eric Laurent0ebd5f92014-11-19 19:04:52 -0800120 Mutex::Autolock _l(mNotificationClientsLock);
Eric Laurentb52c1522014-05-20 11:27:36 -0700121
122 uid_t uid = IPCThreadState::self()->getCallingUid();
luochaojiang908c7d72018-06-21 14:58:04 +0800123 pid_t pid = IPCThreadState::self()->getCallingPid();
124 int64_t token = ((int64_t)uid<<32) | pid;
125
126 if (mNotificationClients.indexOfKey(token) < 0) {
Eric Laurentb52c1522014-05-20 11:27:36 -0700127 sp<NotificationClient> notificationClient = new NotificationClient(this,
128 client,
luochaojiang908c7d72018-06-21 14:58:04 +0800129 uid,
130 pid);
131 ALOGV("registerClient() client %p, uid %d pid %d", client.get(), uid, pid);
Eric Laurentb52c1522014-05-20 11:27:36 -0700132
luochaojiang908c7d72018-06-21 14:58:04 +0800133 mNotificationClients.add(token, notificationClient);
Eric Laurentb52c1522014-05-20 11:27:36 -0700134
Marco Nelissenf8880202014-11-14 07:58:25 -0800135 sp<IBinder> binder = IInterface::asBinder(client);
Eric Laurentb52c1522014-05-20 11:27:36 -0700136 binder->linkToDeath(notificationClient);
137 }
138}
139
Eric Laurente8726fe2015-06-26 09:39:24 -0700140void AudioPolicyService::setAudioPortCallbacksEnabled(bool enabled)
141{
142 Mutex::Autolock _l(mNotificationClientsLock);
143
144 uid_t uid = IPCThreadState::self()->getCallingUid();
luochaojiang908c7d72018-06-21 14:58:04 +0800145 pid_t pid = IPCThreadState::self()->getCallingPid();
146 int64_t token = ((int64_t)uid<<32) | pid;
147
148 if (mNotificationClients.indexOfKey(token) < 0) {
Eric Laurente8726fe2015-06-26 09:39:24 -0700149 return;
150 }
luochaojiang908c7d72018-06-21 14:58:04 +0800151 mNotificationClients.valueFor(token)->setAudioPortCallbacksEnabled(enabled);
Eric Laurente8726fe2015-06-26 09:39:24 -0700152}
153
François Gaffiecfe17322018-11-07 13:41:29 +0100154void AudioPolicyService::setAudioVolumeGroupCallbacksEnabled(bool enabled)
155{
156 Mutex::Autolock _l(mNotificationClientsLock);
157
158 uid_t uid = IPCThreadState::self()->getCallingUid();
159 pid_t pid = IPCThreadState::self()->getCallingPid();
160 int64_t token = ((int64_t)uid<<32) | pid;
161
162 if (mNotificationClients.indexOfKey(token) < 0) {
163 return;
164 }
165 mNotificationClients.valueFor(token)->setAudioVolumeGroupCallbacksEnabled(enabled);
166}
167
Eric Laurentb52c1522014-05-20 11:27:36 -0700168// removeNotificationClient() is called when the client process dies.
luochaojiang908c7d72018-06-21 14:58:04 +0800169void AudioPolicyService::removeNotificationClient(uid_t uid, pid_t pid)
Eric Laurentb52c1522014-05-20 11:27:36 -0700170{
Eric Laurent0ebd5f92014-11-19 19:04:52 -0800171 {
172 Mutex::Autolock _l(mNotificationClientsLock);
luochaojiang908c7d72018-06-21 14:58:04 +0800173 int64_t token = ((int64_t)uid<<32) | pid;
174 mNotificationClients.removeItem(token);
Eric Laurent0ebd5f92014-11-19 19:04:52 -0800175 }
Eric Laurent0ebd5f92014-11-19 19:04:52 -0800176 {
177 Mutex::Autolock _l(mLock);
luochaojiang908c7d72018-06-21 14:58:04 +0800178 bool hasSameUid = false;
179 for (size_t i = 0; i < mNotificationClients.size(); i++) {
180 if (mNotificationClients.valueAt(i)->uid() == uid) {
181 hasSameUid = true;
182 break;
183 }
184 }
185 if (mAudioPolicyManager && !hasSameUid) {
Eric Laurent10b71232018-04-13 18:14:44 -0700186 // called from binder death notification: no need to clear caller identity
Eric Laurent8c7e6da2015-04-21 17:37:00 -0700187 mAudioPolicyManager->releaseResourcesForUid(uid);
Eric Laurentb52c1522014-05-20 11:27:36 -0700188 }
Eric Laurent0ebd5f92014-11-19 19:04:52 -0800189 }
Eric Laurentb52c1522014-05-20 11:27:36 -0700190}
191
192void AudioPolicyService::onAudioPortListUpdate()
193{
194 mOutputCommandThread->updateAudioPortListCommand();
195}
196
197void AudioPolicyService::doOnAudioPortListUpdate()
198{
Eric Laurent0ebd5f92014-11-19 19:04:52 -0800199 Mutex::Autolock _l(mNotificationClientsLock);
Eric Laurentb52c1522014-05-20 11:27:36 -0700200 for (size_t i = 0; i < mNotificationClients.size(); i++) {
201 mNotificationClients.valueAt(i)->onAudioPortListUpdate();
202 }
203}
204
205void AudioPolicyService::onAudioPatchListUpdate()
206{
207 mOutputCommandThread->updateAudioPatchListCommand();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700208}
209
Eric Laurentb52c1522014-05-20 11:27:36 -0700210void AudioPolicyService::doOnAudioPatchListUpdate()
211{
Eric Laurent0ebd5f92014-11-19 19:04:52 -0800212 Mutex::Autolock _l(mNotificationClientsLock);
Eric Laurentb52c1522014-05-20 11:27:36 -0700213 for (size_t i = 0; i < mNotificationClients.size(); i++) {
214 mNotificationClients.valueAt(i)->onAudioPatchListUpdate();
215 }
216}
217
François Gaffiecfe17322018-11-07 13:41:29 +0100218void AudioPolicyService::onAudioVolumeGroupChanged(volume_group_t group, int flags)
219{
220 mOutputCommandThread->changeAudioVolumeGroupCommand(group, flags);
221}
222
223void AudioPolicyService::doOnAudioVolumeGroupChanged(volume_group_t group, int flags)
224{
225 Mutex::Autolock _l(mNotificationClientsLock);
226 for (size_t i = 0; i < mNotificationClients.size(); i++) {
227 mNotificationClients.valueAt(i)->onAudioVolumeGroupChanged(group, flags);
228 }
229}
230
Chih-Hung Hsiehe964d4e2016-08-09 14:31:32 -0700231void AudioPolicyService::onDynamicPolicyMixStateUpdate(const String8& regId, int32_t state)
Jean-Michel Trivide801052015-04-14 19:10:14 -0700232{
233 ALOGV("AudioPolicyService::onDynamicPolicyMixStateUpdate(%s, %d)",
234 regId.string(), state);
235 mOutputCommandThread->dynamicPolicyMixStateUpdateCommand(regId, state);
236}
237
Chih-Hung Hsiehe964d4e2016-08-09 14:31:32 -0700238void AudioPolicyService::doOnDynamicPolicyMixStateUpdate(const String8& regId, int32_t state)
Jean-Michel Trivide801052015-04-14 19:10:14 -0700239{
240 Mutex::Autolock _l(mNotificationClientsLock);
241 for (size_t i = 0; i < mNotificationClients.size(); i++) {
242 mNotificationClients.valueAt(i)->onDynamicPolicyMixStateUpdate(regId, state);
243 }
244}
245
Eric Laurenta9f86652018-11-28 17:23:11 -0800246void AudioPolicyService::onRecordingConfigurationUpdate(
247 int event,
248 const record_client_info_t *clientInfo,
249 const audio_config_base_t *clientConfig,
250 std::vector<effect_descriptor_t> clientEffects,
251 const audio_config_base_t *deviceConfig,
252 std::vector<effect_descriptor_t> effects,
253 audio_patch_handle_t patchHandle,
254 audio_source_t source)
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -0800255{
Jean-Michel Triviac4e4292016-12-22 11:39:31 -0800256 mOutputCommandThread->recordingConfigurationUpdateCommand(event, clientInfo,
Eric Laurenta9f86652018-11-28 17:23:11 -0800257 clientConfig, clientEffects, deviceConfig, effects, patchHandle, source);
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -0800258}
259
Eric Laurenta9f86652018-11-28 17:23:11 -0800260void AudioPolicyService::doOnRecordingConfigurationUpdate(
261 int event,
262 const record_client_info_t *clientInfo,
263 const audio_config_base_t *clientConfig,
264 std::vector<effect_descriptor_t> clientEffects,
265 const audio_config_base_t *deviceConfig,
266 std::vector<effect_descriptor_t> effects,
267 audio_patch_handle_t patchHandle,
268 audio_source_t source)
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -0800269{
270 Mutex::Autolock _l(mNotificationClientsLock);
271 for (size_t i = 0; i < mNotificationClients.size(); i++) {
Jean-Michel Triviac4e4292016-12-22 11:39:31 -0800272 mNotificationClients.valueAt(i)->onRecordingConfigurationUpdate(event, clientInfo,
Eric Laurenta9f86652018-11-28 17:23:11 -0800273 clientConfig, clientEffects, deviceConfig, effects, patchHandle, source);
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -0800274 }
275}
276
277status_t AudioPolicyService::clientCreateAudioPatch(const struct audio_patch *patch,
278 audio_patch_handle_t *handle,
279 int delayMs)
280{
281 return mAudioCommandThread->createAudioPatchCommand(patch, handle, delayMs);
282}
283
284status_t AudioPolicyService::clientReleaseAudioPatch(audio_patch_handle_t handle,
285 int delayMs)
286{
287 return mAudioCommandThread->releaseAudioPatchCommand(handle, delayMs);
288}
289
Eric Laurente1715a42014-05-20 11:30:42 -0700290status_t AudioPolicyService::clientSetAudioPortConfig(const struct audio_port_config *config,
291 int delayMs)
292{
293 return mAudioCommandThread->setAudioPortConfigCommand(config, delayMs);
294}
295
Eric Laurentb52c1522014-05-20 11:27:36 -0700296AudioPolicyService::NotificationClient::NotificationClient(const sp<AudioPolicyService>& service,
297 const sp<IAudioPolicyServiceClient>& client,
luochaojiang908c7d72018-06-21 14:58:04 +0800298 uid_t uid,
299 pid_t pid)
300 : mService(service), mUid(uid), mPid(pid), mAudioPolicyServiceClient(client),
François Gaffiecfe17322018-11-07 13:41:29 +0100301 mAudioPortCallbacksEnabled(false), mAudioVolumeGroupCallbacksEnabled(false)
Eric Laurentb52c1522014-05-20 11:27:36 -0700302{
303}
304
305AudioPolicyService::NotificationClient::~NotificationClient()
306{
307}
308
309void AudioPolicyService::NotificationClient::binderDied(const wp<IBinder>& who __unused)
310{
311 sp<NotificationClient> keep(this);
312 sp<AudioPolicyService> service = mService.promote();
313 if (service != 0) {
luochaojiang908c7d72018-06-21 14:58:04 +0800314 service->removeNotificationClient(mUid, mPid);
Eric Laurentb52c1522014-05-20 11:27:36 -0700315 }
316}
317
318void AudioPolicyService::NotificationClient::onAudioPortListUpdate()
319{
Eric Laurente8726fe2015-06-26 09:39:24 -0700320 if (mAudioPolicyServiceClient != 0 && mAudioPortCallbacksEnabled) {
Eric Laurentb52c1522014-05-20 11:27:36 -0700321 mAudioPolicyServiceClient->onAudioPortListUpdate();
322 }
323}
324
325void AudioPolicyService::NotificationClient::onAudioPatchListUpdate()
326{
Eric Laurente8726fe2015-06-26 09:39:24 -0700327 if (mAudioPolicyServiceClient != 0 && mAudioPortCallbacksEnabled) {
Eric Laurentb52c1522014-05-20 11:27:36 -0700328 mAudioPolicyServiceClient->onAudioPatchListUpdate();
329 }
330}
Eric Laurent57dae992011-07-24 13:36:09 -0700331
François Gaffiecfe17322018-11-07 13:41:29 +0100332void AudioPolicyService::NotificationClient::onAudioVolumeGroupChanged(volume_group_t group,
333 int flags)
334{
335 if (mAudioPolicyServiceClient != 0 && mAudioVolumeGroupCallbacksEnabled) {
336 mAudioPolicyServiceClient->onAudioVolumeGroupChanged(group, flags);
337 }
338}
339
340
Jean-Michel Trivide801052015-04-14 19:10:14 -0700341void AudioPolicyService::NotificationClient::onDynamicPolicyMixStateUpdate(
Chih-Hung Hsiehe964d4e2016-08-09 14:31:32 -0700342 const String8& regId, int32_t state)
Jean-Michel Trivide801052015-04-14 19:10:14 -0700343{
Andy Hung4ef19fa2018-05-15 19:35:29 -0700344 if (mAudioPolicyServiceClient != 0 && isServiceUid(mUid)) {
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -0800345 mAudioPolicyServiceClient->onDynamicPolicyMixStateUpdate(regId, state);
346 }
347}
348
349void AudioPolicyService::NotificationClient::onRecordingConfigurationUpdate(
Eric Laurenta9f86652018-11-28 17:23:11 -0800350 int event,
351 const record_client_info_t *clientInfo,
352 const audio_config_base_t *clientConfig,
353 std::vector<effect_descriptor_t> clientEffects,
354 const audio_config_base_t *deviceConfig,
355 std::vector<effect_descriptor_t> effects,
356 audio_patch_handle_t patchHandle,
357 audio_source_t source)
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -0800358{
Andy Hung4ef19fa2018-05-15 19:35:29 -0700359 if (mAudioPolicyServiceClient != 0 && isServiceUid(mUid)) {
Jean-Michel Triviac4e4292016-12-22 11:39:31 -0800360 mAudioPolicyServiceClient->onRecordingConfigurationUpdate(event, clientInfo,
Eric Laurenta9f86652018-11-28 17:23:11 -0800361 clientConfig, clientEffects, deviceConfig, effects, patchHandle, source);
Jean-Michel Trivide801052015-04-14 19:10:14 -0700362 }
363}
364
Eric Laurente8726fe2015-06-26 09:39:24 -0700365void AudioPolicyService::NotificationClient::setAudioPortCallbacksEnabled(bool enabled)
366{
367 mAudioPortCallbacksEnabled = enabled;
368}
369
François Gaffiecfe17322018-11-07 13:41:29 +0100370void AudioPolicyService::NotificationClient::setAudioVolumeGroupCallbacksEnabled(bool enabled)
371{
372 mAudioVolumeGroupCallbacksEnabled = enabled;
373}
Eric Laurente8726fe2015-06-26 09:39:24 -0700374
Mathias Agopian65ab4712010-07-14 17:59:35 -0700375void AudioPolicyService::binderDied(const wp<IBinder>& who) {
Glenn Kasten411e4472012-11-02 10:00:06 -0700376 ALOGW("binderDied() %p, calling pid %d", who.unsafe_get(),
Eric Laurentde070132010-07-13 04:45:46 -0700377 IPCThreadState::self()->getCallingPid());
Mathias Agopian65ab4712010-07-14 17:59:35 -0700378}
379
Mikhail Naganov959e2d02019-03-28 11:08:19 -0700380static bool dumpTryLock(Mutex& mutex)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700381{
Mikhail Naganov959e2d02019-03-28 11:08:19 -0700382 status_t err = mutex.timedLock(kDumpLockTimeoutNs);
383 return err == NO_ERROR;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700384}
385
386status_t AudioPolicyService::dumpInternals(int fd)
387{
388 const size_t SIZE = 256;
389 char buffer[SIZE];
390 String8 result;
391
Eric Laurentdce54a12014-03-10 12:19:46 -0700392 snprintf(buffer, SIZE, "AudioPolicyManager: %p\n", mAudioPolicyManager);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700393 result.append(buffer);
394 snprintf(buffer, SIZE, "Command Thread: %p\n", mAudioCommandThread.get());
395 result.append(buffer);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700396
Hayden Gomes524159d2019-12-23 14:41:47 -0800397 snprintf(buffer, SIZE, "Supported System Usages:\n");
398 result.append(buffer);
399 for (std::vector<audio_usage_t>::iterator it = mSupportedSystemUsages.begin();
400 it != mSupportedSystemUsages.end(); ++it) {
401 snprintf(buffer, SIZE, "\t%d\n", *it);
402 result.append(buffer);
403 }
404
Mathias Agopian65ab4712010-07-14 17:59:35 -0700405 write(fd, result.string(), result.size());
406 return NO_ERROR;
407}
408
Eric Laurente8c8b432018-10-17 10:08:02 -0700409void AudioPolicyService::updateUidStates()
Svet Ganovf4ddfef2018-01-16 07:37:58 -0800410{
Eric Laurente8c8b432018-10-17 10:08:02 -0700411 Mutex::Autolock _l(mLock);
412 updateUidStates_l();
413}
414
415void AudioPolicyService::updateUidStates_l()
416{
Eric Laurent4eb58f12018-12-07 16:41:02 -0800417// Go over all active clients and allow capture (does not force silence) in the
418// following cases:
Eric Laurent8ed73c12019-08-30 17:25:19 -0700419// The client is the assistant
Eric Laurent6ede98f2019-06-11 14:50:30 -0700420// AND an accessibility service is on TOP or a RTT call is active
Eric Laurent589171c2019-07-25 18:04:29 -0700421// AND the source is VOICE_RECOGNITION or HOTWORD
422// OR uses VOICE_RECOGNITION AND is on TOP
423// OR uses HOTWORD
Eric Laurent1ff16a72019-03-14 18:35:04 -0700424// AND there is no active privacy sensitive capture or call
425// OR client has CAPTURE_AUDIO_OUTPUT privileged permission
Eric Laurenta46bedb2018-12-07 18:01:26 -0800426// OR The client is an accessibility service
Eric Laurent589171c2019-07-25 18:04:29 -0700427// AND Is on TOP
428// AND the source is VOICE_RECOGNITION or HOTWORD
429// OR The assistant is not on TOP
430// AND there is no active privacy sensitive capture or call
431// OR client has CAPTURE_AUDIO_OUTPUT privileged permission
Eric Laurenta171e352019-05-07 13:04:45 -0700432// AND is on TOP
Eric Laurenta46bedb2018-12-07 18:01:26 -0800433// AND the source is VOICE_RECOGNITION or HOTWORD
Eric Laurent1ff16a72019-03-14 18:35:04 -0700434// OR the client source is virtual (remote submix, call audio TX or RX...)
Eric Laurent4e947da2019-10-17 15:24:06 -0700435// OR the client source is HOTWORD
436// AND is on TOP
437// OR all active clients are using HOTWORD source
438// AND no call is active
439// OR client has CAPTURE_AUDIO_OUTPUT privileged permission
Kohsuke Yatoha623a132020-03-24 20:10:26 -0700440// OR the client is the current InputMethodService
441// AND a RTT call is active AND the source is VOICE_RECOGNITION
Eric Laurenta171e352019-05-07 13:04:45 -0700442// OR Any client
Eric Laurenta46bedb2018-12-07 18:01:26 -0800443// AND The assistant is not on TOP
Eric Laurenta171e352019-05-07 13:04:45 -0700444// AND is on TOP or latest started
Eric Laurent1ff16a72019-03-14 18:35:04 -0700445// AND there is no active privacy sensitive capture or call
Eric Laurent589171c2019-07-25 18:04:29 -0700446// OR client has CAPTURE_AUDIO_OUTPUT privileged permission
Eric Laurent4eb58f12018-12-07 16:41:02 -0800447
Eric Laurent4e947da2019-10-17 15:24:06 -0700448
Eric Laurent4eb58f12018-12-07 16:41:02 -0800449 sp<AudioRecordClient> topActive;
450 sp<AudioRecordClient> latestActive;
Eric Laurentc21d5692020-02-25 10:24:36 -0800451 sp<AudioRecordClient> topSensitiveActive;
Eric Laurent4eb58f12018-12-07 16:41:02 -0800452 sp<AudioRecordClient> latestSensitiveActive;
Eric Laurent1ff16a72019-03-14 18:35:04 -0700453
Eric Laurenta46bedb2018-12-07 18:01:26 -0800454 nsecs_t topStartNs = 0;
455 nsecs_t latestStartNs = 0;
Eric Laurentc21d5692020-02-25 10:24:36 -0800456 nsecs_t topSensitiveStartNs = 0;
Eric Laurent4eb58f12018-12-07 16:41:02 -0800457 nsecs_t latestSensitiveStartNs = 0;
458 bool isA11yOnTop = mUidPolicy->isA11yOnTop();
459 bool isAssistantOnTop = false;
460 bool isSensitiveActive = false;
Eric Laurent1ff16a72019-03-14 18:35:04 -0700461 bool isInCall = mPhoneState == AUDIO_MODE_IN_CALL;
Eric Laurentc21d5692020-02-25 10:24:36 -0800462 bool isInCommunication = mPhoneState == AUDIO_MODE_IN_COMMUNICATION;
463 bool rttCallActive = (isInCall || isInCommunication)
Eric Laurent6ede98f2019-06-11 14:50:30 -0700464 && mUidPolicy->isRttEnabled();
Eric Laurent4e947da2019-10-17 15:24:06 -0700465 bool onlyHotwordActive = true;
Eric Laurent4eb58f12018-12-07 16:41:02 -0800466
Michael Groovercfd28302018-12-11 19:16:46 -0800467 // if Sensor Privacy is enabled then all recordings should be silenced.
468 if (mSensorPrivacyPolicy->isSensorPrivacyEnabled()) {
469 silenceAllRecordings_l();
470 return;
471 }
472
Eric Laurente8c8b432018-10-17 10:08:02 -0700473 for (size_t i =0; i < mAudioRecordClients.size(); i++) {
474 sp<AudioRecordClient> current = mAudioRecordClients[i];
Eric Laurent1ff16a72019-03-14 18:35:04 -0700475 if (!current->active) {
476 continue;
Eric Laurent4eb58f12018-12-07 16:41:02 -0800477 }
Eric Laurent1ff16a72019-03-14 18:35:04 -0700478
479 app_state_t appState = apmStatFromAmState(mUidPolicy->getUidState(current->uid));
480 // clients which app is in IDLE state are not eligible for top active or
481 // latest active
482 if (appState == APP_STATE_IDLE) {
483 continue;
484 }
485
Eric Laurent589171c2019-07-25 18:04:29 -0700486 bool isAccessibility = mUidPolicy->isA11yUid(current->uid);
Eric Laurentc21d5692020-02-25 10:24:36 -0800487 // Clients capturing for Accessibility services are not considered
488 // for top or latest active to avoid masking regular clients started before
489 if (!isAccessibility) {
490 bool isAssistant = mUidPolicy->isAssistantUid(current->uid);
491 bool isPrivacySensitive =
492 (current->attributes.flags & AUDIO_FLAG_CAPTURE_PRIVATE) != 0;
493 if (appState == APP_STATE_TOP) {
494 if (isPrivacySensitive) {
495 if (current->startTimeNs > topSensitiveStartNs) {
496 topSensitiveActive = current;
497 topSensitiveStartNs = current->startTimeNs;
498 }
499 } else {
500 if (current->startTimeNs > topStartNs) {
501 topActive = current;
502 topStartNs = current->startTimeNs;
503 }
504 }
505 if (isAssistant) {
506 isAssistantOnTop = true;
507 }
Eric Laurenta46bedb2018-12-07 18:01:26 -0800508 }
Eric Laurentc21d5692020-02-25 10:24:36 -0800509 // Clients capturing for HOTWORD are not considered
510 // for latest active to avoid masking regular clients started before
511 if (!(current->attributes.source == AUDIO_SOURCE_HOTWORD
512 || ((isA11yOnTop || rttCallActive) && isAssistant))) {
513 if (isPrivacySensitive) {
514 if (current->startTimeNs > latestSensitiveStartNs) {
515 latestSensitiveActive = current;
516 latestSensitiveStartNs = current->startTimeNs;
517 }
518 isSensitiveActive = true;
519 } else {
520 if (current->startTimeNs > latestStartNs) {
521 latestActive = current;
522 latestStartNs = current->startTimeNs;
523 }
524 }
Eric Laurent4eb58f12018-12-07 16:41:02 -0800525 }
526 }
Eric Laurent4e947da2019-10-17 15:24:06 -0700527 if (current->attributes.source != AUDIO_SOURCE_HOTWORD) {
528 onlyHotwordActive = false;
529 }
Eric Laurent4eb58f12018-12-07 16:41:02 -0800530 }
531
Eric Laurent1ff16a72019-03-14 18:35:04 -0700532 // if no active client with UI on Top, consider latest active as top
533 if (topActive == nullptr) {
534 topActive = latestActive;
Eric Laurentc21d5692020-02-25 10:24:36 -0800535 topStartNs = latestStartNs;
536 }
537 if (topSensitiveActive == nullptr) {
538 topSensitiveActive = latestSensitiveActive;
539 topSensitiveStartNs = latestSensitiveStartNs;
540 }
541
542 // If both privacy sensitive and regular capture are active:
543 // if the regular capture is privileged
544 // allow concurrency
545 // else
546 // favor the privacy sensitive case
547 if (topActive != nullptr && topSensitiveActive != nullptr
Ricardo Correa57a37692020-03-23 17:27:25 -0700548 && !topActive->canCaptureOutput) {
Eric Laurentc21d5692020-02-25 10:24:36 -0800549 topActive = nullptr;
Eric Laurent4eb58f12018-12-07 16:41:02 -0800550 }
551
552 for (size_t i =0; i < mAudioRecordClients.size(); i++) {
553 sp<AudioRecordClient> current = mAudioRecordClients[i];
Eric Laurent1ff16a72019-03-14 18:35:04 -0700554 if (!current->active) {
555 continue;
556 }
557
Eric Laurent4eb58f12018-12-07 16:41:02 -0800558 audio_source_t source = current->attributes.source;
Eric Laurent1ff16a72019-03-14 18:35:04 -0700559 bool isTopOrLatestActive = topActive == nullptr ? false : current->uid == topActive->uid;
Eric Laurentc21d5692020-02-25 10:24:36 -0800560 bool isTopOrLatestSensitive = topSensitiveActive == nullptr ?
561 false : current->uid == topSensitiveActive->uid;
562
563 auto canCaptureIfInCallOrCommunication = [&](const auto &recordClient) {
Ricardo Correa57a37692020-03-23 17:27:25 -0700564 bool canCaptureCall = recordClient->canCaptureOutput;
565 bool canCaptureCommunication = recordClient->canCaptureOutput
Eric Laurentc21d5692020-02-25 10:24:36 -0800566 || recordClient->uid == mPhoneStateOwnerUid
567 || isServiceUid(mPhoneStateOwnerUid);
568 return !(isInCall && !canCaptureCall)
569 && !(isInCommunication && !canCaptureCommunication);
570 };
Eric Laurent1ff16a72019-03-14 18:35:04 -0700571
572 // By default allow capture if:
573 // The assistant is not on TOP
Eric Laurenta171e352019-05-07 13:04:45 -0700574 // AND is on TOP or latest started
Eric Laurent1ff16a72019-03-14 18:35:04 -0700575 // AND there is no active privacy sensitive capture or call
576 // OR client has CAPTURE_AUDIO_OUTPUT privileged permission
577 bool allowCapture = !isAssistantOnTop
Eric Laurentc21d5692020-02-25 10:24:36 -0800578 && (isTopOrLatestActive || isTopOrLatestSensitive)
579 && !(isSensitiveActive
Ricardo Correa57a37692020-03-23 17:27:25 -0700580 && !(isTopOrLatestSensitive || current->canCaptureOutput))
Eric Laurentc21d5692020-02-25 10:24:36 -0800581 && canCaptureIfInCallOrCommunication(current);
Eric Laurent2dc962b2019-03-01 08:25:25 -0800582
583 if (isVirtualSource(source)) {
Eric Laurent1ff16a72019-03-14 18:35:04 -0700584 // Allow capture for virtual (remote submix, call audio TX or RX...) sources
585 allowCapture = true;
Eric Laurent2dc962b2019-03-01 08:25:25 -0800586 } else if (mUidPolicy->isAssistantUid(current->uid)) {
Eric Laurent1ff16a72019-03-14 18:35:04 -0700587 // For assistant allow capture if:
Eric Laurent6ede98f2019-06-11 14:50:30 -0700588 // An accessibility service is on TOP or a RTT call is active
Eric Laurent1ff16a72019-03-14 18:35:04 -0700589 // AND the source is VOICE_RECOGNITION or HOTWORD
Eric Laurenta171e352019-05-07 13:04:45 -0700590 // OR is on TOP AND uses VOICE_RECOGNITION
Eric Laurent1ff16a72019-03-14 18:35:04 -0700591 // OR uses HOTWORD
592 // AND there is no active privacy sensitive capture or call
593 // OR client has CAPTURE_AUDIO_OUTPUT privileged permission
Eric Laurent6ede98f2019-06-11 14:50:30 -0700594 if (isA11yOnTop || rttCallActive) {
Eric Laurent4eb58f12018-12-07 16:41:02 -0800595 if (source == AUDIO_SOURCE_HOTWORD || source == AUDIO_SOURCE_VOICE_RECOGNITION) {
Eric Laurent1ff16a72019-03-14 18:35:04 -0700596 allowCapture = true;
Eric Laurent4eb58f12018-12-07 16:41:02 -0800597 }
598 } else {
Eric Laurenta171e352019-05-07 13:04:45 -0700599 if (((isAssistantOnTop && source == AUDIO_SOURCE_VOICE_RECOGNITION) ||
Eric Laurentc21d5692020-02-25 10:24:36 -0800600 source == AUDIO_SOURCE_HOTWORD)
Ricardo Correa57a37692020-03-23 17:27:25 -0700601 && !(isSensitiveActive && !current->canCaptureOutput)
Eric Laurentc21d5692020-02-25 10:24:36 -0800602 && canCaptureIfInCallOrCommunication(current)) {
Eric Laurent1ff16a72019-03-14 18:35:04 -0700603 allowCapture = true;
Eric Laurent4eb58f12018-12-07 16:41:02 -0800604 }
605 }
606 } else if (mUidPolicy->isA11yUid(current->uid)) {
Eric Laurent1ff16a72019-03-14 18:35:04 -0700607 // For accessibility service allow capture if:
Eric Laurent47670c92019-08-28 16:59:05 -0700608 // The assistant is not on TOP
609 // AND there is no active privacy sensitive capture or call
Eric Laurent589171c2019-07-25 18:04:29 -0700610 // OR client has CAPTURE_AUDIO_OUTPUT privileged permission
Eric Laurent47670c92019-08-28 16:59:05 -0700611 // OR
612 // Is on TOP AND the source is VOICE_RECOGNITION or HOTWORD
613 if (!isAssistantOnTop
Ricardo Correa57a37692020-03-23 17:27:25 -0700614 && !(isSensitiveActive && !current->canCaptureOutput)
Eric Laurentc21d5692020-02-25 10:24:36 -0800615 && canCaptureIfInCallOrCommunication(current)) {
Eric Laurent47670c92019-08-28 16:59:05 -0700616 allowCapture = true;
617 }
Eric Laurent589171c2019-07-25 18:04:29 -0700618 if (isA11yOnTop) {
619 if (source == AUDIO_SOURCE_VOICE_RECOGNITION || source == AUDIO_SOURCE_HOTWORD) {
620 allowCapture = true;
621 }
Eric Laurent4eb58f12018-12-07 16:41:02 -0800622 }
Eric Laurent4e947da2019-10-17 15:24:06 -0700623 } else if (source == AUDIO_SOURCE_HOTWORD) {
624 // For HOTWORD source allow capture when not on TOP if:
625 // All active clients are using HOTWORD source
626 // AND no call is active
627 // OR client has CAPTURE_AUDIO_OUTPUT privileged permission
Eric Laurentc21d5692020-02-25 10:24:36 -0800628 if (onlyHotwordActive
629 && canCaptureIfInCallOrCommunication(current)) {
Eric Laurent4e947da2019-10-17 15:24:06 -0700630 allowCapture = true;
631 }
Kohsuke Yatoha623a132020-03-24 20:10:26 -0700632 } else if (mUidPolicy->isCurrentImeUid(current->uid)) {
633 // For current InputMethodService allow capture if:
634 // A RTT call is active AND the source is VOICE_RECOGNITION
635 if (rttCallActive && source == AUDIO_SOURCE_VOICE_RECOGNITION) {
636 allowCapture = true;
637 }
Eric Laurent4eb58f12018-12-07 16:41:02 -0800638 }
Eric Laurent5ada82e2019-08-29 17:53:54 -0700639 setAppState_l(current->portId,
Eric Laurent1ff16a72019-03-14 18:35:04 -0700640 allowCapture ? apmStatFromAmState(mUidPolicy->getUidState(current->uid)) :
641 APP_STATE_IDLE);
Eric Laurente8c8b432018-10-17 10:08:02 -0700642 }
643}
644
Michael Groovercfd28302018-12-11 19:16:46 -0800645void AudioPolicyService::silenceAllRecordings_l() {
646 for (size_t i = 0; i < mAudioRecordClients.size(); i++) {
647 sp<AudioRecordClient> current = mAudioRecordClients[i];
Eric Laurent1ff16a72019-03-14 18:35:04 -0700648 if (!isVirtualSource(current->attributes.source)) {
Eric Laurent5ada82e2019-08-29 17:53:54 -0700649 setAppState_l(current->portId, APP_STATE_IDLE);
Eric Laurent1ff16a72019-03-14 18:35:04 -0700650 }
Michael Groovercfd28302018-12-11 19:16:46 -0800651 }
652}
653
Eric Laurente8c8b432018-10-17 10:08:02 -0700654/* static */
655app_state_t AudioPolicyService::apmStatFromAmState(int amState) {
Eric Laurent1ff16a72019-03-14 18:35:04 -0700656
657 if (amState == ActivityManager::PROCESS_STATE_UNKNOWN) {
Eric Laurente8c8b432018-10-17 10:08:02 -0700658 return APP_STATE_IDLE;
Eric Laurent1ff16a72019-03-14 18:35:04 -0700659 } else if (amState <= ActivityManager::PROCESS_STATE_TOP) {
660 // include persistent services
661 return APP_STATE_TOP;
Eric Laurente8c8b432018-10-17 10:08:02 -0700662 }
663 return APP_STATE_FOREGROUND;
664}
665
Eric Laurent4eb58f12018-12-07 16:41:02 -0800666/* static */
Eric Laurent2dc962b2019-03-01 08:25:25 -0800667bool AudioPolicyService::isVirtualSource(audio_source_t source)
Eric Laurent4eb58f12018-12-07 16:41:02 -0800668{
669 switch (source) {
670 case AUDIO_SOURCE_VOICE_UPLINK:
671 case AUDIO_SOURCE_VOICE_DOWNLINK:
672 case AUDIO_SOURCE_VOICE_CALL:
Eric Laurent2dc962b2019-03-01 08:25:25 -0800673 case AUDIO_SOURCE_REMOTE_SUBMIX:
674 case AUDIO_SOURCE_FM_TUNER:
Eric Laurent4eb58f12018-12-07 16:41:02 -0800675 return true;
676 default:
677 break;
678 }
679 return false;
680}
681
Eric Laurent5ada82e2019-08-29 17:53:54 -0700682void AudioPolicyService::setAppState_l(audio_port_handle_t portId, app_state_t state)
Eric Laurente8c8b432018-10-17 10:08:02 -0700683{
684 AutoCallerClear acc;
685
686 if (mAudioPolicyManager) {
Eric Laurent5ada82e2019-08-29 17:53:54 -0700687 mAudioPolicyManager->setAppState(portId, state);
Mikhail Naganoveae73eb2018-04-03 16:57:36 -0700688 }
689 sp<IAudioFlinger> af = AudioSystem::get_audio_flinger();
690 if (af) {
Eric Laurentf32108e2018-10-04 17:22:04 -0700691 bool silenced = state == APP_STATE_IDLE;
Eric Laurent5ada82e2019-08-29 17:53:54 -0700692 af->setRecordSilenced(portId, silenced);
Mikhail Naganoveae73eb2018-04-03 16:57:36 -0700693 }
Svet Ganovf4ddfef2018-01-16 07:37:58 -0800694}
695
Glenn Kasten0f11b512014-01-31 16:18:54 -0800696status_t AudioPolicyService::dump(int fd, const Vector<String16>& args __unused)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700697{
Glenn Kasten44deb052012-02-05 18:09:08 -0800698 if (!dumpAllowed()) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700699 dumpPermissionDenial(fd);
700 } else {
Mikhail Naganov959e2d02019-03-28 11:08:19 -0700701 bool locked = dumpTryLock(mLock);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700702 if (!locked) {
703 String8 result(kDeadlockedString);
704 write(fd, result.string(), result.size());
705 }
706
707 dumpInternals(fd);
Glenn Kasten9d1f02d2012-02-08 17:47:58 -0800708 if (mAudioCommandThread != 0) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700709 mAudioCommandThread->dump(fd);
710 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700711
Eric Laurentdce54a12014-03-10 12:19:46 -0700712 if (mAudioPolicyManager) {
713 mAudioPolicyManager->dump(fd);
714 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700715
Kevin Rocard8be94972019-02-22 13:26:25 -0800716 mPackageManager.dump(fd);
717
Mathias Agopian65ab4712010-07-14 17:59:35 -0700718 if (locked) mLock.unlock();
719 }
720 return NO_ERROR;
721}
722
723status_t AudioPolicyService::dumpPermissionDenial(int fd)
724{
725 const size_t SIZE = 256;
726 char buffer[SIZE];
727 String8 result;
728 snprintf(buffer, SIZE, "Permission Denial: "
729 "can't dump AudioPolicyService from pid=%d, uid=%d\n",
730 IPCThreadState::self()->getCallingPid(),
731 IPCThreadState::self()->getCallingUid());
732 result.append(buffer);
733 write(fd, result.string(), result.size());
734 return NO_ERROR;
735}
736
737status_t AudioPolicyService::onTransact(
Svet Ganovf4ddfef2018-01-16 07:37:58 -0800738 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags) {
739 switch (code) {
740 case SHELL_COMMAND_TRANSACTION: {
741 int in = data.readFileDescriptor();
742 int out = data.readFileDescriptor();
743 int err = data.readFileDescriptor();
744 int argc = data.readInt32();
745 Vector<String16> args;
746 for (int i = 0; i < argc && data.dataAvail() > 0; i++) {
747 args.add(data.readString16());
748 }
749 sp<IBinder> unusedCallback;
750 sp<IResultReceiver> resultReceiver;
751 status_t status;
752 if ((status = data.readNullableStrongBinder(&unusedCallback)) != NO_ERROR) {
753 return status;
754 }
755 if ((status = data.readNullableStrongBinder(&resultReceiver)) != NO_ERROR) {
756 return status;
757 }
758 status = shellCommand(in, out, err, args);
759 if (resultReceiver != nullptr) {
760 resultReceiver->send(status);
761 }
762 return NO_ERROR;
763 }
764 }
765
Mathias Agopian65ab4712010-07-14 17:59:35 -0700766 return BnAudioPolicyService::onTransact(code, data, reply, flags);
767}
768
Svet Ganovf4ddfef2018-01-16 07:37:58 -0800769// ------------------- Shell command implementation -------------------
770
771// NOTE: This is a remote API - make sure all args are validated
772status_t AudioPolicyService::shellCommand(int in, int out, int err, Vector<String16>& args) {
773 if (!checkCallingPermission(sManageAudioPolicyPermission, nullptr, nullptr)) {
774 return PERMISSION_DENIED;
775 }
776 if (in == BAD_TYPE || out == BAD_TYPE || err == BAD_TYPE) {
777 return BAD_VALUE;
778 }
jovanakbe066e12019-09-02 11:54:39 -0700779 if (args.size() >= 3 && args[0] == String16("set-uid-state")) {
Svet Ganovf4ddfef2018-01-16 07:37:58 -0800780 return handleSetUidState(args, err);
jovanakbe066e12019-09-02 11:54:39 -0700781 } else if (args.size() >= 2 && args[0] == String16("reset-uid-state")) {
Svet Ganovf4ddfef2018-01-16 07:37:58 -0800782 return handleResetUidState(args, err);
jovanakbe066e12019-09-02 11:54:39 -0700783 } else if (args.size() >= 2 && args[0] == String16("get-uid-state")) {
Svet Ganovf4ddfef2018-01-16 07:37:58 -0800784 return handleGetUidState(args, out, err);
785 } else if (args.size() == 1 && args[0] == String16("help")) {
786 printHelp(out);
787 return NO_ERROR;
788 }
789 printHelp(err);
790 return BAD_VALUE;
791}
792
jovanakbe066e12019-09-02 11:54:39 -0700793static status_t getUidForPackage(String16 packageName, int userId, /*inout*/uid_t& uid, int err) {
794 if (userId < 0) {
795 ALOGE("Invalid user: %d", userId);
796 dprintf(err, "Invalid user: %d\n", userId);
Svet Ganovf4ddfef2018-01-16 07:37:58 -0800797 return BAD_VALUE;
798 }
jovanakbe066e12019-09-02 11:54:39 -0700799
800 PermissionController pc;
801 uid = pc.getPackageUid(packageName, 0);
802 if (uid <= 0) {
803 ALOGE("Unknown package: '%s'", String8(packageName).string());
804 dprintf(err, "Unknown package: '%s'\n", String8(packageName).string());
805 return BAD_VALUE;
806 }
807
808 uid = multiuser_get_uid(userId, uid);
809 return NO_ERROR;
810}
811
812status_t AudioPolicyService::handleSetUidState(Vector<String16>& args, int err) {
813 // Valid arg.size() is 3 or 5, args.size() is 5 with --user option.
814 if (!(args.size() == 3 || args.size() == 5)) {
815 printHelp(err);
816 return BAD_VALUE;
817 }
818
Svet Ganovf4ddfef2018-01-16 07:37:58 -0800819 bool active = false;
820 if (args[2] == String16("active")) {
821 active = true;
822 } else if ((args[2] != String16("idle"))) {
823 ALOGE("Expected active or idle but got: '%s'", String8(args[2]).string());
824 return BAD_VALUE;
825 }
jovanakbe066e12019-09-02 11:54:39 -0700826
827 int userId = 0;
828 if (args.size() >= 5 && args[3] == String16("--user")) {
829 userId = atoi(String8(args[4]));
830 }
831
832 uid_t uid;
833 if (getUidForPackage(args[1], userId, uid, err) == BAD_VALUE) {
834 return BAD_VALUE;
835 }
836
Svet Ganovf4ddfef2018-01-16 07:37:58 -0800837 mUidPolicy->addOverrideUid(uid, active);
838 return NO_ERROR;
839}
840
841status_t AudioPolicyService::handleResetUidState(Vector<String16>& args, int err) {
jovanakbe066e12019-09-02 11:54:39 -0700842 // Valid arg.size() is 2 or 4, args.size() is 4 with --user option.
843 if (!(args.size() == 2 || args.size() == 4)) {
844 printHelp(err);
Svet Ganovf4ddfef2018-01-16 07:37:58 -0800845 return BAD_VALUE;
846 }
jovanakbe066e12019-09-02 11:54:39 -0700847
848 int userId = 0;
849 if (args.size() >= 4 && args[2] == String16("--user")) {
850 userId = atoi(String8(args[3]));
851 }
852
853 uid_t uid;
854 if (getUidForPackage(args[1], userId, uid, err) == BAD_VALUE) {
855 return BAD_VALUE;
856 }
857
Svet Ganovf4ddfef2018-01-16 07:37:58 -0800858 mUidPolicy->removeOverrideUid(uid);
859 return NO_ERROR;
860}
861
862status_t AudioPolicyService::handleGetUidState(Vector<String16>& args, int out, int err) {
jovanakbe066e12019-09-02 11:54:39 -0700863 // Valid arg.size() is 2 or 4, args.size() is 4 with --user option.
864 if (!(args.size() == 2 || args.size() == 4)) {
865 printHelp(err);
Svet Ganovf4ddfef2018-01-16 07:37:58 -0800866 return BAD_VALUE;
867 }
jovanakbe066e12019-09-02 11:54:39 -0700868
869 int userId = 0;
870 if (args.size() >= 4 && args[2] == String16("--user")) {
871 userId = atoi(String8(args[3]));
872 }
873
874 uid_t uid;
875 if (getUidForPackage(args[1], userId, uid, err) == BAD_VALUE) {
876 return BAD_VALUE;
877 }
878
Svet Ganovf4ddfef2018-01-16 07:37:58 -0800879 if (mUidPolicy->isUidActive(uid)) {
880 return dprintf(out, "active\n");
881 } else {
882 return dprintf(out, "idle\n");
883 }
884}
885
886status_t AudioPolicyService::printHelp(int out) {
887 return dprintf(out, "Audio policy service commands:\n"
jovanakbe066e12019-09-02 11:54:39 -0700888 " get-uid-state <PACKAGE> [--user USER_ID] gets the uid state\n"
889 " set-uid-state <PACKAGE> <active|idle> [--user USER_ID] overrides the uid state\n"
890 " reset-uid-state <PACKAGE> [--user USER_ID] clears the uid state override\n"
Svet Ganovf4ddfef2018-01-16 07:37:58 -0800891 " help print this message\n");
892}
893
894// ----------- AudioPolicyService::UidPolicy implementation ----------
895
896void AudioPolicyService::UidPolicy::registerSelf() {
Steven Moreland2f348142019-07-02 15:59:07 -0700897 status_t res = mAm.linkToDeath(this);
898 mAm.registerUidObserver(this, ActivityManager::UID_OBSERVER_GONE
Svet Ganovf4ddfef2018-01-16 07:37:58 -0800899 | ActivityManager::UID_OBSERVER_IDLE
Eric Laurente8c8b432018-10-17 10:08:02 -0700900 | ActivityManager::UID_OBSERVER_ACTIVE
901 | ActivityManager::UID_OBSERVER_PROCSTATE,
Svet Ganovf4ddfef2018-01-16 07:37:58 -0800902 ActivityManager::PROCESS_STATE_UNKNOWN,
903 String16("audioserver"));
Mikhail Naganoveae73eb2018-04-03 16:57:36 -0700904 if (!res) {
905 Mutex::Autolock _l(mLock);
906 mObserverRegistered = true;
907 } else {
908 ALOGE("UidPolicy::registerSelf linkToDeath failed: %d", res);
Eric Laurent4eb58f12018-12-07 16:41:02 -0800909
Steven Moreland2f348142019-07-02 15:59:07 -0700910 mAm.unregisterUidObserver(this);
Mikhail Naganoveae73eb2018-04-03 16:57:36 -0700911 }
Svet Ganovf4ddfef2018-01-16 07:37:58 -0800912}
913
914void AudioPolicyService::UidPolicy::unregisterSelf() {
Steven Moreland2f348142019-07-02 15:59:07 -0700915 mAm.unlinkToDeath(this);
916 mAm.unregisterUidObserver(this);
Mikhail Naganoveae73eb2018-04-03 16:57:36 -0700917 Mutex::Autolock _l(mLock);
918 mObserverRegistered = false;
Svet Ganovf4ddfef2018-01-16 07:37:58 -0800919}
920
Mikhail Naganoveae73eb2018-04-03 16:57:36 -0700921void AudioPolicyService::UidPolicy::binderDied(__unused const wp<IBinder> &who) {
922 Mutex::Autolock _l(mLock);
923 mCachedUids.clear();
924 mObserverRegistered = false;
Svet Ganovf4ddfef2018-01-16 07:37:58 -0800925}
926
Eric Laurente8c8b432018-10-17 10:08:02 -0700927void AudioPolicyService::UidPolicy::checkRegistered() {
Mikhail Naganoveae73eb2018-04-03 16:57:36 -0700928 bool needToReregister = false;
929 {
930 Mutex::Autolock _l(mLock);
931 needToReregister = !mObserverRegistered;
Svet Ganovf4ddfef2018-01-16 07:37:58 -0800932 }
Mikhail Naganoveae73eb2018-04-03 16:57:36 -0700933 if (needToReregister) {
934 // Looks like ActivityManager has died previously, attempt to re-register.
935 registerSelf();
936 }
Eric Laurente8c8b432018-10-17 10:08:02 -0700937}
938
939bool AudioPolicyService::UidPolicy::isUidActive(uid_t uid) {
940 if (isServiceUid(uid)) return true;
941 checkRegistered();
Mikhail Naganoveae73eb2018-04-03 16:57:36 -0700942 {
943 Mutex::Autolock _l(mLock);
944 auto overrideIter = mOverrideUids.find(uid);
945 if (overrideIter != mOverrideUids.end()) {
Eric Laurente8c8b432018-10-17 10:08:02 -0700946 return overrideIter->second.first;
Mikhail Naganoveae73eb2018-04-03 16:57:36 -0700947 }
948 // In an absense of the ActivityManager, assume everything to be active.
949 if (!mObserverRegistered) return true;
950 auto cacheIter = mCachedUids.find(uid);
Mikhail Naganoveba668a2018-04-05 08:13:15 -0700951 if (cacheIter != mCachedUids.end()) {
Eric Laurente8c8b432018-10-17 10:08:02 -0700952 return cacheIter->second.first;
Mikhail Naganoveae73eb2018-04-03 16:57:36 -0700953 }
954 }
955 ActivityManager am;
Hui Yuffffffc2020-03-24 16:13:59 -0700956 bool active = am.isUidActiveOrForeground(uid, String16("audioserver"));
Mikhail Naganoveae73eb2018-04-03 16:57:36 -0700957 {
958 Mutex::Autolock _l(mLock);
Eric Laurente8c8b432018-10-17 10:08:02 -0700959 mCachedUids.insert(std::pair<uid_t,
960 std::pair<bool, int>>(uid, std::pair<bool, int>(active,
961 ActivityManager::PROCESS_STATE_UNKNOWN)));
Mikhail Naganoveae73eb2018-04-03 16:57:36 -0700962 }
963 return active;
Svet Ganovf4ddfef2018-01-16 07:37:58 -0800964}
965
Eric Laurente8c8b432018-10-17 10:08:02 -0700966int AudioPolicyService::UidPolicy::getUidState(uid_t uid) {
967 if (isServiceUid(uid)) {
968 return ActivityManager::PROCESS_STATE_TOP;
969 }
970 checkRegistered();
971 {
972 Mutex::Autolock _l(mLock);
973 auto overrideIter = mOverrideUids.find(uid);
974 if (overrideIter != mOverrideUids.end()) {
975 if (overrideIter->second.first) {
976 if (overrideIter->second.second != ActivityManager::PROCESS_STATE_UNKNOWN) {
977 return overrideIter->second.second;
978 } else {
979 auto cacheIter = mCachedUids.find(uid);
980 if (cacheIter != mCachedUids.end()) {
981 return cacheIter->second.second;
982 }
983 }
984 }
985 return ActivityManager::PROCESS_STATE_UNKNOWN;
986 }
987 // In an absense of the ActivityManager, assume everything to be active.
988 if (!mObserverRegistered) {
989 return ActivityManager::PROCESS_STATE_TOP;
990 }
991 auto cacheIter = mCachedUids.find(uid);
992 if (cacheIter != mCachedUids.end()) {
993 if (cacheIter->second.first) {
994 return cacheIter->second.second;
995 } else {
996 return ActivityManager::PROCESS_STATE_UNKNOWN;
997 }
998 }
999 }
1000 ActivityManager am;
Hui Yuffffffc2020-03-24 16:13:59 -07001001 bool active = am.isUidActiveOrForeground(uid, String16("audioserver"));
Eric Laurente8c8b432018-10-17 10:08:02 -07001002 int state = ActivityManager::PROCESS_STATE_UNKNOWN;
1003 if (active) {
1004 state = am.getUidProcessState(uid, String16("audioserver"));
1005 }
1006 {
1007 Mutex::Autolock _l(mLock);
1008 mCachedUids.insert(std::pair<uid_t,
1009 std::pair<bool, int>>(uid, std::pair<bool, int>(active, state)));
1010 }
Eric Laurent4eb58f12018-12-07 16:41:02 -08001011
Eric Laurente8c8b432018-10-17 10:08:02 -07001012 return state;
1013}
1014
Mikhail Naganoveae73eb2018-04-03 16:57:36 -07001015void AudioPolicyService::UidPolicy::onUidActive(uid_t uid) {
Eric Laurente8c8b432018-10-17 10:08:02 -07001016 updateUid(&mCachedUids, uid, true, ActivityManager::PROCESS_STATE_UNKNOWN, true);
Mikhail Naganoveae73eb2018-04-03 16:57:36 -07001017}
1018
1019void AudioPolicyService::UidPolicy::onUidGone(uid_t uid, __unused bool disabled) {
Eric Laurente8c8b432018-10-17 10:08:02 -07001020 updateUid(&mCachedUids, uid, false, ActivityManager::PROCESS_STATE_UNKNOWN, false);
Mikhail Naganoveae73eb2018-04-03 16:57:36 -07001021}
1022
1023void AudioPolicyService::UidPolicy::onUidIdle(uid_t uid, __unused bool disabled) {
Eric Laurente8c8b432018-10-17 10:08:02 -07001024 updateUid(&mCachedUids, uid, false, ActivityManager::PROCESS_STATE_UNKNOWN, true);
Mikhail Naganoveae73eb2018-04-03 16:57:36 -07001025}
1026
Eric Laurente8c8b432018-10-17 10:08:02 -07001027void AudioPolicyService::UidPolicy::onUidStateChanged(uid_t uid,
1028 int32_t procState,
Hui Yu13ad0eb2019-09-09 10:27:07 -07001029 int64_t procStateSeq __unused,
1030 int32_t capability __unused) {
Eric Laurente8c8b432018-10-17 10:08:02 -07001031 if (procState != ActivityManager::PROCESS_STATE_UNKNOWN) {
1032 updateUid(&mCachedUids, uid, true, procState, true);
Mikhail Naganoveae73eb2018-04-03 16:57:36 -07001033 }
1034}
1035
1036void AudioPolicyService::UidPolicy::updateOverrideUid(uid_t uid, bool active, bool insert) {
Eric Laurente8c8b432018-10-17 10:08:02 -07001037 updateUid(&mOverrideUids, uid, active, ActivityManager::PROCESS_STATE_UNKNOWN, insert);
1038}
1039
1040void AudioPolicyService::UidPolicy::notifyService() {
1041 sp<AudioPolicyService> service = mService.promote();
1042 if (service != nullptr) {
1043 service->updateUidStates();
Mikhail Naganoveae73eb2018-04-03 16:57:36 -07001044 }
1045}
1046
Eric Laurente8c8b432018-10-17 10:08:02 -07001047void AudioPolicyService::UidPolicy::updateUid(std::unordered_map<uid_t,
1048 std::pair<bool, int>> *uids,
1049 uid_t uid,
1050 bool active,
1051 int state,
1052 bool insert) {
1053 if (isServiceUid(uid)) {
1054 return;
1055 }
1056 bool wasActive = isUidActive(uid);
1057 int previousState = getUidState(uid);
Mikhail Naganoveae73eb2018-04-03 16:57:36 -07001058 {
1059 Mutex::Autolock _l(mLock);
Eric Laurente8c8b432018-10-17 10:08:02 -07001060 updateUidLocked(uids, uid, active, state, insert);
Mikhail Naganoveae73eb2018-04-03 16:57:36 -07001061 }
Eric Laurente8c8b432018-10-17 10:08:02 -07001062 if (wasActive != isUidActive(uid) || state != previousState) {
1063 notifyService();
1064 }
Mikhail Naganoveae73eb2018-04-03 16:57:36 -07001065}
1066
Eric Laurente8c8b432018-10-17 10:08:02 -07001067void AudioPolicyService::UidPolicy::updateUidLocked(std::unordered_map<uid_t,
1068 std::pair<bool, int>> *uids,
1069 uid_t uid,
1070 bool active,
1071 int state,
1072 bool insert) {
Mikhail Naganoveae73eb2018-04-03 16:57:36 -07001073 auto it = uids->find(uid);
1074 if (it != uids->end()) {
Mikhail Naganoveae73eb2018-04-03 16:57:36 -07001075 if (insert) {
Eric Laurente8c8b432018-10-17 10:08:02 -07001076 if (state == ActivityManager::PROCESS_STATE_UNKNOWN) {
1077 it->second.first = active;
1078 }
1079 if (it->second.first) {
1080 it->second.second = state;
1081 } else {
1082 it->second.second = ActivityManager::PROCESS_STATE_UNKNOWN;
1083 }
Mikhail Naganoveae73eb2018-04-03 16:57:36 -07001084 } else {
1085 uids->erase(it);
1086 }
Eric Laurente8c8b432018-10-17 10:08:02 -07001087 } else if (insert && (state == ActivityManager::PROCESS_STATE_UNKNOWN)) {
1088 uids->insert(std::pair<uid_t, std::pair<bool, int>>(uid,
1089 std::pair<bool, int>(active, state)));
Mikhail Naganoveae73eb2018-04-03 16:57:36 -07001090 }
Svet Ganovf4ddfef2018-01-16 07:37:58 -08001091}
Mathias Agopian65ab4712010-07-14 17:59:35 -07001092
Eric Laurent4eb58f12018-12-07 16:41:02 -08001093bool AudioPolicyService::UidPolicy::isA11yOnTop() {
1094 for (const auto &uid : mCachedUids) {
Eric Laurent47670c92019-08-28 16:59:05 -07001095 if (!isA11yUid(uid.first)) {
Eric Laurent4eb58f12018-12-07 16:41:02 -08001096 continue;
1097 }
Amith Yamasanibcbb3002019-01-23 13:53:33 -08001098 if (uid.second.second >= ActivityManager::PROCESS_STATE_TOP
1099 && uid.second.second <= ActivityManager::PROCESS_STATE_BOUND_FOREGROUND_SERVICE) {
Eric Laurent4eb58f12018-12-07 16:41:02 -08001100 return true;
1101 }
1102 }
1103 return false;
1104}
1105
Eric Laurentb78763e2018-10-17 10:08:02 -07001106bool AudioPolicyService::UidPolicy::isA11yUid(uid_t uid)
1107{
1108 std::vector<uid_t>::iterator it = find(mA11yUids.begin(), mA11yUids.end(), uid);
1109 return it != mA11yUids.end();
1110}
1111
Michael Groovercfd28302018-12-11 19:16:46 -08001112// ----------- AudioPolicyService::SensorPrivacyService implementation ----------
1113void AudioPolicyService::SensorPrivacyPolicy::registerSelf() {
1114 SensorPrivacyManager spm;
1115 mSensorPrivacyEnabled = spm.isSensorPrivacyEnabled();
1116 spm.addSensorPrivacyListener(this);
1117}
1118
1119void AudioPolicyService::SensorPrivacyPolicy::unregisterSelf() {
1120 SensorPrivacyManager spm;
1121 spm.removeSensorPrivacyListener(this);
1122}
1123
1124bool AudioPolicyService::SensorPrivacyPolicy::isSensorPrivacyEnabled() {
1125 return mSensorPrivacyEnabled;
1126}
1127
1128binder::Status AudioPolicyService::SensorPrivacyPolicy::onSensorPrivacyChanged(bool enabled) {
1129 mSensorPrivacyEnabled = enabled;
1130 sp<AudioPolicyService> service = mService.promote();
1131 if (service != nullptr) {
1132 service->updateUidStates();
1133 }
1134 return binder::Status::ok();
1135}
1136
Mathias Agopian65ab4712010-07-14 17:59:35 -07001137// ----------- AudioPolicyService::AudioCommandThread implementation ----------
1138
Eric Laurentbfb1b832013-01-07 09:53:42 -08001139AudioPolicyService::AudioCommandThread::AudioCommandThread(String8 name,
1140 const wp<AudioPolicyService>& service)
1141 : Thread(false), mName(name), mService(service)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001142{
Mathias Agopian65ab4712010-07-14 17:59:35 -07001143}
1144
1145
1146AudioPolicyService::AudioCommandThread::~AudioCommandThread()
1147{
Eric Laurentbfb1b832013-01-07 09:53:42 -08001148 if (!mAudioCommands.isEmpty()) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07001149 release_wake_lock(mName.string());
1150 }
1151 mAudioCommands.clear();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001152}
1153
1154void AudioPolicyService::AudioCommandThread::onFirstRef()
1155{
Eric Laurentbfb1b832013-01-07 09:53:42 -08001156 run(mName.string(), ANDROID_PRIORITY_AUDIO);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001157}
1158
1159bool AudioPolicyService::AudioCommandThread::threadLoop()
1160{
Eric Laurentd7eda8d2016-02-02 17:18:39 -08001161 nsecs_t waitTime = -1;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001162
1163 mLock.lock();
1164 while (!exitPending())
1165 {
Eric Laurent59a89232014-06-08 14:14:17 -07001166 sp<AudioPolicyService> svc;
1167 while (!mAudioCommands.isEmpty() && !exitPending()) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07001168 nsecs_t curTime = systemTime();
1169 // commands are sorted by increasing time stamp: execute them from index 0 and up
1170 if (mAudioCommands[0]->mTime <= curTime) {
Eric Laurent0ede8922014-05-09 18:04:42 -07001171 sp<AudioCommand> command = mAudioCommands[0];
Mathias Agopian65ab4712010-07-14 17:59:35 -07001172 mAudioCommands.removeAt(0);
Eric Laurent0ede8922014-05-09 18:04:42 -07001173 mLastCommand = command;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001174
1175 switch (command->mCommand) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07001176 case SET_VOLUME: {
Eric Laurent0ede8922014-05-09 18:04:42 -07001177 VolumeData *data = (VolumeData *)command->mParam.get();
Steve Block3856b092011-10-20 11:56:00 +01001178 ALOGV("AudioCommandThread() processing set volume stream %d, \
Eric Laurentde070132010-07-13 04:45:46 -07001179 volume %f, output %d", data->mStream, data->mVolume, data->mIO);
Andy Hungfe726a62018-09-27 15:17:25 -07001180 mLock.unlock();
Eric Laurentde070132010-07-13 04:45:46 -07001181 command->mStatus = AudioSystem::setStreamVolume(data->mStream,
1182 data->mVolume,
1183 data->mIO);
Andy Hungfe726a62018-09-27 15:17:25 -07001184 mLock.lock();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001185 }break;
1186 case SET_PARAMETERS: {
Eric Laurent0ede8922014-05-09 18:04:42 -07001187 ParametersData *data = (ParametersData *)command->mParam.get();
Glenn Kastene53b9ea2012-03-12 16:29:55 -07001188 ALOGV("AudioCommandThread() processing set parameters string %s, io %d",
1189 data->mKeyValuePairs.string(), data->mIO);
Andy Hungfe726a62018-09-27 15:17:25 -07001190 mLock.unlock();
Glenn Kastene53b9ea2012-03-12 16:29:55 -07001191 command->mStatus = AudioSystem::setParameters(data->mIO, data->mKeyValuePairs);
Andy Hungfe726a62018-09-27 15:17:25 -07001192 mLock.lock();
Glenn Kastene53b9ea2012-03-12 16:29:55 -07001193 }break;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001194 case SET_VOICE_VOLUME: {
Eric Laurent0ede8922014-05-09 18:04:42 -07001195 VoiceVolumeData *data = (VoiceVolumeData *)command->mParam.get();
Steve Block3856b092011-10-20 11:56:00 +01001196 ALOGV("AudioCommandThread() processing set voice volume volume %f",
Eric Laurentde070132010-07-13 04:45:46 -07001197 data->mVolume);
Andy Hungfe726a62018-09-27 15:17:25 -07001198 mLock.unlock();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001199 command->mStatus = AudioSystem::setVoiceVolume(data->mVolume);
Andy Hungfe726a62018-09-27 15:17:25 -07001200 mLock.lock();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001201 }break;
Eric Laurentbfb1b832013-01-07 09:53:42 -08001202 case STOP_OUTPUT: {
Eric Laurent0ede8922014-05-09 18:04:42 -07001203 StopOutputData *data = (StopOutputData *)command->mParam.get();
Eric Laurentd7fe0862018-07-14 16:48:01 -07001204 ALOGV("AudioCommandThread() processing stop output portId %d",
1205 data->mPortId);
Eric Laurent59a89232014-06-08 14:14:17 -07001206 svc = mService.promote();
Eric Laurentbfb1b832013-01-07 09:53:42 -08001207 if (svc == 0) {
1208 break;
1209 }
1210 mLock.unlock();
Eric Laurentd7fe0862018-07-14 16:48:01 -07001211 svc->doStopOutput(data->mPortId);
Eric Laurentbfb1b832013-01-07 09:53:42 -08001212 mLock.lock();
Eric Laurentbfb1b832013-01-07 09:53:42 -08001213 }break;
1214 case RELEASE_OUTPUT: {
Eric Laurent0ede8922014-05-09 18:04:42 -07001215 ReleaseOutputData *data = (ReleaseOutputData *)command->mParam.get();
Eric Laurentd7fe0862018-07-14 16:48:01 -07001216 ALOGV("AudioCommandThread() processing release output portId %d",
1217 data->mPortId);
Eric Laurent59a89232014-06-08 14:14:17 -07001218 svc = mService.promote();
Eric Laurentbfb1b832013-01-07 09:53:42 -08001219 if (svc == 0) {
1220 break;
1221 }
1222 mLock.unlock();
Eric Laurentd7fe0862018-07-14 16:48:01 -07001223 svc->doReleaseOutput(data->mPortId);
Eric Laurentbfb1b832013-01-07 09:53:42 -08001224 mLock.lock();
Eric Laurentbfb1b832013-01-07 09:53:42 -08001225 }break;
Eric Laurent951f4552014-05-20 10:48:17 -07001226 case CREATE_AUDIO_PATCH: {
1227 CreateAudioPatchData *data = (CreateAudioPatchData *)command->mParam.get();
1228 ALOGV("AudioCommandThread() processing create audio patch");
1229 sp<IAudioFlinger> af = AudioSystem::get_audio_flinger();
1230 if (af == 0) {
1231 command->mStatus = PERMISSION_DENIED;
1232 } else {
Andy Hungfe726a62018-09-27 15:17:25 -07001233 mLock.unlock();
Eric Laurent951f4552014-05-20 10:48:17 -07001234 command->mStatus = af->createAudioPatch(&data->mPatch, &data->mHandle);
Andy Hungfe726a62018-09-27 15:17:25 -07001235 mLock.lock();
Eric Laurent951f4552014-05-20 10:48:17 -07001236 }
1237 } break;
1238 case RELEASE_AUDIO_PATCH: {
1239 ReleaseAudioPatchData *data = (ReleaseAudioPatchData *)command->mParam.get();
1240 ALOGV("AudioCommandThread() processing release audio patch");
1241 sp<IAudioFlinger> af = AudioSystem::get_audio_flinger();
1242 if (af == 0) {
1243 command->mStatus = PERMISSION_DENIED;
1244 } else {
Andy Hungfe726a62018-09-27 15:17:25 -07001245 mLock.unlock();
Eric Laurent951f4552014-05-20 10:48:17 -07001246 command->mStatus = af->releaseAudioPatch(data->mHandle);
Andy Hungfe726a62018-09-27 15:17:25 -07001247 mLock.lock();
Eric Laurent951f4552014-05-20 10:48:17 -07001248 }
1249 } break;
Eric Laurentb52c1522014-05-20 11:27:36 -07001250 case UPDATE_AUDIOPORT_LIST: {
1251 ALOGV("AudioCommandThread() processing update audio port list");
Eric Laurent59a89232014-06-08 14:14:17 -07001252 svc = mService.promote();
Eric Laurentb52c1522014-05-20 11:27:36 -07001253 if (svc == 0) {
1254 break;
1255 }
1256 mLock.unlock();
1257 svc->doOnAudioPortListUpdate();
1258 mLock.lock();
1259 }break;
1260 case UPDATE_AUDIOPATCH_LIST: {
1261 ALOGV("AudioCommandThread() processing update audio patch list");
Eric Laurent59a89232014-06-08 14:14:17 -07001262 svc = mService.promote();
Eric Laurentb52c1522014-05-20 11:27:36 -07001263 if (svc == 0) {
1264 break;
1265 }
1266 mLock.unlock();
1267 svc->doOnAudioPatchListUpdate();
1268 mLock.lock();
1269 }break;
François Gaffiecfe17322018-11-07 13:41:29 +01001270 case CHANGED_AUDIOVOLUMEGROUP: {
1271 AudioVolumeGroupData *data =
1272 static_cast<AudioVolumeGroupData *>(command->mParam.get());
1273 ALOGV("AudioCommandThread() processing update audio volume group");
1274 svc = mService.promote();
1275 if (svc == 0) {
1276 break;
1277 }
1278 mLock.unlock();
1279 svc->doOnAudioVolumeGroupChanged(data->mGroup, data->mFlags);
1280 mLock.lock();
1281 }break;
Eric Laurente1715a42014-05-20 11:30:42 -07001282 case SET_AUDIOPORT_CONFIG: {
1283 SetAudioPortConfigData *data = (SetAudioPortConfigData *)command->mParam.get();
1284 ALOGV("AudioCommandThread() processing set port config");
1285 sp<IAudioFlinger> af = AudioSystem::get_audio_flinger();
1286 if (af == 0) {
1287 command->mStatus = PERMISSION_DENIED;
1288 } else {
Andy Hungfe726a62018-09-27 15:17:25 -07001289 mLock.unlock();
Eric Laurente1715a42014-05-20 11:30:42 -07001290 command->mStatus = af->setAudioPortConfig(&data->mConfig);
Andy Hungfe726a62018-09-27 15:17:25 -07001291 mLock.lock();
Eric Laurente1715a42014-05-20 11:30:42 -07001292 }
1293 } break;
Jean-Michel Trivide801052015-04-14 19:10:14 -07001294 case DYN_POLICY_MIX_STATE_UPDATE: {
1295 DynPolicyMixStateUpdateData *data =
1296 (DynPolicyMixStateUpdateData *)command->mParam.get();
Jean-Michel Trivide801052015-04-14 19:10:14 -07001297 ALOGV("AudioCommandThread() processing dyn policy mix state update %s %d",
1298 data->mRegId.string(), data->mState);
1299 svc = mService.promote();
1300 if (svc == 0) {
1301 break;
1302 }
1303 mLock.unlock();
1304 svc->doOnDynamicPolicyMixStateUpdate(data->mRegId, data->mState);
1305 mLock.lock();
1306 } break;
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -08001307 case RECORDING_CONFIGURATION_UPDATE: {
1308 RecordingConfigurationUpdateData *data =
1309 (RecordingConfigurationUpdateData *)command->mParam.get();
1310 ALOGV("AudioCommandThread() processing recording configuration update");
1311 svc = mService.promote();
1312 if (svc == 0) {
1313 break;
1314 }
1315 mLock.unlock();
Jean-Michel Triviac4e4292016-12-22 11:39:31 -08001316 svc->doOnRecordingConfigurationUpdate(data->mEvent, &data->mClientInfo,
Eric Laurenta9f86652018-11-28 17:23:11 -08001317 &data->mClientConfig, data->mClientEffects,
1318 &data->mDeviceConfig, data->mEffects,
1319 data->mPatchHandle, data->mSource);
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -08001320 mLock.lock();
1321 } break;
Eric Laurentb20cf7d2019-04-05 19:37:34 -07001322 case SET_EFFECT_SUSPENDED: {
1323 SetEffectSuspendedData *data = (SetEffectSuspendedData *)command->mParam.get();
1324 ALOGV("AudioCommandThread() processing set effect suspended");
1325 sp<IAudioFlinger> af = AudioSystem::get_audio_flinger();
1326 if (af != 0) {
1327 mLock.unlock();
1328 af->setEffectSuspended(data->mEffectId, data->mSessionId, data->mSuspended);
1329 mLock.lock();
1330 }
1331 } break;
Mikhail Naganov88b30d22020-03-09 19:43:13 +00001332 case AUDIO_MODULES_UPDATE: {
1333 ALOGV("AudioCommandThread() processing audio modules update");
1334 svc = mService.promote();
1335 if (svc == 0) {
1336 break;
1337 }
1338 mLock.unlock();
1339 svc->doOnNewAudioModulesAvailable();
1340 mLock.lock();
1341 } break;
Eric Laurentb20cf7d2019-04-05 19:37:34 -07001342
Mathias Agopian65ab4712010-07-14 17:59:35 -07001343 default:
Steve Block5ff1dd52012-01-05 23:22:43 +00001344 ALOGW("AudioCommandThread() unknown command %d", command->mCommand);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001345 }
Eric Laurent0ede8922014-05-09 18:04:42 -07001346 {
1347 Mutex::Autolock _l(command->mLock);
1348 if (command->mWaitStatus) {
1349 command->mWaitStatus = false;
1350 command->mCond.signal();
1351 }
1352 }
Eric Laurentd7eda8d2016-02-02 17:18:39 -08001353 waitTime = -1;
Zach Janga754b4f2015-10-27 01:29:34 +00001354 // release mLock before releasing strong reference on the service as
1355 // AudioPolicyService destructor calls AudioCommandThread::exit() which
1356 // acquires mLock.
1357 mLock.unlock();
1358 svc.clear();
1359 mLock.lock();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001360 } else {
1361 waitTime = mAudioCommands[0]->mTime - curTime;
1362 break;
1363 }
1364 }
Zach Janga754b4f2015-10-27 01:29:34 +00001365
1366 // release delayed commands wake lock if the queue is empty
1367 if (mAudioCommands.isEmpty()) {
Ricardo Garcia05f2fdc2014-07-24 15:48:24 -07001368 release_wake_lock(mName.string());
Zach Janga754b4f2015-10-27 01:29:34 +00001369 }
1370
1371 // At this stage we have either an empty command queue or the first command in the queue
1372 // has a finite delay. So unless we are exiting it is safe to wait.
1373 if (!exitPending()) {
Eric Laurent59a89232014-06-08 14:14:17 -07001374 ALOGV("AudioCommandThread() going to sleep");
Eric Laurentd7eda8d2016-02-02 17:18:39 -08001375 if (waitTime == -1) {
1376 mWaitWorkCV.wait(mLock);
1377 } else {
1378 mWaitWorkCV.waitRelative(mLock, waitTime);
1379 }
Eric Laurent59a89232014-06-08 14:14:17 -07001380 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001381 }
Ricardo Garcia05f2fdc2014-07-24 15:48:24 -07001382 // release delayed commands wake lock before quitting
1383 if (!mAudioCommands.isEmpty()) {
1384 release_wake_lock(mName.string());
1385 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001386 mLock.unlock();
1387 return false;
1388}
1389
1390status_t AudioPolicyService::AudioCommandThread::dump(int fd)
1391{
1392 const size_t SIZE = 256;
1393 char buffer[SIZE];
1394 String8 result;
1395
1396 snprintf(buffer, SIZE, "AudioCommandThread %p Dump\n", this);
1397 result.append(buffer);
1398 write(fd, result.string(), result.size());
1399
Mikhail Naganov959e2d02019-03-28 11:08:19 -07001400 bool locked = dumpTryLock(mLock);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001401 if (!locked) {
1402 String8 result2(kCmdDeadlockedString);
1403 write(fd, result2.string(), result2.size());
1404 }
1405
1406 snprintf(buffer, SIZE, "- Commands:\n");
1407 result = String8(buffer);
1408 result.append(" Command Time Wait pParam\n");
Glenn Kasten8d6a2442012-02-08 14:04:28 -08001409 for (size_t i = 0; i < mAudioCommands.size(); i++) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07001410 mAudioCommands[i]->dump(buffer, SIZE);
1411 result.append(buffer);
1412 }
1413 result.append(" Last Command\n");
Eric Laurent0ede8922014-05-09 18:04:42 -07001414 if (mLastCommand != 0) {
1415 mLastCommand->dump(buffer, SIZE);
1416 result.append(buffer);
1417 } else {
1418 result.append(" none\n");
1419 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001420
1421 write(fd, result.string(), result.size());
1422
1423 if (locked) mLock.unlock();
1424
1425 return NO_ERROR;
1426}
1427
Glenn Kastenfff6d712012-01-12 16:38:12 -08001428status_t AudioPolicyService::AudioCommandThread::volumeCommand(audio_stream_type_t stream,
Eric Laurentde070132010-07-13 04:45:46 -07001429 float volume,
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001430 audio_io_handle_t output,
Eric Laurentde070132010-07-13 04:45:46 -07001431 int delayMs)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001432{
Eric Laurent0ede8922014-05-09 18:04:42 -07001433 sp<AudioCommand> command = new AudioCommand();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001434 command->mCommand = SET_VOLUME;
Eric Laurent0ede8922014-05-09 18:04:42 -07001435 sp<VolumeData> data = new VolumeData();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001436 data->mStream = stream;
1437 data->mVolume = volume;
1438 data->mIO = output;
1439 command->mParam = data;
Eric Laurent0ede8922014-05-09 18:04:42 -07001440 command->mWaitStatus = true;
Steve Block3856b092011-10-20 11:56:00 +01001441 ALOGV("AudioCommandThread() adding set volume stream %d, volume %f, output %d",
Eric Laurentde070132010-07-13 04:45:46 -07001442 stream, volume, output);
Eric Laurent0ede8922014-05-09 18:04:42 -07001443 return sendCommand(command, delayMs);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001444}
1445
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001446status_t AudioPolicyService::AudioCommandThread::parametersCommand(audio_io_handle_t ioHandle,
Dima Zavinfce7a472011-04-19 22:30:36 -07001447 const char *keyValuePairs,
Eric Laurentde070132010-07-13 04:45:46 -07001448 int delayMs)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001449{
Eric Laurent0ede8922014-05-09 18:04:42 -07001450 sp<AudioCommand> command = new AudioCommand();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001451 command->mCommand = SET_PARAMETERS;
Eric Laurent0ede8922014-05-09 18:04:42 -07001452 sp<ParametersData> data = new ParametersData();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001453 data->mIO = ioHandle;
Dima Zavinfce7a472011-04-19 22:30:36 -07001454 data->mKeyValuePairs = String8(keyValuePairs);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001455 command->mParam = data;
Eric Laurent0ede8922014-05-09 18:04:42 -07001456 command->mWaitStatus = true;
Steve Block3856b092011-10-20 11:56:00 +01001457 ALOGV("AudioCommandThread() adding set parameter string %s, io %d ,delay %d",
Dima Zavinfce7a472011-04-19 22:30:36 -07001458 keyValuePairs, ioHandle, delayMs);
Eric Laurent0ede8922014-05-09 18:04:42 -07001459 return sendCommand(command, delayMs);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001460}
1461
1462status_t AudioPolicyService::AudioCommandThread::voiceVolumeCommand(float volume, int delayMs)
1463{
Eric Laurent0ede8922014-05-09 18:04:42 -07001464 sp<AudioCommand> command = new AudioCommand();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001465 command->mCommand = SET_VOICE_VOLUME;
Eric Laurent0ede8922014-05-09 18:04:42 -07001466 sp<VoiceVolumeData> data = new VoiceVolumeData();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001467 data->mVolume = volume;
1468 command->mParam = data;
Eric Laurent0ede8922014-05-09 18:04:42 -07001469 command->mWaitStatus = true;
Steve Block3856b092011-10-20 11:56:00 +01001470 ALOGV("AudioCommandThread() adding set voice volume volume %f", volume);
Eric Laurent0ede8922014-05-09 18:04:42 -07001471 return sendCommand(command, delayMs);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001472}
1473
Eric Laurentb20cf7d2019-04-05 19:37:34 -07001474void AudioPolicyService::AudioCommandThread::setEffectSuspendedCommand(int effectId,
1475 audio_session_t sessionId,
1476 bool suspended)
1477{
1478 sp<AudioCommand> command = new AudioCommand();
1479 command->mCommand = SET_EFFECT_SUSPENDED;
1480 sp<SetEffectSuspendedData> data = new SetEffectSuspendedData();
1481 data->mEffectId = effectId;
1482 data->mSessionId = sessionId;
1483 data->mSuspended = suspended;
1484 command->mParam = data;
1485 ALOGV("AudioCommandThread() adding set suspended effectId %d sessionId %d suspended %d",
1486 effectId, sessionId, suspended);
1487 sendCommand(command);
1488}
1489
1490
Eric Laurentd7fe0862018-07-14 16:48:01 -07001491void AudioPolicyService::AudioCommandThread::stopOutputCommand(audio_port_handle_t portId)
Eric Laurentbfb1b832013-01-07 09:53:42 -08001492{
Eric Laurent0ede8922014-05-09 18:04:42 -07001493 sp<AudioCommand> command = new AudioCommand();
Eric Laurentbfb1b832013-01-07 09:53:42 -08001494 command->mCommand = STOP_OUTPUT;
Eric Laurent0ede8922014-05-09 18:04:42 -07001495 sp<StopOutputData> data = new StopOutputData();
Eric Laurentd7fe0862018-07-14 16:48:01 -07001496 data->mPortId = portId;
Jesper Tragardh48412dc2014-03-24 14:12:43 +01001497 command->mParam = data;
Eric Laurentd7fe0862018-07-14 16:48:01 -07001498 ALOGV("AudioCommandThread() adding stop output portId %d", portId);
Eric Laurent0ede8922014-05-09 18:04:42 -07001499 sendCommand(command);
Eric Laurentbfb1b832013-01-07 09:53:42 -08001500}
1501
Eric Laurentd7fe0862018-07-14 16:48:01 -07001502void AudioPolicyService::AudioCommandThread::releaseOutputCommand(audio_port_handle_t portId)
Eric Laurentbfb1b832013-01-07 09:53:42 -08001503{
Eric Laurent0ede8922014-05-09 18:04:42 -07001504 sp<AudioCommand> command = new AudioCommand();
Eric Laurentbfb1b832013-01-07 09:53:42 -08001505 command->mCommand = RELEASE_OUTPUT;
Eric Laurent0ede8922014-05-09 18:04:42 -07001506 sp<ReleaseOutputData> data = new ReleaseOutputData();
Eric Laurentd7fe0862018-07-14 16:48:01 -07001507 data->mPortId = portId;
Jesper Tragardh48412dc2014-03-24 14:12:43 +01001508 command->mParam = data;
Eric Laurentd7fe0862018-07-14 16:48:01 -07001509 ALOGV("AudioCommandThread() adding release output portId %d", portId);
Eric Laurent0ede8922014-05-09 18:04:42 -07001510 sendCommand(command);
1511}
1512
Eric Laurent951f4552014-05-20 10:48:17 -07001513status_t AudioPolicyService::AudioCommandThread::createAudioPatchCommand(
1514 const struct audio_patch *patch,
1515 audio_patch_handle_t *handle,
1516 int delayMs)
1517{
1518 status_t status = NO_ERROR;
1519
1520 sp<AudioCommand> command = new AudioCommand();
1521 command->mCommand = CREATE_AUDIO_PATCH;
1522 CreateAudioPatchData *data = new CreateAudioPatchData();
1523 data->mPatch = *patch;
1524 data->mHandle = *handle;
1525 command->mParam = data;
1526 command->mWaitStatus = true;
1527 ALOGV("AudioCommandThread() adding create patch delay %d", delayMs);
1528 status = sendCommand(command, delayMs);
1529 if (status == NO_ERROR) {
1530 *handle = data->mHandle;
1531 }
1532 return status;
1533}
1534
1535status_t AudioPolicyService::AudioCommandThread::releaseAudioPatchCommand(audio_patch_handle_t handle,
1536 int delayMs)
1537{
1538 sp<AudioCommand> command = new AudioCommand();
1539 command->mCommand = RELEASE_AUDIO_PATCH;
1540 ReleaseAudioPatchData *data = new ReleaseAudioPatchData();
1541 data->mHandle = handle;
1542 command->mParam = data;
1543 command->mWaitStatus = true;
1544 ALOGV("AudioCommandThread() adding release patch delay %d", delayMs);
1545 return sendCommand(command, delayMs);
1546}
1547
Eric Laurentb52c1522014-05-20 11:27:36 -07001548void AudioPolicyService::AudioCommandThread::updateAudioPortListCommand()
1549{
1550 sp<AudioCommand> command = new AudioCommand();
1551 command->mCommand = UPDATE_AUDIOPORT_LIST;
1552 ALOGV("AudioCommandThread() adding update audio port list");
1553 sendCommand(command);
1554}
1555
1556void AudioPolicyService::AudioCommandThread::updateAudioPatchListCommand()
1557{
1558 sp<AudioCommand>command = new AudioCommand();
1559 command->mCommand = UPDATE_AUDIOPATCH_LIST;
1560 ALOGV("AudioCommandThread() adding update audio patch list");
1561 sendCommand(command);
1562}
1563
François Gaffiecfe17322018-11-07 13:41:29 +01001564void AudioPolicyService::AudioCommandThread::changeAudioVolumeGroupCommand(volume_group_t group,
1565 int flags)
1566{
1567 sp<AudioCommand>command = new AudioCommand();
1568 command->mCommand = CHANGED_AUDIOVOLUMEGROUP;
1569 AudioVolumeGroupData *data= new AudioVolumeGroupData();
1570 data->mGroup = group;
1571 data->mFlags = flags;
1572 command->mParam = data;
1573 ALOGV("AudioCommandThread() adding audio volume group changed");
1574 sendCommand(command);
1575}
1576
Eric Laurente1715a42014-05-20 11:30:42 -07001577status_t AudioPolicyService::AudioCommandThread::setAudioPortConfigCommand(
1578 const struct audio_port_config *config, int delayMs)
1579{
1580 sp<AudioCommand> command = new AudioCommand();
1581 command->mCommand = SET_AUDIOPORT_CONFIG;
1582 SetAudioPortConfigData *data = new SetAudioPortConfigData();
1583 data->mConfig = *config;
1584 command->mParam = data;
1585 command->mWaitStatus = true;
1586 ALOGV("AudioCommandThread() adding set port config delay %d", delayMs);
1587 return sendCommand(command, delayMs);
1588}
1589
Jean-Michel Trivide801052015-04-14 19:10:14 -07001590void AudioPolicyService::AudioCommandThread::dynamicPolicyMixStateUpdateCommand(
Chih-Hung Hsiehe964d4e2016-08-09 14:31:32 -07001591 const String8& regId, int32_t state)
Jean-Michel Trivide801052015-04-14 19:10:14 -07001592{
1593 sp<AudioCommand> command = new AudioCommand();
1594 command->mCommand = DYN_POLICY_MIX_STATE_UPDATE;
1595 DynPolicyMixStateUpdateData *data = new DynPolicyMixStateUpdateData();
1596 data->mRegId = regId;
1597 data->mState = state;
1598 command->mParam = data;
1599 ALOGV("AudioCommandThread() sending dynamic policy mix (id=%s) state update to %d",
1600 regId.string(), state);
1601 sendCommand(command);
1602}
1603
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -08001604void AudioPolicyService::AudioCommandThread::recordingConfigurationUpdateCommand(
Eric Laurenta9f86652018-11-28 17:23:11 -08001605 int event,
1606 const record_client_info_t *clientInfo,
1607 const audio_config_base_t *clientConfig,
1608 std::vector<effect_descriptor_t> clientEffects,
1609 const audio_config_base_t *deviceConfig,
1610 std::vector<effect_descriptor_t> effects,
1611 audio_patch_handle_t patchHandle,
1612 audio_source_t source)
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -08001613{
1614 sp<AudioCommand>command = new AudioCommand();
1615 command->mCommand = RECORDING_CONFIGURATION_UPDATE;
1616 RecordingConfigurationUpdateData *data = new RecordingConfigurationUpdateData();
1617 data->mEvent = event;
Jean-Michel Triviac4e4292016-12-22 11:39:31 -08001618 data->mClientInfo = *clientInfo;
Jean-Michel Trivi7281aa92016-02-17 15:33:40 -08001619 data->mClientConfig = *clientConfig;
Eric Laurenta9f86652018-11-28 17:23:11 -08001620 data->mClientEffects = clientEffects;
Jean-Michel Trivi7281aa92016-02-17 15:33:40 -08001621 data->mDeviceConfig = *deviceConfig;
Eric Laurenta9f86652018-11-28 17:23:11 -08001622 data->mEffects = effects;
Jean-Michel Trivi8c7cf3b2016-02-25 17:08:24 -08001623 data->mPatchHandle = patchHandle;
Eric Laurenta9f86652018-11-28 17:23:11 -08001624 data->mSource = source;
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -08001625 command->mParam = data;
Jean-Michel Triviac4e4292016-12-22 11:39:31 -08001626 ALOGV("AudioCommandThread() adding recording configuration update event %d, source %d uid %u",
1627 event, clientInfo->source, clientInfo->uid);
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -08001628 sendCommand(command);
1629}
1630
Mikhail Naganov88b30d22020-03-09 19:43:13 +00001631void AudioPolicyService::AudioCommandThread::audioModulesUpdateCommand()
1632{
1633 sp<AudioCommand> command = new AudioCommand();
1634 command->mCommand = AUDIO_MODULES_UPDATE;
1635 sendCommand(command);
1636}
1637
Eric Laurent0ede8922014-05-09 18:04:42 -07001638status_t AudioPolicyService::AudioCommandThread::sendCommand(sp<AudioCommand>& command, int delayMs)
1639{
1640 {
1641 Mutex::Autolock _l(mLock);
1642 insertCommand_l(command, delayMs);
1643 mWaitWorkCV.signal();
1644 }
1645 Mutex::Autolock _l(command->mLock);
1646 while (command->mWaitStatus) {
1647 nsecs_t timeOutNs = kAudioCommandTimeoutNs + milliseconds(delayMs);
1648 if (command->mCond.waitRelative(command->mLock, timeOutNs) != NO_ERROR) {
1649 command->mStatus = TIMED_OUT;
1650 command->mWaitStatus = false;
1651 }
1652 }
1653 return command->mStatus;
Eric Laurentbfb1b832013-01-07 09:53:42 -08001654}
1655
Mathias Agopian65ab4712010-07-14 17:59:35 -07001656// insertCommand_l() must be called with mLock held
Eric Laurent0ede8922014-05-09 18:04:42 -07001657void AudioPolicyService::AudioCommandThread::insertCommand_l(sp<AudioCommand>& command, int delayMs)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001658{
Glenn Kasten8d6a2442012-02-08 14:04:28 -08001659 ssize_t i; // not size_t because i will count down to -1
Eric Laurent0ede8922014-05-09 18:04:42 -07001660 Vector < sp<AudioCommand> > removedCommands;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001661 command->mTime = systemTime() + milliseconds(delayMs);
1662
1663 // acquire wake lock to make sure delayed commands are processed
Eric Laurentbfb1b832013-01-07 09:53:42 -08001664 if (mAudioCommands.isEmpty()) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07001665 acquire_wake_lock(PARTIAL_WAKE_LOCK, mName.string());
1666 }
1667
1668 // check same pending commands with later time stamps and eliminate them
Ivan Lozano5ff158f2017-10-30 09:06:24 -07001669 for (i = (ssize_t)mAudioCommands.size()-1; i >= 0; i--) {
Eric Laurent0ede8922014-05-09 18:04:42 -07001670 sp<AudioCommand> command2 = mAudioCommands[i];
Mathias Agopian65ab4712010-07-14 17:59:35 -07001671 // commands are sorted by increasing time stamp: no need to scan the rest of mAudioCommands
1672 if (command2->mTime <= command->mTime) break;
Eric Laurente45b48a2014-09-04 16:40:57 -07001673
1674 // create audio patch or release audio patch commands are equivalent
1675 // with regard to filtering
1676 if ((command->mCommand == CREATE_AUDIO_PATCH) ||
1677 (command->mCommand == RELEASE_AUDIO_PATCH)) {
1678 if ((command2->mCommand != CREATE_AUDIO_PATCH) &&
1679 (command2->mCommand != RELEASE_AUDIO_PATCH)) {
1680 continue;
1681 }
1682 } else if (command2->mCommand != command->mCommand) continue;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001683
1684 switch (command->mCommand) {
1685 case SET_PARAMETERS: {
Eric Laurent0ede8922014-05-09 18:04:42 -07001686 ParametersData *data = (ParametersData *)command->mParam.get();
1687 ParametersData *data2 = (ParametersData *)command2->mParam.get();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001688 if (data->mIO != data2->mIO) break;
Steve Block3856b092011-10-20 11:56:00 +01001689 ALOGV("Comparing parameter command %s to new command %s",
Eric Laurentde070132010-07-13 04:45:46 -07001690 data2->mKeyValuePairs.string(), data->mKeyValuePairs.string());
Mathias Agopian65ab4712010-07-14 17:59:35 -07001691 AudioParameter param = AudioParameter(data->mKeyValuePairs);
1692 AudioParameter param2 = AudioParameter(data2->mKeyValuePairs);
1693 for (size_t j = 0; j < param.size(); j++) {
Glenn Kastene53b9ea2012-03-12 16:29:55 -07001694 String8 key;
1695 String8 value;
1696 param.getAt(j, key, value);
1697 for (size_t k = 0; k < param2.size(); k++) {
1698 String8 key2;
1699 String8 value2;
1700 param2.getAt(k, key2, value2);
1701 if (key2 == key) {
1702 param2.remove(key2);
1703 ALOGV("Filtering out parameter %s", key2.string());
1704 break;
1705 }
1706 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001707 }
1708 // if all keys have been filtered out, remove the command.
1709 // otherwise, update the key value pairs
1710 if (param2.size() == 0) {
1711 removedCommands.add(command2);
1712 } else {
1713 data2->mKeyValuePairs = param2.toString();
1714 }
Eric Laurent21e54562013-09-23 12:08:05 -07001715 command->mTime = command2->mTime;
1716 // force delayMs to non 0 so that code below does not request to wait for
1717 // command status as the command is now delayed
1718 delayMs = 1;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001719 } break;
1720
1721 case SET_VOLUME: {
Eric Laurent0ede8922014-05-09 18:04:42 -07001722 VolumeData *data = (VolumeData *)command->mParam.get();
1723 VolumeData *data2 = (VolumeData *)command2->mParam.get();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001724 if (data->mIO != data2->mIO) break;
1725 if (data->mStream != data2->mStream) break;
Steve Block3856b092011-10-20 11:56:00 +01001726 ALOGV("Filtering out volume command on output %d for stream %d",
Eric Laurentde070132010-07-13 04:45:46 -07001727 data->mIO, data->mStream);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001728 removedCommands.add(command2);
Eric Laurent21e54562013-09-23 12:08:05 -07001729 command->mTime = command2->mTime;
1730 // force delayMs to non 0 so that code below does not request to wait for
1731 // command status as the command is now delayed
1732 delayMs = 1;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001733 } break;
Eric Laurente45b48a2014-09-04 16:40:57 -07001734
Eric Laurentbaf35fe2016-07-27 15:36:53 -07001735 case SET_VOICE_VOLUME: {
1736 VoiceVolumeData *data = (VoiceVolumeData *)command->mParam.get();
1737 VoiceVolumeData *data2 = (VoiceVolumeData *)command2->mParam.get();
1738 ALOGV("Filtering out voice volume command value %f replaced by %f",
1739 data2->mVolume, data->mVolume);
1740 removedCommands.add(command2);
1741 command->mTime = command2->mTime;
1742 // force delayMs to non 0 so that code below does not request to wait for
1743 // command status as the command is now delayed
1744 delayMs = 1;
1745 } break;
1746
Eric Laurente45b48a2014-09-04 16:40:57 -07001747 case CREATE_AUDIO_PATCH:
1748 case RELEASE_AUDIO_PATCH: {
1749 audio_patch_handle_t handle;
Haynes Mathew Georgea2d4a6d2014-10-13 13:05:22 -07001750 struct audio_patch patch;
Eric Laurente45b48a2014-09-04 16:40:57 -07001751 if (command->mCommand == CREATE_AUDIO_PATCH) {
1752 handle = ((CreateAudioPatchData *)command->mParam.get())->mHandle;
Haynes Mathew Georgea2d4a6d2014-10-13 13:05:22 -07001753 patch = ((CreateAudioPatchData *)command->mParam.get())->mPatch;
Eric Laurente45b48a2014-09-04 16:40:57 -07001754 } else {
1755 handle = ((ReleaseAudioPatchData *)command->mParam.get())->mHandle;
Mikhail Naganov7be71d22018-05-23 16:51:46 -07001756 memset(&patch, 0, sizeof(patch));
Eric Laurente45b48a2014-09-04 16:40:57 -07001757 }
1758 audio_patch_handle_t handle2;
Haynes Mathew Georgea2d4a6d2014-10-13 13:05:22 -07001759 struct audio_patch patch2;
Eric Laurente45b48a2014-09-04 16:40:57 -07001760 if (command2->mCommand == CREATE_AUDIO_PATCH) {
1761 handle2 = ((CreateAudioPatchData *)command2->mParam.get())->mHandle;
Haynes Mathew Georgea2d4a6d2014-10-13 13:05:22 -07001762 patch2 = ((CreateAudioPatchData *)command2->mParam.get())->mPatch;
Eric Laurente45b48a2014-09-04 16:40:57 -07001763 } else {
1764 handle2 = ((ReleaseAudioPatchData *)command2->mParam.get())->mHandle;
Glenn Kastenf60b6b62015-07-06 10:53:26 -07001765 memset(&patch2, 0, sizeof(patch2));
Eric Laurente45b48a2014-09-04 16:40:57 -07001766 }
1767 if (handle != handle2) break;
Haynes Mathew Georgea2d4a6d2014-10-13 13:05:22 -07001768 /* Filter CREATE_AUDIO_PATCH commands only when they are issued for
1769 same output. */
1770 if( (command->mCommand == CREATE_AUDIO_PATCH) &&
1771 (command2->mCommand == CREATE_AUDIO_PATCH) ) {
1772 bool isOutputDiff = false;
1773 if (patch.num_sources == patch2.num_sources) {
1774 for (unsigned count = 0; count < patch.num_sources; count++) {
1775 if (patch.sources[count].id != patch2.sources[count].id) {
1776 isOutputDiff = true;
1777 break;
1778 }
1779 }
1780 if (isOutputDiff)
1781 break;
1782 }
1783 }
Eric Laurente45b48a2014-09-04 16:40:57 -07001784 ALOGV("Filtering out %s audio patch command for handle %d",
1785 (command->mCommand == CREATE_AUDIO_PATCH) ? "create" : "release", handle);
1786 removedCommands.add(command2);
1787 command->mTime = command2->mTime;
1788 // force delayMs to non 0 so that code below does not request to wait for
1789 // command status as the command is now delayed
1790 delayMs = 1;
1791 } break;
1792
Jean-Michel Trivide801052015-04-14 19:10:14 -07001793 case DYN_POLICY_MIX_STATE_UPDATE: {
1794
1795 } break;
1796
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -08001797 case RECORDING_CONFIGURATION_UPDATE: {
1798
1799 } break;
1800
Mathias Agopian65ab4712010-07-14 17:59:35 -07001801 default:
1802 break;
1803 }
1804 }
1805
1806 // remove filtered commands
1807 for (size_t j = 0; j < removedCommands.size(); j++) {
1808 // removed commands always have time stamps greater than current command
1809 for (size_t k = i + 1; k < mAudioCommands.size(); k++) {
Eric Laurent0ede8922014-05-09 18:04:42 -07001810 if (mAudioCommands[k].get() == removedCommands[j].get()) {
Steve Block3856b092011-10-20 11:56:00 +01001811 ALOGV("suppressing command: %d", mAudioCommands[k]->mCommand);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001812 mAudioCommands.removeAt(k);
1813 break;
1814 }
1815 }
1816 }
1817 removedCommands.clear();
1818
Eric Laurentaa79bef2015-01-15 14:33:51 -08001819 // Disable wait for status if delay is not 0.
1820 // Except for create audio patch command because the returned patch handle
1821 // is needed by audio policy manager
1822 if (delayMs != 0 && command->mCommand != CREATE_AUDIO_PATCH) {
Eric Laurentcec4abb2012-07-03 12:23:02 -07001823 command->mWaitStatus = false;
1824 }
Eric Laurentcec4abb2012-07-03 12:23:02 -07001825
Mathias Agopian65ab4712010-07-14 17:59:35 -07001826 // insert command at the right place according to its time stamp
Eric Laurent1e693b52014-07-09 15:03:28 -07001827 ALOGV("inserting command: %d at index %zd, num commands %zu",
1828 command->mCommand, i+1, mAudioCommands.size());
Mathias Agopian65ab4712010-07-14 17:59:35 -07001829 mAudioCommands.insertAt(command, i + 1);
1830}
1831
1832void AudioPolicyService::AudioCommandThread::exit()
1833{
Steve Block3856b092011-10-20 11:56:00 +01001834 ALOGV("AudioCommandThread::exit");
Mathias Agopian65ab4712010-07-14 17:59:35 -07001835 {
1836 AutoMutex _l(mLock);
1837 requestExit();
1838 mWaitWorkCV.signal();
1839 }
Zach Janga754b4f2015-10-27 01:29:34 +00001840 // Note that we can call it from the thread loop if all other references have been released
1841 // but it will safely return WOULD_BLOCK in this case
Mathias Agopian65ab4712010-07-14 17:59:35 -07001842 requestExitAndWait();
1843}
1844
1845void AudioPolicyService::AudioCommandThread::AudioCommand::dump(char* buffer, size_t size)
1846{
1847 snprintf(buffer, size, " %02d %06d.%03d %01u %p\n",
1848 mCommand,
1849 (int)ns2s(mTime),
1850 (int)ns2ms(mTime)%1000,
1851 mWaitStatus,
Eric Laurent0ede8922014-05-09 18:04:42 -07001852 mParam.get());
Mathias Agopian65ab4712010-07-14 17:59:35 -07001853}
1854
Dima Zavinfce7a472011-04-19 22:30:36 -07001855/******* helpers for the service_ops callbacks defined below *********/
1856void AudioPolicyService::setParameters(audio_io_handle_t ioHandle,
1857 const char *keyValuePairs,
1858 int delayMs)
1859{
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001860 mAudioCommandThread->parametersCommand(ioHandle, keyValuePairs,
Dima Zavinfce7a472011-04-19 22:30:36 -07001861 delayMs);
1862}
1863
1864int AudioPolicyService::setStreamVolume(audio_stream_type_t stream,
1865 float volume,
1866 audio_io_handle_t output,
1867 int delayMs)
1868{
Glenn Kastenfff6d712012-01-12 16:38:12 -08001869 return (int)mAudioCommandThread->volumeCommand(stream, volume,
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001870 output, delayMs);
Dima Zavinfce7a472011-04-19 22:30:36 -07001871}
1872
Dima Zavinfce7a472011-04-19 22:30:36 -07001873int AudioPolicyService::setVoiceVolume(float volume, int delayMs)
1874{
1875 return (int)mAudioCommandThread->voiceVolumeCommand(volume, delayMs);
1876}
1877
Eric Laurentb20cf7d2019-04-05 19:37:34 -07001878void AudioPolicyService::setEffectSuspended(int effectId,
1879 audio_session_t sessionId,
1880 bool suspended)
1881{
1882 mAudioCommandThread->setEffectSuspendedCommand(effectId, sessionId, suspended);
1883}
1884
Mikhail Naganov88b30d22020-03-09 19:43:13 +00001885void AudioPolicyService::onNewAudioModulesAvailable()
1886{
Mikhail Naganovd0e2c742020-03-25 15:59:59 -07001887 mOutputCommandThread->audioModulesUpdateCommand();
Mikhail Naganov88b30d22020-03-09 19:43:13 +00001888}
1889
Eric Laurentb20cf7d2019-04-05 19:37:34 -07001890
Dima Zavinfce7a472011-04-19 22:30:36 -07001891extern "C" {
Eric Laurent2d388ec2014-03-07 13:25:54 -08001892audio_module_handle_t aps_load_hw_module(void *service __unused,
1893 const char *name);
1894audio_io_handle_t aps_open_output(void *service __unused,
Eric Laurenta4c5a552012-03-29 10:12:40 -07001895 audio_devices_t *pDevices,
1896 uint32_t *pSamplingRate,
1897 audio_format_t *pFormat,
1898 audio_channel_mask_t *pChannelMask,
1899 uint32_t *pLatencyMs,
Eric Laurent2d388ec2014-03-07 13:25:54 -08001900 audio_output_flags_t flags);
Eric Laurenta4c5a552012-03-29 10:12:40 -07001901
Eric Laurent2d388ec2014-03-07 13:25:54 -08001902audio_io_handle_t aps_open_output_on_module(void *service __unused,
Eric Laurenta4c5a552012-03-29 10:12:40 -07001903 audio_module_handle_t module,
1904 audio_devices_t *pDevices,
1905 uint32_t *pSamplingRate,
1906 audio_format_t *pFormat,
1907 audio_channel_mask_t *pChannelMask,
1908 uint32_t *pLatencyMs,
Richard Fitzgeraldad3af332013-03-25 16:54:37 +00001909 audio_output_flags_t flags,
Eric Laurent2d388ec2014-03-07 13:25:54 -08001910 const audio_offload_info_t *offloadInfo);
1911audio_io_handle_t aps_open_dup_output(void *service __unused,
Dima Zavinfce7a472011-04-19 22:30:36 -07001912 audio_io_handle_t output1,
Eric Laurent2d388ec2014-03-07 13:25:54 -08001913 audio_io_handle_t output2);
1914int aps_close_output(void *service __unused, audio_io_handle_t output);
1915int aps_suspend_output(void *service __unused, audio_io_handle_t output);
1916int aps_restore_output(void *service __unused, audio_io_handle_t output);
1917audio_io_handle_t aps_open_input(void *service __unused,
Eric Laurenta4c5a552012-03-29 10:12:40 -07001918 audio_devices_t *pDevices,
1919 uint32_t *pSamplingRate,
1920 audio_format_t *pFormat,
1921 audio_channel_mask_t *pChannelMask,
Eric Laurent2d388ec2014-03-07 13:25:54 -08001922 audio_in_acoustics_t acoustics __unused);
1923audio_io_handle_t aps_open_input_on_module(void *service __unused,
Eric Laurenta4c5a552012-03-29 10:12:40 -07001924 audio_module_handle_t module,
1925 audio_devices_t *pDevices,
1926 uint32_t *pSamplingRate,
1927 audio_format_t *pFormat,
Eric Laurent2d388ec2014-03-07 13:25:54 -08001928 audio_channel_mask_t *pChannelMask);
1929int aps_close_input(void *service __unused, audio_io_handle_t input);
1930int aps_invalidate_stream(void *service __unused, audio_stream_type_t stream);
Glenn Kastend848eb42016-03-08 13:42:11 -08001931int aps_move_effects(void *service __unused, audio_session_t session,
Dima Zavinfce7a472011-04-19 22:30:36 -07001932 audio_io_handle_t src_output,
Eric Laurent2d388ec2014-03-07 13:25:54 -08001933 audio_io_handle_t dst_output);
1934char * aps_get_parameters(void *service __unused, audio_io_handle_t io_handle,
1935 const char *keys);
1936void aps_set_parameters(void *service, audio_io_handle_t io_handle,
1937 const char *kv_pairs, int delay_ms);
1938int aps_set_stream_volume(void *service, audio_stream_type_t stream,
Dima Zavinfce7a472011-04-19 22:30:36 -07001939 float volume, audio_io_handle_t output,
Eric Laurent2d388ec2014-03-07 13:25:54 -08001940 int delay_ms);
Eric Laurent2d388ec2014-03-07 13:25:54 -08001941int aps_set_voice_volume(void *service, float volume, int delay_ms);
1942};
Dima Zavinfce7a472011-04-19 22:30:36 -07001943
Mikhail Naganov1b2a7942017-12-08 10:18:09 -08001944} // namespace android