blob: cd0637cfa0e295a5bed6dec2e153a099095e3fcf [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)
Ricardo Correaac26cf72020-01-06 14:43:38 -0800537 && !(isSensitiveActive && !(isLatestSensitive || current->canCaptureCallOrOutput))
538 && !(isInCall && !current->canCaptureCallOrOutput);
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) &&
Ricardo Correaac26cf72020-01-06 14:43:38 -0800558 (!(isSensitiveActive || isInCall) || current->canCaptureCallOrOutput)) {
Eric Laurent1ff16a72019-03-14 18:35:04 -0700559 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
Ricardo Correaac26cf72020-01-06 14:43:38 -0800570 && (!(isSensitiveActive || isInCall) || current->canCaptureCallOrOutput)) {
Eric Laurent47670c92019-08-28 16:59:05 -0700571 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
Ricardo Correaac26cf72020-01-06 14:43:38 -0800583 if (onlyHotwordActive && !(isInCall && !current->canCaptureCallOrOutput)) {
Eric Laurent4e947da2019-10-17 15:24:06 -0700584 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;
Mikhail Naganov88b30d22020-03-09 19:43:13 +00001280 case AUDIO_MODULES_UPDATE: {
1281 ALOGV("AudioCommandThread() processing audio modules update");
1282 svc = mService.promote();
1283 if (svc == 0) {
1284 break;
1285 }
1286 mLock.unlock();
1287 svc->doOnNewAudioModulesAvailable();
1288 mLock.lock();
1289 } break;
Eric Laurentb20cf7d2019-04-05 19:37:34 -07001290
Mathias Agopian65ab4712010-07-14 17:59:35 -07001291 default:
Steve Block5ff1dd52012-01-05 23:22:43 +00001292 ALOGW("AudioCommandThread() unknown command %d", command->mCommand);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001293 }
Eric Laurent0ede8922014-05-09 18:04:42 -07001294 {
1295 Mutex::Autolock _l(command->mLock);
1296 if (command->mWaitStatus) {
1297 command->mWaitStatus = false;
1298 command->mCond.signal();
1299 }
1300 }
Eric Laurentd7eda8d2016-02-02 17:18:39 -08001301 waitTime = -1;
Zach Janga754b4f2015-10-27 01:29:34 +00001302 // release mLock before releasing strong reference on the service as
1303 // AudioPolicyService destructor calls AudioCommandThread::exit() which
1304 // acquires mLock.
1305 mLock.unlock();
1306 svc.clear();
1307 mLock.lock();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001308 } else {
1309 waitTime = mAudioCommands[0]->mTime - curTime;
1310 break;
1311 }
1312 }
Zach Janga754b4f2015-10-27 01:29:34 +00001313
1314 // release delayed commands wake lock if the queue is empty
1315 if (mAudioCommands.isEmpty()) {
Ricardo Garcia05f2fdc2014-07-24 15:48:24 -07001316 release_wake_lock(mName.string());
Zach Janga754b4f2015-10-27 01:29:34 +00001317 }
1318
1319 // At this stage we have either an empty command queue or the first command in the queue
1320 // has a finite delay. So unless we are exiting it is safe to wait.
1321 if (!exitPending()) {
Eric Laurent59a89232014-06-08 14:14:17 -07001322 ALOGV("AudioCommandThread() going to sleep");
Eric Laurentd7eda8d2016-02-02 17:18:39 -08001323 if (waitTime == -1) {
1324 mWaitWorkCV.wait(mLock);
1325 } else {
1326 mWaitWorkCV.waitRelative(mLock, waitTime);
1327 }
Eric Laurent59a89232014-06-08 14:14:17 -07001328 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001329 }
Ricardo Garcia05f2fdc2014-07-24 15:48:24 -07001330 // release delayed commands wake lock before quitting
1331 if (!mAudioCommands.isEmpty()) {
1332 release_wake_lock(mName.string());
1333 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001334 mLock.unlock();
1335 return false;
1336}
1337
1338status_t AudioPolicyService::AudioCommandThread::dump(int fd)
1339{
1340 const size_t SIZE = 256;
1341 char buffer[SIZE];
1342 String8 result;
1343
1344 snprintf(buffer, SIZE, "AudioCommandThread %p Dump\n", this);
1345 result.append(buffer);
1346 write(fd, result.string(), result.size());
1347
Mikhail Naganov959e2d02019-03-28 11:08:19 -07001348 bool locked = dumpTryLock(mLock);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001349 if (!locked) {
1350 String8 result2(kCmdDeadlockedString);
1351 write(fd, result2.string(), result2.size());
1352 }
1353
1354 snprintf(buffer, SIZE, "- Commands:\n");
1355 result = String8(buffer);
1356 result.append(" Command Time Wait pParam\n");
Glenn Kasten8d6a2442012-02-08 14:04:28 -08001357 for (size_t i = 0; i < mAudioCommands.size(); i++) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07001358 mAudioCommands[i]->dump(buffer, SIZE);
1359 result.append(buffer);
1360 }
1361 result.append(" Last Command\n");
Eric Laurent0ede8922014-05-09 18:04:42 -07001362 if (mLastCommand != 0) {
1363 mLastCommand->dump(buffer, SIZE);
1364 result.append(buffer);
1365 } else {
1366 result.append(" none\n");
1367 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001368
1369 write(fd, result.string(), result.size());
1370
1371 if (locked) mLock.unlock();
1372
1373 return NO_ERROR;
1374}
1375
Glenn Kastenfff6d712012-01-12 16:38:12 -08001376status_t AudioPolicyService::AudioCommandThread::volumeCommand(audio_stream_type_t stream,
Eric Laurentde070132010-07-13 04:45:46 -07001377 float volume,
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001378 audio_io_handle_t output,
Eric Laurentde070132010-07-13 04:45:46 -07001379 int delayMs)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001380{
Eric Laurent0ede8922014-05-09 18:04:42 -07001381 sp<AudioCommand> command = new AudioCommand();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001382 command->mCommand = SET_VOLUME;
Eric Laurent0ede8922014-05-09 18:04:42 -07001383 sp<VolumeData> data = new VolumeData();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001384 data->mStream = stream;
1385 data->mVolume = volume;
1386 data->mIO = output;
1387 command->mParam = data;
Eric Laurent0ede8922014-05-09 18:04:42 -07001388 command->mWaitStatus = true;
Steve Block3856b092011-10-20 11:56:00 +01001389 ALOGV("AudioCommandThread() adding set volume stream %d, volume %f, output %d",
Eric Laurentde070132010-07-13 04:45:46 -07001390 stream, volume, output);
Eric Laurent0ede8922014-05-09 18:04:42 -07001391 return sendCommand(command, delayMs);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001392}
1393
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001394status_t AudioPolicyService::AudioCommandThread::parametersCommand(audio_io_handle_t ioHandle,
Dima Zavinfce7a472011-04-19 22:30:36 -07001395 const char *keyValuePairs,
Eric Laurentde070132010-07-13 04:45:46 -07001396 int delayMs)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001397{
Eric Laurent0ede8922014-05-09 18:04:42 -07001398 sp<AudioCommand> command = new AudioCommand();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001399 command->mCommand = SET_PARAMETERS;
Eric Laurent0ede8922014-05-09 18:04:42 -07001400 sp<ParametersData> data = new ParametersData();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001401 data->mIO = ioHandle;
Dima Zavinfce7a472011-04-19 22:30:36 -07001402 data->mKeyValuePairs = String8(keyValuePairs);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001403 command->mParam = data;
Eric Laurent0ede8922014-05-09 18:04:42 -07001404 command->mWaitStatus = true;
Steve Block3856b092011-10-20 11:56:00 +01001405 ALOGV("AudioCommandThread() adding set parameter string %s, io %d ,delay %d",
Dima Zavinfce7a472011-04-19 22:30:36 -07001406 keyValuePairs, ioHandle, delayMs);
Eric Laurent0ede8922014-05-09 18:04:42 -07001407 return sendCommand(command, delayMs);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001408}
1409
1410status_t AudioPolicyService::AudioCommandThread::voiceVolumeCommand(float volume, int delayMs)
1411{
Eric Laurent0ede8922014-05-09 18:04:42 -07001412 sp<AudioCommand> command = new AudioCommand();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001413 command->mCommand = SET_VOICE_VOLUME;
Eric Laurent0ede8922014-05-09 18:04:42 -07001414 sp<VoiceVolumeData> data = new VoiceVolumeData();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001415 data->mVolume = volume;
1416 command->mParam = data;
Eric Laurent0ede8922014-05-09 18:04:42 -07001417 command->mWaitStatus = true;
Steve Block3856b092011-10-20 11:56:00 +01001418 ALOGV("AudioCommandThread() adding set voice volume volume %f", volume);
Eric Laurent0ede8922014-05-09 18:04:42 -07001419 return sendCommand(command, delayMs);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001420}
1421
Eric Laurentb20cf7d2019-04-05 19:37:34 -07001422void AudioPolicyService::AudioCommandThread::setEffectSuspendedCommand(int effectId,
1423 audio_session_t sessionId,
1424 bool suspended)
1425{
1426 sp<AudioCommand> command = new AudioCommand();
1427 command->mCommand = SET_EFFECT_SUSPENDED;
1428 sp<SetEffectSuspendedData> data = new SetEffectSuspendedData();
1429 data->mEffectId = effectId;
1430 data->mSessionId = sessionId;
1431 data->mSuspended = suspended;
1432 command->mParam = data;
1433 ALOGV("AudioCommandThread() adding set suspended effectId %d sessionId %d suspended %d",
1434 effectId, sessionId, suspended);
1435 sendCommand(command);
1436}
1437
1438
Eric Laurentd7fe0862018-07-14 16:48:01 -07001439void AudioPolicyService::AudioCommandThread::stopOutputCommand(audio_port_handle_t portId)
Eric Laurentbfb1b832013-01-07 09:53:42 -08001440{
Eric Laurent0ede8922014-05-09 18:04:42 -07001441 sp<AudioCommand> command = new AudioCommand();
Eric Laurentbfb1b832013-01-07 09:53:42 -08001442 command->mCommand = STOP_OUTPUT;
Eric Laurent0ede8922014-05-09 18:04:42 -07001443 sp<StopOutputData> data = new StopOutputData();
Eric Laurentd7fe0862018-07-14 16:48:01 -07001444 data->mPortId = portId;
Jesper Tragardh48412dc2014-03-24 14:12:43 +01001445 command->mParam = data;
Eric Laurentd7fe0862018-07-14 16:48:01 -07001446 ALOGV("AudioCommandThread() adding stop output portId %d", portId);
Eric Laurent0ede8922014-05-09 18:04:42 -07001447 sendCommand(command);
Eric Laurentbfb1b832013-01-07 09:53:42 -08001448}
1449
Eric Laurentd7fe0862018-07-14 16:48:01 -07001450void AudioPolicyService::AudioCommandThread::releaseOutputCommand(audio_port_handle_t portId)
Eric Laurentbfb1b832013-01-07 09:53:42 -08001451{
Eric Laurent0ede8922014-05-09 18:04:42 -07001452 sp<AudioCommand> command = new AudioCommand();
Eric Laurentbfb1b832013-01-07 09:53:42 -08001453 command->mCommand = RELEASE_OUTPUT;
Eric Laurent0ede8922014-05-09 18:04:42 -07001454 sp<ReleaseOutputData> data = new ReleaseOutputData();
Eric Laurentd7fe0862018-07-14 16:48:01 -07001455 data->mPortId = portId;
Jesper Tragardh48412dc2014-03-24 14:12:43 +01001456 command->mParam = data;
Eric Laurentd7fe0862018-07-14 16:48:01 -07001457 ALOGV("AudioCommandThread() adding release output portId %d", portId);
Eric Laurent0ede8922014-05-09 18:04:42 -07001458 sendCommand(command);
1459}
1460
Eric Laurent951f4552014-05-20 10:48:17 -07001461status_t AudioPolicyService::AudioCommandThread::createAudioPatchCommand(
1462 const struct audio_patch *patch,
1463 audio_patch_handle_t *handle,
1464 int delayMs)
1465{
1466 status_t status = NO_ERROR;
1467
1468 sp<AudioCommand> command = new AudioCommand();
1469 command->mCommand = CREATE_AUDIO_PATCH;
1470 CreateAudioPatchData *data = new CreateAudioPatchData();
1471 data->mPatch = *patch;
1472 data->mHandle = *handle;
1473 command->mParam = data;
1474 command->mWaitStatus = true;
1475 ALOGV("AudioCommandThread() adding create patch delay %d", delayMs);
1476 status = sendCommand(command, delayMs);
1477 if (status == NO_ERROR) {
1478 *handle = data->mHandle;
1479 }
1480 return status;
1481}
1482
1483status_t AudioPolicyService::AudioCommandThread::releaseAudioPatchCommand(audio_patch_handle_t handle,
1484 int delayMs)
1485{
1486 sp<AudioCommand> command = new AudioCommand();
1487 command->mCommand = RELEASE_AUDIO_PATCH;
1488 ReleaseAudioPatchData *data = new ReleaseAudioPatchData();
1489 data->mHandle = handle;
1490 command->mParam = data;
1491 command->mWaitStatus = true;
1492 ALOGV("AudioCommandThread() adding release patch delay %d", delayMs);
1493 return sendCommand(command, delayMs);
1494}
1495
Eric Laurentb52c1522014-05-20 11:27:36 -07001496void AudioPolicyService::AudioCommandThread::updateAudioPortListCommand()
1497{
1498 sp<AudioCommand> command = new AudioCommand();
1499 command->mCommand = UPDATE_AUDIOPORT_LIST;
1500 ALOGV("AudioCommandThread() adding update audio port list");
1501 sendCommand(command);
1502}
1503
1504void AudioPolicyService::AudioCommandThread::updateAudioPatchListCommand()
1505{
1506 sp<AudioCommand>command = new AudioCommand();
1507 command->mCommand = UPDATE_AUDIOPATCH_LIST;
1508 ALOGV("AudioCommandThread() adding update audio patch list");
1509 sendCommand(command);
1510}
1511
François Gaffiecfe17322018-11-07 13:41:29 +01001512void AudioPolicyService::AudioCommandThread::changeAudioVolumeGroupCommand(volume_group_t group,
1513 int flags)
1514{
1515 sp<AudioCommand>command = new AudioCommand();
1516 command->mCommand = CHANGED_AUDIOVOLUMEGROUP;
1517 AudioVolumeGroupData *data= new AudioVolumeGroupData();
1518 data->mGroup = group;
1519 data->mFlags = flags;
1520 command->mParam = data;
1521 ALOGV("AudioCommandThread() adding audio volume group changed");
1522 sendCommand(command);
1523}
1524
Eric Laurente1715a42014-05-20 11:30:42 -07001525status_t AudioPolicyService::AudioCommandThread::setAudioPortConfigCommand(
1526 const struct audio_port_config *config, int delayMs)
1527{
1528 sp<AudioCommand> command = new AudioCommand();
1529 command->mCommand = SET_AUDIOPORT_CONFIG;
1530 SetAudioPortConfigData *data = new SetAudioPortConfigData();
1531 data->mConfig = *config;
1532 command->mParam = data;
1533 command->mWaitStatus = true;
1534 ALOGV("AudioCommandThread() adding set port config delay %d", delayMs);
1535 return sendCommand(command, delayMs);
1536}
1537
Jean-Michel Trivide801052015-04-14 19:10:14 -07001538void AudioPolicyService::AudioCommandThread::dynamicPolicyMixStateUpdateCommand(
Chih-Hung Hsiehe964d4e2016-08-09 14:31:32 -07001539 const String8& regId, int32_t state)
Jean-Michel Trivide801052015-04-14 19:10:14 -07001540{
1541 sp<AudioCommand> command = new AudioCommand();
1542 command->mCommand = DYN_POLICY_MIX_STATE_UPDATE;
1543 DynPolicyMixStateUpdateData *data = new DynPolicyMixStateUpdateData();
1544 data->mRegId = regId;
1545 data->mState = state;
1546 command->mParam = data;
1547 ALOGV("AudioCommandThread() sending dynamic policy mix (id=%s) state update to %d",
1548 regId.string(), state);
1549 sendCommand(command);
1550}
1551
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -08001552void AudioPolicyService::AudioCommandThread::recordingConfigurationUpdateCommand(
Eric Laurenta9f86652018-11-28 17:23:11 -08001553 int event,
1554 const record_client_info_t *clientInfo,
1555 const audio_config_base_t *clientConfig,
1556 std::vector<effect_descriptor_t> clientEffects,
1557 const audio_config_base_t *deviceConfig,
1558 std::vector<effect_descriptor_t> effects,
1559 audio_patch_handle_t patchHandle,
1560 audio_source_t source)
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -08001561{
1562 sp<AudioCommand>command = new AudioCommand();
1563 command->mCommand = RECORDING_CONFIGURATION_UPDATE;
1564 RecordingConfigurationUpdateData *data = new RecordingConfigurationUpdateData();
1565 data->mEvent = event;
Jean-Michel Triviac4e4292016-12-22 11:39:31 -08001566 data->mClientInfo = *clientInfo;
Jean-Michel Trivi7281aa92016-02-17 15:33:40 -08001567 data->mClientConfig = *clientConfig;
Eric Laurenta9f86652018-11-28 17:23:11 -08001568 data->mClientEffects = clientEffects;
Jean-Michel Trivi7281aa92016-02-17 15:33:40 -08001569 data->mDeviceConfig = *deviceConfig;
Eric Laurenta9f86652018-11-28 17:23:11 -08001570 data->mEffects = effects;
Jean-Michel Trivi8c7cf3b2016-02-25 17:08:24 -08001571 data->mPatchHandle = patchHandle;
Eric Laurenta9f86652018-11-28 17:23:11 -08001572 data->mSource = source;
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -08001573 command->mParam = data;
Jean-Michel Triviac4e4292016-12-22 11:39:31 -08001574 ALOGV("AudioCommandThread() adding recording configuration update event %d, source %d uid %u",
1575 event, clientInfo->source, clientInfo->uid);
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -08001576 sendCommand(command);
1577}
1578
Mikhail Naganov88b30d22020-03-09 19:43:13 +00001579void AudioPolicyService::AudioCommandThread::audioModulesUpdateCommand()
1580{
1581 sp<AudioCommand> command = new AudioCommand();
1582 command->mCommand = AUDIO_MODULES_UPDATE;
1583 sendCommand(command);
1584}
1585
Eric Laurent0ede8922014-05-09 18:04:42 -07001586status_t AudioPolicyService::AudioCommandThread::sendCommand(sp<AudioCommand>& command, int delayMs)
1587{
1588 {
1589 Mutex::Autolock _l(mLock);
1590 insertCommand_l(command, delayMs);
1591 mWaitWorkCV.signal();
1592 }
1593 Mutex::Autolock _l(command->mLock);
1594 while (command->mWaitStatus) {
1595 nsecs_t timeOutNs = kAudioCommandTimeoutNs + milliseconds(delayMs);
1596 if (command->mCond.waitRelative(command->mLock, timeOutNs) != NO_ERROR) {
1597 command->mStatus = TIMED_OUT;
1598 command->mWaitStatus = false;
1599 }
1600 }
1601 return command->mStatus;
Eric Laurentbfb1b832013-01-07 09:53:42 -08001602}
1603
Mathias Agopian65ab4712010-07-14 17:59:35 -07001604// insertCommand_l() must be called with mLock held
Eric Laurent0ede8922014-05-09 18:04:42 -07001605void AudioPolicyService::AudioCommandThread::insertCommand_l(sp<AudioCommand>& command, int delayMs)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001606{
Glenn Kasten8d6a2442012-02-08 14:04:28 -08001607 ssize_t i; // not size_t because i will count down to -1
Eric Laurent0ede8922014-05-09 18:04:42 -07001608 Vector < sp<AudioCommand> > removedCommands;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001609 command->mTime = systemTime() + milliseconds(delayMs);
1610
1611 // acquire wake lock to make sure delayed commands are processed
Eric Laurentbfb1b832013-01-07 09:53:42 -08001612 if (mAudioCommands.isEmpty()) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07001613 acquire_wake_lock(PARTIAL_WAKE_LOCK, mName.string());
1614 }
1615
1616 // check same pending commands with later time stamps and eliminate them
Ivan Lozano5ff158f2017-10-30 09:06:24 -07001617 for (i = (ssize_t)mAudioCommands.size()-1; i >= 0; i--) {
Eric Laurent0ede8922014-05-09 18:04:42 -07001618 sp<AudioCommand> command2 = mAudioCommands[i];
Mathias Agopian65ab4712010-07-14 17:59:35 -07001619 // commands are sorted by increasing time stamp: no need to scan the rest of mAudioCommands
1620 if (command2->mTime <= command->mTime) break;
Eric Laurente45b48a2014-09-04 16:40:57 -07001621
1622 // create audio patch or release audio patch commands are equivalent
1623 // with regard to filtering
1624 if ((command->mCommand == CREATE_AUDIO_PATCH) ||
1625 (command->mCommand == RELEASE_AUDIO_PATCH)) {
1626 if ((command2->mCommand != CREATE_AUDIO_PATCH) &&
1627 (command2->mCommand != RELEASE_AUDIO_PATCH)) {
1628 continue;
1629 }
1630 } else if (command2->mCommand != command->mCommand) continue;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001631
1632 switch (command->mCommand) {
1633 case SET_PARAMETERS: {
Eric Laurent0ede8922014-05-09 18:04:42 -07001634 ParametersData *data = (ParametersData *)command->mParam.get();
1635 ParametersData *data2 = (ParametersData *)command2->mParam.get();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001636 if (data->mIO != data2->mIO) break;
Steve Block3856b092011-10-20 11:56:00 +01001637 ALOGV("Comparing parameter command %s to new command %s",
Eric Laurentde070132010-07-13 04:45:46 -07001638 data2->mKeyValuePairs.string(), data->mKeyValuePairs.string());
Mathias Agopian65ab4712010-07-14 17:59:35 -07001639 AudioParameter param = AudioParameter(data->mKeyValuePairs);
1640 AudioParameter param2 = AudioParameter(data2->mKeyValuePairs);
1641 for (size_t j = 0; j < param.size(); j++) {
Glenn Kastene53b9ea2012-03-12 16:29:55 -07001642 String8 key;
1643 String8 value;
1644 param.getAt(j, key, value);
1645 for (size_t k = 0; k < param2.size(); k++) {
1646 String8 key2;
1647 String8 value2;
1648 param2.getAt(k, key2, value2);
1649 if (key2 == key) {
1650 param2.remove(key2);
1651 ALOGV("Filtering out parameter %s", key2.string());
1652 break;
1653 }
1654 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001655 }
1656 // if all keys have been filtered out, remove the command.
1657 // otherwise, update the key value pairs
1658 if (param2.size() == 0) {
1659 removedCommands.add(command2);
1660 } else {
1661 data2->mKeyValuePairs = param2.toString();
1662 }
Eric Laurent21e54562013-09-23 12:08:05 -07001663 command->mTime = command2->mTime;
1664 // force delayMs to non 0 so that code below does not request to wait for
1665 // command status as the command is now delayed
1666 delayMs = 1;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001667 } break;
1668
1669 case SET_VOLUME: {
Eric Laurent0ede8922014-05-09 18:04:42 -07001670 VolumeData *data = (VolumeData *)command->mParam.get();
1671 VolumeData *data2 = (VolumeData *)command2->mParam.get();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001672 if (data->mIO != data2->mIO) break;
1673 if (data->mStream != data2->mStream) break;
Steve Block3856b092011-10-20 11:56:00 +01001674 ALOGV("Filtering out volume command on output %d for stream %d",
Eric Laurentde070132010-07-13 04:45:46 -07001675 data->mIO, data->mStream);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001676 removedCommands.add(command2);
Eric Laurent21e54562013-09-23 12:08:05 -07001677 command->mTime = command2->mTime;
1678 // force delayMs to non 0 so that code below does not request to wait for
1679 // command status as the command is now delayed
1680 delayMs = 1;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001681 } break;
Eric Laurente45b48a2014-09-04 16:40:57 -07001682
Eric Laurentbaf35fe2016-07-27 15:36:53 -07001683 case SET_VOICE_VOLUME: {
1684 VoiceVolumeData *data = (VoiceVolumeData *)command->mParam.get();
1685 VoiceVolumeData *data2 = (VoiceVolumeData *)command2->mParam.get();
1686 ALOGV("Filtering out voice volume command value %f replaced by %f",
1687 data2->mVolume, data->mVolume);
1688 removedCommands.add(command2);
1689 command->mTime = command2->mTime;
1690 // force delayMs to non 0 so that code below does not request to wait for
1691 // command status as the command is now delayed
1692 delayMs = 1;
1693 } break;
1694
Eric Laurente45b48a2014-09-04 16:40:57 -07001695 case CREATE_AUDIO_PATCH:
1696 case RELEASE_AUDIO_PATCH: {
1697 audio_patch_handle_t handle;
Haynes Mathew Georgea2d4a6d2014-10-13 13:05:22 -07001698 struct audio_patch patch;
Eric Laurente45b48a2014-09-04 16:40:57 -07001699 if (command->mCommand == CREATE_AUDIO_PATCH) {
1700 handle = ((CreateAudioPatchData *)command->mParam.get())->mHandle;
Haynes Mathew Georgea2d4a6d2014-10-13 13:05:22 -07001701 patch = ((CreateAudioPatchData *)command->mParam.get())->mPatch;
Eric Laurente45b48a2014-09-04 16:40:57 -07001702 } else {
1703 handle = ((ReleaseAudioPatchData *)command->mParam.get())->mHandle;
Mikhail Naganov7be71d22018-05-23 16:51:46 -07001704 memset(&patch, 0, sizeof(patch));
Eric Laurente45b48a2014-09-04 16:40:57 -07001705 }
1706 audio_patch_handle_t handle2;
Haynes Mathew Georgea2d4a6d2014-10-13 13:05:22 -07001707 struct audio_patch patch2;
Eric Laurente45b48a2014-09-04 16:40:57 -07001708 if (command2->mCommand == CREATE_AUDIO_PATCH) {
1709 handle2 = ((CreateAudioPatchData *)command2->mParam.get())->mHandle;
Haynes Mathew Georgea2d4a6d2014-10-13 13:05:22 -07001710 patch2 = ((CreateAudioPatchData *)command2->mParam.get())->mPatch;
Eric Laurente45b48a2014-09-04 16:40:57 -07001711 } else {
1712 handle2 = ((ReleaseAudioPatchData *)command2->mParam.get())->mHandle;
Glenn Kastenf60b6b62015-07-06 10:53:26 -07001713 memset(&patch2, 0, sizeof(patch2));
Eric Laurente45b48a2014-09-04 16:40:57 -07001714 }
1715 if (handle != handle2) break;
Haynes Mathew Georgea2d4a6d2014-10-13 13:05:22 -07001716 /* Filter CREATE_AUDIO_PATCH commands only when they are issued for
1717 same output. */
1718 if( (command->mCommand == CREATE_AUDIO_PATCH) &&
1719 (command2->mCommand == CREATE_AUDIO_PATCH) ) {
1720 bool isOutputDiff = false;
1721 if (patch.num_sources == patch2.num_sources) {
1722 for (unsigned count = 0; count < patch.num_sources; count++) {
1723 if (patch.sources[count].id != patch2.sources[count].id) {
1724 isOutputDiff = true;
1725 break;
1726 }
1727 }
1728 if (isOutputDiff)
1729 break;
1730 }
1731 }
Eric Laurente45b48a2014-09-04 16:40:57 -07001732 ALOGV("Filtering out %s audio patch command for handle %d",
1733 (command->mCommand == CREATE_AUDIO_PATCH) ? "create" : "release", handle);
1734 removedCommands.add(command2);
1735 command->mTime = command2->mTime;
1736 // force delayMs to non 0 so that code below does not request to wait for
1737 // command status as the command is now delayed
1738 delayMs = 1;
1739 } break;
1740
Jean-Michel Trivide801052015-04-14 19:10:14 -07001741 case DYN_POLICY_MIX_STATE_UPDATE: {
1742
1743 } break;
1744
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -08001745 case RECORDING_CONFIGURATION_UPDATE: {
1746
1747 } break;
1748
Mathias Agopian65ab4712010-07-14 17:59:35 -07001749 default:
1750 break;
1751 }
1752 }
1753
1754 // remove filtered commands
1755 for (size_t j = 0; j < removedCommands.size(); j++) {
1756 // removed commands always have time stamps greater than current command
1757 for (size_t k = i + 1; k < mAudioCommands.size(); k++) {
Eric Laurent0ede8922014-05-09 18:04:42 -07001758 if (mAudioCommands[k].get() == removedCommands[j].get()) {
Steve Block3856b092011-10-20 11:56:00 +01001759 ALOGV("suppressing command: %d", mAudioCommands[k]->mCommand);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001760 mAudioCommands.removeAt(k);
1761 break;
1762 }
1763 }
1764 }
1765 removedCommands.clear();
1766
Eric Laurentaa79bef2015-01-15 14:33:51 -08001767 // Disable wait for status if delay is not 0.
1768 // Except for create audio patch command because the returned patch handle
1769 // is needed by audio policy manager
1770 if (delayMs != 0 && command->mCommand != CREATE_AUDIO_PATCH) {
Eric Laurentcec4abb2012-07-03 12:23:02 -07001771 command->mWaitStatus = false;
1772 }
Eric Laurentcec4abb2012-07-03 12:23:02 -07001773
Mathias Agopian65ab4712010-07-14 17:59:35 -07001774 // insert command at the right place according to its time stamp
Eric Laurent1e693b52014-07-09 15:03:28 -07001775 ALOGV("inserting command: %d at index %zd, num commands %zu",
1776 command->mCommand, i+1, mAudioCommands.size());
Mathias Agopian65ab4712010-07-14 17:59:35 -07001777 mAudioCommands.insertAt(command, i + 1);
1778}
1779
1780void AudioPolicyService::AudioCommandThread::exit()
1781{
Steve Block3856b092011-10-20 11:56:00 +01001782 ALOGV("AudioCommandThread::exit");
Mathias Agopian65ab4712010-07-14 17:59:35 -07001783 {
1784 AutoMutex _l(mLock);
1785 requestExit();
1786 mWaitWorkCV.signal();
1787 }
Zach Janga754b4f2015-10-27 01:29:34 +00001788 // Note that we can call it from the thread loop if all other references have been released
1789 // but it will safely return WOULD_BLOCK in this case
Mathias Agopian65ab4712010-07-14 17:59:35 -07001790 requestExitAndWait();
1791}
1792
1793void AudioPolicyService::AudioCommandThread::AudioCommand::dump(char* buffer, size_t size)
1794{
1795 snprintf(buffer, size, " %02d %06d.%03d %01u %p\n",
1796 mCommand,
1797 (int)ns2s(mTime),
1798 (int)ns2ms(mTime)%1000,
1799 mWaitStatus,
Eric Laurent0ede8922014-05-09 18:04:42 -07001800 mParam.get());
Mathias Agopian65ab4712010-07-14 17:59:35 -07001801}
1802
Dima Zavinfce7a472011-04-19 22:30:36 -07001803/******* helpers for the service_ops callbacks defined below *********/
1804void AudioPolicyService::setParameters(audio_io_handle_t ioHandle,
1805 const char *keyValuePairs,
1806 int delayMs)
1807{
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001808 mAudioCommandThread->parametersCommand(ioHandle, keyValuePairs,
Dima Zavinfce7a472011-04-19 22:30:36 -07001809 delayMs);
1810}
1811
1812int AudioPolicyService::setStreamVolume(audio_stream_type_t stream,
1813 float volume,
1814 audio_io_handle_t output,
1815 int delayMs)
1816{
Glenn Kastenfff6d712012-01-12 16:38:12 -08001817 return (int)mAudioCommandThread->volumeCommand(stream, volume,
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001818 output, delayMs);
Dima Zavinfce7a472011-04-19 22:30:36 -07001819}
1820
Dima Zavinfce7a472011-04-19 22:30:36 -07001821int AudioPolicyService::setVoiceVolume(float volume, int delayMs)
1822{
1823 return (int)mAudioCommandThread->voiceVolumeCommand(volume, delayMs);
1824}
1825
Eric Laurentb20cf7d2019-04-05 19:37:34 -07001826void AudioPolicyService::setEffectSuspended(int effectId,
1827 audio_session_t sessionId,
1828 bool suspended)
1829{
1830 mAudioCommandThread->setEffectSuspendedCommand(effectId, sessionId, suspended);
1831}
1832
Mikhail Naganov88b30d22020-03-09 19:43:13 +00001833void AudioPolicyService::onNewAudioModulesAvailable()
1834{
1835 mAudioCommandThread->audioModulesUpdateCommand();
1836}
1837
Eric Laurentb20cf7d2019-04-05 19:37:34 -07001838
Dima Zavinfce7a472011-04-19 22:30:36 -07001839extern "C" {
Eric Laurent2d388ec2014-03-07 13:25:54 -08001840audio_module_handle_t aps_load_hw_module(void *service __unused,
1841 const char *name);
1842audio_io_handle_t aps_open_output(void *service __unused,
Eric Laurenta4c5a552012-03-29 10:12:40 -07001843 audio_devices_t *pDevices,
1844 uint32_t *pSamplingRate,
1845 audio_format_t *pFormat,
1846 audio_channel_mask_t *pChannelMask,
1847 uint32_t *pLatencyMs,
Eric Laurent2d388ec2014-03-07 13:25:54 -08001848 audio_output_flags_t flags);
Eric Laurenta4c5a552012-03-29 10:12:40 -07001849
Eric Laurent2d388ec2014-03-07 13:25:54 -08001850audio_io_handle_t aps_open_output_on_module(void *service __unused,
Eric Laurenta4c5a552012-03-29 10:12:40 -07001851 audio_module_handle_t module,
1852 audio_devices_t *pDevices,
1853 uint32_t *pSamplingRate,
1854 audio_format_t *pFormat,
1855 audio_channel_mask_t *pChannelMask,
1856 uint32_t *pLatencyMs,
Richard Fitzgeraldad3af332013-03-25 16:54:37 +00001857 audio_output_flags_t flags,
Eric Laurent2d388ec2014-03-07 13:25:54 -08001858 const audio_offload_info_t *offloadInfo);
1859audio_io_handle_t aps_open_dup_output(void *service __unused,
Dima Zavinfce7a472011-04-19 22:30:36 -07001860 audio_io_handle_t output1,
Eric Laurent2d388ec2014-03-07 13:25:54 -08001861 audio_io_handle_t output2);
1862int aps_close_output(void *service __unused, audio_io_handle_t output);
1863int aps_suspend_output(void *service __unused, audio_io_handle_t output);
1864int aps_restore_output(void *service __unused, audio_io_handle_t output);
1865audio_io_handle_t aps_open_input(void *service __unused,
Eric Laurenta4c5a552012-03-29 10:12:40 -07001866 audio_devices_t *pDevices,
1867 uint32_t *pSamplingRate,
1868 audio_format_t *pFormat,
1869 audio_channel_mask_t *pChannelMask,
Eric Laurent2d388ec2014-03-07 13:25:54 -08001870 audio_in_acoustics_t acoustics __unused);
1871audio_io_handle_t aps_open_input_on_module(void *service __unused,
Eric Laurenta4c5a552012-03-29 10:12:40 -07001872 audio_module_handle_t module,
1873 audio_devices_t *pDevices,
1874 uint32_t *pSamplingRate,
1875 audio_format_t *pFormat,
Eric Laurent2d388ec2014-03-07 13:25:54 -08001876 audio_channel_mask_t *pChannelMask);
1877int aps_close_input(void *service __unused, audio_io_handle_t input);
1878int aps_invalidate_stream(void *service __unused, audio_stream_type_t stream);
Glenn Kastend848eb42016-03-08 13:42:11 -08001879int aps_move_effects(void *service __unused, audio_session_t session,
Dima Zavinfce7a472011-04-19 22:30:36 -07001880 audio_io_handle_t src_output,
Eric Laurent2d388ec2014-03-07 13:25:54 -08001881 audio_io_handle_t dst_output);
1882char * aps_get_parameters(void *service __unused, audio_io_handle_t io_handle,
1883 const char *keys);
1884void aps_set_parameters(void *service, audio_io_handle_t io_handle,
1885 const char *kv_pairs, int delay_ms);
1886int aps_set_stream_volume(void *service, audio_stream_type_t stream,
Dima Zavinfce7a472011-04-19 22:30:36 -07001887 float volume, audio_io_handle_t output,
Eric Laurent2d388ec2014-03-07 13:25:54 -08001888 int delay_ms);
Eric Laurent2d388ec2014-03-07 13:25:54 -08001889int aps_set_voice_volume(void *service, float volume, int delay_ms);
1890};
Dima Zavinfce7a472011-04-19 22:30:36 -07001891
Mikhail Naganov1b2a7942017-12-08 10:18:09 -08001892} // namespace android