blob: a2a0461b8a2301ce729441f552fda02301897e4a [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>
Mathias Agopian65ab4712010-07-14 17:59:35 -070038
Dima Zavinfce7a472011-04-19 22:30:36 -070039#include <hardware/hardware.h>
Dima Zavin64760242011-05-11 14:15:23 -070040#include <system/audio.h>
Dima Zavin7394a4f2011-06-13 18:16:26 -070041#include <system/audio_policy.h>
Dima Zavinfce7a472011-04-19 22:30:36 -070042#include <hardware/audio_policy.h>
Eric Laurent7c7f10b2011-06-17 21:29:58 -070043#include <audio_effects/audio_effects_conf.h>
Glenn Kasten1ab85ec2013-05-31 09:18:43 -070044#include <media/AudioParameter.h>
Dima Zavinfce7a472011-04-19 22:30:36 -070045
Mathias Agopian65ab4712010-07-14 17:59:35 -070046namespace android {
47
Glenn Kasten8dad0e32012-01-09 08:41:22 -080048static const char kDeadlockedString[] = "AudioPolicyService may be deadlocked\n";
49static const char kCmdDeadlockedString[] = "AudioPolicyService command thread may be deadlocked\n";
Mathias Agopian65ab4712010-07-14 17:59:35 -070050
51static const int kDumpLockRetries = 50;
Glenn Kasten22ecc912012-01-09 08:33:38 -080052static const int kDumpLockSleepUs = 20000;
Mathias Agopian65ab4712010-07-14 17:59:35 -070053
Eric Laurent0ede8922014-05-09 18:04:42 -070054static const nsecs_t kAudioCommandTimeoutNs = seconds(3); // 3 seconds
Christer Fletcher5fa8c4b2013-01-18 15:27:03 +010055
Dima Zavinfce7a472011-04-19 22:30:36 -070056namespace {
57 extern struct audio_policy_service_ops aps_ops;
58};
59
Mathias Agopian65ab4712010-07-14 17:59:35 -070060// ----------------------------------------------------------------------------
61
62AudioPolicyService::AudioPolicyService()
Eric Laurentdce54a12014-03-10 12:19:46 -070063 : BnAudioPolicyService(), mpAudioPolicyDev(NULL), mpAudioPolicy(NULL),
64 mAudioPolicyManager(NULL), mAudioPolicyClient(NULL)
Mathias Agopian65ab4712010-07-14 17:59:35 -070065{
66 char value[PROPERTY_VALUE_MAX];
Dima Zavinfce7a472011-04-19 22:30:36 -070067 const struct hw_module_t *module;
68 int forced_val;
69 int rc;
Mathias Agopian65ab4712010-07-14 17:59:35 -070070
Eric Laurent93575202011-01-18 18:39:02 -080071 Mutex::Autolock _l(mLock);
72
Mathias Agopian65ab4712010-07-14 17:59:35 -070073 // start tone playback thread
Eric Laurentbfb1b832013-01-07 09:53:42 -080074 mTonePlaybackThread = new AudioCommandThread(String8("ApmTone"), this);
Mathias Agopian65ab4712010-07-14 17:59:35 -070075 // start audio commands thread
Eric Laurentbfb1b832013-01-07 09:53:42 -080076 mAudioCommandThread = new AudioCommandThread(String8("ApmAudio"), this);
77 // start output activity command thread
78 mOutputCommandThread = new AudioCommandThread(String8("ApmOutput"), this);
Eric Laurentdce54a12014-03-10 12:19:46 -070079
80#ifdef USE_LEGACY_AUDIO_POLICY
81 ALOGI("AudioPolicyService CSTOR in legacy mode");
82
Dima Zavinfce7a472011-04-19 22:30:36 -070083 /* instantiate the audio policy manager */
84 rc = hw_get_module(AUDIO_POLICY_HARDWARE_MODULE_ID, &module);
Glenn Kasten6e2ebe92013-08-13 09:14:51 -070085 if (rc) {
Dima Zavinfce7a472011-04-19 22:30:36 -070086 return;
Glenn Kasten6e2ebe92013-08-13 09:14:51 -070087 }
Dima Zavinfce7a472011-04-19 22:30:36 -070088 rc = audio_policy_dev_open(module, &mpAudioPolicyDev);
Steve Block29357bc2012-01-06 19:20:56 +000089 ALOGE_IF(rc, "couldn't open audio policy device (%s)", strerror(-rc));
Glenn Kasten6e2ebe92013-08-13 09:14:51 -070090 if (rc) {
Dima Zavinfce7a472011-04-19 22:30:36 -070091 return;
Glenn Kasten6e2ebe92013-08-13 09:14:51 -070092 }
Eric Laurent93575202011-01-18 18:39:02 -080093
Dima Zavinfce7a472011-04-19 22:30:36 -070094 rc = mpAudioPolicyDev->create_audio_policy(mpAudioPolicyDev, &aps_ops, this,
95 &mpAudioPolicy);
Steve Block29357bc2012-01-06 19:20:56 +000096 ALOGE_IF(rc, "couldn't create audio policy (%s)", strerror(-rc));
Glenn Kasten6e2ebe92013-08-13 09:14:51 -070097 if (rc) {
Dima Zavinfce7a472011-04-19 22:30:36 -070098 return;
Glenn Kasten6e2ebe92013-08-13 09:14:51 -070099 }
Dima Zavinfce7a472011-04-19 22:30:36 -0700100
101 rc = mpAudioPolicy->init_check(mpAudioPolicy);
Steve Block29357bc2012-01-06 19:20:56 +0000102 ALOGE_IF(rc, "couldn't init_check the audio policy (%s)", strerror(-rc));
Glenn Kasten6e2ebe92013-08-13 09:14:51 -0700103 if (rc) {
Dima Zavinfce7a472011-04-19 22:30:36 -0700104 return;
Glenn Kasten6e2ebe92013-08-13 09:14:51 -0700105 }
Steve Blockdf64d152012-01-04 20:05:49 +0000106 ALOGI("Loaded audio policy from %s (%s)", module->name, module->id);
Eric Laurentdce54a12014-03-10 12:19:46 -0700107#else
108 ALOGI("AudioPolicyService CSTOR in new mode");
109
110 mAudioPolicyClient = new AudioPolicyClient(this);
111 mAudioPolicyManager = new AudioPolicyManager(mAudioPolicyClient);
112#endif
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700113
114 // load audio pre processing modules
115 if (access(AUDIO_EFFECT_VENDOR_CONFIG_FILE, R_OK) == 0) {
116 loadPreProcessorConfig(AUDIO_EFFECT_VENDOR_CONFIG_FILE);
117 } else if (access(AUDIO_EFFECT_DEFAULT_CONFIG_FILE, R_OK) == 0) {
118 loadPreProcessorConfig(AUDIO_EFFECT_DEFAULT_CONFIG_FILE);
119 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700120}
121
122AudioPolicyService::~AudioPolicyService()
123{
124 mTonePlaybackThread->exit();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700125 mAudioCommandThread->exit();
Eric Laurent657ff612014-05-07 11:58:24 -0700126 mOutputCommandThread->exit();
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700127
128 // release audio pre processing resources
129 for (size_t i = 0; i < mInputSources.size(); i++) {
Glenn Kasten9fda4b82012-02-02 14:04:37 -0800130 delete mInputSources.valueAt(i);
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700131 }
132 mInputSources.clear();
133
134 for (size_t i = 0; i < mInputs.size(); i++) {
135 mInputs.valueAt(i)->mEffects.clear();
136 delete mInputs.valueAt(i);
137 }
138 mInputs.clear();
139
Eric Laurentdce54a12014-03-10 12:19:46 -0700140#ifdef USE_LEGACY_AUDIO_POLICY
Glenn Kasten6e2ebe92013-08-13 09:14:51 -0700141 if (mpAudioPolicy != NULL && mpAudioPolicyDev != NULL) {
Dima Zavinfce7a472011-04-19 22:30:36 -0700142 mpAudioPolicyDev->destroy_audio_policy(mpAudioPolicyDev, mpAudioPolicy);
Glenn Kasten6e2ebe92013-08-13 09:14:51 -0700143 }
144 if (mpAudioPolicyDev != NULL) {
Dima Zavinfce7a472011-04-19 22:30:36 -0700145 audio_policy_dev_close(mpAudioPolicyDev);
Glenn Kasten6e2ebe92013-08-13 09:14:51 -0700146 }
Eric Laurentdce54a12014-03-10 12:19:46 -0700147#else
148 delete mAudioPolicyManager;
149 delete mAudioPolicyClient;
150#endif
Eric Laurentb52c1522014-05-20 11:27:36 -0700151
152 mNotificationClients.clear();
153}
154
155// A notification client is always registered by AudioSystem when the client process
156// connects to AudioPolicyService.
157void AudioPolicyService::registerClient(const sp<IAudioPolicyServiceClient>& client)
158{
159
160 Mutex::Autolock _l(mLock);
161
162 uid_t uid = IPCThreadState::self()->getCallingUid();
163 if (mNotificationClients.indexOfKey(uid) < 0) {
164 sp<NotificationClient> notificationClient = new NotificationClient(this,
165 client,
166 uid);
167 ALOGV("registerClient() client %p, uid %d", client.get(), uid);
168
169 mNotificationClients.add(uid, notificationClient);
170
171 sp<IBinder> binder = client->asBinder();
172 binder->linkToDeath(notificationClient);
173 }
174}
175
176// removeNotificationClient() is called when the client process dies.
177void AudioPolicyService::removeNotificationClient(uid_t uid)
178{
179 Mutex::Autolock _l(mLock);
180
181 mNotificationClients.removeItem(uid);
182
183#ifndef USE_LEGACY_AUDIO_POLICY
184 if (mAudioPolicyManager) {
185 mAudioPolicyManager->clearAudioPatches(uid);
186 }
187#endif
188}
189
190void AudioPolicyService::onAudioPortListUpdate()
191{
192 mOutputCommandThread->updateAudioPortListCommand();
193}
194
195void AudioPolicyService::doOnAudioPortListUpdate()
196{
197 Mutex::Autolock _l(mLock);
198 for (size_t i = 0; i < mNotificationClients.size(); i++) {
199 mNotificationClients.valueAt(i)->onAudioPortListUpdate();
200 }
201}
202
203void AudioPolicyService::onAudioPatchListUpdate()
204{
205 mOutputCommandThread->updateAudioPatchListCommand();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700206}
207
Eric Laurent951f4552014-05-20 10:48:17 -0700208status_t AudioPolicyService::clientCreateAudioPatch(const struct audio_patch *patch,
209 audio_patch_handle_t *handle,
210 int delayMs)
211{
212 return mAudioCommandThread->createAudioPatchCommand(patch, handle, delayMs);
213}
214
215status_t AudioPolicyService::clientReleaseAudioPatch(audio_patch_handle_t handle,
216 int delayMs)
217{
218 return mAudioCommandThread->releaseAudioPatchCommand(handle, delayMs);
219}
220
Eric Laurentb52c1522014-05-20 11:27:36 -0700221void AudioPolicyService::doOnAudioPatchListUpdate()
222{
223 Mutex::Autolock _l(mLock);
224 for (size_t i = 0; i < mNotificationClients.size(); i++) {
225 mNotificationClients.valueAt(i)->onAudioPatchListUpdate();
226 }
227}
228
Eric Laurente1715a42014-05-20 11:30:42 -0700229status_t AudioPolicyService::clientSetAudioPortConfig(const struct audio_port_config *config,
230 int delayMs)
231{
232 return mAudioCommandThread->setAudioPortConfigCommand(config, delayMs);
233}
234
Eric Laurentb52c1522014-05-20 11:27:36 -0700235AudioPolicyService::NotificationClient::NotificationClient(const sp<AudioPolicyService>& service,
236 const sp<IAudioPolicyServiceClient>& client,
237 uid_t uid)
238 : mService(service), mUid(uid), mAudioPolicyServiceClient(client)
239{
240}
241
242AudioPolicyService::NotificationClient::~NotificationClient()
243{
244}
245
246void AudioPolicyService::NotificationClient::binderDied(const wp<IBinder>& who __unused)
247{
248 sp<NotificationClient> keep(this);
249 sp<AudioPolicyService> service = mService.promote();
250 if (service != 0) {
251 service->removeNotificationClient(mUid);
252 }
253}
254
255void AudioPolicyService::NotificationClient::onAudioPortListUpdate()
256{
257 if (mAudioPolicyServiceClient != 0) {
258 mAudioPolicyServiceClient->onAudioPortListUpdate();
259 }
260}
261
262void AudioPolicyService::NotificationClient::onAudioPatchListUpdate()
263{
264 if (mAudioPolicyServiceClient != 0) {
265 mAudioPolicyServiceClient->onAudioPatchListUpdate();
266 }
267}
Eric Laurent57dae992011-07-24 13:36:09 -0700268
Mathias Agopian65ab4712010-07-14 17:59:35 -0700269void AudioPolicyService::binderDied(const wp<IBinder>& who) {
Glenn Kasten411e4472012-11-02 10:00:06 -0700270 ALOGW("binderDied() %p, calling pid %d", who.unsafe_get(),
Eric Laurentde070132010-07-13 04:45:46 -0700271 IPCThreadState::self()->getCallingPid());
Mathias Agopian65ab4712010-07-14 17:59:35 -0700272}
273
274static bool tryLock(Mutex& mutex)
275{
276 bool locked = false;
277 for (int i = 0; i < kDumpLockRetries; ++i) {
278 if (mutex.tryLock() == NO_ERROR) {
279 locked = true;
280 break;
281 }
Glenn Kasten22ecc912012-01-09 08:33:38 -0800282 usleep(kDumpLockSleepUs);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700283 }
284 return locked;
285}
286
287status_t AudioPolicyService::dumpInternals(int fd)
288{
289 const size_t SIZE = 256;
290 char buffer[SIZE];
291 String8 result;
292
Eric Laurentdce54a12014-03-10 12:19:46 -0700293#ifdef USE_LEGACY_AUDIO_POLICY
Dima Zavinfce7a472011-04-19 22:30:36 -0700294 snprintf(buffer, SIZE, "PolicyManager Interface: %p\n", mpAudioPolicy);
Eric Laurentdce54a12014-03-10 12:19:46 -0700295#else
296 snprintf(buffer, SIZE, "AudioPolicyManager: %p\n", mAudioPolicyManager);
297#endif
Mathias Agopian65ab4712010-07-14 17:59:35 -0700298 result.append(buffer);
299 snprintf(buffer, SIZE, "Command Thread: %p\n", mAudioCommandThread.get());
300 result.append(buffer);
301 snprintf(buffer, SIZE, "Tones Thread: %p\n", mTonePlaybackThread.get());
302 result.append(buffer);
303
304 write(fd, result.string(), result.size());
305 return NO_ERROR;
306}
307
Glenn Kasten0f11b512014-01-31 16:18:54 -0800308status_t AudioPolicyService::dump(int fd, const Vector<String16>& args __unused)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700309{
Glenn Kasten44deb052012-02-05 18:09:08 -0800310 if (!dumpAllowed()) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700311 dumpPermissionDenial(fd);
312 } else {
313 bool locked = tryLock(mLock);
314 if (!locked) {
315 String8 result(kDeadlockedString);
316 write(fd, result.string(), result.size());
317 }
318
319 dumpInternals(fd);
Glenn Kasten9d1f02d2012-02-08 17:47:58 -0800320 if (mAudioCommandThread != 0) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700321 mAudioCommandThread->dump(fd);
322 }
Glenn Kasten9d1f02d2012-02-08 17:47:58 -0800323 if (mTonePlaybackThread != 0) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700324 mTonePlaybackThread->dump(fd);
325 }
326
Eric Laurentdce54a12014-03-10 12:19:46 -0700327#ifdef USE_LEGACY_AUDIO_POLICY
Dima Zavinfce7a472011-04-19 22:30:36 -0700328 if (mpAudioPolicy) {
329 mpAudioPolicy->dump(mpAudioPolicy, fd);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700330 }
Eric Laurentdce54a12014-03-10 12:19:46 -0700331#else
332 if (mAudioPolicyManager) {
333 mAudioPolicyManager->dump(fd);
334 }
335#endif
Mathias Agopian65ab4712010-07-14 17:59:35 -0700336
337 if (locked) mLock.unlock();
338 }
339 return NO_ERROR;
340}
341
342status_t AudioPolicyService::dumpPermissionDenial(int fd)
343{
344 const size_t SIZE = 256;
345 char buffer[SIZE];
346 String8 result;
347 snprintf(buffer, SIZE, "Permission Denial: "
348 "can't dump AudioPolicyService from pid=%d, uid=%d\n",
349 IPCThreadState::self()->getCallingPid(),
350 IPCThreadState::self()->getCallingUid());
351 result.append(buffer);
352 write(fd, result.string(), result.size());
353 return NO_ERROR;
354}
355
Glenn Kasten81872a22012-03-07 16:49:22 -0800356void AudioPolicyService::setPreProcessorEnabled(const InputDesc *inputDesc, bool enabled)
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700357{
Glenn Kasten81872a22012-03-07 16:49:22 -0800358 const Vector<sp<AudioEffect> > &fxVector = inputDesc->mEffects;
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700359 for (size_t i = 0; i < fxVector.size(); i++) {
Glenn Kastena1117922012-01-26 10:53:32 -0800360 fxVector.itemAt(i)->setEnabled(enabled);
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700361 }
362}
363
Mathias Agopian65ab4712010-07-14 17:59:35 -0700364status_t AudioPolicyService::onTransact(
365 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
366{
367 return BnAudioPolicyService::onTransact(code, data, reply, flags);
368}
369
370
Mathias Agopian65ab4712010-07-14 17:59:35 -0700371// ----------- AudioPolicyService::AudioCommandThread implementation ----------
372
Eric Laurentbfb1b832013-01-07 09:53:42 -0800373AudioPolicyService::AudioCommandThread::AudioCommandThread(String8 name,
374 const wp<AudioPolicyService>& service)
375 : Thread(false), mName(name), mService(service)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700376{
377 mpToneGenerator = NULL;
378}
379
380
381AudioPolicyService::AudioCommandThread::~AudioCommandThread()
382{
Eric Laurentbfb1b832013-01-07 09:53:42 -0800383 if (!mAudioCommands.isEmpty()) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700384 release_wake_lock(mName.string());
385 }
386 mAudioCommands.clear();
Glenn Kastene9dd0172012-01-27 18:08:45 -0800387 delete mpToneGenerator;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700388}
389
390void AudioPolicyService::AudioCommandThread::onFirstRef()
391{
Eric Laurentbfb1b832013-01-07 09:53:42 -0800392 run(mName.string(), ANDROID_PRIORITY_AUDIO);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700393}
394
395bool AudioPolicyService::AudioCommandThread::threadLoop()
396{
397 nsecs_t waitTime = INT64_MAX;
398
399 mLock.lock();
400 while (!exitPending())
401 {
Glenn Kastene53b9ea2012-03-12 16:29:55 -0700402 while (!mAudioCommands.isEmpty()) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700403 nsecs_t curTime = systemTime();
404 // commands are sorted by increasing time stamp: execute them from index 0 and up
405 if (mAudioCommands[0]->mTime <= curTime) {
Eric Laurent0ede8922014-05-09 18:04:42 -0700406 sp<AudioCommand> command = mAudioCommands[0];
Mathias Agopian65ab4712010-07-14 17:59:35 -0700407 mAudioCommands.removeAt(0);
Eric Laurent0ede8922014-05-09 18:04:42 -0700408 mLastCommand = command;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700409
410 switch (command->mCommand) {
411 case START_TONE: {
412 mLock.unlock();
Eric Laurent0ede8922014-05-09 18:04:42 -0700413 ToneData *data = (ToneData *)command->mParam.get();
Steve Block3856b092011-10-20 11:56:00 +0100414 ALOGV("AudioCommandThread() processing start tone %d on stream %d",
Mathias Agopian65ab4712010-07-14 17:59:35 -0700415 data->mType, data->mStream);
Glenn Kastene9dd0172012-01-27 18:08:45 -0800416 delete mpToneGenerator;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700417 mpToneGenerator = new ToneGenerator(data->mStream, 1.0);
418 mpToneGenerator->startTone(data->mType);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700419 mLock.lock();
420 }break;
421 case STOP_TONE: {
422 mLock.unlock();
Steve Block3856b092011-10-20 11:56:00 +0100423 ALOGV("AudioCommandThread() processing stop tone");
Mathias Agopian65ab4712010-07-14 17:59:35 -0700424 if (mpToneGenerator != NULL) {
425 mpToneGenerator->stopTone();
426 delete mpToneGenerator;
427 mpToneGenerator = NULL;
428 }
429 mLock.lock();
430 }break;
431 case SET_VOLUME: {
Eric Laurent0ede8922014-05-09 18:04:42 -0700432 VolumeData *data = (VolumeData *)command->mParam.get();
Steve Block3856b092011-10-20 11:56:00 +0100433 ALOGV("AudioCommandThread() processing set volume stream %d, \
Eric Laurentde070132010-07-13 04:45:46 -0700434 volume %f, output %d", data->mStream, data->mVolume, data->mIO);
435 command->mStatus = AudioSystem::setStreamVolume(data->mStream,
436 data->mVolume,
437 data->mIO);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700438 }break;
439 case SET_PARAMETERS: {
Eric Laurent0ede8922014-05-09 18:04:42 -0700440 ParametersData *data = (ParametersData *)command->mParam.get();
Glenn Kastene53b9ea2012-03-12 16:29:55 -0700441 ALOGV("AudioCommandThread() processing set parameters string %s, io %d",
442 data->mKeyValuePairs.string(), data->mIO);
443 command->mStatus = AudioSystem::setParameters(data->mIO, data->mKeyValuePairs);
Glenn Kastene53b9ea2012-03-12 16:29:55 -0700444 }break;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700445 case SET_VOICE_VOLUME: {
Eric Laurent0ede8922014-05-09 18:04:42 -0700446 VoiceVolumeData *data = (VoiceVolumeData *)command->mParam.get();
Steve Block3856b092011-10-20 11:56:00 +0100447 ALOGV("AudioCommandThread() processing set voice volume volume %f",
Eric Laurentde070132010-07-13 04:45:46 -0700448 data->mVolume);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700449 command->mStatus = AudioSystem::setVoiceVolume(data->mVolume);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700450 }break;
Eric Laurentbfb1b832013-01-07 09:53:42 -0800451 case STOP_OUTPUT: {
Eric Laurent0ede8922014-05-09 18:04:42 -0700452 StopOutputData *data = (StopOutputData *)command->mParam.get();
Eric Laurentbfb1b832013-01-07 09:53:42 -0800453 ALOGV("AudioCommandThread() processing stop output %d",
454 data->mIO);
455 sp<AudioPolicyService> svc = mService.promote();
456 if (svc == 0) {
457 break;
458 }
459 mLock.unlock();
460 svc->doStopOutput(data->mIO, data->mStream, data->mSession);
461 mLock.lock();
Eric Laurentbfb1b832013-01-07 09:53:42 -0800462 }break;
463 case RELEASE_OUTPUT: {
Eric Laurent0ede8922014-05-09 18:04:42 -0700464 ReleaseOutputData *data = (ReleaseOutputData *)command->mParam.get();
Eric Laurentbfb1b832013-01-07 09:53:42 -0800465 ALOGV("AudioCommandThread() processing release output %d",
466 data->mIO);
467 sp<AudioPolicyService> svc = mService.promote();
468 if (svc == 0) {
469 break;
470 }
471 mLock.unlock();
472 svc->doReleaseOutput(data->mIO);
473 mLock.lock();
Eric Laurentbfb1b832013-01-07 09:53:42 -0800474 }break;
Eric Laurent951f4552014-05-20 10:48:17 -0700475 case CREATE_AUDIO_PATCH: {
476 CreateAudioPatchData *data = (CreateAudioPatchData *)command->mParam.get();
477 ALOGV("AudioCommandThread() processing create audio patch");
478 sp<IAudioFlinger> af = AudioSystem::get_audio_flinger();
479 if (af == 0) {
480 command->mStatus = PERMISSION_DENIED;
481 } else {
482 command->mStatus = af->createAudioPatch(&data->mPatch, &data->mHandle);
483 }
484 } break;
485 case RELEASE_AUDIO_PATCH: {
486 ReleaseAudioPatchData *data = (ReleaseAudioPatchData *)command->mParam.get();
487 ALOGV("AudioCommandThread() processing release audio patch");
488 sp<IAudioFlinger> af = AudioSystem::get_audio_flinger();
489 if (af == 0) {
490 command->mStatus = PERMISSION_DENIED;
491 } else {
492 command->mStatus = af->releaseAudioPatch(data->mHandle);
493 }
494 } break;
Eric Laurentb52c1522014-05-20 11:27:36 -0700495 case UPDATE_AUDIOPORT_LIST: {
496 ALOGV("AudioCommandThread() processing update audio port list");
497 sp<AudioPolicyService> svc = mService.promote();
498 if (svc == 0) {
499 break;
500 }
501 mLock.unlock();
502 svc->doOnAudioPortListUpdate();
503 mLock.lock();
504 }break;
505 case UPDATE_AUDIOPATCH_LIST: {
506 ALOGV("AudioCommandThread() processing update audio patch list");
507 sp<AudioPolicyService> svc = mService.promote();
508 if (svc == 0) {
509 break;
510 }
511 mLock.unlock();
512 svc->doOnAudioPatchListUpdate();
513 mLock.lock();
514 }break;
Eric Laurente1715a42014-05-20 11:30:42 -0700515 case SET_AUDIOPORT_CONFIG: {
516 SetAudioPortConfigData *data = (SetAudioPortConfigData *)command->mParam.get();
517 ALOGV("AudioCommandThread() processing set port config");
518 sp<IAudioFlinger> af = AudioSystem::get_audio_flinger();
519 if (af == 0) {
520 command->mStatus = PERMISSION_DENIED;
521 } else {
522 command->mStatus = af->setAudioPortConfig(&data->mConfig);
523 }
524 } break;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700525 default:
Steve Block5ff1dd52012-01-05 23:22:43 +0000526 ALOGW("AudioCommandThread() unknown command %d", command->mCommand);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700527 }
Eric Laurent0ede8922014-05-09 18:04:42 -0700528 {
529 Mutex::Autolock _l(command->mLock);
530 if (command->mWaitStatus) {
531 command->mWaitStatus = false;
532 command->mCond.signal();
533 }
534 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700535 waitTime = INT64_MAX;
536 } else {
537 waitTime = mAudioCommands[0]->mTime - curTime;
538 break;
539 }
540 }
541 // release delayed commands wake lock
Eric Laurentbfb1b832013-01-07 09:53:42 -0800542 if (mAudioCommands.isEmpty()) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700543 release_wake_lock(mName.string());
544 }
Steve Block3856b092011-10-20 11:56:00 +0100545 ALOGV("AudioCommandThread() going to sleep");
Mathias Agopian65ab4712010-07-14 17:59:35 -0700546 mWaitWorkCV.waitRelative(mLock, waitTime);
Steve Block3856b092011-10-20 11:56:00 +0100547 ALOGV("AudioCommandThread() waking up");
Mathias Agopian65ab4712010-07-14 17:59:35 -0700548 }
549 mLock.unlock();
550 return false;
551}
552
553status_t AudioPolicyService::AudioCommandThread::dump(int fd)
554{
555 const size_t SIZE = 256;
556 char buffer[SIZE];
557 String8 result;
558
559 snprintf(buffer, SIZE, "AudioCommandThread %p Dump\n", this);
560 result.append(buffer);
561 write(fd, result.string(), result.size());
562
563 bool locked = tryLock(mLock);
564 if (!locked) {
565 String8 result2(kCmdDeadlockedString);
566 write(fd, result2.string(), result2.size());
567 }
568
569 snprintf(buffer, SIZE, "- Commands:\n");
570 result = String8(buffer);
571 result.append(" Command Time Wait pParam\n");
Glenn Kasten8d6a2442012-02-08 14:04:28 -0800572 for (size_t i = 0; i < mAudioCommands.size(); i++) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700573 mAudioCommands[i]->dump(buffer, SIZE);
574 result.append(buffer);
575 }
576 result.append(" Last Command\n");
Eric Laurent0ede8922014-05-09 18:04:42 -0700577 if (mLastCommand != 0) {
578 mLastCommand->dump(buffer, SIZE);
579 result.append(buffer);
580 } else {
581 result.append(" none\n");
582 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700583
584 write(fd, result.string(), result.size());
585
586 if (locked) mLock.unlock();
587
588 return NO_ERROR;
589}
590
Glenn Kasten3d2f8772012-01-27 15:25:25 -0800591void AudioPolicyService::AudioCommandThread::startToneCommand(ToneGenerator::tone_type type,
592 audio_stream_type_t stream)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700593{
Eric Laurent0ede8922014-05-09 18:04:42 -0700594 sp<AudioCommand> command = new AudioCommand();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700595 command->mCommand = START_TONE;
Eric Laurent0ede8922014-05-09 18:04:42 -0700596 sp<ToneData> data = new ToneData();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700597 data->mType = type;
598 data->mStream = stream;
Jesper Tragardh48412dc2014-03-24 14:12:43 +0100599 command->mParam = data;
Steve Block3856b092011-10-20 11:56:00 +0100600 ALOGV("AudioCommandThread() adding tone start type %d, stream %d", type, stream);
Eric Laurent0ede8922014-05-09 18:04:42 -0700601 sendCommand(command);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700602}
603
604void AudioPolicyService::AudioCommandThread::stopToneCommand()
605{
Eric Laurent0ede8922014-05-09 18:04:42 -0700606 sp<AudioCommand> command = new AudioCommand();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700607 command->mCommand = STOP_TONE;
Steve Block3856b092011-10-20 11:56:00 +0100608 ALOGV("AudioCommandThread() adding tone stop");
Eric Laurent0ede8922014-05-09 18:04:42 -0700609 sendCommand(command);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700610}
611
Glenn Kastenfff6d712012-01-12 16:38:12 -0800612status_t AudioPolicyService::AudioCommandThread::volumeCommand(audio_stream_type_t stream,
Eric Laurentde070132010-07-13 04:45:46 -0700613 float volume,
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800614 audio_io_handle_t output,
Eric Laurentde070132010-07-13 04:45:46 -0700615 int delayMs)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700616{
Eric Laurent0ede8922014-05-09 18:04:42 -0700617 sp<AudioCommand> command = new AudioCommand();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700618 command->mCommand = SET_VOLUME;
Eric Laurent0ede8922014-05-09 18:04:42 -0700619 sp<VolumeData> data = new VolumeData();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700620 data->mStream = stream;
621 data->mVolume = volume;
622 data->mIO = output;
623 command->mParam = data;
Eric Laurent0ede8922014-05-09 18:04:42 -0700624 command->mWaitStatus = true;
Steve Block3856b092011-10-20 11:56:00 +0100625 ALOGV("AudioCommandThread() adding set volume stream %d, volume %f, output %d",
Eric Laurentde070132010-07-13 04:45:46 -0700626 stream, volume, output);
Eric Laurent0ede8922014-05-09 18:04:42 -0700627 return sendCommand(command, delayMs);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700628}
629
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800630status_t AudioPolicyService::AudioCommandThread::parametersCommand(audio_io_handle_t ioHandle,
Dima Zavinfce7a472011-04-19 22:30:36 -0700631 const char *keyValuePairs,
Eric Laurentde070132010-07-13 04:45:46 -0700632 int delayMs)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700633{
Eric Laurent0ede8922014-05-09 18:04:42 -0700634 sp<AudioCommand> command = new AudioCommand();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700635 command->mCommand = SET_PARAMETERS;
Eric Laurent0ede8922014-05-09 18:04:42 -0700636 sp<ParametersData> data = new ParametersData();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700637 data->mIO = ioHandle;
Dima Zavinfce7a472011-04-19 22:30:36 -0700638 data->mKeyValuePairs = String8(keyValuePairs);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700639 command->mParam = data;
Eric Laurent0ede8922014-05-09 18:04:42 -0700640 command->mWaitStatus = true;
Steve Block3856b092011-10-20 11:56:00 +0100641 ALOGV("AudioCommandThread() adding set parameter string %s, io %d ,delay %d",
Dima Zavinfce7a472011-04-19 22:30:36 -0700642 keyValuePairs, ioHandle, delayMs);
Eric Laurent0ede8922014-05-09 18:04:42 -0700643 return sendCommand(command, delayMs);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700644}
645
646status_t AudioPolicyService::AudioCommandThread::voiceVolumeCommand(float volume, int delayMs)
647{
Eric Laurent0ede8922014-05-09 18:04:42 -0700648 sp<AudioCommand> command = new AudioCommand();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700649 command->mCommand = SET_VOICE_VOLUME;
Eric Laurent0ede8922014-05-09 18:04:42 -0700650 sp<VoiceVolumeData> data = new VoiceVolumeData();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700651 data->mVolume = volume;
652 command->mParam = data;
Eric Laurent0ede8922014-05-09 18:04:42 -0700653 command->mWaitStatus = true;
Steve Block3856b092011-10-20 11:56:00 +0100654 ALOGV("AudioCommandThread() adding set voice volume volume %f", volume);
Eric Laurent0ede8922014-05-09 18:04:42 -0700655 return sendCommand(command, delayMs);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700656}
657
Eric Laurentbfb1b832013-01-07 09:53:42 -0800658void AudioPolicyService::AudioCommandThread::stopOutputCommand(audio_io_handle_t output,
659 audio_stream_type_t stream,
660 int session)
661{
Eric Laurent0ede8922014-05-09 18:04:42 -0700662 sp<AudioCommand> command = new AudioCommand();
Eric Laurentbfb1b832013-01-07 09:53:42 -0800663 command->mCommand = STOP_OUTPUT;
Eric Laurent0ede8922014-05-09 18:04:42 -0700664 sp<StopOutputData> data = new StopOutputData();
Eric Laurentbfb1b832013-01-07 09:53:42 -0800665 data->mIO = output;
666 data->mStream = stream;
667 data->mSession = session;
Jesper Tragardh48412dc2014-03-24 14:12:43 +0100668 command->mParam = data;
Eric Laurentbfb1b832013-01-07 09:53:42 -0800669 ALOGV("AudioCommandThread() adding stop output %d", output);
Eric Laurent0ede8922014-05-09 18:04:42 -0700670 sendCommand(command);
Eric Laurentbfb1b832013-01-07 09:53:42 -0800671}
672
673void AudioPolicyService::AudioCommandThread::releaseOutputCommand(audio_io_handle_t output)
674{
Eric Laurent0ede8922014-05-09 18:04:42 -0700675 sp<AudioCommand> command = new AudioCommand();
Eric Laurentbfb1b832013-01-07 09:53:42 -0800676 command->mCommand = RELEASE_OUTPUT;
Eric Laurent0ede8922014-05-09 18:04:42 -0700677 sp<ReleaseOutputData> data = new ReleaseOutputData();
Eric Laurentbfb1b832013-01-07 09:53:42 -0800678 data->mIO = output;
Jesper Tragardh48412dc2014-03-24 14:12:43 +0100679 command->mParam = data;
Eric Laurentbfb1b832013-01-07 09:53:42 -0800680 ALOGV("AudioCommandThread() adding release output %d", output);
Eric Laurent0ede8922014-05-09 18:04:42 -0700681 sendCommand(command);
682}
683
Eric Laurent951f4552014-05-20 10:48:17 -0700684status_t AudioPolicyService::AudioCommandThread::createAudioPatchCommand(
685 const struct audio_patch *patch,
686 audio_patch_handle_t *handle,
687 int delayMs)
688{
689 status_t status = NO_ERROR;
690
691 sp<AudioCommand> command = new AudioCommand();
692 command->mCommand = CREATE_AUDIO_PATCH;
693 CreateAudioPatchData *data = new CreateAudioPatchData();
694 data->mPatch = *patch;
695 data->mHandle = *handle;
696 command->mParam = data;
697 command->mWaitStatus = true;
698 ALOGV("AudioCommandThread() adding create patch delay %d", delayMs);
699 status = sendCommand(command, delayMs);
700 if (status == NO_ERROR) {
701 *handle = data->mHandle;
702 }
703 return status;
704}
705
706status_t AudioPolicyService::AudioCommandThread::releaseAudioPatchCommand(audio_patch_handle_t handle,
707 int delayMs)
708{
709 sp<AudioCommand> command = new AudioCommand();
710 command->mCommand = RELEASE_AUDIO_PATCH;
711 ReleaseAudioPatchData *data = new ReleaseAudioPatchData();
712 data->mHandle = handle;
713 command->mParam = data;
714 command->mWaitStatus = true;
715 ALOGV("AudioCommandThread() adding release patch delay %d", delayMs);
716 return sendCommand(command, delayMs);
717}
718
Eric Laurentb52c1522014-05-20 11:27:36 -0700719void AudioPolicyService::AudioCommandThread::updateAudioPortListCommand()
720{
721 sp<AudioCommand> command = new AudioCommand();
722 command->mCommand = UPDATE_AUDIOPORT_LIST;
723 ALOGV("AudioCommandThread() adding update audio port list");
724 sendCommand(command);
725}
726
727void AudioPolicyService::AudioCommandThread::updateAudioPatchListCommand()
728{
729 sp<AudioCommand>command = new AudioCommand();
730 command->mCommand = UPDATE_AUDIOPATCH_LIST;
731 ALOGV("AudioCommandThread() adding update audio patch list");
732 sendCommand(command);
733}
734
Eric Laurente1715a42014-05-20 11:30:42 -0700735status_t AudioPolicyService::AudioCommandThread::setAudioPortConfigCommand(
736 const struct audio_port_config *config, int delayMs)
737{
738 sp<AudioCommand> command = new AudioCommand();
739 command->mCommand = SET_AUDIOPORT_CONFIG;
740 SetAudioPortConfigData *data = new SetAudioPortConfigData();
741 data->mConfig = *config;
742 command->mParam = data;
743 command->mWaitStatus = true;
744 ALOGV("AudioCommandThread() adding set port config delay %d", delayMs);
745 return sendCommand(command, delayMs);
746}
747
Eric Laurent0ede8922014-05-09 18:04:42 -0700748status_t AudioPolicyService::AudioCommandThread::sendCommand(sp<AudioCommand>& command, int delayMs)
749{
750 {
751 Mutex::Autolock _l(mLock);
752 insertCommand_l(command, delayMs);
753 mWaitWorkCV.signal();
754 }
755 Mutex::Autolock _l(command->mLock);
756 while (command->mWaitStatus) {
757 nsecs_t timeOutNs = kAudioCommandTimeoutNs + milliseconds(delayMs);
758 if (command->mCond.waitRelative(command->mLock, timeOutNs) != NO_ERROR) {
759 command->mStatus = TIMED_OUT;
760 command->mWaitStatus = false;
761 }
762 }
763 return command->mStatus;
Eric Laurentbfb1b832013-01-07 09:53:42 -0800764}
765
Mathias Agopian65ab4712010-07-14 17:59:35 -0700766// insertCommand_l() must be called with mLock held
Eric Laurent0ede8922014-05-09 18:04:42 -0700767void AudioPolicyService::AudioCommandThread::insertCommand_l(sp<AudioCommand>& command, int delayMs)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700768{
Glenn Kasten8d6a2442012-02-08 14:04:28 -0800769 ssize_t i; // not size_t because i will count down to -1
Eric Laurent0ede8922014-05-09 18:04:42 -0700770 Vector < sp<AudioCommand> > removedCommands;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700771 command->mTime = systemTime() + milliseconds(delayMs);
772
773 // acquire wake lock to make sure delayed commands are processed
Eric Laurentbfb1b832013-01-07 09:53:42 -0800774 if (mAudioCommands.isEmpty()) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700775 acquire_wake_lock(PARTIAL_WAKE_LOCK, mName.string());
776 }
777
778 // check same pending commands with later time stamps and eliminate them
779 for (i = mAudioCommands.size()-1; i >= 0; i--) {
Eric Laurent0ede8922014-05-09 18:04:42 -0700780 sp<AudioCommand> command2 = mAudioCommands[i];
Mathias Agopian65ab4712010-07-14 17:59:35 -0700781 // commands are sorted by increasing time stamp: no need to scan the rest of mAudioCommands
782 if (command2->mTime <= command->mTime) break;
783 if (command2->mCommand != command->mCommand) continue;
784
785 switch (command->mCommand) {
786 case SET_PARAMETERS: {
Eric Laurent0ede8922014-05-09 18:04:42 -0700787 ParametersData *data = (ParametersData *)command->mParam.get();
788 ParametersData *data2 = (ParametersData *)command2->mParam.get();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700789 if (data->mIO != data2->mIO) break;
Steve Block3856b092011-10-20 11:56:00 +0100790 ALOGV("Comparing parameter command %s to new command %s",
Eric Laurentde070132010-07-13 04:45:46 -0700791 data2->mKeyValuePairs.string(), data->mKeyValuePairs.string());
Mathias Agopian65ab4712010-07-14 17:59:35 -0700792 AudioParameter param = AudioParameter(data->mKeyValuePairs);
793 AudioParameter param2 = AudioParameter(data2->mKeyValuePairs);
794 for (size_t j = 0; j < param.size(); j++) {
Glenn Kastene53b9ea2012-03-12 16:29:55 -0700795 String8 key;
796 String8 value;
797 param.getAt(j, key, value);
798 for (size_t k = 0; k < param2.size(); k++) {
799 String8 key2;
800 String8 value2;
801 param2.getAt(k, key2, value2);
802 if (key2 == key) {
803 param2.remove(key2);
804 ALOGV("Filtering out parameter %s", key2.string());
805 break;
806 }
807 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700808 }
809 // if all keys have been filtered out, remove the command.
810 // otherwise, update the key value pairs
811 if (param2.size() == 0) {
812 removedCommands.add(command2);
813 } else {
814 data2->mKeyValuePairs = param2.toString();
815 }
Eric Laurent21e54562013-09-23 12:08:05 -0700816 command->mTime = command2->mTime;
817 // force delayMs to non 0 so that code below does not request to wait for
818 // command status as the command is now delayed
819 delayMs = 1;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700820 } break;
821
822 case SET_VOLUME: {
Eric Laurent0ede8922014-05-09 18:04:42 -0700823 VolumeData *data = (VolumeData *)command->mParam.get();
824 VolumeData *data2 = (VolumeData *)command2->mParam.get();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700825 if (data->mIO != data2->mIO) break;
826 if (data->mStream != data2->mStream) break;
Steve Block3856b092011-10-20 11:56:00 +0100827 ALOGV("Filtering out volume command on output %d for stream %d",
Eric Laurentde070132010-07-13 04:45:46 -0700828 data->mIO, data->mStream);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700829 removedCommands.add(command2);
Eric Laurent21e54562013-09-23 12:08:05 -0700830 command->mTime = command2->mTime;
831 // force delayMs to non 0 so that code below does not request to wait for
832 // command status as the command is now delayed
833 delayMs = 1;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700834 } break;
835 case START_TONE:
836 case STOP_TONE:
837 default:
838 break;
839 }
840 }
841
842 // remove filtered commands
843 for (size_t j = 0; j < removedCommands.size(); j++) {
844 // removed commands always have time stamps greater than current command
845 for (size_t k = i + 1; k < mAudioCommands.size(); k++) {
Eric Laurent0ede8922014-05-09 18:04:42 -0700846 if (mAudioCommands[k].get() == removedCommands[j].get()) {
Steve Block3856b092011-10-20 11:56:00 +0100847 ALOGV("suppressing command: %d", mAudioCommands[k]->mCommand);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700848 mAudioCommands.removeAt(k);
849 break;
850 }
851 }
852 }
853 removedCommands.clear();
854
Eric Laurent0ede8922014-05-09 18:04:42 -0700855 // Disable wait for status if delay is not 0
856 if (delayMs != 0) {
Eric Laurentcec4abb2012-07-03 12:23:02 -0700857 command->mWaitStatus = false;
858 }
Eric Laurentcec4abb2012-07-03 12:23:02 -0700859
Mathias Agopian65ab4712010-07-14 17:59:35 -0700860 // insert command at the right place according to its time stamp
Steve Block3856b092011-10-20 11:56:00 +0100861 ALOGV("inserting command: %d at index %d, num commands %d",
Eric Laurentde070132010-07-13 04:45:46 -0700862 command->mCommand, (int)i+1, mAudioCommands.size());
Mathias Agopian65ab4712010-07-14 17:59:35 -0700863 mAudioCommands.insertAt(command, i + 1);
864}
865
866void AudioPolicyService::AudioCommandThread::exit()
867{
Steve Block3856b092011-10-20 11:56:00 +0100868 ALOGV("AudioCommandThread::exit");
Mathias Agopian65ab4712010-07-14 17:59:35 -0700869 {
870 AutoMutex _l(mLock);
871 requestExit();
872 mWaitWorkCV.signal();
873 }
874 requestExitAndWait();
875}
876
877void AudioPolicyService::AudioCommandThread::AudioCommand::dump(char* buffer, size_t size)
878{
879 snprintf(buffer, size, " %02d %06d.%03d %01u %p\n",
880 mCommand,
881 (int)ns2s(mTime),
882 (int)ns2ms(mTime)%1000,
883 mWaitStatus,
Eric Laurent0ede8922014-05-09 18:04:42 -0700884 mParam.get());
Mathias Agopian65ab4712010-07-14 17:59:35 -0700885}
886
Dima Zavinfce7a472011-04-19 22:30:36 -0700887/******* helpers for the service_ops callbacks defined below *********/
888void AudioPolicyService::setParameters(audio_io_handle_t ioHandle,
889 const char *keyValuePairs,
890 int delayMs)
891{
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800892 mAudioCommandThread->parametersCommand(ioHandle, keyValuePairs,
Dima Zavinfce7a472011-04-19 22:30:36 -0700893 delayMs);
894}
895
896int AudioPolicyService::setStreamVolume(audio_stream_type_t stream,
897 float volume,
898 audio_io_handle_t output,
899 int delayMs)
900{
Glenn Kastenfff6d712012-01-12 16:38:12 -0800901 return (int)mAudioCommandThread->volumeCommand(stream, volume,
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800902 output, delayMs);
Dima Zavinfce7a472011-04-19 22:30:36 -0700903}
904
905int AudioPolicyService::startTone(audio_policy_tone_t tone,
906 audio_stream_type_t stream)
907{
Glenn Kasten6e2ebe92013-08-13 09:14:51 -0700908 if (tone != AUDIO_POLICY_TONE_IN_CALL_NOTIFICATION) {
Steve Block29357bc2012-01-06 19:20:56 +0000909 ALOGE("startTone: illegal tone requested (%d)", tone);
Glenn Kasten6e2ebe92013-08-13 09:14:51 -0700910 }
911 if (stream != AUDIO_STREAM_VOICE_CALL) {
Steve Block29357bc2012-01-06 19:20:56 +0000912 ALOGE("startTone: illegal stream (%d) requested for tone %d", stream,
Glenn Kastene53b9ea2012-03-12 16:29:55 -0700913 tone);
Glenn Kasten6e2ebe92013-08-13 09:14:51 -0700914 }
Dima Zavinfce7a472011-04-19 22:30:36 -0700915 mTonePlaybackThread->startToneCommand(ToneGenerator::TONE_SUP_CALL_WAITING,
916 AUDIO_STREAM_VOICE_CALL);
917 return 0;
918}
919
920int AudioPolicyService::stopTone()
921{
922 mTonePlaybackThread->stopToneCommand();
923 return 0;
924}
925
926int AudioPolicyService::setVoiceVolume(float volume, int delayMs)
927{
928 return (int)mAudioCommandThread->voiceVolumeCommand(volume, delayMs);
929}
930
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700931// ----------------------------------------------------------------------------
932// Audio pre-processing configuration
933// ----------------------------------------------------------------------------
934
Glenn Kasten8dad0e32012-01-09 08:41:22 -0800935/*static*/ const char * const AudioPolicyService::kInputSourceNames[AUDIO_SOURCE_CNT -1] = {
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700936 MIC_SRC_TAG,
937 VOICE_UL_SRC_TAG,
938 VOICE_DL_SRC_TAG,
939 VOICE_CALL_SRC_TAG,
940 CAMCORDER_SRC_TAG,
941 VOICE_REC_SRC_TAG,
942 VOICE_COMM_SRC_TAG
943};
944
945// returns the audio_source_t enum corresponding to the input source name or
946// AUDIO_SOURCE_CNT is no match found
947audio_source_t AudioPolicyService::inputSourceNameToEnum(const char *name)
948{
949 int i;
950 for (i = AUDIO_SOURCE_MIC; i < AUDIO_SOURCE_CNT; i++) {
951 if (strcmp(name, kInputSourceNames[i - AUDIO_SOURCE_MIC]) == 0) {
Steve Block3856b092011-10-20 11:56:00 +0100952 ALOGV("inputSourceNameToEnum found source %s %d", name, i);
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700953 break;
954 }
955 }
956 return (audio_source_t)i;
957}
958
959size_t AudioPolicyService::growParamSize(char *param,
960 size_t size,
961 size_t *curSize,
962 size_t *totSize)
963{
964 // *curSize is at least sizeof(effect_param_t) + 2 * sizeof(int)
965 size_t pos = ((*curSize - 1 ) / size + 1) * size;
966
967 if (pos + size > *totSize) {
968 while (pos + size > *totSize) {
969 *totSize += ((*totSize + 7) / 8) * 4;
970 }
971 param = (char *)realloc(param, *totSize);
972 }
973 *curSize = pos + size;
974 return pos;
975}
976
977size_t AudioPolicyService::readParamValue(cnode *node,
978 char *param,
979 size_t *curSize,
980 size_t *totSize)
981{
982 if (strncmp(node->name, SHORT_TAG, sizeof(SHORT_TAG) + 1) == 0) {
983 size_t pos = growParamSize(param, sizeof(short), curSize, totSize);
984 *(short *)((char *)param + pos) = (short)atoi(node->value);
Steve Block3856b092011-10-20 11:56:00 +0100985 ALOGV("readParamValue() reading short %d", *(short *)((char *)param + pos));
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700986 return sizeof(short);
987 } else if (strncmp(node->name, INT_TAG, sizeof(INT_TAG) + 1) == 0) {
988 size_t pos = growParamSize(param, sizeof(int), curSize, totSize);
989 *(int *)((char *)param + pos) = atoi(node->value);
Steve Block3856b092011-10-20 11:56:00 +0100990 ALOGV("readParamValue() reading int %d", *(int *)((char *)param + pos));
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700991 return sizeof(int);
992 } else if (strncmp(node->name, FLOAT_TAG, sizeof(FLOAT_TAG) + 1) == 0) {
993 size_t pos = growParamSize(param, sizeof(float), curSize, totSize);
994 *(float *)((char *)param + pos) = (float)atof(node->value);
Steve Block3856b092011-10-20 11:56:00 +0100995 ALOGV("readParamValue() reading float %f",*(float *)((char *)param + pos));
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700996 return sizeof(float);
997 } else if (strncmp(node->name, BOOL_TAG, sizeof(BOOL_TAG) + 1) == 0) {
998 size_t pos = growParamSize(param, sizeof(bool), curSize, totSize);
999 if (strncmp(node->value, "false", strlen("false") + 1) == 0) {
1000 *(bool *)((char *)param + pos) = false;
1001 } else {
1002 *(bool *)((char *)param + pos) = true;
1003 }
Steve Block3856b092011-10-20 11:56:00 +01001004 ALOGV("readParamValue() reading bool %s",*(bool *)((char *)param + pos) ? "true" : "false");
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001005 return sizeof(bool);
1006 } else if (strncmp(node->name, STRING_TAG, sizeof(STRING_TAG) + 1) == 0) {
1007 size_t len = strnlen(node->value, EFFECT_STRING_LEN_MAX);
1008 if (*curSize + len + 1 > *totSize) {
1009 *totSize = *curSize + len + 1;
1010 param = (char *)realloc(param, *totSize);
1011 }
1012 strncpy(param + *curSize, node->value, len);
1013 *curSize += len;
1014 param[*curSize] = '\0';
Steve Block3856b092011-10-20 11:56:00 +01001015 ALOGV("readParamValue() reading string %s", param + *curSize - len);
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001016 return len;
1017 }
Steve Block5ff1dd52012-01-05 23:22:43 +00001018 ALOGW("readParamValue() unknown param type %s", node->name);
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001019 return 0;
1020}
1021
1022effect_param_t *AudioPolicyService::loadEffectParameter(cnode *root)
1023{
1024 cnode *param;
1025 cnode *value;
1026 size_t curSize = sizeof(effect_param_t);
1027 size_t totSize = sizeof(effect_param_t) + 2 * sizeof(int);
1028 effect_param_t *fx_param = (effect_param_t *)malloc(totSize);
1029
1030 param = config_find(root, PARAM_TAG);
1031 value = config_find(root, VALUE_TAG);
1032 if (param == NULL && value == NULL) {
1033 // try to parse simple parameter form {int int}
1034 param = root->first_child;
Glenn Kastena0d68332012-01-27 16:47:15 -08001035 if (param != NULL) {
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001036 // Note: that a pair of random strings is read as 0 0
1037 int *ptr = (int *)fx_param->data;
1038 int *ptr2 = (int *)((char *)param + sizeof(effect_param_t));
Steve Block5ff1dd52012-01-05 23:22:43 +00001039 ALOGW("loadEffectParameter() ptr %p ptr2 %p", ptr, ptr2);
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001040 *ptr++ = atoi(param->name);
1041 *ptr = atoi(param->value);
1042 fx_param->psize = sizeof(int);
1043 fx_param->vsize = sizeof(int);
1044 return fx_param;
1045 }
1046 }
1047 if (param == NULL || value == NULL) {
Steve Block5ff1dd52012-01-05 23:22:43 +00001048 ALOGW("loadEffectParameter() invalid parameter description %s", root->name);
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001049 goto error;
1050 }
1051
1052 fx_param->psize = 0;
1053 param = param->first_child;
1054 while (param) {
Steve Block3856b092011-10-20 11:56:00 +01001055 ALOGV("loadEffectParameter() reading param of type %s", param->name);
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001056 size_t size = readParamValue(param, (char *)fx_param, &curSize, &totSize);
1057 if (size == 0) {
1058 goto error;
1059 }
1060 fx_param->psize += size;
1061 param = param->next;
1062 }
1063
1064 // align start of value field on 32 bit boundary
1065 curSize = ((curSize - 1 ) / sizeof(int) + 1) * sizeof(int);
1066
1067 fx_param->vsize = 0;
1068 value = value->first_child;
1069 while (value) {
Steve Block3856b092011-10-20 11:56:00 +01001070 ALOGV("loadEffectParameter() reading value of type %s", value->name);
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001071 size_t size = readParamValue(value, (char *)fx_param, &curSize, &totSize);
1072 if (size == 0) {
1073 goto error;
1074 }
1075 fx_param->vsize += size;
1076 value = value->next;
1077 }
1078
1079 return fx_param;
1080
1081error:
You Kimdb358af2013-09-12 20:48:08 +09001082 free(fx_param);
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001083 return NULL;
1084}
1085
1086void AudioPolicyService::loadEffectParameters(cnode *root, Vector <effect_param_t *>& params)
1087{
1088 cnode *node = root->first_child;
1089 while (node) {
Steve Block3856b092011-10-20 11:56:00 +01001090 ALOGV("loadEffectParameters() loading param %s", node->name);
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001091 effect_param_t *param = loadEffectParameter(node);
1092 if (param == NULL) {
1093 node = node->next;
1094 continue;
1095 }
1096 params.add(param);
1097 node = node->next;
1098 }
1099}
1100
1101AudioPolicyService::InputSourceDesc *AudioPolicyService::loadInputSource(
1102 cnode *root,
1103 const Vector <EffectDesc *>& effects)
1104{
1105 cnode *node = root->first_child;
1106 if (node == NULL) {
Steve Block5ff1dd52012-01-05 23:22:43 +00001107 ALOGW("loadInputSource() empty element %s", root->name);
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001108 return NULL;
1109 }
1110 InputSourceDesc *source = new InputSourceDesc();
1111 while (node) {
1112 size_t i;
1113 for (i = 0; i < effects.size(); i++) {
1114 if (strncmp(effects[i]->mName, node->name, EFFECT_STRING_LEN_MAX) == 0) {
Steve Block3856b092011-10-20 11:56:00 +01001115 ALOGV("loadInputSource() found effect %s in list", node->name);
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001116 break;
1117 }
1118 }
1119 if (i == effects.size()) {
Steve Block3856b092011-10-20 11:56:00 +01001120 ALOGV("loadInputSource() effect %s not in list", node->name);
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001121 node = node->next;
1122 continue;
1123 }
Glenn Kasten9fda4b82012-02-02 14:04:37 -08001124 EffectDesc *effect = new EffectDesc(*effects[i]); // deep copy
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001125 loadEffectParameters(node, effect->mParams);
Steve Block3856b092011-10-20 11:56:00 +01001126 ALOGV("loadInputSource() adding effect %s uuid %08x", effect->mName, effect->mUuid.timeLow);
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001127 source->mEffects.add(effect);
1128 node = node->next;
1129 }
1130 if (source->mEffects.size() == 0) {
Steve Block5ff1dd52012-01-05 23:22:43 +00001131 ALOGW("loadInputSource() no valid effects found in source %s", root->name);
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001132 delete source;
1133 return NULL;
1134 }
1135 return source;
1136}
1137
1138status_t AudioPolicyService::loadInputSources(cnode *root, const Vector <EffectDesc *>& effects)
1139{
1140 cnode *node = config_find(root, PREPROCESSING_TAG);
1141 if (node == NULL) {
1142 return -ENOENT;
1143 }
1144 node = node->first_child;
1145 while (node) {
1146 audio_source_t source = inputSourceNameToEnum(node->name);
1147 if (source == AUDIO_SOURCE_CNT) {
Steve Block5ff1dd52012-01-05 23:22:43 +00001148 ALOGW("loadInputSources() invalid input source %s", node->name);
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001149 node = node->next;
1150 continue;
1151 }
Steve Block3856b092011-10-20 11:56:00 +01001152 ALOGV("loadInputSources() loading input source %s", node->name);
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001153 InputSourceDesc *desc = loadInputSource(node, effects);
1154 if (desc == NULL) {
1155 node = node->next;
1156 continue;
1157 }
1158 mInputSources.add(source, desc);
1159 node = node->next;
1160 }
1161 return NO_ERROR;
1162}
1163
1164AudioPolicyService::EffectDesc *AudioPolicyService::loadEffect(cnode *root)
1165{
1166 cnode *node = config_find(root, UUID_TAG);
1167 if (node == NULL) {
1168 return NULL;
1169 }
1170 effect_uuid_t uuid;
1171 if (AudioEffect::stringToGuid(node->value, &uuid) != NO_ERROR) {
Steve Block5ff1dd52012-01-05 23:22:43 +00001172 ALOGW("loadEffect() invalid uuid %s", node->value);
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001173 return NULL;
1174 }
Glenn Kasten9fda4b82012-02-02 14:04:37 -08001175 return new EffectDesc(root->name, uuid);
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001176}
1177
1178status_t AudioPolicyService::loadEffects(cnode *root, Vector <EffectDesc *>& effects)
1179{
1180 cnode *node = config_find(root, EFFECTS_TAG);
1181 if (node == NULL) {
1182 return -ENOENT;
1183 }
1184 node = node->first_child;
1185 while (node) {
Steve Block3856b092011-10-20 11:56:00 +01001186 ALOGV("loadEffects() loading effect %s", node->name);
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001187 EffectDesc *effect = loadEffect(node);
1188 if (effect == NULL) {
1189 node = node->next;
1190 continue;
1191 }
1192 effects.add(effect);
1193 node = node->next;
1194 }
1195 return NO_ERROR;
1196}
1197
1198status_t AudioPolicyService::loadPreProcessorConfig(const char *path)
1199{
1200 cnode *root;
1201 char *data;
1202
1203 data = (char *)load_file(path, NULL);
1204 if (data == NULL) {
1205 return -ENODEV;
1206 }
1207 root = config_node("", "");
1208 config_load(root, data);
1209
1210 Vector <EffectDesc *> effects;
1211 loadEffects(root, effects);
1212 loadInputSources(root, effects);
1213
Yu Yezhonge6056ba2013-10-15 14:32:34 +08001214 // delete effects to fix memory leak.
1215 // as effects is local var and valgrind would treat this as memory leak
1216 // and although it only did in mediaserver init, but free it in case mediaserver reboot
1217 size_t i;
1218 for (i = 0; i < effects.size(); i++) {
1219 delete effects[i];
1220 }
1221
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001222 config_free(root);
1223 free(root);
1224 free(data);
1225
1226 return NO_ERROR;
1227}
1228
Dima Zavinfce7a472011-04-19 22:30:36 -07001229extern "C" {
Eric Laurent2d388ec2014-03-07 13:25:54 -08001230audio_module_handle_t aps_load_hw_module(void *service __unused,
1231 const char *name);
1232audio_io_handle_t aps_open_output(void *service __unused,
Eric Laurenta4c5a552012-03-29 10:12:40 -07001233 audio_devices_t *pDevices,
1234 uint32_t *pSamplingRate,
1235 audio_format_t *pFormat,
1236 audio_channel_mask_t *pChannelMask,
1237 uint32_t *pLatencyMs,
Eric Laurent2d388ec2014-03-07 13:25:54 -08001238 audio_output_flags_t flags);
Eric Laurenta4c5a552012-03-29 10:12:40 -07001239
Eric Laurent2d388ec2014-03-07 13:25:54 -08001240audio_io_handle_t aps_open_output_on_module(void *service __unused,
Eric Laurenta4c5a552012-03-29 10:12:40 -07001241 audio_module_handle_t module,
1242 audio_devices_t *pDevices,
1243 uint32_t *pSamplingRate,
1244 audio_format_t *pFormat,
1245 audio_channel_mask_t *pChannelMask,
1246 uint32_t *pLatencyMs,
Richard Fitzgeraldad3af332013-03-25 16:54:37 +00001247 audio_output_flags_t flags,
Eric Laurent2d388ec2014-03-07 13:25:54 -08001248 const audio_offload_info_t *offloadInfo);
1249audio_io_handle_t aps_open_dup_output(void *service __unused,
Dima Zavinfce7a472011-04-19 22:30:36 -07001250 audio_io_handle_t output1,
Eric Laurent2d388ec2014-03-07 13:25:54 -08001251 audio_io_handle_t output2);
1252int aps_close_output(void *service __unused, audio_io_handle_t output);
1253int aps_suspend_output(void *service __unused, audio_io_handle_t output);
1254int aps_restore_output(void *service __unused, audio_io_handle_t output);
1255audio_io_handle_t aps_open_input(void *service __unused,
Eric Laurenta4c5a552012-03-29 10:12:40 -07001256 audio_devices_t *pDevices,
1257 uint32_t *pSamplingRate,
1258 audio_format_t *pFormat,
1259 audio_channel_mask_t *pChannelMask,
Eric Laurent2d388ec2014-03-07 13:25:54 -08001260 audio_in_acoustics_t acoustics __unused);
1261audio_io_handle_t aps_open_input_on_module(void *service __unused,
Eric Laurenta4c5a552012-03-29 10:12:40 -07001262 audio_module_handle_t module,
1263 audio_devices_t *pDevices,
1264 uint32_t *pSamplingRate,
1265 audio_format_t *pFormat,
Eric Laurent2d388ec2014-03-07 13:25:54 -08001266 audio_channel_mask_t *pChannelMask);
1267int aps_close_input(void *service __unused, audio_io_handle_t input);
1268int aps_invalidate_stream(void *service __unused, audio_stream_type_t stream);
1269int aps_move_effects(void *service __unused, int session,
Dima Zavinfce7a472011-04-19 22:30:36 -07001270 audio_io_handle_t src_output,
Eric Laurent2d388ec2014-03-07 13:25:54 -08001271 audio_io_handle_t dst_output);
1272char * aps_get_parameters(void *service __unused, audio_io_handle_t io_handle,
1273 const char *keys);
1274void aps_set_parameters(void *service, audio_io_handle_t io_handle,
1275 const char *kv_pairs, int delay_ms);
1276int aps_set_stream_volume(void *service, audio_stream_type_t stream,
Dima Zavinfce7a472011-04-19 22:30:36 -07001277 float volume, audio_io_handle_t output,
Eric Laurent2d388ec2014-03-07 13:25:54 -08001278 int delay_ms);
1279int aps_start_tone(void *service, audio_policy_tone_t tone,
1280 audio_stream_type_t stream);
1281int aps_stop_tone(void *service);
1282int aps_set_voice_volume(void *service, float volume, int delay_ms);
1283};
Dima Zavinfce7a472011-04-19 22:30:36 -07001284
1285namespace {
1286 struct audio_policy_service_ops aps_ops = {
Glenn Kasten01d3acb2014-02-06 08:24:07 -08001287 .open_output = aps_open_output,
1288 .open_duplicate_output = aps_open_dup_output,
1289 .close_output = aps_close_output,
1290 .suspend_output = aps_suspend_output,
1291 .restore_output = aps_restore_output,
1292 .open_input = aps_open_input,
1293 .close_input = aps_close_input,
1294 .set_stream_volume = aps_set_stream_volume,
Glenn Kastend2304db2014-02-03 07:40:31 -08001295 .invalidate_stream = aps_invalidate_stream,
Glenn Kasten01d3acb2014-02-06 08:24:07 -08001296 .set_parameters = aps_set_parameters,
1297 .get_parameters = aps_get_parameters,
1298 .start_tone = aps_start_tone,
1299 .stop_tone = aps_stop_tone,
1300 .set_voice_volume = aps_set_voice_volume,
1301 .move_effects = aps_move_effects,
1302 .load_hw_module = aps_load_hw_module,
1303 .open_output_on_module = aps_open_output_on_module,
1304 .open_input_on_module = aps_open_input_on_module,
Dima Zavinfce7a472011-04-19 22:30:36 -07001305 };
1306}; // namespace <unnamed>
1307
Mathias Agopian65ab4712010-07-14 17:59:35 -07001308}; // namespace android