blob: 0955e10da44361e5a9dbd604ff2a4cd0ce40078d [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();
Eric Laurente83b55d2014-11-14 10:06:21 -0800460 svc->doReleaseOutput(data->mIO, data->mStream, data->mSession);
Eric Laurentbfb1b832013-01-07 09:53:42 -0800461 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,
Eric Laurente83b55d2014-11-14 10:06:21 -0800657 audio_session_t session)
Eric Laurentbfb1b832013-01-07 09:53:42 -0800658{
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
Eric Laurente83b55d2014-11-14 10:06:21 -0800670void AudioPolicyService::AudioCommandThread::releaseOutputCommand(audio_io_handle_t output,
671 audio_stream_type_t stream,
672 audio_session_t session)
Eric Laurentbfb1b832013-01-07 09:53:42 -0800673{
Eric Laurent0ede8922014-05-09 18:04:42 -0700674 sp<AudioCommand> command = new AudioCommand();
Eric Laurentbfb1b832013-01-07 09:53:42 -0800675 command->mCommand = RELEASE_OUTPUT;
Eric Laurent0ede8922014-05-09 18:04:42 -0700676 sp<ReleaseOutputData> data = new ReleaseOutputData();
Eric Laurentbfb1b832013-01-07 09:53:42 -0800677 data->mIO = output;
Eric Laurente83b55d2014-11-14 10:06:21 -0800678 data->mStream = stream;
679 data->mSession = session;
Jesper Tragardh48412dc2014-03-24 14:12:43 +0100680 command->mParam = data;
Eric Laurentbfb1b832013-01-07 09:53:42 -0800681 ALOGV("AudioCommandThread() adding release output %d", output);
Eric Laurent0ede8922014-05-09 18:04:42 -0700682 sendCommand(command);
683}
684
Eric Laurent951f4552014-05-20 10:48:17 -0700685status_t AudioPolicyService::AudioCommandThread::createAudioPatchCommand(
686 const struct audio_patch *patch,
687 audio_patch_handle_t *handle,
688 int delayMs)
689{
690 status_t status = NO_ERROR;
691
692 sp<AudioCommand> command = new AudioCommand();
693 command->mCommand = CREATE_AUDIO_PATCH;
694 CreateAudioPatchData *data = new CreateAudioPatchData();
695 data->mPatch = *patch;
696 data->mHandle = *handle;
697 command->mParam = data;
698 command->mWaitStatus = true;
699 ALOGV("AudioCommandThread() adding create patch delay %d", delayMs);
700 status = sendCommand(command, delayMs);
701 if (status == NO_ERROR) {
702 *handle = data->mHandle;
703 }
704 return status;
705}
706
707status_t AudioPolicyService::AudioCommandThread::releaseAudioPatchCommand(audio_patch_handle_t handle,
708 int delayMs)
709{
710 sp<AudioCommand> command = new AudioCommand();
711 command->mCommand = RELEASE_AUDIO_PATCH;
712 ReleaseAudioPatchData *data = new ReleaseAudioPatchData();
713 data->mHandle = handle;
714 command->mParam = data;
715 command->mWaitStatus = true;
716 ALOGV("AudioCommandThread() adding release patch delay %d", delayMs);
717 return sendCommand(command, delayMs);
718}
719
Eric Laurentb52c1522014-05-20 11:27:36 -0700720void AudioPolicyService::AudioCommandThread::updateAudioPortListCommand()
721{
722 sp<AudioCommand> command = new AudioCommand();
723 command->mCommand = UPDATE_AUDIOPORT_LIST;
724 ALOGV("AudioCommandThread() adding update audio port list");
725 sendCommand(command);
726}
727
728void AudioPolicyService::AudioCommandThread::updateAudioPatchListCommand()
729{
730 sp<AudioCommand>command = new AudioCommand();
731 command->mCommand = UPDATE_AUDIOPATCH_LIST;
732 ALOGV("AudioCommandThread() adding update audio patch list");
733 sendCommand(command);
734}
735
Eric Laurente1715a42014-05-20 11:30:42 -0700736status_t AudioPolicyService::AudioCommandThread::setAudioPortConfigCommand(
737 const struct audio_port_config *config, int delayMs)
738{
739 sp<AudioCommand> command = new AudioCommand();
740 command->mCommand = SET_AUDIOPORT_CONFIG;
741 SetAudioPortConfigData *data = new SetAudioPortConfigData();
742 data->mConfig = *config;
743 command->mParam = data;
744 command->mWaitStatus = true;
745 ALOGV("AudioCommandThread() adding set port config delay %d", delayMs);
746 return sendCommand(command, delayMs);
747}
748
Eric Laurent0ede8922014-05-09 18:04:42 -0700749status_t AudioPolicyService::AudioCommandThread::sendCommand(sp<AudioCommand>& command, int delayMs)
750{
751 {
752 Mutex::Autolock _l(mLock);
753 insertCommand_l(command, delayMs);
754 mWaitWorkCV.signal();
755 }
756 Mutex::Autolock _l(command->mLock);
757 while (command->mWaitStatus) {
758 nsecs_t timeOutNs = kAudioCommandTimeoutNs + milliseconds(delayMs);
759 if (command->mCond.waitRelative(command->mLock, timeOutNs) != NO_ERROR) {
760 command->mStatus = TIMED_OUT;
761 command->mWaitStatus = false;
762 }
763 }
764 return command->mStatus;
Eric Laurentbfb1b832013-01-07 09:53:42 -0800765}
766
Mathias Agopian65ab4712010-07-14 17:59:35 -0700767// insertCommand_l() must be called with mLock held
Eric Laurent0ede8922014-05-09 18:04:42 -0700768void AudioPolicyService::AudioCommandThread::insertCommand_l(sp<AudioCommand>& command, int delayMs)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700769{
Glenn Kasten8d6a2442012-02-08 14:04:28 -0800770 ssize_t i; // not size_t because i will count down to -1
Eric Laurent0ede8922014-05-09 18:04:42 -0700771 Vector < sp<AudioCommand> > removedCommands;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700772 command->mTime = systemTime() + milliseconds(delayMs);
773
774 // acquire wake lock to make sure delayed commands are processed
Eric Laurentbfb1b832013-01-07 09:53:42 -0800775 if (mAudioCommands.isEmpty()) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700776 acquire_wake_lock(PARTIAL_WAKE_LOCK, mName.string());
777 }
778
779 // check same pending commands with later time stamps and eliminate them
780 for (i = mAudioCommands.size()-1; i >= 0; i--) {
Eric Laurent0ede8922014-05-09 18:04:42 -0700781 sp<AudioCommand> command2 = mAudioCommands[i];
Mathias Agopian65ab4712010-07-14 17:59:35 -0700782 // commands are sorted by increasing time stamp: no need to scan the rest of mAudioCommands
783 if (command2->mTime <= command->mTime) break;
Eric Laurente45b48a2014-09-04 16:40:57 -0700784
785 // create audio patch or release audio patch commands are equivalent
786 // with regard to filtering
787 if ((command->mCommand == CREATE_AUDIO_PATCH) ||
788 (command->mCommand == RELEASE_AUDIO_PATCH)) {
789 if ((command2->mCommand != CREATE_AUDIO_PATCH) &&
790 (command2->mCommand != RELEASE_AUDIO_PATCH)) {
791 continue;
792 }
793 } else if (command2->mCommand != command->mCommand) continue;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700794
795 switch (command->mCommand) {
796 case SET_PARAMETERS: {
Eric Laurent0ede8922014-05-09 18:04:42 -0700797 ParametersData *data = (ParametersData *)command->mParam.get();
798 ParametersData *data2 = (ParametersData *)command2->mParam.get();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700799 if (data->mIO != data2->mIO) break;
Steve Block3856b092011-10-20 11:56:00 +0100800 ALOGV("Comparing parameter command %s to new command %s",
Eric Laurentde070132010-07-13 04:45:46 -0700801 data2->mKeyValuePairs.string(), data->mKeyValuePairs.string());
Mathias Agopian65ab4712010-07-14 17:59:35 -0700802 AudioParameter param = AudioParameter(data->mKeyValuePairs);
803 AudioParameter param2 = AudioParameter(data2->mKeyValuePairs);
804 for (size_t j = 0; j < param.size(); j++) {
Glenn Kastene53b9ea2012-03-12 16:29:55 -0700805 String8 key;
806 String8 value;
807 param.getAt(j, key, value);
808 for (size_t k = 0; k < param2.size(); k++) {
809 String8 key2;
810 String8 value2;
811 param2.getAt(k, key2, value2);
812 if (key2 == key) {
813 param2.remove(key2);
814 ALOGV("Filtering out parameter %s", key2.string());
815 break;
816 }
817 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700818 }
819 // if all keys have been filtered out, remove the command.
820 // otherwise, update the key value pairs
821 if (param2.size() == 0) {
822 removedCommands.add(command2);
823 } else {
824 data2->mKeyValuePairs = param2.toString();
825 }
Eric Laurent21e54562013-09-23 12:08:05 -0700826 command->mTime = command2->mTime;
827 // force delayMs to non 0 so that code below does not request to wait for
828 // command status as the command is now delayed
829 delayMs = 1;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700830 } break;
831
832 case SET_VOLUME: {
Eric Laurent0ede8922014-05-09 18:04:42 -0700833 VolumeData *data = (VolumeData *)command->mParam.get();
834 VolumeData *data2 = (VolumeData *)command2->mParam.get();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700835 if (data->mIO != data2->mIO) break;
836 if (data->mStream != data2->mStream) break;
Steve Block3856b092011-10-20 11:56:00 +0100837 ALOGV("Filtering out volume command on output %d for stream %d",
Eric Laurentde070132010-07-13 04:45:46 -0700838 data->mIO, data->mStream);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700839 removedCommands.add(command2);
Eric Laurent21e54562013-09-23 12:08:05 -0700840 command->mTime = command2->mTime;
841 // force delayMs to non 0 so that code below does not request to wait for
842 // command status as the command is now delayed
843 delayMs = 1;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700844 } break;
Eric Laurente45b48a2014-09-04 16:40:57 -0700845
846 case CREATE_AUDIO_PATCH:
847 case RELEASE_AUDIO_PATCH: {
848 audio_patch_handle_t handle;
Haynes Mathew Georgea2d4a6d2014-10-13 13:05:22 -0700849 struct audio_patch patch;
Eric Laurente45b48a2014-09-04 16:40:57 -0700850 if (command->mCommand == CREATE_AUDIO_PATCH) {
851 handle = ((CreateAudioPatchData *)command->mParam.get())->mHandle;
Haynes Mathew Georgea2d4a6d2014-10-13 13:05:22 -0700852 patch = ((CreateAudioPatchData *)command->mParam.get())->mPatch;
Eric Laurente45b48a2014-09-04 16:40:57 -0700853 } else {
854 handle = ((ReleaseAudioPatchData *)command->mParam.get())->mHandle;
855 }
856 audio_patch_handle_t handle2;
Haynes Mathew Georgea2d4a6d2014-10-13 13:05:22 -0700857 struct audio_patch patch2;
Eric Laurente45b48a2014-09-04 16:40:57 -0700858 if (command2->mCommand == CREATE_AUDIO_PATCH) {
859 handle2 = ((CreateAudioPatchData *)command2->mParam.get())->mHandle;
Haynes Mathew Georgea2d4a6d2014-10-13 13:05:22 -0700860 patch2 = ((CreateAudioPatchData *)command2->mParam.get())->mPatch;
Eric Laurente45b48a2014-09-04 16:40:57 -0700861 } else {
862 handle2 = ((ReleaseAudioPatchData *)command2->mParam.get())->mHandle;
863 }
864 if (handle != handle2) break;
Haynes Mathew Georgea2d4a6d2014-10-13 13:05:22 -0700865 /* Filter CREATE_AUDIO_PATCH commands only when they are issued for
866 same output. */
867 if( (command->mCommand == CREATE_AUDIO_PATCH) &&
868 (command2->mCommand == CREATE_AUDIO_PATCH) ) {
869 bool isOutputDiff = false;
870 if (patch.num_sources == patch2.num_sources) {
871 for (unsigned count = 0; count < patch.num_sources; count++) {
872 if (patch.sources[count].id != patch2.sources[count].id) {
873 isOutputDiff = true;
874 break;
875 }
876 }
877 if (isOutputDiff)
878 break;
879 }
880 }
Eric Laurente45b48a2014-09-04 16:40:57 -0700881 ALOGV("Filtering out %s audio patch command for handle %d",
882 (command->mCommand == CREATE_AUDIO_PATCH) ? "create" : "release", handle);
883 removedCommands.add(command2);
884 command->mTime = command2->mTime;
885 // force delayMs to non 0 so that code below does not request to wait for
886 // command status as the command is now delayed
887 delayMs = 1;
888 } break;
889
Mathias Agopian65ab4712010-07-14 17:59:35 -0700890 case START_TONE:
891 case STOP_TONE:
892 default:
893 break;
894 }
895 }
896
897 // remove filtered commands
898 for (size_t j = 0; j < removedCommands.size(); j++) {
899 // removed commands always have time stamps greater than current command
900 for (size_t k = i + 1; k < mAudioCommands.size(); k++) {
Eric Laurent0ede8922014-05-09 18:04:42 -0700901 if (mAudioCommands[k].get() == removedCommands[j].get()) {
Steve Block3856b092011-10-20 11:56:00 +0100902 ALOGV("suppressing command: %d", mAudioCommands[k]->mCommand);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700903 mAudioCommands.removeAt(k);
904 break;
905 }
906 }
907 }
908 removedCommands.clear();
909
Eric Laurentaa79bef2015-01-15 14:33:51 -0800910 // Disable wait for status if delay is not 0.
911 // Except for create audio patch command because the returned patch handle
912 // is needed by audio policy manager
913 if (delayMs != 0 && command->mCommand != CREATE_AUDIO_PATCH) {
Eric Laurentcec4abb2012-07-03 12:23:02 -0700914 command->mWaitStatus = false;
915 }
Eric Laurentcec4abb2012-07-03 12:23:02 -0700916
Mathias Agopian65ab4712010-07-14 17:59:35 -0700917 // insert command at the right place according to its time stamp
Eric Laurent1e693b52014-07-09 15:03:28 -0700918 ALOGV("inserting command: %d at index %zd, num commands %zu",
919 command->mCommand, i+1, mAudioCommands.size());
Mathias Agopian65ab4712010-07-14 17:59:35 -0700920 mAudioCommands.insertAt(command, i + 1);
921}
922
923void AudioPolicyService::AudioCommandThread::exit()
924{
Steve Block3856b092011-10-20 11:56:00 +0100925 ALOGV("AudioCommandThread::exit");
Mathias Agopian65ab4712010-07-14 17:59:35 -0700926 {
927 AutoMutex _l(mLock);
928 requestExit();
929 mWaitWorkCV.signal();
930 }
931 requestExitAndWait();
932}
933
934void AudioPolicyService::AudioCommandThread::AudioCommand::dump(char* buffer, size_t size)
935{
936 snprintf(buffer, size, " %02d %06d.%03d %01u %p\n",
937 mCommand,
938 (int)ns2s(mTime),
939 (int)ns2ms(mTime)%1000,
940 mWaitStatus,
Eric Laurent0ede8922014-05-09 18:04:42 -0700941 mParam.get());
Mathias Agopian65ab4712010-07-14 17:59:35 -0700942}
943
Dima Zavinfce7a472011-04-19 22:30:36 -0700944/******* helpers for the service_ops callbacks defined below *********/
945void AudioPolicyService::setParameters(audio_io_handle_t ioHandle,
946 const char *keyValuePairs,
947 int delayMs)
948{
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800949 mAudioCommandThread->parametersCommand(ioHandle, keyValuePairs,
Dima Zavinfce7a472011-04-19 22:30:36 -0700950 delayMs);
951}
952
953int AudioPolicyService::setStreamVolume(audio_stream_type_t stream,
954 float volume,
955 audio_io_handle_t output,
956 int delayMs)
957{
Glenn Kastenfff6d712012-01-12 16:38:12 -0800958 return (int)mAudioCommandThread->volumeCommand(stream, volume,
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800959 output, delayMs);
Dima Zavinfce7a472011-04-19 22:30:36 -0700960}
961
962int AudioPolicyService::startTone(audio_policy_tone_t tone,
963 audio_stream_type_t stream)
964{
Glenn Kasten6e2ebe92013-08-13 09:14:51 -0700965 if (tone != AUDIO_POLICY_TONE_IN_CALL_NOTIFICATION) {
Steve Block29357bc2012-01-06 19:20:56 +0000966 ALOGE("startTone: illegal tone requested (%d)", tone);
Glenn Kasten6e2ebe92013-08-13 09:14:51 -0700967 }
968 if (stream != AUDIO_STREAM_VOICE_CALL) {
Steve Block29357bc2012-01-06 19:20:56 +0000969 ALOGE("startTone: illegal stream (%d) requested for tone %d", stream,
Glenn Kastene53b9ea2012-03-12 16:29:55 -0700970 tone);
Glenn Kasten6e2ebe92013-08-13 09:14:51 -0700971 }
Dima Zavinfce7a472011-04-19 22:30:36 -0700972 mTonePlaybackThread->startToneCommand(ToneGenerator::TONE_SUP_CALL_WAITING,
973 AUDIO_STREAM_VOICE_CALL);
974 return 0;
975}
976
977int AudioPolicyService::stopTone()
978{
979 mTonePlaybackThread->stopToneCommand();
980 return 0;
981}
982
983int AudioPolicyService::setVoiceVolume(float volume, int delayMs)
984{
985 return (int)mAudioCommandThread->voiceVolumeCommand(volume, delayMs);
986}
987
Dima Zavinfce7a472011-04-19 22:30:36 -0700988extern "C" {
Eric Laurent2d388ec2014-03-07 13:25:54 -0800989audio_module_handle_t aps_load_hw_module(void *service __unused,
990 const char *name);
991audio_io_handle_t aps_open_output(void *service __unused,
Eric Laurenta4c5a552012-03-29 10:12:40 -0700992 audio_devices_t *pDevices,
993 uint32_t *pSamplingRate,
994 audio_format_t *pFormat,
995 audio_channel_mask_t *pChannelMask,
996 uint32_t *pLatencyMs,
Eric Laurent2d388ec2014-03-07 13:25:54 -0800997 audio_output_flags_t flags);
Eric Laurenta4c5a552012-03-29 10:12:40 -0700998
Eric Laurent2d388ec2014-03-07 13:25:54 -0800999audio_io_handle_t aps_open_output_on_module(void *service __unused,
Eric Laurenta4c5a552012-03-29 10:12:40 -07001000 audio_module_handle_t module,
1001 audio_devices_t *pDevices,
1002 uint32_t *pSamplingRate,
1003 audio_format_t *pFormat,
1004 audio_channel_mask_t *pChannelMask,
1005 uint32_t *pLatencyMs,
Richard Fitzgeraldad3af332013-03-25 16:54:37 +00001006 audio_output_flags_t flags,
Eric Laurent2d388ec2014-03-07 13:25:54 -08001007 const audio_offload_info_t *offloadInfo);
1008audio_io_handle_t aps_open_dup_output(void *service __unused,
Dima Zavinfce7a472011-04-19 22:30:36 -07001009 audio_io_handle_t output1,
Eric Laurent2d388ec2014-03-07 13:25:54 -08001010 audio_io_handle_t output2);
1011int aps_close_output(void *service __unused, audio_io_handle_t output);
1012int aps_suspend_output(void *service __unused, audio_io_handle_t output);
1013int aps_restore_output(void *service __unused, audio_io_handle_t output);
1014audio_io_handle_t aps_open_input(void *service __unused,
Eric Laurenta4c5a552012-03-29 10:12:40 -07001015 audio_devices_t *pDevices,
1016 uint32_t *pSamplingRate,
1017 audio_format_t *pFormat,
1018 audio_channel_mask_t *pChannelMask,
Eric Laurent2d388ec2014-03-07 13:25:54 -08001019 audio_in_acoustics_t acoustics __unused);
1020audio_io_handle_t aps_open_input_on_module(void *service __unused,
Eric Laurenta4c5a552012-03-29 10:12:40 -07001021 audio_module_handle_t module,
1022 audio_devices_t *pDevices,
1023 uint32_t *pSamplingRate,
1024 audio_format_t *pFormat,
Eric Laurent2d388ec2014-03-07 13:25:54 -08001025 audio_channel_mask_t *pChannelMask);
1026int aps_close_input(void *service __unused, audio_io_handle_t input);
1027int aps_invalidate_stream(void *service __unused, audio_stream_type_t stream);
1028int aps_move_effects(void *service __unused, int session,
Dima Zavinfce7a472011-04-19 22:30:36 -07001029 audio_io_handle_t src_output,
Eric Laurent2d388ec2014-03-07 13:25:54 -08001030 audio_io_handle_t dst_output);
1031char * aps_get_parameters(void *service __unused, audio_io_handle_t io_handle,
1032 const char *keys);
1033void aps_set_parameters(void *service, audio_io_handle_t io_handle,
1034 const char *kv_pairs, int delay_ms);
1035int aps_set_stream_volume(void *service, audio_stream_type_t stream,
Dima Zavinfce7a472011-04-19 22:30:36 -07001036 float volume, audio_io_handle_t output,
Eric Laurent2d388ec2014-03-07 13:25:54 -08001037 int delay_ms);
1038int aps_start_tone(void *service, audio_policy_tone_t tone,
1039 audio_stream_type_t stream);
1040int aps_stop_tone(void *service);
1041int aps_set_voice_volume(void *service, float volume, int delay_ms);
1042};
Dima Zavinfce7a472011-04-19 22:30:36 -07001043
1044namespace {
1045 struct audio_policy_service_ops aps_ops = {
Glenn Kasten01d3acb2014-02-06 08:24:07 -08001046 .open_output = aps_open_output,
1047 .open_duplicate_output = aps_open_dup_output,
1048 .close_output = aps_close_output,
1049 .suspend_output = aps_suspend_output,
1050 .restore_output = aps_restore_output,
1051 .open_input = aps_open_input,
1052 .close_input = aps_close_input,
1053 .set_stream_volume = aps_set_stream_volume,
Glenn Kastend2304db2014-02-03 07:40:31 -08001054 .invalidate_stream = aps_invalidate_stream,
Glenn Kasten01d3acb2014-02-06 08:24:07 -08001055 .set_parameters = aps_set_parameters,
1056 .get_parameters = aps_get_parameters,
1057 .start_tone = aps_start_tone,
1058 .stop_tone = aps_stop_tone,
1059 .set_voice_volume = aps_set_voice_volume,
1060 .move_effects = aps_move_effects,
1061 .load_hw_module = aps_load_hw_module,
1062 .open_output_on_module = aps_open_output_on_module,
1063 .open_input_on_module = aps_open_input_on_module,
Dima Zavinfce7a472011-04-19 22:30:36 -07001064 };
1065}; // namespace <unnamed>
1066
Mathias Agopian65ab4712010-07-14 17:59:35 -07001067}; // namespace android