blob: 9b61e748037bf9cccc380aff9ba9f748cc4850ae [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()
Ytai Ben-Tsvi85093d52020-03-26 09:41:15 -070060 : BnAudioPolicyService(),
Ytai Ben-Tsvi85093d52020-03-26 09:41:15 -070061 mAudioPolicyManager(NULL),
62 mAudioPolicyClient(NULL),
63 mPhoneState(AUDIO_MODE_INVALID),
64 mCaptureStateNotifier(false) {
Eric Laurentf5ada6e2014-10-09 17:49:00 -070065}
66
67void AudioPolicyService::onFirstRef()
68{
Eric Laurent8b1e80b2014-10-07 09:08:47 -070069 {
70 Mutex::Autolock _l(mLock);
Eric Laurent93575202011-01-18 18:39:02 -080071
Eric Laurent8b1e80b2014-10-07 09:08:47 -070072 // start audio commands thread
73 mAudioCommandThread = new AudioCommandThread(String8("ApmAudio"), this);
74 // start output activity command thread
75 mOutputCommandThread = new AudioCommandThread(String8("ApmOutput"), this);
Eric Laurentdce54a12014-03-10 12:19:46 -070076
Eric Laurent8b1e80b2014-10-07 09:08:47 -070077 mAudioPolicyClient = new AudioPolicyClient(this);
78 mAudioPolicyManager = createAudioPolicyManager(mAudioPolicyClient);
Eric Laurent8b1e80b2014-10-07 09:08:47 -070079 }
bryant_liuba2b4392014-06-11 16:49:30 +080080 // load audio processing modules
Mikhail Naganov12b716c2020-04-30 22:37:43 +000081 sp<AudioPolicyEffects> audioPolicyEffects = new AudioPolicyEffects();
82 sp<UidPolicy> uidPolicy = new UidPolicy(this);
83 sp<SensorPrivacyPolicy> sensorPrivacyPolicy = new SensorPrivacyPolicy(this);
Eric Laurent8b1e80b2014-10-07 09:08:47 -070084 {
85 Mutex::Autolock _l(mLock);
86 mAudioPolicyEffects = audioPolicyEffects;
Mikhail Naganov12b716c2020-04-30 22:37:43 +000087 mUidPolicy = uidPolicy;
88 mSensorPrivacyPolicy = sensorPrivacyPolicy;
Eric Laurent8b1e80b2014-10-07 09:08:47 -070089 }
Mikhail Naganov12b716c2020-04-30 22:37:43 +000090 uidPolicy->registerSelf();
91 sensorPrivacyPolicy->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();
Michael Groovercfd28302018-12-11 19:16:46 -0800106 mSensorPrivacyPolicy->unregisterSelf();
Mikhail Naganov12b716c2020-04-30 22:37:43 +0000107
108 mUidPolicy.clear();
Michael Groovercfd28302018-12-11 19:16:46 -0800109 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{
Mikhail Naganov12b716c2020-04-30 22:37:43 +0000171 bool hasSameUid = false;
Eric Laurent0ebd5f92014-11-19 19:04:52 -0800172 {
173 Mutex::Autolock _l(mNotificationClientsLock);
luochaojiang908c7d72018-06-21 14:58:04 +0800174 int64_t token = ((int64_t)uid<<32) | pid;
175 mNotificationClients.removeItem(token);
luochaojiang908c7d72018-06-21 14:58:04 +0800176 for (size_t i = 0; i < mNotificationClients.size(); i++) {
177 if (mNotificationClients.valueAt(i)->uid() == uid) {
178 hasSameUid = true;
179 break;
180 }
181 }
Mikhail Naganov12b716c2020-04-30 22:37:43 +0000182 }
183 {
184 Mutex::Autolock _l(mLock);
luochaojiang908c7d72018-06-21 14:58:04 +0800185 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 Naganov12b716c2020-04-30 22:37:43 +0000380static bool dumpTryLock(Mutex& mutex) ACQUIRE(mutex) NO_THREAD_SAFETY_ANALYSIS
Mathias Agopian65ab4712010-07-14 17:59:35 -0700381{
Mikhail Naganov12b716c2020-04-30 22:37:43 +0000382 return mutex.timedLock(kDumpLockTimeoutNs) == NO_ERROR;
383}
384
385static void dumpReleaseLock(Mutex& mutex, bool locked) RELEASE(mutex) NO_THREAD_SAFETY_ANALYSIS
386{
387 if (locked) mutex.unlock();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700388}
389
390status_t AudioPolicyService::dumpInternals(int fd)
391{
392 const size_t SIZE = 256;
393 char buffer[SIZE];
394 String8 result;
395
Eric Laurentdce54a12014-03-10 12:19:46 -0700396 snprintf(buffer, SIZE, "AudioPolicyManager: %p\n", mAudioPolicyManager);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700397 result.append(buffer);
398 snprintf(buffer, SIZE, "Command Thread: %p\n", mAudioCommandThread.get());
399 result.append(buffer);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700400
Hayden Gomes524159d2019-12-23 14:41:47 -0800401 snprintf(buffer, SIZE, "Supported System Usages:\n");
402 result.append(buffer);
403 for (std::vector<audio_usage_t>::iterator it = mSupportedSystemUsages.begin();
404 it != mSupportedSystemUsages.end(); ++it) {
405 snprintf(buffer, SIZE, "\t%d\n", *it);
406 result.append(buffer);
407 }
408
Mathias Agopian65ab4712010-07-14 17:59:35 -0700409 write(fd, result.string(), result.size());
410 return NO_ERROR;
411}
412
Eric Laurente8c8b432018-10-17 10:08:02 -0700413void AudioPolicyService::updateUidStates()
Svet Ganovf4ddfef2018-01-16 07:37:58 -0800414{
Eric Laurente8c8b432018-10-17 10:08:02 -0700415 Mutex::Autolock _l(mLock);
416 updateUidStates_l();
417}
418
419void AudioPolicyService::updateUidStates_l()
420{
Eric Laurent4eb58f12018-12-07 16:41:02 -0800421// Go over all active clients and allow capture (does not force silence) in the
422// following cases:
Eric Laurent8ed73c12019-08-30 17:25:19 -0700423// The client is the assistant
Eric Laurent6ede98f2019-06-11 14:50:30 -0700424// AND an accessibility service is on TOP or a RTT call is active
Eric Laurent589171c2019-07-25 18:04:29 -0700425// AND the source is VOICE_RECOGNITION or HOTWORD
426// OR uses VOICE_RECOGNITION AND is on TOP
427// OR uses HOTWORD
Eric Laurent1ff16a72019-03-14 18:35:04 -0700428// AND there is no active privacy sensitive capture or call
429// OR client has CAPTURE_AUDIO_OUTPUT privileged permission
Eric Laurenta46bedb2018-12-07 18:01:26 -0800430// OR The client is an accessibility service
Eric Laurent589171c2019-07-25 18:04:29 -0700431// AND Is on TOP
432// AND the source is VOICE_RECOGNITION or HOTWORD
433// OR The assistant is not on TOP
434// AND there is no active privacy sensitive capture or call
435// OR client has CAPTURE_AUDIO_OUTPUT privileged permission
Eric Laurenta171e352019-05-07 13:04:45 -0700436// AND is on TOP
Eric Laurenta46bedb2018-12-07 18:01:26 -0800437// AND the source is VOICE_RECOGNITION or HOTWORD
Eric Laurent1ff16a72019-03-14 18:35:04 -0700438// OR the client source is virtual (remote submix, call audio TX or RX...)
Eric Laurent4e947da2019-10-17 15:24:06 -0700439// OR the client source is HOTWORD
440// AND is on TOP
441// OR all active clients are using HOTWORD source
442// AND no call is active
443// OR client has CAPTURE_AUDIO_OUTPUT privileged permission
Kohsuke Yatoha623a132020-03-24 20:10:26 -0700444// OR the client is the current InputMethodService
445// AND a RTT call is active AND the source is VOICE_RECOGNITION
Eric Laurenta171e352019-05-07 13:04:45 -0700446// OR Any client
Eric Laurenta46bedb2018-12-07 18:01:26 -0800447// AND The assistant is not on TOP
Eric Laurenta171e352019-05-07 13:04:45 -0700448// AND is on TOP or latest started
Eric Laurent1ff16a72019-03-14 18:35:04 -0700449// AND there is no active privacy sensitive capture or call
Eric Laurent589171c2019-07-25 18:04:29 -0700450// OR client has CAPTURE_AUDIO_OUTPUT privileged permission
Eric Laurent4eb58f12018-12-07 16:41:02 -0800451
Eric Laurent4e947da2019-10-17 15:24:06 -0700452
Eric Laurent4eb58f12018-12-07 16:41:02 -0800453 sp<AudioRecordClient> topActive;
454 sp<AudioRecordClient> latestActive;
Eric Laurentc21d5692020-02-25 10:24:36 -0800455 sp<AudioRecordClient> topSensitiveActive;
Eric Laurent4eb58f12018-12-07 16:41:02 -0800456 sp<AudioRecordClient> latestSensitiveActive;
Eric Laurent1ff16a72019-03-14 18:35:04 -0700457
Eric Laurenta46bedb2018-12-07 18:01:26 -0800458 nsecs_t topStartNs = 0;
459 nsecs_t latestStartNs = 0;
Eric Laurentc21d5692020-02-25 10:24:36 -0800460 nsecs_t topSensitiveStartNs = 0;
Eric Laurent4eb58f12018-12-07 16:41:02 -0800461 nsecs_t latestSensitiveStartNs = 0;
462 bool isA11yOnTop = mUidPolicy->isA11yOnTop();
463 bool isAssistantOnTop = false;
464 bool isSensitiveActive = false;
Eric Laurent1ff16a72019-03-14 18:35:04 -0700465 bool isInCall = mPhoneState == AUDIO_MODE_IN_CALL;
Eric Laurentc21d5692020-02-25 10:24:36 -0800466 bool isInCommunication = mPhoneState == AUDIO_MODE_IN_COMMUNICATION;
467 bool rttCallActive = (isInCall || isInCommunication)
Eric Laurent6ede98f2019-06-11 14:50:30 -0700468 && mUidPolicy->isRttEnabled();
Eric Laurent4e947da2019-10-17 15:24:06 -0700469 bool onlyHotwordActive = true;
Eric Laurent4eb58f12018-12-07 16:41:02 -0800470
Michael Groovercfd28302018-12-11 19:16:46 -0800471 // if Sensor Privacy is enabled then all recordings should be silenced.
472 if (mSensorPrivacyPolicy->isSensorPrivacyEnabled()) {
473 silenceAllRecordings_l();
474 return;
475 }
476
Eric Laurente8c8b432018-10-17 10:08:02 -0700477 for (size_t i =0; i < mAudioRecordClients.size(); i++) {
478 sp<AudioRecordClient> current = mAudioRecordClients[i];
Eric Laurent1ff16a72019-03-14 18:35:04 -0700479 if (!current->active) {
480 continue;
Eric Laurent4eb58f12018-12-07 16:41:02 -0800481 }
Eric Laurent1ff16a72019-03-14 18:35:04 -0700482
483 app_state_t appState = apmStatFromAmState(mUidPolicy->getUidState(current->uid));
484 // clients which app is in IDLE state are not eligible for top active or
485 // latest active
486 if (appState == APP_STATE_IDLE) {
487 continue;
488 }
489
Eric Laurent589171c2019-07-25 18:04:29 -0700490 bool isAccessibility = mUidPolicy->isA11yUid(current->uid);
Eric Laurentc21d5692020-02-25 10:24:36 -0800491 // Clients capturing for Accessibility services are not considered
492 // for top or latest active to avoid masking regular clients started before
493 if (!isAccessibility) {
494 bool isAssistant = mUidPolicy->isAssistantUid(current->uid);
495 bool isPrivacySensitive =
496 (current->attributes.flags & AUDIO_FLAG_CAPTURE_PRIVATE) != 0;
497 if (appState == APP_STATE_TOP) {
498 if (isPrivacySensitive) {
499 if (current->startTimeNs > topSensitiveStartNs) {
500 topSensitiveActive = current;
501 topSensitiveStartNs = current->startTimeNs;
502 }
503 } else {
504 if (current->startTimeNs > topStartNs) {
505 topActive = current;
506 topStartNs = current->startTimeNs;
507 }
508 }
509 if (isAssistant) {
510 isAssistantOnTop = true;
511 }
Eric Laurenta46bedb2018-12-07 18:01:26 -0800512 }
Eric Laurentc21d5692020-02-25 10:24:36 -0800513 // Clients capturing for HOTWORD are not considered
514 // for latest active to avoid masking regular clients started before
515 if (!(current->attributes.source == AUDIO_SOURCE_HOTWORD
516 || ((isA11yOnTop || rttCallActive) && isAssistant))) {
517 if (isPrivacySensitive) {
518 if (current->startTimeNs > latestSensitiveStartNs) {
519 latestSensitiveActive = current;
520 latestSensitiveStartNs = current->startTimeNs;
521 }
522 isSensitiveActive = true;
523 } else {
524 if (current->startTimeNs > latestStartNs) {
525 latestActive = current;
526 latestStartNs = current->startTimeNs;
527 }
528 }
Eric Laurent4eb58f12018-12-07 16:41:02 -0800529 }
530 }
Eric Laurent4e947da2019-10-17 15:24:06 -0700531 if (current->attributes.source != AUDIO_SOURCE_HOTWORD) {
532 onlyHotwordActive = false;
533 }
Eric Laurent4eb58f12018-12-07 16:41:02 -0800534 }
535
Eric Laurent1ff16a72019-03-14 18:35:04 -0700536 // if no active client with UI on Top, consider latest active as top
537 if (topActive == nullptr) {
538 topActive = latestActive;
Eric Laurentc21d5692020-02-25 10:24:36 -0800539 topStartNs = latestStartNs;
540 }
541 if (topSensitiveActive == nullptr) {
542 topSensitiveActive = latestSensitiveActive;
543 topSensitiveStartNs = latestSensitiveStartNs;
544 }
545
546 // If both privacy sensitive and regular capture are active:
547 // if the regular capture is privileged
548 // allow concurrency
549 // else
550 // favor the privacy sensitive case
551 if (topActive != nullptr && topSensitiveActive != nullptr
Ricardo Correa57a37692020-03-23 17:27:25 -0700552 && !topActive->canCaptureOutput) {
Eric Laurentc21d5692020-02-25 10:24:36 -0800553 topActive = nullptr;
Eric Laurent4eb58f12018-12-07 16:41:02 -0800554 }
555
556 for (size_t i =0; i < mAudioRecordClients.size(); i++) {
557 sp<AudioRecordClient> current = mAudioRecordClients[i];
Eric Laurent1ff16a72019-03-14 18:35:04 -0700558 if (!current->active) {
559 continue;
560 }
561
Eric Laurent4eb58f12018-12-07 16:41:02 -0800562 audio_source_t source = current->attributes.source;
Eric Laurent1ff16a72019-03-14 18:35:04 -0700563 bool isTopOrLatestActive = topActive == nullptr ? false : current->uid == topActive->uid;
Eric Laurentc21d5692020-02-25 10:24:36 -0800564 bool isTopOrLatestSensitive = topSensitiveActive == nullptr ?
565 false : current->uid == topSensitiveActive->uid;
566
Mikhail Naganov12b716c2020-04-30 22:37:43 +0000567 auto canCaptureIfInCallOrCommunication = [&](const auto &recordClient) REQUIRES(mLock) {
Ricardo Correa57a37692020-03-23 17:27:25 -0700568 bool canCaptureCall = recordClient->canCaptureOutput;
569 bool canCaptureCommunication = recordClient->canCaptureOutput
Eric Laurentc21d5692020-02-25 10:24:36 -0800570 || recordClient->uid == mPhoneStateOwnerUid
571 || isServiceUid(mPhoneStateOwnerUid);
572 return !(isInCall && !canCaptureCall)
573 && !(isInCommunication && !canCaptureCommunication);
574 };
Eric Laurent1ff16a72019-03-14 18:35:04 -0700575
576 // By default allow capture if:
577 // The assistant is not on TOP
Eric Laurenta171e352019-05-07 13:04:45 -0700578 // AND is on TOP or latest started
Eric Laurent1ff16a72019-03-14 18:35:04 -0700579 // AND there is no active privacy sensitive capture or call
580 // OR client has CAPTURE_AUDIO_OUTPUT privileged permission
581 bool allowCapture = !isAssistantOnTop
Eric Laurentc21d5692020-02-25 10:24:36 -0800582 && (isTopOrLatestActive || isTopOrLatestSensitive)
583 && !(isSensitiveActive
Ricardo Correa57a37692020-03-23 17:27:25 -0700584 && !(isTopOrLatestSensitive || current->canCaptureOutput))
Eric Laurentc21d5692020-02-25 10:24:36 -0800585 && canCaptureIfInCallOrCommunication(current);
Eric Laurent2dc962b2019-03-01 08:25:25 -0800586
587 if (isVirtualSource(source)) {
Eric Laurent1ff16a72019-03-14 18:35:04 -0700588 // Allow capture for virtual (remote submix, call audio TX or RX...) sources
589 allowCapture = true;
Eric Laurent2dc962b2019-03-01 08:25:25 -0800590 } else if (mUidPolicy->isAssistantUid(current->uid)) {
Eric Laurent1ff16a72019-03-14 18:35:04 -0700591 // For assistant allow capture if:
Eric Laurent6ede98f2019-06-11 14:50:30 -0700592 // An accessibility service is on TOP or a RTT call is active
Eric Laurent1ff16a72019-03-14 18:35:04 -0700593 // AND the source is VOICE_RECOGNITION or HOTWORD
Eric Laurenta171e352019-05-07 13:04:45 -0700594 // OR is on TOP AND uses VOICE_RECOGNITION
Eric Laurent1ff16a72019-03-14 18:35:04 -0700595 // OR uses HOTWORD
596 // AND there is no active privacy sensitive capture or call
597 // OR client has CAPTURE_AUDIO_OUTPUT privileged permission
Eric Laurent6ede98f2019-06-11 14:50:30 -0700598 if (isA11yOnTop || rttCallActive) {
Eric Laurent4eb58f12018-12-07 16:41:02 -0800599 if (source == AUDIO_SOURCE_HOTWORD || source == AUDIO_SOURCE_VOICE_RECOGNITION) {
Eric Laurent1ff16a72019-03-14 18:35:04 -0700600 allowCapture = true;
Eric Laurent4eb58f12018-12-07 16:41:02 -0800601 }
602 } else {
Eric Laurenta171e352019-05-07 13:04:45 -0700603 if (((isAssistantOnTop && source == AUDIO_SOURCE_VOICE_RECOGNITION) ||
Eric Laurentc21d5692020-02-25 10:24:36 -0800604 source == AUDIO_SOURCE_HOTWORD)
Ricardo Correa57a37692020-03-23 17:27:25 -0700605 && !(isSensitiveActive && !current->canCaptureOutput)
Eric Laurentc21d5692020-02-25 10:24:36 -0800606 && canCaptureIfInCallOrCommunication(current)) {
Eric Laurent1ff16a72019-03-14 18:35:04 -0700607 allowCapture = true;
Eric Laurent4eb58f12018-12-07 16:41:02 -0800608 }
609 }
610 } else if (mUidPolicy->isA11yUid(current->uid)) {
Eric Laurent1ff16a72019-03-14 18:35:04 -0700611 // For accessibility service allow capture if:
Eric Laurent47670c92019-08-28 16:59:05 -0700612 // The assistant is not on TOP
613 // AND there is no active privacy sensitive capture or call
Eric Laurent589171c2019-07-25 18:04:29 -0700614 // OR client has CAPTURE_AUDIO_OUTPUT privileged permission
Eric Laurent47670c92019-08-28 16:59:05 -0700615 // OR
616 // Is on TOP AND the source is VOICE_RECOGNITION or HOTWORD
617 if (!isAssistantOnTop
Ricardo Correa57a37692020-03-23 17:27:25 -0700618 && !(isSensitiveActive && !current->canCaptureOutput)
Eric Laurentc21d5692020-02-25 10:24:36 -0800619 && canCaptureIfInCallOrCommunication(current)) {
Eric Laurent47670c92019-08-28 16:59:05 -0700620 allowCapture = true;
621 }
Eric Laurent589171c2019-07-25 18:04:29 -0700622 if (isA11yOnTop) {
623 if (source == AUDIO_SOURCE_VOICE_RECOGNITION || source == AUDIO_SOURCE_HOTWORD) {
624 allowCapture = true;
625 }
Eric Laurent4eb58f12018-12-07 16:41:02 -0800626 }
Eric Laurent4e947da2019-10-17 15:24:06 -0700627 } else if (source == AUDIO_SOURCE_HOTWORD) {
628 // For HOTWORD source allow capture when not on TOP if:
629 // All active clients are using HOTWORD source
630 // AND no call is active
631 // OR client has CAPTURE_AUDIO_OUTPUT privileged permission
Eric Laurentc21d5692020-02-25 10:24:36 -0800632 if (onlyHotwordActive
633 && canCaptureIfInCallOrCommunication(current)) {
Eric Laurent4e947da2019-10-17 15:24:06 -0700634 allowCapture = true;
635 }
Kohsuke Yatoha623a132020-03-24 20:10:26 -0700636 } else if (mUidPolicy->isCurrentImeUid(current->uid)) {
637 // For current InputMethodService allow capture if:
638 // A RTT call is active AND the source is VOICE_RECOGNITION
639 if (rttCallActive && source == AUDIO_SOURCE_VOICE_RECOGNITION) {
640 allowCapture = true;
641 }
Eric Laurent4eb58f12018-12-07 16:41:02 -0800642 }
Eric Laurent5ada82e2019-08-29 17:53:54 -0700643 setAppState_l(current->portId,
Eric Laurent1ff16a72019-03-14 18:35:04 -0700644 allowCapture ? apmStatFromAmState(mUidPolicy->getUidState(current->uid)) :
645 APP_STATE_IDLE);
Eric Laurente8c8b432018-10-17 10:08:02 -0700646 }
647}
648
Michael Groovercfd28302018-12-11 19:16:46 -0800649void AudioPolicyService::silenceAllRecordings_l() {
650 for (size_t i = 0; i < mAudioRecordClients.size(); i++) {
651 sp<AudioRecordClient> current = mAudioRecordClients[i];
Eric Laurent1ff16a72019-03-14 18:35:04 -0700652 if (!isVirtualSource(current->attributes.source)) {
Eric Laurent5ada82e2019-08-29 17:53:54 -0700653 setAppState_l(current->portId, APP_STATE_IDLE);
Eric Laurent1ff16a72019-03-14 18:35:04 -0700654 }
Michael Groovercfd28302018-12-11 19:16:46 -0800655 }
656}
657
Eric Laurente8c8b432018-10-17 10:08:02 -0700658/* static */
659app_state_t AudioPolicyService::apmStatFromAmState(int amState) {
Eric Laurent1ff16a72019-03-14 18:35:04 -0700660
661 if (amState == ActivityManager::PROCESS_STATE_UNKNOWN) {
Eric Laurente8c8b432018-10-17 10:08:02 -0700662 return APP_STATE_IDLE;
Eric Laurent1ff16a72019-03-14 18:35:04 -0700663 } else if (amState <= ActivityManager::PROCESS_STATE_TOP) {
664 // include persistent services
665 return APP_STATE_TOP;
Eric Laurente8c8b432018-10-17 10:08:02 -0700666 }
667 return APP_STATE_FOREGROUND;
668}
669
Eric Laurent4eb58f12018-12-07 16:41:02 -0800670/* static */
Eric Laurent2dc962b2019-03-01 08:25:25 -0800671bool AudioPolicyService::isVirtualSource(audio_source_t source)
Eric Laurent4eb58f12018-12-07 16:41:02 -0800672{
673 switch (source) {
674 case AUDIO_SOURCE_VOICE_UPLINK:
675 case AUDIO_SOURCE_VOICE_DOWNLINK:
676 case AUDIO_SOURCE_VOICE_CALL:
Eric Laurent2dc962b2019-03-01 08:25:25 -0800677 case AUDIO_SOURCE_REMOTE_SUBMIX:
678 case AUDIO_SOURCE_FM_TUNER:
Eric Laurent68eb2122020-04-30 17:40:57 -0700679 case AUDIO_SOURCE_ECHO_REFERENCE:
Eric Laurent4eb58f12018-12-07 16:41:02 -0800680 return true;
681 default:
682 break;
683 }
684 return false;
685}
686
Eric Laurent5ada82e2019-08-29 17:53:54 -0700687void AudioPolicyService::setAppState_l(audio_port_handle_t portId, app_state_t state)
Eric Laurente8c8b432018-10-17 10:08:02 -0700688{
689 AutoCallerClear acc;
690
691 if (mAudioPolicyManager) {
Eric Laurent5ada82e2019-08-29 17:53:54 -0700692 mAudioPolicyManager->setAppState(portId, state);
Mikhail Naganoveae73eb2018-04-03 16:57:36 -0700693 }
694 sp<IAudioFlinger> af = AudioSystem::get_audio_flinger();
695 if (af) {
Eric Laurentf32108e2018-10-04 17:22:04 -0700696 bool silenced = state == APP_STATE_IDLE;
Eric Laurent5ada82e2019-08-29 17:53:54 -0700697 af->setRecordSilenced(portId, silenced);
Mikhail Naganoveae73eb2018-04-03 16:57:36 -0700698 }
Svet Ganovf4ddfef2018-01-16 07:37:58 -0800699}
700
Glenn Kasten0f11b512014-01-31 16:18:54 -0800701status_t AudioPolicyService::dump(int fd, const Vector<String16>& args __unused)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700702{
Glenn Kasten44deb052012-02-05 18:09:08 -0800703 if (!dumpAllowed()) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700704 dumpPermissionDenial(fd);
705 } else {
Mikhail Naganov12b716c2020-04-30 22:37:43 +0000706 const bool locked = dumpTryLock(mLock);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700707 if (!locked) {
708 String8 result(kDeadlockedString);
709 write(fd, result.string(), result.size());
710 }
711
712 dumpInternals(fd);
Glenn Kasten9d1f02d2012-02-08 17:47:58 -0800713 if (mAudioCommandThread != 0) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700714 mAudioCommandThread->dump(fd);
715 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700716
Eric Laurentdce54a12014-03-10 12:19:46 -0700717 if (mAudioPolicyManager) {
718 mAudioPolicyManager->dump(fd);
719 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700720
Kevin Rocard8be94972019-02-22 13:26:25 -0800721 mPackageManager.dump(fd);
722
Mikhail Naganov12b716c2020-04-30 22:37:43 +0000723 dumpReleaseLock(mLock, locked);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700724 }
725 return NO_ERROR;
726}
727
728status_t AudioPolicyService::dumpPermissionDenial(int fd)
729{
730 const size_t SIZE = 256;
731 char buffer[SIZE];
732 String8 result;
733 snprintf(buffer, SIZE, "Permission Denial: "
734 "can't dump AudioPolicyService from pid=%d, uid=%d\n",
735 IPCThreadState::self()->getCallingPid(),
736 IPCThreadState::self()->getCallingUid());
737 result.append(buffer);
738 write(fd, result.string(), result.size());
739 return NO_ERROR;
740}
741
742status_t AudioPolicyService::onTransact(
Svet Ganovf4ddfef2018-01-16 07:37:58 -0800743 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags) {
744 switch (code) {
745 case SHELL_COMMAND_TRANSACTION: {
746 int in = data.readFileDescriptor();
747 int out = data.readFileDescriptor();
748 int err = data.readFileDescriptor();
749 int argc = data.readInt32();
750 Vector<String16> args;
751 for (int i = 0; i < argc && data.dataAvail() > 0; i++) {
752 args.add(data.readString16());
753 }
754 sp<IBinder> unusedCallback;
755 sp<IResultReceiver> resultReceiver;
756 status_t status;
757 if ((status = data.readNullableStrongBinder(&unusedCallback)) != NO_ERROR) {
758 return status;
759 }
760 if ((status = data.readNullableStrongBinder(&resultReceiver)) != NO_ERROR) {
761 return status;
762 }
763 status = shellCommand(in, out, err, args);
764 if (resultReceiver != nullptr) {
765 resultReceiver->send(status);
766 }
767 return NO_ERROR;
768 }
769 }
770
Mathias Agopian65ab4712010-07-14 17:59:35 -0700771 return BnAudioPolicyService::onTransact(code, data, reply, flags);
772}
773
Svet Ganovf4ddfef2018-01-16 07:37:58 -0800774// ------------------- Shell command implementation -------------------
775
776// NOTE: This is a remote API - make sure all args are validated
777status_t AudioPolicyService::shellCommand(int in, int out, int err, Vector<String16>& args) {
778 if (!checkCallingPermission(sManageAudioPolicyPermission, nullptr, nullptr)) {
779 return PERMISSION_DENIED;
780 }
781 if (in == BAD_TYPE || out == BAD_TYPE || err == BAD_TYPE) {
782 return BAD_VALUE;
783 }
jovanakbe066e12019-09-02 11:54:39 -0700784 if (args.size() >= 3 && args[0] == String16("set-uid-state")) {
Svet Ganovf4ddfef2018-01-16 07:37:58 -0800785 return handleSetUidState(args, err);
jovanakbe066e12019-09-02 11:54:39 -0700786 } else if (args.size() >= 2 && args[0] == String16("reset-uid-state")) {
Svet Ganovf4ddfef2018-01-16 07:37:58 -0800787 return handleResetUidState(args, err);
jovanakbe066e12019-09-02 11:54:39 -0700788 } else if (args.size() >= 2 && args[0] == String16("get-uid-state")) {
Svet Ganovf4ddfef2018-01-16 07:37:58 -0800789 return handleGetUidState(args, out, err);
790 } else if (args.size() == 1 && args[0] == String16("help")) {
791 printHelp(out);
792 return NO_ERROR;
793 }
794 printHelp(err);
795 return BAD_VALUE;
796}
797
jovanakbe066e12019-09-02 11:54:39 -0700798static status_t getUidForPackage(String16 packageName, int userId, /*inout*/uid_t& uid, int err) {
799 if (userId < 0) {
800 ALOGE("Invalid user: %d", userId);
801 dprintf(err, "Invalid user: %d\n", userId);
Svet Ganovf4ddfef2018-01-16 07:37:58 -0800802 return BAD_VALUE;
803 }
jovanakbe066e12019-09-02 11:54:39 -0700804
805 PermissionController pc;
806 uid = pc.getPackageUid(packageName, 0);
807 if (uid <= 0) {
808 ALOGE("Unknown package: '%s'", String8(packageName).string());
809 dprintf(err, "Unknown package: '%s'\n", String8(packageName).string());
810 return BAD_VALUE;
811 }
812
813 uid = multiuser_get_uid(userId, uid);
814 return NO_ERROR;
815}
816
817status_t AudioPolicyService::handleSetUidState(Vector<String16>& args, int err) {
818 // Valid arg.size() is 3 or 5, args.size() is 5 with --user option.
819 if (!(args.size() == 3 || args.size() == 5)) {
820 printHelp(err);
821 return BAD_VALUE;
822 }
823
Svet Ganovf4ddfef2018-01-16 07:37:58 -0800824 bool active = false;
825 if (args[2] == String16("active")) {
826 active = true;
827 } else if ((args[2] != String16("idle"))) {
828 ALOGE("Expected active or idle but got: '%s'", String8(args[2]).string());
829 return BAD_VALUE;
830 }
jovanakbe066e12019-09-02 11:54:39 -0700831
832 int userId = 0;
833 if (args.size() >= 5 && args[3] == String16("--user")) {
834 userId = atoi(String8(args[4]));
835 }
836
837 uid_t uid;
838 if (getUidForPackage(args[1], userId, uid, err) == BAD_VALUE) {
839 return BAD_VALUE;
840 }
841
Mikhail Naganov12b716c2020-04-30 22:37:43 +0000842 sp<UidPolicy> uidPolicy;
843 {
844 Mutex::Autolock _l(mLock);
845 uidPolicy = mUidPolicy;
846 }
847 if (uidPolicy) {
848 uidPolicy->addOverrideUid(uid, active);
849 return NO_ERROR;
850 }
851 return NO_INIT;
Svet Ganovf4ddfef2018-01-16 07:37:58 -0800852}
853
854status_t AudioPolicyService::handleResetUidState(Vector<String16>& args, int err) {
jovanakbe066e12019-09-02 11:54:39 -0700855 // Valid arg.size() is 2 or 4, args.size() is 4 with --user option.
856 if (!(args.size() == 2 || args.size() == 4)) {
857 printHelp(err);
Svet Ganovf4ddfef2018-01-16 07:37:58 -0800858 return BAD_VALUE;
859 }
jovanakbe066e12019-09-02 11:54:39 -0700860
861 int userId = 0;
862 if (args.size() >= 4 && args[2] == String16("--user")) {
863 userId = atoi(String8(args[3]));
864 }
865
866 uid_t uid;
867 if (getUidForPackage(args[1], userId, uid, err) == BAD_VALUE) {
868 return BAD_VALUE;
869 }
870
Mikhail Naganov12b716c2020-04-30 22:37:43 +0000871 sp<UidPolicy> uidPolicy;
872 {
873 Mutex::Autolock _l(mLock);
874 uidPolicy = mUidPolicy;
875 }
876 if (uidPolicy) {
877 uidPolicy->removeOverrideUid(uid);
878 return NO_ERROR;
879 }
880 return NO_INIT;
Svet Ganovf4ddfef2018-01-16 07:37:58 -0800881}
882
883status_t AudioPolicyService::handleGetUidState(Vector<String16>& args, int out, int err) {
jovanakbe066e12019-09-02 11:54:39 -0700884 // Valid arg.size() is 2 or 4, args.size() is 4 with --user option.
885 if (!(args.size() == 2 || args.size() == 4)) {
886 printHelp(err);
Svet Ganovf4ddfef2018-01-16 07:37:58 -0800887 return BAD_VALUE;
888 }
jovanakbe066e12019-09-02 11:54:39 -0700889
890 int userId = 0;
891 if (args.size() >= 4 && args[2] == String16("--user")) {
892 userId = atoi(String8(args[3]));
893 }
894
895 uid_t uid;
896 if (getUidForPackage(args[1], userId, uid, err) == BAD_VALUE) {
897 return BAD_VALUE;
898 }
899
Mikhail Naganov12b716c2020-04-30 22:37:43 +0000900 sp<UidPolicy> uidPolicy;
901 {
902 Mutex::Autolock _l(mLock);
903 uidPolicy = mUidPolicy;
Svet Ganovf4ddfef2018-01-16 07:37:58 -0800904 }
Mikhail Naganov12b716c2020-04-30 22:37:43 +0000905 if (uidPolicy) {
906 return dprintf(out, uidPolicy->isUidActive(uid) ? "active\n" : "idle\n");
907 }
908 return NO_INIT;
Svet Ganovf4ddfef2018-01-16 07:37:58 -0800909}
910
911status_t AudioPolicyService::printHelp(int out) {
912 return dprintf(out, "Audio policy service commands:\n"
jovanakbe066e12019-09-02 11:54:39 -0700913 " get-uid-state <PACKAGE> [--user USER_ID] gets the uid state\n"
914 " set-uid-state <PACKAGE> <active|idle> [--user USER_ID] overrides the uid state\n"
915 " reset-uid-state <PACKAGE> [--user USER_ID] clears the uid state override\n"
Svet Ganovf4ddfef2018-01-16 07:37:58 -0800916 " help print this message\n");
917}
918
919// ----------- AudioPolicyService::UidPolicy implementation ----------
920
921void AudioPolicyService::UidPolicy::registerSelf() {
Steven Moreland2f348142019-07-02 15:59:07 -0700922 status_t res = mAm.linkToDeath(this);
923 mAm.registerUidObserver(this, ActivityManager::UID_OBSERVER_GONE
Svet Ganovf4ddfef2018-01-16 07:37:58 -0800924 | ActivityManager::UID_OBSERVER_IDLE
Eric Laurente8c8b432018-10-17 10:08:02 -0700925 | ActivityManager::UID_OBSERVER_ACTIVE
926 | ActivityManager::UID_OBSERVER_PROCSTATE,
Svet Ganovf4ddfef2018-01-16 07:37:58 -0800927 ActivityManager::PROCESS_STATE_UNKNOWN,
928 String16("audioserver"));
Mikhail Naganoveae73eb2018-04-03 16:57:36 -0700929 if (!res) {
930 Mutex::Autolock _l(mLock);
931 mObserverRegistered = true;
932 } else {
933 ALOGE("UidPolicy::registerSelf linkToDeath failed: %d", res);
Eric Laurent4eb58f12018-12-07 16:41:02 -0800934
Steven Moreland2f348142019-07-02 15:59:07 -0700935 mAm.unregisterUidObserver(this);
Mikhail Naganoveae73eb2018-04-03 16:57:36 -0700936 }
Svet Ganovf4ddfef2018-01-16 07:37:58 -0800937}
938
939void AudioPolicyService::UidPolicy::unregisterSelf() {
Steven Moreland2f348142019-07-02 15:59:07 -0700940 mAm.unlinkToDeath(this);
941 mAm.unregisterUidObserver(this);
Mikhail Naganoveae73eb2018-04-03 16:57:36 -0700942 Mutex::Autolock _l(mLock);
943 mObserverRegistered = false;
Svet Ganovf4ddfef2018-01-16 07:37:58 -0800944}
945
Mikhail Naganoveae73eb2018-04-03 16:57:36 -0700946void AudioPolicyService::UidPolicy::binderDied(__unused const wp<IBinder> &who) {
947 Mutex::Autolock _l(mLock);
948 mCachedUids.clear();
949 mObserverRegistered = false;
Svet Ganovf4ddfef2018-01-16 07:37:58 -0800950}
951
Eric Laurente8c8b432018-10-17 10:08:02 -0700952void AudioPolicyService::UidPolicy::checkRegistered() {
Mikhail Naganoveae73eb2018-04-03 16:57:36 -0700953 bool needToReregister = false;
954 {
955 Mutex::Autolock _l(mLock);
956 needToReregister = !mObserverRegistered;
Svet Ganovf4ddfef2018-01-16 07:37:58 -0800957 }
Mikhail Naganoveae73eb2018-04-03 16:57:36 -0700958 if (needToReregister) {
959 // Looks like ActivityManager has died previously, attempt to re-register.
960 registerSelf();
961 }
Eric Laurente8c8b432018-10-17 10:08:02 -0700962}
963
964bool AudioPolicyService::UidPolicy::isUidActive(uid_t uid) {
965 if (isServiceUid(uid)) return true;
966 checkRegistered();
Mikhail Naganoveae73eb2018-04-03 16:57:36 -0700967 {
968 Mutex::Autolock _l(mLock);
969 auto overrideIter = mOverrideUids.find(uid);
970 if (overrideIter != mOverrideUids.end()) {
Eric Laurente8c8b432018-10-17 10:08:02 -0700971 return overrideIter->second.first;
Mikhail Naganoveae73eb2018-04-03 16:57:36 -0700972 }
973 // In an absense of the ActivityManager, assume everything to be active.
974 if (!mObserverRegistered) return true;
975 auto cacheIter = mCachedUids.find(uid);
Mikhail Naganoveba668a2018-04-05 08:13:15 -0700976 if (cacheIter != mCachedUids.end()) {
Eric Laurente8c8b432018-10-17 10:08:02 -0700977 return cacheIter->second.first;
Mikhail Naganoveae73eb2018-04-03 16:57:36 -0700978 }
979 }
980 ActivityManager am;
Hui Yu12c7ec72020-05-04 17:40:52 +0000981 bool active = am.isUidActive(uid, String16("audioserver"));
Mikhail Naganoveae73eb2018-04-03 16:57:36 -0700982 {
983 Mutex::Autolock _l(mLock);
Eric Laurente8c8b432018-10-17 10:08:02 -0700984 mCachedUids.insert(std::pair<uid_t,
985 std::pair<bool, int>>(uid, std::pair<bool, int>(active,
986 ActivityManager::PROCESS_STATE_UNKNOWN)));
Mikhail Naganoveae73eb2018-04-03 16:57:36 -0700987 }
988 return active;
Svet Ganovf4ddfef2018-01-16 07:37:58 -0800989}
990
Eric Laurente8c8b432018-10-17 10:08:02 -0700991int AudioPolicyService::UidPolicy::getUidState(uid_t uid) {
992 if (isServiceUid(uid)) {
993 return ActivityManager::PROCESS_STATE_TOP;
994 }
995 checkRegistered();
996 {
997 Mutex::Autolock _l(mLock);
998 auto overrideIter = mOverrideUids.find(uid);
999 if (overrideIter != mOverrideUids.end()) {
1000 if (overrideIter->second.first) {
1001 if (overrideIter->second.second != ActivityManager::PROCESS_STATE_UNKNOWN) {
1002 return overrideIter->second.second;
1003 } else {
1004 auto cacheIter = mCachedUids.find(uid);
1005 if (cacheIter != mCachedUids.end()) {
1006 return cacheIter->second.second;
1007 }
1008 }
1009 }
1010 return ActivityManager::PROCESS_STATE_UNKNOWN;
1011 }
1012 // In an absense of the ActivityManager, assume everything to be active.
1013 if (!mObserverRegistered) {
1014 return ActivityManager::PROCESS_STATE_TOP;
1015 }
1016 auto cacheIter = mCachedUids.find(uid);
1017 if (cacheIter != mCachedUids.end()) {
1018 if (cacheIter->second.first) {
1019 return cacheIter->second.second;
1020 } else {
1021 return ActivityManager::PROCESS_STATE_UNKNOWN;
1022 }
1023 }
1024 }
1025 ActivityManager am;
Hui Yu12c7ec72020-05-04 17:40:52 +00001026 bool active = am.isUidActive(uid, String16("audioserver"));
Eric Laurente8c8b432018-10-17 10:08:02 -07001027 int state = ActivityManager::PROCESS_STATE_UNKNOWN;
1028 if (active) {
1029 state = am.getUidProcessState(uid, String16("audioserver"));
1030 }
1031 {
1032 Mutex::Autolock _l(mLock);
1033 mCachedUids.insert(std::pair<uid_t,
1034 std::pair<bool, int>>(uid, std::pair<bool, int>(active, state)));
1035 }
Eric Laurent4eb58f12018-12-07 16:41:02 -08001036
Eric Laurente8c8b432018-10-17 10:08:02 -07001037 return state;
1038}
1039
Mikhail Naganoveae73eb2018-04-03 16:57:36 -07001040void AudioPolicyService::UidPolicy::onUidActive(uid_t uid) {
Eric Laurente8c8b432018-10-17 10:08:02 -07001041 updateUid(&mCachedUids, uid, true, ActivityManager::PROCESS_STATE_UNKNOWN, true);
Mikhail Naganoveae73eb2018-04-03 16:57:36 -07001042}
1043
1044void AudioPolicyService::UidPolicy::onUidGone(uid_t uid, __unused bool disabled) {
Eric Laurente8c8b432018-10-17 10:08:02 -07001045 updateUid(&mCachedUids, uid, false, ActivityManager::PROCESS_STATE_UNKNOWN, false);
Mikhail Naganoveae73eb2018-04-03 16:57:36 -07001046}
1047
1048void AudioPolicyService::UidPolicy::onUidIdle(uid_t uid, __unused bool disabled) {
Eric Laurente8c8b432018-10-17 10:08:02 -07001049 updateUid(&mCachedUids, uid, false, ActivityManager::PROCESS_STATE_UNKNOWN, true);
Mikhail Naganoveae73eb2018-04-03 16:57:36 -07001050}
1051
Eric Laurente8c8b432018-10-17 10:08:02 -07001052void AudioPolicyService::UidPolicy::onUidStateChanged(uid_t uid,
1053 int32_t procState,
Hui Yu13ad0eb2019-09-09 10:27:07 -07001054 int64_t procStateSeq __unused,
1055 int32_t capability __unused) {
Eric Laurente8c8b432018-10-17 10:08:02 -07001056 if (procState != ActivityManager::PROCESS_STATE_UNKNOWN) {
1057 updateUid(&mCachedUids, uid, true, procState, true);
Mikhail Naganoveae73eb2018-04-03 16:57:36 -07001058 }
1059}
1060
1061void AudioPolicyService::UidPolicy::updateOverrideUid(uid_t uid, bool active, bool insert) {
Eric Laurente8c8b432018-10-17 10:08:02 -07001062 updateUid(&mOverrideUids, uid, active, ActivityManager::PROCESS_STATE_UNKNOWN, insert);
1063}
1064
1065void AudioPolicyService::UidPolicy::notifyService() {
1066 sp<AudioPolicyService> service = mService.promote();
1067 if (service != nullptr) {
1068 service->updateUidStates();
Mikhail Naganoveae73eb2018-04-03 16:57:36 -07001069 }
1070}
1071
Eric Laurente8c8b432018-10-17 10:08:02 -07001072void AudioPolicyService::UidPolicy::updateUid(std::unordered_map<uid_t,
1073 std::pair<bool, int>> *uids,
1074 uid_t uid,
1075 bool active,
1076 int state,
1077 bool insert) {
1078 if (isServiceUid(uid)) {
1079 return;
1080 }
1081 bool wasActive = isUidActive(uid);
1082 int previousState = getUidState(uid);
Mikhail Naganoveae73eb2018-04-03 16:57:36 -07001083 {
1084 Mutex::Autolock _l(mLock);
Eric Laurente8c8b432018-10-17 10:08:02 -07001085 updateUidLocked(uids, uid, active, state, insert);
Mikhail Naganoveae73eb2018-04-03 16:57:36 -07001086 }
Eric Laurente8c8b432018-10-17 10:08:02 -07001087 if (wasActive != isUidActive(uid) || state != previousState) {
1088 notifyService();
1089 }
Mikhail Naganoveae73eb2018-04-03 16:57:36 -07001090}
1091
Eric Laurente8c8b432018-10-17 10:08:02 -07001092void AudioPolicyService::UidPolicy::updateUidLocked(std::unordered_map<uid_t,
1093 std::pair<bool, int>> *uids,
1094 uid_t uid,
1095 bool active,
1096 int state,
1097 bool insert) {
Mikhail Naganoveae73eb2018-04-03 16:57:36 -07001098 auto it = uids->find(uid);
1099 if (it != uids->end()) {
Mikhail Naganoveae73eb2018-04-03 16:57:36 -07001100 if (insert) {
Eric Laurente8c8b432018-10-17 10:08:02 -07001101 if (state == ActivityManager::PROCESS_STATE_UNKNOWN) {
1102 it->second.first = active;
1103 }
1104 if (it->second.first) {
1105 it->second.second = state;
1106 } else {
1107 it->second.second = ActivityManager::PROCESS_STATE_UNKNOWN;
1108 }
Mikhail Naganoveae73eb2018-04-03 16:57:36 -07001109 } else {
1110 uids->erase(it);
1111 }
Eric Laurente8c8b432018-10-17 10:08:02 -07001112 } else if (insert && (state == ActivityManager::PROCESS_STATE_UNKNOWN)) {
1113 uids->insert(std::pair<uid_t, std::pair<bool, int>>(uid,
1114 std::pair<bool, int>(active, state)));
Mikhail Naganoveae73eb2018-04-03 16:57:36 -07001115 }
Svet Ganovf4ddfef2018-01-16 07:37:58 -08001116}
Mathias Agopian65ab4712010-07-14 17:59:35 -07001117
Eric Laurent4eb58f12018-12-07 16:41:02 -08001118bool AudioPolicyService::UidPolicy::isA11yOnTop() {
1119 for (const auto &uid : mCachedUids) {
Eric Laurent47670c92019-08-28 16:59:05 -07001120 if (!isA11yUid(uid.first)) {
Eric Laurent4eb58f12018-12-07 16:41:02 -08001121 continue;
1122 }
Amith Yamasanibcbb3002019-01-23 13:53:33 -08001123 if (uid.second.second >= ActivityManager::PROCESS_STATE_TOP
1124 && uid.second.second <= ActivityManager::PROCESS_STATE_BOUND_FOREGROUND_SERVICE) {
Eric Laurent4eb58f12018-12-07 16:41:02 -08001125 return true;
1126 }
1127 }
1128 return false;
1129}
1130
Eric Laurentb78763e2018-10-17 10:08:02 -07001131bool AudioPolicyService::UidPolicy::isA11yUid(uid_t uid)
1132{
1133 std::vector<uid_t>::iterator it = find(mA11yUids.begin(), mA11yUids.end(), uid);
1134 return it != mA11yUids.end();
1135}
1136
Michael Groovercfd28302018-12-11 19:16:46 -08001137// ----------- AudioPolicyService::SensorPrivacyService implementation ----------
1138void AudioPolicyService::SensorPrivacyPolicy::registerSelf() {
1139 SensorPrivacyManager spm;
1140 mSensorPrivacyEnabled = spm.isSensorPrivacyEnabled();
1141 spm.addSensorPrivacyListener(this);
1142}
1143
1144void AudioPolicyService::SensorPrivacyPolicy::unregisterSelf() {
1145 SensorPrivacyManager spm;
1146 spm.removeSensorPrivacyListener(this);
1147}
1148
1149bool AudioPolicyService::SensorPrivacyPolicy::isSensorPrivacyEnabled() {
1150 return mSensorPrivacyEnabled;
1151}
1152
1153binder::Status AudioPolicyService::SensorPrivacyPolicy::onSensorPrivacyChanged(bool enabled) {
1154 mSensorPrivacyEnabled = enabled;
1155 sp<AudioPolicyService> service = mService.promote();
1156 if (service != nullptr) {
1157 service->updateUidStates();
1158 }
1159 return binder::Status::ok();
1160}
1161
Mathias Agopian65ab4712010-07-14 17:59:35 -07001162// ----------- AudioPolicyService::AudioCommandThread implementation ----------
1163
Eric Laurentbfb1b832013-01-07 09:53:42 -08001164AudioPolicyService::AudioCommandThread::AudioCommandThread(String8 name,
1165 const wp<AudioPolicyService>& service)
1166 : Thread(false), mName(name), mService(service)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001167{
Mathias Agopian65ab4712010-07-14 17:59:35 -07001168}
1169
1170
1171AudioPolicyService::AudioCommandThread::~AudioCommandThread()
1172{
Eric Laurentbfb1b832013-01-07 09:53:42 -08001173 if (!mAudioCommands.isEmpty()) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07001174 release_wake_lock(mName.string());
1175 }
1176 mAudioCommands.clear();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001177}
1178
1179void AudioPolicyService::AudioCommandThread::onFirstRef()
1180{
Eric Laurentbfb1b832013-01-07 09:53:42 -08001181 run(mName.string(), ANDROID_PRIORITY_AUDIO);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001182}
1183
1184bool AudioPolicyService::AudioCommandThread::threadLoop()
1185{
Eric Laurentd7eda8d2016-02-02 17:18:39 -08001186 nsecs_t waitTime = -1;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001187
1188 mLock.lock();
1189 while (!exitPending())
1190 {
Eric Laurent59a89232014-06-08 14:14:17 -07001191 sp<AudioPolicyService> svc;
1192 while (!mAudioCommands.isEmpty() && !exitPending()) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07001193 nsecs_t curTime = systemTime();
1194 // commands are sorted by increasing time stamp: execute them from index 0 and up
1195 if (mAudioCommands[0]->mTime <= curTime) {
Eric Laurent0ede8922014-05-09 18:04:42 -07001196 sp<AudioCommand> command = mAudioCommands[0];
Mathias Agopian65ab4712010-07-14 17:59:35 -07001197 mAudioCommands.removeAt(0);
Eric Laurent0ede8922014-05-09 18:04:42 -07001198 mLastCommand = command;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001199
1200 switch (command->mCommand) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07001201 case SET_VOLUME: {
Eric Laurent0ede8922014-05-09 18:04:42 -07001202 VolumeData *data = (VolumeData *)command->mParam.get();
Steve Block3856b092011-10-20 11:56:00 +01001203 ALOGV("AudioCommandThread() processing set volume stream %d, \
Eric Laurentde070132010-07-13 04:45:46 -07001204 volume %f, output %d", data->mStream, data->mVolume, data->mIO);
Andy Hungfe726a62018-09-27 15:17:25 -07001205 mLock.unlock();
Eric Laurentde070132010-07-13 04:45:46 -07001206 command->mStatus = AudioSystem::setStreamVolume(data->mStream,
1207 data->mVolume,
1208 data->mIO);
Andy Hungfe726a62018-09-27 15:17:25 -07001209 mLock.lock();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001210 }break;
1211 case SET_PARAMETERS: {
Eric Laurent0ede8922014-05-09 18:04:42 -07001212 ParametersData *data = (ParametersData *)command->mParam.get();
Glenn Kastene53b9ea2012-03-12 16:29:55 -07001213 ALOGV("AudioCommandThread() processing set parameters string %s, io %d",
1214 data->mKeyValuePairs.string(), data->mIO);
Andy Hungfe726a62018-09-27 15:17:25 -07001215 mLock.unlock();
Glenn Kastene53b9ea2012-03-12 16:29:55 -07001216 command->mStatus = AudioSystem::setParameters(data->mIO, data->mKeyValuePairs);
Andy Hungfe726a62018-09-27 15:17:25 -07001217 mLock.lock();
Glenn Kastene53b9ea2012-03-12 16:29:55 -07001218 }break;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001219 case SET_VOICE_VOLUME: {
Eric Laurent0ede8922014-05-09 18:04:42 -07001220 VoiceVolumeData *data = (VoiceVolumeData *)command->mParam.get();
Steve Block3856b092011-10-20 11:56:00 +01001221 ALOGV("AudioCommandThread() processing set voice volume volume %f",
Eric Laurentde070132010-07-13 04:45:46 -07001222 data->mVolume);
Andy Hungfe726a62018-09-27 15:17:25 -07001223 mLock.unlock();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001224 command->mStatus = AudioSystem::setVoiceVolume(data->mVolume);
Andy Hungfe726a62018-09-27 15:17:25 -07001225 mLock.lock();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001226 }break;
Eric Laurentbfb1b832013-01-07 09:53:42 -08001227 case STOP_OUTPUT: {
Eric Laurent0ede8922014-05-09 18:04:42 -07001228 StopOutputData *data = (StopOutputData *)command->mParam.get();
Eric Laurentd7fe0862018-07-14 16:48:01 -07001229 ALOGV("AudioCommandThread() processing stop output portId %d",
1230 data->mPortId);
Eric Laurent59a89232014-06-08 14:14:17 -07001231 svc = mService.promote();
Eric Laurentbfb1b832013-01-07 09:53:42 -08001232 if (svc == 0) {
1233 break;
1234 }
1235 mLock.unlock();
Eric Laurentd7fe0862018-07-14 16:48:01 -07001236 svc->doStopOutput(data->mPortId);
Eric Laurentbfb1b832013-01-07 09:53:42 -08001237 mLock.lock();
Eric Laurentbfb1b832013-01-07 09:53:42 -08001238 }break;
1239 case RELEASE_OUTPUT: {
Eric Laurent0ede8922014-05-09 18:04:42 -07001240 ReleaseOutputData *data = (ReleaseOutputData *)command->mParam.get();
Eric Laurentd7fe0862018-07-14 16:48:01 -07001241 ALOGV("AudioCommandThread() processing release output portId %d",
1242 data->mPortId);
Eric Laurent59a89232014-06-08 14:14:17 -07001243 svc = mService.promote();
Eric Laurentbfb1b832013-01-07 09:53:42 -08001244 if (svc == 0) {
1245 break;
1246 }
1247 mLock.unlock();
Eric Laurentd7fe0862018-07-14 16:48:01 -07001248 svc->doReleaseOutput(data->mPortId);
Eric Laurentbfb1b832013-01-07 09:53:42 -08001249 mLock.lock();
Eric Laurentbfb1b832013-01-07 09:53:42 -08001250 }break;
Eric Laurent951f4552014-05-20 10:48:17 -07001251 case CREATE_AUDIO_PATCH: {
1252 CreateAudioPatchData *data = (CreateAudioPatchData *)command->mParam.get();
1253 ALOGV("AudioCommandThread() processing create audio patch");
1254 sp<IAudioFlinger> af = AudioSystem::get_audio_flinger();
1255 if (af == 0) {
1256 command->mStatus = PERMISSION_DENIED;
1257 } else {
Andy Hungfe726a62018-09-27 15:17:25 -07001258 mLock.unlock();
Eric Laurent951f4552014-05-20 10:48:17 -07001259 command->mStatus = af->createAudioPatch(&data->mPatch, &data->mHandle);
Andy Hungfe726a62018-09-27 15:17:25 -07001260 mLock.lock();
Eric Laurent951f4552014-05-20 10:48:17 -07001261 }
1262 } break;
1263 case RELEASE_AUDIO_PATCH: {
1264 ReleaseAudioPatchData *data = (ReleaseAudioPatchData *)command->mParam.get();
1265 ALOGV("AudioCommandThread() processing release audio patch");
1266 sp<IAudioFlinger> af = AudioSystem::get_audio_flinger();
1267 if (af == 0) {
1268 command->mStatus = PERMISSION_DENIED;
1269 } else {
Andy Hungfe726a62018-09-27 15:17:25 -07001270 mLock.unlock();
Eric Laurent951f4552014-05-20 10:48:17 -07001271 command->mStatus = af->releaseAudioPatch(data->mHandle);
Andy Hungfe726a62018-09-27 15:17:25 -07001272 mLock.lock();
Eric Laurent951f4552014-05-20 10:48:17 -07001273 }
1274 } break;
Eric Laurentb52c1522014-05-20 11:27:36 -07001275 case UPDATE_AUDIOPORT_LIST: {
1276 ALOGV("AudioCommandThread() processing update audio port list");
Eric Laurent59a89232014-06-08 14:14:17 -07001277 svc = mService.promote();
Eric Laurentb52c1522014-05-20 11:27:36 -07001278 if (svc == 0) {
1279 break;
1280 }
1281 mLock.unlock();
1282 svc->doOnAudioPortListUpdate();
1283 mLock.lock();
1284 }break;
1285 case UPDATE_AUDIOPATCH_LIST: {
1286 ALOGV("AudioCommandThread() processing update audio patch list");
Eric Laurent59a89232014-06-08 14:14:17 -07001287 svc = mService.promote();
Eric Laurentb52c1522014-05-20 11:27:36 -07001288 if (svc == 0) {
1289 break;
1290 }
1291 mLock.unlock();
1292 svc->doOnAudioPatchListUpdate();
1293 mLock.lock();
1294 }break;
François Gaffiecfe17322018-11-07 13:41:29 +01001295 case CHANGED_AUDIOVOLUMEGROUP: {
1296 AudioVolumeGroupData *data =
1297 static_cast<AudioVolumeGroupData *>(command->mParam.get());
1298 ALOGV("AudioCommandThread() processing update audio volume group");
1299 svc = mService.promote();
1300 if (svc == 0) {
1301 break;
1302 }
1303 mLock.unlock();
1304 svc->doOnAudioVolumeGroupChanged(data->mGroup, data->mFlags);
1305 mLock.lock();
1306 }break;
Eric Laurente1715a42014-05-20 11:30:42 -07001307 case SET_AUDIOPORT_CONFIG: {
1308 SetAudioPortConfigData *data = (SetAudioPortConfigData *)command->mParam.get();
1309 ALOGV("AudioCommandThread() processing set port config");
1310 sp<IAudioFlinger> af = AudioSystem::get_audio_flinger();
1311 if (af == 0) {
1312 command->mStatus = PERMISSION_DENIED;
1313 } else {
Andy Hungfe726a62018-09-27 15:17:25 -07001314 mLock.unlock();
Eric Laurente1715a42014-05-20 11:30:42 -07001315 command->mStatus = af->setAudioPortConfig(&data->mConfig);
Andy Hungfe726a62018-09-27 15:17:25 -07001316 mLock.lock();
Eric Laurente1715a42014-05-20 11:30:42 -07001317 }
1318 } break;
Jean-Michel Trivide801052015-04-14 19:10:14 -07001319 case DYN_POLICY_MIX_STATE_UPDATE: {
1320 DynPolicyMixStateUpdateData *data =
1321 (DynPolicyMixStateUpdateData *)command->mParam.get();
Jean-Michel Trivide801052015-04-14 19:10:14 -07001322 ALOGV("AudioCommandThread() processing dyn policy mix state update %s %d",
1323 data->mRegId.string(), data->mState);
1324 svc = mService.promote();
1325 if (svc == 0) {
1326 break;
1327 }
1328 mLock.unlock();
1329 svc->doOnDynamicPolicyMixStateUpdate(data->mRegId, data->mState);
1330 mLock.lock();
1331 } break;
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -08001332 case RECORDING_CONFIGURATION_UPDATE: {
1333 RecordingConfigurationUpdateData *data =
1334 (RecordingConfigurationUpdateData *)command->mParam.get();
1335 ALOGV("AudioCommandThread() processing recording configuration update");
1336 svc = mService.promote();
1337 if (svc == 0) {
1338 break;
1339 }
1340 mLock.unlock();
Jean-Michel Triviac4e4292016-12-22 11:39:31 -08001341 svc->doOnRecordingConfigurationUpdate(data->mEvent, &data->mClientInfo,
Eric Laurenta9f86652018-11-28 17:23:11 -08001342 &data->mClientConfig, data->mClientEffects,
1343 &data->mDeviceConfig, data->mEffects,
1344 data->mPatchHandle, data->mSource);
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -08001345 mLock.lock();
1346 } break;
Eric Laurentb20cf7d2019-04-05 19:37:34 -07001347 case SET_EFFECT_SUSPENDED: {
1348 SetEffectSuspendedData *data = (SetEffectSuspendedData *)command->mParam.get();
1349 ALOGV("AudioCommandThread() processing set effect suspended");
1350 sp<IAudioFlinger> af = AudioSystem::get_audio_flinger();
1351 if (af != 0) {
1352 mLock.unlock();
1353 af->setEffectSuspended(data->mEffectId, data->mSessionId, data->mSuspended);
1354 mLock.lock();
1355 }
1356 } break;
Mikhail Naganov88b30d22020-03-09 19:43:13 +00001357 case AUDIO_MODULES_UPDATE: {
1358 ALOGV("AudioCommandThread() processing audio modules update");
1359 svc = mService.promote();
1360 if (svc == 0) {
1361 break;
1362 }
1363 mLock.unlock();
1364 svc->doOnNewAudioModulesAvailable();
1365 mLock.lock();
1366 } break;
Eric Laurentb20cf7d2019-04-05 19:37:34 -07001367
Mathias Agopian65ab4712010-07-14 17:59:35 -07001368 default:
Steve Block5ff1dd52012-01-05 23:22:43 +00001369 ALOGW("AudioCommandThread() unknown command %d", command->mCommand);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001370 }
Eric Laurent0ede8922014-05-09 18:04:42 -07001371 {
1372 Mutex::Autolock _l(command->mLock);
1373 if (command->mWaitStatus) {
1374 command->mWaitStatus = false;
1375 command->mCond.signal();
1376 }
1377 }
Eric Laurentd7eda8d2016-02-02 17:18:39 -08001378 waitTime = -1;
Zach Janga754b4f2015-10-27 01:29:34 +00001379 // release mLock before releasing strong reference on the service as
1380 // AudioPolicyService destructor calls AudioCommandThread::exit() which
1381 // acquires mLock.
1382 mLock.unlock();
1383 svc.clear();
1384 mLock.lock();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001385 } else {
1386 waitTime = mAudioCommands[0]->mTime - curTime;
1387 break;
1388 }
1389 }
Zach Janga754b4f2015-10-27 01:29:34 +00001390
1391 // release delayed commands wake lock if the queue is empty
1392 if (mAudioCommands.isEmpty()) {
Ricardo Garcia05f2fdc2014-07-24 15:48:24 -07001393 release_wake_lock(mName.string());
Zach Janga754b4f2015-10-27 01:29:34 +00001394 }
1395
1396 // At this stage we have either an empty command queue or the first command in the queue
1397 // has a finite delay. So unless we are exiting it is safe to wait.
1398 if (!exitPending()) {
Eric Laurent59a89232014-06-08 14:14:17 -07001399 ALOGV("AudioCommandThread() going to sleep");
Eric Laurentd7eda8d2016-02-02 17:18:39 -08001400 if (waitTime == -1) {
1401 mWaitWorkCV.wait(mLock);
1402 } else {
1403 mWaitWorkCV.waitRelative(mLock, waitTime);
1404 }
Eric Laurent59a89232014-06-08 14:14:17 -07001405 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001406 }
Ricardo Garcia05f2fdc2014-07-24 15:48:24 -07001407 // release delayed commands wake lock before quitting
1408 if (!mAudioCommands.isEmpty()) {
1409 release_wake_lock(mName.string());
1410 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001411 mLock.unlock();
1412 return false;
1413}
1414
1415status_t AudioPolicyService::AudioCommandThread::dump(int fd)
1416{
1417 const size_t SIZE = 256;
1418 char buffer[SIZE];
1419 String8 result;
1420
1421 snprintf(buffer, SIZE, "AudioCommandThread %p Dump\n", this);
1422 result.append(buffer);
1423 write(fd, result.string(), result.size());
1424
Mikhail Naganov12b716c2020-04-30 22:37:43 +00001425 const bool locked = dumpTryLock(mLock);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001426 if (!locked) {
1427 String8 result2(kCmdDeadlockedString);
1428 write(fd, result2.string(), result2.size());
1429 }
1430
1431 snprintf(buffer, SIZE, "- Commands:\n");
1432 result = String8(buffer);
1433 result.append(" Command Time Wait pParam\n");
Glenn Kasten8d6a2442012-02-08 14:04:28 -08001434 for (size_t i = 0; i < mAudioCommands.size(); i++) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07001435 mAudioCommands[i]->dump(buffer, SIZE);
1436 result.append(buffer);
1437 }
1438 result.append(" Last Command\n");
Eric Laurent0ede8922014-05-09 18:04:42 -07001439 if (mLastCommand != 0) {
1440 mLastCommand->dump(buffer, SIZE);
1441 result.append(buffer);
1442 } else {
1443 result.append(" none\n");
1444 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001445
1446 write(fd, result.string(), result.size());
1447
Mikhail Naganov12b716c2020-04-30 22:37:43 +00001448 dumpReleaseLock(mLock, locked);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001449
1450 return NO_ERROR;
1451}
1452
Glenn Kastenfff6d712012-01-12 16:38:12 -08001453status_t AudioPolicyService::AudioCommandThread::volumeCommand(audio_stream_type_t stream,
Eric Laurentde070132010-07-13 04:45:46 -07001454 float volume,
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001455 audio_io_handle_t output,
Eric Laurentde070132010-07-13 04:45:46 -07001456 int delayMs)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001457{
Eric Laurent0ede8922014-05-09 18:04:42 -07001458 sp<AudioCommand> command = new AudioCommand();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001459 command->mCommand = SET_VOLUME;
Eric Laurent0ede8922014-05-09 18:04:42 -07001460 sp<VolumeData> data = new VolumeData();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001461 data->mStream = stream;
1462 data->mVolume = volume;
1463 data->mIO = output;
1464 command->mParam = data;
Eric Laurent0ede8922014-05-09 18:04:42 -07001465 command->mWaitStatus = true;
Steve Block3856b092011-10-20 11:56:00 +01001466 ALOGV("AudioCommandThread() adding set volume stream %d, volume %f, output %d",
Eric Laurentde070132010-07-13 04:45:46 -07001467 stream, volume, output);
Eric Laurent0ede8922014-05-09 18:04:42 -07001468 return sendCommand(command, delayMs);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001469}
1470
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001471status_t AudioPolicyService::AudioCommandThread::parametersCommand(audio_io_handle_t ioHandle,
Dima Zavinfce7a472011-04-19 22:30:36 -07001472 const char *keyValuePairs,
Eric Laurentde070132010-07-13 04:45:46 -07001473 int delayMs)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001474{
Eric Laurent0ede8922014-05-09 18:04:42 -07001475 sp<AudioCommand> command = new AudioCommand();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001476 command->mCommand = SET_PARAMETERS;
Eric Laurent0ede8922014-05-09 18:04:42 -07001477 sp<ParametersData> data = new ParametersData();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001478 data->mIO = ioHandle;
Dima Zavinfce7a472011-04-19 22:30:36 -07001479 data->mKeyValuePairs = String8(keyValuePairs);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001480 command->mParam = data;
Eric Laurent0ede8922014-05-09 18:04:42 -07001481 command->mWaitStatus = true;
Steve Block3856b092011-10-20 11:56:00 +01001482 ALOGV("AudioCommandThread() adding set parameter string %s, io %d ,delay %d",
Dima Zavinfce7a472011-04-19 22:30:36 -07001483 keyValuePairs, ioHandle, delayMs);
Eric Laurent0ede8922014-05-09 18:04:42 -07001484 return sendCommand(command, delayMs);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001485}
1486
1487status_t AudioPolicyService::AudioCommandThread::voiceVolumeCommand(float volume, int delayMs)
1488{
Eric Laurent0ede8922014-05-09 18:04:42 -07001489 sp<AudioCommand> command = new AudioCommand();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001490 command->mCommand = SET_VOICE_VOLUME;
Eric Laurent0ede8922014-05-09 18:04:42 -07001491 sp<VoiceVolumeData> data = new VoiceVolumeData();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001492 data->mVolume = volume;
1493 command->mParam = data;
Eric Laurent0ede8922014-05-09 18:04:42 -07001494 command->mWaitStatus = true;
Steve Block3856b092011-10-20 11:56:00 +01001495 ALOGV("AudioCommandThread() adding set voice volume volume %f", volume);
Eric Laurent0ede8922014-05-09 18:04:42 -07001496 return sendCommand(command, delayMs);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001497}
1498
Eric Laurentb20cf7d2019-04-05 19:37:34 -07001499void AudioPolicyService::AudioCommandThread::setEffectSuspendedCommand(int effectId,
1500 audio_session_t sessionId,
1501 bool suspended)
1502{
1503 sp<AudioCommand> command = new AudioCommand();
1504 command->mCommand = SET_EFFECT_SUSPENDED;
1505 sp<SetEffectSuspendedData> data = new SetEffectSuspendedData();
1506 data->mEffectId = effectId;
1507 data->mSessionId = sessionId;
1508 data->mSuspended = suspended;
1509 command->mParam = data;
1510 ALOGV("AudioCommandThread() adding set suspended effectId %d sessionId %d suspended %d",
1511 effectId, sessionId, suspended);
1512 sendCommand(command);
1513}
1514
1515
Eric Laurentd7fe0862018-07-14 16:48:01 -07001516void AudioPolicyService::AudioCommandThread::stopOutputCommand(audio_port_handle_t portId)
Eric Laurentbfb1b832013-01-07 09:53:42 -08001517{
Eric Laurent0ede8922014-05-09 18:04:42 -07001518 sp<AudioCommand> command = new AudioCommand();
Eric Laurentbfb1b832013-01-07 09:53:42 -08001519 command->mCommand = STOP_OUTPUT;
Eric Laurent0ede8922014-05-09 18:04:42 -07001520 sp<StopOutputData> data = new StopOutputData();
Eric Laurentd7fe0862018-07-14 16:48:01 -07001521 data->mPortId = portId;
Jesper Tragardh48412dc2014-03-24 14:12:43 +01001522 command->mParam = data;
Eric Laurentd7fe0862018-07-14 16:48:01 -07001523 ALOGV("AudioCommandThread() adding stop output portId %d", portId);
Eric Laurent0ede8922014-05-09 18:04:42 -07001524 sendCommand(command);
Eric Laurentbfb1b832013-01-07 09:53:42 -08001525}
1526
Eric Laurentd7fe0862018-07-14 16:48:01 -07001527void AudioPolicyService::AudioCommandThread::releaseOutputCommand(audio_port_handle_t portId)
Eric Laurentbfb1b832013-01-07 09:53:42 -08001528{
Eric Laurent0ede8922014-05-09 18:04:42 -07001529 sp<AudioCommand> command = new AudioCommand();
Eric Laurentbfb1b832013-01-07 09:53:42 -08001530 command->mCommand = RELEASE_OUTPUT;
Eric Laurent0ede8922014-05-09 18:04:42 -07001531 sp<ReleaseOutputData> data = new ReleaseOutputData();
Eric Laurentd7fe0862018-07-14 16:48:01 -07001532 data->mPortId = portId;
Jesper Tragardh48412dc2014-03-24 14:12:43 +01001533 command->mParam = data;
Eric Laurentd7fe0862018-07-14 16:48:01 -07001534 ALOGV("AudioCommandThread() adding release output portId %d", portId);
Eric Laurent0ede8922014-05-09 18:04:42 -07001535 sendCommand(command);
1536}
1537
Eric Laurent951f4552014-05-20 10:48:17 -07001538status_t AudioPolicyService::AudioCommandThread::createAudioPatchCommand(
1539 const struct audio_patch *patch,
1540 audio_patch_handle_t *handle,
1541 int delayMs)
1542{
1543 status_t status = NO_ERROR;
1544
1545 sp<AudioCommand> command = new AudioCommand();
1546 command->mCommand = CREATE_AUDIO_PATCH;
1547 CreateAudioPatchData *data = new CreateAudioPatchData();
1548 data->mPatch = *patch;
1549 data->mHandle = *handle;
1550 command->mParam = data;
1551 command->mWaitStatus = true;
1552 ALOGV("AudioCommandThread() adding create patch delay %d", delayMs);
1553 status = sendCommand(command, delayMs);
1554 if (status == NO_ERROR) {
1555 *handle = data->mHandle;
1556 }
1557 return status;
1558}
1559
1560status_t AudioPolicyService::AudioCommandThread::releaseAudioPatchCommand(audio_patch_handle_t handle,
1561 int delayMs)
1562{
1563 sp<AudioCommand> command = new AudioCommand();
1564 command->mCommand = RELEASE_AUDIO_PATCH;
1565 ReleaseAudioPatchData *data = new ReleaseAudioPatchData();
1566 data->mHandle = handle;
1567 command->mParam = data;
1568 command->mWaitStatus = true;
1569 ALOGV("AudioCommandThread() adding release patch delay %d", delayMs);
1570 return sendCommand(command, delayMs);
1571}
1572
Eric Laurentb52c1522014-05-20 11:27:36 -07001573void AudioPolicyService::AudioCommandThread::updateAudioPortListCommand()
1574{
1575 sp<AudioCommand> command = new AudioCommand();
1576 command->mCommand = UPDATE_AUDIOPORT_LIST;
1577 ALOGV("AudioCommandThread() adding update audio port list");
1578 sendCommand(command);
1579}
1580
1581void AudioPolicyService::AudioCommandThread::updateAudioPatchListCommand()
1582{
1583 sp<AudioCommand>command = new AudioCommand();
1584 command->mCommand = UPDATE_AUDIOPATCH_LIST;
1585 ALOGV("AudioCommandThread() adding update audio patch list");
1586 sendCommand(command);
1587}
1588
François Gaffiecfe17322018-11-07 13:41:29 +01001589void AudioPolicyService::AudioCommandThread::changeAudioVolumeGroupCommand(volume_group_t group,
1590 int flags)
1591{
1592 sp<AudioCommand>command = new AudioCommand();
1593 command->mCommand = CHANGED_AUDIOVOLUMEGROUP;
1594 AudioVolumeGroupData *data= new AudioVolumeGroupData();
1595 data->mGroup = group;
1596 data->mFlags = flags;
1597 command->mParam = data;
1598 ALOGV("AudioCommandThread() adding audio volume group changed");
1599 sendCommand(command);
1600}
1601
Eric Laurente1715a42014-05-20 11:30:42 -07001602status_t AudioPolicyService::AudioCommandThread::setAudioPortConfigCommand(
1603 const struct audio_port_config *config, int delayMs)
1604{
1605 sp<AudioCommand> command = new AudioCommand();
1606 command->mCommand = SET_AUDIOPORT_CONFIG;
1607 SetAudioPortConfigData *data = new SetAudioPortConfigData();
1608 data->mConfig = *config;
1609 command->mParam = data;
1610 command->mWaitStatus = true;
1611 ALOGV("AudioCommandThread() adding set port config delay %d", delayMs);
1612 return sendCommand(command, delayMs);
1613}
1614
Jean-Michel Trivide801052015-04-14 19:10:14 -07001615void AudioPolicyService::AudioCommandThread::dynamicPolicyMixStateUpdateCommand(
Chih-Hung Hsiehe964d4e2016-08-09 14:31:32 -07001616 const String8& regId, int32_t state)
Jean-Michel Trivide801052015-04-14 19:10:14 -07001617{
1618 sp<AudioCommand> command = new AudioCommand();
1619 command->mCommand = DYN_POLICY_MIX_STATE_UPDATE;
1620 DynPolicyMixStateUpdateData *data = new DynPolicyMixStateUpdateData();
1621 data->mRegId = regId;
1622 data->mState = state;
1623 command->mParam = data;
1624 ALOGV("AudioCommandThread() sending dynamic policy mix (id=%s) state update to %d",
1625 regId.string(), state);
1626 sendCommand(command);
1627}
1628
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -08001629void AudioPolicyService::AudioCommandThread::recordingConfigurationUpdateCommand(
Eric Laurenta9f86652018-11-28 17:23:11 -08001630 int event,
1631 const record_client_info_t *clientInfo,
1632 const audio_config_base_t *clientConfig,
1633 std::vector<effect_descriptor_t> clientEffects,
1634 const audio_config_base_t *deviceConfig,
1635 std::vector<effect_descriptor_t> effects,
1636 audio_patch_handle_t patchHandle,
1637 audio_source_t source)
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -08001638{
1639 sp<AudioCommand>command = new AudioCommand();
1640 command->mCommand = RECORDING_CONFIGURATION_UPDATE;
1641 RecordingConfigurationUpdateData *data = new RecordingConfigurationUpdateData();
1642 data->mEvent = event;
Jean-Michel Triviac4e4292016-12-22 11:39:31 -08001643 data->mClientInfo = *clientInfo;
Jean-Michel Trivi7281aa92016-02-17 15:33:40 -08001644 data->mClientConfig = *clientConfig;
Eric Laurenta9f86652018-11-28 17:23:11 -08001645 data->mClientEffects = clientEffects;
Jean-Michel Trivi7281aa92016-02-17 15:33:40 -08001646 data->mDeviceConfig = *deviceConfig;
Eric Laurenta9f86652018-11-28 17:23:11 -08001647 data->mEffects = effects;
Jean-Michel Trivi8c7cf3b2016-02-25 17:08:24 -08001648 data->mPatchHandle = patchHandle;
Eric Laurenta9f86652018-11-28 17:23:11 -08001649 data->mSource = source;
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -08001650 command->mParam = data;
Jean-Michel Triviac4e4292016-12-22 11:39:31 -08001651 ALOGV("AudioCommandThread() adding recording configuration update event %d, source %d uid %u",
1652 event, clientInfo->source, clientInfo->uid);
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -08001653 sendCommand(command);
1654}
1655
Mikhail Naganov88b30d22020-03-09 19:43:13 +00001656void AudioPolicyService::AudioCommandThread::audioModulesUpdateCommand()
1657{
1658 sp<AudioCommand> command = new AudioCommand();
1659 command->mCommand = AUDIO_MODULES_UPDATE;
1660 sendCommand(command);
1661}
1662
Eric Laurent0ede8922014-05-09 18:04:42 -07001663status_t AudioPolicyService::AudioCommandThread::sendCommand(sp<AudioCommand>& command, int delayMs)
1664{
1665 {
1666 Mutex::Autolock _l(mLock);
1667 insertCommand_l(command, delayMs);
1668 mWaitWorkCV.signal();
1669 }
1670 Mutex::Autolock _l(command->mLock);
1671 while (command->mWaitStatus) {
1672 nsecs_t timeOutNs = kAudioCommandTimeoutNs + milliseconds(delayMs);
1673 if (command->mCond.waitRelative(command->mLock, timeOutNs) != NO_ERROR) {
1674 command->mStatus = TIMED_OUT;
1675 command->mWaitStatus = false;
1676 }
1677 }
1678 return command->mStatus;
Eric Laurentbfb1b832013-01-07 09:53:42 -08001679}
1680
Mathias Agopian65ab4712010-07-14 17:59:35 -07001681// insertCommand_l() must be called with mLock held
Eric Laurent0ede8922014-05-09 18:04:42 -07001682void AudioPolicyService::AudioCommandThread::insertCommand_l(sp<AudioCommand>& command, int delayMs)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001683{
Glenn Kasten8d6a2442012-02-08 14:04:28 -08001684 ssize_t i; // not size_t because i will count down to -1
Eric Laurent0ede8922014-05-09 18:04:42 -07001685 Vector < sp<AudioCommand> > removedCommands;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001686 command->mTime = systemTime() + milliseconds(delayMs);
1687
1688 // acquire wake lock to make sure delayed commands are processed
Eric Laurentbfb1b832013-01-07 09:53:42 -08001689 if (mAudioCommands.isEmpty()) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07001690 acquire_wake_lock(PARTIAL_WAKE_LOCK, mName.string());
1691 }
1692
1693 // check same pending commands with later time stamps and eliminate them
Ivan Lozano5ff158f2017-10-30 09:06:24 -07001694 for (i = (ssize_t)mAudioCommands.size()-1; i >= 0; i--) {
Eric Laurent0ede8922014-05-09 18:04:42 -07001695 sp<AudioCommand> command2 = mAudioCommands[i];
Mathias Agopian65ab4712010-07-14 17:59:35 -07001696 // commands are sorted by increasing time stamp: no need to scan the rest of mAudioCommands
1697 if (command2->mTime <= command->mTime) break;
Eric Laurente45b48a2014-09-04 16:40:57 -07001698
1699 // create audio patch or release audio patch commands are equivalent
1700 // with regard to filtering
1701 if ((command->mCommand == CREATE_AUDIO_PATCH) ||
1702 (command->mCommand == RELEASE_AUDIO_PATCH)) {
1703 if ((command2->mCommand != CREATE_AUDIO_PATCH) &&
1704 (command2->mCommand != RELEASE_AUDIO_PATCH)) {
1705 continue;
1706 }
1707 } else if (command2->mCommand != command->mCommand) continue;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001708
1709 switch (command->mCommand) {
1710 case SET_PARAMETERS: {
Eric Laurent0ede8922014-05-09 18:04:42 -07001711 ParametersData *data = (ParametersData *)command->mParam.get();
1712 ParametersData *data2 = (ParametersData *)command2->mParam.get();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001713 if (data->mIO != data2->mIO) break;
Steve Block3856b092011-10-20 11:56:00 +01001714 ALOGV("Comparing parameter command %s to new command %s",
Eric Laurentde070132010-07-13 04:45:46 -07001715 data2->mKeyValuePairs.string(), data->mKeyValuePairs.string());
Mathias Agopian65ab4712010-07-14 17:59:35 -07001716 AudioParameter param = AudioParameter(data->mKeyValuePairs);
1717 AudioParameter param2 = AudioParameter(data2->mKeyValuePairs);
1718 for (size_t j = 0; j < param.size(); j++) {
Glenn Kastene53b9ea2012-03-12 16:29:55 -07001719 String8 key;
1720 String8 value;
1721 param.getAt(j, key, value);
1722 for (size_t k = 0; k < param2.size(); k++) {
1723 String8 key2;
1724 String8 value2;
1725 param2.getAt(k, key2, value2);
1726 if (key2 == key) {
1727 param2.remove(key2);
1728 ALOGV("Filtering out parameter %s", key2.string());
1729 break;
1730 }
1731 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001732 }
1733 // if all keys have been filtered out, remove the command.
1734 // otherwise, update the key value pairs
1735 if (param2.size() == 0) {
1736 removedCommands.add(command2);
1737 } else {
1738 data2->mKeyValuePairs = param2.toString();
1739 }
Eric Laurent21e54562013-09-23 12:08:05 -07001740 command->mTime = command2->mTime;
1741 // force delayMs to non 0 so that code below does not request to wait for
1742 // command status as the command is now delayed
1743 delayMs = 1;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001744 } break;
1745
1746 case SET_VOLUME: {
Eric Laurent0ede8922014-05-09 18:04:42 -07001747 VolumeData *data = (VolumeData *)command->mParam.get();
1748 VolumeData *data2 = (VolumeData *)command2->mParam.get();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001749 if (data->mIO != data2->mIO) break;
1750 if (data->mStream != data2->mStream) break;
Steve Block3856b092011-10-20 11:56:00 +01001751 ALOGV("Filtering out volume command on output %d for stream %d",
Eric Laurentde070132010-07-13 04:45:46 -07001752 data->mIO, data->mStream);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001753 removedCommands.add(command2);
Eric Laurent21e54562013-09-23 12:08:05 -07001754 command->mTime = command2->mTime;
1755 // force delayMs to non 0 so that code below does not request to wait for
1756 // command status as the command is now delayed
1757 delayMs = 1;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001758 } break;
Eric Laurente45b48a2014-09-04 16:40:57 -07001759
Eric Laurentbaf35fe2016-07-27 15:36:53 -07001760 case SET_VOICE_VOLUME: {
1761 VoiceVolumeData *data = (VoiceVolumeData *)command->mParam.get();
1762 VoiceVolumeData *data2 = (VoiceVolumeData *)command2->mParam.get();
1763 ALOGV("Filtering out voice volume command value %f replaced by %f",
1764 data2->mVolume, data->mVolume);
1765 removedCommands.add(command2);
1766 command->mTime = command2->mTime;
1767 // force delayMs to non 0 so that code below does not request to wait for
1768 // command status as the command is now delayed
1769 delayMs = 1;
1770 } break;
1771
Eric Laurente45b48a2014-09-04 16:40:57 -07001772 case CREATE_AUDIO_PATCH:
1773 case RELEASE_AUDIO_PATCH: {
1774 audio_patch_handle_t handle;
Haynes Mathew Georgea2d4a6d2014-10-13 13:05:22 -07001775 struct audio_patch patch;
Eric Laurente45b48a2014-09-04 16:40:57 -07001776 if (command->mCommand == CREATE_AUDIO_PATCH) {
1777 handle = ((CreateAudioPatchData *)command->mParam.get())->mHandle;
Haynes Mathew Georgea2d4a6d2014-10-13 13:05:22 -07001778 patch = ((CreateAudioPatchData *)command->mParam.get())->mPatch;
Eric Laurente45b48a2014-09-04 16:40:57 -07001779 } else {
1780 handle = ((ReleaseAudioPatchData *)command->mParam.get())->mHandle;
Mikhail Naganov7be71d22018-05-23 16:51:46 -07001781 memset(&patch, 0, sizeof(patch));
Eric Laurente45b48a2014-09-04 16:40:57 -07001782 }
1783 audio_patch_handle_t handle2;
Haynes Mathew Georgea2d4a6d2014-10-13 13:05:22 -07001784 struct audio_patch patch2;
Eric Laurente45b48a2014-09-04 16:40:57 -07001785 if (command2->mCommand == CREATE_AUDIO_PATCH) {
1786 handle2 = ((CreateAudioPatchData *)command2->mParam.get())->mHandle;
Haynes Mathew Georgea2d4a6d2014-10-13 13:05:22 -07001787 patch2 = ((CreateAudioPatchData *)command2->mParam.get())->mPatch;
Eric Laurente45b48a2014-09-04 16:40:57 -07001788 } else {
1789 handle2 = ((ReleaseAudioPatchData *)command2->mParam.get())->mHandle;
Glenn Kastenf60b6b62015-07-06 10:53:26 -07001790 memset(&patch2, 0, sizeof(patch2));
Eric Laurente45b48a2014-09-04 16:40:57 -07001791 }
1792 if (handle != handle2) break;
Haynes Mathew Georgea2d4a6d2014-10-13 13:05:22 -07001793 /* Filter CREATE_AUDIO_PATCH commands only when they are issued for
1794 same output. */
1795 if( (command->mCommand == CREATE_AUDIO_PATCH) &&
1796 (command2->mCommand == CREATE_AUDIO_PATCH) ) {
1797 bool isOutputDiff = false;
1798 if (patch.num_sources == patch2.num_sources) {
1799 for (unsigned count = 0; count < patch.num_sources; count++) {
1800 if (patch.sources[count].id != patch2.sources[count].id) {
1801 isOutputDiff = true;
1802 break;
1803 }
1804 }
1805 if (isOutputDiff)
1806 break;
1807 }
1808 }
Eric Laurente45b48a2014-09-04 16:40:57 -07001809 ALOGV("Filtering out %s audio patch command for handle %d",
1810 (command->mCommand == CREATE_AUDIO_PATCH) ? "create" : "release", handle);
1811 removedCommands.add(command2);
1812 command->mTime = command2->mTime;
1813 // force delayMs to non 0 so that code below does not request to wait for
1814 // command status as the command is now delayed
1815 delayMs = 1;
1816 } break;
1817
Jean-Michel Trivide801052015-04-14 19:10:14 -07001818 case DYN_POLICY_MIX_STATE_UPDATE: {
1819
1820 } break;
1821
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -08001822 case RECORDING_CONFIGURATION_UPDATE: {
1823
1824 } break;
1825
Mathias Agopian65ab4712010-07-14 17:59:35 -07001826 default:
1827 break;
1828 }
1829 }
1830
1831 // remove filtered commands
1832 for (size_t j = 0; j < removedCommands.size(); j++) {
1833 // removed commands always have time stamps greater than current command
1834 for (size_t k = i + 1; k < mAudioCommands.size(); k++) {
Eric Laurent0ede8922014-05-09 18:04:42 -07001835 if (mAudioCommands[k].get() == removedCommands[j].get()) {
Steve Block3856b092011-10-20 11:56:00 +01001836 ALOGV("suppressing command: %d", mAudioCommands[k]->mCommand);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001837 mAudioCommands.removeAt(k);
1838 break;
1839 }
1840 }
1841 }
1842 removedCommands.clear();
1843
Eric Laurentaa79bef2015-01-15 14:33:51 -08001844 // Disable wait for status if delay is not 0.
1845 // Except for create audio patch command because the returned patch handle
1846 // is needed by audio policy manager
1847 if (delayMs != 0 && command->mCommand != CREATE_AUDIO_PATCH) {
Eric Laurentcec4abb2012-07-03 12:23:02 -07001848 command->mWaitStatus = false;
1849 }
Eric Laurentcec4abb2012-07-03 12:23:02 -07001850
Mathias Agopian65ab4712010-07-14 17:59:35 -07001851 // insert command at the right place according to its time stamp
Eric Laurent1e693b52014-07-09 15:03:28 -07001852 ALOGV("inserting command: %d at index %zd, num commands %zu",
1853 command->mCommand, i+1, mAudioCommands.size());
Mathias Agopian65ab4712010-07-14 17:59:35 -07001854 mAudioCommands.insertAt(command, i + 1);
1855}
1856
1857void AudioPolicyService::AudioCommandThread::exit()
1858{
Steve Block3856b092011-10-20 11:56:00 +01001859 ALOGV("AudioCommandThread::exit");
Mathias Agopian65ab4712010-07-14 17:59:35 -07001860 {
1861 AutoMutex _l(mLock);
1862 requestExit();
1863 mWaitWorkCV.signal();
1864 }
Zach Janga754b4f2015-10-27 01:29:34 +00001865 // Note that we can call it from the thread loop if all other references have been released
1866 // but it will safely return WOULD_BLOCK in this case
Mathias Agopian65ab4712010-07-14 17:59:35 -07001867 requestExitAndWait();
1868}
1869
1870void AudioPolicyService::AudioCommandThread::AudioCommand::dump(char* buffer, size_t size)
1871{
1872 snprintf(buffer, size, " %02d %06d.%03d %01u %p\n",
1873 mCommand,
1874 (int)ns2s(mTime),
1875 (int)ns2ms(mTime)%1000,
1876 mWaitStatus,
Eric Laurent0ede8922014-05-09 18:04:42 -07001877 mParam.get());
Mathias Agopian65ab4712010-07-14 17:59:35 -07001878}
1879
Dima Zavinfce7a472011-04-19 22:30:36 -07001880/******* helpers for the service_ops callbacks defined below *********/
1881void AudioPolicyService::setParameters(audio_io_handle_t ioHandle,
1882 const char *keyValuePairs,
1883 int delayMs)
1884{
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001885 mAudioCommandThread->parametersCommand(ioHandle, keyValuePairs,
Dima Zavinfce7a472011-04-19 22:30:36 -07001886 delayMs);
1887}
1888
1889int AudioPolicyService::setStreamVolume(audio_stream_type_t stream,
1890 float volume,
1891 audio_io_handle_t output,
1892 int delayMs)
1893{
Glenn Kastenfff6d712012-01-12 16:38:12 -08001894 return (int)mAudioCommandThread->volumeCommand(stream, volume,
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001895 output, delayMs);
Dima Zavinfce7a472011-04-19 22:30:36 -07001896}
1897
Dima Zavinfce7a472011-04-19 22:30:36 -07001898int AudioPolicyService::setVoiceVolume(float volume, int delayMs)
1899{
1900 return (int)mAudioCommandThread->voiceVolumeCommand(volume, delayMs);
1901}
1902
Eric Laurentb20cf7d2019-04-05 19:37:34 -07001903void AudioPolicyService::setEffectSuspended(int effectId,
1904 audio_session_t sessionId,
1905 bool suspended)
1906{
1907 mAudioCommandThread->setEffectSuspendedCommand(effectId, sessionId, suspended);
1908}
1909
Mikhail Naganov88b30d22020-03-09 19:43:13 +00001910void AudioPolicyService::onNewAudioModulesAvailable()
1911{
Mikhail Naganovd0e2c742020-03-25 15:59:59 -07001912 mOutputCommandThread->audioModulesUpdateCommand();
Mikhail Naganov88b30d22020-03-09 19:43:13 +00001913}
1914
Eric Laurentb20cf7d2019-04-05 19:37:34 -07001915
Dima Zavinfce7a472011-04-19 22:30:36 -07001916extern "C" {
Eric Laurent2d388ec2014-03-07 13:25:54 -08001917audio_module_handle_t aps_load_hw_module(void *service __unused,
1918 const char *name);
1919audio_io_handle_t aps_open_output(void *service __unused,
Eric Laurenta4c5a552012-03-29 10:12:40 -07001920 audio_devices_t *pDevices,
1921 uint32_t *pSamplingRate,
1922 audio_format_t *pFormat,
1923 audio_channel_mask_t *pChannelMask,
1924 uint32_t *pLatencyMs,
Eric Laurent2d388ec2014-03-07 13:25:54 -08001925 audio_output_flags_t flags);
Eric Laurenta4c5a552012-03-29 10:12:40 -07001926
Eric Laurent2d388ec2014-03-07 13:25:54 -08001927audio_io_handle_t aps_open_output_on_module(void *service __unused,
Eric Laurenta4c5a552012-03-29 10:12:40 -07001928 audio_module_handle_t module,
1929 audio_devices_t *pDevices,
1930 uint32_t *pSamplingRate,
1931 audio_format_t *pFormat,
1932 audio_channel_mask_t *pChannelMask,
1933 uint32_t *pLatencyMs,
Richard Fitzgeraldad3af332013-03-25 16:54:37 +00001934 audio_output_flags_t flags,
Eric Laurent2d388ec2014-03-07 13:25:54 -08001935 const audio_offload_info_t *offloadInfo);
1936audio_io_handle_t aps_open_dup_output(void *service __unused,
Dima Zavinfce7a472011-04-19 22:30:36 -07001937 audio_io_handle_t output1,
Eric Laurent2d388ec2014-03-07 13:25:54 -08001938 audio_io_handle_t output2);
1939int aps_close_output(void *service __unused, audio_io_handle_t output);
1940int aps_suspend_output(void *service __unused, audio_io_handle_t output);
1941int aps_restore_output(void *service __unused, audio_io_handle_t output);
1942audio_io_handle_t aps_open_input(void *service __unused,
Eric Laurenta4c5a552012-03-29 10:12:40 -07001943 audio_devices_t *pDevices,
1944 uint32_t *pSamplingRate,
1945 audio_format_t *pFormat,
1946 audio_channel_mask_t *pChannelMask,
Eric Laurent2d388ec2014-03-07 13:25:54 -08001947 audio_in_acoustics_t acoustics __unused);
1948audio_io_handle_t aps_open_input_on_module(void *service __unused,
Eric Laurenta4c5a552012-03-29 10:12:40 -07001949 audio_module_handle_t module,
1950 audio_devices_t *pDevices,
1951 uint32_t *pSamplingRate,
1952 audio_format_t *pFormat,
Eric Laurent2d388ec2014-03-07 13:25:54 -08001953 audio_channel_mask_t *pChannelMask);
1954int aps_close_input(void *service __unused, audio_io_handle_t input);
1955int aps_invalidate_stream(void *service __unused, audio_stream_type_t stream);
Glenn Kastend848eb42016-03-08 13:42:11 -08001956int aps_move_effects(void *service __unused, audio_session_t session,
Dima Zavinfce7a472011-04-19 22:30:36 -07001957 audio_io_handle_t src_output,
Eric Laurent2d388ec2014-03-07 13:25:54 -08001958 audio_io_handle_t dst_output);
1959char * aps_get_parameters(void *service __unused, audio_io_handle_t io_handle,
1960 const char *keys);
1961void aps_set_parameters(void *service, audio_io_handle_t io_handle,
1962 const char *kv_pairs, int delay_ms);
1963int aps_set_stream_volume(void *service, audio_stream_type_t stream,
Dima Zavinfce7a472011-04-19 22:30:36 -07001964 float volume, audio_io_handle_t output,
Eric Laurent2d388ec2014-03-07 13:25:54 -08001965 int delay_ms);
Eric Laurent2d388ec2014-03-07 13:25:54 -08001966int aps_set_voice_volume(void *service, float volume, int delay_ms);
1967};
Dima Zavinfce7a472011-04-19 22:30:36 -07001968
Mikhail Naganov1b2a7942017-12-08 10:18:09 -08001969} // namespace android