blob: a6e898972256efa321308f86206f3704fb83f013 [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 Laurent55fd81d2020-07-16 12:28:30 -0700491 // Clients capturing for Accessibility services or virtual sources are not considered
Eric Laurentc21d5692020-02-25 10:24:36 -0800492 // for top or latest active to avoid masking regular clients started before
Eric Laurent55fd81d2020-07-16 12:28:30 -0700493 if (!isAccessibility && !isVirtualSource(current->attributes.source)) {
Eric Laurentc21d5692020-02-25 10:24:36 -0800494 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;
Eric Laurentffabb492020-06-30 14:06:37 -0700569 return !(isInCall && !canCaptureCall);
570//TODO(b/160260850): restore restriction to mode owner once fix for misbehaving apps is merged
571// bool canCaptureCommunication = recordClient->canCaptureOutput
572// || recordClient->uid == mPhoneStateOwnerUid
573// || isServiceUid(mPhoneStateOwnerUid);
574// return !(isInCall && !canCaptureCall)
575// && !(isInCommunication && !canCaptureCommunication);
Eric Laurentc21d5692020-02-25 10:24:36 -0800576 };
Eric Laurent1ff16a72019-03-14 18:35:04 -0700577
578 // By default allow capture if:
579 // The assistant is not on TOP
Eric Laurenta171e352019-05-07 13:04:45 -0700580 // AND is on TOP or latest started
Eric Laurent1ff16a72019-03-14 18:35:04 -0700581 // AND there is no active privacy sensitive capture or call
582 // OR client has CAPTURE_AUDIO_OUTPUT privileged permission
583 bool allowCapture = !isAssistantOnTop
Eric Laurentc21d5692020-02-25 10:24:36 -0800584 && (isTopOrLatestActive || isTopOrLatestSensitive)
585 && !(isSensitiveActive
Ricardo Correa57a37692020-03-23 17:27:25 -0700586 && !(isTopOrLatestSensitive || current->canCaptureOutput))
Eric Laurentc21d5692020-02-25 10:24:36 -0800587 && canCaptureIfInCallOrCommunication(current);
Eric Laurent2dc962b2019-03-01 08:25:25 -0800588
589 if (isVirtualSource(source)) {
Eric Laurent1ff16a72019-03-14 18:35:04 -0700590 // Allow capture for virtual (remote submix, call audio TX or RX...) sources
591 allowCapture = true;
Eric Laurent2dc962b2019-03-01 08:25:25 -0800592 } else if (mUidPolicy->isAssistantUid(current->uid)) {
Eric Laurent1ff16a72019-03-14 18:35:04 -0700593 // For assistant allow capture if:
Eric Laurent6ede98f2019-06-11 14:50:30 -0700594 // An accessibility service is on TOP or a RTT call is active
Eric Laurent1ff16a72019-03-14 18:35:04 -0700595 // AND the source is VOICE_RECOGNITION or HOTWORD
Eric Laurenta171e352019-05-07 13:04:45 -0700596 // OR is on TOP AND uses VOICE_RECOGNITION
Eric Laurent1ff16a72019-03-14 18:35:04 -0700597 // OR uses HOTWORD
598 // AND there is no active privacy sensitive capture or call
599 // OR client has CAPTURE_AUDIO_OUTPUT privileged permission
Eric Laurent6ede98f2019-06-11 14:50:30 -0700600 if (isA11yOnTop || rttCallActive) {
Eric Laurent4eb58f12018-12-07 16:41:02 -0800601 if (source == AUDIO_SOURCE_HOTWORD || source == AUDIO_SOURCE_VOICE_RECOGNITION) {
Eric Laurent1ff16a72019-03-14 18:35:04 -0700602 allowCapture = true;
Eric Laurent4eb58f12018-12-07 16:41:02 -0800603 }
604 } else {
Eric Laurenta171e352019-05-07 13:04:45 -0700605 if (((isAssistantOnTop && source == AUDIO_SOURCE_VOICE_RECOGNITION) ||
Eric Laurentc21d5692020-02-25 10:24:36 -0800606 source == AUDIO_SOURCE_HOTWORD)
Ricardo Correa57a37692020-03-23 17:27:25 -0700607 && !(isSensitiveActive && !current->canCaptureOutput)
Eric Laurentc21d5692020-02-25 10:24:36 -0800608 && canCaptureIfInCallOrCommunication(current)) {
Eric Laurent1ff16a72019-03-14 18:35:04 -0700609 allowCapture = true;
Eric Laurent4eb58f12018-12-07 16:41:02 -0800610 }
611 }
612 } else if (mUidPolicy->isA11yUid(current->uid)) {
Eric Laurent1ff16a72019-03-14 18:35:04 -0700613 // For accessibility service allow capture if:
Eric Laurent47670c92019-08-28 16:59:05 -0700614 // The assistant is not on TOP
615 // AND there is no active privacy sensitive capture or call
Eric Laurent589171c2019-07-25 18:04:29 -0700616 // OR client has CAPTURE_AUDIO_OUTPUT privileged permission
Eric Laurent47670c92019-08-28 16:59:05 -0700617 // OR
618 // Is on TOP AND the source is VOICE_RECOGNITION or HOTWORD
619 if (!isAssistantOnTop
Ricardo Correa57a37692020-03-23 17:27:25 -0700620 && !(isSensitiveActive && !current->canCaptureOutput)
Eric Laurentc21d5692020-02-25 10:24:36 -0800621 && canCaptureIfInCallOrCommunication(current)) {
Eric Laurent47670c92019-08-28 16:59:05 -0700622 allowCapture = true;
623 }
Eric Laurent589171c2019-07-25 18:04:29 -0700624 if (isA11yOnTop) {
625 if (source == AUDIO_SOURCE_VOICE_RECOGNITION || source == AUDIO_SOURCE_HOTWORD) {
626 allowCapture = true;
627 }
Eric Laurent4eb58f12018-12-07 16:41:02 -0800628 }
Eric Laurent4e947da2019-10-17 15:24:06 -0700629 } else if (source == AUDIO_SOURCE_HOTWORD) {
630 // For HOTWORD source allow capture when not on TOP if:
631 // All active clients are using HOTWORD source
632 // AND no call is active
633 // OR client has CAPTURE_AUDIO_OUTPUT privileged permission
Eric Laurentc21d5692020-02-25 10:24:36 -0800634 if (onlyHotwordActive
635 && canCaptureIfInCallOrCommunication(current)) {
Eric Laurent4e947da2019-10-17 15:24:06 -0700636 allowCapture = true;
637 }
Kohsuke Yatoha623a132020-03-24 20:10:26 -0700638 } else if (mUidPolicy->isCurrentImeUid(current->uid)) {
639 // For current InputMethodService allow capture if:
640 // A RTT call is active AND the source is VOICE_RECOGNITION
641 if (rttCallActive && source == AUDIO_SOURCE_VOICE_RECOGNITION) {
642 allowCapture = true;
643 }
Eric Laurent4eb58f12018-12-07 16:41:02 -0800644 }
Eric Laurent5ada82e2019-08-29 17:53:54 -0700645 setAppState_l(current->portId,
Eric Laurent1ff16a72019-03-14 18:35:04 -0700646 allowCapture ? apmStatFromAmState(mUidPolicy->getUidState(current->uid)) :
647 APP_STATE_IDLE);
Eric Laurente8c8b432018-10-17 10:08:02 -0700648 }
649}
650
Michael Groovercfd28302018-12-11 19:16:46 -0800651void AudioPolicyService::silenceAllRecordings_l() {
652 for (size_t i = 0; i < mAudioRecordClients.size(); i++) {
653 sp<AudioRecordClient> current = mAudioRecordClients[i];
Eric Laurent1ff16a72019-03-14 18:35:04 -0700654 if (!isVirtualSource(current->attributes.source)) {
Eric Laurent5ada82e2019-08-29 17:53:54 -0700655 setAppState_l(current->portId, APP_STATE_IDLE);
Eric Laurent1ff16a72019-03-14 18:35:04 -0700656 }
Michael Groovercfd28302018-12-11 19:16:46 -0800657 }
658}
659
Eric Laurente8c8b432018-10-17 10:08:02 -0700660/* static */
661app_state_t AudioPolicyService::apmStatFromAmState(int amState) {
Eric Laurent1ff16a72019-03-14 18:35:04 -0700662
663 if (amState == ActivityManager::PROCESS_STATE_UNKNOWN) {
Eric Laurente8c8b432018-10-17 10:08:02 -0700664 return APP_STATE_IDLE;
Eric Laurent1ff16a72019-03-14 18:35:04 -0700665 } else if (amState <= ActivityManager::PROCESS_STATE_TOP) {
666 // include persistent services
667 return APP_STATE_TOP;
Eric Laurente8c8b432018-10-17 10:08:02 -0700668 }
669 return APP_STATE_FOREGROUND;
670}
671
Eric Laurent4eb58f12018-12-07 16:41:02 -0800672/* static */
Eric Laurent2dc962b2019-03-01 08:25:25 -0800673bool AudioPolicyService::isVirtualSource(audio_source_t source)
Eric Laurent4eb58f12018-12-07 16:41:02 -0800674{
675 switch (source) {
676 case AUDIO_SOURCE_VOICE_UPLINK:
677 case AUDIO_SOURCE_VOICE_DOWNLINK:
678 case AUDIO_SOURCE_VOICE_CALL:
Eric Laurent2dc962b2019-03-01 08:25:25 -0800679 case AUDIO_SOURCE_REMOTE_SUBMIX:
680 case AUDIO_SOURCE_FM_TUNER:
Eric Laurent68eb2122020-04-30 17:40:57 -0700681 case AUDIO_SOURCE_ECHO_REFERENCE:
Eric Laurent4eb58f12018-12-07 16:41:02 -0800682 return true;
683 default:
684 break;
685 }
686 return false;
687}
688
Eric Laurent5ada82e2019-08-29 17:53:54 -0700689void AudioPolicyService::setAppState_l(audio_port_handle_t portId, app_state_t state)
Eric Laurente8c8b432018-10-17 10:08:02 -0700690{
691 AutoCallerClear acc;
692
693 if (mAudioPolicyManager) {
Eric Laurent5ada82e2019-08-29 17:53:54 -0700694 mAudioPolicyManager->setAppState(portId, state);
Mikhail Naganoveae73eb2018-04-03 16:57:36 -0700695 }
696 sp<IAudioFlinger> af = AudioSystem::get_audio_flinger();
697 if (af) {
Eric Laurentf32108e2018-10-04 17:22:04 -0700698 bool silenced = state == APP_STATE_IDLE;
Eric Laurent5ada82e2019-08-29 17:53:54 -0700699 af->setRecordSilenced(portId, silenced);
Mikhail Naganoveae73eb2018-04-03 16:57:36 -0700700 }
Svet Ganovf4ddfef2018-01-16 07:37:58 -0800701}
702
Glenn Kasten0f11b512014-01-31 16:18:54 -0800703status_t AudioPolicyService::dump(int fd, const Vector<String16>& args __unused)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700704{
Glenn Kasten44deb052012-02-05 18:09:08 -0800705 if (!dumpAllowed()) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700706 dumpPermissionDenial(fd);
707 } else {
Mikhail Naganov12b716c2020-04-30 22:37:43 +0000708 const bool locked = dumpTryLock(mLock);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700709 if (!locked) {
710 String8 result(kDeadlockedString);
711 write(fd, result.string(), result.size());
712 }
713
714 dumpInternals(fd);
Glenn Kasten9d1f02d2012-02-08 17:47:58 -0800715 if (mAudioCommandThread != 0) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700716 mAudioCommandThread->dump(fd);
717 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700718
Eric Laurentdce54a12014-03-10 12:19:46 -0700719 if (mAudioPolicyManager) {
720 mAudioPolicyManager->dump(fd);
721 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700722
Kevin Rocard8be94972019-02-22 13:26:25 -0800723 mPackageManager.dump(fd);
724
Mikhail Naganov12b716c2020-04-30 22:37:43 +0000725 dumpReleaseLock(mLock, locked);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700726 }
727 return NO_ERROR;
728}
729
730status_t AudioPolicyService::dumpPermissionDenial(int fd)
731{
732 const size_t SIZE = 256;
733 char buffer[SIZE];
734 String8 result;
735 snprintf(buffer, SIZE, "Permission Denial: "
736 "can't dump AudioPolicyService from pid=%d, uid=%d\n",
737 IPCThreadState::self()->getCallingPid(),
738 IPCThreadState::self()->getCallingUid());
739 result.append(buffer);
740 write(fd, result.string(), result.size());
741 return NO_ERROR;
742}
743
744status_t AudioPolicyService::onTransact(
Svet Ganovf4ddfef2018-01-16 07:37:58 -0800745 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags) {
746 switch (code) {
747 case SHELL_COMMAND_TRANSACTION: {
748 int in = data.readFileDescriptor();
749 int out = data.readFileDescriptor();
750 int err = data.readFileDescriptor();
751 int argc = data.readInt32();
752 Vector<String16> args;
753 for (int i = 0; i < argc && data.dataAvail() > 0; i++) {
754 args.add(data.readString16());
755 }
756 sp<IBinder> unusedCallback;
757 sp<IResultReceiver> resultReceiver;
758 status_t status;
759 if ((status = data.readNullableStrongBinder(&unusedCallback)) != NO_ERROR) {
760 return status;
761 }
762 if ((status = data.readNullableStrongBinder(&resultReceiver)) != NO_ERROR) {
763 return status;
764 }
765 status = shellCommand(in, out, err, args);
766 if (resultReceiver != nullptr) {
767 resultReceiver->send(status);
768 }
769 return NO_ERROR;
770 }
771 }
772
Mathias Agopian65ab4712010-07-14 17:59:35 -0700773 return BnAudioPolicyService::onTransact(code, data, reply, flags);
774}
775
Svet Ganovf4ddfef2018-01-16 07:37:58 -0800776// ------------------- Shell command implementation -------------------
777
778// NOTE: This is a remote API - make sure all args are validated
779status_t AudioPolicyService::shellCommand(int in, int out, int err, Vector<String16>& args) {
780 if (!checkCallingPermission(sManageAudioPolicyPermission, nullptr, nullptr)) {
781 return PERMISSION_DENIED;
782 }
783 if (in == BAD_TYPE || out == BAD_TYPE || err == BAD_TYPE) {
784 return BAD_VALUE;
785 }
jovanakbe066e12019-09-02 11:54:39 -0700786 if (args.size() >= 3 && args[0] == String16("set-uid-state")) {
Svet Ganovf4ddfef2018-01-16 07:37:58 -0800787 return handleSetUidState(args, err);
jovanakbe066e12019-09-02 11:54:39 -0700788 } else if (args.size() >= 2 && args[0] == String16("reset-uid-state")) {
Svet Ganovf4ddfef2018-01-16 07:37:58 -0800789 return handleResetUidState(args, err);
jovanakbe066e12019-09-02 11:54:39 -0700790 } else if (args.size() >= 2 && args[0] == String16("get-uid-state")) {
Svet Ganovf4ddfef2018-01-16 07:37:58 -0800791 return handleGetUidState(args, out, err);
792 } else if (args.size() == 1 && args[0] == String16("help")) {
793 printHelp(out);
794 return NO_ERROR;
795 }
796 printHelp(err);
797 return BAD_VALUE;
798}
799
jovanakbe066e12019-09-02 11:54:39 -0700800static status_t getUidForPackage(String16 packageName, int userId, /*inout*/uid_t& uid, int err) {
801 if (userId < 0) {
802 ALOGE("Invalid user: %d", userId);
803 dprintf(err, "Invalid user: %d\n", userId);
Svet Ganovf4ddfef2018-01-16 07:37:58 -0800804 return BAD_VALUE;
805 }
jovanakbe066e12019-09-02 11:54:39 -0700806
807 PermissionController pc;
808 uid = pc.getPackageUid(packageName, 0);
809 if (uid <= 0) {
810 ALOGE("Unknown package: '%s'", String8(packageName).string());
811 dprintf(err, "Unknown package: '%s'\n", String8(packageName).string());
812 return BAD_VALUE;
813 }
814
815 uid = multiuser_get_uid(userId, uid);
816 return NO_ERROR;
817}
818
819status_t AudioPolicyService::handleSetUidState(Vector<String16>& args, int err) {
820 // Valid arg.size() is 3 or 5, args.size() is 5 with --user option.
821 if (!(args.size() == 3 || args.size() == 5)) {
822 printHelp(err);
823 return BAD_VALUE;
824 }
825
Svet Ganovf4ddfef2018-01-16 07:37:58 -0800826 bool active = false;
827 if (args[2] == String16("active")) {
828 active = true;
829 } else if ((args[2] != String16("idle"))) {
830 ALOGE("Expected active or idle but got: '%s'", String8(args[2]).string());
831 return BAD_VALUE;
832 }
jovanakbe066e12019-09-02 11:54:39 -0700833
834 int userId = 0;
835 if (args.size() >= 5 && args[3] == String16("--user")) {
836 userId = atoi(String8(args[4]));
837 }
838
839 uid_t uid;
840 if (getUidForPackage(args[1], userId, uid, err) == BAD_VALUE) {
841 return BAD_VALUE;
842 }
843
Mikhail Naganov12b716c2020-04-30 22:37:43 +0000844 sp<UidPolicy> uidPolicy;
845 {
846 Mutex::Autolock _l(mLock);
847 uidPolicy = mUidPolicy;
848 }
849 if (uidPolicy) {
850 uidPolicy->addOverrideUid(uid, active);
851 return NO_ERROR;
852 }
853 return NO_INIT;
Svet Ganovf4ddfef2018-01-16 07:37:58 -0800854}
855
856status_t AudioPolicyService::handleResetUidState(Vector<String16>& args, int err) {
jovanakbe066e12019-09-02 11:54:39 -0700857 // Valid arg.size() is 2 or 4, args.size() is 4 with --user option.
858 if (!(args.size() == 2 || args.size() == 4)) {
859 printHelp(err);
Svet Ganovf4ddfef2018-01-16 07:37:58 -0800860 return BAD_VALUE;
861 }
jovanakbe066e12019-09-02 11:54:39 -0700862
863 int userId = 0;
864 if (args.size() >= 4 && args[2] == String16("--user")) {
865 userId = atoi(String8(args[3]));
866 }
867
868 uid_t uid;
869 if (getUidForPackage(args[1], userId, uid, err) == BAD_VALUE) {
870 return BAD_VALUE;
871 }
872
Mikhail Naganov12b716c2020-04-30 22:37:43 +0000873 sp<UidPolicy> uidPolicy;
874 {
875 Mutex::Autolock _l(mLock);
876 uidPolicy = mUidPolicy;
877 }
878 if (uidPolicy) {
879 uidPolicy->removeOverrideUid(uid);
880 return NO_ERROR;
881 }
882 return NO_INIT;
Svet Ganovf4ddfef2018-01-16 07:37:58 -0800883}
884
885status_t AudioPolicyService::handleGetUidState(Vector<String16>& args, int out, int err) {
jovanakbe066e12019-09-02 11:54:39 -0700886 // Valid arg.size() is 2 or 4, args.size() is 4 with --user option.
887 if (!(args.size() == 2 || args.size() == 4)) {
888 printHelp(err);
Svet Ganovf4ddfef2018-01-16 07:37:58 -0800889 return BAD_VALUE;
890 }
jovanakbe066e12019-09-02 11:54:39 -0700891
892 int userId = 0;
893 if (args.size() >= 4 && args[2] == String16("--user")) {
894 userId = atoi(String8(args[3]));
895 }
896
897 uid_t uid;
898 if (getUidForPackage(args[1], userId, uid, err) == BAD_VALUE) {
899 return BAD_VALUE;
900 }
901
Mikhail Naganov12b716c2020-04-30 22:37:43 +0000902 sp<UidPolicy> uidPolicy;
903 {
904 Mutex::Autolock _l(mLock);
905 uidPolicy = mUidPolicy;
Svet Ganovf4ddfef2018-01-16 07:37:58 -0800906 }
Mikhail Naganov12b716c2020-04-30 22:37:43 +0000907 if (uidPolicy) {
908 return dprintf(out, uidPolicy->isUidActive(uid) ? "active\n" : "idle\n");
909 }
910 return NO_INIT;
Svet Ganovf4ddfef2018-01-16 07:37:58 -0800911}
912
913status_t AudioPolicyService::printHelp(int out) {
914 return dprintf(out, "Audio policy service commands:\n"
jovanakbe066e12019-09-02 11:54:39 -0700915 " get-uid-state <PACKAGE> [--user USER_ID] gets the uid state\n"
916 " set-uid-state <PACKAGE> <active|idle> [--user USER_ID] overrides the uid state\n"
917 " reset-uid-state <PACKAGE> [--user USER_ID] clears the uid state override\n"
Svet Ganovf4ddfef2018-01-16 07:37:58 -0800918 " help print this message\n");
919}
920
921// ----------- AudioPolicyService::UidPolicy implementation ----------
922
923void AudioPolicyService::UidPolicy::registerSelf() {
Steven Moreland2f348142019-07-02 15:59:07 -0700924 status_t res = mAm.linkToDeath(this);
925 mAm.registerUidObserver(this, ActivityManager::UID_OBSERVER_GONE
Svet Ganovf4ddfef2018-01-16 07:37:58 -0800926 | ActivityManager::UID_OBSERVER_IDLE
Eric Laurente8c8b432018-10-17 10:08:02 -0700927 | ActivityManager::UID_OBSERVER_ACTIVE
928 | ActivityManager::UID_OBSERVER_PROCSTATE,
Svet Ganovf4ddfef2018-01-16 07:37:58 -0800929 ActivityManager::PROCESS_STATE_UNKNOWN,
930 String16("audioserver"));
Mikhail Naganoveae73eb2018-04-03 16:57:36 -0700931 if (!res) {
932 Mutex::Autolock _l(mLock);
933 mObserverRegistered = true;
934 } else {
935 ALOGE("UidPolicy::registerSelf linkToDeath failed: %d", res);
Eric Laurent4eb58f12018-12-07 16:41:02 -0800936
Steven Moreland2f348142019-07-02 15:59:07 -0700937 mAm.unregisterUidObserver(this);
Mikhail Naganoveae73eb2018-04-03 16:57:36 -0700938 }
Svet Ganovf4ddfef2018-01-16 07:37:58 -0800939}
940
941void AudioPolicyService::UidPolicy::unregisterSelf() {
Steven Moreland2f348142019-07-02 15:59:07 -0700942 mAm.unlinkToDeath(this);
943 mAm.unregisterUidObserver(this);
Mikhail Naganoveae73eb2018-04-03 16:57:36 -0700944 Mutex::Autolock _l(mLock);
945 mObserverRegistered = false;
Svet Ganovf4ddfef2018-01-16 07:37:58 -0800946}
947
Mikhail Naganoveae73eb2018-04-03 16:57:36 -0700948void AudioPolicyService::UidPolicy::binderDied(__unused const wp<IBinder> &who) {
949 Mutex::Autolock _l(mLock);
950 mCachedUids.clear();
951 mObserverRegistered = false;
Svet Ganovf4ddfef2018-01-16 07:37:58 -0800952}
953
Eric Laurente8c8b432018-10-17 10:08:02 -0700954void AudioPolicyService::UidPolicy::checkRegistered() {
Mikhail Naganoveae73eb2018-04-03 16:57:36 -0700955 bool needToReregister = false;
956 {
957 Mutex::Autolock _l(mLock);
958 needToReregister = !mObserverRegistered;
Svet Ganovf4ddfef2018-01-16 07:37:58 -0800959 }
Mikhail Naganoveae73eb2018-04-03 16:57:36 -0700960 if (needToReregister) {
961 // Looks like ActivityManager has died previously, attempt to re-register.
962 registerSelf();
963 }
Eric Laurente8c8b432018-10-17 10:08:02 -0700964}
965
966bool AudioPolicyService::UidPolicy::isUidActive(uid_t uid) {
967 if (isServiceUid(uid)) return true;
968 checkRegistered();
Mikhail Naganoveae73eb2018-04-03 16:57:36 -0700969 {
970 Mutex::Autolock _l(mLock);
971 auto overrideIter = mOverrideUids.find(uid);
972 if (overrideIter != mOverrideUids.end()) {
Eric Laurente8c8b432018-10-17 10:08:02 -0700973 return overrideIter->second.first;
Mikhail Naganoveae73eb2018-04-03 16:57:36 -0700974 }
975 // In an absense of the ActivityManager, assume everything to be active.
976 if (!mObserverRegistered) return true;
977 auto cacheIter = mCachedUids.find(uid);
Mikhail Naganoveba668a2018-04-05 08:13:15 -0700978 if (cacheIter != mCachedUids.end()) {
Eric Laurente8c8b432018-10-17 10:08:02 -0700979 return cacheIter->second.first;
Mikhail Naganoveae73eb2018-04-03 16:57:36 -0700980 }
981 }
982 ActivityManager am;
Hui Yu12c7ec72020-05-04 17:40:52 +0000983 bool active = am.isUidActive(uid, String16("audioserver"));
Mikhail Naganoveae73eb2018-04-03 16:57:36 -0700984 {
985 Mutex::Autolock _l(mLock);
Eric Laurente8c8b432018-10-17 10:08:02 -0700986 mCachedUids.insert(std::pair<uid_t,
987 std::pair<bool, int>>(uid, std::pair<bool, int>(active,
988 ActivityManager::PROCESS_STATE_UNKNOWN)));
Mikhail Naganoveae73eb2018-04-03 16:57:36 -0700989 }
990 return active;
Svet Ganovf4ddfef2018-01-16 07:37:58 -0800991}
992
Eric Laurente8c8b432018-10-17 10:08:02 -0700993int AudioPolicyService::UidPolicy::getUidState(uid_t uid) {
994 if (isServiceUid(uid)) {
995 return ActivityManager::PROCESS_STATE_TOP;
996 }
997 checkRegistered();
998 {
999 Mutex::Autolock _l(mLock);
1000 auto overrideIter = mOverrideUids.find(uid);
1001 if (overrideIter != mOverrideUids.end()) {
1002 if (overrideIter->second.first) {
1003 if (overrideIter->second.second != ActivityManager::PROCESS_STATE_UNKNOWN) {
1004 return overrideIter->second.second;
1005 } else {
1006 auto cacheIter = mCachedUids.find(uid);
1007 if (cacheIter != mCachedUids.end()) {
1008 return cacheIter->second.second;
1009 }
1010 }
1011 }
1012 return ActivityManager::PROCESS_STATE_UNKNOWN;
1013 }
1014 // In an absense of the ActivityManager, assume everything to be active.
1015 if (!mObserverRegistered) {
1016 return ActivityManager::PROCESS_STATE_TOP;
1017 }
1018 auto cacheIter = mCachedUids.find(uid);
1019 if (cacheIter != mCachedUids.end()) {
1020 if (cacheIter->second.first) {
1021 return cacheIter->second.second;
1022 } else {
1023 return ActivityManager::PROCESS_STATE_UNKNOWN;
1024 }
1025 }
1026 }
1027 ActivityManager am;
Hui Yu12c7ec72020-05-04 17:40:52 +00001028 bool active = am.isUidActive(uid, String16("audioserver"));
Eric Laurente8c8b432018-10-17 10:08:02 -07001029 int state = ActivityManager::PROCESS_STATE_UNKNOWN;
1030 if (active) {
1031 state = am.getUidProcessState(uid, String16("audioserver"));
1032 }
1033 {
1034 Mutex::Autolock _l(mLock);
1035 mCachedUids.insert(std::pair<uid_t,
1036 std::pair<bool, int>>(uid, std::pair<bool, int>(active, state)));
1037 }
Eric Laurent4eb58f12018-12-07 16:41:02 -08001038
Eric Laurente8c8b432018-10-17 10:08:02 -07001039 return state;
1040}
1041
Mikhail Naganoveae73eb2018-04-03 16:57:36 -07001042void AudioPolicyService::UidPolicy::onUidActive(uid_t uid) {
Eric Laurente8c8b432018-10-17 10:08:02 -07001043 updateUid(&mCachedUids, uid, true, ActivityManager::PROCESS_STATE_UNKNOWN, true);
Mikhail Naganoveae73eb2018-04-03 16:57:36 -07001044}
1045
1046void AudioPolicyService::UidPolicy::onUidGone(uid_t uid, __unused bool disabled) {
Eric Laurente8c8b432018-10-17 10:08:02 -07001047 updateUid(&mCachedUids, uid, false, ActivityManager::PROCESS_STATE_UNKNOWN, false);
Mikhail Naganoveae73eb2018-04-03 16:57:36 -07001048}
1049
1050void AudioPolicyService::UidPolicy::onUidIdle(uid_t uid, __unused bool disabled) {
Eric Laurente8c8b432018-10-17 10:08:02 -07001051 updateUid(&mCachedUids, uid, false, ActivityManager::PROCESS_STATE_UNKNOWN, true);
Mikhail Naganoveae73eb2018-04-03 16:57:36 -07001052}
1053
Eric Laurente8c8b432018-10-17 10:08:02 -07001054void AudioPolicyService::UidPolicy::onUidStateChanged(uid_t uid,
1055 int32_t procState,
Hui Yu13ad0eb2019-09-09 10:27:07 -07001056 int64_t procStateSeq __unused,
1057 int32_t capability __unused) {
Eric Laurente8c8b432018-10-17 10:08:02 -07001058 if (procState != ActivityManager::PROCESS_STATE_UNKNOWN) {
1059 updateUid(&mCachedUids, uid, true, procState, true);
Mikhail Naganoveae73eb2018-04-03 16:57:36 -07001060 }
1061}
1062
1063void AudioPolicyService::UidPolicy::updateOverrideUid(uid_t uid, bool active, bool insert) {
Eric Laurente8c8b432018-10-17 10:08:02 -07001064 updateUid(&mOverrideUids, uid, active, ActivityManager::PROCESS_STATE_UNKNOWN, insert);
1065}
1066
1067void AudioPolicyService::UidPolicy::notifyService() {
1068 sp<AudioPolicyService> service = mService.promote();
1069 if (service != nullptr) {
1070 service->updateUidStates();
Mikhail Naganoveae73eb2018-04-03 16:57:36 -07001071 }
1072}
1073
Eric Laurente8c8b432018-10-17 10:08:02 -07001074void AudioPolicyService::UidPolicy::updateUid(std::unordered_map<uid_t,
1075 std::pair<bool, int>> *uids,
1076 uid_t uid,
1077 bool active,
1078 int state,
1079 bool insert) {
1080 if (isServiceUid(uid)) {
1081 return;
1082 }
1083 bool wasActive = isUidActive(uid);
1084 int previousState = getUidState(uid);
Mikhail Naganoveae73eb2018-04-03 16:57:36 -07001085 {
1086 Mutex::Autolock _l(mLock);
Eric Laurente8c8b432018-10-17 10:08:02 -07001087 updateUidLocked(uids, uid, active, state, insert);
Mikhail Naganoveae73eb2018-04-03 16:57:36 -07001088 }
Eric Laurente8c8b432018-10-17 10:08:02 -07001089 if (wasActive != isUidActive(uid) || state != previousState) {
1090 notifyService();
1091 }
Mikhail Naganoveae73eb2018-04-03 16:57:36 -07001092}
1093
Eric Laurente8c8b432018-10-17 10:08:02 -07001094void AudioPolicyService::UidPolicy::updateUidLocked(std::unordered_map<uid_t,
1095 std::pair<bool, int>> *uids,
1096 uid_t uid,
1097 bool active,
1098 int state,
1099 bool insert) {
Mikhail Naganoveae73eb2018-04-03 16:57:36 -07001100 auto it = uids->find(uid);
1101 if (it != uids->end()) {
Mikhail Naganoveae73eb2018-04-03 16:57:36 -07001102 if (insert) {
Eric Laurente8c8b432018-10-17 10:08:02 -07001103 if (state == ActivityManager::PROCESS_STATE_UNKNOWN) {
1104 it->second.first = active;
1105 }
1106 if (it->second.first) {
1107 it->second.second = state;
1108 } else {
1109 it->second.second = ActivityManager::PROCESS_STATE_UNKNOWN;
1110 }
Mikhail Naganoveae73eb2018-04-03 16:57:36 -07001111 } else {
1112 uids->erase(it);
1113 }
Eric Laurente8c8b432018-10-17 10:08:02 -07001114 } else if (insert && (state == ActivityManager::PROCESS_STATE_UNKNOWN)) {
1115 uids->insert(std::pair<uid_t, std::pair<bool, int>>(uid,
1116 std::pair<bool, int>(active, state)));
Mikhail Naganoveae73eb2018-04-03 16:57:36 -07001117 }
Svet Ganovf4ddfef2018-01-16 07:37:58 -08001118}
Mathias Agopian65ab4712010-07-14 17:59:35 -07001119
Eric Laurent4eb58f12018-12-07 16:41:02 -08001120bool AudioPolicyService::UidPolicy::isA11yOnTop() {
1121 for (const auto &uid : mCachedUids) {
Eric Laurent47670c92019-08-28 16:59:05 -07001122 if (!isA11yUid(uid.first)) {
Eric Laurent4eb58f12018-12-07 16:41:02 -08001123 continue;
1124 }
Amith Yamasanibcbb3002019-01-23 13:53:33 -08001125 if (uid.second.second >= ActivityManager::PROCESS_STATE_TOP
1126 && uid.second.second <= ActivityManager::PROCESS_STATE_BOUND_FOREGROUND_SERVICE) {
Eric Laurent4eb58f12018-12-07 16:41:02 -08001127 return true;
1128 }
1129 }
1130 return false;
1131}
1132
Eric Laurentb78763e2018-10-17 10:08:02 -07001133bool AudioPolicyService::UidPolicy::isA11yUid(uid_t uid)
1134{
1135 std::vector<uid_t>::iterator it = find(mA11yUids.begin(), mA11yUids.end(), uid);
1136 return it != mA11yUids.end();
1137}
1138
Michael Groovercfd28302018-12-11 19:16:46 -08001139// ----------- AudioPolicyService::SensorPrivacyService implementation ----------
1140void AudioPolicyService::SensorPrivacyPolicy::registerSelf() {
1141 SensorPrivacyManager spm;
1142 mSensorPrivacyEnabled = spm.isSensorPrivacyEnabled();
1143 spm.addSensorPrivacyListener(this);
1144}
1145
1146void AudioPolicyService::SensorPrivacyPolicy::unregisterSelf() {
1147 SensorPrivacyManager spm;
1148 spm.removeSensorPrivacyListener(this);
1149}
1150
1151bool AudioPolicyService::SensorPrivacyPolicy::isSensorPrivacyEnabled() {
1152 return mSensorPrivacyEnabled;
1153}
1154
1155binder::Status AudioPolicyService::SensorPrivacyPolicy::onSensorPrivacyChanged(bool enabled) {
1156 mSensorPrivacyEnabled = enabled;
1157 sp<AudioPolicyService> service = mService.promote();
1158 if (service != nullptr) {
1159 service->updateUidStates();
1160 }
1161 return binder::Status::ok();
1162}
1163
Mathias Agopian65ab4712010-07-14 17:59:35 -07001164// ----------- AudioPolicyService::AudioCommandThread implementation ----------
1165
Eric Laurentbfb1b832013-01-07 09:53:42 -08001166AudioPolicyService::AudioCommandThread::AudioCommandThread(String8 name,
1167 const wp<AudioPolicyService>& service)
1168 : Thread(false), mName(name), mService(service)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001169{
Mathias Agopian65ab4712010-07-14 17:59:35 -07001170}
1171
1172
1173AudioPolicyService::AudioCommandThread::~AudioCommandThread()
1174{
Eric Laurentbfb1b832013-01-07 09:53:42 -08001175 if (!mAudioCommands.isEmpty()) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07001176 release_wake_lock(mName.string());
1177 }
1178 mAudioCommands.clear();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001179}
1180
1181void AudioPolicyService::AudioCommandThread::onFirstRef()
1182{
Eric Laurentbfb1b832013-01-07 09:53:42 -08001183 run(mName.string(), ANDROID_PRIORITY_AUDIO);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001184}
1185
1186bool AudioPolicyService::AudioCommandThread::threadLoop()
1187{
Eric Laurentd7eda8d2016-02-02 17:18:39 -08001188 nsecs_t waitTime = -1;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001189
1190 mLock.lock();
1191 while (!exitPending())
1192 {
Eric Laurent59a89232014-06-08 14:14:17 -07001193 sp<AudioPolicyService> svc;
1194 while (!mAudioCommands.isEmpty() && !exitPending()) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07001195 nsecs_t curTime = systemTime();
1196 // commands are sorted by increasing time stamp: execute them from index 0 and up
1197 if (mAudioCommands[0]->mTime <= curTime) {
Eric Laurent0ede8922014-05-09 18:04:42 -07001198 sp<AudioCommand> command = mAudioCommands[0];
Mathias Agopian65ab4712010-07-14 17:59:35 -07001199 mAudioCommands.removeAt(0);
Eric Laurent0ede8922014-05-09 18:04:42 -07001200 mLastCommand = command;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001201
1202 switch (command->mCommand) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07001203 case SET_VOLUME: {
Eric Laurent0ede8922014-05-09 18:04:42 -07001204 VolumeData *data = (VolumeData *)command->mParam.get();
Steve Block3856b092011-10-20 11:56:00 +01001205 ALOGV("AudioCommandThread() processing set volume stream %d, \
Eric Laurentde070132010-07-13 04:45:46 -07001206 volume %f, output %d", data->mStream, data->mVolume, data->mIO);
Andy Hungfe726a62018-09-27 15:17:25 -07001207 mLock.unlock();
Eric Laurentde070132010-07-13 04:45:46 -07001208 command->mStatus = AudioSystem::setStreamVolume(data->mStream,
1209 data->mVolume,
1210 data->mIO);
Andy Hungfe726a62018-09-27 15:17:25 -07001211 mLock.lock();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001212 }break;
1213 case SET_PARAMETERS: {
Eric Laurent0ede8922014-05-09 18:04:42 -07001214 ParametersData *data = (ParametersData *)command->mParam.get();
Glenn Kastene53b9ea2012-03-12 16:29:55 -07001215 ALOGV("AudioCommandThread() processing set parameters string %s, io %d",
1216 data->mKeyValuePairs.string(), data->mIO);
Andy Hungfe726a62018-09-27 15:17:25 -07001217 mLock.unlock();
Glenn Kastene53b9ea2012-03-12 16:29:55 -07001218 command->mStatus = AudioSystem::setParameters(data->mIO, data->mKeyValuePairs);
Andy Hungfe726a62018-09-27 15:17:25 -07001219 mLock.lock();
Glenn Kastene53b9ea2012-03-12 16:29:55 -07001220 }break;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001221 case SET_VOICE_VOLUME: {
Eric Laurent0ede8922014-05-09 18:04:42 -07001222 VoiceVolumeData *data = (VoiceVolumeData *)command->mParam.get();
Steve Block3856b092011-10-20 11:56:00 +01001223 ALOGV("AudioCommandThread() processing set voice volume volume %f",
Eric Laurentde070132010-07-13 04:45:46 -07001224 data->mVolume);
Andy Hungfe726a62018-09-27 15:17:25 -07001225 mLock.unlock();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001226 command->mStatus = AudioSystem::setVoiceVolume(data->mVolume);
Andy Hungfe726a62018-09-27 15:17:25 -07001227 mLock.lock();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001228 }break;
Eric Laurentbfb1b832013-01-07 09:53:42 -08001229 case STOP_OUTPUT: {
Eric Laurent0ede8922014-05-09 18:04:42 -07001230 StopOutputData *data = (StopOutputData *)command->mParam.get();
Eric Laurentd7fe0862018-07-14 16:48:01 -07001231 ALOGV("AudioCommandThread() processing stop output portId %d",
1232 data->mPortId);
Eric Laurent59a89232014-06-08 14:14:17 -07001233 svc = mService.promote();
Eric Laurentbfb1b832013-01-07 09:53:42 -08001234 if (svc == 0) {
1235 break;
1236 }
1237 mLock.unlock();
Eric Laurentd7fe0862018-07-14 16:48:01 -07001238 svc->doStopOutput(data->mPortId);
Eric Laurentbfb1b832013-01-07 09:53:42 -08001239 mLock.lock();
Eric Laurentbfb1b832013-01-07 09:53:42 -08001240 }break;
1241 case RELEASE_OUTPUT: {
Eric Laurent0ede8922014-05-09 18:04:42 -07001242 ReleaseOutputData *data = (ReleaseOutputData *)command->mParam.get();
Eric Laurentd7fe0862018-07-14 16:48:01 -07001243 ALOGV("AudioCommandThread() processing release output portId %d",
1244 data->mPortId);
Eric Laurent59a89232014-06-08 14:14:17 -07001245 svc = mService.promote();
Eric Laurentbfb1b832013-01-07 09:53:42 -08001246 if (svc == 0) {
1247 break;
1248 }
1249 mLock.unlock();
Eric Laurentd7fe0862018-07-14 16:48:01 -07001250 svc->doReleaseOutput(data->mPortId);
Eric Laurentbfb1b832013-01-07 09:53:42 -08001251 mLock.lock();
Eric Laurentbfb1b832013-01-07 09:53:42 -08001252 }break;
Eric Laurent951f4552014-05-20 10:48:17 -07001253 case CREATE_AUDIO_PATCH: {
1254 CreateAudioPatchData *data = (CreateAudioPatchData *)command->mParam.get();
1255 ALOGV("AudioCommandThread() processing create audio patch");
1256 sp<IAudioFlinger> af = AudioSystem::get_audio_flinger();
1257 if (af == 0) {
1258 command->mStatus = PERMISSION_DENIED;
1259 } else {
Andy Hungfe726a62018-09-27 15:17:25 -07001260 mLock.unlock();
Eric Laurent951f4552014-05-20 10:48:17 -07001261 command->mStatus = af->createAudioPatch(&data->mPatch, &data->mHandle);
Andy Hungfe726a62018-09-27 15:17:25 -07001262 mLock.lock();
Eric Laurent951f4552014-05-20 10:48:17 -07001263 }
1264 } break;
1265 case RELEASE_AUDIO_PATCH: {
1266 ReleaseAudioPatchData *data = (ReleaseAudioPatchData *)command->mParam.get();
1267 ALOGV("AudioCommandThread() processing release audio patch");
1268 sp<IAudioFlinger> af = AudioSystem::get_audio_flinger();
1269 if (af == 0) {
1270 command->mStatus = PERMISSION_DENIED;
1271 } else {
Andy Hungfe726a62018-09-27 15:17:25 -07001272 mLock.unlock();
Eric Laurent951f4552014-05-20 10:48:17 -07001273 command->mStatus = af->releaseAudioPatch(data->mHandle);
Andy Hungfe726a62018-09-27 15:17:25 -07001274 mLock.lock();
Eric Laurent951f4552014-05-20 10:48:17 -07001275 }
1276 } break;
Eric Laurentb52c1522014-05-20 11:27:36 -07001277 case UPDATE_AUDIOPORT_LIST: {
1278 ALOGV("AudioCommandThread() processing update audio port list");
Eric Laurent59a89232014-06-08 14:14:17 -07001279 svc = mService.promote();
Eric Laurentb52c1522014-05-20 11:27:36 -07001280 if (svc == 0) {
1281 break;
1282 }
1283 mLock.unlock();
1284 svc->doOnAudioPortListUpdate();
1285 mLock.lock();
1286 }break;
1287 case UPDATE_AUDIOPATCH_LIST: {
1288 ALOGV("AudioCommandThread() processing update audio patch list");
Eric Laurent59a89232014-06-08 14:14:17 -07001289 svc = mService.promote();
Eric Laurentb52c1522014-05-20 11:27:36 -07001290 if (svc == 0) {
1291 break;
1292 }
1293 mLock.unlock();
1294 svc->doOnAudioPatchListUpdate();
1295 mLock.lock();
1296 }break;
François Gaffiecfe17322018-11-07 13:41:29 +01001297 case CHANGED_AUDIOVOLUMEGROUP: {
1298 AudioVolumeGroupData *data =
1299 static_cast<AudioVolumeGroupData *>(command->mParam.get());
1300 ALOGV("AudioCommandThread() processing update audio volume group");
1301 svc = mService.promote();
1302 if (svc == 0) {
1303 break;
1304 }
1305 mLock.unlock();
1306 svc->doOnAudioVolumeGroupChanged(data->mGroup, data->mFlags);
1307 mLock.lock();
1308 }break;
Eric Laurente1715a42014-05-20 11:30:42 -07001309 case SET_AUDIOPORT_CONFIG: {
1310 SetAudioPortConfigData *data = (SetAudioPortConfigData *)command->mParam.get();
1311 ALOGV("AudioCommandThread() processing set port config");
1312 sp<IAudioFlinger> af = AudioSystem::get_audio_flinger();
1313 if (af == 0) {
1314 command->mStatus = PERMISSION_DENIED;
1315 } else {
Andy Hungfe726a62018-09-27 15:17:25 -07001316 mLock.unlock();
Eric Laurente1715a42014-05-20 11:30:42 -07001317 command->mStatus = af->setAudioPortConfig(&data->mConfig);
Andy Hungfe726a62018-09-27 15:17:25 -07001318 mLock.lock();
Eric Laurente1715a42014-05-20 11:30:42 -07001319 }
1320 } break;
Jean-Michel Trivide801052015-04-14 19:10:14 -07001321 case DYN_POLICY_MIX_STATE_UPDATE: {
1322 DynPolicyMixStateUpdateData *data =
1323 (DynPolicyMixStateUpdateData *)command->mParam.get();
Jean-Michel Trivide801052015-04-14 19:10:14 -07001324 ALOGV("AudioCommandThread() processing dyn policy mix state update %s %d",
1325 data->mRegId.string(), data->mState);
1326 svc = mService.promote();
1327 if (svc == 0) {
1328 break;
1329 }
1330 mLock.unlock();
1331 svc->doOnDynamicPolicyMixStateUpdate(data->mRegId, data->mState);
1332 mLock.lock();
1333 } break;
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -08001334 case RECORDING_CONFIGURATION_UPDATE: {
1335 RecordingConfigurationUpdateData *data =
1336 (RecordingConfigurationUpdateData *)command->mParam.get();
1337 ALOGV("AudioCommandThread() processing recording configuration update");
1338 svc = mService.promote();
1339 if (svc == 0) {
1340 break;
1341 }
1342 mLock.unlock();
Jean-Michel Triviac4e4292016-12-22 11:39:31 -08001343 svc->doOnRecordingConfigurationUpdate(data->mEvent, &data->mClientInfo,
Eric Laurenta9f86652018-11-28 17:23:11 -08001344 &data->mClientConfig, data->mClientEffects,
1345 &data->mDeviceConfig, data->mEffects,
1346 data->mPatchHandle, data->mSource);
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -08001347 mLock.lock();
1348 } break;
Eric Laurentb20cf7d2019-04-05 19:37:34 -07001349 case SET_EFFECT_SUSPENDED: {
1350 SetEffectSuspendedData *data = (SetEffectSuspendedData *)command->mParam.get();
1351 ALOGV("AudioCommandThread() processing set effect suspended");
1352 sp<IAudioFlinger> af = AudioSystem::get_audio_flinger();
1353 if (af != 0) {
1354 mLock.unlock();
1355 af->setEffectSuspended(data->mEffectId, data->mSessionId, data->mSuspended);
1356 mLock.lock();
1357 }
1358 } break;
Mikhail Naganov88b30d22020-03-09 19:43:13 +00001359 case AUDIO_MODULES_UPDATE: {
1360 ALOGV("AudioCommandThread() processing audio modules update");
1361 svc = mService.promote();
1362 if (svc == 0) {
1363 break;
1364 }
1365 mLock.unlock();
1366 svc->doOnNewAudioModulesAvailable();
1367 mLock.lock();
1368 } break;
Eric Laurentb20cf7d2019-04-05 19:37:34 -07001369
Mathias Agopian65ab4712010-07-14 17:59:35 -07001370 default:
Steve Block5ff1dd52012-01-05 23:22:43 +00001371 ALOGW("AudioCommandThread() unknown command %d", command->mCommand);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001372 }
Eric Laurent0ede8922014-05-09 18:04:42 -07001373 {
1374 Mutex::Autolock _l(command->mLock);
1375 if (command->mWaitStatus) {
1376 command->mWaitStatus = false;
1377 command->mCond.signal();
1378 }
1379 }
Eric Laurentd7eda8d2016-02-02 17:18:39 -08001380 waitTime = -1;
Zach Janga754b4f2015-10-27 01:29:34 +00001381 // release mLock before releasing strong reference on the service as
1382 // AudioPolicyService destructor calls AudioCommandThread::exit() which
1383 // acquires mLock.
1384 mLock.unlock();
1385 svc.clear();
1386 mLock.lock();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001387 } else {
1388 waitTime = mAudioCommands[0]->mTime - curTime;
1389 break;
1390 }
1391 }
Zach Janga754b4f2015-10-27 01:29:34 +00001392
1393 // release delayed commands wake lock if the queue is empty
1394 if (mAudioCommands.isEmpty()) {
Ricardo Garcia05f2fdc2014-07-24 15:48:24 -07001395 release_wake_lock(mName.string());
Zach Janga754b4f2015-10-27 01:29:34 +00001396 }
1397
1398 // At this stage we have either an empty command queue or the first command in the queue
1399 // has a finite delay. So unless we are exiting it is safe to wait.
1400 if (!exitPending()) {
Eric Laurent59a89232014-06-08 14:14:17 -07001401 ALOGV("AudioCommandThread() going to sleep");
Eric Laurentd7eda8d2016-02-02 17:18:39 -08001402 if (waitTime == -1) {
1403 mWaitWorkCV.wait(mLock);
1404 } else {
1405 mWaitWorkCV.waitRelative(mLock, waitTime);
1406 }
Eric Laurent59a89232014-06-08 14:14:17 -07001407 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001408 }
Ricardo Garcia05f2fdc2014-07-24 15:48:24 -07001409 // release delayed commands wake lock before quitting
1410 if (!mAudioCommands.isEmpty()) {
1411 release_wake_lock(mName.string());
1412 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001413 mLock.unlock();
1414 return false;
1415}
1416
1417status_t AudioPolicyService::AudioCommandThread::dump(int fd)
1418{
1419 const size_t SIZE = 256;
1420 char buffer[SIZE];
1421 String8 result;
1422
1423 snprintf(buffer, SIZE, "AudioCommandThread %p Dump\n", this);
1424 result.append(buffer);
1425 write(fd, result.string(), result.size());
1426
Mikhail Naganov12b716c2020-04-30 22:37:43 +00001427 const bool locked = dumpTryLock(mLock);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001428 if (!locked) {
1429 String8 result2(kCmdDeadlockedString);
1430 write(fd, result2.string(), result2.size());
1431 }
1432
1433 snprintf(buffer, SIZE, "- Commands:\n");
1434 result = String8(buffer);
1435 result.append(" Command Time Wait pParam\n");
Glenn Kasten8d6a2442012-02-08 14:04:28 -08001436 for (size_t i = 0; i < mAudioCommands.size(); i++) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07001437 mAudioCommands[i]->dump(buffer, SIZE);
1438 result.append(buffer);
1439 }
1440 result.append(" Last Command\n");
Eric Laurent0ede8922014-05-09 18:04:42 -07001441 if (mLastCommand != 0) {
1442 mLastCommand->dump(buffer, SIZE);
1443 result.append(buffer);
1444 } else {
1445 result.append(" none\n");
1446 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001447
1448 write(fd, result.string(), result.size());
1449
Mikhail Naganov12b716c2020-04-30 22:37:43 +00001450 dumpReleaseLock(mLock, locked);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001451
1452 return NO_ERROR;
1453}
1454
Glenn Kastenfff6d712012-01-12 16:38:12 -08001455status_t AudioPolicyService::AudioCommandThread::volumeCommand(audio_stream_type_t stream,
Eric Laurentde070132010-07-13 04:45:46 -07001456 float volume,
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001457 audio_io_handle_t output,
Eric Laurentde070132010-07-13 04:45:46 -07001458 int delayMs)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001459{
Eric Laurent0ede8922014-05-09 18:04:42 -07001460 sp<AudioCommand> command = new AudioCommand();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001461 command->mCommand = SET_VOLUME;
Eric Laurent0ede8922014-05-09 18:04:42 -07001462 sp<VolumeData> data = new VolumeData();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001463 data->mStream = stream;
1464 data->mVolume = volume;
1465 data->mIO = output;
1466 command->mParam = data;
Eric Laurent0ede8922014-05-09 18:04:42 -07001467 command->mWaitStatus = true;
Steve Block3856b092011-10-20 11:56:00 +01001468 ALOGV("AudioCommandThread() adding set volume stream %d, volume %f, output %d",
Eric Laurentde070132010-07-13 04:45:46 -07001469 stream, volume, output);
Eric Laurent0ede8922014-05-09 18:04:42 -07001470 return sendCommand(command, delayMs);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001471}
1472
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001473status_t AudioPolicyService::AudioCommandThread::parametersCommand(audio_io_handle_t ioHandle,
Dima Zavinfce7a472011-04-19 22:30:36 -07001474 const char *keyValuePairs,
Eric Laurentde070132010-07-13 04:45:46 -07001475 int delayMs)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001476{
Eric Laurent0ede8922014-05-09 18:04:42 -07001477 sp<AudioCommand> command = new AudioCommand();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001478 command->mCommand = SET_PARAMETERS;
Eric Laurent0ede8922014-05-09 18:04:42 -07001479 sp<ParametersData> data = new ParametersData();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001480 data->mIO = ioHandle;
Dima Zavinfce7a472011-04-19 22:30:36 -07001481 data->mKeyValuePairs = String8(keyValuePairs);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001482 command->mParam = data;
Eric Laurent0ede8922014-05-09 18:04:42 -07001483 command->mWaitStatus = true;
Steve Block3856b092011-10-20 11:56:00 +01001484 ALOGV("AudioCommandThread() adding set parameter string %s, io %d ,delay %d",
Dima Zavinfce7a472011-04-19 22:30:36 -07001485 keyValuePairs, ioHandle, delayMs);
Eric Laurent0ede8922014-05-09 18:04:42 -07001486 return sendCommand(command, delayMs);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001487}
1488
1489status_t AudioPolicyService::AudioCommandThread::voiceVolumeCommand(float volume, int delayMs)
1490{
Eric Laurent0ede8922014-05-09 18:04:42 -07001491 sp<AudioCommand> command = new AudioCommand();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001492 command->mCommand = SET_VOICE_VOLUME;
Eric Laurent0ede8922014-05-09 18:04:42 -07001493 sp<VoiceVolumeData> data = new VoiceVolumeData();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001494 data->mVolume = volume;
1495 command->mParam = data;
Eric Laurent0ede8922014-05-09 18:04:42 -07001496 command->mWaitStatus = true;
Steve Block3856b092011-10-20 11:56:00 +01001497 ALOGV("AudioCommandThread() adding set voice volume volume %f", volume);
Eric Laurent0ede8922014-05-09 18:04:42 -07001498 return sendCommand(command, delayMs);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001499}
1500
Eric Laurentb20cf7d2019-04-05 19:37:34 -07001501void AudioPolicyService::AudioCommandThread::setEffectSuspendedCommand(int effectId,
1502 audio_session_t sessionId,
1503 bool suspended)
1504{
1505 sp<AudioCommand> command = new AudioCommand();
1506 command->mCommand = SET_EFFECT_SUSPENDED;
1507 sp<SetEffectSuspendedData> data = new SetEffectSuspendedData();
1508 data->mEffectId = effectId;
1509 data->mSessionId = sessionId;
1510 data->mSuspended = suspended;
1511 command->mParam = data;
1512 ALOGV("AudioCommandThread() adding set suspended effectId %d sessionId %d suspended %d",
1513 effectId, sessionId, suspended);
1514 sendCommand(command);
1515}
1516
1517
Eric Laurentd7fe0862018-07-14 16:48:01 -07001518void AudioPolicyService::AudioCommandThread::stopOutputCommand(audio_port_handle_t portId)
Eric Laurentbfb1b832013-01-07 09:53:42 -08001519{
Eric Laurent0ede8922014-05-09 18:04:42 -07001520 sp<AudioCommand> command = new AudioCommand();
Eric Laurentbfb1b832013-01-07 09:53:42 -08001521 command->mCommand = STOP_OUTPUT;
Eric Laurent0ede8922014-05-09 18:04:42 -07001522 sp<StopOutputData> data = new StopOutputData();
Eric Laurentd7fe0862018-07-14 16:48:01 -07001523 data->mPortId = portId;
Jesper Tragardh48412dc2014-03-24 14:12:43 +01001524 command->mParam = data;
Eric Laurentd7fe0862018-07-14 16:48:01 -07001525 ALOGV("AudioCommandThread() adding stop output portId %d", portId);
Eric Laurent0ede8922014-05-09 18:04:42 -07001526 sendCommand(command);
Eric Laurentbfb1b832013-01-07 09:53:42 -08001527}
1528
Eric Laurentd7fe0862018-07-14 16:48:01 -07001529void AudioPolicyService::AudioCommandThread::releaseOutputCommand(audio_port_handle_t portId)
Eric Laurentbfb1b832013-01-07 09:53:42 -08001530{
Eric Laurent0ede8922014-05-09 18:04:42 -07001531 sp<AudioCommand> command = new AudioCommand();
Eric Laurentbfb1b832013-01-07 09:53:42 -08001532 command->mCommand = RELEASE_OUTPUT;
Eric Laurent0ede8922014-05-09 18:04:42 -07001533 sp<ReleaseOutputData> data = new ReleaseOutputData();
Eric Laurentd7fe0862018-07-14 16:48:01 -07001534 data->mPortId = portId;
Jesper Tragardh48412dc2014-03-24 14:12:43 +01001535 command->mParam = data;
Eric Laurentd7fe0862018-07-14 16:48:01 -07001536 ALOGV("AudioCommandThread() adding release output portId %d", portId);
Eric Laurent0ede8922014-05-09 18:04:42 -07001537 sendCommand(command);
1538}
1539
Eric Laurent951f4552014-05-20 10:48:17 -07001540status_t AudioPolicyService::AudioCommandThread::createAudioPatchCommand(
1541 const struct audio_patch *patch,
1542 audio_patch_handle_t *handle,
1543 int delayMs)
1544{
1545 status_t status = NO_ERROR;
1546
1547 sp<AudioCommand> command = new AudioCommand();
1548 command->mCommand = CREATE_AUDIO_PATCH;
1549 CreateAudioPatchData *data = new CreateAudioPatchData();
1550 data->mPatch = *patch;
1551 data->mHandle = *handle;
1552 command->mParam = data;
1553 command->mWaitStatus = true;
1554 ALOGV("AudioCommandThread() adding create patch delay %d", delayMs);
1555 status = sendCommand(command, delayMs);
1556 if (status == NO_ERROR) {
1557 *handle = data->mHandle;
1558 }
1559 return status;
1560}
1561
1562status_t AudioPolicyService::AudioCommandThread::releaseAudioPatchCommand(audio_patch_handle_t handle,
1563 int delayMs)
1564{
1565 sp<AudioCommand> command = new AudioCommand();
1566 command->mCommand = RELEASE_AUDIO_PATCH;
1567 ReleaseAudioPatchData *data = new ReleaseAudioPatchData();
1568 data->mHandle = handle;
1569 command->mParam = data;
1570 command->mWaitStatus = true;
1571 ALOGV("AudioCommandThread() adding release patch delay %d", delayMs);
1572 return sendCommand(command, delayMs);
1573}
1574
Eric Laurentb52c1522014-05-20 11:27:36 -07001575void AudioPolicyService::AudioCommandThread::updateAudioPortListCommand()
1576{
1577 sp<AudioCommand> command = new AudioCommand();
1578 command->mCommand = UPDATE_AUDIOPORT_LIST;
1579 ALOGV("AudioCommandThread() adding update audio port list");
1580 sendCommand(command);
1581}
1582
1583void AudioPolicyService::AudioCommandThread::updateAudioPatchListCommand()
1584{
1585 sp<AudioCommand>command = new AudioCommand();
1586 command->mCommand = UPDATE_AUDIOPATCH_LIST;
1587 ALOGV("AudioCommandThread() adding update audio patch list");
1588 sendCommand(command);
1589}
1590
François Gaffiecfe17322018-11-07 13:41:29 +01001591void AudioPolicyService::AudioCommandThread::changeAudioVolumeGroupCommand(volume_group_t group,
1592 int flags)
1593{
1594 sp<AudioCommand>command = new AudioCommand();
1595 command->mCommand = CHANGED_AUDIOVOLUMEGROUP;
1596 AudioVolumeGroupData *data= new AudioVolumeGroupData();
1597 data->mGroup = group;
1598 data->mFlags = flags;
1599 command->mParam = data;
1600 ALOGV("AudioCommandThread() adding audio volume group changed");
1601 sendCommand(command);
1602}
1603
Eric Laurente1715a42014-05-20 11:30:42 -07001604status_t AudioPolicyService::AudioCommandThread::setAudioPortConfigCommand(
1605 const struct audio_port_config *config, int delayMs)
1606{
1607 sp<AudioCommand> command = new AudioCommand();
1608 command->mCommand = SET_AUDIOPORT_CONFIG;
1609 SetAudioPortConfigData *data = new SetAudioPortConfigData();
1610 data->mConfig = *config;
1611 command->mParam = data;
1612 command->mWaitStatus = true;
1613 ALOGV("AudioCommandThread() adding set port config delay %d", delayMs);
1614 return sendCommand(command, delayMs);
1615}
1616
Jean-Michel Trivide801052015-04-14 19:10:14 -07001617void AudioPolicyService::AudioCommandThread::dynamicPolicyMixStateUpdateCommand(
Chih-Hung Hsiehe964d4e2016-08-09 14:31:32 -07001618 const String8& regId, int32_t state)
Jean-Michel Trivide801052015-04-14 19:10:14 -07001619{
1620 sp<AudioCommand> command = new AudioCommand();
1621 command->mCommand = DYN_POLICY_MIX_STATE_UPDATE;
1622 DynPolicyMixStateUpdateData *data = new DynPolicyMixStateUpdateData();
1623 data->mRegId = regId;
1624 data->mState = state;
1625 command->mParam = data;
1626 ALOGV("AudioCommandThread() sending dynamic policy mix (id=%s) state update to %d",
1627 regId.string(), state);
1628 sendCommand(command);
1629}
1630
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -08001631void AudioPolicyService::AudioCommandThread::recordingConfigurationUpdateCommand(
Eric Laurenta9f86652018-11-28 17:23:11 -08001632 int event,
1633 const record_client_info_t *clientInfo,
1634 const audio_config_base_t *clientConfig,
1635 std::vector<effect_descriptor_t> clientEffects,
1636 const audio_config_base_t *deviceConfig,
1637 std::vector<effect_descriptor_t> effects,
1638 audio_patch_handle_t patchHandle,
1639 audio_source_t source)
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -08001640{
1641 sp<AudioCommand>command = new AudioCommand();
1642 command->mCommand = RECORDING_CONFIGURATION_UPDATE;
1643 RecordingConfigurationUpdateData *data = new RecordingConfigurationUpdateData();
1644 data->mEvent = event;
Jean-Michel Triviac4e4292016-12-22 11:39:31 -08001645 data->mClientInfo = *clientInfo;
Jean-Michel Trivi7281aa92016-02-17 15:33:40 -08001646 data->mClientConfig = *clientConfig;
Eric Laurenta9f86652018-11-28 17:23:11 -08001647 data->mClientEffects = clientEffects;
Jean-Michel Trivi7281aa92016-02-17 15:33:40 -08001648 data->mDeviceConfig = *deviceConfig;
Eric Laurenta9f86652018-11-28 17:23:11 -08001649 data->mEffects = effects;
Jean-Michel Trivi8c7cf3b2016-02-25 17:08:24 -08001650 data->mPatchHandle = patchHandle;
Eric Laurenta9f86652018-11-28 17:23:11 -08001651 data->mSource = source;
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -08001652 command->mParam = data;
Jean-Michel Triviac4e4292016-12-22 11:39:31 -08001653 ALOGV("AudioCommandThread() adding recording configuration update event %d, source %d uid %u",
1654 event, clientInfo->source, clientInfo->uid);
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -08001655 sendCommand(command);
1656}
1657
Mikhail Naganov88b30d22020-03-09 19:43:13 +00001658void AudioPolicyService::AudioCommandThread::audioModulesUpdateCommand()
1659{
1660 sp<AudioCommand> command = new AudioCommand();
1661 command->mCommand = AUDIO_MODULES_UPDATE;
1662 sendCommand(command);
1663}
1664
Eric Laurent0ede8922014-05-09 18:04:42 -07001665status_t AudioPolicyService::AudioCommandThread::sendCommand(sp<AudioCommand>& command, int delayMs)
1666{
1667 {
1668 Mutex::Autolock _l(mLock);
1669 insertCommand_l(command, delayMs);
1670 mWaitWorkCV.signal();
1671 }
1672 Mutex::Autolock _l(command->mLock);
1673 while (command->mWaitStatus) {
1674 nsecs_t timeOutNs = kAudioCommandTimeoutNs + milliseconds(delayMs);
1675 if (command->mCond.waitRelative(command->mLock, timeOutNs) != NO_ERROR) {
1676 command->mStatus = TIMED_OUT;
1677 command->mWaitStatus = false;
1678 }
1679 }
1680 return command->mStatus;
Eric Laurentbfb1b832013-01-07 09:53:42 -08001681}
1682
Mathias Agopian65ab4712010-07-14 17:59:35 -07001683// insertCommand_l() must be called with mLock held
Eric Laurent0ede8922014-05-09 18:04:42 -07001684void AudioPolicyService::AudioCommandThread::insertCommand_l(sp<AudioCommand>& command, int delayMs)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001685{
Glenn Kasten8d6a2442012-02-08 14:04:28 -08001686 ssize_t i; // not size_t because i will count down to -1
Eric Laurent0ede8922014-05-09 18:04:42 -07001687 Vector < sp<AudioCommand> > removedCommands;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001688 command->mTime = systemTime() + milliseconds(delayMs);
1689
1690 // acquire wake lock to make sure delayed commands are processed
Eric Laurentbfb1b832013-01-07 09:53:42 -08001691 if (mAudioCommands.isEmpty()) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07001692 acquire_wake_lock(PARTIAL_WAKE_LOCK, mName.string());
1693 }
1694
1695 // check same pending commands with later time stamps and eliminate them
Ivan Lozano5ff158f2017-10-30 09:06:24 -07001696 for (i = (ssize_t)mAudioCommands.size()-1; i >= 0; i--) {
Eric Laurent0ede8922014-05-09 18:04:42 -07001697 sp<AudioCommand> command2 = mAudioCommands[i];
Mathias Agopian65ab4712010-07-14 17:59:35 -07001698 // commands are sorted by increasing time stamp: no need to scan the rest of mAudioCommands
1699 if (command2->mTime <= command->mTime) break;
Eric Laurente45b48a2014-09-04 16:40:57 -07001700
1701 // create audio patch or release audio patch commands are equivalent
1702 // with regard to filtering
1703 if ((command->mCommand == CREATE_AUDIO_PATCH) ||
1704 (command->mCommand == RELEASE_AUDIO_PATCH)) {
1705 if ((command2->mCommand != CREATE_AUDIO_PATCH) &&
1706 (command2->mCommand != RELEASE_AUDIO_PATCH)) {
1707 continue;
1708 }
1709 } else if (command2->mCommand != command->mCommand) continue;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001710
1711 switch (command->mCommand) {
1712 case SET_PARAMETERS: {
Eric Laurent0ede8922014-05-09 18:04:42 -07001713 ParametersData *data = (ParametersData *)command->mParam.get();
1714 ParametersData *data2 = (ParametersData *)command2->mParam.get();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001715 if (data->mIO != data2->mIO) break;
Steve Block3856b092011-10-20 11:56:00 +01001716 ALOGV("Comparing parameter command %s to new command %s",
Eric Laurentde070132010-07-13 04:45:46 -07001717 data2->mKeyValuePairs.string(), data->mKeyValuePairs.string());
Mathias Agopian65ab4712010-07-14 17:59:35 -07001718 AudioParameter param = AudioParameter(data->mKeyValuePairs);
1719 AudioParameter param2 = AudioParameter(data2->mKeyValuePairs);
1720 for (size_t j = 0; j < param.size(); j++) {
Glenn Kastene53b9ea2012-03-12 16:29:55 -07001721 String8 key;
1722 String8 value;
1723 param.getAt(j, key, value);
1724 for (size_t k = 0; k < param2.size(); k++) {
1725 String8 key2;
1726 String8 value2;
1727 param2.getAt(k, key2, value2);
1728 if (key2 == key) {
1729 param2.remove(key2);
1730 ALOGV("Filtering out parameter %s", key2.string());
1731 break;
1732 }
1733 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001734 }
1735 // if all keys have been filtered out, remove the command.
1736 // otherwise, update the key value pairs
1737 if (param2.size() == 0) {
1738 removedCommands.add(command2);
1739 } else {
1740 data2->mKeyValuePairs = param2.toString();
1741 }
Eric Laurent21e54562013-09-23 12:08:05 -07001742 command->mTime = command2->mTime;
1743 // force delayMs to non 0 so that code below does not request to wait for
1744 // command status as the command is now delayed
1745 delayMs = 1;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001746 } break;
1747
1748 case SET_VOLUME: {
Eric Laurent0ede8922014-05-09 18:04:42 -07001749 VolumeData *data = (VolumeData *)command->mParam.get();
1750 VolumeData *data2 = (VolumeData *)command2->mParam.get();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001751 if (data->mIO != data2->mIO) break;
1752 if (data->mStream != data2->mStream) break;
Steve Block3856b092011-10-20 11:56:00 +01001753 ALOGV("Filtering out volume command on output %d for stream %d",
Eric Laurentde070132010-07-13 04:45:46 -07001754 data->mIO, data->mStream);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001755 removedCommands.add(command2);
Eric Laurent21e54562013-09-23 12:08:05 -07001756 command->mTime = command2->mTime;
1757 // force delayMs to non 0 so that code below does not request to wait for
1758 // command status as the command is now delayed
1759 delayMs = 1;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001760 } break;
Eric Laurente45b48a2014-09-04 16:40:57 -07001761
Eric Laurentbaf35fe2016-07-27 15:36:53 -07001762 case SET_VOICE_VOLUME: {
1763 VoiceVolumeData *data = (VoiceVolumeData *)command->mParam.get();
1764 VoiceVolumeData *data2 = (VoiceVolumeData *)command2->mParam.get();
1765 ALOGV("Filtering out voice volume command value %f replaced by %f",
1766 data2->mVolume, data->mVolume);
1767 removedCommands.add(command2);
1768 command->mTime = command2->mTime;
1769 // force delayMs to non 0 so that code below does not request to wait for
1770 // command status as the command is now delayed
1771 delayMs = 1;
1772 } break;
1773
Eric Laurente45b48a2014-09-04 16:40:57 -07001774 case CREATE_AUDIO_PATCH:
1775 case RELEASE_AUDIO_PATCH: {
1776 audio_patch_handle_t handle;
Haynes Mathew Georgea2d4a6d2014-10-13 13:05:22 -07001777 struct audio_patch patch;
Eric Laurente45b48a2014-09-04 16:40:57 -07001778 if (command->mCommand == CREATE_AUDIO_PATCH) {
1779 handle = ((CreateAudioPatchData *)command->mParam.get())->mHandle;
Haynes Mathew Georgea2d4a6d2014-10-13 13:05:22 -07001780 patch = ((CreateAudioPatchData *)command->mParam.get())->mPatch;
Eric Laurente45b48a2014-09-04 16:40:57 -07001781 } else {
1782 handle = ((ReleaseAudioPatchData *)command->mParam.get())->mHandle;
Mikhail Naganov7be71d22018-05-23 16:51:46 -07001783 memset(&patch, 0, sizeof(patch));
Eric Laurente45b48a2014-09-04 16:40:57 -07001784 }
1785 audio_patch_handle_t handle2;
Haynes Mathew Georgea2d4a6d2014-10-13 13:05:22 -07001786 struct audio_patch patch2;
Eric Laurente45b48a2014-09-04 16:40:57 -07001787 if (command2->mCommand == CREATE_AUDIO_PATCH) {
1788 handle2 = ((CreateAudioPatchData *)command2->mParam.get())->mHandle;
Haynes Mathew Georgea2d4a6d2014-10-13 13:05:22 -07001789 patch2 = ((CreateAudioPatchData *)command2->mParam.get())->mPatch;
Eric Laurente45b48a2014-09-04 16:40:57 -07001790 } else {
1791 handle2 = ((ReleaseAudioPatchData *)command2->mParam.get())->mHandle;
Glenn Kastenf60b6b62015-07-06 10:53:26 -07001792 memset(&patch2, 0, sizeof(patch2));
Eric Laurente45b48a2014-09-04 16:40:57 -07001793 }
1794 if (handle != handle2) break;
Haynes Mathew Georgea2d4a6d2014-10-13 13:05:22 -07001795 /* Filter CREATE_AUDIO_PATCH commands only when they are issued for
1796 same output. */
1797 if( (command->mCommand == CREATE_AUDIO_PATCH) &&
1798 (command2->mCommand == CREATE_AUDIO_PATCH) ) {
1799 bool isOutputDiff = false;
1800 if (patch.num_sources == patch2.num_sources) {
1801 for (unsigned count = 0; count < patch.num_sources; count++) {
1802 if (patch.sources[count].id != patch2.sources[count].id) {
1803 isOutputDiff = true;
1804 break;
1805 }
1806 }
1807 if (isOutputDiff)
1808 break;
1809 }
1810 }
Eric Laurente45b48a2014-09-04 16:40:57 -07001811 ALOGV("Filtering out %s audio patch command for handle %d",
1812 (command->mCommand == CREATE_AUDIO_PATCH) ? "create" : "release", handle);
1813 removedCommands.add(command2);
1814 command->mTime = command2->mTime;
1815 // force delayMs to non 0 so that code below does not request to wait for
1816 // command status as the command is now delayed
1817 delayMs = 1;
1818 } break;
1819
Jean-Michel Trivide801052015-04-14 19:10:14 -07001820 case DYN_POLICY_MIX_STATE_UPDATE: {
1821
1822 } break;
1823
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -08001824 case RECORDING_CONFIGURATION_UPDATE: {
1825
1826 } break;
1827
Mathias Agopian65ab4712010-07-14 17:59:35 -07001828 default:
1829 break;
1830 }
1831 }
1832
1833 // remove filtered commands
1834 for (size_t j = 0; j < removedCommands.size(); j++) {
1835 // removed commands always have time stamps greater than current command
1836 for (size_t k = i + 1; k < mAudioCommands.size(); k++) {
Eric Laurent0ede8922014-05-09 18:04:42 -07001837 if (mAudioCommands[k].get() == removedCommands[j].get()) {
Steve Block3856b092011-10-20 11:56:00 +01001838 ALOGV("suppressing command: %d", mAudioCommands[k]->mCommand);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001839 mAudioCommands.removeAt(k);
1840 break;
1841 }
1842 }
1843 }
1844 removedCommands.clear();
1845
Eric Laurentaa79bef2015-01-15 14:33:51 -08001846 // Disable wait for status if delay is not 0.
1847 // Except for create audio patch command because the returned patch handle
1848 // is needed by audio policy manager
1849 if (delayMs != 0 && command->mCommand != CREATE_AUDIO_PATCH) {
Eric Laurentcec4abb2012-07-03 12:23:02 -07001850 command->mWaitStatus = false;
1851 }
Eric Laurentcec4abb2012-07-03 12:23:02 -07001852
Mathias Agopian65ab4712010-07-14 17:59:35 -07001853 // insert command at the right place according to its time stamp
Eric Laurent1e693b52014-07-09 15:03:28 -07001854 ALOGV("inserting command: %d at index %zd, num commands %zu",
1855 command->mCommand, i+1, mAudioCommands.size());
Mathias Agopian65ab4712010-07-14 17:59:35 -07001856 mAudioCommands.insertAt(command, i + 1);
1857}
1858
1859void AudioPolicyService::AudioCommandThread::exit()
1860{
Steve Block3856b092011-10-20 11:56:00 +01001861 ALOGV("AudioCommandThread::exit");
Mathias Agopian65ab4712010-07-14 17:59:35 -07001862 {
1863 AutoMutex _l(mLock);
1864 requestExit();
1865 mWaitWorkCV.signal();
1866 }
Zach Janga754b4f2015-10-27 01:29:34 +00001867 // Note that we can call it from the thread loop if all other references have been released
1868 // but it will safely return WOULD_BLOCK in this case
Mathias Agopian65ab4712010-07-14 17:59:35 -07001869 requestExitAndWait();
1870}
1871
1872void AudioPolicyService::AudioCommandThread::AudioCommand::dump(char* buffer, size_t size)
1873{
1874 snprintf(buffer, size, " %02d %06d.%03d %01u %p\n",
1875 mCommand,
1876 (int)ns2s(mTime),
1877 (int)ns2ms(mTime)%1000,
1878 mWaitStatus,
Eric Laurent0ede8922014-05-09 18:04:42 -07001879 mParam.get());
Mathias Agopian65ab4712010-07-14 17:59:35 -07001880}
1881
Dima Zavinfce7a472011-04-19 22:30:36 -07001882/******* helpers for the service_ops callbacks defined below *********/
1883void AudioPolicyService::setParameters(audio_io_handle_t ioHandle,
1884 const char *keyValuePairs,
1885 int delayMs)
1886{
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001887 mAudioCommandThread->parametersCommand(ioHandle, keyValuePairs,
Dima Zavinfce7a472011-04-19 22:30:36 -07001888 delayMs);
1889}
1890
1891int AudioPolicyService::setStreamVolume(audio_stream_type_t stream,
1892 float volume,
1893 audio_io_handle_t output,
1894 int delayMs)
1895{
Glenn Kastenfff6d712012-01-12 16:38:12 -08001896 return (int)mAudioCommandThread->volumeCommand(stream, volume,
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001897 output, delayMs);
Dima Zavinfce7a472011-04-19 22:30:36 -07001898}
1899
Dima Zavinfce7a472011-04-19 22:30:36 -07001900int AudioPolicyService::setVoiceVolume(float volume, int delayMs)
1901{
1902 return (int)mAudioCommandThread->voiceVolumeCommand(volume, delayMs);
1903}
1904
Eric Laurentb20cf7d2019-04-05 19:37:34 -07001905void AudioPolicyService::setEffectSuspended(int effectId,
1906 audio_session_t sessionId,
1907 bool suspended)
1908{
1909 mAudioCommandThread->setEffectSuspendedCommand(effectId, sessionId, suspended);
1910}
1911
Mikhail Naganov88b30d22020-03-09 19:43:13 +00001912void AudioPolicyService::onNewAudioModulesAvailable()
1913{
Mikhail Naganovd0e2c742020-03-25 15:59:59 -07001914 mOutputCommandThread->audioModulesUpdateCommand();
Mikhail Naganov88b30d22020-03-09 19:43:13 +00001915}
1916
Eric Laurentb20cf7d2019-04-05 19:37:34 -07001917
Dima Zavinfce7a472011-04-19 22:30:36 -07001918extern "C" {
Eric Laurent2d388ec2014-03-07 13:25:54 -08001919audio_module_handle_t aps_load_hw_module(void *service __unused,
1920 const char *name);
1921audio_io_handle_t aps_open_output(void *service __unused,
Eric Laurenta4c5a552012-03-29 10:12:40 -07001922 audio_devices_t *pDevices,
1923 uint32_t *pSamplingRate,
1924 audio_format_t *pFormat,
1925 audio_channel_mask_t *pChannelMask,
1926 uint32_t *pLatencyMs,
Eric Laurent2d388ec2014-03-07 13:25:54 -08001927 audio_output_flags_t flags);
Eric Laurenta4c5a552012-03-29 10:12:40 -07001928
Eric Laurent2d388ec2014-03-07 13:25:54 -08001929audio_io_handle_t aps_open_output_on_module(void *service __unused,
Eric Laurenta4c5a552012-03-29 10:12:40 -07001930 audio_module_handle_t module,
1931 audio_devices_t *pDevices,
1932 uint32_t *pSamplingRate,
1933 audio_format_t *pFormat,
1934 audio_channel_mask_t *pChannelMask,
1935 uint32_t *pLatencyMs,
Richard Fitzgeraldad3af332013-03-25 16:54:37 +00001936 audio_output_flags_t flags,
Eric Laurent2d388ec2014-03-07 13:25:54 -08001937 const audio_offload_info_t *offloadInfo);
1938audio_io_handle_t aps_open_dup_output(void *service __unused,
Dima Zavinfce7a472011-04-19 22:30:36 -07001939 audio_io_handle_t output1,
Eric Laurent2d388ec2014-03-07 13:25:54 -08001940 audio_io_handle_t output2);
1941int aps_close_output(void *service __unused, audio_io_handle_t output);
1942int aps_suspend_output(void *service __unused, audio_io_handle_t output);
1943int aps_restore_output(void *service __unused, audio_io_handle_t output);
1944audio_io_handle_t aps_open_input(void *service __unused,
Eric Laurenta4c5a552012-03-29 10:12:40 -07001945 audio_devices_t *pDevices,
1946 uint32_t *pSamplingRate,
1947 audio_format_t *pFormat,
1948 audio_channel_mask_t *pChannelMask,
Eric Laurent2d388ec2014-03-07 13:25:54 -08001949 audio_in_acoustics_t acoustics __unused);
1950audio_io_handle_t aps_open_input_on_module(void *service __unused,
Eric Laurenta4c5a552012-03-29 10:12:40 -07001951 audio_module_handle_t module,
1952 audio_devices_t *pDevices,
1953 uint32_t *pSamplingRate,
1954 audio_format_t *pFormat,
Eric Laurent2d388ec2014-03-07 13:25:54 -08001955 audio_channel_mask_t *pChannelMask);
1956int aps_close_input(void *service __unused, audio_io_handle_t input);
1957int aps_invalidate_stream(void *service __unused, audio_stream_type_t stream);
Glenn Kastend848eb42016-03-08 13:42:11 -08001958int aps_move_effects(void *service __unused, audio_session_t session,
Dima Zavinfce7a472011-04-19 22:30:36 -07001959 audio_io_handle_t src_output,
Eric Laurent2d388ec2014-03-07 13:25:54 -08001960 audio_io_handle_t dst_output);
1961char * aps_get_parameters(void *service __unused, audio_io_handle_t io_handle,
1962 const char *keys);
1963void aps_set_parameters(void *service, audio_io_handle_t io_handle,
1964 const char *kv_pairs, int delay_ms);
1965int aps_set_stream_volume(void *service, audio_stream_type_t stream,
Dima Zavinfce7a472011-04-19 22:30:36 -07001966 float volume, audio_io_handle_t output,
Eric Laurent2d388ec2014-03-07 13:25:54 -08001967 int delay_ms);
Eric Laurent2d388ec2014-03-07 13:25:54 -08001968int aps_set_voice_volume(void *service, float volume, int delay_ms);
1969};
Dima Zavinfce7a472011-04-19 22:30:36 -07001970
Mikhail Naganov1b2a7942017-12-08 10:18:09 -08001971} // namespace android