blob: 81ef2ac6625face45d0635d0ba22fe04d6226ca4 [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"
Glenn Kasten44deb052012-02-05 18:09:08 -080037#include "ServiceUtilities.h"
Mathias Agopian65ab4712010-07-14 17:59:35 -070038#include <hardware_legacy/power.h>
Eric Laurent7c7f10b2011-06-17 21:29:58 -070039#include <media/AudioEffect.h>
Chih-Hung Hsiehc84d9d22014-11-14 13:33:34 -080040#include <media/AudioParameter.h>
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
Svet Ganovf4ddfef2018-01-16 07:37:58 -080045#include <private/android_filesystem_config.h>
46
Mathias Agopian65ab4712010-07-14 17:59:35 -070047namespace android {
48
Glenn Kasten8dad0e32012-01-09 08:41:22 -080049static const char kDeadlockedString[] = "AudioPolicyService may be deadlocked\n";
50static const char kCmdDeadlockedString[] = "AudioPolicyService command thread may be deadlocked\n";
Mathias Agopian65ab4712010-07-14 17:59:35 -070051
52static const int kDumpLockRetries = 50;
Glenn Kasten22ecc912012-01-09 08:33:38 -080053static const int kDumpLockSleepUs = 20000;
Mathias Agopian65ab4712010-07-14 17:59:35 -070054
Eric Laurent0ede8922014-05-09 18:04:42 -070055static const nsecs_t kAudioCommandTimeoutNs = seconds(3); // 3 seconds
Christer Fletcher5fa8c4b2013-01-18 15:27:03 +010056
Svet Ganovf4ddfef2018-01-16 07:37:58 -080057static const String16 sManageAudioPolicyPermission("android.permission.MANAGE_AUDIO_POLICY");
Dima Zavinfce7a472011-04-19 22:30:36 -070058
Mathias Agopian65ab4712010-07-14 17:59:35 -070059// ----------------------------------------------------------------------------
60
61AudioPolicyService::AudioPolicyService()
Eric Laurentdce54a12014-03-10 12:19:46 -070062 : BnAudioPolicyService(), mpAudioPolicyDev(NULL), mpAudioPolicy(NULL),
Eric Laurentbb6c9a02014-09-25 14:11:47 -070063 mAudioPolicyManager(NULL), mAudioPolicyClient(NULL), mPhoneState(AUDIO_MODE_INVALID)
Mathias Agopian65ab4712010-07-14 17:59:35 -070064{
Eric Laurentf5ada6e2014-10-09 17:49:00 -070065}
66
67void AudioPolicyService::onFirstRef()
68{
Eric Laurent8b1e80b2014-10-07 09:08:47 -070069 {
70 Mutex::Autolock _l(mLock);
Eric Laurent93575202011-01-18 18:39:02 -080071
Eric Laurent8b1e80b2014-10-07 09:08:47 -070072 // start tone playback thread
73 mTonePlaybackThread = new AudioCommandThread(String8("ApmTone"), this);
74 // start audio commands thread
75 mAudioCommandThread = new AudioCommandThread(String8("ApmAudio"), this);
76 // start output activity command thread
77 mOutputCommandThread = new AudioCommandThread(String8("ApmOutput"), this);
Eric Laurentdce54a12014-03-10 12:19:46 -070078
Eric Laurent8b1e80b2014-10-07 09:08:47 -070079 mAudioPolicyClient = new AudioPolicyClient(this);
80 mAudioPolicyManager = createAudioPolicyManager(mAudioPolicyClient);
Eric Laurent8b1e80b2014-10-07 09:08:47 -070081 }
bryant_liuba2b4392014-06-11 16:49:30 +080082 // load audio processing modules
Eric Laurent8b1e80b2014-10-07 09:08:47 -070083 sp<AudioPolicyEffects>audioPolicyEffects = new AudioPolicyEffects();
84 {
85 Mutex::Autolock _l(mLock);
86 mAudioPolicyEffects = audioPolicyEffects;
87 }
Svet Ganovf4ddfef2018-01-16 07:37:58 -080088
89 mUidPolicy = new UidPolicy(this);
90 mUidPolicy->registerSelf();
Mathias Agopian65ab4712010-07-14 17:59:35 -070091}
92
93AudioPolicyService::~AudioPolicyService()
94{
95 mTonePlaybackThread->exit();
Mathias Agopian65ab4712010-07-14 17:59:35 -070096 mAudioCommandThread->exit();
Eric Laurent657ff612014-05-07 11:58:24 -070097 mOutputCommandThread->exit();
Eric Laurent7c7f10b2011-06-17 21:29:58 -070098
Eric Laurentf269b8e2014-06-09 20:01:29 -070099 destroyAudioPolicyManager(mAudioPolicyManager);
Eric Laurentdce54a12014-03-10 12:19:46 -0700100 delete mAudioPolicyClient;
Eric Laurentb52c1522014-05-20 11:27:36 -0700101
102 mNotificationClients.clear();
bryant_liuba2b4392014-06-11 16:49:30 +0800103 mAudioPolicyEffects.clear();
Svet Ganovf4ddfef2018-01-16 07:37:58 -0800104
105 mUidPolicy->unregisterSelf();
106 mUidPolicy.clear();
Eric Laurentb52c1522014-05-20 11:27:36 -0700107}
108
109// A notification client is always registered by AudioSystem when the client process
110// connects to AudioPolicyService.
111void AudioPolicyService::registerClient(const sp<IAudioPolicyServiceClient>& client)
112{
Eric Laurent12590252015-08-21 18:40:20 -0700113 if (client == 0) {
114 ALOGW("%s got NULL client", __FUNCTION__);
115 return;
116 }
Eric Laurent0ebd5f92014-11-19 19:04:52 -0800117 Mutex::Autolock _l(mNotificationClientsLock);
Eric Laurentb52c1522014-05-20 11:27:36 -0700118
119 uid_t uid = IPCThreadState::self()->getCallingUid();
120 if (mNotificationClients.indexOfKey(uid) < 0) {
121 sp<NotificationClient> notificationClient = new NotificationClient(this,
122 client,
123 uid);
124 ALOGV("registerClient() client %p, uid %d", client.get(), uid);
125
126 mNotificationClients.add(uid, notificationClient);
127
Marco Nelissenf8880202014-11-14 07:58:25 -0800128 sp<IBinder> binder = IInterface::asBinder(client);
Eric Laurentb52c1522014-05-20 11:27:36 -0700129 binder->linkToDeath(notificationClient);
130 }
131}
132
Eric Laurente8726fe2015-06-26 09:39:24 -0700133void AudioPolicyService::setAudioPortCallbacksEnabled(bool enabled)
134{
135 Mutex::Autolock _l(mNotificationClientsLock);
136
137 uid_t uid = IPCThreadState::self()->getCallingUid();
138 if (mNotificationClients.indexOfKey(uid) < 0) {
139 return;
140 }
141 mNotificationClients.valueFor(uid)->setAudioPortCallbacksEnabled(enabled);
142}
143
Eric Laurentb52c1522014-05-20 11:27:36 -0700144// removeNotificationClient() is called when the client process dies.
145void AudioPolicyService::removeNotificationClient(uid_t uid)
146{
Eric Laurent0ebd5f92014-11-19 19:04:52 -0800147 {
148 Mutex::Autolock _l(mNotificationClientsLock);
149 mNotificationClients.removeItem(uid);
150 }
Eric Laurent0ebd5f92014-11-19 19:04:52 -0800151 {
152 Mutex::Autolock _l(mLock);
Eric Laurentb52c1522014-05-20 11:27:36 -0700153 if (mAudioPolicyManager) {
Eric Laurent8c7e6da2015-04-21 17:37:00 -0700154 mAudioPolicyManager->releaseResourcesForUid(uid);
Eric Laurentb52c1522014-05-20 11:27:36 -0700155 }
Eric Laurent0ebd5f92014-11-19 19:04:52 -0800156 }
Eric Laurentb52c1522014-05-20 11:27:36 -0700157}
158
159void AudioPolicyService::onAudioPortListUpdate()
160{
161 mOutputCommandThread->updateAudioPortListCommand();
162}
163
164void AudioPolicyService::doOnAudioPortListUpdate()
165{
Eric Laurent0ebd5f92014-11-19 19:04:52 -0800166 Mutex::Autolock _l(mNotificationClientsLock);
Eric Laurentb52c1522014-05-20 11:27:36 -0700167 for (size_t i = 0; i < mNotificationClients.size(); i++) {
168 mNotificationClients.valueAt(i)->onAudioPortListUpdate();
169 }
170}
171
172void AudioPolicyService::onAudioPatchListUpdate()
173{
174 mOutputCommandThread->updateAudioPatchListCommand();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700175}
176
Eric Laurentb52c1522014-05-20 11:27:36 -0700177void AudioPolicyService::doOnAudioPatchListUpdate()
178{
Eric Laurent0ebd5f92014-11-19 19:04:52 -0800179 Mutex::Autolock _l(mNotificationClientsLock);
Eric Laurentb52c1522014-05-20 11:27:36 -0700180 for (size_t i = 0; i < mNotificationClients.size(); i++) {
181 mNotificationClients.valueAt(i)->onAudioPatchListUpdate();
182 }
183}
184
Chih-Hung Hsiehe964d4e2016-08-09 14:31:32 -0700185void AudioPolicyService::onDynamicPolicyMixStateUpdate(const String8& regId, int32_t state)
Jean-Michel Trivide801052015-04-14 19:10:14 -0700186{
187 ALOGV("AudioPolicyService::onDynamicPolicyMixStateUpdate(%s, %d)",
188 regId.string(), state);
189 mOutputCommandThread->dynamicPolicyMixStateUpdateCommand(regId, state);
190}
191
Chih-Hung Hsiehe964d4e2016-08-09 14:31:32 -0700192void AudioPolicyService::doOnDynamicPolicyMixStateUpdate(const String8& regId, int32_t state)
Jean-Michel Trivide801052015-04-14 19:10:14 -0700193{
194 Mutex::Autolock _l(mNotificationClientsLock);
195 for (size_t i = 0; i < mNotificationClients.size(); i++) {
196 mNotificationClients.valueAt(i)->onDynamicPolicyMixStateUpdate(regId, state);
197 }
198}
199
Jean-Michel Triviac4e4292016-12-22 11:39:31 -0800200void AudioPolicyService::onRecordingConfigurationUpdate(int event,
201 const record_client_info_t *clientInfo, const audio_config_base_t *clientConfig,
Jean-Michel Trivi8c7cf3b2016-02-25 17:08:24 -0800202 const audio_config_base_t *deviceConfig, audio_patch_handle_t patchHandle)
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -0800203{
Jean-Michel Triviac4e4292016-12-22 11:39:31 -0800204 mOutputCommandThread->recordingConfigurationUpdateCommand(event, clientInfo,
Jean-Michel Trivi8c7cf3b2016-02-25 17:08:24 -0800205 clientConfig, deviceConfig, patchHandle);
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -0800206}
207
Jean-Michel Triviac4e4292016-12-22 11:39:31 -0800208void AudioPolicyService::doOnRecordingConfigurationUpdate(int event,
209 const record_client_info_t *clientInfo, const audio_config_base_t *clientConfig,
Jean-Michel Trivi8c7cf3b2016-02-25 17:08:24 -0800210 const audio_config_base_t *deviceConfig, audio_patch_handle_t patchHandle)
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -0800211{
212 Mutex::Autolock _l(mNotificationClientsLock);
213 for (size_t i = 0; i < mNotificationClients.size(); i++) {
Jean-Michel Triviac4e4292016-12-22 11:39:31 -0800214 mNotificationClients.valueAt(i)->onRecordingConfigurationUpdate(event, clientInfo,
Jean-Michel Trivi8c7cf3b2016-02-25 17:08:24 -0800215 clientConfig, deviceConfig, patchHandle);
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -0800216 }
217}
218
219status_t AudioPolicyService::clientCreateAudioPatch(const struct audio_patch *patch,
220 audio_patch_handle_t *handle,
221 int delayMs)
222{
223 return mAudioCommandThread->createAudioPatchCommand(patch, handle, delayMs);
224}
225
226status_t AudioPolicyService::clientReleaseAudioPatch(audio_patch_handle_t handle,
227 int delayMs)
228{
229 return mAudioCommandThread->releaseAudioPatchCommand(handle, delayMs);
230}
231
Eric Laurente1715a42014-05-20 11:30:42 -0700232status_t AudioPolicyService::clientSetAudioPortConfig(const struct audio_port_config *config,
233 int delayMs)
234{
235 return mAudioCommandThread->setAudioPortConfigCommand(config, delayMs);
236}
237
Eric Laurentb52c1522014-05-20 11:27:36 -0700238AudioPolicyService::NotificationClient::NotificationClient(const sp<AudioPolicyService>& service,
239 const sp<IAudioPolicyServiceClient>& client,
240 uid_t uid)
Eric Laurente8726fe2015-06-26 09:39:24 -0700241 : mService(service), mUid(uid), mAudioPolicyServiceClient(client),
242 mAudioPortCallbacksEnabled(false)
Eric Laurentb52c1522014-05-20 11:27:36 -0700243{
244}
245
246AudioPolicyService::NotificationClient::~NotificationClient()
247{
248}
249
250void AudioPolicyService::NotificationClient::binderDied(const wp<IBinder>& who __unused)
251{
252 sp<NotificationClient> keep(this);
253 sp<AudioPolicyService> service = mService.promote();
254 if (service != 0) {
255 service->removeNotificationClient(mUid);
256 }
257}
258
259void AudioPolicyService::NotificationClient::onAudioPortListUpdate()
260{
Eric Laurente8726fe2015-06-26 09:39:24 -0700261 if (mAudioPolicyServiceClient != 0 && mAudioPortCallbacksEnabled) {
Eric Laurentb52c1522014-05-20 11:27:36 -0700262 mAudioPolicyServiceClient->onAudioPortListUpdate();
263 }
264}
265
266void AudioPolicyService::NotificationClient::onAudioPatchListUpdate()
267{
Eric Laurente8726fe2015-06-26 09:39:24 -0700268 if (mAudioPolicyServiceClient != 0 && mAudioPortCallbacksEnabled) {
Eric Laurentb52c1522014-05-20 11:27:36 -0700269 mAudioPolicyServiceClient->onAudioPatchListUpdate();
270 }
271}
Eric Laurent57dae992011-07-24 13:36:09 -0700272
Jean-Michel Trivide801052015-04-14 19:10:14 -0700273void AudioPolicyService::NotificationClient::onDynamicPolicyMixStateUpdate(
Chih-Hung Hsiehe964d4e2016-08-09 14:31:32 -0700274 const String8& regId, int32_t state)
Jean-Michel Trivide801052015-04-14 19:10:14 -0700275{
Eric Laurent96c7eed2018-03-26 17:57:01 -0700276 if (mAudioPolicyServiceClient != 0 && (mUid % AID_USER_OFFSET) < AID_APP_START) {
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -0800277 mAudioPolicyServiceClient->onDynamicPolicyMixStateUpdate(regId, state);
278 }
279}
280
281void AudioPolicyService::NotificationClient::onRecordingConfigurationUpdate(
Jean-Michel Triviac4e4292016-12-22 11:39:31 -0800282 int event, const record_client_info_t *clientInfo,
Jean-Michel Trivi8c7cf3b2016-02-25 17:08:24 -0800283 const audio_config_base_t *clientConfig, const audio_config_base_t *deviceConfig,
284 audio_patch_handle_t patchHandle)
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -0800285{
Eric Laurent96c7eed2018-03-26 17:57:01 -0700286 if (mAudioPolicyServiceClient != 0 && (mUid % AID_USER_OFFSET) < AID_APP_START) {
Jean-Michel Triviac4e4292016-12-22 11:39:31 -0800287 mAudioPolicyServiceClient->onRecordingConfigurationUpdate(event, clientInfo,
Jean-Michel Trivi8c7cf3b2016-02-25 17:08:24 -0800288 clientConfig, deviceConfig, patchHandle);
Jean-Michel Trivide801052015-04-14 19:10:14 -0700289 }
290}
291
Eric Laurente8726fe2015-06-26 09:39:24 -0700292void AudioPolicyService::NotificationClient::setAudioPortCallbacksEnabled(bool enabled)
293{
294 mAudioPortCallbacksEnabled = enabled;
295}
296
297
Mathias Agopian65ab4712010-07-14 17:59:35 -0700298void AudioPolicyService::binderDied(const wp<IBinder>& who) {
Glenn Kasten411e4472012-11-02 10:00:06 -0700299 ALOGW("binderDied() %p, calling pid %d", who.unsafe_get(),
Eric Laurentde070132010-07-13 04:45:46 -0700300 IPCThreadState::self()->getCallingPid());
Mathias Agopian65ab4712010-07-14 17:59:35 -0700301}
302
303static bool tryLock(Mutex& mutex)
304{
305 bool locked = false;
306 for (int i = 0; i < kDumpLockRetries; ++i) {
307 if (mutex.tryLock() == NO_ERROR) {
308 locked = true;
309 break;
310 }
Glenn Kasten22ecc912012-01-09 08:33:38 -0800311 usleep(kDumpLockSleepUs);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700312 }
313 return locked;
314}
315
316status_t AudioPolicyService::dumpInternals(int fd)
317{
318 const size_t SIZE = 256;
319 char buffer[SIZE];
320 String8 result;
321
Eric Laurentdce54a12014-03-10 12:19:46 -0700322 snprintf(buffer, SIZE, "AudioPolicyManager: %p\n", mAudioPolicyManager);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700323 result.append(buffer);
324 snprintf(buffer, SIZE, "Command Thread: %p\n", mAudioCommandThread.get());
325 result.append(buffer);
326 snprintf(buffer, SIZE, "Tones Thread: %p\n", mTonePlaybackThread.get());
327 result.append(buffer);
328
329 write(fd, result.string(), result.size());
330 return NO_ERROR;
331}
332
Svet Ganovf4ddfef2018-01-16 07:37:58 -0800333void AudioPolicyService::setRecordSilenced(uid_t uid, bool silenced)
334{
Eric Laurent2af67732018-03-30 18:17:20 -0700335// FIXME: temporarily disable while investigating issue b/77300296
336// {
337// Mutex::Autolock _l(mLock);
338// if (mAudioPolicyManager) {
339// mAudioPolicyManager->setRecordSilenced(uid, silenced);
340// }
341// }
342// sp<IAudioFlinger> af = AudioSystem::get_audio_flinger();
343// if (af) {
344// af->setRecordSilenced(uid, silenced);
345// }
346 (void)uid;
347 (void)silenced;
Svet Ganovf4ddfef2018-01-16 07:37:58 -0800348}
349
Glenn Kasten0f11b512014-01-31 16:18:54 -0800350status_t AudioPolicyService::dump(int fd, const Vector<String16>& args __unused)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700351{
Glenn Kasten44deb052012-02-05 18:09:08 -0800352 if (!dumpAllowed()) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700353 dumpPermissionDenial(fd);
354 } else {
355 bool locked = tryLock(mLock);
356 if (!locked) {
357 String8 result(kDeadlockedString);
358 write(fd, result.string(), result.size());
359 }
360
361 dumpInternals(fd);
Glenn Kasten9d1f02d2012-02-08 17:47:58 -0800362 if (mAudioCommandThread != 0) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700363 mAudioCommandThread->dump(fd);
364 }
Glenn Kasten9d1f02d2012-02-08 17:47:58 -0800365 if (mTonePlaybackThread != 0) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700366 mTonePlaybackThread->dump(fd);
367 }
368
Eric Laurentdce54a12014-03-10 12:19:46 -0700369 if (mAudioPolicyManager) {
370 mAudioPolicyManager->dump(fd);
371 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700372
373 if (locked) mLock.unlock();
374 }
375 return NO_ERROR;
376}
377
378status_t AudioPolicyService::dumpPermissionDenial(int fd)
379{
380 const size_t SIZE = 256;
381 char buffer[SIZE];
382 String8 result;
383 snprintf(buffer, SIZE, "Permission Denial: "
384 "can't dump AudioPolicyService from pid=%d, uid=%d\n",
385 IPCThreadState::self()->getCallingPid(),
386 IPCThreadState::self()->getCallingUid());
387 result.append(buffer);
388 write(fd, result.string(), result.size());
389 return NO_ERROR;
390}
391
392status_t AudioPolicyService::onTransact(
Svet Ganovf4ddfef2018-01-16 07:37:58 -0800393 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags) {
394 switch (code) {
395 case SHELL_COMMAND_TRANSACTION: {
396 int in = data.readFileDescriptor();
397 int out = data.readFileDescriptor();
398 int err = data.readFileDescriptor();
399 int argc = data.readInt32();
400 Vector<String16> args;
401 for (int i = 0; i < argc && data.dataAvail() > 0; i++) {
402 args.add(data.readString16());
403 }
404 sp<IBinder> unusedCallback;
405 sp<IResultReceiver> resultReceiver;
406 status_t status;
407 if ((status = data.readNullableStrongBinder(&unusedCallback)) != NO_ERROR) {
408 return status;
409 }
410 if ((status = data.readNullableStrongBinder(&resultReceiver)) != NO_ERROR) {
411 return status;
412 }
413 status = shellCommand(in, out, err, args);
414 if (resultReceiver != nullptr) {
415 resultReceiver->send(status);
416 }
417 return NO_ERROR;
418 }
419 }
420
Mathias Agopian65ab4712010-07-14 17:59:35 -0700421 return BnAudioPolicyService::onTransact(code, data, reply, flags);
422}
423
Svet Ganovf4ddfef2018-01-16 07:37:58 -0800424// ------------------- Shell command implementation -------------------
425
426// NOTE: This is a remote API - make sure all args are validated
427status_t AudioPolicyService::shellCommand(int in, int out, int err, Vector<String16>& args) {
428 if (!checkCallingPermission(sManageAudioPolicyPermission, nullptr, nullptr)) {
429 return PERMISSION_DENIED;
430 }
431 if (in == BAD_TYPE || out == BAD_TYPE || err == BAD_TYPE) {
432 return BAD_VALUE;
433 }
434 if (args.size() == 3 && args[0] == String16("set-uid-state")) {
435 return handleSetUidState(args, err);
436 } else if (args.size() == 2 && args[0] == String16("reset-uid-state")) {
437 return handleResetUidState(args, err);
438 } else if (args.size() == 2 && args[0] == String16("get-uid-state")) {
439 return handleGetUidState(args, out, err);
440 } else if (args.size() == 1 && args[0] == String16("help")) {
441 printHelp(out);
442 return NO_ERROR;
443 }
444 printHelp(err);
445 return BAD_VALUE;
446}
447
448status_t AudioPolicyService::handleSetUidState(Vector<String16>& args, int err) {
449 PermissionController pc;
450 int uid = pc.getPackageUid(args[1], 0);
451 if (uid <= 0) {
452 ALOGE("Unknown package: '%s'", String8(args[1]).string());
453 dprintf(err, "Unknown package: '%s'\n", String8(args[1]).string());
454 return BAD_VALUE;
455 }
456 bool active = false;
457 if (args[2] == String16("active")) {
458 active = true;
459 } else if ((args[2] != String16("idle"))) {
460 ALOGE("Expected active or idle but got: '%s'", String8(args[2]).string());
461 return BAD_VALUE;
462 }
463 mUidPolicy->addOverrideUid(uid, active);
464 return NO_ERROR;
465}
466
467status_t AudioPolicyService::handleResetUidState(Vector<String16>& args, int err) {
468 PermissionController pc;
469 int uid = pc.getPackageUid(args[1], 0);
470 if (uid < 0) {
471 ALOGE("Unknown package: '%s'", String8(args[1]).string());
472 dprintf(err, "Unknown package: '%s'\n", String8(args[1]).string());
473 return BAD_VALUE;
474 }
475 mUidPolicy->removeOverrideUid(uid);
476 return NO_ERROR;
477}
478
479status_t AudioPolicyService::handleGetUidState(Vector<String16>& args, int out, int err) {
480 PermissionController pc;
481 int uid = pc.getPackageUid(args[1], 0);
482 if (uid < 0) {
483 ALOGE("Unknown package: '%s'", String8(args[1]).string());
484 dprintf(err, "Unknown package: '%s'\n", String8(args[1]).string());
485 return BAD_VALUE;
486 }
487 if (mUidPolicy->isUidActive(uid)) {
488 return dprintf(out, "active\n");
489 } else {
490 return dprintf(out, "idle\n");
491 }
492}
493
494status_t AudioPolicyService::printHelp(int out) {
495 return dprintf(out, "Audio policy service commands:\n"
496 " get-uid-state <PACKAGE> gets the uid state\n"
497 " set-uid-state <PACKAGE> <active|idle> overrides the uid state\n"
498 " reset-uid-state <PACKAGE> clears the uid state override\n"
499 " help print this message\n");
500}
501
502// ----------- AudioPolicyService::UidPolicy implementation ----------
503
504void AudioPolicyService::UidPolicy::registerSelf() {
505 ActivityManager am;
506 am.registerUidObserver(this, ActivityManager::UID_OBSERVER_GONE
507 | ActivityManager::UID_OBSERVER_IDLE
508 | ActivityManager::UID_OBSERVER_ACTIVE,
509 ActivityManager::PROCESS_STATE_UNKNOWN,
510 String16("audioserver"));
511}
512
513void AudioPolicyService::UidPolicy::unregisterSelf() {
514 ActivityManager am;
515 am.unregisterUidObserver(this);
516}
517
518void AudioPolicyService::UidPolicy::onUidGone(uid_t uid, __unused bool disabled) {
519 onUidIdle(uid, disabled);
520}
521
522void AudioPolicyService::UidPolicy::onUidActive(uid_t uid) {
523 {
524 Mutex::Autolock _l(mUidLock);
525 mActiveUids.insert(uid);
526 }
527 sp<AudioPolicyService> service = mService.promote();
528 if (service != nullptr) {
529 service->setRecordSilenced(uid, false);
530 }
531}
532
533void AudioPolicyService::UidPolicy::onUidIdle(uid_t uid, __unused bool disabled) {
534 bool deleted = false;
535 {
536 Mutex::Autolock _l(mUidLock);
537 if (mActiveUids.erase(uid) > 0) {
538 deleted = true;
539 }
540 }
541 if (deleted) {
542 sp<AudioPolicyService> service = mService.promote();
543 if (service != nullptr) {
544 service->setRecordSilenced(uid, true);
545 }
546 }
547}
548
549void AudioPolicyService::UidPolicy::addOverrideUid(uid_t uid, bool active) {
550 updateOverrideUid(uid, active, true);
551}
552
553void AudioPolicyService::UidPolicy::removeOverrideUid(uid_t uid) {
554 updateOverrideUid(uid, false, false);
555}
556
557void AudioPolicyService::UidPolicy::updateOverrideUid(uid_t uid, bool active, bool insert) {
558 bool wasActive = false;
559 bool isActive = false;
560 {
561 Mutex::Autolock _l(mUidLock);
562 wasActive = isUidActiveLocked(uid);
563 mOverrideUids.erase(uid);
564 if (insert) {
565 mOverrideUids.insert(std::pair<uid_t, bool>(uid, active));
566 }
567 isActive = isUidActiveLocked(uid);
568 }
569 if (wasActive != isActive) {
570 sp<AudioPolicyService> service = mService.promote();
571 if (service != nullptr) {
572 service->setRecordSilenced(uid, !isActive);
573 }
574 }
575}
576
577bool AudioPolicyService::UidPolicy::isUidActive(uid_t uid) {
578 // Non-app UIDs are considered always active
579 if (uid < FIRST_APPLICATION_UID) {
580 return true;
581 }
582 Mutex::Autolock _l(mUidLock);
583 return isUidActiveLocked(uid);
584}
585
586bool AudioPolicyService::UidPolicy::isUidActiveLocked(uid_t uid) {
587 // Non-app UIDs are considered always active
Eric Laurent2af67732018-03-30 18:17:20 -0700588// FIXME: temporarily disable while investigating issue b/77300296
589// if (uid < FIRST_APPLICATION_UID) {
590// return true;
591// }
592// auto it = mOverrideUids.find(uid);
593// if (it != mOverrideUids.end()) {
594// return it->second;
595// }
596// return mActiveUids.find(uid) != mActiveUids.end();
597 (void)uid;
598 return true;
Svet Ganovf4ddfef2018-01-16 07:37:58 -0800599}
Mathias Agopian65ab4712010-07-14 17:59:35 -0700600
Mathias Agopian65ab4712010-07-14 17:59:35 -0700601// ----------- AudioPolicyService::AudioCommandThread implementation ----------
602
Eric Laurentbfb1b832013-01-07 09:53:42 -0800603AudioPolicyService::AudioCommandThread::AudioCommandThread(String8 name,
604 const wp<AudioPolicyService>& service)
605 : Thread(false), mName(name), mService(service)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700606{
607 mpToneGenerator = NULL;
608}
609
610
611AudioPolicyService::AudioCommandThread::~AudioCommandThread()
612{
Eric Laurentbfb1b832013-01-07 09:53:42 -0800613 if (!mAudioCommands.isEmpty()) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700614 release_wake_lock(mName.string());
615 }
616 mAudioCommands.clear();
Glenn Kastene9dd0172012-01-27 18:08:45 -0800617 delete mpToneGenerator;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700618}
619
620void AudioPolicyService::AudioCommandThread::onFirstRef()
621{
Eric Laurentbfb1b832013-01-07 09:53:42 -0800622 run(mName.string(), ANDROID_PRIORITY_AUDIO);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700623}
624
625bool AudioPolicyService::AudioCommandThread::threadLoop()
626{
Eric Laurentd7eda8d2016-02-02 17:18:39 -0800627 nsecs_t waitTime = -1;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700628
629 mLock.lock();
630 while (!exitPending())
631 {
Eric Laurent59a89232014-06-08 14:14:17 -0700632 sp<AudioPolicyService> svc;
633 while (!mAudioCommands.isEmpty() && !exitPending()) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700634 nsecs_t curTime = systemTime();
635 // commands are sorted by increasing time stamp: execute them from index 0 and up
636 if (mAudioCommands[0]->mTime <= curTime) {
Eric Laurent0ede8922014-05-09 18:04:42 -0700637 sp<AudioCommand> command = mAudioCommands[0];
Mathias Agopian65ab4712010-07-14 17:59:35 -0700638 mAudioCommands.removeAt(0);
Eric Laurent0ede8922014-05-09 18:04:42 -0700639 mLastCommand = command;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700640
641 switch (command->mCommand) {
642 case START_TONE: {
643 mLock.unlock();
Eric Laurent0ede8922014-05-09 18:04:42 -0700644 ToneData *data = (ToneData *)command->mParam.get();
Steve Block3856b092011-10-20 11:56:00 +0100645 ALOGV("AudioCommandThread() processing start tone %d on stream %d",
Mathias Agopian65ab4712010-07-14 17:59:35 -0700646 data->mType, data->mStream);
Glenn Kastene9dd0172012-01-27 18:08:45 -0800647 delete mpToneGenerator;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700648 mpToneGenerator = new ToneGenerator(data->mStream, 1.0);
649 mpToneGenerator->startTone(data->mType);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700650 mLock.lock();
651 }break;
652 case STOP_TONE: {
653 mLock.unlock();
Steve Block3856b092011-10-20 11:56:00 +0100654 ALOGV("AudioCommandThread() processing stop tone");
Mathias Agopian65ab4712010-07-14 17:59:35 -0700655 if (mpToneGenerator != NULL) {
656 mpToneGenerator->stopTone();
657 delete mpToneGenerator;
658 mpToneGenerator = NULL;
659 }
660 mLock.lock();
661 }break;
662 case SET_VOLUME: {
Eric Laurent0ede8922014-05-09 18:04:42 -0700663 VolumeData *data = (VolumeData *)command->mParam.get();
Steve Block3856b092011-10-20 11:56:00 +0100664 ALOGV("AudioCommandThread() processing set volume stream %d, \
Eric Laurentde070132010-07-13 04:45:46 -0700665 volume %f, output %d", data->mStream, data->mVolume, data->mIO);
666 command->mStatus = AudioSystem::setStreamVolume(data->mStream,
667 data->mVolume,
668 data->mIO);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700669 }break;
670 case SET_PARAMETERS: {
Eric Laurent0ede8922014-05-09 18:04:42 -0700671 ParametersData *data = (ParametersData *)command->mParam.get();
Glenn Kastene53b9ea2012-03-12 16:29:55 -0700672 ALOGV("AudioCommandThread() processing set parameters string %s, io %d",
673 data->mKeyValuePairs.string(), data->mIO);
674 command->mStatus = AudioSystem::setParameters(data->mIO, data->mKeyValuePairs);
Glenn Kastene53b9ea2012-03-12 16:29:55 -0700675 }break;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700676 case SET_VOICE_VOLUME: {
Eric Laurent0ede8922014-05-09 18:04:42 -0700677 VoiceVolumeData *data = (VoiceVolumeData *)command->mParam.get();
Steve Block3856b092011-10-20 11:56:00 +0100678 ALOGV("AudioCommandThread() processing set voice volume volume %f",
Eric Laurentde070132010-07-13 04:45:46 -0700679 data->mVolume);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700680 command->mStatus = AudioSystem::setVoiceVolume(data->mVolume);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700681 }break;
Eric Laurentbfb1b832013-01-07 09:53:42 -0800682 case STOP_OUTPUT: {
Eric Laurent0ede8922014-05-09 18:04:42 -0700683 StopOutputData *data = (StopOutputData *)command->mParam.get();
Eric Laurentbfb1b832013-01-07 09:53:42 -0800684 ALOGV("AudioCommandThread() processing stop output %d",
685 data->mIO);
Eric Laurent59a89232014-06-08 14:14:17 -0700686 svc = mService.promote();
Eric Laurentbfb1b832013-01-07 09:53:42 -0800687 if (svc == 0) {
688 break;
689 }
690 mLock.unlock();
691 svc->doStopOutput(data->mIO, data->mStream, data->mSession);
692 mLock.lock();
Eric Laurentbfb1b832013-01-07 09:53:42 -0800693 }break;
694 case RELEASE_OUTPUT: {
Eric Laurent0ede8922014-05-09 18:04:42 -0700695 ReleaseOutputData *data = (ReleaseOutputData *)command->mParam.get();
Eric Laurentbfb1b832013-01-07 09:53:42 -0800696 ALOGV("AudioCommandThread() processing release output %d",
697 data->mIO);
Eric Laurent59a89232014-06-08 14:14:17 -0700698 svc = mService.promote();
Eric Laurentbfb1b832013-01-07 09:53:42 -0800699 if (svc == 0) {
700 break;
701 }
702 mLock.unlock();
Eric Laurente83b55d2014-11-14 10:06:21 -0800703 svc->doReleaseOutput(data->mIO, data->mStream, data->mSession);
Eric Laurentbfb1b832013-01-07 09:53:42 -0800704 mLock.lock();
Eric Laurentbfb1b832013-01-07 09:53:42 -0800705 }break;
Eric Laurent951f4552014-05-20 10:48:17 -0700706 case CREATE_AUDIO_PATCH: {
707 CreateAudioPatchData *data = (CreateAudioPatchData *)command->mParam.get();
708 ALOGV("AudioCommandThread() processing create audio patch");
709 sp<IAudioFlinger> af = AudioSystem::get_audio_flinger();
710 if (af == 0) {
711 command->mStatus = PERMISSION_DENIED;
712 } else {
713 command->mStatus = af->createAudioPatch(&data->mPatch, &data->mHandle);
714 }
715 } break;
716 case RELEASE_AUDIO_PATCH: {
717 ReleaseAudioPatchData *data = (ReleaseAudioPatchData *)command->mParam.get();
718 ALOGV("AudioCommandThread() processing release audio patch");
719 sp<IAudioFlinger> af = AudioSystem::get_audio_flinger();
720 if (af == 0) {
721 command->mStatus = PERMISSION_DENIED;
722 } else {
723 command->mStatus = af->releaseAudioPatch(data->mHandle);
724 }
725 } break;
Eric Laurentb52c1522014-05-20 11:27:36 -0700726 case UPDATE_AUDIOPORT_LIST: {
727 ALOGV("AudioCommandThread() processing update audio port list");
Eric Laurent59a89232014-06-08 14:14:17 -0700728 svc = mService.promote();
Eric Laurentb52c1522014-05-20 11:27:36 -0700729 if (svc == 0) {
730 break;
731 }
732 mLock.unlock();
733 svc->doOnAudioPortListUpdate();
734 mLock.lock();
735 }break;
736 case UPDATE_AUDIOPATCH_LIST: {
737 ALOGV("AudioCommandThread() processing update audio patch list");
Eric Laurent59a89232014-06-08 14:14:17 -0700738 svc = mService.promote();
Eric Laurentb52c1522014-05-20 11:27:36 -0700739 if (svc == 0) {
740 break;
741 }
742 mLock.unlock();
743 svc->doOnAudioPatchListUpdate();
744 mLock.lock();
745 }break;
Eric Laurente1715a42014-05-20 11:30:42 -0700746 case SET_AUDIOPORT_CONFIG: {
747 SetAudioPortConfigData *data = (SetAudioPortConfigData *)command->mParam.get();
748 ALOGV("AudioCommandThread() processing set port config");
749 sp<IAudioFlinger> af = AudioSystem::get_audio_flinger();
750 if (af == 0) {
751 command->mStatus = PERMISSION_DENIED;
752 } else {
753 command->mStatus = af->setAudioPortConfig(&data->mConfig);
754 }
755 } break;
Jean-Michel Trivide801052015-04-14 19:10:14 -0700756 case DYN_POLICY_MIX_STATE_UPDATE: {
757 DynPolicyMixStateUpdateData *data =
758 (DynPolicyMixStateUpdateData *)command->mParam.get();
Jean-Michel Trivide801052015-04-14 19:10:14 -0700759 ALOGV("AudioCommandThread() processing dyn policy mix state update %s %d",
760 data->mRegId.string(), data->mState);
761 svc = mService.promote();
762 if (svc == 0) {
763 break;
764 }
765 mLock.unlock();
766 svc->doOnDynamicPolicyMixStateUpdate(data->mRegId, data->mState);
767 mLock.lock();
768 } break;
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -0800769 case RECORDING_CONFIGURATION_UPDATE: {
770 RecordingConfigurationUpdateData *data =
771 (RecordingConfigurationUpdateData *)command->mParam.get();
772 ALOGV("AudioCommandThread() processing recording configuration update");
773 svc = mService.promote();
774 if (svc == 0) {
775 break;
776 }
777 mLock.unlock();
Jean-Michel Triviac4e4292016-12-22 11:39:31 -0800778 svc->doOnRecordingConfigurationUpdate(data->mEvent, &data->mClientInfo,
779 &data->mClientConfig, &data->mDeviceConfig,
Jean-Michel Trivi8c7cf3b2016-02-25 17:08:24 -0800780 data->mPatchHandle);
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -0800781 mLock.lock();
782 } break;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700783 default:
Steve Block5ff1dd52012-01-05 23:22:43 +0000784 ALOGW("AudioCommandThread() unknown command %d", command->mCommand);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700785 }
Eric Laurent0ede8922014-05-09 18:04:42 -0700786 {
787 Mutex::Autolock _l(command->mLock);
788 if (command->mWaitStatus) {
789 command->mWaitStatus = false;
790 command->mCond.signal();
791 }
792 }
Eric Laurentd7eda8d2016-02-02 17:18:39 -0800793 waitTime = -1;
Zach Janga754b4f2015-10-27 01:29:34 +0000794 // release mLock before releasing strong reference on the service as
795 // AudioPolicyService destructor calls AudioCommandThread::exit() which
796 // acquires mLock.
797 mLock.unlock();
798 svc.clear();
799 mLock.lock();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700800 } else {
801 waitTime = mAudioCommands[0]->mTime - curTime;
802 break;
803 }
804 }
Zach Janga754b4f2015-10-27 01:29:34 +0000805
806 // release delayed commands wake lock if the queue is empty
807 if (mAudioCommands.isEmpty()) {
Ricardo Garcia05f2fdc2014-07-24 15:48:24 -0700808 release_wake_lock(mName.string());
Zach Janga754b4f2015-10-27 01:29:34 +0000809 }
810
811 // At this stage we have either an empty command queue or the first command in the queue
812 // has a finite delay. So unless we are exiting it is safe to wait.
813 if (!exitPending()) {
Eric Laurent59a89232014-06-08 14:14:17 -0700814 ALOGV("AudioCommandThread() going to sleep");
Eric Laurentd7eda8d2016-02-02 17:18:39 -0800815 if (waitTime == -1) {
816 mWaitWorkCV.wait(mLock);
817 } else {
818 mWaitWorkCV.waitRelative(mLock, waitTime);
819 }
Eric Laurent59a89232014-06-08 14:14:17 -0700820 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700821 }
Ricardo Garcia05f2fdc2014-07-24 15:48:24 -0700822 // release delayed commands wake lock before quitting
823 if (!mAudioCommands.isEmpty()) {
824 release_wake_lock(mName.string());
825 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700826 mLock.unlock();
827 return false;
828}
829
830status_t AudioPolicyService::AudioCommandThread::dump(int fd)
831{
832 const size_t SIZE = 256;
833 char buffer[SIZE];
834 String8 result;
835
836 snprintf(buffer, SIZE, "AudioCommandThread %p Dump\n", this);
837 result.append(buffer);
838 write(fd, result.string(), result.size());
839
840 bool locked = tryLock(mLock);
841 if (!locked) {
842 String8 result2(kCmdDeadlockedString);
843 write(fd, result2.string(), result2.size());
844 }
845
846 snprintf(buffer, SIZE, "- Commands:\n");
847 result = String8(buffer);
848 result.append(" Command Time Wait pParam\n");
Glenn Kasten8d6a2442012-02-08 14:04:28 -0800849 for (size_t i = 0; i < mAudioCommands.size(); i++) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700850 mAudioCommands[i]->dump(buffer, SIZE);
851 result.append(buffer);
852 }
853 result.append(" Last Command\n");
Eric Laurent0ede8922014-05-09 18:04:42 -0700854 if (mLastCommand != 0) {
855 mLastCommand->dump(buffer, SIZE);
856 result.append(buffer);
857 } else {
858 result.append(" none\n");
859 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700860
861 write(fd, result.string(), result.size());
862
863 if (locked) mLock.unlock();
864
865 return NO_ERROR;
866}
867
Glenn Kasten3d2f8772012-01-27 15:25:25 -0800868void AudioPolicyService::AudioCommandThread::startToneCommand(ToneGenerator::tone_type type,
869 audio_stream_type_t stream)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700870{
Eric Laurent0ede8922014-05-09 18:04:42 -0700871 sp<AudioCommand> command = new AudioCommand();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700872 command->mCommand = START_TONE;
Eric Laurent0ede8922014-05-09 18:04:42 -0700873 sp<ToneData> data = new ToneData();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700874 data->mType = type;
875 data->mStream = stream;
Jesper Tragardh48412dc2014-03-24 14:12:43 +0100876 command->mParam = data;
Steve Block3856b092011-10-20 11:56:00 +0100877 ALOGV("AudioCommandThread() adding tone start type %d, stream %d", type, stream);
Eric Laurent0ede8922014-05-09 18:04:42 -0700878 sendCommand(command);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700879}
880
881void AudioPolicyService::AudioCommandThread::stopToneCommand()
882{
Eric Laurent0ede8922014-05-09 18:04:42 -0700883 sp<AudioCommand> command = new AudioCommand();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700884 command->mCommand = STOP_TONE;
Steve Block3856b092011-10-20 11:56:00 +0100885 ALOGV("AudioCommandThread() adding tone stop");
Eric Laurent0ede8922014-05-09 18:04:42 -0700886 sendCommand(command);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700887}
888
Glenn Kastenfff6d712012-01-12 16:38:12 -0800889status_t AudioPolicyService::AudioCommandThread::volumeCommand(audio_stream_type_t stream,
Eric Laurentde070132010-07-13 04:45:46 -0700890 float volume,
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800891 audio_io_handle_t output,
Eric Laurentde070132010-07-13 04:45:46 -0700892 int delayMs)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700893{
Eric Laurent0ede8922014-05-09 18:04:42 -0700894 sp<AudioCommand> command = new AudioCommand();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700895 command->mCommand = SET_VOLUME;
Eric Laurent0ede8922014-05-09 18:04:42 -0700896 sp<VolumeData> data = new VolumeData();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700897 data->mStream = stream;
898 data->mVolume = volume;
899 data->mIO = output;
900 command->mParam = data;
Eric Laurent0ede8922014-05-09 18:04:42 -0700901 command->mWaitStatus = true;
Steve Block3856b092011-10-20 11:56:00 +0100902 ALOGV("AudioCommandThread() adding set volume stream %d, volume %f, output %d",
Eric Laurentde070132010-07-13 04:45:46 -0700903 stream, volume, output);
Eric Laurent0ede8922014-05-09 18:04:42 -0700904 return sendCommand(command, delayMs);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700905}
906
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800907status_t AudioPolicyService::AudioCommandThread::parametersCommand(audio_io_handle_t ioHandle,
Dima Zavinfce7a472011-04-19 22:30:36 -0700908 const char *keyValuePairs,
Eric Laurentde070132010-07-13 04:45:46 -0700909 int delayMs)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700910{
Eric Laurent0ede8922014-05-09 18:04:42 -0700911 sp<AudioCommand> command = new AudioCommand();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700912 command->mCommand = SET_PARAMETERS;
Eric Laurent0ede8922014-05-09 18:04:42 -0700913 sp<ParametersData> data = new ParametersData();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700914 data->mIO = ioHandle;
Dima Zavinfce7a472011-04-19 22:30:36 -0700915 data->mKeyValuePairs = String8(keyValuePairs);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700916 command->mParam = data;
Eric Laurent0ede8922014-05-09 18:04:42 -0700917 command->mWaitStatus = true;
Steve Block3856b092011-10-20 11:56:00 +0100918 ALOGV("AudioCommandThread() adding set parameter string %s, io %d ,delay %d",
Dima Zavinfce7a472011-04-19 22:30:36 -0700919 keyValuePairs, ioHandle, delayMs);
Eric Laurent0ede8922014-05-09 18:04:42 -0700920 return sendCommand(command, delayMs);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700921}
922
923status_t AudioPolicyService::AudioCommandThread::voiceVolumeCommand(float volume, int delayMs)
924{
Eric Laurent0ede8922014-05-09 18:04:42 -0700925 sp<AudioCommand> command = new AudioCommand();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700926 command->mCommand = SET_VOICE_VOLUME;
Eric Laurent0ede8922014-05-09 18:04:42 -0700927 sp<VoiceVolumeData> data = new VoiceVolumeData();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700928 data->mVolume = volume;
929 command->mParam = data;
Eric Laurent0ede8922014-05-09 18:04:42 -0700930 command->mWaitStatus = true;
Steve Block3856b092011-10-20 11:56:00 +0100931 ALOGV("AudioCommandThread() adding set voice volume volume %f", volume);
Eric Laurent0ede8922014-05-09 18:04:42 -0700932 return sendCommand(command, delayMs);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700933}
934
Eric Laurentbfb1b832013-01-07 09:53:42 -0800935void AudioPolicyService::AudioCommandThread::stopOutputCommand(audio_io_handle_t output,
936 audio_stream_type_t stream,
Eric Laurente83b55d2014-11-14 10:06:21 -0800937 audio_session_t session)
Eric Laurentbfb1b832013-01-07 09:53:42 -0800938{
Eric Laurent0ede8922014-05-09 18:04:42 -0700939 sp<AudioCommand> command = new AudioCommand();
Eric Laurentbfb1b832013-01-07 09:53:42 -0800940 command->mCommand = STOP_OUTPUT;
Eric Laurent0ede8922014-05-09 18:04:42 -0700941 sp<StopOutputData> data = new StopOutputData();
Eric Laurentbfb1b832013-01-07 09:53:42 -0800942 data->mIO = output;
943 data->mStream = stream;
944 data->mSession = session;
Jesper Tragardh48412dc2014-03-24 14:12:43 +0100945 command->mParam = data;
Eric Laurentbfb1b832013-01-07 09:53:42 -0800946 ALOGV("AudioCommandThread() adding stop output %d", output);
Eric Laurent0ede8922014-05-09 18:04:42 -0700947 sendCommand(command);
Eric Laurentbfb1b832013-01-07 09:53:42 -0800948}
949
Eric Laurente83b55d2014-11-14 10:06:21 -0800950void AudioPolicyService::AudioCommandThread::releaseOutputCommand(audio_io_handle_t output,
951 audio_stream_type_t stream,
952 audio_session_t session)
Eric Laurentbfb1b832013-01-07 09:53:42 -0800953{
Eric Laurent0ede8922014-05-09 18:04:42 -0700954 sp<AudioCommand> command = new AudioCommand();
Eric Laurentbfb1b832013-01-07 09:53:42 -0800955 command->mCommand = RELEASE_OUTPUT;
Eric Laurent0ede8922014-05-09 18:04:42 -0700956 sp<ReleaseOutputData> data = new ReleaseOutputData();
Eric Laurentbfb1b832013-01-07 09:53:42 -0800957 data->mIO = output;
Eric Laurente83b55d2014-11-14 10:06:21 -0800958 data->mStream = stream;
959 data->mSession = session;
Jesper Tragardh48412dc2014-03-24 14:12:43 +0100960 command->mParam = data;
Eric Laurentbfb1b832013-01-07 09:53:42 -0800961 ALOGV("AudioCommandThread() adding release output %d", output);
Eric Laurent0ede8922014-05-09 18:04:42 -0700962 sendCommand(command);
963}
964
Eric Laurent951f4552014-05-20 10:48:17 -0700965status_t AudioPolicyService::AudioCommandThread::createAudioPatchCommand(
966 const struct audio_patch *patch,
967 audio_patch_handle_t *handle,
968 int delayMs)
969{
970 status_t status = NO_ERROR;
971
972 sp<AudioCommand> command = new AudioCommand();
973 command->mCommand = CREATE_AUDIO_PATCH;
974 CreateAudioPatchData *data = new CreateAudioPatchData();
975 data->mPatch = *patch;
976 data->mHandle = *handle;
977 command->mParam = data;
978 command->mWaitStatus = true;
979 ALOGV("AudioCommandThread() adding create patch delay %d", delayMs);
980 status = sendCommand(command, delayMs);
981 if (status == NO_ERROR) {
982 *handle = data->mHandle;
983 }
984 return status;
985}
986
987status_t AudioPolicyService::AudioCommandThread::releaseAudioPatchCommand(audio_patch_handle_t handle,
988 int delayMs)
989{
990 sp<AudioCommand> command = new AudioCommand();
991 command->mCommand = RELEASE_AUDIO_PATCH;
992 ReleaseAudioPatchData *data = new ReleaseAudioPatchData();
993 data->mHandle = handle;
994 command->mParam = data;
995 command->mWaitStatus = true;
996 ALOGV("AudioCommandThread() adding release patch delay %d", delayMs);
997 return sendCommand(command, delayMs);
998}
999
Eric Laurentb52c1522014-05-20 11:27:36 -07001000void AudioPolicyService::AudioCommandThread::updateAudioPortListCommand()
1001{
1002 sp<AudioCommand> command = new AudioCommand();
1003 command->mCommand = UPDATE_AUDIOPORT_LIST;
1004 ALOGV("AudioCommandThread() adding update audio port list");
1005 sendCommand(command);
1006}
1007
1008void AudioPolicyService::AudioCommandThread::updateAudioPatchListCommand()
1009{
1010 sp<AudioCommand>command = new AudioCommand();
1011 command->mCommand = UPDATE_AUDIOPATCH_LIST;
1012 ALOGV("AudioCommandThread() adding update audio patch list");
1013 sendCommand(command);
1014}
1015
Eric Laurente1715a42014-05-20 11:30:42 -07001016status_t AudioPolicyService::AudioCommandThread::setAudioPortConfigCommand(
1017 const struct audio_port_config *config, int delayMs)
1018{
1019 sp<AudioCommand> command = new AudioCommand();
1020 command->mCommand = SET_AUDIOPORT_CONFIG;
1021 SetAudioPortConfigData *data = new SetAudioPortConfigData();
1022 data->mConfig = *config;
1023 command->mParam = data;
1024 command->mWaitStatus = true;
1025 ALOGV("AudioCommandThread() adding set port config delay %d", delayMs);
1026 return sendCommand(command, delayMs);
1027}
1028
Jean-Michel Trivide801052015-04-14 19:10:14 -07001029void AudioPolicyService::AudioCommandThread::dynamicPolicyMixStateUpdateCommand(
Chih-Hung Hsiehe964d4e2016-08-09 14:31:32 -07001030 const String8& regId, int32_t state)
Jean-Michel Trivide801052015-04-14 19:10:14 -07001031{
1032 sp<AudioCommand> command = new AudioCommand();
1033 command->mCommand = DYN_POLICY_MIX_STATE_UPDATE;
1034 DynPolicyMixStateUpdateData *data = new DynPolicyMixStateUpdateData();
1035 data->mRegId = regId;
1036 data->mState = state;
1037 command->mParam = data;
1038 ALOGV("AudioCommandThread() sending dynamic policy mix (id=%s) state update to %d",
1039 regId.string(), state);
1040 sendCommand(command);
1041}
1042
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -08001043void AudioPolicyService::AudioCommandThread::recordingConfigurationUpdateCommand(
Jean-Michel Triviac4e4292016-12-22 11:39:31 -08001044 int event, const record_client_info_t *clientInfo,
Jean-Michel Trivi8c7cf3b2016-02-25 17:08:24 -08001045 const audio_config_base_t *clientConfig, const audio_config_base_t *deviceConfig,
1046 audio_patch_handle_t patchHandle)
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -08001047{
1048 sp<AudioCommand>command = new AudioCommand();
1049 command->mCommand = RECORDING_CONFIGURATION_UPDATE;
1050 RecordingConfigurationUpdateData *data = new RecordingConfigurationUpdateData();
1051 data->mEvent = event;
Jean-Michel Triviac4e4292016-12-22 11:39:31 -08001052 data->mClientInfo = *clientInfo;
Jean-Michel Trivi7281aa92016-02-17 15:33:40 -08001053 data->mClientConfig = *clientConfig;
1054 data->mDeviceConfig = *deviceConfig;
Jean-Michel Trivi8c7cf3b2016-02-25 17:08:24 -08001055 data->mPatchHandle = patchHandle;
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -08001056 command->mParam = data;
Jean-Michel Triviac4e4292016-12-22 11:39:31 -08001057 ALOGV("AudioCommandThread() adding recording configuration update event %d, source %d uid %u",
1058 event, clientInfo->source, clientInfo->uid);
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -08001059 sendCommand(command);
1060}
1061
Eric Laurent0ede8922014-05-09 18:04:42 -07001062status_t AudioPolicyService::AudioCommandThread::sendCommand(sp<AudioCommand>& command, int delayMs)
1063{
1064 {
1065 Mutex::Autolock _l(mLock);
1066 insertCommand_l(command, delayMs);
1067 mWaitWorkCV.signal();
1068 }
1069 Mutex::Autolock _l(command->mLock);
1070 while (command->mWaitStatus) {
1071 nsecs_t timeOutNs = kAudioCommandTimeoutNs + milliseconds(delayMs);
1072 if (command->mCond.waitRelative(command->mLock, timeOutNs) != NO_ERROR) {
1073 command->mStatus = TIMED_OUT;
1074 command->mWaitStatus = false;
1075 }
1076 }
1077 return command->mStatus;
Eric Laurentbfb1b832013-01-07 09:53:42 -08001078}
1079
Mathias Agopian65ab4712010-07-14 17:59:35 -07001080// insertCommand_l() must be called with mLock held
Eric Laurent0ede8922014-05-09 18:04:42 -07001081void AudioPolicyService::AudioCommandThread::insertCommand_l(sp<AudioCommand>& command, int delayMs)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001082{
Glenn Kasten8d6a2442012-02-08 14:04:28 -08001083 ssize_t i; // not size_t because i will count down to -1
Eric Laurent0ede8922014-05-09 18:04:42 -07001084 Vector < sp<AudioCommand> > removedCommands;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001085 command->mTime = systemTime() + milliseconds(delayMs);
1086
1087 // acquire wake lock to make sure delayed commands are processed
Eric Laurentbfb1b832013-01-07 09:53:42 -08001088 if (mAudioCommands.isEmpty()) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07001089 acquire_wake_lock(PARTIAL_WAKE_LOCK, mName.string());
1090 }
1091
1092 // check same pending commands with later time stamps and eliminate them
Ivan Lozano5ff158f2017-10-30 09:06:24 -07001093 for (i = (ssize_t)mAudioCommands.size()-1; i >= 0; i--) {
Eric Laurent0ede8922014-05-09 18:04:42 -07001094 sp<AudioCommand> command2 = mAudioCommands[i];
Mathias Agopian65ab4712010-07-14 17:59:35 -07001095 // commands are sorted by increasing time stamp: no need to scan the rest of mAudioCommands
1096 if (command2->mTime <= command->mTime) break;
Eric Laurente45b48a2014-09-04 16:40:57 -07001097
1098 // create audio patch or release audio patch commands are equivalent
1099 // with regard to filtering
1100 if ((command->mCommand == CREATE_AUDIO_PATCH) ||
1101 (command->mCommand == RELEASE_AUDIO_PATCH)) {
1102 if ((command2->mCommand != CREATE_AUDIO_PATCH) &&
1103 (command2->mCommand != RELEASE_AUDIO_PATCH)) {
1104 continue;
1105 }
1106 } else if (command2->mCommand != command->mCommand) continue;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001107
1108 switch (command->mCommand) {
1109 case SET_PARAMETERS: {
Eric Laurent0ede8922014-05-09 18:04:42 -07001110 ParametersData *data = (ParametersData *)command->mParam.get();
1111 ParametersData *data2 = (ParametersData *)command2->mParam.get();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001112 if (data->mIO != data2->mIO) break;
Steve Block3856b092011-10-20 11:56:00 +01001113 ALOGV("Comparing parameter command %s to new command %s",
Eric Laurentde070132010-07-13 04:45:46 -07001114 data2->mKeyValuePairs.string(), data->mKeyValuePairs.string());
Mathias Agopian65ab4712010-07-14 17:59:35 -07001115 AudioParameter param = AudioParameter(data->mKeyValuePairs);
1116 AudioParameter param2 = AudioParameter(data2->mKeyValuePairs);
1117 for (size_t j = 0; j < param.size(); j++) {
Glenn Kastene53b9ea2012-03-12 16:29:55 -07001118 String8 key;
1119 String8 value;
1120 param.getAt(j, key, value);
1121 for (size_t k = 0; k < param2.size(); k++) {
1122 String8 key2;
1123 String8 value2;
1124 param2.getAt(k, key2, value2);
1125 if (key2 == key) {
1126 param2.remove(key2);
1127 ALOGV("Filtering out parameter %s", key2.string());
1128 break;
1129 }
1130 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001131 }
1132 // if all keys have been filtered out, remove the command.
1133 // otherwise, update the key value pairs
1134 if (param2.size() == 0) {
1135 removedCommands.add(command2);
1136 } else {
1137 data2->mKeyValuePairs = param2.toString();
1138 }
Eric Laurent21e54562013-09-23 12:08:05 -07001139 command->mTime = command2->mTime;
1140 // force delayMs to non 0 so that code below does not request to wait for
1141 // command status as the command is now delayed
1142 delayMs = 1;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001143 } break;
1144
1145 case SET_VOLUME: {
Eric Laurent0ede8922014-05-09 18:04:42 -07001146 VolumeData *data = (VolumeData *)command->mParam.get();
1147 VolumeData *data2 = (VolumeData *)command2->mParam.get();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001148 if (data->mIO != data2->mIO) break;
1149 if (data->mStream != data2->mStream) break;
Steve Block3856b092011-10-20 11:56:00 +01001150 ALOGV("Filtering out volume command on output %d for stream %d",
Eric Laurentde070132010-07-13 04:45:46 -07001151 data->mIO, data->mStream);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001152 removedCommands.add(command2);
Eric Laurent21e54562013-09-23 12:08:05 -07001153 command->mTime = command2->mTime;
1154 // force delayMs to non 0 so that code below does not request to wait for
1155 // command status as the command is now delayed
1156 delayMs = 1;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001157 } break;
Eric Laurente45b48a2014-09-04 16:40:57 -07001158
Eric Laurentbaf35fe2016-07-27 15:36:53 -07001159 case SET_VOICE_VOLUME: {
1160 VoiceVolumeData *data = (VoiceVolumeData *)command->mParam.get();
1161 VoiceVolumeData *data2 = (VoiceVolumeData *)command2->mParam.get();
1162 ALOGV("Filtering out voice volume command value %f replaced by %f",
1163 data2->mVolume, data->mVolume);
1164 removedCommands.add(command2);
1165 command->mTime = command2->mTime;
1166 // force delayMs to non 0 so that code below does not request to wait for
1167 // command status as the command is now delayed
1168 delayMs = 1;
1169 } break;
1170
Eric Laurente45b48a2014-09-04 16:40:57 -07001171 case CREATE_AUDIO_PATCH:
1172 case RELEASE_AUDIO_PATCH: {
1173 audio_patch_handle_t handle;
Haynes Mathew Georgea2d4a6d2014-10-13 13:05:22 -07001174 struct audio_patch patch;
Eric Laurente45b48a2014-09-04 16:40:57 -07001175 if (command->mCommand == CREATE_AUDIO_PATCH) {
1176 handle = ((CreateAudioPatchData *)command->mParam.get())->mHandle;
Haynes Mathew Georgea2d4a6d2014-10-13 13:05:22 -07001177 patch = ((CreateAudioPatchData *)command->mParam.get())->mPatch;
Eric Laurente45b48a2014-09-04 16:40:57 -07001178 } else {
1179 handle = ((ReleaseAudioPatchData *)command->mParam.get())->mHandle;
1180 }
1181 audio_patch_handle_t handle2;
Haynes Mathew Georgea2d4a6d2014-10-13 13:05:22 -07001182 struct audio_patch patch2;
Eric Laurente45b48a2014-09-04 16:40:57 -07001183 if (command2->mCommand == CREATE_AUDIO_PATCH) {
1184 handle2 = ((CreateAudioPatchData *)command2->mParam.get())->mHandle;
Haynes Mathew Georgea2d4a6d2014-10-13 13:05:22 -07001185 patch2 = ((CreateAudioPatchData *)command2->mParam.get())->mPatch;
Eric Laurente45b48a2014-09-04 16:40:57 -07001186 } else {
1187 handle2 = ((ReleaseAudioPatchData *)command2->mParam.get())->mHandle;
Glenn Kastenf60b6b62015-07-06 10:53:26 -07001188 memset(&patch2, 0, sizeof(patch2));
Eric Laurente45b48a2014-09-04 16:40:57 -07001189 }
1190 if (handle != handle2) break;
Haynes Mathew Georgea2d4a6d2014-10-13 13:05:22 -07001191 /* Filter CREATE_AUDIO_PATCH commands only when they are issued for
1192 same output. */
1193 if( (command->mCommand == CREATE_AUDIO_PATCH) &&
1194 (command2->mCommand == CREATE_AUDIO_PATCH) ) {
1195 bool isOutputDiff = false;
1196 if (patch.num_sources == patch2.num_sources) {
1197 for (unsigned count = 0; count < patch.num_sources; count++) {
1198 if (patch.sources[count].id != patch2.sources[count].id) {
1199 isOutputDiff = true;
1200 break;
1201 }
1202 }
1203 if (isOutputDiff)
1204 break;
1205 }
1206 }
Eric Laurente45b48a2014-09-04 16:40:57 -07001207 ALOGV("Filtering out %s audio patch command for handle %d",
1208 (command->mCommand == CREATE_AUDIO_PATCH) ? "create" : "release", handle);
1209 removedCommands.add(command2);
1210 command->mTime = command2->mTime;
1211 // force delayMs to non 0 so that code below does not request to wait for
1212 // command status as the command is now delayed
1213 delayMs = 1;
1214 } break;
1215
Jean-Michel Trivide801052015-04-14 19:10:14 -07001216 case DYN_POLICY_MIX_STATE_UPDATE: {
1217
1218 } break;
1219
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -08001220 case RECORDING_CONFIGURATION_UPDATE: {
1221
1222 } break;
1223
Mathias Agopian65ab4712010-07-14 17:59:35 -07001224 case START_TONE:
1225 case STOP_TONE:
1226 default:
1227 break;
1228 }
1229 }
1230
1231 // remove filtered commands
1232 for (size_t j = 0; j < removedCommands.size(); j++) {
1233 // removed commands always have time stamps greater than current command
1234 for (size_t k = i + 1; k < mAudioCommands.size(); k++) {
Eric Laurent0ede8922014-05-09 18:04:42 -07001235 if (mAudioCommands[k].get() == removedCommands[j].get()) {
Steve Block3856b092011-10-20 11:56:00 +01001236 ALOGV("suppressing command: %d", mAudioCommands[k]->mCommand);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001237 mAudioCommands.removeAt(k);
1238 break;
1239 }
1240 }
1241 }
1242 removedCommands.clear();
1243
Eric Laurentaa79bef2015-01-15 14:33:51 -08001244 // Disable wait for status if delay is not 0.
1245 // Except for create audio patch command because the returned patch handle
1246 // is needed by audio policy manager
1247 if (delayMs != 0 && command->mCommand != CREATE_AUDIO_PATCH) {
Eric Laurentcec4abb2012-07-03 12:23:02 -07001248 command->mWaitStatus = false;
1249 }
Eric Laurentcec4abb2012-07-03 12:23:02 -07001250
Mathias Agopian65ab4712010-07-14 17:59:35 -07001251 // insert command at the right place according to its time stamp
Eric Laurent1e693b52014-07-09 15:03:28 -07001252 ALOGV("inserting command: %d at index %zd, num commands %zu",
1253 command->mCommand, i+1, mAudioCommands.size());
Mathias Agopian65ab4712010-07-14 17:59:35 -07001254 mAudioCommands.insertAt(command, i + 1);
1255}
1256
1257void AudioPolicyService::AudioCommandThread::exit()
1258{
Steve Block3856b092011-10-20 11:56:00 +01001259 ALOGV("AudioCommandThread::exit");
Mathias Agopian65ab4712010-07-14 17:59:35 -07001260 {
1261 AutoMutex _l(mLock);
1262 requestExit();
1263 mWaitWorkCV.signal();
1264 }
Zach Janga754b4f2015-10-27 01:29:34 +00001265 // Note that we can call it from the thread loop if all other references have been released
1266 // but it will safely return WOULD_BLOCK in this case
Mathias Agopian65ab4712010-07-14 17:59:35 -07001267 requestExitAndWait();
1268}
1269
1270void AudioPolicyService::AudioCommandThread::AudioCommand::dump(char* buffer, size_t size)
1271{
1272 snprintf(buffer, size, " %02d %06d.%03d %01u %p\n",
1273 mCommand,
1274 (int)ns2s(mTime),
1275 (int)ns2ms(mTime)%1000,
1276 mWaitStatus,
Eric Laurent0ede8922014-05-09 18:04:42 -07001277 mParam.get());
Mathias Agopian65ab4712010-07-14 17:59:35 -07001278}
1279
Dima Zavinfce7a472011-04-19 22:30:36 -07001280/******* helpers for the service_ops callbacks defined below *********/
1281void AudioPolicyService::setParameters(audio_io_handle_t ioHandle,
1282 const char *keyValuePairs,
1283 int delayMs)
1284{
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001285 mAudioCommandThread->parametersCommand(ioHandle, keyValuePairs,
Dima Zavinfce7a472011-04-19 22:30:36 -07001286 delayMs);
1287}
1288
1289int AudioPolicyService::setStreamVolume(audio_stream_type_t stream,
1290 float volume,
1291 audio_io_handle_t output,
1292 int delayMs)
1293{
Glenn Kastenfff6d712012-01-12 16:38:12 -08001294 return (int)mAudioCommandThread->volumeCommand(stream, volume,
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001295 output, delayMs);
Dima Zavinfce7a472011-04-19 22:30:36 -07001296}
1297
1298int AudioPolicyService::startTone(audio_policy_tone_t tone,
1299 audio_stream_type_t stream)
1300{
Glenn Kasten6e2ebe92013-08-13 09:14:51 -07001301 if (tone != AUDIO_POLICY_TONE_IN_CALL_NOTIFICATION) {
Steve Block29357bc2012-01-06 19:20:56 +00001302 ALOGE("startTone: illegal tone requested (%d)", tone);
Glenn Kasten6e2ebe92013-08-13 09:14:51 -07001303 }
1304 if (stream != AUDIO_STREAM_VOICE_CALL) {
Steve Block29357bc2012-01-06 19:20:56 +00001305 ALOGE("startTone: illegal stream (%d) requested for tone %d", stream,
Glenn Kastene53b9ea2012-03-12 16:29:55 -07001306 tone);
Glenn Kasten6e2ebe92013-08-13 09:14:51 -07001307 }
Dima Zavinfce7a472011-04-19 22:30:36 -07001308 mTonePlaybackThread->startToneCommand(ToneGenerator::TONE_SUP_CALL_WAITING,
1309 AUDIO_STREAM_VOICE_CALL);
1310 return 0;
1311}
1312
1313int AudioPolicyService::stopTone()
1314{
1315 mTonePlaybackThread->stopToneCommand();
1316 return 0;
1317}
1318
1319int AudioPolicyService::setVoiceVolume(float volume, int delayMs)
1320{
1321 return (int)mAudioCommandThread->voiceVolumeCommand(volume, delayMs);
1322}
1323
Dima Zavinfce7a472011-04-19 22:30:36 -07001324extern "C" {
Eric Laurent2d388ec2014-03-07 13:25:54 -08001325audio_module_handle_t aps_load_hw_module(void *service __unused,
1326 const char *name);
1327audio_io_handle_t aps_open_output(void *service __unused,
Eric Laurenta4c5a552012-03-29 10:12:40 -07001328 audio_devices_t *pDevices,
1329 uint32_t *pSamplingRate,
1330 audio_format_t *pFormat,
1331 audio_channel_mask_t *pChannelMask,
1332 uint32_t *pLatencyMs,
Eric Laurent2d388ec2014-03-07 13:25:54 -08001333 audio_output_flags_t flags);
Eric Laurenta4c5a552012-03-29 10:12:40 -07001334
Eric Laurent2d388ec2014-03-07 13:25:54 -08001335audio_io_handle_t aps_open_output_on_module(void *service __unused,
Eric Laurenta4c5a552012-03-29 10:12:40 -07001336 audio_module_handle_t module,
1337 audio_devices_t *pDevices,
1338 uint32_t *pSamplingRate,
1339 audio_format_t *pFormat,
1340 audio_channel_mask_t *pChannelMask,
1341 uint32_t *pLatencyMs,
Richard Fitzgeraldad3af332013-03-25 16:54:37 +00001342 audio_output_flags_t flags,
Eric Laurent2d388ec2014-03-07 13:25:54 -08001343 const audio_offload_info_t *offloadInfo);
1344audio_io_handle_t aps_open_dup_output(void *service __unused,
Dima Zavinfce7a472011-04-19 22:30:36 -07001345 audio_io_handle_t output1,
Eric Laurent2d388ec2014-03-07 13:25:54 -08001346 audio_io_handle_t output2);
1347int aps_close_output(void *service __unused, audio_io_handle_t output);
1348int aps_suspend_output(void *service __unused, audio_io_handle_t output);
1349int aps_restore_output(void *service __unused, audio_io_handle_t output);
1350audio_io_handle_t aps_open_input(void *service __unused,
Eric Laurenta4c5a552012-03-29 10:12:40 -07001351 audio_devices_t *pDevices,
1352 uint32_t *pSamplingRate,
1353 audio_format_t *pFormat,
1354 audio_channel_mask_t *pChannelMask,
Eric Laurent2d388ec2014-03-07 13:25:54 -08001355 audio_in_acoustics_t acoustics __unused);
1356audio_io_handle_t aps_open_input_on_module(void *service __unused,
Eric Laurenta4c5a552012-03-29 10:12:40 -07001357 audio_module_handle_t module,
1358 audio_devices_t *pDevices,
1359 uint32_t *pSamplingRate,
1360 audio_format_t *pFormat,
Eric Laurent2d388ec2014-03-07 13:25:54 -08001361 audio_channel_mask_t *pChannelMask);
1362int aps_close_input(void *service __unused, audio_io_handle_t input);
1363int aps_invalidate_stream(void *service __unused, audio_stream_type_t stream);
Glenn Kastend848eb42016-03-08 13:42:11 -08001364int aps_move_effects(void *service __unused, audio_session_t session,
Dima Zavinfce7a472011-04-19 22:30:36 -07001365 audio_io_handle_t src_output,
Eric Laurent2d388ec2014-03-07 13:25:54 -08001366 audio_io_handle_t dst_output);
1367char * aps_get_parameters(void *service __unused, audio_io_handle_t io_handle,
1368 const char *keys);
1369void aps_set_parameters(void *service, audio_io_handle_t io_handle,
1370 const char *kv_pairs, int delay_ms);
1371int aps_set_stream_volume(void *service, audio_stream_type_t stream,
Dima Zavinfce7a472011-04-19 22:30:36 -07001372 float volume, audio_io_handle_t output,
Eric Laurent2d388ec2014-03-07 13:25:54 -08001373 int delay_ms);
1374int aps_start_tone(void *service, audio_policy_tone_t tone,
1375 audio_stream_type_t stream);
1376int aps_stop_tone(void *service);
1377int aps_set_voice_volume(void *service, float volume, int delay_ms);
1378};
Dima Zavinfce7a472011-04-19 22:30:36 -07001379
Mikhail Naganov1b2a7942017-12-08 10:18:09 -08001380} // namespace android