Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1 | /* |
| 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 Kasten | 153b9fe | 2013-07-15 11:23:36 -0700 | [diff] [blame] | 20 | #include "Configuration.h" |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 21 | #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 Kasten | 44deb05 | 2012-02-05 18:09:08 -0800 | [diff] [blame] | 34 | #include "ServiceUtilities.h" |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 35 | #include <hardware_legacy/power.h> |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 36 | #include <media/AudioEffect.h> |
| 37 | #include <media/EffectsFactoryApi.h> |
Chih-Hung Hsieh | c84d9d2 | 2014-11-14 13:33:34 -0800 | [diff] [blame] | 38 | #include <media/AudioParameter.h> |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 39 | |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 40 | #include <hardware/hardware.h> |
Dima Zavin | 6476024 | 2011-05-11 14:15:23 -0700 | [diff] [blame] | 41 | #include <system/audio.h> |
Dima Zavin | 7394a4f | 2011-06-13 18:16:26 -0700 | [diff] [blame] | 42 | #include <system/audio_policy.h> |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 43 | #include <hardware/audio_policy.h> |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 44 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 45 | namespace android { |
| 46 | |
Glenn Kasten | 8dad0e3 | 2012-01-09 08:41:22 -0800 | [diff] [blame] | 47 | static const char kDeadlockedString[] = "AudioPolicyService may be deadlocked\n"; |
| 48 | static const char kCmdDeadlockedString[] = "AudioPolicyService command thread may be deadlocked\n"; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 49 | |
| 50 | static const int kDumpLockRetries = 50; |
Glenn Kasten | 22ecc91 | 2012-01-09 08:33:38 -0800 | [diff] [blame] | 51 | static const int kDumpLockSleepUs = 20000; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 52 | |
Eric Laurent | 0ede892 | 2014-05-09 18:04:42 -0700 | [diff] [blame] | 53 | static const nsecs_t kAudioCommandTimeoutNs = seconds(3); // 3 seconds |
Christer Fletcher | 5fa8c4b | 2013-01-18 15:27:03 +0100 | [diff] [blame] | 54 | |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 55 | namespace { |
| 56 | extern struct audio_policy_service_ops aps_ops; |
| 57 | }; |
| 58 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 59 | // ---------------------------------------------------------------------------- |
| 60 | |
| 61 | AudioPolicyService::AudioPolicyService() |
Eric Laurent | dce54a1 | 2014-03-10 12:19:46 -0700 | [diff] [blame] | 62 | : BnAudioPolicyService(), mpAudioPolicyDev(NULL), mpAudioPolicy(NULL), |
Eric Laurent | bb6c9a0 | 2014-09-25 14:11:47 -0700 | [diff] [blame] | 63 | mAudioPolicyManager(NULL), mAudioPolicyClient(NULL), mPhoneState(AUDIO_MODE_INVALID) |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 64 | { |
Eric Laurent | f5ada6e | 2014-10-09 17:49:00 -0700 | [diff] [blame] | 65 | } |
| 66 | |
| 67 | void AudioPolicyService::onFirstRef() |
| 68 | { |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 69 | char value[PROPERTY_VALUE_MAX]; |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 70 | const struct hw_module_t *module; |
| 71 | int forced_val; |
| 72 | int rc; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 73 | |
Eric Laurent | 8b1e80b | 2014-10-07 09:08:47 -0700 | [diff] [blame] | 74 | { |
| 75 | Mutex::Autolock _l(mLock); |
Eric Laurent | 9357520 | 2011-01-18 18:39:02 -0800 | [diff] [blame] | 76 | |
Eric Laurent | 8b1e80b | 2014-10-07 09:08:47 -0700 | [diff] [blame] | 77 | // start tone playback thread |
| 78 | mTonePlaybackThread = new AudioCommandThread(String8("ApmTone"), this); |
| 79 | // start audio commands thread |
| 80 | mAudioCommandThread = new AudioCommandThread(String8("ApmAudio"), this); |
| 81 | // start output activity command thread |
| 82 | mOutputCommandThread = new AudioCommandThread(String8("ApmOutput"), this); |
Eric Laurent | dce54a1 | 2014-03-10 12:19:46 -0700 | [diff] [blame] | 83 | |
| 84 | #ifdef USE_LEGACY_AUDIO_POLICY |
Eric Laurent | 8b1e80b | 2014-10-07 09:08:47 -0700 | [diff] [blame] | 85 | ALOGI("AudioPolicyService CSTOR in legacy mode"); |
Eric Laurent | dce54a1 | 2014-03-10 12:19:46 -0700 | [diff] [blame] | 86 | |
Eric Laurent | 8b1e80b | 2014-10-07 09:08:47 -0700 | [diff] [blame] | 87 | /* instantiate the audio policy manager */ |
| 88 | rc = hw_get_module(AUDIO_POLICY_HARDWARE_MODULE_ID, &module); |
| 89 | if (rc) { |
| 90 | return; |
| 91 | } |
| 92 | rc = audio_policy_dev_open(module, &mpAudioPolicyDev); |
| 93 | ALOGE_IF(rc, "couldn't open audio policy device (%s)", strerror(-rc)); |
| 94 | if (rc) { |
| 95 | return; |
| 96 | } |
Eric Laurent | 9357520 | 2011-01-18 18:39:02 -0800 | [diff] [blame] | 97 | |
Eric Laurent | 8b1e80b | 2014-10-07 09:08:47 -0700 | [diff] [blame] | 98 | rc = mpAudioPolicyDev->create_audio_policy(mpAudioPolicyDev, &aps_ops, this, |
| 99 | &mpAudioPolicy); |
| 100 | ALOGE_IF(rc, "couldn't create audio policy (%s)", strerror(-rc)); |
| 101 | if (rc) { |
| 102 | return; |
| 103 | } |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 104 | |
Eric Laurent | 8b1e80b | 2014-10-07 09:08:47 -0700 | [diff] [blame] | 105 | rc = mpAudioPolicy->init_check(mpAudioPolicy); |
| 106 | ALOGE_IF(rc, "couldn't init_check the audio policy (%s)", strerror(-rc)); |
| 107 | if (rc) { |
| 108 | return; |
| 109 | } |
| 110 | ALOGI("Loaded audio policy from %s (%s)", module->name, module->id); |
Eric Laurent | dce54a1 | 2014-03-10 12:19:46 -0700 | [diff] [blame] | 111 | #else |
Eric Laurent | 8b1e80b | 2014-10-07 09:08:47 -0700 | [diff] [blame] | 112 | ALOGI("AudioPolicyService CSTOR in new mode"); |
Eric Laurent | dce54a1 | 2014-03-10 12:19:46 -0700 | [diff] [blame] | 113 | |
Eric Laurent | 8b1e80b | 2014-10-07 09:08:47 -0700 | [diff] [blame] | 114 | mAudioPolicyClient = new AudioPolicyClient(this); |
| 115 | mAudioPolicyManager = createAudioPolicyManager(mAudioPolicyClient); |
Eric Laurent | dce54a1 | 2014-03-10 12:19:46 -0700 | [diff] [blame] | 116 | #endif |
Eric Laurent | 8b1e80b | 2014-10-07 09:08:47 -0700 | [diff] [blame] | 117 | } |
bryant_liu | ba2b439 | 2014-06-11 16:49:30 +0800 | [diff] [blame] | 118 | // load audio processing modules |
Eric Laurent | 8b1e80b | 2014-10-07 09:08:47 -0700 | [diff] [blame] | 119 | sp<AudioPolicyEffects>audioPolicyEffects = new AudioPolicyEffects(); |
| 120 | { |
| 121 | Mutex::Autolock _l(mLock); |
| 122 | mAudioPolicyEffects = audioPolicyEffects; |
| 123 | } |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 124 | } |
| 125 | |
| 126 | AudioPolicyService::~AudioPolicyService() |
| 127 | { |
| 128 | mTonePlaybackThread->exit(); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 129 | mAudioCommandThread->exit(); |
Eric Laurent | 657ff61 | 2014-05-07 11:58:24 -0700 | [diff] [blame] | 130 | mOutputCommandThread->exit(); |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 131 | |
Eric Laurent | dce54a1 | 2014-03-10 12:19:46 -0700 | [diff] [blame] | 132 | #ifdef USE_LEGACY_AUDIO_POLICY |
Glenn Kasten | 6e2ebe9 | 2013-08-13 09:14:51 -0700 | [diff] [blame] | 133 | if (mpAudioPolicy != NULL && mpAudioPolicyDev != NULL) { |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 134 | mpAudioPolicyDev->destroy_audio_policy(mpAudioPolicyDev, mpAudioPolicy); |
Glenn Kasten | 6e2ebe9 | 2013-08-13 09:14:51 -0700 | [diff] [blame] | 135 | } |
| 136 | if (mpAudioPolicyDev != NULL) { |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 137 | audio_policy_dev_close(mpAudioPolicyDev); |
Glenn Kasten | 6e2ebe9 | 2013-08-13 09:14:51 -0700 | [diff] [blame] | 138 | } |
Eric Laurent | dce54a1 | 2014-03-10 12:19:46 -0700 | [diff] [blame] | 139 | #else |
Eric Laurent | f269b8e | 2014-06-09 20:01:29 -0700 | [diff] [blame] | 140 | destroyAudioPolicyManager(mAudioPolicyManager); |
Eric Laurent | dce54a1 | 2014-03-10 12:19:46 -0700 | [diff] [blame] | 141 | delete mAudioPolicyClient; |
| 142 | #endif |
Eric Laurent | b52c152 | 2014-05-20 11:27:36 -0700 | [diff] [blame] | 143 | |
| 144 | mNotificationClients.clear(); |
bryant_liu | ba2b439 | 2014-06-11 16:49:30 +0800 | [diff] [blame] | 145 | mAudioPolicyEffects.clear(); |
Eric Laurent | b52c152 | 2014-05-20 11:27:36 -0700 | [diff] [blame] | 146 | } |
| 147 | |
| 148 | // A notification client is always registered by AudioSystem when the client process |
| 149 | // connects to AudioPolicyService. |
| 150 | void AudioPolicyService::registerClient(const sp<IAudioPolicyServiceClient>& client) |
| 151 | { |
| 152 | |
Eric Laurent | 0ebd5f9 | 2014-11-19 19:04:52 -0800 | [diff] [blame] | 153 | Mutex::Autolock _l(mNotificationClientsLock); |
Eric Laurent | b52c152 | 2014-05-20 11:27:36 -0700 | [diff] [blame] | 154 | |
| 155 | uid_t uid = IPCThreadState::self()->getCallingUid(); |
| 156 | if (mNotificationClients.indexOfKey(uid) < 0) { |
| 157 | sp<NotificationClient> notificationClient = new NotificationClient(this, |
| 158 | client, |
| 159 | uid); |
| 160 | ALOGV("registerClient() client %p, uid %d", client.get(), uid); |
| 161 | |
| 162 | mNotificationClients.add(uid, notificationClient); |
| 163 | |
Marco Nelissen | f888020 | 2014-11-14 07:58:25 -0800 | [diff] [blame] | 164 | sp<IBinder> binder = IInterface::asBinder(client); |
Eric Laurent | b52c152 | 2014-05-20 11:27:36 -0700 | [diff] [blame] | 165 | binder->linkToDeath(notificationClient); |
| 166 | } |
| 167 | } |
| 168 | |
| 169 | // removeNotificationClient() is called when the client process dies. |
| 170 | void AudioPolicyService::removeNotificationClient(uid_t uid) |
| 171 | { |
Eric Laurent | 0ebd5f9 | 2014-11-19 19:04:52 -0800 | [diff] [blame] | 172 | { |
| 173 | Mutex::Autolock _l(mNotificationClientsLock); |
| 174 | mNotificationClients.removeItem(uid); |
| 175 | } |
Eric Laurent | b52c152 | 2014-05-20 11:27:36 -0700 | [diff] [blame] | 176 | #ifndef USE_LEGACY_AUDIO_POLICY |
Eric Laurent | 0ebd5f9 | 2014-11-19 19:04:52 -0800 | [diff] [blame] | 177 | { |
| 178 | Mutex::Autolock _l(mLock); |
Eric Laurent | b52c152 | 2014-05-20 11:27:36 -0700 | [diff] [blame] | 179 | if (mAudioPolicyManager) { |
| 180 | mAudioPolicyManager->clearAudioPatches(uid); |
| 181 | } |
Eric Laurent | 0ebd5f9 | 2014-11-19 19:04:52 -0800 | [diff] [blame] | 182 | } |
Eric Laurent | b52c152 | 2014-05-20 11:27:36 -0700 | [diff] [blame] | 183 | #endif |
| 184 | } |
| 185 | |
| 186 | void AudioPolicyService::onAudioPortListUpdate() |
| 187 | { |
| 188 | mOutputCommandThread->updateAudioPortListCommand(); |
| 189 | } |
| 190 | |
| 191 | void AudioPolicyService::doOnAudioPortListUpdate() |
| 192 | { |
Eric Laurent | 0ebd5f9 | 2014-11-19 19:04:52 -0800 | [diff] [blame] | 193 | Mutex::Autolock _l(mNotificationClientsLock); |
Eric Laurent | b52c152 | 2014-05-20 11:27:36 -0700 | [diff] [blame] | 194 | for (size_t i = 0; i < mNotificationClients.size(); i++) { |
| 195 | mNotificationClients.valueAt(i)->onAudioPortListUpdate(); |
| 196 | } |
| 197 | } |
| 198 | |
| 199 | void AudioPolicyService::onAudioPatchListUpdate() |
| 200 | { |
| 201 | mOutputCommandThread->updateAudioPatchListCommand(); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 202 | } |
| 203 | |
Eric Laurent | 951f455 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 204 | status_t AudioPolicyService::clientCreateAudioPatch(const struct audio_patch *patch, |
| 205 | audio_patch_handle_t *handle, |
| 206 | int delayMs) |
| 207 | { |
| 208 | return mAudioCommandThread->createAudioPatchCommand(patch, handle, delayMs); |
| 209 | } |
| 210 | |
| 211 | status_t AudioPolicyService::clientReleaseAudioPatch(audio_patch_handle_t handle, |
| 212 | int delayMs) |
| 213 | { |
| 214 | return mAudioCommandThread->releaseAudioPatchCommand(handle, delayMs); |
| 215 | } |
| 216 | |
Eric Laurent | b52c152 | 2014-05-20 11:27:36 -0700 | [diff] [blame] | 217 | void AudioPolicyService::doOnAudioPatchListUpdate() |
| 218 | { |
Eric Laurent | 0ebd5f9 | 2014-11-19 19:04:52 -0800 | [diff] [blame] | 219 | Mutex::Autolock _l(mNotificationClientsLock); |
Eric Laurent | b52c152 | 2014-05-20 11:27:36 -0700 | [diff] [blame] | 220 | for (size_t i = 0; i < mNotificationClients.size(); i++) { |
| 221 | mNotificationClients.valueAt(i)->onAudioPatchListUpdate(); |
| 222 | } |
| 223 | } |
| 224 | |
Eric Laurent | e1715a4 | 2014-05-20 11:30:42 -0700 | [diff] [blame] | 225 | status_t AudioPolicyService::clientSetAudioPortConfig(const struct audio_port_config *config, |
| 226 | int delayMs) |
| 227 | { |
| 228 | return mAudioCommandThread->setAudioPortConfigCommand(config, delayMs); |
| 229 | } |
| 230 | |
Eric Laurent | b52c152 | 2014-05-20 11:27:36 -0700 | [diff] [blame] | 231 | AudioPolicyService::NotificationClient::NotificationClient(const sp<AudioPolicyService>& service, |
| 232 | const sp<IAudioPolicyServiceClient>& client, |
| 233 | uid_t uid) |
| 234 | : mService(service), mUid(uid), mAudioPolicyServiceClient(client) |
| 235 | { |
| 236 | } |
| 237 | |
| 238 | AudioPolicyService::NotificationClient::~NotificationClient() |
| 239 | { |
| 240 | } |
| 241 | |
| 242 | void AudioPolicyService::NotificationClient::binderDied(const wp<IBinder>& who __unused) |
| 243 | { |
| 244 | sp<NotificationClient> keep(this); |
| 245 | sp<AudioPolicyService> service = mService.promote(); |
| 246 | if (service != 0) { |
| 247 | service->removeNotificationClient(mUid); |
| 248 | } |
| 249 | } |
| 250 | |
| 251 | void AudioPolicyService::NotificationClient::onAudioPortListUpdate() |
| 252 | { |
| 253 | if (mAudioPolicyServiceClient != 0) { |
| 254 | mAudioPolicyServiceClient->onAudioPortListUpdate(); |
| 255 | } |
| 256 | } |
| 257 | |
| 258 | void AudioPolicyService::NotificationClient::onAudioPatchListUpdate() |
| 259 | { |
| 260 | if (mAudioPolicyServiceClient != 0) { |
| 261 | mAudioPolicyServiceClient->onAudioPatchListUpdate(); |
| 262 | } |
| 263 | } |
Eric Laurent | 57dae99 | 2011-07-24 13:36:09 -0700 | [diff] [blame] | 264 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 265 | void AudioPolicyService::binderDied(const wp<IBinder>& who) { |
Glenn Kasten | 411e447 | 2012-11-02 10:00:06 -0700 | [diff] [blame] | 266 | ALOGW("binderDied() %p, calling pid %d", who.unsafe_get(), |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 267 | IPCThreadState::self()->getCallingPid()); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 268 | } |
| 269 | |
| 270 | static bool tryLock(Mutex& mutex) |
| 271 | { |
| 272 | bool locked = false; |
| 273 | for (int i = 0; i < kDumpLockRetries; ++i) { |
| 274 | if (mutex.tryLock() == NO_ERROR) { |
| 275 | locked = true; |
| 276 | break; |
| 277 | } |
Glenn Kasten | 22ecc91 | 2012-01-09 08:33:38 -0800 | [diff] [blame] | 278 | usleep(kDumpLockSleepUs); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 279 | } |
| 280 | return locked; |
| 281 | } |
| 282 | |
| 283 | status_t AudioPolicyService::dumpInternals(int fd) |
| 284 | { |
| 285 | const size_t SIZE = 256; |
| 286 | char buffer[SIZE]; |
| 287 | String8 result; |
| 288 | |
Eric Laurent | dce54a1 | 2014-03-10 12:19:46 -0700 | [diff] [blame] | 289 | #ifdef USE_LEGACY_AUDIO_POLICY |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 290 | snprintf(buffer, SIZE, "PolicyManager Interface: %p\n", mpAudioPolicy); |
Eric Laurent | dce54a1 | 2014-03-10 12:19:46 -0700 | [diff] [blame] | 291 | #else |
| 292 | snprintf(buffer, SIZE, "AudioPolicyManager: %p\n", mAudioPolicyManager); |
| 293 | #endif |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 294 | result.append(buffer); |
| 295 | snprintf(buffer, SIZE, "Command Thread: %p\n", mAudioCommandThread.get()); |
| 296 | result.append(buffer); |
| 297 | snprintf(buffer, SIZE, "Tones Thread: %p\n", mTonePlaybackThread.get()); |
| 298 | result.append(buffer); |
| 299 | |
| 300 | write(fd, result.string(), result.size()); |
| 301 | return NO_ERROR; |
| 302 | } |
| 303 | |
Glenn Kasten | 0f11b51 | 2014-01-31 16:18:54 -0800 | [diff] [blame] | 304 | status_t AudioPolicyService::dump(int fd, const Vector<String16>& args __unused) |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 305 | { |
Glenn Kasten | 44deb05 | 2012-02-05 18:09:08 -0800 | [diff] [blame] | 306 | if (!dumpAllowed()) { |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 307 | dumpPermissionDenial(fd); |
| 308 | } else { |
| 309 | bool locked = tryLock(mLock); |
| 310 | if (!locked) { |
| 311 | String8 result(kDeadlockedString); |
| 312 | write(fd, result.string(), result.size()); |
| 313 | } |
| 314 | |
| 315 | dumpInternals(fd); |
Glenn Kasten | 9d1f02d | 2012-02-08 17:47:58 -0800 | [diff] [blame] | 316 | if (mAudioCommandThread != 0) { |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 317 | mAudioCommandThread->dump(fd); |
| 318 | } |
Glenn Kasten | 9d1f02d | 2012-02-08 17:47:58 -0800 | [diff] [blame] | 319 | if (mTonePlaybackThread != 0) { |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 320 | mTonePlaybackThread->dump(fd); |
| 321 | } |
| 322 | |
Eric Laurent | dce54a1 | 2014-03-10 12:19:46 -0700 | [diff] [blame] | 323 | #ifdef USE_LEGACY_AUDIO_POLICY |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 324 | if (mpAudioPolicy) { |
| 325 | mpAudioPolicy->dump(mpAudioPolicy, fd); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 326 | } |
Eric Laurent | dce54a1 | 2014-03-10 12:19:46 -0700 | [diff] [blame] | 327 | #else |
| 328 | if (mAudioPolicyManager) { |
| 329 | mAudioPolicyManager->dump(fd); |
| 330 | } |
| 331 | #endif |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 332 | |
| 333 | if (locked) mLock.unlock(); |
| 334 | } |
| 335 | return NO_ERROR; |
| 336 | } |
| 337 | |
| 338 | status_t AudioPolicyService::dumpPermissionDenial(int fd) |
| 339 | { |
| 340 | const size_t SIZE = 256; |
| 341 | char buffer[SIZE]; |
| 342 | String8 result; |
| 343 | snprintf(buffer, SIZE, "Permission Denial: " |
| 344 | "can't dump AudioPolicyService from pid=%d, uid=%d\n", |
| 345 | IPCThreadState::self()->getCallingPid(), |
| 346 | IPCThreadState::self()->getCallingUid()); |
| 347 | result.append(buffer); |
| 348 | write(fd, result.string(), result.size()); |
| 349 | return NO_ERROR; |
| 350 | } |
| 351 | |
| 352 | status_t AudioPolicyService::onTransact( |
| 353 | uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags) |
| 354 | { |
| 355 | return BnAudioPolicyService::onTransact(code, data, reply, flags); |
| 356 | } |
| 357 | |
| 358 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 359 | // ----------- AudioPolicyService::AudioCommandThread implementation ---------- |
| 360 | |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 361 | AudioPolicyService::AudioCommandThread::AudioCommandThread(String8 name, |
| 362 | const wp<AudioPolicyService>& service) |
| 363 | : Thread(false), mName(name), mService(service) |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 364 | { |
| 365 | mpToneGenerator = NULL; |
| 366 | } |
| 367 | |
| 368 | |
| 369 | AudioPolicyService::AudioCommandThread::~AudioCommandThread() |
| 370 | { |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 371 | if (!mAudioCommands.isEmpty()) { |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 372 | release_wake_lock(mName.string()); |
| 373 | } |
| 374 | mAudioCommands.clear(); |
Glenn Kasten | e9dd017 | 2012-01-27 18:08:45 -0800 | [diff] [blame] | 375 | delete mpToneGenerator; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 376 | } |
| 377 | |
| 378 | void AudioPolicyService::AudioCommandThread::onFirstRef() |
| 379 | { |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 380 | run(mName.string(), ANDROID_PRIORITY_AUDIO); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 381 | } |
| 382 | |
| 383 | bool AudioPolicyService::AudioCommandThread::threadLoop() |
| 384 | { |
| 385 | nsecs_t waitTime = INT64_MAX; |
| 386 | |
| 387 | mLock.lock(); |
| 388 | while (!exitPending()) |
| 389 | { |
Eric Laurent | 59a8923 | 2014-06-08 14:14:17 -0700 | [diff] [blame] | 390 | sp<AudioPolicyService> svc; |
| 391 | while (!mAudioCommands.isEmpty() && !exitPending()) { |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 392 | nsecs_t curTime = systemTime(); |
| 393 | // commands are sorted by increasing time stamp: execute them from index 0 and up |
| 394 | if (mAudioCommands[0]->mTime <= curTime) { |
Eric Laurent | 0ede892 | 2014-05-09 18:04:42 -0700 | [diff] [blame] | 395 | sp<AudioCommand> command = mAudioCommands[0]; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 396 | mAudioCommands.removeAt(0); |
Eric Laurent | 0ede892 | 2014-05-09 18:04:42 -0700 | [diff] [blame] | 397 | mLastCommand = command; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 398 | |
| 399 | switch (command->mCommand) { |
| 400 | case START_TONE: { |
| 401 | mLock.unlock(); |
Eric Laurent | 0ede892 | 2014-05-09 18:04:42 -0700 | [diff] [blame] | 402 | ToneData *data = (ToneData *)command->mParam.get(); |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 403 | ALOGV("AudioCommandThread() processing start tone %d on stream %d", |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 404 | data->mType, data->mStream); |
Glenn Kasten | e9dd017 | 2012-01-27 18:08:45 -0800 | [diff] [blame] | 405 | delete mpToneGenerator; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 406 | mpToneGenerator = new ToneGenerator(data->mStream, 1.0); |
| 407 | mpToneGenerator->startTone(data->mType); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 408 | mLock.lock(); |
| 409 | }break; |
| 410 | case STOP_TONE: { |
| 411 | mLock.unlock(); |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 412 | ALOGV("AudioCommandThread() processing stop tone"); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 413 | if (mpToneGenerator != NULL) { |
| 414 | mpToneGenerator->stopTone(); |
| 415 | delete mpToneGenerator; |
| 416 | mpToneGenerator = NULL; |
| 417 | } |
| 418 | mLock.lock(); |
| 419 | }break; |
| 420 | case SET_VOLUME: { |
Eric Laurent | 0ede892 | 2014-05-09 18:04:42 -0700 | [diff] [blame] | 421 | VolumeData *data = (VolumeData *)command->mParam.get(); |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 422 | ALOGV("AudioCommandThread() processing set volume stream %d, \ |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 423 | volume %f, output %d", data->mStream, data->mVolume, data->mIO); |
| 424 | command->mStatus = AudioSystem::setStreamVolume(data->mStream, |
| 425 | data->mVolume, |
| 426 | data->mIO); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 427 | }break; |
| 428 | case SET_PARAMETERS: { |
Eric Laurent | 0ede892 | 2014-05-09 18:04:42 -0700 | [diff] [blame] | 429 | ParametersData *data = (ParametersData *)command->mParam.get(); |
Glenn Kasten | e53b9ea | 2012-03-12 16:29:55 -0700 | [diff] [blame] | 430 | ALOGV("AudioCommandThread() processing set parameters string %s, io %d", |
| 431 | data->mKeyValuePairs.string(), data->mIO); |
| 432 | command->mStatus = AudioSystem::setParameters(data->mIO, data->mKeyValuePairs); |
Glenn Kasten | e53b9ea | 2012-03-12 16:29:55 -0700 | [diff] [blame] | 433 | }break; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 434 | case SET_VOICE_VOLUME: { |
Eric Laurent | 0ede892 | 2014-05-09 18:04:42 -0700 | [diff] [blame] | 435 | VoiceVolumeData *data = (VoiceVolumeData *)command->mParam.get(); |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 436 | ALOGV("AudioCommandThread() processing set voice volume volume %f", |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 437 | data->mVolume); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 438 | command->mStatus = AudioSystem::setVoiceVolume(data->mVolume); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 439 | }break; |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 440 | case STOP_OUTPUT: { |
Eric Laurent | 0ede892 | 2014-05-09 18:04:42 -0700 | [diff] [blame] | 441 | StopOutputData *data = (StopOutputData *)command->mParam.get(); |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 442 | ALOGV("AudioCommandThread() processing stop output %d", |
| 443 | data->mIO); |
Eric Laurent | 59a8923 | 2014-06-08 14:14:17 -0700 | [diff] [blame] | 444 | svc = mService.promote(); |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 445 | if (svc == 0) { |
| 446 | break; |
| 447 | } |
| 448 | mLock.unlock(); |
| 449 | svc->doStopOutput(data->mIO, data->mStream, data->mSession); |
| 450 | mLock.lock(); |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 451 | }break; |
| 452 | case RELEASE_OUTPUT: { |
Eric Laurent | 0ede892 | 2014-05-09 18:04:42 -0700 | [diff] [blame] | 453 | ReleaseOutputData *data = (ReleaseOutputData *)command->mParam.get(); |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 454 | ALOGV("AudioCommandThread() processing release output %d", |
| 455 | data->mIO); |
Eric Laurent | 59a8923 | 2014-06-08 14:14:17 -0700 | [diff] [blame] | 456 | svc = mService.promote(); |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 457 | if (svc == 0) { |
| 458 | break; |
| 459 | } |
| 460 | mLock.unlock(); |
Eric Laurent | e83b55d | 2014-11-14 10:06:21 -0800 | [diff] [blame] | 461 | svc->doReleaseOutput(data->mIO, data->mStream, data->mSession); |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 462 | mLock.lock(); |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 463 | }break; |
Eric Laurent | 951f455 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 464 | case CREATE_AUDIO_PATCH: { |
| 465 | CreateAudioPatchData *data = (CreateAudioPatchData *)command->mParam.get(); |
| 466 | ALOGV("AudioCommandThread() processing create audio patch"); |
| 467 | sp<IAudioFlinger> af = AudioSystem::get_audio_flinger(); |
| 468 | if (af == 0) { |
| 469 | command->mStatus = PERMISSION_DENIED; |
| 470 | } else { |
| 471 | command->mStatus = af->createAudioPatch(&data->mPatch, &data->mHandle); |
| 472 | } |
| 473 | } break; |
| 474 | case RELEASE_AUDIO_PATCH: { |
| 475 | ReleaseAudioPatchData *data = (ReleaseAudioPatchData *)command->mParam.get(); |
| 476 | ALOGV("AudioCommandThread() processing release audio patch"); |
| 477 | sp<IAudioFlinger> af = AudioSystem::get_audio_flinger(); |
| 478 | if (af == 0) { |
| 479 | command->mStatus = PERMISSION_DENIED; |
| 480 | } else { |
| 481 | command->mStatus = af->releaseAudioPatch(data->mHandle); |
| 482 | } |
| 483 | } break; |
Eric Laurent | b52c152 | 2014-05-20 11:27:36 -0700 | [diff] [blame] | 484 | case UPDATE_AUDIOPORT_LIST: { |
| 485 | ALOGV("AudioCommandThread() processing update audio port list"); |
Eric Laurent | 59a8923 | 2014-06-08 14:14:17 -0700 | [diff] [blame] | 486 | svc = mService.promote(); |
Eric Laurent | b52c152 | 2014-05-20 11:27:36 -0700 | [diff] [blame] | 487 | if (svc == 0) { |
| 488 | break; |
| 489 | } |
| 490 | mLock.unlock(); |
| 491 | svc->doOnAudioPortListUpdate(); |
| 492 | mLock.lock(); |
| 493 | }break; |
| 494 | case UPDATE_AUDIOPATCH_LIST: { |
| 495 | ALOGV("AudioCommandThread() processing update audio patch list"); |
Eric Laurent | 59a8923 | 2014-06-08 14:14:17 -0700 | [diff] [blame] | 496 | svc = mService.promote(); |
Eric Laurent | b52c152 | 2014-05-20 11:27:36 -0700 | [diff] [blame] | 497 | if (svc == 0) { |
| 498 | break; |
| 499 | } |
| 500 | mLock.unlock(); |
| 501 | svc->doOnAudioPatchListUpdate(); |
| 502 | mLock.lock(); |
| 503 | }break; |
Eric Laurent | e1715a4 | 2014-05-20 11:30:42 -0700 | [diff] [blame] | 504 | case SET_AUDIOPORT_CONFIG: { |
| 505 | SetAudioPortConfigData *data = (SetAudioPortConfigData *)command->mParam.get(); |
| 506 | ALOGV("AudioCommandThread() processing set port config"); |
| 507 | sp<IAudioFlinger> af = AudioSystem::get_audio_flinger(); |
| 508 | if (af == 0) { |
| 509 | command->mStatus = PERMISSION_DENIED; |
| 510 | } else { |
| 511 | command->mStatus = af->setAudioPortConfig(&data->mConfig); |
| 512 | } |
| 513 | } break; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 514 | default: |
Steve Block | 5ff1dd5 | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 515 | ALOGW("AudioCommandThread() unknown command %d", command->mCommand); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 516 | } |
Eric Laurent | 0ede892 | 2014-05-09 18:04:42 -0700 | [diff] [blame] | 517 | { |
| 518 | Mutex::Autolock _l(command->mLock); |
| 519 | if (command->mWaitStatus) { |
| 520 | command->mWaitStatus = false; |
| 521 | command->mCond.signal(); |
| 522 | } |
| 523 | } |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 524 | waitTime = INT64_MAX; |
| 525 | } else { |
| 526 | waitTime = mAudioCommands[0]->mTime - curTime; |
| 527 | break; |
| 528 | } |
| 529 | } |
Eric Laurent | 59a8923 | 2014-06-08 14:14:17 -0700 | [diff] [blame] | 530 | // release mLock before releasing strong reference on the service as |
| 531 | // AudioPolicyService destructor calls AudioCommandThread::exit() which acquires mLock. |
| 532 | mLock.unlock(); |
| 533 | svc.clear(); |
| 534 | mLock.lock(); |
Ricardo Garcia | 05f2fdc | 2014-07-24 15:48:24 -0700 | [diff] [blame] | 535 | if (!exitPending() && mAudioCommands.isEmpty()) { |
| 536 | // release delayed commands wake lock |
| 537 | release_wake_lock(mName.string()); |
Eric Laurent | 59a8923 | 2014-06-08 14:14:17 -0700 | [diff] [blame] | 538 | ALOGV("AudioCommandThread() going to sleep"); |
| 539 | mWaitWorkCV.waitRelative(mLock, waitTime); |
| 540 | ALOGV("AudioCommandThread() waking up"); |
| 541 | } |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 542 | } |
Ricardo Garcia | 05f2fdc | 2014-07-24 15:48:24 -0700 | [diff] [blame] | 543 | // release delayed commands wake lock before quitting |
| 544 | if (!mAudioCommands.isEmpty()) { |
| 545 | release_wake_lock(mName.string()); |
| 546 | } |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 547 | mLock.unlock(); |
| 548 | return false; |
| 549 | } |
| 550 | |
| 551 | status_t AudioPolicyService::AudioCommandThread::dump(int fd) |
| 552 | { |
| 553 | const size_t SIZE = 256; |
| 554 | char buffer[SIZE]; |
| 555 | String8 result; |
| 556 | |
| 557 | snprintf(buffer, SIZE, "AudioCommandThread %p Dump\n", this); |
| 558 | result.append(buffer); |
| 559 | write(fd, result.string(), result.size()); |
| 560 | |
| 561 | bool locked = tryLock(mLock); |
| 562 | if (!locked) { |
| 563 | String8 result2(kCmdDeadlockedString); |
| 564 | write(fd, result2.string(), result2.size()); |
| 565 | } |
| 566 | |
| 567 | snprintf(buffer, SIZE, "- Commands:\n"); |
| 568 | result = String8(buffer); |
| 569 | result.append(" Command Time Wait pParam\n"); |
Glenn Kasten | 8d6a244 | 2012-02-08 14:04:28 -0800 | [diff] [blame] | 570 | for (size_t i = 0; i < mAudioCommands.size(); i++) { |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 571 | mAudioCommands[i]->dump(buffer, SIZE); |
| 572 | result.append(buffer); |
| 573 | } |
| 574 | result.append(" Last Command\n"); |
Eric Laurent | 0ede892 | 2014-05-09 18:04:42 -0700 | [diff] [blame] | 575 | if (mLastCommand != 0) { |
| 576 | mLastCommand->dump(buffer, SIZE); |
| 577 | result.append(buffer); |
| 578 | } else { |
| 579 | result.append(" none\n"); |
| 580 | } |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 581 | |
| 582 | write(fd, result.string(), result.size()); |
| 583 | |
| 584 | if (locked) mLock.unlock(); |
| 585 | |
| 586 | return NO_ERROR; |
| 587 | } |
| 588 | |
Glenn Kasten | 3d2f877 | 2012-01-27 15:25:25 -0800 | [diff] [blame] | 589 | void AudioPolicyService::AudioCommandThread::startToneCommand(ToneGenerator::tone_type type, |
| 590 | audio_stream_type_t stream) |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 591 | { |
Eric Laurent | 0ede892 | 2014-05-09 18:04:42 -0700 | [diff] [blame] | 592 | sp<AudioCommand> command = new AudioCommand(); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 593 | command->mCommand = START_TONE; |
Eric Laurent | 0ede892 | 2014-05-09 18:04:42 -0700 | [diff] [blame] | 594 | sp<ToneData> data = new ToneData(); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 595 | data->mType = type; |
| 596 | data->mStream = stream; |
Jesper Tragardh | 48412dc | 2014-03-24 14:12:43 +0100 | [diff] [blame] | 597 | command->mParam = data; |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 598 | ALOGV("AudioCommandThread() adding tone start type %d, stream %d", type, stream); |
Eric Laurent | 0ede892 | 2014-05-09 18:04:42 -0700 | [diff] [blame] | 599 | sendCommand(command); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 600 | } |
| 601 | |
| 602 | void AudioPolicyService::AudioCommandThread::stopToneCommand() |
| 603 | { |
Eric Laurent | 0ede892 | 2014-05-09 18:04:42 -0700 | [diff] [blame] | 604 | sp<AudioCommand> command = new AudioCommand(); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 605 | command->mCommand = STOP_TONE; |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 606 | ALOGV("AudioCommandThread() adding tone stop"); |
Eric Laurent | 0ede892 | 2014-05-09 18:04:42 -0700 | [diff] [blame] | 607 | sendCommand(command); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 608 | } |
| 609 | |
Glenn Kasten | fff6d71 | 2012-01-12 16:38:12 -0800 | [diff] [blame] | 610 | status_t AudioPolicyService::AudioCommandThread::volumeCommand(audio_stream_type_t stream, |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 611 | float volume, |
Glenn Kasten | 72ef00d | 2012-01-17 11:09:42 -0800 | [diff] [blame] | 612 | audio_io_handle_t output, |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 613 | int delayMs) |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 614 | { |
Eric Laurent | 0ede892 | 2014-05-09 18:04:42 -0700 | [diff] [blame] | 615 | sp<AudioCommand> command = new AudioCommand(); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 616 | command->mCommand = SET_VOLUME; |
Eric Laurent | 0ede892 | 2014-05-09 18:04:42 -0700 | [diff] [blame] | 617 | sp<VolumeData> data = new VolumeData(); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 618 | data->mStream = stream; |
| 619 | data->mVolume = volume; |
| 620 | data->mIO = output; |
| 621 | command->mParam = data; |
Eric Laurent | 0ede892 | 2014-05-09 18:04:42 -0700 | [diff] [blame] | 622 | command->mWaitStatus = true; |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 623 | ALOGV("AudioCommandThread() adding set volume stream %d, volume %f, output %d", |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 624 | stream, volume, output); |
Eric Laurent | 0ede892 | 2014-05-09 18:04:42 -0700 | [diff] [blame] | 625 | return sendCommand(command, delayMs); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 626 | } |
| 627 | |
Glenn Kasten | 72ef00d | 2012-01-17 11:09:42 -0800 | [diff] [blame] | 628 | status_t AudioPolicyService::AudioCommandThread::parametersCommand(audio_io_handle_t ioHandle, |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 629 | const char *keyValuePairs, |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 630 | int delayMs) |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 631 | { |
Eric Laurent | 0ede892 | 2014-05-09 18:04:42 -0700 | [diff] [blame] | 632 | sp<AudioCommand> command = new AudioCommand(); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 633 | command->mCommand = SET_PARAMETERS; |
Eric Laurent | 0ede892 | 2014-05-09 18:04:42 -0700 | [diff] [blame] | 634 | sp<ParametersData> data = new ParametersData(); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 635 | data->mIO = ioHandle; |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 636 | data->mKeyValuePairs = String8(keyValuePairs); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 637 | command->mParam = data; |
Eric Laurent | 0ede892 | 2014-05-09 18:04:42 -0700 | [diff] [blame] | 638 | command->mWaitStatus = true; |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 639 | ALOGV("AudioCommandThread() adding set parameter string %s, io %d ,delay %d", |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 640 | keyValuePairs, ioHandle, delayMs); |
Eric Laurent | 0ede892 | 2014-05-09 18:04:42 -0700 | [diff] [blame] | 641 | return sendCommand(command, delayMs); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 642 | } |
| 643 | |
| 644 | status_t AudioPolicyService::AudioCommandThread::voiceVolumeCommand(float volume, int delayMs) |
| 645 | { |
Eric Laurent | 0ede892 | 2014-05-09 18:04:42 -0700 | [diff] [blame] | 646 | sp<AudioCommand> command = new AudioCommand(); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 647 | command->mCommand = SET_VOICE_VOLUME; |
Eric Laurent | 0ede892 | 2014-05-09 18:04:42 -0700 | [diff] [blame] | 648 | sp<VoiceVolumeData> data = new VoiceVolumeData(); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 649 | data->mVolume = volume; |
| 650 | command->mParam = data; |
Eric Laurent | 0ede892 | 2014-05-09 18:04:42 -0700 | [diff] [blame] | 651 | command->mWaitStatus = true; |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 652 | ALOGV("AudioCommandThread() adding set voice volume volume %f", volume); |
Eric Laurent | 0ede892 | 2014-05-09 18:04:42 -0700 | [diff] [blame] | 653 | return sendCommand(command, delayMs); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 654 | } |
| 655 | |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 656 | void AudioPolicyService::AudioCommandThread::stopOutputCommand(audio_io_handle_t output, |
| 657 | audio_stream_type_t stream, |
Eric Laurent | e83b55d | 2014-11-14 10:06:21 -0800 | [diff] [blame] | 658 | audio_session_t session) |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 659 | { |
Eric Laurent | 0ede892 | 2014-05-09 18:04:42 -0700 | [diff] [blame] | 660 | sp<AudioCommand> command = new AudioCommand(); |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 661 | command->mCommand = STOP_OUTPUT; |
Eric Laurent | 0ede892 | 2014-05-09 18:04:42 -0700 | [diff] [blame] | 662 | sp<StopOutputData> data = new StopOutputData(); |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 663 | data->mIO = output; |
| 664 | data->mStream = stream; |
| 665 | data->mSession = session; |
Jesper Tragardh | 48412dc | 2014-03-24 14:12:43 +0100 | [diff] [blame] | 666 | command->mParam = data; |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 667 | ALOGV("AudioCommandThread() adding stop output %d", output); |
Eric Laurent | 0ede892 | 2014-05-09 18:04:42 -0700 | [diff] [blame] | 668 | sendCommand(command); |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 669 | } |
| 670 | |
Eric Laurent | e83b55d | 2014-11-14 10:06:21 -0800 | [diff] [blame] | 671 | void AudioPolicyService::AudioCommandThread::releaseOutputCommand(audio_io_handle_t output, |
| 672 | audio_stream_type_t stream, |
| 673 | audio_session_t session) |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 674 | { |
Eric Laurent | 0ede892 | 2014-05-09 18:04:42 -0700 | [diff] [blame] | 675 | sp<AudioCommand> command = new AudioCommand(); |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 676 | command->mCommand = RELEASE_OUTPUT; |
Eric Laurent | 0ede892 | 2014-05-09 18:04:42 -0700 | [diff] [blame] | 677 | sp<ReleaseOutputData> data = new ReleaseOutputData(); |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 678 | data->mIO = output; |
Eric Laurent | e83b55d | 2014-11-14 10:06:21 -0800 | [diff] [blame] | 679 | data->mStream = stream; |
| 680 | data->mSession = session; |
Jesper Tragardh | 48412dc | 2014-03-24 14:12:43 +0100 | [diff] [blame] | 681 | command->mParam = data; |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 682 | ALOGV("AudioCommandThread() adding release output %d", output); |
Eric Laurent | 0ede892 | 2014-05-09 18:04:42 -0700 | [diff] [blame] | 683 | sendCommand(command); |
| 684 | } |
| 685 | |
Eric Laurent | 951f455 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 686 | status_t AudioPolicyService::AudioCommandThread::createAudioPatchCommand( |
| 687 | const struct audio_patch *patch, |
| 688 | audio_patch_handle_t *handle, |
| 689 | int delayMs) |
| 690 | { |
| 691 | status_t status = NO_ERROR; |
| 692 | |
| 693 | sp<AudioCommand> command = new AudioCommand(); |
| 694 | command->mCommand = CREATE_AUDIO_PATCH; |
| 695 | CreateAudioPatchData *data = new CreateAudioPatchData(); |
| 696 | data->mPatch = *patch; |
| 697 | data->mHandle = *handle; |
| 698 | command->mParam = data; |
| 699 | command->mWaitStatus = true; |
| 700 | ALOGV("AudioCommandThread() adding create patch delay %d", delayMs); |
| 701 | status = sendCommand(command, delayMs); |
| 702 | if (status == NO_ERROR) { |
| 703 | *handle = data->mHandle; |
| 704 | } |
| 705 | return status; |
| 706 | } |
| 707 | |
| 708 | status_t AudioPolicyService::AudioCommandThread::releaseAudioPatchCommand(audio_patch_handle_t handle, |
| 709 | int delayMs) |
| 710 | { |
| 711 | sp<AudioCommand> command = new AudioCommand(); |
| 712 | command->mCommand = RELEASE_AUDIO_PATCH; |
| 713 | ReleaseAudioPatchData *data = new ReleaseAudioPatchData(); |
| 714 | data->mHandle = handle; |
| 715 | command->mParam = data; |
| 716 | command->mWaitStatus = true; |
| 717 | ALOGV("AudioCommandThread() adding release patch delay %d", delayMs); |
| 718 | return sendCommand(command, delayMs); |
| 719 | } |
| 720 | |
Eric Laurent | b52c152 | 2014-05-20 11:27:36 -0700 | [diff] [blame] | 721 | void AudioPolicyService::AudioCommandThread::updateAudioPortListCommand() |
| 722 | { |
| 723 | sp<AudioCommand> command = new AudioCommand(); |
| 724 | command->mCommand = UPDATE_AUDIOPORT_LIST; |
| 725 | ALOGV("AudioCommandThread() adding update audio port list"); |
| 726 | sendCommand(command); |
| 727 | } |
| 728 | |
| 729 | void AudioPolicyService::AudioCommandThread::updateAudioPatchListCommand() |
| 730 | { |
| 731 | sp<AudioCommand>command = new AudioCommand(); |
| 732 | command->mCommand = UPDATE_AUDIOPATCH_LIST; |
| 733 | ALOGV("AudioCommandThread() adding update audio patch list"); |
| 734 | sendCommand(command); |
| 735 | } |
| 736 | |
Eric Laurent | e1715a4 | 2014-05-20 11:30:42 -0700 | [diff] [blame] | 737 | status_t AudioPolicyService::AudioCommandThread::setAudioPortConfigCommand( |
| 738 | const struct audio_port_config *config, int delayMs) |
| 739 | { |
| 740 | sp<AudioCommand> command = new AudioCommand(); |
| 741 | command->mCommand = SET_AUDIOPORT_CONFIG; |
| 742 | SetAudioPortConfigData *data = new SetAudioPortConfigData(); |
| 743 | data->mConfig = *config; |
| 744 | command->mParam = data; |
| 745 | command->mWaitStatus = true; |
| 746 | ALOGV("AudioCommandThread() adding set port config delay %d", delayMs); |
| 747 | return sendCommand(command, delayMs); |
| 748 | } |
| 749 | |
Eric Laurent | 0ede892 | 2014-05-09 18:04:42 -0700 | [diff] [blame] | 750 | status_t AudioPolicyService::AudioCommandThread::sendCommand(sp<AudioCommand>& command, int delayMs) |
| 751 | { |
| 752 | { |
| 753 | Mutex::Autolock _l(mLock); |
| 754 | insertCommand_l(command, delayMs); |
| 755 | mWaitWorkCV.signal(); |
| 756 | } |
| 757 | Mutex::Autolock _l(command->mLock); |
| 758 | while (command->mWaitStatus) { |
| 759 | nsecs_t timeOutNs = kAudioCommandTimeoutNs + milliseconds(delayMs); |
| 760 | if (command->mCond.waitRelative(command->mLock, timeOutNs) != NO_ERROR) { |
| 761 | command->mStatus = TIMED_OUT; |
| 762 | command->mWaitStatus = false; |
| 763 | } |
| 764 | } |
| 765 | return command->mStatus; |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 766 | } |
| 767 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 768 | // insertCommand_l() must be called with mLock held |
Eric Laurent | 0ede892 | 2014-05-09 18:04:42 -0700 | [diff] [blame] | 769 | void AudioPolicyService::AudioCommandThread::insertCommand_l(sp<AudioCommand>& command, int delayMs) |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 770 | { |
Glenn Kasten | 8d6a244 | 2012-02-08 14:04:28 -0800 | [diff] [blame] | 771 | ssize_t i; // not size_t because i will count down to -1 |
Eric Laurent | 0ede892 | 2014-05-09 18:04:42 -0700 | [diff] [blame] | 772 | Vector < sp<AudioCommand> > removedCommands; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 773 | command->mTime = systemTime() + milliseconds(delayMs); |
| 774 | |
| 775 | // acquire wake lock to make sure delayed commands are processed |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 776 | if (mAudioCommands.isEmpty()) { |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 777 | acquire_wake_lock(PARTIAL_WAKE_LOCK, mName.string()); |
| 778 | } |
| 779 | |
| 780 | // check same pending commands with later time stamps and eliminate them |
| 781 | for (i = mAudioCommands.size()-1; i >= 0; i--) { |
Eric Laurent | 0ede892 | 2014-05-09 18:04:42 -0700 | [diff] [blame] | 782 | sp<AudioCommand> command2 = mAudioCommands[i]; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 783 | // commands are sorted by increasing time stamp: no need to scan the rest of mAudioCommands |
| 784 | if (command2->mTime <= command->mTime) break; |
Eric Laurent | e45b48a | 2014-09-04 16:40:57 -0700 | [diff] [blame] | 785 | |
| 786 | // create audio patch or release audio patch commands are equivalent |
| 787 | // with regard to filtering |
| 788 | if ((command->mCommand == CREATE_AUDIO_PATCH) || |
| 789 | (command->mCommand == RELEASE_AUDIO_PATCH)) { |
| 790 | if ((command2->mCommand != CREATE_AUDIO_PATCH) && |
| 791 | (command2->mCommand != RELEASE_AUDIO_PATCH)) { |
| 792 | continue; |
| 793 | } |
| 794 | } else if (command2->mCommand != command->mCommand) continue; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 795 | |
| 796 | switch (command->mCommand) { |
| 797 | case SET_PARAMETERS: { |
Eric Laurent | 0ede892 | 2014-05-09 18:04:42 -0700 | [diff] [blame] | 798 | ParametersData *data = (ParametersData *)command->mParam.get(); |
| 799 | ParametersData *data2 = (ParametersData *)command2->mParam.get(); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 800 | if (data->mIO != data2->mIO) break; |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 801 | ALOGV("Comparing parameter command %s to new command %s", |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 802 | data2->mKeyValuePairs.string(), data->mKeyValuePairs.string()); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 803 | AudioParameter param = AudioParameter(data->mKeyValuePairs); |
| 804 | AudioParameter param2 = AudioParameter(data2->mKeyValuePairs); |
| 805 | for (size_t j = 0; j < param.size(); j++) { |
Glenn Kasten | e53b9ea | 2012-03-12 16:29:55 -0700 | [diff] [blame] | 806 | String8 key; |
| 807 | String8 value; |
| 808 | param.getAt(j, key, value); |
| 809 | for (size_t k = 0; k < param2.size(); k++) { |
| 810 | String8 key2; |
| 811 | String8 value2; |
| 812 | param2.getAt(k, key2, value2); |
| 813 | if (key2 == key) { |
| 814 | param2.remove(key2); |
| 815 | ALOGV("Filtering out parameter %s", key2.string()); |
| 816 | break; |
| 817 | } |
| 818 | } |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 819 | } |
| 820 | // if all keys have been filtered out, remove the command. |
| 821 | // otherwise, update the key value pairs |
| 822 | if (param2.size() == 0) { |
| 823 | removedCommands.add(command2); |
| 824 | } else { |
| 825 | data2->mKeyValuePairs = param2.toString(); |
| 826 | } |
Eric Laurent | 21e5456 | 2013-09-23 12:08:05 -0700 | [diff] [blame] | 827 | command->mTime = command2->mTime; |
| 828 | // force delayMs to non 0 so that code below does not request to wait for |
| 829 | // command status as the command is now delayed |
| 830 | delayMs = 1; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 831 | } break; |
| 832 | |
| 833 | case SET_VOLUME: { |
Eric Laurent | 0ede892 | 2014-05-09 18:04:42 -0700 | [diff] [blame] | 834 | VolumeData *data = (VolumeData *)command->mParam.get(); |
| 835 | VolumeData *data2 = (VolumeData *)command2->mParam.get(); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 836 | if (data->mIO != data2->mIO) break; |
| 837 | if (data->mStream != data2->mStream) break; |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 838 | ALOGV("Filtering out volume command on output %d for stream %d", |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 839 | data->mIO, data->mStream); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 840 | removedCommands.add(command2); |
Eric Laurent | 21e5456 | 2013-09-23 12:08:05 -0700 | [diff] [blame] | 841 | command->mTime = command2->mTime; |
| 842 | // force delayMs to non 0 so that code below does not request to wait for |
| 843 | // command status as the command is now delayed |
| 844 | delayMs = 1; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 845 | } break; |
Eric Laurent | e45b48a | 2014-09-04 16:40:57 -0700 | [diff] [blame] | 846 | |
| 847 | case CREATE_AUDIO_PATCH: |
| 848 | case RELEASE_AUDIO_PATCH: { |
| 849 | audio_patch_handle_t handle; |
Haynes Mathew George | a2d4a6d | 2014-10-13 13:05:22 -0700 | [diff] [blame] | 850 | struct audio_patch patch; |
Eric Laurent | e45b48a | 2014-09-04 16:40:57 -0700 | [diff] [blame] | 851 | if (command->mCommand == CREATE_AUDIO_PATCH) { |
| 852 | handle = ((CreateAudioPatchData *)command->mParam.get())->mHandle; |
Haynes Mathew George | a2d4a6d | 2014-10-13 13:05:22 -0700 | [diff] [blame] | 853 | patch = ((CreateAudioPatchData *)command->mParam.get())->mPatch; |
Eric Laurent | e45b48a | 2014-09-04 16:40:57 -0700 | [diff] [blame] | 854 | } else { |
| 855 | handle = ((ReleaseAudioPatchData *)command->mParam.get())->mHandle; |
| 856 | } |
| 857 | audio_patch_handle_t handle2; |
Haynes Mathew George | a2d4a6d | 2014-10-13 13:05:22 -0700 | [diff] [blame] | 858 | struct audio_patch patch2; |
Eric Laurent | e45b48a | 2014-09-04 16:40:57 -0700 | [diff] [blame] | 859 | if (command2->mCommand == CREATE_AUDIO_PATCH) { |
| 860 | handle2 = ((CreateAudioPatchData *)command2->mParam.get())->mHandle; |
Haynes Mathew George | a2d4a6d | 2014-10-13 13:05:22 -0700 | [diff] [blame] | 861 | patch2 = ((CreateAudioPatchData *)command2->mParam.get())->mPatch; |
Eric Laurent | e45b48a | 2014-09-04 16:40:57 -0700 | [diff] [blame] | 862 | } else { |
| 863 | handle2 = ((ReleaseAudioPatchData *)command2->mParam.get())->mHandle; |
| 864 | } |
| 865 | if (handle != handle2) break; |
Haynes Mathew George | a2d4a6d | 2014-10-13 13:05:22 -0700 | [diff] [blame] | 866 | /* Filter CREATE_AUDIO_PATCH commands only when they are issued for |
| 867 | same output. */ |
| 868 | if( (command->mCommand == CREATE_AUDIO_PATCH) && |
| 869 | (command2->mCommand == CREATE_AUDIO_PATCH) ) { |
| 870 | bool isOutputDiff = false; |
| 871 | if (patch.num_sources == patch2.num_sources) { |
| 872 | for (unsigned count = 0; count < patch.num_sources; count++) { |
| 873 | if (patch.sources[count].id != patch2.sources[count].id) { |
| 874 | isOutputDiff = true; |
| 875 | break; |
| 876 | } |
| 877 | } |
| 878 | if (isOutputDiff) |
| 879 | break; |
| 880 | } |
| 881 | } |
Eric Laurent | e45b48a | 2014-09-04 16:40:57 -0700 | [diff] [blame] | 882 | ALOGV("Filtering out %s audio patch command for handle %d", |
| 883 | (command->mCommand == CREATE_AUDIO_PATCH) ? "create" : "release", handle); |
| 884 | removedCommands.add(command2); |
| 885 | command->mTime = command2->mTime; |
| 886 | // force delayMs to non 0 so that code below does not request to wait for |
| 887 | // command status as the command is now delayed |
| 888 | delayMs = 1; |
| 889 | } break; |
| 890 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 891 | case START_TONE: |
| 892 | case STOP_TONE: |
| 893 | default: |
| 894 | break; |
| 895 | } |
| 896 | } |
| 897 | |
| 898 | // remove filtered commands |
| 899 | for (size_t j = 0; j < removedCommands.size(); j++) { |
| 900 | // removed commands always have time stamps greater than current command |
| 901 | for (size_t k = i + 1; k < mAudioCommands.size(); k++) { |
Eric Laurent | 0ede892 | 2014-05-09 18:04:42 -0700 | [diff] [blame] | 902 | if (mAudioCommands[k].get() == removedCommands[j].get()) { |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 903 | ALOGV("suppressing command: %d", mAudioCommands[k]->mCommand); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 904 | mAudioCommands.removeAt(k); |
| 905 | break; |
| 906 | } |
| 907 | } |
| 908 | } |
| 909 | removedCommands.clear(); |
| 910 | |
Eric Laurent | aa79bef | 2015-01-15 14:33:51 -0800 | [diff] [blame] | 911 | // Disable wait for status if delay is not 0. |
| 912 | // Except for create audio patch command because the returned patch handle |
| 913 | // is needed by audio policy manager |
| 914 | if (delayMs != 0 && command->mCommand != CREATE_AUDIO_PATCH) { |
Eric Laurent | cec4abb | 2012-07-03 12:23:02 -0700 | [diff] [blame] | 915 | command->mWaitStatus = false; |
| 916 | } |
Eric Laurent | cec4abb | 2012-07-03 12:23:02 -0700 | [diff] [blame] | 917 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 918 | // insert command at the right place according to its time stamp |
Eric Laurent | 1e693b5 | 2014-07-09 15:03:28 -0700 | [diff] [blame] | 919 | ALOGV("inserting command: %d at index %zd, num commands %zu", |
| 920 | command->mCommand, i+1, mAudioCommands.size()); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 921 | mAudioCommands.insertAt(command, i + 1); |
| 922 | } |
| 923 | |
| 924 | void AudioPolicyService::AudioCommandThread::exit() |
| 925 | { |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 926 | ALOGV("AudioCommandThread::exit"); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 927 | { |
| 928 | AutoMutex _l(mLock); |
| 929 | requestExit(); |
| 930 | mWaitWorkCV.signal(); |
| 931 | } |
| 932 | requestExitAndWait(); |
| 933 | } |
| 934 | |
| 935 | void AudioPolicyService::AudioCommandThread::AudioCommand::dump(char* buffer, size_t size) |
| 936 | { |
| 937 | snprintf(buffer, size, " %02d %06d.%03d %01u %p\n", |
| 938 | mCommand, |
| 939 | (int)ns2s(mTime), |
| 940 | (int)ns2ms(mTime)%1000, |
| 941 | mWaitStatus, |
Eric Laurent | 0ede892 | 2014-05-09 18:04:42 -0700 | [diff] [blame] | 942 | mParam.get()); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 943 | } |
| 944 | |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 945 | /******* helpers for the service_ops callbacks defined below *********/ |
| 946 | void AudioPolicyService::setParameters(audio_io_handle_t ioHandle, |
| 947 | const char *keyValuePairs, |
| 948 | int delayMs) |
| 949 | { |
Glenn Kasten | 72ef00d | 2012-01-17 11:09:42 -0800 | [diff] [blame] | 950 | mAudioCommandThread->parametersCommand(ioHandle, keyValuePairs, |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 951 | delayMs); |
| 952 | } |
| 953 | |
| 954 | int AudioPolicyService::setStreamVolume(audio_stream_type_t stream, |
| 955 | float volume, |
| 956 | audio_io_handle_t output, |
| 957 | int delayMs) |
| 958 | { |
Glenn Kasten | fff6d71 | 2012-01-12 16:38:12 -0800 | [diff] [blame] | 959 | return (int)mAudioCommandThread->volumeCommand(stream, volume, |
Glenn Kasten | 72ef00d | 2012-01-17 11:09:42 -0800 | [diff] [blame] | 960 | output, delayMs); |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 961 | } |
| 962 | |
| 963 | int AudioPolicyService::startTone(audio_policy_tone_t tone, |
| 964 | audio_stream_type_t stream) |
| 965 | { |
Glenn Kasten | 6e2ebe9 | 2013-08-13 09:14:51 -0700 | [diff] [blame] | 966 | if (tone != AUDIO_POLICY_TONE_IN_CALL_NOTIFICATION) { |
Steve Block | 29357bc | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 967 | ALOGE("startTone: illegal tone requested (%d)", tone); |
Glenn Kasten | 6e2ebe9 | 2013-08-13 09:14:51 -0700 | [diff] [blame] | 968 | } |
| 969 | if (stream != AUDIO_STREAM_VOICE_CALL) { |
Steve Block | 29357bc | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 970 | ALOGE("startTone: illegal stream (%d) requested for tone %d", stream, |
Glenn Kasten | e53b9ea | 2012-03-12 16:29:55 -0700 | [diff] [blame] | 971 | tone); |
Glenn Kasten | 6e2ebe9 | 2013-08-13 09:14:51 -0700 | [diff] [blame] | 972 | } |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 973 | mTonePlaybackThread->startToneCommand(ToneGenerator::TONE_SUP_CALL_WAITING, |
| 974 | AUDIO_STREAM_VOICE_CALL); |
| 975 | return 0; |
| 976 | } |
| 977 | |
| 978 | int AudioPolicyService::stopTone() |
| 979 | { |
| 980 | mTonePlaybackThread->stopToneCommand(); |
| 981 | return 0; |
| 982 | } |
| 983 | |
| 984 | int AudioPolicyService::setVoiceVolume(float volume, int delayMs) |
| 985 | { |
| 986 | return (int)mAudioCommandThread->voiceVolumeCommand(volume, delayMs); |
| 987 | } |
| 988 | |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 989 | extern "C" { |
Eric Laurent | 2d388ec | 2014-03-07 13:25:54 -0800 | [diff] [blame] | 990 | audio_module_handle_t aps_load_hw_module(void *service __unused, |
| 991 | const char *name); |
| 992 | audio_io_handle_t aps_open_output(void *service __unused, |
Eric Laurent | a4c5a55 | 2012-03-29 10:12:40 -0700 | [diff] [blame] | 993 | audio_devices_t *pDevices, |
| 994 | uint32_t *pSamplingRate, |
| 995 | audio_format_t *pFormat, |
| 996 | audio_channel_mask_t *pChannelMask, |
| 997 | uint32_t *pLatencyMs, |
Eric Laurent | 2d388ec | 2014-03-07 13:25:54 -0800 | [diff] [blame] | 998 | audio_output_flags_t flags); |
Eric Laurent | a4c5a55 | 2012-03-29 10:12:40 -0700 | [diff] [blame] | 999 | |
Eric Laurent | 2d388ec | 2014-03-07 13:25:54 -0800 | [diff] [blame] | 1000 | audio_io_handle_t aps_open_output_on_module(void *service __unused, |
Eric Laurent | a4c5a55 | 2012-03-29 10:12:40 -0700 | [diff] [blame] | 1001 | audio_module_handle_t module, |
| 1002 | audio_devices_t *pDevices, |
| 1003 | uint32_t *pSamplingRate, |
| 1004 | audio_format_t *pFormat, |
| 1005 | audio_channel_mask_t *pChannelMask, |
| 1006 | uint32_t *pLatencyMs, |
Richard Fitzgerald | ad3af33 | 2013-03-25 16:54:37 +0000 | [diff] [blame] | 1007 | audio_output_flags_t flags, |
Eric Laurent | 2d388ec | 2014-03-07 13:25:54 -0800 | [diff] [blame] | 1008 | const audio_offload_info_t *offloadInfo); |
| 1009 | audio_io_handle_t aps_open_dup_output(void *service __unused, |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 1010 | audio_io_handle_t output1, |
Eric Laurent | 2d388ec | 2014-03-07 13:25:54 -0800 | [diff] [blame] | 1011 | audio_io_handle_t output2); |
| 1012 | int aps_close_output(void *service __unused, audio_io_handle_t output); |
| 1013 | int aps_suspend_output(void *service __unused, audio_io_handle_t output); |
| 1014 | int aps_restore_output(void *service __unused, audio_io_handle_t output); |
| 1015 | audio_io_handle_t aps_open_input(void *service __unused, |
Eric Laurent | a4c5a55 | 2012-03-29 10:12:40 -0700 | [diff] [blame] | 1016 | audio_devices_t *pDevices, |
| 1017 | uint32_t *pSamplingRate, |
| 1018 | audio_format_t *pFormat, |
| 1019 | audio_channel_mask_t *pChannelMask, |
Eric Laurent | 2d388ec | 2014-03-07 13:25:54 -0800 | [diff] [blame] | 1020 | audio_in_acoustics_t acoustics __unused); |
| 1021 | audio_io_handle_t aps_open_input_on_module(void *service __unused, |
Eric Laurent | a4c5a55 | 2012-03-29 10:12:40 -0700 | [diff] [blame] | 1022 | audio_module_handle_t module, |
| 1023 | audio_devices_t *pDevices, |
| 1024 | uint32_t *pSamplingRate, |
| 1025 | audio_format_t *pFormat, |
Eric Laurent | 2d388ec | 2014-03-07 13:25:54 -0800 | [diff] [blame] | 1026 | audio_channel_mask_t *pChannelMask); |
| 1027 | int aps_close_input(void *service __unused, audio_io_handle_t input); |
| 1028 | int aps_invalidate_stream(void *service __unused, audio_stream_type_t stream); |
| 1029 | int aps_move_effects(void *service __unused, int session, |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 1030 | audio_io_handle_t src_output, |
Eric Laurent | 2d388ec | 2014-03-07 13:25:54 -0800 | [diff] [blame] | 1031 | audio_io_handle_t dst_output); |
| 1032 | char * aps_get_parameters(void *service __unused, audio_io_handle_t io_handle, |
| 1033 | const char *keys); |
| 1034 | void aps_set_parameters(void *service, audio_io_handle_t io_handle, |
| 1035 | const char *kv_pairs, int delay_ms); |
| 1036 | int aps_set_stream_volume(void *service, audio_stream_type_t stream, |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 1037 | float volume, audio_io_handle_t output, |
Eric Laurent | 2d388ec | 2014-03-07 13:25:54 -0800 | [diff] [blame] | 1038 | int delay_ms); |
| 1039 | int aps_start_tone(void *service, audio_policy_tone_t tone, |
| 1040 | audio_stream_type_t stream); |
| 1041 | int aps_stop_tone(void *service); |
| 1042 | int aps_set_voice_volume(void *service, float volume, int delay_ms); |
| 1043 | }; |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 1044 | |
| 1045 | namespace { |
| 1046 | struct audio_policy_service_ops aps_ops = { |
Glenn Kasten | 01d3acb | 2014-02-06 08:24:07 -0800 | [diff] [blame] | 1047 | .open_output = aps_open_output, |
| 1048 | .open_duplicate_output = aps_open_dup_output, |
| 1049 | .close_output = aps_close_output, |
| 1050 | .suspend_output = aps_suspend_output, |
| 1051 | .restore_output = aps_restore_output, |
| 1052 | .open_input = aps_open_input, |
| 1053 | .close_input = aps_close_input, |
| 1054 | .set_stream_volume = aps_set_stream_volume, |
Glenn Kasten | d2304db | 2014-02-03 07:40:31 -0800 | [diff] [blame] | 1055 | .invalidate_stream = aps_invalidate_stream, |
Glenn Kasten | 01d3acb | 2014-02-06 08:24:07 -0800 | [diff] [blame] | 1056 | .set_parameters = aps_set_parameters, |
| 1057 | .get_parameters = aps_get_parameters, |
| 1058 | .start_tone = aps_start_tone, |
| 1059 | .stop_tone = aps_stop_tone, |
| 1060 | .set_voice_volume = aps_set_voice_volume, |
| 1061 | .move_effects = aps_move_effects, |
| 1062 | .load_hw_module = aps_load_hw_module, |
| 1063 | .open_output_on_module = aps_open_output_on_module, |
| 1064 | .open_input_on_module = aps_open_input_on_module, |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 1065 | }; |
| 1066 | }; // namespace <unnamed> |
| 1067 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1068 | }; // namespace android |