blob: 06a7e84e36afc52c8b9b78b617964e7249b9bc61 [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
152 Mutex::Autolock _l(mLock);
153
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{
171 Mutex::Autolock _l(mLock);
172
173 mNotificationClients.removeItem(uid);
174
175#ifndef USE_LEGACY_AUDIO_POLICY
176 if (mAudioPolicyManager) {
177 mAudioPolicyManager->clearAudioPatches(uid);
178 }
179#endif
180}
181
182void AudioPolicyService::onAudioPortListUpdate()
183{
184 mOutputCommandThread->updateAudioPortListCommand();
185}
186
187void AudioPolicyService::doOnAudioPortListUpdate()
188{
189 Mutex::Autolock _l(mLock);
190 for (size_t i = 0; i < mNotificationClients.size(); i++) {
191 mNotificationClients.valueAt(i)->onAudioPortListUpdate();
192 }
193}
194
195void AudioPolicyService::onAudioPatchListUpdate()
196{
197 mOutputCommandThread->updateAudioPatchListCommand();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700198}
199
Eric Laurent951f4552014-05-20 10:48:17 -0700200status_t AudioPolicyService::clientCreateAudioPatch(const struct audio_patch *patch,
201 audio_patch_handle_t *handle,
202 int delayMs)
203{
204 return mAudioCommandThread->createAudioPatchCommand(patch, handle, delayMs);
205}
206
207status_t AudioPolicyService::clientReleaseAudioPatch(audio_patch_handle_t handle,
208 int delayMs)
209{
210 return mAudioCommandThread->releaseAudioPatchCommand(handle, delayMs);
211}
212
Eric Laurentb52c1522014-05-20 11:27:36 -0700213void AudioPolicyService::doOnAudioPatchListUpdate()
214{
215 Mutex::Autolock _l(mLock);
216 for (size_t i = 0; i < mNotificationClients.size(); i++) {
217 mNotificationClients.valueAt(i)->onAudioPatchListUpdate();
218 }
219}
220
Eric Laurente1715a42014-05-20 11:30:42 -0700221status_t AudioPolicyService::clientSetAudioPortConfig(const struct audio_port_config *config,
222 int delayMs)
223{
224 return mAudioCommandThread->setAudioPortConfigCommand(config, delayMs);
225}
226
Eric Laurentb52c1522014-05-20 11:27:36 -0700227AudioPolicyService::NotificationClient::NotificationClient(const sp<AudioPolicyService>& service,
228 const sp<IAudioPolicyServiceClient>& client,
229 uid_t uid)
230 : mService(service), mUid(uid), mAudioPolicyServiceClient(client)
231{
232}
233
234AudioPolicyService::NotificationClient::~NotificationClient()
235{
236}
237
238void AudioPolicyService::NotificationClient::binderDied(const wp<IBinder>& who __unused)
239{
240 sp<NotificationClient> keep(this);
241 sp<AudioPolicyService> service = mService.promote();
242 if (service != 0) {
243 service->removeNotificationClient(mUid);
244 }
245}
246
247void AudioPolicyService::NotificationClient::onAudioPortListUpdate()
248{
249 if (mAudioPolicyServiceClient != 0) {
250 mAudioPolicyServiceClient->onAudioPortListUpdate();
251 }
252}
253
254void AudioPolicyService::NotificationClient::onAudioPatchListUpdate()
255{
256 if (mAudioPolicyServiceClient != 0) {
257 mAudioPolicyServiceClient->onAudioPatchListUpdate();
258 }
259}
Eric Laurent57dae992011-07-24 13:36:09 -0700260
Mathias Agopian65ab4712010-07-14 17:59:35 -0700261void AudioPolicyService::binderDied(const wp<IBinder>& who) {
Glenn Kasten411e4472012-11-02 10:00:06 -0700262 ALOGW("binderDied() %p, calling pid %d", who.unsafe_get(),
Eric Laurentde070132010-07-13 04:45:46 -0700263 IPCThreadState::self()->getCallingPid());
Mathias Agopian65ab4712010-07-14 17:59:35 -0700264}
265
266static bool tryLock(Mutex& mutex)
267{
268 bool locked = false;
269 for (int i = 0; i < kDumpLockRetries; ++i) {
270 if (mutex.tryLock() == NO_ERROR) {
271 locked = true;
272 break;
273 }
Glenn Kasten22ecc912012-01-09 08:33:38 -0800274 usleep(kDumpLockSleepUs);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700275 }
276 return locked;
277}
278
279status_t AudioPolicyService::dumpInternals(int fd)
280{
281 const size_t SIZE = 256;
282 char buffer[SIZE];
283 String8 result;
284
Eric Laurentdce54a12014-03-10 12:19:46 -0700285#ifdef USE_LEGACY_AUDIO_POLICY
Dima Zavinfce7a472011-04-19 22:30:36 -0700286 snprintf(buffer, SIZE, "PolicyManager Interface: %p\n", mpAudioPolicy);
Eric Laurentdce54a12014-03-10 12:19:46 -0700287#else
288 snprintf(buffer, SIZE, "AudioPolicyManager: %p\n", mAudioPolicyManager);
289#endif
Mathias Agopian65ab4712010-07-14 17:59:35 -0700290 result.append(buffer);
291 snprintf(buffer, SIZE, "Command Thread: %p\n", mAudioCommandThread.get());
292 result.append(buffer);
293 snprintf(buffer, SIZE, "Tones Thread: %p\n", mTonePlaybackThread.get());
294 result.append(buffer);
295
296 write(fd, result.string(), result.size());
297 return NO_ERROR;
298}
299
Glenn Kasten0f11b512014-01-31 16:18:54 -0800300status_t AudioPolicyService::dump(int fd, const Vector<String16>& args __unused)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700301{
Glenn Kasten44deb052012-02-05 18:09:08 -0800302 if (!dumpAllowed()) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700303 dumpPermissionDenial(fd);
304 } else {
305 bool locked = tryLock(mLock);
306 if (!locked) {
307 String8 result(kDeadlockedString);
308 write(fd, result.string(), result.size());
309 }
310
311 dumpInternals(fd);
Glenn Kasten9d1f02d2012-02-08 17:47:58 -0800312 if (mAudioCommandThread != 0) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700313 mAudioCommandThread->dump(fd);
314 }
Glenn Kasten9d1f02d2012-02-08 17:47:58 -0800315 if (mTonePlaybackThread != 0) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700316 mTonePlaybackThread->dump(fd);
317 }
318
Eric Laurentdce54a12014-03-10 12:19:46 -0700319#ifdef USE_LEGACY_AUDIO_POLICY
Dima Zavinfce7a472011-04-19 22:30:36 -0700320 if (mpAudioPolicy) {
321 mpAudioPolicy->dump(mpAudioPolicy, fd);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700322 }
Eric Laurentdce54a12014-03-10 12:19:46 -0700323#else
324 if (mAudioPolicyManager) {
325 mAudioPolicyManager->dump(fd);
326 }
327#endif
Mathias Agopian65ab4712010-07-14 17:59:35 -0700328
329 if (locked) mLock.unlock();
330 }
331 return NO_ERROR;
332}
333
334status_t AudioPolicyService::dumpPermissionDenial(int fd)
335{
336 const size_t SIZE = 256;
337 char buffer[SIZE];
338 String8 result;
339 snprintf(buffer, SIZE, "Permission Denial: "
340 "can't dump AudioPolicyService from pid=%d, uid=%d\n",
341 IPCThreadState::self()->getCallingPid(),
342 IPCThreadState::self()->getCallingUid());
343 result.append(buffer);
344 write(fd, result.string(), result.size());
345 return NO_ERROR;
346}
347
348status_t AudioPolicyService::onTransact(
349 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
350{
351 return BnAudioPolicyService::onTransact(code, data, reply, flags);
352}
353
354
Mathias Agopian65ab4712010-07-14 17:59:35 -0700355// ----------- AudioPolicyService::AudioCommandThread implementation ----------
356
Eric Laurentbfb1b832013-01-07 09:53:42 -0800357AudioPolicyService::AudioCommandThread::AudioCommandThread(String8 name,
358 const wp<AudioPolicyService>& service)
359 : Thread(false), mName(name), mService(service)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700360{
361 mpToneGenerator = NULL;
362}
363
364
365AudioPolicyService::AudioCommandThread::~AudioCommandThread()
366{
Eric Laurentbfb1b832013-01-07 09:53:42 -0800367 if (!mAudioCommands.isEmpty()) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700368 release_wake_lock(mName.string());
369 }
370 mAudioCommands.clear();
Glenn Kastene9dd0172012-01-27 18:08:45 -0800371 delete mpToneGenerator;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700372}
373
374void AudioPolicyService::AudioCommandThread::onFirstRef()
375{
Eric Laurentbfb1b832013-01-07 09:53:42 -0800376 run(mName.string(), ANDROID_PRIORITY_AUDIO);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700377}
378
379bool AudioPolicyService::AudioCommandThread::threadLoop()
380{
381 nsecs_t waitTime = INT64_MAX;
382
383 mLock.lock();
384 while (!exitPending())
385 {
Eric Laurent59a89232014-06-08 14:14:17 -0700386 sp<AudioPolicyService> svc;
387 while (!mAudioCommands.isEmpty() && !exitPending()) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700388 nsecs_t curTime = systemTime();
389 // commands are sorted by increasing time stamp: execute them from index 0 and up
390 if (mAudioCommands[0]->mTime <= curTime) {
Eric Laurent0ede8922014-05-09 18:04:42 -0700391 sp<AudioCommand> command = mAudioCommands[0];
Mathias Agopian65ab4712010-07-14 17:59:35 -0700392 mAudioCommands.removeAt(0);
Eric Laurent0ede8922014-05-09 18:04:42 -0700393 mLastCommand = command;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700394
395 switch (command->mCommand) {
396 case START_TONE: {
397 mLock.unlock();
Eric Laurent0ede8922014-05-09 18:04:42 -0700398 ToneData *data = (ToneData *)command->mParam.get();
Steve Block3856b092011-10-20 11:56:00 +0100399 ALOGV("AudioCommandThread() processing start tone %d on stream %d",
Mathias Agopian65ab4712010-07-14 17:59:35 -0700400 data->mType, data->mStream);
Glenn Kastene9dd0172012-01-27 18:08:45 -0800401 delete mpToneGenerator;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700402 mpToneGenerator = new ToneGenerator(data->mStream, 1.0);
403 mpToneGenerator->startTone(data->mType);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700404 mLock.lock();
405 }break;
406 case STOP_TONE: {
407 mLock.unlock();
Steve Block3856b092011-10-20 11:56:00 +0100408 ALOGV("AudioCommandThread() processing stop tone");
Mathias Agopian65ab4712010-07-14 17:59:35 -0700409 if (mpToneGenerator != NULL) {
410 mpToneGenerator->stopTone();
411 delete mpToneGenerator;
412 mpToneGenerator = NULL;
413 }
414 mLock.lock();
415 }break;
416 case SET_VOLUME: {
Eric Laurent0ede8922014-05-09 18:04:42 -0700417 VolumeData *data = (VolumeData *)command->mParam.get();
Steve Block3856b092011-10-20 11:56:00 +0100418 ALOGV("AudioCommandThread() processing set volume stream %d, \
Eric Laurentde070132010-07-13 04:45:46 -0700419 volume %f, output %d", data->mStream, data->mVolume, data->mIO);
420 command->mStatus = AudioSystem::setStreamVolume(data->mStream,
421 data->mVolume,
422 data->mIO);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700423 }break;
424 case SET_PARAMETERS: {
Eric Laurent0ede8922014-05-09 18:04:42 -0700425 ParametersData *data = (ParametersData *)command->mParam.get();
Glenn Kastene53b9ea2012-03-12 16:29:55 -0700426 ALOGV("AudioCommandThread() processing set parameters string %s, io %d",
427 data->mKeyValuePairs.string(), data->mIO);
428 command->mStatus = AudioSystem::setParameters(data->mIO, data->mKeyValuePairs);
Glenn Kastene53b9ea2012-03-12 16:29:55 -0700429 }break;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700430 case SET_VOICE_VOLUME: {
Eric Laurent0ede8922014-05-09 18:04:42 -0700431 VoiceVolumeData *data = (VoiceVolumeData *)command->mParam.get();
Steve Block3856b092011-10-20 11:56:00 +0100432 ALOGV("AudioCommandThread() processing set voice volume volume %f",
Eric Laurentde070132010-07-13 04:45:46 -0700433 data->mVolume);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700434 command->mStatus = AudioSystem::setVoiceVolume(data->mVolume);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700435 }break;
Eric Laurentbfb1b832013-01-07 09:53:42 -0800436 case STOP_OUTPUT: {
Eric Laurent0ede8922014-05-09 18:04:42 -0700437 StopOutputData *data = (StopOutputData *)command->mParam.get();
Eric Laurentbfb1b832013-01-07 09:53:42 -0800438 ALOGV("AudioCommandThread() processing stop output %d",
439 data->mIO);
Eric Laurent59a89232014-06-08 14:14:17 -0700440 svc = mService.promote();
Eric Laurentbfb1b832013-01-07 09:53:42 -0800441 if (svc == 0) {
442 break;
443 }
444 mLock.unlock();
445 svc->doStopOutput(data->mIO, data->mStream, data->mSession);
446 mLock.lock();
Eric Laurentbfb1b832013-01-07 09:53:42 -0800447 }break;
448 case RELEASE_OUTPUT: {
Eric Laurent0ede8922014-05-09 18:04:42 -0700449 ReleaseOutputData *data = (ReleaseOutputData *)command->mParam.get();
Eric Laurentbfb1b832013-01-07 09:53:42 -0800450 ALOGV("AudioCommandThread() processing release output %d",
451 data->mIO);
Eric Laurent59a89232014-06-08 14:14:17 -0700452 svc = mService.promote();
Eric Laurentbfb1b832013-01-07 09:53:42 -0800453 if (svc == 0) {
454 break;
455 }
456 mLock.unlock();
457 svc->doReleaseOutput(data->mIO);
458 mLock.lock();
Eric Laurentbfb1b832013-01-07 09:53:42 -0800459 }break;
Eric Laurent951f4552014-05-20 10:48:17 -0700460 case CREATE_AUDIO_PATCH: {
461 CreateAudioPatchData *data = (CreateAudioPatchData *)command->mParam.get();
462 ALOGV("AudioCommandThread() processing create audio patch");
463 sp<IAudioFlinger> af = AudioSystem::get_audio_flinger();
464 if (af == 0) {
465 command->mStatus = PERMISSION_DENIED;
466 } else {
467 command->mStatus = af->createAudioPatch(&data->mPatch, &data->mHandle);
468 }
469 } break;
470 case RELEASE_AUDIO_PATCH: {
471 ReleaseAudioPatchData *data = (ReleaseAudioPatchData *)command->mParam.get();
472 ALOGV("AudioCommandThread() processing release audio patch");
473 sp<IAudioFlinger> af = AudioSystem::get_audio_flinger();
474 if (af == 0) {
475 command->mStatus = PERMISSION_DENIED;
476 } else {
477 command->mStatus = af->releaseAudioPatch(data->mHandle);
478 }
479 } break;
Eric Laurentb52c1522014-05-20 11:27:36 -0700480 case UPDATE_AUDIOPORT_LIST: {
481 ALOGV("AudioCommandThread() processing update audio port list");
Eric Laurent59a89232014-06-08 14:14:17 -0700482 svc = mService.promote();
Eric Laurentb52c1522014-05-20 11:27:36 -0700483 if (svc == 0) {
484 break;
485 }
486 mLock.unlock();
487 svc->doOnAudioPortListUpdate();
488 mLock.lock();
489 }break;
490 case UPDATE_AUDIOPATCH_LIST: {
491 ALOGV("AudioCommandThread() processing update audio patch list");
Eric Laurent59a89232014-06-08 14:14:17 -0700492 svc = mService.promote();
Eric Laurentb52c1522014-05-20 11:27:36 -0700493 if (svc == 0) {
494 break;
495 }
496 mLock.unlock();
497 svc->doOnAudioPatchListUpdate();
498 mLock.lock();
499 }break;
Eric Laurente1715a42014-05-20 11:30:42 -0700500 case SET_AUDIOPORT_CONFIG: {
501 SetAudioPortConfigData *data = (SetAudioPortConfigData *)command->mParam.get();
502 ALOGV("AudioCommandThread() processing set port config");
503 sp<IAudioFlinger> af = AudioSystem::get_audio_flinger();
504 if (af == 0) {
505 command->mStatus = PERMISSION_DENIED;
506 } else {
507 command->mStatus = af->setAudioPortConfig(&data->mConfig);
508 }
509 } break;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700510 default:
Steve Block5ff1dd52012-01-05 23:22:43 +0000511 ALOGW("AudioCommandThread() unknown command %d", command->mCommand);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700512 }
Eric Laurent0ede8922014-05-09 18:04:42 -0700513 {
514 Mutex::Autolock _l(command->mLock);
515 if (command->mWaitStatus) {
516 command->mWaitStatus = false;
517 command->mCond.signal();
518 }
519 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700520 waitTime = INT64_MAX;
521 } else {
522 waitTime = mAudioCommands[0]->mTime - curTime;
523 break;
524 }
525 }
Eric Laurent59a89232014-06-08 14:14:17 -0700526 // release mLock before releasing strong reference on the service as
527 // AudioPolicyService destructor calls AudioCommandThread::exit() which acquires mLock.
528 mLock.unlock();
529 svc.clear();
530 mLock.lock();
Ricardo Garcia05f2fdc2014-07-24 15:48:24 -0700531 if (!exitPending() && mAudioCommands.isEmpty()) {
532 // release delayed commands wake lock
533 release_wake_lock(mName.string());
Eric Laurent59a89232014-06-08 14:14:17 -0700534 ALOGV("AudioCommandThread() going to sleep");
535 mWaitWorkCV.waitRelative(mLock, waitTime);
536 ALOGV("AudioCommandThread() waking up");
537 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700538 }
Ricardo Garcia05f2fdc2014-07-24 15:48:24 -0700539 // release delayed commands wake lock before quitting
540 if (!mAudioCommands.isEmpty()) {
541 release_wake_lock(mName.string());
542 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700543 mLock.unlock();
544 return false;
545}
546
547status_t AudioPolicyService::AudioCommandThread::dump(int fd)
548{
549 const size_t SIZE = 256;
550 char buffer[SIZE];
551 String8 result;
552
553 snprintf(buffer, SIZE, "AudioCommandThread %p Dump\n", this);
554 result.append(buffer);
555 write(fd, result.string(), result.size());
556
557 bool locked = tryLock(mLock);
558 if (!locked) {
559 String8 result2(kCmdDeadlockedString);
560 write(fd, result2.string(), result2.size());
561 }
562
563 snprintf(buffer, SIZE, "- Commands:\n");
564 result = String8(buffer);
565 result.append(" Command Time Wait pParam\n");
Glenn Kasten8d6a2442012-02-08 14:04:28 -0800566 for (size_t i = 0; i < mAudioCommands.size(); i++) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700567 mAudioCommands[i]->dump(buffer, SIZE);
568 result.append(buffer);
569 }
570 result.append(" Last Command\n");
Eric Laurent0ede8922014-05-09 18:04:42 -0700571 if (mLastCommand != 0) {
572 mLastCommand->dump(buffer, SIZE);
573 result.append(buffer);
574 } else {
575 result.append(" none\n");
576 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700577
578 write(fd, result.string(), result.size());
579
580 if (locked) mLock.unlock();
581
582 return NO_ERROR;
583}
584
Glenn Kasten3d2f8772012-01-27 15:25:25 -0800585void AudioPolicyService::AudioCommandThread::startToneCommand(ToneGenerator::tone_type type,
586 audio_stream_type_t stream)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700587{
Eric Laurent0ede8922014-05-09 18:04:42 -0700588 sp<AudioCommand> command = new AudioCommand();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700589 command->mCommand = START_TONE;
Eric Laurent0ede8922014-05-09 18:04:42 -0700590 sp<ToneData> data = new ToneData();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700591 data->mType = type;
592 data->mStream = stream;
Jesper Tragardh48412dc2014-03-24 14:12:43 +0100593 command->mParam = data;
Steve Block3856b092011-10-20 11:56:00 +0100594 ALOGV("AudioCommandThread() adding tone start type %d, stream %d", type, stream);
Eric Laurent0ede8922014-05-09 18:04:42 -0700595 sendCommand(command);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700596}
597
598void AudioPolicyService::AudioCommandThread::stopToneCommand()
599{
Eric Laurent0ede8922014-05-09 18:04:42 -0700600 sp<AudioCommand> command = new AudioCommand();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700601 command->mCommand = STOP_TONE;
Steve Block3856b092011-10-20 11:56:00 +0100602 ALOGV("AudioCommandThread() adding tone stop");
Eric Laurent0ede8922014-05-09 18:04:42 -0700603 sendCommand(command);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700604}
605
Glenn Kastenfff6d712012-01-12 16:38:12 -0800606status_t AudioPolicyService::AudioCommandThread::volumeCommand(audio_stream_type_t stream,
Eric Laurentde070132010-07-13 04:45:46 -0700607 float volume,
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800608 audio_io_handle_t output,
Eric Laurentde070132010-07-13 04:45:46 -0700609 int delayMs)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700610{
Eric Laurent0ede8922014-05-09 18:04:42 -0700611 sp<AudioCommand> command = new AudioCommand();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700612 command->mCommand = SET_VOLUME;
Eric Laurent0ede8922014-05-09 18:04:42 -0700613 sp<VolumeData> data = new VolumeData();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700614 data->mStream = stream;
615 data->mVolume = volume;
616 data->mIO = output;
617 command->mParam = data;
Eric Laurent0ede8922014-05-09 18:04:42 -0700618 command->mWaitStatus = true;
Steve Block3856b092011-10-20 11:56:00 +0100619 ALOGV("AudioCommandThread() adding set volume stream %d, volume %f, output %d",
Eric Laurentde070132010-07-13 04:45:46 -0700620 stream, volume, output);
Eric Laurent0ede8922014-05-09 18:04:42 -0700621 return sendCommand(command, delayMs);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700622}
623
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800624status_t AudioPolicyService::AudioCommandThread::parametersCommand(audio_io_handle_t ioHandle,
Dima Zavinfce7a472011-04-19 22:30:36 -0700625 const char *keyValuePairs,
Eric Laurentde070132010-07-13 04:45:46 -0700626 int delayMs)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700627{
Eric Laurent0ede8922014-05-09 18:04:42 -0700628 sp<AudioCommand> command = new AudioCommand();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700629 command->mCommand = SET_PARAMETERS;
Eric Laurent0ede8922014-05-09 18:04:42 -0700630 sp<ParametersData> data = new ParametersData();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700631 data->mIO = ioHandle;
Dima Zavinfce7a472011-04-19 22:30:36 -0700632 data->mKeyValuePairs = String8(keyValuePairs);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700633 command->mParam = data;
Eric Laurent0ede8922014-05-09 18:04:42 -0700634 command->mWaitStatus = true;
Steve Block3856b092011-10-20 11:56:00 +0100635 ALOGV("AudioCommandThread() adding set parameter string %s, io %d ,delay %d",
Dima Zavinfce7a472011-04-19 22:30:36 -0700636 keyValuePairs, ioHandle, delayMs);
Eric Laurent0ede8922014-05-09 18:04:42 -0700637 return sendCommand(command, delayMs);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700638}
639
640status_t AudioPolicyService::AudioCommandThread::voiceVolumeCommand(float volume, int delayMs)
641{
Eric Laurent0ede8922014-05-09 18:04:42 -0700642 sp<AudioCommand> command = new AudioCommand();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700643 command->mCommand = SET_VOICE_VOLUME;
Eric Laurent0ede8922014-05-09 18:04:42 -0700644 sp<VoiceVolumeData> data = new VoiceVolumeData();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700645 data->mVolume = volume;
646 command->mParam = data;
Eric Laurent0ede8922014-05-09 18:04:42 -0700647 command->mWaitStatus = true;
Steve Block3856b092011-10-20 11:56:00 +0100648 ALOGV("AudioCommandThread() adding set voice volume volume %f", volume);
Eric Laurent0ede8922014-05-09 18:04:42 -0700649 return sendCommand(command, delayMs);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700650}
651
Eric Laurentbfb1b832013-01-07 09:53:42 -0800652void AudioPolicyService::AudioCommandThread::stopOutputCommand(audio_io_handle_t output,
653 audio_stream_type_t stream,
654 int session)
655{
Eric Laurent0ede8922014-05-09 18:04:42 -0700656 sp<AudioCommand> command = new AudioCommand();
Eric Laurentbfb1b832013-01-07 09:53:42 -0800657 command->mCommand = STOP_OUTPUT;
Eric Laurent0ede8922014-05-09 18:04:42 -0700658 sp<StopOutputData> data = new StopOutputData();
Eric Laurentbfb1b832013-01-07 09:53:42 -0800659 data->mIO = output;
660 data->mStream = stream;
661 data->mSession = session;
Jesper Tragardh48412dc2014-03-24 14:12:43 +0100662 command->mParam = data;
Eric Laurentbfb1b832013-01-07 09:53:42 -0800663 ALOGV("AudioCommandThread() adding stop output %d", output);
Eric Laurent0ede8922014-05-09 18:04:42 -0700664 sendCommand(command);
Eric Laurentbfb1b832013-01-07 09:53:42 -0800665}
666
667void AudioPolicyService::AudioCommandThread::releaseOutputCommand(audio_io_handle_t output)
668{
Eric Laurent0ede8922014-05-09 18:04:42 -0700669 sp<AudioCommand> command = new AudioCommand();
Eric Laurentbfb1b832013-01-07 09:53:42 -0800670 command->mCommand = RELEASE_OUTPUT;
Eric Laurent0ede8922014-05-09 18:04:42 -0700671 sp<ReleaseOutputData> data = new ReleaseOutputData();
Eric Laurentbfb1b832013-01-07 09:53:42 -0800672 data->mIO = output;
Jesper Tragardh48412dc2014-03-24 14:12:43 +0100673 command->mParam = data;
Eric Laurentbfb1b832013-01-07 09:53:42 -0800674 ALOGV("AudioCommandThread() adding release output %d", output);
Eric Laurent0ede8922014-05-09 18:04:42 -0700675 sendCommand(command);
676}
677
Eric Laurent951f4552014-05-20 10:48:17 -0700678status_t AudioPolicyService::AudioCommandThread::createAudioPatchCommand(
679 const struct audio_patch *patch,
680 audio_patch_handle_t *handle,
681 int delayMs)
682{
683 status_t status = NO_ERROR;
684
685 sp<AudioCommand> command = new AudioCommand();
686 command->mCommand = CREATE_AUDIO_PATCH;
687 CreateAudioPatchData *data = new CreateAudioPatchData();
688 data->mPatch = *patch;
689 data->mHandle = *handle;
690 command->mParam = data;
691 command->mWaitStatus = true;
692 ALOGV("AudioCommandThread() adding create patch delay %d", delayMs);
693 status = sendCommand(command, delayMs);
694 if (status == NO_ERROR) {
695 *handle = data->mHandle;
696 }
697 return status;
698}
699
700status_t AudioPolicyService::AudioCommandThread::releaseAudioPatchCommand(audio_patch_handle_t handle,
701 int delayMs)
702{
703 sp<AudioCommand> command = new AudioCommand();
704 command->mCommand = RELEASE_AUDIO_PATCH;
705 ReleaseAudioPatchData *data = new ReleaseAudioPatchData();
706 data->mHandle = handle;
707 command->mParam = data;
708 command->mWaitStatus = true;
709 ALOGV("AudioCommandThread() adding release patch delay %d", delayMs);
710 return sendCommand(command, delayMs);
711}
712
Eric Laurentb52c1522014-05-20 11:27:36 -0700713void AudioPolicyService::AudioCommandThread::updateAudioPortListCommand()
714{
715 sp<AudioCommand> command = new AudioCommand();
716 command->mCommand = UPDATE_AUDIOPORT_LIST;
717 ALOGV("AudioCommandThread() adding update audio port list");
718 sendCommand(command);
719}
720
721void AudioPolicyService::AudioCommandThread::updateAudioPatchListCommand()
722{
723 sp<AudioCommand>command = new AudioCommand();
724 command->mCommand = UPDATE_AUDIOPATCH_LIST;
725 ALOGV("AudioCommandThread() adding update audio patch list");
726 sendCommand(command);
727}
728
Eric Laurente1715a42014-05-20 11:30:42 -0700729status_t AudioPolicyService::AudioCommandThread::setAudioPortConfigCommand(
730 const struct audio_port_config *config, int delayMs)
731{
732 sp<AudioCommand> command = new AudioCommand();
733 command->mCommand = SET_AUDIOPORT_CONFIG;
734 SetAudioPortConfigData *data = new SetAudioPortConfigData();
735 data->mConfig = *config;
736 command->mParam = data;
737 command->mWaitStatus = true;
738 ALOGV("AudioCommandThread() adding set port config delay %d", delayMs);
739 return sendCommand(command, delayMs);
740}
741
Eric Laurent0ede8922014-05-09 18:04:42 -0700742status_t AudioPolicyService::AudioCommandThread::sendCommand(sp<AudioCommand>& command, int delayMs)
743{
744 {
745 Mutex::Autolock _l(mLock);
746 insertCommand_l(command, delayMs);
747 mWaitWorkCV.signal();
748 }
749 Mutex::Autolock _l(command->mLock);
750 while (command->mWaitStatus) {
751 nsecs_t timeOutNs = kAudioCommandTimeoutNs + milliseconds(delayMs);
752 if (command->mCond.waitRelative(command->mLock, timeOutNs) != NO_ERROR) {
753 command->mStatus = TIMED_OUT;
754 command->mWaitStatus = false;
755 }
756 }
757 return command->mStatus;
Eric Laurentbfb1b832013-01-07 09:53:42 -0800758}
759
Mathias Agopian65ab4712010-07-14 17:59:35 -0700760// insertCommand_l() must be called with mLock held
Eric Laurent0ede8922014-05-09 18:04:42 -0700761void AudioPolicyService::AudioCommandThread::insertCommand_l(sp<AudioCommand>& command, int delayMs)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700762{
Glenn Kasten8d6a2442012-02-08 14:04:28 -0800763 ssize_t i; // not size_t because i will count down to -1
Eric Laurent0ede8922014-05-09 18:04:42 -0700764 Vector < sp<AudioCommand> > removedCommands;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700765 command->mTime = systemTime() + milliseconds(delayMs);
766
767 // acquire wake lock to make sure delayed commands are processed
Eric Laurentbfb1b832013-01-07 09:53:42 -0800768 if (mAudioCommands.isEmpty()) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700769 acquire_wake_lock(PARTIAL_WAKE_LOCK, mName.string());
770 }
771
772 // check same pending commands with later time stamps and eliminate them
773 for (i = mAudioCommands.size()-1; i >= 0; i--) {
Eric Laurent0ede8922014-05-09 18:04:42 -0700774 sp<AudioCommand> command2 = mAudioCommands[i];
Mathias Agopian65ab4712010-07-14 17:59:35 -0700775 // commands are sorted by increasing time stamp: no need to scan the rest of mAudioCommands
776 if (command2->mTime <= command->mTime) break;
Eric Laurente45b48a2014-09-04 16:40:57 -0700777
778 // create audio patch or release audio patch commands are equivalent
779 // with regard to filtering
780 if ((command->mCommand == CREATE_AUDIO_PATCH) ||
781 (command->mCommand == RELEASE_AUDIO_PATCH)) {
782 if ((command2->mCommand != CREATE_AUDIO_PATCH) &&
783 (command2->mCommand != RELEASE_AUDIO_PATCH)) {
784 continue;
785 }
786 } else if (command2->mCommand != command->mCommand) continue;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700787
788 switch (command->mCommand) {
789 case SET_PARAMETERS: {
Eric Laurent0ede8922014-05-09 18:04:42 -0700790 ParametersData *data = (ParametersData *)command->mParam.get();
791 ParametersData *data2 = (ParametersData *)command2->mParam.get();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700792 if (data->mIO != data2->mIO) break;
Steve Block3856b092011-10-20 11:56:00 +0100793 ALOGV("Comparing parameter command %s to new command %s",
Eric Laurentde070132010-07-13 04:45:46 -0700794 data2->mKeyValuePairs.string(), data->mKeyValuePairs.string());
Mathias Agopian65ab4712010-07-14 17:59:35 -0700795 AudioParameter param = AudioParameter(data->mKeyValuePairs);
796 AudioParameter param2 = AudioParameter(data2->mKeyValuePairs);
797 for (size_t j = 0; j < param.size(); j++) {
Glenn Kastene53b9ea2012-03-12 16:29:55 -0700798 String8 key;
799 String8 value;
800 param.getAt(j, key, value);
801 for (size_t k = 0; k < param2.size(); k++) {
802 String8 key2;
803 String8 value2;
804 param2.getAt(k, key2, value2);
805 if (key2 == key) {
806 param2.remove(key2);
807 ALOGV("Filtering out parameter %s", key2.string());
808 break;
809 }
810 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700811 }
812 // if all keys have been filtered out, remove the command.
813 // otherwise, update the key value pairs
814 if (param2.size() == 0) {
815 removedCommands.add(command2);
816 } else {
817 data2->mKeyValuePairs = param2.toString();
818 }
Eric Laurent21e54562013-09-23 12:08:05 -0700819 command->mTime = command2->mTime;
820 // force delayMs to non 0 so that code below does not request to wait for
821 // command status as the command is now delayed
822 delayMs = 1;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700823 } break;
824
825 case SET_VOLUME: {
Eric Laurent0ede8922014-05-09 18:04:42 -0700826 VolumeData *data = (VolumeData *)command->mParam.get();
827 VolumeData *data2 = (VolumeData *)command2->mParam.get();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700828 if (data->mIO != data2->mIO) break;
829 if (data->mStream != data2->mStream) break;
Steve Block3856b092011-10-20 11:56:00 +0100830 ALOGV("Filtering out volume command on output %d for stream %d",
Eric Laurentde070132010-07-13 04:45:46 -0700831 data->mIO, data->mStream);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700832 removedCommands.add(command2);
Eric Laurent21e54562013-09-23 12:08:05 -0700833 command->mTime = command2->mTime;
834 // force delayMs to non 0 so that code below does not request to wait for
835 // command status as the command is now delayed
836 delayMs = 1;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700837 } break;
Eric Laurente45b48a2014-09-04 16:40:57 -0700838
839 case CREATE_AUDIO_PATCH:
840 case RELEASE_AUDIO_PATCH: {
841 audio_patch_handle_t handle;
842 if (command->mCommand == CREATE_AUDIO_PATCH) {
843 handle = ((CreateAudioPatchData *)command->mParam.get())->mHandle;
844 } else {
845 handle = ((ReleaseAudioPatchData *)command->mParam.get())->mHandle;
846 }
847 audio_patch_handle_t handle2;
848 if (command2->mCommand == CREATE_AUDIO_PATCH) {
849 handle2 = ((CreateAudioPatchData *)command2->mParam.get())->mHandle;
850 } else {
851 handle2 = ((ReleaseAudioPatchData *)command2->mParam.get())->mHandle;
852 }
853 if (handle != handle2) break;
854 ALOGV("Filtering out %s audio patch command for handle %d",
855 (command->mCommand == CREATE_AUDIO_PATCH) ? "create" : "release", handle);
856 removedCommands.add(command2);
857 command->mTime = command2->mTime;
858 // force delayMs to non 0 so that code below does not request to wait for
859 // command status as the command is now delayed
860 delayMs = 1;
861 } break;
862
Mathias Agopian65ab4712010-07-14 17:59:35 -0700863 case START_TONE:
864 case STOP_TONE:
865 default:
866 break;
867 }
868 }
869
870 // remove filtered commands
871 for (size_t j = 0; j < removedCommands.size(); j++) {
872 // removed commands always have time stamps greater than current command
873 for (size_t k = i + 1; k < mAudioCommands.size(); k++) {
Eric Laurent0ede8922014-05-09 18:04:42 -0700874 if (mAudioCommands[k].get() == removedCommands[j].get()) {
Steve Block3856b092011-10-20 11:56:00 +0100875 ALOGV("suppressing command: %d", mAudioCommands[k]->mCommand);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700876 mAudioCommands.removeAt(k);
877 break;
878 }
879 }
880 }
881 removedCommands.clear();
882
Eric Laurent0ede8922014-05-09 18:04:42 -0700883 // Disable wait for status if delay is not 0
884 if (delayMs != 0) {
Eric Laurentcec4abb2012-07-03 12:23:02 -0700885 command->mWaitStatus = false;
886 }
Eric Laurentcec4abb2012-07-03 12:23:02 -0700887
Mathias Agopian65ab4712010-07-14 17:59:35 -0700888 // insert command at the right place according to its time stamp
Eric Laurent1e693b52014-07-09 15:03:28 -0700889 ALOGV("inserting command: %d at index %zd, num commands %zu",
890 command->mCommand, i+1, mAudioCommands.size());
Mathias Agopian65ab4712010-07-14 17:59:35 -0700891 mAudioCommands.insertAt(command, i + 1);
892}
893
894void AudioPolicyService::AudioCommandThread::exit()
895{
Steve Block3856b092011-10-20 11:56:00 +0100896 ALOGV("AudioCommandThread::exit");
Mathias Agopian65ab4712010-07-14 17:59:35 -0700897 {
898 AutoMutex _l(mLock);
899 requestExit();
900 mWaitWorkCV.signal();
901 }
902 requestExitAndWait();
903}
904
905void AudioPolicyService::AudioCommandThread::AudioCommand::dump(char* buffer, size_t size)
906{
907 snprintf(buffer, size, " %02d %06d.%03d %01u %p\n",
908 mCommand,
909 (int)ns2s(mTime),
910 (int)ns2ms(mTime)%1000,
911 mWaitStatus,
Eric Laurent0ede8922014-05-09 18:04:42 -0700912 mParam.get());
Mathias Agopian65ab4712010-07-14 17:59:35 -0700913}
914
Dima Zavinfce7a472011-04-19 22:30:36 -0700915/******* helpers for the service_ops callbacks defined below *********/
916void AudioPolicyService::setParameters(audio_io_handle_t ioHandle,
917 const char *keyValuePairs,
918 int delayMs)
919{
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800920 mAudioCommandThread->parametersCommand(ioHandle, keyValuePairs,
Dima Zavinfce7a472011-04-19 22:30:36 -0700921 delayMs);
922}
923
924int AudioPolicyService::setStreamVolume(audio_stream_type_t stream,
925 float volume,
926 audio_io_handle_t output,
927 int delayMs)
928{
Glenn Kastenfff6d712012-01-12 16:38:12 -0800929 return (int)mAudioCommandThread->volumeCommand(stream, volume,
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800930 output, delayMs);
Dima Zavinfce7a472011-04-19 22:30:36 -0700931}
932
933int AudioPolicyService::startTone(audio_policy_tone_t tone,
934 audio_stream_type_t stream)
935{
Glenn Kasten6e2ebe92013-08-13 09:14:51 -0700936 if (tone != AUDIO_POLICY_TONE_IN_CALL_NOTIFICATION) {
Steve Block29357bc2012-01-06 19:20:56 +0000937 ALOGE("startTone: illegal tone requested (%d)", tone);
Glenn Kasten6e2ebe92013-08-13 09:14:51 -0700938 }
939 if (stream != AUDIO_STREAM_VOICE_CALL) {
Steve Block29357bc2012-01-06 19:20:56 +0000940 ALOGE("startTone: illegal stream (%d) requested for tone %d", stream,
Glenn Kastene53b9ea2012-03-12 16:29:55 -0700941 tone);
Glenn Kasten6e2ebe92013-08-13 09:14:51 -0700942 }
Dima Zavinfce7a472011-04-19 22:30:36 -0700943 mTonePlaybackThread->startToneCommand(ToneGenerator::TONE_SUP_CALL_WAITING,
944 AUDIO_STREAM_VOICE_CALL);
945 return 0;
946}
947
948int AudioPolicyService::stopTone()
949{
950 mTonePlaybackThread->stopToneCommand();
951 return 0;
952}
953
954int AudioPolicyService::setVoiceVolume(float volume, int delayMs)
955{
956 return (int)mAudioCommandThread->voiceVolumeCommand(volume, delayMs);
957}
958
Dima Zavinfce7a472011-04-19 22:30:36 -0700959extern "C" {
Eric Laurent2d388ec2014-03-07 13:25:54 -0800960audio_module_handle_t aps_load_hw_module(void *service __unused,
961 const char *name);
962audio_io_handle_t aps_open_output(void *service __unused,
Eric Laurenta4c5a552012-03-29 10:12:40 -0700963 audio_devices_t *pDevices,
964 uint32_t *pSamplingRate,
965 audio_format_t *pFormat,
966 audio_channel_mask_t *pChannelMask,
967 uint32_t *pLatencyMs,
Eric Laurent2d388ec2014-03-07 13:25:54 -0800968 audio_output_flags_t flags);
Eric Laurenta4c5a552012-03-29 10:12:40 -0700969
Eric Laurent2d388ec2014-03-07 13:25:54 -0800970audio_io_handle_t aps_open_output_on_module(void *service __unused,
Eric Laurenta4c5a552012-03-29 10:12:40 -0700971 audio_module_handle_t module,
972 audio_devices_t *pDevices,
973 uint32_t *pSamplingRate,
974 audio_format_t *pFormat,
975 audio_channel_mask_t *pChannelMask,
976 uint32_t *pLatencyMs,
Richard Fitzgeraldad3af332013-03-25 16:54:37 +0000977 audio_output_flags_t flags,
Eric Laurent2d388ec2014-03-07 13:25:54 -0800978 const audio_offload_info_t *offloadInfo);
979audio_io_handle_t aps_open_dup_output(void *service __unused,
Dima Zavinfce7a472011-04-19 22:30:36 -0700980 audio_io_handle_t output1,
Eric Laurent2d388ec2014-03-07 13:25:54 -0800981 audio_io_handle_t output2);
982int aps_close_output(void *service __unused, audio_io_handle_t output);
983int aps_suspend_output(void *service __unused, audio_io_handle_t output);
984int aps_restore_output(void *service __unused, audio_io_handle_t output);
985audio_io_handle_t aps_open_input(void *service __unused,
Eric Laurenta4c5a552012-03-29 10:12:40 -0700986 audio_devices_t *pDevices,
987 uint32_t *pSamplingRate,
988 audio_format_t *pFormat,
989 audio_channel_mask_t *pChannelMask,
Eric Laurent2d388ec2014-03-07 13:25:54 -0800990 audio_in_acoustics_t acoustics __unused);
991audio_io_handle_t aps_open_input_on_module(void *service __unused,
Eric Laurenta4c5a552012-03-29 10:12:40 -0700992 audio_module_handle_t module,
993 audio_devices_t *pDevices,
994 uint32_t *pSamplingRate,
995 audio_format_t *pFormat,
Eric Laurent2d388ec2014-03-07 13:25:54 -0800996 audio_channel_mask_t *pChannelMask);
997int aps_close_input(void *service __unused, audio_io_handle_t input);
998int aps_invalidate_stream(void *service __unused, audio_stream_type_t stream);
999int aps_move_effects(void *service __unused, int session,
Dima Zavinfce7a472011-04-19 22:30:36 -07001000 audio_io_handle_t src_output,
Eric Laurent2d388ec2014-03-07 13:25:54 -08001001 audio_io_handle_t dst_output);
1002char * aps_get_parameters(void *service __unused, audio_io_handle_t io_handle,
1003 const char *keys);
1004void aps_set_parameters(void *service, audio_io_handle_t io_handle,
1005 const char *kv_pairs, int delay_ms);
1006int aps_set_stream_volume(void *service, audio_stream_type_t stream,
Dima Zavinfce7a472011-04-19 22:30:36 -07001007 float volume, audio_io_handle_t output,
Eric Laurent2d388ec2014-03-07 13:25:54 -08001008 int delay_ms);
1009int aps_start_tone(void *service, audio_policy_tone_t tone,
1010 audio_stream_type_t stream);
1011int aps_stop_tone(void *service);
1012int aps_set_voice_volume(void *service, float volume, int delay_ms);
1013};
Dima Zavinfce7a472011-04-19 22:30:36 -07001014
1015namespace {
1016 struct audio_policy_service_ops aps_ops = {
Glenn Kasten01d3acb2014-02-06 08:24:07 -08001017 .open_output = aps_open_output,
1018 .open_duplicate_output = aps_open_dup_output,
1019 .close_output = aps_close_output,
1020 .suspend_output = aps_suspend_output,
1021 .restore_output = aps_restore_output,
1022 .open_input = aps_open_input,
1023 .close_input = aps_close_input,
1024 .set_stream_volume = aps_set_stream_volume,
Glenn Kastend2304db2014-02-03 07:40:31 -08001025 .invalidate_stream = aps_invalidate_stream,
Glenn Kasten01d3acb2014-02-06 08:24:07 -08001026 .set_parameters = aps_set_parameters,
1027 .get_parameters = aps_get_parameters,
1028 .start_tone = aps_start_tone,
1029 .stop_tone = aps_stop_tone,
1030 .set_voice_volume = aps_set_voice_volume,
1031 .move_effects = aps_move_effects,
1032 .load_hw_module = aps_load_hw_module,
1033 .open_output_on_module = aps_open_output_on_module,
1034 .open_input_on_module = aps_open_input_on_module,
Dima Zavinfce7a472011-04-19 22:30:36 -07001035 };
1036}; // namespace <unnamed>
1037
Mathias Agopian65ab4712010-07-14 17:59:35 -07001038}; // namespace android