blob: 619e5c1a8d79b33f538a0a80a0383afc87deefe0 [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>
31#include <utils/String16.h>
32#include <utils/threads.h>
33#include "AudioPolicyService.h"
Glenn Kasten44deb052012-02-05 18:09:08 -080034#include "ServiceUtilities.h"
Mathias Agopian65ab4712010-07-14 17:59:35 -070035#include <hardware_legacy/power.h>
Eric Laurent7c7f10b2011-06-17 21:29:58 -070036#include <media/AudioEffect.h>
37#include <media/EffectsFactoryApi.h>
Chih-Hung Hsiehc84d9d22014-11-14 13:33:34 -080038#include <media/AudioParameter.h>
Mathias Agopian65ab4712010-07-14 17:59:35 -070039
Dima Zavinfce7a472011-04-19 22:30:36 -070040#include <hardware/hardware.h>
Dima Zavin64760242011-05-11 14:15:23 -070041#include <system/audio.h>
Dima Zavin7394a4f2011-06-13 18:16:26 -070042#include <system/audio_policy.h>
Dima Zavinfce7a472011-04-19 22:30:36 -070043#include <hardware/audio_policy.h>
Dima Zavinfce7a472011-04-19 22:30:36 -070044
Mathias Agopian65ab4712010-07-14 17:59:35 -070045namespace android {
46
Glenn Kasten8dad0e32012-01-09 08:41:22 -080047static const char kDeadlockedString[] = "AudioPolicyService may be deadlocked\n";
48static const char kCmdDeadlockedString[] = "AudioPolicyService command thread may be deadlocked\n";
Mathias Agopian65ab4712010-07-14 17:59:35 -070049
50static const int kDumpLockRetries = 50;
Glenn Kasten22ecc912012-01-09 08:33:38 -080051static const int kDumpLockSleepUs = 20000;
Mathias Agopian65ab4712010-07-14 17:59:35 -070052
Eric Laurent0ede8922014-05-09 18:04:42 -070053static const nsecs_t kAudioCommandTimeoutNs = seconds(3); // 3 seconds
Christer Fletcher5fa8c4b2013-01-18 15:27:03 +010054
Dima Zavinfce7a472011-04-19 22:30:36 -070055namespace {
56 extern struct audio_policy_service_ops aps_ops;
57};
58
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{
Mathias Agopian65ab4712010-07-14 17:59:35 -070069 char value[PROPERTY_VALUE_MAX];
Dima Zavinfce7a472011-04-19 22:30:36 -070070 const struct hw_module_t *module;
71 int forced_val;
72 int rc;
Mathias Agopian65ab4712010-07-14 17:59:35 -070073
Eric Laurent8b1e80b2014-10-07 09:08:47 -070074 {
75 Mutex::Autolock _l(mLock);
Eric Laurent93575202011-01-18 18:39:02 -080076
Eric Laurent8b1e80b2014-10-07 09:08:47 -070077 // start tone playback thread
78 mTonePlaybackThread = new AudioCommandThread(String8("ApmTone"), this);
79 // start audio commands thread
80 mAudioCommandThread = new AudioCommandThread(String8("ApmAudio"), this);
81 // start output activity command thread
82 mOutputCommandThread = new AudioCommandThread(String8("ApmOutput"), this);
Eric Laurentdce54a12014-03-10 12:19:46 -070083
84#ifdef USE_LEGACY_AUDIO_POLICY
Eric Laurent8b1e80b2014-10-07 09:08:47 -070085 ALOGI("AudioPolicyService CSTOR in legacy mode");
Eric Laurentdce54a12014-03-10 12:19:46 -070086
Eric Laurent8b1e80b2014-10-07 09:08:47 -070087 /* instantiate the audio policy manager */
88 rc = hw_get_module(AUDIO_POLICY_HARDWARE_MODULE_ID, &module);
89 if (rc) {
90 return;
91 }
92 rc = audio_policy_dev_open(module, &mpAudioPolicyDev);
93 ALOGE_IF(rc, "couldn't open audio policy device (%s)", strerror(-rc));
94 if (rc) {
95 return;
96 }
Eric Laurent93575202011-01-18 18:39:02 -080097
Eric Laurent8b1e80b2014-10-07 09:08:47 -070098 rc = mpAudioPolicyDev->create_audio_policy(mpAudioPolicyDev, &aps_ops, this,
99 &mpAudioPolicy);
100 ALOGE_IF(rc, "couldn't create audio policy (%s)", strerror(-rc));
101 if (rc) {
102 return;
103 }
Dima Zavinfce7a472011-04-19 22:30:36 -0700104
Eric Laurent8b1e80b2014-10-07 09:08:47 -0700105 rc = mpAudioPolicy->init_check(mpAudioPolicy);
106 ALOGE_IF(rc, "couldn't init_check the audio policy (%s)", strerror(-rc));
107 if (rc) {
108 return;
109 }
110 ALOGI("Loaded audio policy from %s (%s)", module->name, module->id);
Eric Laurentdce54a12014-03-10 12:19:46 -0700111#else
Eric Laurent8b1e80b2014-10-07 09:08:47 -0700112 ALOGI("AudioPolicyService CSTOR in new mode");
Eric Laurentdce54a12014-03-10 12:19:46 -0700113
Eric Laurent8b1e80b2014-10-07 09:08:47 -0700114 mAudioPolicyClient = new AudioPolicyClient(this);
115 mAudioPolicyManager = createAudioPolicyManager(mAudioPolicyClient);
Eric Laurentdce54a12014-03-10 12:19:46 -0700116#endif
Eric Laurent8b1e80b2014-10-07 09:08:47 -0700117 }
bryant_liuba2b4392014-06-11 16:49:30 +0800118 // load audio processing modules
Eric Laurent8b1e80b2014-10-07 09:08:47 -0700119 sp<AudioPolicyEffects>audioPolicyEffects = new AudioPolicyEffects();
120 {
121 Mutex::Autolock _l(mLock);
122 mAudioPolicyEffects = audioPolicyEffects;
123 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700124}
125
126AudioPolicyService::~AudioPolicyService()
127{
128 mTonePlaybackThread->exit();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700129 mAudioCommandThread->exit();
Eric Laurent657ff612014-05-07 11:58:24 -0700130 mOutputCommandThread->exit();
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700131
Eric Laurentdce54a12014-03-10 12:19:46 -0700132#ifdef USE_LEGACY_AUDIO_POLICY
Glenn Kasten6e2ebe92013-08-13 09:14:51 -0700133 if (mpAudioPolicy != NULL && mpAudioPolicyDev != NULL) {
Dima Zavinfce7a472011-04-19 22:30:36 -0700134 mpAudioPolicyDev->destroy_audio_policy(mpAudioPolicyDev, mpAudioPolicy);
Glenn Kasten6e2ebe92013-08-13 09:14:51 -0700135 }
136 if (mpAudioPolicyDev != NULL) {
Dima Zavinfce7a472011-04-19 22:30:36 -0700137 audio_policy_dev_close(mpAudioPolicyDev);
Glenn Kasten6e2ebe92013-08-13 09:14:51 -0700138 }
Eric Laurentdce54a12014-03-10 12:19:46 -0700139#else
Eric Laurentf269b8e2014-06-09 20:01:29 -0700140 destroyAudioPolicyManager(mAudioPolicyManager);
Eric Laurentdce54a12014-03-10 12:19:46 -0700141 delete mAudioPolicyClient;
142#endif
Eric Laurentb52c1522014-05-20 11:27:36 -0700143
144 mNotificationClients.clear();
bryant_liuba2b4392014-06-11 16:49:30 +0800145 mAudioPolicyEffects.clear();
Eric Laurentb52c1522014-05-20 11:27:36 -0700146}
147
148// A notification client is always registered by AudioSystem when the client process
149// connects to AudioPolicyService.
150void AudioPolicyService::registerClient(const sp<IAudioPolicyServiceClient>& client)
151{
Eric Laurent12590252015-08-21 18:40:20 -0700152 if (client == 0) {
153 ALOGW("%s got NULL client", __FUNCTION__);
154 return;
155 }
Eric Laurent0ebd5f92014-11-19 19:04:52 -0800156 Mutex::Autolock _l(mNotificationClientsLock);
Eric Laurentb52c1522014-05-20 11:27:36 -0700157
158 uid_t uid = IPCThreadState::self()->getCallingUid();
159 if (mNotificationClients.indexOfKey(uid) < 0) {
160 sp<NotificationClient> notificationClient = new NotificationClient(this,
161 client,
162 uid);
163 ALOGV("registerClient() client %p, uid %d", client.get(), uid);
164
165 mNotificationClients.add(uid, notificationClient);
166
Marco Nelissenf8880202014-11-14 07:58:25 -0800167 sp<IBinder> binder = IInterface::asBinder(client);
Eric Laurentb52c1522014-05-20 11:27:36 -0700168 binder->linkToDeath(notificationClient);
169 }
170}
171
Eric Laurente8726fe2015-06-26 09:39:24 -0700172void AudioPolicyService::setAudioPortCallbacksEnabled(bool enabled)
173{
174 Mutex::Autolock _l(mNotificationClientsLock);
175
176 uid_t uid = IPCThreadState::self()->getCallingUid();
177 if (mNotificationClients.indexOfKey(uid) < 0) {
178 return;
179 }
180 mNotificationClients.valueFor(uid)->setAudioPortCallbacksEnabled(enabled);
181}
182
Eric Laurentb52c1522014-05-20 11:27:36 -0700183// removeNotificationClient() is called when the client process dies.
184void AudioPolicyService::removeNotificationClient(uid_t uid)
185{
Eric Laurent0ebd5f92014-11-19 19:04:52 -0800186 {
187 Mutex::Autolock _l(mNotificationClientsLock);
188 mNotificationClients.removeItem(uid);
189 }
Eric Laurentb52c1522014-05-20 11:27:36 -0700190#ifndef USE_LEGACY_AUDIO_POLICY
Eric Laurent0ebd5f92014-11-19 19:04:52 -0800191 {
192 Mutex::Autolock _l(mLock);
Eric Laurentb52c1522014-05-20 11:27:36 -0700193 if (mAudioPolicyManager) {
Eric Laurent8c7e6da2015-04-21 17:37:00 -0700194 mAudioPolicyManager->releaseResourcesForUid(uid);
Eric Laurentb52c1522014-05-20 11:27:36 -0700195 }
Eric Laurent0ebd5f92014-11-19 19:04:52 -0800196 }
Eric Laurentb52c1522014-05-20 11:27:36 -0700197#endif
198}
199
200void AudioPolicyService::onAudioPortListUpdate()
201{
202 mOutputCommandThread->updateAudioPortListCommand();
203}
204
205void AudioPolicyService::doOnAudioPortListUpdate()
206{
Eric Laurent0ebd5f92014-11-19 19:04:52 -0800207 Mutex::Autolock _l(mNotificationClientsLock);
Eric Laurentb52c1522014-05-20 11:27:36 -0700208 for (size_t i = 0; i < mNotificationClients.size(); i++) {
209 mNotificationClients.valueAt(i)->onAudioPortListUpdate();
210 }
211}
212
213void AudioPolicyService::onAudioPatchListUpdate()
214{
215 mOutputCommandThread->updateAudioPatchListCommand();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700216}
217
Eric Laurent951f4552014-05-20 10:48:17 -0700218status_t AudioPolicyService::clientCreateAudioPatch(const struct audio_patch *patch,
219 audio_patch_handle_t *handle,
220 int delayMs)
221{
222 return mAudioCommandThread->createAudioPatchCommand(patch, handle, delayMs);
223}
224
225status_t AudioPolicyService::clientReleaseAudioPatch(audio_patch_handle_t handle,
226 int delayMs)
227{
228 return mAudioCommandThread->releaseAudioPatchCommand(handle, delayMs);
229}
230
Eric Laurentb52c1522014-05-20 11:27:36 -0700231void AudioPolicyService::doOnAudioPatchListUpdate()
232{
Eric Laurent0ebd5f92014-11-19 19:04:52 -0800233 Mutex::Autolock _l(mNotificationClientsLock);
Eric Laurentb52c1522014-05-20 11:27:36 -0700234 for (size_t i = 0; i < mNotificationClients.size(); i++) {
235 mNotificationClients.valueAt(i)->onAudioPatchListUpdate();
236 }
237}
238
Jean-Michel Trivide801052015-04-14 19:10:14 -0700239void AudioPolicyService::onDynamicPolicyMixStateUpdate(String8 regId, int32_t state)
240{
241 ALOGV("AudioPolicyService::onDynamicPolicyMixStateUpdate(%s, %d)",
242 regId.string(), state);
243 mOutputCommandThread->dynamicPolicyMixStateUpdateCommand(regId, state);
244}
245
246void AudioPolicyService::doOnDynamicPolicyMixStateUpdate(String8 regId, int32_t state)
247{
248 Mutex::Autolock _l(mNotificationClientsLock);
249 for (size_t i = 0; i < mNotificationClients.size(); i++) {
250 mNotificationClients.valueAt(i)->onDynamicPolicyMixStateUpdate(regId, state);
251 }
252}
253
Eric Laurente1715a42014-05-20 11:30:42 -0700254status_t AudioPolicyService::clientSetAudioPortConfig(const struct audio_port_config *config,
255 int delayMs)
256{
257 return mAudioCommandThread->setAudioPortConfigCommand(config, delayMs);
258}
259
Eric Laurentb52c1522014-05-20 11:27:36 -0700260AudioPolicyService::NotificationClient::NotificationClient(const sp<AudioPolicyService>& service,
261 const sp<IAudioPolicyServiceClient>& client,
262 uid_t uid)
Eric Laurente8726fe2015-06-26 09:39:24 -0700263 : mService(service), mUid(uid), mAudioPolicyServiceClient(client),
264 mAudioPortCallbacksEnabled(false)
Eric Laurentb52c1522014-05-20 11:27:36 -0700265{
266}
267
268AudioPolicyService::NotificationClient::~NotificationClient()
269{
270}
271
272void AudioPolicyService::NotificationClient::binderDied(const wp<IBinder>& who __unused)
273{
274 sp<NotificationClient> keep(this);
275 sp<AudioPolicyService> service = mService.promote();
276 if (service != 0) {
277 service->removeNotificationClient(mUid);
278 }
279}
280
281void AudioPolicyService::NotificationClient::onAudioPortListUpdate()
282{
Eric Laurente8726fe2015-06-26 09:39:24 -0700283 if (mAudioPolicyServiceClient != 0 && mAudioPortCallbacksEnabled) {
Eric Laurentb52c1522014-05-20 11:27:36 -0700284 mAudioPolicyServiceClient->onAudioPortListUpdate();
285 }
286}
287
288void AudioPolicyService::NotificationClient::onAudioPatchListUpdate()
289{
Eric Laurente8726fe2015-06-26 09:39:24 -0700290 if (mAudioPolicyServiceClient != 0 && mAudioPortCallbacksEnabled) {
Eric Laurentb52c1522014-05-20 11:27:36 -0700291 mAudioPolicyServiceClient->onAudioPatchListUpdate();
292 }
293}
Eric Laurent57dae992011-07-24 13:36:09 -0700294
Jean-Michel Trivide801052015-04-14 19:10:14 -0700295void AudioPolicyService::NotificationClient::onDynamicPolicyMixStateUpdate(
296 String8 regId, int32_t state)
297{
298 if (mAudioPolicyServiceClient != 0) {
299 mAudioPolicyServiceClient->onDynamicPolicyMixStateUpdate(regId, state);
300 }
301}
302
Eric Laurente8726fe2015-06-26 09:39:24 -0700303void AudioPolicyService::NotificationClient::setAudioPortCallbacksEnabled(bool enabled)
304{
305 mAudioPortCallbacksEnabled = enabled;
306}
307
308
Mathias Agopian65ab4712010-07-14 17:59:35 -0700309void AudioPolicyService::binderDied(const wp<IBinder>& who) {
Glenn Kasten411e4472012-11-02 10:00:06 -0700310 ALOGW("binderDied() %p, calling pid %d", who.unsafe_get(),
Eric Laurentde070132010-07-13 04:45:46 -0700311 IPCThreadState::self()->getCallingPid());
Mathias Agopian65ab4712010-07-14 17:59:35 -0700312}
313
314static bool tryLock(Mutex& mutex)
315{
316 bool locked = false;
317 for (int i = 0; i < kDumpLockRetries; ++i) {
318 if (mutex.tryLock() == NO_ERROR) {
319 locked = true;
320 break;
321 }
Glenn Kasten22ecc912012-01-09 08:33:38 -0800322 usleep(kDumpLockSleepUs);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700323 }
324 return locked;
325}
326
327status_t AudioPolicyService::dumpInternals(int fd)
328{
329 const size_t SIZE = 256;
330 char buffer[SIZE];
331 String8 result;
332
Eric Laurentdce54a12014-03-10 12:19:46 -0700333#ifdef USE_LEGACY_AUDIO_POLICY
Dima Zavinfce7a472011-04-19 22:30:36 -0700334 snprintf(buffer, SIZE, "PolicyManager Interface: %p\n", mpAudioPolicy);
Eric Laurentdce54a12014-03-10 12:19:46 -0700335#else
336 snprintf(buffer, SIZE, "AudioPolicyManager: %p\n", mAudioPolicyManager);
337#endif
Mathias Agopian65ab4712010-07-14 17:59:35 -0700338 result.append(buffer);
339 snprintf(buffer, SIZE, "Command Thread: %p\n", mAudioCommandThread.get());
340 result.append(buffer);
341 snprintf(buffer, SIZE, "Tones Thread: %p\n", mTonePlaybackThread.get());
342 result.append(buffer);
343
344 write(fd, result.string(), result.size());
345 return NO_ERROR;
346}
347
Glenn Kasten0f11b512014-01-31 16:18:54 -0800348status_t AudioPolicyService::dump(int fd, const Vector<String16>& args __unused)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700349{
Glenn Kasten44deb052012-02-05 18:09:08 -0800350 if (!dumpAllowed()) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700351 dumpPermissionDenial(fd);
352 } else {
353 bool locked = tryLock(mLock);
354 if (!locked) {
355 String8 result(kDeadlockedString);
356 write(fd, result.string(), result.size());
357 }
358
359 dumpInternals(fd);
Glenn Kasten9d1f02d2012-02-08 17:47:58 -0800360 if (mAudioCommandThread != 0) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700361 mAudioCommandThread->dump(fd);
362 }
Glenn Kasten9d1f02d2012-02-08 17:47:58 -0800363 if (mTonePlaybackThread != 0) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700364 mTonePlaybackThread->dump(fd);
365 }
366
Eric Laurentdce54a12014-03-10 12:19:46 -0700367#ifdef USE_LEGACY_AUDIO_POLICY
Dima Zavinfce7a472011-04-19 22:30:36 -0700368 if (mpAudioPolicy) {
369 mpAudioPolicy->dump(mpAudioPolicy, fd);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700370 }
Eric Laurentdce54a12014-03-10 12:19:46 -0700371#else
372 if (mAudioPolicyManager) {
373 mAudioPolicyManager->dump(fd);
374 }
375#endif
Mathias Agopian65ab4712010-07-14 17:59:35 -0700376
377 if (locked) mLock.unlock();
378 }
379 return NO_ERROR;
380}
381
382status_t AudioPolicyService::dumpPermissionDenial(int fd)
383{
384 const size_t SIZE = 256;
385 char buffer[SIZE];
386 String8 result;
387 snprintf(buffer, SIZE, "Permission Denial: "
388 "can't dump AudioPolicyService from pid=%d, uid=%d\n",
389 IPCThreadState::self()->getCallingPid(),
390 IPCThreadState::self()->getCallingUid());
391 result.append(buffer);
392 write(fd, result.string(), result.size());
393 return NO_ERROR;
394}
395
396status_t AudioPolicyService::onTransact(
397 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
398{
399 return BnAudioPolicyService::onTransact(code, data, reply, flags);
400}
401
402
Mathias Agopian65ab4712010-07-14 17:59:35 -0700403// ----------- AudioPolicyService::AudioCommandThread implementation ----------
404
Eric Laurentbfb1b832013-01-07 09:53:42 -0800405AudioPolicyService::AudioCommandThread::AudioCommandThread(String8 name,
406 const wp<AudioPolicyService>& service)
407 : Thread(false), mName(name), mService(service)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700408{
409 mpToneGenerator = NULL;
410}
411
412
413AudioPolicyService::AudioCommandThread::~AudioCommandThread()
414{
Eric Laurentbfb1b832013-01-07 09:53:42 -0800415 if (!mAudioCommands.isEmpty()) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700416 release_wake_lock(mName.string());
417 }
418 mAudioCommands.clear();
Glenn Kastene9dd0172012-01-27 18:08:45 -0800419 delete mpToneGenerator;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700420}
421
422void AudioPolicyService::AudioCommandThread::onFirstRef()
423{
Eric Laurentbfb1b832013-01-07 09:53:42 -0800424 run(mName.string(), ANDROID_PRIORITY_AUDIO);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700425}
426
427bool AudioPolicyService::AudioCommandThread::threadLoop()
428{
429 nsecs_t waitTime = INT64_MAX;
430
431 mLock.lock();
432 while (!exitPending())
433 {
Eric Laurent59a89232014-06-08 14:14:17 -0700434 sp<AudioPolicyService> svc;
435 while (!mAudioCommands.isEmpty() && !exitPending()) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700436 nsecs_t curTime = systemTime();
437 // commands are sorted by increasing time stamp: execute them from index 0 and up
438 if (mAudioCommands[0]->mTime <= curTime) {
Eric Laurent0ede8922014-05-09 18:04:42 -0700439 sp<AudioCommand> command = mAudioCommands[0];
Mathias Agopian65ab4712010-07-14 17:59:35 -0700440 mAudioCommands.removeAt(0);
Eric Laurent0ede8922014-05-09 18:04:42 -0700441 mLastCommand = command;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700442
443 switch (command->mCommand) {
444 case START_TONE: {
445 mLock.unlock();
Eric Laurent0ede8922014-05-09 18:04:42 -0700446 ToneData *data = (ToneData *)command->mParam.get();
Steve Block3856b092011-10-20 11:56:00 +0100447 ALOGV("AudioCommandThread() processing start tone %d on stream %d",
Mathias Agopian65ab4712010-07-14 17:59:35 -0700448 data->mType, data->mStream);
Glenn Kastene9dd0172012-01-27 18:08:45 -0800449 delete mpToneGenerator;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700450 mpToneGenerator = new ToneGenerator(data->mStream, 1.0);
451 mpToneGenerator->startTone(data->mType);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700452 mLock.lock();
453 }break;
454 case STOP_TONE: {
455 mLock.unlock();
Steve Block3856b092011-10-20 11:56:00 +0100456 ALOGV("AudioCommandThread() processing stop tone");
Mathias Agopian65ab4712010-07-14 17:59:35 -0700457 if (mpToneGenerator != NULL) {
458 mpToneGenerator->stopTone();
459 delete mpToneGenerator;
460 mpToneGenerator = NULL;
461 }
462 mLock.lock();
463 }break;
464 case SET_VOLUME: {
Eric Laurent0ede8922014-05-09 18:04:42 -0700465 VolumeData *data = (VolumeData *)command->mParam.get();
Steve Block3856b092011-10-20 11:56:00 +0100466 ALOGV("AudioCommandThread() processing set volume stream %d, \
Eric Laurentde070132010-07-13 04:45:46 -0700467 volume %f, output %d", data->mStream, data->mVolume, data->mIO);
468 command->mStatus = AudioSystem::setStreamVolume(data->mStream,
469 data->mVolume,
470 data->mIO);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700471 }break;
472 case SET_PARAMETERS: {
Eric Laurent0ede8922014-05-09 18:04:42 -0700473 ParametersData *data = (ParametersData *)command->mParam.get();
Glenn Kastene53b9ea2012-03-12 16:29:55 -0700474 ALOGV("AudioCommandThread() processing set parameters string %s, io %d",
475 data->mKeyValuePairs.string(), data->mIO);
476 command->mStatus = AudioSystem::setParameters(data->mIO, data->mKeyValuePairs);
Glenn Kastene53b9ea2012-03-12 16:29:55 -0700477 }break;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700478 case SET_VOICE_VOLUME: {
Eric Laurent0ede8922014-05-09 18:04:42 -0700479 VoiceVolumeData *data = (VoiceVolumeData *)command->mParam.get();
Steve Block3856b092011-10-20 11:56:00 +0100480 ALOGV("AudioCommandThread() processing set voice volume volume %f",
Eric Laurentde070132010-07-13 04:45:46 -0700481 data->mVolume);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700482 command->mStatus = AudioSystem::setVoiceVolume(data->mVolume);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700483 }break;
Eric Laurentbfb1b832013-01-07 09:53:42 -0800484 case STOP_OUTPUT: {
Eric Laurent0ede8922014-05-09 18:04:42 -0700485 StopOutputData *data = (StopOutputData *)command->mParam.get();
Eric Laurentbfb1b832013-01-07 09:53:42 -0800486 ALOGV("AudioCommandThread() processing stop output %d",
487 data->mIO);
Eric Laurent59a89232014-06-08 14:14:17 -0700488 svc = mService.promote();
Eric Laurentbfb1b832013-01-07 09:53:42 -0800489 if (svc == 0) {
490 break;
491 }
492 mLock.unlock();
493 svc->doStopOutput(data->mIO, data->mStream, data->mSession);
494 mLock.lock();
Eric Laurentbfb1b832013-01-07 09:53:42 -0800495 }break;
496 case RELEASE_OUTPUT: {
Eric Laurent0ede8922014-05-09 18:04:42 -0700497 ReleaseOutputData *data = (ReleaseOutputData *)command->mParam.get();
Eric Laurentbfb1b832013-01-07 09:53:42 -0800498 ALOGV("AudioCommandThread() processing release output %d",
499 data->mIO);
Eric Laurent59a89232014-06-08 14:14:17 -0700500 svc = mService.promote();
Eric Laurentbfb1b832013-01-07 09:53:42 -0800501 if (svc == 0) {
502 break;
503 }
504 mLock.unlock();
Eric Laurente83b55d2014-11-14 10:06:21 -0800505 svc->doReleaseOutput(data->mIO, data->mStream, data->mSession);
Eric Laurentbfb1b832013-01-07 09:53:42 -0800506 mLock.lock();
Eric Laurentbfb1b832013-01-07 09:53:42 -0800507 }break;
Eric Laurent951f4552014-05-20 10:48:17 -0700508 case CREATE_AUDIO_PATCH: {
509 CreateAudioPatchData *data = (CreateAudioPatchData *)command->mParam.get();
510 ALOGV("AudioCommandThread() processing create audio patch");
511 sp<IAudioFlinger> af = AudioSystem::get_audio_flinger();
512 if (af == 0) {
513 command->mStatus = PERMISSION_DENIED;
514 } else {
515 command->mStatus = af->createAudioPatch(&data->mPatch, &data->mHandle);
516 }
517 } break;
518 case RELEASE_AUDIO_PATCH: {
519 ReleaseAudioPatchData *data = (ReleaseAudioPatchData *)command->mParam.get();
520 ALOGV("AudioCommandThread() processing release audio patch");
521 sp<IAudioFlinger> af = AudioSystem::get_audio_flinger();
522 if (af == 0) {
523 command->mStatus = PERMISSION_DENIED;
524 } else {
525 command->mStatus = af->releaseAudioPatch(data->mHandle);
526 }
527 } break;
Eric Laurentb52c1522014-05-20 11:27:36 -0700528 case UPDATE_AUDIOPORT_LIST: {
529 ALOGV("AudioCommandThread() processing update audio port list");
Eric Laurent59a89232014-06-08 14:14:17 -0700530 svc = mService.promote();
Eric Laurentb52c1522014-05-20 11:27:36 -0700531 if (svc == 0) {
532 break;
533 }
534 mLock.unlock();
535 svc->doOnAudioPortListUpdate();
536 mLock.lock();
537 }break;
538 case UPDATE_AUDIOPATCH_LIST: {
539 ALOGV("AudioCommandThread() processing update audio patch list");
Eric Laurent59a89232014-06-08 14:14:17 -0700540 svc = mService.promote();
Eric Laurentb52c1522014-05-20 11:27:36 -0700541 if (svc == 0) {
542 break;
543 }
544 mLock.unlock();
545 svc->doOnAudioPatchListUpdate();
546 mLock.lock();
547 }break;
Eric Laurente1715a42014-05-20 11:30:42 -0700548 case SET_AUDIOPORT_CONFIG: {
549 SetAudioPortConfigData *data = (SetAudioPortConfigData *)command->mParam.get();
550 ALOGV("AudioCommandThread() processing set port config");
551 sp<IAudioFlinger> af = AudioSystem::get_audio_flinger();
552 if (af == 0) {
553 command->mStatus = PERMISSION_DENIED;
554 } else {
555 command->mStatus = af->setAudioPortConfig(&data->mConfig);
556 }
557 } break;
Jean-Michel Trivide801052015-04-14 19:10:14 -0700558 case DYN_POLICY_MIX_STATE_UPDATE: {
559 DynPolicyMixStateUpdateData *data =
560 (DynPolicyMixStateUpdateData *)command->mParam.get();
561 //###ALOGV("AudioCommandThread() processing dyn policy mix state update");
562 ALOGV("AudioCommandThread() processing dyn policy mix state update %s %d",
563 data->mRegId.string(), data->mState);
564 svc = mService.promote();
565 if (svc == 0) {
566 break;
567 }
568 mLock.unlock();
569 svc->doOnDynamicPolicyMixStateUpdate(data->mRegId, data->mState);
570 mLock.lock();
571 } break;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700572 default:
Steve Block5ff1dd52012-01-05 23:22:43 +0000573 ALOGW("AudioCommandThread() unknown command %d", command->mCommand);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700574 }
Eric Laurent0ede8922014-05-09 18:04:42 -0700575 {
576 Mutex::Autolock _l(command->mLock);
577 if (command->mWaitStatus) {
578 command->mWaitStatus = false;
579 command->mCond.signal();
580 }
581 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700582 waitTime = INT64_MAX;
583 } else {
584 waitTime = mAudioCommands[0]->mTime - curTime;
585 break;
586 }
587 }
Eric Laurent59a89232014-06-08 14:14:17 -0700588 // release mLock before releasing strong reference on the service as
589 // AudioPolicyService destructor calls AudioCommandThread::exit() which acquires mLock.
590 mLock.unlock();
591 svc.clear();
592 mLock.lock();
Wally Yau884de212015-03-24 09:10:58 -0700593 if (!exitPending() && (mAudioCommands.isEmpty() || waitTime != INT64_MAX)) {
Ricardo Garcia05f2fdc2014-07-24 15:48:24 -0700594 // release delayed commands wake lock
595 release_wake_lock(mName.string());
Eric Laurent59a89232014-06-08 14:14:17 -0700596 ALOGV("AudioCommandThread() going to sleep");
597 mWaitWorkCV.waitRelative(mLock, waitTime);
598 ALOGV("AudioCommandThread() waking up");
599 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700600 }
Ricardo Garcia05f2fdc2014-07-24 15:48:24 -0700601 // release delayed commands wake lock before quitting
602 if (!mAudioCommands.isEmpty()) {
603 release_wake_lock(mName.string());
604 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700605 mLock.unlock();
606 return false;
607}
608
609status_t AudioPolicyService::AudioCommandThread::dump(int fd)
610{
611 const size_t SIZE = 256;
612 char buffer[SIZE];
613 String8 result;
614
615 snprintf(buffer, SIZE, "AudioCommandThread %p Dump\n", this);
616 result.append(buffer);
617 write(fd, result.string(), result.size());
618
619 bool locked = tryLock(mLock);
620 if (!locked) {
621 String8 result2(kCmdDeadlockedString);
622 write(fd, result2.string(), result2.size());
623 }
624
625 snprintf(buffer, SIZE, "- Commands:\n");
626 result = String8(buffer);
627 result.append(" Command Time Wait pParam\n");
Glenn Kasten8d6a2442012-02-08 14:04:28 -0800628 for (size_t i = 0; i < mAudioCommands.size(); i++) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700629 mAudioCommands[i]->dump(buffer, SIZE);
630 result.append(buffer);
631 }
632 result.append(" Last Command\n");
Eric Laurent0ede8922014-05-09 18:04:42 -0700633 if (mLastCommand != 0) {
634 mLastCommand->dump(buffer, SIZE);
635 result.append(buffer);
636 } else {
637 result.append(" none\n");
638 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700639
640 write(fd, result.string(), result.size());
641
642 if (locked) mLock.unlock();
643
644 return NO_ERROR;
645}
646
Glenn Kasten3d2f8772012-01-27 15:25:25 -0800647void AudioPolicyService::AudioCommandThread::startToneCommand(ToneGenerator::tone_type type,
648 audio_stream_type_t stream)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700649{
Eric Laurent0ede8922014-05-09 18:04:42 -0700650 sp<AudioCommand> command = new AudioCommand();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700651 command->mCommand = START_TONE;
Eric Laurent0ede8922014-05-09 18:04:42 -0700652 sp<ToneData> data = new ToneData();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700653 data->mType = type;
654 data->mStream = stream;
Jesper Tragardh48412dc2014-03-24 14:12:43 +0100655 command->mParam = data;
Steve Block3856b092011-10-20 11:56:00 +0100656 ALOGV("AudioCommandThread() adding tone start type %d, stream %d", type, stream);
Eric Laurent0ede8922014-05-09 18:04:42 -0700657 sendCommand(command);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700658}
659
660void AudioPolicyService::AudioCommandThread::stopToneCommand()
661{
Eric Laurent0ede8922014-05-09 18:04:42 -0700662 sp<AudioCommand> command = new AudioCommand();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700663 command->mCommand = STOP_TONE;
Steve Block3856b092011-10-20 11:56:00 +0100664 ALOGV("AudioCommandThread() adding tone stop");
Eric Laurent0ede8922014-05-09 18:04:42 -0700665 sendCommand(command);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700666}
667
Glenn Kastenfff6d712012-01-12 16:38:12 -0800668status_t AudioPolicyService::AudioCommandThread::volumeCommand(audio_stream_type_t stream,
Eric Laurentde070132010-07-13 04:45:46 -0700669 float volume,
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800670 audio_io_handle_t output,
Eric Laurentde070132010-07-13 04:45:46 -0700671 int delayMs)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700672{
Eric Laurent0ede8922014-05-09 18:04:42 -0700673 sp<AudioCommand> command = new AudioCommand();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700674 command->mCommand = SET_VOLUME;
Eric Laurent0ede8922014-05-09 18:04:42 -0700675 sp<VolumeData> data = new VolumeData();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700676 data->mStream = stream;
677 data->mVolume = volume;
678 data->mIO = output;
679 command->mParam = data;
Eric Laurent0ede8922014-05-09 18:04:42 -0700680 command->mWaitStatus = true;
Steve Block3856b092011-10-20 11:56:00 +0100681 ALOGV("AudioCommandThread() adding set volume stream %d, volume %f, output %d",
Eric Laurentde070132010-07-13 04:45:46 -0700682 stream, volume, output);
Eric Laurent0ede8922014-05-09 18:04:42 -0700683 return sendCommand(command, delayMs);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700684}
685
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800686status_t AudioPolicyService::AudioCommandThread::parametersCommand(audio_io_handle_t ioHandle,
Dima Zavinfce7a472011-04-19 22:30:36 -0700687 const char *keyValuePairs,
Eric Laurentde070132010-07-13 04:45:46 -0700688 int delayMs)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700689{
Eric Laurent0ede8922014-05-09 18:04:42 -0700690 sp<AudioCommand> command = new AudioCommand();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700691 command->mCommand = SET_PARAMETERS;
Eric Laurent0ede8922014-05-09 18:04:42 -0700692 sp<ParametersData> data = new ParametersData();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700693 data->mIO = ioHandle;
Dima Zavinfce7a472011-04-19 22:30:36 -0700694 data->mKeyValuePairs = String8(keyValuePairs);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700695 command->mParam = data;
Eric Laurent0ede8922014-05-09 18:04:42 -0700696 command->mWaitStatus = true;
Steve Block3856b092011-10-20 11:56:00 +0100697 ALOGV("AudioCommandThread() adding set parameter string %s, io %d ,delay %d",
Dima Zavinfce7a472011-04-19 22:30:36 -0700698 keyValuePairs, ioHandle, delayMs);
Eric Laurent0ede8922014-05-09 18:04:42 -0700699 return sendCommand(command, delayMs);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700700}
701
702status_t AudioPolicyService::AudioCommandThread::voiceVolumeCommand(float volume, int delayMs)
703{
Eric Laurent0ede8922014-05-09 18:04:42 -0700704 sp<AudioCommand> command = new AudioCommand();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700705 command->mCommand = SET_VOICE_VOLUME;
Eric Laurent0ede8922014-05-09 18:04:42 -0700706 sp<VoiceVolumeData> data = new VoiceVolumeData();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700707 data->mVolume = volume;
708 command->mParam = data;
Eric Laurent0ede8922014-05-09 18:04:42 -0700709 command->mWaitStatus = true;
Steve Block3856b092011-10-20 11:56:00 +0100710 ALOGV("AudioCommandThread() adding set voice volume volume %f", volume);
Eric Laurent0ede8922014-05-09 18:04:42 -0700711 return sendCommand(command, delayMs);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700712}
713
Eric Laurentbfb1b832013-01-07 09:53:42 -0800714void AudioPolicyService::AudioCommandThread::stopOutputCommand(audio_io_handle_t output,
715 audio_stream_type_t stream,
Eric Laurente83b55d2014-11-14 10:06:21 -0800716 audio_session_t session)
Eric Laurentbfb1b832013-01-07 09:53:42 -0800717{
Eric Laurent0ede8922014-05-09 18:04:42 -0700718 sp<AudioCommand> command = new AudioCommand();
Eric Laurentbfb1b832013-01-07 09:53:42 -0800719 command->mCommand = STOP_OUTPUT;
Eric Laurent0ede8922014-05-09 18:04:42 -0700720 sp<StopOutputData> data = new StopOutputData();
Eric Laurentbfb1b832013-01-07 09:53:42 -0800721 data->mIO = output;
722 data->mStream = stream;
723 data->mSession = session;
Jesper Tragardh48412dc2014-03-24 14:12:43 +0100724 command->mParam = data;
Eric Laurentbfb1b832013-01-07 09:53:42 -0800725 ALOGV("AudioCommandThread() adding stop output %d", output);
Eric Laurent0ede8922014-05-09 18:04:42 -0700726 sendCommand(command);
Eric Laurentbfb1b832013-01-07 09:53:42 -0800727}
728
Eric Laurente83b55d2014-11-14 10:06:21 -0800729void AudioPolicyService::AudioCommandThread::releaseOutputCommand(audio_io_handle_t output,
730 audio_stream_type_t stream,
731 audio_session_t session)
Eric Laurentbfb1b832013-01-07 09:53:42 -0800732{
Eric Laurent0ede8922014-05-09 18:04:42 -0700733 sp<AudioCommand> command = new AudioCommand();
Eric Laurentbfb1b832013-01-07 09:53:42 -0800734 command->mCommand = RELEASE_OUTPUT;
Eric Laurent0ede8922014-05-09 18:04:42 -0700735 sp<ReleaseOutputData> data = new ReleaseOutputData();
Eric Laurentbfb1b832013-01-07 09:53:42 -0800736 data->mIO = output;
Eric Laurente83b55d2014-11-14 10:06:21 -0800737 data->mStream = stream;
738 data->mSession = session;
Jesper Tragardh48412dc2014-03-24 14:12:43 +0100739 command->mParam = data;
Eric Laurentbfb1b832013-01-07 09:53:42 -0800740 ALOGV("AudioCommandThread() adding release output %d", output);
Eric Laurent0ede8922014-05-09 18:04:42 -0700741 sendCommand(command);
742}
743
Eric Laurent951f4552014-05-20 10:48:17 -0700744status_t AudioPolicyService::AudioCommandThread::createAudioPatchCommand(
745 const struct audio_patch *patch,
746 audio_patch_handle_t *handle,
747 int delayMs)
748{
749 status_t status = NO_ERROR;
750
751 sp<AudioCommand> command = new AudioCommand();
752 command->mCommand = CREATE_AUDIO_PATCH;
753 CreateAudioPatchData *data = new CreateAudioPatchData();
754 data->mPatch = *patch;
755 data->mHandle = *handle;
756 command->mParam = data;
757 command->mWaitStatus = true;
758 ALOGV("AudioCommandThread() adding create patch delay %d", delayMs);
759 status = sendCommand(command, delayMs);
760 if (status == NO_ERROR) {
761 *handle = data->mHandle;
762 }
763 return status;
764}
765
766status_t AudioPolicyService::AudioCommandThread::releaseAudioPatchCommand(audio_patch_handle_t handle,
767 int delayMs)
768{
769 sp<AudioCommand> command = new AudioCommand();
770 command->mCommand = RELEASE_AUDIO_PATCH;
771 ReleaseAudioPatchData *data = new ReleaseAudioPatchData();
772 data->mHandle = handle;
773 command->mParam = data;
774 command->mWaitStatus = true;
775 ALOGV("AudioCommandThread() adding release patch delay %d", delayMs);
776 return sendCommand(command, delayMs);
777}
778
Eric Laurentb52c1522014-05-20 11:27:36 -0700779void AudioPolicyService::AudioCommandThread::updateAudioPortListCommand()
780{
781 sp<AudioCommand> command = new AudioCommand();
782 command->mCommand = UPDATE_AUDIOPORT_LIST;
783 ALOGV("AudioCommandThread() adding update audio port list");
784 sendCommand(command);
785}
786
787void AudioPolicyService::AudioCommandThread::updateAudioPatchListCommand()
788{
789 sp<AudioCommand>command = new AudioCommand();
790 command->mCommand = UPDATE_AUDIOPATCH_LIST;
791 ALOGV("AudioCommandThread() adding update audio patch list");
792 sendCommand(command);
793}
794
Eric Laurente1715a42014-05-20 11:30:42 -0700795status_t AudioPolicyService::AudioCommandThread::setAudioPortConfigCommand(
796 const struct audio_port_config *config, int delayMs)
797{
798 sp<AudioCommand> command = new AudioCommand();
799 command->mCommand = SET_AUDIOPORT_CONFIG;
800 SetAudioPortConfigData *data = new SetAudioPortConfigData();
801 data->mConfig = *config;
802 command->mParam = data;
803 command->mWaitStatus = true;
804 ALOGV("AudioCommandThread() adding set port config delay %d", delayMs);
805 return sendCommand(command, delayMs);
806}
807
Jean-Michel Trivide801052015-04-14 19:10:14 -0700808void AudioPolicyService::AudioCommandThread::dynamicPolicyMixStateUpdateCommand(
809 String8 regId, int32_t state)
810{
811 sp<AudioCommand> command = new AudioCommand();
812 command->mCommand = DYN_POLICY_MIX_STATE_UPDATE;
813 DynPolicyMixStateUpdateData *data = new DynPolicyMixStateUpdateData();
814 data->mRegId = regId;
815 data->mState = state;
816 command->mParam = data;
817 ALOGV("AudioCommandThread() sending dynamic policy mix (id=%s) state update to %d",
818 regId.string(), state);
819 sendCommand(command);
820}
821
Eric Laurent0ede8922014-05-09 18:04:42 -0700822status_t AudioPolicyService::AudioCommandThread::sendCommand(sp<AudioCommand>& command, int delayMs)
823{
824 {
825 Mutex::Autolock _l(mLock);
826 insertCommand_l(command, delayMs);
827 mWaitWorkCV.signal();
828 }
829 Mutex::Autolock _l(command->mLock);
830 while (command->mWaitStatus) {
831 nsecs_t timeOutNs = kAudioCommandTimeoutNs + milliseconds(delayMs);
832 if (command->mCond.waitRelative(command->mLock, timeOutNs) != NO_ERROR) {
833 command->mStatus = TIMED_OUT;
834 command->mWaitStatus = false;
835 }
836 }
837 return command->mStatus;
Eric Laurentbfb1b832013-01-07 09:53:42 -0800838}
839
Mathias Agopian65ab4712010-07-14 17:59:35 -0700840// insertCommand_l() must be called with mLock held
Eric Laurent0ede8922014-05-09 18:04:42 -0700841void AudioPolicyService::AudioCommandThread::insertCommand_l(sp<AudioCommand>& command, int delayMs)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700842{
Glenn Kasten8d6a2442012-02-08 14:04:28 -0800843 ssize_t i; // not size_t because i will count down to -1
Eric Laurent0ede8922014-05-09 18:04:42 -0700844 Vector < sp<AudioCommand> > removedCommands;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700845 command->mTime = systemTime() + milliseconds(delayMs);
846
847 // acquire wake lock to make sure delayed commands are processed
Eric Laurentbfb1b832013-01-07 09:53:42 -0800848 if (mAudioCommands.isEmpty()) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700849 acquire_wake_lock(PARTIAL_WAKE_LOCK, mName.string());
850 }
851
852 // check same pending commands with later time stamps and eliminate them
853 for (i = mAudioCommands.size()-1; i >= 0; i--) {
Eric Laurent0ede8922014-05-09 18:04:42 -0700854 sp<AudioCommand> command2 = mAudioCommands[i];
Mathias Agopian65ab4712010-07-14 17:59:35 -0700855 // commands are sorted by increasing time stamp: no need to scan the rest of mAudioCommands
856 if (command2->mTime <= command->mTime) break;
Eric Laurente45b48a2014-09-04 16:40:57 -0700857
858 // create audio patch or release audio patch commands are equivalent
859 // with regard to filtering
860 if ((command->mCommand == CREATE_AUDIO_PATCH) ||
861 (command->mCommand == RELEASE_AUDIO_PATCH)) {
862 if ((command2->mCommand != CREATE_AUDIO_PATCH) &&
863 (command2->mCommand != RELEASE_AUDIO_PATCH)) {
864 continue;
865 }
866 } else if (command2->mCommand != command->mCommand) continue;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700867
868 switch (command->mCommand) {
869 case SET_PARAMETERS: {
Eric Laurent0ede8922014-05-09 18:04:42 -0700870 ParametersData *data = (ParametersData *)command->mParam.get();
871 ParametersData *data2 = (ParametersData *)command2->mParam.get();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700872 if (data->mIO != data2->mIO) break;
Steve Block3856b092011-10-20 11:56:00 +0100873 ALOGV("Comparing parameter command %s to new command %s",
Eric Laurentde070132010-07-13 04:45:46 -0700874 data2->mKeyValuePairs.string(), data->mKeyValuePairs.string());
Mathias Agopian65ab4712010-07-14 17:59:35 -0700875 AudioParameter param = AudioParameter(data->mKeyValuePairs);
876 AudioParameter param2 = AudioParameter(data2->mKeyValuePairs);
877 for (size_t j = 0; j < param.size(); j++) {
Glenn Kastene53b9ea2012-03-12 16:29:55 -0700878 String8 key;
879 String8 value;
880 param.getAt(j, key, value);
881 for (size_t k = 0; k < param2.size(); k++) {
882 String8 key2;
883 String8 value2;
884 param2.getAt(k, key2, value2);
885 if (key2 == key) {
886 param2.remove(key2);
887 ALOGV("Filtering out parameter %s", key2.string());
888 break;
889 }
890 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700891 }
892 // if all keys have been filtered out, remove the command.
893 // otherwise, update the key value pairs
894 if (param2.size() == 0) {
895 removedCommands.add(command2);
896 } else {
897 data2->mKeyValuePairs = param2.toString();
898 }
Eric Laurent21e54562013-09-23 12:08:05 -0700899 command->mTime = command2->mTime;
900 // force delayMs to non 0 so that code below does not request to wait for
901 // command status as the command is now delayed
902 delayMs = 1;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700903 } break;
904
905 case SET_VOLUME: {
Eric Laurent0ede8922014-05-09 18:04:42 -0700906 VolumeData *data = (VolumeData *)command->mParam.get();
907 VolumeData *data2 = (VolumeData *)command2->mParam.get();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700908 if (data->mIO != data2->mIO) break;
909 if (data->mStream != data2->mStream) break;
Steve Block3856b092011-10-20 11:56:00 +0100910 ALOGV("Filtering out volume command on output %d for stream %d",
Eric Laurentde070132010-07-13 04:45:46 -0700911 data->mIO, data->mStream);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700912 removedCommands.add(command2);
Eric Laurent21e54562013-09-23 12:08:05 -0700913 command->mTime = command2->mTime;
914 // force delayMs to non 0 so that code below does not request to wait for
915 // command status as the command is now delayed
916 delayMs = 1;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700917 } break;
Eric Laurente45b48a2014-09-04 16:40:57 -0700918
919 case CREATE_AUDIO_PATCH:
920 case RELEASE_AUDIO_PATCH: {
921 audio_patch_handle_t handle;
Haynes Mathew Georgea2d4a6d2014-10-13 13:05:22 -0700922 struct audio_patch patch;
Eric Laurente45b48a2014-09-04 16:40:57 -0700923 if (command->mCommand == CREATE_AUDIO_PATCH) {
924 handle = ((CreateAudioPatchData *)command->mParam.get())->mHandle;
Haynes Mathew Georgea2d4a6d2014-10-13 13:05:22 -0700925 patch = ((CreateAudioPatchData *)command->mParam.get())->mPatch;
Eric Laurente45b48a2014-09-04 16:40:57 -0700926 } else {
927 handle = ((ReleaseAudioPatchData *)command->mParam.get())->mHandle;
928 }
929 audio_patch_handle_t handle2;
Haynes Mathew Georgea2d4a6d2014-10-13 13:05:22 -0700930 struct audio_patch patch2;
Eric Laurente45b48a2014-09-04 16:40:57 -0700931 if (command2->mCommand == CREATE_AUDIO_PATCH) {
932 handle2 = ((CreateAudioPatchData *)command2->mParam.get())->mHandle;
Haynes Mathew Georgea2d4a6d2014-10-13 13:05:22 -0700933 patch2 = ((CreateAudioPatchData *)command2->mParam.get())->mPatch;
Eric Laurente45b48a2014-09-04 16:40:57 -0700934 } else {
935 handle2 = ((ReleaseAudioPatchData *)command2->mParam.get())->mHandle;
Glenn Kastenf60b6b62015-07-06 10:53:26 -0700936 memset(&patch2, 0, sizeof(patch2));
Eric Laurente45b48a2014-09-04 16:40:57 -0700937 }
938 if (handle != handle2) break;
Haynes Mathew Georgea2d4a6d2014-10-13 13:05:22 -0700939 /* Filter CREATE_AUDIO_PATCH commands only when they are issued for
940 same output. */
941 if( (command->mCommand == CREATE_AUDIO_PATCH) &&
942 (command2->mCommand == CREATE_AUDIO_PATCH) ) {
943 bool isOutputDiff = false;
944 if (patch.num_sources == patch2.num_sources) {
945 for (unsigned count = 0; count < patch.num_sources; count++) {
946 if (patch.sources[count].id != patch2.sources[count].id) {
947 isOutputDiff = true;
948 break;
949 }
950 }
951 if (isOutputDiff)
952 break;
953 }
954 }
Eric Laurente45b48a2014-09-04 16:40:57 -0700955 ALOGV("Filtering out %s audio patch command for handle %d",
956 (command->mCommand == CREATE_AUDIO_PATCH) ? "create" : "release", handle);
957 removedCommands.add(command2);
958 command->mTime = command2->mTime;
959 // force delayMs to non 0 so that code below does not request to wait for
960 // command status as the command is now delayed
961 delayMs = 1;
962 } break;
963
Jean-Michel Trivide801052015-04-14 19:10:14 -0700964 case DYN_POLICY_MIX_STATE_UPDATE: {
965
966 } break;
967
Mathias Agopian65ab4712010-07-14 17:59:35 -0700968 case START_TONE:
969 case STOP_TONE:
970 default:
971 break;
972 }
973 }
974
975 // remove filtered commands
976 for (size_t j = 0; j < removedCommands.size(); j++) {
977 // removed commands always have time stamps greater than current command
978 for (size_t k = i + 1; k < mAudioCommands.size(); k++) {
Eric Laurent0ede8922014-05-09 18:04:42 -0700979 if (mAudioCommands[k].get() == removedCommands[j].get()) {
Steve Block3856b092011-10-20 11:56:00 +0100980 ALOGV("suppressing command: %d", mAudioCommands[k]->mCommand);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700981 mAudioCommands.removeAt(k);
982 break;
983 }
984 }
985 }
986 removedCommands.clear();
987
Eric Laurentaa79bef2015-01-15 14:33:51 -0800988 // Disable wait for status if delay is not 0.
989 // Except for create audio patch command because the returned patch handle
990 // is needed by audio policy manager
991 if (delayMs != 0 && command->mCommand != CREATE_AUDIO_PATCH) {
Eric Laurentcec4abb2012-07-03 12:23:02 -0700992 command->mWaitStatus = false;
993 }
Eric Laurentcec4abb2012-07-03 12:23:02 -0700994
Mathias Agopian65ab4712010-07-14 17:59:35 -0700995 // insert command at the right place according to its time stamp
Eric Laurent1e693b52014-07-09 15:03:28 -0700996 ALOGV("inserting command: %d at index %zd, num commands %zu",
997 command->mCommand, i+1, mAudioCommands.size());
Mathias Agopian65ab4712010-07-14 17:59:35 -0700998 mAudioCommands.insertAt(command, i + 1);
999}
1000
1001void AudioPolicyService::AudioCommandThread::exit()
1002{
Steve Block3856b092011-10-20 11:56:00 +01001003 ALOGV("AudioCommandThread::exit");
Mathias Agopian65ab4712010-07-14 17:59:35 -07001004 {
1005 AutoMutex _l(mLock);
1006 requestExit();
1007 mWaitWorkCV.signal();
1008 }
1009 requestExitAndWait();
1010}
1011
1012void AudioPolicyService::AudioCommandThread::AudioCommand::dump(char* buffer, size_t size)
1013{
1014 snprintf(buffer, size, " %02d %06d.%03d %01u %p\n",
1015 mCommand,
1016 (int)ns2s(mTime),
1017 (int)ns2ms(mTime)%1000,
1018 mWaitStatus,
Eric Laurent0ede8922014-05-09 18:04:42 -07001019 mParam.get());
Mathias Agopian65ab4712010-07-14 17:59:35 -07001020}
1021
Dima Zavinfce7a472011-04-19 22:30:36 -07001022/******* helpers for the service_ops callbacks defined below *********/
1023void AudioPolicyService::setParameters(audio_io_handle_t ioHandle,
1024 const char *keyValuePairs,
1025 int delayMs)
1026{
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001027 mAudioCommandThread->parametersCommand(ioHandle, keyValuePairs,
Dima Zavinfce7a472011-04-19 22:30:36 -07001028 delayMs);
1029}
1030
1031int AudioPolicyService::setStreamVolume(audio_stream_type_t stream,
1032 float volume,
1033 audio_io_handle_t output,
1034 int delayMs)
1035{
Glenn Kastenfff6d712012-01-12 16:38:12 -08001036 return (int)mAudioCommandThread->volumeCommand(stream, volume,
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001037 output, delayMs);
Dima Zavinfce7a472011-04-19 22:30:36 -07001038}
1039
1040int AudioPolicyService::startTone(audio_policy_tone_t tone,
1041 audio_stream_type_t stream)
1042{
Glenn Kasten6e2ebe92013-08-13 09:14:51 -07001043 if (tone != AUDIO_POLICY_TONE_IN_CALL_NOTIFICATION) {
Steve Block29357bc2012-01-06 19:20:56 +00001044 ALOGE("startTone: illegal tone requested (%d)", tone);
Glenn Kasten6e2ebe92013-08-13 09:14:51 -07001045 }
1046 if (stream != AUDIO_STREAM_VOICE_CALL) {
Steve Block29357bc2012-01-06 19:20:56 +00001047 ALOGE("startTone: illegal stream (%d) requested for tone %d", stream,
Glenn Kastene53b9ea2012-03-12 16:29:55 -07001048 tone);
Glenn Kasten6e2ebe92013-08-13 09:14:51 -07001049 }
Dima Zavinfce7a472011-04-19 22:30:36 -07001050 mTonePlaybackThread->startToneCommand(ToneGenerator::TONE_SUP_CALL_WAITING,
1051 AUDIO_STREAM_VOICE_CALL);
1052 return 0;
1053}
1054
1055int AudioPolicyService::stopTone()
1056{
1057 mTonePlaybackThread->stopToneCommand();
1058 return 0;
1059}
1060
1061int AudioPolicyService::setVoiceVolume(float volume, int delayMs)
1062{
1063 return (int)mAudioCommandThread->voiceVolumeCommand(volume, delayMs);
1064}
1065
Dima Zavinfce7a472011-04-19 22:30:36 -07001066extern "C" {
Eric Laurent2d388ec2014-03-07 13:25:54 -08001067audio_module_handle_t aps_load_hw_module(void *service __unused,
1068 const char *name);
1069audio_io_handle_t aps_open_output(void *service __unused,
Eric Laurenta4c5a552012-03-29 10:12:40 -07001070 audio_devices_t *pDevices,
1071 uint32_t *pSamplingRate,
1072 audio_format_t *pFormat,
1073 audio_channel_mask_t *pChannelMask,
1074 uint32_t *pLatencyMs,
Eric Laurent2d388ec2014-03-07 13:25:54 -08001075 audio_output_flags_t flags);
Eric Laurenta4c5a552012-03-29 10:12:40 -07001076
Eric Laurent2d388ec2014-03-07 13:25:54 -08001077audio_io_handle_t aps_open_output_on_module(void *service __unused,
Eric Laurenta4c5a552012-03-29 10:12:40 -07001078 audio_module_handle_t module,
1079 audio_devices_t *pDevices,
1080 uint32_t *pSamplingRate,
1081 audio_format_t *pFormat,
1082 audio_channel_mask_t *pChannelMask,
1083 uint32_t *pLatencyMs,
Richard Fitzgeraldad3af332013-03-25 16:54:37 +00001084 audio_output_flags_t flags,
Eric Laurent2d388ec2014-03-07 13:25:54 -08001085 const audio_offload_info_t *offloadInfo);
1086audio_io_handle_t aps_open_dup_output(void *service __unused,
Dima Zavinfce7a472011-04-19 22:30:36 -07001087 audio_io_handle_t output1,
Eric Laurent2d388ec2014-03-07 13:25:54 -08001088 audio_io_handle_t output2);
1089int aps_close_output(void *service __unused, audio_io_handle_t output);
1090int aps_suspend_output(void *service __unused, audio_io_handle_t output);
1091int aps_restore_output(void *service __unused, audio_io_handle_t output);
1092audio_io_handle_t aps_open_input(void *service __unused,
Eric Laurenta4c5a552012-03-29 10:12:40 -07001093 audio_devices_t *pDevices,
1094 uint32_t *pSamplingRate,
1095 audio_format_t *pFormat,
1096 audio_channel_mask_t *pChannelMask,
Eric Laurent2d388ec2014-03-07 13:25:54 -08001097 audio_in_acoustics_t acoustics __unused);
1098audio_io_handle_t aps_open_input_on_module(void *service __unused,
Eric Laurenta4c5a552012-03-29 10:12:40 -07001099 audio_module_handle_t module,
1100 audio_devices_t *pDevices,
1101 uint32_t *pSamplingRate,
1102 audio_format_t *pFormat,
Eric Laurent2d388ec2014-03-07 13:25:54 -08001103 audio_channel_mask_t *pChannelMask);
1104int aps_close_input(void *service __unused, audio_io_handle_t input);
1105int aps_invalidate_stream(void *service __unused, audio_stream_type_t stream);
1106int aps_move_effects(void *service __unused, int session,
Dima Zavinfce7a472011-04-19 22:30:36 -07001107 audio_io_handle_t src_output,
Eric Laurent2d388ec2014-03-07 13:25:54 -08001108 audio_io_handle_t dst_output);
1109char * aps_get_parameters(void *service __unused, audio_io_handle_t io_handle,
1110 const char *keys);
1111void aps_set_parameters(void *service, audio_io_handle_t io_handle,
1112 const char *kv_pairs, int delay_ms);
1113int aps_set_stream_volume(void *service, audio_stream_type_t stream,
Dima Zavinfce7a472011-04-19 22:30:36 -07001114 float volume, audio_io_handle_t output,
Eric Laurent2d388ec2014-03-07 13:25:54 -08001115 int delay_ms);
1116int aps_start_tone(void *service, audio_policy_tone_t tone,
1117 audio_stream_type_t stream);
1118int aps_stop_tone(void *service);
1119int aps_set_voice_volume(void *service, float volume, int delay_ms);
1120};
Dima Zavinfce7a472011-04-19 22:30:36 -07001121
1122namespace {
1123 struct audio_policy_service_ops aps_ops = {
Glenn Kasten01d3acb2014-02-06 08:24:07 -08001124 .open_output = aps_open_output,
1125 .open_duplicate_output = aps_open_dup_output,
1126 .close_output = aps_close_output,
1127 .suspend_output = aps_suspend_output,
1128 .restore_output = aps_restore_output,
1129 .open_input = aps_open_input,
1130 .close_input = aps_close_input,
1131 .set_stream_volume = aps_set_stream_volume,
Glenn Kastend2304db2014-02-03 07:40:31 -08001132 .invalidate_stream = aps_invalidate_stream,
Glenn Kasten01d3acb2014-02-06 08:24:07 -08001133 .set_parameters = aps_set_parameters,
1134 .get_parameters = aps_get_parameters,
1135 .start_tone = aps_start_tone,
1136 .stop_tone = aps_stop_tone,
1137 .set_voice_volume = aps_set_voice_volume,
1138 .move_effects = aps_move_effects,
1139 .load_hw_module = aps_load_hw_module,
1140 .open_output_on_module = aps_open_output_on_module,
1141 .open_input_on_module = aps_open_input_on_module,
Dima Zavinfce7a472011-04-19 22:30:36 -07001142 };
1143}; // namespace <unnamed>
1144
Mathias Agopian65ab4712010-07-14 17:59:35 -07001145}; // namespace android