blob: 6a4a669f6425730f0fd0b3cb7f258321613b4c73 [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>
Dima Zavinfce7a472011-04-19 22:30:36 -070043
Mathias Agopian65ab4712010-07-14 17:59:35 -070044namespace android {
45
Glenn Kasten8dad0e32012-01-09 08:41:22 -080046static const char kDeadlockedString[] = "AudioPolicyService may be deadlocked\n";
47static const char kCmdDeadlockedString[] = "AudioPolicyService command thread may be deadlocked\n";
Mathias Agopian65ab4712010-07-14 17:59:35 -070048
49static const int kDumpLockRetries = 50;
Glenn Kasten22ecc912012-01-09 08:33:38 -080050static const int kDumpLockSleepUs = 20000;
Mathias Agopian65ab4712010-07-14 17:59:35 -070051
Eric Laurent0ede8922014-05-09 18:04:42 -070052static const nsecs_t kAudioCommandTimeoutNs = seconds(3); // 3 seconds
Christer Fletcher5fa8c4b2013-01-18 15:27:03 +010053
Dima Zavinfce7a472011-04-19 22:30:36 -070054namespace {
55 extern struct audio_policy_service_ops aps_ops;
56};
57
Mathias Agopian65ab4712010-07-14 17:59:35 -070058// ----------------------------------------------------------------------------
59
60AudioPolicyService::AudioPolicyService()
Eric Laurentdce54a12014-03-10 12:19:46 -070061 : BnAudioPolicyService(), mpAudioPolicyDev(NULL), mpAudioPolicy(NULL),
Eric Laurentbb6c9a02014-09-25 14:11:47 -070062 mAudioPolicyManager(NULL), mAudioPolicyClient(NULL), mPhoneState(AUDIO_MODE_INVALID)
Mathias Agopian65ab4712010-07-14 17:59:35 -070063{
Eric Laurentf5ada6e2014-10-09 17:49:00 -070064}
65
66void AudioPolicyService::onFirstRef()
67{
Mathias Agopian65ab4712010-07-14 17:59:35 -070068 char value[PROPERTY_VALUE_MAX];
Dima Zavinfce7a472011-04-19 22:30:36 -070069 const struct hw_module_t *module;
70 int forced_val;
71 int rc;
Mathias Agopian65ab4712010-07-14 17:59:35 -070072
Eric Laurent8b1e80b2014-10-07 09:08:47 -070073 {
74 Mutex::Autolock _l(mLock);
Eric Laurent93575202011-01-18 18:39:02 -080075
Eric Laurent8b1e80b2014-10-07 09:08:47 -070076 // start tone playback thread
77 mTonePlaybackThread = new AudioCommandThread(String8("ApmTone"), this);
78 // start audio commands thread
79 mAudioCommandThread = new AudioCommandThread(String8("ApmAudio"), this);
80 // start output activity command thread
81 mOutputCommandThread = new AudioCommandThread(String8("ApmOutput"), this);
Eric Laurentdce54a12014-03-10 12:19:46 -070082
83#ifdef USE_LEGACY_AUDIO_POLICY
Eric Laurent8b1e80b2014-10-07 09:08:47 -070084 ALOGI("AudioPolicyService CSTOR in legacy mode");
Eric Laurentdce54a12014-03-10 12:19:46 -070085
Eric Laurent8b1e80b2014-10-07 09:08:47 -070086 /* instantiate the audio policy manager */
87 rc = hw_get_module(AUDIO_POLICY_HARDWARE_MODULE_ID, &module);
88 if (rc) {
89 return;
90 }
91 rc = audio_policy_dev_open(module, &mpAudioPolicyDev);
92 ALOGE_IF(rc, "couldn't open audio policy device (%s)", strerror(-rc));
93 if (rc) {
94 return;
95 }
Eric Laurent93575202011-01-18 18:39:02 -080096
Eric Laurent8b1e80b2014-10-07 09:08:47 -070097 rc = mpAudioPolicyDev->create_audio_policy(mpAudioPolicyDev, &aps_ops, this,
98 &mpAudioPolicy);
99 ALOGE_IF(rc, "couldn't create audio policy (%s)", strerror(-rc));
100 if (rc) {
101 return;
102 }
Dima Zavinfce7a472011-04-19 22:30:36 -0700103
Eric Laurent8b1e80b2014-10-07 09:08:47 -0700104 rc = mpAudioPolicy->init_check(mpAudioPolicy);
105 ALOGE_IF(rc, "couldn't init_check the audio policy (%s)", strerror(-rc));
106 if (rc) {
107 return;
108 }
109 ALOGI("Loaded audio policy from %s (%s)", module->name, module->id);
Eric Laurentdce54a12014-03-10 12:19:46 -0700110#else
Eric Laurent8b1e80b2014-10-07 09:08:47 -0700111 ALOGI("AudioPolicyService CSTOR in new mode");
Eric Laurentdce54a12014-03-10 12:19:46 -0700112
Eric Laurent8b1e80b2014-10-07 09:08:47 -0700113 mAudioPolicyClient = new AudioPolicyClient(this);
114 mAudioPolicyManager = createAudioPolicyManager(mAudioPolicyClient);
Eric Laurentdce54a12014-03-10 12:19:46 -0700115#endif
Eric Laurent8b1e80b2014-10-07 09:08:47 -0700116 }
bryant_liuba2b4392014-06-11 16:49:30 +0800117 // load audio processing modules
Eric Laurent8b1e80b2014-10-07 09:08:47 -0700118 sp<AudioPolicyEffects>audioPolicyEffects = new AudioPolicyEffects();
119 {
120 Mutex::Autolock _l(mLock);
121 mAudioPolicyEffects = audioPolicyEffects;
122 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700123}
124
125AudioPolicyService::~AudioPolicyService()
126{
127 mTonePlaybackThread->exit();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700128 mAudioCommandThread->exit();
Eric Laurent657ff612014-05-07 11:58:24 -0700129 mOutputCommandThread->exit();
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700130
Eric Laurentdce54a12014-03-10 12:19:46 -0700131#ifdef USE_LEGACY_AUDIO_POLICY
Glenn Kasten6e2ebe92013-08-13 09:14:51 -0700132 if (mpAudioPolicy != NULL && mpAudioPolicyDev != NULL) {
Dima Zavinfce7a472011-04-19 22:30:36 -0700133 mpAudioPolicyDev->destroy_audio_policy(mpAudioPolicyDev, mpAudioPolicy);
Glenn Kasten6e2ebe92013-08-13 09:14:51 -0700134 }
135 if (mpAudioPolicyDev != NULL) {
Dima Zavinfce7a472011-04-19 22:30:36 -0700136 audio_policy_dev_close(mpAudioPolicyDev);
Glenn Kasten6e2ebe92013-08-13 09:14:51 -0700137 }
Eric Laurentdce54a12014-03-10 12:19:46 -0700138#else
Eric Laurentf269b8e2014-06-09 20:01:29 -0700139 destroyAudioPolicyManager(mAudioPolicyManager);
Eric Laurentdce54a12014-03-10 12:19:46 -0700140 delete mAudioPolicyClient;
141#endif
Eric Laurentb52c1522014-05-20 11:27:36 -0700142
143 mNotificationClients.clear();
bryant_liuba2b4392014-06-11 16:49:30 +0800144 mAudioPolicyEffects.clear();
Eric Laurentb52c1522014-05-20 11:27:36 -0700145}
146
147// A notification client is always registered by AudioSystem when the client process
148// connects to AudioPolicyService.
149void AudioPolicyService::registerClient(const sp<IAudioPolicyServiceClient>& client)
150{
151
Eric Laurent0ebd5f92014-11-19 19:04:52 -0800152 Mutex::Autolock _l(mNotificationClientsLock);
Eric Laurentb52c1522014-05-20 11:27:36 -0700153
154 uid_t uid = IPCThreadState::self()->getCallingUid();
155 if (mNotificationClients.indexOfKey(uid) < 0) {
156 sp<NotificationClient> notificationClient = new NotificationClient(this,
157 client,
158 uid);
159 ALOGV("registerClient() client %p, uid %d", client.get(), uid);
160
161 mNotificationClients.add(uid, notificationClient);
162
163 sp<IBinder> binder = client->asBinder();
164 binder->linkToDeath(notificationClient);
165 }
166}
167
168// removeNotificationClient() is called when the client process dies.
169void AudioPolicyService::removeNotificationClient(uid_t uid)
170{
Eric Laurent0ebd5f92014-11-19 19:04:52 -0800171 {
172 Mutex::Autolock _l(mNotificationClientsLock);
173 mNotificationClients.removeItem(uid);
174 }
Eric Laurentb52c1522014-05-20 11:27:36 -0700175#ifndef USE_LEGACY_AUDIO_POLICY
Eric Laurent0ebd5f92014-11-19 19:04:52 -0800176 {
177 Mutex::Autolock _l(mLock);
Eric Laurentb52c1522014-05-20 11:27:36 -0700178 if (mAudioPolicyManager) {
179 mAudioPolicyManager->clearAudioPatches(uid);
180 }
Eric Laurent0ebd5f92014-11-19 19:04:52 -0800181 }
Eric Laurentb52c1522014-05-20 11:27:36 -0700182#endif
183}
184
185void AudioPolicyService::onAudioPortListUpdate()
186{
187 mOutputCommandThread->updateAudioPortListCommand();
188}
189
190void AudioPolicyService::doOnAudioPortListUpdate()
191{
Eric Laurent0ebd5f92014-11-19 19:04:52 -0800192 Mutex::Autolock _l(mNotificationClientsLock);
Eric Laurentb52c1522014-05-20 11:27:36 -0700193 for (size_t i = 0; i < mNotificationClients.size(); i++) {
194 mNotificationClients.valueAt(i)->onAudioPortListUpdate();
195 }
196}
197
198void AudioPolicyService::onAudioPatchListUpdate()
199{
200 mOutputCommandThread->updateAudioPatchListCommand();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700201}
202
Eric Laurent951f4552014-05-20 10:48:17 -0700203status_t AudioPolicyService::clientCreateAudioPatch(const struct audio_patch *patch,
204 audio_patch_handle_t *handle,
205 int delayMs)
206{
207 return mAudioCommandThread->createAudioPatchCommand(patch, handle, delayMs);
208}
209
210status_t AudioPolicyService::clientReleaseAudioPatch(audio_patch_handle_t handle,
211 int delayMs)
212{
213 return mAudioCommandThread->releaseAudioPatchCommand(handle, delayMs);
214}
215
Eric Laurentb52c1522014-05-20 11:27:36 -0700216void AudioPolicyService::doOnAudioPatchListUpdate()
217{
Eric Laurent0ebd5f92014-11-19 19:04:52 -0800218 Mutex::Autolock _l(mNotificationClientsLock);
Eric Laurentb52c1522014-05-20 11:27:36 -0700219 for (size_t i = 0; i < mNotificationClients.size(); i++) {
220 mNotificationClients.valueAt(i)->onAudioPatchListUpdate();
221 }
222}
223
Eric Laurente1715a42014-05-20 11:30:42 -0700224status_t AudioPolicyService::clientSetAudioPortConfig(const struct audio_port_config *config,
225 int delayMs)
226{
227 return mAudioCommandThread->setAudioPortConfigCommand(config, delayMs);
228}
229
Eric Laurentb52c1522014-05-20 11:27:36 -0700230AudioPolicyService::NotificationClient::NotificationClient(const sp<AudioPolicyService>& service,
231 const sp<IAudioPolicyServiceClient>& client,
232 uid_t uid)
233 : mService(service), mUid(uid), mAudioPolicyServiceClient(client)
234{
235}
236
237AudioPolicyService::NotificationClient::~NotificationClient()
238{
239}
240
241void AudioPolicyService::NotificationClient::binderDied(const wp<IBinder>& who __unused)
242{
243 sp<NotificationClient> keep(this);
244 sp<AudioPolicyService> service = mService.promote();
245 if (service != 0) {
246 service->removeNotificationClient(mUid);
247 }
248}
249
250void AudioPolicyService::NotificationClient::onAudioPortListUpdate()
251{
252 if (mAudioPolicyServiceClient != 0) {
253 mAudioPolicyServiceClient->onAudioPortListUpdate();
254 }
255}
256
257void AudioPolicyService::NotificationClient::onAudioPatchListUpdate()
258{
259 if (mAudioPolicyServiceClient != 0) {
260 mAudioPolicyServiceClient->onAudioPatchListUpdate();
261 }
262}
Eric Laurent57dae992011-07-24 13:36:09 -0700263
Mathias Agopian65ab4712010-07-14 17:59:35 -0700264void AudioPolicyService::binderDied(const wp<IBinder>& who) {
Glenn Kasten411e4472012-11-02 10:00:06 -0700265 ALOGW("binderDied() %p, calling pid %d", who.unsafe_get(),
Eric Laurentde070132010-07-13 04:45:46 -0700266 IPCThreadState::self()->getCallingPid());
Mathias Agopian65ab4712010-07-14 17:59:35 -0700267}
268
269static bool tryLock(Mutex& mutex)
270{
271 bool locked = false;
272 for (int i = 0; i < kDumpLockRetries; ++i) {
273 if (mutex.tryLock() == NO_ERROR) {
274 locked = true;
275 break;
276 }
Glenn Kasten22ecc912012-01-09 08:33:38 -0800277 usleep(kDumpLockSleepUs);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700278 }
279 return locked;
280}
281
282status_t AudioPolicyService::dumpInternals(int fd)
283{
284 const size_t SIZE = 256;
285 char buffer[SIZE];
286 String8 result;
287
Eric Laurentdce54a12014-03-10 12:19:46 -0700288#ifdef USE_LEGACY_AUDIO_POLICY
Dima Zavinfce7a472011-04-19 22:30:36 -0700289 snprintf(buffer, SIZE, "PolicyManager Interface: %p\n", mpAudioPolicy);
Eric Laurentdce54a12014-03-10 12:19:46 -0700290#else
291 snprintf(buffer, SIZE, "AudioPolicyManager: %p\n", mAudioPolicyManager);
292#endif
Mathias Agopian65ab4712010-07-14 17:59:35 -0700293 result.append(buffer);
294 snprintf(buffer, SIZE, "Command Thread: %p\n", mAudioCommandThread.get());
295 result.append(buffer);
296 snprintf(buffer, SIZE, "Tones Thread: %p\n", mTonePlaybackThread.get());
297 result.append(buffer);
298
299 write(fd, result.string(), result.size());
300 return NO_ERROR;
301}
302
Glenn Kasten0f11b512014-01-31 16:18:54 -0800303status_t AudioPolicyService::dump(int fd, const Vector<String16>& args __unused)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700304{
Glenn Kasten44deb052012-02-05 18:09:08 -0800305 if (!dumpAllowed()) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700306 dumpPermissionDenial(fd);
307 } else {
308 bool locked = tryLock(mLock);
309 if (!locked) {
310 String8 result(kDeadlockedString);
311 write(fd, result.string(), result.size());
312 }
313
314 dumpInternals(fd);
Glenn Kasten9d1f02d2012-02-08 17:47:58 -0800315 if (mAudioCommandThread != 0) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700316 mAudioCommandThread->dump(fd);
317 }
Glenn Kasten9d1f02d2012-02-08 17:47:58 -0800318 if (mTonePlaybackThread != 0) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700319 mTonePlaybackThread->dump(fd);
320 }
321
Eric Laurentdce54a12014-03-10 12:19:46 -0700322#ifdef USE_LEGACY_AUDIO_POLICY
Dima Zavinfce7a472011-04-19 22:30:36 -0700323 if (mpAudioPolicy) {
324 mpAudioPolicy->dump(mpAudioPolicy, fd);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700325 }
Eric Laurentdce54a12014-03-10 12:19:46 -0700326#else
327 if (mAudioPolicyManager) {
328 mAudioPolicyManager->dump(fd);
329 }
330#endif
Mathias Agopian65ab4712010-07-14 17:59:35 -0700331
332 if (locked) mLock.unlock();
333 }
334 return NO_ERROR;
335}
336
337status_t AudioPolicyService::dumpPermissionDenial(int fd)
338{
339 const size_t SIZE = 256;
340 char buffer[SIZE];
341 String8 result;
342 snprintf(buffer, SIZE, "Permission Denial: "
343 "can't dump AudioPolicyService from pid=%d, uid=%d\n",
344 IPCThreadState::self()->getCallingPid(),
345 IPCThreadState::self()->getCallingUid());
346 result.append(buffer);
347 write(fd, result.string(), result.size());
348 return NO_ERROR;
349}
350
351status_t AudioPolicyService::onTransact(
352 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
353{
354 return BnAudioPolicyService::onTransact(code, data, reply, flags);
355}
356
357
Mathias Agopian65ab4712010-07-14 17:59:35 -0700358// ----------- AudioPolicyService::AudioCommandThread implementation ----------
359
Eric Laurentbfb1b832013-01-07 09:53:42 -0800360AudioPolicyService::AudioCommandThread::AudioCommandThread(String8 name,
361 const wp<AudioPolicyService>& service)
362 : Thread(false), mName(name), mService(service)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700363{
364 mpToneGenerator = NULL;
365}
366
367
368AudioPolicyService::AudioCommandThread::~AudioCommandThread()
369{
Eric Laurentbfb1b832013-01-07 09:53:42 -0800370 if (!mAudioCommands.isEmpty()) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700371 release_wake_lock(mName.string());
372 }
373 mAudioCommands.clear();
Glenn Kastene9dd0172012-01-27 18:08:45 -0800374 delete mpToneGenerator;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700375}
376
377void AudioPolicyService::AudioCommandThread::onFirstRef()
378{
Eric Laurentbfb1b832013-01-07 09:53:42 -0800379 run(mName.string(), ANDROID_PRIORITY_AUDIO);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700380}
381
382bool AudioPolicyService::AudioCommandThread::threadLoop()
383{
384 nsecs_t waitTime = INT64_MAX;
385
386 mLock.lock();
387 while (!exitPending())
388 {
Eric Laurent59a89232014-06-08 14:14:17 -0700389 sp<AudioPolicyService> svc;
390 while (!mAudioCommands.isEmpty() && !exitPending()) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700391 nsecs_t curTime = systemTime();
392 // commands are sorted by increasing time stamp: execute them from index 0 and up
393 if (mAudioCommands[0]->mTime <= curTime) {
Eric Laurent0ede8922014-05-09 18:04:42 -0700394 sp<AudioCommand> command = mAudioCommands[0];
Mathias Agopian65ab4712010-07-14 17:59:35 -0700395 mAudioCommands.removeAt(0);
Eric Laurent0ede8922014-05-09 18:04:42 -0700396 mLastCommand = command;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700397
398 switch (command->mCommand) {
399 case START_TONE: {
400 mLock.unlock();
Eric Laurent0ede8922014-05-09 18:04:42 -0700401 ToneData *data = (ToneData *)command->mParam.get();
Steve Block3856b092011-10-20 11:56:00 +0100402 ALOGV("AudioCommandThread() processing start tone %d on stream %d",
Mathias Agopian65ab4712010-07-14 17:59:35 -0700403 data->mType, data->mStream);
Glenn Kastene9dd0172012-01-27 18:08:45 -0800404 delete mpToneGenerator;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700405 mpToneGenerator = new ToneGenerator(data->mStream, 1.0);
406 mpToneGenerator->startTone(data->mType);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700407 mLock.lock();
408 }break;
409 case STOP_TONE: {
410 mLock.unlock();
Steve Block3856b092011-10-20 11:56:00 +0100411 ALOGV("AudioCommandThread() processing stop tone");
Mathias Agopian65ab4712010-07-14 17:59:35 -0700412 if (mpToneGenerator != NULL) {
413 mpToneGenerator->stopTone();
414 delete mpToneGenerator;
415 mpToneGenerator = NULL;
416 }
417 mLock.lock();
418 }break;
419 case SET_VOLUME: {
Eric Laurent0ede8922014-05-09 18:04:42 -0700420 VolumeData *data = (VolumeData *)command->mParam.get();
Steve Block3856b092011-10-20 11:56:00 +0100421 ALOGV("AudioCommandThread() processing set volume stream %d, \
Eric Laurentde070132010-07-13 04:45:46 -0700422 volume %f, output %d", data->mStream, data->mVolume, data->mIO);
423 command->mStatus = AudioSystem::setStreamVolume(data->mStream,
424 data->mVolume,
425 data->mIO);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700426 }break;
427 case SET_PARAMETERS: {
Eric Laurent0ede8922014-05-09 18:04:42 -0700428 ParametersData *data = (ParametersData *)command->mParam.get();
Glenn Kastene53b9ea2012-03-12 16:29:55 -0700429 ALOGV("AudioCommandThread() processing set parameters string %s, io %d",
430 data->mKeyValuePairs.string(), data->mIO);
431 command->mStatus = AudioSystem::setParameters(data->mIO, data->mKeyValuePairs);
Glenn Kastene53b9ea2012-03-12 16:29:55 -0700432 }break;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700433 case SET_VOICE_VOLUME: {
Eric Laurent0ede8922014-05-09 18:04:42 -0700434 VoiceVolumeData *data = (VoiceVolumeData *)command->mParam.get();
Steve Block3856b092011-10-20 11:56:00 +0100435 ALOGV("AudioCommandThread() processing set voice volume volume %f",
Eric Laurentde070132010-07-13 04:45:46 -0700436 data->mVolume);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700437 command->mStatus = AudioSystem::setVoiceVolume(data->mVolume);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700438 }break;
Eric Laurentbfb1b832013-01-07 09:53:42 -0800439 case STOP_OUTPUT: {
Eric Laurent0ede8922014-05-09 18:04:42 -0700440 StopOutputData *data = (StopOutputData *)command->mParam.get();
Eric Laurentbfb1b832013-01-07 09:53:42 -0800441 ALOGV("AudioCommandThread() processing stop output %d",
442 data->mIO);
Eric Laurent59a89232014-06-08 14:14:17 -0700443 svc = mService.promote();
Eric Laurentbfb1b832013-01-07 09:53:42 -0800444 if (svc == 0) {
445 break;
446 }
447 mLock.unlock();
448 svc->doStopOutput(data->mIO, data->mStream, data->mSession);
449 mLock.lock();
Eric Laurentbfb1b832013-01-07 09:53:42 -0800450 }break;
451 case RELEASE_OUTPUT: {
Eric Laurent0ede8922014-05-09 18:04:42 -0700452 ReleaseOutputData *data = (ReleaseOutputData *)command->mParam.get();
Eric Laurentbfb1b832013-01-07 09:53:42 -0800453 ALOGV("AudioCommandThread() processing release output %d",
454 data->mIO);
Eric Laurent59a89232014-06-08 14:14:17 -0700455 svc = mService.promote();
Eric Laurentbfb1b832013-01-07 09:53:42 -0800456 if (svc == 0) {
457 break;
458 }
459 mLock.unlock();
460 svc->doReleaseOutput(data->mIO);
461 mLock.lock();
Eric Laurentbfb1b832013-01-07 09:53:42 -0800462 }break;
Eric Laurent951f4552014-05-20 10:48:17 -0700463 case CREATE_AUDIO_PATCH: {
464 CreateAudioPatchData *data = (CreateAudioPatchData *)command->mParam.get();
465 ALOGV("AudioCommandThread() processing create audio patch");
466 sp<IAudioFlinger> af = AudioSystem::get_audio_flinger();
467 if (af == 0) {
468 command->mStatus = PERMISSION_DENIED;
469 } else {
470 command->mStatus = af->createAudioPatch(&data->mPatch, &data->mHandle);
471 }
472 } break;
473 case RELEASE_AUDIO_PATCH: {
474 ReleaseAudioPatchData *data = (ReleaseAudioPatchData *)command->mParam.get();
475 ALOGV("AudioCommandThread() processing release audio patch");
476 sp<IAudioFlinger> af = AudioSystem::get_audio_flinger();
477 if (af == 0) {
478 command->mStatus = PERMISSION_DENIED;
479 } else {
480 command->mStatus = af->releaseAudioPatch(data->mHandle);
481 }
482 } break;
Eric Laurentb52c1522014-05-20 11:27:36 -0700483 case UPDATE_AUDIOPORT_LIST: {
484 ALOGV("AudioCommandThread() processing update audio port list");
Eric Laurent59a89232014-06-08 14:14:17 -0700485 svc = mService.promote();
Eric Laurentb52c1522014-05-20 11:27:36 -0700486 if (svc == 0) {
487 break;
488 }
489 mLock.unlock();
490 svc->doOnAudioPortListUpdate();
491 mLock.lock();
492 }break;
493 case UPDATE_AUDIOPATCH_LIST: {
494 ALOGV("AudioCommandThread() processing update audio patch list");
Eric Laurent59a89232014-06-08 14:14:17 -0700495 svc = mService.promote();
Eric Laurentb52c1522014-05-20 11:27:36 -0700496 if (svc == 0) {
497 break;
498 }
499 mLock.unlock();
500 svc->doOnAudioPatchListUpdate();
501 mLock.lock();
502 }break;
Eric Laurente1715a42014-05-20 11:30:42 -0700503 case SET_AUDIOPORT_CONFIG: {
504 SetAudioPortConfigData *data = (SetAudioPortConfigData *)command->mParam.get();
505 ALOGV("AudioCommandThread() processing set port config");
506 sp<IAudioFlinger> af = AudioSystem::get_audio_flinger();
507 if (af == 0) {
508 command->mStatus = PERMISSION_DENIED;
509 } else {
510 command->mStatus = af->setAudioPortConfig(&data->mConfig);
511 }
512 } break;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700513 default:
Steve Block5ff1dd52012-01-05 23:22:43 +0000514 ALOGW("AudioCommandThread() unknown command %d", command->mCommand);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700515 }
Eric Laurent0ede8922014-05-09 18:04:42 -0700516 {
517 Mutex::Autolock _l(command->mLock);
518 if (command->mWaitStatus) {
519 command->mWaitStatus = false;
520 command->mCond.signal();
521 }
522 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700523 waitTime = INT64_MAX;
524 } else {
525 waitTime = mAudioCommands[0]->mTime - curTime;
526 break;
527 }
528 }
Eric Laurent59a89232014-06-08 14:14:17 -0700529 // release mLock before releasing strong reference on the service as
530 // AudioPolicyService destructor calls AudioCommandThread::exit() which acquires mLock.
531 mLock.unlock();
532 svc.clear();
533 mLock.lock();
Ricardo Garcia05f2fdc2014-07-24 15:48:24 -0700534 if (!exitPending() && mAudioCommands.isEmpty()) {
535 // release delayed commands wake lock
536 release_wake_lock(mName.string());
Eric Laurent59a89232014-06-08 14:14:17 -0700537 ALOGV("AudioCommandThread() going to sleep");
538 mWaitWorkCV.waitRelative(mLock, waitTime);
539 ALOGV("AudioCommandThread() waking up");
540 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700541 }
Ricardo Garcia05f2fdc2014-07-24 15:48:24 -0700542 // release delayed commands wake lock before quitting
543 if (!mAudioCommands.isEmpty()) {
544 release_wake_lock(mName.string());
545 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700546 mLock.unlock();
547 return false;
548}
549
550status_t AudioPolicyService::AudioCommandThread::dump(int fd)
551{
552 const size_t SIZE = 256;
553 char buffer[SIZE];
554 String8 result;
555
556 snprintf(buffer, SIZE, "AudioCommandThread %p Dump\n", this);
557 result.append(buffer);
558 write(fd, result.string(), result.size());
559
560 bool locked = tryLock(mLock);
561 if (!locked) {
562 String8 result2(kCmdDeadlockedString);
563 write(fd, result2.string(), result2.size());
564 }
565
566 snprintf(buffer, SIZE, "- Commands:\n");
567 result = String8(buffer);
568 result.append(" Command Time Wait pParam\n");
Glenn Kasten8d6a2442012-02-08 14:04:28 -0800569 for (size_t i = 0; i < mAudioCommands.size(); i++) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700570 mAudioCommands[i]->dump(buffer, SIZE);
571 result.append(buffer);
572 }
573 result.append(" Last Command\n");
Eric Laurent0ede8922014-05-09 18:04:42 -0700574 if (mLastCommand != 0) {
575 mLastCommand->dump(buffer, SIZE);
576 result.append(buffer);
577 } else {
578 result.append(" none\n");
579 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700580
581 write(fd, result.string(), result.size());
582
583 if (locked) mLock.unlock();
584
585 return NO_ERROR;
586}
587
Glenn Kasten3d2f8772012-01-27 15:25:25 -0800588void AudioPolicyService::AudioCommandThread::startToneCommand(ToneGenerator::tone_type type,
589 audio_stream_type_t stream)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700590{
Eric Laurent0ede8922014-05-09 18:04:42 -0700591 sp<AudioCommand> command = new AudioCommand();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700592 command->mCommand = START_TONE;
Eric Laurent0ede8922014-05-09 18:04:42 -0700593 sp<ToneData> data = new ToneData();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700594 data->mType = type;
595 data->mStream = stream;
Jesper Tragardh48412dc2014-03-24 14:12:43 +0100596 command->mParam = data;
Steve Block3856b092011-10-20 11:56:00 +0100597 ALOGV("AudioCommandThread() adding tone start type %d, stream %d", type, stream);
Eric Laurent0ede8922014-05-09 18:04:42 -0700598 sendCommand(command);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700599}
600
601void AudioPolicyService::AudioCommandThread::stopToneCommand()
602{
Eric Laurent0ede8922014-05-09 18:04:42 -0700603 sp<AudioCommand> command = new AudioCommand();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700604 command->mCommand = STOP_TONE;
Steve Block3856b092011-10-20 11:56:00 +0100605 ALOGV("AudioCommandThread() adding tone stop");
Eric Laurent0ede8922014-05-09 18:04:42 -0700606 sendCommand(command);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700607}
608
Glenn Kastenfff6d712012-01-12 16:38:12 -0800609status_t AudioPolicyService::AudioCommandThread::volumeCommand(audio_stream_type_t stream,
Eric Laurentde070132010-07-13 04:45:46 -0700610 float volume,
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800611 audio_io_handle_t output,
Eric Laurentde070132010-07-13 04:45:46 -0700612 int delayMs)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700613{
Eric Laurent0ede8922014-05-09 18:04:42 -0700614 sp<AudioCommand> command = new AudioCommand();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700615 command->mCommand = SET_VOLUME;
Eric Laurent0ede8922014-05-09 18:04:42 -0700616 sp<VolumeData> data = new VolumeData();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700617 data->mStream = stream;
618 data->mVolume = volume;
619 data->mIO = output;
620 command->mParam = data;
Eric Laurent0ede8922014-05-09 18:04:42 -0700621 command->mWaitStatus = true;
Steve Block3856b092011-10-20 11:56:00 +0100622 ALOGV("AudioCommandThread() adding set volume stream %d, volume %f, output %d",
Eric Laurentde070132010-07-13 04:45:46 -0700623 stream, volume, output);
Eric Laurent0ede8922014-05-09 18:04:42 -0700624 return sendCommand(command, delayMs);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700625}
626
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800627status_t AudioPolicyService::AudioCommandThread::parametersCommand(audio_io_handle_t ioHandle,
Dima Zavinfce7a472011-04-19 22:30:36 -0700628 const char *keyValuePairs,
Eric Laurentde070132010-07-13 04:45:46 -0700629 int delayMs)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700630{
Eric Laurent0ede8922014-05-09 18:04:42 -0700631 sp<AudioCommand> command = new AudioCommand();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700632 command->mCommand = SET_PARAMETERS;
Eric Laurent0ede8922014-05-09 18:04:42 -0700633 sp<ParametersData> data = new ParametersData();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700634 data->mIO = ioHandle;
Dima Zavinfce7a472011-04-19 22:30:36 -0700635 data->mKeyValuePairs = String8(keyValuePairs);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700636 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 parameter string %s, io %d ,delay %d",
Dima Zavinfce7a472011-04-19 22:30:36 -0700639 keyValuePairs, ioHandle, delayMs);
Eric Laurent0ede8922014-05-09 18:04:42 -0700640 return sendCommand(command, delayMs);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700641}
642
643status_t AudioPolicyService::AudioCommandThread::voiceVolumeCommand(float volume, int delayMs)
644{
Eric Laurent0ede8922014-05-09 18:04:42 -0700645 sp<AudioCommand> command = new AudioCommand();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700646 command->mCommand = SET_VOICE_VOLUME;
Eric Laurent0ede8922014-05-09 18:04:42 -0700647 sp<VoiceVolumeData> data = new VoiceVolumeData();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700648 data->mVolume = volume;
649 command->mParam = data;
Eric Laurent0ede8922014-05-09 18:04:42 -0700650 command->mWaitStatus = true;
Steve Block3856b092011-10-20 11:56:00 +0100651 ALOGV("AudioCommandThread() adding set voice volume volume %f", volume);
Eric Laurent0ede8922014-05-09 18:04:42 -0700652 return sendCommand(command, delayMs);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700653}
654
Eric Laurentbfb1b832013-01-07 09:53:42 -0800655void AudioPolicyService::AudioCommandThread::stopOutputCommand(audio_io_handle_t output,
656 audio_stream_type_t stream,
657 int session)
658{
Eric Laurent0ede8922014-05-09 18:04:42 -0700659 sp<AudioCommand> command = new AudioCommand();
Eric Laurentbfb1b832013-01-07 09:53:42 -0800660 command->mCommand = STOP_OUTPUT;
Eric Laurent0ede8922014-05-09 18:04:42 -0700661 sp<StopOutputData> data = new StopOutputData();
Eric Laurentbfb1b832013-01-07 09:53:42 -0800662 data->mIO = output;
663 data->mStream = stream;
664 data->mSession = session;
Jesper Tragardh48412dc2014-03-24 14:12:43 +0100665 command->mParam = data;
Eric Laurentbfb1b832013-01-07 09:53:42 -0800666 ALOGV("AudioCommandThread() adding stop output %d", output);
Eric Laurent0ede8922014-05-09 18:04:42 -0700667 sendCommand(command);
Eric Laurentbfb1b832013-01-07 09:53:42 -0800668}
669
670void AudioPolicyService::AudioCommandThread::releaseOutputCommand(audio_io_handle_t output)
671{
Eric Laurent0ede8922014-05-09 18:04:42 -0700672 sp<AudioCommand> command = new AudioCommand();
Eric Laurentbfb1b832013-01-07 09:53:42 -0800673 command->mCommand = RELEASE_OUTPUT;
Eric Laurent0ede8922014-05-09 18:04:42 -0700674 sp<ReleaseOutputData> data = new ReleaseOutputData();
Eric Laurentbfb1b832013-01-07 09:53:42 -0800675 data->mIO = output;
Jesper Tragardh48412dc2014-03-24 14:12:43 +0100676 command->mParam = data;
Eric Laurentbfb1b832013-01-07 09:53:42 -0800677 ALOGV("AudioCommandThread() adding release output %d", output);
Eric Laurent0ede8922014-05-09 18:04:42 -0700678 sendCommand(command);
679}
680
Eric Laurent951f4552014-05-20 10:48:17 -0700681status_t AudioPolicyService::AudioCommandThread::createAudioPatchCommand(
682 const struct audio_patch *patch,
683 audio_patch_handle_t *handle,
684 int delayMs)
685{
686 status_t status = NO_ERROR;
687
688 sp<AudioCommand> command = new AudioCommand();
689 command->mCommand = CREATE_AUDIO_PATCH;
690 CreateAudioPatchData *data = new CreateAudioPatchData();
691 data->mPatch = *patch;
692 data->mHandle = *handle;
693 command->mParam = data;
694 command->mWaitStatus = true;
695 ALOGV("AudioCommandThread() adding create patch delay %d", delayMs);
696 status = sendCommand(command, delayMs);
697 if (status == NO_ERROR) {
698 *handle = data->mHandle;
699 }
700 return status;
701}
702
703status_t AudioPolicyService::AudioCommandThread::releaseAudioPatchCommand(audio_patch_handle_t handle,
704 int delayMs)
705{
706 sp<AudioCommand> command = new AudioCommand();
707 command->mCommand = RELEASE_AUDIO_PATCH;
708 ReleaseAudioPatchData *data = new ReleaseAudioPatchData();
709 data->mHandle = handle;
710 command->mParam = data;
711 command->mWaitStatus = true;
712 ALOGV("AudioCommandThread() adding release patch delay %d", delayMs);
713 return sendCommand(command, delayMs);
714}
715
Eric Laurentb52c1522014-05-20 11:27:36 -0700716void AudioPolicyService::AudioCommandThread::updateAudioPortListCommand()
717{
718 sp<AudioCommand> command = new AudioCommand();
719 command->mCommand = UPDATE_AUDIOPORT_LIST;
720 ALOGV("AudioCommandThread() adding update audio port list");
721 sendCommand(command);
722}
723
724void AudioPolicyService::AudioCommandThread::updateAudioPatchListCommand()
725{
726 sp<AudioCommand>command = new AudioCommand();
727 command->mCommand = UPDATE_AUDIOPATCH_LIST;
728 ALOGV("AudioCommandThread() adding update audio patch list");
729 sendCommand(command);
730}
731
Eric Laurente1715a42014-05-20 11:30:42 -0700732status_t AudioPolicyService::AudioCommandThread::setAudioPortConfigCommand(
733 const struct audio_port_config *config, int delayMs)
734{
735 sp<AudioCommand> command = new AudioCommand();
736 command->mCommand = SET_AUDIOPORT_CONFIG;
737 SetAudioPortConfigData *data = new SetAudioPortConfigData();
738 data->mConfig = *config;
739 command->mParam = data;
740 command->mWaitStatus = true;
741 ALOGV("AudioCommandThread() adding set port config delay %d", delayMs);
742 return sendCommand(command, delayMs);
743}
744
Eric Laurent0ede8922014-05-09 18:04:42 -0700745status_t AudioPolicyService::AudioCommandThread::sendCommand(sp<AudioCommand>& command, int delayMs)
746{
747 {
748 Mutex::Autolock _l(mLock);
749 insertCommand_l(command, delayMs);
750 mWaitWorkCV.signal();
751 }
752 Mutex::Autolock _l(command->mLock);
753 while (command->mWaitStatus) {
754 nsecs_t timeOutNs = kAudioCommandTimeoutNs + milliseconds(delayMs);
755 if (command->mCond.waitRelative(command->mLock, timeOutNs) != NO_ERROR) {
756 command->mStatus = TIMED_OUT;
757 command->mWaitStatus = false;
758 }
759 }
760 return command->mStatus;
Eric Laurentbfb1b832013-01-07 09:53:42 -0800761}
762
Mathias Agopian65ab4712010-07-14 17:59:35 -0700763// insertCommand_l() must be called with mLock held
Eric Laurent0ede8922014-05-09 18:04:42 -0700764void AudioPolicyService::AudioCommandThread::insertCommand_l(sp<AudioCommand>& command, int delayMs)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700765{
Glenn Kasten8d6a2442012-02-08 14:04:28 -0800766 ssize_t i; // not size_t because i will count down to -1
Eric Laurent0ede8922014-05-09 18:04:42 -0700767 Vector < sp<AudioCommand> > removedCommands;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700768 command->mTime = systemTime() + milliseconds(delayMs);
769
770 // acquire wake lock to make sure delayed commands are processed
Eric Laurentbfb1b832013-01-07 09:53:42 -0800771 if (mAudioCommands.isEmpty()) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700772 acquire_wake_lock(PARTIAL_WAKE_LOCK, mName.string());
773 }
774
775 // check same pending commands with later time stamps and eliminate them
776 for (i = mAudioCommands.size()-1; i >= 0; i--) {
Eric Laurent0ede8922014-05-09 18:04:42 -0700777 sp<AudioCommand> command2 = mAudioCommands[i];
Mathias Agopian65ab4712010-07-14 17:59:35 -0700778 // commands are sorted by increasing time stamp: no need to scan the rest of mAudioCommands
779 if (command2->mTime <= command->mTime) break;
Eric Laurente45b48a2014-09-04 16:40:57 -0700780
781 // create audio patch or release audio patch commands are equivalent
782 // with regard to filtering
783 if ((command->mCommand == CREATE_AUDIO_PATCH) ||
784 (command->mCommand == RELEASE_AUDIO_PATCH)) {
785 if ((command2->mCommand != CREATE_AUDIO_PATCH) &&
786 (command2->mCommand != RELEASE_AUDIO_PATCH)) {
787 continue;
788 }
789 } else if (command2->mCommand != command->mCommand) continue;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700790
791 switch (command->mCommand) {
792 case SET_PARAMETERS: {
Eric Laurent0ede8922014-05-09 18:04:42 -0700793 ParametersData *data = (ParametersData *)command->mParam.get();
794 ParametersData *data2 = (ParametersData *)command2->mParam.get();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700795 if (data->mIO != data2->mIO) break;
Steve Block3856b092011-10-20 11:56:00 +0100796 ALOGV("Comparing parameter command %s to new command %s",
Eric Laurentde070132010-07-13 04:45:46 -0700797 data2->mKeyValuePairs.string(), data->mKeyValuePairs.string());
Mathias Agopian65ab4712010-07-14 17:59:35 -0700798 AudioParameter param = AudioParameter(data->mKeyValuePairs);
799 AudioParameter param2 = AudioParameter(data2->mKeyValuePairs);
800 for (size_t j = 0; j < param.size(); j++) {
Glenn Kastene53b9ea2012-03-12 16:29:55 -0700801 String8 key;
802 String8 value;
803 param.getAt(j, key, value);
804 for (size_t k = 0; k < param2.size(); k++) {
805 String8 key2;
806 String8 value2;
807 param2.getAt(k, key2, value2);
808 if (key2 == key) {
809 param2.remove(key2);
810 ALOGV("Filtering out parameter %s", key2.string());
811 break;
812 }
813 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700814 }
815 // if all keys have been filtered out, remove the command.
816 // otherwise, update the key value pairs
817 if (param2.size() == 0) {
818 removedCommands.add(command2);
819 } else {
820 data2->mKeyValuePairs = param2.toString();
821 }
Eric Laurent21e54562013-09-23 12:08:05 -0700822 command->mTime = command2->mTime;
823 // force delayMs to non 0 so that code below does not request to wait for
824 // command status as the command is now delayed
825 delayMs = 1;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700826 } break;
827
828 case SET_VOLUME: {
Eric Laurent0ede8922014-05-09 18:04:42 -0700829 VolumeData *data = (VolumeData *)command->mParam.get();
830 VolumeData *data2 = (VolumeData *)command2->mParam.get();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700831 if (data->mIO != data2->mIO) break;
832 if (data->mStream != data2->mStream) break;
Steve Block3856b092011-10-20 11:56:00 +0100833 ALOGV("Filtering out volume command on output %d for stream %d",
Eric Laurentde070132010-07-13 04:45:46 -0700834 data->mIO, data->mStream);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700835 removedCommands.add(command2);
Eric Laurent21e54562013-09-23 12:08:05 -0700836 command->mTime = command2->mTime;
837 // force delayMs to non 0 so that code below does not request to wait for
838 // command status as the command is now delayed
839 delayMs = 1;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700840 } break;
Eric Laurente45b48a2014-09-04 16:40:57 -0700841
842 case CREATE_AUDIO_PATCH:
843 case RELEASE_AUDIO_PATCH: {
844 audio_patch_handle_t handle;
Haynes Mathew Georgea2d4a6d2014-10-13 13:05:22 -0700845 struct audio_patch patch;
Eric Laurente45b48a2014-09-04 16:40:57 -0700846 if (command->mCommand == CREATE_AUDIO_PATCH) {
847 handle = ((CreateAudioPatchData *)command->mParam.get())->mHandle;
Haynes Mathew Georgea2d4a6d2014-10-13 13:05:22 -0700848 patch = ((CreateAudioPatchData *)command->mParam.get())->mPatch;
Eric Laurente45b48a2014-09-04 16:40:57 -0700849 } else {
850 handle = ((ReleaseAudioPatchData *)command->mParam.get())->mHandle;
851 }
852 audio_patch_handle_t handle2;
Haynes Mathew Georgea2d4a6d2014-10-13 13:05:22 -0700853 struct audio_patch patch2;
Eric Laurente45b48a2014-09-04 16:40:57 -0700854 if (command2->mCommand == CREATE_AUDIO_PATCH) {
855 handle2 = ((CreateAudioPatchData *)command2->mParam.get())->mHandle;
Haynes Mathew Georgea2d4a6d2014-10-13 13:05:22 -0700856 patch2 = ((CreateAudioPatchData *)command2->mParam.get())->mPatch;
Eric Laurente45b48a2014-09-04 16:40:57 -0700857 } else {
858 handle2 = ((ReleaseAudioPatchData *)command2->mParam.get())->mHandle;
859 }
860 if (handle != handle2) break;
Haynes Mathew Georgea2d4a6d2014-10-13 13:05:22 -0700861 /* Filter CREATE_AUDIO_PATCH commands only when they are issued for
862 same output. */
863 if( (command->mCommand == CREATE_AUDIO_PATCH) &&
864 (command2->mCommand == CREATE_AUDIO_PATCH) ) {
865 bool isOutputDiff = false;
866 if (patch.num_sources == patch2.num_sources) {
867 for (unsigned count = 0; count < patch.num_sources; count++) {
868 if (patch.sources[count].id != patch2.sources[count].id) {
869 isOutputDiff = true;
870 break;
871 }
872 }
873 if (isOutputDiff)
874 break;
875 }
876 }
Eric Laurente45b48a2014-09-04 16:40:57 -0700877 ALOGV("Filtering out %s audio patch command for handle %d",
878 (command->mCommand == CREATE_AUDIO_PATCH) ? "create" : "release", handle);
879 removedCommands.add(command2);
880 command->mTime = command2->mTime;
881 // force delayMs to non 0 so that code below does not request to wait for
882 // command status as the command is now delayed
883 delayMs = 1;
884 } break;
885
Mathias Agopian65ab4712010-07-14 17:59:35 -0700886 case START_TONE:
887 case STOP_TONE:
888 default:
889 break;
890 }
891 }
892
893 // remove filtered commands
894 for (size_t j = 0; j < removedCommands.size(); j++) {
895 // removed commands always have time stamps greater than current command
896 for (size_t k = i + 1; k < mAudioCommands.size(); k++) {
Eric Laurent0ede8922014-05-09 18:04:42 -0700897 if (mAudioCommands[k].get() == removedCommands[j].get()) {
Steve Block3856b092011-10-20 11:56:00 +0100898 ALOGV("suppressing command: %d", mAudioCommands[k]->mCommand);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700899 mAudioCommands.removeAt(k);
900 break;
901 }
902 }
903 }
904 removedCommands.clear();
905
Eric Laurent0ede8922014-05-09 18:04:42 -0700906 // Disable wait for status if delay is not 0
907 if (delayMs != 0) {
Eric Laurentcec4abb2012-07-03 12:23:02 -0700908 command->mWaitStatus = false;
909 }
Eric Laurentcec4abb2012-07-03 12:23:02 -0700910
Mathias Agopian65ab4712010-07-14 17:59:35 -0700911 // insert command at the right place according to its time stamp
Eric Laurent1e693b52014-07-09 15:03:28 -0700912 ALOGV("inserting command: %d at index %zd, num commands %zu",
913 command->mCommand, i+1, mAudioCommands.size());
Mathias Agopian65ab4712010-07-14 17:59:35 -0700914 mAudioCommands.insertAt(command, i + 1);
915}
916
917void AudioPolicyService::AudioCommandThread::exit()
918{
Steve Block3856b092011-10-20 11:56:00 +0100919 ALOGV("AudioCommandThread::exit");
Mathias Agopian65ab4712010-07-14 17:59:35 -0700920 {
921 AutoMutex _l(mLock);
922 requestExit();
923 mWaitWorkCV.signal();
924 }
925 requestExitAndWait();
926}
927
928void AudioPolicyService::AudioCommandThread::AudioCommand::dump(char* buffer, size_t size)
929{
930 snprintf(buffer, size, " %02d %06d.%03d %01u %p\n",
931 mCommand,
932 (int)ns2s(mTime),
933 (int)ns2ms(mTime)%1000,
934 mWaitStatus,
Eric Laurent0ede8922014-05-09 18:04:42 -0700935 mParam.get());
Mathias Agopian65ab4712010-07-14 17:59:35 -0700936}
937
Dima Zavinfce7a472011-04-19 22:30:36 -0700938/******* helpers for the service_ops callbacks defined below *********/
939void AudioPolicyService::setParameters(audio_io_handle_t ioHandle,
940 const char *keyValuePairs,
941 int delayMs)
942{
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800943 mAudioCommandThread->parametersCommand(ioHandle, keyValuePairs,
Dima Zavinfce7a472011-04-19 22:30:36 -0700944 delayMs);
945}
946
947int AudioPolicyService::setStreamVolume(audio_stream_type_t stream,
948 float volume,
949 audio_io_handle_t output,
950 int delayMs)
951{
Glenn Kastenfff6d712012-01-12 16:38:12 -0800952 return (int)mAudioCommandThread->volumeCommand(stream, volume,
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800953 output, delayMs);
Dima Zavinfce7a472011-04-19 22:30:36 -0700954}
955
956int AudioPolicyService::startTone(audio_policy_tone_t tone,
957 audio_stream_type_t stream)
958{
Glenn Kasten6e2ebe92013-08-13 09:14:51 -0700959 if (tone != AUDIO_POLICY_TONE_IN_CALL_NOTIFICATION) {
Steve Block29357bc2012-01-06 19:20:56 +0000960 ALOGE("startTone: illegal tone requested (%d)", tone);
Glenn Kasten6e2ebe92013-08-13 09:14:51 -0700961 }
962 if (stream != AUDIO_STREAM_VOICE_CALL) {
Steve Block29357bc2012-01-06 19:20:56 +0000963 ALOGE("startTone: illegal stream (%d) requested for tone %d", stream,
Glenn Kastene53b9ea2012-03-12 16:29:55 -0700964 tone);
Glenn Kasten6e2ebe92013-08-13 09:14:51 -0700965 }
Dima Zavinfce7a472011-04-19 22:30:36 -0700966 mTonePlaybackThread->startToneCommand(ToneGenerator::TONE_SUP_CALL_WAITING,
967 AUDIO_STREAM_VOICE_CALL);
968 return 0;
969}
970
971int AudioPolicyService::stopTone()
972{
973 mTonePlaybackThread->stopToneCommand();
974 return 0;
975}
976
977int AudioPolicyService::setVoiceVolume(float volume, int delayMs)
978{
979 return (int)mAudioCommandThread->voiceVolumeCommand(volume, delayMs);
980}
981
Dima Zavinfce7a472011-04-19 22:30:36 -0700982extern "C" {
Eric Laurent2d388ec2014-03-07 13:25:54 -0800983audio_module_handle_t aps_load_hw_module(void *service __unused,
984 const char *name);
985audio_io_handle_t aps_open_output(void *service __unused,
Eric Laurenta4c5a552012-03-29 10:12:40 -0700986 audio_devices_t *pDevices,
987 uint32_t *pSamplingRate,
988 audio_format_t *pFormat,
989 audio_channel_mask_t *pChannelMask,
990 uint32_t *pLatencyMs,
Eric Laurent2d388ec2014-03-07 13:25:54 -0800991 audio_output_flags_t flags);
Eric Laurenta4c5a552012-03-29 10:12:40 -0700992
Eric Laurent2d388ec2014-03-07 13:25:54 -0800993audio_io_handle_t aps_open_output_on_module(void *service __unused,
Eric Laurenta4c5a552012-03-29 10:12:40 -0700994 audio_module_handle_t module,
995 audio_devices_t *pDevices,
996 uint32_t *pSamplingRate,
997 audio_format_t *pFormat,
998 audio_channel_mask_t *pChannelMask,
999 uint32_t *pLatencyMs,
Richard Fitzgeraldad3af332013-03-25 16:54:37 +00001000 audio_output_flags_t flags,
Eric Laurent2d388ec2014-03-07 13:25:54 -08001001 const audio_offload_info_t *offloadInfo);
1002audio_io_handle_t aps_open_dup_output(void *service __unused,
Dima Zavinfce7a472011-04-19 22:30:36 -07001003 audio_io_handle_t output1,
Eric Laurent2d388ec2014-03-07 13:25:54 -08001004 audio_io_handle_t output2);
1005int aps_close_output(void *service __unused, audio_io_handle_t output);
1006int aps_suspend_output(void *service __unused, audio_io_handle_t output);
1007int aps_restore_output(void *service __unused, audio_io_handle_t output);
1008audio_io_handle_t aps_open_input(void *service __unused,
Eric Laurenta4c5a552012-03-29 10:12:40 -07001009 audio_devices_t *pDevices,
1010 uint32_t *pSamplingRate,
1011 audio_format_t *pFormat,
1012 audio_channel_mask_t *pChannelMask,
Eric Laurent2d388ec2014-03-07 13:25:54 -08001013 audio_in_acoustics_t acoustics __unused);
1014audio_io_handle_t aps_open_input_on_module(void *service __unused,
Eric Laurenta4c5a552012-03-29 10:12:40 -07001015 audio_module_handle_t module,
1016 audio_devices_t *pDevices,
1017 uint32_t *pSamplingRate,
1018 audio_format_t *pFormat,
Eric Laurent2d388ec2014-03-07 13:25:54 -08001019 audio_channel_mask_t *pChannelMask);
1020int aps_close_input(void *service __unused, audio_io_handle_t input);
1021int aps_invalidate_stream(void *service __unused, audio_stream_type_t stream);
1022int aps_move_effects(void *service __unused, int session,
Dima Zavinfce7a472011-04-19 22:30:36 -07001023 audio_io_handle_t src_output,
Eric Laurent2d388ec2014-03-07 13:25:54 -08001024 audio_io_handle_t dst_output);
1025char * aps_get_parameters(void *service __unused, audio_io_handle_t io_handle,
1026 const char *keys);
1027void aps_set_parameters(void *service, audio_io_handle_t io_handle,
1028 const char *kv_pairs, int delay_ms);
1029int aps_set_stream_volume(void *service, audio_stream_type_t stream,
Dima Zavinfce7a472011-04-19 22:30:36 -07001030 float volume, audio_io_handle_t output,
Eric Laurent2d388ec2014-03-07 13:25:54 -08001031 int delay_ms);
1032int aps_start_tone(void *service, audio_policy_tone_t tone,
1033 audio_stream_type_t stream);
1034int aps_stop_tone(void *service);
1035int aps_set_voice_volume(void *service, float volume, int delay_ms);
1036};
Dima Zavinfce7a472011-04-19 22:30:36 -07001037
1038namespace {
1039 struct audio_policy_service_ops aps_ops = {
Glenn Kasten01d3acb2014-02-06 08:24:07 -08001040 .open_output = aps_open_output,
1041 .open_duplicate_output = aps_open_dup_output,
1042 .close_output = aps_close_output,
1043 .suspend_output = aps_suspend_output,
1044 .restore_output = aps_restore_output,
1045 .open_input = aps_open_input,
1046 .close_input = aps_close_input,
1047 .set_stream_volume = aps_set_stream_volume,
Glenn Kastend2304db2014-02-03 07:40:31 -08001048 .invalidate_stream = aps_invalidate_stream,
Glenn Kasten01d3acb2014-02-06 08:24:07 -08001049 .set_parameters = aps_set_parameters,
1050 .get_parameters = aps_get_parameters,
1051 .start_tone = aps_start_tone,
1052 .stop_tone = aps_stop_tone,
1053 .set_voice_volume = aps_set_voice_volume,
1054 .move_effects = aps_move_effects,
1055 .load_hw_module = aps_load_hw_module,
1056 .open_output_on_module = aps_open_output_on_module,
1057 .open_input_on_module = aps_open_input_on_module,
Dima Zavinfce7a472011-04-19 22:30:36 -07001058 };
1059}; // namespace <unnamed>
1060
Mathias Agopian65ab4712010-07-14 17:59:35 -07001061}; // namespace android