blob: d9514f615009cc7b5657e69561238026bd7cca04 [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>
25
26#include <sys/time.h>
27#include <binder/IServiceManager.h>
28#include <utils/Log.h>
29#include <cutils/properties.h>
30#include <binder/IPCThreadState.h>
Svet Ganovf4ddfef2018-01-16 07:37:58 -080031#include <binder/ActivityManager.h>
32#include <binder/PermissionController.h>
33#include <binder/IResultReceiver.h>
Mathias Agopian65ab4712010-07-14 17:59:35 -070034#include <utils/String16.h>
35#include <utils/threads.h>
36#include "AudioPolicyService.h"
Mathias Agopian65ab4712010-07-14 17:59:35 -070037#include <hardware_legacy/power.h>
Eric Laurent7c7f10b2011-06-17 21:29:58 -070038#include <media/AudioEffect.h>
Chih-Hung Hsiehc84d9d22014-11-14 13:33:34 -080039#include <media/AudioParameter.h>
Andy Hungab7ef302018-05-15 19:35:29 -070040#include <mediautils/ServiceUtilities.h>
Michael Groovercfd28302018-12-11 19:16:46 -080041#include <sensorprivacy/SensorPrivacyManager.h>
Mathias Agopian65ab4712010-07-14 17:59:35 -070042
Dima Zavin64760242011-05-11 14:15:23 -070043#include <system/audio.h>
Dima Zavin7394a4f2011-06-13 18:16:26 -070044#include <system/audio_policy.h>
Mikhail Naganov61a4fac2016-10-13 14:44:18 -070045
Mathias Agopian65ab4712010-07-14 17:59:35 -070046namespace android {
47
Glenn Kasten8dad0e32012-01-09 08:41:22 -080048static const char kDeadlockedString[] = "AudioPolicyService may be deadlocked\n";
49static const char kCmdDeadlockedString[] = "AudioPolicyService command thread may be deadlocked\n";
Mathias Agopian65ab4712010-07-14 17:59:35 -070050
51static const int kDumpLockRetries = 50;
Glenn Kasten22ecc912012-01-09 08:33:38 -080052static const int kDumpLockSleepUs = 20000;
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
379static bool tryLock(Mutex& mutex)
380{
381 bool locked = false;
382 for (int i = 0; i < kDumpLockRetries; ++i) {
383 if (mutex.tryLock() == NO_ERROR) {
384 locked = true;
385 break;
386 }
Glenn Kasten22ecc912012-01-09 08:33:38 -0800387 usleep(kDumpLockSleepUs);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700388 }
389 return locked;
390}
391
392status_t AudioPolicyService::dumpInternals(int fd)
393{
394 const size_t SIZE = 256;
395 char buffer[SIZE];
396 String8 result;
397
Eric Laurentdce54a12014-03-10 12:19:46 -0700398 snprintf(buffer, SIZE, "AudioPolicyManager: %p\n", mAudioPolicyManager);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700399 result.append(buffer);
400 snprintf(buffer, SIZE, "Command Thread: %p\n", mAudioCommandThread.get());
401 result.append(buffer);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700402
403 write(fd, result.string(), result.size());
404 return NO_ERROR;
405}
406
Eric Laurente8c8b432018-10-17 10:08:02 -0700407void AudioPolicyService::updateUidStates()
Svet Ganovf4ddfef2018-01-16 07:37:58 -0800408{
Eric Laurente8c8b432018-10-17 10:08:02 -0700409 Mutex::Autolock _l(mLock);
410 updateUidStates_l();
411}
412
413void AudioPolicyService::updateUidStates_l()
414{
Eric Laurent4eb58f12018-12-07 16:41:02 -0800415// Go over all active clients and allow capture (does not force silence) in the
416// following cases:
Eric Laurenta46bedb2018-12-07 18:01:26 -0800417// The client is the assistant
418// AND an accessibility service is on TOP
419// AND the source is VOICE_RECOGNITION or HOTWORD
420// OR uses VOICE_RECOGNITION AND is on TOP OR latest started
421// OR uses HOTWORD
422// AND there is no privacy sensitive active capture
423// OR The client is an accessibility service
424// AND is on TOP OR latest started
425// AND the source is VOICE_RECOGNITION or HOTWORD
Sapir Caduri9a183af2019-02-14 22:23:47 +0200426// OR the source is one of: AUDIO_SOURCE_VOICE_DOWNLINK, AUDIO_SOURCE_VOICE_UPLINK,
427// AUDIO_SOURCE_VOICE_CALL
Eric Laurenta46bedb2018-12-07 18:01:26 -0800428// OR Any other client
429// AND The assistant is not on TOP
430// AND is on TOP OR latest started
431// AND there is no privacy sensitive active capture
Eric Laurent4eb58f12018-12-07 16:41:02 -0800432//TODO: mamanage pre processing effects according to use case priority
433
434 sp<AudioRecordClient> topActive;
435 sp<AudioRecordClient> latestActive;
Eric Laurent4eb58f12018-12-07 16:41:02 -0800436 sp<AudioRecordClient> latestSensitiveActive;
Eric Laurenta46bedb2018-12-07 18:01:26 -0800437 nsecs_t topStartNs = 0;
438 nsecs_t latestStartNs = 0;
Eric Laurent4eb58f12018-12-07 16:41:02 -0800439 nsecs_t latestSensitiveStartNs = 0;
440 bool isA11yOnTop = mUidPolicy->isA11yOnTop();
441 bool isAssistantOnTop = false;
442 bool isSensitiveActive = false;
443
Michael Groovercfd28302018-12-11 19:16:46 -0800444 // if Sensor Privacy is enabled then all recordings should be silenced.
445 if (mSensorPrivacyPolicy->isSensorPrivacyEnabled()) {
446 silenceAllRecordings_l();
447 return;
448 }
449
Eric Laurente8c8b432018-10-17 10:08:02 -0700450 for (size_t i =0; i < mAudioRecordClients.size(); i++) {
451 sp<AudioRecordClient> current = mAudioRecordClients[i];
452 if (!current->active) continue;
Eric Laurent4eb58f12018-12-07 16:41:02 -0800453 if (isPrivacySensitive(current->attributes.source)) {
454 if (current->startTimeNs > latestSensitiveStartNs) {
455 latestSensitiveActive = current;
456 latestSensitiveStartNs = current->startTimeNs;
457 }
458 isSensitiveActive = true;
459 }
460 if (mUidPolicy->getUidState(current->uid) == ActivityManager::PROCESS_STATE_TOP) {
Eric Laurenta46bedb2018-12-07 18:01:26 -0800461 if (current->startTimeNs > topStartNs) {
462 topActive = current;
463 topStartNs = current->startTimeNs;
464 }
Eric Laurent4eb58f12018-12-07 16:41:02 -0800465 if (mUidPolicy->isAssistantUid(current->uid)) {
466 isAssistantOnTop = true;
467 }
468 }
469 if (current->startTimeNs > latestStartNs) {
470 latestActive = current;
471 latestStartNs = current->startTimeNs;
472 }
473 }
474
475 if (topActive == nullptr && latestActive == nullptr) {
476 return;
477 }
478
Eric Laurenta46bedb2018-12-07 18:01:26 -0800479 if (topActive != nullptr) {
480 latestActive = nullptr;
481 }
482
Eric Laurent4eb58f12018-12-07 16:41:02 -0800483 for (size_t i =0; i < mAudioRecordClients.size(); i++) {
484 sp<AudioRecordClient> current = mAudioRecordClients[i];
485 if (!current->active) continue;
486
487 audio_source_t source = current->attributes.source;
Eric Laurenta46bedb2018-12-07 18:01:26 -0800488 bool isOnTop = current == topActive;
Eric Laurent4eb58f12018-12-07 16:41:02 -0800489 bool isLatest = current == latestActive;
490 bool isLatestSensitive = current == latestSensitiveActive;
491 bool forceIdle = true;
492 if (mUidPolicy->isAssistantUid(current->uid)) {
493 if (isA11yOnTop) {
494 if (source == AUDIO_SOURCE_HOTWORD || source == AUDIO_SOURCE_VOICE_RECOGNITION) {
495 forceIdle = false;
496 }
497 } else {
Eric Laurenta46bedb2018-12-07 18:01:26 -0800498 if ((((isOnTop || isLatest) && source == AUDIO_SOURCE_VOICE_RECOGNITION) ||
Eric Laurent4eb58f12018-12-07 16:41:02 -0800499 source == AUDIO_SOURCE_HOTWORD) && !isSensitiveActive) {
500 forceIdle = false;
501 }
502 }
503 } else if (mUidPolicy->isA11yUid(current->uid)) {
Eric Laurenta46bedb2018-12-07 18:01:26 -0800504 if ((isOnTop || isLatest) &&
Eric Laurent4eb58f12018-12-07 16:41:02 -0800505 (source == AUDIO_SOURCE_VOICE_RECOGNITION || source == AUDIO_SOURCE_HOTWORD)) {
506 forceIdle = false;
507 }
Sapir Caduri9a183af2019-02-14 22:23:47 +0200508 } else if (source == AUDIO_SOURCE_VOICE_DOWNLINK ||
509 source == AUDIO_SOURCE_VOICE_CALL ||
510 (source == AUDIO_SOURCE_VOICE_UPLINK)) {
511 forceIdle = false;
Eric Laurent4eb58f12018-12-07 16:41:02 -0800512 } else {
Eric Laurenta46bedb2018-12-07 18:01:26 -0800513 if (!isAssistantOnTop && (isOnTop || isLatest) &&
Eric Laurent4eb58f12018-12-07 16:41:02 -0800514 (!isSensitiveActive || isLatestSensitive)) {
515 forceIdle = false;
516 }
517 }
518 setAppState_l(current->uid,
519 forceIdle ? APP_STATE_IDLE :
520 apmStatFromAmState(mUidPolicy->getUidState(current->uid)));
Eric Laurente8c8b432018-10-17 10:08:02 -0700521 }
522}
523
Michael Groovercfd28302018-12-11 19:16:46 -0800524void AudioPolicyService::silenceAllRecordings_l() {
525 for (size_t i = 0; i < mAudioRecordClients.size(); i++) {
526 sp<AudioRecordClient> current = mAudioRecordClients[i];
527 setAppState_l(current->uid, APP_STATE_IDLE);
528 }
529}
530
Eric Laurente8c8b432018-10-17 10:08:02 -0700531/* static */
532app_state_t AudioPolicyService::apmStatFromAmState(int amState) {
533 switch (amState) {
534 case ActivityManager::PROCESS_STATE_UNKNOWN:
535 return APP_STATE_IDLE;
536 case ActivityManager::PROCESS_STATE_TOP:
537 return APP_STATE_TOP;
538 default:
539 break;
540 }
541 return APP_STATE_FOREGROUND;
542}
543
Eric Laurent4eb58f12018-12-07 16:41:02 -0800544/* static */
545bool AudioPolicyService::isPrivacySensitive(audio_source_t source)
546{
547 switch (source) {
548 case AUDIO_SOURCE_VOICE_UPLINK:
549 case AUDIO_SOURCE_VOICE_DOWNLINK:
550 case AUDIO_SOURCE_VOICE_CALL:
551 case AUDIO_SOURCE_CAMCORDER:
552 case AUDIO_SOURCE_VOICE_COMMUNICATION:
553 return true;
554 default:
555 break;
556 }
557 return false;
558}
559
Eric Laurente8c8b432018-10-17 10:08:02 -0700560void AudioPolicyService::setAppState_l(uid_t uid, app_state_t state)
561{
562 AutoCallerClear acc;
563
564 if (mAudioPolicyManager) {
565 mAudioPolicyManager->setAppState(uid, state);
Mikhail Naganoveae73eb2018-04-03 16:57:36 -0700566 }
567 sp<IAudioFlinger> af = AudioSystem::get_audio_flinger();
568 if (af) {
Eric Laurentf32108e2018-10-04 17:22:04 -0700569 bool silenced = state == APP_STATE_IDLE;
Mikhail Naganoveae73eb2018-04-03 16:57:36 -0700570 af->setRecordSilenced(uid, silenced);
571 }
Svet Ganovf4ddfef2018-01-16 07:37:58 -0800572}
573
Glenn Kasten0f11b512014-01-31 16:18:54 -0800574status_t AudioPolicyService::dump(int fd, const Vector<String16>& args __unused)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700575{
Glenn Kasten44deb052012-02-05 18:09:08 -0800576 if (!dumpAllowed()) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700577 dumpPermissionDenial(fd);
578 } else {
579 bool locked = tryLock(mLock);
580 if (!locked) {
581 String8 result(kDeadlockedString);
582 write(fd, result.string(), result.size());
583 }
584
585 dumpInternals(fd);
Glenn Kasten9d1f02d2012-02-08 17:47:58 -0800586 if (mAudioCommandThread != 0) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700587 mAudioCommandThread->dump(fd);
588 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700589
Eric Laurentdce54a12014-03-10 12:19:46 -0700590 if (mAudioPolicyManager) {
591 mAudioPolicyManager->dump(fd);
592 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700593
Kevin Rocard8be94972019-02-22 13:26:25 -0800594 mPackageManager.dump(fd);
595
Mathias Agopian65ab4712010-07-14 17:59:35 -0700596 if (locked) mLock.unlock();
597 }
598 return NO_ERROR;
599}
600
601status_t AudioPolicyService::dumpPermissionDenial(int fd)
602{
603 const size_t SIZE = 256;
604 char buffer[SIZE];
605 String8 result;
606 snprintf(buffer, SIZE, "Permission Denial: "
607 "can't dump AudioPolicyService from pid=%d, uid=%d\n",
608 IPCThreadState::self()->getCallingPid(),
609 IPCThreadState::self()->getCallingUid());
610 result.append(buffer);
611 write(fd, result.string(), result.size());
612 return NO_ERROR;
613}
614
615status_t AudioPolicyService::onTransact(
Svet Ganovf4ddfef2018-01-16 07:37:58 -0800616 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags) {
617 switch (code) {
618 case SHELL_COMMAND_TRANSACTION: {
619 int in = data.readFileDescriptor();
620 int out = data.readFileDescriptor();
621 int err = data.readFileDescriptor();
622 int argc = data.readInt32();
623 Vector<String16> args;
624 for (int i = 0; i < argc && data.dataAvail() > 0; i++) {
625 args.add(data.readString16());
626 }
627 sp<IBinder> unusedCallback;
628 sp<IResultReceiver> resultReceiver;
629 status_t status;
630 if ((status = data.readNullableStrongBinder(&unusedCallback)) != NO_ERROR) {
631 return status;
632 }
633 if ((status = data.readNullableStrongBinder(&resultReceiver)) != NO_ERROR) {
634 return status;
635 }
636 status = shellCommand(in, out, err, args);
637 if (resultReceiver != nullptr) {
638 resultReceiver->send(status);
639 }
640 return NO_ERROR;
641 }
642 }
643
Mathias Agopian65ab4712010-07-14 17:59:35 -0700644 return BnAudioPolicyService::onTransact(code, data, reply, flags);
645}
646
Svet Ganovf4ddfef2018-01-16 07:37:58 -0800647// ------------------- Shell command implementation -------------------
648
649// NOTE: This is a remote API - make sure all args are validated
650status_t AudioPolicyService::shellCommand(int in, int out, int err, Vector<String16>& args) {
651 if (!checkCallingPermission(sManageAudioPolicyPermission, nullptr, nullptr)) {
652 return PERMISSION_DENIED;
653 }
654 if (in == BAD_TYPE || out == BAD_TYPE || err == BAD_TYPE) {
655 return BAD_VALUE;
656 }
657 if (args.size() == 3 && args[0] == String16("set-uid-state")) {
658 return handleSetUidState(args, err);
659 } else if (args.size() == 2 && args[0] == String16("reset-uid-state")) {
660 return handleResetUidState(args, err);
661 } else if (args.size() == 2 && args[0] == String16("get-uid-state")) {
662 return handleGetUidState(args, out, err);
663 } else if (args.size() == 1 && args[0] == String16("help")) {
664 printHelp(out);
665 return NO_ERROR;
666 }
667 printHelp(err);
668 return BAD_VALUE;
669}
670
671status_t AudioPolicyService::handleSetUidState(Vector<String16>& args, int err) {
672 PermissionController pc;
673 int uid = pc.getPackageUid(args[1], 0);
674 if (uid <= 0) {
675 ALOGE("Unknown package: '%s'", String8(args[1]).string());
676 dprintf(err, "Unknown package: '%s'\n", String8(args[1]).string());
677 return BAD_VALUE;
678 }
679 bool active = false;
680 if (args[2] == String16("active")) {
681 active = true;
682 } else if ((args[2] != String16("idle"))) {
683 ALOGE("Expected active or idle but got: '%s'", String8(args[2]).string());
684 return BAD_VALUE;
685 }
686 mUidPolicy->addOverrideUid(uid, active);
687 return NO_ERROR;
688}
689
690status_t AudioPolicyService::handleResetUidState(Vector<String16>& args, int err) {
691 PermissionController pc;
692 int uid = pc.getPackageUid(args[1], 0);
693 if (uid < 0) {
694 ALOGE("Unknown package: '%s'", String8(args[1]).string());
695 dprintf(err, "Unknown package: '%s'\n", String8(args[1]).string());
696 return BAD_VALUE;
697 }
698 mUidPolicy->removeOverrideUid(uid);
699 return NO_ERROR;
700}
701
702status_t AudioPolicyService::handleGetUidState(Vector<String16>& args, int out, int err) {
703 PermissionController pc;
704 int uid = pc.getPackageUid(args[1], 0);
705 if (uid < 0) {
706 ALOGE("Unknown package: '%s'", String8(args[1]).string());
707 dprintf(err, "Unknown package: '%s'\n", String8(args[1]).string());
708 return BAD_VALUE;
709 }
710 if (mUidPolicy->isUidActive(uid)) {
711 return dprintf(out, "active\n");
712 } else {
713 return dprintf(out, "idle\n");
714 }
715}
716
717status_t AudioPolicyService::printHelp(int out) {
718 return dprintf(out, "Audio policy service commands:\n"
719 " get-uid-state <PACKAGE> gets the uid state\n"
720 " set-uid-state <PACKAGE> <active|idle> overrides the uid state\n"
721 " reset-uid-state <PACKAGE> clears the uid state override\n"
722 " help print this message\n");
723}
724
725// ----------- AudioPolicyService::UidPolicy implementation ----------
726
727void AudioPolicyService::UidPolicy::registerSelf() {
728 ActivityManager am;
729 am.registerUidObserver(this, ActivityManager::UID_OBSERVER_GONE
730 | ActivityManager::UID_OBSERVER_IDLE
Eric Laurente8c8b432018-10-17 10:08:02 -0700731 | ActivityManager::UID_OBSERVER_ACTIVE
732 | ActivityManager::UID_OBSERVER_PROCSTATE,
Svet Ganovf4ddfef2018-01-16 07:37:58 -0800733 ActivityManager::PROCESS_STATE_UNKNOWN,
734 String16("audioserver"));
Mikhail Naganoveae73eb2018-04-03 16:57:36 -0700735 status_t res = am.linkToDeath(this);
736 if (!res) {
737 Mutex::Autolock _l(mLock);
738 mObserverRegistered = true;
739 } else {
740 ALOGE("UidPolicy::registerSelf linkToDeath failed: %d", res);
Eric Laurent4eb58f12018-12-07 16:41:02 -0800741
Mikhail Naganoveae73eb2018-04-03 16:57:36 -0700742 am.unregisterUidObserver(this);
743 }
Svet Ganovf4ddfef2018-01-16 07:37:58 -0800744}
745
746void AudioPolicyService::UidPolicy::unregisterSelf() {
747 ActivityManager am;
Mikhail Naganoveae73eb2018-04-03 16:57:36 -0700748 am.unlinkToDeath(this);
Svet Ganovf4ddfef2018-01-16 07:37:58 -0800749 am.unregisterUidObserver(this);
Mikhail Naganoveae73eb2018-04-03 16:57:36 -0700750 Mutex::Autolock _l(mLock);
751 mObserverRegistered = false;
Svet Ganovf4ddfef2018-01-16 07:37:58 -0800752}
753
Mikhail Naganoveae73eb2018-04-03 16:57:36 -0700754void AudioPolicyService::UidPolicy::binderDied(__unused const wp<IBinder> &who) {
755 Mutex::Autolock _l(mLock);
756 mCachedUids.clear();
757 mObserverRegistered = false;
Svet Ganovf4ddfef2018-01-16 07:37:58 -0800758}
759
Eric Laurente8c8b432018-10-17 10:08:02 -0700760void AudioPolicyService::UidPolicy::checkRegistered() {
Mikhail Naganoveae73eb2018-04-03 16:57:36 -0700761 bool needToReregister = false;
762 {
763 Mutex::Autolock _l(mLock);
764 needToReregister = !mObserverRegistered;
Svet Ganovf4ddfef2018-01-16 07:37:58 -0800765 }
Mikhail Naganoveae73eb2018-04-03 16:57:36 -0700766 if (needToReregister) {
767 // Looks like ActivityManager has died previously, attempt to re-register.
768 registerSelf();
769 }
Eric Laurente8c8b432018-10-17 10:08:02 -0700770}
771
772bool AudioPolicyService::UidPolicy::isUidActive(uid_t uid) {
773 if (isServiceUid(uid)) return true;
774 checkRegistered();
Mikhail Naganoveae73eb2018-04-03 16:57:36 -0700775 {
776 Mutex::Autolock _l(mLock);
777 auto overrideIter = mOverrideUids.find(uid);
778 if (overrideIter != mOverrideUids.end()) {
Eric Laurente8c8b432018-10-17 10:08:02 -0700779 return overrideIter->second.first;
Mikhail Naganoveae73eb2018-04-03 16:57:36 -0700780 }
781 // In an absense of the ActivityManager, assume everything to be active.
782 if (!mObserverRegistered) return true;
783 auto cacheIter = mCachedUids.find(uid);
Mikhail Naganoveba668a2018-04-05 08:13:15 -0700784 if (cacheIter != mCachedUids.end()) {
Eric Laurente8c8b432018-10-17 10:08:02 -0700785 return cacheIter->second.first;
Mikhail Naganoveae73eb2018-04-03 16:57:36 -0700786 }
787 }
788 ActivityManager am;
789 bool active = am.isUidActive(uid, String16("audioserver"));
790 {
791 Mutex::Autolock _l(mLock);
Eric Laurente8c8b432018-10-17 10:08:02 -0700792 mCachedUids.insert(std::pair<uid_t,
793 std::pair<bool, int>>(uid, std::pair<bool, int>(active,
794 ActivityManager::PROCESS_STATE_UNKNOWN)));
Mikhail Naganoveae73eb2018-04-03 16:57:36 -0700795 }
796 return active;
Svet Ganovf4ddfef2018-01-16 07:37:58 -0800797}
798
Eric Laurente8c8b432018-10-17 10:08:02 -0700799int AudioPolicyService::UidPolicy::getUidState(uid_t uid) {
800 if (isServiceUid(uid)) {
801 return ActivityManager::PROCESS_STATE_TOP;
802 }
803 checkRegistered();
804 {
805 Mutex::Autolock _l(mLock);
806 auto overrideIter = mOverrideUids.find(uid);
807 if (overrideIter != mOverrideUids.end()) {
808 if (overrideIter->second.first) {
809 if (overrideIter->second.second != ActivityManager::PROCESS_STATE_UNKNOWN) {
810 return overrideIter->second.second;
811 } else {
812 auto cacheIter = mCachedUids.find(uid);
813 if (cacheIter != mCachedUids.end()) {
814 return cacheIter->second.second;
815 }
816 }
817 }
818 return ActivityManager::PROCESS_STATE_UNKNOWN;
819 }
820 // In an absense of the ActivityManager, assume everything to be active.
821 if (!mObserverRegistered) {
822 return ActivityManager::PROCESS_STATE_TOP;
823 }
824 auto cacheIter = mCachedUids.find(uid);
825 if (cacheIter != mCachedUids.end()) {
826 if (cacheIter->second.first) {
827 return cacheIter->second.second;
828 } else {
829 return ActivityManager::PROCESS_STATE_UNKNOWN;
830 }
831 }
832 }
833 ActivityManager am;
834 bool active = am.isUidActive(uid, String16("audioserver"));
835 int state = ActivityManager::PROCESS_STATE_UNKNOWN;
836 if (active) {
837 state = am.getUidProcessState(uid, String16("audioserver"));
838 }
839 {
840 Mutex::Autolock _l(mLock);
841 mCachedUids.insert(std::pair<uid_t,
842 std::pair<bool, int>>(uid, std::pair<bool, int>(active, state)));
843 }
Eric Laurent4eb58f12018-12-07 16:41:02 -0800844
Eric Laurente8c8b432018-10-17 10:08:02 -0700845 return state;
846}
847
Mikhail Naganoveae73eb2018-04-03 16:57:36 -0700848void AudioPolicyService::UidPolicy::onUidActive(uid_t uid) {
Eric Laurente8c8b432018-10-17 10:08:02 -0700849 updateUid(&mCachedUids, uid, true, ActivityManager::PROCESS_STATE_UNKNOWN, true);
Mikhail Naganoveae73eb2018-04-03 16:57:36 -0700850}
851
852void AudioPolicyService::UidPolicy::onUidGone(uid_t uid, __unused bool disabled) {
Eric Laurente8c8b432018-10-17 10:08:02 -0700853 updateUid(&mCachedUids, uid, false, ActivityManager::PROCESS_STATE_UNKNOWN, false);
Mikhail Naganoveae73eb2018-04-03 16:57:36 -0700854}
855
856void AudioPolicyService::UidPolicy::onUidIdle(uid_t uid, __unused bool disabled) {
Eric Laurente8c8b432018-10-17 10:08:02 -0700857 updateUid(&mCachedUids, uid, false, ActivityManager::PROCESS_STATE_UNKNOWN, true);
Mikhail Naganoveae73eb2018-04-03 16:57:36 -0700858}
859
Eric Laurente8c8b432018-10-17 10:08:02 -0700860void AudioPolicyService::UidPolicy::onUidStateChanged(uid_t uid,
861 int32_t procState,
862 int64_t procStateSeq __unused) {
863 if (procState != ActivityManager::PROCESS_STATE_UNKNOWN) {
864 updateUid(&mCachedUids, uid, true, procState, true);
Mikhail Naganoveae73eb2018-04-03 16:57:36 -0700865 }
866}
867
868void AudioPolicyService::UidPolicy::updateOverrideUid(uid_t uid, bool active, bool insert) {
Eric Laurente8c8b432018-10-17 10:08:02 -0700869 updateUid(&mOverrideUids, uid, active, ActivityManager::PROCESS_STATE_UNKNOWN, insert);
870}
871
872void AudioPolicyService::UidPolicy::notifyService() {
873 sp<AudioPolicyService> service = mService.promote();
874 if (service != nullptr) {
875 service->updateUidStates();
Mikhail Naganoveae73eb2018-04-03 16:57:36 -0700876 }
877}
878
Eric Laurente8c8b432018-10-17 10:08:02 -0700879void AudioPolicyService::UidPolicy::updateUid(std::unordered_map<uid_t,
880 std::pair<bool, int>> *uids,
881 uid_t uid,
882 bool active,
883 int state,
884 bool insert) {
885 if (isServiceUid(uid)) {
886 return;
887 }
888 bool wasActive = isUidActive(uid);
889 int previousState = getUidState(uid);
Mikhail Naganoveae73eb2018-04-03 16:57:36 -0700890 {
891 Mutex::Autolock _l(mLock);
Eric Laurente8c8b432018-10-17 10:08:02 -0700892 updateUidLocked(uids, uid, active, state, insert);
Mikhail Naganoveae73eb2018-04-03 16:57:36 -0700893 }
Eric Laurente8c8b432018-10-17 10:08:02 -0700894 if (wasActive != isUidActive(uid) || state != previousState) {
895 notifyService();
896 }
Mikhail Naganoveae73eb2018-04-03 16:57:36 -0700897}
898
Eric Laurente8c8b432018-10-17 10:08:02 -0700899void AudioPolicyService::UidPolicy::updateUidLocked(std::unordered_map<uid_t,
900 std::pair<bool, int>> *uids,
901 uid_t uid,
902 bool active,
903 int state,
904 bool insert) {
Mikhail Naganoveae73eb2018-04-03 16:57:36 -0700905 auto it = uids->find(uid);
906 if (it != uids->end()) {
Mikhail Naganoveae73eb2018-04-03 16:57:36 -0700907 if (insert) {
Eric Laurente8c8b432018-10-17 10:08:02 -0700908 if (state == ActivityManager::PROCESS_STATE_UNKNOWN) {
909 it->second.first = active;
910 }
911 if (it->second.first) {
912 it->second.second = state;
913 } else {
914 it->second.second = ActivityManager::PROCESS_STATE_UNKNOWN;
915 }
Mikhail Naganoveae73eb2018-04-03 16:57:36 -0700916 } else {
917 uids->erase(it);
918 }
Eric Laurente8c8b432018-10-17 10:08:02 -0700919 } else if (insert && (state == ActivityManager::PROCESS_STATE_UNKNOWN)) {
920 uids->insert(std::pair<uid_t, std::pair<bool, int>>(uid,
921 std::pair<bool, int>(active, state)));
Mikhail Naganoveae73eb2018-04-03 16:57:36 -0700922 }
Svet Ganovf4ddfef2018-01-16 07:37:58 -0800923}
Mathias Agopian65ab4712010-07-14 17:59:35 -0700924
Eric Laurent4eb58f12018-12-07 16:41:02 -0800925bool AudioPolicyService::UidPolicy::isA11yOnTop() {
926 for (const auto &uid : mCachedUids) {
927 std::vector<uid_t>::iterator it = find(mA11yUids.begin(), mA11yUids.end(), uid.first);
928 if (it == mA11yUids.end()) {
929 continue;
930 }
Amith Yamasanibcbb3002019-01-23 13:53:33 -0800931 if (uid.second.second >= ActivityManager::PROCESS_STATE_TOP
932 && uid.second.second <= ActivityManager::PROCESS_STATE_BOUND_FOREGROUND_SERVICE) {
Eric Laurent4eb58f12018-12-07 16:41:02 -0800933 return true;
934 }
935 }
936 return false;
937}
938
Eric Laurentb78763e2018-10-17 10:08:02 -0700939bool AudioPolicyService::UidPolicy::isA11yUid(uid_t uid)
940{
941 std::vector<uid_t>::iterator it = find(mA11yUids.begin(), mA11yUids.end(), uid);
942 return it != mA11yUids.end();
943}
944
Michael Groovercfd28302018-12-11 19:16:46 -0800945// ----------- AudioPolicyService::SensorPrivacyService implementation ----------
946void AudioPolicyService::SensorPrivacyPolicy::registerSelf() {
947 SensorPrivacyManager spm;
948 mSensorPrivacyEnabled = spm.isSensorPrivacyEnabled();
949 spm.addSensorPrivacyListener(this);
950}
951
952void AudioPolicyService::SensorPrivacyPolicy::unregisterSelf() {
953 SensorPrivacyManager spm;
954 spm.removeSensorPrivacyListener(this);
955}
956
957bool AudioPolicyService::SensorPrivacyPolicy::isSensorPrivacyEnabled() {
958 return mSensorPrivacyEnabled;
959}
960
961binder::Status AudioPolicyService::SensorPrivacyPolicy::onSensorPrivacyChanged(bool enabled) {
962 mSensorPrivacyEnabled = enabled;
963 sp<AudioPolicyService> service = mService.promote();
964 if (service != nullptr) {
965 service->updateUidStates();
966 }
967 return binder::Status::ok();
968}
969
Mathias Agopian65ab4712010-07-14 17:59:35 -0700970// ----------- AudioPolicyService::AudioCommandThread implementation ----------
971
Eric Laurentbfb1b832013-01-07 09:53:42 -0800972AudioPolicyService::AudioCommandThread::AudioCommandThread(String8 name,
973 const wp<AudioPolicyService>& service)
974 : Thread(false), mName(name), mService(service)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700975{
Mathias Agopian65ab4712010-07-14 17:59:35 -0700976}
977
978
979AudioPolicyService::AudioCommandThread::~AudioCommandThread()
980{
Eric Laurentbfb1b832013-01-07 09:53:42 -0800981 if (!mAudioCommands.isEmpty()) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700982 release_wake_lock(mName.string());
983 }
984 mAudioCommands.clear();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700985}
986
987void AudioPolicyService::AudioCommandThread::onFirstRef()
988{
Eric Laurentbfb1b832013-01-07 09:53:42 -0800989 run(mName.string(), ANDROID_PRIORITY_AUDIO);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700990}
991
992bool AudioPolicyService::AudioCommandThread::threadLoop()
993{
Eric Laurentd7eda8d2016-02-02 17:18:39 -0800994 nsecs_t waitTime = -1;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700995
996 mLock.lock();
997 while (!exitPending())
998 {
Eric Laurent59a89232014-06-08 14:14:17 -0700999 sp<AudioPolicyService> svc;
1000 while (!mAudioCommands.isEmpty() && !exitPending()) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07001001 nsecs_t curTime = systemTime();
1002 // commands are sorted by increasing time stamp: execute them from index 0 and up
1003 if (mAudioCommands[0]->mTime <= curTime) {
Eric Laurent0ede8922014-05-09 18:04:42 -07001004 sp<AudioCommand> command = mAudioCommands[0];
Mathias Agopian65ab4712010-07-14 17:59:35 -07001005 mAudioCommands.removeAt(0);
Eric Laurent0ede8922014-05-09 18:04:42 -07001006 mLastCommand = command;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001007
1008 switch (command->mCommand) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07001009 case SET_VOLUME: {
Eric Laurent0ede8922014-05-09 18:04:42 -07001010 VolumeData *data = (VolumeData *)command->mParam.get();
Steve Block3856b092011-10-20 11:56:00 +01001011 ALOGV("AudioCommandThread() processing set volume stream %d, \
Eric Laurentde070132010-07-13 04:45:46 -07001012 volume %f, output %d", data->mStream, data->mVolume, data->mIO);
Andy Hungfe726a62018-09-27 15:17:25 -07001013 mLock.unlock();
Eric Laurentde070132010-07-13 04:45:46 -07001014 command->mStatus = AudioSystem::setStreamVolume(data->mStream,
1015 data->mVolume,
1016 data->mIO);
Andy Hungfe726a62018-09-27 15:17:25 -07001017 mLock.lock();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001018 }break;
1019 case SET_PARAMETERS: {
Eric Laurent0ede8922014-05-09 18:04:42 -07001020 ParametersData *data = (ParametersData *)command->mParam.get();
Glenn Kastene53b9ea2012-03-12 16:29:55 -07001021 ALOGV("AudioCommandThread() processing set parameters string %s, io %d",
1022 data->mKeyValuePairs.string(), data->mIO);
Andy Hungfe726a62018-09-27 15:17:25 -07001023 mLock.unlock();
Glenn Kastene53b9ea2012-03-12 16:29:55 -07001024 command->mStatus = AudioSystem::setParameters(data->mIO, data->mKeyValuePairs);
Andy Hungfe726a62018-09-27 15:17:25 -07001025 mLock.lock();
Glenn Kastene53b9ea2012-03-12 16:29:55 -07001026 }break;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001027 case SET_VOICE_VOLUME: {
Eric Laurent0ede8922014-05-09 18:04:42 -07001028 VoiceVolumeData *data = (VoiceVolumeData *)command->mParam.get();
Steve Block3856b092011-10-20 11:56:00 +01001029 ALOGV("AudioCommandThread() processing set voice volume volume %f",
Eric Laurentde070132010-07-13 04:45:46 -07001030 data->mVolume);
Andy Hungfe726a62018-09-27 15:17:25 -07001031 mLock.unlock();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001032 command->mStatus = AudioSystem::setVoiceVolume(data->mVolume);
Andy Hungfe726a62018-09-27 15:17:25 -07001033 mLock.lock();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001034 }break;
Eric Laurentbfb1b832013-01-07 09:53:42 -08001035 case STOP_OUTPUT: {
Eric Laurent0ede8922014-05-09 18:04:42 -07001036 StopOutputData *data = (StopOutputData *)command->mParam.get();
Eric Laurentd7fe0862018-07-14 16:48:01 -07001037 ALOGV("AudioCommandThread() processing stop output portId %d",
1038 data->mPortId);
Eric Laurent59a89232014-06-08 14:14:17 -07001039 svc = mService.promote();
Eric Laurentbfb1b832013-01-07 09:53:42 -08001040 if (svc == 0) {
1041 break;
1042 }
1043 mLock.unlock();
Eric Laurentd7fe0862018-07-14 16:48:01 -07001044 svc->doStopOutput(data->mPortId);
Eric Laurentbfb1b832013-01-07 09:53:42 -08001045 mLock.lock();
Eric Laurentbfb1b832013-01-07 09:53:42 -08001046 }break;
1047 case RELEASE_OUTPUT: {
Eric Laurent0ede8922014-05-09 18:04:42 -07001048 ReleaseOutputData *data = (ReleaseOutputData *)command->mParam.get();
Eric Laurentd7fe0862018-07-14 16:48:01 -07001049 ALOGV("AudioCommandThread() processing release output portId %d",
1050 data->mPortId);
Eric Laurent59a89232014-06-08 14:14:17 -07001051 svc = mService.promote();
Eric Laurentbfb1b832013-01-07 09:53:42 -08001052 if (svc == 0) {
1053 break;
1054 }
1055 mLock.unlock();
Eric Laurentd7fe0862018-07-14 16:48:01 -07001056 svc->doReleaseOutput(data->mPortId);
Eric Laurentbfb1b832013-01-07 09:53:42 -08001057 mLock.lock();
Eric Laurentbfb1b832013-01-07 09:53:42 -08001058 }break;
Eric Laurent951f4552014-05-20 10:48:17 -07001059 case CREATE_AUDIO_PATCH: {
1060 CreateAudioPatchData *data = (CreateAudioPatchData *)command->mParam.get();
1061 ALOGV("AudioCommandThread() processing create audio patch");
1062 sp<IAudioFlinger> af = AudioSystem::get_audio_flinger();
1063 if (af == 0) {
1064 command->mStatus = PERMISSION_DENIED;
1065 } else {
Andy Hungfe726a62018-09-27 15:17:25 -07001066 mLock.unlock();
Eric Laurent951f4552014-05-20 10:48:17 -07001067 command->mStatus = af->createAudioPatch(&data->mPatch, &data->mHandle);
Andy Hungfe726a62018-09-27 15:17:25 -07001068 mLock.lock();
Eric Laurent951f4552014-05-20 10:48:17 -07001069 }
1070 } break;
1071 case RELEASE_AUDIO_PATCH: {
1072 ReleaseAudioPatchData *data = (ReleaseAudioPatchData *)command->mParam.get();
1073 ALOGV("AudioCommandThread() processing release audio patch");
1074 sp<IAudioFlinger> af = AudioSystem::get_audio_flinger();
1075 if (af == 0) {
1076 command->mStatus = PERMISSION_DENIED;
1077 } else {
Andy Hungfe726a62018-09-27 15:17:25 -07001078 mLock.unlock();
Eric Laurent951f4552014-05-20 10:48:17 -07001079 command->mStatus = af->releaseAudioPatch(data->mHandle);
Andy Hungfe726a62018-09-27 15:17:25 -07001080 mLock.lock();
Eric Laurent951f4552014-05-20 10:48:17 -07001081 }
1082 } break;
Eric Laurentb52c1522014-05-20 11:27:36 -07001083 case UPDATE_AUDIOPORT_LIST: {
1084 ALOGV("AudioCommandThread() processing update audio port list");
Eric Laurent59a89232014-06-08 14:14:17 -07001085 svc = mService.promote();
Eric Laurentb52c1522014-05-20 11:27:36 -07001086 if (svc == 0) {
1087 break;
1088 }
1089 mLock.unlock();
1090 svc->doOnAudioPortListUpdate();
1091 mLock.lock();
1092 }break;
1093 case UPDATE_AUDIOPATCH_LIST: {
1094 ALOGV("AudioCommandThread() processing update audio patch list");
Eric Laurent59a89232014-06-08 14:14:17 -07001095 svc = mService.promote();
Eric Laurentb52c1522014-05-20 11:27:36 -07001096 if (svc == 0) {
1097 break;
1098 }
1099 mLock.unlock();
1100 svc->doOnAudioPatchListUpdate();
1101 mLock.lock();
1102 }break;
François Gaffiecfe17322018-11-07 13:41:29 +01001103 case CHANGED_AUDIOVOLUMEGROUP: {
1104 AudioVolumeGroupData *data =
1105 static_cast<AudioVolumeGroupData *>(command->mParam.get());
1106 ALOGV("AudioCommandThread() processing update audio volume group");
1107 svc = mService.promote();
1108 if (svc == 0) {
1109 break;
1110 }
1111 mLock.unlock();
1112 svc->doOnAudioVolumeGroupChanged(data->mGroup, data->mFlags);
1113 mLock.lock();
1114 }break;
Eric Laurente1715a42014-05-20 11:30:42 -07001115 case SET_AUDIOPORT_CONFIG: {
1116 SetAudioPortConfigData *data = (SetAudioPortConfigData *)command->mParam.get();
1117 ALOGV("AudioCommandThread() processing set port config");
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 Laurente1715a42014-05-20 11:30:42 -07001123 command->mStatus = af->setAudioPortConfig(&data->mConfig);
Andy Hungfe726a62018-09-27 15:17:25 -07001124 mLock.lock();
Eric Laurente1715a42014-05-20 11:30:42 -07001125 }
1126 } break;
Jean-Michel Trivide801052015-04-14 19:10:14 -07001127 case DYN_POLICY_MIX_STATE_UPDATE: {
1128 DynPolicyMixStateUpdateData *data =
1129 (DynPolicyMixStateUpdateData *)command->mParam.get();
Jean-Michel Trivide801052015-04-14 19:10:14 -07001130 ALOGV("AudioCommandThread() processing dyn policy mix state update %s %d",
1131 data->mRegId.string(), data->mState);
1132 svc = mService.promote();
1133 if (svc == 0) {
1134 break;
1135 }
1136 mLock.unlock();
1137 svc->doOnDynamicPolicyMixStateUpdate(data->mRegId, data->mState);
1138 mLock.lock();
1139 } break;
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -08001140 case RECORDING_CONFIGURATION_UPDATE: {
1141 RecordingConfigurationUpdateData *data =
1142 (RecordingConfigurationUpdateData *)command->mParam.get();
1143 ALOGV("AudioCommandThread() processing recording configuration update");
1144 svc = mService.promote();
1145 if (svc == 0) {
1146 break;
1147 }
1148 mLock.unlock();
Jean-Michel Triviac4e4292016-12-22 11:39:31 -08001149 svc->doOnRecordingConfigurationUpdate(data->mEvent, &data->mClientInfo,
Eric Laurenta9f86652018-11-28 17:23:11 -08001150 &data->mClientConfig, data->mClientEffects,
1151 &data->mDeviceConfig, data->mEffects,
1152 data->mPatchHandle, data->mSource);
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -08001153 mLock.lock();
1154 } break;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001155 default:
Steve Block5ff1dd52012-01-05 23:22:43 +00001156 ALOGW("AudioCommandThread() unknown command %d", command->mCommand);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001157 }
Eric Laurent0ede8922014-05-09 18:04:42 -07001158 {
1159 Mutex::Autolock _l(command->mLock);
1160 if (command->mWaitStatus) {
1161 command->mWaitStatus = false;
1162 command->mCond.signal();
1163 }
1164 }
Eric Laurentd7eda8d2016-02-02 17:18:39 -08001165 waitTime = -1;
Zach Janga754b4f2015-10-27 01:29:34 +00001166 // release mLock before releasing strong reference on the service as
1167 // AudioPolicyService destructor calls AudioCommandThread::exit() which
1168 // acquires mLock.
1169 mLock.unlock();
1170 svc.clear();
1171 mLock.lock();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001172 } else {
1173 waitTime = mAudioCommands[0]->mTime - curTime;
1174 break;
1175 }
1176 }
Zach Janga754b4f2015-10-27 01:29:34 +00001177
1178 // release delayed commands wake lock if the queue is empty
1179 if (mAudioCommands.isEmpty()) {
Ricardo Garcia05f2fdc2014-07-24 15:48:24 -07001180 release_wake_lock(mName.string());
Zach Janga754b4f2015-10-27 01:29:34 +00001181 }
1182
1183 // At this stage we have either an empty command queue or the first command in the queue
1184 // has a finite delay. So unless we are exiting it is safe to wait.
1185 if (!exitPending()) {
Eric Laurent59a89232014-06-08 14:14:17 -07001186 ALOGV("AudioCommandThread() going to sleep");
Eric Laurentd7eda8d2016-02-02 17:18:39 -08001187 if (waitTime == -1) {
1188 mWaitWorkCV.wait(mLock);
1189 } else {
1190 mWaitWorkCV.waitRelative(mLock, waitTime);
1191 }
Eric Laurent59a89232014-06-08 14:14:17 -07001192 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001193 }
Ricardo Garcia05f2fdc2014-07-24 15:48:24 -07001194 // release delayed commands wake lock before quitting
1195 if (!mAudioCommands.isEmpty()) {
1196 release_wake_lock(mName.string());
1197 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001198 mLock.unlock();
1199 return false;
1200}
1201
1202status_t AudioPolicyService::AudioCommandThread::dump(int fd)
1203{
1204 const size_t SIZE = 256;
1205 char buffer[SIZE];
1206 String8 result;
1207
1208 snprintf(buffer, SIZE, "AudioCommandThread %p Dump\n", this);
1209 result.append(buffer);
1210 write(fd, result.string(), result.size());
1211
1212 bool locked = tryLock(mLock);
1213 if (!locked) {
1214 String8 result2(kCmdDeadlockedString);
1215 write(fd, result2.string(), result2.size());
1216 }
1217
1218 snprintf(buffer, SIZE, "- Commands:\n");
1219 result = String8(buffer);
1220 result.append(" Command Time Wait pParam\n");
Glenn Kasten8d6a2442012-02-08 14:04:28 -08001221 for (size_t i = 0; i < mAudioCommands.size(); i++) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07001222 mAudioCommands[i]->dump(buffer, SIZE);
1223 result.append(buffer);
1224 }
1225 result.append(" Last Command\n");
Eric Laurent0ede8922014-05-09 18:04:42 -07001226 if (mLastCommand != 0) {
1227 mLastCommand->dump(buffer, SIZE);
1228 result.append(buffer);
1229 } else {
1230 result.append(" none\n");
1231 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001232
1233 write(fd, result.string(), result.size());
1234
1235 if (locked) mLock.unlock();
1236
1237 return NO_ERROR;
1238}
1239
Glenn Kastenfff6d712012-01-12 16:38:12 -08001240status_t AudioPolicyService::AudioCommandThread::volumeCommand(audio_stream_type_t stream,
Eric Laurentde070132010-07-13 04:45:46 -07001241 float volume,
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001242 audio_io_handle_t output,
Eric Laurentde070132010-07-13 04:45:46 -07001243 int delayMs)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001244{
Eric Laurent0ede8922014-05-09 18:04:42 -07001245 sp<AudioCommand> command = new AudioCommand();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001246 command->mCommand = SET_VOLUME;
Eric Laurent0ede8922014-05-09 18:04:42 -07001247 sp<VolumeData> data = new VolumeData();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001248 data->mStream = stream;
1249 data->mVolume = volume;
1250 data->mIO = output;
1251 command->mParam = data;
Eric Laurent0ede8922014-05-09 18:04:42 -07001252 command->mWaitStatus = true;
Steve Block3856b092011-10-20 11:56:00 +01001253 ALOGV("AudioCommandThread() adding set volume stream %d, volume %f, output %d",
Eric Laurentde070132010-07-13 04:45:46 -07001254 stream, volume, output);
Eric Laurent0ede8922014-05-09 18:04:42 -07001255 return sendCommand(command, delayMs);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001256}
1257
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001258status_t AudioPolicyService::AudioCommandThread::parametersCommand(audio_io_handle_t ioHandle,
Dima Zavinfce7a472011-04-19 22:30:36 -07001259 const char *keyValuePairs,
Eric Laurentde070132010-07-13 04:45:46 -07001260 int delayMs)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001261{
Eric Laurent0ede8922014-05-09 18:04:42 -07001262 sp<AudioCommand> command = new AudioCommand();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001263 command->mCommand = SET_PARAMETERS;
Eric Laurent0ede8922014-05-09 18:04:42 -07001264 sp<ParametersData> data = new ParametersData();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001265 data->mIO = ioHandle;
Dima Zavinfce7a472011-04-19 22:30:36 -07001266 data->mKeyValuePairs = String8(keyValuePairs);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001267 command->mParam = data;
Eric Laurent0ede8922014-05-09 18:04:42 -07001268 command->mWaitStatus = true;
Steve Block3856b092011-10-20 11:56:00 +01001269 ALOGV("AudioCommandThread() adding set parameter string %s, io %d ,delay %d",
Dima Zavinfce7a472011-04-19 22:30:36 -07001270 keyValuePairs, ioHandle, delayMs);
Eric Laurent0ede8922014-05-09 18:04:42 -07001271 return sendCommand(command, delayMs);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001272}
1273
1274status_t AudioPolicyService::AudioCommandThread::voiceVolumeCommand(float volume, int delayMs)
1275{
Eric Laurent0ede8922014-05-09 18:04:42 -07001276 sp<AudioCommand> command = new AudioCommand();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001277 command->mCommand = SET_VOICE_VOLUME;
Eric Laurent0ede8922014-05-09 18:04:42 -07001278 sp<VoiceVolumeData> data = new VoiceVolumeData();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001279 data->mVolume = volume;
1280 command->mParam = data;
Eric Laurent0ede8922014-05-09 18:04:42 -07001281 command->mWaitStatus = true;
Steve Block3856b092011-10-20 11:56:00 +01001282 ALOGV("AudioCommandThread() adding set voice volume volume %f", volume);
Eric Laurent0ede8922014-05-09 18:04:42 -07001283 return sendCommand(command, delayMs);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001284}
1285
Eric Laurentd7fe0862018-07-14 16:48:01 -07001286void AudioPolicyService::AudioCommandThread::stopOutputCommand(audio_port_handle_t portId)
Eric Laurentbfb1b832013-01-07 09:53:42 -08001287{
Eric Laurent0ede8922014-05-09 18:04:42 -07001288 sp<AudioCommand> command = new AudioCommand();
Eric Laurentbfb1b832013-01-07 09:53:42 -08001289 command->mCommand = STOP_OUTPUT;
Eric Laurent0ede8922014-05-09 18:04:42 -07001290 sp<StopOutputData> data = new StopOutputData();
Eric Laurentd7fe0862018-07-14 16:48:01 -07001291 data->mPortId = portId;
Jesper Tragardh48412dc2014-03-24 14:12:43 +01001292 command->mParam = data;
Eric Laurentd7fe0862018-07-14 16:48:01 -07001293 ALOGV("AudioCommandThread() adding stop output portId %d", portId);
Eric Laurent0ede8922014-05-09 18:04:42 -07001294 sendCommand(command);
Eric Laurentbfb1b832013-01-07 09:53:42 -08001295}
1296
Eric Laurentd7fe0862018-07-14 16:48:01 -07001297void AudioPolicyService::AudioCommandThread::releaseOutputCommand(audio_port_handle_t portId)
Eric Laurentbfb1b832013-01-07 09:53:42 -08001298{
Eric Laurent0ede8922014-05-09 18:04:42 -07001299 sp<AudioCommand> command = new AudioCommand();
Eric Laurentbfb1b832013-01-07 09:53:42 -08001300 command->mCommand = RELEASE_OUTPUT;
Eric Laurent0ede8922014-05-09 18:04:42 -07001301 sp<ReleaseOutputData> data = new ReleaseOutputData();
Eric Laurentd7fe0862018-07-14 16:48:01 -07001302 data->mPortId = portId;
Jesper Tragardh48412dc2014-03-24 14:12:43 +01001303 command->mParam = data;
Eric Laurentd7fe0862018-07-14 16:48:01 -07001304 ALOGV("AudioCommandThread() adding release output portId %d", portId);
Eric Laurent0ede8922014-05-09 18:04:42 -07001305 sendCommand(command);
1306}
1307
Eric Laurent951f4552014-05-20 10:48:17 -07001308status_t AudioPolicyService::AudioCommandThread::createAudioPatchCommand(
1309 const struct audio_patch *patch,
1310 audio_patch_handle_t *handle,
1311 int delayMs)
1312{
1313 status_t status = NO_ERROR;
1314
1315 sp<AudioCommand> command = new AudioCommand();
1316 command->mCommand = CREATE_AUDIO_PATCH;
1317 CreateAudioPatchData *data = new CreateAudioPatchData();
1318 data->mPatch = *patch;
1319 data->mHandle = *handle;
1320 command->mParam = data;
1321 command->mWaitStatus = true;
1322 ALOGV("AudioCommandThread() adding create patch delay %d", delayMs);
1323 status = sendCommand(command, delayMs);
1324 if (status == NO_ERROR) {
1325 *handle = data->mHandle;
1326 }
1327 return status;
1328}
1329
1330status_t AudioPolicyService::AudioCommandThread::releaseAudioPatchCommand(audio_patch_handle_t handle,
1331 int delayMs)
1332{
1333 sp<AudioCommand> command = new AudioCommand();
1334 command->mCommand = RELEASE_AUDIO_PATCH;
1335 ReleaseAudioPatchData *data = new ReleaseAudioPatchData();
1336 data->mHandle = handle;
1337 command->mParam = data;
1338 command->mWaitStatus = true;
1339 ALOGV("AudioCommandThread() adding release patch delay %d", delayMs);
1340 return sendCommand(command, delayMs);
1341}
1342
Eric Laurentb52c1522014-05-20 11:27:36 -07001343void AudioPolicyService::AudioCommandThread::updateAudioPortListCommand()
1344{
1345 sp<AudioCommand> command = new AudioCommand();
1346 command->mCommand = UPDATE_AUDIOPORT_LIST;
1347 ALOGV("AudioCommandThread() adding update audio port list");
1348 sendCommand(command);
1349}
1350
1351void AudioPolicyService::AudioCommandThread::updateAudioPatchListCommand()
1352{
1353 sp<AudioCommand>command = new AudioCommand();
1354 command->mCommand = UPDATE_AUDIOPATCH_LIST;
1355 ALOGV("AudioCommandThread() adding update audio patch list");
1356 sendCommand(command);
1357}
1358
François Gaffiecfe17322018-11-07 13:41:29 +01001359void AudioPolicyService::AudioCommandThread::changeAudioVolumeGroupCommand(volume_group_t group,
1360 int flags)
1361{
1362 sp<AudioCommand>command = new AudioCommand();
1363 command->mCommand = CHANGED_AUDIOVOLUMEGROUP;
1364 AudioVolumeGroupData *data= new AudioVolumeGroupData();
1365 data->mGroup = group;
1366 data->mFlags = flags;
1367 command->mParam = data;
1368 ALOGV("AudioCommandThread() adding audio volume group changed");
1369 sendCommand(command);
1370}
1371
Eric Laurente1715a42014-05-20 11:30:42 -07001372status_t AudioPolicyService::AudioCommandThread::setAudioPortConfigCommand(
1373 const struct audio_port_config *config, int delayMs)
1374{
1375 sp<AudioCommand> command = new AudioCommand();
1376 command->mCommand = SET_AUDIOPORT_CONFIG;
1377 SetAudioPortConfigData *data = new SetAudioPortConfigData();
1378 data->mConfig = *config;
1379 command->mParam = data;
1380 command->mWaitStatus = true;
1381 ALOGV("AudioCommandThread() adding set port config delay %d", delayMs);
1382 return sendCommand(command, delayMs);
1383}
1384
Jean-Michel Trivide801052015-04-14 19:10:14 -07001385void AudioPolicyService::AudioCommandThread::dynamicPolicyMixStateUpdateCommand(
Chih-Hung Hsiehe964d4e2016-08-09 14:31:32 -07001386 const String8& regId, int32_t state)
Jean-Michel Trivide801052015-04-14 19:10:14 -07001387{
1388 sp<AudioCommand> command = new AudioCommand();
1389 command->mCommand = DYN_POLICY_MIX_STATE_UPDATE;
1390 DynPolicyMixStateUpdateData *data = new DynPolicyMixStateUpdateData();
1391 data->mRegId = regId;
1392 data->mState = state;
1393 command->mParam = data;
1394 ALOGV("AudioCommandThread() sending dynamic policy mix (id=%s) state update to %d",
1395 regId.string(), state);
1396 sendCommand(command);
1397}
1398
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -08001399void AudioPolicyService::AudioCommandThread::recordingConfigurationUpdateCommand(
Eric Laurenta9f86652018-11-28 17:23:11 -08001400 int event,
1401 const record_client_info_t *clientInfo,
1402 const audio_config_base_t *clientConfig,
1403 std::vector<effect_descriptor_t> clientEffects,
1404 const audio_config_base_t *deviceConfig,
1405 std::vector<effect_descriptor_t> effects,
1406 audio_patch_handle_t patchHandle,
1407 audio_source_t source)
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -08001408{
1409 sp<AudioCommand>command = new AudioCommand();
1410 command->mCommand = RECORDING_CONFIGURATION_UPDATE;
1411 RecordingConfigurationUpdateData *data = new RecordingConfigurationUpdateData();
1412 data->mEvent = event;
Jean-Michel Triviac4e4292016-12-22 11:39:31 -08001413 data->mClientInfo = *clientInfo;
Jean-Michel Trivi7281aa92016-02-17 15:33:40 -08001414 data->mClientConfig = *clientConfig;
Eric Laurenta9f86652018-11-28 17:23:11 -08001415 data->mClientEffects = clientEffects;
Jean-Michel Trivi7281aa92016-02-17 15:33:40 -08001416 data->mDeviceConfig = *deviceConfig;
Eric Laurenta9f86652018-11-28 17:23:11 -08001417 data->mEffects = effects;
Jean-Michel Trivi8c7cf3b2016-02-25 17:08:24 -08001418 data->mPatchHandle = patchHandle;
Eric Laurenta9f86652018-11-28 17:23:11 -08001419 data->mSource = source;
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -08001420 command->mParam = data;
Jean-Michel Triviac4e4292016-12-22 11:39:31 -08001421 ALOGV("AudioCommandThread() adding recording configuration update event %d, source %d uid %u",
1422 event, clientInfo->source, clientInfo->uid);
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -08001423 sendCommand(command);
1424}
1425
Eric Laurent0ede8922014-05-09 18:04:42 -07001426status_t AudioPolicyService::AudioCommandThread::sendCommand(sp<AudioCommand>& command, int delayMs)
1427{
1428 {
1429 Mutex::Autolock _l(mLock);
1430 insertCommand_l(command, delayMs);
1431 mWaitWorkCV.signal();
1432 }
1433 Mutex::Autolock _l(command->mLock);
1434 while (command->mWaitStatus) {
1435 nsecs_t timeOutNs = kAudioCommandTimeoutNs + milliseconds(delayMs);
1436 if (command->mCond.waitRelative(command->mLock, timeOutNs) != NO_ERROR) {
1437 command->mStatus = TIMED_OUT;
1438 command->mWaitStatus = false;
1439 }
1440 }
1441 return command->mStatus;
Eric Laurentbfb1b832013-01-07 09:53:42 -08001442}
1443
Mathias Agopian65ab4712010-07-14 17:59:35 -07001444// insertCommand_l() must be called with mLock held
Eric Laurent0ede8922014-05-09 18:04:42 -07001445void AudioPolicyService::AudioCommandThread::insertCommand_l(sp<AudioCommand>& command, int delayMs)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001446{
Glenn Kasten8d6a2442012-02-08 14:04:28 -08001447 ssize_t i; // not size_t because i will count down to -1
Eric Laurent0ede8922014-05-09 18:04:42 -07001448 Vector < sp<AudioCommand> > removedCommands;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001449 command->mTime = systemTime() + milliseconds(delayMs);
1450
1451 // acquire wake lock to make sure delayed commands are processed
Eric Laurentbfb1b832013-01-07 09:53:42 -08001452 if (mAudioCommands.isEmpty()) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07001453 acquire_wake_lock(PARTIAL_WAKE_LOCK, mName.string());
1454 }
1455
1456 // check same pending commands with later time stamps and eliminate them
Ivan Lozano5ff158f2017-10-30 09:06:24 -07001457 for (i = (ssize_t)mAudioCommands.size()-1; i >= 0; i--) {
Eric Laurent0ede8922014-05-09 18:04:42 -07001458 sp<AudioCommand> command2 = mAudioCommands[i];
Mathias Agopian65ab4712010-07-14 17:59:35 -07001459 // commands are sorted by increasing time stamp: no need to scan the rest of mAudioCommands
1460 if (command2->mTime <= command->mTime) break;
Eric Laurente45b48a2014-09-04 16:40:57 -07001461
1462 // create audio patch or release audio patch commands are equivalent
1463 // with regard to filtering
1464 if ((command->mCommand == CREATE_AUDIO_PATCH) ||
1465 (command->mCommand == RELEASE_AUDIO_PATCH)) {
1466 if ((command2->mCommand != CREATE_AUDIO_PATCH) &&
1467 (command2->mCommand != RELEASE_AUDIO_PATCH)) {
1468 continue;
1469 }
1470 } else if (command2->mCommand != command->mCommand) continue;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001471
1472 switch (command->mCommand) {
1473 case SET_PARAMETERS: {
Eric Laurent0ede8922014-05-09 18:04:42 -07001474 ParametersData *data = (ParametersData *)command->mParam.get();
1475 ParametersData *data2 = (ParametersData *)command2->mParam.get();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001476 if (data->mIO != data2->mIO) break;
Steve Block3856b092011-10-20 11:56:00 +01001477 ALOGV("Comparing parameter command %s to new command %s",
Eric Laurentde070132010-07-13 04:45:46 -07001478 data2->mKeyValuePairs.string(), data->mKeyValuePairs.string());
Mathias Agopian65ab4712010-07-14 17:59:35 -07001479 AudioParameter param = AudioParameter(data->mKeyValuePairs);
1480 AudioParameter param2 = AudioParameter(data2->mKeyValuePairs);
1481 for (size_t j = 0; j < param.size(); j++) {
Glenn Kastene53b9ea2012-03-12 16:29:55 -07001482 String8 key;
1483 String8 value;
1484 param.getAt(j, key, value);
1485 for (size_t k = 0; k < param2.size(); k++) {
1486 String8 key2;
1487 String8 value2;
1488 param2.getAt(k, key2, value2);
1489 if (key2 == key) {
1490 param2.remove(key2);
1491 ALOGV("Filtering out parameter %s", key2.string());
1492 break;
1493 }
1494 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001495 }
1496 // if all keys have been filtered out, remove the command.
1497 // otherwise, update the key value pairs
1498 if (param2.size() == 0) {
1499 removedCommands.add(command2);
1500 } else {
1501 data2->mKeyValuePairs = param2.toString();
1502 }
Eric Laurent21e54562013-09-23 12:08:05 -07001503 command->mTime = command2->mTime;
1504 // force delayMs to non 0 so that code below does not request to wait for
1505 // command status as the command is now delayed
1506 delayMs = 1;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001507 } break;
1508
1509 case SET_VOLUME: {
Eric Laurent0ede8922014-05-09 18:04:42 -07001510 VolumeData *data = (VolumeData *)command->mParam.get();
1511 VolumeData *data2 = (VolumeData *)command2->mParam.get();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001512 if (data->mIO != data2->mIO) break;
1513 if (data->mStream != data2->mStream) break;
Steve Block3856b092011-10-20 11:56:00 +01001514 ALOGV("Filtering out volume command on output %d for stream %d",
Eric Laurentde070132010-07-13 04:45:46 -07001515 data->mIO, data->mStream);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001516 removedCommands.add(command2);
Eric Laurent21e54562013-09-23 12:08:05 -07001517 command->mTime = command2->mTime;
1518 // force delayMs to non 0 so that code below does not request to wait for
1519 // command status as the command is now delayed
1520 delayMs = 1;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001521 } break;
Eric Laurente45b48a2014-09-04 16:40:57 -07001522
Eric Laurentbaf35fe2016-07-27 15:36:53 -07001523 case SET_VOICE_VOLUME: {
1524 VoiceVolumeData *data = (VoiceVolumeData *)command->mParam.get();
1525 VoiceVolumeData *data2 = (VoiceVolumeData *)command2->mParam.get();
1526 ALOGV("Filtering out voice volume command value %f replaced by %f",
1527 data2->mVolume, data->mVolume);
1528 removedCommands.add(command2);
1529 command->mTime = command2->mTime;
1530 // force delayMs to non 0 so that code below does not request to wait for
1531 // command status as the command is now delayed
1532 delayMs = 1;
1533 } break;
1534
Eric Laurente45b48a2014-09-04 16:40:57 -07001535 case CREATE_AUDIO_PATCH:
1536 case RELEASE_AUDIO_PATCH: {
1537 audio_patch_handle_t handle;
Haynes Mathew Georgea2d4a6d2014-10-13 13:05:22 -07001538 struct audio_patch patch;
Eric Laurente45b48a2014-09-04 16:40:57 -07001539 if (command->mCommand == CREATE_AUDIO_PATCH) {
1540 handle = ((CreateAudioPatchData *)command->mParam.get())->mHandle;
Haynes Mathew Georgea2d4a6d2014-10-13 13:05:22 -07001541 patch = ((CreateAudioPatchData *)command->mParam.get())->mPatch;
Eric Laurente45b48a2014-09-04 16:40:57 -07001542 } else {
1543 handle = ((ReleaseAudioPatchData *)command->mParam.get())->mHandle;
Mikhail Naganov7be71d22018-05-23 16:51:46 -07001544 memset(&patch, 0, sizeof(patch));
Eric Laurente45b48a2014-09-04 16:40:57 -07001545 }
1546 audio_patch_handle_t handle2;
Haynes Mathew Georgea2d4a6d2014-10-13 13:05:22 -07001547 struct audio_patch patch2;
Eric Laurente45b48a2014-09-04 16:40:57 -07001548 if (command2->mCommand == CREATE_AUDIO_PATCH) {
1549 handle2 = ((CreateAudioPatchData *)command2->mParam.get())->mHandle;
Haynes Mathew Georgea2d4a6d2014-10-13 13:05:22 -07001550 patch2 = ((CreateAudioPatchData *)command2->mParam.get())->mPatch;
Eric Laurente45b48a2014-09-04 16:40:57 -07001551 } else {
1552 handle2 = ((ReleaseAudioPatchData *)command2->mParam.get())->mHandle;
Glenn Kastenf60b6b62015-07-06 10:53:26 -07001553 memset(&patch2, 0, sizeof(patch2));
Eric Laurente45b48a2014-09-04 16:40:57 -07001554 }
1555 if (handle != handle2) break;
Haynes Mathew Georgea2d4a6d2014-10-13 13:05:22 -07001556 /* Filter CREATE_AUDIO_PATCH commands only when they are issued for
1557 same output. */
1558 if( (command->mCommand == CREATE_AUDIO_PATCH) &&
1559 (command2->mCommand == CREATE_AUDIO_PATCH) ) {
1560 bool isOutputDiff = false;
1561 if (patch.num_sources == patch2.num_sources) {
1562 for (unsigned count = 0; count < patch.num_sources; count++) {
1563 if (patch.sources[count].id != patch2.sources[count].id) {
1564 isOutputDiff = true;
1565 break;
1566 }
1567 }
1568 if (isOutputDiff)
1569 break;
1570 }
1571 }
Eric Laurente45b48a2014-09-04 16:40:57 -07001572 ALOGV("Filtering out %s audio patch command for handle %d",
1573 (command->mCommand == CREATE_AUDIO_PATCH) ? "create" : "release", handle);
1574 removedCommands.add(command2);
1575 command->mTime = command2->mTime;
1576 // force delayMs to non 0 so that code below does not request to wait for
1577 // command status as the command is now delayed
1578 delayMs = 1;
1579 } break;
1580
Jean-Michel Trivide801052015-04-14 19:10:14 -07001581 case DYN_POLICY_MIX_STATE_UPDATE: {
1582
1583 } break;
1584
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -08001585 case RECORDING_CONFIGURATION_UPDATE: {
1586
1587 } break;
1588
Mathias Agopian65ab4712010-07-14 17:59:35 -07001589 default:
1590 break;
1591 }
1592 }
1593
1594 // remove filtered commands
1595 for (size_t j = 0; j < removedCommands.size(); j++) {
1596 // removed commands always have time stamps greater than current command
1597 for (size_t k = i + 1; k < mAudioCommands.size(); k++) {
Eric Laurent0ede8922014-05-09 18:04:42 -07001598 if (mAudioCommands[k].get() == removedCommands[j].get()) {
Steve Block3856b092011-10-20 11:56:00 +01001599 ALOGV("suppressing command: %d", mAudioCommands[k]->mCommand);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001600 mAudioCommands.removeAt(k);
1601 break;
1602 }
1603 }
1604 }
1605 removedCommands.clear();
1606
Eric Laurentaa79bef2015-01-15 14:33:51 -08001607 // Disable wait for status if delay is not 0.
1608 // Except for create audio patch command because the returned patch handle
1609 // is needed by audio policy manager
1610 if (delayMs != 0 && command->mCommand != CREATE_AUDIO_PATCH) {
Eric Laurentcec4abb2012-07-03 12:23:02 -07001611 command->mWaitStatus = false;
1612 }
Eric Laurentcec4abb2012-07-03 12:23:02 -07001613
Mathias Agopian65ab4712010-07-14 17:59:35 -07001614 // insert command at the right place according to its time stamp
Eric Laurent1e693b52014-07-09 15:03:28 -07001615 ALOGV("inserting command: %d at index %zd, num commands %zu",
1616 command->mCommand, i+1, mAudioCommands.size());
Mathias Agopian65ab4712010-07-14 17:59:35 -07001617 mAudioCommands.insertAt(command, i + 1);
1618}
1619
1620void AudioPolicyService::AudioCommandThread::exit()
1621{
Steve Block3856b092011-10-20 11:56:00 +01001622 ALOGV("AudioCommandThread::exit");
Mathias Agopian65ab4712010-07-14 17:59:35 -07001623 {
1624 AutoMutex _l(mLock);
1625 requestExit();
1626 mWaitWorkCV.signal();
1627 }
Zach Janga754b4f2015-10-27 01:29:34 +00001628 // Note that we can call it from the thread loop if all other references have been released
1629 // but it will safely return WOULD_BLOCK in this case
Mathias Agopian65ab4712010-07-14 17:59:35 -07001630 requestExitAndWait();
1631}
1632
1633void AudioPolicyService::AudioCommandThread::AudioCommand::dump(char* buffer, size_t size)
1634{
1635 snprintf(buffer, size, " %02d %06d.%03d %01u %p\n",
1636 mCommand,
1637 (int)ns2s(mTime),
1638 (int)ns2ms(mTime)%1000,
1639 mWaitStatus,
Eric Laurent0ede8922014-05-09 18:04:42 -07001640 mParam.get());
Mathias Agopian65ab4712010-07-14 17:59:35 -07001641}
1642
Dima Zavinfce7a472011-04-19 22:30:36 -07001643/******* helpers for the service_ops callbacks defined below *********/
1644void AudioPolicyService::setParameters(audio_io_handle_t ioHandle,
1645 const char *keyValuePairs,
1646 int delayMs)
1647{
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001648 mAudioCommandThread->parametersCommand(ioHandle, keyValuePairs,
Dima Zavinfce7a472011-04-19 22:30:36 -07001649 delayMs);
1650}
1651
1652int AudioPolicyService::setStreamVolume(audio_stream_type_t stream,
1653 float volume,
1654 audio_io_handle_t output,
1655 int delayMs)
1656{
Glenn Kastenfff6d712012-01-12 16:38:12 -08001657 return (int)mAudioCommandThread->volumeCommand(stream, volume,
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001658 output, delayMs);
Dima Zavinfce7a472011-04-19 22:30:36 -07001659}
1660
Dima Zavinfce7a472011-04-19 22:30:36 -07001661int AudioPolicyService::setVoiceVolume(float volume, int delayMs)
1662{
1663 return (int)mAudioCommandThread->voiceVolumeCommand(volume, delayMs);
1664}
1665
Dima Zavinfce7a472011-04-19 22:30:36 -07001666extern "C" {
Eric Laurent2d388ec2014-03-07 13:25:54 -08001667audio_module_handle_t aps_load_hw_module(void *service __unused,
1668 const char *name);
1669audio_io_handle_t aps_open_output(void *service __unused,
Eric Laurenta4c5a552012-03-29 10:12:40 -07001670 audio_devices_t *pDevices,
1671 uint32_t *pSamplingRate,
1672 audio_format_t *pFormat,
1673 audio_channel_mask_t *pChannelMask,
1674 uint32_t *pLatencyMs,
Eric Laurent2d388ec2014-03-07 13:25:54 -08001675 audio_output_flags_t flags);
Eric Laurenta4c5a552012-03-29 10:12:40 -07001676
Eric Laurent2d388ec2014-03-07 13:25:54 -08001677audio_io_handle_t aps_open_output_on_module(void *service __unused,
Eric Laurenta4c5a552012-03-29 10:12:40 -07001678 audio_module_handle_t module,
1679 audio_devices_t *pDevices,
1680 uint32_t *pSamplingRate,
1681 audio_format_t *pFormat,
1682 audio_channel_mask_t *pChannelMask,
1683 uint32_t *pLatencyMs,
Richard Fitzgeraldad3af332013-03-25 16:54:37 +00001684 audio_output_flags_t flags,
Eric Laurent2d388ec2014-03-07 13:25:54 -08001685 const audio_offload_info_t *offloadInfo);
1686audio_io_handle_t aps_open_dup_output(void *service __unused,
Dima Zavinfce7a472011-04-19 22:30:36 -07001687 audio_io_handle_t output1,
Eric Laurent2d388ec2014-03-07 13:25:54 -08001688 audio_io_handle_t output2);
1689int aps_close_output(void *service __unused, audio_io_handle_t output);
1690int aps_suspend_output(void *service __unused, audio_io_handle_t output);
1691int aps_restore_output(void *service __unused, audio_io_handle_t output);
1692audio_io_handle_t aps_open_input(void *service __unused,
Eric Laurenta4c5a552012-03-29 10:12:40 -07001693 audio_devices_t *pDevices,
1694 uint32_t *pSamplingRate,
1695 audio_format_t *pFormat,
1696 audio_channel_mask_t *pChannelMask,
Eric Laurent2d388ec2014-03-07 13:25:54 -08001697 audio_in_acoustics_t acoustics __unused);
1698audio_io_handle_t aps_open_input_on_module(void *service __unused,
Eric Laurenta4c5a552012-03-29 10:12:40 -07001699 audio_module_handle_t module,
1700 audio_devices_t *pDevices,
1701 uint32_t *pSamplingRate,
1702 audio_format_t *pFormat,
Eric Laurent2d388ec2014-03-07 13:25:54 -08001703 audio_channel_mask_t *pChannelMask);
1704int aps_close_input(void *service __unused, audio_io_handle_t input);
1705int aps_invalidate_stream(void *service __unused, audio_stream_type_t stream);
Glenn Kastend848eb42016-03-08 13:42:11 -08001706int aps_move_effects(void *service __unused, audio_session_t session,
Dima Zavinfce7a472011-04-19 22:30:36 -07001707 audio_io_handle_t src_output,
Eric Laurent2d388ec2014-03-07 13:25:54 -08001708 audio_io_handle_t dst_output);
1709char * aps_get_parameters(void *service __unused, audio_io_handle_t io_handle,
1710 const char *keys);
1711void aps_set_parameters(void *service, audio_io_handle_t io_handle,
1712 const char *kv_pairs, int delay_ms);
1713int aps_set_stream_volume(void *service, audio_stream_type_t stream,
Dima Zavinfce7a472011-04-19 22:30:36 -07001714 float volume, audio_io_handle_t output,
Eric Laurent2d388ec2014-03-07 13:25:54 -08001715 int delay_ms);
Eric Laurent2d388ec2014-03-07 13:25:54 -08001716int aps_set_voice_volume(void *service, float volume, int delay_ms);
1717};
Dima Zavinfce7a472011-04-19 22:30:36 -07001718
Mikhail Naganov1b2a7942017-12-08 10:18:09 -08001719} // namespace android