blob: c83512fed0b0f3fa08d037b691a64de52c5a7d9c [file] [log] [blame]
Mathias Agopian65ab4712010-07-14 17:59:35 -07001/*
2 * Copyright (C) 2009 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#define LOG_TAG "AudioPolicyService"
18//#define LOG_NDEBUG 0
19
Glenn Kasten153b9fe2013-07-15 11:23:36 -070020#include "Configuration.h"
Mathias Agopian65ab4712010-07-14 17:59:35 -070021#undef __STRICT_ANSI__
22#define __STDINT_LIMITS
23#define __STDC_LIMIT_MACROS
24#include <stdint.h>
Mathias Agopian65ab4712010-07-14 17:59:35 -070025#include <sys/time.h>
Mikhail Naganov959e2d02019-03-28 11:08:19 -070026
27#include <audio_utils/clock.h>
Mathias Agopian65ab4712010-07-14 17:59:35 -070028#include <binder/IServiceManager.h>
29#include <utils/Log.h>
30#include <cutils/properties.h>
31#include <binder/IPCThreadState.h>
Svet Ganovf4ddfef2018-01-16 07:37:58 -080032#include <binder/PermissionController.h>
33#include <binder/IResultReceiver.h>
Mathias Agopian65ab4712010-07-14 17:59:35 -070034#include <utils/String16.h>
35#include <utils/threads.h>
36#include "AudioPolicyService.h"
Mathias Agopian65ab4712010-07-14 17:59:35 -070037#include <hardware_legacy/power.h>
Eric Laurent7c7f10b2011-06-17 21:29:58 -070038#include <media/AudioEffect.h>
Chih-Hung Hsiehc84d9d22014-11-14 13:33:34 -080039#include <media/AudioParameter.h>
Andy Hungab7ef302018-05-15 19:35:29 -070040#include <mediautils/ServiceUtilities.h>
Michael Groovercfd28302018-12-11 19:16:46 -080041#include <sensorprivacy/SensorPrivacyManager.h>
Mathias Agopian65ab4712010-07-14 17:59:35 -070042
Dima Zavin64760242011-05-11 14:15:23 -070043#include <system/audio.h>
Dima Zavin7394a4f2011-06-13 18:16:26 -070044#include <system/audio_policy.h>
Mikhail Naganov61a4fac2016-10-13 14:44:18 -070045
Mathias Agopian65ab4712010-07-14 17:59:35 -070046namespace android {
47
Glenn Kasten8dad0e32012-01-09 08:41:22 -080048static const char kDeadlockedString[] = "AudioPolicyService may be deadlocked\n";
49static const char kCmdDeadlockedString[] = "AudioPolicyService command thread may be deadlocked\n";
Mathias Agopian65ab4712010-07-14 17:59:35 -070050
Mikhail Naganov959e2d02019-03-28 11:08:19 -070051static const int kDumpLockTimeoutNs = 1 * NANOS_PER_SECOND;
Mathias Agopian65ab4712010-07-14 17:59:35 -070052
Eric Laurent0ede8922014-05-09 18:04:42 -070053static const nsecs_t kAudioCommandTimeoutNs = seconds(3); // 3 seconds
Christer Fletcher5fa8c4b2013-01-18 15:27:03 +010054
Svet Ganovf4ddfef2018-01-16 07:37:58 -080055static const String16 sManageAudioPolicyPermission("android.permission.MANAGE_AUDIO_POLICY");
Dima Zavinfce7a472011-04-19 22:30:36 -070056
Mathias Agopian65ab4712010-07-14 17:59:35 -070057// ----------------------------------------------------------------------------
58
59AudioPolicyService::AudioPolicyService()
Eric Laurentdce54a12014-03-10 12:19:46 -070060 : BnAudioPolicyService(), mpAudioPolicyDev(NULL), mpAudioPolicy(NULL),
Eric Laurentbb6c9a02014-09-25 14:11:47 -070061 mAudioPolicyManager(NULL), mAudioPolicyClient(NULL), mPhoneState(AUDIO_MODE_INVALID)
Mathias Agopian65ab4712010-07-14 17:59:35 -070062{
Eric Laurentf5ada6e2014-10-09 17:49:00 -070063}
64
65void AudioPolicyService::onFirstRef()
66{
Eric Laurent8b1e80b2014-10-07 09:08:47 -070067 {
68 Mutex::Autolock _l(mLock);
Eric Laurent93575202011-01-18 18:39:02 -080069
Eric Laurent8b1e80b2014-10-07 09:08:47 -070070 // start audio commands thread
71 mAudioCommandThread = new AudioCommandThread(String8("ApmAudio"), this);
72 // start output activity command thread
73 mOutputCommandThread = new AudioCommandThread(String8("ApmOutput"), this);
Eric Laurentdce54a12014-03-10 12:19:46 -070074
Eric Laurent8b1e80b2014-10-07 09:08:47 -070075 mAudioPolicyClient = new AudioPolicyClient(this);
76 mAudioPolicyManager = createAudioPolicyManager(mAudioPolicyClient);
Eric Laurent8b1e80b2014-10-07 09:08:47 -070077 }
bryant_liuba2b4392014-06-11 16:49:30 +080078 // load audio processing modules
Eric Laurent8b1e80b2014-10-07 09:08:47 -070079 sp<AudioPolicyEffects>audioPolicyEffects = new AudioPolicyEffects();
80 {
81 Mutex::Autolock _l(mLock);
82 mAudioPolicyEffects = audioPolicyEffects;
83 }
Svet Ganovf4ddfef2018-01-16 07:37:58 -080084
85 mUidPolicy = new UidPolicy(this);
86 mUidPolicy->registerSelf();
Michael Groovercfd28302018-12-11 19:16:46 -080087
88 mSensorPrivacyPolicy = new SensorPrivacyPolicy(this);
89 mSensorPrivacyPolicy->registerSelf();
Mathias Agopian65ab4712010-07-14 17:59:35 -070090}
91
92AudioPolicyService::~AudioPolicyService()
93{
Mathias Agopian65ab4712010-07-14 17:59:35 -070094 mAudioCommandThread->exit();
Eric Laurent657ff612014-05-07 11:58:24 -070095 mOutputCommandThread->exit();
Eric Laurent7c7f10b2011-06-17 21:29:58 -070096
Eric Laurentf269b8e2014-06-09 20:01:29 -070097 destroyAudioPolicyManager(mAudioPolicyManager);
Eric Laurentdce54a12014-03-10 12:19:46 -070098 delete mAudioPolicyClient;
Eric Laurentb52c1522014-05-20 11:27:36 -070099
100 mNotificationClients.clear();
bryant_liuba2b4392014-06-11 16:49:30 +0800101 mAudioPolicyEffects.clear();
Svet Ganovf4ddfef2018-01-16 07:37:58 -0800102
103 mUidPolicy->unregisterSelf();
104 mUidPolicy.clear();
Michael Groovercfd28302018-12-11 19:16:46 -0800105
106 mSensorPrivacyPolicy->unregisterSelf();
107 mSensorPrivacyPolicy.clear();
Eric Laurentb52c1522014-05-20 11:27:36 -0700108}
109
110// A notification client is always registered by AudioSystem when the client process
111// connects to AudioPolicyService.
112void AudioPolicyService::registerClient(const sp<IAudioPolicyServiceClient>& client)
113{
Eric Laurent12590252015-08-21 18:40:20 -0700114 if (client == 0) {
115 ALOGW("%s got NULL client", __FUNCTION__);
116 return;
117 }
Eric Laurent0ebd5f92014-11-19 19:04:52 -0800118 Mutex::Autolock _l(mNotificationClientsLock);
Eric Laurentb52c1522014-05-20 11:27:36 -0700119
120 uid_t uid = IPCThreadState::self()->getCallingUid();
luochaojiang908c7d72018-06-21 14:58:04 +0800121 pid_t pid = IPCThreadState::self()->getCallingPid();
122 int64_t token = ((int64_t)uid<<32) | pid;
123
124 if (mNotificationClients.indexOfKey(token) < 0) {
Eric Laurentb52c1522014-05-20 11:27:36 -0700125 sp<NotificationClient> notificationClient = new NotificationClient(this,
126 client,
luochaojiang908c7d72018-06-21 14:58:04 +0800127 uid,
128 pid);
129 ALOGV("registerClient() client %p, uid %d pid %d", client.get(), uid, pid);
Eric Laurentb52c1522014-05-20 11:27:36 -0700130
luochaojiang908c7d72018-06-21 14:58:04 +0800131 mNotificationClients.add(token, notificationClient);
Eric Laurentb52c1522014-05-20 11:27:36 -0700132
Marco Nelissenf8880202014-11-14 07:58:25 -0800133 sp<IBinder> binder = IInterface::asBinder(client);
Eric Laurentb52c1522014-05-20 11:27:36 -0700134 binder->linkToDeath(notificationClient);
135 }
136}
137
Eric Laurente8726fe2015-06-26 09:39:24 -0700138void AudioPolicyService::setAudioPortCallbacksEnabled(bool enabled)
139{
140 Mutex::Autolock _l(mNotificationClientsLock);
141
142 uid_t uid = IPCThreadState::self()->getCallingUid();
luochaojiang908c7d72018-06-21 14:58:04 +0800143 pid_t pid = IPCThreadState::self()->getCallingPid();
144 int64_t token = ((int64_t)uid<<32) | pid;
145
146 if (mNotificationClients.indexOfKey(token) < 0) {
Eric Laurente8726fe2015-06-26 09:39:24 -0700147 return;
148 }
luochaojiang908c7d72018-06-21 14:58:04 +0800149 mNotificationClients.valueFor(token)->setAudioPortCallbacksEnabled(enabled);
Eric Laurente8726fe2015-06-26 09:39:24 -0700150}
151
François Gaffiecfe17322018-11-07 13:41:29 +0100152void AudioPolicyService::setAudioVolumeGroupCallbacksEnabled(bool enabled)
153{
154 Mutex::Autolock _l(mNotificationClientsLock);
155
156 uid_t uid = IPCThreadState::self()->getCallingUid();
157 pid_t pid = IPCThreadState::self()->getCallingPid();
158 int64_t token = ((int64_t)uid<<32) | pid;
159
160 if (mNotificationClients.indexOfKey(token) < 0) {
161 return;
162 }
163 mNotificationClients.valueFor(token)->setAudioVolumeGroupCallbacksEnabled(enabled);
164}
165
Eric Laurentb52c1522014-05-20 11:27:36 -0700166// removeNotificationClient() is called when the client process dies.
luochaojiang908c7d72018-06-21 14:58:04 +0800167void AudioPolicyService::removeNotificationClient(uid_t uid, pid_t pid)
Eric Laurentb52c1522014-05-20 11:27:36 -0700168{
Eric Laurent0ebd5f92014-11-19 19:04:52 -0800169 {
170 Mutex::Autolock _l(mNotificationClientsLock);
luochaojiang908c7d72018-06-21 14:58:04 +0800171 int64_t token = ((int64_t)uid<<32) | pid;
172 mNotificationClients.removeItem(token);
Eric Laurent0ebd5f92014-11-19 19:04:52 -0800173 }
Eric Laurent0ebd5f92014-11-19 19:04:52 -0800174 {
175 Mutex::Autolock _l(mLock);
luochaojiang908c7d72018-06-21 14:58:04 +0800176 bool hasSameUid = false;
177 for (size_t i = 0; i < mNotificationClients.size(); i++) {
178 if (mNotificationClients.valueAt(i)->uid() == uid) {
179 hasSameUid = true;
180 break;
181 }
182 }
183 if (mAudioPolicyManager && !hasSameUid) {
Eric Laurent10b71232018-04-13 18:14:44 -0700184 // called from binder death notification: no need to clear caller identity
Eric Laurent8c7e6da2015-04-21 17:37:00 -0700185 mAudioPolicyManager->releaseResourcesForUid(uid);
Eric Laurentb52c1522014-05-20 11:27:36 -0700186 }
Eric Laurent0ebd5f92014-11-19 19:04:52 -0800187 }
Eric Laurentb52c1522014-05-20 11:27:36 -0700188}
189
190void AudioPolicyService::onAudioPortListUpdate()
191{
192 mOutputCommandThread->updateAudioPortListCommand();
193}
194
195void AudioPolicyService::doOnAudioPortListUpdate()
196{
Eric Laurent0ebd5f92014-11-19 19:04:52 -0800197 Mutex::Autolock _l(mNotificationClientsLock);
Eric Laurentb52c1522014-05-20 11:27:36 -0700198 for (size_t i = 0; i < mNotificationClients.size(); i++) {
199 mNotificationClients.valueAt(i)->onAudioPortListUpdate();
200 }
201}
202
203void AudioPolicyService::onAudioPatchListUpdate()
204{
205 mOutputCommandThread->updateAudioPatchListCommand();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700206}
207
Eric Laurentb52c1522014-05-20 11:27:36 -0700208void AudioPolicyService::doOnAudioPatchListUpdate()
209{
Eric Laurent0ebd5f92014-11-19 19:04:52 -0800210 Mutex::Autolock _l(mNotificationClientsLock);
Eric Laurentb52c1522014-05-20 11:27:36 -0700211 for (size_t i = 0; i < mNotificationClients.size(); i++) {
212 mNotificationClients.valueAt(i)->onAudioPatchListUpdate();
213 }
214}
215
François Gaffiecfe17322018-11-07 13:41:29 +0100216void AudioPolicyService::onAudioVolumeGroupChanged(volume_group_t group, int flags)
217{
218 mOutputCommandThread->changeAudioVolumeGroupCommand(group, flags);
219}
220
221void AudioPolicyService::doOnAudioVolumeGroupChanged(volume_group_t group, int flags)
222{
223 Mutex::Autolock _l(mNotificationClientsLock);
224 for (size_t i = 0; i < mNotificationClients.size(); i++) {
225 mNotificationClients.valueAt(i)->onAudioVolumeGroupChanged(group, flags);
226 }
227}
228
Chih-Hung Hsiehe964d4e2016-08-09 14:31:32 -0700229void AudioPolicyService::onDynamicPolicyMixStateUpdate(const String8& regId, int32_t state)
Jean-Michel Trivide801052015-04-14 19:10:14 -0700230{
231 ALOGV("AudioPolicyService::onDynamicPolicyMixStateUpdate(%s, %d)",
232 regId.string(), state);
233 mOutputCommandThread->dynamicPolicyMixStateUpdateCommand(regId, state);
234}
235
Chih-Hung Hsiehe964d4e2016-08-09 14:31:32 -0700236void AudioPolicyService::doOnDynamicPolicyMixStateUpdate(const String8& regId, int32_t state)
Jean-Michel Trivide801052015-04-14 19:10:14 -0700237{
238 Mutex::Autolock _l(mNotificationClientsLock);
239 for (size_t i = 0; i < mNotificationClients.size(); i++) {
240 mNotificationClients.valueAt(i)->onDynamicPolicyMixStateUpdate(regId, state);
241 }
242}
243
Eric Laurenta9f86652018-11-28 17:23:11 -0800244void AudioPolicyService::onRecordingConfigurationUpdate(
245 int event,
246 const record_client_info_t *clientInfo,
247 const audio_config_base_t *clientConfig,
248 std::vector<effect_descriptor_t> clientEffects,
249 const audio_config_base_t *deviceConfig,
250 std::vector<effect_descriptor_t> effects,
251 audio_patch_handle_t patchHandle,
252 audio_source_t source)
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -0800253{
Jean-Michel Triviac4e4292016-12-22 11:39:31 -0800254 mOutputCommandThread->recordingConfigurationUpdateCommand(event, clientInfo,
Eric Laurenta9f86652018-11-28 17:23:11 -0800255 clientConfig, clientEffects, deviceConfig, effects, patchHandle, source);
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -0800256}
257
Eric Laurenta9f86652018-11-28 17:23:11 -0800258void AudioPolicyService::doOnRecordingConfigurationUpdate(
259 int event,
260 const record_client_info_t *clientInfo,
261 const audio_config_base_t *clientConfig,
262 std::vector<effect_descriptor_t> clientEffects,
263 const audio_config_base_t *deviceConfig,
264 std::vector<effect_descriptor_t> effects,
265 audio_patch_handle_t patchHandle,
266 audio_source_t source)
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -0800267{
268 Mutex::Autolock _l(mNotificationClientsLock);
269 for (size_t i = 0; i < mNotificationClients.size(); i++) {
Jean-Michel Triviac4e4292016-12-22 11:39:31 -0800270 mNotificationClients.valueAt(i)->onRecordingConfigurationUpdate(event, clientInfo,
Eric Laurenta9f86652018-11-28 17:23:11 -0800271 clientConfig, clientEffects, deviceConfig, effects, patchHandle, source);
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -0800272 }
273}
274
275status_t AudioPolicyService::clientCreateAudioPatch(const struct audio_patch *patch,
276 audio_patch_handle_t *handle,
277 int delayMs)
278{
279 return mAudioCommandThread->createAudioPatchCommand(patch, handle, delayMs);
280}
281
282status_t AudioPolicyService::clientReleaseAudioPatch(audio_patch_handle_t handle,
283 int delayMs)
284{
285 return mAudioCommandThread->releaseAudioPatchCommand(handle, delayMs);
286}
287
Eric Laurente1715a42014-05-20 11:30:42 -0700288status_t AudioPolicyService::clientSetAudioPortConfig(const struct audio_port_config *config,
289 int delayMs)
290{
291 return mAudioCommandThread->setAudioPortConfigCommand(config, delayMs);
292}
293
Eric Laurentb52c1522014-05-20 11:27:36 -0700294AudioPolicyService::NotificationClient::NotificationClient(const sp<AudioPolicyService>& service,
295 const sp<IAudioPolicyServiceClient>& client,
luochaojiang908c7d72018-06-21 14:58:04 +0800296 uid_t uid,
297 pid_t pid)
298 : mService(service), mUid(uid), mPid(pid), mAudioPolicyServiceClient(client),
François Gaffiecfe17322018-11-07 13:41:29 +0100299 mAudioPortCallbacksEnabled(false), mAudioVolumeGroupCallbacksEnabled(false)
Eric Laurentb52c1522014-05-20 11:27:36 -0700300{
301}
302
303AudioPolicyService::NotificationClient::~NotificationClient()
304{
305}
306
307void AudioPolicyService::NotificationClient::binderDied(const wp<IBinder>& who __unused)
308{
309 sp<NotificationClient> keep(this);
310 sp<AudioPolicyService> service = mService.promote();
311 if (service != 0) {
luochaojiang908c7d72018-06-21 14:58:04 +0800312 service->removeNotificationClient(mUid, mPid);
Eric Laurentb52c1522014-05-20 11:27:36 -0700313 }
314}
315
316void AudioPolicyService::NotificationClient::onAudioPortListUpdate()
317{
Eric Laurente8726fe2015-06-26 09:39:24 -0700318 if (mAudioPolicyServiceClient != 0 && mAudioPortCallbacksEnabled) {
Eric Laurentb52c1522014-05-20 11:27:36 -0700319 mAudioPolicyServiceClient->onAudioPortListUpdate();
320 }
321}
322
323void AudioPolicyService::NotificationClient::onAudioPatchListUpdate()
324{
Eric Laurente8726fe2015-06-26 09:39:24 -0700325 if (mAudioPolicyServiceClient != 0 && mAudioPortCallbacksEnabled) {
Eric Laurentb52c1522014-05-20 11:27:36 -0700326 mAudioPolicyServiceClient->onAudioPatchListUpdate();
327 }
328}
Eric Laurent57dae992011-07-24 13:36:09 -0700329
François Gaffiecfe17322018-11-07 13:41:29 +0100330void AudioPolicyService::NotificationClient::onAudioVolumeGroupChanged(volume_group_t group,
331 int flags)
332{
333 if (mAudioPolicyServiceClient != 0 && mAudioVolumeGroupCallbacksEnabled) {
334 mAudioPolicyServiceClient->onAudioVolumeGroupChanged(group, flags);
335 }
336}
337
338
Jean-Michel Trivide801052015-04-14 19:10:14 -0700339void AudioPolicyService::NotificationClient::onDynamicPolicyMixStateUpdate(
Chih-Hung Hsiehe964d4e2016-08-09 14:31:32 -0700340 const String8& regId, int32_t state)
Jean-Michel Trivide801052015-04-14 19:10:14 -0700341{
Andy Hung4ef19fa2018-05-15 19:35:29 -0700342 if (mAudioPolicyServiceClient != 0 && isServiceUid(mUid)) {
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -0800343 mAudioPolicyServiceClient->onDynamicPolicyMixStateUpdate(regId, state);
344 }
345}
346
347void AudioPolicyService::NotificationClient::onRecordingConfigurationUpdate(
Eric Laurenta9f86652018-11-28 17:23:11 -0800348 int event,
349 const record_client_info_t *clientInfo,
350 const audio_config_base_t *clientConfig,
351 std::vector<effect_descriptor_t> clientEffects,
352 const audio_config_base_t *deviceConfig,
353 std::vector<effect_descriptor_t> effects,
354 audio_patch_handle_t patchHandle,
355 audio_source_t source)
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -0800356{
Andy Hung4ef19fa2018-05-15 19:35:29 -0700357 if (mAudioPolicyServiceClient != 0 && isServiceUid(mUid)) {
Jean-Michel Triviac4e4292016-12-22 11:39:31 -0800358 mAudioPolicyServiceClient->onRecordingConfigurationUpdate(event, clientInfo,
Eric Laurenta9f86652018-11-28 17:23:11 -0800359 clientConfig, clientEffects, deviceConfig, effects, patchHandle, source);
Jean-Michel Trivide801052015-04-14 19:10:14 -0700360 }
361}
362
Eric Laurente8726fe2015-06-26 09:39:24 -0700363void AudioPolicyService::NotificationClient::setAudioPortCallbacksEnabled(bool enabled)
364{
365 mAudioPortCallbacksEnabled = enabled;
366}
367
François Gaffiecfe17322018-11-07 13:41:29 +0100368void AudioPolicyService::NotificationClient::setAudioVolumeGroupCallbacksEnabled(bool enabled)
369{
370 mAudioVolumeGroupCallbacksEnabled = enabled;
371}
Eric Laurente8726fe2015-06-26 09:39:24 -0700372
Mathias Agopian65ab4712010-07-14 17:59:35 -0700373void AudioPolicyService::binderDied(const wp<IBinder>& who) {
Glenn Kasten411e4472012-11-02 10:00:06 -0700374 ALOGW("binderDied() %p, calling pid %d", who.unsafe_get(),
Eric Laurentde070132010-07-13 04:45:46 -0700375 IPCThreadState::self()->getCallingPid());
Mathias Agopian65ab4712010-07-14 17:59:35 -0700376}
377
Mikhail Naganov959e2d02019-03-28 11:08:19 -0700378static bool dumpTryLock(Mutex& mutex)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700379{
Mikhail Naganov959e2d02019-03-28 11:08:19 -0700380 status_t err = mutex.timedLock(kDumpLockTimeoutNs);
381 return err == NO_ERROR;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700382}
383
384status_t AudioPolicyService::dumpInternals(int fd)
385{
386 const size_t SIZE = 256;
387 char buffer[SIZE];
388 String8 result;
389
Eric Laurentdce54a12014-03-10 12:19:46 -0700390 snprintf(buffer, SIZE, "AudioPolicyManager: %p\n", mAudioPolicyManager);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700391 result.append(buffer);
392 snprintf(buffer, SIZE, "Command Thread: %p\n", mAudioCommandThread.get());
393 result.append(buffer);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700394
395 write(fd, result.string(), result.size());
396 return NO_ERROR;
397}
398
Eric Laurente8c8b432018-10-17 10:08:02 -0700399void AudioPolicyService::updateUidStates()
Svet Ganovf4ddfef2018-01-16 07:37:58 -0800400{
Eric Laurente8c8b432018-10-17 10:08:02 -0700401 Mutex::Autolock _l(mLock);
402 updateUidStates_l();
403}
404
405void AudioPolicyService::updateUidStates_l()
406{
Eric Laurent4eb58f12018-12-07 16:41:02 -0800407// Go over all active clients and allow capture (does not force silence) in the
408// following cases:
Eric Laurent8ed73c12019-08-30 17:25:19 -0700409// The client is the assistant
Eric Laurent6ede98f2019-06-11 14:50:30 -0700410// AND an accessibility service is on TOP or a RTT call is active
Eric Laurent589171c2019-07-25 18:04:29 -0700411// AND the source is VOICE_RECOGNITION or HOTWORD
412// OR uses VOICE_RECOGNITION AND is on TOP
413// OR uses HOTWORD
Eric Laurent1ff16a72019-03-14 18:35:04 -0700414// AND there is no active privacy sensitive capture or call
415// OR client has CAPTURE_AUDIO_OUTPUT privileged permission
Eric Laurenta46bedb2018-12-07 18:01:26 -0800416// OR The client is an accessibility service
Eric Laurent589171c2019-07-25 18:04:29 -0700417// AND Is on TOP
418// AND the source is VOICE_RECOGNITION or HOTWORD
419// OR The assistant is not on TOP
420// AND there is no active privacy sensitive capture or call
421// OR client has CAPTURE_AUDIO_OUTPUT privileged permission
Eric Laurenta171e352019-05-07 13:04:45 -0700422// AND is on TOP
Eric Laurenta46bedb2018-12-07 18:01:26 -0800423// AND the source is VOICE_RECOGNITION or HOTWORD
Eric Laurent1ff16a72019-03-14 18:35:04 -0700424// OR the client source is virtual (remote submix, call audio TX or RX...)
Eric Laurent4e947da2019-10-17 15:24:06 -0700425// OR the client source is HOTWORD
426// AND is on TOP
427// OR all active clients are using HOTWORD source
428// AND no call is active
429// OR client has CAPTURE_AUDIO_OUTPUT privileged permission
Eric Laurenta171e352019-05-07 13:04:45 -0700430// OR Any client
Eric Laurenta46bedb2018-12-07 18:01:26 -0800431// AND The assistant is not on TOP
Eric Laurenta171e352019-05-07 13:04:45 -0700432// AND is on TOP or latest started
Eric Laurent1ff16a72019-03-14 18:35:04 -0700433// AND there is no active privacy sensitive capture or call
Eric Laurent589171c2019-07-25 18:04:29 -0700434// OR client has CAPTURE_AUDIO_OUTPUT privileged permission
Eric Laurent4eb58f12018-12-07 16:41:02 -0800435
Eric Laurent4e947da2019-10-17 15:24:06 -0700436
Eric Laurent4eb58f12018-12-07 16:41:02 -0800437 sp<AudioRecordClient> topActive;
438 sp<AudioRecordClient> latestActive;
Eric Laurent4eb58f12018-12-07 16:41:02 -0800439 sp<AudioRecordClient> latestSensitiveActive;
Eric Laurent1ff16a72019-03-14 18:35:04 -0700440
Eric Laurenta46bedb2018-12-07 18:01:26 -0800441 nsecs_t topStartNs = 0;
442 nsecs_t latestStartNs = 0;
Eric Laurent4eb58f12018-12-07 16:41:02 -0800443 nsecs_t latestSensitiveStartNs = 0;
444 bool isA11yOnTop = mUidPolicy->isA11yOnTop();
445 bool isAssistantOnTop = false;
446 bool isSensitiveActive = false;
Eric Laurent1ff16a72019-03-14 18:35:04 -0700447 bool isInCall = mPhoneState == AUDIO_MODE_IN_CALL;
Eric Laurent6ede98f2019-06-11 14:50:30 -0700448 bool rttCallActive =
449 (mPhoneState == AUDIO_MODE_IN_CALL || mPhoneState == AUDIO_MODE_IN_COMMUNICATION)
450 && mUidPolicy->isRttEnabled();
Eric Laurent4e947da2019-10-17 15:24:06 -0700451 bool onlyHotwordActive = true;
Eric Laurent4eb58f12018-12-07 16:41:02 -0800452
Michael Groovercfd28302018-12-11 19:16:46 -0800453 // if Sensor Privacy is enabled then all recordings should be silenced.
454 if (mSensorPrivacyPolicy->isSensorPrivacyEnabled()) {
455 silenceAllRecordings_l();
456 return;
457 }
458
Eric Laurente8c8b432018-10-17 10:08:02 -0700459 for (size_t i =0; i < mAudioRecordClients.size(); i++) {
460 sp<AudioRecordClient> current = mAudioRecordClients[i];
Eric Laurent1ff16a72019-03-14 18:35:04 -0700461 if (!current->active) {
462 continue;
Eric Laurent4eb58f12018-12-07 16:41:02 -0800463 }
Eric Laurent1ff16a72019-03-14 18:35:04 -0700464
465 app_state_t appState = apmStatFromAmState(mUidPolicy->getUidState(current->uid));
466 // clients which app is in IDLE state are not eligible for top active or
467 // latest active
468 if (appState == APP_STATE_IDLE) {
469 continue;
470 }
471
Eric Laurentc6af90d2019-07-19 10:31:39 -0700472 bool isAssistant = mUidPolicy->isAssistantUid(current->uid);
Eric Laurent589171c2019-07-25 18:04:29 -0700473 bool isAccessibility = mUidPolicy->isA11yUid(current->uid);
474 if (appState == APP_STATE_TOP && !isAccessibility) {
Eric Laurenta46bedb2018-12-07 18:01:26 -0800475 if (current->startTimeNs > topStartNs) {
476 topActive = current;
477 topStartNs = current->startTimeNs;
478 }
Eric Laurentc6af90d2019-07-19 10:31:39 -0700479 if (isAssistant) {
Eric Laurent4eb58f12018-12-07 16:41:02 -0800480 isAssistantOnTop = true;
481 }
482 }
Eric Laurent4e947da2019-10-17 15:24:06 -0700483 // Client capturing for HOTWORD or Accessibility services not considered
Eric Laurent589171c2019-07-25 18:04:29 -0700484 // for latest active to avoid masking regular clients started before
485 if (current->startTimeNs > latestStartNs
Eric Laurent4e947da2019-10-17 15:24:06 -0700486 && !(current->attributes.source == AUDIO_SOURCE_HOTWORD
487 || ((isA11yOnTop || rttCallActive) && isAssistant))
Eric Laurent589171c2019-07-25 18:04:29 -0700488 && !isAccessibility) {
Eric Laurent4eb58f12018-12-07 16:41:02 -0800489 latestActive = current;
490 latestStartNs = current->startTimeNs;
491 }
Eric Laurentd17c8502019-10-24 15:58:35 -0700492 if ((current->attributes.flags & AUDIO_FLAG_CAPTURE_PRIVATE) != 0) {
Eric Laurent1ff16a72019-03-14 18:35:04 -0700493 if (current->startTimeNs > latestSensitiveStartNs) {
494 latestSensitiveActive = current;
495 latestSensitiveStartNs = current->startTimeNs;
496 }
497 isSensitiveActive = true;
498 }
Eric Laurent4e947da2019-10-17 15:24:06 -0700499 if (current->attributes.source != AUDIO_SOURCE_HOTWORD) {
500 onlyHotwordActive = false;
501 }
Eric Laurent4eb58f12018-12-07 16:41:02 -0800502 }
503
Eric Laurent1ff16a72019-03-14 18:35:04 -0700504 // if no active client with UI on Top, consider latest active as top
505 if (topActive == nullptr) {
506 topActive = latestActive;
Eric Laurent4eb58f12018-12-07 16:41:02 -0800507 }
508
509 for (size_t i =0; i < mAudioRecordClients.size(); i++) {
510 sp<AudioRecordClient> current = mAudioRecordClients[i];
Eric Laurent1ff16a72019-03-14 18:35:04 -0700511 if (!current->active) {
512 continue;
513 }
514
Eric Laurent4eb58f12018-12-07 16:41:02 -0800515 audio_source_t source = current->attributes.source;
Eric Laurent1ff16a72019-03-14 18:35:04 -0700516 bool isTopOrLatestActive = topActive == nullptr ? false : current->uid == topActive->uid;
517 bool isLatestSensitive = latestSensitiveActive == nullptr ?
518 false : current->uid == latestSensitiveActive->uid;
519
520 // By default allow capture if:
521 // The assistant is not on TOP
Eric Laurenta171e352019-05-07 13:04:45 -0700522 // AND is on TOP or latest started
Eric Laurent1ff16a72019-03-14 18:35:04 -0700523 // AND there is no active privacy sensitive capture or call
524 // OR client has CAPTURE_AUDIO_OUTPUT privileged permission
525 bool allowCapture = !isAssistantOnTop
Eric Laurenta171e352019-05-07 13:04:45 -0700526 && ((isTopOrLatestActive && !isLatestSensitive) || isLatestSensitive)
Eric Laurent1ff16a72019-03-14 18:35:04 -0700527 && !(isSensitiveActive && !(isLatestSensitive || current->canCaptureOutput))
528 && !(isInCall && !current->canCaptureOutput);
Eric Laurent2dc962b2019-03-01 08:25:25 -0800529
530 if (isVirtualSource(source)) {
Eric Laurent1ff16a72019-03-14 18:35:04 -0700531 // Allow capture for virtual (remote submix, call audio TX or RX...) sources
532 allowCapture = true;
Eric Laurent2dc962b2019-03-01 08:25:25 -0800533 } else if (mUidPolicy->isAssistantUid(current->uid)) {
Eric Laurent1ff16a72019-03-14 18:35:04 -0700534 // For assistant allow capture if:
Eric Laurent6ede98f2019-06-11 14:50:30 -0700535 // An accessibility service is on TOP or a RTT call is active
Eric Laurent1ff16a72019-03-14 18:35:04 -0700536 // AND the source is VOICE_RECOGNITION or HOTWORD
Eric Laurenta171e352019-05-07 13:04:45 -0700537 // OR is on TOP AND uses VOICE_RECOGNITION
Eric Laurent1ff16a72019-03-14 18:35:04 -0700538 // OR uses HOTWORD
539 // AND there is no active privacy sensitive capture or call
540 // OR client has CAPTURE_AUDIO_OUTPUT privileged permission
Eric Laurent6ede98f2019-06-11 14:50:30 -0700541 if (isA11yOnTop || rttCallActive) {
Eric Laurent4eb58f12018-12-07 16:41:02 -0800542 if (source == AUDIO_SOURCE_HOTWORD || source == AUDIO_SOURCE_VOICE_RECOGNITION) {
Eric Laurent1ff16a72019-03-14 18:35:04 -0700543 allowCapture = true;
Eric Laurent4eb58f12018-12-07 16:41:02 -0800544 }
545 } else {
Eric Laurenta171e352019-05-07 13:04:45 -0700546 if (((isAssistantOnTop && source == AUDIO_SOURCE_VOICE_RECOGNITION) ||
Eric Laurent1ff16a72019-03-14 18:35:04 -0700547 source == AUDIO_SOURCE_HOTWORD) &&
548 (!(isSensitiveActive || isInCall) || current->canCaptureOutput)) {
549 allowCapture = true;
Eric Laurent4eb58f12018-12-07 16:41:02 -0800550 }
551 }
552 } else if (mUidPolicy->isA11yUid(current->uid)) {
Eric Laurent1ff16a72019-03-14 18:35:04 -0700553 // For accessibility service allow capture if:
Eric Laurent47670c92019-08-28 16:59:05 -0700554 // The assistant is not on TOP
555 // AND there is no active privacy sensitive capture or call
Eric Laurent589171c2019-07-25 18:04:29 -0700556 // OR client has CAPTURE_AUDIO_OUTPUT privileged permission
Eric Laurent47670c92019-08-28 16:59:05 -0700557 // OR
558 // Is on TOP AND the source is VOICE_RECOGNITION or HOTWORD
559 if (!isAssistantOnTop
560 && (!(isSensitiveActive || isInCall) || current->canCaptureOutput)) {
561 allowCapture = true;
562 }
Eric Laurent589171c2019-07-25 18:04:29 -0700563 if (isA11yOnTop) {
564 if (source == AUDIO_SOURCE_VOICE_RECOGNITION || source == AUDIO_SOURCE_HOTWORD) {
565 allowCapture = true;
566 }
Eric Laurent4eb58f12018-12-07 16:41:02 -0800567 }
Eric Laurent4e947da2019-10-17 15:24:06 -0700568 } else if (source == AUDIO_SOURCE_HOTWORD) {
569 // For HOTWORD source allow capture when not on TOP if:
570 // All active clients are using HOTWORD source
571 // AND no call is active
572 // OR client has CAPTURE_AUDIO_OUTPUT privileged permission
573 if (onlyHotwordActive && !(isInCall && !current->canCaptureOutput)) {
574 allowCapture = true;
575 }
Eric Laurent4eb58f12018-12-07 16:41:02 -0800576 }
Eric Laurent5ada82e2019-08-29 17:53:54 -0700577 setAppState_l(current->portId,
Eric Laurent1ff16a72019-03-14 18:35:04 -0700578 allowCapture ? apmStatFromAmState(mUidPolicy->getUidState(current->uid)) :
579 APP_STATE_IDLE);
Eric Laurente8c8b432018-10-17 10:08:02 -0700580 }
581}
582
Michael Groovercfd28302018-12-11 19:16:46 -0800583void AudioPolicyService::silenceAllRecordings_l() {
584 for (size_t i = 0; i < mAudioRecordClients.size(); i++) {
585 sp<AudioRecordClient> current = mAudioRecordClients[i];
Eric Laurent1ff16a72019-03-14 18:35:04 -0700586 if (!isVirtualSource(current->attributes.source)) {
Eric Laurent5ada82e2019-08-29 17:53:54 -0700587 setAppState_l(current->portId, APP_STATE_IDLE);
Eric Laurent1ff16a72019-03-14 18:35:04 -0700588 }
Michael Groovercfd28302018-12-11 19:16:46 -0800589 }
590}
591
Eric Laurente8c8b432018-10-17 10:08:02 -0700592/* static */
593app_state_t AudioPolicyService::apmStatFromAmState(int amState) {
Eric Laurent1ff16a72019-03-14 18:35:04 -0700594
595 if (amState == ActivityManager::PROCESS_STATE_UNKNOWN) {
Eric Laurente8c8b432018-10-17 10:08:02 -0700596 return APP_STATE_IDLE;
Eric Laurent1ff16a72019-03-14 18:35:04 -0700597 } else if (amState <= ActivityManager::PROCESS_STATE_TOP) {
598 // include persistent services
599 return APP_STATE_TOP;
Eric Laurente8c8b432018-10-17 10:08:02 -0700600 }
601 return APP_STATE_FOREGROUND;
602}
603
Eric Laurent4eb58f12018-12-07 16:41:02 -0800604/* static */
Eric Laurent2dc962b2019-03-01 08:25:25 -0800605bool AudioPolicyService::isVirtualSource(audio_source_t source)
Eric Laurent4eb58f12018-12-07 16:41:02 -0800606{
607 switch (source) {
608 case AUDIO_SOURCE_VOICE_UPLINK:
609 case AUDIO_SOURCE_VOICE_DOWNLINK:
610 case AUDIO_SOURCE_VOICE_CALL:
Eric Laurent2dc962b2019-03-01 08:25:25 -0800611 case AUDIO_SOURCE_REMOTE_SUBMIX:
612 case AUDIO_SOURCE_FM_TUNER:
Eric Laurent4eb58f12018-12-07 16:41:02 -0800613 return true;
614 default:
615 break;
616 }
617 return false;
618}
619
Eric Laurent5ada82e2019-08-29 17:53:54 -0700620void AudioPolicyService::setAppState_l(audio_port_handle_t portId, app_state_t state)
Eric Laurente8c8b432018-10-17 10:08:02 -0700621{
622 AutoCallerClear acc;
623
624 if (mAudioPolicyManager) {
Eric Laurent5ada82e2019-08-29 17:53:54 -0700625 mAudioPolicyManager->setAppState(portId, state);
Mikhail Naganoveae73eb2018-04-03 16:57:36 -0700626 }
627 sp<IAudioFlinger> af = AudioSystem::get_audio_flinger();
628 if (af) {
Eric Laurentf32108e2018-10-04 17:22:04 -0700629 bool silenced = state == APP_STATE_IDLE;
Eric Laurent5ada82e2019-08-29 17:53:54 -0700630 af->setRecordSilenced(portId, silenced);
Mikhail Naganoveae73eb2018-04-03 16:57:36 -0700631 }
Svet Ganovf4ddfef2018-01-16 07:37:58 -0800632}
633
Glenn Kasten0f11b512014-01-31 16:18:54 -0800634status_t AudioPolicyService::dump(int fd, const Vector<String16>& args __unused)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700635{
Glenn Kasten44deb052012-02-05 18:09:08 -0800636 if (!dumpAllowed()) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700637 dumpPermissionDenial(fd);
638 } else {
Mikhail Naganov959e2d02019-03-28 11:08:19 -0700639 bool locked = dumpTryLock(mLock);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700640 if (!locked) {
641 String8 result(kDeadlockedString);
642 write(fd, result.string(), result.size());
643 }
644
645 dumpInternals(fd);
Glenn Kasten9d1f02d2012-02-08 17:47:58 -0800646 if (mAudioCommandThread != 0) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700647 mAudioCommandThread->dump(fd);
648 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700649
Eric Laurentdce54a12014-03-10 12:19:46 -0700650 if (mAudioPolicyManager) {
651 mAudioPolicyManager->dump(fd);
652 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700653
Kevin Rocard8be94972019-02-22 13:26:25 -0800654 mPackageManager.dump(fd);
655
Mathias Agopian65ab4712010-07-14 17:59:35 -0700656 if (locked) mLock.unlock();
657 }
658 return NO_ERROR;
659}
660
661status_t AudioPolicyService::dumpPermissionDenial(int fd)
662{
663 const size_t SIZE = 256;
664 char buffer[SIZE];
665 String8 result;
666 snprintf(buffer, SIZE, "Permission Denial: "
667 "can't dump AudioPolicyService from pid=%d, uid=%d\n",
668 IPCThreadState::self()->getCallingPid(),
669 IPCThreadState::self()->getCallingUid());
670 result.append(buffer);
671 write(fd, result.string(), result.size());
672 return NO_ERROR;
673}
674
675status_t AudioPolicyService::onTransact(
Svet Ganovf4ddfef2018-01-16 07:37:58 -0800676 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags) {
677 switch (code) {
678 case SHELL_COMMAND_TRANSACTION: {
679 int in = data.readFileDescriptor();
680 int out = data.readFileDescriptor();
681 int err = data.readFileDescriptor();
682 int argc = data.readInt32();
683 Vector<String16> args;
684 for (int i = 0; i < argc && data.dataAvail() > 0; i++) {
685 args.add(data.readString16());
686 }
687 sp<IBinder> unusedCallback;
688 sp<IResultReceiver> resultReceiver;
689 status_t status;
690 if ((status = data.readNullableStrongBinder(&unusedCallback)) != NO_ERROR) {
691 return status;
692 }
693 if ((status = data.readNullableStrongBinder(&resultReceiver)) != NO_ERROR) {
694 return status;
695 }
696 status = shellCommand(in, out, err, args);
697 if (resultReceiver != nullptr) {
698 resultReceiver->send(status);
699 }
700 return NO_ERROR;
701 }
702 }
703
Mathias Agopian65ab4712010-07-14 17:59:35 -0700704 return BnAudioPolicyService::onTransact(code, data, reply, flags);
705}
706
Svet Ganovf4ddfef2018-01-16 07:37:58 -0800707// ------------------- Shell command implementation -------------------
708
709// NOTE: This is a remote API - make sure all args are validated
710status_t AudioPolicyService::shellCommand(int in, int out, int err, Vector<String16>& args) {
711 if (!checkCallingPermission(sManageAudioPolicyPermission, nullptr, nullptr)) {
712 return PERMISSION_DENIED;
713 }
714 if (in == BAD_TYPE || out == BAD_TYPE || err == BAD_TYPE) {
715 return BAD_VALUE;
716 }
jovanakbe066e12019-09-02 11:54:39 -0700717 if (args.size() >= 3 && args[0] == String16("set-uid-state")) {
Svet Ganovf4ddfef2018-01-16 07:37:58 -0800718 return handleSetUidState(args, err);
jovanakbe066e12019-09-02 11:54:39 -0700719 } else if (args.size() >= 2 && args[0] == String16("reset-uid-state")) {
Svet Ganovf4ddfef2018-01-16 07:37:58 -0800720 return handleResetUidState(args, err);
jovanakbe066e12019-09-02 11:54:39 -0700721 } else if (args.size() >= 2 && args[0] == String16("get-uid-state")) {
Svet Ganovf4ddfef2018-01-16 07:37:58 -0800722 return handleGetUidState(args, out, err);
723 } else if (args.size() == 1 && args[0] == String16("help")) {
724 printHelp(out);
725 return NO_ERROR;
726 }
727 printHelp(err);
728 return BAD_VALUE;
729}
730
jovanakbe066e12019-09-02 11:54:39 -0700731static status_t getUidForPackage(String16 packageName, int userId, /*inout*/uid_t& uid, int err) {
732 if (userId < 0) {
733 ALOGE("Invalid user: %d", userId);
734 dprintf(err, "Invalid user: %d\n", userId);
Svet Ganovf4ddfef2018-01-16 07:37:58 -0800735 return BAD_VALUE;
736 }
jovanakbe066e12019-09-02 11:54:39 -0700737
738 PermissionController pc;
739 uid = pc.getPackageUid(packageName, 0);
740 if (uid <= 0) {
741 ALOGE("Unknown package: '%s'", String8(packageName).string());
742 dprintf(err, "Unknown package: '%s'\n", String8(packageName).string());
743 return BAD_VALUE;
744 }
745
746 uid = multiuser_get_uid(userId, uid);
747 return NO_ERROR;
748}
749
750status_t AudioPolicyService::handleSetUidState(Vector<String16>& args, int err) {
751 // Valid arg.size() is 3 or 5, args.size() is 5 with --user option.
752 if (!(args.size() == 3 || args.size() == 5)) {
753 printHelp(err);
754 return BAD_VALUE;
755 }
756
Svet Ganovf4ddfef2018-01-16 07:37:58 -0800757 bool active = false;
758 if (args[2] == String16("active")) {
759 active = true;
760 } else if ((args[2] != String16("idle"))) {
761 ALOGE("Expected active or idle but got: '%s'", String8(args[2]).string());
762 return BAD_VALUE;
763 }
jovanakbe066e12019-09-02 11:54:39 -0700764
765 int userId = 0;
766 if (args.size() >= 5 && args[3] == String16("--user")) {
767 userId = atoi(String8(args[4]));
768 }
769
770 uid_t uid;
771 if (getUidForPackage(args[1], userId, uid, err) == BAD_VALUE) {
772 return BAD_VALUE;
773 }
774
Svet Ganovf4ddfef2018-01-16 07:37:58 -0800775 mUidPolicy->addOverrideUid(uid, active);
776 return NO_ERROR;
777}
778
779status_t AudioPolicyService::handleResetUidState(Vector<String16>& args, int err) {
jovanakbe066e12019-09-02 11:54:39 -0700780 // Valid arg.size() is 2 or 4, args.size() is 4 with --user option.
781 if (!(args.size() == 2 || args.size() == 4)) {
782 printHelp(err);
Svet Ganovf4ddfef2018-01-16 07:37:58 -0800783 return BAD_VALUE;
784 }
jovanakbe066e12019-09-02 11:54:39 -0700785
786 int userId = 0;
787 if (args.size() >= 4 && args[2] == String16("--user")) {
788 userId = atoi(String8(args[3]));
789 }
790
791 uid_t uid;
792 if (getUidForPackage(args[1], userId, uid, err) == BAD_VALUE) {
793 return BAD_VALUE;
794 }
795
Svet Ganovf4ddfef2018-01-16 07:37:58 -0800796 mUidPolicy->removeOverrideUid(uid);
797 return NO_ERROR;
798}
799
800status_t AudioPolicyService::handleGetUidState(Vector<String16>& args, int out, int err) {
jovanakbe066e12019-09-02 11:54:39 -0700801 // Valid arg.size() is 2 or 4, args.size() is 4 with --user option.
802 if (!(args.size() == 2 || args.size() == 4)) {
803 printHelp(err);
Svet Ganovf4ddfef2018-01-16 07:37:58 -0800804 return BAD_VALUE;
805 }
jovanakbe066e12019-09-02 11:54:39 -0700806
807 int userId = 0;
808 if (args.size() >= 4 && args[2] == String16("--user")) {
809 userId = atoi(String8(args[3]));
810 }
811
812 uid_t uid;
813 if (getUidForPackage(args[1], userId, uid, err) == BAD_VALUE) {
814 return BAD_VALUE;
815 }
816
Svet Ganovf4ddfef2018-01-16 07:37:58 -0800817 if (mUidPolicy->isUidActive(uid)) {
818 return dprintf(out, "active\n");
819 } else {
820 return dprintf(out, "idle\n");
821 }
822}
823
824status_t AudioPolicyService::printHelp(int out) {
825 return dprintf(out, "Audio policy service commands:\n"
jovanakbe066e12019-09-02 11:54:39 -0700826 " get-uid-state <PACKAGE> [--user USER_ID] gets the uid state\n"
827 " set-uid-state <PACKAGE> <active|idle> [--user USER_ID] overrides the uid state\n"
828 " reset-uid-state <PACKAGE> [--user USER_ID] clears the uid state override\n"
Svet Ganovf4ddfef2018-01-16 07:37:58 -0800829 " help print this message\n");
830}
831
832// ----------- AudioPolicyService::UidPolicy implementation ----------
833
834void AudioPolicyService::UidPolicy::registerSelf() {
Steven Moreland2f348142019-07-02 15:59:07 -0700835 status_t res = mAm.linkToDeath(this);
836 mAm.registerUidObserver(this, ActivityManager::UID_OBSERVER_GONE
Svet Ganovf4ddfef2018-01-16 07:37:58 -0800837 | ActivityManager::UID_OBSERVER_IDLE
Eric Laurente8c8b432018-10-17 10:08:02 -0700838 | ActivityManager::UID_OBSERVER_ACTIVE
839 | ActivityManager::UID_OBSERVER_PROCSTATE,
Svet Ganovf4ddfef2018-01-16 07:37:58 -0800840 ActivityManager::PROCESS_STATE_UNKNOWN,
841 String16("audioserver"));
Mikhail Naganoveae73eb2018-04-03 16:57:36 -0700842 if (!res) {
843 Mutex::Autolock _l(mLock);
844 mObserverRegistered = true;
845 } else {
846 ALOGE("UidPolicy::registerSelf linkToDeath failed: %d", res);
Eric Laurent4eb58f12018-12-07 16:41:02 -0800847
Steven Moreland2f348142019-07-02 15:59:07 -0700848 mAm.unregisterUidObserver(this);
Mikhail Naganoveae73eb2018-04-03 16:57:36 -0700849 }
Svet Ganovf4ddfef2018-01-16 07:37:58 -0800850}
851
852void AudioPolicyService::UidPolicy::unregisterSelf() {
Steven Moreland2f348142019-07-02 15:59:07 -0700853 mAm.unlinkToDeath(this);
854 mAm.unregisterUidObserver(this);
Mikhail Naganoveae73eb2018-04-03 16:57:36 -0700855 Mutex::Autolock _l(mLock);
856 mObserverRegistered = false;
Svet Ganovf4ddfef2018-01-16 07:37:58 -0800857}
858
Mikhail Naganoveae73eb2018-04-03 16:57:36 -0700859void AudioPolicyService::UidPolicy::binderDied(__unused const wp<IBinder> &who) {
860 Mutex::Autolock _l(mLock);
861 mCachedUids.clear();
862 mObserverRegistered = false;
Svet Ganovf4ddfef2018-01-16 07:37:58 -0800863}
864
Eric Laurente8c8b432018-10-17 10:08:02 -0700865void AudioPolicyService::UidPolicy::checkRegistered() {
Mikhail Naganoveae73eb2018-04-03 16:57:36 -0700866 bool needToReregister = false;
867 {
868 Mutex::Autolock _l(mLock);
869 needToReregister = !mObserverRegistered;
Svet Ganovf4ddfef2018-01-16 07:37:58 -0800870 }
Mikhail Naganoveae73eb2018-04-03 16:57:36 -0700871 if (needToReregister) {
872 // Looks like ActivityManager has died previously, attempt to re-register.
873 registerSelf();
874 }
Eric Laurente8c8b432018-10-17 10:08:02 -0700875}
876
877bool AudioPolicyService::UidPolicy::isUidActive(uid_t uid) {
878 if (isServiceUid(uid)) return true;
879 checkRegistered();
Mikhail Naganoveae73eb2018-04-03 16:57:36 -0700880 {
881 Mutex::Autolock _l(mLock);
882 auto overrideIter = mOverrideUids.find(uid);
883 if (overrideIter != mOverrideUids.end()) {
Eric Laurente8c8b432018-10-17 10:08:02 -0700884 return overrideIter->second.first;
Mikhail Naganoveae73eb2018-04-03 16:57:36 -0700885 }
886 // In an absense of the ActivityManager, assume everything to be active.
887 if (!mObserverRegistered) return true;
888 auto cacheIter = mCachedUids.find(uid);
Mikhail Naganoveba668a2018-04-05 08:13:15 -0700889 if (cacheIter != mCachedUids.end()) {
Eric Laurente8c8b432018-10-17 10:08:02 -0700890 return cacheIter->second.first;
Mikhail Naganoveae73eb2018-04-03 16:57:36 -0700891 }
892 }
893 ActivityManager am;
894 bool active = am.isUidActive(uid, String16("audioserver"));
895 {
896 Mutex::Autolock _l(mLock);
Eric Laurente8c8b432018-10-17 10:08:02 -0700897 mCachedUids.insert(std::pair<uid_t,
898 std::pair<bool, int>>(uid, std::pair<bool, int>(active,
899 ActivityManager::PROCESS_STATE_UNKNOWN)));
Mikhail Naganoveae73eb2018-04-03 16:57:36 -0700900 }
901 return active;
Svet Ganovf4ddfef2018-01-16 07:37:58 -0800902}
903
Eric Laurente8c8b432018-10-17 10:08:02 -0700904int AudioPolicyService::UidPolicy::getUidState(uid_t uid) {
905 if (isServiceUid(uid)) {
906 return ActivityManager::PROCESS_STATE_TOP;
907 }
908 checkRegistered();
909 {
910 Mutex::Autolock _l(mLock);
911 auto overrideIter = mOverrideUids.find(uid);
912 if (overrideIter != mOverrideUids.end()) {
913 if (overrideIter->second.first) {
914 if (overrideIter->second.second != ActivityManager::PROCESS_STATE_UNKNOWN) {
915 return overrideIter->second.second;
916 } else {
917 auto cacheIter = mCachedUids.find(uid);
918 if (cacheIter != mCachedUids.end()) {
919 return cacheIter->second.second;
920 }
921 }
922 }
923 return ActivityManager::PROCESS_STATE_UNKNOWN;
924 }
925 // In an absense of the ActivityManager, assume everything to be active.
926 if (!mObserverRegistered) {
927 return ActivityManager::PROCESS_STATE_TOP;
928 }
929 auto cacheIter = mCachedUids.find(uid);
930 if (cacheIter != mCachedUids.end()) {
931 if (cacheIter->second.first) {
932 return cacheIter->second.second;
933 } else {
934 return ActivityManager::PROCESS_STATE_UNKNOWN;
935 }
936 }
937 }
938 ActivityManager am;
939 bool active = am.isUidActive(uid, String16("audioserver"));
940 int state = ActivityManager::PROCESS_STATE_UNKNOWN;
941 if (active) {
942 state = am.getUidProcessState(uid, String16("audioserver"));
943 }
944 {
945 Mutex::Autolock _l(mLock);
946 mCachedUids.insert(std::pair<uid_t,
947 std::pair<bool, int>>(uid, std::pair<bool, int>(active, state)));
948 }
Eric Laurent4eb58f12018-12-07 16:41:02 -0800949
Eric Laurente8c8b432018-10-17 10:08:02 -0700950 return state;
951}
952
Mikhail Naganoveae73eb2018-04-03 16:57:36 -0700953void AudioPolicyService::UidPolicy::onUidActive(uid_t uid) {
Eric Laurente8c8b432018-10-17 10:08:02 -0700954 updateUid(&mCachedUids, uid, true, ActivityManager::PROCESS_STATE_UNKNOWN, true);
Mikhail Naganoveae73eb2018-04-03 16:57:36 -0700955}
956
957void AudioPolicyService::UidPolicy::onUidGone(uid_t uid, __unused bool disabled) {
Eric Laurente8c8b432018-10-17 10:08:02 -0700958 updateUid(&mCachedUids, uid, false, ActivityManager::PROCESS_STATE_UNKNOWN, false);
Mikhail Naganoveae73eb2018-04-03 16:57:36 -0700959}
960
961void AudioPolicyService::UidPolicy::onUidIdle(uid_t uid, __unused bool disabled) {
Eric Laurente8c8b432018-10-17 10:08:02 -0700962 updateUid(&mCachedUids, uid, false, ActivityManager::PROCESS_STATE_UNKNOWN, true);
Mikhail Naganoveae73eb2018-04-03 16:57:36 -0700963}
964
Eric Laurente8c8b432018-10-17 10:08:02 -0700965void AudioPolicyService::UidPolicy::onUidStateChanged(uid_t uid,
966 int32_t procState,
Hui Yu13ad0eb2019-09-09 10:27:07 -0700967 int64_t procStateSeq __unused,
968 int32_t capability __unused) {
Eric Laurente8c8b432018-10-17 10:08:02 -0700969 if (procState != ActivityManager::PROCESS_STATE_UNKNOWN) {
970 updateUid(&mCachedUids, uid, true, procState, true);
Mikhail Naganoveae73eb2018-04-03 16:57:36 -0700971 }
972}
973
974void AudioPolicyService::UidPolicy::updateOverrideUid(uid_t uid, bool active, bool insert) {
Eric Laurente8c8b432018-10-17 10:08:02 -0700975 updateUid(&mOverrideUids, uid, active, ActivityManager::PROCESS_STATE_UNKNOWN, insert);
976}
977
978void AudioPolicyService::UidPolicy::notifyService() {
979 sp<AudioPolicyService> service = mService.promote();
980 if (service != nullptr) {
981 service->updateUidStates();
Mikhail Naganoveae73eb2018-04-03 16:57:36 -0700982 }
983}
984
Eric Laurente8c8b432018-10-17 10:08:02 -0700985void AudioPolicyService::UidPolicy::updateUid(std::unordered_map<uid_t,
986 std::pair<bool, int>> *uids,
987 uid_t uid,
988 bool active,
989 int state,
990 bool insert) {
991 if (isServiceUid(uid)) {
992 return;
993 }
994 bool wasActive = isUidActive(uid);
995 int previousState = getUidState(uid);
Mikhail Naganoveae73eb2018-04-03 16:57:36 -0700996 {
997 Mutex::Autolock _l(mLock);
Eric Laurente8c8b432018-10-17 10:08:02 -0700998 updateUidLocked(uids, uid, active, state, insert);
Mikhail Naganoveae73eb2018-04-03 16:57:36 -0700999 }
Eric Laurente8c8b432018-10-17 10:08:02 -07001000 if (wasActive != isUidActive(uid) || state != previousState) {
1001 notifyService();
1002 }
Mikhail Naganoveae73eb2018-04-03 16:57:36 -07001003}
1004
Eric Laurente8c8b432018-10-17 10:08:02 -07001005void AudioPolicyService::UidPolicy::updateUidLocked(std::unordered_map<uid_t,
1006 std::pair<bool, int>> *uids,
1007 uid_t uid,
1008 bool active,
1009 int state,
1010 bool insert) {
Mikhail Naganoveae73eb2018-04-03 16:57:36 -07001011 auto it = uids->find(uid);
1012 if (it != uids->end()) {
Mikhail Naganoveae73eb2018-04-03 16:57:36 -07001013 if (insert) {
Eric Laurente8c8b432018-10-17 10:08:02 -07001014 if (state == ActivityManager::PROCESS_STATE_UNKNOWN) {
1015 it->second.first = active;
1016 }
1017 if (it->second.first) {
1018 it->second.second = state;
1019 } else {
1020 it->second.second = ActivityManager::PROCESS_STATE_UNKNOWN;
1021 }
Mikhail Naganoveae73eb2018-04-03 16:57:36 -07001022 } else {
1023 uids->erase(it);
1024 }
Eric Laurente8c8b432018-10-17 10:08:02 -07001025 } else if (insert && (state == ActivityManager::PROCESS_STATE_UNKNOWN)) {
1026 uids->insert(std::pair<uid_t, std::pair<bool, int>>(uid,
1027 std::pair<bool, int>(active, state)));
Mikhail Naganoveae73eb2018-04-03 16:57:36 -07001028 }
Svet Ganovf4ddfef2018-01-16 07:37:58 -08001029}
Mathias Agopian65ab4712010-07-14 17:59:35 -07001030
Eric Laurent4eb58f12018-12-07 16:41:02 -08001031bool AudioPolicyService::UidPolicy::isA11yOnTop() {
1032 for (const auto &uid : mCachedUids) {
Eric Laurent47670c92019-08-28 16:59:05 -07001033 if (!isA11yUid(uid.first)) {
Eric Laurent4eb58f12018-12-07 16:41:02 -08001034 continue;
1035 }
Amith Yamasanibcbb3002019-01-23 13:53:33 -08001036 if (uid.second.second >= ActivityManager::PROCESS_STATE_TOP
1037 && uid.second.second <= ActivityManager::PROCESS_STATE_BOUND_FOREGROUND_SERVICE) {
Eric Laurent4eb58f12018-12-07 16:41:02 -08001038 return true;
1039 }
1040 }
1041 return false;
1042}
1043
Eric Laurentb78763e2018-10-17 10:08:02 -07001044bool AudioPolicyService::UidPolicy::isA11yUid(uid_t uid)
1045{
1046 std::vector<uid_t>::iterator it = find(mA11yUids.begin(), mA11yUids.end(), uid);
1047 return it != mA11yUids.end();
1048}
1049
Michael Groovercfd28302018-12-11 19:16:46 -08001050// ----------- AudioPolicyService::SensorPrivacyService implementation ----------
1051void AudioPolicyService::SensorPrivacyPolicy::registerSelf() {
1052 SensorPrivacyManager spm;
1053 mSensorPrivacyEnabled = spm.isSensorPrivacyEnabled();
1054 spm.addSensorPrivacyListener(this);
1055}
1056
1057void AudioPolicyService::SensorPrivacyPolicy::unregisterSelf() {
1058 SensorPrivacyManager spm;
1059 spm.removeSensorPrivacyListener(this);
1060}
1061
1062bool AudioPolicyService::SensorPrivacyPolicy::isSensorPrivacyEnabled() {
1063 return mSensorPrivacyEnabled;
1064}
1065
1066binder::Status AudioPolicyService::SensorPrivacyPolicy::onSensorPrivacyChanged(bool enabled) {
1067 mSensorPrivacyEnabled = enabled;
1068 sp<AudioPolicyService> service = mService.promote();
1069 if (service != nullptr) {
1070 service->updateUidStates();
1071 }
1072 return binder::Status::ok();
1073}
1074
Mathias Agopian65ab4712010-07-14 17:59:35 -07001075// ----------- AudioPolicyService::AudioCommandThread implementation ----------
1076
Eric Laurentbfb1b832013-01-07 09:53:42 -08001077AudioPolicyService::AudioCommandThread::AudioCommandThread(String8 name,
1078 const wp<AudioPolicyService>& service)
1079 : Thread(false), mName(name), mService(service)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001080{
Mathias Agopian65ab4712010-07-14 17:59:35 -07001081}
1082
1083
1084AudioPolicyService::AudioCommandThread::~AudioCommandThread()
1085{
Eric Laurentbfb1b832013-01-07 09:53:42 -08001086 if (!mAudioCommands.isEmpty()) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07001087 release_wake_lock(mName.string());
1088 }
1089 mAudioCommands.clear();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001090}
1091
1092void AudioPolicyService::AudioCommandThread::onFirstRef()
1093{
Eric Laurentbfb1b832013-01-07 09:53:42 -08001094 run(mName.string(), ANDROID_PRIORITY_AUDIO);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001095}
1096
1097bool AudioPolicyService::AudioCommandThread::threadLoop()
1098{
Eric Laurentd7eda8d2016-02-02 17:18:39 -08001099 nsecs_t waitTime = -1;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001100
1101 mLock.lock();
1102 while (!exitPending())
1103 {
Eric Laurent59a89232014-06-08 14:14:17 -07001104 sp<AudioPolicyService> svc;
1105 while (!mAudioCommands.isEmpty() && !exitPending()) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07001106 nsecs_t curTime = systemTime();
1107 // commands are sorted by increasing time stamp: execute them from index 0 and up
1108 if (mAudioCommands[0]->mTime <= curTime) {
Eric Laurent0ede8922014-05-09 18:04:42 -07001109 sp<AudioCommand> command = mAudioCommands[0];
Mathias Agopian65ab4712010-07-14 17:59:35 -07001110 mAudioCommands.removeAt(0);
Eric Laurent0ede8922014-05-09 18:04:42 -07001111 mLastCommand = command;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001112
1113 switch (command->mCommand) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07001114 case SET_VOLUME: {
Eric Laurent0ede8922014-05-09 18:04:42 -07001115 VolumeData *data = (VolumeData *)command->mParam.get();
Steve Block3856b092011-10-20 11:56:00 +01001116 ALOGV("AudioCommandThread() processing set volume stream %d, \
Eric Laurentde070132010-07-13 04:45:46 -07001117 volume %f, output %d", data->mStream, data->mVolume, data->mIO);
Andy Hungfe726a62018-09-27 15:17:25 -07001118 mLock.unlock();
Eric Laurentde070132010-07-13 04:45:46 -07001119 command->mStatus = AudioSystem::setStreamVolume(data->mStream,
1120 data->mVolume,
1121 data->mIO);
Andy Hungfe726a62018-09-27 15:17:25 -07001122 mLock.lock();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001123 }break;
1124 case SET_PARAMETERS: {
Eric Laurent0ede8922014-05-09 18:04:42 -07001125 ParametersData *data = (ParametersData *)command->mParam.get();
Glenn Kastene53b9ea2012-03-12 16:29:55 -07001126 ALOGV("AudioCommandThread() processing set parameters string %s, io %d",
1127 data->mKeyValuePairs.string(), data->mIO);
Andy Hungfe726a62018-09-27 15:17:25 -07001128 mLock.unlock();
Glenn Kastene53b9ea2012-03-12 16:29:55 -07001129 command->mStatus = AudioSystem::setParameters(data->mIO, data->mKeyValuePairs);
Andy Hungfe726a62018-09-27 15:17:25 -07001130 mLock.lock();
Glenn Kastene53b9ea2012-03-12 16:29:55 -07001131 }break;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001132 case SET_VOICE_VOLUME: {
Eric Laurent0ede8922014-05-09 18:04:42 -07001133 VoiceVolumeData *data = (VoiceVolumeData *)command->mParam.get();
Steve Block3856b092011-10-20 11:56:00 +01001134 ALOGV("AudioCommandThread() processing set voice volume volume %f",
Eric Laurentde070132010-07-13 04:45:46 -07001135 data->mVolume);
Andy Hungfe726a62018-09-27 15:17:25 -07001136 mLock.unlock();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001137 command->mStatus = AudioSystem::setVoiceVolume(data->mVolume);
Andy Hungfe726a62018-09-27 15:17:25 -07001138 mLock.lock();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001139 }break;
Eric Laurentbfb1b832013-01-07 09:53:42 -08001140 case STOP_OUTPUT: {
Eric Laurent0ede8922014-05-09 18:04:42 -07001141 StopOutputData *data = (StopOutputData *)command->mParam.get();
Eric Laurentd7fe0862018-07-14 16:48:01 -07001142 ALOGV("AudioCommandThread() processing stop output portId %d",
1143 data->mPortId);
Eric Laurent59a89232014-06-08 14:14:17 -07001144 svc = mService.promote();
Eric Laurentbfb1b832013-01-07 09:53:42 -08001145 if (svc == 0) {
1146 break;
1147 }
1148 mLock.unlock();
Eric Laurentd7fe0862018-07-14 16:48:01 -07001149 svc->doStopOutput(data->mPortId);
Eric Laurentbfb1b832013-01-07 09:53:42 -08001150 mLock.lock();
Eric Laurentbfb1b832013-01-07 09:53:42 -08001151 }break;
1152 case RELEASE_OUTPUT: {
Eric Laurent0ede8922014-05-09 18:04:42 -07001153 ReleaseOutputData *data = (ReleaseOutputData *)command->mParam.get();
Eric Laurentd7fe0862018-07-14 16:48:01 -07001154 ALOGV("AudioCommandThread() processing release output portId %d",
1155 data->mPortId);
Eric Laurent59a89232014-06-08 14:14:17 -07001156 svc = mService.promote();
Eric Laurentbfb1b832013-01-07 09:53:42 -08001157 if (svc == 0) {
1158 break;
1159 }
1160 mLock.unlock();
Eric Laurentd7fe0862018-07-14 16:48:01 -07001161 svc->doReleaseOutput(data->mPortId);
Eric Laurentbfb1b832013-01-07 09:53:42 -08001162 mLock.lock();
Eric Laurentbfb1b832013-01-07 09:53:42 -08001163 }break;
Eric Laurent951f4552014-05-20 10:48:17 -07001164 case CREATE_AUDIO_PATCH: {
1165 CreateAudioPatchData *data = (CreateAudioPatchData *)command->mParam.get();
1166 ALOGV("AudioCommandThread() processing create audio patch");
1167 sp<IAudioFlinger> af = AudioSystem::get_audio_flinger();
1168 if (af == 0) {
1169 command->mStatus = PERMISSION_DENIED;
1170 } else {
Andy Hungfe726a62018-09-27 15:17:25 -07001171 mLock.unlock();
Eric Laurent951f4552014-05-20 10:48:17 -07001172 command->mStatus = af->createAudioPatch(&data->mPatch, &data->mHandle);
Andy Hungfe726a62018-09-27 15:17:25 -07001173 mLock.lock();
Eric Laurent951f4552014-05-20 10:48:17 -07001174 }
1175 } break;
1176 case RELEASE_AUDIO_PATCH: {
1177 ReleaseAudioPatchData *data = (ReleaseAudioPatchData *)command->mParam.get();
1178 ALOGV("AudioCommandThread() processing release audio patch");
1179 sp<IAudioFlinger> af = AudioSystem::get_audio_flinger();
1180 if (af == 0) {
1181 command->mStatus = PERMISSION_DENIED;
1182 } else {
Andy Hungfe726a62018-09-27 15:17:25 -07001183 mLock.unlock();
Eric Laurent951f4552014-05-20 10:48:17 -07001184 command->mStatus = af->releaseAudioPatch(data->mHandle);
Andy Hungfe726a62018-09-27 15:17:25 -07001185 mLock.lock();
Eric Laurent951f4552014-05-20 10:48:17 -07001186 }
1187 } break;
Eric Laurentb52c1522014-05-20 11:27:36 -07001188 case UPDATE_AUDIOPORT_LIST: {
1189 ALOGV("AudioCommandThread() processing update audio port list");
Eric Laurent59a89232014-06-08 14:14:17 -07001190 svc = mService.promote();
Eric Laurentb52c1522014-05-20 11:27:36 -07001191 if (svc == 0) {
1192 break;
1193 }
1194 mLock.unlock();
1195 svc->doOnAudioPortListUpdate();
1196 mLock.lock();
1197 }break;
1198 case UPDATE_AUDIOPATCH_LIST: {
1199 ALOGV("AudioCommandThread() processing update audio patch list");
Eric Laurent59a89232014-06-08 14:14:17 -07001200 svc = mService.promote();
Eric Laurentb52c1522014-05-20 11:27:36 -07001201 if (svc == 0) {
1202 break;
1203 }
1204 mLock.unlock();
1205 svc->doOnAudioPatchListUpdate();
1206 mLock.lock();
1207 }break;
François Gaffiecfe17322018-11-07 13:41:29 +01001208 case CHANGED_AUDIOVOLUMEGROUP: {
1209 AudioVolumeGroupData *data =
1210 static_cast<AudioVolumeGroupData *>(command->mParam.get());
1211 ALOGV("AudioCommandThread() processing update audio volume group");
1212 svc = mService.promote();
1213 if (svc == 0) {
1214 break;
1215 }
1216 mLock.unlock();
1217 svc->doOnAudioVolumeGroupChanged(data->mGroup, data->mFlags);
1218 mLock.lock();
1219 }break;
Eric Laurente1715a42014-05-20 11:30:42 -07001220 case SET_AUDIOPORT_CONFIG: {
1221 SetAudioPortConfigData *data = (SetAudioPortConfigData *)command->mParam.get();
1222 ALOGV("AudioCommandThread() processing set port config");
1223 sp<IAudioFlinger> af = AudioSystem::get_audio_flinger();
1224 if (af == 0) {
1225 command->mStatus = PERMISSION_DENIED;
1226 } else {
Andy Hungfe726a62018-09-27 15:17:25 -07001227 mLock.unlock();
Eric Laurente1715a42014-05-20 11:30:42 -07001228 command->mStatus = af->setAudioPortConfig(&data->mConfig);
Andy Hungfe726a62018-09-27 15:17:25 -07001229 mLock.lock();
Eric Laurente1715a42014-05-20 11:30:42 -07001230 }
1231 } break;
Jean-Michel Trivide801052015-04-14 19:10:14 -07001232 case DYN_POLICY_MIX_STATE_UPDATE: {
1233 DynPolicyMixStateUpdateData *data =
1234 (DynPolicyMixStateUpdateData *)command->mParam.get();
Jean-Michel Trivide801052015-04-14 19:10:14 -07001235 ALOGV("AudioCommandThread() processing dyn policy mix state update %s %d",
1236 data->mRegId.string(), data->mState);
1237 svc = mService.promote();
1238 if (svc == 0) {
1239 break;
1240 }
1241 mLock.unlock();
1242 svc->doOnDynamicPolicyMixStateUpdate(data->mRegId, data->mState);
1243 mLock.lock();
1244 } break;
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -08001245 case RECORDING_CONFIGURATION_UPDATE: {
1246 RecordingConfigurationUpdateData *data =
1247 (RecordingConfigurationUpdateData *)command->mParam.get();
1248 ALOGV("AudioCommandThread() processing recording configuration update");
1249 svc = mService.promote();
1250 if (svc == 0) {
1251 break;
1252 }
1253 mLock.unlock();
Jean-Michel Triviac4e4292016-12-22 11:39:31 -08001254 svc->doOnRecordingConfigurationUpdate(data->mEvent, &data->mClientInfo,
Eric Laurenta9f86652018-11-28 17:23:11 -08001255 &data->mClientConfig, data->mClientEffects,
1256 &data->mDeviceConfig, data->mEffects,
1257 data->mPatchHandle, data->mSource);
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -08001258 mLock.lock();
1259 } break;
Eric Laurentb20cf7d2019-04-05 19:37:34 -07001260 case SET_EFFECT_SUSPENDED: {
1261 SetEffectSuspendedData *data = (SetEffectSuspendedData *)command->mParam.get();
1262 ALOGV("AudioCommandThread() processing set effect suspended");
1263 sp<IAudioFlinger> af = AudioSystem::get_audio_flinger();
1264 if (af != 0) {
1265 mLock.unlock();
1266 af->setEffectSuspended(data->mEffectId, data->mSessionId, data->mSuspended);
1267 mLock.lock();
1268 }
1269 } break;
1270
Mathias Agopian65ab4712010-07-14 17:59:35 -07001271 default:
Steve Block5ff1dd52012-01-05 23:22:43 +00001272 ALOGW("AudioCommandThread() unknown command %d", command->mCommand);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001273 }
Eric Laurent0ede8922014-05-09 18:04:42 -07001274 {
1275 Mutex::Autolock _l(command->mLock);
1276 if (command->mWaitStatus) {
1277 command->mWaitStatus = false;
1278 command->mCond.signal();
1279 }
1280 }
Eric Laurentd7eda8d2016-02-02 17:18:39 -08001281 waitTime = -1;
Zach Janga754b4f2015-10-27 01:29:34 +00001282 // release mLock before releasing strong reference on the service as
1283 // AudioPolicyService destructor calls AudioCommandThread::exit() which
1284 // acquires mLock.
1285 mLock.unlock();
1286 svc.clear();
1287 mLock.lock();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001288 } else {
1289 waitTime = mAudioCommands[0]->mTime - curTime;
1290 break;
1291 }
1292 }
Zach Janga754b4f2015-10-27 01:29:34 +00001293
1294 // release delayed commands wake lock if the queue is empty
1295 if (mAudioCommands.isEmpty()) {
Ricardo Garcia05f2fdc2014-07-24 15:48:24 -07001296 release_wake_lock(mName.string());
Zach Janga754b4f2015-10-27 01:29:34 +00001297 }
1298
1299 // At this stage we have either an empty command queue or the first command in the queue
1300 // has a finite delay. So unless we are exiting it is safe to wait.
1301 if (!exitPending()) {
Eric Laurent59a89232014-06-08 14:14:17 -07001302 ALOGV("AudioCommandThread() going to sleep");
Eric Laurentd7eda8d2016-02-02 17:18:39 -08001303 if (waitTime == -1) {
1304 mWaitWorkCV.wait(mLock);
1305 } else {
1306 mWaitWorkCV.waitRelative(mLock, waitTime);
1307 }
Eric Laurent59a89232014-06-08 14:14:17 -07001308 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001309 }
Ricardo Garcia05f2fdc2014-07-24 15:48:24 -07001310 // release delayed commands wake lock before quitting
1311 if (!mAudioCommands.isEmpty()) {
1312 release_wake_lock(mName.string());
1313 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001314 mLock.unlock();
1315 return false;
1316}
1317
1318status_t AudioPolicyService::AudioCommandThread::dump(int fd)
1319{
1320 const size_t SIZE = 256;
1321 char buffer[SIZE];
1322 String8 result;
1323
1324 snprintf(buffer, SIZE, "AudioCommandThread %p Dump\n", this);
1325 result.append(buffer);
1326 write(fd, result.string(), result.size());
1327
Mikhail Naganov959e2d02019-03-28 11:08:19 -07001328 bool locked = dumpTryLock(mLock);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001329 if (!locked) {
1330 String8 result2(kCmdDeadlockedString);
1331 write(fd, result2.string(), result2.size());
1332 }
1333
1334 snprintf(buffer, SIZE, "- Commands:\n");
1335 result = String8(buffer);
1336 result.append(" Command Time Wait pParam\n");
Glenn Kasten8d6a2442012-02-08 14:04:28 -08001337 for (size_t i = 0; i < mAudioCommands.size(); i++) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07001338 mAudioCommands[i]->dump(buffer, SIZE);
1339 result.append(buffer);
1340 }
1341 result.append(" Last Command\n");
Eric Laurent0ede8922014-05-09 18:04:42 -07001342 if (mLastCommand != 0) {
1343 mLastCommand->dump(buffer, SIZE);
1344 result.append(buffer);
1345 } else {
1346 result.append(" none\n");
1347 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001348
1349 write(fd, result.string(), result.size());
1350
1351 if (locked) mLock.unlock();
1352
1353 return NO_ERROR;
1354}
1355
Glenn Kastenfff6d712012-01-12 16:38:12 -08001356status_t AudioPolicyService::AudioCommandThread::volumeCommand(audio_stream_type_t stream,
Eric Laurentde070132010-07-13 04:45:46 -07001357 float volume,
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001358 audio_io_handle_t output,
Eric Laurentde070132010-07-13 04:45:46 -07001359 int delayMs)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001360{
Eric Laurent0ede8922014-05-09 18:04:42 -07001361 sp<AudioCommand> command = new AudioCommand();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001362 command->mCommand = SET_VOLUME;
Eric Laurent0ede8922014-05-09 18:04:42 -07001363 sp<VolumeData> data = new VolumeData();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001364 data->mStream = stream;
1365 data->mVolume = volume;
1366 data->mIO = output;
1367 command->mParam = data;
Eric Laurent0ede8922014-05-09 18:04:42 -07001368 command->mWaitStatus = true;
Steve Block3856b092011-10-20 11:56:00 +01001369 ALOGV("AudioCommandThread() adding set volume stream %d, volume %f, output %d",
Eric Laurentde070132010-07-13 04:45:46 -07001370 stream, volume, output);
Eric Laurent0ede8922014-05-09 18:04:42 -07001371 return sendCommand(command, delayMs);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001372}
1373
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001374status_t AudioPolicyService::AudioCommandThread::parametersCommand(audio_io_handle_t ioHandle,
Dima Zavinfce7a472011-04-19 22:30:36 -07001375 const char *keyValuePairs,
Eric Laurentde070132010-07-13 04:45:46 -07001376 int delayMs)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001377{
Eric Laurent0ede8922014-05-09 18:04:42 -07001378 sp<AudioCommand> command = new AudioCommand();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001379 command->mCommand = SET_PARAMETERS;
Eric Laurent0ede8922014-05-09 18:04:42 -07001380 sp<ParametersData> data = new ParametersData();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001381 data->mIO = ioHandle;
Dima Zavinfce7a472011-04-19 22:30:36 -07001382 data->mKeyValuePairs = String8(keyValuePairs);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001383 command->mParam = data;
Eric Laurent0ede8922014-05-09 18:04:42 -07001384 command->mWaitStatus = true;
Steve Block3856b092011-10-20 11:56:00 +01001385 ALOGV("AudioCommandThread() adding set parameter string %s, io %d ,delay %d",
Dima Zavinfce7a472011-04-19 22:30:36 -07001386 keyValuePairs, ioHandle, delayMs);
Eric Laurent0ede8922014-05-09 18:04:42 -07001387 return sendCommand(command, delayMs);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001388}
1389
1390status_t AudioPolicyService::AudioCommandThread::voiceVolumeCommand(float volume, int delayMs)
1391{
Eric Laurent0ede8922014-05-09 18:04:42 -07001392 sp<AudioCommand> command = new AudioCommand();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001393 command->mCommand = SET_VOICE_VOLUME;
Eric Laurent0ede8922014-05-09 18:04:42 -07001394 sp<VoiceVolumeData> data = new VoiceVolumeData();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001395 data->mVolume = volume;
1396 command->mParam = data;
Eric Laurent0ede8922014-05-09 18:04:42 -07001397 command->mWaitStatus = true;
Steve Block3856b092011-10-20 11:56:00 +01001398 ALOGV("AudioCommandThread() adding set voice volume volume %f", volume);
Eric Laurent0ede8922014-05-09 18:04:42 -07001399 return sendCommand(command, delayMs);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001400}
1401
Eric Laurentb20cf7d2019-04-05 19:37:34 -07001402void AudioPolicyService::AudioCommandThread::setEffectSuspendedCommand(int effectId,
1403 audio_session_t sessionId,
1404 bool suspended)
1405{
1406 sp<AudioCommand> command = new AudioCommand();
1407 command->mCommand = SET_EFFECT_SUSPENDED;
1408 sp<SetEffectSuspendedData> data = new SetEffectSuspendedData();
1409 data->mEffectId = effectId;
1410 data->mSessionId = sessionId;
1411 data->mSuspended = suspended;
1412 command->mParam = data;
1413 ALOGV("AudioCommandThread() adding set suspended effectId %d sessionId %d suspended %d",
1414 effectId, sessionId, suspended);
1415 sendCommand(command);
1416}
1417
1418
Eric Laurentd7fe0862018-07-14 16:48:01 -07001419void AudioPolicyService::AudioCommandThread::stopOutputCommand(audio_port_handle_t portId)
Eric Laurentbfb1b832013-01-07 09:53:42 -08001420{
Eric Laurent0ede8922014-05-09 18:04:42 -07001421 sp<AudioCommand> command = new AudioCommand();
Eric Laurentbfb1b832013-01-07 09:53:42 -08001422 command->mCommand = STOP_OUTPUT;
Eric Laurent0ede8922014-05-09 18:04:42 -07001423 sp<StopOutputData> data = new StopOutputData();
Eric Laurentd7fe0862018-07-14 16:48:01 -07001424 data->mPortId = portId;
Jesper Tragardh48412dc2014-03-24 14:12:43 +01001425 command->mParam = data;
Eric Laurentd7fe0862018-07-14 16:48:01 -07001426 ALOGV("AudioCommandThread() adding stop output portId %d", portId);
Eric Laurent0ede8922014-05-09 18:04:42 -07001427 sendCommand(command);
Eric Laurentbfb1b832013-01-07 09:53:42 -08001428}
1429
Eric Laurentd7fe0862018-07-14 16:48:01 -07001430void AudioPolicyService::AudioCommandThread::releaseOutputCommand(audio_port_handle_t portId)
Eric Laurentbfb1b832013-01-07 09:53:42 -08001431{
Eric Laurent0ede8922014-05-09 18:04:42 -07001432 sp<AudioCommand> command = new AudioCommand();
Eric Laurentbfb1b832013-01-07 09:53:42 -08001433 command->mCommand = RELEASE_OUTPUT;
Eric Laurent0ede8922014-05-09 18:04:42 -07001434 sp<ReleaseOutputData> data = new ReleaseOutputData();
Eric Laurentd7fe0862018-07-14 16:48:01 -07001435 data->mPortId = portId;
Jesper Tragardh48412dc2014-03-24 14:12:43 +01001436 command->mParam = data;
Eric Laurentd7fe0862018-07-14 16:48:01 -07001437 ALOGV("AudioCommandThread() adding release output portId %d", portId);
Eric Laurent0ede8922014-05-09 18:04:42 -07001438 sendCommand(command);
1439}
1440
Eric Laurent951f4552014-05-20 10:48:17 -07001441status_t AudioPolicyService::AudioCommandThread::createAudioPatchCommand(
1442 const struct audio_patch *patch,
1443 audio_patch_handle_t *handle,
1444 int delayMs)
1445{
1446 status_t status = NO_ERROR;
1447
1448 sp<AudioCommand> command = new AudioCommand();
1449 command->mCommand = CREATE_AUDIO_PATCH;
1450 CreateAudioPatchData *data = new CreateAudioPatchData();
1451 data->mPatch = *patch;
1452 data->mHandle = *handle;
1453 command->mParam = data;
1454 command->mWaitStatus = true;
1455 ALOGV("AudioCommandThread() adding create patch delay %d", delayMs);
1456 status = sendCommand(command, delayMs);
1457 if (status == NO_ERROR) {
1458 *handle = data->mHandle;
1459 }
1460 return status;
1461}
1462
1463status_t AudioPolicyService::AudioCommandThread::releaseAudioPatchCommand(audio_patch_handle_t handle,
1464 int delayMs)
1465{
1466 sp<AudioCommand> command = new AudioCommand();
1467 command->mCommand = RELEASE_AUDIO_PATCH;
1468 ReleaseAudioPatchData *data = new ReleaseAudioPatchData();
1469 data->mHandle = handle;
1470 command->mParam = data;
1471 command->mWaitStatus = true;
1472 ALOGV("AudioCommandThread() adding release patch delay %d", delayMs);
1473 return sendCommand(command, delayMs);
1474}
1475
Eric Laurentb52c1522014-05-20 11:27:36 -07001476void AudioPolicyService::AudioCommandThread::updateAudioPortListCommand()
1477{
1478 sp<AudioCommand> command = new AudioCommand();
1479 command->mCommand = UPDATE_AUDIOPORT_LIST;
1480 ALOGV("AudioCommandThread() adding update audio port list");
1481 sendCommand(command);
1482}
1483
1484void AudioPolicyService::AudioCommandThread::updateAudioPatchListCommand()
1485{
1486 sp<AudioCommand>command = new AudioCommand();
1487 command->mCommand = UPDATE_AUDIOPATCH_LIST;
1488 ALOGV("AudioCommandThread() adding update audio patch list");
1489 sendCommand(command);
1490}
1491
François Gaffiecfe17322018-11-07 13:41:29 +01001492void AudioPolicyService::AudioCommandThread::changeAudioVolumeGroupCommand(volume_group_t group,
1493 int flags)
1494{
1495 sp<AudioCommand>command = new AudioCommand();
1496 command->mCommand = CHANGED_AUDIOVOLUMEGROUP;
1497 AudioVolumeGroupData *data= new AudioVolumeGroupData();
1498 data->mGroup = group;
1499 data->mFlags = flags;
1500 command->mParam = data;
1501 ALOGV("AudioCommandThread() adding audio volume group changed");
1502 sendCommand(command);
1503}
1504
Eric Laurente1715a42014-05-20 11:30:42 -07001505status_t AudioPolicyService::AudioCommandThread::setAudioPortConfigCommand(
1506 const struct audio_port_config *config, int delayMs)
1507{
1508 sp<AudioCommand> command = new AudioCommand();
1509 command->mCommand = SET_AUDIOPORT_CONFIG;
1510 SetAudioPortConfigData *data = new SetAudioPortConfigData();
1511 data->mConfig = *config;
1512 command->mParam = data;
1513 command->mWaitStatus = true;
1514 ALOGV("AudioCommandThread() adding set port config delay %d", delayMs);
1515 return sendCommand(command, delayMs);
1516}
1517
Jean-Michel Trivide801052015-04-14 19:10:14 -07001518void AudioPolicyService::AudioCommandThread::dynamicPolicyMixStateUpdateCommand(
Chih-Hung Hsiehe964d4e2016-08-09 14:31:32 -07001519 const String8& regId, int32_t state)
Jean-Michel Trivide801052015-04-14 19:10:14 -07001520{
1521 sp<AudioCommand> command = new AudioCommand();
1522 command->mCommand = DYN_POLICY_MIX_STATE_UPDATE;
1523 DynPolicyMixStateUpdateData *data = new DynPolicyMixStateUpdateData();
1524 data->mRegId = regId;
1525 data->mState = state;
1526 command->mParam = data;
1527 ALOGV("AudioCommandThread() sending dynamic policy mix (id=%s) state update to %d",
1528 regId.string(), state);
1529 sendCommand(command);
1530}
1531
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -08001532void AudioPolicyService::AudioCommandThread::recordingConfigurationUpdateCommand(
Eric Laurenta9f86652018-11-28 17:23:11 -08001533 int event,
1534 const record_client_info_t *clientInfo,
1535 const audio_config_base_t *clientConfig,
1536 std::vector<effect_descriptor_t> clientEffects,
1537 const audio_config_base_t *deviceConfig,
1538 std::vector<effect_descriptor_t> effects,
1539 audio_patch_handle_t patchHandle,
1540 audio_source_t source)
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -08001541{
1542 sp<AudioCommand>command = new AudioCommand();
1543 command->mCommand = RECORDING_CONFIGURATION_UPDATE;
1544 RecordingConfigurationUpdateData *data = new RecordingConfigurationUpdateData();
1545 data->mEvent = event;
Jean-Michel Triviac4e4292016-12-22 11:39:31 -08001546 data->mClientInfo = *clientInfo;
Jean-Michel Trivi7281aa92016-02-17 15:33:40 -08001547 data->mClientConfig = *clientConfig;
Eric Laurenta9f86652018-11-28 17:23:11 -08001548 data->mClientEffects = clientEffects;
Jean-Michel Trivi7281aa92016-02-17 15:33:40 -08001549 data->mDeviceConfig = *deviceConfig;
Eric Laurenta9f86652018-11-28 17:23:11 -08001550 data->mEffects = effects;
Jean-Michel Trivi8c7cf3b2016-02-25 17:08:24 -08001551 data->mPatchHandle = patchHandle;
Eric Laurenta9f86652018-11-28 17:23:11 -08001552 data->mSource = source;
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -08001553 command->mParam = data;
Jean-Michel Triviac4e4292016-12-22 11:39:31 -08001554 ALOGV("AudioCommandThread() adding recording configuration update event %d, source %d uid %u",
1555 event, clientInfo->source, clientInfo->uid);
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -08001556 sendCommand(command);
1557}
1558
Eric Laurent0ede8922014-05-09 18:04:42 -07001559status_t AudioPolicyService::AudioCommandThread::sendCommand(sp<AudioCommand>& command, int delayMs)
1560{
1561 {
1562 Mutex::Autolock _l(mLock);
1563 insertCommand_l(command, delayMs);
1564 mWaitWorkCV.signal();
1565 }
1566 Mutex::Autolock _l(command->mLock);
1567 while (command->mWaitStatus) {
1568 nsecs_t timeOutNs = kAudioCommandTimeoutNs + milliseconds(delayMs);
1569 if (command->mCond.waitRelative(command->mLock, timeOutNs) != NO_ERROR) {
1570 command->mStatus = TIMED_OUT;
1571 command->mWaitStatus = false;
1572 }
1573 }
1574 return command->mStatus;
Eric Laurentbfb1b832013-01-07 09:53:42 -08001575}
1576
Mathias Agopian65ab4712010-07-14 17:59:35 -07001577// insertCommand_l() must be called with mLock held
Eric Laurent0ede8922014-05-09 18:04:42 -07001578void AudioPolicyService::AudioCommandThread::insertCommand_l(sp<AudioCommand>& command, int delayMs)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001579{
Glenn Kasten8d6a2442012-02-08 14:04:28 -08001580 ssize_t i; // not size_t because i will count down to -1
Eric Laurent0ede8922014-05-09 18:04:42 -07001581 Vector < sp<AudioCommand> > removedCommands;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001582 command->mTime = systemTime() + milliseconds(delayMs);
1583
1584 // acquire wake lock to make sure delayed commands are processed
Eric Laurentbfb1b832013-01-07 09:53:42 -08001585 if (mAudioCommands.isEmpty()) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07001586 acquire_wake_lock(PARTIAL_WAKE_LOCK, mName.string());
1587 }
1588
1589 // check same pending commands with later time stamps and eliminate them
Ivan Lozano5ff158f2017-10-30 09:06:24 -07001590 for (i = (ssize_t)mAudioCommands.size()-1; i >= 0; i--) {
Eric Laurent0ede8922014-05-09 18:04:42 -07001591 sp<AudioCommand> command2 = mAudioCommands[i];
Mathias Agopian65ab4712010-07-14 17:59:35 -07001592 // commands are sorted by increasing time stamp: no need to scan the rest of mAudioCommands
1593 if (command2->mTime <= command->mTime) break;
Eric Laurente45b48a2014-09-04 16:40:57 -07001594
1595 // create audio patch or release audio patch commands are equivalent
1596 // with regard to filtering
1597 if ((command->mCommand == CREATE_AUDIO_PATCH) ||
1598 (command->mCommand == RELEASE_AUDIO_PATCH)) {
1599 if ((command2->mCommand != CREATE_AUDIO_PATCH) &&
1600 (command2->mCommand != RELEASE_AUDIO_PATCH)) {
1601 continue;
1602 }
1603 } else if (command2->mCommand != command->mCommand) continue;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001604
1605 switch (command->mCommand) {
1606 case SET_PARAMETERS: {
Eric Laurent0ede8922014-05-09 18:04:42 -07001607 ParametersData *data = (ParametersData *)command->mParam.get();
1608 ParametersData *data2 = (ParametersData *)command2->mParam.get();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001609 if (data->mIO != data2->mIO) break;
Steve Block3856b092011-10-20 11:56:00 +01001610 ALOGV("Comparing parameter command %s to new command %s",
Eric Laurentde070132010-07-13 04:45:46 -07001611 data2->mKeyValuePairs.string(), data->mKeyValuePairs.string());
Mathias Agopian65ab4712010-07-14 17:59:35 -07001612 AudioParameter param = AudioParameter(data->mKeyValuePairs);
1613 AudioParameter param2 = AudioParameter(data2->mKeyValuePairs);
1614 for (size_t j = 0; j < param.size(); j++) {
Glenn Kastene53b9ea2012-03-12 16:29:55 -07001615 String8 key;
1616 String8 value;
1617 param.getAt(j, key, value);
1618 for (size_t k = 0; k < param2.size(); k++) {
1619 String8 key2;
1620 String8 value2;
1621 param2.getAt(k, key2, value2);
1622 if (key2 == key) {
1623 param2.remove(key2);
1624 ALOGV("Filtering out parameter %s", key2.string());
1625 break;
1626 }
1627 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001628 }
1629 // if all keys have been filtered out, remove the command.
1630 // otherwise, update the key value pairs
1631 if (param2.size() == 0) {
1632 removedCommands.add(command2);
1633 } else {
1634 data2->mKeyValuePairs = param2.toString();
1635 }
Eric Laurent21e54562013-09-23 12:08:05 -07001636 command->mTime = command2->mTime;
1637 // force delayMs to non 0 so that code below does not request to wait for
1638 // command status as the command is now delayed
1639 delayMs = 1;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001640 } break;
1641
1642 case SET_VOLUME: {
Eric Laurent0ede8922014-05-09 18:04:42 -07001643 VolumeData *data = (VolumeData *)command->mParam.get();
1644 VolumeData *data2 = (VolumeData *)command2->mParam.get();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001645 if (data->mIO != data2->mIO) break;
1646 if (data->mStream != data2->mStream) break;
Steve Block3856b092011-10-20 11:56:00 +01001647 ALOGV("Filtering out volume command on output %d for stream %d",
Eric Laurentde070132010-07-13 04:45:46 -07001648 data->mIO, data->mStream);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001649 removedCommands.add(command2);
Eric Laurent21e54562013-09-23 12:08:05 -07001650 command->mTime = command2->mTime;
1651 // force delayMs to non 0 so that code below does not request to wait for
1652 // command status as the command is now delayed
1653 delayMs = 1;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001654 } break;
Eric Laurente45b48a2014-09-04 16:40:57 -07001655
Eric Laurentbaf35fe2016-07-27 15:36:53 -07001656 case SET_VOICE_VOLUME: {
1657 VoiceVolumeData *data = (VoiceVolumeData *)command->mParam.get();
1658 VoiceVolumeData *data2 = (VoiceVolumeData *)command2->mParam.get();
1659 ALOGV("Filtering out voice volume command value %f replaced by %f",
1660 data2->mVolume, data->mVolume);
1661 removedCommands.add(command2);
1662 command->mTime = command2->mTime;
1663 // force delayMs to non 0 so that code below does not request to wait for
1664 // command status as the command is now delayed
1665 delayMs = 1;
1666 } break;
1667
Eric Laurente45b48a2014-09-04 16:40:57 -07001668 case CREATE_AUDIO_PATCH:
1669 case RELEASE_AUDIO_PATCH: {
1670 audio_patch_handle_t handle;
Haynes Mathew Georgea2d4a6d2014-10-13 13:05:22 -07001671 struct audio_patch patch;
Eric Laurente45b48a2014-09-04 16:40:57 -07001672 if (command->mCommand == CREATE_AUDIO_PATCH) {
1673 handle = ((CreateAudioPatchData *)command->mParam.get())->mHandle;
Haynes Mathew Georgea2d4a6d2014-10-13 13:05:22 -07001674 patch = ((CreateAudioPatchData *)command->mParam.get())->mPatch;
Eric Laurente45b48a2014-09-04 16:40:57 -07001675 } else {
1676 handle = ((ReleaseAudioPatchData *)command->mParam.get())->mHandle;
Mikhail Naganov7be71d22018-05-23 16:51:46 -07001677 memset(&patch, 0, sizeof(patch));
Eric Laurente45b48a2014-09-04 16:40:57 -07001678 }
1679 audio_patch_handle_t handle2;
Haynes Mathew Georgea2d4a6d2014-10-13 13:05:22 -07001680 struct audio_patch patch2;
Eric Laurente45b48a2014-09-04 16:40:57 -07001681 if (command2->mCommand == CREATE_AUDIO_PATCH) {
1682 handle2 = ((CreateAudioPatchData *)command2->mParam.get())->mHandle;
Haynes Mathew Georgea2d4a6d2014-10-13 13:05:22 -07001683 patch2 = ((CreateAudioPatchData *)command2->mParam.get())->mPatch;
Eric Laurente45b48a2014-09-04 16:40:57 -07001684 } else {
1685 handle2 = ((ReleaseAudioPatchData *)command2->mParam.get())->mHandle;
Glenn Kastenf60b6b62015-07-06 10:53:26 -07001686 memset(&patch2, 0, sizeof(patch2));
Eric Laurente45b48a2014-09-04 16:40:57 -07001687 }
1688 if (handle != handle2) break;
Haynes Mathew Georgea2d4a6d2014-10-13 13:05:22 -07001689 /* Filter CREATE_AUDIO_PATCH commands only when they are issued for
1690 same output. */
1691 if( (command->mCommand == CREATE_AUDIO_PATCH) &&
1692 (command2->mCommand == CREATE_AUDIO_PATCH) ) {
1693 bool isOutputDiff = false;
1694 if (patch.num_sources == patch2.num_sources) {
1695 for (unsigned count = 0; count < patch.num_sources; count++) {
1696 if (patch.sources[count].id != patch2.sources[count].id) {
1697 isOutputDiff = true;
1698 break;
1699 }
1700 }
1701 if (isOutputDiff)
1702 break;
1703 }
1704 }
Eric Laurente45b48a2014-09-04 16:40:57 -07001705 ALOGV("Filtering out %s audio patch command for handle %d",
1706 (command->mCommand == CREATE_AUDIO_PATCH) ? "create" : "release", handle);
1707 removedCommands.add(command2);
1708 command->mTime = command2->mTime;
1709 // force delayMs to non 0 so that code below does not request to wait for
1710 // command status as the command is now delayed
1711 delayMs = 1;
1712 } break;
1713
Jean-Michel Trivide801052015-04-14 19:10:14 -07001714 case DYN_POLICY_MIX_STATE_UPDATE: {
1715
1716 } break;
1717
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -08001718 case RECORDING_CONFIGURATION_UPDATE: {
1719
1720 } break;
1721
Mathias Agopian65ab4712010-07-14 17:59:35 -07001722 default:
1723 break;
1724 }
1725 }
1726
1727 // remove filtered commands
1728 for (size_t j = 0; j < removedCommands.size(); j++) {
1729 // removed commands always have time stamps greater than current command
1730 for (size_t k = i + 1; k < mAudioCommands.size(); k++) {
Eric Laurent0ede8922014-05-09 18:04:42 -07001731 if (mAudioCommands[k].get() == removedCommands[j].get()) {
Steve Block3856b092011-10-20 11:56:00 +01001732 ALOGV("suppressing command: %d", mAudioCommands[k]->mCommand);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001733 mAudioCommands.removeAt(k);
1734 break;
1735 }
1736 }
1737 }
1738 removedCommands.clear();
1739
Eric Laurentaa79bef2015-01-15 14:33:51 -08001740 // Disable wait for status if delay is not 0.
1741 // Except for create audio patch command because the returned patch handle
1742 // is needed by audio policy manager
1743 if (delayMs != 0 && command->mCommand != CREATE_AUDIO_PATCH) {
Eric Laurentcec4abb2012-07-03 12:23:02 -07001744 command->mWaitStatus = false;
1745 }
Eric Laurentcec4abb2012-07-03 12:23:02 -07001746
Mathias Agopian65ab4712010-07-14 17:59:35 -07001747 // insert command at the right place according to its time stamp
Eric Laurent1e693b52014-07-09 15:03:28 -07001748 ALOGV("inserting command: %d at index %zd, num commands %zu",
1749 command->mCommand, i+1, mAudioCommands.size());
Mathias Agopian65ab4712010-07-14 17:59:35 -07001750 mAudioCommands.insertAt(command, i + 1);
1751}
1752
1753void AudioPolicyService::AudioCommandThread::exit()
1754{
Steve Block3856b092011-10-20 11:56:00 +01001755 ALOGV("AudioCommandThread::exit");
Mathias Agopian65ab4712010-07-14 17:59:35 -07001756 {
1757 AutoMutex _l(mLock);
1758 requestExit();
1759 mWaitWorkCV.signal();
1760 }
Zach Janga754b4f2015-10-27 01:29:34 +00001761 // Note that we can call it from the thread loop if all other references have been released
1762 // but it will safely return WOULD_BLOCK in this case
Mathias Agopian65ab4712010-07-14 17:59:35 -07001763 requestExitAndWait();
1764}
1765
1766void AudioPolicyService::AudioCommandThread::AudioCommand::dump(char* buffer, size_t size)
1767{
1768 snprintf(buffer, size, " %02d %06d.%03d %01u %p\n",
1769 mCommand,
1770 (int)ns2s(mTime),
1771 (int)ns2ms(mTime)%1000,
1772 mWaitStatus,
Eric Laurent0ede8922014-05-09 18:04:42 -07001773 mParam.get());
Mathias Agopian65ab4712010-07-14 17:59:35 -07001774}
1775
Dima Zavinfce7a472011-04-19 22:30:36 -07001776/******* helpers for the service_ops callbacks defined below *********/
1777void AudioPolicyService::setParameters(audio_io_handle_t ioHandle,
1778 const char *keyValuePairs,
1779 int delayMs)
1780{
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001781 mAudioCommandThread->parametersCommand(ioHandle, keyValuePairs,
Dima Zavinfce7a472011-04-19 22:30:36 -07001782 delayMs);
1783}
1784
1785int AudioPolicyService::setStreamVolume(audio_stream_type_t stream,
1786 float volume,
1787 audio_io_handle_t output,
1788 int delayMs)
1789{
Glenn Kastenfff6d712012-01-12 16:38:12 -08001790 return (int)mAudioCommandThread->volumeCommand(stream, volume,
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001791 output, delayMs);
Dima Zavinfce7a472011-04-19 22:30:36 -07001792}
1793
Dima Zavinfce7a472011-04-19 22:30:36 -07001794int AudioPolicyService::setVoiceVolume(float volume, int delayMs)
1795{
1796 return (int)mAudioCommandThread->voiceVolumeCommand(volume, delayMs);
1797}
1798
Eric Laurentb20cf7d2019-04-05 19:37:34 -07001799void AudioPolicyService::setEffectSuspended(int effectId,
1800 audio_session_t sessionId,
1801 bool suspended)
1802{
1803 mAudioCommandThread->setEffectSuspendedCommand(effectId, sessionId, suspended);
1804}
1805
1806
Dima Zavinfce7a472011-04-19 22:30:36 -07001807extern "C" {
Eric Laurent2d388ec2014-03-07 13:25:54 -08001808audio_module_handle_t aps_load_hw_module(void *service __unused,
1809 const char *name);
1810audio_io_handle_t aps_open_output(void *service __unused,
Eric Laurenta4c5a552012-03-29 10:12:40 -07001811 audio_devices_t *pDevices,
1812 uint32_t *pSamplingRate,
1813 audio_format_t *pFormat,
1814 audio_channel_mask_t *pChannelMask,
1815 uint32_t *pLatencyMs,
Eric Laurent2d388ec2014-03-07 13:25:54 -08001816 audio_output_flags_t flags);
Eric Laurenta4c5a552012-03-29 10:12:40 -07001817
Eric Laurent2d388ec2014-03-07 13:25:54 -08001818audio_io_handle_t aps_open_output_on_module(void *service __unused,
Eric Laurenta4c5a552012-03-29 10:12:40 -07001819 audio_module_handle_t module,
1820 audio_devices_t *pDevices,
1821 uint32_t *pSamplingRate,
1822 audio_format_t *pFormat,
1823 audio_channel_mask_t *pChannelMask,
1824 uint32_t *pLatencyMs,
Richard Fitzgeraldad3af332013-03-25 16:54:37 +00001825 audio_output_flags_t flags,
Eric Laurent2d388ec2014-03-07 13:25:54 -08001826 const audio_offload_info_t *offloadInfo);
1827audio_io_handle_t aps_open_dup_output(void *service __unused,
Dima Zavinfce7a472011-04-19 22:30:36 -07001828 audio_io_handle_t output1,
Eric Laurent2d388ec2014-03-07 13:25:54 -08001829 audio_io_handle_t output2);
1830int aps_close_output(void *service __unused, audio_io_handle_t output);
1831int aps_suspend_output(void *service __unused, audio_io_handle_t output);
1832int aps_restore_output(void *service __unused, audio_io_handle_t output);
1833audio_io_handle_t aps_open_input(void *service __unused,
Eric Laurenta4c5a552012-03-29 10:12:40 -07001834 audio_devices_t *pDevices,
1835 uint32_t *pSamplingRate,
1836 audio_format_t *pFormat,
1837 audio_channel_mask_t *pChannelMask,
Eric Laurent2d388ec2014-03-07 13:25:54 -08001838 audio_in_acoustics_t acoustics __unused);
1839audio_io_handle_t aps_open_input_on_module(void *service __unused,
Eric Laurenta4c5a552012-03-29 10:12:40 -07001840 audio_module_handle_t module,
1841 audio_devices_t *pDevices,
1842 uint32_t *pSamplingRate,
1843 audio_format_t *pFormat,
Eric Laurent2d388ec2014-03-07 13:25:54 -08001844 audio_channel_mask_t *pChannelMask);
1845int aps_close_input(void *service __unused, audio_io_handle_t input);
1846int aps_invalidate_stream(void *service __unused, audio_stream_type_t stream);
Glenn Kastend848eb42016-03-08 13:42:11 -08001847int aps_move_effects(void *service __unused, audio_session_t session,
Dima Zavinfce7a472011-04-19 22:30:36 -07001848 audio_io_handle_t src_output,
Eric Laurent2d388ec2014-03-07 13:25:54 -08001849 audio_io_handle_t dst_output);
1850char * aps_get_parameters(void *service __unused, audio_io_handle_t io_handle,
1851 const char *keys);
1852void aps_set_parameters(void *service, audio_io_handle_t io_handle,
1853 const char *kv_pairs, int delay_ms);
1854int aps_set_stream_volume(void *service, audio_stream_type_t stream,
Dima Zavinfce7a472011-04-19 22:30:36 -07001855 float volume, audio_io_handle_t output,
Eric Laurent2d388ec2014-03-07 13:25:54 -08001856 int delay_ms);
Eric Laurent2d388ec2014-03-07 13:25:54 -08001857int aps_set_voice_volume(void *service, float volume, int delay_ms);
1858};
Dima Zavinfce7a472011-04-19 22:30:36 -07001859
Mikhail Naganov1b2a7942017-12-08 10:18:09 -08001860} // namespace android