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 | |
| 20 | #undef __STRICT_ANSI__ |
| 21 | #define __STDINT_LIMITS |
| 22 | #define __STDC_LIMIT_MACROS |
| 23 | #include <stdint.h> |
| 24 | |
| 25 | #include <sys/time.h> |
| 26 | #include <binder/IServiceManager.h> |
| 27 | #include <utils/Log.h> |
| 28 | #include <cutils/properties.h> |
| 29 | #include <binder/IPCThreadState.h> |
| 30 | #include <utils/String16.h> |
| 31 | #include <utils/threads.h> |
| 32 | #include "AudioPolicyService.h" |
Glenn Kasten | 44deb05 | 2012-02-05 18:09:08 -0800 | [diff] [blame] | 33 | #include "ServiceUtilities.h" |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 34 | #include <hardware_legacy/power.h> |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 35 | #include <media/AudioEffect.h> |
| 36 | #include <media/EffectsFactoryApi.h> |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 37 | |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 38 | #include <hardware/hardware.h> |
Dima Zavin | 6476024 | 2011-05-11 14:15:23 -0700 | [diff] [blame] | 39 | #include <system/audio.h> |
Dima Zavin | 7394a4f | 2011-06-13 18:16:26 -0700 | [diff] [blame] | 40 | #include <system/audio_policy.h> |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 41 | #include <hardware/audio_policy.h> |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 42 | #include <audio_effects/audio_effects_conf.h> |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 43 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 44 | namespace android { |
| 45 | |
Glenn Kasten | 8dad0e3 | 2012-01-09 08:41:22 -0800 | [diff] [blame] | 46 | static const char kDeadlockedString[] = "AudioPolicyService may be deadlocked\n"; |
| 47 | static const char kCmdDeadlockedString[] = "AudioPolicyService command thread may be deadlocked\n"; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 48 | |
| 49 | static const int kDumpLockRetries = 50; |
Glenn Kasten | 22ecc91 | 2012-01-09 08:33:38 -0800 | [diff] [blame] | 50 | static const int kDumpLockSleepUs = 20000; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 51 | |
Christer Fletcher | 5fa8c4b | 2013-01-18 15:27:03 +0100 | [diff] [blame] | 52 | static const nsecs_t kAudioCommandTimeout = 3000000000; // 3 seconds |
| 53 | |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 54 | namespace { |
| 55 | extern struct audio_policy_service_ops aps_ops; |
| 56 | }; |
| 57 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 58 | // ---------------------------------------------------------------------------- |
| 59 | |
| 60 | AudioPolicyService::AudioPolicyService() |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 61 | : BnAudioPolicyService() , mpAudioPolicyDev(NULL) , mpAudioPolicy(NULL) |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 62 | { |
| 63 | char value[PROPERTY_VALUE_MAX]; |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 64 | const struct hw_module_t *module; |
| 65 | int forced_val; |
| 66 | int rc; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 67 | |
Eric Laurent | 9357520 | 2011-01-18 18:39:02 -0800 | [diff] [blame] | 68 | Mutex::Autolock _l(mLock); |
| 69 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 70 | // start tone playback thread |
| 71 | mTonePlaybackThread = new AudioCommandThread(String8("")); |
| 72 | // start audio commands thread |
Glenn Kasten | 480b468 | 2012-02-28 12:30:08 -0800 | [diff] [blame] | 73 | mAudioCommandThread = new AudioCommandThread(String8("ApmCommand")); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 74 | |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 75 | /* instantiate the audio policy manager */ |
| 76 | rc = hw_get_module(AUDIO_POLICY_HARDWARE_MODULE_ID, &module); |
| 77 | if (rc) |
| 78 | return; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 79 | |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 80 | rc = audio_policy_dev_open(module, &mpAudioPolicyDev); |
Steve Block | 29357bc | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 81 | ALOGE_IF(rc, "couldn't open audio policy device (%s)", strerror(-rc)); |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 82 | if (rc) |
| 83 | return; |
Eric Laurent | 9357520 | 2011-01-18 18:39:02 -0800 | [diff] [blame] | 84 | |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 85 | rc = mpAudioPolicyDev->create_audio_policy(mpAudioPolicyDev, &aps_ops, this, |
| 86 | &mpAudioPolicy); |
Steve Block | 29357bc | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 87 | ALOGE_IF(rc, "couldn't create audio policy (%s)", strerror(-rc)); |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 88 | if (rc) |
| 89 | return; |
| 90 | |
| 91 | rc = mpAudioPolicy->init_check(mpAudioPolicy); |
Steve Block | 29357bc | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 92 | ALOGE_IF(rc, "couldn't init_check the audio policy (%s)", strerror(-rc)); |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 93 | if (rc) |
| 94 | return; |
| 95 | |
Steve Block | df64d15 | 2012-01-04 20:05:49 +0000 | [diff] [blame] | 96 | ALOGI("Loaded audio policy from %s (%s)", module->name, module->id); |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 97 | |
| 98 | // load audio pre processing modules |
| 99 | if (access(AUDIO_EFFECT_VENDOR_CONFIG_FILE, R_OK) == 0) { |
| 100 | loadPreProcessorConfig(AUDIO_EFFECT_VENDOR_CONFIG_FILE); |
| 101 | } else if (access(AUDIO_EFFECT_DEFAULT_CONFIG_FILE, R_OK) == 0) { |
| 102 | loadPreProcessorConfig(AUDIO_EFFECT_DEFAULT_CONFIG_FILE); |
| 103 | } |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 104 | } |
| 105 | |
| 106 | AudioPolicyService::~AudioPolicyService() |
| 107 | { |
| 108 | mTonePlaybackThread->exit(); |
| 109 | mTonePlaybackThread.clear(); |
| 110 | mAudioCommandThread->exit(); |
| 111 | mAudioCommandThread.clear(); |
| 112 | |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 113 | |
| 114 | // release audio pre processing resources |
| 115 | for (size_t i = 0; i < mInputSources.size(); i++) { |
Glenn Kasten | 9fda4b8 | 2012-02-02 14:04:37 -0800 | [diff] [blame] | 116 | delete mInputSources.valueAt(i); |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 117 | } |
| 118 | mInputSources.clear(); |
| 119 | |
| 120 | for (size_t i = 0; i < mInputs.size(); i++) { |
| 121 | mInputs.valueAt(i)->mEffects.clear(); |
| 122 | delete mInputs.valueAt(i); |
| 123 | } |
| 124 | mInputs.clear(); |
| 125 | |
Glenn Kasten | a0d6833 | 2012-01-27 16:47:15 -0800 | [diff] [blame] | 126 | if (mpAudioPolicy != NULL && mpAudioPolicyDev != NULL) |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 127 | mpAudioPolicyDev->destroy_audio_policy(mpAudioPolicyDev, mpAudioPolicy); |
Glenn Kasten | a0d6833 | 2012-01-27 16:47:15 -0800 | [diff] [blame] | 128 | if (mpAudioPolicyDev != NULL) |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 129 | audio_policy_dev_close(mpAudioPolicyDev); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 130 | } |
| 131 | |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 132 | status_t AudioPolicyService::setDeviceConnectionState(audio_devices_t device, |
| 133 | audio_policy_dev_state_t state, |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 134 | const char *device_address) |
| 135 | { |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 136 | if (mpAudioPolicy == NULL) { |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 137 | return NO_INIT; |
| 138 | } |
Glenn Kasten | 44deb05 | 2012-02-05 18:09:08 -0800 | [diff] [blame] | 139 | if (!settingsAllowed()) { |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 140 | return PERMISSION_DENIED; |
| 141 | } |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 142 | if (!audio_is_output_device(device) && !audio_is_input_device(device)) { |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 143 | return BAD_VALUE; |
| 144 | } |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 145 | if (state != AUDIO_POLICY_DEVICE_STATE_AVAILABLE && |
| 146 | state != AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE) { |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 147 | return BAD_VALUE; |
| 148 | } |
| 149 | |
Glenn Kasten | 411e447 | 2012-11-02 10:00:06 -0700 | [diff] [blame] | 150 | ALOGV("setDeviceConnectionState()"); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 151 | Mutex::Autolock _l(mLock); |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 152 | return mpAudioPolicy->set_device_connection_state(mpAudioPolicy, device, |
| 153 | state, device_address); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 154 | } |
| 155 | |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 156 | audio_policy_dev_state_t AudioPolicyService::getDeviceConnectionState( |
| 157 | audio_devices_t device, |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 158 | const char *device_address) |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 159 | { |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 160 | if (mpAudioPolicy == NULL) { |
| 161 | return AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 162 | } |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 163 | return mpAudioPolicy->get_device_connection_state(mpAudioPolicy, device, |
| 164 | device_address); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 165 | } |
| 166 | |
Glenn Kasten | f78aee7 | 2012-01-04 11:00:47 -0800 | [diff] [blame] | 167 | status_t AudioPolicyService::setPhoneState(audio_mode_t state) |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 168 | { |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 169 | if (mpAudioPolicy == NULL) { |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 170 | return NO_INIT; |
| 171 | } |
Glenn Kasten | 44deb05 | 2012-02-05 18:09:08 -0800 | [diff] [blame] | 172 | if (!settingsAllowed()) { |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 173 | return PERMISSION_DENIED; |
| 174 | } |
Glenn Kasten | 930f4ca | 2012-01-06 16:47:31 -0800 | [diff] [blame] | 175 | if (uint32_t(state) >= AUDIO_MODE_CNT) { |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 176 | return BAD_VALUE; |
| 177 | } |
| 178 | |
Glenn Kasten | 411e447 | 2012-11-02 10:00:06 -0700 | [diff] [blame] | 179 | ALOGV("setPhoneState()"); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 180 | |
| 181 | // TODO: check if it is more appropriate to do it in platform specific policy manager |
| 182 | AudioSystem::setMode(state); |
| 183 | |
| 184 | Mutex::Autolock _l(mLock); |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 185 | mpAudioPolicy->set_phone_state(mpAudioPolicy, state); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 186 | return NO_ERROR; |
| 187 | } |
| 188 | |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 189 | status_t AudioPolicyService::setForceUse(audio_policy_force_use_t usage, |
| 190 | audio_policy_forced_cfg_t config) |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 191 | { |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 192 | if (mpAudioPolicy == NULL) { |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 193 | return NO_INIT; |
| 194 | } |
Glenn Kasten | 44deb05 | 2012-02-05 18:09:08 -0800 | [diff] [blame] | 195 | if (!settingsAllowed()) { |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 196 | return PERMISSION_DENIED; |
| 197 | } |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 198 | if (usage < 0 || usage >= AUDIO_POLICY_FORCE_USE_CNT) { |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 199 | return BAD_VALUE; |
| 200 | } |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 201 | if (config < 0 || config >= AUDIO_POLICY_FORCE_CFG_CNT) { |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 202 | return BAD_VALUE; |
| 203 | } |
Glenn Kasten | 411e447 | 2012-11-02 10:00:06 -0700 | [diff] [blame] | 204 | ALOGV("setForceUse()"); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 205 | Mutex::Autolock _l(mLock); |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 206 | mpAudioPolicy->set_force_use(mpAudioPolicy, usage, config); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 207 | return NO_ERROR; |
| 208 | } |
| 209 | |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 210 | audio_policy_forced_cfg_t AudioPolicyService::getForceUse(audio_policy_force_use_t usage) |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 211 | { |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 212 | if (mpAudioPolicy == NULL) { |
| 213 | return AUDIO_POLICY_FORCE_NONE; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 214 | } |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 215 | if (usage < 0 || usage >= AUDIO_POLICY_FORCE_USE_CNT) { |
| 216 | return AUDIO_POLICY_FORCE_NONE; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 217 | } |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 218 | return mpAudioPolicy->get_force_use(mpAudioPolicy, usage); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 219 | } |
| 220 | |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 221 | audio_io_handle_t AudioPolicyService::getOutput(audio_stream_type_t stream, |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 222 | uint32_t samplingRate, |
Glenn Kasten | 58f3021 | 2012-01-12 12:27:51 -0800 | [diff] [blame] | 223 | audio_format_t format, |
Glenn Kasten | 254af18 | 2012-07-03 14:59:05 -0700 | [diff] [blame] | 224 | audio_channel_mask_t channelMask, |
Richard Fitzgerald | ad3af33 | 2013-03-25 16:54:37 +0000 | [diff] [blame^] | 225 | audio_output_flags_t flags, |
| 226 | const audio_offload_info_t *offloadInfo) |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 227 | { |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 228 | if (mpAudioPolicy == NULL) { |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 229 | return 0; |
| 230 | } |
Glenn Kasten | 411e447 | 2012-11-02 10:00:06 -0700 | [diff] [blame] | 231 | ALOGV("getOutput()"); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 232 | Mutex::Autolock _l(mLock); |
Richard Fitzgerald | ad3af33 | 2013-03-25 16:54:37 +0000 | [diff] [blame^] | 233 | return mpAudioPolicy->get_output(mpAudioPolicy, stream, samplingRate, |
| 234 | format, channelMask, flags, offloadInfo); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 235 | } |
| 236 | |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 237 | status_t AudioPolicyService::startOutput(audio_io_handle_t output, |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 238 | audio_stream_type_t stream, |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 239 | int session) |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 240 | { |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 241 | if (mpAudioPolicy == NULL) { |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 242 | return NO_INIT; |
| 243 | } |
Glenn Kasten | 411e447 | 2012-11-02 10:00:06 -0700 | [diff] [blame] | 244 | ALOGV("startOutput()"); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 245 | Mutex::Autolock _l(mLock); |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 246 | return mpAudioPolicy->start_output(mpAudioPolicy, output, stream, session); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 247 | } |
| 248 | |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 249 | status_t AudioPolicyService::stopOutput(audio_io_handle_t output, |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 250 | audio_stream_type_t stream, |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 251 | int session) |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 252 | { |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 253 | if (mpAudioPolicy == NULL) { |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 254 | return NO_INIT; |
| 255 | } |
Glenn Kasten | 411e447 | 2012-11-02 10:00:06 -0700 | [diff] [blame] | 256 | ALOGV("stopOutput()"); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 257 | Mutex::Autolock _l(mLock); |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 258 | return mpAudioPolicy->stop_output(mpAudioPolicy, output, stream, session); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 259 | } |
| 260 | |
| 261 | void AudioPolicyService::releaseOutput(audio_io_handle_t output) |
| 262 | { |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 263 | if (mpAudioPolicy == NULL) { |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 264 | return; |
| 265 | } |
Glenn Kasten | 411e447 | 2012-11-02 10:00:06 -0700 | [diff] [blame] | 266 | ALOGV("releaseOutput()"); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 267 | Mutex::Autolock _l(mLock); |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 268 | mpAudioPolicy->release_output(mpAudioPolicy, output); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 269 | } |
| 270 | |
Glenn Kasten | eba51fb | 2012-01-23 13:58:49 -0800 | [diff] [blame] | 271 | audio_io_handle_t AudioPolicyService::getInput(audio_source_t inputSource, |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 272 | uint32_t samplingRate, |
Glenn Kasten | 58f3021 | 2012-01-12 12:27:51 -0800 | [diff] [blame] | 273 | audio_format_t format, |
Glenn Kasten | 254af18 | 2012-07-03 14:59:05 -0700 | [diff] [blame] | 274 | audio_channel_mask_t channelMask, |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 275 | int audioSession) |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 276 | { |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 277 | if (mpAudioPolicy == NULL) { |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 278 | return 0; |
| 279 | } |
Glenn Kasten | eba51fb | 2012-01-23 13:58:49 -0800 | [diff] [blame] | 280 | // already checked by client, but double-check in case the client wrapper is bypassed |
| 281 | if (uint32_t(inputSource) >= AUDIO_SOURCE_CNT) { |
| 282 | return 0; |
| 283 | } |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 284 | Mutex::Autolock _l(mLock); |
Glenn Kasten | 2001005 | 2012-06-22 13:43:51 -0700 | [diff] [blame] | 285 | // the audio_in_acoustics_t parameter is ignored by get_input() |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 286 | audio_io_handle_t input = mpAudioPolicy->get_input(mpAudioPolicy, inputSource, samplingRate, |
Glenn Kasten | 8af901c | 2012-11-01 11:11:38 -0700 | [diff] [blame] | 287 | format, channelMask, (audio_in_acoustics_t) 0); |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 288 | |
| 289 | if (input == 0) { |
| 290 | return input; |
| 291 | } |
| 292 | // create audio pre processors according to input source |
Glenn Kasten | eba51fb | 2012-01-23 13:58:49 -0800 | [diff] [blame] | 293 | ssize_t index = mInputSources.indexOfKey(inputSource); |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 294 | if (index < 0) { |
| 295 | return input; |
| 296 | } |
| 297 | ssize_t idx = mInputs.indexOfKey(input); |
| 298 | InputDesc *inputDesc; |
| 299 | if (idx < 0) { |
Glenn Kasten | 81872a2 | 2012-03-07 16:49:22 -0800 | [diff] [blame] | 300 | inputDesc = new InputDesc(audioSession); |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 301 | mInputs.add(input, inputDesc); |
| 302 | } else { |
| 303 | inputDesc = mInputs.valueAt(idx); |
| 304 | } |
| 305 | |
| 306 | Vector <EffectDesc *> effects = mInputSources.valueAt(index)->mEffects; |
| 307 | for (size_t i = 0; i < effects.size(); i++) { |
| 308 | EffectDesc *effect = effects[i]; |
| 309 | sp<AudioEffect> fx = new AudioEffect(NULL, &effect->mUuid, -1, 0, 0, audioSession, input); |
| 310 | status_t status = fx->initCheck(); |
| 311 | if (status != NO_ERROR && status != ALREADY_EXISTS) { |
Steve Block | 5ff1dd5 | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 312 | ALOGW("Failed to create Fx %s on input %d", effect->mName, input); |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 313 | // fx goes out of scope and strong ref on AudioEffect is released |
| 314 | continue; |
| 315 | } |
| 316 | for (size_t j = 0; j < effect->mParams.size(); j++) { |
| 317 | fx->setParameter(effect->mParams[j]); |
| 318 | } |
| 319 | inputDesc->mEffects.add(fx); |
| 320 | } |
| 321 | setPreProcessorEnabled(inputDesc, true); |
| 322 | return input; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 323 | } |
| 324 | |
| 325 | status_t AudioPolicyService::startInput(audio_io_handle_t input) |
| 326 | { |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 327 | if (mpAudioPolicy == NULL) { |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 328 | return NO_INIT; |
| 329 | } |
| 330 | Mutex::Autolock _l(mLock); |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 331 | |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 332 | return mpAudioPolicy->start_input(mpAudioPolicy, input); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 333 | } |
| 334 | |
| 335 | status_t AudioPolicyService::stopInput(audio_io_handle_t input) |
| 336 | { |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 337 | if (mpAudioPolicy == NULL) { |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 338 | return NO_INIT; |
| 339 | } |
| 340 | Mutex::Autolock _l(mLock); |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 341 | |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 342 | return mpAudioPolicy->stop_input(mpAudioPolicy, input); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 343 | } |
| 344 | |
| 345 | void AudioPolicyService::releaseInput(audio_io_handle_t input) |
| 346 | { |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 347 | if (mpAudioPolicy == NULL) { |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 348 | return; |
| 349 | } |
| 350 | Mutex::Autolock _l(mLock); |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 351 | mpAudioPolicy->release_input(mpAudioPolicy, input); |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 352 | |
| 353 | ssize_t index = mInputs.indexOfKey(input); |
| 354 | if (index < 0) { |
| 355 | return; |
| 356 | } |
| 357 | InputDesc *inputDesc = mInputs.valueAt(index); |
| 358 | setPreProcessorEnabled(inputDesc, false); |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 359 | delete inputDesc; |
| 360 | mInputs.removeItemsAt(index); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 361 | } |
| 362 | |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 363 | status_t AudioPolicyService::initStreamVolume(audio_stream_type_t stream, |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 364 | int indexMin, |
| 365 | int indexMax) |
| 366 | { |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 367 | if (mpAudioPolicy == NULL) { |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 368 | return NO_INIT; |
| 369 | } |
Glenn Kasten | 44deb05 | 2012-02-05 18:09:08 -0800 | [diff] [blame] | 370 | if (!settingsAllowed()) { |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 371 | return PERMISSION_DENIED; |
| 372 | } |
Glenn Kasten | 263709e | 2012-01-06 08:40:01 -0800 | [diff] [blame] | 373 | if (uint32_t(stream) >= AUDIO_STREAM_CNT) { |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 374 | return BAD_VALUE; |
| 375 | } |
Eric Laurent | 5f12136 | 2012-06-15 14:45:03 -0700 | [diff] [blame] | 376 | Mutex::Autolock _l(mLock); |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 377 | mpAudioPolicy->init_stream_volume(mpAudioPolicy, stream, indexMin, indexMax); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 378 | return NO_ERROR; |
| 379 | } |
| 380 | |
Eric Laurent | 83844cc | 2011-11-18 16:43:31 -0800 | [diff] [blame] | 381 | status_t AudioPolicyService::setStreamVolumeIndex(audio_stream_type_t stream, |
| 382 | int index, |
| 383 | audio_devices_t device) |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 384 | { |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 385 | if (mpAudioPolicy == NULL) { |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 386 | return NO_INIT; |
| 387 | } |
Glenn Kasten | 44deb05 | 2012-02-05 18:09:08 -0800 | [diff] [blame] | 388 | if (!settingsAllowed()) { |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 389 | return PERMISSION_DENIED; |
| 390 | } |
Glenn Kasten | 263709e | 2012-01-06 08:40:01 -0800 | [diff] [blame] | 391 | if (uint32_t(stream) >= AUDIO_STREAM_CNT) { |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 392 | return BAD_VALUE; |
| 393 | } |
Eric Laurent | 5f12136 | 2012-06-15 14:45:03 -0700 | [diff] [blame] | 394 | Mutex::Autolock _l(mLock); |
Eric Laurent | 83844cc | 2011-11-18 16:43:31 -0800 | [diff] [blame] | 395 | if (mpAudioPolicy->set_stream_volume_index_for_device) { |
| 396 | return mpAudioPolicy->set_stream_volume_index_for_device(mpAudioPolicy, |
| 397 | stream, |
| 398 | index, |
| 399 | device); |
| 400 | } else { |
| 401 | return mpAudioPolicy->set_stream_volume_index(mpAudioPolicy, stream, index); |
| 402 | } |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 403 | } |
| 404 | |
Eric Laurent | 83844cc | 2011-11-18 16:43:31 -0800 | [diff] [blame] | 405 | status_t AudioPolicyService::getStreamVolumeIndex(audio_stream_type_t stream, |
| 406 | int *index, |
| 407 | audio_devices_t device) |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 408 | { |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 409 | if (mpAudioPolicy == NULL) { |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 410 | return NO_INIT; |
| 411 | } |
Glenn Kasten | 263709e | 2012-01-06 08:40:01 -0800 | [diff] [blame] | 412 | if (uint32_t(stream) >= AUDIO_STREAM_CNT) { |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 413 | return BAD_VALUE; |
| 414 | } |
Eric Laurent | 5f12136 | 2012-06-15 14:45:03 -0700 | [diff] [blame] | 415 | Mutex::Autolock _l(mLock); |
Eric Laurent | 83844cc | 2011-11-18 16:43:31 -0800 | [diff] [blame] | 416 | if (mpAudioPolicy->get_stream_volume_index_for_device) { |
| 417 | return mpAudioPolicy->get_stream_volume_index_for_device(mpAudioPolicy, |
| 418 | stream, |
| 419 | index, |
| 420 | device); |
| 421 | } else { |
| 422 | return mpAudioPolicy->get_stream_volume_index(mpAudioPolicy, stream, index); |
| 423 | } |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 424 | } |
| 425 | |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 426 | uint32_t AudioPolicyService::getStrategyForStream(audio_stream_type_t stream) |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 427 | { |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 428 | if (mpAudioPolicy == NULL) { |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 429 | return 0; |
| 430 | } |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 431 | return mpAudioPolicy->get_strategy_for_stream(mpAudioPolicy, stream); |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 432 | } |
| 433 | |
Eric Laurent | 6374252 | 2012-03-08 13:42:42 -0800 | [diff] [blame] | 434 | //audio policy: use audio_device_t appropriately |
| 435 | |
| 436 | audio_devices_t AudioPolicyService::getDevicesForStream(audio_stream_type_t stream) |
Glenn Kasten | 6b2718c | 2011-02-04 13:54:26 -0800 | [diff] [blame] | 437 | { |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 438 | if (mpAudioPolicy == NULL) { |
Eric Laurent | 6374252 | 2012-03-08 13:42:42 -0800 | [diff] [blame] | 439 | return (audio_devices_t)0; |
Glenn Kasten | 6b2718c | 2011-02-04 13:54:26 -0800 | [diff] [blame] | 440 | } |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 441 | return mpAudioPolicy->get_devices_for_stream(mpAudioPolicy, stream); |
Glenn Kasten | 6b2718c | 2011-02-04 13:54:26 -0800 | [diff] [blame] | 442 | } |
| 443 | |
Glenn Kasten | 58e5aa3 | 2012-06-20 14:08:14 -0700 | [diff] [blame] | 444 | audio_io_handle_t AudioPolicyService::getOutputForEffect(const effect_descriptor_t *desc) |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 445 | { |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 446 | if (mpAudioPolicy == NULL) { |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 447 | return NO_INIT; |
| 448 | } |
| 449 | Mutex::Autolock _l(mLock); |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 450 | return mpAudioPolicy->get_output_for_effect(mpAudioPolicy, desc); |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 451 | } |
| 452 | |
Glenn Kasten | 58e5aa3 | 2012-06-20 14:08:14 -0700 | [diff] [blame] | 453 | status_t AudioPolicyService::registerEffect(const effect_descriptor_t *desc, |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 454 | audio_io_handle_t io, |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 455 | uint32_t strategy, |
| 456 | int session, |
| 457 | int id) |
| 458 | { |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 459 | if (mpAudioPolicy == NULL) { |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 460 | return NO_INIT; |
| 461 | } |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 462 | return mpAudioPolicy->register_effect(mpAudioPolicy, desc, io, strategy, session, id); |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 463 | } |
| 464 | |
| 465 | status_t AudioPolicyService::unregisterEffect(int id) |
| 466 | { |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 467 | if (mpAudioPolicy == NULL) { |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 468 | return NO_INIT; |
| 469 | } |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 470 | return mpAudioPolicy->unregister_effect(mpAudioPolicy, id); |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 471 | } |
| 472 | |
Eric Laurent | db7c079 | 2011-08-10 10:37:50 -0700 | [diff] [blame] | 473 | status_t AudioPolicyService::setEffectEnabled(int id, bool enabled) |
| 474 | { |
| 475 | if (mpAudioPolicy == NULL) { |
| 476 | return NO_INIT; |
| 477 | } |
| 478 | return mpAudioPolicy->set_effect_enabled(mpAudioPolicy, id, enabled); |
| 479 | } |
| 480 | |
Glenn Kasten | fff6d71 | 2012-01-12 16:38:12 -0800 | [diff] [blame] | 481 | bool AudioPolicyService::isStreamActive(audio_stream_type_t stream, uint32_t inPastMs) const |
Eric Laurent | eda6c36 | 2011-02-02 09:33:30 -0800 | [diff] [blame] | 482 | { |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 483 | if (mpAudioPolicy == NULL) { |
Eric Laurent | eda6c36 | 2011-02-02 09:33:30 -0800 | [diff] [blame] | 484 | return 0; |
| 485 | } |
| 486 | Mutex::Autolock _l(mLock); |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 487 | return mpAudioPolicy->is_stream_active(mpAudioPolicy, stream, inPastMs); |
Eric Laurent | eda6c36 | 2011-02-02 09:33:30 -0800 | [diff] [blame] | 488 | } |
| 489 | |
Jean-Michel Trivi | e336f91 | 2013-02-04 16:26:02 -0800 | [diff] [blame] | 490 | bool AudioPolicyService::isStreamActiveRemotely(audio_stream_type_t stream, uint32_t inPastMs) const |
| 491 | { |
| 492 | if (mpAudioPolicy == NULL) { |
| 493 | return 0; |
| 494 | } |
| 495 | Mutex::Autolock _l(mLock); |
| 496 | return mpAudioPolicy->is_stream_active_remotely(mpAudioPolicy, stream, inPastMs); |
| 497 | } |
| 498 | |
Jean-Michel Trivi | e3f641f | 2012-10-10 12:11:16 -0700 | [diff] [blame] | 499 | bool AudioPolicyService::isSourceActive(audio_source_t source) const |
| 500 | { |
| 501 | if (mpAudioPolicy == NULL) { |
| 502 | return false; |
| 503 | } |
| 504 | if (mpAudioPolicy->is_source_active == 0) { |
| 505 | return false; |
| 506 | } |
| 507 | Mutex::Autolock _l(mLock); |
| 508 | return mpAudioPolicy->is_source_active(mpAudioPolicy, source); |
| 509 | } |
| 510 | |
Eric Laurent | 57dae99 | 2011-07-24 13:36:09 -0700 | [diff] [blame] | 511 | status_t AudioPolicyService::queryDefaultPreProcessing(int audioSession, |
| 512 | effect_descriptor_t *descriptors, |
| 513 | uint32_t *count) |
| 514 | { |
| 515 | |
| 516 | if (mpAudioPolicy == NULL) { |
| 517 | *count = 0; |
| 518 | return NO_INIT; |
| 519 | } |
| 520 | Mutex::Autolock _l(mLock); |
| 521 | status_t status = NO_ERROR; |
| 522 | |
| 523 | size_t index; |
| 524 | for (index = 0; index < mInputs.size(); index++) { |
| 525 | if (mInputs.valueAt(index)->mSessionId == audioSession) { |
| 526 | break; |
| 527 | } |
| 528 | } |
| 529 | if (index == mInputs.size()) { |
| 530 | *count = 0; |
| 531 | return BAD_VALUE; |
| 532 | } |
| 533 | Vector< sp<AudioEffect> > effects = mInputs.valueAt(index)->mEffects; |
| 534 | |
| 535 | for (size_t i = 0; i < effects.size(); i++) { |
| 536 | effect_descriptor_t desc = effects[i]->descriptor(); |
| 537 | if (i < *count) { |
Glenn Kasten | a189a68 | 2012-02-20 12:16:30 -0800 | [diff] [blame] | 538 | descriptors[i] = desc; |
Eric Laurent | 57dae99 | 2011-07-24 13:36:09 -0700 | [diff] [blame] | 539 | } |
| 540 | } |
| 541 | if (effects.size() > *count) { |
| 542 | status = NO_MEMORY; |
| 543 | } |
| 544 | *count = effects.size(); |
| 545 | return status; |
| 546 | } |
| 547 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 548 | void AudioPolicyService::binderDied(const wp<IBinder>& who) { |
Glenn Kasten | 411e447 | 2012-11-02 10:00:06 -0700 | [diff] [blame] | 549 | ALOGW("binderDied() %p, calling pid %d", who.unsafe_get(), |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 550 | IPCThreadState::self()->getCallingPid()); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 551 | } |
| 552 | |
| 553 | static bool tryLock(Mutex& mutex) |
| 554 | { |
| 555 | bool locked = false; |
| 556 | for (int i = 0; i < kDumpLockRetries; ++i) { |
| 557 | if (mutex.tryLock() == NO_ERROR) { |
| 558 | locked = true; |
| 559 | break; |
| 560 | } |
Glenn Kasten | 22ecc91 | 2012-01-09 08:33:38 -0800 | [diff] [blame] | 561 | usleep(kDumpLockSleepUs); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 562 | } |
| 563 | return locked; |
| 564 | } |
| 565 | |
| 566 | status_t AudioPolicyService::dumpInternals(int fd) |
| 567 | { |
| 568 | const size_t SIZE = 256; |
| 569 | char buffer[SIZE]; |
| 570 | String8 result; |
| 571 | |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 572 | snprintf(buffer, SIZE, "PolicyManager Interface: %p\n", mpAudioPolicy); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 573 | result.append(buffer); |
| 574 | snprintf(buffer, SIZE, "Command Thread: %p\n", mAudioCommandThread.get()); |
| 575 | result.append(buffer); |
| 576 | snprintf(buffer, SIZE, "Tones Thread: %p\n", mTonePlaybackThread.get()); |
| 577 | result.append(buffer); |
| 578 | |
| 579 | write(fd, result.string(), result.size()); |
| 580 | return NO_ERROR; |
| 581 | } |
| 582 | |
| 583 | status_t AudioPolicyService::dump(int fd, const Vector<String16>& args) |
| 584 | { |
Glenn Kasten | 44deb05 | 2012-02-05 18:09:08 -0800 | [diff] [blame] | 585 | if (!dumpAllowed()) { |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 586 | dumpPermissionDenial(fd); |
| 587 | } else { |
| 588 | bool locked = tryLock(mLock); |
| 589 | if (!locked) { |
| 590 | String8 result(kDeadlockedString); |
| 591 | write(fd, result.string(), result.size()); |
| 592 | } |
| 593 | |
| 594 | dumpInternals(fd); |
Glenn Kasten | 9d1f02d | 2012-02-08 17:47:58 -0800 | [diff] [blame] | 595 | if (mAudioCommandThread != 0) { |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 596 | mAudioCommandThread->dump(fd); |
| 597 | } |
Glenn Kasten | 9d1f02d | 2012-02-08 17:47:58 -0800 | [diff] [blame] | 598 | if (mTonePlaybackThread != 0) { |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 599 | mTonePlaybackThread->dump(fd); |
| 600 | } |
| 601 | |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 602 | if (mpAudioPolicy) { |
| 603 | mpAudioPolicy->dump(mpAudioPolicy, fd); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 604 | } |
| 605 | |
| 606 | if (locked) mLock.unlock(); |
| 607 | } |
| 608 | return NO_ERROR; |
| 609 | } |
| 610 | |
| 611 | status_t AudioPolicyService::dumpPermissionDenial(int fd) |
| 612 | { |
| 613 | const size_t SIZE = 256; |
| 614 | char buffer[SIZE]; |
| 615 | String8 result; |
| 616 | snprintf(buffer, SIZE, "Permission Denial: " |
| 617 | "can't dump AudioPolicyService from pid=%d, uid=%d\n", |
| 618 | IPCThreadState::self()->getCallingPid(), |
| 619 | IPCThreadState::self()->getCallingUid()); |
| 620 | result.append(buffer); |
| 621 | write(fd, result.string(), result.size()); |
| 622 | return NO_ERROR; |
| 623 | } |
| 624 | |
Glenn Kasten | 81872a2 | 2012-03-07 16:49:22 -0800 | [diff] [blame] | 625 | void AudioPolicyService::setPreProcessorEnabled(const InputDesc *inputDesc, bool enabled) |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 626 | { |
Glenn Kasten | 81872a2 | 2012-03-07 16:49:22 -0800 | [diff] [blame] | 627 | const Vector<sp<AudioEffect> > &fxVector = inputDesc->mEffects; |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 628 | for (size_t i = 0; i < fxVector.size(); i++) { |
Glenn Kasten | a111792 | 2012-01-26 10:53:32 -0800 | [diff] [blame] | 629 | fxVector.itemAt(i)->setEnabled(enabled); |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 630 | } |
| 631 | } |
| 632 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 633 | status_t AudioPolicyService::onTransact( |
| 634 | uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags) |
| 635 | { |
| 636 | return BnAudioPolicyService::onTransact(code, data, reply, flags); |
| 637 | } |
| 638 | |
| 639 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 640 | // ----------- AudioPolicyService::AudioCommandThread implementation ---------- |
| 641 | |
| 642 | AudioPolicyService::AudioCommandThread::AudioCommandThread(String8 name) |
| 643 | : Thread(false), mName(name) |
| 644 | { |
| 645 | mpToneGenerator = NULL; |
| 646 | } |
| 647 | |
| 648 | |
| 649 | AudioPolicyService::AudioCommandThread::~AudioCommandThread() |
| 650 | { |
| 651 | if (mName != "" && !mAudioCommands.isEmpty()) { |
| 652 | release_wake_lock(mName.string()); |
| 653 | } |
| 654 | mAudioCommands.clear(); |
Glenn Kasten | e9dd017 | 2012-01-27 18:08:45 -0800 | [diff] [blame] | 655 | delete mpToneGenerator; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 656 | } |
| 657 | |
| 658 | void AudioPolicyService::AudioCommandThread::onFirstRef() |
| 659 | { |
| 660 | if (mName != "") { |
| 661 | run(mName.string(), ANDROID_PRIORITY_AUDIO); |
| 662 | } else { |
Glenn Kasten | 480b468 | 2012-02-28 12:30:08 -0800 | [diff] [blame] | 663 | run("AudioCommand", ANDROID_PRIORITY_AUDIO); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 664 | } |
| 665 | } |
| 666 | |
| 667 | bool AudioPolicyService::AudioCommandThread::threadLoop() |
| 668 | { |
| 669 | nsecs_t waitTime = INT64_MAX; |
| 670 | |
| 671 | mLock.lock(); |
| 672 | while (!exitPending()) |
| 673 | { |
Glenn Kasten | e53b9ea | 2012-03-12 16:29:55 -0700 | [diff] [blame] | 674 | while (!mAudioCommands.isEmpty()) { |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 675 | nsecs_t curTime = systemTime(); |
| 676 | // commands are sorted by increasing time stamp: execute them from index 0 and up |
| 677 | if (mAudioCommands[0]->mTime <= curTime) { |
| 678 | AudioCommand *command = mAudioCommands[0]; |
| 679 | mAudioCommands.removeAt(0); |
| 680 | mLastCommand = *command; |
| 681 | |
| 682 | switch (command->mCommand) { |
| 683 | case START_TONE: { |
| 684 | mLock.unlock(); |
| 685 | ToneData *data = (ToneData *)command->mParam; |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 686 | ALOGV("AudioCommandThread() processing start tone %d on stream %d", |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 687 | data->mType, data->mStream); |
Glenn Kasten | e9dd017 | 2012-01-27 18:08:45 -0800 | [diff] [blame] | 688 | delete mpToneGenerator; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 689 | mpToneGenerator = new ToneGenerator(data->mStream, 1.0); |
| 690 | mpToneGenerator->startTone(data->mType); |
| 691 | delete data; |
| 692 | mLock.lock(); |
| 693 | }break; |
| 694 | case STOP_TONE: { |
| 695 | mLock.unlock(); |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 696 | ALOGV("AudioCommandThread() processing stop tone"); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 697 | if (mpToneGenerator != NULL) { |
| 698 | mpToneGenerator->stopTone(); |
| 699 | delete mpToneGenerator; |
| 700 | mpToneGenerator = NULL; |
| 701 | } |
| 702 | mLock.lock(); |
| 703 | }break; |
| 704 | case SET_VOLUME: { |
| 705 | VolumeData *data = (VolumeData *)command->mParam; |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 706 | ALOGV("AudioCommandThread() processing set volume stream %d, \ |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 707 | volume %f, output %d", data->mStream, data->mVolume, data->mIO); |
| 708 | command->mStatus = AudioSystem::setStreamVolume(data->mStream, |
| 709 | data->mVolume, |
| 710 | data->mIO); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 711 | if (command->mWaitStatus) { |
| 712 | command->mCond.signal(); |
Christer Fletcher | 5fa8c4b | 2013-01-18 15:27:03 +0100 | [diff] [blame] | 713 | command->mCond.waitRelative(mLock, kAudioCommandTimeout); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 714 | } |
| 715 | delete data; |
| 716 | }break; |
| 717 | case SET_PARAMETERS: { |
Glenn Kasten | e53b9ea | 2012-03-12 16:29:55 -0700 | [diff] [blame] | 718 | ParametersData *data = (ParametersData *)command->mParam; |
| 719 | ALOGV("AudioCommandThread() processing set parameters string %s, io %d", |
| 720 | data->mKeyValuePairs.string(), data->mIO); |
| 721 | command->mStatus = AudioSystem::setParameters(data->mIO, data->mKeyValuePairs); |
| 722 | if (command->mWaitStatus) { |
| 723 | command->mCond.signal(); |
Christer Fletcher | 5fa8c4b | 2013-01-18 15:27:03 +0100 | [diff] [blame] | 724 | command->mCond.waitRelative(mLock, kAudioCommandTimeout); |
Glenn Kasten | e53b9ea | 2012-03-12 16:29:55 -0700 | [diff] [blame] | 725 | } |
| 726 | delete data; |
| 727 | }break; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 728 | case SET_VOICE_VOLUME: { |
| 729 | VoiceVolumeData *data = (VoiceVolumeData *)command->mParam; |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 730 | ALOGV("AudioCommandThread() processing set voice volume volume %f", |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 731 | data->mVolume); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 732 | command->mStatus = AudioSystem::setVoiceVolume(data->mVolume); |
| 733 | if (command->mWaitStatus) { |
| 734 | command->mCond.signal(); |
Christer Fletcher | 5fa8c4b | 2013-01-18 15:27:03 +0100 | [diff] [blame] | 735 | command->mCond.waitRelative(mLock, kAudioCommandTimeout); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 736 | } |
| 737 | delete data; |
| 738 | }break; |
| 739 | default: |
Steve Block | 5ff1dd5 | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 740 | ALOGW("AudioCommandThread() unknown command %d", command->mCommand); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 741 | } |
| 742 | delete command; |
| 743 | waitTime = INT64_MAX; |
| 744 | } else { |
| 745 | waitTime = mAudioCommands[0]->mTime - curTime; |
| 746 | break; |
| 747 | } |
| 748 | } |
| 749 | // release delayed commands wake lock |
| 750 | if (mName != "" && mAudioCommands.isEmpty()) { |
| 751 | release_wake_lock(mName.string()); |
| 752 | } |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 753 | ALOGV("AudioCommandThread() going to sleep"); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 754 | mWaitWorkCV.waitRelative(mLock, waitTime); |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 755 | ALOGV("AudioCommandThread() waking up"); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 756 | } |
| 757 | mLock.unlock(); |
| 758 | return false; |
| 759 | } |
| 760 | |
| 761 | status_t AudioPolicyService::AudioCommandThread::dump(int fd) |
| 762 | { |
| 763 | const size_t SIZE = 256; |
| 764 | char buffer[SIZE]; |
| 765 | String8 result; |
| 766 | |
| 767 | snprintf(buffer, SIZE, "AudioCommandThread %p Dump\n", this); |
| 768 | result.append(buffer); |
| 769 | write(fd, result.string(), result.size()); |
| 770 | |
| 771 | bool locked = tryLock(mLock); |
| 772 | if (!locked) { |
| 773 | String8 result2(kCmdDeadlockedString); |
| 774 | write(fd, result2.string(), result2.size()); |
| 775 | } |
| 776 | |
| 777 | snprintf(buffer, SIZE, "- Commands:\n"); |
| 778 | result = String8(buffer); |
| 779 | result.append(" Command Time Wait pParam\n"); |
Glenn Kasten | 8d6a244 | 2012-02-08 14:04:28 -0800 | [diff] [blame] | 780 | for (size_t i = 0; i < mAudioCommands.size(); i++) { |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 781 | mAudioCommands[i]->dump(buffer, SIZE); |
| 782 | result.append(buffer); |
| 783 | } |
| 784 | result.append(" Last Command\n"); |
| 785 | mLastCommand.dump(buffer, SIZE); |
| 786 | result.append(buffer); |
| 787 | |
| 788 | write(fd, result.string(), result.size()); |
| 789 | |
| 790 | if (locked) mLock.unlock(); |
| 791 | |
| 792 | return NO_ERROR; |
| 793 | } |
| 794 | |
Glenn Kasten | 3d2f877 | 2012-01-27 15:25:25 -0800 | [diff] [blame] | 795 | void AudioPolicyService::AudioCommandThread::startToneCommand(ToneGenerator::tone_type type, |
| 796 | audio_stream_type_t stream) |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 797 | { |
| 798 | AudioCommand *command = new AudioCommand(); |
| 799 | command->mCommand = START_TONE; |
| 800 | ToneData *data = new ToneData(); |
| 801 | data->mType = type; |
| 802 | data->mStream = stream; |
| 803 | command->mParam = (void *)data; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 804 | Mutex::Autolock _l(mLock); |
| 805 | insertCommand_l(command); |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 806 | ALOGV("AudioCommandThread() adding tone start type %d, stream %d", type, stream); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 807 | mWaitWorkCV.signal(); |
| 808 | } |
| 809 | |
| 810 | void AudioPolicyService::AudioCommandThread::stopToneCommand() |
| 811 | { |
| 812 | AudioCommand *command = new AudioCommand(); |
| 813 | command->mCommand = STOP_TONE; |
| 814 | command->mParam = NULL; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 815 | Mutex::Autolock _l(mLock); |
| 816 | insertCommand_l(command); |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 817 | ALOGV("AudioCommandThread() adding tone stop"); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 818 | mWaitWorkCV.signal(); |
| 819 | } |
| 820 | |
Glenn Kasten | fff6d71 | 2012-01-12 16:38:12 -0800 | [diff] [blame] | 821 | status_t AudioPolicyService::AudioCommandThread::volumeCommand(audio_stream_type_t stream, |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 822 | float volume, |
Glenn Kasten | 72ef00d | 2012-01-17 11:09:42 -0800 | [diff] [blame] | 823 | audio_io_handle_t output, |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 824 | int delayMs) |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 825 | { |
| 826 | status_t status = NO_ERROR; |
| 827 | |
| 828 | AudioCommand *command = new AudioCommand(); |
| 829 | command->mCommand = SET_VOLUME; |
| 830 | VolumeData *data = new VolumeData(); |
| 831 | data->mStream = stream; |
| 832 | data->mVolume = volume; |
| 833 | data->mIO = output; |
| 834 | command->mParam = data; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 835 | Mutex::Autolock _l(mLock); |
| 836 | insertCommand_l(command, delayMs); |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 837 | ALOGV("AudioCommandThread() adding set volume stream %d, volume %f, output %d", |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 838 | stream, volume, output); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 839 | mWaitWorkCV.signal(); |
| 840 | if (command->mWaitStatus) { |
| 841 | command->mCond.wait(mLock); |
| 842 | status = command->mStatus; |
Christer Fletcher | 5fa8c4b | 2013-01-18 15:27:03 +0100 | [diff] [blame] | 843 | command->mCond.signal(); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 844 | } |
| 845 | return status; |
| 846 | } |
| 847 | |
Glenn Kasten | 72ef00d | 2012-01-17 11:09:42 -0800 | [diff] [blame] | 848 | status_t AudioPolicyService::AudioCommandThread::parametersCommand(audio_io_handle_t ioHandle, |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 849 | const char *keyValuePairs, |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 850 | int delayMs) |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 851 | { |
| 852 | status_t status = NO_ERROR; |
| 853 | |
| 854 | AudioCommand *command = new AudioCommand(); |
| 855 | command->mCommand = SET_PARAMETERS; |
| 856 | ParametersData *data = new ParametersData(); |
| 857 | data->mIO = ioHandle; |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 858 | data->mKeyValuePairs = String8(keyValuePairs); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 859 | command->mParam = data; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 860 | Mutex::Autolock _l(mLock); |
| 861 | insertCommand_l(command, delayMs); |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 862 | ALOGV("AudioCommandThread() adding set parameter string %s, io %d ,delay %d", |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 863 | keyValuePairs, ioHandle, delayMs); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 864 | mWaitWorkCV.signal(); |
| 865 | if (command->mWaitStatus) { |
| 866 | command->mCond.wait(mLock); |
| 867 | status = command->mStatus; |
Christer Fletcher | 5fa8c4b | 2013-01-18 15:27:03 +0100 | [diff] [blame] | 868 | command->mCond.signal(); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 869 | } |
| 870 | return status; |
| 871 | } |
| 872 | |
| 873 | status_t AudioPolicyService::AudioCommandThread::voiceVolumeCommand(float volume, int delayMs) |
| 874 | { |
| 875 | status_t status = NO_ERROR; |
| 876 | |
| 877 | AudioCommand *command = new AudioCommand(); |
| 878 | command->mCommand = SET_VOICE_VOLUME; |
| 879 | VoiceVolumeData *data = new VoiceVolumeData(); |
| 880 | data->mVolume = volume; |
| 881 | command->mParam = data; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 882 | Mutex::Autolock _l(mLock); |
| 883 | insertCommand_l(command, delayMs); |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 884 | ALOGV("AudioCommandThread() adding set voice volume volume %f", volume); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 885 | mWaitWorkCV.signal(); |
| 886 | if (command->mWaitStatus) { |
| 887 | command->mCond.wait(mLock); |
| 888 | status = command->mStatus; |
Christer Fletcher | 5fa8c4b | 2013-01-18 15:27:03 +0100 | [diff] [blame] | 889 | command->mCond.signal(); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 890 | } |
| 891 | return status; |
| 892 | } |
| 893 | |
| 894 | // insertCommand_l() must be called with mLock held |
| 895 | void AudioPolicyService::AudioCommandThread::insertCommand_l(AudioCommand *command, int delayMs) |
| 896 | { |
Glenn Kasten | 8d6a244 | 2012-02-08 14:04:28 -0800 | [diff] [blame] | 897 | ssize_t i; // not size_t because i will count down to -1 |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 898 | Vector <AudioCommand *> removedCommands; |
| 899 | |
Eric Laurent | cec4abb | 2012-07-03 12:23:02 -0700 | [diff] [blame] | 900 | nsecs_t time = 0; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 901 | command->mTime = systemTime() + milliseconds(delayMs); |
| 902 | |
| 903 | // acquire wake lock to make sure delayed commands are processed |
| 904 | if (mName != "" && mAudioCommands.isEmpty()) { |
| 905 | acquire_wake_lock(PARTIAL_WAKE_LOCK, mName.string()); |
| 906 | } |
| 907 | |
| 908 | // check same pending commands with later time stamps and eliminate them |
| 909 | for (i = mAudioCommands.size()-1; i >= 0; i--) { |
| 910 | AudioCommand *command2 = mAudioCommands[i]; |
| 911 | // commands are sorted by increasing time stamp: no need to scan the rest of mAudioCommands |
| 912 | if (command2->mTime <= command->mTime) break; |
| 913 | if (command2->mCommand != command->mCommand) continue; |
| 914 | |
| 915 | switch (command->mCommand) { |
| 916 | case SET_PARAMETERS: { |
| 917 | ParametersData *data = (ParametersData *)command->mParam; |
| 918 | ParametersData *data2 = (ParametersData *)command2->mParam; |
| 919 | if (data->mIO != data2->mIO) break; |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 920 | ALOGV("Comparing parameter command %s to new command %s", |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 921 | data2->mKeyValuePairs.string(), data->mKeyValuePairs.string()); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 922 | AudioParameter param = AudioParameter(data->mKeyValuePairs); |
| 923 | AudioParameter param2 = AudioParameter(data2->mKeyValuePairs); |
| 924 | for (size_t j = 0; j < param.size(); j++) { |
Glenn Kasten | e53b9ea | 2012-03-12 16:29:55 -0700 | [diff] [blame] | 925 | String8 key; |
| 926 | String8 value; |
| 927 | param.getAt(j, key, value); |
| 928 | for (size_t k = 0; k < param2.size(); k++) { |
| 929 | String8 key2; |
| 930 | String8 value2; |
| 931 | param2.getAt(k, key2, value2); |
| 932 | if (key2 == key) { |
| 933 | param2.remove(key2); |
| 934 | ALOGV("Filtering out parameter %s", key2.string()); |
| 935 | break; |
| 936 | } |
| 937 | } |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 938 | } |
| 939 | // if all keys have been filtered out, remove the command. |
| 940 | // otherwise, update the key value pairs |
| 941 | if (param2.size() == 0) { |
| 942 | removedCommands.add(command2); |
| 943 | } else { |
| 944 | data2->mKeyValuePairs = param2.toString(); |
| 945 | } |
Eric Laurent | cec4abb | 2012-07-03 12:23:02 -0700 | [diff] [blame] | 946 | time = command2->mTime; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 947 | } break; |
| 948 | |
| 949 | case SET_VOLUME: { |
| 950 | VolumeData *data = (VolumeData *)command->mParam; |
| 951 | VolumeData *data2 = (VolumeData *)command2->mParam; |
| 952 | if (data->mIO != data2->mIO) break; |
| 953 | if (data->mStream != data2->mStream) break; |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 954 | ALOGV("Filtering out volume command on output %d for stream %d", |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 955 | data->mIO, data->mStream); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 956 | removedCommands.add(command2); |
Eric Laurent | cec4abb | 2012-07-03 12:23:02 -0700 | [diff] [blame] | 957 | time = command2->mTime; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 958 | } break; |
| 959 | case START_TONE: |
| 960 | case STOP_TONE: |
| 961 | default: |
| 962 | break; |
| 963 | } |
| 964 | } |
| 965 | |
| 966 | // remove filtered commands |
| 967 | for (size_t j = 0; j < removedCommands.size(); j++) { |
| 968 | // removed commands always have time stamps greater than current command |
| 969 | for (size_t k = i + 1; k < mAudioCommands.size(); k++) { |
| 970 | if (mAudioCommands[k] == removedCommands[j]) { |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 971 | ALOGV("suppressing command: %d", mAudioCommands[k]->mCommand); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 972 | mAudioCommands.removeAt(k); |
| 973 | break; |
| 974 | } |
| 975 | } |
| 976 | } |
| 977 | removedCommands.clear(); |
| 978 | |
Eric Laurent | cec4abb | 2012-07-03 12:23:02 -0700 | [diff] [blame] | 979 | // wait for status only if delay is 0 and command time was not modified above |
| 980 | if (delayMs == 0 && time == 0) { |
| 981 | command->mWaitStatus = true; |
| 982 | } else { |
| 983 | command->mWaitStatus = false; |
| 984 | } |
| 985 | // update command time if modified above |
| 986 | if (time != 0) { |
| 987 | command->mTime = time; |
| 988 | } |
| 989 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 990 | // insert command at the right place according to its time stamp |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 991 | ALOGV("inserting command: %d at index %d, num commands %d", |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 992 | command->mCommand, (int)i+1, mAudioCommands.size()); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 993 | mAudioCommands.insertAt(command, i + 1); |
| 994 | } |
| 995 | |
| 996 | void AudioPolicyService::AudioCommandThread::exit() |
| 997 | { |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 998 | ALOGV("AudioCommandThread::exit"); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 999 | { |
| 1000 | AutoMutex _l(mLock); |
| 1001 | requestExit(); |
| 1002 | mWaitWorkCV.signal(); |
| 1003 | } |
| 1004 | requestExitAndWait(); |
| 1005 | } |
| 1006 | |
| 1007 | void AudioPolicyService::AudioCommandThread::AudioCommand::dump(char* buffer, size_t size) |
| 1008 | { |
| 1009 | snprintf(buffer, size, " %02d %06d.%03d %01u %p\n", |
| 1010 | mCommand, |
| 1011 | (int)ns2s(mTime), |
| 1012 | (int)ns2ms(mTime)%1000, |
| 1013 | mWaitStatus, |
| 1014 | mParam); |
| 1015 | } |
| 1016 | |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 1017 | /******* helpers for the service_ops callbacks defined below *********/ |
| 1018 | void AudioPolicyService::setParameters(audio_io_handle_t ioHandle, |
| 1019 | const char *keyValuePairs, |
| 1020 | int delayMs) |
| 1021 | { |
Glenn Kasten | 72ef00d | 2012-01-17 11:09:42 -0800 | [diff] [blame] | 1022 | mAudioCommandThread->parametersCommand(ioHandle, keyValuePairs, |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 1023 | delayMs); |
| 1024 | } |
| 1025 | |
| 1026 | int AudioPolicyService::setStreamVolume(audio_stream_type_t stream, |
| 1027 | float volume, |
| 1028 | audio_io_handle_t output, |
| 1029 | int delayMs) |
| 1030 | { |
Glenn Kasten | fff6d71 | 2012-01-12 16:38:12 -0800 | [diff] [blame] | 1031 | return (int)mAudioCommandThread->volumeCommand(stream, volume, |
Glenn Kasten | 72ef00d | 2012-01-17 11:09:42 -0800 | [diff] [blame] | 1032 | output, delayMs); |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 1033 | } |
| 1034 | |
| 1035 | int AudioPolicyService::startTone(audio_policy_tone_t tone, |
| 1036 | audio_stream_type_t stream) |
| 1037 | { |
| 1038 | if (tone != AUDIO_POLICY_TONE_IN_CALL_NOTIFICATION) |
Steve Block | 29357bc | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 1039 | ALOGE("startTone: illegal tone requested (%d)", tone); |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 1040 | if (stream != AUDIO_STREAM_VOICE_CALL) |
Steve Block | 29357bc | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 1041 | ALOGE("startTone: illegal stream (%d) requested for tone %d", stream, |
Glenn Kasten | e53b9ea | 2012-03-12 16:29:55 -0700 | [diff] [blame] | 1042 | tone); |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 1043 | mTonePlaybackThread->startToneCommand(ToneGenerator::TONE_SUP_CALL_WAITING, |
| 1044 | AUDIO_STREAM_VOICE_CALL); |
| 1045 | return 0; |
| 1046 | } |
| 1047 | |
| 1048 | int AudioPolicyService::stopTone() |
| 1049 | { |
| 1050 | mTonePlaybackThread->stopToneCommand(); |
| 1051 | return 0; |
| 1052 | } |
| 1053 | |
| 1054 | int AudioPolicyService::setVoiceVolume(float volume, int delayMs) |
| 1055 | { |
| 1056 | return (int)mAudioCommandThread->voiceVolumeCommand(volume, delayMs); |
| 1057 | } |
| 1058 | |
Richard Fitzgerald | ad3af33 | 2013-03-25 16:54:37 +0000 | [diff] [blame^] | 1059 | bool AudioPolicyService::isOffloadSupported(const audio_offload_info_t& info) |
| 1060 | { |
| 1061 | return false; // stub function |
| 1062 | } |
| 1063 | |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 1064 | // ---------------------------------------------------------------------------- |
| 1065 | // Audio pre-processing configuration |
| 1066 | // ---------------------------------------------------------------------------- |
| 1067 | |
Glenn Kasten | 8dad0e3 | 2012-01-09 08:41:22 -0800 | [diff] [blame] | 1068 | /*static*/ const char * const AudioPolicyService::kInputSourceNames[AUDIO_SOURCE_CNT -1] = { |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 1069 | MIC_SRC_TAG, |
| 1070 | VOICE_UL_SRC_TAG, |
| 1071 | VOICE_DL_SRC_TAG, |
| 1072 | VOICE_CALL_SRC_TAG, |
| 1073 | CAMCORDER_SRC_TAG, |
| 1074 | VOICE_REC_SRC_TAG, |
| 1075 | VOICE_COMM_SRC_TAG |
| 1076 | }; |
| 1077 | |
| 1078 | // returns the audio_source_t enum corresponding to the input source name or |
| 1079 | // AUDIO_SOURCE_CNT is no match found |
| 1080 | audio_source_t AudioPolicyService::inputSourceNameToEnum(const char *name) |
| 1081 | { |
| 1082 | int i; |
| 1083 | for (i = AUDIO_SOURCE_MIC; i < AUDIO_SOURCE_CNT; i++) { |
| 1084 | if (strcmp(name, kInputSourceNames[i - AUDIO_SOURCE_MIC]) == 0) { |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 1085 | ALOGV("inputSourceNameToEnum found source %s %d", name, i); |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 1086 | break; |
| 1087 | } |
| 1088 | } |
| 1089 | return (audio_source_t)i; |
| 1090 | } |
| 1091 | |
| 1092 | size_t AudioPolicyService::growParamSize(char *param, |
| 1093 | size_t size, |
| 1094 | size_t *curSize, |
| 1095 | size_t *totSize) |
| 1096 | { |
| 1097 | // *curSize is at least sizeof(effect_param_t) + 2 * sizeof(int) |
| 1098 | size_t pos = ((*curSize - 1 ) / size + 1) * size; |
| 1099 | |
| 1100 | if (pos + size > *totSize) { |
| 1101 | while (pos + size > *totSize) { |
| 1102 | *totSize += ((*totSize + 7) / 8) * 4; |
| 1103 | } |
| 1104 | param = (char *)realloc(param, *totSize); |
| 1105 | } |
| 1106 | *curSize = pos + size; |
| 1107 | return pos; |
| 1108 | } |
| 1109 | |
| 1110 | size_t AudioPolicyService::readParamValue(cnode *node, |
| 1111 | char *param, |
| 1112 | size_t *curSize, |
| 1113 | size_t *totSize) |
| 1114 | { |
| 1115 | if (strncmp(node->name, SHORT_TAG, sizeof(SHORT_TAG) + 1) == 0) { |
| 1116 | size_t pos = growParamSize(param, sizeof(short), curSize, totSize); |
| 1117 | *(short *)((char *)param + pos) = (short)atoi(node->value); |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 1118 | ALOGV("readParamValue() reading short %d", *(short *)((char *)param + pos)); |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 1119 | return sizeof(short); |
| 1120 | } else if (strncmp(node->name, INT_TAG, sizeof(INT_TAG) + 1) == 0) { |
| 1121 | size_t pos = growParamSize(param, sizeof(int), curSize, totSize); |
| 1122 | *(int *)((char *)param + pos) = atoi(node->value); |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 1123 | ALOGV("readParamValue() reading int %d", *(int *)((char *)param + pos)); |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 1124 | return sizeof(int); |
| 1125 | } else if (strncmp(node->name, FLOAT_TAG, sizeof(FLOAT_TAG) + 1) == 0) { |
| 1126 | size_t pos = growParamSize(param, sizeof(float), curSize, totSize); |
| 1127 | *(float *)((char *)param + pos) = (float)atof(node->value); |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 1128 | ALOGV("readParamValue() reading float %f",*(float *)((char *)param + pos)); |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 1129 | return sizeof(float); |
| 1130 | } else if (strncmp(node->name, BOOL_TAG, sizeof(BOOL_TAG) + 1) == 0) { |
| 1131 | size_t pos = growParamSize(param, sizeof(bool), curSize, totSize); |
| 1132 | if (strncmp(node->value, "false", strlen("false") + 1) == 0) { |
| 1133 | *(bool *)((char *)param + pos) = false; |
| 1134 | } else { |
| 1135 | *(bool *)((char *)param + pos) = true; |
| 1136 | } |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 1137 | ALOGV("readParamValue() reading bool %s",*(bool *)((char *)param + pos) ? "true" : "false"); |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 1138 | return sizeof(bool); |
| 1139 | } else if (strncmp(node->name, STRING_TAG, sizeof(STRING_TAG) + 1) == 0) { |
| 1140 | size_t len = strnlen(node->value, EFFECT_STRING_LEN_MAX); |
| 1141 | if (*curSize + len + 1 > *totSize) { |
| 1142 | *totSize = *curSize + len + 1; |
| 1143 | param = (char *)realloc(param, *totSize); |
| 1144 | } |
| 1145 | strncpy(param + *curSize, node->value, len); |
| 1146 | *curSize += len; |
| 1147 | param[*curSize] = '\0'; |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 1148 | ALOGV("readParamValue() reading string %s", param + *curSize - len); |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 1149 | return len; |
| 1150 | } |
Steve Block | 5ff1dd5 | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 1151 | ALOGW("readParamValue() unknown param type %s", node->name); |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 1152 | return 0; |
| 1153 | } |
| 1154 | |
| 1155 | effect_param_t *AudioPolicyService::loadEffectParameter(cnode *root) |
| 1156 | { |
| 1157 | cnode *param; |
| 1158 | cnode *value; |
| 1159 | size_t curSize = sizeof(effect_param_t); |
| 1160 | size_t totSize = sizeof(effect_param_t) + 2 * sizeof(int); |
| 1161 | effect_param_t *fx_param = (effect_param_t *)malloc(totSize); |
| 1162 | |
| 1163 | param = config_find(root, PARAM_TAG); |
| 1164 | value = config_find(root, VALUE_TAG); |
| 1165 | if (param == NULL && value == NULL) { |
| 1166 | // try to parse simple parameter form {int int} |
| 1167 | param = root->first_child; |
Glenn Kasten | a0d6833 | 2012-01-27 16:47:15 -0800 | [diff] [blame] | 1168 | if (param != NULL) { |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 1169 | // Note: that a pair of random strings is read as 0 0 |
| 1170 | int *ptr = (int *)fx_param->data; |
| 1171 | int *ptr2 = (int *)((char *)param + sizeof(effect_param_t)); |
Steve Block | 5ff1dd5 | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 1172 | ALOGW("loadEffectParameter() ptr %p ptr2 %p", ptr, ptr2); |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 1173 | *ptr++ = atoi(param->name); |
| 1174 | *ptr = atoi(param->value); |
| 1175 | fx_param->psize = sizeof(int); |
| 1176 | fx_param->vsize = sizeof(int); |
| 1177 | return fx_param; |
| 1178 | } |
| 1179 | } |
| 1180 | if (param == NULL || value == NULL) { |
Steve Block | 5ff1dd5 | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 1181 | ALOGW("loadEffectParameter() invalid parameter description %s", root->name); |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 1182 | goto error; |
| 1183 | } |
| 1184 | |
| 1185 | fx_param->psize = 0; |
| 1186 | param = param->first_child; |
| 1187 | while (param) { |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 1188 | ALOGV("loadEffectParameter() reading param of type %s", param->name); |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 1189 | size_t size = readParamValue(param, (char *)fx_param, &curSize, &totSize); |
| 1190 | if (size == 0) { |
| 1191 | goto error; |
| 1192 | } |
| 1193 | fx_param->psize += size; |
| 1194 | param = param->next; |
| 1195 | } |
| 1196 | |
| 1197 | // align start of value field on 32 bit boundary |
| 1198 | curSize = ((curSize - 1 ) / sizeof(int) + 1) * sizeof(int); |
| 1199 | |
| 1200 | fx_param->vsize = 0; |
| 1201 | value = value->first_child; |
| 1202 | while (value) { |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 1203 | ALOGV("loadEffectParameter() reading value of type %s", value->name); |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 1204 | size_t size = readParamValue(value, (char *)fx_param, &curSize, &totSize); |
| 1205 | if (size == 0) { |
| 1206 | goto error; |
| 1207 | } |
| 1208 | fx_param->vsize += size; |
| 1209 | value = value->next; |
| 1210 | } |
| 1211 | |
| 1212 | return fx_param; |
| 1213 | |
| 1214 | error: |
| 1215 | delete fx_param; |
| 1216 | return NULL; |
| 1217 | } |
| 1218 | |
| 1219 | void AudioPolicyService::loadEffectParameters(cnode *root, Vector <effect_param_t *>& params) |
| 1220 | { |
| 1221 | cnode *node = root->first_child; |
| 1222 | while (node) { |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 1223 | ALOGV("loadEffectParameters() loading param %s", node->name); |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 1224 | effect_param_t *param = loadEffectParameter(node); |
| 1225 | if (param == NULL) { |
| 1226 | node = node->next; |
| 1227 | continue; |
| 1228 | } |
| 1229 | params.add(param); |
| 1230 | node = node->next; |
| 1231 | } |
| 1232 | } |
| 1233 | |
| 1234 | AudioPolicyService::InputSourceDesc *AudioPolicyService::loadInputSource( |
| 1235 | cnode *root, |
| 1236 | const Vector <EffectDesc *>& effects) |
| 1237 | { |
| 1238 | cnode *node = root->first_child; |
| 1239 | if (node == NULL) { |
Steve Block | 5ff1dd5 | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 1240 | ALOGW("loadInputSource() empty element %s", root->name); |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 1241 | return NULL; |
| 1242 | } |
| 1243 | InputSourceDesc *source = new InputSourceDesc(); |
| 1244 | while (node) { |
| 1245 | size_t i; |
| 1246 | for (i = 0; i < effects.size(); i++) { |
| 1247 | if (strncmp(effects[i]->mName, node->name, EFFECT_STRING_LEN_MAX) == 0) { |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 1248 | ALOGV("loadInputSource() found effect %s in list", node->name); |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 1249 | break; |
| 1250 | } |
| 1251 | } |
| 1252 | if (i == effects.size()) { |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 1253 | ALOGV("loadInputSource() effect %s not in list", node->name); |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 1254 | node = node->next; |
| 1255 | continue; |
| 1256 | } |
Glenn Kasten | 9fda4b8 | 2012-02-02 14:04:37 -0800 | [diff] [blame] | 1257 | EffectDesc *effect = new EffectDesc(*effects[i]); // deep copy |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 1258 | loadEffectParameters(node, effect->mParams); |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 1259 | ALOGV("loadInputSource() adding effect %s uuid %08x", effect->mName, effect->mUuid.timeLow); |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 1260 | source->mEffects.add(effect); |
| 1261 | node = node->next; |
| 1262 | } |
| 1263 | if (source->mEffects.size() == 0) { |
Steve Block | 5ff1dd5 | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 1264 | ALOGW("loadInputSource() no valid effects found in source %s", root->name); |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 1265 | delete source; |
| 1266 | return NULL; |
| 1267 | } |
| 1268 | return source; |
| 1269 | } |
| 1270 | |
| 1271 | status_t AudioPolicyService::loadInputSources(cnode *root, const Vector <EffectDesc *>& effects) |
| 1272 | { |
| 1273 | cnode *node = config_find(root, PREPROCESSING_TAG); |
| 1274 | if (node == NULL) { |
| 1275 | return -ENOENT; |
| 1276 | } |
| 1277 | node = node->first_child; |
| 1278 | while (node) { |
| 1279 | audio_source_t source = inputSourceNameToEnum(node->name); |
| 1280 | if (source == AUDIO_SOURCE_CNT) { |
Steve Block | 5ff1dd5 | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 1281 | ALOGW("loadInputSources() invalid input source %s", node->name); |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 1282 | node = node->next; |
| 1283 | continue; |
| 1284 | } |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 1285 | ALOGV("loadInputSources() loading input source %s", node->name); |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 1286 | InputSourceDesc *desc = loadInputSource(node, effects); |
| 1287 | if (desc == NULL) { |
| 1288 | node = node->next; |
| 1289 | continue; |
| 1290 | } |
| 1291 | mInputSources.add(source, desc); |
| 1292 | node = node->next; |
| 1293 | } |
| 1294 | return NO_ERROR; |
| 1295 | } |
| 1296 | |
| 1297 | AudioPolicyService::EffectDesc *AudioPolicyService::loadEffect(cnode *root) |
| 1298 | { |
| 1299 | cnode *node = config_find(root, UUID_TAG); |
| 1300 | if (node == NULL) { |
| 1301 | return NULL; |
| 1302 | } |
| 1303 | effect_uuid_t uuid; |
| 1304 | if (AudioEffect::stringToGuid(node->value, &uuid) != NO_ERROR) { |
Steve Block | 5ff1dd5 | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 1305 | ALOGW("loadEffect() invalid uuid %s", node->value); |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 1306 | return NULL; |
| 1307 | } |
Glenn Kasten | 9fda4b8 | 2012-02-02 14:04:37 -0800 | [diff] [blame] | 1308 | return new EffectDesc(root->name, uuid); |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 1309 | } |
| 1310 | |
| 1311 | status_t AudioPolicyService::loadEffects(cnode *root, Vector <EffectDesc *>& effects) |
| 1312 | { |
| 1313 | cnode *node = config_find(root, EFFECTS_TAG); |
| 1314 | if (node == NULL) { |
| 1315 | return -ENOENT; |
| 1316 | } |
| 1317 | node = node->first_child; |
| 1318 | while (node) { |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 1319 | ALOGV("loadEffects() loading effect %s", node->name); |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 1320 | EffectDesc *effect = loadEffect(node); |
| 1321 | if (effect == NULL) { |
| 1322 | node = node->next; |
| 1323 | continue; |
| 1324 | } |
| 1325 | effects.add(effect); |
| 1326 | node = node->next; |
| 1327 | } |
| 1328 | return NO_ERROR; |
| 1329 | } |
| 1330 | |
| 1331 | status_t AudioPolicyService::loadPreProcessorConfig(const char *path) |
| 1332 | { |
| 1333 | cnode *root; |
| 1334 | char *data; |
| 1335 | |
| 1336 | data = (char *)load_file(path, NULL); |
| 1337 | if (data == NULL) { |
| 1338 | return -ENODEV; |
| 1339 | } |
| 1340 | root = config_node("", ""); |
| 1341 | config_load(root, data); |
| 1342 | |
| 1343 | Vector <EffectDesc *> effects; |
| 1344 | loadEffects(root, effects); |
| 1345 | loadInputSources(root, effects); |
| 1346 | |
| 1347 | config_free(root); |
| 1348 | free(root); |
| 1349 | free(data); |
| 1350 | |
| 1351 | return NO_ERROR; |
| 1352 | } |
| 1353 | |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 1354 | /* implementation of the interface to the policy manager */ |
| 1355 | extern "C" { |
| 1356 | |
Eric Laurent | a4c5a55 | 2012-03-29 10:12:40 -0700 | [diff] [blame] | 1357 | |
| 1358 | static audio_module_handle_t aps_load_hw_module(void *service, |
| 1359 | const char *name) |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 1360 | { |
| 1361 | sp<IAudioFlinger> af = AudioSystem::get_audio_flinger(); |
Glenn Kasten | 7378ca5 | 2012-01-20 13:44:40 -0800 | [diff] [blame] | 1362 | if (af == 0) { |
Steve Block | 5ff1dd5 | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 1363 | ALOGW("%s: could not get AudioFlinger", __func__); |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 1364 | return 0; |
| 1365 | } |
| 1366 | |
Eric Laurent | a4c5a55 | 2012-03-29 10:12:40 -0700 | [diff] [blame] | 1367 | return af->loadHwModule(name); |
| 1368 | } |
| 1369 | |
| 1370 | // deprecated: replaced by aps_open_output_on_module() |
| 1371 | static audio_io_handle_t aps_open_output(void *service, |
| 1372 | audio_devices_t *pDevices, |
| 1373 | uint32_t *pSamplingRate, |
| 1374 | audio_format_t *pFormat, |
| 1375 | audio_channel_mask_t *pChannelMask, |
| 1376 | uint32_t *pLatencyMs, |
Eric Laurent | 0ca3cf9 | 2012-04-18 09:24:29 -0700 | [diff] [blame] | 1377 | audio_output_flags_t flags) |
Eric Laurent | a4c5a55 | 2012-03-29 10:12:40 -0700 | [diff] [blame] | 1378 | { |
| 1379 | sp<IAudioFlinger> af = AudioSystem::get_audio_flinger(); |
| 1380 | if (af == 0) { |
| 1381 | ALOGW("%s: could not get AudioFlinger", __func__); |
| 1382 | return 0; |
| 1383 | } |
| 1384 | |
| 1385 | return af->openOutput((audio_module_handle_t)0, pDevices, pSamplingRate, pFormat, pChannelMask, |
| 1386 | pLatencyMs, flags); |
| 1387 | } |
| 1388 | |
| 1389 | static audio_io_handle_t aps_open_output_on_module(void *service, |
| 1390 | audio_module_handle_t module, |
| 1391 | audio_devices_t *pDevices, |
| 1392 | uint32_t *pSamplingRate, |
| 1393 | audio_format_t *pFormat, |
| 1394 | audio_channel_mask_t *pChannelMask, |
| 1395 | uint32_t *pLatencyMs, |
Richard Fitzgerald | ad3af33 | 2013-03-25 16:54:37 +0000 | [diff] [blame^] | 1396 | audio_output_flags_t flags, |
| 1397 | const audio_offload_info_t *offloadInfo) |
Eric Laurent | a4c5a55 | 2012-03-29 10:12:40 -0700 | [diff] [blame] | 1398 | { |
| 1399 | sp<IAudioFlinger> af = AudioSystem::get_audio_flinger(); |
| 1400 | if (af == 0) { |
| 1401 | ALOGW("%s: could not get AudioFlinger", __func__); |
| 1402 | return 0; |
| 1403 | } |
Eric Laurent | a4c5a55 | 2012-03-29 10:12:40 -0700 | [diff] [blame] | 1404 | return af->openOutput(module, pDevices, pSamplingRate, pFormat, pChannelMask, |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 1405 | pLatencyMs, flags); |
| 1406 | } |
| 1407 | |
| 1408 | static audio_io_handle_t aps_open_dup_output(void *service, |
| 1409 | audio_io_handle_t output1, |
| 1410 | audio_io_handle_t output2) |
| 1411 | { |
| 1412 | sp<IAudioFlinger> af = AudioSystem::get_audio_flinger(); |
Glenn Kasten | 7378ca5 | 2012-01-20 13:44:40 -0800 | [diff] [blame] | 1413 | if (af == 0) { |
Steve Block | 5ff1dd5 | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 1414 | ALOGW("%s: could not get AudioFlinger", __func__); |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 1415 | return 0; |
| 1416 | } |
| 1417 | return af->openDuplicateOutput(output1, output2); |
| 1418 | } |
| 1419 | |
| 1420 | static int aps_close_output(void *service, audio_io_handle_t output) |
| 1421 | { |
| 1422 | sp<IAudioFlinger> af = AudioSystem::get_audio_flinger(); |
Glenn Kasten | 7378ca5 | 2012-01-20 13:44:40 -0800 | [diff] [blame] | 1423 | if (af == 0) |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 1424 | return PERMISSION_DENIED; |
| 1425 | |
| 1426 | return af->closeOutput(output); |
| 1427 | } |
| 1428 | |
| 1429 | static int aps_suspend_output(void *service, audio_io_handle_t output) |
| 1430 | { |
| 1431 | sp<IAudioFlinger> af = AudioSystem::get_audio_flinger(); |
Glenn Kasten | 7378ca5 | 2012-01-20 13:44:40 -0800 | [diff] [blame] | 1432 | if (af == 0) { |
Steve Block | 5ff1dd5 | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 1433 | ALOGW("%s: could not get AudioFlinger", __func__); |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 1434 | return PERMISSION_DENIED; |
| 1435 | } |
| 1436 | |
| 1437 | return af->suspendOutput(output); |
| 1438 | } |
| 1439 | |
| 1440 | static int aps_restore_output(void *service, audio_io_handle_t output) |
| 1441 | { |
| 1442 | sp<IAudioFlinger> af = AudioSystem::get_audio_flinger(); |
Glenn Kasten | 7378ca5 | 2012-01-20 13:44:40 -0800 | [diff] [blame] | 1443 | if (af == 0) { |
Steve Block | 5ff1dd5 | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 1444 | ALOGW("%s: could not get AudioFlinger", __func__); |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 1445 | return PERMISSION_DENIED; |
| 1446 | } |
| 1447 | |
| 1448 | return af->restoreOutput(output); |
| 1449 | } |
| 1450 | |
Glenn Kasten | 2001005 | 2012-06-22 13:43:51 -0700 | [diff] [blame] | 1451 | // deprecated: replaced by aps_open_input_on_module(), and acoustics parameter is ignored |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 1452 | static audio_io_handle_t aps_open_input(void *service, |
Eric Laurent | a4c5a55 | 2012-03-29 10:12:40 -0700 | [diff] [blame] | 1453 | audio_devices_t *pDevices, |
| 1454 | uint32_t *pSamplingRate, |
| 1455 | audio_format_t *pFormat, |
| 1456 | audio_channel_mask_t *pChannelMask, |
| 1457 | audio_in_acoustics_t acoustics) |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 1458 | { |
| 1459 | sp<IAudioFlinger> af = AudioSystem::get_audio_flinger(); |
Glenn Kasten | 7378ca5 | 2012-01-20 13:44:40 -0800 | [diff] [blame] | 1460 | if (af == 0) { |
Steve Block | 5ff1dd5 | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 1461 | ALOGW("%s: could not get AudioFlinger", __func__); |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 1462 | return 0; |
| 1463 | } |
| 1464 | |
Eric Laurent | a4c5a55 | 2012-03-29 10:12:40 -0700 | [diff] [blame] | 1465 | return af->openInput((audio_module_handle_t)0, pDevices, pSamplingRate, pFormat, pChannelMask); |
| 1466 | } |
| 1467 | |
| 1468 | static audio_io_handle_t aps_open_input_on_module(void *service, |
| 1469 | audio_module_handle_t module, |
| 1470 | audio_devices_t *pDevices, |
| 1471 | uint32_t *pSamplingRate, |
| 1472 | audio_format_t *pFormat, |
| 1473 | audio_channel_mask_t *pChannelMask) |
| 1474 | { |
| 1475 | sp<IAudioFlinger> af = AudioSystem::get_audio_flinger(); |
| 1476 | if (af == 0) { |
| 1477 | ALOGW("%s: could not get AudioFlinger", __func__); |
| 1478 | return 0; |
| 1479 | } |
| 1480 | |
| 1481 | return af->openInput(module, pDevices, pSamplingRate, pFormat, pChannelMask); |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 1482 | } |
| 1483 | |
| 1484 | static int aps_close_input(void *service, audio_io_handle_t input) |
| 1485 | { |
| 1486 | sp<IAudioFlinger> af = AudioSystem::get_audio_flinger(); |
Glenn Kasten | 7378ca5 | 2012-01-20 13:44:40 -0800 | [diff] [blame] | 1487 | if (af == 0) |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 1488 | return PERMISSION_DENIED; |
| 1489 | |
| 1490 | return af->closeInput(input); |
| 1491 | } |
| 1492 | |
| 1493 | static int aps_set_stream_output(void *service, audio_stream_type_t stream, |
| 1494 | audio_io_handle_t output) |
| 1495 | { |
| 1496 | sp<IAudioFlinger> af = AudioSystem::get_audio_flinger(); |
Glenn Kasten | 7378ca5 | 2012-01-20 13:44:40 -0800 | [diff] [blame] | 1497 | if (af == 0) |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 1498 | return PERMISSION_DENIED; |
| 1499 | |
| 1500 | return af->setStreamOutput(stream, output); |
| 1501 | } |
| 1502 | |
| 1503 | static int aps_move_effects(void *service, int session, |
| 1504 | audio_io_handle_t src_output, |
| 1505 | audio_io_handle_t dst_output) |
| 1506 | { |
| 1507 | sp<IAudioFlinger> af = AudioSystem::get_audio_flinger(); |
Glenn Kasten | 7378ca5 | 2012-01-20 13:44:40 -0800 | [diff] [blame] | 1508 | if (af == 0) |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 1509 | return PERMISSION_DENIED; |
| 1510 | |
Glenn Kasten | 72ef00d | 2012-01-17 11:09:42 -0800 | [diff] [blame] | 1511 | return af->moveEffects(session, src_output, dst_output); |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 1512 | } |
| 1513 | |
| 1514 | static char * aps_get_parameters(void *service, audio_io_handle_t io_handle, |
| 1515 | const char *keys) |
| 1516 | { |
| 1517 | String8 result = AudioSystem::getParameters(io_handle, String8(keys)); |
| 1518 | return strdup(result.string()); |
| 1519 | } |
| 1520 | |
| 1521 | static void aps_set_parameters(void *service, audio_io_handle_t io_handle, |
| 1522 | const char *kv_pairs, int delay_ms) |
| 1523 | { |
| 1524 | AudioPolicyService *audioPolicyService = (AudioPolicyService *)service; |
| 1525 | |
| 1526 | audioPolicyService->setParameters(io_handle, kv_pairs, delay_ms); |
| 1527 | } |
| 1528 | |
| 1529 | static int aps_set_stream_volume(void *service, audio_stream_type_t stream, |
| 1530 | float volume, audio_io_handle_t output, |
| 1531 | int delay_ms) |
| 1532 | { |
| 1533 | AudioPolicyService *audioPolicyService = (AudioPolicyService *)service; |
| 1534 | |
| 1535 | return audioPolicyService->setStreamVolume(stream, volume, output, |
| 1536 | delay_ms); |
| 1537 | } |
| 1538 | |
| 1539 | static int aps_start_tone(void *service, audio_policy_tone_t tone, |
| 1540 | audio_stream_type_t stream) |
| 1541 | { |
| 1542 | AudioPolicyService *audioPolicyService = (AudioPolicyService *)service; |
| 1543 | |
| 1544 | return audioPolicyService->startTone(tone, stream); |
| 1545 | } |
| 1546 | |
| 1547 | static int aps_stop_tone(void *service) |
| 1548 | { |
| 1549 | AudioPolicyService *audioPolicyService = (AudioPolicyService *)service; |
| 1550 | |
| 1551 | return audioPolicyService->stopTone(); |
| 1552 | } |
| 1553 | |
| 1554 | static int aps_set_voice_volume(void *service, float volume, int delay_ms) |
| 1555 | { |
| 1556 | AudioPolicyService *audioPolicyService = (AudioPolicyService *)service; |
| 1557 | |
| 1558 | return audioPolicyService->setVoiceVolume(volume, delay_ms); |
| 1559 | } |
| 1560 | |
| 1561 | }; // extern "C" |
| 1562 | |
| 1563 | namespace { |
| 1564 | struct audio_policy_service_ops aps_ops = { |
| 1565 | open_output : aps_open_output, |
| 1566 | open_duplicate_output : aps_open_dup_output, |
| 1567 | close_output : aps_close_output, |
| 1568 | suspend_output : aps_suspend_output, |
| 1569 | restore_output : aps_restore_output, |
| 1570 | open_input : aps_open_input, |
| 1571 | close_input : aps_close_input, |
| 1572 | set_stream_volume : aps_set_stream_volume, |
| 1573 | set_stream_output : aps_set_stream_output, |
| 1574 | set_parameters : aps_set_parameters, |
| 1575 | get_parameters : aps_get_parameters, |
| 1576 | start_tone : aps_start_tone, |
| 1577 | stop_tone : aps_stop_tone, |
| 1578 | set_voice_volume : aps_set_voice_volume, |
| 1579 | move_effects : aps_move_effects, |
Eric Laurent | a4c5a55 | 2012-03-29 10:12:40 -0700 | [diff] [blame] | 1580 | load_hw_module : aps_load_hw_module, |
| 1581 | open_output_on_module : aps_open_output_on_module, |
| 1582 | open_input_on_module : aps_open_input_on_module, |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 1583 | }; |
| 1584 | }; // namespace <unnamed> |
| 1585 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1586 | }; // namespace android |