blob: 943579794d835d41e32158c3aeba1ffa50f25b63 [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),
62 mAudioPolicyManager(NULL), mAudioPolicyClient(NULL)
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 Laurent93575202011-01-18 18:39:02 -080069 Mutex::Autolock _l(mLock);
70
Mathias Agopian65ab4712010-07-14 17:59:35 -070071 // start tone playback thread
Eric Laurentbfb1b832013-01-07 09:53:42 -080072 mTonePlaybackThread = new AudioCommandThread(String8("ApmTone"), this);
Mathias Agopian65ab4712010-07-14 17:59:35 -070073 // start audio commands thread
Eric Laurentbfb1b832013-01-07 09:53:42 -080074 mAudioCommandThread = new AudioCommandThread(String8("ApmAudio"), this);
75 // start output activity command thread
76 mOutputCommandThread = new AudioCommandThread(String8("ApmOutput"), this);
Eric Laurentdce54a12014-03-10 12:19:46 -070077
78#ifdef USE_LEGACY_AUDIO_POLICY
79 ALOGI("AudioPolicyService CSTOR in legacy mode");
80
Dima Zavinfce7a472011-04-19 22:30:36 -070081 /* instantiate the audio policy manager */
82 rc = hw_get_module(AUDIO_POLICY_HARDWARE_MODULE_ID, &module);
Glenn Kasten6e2ebe92013-08-13 09:14:51 -070083 if (rc) {
Dima Zavinfce7a472011-04-19 22:30:36 -070084 return;
Glenn Kasten6e2ebe92013-08-13 09:14:51 -070085 }
Dima Zavinfce7a472011-04-19 22:30:36 -070086 rc = audio_policy_dev_open(module, &mpAudioPolicyDev);
Steve Block29357bc2012-01-06 19:20:56 +000087 ALOGE_IF(rc, "couldn't open audio policy device (%s)", strerror(-rc));
Glenn Kasten6e2ebe92013-08-13 09:14:51 -070088 if (rc) {
Dima Zavinfce7a472011-04-19 22:30:36 -070089 return;
Glenn Kasten6e2ebe92013-08-13 09:14:51 -070090 }
Eric Laurent93575202011-01-18 18:39:02 -080091
Dima Zavinfce7a472011-04-19 22:30:36 -070092 rc = mpAudioPolicyDev->create_audio_policy(mpAudioPolicyDev, &aps_ops, this,
93 &mpAudioPolicy);
Steve Block29357bc2012-01-06 19:20:56 +000094 ALOGE_IF(rc, "couldn't create audio policy (%s)", strerror(-rc));
Glenn Kasten6e2ebe92013-08-13 09:14:51 -070095 if (rc) {
Dima Zavinfce7a472011-04-19 22:30:36 -070096 return;
Glenn Kasten6e2ebe92013-08-13 09:14:51 -070097 }
Dima Zavinfce7a472011-04-19 22:30:36 -070098
99 rc = mpAudioPolicy->init_check(mpAudioPolicy);
Steve Block29357bc2012-01-06 19:20:56 +0000100 ALOGE_IF(rc, "couldn't init_check the audio policy (%s)", strerror(-rc));
Glenn Kasten6e2ebe92013-08-13 09:14:51 -0700101 if (rc) {
Dima Zavinfce7a472011-04-19 22:30:36 -0700102 return;
Glenn Kasten6e2ebe92013-08-13 09:14:51 -0700103 }
Steve Blockdf64d152012-01-04 20:05:49 +0000104 ALOGI("Loaded audio policy from %s (%s)", module->name, module->id);
Eric Laurentdce54a12014-03-10 12:19:46 -0700105#else
106 ALOGI("AudioPolicyService CSTOR in new mode");
107
108 mAudioPolicyClient = new AudioPolicyClient(this);
Eric Laurentf269b8e2014-06-09 20:01:29 -0700109 mAudioPolicyManager = createAudioPolicyManager(mAudioPolicyClient);
Eric Laurentdce54a12014-03-10 12:19:46 -0700110#endif
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700111
bryant_liuba2b4392014-06-11 16:49:30 +0800112 // load audio processing modules
113 mAudioPolicyEffects = new AudioPolicyEffects();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700114}
115
116AudioPolicyService::~AudioPolicyService()
117{
118 mTonePlaybackThread->exit();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700119 mAudioCommandThread->exit();
Eric Laurent657ff612014-05-07 11:58:24 -0700120 mOutputCommandThread->exit();
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700121
Eric Laurentdce54a12014-03-10 12:19:46 -0700122#ifdef USE_LEGACY_AUDIO_POLICY
Glenn Kasten6e2ebe92013-08-13 09:14:51 -0700123 if (mpAudioPolicy != NULL && mpAudioPolicyDev != NULL) {
Dima Zavinfce7a472011-04-19 22:30:36 -0700124 mpAudioPolicyDev->destroy_audio_policy(mpAudioPolicyDev, mpAudioPolicy);
Glenn Kasten6e2ebe92013-08-13 09:14:51 -0700125 }
126 if (mpAudioPolicyDev != NULL) {
Dima Zavinfce7a472011-04-19 22:30:36 -0700127 audio_policy_dev_close(mpAudioPolicyDev);
Glenn Kasten6e2ebe92013-08-13 09:14:51 -0700128 }
Eric Laurentdce54a12014-03-10 12:19:46 -0700129#else
Eric Laurentf269b8e2014-06-09 20:01:29 -0700130 destroyAudioPolicyManager(mAudioPolicyManager);
Eric Laurentdce54a12014-03-10 12:19:46 -0700131 delete mAudioPolicyClient;
132#endif
Eric Laurentb52c1522014-05-20 11:27:36 -0700133
134 mNotificationClients.clear();
bryant_liuba2b4392014-06-11 16:49:30 +0800135 mAudioPolicyEffects.clear();
Eric Laurentb52c1522014-05-20 11:27:36 -0700136}
137
138// A notification client is always registered by AudioSystem when the client process
139// connects to AudioPolicyService.
140void AudioPolicyService::registerClient(const sp<IAudioPolicyServiceClient>& client)
141{
142
143 Mutex::Autolock _l(mLock);
144
145 uid_t uid = IPCThreadState::self()->getCallingUid();
146 if (mNotificationClients.indexOfKey(uid) < 0) {
147 sp<NotificationClient> notificationClient = new NotificationClient(this,
148 client,
149 uid);
150 ALOGV("registerClient() client %p, uid %d", client.get(), uid);
151
152 mNotificationClients.add(uid, notificationClient);
153
154 sp<IBinder> binder = client->asBinder();
155 binder->linkToDeath(notificationClient);
156 }
157}
158
159// removeNotificationClient() is called when the client process dies.
160void AudioPolicyService::removeNotificationClient(uid_t uid)
161{
162 Mutex::Autolock _l(mLock);
163
164 mNotificationClients.removeItem(uid);
165
166#ifndef USE_LEGACY_AUDIO_POLICY
167 if (mAudioPolicyManager) {
168 mAudioPolicyManager->clearAudioPatches(uid);
169 }
170#endif
171}
172
173void AudioPolicyService::onAudioPortListUpdate()
174{
175 mOutputCommandThread->updateAudioPortListCommand();
176}
177
178void AudioPolicyService::doOnAudioPortListUpdate()
179{
180 Mutex::Autolock _l(mLock);
181 for (size_t i = 0; i < mNotificationClients.size(); i++) {
182 mNotificationClients.valueAt(i)->onAudioPortListUpdate();
183 }
184}
185
186void AudioPolicyService::onAudioPatchListUpdate()
187{
188 mOutputCommandThread->updateAudioPatchListCommand();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700189}
190
Eric Laurent951f4552014-05-20 10:48:17 -0700191status_t AudioPolicyService::clientCreateAudioPatch(const struct audio_patch *patch,
192 audio_patch_handle_t *handle,
193 int delayMs)
194{
195 return mAudioCommandThread->createAudioPatchCommand(patch, handle, delayMs);
196}
197
198status_t AudioPolicyService::clientReleaseAudioPatch(audio_patch_handle_t handle,
199 int delayMs)
200{
201 return mAudioCommandThread->releaseAudioPatchCommand(handle, delayMs);
202}
203
Eric Laurentb52c1522014-05-20 11:27:36 -0700204void AudioPolicyService::doOnAudioPatchListUpdate()
205{
206 Mutex::Autolock _l(mLock);
207 for (size_t i = 0; i < mNotificationClients.size(); i++) {
208 mNotificationClients.valueAt(i)->onAudioPatchListUpdate();
209 }
210}
211
Eric Laurente1715a42014-05-20 11:30:42 -0700212status_t AudioPolicyService::clientSetAudioPortConfig(const struct audio_port_config *config,
213 int delayMs)
214{
215 return mAudioCommandThread->setAudioPortConfigCommand(config, delayMs);
216}
217
Eric Laurentb52c1522014-05-20 11:27:36 -0700218AudioPolicyService::NotificationClient::NotificationClient(const sp<AudioPolicyService>& service,
219 const sp<IAudioPolicyServiceClient>& client,
220 uid_t uid)
221 : mService(service), mUid(uid), mAudioPolicyServiceClient(client)
222{
223}
224
225AudioPolicyService::NotificationClient::~NotificationClient()
226{
227}
228
229void AudioPolicyService::NotificationClient::binderDied(const wp<IBinder>& who __unused)
230{
231 sp<NotificationClient> keep(this);
232 sp<AudioPolicyService> service = mService.promote();
233 if (service != 0) {
234 service->removeNotificationClient(mUid);
235 }
236}
237
238void AudioPolicyService::NotificationClient::onAudioPortListUpdate()
239{
240 if (mAudioPolicyServiceClient != 0) {
241 mAudioPolicyServiceClient->onAudioPortListUpdate();
242 }
243}
244
245void AudioPolicyService::NotificationClient::onAudioPatchListUpdate()
246{
247 if (mAudioPolicyServiceClient != 0) {
248 mAudioPolicyServiceClient->onAudioPatchListUpdate();
249 }
250}
Eric Laurent57dae992011-07-24 13:36:09 -0700251
Mathias Agopian65ab4712010-07-14 17:59:35 -0700252void AudioPolicyService::binderDied(const wp<IBinder>& who) {
Glenn Kasten411e4472012-11-02 10:00:06 -0700253 ALOGW("binderDied() %p, calling pid %d", who.unsafe_get(),
Eric Laurentde070132010-07-13 04:45:46 -0700254 IPCThreadState::self()->getCallingPid());
Mathias Agopian65ab4712010-07-14 17:59:35 -0700255}
256
257static bool tryLock(Mutex& mutex)
258{
259 bool locked = false;
260 for (int i = 0; i < kDumpLockRetries; ++i) {
261 if (mutex.tryLock() == NO_ERROR) {
262 locked = true;
263 break;
264 }
Glenn Kasten22ecc912012-01-09 08:33:38 -0800265 usleep(kDumpLockSleepUs);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700266 }
267 return locked;
268}
269
270status_t AudioPolicyService::dumpInternals(int fd)
271{
272 const size_t SIZE = 256;
273 char buffer[SIZE];
274 String8 result;
275
Eric Laurentdce54a12014-03-10 12:19:46 -0700276#ifdef USE_LEGACY_AUDIO_POLICY
Dima Zavinfce7a472011-04-19 22:30:36 -0700277 snprintf(buffer, SIZE, "PolicyManager Interface: %p\n", mpAudioPolicy);
Eric Laurentdce54a12014-03-10 12:19:46 -0700278#else
279 snprintf(buffer, SIZE, "AudioPolicyManager: %p\n", mAudioPolicyManager);
280#endif
Mathias Agopian65ab4712010-07-14 17:59:35 -0700281 result.append(buffer);
282 snprintf(buffer, SIZE, "Command Thread: %p\n", mAudioCommandThread.get());
283 result.append(buffer);
284 snprintf(buffer, SIZE, "Tones Thread: %p\n", mTonePlaybackThread.get());
285 result.append(buffer);
286
287 write(fd, result.string(), result.size());
288 return NO_ERROR;
289}
290
Glenn Kasten0f11b512014-01-31 16:18:54 -0800291status_t AudioPolicyService::dump(int fd, const Vector<String16>& args __unused)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700292{
Glenn Kasten44deb052012-02-05 18:09:08 -0800293 if (!dumpAllowed()) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700294 dumpPermissionDenial(fd);
295 } else {
296 bool locked = tryLock(mLock);
297 if (!locked) {
298 String8 result(kDeadlockedString);
299 write(fd, result.string(), result.size());
300 }
301
302 dumpInternals(fd);
Glenn Kasten9d1f02d2012-02-08 17:47:58 -0800303 if (mAudioCommandThread != 0) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700304 mAudioCommandThread->dump(fd);
305 }
Glenn Kasten9d1f02d2012-02-08 17:47:58 -0800306 if (mTonePlaybackThread != 0) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700307 mTonePlaybackThread->dump(fd);
308 }
309
Eric Laurentdce54a12014-03-10 12:19:46 -0700310#ifdef USE_LEGACY_AUDIO_POLICY
Dima Zavinfce7a472011-04-19 22:30:36 -0700311 if (mpAudioPolicy) {
312 mpAudioPolicy->dump(mpAudioPolicy, fd);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700313 }
Eric Laurentdce54a12014-03-10 12:19:46 -0700314#else
315 if (mAudioPolicyManager) {
316 mAudioPolicyManager->dump(fd);
317 }
318#endif
Mathias Agopian65ab4712010-07-14 17:59:35 -0700319
320 if (locked) mLock.unlock();
321 }
322 return NO_ERROR;
323}
324
325status_t AudioPolicyService::dumpPermissionDenial(int fd)
326{
327 const size_t SIZE = 256;
328 char buffer[SIZE];
329 String8 result;
330 snprintf(buffer, SIZE, "Permission Denial: "
331 "can't dump AudioPolicyService from pid=%d, uid=%d\n",
332 IPCThreadState::self()->getCallingPid(),
333 IPCThreadState::self()->getCallingUid());
334 result.append(buffer);
335 write(fd, result.string(), result.size());
336 return NO_ERROR;
337}
338
339status_t AudioPolicyService::onTransact(
340 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
341{
342 return BnAudioPolicyService::onTransact(code, data, reply, flags);
343}
344
345
Mathias Agopian65ab4712010-07-14 17:59:35 -0700346// ----------- AudioPolicyService::AudioCommandThread implementation ----------
347
Eric Laurentbfb1b832013-01-07 09:53:42 -0800348AudioPolicyService::AudioCommandThread::AudioCommandThread(String8 name,
349 const wp<AudioPolicyService>& service)
350 : Thread(false), mName(name), mService(service)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700351{
352 mpToneGenerator = NULL;
353}
354
355
356AudioPolicyService::AudioCommandThread::~AudioCommandThread()
357{
Eric Laurentbfb1b832013-01-07 09:53:42 -0800358 if (!mAudioCommands.isEmpty()) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700359 release_wake_lock(mName.string());
360 }
361 mAudioCommands.clear();
Glenn Kastene9dd0172012-01-27 18:08:45 -0800362 delete mpToneGenerator;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700363}
364
365void AudioPolicyService::AudioCommandThread::onFirstRef()
366{
Eric Laurentbfb1b832013-01-07 09:53:42 -0800367 run(mName.string(), ANDROID_PRIORITY_AUDIO);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700368}
369
370bool AudioPolicyService::AudioCommandThread::threadLoop()
371{
372 nsecs_t waitTime = INT64_MAX;
373
374 mLock.lock();
375 while (!exitPending())
376 {
Eric Laurent59a89232014-06-08 14:14:17 -0700377 sp<AudioPolicyService> svc;
378 while (!mAudioCommands.isEmpty() && !exitPending()) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700379 nsecs_t curTime = systemTime();
380 // commands are sorted by increasing time stamp: execute them from index 0 and up
381 if (mAudioCommands[0]->mTime <= curTime) {
Eric Laurent0ede8922014-05-09 18:04:42 -0700382 sp<AudioCommand> command = mAudioCommands[0];
Mathias Agopian65ab4712010-07-14 17:59:35 -0700383 mAudioCommands.removeAt(0);
Eric Laurent0ede8922014-05-09 18:04:42 -0700384 mLastCommand = command;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700385
386 switch (command->mCommand) {
387 case START_TONE: {
388 mLock.unlock();
Eric Laurent0ede8922014-05-09 18:04:42 -0700389 ToneData *data = (ToneData *)command->mParam.get();
Steve Block3856b092011-10-20 11:56:00 +0100390 ALOGV("AudioCommandThread() processing start tone %d on stream %d",
Mathias Agopian65ab4712010-07-14 17:59:35 -0700391 data->mType, data->mStream);
Glenn Kastene9dd0172012-01-27 18:08:45 -0800392 delete mpToneGenerator;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700393 mpToneGenerator = new ToneGenerator(data->mStream, 1.0);
394 mpToneGenerator->startTone(data->mType);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700395 mLock.lock();
396 }break;
397 case STOP_TONE: {
398 mLock.unlock();
Steve Block3856b092011-10-20 11:56:00 +0100399 ALOGV("AudioCommandThread() processing stop tone");
Mathias Agopian65ab4712010-07-14 17:59:35 -0700400 if (mpToneGenerator != NULL) {
401 mpToneGenerator->stopTone();
402 delete mpToneGenerator;
403 mpToneGenerator = NULL;
404 }
405 mLock.lock();
406 }break;
407 case SET_VOLUME: {
Eric Laurent0ede8922014-05-09 18:04:42 -0700408 VolumeData *data = (VolumeData *)command->mParam.get();
Steve Block3856b092011-10-20 11:56:00 +0100409 ALOGV("AudioCommandThread() processing set volume stream %d, \
Eric Laurentde070132010-07-13 04:45:46 -0700410 volume %f, output %d", data->mStream, data->mVolume, data->mIO);
411 command->mStatus = AudioSystem::setStreamVolume(data->mStream,
412 data->mVolume,
413 data->mIO);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700414 }break;
415 case SET_PARAMETERS: {
Eric Laurent0ede8922014-05-09 18:04:42 -0700416 ParametersData *data = (ParametersData *)command->mParam.get();
Glenn Kastene53b9ea2012-03-12 16:29:55 -0700417 ALOGV("AudioCommandThread() processing set parameters string %s, io %d",
418 data->mKeyValuePairs.string(), data->mIO);
419 command->mStatus = AudioSystem::setParameters(data->mIO, data->mKeyValuePairs);
Glenn Kastene53b9ea2012-03-12 16:29:55 -0700420 }break;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700421 case SET_VOICE_VOLUME: {
Eric Laurent0ede8922014-05-09 18:04:42 -0700422 VoiceVolumeData *data = (VoiceVolumeData *)command->mParam.get();
Steve Block3856b092011-10-20 11:56:00 +0100423 ALOGV("AudioCommandThread() processing set voice volume volume %f",
Eric Laurentde070132010-07-13 04:45:46 -0700424 data->mVolume);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700425 command->mStatus = AudioSystem::setVoiceVolume(data->mVolume);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700426 }break;
Eric Laurentbfb1b832013-01-07 09:53:42 -0800427 case STOP_OUTPUT: {
Eric Laurent0ede8922014-05-09 18:04:42 -0700428 StopOutputData *data = (StopOutputData *)command->mParam.get();
Eric Laurentbfb1b832013-01-07 09:53:42 -0800429 ALOGV("AudioCommandThread() processing stop output %d",
430 data->mIO);
Eric Laurent59a89232014-06-08 14:14:17 -0700431 svc = mService.promote();
Eric Laurentbfb1b832013-01-07 09:53:42 -0800432 if (svc == 0) {
433 break;
434 }
435 mLock.unlock();
436 svc->doStopOutput(data->mIO, data->mStream, data->mSession);
437 mLock.lock();
Eric Laurentbfb1b832013-01-07 09:53:42 -0800438 }break;
439 case RELEASE_OUTPUT: {
Eric Laurent0ede8922014-05-09 18:04:42 -0700440 ReleaseOutputData *data = (ReleaseOutputData *)command->mParam.get();
Eric Laurentbfb1b832013-01-07 09:53:42 -0800441 ALOGV("AudioCommandThread() processing release output %d",
442 data->mIO);
Eric Laurent59a89232014-06-08 14:14:17 -0700443 svc = mService.promote();
Eric Laurentbfb1b832013-01-07 09:53:42 -0800444 if (svc == 0) {
445 break;
446 }
447 mLock.unlock();
448 svc->doReleaseOutput(data->mIO);
449 mLock.lock();
Eric Laurentbfb1b832013-01-07 09:53:42 -0800450 }break;
Eric Laurent951f4552014-05-20 10:48:17 -0700451 case CREATE_AUDIO_PATCH: {
452 CreateAudioPatchData *data = (CreateAudioPatchData *)command->mParam.get();
453 ALOGV("AudioCommandThread() processing create audio patch");
454 sp<IAudioFlinger> af = AudioSystem::get_audio_flinger();
455 if (af == 0) {
456 command->mStatus = PERMISSION_DENIED;
457 } else {
458 command->mStatus = af->createAudioPatch(&data->mPatch, &data->mHandle);
459 }
460 } break;
461 case RELEASE_AUDIO_PATCH: {
462 ReleaseAudioPatchData *data = (ReleaseAudioPatchData *)command->mParam.get();
463 ALOGV("AudioCommandThread() processing release audio patch");
464 sp<IAudioFlinger> af = AudioSystem::get_audio_flinger();
465 if (af == 0) {
466 command->mStatus = PERMISSION_DENIED;
467 } else {
468 command->mStatus = af->releaseAudioPatch(data->mHandle);
469 }
470 } break;
Eric Laurentb52c1522014-05-20 11:27:36 -0700471 case UPDATE_AUDIOPORT_LIST: {
472 ALOGV("AudioCommandThread() processing update audio port list");
Eric Laurent59a89232014-06-08 14:14:17 -0700473 svc = mService.promote();
Eric Laurentb52c1522014-05-20 11:27:36 -0700474 if (svc == 0) {
475 break;
476 }
477 mLock.unlock();
478 svc->doOnAudioPortListUpdate();
479 mLock.lock();
480 }break;
481 case UPDATE_AUDIOPATCH_LIST: {
482 ALOGV("AudioCommandThread() processing update audio patch list");
Eric Laurent59a89232014-06-08 14:14:17 -0700483 svc = mService.promote();
Eric Laurentb52c1522014-05-20 11:27:36 -0700484 if (svc == 0) {
485 break;
486 }
487 mLock.unlock();
488 svc->doOnAudioPatchListUpdate();
489 mLock.lock();
490 }break;
Eric Laurente1715a42014-05-20 11:30:42 -0700491 case SET_AUDIOPORT_CONFIG: {
492 SetAudioPortConfigData *data = (SetAudioPortConfigData *)command->mParam.get();
493 ALOGV("AudioCommandThread() processing set port config");
494 sp<IAudioFlinger> af = AudioSystem::get_audio_flinger();
495 if (af == 0) {
496 command->mStatus = PERMISSION_DENIED;
497 } else {
498 command->mStatus = af->setAudioPortConfig(&data->mConfig);
499 }
500 } break;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700501 default:
Steve Block5ff1dd52012-01-05 23:22:43 +0000502 ALOGW("AudioCommandThread() unknown command %d", command->mCommand);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700503 }
Eric Laurent0ede8922014-05-09 18:04:42 -0700504 {
505 Mutex::Autolock _l(command->mLock);
506 if (command->mWaitStatus) {
507 command->mWaitStatus = false;
508 command->mCond.signal();
509 }
510 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700511 waitTime = INT64_MAX;
512 } else {
513 waitTime = mAudioCommands[0]->mTime - curTime;
514 break;
515 }
516 }
517 // release delayed commands wake lock
Eric Laurentbfb1b832013-01-07 09:53:42 -0800518 if (mAudioCommands.isEmpty()) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700519 release_wake_lock(mName.string());
520 }
Eric Laurent59a89232014-06-08 14:14:17 -0700521 // release mLock before releasing strong reference on the service as
522 // AudioPolicyService destructor calls AudioCommandThread::exit() which acquires mLock.
523 mLock.unlock();
524 svc.clear();
525 mLock.lock();
526 if (!exitPending()) {
527 ALOGV("AudioCommandThread() going to sleep");
528 mWaitWorkCV.waitRelative(mLock, waitTime);
529 ALOGV("AudioCommandThread() waking up");
530 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700531 }
532 mLock.unlock();
533 return false;
534}
535
536status_t AudioPolicyService::AudioCommandThread::dump(int fd)
537{
538 const size_t SIZE = 256;
539 char buffer[SIZE];
540 String8 result;
541
542 snprintf(buffer, SIZE, "AudioCommandThread %p Dump\n", this);
543 result.append(buffer);
544 write(fd, result.string(), result.size());
545
546 bool locked = tryLock(mLock);
547 if (!locked) {
548 String8 result2(kCmdDeadlockedString);
549 write(fd, result2.string(), result2.size());
550 }
551
552 snprintf(buffer, SIZE, "- Commands:\n");
553 result = String8(buffer);
554 result.append(" Command Time Wait pParam\n");
Glenn Kasten8d6a2442012-02-08 14:04:28 -0800555 for (size_t i = 0; i < mAudioCommands.size(); i++) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700556 mAudioCommands[i]->dump(buffer, SIZE);
557 result.append(buffer);
558 }
559 result.append(" Last Command\n");
Eric Laurent0ede8922014-05-09 18:04:42 -0700560 if (mLastCommand != 0) {
561 mLastCommand->dump(buffer, SIZE);
562 result.append(buffer);
563 } else {
564 result.append(" none\n");
565 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700566
567 write(fd, result.string(), result.size());
568
569 if (locked) mLock.unlock();
570
571 return NO_ERROR;
572}
573
Glenn Kasten3d2f8772012-01-27 15:25:25 -0800574void AudioPolicyService::AudioCommandThread::startToneCommand(ToneGenerator::tone_type type,
575 audio_stream_type_t stream)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700576{
Eric Laurent0ede8922014-05-09 18:04:42 -0700577 sp<AudioCommand> command = new AudioCommand();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700578 command->mCommand = START_TONE;
Eric Laurent0ede8922014-05-09 18:04:42 -0700579 sp<ToneData> data = new ToneData();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700580 data->mType = type;
581 data->mStream = stream;
Jesper Tragardh48412dc2014-03-24 14:12:43 +0100582 command->mParam = data;
Steve Block3856b092011-10-20 11:56:00 +0100583 ALOGV("AudioCommandThread() adding tone start type %d, stream %d", type, stream);
Eric Laurent0ede8922014-05-09 18:04:42 -0700584 sendCommand(command);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700585}
586
587void AudioPolicyService::AudioCommandThread::stopToneCommand()
588{
Eric Laurent0ede8922014-05-09 18:04:42 -0700589 sp<AudioCommand> command = new AudioCommand();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700590 command->mCommand = STOP_TONE;
Steve Block3856b092011-10-20 11:56:00 +0100591 ALOGV("AudioCommandThread() adding tone stop");
Eric Laurent0ede8922014-05-09 18:04:42 -0700592 sendCommand(command);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700593}
594
Glenn Kastenfff6d712012-01-12 16:38:12 -0800595status_t AudioPolicyService::AudioCommandThread::volumeCommand(audio_stream_type_t stream,
Eric Laurentde070132010-07-13 04:45:46 -0700596 float volume,
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800597 audio_io_handle_t output,
Eric Laurentde070132010-07-13 04:45:46 -0700598 int delayMs)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700599{
Eric Laurent0ede8922014-05-09 18:04:42 -0700600 sp<AudioCommand> command = new AudioCommand();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700601 command->mCommand = SET_VOLUME;
Eric Laurent0ede8922014-05-09 18:04:42 -0700602 sp<VolumeData> data = new VolumeData();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700603 data->mStream = stream;
604 data->mVolume = volume;
605 data->mIO = output;
606 command->mParam = data;
Eric Laurent0ede8922014-05-09 18:04:42 -0700607 command->mWaitStatus = true;
Steve Block3856b092011-10-20 11:56:00 +0100608 ALOGV("AudioCommandThread() adding set volume stream %d, volume %f, output %d",
Eric Laurentde070132010-07-13 04:45:46 -0700609 stream, volume, output);
Eric Laurent0ede8922014-05-09 18:04:42 -0700610 return sendCommand(command, delayMs);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700611}
612
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800613status_t AudioPolicyService::AudioCommandThread::parametersCommand(audio_io_handle_t ioHandle,
Dima Zavinfce7a472011-04-19 22:30:36 -0700614 const char *keyValuePairs,
Eric Laurentde070132010-07-13 04:45:46 -0700615 int delayMs)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700616{
Eric Laurent0ede8922014-05-09 18:04:42 -0700617 sp<AudioCommand> command = new AudioCommand();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700618 command->mCommand = SET_PARAMETERS;
Eric Laurent0ede8922014-05-09 18:04:42 -0700619 sp<ParametersData> data = new ParametersData();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700620 data->mIO = ioHandle;
Dima Zavinfce7a472011-04-19 22:30:36 -0700621 data->mKeyValuePairs = String8(keyValuePairs);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700622 command->mParam = data;
Eric Laurent0ede8922014-05-09 18:04:42 -0700623 command->mWaitStatus = true;
Steve Block3856b092011-10-20 11:56:00 +0100624 ALOGV("AudioCommandThread() adding set parameter string %s, io %d ,delay %d",
Dima Zavinfce7a472011-04-19 22:30:36 -0700625 keyValuePairs, ioHandle, delayMs);
Eric Laurent0ede8922014-05-09 18:04:42 -0700626 return sendCommand(command, delayMs);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700627}
628
629status_t AudioPolicyService::AudioCommandThread::voiceVolumeCommand(float volume, int delayMs)
630{
Eric Laurent0ede8922014-05-09 18:04:42 -0700631 sp<AudioCommand> command = new AudioCommand();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700632 command->mCommand = SET_VOICE_VOLUME;
Eric Laurent0ede8922014-05-09 18:04:42 -0700633 sp<VoiceVolumeData> data = new VoiceVolumeData();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700634 data->mVolume = volume;
635 command->mParam = data;
Eric Laurent0ede8922014-05-09 18:04:42 -0700636 command->mWaitStatus = true;
Steve Block3856b092011-10-20 11:56:00 +0100637 ALOGV("AudioCommandThread() adding set voice volume volume %f", volume);
Eric Laurent0ede8922014-05-09 18:04:42 -0700638 return sendCommand(command, delayMs);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700639}
640
Eric Laurentbfb1b832013-01-07 09:53:42 -0800641void AudioPolicyService::AudioCommandThread::stopOutputCommand(audio_io_handle_t output,
642 audio_stream_type_t stream,
643 int session)
644{
Eric Laurent0ede8922014-05-09 18:04:42 -0700645 sp<AudioCommand> command = new AudioCommand();
Eric Laurentbfb1b832013-01-07 09:53:42 -0800646 command->mCommand = STOP_OUTPUT;
Eric Laurent0ede8922014-05-09 18:04:42 -0700647 sp<StopOutputData> data = new StopOutputData();
Eric Laurentbfb1b832013-01-07 09:53:42 -0800648 data->mIO = output;
649 data->mStream = stream;
650 data->mSession = session;
Jesper Tragardh48412dc2014-03-24 14:12:43 +0100651 command->mParam = data;
Eric Laurentbfb1b832013-01-07 09:53:42 -0800652 ALOGV("AudioCommandThread() adding stop output %d", output);
Eric Laurent0ede8922014-05-09 18:04:42 -0700653 sendCommand(command);
Eric Laurentbfb1b832013-01-07 09:53:42 -0800654}
655
656void AudioPolicyService::AudioCommandThread::releaseOutputCommand(audio_io_handle_t output)
657{
Eric Laurent0ede8922014-05-09 18:04:42 -0700658 sp<AudioCommand> command = new AudioCommand();
Eric Laurentbfb1b832013-01-07 09:53:42 -0800659 command->mCommand = RELEASE_OUTPUT;
Eric Laurent0ede8922014-05-09 18:04:42 -0700660 sp<ReleaseOutputData> data = new ReleaseOutputData();
Eric Laurentbfb1b832013-01-07 09:53:42 -0800661 data->mIO = output;
Jesper Tragardh48412dc2014-03-24 14:12:43 +0100662 command->mParam = data;
Eric Laurentbfb1b832013-01-07 09:53:42 -0800663 ALOGV("AudioCommandThread() adding release output %d", output);
Eric Laurent0ede8922014-05-09 18:04:42 -0700664 sendCommand(command);
665}
666
Eric Laurent951f4552014-05-20 10:48:17 -0700667status_t AudioPolicyService::AudioCommandThread::createAudioPatchCommand(
668 const struct audio_patch *patch,
669 audio_patch_handle_t *handle,
670 int delayMs)
671{
672 status_t status = NO_ERROR;
673
674 sp<AudioCommand> command = new AudioCommand();
675 command->mCommand = CREATE_AUDIO_PATCH;
676 CreateAudioPatchData *data = new CreateAudioPatchData();
677 data->mPatch = *patch;
678 data->mHandle = *handle;
679 command->mParam = data;
680 command->mWaitStatus = true;
681 ALOGV("AudioCommandThread() adding create patch delay %d", delayMs);
682 status = sendCommand(command, delayMs);
683 if (status == NO_ERROR) {
684 *handle = data->mHandle;
685 }
686 return status;
687}
688
689status_t AudioPolicyService::AudioCommandThread::releaseAudioPatchCommand(audio_patch_handle_t handle,
690 int delayMs)
691{
692 sp<AudioCommand> command = new AudioCommand();
693 command->mCommand = RELEASE_AUDIO_PATCH;
694 ReleaseAudioPatchData *data = new ReleaseAudioPatchData();
695 data->mHandle = handle;
696 command->mParam = data;
697 command->mWaitStatus = true;
698 ALOGV("AudioCommandThread() adding release patch delay %d", delayMs);
699 return sendCommand(command, delayMs);
700}
701
Eric Laurentb52c1522014-05-20 11:27:36 -0700702void AudioPolicyService::AudioCommandThread::updateAudioPortListCommand()
703{
704 sp<AudioCommand> command = new AudioCommand();
705 command->mCommand = UPDATE_AUDIOPORT_LIST;
706 ALOGV("AudioCommandThread() adding update audio port list");
707 sendCommand(command);
708}
709
710void AudioPolicyService::AudioCommandThread::updateAudioPatchListCommand()
711{
712 sp<AudioCommand>command = new AudioCommand();
713 command->mCommand = UPDATE_AUDIOPATCH_LIST;
714 ALOGV("AudioCommandThread() adding update audio patch list");
715 sendCommand(command);
716}
717
Eric Laurente1715a42014-05-20 11:30:42 -0700718status_t AudioPolicyService::AudioCommandThread::setAudioPortConfigCommand(
719 const struct audio_port_config *config, int delayMs)
720{
721 sp<AudioCommand> command = new AudioCommand();
722 command->mCommand = SET_AUDIOPORT_CONFIG;
723 SetAudioPortConfigData *data = new SetAudioPortConfigData();
724 data->mConfig = *config;
725 command->mParam = data;
726 command->mWaitStatus = true;
727 ALOGV("AudioCommandThread() adding set port config delay %d", delayMs);
728 return sendCommand(command, delayMs);
729}
730
Eric Laurent0ede8922014-05-09 18:04:42 -0700731status_t AudioPolicyService::AudioCommandThread::sendCommand(sp<AudioCommand>& command, int delayMs)
732{
733 {
734 Mutex::Autolock _l(mLock);
735 insertCommand_l(command, delayMs);
736 mWaitWorkCV.signal();
737 }
738 Mutex::Autolock _l(command->mLock);
739 while (command->mWaitStatus) {
740 nsecs_t timeOutNs = kAudioCommandTimeoutNs + milliseconds(delayMs);
741 if (command->mCond.waitRelative(command->mLock, timeOutNs) != NO_ERROR) {
742 command->mStatus = TIMED_OUT;
743 command->mWaitStatus = false;
744 }
745 }
746 return command->mStatus;
Eric Laurentbfb1b832013-01-07 09:53:42 -0800747}
748
Mathias Agopian65ab4712010-07-14 17:59:35 -0700749// insertCommand_l() must be called with mLock held
Eric Laurent0ede8922014-05-09 18:04:42 -0700750void AudioPolicyService::AudioCommandThread::insertCommand_l(sp<AudioCommand>& command, int delayMs)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700751{
Glenn Kasten8d6a2442012-02-08 14:04:28 -0800752 ssize_t i; // not size_t because i will count down to -1
Eric Laurent0ede8922014-05-09 18:04:42 -0700753 Vector < sp<AudioCommand> > removedCommands;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700754 command->mTime = systemTime() + milliseconds(delayMs);
755
756 // acquire wake lock to make sure delayed commands are processed
Eric Laurentbfb1b832013-01-07 09:53:42 -0800757 if (mAudioCommands.isEmpty()) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700758 acquire_wake_lock(PARTIAL_WAKE_LOCK, mName.string());
759 }
760
761 // check same pending commands with later time stamps and eliminate them
762 for (i = mAudioCommands.size()-1; i >= 0; i--) {
Eric Laurent0ede8922014-05-09 18:04:42 -0700763 sp<AudioCommand> command2 = mAudioCommands[i];
Mathias Agopian65ab4712010-07-14 17:59:35 -0700764 // commands are sorted by increasing time stamp: no need to scan the rest of mAudioCommands
765 if (command2->mTime <= command->mTime) break;
766 if (command2->mCommand != command->mCommand) continue;
767
768 switch (command->mCommand) {
769 case SET_PARAMETERS: {
Eric Laurent0ede8922014-05-09 18:04:42 -0700770 ParametersData *data = (ParametersData *)command->mParam.get();
771 ParametersData *data2 = (ParametersData *)command2->mParam.get();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700772 if (data->mIO != data2->mIO) break;
Steve Block3856b092011-10-20 11:56:00 +0100773 ALOGV("Comparing parameter command %s to new command %s",
Eric Laurentde070132010-07-13 04:45:46 -0700774 data2->mKeyValuePairs.string(), data->mKeyValuePairs.string());
Mathias Agopian65ab4712010-07-14 17:59:35 -0700775 AudioParameter param = AudioParameter(data->mKeyValuePairs);
776 AudioParameter param2 = AudioParameter(data2->mKeyValuePairs);
777 for (size_t j = 0; j < param.size(); j++) {
Glenn Kastene53b9ea2012-03-12 16:29:55 -0700778 String8 key;
779 String8 value;
780 param.getAt(j, key, value);
781 for (size_t k = 0; k < param2.size(); k++) {
782 String8 key2;
783 String8 value2;
784 param2.getAt(k, key2, value2);
785 if (key2 == key) {
786 param2.remove(key2);
787 ALOGV("Filtering out parameter %s", key2.string());
788 break;
789 }
790 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700791 }
792 // if all keys have been filtered out, remove the command.
793 // otherwise, update the key value pairs
794 if (param2.size() == 0) {
795 removedCommands.add(command2);
796 } else {
797 data2->mKeyValuePairs = param2.toString();
798 }
Eric Laurent21e54562013-09-23 12:08:05 -0700799 command->mTime = command2->mTime;
800 // force delayMs to non 0 so that code below does not request to wait for
801 // command status as the command is now delayed
802 delayMs = 1;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700803 } break;
804
805 case SET_VOLUME: {
Eric Laurent0ede8922014-05-09 18:04:42 -0700806 VolumeData *data = (VolumeData *)command->mParam.get();
807 VolumeData *data2 = (VolumeData *)command2->mParam.get();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700808 if (data->mIO != data2->mIO) break;
809 if (data->mStream != data2->mStream) break;
Steve Block3856b092011-10-20 11:56:00 +0100810 ALOGV("Filtering out volume command on output %d for stream %d",
Eric Laurentde070132010-07-13 04:45:46 -0700811 data->mIO, data->mStream);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700812 removedCommands.add(command2);
Eric Laurent21e54562013-09-23 12:08:05 -0700813 command->mTime = command2->mTime;
814 // force delayMs to non 0 so that code below does not request to wait for
815 // command status as the command is now delayed
816 delayMs = 1;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700817 } break;
818 case START_TONE:
819 case STOP_TONE:
820 default:
821 break;
822 }
823 }
824
825 // remove filtered commands
826 for (size_t j = 0; j < removedCommands.size(); j++) {
827 // removed commands always have time stamps greater than current command
828 for (size_t k = i + 1; k < mAudioCommands.size(); k++) {
Eric Laurent0ede8922014-05-09 18:04:42 -0700829 if (mAudioCommands[k].get() == removedCommands[j].get()) {
Steve Block3856b092011-10-20 11:56:00 +0100830 ALOGV("suppressing command: %d", mAudioCommands[k]->mCommand);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700831 mAudioCommands.removeAt(k);
832 break;
833 }
834 }
835 }
836 removedCommands.clear();
837
Eric Laurent0ede8922014-05-09 18:04:42 -0700838 // Disable wait for status if delay is not 0
839 if (delayMs != 0) {
Eric Laurentcec4abb2012-07-03 12:23:02 -0700840 command->mWaitStatus = false;
841 }
Eric Laurentcec4abb2012-07-03 12:23:02 -0700842
Mathias Agopian65ab4712010-07-14 17:59:35 -0700843 // insert command at the right place according to its time stamp
Steve Block3856b092011-10-20 11:56:00 +0100844 ALOGV("inserting command: %d at index %d, num commands %d",
Eric Laurentde070132010-07-13 04:45:46 -0700845 command->mCommand, (int)i+1, mAudioCommands.size());
Mathias Agopian65ab4712010-07-14 17:59:35 -0700846 mAudioCommands.insertAt(command, i + 1);
847}
848
849void AudioPolicyService::AudioCommandThread::exit()
850{
Steve Block3856b092011-10-20 11:56:00 +0100851 ALOGV("AudioCommandThread::exit");
Mathias Agopian65ab4712010-07-14 17:59:35 -0700852 {
853 AutoMutex _l(mLock);
854 requestExit();
855 mWaitWorkCV.signal();
856 }
857 requestExitAndWait();
858}
859
860void AudioPolicyService::AudioCommandThread::AudioCommand::dump(char* buffer, size_t size)
861{
862 snprintf(buffer, size, " %02d %06d.%03d %01u %p\n",
863 mCommand,
864 (int)ns2s(mTime),
865 (int)ns2ms(mTime)%1000,
866 mWaitStatus,
Eric Laurent0ede8922014-05-09 18:04:42 -0700867 mParam.get());
Mathias Agopian65ab4712010-07-14 17:59:35 -0700868}
869
Dima Zavinfce7a472011-04-19 22:30:36 -0700870/******* helpers for the service_ops callbacks defined below *********/
871void AudioPolicyService::setParameters(audio_io_handle_t ioHandle,
872 const char *keyValuePairs,
873 int delayMs)
874{
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800875 mAudioCommandThread->parametersCommand(ioHandle, keyValuePairs,
Dima Zavinfce7a472011-04-19 22:30:36 -0700876 delayMs);
877}
878
879int AudioPolicyService::setStreamVolume(audio_stream_type_t stream,
880 float volume,
881 audio_io_handle_t output,
882 int delayMs)
883{
Glenn Kastenfff6d712012-01-12 16:38:12 -0800884 return (int)mAudioCommandThread->volumeCommand(stream, volume,
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800885 output, delayMs);
Dima Zavinfce7a472011-04-19 22:30:36 -0700886}
887
888int AudioPolicyService::startTone(audio_policy_tone_t tone,
889 audio_stream_type_t stream)
890{
Glenn Kasten6e2ebe92013-08-13 09:14:51 -0700891 if (tone != AUDIO_POLICY_TONE_IN_CALL_NOTIFICATION) {
Steve Block29357bc2012-01-06 19:20:56 +0000892 ALOGE("startTone: illegal tone requested (%d)", tone);
Glenn Kasten6e2ebe92013-08-13 09:14:51 -0700893 }
894 if (stream != AUDIO_STREAM_VOICE_CALL) {
Steve Block29357bc2012-01-06 19:20:56 +0000895 ALOGE("startTone: illegal stream (%d) requested for tone %d", stream,
Glenn Kastene53b9ea2012-03-12 16:29:55 -0700896 tone);
Glenn Kasten6e2ebe92013-08-13 09:14:51 -0700897 }
Dima Zavinfce7a472011-04-19 22:30:36 -0700898 mTonePlaybackThread->startToneCommand(ToneGenerator::TONE_SUP_CALL_WAITING,
899 AUDIO_STREAM_VOICE_CALL);
900 return 0;
901}
902
903int AudioPolicyService::stopTone()
904{
905 mTonePlaybackThread->stopToneCommand();
906 return 0;
907}
908
909int AudioPolicyService::setVoiceVolume(float volume, int delayMs)
910{
911 return (int)mAudioCommandThread->voiceVolumeCommand(volume, delayMs);
912}
913
Dima Zavinfce7a472011-04-19 22:30:36 -0700914extern "C" {
Eric Laurent2d388ec2014-03-07 13:25:54 -0800915audio_module_handle_t aps_load_hw_module(void *service __unused,
916 const char *name);
917audio_io_handle_t aps_open_output(void *service __unused,
Eric Laurenta4c5a552012-03-29 10:12:40 -0700918 audio_devices_t *pDevices,
919 uint32_t *pSamplingRate,
920 audio_format_t *pFormat,
921 audio_channel_mask_t *pChannelMask,
922 uint32_t *pLatencyMs,
Eric Laurent2d388ec2014-03-07 13:25:54 -0800923 audio_output_flags_t flags);
Eric Laurenta4c5a552012-03-29 10:12:40 -0700924
Eric Laurent2d388ec2014-03-07 13:25:54 -0800925audio_io_handle_t aps_open_output_on_module(void *service __unused,
Eric Laurenta4c5a552012-03-29 10:12:40 -0700926 audio_module_handle_t module,
927 audio_devices_t *pDevices,
928 uint32_t *pSamplingRate,
929 audio_format_t *pFormat,
930 audio_channel_mask_t *pChannelMask,
931 uint32_t *pLatencyMs,
Richard Fitzgeraldad3af332013-03-25 16:54:37 +0000932 audio_output_flags_t flags,
Eric Laurent2d388ec2014-03-07 13:25:54 -0800933 const audio_offload_info_t *offloadInfo);
934audio_io_handle_t aps_open_dup_output(void *service __unused,
Dima Zavinfce7a472011-04-19 22:30:36 -0700935 audio_io_handle_t output1,
Eric Laurent2d388ec2014-03-07 13:25:54 -0800936 audio_io_handle_t output2);
937int aps_close_output(void *service __unused, audio_io_handle_t output);
938int aps_suspend_output(void *service __unused, audio_io_handle_t output);
939int aps_restore_output(void *service __unused, audio_io_handle_t output);
940audio_io_handle_t aps_open_input(void *service __unused,
Eric Laurenta4c5a552012-03-29 10:12:40 -0700941 audio_devices_t *pDevices,
942 uint32_t *pSamplingRate,
943 audio_format_t *pFormat,
944 audio_channel_mask_t *pChannelMask,
Eric Laurent2d388ec2014-03-07 13:25:54 -0800945 audio_in_acoustics_t acoustics __unused);
946audio_io_handle_t aps_open_input_on_module(void *service __unused,
Eric Laurenta4c5a552012-03-29 10:12:40 -0700947 audio_module_handle_t module,
948 audio_devices_t *pDevices,
949 uint32_t *pSamplingRate,
950 audio_format_t *pFormat,
Eric Laurent2d388ec2014-03-07 13:25:54 -0800951 audio_channel_mask_t *pChannelMask);
952int aps_close_input(void *service __unused, audio_io_handle_t input);
953int aps_invalidate_stream(void *service __unused, audio_stream_type_t stream);
954int aps_move_effects(void *service __unused, int session,
Dima Zavinfce7a472011-04-19 22:30:36 -0700955 audio_io_handle_t src_output,
Eric Laurent2d388ec2014-03-07 13:25:54 -0800956 audio_io_handle_t dst_output);
957char * aps_get_parameters(void *service __unused, audio_io_handle_t io_handle,
958 const char *keys);
959void aps_set_parameters(void *service, audio_io_handle_t io_handle,
960 const char *kv_pairs, int delay_ms);
961int aps_set_stream_volume(void *service, audio_stream_type_t stream,
Dima Zavinfce7a472011-04-19 22:30:36 -0700962 float volume, audio_io_handle_t output,
Eric Laurent2d388ec2014-03-07 13:25:54 -0800963 int delay_ms);
964int aps_start_tone(void *service, audio_policy_tone_t tone,
965 audio_stream_type_t stream);
966int aps_stop_tone(void *service);
967int aps_set_voice_volume(void *service, float volume, int delay_ms);
968};
Dima Zavinfce7a472011-04-19 22:30:36 -0700969
970namespace {
971 struct audio_policy_service_ops aps_ops = {
Glenn Kasten01d3acb2014-02-06 08:24:07 -0800972 .open_output = aps_open_output,
973 .open_duplicate_output = aps_open_dup_output,
974 .close_output = aps_close_output,
975 .suspend_output = aps_suspend_output,
976 .restore_output = aps_restore_output,
977 .open_input = aps_open_input,
978 .close_input = aps_close_input,
979 .set_stream_volume = aps_set_stream_volume,
Glenn Kastend2304db2014-02-03 07:40:31 -0800980 .invalidate_stream = aps_invalidate_stream,
Glenn Kasten01d3acb2014-02-06 08:24:07 -0800981 .set_parameters = aps_set_parameters,
982 .get_parameters = aps_get_parameters,
983 .start_tone = aps_start_tone,
984 .stop_tone = aps_stop_tone,
985 .set_voice_volume = aps_set_voice_volume,
986 .move_effects = aps_move_effects,
987 .load_hw_module = aps_load_hw_module,
988 .open_output_on_module = aps_open_output_on_module,
989 .open_input_on_module = aps_open_input_on_module,
Dima Zavinfce7a472011-04-19 22:30:36 -0700990 };
991}; // namespace <unnamed>
992
Mathias Agopian65ab4712010-07-14 17:59:35 -0700993}; // namespace android