blob: 018d9e36d6f032d1654fa37c3ed7471d44b2aeba [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>
Mathias Agopian65ab4712010-07-14 17:59:35 -070041
Dima Zavin64760242011-05-11 14:15:23 -070042#include <system/audio.h>
Dima Zavin7394a4f2011-06-13 18:16:26 -070043#include <system/audio_policy.h>
Mikhail Naganov61a4fac2016-10-13 14:44:18 -070044
Mathias Agopian65ab4712010-07-14 17:59:35 -070045namespace android {
46
Glenn Kasten8dad0e32012-01-09 08:41:22 -080047static const char kDeadlockedString[] = "AudioPolicyService may be deadlocked\n";
48static const char kCmdDeadlockedString[] = "AudioPolicyService command thread may be deadlocked\n";
Mathias Agopian65ab4712010-07-14 17:59:35 -070049
50static const int kDumpLockRetries = 50;
Glenn Kasten22ecc912012-01-09 08:33:38 -080051static const int kDumpLockSleepUs = 20000;
Mathias Agopian65ab4712010-07-14 17:59:35 -070052
Eric Laurent0ede8922014-05-09 18:04:42 -070053static const nsecs_t kAudioCommandTimeoutNs = seconds(3); // 3 seconds
Christer Fletcher5fa8c4b2013-01-18 15:27:03 +010054
Svet Ganovf4ddfef2018-01-16 07:37:58 -080055static const String16 sManageAudioPolicyPermission("android.permission.MANAGE_AUDIO_POLICY");
Dima Zavinfce7a472011-04-19 22:30:36 -070056
Mathias Agopian65ab4712010-07-14 17:59:35 -070057// ----------------------------------------------------------------------------
58
59AudioPolicyService::AudioPolicyService()
Eric Laurentdce54a12014-03-10 12:19:46 -070060 : BnAudioPolicyService(), mpAudioPolicyDev(NULL), mpAudioPolicy(NULL),
Eric Laurentbb6c9a02014-09-25 14:11:47 -070061 mAudioPolicyManager(NULL), mAudioPolicyClient(NULL), mPhoneState(AUDIO_MODE_INVALID)
Mathias Agopian65ab4712010-07-14 17:59:35 -070062{
Eric Laurentf5ada6e2014-10-09 17:49:00 -070063}
64
65void AudioPolicyService::onFirstRef()
66{
Eric Laurent8b1e80b2014-10-07 09:08:47 -070067 {
68 Mutex::Autolock _l(mLock);
Eric Laurent93575202011-01-18 18:39:02 -080069
Eric Laurent8b1e80b2014-10-07 09:08:47 -070070 // start audio commands thread
71 mAudioCommandThread = new AudioCommandThread(String8("ApmAudio"), this);
72 // start output activity command thread
73 mOutputCommandThread = new AudioCommandThread(String8("ApmOutput"), this);
Eric Laurentdce54a12014-03-10 12:19:46 -070074
Eric Laurent8b1e80b2014-10-07 09:08:47 -070075 mAudioPolicyClient = new AudioPolicyClient(this);
76 mAudioPolicyManager = createAudioPolicyManager(mAudioPolicyClient);
Eric Laurent8b1e80b2014-10-07 09:08:47 -070077 }
bryant_liuba2b4392014-06-11 16:49:30 +080078 // load audio processing modules
Eric Laurent8b1e80b2014-10-07 09:08:47 -070079 sp<AudioPolicyEffects>audioPolicyEffects = new AudioPolicyEffects();
80 {
81 Mutex::Autolock _l(mLock);
82 mAudioPolicyEffects = audioPolicyEffects;
83 }
Svet Ganovf4ddfef2018-01-16 07:37:58 -080084
85 mUidPolicy = new UidPolicy(this);
86 mUidPolicy->registerSelf();
Mathias Agopian65ab4712010-07-14 17:59:35 -070087}
88
89AudioPolicyService::~AudioPolicyService()
90{
Mathias Agopian65ab4712010-07-14 17:59:35 -070091 mAudioCommandThread->exit();
Eric Laurent657ff612014-05-07 11:58:24 -070092 mOutputCommandThread->exit();
Eric Laurent7c7f10b2011-06-17 21:29:58 -070093
Eric Laurentf269b8e2014-06-09 20:01:29 -070094 destroyAudioPolicyManager(mAudioPolicyManager);
Eric Laurentdce54a12014-03-10 12:19:46 -070095 delete mAudioPolicyClient;
Eric Laurentb52c1522014-05-20 11:27:36 -070096
97 mNotificationClients.clear();
bryant_liuba2b4392014-06-11 16:49:30 +080098 mAudioPolicyEffects.clear();
Svet Ganovf4ddfef2018-01-16 07:37:58 -080099
100 mUidPolicy->unregisterSelf();
101 mUidPolicy.clear();
Eric Laurentb52c1522014-05-20 11:27:36 -0700102}
103
104// A notification client is always registered by AudioSystem when the client process
105// connects to AudioPolicyService.
106void AudioPolicyService::registerClient(const sp<IAudioPolicyServiceClient>& client)
107{
Eric Laurent12590252015-08-21 18:40:20 -0700108 if (client == 0) {
109 ALOGW("%s got NULL client", __FUNCTION__);
110 return;
111 }
Eric Laurent0ebd5f92014-11-19 19:04:52 -0800112 Mutex::Autolock _l(mNotificationClientsLock);
Eric Laurentb52c1522014-05-20 11:27:36 -0700113
114 uid_t uid = IPCThreadState::self()->getCallingUid();
luochaojiang908c7d72018-06-21 14:58:04 +0800115 pid_t pid = IPCThreadState::self()->getCallingPid();
116 int64_t token = ((int64_t)uid<<32) | pid;
117
118 if (mNotificationClients.indexOfKey(token) < 0) {
Eric Laurentb52c1522014-05-20 11:27:36 -0700119 sp<NotificationClient> notificationClient = new NotificationClient(this,
120 client,
luochaojiang908c7d72018-06-21 14:58:04 +0800121 uid,
122 pid);
123 ALOGV("registerClient() client %p, uid %d pid %d", client.get(), uid, pid);
Eric Laurentb52c1522014-05-20 11:27:36 -0700124
luochaojiang908c7d72018-06-21 14:58:04 +0800125 mNotificationClients.add(token, notificationClient);
Eric Laurentb52c1522014-05-20 11:27:36 -0700126
Marco Nelissenf8880202014-11-14 07:58:25 -0800127 sp<IBinder> binder = IInterface::asBinder(client);
Eric Laurentb52c1522014-05-20 11:27:36 -0700128 binder->linkToDeath(notificationClient);
129 }
130}
131
Eric Laurente8726fe2015-06-26 09:39:24 -0700132void AudioPolicyService::setAudioPortCallbacksEnabled(bool enabled)
133{
134 Mutex::Autolock _l(mNotificationClientsLock);
135
136 uid_t uid = IPCThreadState::self()->getCallingUid();
luochaojiang908c7d72018-06-21 14:58:04 +0800137 pid_t pid = IPCThreadState::self()->getCallingPid();
138 int64_t token = ((int64_t)uid<<32) | pid;
139
140 if (mNotificationClients.indexOfKey(token) < 0) {
Eric Laurente8726fe2015-06-26 09:39:24 -0700141 return;
142 }
luochaojiang908c7d72018-06-21 14:58:04 +0800143 mNotificationClients.valueFor(token)->setAudioPortCallbacksEnabled(enabled);
Eric Laurente8726fe2015-06-26 09:39:24 -0700144}
145
Eric Laurentb52c1522014-05-20 11:27:36 -0700146// removeNotificationClient() is called when the client process dies.
luochaojiang908c7d72018-06-21 14:58:04 +0800147void AudioPolicyService::removeNotificationClient(uid_t uid, pid_t pid)
Eric Laurentb52c1522014-05-20 11:27:36 -0700148{
Eric Laurent0ebd5f92014-11-19 19:04:52 -0800149 {
150 Mutex::Autolock _l(mNotificationClientsLock);
luochaojiang908c7d72018-06-21 14:58:04 +0800151 int64_t token = ((int64_t)uid<<32) | pid;
152 mNotificationClients.removeItem(token);
Eric Laurent0ebd5f92014-11-19 19:04:52 -0800153 }
Eric Laurent0ebd5f92014-11-19 19:04:52 -0800154 {
155 Mutex::Autolock _l(mLock);
luochaojiang908c7d72018-06-21 14:58:04 +0800156 bool hasSameUid = false;
157 for (size_t i = 0; i < mNotificationClients.size(); i++) {
158 if (mNotificationClients.valueAt(i)->uid() == uid) {
159 hasSameUid = true;
160 break;
161 }
162 }
163 if (mAudioPolicyManager && !hasSameUid) {
Eric Laurent10b71232018-04-13 18:14:44 -0700164 // called from binder death notification: no need to clear caller identity
Eric Laurent8c7e6da2015-04-21 17:37:00 -0700165 mAudioPolicyManager->releaseResourcesForUid(uid);
Eric Laurentb52c1522014-05-20 11:27:36 -0700166 }
Eric Laurent0ebd5f92014-11-19 19:04:52 -0800167 }
Eric Laurentb52c1522014-05-20 11:27:36 -0700168}
169
170void AudioPolicyService::onAudioPortListUpdate()
171{
172 mOutputCommandThread->updateAudioPortListCommand();
173}
174
175void AudioPolicyService::doOnAudioPortListUpdate()
176{
Eric Laurent0ebd5f92014-11-19 19:04:52 -0800177 Mutex::Autolock _l(mNotificationClientsLock);
Eric Laurentb52c1522014-05-20 11:27:36 -0700178 for (size_t i = 0; i < mNotificationClients.size(); i++) {
179 mNotificationClients.valueAt(i)->onAudioPortListUpdate();
180 }
181}
182
183void AudioPolicyService::onAudioPatchListUpdate()
184{
185 mOutputCommandThread->updateAudioPatchListCommand();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700186}
187
Eric Laurentb52c1522014-05-20 11:27:36 -0700188void AudioPolicyService::doOnAudioPatchListUpdate()
189{
Eric Laurent0ebd5f92014-11-19 19:04:52 -0800190 Mutex::Autolock _l(mNotificationClientsLock);
Eric Laurentb52c1522014-05-20 11:27:36 -0700191 for (size_t i = 0; i < mNotificationClients.size(); i++) {
192 mNotificationClients.valueAt(i)->onAudioPatchListUpdate();
193 }
194}
195
Chih-Hung Hsiehe964d4e2016-08-09 14:31:32 -0700196void AudioPolicyService::onDynamicPolicyMixStateUpdate(const String8& regId, int32_t state)
Jean-Michel Trivide801052015-04-14 19:10:14 -0700197{
198 ALOGV("AudioPolicyService::onDynamicPolicyMixStateUpdate(%s, %d)",
199 regId.string(), state);
200 mOutputCommandThread->dynamicPolicyMixStateUpdateCommand(regId, state);
201}
202
Chih-Hung Hsiehe964d4e2016-08-09 14:31:32 -0700203void AudioPolicyService::doOnDynamicPolicyMixStateUpdate(const String8& regId, int32_t state)
Jean-Michel Trivide801052015-04-14 19:10:14 -0700204{
205 Mutex::Autolock _l(mNotificationClientsLock);
206 for (size_t i = 0; i < mNotificationClients.size(); i++) {
207 mNotificationClients.valueAt(i)->onDynamicPolicyMixStateUpdate(regId, state);
208 }
209}
210
Jean-Michel Triviac4e4292016-12-22 11:39:31 -0800211void AudioPolicyService::onRecordingConfigurationUpdate(int event,
212 const record_client_info_t *clientInfo, const audio_config_base_t *clientConfig,
Jean-Michel Trivi8c7cf3b2016-02-25 17:08:24 -0800213 const audio_config_base_t *deviceConfig, audio_patch_handle_t patchHandle)
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -0800214{
Jean-Michel Triviac4e4292016-12-22 11:39:31 -0800215 mOutputCommandThread->recordingConfigurationUpdateCommand(event, clientInfo,
Jean-Michel Trivi8c7cf3b2016-02-25 17:08:24 -0800216 clientConfig, deviceConfig, patchHandle);
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -0800217}
218
Jean-Michel Triviac4e4292016-12-22 11:39:31 -0800219void AudioPolicyService::doOnRecordingConfigurationUpdate(int event,
220 const record_client_info_t *clientInfo, const audio_config_base_t *clientConfig,
Jean-Michel Trivi8c7cf3b2016-02-25 17:08:24 -0800221 const audio_config_base_t *deviceConfig, audio_patch_handle_t patchHandle)
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -0800222{
223 Mutex::Autolock _l(mNotificationClientsLock);
224 for (size_t i = 0; i < mNotificationClients.size(); i++) {
Jean-Michel Triviac4e4292016-12-22 11:39:31 -0800225 mNotificationClients.valueAt(i)->onRecordingConfigurationUpdate(event, clientInfo,
Jean-Michel Trivi8c7cf3b2016-02-25 17:08:24 -0800226 clientConfig, deviceConfig, patchHandle);
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -0800227 }
228}
229
230status_t AudioPolicyService::clientCreateAudioPatch(const struct audio_patch *patch,
231 audio_patch_handle_t *handle,
232 int delayMs)
233{
234 return mAudioCommandThread->createAudioPatchCommand(patch, handle, delayMs);
235}
236
237status_t AudioPolicyService::clientReleaseAudioPatch(audio_patch_handle_t handle,
238 int delayMs)
239{
240 return mAudioCommandThread->releaseAudioPatchCommand(handle, delayMs);
241}
242
Eric Laurente1715a42014-05-20 11:30:42 -0700243status_t AudioPolicyService::clientSetAudioPortConfig(const struct audio_port_config *config,
244 int delayMs)
245{
246 return mAudioCommandThread->setAudioPortConfigCommand(config, delayMs);
247}
248
Eric Laurentb52c1522014-05-20 11:27:36 -0700249AudioPolicyService::NotificationClient::NotificationClient(const sp<AudioPolicyService>& service,
250 const sp<IAudioPolicyServiceClient>& client,
luochaojiang908c7d72018-06-21 14:58:04 +0800251 uid_t uid,
252 pid_t pid)
253 : mService(service), mUid(uid), mPid(pid), mAudioPolicyServiceClient(client),
Eric Laurente8726fe2015-06-26 09:39:24 -0700254 mAudioPortCallbacksEnabled(false)
Eric Laurentb52c1522014-05-20 11:27:36 -0700255{
256}
257
258AudioPolicyService::NotificationClient::~NotificationClient()
259{
260}
261
262void AudioPolicyService::NotificationClient::binderDied(const wp<IBinder>& who __unused)
263{
264 sp<NotificationClient> keep(this);
265 sp<AudioPolicyService> service = mService.promote();
266 if (service != 0) {
luochaojiang908c7d72018-06-21 14:58:04 +0800267 service->removeNotificationClient(mUid, mPid);
Eric Laurentb52c1522014-05-20 11:27:36 -0700268 }
269}
270
271void AudioPolicyService::NotificationClient::onAudioPortListUpdate()
272{
Eric Laurente8726fe2015-06-26 09:39:24 -0700273 if (mAudioPolicyServiceClient != 0 && mAudioPortCallbacksEnabled) {
Eric Laurentb52c1522014-05-20 11:27:36 -0700274 mAudioPolicyServiceClient->onAudioPortListUpdate();
275 }
276}
277
278void AudioPolicyService::NotificationClient::onAudioPatchListUpdate()
279{
Eric Laurente8726fe2015-06-26 09:39:24 -0700280 if (mAudioPolicyServiceClient != 0 && mAudioPortCallbacksEnabled) {
Eric Laurentb52c1522014-05-20 11:27:36 -0700281 mAudioPolicyServiceClient->onAudioPatchListUpdate();
282 }
283}
Eric Laurent57dae992011-07-24 13:36:09 -0700284
Jean-Michel Trivide801052015-04-14 19:10:14 -0700285void AudioPolicyService::NotificationClient::onDynamicPolicyMixStateUpdate(
Chih-Hung Hsiehe964d4e2016-08-09 14:31:32 -0700286 const String8& regId, int32_t state)
Jean-Michel Trivide801052015-04-14 19:10:14 -0700287{
Andy Hung4ef19fa2018-05-15 19:35:29 -0700288 if (mAudioPolicyServiceClient != 0 && isServiceUid(mUid)) {
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -0800289 mAudioPolicyServiceClient->onDynamicPolicyMixStateUpdate(regId, state);
290 }
291}
292
293void AudioPolicyService::NotificationClient::onRecordingConfigurationUpdate(
Jean-Michel Triviac4e4292016-12-22 11:39:31 -0800294 int event, const record_client_info_t *clientInfo,
Jean-Michel Trivi8c7cf3b2016-02-25 17:08:24 -0800295 const audio_config_base_t *clientConfig, const audio_config_base_t *deviceConfig,
296 audio_patch_handle_t patchHandle)
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -0800297{
Andy Hung4ef19fa2018-05-15 19:35:29 -0700298 if (mAudioPolicyServiceClient != 0 && isServiceUid(mUid)) {
Jean-Michel Triviac4e4292016-12-22 11:39:31 -0800299 mAudioPolicyServiceClient->onRecordingConfigurationUpdate(event, clientInfo,
Jean-Michel Trivi8c7cf3b2016-02-25 17:08:24 -0800300 clientConfig, deviceConfig, patchHandle);
Jean-Michel Trivide801052015-04-14 19:10:14 -0700301 }
302}
303
Eric Laurente8726fe2015-06-26 09:39:24 -0700304void AudioPolicyService::NotificationClient::setAudioPortCallbacksEnabled(bool enabled)
305{
306 mAudioPortCallbacksEnabled = enabled;
307}
308
309
Mathias Agopian65ab4712010-07-14 17:59:35 -0700310void AudioPolicyService::binderDied(const wp<IBinder>& who) {
Glenn Kasten411e4472012-11-02 10:00:06 -0700311 ALOGW("binderDied() %p, calling pid %d", who.unsafe_get(),
Eric Laurentde070132010-07-13 04:45:46 -0700312 IPCThreadState::self()->getCallingPid());
Mathias Agopian65ab4712010-07-14 17:59:35 -0700313}
314
315static bool tryLock(Mutex& mutex)
316{
317 bool locked = false;
318 for (int i = 0; i < kDumpLockRetries; ++i) {
319 if (mutex.tryLock() == NO_ERROR) {
320 locked = true;
321 break;
322 }
Glenn Kasten22ecc912012-01-09 08:33:38 -0800323 usleep(kDumpLockSleepUs);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700324 }
325 return locked;
326}
327
328status_t AudioPolicyService::dumpInternals(int fd)
329{
330 const size_t SIZE = 256;
331 char buffer[SIZE];
332 String8 result;
333
Eric Laurentdce54a12014-03-10 12:19:46 -0700334 snprintf(buffer, SIZE, "AudioPolicyManager: %p\n", mAudioPolicyManager);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700335 result.append(buffer);
336 snprintf(buffer, SIZE, "Command Thread: %p\n", mAudioCommandThread.get());
337 result.append(buffer);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700338
339 write(fd, result.string(), result.size());
340 return NO_ERROR;
341}
342
Eric Laurentf32108e2018-10-04 17:22:04 -0700343void AudioPolicyService::setAppState(uid_t uid, app_state_t state)
Svet Ganovf4ddfef2018-01-16 07:37:58 -0800344{
Mikhail Naganoveae73eb2018-04-03 16:57:36 -0700345 {
346 Mutex::Autolock _l(mLock);
347 if (mAudioPolicyManager) {
Eric Laurent10b71232018-04-13 18:14:44 -0700348 AutoCallerClear acc;
Eric Laurentf32108e2018-10-04 17:22:04 -0700349 mAudioPolicyManager->setAppState(uid, state);
Mikhail Naganoveae73eb2018-04-03 16:57:36 -0700350 }
351 }
352 sp<IAudioFlinger> af = AudioSystem::get_audio_flinger();
353 if (af) {
Eric Laurentf32108e2018-10-04 17:22:04 -0700354 bool silenced = state == APP_STATE_IDLE;
Mikhail Naganoveae73eb2018-04-03 16:57:36 -0700355 af->setRecordSilenced(uid, silenced);
356 }
Svet Ganovf4ddfef2018-01-16 07:37:58 -0800357}
358
Glenn Kasten0f11b512014-01-31 16:18:54 -0800359status_t AudioPolicyService::dump(int fd, const Vector<String16>& args __unused)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700360{
Glenn Kasten44deb052012-02-05 18:09:08 -0800361 if (!dumpAllowed()) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700362 dumpPermissionDenial(fd);
363 } else {
364 bool locked = tryLock(mLock);
365 if (!locked) {
366 String8 result(kDeadlockedString);
367 write(fd, result.string(), result.size());
368 }
369
370 dumpInternals(fd);
Glenn Kasten9d1f02d2012-02-08 17:47:58 -0800371 if (mAudioCommandThread != 0) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700372 mAudioCommandThread->dump(fd);
373 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700374
Eric Laurentdce54a12014-03-10 12:19:46 -0700375 if (mAudioPolicyManager) {
376 mAudioPolicyManager->dump(fd);
377 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700378
379 if (locked) mLock.unlock();
380 }
381 return NO_ERROR;
382}
383
384status_t AudioPolicyService::dumpPermissionDenial(int fd)
385{
386 const size_t SIZE = 256;
387 char buffer[SIZE];
388 String8 result;
389 snprintf(buffer, SIZE, "Permission Denial: "
390 "can't dump AudioPolicyService from pid=%d, uid=%d\n",
391 IPCThreadState::self()->getCallingPid(),
392 IPCThreadState::self()->getCallingUid());
393 result.append(buffer);
394 write(fd, result.string(), result.size());
395 return NO_ERROR;
396}
397
398status_t AudioPolicyService::onTransact(
Svet Ganovf4ddfef2018-01-16 07:37:58 -0800399 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags) {
400 switch (code) {
401 case SHELL_COMMAND_TRANSACTION: {
402 int in = data.readFileDescriptor();
403 int out = data.readFileDescriptor();
404 int err = data.readFileDescriptor();
405 int argc = data.readInt32();
406 Vector<String16> args;
407 for (int i = 0; i < argc && data.dataAvail() > 0; i++) {
408 args.add(data.readString16());
409 }
410 sp<IBinder> unusedCallback;
411 sp<IResultReceiver> resultReceiver;
412 status_t status;
413 if ((status = data.readNullableStrongBinder(&unusedCallback)) != NO_ERROR) {
414 return status;
415 }
416 if ((status = data.readNullableStrongBinder(&resultReceiver)) != NO_ERROR) {
417 return status;
418 }
419 status = shellCommand(in, out, err, args);
420 if (resultReceiver != nullptr) {
421 resultReceiver->send(status);
422 }
423 return NO_ERROR;
424 }
425 }
426
Mathias Agopian65ab4712010-07-14 17:59:35 -0700427 return BnAudioPolicyService::onTransact(code, data, reply, flags);
428}
429
Svet Ganovf4ddfef2018-01-16 07:37:58 -0800430// ------------------- Shell command implementation -------------------
431
432// NOTE: This is a remote API - make sure all args are validated
433status_t AudioPolicyService::shellCommand(int in, int out, int err, Vector<String16>& args) {
434 if (!checkCallingPermission(sManageAudioPolicyPermission, nullptr, nullptr)) {
435 return PERMISSION_DENIED;
436 }
437 if (in == BAD_TYPE || out == BAD_TYPE || err == BAD_TYPE) {
438 return BAD_VALUE;
439 }
440 if (args.size() == 3 && args[0] == String16("set-uid-state")) {
441 return handleSetUidState(args, err);
442 } else if (args.size() == 2 && args[0] == String16("reset-uid-state")) {
443 return handleResetUidState(args, err);
444 } else if (args.size() == 2 && args[0] == String16("get-uid-state")) {
445 return handleGetUidState(args, out, err);
446 } else if (args.size() == 1 && args[0] == String16("help")) {
447 printHelp(out);
448 return NO_ERROR;
449 }
450 printHelp(err);
451 return BAD_VALUE;
452}
453
454status_t AudioPolicyService::handleSetUidState(Vector<String16>& args, int err) {
455 PermissionController pc;
456 int uid = pc.getPackageUid(args[1], 0);
457 if (uid <= 0) {
458 ALOGE("Unknown package: '%s'", String8(args[1]).string());
459 dprintf(err, "Unknown package: '%s'\n", String8(args[1]).string());
460 return BAD_VALUE;
461 }
462 bool active = false;
463 if (args[2] == String16("active")) {
464 active = true;
465 } else if ((args[2] != String16("idle"))) {
466 ALOGE("Expected active or idle but got: '%s'", String8(args[2]).string());
467 return BAD_VALUE;
468 }
469 mUidPolicy->addOverrideUid(uid, active);
470 return NO_ERROR;
471}
472
473status_t AudioPolicyService::handleResetUidState(Vector<String16>& args, int err) {
474 PermissionController pc;
475 int uid = pc.getPackageUid(args[1], 0);
476 if (uid < 0) {
477 ALOGE("Unknown package: '%s'", String8(args[1]).string());
478 dprintf(err, "Unknown package: '%s'\n", String8(args[1]).string());
479 return BAD_VALUE;
480 }
481 mUidPolicy->removeOverrideUid(uid);
482 return NO_ERROR;
483}
484
485status_t AudioPolicyService::handleGetUidState(Vector<String16>& args, int out, int err) {
486 PermissionController pc;
487 int uid = pc.getPackageUid(args[1], 0);
488 if (uid < 0) {
489 ALOGE("Unknown package: '%s'", String8(args[1]).string());
490 dprintf(err, "Unknown package: '%s'\n", String8(args[1]).string());
491 return BAD_VALUE;
492 }
493 if (mUidPolicy->isUidActive(uid)) {
494 return dprintf(out, "active\n");
495 } else {
496 return dprintf(out, "idle\n");
497 }
498}
499
500status_t AudioPolicyService::printHelp(int out) {
501 return dprintf(out, "Audio policy service commands:\n"
502 " get-uid-state <PACKAGE> gets the uid state\n"
503 " set-uid-state <PACKAGE> <active|idle> overrides the uid state\n"
504 " reset-uid-state <PACKAGE> clears the uid state override\n"
505 " help print this message\n");
506}
507
508// ----------- AudioPolicyService::UidPolicy implementation ----------
509
510void AudioPolicyService::UidPolicy::registerSelf() {
511 ActivityManager am;
512 am.registerUidObserver(this, ActivityManager::UID_OBSERVER_GONE
513 | ActivityManager::UID_OBSERVER_IDLE
514 | ActivityManager::UID_OBSERVER_ACTIVE,
515 ActivityManager::PROCESS_STATE_UNKNOWN,
516 String16("audioserver"));
Mikhail Naganoveae73eb2018-04-03 16:57:36 -0700517 status_t res = am.linkToDeath(this);
518 if (!res) {
519 Mutex::Autolock _l(mLock);
520 mObserverRegistered = true;
521 } else {
522 ALOGE("UidPolicy::registerSelf linkToDeath failed: %d", res);
523 am.unregisterUidObserver(this);
524 }
Svet Ganovf4ddfef2018-01-16 07:37:58 -0800525}
526
527void AudioPolicyService::UidPolicy::unregisterSelf() {
528 ActivityManager am;
Mikhail Naganoveae73eb2018-04-03 16:57:36 -0700529 am.unlinkToDeath(this);
Svet Ganovf4ddfef2018-01-16 07:37:58 -0800530 am.unregisterUidObserver(this);
Mikhail Naganoveae73eb2018-04-03 16:57:36 -0700531 Mutex::Autolock _l(mLock);
532 mObserverRegistered = false;
Svet Ganovf4ddfef2018-01-16 07:37:58 -0800533}
534
Mikhail Naganoveae73eb2018-04-03 16:57:36 -0700535void AudioPolicyService::UidPolicy::binderDied(__unused const wp<IBinder> &who) {
536 Mutex::Autolock _l(mLock);
537 mCachedUids.clear();
538 mObserverRegistered = false;
Svet Ganovf4ddfef2018-01-16 07:37:58 -0800539}
540
541bool AudioPolicyService::UidPolicy::isUidActive(uid_t uid) {
Mikhail Naganoveae73eb2018-04-03 16:57:36 -0700542 if (isServiceUid(uid)) return true;
543 bool needToReregister = false;
544 {
545 Mutex::Autolock _l(mLock);
546 needToReregister = !mObserverRegistered;
Svet Ganovf4ddfef2018-01-16 07:37:58 -0800547 }
Mikhail Naganoveae73eb2018-04-03 16:57:36 -0700548 if (needToReregister) {
549 // Looks like ActivityManager has died previously, attempt to re-register.
550 registerSelf();
551 }
552 {
553 Mutex::Autolock _l(mLock);
554 auto overrideIter = mOverrideUids.find(uid);
555 if (overrideIter != mOverrideUids.end()) {
556 return overrideIter->second;
557 }
558 // In an absense of the ActivityManager, assume everything to be active.
559 if (!mObserverRegistered) return true;
560 auto cacheIter = mCachedUids.find(uid);
Mikhail Naganoveba668a2018-04-05 08:13:15 -0700561 if (cacheIter != mCachedUids.end()) {
Mikhail Naganoveae73eb2018-04-03 16:57:36 -0700562 return cacheIter->second;
563 }
564 }
565 ActivityManager am;
566 bool active = am.isUidActive(uid, String16("audioserver"));
567 {
568 Mutex::Autolock _l(mLock);
569 mCachedUids.insert(std::pair<uid_t, bool>(uid, active));
570 }
571 return active;
Svet Ganovf4ddfef2018-01-16 07:37:58 -0800572}
573
Mikhail Naganoveae73eb2018-04-03 16:57:36 -0700574void AudioPolicyService::UidPolicy::onUidActive(uid_t uid) {
575 updateUidCache(uid, true, true);
576}
577
578void AudioPolicyService::UidPolicy::onUidGone(uid_t uid, __unused bool disabled) {
579 updateUidCache(uid, false, false);
580}
581
582void AudioPolicyService::UidPolicy::onUidIdle(uid_t uid, __unused bool disabled) {
583 updateUidCache(uid, false, true);
584}
585
Mikhail Naganoveae73eb2018-04-03 16:57:36 -0700586void AudioPolicyService::UidPolicy::notifyService(uid_t uid, bool active) {
587 sp<AudioPolicyService> service = mService.promote();
588 if (service != nullptr) {
Eric Laurentf32108e2018-10-04 17:22:04 -0700589 service->setAppState(uid, active ?
590 APP_STATE_FOREGROUND :
591 APP_STATE_IDLE);
Mikhail Naganoveae73eb2018-04-03 16:57:36 -0700592 }
593}
594
595void AudioPolicyService::UidPolicy::updateOverrideUid(uid_t uid, bool active, bool insert) {
596 if (isServiceUid(uid)) return;
597 bool wasOverridden = false, wasActive = false;
598 {
599 Mutex::Autolock _l(mLock);
600 updateUidLocked(&mOverrideUids, uid, active, insert, &wasOverridden, &wasActive);
601 }
602 if (!wasOverridden && insert) {
603 notifyService(uid, active); // Started to override.
604 } else if (wasOverridden && !insert) {
605 notifyService(uid, isUidActive(uid)); // Override ceased, notify with ground truth.
606 } else if (wasActive != active) {
607 notifyService(uid, active); // Override updated.
608 }
609}
610
611void AudioPolicyService::UidPolicy::updateUidCache(uid_t uid, bool active, bool insert) {
612 if (isServiceUid(uid)) return;
613 bool wasActive = false;
614 {
615 Mutex::Autolock _l(mLock);
616 updateUidLocked(&mCachedUids, uid, active, insert, nullptr, &wasActive);
617 // Do not notify service if currently overridden.
618 if (mOverrideUids.find(uid) != mOverrideUids.end()) return;
619 }
620 bool nowActive = active && insert;
621 if (wasActive != nowActive) notifyService(uid, nowActive);
622}
623
624void AudioPolicyService::UidPolicy::updateUidLocked(std::unordered_map<uid_t, bool> *uids,
625 uid_t uid, bool active, bool insert, bool *wasThere, bool *wasActive) {
626 auto it = uids->find(uid);
627 if (it != uids->end()) {
628 if (wasThere != nullptr) *wasThere = true;
629 if (wasActive != nullptr) *wasActive = it->second;
630 if (insert) {
631 it->second = active;
632 } else {
633 uids->erase(it);
634 }
635 } else if (insert) {
636 uids->insert(std::pair<uid_t, bool>(uid, active));
637 }
Svet Ganovf4ddfef2018-01-16 07:37:58 -0800638}
Mathias Agopian65ab4712010-07-14 17:59:35 -0700639
Eric Laurentb78763e2018-10-17 10:08:02 -0700640bool AudioPolicyService::UidPolicy::isA11yUid(uid_t uid)
641{
642 std::vector<uid_t>::iterator it = find(mA11yUids.begin(), mA11yUids.end(), uid);
643 return it != mA11yUids.end();
644}
645
Mathias Agopian65ab4712010-07-14 17:59:35 -0700646// ----------- AudioPolicyService::AudioCommandThread implementation ----------
647
Eric Laurentbfb1b832013-01-07 09:53:42 -0800648AudioPolicyService::AudioCommandThread::AudioCommandThread(String8 name,
649 const wp<AudioPolicyService>& service)
650 : Thread(false), mName(name), mService(service)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700651{
Mathias Agopian65ab4712010-07-14 17:59:35 -0700652}
653
654
655AudioPolicyService::AudioCommandThread::~AudioCommandThread()
656{
Eric Laurentbfb1b832013-01-07 09:53:42 -0800657 if (!mAudioCommands.isEmpty()) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700658 release_wake_lock(mName.string());
659 }
660 mAudioCommands.clear();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700661}
662
663void AudioPolicyService::AudioCommandThread::onFirstRef()
664{
Eric Laurentbfb1b832013-01-07 09:53:42 -0800665 run(mName.string(), ANDROID_PRIORITY_AUDIO);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700666}
667
668bool AudioPolicyService::AudioCommandThread::threadLoop()
669{
Eric Laurentd7eda8d2016-02-02 17:18:39 -0800670 nsecs_t waitTime = -1;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700671
672 mLock.lock();
673 while (!exitPending())
674 {
Eric Laurent59a89232014-06-08 14:14:17 -0700675 sp<AudioPolicyService> svc;
676 while (!mAudioCommands.isEmpty() && !exitPending()) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700677 nsecs_t curTime = systemTime();
678 // commands are sorted by increasing time stamp: execute them from index 0 and up
679 if (mAudioCommands[0]->mTime <= curTime) {
Eric Laurent0ede8922014-05-09 18:04:42 -0700680 sp<AudioCommand> command = mAudioCommands[0];
Mathias Agopian65ab4712010-07-14 17:59:35 -0700681 mAudioCommands.removeAt(0);
Eric Laurent0ede8922014-05-09 18:04:42 -0700682 mLastCommand = command;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700683
684 switch (command->mCommand) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700685 case SET_VOLUME: {
Eric Laurent0ede8922014-05-09 18:04:42 -0700686 VolumeData *data = (VolumeData *)command->mParam.get();
Steve Block3856b092011-10-20 11:56:00 +0100687 ALOGV("AudioCommandThread() processing set volume stream %d, \
Eric Laurentde070132010-07-13 04:45:46 -0700688 volume %f, output %d", data->mStream, data->mVolume, data->mIO);
Andy Hungfe726a62018-09-27 15:17:25 -0700689 mLock.unlock();
Eric Laurentde070132010-07-13 04:45:46 -0700690 command->mStatus = AudioSystem::setStreamVolume(data->mStream,
691 data->mVolume,
692 data->mIO);
Andy Hungfe726a62018-09-27 15:17:25 -0700693 mLock.lock();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700694 }break;
695 case SET_PARAMETERS: {
Eric Laurent0ede8922014-05-09 18:04:42 -0700696 ParametersData *data = (ParametersData *)command->mParam.get();
Glenn Kastene53b9ea2012-03-12 16:29:55 -0700697 ALOGV("AudioCommandThread() processing set parameters string %s, io %d",
698 data->mKeyValuePairs.string(), data->mIO);
Andy Hungfe726a62018-09-27 15:17:25 -0700699 mLock.unlock();
Glenn Kastene53b9ea2012-03-12 16:29:55 -0700700 command->mStatus = AudioSystem::setParameters(data->mIO, data->mKeyValuePairs);
Andy Hungfe726a62018-09-27 15:17:25 -0700701 mLock.lock();
Glenn Kastene53b9ea2012-03-12 16:29:55 -0700702 }break;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700703 case SET_VOICE_VOLUME: {
Eric Laurent0ede8922014-05-09 18:04:42 -0700704 VoiceVolumeData *data = (VoiceVolumeData *)command->mParam.get();
Steve Block3856b092011-10-20 11:56:00 +0100705 ALOGV("AudioCommandThread() processing set voice volume volume %f",
Eric Laurentde070132010-07-13 04:45:46 -0700706 data->mVolume);
Andy Hungfe726a62018-09-27 15:17:25 -0700707 mLock.unlock();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700708 command->mStatus = AudioSystem::setVoiceVolume(data->mVolume);
Andy Hungfe726a62018-09-27 15:17:25 -0700709 mLock.lock();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700710 }break;
Eric Laurentbfb1b832013-01-07 09:53:42 -0800711 case STOP_OUTPUT: {
Eric Laurent0ede8922014-05-09 18:04:42 -0700712 StopOutputData *data = (StopOutputData *)command->mParam.get();
Eric Laurentd7fe0862018-07-14 16:48:01 -0700713 ALOGV("AudioCommandThread() processing stop output portId %d",
714 data->mPortId);
Eric Laurent59a89232014-06-08 14:14:17 -0700715 svc = mService.promote();
Eric Laurentbfb1b832013-01-07 09:53:42 -0800716 if (svc == 0) {
717 break;
718 }
719 mLock.unlock();
Eric Laurentd7fe0862018-07-14 16:48:01 -0700720 svc->doStopOutput(data->mPortId);
Eric Laurentbfb1b832013-01-07 09:53:42 -0800721 mLock.lock();
Eric Laurentbfb1b832013-01-07 09:53:42 -0800722 }break;
723 case RELEASE_OUTPUT: {
Eric Laurent0ede8922014-05-09 18:04:42 -0700724 ReleaseOutputData *data = (ReleaseOutputData *)command->mParam.get();
Eric Laurentd7fe0862018-07-14 16:48:01 -0700725 ALOGV("AudioCommandThread() processing release output portId %d",
726 data->mPortId);
Eric Laurent59a89232014-06-08 14:14:17 -0700727 svc = mService.promote();
Eric Laurentbfb1b832013-01-07 09:53:42 -0800728 if (svc == 0) {
729 break;
730 }
731 mLock.unlock();
Eric Laurentd7fe0862018-07-14 16:48:01 -0700732 svc->doReleaseOutput(data->mPortId);
Eric Laurentbfb1b832013-01-07 09:53:42 -0800733 mLock.lock();
Eric Laurentbfb1b832013-01-07 09:53:42 -0800734 }break;
Eric Laurent951f4552014-05-20 10:48:17 -0700735 case CREATE_AUDIO_PATCH: {
736 CreateAudioPatchData *data = (CreateAudioPatchData *)command->mParam.get();
737 ALOGV("AudioCommandThread() processing create audio patch");
738 sp<IAudioFlinger> af = AudioSystem::get_audio_flinger();
739 if (af == 0) {
740 command->mStatus = PERMISSION_DENIED;
741 } else {
Andy Hungfe726a62018-09-27 15:17:25 -0700742 mLock.unlock();
Eric Laurent951f4552014-05-20 10:48:17 -0700743 command->mStatus = af->createAudioPatch(&data->mPatch, &data->mHandle);
Andy Hungfe726a62018-09-27 15:17:25 -0700744 mLock.lock();
Eric Laurent951f4552014-05-20 10:48:17 -0700745 }
746 } break;
747 case RELEASE_AUDIO_PATCH: {
748 ReleaseAudioPatchData *data = (ReleaseAudioPatchData *)command->mParam.get();
749 ALOGV("AudioCommandThread() processing release audio patch");
750 sp<IAudioFlinger> af = AudioSystem::get_audio_flinger();
751 if (af == 0) {
752 command->mStatus = PERMISSION_DENIED;
753 } else {
Andy Hungfe726a62018-09-27 15:17:25 -0700754 mLock.unlock();
Eric Laurent951f4552014-05-20 10:48:17 -0700755 command->mStatus = af->releaseAudioPatch(data->mHandle);
Andy Hungfe726a62018-09-27 15:17:25 -0700756 mLock.lock();
Eric Laurent951f4552014-05-20 10:48:17 -0700757 }
758 } break;
Eric Laurentb52c1522014-05-20 11:27:36 -0700759 case UPDATE_AUDIOPORT_LIST: {
760 ALOGV("AudioCommandThread() processing update audio port list");
Eric Laurent59a89232014-06-08 14:14:17 -0700761 svc = mService.promote();
Eric Laurentb52c1522014-05-20 11:27:36 -0700762 if (svc == 0) {
763 break;
764 }
765 mLock.unlock();
766 svc->doOnAudioPortListUpdate();
767 mLock.lock();
768 }break;
769 case UPDATE_AUDIOPATCH_LIST: {
770 ALOGV("AudioCommandThread() processing update audio patch list");
Eric Laurent59a89232014-06-08 14:14:17 -0700771 svc = mService.promote();
Eric Laurentb52c1522014-05-20 11:27:36 -0700772 if (svc == 0) {
773 break;
774 }
775 mLock.unlock();
776 svc->doOnAudioPatchListUpdate();
777 mLock.lock();
778 }break;
Eric Laurente1715a42014-05-20 11:30:42 -0700779 case SET_AUDIOPORT_CONFIG: {
780 SetAudioPortConfigData *data = (SetAudioPortConfigData *)command->mParam.get();
781 ALOGV("AudioCommandThread() processing set port config");
782 sp<IAudioFlinger> af = AudioSystem::get_audio_flinger();
783 if (af == 0) {
784 command->mStatus = PERMISSION_DENIED;
785 } else {
Andy Hungfe726a62018-09-27 15:17:25 -0700786 mLock.unlock();
Eric Laurente1715a42014-05-20 11:30:42 -0700787 command->mStatus = af->setAudioPortConfig(&data->mConfig);
Andy Hungfe726a62018-09-27 15:17:25 -0700788 mLock.lock();
Eric Laurente1715a42014-05-20 11:30:42 -0700789 }
790 } break;
Jean-Michel Trivide801052015-04-14 19:10:14 -0700791 case DYN_POLICY_MIX_STATE_UPDATE: {
792 DynPolicyMixStateUpdateData *data =
793 (DynPolicyMixStateUpdateData *)command->mParam.get();
Jean-Michel Trivide801052015-04-14 19:10:14 -0700794 ALOGV("AudioCommandThread() processing dyn policy mix state update %s %d",
795 data->mRegId.string(), data->mState);
796 svc = mService.promote();
797 if (svc == 0) {
798 break;
799 }
800 mLock.unlock();
801 svc->doOnDynamicPolicyMixStateUpdate(data->mRegId, data->mState);
802 mLock.lock();
803 } break;
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -0800804 case RECORDING_CONFIGURATION_UPDATE: {
805 RecordingConfigurationUpdateData *data =
806 (RecordingConfigurationUpdateData *)command->mParam.get();
807 ALOGV("AudioCommandThread() processing recording configuration update");
808 svc = mService.promote();
809 if (svc == 0) {
810 break;
811 }
812 mLock.unlock();
Jean-Michel Triviac4e4292016-12-22 11:39:31 -0800813 svc->doOnRecordingConfigurationUpdate(data->mEvent, &data->mClientInfo,
814 &data->mClientConfig, &data->mDeviceConfig,
Jean-Michel Trivi8c7cf3b2016-02-25 17:08:24 -0800815 data->mPatchHandle);
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -0800816 mLock.lock();
817 } break;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700818 default:
Steve Block5ff1dd52012-01-05 23:22:43 +0000819 ALOGW("AudioCommandThread() unknown command %d", command->mCommand);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700820 }
Eric Laurent0ede8922014-05-09 18:04:42 -0700821 {
822 Mutex::Autolock _l(command->mLock);
823 if (command->mWaitStatus) {
824 command->mWaitStatus = false;
825 command->mCond.signal();
826 }
827 }
Eric Laurentd7eda8d2016-02-02 17:18:39 -0800828 waitTime = -1;
Zach Janga754b4f2015-10-27 01:29:34 +0000829 // release mLock before releasing strong reference on the service as
830 // AudioPolicyService destructor calls AudioCommandThread::exit() which
831 // acquires mLock.
832 mLock.unlock();
833 svc.clear();
834 mLock.lock();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700835 } else {
836 waitTime = mAudioCommands[0]->mTime - curTime;
837 break;
838 }
839 }
Zach Janga754b4f2015-10-27 01:29:34 +0000840
841 // release delayed commands wake lock if the queue is empty
842 if (mAudioCommands.isEmpty()) {
Ricardo Garcia05f2fdc2014-07-24 15:48:24 -0700843 release_wake_lock(mName.string());
Zach Janga754b4f2015-10-27 01:29:34 +0000844 }
845
846 // At this stage we have either an empty command queue or the first command in the queue
847 // has a finite delay. So unless we are exiting it is safe to wait.
848 if (!exitPending()) {
Eric Laurent59a89232014-06-08 14:14:17 -0700849 ALOGV("AudioCommandThread() going to sleep");
Eric Laurentd7eda8d2016-02-02 17:18:39 -0800850 if (waitTime == -1) {
851 mWaitWorkCV.wait(mLock);
852 } else {
853 mWaitWorkCV.waitRelative(mLock, waitTime);
854 }
Eric Laurent59a89232014-06-08 14:14:17 -0700855 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700856 }
Ricardo Garcia05f2fdc2014-07-24 15:48:24 -0700857 // release delayed commands wake lock before quitting
858 if (!mAudioCommands.isEmpty()) {
859 release_wake_lock(mName.string());
860 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700861 mLock.unlock();
862 return false;
863}
864
865status_t AudioPolicyService::AudioCommandThread::dump(int fd)
866{
867 const size_t SIZE = 256;
868 char buffer[SIZE];
869 String8 result;
870
871 snprintf(buffer, SIZE, "AudioCommandThread %p Dump\n", this);
872 result.append(buffer);
873 write(fd, result.string(), result.size());
874
875 bool locked = tryLock(mLock);
876 if (!locked) {
877 String8 result2(kCmdDeadlockedString);
878 write(fd, result2.string(), result2.size());
879 }
880
881 snprintf(buffer, SIZE, "- Commands:\n");
882 result = String8(buffer);
883 result.append(" Command Time Wait pParam\n");
Glenn Kasten8d6a2442012-02-08 14:04:28 -0800884 for (size_t i = 0; i < mAudioCommands.size(); i++) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700885 mAudioCommands[i]->dump(buffer, SIZE);
886 result.append(buffer);
887 }
888 result.append(" Last Command\n");
Eric Laurent0ede8922014-05-09 18:04:42 -0700889 if (mLastCommand != 0) {
890 mLastCommand->dump(buffer, SIZE);
891 result.append(buffer);
892 } else {
893 result.append(" none\n");
894 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700895
896 write(fd, result.string(), result.size());
897
898 if (locked) mLock.unlock();
899
900 return NO_ERROR;
901}
902
Glenn Kastenfff6d712012-01-12 16:38:12 -0800903status_t AudioPolicyService::AudioCommandThread::volumeCommand(audio_stream_type_t stream,
Eric Laurentde070132010-07-13 04:45:46 -0700904 float volume,
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800905 audio_io_handle_t output,
Eric Laurentde070132010-07-13 04:45:46 -0700906 int delayMs)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700907{
Eric Laurent0ede8922014-05-09 18:04:42 -0700908 sp<AudioCommand> command = new AudioCommand();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700909 command->mCommand = SET_VOLUME;
Eric Laurent0ede8922014-05-09 18:04:42 -0700910 sp<VolumeData> data = new VolumeData();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700911 data->mStream = stream;
912 data->mVolume = volume;
913 data->mIO = output;
914 command->mParam = data;
Eric Laurent0ede8922014-05-09 18:04:42 -0700915 command->mWaitStatus = true;
Steve Block3856b092011-10-20 11:56:00 +0100916 ALOGV("AudioCommandThread() adding set volume stream %d, volume %f, output %d",
Eric Laurentde070132010-07-13 04:45:46 -0700917 stream, volume, output);
Eric Laurent0ede8922014-05-09 18:04:42 -0700918 return sendCommand(command, delayMs);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700919}
920
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800921status_t AudioPolicyService::AudioCommandThread::parametersCommand(audio_io_handle_t ioHandle,
Dima Zavinfce7a472011-04-19 22:30:36 -0700922 const char *keyValuePairs,
Eric Laurentde070132010-07-13 04:45:46 -0700923 int delayMs)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700924{
Eric Laurent0ede8922014-05-09 18:04:42 -0700925 sp<AudioCommand> command = new AudioCommand();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700926 command->mCommand = SET_PARAMETERS;
Eric Laurent0ede8922014-05-09 18:04:42 -0700927 sp<ParametersData> data = new ParametersData();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700928 data->mIO = ioHandle;
Dima Zavinfce7a472011-04-19 22:30:36 -0700929 data->mKeyValuePairs = String8(keyValuePairs);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700930 command->mParam = data;
Eric Laurent0ede8922014-05-09 18:04:42 -0700931 command->mWaitStatus = true;
Steve Block3856b092011-10-20 11:56:00 +0100932 ALOGV("AudioCommandThread() adding set parameter string %s, io %d ,delay %d",
Dima Zavinfce7a472011-04-19 22:30:36 -0700933 keyValuePairs, ioHandle, delayMs);
Eric Laurent0ede8922014-05-09 18:04:42 -0700934 return sendCommand(command, delayMs);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700935}
936
937status_t AudioPolicyService::AudioCommandThread::voiceVolumeCommand(float volume, int delayMs)
938{
Eric Laurent0ede8922014-05-09 18:04:42 -0700939 sp<AudioCommand> command = new AudioCommand();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700940 command->mCommand = SET_VOICE_VOLUME;
Eric Laurent0ede8922014-05-09 18:04:42 -0700941 sp<VoiceVolumeData> data = new VoiceVolumeData();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700942 data->mVolume = volume;
943 command->mParam = data;
Eric Laurent0ede8922014-05-09 18:04:42 -0700944 command->mWaitStatus = true;
Steve Block3856b092011-10-20 11:56:00 +0100945 ALOGV("AudioCommandThread() adding set voice volume volume %f", volume);
Eric Laurent0ede8922014-05-09 18:04:42 -0700946 return sendCommand(command, delayMs);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700947}
948
Eric Laurentd7fe0862018-07-14 16:48:01 -0700949void AudioPolicyService::AudioCommandThread::stopOutputCommand(audio_port_handle_t portId)
Eric Laurentbfb1b832013-01-07 09:53:42 -0800950{
Eric Laurent0ede8922014-05-09 18:04:42 -0700951 sp<AudioCommand> command = new AudioCommand();
Eric Laurentbfb1b832013-01-07 09:53:42 -0800952 command->mCommand = STOP_OUTPUT;
Eric Laurent0ede8922014-05-09 18:04:42 -0700953 sp<StopOutputData> data = new StopOutputData();
Eric Laurentd7fe0862018-07-14 16:48:01 -0700954 data->mPortId = portId;
Jesper Tragardh48412dc2014-03-24 14:12:43 +0100955 command->mParam = data;
Eric Laurentd7fe0862018-07-14 16:48:01 -0700956 ALOGV("AudioCommandThread() adding stop output portId %d", portId);
Eric Laurent0ede8922014-05-09 18:04:42 -0700957 sendCommand(command);
Eric Laurentbfb1b832013-01-07 09:53:42 -0800958}
959
Eric Laurentd7fe0862018-07-14 16:48:01 -0700960void AudioPolicyService::AudioCommandThread::releaseOutputCommand(audio_port_handle_t portId)
Eric Laurentbfb1b832013-01-07 09:53:42 -0800961{
Eric Laurent0ede8922014-05-09 18:04:42 -0700962 sp<AudioCommand> command = new AudioCommand();
Eric Laurentbfb1b832013-01-07 09:53:42 -0800963 command->mCommand = RELEASE_OUTPUT;
Eric Laurent0ede8922014-05-09 18:04:42 -0700964 sp<ReleaseOutputData> data = new ReleaseOutputData();
Eric Laurentd7fe0862018-07-14 16:48:01 -0700965 data->mPortId = portId;
Jesper Tragardh48412dc2014-03-24 14:12:43 +0100966 command->mParam = data;
Eric Laurentd7fe0862018-07-14 16:48:01 -0700967 ALOGV("AudioCommandThread() adding release output portId %d", portId);
Eric Laurent0ede8922014-05-09 18:04:42 -0700968 sendCommand(command);
969}
970
Eric Laurent951f4552014-05-20 10:48:17 -0700971status_t AudioPolicyService::AudioCommandThread::createAudioPatchCommand(
972 const struct audio_patch *patch,
973 audio_patch_handle_t *handle,
974 int delayMs)
975{
976 status_t status = NO_ERROR;
977
978 sp<AudioCommand> command = new AudioCommand();
979 command->mCommand = CREATE_AUDIO_PATCH;
980 CreateAudioPatchData *data = new CreateAudioPatchData();
981 data->mPatch = *patch;
982 data->mHandle = *handle;
983 command->mParam = data;
984 command->mWaitStatus = true;
985 ALOGV("AudioCommandThread() adding create patch delay %d", delayMs);
986 status = sendCommand(command, delayMs);
987 if (status == NO_ERROR) {
988 *handle = data->mHandle;
989 }
990 return status;
991}
992
993status_t AudioPolicyService::AudioCommandThread::releaseAudioPatchCommand(audio_patch_handle_t handle,
994 int delayMs)
995{
996 sp<AudioCommand> command = new AudioCommand();
997 command->mCommand = RELEASE_AUDIO_PATCH;
998 ReleaseAudioPatchData *data = new ReleaseAudioPatchData();
999 data->mHandle = handle;
1000 command->mParam = data;
1001 command->mWaitStatus = true;
1002 ALOGV("AudioCommandThread() adding release patch delay %d", delayMs);
1003 return sendCommand(command, delayMs);
1004}
1005
Eric Laurentb52c1522014-05-20 11:27:36 -07001006void AudioPolicyService::AudioCommandThread::updateAudioPortListCommand()
1007{
1008 sp<AudioCommand> command = new AudioCommand();
1009 command->mCommand = UPDATE_AUDIOPORT_LIST;
1010 ALOGV("AudioCommandThread() adding update audio port list");
1011 sendCommand(command);
1012}
1013
1014void AudioPolicyService::AudioCommandThread::updateAudioPatchListCommand()
1015{
1016 sp<AudioCommand>command = new AudioCommand();
1017 command->mCommand = UPDATE_AUDIOPATCH_LIST;
1018 ALOGV("AudioCommandThread() adding update audio patch list");
1019 sendCommand(command);
1020}
1021
Eric Laurente1715a42014-05-20 11:30:42 -07001022status_t AudioPolicyService::AudioCommandThread::setAudioPortConfigCommand(
1023 const struct audio_port_config *config, int delayMs)
1024{
1025 sp<AudioCommand> command = new AudioCommand();
1026 command->mCommand = SET_AUDIOPORT_CONFIG;
1027 SetAudioPortConfigData *data = new SetAudioPortConfigData();
1028 data->mConfig = *config;
1029 command->mParam = data;
1030 command->mWaitStatus = true;
1031 ALOGV("AudioCommandThread() adding set port config delay %d", delayMs);
1032 return sendCommand(command, delayMs);
1033}
1034
Jean-Michel Trivide801052015-04-14 19:10:14 -07001035void AudioPolicyService::AudioCommandThread::dynamicPolicyMixStateUpdateCommand(
Chih-Hung Hsiehe964d4e2016-08-09 14:31:32 -07001036 const String8& regId, int32_t state)
Jean-Michel Trivide801052015-04-14 19:10:14 -07001037{
1038 sp<AudioCommand> command = new AudioCommand();
1039 command->mCommand = DYN_POLICY_MIX_STATE_UPDATE;
1040 DynPolicyMixStateUpdateData *data = new DynPolicyMixStateUpdateData();
1041 data->mRegId = regId;
1042 data->mState = state;
1043 command->mParam = data;
1044 ALOGV("AudioCommandThread() sending dynamic policy mix (id=%s) state update to %d",
1045 regId.string(), state);
1046 sendCommand(command);
1047}
1048
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -08001049void AudioPolicyService::AudioCommandThread::recordingConfigurationUpdateCommand(
Jean-Michel Triviac4e4292016-12-22 11:39:31 -08001050 int event, const record_client_info_t *clientInfo,
Jean-Michel Trivi8c7cf3b2016-02-25 17:08:24 -08001051 const audio_config_base_t *clientConfig, const audio_config_base_t *deviceConfig,
1052 audio_patch_handle_t patchHandle)
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -08001053{
1054 sp<AudioCommand>command = new AudioCommand();
1055 command->mCommand = RECORDING_CONFIGURATION_UPDATE;
1056 RecordingConfigurationUpdateData *data = new RecordingConfigurationUpdateData();
1057 data->mEvent = event;
Jean-Michel Triviac4e4292016-12-22 11:39:31 -08001058 data->mClientInfo = *clientInfo;
Jean-Michel Trivi7281aa92016-02-17 15:33:40 -08001059 data->mClientConfig = *clientConfig;
1060 data->mDeviceConfig = *deviceConfig;
Jean-Michel Trivi8c7cf3b2016-02-25 17:08:24 -08001061 data->mPatchHandle = patchHandle;
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -08001062 command->mParam = data;
Jean-Michel Triviac4e4292016-12-22 11:39:31 -08001063 ALOGV("AudioCommandThread() adding recording configuration update event %d, source %d uid %u",
1064 event, clientInfo->source, clientInfo->uid);
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -08001065 sendCommand(command);
1066}
1067
Eric Laurent0ede8922014-05-09 18:04:42 -07001068status_t AudioPolicyService::AudioCommandThread::sendCommand(sp<AudioCommand>& command, int delayMs)
1069{
1070 {
1071 Mutex::Autolock _l(mLock);
1072 insertCommand_l(command, delayMs);
1073 mWaitWorkCV.signal();
1074 }
1075 Mutex::Autolock _l(command->mLock);
1076 while (command->mWaitStatus) {
1077 nsecs_t timeOutNs = kAudioCommandTimeoutNs + milliseconds(delayMs);
1078 if (command->mCond.waitRelative(command->mLock, timeOutNs) != NO_ERROR) {
1079 command->mStatus = TIMED_OUT;
1080 command->mWaitStatus = false;
1081 }
1082 }
1083 return command->mStatus;
Eric Laurentbfb1b832013-01-07 09:53:42 -08001084}
1085
Mathias Agopian65ab4712010-07-14 17:59:35 -07001086// insertCommand_l() must be called with mLock held
Eric Laurent0ede8922014-05-09 18:04:42 -07001087void AudioPolicyService::AudioCommandThread::insertCommand_l(sp<AudioCommand>& command, int delayMs)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001088{
Glenn Kasten8d6a2442012-02-08 14:04:28 -08001089 ssize_t i; // not size_t because i will count down to -1
Eric Laurent0ede8922014-05-09 18:04:42 -07001090 Vector < sp<AudioCommand> > removedCommands;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001091 command->mTime = systemTime() + milliseconds(delayMs);
1092
1093 // acquire wake lock to make sure delayed commands are processed
Eric Laurentbfb1b832013-01-07 09:53:42 -08001094 if (mAudioCommands.isEmpty()) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07001095 acquire_wake_lock(PARTIAL_WAKE_LOCK, mName.string());
1096 }
1097
1098 // check same pending commands with later time stamps and eliminate them
Ivan Lozano5ff158f2017-10-30 09:06:24 -07001099 for (i = (ssize_t)mAudioCommands.size()-1; i >= 0; i--) {
Eric Laurent0ede8922014-05-09 18:04:42 -07001100 sp<AudioCommand> command2 = mAudioCommands[i];
Mathias Agopian65ab4712010-07-14 17:59:35 -07001101 // commands are sorted by increasing time stamp: no need to scan the rest of mAudioCommands
1102 if (command2->mTime <= command->mTime) break;
Eric Laurente45b48a2014-09-04 16:40:57 -07001103
1104 // create audio patch or release audio patch commands are equivalent
1105 // with regard to filtering
1106 if ((command->mCommand == CREATE_AUDIO_PATCH) ||
1107 (command->mCommand == RELEASE_AUDIO_PATCH)) {
1108 if ((command2->mCommand != CREATE_AUDIO_PATCH) &&
1109 (command2->mCommand != RELEASE_AUDIO_PATCH)) {
1110 continue;
1111 }
1112 } else if (command2->mCommand != command->mCommand) continue;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001113
1114 switch (command->mCommand) {
1115 case SET_PARAMETERS: {
Eric Laurent0ede8922014-05-09 18:04:42 -07001116 ParametersData *data = (ParametersData *)command->mParam.get();
1117 ParametersData *data2 = (ParametersData *)command2->mParam.get();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001118 if (data->mIO != data2->mIO) break;
Steve Block3856b092011-10-20 11:56:00 +01001119 ALOGV("Comparing parameter command %s to new command %s",
Eric Laurentde070132010-07-13 04:45:46 -07001120 data2->mKeyValuePairs.string(), data->mKeyValuePairs.string());
Mathias Agopian65ab4712010-07-14 17:59:35 -07001121 AudioParameter param = AudioParameter(data->mKeyValuePairs);
1122 AudioParameter param2 = AudioParameter(data2->mKeyValuePairs);
1123 for (size_t j = 0; j < param.size(); j++) {
Glenn Kastene53b9ea2012-03-12 16:29:55 -07001124 String8 key;
1125 String8 value;
1126 param.getAt(j, key, value);
1127 for (size_t k = 0; k < param2.size(); k++) {
1128 String8 key2;
1129 String8 value2;
1130 param2.getAt(k, key2, value2);
1131 if (key2 == key) {
1132 param2.remove(key2);
1133 ALOGV("Filtering out parameter %s", key2.string());
1134 break;
1135 }
1136 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001137 }
1138 // if all keys have been filtered out, remove the command.
1139 // otherwise, update the key value pairs
1140 if (param2.size() == 0) {
1141 removedCommands.add(command2);
1142 } else {
1143 data2->mKeyValuePairs = param2.toString();
1144 }
Eric Laurent21e54562013-09-23 12:08:05 -07001145 command->mTime = command2->mTime;
1146 // force delayMs to non 0 so that code below does not request to wait for
1147 // command status as the command is now delayed
1148 delayMs = 1;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001149 } break;
1150
1151 case SET_VOLUME: {
Eric Laurent0ede8922014-05-09 18:04:42 -07001152 VolumeData *data = (VolumeData *)command->mParam.get();
1153 VolumeData *data2 = (VolumeData *)command2->mParam.get();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001154 if (data->mIO != data2->mIO) break;
1155 if (data->mStream != data2->mStream) break;
Steve Block3856b092011-10-20 11:56:00 +01001156 ALOGV("Filtering out volume command on output %d for stream %d",
Eric Laurentde070132010-07-13 04:45:46 -07001157 data->mIO, data->mStream);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001158 removedCommands.add(command2);
Eric Laurent21e54562013-09-23 12:08:05 -07001159 command->mTime = command2->mTime;
1160 // force delayMs to non 0 so that code below does not request to wait for
1161 // command status as the command is now delayed
1162 delayMs = 1;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001163 } break;
Eric Laurente45b48a2014-09-04 16:40:57 -07001164
Eric Laurentbaf35fe2016-07-27 15:36:53 -07001165 case SET_VOICE_VOLUME: {
1166 VoiceVolumeData *data = (VoiceVolumeData *)command->mParam.get();
1167 VoiceVolumeData *data2 = (VoiceVolumeData *)command2->mParam.get();
1168 ALOGV("Filtering out voice volume command value %f replaced by %f",
1169 data2->mVolume, data->mVolume);
1170 removedCommands.add(command2);
1171 command->mTime = command2->mTime;
1172 // force delayMs to non 0 so that code below does not request to wait for
1173 // command status as the command is now delayed
1174 delayMs = 1;
1175 } break;
1176
Eric Laurente45b48a2014-09-04 16:40:57 -07001177 case CREATE_AUDIO_PATCH:
1178 case RELEASE_AUDIO_PATCH: {
1179 audio_patch_handle_t handle;
Haynes Mathew Georgea2d4a6d2014-10-13 13:05:22 -07001180 struct audio_patch patch;
Eric Laurente45b48a2014-09-04 16:40:57 -07001181 if (command->mCommand == CREATE_AUDIO_PATCH) {
1182 handle = ((CreateAudioPatchData *)command->mParam.get())->mHandle;
Haynes Mathew Georgea2d4a6d2014-10-13 13:05:22 -07001183 patch = ((CreateAudioPatchData *)command->mParam.get())->mPatch;
Eric Laurente45b48a2014-09-04 16:40:57 -07001184 } else {
1185 handle = ((ReleaseAudioPatchData *)command->mParam.get())->mHandle;
Mikhail Naganov7be71d22018-05-23 16:51:46 -07001186 memset(&patch, 0, sizeof(patch));
Eric Laurente45b48a2014-09-04 16:40:57 -07001187 }
1188 audio_patch_handle_t handle2;
Haynes Mathew Georgea2d4a6d2014-10-13 13:05:22 -07001189 struct audio_patch patch2;
Eric Laurente45b48a2014-09-04 16:40:57 -07001190 if (command2->mCommand == CREATE_AUDIO_PATCH) {
1191 handle2 = ((CreateAudioPatchData *)command2->mParam.get())->mHandle;
Haynes Mathew Georgea2d4a6d2014-10-13 13:05:22 -07001192 patch2 = ((CreateAudioPatchData *)command2->mParam.get())->mPatch;
Eric Laurente45b48a2014-09-04 16:40:57 -07001193 } else {
1194 handle2 = ((ReleaseAudioPatchData *)command2->mParam.get())->mHandle;
Glenn Kastenf60b6b62015-07-06 10:53:26 -07001195 memset(&patch2, 0, sizeof(patch2));
Eric Laurente45b48a2014-09-04 16:40:57 -07001196 }
1197 if (handle != handle2) break;
Haynes Mathew Georgea2d4a6d2014-10-13 13:05:22 -07001198 /* Filter CREATE_AUDIO_PATCH commands only when they are issued for
1199 same output. */
1200 if( (command->mCommand == CREATE_AUDIO_PATCH) &&
1201 (command2->mCommand == CREATE_AUDIO_PATCH) ) {
1202 bool isOutputDiff = false;
1203 if (patch.num_sources == patch2.num_sources) {
1204 for (unsigned count = 0; count < patch.num_sources; count++) {
1205 if (patch.sources[count].id != patch2.sources[count].id) {
1206 isOutputDiff = true;
1207 break;
1208 }
1209 }
1210 if (isOutputDiff)
1211 break;
1212 }
1213 }
Eric Laurente45b48a2014-09-04 16:40:57 -07001214 ALOGV("Filtering out %s audio patch command for handle %d",
1215 (command->mCommand == CREATE_AUDIO_PATCH) ? "create" : "release", handle);
1216 removedCommands.add(command2);
1217 command->mTime = command2->mTime;
1218 // force delayMs to non 0 so that code below does not request to wait for
1219 // command status as the command is now delayed
1220 delayMs = 1;
1221 } break;
1222
Jean-Michel Trivide801052015-04-14 19:10:14 -07001223 case DYN_POLICY_MIX_STATE_UPDATE: {
1224
1225 } break;
1226
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -08001227 case RECORDING_CONFIGURATION_UPDATE: {
1228
1229 } break;
1230
Mathias Agopian65ab4712010-07-14 17:59:35 -07001231 default:
1232 break;
1233 }
1234 }
1235
1236 // remove filtered commands
1237 for (size_t j = 0; j < removedCommands.size(); j++) {
1238 // removed commands always have time stamps greater than current command
1239 for (size_t k = i + 1; k < mAudioCommands.size(); k++) {
Eric Laurent0ede8922014-05-09 18:04:42 -07001240 if (mAudioCommands[k].get() == removedCommands[j].get()) {
Steve Block3856b092011-10-20 11:56:00 +01001241 ALOGV("suppressing command: %d", mAudioCommands[k]->mCommand);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001242 mAudioCommands.removeAt(k);
1243 break;
1244 }
1245 }
1246 }
1247 removedCommands.clear();
1248
Eric Laurentaa79bef2015-01-15 14:33:51 -08001249 // Disable wait for status if delay is not 0.
1250 // Except for create audio patch command because the returned patch handle
1251 // is needed by audio policy manager
1252 if (delayMs != 0 && command->mCommand != CREATE_AUDIO_PATCH) {
Eric Laurentcec4abb2012-07-03 12:23:02 -07001253 command->mWaitStatus = false;
1254 }
Eric Laurentcec4abb2012-07-03 12:23:02 -07001255
Mathias Agopian65ab4712010-07-14 17:59:35 -07001256 // insert command at the right place according to its time stamp
Eric Laurent1e693b52014-07-09 15:03:28 -07001257 ALOGV("inserting command: %d at index %zd, num commands %zu",
1258 command->mCommand, i+1, mAudioCommands.size());
Mathias Agopian65ab4712010-07-14 17:59:35 -07001259 mAudioCommands.insertAt(command, i + 1);
1260}
1261
1262void AudioPolicyService::AudioCommandThread::exit()
1263{
Steve Block3856b092011-10-20 11:56:00 +01001264 ALOGV("AudioCommandThread::exit");
Mathias Agopian65ab4712010-07-14 17:59:35 -07001265 {
1266 AutoMutex _l(mLock);
1267 requestExit();
1268 mWaitWorkCV.signal();
1269 }
Zach Janga754b4f2015-10-27 01:29:34 +00001270 // Note that we can call it from the thread loop if all other references have been released
1271 // but it will safely return WOULD_BLOCK in this case
Mathias Agopian65ab4712010-07-14 17:59:35 -07001272 requestExitAndWait();
1273}
1274
1275void AudioPolicyService::AudioCommandThread::AudioCommand::dump(char* buffer, size_t size)
1276{
1277 snprintf(buffer, size, " %02d %06d.%03d %01u %p\n",
1278 mCommand,
1279 (int)ns2s(mTime),
1280 (int)ns2ms(mTime)%1000,
1281 mWaitStatus,
Eric Laurent0ede8922014-05-09 18:04:42 -07001282 mParam.get());
Mathias Agopian65ab4712010-07-14 17:59:35 -07001283}
1284
Dima Zavinfce7a472011-04-19 22:30:36 -07001285/******* helpers for the service_ops callbacks defined below *********/
1286void AudioPolicyService::setParameters(audio_io_handle_t ioHandle,
1287 const char *keyValuePairs,
1288 int delayMs)
1289{
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001290 mAudioCommandThread->parametersCommand(ioHandle, keyValuePairs,
Dima Zavinfce7a472011-04-19 22:30:36 -07001291 delayMs);
1292}
1293
1294int AudioPolicyService::setStreamVolume(audio_stream_type_t stream,
1295 float volume,
1296 audio_io_handle_t output,
1297 int delayMs)
1298{
Glenn Kastenfff6d712012-01-12 16:38:12 -08001299 return (int)mAudioCommandThread->volumeCommand(stream, volume,
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001300 output, delayMs);
Dima Zavinfce7a472011-04-19 22:30:36 -07001301}
1302
Dima Zavinfce7a472011-04-19 22:30:36 -07001303int AudioPolicyService::setVoiceVolume(float volume, int delayMs)
1304{
1305 return (int)mAudioCommandThread->voiceVolumeCommand(volume, delayMs);
1306}
1307
Dima Zavinfce7a472011-04-19 22:30:36 -07001308extern "C" {
Eric Laurent2d388ec2014-03-07 13:25:54 -08001309audio_module_handle_t aps_load_hw_module(void *service __unused,
1310 const char *name);
1311audio_io_handle_t aps_open_output(void *service __unused,
Eric Laurenta4c5a552012-03-29 10:12:40 -07001312 audio_devices_t *pDevices,
1313 uint32_t *pSamplingRate,
1314 audio_format_t *pFormat,
1315 audio_channel_mask_t *pChannelMask,
1316 uint32_t *pLatencyMs,
Eric Laurent2d388ec2014-03-07 13:25:54 -08001317 audio_output_flags_t flags);
Eric Laurenta4c5a552012-03-29 10:12:40 -07001318
Eric Laurent2d388ec2014-03-07 13:25:54 -08001319audio_io_handle_t aps_open_output_on_module(void *service __unused,
Eric Laurenta4c5a552012-03-29 10:12:40 -07001320 audio_module_handle_t module,
1321 audio_devices_t *pDevices,
1322 uint32_t *pSamplingRate,
1323 audio_format_t *pFormat,
1324 audio_channel_mask_t *pChannelMask,
1325 uint32_t *pLatencyMs,
Richard Fitzgeraldad3af332013-03-25 16:54:37 +00001326 audio_output_flags_t flags,
Eric Laurent2d388ec2014-03-07 13:25:54 -08001327 const audio_offload_info_t *offloadInfo);
1328audio_io_handle_t aps_open_dup_output(void *service __unused,
Dima Zavinfce7a472011-04-19 22:30:36 -07001329 audio_io_handle_t output1,
Eric Laurent2d388ec2014-03-07 13:25:54 -08001330 audio_io_handle_t output2);
1331int aps_close_output(void *service __unused, audio_io_handle_t output);
1332int aps_suspend_output(void *service __unused, audio_io_handle_t output);
1333int aps_restore_output(void *service __unused, audio_io_handle_t output);
1334audio_io_handle_t aps_open_input(void *service __unused,
Eric Laurenta4c5a552012-03-29 10:12:40 -07001335 audio_devices_t *pDevices,
1336 uint32_t *pSamplingRate,
1337 audio_format_t *pFormat,
1338 audio_channel_mask_t *pChannelMask,
Eric Laurent2d388ec2014-03-07 13:25:54 -08001339 audio_in_acoustics_t acoustics __unused);
1340audio_io_handle_t aps_open_input_on_module(void *service __unused,
Eric Laurenta4c5a552012-03-29 10:12:40 -07001341 audio_module_handle_t module,
1342 audio_devices_t *pDevices,
1343 uint32_t *pSamplingRate,
1344 audio_format_t *pFormat,
Eric Laurent2d388ec2014-03-07 13:25:54 -08001345 audio_channel_mask_t *pChannelMask);
1346int aps_close_input(void *service __unused, audio_io_handle_t input);
1347int aps_invalidate_stream(void *service __unused, audio_stream_type_t stream);
Glenn Kastend848eb42016-03-08 13:42:11 -08001348int aps_move_effects(void *service __unused, audio_session_t session,
Dima Zavinfce7a472011-04-19 22:30:36 -07001349 audio_io_handle_t src_output,
Eric Laurent2d388ec2014-03-07 13:25:54 -08001350 audio_io_handle_t dst_output);
1351char * aps_get_parameters(void *service __unused, audio_io_handle_t io_handle,
1352 const char *keys);
1353void aps_set_parameters(void *service, audio_io_handle_t io_handle,
1354 const char *kv_pairs, int delay_ms);
1355int aps_set_stream_volume(void *service, audio_stream_type_t stream,
Dima Zavinfce7a472011-04-19 22:30:36 -07001356 float volume, audio_io_handle_t output,
Eric Laurent2d388ec2014-03-07 13:25:54 -08001357 int delay_ms);
Eric Laurent2d388ec2014-03-07 13:25:54 -08001358int aps_set_voice_volume(void *service, float volume, int delay_ms);
1359};
Dima Zavinfce7a472011-04-19 22:30:36 -07001360
Mikhail Naganov1b2a7942017-12-08 10:18:09 -08001361} // namespace android