blob: e5aed9a237e4295747f1b1d19beb5d9db66bf2a8 [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{
276 if (mAudioPolicyServiceClient != 0) {
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{
286 if (mAudioPolicyServiceClient != 0) {
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{
335 {
336 Mutex::Autolock _l(mLock);
337 if (mAudioPolicyManager) {
338 mAudioPolicyManager->setRecordSilenced(uid, silenced);
339 }
340 }
341 sp<IAudioFlinger> af = AudioSystem::get_audio_flinger();
342 if (af) {
343 af->setRecordSilenced(uid, silenced);
344 }
345}
346
Glenn Kasten0f11b512014-01-31 16:18:54 -0800347status_t AudioPolicyService::dump(int fd, const Vector<String16>& args __unused)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700348{
Glenn Kasten44deb052012-02-05 18:09:08 -0800349 if (!dumpAllowed()) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700350 dumpPermissionDenial(fd);
351 } else {
352 bool locked = tryLock(mLock);
353 if (!locked) {
354 String8 result(kDeadlockedString);
355 write(fd, result.string(), result.size());
356 }
357
358 dumpInternals(fd);
Glenn Kasten9d1f02d2012-02-08 17:47:58 -0800359 if (mAudioCommandThread != 0) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700360 mAudioCommandThread->dump(fd);
361 }
Glenn Kasten9d1f02d2012-02-08 17:47:58 -0800362 if (mTonePlaybackThread != 0) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700363 mTonePlaybackThread->dump(fd);
364 }
365
Eric Laurentdce54a12014-03-10 12:19:46 -0700366 if (mAudioPolicyManager) {
367 mAudioPolicyManager->dump(fd);
368 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700369
370 if (locked) mLock.unlock();
371 }
372 return NO_ERROR;
373}
374
375status_t AudioPolicyService::dumpPermissionDenial(int fd)
376{
377 const size_t SIZE = 256;
378 char buffer[SIZE];
379 String8 result;
380 snprintf(buffer, SIZE, "Permission Denial: "
381 "can't dump AudioPolicyService from pid=%d, uid=%d\n",
382 IPCThreadState::self()->getCallingPid(),
383 IPCThreadState::self()->getCallingUid());
384 result.append(buffer);
385 write(fd, result.string(), result.size());
386 return NO_ERROR;
387}
388
389status_t AudioPolicyService::onTransact(
Svet Ganovf4ddfef2018-01-16 07:37:58 -0800390 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags) {
391 switch (code) {
392 case SHELL_COMMAND_TRANSACTION: {
393 int in = data.readFileDescriptor();
394 int out = data.readFileDescriptor();
395 int err = data.readFileDescriptor();
396 int argc = data.readInt32();
397 Vector<String16> args;
398 for (int i = 0; i < argc && data.dataAvail() > 0; i++) {
399 args.add(data.readString16());
400 }
401 sp<IBinder> unusedCallback;
402 sp<IResultReceiver> resultReceiver;
403 status_t status;
404 if ((status = data.readNullableStrongBinder(&unusedCallback)) != NO_ERROR) {
405 return status;
406 }
407 if ((status = data.readNullableStrongBinder(&resultReceiver)) != NO_ERROR) {
408 return status;
409 }
410 status = shellCommand(in, out, err, args);
411 if (resultReceiver != nullptr) {
412 resultReceiver->send(status);
413 }
414 return NO_ERROR;
415 }
416 }
417
Mathias Agopian65ab4712010-07-14 17:59:35 -0700418 return BnAudioPolicyService::onTransact(code, data, reply, flags);
419}
420
Svet Ganovf4ddfef2018-01-16 07:37:58 -0800421// ------------------- Shell command implementation -------------------
422
423// NOTE: This is a remote API - make sure all args are validated
424status_t AudioPolicyService::shellCommand(int in, int out, int err, Vector<String16>& args) {
425 if (!checkCallingPermission(sManageAudioPolicyPermission, nullptr, nullptr)) {
426 return PERMISSION_DENIED;
427 }
428 if (in == BAD_TYPE || out == BAD_TYPE || err == BAD_TYPE) {
429 return BAD_VALUE;
430 }
431 if (args.size() == 3 && args[0] == String16("set-uid-state")) {
432 return handleSetUidState(args, err);
433 } else if (args.size() == 2 && args[0] == String16("reset-uid-state")) {
434 return handleResetUidState(args, err);
435 } else if (args.size() == 2 && args[0] == String16("get-uid-state")) {
436 return handleGetUidState(args, out, err);
437 } else if (args.size() == 1 && args[0] == String16("help")) {
438 printHelp(out);
439 return NO_ERROR;
440 }
441 printHelp(err);
442 return BAD_VALUE;
443}
444
445status_t AudioPolicyService::handleSetUidState(Vector<String16>& args, int err) {
446 PermissionController pc;
447 int uid = pc.getPackageUid(args[1], 0);
448 if (uid <= 0) {
449 ALOGE("Unknown package: '%s'", String8(args[1]).string());
450 dprintf(err, "Unknown package: '%s'\n", String8(args[1]).string());
451 return BAD_VALUE;
452 }
453 bool active = false;
454 if (args[2] == String16("active")) {
455 active = true;
456 } else if ((args[2] != String16("idle"))) {
457 ALOGE("Expected active or idle but got: '%s'", String8(args[2]).string());
458 return BAD_VALUE;
459 }
460 mUidPolicy->addOverrideUid(uid, active);
461 return NO_ERROR;
462}
463
464status_t AudioPolicyService::handleResetUidState(Vector<String16>& args, int err) {
465 PermissionController pc;
466 int uid = pc.getPackageUid(args[1], 0);
467 if (uid < 0) {
468 ALOGE("Unknown package: '%s'", String8(args[1]).string());
469 dprintf(err, "Unknown package: '%s'\n", String8(args[1]).string());
470 return BAD_VALUE;
471 }
472 mUidPolicy->removeOverrideUid(uid);
473 return NO_ERROR;
474}
475
476status_t AudioPolicyService::handleGetUidState(Vector<String16>& args, int out, int err) {
477 PermissionController pc;
478 int uid = pc.getPackageUid(args[1], 0);
479 if (uid < 0) {
480 ALOGE("Unknown package: '%s'", String8(args[1]).string());
481 dprintf(err, "Unknown package: '%s'\n", String8(args[1]).string());
482 return BAD_VALUE;
483 }
484 if (mUidPolicy->isUidActive(uid)) {
485 return dprintf(out, "active\n");
486 } else {
487 return dprintf(out, "idle\n");
488 }
489}
490
491status_t AudioPolicyService::printHelp(int out) {
492 return dprintf(out, "Audio policy service commands:\n"
493 " get-uid-state <PACKAGE> gets the uid state\n"
494 " set-uid-state <PACKAGE> <active|idle> overrides the uid state\n"
495 " reset-uid-state <PACKAGE> clears the uid state override\n"
496 " help print this message\n");
497}
498
499// ----------- AudioPolicyService::UidPolicy implementation ----------
500
501void AudioPolicyService::UidPolicy::registerSelf() {
502 ActivityManager am;
503 am.registerUidObserver(this, ActivityManager::UID_OBSERVER_GONE
504 | ActivityManager::UID_OBSERVER_IDLE
505 | ActivityManager::UID_OBSERVER_ACTIVE,
506 ActivityManager::PROCESS_STATE_UNKNOWN,
507 String16("audioserver"));
508}
509
510void AudioPolicyService::UidPolicy::unregisterSelf() {
511 ActivityManager am;
512 am.unregisterUidObserver(this);
513}
514
515void AudioPolicyService::UidPolicy::onUidGone(uid_t uid, __unused bool disabled) {
516 onUidIdle(uid, disabled);
517}
518
519void AudioPolicyService::UidPolicy::onUidActive(uid_t uid) {
520 {
521 Mutex::Autolock _l(mUidLock);
522 mActiveUids.insert(uid);
523 }
524 sp<AudioPolicyService> service = mService.promote();
525 if (service != nullptr) {
526 service->setRecordSilenced(uid, false);
527 }
528}
529
530void AudioPolicyService::UidPolicy::onUidIdle(uid_t uid, __unused bool disabled) {
531 bool deleted = false;
532 {
533 Mutex::Autolock _l(mUidLock);
534 if (mActiveUids.erase(uid) > 0) {
535 deleted = true;
536 }
537 }
538 if (deleted) {
539 sp<AudioPolicyService> service = mService.promote();
540 if (service != nullptr) {
541 service->setRecordSilenced(uid, true);
542 }
543 }
544}
545
546void AudioPolicyService::UidPolicy::addOverrideUid(uid_t uid, bool active) {
547 updateOverrideUid(uid, active, true);
548}
549
550void AudioPolicyService::UidPolicy::removeOverrideUid(uid_t uid) {
551 updateOverrideUid(uid, false, false);
552}
553
554void AudioPolicyService::UidPolicy::updateOverrideUid(uid_t uid, bool active, bool insert) {
555 bool wasActive = false;
556 bool isActive = false;
557 {
558 Mutex::Autolock _l(mUidLock);
559 wasActive = isUidActiveLocked(uid);
560 mOverrideUids.erase(uid);
561 if (insert) {
562 mOverrideUids.insert(std::pair<uid_t, bool>(uid, active));
563 }
564 isActive = isUidActiveLocked(uid);
565 }
566 if (wasActive != isActive) {
567 sp<AudioPolicyService> service = mService.promote();
568 if (service != nullptr) {
569 service->setRecordSilenced(uid, !isActive);
570 }
571 }
572}
573
574bool AudioPolicyService::UidPolicy::isUidActive(uid_t uid) {
575 // Non-app UIDs are considered always active
576 if (uid < FIRST_APPLICATION_UID) {
577 return true;
578 }
579 Mutex::Autolock _l(mUidLock);
580 return isUidActiveLocked(uid);
581}
582
583bool AudioPolicyService::UidPolicy::isUidActiveLocked(uid_t uid) {
584 // Non-app UIDs are considered always active
585 if (uid < FIRST_APPLICATION_UID) {
586 return true;
587 }
588 auto it = mOverrideUids.find(uid);
589 if (it != mOverrideUids.end()) {
590 return it->second;
591 }
592 return mActiveUids.find(uid) != mActiveUids.end();
593}
Mathias Agopian65ab4712010-07-14 17:59:35 -0700594
Mathias Agopian65ab4712010-07-14 17:59:35 -0700595// ----------- AudioPolicyService::AudioCommandThread implementation ----------
596
Eric Laurentbfb1b832013-01-07 09:53:42 -0800597AudioPolicyService::AudioCommandThread::AudioCommandThread(String8 name,
598 const wp<AudioPolicyService>& service)
599 : Thread(false), mName(name), mService(service)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700600{
601 mpToneGenerator = NULL;
602}
603
604
605AudioPolicyService::AudioCommandThread::~AudioCommandThread()
606{
Eric Laurentbfb1b832013-01-07 09:53:42 -0800607 if (!mAudioCommands.isEmpty()) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700608 release_wake_lock(mName.string());
609 }
610 mAudioCommands.clear();
Glenn Kastene9dd0172012-01-27 18:08:45 -0800611 delete mpToneGenerator;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700612}
613
614void AudioPolicyService::AudioCommandThread::onFirstRef()
615{
Eric Laurentbfb1b832013-01-07 09:53:42 -0800616 run(mName.string(), ANDROID_PRIORITY_AUDIO);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700617}
618
619bool AudioPolicyService::AudioCommandThread::threadLoop()
620{
Eric Laurentd7eda8d2016-02-02 17:18:39 -0800621 nsecs_t waitTime = -1;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700622
623 mLock.lock();
624 while (!exitPending())
625 {
Eric Laurent59a89232014-06-08 14:14:17 -0700626 sp<AudioPolicyService> svc;
627 while (!mAudioCommands.isEmpty() && !exitPending()) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700628 nsecs_t curTime = systemTime();
629 // commands are sorted by increasing time stamp: execute them from index 0 and up
630 if (mAudioCommands[0]->mTime <= curTime) {
Eric Laurent0ede8922014-05-09 18:04:42 -0700631 sp<AudioCommand> command = mAudioCommands[0];
Mathias Agopian65ab4712010-07-14 17:59:35 -0700632 mAudioCommands.removeAt(0);
Eric Laurent0ede8922014-05-09 18:04:42 -0700633 mLastCommand = command;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700634
635 switch (command->mCommand) {
636 case START_TONE: {
637 mLock.unlock();
Eric Laurent0ede8922014-05-09 18:04:42 -0700638 ToneData *data = (ToneData *)command->mParam.get();
Steve Block3856b092011-10-20 11:56:00 +0100639 ALOGV("AudioCommandThread() processing start tone %d on stream %d",
Mathias Agopian65ab4712010-07-14 17:59:35 -0700640 data->mType, data->mStream);
Glenn Kastene9dd0172012-01-27 18:08:45 -0800641 delete mpToneGenerator;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700642 mpToneGenerator = new ToneGenerator(data->mStream, 1.0);
643 mpToneGenerator->startTone(data->mType);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700644 mLock.lock();
645 }break;
646 case STOP_TONE: {
647 mLock.unlock();
Steve Block3856b092011-10-20 11:56:00 +0100648 ALOGV("AudioCommandThread() processing stop tone");
Mathias Agopian65ab4712010-07-14 17:59:35 -0700649 if (mpToneGenerator != NULL) {
650 mpToneGenerator->stopTone();
651 delete mpToneGenerator;
652 mpToneGenerator = NULL;
653 }
654 mLock.lock();
655 }break;
656 case SET_VOLUME: {
Eric Laurent0ede8922014-05-09 18:04:42 -0700657 VolumeData *data = (VolumeData *)command->mParam.get();
Steve Block3856b092011-10-20 11:56:00 +0100658 ALOGV("AudioCommandThread() processing set volume stream %d, \
Eric Laurentde070132010-07-13 04:45:46 -0700659 volume %f, output %d", data->mStream, data->mVolume, data->mIO);
660 command->mStatus = AudioSystem::setStreamVolume(data->mStream,
661 data->mVolume,
662 data->mIO);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700663 }break;
664 case SET_PARAMETERS: {
Eric Laurent0ede8922014-05-09 18:04:42 -0700665 ParametersData *data = (ParametersData *)command->mParam.get();
Glenn Kastene53b9ea2012-03-12 16:29:55 -0700666 ALOGV("AudioCommandThread() processing set parameters string %s, io %d",
667 data->mKeyValuePairs.string(), data->mIO);
668 command->mStatus = AudioSystem::setParameters(data->mIO, data->mKeyValuePairs);
Glenn Kastene53b9ea2012-03-12 16:29:55 -0700669 }break;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700670 case SET_VOICE_VOLUME: {
Eric Laurent0ede8922014-05-09 18:04:42 -0700671 VoiceVolumeData *data = (VoiceVolumeData *)command->mParam.get();
Steve Block3856b092011-10-20 11:56:00 +0100672 ALOGV("AudioCommandThread() processing set voice volume volume %f",
Eric Laurentde070132010-07-13 04:45:46 -0700673 data->mVolume);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700674 command->mStatus = AudioSystem::setVoiceVolume(data->mVolume);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700675 }break;
Eric Laurentbfb1b832013-01-07 09:53:42 -0800676 case STOP_OUTPUT: {
Eric Laurent0ede8922014-05-09 18:04:42 -0700677 StopOutputData *data = (StopOutputData *)command->mParam.get();
Eric Laurentbfb1b832013-01-07 09:53:42 -0800678 ALOGV("AudioCommandThread() processing stop output %d",
679 data->mIO);
Eric Laurent59a89232014-06-08 14:14:17 -0700680 svc = mService.promote();
Eric Laurentbfb1b832013-01-07 09:53:42 -0800681 if (svc == 0) {
682 break;
683 }
684 mLock.unlock();
685 svc->doStopOutput(data->mIO, data->mStream, data->mSession);
686 mLock.lock();
Eric Laurentbfb1b832013-01-07 09:53:42 -0800687 }break;
688 case RELEASE_OUTPUT: {
Eric Laurent0ede8922014-05-09 18:04:42 -0700689 ReleaseOutputData *data = (ReleaseOutputData *)command->mParam.get();
Eric Laurentbfb1b832013-01-07 09:53:42 -0800690 ALOGV("AudioCommandThread() processing release output %d",
691 data->mIO);
Eric Laurent59a89232014-06-08 14:14:17 -0700692 svc = mService.promote();
Eric Laurentbfb1b832013-01-07 09:53:42 -0800693 if (svc == 0) {
694 break;
695 }
696 mLock.unlock();
Eric Laurente83b55d2014-11-14 10:06:21 -0800697 svc->doReleaseOutput(data->mIO, data->mStream, data->mSession);
Eric Laurentbfb1b832013-01-07 09:53:42 -0800698 mLock.lock();
Eric Laurentbfb1b832013-01-07 09:53:42 -0800699 }break;
Eric Laurent951f4552014-05-20 10:48:17 -0700700 case CREATE_AUDIO_PATCH: {
701 CreateAudioPatchData *data = (CreateAudioPatchData *)command->mParam.get();
702 ALOGV("AudioCommandThread() processing create audio patch");
703 sp<IAudioFlinger> af = AudioSystem::get_audio_flinger();
704 if (af == 0) {
705 command->mStatus = PERMISSION_DENIED;
706 } else {
707 command->mStatus = af->createAudioPatch(&data->mPatch, &data->mHandle);
708 }
709 } break;
710 case RELEASE_AUDIO_PATCH: {
711 ReleaseAudioPatchData *data = (ReleaseAudioPatchData *)command->mParam.get();
712 ALOGV("AudioCommandThread() processing release audio patch");
713 sp<IAudioFlinger> af = AudioSystem::get_audio_flinger();
714 if (af == 0) {
715 command->mStatus = PERMISSION_DENIED;
716 } else {
717 command->mStatus = af->releaseAudioPatch(data->mHandle);
718 }
719 } break;
Eric Laurentb52c1522014-05-20 11:27:36 -0700720 case UPDATE_AUDIOPORT_LIST: {
721 ALOGV("AudioCommandThread() processing update audio port list");
Eric Laurent59a89232014-06-08 14:14:17 -0700722 svc = mService.promote();
Eric Laurentb52c1522014-05-20 11:27:36 -0700723 if (svc == 0) {
724 break;
725 }
726 mLock.unlock();
727 svc->doOnAudioPortListUpdate();
728 mLock.lock();
729 }break;
730 case UPDATE_AUDIOPATCH_LIST: {
731 ALOGV("AudioCommandThread() processing update audio patch list");
Eric Laurent59a89232014-06-08 14:14:17 -0700732 svc = mService.promote();
Eric Laurentb52c1522014-05-20 11:27:36 -0700733 if (svc == 0) {
734 break;
735 }
736 mLock.unlock();
737 svc->doOnAudioPatchListUpdate();
738 mLock.lock();
739 }break;
Eric Laurente1715a42014-05-20 11:30:42 -0700740 case SET_AUDIOPORT_CONFIG: {
741 SetAudioPortConfigData *data = (SetAudioPortConfigData *)command->mParam.get();
742 ALOGV("AudioCommandThread() processing set port config");
743 sp<IAudioFlinger> af = AudioSystem::get_audio_flinger();
744 if (af == 0) {
745 command->mStatus = PERMISSION_DENIED;
746 } else {
747 command->mStatus = af->setAudioPortConfig(&data->mConfig);
748 }
749 } break;
Jean-Michel Trivide801052015-04-14 19:10:14 -0700750 case DYN_POLICY_MIX_STATE_UPDATE: {
751 DynPolicyMixStateUpdateData *data =
752 (DynPolicyMixStateUpdateData *)command->mParam.get();
Jean-Michel Trivide801052015-04-14 19:10:14 -0700753 ALOGV("AudioCommandThread() processing dyn policy mix state update %s %d",
754 data->mRegId.string(), data->mState);
755 svc = mService.promote();
756 if (svc == 0) {
757 break;
758 }
759 mLock.unlock();
760 svc->doOnDynamicPolicyMixStateUpdate(data->mRegId, data->mState);
761 mLock.lock();
762 } break;
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -0800763 case RECORDING_CONFIGURATION_UPDATE: {
764 RecordingConfigurationUpdateData *data =
765 (RecordingConfigurationUpdateData *)command->mParam.get();
766 ALOGV("AudioCommandThread() processing recording configuration update");
767 svc = mService.promote();
768 if (svc == 0) {
769 break;
770 }
771 mLock.unlock();
Jean-Michel Triviac4e4292016-12-22 11:39:31 -0800772 svc->doOnRecordingConfigurationUpdate(data->mEvent, &data->mClientInfo,
773 &data->mClientConfig, &data->mDeviceConfig,
Jean-Michel Trivi8c7cf3b2016-02-25 17:08:24 -0800774 data->mPatchHandle);
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -0800775 mLock.lock();
776 } break;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700777 default:
Steve Block5ff1dd52012-01-05 23:22:43 +0000778 ALOGW("AudioCommandThread() unknown command %d", command->mCommand);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700779 }
Eric Laurent0ede8922014-05-09 18:04:42 -0700780 {
781 Mutex::Autolock _l(command->mLock);
782 if (command->mWaitStatus) {
783 command->mWaitStatus = false;
784 command->mCond.signal();
785 }
786 }
Eric Laurentd7eda8d2016-02-02 17:18:39 -0800787 waitTime = -1;
Zach Janga754b4f2015-10-27 01:29:34 +0000788 // release mLock before releasing strong reference on the service as
789 // AudioPolicyService destructor calls AudioCommandThread::exit() which
790 // acquires mLock.
791 mLock.unlock();
792 svc.clear();
793 mLock.lock();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700794 } else {
795 waitTime = mAudioCommands[0]->mTime - curTime;
796 break;
797 }
798 }
Zach Janga754b4f2015-10-27 01:29:34 +0000799
800 // release delayed commands wake lock if the queue is empty
801 if (mAudioCommands.isEmpty()) {
Ricardo Garcia05f2fdc2014-07-24 15:48:24 -0700802 release_wake_lock(mName.string());
Zach Janga754b4f2015-10-27 01:29:34 +0000803 }
804
805 // At this stage we have either an empty command queue or the first command in the queue
806 // has a finite delay. So unless we are exiting it is safe to wait.
807 if (!exitPending()) {
Eric Laurent59a89232014-06-08 14:14:17 -0700808 ALOGV("AudioCommandThread() going to sleep");
Eric Laurentd7eda8d2016-02-02 17:18:39 -0800809 if (waitTime == -1) {
810 mWaitWorkCV.wait(mLock);
811 } else {
812 mWaitWorkCV.waitRelative(mLock, waitTime);
813 }
Eric Laurent59a89232014-06-08 14:14:17 -0700814 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700815 }
Ricardo Garcia05f2fdc2014-07-24 15:48:24 -0700816 // release delayed commands wake lock before quitting
817 if (!mAudioCommands.isEmpty()) {
818 release_wake_lock(mName.string());
819 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700820 mLock.unlock();
821 return false;
822}
823
824status_t AudioPolicyService::AudioCommandThread::dump(int fd)
825{
826 const size_t SIZE = 256;
827 char buffer[SIZE];
828 String8 result;
829
830 snprintf(buffer, SIZE, "AudioCommandThread %p Dump\n", this);
831 result.append(buffer);
832 write(fd, result.string(), result.size());
833
834 bool locked = tryLock(mLock);
835 if (!locked) {
836 String8 result2(kCmdDeadlockedString);
837 write(fd, result2.string(), result2.size());
838 }
839
840 snprintf(buffer, SIZE, "- Commands:\n");
841 result = String8(buffer);
842 result.append(" Command Time Wait pParam\n");
Glenn Kasten8d6a2442012-02-08 14:04:28 -0800843 for (size_t i = 0; i < mAudioCommands.size(); i++) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700844 mAudioCommands[i]->dump(buffer, SIZE);
845 result.append(buffer);
846 }
847 result.append(" Last Command\n");
Eric Laurent0ede8922014-05-09 18:04:42 -0700848 if (mLastCommand != 0) {
849 mLastCommand->dump(buffer, SIZE);
850 result.append(buffer);
851 } else {
852 result.append(" none\n");
853 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700854
855 write(fd, result.string(), result.size());
856
857 if (locked) mLock.unlock();
858
859 return NO_ERROR;
860}
861
Glenn Kasten3d2f8772012-01-27 15:25:25 -0800862void AudioPolicyService::AudioCommandThread::startToneCommand(ToneGenerator::tone_type type,
863 audio_stream_type_t stream)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700864{
Eric Laurent0ede8922014-05-09 18:04:42 -0700865 sp<AudioCommand> command = new AudioCommand();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700866 command->mCommand = START_TONE;
Eric Laurent0ede8922014-05-09 18:04:42 -0700867 sp<ToneData> data = new ToneData();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700868 data->mType = type;
869 data->mStream = stream;
Jesper Tragardh48412dc2014-03-24 14:12:43 +0100870 command->mParam = data;
Steve Block3856b092011-10-20 11:56:00 +0100871 ALOGV("AudioCommandThread() adding tone start type %d, stream %d", type, stream);
Eric Laurent0ede8922014-05-09 18:04:42 -0700872 sendCommand(command);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700873}
874
875void AudioPolicyService::AudioCommandThread::stopToneCommand()
876{
Eric Laurent0ede8922014-05-09 18:04:42 -0700877 sp<AudioCommand> command = new AudioCommand();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700878 command->mCommand = STOP_TONE;
Steve Block3856b092011-10-20 11:56:00 +0100879 ALOGV("AudioCommandThread() adding tone stop");
Eric Laurent0ede8922014-05-09 18:04:42 -0700880 sendCommand(command);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700881}
882
Glenn Kastenfff6d712012-01-12 16:38:12 -0800883status_t AudioPolicyService::AudioCommandThread::volumeCommand(audio_stream_type_t stream,
Eric Laurentde070132010-07-13 04:45:46 -0700884 float volume,
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800885 audio_io_handle_t output,
Eric Laurentde070132010-07-13 04:45:46 -0700886 int delayMs)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700887{
Eric Laurent0ede8922014-05-09 18:04:42 -0700888 sp<AudioCommand> command = new AudioCommand();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700889 command->mCommand = SET_VOLUME;
Eric Laurent0ede8922014-05-09 18:04:42 -0700890 sp<VolumeData> data = new VolumeData();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700891 data->mStream = stream;
892 data->mVolume = volume;
893 data->mIO = output;
894 command->mParam = data;
Eric Laurent0ede8922014-05-09 18:04:42 -0700895 command->mWaitStatus = true;
Steve Block3856b092011-10-20 11:56:00 +0100896 ALOGV("AudioCommandThread() adding set volume stream %d, volume %f, output %d",
Eric Laurentde070132010-07-13 04:45:46 -0700897 stream, volume, output);
Eric Laurent0ede8922014-05-09 18:04:42 -0700898 return sendCommand(command, delayMs);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700899}
900
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800901status_t AudioPolicyService::AudioCommandThread::parametersCommand(audio_io_handle_t ioHandle,
Dima Zavinfce7a472011-04-19 22:30:36 -0700902 const char *keyValuePairs,
Eric Laurentde070132010-07-13 04:45:46 -0700903 int delayMs)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700904{
Eric Laurent0ede8922014-05-09 18:04:42 -0700905 sp<AudioCommand> command = new AudioCommand();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700906 command->mCommand = SET_PARAMETERS;
Eric Laurent0ede8922014-05-09 18:04:42 -0700907 sp<ParametersData> data = new ParametersData();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700908 data->mIO = ioHandle;
Dima Zavinfce7a472011-04-19 22:30:36 -0700909 data->mKeyValuePairs = String8(keyValuePairs);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700910 command->mParam = data;
Eric Laurent0ede8922014-05-09 18:04:42 -0700911 command->mWaitStatus = true;
Steve Block3856b092011-10-20 11:56:00 +0100912 ALOGV("AudioCommandThread() adding set parameter string %s, io %d ,delay %d",
Dima Zavinfce7a472011-04-19 22:30:36 -0700913 keyValuePairs, ioHandle, delayMs);
Eric Laurent0ede8922014-05-09 18:04:42 -0700914 return sendCommand(command, delayMs);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700915}
916
917status_t AudioPolicyService::AudioCommandThread::voiceVolumeCommand(float volume, int delayMs)
918{
Eric Laurent0ede8922014-05-09 18:04:42 -0700919 sp<AudioCommand> command = new AudioCommand();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700920 command->mCommand = SET_VOICE_VOLUME;
Eric Laurent0ede8922014-05-09 18:04:42 -0700921 sp<VoiceVolumeData> data = new VoiceVolumeData();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700922 data->mVolume = volume;
923 command->mParam = data;
Eric Laurent0ede8922014-05-09 18:04:42 -0700924 command->mWaitStatus = true;
Steve Block3856b092011-10-20 11:56:00 +0100925 ALOGV("AudioCommandThread() adding set voice volume volume %f", volume);
Eric Laurent0ede8922014-05-09 18:04:42 -0700926 return sendCommand(command, delayMs);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700927}
928
Eric Laurentbfb1b832013-01-07 09:53:42 -0800929void AudioPolicyService::AudioCommandThread::stopOutputCommand(audio_io_handle_t output,
930 audio_stream_type_t stream,
Eric Laurente83b55d2014-11-14 10:06:21 -0800931 audio_session_t session)
Eric Laurentbfb1b832013-01-07 09:53:42 -0800932{
Eric Laurent0ede8922014-05-09 18:04:42 -0700933 sp<AudioCommand> command = new AudioCommand();
Eric Laurentbfb1b832013-01-07 09:53:42 -0800934 command->mCommand = STOP_OUTPUT;
Eric Laurent0ede8922014-05-09 18:04:42 -0700935 sp<StopOutputData> data = new StopOutputData();
Eric Laurentbfb1b832013-01-07 09:53:42 -0800936 data->mIO = output;
937 data->mStream = stream;
938 data->mSession = session;
Jesper Tragardh48412dc2014-03-24 14:12:43 +0100939 command->mParam = data;
Eric Laurentbfb1b832013-01-07 09:53:42 -0800940 ALOGV("AudioCommandThread() adding stop output %d", output);
Eric Laurent0ede8922014-05-09 18:04:42 -0700941 sendCommand(command);
Eric Laurentbfb1b832013-01-07 09:53:42 -0800942}
943
Eric Laurente83b55d2014-11-14 10:06:21 -0800944void AudioPolicyService::AudioCommandThread::releaseOutputCommand(audio_io_handle_t output,
945 audio_stream_type_t stream,
946 audio_session_t session)
Eric Laurentbfb1b832013-01-07 09:53:42 -0800947{
Eric Laurent0ede8922014-05-09 18:04:42 -0700948 sp<AudioCommand> command = new AudioCommand();
Eric Laurentbfb1b832013-01-07 09:53:42 -0800949 command->mCommand = RELEASE_OUTPUT;
Eric Laurent0ede8922014-05-09 18:04:42 -0700950 sp<ReleaseOutputData> data = new ReleaseOutputData();
Eric Laurentbfb1b832013-01-07 09:53:42 -0800951 data->mIO = output;
Eric Laurente83b55d2014-11-14 10:06:21 -0800952 data->mStream = stream;
953 data->mSession = session;
Jesper Tragardh48412dc2014-03-24 14:12:43 +0100954 command->mParam = data;
Eric Laurentbfb1b832013-01-07 09:53:42 -0800955 ALOGV("AudioCommandThread() adding release output %d", output);
Eric Laurent0ede8922014-05-09 18:04:42 -0700956 sendCommand(command);
957}
958
Eric Laurent951f4552014-05-20 10:48:17 -0700959status_t AudioPolicyService::AudioCommandThread::createAudioPatchCommand(
960 const struct audio_patch *patch,
961 audio_patch_handle_t *handle,
962 int delayMs)
963{
964 status_t status = NO_ERROR;
965
966 sp<AudioCommand> command = new AudioCommand();
967 command->mCommand = CREATE_AUDIO_PATCH;
968 CreateAudioPatchData *data = new CreateAudioPatchData();
969 data->mPatch = *patch;
970 data->mHandle = *handle;
971 command->mParam = data;
972 command->mWaitStatus = true;
973 ALOGV("AudioCommandThread() adding create patch delay %d", delayMs);
974 status = sendCommand(command, delayMs);
975 if (status == NO_ERROR) {
976 *handle = data->mHandle;
977 }
978 return status;
979}
980
981status_t AudioPolicyService::AudioCommandThread::releaseAudioPatchCommand(audio_patch_handle_t handle,
982 int delayMs)
983{
984 sp<AudioCommand> command = new AudioCommand();
985 command->mCommand = RELEASE_AUDIO_PATCH;
986 ReleaseAudioPatchData *data = new ReleaseAudioPatchData();
987 data->mHandle = handle;
988 command->mParam = data;
989 command->mWaitStatus = true;
990 ALOGV("AudioCommandThread() adding release patch delay %d", delayMs);
991 return sendCommand(command, delayMs);
992}
993
Eric Laurentb52c1522014-05-20 11:27:36 -0700994void AudioPolicyService::AudioCommandThread::updateAudioPortListCommand()
995{
996 sp<AudioCommand> command = new AudioCommand();
997 command->mCommand = UPDATE_AUDIOPORT_LIST;
998 ALOGV("AudioCommandThread() adding update audio port list");
999 sendCommand(command);
1000}
1001
1002void AudioPolicyService::AudioCommandThread::updateAudioPatchListCommand()
1003{
1004 sp<AudioCommand>command = new AudioCommand();
1005 command->mCommand = UPDATE_AUDIOPATCH_LIST;
1006 ALOGV("AudioCommandThread() adding update audio patch list");
1007 sendCommand(command);
1008}
1009
Eric Laurente1715a42014-05-20 11:30:42 -07001010status_t AudioPolicyService::AudioCommandThread::setAudioPortConfigCommand(
1011 const struct audio_port_config *config, int delayMs)
1012{
1013 sp<AudioCommand> command = new AudioCommand();
1014 command->mCommand = SET_AUDIOPORT_CONFIG;
1015 SetAudioPortConfigData *data = new SetAudioPortConfigData();
1016 data->mConfig = *config;
1017 command->mParam = data;
1018 command->mWaitStatus = true;
1019 ALOGV("AudioCommandThread() adding set port config delay %d", delayMs);
1020 return sendCommand(command, delayMs);
1021}
1022
Jean-Michel Trivide801052015-04-14 19:10:14 -07001023void AudioPolicyService::AudioCommandThread::dynamicPolicyMixStateUpdateCommand(
Chih-Hung Hsiehe964d4e2016-08-09 14:31:32 -07001024 const String8& regId, int32_t state)
Jean-Michel Trivide801052015-04-14 19:10:14 -07001025{
1026 sp<AudioCommand> command = new AudioCommand();
1027 command->mCommand = DYN_POLICY_MIX_STATE_UPDATE;
1028 DynPolicyMixStateUpdateData *data = new DynPolicyMixStateUpdateData();
1029 data->mRegId = regId;
1030 data->mState = state;
1031 command->mParam = data;
1032 ALOGV("AudioCommandThread() sending dynamic policy mix (id=%s) state update to %d",
1033 regId.string(), state);
1034 sendCommand(command);
1035}
1036
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -08001037void AudioPolicyService::AudioCommandThread::recordingConfigurationUpdateCommand(
Jean-Michel Triviac4e4292016-12-22 11:39:31 -08001038 int event, const record_client_info_t *clientInfo,
Jean-Michel Trivi8c7cf3b2016-02-25 17:08:24 -08001039 const audio_config_base_t *clientConfig, const audio_config_base_t *deviceConfig,
1040 audio_patch_handle_t patchHandle)
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -08001041{
1042 sp<AudioCommand>command = new AudioCommand();
1043 command->mCommand = RECORDING_CONFIGURATION_UPDATE;
1044 RecordingConfigurationUpdateData *data = new RecordingConfigurationUpdateData();
1045 data->mEvent = event;
Jean-Michel Triviac4e4292016-12-22 11:39:31 -08001046 data->mClientInfo = *clientInfo;
Jean-Michel Trivi7281aa92016-02-17 15:33:40 -08001047 data->mClientConfig = *clientConfig;
1048 data->mDeviceConfig = *deviceConfig;
Jean-Michel Trivi8c7cf3b2016-02-25 17:08:24 -08001049 data->mPatchHandle = patchHandle;
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -08001050 command->mParam = data;
Jean-Michel Triviac4e4292016-12-22 11:39:31 -08001051 ALOGV("AudioCommandThread() adding recording configuration update event %d, source %d uid %u",
1052 event, clientInfo->source, clientInfo->uid);
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -08001053 sendCommand(command);
1054}
1055
Eric Laurent0ede8922014-05-09 18:04:42 -07001056status_t AudioPolicyService::AudioCommandThread::sendCommand(sp<AudioCommand>& command, int delayMs)
1057{
1058 {
1059 Mutex::Autolock _l(mLock);
1060 insertCommand_l(command, delayMs);
1061 mWaitWorkCV.signal();
1062 }
1063 Mutex::Autolock _l(command->mLock);
1064 while (command->mWaitStatus) {
1065 nsecs_t timeOutNs = kAudioCommandTimeoutNs + milliseconds(delayMs);
1066 if (command->mCond.waitRelative(command->mLock, timeOutNs) != NO_ERROR) {
1067 command->mStatus = TIMED_OUT;
1068 command->mWaitStatus = false;
1069 }
1070 }
1071 return command->mStatus;
Eric Laurentbfb1b832013-01-07 09:53:42 -08001072}
1073
Mathias Agopian65ab4712010-07-14 17:59:35 -07001074// insertCommand_l() must be called with mLock held
Eric Laurent0ede8922014-05-09 18:04:42 -07001075void AudioPolicyService::AudioCommandThread::insertCommand_l(sp<AudioCommand>& command, int delayMs)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001076{
Glenn Kasten8d6a2442012-02-08 14:04:28 -08001077 ssize_t i; // not size_t because i will count down to -1
Eric Laurent0ede8922014-05-09 18:04:42 -07001078 Vector < sp<AudioCommand> > removedCommands;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001079 command->mTime = systemTime() + milliseconds(delayMs);
1080
1081 // acquire wake lock to make sure delayed commands are processed
Eric Laurentbfb1b832013-01-07 09:53:42 -08001082 if (mAudioCommands.isEmpty()) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07001083 acquire_wake_lock(PARTIAL_WAKE_LOCK, mName.string());
1084 }
1085
1086 // check same pending commands with later time stamps and eliminate them
Ivan Lozano5ff158f2017-10-30 09:06:24 -07001087 for (i = (ssize_t)mAudioCommands.size()-1; i >= 0; i--) {
Eric Laurent0ede8922014-05-09 18:04:42 -07001088 sp<AudioCommand> command2 = mAudioCommands[i];
Mathias Agopian65ab4712010-07-14 17:59:35 -07001089 // commands are sorted by increasing time stamp: no need to scan the rest of mAudioCommands
1090 if (command2->mTime <= command->mTime) break;
Eric Laurente45b48a2014-09-04 16:40:57 -07001091
1092 // create audio patch or release audio patch commands are equivalent
1093 // with regard to filtering
1094 if ((command->mCommand == CREATE_AUDIO_PATCH) ||
1095 (command->mCommand == RELEASE_AUDIO_PATCH)) {
1096 if ((command2->mCommand != CREATE_AUDIO_PATCH) &&
1097 (command2->mCommand != RELEASE_AUDIO_PATCH)) {
1098 continue;
1099 }
1100 } else if (command2->mCommand != command->mCommand) continue;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001101
1102 switch (command->mCommand) {
1103 case SET_PARAMETERS: {
Eric Laurent0ede8922014-05-09 18:04:42 -07001104 ParametersData *data = (ParametersData *)command->mParam.get();
1105 ParametersData *data2 = (ParametersData *)command2->mParam.get();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001106 if (data->mIO != data2->mIO) break;
Steve Block3856b092011-10-20 11:56:00 +01001107 ALOGV("Comparing parameter command %s to new command %s",
Eric Laurentde070132010-07-13 04:45:46 -07001108 data2->mKeyValuePairs.string(), data->mKeyValuePairs.string());
Mathias Agopian65ab4712010-07-14 17:59:35 -07001109 AudioParameter param = AudioParameter(data->mKeyValuePairs);
1110 AudioParameter param2 = AudioParameter(data2->mKeyValuePairs);
1111 for (size_t j = 0; j < param.size(); j++) {
Glenn Kastene53b9ea2012-03-12 16:29:55 -07001112 String8 key;
1113 String8 value;
1114 param.getAt(j, key, value);
1115 for (size_t k = 0; k < param2.size(); k++) {
1116 String8 key2;
1117 String8 value2;
1118 param2.getAt(k, key2, value2);
1119 if (key2 == key) {
1120 param2.remove(key2);
1121 ALOGV("Filtering out parameter %s", key2.string());
1122 break;
1123 }
1124 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001125 }
1126 // if all keys have been filtered out, remove the command.
1127 // otherwise, update the key value pairs
1128 if (param2.size() == 0) {
1129 removedCommands.add(command2);
1130 } else {
1131 data2->mKeyValuePairs = param2.toString();
1132 }
Eric Laurent21e54562013-09-23 12:08:05 -07001133 command->mTime = command2->mTime;
1134 // force delayMs to non 0 so that code below does not request to wait for
1135 // command status as the command is now delayed
1136 delayMs = 1;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001137 } break;
1138
1139 case SET_VOLUME: {
Eric Laurent0ede8922014-05-09 18:04:42 -07001140 VolumeData *data = (VolumeData *)command->mParam.get();
1141 VolumeData *data2 = (VolumeData *)command2->mParam.get();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001142 if (data->mIO != data2->mIO) break;
1143 if (data->mStream != data2->mStream) break;
Steve Block3856b092011-10-20 11:56:00 +01001144 ALOGV("Filtering out volume command on output %d for stream %d",
Eric Laurentde070132010-07-13 04:45:46 -07001145 data->mIO, data->mStream);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001146 removedCommands.add(command2);
Eric Laurent21e54562013-09-23 12:08:05 -07001147 command->mTime = command2->mTime;
1148 // force delayMs to non 0 so that code below does not request to wait for
1149 // command status as the command is now delayed
1150 delayMs = 1;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001151 } break;
Eric Laurente45b48a2014-09-04 16:40:57 -07001152
Eric Laurentbaf35fe2016-07-27 15:36:53 -07001153 case SET_VOICE_VOLUME: {
1154 VoiceVolumeData *data = (VoiceVolumeData *)command->mParam.get();
1155 VoiceVolumeData *data2 = (VoiceVolumeData *)command2->mParam.get();
1156 ALOGV("Filtering out voice volume command value %f replaced by %f",
1157 data2->mVolume, data->mVolume);
1158 removedCommands.add(command2);
1159 command->mTime = command2->mTime;
1160 // force delayMs to non 0 so that code below does not request to wait for
1161 // command status as the command is now delayed
1162 delayMs = 1;
1163 } break;
1164
Eric Laurente45b48a2014-09-04 16:40:57 -07001165 case CREATE_AUDIO_PATCH:
1166 case RELEASE_AUDIO_PATCH: {
1167 audio_patch_handle_t handle;
Haynes Mathew Georgea2d4a6d2014-10-13 13:05:22 -07001168 struct audio_patch patch;
Eric Laurente45b48a2014-09-04 16:40:57 -07001169 if (command->mCommand == CREATE_AUDIO_PATCH) {
1170 handle = ((CreateAudioPatchData *)command->mParam.get())->mHandle;
Haynes Mathew Georgea2d4a6d2014-10-13 13:05:22 -07001171 patch = ((CreateAudioPatchData *)command->mParam.get())->mPatch;
Eric Laurente45b48a2014-09-04 16:40:57 -07001172 } else {
1173 handle = ((ReleaseAudioPatchData *)command->mParam.get())->mHandle;
1174 }
1175 audio_patch_handle_t handle2;
Haynes Mathew Georgea2d4a6d2014-10-13 13:05:22 -07001176 struct audio_patch patch2;
Eric Laurente45b48a2014-09-04 16:40:57 -07001177 if (command2->mCommand == CREATE_AUDIO_PATCH) {
1178 handle2 = ((CreateAudioPatchData *)command2->mParam.get())->mHandle;
Haynes Mathew Georgea2d4a6d2014-10-13 13:05:22 -07001179 patch2 = ((CreateAudioPatchData *)command2->mParam.get())->mPatch;
Eric Laurente45b48a2014-09-04 16:40:57 -07001180 } else {
1181 handle2 = ((ReleaseAudioPatchData *)command2->mParam.get())->mHandle;
Glenn Kastenf60b6b62015-07-06 10:53:26 -07001182 memset(&patch2, 0, sizeof(patch2));
Eric Laurente45b48a2014-09-04 16:40:57 -07001183 }
1184 if (handle != handle2) break;
Haynes Mathew Georgea2d4a6d2014-10-13 13:05:22 -07001185 /* Filter CREATE_AUDIO_PATCH commands only when they are issued for
1186 same output. */
1187 if( (command->mCommand == CREATE_AUDIO_PATCH) &&
1188 (command2->mCommand == CREATE_AUDIO_PATCH) ) {
1189 bool isOutputDiff = false;
1190 if (patch.num_sources == patch2.num_sources) {
1191 for (unsigned count = 0; count < patch.num_sources; count++) {
1192 if (patch.sources[count].id != patch2.sources[count].id) {
1193 isOutputDiff = true;
1194 break;
1195 }
1196 }
1197 if (isOutputDiff)
1198 break;
1199 }
1200 }
Eric Laurente45b48a2014-09-04 16:40:57 -07001201 ALOGV("Filtering out %s audio patch command for handle %d",
1202 (command->mCommand == CREATE_AUDIO_PATCH) ? "create" : "release", handle);
1203 removedCommands.add(command2);
1204 command->mTime = command2->mTime;
1205 // force delayMs to non 0 so that code below does not request to wait for
1206 // command status as the command is now delayed
1207 delayMs = 1;
1208 } break;
1209
Jean-Michel Trivide801052015-04-14 19:10:14 -07001210 case DYN_POLICY_MIX_STATE_UPDATE: {
1211
1212 } break;
1213
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -08001214 case RECORDING_CONFIGURATION_UPDATE: {
1215
1216 } break;
1217
Mathias Agopian65ab4712010-07-14 17:59:35 -07001218 case START_TONE:
1219 case STOP_TONE:
1220 default:
1221 break;
1222 }
1223 }
1224
1225 // remove filtered commands
1226 for (size_t j = 0; j < removedCommands.size(); j++) {
1227 // removed commands always have time stamps greater than current command
1228 for (size_t k = i + 1; k < mAudioCommands.size(); k++) {
Eric Laurent0ede8922014-05-09 18:04:42 -07001229 if (mAudioCommands[k].get() == removedCommands[j].get()) {
Steve Block3856b092011-10-20 11:56:00 +01001230 ALOGV("suppressing command: %d", mAudioCommands[k]->mCommand);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001231 mAudioCommands.removeAt(k);
1232 break;
1233 }
1234 }
1235 }
1236 removedCommands.clear();
1237
Eric Laurentaa79bef2015-01-15 14:33:51 -08001238 // Disable wait for status if delay is not 0.
1239 // Except for create audio patch command because the returned patch handle
1240 // is needed by audio policy manager
1241 if (delayMs != 0 && command->mCommand != CREATE_AUDIO_PATCH) {
Eric Laurentcec4abb2012-07-03 12:23:02 -07001242 command->mWaitStatus = false;
1243 }
Eric Laurentcec4abb2012-07-03 12:23:02 -07001244
Mathias Agopian65ab4712010-07-14 17:59:35 -07001245 // insert command at the right place according to its time stamp
Eric Laurent1e693b52014-07-09 15:03:28 -07001246 ALOGV("inserting command: %d at index %zd, num commands %zu",
1247 command->mCommand, i+1, mAudioCommands.size());
Mathias Agopian65ab4712010-07-14 17:59:35 -07001248 mAudioCommands.insertAt(command, i + 1);
1249}
1250
1251void AudioPolicyService::AudioCommandThread::exit()
1252{
Steve Block3856b092011-10-20 11:56:00 +01001253 ALOGV("AudioCommandThread::exit");
Mathias Agopian65ab4712010-07-14 17:59:35 -07001254 {
1255 AutoMutex _l(mLock);
1256 requestExit();
1257 mWaitWorkCV.signal();
1258 }
Zach Janga754b4f2015-10-27 01:29:34 +00001259 // Note that we can call it from the thread loop if all other references have been released
1260 // but it will safely return WOULD_BLOCK in this case
Mathias Agopian65ab4712010-07-14 17:59:35 -07001261 requestExitAndWait();
1262}
1263
1264void AudioPolicyService::AudioCommandThread::AudioCommand::dump(char* buffer, size_t size)
1265{
1266 snprintf(buffer, size, " %02d %06d.%03d %01u %p\n",
1267 mCommand,
1268 (int)ns2s(mTime),
1269 (int)ns2ms(mTime)%1000,
1270 mWaitStatus,
Eric Laurent0ede8922014-05-09 18:04:42 -07001271 mParam.get());
Mathias Agopian65ab4712010-07-14 17:59:35 -07001272}
1273
Dima Zavinfce7a472011-04-19 22:30:36 -07001274/******* helpers for the service_ops callbacks defined below *********/
1275void AudioPolicyService::setParameters(audio_io_handle_t ioHandle,
1276 const char *keyValuePairs,
1277 int delayMs)
1278{
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001279 mAudioCommandThread->parametersCommand(ioHandle, keyValuePairs,
Dima Zavinfce7a472011-04-19 22:30:36 -07001280 delayMs);
1281}
1282
1283int AudioPolicyService::setStreamVolume(audio_stream_type_t stream,
1284 float volume,
1285 audio_io_handle_t output,
1286 int delayMs)
1287{
Glenn Kastenfff6d712012-01-12 16:38:12 -08001288 return (int)mAudioCommandThread->volumeCommand(stream, volume,
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001289 output, delayMs);
Dima Zavinfce7a472011-04-19 22:30:36 -07001290}
1291
1292int AudioPolicyService::startTone(audio_policy_tone_t tone,
1293 audio_stream_type_t stream)
1294{
Glenn Kasten6e2ebe92013-08-13 09:14:51 -07001295 if (tone != AUDIO_POLICY_TONE_IN_CALL_NOTIFICATION) {
Steve Block29357bc2012-01-06 19:20:56 +00001296 ALOGE("startTone: illegal tone requested (%d)", tone);
Glenn Kasten6e2ebe92013-08-13 09:14:51 -07001297 }
1298 if (stream != AUDIO_STREAM_VOICE_CALL) {
Steve Block29357bc2012-01-06 19:20:56 +00001299 ALOGE("startTone: illegal stream (%d) requested for tone %d", stream,
Glenn Kastene53b9ea2012-03-12 16:29:55 -07001300 tone);
Glenn Kasten6e2ebe92013-08-13 09:14:51 -07001301 }
Dima Zavinfce7a472011-04-19 22:30:36 -07001302 mTonePlaybackThread->startToneCommand(ToneGenerator::TONE_SUP_CALL_WAITING,
1303 AUDIO_STREAM_VOICE_CALL);
1304 return 0;
1305}
1306
1307int AudioPolicyService::stopTone()
1308{
1309 mTonePlaybackThread->stopToneCommand();
1310 return 0;
1311}
1312
1313int AudioPolicyService::setVoiceVolume(float volume, int delayMs)
1314{
1315 return (int)mAudioCommandThread->voiceVolumeCommand(volume, delayMs);
1316}
1317
Dima Zavinfce7a472011-04-19 22:30:36 -07001318extern "C" {
Eric Laurent2d388ec2014-03-07 13:25:54 -08001319audio_module_handle_t aps_load_hw_module(void *service __unused,
1320 const char *name);
1321audio_io_handle_t aps_open_output(void *service __unused,
Eric Laurenta4c5a552012-03-29 10:12:40 -07001322 audio_devices_t *pDevices,
1323 uint32_t *pSamplingRate,
1324 audio_format_t *pFormat,
1325 audio_channel_mask_t *pChannelMask,
1326 uint32_t *pLatencyMs,
Eric Laurent2d388ec2014-03-07 13:25:54 -08001327 audio_output_flags_t flags);
Eric Laurenta4c5a552012-03-29 10:12:40 -07001328
Eric Laurent2d388ec2014-03-07 13:25:54 -08001329audio_io_handle_t aps_open_output_on_module(void *service __unused,
Eric Laurenta4c5a552012-03-29 10:12:40 -07001330 audio_module_handle_t module,
1331 audio_devices_t *pDevices,
1332 uint32_t *pSamplingRate,
1333 audio_format_t *pFormat,
1334 audio_channel_mask_t *pChannelMask,
1335 uint32_t *pLatencyMs,
Richard Fitzgeraldad3af332013-03-25 16:54:37 +00001336 audio_output_flags_t flags,
Eric Laurent2d388ec2014-03-07 13:25:54 -08001337 const audio_offload_info_t *offloadInfo);
1338audio_io_handle_t aps_open_dup_output(void *service __unused,
Dima Zavinfce7a472011-04-19 22:30:36 -07001339 audio_io_handle_t output1,
Eric Laurent2d388ec2014-03-07 13:25:54 -08001340 audio_io_handle_t output2);
1341int aps_close_output(void *service __unused, audio_io_handle_t output);
1342int aps_suspend_output(void *service __unused, audio_io_handle_t output);
1343int aps_restore_output(void *service __unused, audio_io_handle_t output);
1344audio_io_handle_t aps_open_input(void *service __unused,
Eric Laurenta4c5a552012-03-29 10:12:40 -07001345 audio_devices_t *pDevices,
1346 uint32_t *pSamplingRate,
1347 audio_format_t *pFormat,
1348 audio_channel_mask_t *pChannelMask,
Eric Laurent2d388ec2014-03-07 13:25:54 -08001349 audio_in_acoustics_t acoustics __unused);
1350audio_io_handle_t aps_open_input_on_module(void *service __unused,
Eric Laurenta4c5a552012-03-29 10:12:40 -07001351 audio_module_handle_t module,
1352 audio_devices_t *pDevices,
1353 uint32_t *pSamplingRate,
1354 audio_format_t *pFormat,
Eric Laurent2d388ec2014-03-07 13:25:54 -08001355 audio_channel_mask_t *pChannelMask);
1356int aps_close_input(void *service __unused, audio_io_handle_t input);
1357int aps_invalidate_stream(void *service __unused, audio_stream_type_t stream);
Glenn Kastend848eb42016-03-08 13:42:11 -08001358int aps_move_effects(void *service __unused, audio_session_t session,
Dima Zavinfce7a472011-04-19 22:30:36 -07001359 audio_io_handle_t src_output,
Eric Laurent2d388ec2014-03-07 13:25:54 -08001360 audio_io_handle_t dst_output);
1361char * aps_get_parameters(void *service __unused, audio_io_handle_t io_handle,
1362 const char *keys);
1363void aps_set_parameters(void *service, audio_io_handle_t io_handle,
1364 const char *kv_pairs, int delay_ms);
1365int aps_set_stream_volume(void *service, audio_stream_type_t stream,
Dima Zavinfce7a472011-04-19 22:30:36 -07001366 float volume, audio_io_handle_t output,
Eric Laurent2d388ec2014-03-07 13:25:54 -08001367 int delay_ms);
1368int aps_start_tone(void *service, audio_policy_tone_t tone,
1369 audio_stream_type_t stream);
1370int aps_stop_tone(void *service);
1371int aps_set_voice_volume(void *service, float volume, int delay_ms);
1372};
Dima Zavinfce7a472011-04-19 22:30:36 -07001373
Mikhail Naganov1b2a7942017-12-08 10:18:09 -08001374} // namespace android