blob: 4761a131f7b7a52fcd6b52fdc46e48ed5c41971d [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{
64 char value[PROPERTY_VALUE_MAX];
Dima Zavinfce7a472011-04-19 22:30:36 -070065 const struct hw_module_t *module;
66 int forced_val;
67 int rc;
Mathias Agopian65ab4712010-07-14 17:59:35 -070068
Eric Laurent8b1e80b2014-10-07 09:08:47 -070069 {
70 Mutex::Autolock _l(mLock);
Eric Laurent93575202011-01-18 18:39:02 -080071
Eric Laurent8b1e80b2014-10-07 09:08:47 -070072 // start tone playback thread
73 mTonePlaybackThread = new AudioCommandThread(String8("ApmTone"), this);
74 // start audio commands thread
75 mAudioCommandThread = new AudioCommandThread(String8("ApmAudio"), this);
76 // start output activity command thread
77 mOutputCommandThread = new AudioCommandThread(String8("ApmOutput"), this);
Eric Laurentdce54a12014-03-10 12:19:46 -070078
79#ifdef USE_LEGACY_AUDIO_POLICY
Eric Laurent8b1e80b2014-10-07 09:08:47 -070080 ALOGI("AudioPolicyService CSTOR in legacy mode");
Eric Laurentdce54a12014-03-10 12:19:46 -070081
Eric Laurent8b1e80b2014-10-07 09:08:47 -070082 /* instantiate the audio policy manager */
83 rc = hw_get_module(AUDIO_POLICY_HARDWARE_MODULE_ID, &module);
84 if (rc) {
85 return;
86 }
87 rc = audio_policy_dev_open(module, &mpAudioPolicyDev);
88 ALOGE_IF(rc, "couldn't open audio policy device (%s)", strerror(-rc));
89 if (rc) {
90 return;
91 }
Eric Laurent93575202011-01-18 18:39:02 -080092
Eric Laurent8b1e80b2014-10-07 09:08:47 -070093 rc = mpAudioPolicyDev->create_audio_policy(mpAudioPolicyDev, &aps_ops, this,
94 &mpAudioPolicy);
95 ALOGE_IF(rc, "couldn't create audio policy (%s)", strerror(-rc));
96 if (rc) {
97 return;
98 }
Dima Zavinfce7a472011-04-19 22:30:36 -070099
Eric Laurent8b1e80b2014-10-07 09:08:47 -0700100 rc = mpAudioPolicy->init_check(mpAudioPolicy);
101 ALOGE_IF(rc, "couldn't init_check the audio policy (%s)", strerror(-rc));
102 if (rc) {
103 return;
104 }
105 ALOGI("Loaded audio policy from %s (%s)", module->name, module->id);
Eric Laurentdce54a12014-03-10 12:19:46 -0700106#else
Eric Laurent8b1e80b2014-10-07 09:08:47 -0700107 ALOGI("AudioPolicyService CSTOR in new mode");
Eric Laurentdce54a12014-03-10 12:19:46 -0700108
Eric Laurent8b1e80b2014-10-07 09:08:47 -0700109 mAudioPolicyClient = new AudioPolicyClient(this);
110 mAudioPolicyManager = createAudioPolicyManager(mAudioPolicyClient);
Eric Laurentdce54a12014-03-10 12:19:46 -0700111#endif
Eric Laurent8b1e80b2014-10-07 09:08:47 -0700112 }
bryant_liuba2b4392014-06-11 16:49:30 +0800113 // load audio processing modules
Eric Laurent8b1e80b2014-10-07 09:08:47 -0700114 sp<AudioPolicyEffects>audioPolicyEffects = new AudioPolicyEffects();
115 {
116 Mutex::Autolock _l(mLock);
117 mAudioPolicyEffects = audioPolicyEffects;
118 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700119}
120
121AudioPolicyService::~AudioPolicyService()
122{
123 mTonePlaybackThread->exit();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700124 mAudioCommandThread->exit();
Eric Laurent657ff612014-05-07 11:58:24 -0700125 mOutputCommandThread->exit();
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700126
Eric Laurentdce54a12014-03-10 12:19:46 -0700127#ifdef USE_LEGACY_AUDIO_POLICY
Glenn Kasten6e2ebe92013-08-13 09:14:51 -0700128 if (mpAudioPolicy != NULL && mpAudioPolicyDev != NULL) {
Dima Zavinfce7a472011-04-19 22:30:36 -0700129 mpAudioPolicyDev->destroy_audio_policy(mpAudioPolicyDev, mpAudioPolicy);
Glenn Kasten6e2ebe92013-08-13 09:14:51 -0700130 }
131 if (mpAudioPolicyDev != NULL) {
Dima Zavinfce7a472011-04-19 22:30:36 -0700132 audio_policy_dev_close(mpAudioPolicyDev);
Glenn Kasten6e2ebe92013-08-13 09:14:51 -0700133 }
Eric Laurentdce54a12014-03-10 12:19:46 -0700134#else
Eric Laurentf269b8e2014-06-09 20:01:29 -0700135 destroyAudioPolicyManager(mAudioPolicyManager);
Eric Laurentdce54a12014-03-10 12:19:46 -0700136 delete mAudioPolicyClient;
137#endif
Eric Laurentb52c1522014-05-20 11:27:36 -0700138
139 mNotificationClients.clear();
bryant_liuba2b4392014-06-11 16:49:30 +0800140 mAudioPolicyEffects.clear();
Eric Laurentb52c1522014-05-20 11:27:36 -0700141}
142
143// A notification client is always registered by AudioSystem when the client process
144// connects to AudioPolicyService.
145void AudioPolicyService::registerClient(const sp<IAudioPolicyServiceClient>& client)
146{
147
148 Mutex::Autolock _l(mLock);
149
150 uid_t uid = IPCThreadState::self()->getCallingUid();
151 if (mNotificationClients.indexOfKey(uid) < 0) {
152 sp<NotificationClient> notificationClient = new NotificationClient(this,
153 client,
154 uid);
155 ALOGV("registerClient() client %p, uid %d", client.get(), uid);
156
157 mNotificationClients.add(uid, notificationClient);
158
159 sp<IBinder> binder = client->asBinder();
160 binder->linkToDeath(notificationClient);
161 }
162}
163
164// removeNotificationClient() is called when the client process dies.
165void AudioPolicyService::removeNotificationClient(uid_t uid)
166{
167 Mutex::Autolock _l(mLock);
168
169 mNotificationClients.removeItem(uid);
170
171#ifndef USE_LEGACY_AUDIO_POLICY
172 if (mAudioPolicyManager) {
173 mAudioPolicyManager->clearAudioPatches(uid);
174 }
175#endif
176}
177
178void AudioPolicyService::onAudioPortListUpdate()
179{
180 mOutputCommandThread->updateAudioPortListCommand();
181}
182
183void AudioPolicyService::doOnAudioPortListUpdate()
184{
185 Mutex::Autolock _l(mLock);
186 for (size_t i = 0; i < mNotificationClients.size(); i++) {
187 mNotificationClients.valueAt(i)->onAudioPortListUpdate();
188 }
189}
190
191void AudioPolicyService::onAudioPatchListUpdate()
192{
193 mOutputCommandThread->updateAudioPatchListCommand();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700194}
195
Eric Laurent951f4552014-05-20 10:48:17 -0700196status_t AudioPolicyService::clientCreateAudioPatch(const struct audio_patch *patch,
197 audio_patch_handle_t *handle,
198 int delayMs)
199{
200 return mAudioCommandThread->createAudioPatchCommand(patch, handle, delayMs);
201}
202
203status_t AudioPolicyService::clientReleaseAudioPatch(audio_patch_handle_t handle,
204 int delayMs)
205{
206 return mAudioCommandThread->releaseAudioPatchCommand(handle, delayMs);
207}
208
Eric Laurentb52c1522014-05-20 11:27:36 -0700209void AudioPolicyService::doOnAudioPatchListUpdate()
210{
211 Mutex::Autolock _l(mLock);
212 for (size_t i = 0; i < mNotificationClients.size(); i++) {
213 mNotificationClients.valueAt(i)->onAudioPatchListUpdate();
214 }
215}
216
Eric Laurente1715a42014-05-20 11:30:42 -0700217status_t AudioPolicyService::clientSetAudioPortConfig(const struct audio_port_config *config,
218 int delayMs)
219{
220 return mAudioCommandThread->setAudioPortConfigCommand(config, delayMs);
221}
222
Eric Laurentb52c1522014-05-20 11:27:36 -0700223AudioPolicyService::NotificationClient::NotificationClient(const sp<AudioPolicyService>& service,
224 const sp<IAudioPolicyServiceClient>& client,
225 uid_t uid)
226 : mService(service), mUid(uid), mAudioPolicyServiceClient(client)
227{
228}
229
230AudioPolicyService::NotificationClient::~NotificationClient()
231{
232}
233
234void AudioPolicyService::NotificationClient::binderDied(const wp<IBinder>& who __unused)
235{
236 sp<NotificationClient> keep(this);
237 sp<AudioPolicyService> service = mService.promote();
238 if (service != 0) {
239 service->removeNotificationClient(mUid);
240 }
241}
242
243void AudioPolicyService::NotificationClient::onAudioPortListUpdate()
244{
245 if (mAudioPolicyServiceClient != 0) {
246 mAudioPolicyServiceClient->onAudioPortListUpdate();
247 }
248}
249
250void AudioPolicyService::NotificationClient::onAudioPatchListUpdate()
251{
252 if (mAudioPolicyServiceClient != 0) {
253 mAudioPolicyServiceClient->onAudioPatchListUpdate();
254 }
255}
Eric Laurent57dae992011-07-24 13:36:09 -0700256
Mathias Agopian65ab4712010-07-14 17:59:35 -0700257void AudioPolicyService::binderDied(const wp<IBinder>& who) {
Glenn Kasten411e4472012-11-02 10:00:06 -0700258 ALOGW("binderDied() %p, calling pid %d", who.unsafe_get(),
Eric Laurentde070132010-07-13 04:45:46 -0700259 IPCThreadState::self()->getCallingPid());
Mathias Agopian65ab4712010-07-14 17:59:35 -0700260}
261
262static bool tryLock(Mutex& mutex)
263{
264 bool locked = false;
265 for (int i = 0; i < kDumpLockRetries; ++i) {
266 if (mutex.tryLock() == NO_ERROR) {
267 locked = true;
268 break;
269 }
Glenn Kasten22ecc912012-01-09 08:33:38 -0800270 usleep(kDumpLockSleepUs);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700271 }
272 return locked;
273}
274
275status_t AudioPolicyService::dumpInternals(int fd)
276{
277 const size_t SIZE = 256;
278 char buffer[SIZE];
279 String8 result;
280
Eric Laurentdce54a12014-03-10 12:19:46 -0700281#ifdef USE_LEGACY_AUDIO_POLICY
Dima Zavinfce7a472011-04-19 22:30:36 -0700282 snprintf(buffer, SIZE, "PolicyManager Interface: %p\n", mpAudioPolicy);
Eric Laurentdce54a12014-03-10 12:19:46 -0700283#else
284 snprintf(buffer, SIZE, "AudioPolicyManager: %p\n", mAudioPolicyManager);
285#endif
Mathias Agopian65ab4712010-07-14 17:59:35 -0700286 result.append(buffer);
287 snprintf(buffer, SIZE, "Command Thread: %p\n", mAudioCommandThread.get());
288 result.append(buffer);
289 snprintf(buffer, SIZE, "Tones Thread: %p\n", mTonePlaybackThread.get());
290 result.append(buffer);
291
292 write(fd, result.string(), result.size());
293 return NO_ERROR;
294}
295
Glenn Kasten0f11b512014-01-31 16:18:54 -0800296status_t AudioPolicyService::dump(int fd, const Vector<String16>& args __unused)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700297{
Glenn Kasten44deb052012-02-05 18:09:08 -0800298 if (!dumpAllowed()) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700299 dumpPermissionDenial(fd);
300 } else {
301 bool locked = tryLock(mLock);
302 if (!locked) {
303 String8 result(kDeadlockedString);
304 write(fd, result.string(), result.size());
305 }
306
307 dumpInternals(fd);
Glenn Kasten9d1f02d2012-02-08 17:47:58 -0800308 if (mAudioCommandThread != 0) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700309 mAudioCommandThread->dump(fd);
310 }
Glenn Kasten9d1f02d2012-02-08 17:47:58 -0800311 if (mTonePlaybackThread != 0) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700312 mTonePlaybackThread->dump(fd);
313 }
314
Eric Laurentdce54a12014-03-10 12:19:46 -0700315#ifdef USE_LEGACY_AUDIO_POLICY
Dima Zavinfce7a472011-04-19 22:30:36 -0700316 if (mpAudioPolicy) {
317 mpAudioPolicy->dump(mpAudioPolicy, fd);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700318 }
Eric Laurentdce54a12014-03-10 12:19:46 -0700319#else
320 if (mAudioPolicyManager) {
321 mAudioPolicyManager->dump(fd);
322 }
323#endif
Mathias Agopian65ab4712010-07-14 17:59:35 -0700324
325 if (locked) mLock.unlock();
326 }
327 return NO_ERROR;
328}
329
330status_t AudioPolicyService::dumpPermissionDenial(int fd)
331{
332 const size_t SIZE = 256;
333 char buffer[SIZE];
334 String8 result;
335 snprintf(buffer, SIZE, "Permission Denial: "
336 "can't dump AudioPolicyService from pid=%d, uid=%d\n",
337 IPCThreadState::self()->getCallingPid(),
338 IPCThreadState::self()->getCallingUid());
339 result.append(buffer);
340 write(fd, result.string(), result.size());
341 return NO_ERROR;
342}
343
344status_t AudioPolicyService::onTransact(
345 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
346{
347 return BnAudioPolicyService::onTransact(code, data, reply, flags);
348}
349
350
Mathias Agopian65ab4712010-07-14 17:59:35 -0700351// ----------- AudioPolicyService::AudioCommandThread implementation ----------
352
Eric Laurentbfb1b832013-01-07 09:53:42 -0800353AudioPolicyService::AudioCommandThread::AudioCommandThread(String8 name,
354 const wp<AudioPolicyService>& service)
355 : Thread(false), mName(name), mService(service)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700356{
357 mpToneGenerator = NULL;
358}
359
360
361AudioPolicyService::AudioCommandThread::~AudioCommandThread()
362{
Eric Laurentbfb1b832013-01-07 09:53:42 -0800363 if (!mAudioCommands.isEmpty()) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700364 release_wake_lock(mName.string());
365 }
366 mAudioCommands.clear();
Glenn Kastene9dd0172012-01-27 18:08:45 -0800367 delete mpToneGenerator;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700368}
369
370void AudioPolicyService::AudioCommandThread::onFirstRef()
371{
Eric Laurentbfb1b832013-01-07 09:53:42 -0800372 run(mName.string(), ANDROID_PRIORITY_AUDIO);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700373}
374
375bool AudioPolicyService::AudioCommandThread::threadLoop()
376{
377 nsecs_t waitTime = INT64_MAX;
378
379 mLock.lock();
380 while (!exitPending())
381 {
Eric Laurent59a89232014-06-08 14:14:17 -0700382 sp<AudioPolicyService> svc;
383 while (!mAudioCommands.isEmpty() && !exitPending()) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700384 nsecs_t curTime = systemTime();
385 // commands are sorted by increasing time stamp: execute them from index 0 and up
386 if (mAudioCommands[0]->mTime <= curTime) {
Eric Laurent0ede8922014-05-09 18:04:42 -0700387 sp<AudioCommand> command = mAudioCommands[0];
Mathias Agopian65ab4712010-07-14 17:59:35 -0700388 mAudioCommands.removeAt(0);
Eric Laurent0ede8922014-05-09 18:04:42 -0700389 mLastCommand = command;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700390
391 switch (command->mCommand) {
392 case START_TONE: {
393 mLock.unlock();
Eric Laurent0ede8922014-05-09 18:04:42 -0700394 ToneData *data = (ToneData *)command->mParam.get();
Steve Block3856b092011-10-20 11:56:00 +0100395 ALOGV("AudioCommandThread() processing start tone %d on stream %d",
Mathias Agopian65ab4712010-07-14 17:59:35 -0700396 data->mType, data->mStream);
Glenn Kastene9dd0172012-01-27 18:08:45 -0800397 delete mpToneGenerator;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700398 mpToneGenerator = new ToneGenerator(data->mStream, 1.0);
399 mpToneGenerator->startTone(data->mType);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700400 mLock.lock();
401 }break;
402 case STOP_TONE: {
403 mLock.unlock();
Steve Block3856b092011-10-20 11:56:00 +0100404 ALOGV("AudioCommandThread() processing stop tone");
Mathias Agopian65ab4712010-07-14 17:59:35 -0700405 if (mpToneGenerator != NULL) {
406 mpToneGenerator->stopTone();
407 delete mpToneGenerator;
408 mpToneGenerator = NULL;
409 }
410 mLock.lock();
411 }break;
412 case SET_VOLUME: {
Eric Laurent0ede8922014-05-09 18:04:42 -0700413 VolumeData *data = (VolumeData *)command->mParam.get();
Steve Block3856b092011-10-20 11:56:00 +0100414 ALOGV("AudioCommandThread() processing set volume stream %d, \
Eric Laurentde070132010-07-13 04:45:46 -0700415 volume %f, output %d", data->mStream, data->mVolume, data->mIO);
416 command->mStatus = AudioSystem::setStreamVolume(data->mStream,
417 data->mVolume,
418 data->mIO);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700419 }break;
420 case SET_PARAMETERS: {
Eric Laurent0ede8922014-05-09 18:04:42 -0700421 ParametersData *data = (ParametersData *)command->mParam.get();
Glenn Kastene53b9ea2012-03-12 16:29:55 -0700422 ALOGV("AudioCommandThread() processing set parameters string %s, io %d",
423 data->mKeyValuePairs.string(), data->mIO);
424 command->mStatus = AudioSystem::setParameters(data->mIO, data->mKeyValuePairs);
Glenn Kastene53b9ea2012-03-12 16:29:55 -0700425 }break;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700426 case SET_VOICE_VOLUME: {
Eric Laurent0ede8922014-05-09 18:04:42 -0700427 VoiceVolumeData *data = (VoiceVolumeData *)command->mParam.get();
Steve Block3856b092011-10-20 11:56:00 +0100428 ALOGV("AudioCommandThread() processing set voice volume volume %f",
Eric Laurentde070132010-07-13 04:45:46 -0700429 data->mVolume);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700430 command->mStatus = AudioSystem::setVoiceVolume(data->mVolume);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700431 }break;
Eric Laurentbfb1b832013-01-07 09:53:42 -0800432 case STOP_OUTPUT: {
Eric Laurent0ede8922014-05-09 18:04:42 -0700433 StopOutputData *data = (StopOutputData *)command->mParam.get();
Eric Laurentbfb1b832013-01-07 09:53:42 -0800434 ALOGV("AudioCommandThread() processing stop output %d",
435 data->mIO);
Eric Laurent59a89232014-06-08 14:14:17 -0700436 svc = mService.promote();
Eric Laurentbfb1b832013-01-07 09:53:42 -0800437 if (svc == 0) {
438 break;
439 }
440 mLock.unlock();
441 svc->doStopOutput(data->mIO, data->mStream, data->mSession);
442 mLock.lock();
Eric Laurentbfb1b832013-01-07 09:53:42 -0800443 }break;
444 case RELEASE_OUTPUT: {
Eric Laurent0ede8922014-05-09 18:04:42 -0700445 ReleaseOutputData *data = (ReleaseOutputData *)command->mParam.get();
Eric Laurentbfb1b832013-01-07 09:53:42 -0800446 ALOGV("AudioCommandThread() processing release output %d",
447 data->mIO);
Eric Laurent59a89232014-06-08 14:14:17 -0700448 svc = mService.promote();
Eric Laurentbfb1b832013-01-07 09:53:42 -0800449 if (svc == 0) {
450 break;
451 }
452 mLock.unlock();
453 svc->doReleaseOutput(data->mIO);
454 mLock.lock();
Eric Laurentbfb1b832013-01-07 09:53:42 -0800455 }break;
Eric Laurent951f4552014-05-20 10:48:17 -0700456 case CREATE_AUDIO_PATCH: {
457 CreateAudioPatchData *data = (CreateAudioPatchData *)command->mParam.get();
458 ALOGV("AudioCommandThread() processing create audio patch");
459 sp<IAudioFlinger> af = AudioSystem::get_audio_flinger();
460 if (af == 0) {
461 command->mStatus = PERMISSION_DENIED;
462 } else {
463 command->mStatus = af->createAudioPatch(&data->mPatch, &data->mHandle);
464 }
465 } break;
466 case RELEASE_AUDIO_PATCH: {
467 ReleaseAudioPatchData *data = (ReleaseAudioPatchData *)command->mParam.get();
468 ALOGV("AudioCommandThread() processing release audio patch");
469 sp<IAudioFlinger> af = AudioSystem::get_audio_flinger();
470 if (af == 0) {
471 command->mStatus = PERMISSION_DENIED;
472 } else {
473 command->mStatus = af->releaseAudioPatch(data->mHandle);
474 }
475 } break;
Eric Laurentb52c1522014-05-20 11:27:36 -0700476 case UPDATE_AUDIOPORT_LIST: {
477 ALOGV("AudioCommandThread() processing update audio port list");
Eric Laurent59a89232014-06-08 14:14:17 -0700478 svc = mService.promote();
Eric Laurentb52c1522014-05-20 11:27:36 -0700479 if (svc == 0) {
480 break;
481 }
482 mLock.unlock();
483 svc->doOnAudioPortListUpdate();
484 mLock.lock();
485 }break;
486 case UPDATE_AUDIOPATCH_LIST: {
487 ALOGV("AudioCommandThread() processing update audio patch list");
Eric Laurent59a89232014-06-08 14:14:17 -0700488 svc = mService.promote();
Eric Laurentb52c1522014-05-20 11:27:36 -0700489 if (svc == 0) {
490 break;
491 }
492 mLock.unlock();
493 svc->doOnAudioPatchListUpdate();
494 mLock.lock();
495 }break;
Eric Laurente1715a42014-05-20 11:30:42 -0700496 case SET_AUDIOPORT_CONFIG: {
497 SetAudioPortConfigData *data = (SetAudioPortConfigData *)command->mParam.get();
498 ALOGV("AudioCommandThread() processing set port config");
499 sp<IAudioFlinger> af = AudioSystem::get_audio_flinger();
500 if (af == 0) {
501 command->mStatus = PERMISSION_DENIED;
502 } else {
503 command->mStatus = af->setAudioPortConfig(&data->mConfig);
504 }
505 } break;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700506 default:
Steve Block5ff1dd52012-01-05 23:22:43 +0000507 ALOGW("AudioCommandThread() unknown command %d", command->mCommand);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700508 }
Eric Laurent0ede8922014-05-09 18:04:42 -0700509 {
510 Mutex::Autolock _l(command->mLock);
511 if (command->mWaitStatus) {
512 command->mWaitStatus = false;
513 command->mCond.signal();
514 }
515 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700516 waitTime = INT64_MAX;
517 } else {
518 waitTime = mAudioCommands[0]->mTime - curTime;
519 break;
520 }
521 }
Eric Laurent59a89232014-06-08 14:14:17 -0700522 // release mLock before releasing strong reference on the service as
523 // AudioPolicyService destructor calls AudioCommandThread::exit() which acquires mLock.
524 mLock.unlock();
525 svc.clear();
526 mLock.lock();
Ricardo Garcia05f2fdc2014-07-24 15:48:24 -0700527 if (!exitPending() && mAudioCommands.isEmpty()) {
528 // release delayed commands wake lock
529 release_wake_lock(mName.string());
Eric Laurent59a89232014-06-08 14:14:17 -0700530 ALOGV("AudioCommandThread() going to sleep");
531 mWaitWorkCV.waitRelative(mLock, waitTime);
532 ALOGV("AudioCommandThread() waking up");
533 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700534 }
Ricardo Garcia05f2fdc2014-07-24 15:48:24 -0700535 // release delayed commands wake lock before quitting
536 if (!mAudioCommands.isEmpty()) {
537 release_wake_lock(mName.string());
538 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700539 mLock.unlock();
540 return false;
541}
542
543status_t AudioPolicyService::AudioCommandThread::dump(int fd)
544{
545 const size_t SIZE = 256;
546 char buffer[SIZE];
547 String8 result;
548
549 snprintf(buffer, SIZE, "AudioCommandThread %p Dump\n", this);
550 result.append(buffer);
551 write(fd, result.string(), result.size());
552
553 bool locked = tryLock(mLock);
554 if (!locked) {
555 String8 result2(kCmdDeadlockedString);
556 write(fd, result2.string(), result2.size());
557 }
558
559 snprintf(buffer, SIZE, "- Commands:\n");
560 result = String8(buffer);
561 result.append(" Command Time Wait pParam\n");
Glenn Kasten8d6a2442012-02-08 14:04:28 -0800562 for (size_t i = 0; i < mAudioCommands.size(); i++) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700563 mAudioCommands[i]->dump(buffer, SIZE);
564 result.append(buffer);
565 }
566 result.append(" Last Command\n");
Eric Laurent0ede8922014-05-09 18:04:42 -0700567 if (mLastCommand != 0) {
568 mLastCommand->dump(buffer, SIZE);
569 result.append(buffer);
570 } else {
571 result.append(" none\n");
572 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700573
574 write(fd, result.string(), result.size());
575
576 if (locked) mLock.unlock();
577
578 return NO_ERROR;
579}
580
Glenn Kasten3d2f8772012-01-27 15:25:25 -0800581void AudioPolicyService::AudioCommandThread::startToneCommand(ToneGenerator::tone_type type,
582 audio_stream_type_t stream)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700583{
Eric Laurent0ede8922014-05-09 18:04:42 -0700584 sp<AudioCommand> command = new AudioCommand();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700585 command->mCommand = START_TONE;
Eric Laurent0ede8922014-05-09 18:04:42 -0700586 sp<ToneData> data = new ToneData();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700587 data->mType = type;
588 data->mStream = stream;
Jesper Tragardh48412dc2014-03-24 14:12:43 +0100589 command->mParam = data;
Steve Block3856b092011-10-20 11:56:00 +0100590 ALOGV("AudioCommandThread() adding tone start type %d, stream %d", type, stream);
Eric Laurent0ede8922014-05-09 18:04:42 -0700591 sendCommand(command);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700592}
593
594void AudioPolicyService::AudioCommandThread::stopToneCommand()
595{
Eric Laurent0ede8922014-05-09 18:04:42 -0700596 sp<AudioCommand> command = new AudioCommand();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700597 command->mCommand = STOP_TONE;
Steve Block3856b092011-10-20 11:56:00 +0100598 ALOGV("AudioCommandThread() adding tone stop");
Eric Laurent0ede8922014-05-09 18:04:42 -0700599 sendCommand(command);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700600}
601
Glenn Kastenfff6d712012-01-12 16:38:12 -0800602status_t AudioPolicyService::AudioCommandThread::volumeCommand(audio_stream_type_t stream,
Eric Laurentde070132010-07-13 04:45:46 -0700603 float volume,
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800604 audio_io_handle_t output,
Eric Laurentde070132010-07-13 04:45:46 -0700605 int delayMs)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700606{
Eric Laurent0ede8922014-05-09 18:04:42 -0700607 sp<AudioCommand> command = new AudioCommand();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700608 command->mCommand = SET_VOLUME;
Eric Laurent0ede8922014-05-09 18:04:42 -0700609 sp<VolumeData> data = new VolumeData();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700610 data->mStream = stream;
611 data->mVolume = volume;
612 data->mIO = output;
613 command->mParam = data;
Eric Laurent0ede8922014-05-09 18:04:42 -0700614 command->mWaitStatus = true;
Steve Block3856b092011-10-20 11:56:00 +0100615 ALOGV("AudioCommandThread() adding set volume stream %d, volume %f, output %d",
Eric Laurentde070132010-07-13 04:45:46 -0700616 stream, volume, output);
Eric Laurent0ede8922014-05-09 18:04:42 -0700617 return sendCommand(command, delayMs);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700618}
619
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800620status_t AudioPolicyService::AudioCommandThread::parametersCommand(audio_io_handle_t ioHandle,
Dima Zavinfce7a472011-04-19 22:30:36 -0700621 const char *keyValuePairs,
Eric Laurentde070132010-07-13 04:45:46 -0700622 int delayMs)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700623{
Eric Laurent0ede8922014-05-09 18:04:42 -0700624 sp<AudioCommand> command = new AudioCommand();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700625 command->mCommand = SET_PARAMETERS;
Eric Laurent0ede8922014-05-09 18:04:42 -0700626 sp<ParametersData> data = new ParametersData();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700627 data->mIO = ioHandle;
Dima Zavinfce7a472011-04-19 22:30:36 -0700628 data->mKeyValuePairs = String8(keyValuePairs);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700629 command->mParam = data;
Eric Laurent0ede8922014-05-09 18:04:42 -0700630 command->mWaitStatus = true;
Steve Block3856b092011-10-20 11:56:00 +0100631 ALOGV("AudioCommandThread() adding set parameter string %s, io %d ,delay %d",
Dima Zavinfce7a472011-04-19 22:30:36 -0700632 keyValuePairs, ioHandle, delayMs);
Eric Laurent0ede8922014-05-09 18:04:42 -0700633 return sendCommand(command, delayMs);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700634}
635
636status_t AudioPolicyService::AudioCommandThread::voiceVolumeCommand(float volume, int delayMs)
637{
Eric Laurent0ede8922014-05-09 18:04:42 -0700638 sp<AudioCommand> command = new AudioCommand();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700639 command->mCommand = SET_VOICE_VOLUME;
Eric Laurent0ede8922014-05-09 18:04:42 -0700640 sp<VoiceVolumeData> data = new VoiceVolumeData();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700641 data->mVolume = volume;
642 command->mParam = data;
Eric Laurent0ede8922014-05-09 18:04:42 -0700643 command->mWaitStatus = true;
Steve Block3856b092011-10-20 11:56:00 +0100644 ALOGV("AudioCommandThread() adding set voice volume volume %f", volume);
Eric Laurent0ede8922014-05-09 18:04:42 -0700645 return sendCommand(command, delayMs);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700646}
647
Eric Laurentbfb1b832013-01-07 09:53:42 -0800648void AudioPolicyService::AudioCommandThread::stopOutputCommand(audio_io_handle_t output,
649 audio_stream_type_t stream,
650 int session)
651{
Eric Laurent0ede8922014-05-09 18:04:42 -0700652 sp<AudioCommand> command = new AudioCommand();
Eric Laurentbfb1b832013-01-07 09:53:42 -0800653 command->mCommand = STOP_OUTPUT;
Eric Laurent0ede8922014-05-09 18:04:42 -0700654 sp<StopOutputData> data = new StopOutputData();
Eric Laurentbfb1b832013-01-07 09:53:42 -0800655 data->mIO = output;
656 data->mStream = stream;
657 data->mSession = session;
Jesper Tragardh48412dc2014-03-24 14:12:43 +0100658 command->mParam = data;
Eric Laurentbfb1b832013-01-07 09:53:42 -0800659 ALOGV("AudioCommandThread() adding stop output %d", output);
Eric Laurent0ede8922014-05-09 18:04:42 -0700660 sendCommand(command);
Eric Laurentbfb1b832013-01-07 09:53:42 -0800661}
662
663void AudioPolicyService::AudioCommandThread::releaseOutputCommand(audio_io_handle_t output)
664{
Eric Laurent0ede8922014-05-09 18:04:42 -0700665 sp<AudioCommand> command = new AudioCommand();
Eric Laurentbfb1b832013-01-07 09:53:42 -0800666 command->mCommand = RELEASE_OUTPUT;
Eric Laurent0ede8922014-05-09 18:04:42 -0700667 sp<ReleaseOutputData> data = new ReleaseOutputData();
Eric Laurentbfb1b832013-01-07 09:53:42 -0800668 data->mIO = output;
Jesper Tragardh48412dc2014-03-24 14:12:43 +0100669 command->mParam = data;
Eric Laurentbfb1b832013-01-07 09:53:42 -0800670 ALOGV("AudioCommandThread() adding release output %d", output);
Eric Laurent0ede8922014-05-09 18:04:42 -0700671 sendCommand(command);
672}
673
Eric Laurent951f4552014-05-20 10:48:17 -0700674status_t AudioPolicyService::AudioCommandThread::createAudioPatchCommand(
675 const struct audio_patch *patch,
676 audio_patch_handle_t *handle,
677 int delayMs)
678{
679 status_t status = NO_ERROR;
680
681 sp<AudioCommand> command = new AudioCommand();
682 command->mCommand = CREATE_AUDIO_PATCH;
683 CreateAudioPatchData *data = new CreateAudioPatchData();
684 data->mPatch = *patch;
685 data->mHandle = *handle;
686 command->mParam = data;
687 command->mWaitStatus = true;
688 ALOGV("AudioCommandThread() adding create patch delay %d", delayMs);
689 status = sendCommand(command, delayMs);
690 if (status == NO_ERROR) {
691 *handle = data->mHandle;
692 }
693 return status;
694}
695
696status_t AudioPolicyService::AudioCommandThread::releaseAudioPatchCommand(audio_patch_handle_t handle,
697 int delayMs)
698{
699 sp<AudioCommand> command = new AudioCommand();
700 command->mCommand = RELEASE_AUDIO_PATCH;
701 ReleaseAudioPatchData *data = new ReleaseAudioPatchData();
702 data->mHandle = handle;
703 command->mParam = data;
704 command->mWaitStatus = true;
705 ALOGV("AudioCommandThread() adding release patch delay %d", delayMs);
706 return sendCommand(command, delayMs);
707}
708
Eric Laurentb52c1522014-05-20 11:27:36 -0700709void AudioPolicyService::AudioCommandThread::updateAudioPortListCommand()
710{
711 sp<AudioCommand> command = new AudioCommand();
712 command->mCommand = UPDATE_AUDIOPORT_LIST;
713 ALOGV("AudioCommandThread() adding update audio port list");
714 sendCommand(command);
715}
716
717void AudioPolicyService::AudioCommandThread::updateAudioPatchListCommand()
718{
719 sp<AudioCommand>command = new AudioCommand();
720 command->mCommand = UPDATE_AUDIOPATCH_LIST;
721 ALOGV("AudioCommandThread() adding update audio patch list");
722 sendCommand(command);
723}
724
Eric Laurente1715a42014-05-20 11:30:42 -0700725status_t AudioPolicyService::AudioCommandThread::setAudioPortConfigCommand(
726 const struct audio_port_config *config, int delayMs)
727{
728 sp<AudioCommand> command = new AudioCommand();
729 command->mCommand = SET_AUDIOPORT_CONFIG;
730 SetAudioPortConfigData *data = new SetAudioPortConfigData();
731 data->mConfig = *config;
732 command->mParam = data;
733 command->mWaitStatus = true;
734 ALOGV("AudioCommandThread() adding set port config delay %d", delayMs);
735 return sendCommand(command, delayMs);
736}
737
Eric Laurent0ede8922014-05-09 18:04:42 -0700738status_t AudioPolicyService::AudioCommandThread::sendCommand(sp<AudioCommand>& command, int delayMs)
739{
740 {
741 Mutex::Autolock _l(mLock);
742 insertCommand_l(command, delayMs);
743 mWaitWorkCV.signal();
744 }
745 Mutex::Autolock _l(command->mLock);
746 while (command->mWaitStatus) {
747 nsecs_t timeOutNs = kAudioCommandTimeoutNs + milliseconds(delayMs);
748 if (command->mCond.waitRelative(command->mLock, timeOutNs) != NO_ERROR) {
749 command->mStatus = TIMED_OUT;
750 command->mWaitStatus = false;
751 }
752 }
753 return command->mStatus;
Eric Laurentbfb1b832013-01-07 09:53:42 -0800754}
755
Mathias Agopian65ab4712010-07-14 17:59:35 -0700756// insertCommand_l() must be called with mLock held
Eric Laurent0ede8922014-05-09 18:04:42 -0700757void AudioPolicyService::AudioCommandThread::insertCommand_l(sp<AudioCommand>& command, int delayMs)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700758{
Glenn Kasten8d6a2442012-02-08 14:04:28 -0800759 ssize_t i; // not size_t because i will count down to -1
Eric Laurent0ede8922014-05-09 18:04:42 -0700760 Vector < sp<AudioCommand> > removedCommands;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700761 command->mTime = systemTime() + milliseconds(delayMs);
762
763 // acquire wake lock to make sure delayed commands are processed
Eric Laurentbfb1b832013-01-07 09:53:42 -0800764 if (mAudioCommands.isEmpty()) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700765 acquire_wake_lock(PARTIAL_WAKE_LOCK, mName.string());
766 }
767
768 // check same pending commands with later time stamps and eliminate them
769 for (i = mAudioCommands.size()-1; i >= 0; i--) {
Eric Laurent0ede8922014-05-09 18:04:42 -0700770 sp<AudioCommand> command2 = mAudioCommands[i];
Mathias Agopian65ab4712010-07-14 17:59:35 -0700771 // commands are sorted by increasing time stamp: no need to scan the rest of mAudioCommands
772 if (command2->mTime <= command->mTime) break;
Eric Laurente45b48a2014-09-04 16:40:57 -0700773
774 // create audio patch or release audio patch commands are equivalent
775 // with regard to filtering
776 if ((command->mCommand == CREATE_AUDIO_PATCH) ||
777 (command->mCommand == RELEASE_AUDIO_PATCH)) {
778 if ((command2->mCommand != CREATE_AUDIO_PATCH) &&
779 (command2->mCommand != RELEASE_AUDIO_PATCH)) {
780 continue;
781 }
782 } else if (command2->mCommand != command->mCommand) continue;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700783
784 switch (command->mCommand) {
785 case SET_PARAMETERS: {
Eric Laurent0ede8922014-05-09 18:04:42 -0700786 ParametersData *data = (ParametersData *)command->mParam.get();
787 ParametersData *data2 = (ParametersData *)command2->mParam.get();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700788 if (data->mIO != data2->mIO) break;
Steve Block3856b092011-10-20 11:56:00 +0100789 ALOGV("Comparing parameter command %s to new command %s",
Eric Laurentde070132010-07-13 04:45:46 -0700790 data2->mKeyValuePairs.string(), data->mKeyValuePairs.string());
Mathias Agopian65ab4712010-07-14 17:59:35 -0700791 AudioParameter param = AudioParameter(data->mKeyValuePairs);
792 AudioParameter param2 = AudioParameter(data2->mKeyValuePairs);
793 for (size_t j = 0; j < param.size(); j++) {
Glenn Kastene53b9ea2012-03-12 16:29:55 -0700794 String8 key;
795 String8 value;
796 param.getAt(j, key, value);
797 for (size_t k = 0; k < param2.size(); k++) {
798 String8 key2;
799 String8 value2;
800 param2.getAt(k, key2, value2);
801 if (key2 == key) {
802 param2.remove(key2);
803 ALOGV("Filtering out parameter %s", key2.string());
804 break;
805 }
806 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700807 }
808 // if all keys have been filtered out, remove the command.
809 // otherwise, update the key value pairs
810 if (param2.size() == 0) {
811 removedCommands.add(command2);
812 } else {
813 data2->mKeyValuePairs = param2.toString();
814 }
Eric Laurent21e54562013-09-23 12:08:05 -0700815 command->mTime = command2->mTime;
816 // force delayMs to non 0 so that code below does not request to wait for
817 // command status as the command is now delayed
818 delayMs = 1;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700819 } break;
820
821 case SET_VOLUME: {
Eric Laurent0ede8922014-05-09 18:04:42 -0700822 VolumeData *data = (VolumeData *)command->mParam.get();
823 VolumeData *data2 = (VolumeData *)command2->mParam.get();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700824 if (data->mIO != data2->mIO) break;
825 if (data->mStream != data2->mStream) break;
Steve Block3856b092011-10-20 11:56:00 +0100826 ALOGV("Filtering out volume command on output %d for stream %d",
Eric Laurentde070132010-07-13 04:45:46 -0700827 data->mIO, data->mStream);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700828 removedCommands.add(command2);
Eric Laurent21e54562013-09-23 12:08:05 -0700829 command->mTime = command2->mTime;
830 // force delayMs to non 0 so that code below does not request to wait for
831 // command status as the command is now delayed
832 delayMs = 1;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700833 } break;
Eric Laurente45b48a2014-09-04 16:40:57 -0700834
835 case CREATE_AUDIO_PATCH:
836 case RELEASE_AUDIO_PATCH: {
837 audio_patch_handle_t handle;
838 if (command->mCommand == CREATE_AUDIO_PATCH) {
839 handle = ((CreateAudioPatchData *)command->mParam.get())->mHandle;
840 } else {
841 handle = ((ReleaseAudioPatchData *)command->mParam.get())->mHandle;
842 }
843 audio_patch_handle_t handle2;
844 if (command2->mCommand == CREATE_AUDIO_PATCH) {
845 handle2 = ((CreateAudioPatchData *)command2->mParam.get())->mHandle;
846 } else {
847 handle2 = ((ReleaseAudioPatchData *)command2->mParam.get())->mHandle;
848 }
849 if (handle != handle2) break;
850 ALOGV("Filtering out %s audio patch command for handle %d",
851 (command->mCommand == CREATE_AUDIO_PATCH) ? "create" : "release", handle);
852 removedCommands.add(command2);
853 command->mTime = command2->mTime;
854 // force delayMs to non 0 so that code below does not request to wait for
855 // command status as the command is now delayed
856 delayMs = 1;
857 } break;
858
Mathias Agopian65ab4712010-07-14 17:59:35 -0700859 case START_TONE:
860 case STOP_TONE:
861 default:
862 break;
863 }
864 }
865
866 // remove filtered commands
867 for (size_t j = 0; j < removedCommands.size(); j++) {
868 // removed commands always have time stamps greater than current command
869 for (size_t k = i + 1; k < mAudioCommands.size(); k++) {
Eric Laurent0ede8922014-05-09 18:04:42 -0700870 if (mAudioCommands[k].get() == removedCommands[j].get()) {
Steve Block3856b092011-10-20 11:56:00 +0100871 ALOGV("suppressing command: %d", mAudioCommands[k]->mCommand);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700872 mAudioCommands.removeAt(k);
873 break;
874 }
875 }
876 }
877 removedCommands.clear();
878
Eric Laurent0ede8922014-05-09 18:04:42 -0700879 // Disable wait for status if delay is not 0
880 if (delayMs != 0) {
Eric Laurentcec4abb2012-07-03 12:23:02 -0700881 command->mWaitStatus = false;
882 }
Eric Laurentcec4abb2012-07-03 12:23:02 -0700883
Mathias Agopian65ab4712010-07-14 17:59:35 -0700884 // insert command at the right place according to its time stamp
Eric Laurent1e693b52014-07-09 15:03:28 -0700885 ALOGV("inserting command: %d at index %zd, num commands %zu",
886 command->mCommand, i+1, mAudioCommands.size());
Mathias Agopian65ab4712010-07-14 17:59:35 -0700887 mAudioCommands.insertAt(command, i + 1);
888}
889
890void AudioPolicyService::AudioCommandThread::exit()
891{
Steve Block3856b092011-10-20 11:56:00 +0100892 ALOGV("AudioCommandThread::exit");
Mathias Agopian65ab4712010-07-14 17:59:35 -0700893 {
894 AutoMutex _l(mLock);
895 requestExit();
896 mWaitWorkCV.signal();
897 }
898 requestExitAndWait();
899}
900
901void AudioPolicyService::AudioCommandThread::AudioCommand::dump(char* buffer, size_t size)
902{
903 snprintf(buffer, size, " %02d %06d.%03d %01u %p\n",
904 mCommand,
905 (int)ns2s(mTime),
906 (int)ns2ms(mTime)%1000,
907 mWaitStatus,
Eric Laurent0ede8922014-05-09 18:04:42 -0700908 mParam.get());
Mathias Agopian65ab4712010-07-14 17:59:35 -0700909}
910
Dima Zavinfce7a472011-04-19 22:30:36 -0700911/******* helpers for the service_ops callbacks defined below *********/
912void AudioPolicyService::setParameters(audio_io_handle_t ioHandle,
913 const char *keyValuePairs,
914 int delayMs)
915{
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800916 mAudioCommandThread->parametersCommand(ioHandle, keyValuePairs,
Dima Zavinfce7a472011-04-19 22:30:36 -0700917 delayMs);
918}
919
920int AudioPolicyService::setStreamVolume(audio_stream_type_t stream,
921 float volume,
922 audio_io_handle_t output,
923 int delayMs)
924{
Glenn Kastenfff6d712012-01-12 16:38:12 -0800925 return (int)mAudioCommandThread->volumeCommand(stream, volume,
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800926 output, delayMs);
Dima Zavinfce7a472011-04-19 22:30:36 -0700927}
928
929int AudioPolicyService::startTone(audio_policy_tone_t tone,
930 audio_stream_type_t stream)
931{
Glenn Kasten6e2ebe92013-08-13 09:14:51 -0700932 if (tone != AUDIO_POLICY_TONE_IN_CALL_NOTIFICATION) {
Steve Block29357bc2012-01-06 19:20:56 +0000933 ALOGE("startTone: illegal tone requested (%d)", tone);
Glenn Kasten6e2ebe92013-08-13 09:14:51 -0700934 }
935 if (stream != AUDIO_STREAM_VOICE_CALL) {
Steve Block29357bc2012-01-06 19:20:56 +0000936 ALOGE("startTone: illegal stream (%d) requested for tone %d", stream,
Glenn Kastene53b9ea2012-03-12 16:29:55 -0700937 tone);
Glenn Kasten6e2ebe92013-08-13 09:14:51 -0700938 }
Dima Zavinfce7a472011-04-19 22:30:36 -0700939 mTonePlaybackThread->startToneCommand(ToneGenerator::TONE_SUP_CALL_WAITING,
940 AUDIO_STREAM_VOICE_CALL);
941 return 0;
942}
943
944int AudioPolicyService::stopTone()
945{
946 mTonePlaybackThread->stopToneCommand();
947 return 0;
948}
949
950int AudioPolicyService::setVoiceVolume(float volume, int delayMs)
951{
952 return (int)mAudioCommandThread->voiceVolumeCommand(volume, delayMs);
953}
954
Dima Zavinfce7a472011-04-19 22:30:36 -0700955extern "C" {
Eric Laurent2d388ec2014-03-07 13:25:54 -0800956audio_module_handle_t aps_load_hw_module(void *service __unused,
957 const char *name);
958audio_io_handle_t aps_open_output(void *service __unused,
Eric Laurenta4c5a552012-03-29 10:12:40 -0700959 audio_devices_t *pDevices,
960 uint32_t *pSamplingRate,
961 audio_format_t *pFormat,
962 audio_channel_mask_t *pChannelMask,
963 uint32_t *pLatencyMs,
Eric Laurent2d388ec2014-03-07 13:25:54 -0800964 audio_output_flags_t flags);
Eric Laurenta4c5a552012-03-29 10:12:40 -0700965
Eric Laurent2d388ec2014-03-07 13:25:54 -0800966audio_io_handle_t aps_open_output_on_module(void *service __unused,
Eric Laurenta4c5a552012-03-29 10:12:40 -0700967 audio_module_handle_t module,
968 audio_devices_t *pDevices,
969 uint32_t *pSamplingRate,
970 audio_format_t *pFormat,
971 audio_channel_mask_t *pChannelMask,
972 uint32_t *pLatencyMs,
Richard Fitzgeraldad3af332013-03-25 16:54:37 +0000973 audio_output_flags_t flags,
Eric Laurent2d388ec2014-03-07 13:25:54 -0800974 const audio_offload_info_t *offloadInfo);
975audio_io_handle_t aps_open_dup_output(void *service __unused,
Dima Zavinfce7a472011-04-19 22:30:36 -0700976 audio_io_handle_t output1,
Eric Laurent2d388ec2014-03-07 13:25:54 -0800977 audio_io_handle_t output2);
978int aps_close_output(void *service __unused, audio_io_handle_t output);
979int aps_suspend_output(void *service __unused, audio_io_handle_t output);
980int aps_restore_output(void *service __unused, audio_io_handle_t output);
981audio_io_handle_t aps_open_input(void *service __unused,
Eric Laurenta4c5a552012-03-29 10:12:40 -0700982 audio_devices_t *pDevices,
983 uint32_t *pSamplingRate,
984 audio_format_t *pFormat,
985 audio_channel_mask_t *pChannelMask,
Eric Laurent2d388ec2014-03-07 13:25:54 -0800986 audio_in_acoustics_t acoustics __unused);
987audio_io_handle_t aps_open_input_on_module(void *service __unused,
Eric Laurenta4c5a552012-03-29 10:12:40 -0700988 audio_module_handle_t module,
989 audio_devices_t *pDevices,
990 uint32_t *pSamplingRate,
991 audio_format_t *pFormat,
Eric Laurent2d388ec2014-03-07 13:25:54 -0800992 audio_channel_mask_t *pChannelMask);
993int aps_close_input(void *service __unused, audio_io_handle_t input);
994int aps_invalidate_stream(void *service __unused, audio_stream_type_t stream);
995int aps_move_effects(void *service __unused, int session,
Dima Zavinfce7a472011-04-19 22:30:36 -0700996 audio_io_handle_t src_output,
Eric Laurent2d388ec2014-03-07 13:25:54 -0800997 audio_io_handle_t dst_output);
998char * aps_get_parameters(void *service __unused, audio_io_handle_t io_handle,
999 const char *keys);
1000void aps_set_parameters(void *service, audio_io_handle_t io_handle,
1001 const char *kv_pairs, int delay_ms);
1002int aps_set_stream_volume(void *service, audio_stream_type_t stream,
Dima Zavinfce7a472011-04-19 22:30:36 -07001003 float volume, audio_io_handle_t output,
Eric Laurent2d388ec2014-03-07 13:25:54 -08001004 int delay_ms);
1005int aps_start_tone(void *service, audio_policy_tone_t tone,
1006 audio_stream_type_t stream);
1007int aps_stop_tone(void *service);
1008int aps_set_voice_volume(void *service, float volume, int delay_ms);
1009};
Dima Zavinfce7a472011-04-19 22:30:36 -07001010
1011namespace {
1012 struct audio_policy_service_ops aps_ops = {
Glenn Kasten01d3acb2014-02-06 08:24:07 -08001013 .open_output = aps_open_output,
1014 .open_duplicate_output = aps_open_dup_output,
1015 .close_output = aps_close_output,
1016 .suspend_output = aps_suspend_output,
1017 .restore_output = aps_restore_output,
1018 .open_input = aps_open_input,
1019 .close_input = aps_close_input,
1020 .set_stream_volume = aps_set_stream_volume,
Glenn Kastend2304db2014-02-03 07:40:31 -08001021 .invalidate_stream = aps_invalidate_stream,
Glenn Kasten01d3acb2014-02-06 08:24:07 -08001022 .set_parameters = aps_set_parameters,
1023 .get_parameters = aps_get_parameters,
1024 .start_tone = aps_start_tone,
1025 .stop_tone = aps_stop_tone,
1026 .set_voice_volume = aps_set_voice_volume,
1027 .move_effects = aps_move_effects,
1028 .load_hw_module = aps_load_hw_module,
1029 .open_output_on_module = aps_open_output_on_module,
1030 .open_input_on_module = aps_open_input_on_module,
Dima Zavinfce7a472011-04-19 22:30:36 -07001031 };
1032}; // namespace <unnamed>
1033
Mathias Agopian65ab4712010-07-14 17:59:35 -07001034}; // namespace android