blob: 692f612bcfaee577e1ddf2616dc125208bdb1054 [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/ActivityManager.h>
33#include <binder/PermissionController.h>
34#include <binder/IResultReceiver.h>
Mathias Agopian65ab4712010-07-14 17:59:35 -070035#include <utils/String16.h>
36#include <utils/threads.h>
37#include "AudioPolicyService.h"
Mathias Agopian65ab4712010-07-14 17:59:35 -070038#include <hardware_legacy/power.h>
Eric Laurent7c7f10b2011-06-17 21:29:58 -070039#include <media/AudioEffect.h>
Chih-Hung Hsiehc84d9d22014-11-14 13:33:34 -080040#include <media/AudioParameter.h>
Andy Hungab7ef302018-05-15 19:35:29 -070041#include <mediautils/ServiceUtilities.h>
Michael Groovercfd28302018-12-11 19:16:46 -080042#include <sensorprivacy/SensorPrivacyManager.h>
Mathias Agopian65ab4712010-07-14 17:59:35 -070043
Dima Zavin64760242011-05-11 14:15:23 -070044#include <system/audio.h>
Dima Zavin7394a4f2011-06-13 18:16:26 -070045#include <system/audio_policy.h>
Mikhail Naganov61a4fac2016-10-13 14:44:18 -070046
Mathias Agopian65ab4712010-07-14 17:59:35 -070047namespace android {
48
Glenn Kasten8dad0e32012-01-09 08:41:22 -080049static const char kDeadlockedString[] = "AudioPolicyService may be deadlocked\n";
50static const char kCmdDeadlockedString[] = "AudioPolicyService command thread may be deadlocked\n";
Mathias Agopian65ab4712010-07-14 17:59:35 -070051
Mikhail Naganov959e2d02019-03-28 11:08:19 -070052static const int kDumpLockTimeoutNs = 1 * NANOS_PER_SECOND;
Mathias Agopian65ab4712010-07-14 17:59:35 -070053
Eric Laurent0ede8922014-05-09 18:04:42 -070054static const nsecs_t kAudioCommandTimeoutNs = seconds(3); // 3 seconds
Christer Fletcher5fa8c4b2013-01-18 15:27:03 +010055
Svet Ganovf4ddfef2018-01-16 07:37:58 -080056static const String16 sManageAudioPolicyPermission("android.permission.MANAGE_AUDIO_POLICY");
Dima Zavinfce7a472011-04-19 22:30:36 -070057
Mathias Agopian65ab4712010-07-14 17:59:35 -070058// ----------------------------------------------------------------------------
59
60AudioPolicyService::AudioPolicyService()
Eric Laurentdce54a12014-03-10 12:19:46 -070061 : BnAudioPolicyService(), mpAudioPolicyDev(NULL), mpAudioPolicy(NULL),
Eric Laurentbb6c9a02014-09-25 14:11:47 -070062 mAudioPolicyManager(NULL), mAudioPolicyClient(NULL), mPhoneState(AUDIO_MODE_INVALID)
Mathias Agopian65ab4712010-07-14 17:59:35 -070063{
Eric Laurentf5ada6e2014-10-09 17:49:00 -070064}
65
66void AudioPolicyService::onFirstRef()
67{
Eric Laurent8b1e80b2014-10-07 09:08:47 -070068 {
69 Mutex::Autolock _l(mLock);
Eric Laurent93575202011-01-18 18:39:02 -080070
Eric Laurent8b1e80b2014-10-07 09:08:47 -070071 // start audio commands thread
72 mAudioCommandThread = new AudioCommandThread(String8("ApmAudio"), this);
73 // start output activity command thread
74 mOutputCommandThread = new AudioCommandThread(String8("ApmOutput"), this);
Eric Laurentdce54a12014-03-10 12:19:46 -070075
Eric Laurent8b1e80b2014-10-07 09:08:47 -070076 mAudioPolicyClient = new AudioPolicyClient(this);
77 mAudioPolicyManager = createAudioPolicyManager(mAudioPolicyClient);
Eric Laurent8b1e80b2014-10-07 09:08:47 -070078 }
bryant_liuba2b4392014-06-11 16:49:30 +080079 // load audio processing modules
Eric Laurent8b1e80b2014-10-07 09:08:47 -070080 sp<AudioPolicyEffects>audioPolicyEffects = new AudioPolicyEffects();
81 {
82 Mutex::Autolock _l(mLock);
83 mAudioPolicyEffects = audioPolicyEffects;
84 }
Svet Ganovf4ddfef2018-01-16 07:37:58 -080085
86 mUidPolicy = new UidPolicy(this);
87 mUidPolicy->registerSelf();
Michael Groovercfd28302018-12-11 19:16:46 -080088
89 mSensorPrivacyPolicy = new SensorPrivacyPolicy(this);
90 mSensorPrivacyPolicy->registerSelf();
Mathias Agopian65ab4712010-07-14 17:59:35 -070091}
92
93AudioPolicyService::~AudioPolicyService()
94{
Mathias Agopian65ab4712010-07-14 17:59:35 -070095 mAudioCommandThread->exit();
Eric Laurent657ff612014-05-07 11:58:24 -070096 mOutputCommandThread->exit();
Eric Laurent7c7f10b2011-06-17 21:29:58 -070097
Eric Laurentf269b8e2014-06-09 20:01:29 -070098 destroyAudioPolicyManager(mAudioPolicyManager);
Eric Laurentdce54a12014-03-10 12:19:46 -070099 delete mAudioPolicyClient;
Eric Laurentb52c1522014-05-20 11:27:36 -0700100
101 mNotificationClients.clear();
bryant_liuba2b4392014-06-11 16:49:30 +0800102 mAudioPolicyEffects.clear();
Svet Ganovf4ddfef2018-01-16 07:37:58 -0800103
104 mUidPolicy->unregisterSelf();
105 mUidPolicy.clear();
Michael Groovercfd28302018-12-11 19:16:46 -0800106
107 mSensorPrivacyPolicy->unregisterSelf();
108 mSensorPrivacyPolicy.clear();
Eric Laurentb52c1522014-05-20 11:27:36 -0700109}
110
111// A notification client is always registered by AudioSystem when the client process
112// connects to AudioPolicyService.
113void AudioPolicyService::registerClient(const sp<IAudioPolicyServiceClient>& client)
114{
Eric Laurent12590252015-08-21 18:40:20 -0700115 if (client == 0) {
116 ALOGW("%s got NULL client", __FUNCTION__);
117 return;
118 }
Eric Laurent0ebd5f92014-11-19 19:04:52 -0800119 Mutex::Autolock _l(mNotificationClientsLock);
Eric Laurentb52c1522014-05-20 11:27:36 -0700120
121 uid_t uid = IPCThreadState::self()->getCallingUid();
luochaojiang908c7d72018-06-21 14:58:04 +0800122 pid_t pid = IPCThreadState::self()->getCallingPid();
123 int64_t token = ((int64_t)uid<<32) | pid;
124
125 if (mNotificationClients.indexOfKey(token) < 0) {
Eric Laurentb52c1522014-05-20 11:27:36 -0700126 sp<NotificationClient> notificationClient = new NotificationClient(this,
127 client,
luochaojiang908c7d72018-06-21 14:58:04 +0800128 uid,
129 pid);
130 ALOGV("registerClient() client %p, uid %d pid %d", client.get(), uid, pid);
Eric Laurentb52c1522014-05-20 11:27:36 -0700131
luochaojiang908c7d72018-06-21 14:58:04 +0800132 mNotificationClients.add(token, notificationClient);
Eric Laurentb52c1522014-05-20 11:27:36 -0700133
Marco Nelissenf8880202014-11-14 07:58:25 -0800134 sp<IBinder> binder = IInterface::asBinder(client);
Eric Laurentb52c1522014-05-20 11:27:36 -0700135 binder->linkToDeath(notificationClient);
136 }
137}
138
Eric Laurente8726fe2015-06-26 09:39:24 -0700139void AudioPolicyService::setAudioPortCallbacksEnabled(bool enabled)
140{
141 Mutex::Autolock _l(mNotificationClientsLock);
142
143 uid_t uid = IPCThreadState::self()->getCallingUid();
luochaojiang908c7d72018-06-21 14:58:04 +0800144 pid_t pid = IPCThreadState::self()->getCallingPid();
145 int64_t token = ((int64_t)uid<<32) | pid;
146
147 if (mNotificationClients.indexOfKey(token) < 0) {
Eric Laurente8726fe2015-06-26 09:39:24 -0700148 return;
149 }
luochaojiang908c7d72018-06-21 14:58:04 +0800150 mNotificationClients.valueFor(token)->setAudioPortCallbacksEnabled(enabled);
Eric Laurente8726fe2015-06-26 09:39:24 -0700151}
152
François Gaffiecfe17322018-11-07 13:41:29 +0100153void AudioPolicyService::setAudioVolumeGroupCallbacksEnabled(bool enabled)
154{
155 Mutex::Autolock _l(mNotificationClientsLock);
156
157 uid_t uid = IPCThreadState::self()->getCallingUid();
158 pid_t pid = IPCThreadState::self()->getCallingPid();
159 int64_t token = ((int64_t)uid<<32) | pid;
160
161 if (mNotificationClients.indexOfKey(token) < 0) {
162 return;
163 }
164 mNotificationClients.valueFor(token)->setAudioVolumeGroupCallbacksEnabled(enabled);
165}
166
Eric Laurentb52c1522014-05-20 11:27:36 -0700167// removeNotificationClient() is called when the client process dies.
luochaojiang908c7d72018-06-21 14:58:04 +0800168void AudioPolicyService::removeNotificationClient(uid_t uid, pid_t pid)
Eric Laurentb52c1522014-05-20 11:27:36 -0700169{
Eric Laurent0ebd5f92014-11-19 19:04:52 -0800170 {
171 Mutex::Autolock _l(mNotificationClientsLock);
luochaojiang908c7d72018-06-21 14:58:04 +0800172 int64_t token = ((int64_t)uid<<32) | pid;
173 mNotificationClients.removeItem(token);
Eric Laurent0ebd5f92014-11-19 19:04:52 -0800174 }
Eric Laurent0ebd5f92014-11-19 19:04:52 -0800175 {
176 Mutex::Autolock _l(mLock);
luochaojiang908c7d72018-06-21 14:58:04 +0800177 bool hasSameUid = false;
178 for (size_t i = 0; i < mNotificationClients.size(); i++) {
179 if (mNotificationClients.valueAt(i)->uid() == uid) {
180 hasSameUid = true;
181 break;
182 }
183 }
184 if (mAudioPolicyManager && !hasSameUid) {
Eric Laurent10b71232018-04-13 18:14:44 -0700185 // called from binder death notification: no need to clear caller identity
Eric Laurent8c7e6da2015-04-21 17:37:00 -0700186 mAudioPolicyManager->releaseResourcesForUid(uid);
Eric Laurentb52c1522014-05-20 11:27:36 -0700187 }
Eric Laurent0ebd5f92014-11-19 19:04:52 -0800188 }
Eric Laurentb52c1522014-05-20 11:27:36 -0700189}
190
191void AudioPolicyService::onAudioPortListUpdate()
192{
193 mOutputCommandThread->updateAudioPortListCommand();
194}
195
196void AudioPolicyService::doOnAudioPortListUpdate()
197{
Eric Laurent0ebd5f92014-11-19 19:04:52 -0800198 Mutex::Autolock _l(mNotificationClientsLock);
Eric Laurentb52c1522014-05-20 11:27:36 -0700199 for (size_t i = 0; i < mNotificationClients.size(); i++) {
200 mNotificationClients.valueAt(i)->onAudioPortListUpdate();
201 }
202}
203
204void AudioPolicyService::onAudioPatchListUpdate()
205{
206 mOutputCommandThread->updateAudioPatchListCommand();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700207}
208
Eric Laurentb52c1522014-05-20 11:27:36 -0700209void AudioPolicyService::doOnAudioPatchListUpdate()
210{
Eric Laurent0ebd5f92014-11-19 19:04:52 -0800211 Mutex::Autolock _l(mNotificationClientsLock);
Eric Laurentb52c1522014-05-20 11:27:36 -0700212 for (size_t i = 0; i < mNotificationClients.size(); i++) {
213 mNotificationClients.valueAt(i)->onAudioPatchListUpdate();
214 }
215}
216
François Gaffiecfe17322018-11-07 13:41:29 +0100217void AudioPolicyService::onAudioVolumeGroupChanged(volume_group_t group, int flags)
218{
219 mOutputCommandThread->changeAudioVolumeGroupCommand(group, flags);
220}
221
222void AudioPolicyService::doOnAudioVolumeGroupChanged(volume_group_t group, int flags)
223{
224 Mutex::Autolock _l(mNotificationClientsLock);
225 for (size_t i = 0; i < mNotificationClients.size(); i++) {
226 mNotificationClients.valueAt(i)->onAudioVolumeGroupChanged(group, flags);
227 }
228}
229
Chih-Hung Hsiehe964d4e2016-08-09 14:31:32 -0700230void AudioPolicyService::onDynamicPolicyMixStateUpdate(const String8& regId, int32_t state)
Jean-Michel Trivide801052015-04-14 19:10:14 -0700231{
232 ALOGV("AudioPolicyService::onDynamicPolicyMixStateUpdate(%s, %d)",
233 regId.string(), state);
234 mOutputCommandThread->dynamicPolicyMixStateUpdateCommand(regId, state);
235}
236
Chih-Hung Hsiehe964d4e2016-08-09 14:31:32 -0700237void AudioPolicyService::doOnDynamicPolicyMixStateUpdate(const String8& regId, int32_t state)
Jean-Michel Trivide801052015-04-14 19:10:14 -0700238{
239 Mutex::Autolock _l(mNotificationClientsLock);
240 for (size_t i = 0; i < mNotificationClients.size(); i++) {
241 mNotificationClients.valueAt(i)->onDynamicPolicyMixStateUpdate(regId, state);
242 }
243}
244
Eric Laurenta9f86652018-11-28 17:23:11 -0800245void AudioPolicyService::onRecordingConfigurationUpdate(
246 int event,
247 const record_client_info_t *clientInfo,
248 const audio_config_base_t *clientConfig,
249 std::vector<effect_descriptor_t> clientEffects,
250 const audio_config_base_t *deviceConfig,
251 std::vector<effect_descriptor_t> effects,
252 audio_patch_handle_t patchHandle,
253 audio_source_t source)
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -0800254{
Jean-Michel Triviac4e4292016-12-22 11:39:31 -0800255 mOutputCommandThread->recordingConfigurationUpdateCommand(event, clientInfo,
Eric Laurenta9f86652018-11-28 17:23:11 -0800256 clientConfig, clientEffects, deviceConfig, effects, patchHandle, source);
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -0800257}
258
Eric Laurenta9f86652018-11-28 17:23:11 -0800259void AudioPolicyService::doOnRecordingConfigurationUpdate(
260 int event,
261 const record_client_info_t *clientInfo,
262 const audio_config_base_t *clientConfig,
263 std::vector<effect_descriptor_t> clientEffects,
264 const audio_config_base_t *deviceConfig,
265 std::vector<effect_descriptor_t> effects,
266 audio_patch_handle_t patchHandle,
267 audio_source_t source)
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -0800268{
269 Mutex::Autolock _l(mNotificationClientsLock);
270 for (size_t i = 0; i < mNotificationClients.size(); i++) {
Jean-Michel Triviac4e4292016-12-22 11:39:31 -0800271 mNotificationClients.valueAt(i)->onRecordingConfigurationUpdate(event, clientInfo,
Eric Laurenta9f86652018-11-28 17:23:11 -0800272 clientConfig, clientEffects, deviceConfig, effects, patchHandle, source);
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -0800273 }
274}
275
276status_t AudioPolicyService::clientCreateAudioPatch(const struct audio_patch *patch,
277 audio_patch_handle_t *handle,
278 int delayMs)
279{
280 return mAudioCommandThread->createAudioPatchCommand(patch, handle, delayMs);
281}
282
283status_t AudioPolicyService::clientReleaseAudioPatch(audio_patch_handle_t handle,
284 int delayMs)
285{
286 return mAudioCommandThread->releaseAudioPatchCommand(handle, delayMs);
287}
288
Eric Laurente1715a42014-05-20 11:30:42 -0700289status_t AudioPolicyService::clientSetAudioPortConfig(const struct audio_port_config *config,
290 int delayMs)
291{
292 return mAudioCommandThread->setAudioPortConfigCommand(config, delayMs);
293}
294
Eric Laurentb52c1522014-05-20 11:27:36 -0700295AudioPolicyService::NotificationClient::NotificationClient(const sp<AudioPolicyService>& service,
296 const sp<IAudioPolicyServiceClient>& client,
luochaojiang908c7d72018-06-21 14:58:04 +0800297 uid_t uid,
298 pid_t pid)
299 : mService(service), mUid(uid), mPid(pid), mAudioPolicyServiceClient(client),
François Gaffiecfe17322018-11-07 13:41:29 +0100300 mAudioPortCallbacksEnabled(false), mAudioVolumeGroupCallbacksEnabled(false)
Eric Laurentb52c1522014-05-20 11:27:36 -0700301{
302}
303
304AudioPolicyService::NotificationClient::~NotificationClient()
305{
306}
307
308void AudioPolicyService::NotificationClient::binderDied(const wp<IBinder>& who __unused)
309{
310 sp<NotificationClient> keep(this);
311 sp<AudioPolicyService> service = mService.promote();
312 if (service != 0) {
luochaojiang908c7d72018-06-21 14:58:04 +0800313 service->removeNotificationClient(mUid, mPid);
Eric Laurentb52c1522014-05-20 11:27:36 -0700314 }
315}
316
317void AudioPolicyService::NotificationClient::onAudioPortListUpdate()
318{
Eric Laurente8726fe2015-06-26 09:39:24 -0700319 if (mAudioPolicyServiceClient != 0 && mAudioPortCallbacksEnabled) {
Eric Laurentb52c1522014-05-20 11:27:36 -0700320 mAudioPolicyServiceClient->onAudioPortListUpdate();
321 }
322}
323
324void AudioPolicyService::NotificationClient::onAudioPatchListUpdate()
325{
Eric Laurente8726fe2015-06-26 09:39:24 -0700326 if (mAudioPolicyServiceClient != 0 && mAudioPortCallbacksEnabled) {
Eric Laurentb52c1522014-05-20 11:27:36 -0700327 mAudioPolicyServiceClient->onAudioPatchListUpdate();
328 }
329}
Eric Laurent57dae992011-07-24 13:36:09 -0700330
François Gaffiecfe17322018-11-07 13:41:29 +0100331void AudioPolicyService::NotificationClient::onAudioVolumeGroupChanged(volume_group_t group,
332 int flags)
333{
334 if (mAudioPolicyServiceClient != 0 && mAudioVolumeGroupCallbacksEnabled) {
335 mAudioPolicyServiceClient->onAudioVolumeGroupChanged(group, flags);
336 }
337}
338
339
Jean-Michel Trivide801052015-04-14 19:10:14 -0700340void AudioPolicyService::NotificationClient::onDynamicPolicyMixStateUpdate(
Chih-Hung Hsiehe964d4e2016-08-09 14:31:32 -0700341 const String8& regId, int32_t state)
Jean-Michel Trivide801052015-04-14 19:10:14 -0700342{
Andy Hung4ef19fa2018-05-15 19:35:29 -0700343 if (mAudioPolicyServiceClient != 0 && isServiceUid(mUid)) {
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -0800344 mAudioPolicyServiceClient->onDynamicPolicyMixStateUpdate(regId, state);
345 }
346}
347
348void AudioPolicyService::NotificationClient::onRecordingConfigurationUpdate(
Eric Laurenta9f86652018-11-28 17:23:11 -0800349 int event,
350 const record_client_info_t *clientInfo,
351 const audio_config_base_t *clientConfig,
352 std::vector<effect_descriptor_t> clientEffects,
353 const audio_config_base_t *deviceConfig,
354 std::vector<effect_descriptor_t> effects,
355 audio_patch_handle_t patchHandle,
356 audio_source_t source)
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -0800357{
Andy Hung4ef19fa2018-05-15 19:35:29 -0700358 if (mAudioPolicyServiceClient != 0 && isServiceUid(mUid)) {
Jean-Michel Triviac4e4292016-12-22 11:39:31 -0800359 mAudioPolicyServiceClient->onRecordingConfigurationUpdate(event, clientInfo,
Eric Laurenta9f86652018-11-28 17:23:11 -0800360 clientConfig, clientEffects, deviceConfig, effects, patchHandle, source);
Jean-Michel Trivide801052015-04-14 19:10:14 -0700361 }
362}
363
Eric Laurente8726fe2015-06-26 09:39:24 -0700364void AudioPolicyService::NotificationClient::setAudioPortCallbacksEnabled(bool enabled)
365{
366 mAudioPortCallbacksEnabled = enabled;
367}
368
François Gaffiecfe17322018-11-07 13:41:29 +0100369void AudioPolicyService::NotificationClient::setAudioVolumeGroupCallbacksEnabled(bool enabled)
370{
371 mAudioVolumeGroupCallbacksEnabled = enabled;
372}
Eric Laurente8726fe2015-06-26 09:39:24 -0700373
Mathias Agopian65ab4712010-07-14 17:59:35 -0700374void AudioPolicyService::binderDied(const wp<IBinder>& who) {
Glenn Kasten411e4472012-11-02 10:00:06 -0700375 ALOGW("binderDied() %p, calling pid %d", who.unsafe_get(),
Eric Laurentde070132010-07-13 04:45:46 -0700376 IPCThreadState::self()->getCallingPid());
Mathias Agopian65ab4712010-07-14 17:59:35 -0700377}
378
Mikhail Naganov959e2d02019-03-28 11:08:19 -0700379static bool dumpTryLock(Mutex& mutex)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700380{
Mikhail Naganov959e2d02019-03-28 11:08:19 -0700381 status_t err = mutex.timedLock(kDumpLockTimeoutNs);
382 return err == NO_ERROR;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700383}
384
385status_t AudioPolicyService::dumpInternals(int fd)
386{
387 const size_t SIZE = 256;
388 char buffer[SIZE];
389 String8 result;
390
Eric Laurentdce54a12014-03-10 12:19:46 -0700391 snprintf(buffer, SIZE, "AudioPolicyManager: %p\n", mAudioPolicyManager);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700392 result.append(buffer);
393 snprintf(buffer, SIZE, "Command Thread: %p\n", mAudioCommandThread.get());
394 result.append(buffer);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700395
396 write(fd, result.string(), result.size());
397 return NO_ERROR;
398}
399
Eric Laurente8c8b432018-10-17 10:08:02 -0700400void AudioPolicyService::updateUidStates()
Svet Ganovf4ddfef2018-01-16 07:37:58 -0800401{
Eric Laurente8c8b432018-10-17 10:08:02 -0700402 Mutex::Autolock _l(mLock);
403 updateUidStates_l();
404}
405
406void AudioPolicyService::updateUidStates_l()
407{
Eric Laurent4eb58f12018-12-07 16:41:02 -0800408// Go over all active clients and allow capture (does not force silence) in the
409// following cases:
Eric Laurent1ff16a72019-03-14 18:35:04 -0700410// Another client in the same UID has already been allowed to capture
411// OR The client is the assistant
Eric Laurent6ede98f2019-06-11 14:50:30 -0700412// AND an accessibility service is on TOP or a RTT call is active
Eric Laurenta46bedb2018-12-07 18:01:26 -0800413// AND the source is VOICE_RECOGNITION or HOTWORD
Eric Laurenta171e352019-05-07 13:04:45 -0700414// OR uses VOICE_RECOGNITION AND is on TOP
Eric Laurenta46bedb2018-12-07 18:01:26 -0800415// OR uses HOTWORD
Eric Laurent1ff16a72019-03-14 18:35:04 -0700416// AND there is no active privacy sensitive capture or call
417// OR client has CAPTURE_AUDIO_OUTPUT privileged permission
Eric Laurenta46bedb2018-12-07 18:01:26 -0800418// OR The client is an accessibility service
Eric Laurenta171e352019-05-07 13:04:45 -0700419// AND is on TOP
Eric Laurenta46bedb2018-12-07 18:01:26 -0800420// AND the source is VOICE_RECOGNITION or HOTWORD
Eric Laurent1ff16a72019-03-14 18:35:04 -0700421// OR the client source is virtual (remote submix, call audio TX or RX...)
Eric Laurenta171e352019-05-07 13:04:45 -0700422// OR Any client
Eric Laurenta46bedb2018-12-07 18:01:26 -0800423// AND The assistant is not on TOP
Eric Laurenta171e352019-05-07 13:04:45 -0700424// AND is on TOP or latest started
Eric Laurent1ff16a72019-03-14 18:35:04 -0700425// AND there is no active privacy sensitive capture or call
426// OR client has CAPTURE_AUDIO_OUTPUT privileged permission
Eric Laurent4eb58f12018-12-07 16:41:02 -0800427
428 sp<AudioRecordClient> topActive;
429 sp<AudioRecordClient> latestActive;
Eric Laurent4eb58f12018-12-07 16:41:02 -0800430 sp<AudioRecordClient> latestSensitiveActive;
Eric Laurent1ff16a72019-03-14 18:35:04 -0700431
Eric Laurenta46bedb2018-12-07 18:01:26 -0800432 nsecs_t topStartNs = 0;
433 nsecs_t latestStartNs = 0;
Eric Laurent4eb58f12018-12-07 16:41:02 -0800434 nsecs_t latestSensitiveStartNs = 0;
435 bool isA11yOnTop = mUidPolicy->isA11yOnTop();
436 bool isAssistantOnTop = false;
437 bool isSensitiveActive = false;
Eric Laurent1ff16a72019-03-14 18:35:04 -0700438 bool isInCall = mPhoneState == AUDIO_MODE_IN_CALL;
Eric Laurent6ede98f2019-06-11 14:50:30 -0700439 bool rttCallActive =
440 (mPhoneState == AUDIO_MODE_IN_CALL || mPhoneState == AUDIO_MODE_IN_COMMUNICATION)
441 && mUidPolicy->isRttEnabled();
Eric Laurent4eb58f12018-12-07 16:41:02 -0800442
Michael Groovercfd28302018-12-11 19:16:46 -0800443 // if Sensor Privacy is enabled then all recordings should be silenced.
444 if (mSensorPrivacyPolicy->isSensorPrivacyEnabled()) {
445 silenceAllRecordings_l();
446 return;
447 }
448
Eric Laurente8c8b432018-10-17 10:08:02 -0700449 for (size_t i =0; i < mAudioRecordClients.size(); i++) {
450 sp<AudioRecordClient> current = mAudioRecordClients[i];
Eric Laurent1ff16a72019-03-14 18:35:04 -0700451 if (!current->active) {
452 continue;
Eric Laurent4eb58f12018-12-07 16:41:02 -0800453 }
Eric Laurent1ff16a72019-03-14 18:35:04 -0700454
455 app_state_t appState = apmStatFromAmState(mUidPolicy->getUidState(current->uid));
456 // clients which app is in IDLE state are not eligible for top active or
457 // latest active
458 if (appState == APP_STATE_IDLE) {
459 continue;
460 }
461
Eric Laurentc6af90d2019-07-19 10:31:39 -0700462 bool isAssistant = mUidPolicy->isAssistantUid(current->uid);
Eric Laurent1ff16a72019-03-14 18:35:04 -0700463 if (appState == APP_STATE_TOP) {
Eric Laurenta46bedb2018-12-07 18:01:26 -0800464 if (current->startTimeNs > topStartNs) {
465 topActive = current;
466 topStartNs = current->startTimeNs;
467 }
Eric Laurentc6af90d2019-07-19 10:31:39 -0700468 if (isAssistant) {
Eric Laurent4eb58f12018-12-07 16:41:02 -0800469 isAssistantOnTop = true;
470 }
471 }
Eric Laurentc6af90d2019-07-19 10:31:39 -0700472 // Assistant capturing for HOTWORD not considered for latest active to avoid
473 // masking regular clients started before
474 if (current->startTimeNs > latestStartNs &&
475 !(current->attributes.source == AUDIO_SOURCE_HOTWORD && isAssistant)) {
Eric Laurent4eb58f12018-12-07 16:41:02 -0800476 latestActive = current;
477 latestStartNs = current->startTimeNs;
478 }
Eric Laurent1ff16a72019-03-14 18:35:04 -0700479 if (isPrivacySensitiveSource(current->attributes.source)) {
480 if (current->startTimeNs > latestSensitiveStartNs) {
481 latestSensitiveActive = current;
482 latestSensitiveStartNs = current->startTimeNs;
483 }
484 isSensitiveActive = true;
485 }
Eric Laurent4eb58f12018-12-07 16:41:02 -0800486 }
487
Eric Laurent1ff16a72019-03-14 18:35:04 -0700488 // if no active client with UI on Top, consider latest active as top
489 if (topActive == nullptr) {
490 topActive = latestActive;
Eric Laurent4eb58f12018-12-07 16:41:02 -0800491 }
492
Eric Laurent1ff16a72019-03-14 18:35:04 -0700493 std::vector<uid_t> enabledUids;
Eric Laurenta46bedb2018-12-07 18:01:26 -0800494
Eric Laurent4eb58f12018-12-07 16:41:02 -0800495 for (size_t i =0; i < mAudioRecordClients.size(); i++) {
496 sp<AudioRecordClient> current = mAudioRecordClients[i];
Eric Laurent1ff16a72019-03-14 18:35:04 -0700497 if (!current->active) {
498 continue;
499 }
500
501 // keep capture allowed if another client with the same UID has already
502 // been allowed to capture
503 if (std::find(enabledUids.begin(), enabledUids.end(), current->uid)
504 != enabledUids.end()) {
505 continue;
506 }
Eric Laurent4eb58f12018-12-07 16:41:02 -0800507
508 audio_source_t source = current->attributes.source;
Eric Laurent1ff16a72019-03-14 18:35:04 -0700509 bool isTopOrLatestActive = topActive == nullptr ? false : current->uid == topActive->uid;
510 bool isLatestSensitive = latestSensitiveActive == nullptr ?
511 false : current->uid == latestSensitiveActive->uid;
512
513 // By default allow capture if:
514 // The assistant is not on TOP
Eric Laurenta171e352019-05-07 13:04:45 -0700515 // AND is on TOP or latest started
Eric Laurent1ff16a72019-03-14 18:35:04 -0700516 // AND there is no active privacy sensitive capture or call
517 // OR client has CAPTURE_AUDIO_OUTPUT privileged permission
518 bool allowCapture = !isAssistantOnTop
Eric Laurenta171e352019-05-07 13:04:45 -0700519 && ((isTopOrLatestActive && !isLatestSensitive) || isLatestSensitive)
Eric Laurent1ff16a72019-03-14 18:35:04 -0700520 && !(isSensitiveActive && !(isLatestSensitive || current->canCaptureOutput))
521 && !(isInCall && !current->canCaptureOutput);
Eric Laurent2dc962b2019-03-01 08:25:25 -0800522
523 if (isVirtualSource(source)) {
Eric Laurent1ff16a72019-03-14 18:35:04 -0700524 // Allow capture for virtual (remote submix, call audio TX or RX...) sources
525 allowCapture = true;
Eric Laurent2dc962b2019-03-01 08:25:25 -0800526 } else if (mUidPolicy->isAssistantUid(current->uid)) {
Eric Laurent1ff16a72019-03-14 18:35:04 -0700527 // For assistant allow capture if:
Eric Laurent6ede98f2019-06-11 14:50:30 -0700528 // An accessibility service is on TOP or a RTT call is active
Eric Laurent1ff16a72019-03-14 18:35:04 -0700529 // AND the source is VOICE_RECOGNITION or HOTWORD
Eric Laurenta171e352019-05-07 13:04:45 -0700530 // OR is on TOP AND uses VOICE_RECOGNITION
Eric Laurent1ff16a72019-03-14 18:35:04 -0700531 // OR uses HOTWORD
532 // AND there is no active privacy sensitive capture or call
533 // OR client has CAPTURE_AUDIO_OUTPUT privileged permission
Eric Laurent6ede98f2019-06-11 14:50:30 -0700534 if (isA11yOnTop || rttCallActive) {
Eric Laurent4eb58f12018-12-07 16:41:02 -0800535 if (source == AUDIO_SOURCE_HOTWORD || source == AUDIO_SOURCE_VOICE_RECOGNITION) {
Eric Laurent1ff16a72019-03-14 18:35:04 -0700536 allowCapture = true;
Eric Laurent4eb58f12018-12-07 16:41:02 -0800537 }
538 } else {
Eric Laurenta171e352019-05-07 13:04:45 -0700539 if (((isAssistantOnTop && source == AUDIO_SOURCE_VOICE_RECOGNITION) ||
Eric Laurent1ff16a72019-03-14 18:35:04 -0700540 source == AUDIO_SOURCE_HOTWORD) &&
541 (!(isSensitiveActive || isInCall) || current->canCaptureOutput)) {
542 allowCapture = true;
Eric Laurent4eb58f12018-12-07 16:41:02 -0800543 }
544 }
545 } else if (mUidPolicy->isA11yUid(current->uid)) {
Eric Laurent1ff16a72019-03-14 18:35:04 -0700546 // For accessibility service allow capture if:
Eric Laurenta171e352019-05-07 13:04:45 -0700547 // Is on TOP
Eric Laurent1ff16a72019-03-14 18:35:04 -0700548 // AND the source is VOICE_RECOGNITION or HOTWORD
Eric Laurenta171e352019-05-07 13:04:45 -0700549 if (isA11yOnTop &&
Eric Laurent1ff16a72019-03-14 18:35:04 -0700550 (source == AUDIO_SOURCE_VOICE_RECOGNITION || source == AUDIO_SOURCE_HOTWORD)) {
551 allowCapture = true;
Eric Laurent4eb58f12018-12-07 16:41:02 -0800552 }
553 }
554 setAppState_l(current->uid,
Eric Laurent1ff16a72019-03-14 18:35:04 -0700555 allowCapture ? apmStatFromAmState(mUidPolicy->getUidState(current->uid)) :
556 APP_STATE_IDLE);
557 if (allowCapture) {
558 enabledUids.push_back(current->uid);
559 }
Eric Laurente8c8b432018-10-17 10:08:02 -0700560 }
561}
562
Michael Groovercfd28302018-12-11 19:16:46 -0800563void AudioPolicyService::silenceAllRecordings_l() {
564 for (size_t i = 0; i < mAudioRecordClients.size(); i++) {
565 sp<AudioRecordClient> current = mAudioRecordClients[i];
Eric Laurent1ff16a72019-03-14 18:35:04 -0700566 if (!isVirtualSource(current->attributes.source)) {
567 setAppState_l(current->uid, APP_STATE_IDLE);
568 }
Michael Groovercfd28302018-12-11 19:16:46 -0800569 }
570}
571
Eric Laurente8c8b432018-10-17 10:08:02 -0700572/* static */
573app_state_t AudioPolicyService::apmStatFromAmState(int amState) {
Eric Laurent1ff16a72019-03-14 18:35:04 -0700574
575 if (amState == ActivityManager::PROCESS_STATE_UNKNOWN) {
Eric Laurente8c8b432018-10-17 10:08:02 -0700576 return APP_STATE_IDLE;
Eric Laurent1ff16a72019-03-14 18:35:04 -0700577 } else if (amState <= ActivityManager::PROCESS_STATE_TOP) {
578 // include persistent services
579 return APP_STATE_TOP;
Eric Laurente8c8b432018-10-17 10:08:02 -0700580 }
581 return APP_STATE_FOREGROUND;
582}
583
Eric Laurent4eb58f12018-12-07 16:41:02 -0800584/* static */
Eric Laurent2dc962b2019-03-01 08:25:25 -0800585bool AudioPolicyService::isPrivacySensitiveSource(audio_source_t source)
586{
587 switch (source) {
588 case AUDIO_SOURCE_CAMCORDER:
589 case AUDIO_SOURCE_VOICE_COMMUNICATION:
590 return true;
591 default:
592 break;
593 }
594 return false;
595}
596
597/* static */
598bool AudioPolicyService::isVirtualSource(audio_source_t source)
Eric Laurent4eb58f12018-12-07 16:41:02 -0800599{
600 switch (source) {
601 case AUDIO_SOURCE_VOICE_UPLINK:
602 case AUDIO_SOURCE_VOICE_DOWNLINK:
603 case AUDIO_SOURCE_VOICE_CALL:
Eric Laurent2dc962b2019-03-01 08:25:25 -0800604 case AUDIO_SOURCE_REMOTE_SUBMIX:
605 case AUDIO_SOURCE_FM_TUNER:
Eric Laurent4eb58f12018-12-07 16:41:02 -0800606 return true;
607 default:
608 break;
609 }
610 return false;
611}
612
Eric Laurente8c8b432018-10-17 10:08:02 -0700613void AudioPolicyService::setAppState_l(uid_t uid, app_state_t state)
614{
615 AutoCallerClear acc;
616
617 if (mAudioPolicyManager) {
618 mAudioPolicyManager->setAppState(uid, state);
Mikhail Naganoveae73eb2018-04-03 16:57:36 -0700619 }
620 sp<IAudioFlinger> af = AudioSystem::get_audio_flinger();
621 if (af) {
Eric Laurentf32108e2018-10-04 17:22:04 -0700622 bool silenced = state == APP_STATE_IDLE;
Mikhail Naganoveae73eb2018-04-03 16:57:36 -0700623 af->setRecordSilenced(uid, silenced);
624 }
Svet Ganovf4ddfef2018-01-16 07:37:58 -0800625}
626
Glenn Kasten0f11b512014-01-31 16:18:54 -0800627status_t AudioPolicyService::dump(int fd, const Vector<String16>& args __unused)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700628{
Glenn Kasten44deb052012-02-05 18:09:08 -0800629 if (!dumpAllowed()) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700630 dumpPermissionDenial(fd);
631 } else {
Mikhail Naganov959e2d02019-03-28 11:08:19 -0700632 bool locked = dumpTryLock(mLock);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700633 if (!locked) {
634 String8 result(kDeadlockedString);
635 write(fd, result.string(), result.size());
636 }
637
638 dumpInternals(fd);
Glenn Kasten9d1f02d2012-02-08 17:47:58 -0800639 if (mAudioCommandThread != 0) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700640 mAudioCommandThread->dump(fd);
641 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700642
Eric Laurentdce54a12014-03-10 12:19:46 -0700643 if (mAudioPolicyManager) {
644 mAudioPolicyManager->dump(fd);
645 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700646
Kevin Rocard8be94972019-02-22 13:26:25 -0800647 mPackageManager.dump(fd);
648
Mathias Agopian65ab4712010-07-14 17:59:35 -0700649 if (locked) mLock.unlock();
650 }
651 return NO_ERROR;
652}
653
654status_t AudioPolicyService::dumpPermissionDenial(int fd)
655{
656 const size_t SIZE = 256;
657 char buffer[SIZE];
658 String8 result;
659 snprintf(buffer, SIZE, "Permission Denial: "
660 "can't dump AudioPolicyService from pid=%d, uid=%d\n",
661 IPCThreadState::self()->getCallingPid(),
662 IPCThreadState::self()->getCallingUid());
663 result.append(buffer);
664 write(fd, result.string(), result.size());
665 return NO_ERROR;
666}
667
668status_t AudioPolicyService::onTransact(
Svet Ganovf4ddfef2018-01-16 07:37:58 -0800669 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags) {
670 switch (code) {
671 case SHELL_COMMAND_TRANSACTION: {
672 int in = data.readFileDescriptor();
673 int out = data.readFileDescriptor();
674 int err = data.readFileDescriptor();
675 int argc = data.readInt32();
676 Vector<String16> args;
677 for (int i = 0; i < argc && data.dataAvail() > 0; i++) {
678 args.add(data.readString16());
679 }
680 sp<IBinder> unusedCallback;
681 sp<IResultReceiver> resultReceiver;
682 status_t status;
683 if ((status = data.readNullableStrongBinder(&unusedCallback)) != NO_ERROR) {
684 return status;
685 }
686 if ((status = data.readNullableStrongBinder(&resultReceiver)) != NO_ERROR) {
687 return status;
688 }
689 status = shellCommand(in, out, err, args);
690 if (resultReceiver != nullptr) {
691 resultReceiver->send(status);
692 }
693 return NO_ERROR;
694 }
695 }
696
Mathias Agopian65ab4712010-07-14 17:59:35 -0700697 return BnAudioPolicyService::onTransact(code, data, reply, flags);
698}
699
Svet Ganovf4ddfef2018-01-16 07:37:58 -0800700// ------------------- Shell command implementation -------------------
701
702// NOTE: This is a remote API - make sure all args are validated
703status_t AudioPolicyService::shellCommand(int in, int out, int err, Vector<String16>& args) {
704 if (!checkCallingPermission(sManageAudioPolicyPermission, nullptr, nullptr)) {
705 return PERMISSION_DENIED;
706 }
707 if (in == BAD_TYPE || out == BAD_TYPE || err == BAD_TYPE) {
708 return BAD_VALUE;
709 }
710 if (args.size() == 3 && args[0] == String16("set-uid-state")) {
711 return handleSetUidState(args, err);
712 } else if (args.size() == 2 && args[0] == String16("reset-uid-state")) {
713 return handleResetUidState(args, err);
714 } else if (args.size() == 2 && args[0] == String16("get-uid-state")) {
715 return handleGetUidState(args, out, err);
716 } else if (args.size() == 1 && args[0] == String16("help")) {
717 printHelp(out);
718 return NO_ERROR;
719 }
720 printHelp(err);
721 return BAD_VALUE;
722}
723
724status_t AudioPolicyService::handleSetUidState(Vector<String16>& args, int err) {
725 PermissionController pc;
726 int uid = pc.getPackageUid(args[1], 0);
727 if (uid <= 0) {
728 ALOGE("Unknown package: '%s'", String8(args[1]).string());
729 dprintf(err, "Unknown package: '%s'\n", String8(args[1]).string());
730 return BAD_VALUE;
731 }
732 bool active = false;
733 if (args[2] == String16("active")) {
734 active = true;
735 } else if ((args[2] != String16("idle"))) {
736 ALOGE("Expected active or idle but got: '%s'", String8(args[2]).string());
737 return BAD_VALUE;
738 }
739 mUidPolicy->addOverrideUid(uid, active);
740 return NO_ERROR;
741}
742
743status_t AudioPolicyService::handleResetUidState(Vector<String16>& args, int err) {
744 PermissionController pc;
745 int uid = pc.getPackageUid(args[1], 0);
746 if (uid < 0) {
747 ALOGE("Unknown package: '%s'", String8(args[1]).string());
748 dprintf(err, "Unknown package: '%s'\n", String8(args[1]).string());
749 return BAD_VALUE;
750 }
751 mUidPolicy->removeOverrideUid(uid);
752 return NO_ERROR;
753}
754
755status_t AudioPolicyService::handleGetUidState(Vector<String16>& args, int out, int err) {
756 PermissionController pc;
757 int uid = pc.getPackageUid(args[1], 0);
758 if (uid < 0) {
759 ALOGE("Unknown package: '%s'", String8(args[1]).string());
760 dprintf(err, "Unknown package: '%s'\n", String8(args[1]).string());
761 return BAD_VALUE;
762 }
763 if (mUidPolicy->isUidActive(uid)) {
764 return dprintf(out, "active\n");
765 } else {
766 return dprintf(out, "idle\n");
767 }
768}
769
770status_t AudioPolicyService::printHelp(int out) {
771 return dprintf(out, "Audio policy service commands:\n"
772 " get-uid-state <PACKAGE> gets the uid state\n"
773 " set-uid-state <PACKAGE> <active|idle> overrides the uid state\n"
774 " reset-uid-state <PACKAGE> clears the uid state override\n"
775 " help print this message\n");
776}
777
778// ----------- AudioPolicyService::UidPolicy implementation ----------
779
780void AudioPolicyService::UidPolicy::registerSelf() {
781 ActivityManager am;
782 am.registerUidObserver(this, ActivityManager::UID_OBSERVER_GONE
783 | ActivityManager::UID_OBSERVER_IDLE
Eric Laurente8c8b432018-10-17 10:08:02 -0700784 | ActivityManager::UID_OBSERVER_ACTIVE
785 | ActivityManager::UID_OBSERVER_PROCSTATE,
Svet Ganovf4ddfef2018-01-16 07:37:58 -0800786 ActivityManager::PROCESS_STATE_UNKNOWN,
787 String16("audioserver"));
Mikhail Naganoveae73eb2018-04-03 16:57:36 -0700788 status_t res = am.linkToDeath(this);
789 if (!res) {
790 Mutex::Autolock _l(mLock);
791 mObserverRegistered = true;
792 } else {
793 ALOGE("UidPolicy::registerSelf linkToDeath failed: %d", res);
Eric Laurent4eb58f12018-12-07 16:41:02 -0800794
Mikhail Naganoveae73eb2018-04-03 16:57:36 -0700795 am.unregisterUidObserver(this);
796 }
Svet Ganovf4ddfef2018-01-16 07:37:58 -0800797}
798
799void AudioPolicyService::UidPolicy::unregisterSelf() {
800 ActivityManager am;
Mikhail Naganoveae73eb2018-04-03 16:57:36 -0700801 am.unlinkToDeath(this);
Svet Ganovf4ddfef2018-01-16 07:37:58 -0800802 am.unregisterUidObserver(this);
Mikhail Naganoveae73eb2018-04-03 16:57:36 -0700803 Mutex::Autolock _l(mLock);
804 mObserverRegistered = false;
Svet Ganovf4ddfef2018-01-16 07:37:58 -0800805}
806
Mikhail Naganoveae73eb2018-04-03 16:57:36 -0700807void AudioPolicyService::UidPolicy::binderDied(__unused const wp<IBinder> &who) {
808 Mutex::Autolock _l(mLock);
809 mCachedUids.clear();
810 mObserverRegistered = false;
Svet Ganovf4ddfef2018-01-16 07:37:58 -0800811}
812
Eric Laurente8c8b432018-10-17 10:08:02 -0700813void AudioPolicyService::UidPolicy::checkRegistered() {
Mikhail Naganoveae73eb2018-04-03 16:57:36 -0700814 bool needToReregister = false;
815 {
816 Mutex::Autolock _l(mLock);
817 needToReregister = !mObserverRegistered;
Svet Ganovf4ddfef2018-01-16 07:37:58 -0800818 }
Mikhail Naganoveae73eb2018-04-03 16:57:36 -0700819 if (needToReregister) {
820 // Looks like ActivityManager has died previously, attempt to re-register.
821 registerSelf();
822 }
Eric Laurente8c8b432018-10-17 10:08:02 -0700823}
824
825bool AudioPolicyService::UidPolicy::isUidActive(uid_t uid) {
826 if (isServiceUid(uid)) return true;
827 checkRegistered();
Mikhail Naganoveae73eb2018-04-03 16:57:36 -0700828 {
829 Mutex::Autolock _l(mLock);
830 auto overrideIter = mOverrideUids.find(uid);
831 if (overrideIter != mOverrideUids.end()) {
Eric Laurente8c8b432018-10-17 10:08:02 -0700832 return overrideIter->second.first;
Mikhail Naganoveae73eb2018-04-03 16:57:36 -0700833 }
834 // In an absense of the ActivityManager, assume everything to be active.
835 if (!mObserverRegistered) return true;
836 auto cacheIter = mCachedUids.find(uid);
Mikhail Naganoveba668a2018-04-05 08:13:15 -0700837 if (cacheIter != mCachedUids.end()) {
Eric Laurente8c8b432018-10-17 10:08:02 -0700838 return cacheIter->second.first;
Mikhail Naganoveae73eb2018-04-03 16:57:36 -0700839 }
840 }
841 ActivityManager am;
842 bool active = am.isUidActive(uid, String16("audioserver"));
843 {
844 Mutex::Autolock _l(mLock);
Eric Laurente8c8b432018-10-17 10:08:02 -0700845 mCachedUids.insert(std::pair<uid_t,
846 std::pair<bool, int>>(uid, std::pair<bool, int>(active,
847 ActivityManager::PROCESS_STATE_UNKNOWN)));
Mikhail Naganoveae73eb2018-04-03 16:57:36 -0700848 }
849 return active;
Svet Ganovf4ddfef2018-01-16 07:37:58 -0800850}
851
Eric Laurente8c8b432018-10-17 10:08:02 -0700852int AudioPolicyService::UidPolicy::getUidState(uid_t uid) {
853 if (isServiceUid(uid)) {
854 return ActivityManager::PROCESS_STATE_TOP;
855 }
856 checkRegistered();
857 {
858 Mutex::Autolock _l(mLock);
859 auto overrideIter = mOverrideUids.find(uid);
860 if (overrideIter != mOverrideUids.end()) {
861 if (overrideIter->second.first) {
862 if (overrideIter->second.second != ActivityManager::PROCESS_STATE_UNKNOWN) {
863 return overrideIter->second.second;
864 } else {
865 auto cacheIter = mCachedUids.find(uid);
866 if (cacheIter != mCachedUids.end()) {
867 return cacheIter->second.second;
868 }
869 }
870 }
871 return ActivityManager::PROCESS_STATE_UNKNOWN;
872 }
873 // In an absense of the ActivityManager, assume everything to be active.
874 if (!mObserverRegistered) {
875 return ActivityManager::PROCESS_STATE_TOP;
876 }
877 auto cacheIter = mCachedUids.find(uid);
878 if (cacheIter != mCachedUids.end()) {
879 if (cacheIter->second.first) {
880 return cacheIter->second.second;
881 } else {
882 return ActivityManager::PROCESS_STATE_UNKNOWN;
883 }
884 }
885 }
886 ActivityManager am;
887 bool active = am.isUidActive(uid, String16("audioserver"));
888 int state = ActivityManager::PROCESS_STATE_UNKNOWN;
889 if (active) {
890 state = am.getUidProcessState(uid, String16("audioserver"));
891 }
892 {
893 Mutex::Autolock _l(mLock);
894 mCachedUids.insert(std::pair<uid_t,
895 std::pair<bool, int>>(uid, std::pair<bool, int>(active, state)));
896 }
Eric Laurent4eb58f12018-12-07 16:41:02 -0800897
Eric Laurente8c8b432018-10-17 10:08:02 -0700898 return state;
899}
900
Mikhail Naganoveae73eb2018-04-03 16:57:36 -0700901void AudioPolicyService::UidPolicy::onUidActive(uid_t uid) {
Eric Laurente8c8b432018-10-17 10:08:02 -0700902 updateUid(&mCachedUids, uid, true, ActivityManager::PROCESS_STATE_UNKNOWN, true);
Mikhail Naganoveae73eb2018-04-03 16:57:36 -0700903}
904
905void AudioPolicyService::UidPolicy::onUidGone(uid_t uid, __unused bool disabled) {
Eric Laurente8c8b432018-10-17 10:08:02 -0700906 updateUid(&mCachedUids, uid, false, ActivityManager::PROCESS_STATE_UNKNOWN, false);
Mikhail Naganoveae73eb2018-04-03 16:57:36 -0700907}
908
909void AudioPolicyService::UidPolicy::onUidIdle(uid_t uid, __unused bool disabled) {
Eric Laurente8c8b432018-10-17 10:08:02 -0700910 updateUid(&mCachedUids, uid, false, ActivityManager::PROCESS_STATE_UNKNOWN, true);
Mikhail Naganoveae73eb2018-04-03 16:57:36 -0700911}
912
Eric Laurente8c8b432018-10-17 10:08:02 -0700913void AudioPolicyService::UidPolicy::onUidStateChanged(uid_t uid,
914 int32_t procState,
915 int64_t procStateSeq __unused) {
916 if (procState != ActivityManager::PROCESS_STATE_UNKNOWN) {
917 updateUid(&mCachedUids, uid, true, procState, true);
Mikhail Naganoveae73eb2018-04-03 16:57:36 -0700918 }
919}
920
921void AudioPolicyService::UidPolicy::updateOverrideUid(uid_t uid, bool active, bool insert) {
Eric Laurente8c8b432018-10-17 10:08:02 -0700922 updateUid(&mOverrideUids, uid, active, ActivityManager::PROCESS_STATE_UNKNOWN, insert);
923}
924
925void AudioPolicyService::UidPolicy::notifyService() {
926 sp<AudioPolicyService> service = mService.promote();
927 if (service != nullptr) {
928 service->updateUidStates();
Mikhail Naganoveae73eb2018-04-03 16:57:36 -0700929 }
930}
931
Eric Laurente8c8b432018-10-17 10:08:02 -0700932void AudioPolicyService::UidPolicy::updateUid(std::unordered_map<uid_t,
933 std::pair<bool, int>> *uids,
934 uid_t uid,
935 bool active,
936 int state,
937 bool insert) {
938 if (isServiceUid(uid)) {
939 return;
940 }
941 bool wasActive = isUidActive(uid);
942 int previousState = getUidState(uid);
Mikhail Naganoveae73eb2018-04-03 16:57:36 -0700943 {
944 Mutex::Autolock _l(mLock);
Eric Laurente8c8b432018-10-17 10:08:02 -0700945 updateUidLocked(uids, uid, active, state, insert);
Mikhail Naganoveae73eb2018-04-03 16:57:36 -0700946 }
Eric Laurente8c8b432018-10-17 10:08:02 -0700947 if (wasActive != isUidActive(uid) || state != previousState) {
948 notifyService();
949 }
Mikhail Naganoveae73eb2018-04-03 16:57:36 -0700950}
951
Eric Laurente8c8b432018-10-17 10:08:02 -0700952void AudioPolicyService::UidPolicy::updateUidLocked(std::unordered_map<uid_t,
953 std::pair<bool, int>> *uids,
954 uid_t uid,
955 bool active,
956 int state,
957 bool insert) {
Mikhail Naganoveae73eb2018-04-03 16:57:36 -0700958 auto it = uids->find(uid);
959 if (it != uids->end()) {
Mikhail Naganoveae73eb2018-04-03 16:57:36 -0700960 if (insert) {
Eric Laurente8c8b432018-10-17 10:08:02 -0700961 if (state == ActivityManager::PROCESS_STATE_UNKNOWN) {
962 it->second.first = active;
963 }
964 if (it->second.first) {
965 it->second.second = state;
966 } else {
967 it->second.second = ActivityManager::PROCESS_STATE_UNKNOWN;
968 }
Mikhail Naganoveae73eb2018-04-03 16:57:36 -0700969 } else {
970 uids->erase(it);
971 }
Eric Laurente8c8b432018-10-17 10:08:02 -0700972 } else if (insert && (state == ActivityManager::PROCESS_STATE_UNKNOWN)) {
973 uids->insert(std::pair<uid_t, std::pair<bool, int>>(uid,
974 std::pair<bool, int>(active, state)));
Mikhail Naganoveae73eb2018-04-03 16:57:36 -0700975 }
Svet Ganovf4ddfef2018-01-16 07:37:58 -0800976}
Mathias Agopian65ab4712010-07-14 17:59:35 -0700977
Eric Laurent4eb58f12018-12-07 16:41:02 -0800978bool AudioPolicyService::UidPolicy::isA11yOnTop() {
979 for (const auto &uid : mCachedUids) {
980 std::vector<uid_t>::iterator it = find(mA11yUids.begin(), mA11yUids.end(), uid.first);
981 if (it == mA11yUids.end()) {
982 continue;
983 }
Amith Yamasanibcbb3002019-01-23 13:53:33 -0800984 if (uid.second.second >= ActivityManager::PROCESS_STATE_TOP
985 && uid.second.second <= ActivityManager::PROCESS_STATE_BOUND_FOREGROUND_SERVICE) {
Eric Laurent4eb58f12018-12-07 16:41:02 -0800986 return true;
987 }
988 }
989 return false;
990}
991
Eric Laurentb78763e2018-10-17 10:08:02 -0700992bool AudioPolicyService::UidPolicy::isA11yUid(uid_t uid)
993{
994 std::vector<uid_t>::iterator it = find(mA11yUids.begin(), mA11yUids.end(), uid);
995 return it != mA11yUids.end();
996}
997
Michael Groovercfd28302018-12-11 19:16:46 -0800998// ----------- AudioPolicyService::SensorPrivacyService implementation ----------
999void AudioPolicyService::SensorPrivacyPolicy::registerSelf() {
1000 SensorPrivacyManager spm;
1001 mSensorPrivacyEnabled = spm.isSensorPrivacyEnabled();
1002 spm.addSensorPrivacyListener(this);
1003}
1004
1005void AudioPolicyService::SensorPrivacyPolicy::unregisterSelf() {
1006 SensorPrivacyManager spm;
1007 spm.removeSensorPrivacyListener(this);
1008}
1009
1010bool AudioPolicyService::SensorPrivacyPolicy::isSensorPrivacyEnabled() {
1011 return mSensorPrivacyEnabled;
1012}
1013
1014binder::Status AudioPolicyService::SensorPrivacyPolicy::onSensorPrivacyChanged(bool enabled) {
1015 mSensorPrivacyEnabled = enabled;
1016 sp<AudioPolicyService> service = mService.promote();
1017 if (service != nullptr) {
1018 service->updateUidStates();
1019 }
1020 return binder::Status::ok();
1021}
1022
Mathias Agopian65ab4712010-07-14 17:59:35 -07001023// ----------- AudioPolicyService::AudioCommandThread implementation ----------
1024
Eric Laurentbfb1b832013-01-07 09:53:42 -08001025AudioPolicyService::AudioCommandThread::AudioCommandThread(String8 name,
1026 const wp<AudioPolicyService>& service)
1027 : Thread(false), mName(name), mService(service)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001028{
Mathias Agopian65ab4712010-07-14 17:59:35 -07001029}
1030
1031
1032AudioPolicyService::AudioCommandThread::~AudioCommandThread()
1033{
Eric Laurentbfb1b832013-01-07 09:53:42 -08001034 if (!mAudioCommands.isEmpty()) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07001035 release_wake_lock(mName.string());
1036 }
1037 mAudioCommands.clear();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001038}
1039
1040void AudioPolicyService::AudioCommandThread::onFirstRef()
1041{
Eric Laurentbfb1b832013-01-07 09:53:42 -08001042 run(mName.string(), ANDROID_PRIORITY_AUDIO);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001043}
1044
1045bool AudioPolicyService::AudioCommandThread::threadLoop()
1046{
Eric Laurentd7eda8d2016-02-02 17:18:39 -08001047 nsecs_t waitTime = -1;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001048
1049 mLock.lock();
1050 while (!exitPending())
1051 {
Eric Laurent59a89232014-06-08 14:14:17 -07001052 sp<AudioPolicyService> svc;
1053 while (!mAudioCommands.isEmpty() && !exitPending()) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07001054 nsecs_t curTime = systemTime();
1055 // commands are sorted by increasing time stamp: execute them from index 0 and up
1056 if (mAudioCommands[0]->mTime <= curTime) {
Eric Laurent0ede8922014-05-09 18:04:42 -07001057 sp<AudioCommand> command = mAudioCommands[0];
Mathias Agopian65ab4712010-07-14 17:59:35 -07001058 mAudioCommands.removeAt(0);
Eric Laurent0ede8922014-05-09 18:04:42 -07001059 mLastCommand = command;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001060
1061 switch (command->mCommand) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07001062 case SET_VOLUME: {
Eric Laurent0ede8922014-05-09 18:04:42 -07001063 VolumeData *data = (VolumeData *)command->mParam.get();
Steve Block3856b092011-10-20 11:56:00 +01001064 ALOGV("AudioCommandThread() processing set volume stream %d, \
Eric Laurentde070132010-07-13 04:45:46 -07001065 volume %f, output %d", data->mStream, data->mVolume, data->mIO);
Andy Hungfe726a62018-09-27 15:17:25 -07001066 mLock.unlock();
Eric Laurentde070132010-07-13 04:45:46 -07001067 command->mStatus = AudioSystem::setStreamVolume(data->mStream,
1068 data->mVolume,
1069 data->mIO);
Andy Hungfe726a62018-09-27 15:17:25 -07001070 mLock.lock();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001071 }break;
1072 case SET_PARAMETERS: {
Eric Laurent0ede8922014-05-09 18:04:42 -07001073 ParametersData *data = (ParametersData *)command->mParam.get();
Glenn Kastene53b9ea2012-03-12 16:29:55 -07001074 ALOGV("AudioCommandThread() processing set parameters string %s, io %d",
1075 data->mKeyValuePairs.string(), data->mIO);
Andy Hungfe726a62018-09-27 15:17:25 -07001076 mLock.unlock();
Glenn Kastene53b9ea2012-03-12 16:29:55 -07001077 command->mStatus = AudioSystem::setParameters(data->mIO, data->mKeyValuePairs);
Andy Hungfe726a62018-09-27 15:17:25 -07001078 mLock.lock();
Glenn Kastene53b9ea2012-03-12 16:29:55 -07001079 }break;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001080 case SET_VOICE_VOLUME: {
Eric Laurent0ede8922014-05-09 18:04:42 -07001081 VoiceVolumeData *data = (VoiceVolumeData *)command->mParam.get();
Steve Block3856b092011-10-20 11:56:00 +01001082 ALOGV("AudioCommandThread() processing set voice volume volume %f",
Eric Laurentde070132010-07-13 04:45:46 -07001083 data->mVolume);
Andy Hungfe726a62018-09-27 15:17:25 -07001084 mLock.unlock();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001085 command->mStatus = AudioSystem::setVoiceVolume(data->mVolume);
Andy Hungfe726a62018-09-27 15:17:25 -07001086 mLock.lock();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001087 }break;
Eric Laurentbfb1b832013-01-07 09:53:42 -08001088 case STOP_OUTPUT: {
Eric Laurent0ede8922014-05-09 18:04:42 -07001089 StopOutputData *data = (StopOutputData *)command->mParam.get();
Eric Laurentd7fe0862018-07-14 16:48:01 -07001090 ALOGV("AudioCommandThread() processing stop output portId %d",
1091 data->mPortId);
Eric Laurent59a89232014-06-08 14:14:17 -07001092 svc = mService.promote();
Eric Laurentbfb1b832013-01-07 09:53:42 -08001093 if (svc == 0) {
1094 break;
1095 }
1096 mLock.unlock();
Eric Laurentd7fe0862018-07-14 16:48:01 -07001097 svc->doStopOutput(data->mPortId);
Eric Laurentbfb1b832013-01-07 09:53:42 -08001098 mLock.lock();
Eric Laurentbfb1b832013-01-07 09:53:42 -08001099 }break;
1100 case RELEASE_OUTPUT: {
Eric Laurent0ede8922014-05-09 18:04:42 -07001101 ReleaseOutputData *data = (ReleaseOutputData *)command->mParam.get();
Eric Laurentd7fe0862018-07-14 16:48:01 -07001102 ALOGV("AudioCommandThread() processing release output portId %d",
1103 data->mPortId);
Eric Laurent59a89232014-06-08 14:14:17 -07001104 svc = mService.promote();
Eric Laurentbfb1b832013-01-07 09:53:42 -08001105 if (svc == 0) {
1106 break;
1107 }
1108 mLock.unlock();
Eric Laurentd7fe0862018-07-14 16:48:01 -07001109 svc->doReleaseOutput(data->mPortId);
Eric Laurentbfb1b832013-01-07 09:53:42 -08001110 mLock.lock();
Eric Laurentbfb1b832013-01-07 09:53:42 -08001111 }break;
Eric Laurent951f4552014-05-20 10:48:17 -07001112 case CREATE_AUDIO_PATCH: {
1113 CreateAudioPatchData *data = (CreateAudioPatchData *)command->mParam.get();
1114 ALOGV("AudioCommandThread() processing create audio patch");
1115 sp<IAudioFlinger> af = AudioSystem::get_audio_flinger();
1116 if (af == 0) {
1117 command->mStatus = PERMISSION_DENIED;
1118 } else {
Andy Hungfe726a62018-09-27 15:17:25 -07001119 mLock.unlock();
Eric Laurent951f4552014-05-20 10:48:17 -07001120 command->mStatus = af->createAudioPatch(&data->mPatch, &data->mHandle);
Andy Hungfe726a62018-09-27 15:17:25 -07001121 mLock.lock();
Eric Laurent951f4552014-05-20 10:48:17 -07001122 }
1123 } break;
1124 case RELEASE_AUDIO_PATCH: {
1125 ReleaseAudioPatchData *data = (ReleaseAudioPatchData *)command->mParam.get();
1126 ALOGV("AudioCommandThread() processing release audio patch");
1127 sp<IAudioFlinger> af = AudioSystem::get_audio_flinger();
1128 if (af == 0) {
1129 command->mStatus = PERMISSION_DENIED;
1130 } else {
Andy Hungfe726a62018-09-27 15:17:25 -07001131 mLock.unlock();
Eric Laurent951f4552014-05-20 10:48:17 -07001132 command->mStatus = af->releaseAudioPatch(data->mHandle);
Andy Hungfe726a62018-09-27 15:17:25 -07001133 mLock.lock();
Eric Laurent951f4552014-05-20 10:48:17 -07001134 }
1135 } break;
Eric Laurentb52c1522014-05-20 11:27:36 -07001136 case UPDATE_AUDIOPORT_LIST: {
1137 ALOGV("AudioCommandThread() processing update audio port list");
Eric Laurent59a89232014-06-08 14:14:17 -07001138 svc = mService.promote();
Eric Laurentb52c1522014-05-20 11:27:36 -07001139 if (svc == 0) {
1140 break;
1141 }
1142 mLock.unlock();
1143 svc->doOnAudioPortListUpdate();
1144 mLock.lock();
1145 }break;
1146 case UPDATE_AUDIOPATCH_LIST: {
1147 ALOGV("AudioCommandThread() processing update audio patch list");
Eric Laurent59a89232014-06-08 14:14:17 -07001148 svc = mService.promote();
Eric Laurentb52c1522014-05-20 11:27:36 -07001149 if (svc == 0) {
1150 break;
1151 }
1152 mLock.unlock();
1153 svc->doOnAudioPatchListUpdate();
1154 mLock.lock();
1155 }break;
François Gaffiecfe17322018-11-07 13:41:29 +01001156 case CHANGED_AUDIOVOLUMEGROUP: {
1157 AudioVolumeGroupData *data =
1158 static_cast<AudioVolumeGroupData *>(command->mParam.get());
1159 ALOGV("AudioCommandThread() processing update audio volume group");
1160 svc = mService.promote();
1161 if (svc == 0) {
1162 break;
1163 }
1164 mLock.unlock();
1165 svc->doOnAudioVolumeGroupChanged(data->mGroup, data->mFlags);
1166 mLock.lock();
1167 }break;
Eric Laurente1715a42014-05-20 11:30:42 -07001168 case SET_AUDIOPORT_CONFIG: {
1169 SetAudioPortConfigData *data = (SetAudioPortConfigData *)command->mParam.get();
1170 ALOGV("AudioCommandThread() processing set port config");
1171 sp<IAudioFlinger> af = AudioSystem::get_audio_flinger();
1172 if (af == 0) {
1173 command->mStatus = PERMISSION_DENIED;
1174 } else {
Andy Hungfe726a62018-09-27 15:17:25 -07001175 mLock.unlock();
Eric Laurente1715a42014-05-20 11:30:42 -07001176 command->mStatus = af->setAudioPortConfig(&data->mConfig);
Andy Hungfe726a62018-09-27 15:17:25 -07001177 mLock.lock();
Eric Laurente1715a42014-05-20 11:30:42 -07001178 }
1179 } break;
Jean-Michel Trivide801052015-04-14 19:10:14 -07001180 case DYN_POLICY_MIX_STATE_UPDATE: {
1181 DynPolicyMixStateUpdateData *data =
1182 (DynPolicyMixStateUpdateData *)command->mParam.get();
Jean-Michel Trivide801052015-04-14 19:10:14 -07001183 ALOGV("AudioCommandThread() processing dyn policy mix state update %s %d",
1184 data->mRegId.string(), data->mState);
1185 svc = mService.promote();
1186 if (svc == 0) {
1187 break;
1188 }
1189 mLock.unlock();
1190 svc->doOnDynamicPolicyMixStateUpdate(data->mRegId, data->mState);
1191 mLock.lock();
1192 } break;
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -08001193 case RECORDING_CONFIGURATION_UPDATE: {
1194 RecordingConfigurationUpdateData *data =
1195 (RecordingConfigurationUpdateData *)command->mParam.get();
1196 ALOGV("AudioCommandThread() processing recording configuration update");
1197 svc = mService.promote();
1198 if (svc == 0) {
1199 break;
1200 }
1201 mLock.unlock();
Jean-Michel Triviac4e4292016-12-22 11:39:31 -08001202 svc->doOnRecordingConfigurationUpdate(data->mEvent, &data->mClientInfo,
Eric Laurenta9f86652018-11-28 17:23:11 -08001203 &data->mClientConfig, data->mClientEffects,
1204 &data->mDeviceConfig, data->mEffects,
1205 data->mPatchHandle, data->mSource);
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -08001206 mLock.lock();
1207 } break;
Eric Laurentb20cf7d2019-04-05 19:37:34 -07001208 case SET_EFFECT_SUSPENDED: {
1209 SetEffectSuspendedData *data = (SetEffectSuspendedData *)command->mParam.get();
1210 ALOGV("AudioCommandThread() processing set effect suspended");
1211 sp<IAudioFlinger> af = AudioSystem::get_audio_flinger();
1212 if (af != 0) {
1213 mLock.unlock();
1214 af->setEffectSuspended(data->mEffectId, data->mSessionId, data->mSuspended);
1215 mLock.lock();
1216 }
1217 } break;
1218
Mathias Agopian65ab4712010-07-14 17:59:35 -07001219 default:
Steve Block5ff1dd52012-01-05 23:22:43 +00001220 ALOGW("AudioCommandThread() unknown command %d", command->mCommand);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001221 }
Eric Laurent0ede8922014-05-09 18:04:42 -07001222 {
1223 Mutex::Autolock _l(command->mLock);
1224 if (command->mWaitStatus) {
1225 command->mWaitStatus = false;
1226 command->mCond.signal();
1227 }
1228 }
Eric Laurentd7eda8d2016-02-02 17:18:39 -08001229 waitTime = -1;
Zach Janga754b4f2015-10-27 01:29:34 +00001230 // release mLock before releasing strong reference on the service as
1231 // AudioPolicyService destructor calls AudioCommandThread::exit() which
1232 // acquires mLock.
1233 mLock.unlock();
1234 svc.clear();
1235 mLock.lock();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001236 } else {
1237 waitTime = mAudioCommands[0]->mTime - curTime;
1238 break;
1239 }
1240 }
Zach Janga754b4f2015-10-27 01:29:34 +00001241
1242 // release delayed commands wake lock if the queue is empty
1243 if (mAudioCommands.isEmpty()) {
Ricardo Garcia05f2fdc2014-07-24 15:48:24 -07001244 release_wake_lock(mName.string());
Zach Janga754b4f2015-10-27 01:29:34 +00001245 }
1246
1247 // At this stage we have either an empty command queue or the first command in the queue
1248 // has a finite delay. So unless we are exiting it is safe to wait.
1249 if (!exitPending()) {
Eric Laurent59a89232014-06-08 14:14:17 -07001250 ALOGV("AudioCommandThread() going to sleep");
Eric Laurentd7eda8d2016-02-02 17:18:39 -08001251 if (waitTime == -1) {
1252 mWaitWorkCV.wait(mLock);
1253 } else {
1254 mWaitWorkCV.waitRelative(mLock, waitTime);
1255 }
Eric Laurent59a89232014-06-08 14:14:17 -07001256 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001257 }
Ricardo Garcia05f2fdc2014-07-24 15:48:24 -07001258 // release delayed commands wake lock before quitting
1259 if (!mAudioCommands.isEmpty()) {
1260 release_wake_lock(mName.string());
1261 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001262 mLock.unlock();
1263 return false;
1264}
1265
1266status_t AudioPolicyService::AudioCommandThread::dump(int fd)
1267{
1268 const size_t SIZE = 256;
1269 char buffer[SIZE];
1270 String8 result;
1271
1272 snprintf(buffer, SIZE, "AudioCommandThread %p Dump\n", this);
1273 result.append(buffer);
1274 write(fd, result.string(), result.size());
1275
Mikhail Naganov959e2d02019-03-28 11:08:19 -07001276 bool locked = dumpTryLock(mLock);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001277 if (!locked) {
1278 String8 result2(kCmdDeadlockedString);
1279 write(fd, result2.string(), result2.size());
1280 }
1281
1282 snprintf(buffer, SIZE, "- Commands:\n");
1283 result = String8(buffer);
1284 result.append(" Command Time Wait pParam\n");
Glenn Kasten8d6a2442012-02-08 14:04:28 -08001285 for (size_t i = 0; i < mAudioCommands.size(); i++) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07001286 mAudioCommands[i]->dump(buffer, SIZE);
1287 result.append(buffer);
1288 }
1289 result.append(" Last Command\n");
Eric Laurent0ede8922014-05-09 18:04:42 -07001290 if (mLastCommand != 0) {
1291 mLastCommand->dump(buffer, SIZE);
1292 result.append(buffer);
1293 } else {
1294 result.append(" none\n");
1295 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001296
1297 write(fd, result.string(), result.size());
1298
1299 if (locked) mLock.unlock();
1300
1301 return NO_ERROR;
1302}
1303
Glenn Kastenfff6d712012-01-12 16:38:12 -08001304status_t AudioPolicyService::AudioCommandThread::volumeCommand(audio_stream_type_t stream,
Eric Laurentde070132010-07-13 04:45:46 -07001305 float volume,
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001306 audio_io_handle_t output,
Eric Laurentde070132010-07-13 04:45:46 -07001307 int delayMs)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001308{
Eric Laurent0ede8922014-05-09 18:04:42 -07001309 sp<AudioCommand> command = new AudioCommand();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001310 command->mCommand = SET_VOLUME;
Eric Laurent0ede8922014-05-09 18:04:42 -07001311 sp<VolumeData> data = new VolumeData();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001312 data->mStream = stream;
1313 data->mVolume = volume;
1314 data->mIO = output;
1315 command->mParam = data;
Eric Laurent0ede8922014-05-09 18:04:42 -07001316 command->mWaitStatus = true;
Steve Block3856b092011-10-20 11:56:00 +01001317 ALOGV("AudioCommandThread() adding set volume stream %d, volume %f, output %d",
Eric Laurentde070132010-07-13 04:45:46 -07001318 stream, volume, output);
Eric Laurent0ede8922014-05-09 18:04:42 -07001319 return sendCommand(command, delayMs);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001320}
1321
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001322status_t AudioPolicyService::AudioCommandThread::parametersCommand(audio_io_handle_t ioHandle,
Dima Zavinfce7a472011-04-19 22:30:36 -07001323 const char *keyValuePairs,
Eric Laurentde070132010-07-13 04:45:46 -07001324 int delayMs)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001325{
Eric Laurent0ede8922014-05-09 18:04:42 -07001326 sp<AudioCommand> command = new AudioCommand();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001327 command->mCommand = SET_PARAMETERS;
Eric Laurent0ede8922014-05-09 18:04:42 -07001328 sp<ParametersData> data = new ParametersData();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001329 data->mIO = ioHandle;
Dima Zavinfce7a472011-04-19 22:30:36 -07001330 data->mKeyValuePairs = String8(keyValuePairs);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001331 command->mParam = data;
Eric Laurent0ede8922014-05-09 18:04:42 -07001332 command->mWaitStatus = true;
Steve Block3856b092011-10-20 11:56:00 +01001333 ALOGV("AudioCommandThread() adding set parameter string %s, io %d ,delay %d",
Dima Zavinfce7a472011-04-19 22:30:36 -07001334 keyValuePairs, ioHandle, delayMs);
Eric Laurent0ede8922014-05-09 18:04:42 -07001335 return sendCommand(command, delayMs);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001336}
1337
1338status_t AudioPolicyService::AudioCommandThread::voiceVolumeCommand(float volume, int delayMs)
1339{
Eric Laurent0ede8922014-05-09 18:04:42 -07001340 sp<AudioCommand> command = new AudioCommand();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001341 command->mCommand = SET_VOICE_VOLUME;
Eric Laurent0ede8922014-05-09 18:04:42 -07001342 sp<VoiceVolumeData> data = new VoiceVolumeData();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001343 data->mVolume = volume;
1344 command->mParam = data;
Eric Laurent0ede8922014-05-09 18:04:42 -07001345 command->mWaitStatus = true;
Steve Block3856b092011-10-20 11:56:00 +01001346 ALOGV("AudioCommandThread() adding set voice volume volume %f", volume);
Eric Laurent0ede8922014-05-09 18:04:42 -07001347 return sendCommand(command, delayMs);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001348}
1349
Eric Laurentb20cf7d2019-04-05 19:37:34 -07001350void AudioPolicyService::AudioCommandThread::setEffectSuspendedCommand(int effectId,
1351 audio_session_t sessionId,
1352 bool suspended)
1353{
1354 sp<AudioCommand> command = new AudioCommand();
1355 command->mCommand = SET_EFFECT_SUSPENDED;
1356 sp<SetEffectSuspendedData> data = new SetEffectSuspendedData();
1357 data->mEffectId = effectId;
1358 data->mSessionId = sessionId;
1359 data->mSuspended = suspended;
1360 command->mParam = data;
1361 ALOGV("AudioCommandThread() adding set suspended effectId %d sessionId %d suspended %d",
1362 effectId, sessionId, suspended);
1363 sendCommand(command);
1364}
1365
1366
Eric Laurentd7fe0862018-07-14 16:48:01 -07001367void AudioPolicyService::AudioCommandThread::stopOutputCommand(audio_port_handle_t portId)
Eric Laurentbfb1b832013-01-07 09:53:42 -08001368{
Eric Laurent0ede8922014-05-09 18:04:42 -07001369 sp<AudioCommand> command = new AudioCommand();
Eric Laurentbfb1b832013-01-07 09:53:42 -08001370 command->mCommand = STOP_OUTPUT;
Eric Laurent0ede8922014-05-09 18:04:42 -07001371 sp<StopOutputData> data = new StopOutputData();
Eric Laurentd7fe0862018-07-14 16:48:01 -07001372 data->mPortId = portId;
Jesper Tragardh48412dc2014-03-24 14:12:43 +01001373 command->mParam = data;
Eric Laurentd7fe0862018-07-14 16:48:01 -07001374 ALOGV("AudioCommandThread() adding stop output portId %d", portId);
Eric Laurent0ede8922014-05-09 18:04:42 -07001375 sendCommand(command);
Eric Laurentbfb1b832013-01-07 09:53:42 -08001376}
1377
Eric Laurentd7fe0862018-07-14 16:48:01 -07001378void AudioPolicyService::AudioCommandThread::releaseOutputCommand(audio_port_handle_t portId)
Eric Laurentbfb1b832013-01-07 09:53:42 -08001379{
Eric Laurent0ede8922014-05-09 18:04:42 -07001380 sp<AudioCommand> command = new AudioCommand();
Eric Laurentbfb1b832013-01-07 09:53:42 -08001381 command->mCommand = RELEASE_OUTPUT;
Eric Laurent0ede8922014-05-09 18:04:42 -07001382 sp<ReleaseOutputData> data = new ReleaseOutputData();
Eric Laurentd7fe0862018-07-14 16:48:01 -07001383 data->mPortId = portId;
Jesper Tragardh48412dc2014-03-24 14:12:43 +01001384 command->mParam = data;
Eric Laurentd7fe0862018-07-14 16:48:01 -07001385 ALOGV("AudioCommandThread() adding release output portId %d", portId);
Eric Laurent0ede8922014-05-09 18:04:42 -07001386 sendCommand(command);
1387}
1388
Eric Laurent951f4552014-05-20 10:48:17 -07001389status_t AudioPolicyService::AudioCommandThread::createAudioPatchCommand(
1390 const struct audio_patch *patch,
1391 audio_patch_handle_t *handle,
1392 int delayMs)
1393{
1394 status_t status = NO_ERROR;
1395
1396 sp<AudioCommand> command = new AudioCommand();
1397 command->mCommand = CREATE_AUDIO_PATCH;
1398 CreateAudioPatchData *data = new CreateAudioPatchData();
1399 data->mPatch = *patch;
1400 data->mHandle = *handle;
1401 command->mParam = data;
1402 command->mWaitStatus = true;
1403 ALOGV("AudioCommandThread() adding create patch delay %d", delayMs);
1404 status = sendCommand(command, delayMs);
1405 if (status == NO_ERROR) {
1406 *handle = data->mHandle;
1407 }
1408 return status;
1409}
1410
1411status_t AudioPolicyService::AudioCommandThread::releaseAudioPatchCommand(audio_patch_handle_t handle,
1412 int delayMs)
1413{
1414 sp<AudioCommand> command = new AudioCommand();
1415 command->mCommand = RELEASE_AUDIO_PATCH;
1416 ReleaseAudioPatchData *data = new ReleaseAudioPatchData();
1417 data->mHandle = handle;
1418 command->mParam = data;
1419 command->mWaitStatus = true;
1420 ALOGV("AudioCommandThread() adding release patch delay %d", delayMs);
1421 return sendCommand(command, delayMs);
1422}
1423
Eric Laurentb52c1522014-05-20 11:27:36 -07001424void AudioPolicyService::AudioCommandThread::updateAudioPortListCommand()
1425{
1426 sp<AudioCommand> command = new AudioCommand();
1427 command->mCommand = UPDATE_AUDIOPORT_LIST;
1428 ALOGV("AudioCommandThread() adding update audio port list");
1429 sendCommand(command);
1430}
1431
1432void AudioPolicyService::AudioCommandThread::updateAudioPatchListCommand()
1433{
1434 sp<AudioCommand>command = new AudioCommand();
1435 command->mCommand = UPDATE_AUDIOPATCH_LIST;
1436 ALOGV("AudioCommandThread() adding update audio patch list");
1437 sendCommand(command);
1438}
1439
François Gaffiecfe17322018-11-07 13:41:29 +01001440void AudioPolicyService::AudioCommandThread::changeAudioVolumeGroupCommand(volume_group_t group,
1441 int flags)
1442{
1443 sp<AudioCommand>command = new AudioCommand();
1444 command->mCommand = CHANGED_AUDIOVOLUMEGROUP;
1445 AudioVolumeGroupData *data= new AudioVolumeGroupData();
1446 data->mGroup = group;
1447 data->mFlags = flags;
1448 command->mParam = data;
1449 ALOGV("AudioCommandThread() adding audio volume group changed");
1450 sendCommand(command);
1451}
1452
Eric Laurente1715a42014-05-20 11:30:42 -07001453status_t AudioPolicyService::AudioCommandThread::setAudioPortConfigCommand(
1454 const struct audio_port_config *config, int delayMs)
1455{
1456 sp<AudioCommand> command = new AudioCommand();
1457 command->mCommand = SET_AUDIOPORT_CONFIG;
1458 SetAudioPortConfigData *data = new SetAudioPortConfigData();
1459 data->mConfig = *config;
1460 command->mParam = data;
1461 command->mWaitStatus = true;
1462 ALOGV("AudioCommandThread() adding set port config delay %d", delayMs);
1463 return sendCommand(command, delayMs);
1464}
1465
Jean-Michel Trivide801052015-04-14 19:10:14 -07001466void AudioPolicyService::AudioCommandThread::dynamicPolicyMixStateUpdateCommand(
Chih-Hung Hsiehe964d4e2016-08-09 14:31:32 -07001467 const String8& regId, int32_t state)
Jean-Michel Trivide801052015-04-14 19:10:14 -07001468{
1469 sp<AudioCommand> command = new AudioCommand();
1470 command->mCommand = DYN_POLICY_MIX_STATE_UPDATE;
1471 DynPolicyMixStateUpdateData *data = new DynPolicyMixStateUpdateData();
1472 data->mRegId = regId;
1473 data->mState = state;
1474 command->mParam = data;
1475 ALOGV("AudioCommandThread() sending dynamic policy mix (id=%s) state update to %d",
1476 regId.string(), state);
1477 sendCommand(command);
1478}
1479
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -08001480void AudioPolicyService::AudioCommandThread::recordingConfigurationUpdateCommand(
Eric Laurenta9f86652018-11-28 17:23:11 -08001481 int event,
1482 const record_client_info_t *clientInfo,
1483 const audio_config_base_t *clientConfig,
1484 std::vector<effect_descriptor_t> clientEffects,
1485 const audio_config_base_t *deviceConfig,
1486 std::vector<effect_descriptor_t> effects,
1487 audio_patch_handle_t patchHandle,
1488 audio_source_t source)
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -08001489{
1490 sp<AudioCommand>command = new AudioCommand();
1491 command->mCommand = RECORDING_CONFIGURATION_UPDATE;
1492 RecordingConfigurationUpdateData *data = new RecordingConfigurationUpdateData();
1493 data->mEvent = event;
Jean-Michel Triviac4e4292016-12-22 11:39:31 -08001494 data->mClientInfo = *clientInfo;
Jean-Michel Trivi7281aa92016-02-17 15:33:40 -08001495 data->mClientConfig = *clientConfig;
Eric Laurenta9f86652018-11-28 17:23:11 -08001496 data->mClientEffects = clientEffects;
Jean-Michel Trivi7281aa92016-02-17 15:33:40 -08001497 data->mDeviceConfig = *deviceConfig;
Eric Laurenta9f86652018-11-28 17:23:11 -08001498 data->mEffects = effects;
Jean-Michel Trivi8c7cf3b2016-02-25 17:08:24 -08001499 data->mPatchHandle = patchHandle;
Eric Laurenta9f86652018-11-28 17:23:11 -08001500 data->mSource = source;
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -08001501 command->mParam = data;
Jean-Michel Triviac4e4292016-12-22 11:39:31 -08001502 ALOGV("AudioCommandThread() adding recording configuration update event %d, source %d uid %u",
1503 event, clientInfo->source, clientInfo->uid);
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -08001504 sendCommand(command);
1505}
1506
Eric Laurent0ede8922014-05-09 18:04:42 -07001507status_t AudioPolicyService::AudioCommandThread::sendCommand(sp<AudioCommand>& command, int delayMs)
1508{
1509 {
1510 Mutex::Autolock _l(mLock);
1511 insertCommand_l(command, delayMs);
1512 mWaitWorkCV.signal();
1513 }
1514 Mutex::Autolock _l(command->mLock);
1515 while (command->mWaitStatus) {
1516 nsecs_t timeOutNs = kAudioCommandTimeoutNs + milliseconds(delayMs);
1517 if (command->mCond.waitRelative(command->mLock, timeOutNs) != NO_ERROR) {
1518 command->mStatus = TIMED_OUT;
1519 command->mWaitStatus = false;
1520 }
1521 }
1522 return command->mStatus;
Eric Laurentbfb1b832013-01-07 09:53:42 -08001523}
1524
Mathias Agopian65ab4712010-07-14 17:59:35 -07001525// insertCommand_l() must be called with mLock held
Eric Laurent0ede8922014-05-09 18:04:42 -07001526void AudioPolicyService::AudioCommandThread::insertCommand_l(sp<AudioCommand>& command, int delayMs)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001527{
Glenn Kasten8d6a2442012-02-08 14:04:28 -08001528 ssize_t i; // not size_t because i will count down to -1
Eric Laurent0ede8922014-05-09 18:04:42 -07001529 Vector < sp<AudioCommand> > removedCommands;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001530 command->mTime = systemTime() + milliseconds(delayMs);
1531
1532 // acquire wake lock to make sure delayed commands are processed
Eric Laurentbfb1b832013-01-07 09:53:42 -08001533 if (mAudioCommands.isEmpty()) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07001534 acquire_wake_lock(PARTIAL_WAKE_LOCK, mName.string());
1535 }
1536
1537 // check same pending commands with later time stamps and eliminate them
Ivan Lozano5ff158f2017-10-30 09:06:24 -07001538 for (i = (ssize_t)mAudioCommands.size()-1; i >= 0; i--) {
Eric Laurent0ede8922014-05-09 18:04:42 -07001539 sp<AudioCommand> command2 = mAudioCommands[i];
Mathias Agopian65ab4712010-07-14 17:59:35 -07001540 // commands are sorted by increasing time stamp: no need to scan the rest of mAudioCommands
1541 if (command2->mTime <= command->mTime) break;
Eric Laurente45b48a2014-09-04 16:40:57 -07001542
1543 // create audio patch or release audio patch commands are equivalent
1544 // with regard to filtering
1545 if ((command->mCommand == CREATE_AUDIO_PATCH) ||
1546 (command->mCommand == RELEASE_AUDIO_PATCH)) {
1547 if ((command2->mCommand != CREATE_AUDIO_PATCH) &&
1548 (command2->mCommand != RELEASE_AUDIO_PATCH)) {
1549 continue;
1550 }
1551 } else if (command2->mCommand != command->mCommand) continue;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001552
1553 switch (command->mCommand) {
1554 case SET_PARAMETERS: {
Eric Laurent0ede8922014-05-09 18:04:42 -07001555 ParametersData *data = (ParametersData *)command->mParam.get();
1556 ParametersData *data2 = (ParametersData *)command2->mParam.get();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001557 if (data->mIO != data2->mIO) break;
Steve Block3856b092011-10-20 11:56:00 +01001558 ALOGV("Comparing parameter command %s to new command %s",
Eric Laurentde070132010-07-13 04:45:46 -07001559 data2->mKeyValuePairs.string(), data->mKeyValuePairs.string());
Mathias Agopian65ab4712010-07-14 17:59:35 -07001560 AudioParameter param = AudioParameter(data->mKeyValuePairs);
1561 AudioParameter param2 = AudioParameter(data2->mKeyValuePairs);
1562 for (size_t j = 0; j < param.size(); j++) {
Glenn Kastene53b9ea2012-03-12 16:29:55 -07001563 String8 key;
1564 String8 value;
1565 param.getAt(j, key, value);
1566 for (size_t k = 0; k < param2.size(); k++) {
1567 String8 key2;
1568 String8 value2;
1569 param2.getAt(k, key2, value2);
1570 if (key2 == key) {
1571 param2.remove(key2);
1572 ALOGV("Filtering out parameter %s", key2.string());
1573 break;
1574 }
1575 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001576 }
1577 // if all keys have been filtered out, remove the command.
1578 // otherwise, update the key value pairs
1579 if (param2.size() == 0) {
1580 removedCommands.add(command2);
1581 } else {
1582 data2->mKeyValuePairs = param2.toString();
1583 }
Eric Laurent21e54562013-09-23 12:08:05 -07001584 command->mTime = command2->mTime;
1585 // force delayMs to non 0 so that code below does not request to wait for
1586 // command status as the command is now delayed
1587 delayMs = 1;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001588 } break;
1589
1590 case SET_VOLUME: {
Eric Laurent0ede8922014-05-09 18:04:42 -07001591 VolumeData *data = (VolumeData *)command->mParam.get();
1592 VolumeData *data2 = (VolumeData *)command2->mParam.get();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001593 if (data->mIO != data2->mIO) break;
1594 if (data->mStream != data2->mStream) break;
Steve Block3856b092011-10-20 11:56:00 +01001595 ALOGV("Filtering out volume command on output %d for stream %d",
Eric Laurentde070132010-07-13 04:45:46 -07001596 data->mIO, data->mStream);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001597 removedCommands.add(command2);
Eric Laurent21e54562013-09-23 12:08:05 -07001598 command->mTime = command2->mTime;
1599 // force delayMs to non 0 so that code below does not request to wait for
1600 // command status as the command is now delayed
1601 delayMs = 1;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001602 } break;
Eric Laurente45b48a2014-09-04 16:40:57 -07001603
Eric Laurentbaf35fe2016-07-27 15:36:53 -07001604 case SET_VOICE_VOLUME: {
1605 VoiceVolumeData *data = (VoiceVolumeData *)command->mParam.get();
1606 VoiceVolumeData *data2 = (VoiceVolumeData *)command2->mParam.get();
1607 ALOGV("Filtering out voice volume command value %f replaced by %f",
1608 data2->mVolume, data->mVolume);
1609 removedCommands.add(command2);
1610 command->mTime = command2->mTime;
1611 // force delayMs to non 0 so that code below does not request to wait for
1612 // command status as the command is now delayed
1613 delayMs = 1;
1614 } break;
1615
Eric Laurente45b48a2014-09-04 16:40:57 -07001616 case CREATE_AUDIO_PATCH:
1617 case RELEASE_AUDIO_PATCH: {
1618 audio_patch_handle_t handle;
Haynes Mathew Georgea2d4a6d2014-10-13 13:05:22 -07001619 struct audio_patch patch;
Eric Laurente45b48a2014-09-04 16:40:57 -07001620 if (command->mCommand == CREATE_AUDIO_PATCH) {
1621 handle = ((CreateAudioPatchData *)command->mParam.get())->mHandle;
Haynes Mathew Georgea2d4a6d2014-10-13 13:05:22 -07001622 patch = ((CreateAudioPatchData *)command->mParam.get())->mPatch;
Eric Laurente45b48a2014-09-04 16:40:57 -07001623 } else {
1624 handle = ((ReleaseAudioPatchData *)command->mParam.get())->mHandle;
Mikhail Naganov7be71d22018-05-23 16:51:46 -07001625 memset(&patch, 0, sizeof(patch));
Eric Laurente45b48a2014-09-04 16:40:57 -07001626 }
1627 audio_patch_handle_t handle2;
Haynes Mathew Georgea2d4a6d2014-10-13 13:05:22 -07001628 struct audio_patch patch2;
Eric Laurente45b48a2014-09-04 16:40:57 -07001629 if (command2->mCommand == CREATE_AUDIO_PATCH) {
1630 handle2 = ((CreateAudioPatchData *)command2->mParam.get())->mHandle;
Haynes Mathew Georgea2d4a6d2014-10-13 13:05:22 -07001631 patch2 = ((CreateAudioPatchData *)command2->mParam.get())->mPatch;
Eric Laurente45b48a2014-09-04 16:40:57 -07001632 } else {
1633 handle2 = ((ReleaseAudioPatchData *)command2->mParam.get())->mHandle;
Glenn Kastenf60b6b62015-07-06 10:53:26 -07001634 memset(&patch2, 0, sizeof(patch2));
Eric Laurente45b48a2014-09-04 16:40:57 -07001635 }
1636 if (handle != handle2) break;
Haynes Mathew Georgea2d4a6d2014-10-13 13:05:22 -07001637 /* Filter CREATE_AUDIO_PATCH commands only when they are issued for
1638 same output. */
1639 if( (command->mCommand == CREATE_AUDIO_PATCH) &&
1640 (command2->mCommand == CREATE_AUDIO_PATCH) ) {
1641 bool isOutputDiff = false;
1642 if (patch.num_sources == patch2.num_sources) {
1643 for (unsigned count = 0; count < patch.num_sources; count++) {
1644 if (patch.sources[count].id != patch2.sources[count].id) {
1645 isOutputDiff = true;
1646 break;
1647 }
1648 }
1649 if (isOutputDiff)
1650 break;
1651 }
1652 }
Eric Laurente45b48a2014-09-04 16:40:57 -07001653 ALOGV("Filtering out %s audio patch command for handle %d",
1654 (command->mCommand == CREATE_AUDIO_PATCH) ? "create" : "release", handle);
1655 removedCommands.add(command2);
1656 command->mTime = command2->mTime;
1657 // force delayMs to non 0 so that code below does not request to wait for
1658 // command status as the command is now delayed
1659 delayMs = 1;
1660 } break;
1661
Jean-Michel Trivide801052015-04-14 19:10:14 -07001662 case DYN_POLICY_MIX_STATE_UPDATE: {
1663
1664 } break;
1665
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -08001666 case RECORDING_CONFIGURATION_UPDATE: {
1667
1668 } break;
1669
Mathias Agopian65ab4712010-07-14 17:59:35 -07001670 default:
1671 break;
1672 }
1673 }
1674
1675 // remove filtered commands
1676 for (size_t j = 0; j < removedCommands.size(); j++) {
1677 // removed commands always have time stamps greater than current command
1678 for (size_t k = i + 1; k < mAudioCommands.size(); k++) {
Eric Laurent0ede8922014-05-09 18:04:42 -07001679 if (mAudioCommands[k].get() == removedCommands[j].get()) {
Steve Block3856b092011-10-20 11:56:00 +01001680 ALOGV("suppressing command: %d", mAudioCommands[k]->mCommand);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001681 mAudioCommands.removeAt(k);
1682 break;
1683 }
1684 }
1685 }
1686 removedCommands.clear();
1687
Eric Laurentaa79bef2015-01-15 14:33:51 -08001688 // Disable wait for status if delay is not 0.
1689 // Except for create audio patch command because the returned patch handle
1690 // is needed by audio policy manager
1691 if (delayMs != 0 && command->mCommand != CREATE_AUDIO_PATCH) {
Eric Laurentcec4abb2012-07-03 12:23:02 -07001692 command->mWaitStatus = false;
1693 }
Eric Laurentcec4abb2012-07-03 12:23:02 -07001694
Mathias Agopian65ab4712010-07-14 17:59:35 -07001695 // insert command at the right place according to its time stamp
Eric Laurent1e693b52014-07-09 15:03:28 -07001696 ALOGV("inserting command: %d at index %zd, num commands %zu",
1697 command->mCommand, i+1, mAudioCommands.size());
Mathias Agopian65ab4712010-07-14 17:59:35 -07001698 mAudioCommands.insertAt(command, i + 1);
1699}
1700
1701void AudioPolicyService::AudioCommandThread::exit()
1702{
Steve Block3856b092011-10-20 11:56:00 +01001703 ALOGV("AudioCommandThread::exit");
Mathias Agopian65ab4712010-07-14 17:59:35 -07001704 {
1705 AutoMutex _l(mLock);
1706 requestExit();
1707 mWaitWorkCV.signal();
1708 }
Zach Janga754b4f2015-10-27 01:29:34 +00001709 // Note that we can call it from the thread loop if all other references have been released
1710 // but it will safely return WOULD_BLOCK in this case
Mathias Agopian65ab4712010-07-14 17:59:35 -07001711 requestExitAndWait();
1712}
1713
1714void AudioPolicyService::AudioCommandThread::AudioCommand::dump(char* buffer, size_t size)
1715{
1716 snprintf(buffer, size, " %02d %06d.%03d %01u %p\n",
1717 mCommand,
1718 (int)ns2s(mTime),
1719 (int)ns2ms(mTime)%1000,
1720 mWaitStatus,
Eric Laurent0ede8922014-05-09 18:04:42 -07001721 mParam.get());
Mathias Agopian65ab4712010-07-14 17:59:35 -07001722}
1723
Dima Zavinfce7a472011-04-19 22:30:36 -07001724/******* helpers for the service_ops callbacks defined below *********/
1725void AudioPolicyService::setParameters(audio_io_handle_t ioHandle,
1726 const char *keyValuePairs,
1727 int delayMs)
1728{
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001729 mAudioCommandThread->parametersCommand(ioHandle, keyValuePairs,
Dima Zavinfce7a472011-04-19 22:30:36 -07001730 delayMs);
1731}
1732
1733int AudioPolicyService::setStreamVolume(audio_stream_type_t stream,
1734 float volume,
1735 audio_io_handle_t output,
1736 int delayMs)
1737{
Glenn Kastenfff6d712012-01-12 16:38:12 -08001738 return (int)mAudioCommandThread->volumeCommand(stream, volume,
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001739 output, delayMs);
Dima Zavinfce7a472011-04-19 22:30:36 -07001740}
1741
Dima Zavinfce7a472011-04-19 22:30:36 -07001742int AudioPolicyService::setVoiceVolume(float volume, int delayMs)
1743{
1744 return (int)mAudioCommandThread->voiceVolumeCommand(volume, delayMs);
1745}
1746
Eric Laurentb20cf7d2019-04-05 19:37:34 -07001747void AudioPolicyService::setEffectSuspended(int effectId,
1748 audio_session_t sessionId,
1749 bool suspended)
1750{
1751 mAudioCommandThread->setEffectSuspendedCommand(effectId, sessionId, suspended);
1752}
1753
1754
Dima Zavinfce7a472011-04-19 22:30:36 -07001755extern "C" {
Eric Laurent2d388ec2014-03-07 13:25:54 -08001756audio_module_handle_t aps_load_hw_module(void *service __unused,
1757 const char *name);
1758audio_io_handle_t aps_open_output(void *service __unused,
Eric Laurenta4c5a552012-03-29 10:12:40 -07001759 audio_devices_t *pDevices,
1760 uint32_t *pSamplingRate,
1761 audio_format_t *pFormat,
1762 audio_channel_mask_t *pChannelMask,
1763 uint32_t *pLatencyMs,
Eric Laurent2d388ec2014-03-07 13:25:54 -08001764 audio_output_flags_t flags);
Eric Laurenta4c5a552012-03-29 10:12:40 -07001765
Eric Laurent2d388ec2014-03-07 13:25:54 -08001766audio_io_handle_t aps_open_output_on_module(void *service __unused,
Eric Laurenta4c5a552012-03-29 10:12:40 -07001767 audio_module_handle_t module,
1768 audio_devices_t *pDevices,
1769 uint32_t *pSamplingRate,
1770 audio_format_t *pFormat,
1771 audio_channel_mask_t *pChannelMask,
1772 uint32_t *pLatencyMs,
Richard Fitzgeraldad3af332013-03-25 16:54:37 +00001773 audio_output_flags_t flags,
Eric Laurent2d388ec2014-03-07 13:25:54 -08001774 const audio_offload_info_t *offloadInfo);
1775audio_io_handle_t aps_open_dup_output(void *service __unused,
Dima Zavinfce7a472011-04-19 22:30:36 -07001776 audio_io_handle_t output1,
Eric Laurent2d388ec2014-03-07 13:25:54 -08001777 audio_io_handle_t output2);
1778int aps_close_output(void *service __unused, audio_io_handle_t output);
1779int aps_suspend_output(void *service __unused, audio_io_handle_t output);
1780int aps_restore_output(void *service __unused, audio_io_handle_t output);
1781audio_io_handle_t aps_open_input(void *service __unused,
Eric Laurenta4c5a552012-03-29 10:12:40 -07001782 audio_devices_t *pDevices,
1783 uint32_t *pSamplingRate,
1784 audio_format_t *pFormat,
1785 audio_channel_mask_t *pChannelMask,
Eric Laurent2d388ec2014-03-07 13:25:54 -08001786 audio_in_acoustics_t acoustics __unused);
1787audio_io_handle_t aps_open_input_on_module(void *service __unused,
Eric Laurenta4c5a552012-03-29 10:12:40 -07001788 audio_module_handle_t module,
1789 audio_devices_t *pDevices,
1790 uint32_t *pSamplingRate,
1791 audio_format_t *pFormat,
Eric Laurent2d388ec2014-03-07 13:25:54 -08001792 audio_channel_mask_t *pChannelMask);
1793int aps_close_input(void *service __unused, audio_io_handle_t input);
1794int aps_invalidate_stream(void *service __unused, audio_stream_type_t stream);
Glenn Kastend848eb42016-03-08 13:42:11 -08001795int aps_move_effects(void *service __unused, audio_session_t session,
Dima Zavinfce7a472011-04-19 22:30:36 -07001796 audio_io_handle_t src_output,
Eric Laurent2d388ec2014-03-07 13:25:54 -08001797 audio_io_handle_t dst_output);
1798char * aps_get_parameters(void *service __unused, audio_io_handle_t io_handle,
1799 const char *keys);
1800void aps_set_parameters(void *service, audio_io_handle_t io_handle,
1801 const char *kv_pairs, int delay_ms);
1802int aps_set_stream_volume(void *service, audio_stream_type_t stream,
Dima Zavinfce7a472011-04-19 22:30:36 -07001803 float volume, audio_io_handle_t output,
Eric Laurent2d388ec2014-03-07 13:25:54 -08001804 int delay_ms);
Eric Laurent2d388ec2014-03-07 13:25:54 -08001805int aps_set_voice_volume(void *service, float volume, int delay_ms);
1806};
Dima Zavinfce7a472011-04-19 22:30:36 -07001807
Mikhail Naganov1b2a7942017-12-08 10:18:09 -08001808} // namespace android