blob: e5c36eaf65faa7aad4efdd3c923afc545d785ec8 [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
Eric Laurenta171e352019-05-07 13:04:45 -0700440// OR Any client
Eric Laurenta46bedb2018-12-07 18:01:26 -0800441// AND The assistant is not on TOP
Eric Laurenta171e352019-05-07 13:04:45 -0700442// AND is on TOP or latest started
Eric Laurent1ff16a72019-03-14 18:35:04 -0700443// AND there is no active privacy sensitive capture or call
Eric Laurent589171c2019-07-25 18:04:29 -0700444// OR client has CAPTURE_AUDIO_OUTPUT privileged permission
Eric Laurent4eb58f12018-12-07 16:41:02 -0800445
Eric Laurent4e947da2019-10-17 15:24:06 -0700446
Eric Laurent4eb58f12018-12-07 16:41:02 -0800447 sp<AudioRecordClient> topActive;
448 sp<AudioRecordClient> latestActive;
Eric Laurent4eb58f12018-12-07 16:41:02 -0800449 sp<AudioRecordClient> latestSensitiveActive;
Eric Laurent1ff16a72019-03-14 18:35:04 -0700450
Eric Laurenta46bedb2018-12-07 18:01:26 -0800451 nsecs_t topStartNs = 0;
452 nsecs_t latestStartNs = 0;
Eric Laurent4eb58f12018-12-07 16:41:02 -0800453 nsecs_t latestSensitiveStartNs = 0;
454 bool isA11yOnTop = mUidPolicy->isA11yOnTop();
455 bool isAssistantOnTop = false;
456 bool isSensitiveActive = false;
Eric Laurent1ff16a72019-03-14 18:35:04 -0700457 bool isInCall = mPhoneState == AUDIO_MODE_IN_CALL;
Eric Laurent6ede98f2019-06-11 14:50:30 -0700458 bool rttCallActive =
459 (mPhoneState == AUDIO_MODE_IN_CALL || mPhoneState == AUDIO_MODE_IN_COMMUNICATION)
460 && mUidPolicy->isRttEnabled();
Eric Laurent4e947da2019-10-17 15:24:06 -0700461 bool onlyHotwordActive = true;
Eric Laurent4eb58f12018-12-07 16:41:02 -0800462
Michael Groovercfd28302018-12-11 19:16:46 -0800463 // if Sensor Privacy is enabled then all recordings should be silenced.
464 if (mSensorPrivacyPolicy->isSensorPrivacyEnabled()) {
465 silenceAllRecordings_l();
466 return;
467 }
468
Eric Laurente8c8b432018-10-17 10:08:02 -0700469 for (size_t i =0; i < mAudioRecordClients.size(); i++) {
470 sp<AudioRecordClient> current = mAudioRecordClients[i];
Eric Laurent1ff16a72019-03-14 18:35:04 -0700471 if (!current->active) {
472 continue;
Eric Laurent4eb58f12018-12-07 16:41:02 -0800473 }
Eric Laurent1ff16a72019-03-14 18:35:04 -0700474
475 app_state_t appState = apmStatFromAmState(mUidPolicy->getUidState(current->uid));
476 // clients which app is in IDLE state are not eligible for top active or
477 // latest active
478 if (appState == APP_STATE_IDLE) {
479 continue;
480 }
481
Eric Laurentc6af90d2019-07-19 10:31:39 -0700482 bool isAssistant = mUidPolicy->isAssistantUid(current->uid);
Eric Laurent589171c2019-07-25 18:04:29 -0700483 bool isAccessibility = mUidPolicy->isA11yUid(current->uid);
484 if (appState == APP_STATE_TOP && !isAccessibility) {
Eric Laurenta46bedb2018-12-07 18:01:26 -0800485 if (current->startTimeNs > topStartNs) {
486 topActive = current;
487 topStartNs = current->startTimeNs;
488 }
Eric Laurentc6af90d2019-07-19 10:31:39 -0700489 if (isAssistant) {
Eric Laurent4eb58f12018-12-07 16:41:02 -0800490 isAssistantOnTop = true;
491 }
492 }
Eric Laurent4e947da2019-10-17 15:24:06 -0700493 // Client capturing for HOTWORD or Accessibility services not considered
Eric Laurent589171c2019-07-25 18:04:29 -0700494 // for latest active to avoid masking regular clients started before
495 if (current->startTimeNs > latestStartNs
Eric Laurent4e947da2019-10-17 15:24:06 -0700496 && !(current->attributes.source == AUDIO_SOURCE_HOTWORD
497 || ((isA11yOnTop || rttCallActive) && isAssistant))
Eric Laurent589171c2019-07-25 18:04:29 -0700498 && !isAccessibility) {
Eric Laurent4eb58f12018-12-07 16:41:02 -0800499 latestActive = current;
500 latestStartNs = current->startTimeNs;
501 }
Eric Laurentd17c8502019-10-24 15:58:35 -0700502 if ((current->attributes.flags & AUDIO_FLAG_CAPTURE_PRIVATE) != 0) {
Eric Laurent1ff16a72019-03-14 18:35:04 -0700503 if (current->startTimeNs > latestSensitiveStartNs) {
504 latestSensitiveActive = current;
505 latestSensitiveStartNs = current->startTimeNs;
506 }
507 isSensitiveActive = true;
508 }
Eric Laurent4e947da2019-10-17 15:24:06 -0700509 if (current->attributes.source != AUDIO_SOURCE_HOTWORD) {
510 onlyHotwordActive = false;
511 }
Eric Laurent4eb58f12018-12-07 16:41:02 -0800512 }
513
Eric Laurent1ff16a72019-03-14 18:35:04 -0700514 // if no active client with UI on Top, consider latest active as top
515 if (topActive == nullptr) {
516 topActive = latestActive;
Eric Laurent4eb58f12018-12-07 16:41:02 -0800517 }
518
519 for (size_t i =0; i < mAudioRecordClients.size(); i++) {
520 sp<AudioRecordClient> current = mAudioRecordClients[i];
Eric Laurent1ff16a72019-03-14 18:35:04 -0700521 if (!current->active) {
522 continue;
523 }
524
Eric Laurent4eb58f12018-12-07 16:41:02 -0800525 audio_source_t source = current->attributes.source;
Eric Laurent1ff16a72019-03-14 18:35:04 -0700526 bool isTopOrLatestActive = topActive == nullptr ? false : current->uid == topActive->uid;
527 bool isLatestSensitive = latestSensitiveActive == nullptr ?
528 false : current->uid == latestSensitiveActive->uid;
529
530 // By default allow capture if:
531 // The assistant is not on TOP
Eric Laurenta171e352019-05-07 13:04:45 -0700532 // AND is on TOP or latest started
Eric Laurent1ff16a72019-03-14 18:35:04 -0700533 // AND there is no active privacy sensitive capture or call
534 // OR client has CAPTURE_AUDIO_OUTPUT privileged permission
535 bool allowCapture = !isAssistantOnTop
Eric Laurenta171e352019-05-07 13:04:45 -0700536 && ((isTopOrLatestActive && !isLatestSensitive) || isLatestSensitive)
Eric Laurent1ff16a72019-03-14 18:35:04 -0700537 && !(isSensitiveActive && !(isLatestSensitive || current->canCaptureOutput))
538 && !(isInCall && !current->canCaptureOutput);
Eric Laurent2dc962b2019-03-01 08:25:25 -0800539
540 if (isVirtualSource(source)) {
Eric Laurent1ff16a72019-03-14 18:35:04 -0700541 // Allow capture for virtual (remote submix, call audio TX or RX...) sources
542 allowCapture = true;
Eric Laurent2dc962b2019-03-01 08:25:25 -0800543 } else if (mUidPolicy->isAssistantUid(current->uid)) {
Eric Laurent1ff16a72019-03-14 18:35:04 -0700544 // For assistant allow capture if:
Eric Laurent6ede98f2019-06-11 14:50:30 -0700545 // An accessibility service is on TOP or a RTT call is active
Eric Laurent1ff16a72019-03-14 18:35:04 -0700546 // AND the source is VOICE_RECOGNITION or HOTWORD
Eric Laurenta171e352019-05-07 13:04:45 -0700547 // OR is on TOP AND uses VOICE_RECOGNITION
Eric Laurent1ff16a72019-03-14 18:35:04 -0700548 // OR uses HOTWORD
549 // AND there is no active privacy sensitive capture or call
550 // OR client has CAPTURE_AUDIO_OUTPUT privileged permission
Eric Laurent6ede98f2019-06-11 14:50:30 -0700551 if (isA11yOnTop || rttCallActive) {
Eric Laurent4eb58f12018-12-07 16:41:02 -0800552 if (source == AUDIO_SOURCE_HOTWORD || source == AUDIO_SOURCE_VOICE_RECOGNITION) {
Eric Laurent1ff16a72019-03-14 18:35:04 -0700553 allowCapture = true;
Eric Laurent4eb58f12018-12-07 16:41:02 -0800554 }
555 } else {
Eric Laurenta171e352019-05-07 13:04:45 -0700556 if (((isAssistantOnTop && source == AUDIO_SOURCE_VOICE_RECOGNITION) ||
Eric Laurent1ff16a72019-03-14 18:35:04 -0700557 source == AUDIO_SOURCE_HOTWORD) &&
558 (!(isSensitiveActive || isInCall) || current->canCaptureOutput)) {
559 allowCapture = true;
Eric Laurent4eb58f12018-12-07 16:41:02 -0800560 }
561 }
562 } else if (mUidPolicy->isA11yUid(current->uid)) {
Eric Laurent1ff16a72019-03-14 18:35:04 -0700563 // For accessibility service allow capture if:
Eric Laurent47670c92019-08-28 16:59:05 -0700564 // The assistant is not on TOP
565 // AND there is no active privacy sensitive capture or call
Eric Laurent589171c2019-07-25 18:04:29 -0700566 // OR client has CAPTURE_AUDIO_OUTPUT privileged permission
Eric Laurent47670c92019-08-28 16:59:05 -0700567 // OR
568 // Is on TOP AND the source is VOICE_RECOGNITION or HOTWORD
569 if (!isAssistantOnTop
570 && (!(isSensitiveActive || isInCall) || current->canCaptureOutput)) {
571 allowCapture = true;
572 }
Eric Laurent589171c2019-07-25 18:04:29 -0700573 if (isA11yOnTop) {
574 if (source == AUDIO_SOURCE_VOICE_RECOGNITION || source == AUDIO_SOURCE_HOTWORD) {
575 allowCapture = true;
576 }
Eric Laurent4eb58f12018-12-07 16:41:02 -0800577 }
Eric Laurent4e947da2019-10-17 15:24:06 -0700578 } else if (source == AUDIO_SOURCE_HOTWORD) {
579 // For HOTWORD source allow capture when not on TOP if:
580 // All active clients are using HOTWORD source
581 // AND no call is active
582 // OR client has CAPTURE_AUDIO_OUTPUT privileged permission
583 if (onlyHotwordActive && !(isInCall && !current->canCaptureOutput)) {
584 allowCapture = true;
585 }
Eric Laurent4eb58f12018-12-07 16:41:02 -0800586 }
Eric Laurent5ada82e2019-08-29 17:53:54 -0700587 setAppState_l(current->portId,
Eric Laurent1ff16a72019-03-14 18:35:04 -0700588 allowCapture ? apmStatFromAmState(mUidPolicy->getUidState(current->uid)) :
589 APP_STATE_IDLE);
Eric Laurente8c8b432018-10-17 10:08:02 -0700590 }
591}
592
Michael Groovercfd28302018-12-11 19:16:46 -0800593void AudioPolicyService::silenceAllRecordings_l() {
594 for (size_t i = 0; i < mAudioRecordClients.size(); i++) {
595 sp<AudioRecordClient> current = mAudioRecordClients[i];
Eric Laurent1ff16a72019-03-14 18:35:04 -0700596 if (!isVirtualSource(current->attributes.source)) {
Eric Laurent5ada82e2019-08-29 17:53:54 -0700597 setAppState_l(current->portId, APP_STATE_IDLE);
Eric Laurent1ff16a72019-03-14 18:35:04 -0700598 }
Michael Groovercfd28302018-12-11 19:16:46 -0800599 }
600}
601
Eric Laurente8c8b432018-10-17 10:08:02 -0700602/* static */
603app_state_t AudioPolicyService::apmStatFromAmState(int amState) {
Eric Laurent1ff16a72019-03-14 18:35:04 -0700604
605 if (amState == ActivityManager::PROCESS_STATE_UNKNOWN) {
Eric Laurente8c8b432018-10-17 10:08:02 -0700606 return APP_STATE_IDLE;
Eric Laurent1ff16a72019-03-14 18:35:04 -0700607 } else if (amState <= ActivityManager::PROCESS_STATE_TOP) {
608 // include persistent services
609 return APP_STATE_TOP;
Eric Laurente8c8b432018-10-17 10:08:02 -0700610 }
611 return APP_STATE_FOREGROUND;
612}
613
Eric Laurent4eb58f12018-12-07 16:41:02 -0800614/* static */
Eric Laurent2dc962b2019-03-01 08:25:25 -0800615bool AudioPolicyService::isVirtualSource(audio_source_t source)
Eric Laurent4eb58f12018-12-07 16:41:02 -0800616{
617 switch (source) {
618 case AUDIO_SOURCE_VOICE_UPLINK:
619 case AUDIO_SOURCE_VOICE_DOWNLINK:
620 case AUDIO_SOURCE_VOICE_CALL:
Eric Laurent2dc962b2019-03-01 08:25:25 -0800621 case AUDIO_SOURCE_REMOTE_SUBMIX:
622 case AUDIO_SOURCE_FM_TUNER:
Eric Laurent4eb58f12018-12-07 16:41:02 -0800623 return true;
624 default:
625 break;
626 }
627 return false;
628}
629
Eric Laurent5ada82e2019-08-29 17:53:54 -0700630void AudioPolicyService::setAppState_l(audio_port_handle_t portId, app_state_t state)
Eric Laurente8c8b432018-10-17 10:08:02 -0700631{
632 AutoCallerClear acc;
633
634 if (mAudioPolicyManager) {
Eric Laurent5ada82e2019-08-29 17:53:54 -0700635 mAudioPolicyManager->setAppState(portId, state);
Mikhail Naganoveae73eb2018-04-03 16:57:36 -0700636 }
637 sp<IAudioFlinger> af = AudioSystem::get_audio_flinger();
638 if (af) {
Eric Laurentf32108e2018-10-04 17:22:04 -0700639 bool silenced = state == APP_STATE_IDLE;
Eric Laurent5ada82e2019-08-29 17:53:54 -0700640 af->setRecordSilenced(portId, silenced);
Mikhail Naganoveae73eb2018-04-03 16:57:36 -0700641 }
Svet Ganovf4ddfef2018-01-16 07:37:58 -0800642}
643
Glenn Kasten0f11b512014-01-31 16:18:54 -0800644status_t AudioPolicyService::dump(int fd, const Vector<String16>& args __unused)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700645{
Glenn Kasten44deb052012-02-05 18:09:08 -0800646 if (!dumpAllowed()) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700647 dumpPermissionDenial(fd);
648 } else {
Mikhail Naganov959e2d02019-03-28 11:08:19 -0700649 bool locked = dumpTryLock(mLock);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700650 if (!locked) {
651 String8 result(kDeadlockedString);
652 write(fd, result.string(), result.size());
653 }
654
655 dumpInternals(fd);
Glenn Kasten9d1f02d2012-02-08 17:47:58 -0800656 if (mAudioCommandThread != 0) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700657 mAudioCommandThread->dump(fd);
658 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700659
Eric Laurentdce54a12014-03-10 12:19:46 -0700660 if (mAudioPolicyManager) {
661 mAudioPolicyManager->dump(fd);
662 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700663
Kevin Rocard8be94972019-02-22 13:26:25 -0800664 mPackageManager.dump(fd);
665
Mathias Agopian65ab4712010-07-14 17:59:35 -0700666 if (locked) mLock.unlock();
667 }
668 return NO_ERROR;
669}
670
671status_t AudioPolicyService::dumpPermissionDenial(int fd)
672{
673 const size_t SIZE = 256;
674 char buffer[SIZE];
675 String8 result;
676 snprintf(buffer, SIZE, "Permission Denial: "
677 "can't dump AudioPolicyService from pid=%d, uid=%d\n",
678 IPCThreadState::self()->getCallingPid(),
679 IPCThreadState::self()->getCallingUid());
680 result.append(buffer);
681 write(fd, result.string(), result.size());
682 return NO_ERROR;
683}
684
685status_t AudioPolicyService::onTransact(
Svet Ganovf4ddfef2018-01-16 07:37:58 -0800686 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags) {
687 switch (code) {
688 case SHELL_COMMAND_TRANSACTION: {
689 int in = data.readFileDescriptor();
690 int out = data.readFileDescriptor();
691 int err = data.readFileDescriptor();
692 int argc = data.readInt32();
693 Vector<String16> args;
694 for (int i = 0; i < argc && data.dataAvail() > 0; i++) {
695 args.add(data.readString16());
696 }
697 sp<IBinder> unusedCallback;
698 sp<IResultReceiver> resultReceiver;
699 status_t status;
700 if ((status = data.readNullableStrongBinder(&unusedCallback)) != NO_ERROR) {
701 return status;
702 }
703 if ((status = data.readNullableStrongBinder(&resultReceiver)) != NO_ERROR) {
704 return status;
705 }
706 status = shellCommand(in, out, err, args);
707 if (resultReceiver != nullptr) {
708 resultReceiver->send(status);
709 }
710 return NO_ERROR;
711 }
712 }
713
Mathias Agopian65ab4712010-07-14 17:59:35 -0700714 return BnAudioPolicyService::onTransact(code, data, reply, flags);
715}
716
Svet Ganovf4ddfef2018-01-16 07:37:58 -0800717// ------------------- Shell command implementation -------------------
718
719// NOTE: This is a remote API - make sure all args are validated
720status_t AudioPolicyService::shellCommand(int in, int out, int err, Vector<String16>& args) {
721 if (!checkCallingPermission(sManageAudioPolicyPermission, nullptr, nullptr)) {
722 return PERMISSION_DENIED;
723 }
724 if (in == BAD_TYPE || out == BAD_TYPE || err == BAD_TYPE) {
725 return BAD_VALUE;
726 }
jovanakbe066e12019-09-02 11:54:39 -0700727 if (args.size() >= 3 && args[0] == String16("set-uid-state")) {
Svet Ganovf4ddfef2018-01-16 07:37:58 -0800728 return handleSetUidState(args, err);
jovanakbe066e12019-09-02 11:54:39 -0700729 } else if (args.size() >= 2 && args[0] == String16("reset-uid-state")) {
Svet Ganovf4ddfef2018-01-16 07:37:58 -0800730 return handleResetUidState(args, err);
jovanakbe066e12019-09-02 11:54:39 -0700731 } else if (args.size() >= 2 && args[0] == String16("get-uid-state")) {
Svet Ganovf4ddfef2018-01-16 07:37:58 -0800732 return handleGetUidState(args, out, err);
733 } else if (args.size() == 1 && args[0] == String16("help")) {
734 printHelp(out);
735 return NO_ERROR;
736 }
737 printHelp(err);
738 return BAD_VALUE;
739}
740
jovanakbe066e12019-09-02 11:54:39 -0700741static status_t getUidForPackage(String16 packageName, int userId, /*inout*/uid_t& uid, int err) {
742 if (userId < 0) {
743 ALOGE("Invalid user: %d", userId);
744 dprintf(err, "Invalid user: %d\n", userId);
Svet Ganovf4ddfef2018-01-16 07:37:58 -0800745 return BAD_VALUE;
746 }
jovanakbe066e12019-09-02 11:54:39 -0700747
748 PermissionController pc;
749 uid = pc.getPackageUid(packageName, 0);
750 if (uid <= 0) {
751 ALOGE("Unknown package: '%s'", String8(packageName).string());
752 dprintf(err, "Unknown package: '%s'\n", String8(packageName).string());
753 return BAD_VALUE;
754 }
755
756 uid = multiuser_get_uid(userId, uid);
757 return NO_ERROR;
758}
759
760status_t AudioPolicyService::handleSetUidState(Vector<String16>& args, int err) {
761 // Valid arg.size() is 3 or 5, args.size() is 5 with --user option.
762 if (!(args.size() == 3 || args.size() == 5)) {
763 printHelp(err);
764 return BAD_VALUE;
765 }
766
Svet Ganovf4ddfef2018-01-16 07:37:58 -0800767 bool active = false;
768 if (args[2] == String16("active")) {
769 active = true;
770 } else if ((args[2] != String16("idle"))) {
771 ALOGE("Expected active or idle but got: '%s'", String8(args[2]).string());
772 return BAD_VALUE;
773 }
jovanakbe066e12019-09-02 11:54:39 -0700774
775 int userId = 0;
776 if (args.size() >= 5 && args[3] == String16("--user")) {
777 userId = atoi(String8(args[4]));
778 }
779
780 uid_t uid;
781 if (getUidForPackage(args[1], userId, uid, err) == BAD_VALUE) {
782 return BAD_VALUE;
783 }
784
Svet Ganovf4ddfef2018-01-16 07:37:58 -0800785 mUidPolicy->addOverrideUid(uid, active);
786 return NO_ERROR;
787}
788
789status_t AudioPolicyService::handleResetUidState(Vector<String16>& args, int err) {
jovanakbe066e12019-09-02 11:54:39 -0700790 // Valid arg.size() is 2 or 4, args.size() is 4 with --user option.
791 if (!(args.size() == 2 || args.size() == 4)) {
792 printHelp(err);
Svet Ganovf4ddfef2018-01-16 07:37:58 -0800793 return BAD_VALUE;
794 }
jovanakbe066e12019-09-02 11:54:39 -0700795
796 int userId = 0;
797 if (args.size() >= 4 && args[2] == String16("--user")) {
798 userId = atoi(String8(args[3]));
799 }
800
801 uid_t uid;
802 if (getUidForPackage(args[1], userId, uid, err) == BAD_VALUE) {
803 return BAD_VALUE;
804 }
805
Svet Ganovf4ddfef2018-01-16 07:37:58 -0800806 mUidPolicy->removeOverrideUid(uid);
807 return NO_ERROR;
808}
809
810status_t AudioPolicyService::handleGetUidState(Vector<String16>& args, int out, int err) {
jovanakbe066e12019-09-02 11:54:39 -0700811 // Valid arg.size() is 2 or 4, args.size() is 4 with --user option.
812 if (!(args.size() == 2 || args.size() == 4)) {
813 printHelp(err);
Svet Ganovf4ddfef2018-01-16 07:37:58 -0800814 return BAD_VALUE;
815 }
jovanakbe066e12019-09-02 11:54:39 -0700816
817 int userId = 0;
818 if (args.size() >= 4 && args[2] == String16("--user")) {
819 userId = atoi(String8(args[3]));
820 }
821
822 uid_t uid;
823 if (getUidForPackage(args[1], userId, uid, err) == BAD_VALUE) {
824 return BAD_VALUE;
825 }
826
Svet Ganovf4ddfef2018-01-16 07:37:58 -0800827 if (mUidPolicy->isUidActive(uid)) {
828 return dprintf(out, "active\n");
829 } else {
830 return dprintf(out, "idle\n");
831 }
832}
833
834status_t AudioPolicyService::printHelp(int out) {
835 return dprintf(out, "Audio policy service commands:\n"
jovanakbe066e12019-09-02 11:54:39 -0700836 " get-uid-state <PACKAGE> [--user USER_ID] gets the uid state\n"
837 " set-uid-state <PACKAGE> <active|idle> [--user USER_ID] overrides the uid state\n"
838 " reset-uid-state <PACKAGE> [--user USER_ID] clears the uid state override\n"
Svet Ganovf4ddfef2018-01-16 07:37:58 -0800839 " help print this message\n");
840}
841
842// ----------- AudioPolicyService::UidPolicy implementation ----------
843
844void AudioPolicyService::UidPolicy::registerSelf() {
Steven Moreland2f348142019-07-02 15:59:07 -0700845 status_t res = mAm.linkToDeath(this);
846 mAm.registerUidObserver(this, ActivityManager::UID_OBSERVER_GONE
Svet Ganovf4ddfef2018-01-16 07:37:58 -0800847 | ActivityManager::UID_OBSERVER_IDLE
Eric Laurente8c8b432018-10-17 10:08:02 -0700848 | ActivityManager::UID_OBSERVER_ACTIVE
849 | ActivityManager::UID_OBSERVER_PROCSTATE,
Svet Ganovf4ddfef2018-01-16 07:37:58 -0800850 ActivityManager::PROCESS_STATE_UNKNOWN,
851 String16("audioserver"));
Mikhail Naganoveae73eb2018-04-03 16:57:36 -0700852 if (!res) {
853 Mutex::Autolock _l(mLock);
854 mObserverRegistered = true;
855 } else {
856 ALOGE("UidPolicy::registerSelf linkToDeath failed: %d", res);
Eric Laurent4eb58f12018-12-07 16:41:02 -0800857
Steven Moreland2f348142019-07-02 15:59:07 -0700858 mAm.unregisterUidObserver(this);
Mikhail Naganoveae73eb2018-04-03 16:57:36 -0700859 }
Svet Ganovf4ddfef2018-01-16 07:37:58 -0800860}
861
862void AudioPolicyService::UidPolicy::unregisterSelf() {
Steven Moreland2f348142019-07-02 15:59:07 -0700863 mAm.unlinkToDeath(this);
864 mAm.unregisterUidObserver(this);
Mikhail Naganoveae73eb2018-04-03 16:57:36 -0700865 Mutex::Autolock _l(mLock);
866 mObserverRegistered = false;
Svet Ganovf4ddfef2018-01-16 07:37:58 -0800867}
868
Mikhail Naganoveae73eb2018-04-03 16:57:36 -0700869void AudioPolicyService::UidPolicy::binderDied(__unused const wp<IBinder> &who) {
870 Mutex::Autolock _l(mLock);
871 mCachedUids.clear();
872 mObserverRegistered = false;
Svet Ganovf4ddfef2018-01-16 07:37:58 -0800873}
874
Eric Laurente8c8b432018-10-17 10:08:02 -0700875void AudioPolicyService::UidPolicy::checkRegistered() {
Mikhail Naganoveae73eb2018-04-03 16:57:36 -0700876 bool needToReregister = false;
877 {
878 Mutex::Autolock _l(mLock);
879 needToReregister = !mObserverRegistered;
Svet Ganovf4ddfef2018-01-16 07:37:58 -0800880 }
Mikhail Naganoveae73eb2018-04-03 16:57:36 -0700881 if (needToReregister) {
882 // Looks like ActivityManager has died previously, attempt to re-register.
883 registerSelf();
884 }
Eric Laurente8c8b432018-10-17 10:08:02 -0700885}
886
887bool AudioPolicyService::UidPolicy::isUidActive(uid_t uid) {
888 if (isServiceUid(uid)) return true;
889 checkRegistered();
Mikhail Naganoveae73eb2018-04-03 16:57:36 -0700890 {
891 Mutex::Autolock _l(mLock);
892 auto overrideIter = mOverrideUids.find(uid);
893 if (overrideIter != mOverrideUids.end()) {
Eric Laurente8c8b432018-10-17 10:08:02 -0700894 return overrideIter->second.first;
Mikhail Naganoveae73eb2018-04-03 16:57:36 -0700895 }
896 // In an absense of the ActivityManager, assume everything to be active.
897 if (!mObserverRegistered) return true;
898 auto cacheIter = mCachedUids.find(uid);
Mikhail Naganoveba668a2018-04-05 08:13:15 -0700899 if (cacheIter != mCachedUids.end()) {
Eric Laurente8c8b432018-10-17 10:08:02 -0700900 return cacheIter->second.first;
Mikhail Naganoveae73eb2018-04-03 16:57:36 -0700901 }
902 }
903 ActivityManager am;
904 bool active = am.isUidActive(uid, String16("audioserver"));
905 {
906 Mutex::Autolock _l(mLock);
Eric Laurente8c8b432018-10-17 10:08:02 -0700907 mCachedUids.insert(std::pair<uid_t,
908 std::pair<bool, int>>(uid, std::pair<bool, int>(active,
909 ActivityManager::PROCESS_STATE_UNKNOWN)));
Mikhail Naganoveae73eb2018-04-03 16:57:36 -0700910 }
911 return active;
Svet Ganovf4ddfef2018-01-16 07:37:58 -0800912}
913
Eric Laurente8c8b432018-10-17 10:08:02 -0700914int AudioPolicyService::UidPolicy::getUidState(uid_t uid) {
915 if (isServiceUid(uid)) {
916 return ActivityManager::PROCESS_STATE_TOP;
917 }
918 checkRegistered();
919 {
920 Mutex::Autolock _l(mLock);
921 auto overrideIter = mOverrideUids.find(uid);
922 if (overrideIter != mOverrideUids.end()) {
923 if (overrideIter->second.first) {
924 if (overrideIter->second.second != ActivityManager::PROCESS_STATE_UNKNOWN) {
925 return overrideIter->second.second;
926 } else {
927 auto cacheIter = mCachedUids.find(uid);
928 if (cacheIter != mCachedUids.end()) {
929 return cacheIter->second.second;
930 }
931 }
932 }
933 return ActivityManager::PROCESS_STATE_UNKNOWN;
934 }
935 // In an absense of the ActivityManager, assume everything to be active.
936 if (!mObserverRegistered) {
937 return ActivityManager::PROCESS_STATE_TOP;
938 }
939 auto cacheIter = mCachedUids.find(uid);
940 if (cacheIter != mCachedUids.end()) {
941 if (cacheIter->second.first) {
942 return cacheIter->second.second;
943 } else {
944 return ActivityManager::PROCESS_STATE_UNKNOWN;
945 }
946 }
947 }
948 ActivityManager am;
949 bool active = am.isUidActive(uid, String16("audioserver"));
950 int state = ActivityManager::PROCESS_STATE_UNKNOWN;
951 if (active) {
952 state = am.getUidProcessState(uid, String16("audioserver"));
953 }
954 {
955 Mutex::Autolock _l(mLock);
956 mCachedUids.insert(std::pair<uid_t,
957 std::pair<bool, int>>(uid, std::pair<bool, int>(active, state)));
958 }
Eric Laurent4eb58f12018-12-07 16:41:02 -0800959
Eric Laurente8c8b432018-10-17 10:08:02 -0700960 return state;
961}
962
Mikhail Naganoveae73eb2018-04-03 16:57:36 -0700963void AudioPolicyService::UidPolicy::onUidActive(uid_t uid) {
Eric Laurente8c8b432018-10-17 10:08:02 -0700964 updateUid(&mCachedUids, uid, true, ActivityManager::PROCESS_STATE_UNKNOWN, true);
Mikhail Naganoveae73eb2018-04-03 16:57:36 -0700965}
966
967void AudioPolicyService::UidPolicy::onUidGone(uid_t uid, __unused bool disabled) {
Eric Laurente8c8b432018-10-17 10:08:02 -0700968 updateUid(&mCachedUids, uid, false, ActivityManager::PROCESS_STATE_UNKNOWN, false);
Mikhail Naganoveae73eb2018-04-03 16:57:36 -0700969}
970
971void AudioPolicyService::UidPolicy::onUidIdle(uid_t uid, __unused bool disabled) {
Eric Laurente8c8b432018-10-17 10:08:02 -0700972 updateUid(&mCachedUids, uid, false, ActivityManager::PROCESS_STATE_UNKNOWN, true);
Mikhail Naganoveae73eb2018-04-03 16:57:36 -0700973}
974
Eric Laurente8c8b432018-10-17 10:08:02 -0700975void AudioPolicyService::UidPolicy::onUidStateChanged(uid_t uid,
976 int32_t procState,
Hui Yu13ad0eb2019-09-09 10:27:07 -0700977 int64_t procStateSeq __unused,
978 int32_t capability __unused) {
Eric Laurente8c8b432018-10-17 10:08:02 -0700979 if (procState != ActivityManager::PROCESS_STATE_UNKNOWN) {
980 updateUid(&mCachedUids, uid, true, procState, true);
Mikhail Naganoveae73eb2018-04-03 16:57:36 -0700981 }
982}
983
984void AudioPolicyService::UidPolicy::updateOverrideUid(uid_t uid, bool active, bool insert) {
Eric Laurente8c8b432018-10-17 10:08:02 -0700985 updateUid(&mOverrideUids, uid, active, ActivityManager::PROCESS_STATE_UNKNOWN, insert);
986}
987
988void AudioPolicyService::UidPolicy::notifyService() {
989 sp<AudioPolicyService> service = mService.promote();
990 if (service != nullptr) {
991 service->updateUidStates();
Mikhail Naganoveae73eb2018-04-03 16:57:36 -0700992 }
993}
994
Eric Laurente8c8b432018-10-17 10:08:02 -0700995void AudioPolicyService::UidPolicy::updateUid(std::unordered_map<uid_t,
996 std::pair<bool, int>> *uids,
997 uid_t uid,
998 bool active,
999 int state,
1000 bool insert) {
1001 if (isServiceUid(uid)) {
1002 return;
1003 }
1004 bool wasActive = isUidActive(uid);
1005 int previousState = getUidState(uid);
Mikhail Naganoveae73eb2018-04-03 16:57:36 -07001006 {
1007 Mutex::Autolock _l(mLock);
Eric Laurente8c8b432018-10-17 10:08:02 -07001008 updateUidLocked(uids, uid, active, state, insert);
Mikhail Naganoveae73eb2018-04-03 16:57:36 -07001009 }
Eric Laurente8c8b432018-10-17 10:08:02 -07001010 if (wasActive != isUidActive(uid) || state != previousState) {
1011 notifyService();
1012 }
Mikhail Naganoveae73eb2018-04-03 16:57:36 -07001013}
1014
Eric Laurente8c8b432018-10-17 10:08:02 -07001015void AudioPolicyService::UidPolicy::updateUidLocked(std::unordered_map<uid_t,
1016 std::pair<bool, int>> *uids,
1017 uid_t uid,
1018 bool active,
1019 int state,
1020 bool insert) {
Mikhail Naganoveae73eb2018-04-03 16:57:36 -07001021 auto it = uids->find(uid);
1022 if (it != uids->end()) {
Mikhail Naganoveae73eb2018-04-03 16:57:36 -07001023 if (insert) {
Eric Laurente8c8b432018-10-17 10:08:02 -07001024 if (state == ActivityManager::PROCESS_STATE_UNKNOWN) {
1025 it->second.first = active;
1026 }
1027 if (it->second.first) {
1028 it->second.second = state;
1029 } else {
1030 it->second.second = ActivityManager::PROCESS_STATE_UNKNOWN;
1031 }
Mikhail Naganoveae73eb2018-04-03 16:57:36 -07001032 } else {
1033 uids->erase(it);
1034 }
Eric Laurente8c8b432018-10-17 10:08:02 -07001035 } else if (insert && (state == ActivityManager::PROCESS_STATE_UNKNOWN)) {
1036 uids->insert(std::pair<uid_t, std::pair<bool, int>>(uid,
1037 std::pair<bool, int>(active, state)));
Mikhail Naganoveae73eb2018-04-03 16:57:36 -07001038 }
Svet Ganovf4ddfef2018-01-16 07:37:58 -08001039}
Mathias Agopian65ab4712010-07-14 17:59:35 -07001040
Eric Laurent4eb58f12018-12-07 16:41:02 -08001041bool AudioPolicyService::UidPolicy::isA11yOnTop() {
1042 for (const auto &uid : mCachedUids) {
Eric Laurent47670c92019-08-28 16:59:05 -07001043 if (!isA11yUid(uid.first)) {
Eric Laurent4eb58f12018-12-07 16:41:02 -08001044 continue;
1045 }
Amith Yamasanibcbb3002019-01-23 13:53:33 -08001046 if (uid.second.second >= ActivityManager::PROCESS_STATE_TOP
1047 && uid.second.second <= ActivityManager::PROCESS_STATE_BOUND_FOREGROUND_SERVICE) {
Eric Laurent4eb58f12018-12-07 16:41:02 -08001048 return true;
1049 }
1050 }
1051 return false;
1052}
1053
Eric Laurentb78763e2018-10-17 10:08:02 -07001054bool AudioPolicyService::UidPolicy::isA11yUid(uid_t uid)
1055{
1056 std::vector<uid_t>::iterator it = find(mA11yUids.begin(), mA11yUids.end(), uid);
1057 return it != mA11yUids.end();
1058}
1059
Michael Groovercfd28302018-12-11 19:16:46 -08001060// ----------- AudioPolicyService::SensorPrivacyService implementation ----------
1061void AudioPolicyService::SensorPrivacyPolicy::registerSelf() {
1062 SensorPrivacyManager spm;
1063 mSensorPrivacyEnabled = spm.isSensorPrivacyEnabled();
1064 spm.addSensorPrivacyListener(this);
1065}
1066
1067void AudioPolicyService::SensorPrivacyPolicy::unregisterSelf() {
1068 SensorPrivacyManager spm;
1069 spm.removeSensorPrivacyListener(this);
1070}
1071
1072bool AudioPolicyService::SensorPrivacyPolicy::isSensorPrivacyEnabled() {
1073 return mSensorPrivacyEnabled;
1074}
1075
1076binder::Status AudioPolicyService::SensorPrivacyPolicy::onSensorPrivacyChanged(bool enabled) {
1077 mSensorPrivacyEnabled = enabled;
1078 sp<AudioPolicyService> service = mService.promote();
1079 if (service != nullptr) {
1080 service->updateUidStates();
1081 }
1082 return binder::Status::ok();
1083}
1084
Mathias Agopian65ab4712010-07-14 17:59:35 -07001085// ----------- AudioPolicyService::AudioCommandThread implementation ----------
1086
Eric Laurentbfb1b832013-01-07 09:53:42 -08001087AudioPolicyService::AudioCommandThread::AudioCommandThread(String8 name,
1088 const wp<AudioPolicyService>& service)
1089 : Thread(false), mName(name), mService(service)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001090{
Mathias Agopian65ab4712010-07-14 17:59:35 -07001091}
1092
1093
1094AudioPolicyService::AudioCommandThread::~AudioCommandThread()
1095{
Eric Laurentbfb1b832013-01-07 09:53:42 -08001096 if (!mAudioCommands.isEmpty()) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07001097 release_wake_lock(mName.string());
1098 }
1099 mAudioCommands.clear();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001100}
1101
1102void AudioPolicyService::AudioCommandThread::onFirstRef()
1103{
Eric Laurentbfb1b832013-01-07 09:53:42 -08001104 run(mName.string(), ANDROID_PRIORITY_AUDIO);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001105}
1106
1107bool AudioPolicyService::AudioCommandThread::threadLoop()
1108{
Eric Laurentd7eda8d2016-02-02 17:18:39 -08001109 nsecs_t waitTime = -1;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001110
1111 mLock.lock();
1112 while (!exitPending())
1113 {
Eric Laurent59a89232014-06-08 14:14:17 -07001114 sp<AudioPolicyService> svc;
1115 while (!mAudioCommands.isEmpty() && !exitPending()) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07001116 nsecs_t curTime = systemTime();
1117 // commands are sorted by increasing time stamp: execute them from index 0 and up
1118 if (mAudioCommands[0]->mTime <= curTime) {
Eric Laurent0ede8922014-05-09 18:04:42 -07001119 sp<AudioCommand> command = mAudioCommands[0];
Mathias Agopian65ab4712010-07-14 17:59:35 -07001120 mAudioCommands.removeAt(0);
Eric Laurent0ede8922014-05-09 18:04:42 -07001121 mLastCommand = command;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001122
1123 switch (command->mCommand) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07001124 case SET_VOLUME: {
Eric Laurent0ede8922014-05-09 18:04:42 -07001125 VolumeData *data = (VolumeData *)command->mParam.get();
Steve Block3856b092011-10-20 11:56:00 +01001126 ALOGV("AudioCommandThread() processing set volume stream %d, \
Eric Laurentde070132010-07-13 04:45:46 -07001127 volume %f, output %d", data->mStream, data->mVolume, data->mIO);
Andy Hungfe726a62018-09-27 15:17:25 -07001128 mLock.unlock();
Eric Laurentde070132010-07-13 04:45:46 -07001129 command->mStatus = AudioSystem::setStreamVolume(data->mStream,
1130 data->mVolume,
1131 data->mIO);
Andy Hungfe726a62018-09-27 15:17:25 -07001132 mLock.lock();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001133 }break;
1134 case SET_PARAMETERS: {
Eric Laurent0ede8922014-05-09 18:04:42 -07001135 ParametersData *data = (ParametersData *)command->mParam.get();
Glenn Kastene53b9ea2012-03-12 16:29:55 -07001136 ALOGV("AudioCommandThread() processing set parameters string %s, io %d",
1137 data->mKeyValuePairs.string(), data->mIO);
Andy Hungfe726a62018-09-27 15:17:25 -07001138 mLock.unlock();
Glenn Kastene53b9ea2012-03-12 16:29:55 -07001139 command->mStatus = AudioSystem::setParameters(data->mIO, data->mKeyValuePairs);
Andy Hungfe726a62018-09-27 15:17:25 -07001140 mLock.lock();
Glenn Kastene53b9ea2012-03-12 16:29:55 -07001141 }break;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001142 case SET_VOICE_VOLUME: {
Eric Laurent0ede8922014-05-09 18:04:42 -07001143 VoiceVolumeData *data = (VoiceVolumeData *)command->mParam.get();
Steve Block3856b092011-10-20 11:56:00 +01001144 ALOGV("AudioCommandThread() processing set voice volume volume %f",
Eric Laurentde070132010-07-13 04:45:46 -07001145 data->mVolume);
Andy Hungfe726a62018-09-27 15:17:25 -07001146 mLock.unlock();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001147 command->mStatus = AudioSystem::setVoiceVolume(data->mVolume);
Andy Hungfe726a62018-09-27 15:17:25 -07001148 mLock.lock();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001149 }break;
Eric Laurentbfb1b832013-01-07 09:53:42 -08001150 case STOP_OUTPUT: {
Eric Laurent0ede8922014-05-09 18:04:42 -07001151 StopOutputData *data = (StopOutputData *)command->mParam.get();
Eric Laurentd7fe0862018-07-14 16:48:01 -07001152 ALOGV("AudioCommandThread() processing stop output portId %d",
1153 data->mPortId);
Eric Laurent59a89232014-06-08 14:14:17 -07001154 svc = mService.promote();
Eric Laurentbfb1b832013-01-07 09:53:42 -08001155 if (svc == 0) {
1156 break;
1157 }
1158 mLock.unlock();
Eric Laurentd7fe0862018-07-14 16:48:01 -07001159 svc->doStopOutput(data->mPortId);
Eric Laurentbfb1b832013-01-07 09:53:42 -08001160 mLock.lock();
Eric Laurentbfb1b832013-01-07 09:53:42 -08001161 }break;
1162 case RELEASE_OUTPUT: {
Eric Laurent0ede8922014-05-09 18:04:42 -07001163 ReleaseOutputData *data = (ReleaseOutputData *)command->mParam.get();
Eric Laurentd7fe0862018-07-14 16:48:01 -07001164 ALOGV("AudioCommandThread() processing release output portId %d",
1165 data->mPortId);
Eric Laurent59a89232014-06-08 14:14:17 -07001166 svc = mService.promote();
Eric Laurentbfb1b832013-01-07 09:53:42 -08001167 if (svc == 0) {
1168 break;
1169 }
1170 mLock.unlock();
Eric Laurentd7fe0862018-07-14 16:48:01 -07001171 svc->doReleaseOutput(data->mPortId);
Eric Laurentbfb1b832013-01-07 09:53:42 -08001172 mLock.lock();
Eric Laurentbfb1b832013-01-07 09:53:42 -08001173 }break;
Eric Laurent951f4552014-05-20 10:48:17 -07001174 case CREATE_AUDIO_PATCH: {
1175 CreateAudioPatchData *data = (CreateAudioPatchData *)command->mParam.get();
1176 ALOGV("AudioCommandThread() processing create audio patch");
1177 sp<IAudioFlinger> af = AudioSystem::get_audio_flinger();
1178 if (af == 0) {
1179 command->mStatus = PERMISSION_DENIED;
1180 } else {
Andy Hungfe726a62018-09-27 15:17:25 -07001181 mLock.unlock();
Eric Laurent951f4552014-05-20 10:48:17 -07001182 command->mStatus = af->createAudioPatch(&data->mPatch, &data->mHandle);
Andy Hungfe726a62018-09-27 15:17:25 -07001183 mLock.lock();
Eric Laurent951f4552014-05-20 10:48:17 -07001184 }
1185 } break;
1186 case RELEASE_AUDIO_PATCH: {
1187 ReleaseAudioPatchData *data = (ReleaseAudioPatchData *)command->mParam.get();
1188 ALOGV("AudioCommandThread() processing release audio patch");
1189 sp<IAudioFlinger> af = AudioSystem::get_audio_flinger();
1190 if (af == 0) {
1191 command->mStatus = PERMISSION_DENIED;
1192 } else {
Andy Hungfe726a62018-09-27 15:17:25 -07001193 mLock.unlock();
Eric Laurent951f4552014-05-20 10:48:17 -07001194 command->mStatus = af->releaseAudioPatch(data->mHandle);
Andy Hungfe726a62018-09-27 15:17:25 -07001195 mLock.lock();
Eric Laurent951f4552014-05-20 10:48:17 -07001196 }
1197 } break;
Eric Laurentb52c1522014-05-20 11:27:36 -07001198 case UPDATE_AUDIOPORT_LIST: {
1199 ALOGV("AudioCommandThread() processing update audio port list");
Eric Laurent59a89232014-06-08 14:14:17 -07001200 svc = mService.promote();
Eric Laurentb52c1522014-05-20 11:27:36 -07001201 if (svc == 0) {
1202 break;
1203 }
1204 mLock.unlock();
1205 svc->doOnAudioPortListUpdate();
1206 mLock.lock();
1207 }break;
1208 case UPDATE_AUDIOPATCH_LIST: {
1209 ALOGV("AudioCommandThread() processing update audio patch list");
Eric Laurent59a89232014-06-08 14:14:17 -07001210 svc = mService.promote();
Eric Laurentb52c1522014-05-20 11:27:36 -07001211 if (svc == 0) {
1212 break;
1213 }
1214 mLock.unlock();
1215 svc->doOnAudioPatchListUpdate();
1216 mLock.lock();
1217 }break;
François Gaffiecfe17322018-11-07 13:41:29 +01001218 case CHANGED_AUDIOVOLUMEGROUP: {
1219 AudioVolumeGroupData *data =
1220 static_cast<AudioVolumeGroupData *>(command->mParam.get());
1221 ALOGV("AudioCommandThread() processing update audio volume group");
1222 svc = mService.promote();
1223 if (svc == 0) {
1224 break;
1225 }
1226 mLock.unlock();
1227 svc->doOnAudioVolumeGroupChanged(data->mGroup, data->mFlags);
1228 mLock.lock();
1229 }break;
Eric Laurente1715a42014-05-20 11:30:42 -07001230 case SET_AUDIOPORT_CONFIG: {
1231 SetAudioPortConfigData *data = (SetAudioPortConfigData *)command->mParam.get();
1232 ALOGV("AudioCommandThread() processing set port config");
1233 sp<IAudioFlinger> af = AudioSystem::get_audio_flinger();
1234 if (af == 0) {
1235 command->mStatus = PERMISSION_DENIED;
1236 } else {
Andy Hungfe726a62018-09-27 15:17:25 -07001237 mLock.unlock();
Eric Laurente1715a42014-05-20 11:30:42 -07001238 command->mStatus = af->setAudioPortConfig(&data->mConfig);
Andy Hungfe726a62018-09-27 15:17:25 -07001239 mLock.lock();
Eric Laurente1715a42014-05-20 11:30:42 -07001240 }
1241 } break;
Jean-Michel Trivide801052015-04-14 19:10:14 -07001242 case DYN_POLICY_MIX_STATE_UPDATE: {
1243 DynPolicyMixStateUpdateData *data =
1244 (DynPolicyMixStateUpdateData *)command->mParam.get();
Jean-Michel Trivide801052015-04-14 19:10:14 -07001245 ALOGV("AudioCommandThread() processing dyn policy mix state update %s %d",
1246 data->mRegId.string(), data->mState);
1247 svc = mService.promote();
1248 if (svc == 0) {
1249 break;
1250 }
1251 mLock.unlock();
1252 svc->doOnDynamicPolicyMixStateUpdate(data->mRegId, data->mState);
1253 mLock.lock();
1254 } break;
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -08001255 case RECORDING_CONFIGURATION_UPDATE: {
1256 RecordingConfigurationUpdateData *data =
1257 (RecordingConfigurationUpdateData *)command->mParam.get();
1258 ALOGV("AudioCommandThread() processing recording configuration update");
1259 svc = mService.promote();
1260 if (svc == 0) {
1261 break;
1262 }
1263 mLock.unlock();
Jean-Michel Triviac4e4292016-12-22 11:39:31 -08001264 svc->doOnRecordingConfigurationUpdate(data->mEvent, &data->mClientInfo,
Eric Laurenta9f86652018-11-28 17:23:11 -08001265 &data->mClientConfig, data->mClientEffects,
1266 &data->mDeviceConfig, data->mEffects,
1267 data->mPatchHandle, data->mSource);
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -08001268 mLock.lock();
1269 } break;
Eric Laurentb20cf7d2019-04-05 19:37:34 -07001270 case SET_EFFECT_SUSPENDED: {
1271 SetEffectSuspendedData *data = (SetEffectSuspendedData *)command->mParam.get();
1272 ALOGV("AudioCommandThread() processing set effect suspended");
1273 sp<IAudioFlinger> af = AudioSystem::get_audio_flinger();
1274 if (af != 0) {
1275 mLock.unlock();
1276 af->setEffectSuspended(data->mEffectId, data->mSessionId, data->mSuspended);
1277 mLock.lock();
1278 }
1279 } break;
1280
Mathias Agopian65ab4712010-07-14 17:59:35 -07001281 default:
Steve Block5ff1dd52012-01-05 23:22:43 +00001282 ALOGW("AudioCommandThread() unknown command %d", command->mCommand);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001283 }
Eric Laurent0ede8922014-05-09 18:04:42 -07001284 {
1285 Mutex::Autolock _l(command->mLock);
1286 if (command->mWaitStatus) {
1287 command->mWaitStatus = false;
1288 command->mCond.signal();
1289 }
1290 }
Eric Laurentd7eda8d2016-02-02 17:18:39 -08001291 waitTime = -1;
Zach Janga754b4f2015-10-27 01:29:34 +00001292 // release mLock before releasing strong reference on the service as
1293 // AudioPolicyService destructor calls AudioCommandThread::exit() which
1294 // acquires mLock.
1295 mLock.unlock();
1296 svc.clear();
1297 mLock.lock();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001298 } else {
1299 waitTime = mAudioCommands[0]->mTime - curTime;
1300 break;
1301 }
1302 }
Zach Janga754b4f2015-10-27 01:29:34 +00001303
1304 // release delayed commands wake lock if the queue is empty
1305 if (mAudioCommands.isEmpty()) {
Ricardo Garcia05f2fdc2014-07-24 15:48:24 -07001306 release_wake_lock(mName.string());
Zach Janga754b4f2015-10-27 01:29:34 +00001307 }
1308
1309 // At this stage we have either an empty command queue or the first command in the queue
1310 // has a finite delay. So unless we are exiting it is safe to wait.
1311 if (!exitPending()) {
Eric Laurent59a89232014-06-08 14:14:17 -07001312 ALOGV("AudioCommandThread() going to sleep");
Eric Laurentd7eda8d2016-02-02 17:18:39 -08001313 if (waitTime == -1) {
1314 mWaitWorkCV.wait(mLock);
1315 } else {
1316 mWaitWorkCV.waitRelative(mLock, waitTime);
1317 }
Eric Laurent59a89232014-06-08 14:14:17 -07001318 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001319 }
Ricardo Garcia05f2fdc2014-07-24 15:48:24 -07001320 // release delayed commands wake lock before quitting
1321 if (!mAudioCommands.isEmpty()) {
1322 release_wake_lock(mName.string());
1323 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001324 mLock.unlock();
1325 return false;
1326}
1327
1328status_t AudioPolicyService::AudioCommandThread::dump(int fd)
1329{
1330 const size_t SIZE = 256;
1331 char buffer[SIZE];
1332 String8 result;
1333
1334 snprintf(buffer, SIZE, "AudioCommandThread %p Dump\n", this);
1335 result.append(buffer);
1336 write(fd, result.string(), result.size());
1337
Mikhail Naganov959e2d02019-03-28 11:08:19 -07001338 bool locked = dumpTryLock(mLock);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001339 if (!locked) {
1340 String8 result2(kCmdDeadlockedString);
1341 write(fd, result2.string(), result2.size());
1342 }
1343
1344 snprintf(buffer, SIZE, "- Commands:\n");
1345 result = String8(buffer);
1346 result.append(" Command Time Wait pParam\n");
Glenn Kasten8d6a2442012-02-08 14:04:28 -08001347 for (size_t i = 0; i < mAudioCommands.size(); i++) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07001348 mAudioCommands[i]->dump(buffer, SIZE);
1349 result.append(buffer);
1350 }
1351 result.append(" Last Command\n");
Eric Laurent0ede8922014-05-09 18:04:42 -07001352 if (mLastCommand != 0) {
1353 mLastCommand->dump(buffer, SIZE);
1354 result.append(buffer);
1355 } else {
1356 result.append(" none\n");
1357 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001358
1359 write(fd, result.string(), result.size());
1360
1361 if (locked) mLock.unlock();
1362
1363 return NO_ERROR;
1364}
1365
Glenn Kastenfff6d712012-01-12 16:38:12 -08001366status_t AudioPolicyService::AudioCommandThread::volumeCommand(audio_stream_type_t stream,
Eric Laurentde070132010-07-13 04:45:46 -07001367 float volume,
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001368 audio_io_handle_t output,
Eric Laurentde070132010-07-13 04:45:46 -07001369 int delayMs)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001370{
Eric Laurent0ede8922014-05-09 18:04:42 -07001371 sp<AudioCommand> command = new AudioCommand();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001372 command->mCommand = SET_VOLUME;
Eric Laurent0ede8922014-05-09 18:04:42 -07001373 sp<VolumeData> data = new VolumeData();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001374 data->mStream = stream;
1375 data->mVolume = volume;
1376 data->mIO = output;
1377 command->mParam = data;
Eric Laurent0ede8922014-05-09 18:04:42 -07001378 command->mWaitStatus = true;
Steve Block3856b092011-10-20 11:56:00 +01001379 ALOGV("AudioCommandThread() adding set volume stream %d, volume %f, output %d",
Eric Laurentde070132010-07-13 04:45:46 -07001380 stream, volume, output);
Eric Laurent0ede8922014-05-09 18:04:42 -07001381 return sendCommand(command, delayMs);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001382}
1383
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001384status_t AudioPolicyService::AudioCommandThread::parametersCommand(audio_io_handle_t ioHandle,
Dima Zavinfce7a472011-04-19 22:30:36 -07001385 const char *keyValuePairs,
Eric Laurentde070132010-07-13 04:45:46 -07001386 int delayMs)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001387{
Eric Laurent0ede8922014-05-09 18:04:42 -07001388 sp<AudioCommand> command = new AudioCommand();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001389 command->mCommand = SET_PARAMETERS;
Eric Laurent0ede8922014-05-09 18:04:42 -07001390 sp<ParametersData> data = new ParametersData();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001391 data->mIO = ioHandle;
Dima Zavinfce7a472011-04-19 22:30:36 -07001392 data->mKeyValuePairs = String8(keyValuePairs);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001393 command->mParam = data;
Eric Laurent0ede8922014-05-09 18:04:42 -07001394 command->mWaitStatus = true;
Steve Block3856b092011-10-20 11:56:00 +01001395 ALOGV("AudioCommandThread() adding set parameter string %s, io %d ,delay %d",
Dima Zavinfce7a472011-04-19 22:30:36 -07001396 keyValuePairs, ioHandle, delayMs);
Eric Laurent0ede8922014-05-09 18:04:42 -07001397 return sendCommand(command, delayMs);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001398}
1399
1400status_t AudioPolicyService::AudioCommandThread::voiceVolumeCommand(float volume, int delayMs)
1401{
Eric Laurent0ede8922014-05-09 18:04:42 -07001402 sp<AudioCommand> command = new AudioCommand();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001403 command->mCommand = SET_VOICE_VOLUME;
Eric Laurent0ede8922014-05-09 18:04:42 -07001404 sp<VoiceVolumeData> data = new VoiceVolumeData();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001405 data->mVolume = volume;
1406 command->mParam = data;
Eric Laurent0ede8922014-05-09 18:04:42 -07001407 command->mWaitStatus = true;
Steve Block3856b092011-10-20 11:56:00 +01001408 ALOGV("AudioCommandThread() adding set voice volume volume %f", volume);
Eric Laurent0ede8922014-05-09 18:04:42 -07001409 return sendCommand(command, delayMs);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001410}
1411
Eric Laurentb20cf7d2019-04-05 19:37:34 -07001412void AudioPolicyService::AudioCommandThread::setEffectSuspendedCommand(int effectId,
1413 audio_session_t sessionId,
1414 bool suspended)
1415{
1416 sp<AudioCommand> command = new AudioCommand();
1417 command->mCommand = SET_EFFECT_SUSPENDED;
1418 sp<SetEffectSuspendedData> data = new SetEffectSuspendedData();
1419 data->mEffectId = effectId;
1420 data->mSessionId = sessionId;
1421 data->mSuspended = suspended;
1422 command->mParam = data;
1423 ALOGV("AudioCommandThread() adding set suspended effectId %d sessionId %d suspended %d",
1424 effectId, sessionId, suspended);
1425 sendCommand(command);
1426}
1427
1428
Eric Laurentd7fe0862018-07-14 16:48:01 -07001429void AudioPolicyService::AudioCommandThread::stopOutputCommand(audio_port_handle_t portId)
Eric Laurentbfb1b832013-01-07 09:53:42 -08001430{
Eric Laurent0ede8922014-05-09 18:04:42 -07001431 sp<AudioCommand> command = new AudioCommand();
Eric Laurentbfb1b832013-01-07 09:53:42 -08001432 command->mCommand = STOP_OUTPUT;
Eric Laurent0ede8922014-05-09 18:04:42 -07001433 sp<StopOutputData> data = new StopOutputData();
Eric Laurentd7fe0862018-07-14 16:48:01 -07001434 data->mPortId = portId;
Jesper Tragardh48412dc2014-03-24 14:12:43 +01001435 command->mParam = data;
Eric Laurentd7fe0862018-07-14 16:48:01 -07001436 ALOGV("AudioCommandThread() adding stop output portId %d", portId);
Eric Laurent0ede8922014-05-09 18:04:42 -07001437 sendCommand(command);
Eric Laurentbfb1b832013-01-07 09:53:42 -08001438}
1439
Eric Laurentd7fe0862018-07-14 16:48:01 -07001440void AudioPolicyService::AudioCommandThread::releaseOutputCommand(audio_port_handle_t portId)
Eric Laurentbfb1b832013-01-07 09:53:42 -08001441{
Eric Laurent0ede8922014-05-09 18:04:42 -07001442 sp<AudioCommand> command = new AudioCommand();
Eric Laurentbfb1b832013-01-07 09:53:42 -08001443 command->mCommand = RELEASE_OUTPUT;
Eric Laurent0ede8922014-05-09 18:04:42 -07001444 sp<ReleaseOutputData> data = new ReleaseOutputData();
Eric Laurentd7fe0862018-07-14 16:48:01 -07001445 data->mPortId = portId;
Jesper Tragardh48412dc2014-03-24 14:12:43 +01001446 command->mParam = data;
Eric Laurentd7fe0862018-07-14 16:48:01 -07001447 ALOGV("AudioCommandThread() adding release output portId %d", portId);
Eric Laurent0ede8922014-05-09 18:04:42 -07001448 sendCommand(command);
1449}
1450
Eric Laurent951f4552014-05-20 10:48:17 -07001451status_t AudioPolicyService::AudioCommandThread::createAudioPatchCommand(
1452 const struct audio_patch *patch,
1453 audio_patch_handle_t *handle,
1454 int delayMs)
1455{
1456 status_t status = NO_ERROR;
1457
1458 sp<AudioCommand> command = new AudioCommand();
1459 command->mCommand = CREATE_AUDIO_PATCH;
1460 CreateAudioPatchData *data = new CreateAudioPatchData();
1461 data->mPatch = *patch;
1462 data->mHandle = *handle;
1463 command->mParam = data;
1464 command->mWaitStatus = true;
1465 ALOGV("AudioCommandThread() adding create patch delay %d", delayMs);
1466 status = sendCommand(command, delayMs);
1467 if (status == NO_ERROR) {
1468 *handle = data->mHandle;
1469 }
1470 return status;
1471}
1472
1473status_t AudioPolicyService::AudioCommandThread::releaseAudioPatchCommand(audio_patch_handle_t handle,
1474 int delayMs)
1475{
1476 sp<AudioCommand> command = new AudioCommand();
1477 command->mCommand = RELEASE_AUDIO_PATCH;
1478 ReleaseAudioPatchData *data = new ReleaseAudioPatchData();
1479 data->mHandle = handle;
1480 command->mParam = data;
1481 command->mWaitStatus = true;
1482 ALOGV("AudioCommandThread() adding release patch delay %d", delayMs);
1483 return sendCommand(command, delayMs);
1484}
1485
Eric Laurentb52c1522014-05-20 11:27:36 -07001486void AudioPolicyService::AudioCommandThread::updateAudioPortListCommand()
1487{
1488 sp<AudioCommand> command = new AudioCommand();
1489 command->mCommand = UPDATE_AUDIOPORT_LIST;
1490 ALOGV("AudioCommandThread() adding update audio port list");
1491 sendCommand(command);
1492}
1493
1494void AudioPolicyService::AudioCommandThread::updateAudioPatchListCommand()
1495{
1496 sp<AudioCommand>command = new AudioCommand();
1497 command->mCommand = UPDATE_AUDIOPATCH_LIST;
1498 ALOGV("AudioCommandThread() adding update audio patch list");
1499 sendCommand(command);
1500}
1501
François Gaffiecfe17322018-11-07 13:41:29 +01001502void AudioPolicyService::AudioCommandThread::changeAudioVolumeGroupCommand(volume_group_t group,
1503 int flags)
1504{
1505 sp<AudioCommand>command = new AudioCommand();
1506 command->mCommand = CHANGED_AUDIOVOLUMEGROUP;
1507 AudioVolumeGroupData *data= new AudioVolumeGroupData();
1508 data->mGroup = group;
1509 data->mFlags = flags;
1510 command->mParam = data;
1511 ALOGV("AudioCommandThread() adding audio volume group changed");
1512 sendCommand(command);
1513}
1514
Eric Laurente1715a42014-05-20 11:30:42 -07001515status_t AudioPolicyService::AudioCommandThread::setAudioPortConfigCommand(
1516 const struct audio_port_config *config, int delayMs)
1517{
1518 sp<AudioCommand> command = new AudioCommand();
1519 command->mCommand = SET_AUDIOPORT_CONFIG;
1520 SetAudioPortConfigData *data = new SetAudioPortConfigData();
1521 data->mConfig = *config;
1522 command->mParam = data;
1523 command->mWaitStatus = true;
1524 ALOGV("AudioCommandThread() adding set port config delay %d", delayMs);
1525 return sendCommand(command, delayMs);
1526}
1527
Jean-Michel Trivide801052015-04-14 19:10:14 -07001528void AudioPolicyService::AudioCommandThread::dynamicPolicyMixStateUpdateCommand(
Chih-Hung Hsiehe964d4e2016-08-09 14:31:32 -07001529 const String8& regId, int32_t state)
Jean-Michel Trivide801052015-04-14 19:10:14 -07001530{
1531 sp<AudioCommand> command = new AudioCommand();
1532 command->mCommand = DYN_POLICY_MIX_STATE_UPDATE;
1533 DynPolicyMixStateUpdateData *data = new DynPolicyMixStateUpdateData();
1534 data->mRegId = regId;
1535 data->mState = state;
1536 command->mParam = data;
1537 ALOGV("AudioCommandThread() sending dynamic policy mix (id=%s) state update to %d",
1538 regId.string(), state);
1539 sendCommand(command);
1540}
1541
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -08001542void AudioPolicyService::AudioCommandThread::recordingConfigurationUpdateCommand(
Eric Laurenta9f86652018-11-28 17:23:11 -08001543 int event,
1544 const record_client_info_t *clientInfo,
1545 const audio_config_base_t *clientConfig,
1546 std::vector<effect_descriptor_t> clientEffects,
1547 const audio_config_base_t *deviceConfig,
1548 std::vector<effect_descriptor_t> effects,
1549 audio_patch_handle_t patchHandle,
1550 audio_source_t source)
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -08001551{
1552 sp<AudioCommand>command = new AudioCommand();
1553 command->mCommand = RECORDING_CONFIGURATION_UPDATE;
1554 RecordingConfigurationUpdateData *data = new RecordingConfigurationUpdateData();
1555 data->mEvent = event;
Jean-Michel Triviac4e4292016-12-22 11:39:31 -08001556 data->mClientInfo = *clientInfo;
Jean-Michel Trivi7281aa92016-02-17 15:33:40 -08001557 data->mClientConfig = *clientConfig;
Eric Laurenta9f86652018-11-28 17:23:11 -08001558 data->mClientEffects = clientEffects;
Jean-Michel Trivi7281aa92016-02-17 15:33:40 -08001559 data->mDeviceConfig = *deviceConfig;
Eric Laurenta9f86652018-11-28 17:23:11 -08001560 data->mEffects = effects;
Jean-Michel Trivi8c7cf3b2016-02-25 17:08:24 -08001561 data->mPatchHandle = patchHandle;
Eric Laurenta9f86652018-11-28 17:23:11 -08001562 data->mSource = source;
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -08001563 command->mParam = data;
Jean-Michel Triviac4e4292016-12-22 11:39:31 -08001564 ALOGV("AudioCommandThread() adding recording configuration update event %d, source %d uid %u",
1565 event, clientInfo->source, clientInfo->uid);
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -08001566 sendCommand(command);
1567}
1568
Eric Laurent0ede8922014-05-09 18:04:42 -07001569status_t AudioPolicyService::AudioCommandThread::sendCommand(sp<AudioCommand>& command, int delayMs)
1570{
1571 {
1572 Mutex::Autolock _l(mLock);
1573 insertCommand_l(command, delayMs);
1574 mWaitWorkCV.signal();
1575 }
1576 Mutex::Autolock _l(command->mLock);
1577 while (command->mWaitStatus) {
1578 nsecs_t timeOutNs = kAudioCommandTimeoutNs + milliseconds(delayMs);
1579 if (command->mCond.waitRelative(command->mLock, timeOutNs) != NO_ERROR) {
1580 command->mStatus = TIMED_OUT;
1581 command->mWaitStatus = false;
1582 }
1583 }
1584 return command->mStatus;
Eric Laurentbfb1b832013-01-07 09:53:42 -08001585}
1586
Mathias Agopian65ab4712010-07-14 17:59:35 -07001587// insertCommand_l() must be called with mLock held
Eric Laurent0ede8922014-05-09 18:04:42 -07001588void AudioPolicyService::AudioCommandThread::insertCommand_l(sp<AudioCommand>& command, int delayMs)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001589{
Glenn Kasten8d6a2442012-02-08 14:04:28 -08001590 ssize_t i; // not size_t because i will count down to -1
Eric Laurent0ede8922014-05-09 18:04:42 -07001591 Vector < sp<AudioCommand> > removedCommands;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001592 command->mTime = systemTime() + milliseconds(delayMs);
1593
1594 // acquire wake lock to make sure delayed commands are processed
Eric Laurentbfb1b832013-01-07 09:53:42 -08001595 if (mAudioCommands.isEmpty()) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07001596 acquire_wake_lock(PARTIAL_WAKE_LOCK, mName.string());
1597 }
1598
1599 // check same pending commands with later time stamps and eliminate them
Ivan Lozano5ff158f2017-10-30 09:06:24 -07001600 for (i = (ssize_t)mAudioCommands.size()-1; i >= 0; i--) {
Eric Laurent0ede8922014-05-09 18:04:42 -07001601 sp<AudioCommand> command2 = mAudioCommands[i];
Mathias Agopian65ab4712010-07-14 17:59:35 -07001602 // commands are sorted by increasing time stamp: no need to scan the rest of mAudioCommands
1603 if (command2->mTime <= command->mTime) break;
Eric Laurente45b48a2014-09-04 16:40:57 -07001604
1605 // create audio patch or release audio patch commands are equivalent
1606 // with regard to filtering
1607 if ((command->mCommand == CREATE_AUDIO_PATCH) ||
1608 (command->mCommand == RELEASE_AUDIO_PATCH)) {
1609 if ((command2->mCommand != CREATE_AUDIO_PATCH) &&
1610 (command2->mCommand != RELEASE_AUDIO_PATCH)) {
1611 continue;
1612 }
1613 } else if (command2->mCommand != command->mCommand) continue;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001614
1615 switch (command->mCommand) {
1616 case SET_PARAMETERS: {
Eric Laurent0ede8922014-05-09 18:04:42 -07001617 ParametersData *data = (ParametersData *)command->mParam.get();
1618 ParametersData *data2 = (ParametersData *)command2->mParam.get();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001619 if (data->mIO != data2->mIO) break;
Steve Block3856b092011-10-20 11:56:00 +01001620 ALOGV("Comparing parameter command %s to new command %s",
Eric Laurentde070132010-07-13 04:45:46 -07001621 data2->mKeyValuePairs.string(), data->mKeyValuePairs.string());
Mathias Agopian65ab4712010-07-14 17:59:35 -07001622 AudioParameter param = AudioParameter(data->mKeyValuePairs);
1623 AudioParameter param2 = AudioParameter(data2->mKeyValuePairs);
1624 for (size_t j = 0; j < param.size(); j++) {
Glenn Kastene53b9ea2012-03-12 16:29:55 -07001625 String8 key;
1626 String8 value;
1627 param.getAt(j, key, value);
1628 for (size_t k = 0; k < param2.size(); k++) {
1629 String8 key2;
1630 String8 value2;
1631 param2.getAt(k, key2, value2);
1632 if (key2 == key) {
1633 param2.remove(key2);
1634 ALOGV("Filtering out parameter %s", key2.string());
1635 break;
1636 }
1637 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001638 }
1639 // if all keys have been filtered out, remove the command.
1640 // otherwise, update the key value pairs
1641 if (param2.size() == 0) {
1642 removedCommands.add(command2);
1643 } else {
1644 data2->mKeyValuePairs = param2.toString();
1645 }
Eric Laurent21e54562013-09-23 12:08:05 -07001646 command->mTime = command2->mTime;
1647 // force delayMs to non 0 so that code below does not request to wait for
1648 // command status as the command is now delayed
1649 delayMs = 1;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001650 } break;
1651
1652 case SET_VOLUME: {
Eric Laurent0ede8922014-05-09 18:04:42 -07001653 VolumeData *data = (VolumeData *)command->mParam.get();
1654 VolumeData *data2 = (VolumeData *)command2->mParam.get();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001655 if (data->mIO != data2->mIO) break;
1656 if (data->mStream != data2->mStream) break;
Steve Block3856b092011-10-20 11:56:00 +01001657 ALOGV("Filtering out volume command on output %d for stream %d",
Eric Laurentde070132010-07-13 04:45:46 -07001658 data->mIO, data->mStream);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001659 removedCommands.add(command2);
Eric Laurent21e54562013-09-23 12:08:05 -07001660 command->mTime = command2->mTime;
1661 // force delayMs to non 0 so that code below does not request to wait for
1662 // command status as the command is now delayed
1663 delayMs = 1;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001664 } break;
Eric Laurente45b48a2014-09-04 16:40:57 -07001665
Eric Laurentbaf35fe2016-07-27 15:36:53 -07001666 case SET_VOICE_VOLUME: {
1667 VoiceVolumeData *data = (VoiceVolumeData *)command->mParam.get();
1668 VoiceVolumeData *data2 = (VoiceVolumeData *)command2->mParam.get();
1669 ALOGV("Filtering out voice volume command value %f replaced by %f",
1670 data2->mVolume, data->mVolume);
1671 removedCommands.add(command2);
1672 command->mTime = command2->mTime;
1673 // force delayMs to non 0 so that code below does not request to wait for
1674 // command status as the command is now delayed
1675 delayMs = 1;
1676 } break;
1677
Eric Laurente45b48a2014-09-04 16:40:57 -07001678 case CREATE_AUDIO_PATCH:
1679 case RELEASE_AUDIO_PATCH: {
1680 audio_patch_handle_t handle;
Haynes Mathew Georgea2d4a6d2014-10-13 13:05:22 -07001681 struct audio_patch patch;
Eric Laurente45b48a2014-09-04 16:40:57 -07001682 if (command->mCommand == CREATE_AUDIO_PATCH) {
1683 handle = ((CreateAudioPatchData *)command->mParam.get())->mHandle;
Haynes Mathew Georgea2d4a6d2014-10-13 13:05:22 -07001684 patch = ((CreateAudioPatchData *)command->mParam.get())->mPatch;
Eric Laurente45b48a2014-09-04 16:40:57 -07001685 } else {
1686 handle = ((ReleaseAudioPatchData *)command->mParam.get())->mHandle;
Mikhail Naganov7be71d22018-05-23 16:51:46 -07001687 memset(&patch, 0, sizeof(patch));
Eric Laurente45b48a2014-09-04 16:40:57 -07001688 }
1689 audio_patch_handle_t handle2;
Haynes Mathew Georgea2d4a6d2014-10-13 13:05:22 -07001690 struct audio_patch patch2;
Eric Laurente45b48a2014-09-04 16:40:57 -07001691 if (command2->mCommand == CREATE_AUDIO_PATCH) {
1692 handle2 = ((CreateAudioPatchData *)command2->mParam.get())->mHandle;
Haynes Mathew Georgea2d4a6d2014-10-13 13:05:22 -07001693 patch2 = ((CreateAudioPatchData *)command2->mParam.get())->mPatch;
Eric Laurente45b48a2014-09-04 16:40:57 -07001694 } else {
1695 handle2 = ((ReleaseAudioPatchData *)command2->mParam.get())->mHandle;
Glenn Kastenf60b6b62015-07-06 10:53:26 -07001696 memset(&patch2, 0, sizeof(patch2));
Eric Laurente45b48a2014-09-04 16:40:57 -07001697 }
1698 if (handle != handle2) break;
Haynes Mathew Georgea2d4a6d2014-10-13 13:05:22 -07001699 /* Filter CREATE_AUDIO_PATCH commands only when they are issued for
1700 same output. */
1701 if( (command->mCommand == CREATE_AUDIO_PATCH) &&
1702 (command2->mCommand == CREATE_AUDIO_PATCH) ) {
1703 bool isOutputDiff = false;
1704 if (patch.num_sources == patch2.num_sources) {
1705 for (unsigned count = 0; count < patch.num_sources; count++) {
1706 if (patch.sources[count].id != patch2.sources[count].id) {
1707 isOutputDiff = true;
1708 break;
1709 }
1710 }
1711 if (isOutputDiff)
1712 break;
1713 }
1714 }
Eric Laurente45b48a2014-09-04 16:40:57 -07001715 ALOGV("Filtering out %s audio patch command for handle %d",
1716 (command->mCommand == CREATE_AUDIO_PATCH) ? "create" : "release", handle);
1717 removedCommands.add(command2);
1718 command->mTime = command2->mTime;
1719 // force delayMs to non 0 so that code below does not request to wait for
1720 // command status as the command is now delayed
1721 delayMs = 1;
1722 } break;
1723
Jean-Michel Trivide801052015-04-14 19:10:14 -07001724 case DYN_POLICY_MIX_STATE_UPDATE: {
1725
1726 } break;
1727
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -08001728 case RECORDING_CONFIGURATION_UPDATE: {
1729
1730 } break;
1731
Mathias Agopian65ab4712010-07-14 17:59:35 -07001732 default:
1733 break;
1734 }
1735 }
1736
1737 // remove filtered commands
1738 for (size_t j = 0; j < removedCommands.size(); j++) {
1739 // removed commands always have time stamps greater than current command
1740 for (size_t k = i + 1; k < mAudioCommands.size(); k++) {
Eric Laurent0ede8922014-05-09 18:04:42 -07001741 if (mAudioCommands[k].get() == removedCommands[j].get()) {
Steve Block3856b092011-10-20 11:56:00 +01001742 ALOGV("suppressing command: %d", mAudioCommands[k]->mCommand);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001743 mAudioCommands.removeAt(k);
1744 break;
1745 }
1746 }
1747 }
1748 removedCommands.clear();
1749
Eric Laurentaa79bef2015-01-15 14:33:51 -08001750 // Disable wait for status if delay is not 0.
1751 // Except for create audio patch command because the returned patch handle
1752 // is needed by audio policy manager
1753 if (delayMs != 0 && command->mCommand != CREATE_AUDIO_PATCH) {
Eric Laurentcec4abb2012-07-03 12:23:02 -07001754 command->mWaitStatus = false;
1755 }
Eric Laurentcec4abb2012-07-03 12:23:02 -07001756
Mathias Agopian65ab4712010-07-14 17:59:35 -07001757 // insert command at the right place according to its time stamp
Eric Laurent1e693b52014-07-09 15:03:28 -07001758 ALOGV("inserting command: %d at index %zd, num commands %zu",
1759 command->mCommand, i+1, mAudioCommands.size());
Mathias Agopian65ab4712010-07-14 17:59:35 -07001760 mAudioCommands.insertAt(command, i + 1);
1761}
1762
1763void AudioPolicyService::AudioCommandThread::exit()
1764{
Steve Block3856b092011-10-20 11:56:00 +01001765 ALOGV("AudioCommandThread::exit");
Mathias Agopian65ab4712010-07-14 17:59:35 -07001766 {
1767 AutoMutex _l(mLock);
1768 requestExit();
1769 mWaitWorkCV.signal();
1770 }
Zach Janga754b4f2015-10-27 01:29:34 +00001771 // Note that we can call it from the thread loop if all other references have been released
1772 // but it will safely return WOULD_BLOCK in this case
Mathias Agopian65ab4712010-07-14 17:59:35 -07001773 requestExitAndWait();
1774}
1775
1776void AudioPolicyService::AudioCommandThread::AudioCommand::dump(char* buffer, size_t size)
1777{
1778 snprintf(buffer, size, " %02d %06d.%03d %01u %p\n",
1779 mCommand,
1780 (int)ns2s(mTime),
1781 (int)ns2ms(mTime)%1000,
1782 mWaitStatus,
Eric Laurent0ede8922014-05-09 18:04:42 -07001783 mParam.get());
Mathias Agopian65ab4712010-07-14 17:59:35 -07001784}
1785
Dima Zavinfce7a472011-04-19 22:30:36 -07001786/******* helpers for the service_ops callbacks defined below *********/
1787void AudioPolicyService::setParameters(audio_io_handle_t ioHandle,
1788 const char *keyValuePairs,
1789 int delayMs)
1790{
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001791 mAudioCommandThread->parametersCommand(ioHandle, keyValuePairs,
Dima Zavinfce7a472011-04-19 22:30:36 -07001792 delayMs);
1793}
1794
1795int AudioPolicyService::setStreamVolume(audio_stream_type_t stream,
1796 float volume,
1797 audio_io_handle_t output,
1798 int delayMs)
1799{
Glenn Kastenfff6d712012-01-12 16:38:12 -08001800 return (int)mAudioCommandThread->volumeCommand(stream, volume,
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001801 output, delayMs);
Dima Zavinfce7a472011-04-19 22:30:36 -07001802}
1803
Dima Zavinfce7a472011-04-19 22:30:36 -07001804int AudioPolicyService::setVoiceVolume(float volume, int delayMs)
1805{
1806 return (int)mAudioCommandThread->voiceVolumeCommand(volume, delayMs);
1807}
1808
Eric Laurentb20cf7d2019-04-05 19:37:34 -07001809void AudioPolicyService::setEffectSuspended(int effectId,
1810 audio_session_t sessionId,
1811 bool suspended)
1812{
1813 mAudioCommandThread->setEffectSuspendedCommand(effectId, sessionId, suspended);
1814}
1815
1816
Dima Zavinfce7a472011-04-19 22:30:36 -07001817extern "C" {
Eric Laurent2d388ec2014-03-07 13:25:54 -08001818audio_module_handle_t aps_load_hw_module(void *service __unused,
1819 const char *name);
1820audio_io_handle_t aps_open_output(void *service __unused,
Eric Laurenta4c5a552012-03-29 10:12:40 -07001821 audio_devices_t *pDevices,
1822 uint32_t *pSamplingRate,
1823 audio_format_t *pFormat,
1824 audio_channel_mask_t *pChannelMask,
1825 uint32_t *pLatencyMs,
Eric Laurent2d388ec2014-03-07 13:25:54 -08001826 audio_output_flags_t flags);
Eric Laurenta4c5a552012-03-29 10:12:40 -07001827
Eric Laurent2d388ec2014-03-07 13:25:54 -08001828audio_io_handle_t aps_open_output_on_module(void *service __unused,
Eric Laurenta4c5a552012-03-29 10:12:40 -07001829 audio_module_handle_t module,
1830 audio_devices_t *pDevices,
1831 uint32_t *pSamplingRate,
1832 audio_format_t *pFormat,
1833 audio_channel_mask_t *pChannelMask,
1834 uint32_t *pLatencyMs,
Richard Fitzgeraldad3af332013-03-25 16:54:37 +00001835 audio_output_flags_t flags,
Eric Laurent2d388ec2014-03-07 13:25:54 -08001836 const audio_offload_info_t *offloadInfo);
1837audio_io_handle_t aps_open_dup_output(void *service __unused,
Dima Zavinfce7a472011-04-19 22:30:36 -07001838 audio_io_handle_t output1,
Eric Laurent2d388ec2014-03-07 13:25:54 -08001839 audio_io_handle_t output2);
1840int aps_close_output(void *service __unused, audio_io_handle_t output);
1841int aps_suspend_output(void *service __unused, audio_io_handle_t output);
1842int aps_restore_output(void *service __unused, audio_io_handle_t output);
1843audio_io_handle_t aps_open_input(void *service __unused,
Eric Laurenta4c5a552012-03-29 10:12:40 -07001844 audio_devices_t *pDevices,
1845 uint32_t *pSamplingRate,
1846 audio_format_t *pFormat,
1847 audio_channel_mask_t *pChannelMask,
Eric Laurent2d388ec2014-03-07 13:25:54 -08001848 audio_in_acoustics_t acoustics __unused);
1849audio_io_handle_t aps_open_input_on_module(void *service __unused,
Eric Laurenta4c5a552012-03-29 10:12:40 -07001850 audio_module_handle_t module,
1851 audio_devices_t *pDevices,
1852 uint32_t *pSamplingRate,
1853 audio_format_t *pFormat,
Eric Laurent2d388ec2014-03-07 13:25:54 -08001854 audio_channel_mask_t *pChannelMask);
1855int aps_close_input(void *service __unused, audio_io_handle_t input);
1856int aps_invalidate_stream(void *service __unused, audio_stream_type_t stream);
Glenn Kastend848eb42016-03-08 13:42:11 -08001857int aps_move_effects(void *service __unused, audio_session_t session,
Dima Zavinfce7a472011-04-19 22:30:36 -07001858 audio_io_handle_t src_output,
Eric Laurent2d388ec2014-03-07 13:25:54 -08001859 audio_io_handle_t dst_output);
1860char * aps_get_parameters(void *service __unused, audio_io_handle_t io_handle,
1861 const char *keys);
1862void aps_set_parameters(void *service, audio_io_handle_t io_handle,
1863 const char *kv_pairs, int delay_ms);
1864int aps_set_stream_volume(void *service, audio_stream_type_t stream,
Dima Zavinfce7a472011-04-19 22:30:36 -07001865 float volume, audio_io_handle_t output,
Eric Laurent2d388ec2014-03-07 13:25:54 -08001866 int delay_ms);
Eric Laurent2d388ec2014-03-07 13:25:54 -08001867int aps_set_voice_volume(void *service, float volume, int delay_ms);
1868};
Dima Zavinfce7a472011-04-19 22:30:36 -07001869
Mikhail Naganov1b2a7942017-12-08 10:18:09 -08001870} // namespace android