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