blob: 4292651f1b56489337a10a613d4dd00b33d3952c [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 tone playback thread
71 mTonePlaybackThread = new AudioCommandThread(String8("ApmTone"), this);
72 // start audio commands thread
73 mAudioCommandThread = new AudioCommandThread(String8("ApmAudio"), this);
74 // start output activity command thread
75 mOutputCommandThread = new AudioCommandThread(String8("ApmOutput"), this);
Eric Laurentdce54a12014-03-10 12:19:46 -070076
Eric Laurent8b1e80b2014-10-07 09:08:47 -070077 mAudioPolicyClient = new AudioPolicyClient(this);
78 mAudioPolicyManager = createAudioPolicyManager(mAudioPolicyClient);
Eric Laurent8b1e80b2014-10-07 09:08:47 -070079 }
bryant_liuba2b4392014-06-11 16:49:30 +080080 // load audio processing modules
Eric Laurent8b1e80b2014-10-07 09:08:47 -070081 sp<AudioPolicyEffects>audioPolicyEffects = new AudioPolicyEffects();
82 {
83 Mutex::Autolock _l(mLock);
84 mAudioPolicyEffects = audioPolicyEffects;
85 }
Svet Ganovf4ddfef2018-01-16 07:37:58 -080086
87 mUidPolicy = new UidPolicy(this);
88 mUidPolicy->registerSelf();
Mathias Agopian65ab4712010-07-14 17:59:35 -070089}
90
91AudioPolicyService::~AudioPolicyService()
92{
93 mTonePlaybackThread->exit();
Mathias Agopian65ab4712010-07-14 17:59:35 -070094 mAudioCommandThread->exit();
Eric Laurent657ff612014-05-07 11:58:24 -070095 mOutputCommandThread->exit();
Eric Laurent7c7f10b2011-06-17 21:29:58 -070096
Eric Laurentf269b8e2014-06-09 20:01:29 -070097 destroyAudioPolicyManager(mAudioPolicyManager);
Eric Laurentdce54a12014-03-10 12:19:46 -070098 delete mAudioPolicyClient;
Eric Laurentb52c1522014-05-20 11:27:36 -070099
100 mNotificationClients.clear();
bryant_liuba2b4392014-06-11 16:49:30 +0800101 mAudioPolicyEffects.clear();
Svet Ganovf4ddfef2018-01-16 07:37:58 -0800102
103 mUidPolicy->unregisterSelf();
104 mUidPolicy.clear();
Eric Laurentb52c1522014-05-20 11:27:36 -0700105}
106
107// A notification client is always registered by AudioSystem when the client process
108// connects to AudioPolicyService.
109void AudioPolicyService::registerClient(const sp<IAudioPolicyServiceClient>& client)
110{
Eric Laurent12590252015-08-21 18:40:20 -0700111 if (client == 0) {
112 ALOGW("%s got NULL client", __FUNCTION__);
113 return;
114 }
Eric Laurent0ebd5f92014-11-19 19:04:52 -0800115 Mutex::Autolock _l(mNotificationClientsLock);
Eric Laurentb52c1522014-05-20 11:27:36 -0700116
117 uid_t uid = IPCThreadState::self()->getCallingUid();
luochaojiang908c7d72018-06-21 14:58:04 +0800118 pid_t pid = IPCThreadState::self()->getCallingPid();
119 int64_t token = ((int64_t)uid<<32) | pid;
120
121 if (mNotificationClients.indexOfKey(token) < 0) {
Eric Laurentb52c1522014-05-20 11:27:36 -0700122 sp<NotificationClient> notificationClient = new NotificationClient(this,
123 client,
luochaojiang908c7d72018-06-21 14:58:04 +0800124 uid,
125 pid);
126 ALOGV("registerClient() client %p, uid %d pid %d", client.get(), uid, pid);
Eric Laurentb52c1522014-05-20 11:27:36 -0700127
luochaojiang908c7d72018-06-21 14:58:04 +0800128 mNotificationClients.add(token, notificationClient);
Eric Laurentb52c1522014-05-20 11:27:36 -0700129
Marco Nelissenf8880202014-11-14 07:58:25 -0800130 sp<IBinder> binder = IInterface::asBinder(client);
Eric Laurentb52c1522014-05-20 11:27:36 -0700131 binder->linkToDeath(notificationClient);
132 }
133}
134
Eric Laurente8726fe2015-06-26 09:39:24 -0700135void AudioPolicyService::setAudioPortCallbacksEnabled(bool enabled)
136{
137 Mutex::Autolock _l(mNotificationClientsLock);
138
139 uid_t uid = IPCThreadState::self()->getCallingUid();
luochaojiang908c7d72018-06-21 14:58:04 +0800140 pid_t pid = IPCThreadState::self()->getCallingPid();
141 int64_t token = ((int64_t)uid<<32) | pid;
142
143 if (mNotificationClients.indexOfKey(token) < 0) {
Eric Laurente8726fe2015-06-26 09:39:24 -0700144 return;
145 }
luochaojiang908c7d72018-06-21 14:58:04 +0800146 mNotificationClients.valueFor(token)->setAudioPortCallbacksEnabled(enabled);
Eric Laurente8726fe2015-06-26 09:39:24 -0700147}
148
Eric Laurentb52c1522014-05-20 11:27:36 -0700149// removeNotificationClient() is called when the client process dies.
luochaojiang908c7d72018-06-21 14:58:04 +0800150void AudioPolicyService::removeNotificationClient(uid_t uid, pid_t pid)
Eric Laurentb52c1522014-05-20 11:27:36 -0700151{
Eric Laurent0ebd5f92014-11-19 19:04:52 -0800152 {
153 Mutex::Autolock _l(mNotificationClientsLock);
luochaojiang908c7d72018-06-21 14:58:04 +0800154 int64_t token = ((int64_t)uid<<32) | pid;
155 mNotificationClients.removeItem(token);
Eric Laurent0ebd5f92014-11-19 19:04:52 -0800156 }
Eric Laurent0ebd5f92014-11-19 19:04:52 -0800157 {
158 Mutex::Autolock _l(mLock);
luochaojiang908c7d72018-06-21 14:58:04 +0800159 bool hasSameUid = false;
160 for (size_t i = 0; i < mNotificationClients.size(); i++) {
161 if (mNotificationClients.valueAt(i)->uid() == uid) {
162 hasSameUid = true;
163 break;
164 }
165 }
166 if (mAudioPolicyManager && !hasSameUid) {
Eric Laurent10b71232018-04-13 18:14:44 -0700167 // called from binder death notification: no need to clear caller identity
Eric Laurent8c7e6da2015-04-21 17:37:00 -0700168 mAudioPolicyManager->releaseResourcesForUid(uid);
Eric Laurentb52c1522014-05-20 11:27:36 -0700169 }
Eric Laurent0ebd5f92014-11-19 19:04:52 -0800170 }
Eric Laurentb52c1522014-05-20 11:27:36 -0700171}
172
173void AudioPolicyService::onAudioPortListUpdate()
174{
175 mOutputCommandThread->updateAudioPortListCommand();
176}
177
178void AudioPolicyService::doOnAudioPortListUpdate()
179{
Eric Laurent0ebd5f92014-11-19 19:04:52 -0800180 Mutex::Autolock _l(mNotificationClientsLock);
Eric Laurentb52c1522014-05-20 11:27:36 -0700181 for (size_t i = 0; i < mNotificationClients.size(); i++) {
182 mNotificationClients.valueAt(i)->onAudioPortListUpdate();
183 }
184}
185
186void AudioPolicyService::onAudioPatchListUpdate()
187{
188 mOutputCommandThread->updateAudioPatchListCommand();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700189}
190
Eric Laurentb52c1522014-05-20 11:27:36 -0700191void AudioPolicyService::doOnAudioPatchListUpdate()
192{
Eric Laurent0ebd5f92014-11-19 19:04:52 -0800193 Mutex::Autolock _l(mNotificationClientsLock);
Eric Laurentb52c1522014-05-20 11:27:36 -0700194 for (size_t i = 0; i < mNotificationClients.size(); i++) {
195 mNotificationClients.valueAt(i)->onAudioPatchListUpdate();
196 }
197}
198
Chih-Hung Hsiehe964d4e2016-08-09 14:31:32 -0700199void AudioPolicyService::onDynamicPolicyMixStateUpdate(const String8& regId, int32_t state)
Jean-Michel Trivide801052015-04-14 19:10:14 -0700200{
201 ALOGV("AudioPolicyService::onDynamicPolicyMixStateUpdate(%s, %d)",
202 regId.string(), state);
203 mOutputCommandThread->dynamicPolicyMixStateUpdateCommand(regId, state);
204}
205
Chih-Hung Hsiehe964d4e2016-08-09 14:31:32 -0700206void AudioPolicyService::doOnDynamicPolicyMixStateUpdate(const String8& regId, int32_t state)
Jean-Michel Trivide801052015-04-14 19:10:14 -0700207{
208 Mutex::Autolock _l(mNotificationClientsLock);
209 for (size_t i = 0; i < mNotificationClients.size(); i++) {
210 mNotificationClients.valueAt(i)->onDynamicPolicyMixStateUpdate(regId, state);
211 }
212}
213
Jean-Michel Triviac4e4292016-12-22 11:39:31 -0800214void AudioPolicyService::onRecordingConfigurationUpdate(int event,
215 const record_client_info_t *clientInfo, const audio_config_base_t *clientConfig,
Jean-Michel Trivi8c7cf3b2016-02-25 17:08:24 -0800216 const audio_config_base_t *deviceConfig, audio_patch_handle_t patchHandle)
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -0800217{
Jean-Michel Triviac4e4292016-12-22 11:39:31 -0800218 mOutputCommandThread->recordingConfigurationUpdateCommand(event, clientInfo,
Jean-Michel Trivi8c7cf3b2016-02-25 17:08:24 -0800219 clientConfig, deviceConfig, patchHandle);
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -0800220}
221
Jean-Michel Triviac4e4292016-12-22 11:39:31 -0800222void AudioPolicyService::doOnRecordingConfigurationUpdate(int event,
223 const record_client_info_t *clientInfo, const audio_config_base_t *clientConfig,
Jean-Michel Trivi8c7cf3b2016-02-25 17:08:24 -0800224 const audio_config_base_t *deviceConfig, audio_patch_handle_t patchHandle)
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -0800225{
226 Mutex::Autolock _l(mNotificationClientsLock);
227 for (size_t i = 0; i < mNotificationClients.size(); i++) {
Jean-Michel Triviac4e4292016-12-22 11:39:31 -0800228 mNotificationClients.valueAt(i)->onRecordingConfigurationUpdate(event, clientInfo,
Jean-Michel Trivi8c7cf3b2016-02-25 17:08:24 -0800229 clientConfig, deviceConfig, patchHandle);
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -0800230 }
231}
232
233status_t AudioPolicyService::clientCreateAudioPatch(const struct audio_patch *patch,
234 audio_patch_handle_t *handle,
235 int delayMs)
236{
237 return mAudioCommandThread->createAudioPatchCommand(patch, handle, delayMs);
238}
239
240status_t AudioPolicyService::clientReleaseAudioPatch(audio_patch_handle_t handle,
241 int delayMs)
242{
243 return mAudioCommandThread->releaseAudioPatchCommand(handle, delayMs);
244}
245
Eric Laurente1715a42014-05-20 11:30:42 -0700246status_t AudioPolicyService::clientSetAudioPortConfig(const struct audio_port_config *config,
247 int delayMs)
248{
249 return mAudioCommandThread->setAudioPortConfigCommand(config, delayMs);
250}
251
Eric Laurentb52c1522014-05-20 11:27:36 -0700252AudioPolicyService::NotificationClient::NotificationClient(const sp<AudioPolicyService>& service,
253 const sp<IAudioPolicyServiceClient>& client,
luochaojiang908c7d72018-06-21 14:58:04 +0800254 uid_t uid,
255 pid_t pid)
256 : mService(service), mUid(uid), mPid(pid), mAudioPolicyServiceClient(client),
Eric Laurente8726fe2015-06-26 09:39:24 -0700257 mAudioPortCallbacksEnabled(false)
Eric Laurentb52c1522014-05-20 11:27:36 -0700258{
259}
260
261AudioPolicyService::NotificationClient::~NotificationClient()
262{
263}
264
265void AudioPolicyService::NotificationClient::binderDied(const wp<IBinder>& who __unused)
266{
267 sp<NotificationClient> keep(this);
268 sp<AudioPolicyService> service = mService.promote();
269 if (service != 0) {
luochaojiang908c7d72018-06-21 14:58:04 +0800270 service->removeNotificationClient(mUid, mPid);
Eric Laurentb52c1522014-05-20 11:27:36 -0700271 }
272}
273
274void AudioPolicyService::NotificationClient::onAudioPortListUpdate()
275{
Eric Laurente8726fe2015-06-26 09:39:24 -0700276 if (mAudioPolicyServiceClient != 0 && mAudioPortCallbacksEnabled) {
Eric Laurentb52c1522014-05-20 11:27:36 -0700277 mAudioPolicyServiceClient->onAudioPortListUpdate();
278 }
279}
280
281void AudioPolicyService::NotificationClient::onAudioPatchListUpdate()
282{
Eric Laurente8726fe2015-06-26 09:39:24 -0700283 if (mAudioPolicyServiceClient != 0 && mAudioPortCallbacksEnabled) {
Eric Laurentb52c1522014-05-20 11:27:36 -0700284 mAudioPolicyServiceClient->onAudioPatchListUpdate();
285 }
286}
Eric Laurent57dae992011-07-24 13:36:09 -0700287
Jean-Michel Trivide801052015-04-14 19:10:14 -0700288void AudioPolicyService::NotificationClient::onDynamicPolicyMixStateUpdate(
Chih-Hung Hsiehe964d4e2016-08-09 14:31:32 -0700289 const String8& regId, int32_t state)
Jean-Michel Trivide801052015-04-14 19:10:14 -0700290{
Andy Hung4ef19fa2018-05-15 19:35:29 -0700291 if (mAudioPolicyServiceClient != 0 && isServiceUid(mUid)) {
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -0800292 mAudioPolicyServiceClient->onDynamicPolicyMixStateUpdate(regId, state);
293 }
294}
295
296void AudioPolicyService::NotificationClient::onRecordingConfigurationUpdate(
Jean-Michel Triviac4e4292016-12-22 11:39:31 -0800297 int event, const record_client_info_t *clientInfo,
Jean-Michel Trivi8c7cf3b2016-02-25 17:08:24 -0800298 const audio_config_base_t *clientConfig, const audio_config_base_t *deviceConfig,
299 audio_patch_handle_t patchHandle)
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -0800300{
Andy Hung4ef19fa2018-05-15 19:35:29 -0700301 if (mAudioPolicyServiceClient != 0 && isServiceUid(mUid)) {
Jean-Michel Triviac4e4292016-12-22 11:39:31 -0800302 mAudioPolicyServiceClient->onRecordingConfigurationUpdate(event, clientInfo,
Jean-Michel Trivi8c7cf3b2016-02-25 17:08:24 -0800303 clientConfig, deviceConfig, patchHandle);
Jean-Michel Trivide801052015-04-14 19:10:14 -0700304 }
305}
306
Eric Laurente8726fe2015-06-26 09:39:24 -0700307void AudioPolicyService::NotificationClient::setAudioPortCallbacksEnabled(bool enabled)
308{
309 mAudioPortCallbacksEnabled = enabled;
310}
311
312
Mathias Agopian65ab4712010-07-14 17:59:35 -0700313void AudioPolicyService::binderDied(const wp<IBinder>& who) {
Glenn Kasten411e4472012-11-02 10:00:06 -0700314 ALOGW("binderDied() %p, calling pid %d", who.unsafe_get(),
Eric Laurentde070132010-07-13 04:45:46 -0700315 IPCThreadState::self()->getCallingPid());
Mathias Agopian65ab4712010-07-14 17:59:35 -0700316}
317
318static bool tryLock(Mutex& mutex)
319{
320 bool locked = false;
321 for (int i = 0; i < kDumpLockRetries; ++i) {
322 if (mutex.tryLock() == NO_ERROR) {
323 locked = true;
324 break;
325 }
Glenn Kasten22ecc912012-01-09 08:33:38 -0800326 usleep(kDumpLockSleepUs);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700327 }
328 return locked;
329}
330
331status_t AudioPolicyService::dumpInternals(int fd)
332{
333 const size_t SIZE = 256;
334 char buffer[SIZE];
335 String8 result;
336
Eric Laurentdce54a12014-03-10 12:19:46 -0700337 snprintf(buffer, SIZE, "AudioPolicyManager: %p\n", mAudioPolicyManager);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700338 result.append(buffer);
339 snprintf(buffer, SIZE, "Command Thread: %p\n", mAudioCommandThread.get());
340 result.append(buffer);
341 snprintf(buffer, SIZE, "Tones Thread: %p\n", mTonePlaybackThread.get());
342 result.append(buffer);
343
344 write(fd, result.string(), result.size());
345 return NO_ERROR;
346}
347
Svet Ganovf4ddfef2018-01-16 07:37:58 -0800348void AudioPolicyService::setRecordSilenced(uid_t uid, bool silenced)
349{
Mikhail Naganoveae73eb2018-04-03 16:57:36 -0700350 {
351 Mutex::Autolock _l(mLock);
352 if (mAudioPolicyManager) {
Eric Laurent10b71232018-04-13 18:14:44 -0700353 AutoCallerClear acc;
Mikhail Naganoveae73eb2018-04-03 16:57:36 -0700354 mAudioPolicyManager->setRecordSilenced(uid, silenced);
355 }
356 }
357 sp<IAudioFlinger> af = AudioSystem::get_audio_flinger();
358 if (af) {
359 af->setRecordSilenced(uid, silenced);
360 }
Svet Ganovf4ddfef2018-01-16 07:37:58 -0800361}
362
Glenn Kasten0f11b512014-01-31 16:18:54 -0800363status_t AudioPolicyService::dump(int fd, const Vector<String16>& args __unused)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700364{
Glenn Kasten44deb052012-02-05 18:09:08 -0800365 if (!dumpAllowed()) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700366 dumpPermissionDenial(fd);
367 } else {
368 bool locked = tryLock(mLock);
369 if (!locked) {
370 String8 result(kDeadlockedString);
371 write(fd, result.string(), result.size());
372 }
373
374 dumpInternals(fd);
Glenn Kasten9d1f02d2012-02-08 17:47:58 -0800375 if (mAudioCommandThread != 0) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700376 mAudioCommandThread->dump(fd);
377 }
Glenn Kasten9d1f02d2012-02-08 17:47:58 -0800378 if (mTonePlaybackThread != 0) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700379 mTonePlaybackThread->dump(fd);
380 }
381
Eric Laurentdce54a12014-03-10 12:19:46 -0700382 if (mAudioPolicyManager) {
383 mAudioPolicyManager->dump(fd);
384 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700385
386 if (locked) mLock.unlock();
387 }
388 return NO_ERROR;
389}
390
391status_t AudioPolicyService::dumpPermissionDenial(int fd)
392{
393 const size_t SIZE = 256;
394 char buffer[SIZE];
395 String8 result;
396 snprintf(buffer, SIZE, "Permission Denial: "
397 "can't dump AudioPolicyService from pid=%d, uid=%d\n",
398 IPCThreadState::self()->getCallingPid(),
399 IPCThreadState::self()->getCallingUid());
400 result.append(buffer);
401 write(fd, result.string(), result.size());
402 return NO_ERROR;
403}
404
405status_t AudioPolicyService::onTransact(
Svet Ganovf4ddfef2018-01-16 07:37:58 -0800406 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags) {
407 switch (code) {
408 case SHELL_COMMAND_TRANSACTION: {
409 int in = data.readFileDescriptor();
410 int out = data.readFileDescriptor();
411 int err = data.readFileDescriptor();
412 int argc = data.readInt32();
413 Vector<String16> args;
414 for (int i = 0; i < argc && data.dataAvail() > 0; i++) {
415 args.add(data.readString16());
416 }
417 sp<IBinder> unusedCallback;
418 sp<IResultReceiver> resultReceiver;
419 status_t status;
420 if ((status = data.readNullableStrongBinder(&unusedCallback)) != NO_ERROR) {
421 return status;
422 }
423 if ((status = data.readNullableStrongBinder(&resultReceiver)) != NO_ERROR) {
424 return status;
425 }
426 status = shellCommand(in, out, err, args);
427 if (resultReceiver != nullptr) {
428 resultReceiver->send(status);
429 }
430 return NO_ERROR;
431 }
432 }
433
Mathias Agopian65ab4712010-07-14 17:59:35 -0700434 return BnAudioPolicyService::onTransact(code, data, reply, flags);
435}
436
Svet Ganovf4ddfef2018-01-16 07:37:58 -0800437// ------------------- Shell command implementation -------------------
438
439// NOTE: This is a remote API - make sure all args are validated
440status_t AudioPolicyService::shellCommand(int in, int out, int err, Vector<String16>& args) {
441 if (!checkCallingPermission(sManageAudioPolicyPermission, nullptr, nullptr)) {
442 return PERMISSION_DENIED;
443 }
444 if (in == BAD_TYPE || out == BAD_TYPE || err == BAD_TYPE) {
445 return BAD_VALUE;
446 }
447 if (args.size() == 3 && args[0] == String16("set-uid-state")) {
448 return handleSetUidState(args, err);
449 } else if (args.size() == 2 && args[0] == String16("reset-uid-state")) {
450 return handleResetUidState(args, err);
451 } else if (args.size() == 2 && args[0] == String16("get-uid-state")) {
452 return handleGetUidState(args, out, err);
453 } else if (args.size() == 1 && args[0] == String16("help")) {
454 printHelp(out);
455 return NO_ERROR;
456 }
457 printHelp(err);
458 return BAD_VALUE;
459}
460
461status_t AudioPolicyService::handleSetUidState(Vector<String16>& args, int err) {
462 PermissionController pc;
463 int uid = pc.getPackageUid(args[1], 0);
464 if (uid <= 0) {
465 ALOGE("Unknown package: '%s'", String8(args[1]).string());
466 dprintf(err, "Unknown package: '%s'\n", String8(args[1]).string());
467 return BAD_VALUE;
468 }
469 bool active = false;
470 if (args[2] == String16("active")) {
471 active = true;
472 } else if ((args[2] != String16("idle"))) {
473 ALOGE("Expected active or idle but got: '%s'", String8(args[2]).string());
474 return BAD_VALUE;
475 }
476 mUidPolicy->addOverrideUid(uid, active);
477 return NO_ERROR;
478}
479
480status_t AudioPolicyService::handleResetUidState(Vector<String16>& args, int err) {
481 PermissionController pc;
482 int uid = pc.getPackageUid(args[1], 0);
483 if (uid < 0) {
484 ALOGE("Unknown package: '%s'", String8(args[1]).string());
485 dprintf(err, "Unknown package: '%s'\n", String8(args[1]).string());
486 return BAD_VALUE;
487 }
488 mUidPolicy->removeOverrideUid(uid);
489 return NO_ERROR;
490}
491
492status_t AudioPolicyService::handleGetUidState(Vector<String16>& args, int out, int err) {
493 PermissionController pc;
494 int uid = pc.getPackageUid(args[1], 0);
495 if (uid < 0) {
496 ALOGE("Unknown package: '%s'", String8(args[1]).string());
497 dprintf(err, "Unknown package: '%s'\n", String8(args[1]).string());
498 return BAD_VALUE;
499 }
500 if (mUidPolicy->isUidActive(uid)) {
501 return dprintf(out, "active\n");
502 } else {
503 return dprintf(out, "idle\n");
504 }
505}
506
507status_t AudioPolicyService::printHelp(int out) {
508 return dprintf(out, "Audio policy service commands:\n"
509 " get-uid-state <PACKAGE> gets the uid state\n"
510 " set-uid-state <PACKAGE> <active|idle> overrides the uid state\n"
511 " reset-uid-state <PACKAGE> clears the uid state override\n"
512 " help print this message\n");
513}
514
515// ----------- AudioPolicyService::UidPolicy implementation ----------
516
517void AudioPolicyService::UidPolicy::registerSelf() {
518 ActivityManager am;
519 am.registerUidObserver(this, ActivityManager::UID_OBSERVER_GONE
520 | ActivityManager::UID_OBSERVER_IDLE
521 | ActivityManager::UID_OBSERVER_ACTIVE,
522 ActivityManager::PROCESS_STATE_UNKNOWN,
523 String16("audioserver"));
Mikhail Naganoveae73eb2018-04-03 16:57:36 -0700524 status_t res = am.linkToDeath(this);
525 if (!res) {
526 Mutex::Autolock _l(mLock);
527 mObserverRegistered = true;
528 } else {
529 ALOGE("UidPolicy::registerSelf linkToDeath failed: %d", res);
530 am.unregisterUidObserver(this);
531 }
Svet Ganovf4ddfef2018-01-16 07:37:58 -0800532}
533
534void AudioPolicyService::UidPolicy::unregisterSelf() {
535 ActivityManager am;
Mikhail Naganoveae73eb2018-04-03 16:57:36 -0700536 am.unlinkToDeath(this);
Svet Ganovf4ddfef2018-01-16 07:37:58 -0800537 am.unregisterUidObserver(this);
Mikhail Naganoveae73eb2018-04-03 16:57:36 -0700538 Mutex::Autolock _l(mLock);
539 mObserverRegistered = false;
Svet Ganovf4ddfef2018-01-16 07:37:58 -0800540}
541
Mikhail Naganoveae73eb2018-04-03 16:57:36 -0700542void AudioPolicyService::UidPolicy::binderDied(__unused const wp<IBinder> &who) {
543 Mutex::Autolock _l(mLock);
544 mCachedUids.clear();
545 mObserverRegistered = false;
Svet Ganovf4ddfef2018-01-16 07:37:58 -0800546}
547
548bool AudioPolicyService::UidPolicy::isUidActive(uid_t uid) {
Mikhail Naganoveae73eb2018-04-03 16:57:36 -0700549 if (isServiceUid(uid)) return true;
550 bool needToReregister = false;
551 {
552 Mutex::Autolock _l(mLock);
553 needToReregister = !mObserverRegistered;
Svet Ganovf4ddfef2018-01-16 07:37:58 -0800554 }
Mikhail Naganoveae73eb2018-04-03 16:57:36 -0700555 if (needToReregister) {
556 // Looks like ActivityManager has died previously, attempt to re-register.
557 registerSelf();
558 }
559 {
560 Mutex::Autolock _l(mLock);
561 auto overrideIter = mOverrideUids.find(uid);
562 if (overrideIter != mOverrideUids.end()) {
563 return overrideIter->second;
564 }
565 // In an absense of the ActivityManager, assume everything to be active.
566 if (!mObserverRegistered) return true;
567 auto cacheIter = mCachedUids.find(uid);
Mikhail Naganoveba668a2018-04-05 08:13:15 -0700568 if (cacheIter != mCachedUids.end()) {
Mikhail Naganoveae73eb2018-04-03 16:57:36 -0700569 return cacheIter->second;
570 }
571 }
572 ActivityManager am;
573 bool active = am.isUidActive(uid, String16("audioserver"));
574 {
575 Mutex::Autolock _l(mLock);
576 mCachedUids.insert(std::pair<uid_t, bool>(uid, active));
577 }
578 return active;
Svet Ganovf4ddfef2018-01-16 07:37:58 -0800579}
580
Mikhail Naganoveae73eb2018-04-03 16:57:36 -0700581void AudioPolicyService::UidPolicy::onUidActive(uid_t uid) {
582 updateUidCache(uid, true, true);
583}
584
585void AudioPolicyService::UidPolicy::onUidGone(uid_t uid, __unused bool disabled) {
586 updateUidCache(uid, false, false);
587}
588
589void AudioPolicyService::UidPolicy::onUidIdle(uid_t uid, __unused bool disabled) {
590 updateUidCache(uid, false, true);
591}
592
Mikhail Naganoveae73eb2018-04-03 16:57:36 -0700593void AudioPolicyService::UidPolicy::notifyService(uid_t uid, bool active) {
594 sp<AudioPolicyService> service = mService.promote();
595 if (service != nullptr) {
596 service->setRecordSilenced(uid, !active);
597 }
598}
599
600void AudioPolicyService::UidPolicy::updateOverrideUid(uid_t uid, bool active, bool insert) {
601 if (isServiceUid(uid)) return;
602 bool wasOverridden = false, wasActive = false;
603 {
604 Mutex::Autolock _l(mLock);
605 updateUidLocked(&mOverrideUids, uid, active, insert, &wasOverridden, &wasActive);
606 }
607 if (!wasOverridden && insert) {
608 notifyService(uid, active); // Started to override.
609 } else if (wasOverridden && !insert) {
610 notifyService(uid, isUidActive(uid)); // Override ceased, notify with ground truth.
611 } else if (wasActive != active) {
612 notifyService(uid, active); // Override updated.
613 }
614}
615
616void AudioPolicyService::UidPolicy::updateUidCache(uid_t uid, bool active, bool insert) {
617 if (isServiceUid(uid)) return;
618 bool wasActive = false;
619 {
620 Mutex::Autolock _l(mLock);
621 updateUidLocked(&mCachedUids, uid, active, insert, nullptr, &wasActive);
622 // Do not notify service if currently overridden.
623 if (mOverrideUids.find(uid) != mOverrideUids.end()) return;
624 }
625 bool nowActive = active && insert;
626 if (wasActive != nowActive) notifyService(uid, nowActive);
627}
628
629void AudioPolicyService::UidPolicy::updateUidLocked(std::unordered_map<uid_t, bool> *uids,
630 uid_t uid, bool active, bool insert, bool *wasThere, bool *wasActive) {
631 auto it = uids->find(uid);
632 if (it != uids->end()) {
633 if (wasThere != nullptr) *wasThere = true;
634 if (wasActive != nullptr) *wasActive = it->second;
635 if (insert) {
636 it->second = active;
637 } else {
638 uids->erase(it);
639 }
640 } else if (insert) {
641 uids->insert(std::pair<uid_t, bool>(uid, active));
642 }
Svet Ganovf4ddfef2018-01-16 07:37:58 -0800643}
Mathias Agopian65ab4712010-07-14 17:59:35 -0700644
Mathias Agopian65ab4712010-07-14 17:59:35 -0700645// ----------- AudioPolicyService::AudioCommandThread implementation ----------
646
Eric Laurentbfb1b832013-01-07 09:53:42 -0800647AudioPolicyService::AudioCommandThread::AudioCommandThread(String8 name,
648 const wp<AudioPolicyService>& service)
649 : Thread(false), mName(name), mService(service)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700650{
651 mpToneGenerator = NULL;
652}
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();
Glenn Kastene9dd0172012-01-27 18:08:45 -0800661 delete mpToneGenerator;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700662}
663
664void AudioPolicyService::AudioCommandThread::onFirstRef()
665{
Eric Laurentbfb1b832013-01-07 09:53:42 -0800666 run(mName.string(), ANDROID_PRIORITY_AUDIO);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700667}
668
669bool AudioPolicyService::AudioCommandThread::threadLoop()
670{
Eric Laurentd7eda8d2016-02-02 17:18:39 -0800671 nsecs_t waitTime = -1;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700672
673 mLock.lock();
674 while (!exitPending())
675 {
Eric Laurent59a89232014-06-08 14:14:17 -0700676 sp<AudioPolicyService> svc;
677 while (!mAudioCommands.isEmpty() && !exitPending()) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700678 nsecs_t curTime = systemTime();
679 // commands are sorted by increasing time stamp: execute them from index 0 and up
680 if (mAudioCommands[0]->mTime <= curTime) {
Eric Laurent0ede8922014-05-09 18:04:42 -0700681 sp<AudioCommand> command = mAudioCommands[0];
Mathias Agopian65ab4712010-07-14 17:59:35 -0700682 mAudioCommands.removeAt(0);
Eric Laurent0ede8922014-05-09 18:04:42 -0700683 mLastCommand = command;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700684
685 switch (command->mCommand) {
686 case START_TONE: {
687 mLock.unlock();
Eric Laurent0ede8922014-05-09 18:04:42 -0700688 ToneData *data = (ToneData *)command->mParam.get();
Steve Block3856b092011-10-20 11:56:00 +0100689 ALOGV("AudioCommandThread() processing start tone %d on stream %d",
Mathias Agopian65ab4712010-07-14 17:59:35 -0700690 data->mType, data->mStream);
Glenn Kastene9dd0172012-01-27 18:08:45 -0800691 delete mpToneGenerator;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700692 mpToneGenerator = new ToneGenerator(data->mStream, 1.0);
693 mpToneGenerator->startTone(data->mType);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700694 mLock.lock();
695 }break;
696 case STOP_TONE: {
697 mLock.unlock();
Steve Block3856b092011-10-20 11:56:00 +0100698 ALOGV("AudioCommandThread() processing stop tone");
Mathias Agopian65ab4712010-07-14 17:59:35 -0700699 if (mpToneGenerator != NULL) {
700 mpToneGenerator->stopTone();
701 delete mpToneGenerator;
702 mpToneGenerator = NULL;
703 }
704 mLock.lock();
705 }break;
706 case SET_VOLUME: {
Eric Laurent0ede8922014-05-09 18:04:42 -0700707 VolumeData *data = (VolumeData *)command->mParam.get();
Steve Block3856b092011-10-20 11:56:00 +0100708 ALOGV("AudioCommandThread() processing set volume stream %d, \
Eric Laurentde070132010-07-13 04:45:46 -0700709 volume %f, output %d", data->mStream, data->mVolume, data->mIO);
710 command->mStatus = AudioSystem::setStreamVolume(data->mStream,
711 data->mVolume,
712 data->mIO);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700713 }break;
714 case SET_PARAMETERS: {
Eric Laurent0ede8922014-05-09 18:04:42 -0700715 ParametersData *data = (ParametersData *)command->mParam.get();
Glenn Kastene53b9ea2012-03-12 16:29:55 -0700716 ALOGV("AudioCommandThread() processing set parameters string %s, io %d",
717 data->mKeyValuePairs.string(), data->mIO);
718 command->mStatus = AudioSystem::setParameters(data->mIO, data->mKeyValuePairs);
Glenn Kastene53b9ea2012-03-12 16:29:55 -0700719 }break;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700720 case SET_VOICE_VOLUME: {
Eric Laurent0ede8922014-05-09 18:04:42 -0700721 VoiceVolumeData *data = (VoiceVolumeData *)command->mParam.get();
Steve Block3856b092011-10-20 11:56:00 +0100722 ALOGV("AudioCommandThread() processing set voice volume volume %f",
Eric Laurentde070132010-07-13 04:45:46 -0700723 data->mVolume);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700724 command->mStatus = AudioSystem::setVoiceVolume(data->mVolume);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700725 }break;
Eric Laurentbfb1b832013-01-07 09:53:42 -0800726 case STOP_OUTPUT: {
Eric Laurent0ede8922014-05-09 18:04:42 -0700727 StopOutputData *data = (StopOutputData *)command->mParam.get();
Eric Laurentbfb1b832013-01-07 09:53:42 -0800728 ALOGV("AudioCommandThread() processing stop output %d",
729 data->mIO);
Eric Laurent59a89232014-06-08 14:14:17 -0700730 svc = mService.promote();
Eric Laurentbfb1b832013-01-07 09:53:42 -0800731 if (svc == 0) {
732 break;
733 }
734 mLock.unlock();
735 svc->doStopOutput(data->mIO, data->mStream, data->mSession);
736 mLock.lock();
Eric Laurentbfb1b832013-01-07 09:53:42 -0800737 }break;
738 case RELEASE_OUTPUT: {
Eric Laurent0ede8922014-05-09 18:04:42 -0700739 ReleaseOutputData *data = (ReleaseOutputData *)command->mParam.get();
Eric Laurentbfb1b832013-01-07 09:53:42 -0800740 ALOGV("AudioCommandThread() processing release output %d",
741 data->mIO);
Eric Laurent59a89232014-06-08 14:14:17 -0700742 svc = mService.promote();
Eric Laurentbfb1b832013-01-07 09:53:42 -0800743 if (svc == 0) {
744 break;
745 }
746 mLock.unlock();
Eric Laurente83b55d2014-11-14 10:06:21 -0800747 svc->doReleaseOutput(data->mIO, data->mStream, data->mSession);
Eric Laurentbfb1b832013-01-07 09:53:42 -0800748 mLock.lock();
Eric Laurentbfb1b832013-01-07 09:53:42 -0800749 }break;
Eric Laurent951f4552014-05-20 10:48:17 -0700750 case CREATE_AUDIO_PATCH: {
751 CreateAudioPatchData *data = (CreateAudioPatchData *)command->mParam.get();
752 ALOGV("AudioCommandThread() processing create audio patch");
753 sp<IAudioFlinger> af = AudioSystem::get_audio_flinger();
754 if (af == 0) {
755 command->mStatus = PERMISSION_DENIED;
756 } else {
757 command->mStatus = af->createAudioPatch(&data->mPatch, &data->mHandle);
758 }
759 } break;
760 case RELEASE_AUDIO_PATCH: {
761 ReleaseAudioPatchData *data = (ReleaseAudioPatchData *)command->mParam.get();
762 ALOGV("AudioCommandThread() processing release audio patch");
763 sp<IAudioFlinger> af = AudioSystem::get_audio_flinger();
764 if (af == 0) {
765 command->mStatus = PERMISSION_DENIED;
766 } else {
767 command->mStatus = af->releaseAudioPatch(data->mHandle);
768 }
769 } break;
Eric Laurentb52c1522014-05-20 11:27:36 -0700770 case UPDATE_AUDIOPORT_LIST: {
771 ALOGV("AudioCommandThread() processing update audio port list");
Eric Laurent59a89232014-06-08 14:14:17 -0700772 svc = mService.promote();
Eric Laurentb52c1522014-05-20 11:27:36 -0700773 if (svc == 0) {
774 break;
775 }
776 mLock.unlock();
777 svc->doOnAudioPortListUpdate();
778 mLock.lock();
779 }break;
780 case UPDATE_AUDIOPATCH_LIST: {
781 ALOGV("AudioCommandThread() processing update audio patch list");
Eric Laurent59a89232014-06-08 14:14:17 -0700782 svc = mService.promote();
Eric Laurentb52c1522014-05-20 11:27:36 -0700783 if (svc == 0) {
784 break;
785 }
786 mLock.unlock();
787 svc->doOnAudioPatchListUpdate();
788 mLock.lock();
789 }break;
Eric Laurente1715a42014-05-20 11:30:42 -0700790 case SET_AUDIOPORT_CONFIG: {
791 SetAudioPortConfigData *data = (SetAudioPortConfigData *)command->mParam.get();
792 ALOGV("AudioCommandThread() processing set port config");
793 sp<IAudioFlinger> af = AudioSystem::get_audio_flinger();
794 if (af == 0) {
795 command->mStatus = PERMISSION_DENIED;
796 } else {
797 command->mStatus = af->setAudioPortConfig(&data->mConfig);
798 }
799 } break;
Jean-Michel Trivide801052015-04-14 19:10:14 -0700800 case DYN_POLICY_MIX_STATE_UPDATE: {
801 DynPolicyMixStateUpdateData *data =
802 (DynPolicyMixStateUpdateData *)command->mParam.get();
Jean-Michel Trivide801052015-04-14 19:10:14 -0700803 ALOGV("AudioCommandThread() processing dyn policy mix state update %s %d",
804 data->mRegId.string(), data->mState);
805 svc = mService.promote();
806 if (svc == 0) {
807 break;
808 }
809 mLock.unlock();
810 svc->doOnDynamicPolicyMixStateUpdate(data->mRegId, data->mState);
811 mLock.lock();
812 } break;
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -0800813 case RECORDING_CONFIGURATION_UPDATE: {
814 RecordingConfigurationUpdateData *data =
815 (RecordingConfigurationUpdateData *)command->mParam.get();
816 ALOGV("AudioCommandThread() processing recording configuration update");
817 svc = mService.promote();
818 if (svc == 0) {
819 break;
820 }
821 mLock.unlock();
Jean-Michel Triviac4e4292016-12-22 11:39:31 -0800822 svc->doOnRecordingConfigurationUpdate(data->mEvent, &data->mClientInfo,
823 &data->mClientConfig, &data->mDeviceConfig,
Jean-Michel Trivi8c7cf3b2016-02-25 17:08:24 -0800824 data->mPatchHandle);
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -0800825 mLock.lock();
826 } break;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700827 default:
Steve Block5ff1dd52012-01-05 23:22:43 +0000828 ALOGW("AudioCommandThread() unknown command %d", command->mCommand);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700829 }
Eric Laurent0ede8922014-05-09 18:04:42 -0700830 {
831 Mutex::Autolock _l(command->mLock);
832 if (command->mWaitStatus) {
833 command->mWaitStatus = false;
834 command->mCond.signal();
835 }
836 }
Eric Laurentd7eda8d2016-02-02 17:18:39 -0800837 waitTime = -1;
Zach Janga754b4f2015-10-27 01:29:34 +0000838 // release mLock before releasing strong reference on the service as
839 // AudioPolicyService destructor calls AudioCommandThread::exit() which
840 // acquires mLock.
841 mLock.unlock();
842 svc.clear();
843 mLock.lock();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700844 } else {
845 waitTime = mAudioCommands[0]->mTime - curTime;
846 break;
847 }
848 }
Zach Janga754b4f2015-10-27 01:29:34 +0000849
850 // release delayed commands wake lock if the queue is empty
851 if (mAudioCommands.isEmpty()) {
Ricardo Garcia05f2fdc2014-07-24 15:48:24 -0700852 release_wake_lock(mName.string());
Zach Janga754b4f2015-10-27 01:29:34 +0000853 }
854
855 // At this stage we have either an empty command queue or the first command in the queue
856 // has a finite delay. So unless we are exiting it is safe to wait.
857 if (!exitPending()) {
Eric Laurent59a89232014-06-08 14:14:17 -0700858 ALOGV("AudioCommandThread() going to sleep");
Eric Laurentd7eda8d2016-02-02 17:18:39 -0800859 if (waitTime == -1) {
860 mWaitWorkCV.wait(mLock);
861 } else {
862 mWaitWorkCV.waitRelative(mLock, waitTime);
863 }
Eric Laurent59a89232014-06-08 14:14:17 -0700864 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700865 }
Ricardo Garcia05f2fdc2014-07-24 15:48:24 -0700866 // release delayed commands wake lock before quitting
867 if (!mAudioCommands.isEmpty()) {
868 release_wake_lock(mName.string());
869 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700870 mLock.unlock();
871 return false;
872}
873
874status_t AudioPolicyService::AudioCommandThread::dump(int fd)
875{
876 const size_t SIZE = 256;
877 char buffer[SIZE];
878 String8 result;
879
880 snprintf(buffer, SIZE, "AudioCommandThread %p Dump\n", this);
881 result.append(buffer);
882 write(fd, result.string(), result.size());
883
884 bool locked = tryLock(mLock);
885 if (!locked) {
886 String8 result2(kCmdDeadlockedString);
887 write(fd, result2.string(), result2.size());
888 }
889
890 snprintf(buffer, SIZE, "- Commands:\n");
891 result = String8(buffer);
892 result.append(" Command Time Wait pParam\n");
Glenn Kasten8d6a2442012-02-08 14:04:28 -0800893 for (size_t i = 0; i < mAudioCommands.size(); i++) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700894 mAudioCommands[i]->dump(buffer, SIZE);
895 result.append(buffer);
896 }
897 result.append(" Last Command\n");
Eric Laurent0ede8922014-05-09 18:04:42 -0700898 if (mLastCommand != 0) {
899 mLastCommand->dump(buffer, SIZE);
900 result.append(buffer);
901 } else {
902 result.append(" none\n");
903 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700904
905 write(fd, result.string(), result.size());
906
907 if (locked) mLock.unlock();
908
909 return NO_ERROR;
910}
911
Glenn Kasten3d2f8772012-01-27 15:25:25 -0800912void AudioPolicyService::AudioCommandThread::startToneCommand(ToneGenerator::tone_type type,
913 audio_stream_type_t stream)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700914{
Eric Laurent0ede8922014-05-09 18:04:42 -0700915 sp<AudioCommand> command = new AudioCommand();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700916 command->mCommand = START_TONE;
Eric Laurent0ede8922014-05-09 18:04:42 -0700917 sp<ToneData> data = new ToneData();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700918 data->mType = type;
919 data->mStream = stream;
Jesper Tragardh48412dc2014-03-24 14:12:43 +0100920 command->mParam = data;
Steve Block3856b092011-10-20 11:56:00 +0100921 ALOGV("AudioCommandThread() adding tone start type %d, stream %d", type, stream);
Eric Laurent0ede8922014-05-09 18:04:42 -0700922 sendCommand(command);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700923}
924
925void AudioPolicyService::AudioCommandThread::stopToneCommand()
926{
Eric Laurent0ede8922014-05-09 18:04:42 -0700927 sp<AudioCommand> command = new AudioCommand();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700928 command->mCommand = STOP_TONE;
Steve Block3856b092011-10-20 11:56:00 +0100929 ALOGV("AudioCommandThread() adding tone stop");
Eric Laurent0ede8922014-05-09 18:04:42 -0700930 sendCommand(command);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700931}
932
Glenn Kastenfff6d712012-01-12 16:38:12 -0800933status_t AudioPolicyService::AudioCommandThread::volumeCommand(audio_stream_type_t stream,
Eric Laurentde070132010-07-13 04:45:46 -0700934 float volume,
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800935 audio_io_handle_t output,
Eric Laurentde070132010-07-13 04:45:46 -0700936 int delayMs)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700937{
Eric Laurent0ede8922014-05-09 18:04:42 -0700938 sp<AudioCommand> command = new AudioCommand();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700939 command->mCommand = SET_VOLUME;
Eric Laurent0ede8922014-05-09 18:04:42 -0700940 sp<VolumeData> data = new VolumeData();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700941 data->mStream = stream;
942 data->mVolume = volume;
943 data->mIO = output;
944 command->mParam = data;
Eric Laurent0ede8922014-05-09 18:04:42 -0700945 command->mWaitStatus = true;
Steve Block3856b092011-10-20 11:56:00 +0100946 ALOGV("AudioCommandThread() adding set volume stream %d, volume %f, output %d",
Eric Laurentde070132010-07-13 04:45:46 -0700947 stream, volume, output);
Eric Laurent0ede8922014-05-09 18:04:42 -0700948 return sendCommand(command, delayMs);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700949}
950
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800951status_t AudioPolicyService::AudioCommandThread::parametersCommand(audio_io_handle_t ioHandle,
Dima Zavinfce7a472011-04-19 22:30:36 -0700952 const char *keyValuePairs,
Eric Laurentde070132010-07-13 04:45:46 -0700953 int delayMs)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700954{
Eric Laurent0ede8922014-05-09 18:04:42 -0700955 sp<AudioCommand> command = new AudioCommand();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700956 command->mCommand = SET_PARAMETERS;
Eric Laurent0ede8922014-05-09 18:04:42 -0700957 sp<ParametersData> data = new ParametersData();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700958 data->mIO = ioHandle;
Dima Zavinfce7a472011-04-19 22:30:36 -0700959 data->mKeyValuePairs = String8(keyValuePairs);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700960 command->mParam = data;
Eric Laurent0ede8922014-05-09 18:04:42 -0700961 command->mWaitStatus = true;
Steve Block3856b092011-10-20 11:56:00 +0100962 ALOGV("AudioCommandThread() adding set parameter string %s, io %d ,delay %d",
Dima Zavinfce7a472011-04-19 22:30:36 -0700963 keyValuePairs, ioHandle, delayMs);
Eric Laurent0ede8922014-05-09 18:04:42 -0700964 return sendCommand(command, delayMs);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700965}
966
967status_t AudioPolicyService::AudioCommandThread::voiceVolumeCommand(float volume, int delayMs)
968{
Eric Laurent0ede8922014-05-09 18:04:42 -0700969 sp<AudioCommand> command = new AudioCommand();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700970 command->mCommand = SET_VOICE_VOLUME;
Eric Laurent0ede8922014-05-09 18:04:42 -0700971 sp<VoiceVolumeData> data = new VoiceVolumeData();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700972 data->mVolume = volume;
973 command->mParam = data;
Eric Laurent0ede8922014-05-09 18:04:42 -0700974 command->mWaitStatus = true;
Steve Block3856b092011-10-20 11:56:00 +0100975 ALOGV("AudioCommandThread() adding set voice volume volume %f", volume);
Eric Laurent0ede8922014-05-09 18:04:42 -0700976 return sendCommand(command, delayMs);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700977}
978
Eric Laurentbfb1b832013-01-07 09:53:42 -0800979void AudioPolicyService::AudioCommandThread::stopOutputCommand(audio_io_handle_t output,
980 audio_stream_type_t stream,
Eric Laurente83b55d2014-11-14 10:06:21 -0800981 audio_session_t session)
Eric Laurentbfb1b832013-01-07 09:53:42 -0800982{
Eric Laurent0ede8922014-05-09 18:04:42 -0700983 sp<AudioCommand> command = new AudioCommand();
Eric Laurentbfb1b832013-01-07 09:53:42 -0800984 command->mCommand = STOP_OUTPUT;
Eric Laurent0ede8922014-05-09 18:04:42 -0700985 sp<StopOutputData> data = new StopOutputData();
Eric Laurentbfb1b832013-01-07 09:53:42 -0800986 data->mIO = output;
987 data->mStream = stream;
988 data->mSession = session;
Jesper Tragardh48412dc2014-03-24 14:12:43 +0100989 command->mParam = data;
Eric Laurentbfb1b832013-01-07 09:53:42 -0800990 ALOGV("AudioCommandThread() adding stop output %d", output);
Eric Laurent0ede8922014-05-09 18:04:42 -0700991 sendCommand(command);
Eric Laurentbfb1b832013-01-07 09:53:42 -0800992}
993
Eric Laurente83b55d2014-11-14 10:06:21 -0800994void AudioPolicyService::AudioCommandThread::releaseOutputCommand(audio_io_handle_t output,
995 audio_stream_type_t stream,
996 audio_session_t session)
Eric Laurentbfb1b832013-01-07 09:53:42 -0800997{
Eric Laurent0ede8922014-05-09 18:04:42 -0700998 sp<AudioCommand> command = new AudioCommand();
Eric Laurentbfb1b832013-01-07 09:53:42 -0800999 command->mCommand = RELEASE_OUTPUT;
Eric Laurent0ede8922014-05-09 18:04:42 -07001000 sp<ReleaseOutputData> data = new ReleaseOutputData();
Eric Laurentbfb1b832013-01-07 09:53:42 -08001001 data->mIO = output;
Eric Laurente83b55d2014-11-14 10:06:21 -08001002 data->mStream = stream;
1003 data->mSession = session;
Jesper Tragardh48412dc2014-03-24 14:12:43 +01001004 command->mParam = data;
Eric Laurentbfb1b832013-01-07 09:53:42 -08001005 ALOGV("AudioCommandThread() adding release output %d", output);
Eric Laurent0ede8922014-05-09 18:04:42 -07001006 sendCommand(command);
1007}
1008
Eric Laurent951f4552014-05-20 10:48:17 -07001009status_t AudioPolicyService::AudioCommandThread::createAudioPatchCommand(
1010 const struct audio_patch *patch,
1011 audio_patch_handle_t *handle,
1012 int delayMs)
1013{
1014 status_t status = NO_ERROR;
1015
1016 sp<AudioCommand> command = new AudioCommand();
1017 command->mCommand = CREATE_AUDIO_PATCH;
1018 CreateAudioPatchData *data = new CreateAudioPatchData();
1019 data->mPatch = *patch;
1020 data->mHandle = *handle;
1021 command->mParam = data;
1022 command->mWaitStatus = true;
1023 ALOGV("AudioCommandThread() adding create patch delay %d", delayMs);
1024 status = sendCommand(command, delayMs);
1025 if (status == NO_ERROR) {
1026 *handle = data->mHandle;
1027 }
1028 return status;
1029}
1030
1031status_t AudioPolicyService::AudioCommandThread::releaseAudioPatchCommand(audio_patch_handle_t handle,
1032 int delayMs)
1033{
1034 sp<AudioCommand> command = new AudioCommand();
1035 command->mCommand = RELEASE_AUDIO_PATCH;
1036 ReleaseAudioPatchData *data = new ReleaseAudioPatchData();
1037 data->mHandle = handle;
1038 command->mParam = data;
1039 command->mWaitStatus = true;
1040 ALOGV("AudioCommandThread() adding release patch delay %d", delayMs);
1041 return sendCommand(command, delayMs);
1042}
1043
Eric Laurentb52c1522014-05-20 11:27:36 -07001044void AudioPolicyService::AudioCommandThread::updateAudioPortListCommand()
1045{
1046 sp<AudioCommand> command = new AudioCommand();
1047 command->mCommand = UPDATE_AUDIOPORT_LIST;
1048 ALOGV("AudioCommandThread() adding update audio port list");
1049 sendCommand(command);
1050}
1051
1052void AudioPolicyService::AudioCommandThread::updateAudioPatchListCommand()
1053{
1054 sp<AudioCommand>command = new AudioCommand();
1055 command->mCommand = UPDATE_AUDIOPATCH_LIST;
1056 ALOGV("AudioCommandThread() adding update audio patch list");
1057 sendCommand(command);
1058}
1059
Eric Laurente1715a42014-05-20 11:30:42 -07001060status_t AudioPolicyService::AudioCommandThread::setAudioPortConfigCommand(
1061 const struct audio_port_config *config, int delayMs)
1062{
1063 sp<AudioCommand> command = new AudioCommand();
1064 command->mCommand = SET_AUDIOPORT_CONFIG;
1065 SetAudioPortConfigData *data = new SetAudioPortConfigData();
1066 data->mConfig = *config;
1067 command->mParam = data;
1068 command->mWaitStatus = true;
1069 ALOGV("AudioCommandThread() adding set port config delay %d", delayMs);
1070 return sendCommand(command, delayMs);
1071}
1072
Jean-Michel Trivide801052015-04-14 19:10:14 -07001073void AudioPolicyService::AudioCommandThread::dynamicPolicyMixStateUpdateCommand(
Chih-Hung Hsiehe964d4e2016-08-09 14:31:32 -07001074 const String8& regId, int32_t state)
Jean-Michel Trivide801052015-04-14 19:10:14 -07001075{
1076 sp<AudioCommand> command = new AudioCommand();
1077 command->mCommand = DYN_POLICY_MIX_STATE_UPDATE;
1078 DynPolicyMixStateUpdateData *data = new DynPolicyMixStateUpdateData();
1079 data->mRegId = regId;
1080 data->mState = state;
1081 command->mParam = data;
1082 ALOGV("AudioCommandThread() sending dynamic policy mix (id=%s) state update to %d",
1083 regId.string(), state);
1084 sendCommand(command);
1085}
1086
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -08001087void AudioPolicyService::AudioCommandThread::recordingConfigurationUpdateCommand(
Jean-Michel Triviac4e4292016-12-22 11:39:31 -08001088 int event, const record_client_info_t *clientInfo,
Jean-Michel Trivi8c7cf3b2016-02-25 17:08:24 -08001089 const audio_config_base_t *clientConfig, const audio_config_base_t *deviceConfig,
1090 audio_patch_handle_t patchHandle)
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -08001091{
1092 sp<AudioCommand>command = new AudioCommand();
1093 command->mCommand = RECORDING_CONFIGURATION_UPDATE;
1094 RecordingConfigurationUpdateData *data = new RecordingConfigurationUpdateData();
1095 data->mEvent = event;
Jean-Michel Triviac4e4292016-12-22 11:39:31 -08001096 data->mClientInfo = *clientInfo;
Jean-Michel Trivi7281aa92016-02-17 15:33:40 -08001097 data->mClientConfig = *clientConfig;
1098 data->mDeviceConfig = *deviceConfig;
Jean-Michel Trivi8c7cf3b2016-02-25 17:08:24 -08001099 data->mPatchHandle = patchHandle;
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -08001100 command->mParam = data;
Jean-Michel Triviac4e4292016-12-22 11:39:31 -08001101 ALOGV("AudioCommandThread() adding recording configuration update event %d, source %d uid %u",
1102 event, clientInfo->source, clientInfo->uid);
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -08001103 sendCommand(command);
1104}
1105
Eric Laurent0ede8922014-05-09 18:04:42 -07001106status_t AudioPolicyService::AudioCommandThread::sendCommand(sp<AudioCommand>& command, int delayMs)
1107{
1108 {
1109 Mutex::Autolock _l(mLock);
1110 insertCommand_l(command, delayMs);
1111 mWaitWorkCV.signal();
1112 }
1113 Mutex::Autolock _l(command->mLock);
1114 while (command->mWaitStatus) {
1115 nsecs_t timeOutNs = kAudioCommandTimeoutNs + milliseconds(delayMs);
1116 if (command->mCond.waitRelative(command->mLock, timeOutNs) != NO_ERROR) {
1117 command->mStatus = TIMED_OUT;
1118 command->mWaitStatus = false;
1119 }
1120 }
1121 return command->mStatus;
Eric Laurentbfb1b832013-01-07 09:53:42 -08001122}
1123
Mathias Agopian65ab4712010-07-14 17:59:35 -07001124// insertCommand_l() must be called with mLock held
Eric Laurent0ede8922014-05-09 18:04:42 -07001125void AudioPolicyService::AudioCommandThread::insertCommand_l(sp<AudioCommand>& command, int delayMs)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001126{
Glenn Kasten8d6a2442012-02-08 14:04:28 -08001127 ssize_t i; // not size_t because i will count down to -1
Eric Laurent0ede8922014-05-09 18:04:42 -07001128 Vector < sp<AudioCommand> > removedCommands;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001129 command->mTime = systemTime() + milliseconds(delayMs);
1130
1131 // acquire wake lock to make sure delayed commands are processed
Eric Laurentbfb1b832013-01-07 09:53:42 -08001132 if (mAudioCommands.isEmpty()) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07001133 acquire_wake_lock(PARTIAL_WAKE_LOCK, mName.string());
1134 }
1135
1136 // check same pending commands with later time stamps and eliminate them
Ivan Lozano5ff158f2017-10-30 09:06:24 -07001137 for (i = (ssize_t)mAudioCommands.size()-1; i >= 0; i--) {
Eric Laurent0ede8922014-05-09 18:04:42 -07001138 sp<AudioCommand> command2 = mAudioCommands[i];
Mathias Agopian65ab4712010-07-14 17:59:35 -07001139 // commands are sorted by increasing time stamp: no need to scan the rest of mAudioCommands
1140 if (command2->mTime <= command->mTime) break;
Eric Laurente45b48a2014-09-04 16:40:57 -07001141
1142 // create audio patch or release audio patch commands are equivalent
1143 // with regard to filtering
1144 if ((command->mCommand == CREATE_AUDIO_PATCH) ||
1145 (command->mCommand == RELEASE_AUDIO_PATCH)) {
1146 if ((command2->mCommand != CREATE_AUDIO_PATCH) &&
1147 (command2->mCommand != RELEASE_AUDIO_PATCH)) {
1148 continue;
1149 }
1150 } else if (command2->mCommand != command->mCommand) continue;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001151
1152 switch (command->mCommand) {
1153 case SET_PARAMETERS: {
Eric Laurent0ede8922014-05-09 18:04:42 -07001154 ParametersData *data = (ParametersData *)command->mParam.get();
1155 ParametersData *data2 = (ParametersData *)command2->mParam.get();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001156 if (data->mIO != data2->mIO) break;
Steve Block3856b092011-10-20 11:56:00 +01001157 ALOGV("Comparing parameter command %s to new command %s",
Eric Laurentde070132010-07-13 04:45:46 -07001158 data2->mKeyValuePairs.string(), data->mKeyValuePairs.string());
Mathias Agopian65ab4712010-07-14 17:59:35 -07001159 AudioParameter param = AudioParameter(data->mKeyValuePairs);
1160 AudioParameter param2 = AudioParameter(data2->mKeyValuePairs);
1161 for (size_t j = 0; j < param.size(); j++) {
Glenn Kastene53b9ea2012-03-12 16:29:55 -07001162 String8 key;
1163 String8 value;
1164 param.getAt(j, key, value);
1165 for (size_t k = 0; k < param2.size(); k++) {
1166 String8 key2;
1167 String8 value2;
1168 param2.getAt(k, key2, value2);
1169 if (key2 == key) {
1170 param2.remove(key2);
1171 ALOGV("Filtering out parameter %s", key2.string());
1172 break;
1173 }
1174 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001175 }
1176 // if all keys have been filtered out, remove the command.
1177 // otherwise, update the key value pairs
1178 if (param2.size() == 0) {
1179 removedCommands.add(command2);
1180 } else {
1181 data2->mKeyValuePairs = param2.toString();
1182 }
Eric Laurent21e54562013-09-23 12:08:05 -07001183 command->mTime = command2->mTime;
1184 // force delayMs to non 0 so that code below does not request to wait for
1185 // command status as the command is now delayed
1186 delayMs = 1;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001187 } break;
1188
1189 case SET_VOLUME: {
Eric Laurent0ede8922014-05-09 18:04:42 -07001190 VolumeData *data = (VolumeData *)command->mParam.get();
1191 VolumeData *data2 = (VolumeData *)command2->mParam.get();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001192 if (data->mIO != data2->mIO) break;
1193 if (data->mStream != data2->mStream) break;
Steve Block3856b092011-10-20 11:56:00 +01001194 ALOGV("Filtering out volume command on output %d for stream %d",
Eric Laurentde070132010-07-13 04:45:46 -07001195 data->mIO, data->mStream);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001196 removedCommands.add(command2);
Eric Laurent21e54562013-09-23 12:08:05 -07001197 command->mTime = command2->mTime;
1198 // force delayMs to non 0 so that code below does not request to wait for
1199 // command status as the command is now delayed
1200 delayMs = 1;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001201 } break;
Eric Laurente45b48a2014-09-04 16:40:57 -07001202
Eric Laurentbaf35fe2016-07-27 15:36:53 -07001203 case SET_VOICE_VOLUME: {
1204 VoiceVolumeData *data = (VoiceVolumeData *)command->mParam.get();
1205 VoiceVolumeData *data2 = (VoiceVolumeData *)command2->mParam.get();
1206 ALOGV("Filtering out voice volume command value %f replaced by %f",
1207 data2->mVolume, data->mVolume);
1208 removedCommands.add(command2);
1209 command->mTime = command2->mTime;
1210 // force delayMs to non 0 so that code below does not request to wait for
1211 // command status as the command is now delayed
1212 delayMs = 1;
1213 } break;
1214
Eric Laurente45b48a2014-09-04 16:40:57 -07001215 case CREATE_AUDIO_PATCH:
1216 case RELEASE_AUDIO_PATCH: {
1217 audio_patch_handle_t handle;
Haynes Mathew Georgea2d4a6d2014-10-13 13:05:22 -07001218 struct audio_patch patch;
Eric Laurente45b48a2014-09-04 16:40:57 -07001219 if (command->mCommand == CREATE_AUDIO_PATCH) {
1220 handle = ((CreateAudioPatchData *)command->mParam.get())->mHandle;
Haynes Mathew Georgea2d4a6d2014-10-13 13:05:22 -07001221 patch = ((CreateAudioPatchData *)command->mParam.get())->mPatch;
Eric Laurente45b48a2014-09-04 16:40:57 -07001222 } else {
1223 handle = ((ReleaseAudioPatchData *)command->mParam.get())->mHandle;
Mikhail Naganov7be71d22018-05-23 16:51:46 -07001224 memset(&patch, 0, sizeof(patch));
Eric Laurente45b48a2014-09-04 16:40:57 -07001225 }
1226 audio_patch_handle_t handle2;
Haynes Mathew Georgea2d4a6d2014-10-13 13:05:22 -07001227 struct audio_patch patch2;
Eric Laurente45b48a2014-09-04 16:40:57 -07001228 if (command2->mCommand == CREATE_AUDIO_PATCH) {
1229 handle2 = ((CreateAudioPatchData *)command2->mParam.get())->mHandle;
Haynes Mathew Georgea2d4a6d2014-10-13 13:05:22 -07001230 patch2 = ((CreateAudioPatchData *)command2->mParam.get())->mPatch;
Eric Laurente45b48a2014-09-04 16:40:57 -07001231 } else {
1232 handle2 = ((ReleaseAudioPatchData *)command2->mParam.get())->mHandle;
Glenn Kastenf60b6b62015-07-06 10:53:26 -07001233 memset(&patch2, 0, sizeof(patch2));
Eric Laurente45b48a2014-09-04 16:40:57 -07001234 }
1235 if (handle != handle2) break;
Haynes Mathew Georgea2d4a6d2014-10-13 13:05:22 -07001236 /* Filter CREATE_AUDIO_PATCH commands only when they are issued for
1237 same output. */
1238 if( (command->mCommand == CREATE_AUDIO_PATCH) &&
1239 (command2->mCommand == CREATE_AUDIO_PATCH) ) {
1240 bool isOutputDiff = false;
1241 if (patch.num_sources == patch2.num_sources) {
1242 for (unsigned count = 0; count < patch.num_sources; count++) {
1243 if (patch.sources[count].id != patch2.sources[count].id) {
1244 isOutputDiff = true;
1245 break;
1246 }
1247 }
1248 if (isOutputDiff)
1249 break;
1250 }
1251 }
Eric Laurente45b48a2014-09-04 16:40:57 -07001252 ALOGV("Filtering out %s audio patch command for handle %d",
1253 (command->mCommand == CREATE_AUDIO_PATCH) ? "create" : "release", handle);
1254 removedCommands.add(command2);
1255 command->mTime = command2->mTime;
1256 // force delayMs to non 0 so that code below does not request to wait for
1257 // command status as the command is now delayed
1258 delayMs = 1;
1259 } break;
1260
Jean-Michel Trivide801052015-04-14 19:10:14 -07001261 case DYN_POLICY_MIX_STATE_UPDATE: {
1262
1263 } break;
1264
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -08001265 case RECORDING_CONFIGURATION_UPDATE: {
1266
1267 } break;
1268
Mathias Agopian65ab4712010-07-14 17:59:35 -07001269 case START_TONE:
1270 case STOP_TONE:
1271 default:
1272 break;
1273 }
1274 }
1275
1276 // remove filtered commands
1277 for (size_t j = 0; j < removedCommands.size(); j++) {
1278 // removed commands always have time stamps greater than current command
1279 for (size_t k = i + 1; k < mAudioCommands.size(); k++) {
Eric Laurent0ede8922014-05-09 18:04:42 -07001280 if (mAudioCommands[k].get() == removedCommands[j].get()) {
Steve Block3856b092011-10-20 11:56:00 +01001281 ALOGV("suppressing command: %d", mAudioCommands[k]->mCommand);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001282 mAudioCommands.removeAt(k);
1283 break;
1284 }
1285 }
1286 }
1287 removedCommands.clear();
1288
Eric Laurentaa79bef2015-01-15 14:33:51 -08001289 // Disable wait for status if delay is not 0.
1290 // Except for create audio patch command because the returned patch handle
1291 // is needed by audio policy manager
1292 if (delayMs != 0 && command->mCommand != CREATE_AUDIO_PATCH) {
Eric Laurentcec4abb2012-07-03 12:23:02 -07001293 command->mWaitStatus = false;
1294 }
Eric Laurentcec4abb2012-07-03 12:23:02 -07001295
Mathias Agopian65ab4712010-07-14 17:59:35 -07001296 // insert command at the right place according to its time stamp
Eric Laurent1e693b52014-07-09 15:03:28 -07001297 ALOGV("inserting command: %d at index %zd, num commands %zu",
1298 command->mCommand, i+1, mAudioCommands.size());
Mathias Agopian65ab4712010-07-14 17:59:35 -07001299 mAudioCommands.insertAt(command, i + 1);
1300}
1301
1302void AudioPolicyService::AudioCommandThread::exit()
1303{
Steve Block3856b092011-10-20 11:56:00 +01001304 ALOGV("AudioCommandThread::exit");
Mathias Agopian65ab4712010-07-14 17:59:35 -07001305 {
1306 AutoMutex _l(mLock);
1307 requestExit();
1308 mWaitWorkCV.signal();
1309 }
Zach Janga754b4f2015-10-27 01:29:34 +00001310 // Note that we can call it from the thread loop if all other references have been released
1311 // but it will safely return WOULD_BLOCK in this case
Mathias Agopian65ab4712010-07-14 17:59:35 -07001312 requestExitAndWait();
1313}
1314
1315void AudioPolicyService::AudioCommandThread::AudioCommand::dump(char* buffer, size_t size)
1316{
1317 snprintf(buffer, size, " %02d %06d.%03d %01u %p\n",
1318 mCommand,
1319 (int)ns2s(mTime),
1320 (int)ns2ms(mTime)%1000,
1321 mWaitStatus,
Eric Laurent0ede8922014-05-09 18:04:42 -07001322 mParam.get());
Mathias Agopian65ab4712010-07-14 17:59:35 -07001323}
1324
Dima Zavinfce7a472011-04-19 22:30:36 -07001325/******* helpers for the service_ops callbacks defined below *********/
1326void AudioPolicyService::setParameters(audio_io_handle_t ioHandle,
1327 const char *keyValuePairs,
1328 int delayMs)
1329{
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001330 mAudioCommandThread->parametersCommand(ioHandle, keyValuePairs,
Dima Zavinfce7a472011-04-19 22:30:36 -07001331 delayMs);
1332}
1333
1334int AudioPolicyService::setStreamVolume(audio_stream_type_t stream,
1335 float volume,
1336 audio_io_handle_t output,
1337 int delayMs)
1338{
Glenn Kastenfff6d712012-01-12 16:38:12 -08001339 return (int)mAudioCommandThread->volumeCommand(stream, volume,
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001340 output, delayMs);
Dima Zavinfce7a472011-04-19 22:30:36 -07001341}
1342
1343int AudioPolicyService::startTone(audio_policy_tone_t tone,
1344 audio_stream_type_t stream)
1345{
Glenn Kasten6e2ebe92013-08-13 09:14:51 -07001346 if (tone != AUDIO_POLICY_TONE_IN_CALL_NOTIFICATION) {
Steve Block29357bc2012-01-06 19:20:56 +00001347 ALOGE("startTone: illegal tone requested (%d)", tone);
Glenn Kasten6e2ebe92013-08-13 09:14:51 -07001348 }
1349 if (stream != AUDIO_STREAM_VOICE_CALL) {
Steve Block29357bc2012-01-06 19:20:56 +00001350 ALOGE("startTone: illegal stream (%d) requested for tone %d", stream,
Glenn Kastene53b9ea2012-03-12 16:29:55 -07001351 tone);
Glenn Kasten6e2ebe92013-08-13 09:14:51 -07001352 }
Dima Zavinfce7a472011-04-19 22:30:36 -07001353 mTonePlaybackThread->startToneCommand(ToneGenerator::TONE_SUP_CALL_WAITING,
1354 AUDIO_STREAM_VOICE_CALL);
1355 return 0;
1356}
1357
1358int AudioPolicyService::stopTone()
1359{
1360 mTonePlaybackThread->stopToneCommand();
1361 return 0;
1362}
1363
1364int AudioPolicyService::setVoiceVolume(float volume, int delayMs)
1365{
1366 return (int)mAudioCommandThread->voiceVolumeCommand(volume, delayMs);
1367}
1368
Dima Zavinfce7a472011-04-19 22:30:36 -07001369extern "C" {
Eric Laurent2d388ec2014-03-07 13:25:54 -08001370audio_module_handle_t aps_load_hw_module(void *service __unused,
1371 const char *name);
1372audio_io_handle_t aps_open_output(void *service __unused,
Eric Laurenta4c5a552012-03-29 10:12:40 -07001373 audio_devices_t *pDevices,
1374 uint32_t *pSamplingRate,
1375 audio_format_t *pFormat,
1376 audio_channel_mask_t *pChannelMask,
1377 uint32_t *pLatencyMs,
Eric Laurent2d388ec2014-03-07 13:25:54 -08001378 audio_output_flags_t flags);
Eric Laurenta4c5a552012-03-29 10:12:40 -07001379
Eric Laurent2d388ec2014-03-07 13:25:54 -08001380audio_io_handle_t aps_open_output_on_module(void *service __unused,
Eric Laurenta4c5a552012-03-29 10:12:40 -07001381 audio_module_handle_t module,
1382 audio_devices_t *pDevices,
1383 uint32_t *pSamplingRate,
1384 audio_format_t *pFormat,
1385 audio_channel_mask_t *pChannelMask,
1386 uint32_t *pLatencyMs,
Richard Fitzgeraldad3af332013-03-25 16:54:37 +00001387 audio_output_flags_t flags,
Eric Laurent2d388ec2014-03-07 13:25:54 -08001388 const audio_offload_info_t *offloadInfo);
1389audio_io_handle_t aps_open_dup_output(void *service __unused,
Dima Zavinfce7a472011-04-19 22:30:36 -07001390 audio_io_handle_t output1,
Eric Laurent2d388ec2014-03-07 13:25:54 -08001391 audio_io_handle_t output2);
1392int aps_close_output(void *service __unused, audio_io_handle_t output);
1393int aps_suspend_output(void *service __unused, audio_io_handle_t output);
1394int aps_restore_output(void *service __unused, audio_io_handle_t output);
1395audio_io_handle_t aps_open_input(void *service __unused,
Eric Laurenta4c5a552012-03-29 10:12:40 -07001396 audio_devices_t *pDevices,
1397 uint32_t *pSamplingRate,
1398 audio_format_t *pFormat,
1399 audio_channel_mask_t *pChannelMask,
Eric Laurent2d388ec2014-03-07 13:25:54 -08001400 audio_in_acoustics_t acoustics __unused);
1401audio_io_handle_t aps_open_input_on_module(void *service __unused,
Eric Laurenta4c5a552012-03-29 10:12:40 -07001402 audio_module_handle_t module,
1403 audio_devices_t *pDevices,
1404 uint32_t *pSamplingRate,
1405 audio_format_t *pFormat,
Eric Laurent2d388ec2014-03-07 13:25:54 -08001406 audio_channel_mask_t *pChannelMask);
1407int aps_close_input(void *service __unused, audio_io_handle_t input);
1408int aps_invalidate_stream(void *service __unused, audio_stream_type_t stream);
Glenn Kastend848eb42016-03-08 13:42:11 -08001409int aps_move_effects(void *service __unused, audio_session_t session,
Dima Zavinfce7a472011-04-19 22:30:36 -07001410 audio_io_handle_t src_output,
Eric Laurent2d388ec2014-03-07 13:25:54 -08001411 audio_io_handle_t dst_output);
1412char * aps_get_parameters(void *service __unused, audio_io_handle_t io_handle,
1413 const char *keys);
1414void aps_set_parameters(void *service, audio_io_handle_t io_handle,
1415 const char *kv_pairs, int delay_ms);
1416int aps_set_stream_volume(void *service, audio_stream_type_t stream,
Dima Zavinfce7a472011-04-19 22:30:36 -07001417 float volume, audio_io_handle_t output,
Eric Laurent2d388ec2014-03-07 13:25:54 -08001418 int delay_ms);
1419int aps_start_tone(void *service, audio_policy_tone_t tone,
1420 audio_stream_type_t stream);
1421int aps_stop_tone(void *service);
1422int aps_set_voice_volume(void *service, float volume, int delay_ms);
1423};
Dima Zavinfce7a472011-04-19 22:30:36 -07001424
Mikhail Naganov1b2a7942017-12-08 10:18:09 -08001425} // namespace android