blob: f3d92ed5fe49590ce7091c64baeed0e80461ad22 [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
229AudioPolicyService::NotificationClient::NotificationClient(const sp<AudioPolicyService>& service,
230 const sp<IAudioPolicyServiceClient>& client,
231 uid_t uid)
232 : mService(service), mUid(uid), mAudioPolicyServiceClient(client)
233{
234}
235
236AudioPolicyService::NotificationClient::~NotificationClient()
237{
238}
239
240void AudioPolicyService::NotificationClient::binderDied(const wp<IBinder>& who __unused)
241{
242 sp<NotificationClient> keep(this);
243 sp<AudioPolicyService> service = mService.promote();
244 if (service != 0) {
245 service->removeNotificationClient(mUid);
246 }
247}
248
249void AudioPolicyService::NotificationClient::onAudioPortListUpdate()
250{
251 if (mAudioPolicyServiceClient != 0) {
252 mAudioPolicyServiceClient->onAudioPortListUpdate();
253 }
254}
255
256void AudioPolicyService::NotificationClient::onAudioPatchListUpdate()
257{
258 if (mAudioPolicyServiceClient != 0) {
259 mAudioPolicyServiceClient->onAudioPatchListUpdate();
260 }
261}
Eric Laurent57dae992011-07-24 13:36:09 -0700262
Mathias Agopian65ab4712010-07-14 17:59:35 -0700263void AudioPolicyService::binderDied(const wp<IBinder>& who) {
Glenn Kasten411e4472012-11-02 10:00:06 -0700264 ALOGW("binderDied() %p, calling pid %d", who.unsafe_get(),
Eric Laurentde070132010-07-13 04:45:46 -0700265 IPCThreadState::self()->getCallingPid());
Mathias Agopian65ab4712010-07-14 17:59:35 -0700266}
267
268static bool tryLock(Mutex& mutex)
269{
270 bool locked = false;
271 for (int i = 0; i < kDumpLockRetries; ++i) {
272 if (mutex.tryLock() == NO_ERROR) {
273 locked = true;
274 break;
275 }
Glenn Kasten22ecc912012-01-09 08:33:38 -0800276 usleep(kDumpLockSleepUs);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700277 }
278 return locked;
279}
280
281status_t AudioPolicyService::dumpInternals(int fd)
282{
283 const size_t SIZE = 256;
284 char buffer[SIZE];
285 String8 result;
286
Eric Laurentdce54a12014-03-10 12:19:46 -0700287#ifdef USE_LEGACY_AUDIO_POLICY
Dima Zavinfce7a472011-04-19 22:30:36 -0700288 snprintf(buffer, SIZE, "PolicyManager Interface: %p\n", mpAudioPolicy);
Eric Laurentdce54a12014-03-10 12:19:46 -0700289#else
290 snprintf(buffer, SIZE, "AudioPolicyManager: %p\n", mAudioPolicyManager);
291#endif
Mathias Agopian65ab4712010-07-14 17:59:35 -0700292 result.append(buffer);
293 snprintf(buffer, SIZE, "Command Thread: %p\n", mAudioCommandThread.get());
294 result.append(buffer);
295 snprintf(buffer, SIZE, "Tones Thread: %p\n", mTonePlaybackThread.get());
296 result.append(buffer);
297
298 write(fd, result.string(), result.size());
299 return NO_ERROR;
300}
301
Glenn Kasten0f11b512014-01-31 16:18:54 -0800302status_t AudioPolicyService::dump(int fd, const Vector<String16>& args __unused)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700303{
Glenn Kasten44deb052012-02-05 18:09:08 -0800304 if (!dumpAllowed()) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700305 dumpPermissionDenial(fd);
306 } else {
307 bool locked = tryLock(mLock);
308 if (!locked) {
309 String8 result(kDeadlockedString);
310 write(fd, result.string(), result.size());
311 }
312
313 dumpInternals(fd);
Glenn Kasten9d1f02d2012-02-08 17:47:58 -0800314 if (mAudioCommandThread != 0) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700315 mAudioCommandThread->dump(fd);
316 }
Glenn Kasten9d1f02d2012-02-08 17:47:58 -0800317 if (mTonePlaybackThread != 0) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700318 mTonePlaybackThread->dump(fd);
319 }
320
Eric Laurentdce54a12014-03-10 12:19:46 -0700321#ifdef USE_LEGACY_AUDIO_POLICY
Dima Zavinfce7a472011-04-19 22:30:36 -0700322 if (mpAudioPolicy) {
323 mpAudioPolicy->dump(mpAudioPolicy, fd);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700324 }
Eric Laurentdce54a12014-03-10 12:19:46 -0700325#else
326 if (mAudioPolicyManager) {
327 mAudioPolicyManager->dump(fd);
328 }
329#endif
Mathias Agopian65ab4712010-07-14 17:59:35 -0700330
331 if (locked) mLock.unlock();
332 }
333 return NO_ERROR;
334}
335
336status_t AudioPolicyService::dumpPermissionDenial(int fd)
337{
338 const size_t SIZE = 256;
339 char buffer[SIZE];
340 String8 result;
341 snprintf(buffer, SIZE, "Permission Denial: "
342 "can't dump AudioPolicyService from pid=%d, uid=%d\n",
343 IPCThreadState::self()->getCallingPid(),
344 IPCThreadState::self()->getCallingUid());
345 result.append(buffer);
346 write(fd, result.string(), result.size());
347 return NO_ERROR;
348}
349
Glenn Kasten81872a22012-03-07 16:49:22 -0800350void AudioPolicyService::setPreProcessorEnabled(const InputDesc *inputDesc, bool enabled)
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700351{
Glenn Kasten81872a22012-03-07 16:49:22 -0800352 const Vector<sp<AudioEffect> > &fxVector = inputDesc->mEffects;
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700353 for (size_t i = 0; i < fxVector.size(); i++) {
Glenn Kastena1117922012-01-26 10:53:32 -0800354 fxVector.itemAt(i)->setEnabled(enabled);
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700355 }
356}
357
Mathias Agopian65ab4712010-07-14 17:59:35 -0700358status_t AudioPolicyService::onTransact(
359 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
360{
361 return BnAudioPolicyService::onTransact(code, data, reply, flags);
362}
363
364
Mathias Agopian65ab4712010-07-14 17:59:35 -0700365// ----------- AudioPolicyService::AudioCommandThread implementation ----------
366
Eric Laurentbfb1b832013-01-07 09:53:42 -0800367AudioPolicyService::AudioCommandThread::AudioCommandThread(String8 name,
368 const wp<AudioPolicyService>& service)
369 : Thread(false), mName(name), mService(service)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700370{
371 mpToneGenerator = NULL;
372}
373
374
375AudioPolicyService::AudioCommandThread::~AudioCommandThread()
376{
Eric Laurentbfb1b832013-01-07 09:53:42 -0800377 if (!mAudioCommands.isEmpty()) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700378 release_wake_lock(mName.string());
379 }
380 mAudioCommands.clear();
Glenn Kastene9dd0172012-01-27 18:08:45 -0800381 delete mpToneGenerator;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700382}
383
384void AudioPolicyService::AudioCommandThread::onFirstRef()
385{
Eric Laurentbfb1b832013-01-07 09:53:42 -0800386 run(mName.string(), ANDROID_PRIORITY_AUDIO);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700387}
388
389bool AudioPolicyService::AudioCommandThread::threadLoop()
390{
391 nsecs_t waitTime = INT64_MAX;
392
393 mLock.lock();
394 while (!exitPending())
395 {
Glenn Kastene53b9ea2012-03-12 16:29:55 -0700396 while (!mAudioCommands.isEmpty()) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700397 nsecs_t curTime = systemTime();
398 // commands are sorted by increasing time stamp: execute them from index 0 and up
399 if (mAudioCommands[0]->mTime <= curTime) {
Eric Laurent0ede8922014-05-09 18:04:42 -0700400 sp<AudioCommand> command = mAudioCommands[0];
Mathias Agopian65ab4712010-07-14 17:59:35 -0700401 mAudioCommands.removeAt(0);
Eric Laurent0ede8922014-05-09 18:04:42 -0700402 mLastCommand = command;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700403
404 switch (command->mCommand) {
405 case START_TONE: {
406 mLock.unlock();
Eric Laurent0ede8922014-05-09 18:04:42 -0700407 ToneData *data = (ToneData *)command->mParam.get();
Steve Block3856b092011-10-20 11:56:00 +0100408 ALOGV("AudioCommandThread() processing start tone %d on stream %d",
Mathias Agopian65ab4712010-07-14 17:59:35 -0700409 data->mType, data->mStream);
Glenn Kastene9dd0172012-01-27 18:08:45 -0800410 delete mpToneGenerator;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700411 mpToneGenerator = new ToneGenerator(data->mStream, 1.0);
412 mpToneGenerator->startTone(data->mType);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700413 mLock.lock();
414 }break;
415 case STOP_TONE: {
416 mLock.unlock();
Steve Block3856b092011-10-20 11:56:00 +0100417 ALOGV("AudioCommandThread() processing stop tone");
Mathias Agopian65ab4712010-07-14 17:59:35 -0700418 if (mpToneGenerator != NULL) {
419 mpToneGenerator->stopTone();
420 delete mpToneGenerator;
421 mpToneGenerator = NULL;
422 }
423 mLock.lock();
424 }break;
425 case SET_VOLUME: {
Eric Laurent0ede8922014-05-09 18:04:42 -0700426 VolumeData *data = (VolumeData *)command->mParam.get();
Steve Block3856b092011-10-20 11:56:00 +0100427 ALOGV("AudioCommandThread() processing set volume stream %d, \
Eric Laurentde070132010-07-13 04:45:46 -0700428 volume %f, output %d", data->mStream, data->mVolume, data->mIO);
429 command->mStatus = AudioSystem::setStreamVolume(data->mStream,
430 data->mVolume,
431 data->mIO);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700432 }break;
433 case SET_PARAMETERS: {
Eric Laurent0ede8922014-05-09 18:04:42 -0700434 ParametersData *data = (ParametersData *)command->mParam.get();
Glenn Kastene53b9ea2012-03-12 16:29:55 -0700435 ALOGV("AudioCommandThread() processing set parameters string %s, io %d",
436 data->mKeyValuePairs.string(), data->mIO);
437 command->mStatus = AudioSystem::setParameters(data->mIO, data->mKeyValuePairs);
Glenn Kastene53b9ea2012-03-12 16:29:55 -0700438 }break;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700439 case SET_VOICE_VOLUME: {
Eric Laurent0ede8922014-05-09 18:04:42 -0700440 VoiceVolumeData *data = (VoiceVolumeData *)command->mParam.get();
Steve Block3856b092011-10-20 11:56:00 +0100441 ALOGV("AudioCommandThread() processing set voice volume volume %f",
Eric Laurentde070132010-07-13 04:45:46 -0700442 data->mVolume);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700443 command->mStatus = AudioSystem::setVoiceVolume(data->mVolume);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700444 }break;
Eric Laurentbfb1b832013-01-07 09:53:42 -0800445 case STOP_OUTPUT: {
Eric Laurent0ede8922014-05-09 18:04:42 -0700446 StopOutputData *data = (StopOutputData *)command->mParam.get();
Eric Laurentbfb1b832013-01-07 09:53:42 -0800447 ALOGV("AudioCommandThread() processing stop output %d",
448 data->mIO);
449 sp<AudioPolicyService> svc = mService.promote();
450 if (svc == 0) {
451 break;
452 }
453 mLock.unlock();
454 svc->doStopOutput(data->mIO, data->mStream, data->mSession);
455 mLock.lock();
Eric Laurentbfb1b832013-01-07 09:53:42 -0800456 }break;
457 case RELEASE_OUTPUT: {
Eric Laurent0ede8922014-05-09 18:04:42 -0700458 ReleaseOutputData *data = (ReleaseOutputData *)command->mParam.get();
Eric Laurentbfb1b832013-01-07 09:53:42 -0800459 ALOGV("AudioCommandThread() processing release output %d",
460 data->mIO);
461 sp<AudioPolicyService> svc = mService.promote();
462 if (svc == 0) {
463 break;
464 }
465 mLock.unlock();
466 svc->doReleaseOutput(data->mIO);
467 mLock.lock();
Eric Laurentbfb1b832013-01-07 09:53:42 -0800468 }break;
Eric Laurent951f4552014-05-20 10:48:17 -0700469 case CREATE_AUDIO_PATCH: {
470 CreateAudioPatchData *data = (CreateAudioPatchData *)command->mParam.get();
471 ALOGV("AudioCommandThread() processing create audio patch");
472 sp<IAudioFlinger> af = AudioSystem::get_audio_flinger();
473 if (af == 0) {
474 command->mStatus = PERMISSION_DENIED;
475 } else {
476 command->mStatus = af->createAudioPatch(&data->mPatch, &data->mHandle);
477 }
478 } break;
479 case RELEASE_AUDIO_PATCH: {
480 ReleaseAudioPatchData *data = (ReleaseAudioPatchData *)command->mParam.get();
481 ALOGV("AudioCommandThread() processing release audio patch");
482 sp<IAudioFlinger> af = AudioSystem::get_audio_flinger();
483 if (af == 0) {
484 command->mStatus = PERMISSION_DENIED;
485 } else {
486 command->mStatus = af->releaseAudioPatch(data->mHandle);
487 }
488 } break;
Eric Laurentb52c1522014-05-20 11:27:36 -0700489 case UPDATE_AUDIOPORT_LIST: {
490 ALOGV("AudioCommandThread() processing update audio port list");
491 sp<AudioPolicyService> svc = mService.promote();
492 if (svc == 0) {
493 break;
494 }
495 mLock.unlock();
496 svc->doOnAudioPortListUpdate();
497 mLock.lock();
498 }break;
499 case UPDATE_AUDIOPATCH_LIST: {
500 ALOGV("AudioCommandThread() processing update audio patch list");
501 sp<AudioPolicyService> svc = mService.promote();
502 if (svc == 0) {
503 break;
504 }
505 mLock.unlock();
506 svc->doOnAudioPatchListUpdate();
507 mLock.lock();
508 }break;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700509 default:
Steve Block5ff1dd52012-01-05 23:22:43 +0000510 ALOGW("AudioCommandThread() unknown command %d", command->mCommand);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700511 }
Eric Laurent0ede8922014-05-09 18:04:42 -0700512 {
513 Mutex::Autolock _l(command->mLock);
514 if (command->mWaitStatus) {
515 command->mWaitStatus = false;
516 command->mCond.signal();
517 }
518 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700519 waitTime = INT64_MAX;
520 } else {
521 waitTime = mAudioCommands[0]->mTime - curTime;
522 break;
523 }
524 }
525 // release delayed commands wake lock
Eric Laurentbfb1b832013-01-07 09:53:42 -0800526 if (mAudioCommands.isEmpty()) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700527 release_wake_lock(mName.string());
528 }
Steve Block3856b092011-10-20 11:56:00 +0100529 ALOGV("AudioCommandThread() going to sleep");
Mathias Agopian65ab4712010-07-14 17:59:35 -0700530 mWaitWorkCV.waitRelative(mLock, waitTime);
Steve Block3856b092011-10-20 11:56:00 +0100531 ALOGV("AudioCommandThread() waking up");
Mathias Agopian65ab4712010-07-14 17:59:35 -0700532 }
533 mLock.unlock();
534 return false;
535}
536
537status_t AudioPolicyService::AudioCommandThread::dump(int fd)
538{
539 const size_t SIZE = 256;
540 char buffer[SIZE];
541 String8 result;
542
543 snprintf(buffer, SIZE, "AudioCommandThread %p Dump\n", this);
544 result.append(buffer);
545 write(fd, result.string(), result.size());
546
547 bool locked = tryLock(mLock);
548 if (!locked) {
549 String8 result2(kCmdDeadlockedString);
550 write(fd, result2.string(), result2.size());
551 }
552
553 snprintf(buffer, SIZE, "- Commands:\n");
554 result = String8(buffer);
555 result.append(" Command Time Wait pParam\n");
Glenn Kasten8d6a2442012-02-08 14:04:28 -0800556 for (size_t i = 0; i < mAudioCommands.size(); i++) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700557 mAudioCommands[i]->dump(buffer, SIZE);
558 result.append(buffer);
559 }
560 result.append(" Last Command\n");
Eric Laurent0ede8922014-05-09 18:04:42 -0700561 if (mLastCommand != 0) {
562 mLastCommand->dump(buffer, SIZE);
563 result.append(buffer);
564 } else {
565 result.append(" none\n");
566 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700567
568 write(fd, result.string(), result.size());
569
570 if (locked) mLock.unlock();
571
572 return NO_ERROR;
573}
574
Glenn Kasten3d2f8772012-01-27 15:25:25 -0800575void AudioPolicyService::AudioCommandThread::startToneCommand(ToneGenerator::tone_type type,
576 audio_stream_type_t stream)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700577{
Eric Laurent0ede8922014-05-09 18:04:42 -0700578 sp<AudioCommand> command = new AudioCommand();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700579 command->mCommand = START_TONE;
Eric Laurent0ede8922014-05-09 18:04:42 -0700580 sp<ToneData> data = new ToneData();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700581 data->mType = type;
582 data->mStream = stream;
Jesper Tragardh48412dc2014-03-24 14:12:43 +0100583 command->mParam = data;
Steve Block3856b092011-10-20 11:56:00 +0100584 ALOGV("AudioCommandThread() adding tone start type %d, stream %d", type, stream);
Eric Laurent0ede8922014-05-09 18:04:42 -0700585 sendCommand(command);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700586}
587
588void AudioPolicyService::AudioCommandThread::stopToneCommand()
589{
Eric Laurent0ede8922014-05-09 18:04:42 -0700590 sp<AudioCommand> command = new AudioCommand();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700591 command->mCommand = STOP_TONE;
Steve Block3856b092011-10-20 11:56:00 +0100592 ALOGV("AudioCommandThread() adding tone stop");
Eric Laurent0ede8922014-05-09 18:04:42 -0700593 sendCommand(command);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700594}
595
Glenn Kastenfff6d712012-01-12 16:38:12 -0800596status_t AudioPolicyService::AudioCommandThread::volumeCommand(audio_stream_type_t stream,
Eric Laurentde070132010-07-13 04:45:46 -0700597 float volume,
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800598 audio_io_handle_t output,
Eric Laurentde070132010-07-13 04:45:46 -0700599 int delayMs)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700600{
Eric Laurent0ede8922014-05-09 18:04:42 -0700601 sp<AudioCommand> command = new AudioCommand();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700602 command->mCommand = SET_VOLUME;
Eric Laurent0ede8922014-05-09 18:04:42 -0700603 sp<VolumeData> data = new VolumeData();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700604 data->mStream = stream;
605 data->mVolume = volume;
606 data->mIO = output;
607 command->mParam = data;
Eric Laurent0ede8922014-05-09 18:04:42 -0700608 command->mWaitStatus = true;
Steve Block3856b092011-10-20 11:56:00 +0100609 ALOGV("AudioCommandThread() adding set volume stream %d, volume %f, output %d",
Eric Laurentde070132010-07-13 04:45:46 -0700610 stream, volume, output);
Eric Laurent0ede8922014-05-09 18:04:42 -0700611 return sendCommand(command, delayMs);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700612}
613
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800614status_t AudioPolicyService::AudioCommandThread::parametersCommand(audio_io_handle_t ioHandle,
Dima Zavinfce7a472011-04-19 22:30:36 -0700615 const char *keyValuePairs,
Eric Laurentde070132010-07-13 04:45:46 -0700616 int delayMs)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700617{
Eric Laurent0ede8922014-05-09 18:04:42 -0700618 sp<AudioCommand> command = new AudioCommand();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700619 command->mCommand = SET_PARAMETERS;
Eric Laurent0ede8922014-05-09 18:04:42 -0700620 sp<ParametersData> data = new ParametersData();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700621 data->mIO = ioHandle;
Dima Zavinfce7a472011-04-19 22:30:36 -0700622 data->mKeyValuePairs = String8(keyValuePairs);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700623 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 parameter string %s, io %d ,delay %d",
Dima Zavinfce7a472011-04-19 22:30:36 -0700626 keyValuePairs, ioHandle, delayMs);
Eric Laurent0ede8922014-05-09 18:04:42 -0700627 return sendCommand(command, delayMs);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700628}
629
630status_t AudioPolicyService::AudioCommandThread::voiceVolumeCommand(float volume, int delayMs)
631{
Eric Laurent0ede8922014-05-09 18:04:42 -0700632 sp<AudioCommand> command = new AudioCommand();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700633 command->mCommand = SET_VOICE_VOLUME;
Eric Laurent0ede8922014-05-09 18:04:42 -0700634 sp<VoiceVolumeData> data = new VoiceVolumeData();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700635 data->mVolume = volume;
636 command->mParam = data;
Eric Laurent0ede8922014-05-09 18:04:42 -0700637 command->mWaitStatus = true;
Steve Block3856b092011-10-20 11:56:00 +0100638 ALOGV("AudioCommandThread() adding set voice volume volume %f", volume);
Eric Laurent0ede8922014-05-09 18:04:42 -0700639 return sendCommand(command, delayMs);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700640}
641
Eric Laurentbfb1b832013-01-07 09:53:42 -0800642void AudioPolicyService::AudioCommandThread::stopOutputCommand(audio_io_handle_t output,
643 audio_stream_type_t stream,
644 int session)
645{
Eric Laurent0ede8922014-05-09 18:04:42 -0700646 sp<AudioCommand> command = new AudioCommand();
Eric Laurentbfb1b832013-01-07 09:53:42 -0800647 command->mCommand = STOP_OUTPUT;
Eric Laurent0ede8922014-05-09 18:04:42 -0700648 sp<StopOutputData> data = new StopOutputData();
Eric Laurentbfb1b832013-01-07 09:53:42 -0800649 data->mIO = output;
650 data->mStream = stream;
651 data->mSession = session;
Jesper Tragardh48412dc2014-03-24 14:12:43 +0100652 command->mParam = data;
Eric Laurentbfb1b832013-01-07 09:53:42 -0800653 ALOGV("AudioCommandThread() adding stop output %d", output);
Eric Laurent0ede8922014-05-09 18:04:42 -0700654 sendCommand(command);
Eric Laurentbfb1b832013-01-07 09:53:42 -0800655}
656
657void AudioPolicyService::AudioCommandThread::releaseOutputCommand(audio_io_handle_t output)
658{
Eric Laurent0ede8922014-05-09 18:04:42 -0700659 sp<AudioCommand> command = new AudioCommand();
Eric Laurentbfb1b832013-01-07 09:53:42 -0800660 command->mCommand = RELEASE_OUTPUT;
Eric Laurent0ede8922014-05-09 18:04:42 -0700661 sp<ReleaseOutputData> data = new ReleaseOutputData();
Eric Laurentbfb1b832013-01-07 09:53:42 -0800662 data->mIO = output;
Jesper Tragardh48412dc2014-03-24 14:12:43 +0100663 command->mParam = data;
Eric Laurentbfb1b832013-01-07 09:53:42 -0800664 ALOGV("AudioCommandThread() adding release output %d", output);
Eric Laurent0ede8922014-05-09 18:04:42 -0700665 sendCommand(command);
666}
667
Eric Laurent951f4552014-05-20 10:48:17 -0700668status_t AudioPolicyService::AudioCommandThread::createAudioPatchCommand(
669 const struct audio_patch *patch,
670 audio_patch_handle_t *handle,
671 int delayMs)
672{
673 status_t status = NO_ERROR;
674
675 sp<AudioCommand> command = new AudioCommand();
676 command->mCommand = CREATE_AUDIO_PATCH;
677 CreateAudioPatchData *data = new CreateAudioPatchData();
678 data->mPatch = *patch;
679 data->mHandle = *handle;
680 command->mParam = data;
681 command->mWaitStatus = true;
682 ALOGV("AudioCommandThread() adding create patch delay %d", delayMs);
683 status = sendCommand(command, delayMs);
684 if (status == NO_ERROR) {
685 *handle = data->mHandle;
686 }
687 return status;
688}
689
690status_t AudioPolicyService::AudioCommandThread::releaseAudioPatchCommand(audio_patch_handle_t handle,
691 int delayMs)
692{
693 sp<AudioCommand> command = new AudioCommand();
694 command->mCommand = RELEASE_AUDIO_PATCH;
695 ReleaseAudioPatchData *data = new ReleaseAudioPatchData();
696 data->mHandle = handle;
697 command->mParam = data;
698 command->mWaitStatus = true;
699 ALOGV("AudioCommandThread() adding release patch delay %d", delayMs);
700 return sendCommand(command, delayMs);
701}
702
Eric Laurentb52c1522014-05-20 11:27:36 -0700703void AudioPolicyService::AudioCommandThread::updateAudioPortListCommand()
704{
705 sp<AudioCommand> command = new AudioCommand();
706 command->mCommand = UPDATE_AUDIOPORT_LIST;
707 ALOGV("AudioCommandThread() adding update audio port list");
708 sendCommand(command);
709}
710
711void AudioPolicyService::AudioCommandThread::updateAudioPatchListCommand()
712{
713 sp<AudioCommand>command = new AudioCommand();
714 command->mCommand = UPDATE_AUDIOPATCH_LIST;
715 ALOGV("AudioCommandThread() adding update audio patch list");
716 sendCommand(command);
717}
718
Eric Laurent0ede8922014-05-09 18:04:42 -0700719status_t AudioPolicyService::AudioCommandThread::sendCommand(sp<AudioCommand>& command, int delayMs)
720{
721 {
722 Mutex::Autolock _l(mLock);
723 insertCommand_l(command, delayMs);
724 mWaitWorkCV.signal();
725 }
726 Mutex::Autolock _l(command->mLock);
727 while (command->mWaitStatus) {
728 nsecs_t timeOutNs = kAudioCommandTimeoutNs + milliseconds(delayMs);
729 if (command->mCond.waitRelative(command->mLock, timeOutNs) != NO_ERROR) {
730 command->mStatus = TIMED_OUT;
731 command->mWaitStatus = false;
732 }
733 }
734 return command->mStatus;
Eric Laurentbfb1b832013-01-07 09:53:42 -0800735}
736
Mathias Agopian65ab4712010-07-14 17:59:35 -0700737// insertCommand_l() must be called with mLock held
Eric Laurent0ede8922014-05-09 18:04:42 -0700738void AudioPolicyService::AudioCommandThread::insertCommand_l(sp<AudioCommand>& command, int delayMs)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700739{
Glenn Kasten8d6a2442012-02-08 14:04:28 -0800740 ssize_t i; // not size_t because i will count down to -1
Eric Laurent0ede8922014-05-09 18:04:42 -0700741 Vector < sp<AudioCommand> > removedCommands;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700742 command->mTime = systemTime() + milliseconds(delayMs);
743
744 // acquire wake lock to make sure delayed commands are processed
Eric Laurentbfb1b832013-01-07 09:53:42 -0800745 if (mAudioCommands.isEmpty()) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700746 acquire_wake_lock(PARTIAL_WAKE_LOCK, mName.string());
747 }
748
749 // check same pending commands with later time stamps and eliminate them
750 for (i = mAudioCommands.size()-1; i >= 0; i--) {
Eric Laurent0ede8922014-05-09 18:04:42 -0700751 sp<AudioCommand> command2 = mAudioCommands[i];
Mathias Agopian65ab4712010-07-14 17:59:35 -0700752 // commands are sorted by increasing time stamp: no need to scan the rest of mAudioCommands
753 if (command2->mTime <= command->mTime) break;
754 if (command2->mCommand != command->mCommand) continue;
755
756 switch (command->mCommand) {
757 case SET_PARAMETERS: {
Eric Laurent0ede8922014-05-09 18:04:42 -0700758 ParametersData *data = (ParametersData *)command->mParam.get();
759 ParametersData *data2 = (ParametersData *)command2->mParam.get();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700760 if (data->mIO != data2->mIO) break;
Steve Block3856b092011-10-20 11:56:00 +0100761 ALOGV("Comparing parameter command %s to new command %s",
Eric Laurentde070132010-07-13 04:45:46 -0700762 data2->mKeyValuePairs.string(), data->mKeyValuePairs.string());
Mathias Agopian65ab4712010-07-14 17:59:35 -0700763 AudioParameter param = AudioParameter(data->mKeyValuePairs);
764 AudioParameter param2 = AudioParameter(data2->mKeyValuePairs);
765 for (size_t j = 0; j < param.size(); j++) {
Glenn Kastene53b9ea2012-03-12 16:29:55 -0700766 String8 key;
767 String8 value;
768 param.getAt(j, key, value);
769 for (size_t k = 0; k < param2.size(); k++) {
770 String8 key2;
771 String8 value2;
772 param2.getAt(k, key2, value2);
773 if (key2 == key) {
774 param2.remove(key2);
775 ALOGV("Filtering out parameter %s", key2.string());
776 break;
777 }
778 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700779 }
780 // if all keys have been filtered out, remove the command.
781 // otherwise, update the key value pairs
782 if (param2.size() == 0) {
783 removedCommands.add(command2);
784 } else {
785 data2->mKeyValuePairs = param2.toString();
786 }
Eric Laurent21e54562013-09-23 12:08:05 -0700787 command->mTime = command2->mTime;
788 // force delayMs to non 0 so that code below does not request to wait for
789 // command status as the command is now delayed
790 delayMs = 1;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700791 } break;
792
793 case SET_VOLUME: {
Eric Laurent0ede8922014-05-09 18:04:42 -0700794 VolumeData *data = (VolumeData *)command->mParam.get();
795 VolumeData *data2 = (VolumeData *)command2->mParam.get();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700796 if (data->mIO != data2->mIO) break;
797 if (data->mStream != data2->mStream) break;
Steve Block3856b092011-10-20 11:56:00 +0100798 ALOGV("Filtering out volume command on output %d for stream %d",
Eric Laurentde070132010-07-13 04:45:46 -0700799 data->mIO, data->mStream);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700800 removedCommands.add(command2);
Eric Laurent21e54562013-09-23 12:08:05 -0700801 command->mTime = command2->mTime;
802 // force delayMs to non 0 so that code below does not request to wait for
803 // command status as the command is now delayed
804 delayMs = 1;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700805 } break;
806 case START_TONE:
807 case STOP_TONE:
808 default:
809 break;
810 }
811 }
812
813 // remove filtered commands
814 for (size_t j = 0; j < removedCommands.size(); j++) {
815 // removed commands always have time stamps greater than current command
816 for (size_t k = i + 1; k < mAudioCommands.size(); k++) {
Eric Laurent0ede8922014-05-09 18:04:42 -0700817 if (mAudioCommands[k].get() == removedCommands[j].get()) {
Steve Block3856b092011-10-20 11:56:00 +0100818 ALOGV("suppressing command: %d", mAudioCommands[k]->mCommand);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700819 mAudioCommands.removeAt(k);
820 break;
821 }
822 }
823 }
824 removedCommands.clear();
825
Eric Laurent0ede8922014-05-09 18:04:42 -0700826 // Disable wait for status if delay is not 0
827 if (delayMs != 0) {
Eric Laurentcec4abb2012-07-03 12:23:02 -0700828 command->mWaitStatus = false;
829 }
Eric Laurentcec4abb2012-07-03 12:23:02 -0700830
Mathias Agopian65ab4712010-07-14 17:59:35 -0700831 // insert command at the right place according to its time stamp
Steve Block3856b092011-10-20 11:56:00 +0100832 ALOGV("inserting command: %d at index %d, num commands %d",
Eric Laurentde070132010-07-13 04:45:46 -0700833 command->mCommand, (int)i+1, mAudioCommands.size());
Mathias Agopian65ab4712010-07-14 17:59:35 -0700834 mAudioCommands.insertAt(command, i + 1);
835}
836
837void AudioPolicyService::AudioCommandThread::exit()
838{
Steve Block3856b092011-10-20 11:56:00 +0100839 ALOGV("AudioCommandThread::exit");
Mathias Agopian65ab4712010-07-14 17:59:35 -0700840 {
841 AutoMutex _l(mLock);
842 requestExit();
843 mWaitWorkCV.signal();
844 }
845 requestExitAndWait();
846}
847
848void AudioPolicyService::AudioCommandThread::AudioCommand::dump(char* buffer, size_t size)
849{
850 snprintf(buffer, size, " %02d %06d.%03d %01u %p\n",
851 mCommand,
852 (int)ns2s(mTime),
853 (int)ns2ms(mTime)%1000,
854 mWaitStatus,
Eric Laurent0ede8922014-05-09 18:04:42 -0700855 mParam.get());
Mathias Agopian65ab4712010-07-14 17:59:35 -0700856}
857
Dima Zavinfce7a472011-04-19 22:30:36 -0700858/******* helpers for the service_ops callbacks defined below *********/
859void AudioPolicyService::setParameters(audio_io_handle_t ioHandle,
860 const char *keyValuePairs,
861 int delayMs)
862{
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800863 mAudioCommandThread->parametersCommand(ioHandle, keyValuePairs,
Dima Zavinfce7a472011-04-19 22:30:36 -0700864 delayMs);
865}
866
867int AudioPolicyService::setStreamVolume(audio_stream_type_t stream,
868 float volume,
869 audio_io_handle_t output,
870 int delayMs)
871{
Glenn Kastenfff6d712012-01-12 16:38:12 -0800872 return (int)mAudioCommandThread->volumeCommand(stream, volume,
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800873 output, delayMs);
Dima Zavinfce7a472011-04-19 22:30:36 -0700874}
875
876int AudioPolicyService::startTone(audio_policy_tone_t tone,
877 audio_stream_type_t stream)
878{
Glenn Kasten6e2ebe92013-08-13 09:14:51 -0700879 if (tone != AUDIO_POLICY_TONE_IN_CALL_NOTIFICATION) {
Steve Block29357bc2012-01-06 19:20:56 +0000880 ALOGE("startTone: illegal tone requested (%d)", tone);
Glenn Kasten6e2ebe92013-08-13 09:14:51 -0700881 }
882 if (stream != AUDIO_STREAM_VOICE_CALL) {
Steve Block29357bc2012-01-06 19:20:56 +0000883 ALOGE("startTone: illegal stream (%d) requested for tone %d", stream,
Glenn Kastene53b9ea2012-03-12 16:29:55 -0700884 tone);
Glenn Kasten6e2ebe92013-08-13 09:14:51 -0700885 }
Dima Zavinfce7a472011-04-19 22:30:36 -0700886 mTonePlaybackThread->startToneCommand(ToneGenerator::TONE_SUP_CALL_WAITING,
887 AUDIO_STREAM_VOICE_CALL);
888 return 0;
889}
890
891int AudioPolicyService::stopTone()
892{
893 mTonePlaybackThread->stopToneCommand();
894 return 0;
895}
896
897int AudioPolicyService::setVoiceVolume(float volume, int delayMs)
898{
899 return (int)mAudioCommandThread->voiceVolumeCommand(volume, delayMs);
900}
901
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700902// ----------------------------------------------------------------------------
903// Audio pre-processing configuration
904// ----------------------------------------------------------------------------
905
Glenn Kasten8dad0e32012-01-09 08:41:22 -0800906/*static*/ const char * const AudioPolicyService::kInputSourceNames[AUDIO_SOURCE_CNT -1] = {
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700907 MIC_SRC_TAG,
908 VOICE_UL_SRC_TAG,
909 VOICE_DL_SRC_TAG,
910 VOICE_CALL_SRC_TAG,
911 CAMCORDER_SRC_TAG,
912 VOICE_REC_SRC_TAG,
913 VOICE_COMM_SRC_TAG
914};
915
916// returns the audio_source_t enum corresponding to the input source name or
917// AUDIO_SOURCE_CNT is no match found
918audio_source_t AudioPolicyService::inputSourceNameToEnum(const char *name)
919{
920 int i;
921 for (i = AUDIO_SOURCE_MIC; i < AUDIO_SOURCE_CNT; i++) {
922 if (strcmp(name, kInputSourceNames[i - AUDIO_SOURCE_MIC]) == 0) {
Steve Block3856b092011-10-20 11:56:00 +0100923 ALOGV("inputSourceNameToEnum found source %s %d", name, i);
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700924 break;
925 }
926 }
927 return (audio_source_t)i;
928}
929
930size_t AudioPolicyService::growParamSize(char *param,
931 size_t size,
932 size_t *curSize,
933 size_t *totSize)
934{
935 // *curSize is at least sizeof(effect_param_t) + 2 * sizeof(int)
936 size_t pos = ((*curSize - 1 ) / size + 1) * size;
937
938 if (pos + size > *totSize) {
939 while (pos + size > *totSize) {
940 *totSize += ((*totSize + 7) / 8) * 4;
941 }
942 param = (char *)realloc(param, *totSize);
943 }
944 *curSize = pos + size;
945 return pos;
946}
947
948size_t AudioPolicyService::readParamValue(cnode *node,
949 char *param,
950 size_t *curSize,
951 size_t *totSize)
952{
953 if (strncmp(node->name, SHORT_TAG, sizeof(SHORT_TAG) + 1) == 0) {
954 size_t pos = growParamSize(param, sizeof(short), curSize, totSize);
955 *(short *)((char *)param + pos) = (short)atoi(node->value);
Steve Block3856b092011-10-20 11:56:00 +0100956 ALOGV("readParamValue() reading short %d", *(short *)((char *)param + pos));
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700957 return sizeof(short);
958 } else if (strncmp(node->name, INT_TAG, sizeof(INT_TAG) + 1) == 0) {
959 size_t pos = growParamSize(param, sizeof(int), curSize, totSize);
960 *(int *)((char *)param + pos) = atoi(node->value);
Steve Block3856b092011-10-20 11:56:00 +0100961 ALOGV("readParamValue() reading int %d", *(int *)((char *)param + pos));
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700962 return sizeof(int);
963 } else if (strncmp(node->name, FLOAT_TAG, sizeof(FLOAT_TAG) + 1) == 0) {
964 size_t pos = growParamSize(param, sizeof(float), curSize, totSize);
965 *(float *)((char *)param + pos) = (float)atof(node->value);
Steve Block3856b092011-10-20 11:56:00 +0100966 ALOGV("readParamValue() reading float %f",*(float *)((char *)param + pos));
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700967 return sizeof(float);
968 } else if (strncmp(node->name, BOOL_TAG, sizeof(BOOL_TAG) + 1) == 0) {
969 size_t pos = growParamSize(param, sizeof(bool), curSize, totSize);
970 if (strncmp(node->value, "false", strlen("false") + 1) == 0) {
971 *(bool *)((char *)param + pos) = false;
972 } else {
973 *(bool *)((char *)param + pos) = true;
974 }
Steve Block3856b092011-10-20 11:56:00 +0100975 ALOGV("readParamValue() reading bool %s",*(bool *)((char *)param + pos) ? "true" : "false");
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700976 return sizeof(bool);
977 } else if (strncmp(node->name, STRING_TAG, sizeof(STRING_TAG) + 1) == 0) {
978 size_t len = strnlen(node->value, EFFECT_STRING_LEN_MAX);
979 if (*curSize + len + 1 > *totSize) {
980 *totSize = *curSize + len + 1;
981 param = (char *)realloc(param, *totSize);
982 }
983 strncpy(param + *curSize, node->value, len);
984 *curSize += len;
985 param[*curSize] = '\0';
Steve Block3856b092011-10-20 11:56:00 +0100986 ALOGV("readParamValue() reading string %s", param + *curSize - len);
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700987 return len;
988 }
Steve Block5ff1dd52012-01-05 23:22:43 +0000989 ALOGW("readParamValue() unknown param type %s", node->name);
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700990 return 0;
991}
992
993effect_param_t *AudioPolicyService::loadEffectParameter(cnode *root)
994{
995 cnode *param;
996 cnode *value;
997 size_t curSize = sizeof(effect_param_t);
998 size_t totSize = sizeof(effect_param_t) + 2 * sizeof(int);
999 effect_param_t *fx_param = (effect_param_t *)malloc(totSize);
1000
1001 param = config_find(root, PARAM_TAG);
1002 value = config_find(root, VALUE_TAG);
1003 if (param == NULL && value == NULL) {
1004 // try to parse simple parameter form {int int}
1005 param = root->first_child;
Glenn Kastena0d68332012-01-27 16:47:15 -08001006 if (param != NULL) {
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001007 // Note: that a pair of random strings is read as 0 0
1008 int *ptr = (int *)fx_param->data;
1009 int *ptr2 = (int *)((char *)param + sizeof(effect_param_t));
Steve Block5ff1dd52012-01-05 23:22:43 +00001010 ALOGW("loadEffectParameter() ptr %p ptr2 %p", ptr, ptr2);
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001011 *ptr++ = atoi(param->name);
1012 *ptr = atoi(param->value);
1013 fx_param->psize = sizeof(int);
1014 fx_param->vsize = sizeof(int);
1015 return fx_param;
1016 }
1017 }
1018 if (param == NULL || value == NULL) {
Steve Block5ff1dd52012-01-05 23:22:43 +00001019 ALOGW("loadEffectParameter() invalid parameter description %s", root->name);
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001020 goto error;
1021 }
1022
1023 fx_param->psize = 0;
1024 param = param->first_child;
1025 while (param) {
Steve Block3856b092011-10-20 11:56:00 +01001026 ALOGV("loadEffectParameter() reading param of type %s", param->name);
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001027 size_t size = readParamValue(param, (char *)fx_param, &curSize, &totSize);
1028 if (size == 0) {
1029 goto error;
1030 }
1031 fx_param->psize += size;
1032 param = param->next;
1033 }
1034
1035 // align start of value field on 32 bit boundary
1036 curSize = ((curSize - 1 ) / sizeof(int) + 1) * sizeof(int);
1037
1038 fx_param->vsize = 0;
1039 value = value->first_child;
1040 while (value) {
Steve Block3856b092011-10-20 11:56:00 +01001041 ALOGV("loadEffectParameter() reading value of type %s", value->name);
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001042 size_t size = readParamValue(value, (char *)fx_param, &curSize, &totSize);
1043 if (size == 0) {
1044 goto error;
1045 }
1046 fx_param->vsize += size;
1047 value = value->next;
1048 }
1049
1050 return fx_param;
1051
1052error:
You Kimdb358af2013-09-12 20:48:08 +09001053 free(fx_param);
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001054 return NULL;
1055}
1056
1057void AudioPolicyService::loadEffectParameters(cnode *root, Vector <effect_param_t *>& params)
1058{
1059 cnode *node = root->first_child;
1060 while (node) {
Steve Block3856b092011-10-20 11:56:00 +01001061 ALOGV("loadEffectParameters() loading param %s", node->name);
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001062 effect_param_t *param = loadEffectParameter(node);
1063 if (param == NULL) {
1064 node = node->next;
1065 continue;
1066 }
1067 params.add(param);
1068 node = node->next;
1069 }
1070}
1071
1072AudioPolicyService::InputSourceDesc *AudioPolicyService::loadInputSource(
1073 cnode *root,
1074 const Vector <EffectDesc *>& effects)
1075{
1076 cnode *node = root->first_child;
1077 if (node == NULL) {
Steve Block5ff1dd52012-01-05 23:22:43 +00001078 ALOGW("loadInputSource() empty element %s", root->name);
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001079 return NULL;
1080 }
1081 InputSourceDesc *source = new InputSourceDesc();
1082 while (node) {
1083 size_t i;
1084 for (i = 0; i < effects.size(); i++) {
1085 if (strncmp(effects[i]->mName, node->name, EFFECT_STRING_LEN_MAX) == 0) {
Steve Block3856b092011-10-20 11:56:00 +01001086 ALOGV("loadInputSource() found effect %s in list", node->name);
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001087 break;
1088 }
1089 }
1090 if (i == effects.size()) {
Steve Block3856b092011-10-20 11:56:00 +01001091 ALOGV("loadInputSource() effect %s not in list", node->name);
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001092 node = node->next;
1093 continue;
1094 }
Glenn Kasten9fda4b82012-02-02 14:04:37 -08001095 EffectDesc *effect = new EffectDesc(*effects[i]); // deep copy
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001096 loadEffectParameters(node, effect->mParams);
Steve Block3856b092011-10-20 11:56:00 +01001097 ALOGV("loadInputSource() adding effect %s uuid %08x", effect->mName, effect->mUuid.timeLow);
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001098 source->mEffects.add(effect);
1099 node = node->next;
1100 }
1101 if (source->mEffects.size() == 0) {
Steve Block5ff1dd52012-01-05 23:22:43 +00001102 ALOGW("loadInputSource() no valid effects found in source %s", root->name);
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001103 delete source;
1104 return NULL;
1105 }
1106 return source;
1107}
1108
1109status_t AudioPolicyService::loadInputSources(cnode *root, const Vector <EffectDesc *>& effects)
1110{
1111 cnode *node = config_find(root, PREPROCESSING_TAG);
1112 if (node == NULL) {
1113 return -ENOENT;
1114 }
1115 node = node->first_child;
1116 while (node) {
1117 audio_source_t source = inputSourceNameToEnum(node->name);
1118 if (source == AUDIO_SOURCE_CNT) {
Steve Block5ff1dd52012-01-05 23:22:43 +00001119 ALOGW("loadInputSources() invalid input source %s", node->name);
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001120 node = node->next;
1121 continue;
1122 }
Steve Block3856b092011-10-20 11:56:00 +01001123 ALOGV("loadInputSources() loading input source %s", node->name);
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001124 InputSourceDesc *desc = loadInputSource(node, effects);
1125 if (desc == NULL) {
1126 node = node->next;
1127 continue;
1128 }
1129 mInputSources.add(source, desc);
1130 node = node->next;
1131 }
1132 return NO_ERROR;
1133}
1134
1135AudioPolicyService::EffectDesc *AudioPolicyService::loadEffect(cnode *root)
1136{
1137 cnode *node = config_find(root, UUID_TAG);
1138 if (node == NULL) {
1139 return NULL;
1140 }
1141 effect_uuid_t uuid;
1142 if (AudioEffect::stringToGuid(node->value, &uuid) != NO_ERROR) {
Steve Block5ff1dd52012-01-05 23:22:43 +00001143 ALOGW("loadEffect() invalid uuid %s", node->value);
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001144 return NULL;
1145 }
Glenn Kasten9fda4b82012-02-02 14:04:37 -08001146 return new EffectDesc(root->name, uuid);
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001147}
1148
1149status_t AudioPolicyService::loadEffects(cnode *root, Vector <EffectDesc *>& effects)
1150{
1151 cnode *node = config_find(root, EFFECTS_TAG);
1152 if (node == NULL) {
1153 return -ENOENT;
1154 }
1155 node = node->first_child;
1156 while (node) {
Steve Block3856b092011-10-20 11:56:00 +01001157 ALOGV("loadEffects() loading effect %s", node->name);
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001158 EffectDesc *effect = loadEffect(node);
1159 if (effect == NULL) {
1160 node = node->next;
1161 continue;
1162 }
1163 effects.add(effect);
1164 node = node->next;
1165 }
1166 return NO_ERROR;
1167}
1168
1169status_t AudioPolicyService::loadPreProcessorConfig(const char *path)
1170{
1171 cnode *root;
1172 char *data;
1173
1174 data = (char *)load_file(path, NULL);
1175 if (data == NULL) {
1176 return -ENODEV;
1177 }
1178 root = config_node("", "");
1179 config_load(root, data);
1180
1181 Vector <EffectDesc *> effects;
1182 loadEffects(root, effects);
1183 loadInputSources(root, effects);
1184
Yu Yezhonge6056ba2013-10-15 14:32:34 +08001185 // delete effects to fix memory leak.
1186 // as effects is local var and valgrind would treat this as memory leak
1187 // and although it only did in mediaserver init, but free it in case mediaserver reboot
1188 size_t i;
1189 for (i = 0; i < effects.size(); i++) {
1190 delete effects[i];
1191 }
1192
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001193 config_free(root);
1194 free(root);
1195 free(data);
1196
1197 return NO_ERROR;
1198}
1199
Dima Zavinfce7a472011-04-19 22:30:36 -07001200extern "C" {
Eric Laurent2d388ec2014-03-07 13:25:54 -08001201audio_module_handle_t aps_load_hw_module(void *service __unused,
1202 const char *name);
1203audio_io_handle_t aps_open_output(void *service __unused,
Eric Laurenta4c5a552012-03-29 10:12:40 -07001204 audio_devices_t *pDevices,
1205 uint32_t *pSamplingRate,
1206 audio_format_t *pFormat,
1207 audio_channel_mask_t *pChannelMask,
1208 uint32_t *pLatencyMs,
Eric Laurent2d388ec2014-03-07 13:25:54 -08001209 audio_output_flags_t flags);
Eric Laurenta4c5a552012-03-29 10:12:40 -07001210
Eric Laurent2d388ec2014-03-07 13:25:54 -08001211audio_io_handle_t aps_open_output_on_module(void *service __unused,
Eric Laurenta4c5a552012-03-29 10:12:40 -07001212 audio_module_handle_t module,
1213 audio_devices_t *pDevices,
1214 uint32_t *pSamplingRate,
1215 audio_format_t *pFormat,
1216 audio_channel_mask_t *pChannelMask,
1217 uint32_t *pLatencyMs,
Richard Fitzgeraldad3af332013-03-25 16:54:37 +00001218 audio_output_flags_t flags,
Eric Laurent2d388ec2014-03-07 13:25:54 -08001219 const audio_offload_info_t *offloadInfo);
1220audio_io_handle_t aps_open_dup_output(void *service __unused,
Dima Zavinfce7a472011-04-19 22:30:36 -07001221 audio_io_handle_t output1,
Eric Laurent2d388ec2014-03-07 13:25:54 -08001222 audio_io_handle_t output2);
1223int aps_close_output(void *service __unused, audio_io_handle_t output);
1224int aps_suspend_output(void *service __unused, audio_io_handle_t output);
1225int aps_restore_output(void *service __unused, audio_io_handle_t output);
1226audio_io_handle_t aps_open_input(void *service __unused,
Eric Laurenta4c5a552012-03-29 10:12:40 -07001227 audio_devices_t *pDevices,
1228 uint32_t *pSamplingRate,
1229 audio_format_t *pFormat,
1230 audio_channel_mask_t *pChannelMask,
Eric Laurent2d388ec2014-03-07 13:25:54 -08001231 audio_in_acoustics_t acoustics __unused);
1232audio_io_handle_t aps_open_input_on_module(void *service __unused,
Eric Laurenta4c5a552012-03-29 10:12:40 -07001233 audio_module_handle_t module,
1234 audio_devices_t *pDevices,
1235 uint32_t *pSamplingRate,
1236 audio_format_t *pFormat,
Eric Laurent2d388ec2014-03-07 13:25:54 -08001237 audio_channel_mask_t *pChannelMask);
1238int aps_close_input(void *service __unused, audio_io_handle_t input);
1239int aps_invalidate_stream(void *service __unused, audio_stream_type_t stream);
1240int aps_move_effects(void *service __unused, int session,
Dima Zavinfce7a472011-04-19 22:30:36 -07001241 audio_io_handle_t src_output,
Eric Laurent2d388ec2014-03-07 13:25:54 -08001242 audio_io_handle_t dst_output);
1243char * aps_get_parameters(void *service __unused, audio_io_handle_t io_handle,
1244 const char *keys);
1245void aps_set_parameters(void *service, audio_io_handle_t io_handle,
1246 const char *kv_pairs, int delay_ms);
1247int aps_set_stream_volume(void *service, audio_stream_type_t stream,
Dima Zavinfce7a472011-04-19 22:30:36 -07001248 float volume, audio_io_handle_t output,
Eric Laurent2d388ec2014-03-07 13:25:54 -08001249 int delay_ms);
1250int aps_start_tone(void *service, audio_policy_tone_t tone,
1251 audio_stream_type_t stream);
1252int aps_stop_tone(void *service);
1253int aps_set_voice_volume(void *service, float volume, int delay_ms);
1254};
Dima Zavinfce7a472011-04-19 22:30:36 -07001255
1256namespace {
1257 struct audio_policy_service_ops aps_ops = {
Glenn Kasten01d3acb2014-02-06 08:24:07 -08001258 .open_output = aps_open_output,
1259 .open_duplicate_output = aps_open_dup_output,
1260 .close_output = aps_close_output,
1261 .suspend_output = aps_suspend_output,
1262 .restore_output = aps_restore_output,
1263 .open_input = aps_open_input,
1264 .close_input = aps_close_input,
1265 .set_stream_volume = aps_set_stream_volume,
Glenn Kastend2304db2014-02-03 07:40:31 -08001266 .invalidate_stream = aps_invalidate_stream,
Glenn Kasten01d3acb2014-02-06 08:24:07 -08001267 .set_parameters = aps_set_parameters,
1268 .get_parameters = aps_get_parameters,
1269 .start_tone = aps_start_tone,
1270 .stop_tone = aps_stop_tone,
1271 .set_voice_volume = aps_set_voice_volume,
1272 .move_effects = aps_move_effects,
1273 .load_hw_module = aps_load_hw_module,
1274 .open_output_on_module = aps_open_output_on_module,
1275 .open_input_on_module = aps_open_input_on_module,
Dima Zavinfce7a472011-04-19 22:30:36 -07001276 };
1277}; // namespace <unnamed>
1278
Mathias Agopian65ab4712010-07-14 17:59:35 -07001279}; // namespace android