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