blob: f63fa8123a7223b8301bfd98a4793e0854bf1bd7 [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 Laurenta46bedb2018-12-07 18:01:26 -0800412// AND an accessibility service is on TOP
413// AND the source is VOICE_RECOGNITION or HOTWORD
414// OR uses VOICE_RECOGNITION AND is on TOP OR latest started
415// 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
419// AND is on TOP OR latest started
420// 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 Laurenta46bedb2018-12-07 18:01:26 -0800422// OR Any other client
423// AND The assistant is not on TOP
Eric Laurent1ff16a72019-03-14 18:35:04 -0700424// AND there is no active privacy sensitive capture or call
425// OR client has CAPTURE_AUDIO_OUTPUT privileged permission
Eric Laurent4eb58f12018-12-07 16:41:02 -0800426//TODO: mamanage pre processing effects according to use case priority
427
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 Laurent4eb58f12018-12-07 16:41:02 -0800439
Michael Groovercfd28302018-12-11 19:16:46 -0800440 // if Sensor Privacy is enabled then all recordings should be silenced.
441 if (mSensorPrivacyPolicy->isSensorPrivacyEnabled()) {
442 silenceAllRecordings_l();
443 return;
444 }
445
Eric Laurente8c8b432018-10-17 10:08:02 -0700446 for (size_t i =0; i < mAudioRecordClients.size(); i++) {
447 sp<AudioRecordClient> current = mAudioRecordClients[i];
Eric Laurent1ff16a72019-03-14 18:35:04 -0700448 if (!current->active) {
449 continue;
Eric Laurent4eb58f12018-12-07 16:41:02 -0800450 }
Eric Laurent1ff16a72019-03-14 18:35:04 -0700451
452 app_state_t appState = apmStatFromAmState(mUidPolicy->getUidState(current->uid));
453 // clients which app is in IDLE state are not eligible for top active or
454 // latest active
455 if (appState == APP_STATE_IDLE) {
456 continue;
457 }
458
459 if (appState == APP_STATE_TOP) {
Eric Laurenta46bedb2018-12-07 18:01:26 -0800460 if (current->startTimeNs > topStartNs) {
461 topActive = current;
462 topStartNs = current->startTimeNs;
463 }
Eric Laurent4eb58f12018-12-07 16:41:02 -0800464 if (mUidPolicy->isAssistantUid(current->uid)) {
465 isAssistantOnTop = true;
466 }
467 }
468 if (current->startTimeNs > latestStartNs) {
469 latestActive = current;
470 latestStartNs = current->startTimeNs;
471 }
Eric Laurent1ff16a72019-03-14 18:35:04 -0700472 if (isPrivacySensitiveSource(current->attributes.source)) {
473 if (current->startTimeNs > latestSensitiveStartNs) {
474 latestSensitiveActive = current;
475 latestSensitiveStartNs = current->startTimeNs;
476 }
477 isSensitiveActive = true;
478 }
Eric Laurent4eb58f12018-12-07 16:41:02 -0800479 }
480
Eric Laurent1ff16a72019-03-14 18:35:04 -0700481 // if no active client with UI on Top, consider latest active as top
482 if (topActive == nullptr) {
483 topActive = latestActive;
Eric Laurent4eb58f12018-12-07 16:41:02 -0800484 }
485
Eric Laurent1ff16a72019-03-14 18:35:04 -0700486 std::vector<uid_t> enabledUids;
Eric Laurenta46bedb2018-12-07 18:01:26 -0800487
Eric Laurent4eb58f12018-12-07 16:41:02 -0800488 for (size_t i =0; i < mAudioRecordClients.size(); i++) {
489 sp<AudioRecordClient> current = mAudioRecordClients[i];
Eric Laurent1ff16a72019-03-14 18:35:04 -0700490 if (!current->active) {
491 continue;
492 }
493
494 // keep capture allowed if another client with the same UID has already
495 // been allowed to capture
496 if (std::find(enabledUids.begin(), enabledUids.end(), current->uid)
497 != enabledUids.end()) {
498 continue;
499 }
Eric Laurent4eb58f12018-12-07 16:41:02 -0800500
501 audio_source_t source = current->attributes.source;
Eric Laurent1ff16a72019-03-14 18:35:04 -0700502 bool isTopOrLatestActive = topActive == nullptr ? false : current->uid == topActive->uid;
503 bool isLatestSensitive = latestSensitiveActive == nullptr ?
504 false : current->uid == latestSensitiveActive->uid;
505
506 // By default allow capture if:
507 // The assistant is not on TOP
508 // AND there is no active privacy sensitive capture or call
509 // OR client has CAPTURE_AUDIO_OUTPUT privileged permission
510 bool allowCapture = !isAssistantOnTop
511 && !(isSensitiveActive && !(isLatestSensitive || current->canCaptureOutput))
512 && !(isInCall && !current->canCaptureOutput);
Eric Laurent2dc962b2019-03-01 08:25:25 -0800513
514 if (isVirtualSource(source)) {
Eric Laurent1ff16a72019-03-14 18:35:04 -0700515 // Allow capture for virtual (remote submix, call audio TX or RX...) sources
516 allowCapture = true;
Eric Laurent2dc962b2019-03-01 08:25:25 -0800517 } else if (mUidPolicy->isAssistantUid(current->uid)) {
Eric Laurent1ff16a72019-03-14 18:35:04 -0700518 // For assistant allow capture if:
519 // An accessibility service is on TOP
520 // AND the source is VOICE_RECOGNITION or HOTWORD
521 // OR is on TOP OR latest started AND uses VOICE_RECOGNITION
522 // OR uses HOTWORD
523 // AND there is no active privacy sensitive capture or call
524 // OR client has CAPTURE_AUDIO_OUTPUT privileged permission
Eric Laurent4eb58f12018-12-07 16:41:02 -0800525 if (isA11yOnTop) {
526 if (source == AUDIO_SOURCE_HOTWORD || source == AUDIO_SOURCE_VOICE_RECOGNITION) {
Eric Laurent1ff16a72019-03-14 18:35:04 -0700527 allowCapture = true;
Eric Laurent4eb58f12018-12-07 16:41:02 -0800528 }
529 } else {
Eric Laurent1ff16a72019-03-14 18:35:04 -0700530 if (((isTopOrLatestActive && source == AUDIO_SOURCE_VOICE_RECOGNITION) ||
531 source == AUDIO_SOURCE_HOTWORD) &&
532 (!(isSensitiveActive || isInCall) || current->canCaptureOutput)) {
533 allowCapture = true;
Eric Laurent4eb58f12018-12-07 16:41:02 -0800534 }
535 }
536 } else if (mUidPolicy->isA11yUid(current->uid)) {
Eric Laurent1ff16a72019-03-14 18:35:04 -0700537 // For accessibility service allow capture if:
538 // Is on TOP OR latest started
539 // AND the source is VOICE_RECOGNITION or HOTWORD
540 if (isTopOrLatestActive &&
541 (source == AUDIO_SOURCE_VOICE_RECOGNITION || source == AUDIO_SOURCE_HOTWORD)) {
542 allowCapture = true;
Eric Laurent4eb58f12018-12-07 16:41:02 -0800543 }
544 }
545 setAppState_l(current->uid,
Eric Laurent1ff16a72019-03-14 18:35:04 -0700546 allowCapture ? apmStatFromAmState(mUidPolicy->getUidState(current->uid)) :
547 APP_STATE_IDLE);
548 if (allowCapture) {
549 enabledUids.push_back(current->uid);
550 }
Eric Laurente8c8b432018-10-17 10:08:02 -0700551 }
552}
553
Michael Groovercfd28302018-12-11 19:16:46 -0800554void AudioPolicyService::silenceAllRecordings_l() {
555 for (size_t i = 0; i < mAudioRecordClients.size(); i++) {
556 sp<AudioRecordClient> current = mAudioRecordClients[i];
Eric Laurent1ff16a72019-03-14 18:35:04 -0700557 if (!isVirtualSource(current->attributes.source)) {
558 setAppState_l(current->uid, APP_STATE_IDLE);
559 }
Michael Groovercfd28302018-12-11 19:16:46 -0800560 }
561}
562
Eric Laurente8c8b432018-10-17 10:08:02 -0700563/* static */
564app_state_t AudioPolicyService::apmStatFromAmState(int amState) {
Eric Laurent1ff16a72019-03-14 18:35:04 -0700565
566 if (amState == ActivityManager::PROCESS_STATE_UNKNOWN) {
Eric Laurente8c8b432018-10-17 10:08:02 -0700567 return APP_STATE_IDLE;
Eric Laurent1ff16a72019-03-14 18:35:04 -0700568 } else if (amState <= ActivityManager::PROCESS_STATE_TOP) {
569 // include persistent services
570 return APP_STATE_TOP;
Eric Laurente8c8b432018-10-17 10:08:02 -0700571 }
572 return APP_STATE_FOREGROUND;
573}
574
Eric Laurent4eb58f12018-12-07 16:41:02 -0800575/* static */
Eric Laurent2dc962b2019-03-01 08:25:25 -0800576bool AudioPolicyService::isPrivacySensitiveSource(audio_source_t source)
577{
578 switch (source) {
579 case AUDIO_SOURCE_CAMCORDER:
580 case AUDIO_SOURCE_VOICE_COMMUNICATION:
581 return true;
582 default:
583 break;
584 }
585 return false;
586}
587
588/* static */
589bool AudioPolicyService::isVirtualSource(audio_source_t source)
Eric Laurent4eb58f12018-12-07 16:41:02 -0800590{
591 switch (source) {
592 case AUDIO_SOURCE_VOICE_UPLINK:
593 case AUDIO_SOURCE_VOICE_DOWNLINK:
594 case AUDIO_SOURCE_VOICE_CALL:
Eric Laurent2dc962b2019-03-01 08:25:25 -0800595 case AUDIO_SOURCE_REMOTE_SUBMIX:
596 case AUDIO_SOURCE_FM_TUNER:
Eric Laurent4eb58f12018-12-07 16:41:02 -0800597 return true;
598 default:
599 break;
600 }
601 return false;
602}
603
Eric Laurente8c8b432018-10-17 10:08:02 -0700604void AudioPolicyService::setAppState_l(uid_t uid, app_state_t state)
605{
606 AutoCallerClear acc;
607
608 if (mAudioPolicyManager) {
609 mAudioPolicyManager->setAppState(uid, state);
Mikhail Naganoveae73eb2018-04-03 16:57:36 -0700610 }
611 sp<IAudioFlinger> af = AudioSystem::get_audio_flinger();
612 if (af) {
Eric Laurentf32108e2018-10-04 17:22:04 -0700613 bool silenced = state == APP_STATE_IDLE;
Mikhail Naganoveae73eb2018-04-03 16:57:36 -0700614 af->setRecordSilenced(uid, silenced);
615 }
Svet Ganovf4ddfef2018-01-16 07:37:58 -0800616}
617
Glenn Kasten0f11b512014-01-31 16:18:54 -0800618status_t AudioPolicyService::dump(int fd, const Vector<String16>& args __unused)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700619{
Glenn Kasten44deb052012-02-05 18:09:08 -0800620 if (!dumpAllowed()) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700621 dumpPermissionDenial(fd);
622 } else {
Mikhail Naganov959e2d02019-03-28 11:08:19 -0700623 bool locked = dumpTryLock(mLock);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700624 if (!locked) {
625 String8 result(kDeadlockedString);
626 write(fd, result.string(), result.size());
627 }
628
629 dumpInternals(fd);
Glenn Kasten9d1f02d2012-02-08 17:47:58 -0800630 if (mAudioCommandThread != 0) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700631 mAudioCommandThread->dump(fd);
632 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700633
Eric Laurentdce54a12014-03-10 12:19:46 -0700634 if (mAudioPolicyManager) {
635 mAudioPolicyManager->dump(fd);
636 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700637
Kevin Rocard8be94972019-02-22 13:26:25 -0800638 mPackageManager.dump(fd);
639
Mathias Agopian65ab4712010-07-14 17:59:35 -0700640 if (locked) mLock.unlock();
641 }
642 return NO_ERROR;
643}
644
645status_t AudioPolicyService::dumpPermissionDenial(int fd)
646{
647 const size_t SIZE = 256;
648 char buffer[SIZE];
649 String8 result;
650 snprintf(buffer, SIZE, "Permission Denial: "
651 "can't dump AudioPolicyService from pid=%d, uid=%d\n",
652 IPCThreadState::self()->getCallingPid(),
653 IPCThreadState::self()->getCallingUid());
654 result.append(buffer);
655 write(fd, result.string(), result.size());
656 return NO_ERROR;
657}
658
659status_t AudioPolicyService::onTransact(
Svet Ganovf4ddfef2018-01-16 07:37:58 -0800660 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags) {
661 switch (code) {
662 case SHELL_COMMAND_TRANSACTION: {
663 int in = data.readFileDescriptor();
664 int out = data.readFileDescriptor();
665 int err = data.readFileDescriptor();
666 int argc = data.readInt32();
667 Vector<String16> args;
668 for (int i = 0; i < argc && data.dataAvail() > 0; i++) {
669 args.add(data.readString16());
670 }
671 sp<IBinder> unusedCallback;
672 sp<IResultReceiver> resultReceiver;
673 status_t status;
674 if ((status = data.readNullableStrongBinder(&unusedCallback)) != NO_ERROR) {
675 return status;
676 }
677 if ((status = data.readNullableStrongBinder(&resultReceiver)) != NO_ERROR) {
678 return status;
679 }
680 status = shellCommand(in, out, err, args);
681 if (resultReceiver != nullptr) {
682 resultReceiver->send(status);
683 }
684 return NO_ERROR;
685 }
686 }
687
Mathias Agopian65ab4712010-07-14 17:59:35 -0700688 return BnAudioPolicyService::onTransact(code, data, reply, flags);
689}
690
Svet Ganovf4ddfef2018-01-16 07:37:58 -0800691// ------------------- Shell command implementation -------------------
692
693// NOTE: This is a remote API - make sure all args are validated
694status_t AudioPolicyService::shellCommand(int in, int out, int err, Vector<String16>& args) {
695 if (!checkCallingPermission(sManageAudioPolicyPermission, nullptr, nullptr)) {
696 return PERMISSION_DENIED;
697 }
698 if (in == BAD_TYPE || out == BAD_TYPE || err == BAD_TYPE) {
699 return BAD_VALUE;
700 }
701 if (args.size() == 3 && args[0] == String16("set-uid-state")) {
702 return handleSetUidState(args, err);
703 } else if (args.size() == 2 && args[0] == String16("reset-uid-state")) {
704 return handleResetUidState(args, err);
705 } else if (args.size() == 2 && args[0] == String16("get-uid-state")) {
706 return handleGetUidState(args, out, err);
707 } else if (args.size() == 1 && args[0] == String16("help")) {
708 printHelp(out);
709 return NO_ERROR;
710 }
711 printHelp(err);
712 return BAD_VALUE;
713}
714
715status_t AudioPolicyService::handleSetUidState(Vector<String16>& args, int err) {
716 PermissionController pc;
717 int uid = pc.getPackageUid(args[1], 0);
718 if (uid <= 0) {
719 ALOGE("Unknown package: '%s'", String8(args[1]).string());
720 dprintf(err, "Unknown package: '%s'\n", String8(args[1]).string());
721 return BAD_VALUE;
722 }
723 bool active = false;
724 if (args[2] == String16("active")) {
725 active = true;
726 } else if ((args[2] != String16("idle"))) {
727 ALOGE("Expected active or idle but got: '%s'", String8(args[2]).string());
728 return BAD_VALUE;
729 }
730 mUidPolicy->addOverrideUid(uid, active);
731 return NO_ERROR;
732}
733
734status_t AudioPolicyService::handleResetUidState(Vector<String16>& args, int err) {
735 PermissionController pc;
736 int uid = pc.getPackageUid(args[1], 0);
737 if (uid < 0) {
738 ALOGE("Unknown package: '%s'", String8(args[1]).string());
739 dprintf(err, "Unknown package: '%s'\n", String8(args[1]).string());
740 return BAD_VALUE;
741 }
742 mUidPolicy->removeOverrideUid(uid);
743 return NO_ERROR;
744}
745
746status_t AudioPolicyService::handleGetUidState(Vector<String16>& args, int out, int err) {
747 PermissionController pc;
748 int uid = pc.getPackageUid(args[1], 0);
749 if (uid < 0) {
750 ALOGE("Unknown package: '%s'", String8(args[1]).string());
751 dprintf(err, "Unknown package: '%s'\n", String8(args[1]).string());
752 return BAD_VALUE;
753 }
754 if (mUidPolicy->isUidActive(uid)) {
755 return dprintf(out, "active\n");
756 } else {
757 return dprintf(out, "idle\n");
758 }
759}
760
761status_t AudioPolicyService::printHelp(int out) {
762 return dprintf(out, "Audio policy service commands:\n"
763 " get-uid-state <PACKAGE> gets the uid state\n"
764 " set-uid-state <PACKAGE> <active|idle> overrides the uid state\n"
765 " reset-uid-state <PACKAGE> clears the uid state override\n"
766 " help print this message\n");
767}
768
769// ----------- AudioPolicyService::UidPolicy implementation ----------
770
771void AudioPolicyService::UidPolicy::registerSelf() {
772 ActivityManager am;
773 am.registerUidObserver(this, ActivityManager::UID_OBSERVER_GONE
774 | ActivityManager::UID_OBSERVER_IDLE
Eric Laurente8c8b432018-10-17 10:08:02 -0700775 | ActivityManager::UID_OBSERVER_ACTIVE
776 | ActivityManager::UID_OBSERVER_PROCSTATE,
Svet Ganovf4ddfef2018-01-16 07:37:58 -0800777 ActivityManager::PROCESS_STATE_UNKNOWN,
778 String16("audioserver"));
Mikhail Naganoveae73eb2018-04-03 16:57:36 -0700779 status_t res = am.linkToDeath(this);
780 if (!res) {
781 Mutex::Autolock _l(mLock);
782 mObserverRegistered = true;
783 } else {
784 ALOGE("UidPolicy::registerSelf linkToDeath failed: %d", res);
Eric Laurent4eb58f12018-12-07 16:41:02 -0800785
Mikhail Naganoveae73eb2018-04-03 16:57:36 -0700786 am.unregisterUidObserver(this);
787 }
Svet Ganovf4ddfef2018-01-16 07:37:58 -0800788}
789
790void AudioPolicyService::UidPolicy::unregisterSelf() {
791 ActivityManager am;
Mikhail Naganoveae73eb2018-04-03 16:57:36 -0700792 am.unlinkToDeath(this);
Svet Ganovf4ddfef2018-01-16 07:37:58 -0800793 am.unregisterUidObserver(this);
Mikhail Naganoveae73eb2018-04-03 16:57:36 -0700794 Mutex::Autolock _l(mLock);
795 mObserverRegistered = false;
Svet Ganovf4ddfef2018-01-16 07:37:58 -0800796}
797
Mikhail Naganoveae73eb2018-04-03 16:57:36 -0700798void AudioPolicyService::UidPolicy::binderDied(__unused const wp<IBinder> &who) {
799 Mutex::Autolock _l(mLock);
800 mCachedUids.clear();
801 mObserverRegistered = false;
Svet Ganovf4ddfef2018-01-16 07:37:58 -0800802}
803
Eric Laurente8c8b432018-10-17 10:08:02 -0700804void AudioPolicyService::UidPolicy::checkRegistered() {
Mikhail Naganoveae73eb2018-04-03 16:57:36 -0700805 bool needToReregister = false;
806 {
807 Mutex::Autolock _l(mLock);
808 needToReregister = !mObserverRegistered;
Svet Ganovf4ddfef2018-01-16 07:37:58 -0800809 }
Mikhail Naganoveae73eb2018-04-03 16:57:36 -0700810 if (needToReregister) {
811 // Looks like ActivityManager has died previously, attempt to re-register.
812 registerSelf();
813 }
Eric Laurente8c8b432018-10-17 10:08:02 -0700814}
815
816bool AudioPolicyService::UidPolicy::isUidActive(uid_t uid) {
817 if (isServiceUid(uid)) return true;
818 checkRegistered();
Mikhail Naganoveae73eb2018-04-03 16:57:36 -0700819 {
820 Mutex::Autolock _l(mLock);
821 auto overrideIter = mOverrideUids.find(uid);
822 if (overrideIter != mOverrideUids.end()) {
Eric Laurente8c8b432018-10-17 10:08:02 -0700823 return overrideIter->second.first;
Mikhail Naganoveae73eb2018-04-03 16:57:36 -0700824 }
825 // In an absense of the ActivityManager, assume everything to be active.
826 if (!mObserverRegistered) return true;
827 auto cacheIter = mCachedUids.find(uid);
Mikhail Naganoveba668a2018-04-05 08:13:15 -0700828 if (cacheIter != mCachedUids.end()) {
Eric Laurente8c8b432018-10-17 10:08:02 -0700829 return cacheIter->second.first;
Mikhail Naganoveae73eb2018-04-03 16:57:36 -0700830 }
831 }
832 ActivityManager am;
833 bool active = am.isUidActive(uid, String16("audioserver"));
834 {
835 Mutex::Autolock _l(mLock);
Eric Laurente8c8b432018-10-17 10:08:02 -0700836 mCachedUids.insert(std::pair<uid_t,
837 std::pair<bool, int>>(uid, std::pair<bool, int>(active,
838 ActivityManager::PROCESS_STATE_UNKNOWN)));
Mikhail Naganoveae73eb2018-04-03 16:57:36 -0700839 }
840 return active;
Svet Ganovf4ddfef2018-01-16 07:37:58 -0800841}
842
Eric Laurente8c8b432018-10-17 10:08:02 -0700843int AudioPolicyService::UidPolicy::getUidState(uid_t uid) {
844 if (isServiceUid(uid)) {
845 return ActivityManager::PROCESS_STATE_TOP;
846 }
847 checkRegistered();
848 {
849 Mutex::Autolock _l(mLock);
850 auto overrideIter = mOverrideUids.find(uid);
851 if (overrideIter != mOverrideUids.end()) {
852 if (overrideIter->second.first) {
853 if (overrideIter->second.second != ActivityManager::PROCESS_STATE_UNKNOWN) {
854 return overrideIter->second.second;
855 } else {
856 auto cacheIter = mCachedUids.find(uid);
857 if (cacheIter != mCachedUids.end()) {
858 return cacheIter->second.second;
859 }
860 }
861 }
862 return ActivityManager::PROCESS_STATE_UNKNOWN;
863 }
864 // In an absense of the ActivityManager, assume everything to be active.
865 if (!mObserverRegistered) {
866 return ActivityManager::PROCESS_STATE_TOP;
867 }
868 auto cacheIter = mCachedUids.find(uid);
869 if (cacheIter != mCachedUids.end()) {
870 if (cacheIter->second.first) {
871 return cacheIter->second.second;
872 } else {
873 return ActivityManager::PROCESS_STATE_UNKNOWN;
874 }
875 }
876 }
877 ActivityManager am;
878 bool active = am.isUidActive(uid, String16("audioserver"));
879 int state = ActivityManager::PROCESS_STATE_UNKNOWN;
880 if (active) {
881 state = am.getUidProcessState(uid, String16("audioserver"));
882 }
883 {
884 Mutex::Autolock _l(mLock);
885 mCachedUids.insert(std::pair<uid_t,
886 std::pair<bool, int>>(uid, std::pair<bool, int>(active, state)));
887 }
Eric Laurent4eb58f12018-12-07 16:41:02 -0800888
Eric Laurente8c8b432018-10-17 10:08:02 -0700889 return state;
890}
891
Mikhail Naganoveae73eb2018-04-03 16:57:36 -0700892void AudioPolicyService::UidPolicy::onUidActive(uid_t uid) {
Eric Laurente8c8b432018-10-17 10:08:02 -0700893 updateUid(&mCachedUids, uid, true, ActivityManager::PROCESS_STATE_UNKNOWN, true);
Mikhail Naganoveae73eb2018-04-03 16:57:36 -0700894}
895
896void AudioPolicyService::UidPolicy::onUidGone(uid_t uid, __unused bool disabled) {
Eric Laurente8c8b432018-10-17 10:08:02 -0700897 updateUid(&mCachedUids, uid, false, ActivityManager::PROCESS_STATE_UNKNOWN, false);
Mikhail Naganoveae73eb2018-04-03 16:57:36 -0700898}
899
900void AudioPolicyService::UidPolicy::onUidIdle(uid_t uid, __unused bool disabled) {
Eric Laurente8c8b432018-10-17 10:08:02 -0700901 updateUid(&mCachedUids, uid, false, ActivityManager::PROCESS_STATE_UNKNOWN, true);
Mikhail Naganoveae73eb2018-04-03 16:57:36 -0700902}
903
Eric Laurente8c8b432018-10-17 10:08:02 -0700904void AudioPolicyService::UidPolicy::onUidStateChanged(uid_t uid,
905 int32_t procState,
906 int64_t procStateSeq __unused) {
907 if (procState != ActivityManager::PROCESS_STATE_UNKNOWN) {
908 updateUid(&mCachedUids, uid, true, procState, true);
Mikhail Naganoveae73eb2018-04-03 16:57:36 -0700909 }
910}
911
912void AudioPolicyService::UidPolicy::updateOverrideUid(uid_t uid, bool active, bool insert) {
Eric Laurente8c8b432018-10-17 10:08:02 -0700913 updateUid(&mOverrideUids, uid, active, ActivityManager::PROCESS_STATE_UNKNOWN, insert);
914}
915
916void AudioPolicyService::UidPolicy::notifyService() {
917 sp<AudioPolicyService> service = mService.promote();
918 if (service != nullptr) {
919 service->updateUidStates();
Mikhail Naganoveae73eb2018-04-03 16:57:36 -0700920 }
921}
922
Eric Laurente8c8b432018-10-17 10:08:02 -0700923void AudioPolicyService::UidPolicy::updateUid(std::unordered_map<uid_t,
924 std::pair<bool, int>> *uids,
925 uid_t uid,
926 bool active,
927 int state,
928 bool insert) {
929 if (isServiceUid(uid)) {
930 return;
931 }
932 bool wasActive = isUidActive(uid);
933 int previousState = getUidState(uid);
Mikhail Naganoveae73eb2018-04-03 16:57:36 -0700934 {
935 Mutex::Autolock _l(mLock);
Eric Laurente8c8b432018-10-17 10:08:02 -0700936 updateUidLocked(uids, uid, active, state, insert);
Mikhail Naganoveae73eb2018-04-03 16:57:36 -0700937 }
Eric Laurente8c8b432018-10-17 10:08:02 -0700938 if (wasActive != isUidActive(uid) || state != previousState) {
939 notifyService();
940 }
Mikhail Naganoveae73eb2018-04-03 16:57:36 -0700941}
942
Eric Laurente8c8b432018-10-17 10:08:02 -0700943void AudioPolicyService::UidPolicy::updateUidLocked(std::unordered_map<uid_t,
944 std::pair<bool, int>> *uids,
945 uid_t uid,
946 bool active,
947 int state,
948 bool insert) {
Mikhail Naganoveae73eb2018-04-03 16:57:36 -0700949 auto it = uids->find(uid);
950 if (it != uids->end()) {
Mikhail Naganoveae73eb2018-04-03 16:57:36 -0700951 if (insert) {
Eric Laurente8c8b432018-10-17 10:08:02 -0700952 if (state == ActivityManager::PROCESS_STATE_UNKNOWN) {
953 it->second.first = active;
954 }
955 if (it->second.first) {
956 it->second.second = state;
957 } else {
958 it->second.second = ActivityManager::PROCESS_STATE_UNKNOWN;
959 }
Mikhail Naganoveae73eb2018-04-03 16:57:36 -0700960 } else {
961 uids->erase(it);
962 }
Eric Laurente8c8b432018-10-17 10:08:02 -0700963 } else if (insert && (state == ActivityManager::PROCESS_STATE_UNKNOWN)) {
964 uids->insert(std::pair<uid_t, std::pair<bool, int>>(uid,
965 std::pair<bool, int>(active, state)));
Mikhail Naganoveae73eb2018-04-03 16:57:36 -0700966 }
Svet Ganovf4ddfef2018-01-16 07:37:58 -0800967}
Mathias Agopian65ab4712010-07-14 17:59:35 -0700968
Eric Laurent4eb58f12018-12-07 16:41:02 -0800969bool AudioPolicyService::UidPolicy::isA11yOnTop() {
970 for (const auto &uid : mCachedUids) {
971 std::vector<uid_t>::iterator it = find(mA11yUids.begin(), mA11yUids.end(), uid.first);
972 if (it == mA11yUids.end()) {
973 continue;
974 }
Amith Yamasanibcbb3002019-01-23 13:53:33 -0800975 if (uid.second.second >= ActivityManager::PROCESS_STATE_TOP
976 && uid.second.second <= ActivityManager::PROCESS_STATE_BOUND_FOREGROUND_SERVICE) {
Eric Laurent4eb58f12018-12-07 16:41:02 -0800977 return true;
978 }
979 }
980 return false;
981}
982
Eric Laurentb78763e2018-10-17 10:08:02 -0700983bool AudioPolicyService::UidPolicy::isA11yUid(uid_t uid)
984{
985 std::vector<uid_t>::iterator it = find(mA11yUids.begin(), mA11yUids.end(), uid);
986 return it != mA11yUids.end();
987}
988
Michael Groovercfd28302018-12-11 19:16:46 -0800989// ----------- AudioPolicyService::SensorPrivacyService implementation ----------
990void AudioPolicyService::SensorPrivacyPolicy::registerSelf() {
991 SensorPrivacyManager spm;
992 mSensorPrivacyEnabled = spm.isSensorPrivacyEnabled();
993 spm.addSensorPrivacyListener(this);
994}
995
996void AudioPolicyService::SensorPrivacyPolicy::unregisterSelf() {
997 SensorPrivacyManager spm;
998 spm.removeSensorPrivacyListener(this);
999}
1000
1001bool AudioPolicyService::SensorPrivacyPolicy::isSensorPrivacyEnabled() {
1002 return mSensorPrivacyEnabled;
1003}
1004
1005binder::Status AudioPolicyService::SensorPrivacyPolicy::onSensorPrivacyChanged(bool enabled) {
1006 mSensorPrivacyEnabled = enabled;
1007 sp<AudioPolicyService> service = mService.promote();
1008 if (service != nullptr) {
1009 service->updateUidStates();
1010 }
1011 return binder::Status::ok();
1012}
1013
Mathias Agopian65ab4712010-07-14 17:59:35 -07001014// ----------- AudioPolicyService::AudioCommandThread implementation ----------
1015
Eric Laurentbfb1b832013-01-07 09:53:42 -08001016AudioPolicyService::AudioCommandThread::AudioCommandThread(String8 name,
1017 const wp<AudioPolicyService>& service)
1018 : Thread(false), mName(name), mService(service)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001019{
Mathias Agopian65ab4712010-07-14 17:59:35 -07001020}
1021
1022
1023AudioPolicyService::AudioCommandThread::~AudioCommandThread()
1024{
Eric Laurentbfb1b832013-01-07 09:53:42 -08001025 if (!mAudioCommands.isEmpty()) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07001026 release_wake_lock(mName.string());
1027 }
1028 mAudioCommands.clear();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001029}
1030
1031void AudioPolicyService::AudioCommandThread::onFirstRef()
1032{
Eric Laurentbfb1b832013-01-07 09:53:42 -08001033 run(mName.string(), ANDROID_PRIORITY_AUDIO);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001034}
1035
1036bool AudioPolicyService::AudioCommandThread::threadLoop()
1037{
Eric Laurentd7eda8d2016-02-02 17:18:39 -08001038 nsecs_t waitTime = -1;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001039
1040 mLock.lock();
1041 while (!exitPending())
1042 {
Eric Laurent59a89232014-06-08 14:14:17 -07001043 sp<AudioPolicyService> svc;
1044 while (!mAudioCommands.isEmpty() && !exitPending()) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07001045 nsecs_t curTime = systemTime();
1046 // commands are sorted by increasing time stamp: execute them from index 0 and up
1047 if (mAudioCommands[0]->mTime <= curTime) {
Eric Laurent0ede8922014-05-09 18:04:42 -07001048 sp<AudioCommand> command = mAudioCommands[0];
Mathias Agopian65ab4712010-07-14 17:59:35 -07001049 mAudioCommands.removeAt(0);
Eric Laurent0ede8922014-05-09 18:04:42 -07001050 mLastCommand = command;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001051
1052 switch (command->mCommand) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07001053 case SET_VOLUME: {
Eric Laurent0ede8922014-05-09 18:04:42 -07001054 VolumeData *data = (VolumeData *)command->mParam.get();
Steve Block3856b092011-10-20 11:56:00 +01001055 ALOGV("AudioCommandThread() processing set volume stream %d, \
Eric Laurentde070132010-07-13 04:45:46 -07001056 volume %f, output %d", data->mStream, data->mVolume, data->mIO);
Andy Hungfe726a62018-09-27 15:17:25 -07001057 mLock.unlock();
Eric Laurentde070132010-07-13 04:45:46 -07001058 command->mStatus = AudioSystem::setStreamVolume(data->mStream,
1059 data->mVolume,
1060 data->mIO);
Andy Hungfe726a62018-09-27 15:17:25 -07001061 mLock.lock();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001062 }break;
1063 case SET_PARAMETERS: {
Eric Laurent0ede8922014-05-09 18:04:42 -07001064 ParametersData *data = (ParametersData *)command->mParam.get();
Glenn Kastene53b9ea2012-03-12 16:29:55 -07001065 ALOGV("AudioCommandThread() processing set parameters string %s, io %d",
1066 data->mKeyValuePairs.string(), data->mIO);
Andy Hungfe726a62018-09-27 15:17:25 -07001067 mLock.unlock();
Glenn Kastene53b9ea2012-03-12 16:29:55 -07001068 command->mStatus = AudioSystem::setParameters(data->mIO, data->mKeyValuePairs);
Andy Hungfe726a62018-09-27 15:17:25 -07001069 mLock.lock();
Glenn Kastene53b9ea2012-03-12 16:29:55 -07001070 }break;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001071 case SET_VOICE_VOLUME: {
Eric Laurent0ede8922014-05-09 18:04:42 -07001072 VoiceVolumeData *data = (VoiceVolumeData *)command->mParam.get();
Steve Block3856b092011-10-20 11:56:00 +01001073 ALOGV("AudioCommandThread() processing set voice volume volume %f",
Eric Laurentde070132010-07-13 04:45:46 -07001074 data->mVolume);
Andy Hungfe726a62018-09-27 15:17:25 -07001075 mLock.unlock();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001076 command->mStatus = AudioSystem::setVoiceVolume(data->mVolume);
Andy Hungfe726a62018-09-27 15:17:25 -07001077 mLock.lock();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001078 }break;
Eric Laurentbfb1b832013-01-07 09:53:42 -08001079 case STOP_OUTPUT: {
Eric Laurent0ede8922014-05-09 18:04:42 -07001080 StopOutputData *data = (StopOutputData *)command->mParam.get();
Eric Laurentd7fe0862018-07-14 16:48:01 -07001081 ALOGV("AudioCommandThread() processing stop output portId %d",
1082 data->mPortId);
Eric Laurent59a89232014-06-08 14:14:17 -07001083 svc = mService.promote();
Eric Laurentbfb1b832013-01-07 09:53:42 -08001084 if (svc == 0) {
1085 break;
1086 }
1087 mLock.unlock();
Eric Laurentd7fe0862018-07-14 16:48:01 -07001088 svc->doStopOutput(data->mPortId);
Eric Laurentbfb1b832013-01-07 09:53:42 -08001089 mLock.lock();
Eric Laurentbfb1b832013-01-07 09:53:42 -08001090 }break;
1091 case RELEASE_OUTPUT: {
Eric Laurent0ede8922014-05-09 18:04:42 -07001092 ReleaseOutputData *data = (ReleaseOutputData *)command->mParam.get();
Eric Laurentd7fe0862018-07-14 16:48:01 -07001093 ALOGV("AudioCommandThread() processing release output portId %d",
1094 data->mPortId);
Eric Laurent59a89232014-06-08 14:14:17 -07001095 svc = mService.promote();
Eric Laurentbfb1b832013-01-07 09:53:42 -08001096 if (svc == 0) {
1097 break;
1098 }
1099 mLock.unlock();
Eric Laurentd7fe0862018-07-14 16:48:01 -07001100 svc->doReleaseOutput(data->mPortId);
Eric Laurentbfb1b832013-01-07 09:53:42 -08001101 mLock.lock();
Eric Laurentbfb1b832013-01-07 09:53:42 -08001102 }break;
Eric Laurent951f4552014-05-20 10:48:17 -07001103 case CREATE_AUDIO_PATCH: {
1104 CreateAudioPatchData *data = (CreateAudioPatchData *)command->mParam.get();
1105 ALOGV("AudioCommandThread() processing create audio patch");
1106 sp<IAudioFlinger> af = AudioSystem::get_audio_flinger();
1107 if (af == 0) {
1108 command->mStatus = PERMISSION_DENIED;
1109 } else {
Andy Hungfe726a62018-09-27 15:17:25 -07001110 mLock.unlock();
Eric Laurent951f4552014-05-20 10:48:17 -07001111 command->mStatus = af->createAudioPatch(&data->mPatch, &data->mHandle);
Andy Hungfe726a62018-09-27 15:17:25 -07001112 mLock.lock();
Eric Laurent951f4552014-05-20 10:48:17 -07001113 }
1114 } break;
1115 case RELEASE_AUDIO_PATCH: {
1116 ReleaseAudioPatchData *data = (ReleaseAudioPatchData *)command->mParam.get();
1117 ALOGV("AudioCommandThread() processing release audio patch");
1118 sp<IAudioFlinger> af = AudioSystem::get_audio_flinger();
1119 if (af == 0) {
1120 command->mStatus = PERMISSION_DENIED;
1121 } else {
Andy Hungfe726a62018-09-27 15:17:25 -07001122 mLock.unlock();
Eric Laurent951f4552014-05-20 10:48:17 -07001123 command->mStatus = af->releaseAudioPatch(data->mHandle);
Andy Hungfe726a62018-09-27 15:17:25 -07001124 mLock.lock();
Eric Laurent951f4552014-05-20 10:48:17 -07001125 }
1126 } break;
Eric Laurentb52c1522014-05-20 11:27:36 -07001127 case UPDATE_AUDIOPORT_LIST: {
1128 ALOGV("AudioCommandThread() processing update audio port list");
Eric Laurent59a89232014-06-08 14:14:17 -07001129 svc = mService.promote();
Eric Laurentb52c1522014-05-20 11:27:36 -07001130 if (svc == 0) {
1131 break;
1132 }
1133 mLock.unlock();
1134 svc->doOnAudioPortListUpdate();
1135 mLock.lock();
1136 }break;
1137 case UPDATE_AUDIOPATCH_LIST: {
1138 ALOGV("AudioCommandThread() processing update audio patch list");
Eric Laurent59a89232014-06-08 14:14:17 -07001139 svc = mService.promote();
Eric Laurentb52c1522014-05-20 11:27:36 -07001140 if (svc == 0) {
1141 break;
1142 }
1143 mLock.unlock();
1144 svc->doOnAudioPatchListUpdate();
1145 mLock.lock();
1146 }break;
François Gaffiecfe17322018-11-07 13:41:29 +01001147 case CHANGED_AUDIOVOLUMEGROUP: {
1148 AudioVolumeGroupData *data =
1149 static_cast<AudioVolumeGroupData *>(command->mParam.get());
1150 ALOGV("AudioCommandThread() processing update audio volume group");
1151 svc = mService.promote();
1152 if (svc == 0) {
1153 break;
1154 }
1155 mLock.unlock();
1156 svc->doOnAudioVolumeGroupChanged(data->mGroup, data->mFlags);
1157 mLock.lock();
1158 }break;
Eric Laurente1715a42014-05-20 11:30:42 -07001159 case SET_AUDIOPORT_CONFIG: {
1160 SetAudioPortConfigData *data = (SetAudioPortConfigData *)command->mParam.get();
1161 ALOGV("AudioCommandThread() processing set port config");
1162 sp<IAudioFlinger> af = AudioSystem::get_audio_flinger();
1163 if (af == 0) {
1164 command->mStatus = PERMISSION_DENIED;
1165 } else {
Andy Hungfe726a62018-09-27 15:17:25 -07001166 mLock.unlock();
Eric Laurente1715a42014-05-20 11:30:42 -07001167 command->mStatus = af->setAudioPortConfig(&data->mConfig);
Andy Hungfe726a62018-09-27 15:17:25 -07001168 mLock.lock();
Eric Laurente1715a42014-05-20 11:30:42 -07001169 }
1170 } break;
Jean-Michel Trivide801052015-04-14 19:10:14 -07001171 case DYN_POLICY_MIX_STATE_UPDATE: {
1172 DynPolicyMixStateUpdateData *data =
1173 (DynPolicyMixStateUpdateData *)command->mParam.get();
Jean-Michel Trivide801052015-04-14 19:10:14 -07001174 ALOGV("AudioCommandThread() processing dyn policy mix state update %s %d",
1175 data->mRegId.string(), data->mState);
1176 svc = mService.promote();
1177 if (svc == 0) {
1178 break;
1179 }
1180 mLock.unlock();
1181 svc->doOnDynamicPolicyMixStateUpdate(data->mRegId, data->mState);
1182 mLock.lock();
1183 } break;
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -08001184 case RECORDING_CONFIGURATION_UPDATE: {
1185 RecordingConfigurationUpdateData *data =
1186 (RecordingConfigurationUpdateData *)command->mParam.get();
1187 ALOGV("AudioCommandThread() processing recording configuration update");
1188 svc = mService.promote();
1189 if (svc == 0) {
1190 break;
1191 }
1192 mLock.unlock();
Jean-Michel Triviac4e4292016-12-22 11:39:31 -08001193 svc->doOnRecordingConfigurationUpdate(data->mEvent, &data->mClientInfo,
Eric Laurenta9f86652018-11-28 17:23:11 -08001194 &data->mClientConfig, data->mClientEffects,
1195 &data->mDeviceConfig, data->mEffects,
1196 data->mPatchHandle, data->mSource);
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -08001197 mLock.lock();
1198 } break;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001199 default:
Steve Block5ff1dd52012-01-05 23:22:43 +00001200 ALOGW("AudioCommandThread() unknown command %d", command->mCommand);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001201 }
Eric Laurent0ede8922014-05-09 18:04:42 -07001202 {
1203 Mutex::Autolock _l(command->mLock);
1204 if (command->mWaitStatus) {
1205 command->mWaitStatus = false;
1206 command->mCond.signal();
1207 }
1208 }
Eric Laurentd7eda8d2016-02-02 17:18:39 -08001209 waitTime = -1;
Zach Janga754b4f2015-10-27 01:29:34 +00001210 // release mLock before releasing strong reference on the service as
1211 // AudioPolicyService destructor calls AudioCommandThread::exit() which
1212 // acquires mLock.
1213 mLock.unlock();
1214 svc.clear();
1215 mLock.lock();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001216 } else {
1217 waitTime = mAudioCommands[0]->mTime - curTime;
1218 break;
1219 }
1220 }
Zach Janga754b4f2015-10-27 01:29:34 +00001221
1222 // release delayed commands wake lock if the queue is empty
1223 if (mAudioCommands.isEmpty()) {
Ricardo Garcia05f2fdc2014-07-24 15:48:24 -07001224 release_wake_lock(mName.string());
Zach Janga754b4f2015-10-27 01:29:34 +00001225 }
1226
1227 // At this stage we have either an empty command queue or the first command in the queue
1228 // has a finite delay. So unless we are exiting it is safe to wait.
1229 if (!exitPending()) {
Eric Laurent59a89232014-06-08 14:14:17 -07001230 ALOGV("AudioCommandThread() going to sleep");
Eric Laurentd7eda8d2016-02-02 17:18:39 -08001231 if (waitTime == -1) {
1232 mWaitWorkCV.wait(mLock);
1233 } else {
1234 mWaitWorkCV.waitRelative(mLock, waitTime);
1235 }
Eric Laurent59a89232014-06-08 14:14:17 -07001236 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001237 }
Ricardo Garcia05f2fdc2014-07-24 15:48:24 -07001238 // release delayed commands wake lock before quitting
1239 if (!mAudioCommands.isEmpty()) {
1240 release_wake_lock(mName.string());
1241 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001242 mLock.unlock();
1243 return false;
1244}
1245
1246status_t AudioPolicyService::AudioCommandThread::dump(int fd)
1247{
1248 const size_t SIZE = 256;
1249 char buffer[SIZE];
1250 String8 result;
1251
1252 snprintf(buffer, SIZE, "AudioCommandThread %p Dump\n", this);
1253 result.append(buffer);
1254 write(fd, result.string(), result.size());
1255
Mikhail Naganov959e2d02019-03-28 11:08:19 -07001256 bool locked = dumpTryLock(mLock);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001257 if (!locked) {
1258 String8 result2(kCmdDeadlockedString);
1259 write(fd, result2.string(), result2.size());
1260 }
1261
1262 snprintf(buffer, SIZE, "- Commands:\n");
1263 result = String8(buffer);
1264 result.append(" Command Time Wait pParam\n");
Glenn Kasten8d6a2442012-02-08 14:04:28 -08001265 for (size_t i = 0; i < mAudioCommands.size(); i++) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07001266 mAudioCommands[i]->dump(buffer, SIZE);
1267 result.append(buffer);
1268 }
1269 result.append(" Last Command\n");
Eric Laurent0ede8922014-05-09 18:04:42 -07001270 if (mLastCommand != 0) {
1271 mLastCommand->dump(buffer, SIZE);
1272 result.append(buffer);
1273 } else {
1274 result.append(" none\n");
1275 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001276
1277 write(fd, result.string(), result.size());
1278
1279 if (locked) mLock.unlock();
1280
1281 return NO_ERROR;
1282}
1283
Glenn Kastenfff6d712012-01-12 16:38:12 -08001284status_t AudioPolicyService::AudioCommandThread::volumeCommand(audio_stream_type_t stream,
Eric Laurentde070132010-07-13 04:45:46 -07001285 float volume,
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001286 audio_io_handle_t output,
Eric Laurentde070132010-07-13 04:45:46 -07001287 int delayMs)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001288{
Eric Laurent0ede8922014-05-09 18:04:42 -07001289 sp<AudioCommand> command = new AudioCommand();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001290 command->mCommand = SET_VOLUME;
Eric Laurent0ede8922014-05-09 18:04:42 -07001291 sp<VolumeData> data = new VolumeData();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001292 data->mStream = stream;
1293 data->mVolume = volume;
1294 data->mIO = output;
1295 command->mParam = data;
Eric Laurent0ede8922014-05-09 18:04:42 -07001296 command->mWaitStatus = true;
Steve Block3856b092011-10-20 11:56:00 +01001297 ALOGV("AudioCommandThread() adding set volume stream %d, volume %f, output %d",
Eric Laurentde070132010-07-13 04:45:46 -07001298 stream, volume, output);
Eric Laurent0ede8922014-05-09 18:04:42 -07001299 return sendCommand(command, delayMs);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001300}
1301
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001302status_t AudioPolicyService::AudioCommandThread::parametersCommand(audio_io_handle_t ioHandle,
Dima Zavinfce7a472011-04-19 22:30:36 -07001303 const char *keyValuePairs,
Eric Laurentde070132010-07-13 04:45:46 -07001304 int delayMs)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001305{
Eric Laurent0ede8922014-05-09 18:04:42 -07001306 sp<AudioCommand> command = new AudioCommand();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001307 command->mCommand = SET_PARAMETERS;
Eric Laurent0ede8922014-05-09 18:04:42 -07001308 sp<ParametersData> data = new ParametersData();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001309 data->mIO = ioHandle;
Dima Zavinfce7a472011-04-19 22:30:36 -07001310 data->mKeyValuePairs = String8(keyValuePairs);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001311 command->mParam = data;
Eric Laurent0ede8922014-05-09 18:04:42 -07001312 command->mWaitStatus = true;
Steve Block3856b092011-10-20 11:56:00 +01001313 ALOGV("AudioCommandThread() adding set parameter string %s, io %d ,delay %d",
Dima Zavinfce7a472011-04-19 22:30:36 -07001314 keyValuePairs, ioHandle, delayMs);
Eric Laurent0ede8922014-05-09 18:04:42 -07001315 return sendCommand(command, delayMs);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001316}
1317
1318status_t AudioPolicyService::AudioCommandThread::voiceVolumeCommand(float volume, int delayMs)
1319{
Eric Laurent0ede8922014-05-09 18:04:42 -07001320 sp<AudioCommand> command = new AudioCommand();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001321 command->mCommand = SET_VOICE_VOLUME;
Eric Laurent0ede8922014-05-09 18:04:42 -07001322 sp<VoiceVolumeData> data = new VoiceVolumeData();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001323 data->mVolume = volume;
1324 command->mParam = data;
Eric Laurent0ede8922014-05-09 18:04:42 -07001325 command->mWaitStatus = true;
Steve Block3856b092011-10-20 11:56:00 +01001326 ALOGV("AudioCommandThread() adding set voice volume volume %f", volume);
Eric Laurent0ede8922014-05-09 18:04:42 -07001327 return sendCommand(command, delayMs);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001328}
1329
Eric Laurentd7fe0862018-07-14 16:48:01 -07001330void AudioPolicyService::AudioCommandThread::stopOutputCommand(audio_port_handle_t portId)
Eric Laurentbfb1b832013-01-07 09:53:42 -08001331{
Eric Laurent0ede8922014-05-09 18:04:42 -07001332 sp<AudioCommand> command = new AudioCommand();
Eric Laurentbfb1b832013-01-07 09:53:42 -08001333 command->mCommand = STOP_OUTPUT;
Eric Laurent0ede8922014-05-09 18:04:42 -07001334 sp<StopOutputData> data = new StopOutputData();
Eric Laurentd7fe0862018-07-14 16:48:01 -07001335 data->mPortId = portId;
Jesper Tragardh48412dc2014-03-24 14:12:43 +01001336 command->mParam = data;
Eric Laurentd7fe0862018-07-14 16:48:01 -07001337 ALOGV("AudioCommandThread() adding stop output portId %d", portId);
Eric Laurent0ede8922014-05-09 18:04:42 -07001338 sendCommand(command);
Eric Laurentbfb1b832013-01-07 09:53:42 -08001339}
1340
Eric Laurentd7fe0862018-07-14 16:48:01 -07001341void AudioPolicyService::AudioCommandThread::releaseOutputCommand(audio_port_handle_t portId)
Eric Laurentbfb1b832013-01-07 09:53:42 -08001342{
Eric Laurent0ede8922014-05-09 18:04:42 -07001343 sp<AudioCommand> command = new AudioCommand();
Eric Laurentbfb1b832013-01-07 09:53:42 -08001344 command->mCommand = RELEASE_OUTPUT;
Eric Laurent0ede8922014-05-09 18:04:42 -07001345 sp<ReleaseOutputData> data = new ReleaseOutputData();
Eric Laurentd7fe0862018-07-14 16:48:01 -07001346 data->mPortId = portId;
Jesper Tragardh48412dc2014-03-24 14:12:43 +01001347 command->mParam = data;
Eric Laurentd7fe0862018-07-14 16:48:01 -07001348 ALOGV("AudioCommandThread() adding release output portId %d", portId);
Eric Laurent0ede8922014-05-09 18:04:42 -07001349 sendCommand(command);
1350}
1351
Eric Laurent951f4552014-05-20 10:48:17 -07001352status_t AudioPolicyService::AudioCommandThread::createAudioPatchCommand(
1353 const struct audio_patch *patch,
1354 audio_patch_handle_t *handle,
1355 int delayMs)
1356{
1357 status_t status = NO_ERROR;
1358
1359 sp<AudioCommand> command = new AudioCommand();
1360 command->mCommand = CREATE_AUDIO_PATCH;
1361 CreateAudioPatchData *data = new CreateAudioPatchData();
1362 data->mPatch = *patch;
1363 data->mHandle = *handle;
1364 command->mParam = data;
1365 command->mWaitStatus = true;
1366 ALOGV("AudioCommandThread() adding create patch delay %d", delayMs);
1367 status = sendCommand(command, delayMs);
1368 if (status == NO_ERROR) {
1369 *handle = data->mHandle;
1370 }
1371 return status;
1372}
1373
1374status_t AudioPolicyService::AudioCommandThread::releaseAudioPatchCommand(audio_patch_handle_t handle,
1375 int delayMs)
1376{
1377 sp<AudioCommand> command = new AudioCommand();
1378 command->mCommand = RELEASE_AUDIO_PATCH;
1379 ReleaseAudioPatchData *data = new ReleaseAudioPatchData();
1380 data->mHandle = handle;
1381 command->mParam = data;
1382 command->mWaitStatus = true;
1383 ALOGV("AudioCommandThread() adding release patch delay %d", delayMs);
1384 return sendCommand(command, delayMs);
1385}
1386
Eric Laurentb52c1522014-05-20 11:27:36 -07001387void AudioPolicyService::AudioCommandThread::updateAudioPortListCommand()
1388{
1389 sp<AudioCommand> command = new AudioCommand();
1390 command->mCommand = UPDATE_AUDIOPORT_LIST;
1391 ALOGV("AudioCommandThread() adding update audio port list");
1392 sendCommand(command);
1393}
1394
1395void AudioPolicyService::AudioCommandThread::updateAudioPatchListCommand()
1396{
1397 sp<AudioCommand>command = new AudioCommand();
1398 command->mCommand = UPDATE_AUDIOPATCH_LIST;
1399 ALOGV("AudioCommandThread() adding update audio patch list");
1400 sendCommand(command);
1401}
1402
François Gaffiecfe17322018-11-07 13:41:29 +01001403void AudioPolicyService::AudioCommandThread::changeAudioVolumeGroupCommand(volume_group_t group,
1404 int flags)
1405{
1406 sp<AudioCommand>command = new AudioCommand();
1407 command->mCommand = CHANGED_AUDIOVOLUMEGROUP;
1408 AudioVolumeGroupData *data= new AudioVolumeGroupData();
1409 data->mGroup = group;
1410 data->mFlags = flags;
1411 command->mParam = data;
1412 ALOGV("AudioCommandThread() adding audio volume group changed");
1413 sendCommand(command);
1414}
1415
Eric Laurente1715a42014-05-20 11:30:42 -07001416status_t AudioPolicyService::AudioCommandThread::setAudioPortConfigCommand(
1417 const struct audio_port_config *config, int delayMs)
1418{
1419 sp<AudioCommand> command = new AudioCommand();
1420 command->mCommand = SET_AUDIOPORT_CONFIG;
1421 SetAudioPortConfigData *data = new SetAudioPortConfigData();
1422 data->mConfig = *config;
1423 command->mParam = data;
1424 command->mWaitStatus = true;
1425 ALOGV("AudioCommandThread() adding set port config delay %d", delayMs);
1426 return sendCommand(command, delayMs);
1427}
1428
Jean-Michel Trivide801052015-04-14 19:10:14 -07001429void AudioPolicyService::AudioCommandThread::dynamicPolicyMixStateUpdateCommand(
Chih-Hung Hsiehe964d4e2016-08-09 14:31:32 -07001430 const String8& regId, int32_t state)
Jean-Michel Trivide801052015-04-14 19:10:14 -07001431{
1432 sp<AudioCommand> command = new AudioCommand();
1433 command->mCommand = DYN_POLICY_MIX_STATE_UPDATE;
1434 DynPolicyMixStateUpdateData *data = new DynPolicyMixStateUpdateData();
1435 data->mRegId = regId;
1436 data->mState = state;
1437 command->mParam = data;
1438 ALOGV("AudioCommandThread() sending dynamic policy mix (id=%s) state update to %d",
1439 regId.string(), state);
1440 sendCommand(command);
1441}
1442
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -08001443void AudioPolicyService::AudioCommandThread::recordingConfigurationUpdateCommand(
Eric Laurenta9f86652018-11-28 17:23:11 -08001444 int event,
1445 const record_client_info_t *clientInfo,
1446 const audio_config_base_t *clientConfig,
1447 std::vector<effect_descriptor_t> clientEffects,
1448 const audio_config_base_t *deviceConfig,
1449 std::vector<effect_descriptor_t> effects,
1450 audio_patch_handle_t patchHandle,
1451 audio_source_t source)
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -08001452{
1453 sp<AudioCommand>command = new AudioCommand();
1454 command->mCommand = RECORDING_CONFIGURATION_UPDATE;
1455 RecordingConfigurationUpdateData *data = new RecordingConfigurationUpdateData();
1456 data->mEvent = event;
Jean-Michel Triviac4e4292016-12-22 11:39:31 -08001457 data->mClientInfo = *clientInfo;
Jean-Michel Trivi7281aa92016-02-17 15:33:40 -08001458 data->mClientConfig = *clientConfig;
Eric Laurenta9f86652018-11-28 17:23:11 -08001459 data->mClientEffects = clientEffects;
Jean-Michel Trivi7281aa92016-02-17 15:33:40 -08001460 data->mDeviceConfig = *deviceConfig;
Eric Laurenta9f86652018-11-28 17:23:11 -08001461 data->mEffects = effects;
Jean-Michel Trivi8c7cf3b2016-02-25 17:08:24 -08001462 data->mPatchHandle = patchHandle;
Eric Laurenta9f86652018-11-28 17:23:11 -08001463 data->mSource = source;
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -08001464 command->mParam = data;
Jean-Michel Triviac4e4292016-12-22 11:39:31 -08001465 ALOGV("AudioCommandThread() adding recording configuration update event %d, source %d uid %u",
1466 event, clientInfo->source, clientInfo->uid);
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -08001467 sendCommand(command);
1468}
1469
Eric Laurent0ede8922014-05-09 18:04:42 -07001470status_t AudioPolicyService::AudioCommandThread::sendCommand(sp<AudioCommand>& command, int delayMs)
1471{
1472 {
1473 Mutex::Autolock _l(mLock);
1474 insertCommand_l(command, delayMs);
1475 mWaitWorkCV.signal();
1476 }
1477 Mutex::Autolock _l(command->mLock);
1478 while (command->mWaitStatus) {
1479 nsecs_t timeOutNs = kAudioCommandTimeoutNs + milliseconds(delayMs);
1480 if (command->mCond.waitRelative(command->mLock, timeOutNs) != NO_ERROR) {
1481 command->mStatus = TIMED_OUT;
1482 command->mWaitStatus = false;
1483 }
1484 }
1485 return command->mStatus;
Eric Laurentbfb1b832013-01-07 09:53:42 -08001486}
1487
Mathias Agopian65ab4712010-07-14 17:59:35 -07001488// insertCommand_l() must be called with mLock held
Eric Laurent0ede8922014-05-09 18:04:42 -07001489void AudioPolicyService::AudioCommandThread::insertCommand_l(sp<AudioCommand>& command, int delayMs)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001490{
Glenn Kasten8d6a2442012-02-08 14:04:28 -08001491 ssize_t i; // not size_t because i will count down to -1
Eric Laurent0ede8922014-05-09 18:04:42 -07001492 Vector < sp<AudioCommand> > removedCommands;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001493 command->mTime = systemTime() + milliseconds(delayMs);
1494
1495 // acquire wake lock to make sure delayed commands are processed
Eric Laurentbfb1b832013-01-07 09:53:42 -08001496 if (mAudioCommands.isEmpty()) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07001497 acquire_wake_lock(PARTIAL_WAKE_LOCK, mName.string());
1498 }
1499
1500 // check same pending commands with later time stamps and eliminate them
Ivan Lozano5ff158f2017-10-30 09:06:24 -07001501 for (i = (ssize_t)mAudioCommands.size()-1; i >= 0; i--) {
Eric Laurent0ede8922014-05-09 18:04:42 -07001502 sp<AudioCommand> command2 = mAudioCommands[i];
Mathias Agopian65ab4712010-07-14 17:59:35 -07001503 // commands are sorted by increasing time stamp: no need to scan the rest of mAudioCommands
1504 if (command2->mTime <= command->mTime) break;
Eric Laurente45b48a2014-09-04 16:40:57 -07001505
1506 // create audio patch or release audio patch commands are equivalent
1507 // with regard to filtering
1508 if ((command->mCommand == CREATE_AUDIO_PATCH) ||
1509 (command->mCommand == RELEASE_AUDIO_PATCH)) {
1510 if ((command2->mCommand != CREATE_AUDIO_PATCH) &&
1511 (command2->mCommand != RELEASE_AUDIO_PATCH)) {
1512 continue;
1513 }
1514 } else if (command2->mCommand != command->mCommand) continue;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001515
1516 switch (command->mCommand) {
1517 case SET_PARAMETERS: {
Eric Laurent0ede8922014-05-09 18:04:42 -07001518 ParametersData *data = (ParametersData *)command->mParam.get();
1519 ParametersData *data2 = (ParametersData *)command2->mParam.get();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001520 if (data->mIO != data2->mIO) break;
Steve Block3856b092011-10-20 11:56:00 +01001521 ALOGV("Comparing parameter command %s to new command %s",
Eric Laurentde070132010-07-13 04:45:46 -07001522 data2->mKeyValuePairs.string(), data->mKeyValuePairs.string());
Mathias Agopian65ab4712010-07-14 17:59:35 -07001523 AudioParameter param = AudioParameter(data->mKeyValuePairs);
1524 AudioParameter param2 = AudioParameter(data2->mKeyValuePairs);
1525 for (size_t j = 0; j < param.size(); j++) {
Glenn Kastene53b9ea2012-03-12 16:29:55 -07001526 String8 key;
1527 String8 value;
1528 param.getAt(j, key, value);
1529 for (size_t k = 0; k < param2.size(); k++) {
1530 String8 key2;
1531 String8 value2;
1532 param2.getAt(k, key2, value2);
1533 if (key2 == key) {
1534 param2.remove(key2);
1535 ALOGV("Filtering out parameter %s", key2.string());
1536 break;
1537 }
1538 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001539 }
1540 // if all keys have been filtered out, remove the command.
1541 // otherwise, update the key value pairs
1542 if (param2.size() == 0) {
1543 removedCommands.add(command2);
1544 } else {
1545 data2->mKeyValuePairs = param2.toString();
1546 }
Eric Laurent21e54562013-09-23 12:08:05 -07001547 command->mTime = command2->mTime;
1548 // force delayMs to non 0 so that code below does not request to wait for
1549 // command status as the command is now delayed
1550 delayMs = 1;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001551 } break;
1552
1553 case SET_VOLUME: {
Eric Laurent0ede8922014-05-09 18:04:42 -07001554 VolumeData *data = (VolumeData *)command->mParam.get();
1555 VolumeData *data2 = (VolumeData *)command2->mParam.get();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001556 if (data->mIO != data2->mIO) break;
1557 if (data->mStream != data2->mStream) break;
Steve Block3856b092011-10-20 11:56:00 +01001558 ALOGV("Filtering out volume command on output %d for stream %d",
Eric Laurentde070132010-07-13 04:45:46 -07001559 data->mIO, data->mStream);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001560 removedCommands.add(command2);
Eric Laurent21e54562013-09-23 12:08:05 -07001561 command->mTime = command2->mTime;
1562 // force delayMs to non 0 so that code below does not request to wait for
1563 // command status as the command is now delayed
1564 delayMs = 1;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001565 } break;
Eric Laurente45b48a2014-09-04 16:40:57 -07001566
Eric Laurentbaf35fe2016-07-27 15:36:53 -07001567 case SET_VOICE_VOLUME: {
1568 VoiceVolumeData *data = (VoiceVolumeData *)command->mParam.get();
1569 VoiceVolumeData *data2 = (VoiceVolumeData *)command2->mParam.get();
1570 ALOGV("Filtering out voice volume command value %f replaced by %f",
1571 data2->mVolume, data->mVolume);
1572 removedCommands.add(command2);
1573 command->mTime = command2->mTime;
1574 // force delayMs to non 0 so that code below does not request to wait for
1575 // command status as the command is now delayed
1576 delayMs = 1;
1577 } break;
1578
Eric Laurente45b48a2014-09-04 16:40:57 -07001579 case CREATE_AUDIO_PATCH:
1580 case RELEASE_AUDIO_PATCH: {
1581 audio_patch_handle_t handle;
Haynes Mathew Georgea2d4a6d2014-10-13 13:05:22 -07001582 struct audio_patch patch;
Eric Laurente45b48a2014-09-04 16:40:57 -07001583 if (command->mCommand == CREATE_AUDIO_PATCH) {
1584 handle = ((CreateAudioPatchData *)command->mParam.get())->mHandle;
Haynes Mathew Georgea2d4a6d2014-10-13 13:05:22 -07001585 patch = ((CreateAudioPatchData *)command->mParam.get())->mPatch;
Eric Laurente45b48a2014-09-04 16:40:57 -07001586 } else {
1587 handle = ((ReleaseAudioPatchData *)command->mParam.get())->mHandle;
Mikhail Naganov7be71d22018-05-23 16:51:46 -07001588 memset(&patch, 0, sizeof(patch));
Eric Laurente45b48a2014-09-04 16:40:57 -07001589 }
1590 audio_patch_handle_t handle2;
Haynes Mathew Georgea2d4a6d2014-10-13 13:05:22 -07001591 struct audio_patch patch2;
Eric Laurente45b48a2014-09-04 16:40:57 -07001592 if (command2->mCommand == CREATE_AUDIO_PATCH) {
1593 handle2 = ((CreateAudioPatchData *)command2->mParam.get())->mHandle;
Haynes Mathew Georgea2d4a6d2014-10-13 13:05:22 -07001594 patch2 = ((CreateAudioPatchData *)command2->mParam.get())->mPatch;
Eric Laurente45b48a2014-09-04 16:40:57 -07001595 } else {
1596 handle2 = ((ReleaseAudioPatchData *)command2->mParam.get())->mHandle;
Glenn Kastenf60b6b62015-07-06 10:53:26 -07001597 memset(&patch2, 0, sizeof(patch2));
Eric Laurente45b48a2014-09-04 16:40:57 -07001598 }
1599 if (handle != handle2) break;
Haynes Mathew Georgea2d4a6d2014-10-13 13:05:22 -07001600 /* Filter CREATE_AUDIO_PATCH commands only when they are issued for
1601 same output. */
1602 if( (command->mCommand == CREATE_AUDIO_PATCH) &&
1603 (command2->mCommand == CREATE_AUDIO_PATCH) ) {
1604 bool isOutputDiff = false;
1605 if (patch.num_sources == patch2.num_sources) {
1606 for (unsigned count = 0; count < patch.num_sources; count++) {
1607 if (patch.sources[count].id != patch2.sources[count].id) {
1608 isOutputDiff = true;
1609 break;
1610 }
1611 }
1612 if (isOutputDiff)
1613 break;
1614 }
1615 }
Eric Laurente45b48a2014-09-04 16:40:57 -07001616 ALOGV("Filtering out %s audio patch command for handle %d",
1617 (command->mCommand == CREATE_AUDIO_PATCH) ? "create" : "release", handle);
1618 removedCommands.add(command2);
1619 command->mTime = command2->mTime;
1620 // force delayMs to non 0 so that code below does not request to wait for
1621 // command status as the command is now delayed
1622 delayMs = 1;
1623 } break;
1624
Jean-Michel Trivide801052015-04-14 19:10:14 -07001625 case DYN_POLICY_MIX_STATE_UPDATE: {
1626
1627 } break;
1628
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -08001629 case RECORDING_CONFIGURATION_UPDATE: {
1630
1631 } break;
1632
Mathias Agopian65ab4712010-07-14 17:59:35 -07001633 default:
1634 break;
1635 }
1636 }
1637
1638 // remove filtered commands
1639 for (size_t j = 0; j < removedCommands.size(); j++) {
1640 // removed commands always have time stamps greater than current command
1641 for (size_t k = i + 1; k < mAudioCommands.size(); k++) {
Eric Laurent0ede8922014-05-09 18:04:42 -07001642 if (mAudioCommands[k].get() == removedCommands[j].get()) {
Steve Block3856b092011-10-20 11:56:00 +01001643 ALOGV("suppressing command: %d", mAudioCommands[k]->mCommand);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001644 mAudioCommands.removeAt(k);
1645 break;
1646 }
1647 }
1648 }
1649 removedCommands.clear();
1650
Eric Laurentaa79bef2015-01-15 14:33:51 -08001651 // Disable wait for status if delay is not 0.
1652 // Except for create audio patch command because the returned patch handle
1653 // is needed by audio policy manager
1654 if (delayMs != 0 && command->mCommand != CREATE_AUDIO_PATCH) {
Eric Laurentcec4abb2012-07-03 12:23:02 -07001655 command->mWaitStatus = false;
1656 }
Eric Laurentcec4abb2012-07-03 12:23:02 -07001657
Mathias Agopian65ab4712010-07-14 17:59:35 -07001658 // insert command at the right place according to its time stamp
Eric Laurent1e693b52014-07-09 15:03:28 -07001659 ALOGV("inserting command: %d at index %zd, num commands %zu",
1660 command->mCommand, i+1, mAudioCommands.size());
Mathias Agopian65ab4712010-07-14 17:59:35 -07001661 mAudioCommands.insertAt(command, i + 1);
1662}
1663
1664void AudioPolicyService::AudioCommandThread::exit()
1665{
Steve Block3856b092011-10-20 11:56:00 +01001666 ALOGV("AudioCommandThread::exit");
Mathias Agopian65ab4712010-07-14 17:59:35 -07001667 {
1668 AutoMutex _l(mLock);
1669 requestExit();
1670 mWaitWorkCV.signal();
1671 }
Zach Janga754b4f2015-10-27 01:29:34 +00001672 // Note that we can call it from the thread loop if all other references have been released
1673 // but it will safely return WOULD_BLOCK in this case
Mathias Agopian65ab4712010-07-14 17:59:35 -07001674 requestExitAndWait();
1675}
1676
1677void AudioPolicyService::AudioCommandThread::AudioCommand::dump(char* buffer, size_t size)
1678{
1679 snprintf(buffer, size, " %02d %06d.%03d %01u %p\n",
1680 mCommand,
1681 (int)ns2s(mTime),
1682 (int)ns2ms(mTime)%1000,
1683 mWaitStatus,
Eric Laurent0ede8922014-05-09 18:04:42 -07001684 mParam.get());
Mathias Agopian65ab4712010-07-14 17:59:35 -07001685}
1686
Dima Zavinfce7a472011-04-19 22:30:36 -07001687/******* helpers for the service_ops callbacks defined below *********/
1688void AudioPolicyService::setParameters(audio_io_handle_t ioHandle,
1689 const char *keyValuePairs,
1690 int delayMs)
1691{
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001692 mAudioCommandThread->parametersCommand(ioHandle, keyValuePairs,
Dima Zavinfce7a472011-04-19 22:30:36 -07001693 delayMs);
1694}
1695
1696int AudioPolicyService::setStreamVolume(audio_stream_type_t stream,
1697 float volume,
1698 audio_io_handle_t output,
1699 int delayMs)
1700{
Glenn Kastenfff6d712012-01-12 16:38:12 -08001701 return (int)mAudioCommandThread->volumeCommand(stream, volume,
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001702 output, delayMs);
Dima Zavinfce7a472011-04-19 22:30:36 -07001703}
1704
Dima Zavinfce7a472011-04-19 22:30:36 -07001705int AudioPolicyService::setVoiceVolume(float volume, int delayMs)
1706{
1707 return (int)mAudioCommandThread->voiceVolumeCommand(volume, delayMs);
1708}
1709
Dima Zavinfce7a472011-04-19 22:30:36 -07001710extern "C" {
Eric Laurent2d388ec2014-03-07 13:25:54 -08001711audio_module_handle_t aps_load_hw_module(void *service __unused,
1712 const char *name);
1713audio_io_handle_t aps_open_output(void *service __unused,
Eric Laurenta4c5a552012-03-29 10:12:40 -07001714 audio_devices_t *pDevices,
1715 uint32_t *pSamplingRate,
1716 audio_format_t *pFormat,
1717 audio_channel_mask_t *pChannelMask,
1718 uint32_t *pLatencyMs,
Eric Laurent2d388ec2014-03-07 13:25:54 -08001719 audio_output_flags_t flags);
Eric Laurenta4c5a552012-03-29 10:12:40 -07001720
Eric Laurent2d388ec2014-03-07 13:25:54 -08001721audio_io_handle_t aps_open_output_on_module(void *service __unused,
Eric Laurenta4c5a552012-03-29 10:12:40 -07001722 audio_module_handle_t module,
1723 audio_devices_t *pDevices,
1724 uint32_t *pSamplingRate,
1725 audio_format_t *pFormat,
1726 audio_channel_mask_t *pChannelMask,
1727 uint32_t *pLatencyMs,
Richard Fitzgeraldad3af332013-03-25 16:54:37 +00001728 audio_output_flags_t flags,
Eric Laurent2d388ec2014-03-07 13:25:54 -08001729 const audio_offload_info_t *offloadInfo);
1730audio_io_handle_t aps_open_dup_output(void *service __unused,
Dima Zavinfce7a472011-04-19 22:30:36 -07001731 audio_io_handle_t output1,
Eric Laurent2d388ec2014-03-07 13:25:54 -08001732 audio_io_handle_t output2);
1733int aps_close_output(void *service __unused, audio_io_handle_t output);
1734int aps_suspend_output(void *service __unused, audio_io_handle_t output);
1735int aps_restore_output(void *service __unused, audio_io_handle_t output);
1736audio_io_handle_t aps_open_input(void *service __unused,
Eric Laurenta4c5a552012-03-29 10:12:40 -07001737 audio_devices_t *pDevices,
1738 uint32_t *pSamplingRate,
1739 audio_format_t *pFormat,
1740 audio_channel_mask_t *pChannelMask,
Eric Laurent2d388ec2014-03-07 13:25:54 -08001741 audio_in_acoustics_t acoustics __unused);
1742audio_io_handle_t aps_open_input_on_module(void *service __unused,
Eric Laurenta4c5a552012-03-29 10:12:40 -07001743 audio_module_handle_t module,
1744 audio_devices_t *pDevices,
1745 uint32_t *pSamplingRate,
1746 audio_format_t *pFormat,
Eric Laurent2d388ec2014-03-07 13:25:54 -08001747 audio_channel_mask_t *pChannelMask);
1748int aps_close_input(void *service __unused, audio_io_handle_t input);
1749int aps_invalidate_stream(void *service __unused, audio_stream_type_t stream);
Glenn Kastend848eb42016-03-08 13:42:11 -08001750int aps_move_effects(void *service __unused, audio_session_t session,
Dima Zavinfce7a472011-04-19 22:30:36 -07001751 audio_io_handle_t src_output,
Eric Laurent2d388ec2014-03-07 13:25:54 -08001752 audio_io_handle_t dst_output);
1753char * aps_get_parameters(void *service __unused, audio_io_handle_t io_handle,
1754 const char *keys);
1755void aps_set_parameters(void *service, audio_io_handle_t io_handle,
1756 const char *kv_pairs, int delay_ms);
1757int aps_set_stream_volume(void *service, audio_stream_type_t stream,
Dima Zavinfce7a472011-04-19 22:30:36 -07001758 float volume, audio_io_handle_t output,
Eric Laurent2d388ec2014-03-07 13:25:54 -08001759 int delay_ms);
Eric Laurent2d388ec2014-03-07 13:25:54 -08001760int aps_set_voice_volume(void *service, float volume, int delay_ms);
1761};
Dima Zavinfce7a472011-04-19 22:30:36 -07001762
Mikhail Naganov1b2a7942017-12-08 10:18:09 -08001763} // namespace android