| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1 | /* //device/include/server/AudioFlinger/AudioFlinger.cpp | 
|  | 2 | ** | 
|  | 3 | ** Copyright 2007, The Android Open Source Project | 
|  | 4 | ** | 
|  | 5 | ** Licensed under the Apache License, Version 2.0 (the "License"); | 
|  | 6 | ** you may not use this file except in compliance with the License. | 
|  | 7 | ** You may obtain a copy of the License at | 
|  | 8 | ** | 
|  | 9 | **     http://www.apache.org/licenses/LICENSE-2.0 | 
|  | 10 | ** | 
|  | 11 | ** Unless required by applicable law or agreed to in writing, software | 
|  | 12 | ** distributed under the License is distributed on an "AS IS" BASIS, | 
|  | 13 | ** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | 
|  | 14 | ** See the License for the specific language governing permissions and | 
|  | 15 | ** limitations under the License. | 
|  | 16 | */ | 
|  | 17 |  | 
|  | 18 |  | 
|  | 19 | #define LOG_TAG "AudioFlinger" | 
|  | 20 | //#define LOG_NDEBUG 0 | 
|  | 21 |  | 
|  | 22 | #include <math.h> | 
|  | 23 | #include <signal.h> | 
|  | 24 | #include <sys/time.h> | 
|  | 25 | #include <sys/resource.h> | 
|  | 26 |  | 
| Gloria Wang | 9ee159b | 2011-02-24 14:51:45 -0800 | [diff] [blame] | 27 | #include <binder/IPCThreadState.h> | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 28 | #include <binder/IServiceManager.h> | 
|  | 29 | #include <utils/Log.h> | 
|  | 30 | #include <binder/Parcel.h> | 
|  | 31 | #include <binder/IPCThreadState.h> | 
|  | 32 | #include <utils/String16.h> | 
|  | 33 | #include <utils/threads.h> | 
| Eric Laurent | 38ccae2 | 2011-03-28 18:37:07 -0700 | [diff] [blame] | 34 | #include <utils/Atomic.h> | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 35 |  | 
| Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 36 | #include <cutils/bitops.h> | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 37 | #include <cutils/properties.h> | 
| Glenn Kasten | f6b1678 | 2011-12-15 09:51:17 -0800 | [diff] [blame] | 38 | #include <cutils/compiler.h> | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 39 |  | 
| Gloria Wang | 9ee159b | 2011-02-24 14:51:45 -0800 | [diff] [blame] | 40 | #include <media/IMediaPlayerService.h> | 
| Glenn Kasten | 25b248e | 2012-01-03 15:28:29 -0800 | [diff] [blame] | 41 | #include <media/IMediaDeathNotifier.h> | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 42 |  | 
|  | 43 | #include <private/media/AudioTrackShared.h> | 
|  | 44 | #include <private/media/AudioEffectShared.h> | 
| Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 45 |  | 
| Dima Zavin | 6476024 | 2011-05-11 14:15:23 -0700 | [diff] [blame] | 46 | #include <system/audio.h> | 
| Dima Zavin | 7394a4f | 2011-06-13 18:16:26 -0700 | [diff] [blame] | 47 | #include <hardware/audio.h> | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 48 |  | 
|  | 49 | #include "AudioMixer.h" | 
|  | 50 | #include "AudioFlinger.h" | 
|  | 51 |  | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 52 | #include <media/EffectsFactoryApi.h> | 
| Eric Laurent | 6d8b694 | 2011-06-24 07:01:31 -0700 | [diff] [blame] | 53 | #include <audio_effects/effect_visualizer.h> | 
| Eric Laurent | 59bd0da | 2011-08-01 09:52:20 -0700 | [diff] [blame] | 54 | #include <audio_effects/effect_ns.h> | 
|  | 55 | #include <audio_effects/effect_aec.h> | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 56 |  | 
| Glenn Kasten | 3b21c50 | 2011-12-15 09:52:39 -0800 | [diff] [blame] | 57 | #include <audio_utils/primitives.h> | 
|  | 58 |  | 
| Glenn Kasten | 4d8d0c3 | 2011-07-08 15:26:12 -0700 | [diff] [blame] | 59 | #include <cpustats/ThreadCpuUsage.h> | 
| Eric Laurent | feb0db6 | 2011-07-22 09:04:31 -0700 | [diff] [blame] | 60 | #include <powermanager/PowerManager.h> | 
| Glenn Kasten | 4d8d0c3 | 2011-07-08 15:26:12 -0700 | [diff] [blame] | 61 | // #define DEBUG_CPU_USAGE 10  // log statistics every n wall clock seconds | 
|  | 62 |  | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 63 | // ---------------------------------------------------------------------------- | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 64 |  | 
| Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 65 |  | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 66 | namespace android { | 
|  | 67 |  | 
| Glenn Kasten | ec1d6b5 | 2011-12-12 09:04:45 -0800 | [diff] [blame] | 68 | static const char kDeadlockedString[] = "AudioFlinger may be deadlocked\n"; | 
|  | 69 | static const char kHardwareLockedString[] = "Hardware lock is taken\n"; | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 70 |  | 
|  | 71 | //static const nsecs_t kStandbyTimeInNsecs = seconds(3); | 
|  | 72 | static const float MAX_GAIN = 4096.0f; | 
| Glenn Kasten | b1cf75c | 2012-01-17 12:20:54 -0800 | [diff] [blame] | 73 | static const uint32_t MAX_GAIN_INT = 0x1000; | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 74 |  | 
|  | 75 | // retry counts for buffer fill timeout | 
|  | 76 | // 50 * ~20msecs = 1 second | 
|  | 77 | static const int8_t kMaxTrackRetries = 50; | 
|  | 78 | static const int8_t kMaxTrackStartupRetries = 50; | 
|  | 79 | // allow less retry attempts on direct output thread. | 
|  | 80 | // direct outputs can be a scarce resource in audio hardware and should | 
|  | 81 | // be released as quickly as possible. | 
|  | 82 | static const int8_t kMaxTrackRetriesDirect = 2; | 
|  | 83 |  | 
|  | 84 | static const int kDumpLockRetries = 50; | 
| Glenn Kasten | 7dede87 | 2011-12-13 11:04:14 -0800 | [diff] [blame] | 85 | static const int kDumpLockSleepUs = 20000; | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 86 |  | 
| Glenn Kasten | 7dede87 | 2011-12-13 11:04:14 -0800 | [diff] [blame] | 87 | // don't warn about blocked writes or record buffer overflows more often than this | 
|  | 88 | static const nsecs_t kWarningThrottleNs = seconds(5); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 89 |  | 
| Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 90 | // RecordThread loop sleep time upon application overrun or audio HAL read error | 
|  | 91 | static const int kRecordThreadSleepUs = 5000; | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 92 |  | 
| Glenn Kasten | 7dede87 | 2011-12-13 11:04:14 -0800 | [diff] [blame] | 93 | // maximum time to wait for setParameters to complete | 
|  | 94 | static const nsecs_t kSetParametersTimeoutNs = seconds(2); | 
| Eric Laurent | 60cd0a0 | 2011-09-13 11:40:21 -0700 | [diff] [blame] | 95 |  | 
| Eric Laurent | 7cafbb3 | 2011-11-22 18:50:29 -0800 | [diff] [blame] | 96 | // minimum sleep time for the mixer thread loop when tracks are active but in underrun | 
|  | 97 | static const uint32_t kMinThreadSleepTimeUs = 5000; | 
|  | 98 | // maximum divider applied to the active sleep time in the mixer thread loop | 
|  | 99 | static const uint32_t kMaxThreadSleepTimeShift = 2; | 
|  | 100 |  | 
|  | 101 |  | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 102 | // ---------------------------------------------------------------------------- | 
|  | 103 |  | 
|  | 104 | static bool recordingAllowed() { | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 105 | if (getpid() == IPCThreadState::self()->getCallingPid()) return true; | 
|  | 106 | bool ok = checkCallingPermission(String16("android.permission.RECORD_AUDIO")); | 
| Steve Block | 29357bc | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 107 | if (!ok) ALOGE("Request requires android.permission.RECORD_AUDIO"); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 108 | return ok; | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 109 | } | 
|  | 110 |  | 
|  | 111 | static bool settingsAllowed() { | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 112 | if (getpid() == IPCThreadState::self()->getCallingPid()) return true; | 
|  | 113 | bool ok = checkCallingPermission(String16("android.permission.MODIFY_AUDIO_SETTINGS")); | 
| Steve Block | 29357bc | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 114 | if (!ok) ALOGE("Request requires android.permission.MODIFY_AUDIO_SETTINGS"); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 115 | return ok; | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 116 | } | 
|  | 117 |  | 
| Gloria Wang | 9ee159b | 2011-02-24 14:51:45 -0800 | [diff] [blame] | 118 | // To collect the amplifier usage | 
|  | 119 | static void addBatteryData(uint32_t params) { | 
| Glenn Kasten | 25b248e | 2012-01-03 15:28:29 -0800 | [diff] [blame] | 120 | sp<IMediaPlayerService> service = IMediaDeathNotifier::getMediaPlayerService(); | 
|  | 121 | if (service == NULL) { | 
|  | 122 | // it already logged | 
| Gloria Wang | 9ee159b | 2011-02-24 14:51:45 -0800 | [diff] [blame] | 123 | return; | 
|  | 124 | } | 
|  | 125 |  | 
|  | 126 | service->addBatteryData(params); | 
|  | 127 | } | 
|  | 128 |  | 
| Dima Zavin | 799a70e | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 129 | static int load_audio_interface(const char *if_name, const hw_module_t **mod, | 
|  | 130 | audio_hw_device_t **dev) | 
|  | 131 | { | 
|  | 132 | int rc; | 
|  | 133 |  | 
|  | 134 | rc = hw_get_module_by_class(AUDIO_HARDWARE_MODULE_ID, if_name, mod); | 
|  | 135 | if (rc) | 
|  | 136 | goto out; | 
|  | 137 |  | 
|  | 138 | rc = audio_hw_device_open(*mod, dev); | 
| Steve Block | 29357bc | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 139 | ALOGE_IF(rc, "couldn't open audio hw device in %s.%s (%s)", | 
| Dima Zavin | 799a70e | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 140 | AUDIO_HARDWARE_MODULE_ID, if_name, strerror(-rc)); | 
|  | 141 | if (rc) | 
|  | 142 | goto out; | 
|  | 143 |  | 
|  | 144 | return 0; | 
|  | 145 |  | 
|  | 146 | out: | 
|  | 147 | *mod = NULL; | 
|  | 148 | *dev = NULL; | 
|  | 149 | return rc; | 
|  | 150 | } | 
|  | 151 |  | 
| Glenn Kasten | ec1d6b5 | 2011-12-12 09:04:45 -0800 | [diff] [blame] | 152 | static const char * const audio_interfaces[] = { | 
| Dima Zavin | 799a70e | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 153 | "primary", | 
|  | 154 | "a2dp", | 
|  | 155 | "usb", | 
|  | 156 | }; | 
|  | 157 | #define ARRAY_SIZE(x) (sizeof((x))/sizeof(((x)[0]))) | 
|  | 158 |  | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 159 | // ---------------------------------------------------------------------------- | 
|  | 160 |  | 
|  | 161 | AudioFlinger::AudioFlinger() | 
|  | 162 | : BnAudioFlinger(), | 
| Glenn Kasten | e0feee3 | 2011-12-13 11:53:26 -0800 | [diff] [blame] | 163 | mPrimaryHardwareDev(NULL), mMasterVolume(1.0f), mMasterMute(false), mNextUniqueId(1), | 
| Eric Laurent | bee5337 | 2011-08-29 12:42:48 -0700 | [diff] [blame] | 164 | mBtNrecIsOff(false) | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 165 | { | 
| Dima Zavin | 5a61d2f | 2011-04-19 19:04:32 -0700 | [diff] [blame] | 166 | } | 
|  | 167 |  | 
|  | 168 | void AudioFlinger::onFirstRef() | 
|  | 169 | { | 
| Dima Zavin | 799a70e | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 170 | int rc = 0; | 
| Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 171 |  | 
| Eric Laurent | 9357520 | 2011-01-18 18:39:02 -0800 | [diff] [blame] | 172 | Mutex::Autolock _l(mLock); | 
|  | 173 |  | 
| Dima Zavin | 799a70e | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 174 | /* TODO: move all this work into an Init() function */ | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 175 | mHardwareStatus = AUDIO_HW_IDLE; | 
|  | 176 |  | 
| Dima Zavin | 799a70e | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 177 | for (size_t i = 0; i < ARRAY_SIZE(audio_interfaces); i++) { | 
|  | 178 | const hw_module_t *mod; | 
|  | 179 | audio_hw_device_t *dev; | 
| Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 180 |  | 
| Dima Zavin | 799a70e | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 181 | rc = load_audio_interface(audio_interfaces[i], &mod, &dev); | 
|  | 182 | if (rc) | 
|  | 183 | continue; | 
|  | 184 |  | 
| Steve Block | df64d15 | 2012-01-04 20:05:49 +0000 | [diff] [blame] | 185 | ALOGI("Loaded %s audio interface from %s (%s)", audio_interfaces[i], | 
| Dima Zavin | 799a70e | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 186 | mod->name, mod->id); | 
|  | 187 | mAudioHwDevs.push(dev); | 
|  | 188 |  | 
|  | 189 | if (!mPrimaryHardwareDev) { | 
|  | 190 | mPrimaryHardwareDev = dev; | 
| Steve Block | df64d15 | 2012-01-04 20:05:49 +0000 | [diff] [blame] | 191 | ALOGI("Using '%s' (%s.%s) as the primary audio interface", | 
| Dima Zavin | 5a61d2f | 2011-04-19 19:04:32 -0700 | [diff] [blame] | 192 | mod->name, mod->id, audio_interfaces[i]); | 
| Dima Zavin | 799a70e | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 193 | } | 
|  | 194 | } | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 195 |  | 
|  | 196 | mHardwareStatus = AUDIO_HW_INIT; | 
| Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 197 |  | 
| Dima Zavin | 799a70e | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 198 | if (!mPrimaryHardwareDev || mAudioHwDevs.size() == 0) { | 
| Steve Block | 29357bc | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 199 | ALOGE("Primary audio interface not found"); | 
| Dima Zavin | 799a70e | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 200 | return; | 
|  | 201 | } | 
|  | 202 |  | 
|  | 203 | for (size_t i = 0; i < mAudioHwDevs.size(); i++) { | 
|  | 204 | audio_hw_device_t *dev = mAudioHwDevs[i]; | 
|  | 205 |  | 
|  | 206 | mHardwareStatus = AUDIO_HW_INIT; | 
|  | 207 | rc = dev->init_check(dev); | 
|  | 208 | if (rc == 0) { | 
|  | 209 | AutoMutex lock(mHardwareLock); | 
|  | 210 |  | 
|  | 211 | mMode = AUDIO_MODE_NORMAL; | 
|  | 212 | mHardwareStatus = AUDIO_HW_SET_MODE; | 
|  | 213 | dev->set_mode(dev, mMode); | 
|  | 214 | mHardwareStatus = AUDIO_HW_SET_MASTER_VOLUME; | 
|  | 215 | dev->set_master_volume(dev, 1.0f); | 
|  | 216 | mHardwareStatus = AUDIO_HW_IDLE; | 
|  | 217 | } | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 218 | } | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 219 | } | 
|  | 220 |  | 
| Dima Zavin | 5a61d2f | 2011-04-19 19:04:32 -0700 | [diff] [blame] | 221 | status_t AudioFlinger::initCheck() const | 
|  | 222 | { | 
|  | 223 | Mutex::Autolock _l(mLock); | 
|  | 224 | if (mPrimaryHardwareDev == NULL || mAudioHwDevs.size() == 0) | 
|  | 225 | return NO_INIT; | 
|  | 226 | return NO_ERROR; | 
|  | 227 | } | 
|  | 228 |  | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 229 | AudioFlinger::~AudioFlinger() | 
|  | 230 | { | 
| Dima Zavin | 799a70e | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 231 | int num_devs = mAudioHwDevs.size(); | 
|  | 232 |  | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 233 | while (!mRecordThreads.isEmpty()) { | 
|  | 234 | // closeInput() will remove first entry from mRecordThreads | 
|  | 235 | closeInput(mRecordThreads.keyAt(0)); | 
|  | 236 | } | 
|  | 237 | while (!mPlaybackThreads.isEmpty()) { | 
|  | 238 | // closeOutput() will remove first entry from mPlaybackThreads | 
|  | 239 | closeOutput(mPlaybackThreads.keyAt(0)); | 
|  | 240 | } | 
| Dima Zavin | 799a70e | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 241 |  | 
|  | 242 | for (int i = 0; i < num_devs; i++) { | 
|  | 243 | audio_hw_device_t *dev = mAudioHwDevs[i]; | 
|  | 244 | audio_hw_device_close(dev); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 245 | } | 
| Dima Zavin | 799a70e | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 246 | mAudioHwDevs.clear(); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 247 | } | 
|  | 248 |  | 
| Dima Zavin | 799a70e | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 249 | audio_hw_device_t* AudioFlinger::findSuitableHwDev_l(uint32_t devices) | 
|  | 250 | { | 
|  | 251 | /* first matching HW device is returned */ | 
|  | 252 | for (size_t i = 0; i < mAudioHwDevs.size(); i++) { | 
|  | 253 | audio_hw_device_t *dev = mAudioHwDevs[i]; | 
|  | 254 | if ((dev->get_supported_devices(dev) & devices) == devices) | 
|  | 255 | return dev; | 
|  | 256 | } | 
|  | 257 | return NULL; | 
|  | 258 | } | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 259 |  | 
|  | 260 | status_t AudioFlinger::dumpClients(int fd, const Vector<String16>& args) | 
|  | 261 | { | 
|  | 262 | const size_t SIZE = 256; | 
|  | 263 | char buffer[SIZE]; | 
|  | 264 | String8 result; | 
|  | 265 |  | 
|  | 266 | result.append("Clients:\n"); | 
|  | 267 | for (size_t i = 0; i < mClients.size(); ++i) { | 
|  | 268 | wp<Client> wClient = mClients.valueAt(i); | 
|  | 269 | if (wClient != 0) { | 
|  | 270 | sp<Client> client = wClient.promote(); | 
|  | 271 | if (client != 0) { | 
|  | 272 | snprintf(buffer, SIZE, "  pid: %d\n", client->pid()); | 
|  | 273 | result.append(buffer); | 
|  | 274 | } | 
|  | 275 | } | 
|  | 276 | } | 
| Marco Nelissen | 3a34bef | 2011-08-02 13:33:41 -0700 | [diff] [blame] | 277 |  | 
|  | 278 | result.append("Global session refs:\n"); | 
|  | 279 | result.append(" session pid cnt\n"); | 
|  | 280 | for (size_t i = 0; i < mAudioSessionRefs.size(); i++) { | 
|  | 281 | AudioSessionRef *r = mAudioSessionRefs[i]; | 
|  | 282 | snprintf(buffer, SIZE, " %7d %3d %3d\n", r->sessionid, r->pid, r->cnt); | 
|  | 283 | result.append(buffer); | 
|  | 284 | } | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 285 | write(fd, result.string(), result.size()); | 
|  | 286 | return NO_ERROR; | 
|  | 287 | } | 
|  | 288 |  | 
|  | 289 |  | 
|  | 290 | status_t AudioFlinger::dumpInternals(int fd, const Vector<String16>& args) | 
|  | 291 | { | 
|  | 292 | const size_t SIZE = 256; | 
|  | 293 | char buffer[SIZE]; | 
|  | 294 | String8 result; | 
| Glenn Kasten | a4454b4 | 2012-01-04 11:02:33 -0800 | [diff] [blame] | 295 | hardware_call_state hardwareStatus = mHardwareStatus; | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 296 |  | 
|  | 297 | snprintf(buffer, SIZE, "Hardware status: %d\n", hardwareStatus); | 
|  | 298 | result.append(buffer); | 
|  | 299 | write(fd, result.string(), result.size()); | 
|  | 300 | return NO_ERROR; | 
|  | 301 | } | 
|  | 302 |  | 
|  | 303 | status_t AudioFlinger::dumpPermissionDenial(int fd, const Vector<String16>& args) | 
|  | 304 | { | 
|  | 305 | const size_t SIZE = 256; | 
|  | 306 | char buffer[SIZE]; | 
|  | 307 | String8 result; | 
|  | 308 | snprintf(buffer, SIZE, "Permission Denial: " | 
|  | 309 | "can't dump AudioFlinger from pid=%d, uid=%d\n", | 
|  | 310 | IPCThreadState::self()->getCallingPid(), | 
|  | 311 | IPCThreadState::self()->getCallingUid()); | 
|  | 312 | result.append(buffer); | 
|  | 313 | write(fd, result.string(), result.size()); | 
|  | 314 | return NO_ERROR; | 
|  | 315 | } | 
|  | 316 |  | 
|  | 317 | static bool tryLock(Mutex& mutex) | 
|  | 318 | { | 
|  | 319 | bool locked = false; | 
|  | 320 | for (int i = 0; i < kDumpLockRetries; ++i) { | 
|  | 321 | if (mutex.tryLock() == NO_ERROR) { | 
|  | 322 | locked = true; | 
|  | 323 | break; | 
|  | 324 | } | 
| Glenn Kasten | 7dede87 | 2011-12-13 11:04:14 -0800 | [diff] [blame] | 325 | usleep(kDumpLockSleepUs); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 326 | } | 
|  | 327 | return locked; | 
|  | 328 | } | 
|  | 329 |  | 
|  | 330 | status_t AudioFlinger::dump(int fd, const Vector<String16>& args) | 
|  | 331 | { | 
| Glenn Kasten | f1d4592 | 2012-01-13 15:54:24 -0800 | [diff] [blame] | 332 | if (!checkCallingPermission(String16("android.permission.DUMP"))) { | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 333 | dumpPermissionDenial(fd, args); | 
|  | 334 | } else { | 
|  | 335 | // get state of hardware lock | 
|  | 336 | bool hardwareLocked = tryLock(mHardwareLock); | 
|  | 337 | if (!hardwareLocked) { | 
|  | 338 | String8 result(kHardwareLockedString); | 
|  | 339 | write(fd, result.string(), result.size()); | 
|  | 340 | } else { | 
|  | 341 | mHardwareLock.unlock(); | 
|  | 342 | } | 
|  | 343 |  | 
|  | 344 | bool locked = tryLock(mLock); | 
|  | 345 |  | 
|  | 346 | // failed to lock - AudioFlinger is probably deadlocked | 
|  | 347 | if (!locked) { | 
|  | 348 | String8 result(kDeadlockedString); | 
|  | 349 | write(fd, result.string(), result.size()); | 
|  | 350 | } | 
|  | 351 |  | 
|  | 352 | dumpClients(fd, args); | 
|  | 353 | dumpInternals(fd, args); | 
|  | 354 |  | 
|  | 355 | // dump playback threads | 
|  | 356 | for (size_t i = 0; i < mPlaybackThreads.size(); i++) { | 
|  | 357 | mPlaybackThreads.valueAt(i)->dump(fd, args); | 
|  | 358 | } | 
|  | 359 |  | 
|  | 360 | // dump record threads | 
|  | 361 | for (size_t i = 0; i < mRecordThreads.size(); i++) { | 
|  | 362 | mRecordThreads.valueAt(i)->dump(fd, args); | 
|  | 363 | } | 
|  | 364 |  | 
| Dima Zavin | 799a70e | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 365 | // dump all hardware devs | 
|  | 366 | for (size_t i = 0; i < mAudioHwDevs.size(); i++) { | 
|  | 367 | audio_hw_device_t *dev = mAudioHwDevs[i]; | 
|  | 368 | dev->dump(dev, fd); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 369 | } | 
|  | 370 | if (locked) mLock.unlock(); | 
|  | 371 | } | 
|  | 372 | return NO_ERROR; | 
|  | 373 | } | 
|  | 374 |  | 
|  | 375 |  | 
|  | 376 | // IAudioFlinger interface | 
|  | 377 |  | 
|  | 378 |  | 
|  | 379 | sp<IAudioTrack> AudioFlinger::createTrack( | 
|  | 380 | pid_t pid, | 
| Glenn Kasten | fff6d71 | 2012-01-12 16:38:12 -0800 | [diff] [blame] | 381 | audio_stream_type_t streamType, | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 382 | uint32_t sampleRate, | 
| Glenn Kasten | 58f3021 | 2012-01-12 12:27:51 -0800 | [diff] [blame] | 383 | audio_format_t format, | 
| Jean-Michel Trivi | 0d255b2 | 2011-05-24 15:53:33 -0700 | [diff] [blame] | 384 | uint32_t channelMask, | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 385 | int frameCount, | 
|  | 386 | uint32_t flags, | 
|  | 387 | const sp<IMemory>& sharedBuffer, | 
|  | 388 | int output, | 
|  | 389 | int *sessionId, | 
|  | 390 | status_t *status) | 
|  | 391 | { | 
|  | 392 | sp<PlaybackThread::Track> track; | 
|  | 393 | sp<TrackHandle> trackHandle; | 
|  | 394 | sp<Client> client; | 
|  | 395 | wp<Client> wclient; | 
|  | 396 | status_t lStatus; | 
|  | 397 | int lSessionId; | 
|  | 398 |  | 
| Glenn Kasten | 263709e | 2012-01-06 08:40:01 -0800 | [diff] [blame] | 399 | // client AudioTrack::set already implements AUDIO_STREAM_DEFAULT => AUDIO_STREAM_MUSIC, | 
|  | 400 | // but if someone uses binder directly they could bypass that and cause us to crash | 
|  | 401 | if (uint32_t(streamType) >= AUDIO_STREAM_CNT) { | 
| Steve Block | 29357bc | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 402 | ALOGE("createTrack() invalid stream type %d", streamType); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 403 | lStatus = BAD_VALUE; | 
|  | 404 | goto Exit; | 
|  | 405 | } | 
|  | 406 |  | 
|  | 407 | { | 
|  | 408 | Mutex::Autolock _l(mLock); | 
|  | 409 | PlaybackThread *thread = checkPlaybackThread_l(output); | 
| Eric Laurent | 39e94f8 | 2010-07-28 01:32:47 -0700 | [diff] [blame] | 410 | PlaybackThread *effectThread = NULL; | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 411 | if (thread == NULL) { | 
| Steve Block | 29357bc | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 412 | ALOGE("unknown output thread"); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 413 | lStatus = BAD_VALUE; | 
|  | 414 | goto Exit; | 
|  | 415 | } | 
|  | 416 |  | 
|  | 417 | wclient = mClients.valueFor(pid); | 
|  | 418 |  | 
|  | 419 | if (wclient != NULL) { | 
|  | 420 | client = wclient.promote(); | 
|  | 421 | } else { | 
|  | 422 | client = new Client(this, pid); | 
|  | 423 | mClients.add(pid, client); | 
|  | 424 | } | 
|  | 425 |  | 
| Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 426 | ALOGV("createTrack() sessionId: %d", (sessionId == NULL) ? -2 : *sessionId); | 
| Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 427 | if (sessionId != NULL && *sessionId != AUDIO_SESSION_OUTPUT_MIX) { | 
| Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 428 | for (size_t i = 0; i < mPlaybackThreads.size(); i++) { | 
| Eric Laurent | 39e94f8 | 2010-07-28 01:32:47 -0700 | [diff] [blame] | 429 | sp<PlaybackThread> t = mPlaybackThreads.valueAt(i); | 
|  | 430 | if (mPlaybackThreads.keyAt(i) != output) { | 
|  | 431 | // prevent same audio session on different output threads | 
|  | 432 | uint32_t sessions = t->hasAudioSession(*sessionId); | 
|  | 433 | if (sessions & PlaybackThread::TRACK_SESSION) { | 
| Steve Block | 29357bc | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 434 | ALOGE("createTrack() session ID %d already in use", *sessionId); | 
| Eric Laurent | 39e94f8 | 2010-07-28 01:32:47 -0700 | [diff] [blame] | 435 | lStatus = BAD_VALUE; | 
|  | 436 | goto Exit; | 
|  | 437 | } | 
|  | 438 | // check if an effect with same session ID is waiting for a track to be created | 
|  | 439 | if (sessions & PlaybackThread::EFFECT_SESSION) { | 
|  | 440 | effectThread = t.get(); | 
|  | 441 | } | 
| Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 442 | } | 
|  | 443 | } | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 444 | lSessionId = *sessionId; | 
|  | 445 | } else { | 
| Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 446 | // if no audio session id is provided, create one here | 
| Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 447 | lSessionId = nextUniqueId(); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 448 | if (sessionId != NULL) { | 
|  | 449 | *sessionId = lSessionId; | 
|  | 450 | } | 
|  | 451 | } | 
| Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 452 | ALOGV("createTrack() lSessionId: %d", lSessionId); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 453 |  | 
|  | 454 | track = thread->createTrack_l(client, streamType, sampleRate, format, | 
| Jean-Michel Trivi | 0d255b2 | 2011-05-24 15:53:33 -0700 | [diff] [blame] | 455 | channelMask, frameCount, sharedBuffer, lSessionId, &lStatus); | 
| Eric Laurent | 39e94f8 | 2010-07-28 01:32:47 -0700 | [diff] [blame] | 456 |  | 
|  | 457 | // move effect chain to this output thread if an effect on same session was waiting | 
|  | 458 | // for a track to be created | 
|  | 459 | if (lStatus == NO_ERROR && effectThread != NULL) { | 
|  | 460 | Mutex::Autolock _dl(thread->mLock); | 
|  | 461 | Mutex::Autolock _sl(effectThread->mLock); | 
|  | 462 | moveEffectChain_l(lSessionId, effectThread, thread, true); | 
|  | 463 | } | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 464 | } | 
|  | 465 | if (lStatus == NO_ERROR) { | 
|  | 466 | trackHandle = new TrackHandle(track); | 
|  | 467 | } else { | 
|  | 468 | // remove local strong reference to Client before deleting the Track so that the Client | 
|  | 469 | // destructor is called by the TrackBase destructor with mLock held | 
|  | 470 | client.clear(); | 
|  | 471 | track.clear(); | 
|  | 472 | } | 
|  | 473 |  | 
|  | 474 | Exit: | 
|  | 475 | if(status) { | 
|  | 476 | *status = lStatus; | 
|  | 477 | } | 
|  | 478 | return trackHandle; | 
|  | 479 | } | 
|  | 480 |  | 
|  | 481 | uint32_t AudioFlinger::sampleRate(int output) const | 
|  | 482 | { | 
|  | 483 | Mutex::Autolock _l(mLock); | 
|  | 484 | PlaybackThread *thread = checkPlaybackThread_l(output); | 
|  | 485 | if (thread == NULL) { | 
| Steve Block | 5ff1dd5 | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 486 | ALOGW("sampleRate() unknown thread %d", output); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 487 | return 0; | 
|  | 488 | } | 
|  | 489 | return thread->sampleRate(); | 
|  | 490 | } | 
|  | 491 |  | 
|  | 492 | int AudioFlinger::channelCount(int output) const | 
|  | 493 | { | 
|  | 494 | Mutex::Autolock _l(mLock); | 
|  | 495 | PlaybackThread *thread = checkPlaybackThread_l(output); | 
|  | 496 | if (thread == NULL) { | 
| Steve Block | 5ff1dd5 | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 497 | ALOGW("channelCount() unknown thread %d", output); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 498 | return 0; | 
|  | 499 | } | 
|  | 500 | return thread->channelCount(); | 
|  | 501 | } | 
|  | 502 |  | 
| Glenn Kasten | 58f3021 | 2012-01-12 12:27:51 -0800 | [diff] [blame] | 503 | audio_format_t AudioFlinger::format(int output) const | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 504 | { | 
|  | 505 | Mutex::Autolock _l(mLock); | 
|  | 506 | PlaybackThread *thread = checkPlaybackThread_l(output); | 
|  | 507 | if (thread == NULL) { | 
| Steve Block | 5ff1dd5 | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 508 | ALOGW("format() unknown thread %d", output); | 
| Glenn Kasten | 58f3021 | 2012-01-12 12:27:51 -0800 | [diff] [blame] | 509 | return AUDIO_FORMAT_INVALID; | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 510 | } | 
|  | 511 | return thread->format(); | 
|  | 512 | } | 
|  | 513 |  | 
|  | 514 | size_t AudioFlinger::frameCount(int output) const | 
|  | 515 | { | 
|  | 516 | Mutex::Autolock _l(mLock); | 
|  | 517 | PlaybackThread *thread = checkPlaybackThread_l(output); | 
|  | 518 | if (thread == NULL) { | 
| Steve Block | 5ff1dd5 | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 519 | ALOGW("frameCount() unknown thread %d", output); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 520 | return 0; | 
|  | 521 | } | 
|  | 522 | return thread->frameCount(); | 
|  | 523 | } | 
|  | 524 |  | 
|  | 525 | uint32_t AudioFlinger::latency(int output) const | 
|  | 526 | { | 
|  | 527 | Mutex::Autolock _l(mLock); | 
|  | 528 | PlaybackThread *thread = checkPlaybackThread_l(output); | 
|  | 529 | if (thread == NULL) { | 
| Steve Block | 5ff1dd5 | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 530 | ALOGW("latency() unknown thread %d", output); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 531 | return 0; | 
|  | 532 | } | 
|  | 533 | return thread->latency(); | 
|  | 534 | } | 
|  | 535 |  | 
|  | 536 | status_t AudioFlinger::setMasterVolume(float value) | 
|  | 537 | { | 
| Eric Laurent | a1884f9 | 2011-08-23 08:25:03 -0700 | [diff] [blame] | 538 | status_t ret = initCheck(); | 
|  | 539 | if (ret != NO_ERROR) { | 
|  | 540 | return ret; | 
|  | 541 | } | 
|  | 542 |  | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 543 | // check calling permissions | 
|  | 544 | if (!settingsAllowed()) { | 
|  | 545 | return PERMISSION_DENIED; | 
|  | 546 | } | 
|  | 547 |  | 
|  | 548 | // when hw supports master volume, don't scale in sw mixer | 
| Eric Laurent | 9357520 | 2011-01-18 18:39:02 -0800 | [diff] [blame] | 549 | { // scope for the lock | 
|  | 550 | AutoMutex lock(mHardwareLock); | 
|  | 551 | mHardwareStatus = AUDIO_HW_SET_MASTER_VOLUME; | 
| Dima Zavin | 799a70e | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 552 | if (mPrimaryHardwareDev->set_master_volume(mPrimaryHardwareDev, value) == NO_ERROR) { | 
| Eric Laurent | 9357520 | 2011-01-18 18:39:02 -0800 | [diff] [blame] | 553 | value = 1.0f; | 
|  | 554 | } | 
|  | 555 | mHardwareStatus = AUDIO_HW_IDLE; | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 556 | } | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 557 |  | 
| Eric Laurent | 9357520 | 2011-01-18 18:39:02 -0800 | [diff] [blame] | 558 | Mutex::Autolock _l(mLock); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 559 | mMasterVolume = value; | 
|  | 560 | for (uint32_t i = 0; i < mPlaybackThreads.size(); i++) | 
|  | 561 | mPlaybackThreads.valueAt(i)->setMasterVolume(value); | 
|  | 562 |  | 
|  | 563 | return NO_ERROR; | 
|  | 564 | } | 
|  | 565 |  | 
| Glenn Kasten | f78aee7 | 2012-01-04 11:00:47 -0800 | [diff] [blame] | 566 | status_t AudioFlinger::setMode(audio_mode_t mode) | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 567 | { | 
| Eric Laurent | a1884f9 | 2011-08-23 08:25:03 -0700 | [diff] [blame] | 568 | status_t ret = initCheck(); | 
|  | 569 | if (ret != NO_ERROR) { | 
|  | 570 | return ret; | 
|  | 571 | } | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 572 |  | 
|  | 573 | // check calling permissions | 
|  | 574 | if (!settingsAllowed()) { | 
|  | 575 | return PERMISSION_DENIED; | 
|  | 576 | } | 
| Glenn Kasten | 930f4ca | 2012-01-06 16:47:31 -0800 | [diff] [blame] | 577 | if (uint32_t(mode) >= AUDIO_MODE_CNT) { | 
| Steve Block | 5ff1dd5 | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 578 | ALOGW("Illegal value: setMode(%d)", mode); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 579 | return BAD_VALUE; | 
|  | 580 | } | 
|  | 581 |  | 
|  | 582 | { // scope for the lock | 
|  | 583 | AutoMutex lock(mHardwareLock); | 
|  | 584 | mHardwareStatus = AUDIO_HW_SET_MODE; | 
| Dima Zavin | 799a70e | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 585 | ret = mPrimaryHardwareDev->set_mode(mPrimaryHardwareDev, mode); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 586 | mHardwareStatus = AUDIO_HW_IDLE; | 
|  | 587 | } | 
|  | 588 |  | 
|  | 589 | if (NO_ERROR == ret) { | 
|  | 590 | Mutex::Autolock _l(mLock); | 
|  | 591 | mMode = mode; | 
|  | 592 | for (uint32_t i = 0; i < mPlaybackThreads.size(); i++) | 
|  | 593 | mPlaybackThreads.valueAt(i)->setMode(mode); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 594 | } | 
|  | 595 |  | 
|  | 596 | return ret; | 
|  | 597 | } | 
|  | 598 |  | 
|  | 599 | status_t AudioFlinger::setMicMute(bool state) | 
|  | 600 | { | 
| Eric Laurent | a1884f9 | 2011-08-23 08:25:03 -0700 | [diff] [blame] | 601 | status_t ret = initCheck(); | 
|  | 602 | if (ret != NO_ERROR) { | 
|  | 603 | return ret; | 
|  | 604 | } | 
|  | 605 |  | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 606 | // check calling permissions | 
|  | 607 | if (!settingsAllowed()) { | 
|  | 608 | return PERMISSION_DENIED; | 
|  | 609 | } | 
|  | 610 |  | 
|  | 611 | AutoMutex lock(mHardwareLock); | 
|  | 612 | mHardwareStatus = AUDIO_HW_SET_MIC_MUTE; | 
| Eric Laurent | a1884f9 | 2011-08-23 08:25:03 -0700 | [diff] [blame] | 613 | ret = mPrimaryHardwareDev->set_mic_mute(mPrimaryHardwareDev, state); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 614 | mHardwareStatus = AUDIO_HW_IDLE; | 
|  | 615 | return ret; | 
|  | 616 | } | 
|  | 617 |  | 
|  | 618 | bool AudioFlinger::getMicMute() const | 
|  | 619 | { | 
| Eric Laurent | a1884f9 | 2011-08-23 08:25:03 -0700 | [diff] [blame] | 620 | status_t ret = initCheck(); | 
|  | 621 | if (ret != NO_ERROR) { | 
|  | 622 | return false; | 
|  | 623 | } | 
|  | 624 |  | 
| Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 625 | bool state = AUDIO_MODE_INVALID; | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 626 | mHardwareStatus = AUDIO_HW_GET_MIC_MUTE; | 
| Dima Zavin | 799a70e | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 627 | mPrimaryHardwareDev->get_mic_mute(mPrimaryHardwareDev, &state); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 628 | mHardwareStatus = AUDIO_HW_IDLE; | 
|  | 629 | return state; | 
|  | 630 | } | 
|  | 631 |  | 
|  | 632 | status_t AudioFlinger::setMasterMute(bool muted) | 
|  | 633 | { | 
|  | 634 | // check calling permissions | 
|  | 635 | if (!settingsAllowed()) { | 
|  | 636 | return PERMISSION_DENIED; | 
|  | 637 | } | 
|  | 638 |  | 
| Eric Laurent | 9357520 | 2011-01-18 18:39:02 -0800 | [diff] [blame] | 639 | Mutex::Autolock _l(mLock); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 640 | mMasterMute = muted; | 
|  | 641 | for (uint32_t i = 0; i < mPlaybackThreads.size(); i++) | 
|  | 642 | mPlaybackThreads.valueAt(i)->setMasterMute(muted); | 
|  | 643 |  | 
|  | 644 | return NO_ERROR; | 
|  | 645 | } | 
|  | 646 |  | 
|  | 647 | float AudioFlinger::masterVolume() const | 
|  | 648 | { | 
| Glenn Kasten | 9806710 | 2011-12-13 11:47:54 -0800 | [diff] [blame] | 649 | Mutex::Autolock _l(mLock); | 
|  | 650 | return masterVolume_l(); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 651 | } | 
|  | 652 |  | 
|  | 653 | bool AudioFlinger::masterMute() const | 
|  | 654 | { | 
| Glenn Kasten | 9806710 | 2011-12-13 11:47:54 -0800 | [diff] [blame] | 655 | Mutex::Autolock _l(mLock); | 
|  | 656 | return masterMute_l(); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 657 | } | 
|  | 658 |  | 
| Glenn Kasten | fff6d71 | 2012-01-12 16:38:12 -0800 | [diff] [blame] | 659 | status_t AudioFlinger::setStreamVolume(audio_stream_type_t stream, float value, int output) | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 660 | { | 
|  | 661 | // check calling permissions | 
|  | 662 | if (!settingsAllowed()) { | 
|  | 663 | return PERMISSION_DENIED; | 
|  | 664 | } | 
|  | 665 |  | 
| Glenn Kasten | 263709e | 2012-01-06 08:40:01 -0800 | [diff] [blame] | 666 | if (uint32_t(stream) >= AUDIO_STREAM_CNT) { | 
| Steve Block | 29357bc | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 667 | ALOGE("setStreamVolume() invalid stream %d", stream); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 668 | return BAD_VALUE; | 
|  | 669 | } | 
|  | 670 |  | 
|  | 671 | AutoMutex lock(mLock); | 
|  | 672 | PlaybackThread *thread = NULL; | 
|  | 673 | if (output) { | 
|  | 674 | thread = checkPlaybackThread_l(output); | 
|  | 675 | if (thread == NULL) { | 
|  | 676 | return BAD_VALUE; | 
|  | 677 | } | 
|  | 678 | } | 
|  | 679 |  | 
|  | 680 | mStreamTypes[stream].volume = value; | 
|  | 681 |  | 
|  | 682 | if (thread == NULL) { | 
|  | 683 | for (uint32_t i = 0; i < mPlaybackThreads.size(); i++) { | 
|  | 684 | mPlaybackThreads.valueAt(i)->setStreamVolume(stream, value); | 
|  | 685 | } | 
|  | 686 | } else { | 
|  | 687 | thread->setStreamVolume(stream, value); | 
|  | 688 | } | 
|  | 689 |  | 
|  | 690 | return NO_ERROR; | 
|  | 691 | } | 
|  | 692 |  | 
| Glenn Kasten | fff6d71 | 2012-01-12 16:38:12 -0800 | [diff] [blame] | 693 | status_t AudioFlinger::setStreamMute(audio_stream_type_t stream, bool muted) | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 694 | { | 
|  | 695 | // check calling permissions | 
|  | 696 | if (!settingsAllowed()) { | 
|  | 697 | return PERMISSION_DENIED; | 
|  | 698 | } | 
|  | 699 |  | 
| Glenn Kasten | 263709e | 2012-01-06 08:40:01 -0800 | [diff] [blame] | 700 | if (uint32_t(stream) >= AUDIO_STREAM_CNT || | 
| Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 701 | uint32_t(stream) == AUDIO_STREAM_ENFORCED_AUDIBLE) { | 
| Steve Block | 29357bc | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 702 | ALOGE("setStreamMute() invalid stream %d", stream); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 703 | return BAD_VALUE; | 
|  | 704 | } | 
|  | 705 |  | 
| Eric Laurent | 9357520 | 2011-01-18 18:39:02 -0800 | [diff] [blame] | 706 | AutoMutex lock(mLock); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 707 | mStreamTypes[stream].mute = muted; | 
|  | 708 | for (uint32_t i = 0; i < mPlaybackThreads.size(); i++) | 
|  | 709 | mPlaybackThreads.valueAt(i)->setStreamMute(stream, muted); | 
|  | 710 |  | 
|  | 711 | return NO_ERROR; | 
|  | 712 | } | 
|  | 713 |  | 
| Glenn Kasten | fff6d71 | 2012-01-12 16:38:12 -0800 | [diff] [blame] | 714 | float AudioFlinger::streamVolume(audio_stream_type_t stream, int output) const | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 715 | { | 
| Glenn Kasten | 263709e | 2012-01-06 08:40:01 -0800 | [diff] [blame] | 716 | if (uint32_t(stream) >= AUDIO_STREAM_CNT) { | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 717 | return 0.0f; | 
|  | 718 | } | 
|  | 719 |  | 
|  | 720 | AutoMutex lock(mLock); | 
|  | 721 | float volume; | 
|  | 722 | if (output) { | 
|  | 723 | PlaybackThread *thread = checkPlaybackThread_l(output); | 
|  | 724 | if (thread == NULL) { | 
|  | 725 | return 0.0f; | 
|  | 726 | } | 
|  | 727 | volume = thread->streamVolume(stream); | 
|  | 728 | } else { | 
|  | 729 | volume = mStreamTypes[stream].volume; | 
|  | 730 | } | 
|  | 731 |  | 
|  | 732 | return volume; | 
|  | 733 | } | 
|  | 734 |  | 
| Glenn Kasten | fff6d71 | 2012-01-12 16:38:12 -0800 | [diff] [blame] | 735 | bool AudioFlinger::streamMute(audio_stream_type_t stream) const | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 736 | { | 
| Glenn Kasten | 263709e | 2012-01-06 08:40:01 -0800 | [diff] [blame] | 737 | if (uint32_t(stream) >= AUDIO_STREAM_CNT) { | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 738 | return true; | 
|  | 739 | } | 
|  | 740 |  | 
|  | 741 | return mStreamTypes[stream].mute; | 
|  | 742 | } | 
|  | 743 |  | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 744 | status_t AudioFlinger::setParameters(int ioHandle, const String8& keyValuePairs) | 
|  | 745 | { | 
|  | 746 | status_t result; | 
|  | 747 |  | 
| Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 748 | ALOGV("setParameters(): io %d, keyvalue %s, tid %d, calling tid %d", | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 749 | ioHandle, keyValuePairs.string(), gettid(), IPCThreadState::self()->getCallingPid()); | 
|  | 750 | // check calling permissions | 
|  | 751 | if (!settingsAllowed()) { | 
|  | 752 | return PERMISSION_DENIED; | 
|  | 753 | } | 
|  | 754 |  | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 755 | // ioHandle == 0 means the parameters are global to the audio hardware interface | 
|  | 756 | if (ioHandle == 0) { | 
|  | 757 | AutoMutex lock(mHardwareLock); | 
|  | 758 | mHardwareStatus = AUDIO_SET_PARAMETER; | 
| Dima Zavin | 799a70e | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 759 | status_t final_result = NO_ERROR; | 
|  | 760 | for (size_t i = 0; i < mAudioHwDevs.size(); i++) { | 
|  | 761 | audio_hw_device_t *dev = mAudioHwDevs[i]; | 
|  | 762 | result = dev->set_parameters(dev, keyValuePairs.string()); | 
|  | 763 | final_result = result ?: final_result; | 
|  | 764 | } | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 765 | mHardwareStatus = AUDIO_HW_IDLE; | 
| Eric Laurent | 59bd0da | 2011-08-01 09:52:20 -0700 | [diff] [blame] | 766 | // disable AEC and NS if the device is a BT SCO headset supporting those pre processings | 
|  | 767 | AudioParameter param = AudioParameter(keyValuePairs); | 
|  | 768 | String8 value; | 
|  | 769 | if (param.get(String8(AUDIO_PARAMETER_KEY_BT_NREC), value) == NO_ERROR) { | 
|  | 770 | Mutex::Autolock _l(mLock); | 
| Eric Laurent | bee5337 | 2011-08-29 12:42:48 -0700 | [diff] [blame] | 771 | bool btNrecIsOff = (value == AUDIO_PARAMETER_VALUE_OFF); | 
|  | 772 | if (mBtNrecIsOff != btNrecIsOff) { | 
| Eric Laurent | 59bd0da | 2011-08-01 09:52:20 -0700 | [diff] [blame] | 773 | for (size_t i = 0; i < mRecordThreads.size(); i++) { | 
|  | 774 | sp<RecordThread> thread = mRecordThreads.valueAt(i); | 
|  | 775 | RecordThread::RecordTrack *track = thread->track(); | 
|  | 776 | if (track != NULL) { | 
|  | 777 | audio_devices_t device = (audio_devices_t)( | 
|  | 778 | thread->device() & AUDIO_DEVICE_IN_ALL); | 
| Eric Laurent | bee5337 | 2011-08-29 12:42:48 -0700 | [diff] [blame] | 779 | bool suspend = audio_is_bluetooth_sco_device(device) && btNrecIsOff; | 
| Eric Laurent | 59bd0da | 2011-08-01 09:52:20 -0700 | [diff] [blame] | 780 | thread->setEffectSuspended(FX_IID_AEC, | 
|  | 781 | suspend, | 
|  | 782 | track->sessionId()); | 
|  | 783 | thread->setEffectSuspended(FX_IID_NS, | 
|  | 784 | suspend, | 
|  | 785 | track->sessionId()); | 
|  | 786 | } | 
|  | 787 | } | 
| Eric Laurent | bee5337 | 2011-08-29 12:42:48 -0700 | [diff] [blame] | 788 | mBtNrecIsOff = btNrecIsOff; | 
| Eric Laurent | 59bd0da | 2011-08-01 09:52:20 -0700 | [diff] [blame] | 789 | } | 
|  | 790 | } | 
| Dima Zavin | 799a70e | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 791 | return final_result; | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 792 | } | 
|  | 793 |  | 
|  | 794 | // hold a strong ref on thread in case closeOutput() or closeInput() is called | 
|  | 795 | // and the thread is exited once the lock is released | 
|  | 796 | sp<ThreadBase> thread; | 
|  | 797 | { | 
|  | 798 | Mutex::Autolock _l(mLock); | 
|  | 799 | thread = checkPlaybackThread_l(ioHandle); | 
|  | 800 | if (thread == NULL) { | 
|  | 801 | thread = checkRecordThread_l(ioHandle); | 
| Glenn Kasten | 7fc9a6f | 2012-01-10 10:46:34 -0800 | [diff] [blame] | 802 | } else if (thread == primaryPlaybackThread_l()) { | 
| Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 803 | // indicate output device change to all input threads for pre processing | 
|  | 804 | AudioParameter param = AudioParameter(keyValuePairs); | 
|  | 805 | int value; | 
|  | 806 | if (param.getInt(String8(AudioParameter::keyRouting), value) == NO_ERROR) { | 
|  | 807 | for (size_t i = 0; i < mRecordThreads.size(); i++) { | 
|  | 808 | mRecordThreads.valueAt(i)->setParameters(keyValuePairs); | 
|  | 809 | } | 
|  | 810 | } | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 811 | } | 
|  | 812 | } | 
|  | 813 | if (thread != NULL) { | 
|  | 814 | result = thread->setParameters(keyValuePairs); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 815 | return result; | 
|  | 816 | } | 
|  | 817 | return BAD_VALUE; | 
|  | 818 | } | 
|  | 819 |  | 
|  | 820 | String8 AudioFlinger::getParameters(int ioHandle, const String8& keys) | 
|  | 821 | { | 
| Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 822 | //    ALOGV("getParameters() io %d, keys %s, tid %d, calling tid %d", | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 823 | //            ioHandle, keys.string(), gettid(), IPCThreadState::self()->getCallingPid()); | 
|  | 824 |  | 
|  | 825 | if (ioHandle == 0) { | 
| Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 826 | String8 out_s8; | 
|  | 827 |  | 
| Dima Zavin | 799a70e | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 828 | for (size_t i = 0; i < mAudioHwDevs.size(); i++) { | 
|  | 829 | audio_hw_device_t *dev = mAudioHwDevs[i]; | 
|  | 830 | char *s = dev->get_parameters(dev, keys.string()); | 
|  | 831 | out_s8 += String8(s); | 
|  | 832 | free(s); | 
|  | 833 | } | 
| Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 834 | return out_s8; | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 835 | } | 
|  | 836 |  | 
|  | 837 | Mutex::Autolock _l(mLock); | 
|  | 838 |  | 
|  | 839 | PlaybackThread *playbackThread = checkPlaybackThread_l(ioHandle); | 
|  | 840 | if (playbackThread != NULL) { | 
|  | 841 | return playbackThread->getParameters(keys); | 
|  | 842 | } | 
|  | 843 | RecordThread *recordThread = checkRecordThread_l(ioHandle); | 
|  | 844 | if (recordThread != NULL) { | 
|  | 845 | return recordThread->getParameters(keys); | 
|  | 846 | } | 
|  | 847 | return String8(""); | 
|  | 848 | } | 
|  | 849 |  | 
| Glenn Kasten | 58f3021 | 2012-01-12 12:27:51 -0800 | [diff] [blame] | 850 | size_t AudioFlinger::getInputBufferSize(uint32_t sampleRate, audio_format_t format, int channelCount) | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 851 | { | 
| Eric Laurent | a1884f9 | 2011-08-23 08:25:03 -0700 | [diff] [blame] | 852 | status_t ret = initCheck(); | 
|  | 853 | if (ret != NO_ERROR) { | 
|  | 854 | return 0; | 
|  | 855 | } | 
|  | 856 |  | 
| Dima Zavin | 799a70e | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 857 | return mPrimaryHardwareDev->get_input_buffer_size(mPrimaryHardwareDev, sampleRate, format, channelCount); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 858 | } | 
|  | 859 |  | 
|  | 860 | unsigned int AudioFlinger::getInputFramesLost(int ioHandle) | 
|  | 861 | { | 
|  | 862 | if (ioHandle == 0) { | 
|  | 863 | return 0; | 
|  | 864 | } | 
|  | 865 |  | 
|  | 866 | Mutex::Autolock _l(mLock); | 
|  | 867 |  | 
|  | 868 | RecordThread *recordThread = checkRecordThread_l(ioHandle); | 
|  | 869 | if (recordThread != NULL) { | 
|  | 870 | return recordThread->getInputFramesLost(); | 
|  | 871 | } | 
|  | 872 | return 0; | 
|  | 873 | } | 
|  | 874 |  | 
|  | 875 | status_t AudioFlinger::setVoiceVolume(float value) | 
|  | 876 | { | 
| Eric Laurent | a1884f9 | 2011-08-23 08:25:03 -0700 | [diff] [blame] | 877 | status_t ret = initCheck(); | 
|  | 878 | if (ret != NO_ERROR) { | 
|  | 879 | return ret; | 
|  | 880 | } | 
|  | 881 |  | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 882 | // check calling permissions | 
|  | 883 | if (!settingsAllowed()) { | 
|  | 884 | return PERMISSION_DENIED; | 
|  | 885 | } | 
|  | 886 |  | 
|  | 887 | AutoMutex lock(mHardwareLock); | 
|  | 888 | mHardwareStatus = AUDIO_SET_VOICE_VOLUME; | 
| Eric Laurent | a1884f9 | 2011-08-23 08:25:03 -0700 | [diff] [blame] | 889 | ret = mPrimaryHardwareDev->set_voice_volume(mPrimaryHardwareDev, value); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 890 | mHardwareStatus = AUDIO_HW_IDLE; | 
|  | 891 |  | 
|  | 892 | return ret; | 
|  | 893 | } | 
|  | 894 |  | 
|  | 895 | status_t AudioFlinger::getRenderPosition(uint32_t *halFrames, uint32_t *dspFrames, int output) | 
|  | 896 | { | 
|  | 897 | status_t status; | 
|  | 898 |  | 
|  | 899 | Mutex::Autolock _l(mLock); | 
|  | 900 |  | 
|  | 901 | PlaybackThread *playbackThread = checkPlaybackThread_l(output); | 
|  | 902 | if (playbackThread != NULL) { | 
|  | 903 | return playbackThread->getRenderPosition(halFrames, dspFrames); | 
|  | 904 | } | 
|  | 905 |  | 
|  | 906 | return BAD_VALUE; | 
|  | 907 | } | 
|  | 908 |  | 
|  | 909 | void AudioFlinger::registerClient(const sp<IAudioFlingerClient>& client) | 
|  | 910 | { | 
|  | 911 |  | 
|  | 912 | Mutex::Autolock _l(mLock); | 
|  | 913 |  | 
|  | 914 | int pid = IPCThreadState::self()->getCallingPid(); | 
|  | 915 | if (mNotificationClients.indexOfKey(pid) < 0) { | 
|  | 916 | sp<NotificationClient> notificationClient = new NotificationClient(this, | 
|  | 917 | client, | 
|  | 918 | pid); | 
| Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 919 | ALOGV("registerClient() client %p, pid %d", notificationClient.get(), pid); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 920 |  | 
|  | 921 | mNotificationClients.add(pid, notificationClient); | 
|  | 922 |  | 
|  | 923 | sp<IBinder> binder = client->asBinder(); | 
|  | 924 | binder->linkToDeath(notificationClient); | 
|  | 925 |  | 
|  | 926 | // the config change is always sent from playback or record threads to avoid deadlock | 
|  | 927 | // with AudioSystem::gLock | 
|  | 928 | for (size_t i = 0; i < mPlaybackThreads.size(); i++) { | 
|  | 929 | mPlaybackThreads.valueAt(i)->sendConfigEvent(AudioSystem::OUTPUT_OPENED); | 
|  | 930 | } | 
|  | 931 |  | 
|  | 932 | for (size_t i = 0; i < mRecordThreads.size(); i++) { | 
|  | 933 | mRecordThreads.valueAt(i)->sendConfigEvent(AudioSystem::INPUT_OPENED); | 
|  | 934 | } | 
|  | 935 | } | 
|  | 936 | } | 
|  | 937 |  | 
|  | 938 | void AudioFlinger::removeNotificationClient(pid_t pid) | 
|  | 939 | { | 
|  | 940 | Mutex::Autolock _l(mLock); | 
|  | 941 |  | 
|  | 942 | int index = mNotificationClients.indexOfKey(pid); | 
|  | 943 | if (index >= 0) { | 
|  | 944 | sp <NotificationClient> client = mNotificationClients.valueFor(pid); | 
| Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 945 | ALOGV("removeNotificationClient() %p, pid %d", client.get(), pid); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 946 | mNotificationClients.removeItem(pid); | 
|  | 947 | } | 
| Marco Nelissen | 3a34bef | 2011-08-02 13:33:41 -0700 | [diff] [blame] | 948 |  | 
| Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 949 | ALOGV("%d died, releasing its sessions", pid); | 
| Marco Nelissen | 3a34bef | 2011-08-02 13:33:41 -0700 | [diff] [blame] | 950 | int num = mAudioSessionRefs.size(); | 
|  | 951 | bool removed = false; | 
|  | 952 | for (int i = 0; i< num; i++) { | 
|  | 953 | AudioSessionRef *ref = mAudioSessionRefs.itemAt(i); | 
| Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 954 | ALOGV(" pid %d @ %d", ref->pid, i); | 
| Marco Nelissen | 3a34bef | 2011-08-02 13:33:41 -0700 | [diff] [blame] | 955 | if (ref->pid == pid) { | 
| Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 956 | ALOGV(" removing entry for pid %d session %d", pid, ref->sessionid); | 
| Marco Nelissen | 3a34bef | 2011-08-02 13:33:41 -0700 | [diff] [blame] | 957 | mAudioSessionRefs.removeAt(i); | 
|  | 958 | delete ref; | 
|  | 959 | removed = true; | 
|  | 960 | i--; | 
|  | 961 | num--; | 
|  | 962 | } | 
|  | 963 | } | 
|  | 964 | if (removed) { | 
|  | 965 | purgeStaleEffects_l(); | 
|  | 966 | } | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 967 | } | 
|  | 968 |  | 
|  | 969 | // audioConfigChanged_l() must be called with AudioFlinger::mLock held | 
|  | 970 | void AudioFlinger::audioConfigChanged_l(int event, int ioHandle, void *param2) | 
|  | 971 | { | 
|  | 972 | size_t size = mNotificationClients.size(); | 
|  | 973 | for (size_t i = 0; i < size; i++) { | 
|  | 974 | mNotificationClients.valueAt(i)->client()->ioConfigChanged(event, ioHandle, param2); | 
|  | 975 | } | 
|  | 976 | } | 
|  | 977 |  | 
|  | 978 | // removeClient_l() must be called with AudioFlinger::mLock held | 
|  | 979 | void AudioFlinger::removeClient_l(pid_t pid) | 
|  | 980 | { | 
| Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 981 | ALOGV("removeClient_l() pid %d, tid %d, calling tid %d", pid, gettid(), IPCThreadState::self()->getCallingPid()); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 982 | mClients.removeItem(pid); | 
|  | 983 | } | 
|  | 984 |  | 
|  | 985 |  | 
|  | 986 | // ---------------------------------------------------------------------------- | 
|  | 987 |  | 
| Glenn Kasten | 23bb8be | 2012-01-26 10:38:26 -0800 | [diff] [blame^] | 988 | AudioFlinger::ThreadBase::ThreadBase(const sp<AudioFlinger>& audioFlinger, int id, uint32_t device, | 
|  | 989 | type_t type) | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 990 | :   Thread(false), | 
| Glenn Kasten | 23bb8be | 2012-01-26 10:38:26 -0800 | [diff] [blame^] | 991 | mType(type), | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 992 | mAudioFlinger(audioFlinger), mSampleRate(0), mFrameCount(0), mChannelCount(0), | 
| Glenn Kasten | 58f3021 | 2012-01-12 12:27:51 -0800 | [diff] [blame] | 993 | mFrameSize(1), mFormat(AUDIO_FORMAT_INVALID), mStandby(false), mId(id), mExiting(false), | 
| Eric Laurent | feb0db6 | 2011-07-22 09:04:31 -0700 | [diff] [blame] | 994 | mDevice(device) | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 995 | { | 
| Eric Laurent | feb0db6 | 2011-07-22 09:04:31 -0700 | [diff] [blame] | 996 | mDeathRecipient = new PMDeathRecipient(this); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 997 | } | 
|  | 998 |  | 
|  | 999 | AudioFlinger::ThreadBase::~ThreadBase() | 
|  | 1000 | { | 
|  | 1001 | mParamCond.broadcast(); | 
| Eric Laurent | feb0db6 | 2011-07-22 09:04:31 -0700 | [diff] [blame] | 1002 | // do not lock the mutex in destructor | 
|  | 1003 | releaseWakeLock_l(); | 
| Eric Laurent | 9d18ec5 | 2011-09-27 12:07:15 -0700 | [diff] [blame] | 1004 | if (mPowerManager != 0) { | 
|  | 1005 | sp<IBinder> binder = mPowerManager->asBinder(); | 
|  | 1006 | binder->unlinkToDeath(mDeathRecipient); | 
|  | 1007 | } | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1008 | } | 
|  | 1009 |  | 
|  | 1010 | void AudioFlinger::ThreadBase::exit() | 
|  | 1011 | { | 
| Glenn Kasten | 362c4e6 | 2011-12-14 10:28:06 -0800 | [diff] [blame] | 1012 | // keep a strong ref on ourself so that we won't get | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1013 | // destroyed in the middle of requestExitAndWait() | 
|  | 1014 | sp <ThreadBase> strongMe = this; | 
|  | 1015 |  | 
| Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 1016 | ALOGV("ThreadBase::exit"); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1017 | { | 
| Glenn Kasten | a7d8d6f | 2012-01-05 15:41:56 -0800 | [diff] [blame] | 1018 | AutoMutex lock(mLock); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1019 | mExiting = true; | 
|  | 1020 | requestExit(); | 
|  | 1021 | mWaitWorkCV.signal(); | 
|  | 1022 | } | 
|  | 1023 | requestExitAndWait(); | 
|  | 1024 | } | 
|  | 1025 |  | 
|  | 1026 | uint32_t AudioFlinger::ThreadBase::sampleRate() const | 
|  | 1027 | { | 
|  | 1028 | return mSampleRate; | 
|  | 1029 | } | 
|  | 1030 |  | 
|  | 1031 | int AudioFlinger::ThreadBase::channelCount() const | 
|  | 1032 | { | 
|  | 1033 | return (int)mChannelCount; | 
|  | 1034 | } | 
|  | 1035 |  | 
| Glenn Kasten | 58f3021 | 2012-01-12 12:27:51 -0800 | [diff] [blame] | 1036 | audio_format_t AudioFlinger::ThreadBase::format() const | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1037 | { | 
|  | 1038 | return mFormat; | 
|  | 1039 | } | 
|  | 1040 |  | 
|  | 1041 | size_t AudioFlinger::ThreadBase::frameCount() const | 
|  | 1042 | { | 
|  | 1043 | return mFrameCount; | 
|  | 1044 | } | 
|  | 1045 |  | 
|  | 1046 | status_t AudioFlinger::ThreadBase::setParameters(const String8& keyValuePairs) | 
|  | 1047 | { | 
|  | 1048 | status_t status; | 
|  | 1049 |  | 
| Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 1050 | ALOGV("ThreadBase::setParameters() %s", keyValuePairs.string()); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1051 | Mutex::Autolock _l(mLock); | 
|  | 1052 |  | 
|  | 1053 | mNewParameters.add(keyValuePairs); | 
|  | 1054 | mWaitWorkCV.signal(); | 
|  | 1055 | // wait condition with timeout in case the thread loop has exited | 
|  | 1056 | // before the request could be processed | 
| Glenn Kasten | 7dede87 | 2011-12-13 11:04:14 -0800 | [diff] [blame] | 1057 | if (mParamCond.waitRelative(mLock, kSetParametersTimeoutNs) == NO_ERROR) { | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1058 | status = mParamStatus; | 
|  | 1059 | mWaitWorkCV.signal(); | 
|  | 1060 | } else { | 
|  | 1061 | status = TIMED_OUT; | 
|  | 1062 | } | 
|  | 1063 | return status; | 
|  | 1064 | } | 
|  | 1065 |  | 
|  | 1066 | void AudioFlinger::ThreadBase::sendConfigEvent(int event, int param) | 
|  | 1067 | { | 
|  | 1068 | Mutex::Autolock _l(mLock); | 
|  | 1069 | sendConfigEvent_l(event, param); | 
|  | 1070 | } | 
|  | 1071 |  | 
|  | 1072 | // sendConfigEvent_l() must be called with ThreadBase::mLock held | 
|  | 1073 | void AudioFlinger::ThreadBase::sendConfigEvent_l(int event, int param) | 
|  | 1074 | { | 
| Glenn Kasten | f3990f2 | 2011-12-13 11:50:00 -0800 | [diff] [blame] | 1075 | ConfigEvent configEvent; | 
|  | 1076 | configEvent.mEvent = event; | 
|  | 1077 | configEvent.mParam = param; | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1078 | mConfigEvents.add(configEvent); | 
| Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 1079 | ALOGV("sendConfigEvent() num events %d event %d, param %d", mConfigEvents.size(), event, param); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1080 | mWaitWorkCV.signal(); | 
|  | 1081 | } | 
|  | 1082 |  | 
|  | 1083 | void AudioFlinger::ThreadBase::processConfigEvents() | 
|  | 1084 | { | 
|  | 1085 | mLock.lock(); | 
|  | 1086 | while(!mConfigEvents.isEmpty()) { | 
| Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 1087 | ALOGV("processConfigEvents() remaining events %d", mConfigEvents.size()); | 
| Glenn Kasten | f3990f2 | 2011-12-13 11:50:00 -0800 | [diff] [blame] | 1088 | ConfigEvent configEvent = mConfigEvents[0]; | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1089 | mConfigEvents.removeAt(0); | 
|  | 1090 | // release mLock before locking AudioFlinger mLock: lock order is always | 
|  | 1091 | // AudioFlinger then ThreadBase to avoid cross deadlock | 
|  | 1092 | mLock.unlock(); | 
|  | 1093 | mAudioFlinger->mLock.lock(); | 
| Glenn Kasten | f3990f2 | 2011-12-13 11:50:00 -0800 | [diff] [blame] | 1094 | audioConfigChanged_l(configEvent.mEvent, configEvent.mParam); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1095 | mAudioFlinger->mLock.unlock(); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1096 | mLock.lock(); | 
|  | 1097 | } | 
|  | 1098 | mLock.unlock(); | 
|  | 1099 | } | 
|  | 1100 |  | 
|  | 1101 | status_t AudioFlinger::ThreadBase::dumpBase(int fd, const Vector<String16>& args) | 
|  | 1102 | { | 
|  | 1103 | const size_t SIZE = 256; | 
|  | 1104 | char buffer[SIZE]; | 
|  | 1105 | String8 result; | 
|  | 1106 |  | 
|  | 1107 | bool locked = tryLock(mLock); | 
|  | 1108 | if (!locked) { | 
|  | 1109 | snprintf(buffer, SIZE, "thread %p maybe dead locked\n", this); | 
|  | 1110 | write(fd, buffer, strlen(buffer)); | 
|  | 1111 | } | 
|  | 1112 |  | 
|  | 1113 | snprintf(buffer, SIZE, "standby: %d\n", mStandby); | 
|  | 1114 | result.append(buffer); | 
|  | 1115 | snprintf(buffer, SIZE, "Sample rate: %d\n", mSampleRate); | 
|  | 1116 | result.append(buffer); | 
|  | 1117 | snprintf(buffer, SIZE, "Frame count: %d\n", mFrameCount); | 
|  | 1118 | result.append(buffer); | 
|  | 1119 | snprintf(buffer, SIZE, "Channel Count: %d\n", mChannelCount); | 
|  | 1120 | result.append(buffer); | 
| Jean-Michel Trivi | 0d255b2 | 2011-05-24 15:53:33 -0700 | [diff] [blame] | 1121 | snprintf(buffer, SIZE, "Channel Mask: 0x%08x\n", mChannelMask); | 
|  | 1122 | result.append(buffer); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1123 | snprintf(buffer, SIZE, "Format: %d\n", mFormat); | 
|  | 1124 | result.append(buffer); | 
| Glenn Kasten | b998065 | 2012-01-11 09:48:27 -0800 | [diff] [blame] | 1125 | snprintf(buffer, SIZE, "Frame size: %u\n", mFrameSize); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1126 | result.append(buffer); | 
|  | 1127 |  | 
|  | 1128 | snprintf(buffer, SIZE, "\nPending setParameters commands: \n"); | 
|  | 1129 | result.append(buffer); | 
|  | 1130 | result.append(" Index Command"); | 
|  | 1131 | for (size_t i = 0; i < mNewParameters.size(); ++i) { | 
|  | 1132 | snprintf(buffer, SIZE, "\n %02d    ", i); | 
|  | 1133 | result.append(buffer); | 
|  | 1134 | result.append(mNewParameters[i]); | 
|  | 1135 | } | 
|  | 1136 |  | 
|  | 1137 | snprintf(buffer, SIZE, "\n\nPending config events: \n"); | 
|  | 1138 | result.append(buffer); | 
|  | 1139 | snprintf(buffer, SIZE, " Index event param\n"); | 
|  | 1140 | result.append(buffer); | 
|  | 1141 | for (size_t i = 0; i < mConfigEvents.size(); i++) { | 
| Glenn Kasten | f3990f2 | 2011-12-13 11:50:00 -0800 | [diff] [blame] | 1142 | snprintf(buffer, SIZE, " %02d    %02d    %d\n", i, mConfigEvents[i].mEvent, mConfigEvents[i].mParam); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1143 | result.append(buffer); | 
|  | 1144 | } | 
|  | 1145 | result.append("\n"); | 
|  | 1146 |  | 
|  | 1147 | write(fd, result.string(), result.size()); | 
|  | 1148 |  | 
|  | 1149 | if (locked) { | 
|  | 1150 | mLock.unlock(); | 
|  | 1151 | } | 
|  | 1152 | return NO_ERROR; | 
|  | 1153 | } | 
|  | 1154 |  | 
| Eric Laurent | 1d2bff0 | 2011-07-24 17:49:51 -0700 | [diff] [blame] | 1155 | status_t AudioFlinger::ThreadBase::dumpEffectChains(int fd, const Vector<String16>& args) | 
|  | 1156 | { | 
|  | 1157 | const size_t SIZE = 256; | 
|  | 1158 | char buffer[SIZE]; | 
|  | 1159 | String8 result; | 
|  | 1160 |  | 
|  | 1161 | snprintf(buffer, SIZE, "\n- %d Effect Chains:\n", mEffectChains.size()); | 
|  | 1162 | write(fd, buffer, strlen(buffer)); | 
|  | 1163 |  | 
|  | 1164 | for (size_t i = 0; i < mEffectChains.size(); ++i) { | 
|  | 1165 | sp<EffectChain> chain = mEffectChains[i]; | 
|  | 1166 | if (chain != 0) { | 
|  | 1167 | chain->dump(fd, args); | 
|  | 1168 | } | 
|  | 1169 | } | 
|  | 1170 | return NO_ERROR; | 
|  | 1171 | } | 
|  | 1172 |  | 
| Eric Laurent | feb0db6 | 2011-07-22 09:04:31 -0700 | [diff] [blame] | 1173 | void AudioFlinger::ThreadBase::acquireWakeLock() | 
|  | 1174 | { | 
|  | 1175 | Mutex::Autolock _l(mLock); | 
|  | 1176 | acquireWakeLock_l(); | 
|  | 1177 | } | 
|  | 1178 |  | 
|  | 1179 | void AudioFlinger::ThreadBase::acquireWakeLock_l() | 
|  | 1180 | { | 
|  | 1181 | if (mPowerManager == 0) { | 
|  | 1182 | // use checkService() to avoid blocking if power service is not up yet | 
|  | 1183 | sp<IBinder> binder = | 
|  | 1184 | defaultServiceManager()->checkService(String16("power")); | 
|  | 1185 | if (binder == 0) { | 
| Steve Block | 5ff1dd5 | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 1186 | ALOGW("Thread %s cannot connect to the power manager service", mName); | 
| Eric Laurent | feb0db6 | 2011-07-22 09:04:31 -0700 | [diff] [blame] | 1187 | } else { | 
|  | 1188 | mPowerManager = interface_cast<IPowerManager>(binder); | 
|  | 1189 | binder->linkToDeath(mDeathRecipient); | 
|  | 1190 | } | 
|  | 1191 | } | 
|  | 1192 | if (mPowerManager != 0) { | 
|  | 1193 | sp<IBinder> binder = new BBinder(); | 
|  | 1194 | status_t status = mPowerManager->acquireWakeLock(POWERMANAGER_PARTIAL_WAKE_LOCK, | 
|  | 1195 | binder, | 
|  | 1196 | String16(mName)); | 
|  | 1197 | if (status == NO_ERROR) { | 
|  | 1198 | mWakeLockToken = binder; | 
|  | 1199 | } | 
| Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 1200 | ALOGV("acquireWakeLock_l() %s status %d", mName, status); | 
| Eric Laurent | feb0db6 | 2011-07-22 09:04:31 -0700 | [diff] [blame] | 1201 | } | 
|  | 1202 | } | 
|  | 1203 |  | 
|  | 1204 | void AudioFlinger::ThreadBase::releaseWakeLock() | 
|  | 1205 | { | 
|  | 1206 | Mutex::Autolock _l(mLock); | 
| Eric Laurent | 6dbe883 | 2011-07-28 13:59:02 -0700 | [diff] [blame] | 1207 | releaseWakeLock_l(); | 
| Eric Laurent | feb0db6 | 2011-07-22 09:04:31 -0700 | [diff] [blame] | 1208 | } | 
|  | 1209 |  | 
|  | 1210 | void AudioFlinger::ThreadBase::releaseWakeLock_l() | 
|  | 1211 | { | 
|  | 1212 | if (mWakeLockToken != 0) { | 
| Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 1213 | ALOGV("releaseWakeLock_l() %s", mName); | 
| Eric Laurent | feb0db6 | 2011-07-22 09:04:31 -0700 | [diff] [blame] | 1214 | if (mPowerManager != 0) { | 
|  | 1215 | mPowerManager->releaseWakeLock(mWakeLockToken, 0); | 
|  | 1216 | } | 
|  | 1217 | mWakeLockToken.clear(); | 
|  | 1218 | } | 
|  | 1219 | } | 
|  | 1220 |  | 
|  | 1221 | void AudioFlinger::ThreadBase::clearPowerManager() | 
|  | 1222 | { | 
|  | 1223 | Mutex::Autolock _l(mLock); | 
|  | 1224 | releaseWakeLock_l(); | 
|  | 1225 | mPowerManager.clear(); | 
|  | 1226 | } | 
|  | 1227 |  | 
|  | 1228 | void AudioFlinger::ThreadBase::PMDeathRecipient::binderDied(const wp<IBinder>& who) | 
|  | 1229 | { | 
|  | 1230 | sp<ThreadBase> thread = mThread.promote(); | 
|  | 1231 | if (thread != 0) { | 
|  | 1232 | thread->clearPowerManager(); | 
|  | 1233 | } | 
| Steve Block | 5ff1dd5 | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 1234 | ALOGW("power manager service died !!!"); | 
| Eric Laurent | feb0db6 | 2011-07-22 09:04:31 -0700 | [diff] [blame] | 1235 | } | 
| Eric Laurent | 1d2bff0 | 2011-07-24 17:49:51 -0700 | [diff] [blame] | 1236 |  | 
| Eric Laurent | 59255e4 | 2011-07-27 19:49:51 -0700 | [diff] [blame] | 1237 | void AudioFlinger::ThreadBase::setEffectSuspended( | 
|  | 1238 | const effect_uuid_t *type, bool suspend, int sessionId) | 
|  | 1239 | { | 
|  | 1240 | Mutex::Autolock _l(mLock); | 
|  | 1241 | setEffectSuspended_l(type, suspend, sessionId); | 
|  | 1242 | } | 
|  | 1243 |  | 
|  | 1244 | void AudioFlinger::ThreadBase::setEffectSuspended_l( | 
|  | 1245 | const effect_uuid_t *type, bool suspend, int sessionId) | 
|  | 1246 | { | 
|  | 1247 | sp<EffectChain> chain; | 
|  | 1248 | chain = getEffectChain_l(sessionId); | 
|  | 1249 | if (chain != 0) { | 
|  | 1250 | if (type != NULL) { | 
|  | 1251 | chain->setEffectSuspended_l(type, suspend); | 
|  | 1252 | } else { | 
|  | 1253 | chain->setEffectSuspendedAll_l(suspend); | 
|  | 1254 | } | 
|  | 1255 | } | 
|  | 1256 |  | 
|  | 1257 | updateSuspendedSessions_l(type, suspend, sessionId); | 
|  | 1258 | } | 
|  | 1259 |  | 
|  | 1260 | void AudioFlinger::ThreadBase::checkSuspendOnAddEffectChain_l(const sp<EffectChain>& chain) | 
|  | 1261 | { | 
|  | 1262 | int index = mSuspendedSessions.indexOfKey(chain->sessionId()); | 
|  | 1263 | if (index < 0) { | 
|  | 1264 | return; | 
|  | 1265 | } | 
|  | 1266 |  | 
|  | 1267 | KeyedVector <int, sp<SuspendedSessionDesc> > sessionEffects = | 
|  | 1268 | mSuspendedSessions.editValueAt(index); | 
|  | 1269 |  | 
|  | 1270 | for (size_t i = 0; i < sessionEffects.size(); i++) { | 
|  | 1271 | sp <SuspendedSessionDesc> desc = sessionEffects.valueAt(i); | 
|  | 1272 | for (int j = 0; j < desc->mRefCount; j++) { | 
|  | 1273 | if (sessionEffects.keyAt(i) == EffectChain::kKeyForSuspendAll) { | 
|  | 1274 | chain->setEffectSuspendedAll_l(true); | 
|  | 1275 | } else { | 
| Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 1276 | ALOGV("checkSuspendOnAddEffectChain_l() suspending effects %08x", | 
| Eric Laurent | 59255e4 | 2011-07-27 19:49:51 -0700 | [diff] [blame] | 1277 | desc->mType.timeLow); | 
|  | 1278 | chain->setEffectSuspended_l(&desc->mType, true); | 
|  | 1279 | } | 
|  | 1280 | } | 
|  | 1281 | } | 
|  | 1282 | } | 
|  | 1283 |  | 
| Eric Laurent | 59255e4 | 2011-07-27 19:49:51 -0700 | [diff] [blame] | 1284 | void AudioFlinger::ThreadBase::updateSuspendedSessions_l(const effect_uuid_t *type, | 
|  | 1285 | bool suspend, | 
|  | 1286 | int sessionId) | 
|  | 1287 | { | 
|  | 1288 | int index = mSuspendedSessions.indexOfKey(sessionId); | 
|  | 1289 |  | 
|  | 1290 | KeyedVector <int, sp<SuspendedSessionDesc> > sessionEffects; | 
|  | 1291 |  | 
|  | 1292 | if (suspend) { | 
|  | 1293 | if (index >= 0) { | 
|  | 1294 | sessionEffects = mSuspendedSessions.editValueAt(index); | 
|  | 1295 | } else { | 
|  | 1296 | mSuspendedSessions.add(sessionId, sessionEffects); | 
|  | 1297 | } | 
|  | 1298 | } else { | 
|  | 1299 | if (index < 0) { | 
|  | 1300 | return; | 
|  | 1301 | } | 
|  | 1302 | sessionEffects = mSuspendedSessions.editValueAt(index); | 
|  | 1303 | } | 
|  | 1304 |  | 
|  | 1305 |  | 
|  | 1306 | int key = EffectChain::kKeyForSuspendAll; | 
|  | 1307 | if (type != NULL) { | 
|  | 1308 | key = type->timeLow; | 
|  | 1309 | } | 
|  | 1310 | index = sessionEffects.indexOfKey(key); | 
|  | 1311 |  | 
|  | 1312 | sp <SuspendedSessionDesc> desc; | 
|  | 1313 | if (suspend) { | 
|  | 1314 | if (index >= 0) { | 
|  | 1315 | desc = sessionEffects.valueAt(index); | 
|  | 1316 | } else { | 
|  | 1317 | desc = new SuspendedSessionDesc(); | 
|  | 1318 | if (type != NULL) { | 
|  | 1319 | memcpy(&desc->mType, type, sizeof(effect_uuid_t)); | 
|  | 1320 | } | 
|  | 1321 | sessionEffects.add(key, desc); | 
| Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 1322 | ALOGV("updateSuspendedSessions_l() suspend adding effect %08x", key); | 
| Eric Laurent | 59255e4 | 2011-07-27 19:49:51 -0700 | [diff] [blame] | 1323 | } | 
|  | 1324 | desc->mRefCount++; | 
|  | 1325 | } else { | 
|  | 1326 | if (index < 0) { | 
|  | 1327 | return; | 
|  | 1328 | } | 
|  | 1329 | desc = sessionEffects.valueAt(index); | 
|  | 1330 | if (--desc->mRefCount == 0) { | 
| Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 1331 | ALOGV("updateSuspendedSessions_l() restore removing effect %08x", key); | 
| Eric Laurent | 59255e4 | 2011-07-27 19:49:51 -0700 | [diff] [blame] | 1332 | sessionEffects.removeItemsAt(index); | 
|  | 1333 | if (sessionEffects.isEmpty()) { | 
| Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 1334 | ALOGV("updateSuspendedSessions_l() restore removing session %d", | 
| Eric Laurent | 59255e4 | 2011-07-27 19:49:51 -0700 | [diff] [blame] | 1335 | sessionId); | 
|  | 1336 | mSuspendedSessions.removeItem(sessionId); | 
|  | 1337 | } | 
|  | 1338 | } | 
|  | 1339 | } | 
|  | 1340 | if (!sessionEffects.isEmpty()) { | 
|  | 1341 | mSuspendedSessions.replaceValueFor(sessionId, sessionEffects); | 
|  | 1342 | } | 
|  | 1343 | } | 
|  | 1344 |  | 
|  | 1345 | void AudioFlinger::ThreadBase::checkSuspendOnEffectEnabled(const sp<EffectModule>& effect, | 
|  | 1346 | bool enabled, | 
|  | 1347 | int sessionId) | 
|  | 1348 | { | 
|  | 1349 | Mutex::Autolock _l(mLock); | 
| Eric Laurent | a85a74a | 2011-10-19 11:44:54 -0700 | [diff] [blame] | 1350 | checkSuspendOnEffectEnabled_l(effect, enabled, sessionId); | 
|  | 1351 | } | 
| Eric Laurent | 59255e4 | 2011-07-27 19:49:51 -0700 | [diff] [blame] | 1352 |  | 
| Eric Laurent | a85a74a | 2011-10-19 11:44:54 -0700 | [diff] [blame] | 1353 | void AudioFlinger::ThreadBase::checkSuspendOnEffectEnabled_l(const sp<EffectModule>& effect, | 
|  | 1354 | bool enabled, | 
|  | 1355 | int sessionId) | 
|  | 1356 | { | 
| Eric Laurent | db7c079 | 2011-08-10 10:37:50 -0700 | [diff] [blame] | 1357 | if (mType != RECORD) { | 
|  | 1358 | // suspend all effects in AUDIO_SESSION_OUTPUT_MIX when enabling any effect on | 
|  | 1359 | // another session. This gives the priority to well behaved effect control panels | 
|  | 1360 | // and applications not using global effects. | 
|  | 1361 | if (sessionId != AUDIO_SESSION_OUTPUT_MIX) { | 
|  | 1362 | setEffectSuspended_l(NULL, enabled, AUDIO_SESSION_OUTPUT_MIX); | 
|  | 1363 | } | 
|  | 1364 | } | 
| Eric Laurent | 59255e4 | 2011-07-27 19:49:51 -0700 | [diff] [blame] | 1365 |  | 
|  | 1366 | sp<EffectChain> chain = getEffectChain_l(sessionId); | 
|  | 1367 | if (chain != 0) { | 
|  | 1368 | chain->checkSuspendOnEffectEnabled(effect, enabled); | 
|  | 1369 | } | 
|  | 1370 | } | 
|  | 1371 |  | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1372 | // ---------------------------------------------------------------------------- | 
|  | 1373 |  | 
| Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 1374 | AudioFlinger::PlaybackThread::PlaybackThread(const sp<AudioFlinger>& audioFlinger, | 
|  | 1375 | AudioStreamOut* output, | 
|  | 1376 | int id, | 
| Glenn Kasten | 23bb8be | 2012-01-26 10:38:26 -0800 | [diff] [blame^] | 1377 | uint32_t device, | 
|  | 1378 | type_t type) | 
|  | 1379 | :   ThreadBase(audioFlinger, id, device, type), | 
| Glenn Kasten | e0feee3 | 2011-12-13 11:53:26 -0800 | [diff] [blame] | 1380 | mMixBuffer(NULL), mSuspended(0), mBytesWritten(0), mOutput(output), | 
| Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 1381 | mLastWriteTime(0), mNumWrites(0), mNumDelayedWrites(0), mInWrite(false) | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1382 | { | 
| Eric Laurent | feb0db6 | 2011-07-22 09:04:31 -0700 | [diff] [blame] | 1383 | snprintf(mName, kNameLength, "AudioOut_%d", id); | 
|  | 1384 |  | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1385 | readOutputParameters(); | 
|  | 1386 |  | 
| Glenn Kasten | 9806710 | 2011-12-13 11:47:54 -0800 | [diff] [blame] | 1387 | // Assumes constructor is called by AudioFlinger with it's mLock held, | 
|  | 1388 | // but it would be safer to explicitly pass these as parameters | 
|  | 1389 | mMasterVolume = mAudioFlinger->masterVolume_l(); | 
|  | 1390 | mMasterMute = mAudioFlinger->masterMute_l(); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1391 |  | 
| Glenn Kasten | 263709e | 2012-01-06 08:40:01 -0800 | [diff] [blame] | 1392 | // mStreamTypes[AUDIO_STREAM_CNT] is initialized by stream_type_t default constructor | 
| Glenn Kasten | fff6d71 | 2012-01-12 16:38:12 -0800 | [diff] [blame] | 1393 | // There is no AUDIO_STREAM_MIN, and ++ operator does not compile | 
|  | 1394 | for (audio_stream_type_t stream = (audio_stream_type_t) 0; stream < AUDIO_STREAM_CNT; | 
|  | 1395 | stream = (audio_stream_type_t) (stream + 1)) { | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1396 | mStreamTypes[stream].volume = mAudioFlinger->streamVolumeInternal(stream); | 
|  | 1397 | mStreamTypes[stream].mute = mAudioFlinger->streamMute(stream); | 
| Glenn Kasten | 263709e | 2012-01-06 08:40:01 -0800 | [diff] [blame] | 1398 | // initialized by stream_type_t default constructor | 
|  | 1399 | // mStreamTypes[stream].valid = true; | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1400 | } | 
|  | 1401 | } | 
|  | 1402 |  | 
|  | 1403 | AudioFlinger::PlaybackThread::~PlaybackThread() | 
|  | 1404 | { | 
|  | 1405 | delete [] mMixBuffer; | 
|  | 1406 | } | 
|  | 1407 |  | 
|  | 1408 | status_t AudioFlinger::PlaybackThread::dump(int fd, const Vector<String16>& args) | 
|  | 1409 | { | 
|  | 1410 | dumpInternals(fd, args); | 
|  | 1411 | dumpTracks(fd, args); | 
|  | 1412 | dumpEffectChains(fd, args); | 
|  | 1413 | return NO_ERROR; | 
|  | 1414 | } | 
|  | 1415 |  | 
|  | 1416 | status_t AudioFlinger::PlaybackThread::dumpTracks(int fd, const Vector<String16>& args) | 
|  | 1417 | { | 
|  | 1418 | const size_t SIZE = 256; | 
|  | 1419 | char buffer[SIZE]; | 
|  | 1420 | String8 result; | 
|  | 1421 |  | 
|  | 1422 | snprintf(buffer, SIZE, "Output thread %p tracks\n", this); | 
|  | 1423 | result.append(buffer); | 
| Jean-Michel Trivi | 0d255b2 | 2011-05-24 15:53:33 -0700 | [diff] [blame] | 1424 | result.append("   Name  Clien Typ Fmt Chn mask   Session Buf  S M F SRate LeftV RighV  Serv       User       Main buf   Aux Buf\n"); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1425 | for (size_t i = 0; i < mTracks.size(); ++i) { | 
|  | 1426 | sp<Track> track = mTracks[i]; | 
|  | 1427 | if (track != 0) { | 
|  | 1428 | track->dump(buffer, SIZE); | 
|  | 1429 | result.append(buffer); | 
|  | 1430 | } | 
|  | 1431 | } | 
|  | 1432 |  | 
|  | 1433 | snprintf(buffer, SIZE, "Output thread %p active tracks\n", this); | 
|  | 1434 | result.append(buffer); | 
| Jean-Michel Trivi | 0d255b2 | 2011-05-24 15:53:33 -0700 | [diff] [blame] | 1435 | result.append("   Name  Clien Typ Fmt Chn mask   Session Buf  S M F SRate LeftV RighV  Serv       User       Main buf   Aux Buf\n"); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1436 | for (size_t i = 0; i < mActiveTracks.size(); ++i) { | 
|  | 1437 | wp<Track> wTrack = mActiveTracks[i]; | 
|  | 1438 | if (wTrack != 0) { | 
|  | 1439 | sp<Track> track = wTrack.promote(); | 
|  | 1440 | if (track != 0) { | 
|  | 1441 | track->dump(buffer, SIZE); | 
|  | 1442 | result.append(buffer); | 
|  | 1443 | } | 
|  | 1444 | } | 
|  | 1445 | } | 
|  | 1446 | write(fd, result.string(), result.size()); | 
|  | 1447 | return NO_ERROR; | 
|  | 1448 | } | 
|  | 1449 |  | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1450 | status_t AudioFlinger::PlaybackThread::dumpInternals(int fd, const Vector<String16>& args) | 
|  | 1451 | { | 
|  | 1452 | const size_t SIZE = 256; | 
|  | 1453 | char buffer[SIZE]; | 
|  | 1454 | String8 result; | 
|  | 1455 |  | 
|  | 1456 | snprintf(buffer, SIZE, "\nOutput thread %p internals\n", this); | 
|  | 1457 | result.append(buffer); | 
|  | 1458 | snprintf(buffer, SIZE, "last write occurred (msecs): %llu\n", ns2ms(systemTime() - mLastWriteTime)); | 
|  | 1459 | result.append(buffer); | 
|  | 1460 | snprintf(buffer, SIZE, "total writes: %d\n", mNumWrites); | 
|  | 1461 | result.append(buffer); | 
|  | 1462 | snprintf(buffer, SIZE, "delayed writes: %d\n", mNumDelayedWrites); | 
|  | 1463 | result.append(buffer); | 
|  | 1464 | snprintf(buffer, SIZE, "blocked in write: %d\n", mInWrite); | 
|  | 1465 | result.append(buffer); | 
|  | 1466 | snprintf(buffer, SIZE, "suspend count: %d\n", mSuspended); | 
|  | 1467 | result.append(buffer); | 
|  | 1468 | snprintf(buffer, SIZE, "mix buffer : %p\n", mMixBuffer); | 
|  | 1469 | result.append(buffer); | 
|  | 1470 | write(fd, result.string(), result.size()); | 
|  | 1471 |  | 
|  | 1472 | dumpBase(fd, args); | 
|  | 1473 |  | 
|  | 1474 | return NO_ERROR; | 
|  | 1475 | } | 
|  | 1476 |  | 
|  | 1477 | // Thread virtuals | 
|  | 1478 | status_t AudioFlinger::PlaybackThread::readyToRun() | 
|  | 1479 | { | 
| Eric Laurent | b8ba0a9 | 2011-08-07 16:32:26 -0700 | [diff] [blame] | 1480 | status_t status = initCheck(); | 
|  | 1481 | if (status == NO_ERROR) { | 
| Steve Block | df64d15 | 2012-01-04 20:05:49 +0000 | [diff] [blame] | 1482 | ALOGI("AudioFlinger's thread %p ready to run", this); | 
| Eric Laurent | b8ba0a9 | 2011-08-07 16:32:26 -0700 | [diff] [blame] | 1483 | } else { | 
| Steve Block | 29357bc | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 1484 | ALOGE("No working audio driver found."); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1485 | } | 
| Eric Laurent | b8ba0a9 | 2011-08-07 16:32:26 -0700 | [diff] [blame] | 1486 | return status; | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1487 | } | 
|  | 1488 |  | 
|  | 1489 | void AudioFlinger::PlaybackThread::onFirstRef() | 
|  | 1490 | { | 
| Eric Laurent | feb0db6 | 2011-07-22 09:04:31 -0700 | [diff] [blame] | 1491 | run(mName, ANDROID_PRIORITY_URGENT_AUDIO); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1492 | } | 
|  | 1493 |  | 
|  | 1494 | // PlaybackThread::createTrack_l() must be called with AudioFlinger::mLock held | 
|  | 1495 | sp<AudioFlinger::PlaybackThread::Track>  AudioFlinger::PlaybackThread::createTrack_l( | 
|  | 1496 | const sp<AudioFlinger::Client>& client, | 
| Glenn Kasten | fff6d71 | 2012-01-12 16:38:12 -0800 | [diff] [blame] | 1497 | audio_stream_type_t streamType, | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1498 | uint32_t sampleRate, | 
| Glenn Kasten | 58f3021 | 2012-01-12 12:27:51 -0800 | [diff] [blame] | 1499 | audio_format_t format, | 
| Jean-Michel Trivi | 0d255b2 | 2011-05-24 15:53:33 -0700 | [diff] [blame] | 1500 | uint32_t channelMask, | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1501 | int frameCount, | 
|  | 1502 | const sp<IMemory>& sharedBuffer, | 
|  | 1503 | int sessionId, | 
|  | 1504 | status_t *status) | 
|  | 1505 | { | 
|  | 1506 | sp<Track> track; | 
|  | 1507 | status_t lStatus; | 
|  | 1508 |  | 
|  | 1509 | if (mType == DIRECT) { | 
| Jean-Michel Trivi | 0d255b2 | 2011-05-24 15:53:33 -0700 | [diff] [blame] | 1510 | if ((format & AUDIO_FORMAT_MAIN_MASK) == AUDIO_FORMAT_PCM) { | 
|  | 1511 | if (sampleRate != mSampleRate || format != mFormat || channelMask != mChannelMask) { | 
| Steve Block | 29357bc | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 1512 | ALOGE("createTrack_l() Bad parameter: sampleRate %d format %d, channelMask 0x%08x \"" | 
| Jean-Michel Trivi | 0d255b2 | 2011-05-24 15:53:33 -0700 | [diff] [blame] | 1513 | "for output %p with format %d", | 
|  | 1514 | sampleRate, format, channelMask, mOutput, mFormat); | 
|  | 1515 | lStatus = BAD_VALUE; | 
|  | 1516 | goto Exit; | 
|  | 1517 | } | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1518 | } | 
|  | 1519 | } else { | 
|  | 1520 | // Resampler implementation limits input sampling rate to 2 x output sampling rate. | 
|  | 1521 | if (sampleRate > mSampleRate*2) { | 
| Steve Block | 29357bc | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 1522 | ALOGE("Sample rate out of range: %d mSampleRate %d", sampleRate, mSampleRate); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1523 | lStatus = BAD_VALUE; | 
|  | 1524 | goto Exit; | 
|  | 1525 | } | 
|  | 1526 | } | 
|  | 1527 |  | 
| Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 1528 | lStatus = initCheck(); | 
|  | 1529 | if (lStatus != NO_ERROR) { | 
| Steve Block | 29357bc | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 1530 | ALOGE("Audio driver not initialized."); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1531 | goto Exit; | 
|  | 1532 | } | 
|  | 1533 |  | 
|  | 1534 | { // scope for mLock | 
|  | 1535 | Mutex::Autolock _l(mLock); | 
| Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 1536 |  | 
|  | 1537 | // all tracks in same audio session must share the same routing strategy otherwise | 
|  | 1538 | // conflicts will happen when tracks are moved from one output to another by audio policy | 
|  | 1539 | // manager | 
|  | 1540 | uint32_t strategy = | 
| Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 1541 | AudioSystem::getStrategyForStream((audio_stream_type_t)streamType); | 
| Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 1542 | for (size_t i = 0; i < mTracks.size(); ++i) { | 
|  | 1543 | sp<Track> t = mTracks[i]; | 
|  | 1544 | if (t != 0) { | 
| Glenn Kasten | d879601 | 2011-10-28 10:31:42 -0700 | [diff] [blame] | 1545 | uint32_t actual = AudioSystem::getStrategyForStream((audio_stream_type_t)t->type()); | 
|  | 1546 | if (sessionId == t->sessionId() && strategy != actual) { | 
| Steve Block | 29357bc | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 1547 | ALOGE("createTrack_l() mismatched strategy; expected %u but found %u", | 
| Glenn Kasten | d879601 | 2011-10-28 10:31:42 -0700 | [diff] [blame] | 1548 | strategy, actual); | 
| Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 1549 | lStatus = BAD_VALUE; | 
|  | 1550 | goto Exit; | 
|  | 1551 | } | 
|  | 1552 | } | 
|  | 1553 | } | 
|  | 1554 |  | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1555 | track = new Track(this, client, streamType, sampleRate, format, | 
| Jean-Michel Trivi | 0d255b2 | 2011-05-24 15:53:33 -0700 | [diff] [blame] | 1556 | channelMask, frameCount, sharedBuffer, sessionId); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1557 | if (track->getCblk() == NULL || track->name() < 0) { | 
|  | 1558 | lStatus = NO_MEMORY; | 
|  | 1559 | goto Exit; | 
|  | 1560 | } | 
|  | 1561 | mTracks.add(track); | 
|  | 1562 |  | 
|  | 1563 | sp<EffectChain> chain = getEffectChain_l(sessionId); | 
|  | 1564 | if (chain != 0) { | 
| Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 1565 | ALOGV("createTrack_l() setting main buffer %p", chain->inBuffer()); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1566 | track->setMainBuffer(chain->inBuffer()); | 
| Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 1567 | chain->setStrategy(AudioSystem::getStrategyForStream((audio_stream_type_t)track->type())); | 
| Eric Laurent | b469b94 | 2011-05-09 12:09:06 -0700 | [diff] [blame] | 1568 | chain->incTrackCnt(); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1569 | } | 
| Eric Laurent | 9f6530f | 2011-08-30 10:18:54 -0700 | [diff] [blame] | 1570 |  | 
|  | 1571 | // invalidate track immediately if the stream type was moved to another thread since | 
|  | 1572 | // createTrack() was called by the client process. | 
|  | 1573 | if (!mStreamTypes[streamType].valid) { | 
| Steve Block | 5ff1dd5 | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 1574 | ALOGW("createTrack_l() on thread %p: invalidating track on stream %d", | 
| Eric Laurent | 9f6530f | 2011-08-30 10:18:54 -0700 | [diff] [blame] | 1575 | this, streamType); | 
|  | 1576 | android_atomic_or(CBLK_INVALID_ON, &track->mCblk->flags); | 
|  | 1577 | } | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1578 | } | 
|  | 1579 | lStatus = NO_ERROR; | 
|  | 1580 |  | 
|  | 1581 | Exit: | 
|  | 1582 | if(status) { | 
|  | 1583 | *status = lStatus; | 
|  | 1584 | } | 
|  | 1585 | return track; | 
|  | 1586 | } | 
|  | 1587 |  | 
|  | 1588 | uint32_t AudioFlinger::PlaybackThread::latency() const | 
|  | 1589 | { | 
| Eric Laurent | b8ba0a9 | 2011-08-07 16:32:26 -0700 | [diff] [blame] | 1590 | Mutex::Autolock _l(mLock); | 
|  | 1591 | if (initCheck() == NO_ERROR) { | 
| Dima Zavin | 799a70e | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 1592 | return mOutput->stream->get_latency(mOutput->stream); | 
| Eric Laurent | b8ba0a9 | 2011-08-07 16:32:26 -0700 | [diff] [blame] | 1593 | } else { | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1594 | return 0; | 
|  | 1595 | } | 
|  | 1596 | } | 
|  | 1597 |  | 
|  | 1598 | status_t AudioFlinger::PlaybackThread::setMasterVolume(float value) | 
|  | 1599 | { | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1600 | mMasterVolume = value; | 
|  | 1601 | return NO_ERROR; | 
|  | 1602 | } | 
|  | 1603 |  | 
|  | 1604 | status_t AudioFlinger::PlaybackThread::setMasterMute(bool muted) | 
|  | 1605 | { | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1606 | mMasterMute = muted; | 
|  | 1607 | return NO_ERROR; | 
|  | 1608 | } | 
|  | 1609 |  | 
|  | 1610 | float AudioFlinger::PlaybackThread::masterVolume() const | 
|  | 1611 | { | 
|  | 1612 | return mMasterVolume; | 
|  | 1613 | } | 
|  | 1614 |  | 
|  | 1615 | bool AudioFlinger::PlaybackThread::masterMute() const | 
|  | 1616 | { | 
|  | 1617 | return mMasterMute; | 
|  | 1618 | } | 
|  | 1619 |  | 
| Glenn Kasten | fff6d71 | 2012-01-12 16:38:12 -0800 | [diff] [blame] | 1620 | status_t AudioFlinger::PlaybackThread::setStreamVolume(audio_stream_type_t stream, float value) | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1621 | { | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1622 | mStreamTypes[stream].volume = value; | 
|  | 1623 | return NO_ERROR; | 
|  | 1624 | } | 
|  | 1625 |  | 
| Glenn Kasten | fff6d71 | 2012-01-12 16:38:12 -0800 | [diff] [blame] | 1626 | status_t AudioFlinger::PlaybackThread::setStreamMute(audio_stream_type_t stream, bool muted) | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1627 | { | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1628 | mStreamTypes[stream].mute = muted; | 
|  | 1629 | return NO_ERROR; | 
|  | 1630 | } | 
|  | 1631 |  | 
| Glenn Kasten | fff6d71 | 2012-01-12 16:38:12 -0800 | [diff] [blame] | 1632 | float AudioFlinger::PlaybackThread::streamVolume(audio_stream_type_t stream) const | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1633 | { | 
|  | 1634 | return mStreamTypes[stream].volume; | 
|  | 1635 | } | 
|  | 1636 |  | 
| Glenn Kasten | fff6d71 | 2012-01-12 16:38:12 -0800 | [diff] [blame] | 1637 | bool AudioFlinger::PlaybackThread::streamMute(audio_stream_type_t stream) const | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1638 | { | 
|  | 1639 | return mStreamTypes[stream].mute; | 
|  | 1640 | } | 
|  | 1641 |  | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1642 | // addTrack_l() must be called with ThreadBase::mLock held | 
|  | 1643 | status_t AudioFlinger::PlaybackThread::addTrack_l(const sp<Track>& track) | 
|  | 1644 | { | 
|  | 1645 | status_t status = ALREADY_EXISTS; | 
|  | 1646 |  | 
|  | 1647 | // set retry count for buffer fill | 
|  | 1648 | track->mRetryCount = kMaxTrackStartupRetries; | 
|  | 1649 | if (mActiveTracks.indexOf(track) < 0) { | 
|  | 1650 | // the track is newly added, make sure it fills up all its | 
|  | 1651 | // buffers before playing. This is to ensure the client will | 
|  | 1652 | // effectively get the latency it requested. | 
|  | 1653 | track->mFillingUpStatus = Track::FS_FILLING; | 
|  | 1654 | track->mResetDone = false; | 
|  | 1655 | mActiveTracks.add(track); | 
|  | 1656 | if (track->mainBuffer() != mMixBuffer) { | 
|  | 1657 | sp<EffectChain> chain = getEffectChain_l(track->sessionId()); | 
|  | 1658 | if (chain != 0) { | 
| Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 1659 | ALOGV("addTrack_l() starting track on chain %p for session %d", chain.get(), track->sessionId()); | 
| Eric Laurent | b469b94 | 2011-05-09 12:09:06 -0700 | [diff] [blame] | 1660 | chain->incActiveTrackCnt(); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1661 | } | 
|  | 1662 | } | 
|  | 1663 |  | 
|  | 1664 | status = NO_ERROR; | 
|  | 1665 | } | 
|  | 1666 |  | 
| Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 1667 | ALOGV("mWaitWorkCV.broadcast"); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1668 | mWaitWorkCV.broadcast(); | 
|  | 1669 |  | 
|  | 1670 | return status; | 
|  | 1671 | } | 
|  | 1672 |  | 
|  | 1673 | // destroyTrack_l() must be called with ThreadBase::mLock held | 
|  | 1674 | void AudioFlinger::PlaybackThread::destroyTrack_l(const sp<Track>& track) | 
|  | 1675 | { | 
|  | 1676 | track->mState = TrackBase::TERMINATED; | 
|  | 1677 | if (mActiveTracks.indexOf(track) < 0) { | 
| Eric Laurent | b469b94 | 2011-05-09 12:09:06 -0700 | [diff] [blame] | 1678 | removeTrack_l(track); | 
|  | 1679 | } | 
|  | 1680 | } | 
|  | 1681 |  | 
|  | 1682 | void AudioFlinger::PlaybackThread::removeTrack_l(const sp<Track>& track) | 
|  | 1683 | { | 
|  | 1684 | mTracks.remove(track); | 
|  | 1685 | deleteTrackName_l(track->name()); | 
|  | 1686 | sp<EffectChain> chain = getEffectChain_l(track->sessionId()); | 
|  | 1687 | if (chain != 0) { | 
|  | 1688 | chain->decTrackCnt(); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1689 | } | 
|  | 1690 | } | 
|  | 1691 |  | 
|  | 1692 | String8 AudioFlinger::PlaybackThread::getParameters(const String8& keys) | 
|  | 1693 | { | 
| Eric Laurent | b8ba0a9 | 2011-08-07 16:32:26 -0700 | [diff] [blame] | 1694 | String8 out_s8 = String8(""); | 
| Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 1695 | char *s; | 
|  | 1696 |  | 
| Eric Laurent | b8ba0a9 | 2011-08-07 16:32:26 -0700 | [diff] [blame] | 1697 | Mutex::Autolock _l(mLock); | 
|  | 1698 | if (initCheck() != NO_ERROR) { | 
|  | 1699 | return out_s8; | 
|  | 1700 | } | 
|  | 1701 |  | 
| Dima Zavin | 799a70e | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 1702 | s = mOutput->stream->common.get_parameters(&mOutput->stream->common, keys.string()); | 
| Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 1703 | out_s8 = String8(s); | 
|  | 1704 | free(s); | 
|  | 1705 | return out_s8; | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1706 | } | 
|  | 1707 |  | 
| Eric Laurent | b8ba0a9 | 2011-08-07 16:32:26 -0700 | [diff] [blame] | 1708 | // audioConfigChanged_l() must be called with AudioFlinger::mLock held | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1709 | void AudioFlinger::PlaybackThread::audioConfigChanged_l(int event, int param) { | 
|  | 1710 | AudioSystem::OutputDescriptor desc; | 
|  | 1711 | void *param2 = 0; | 
|  | 1712 |  | 
| Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 1713 | ALOGV("PlaybackThread::audioConfigChanged_l, thread %p, event %d, param %d", this, event, param); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1714 |  | 
|  | 1715 | switch (event) { | 
|  | 1716 | case AudioSystem::OUTPUT_OPENED: | 
|  | 1717 | case AudioSystem::OUTPUT_CONFIG_CHANGED: | 
| Jean-Michel Trivi | 0d255b2 | 2011-05-24 15:53:33 -0700 | [diff] [blame] | 1718 | desc.channels = mChannelMask; | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1719 | desc.samplingRate = mSampleRate; | 
|  | 1720 | desc.format = mFormat; | 
|  | 1721 | desc.frameCount = mFrameCount; | 
|  | 1722 | desc.latency = latency(); | 
|  | 1723 | param2 = &desc; | 
|  | 1724 | break; | 
|  | 1725 |  | 
|  | 1726 | case AudioSystem::STREAM_CONFIG_CHANGED: | 
|  | 1727 | param2 = ¶m; | 
|  | 1728 | case AudioSystem::OUTPUT_CLOSED: | 
|  | 1729 | default: | 
|  | 1730 | break; | 
|  | 1731 | } | 
|  | 1732 | mAudioFlinger->audioConfigChanged_l(event, mId, param2); | 
|  | 1733 | } | 
|  | 1734 |  | 
|  | 1735 | void AudioFlinger::PlaybackThread::readOutputParameters() | 
|  | 1736 | { | 
| Dima Zavin | 799a70e | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 1737 | mSampleRate = mOutput->stream->common.get_sample_rate(&mOutput->stream->common); | 
| Jean-Michel Trivi | 0d255b2 | 2011-05-24 15:53:33 -0700 | [diff] [blame] | 1738 | mChannelMask = mOutput->stream->common.get_channels(&mOutput->stream->common); | 
|  | 1739 | mChannelCount = (uint16_t)popcount(mChannelMask); | 
| Dima Zavin | 799a70e | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 1740 | mFormat = mOutput->stream->common.get_format(&mOutput->stream->common); | 
| Glenn Kasten | b998065 | 2012-01-11 09:48:27 -0800 | [diff] [blame] | 1741 | mFrameSize = audio_stream_frame_size(&mOutput->stream->common); | 
| Dima Zavin | 799a70e | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 1742 | mFrameCount = mOutput->stream->common.get_buffer_size(&mOutput->stream->common) / mFrameSize; | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1743 |  | 
|  | 1744 | // FIXME - Current mixer implementation only supports stereo output: Always | 
|  | 1745 | // Allocate a stereo buffer even if HW output is mono. | 
|  | 1746 | if (mMixBuffer != NULL) delete[] mMixBuffer; | 
|  | 1747 | mMixBuffer = new int16_t[mFrameCount * 2]; | 
|  | 1748 | memset(mMixBuffer, 0, mFrameCount * 2 * sizeof(int16_t)); | 
|  | 1749 |  | 
| Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 1750 | // force reconfiguration of effect chains and engines to take new buffer size and audio | 
|  | 1751 | // parameters into account | 
|  | 1752 | // Note that mLock is not held when readOutputParameters() is called from the constructor | 
|  | 1753 | // but in this case nothing is done below as no audio sessions have effect yet so it doesn't | 
|  | 1754 | // matter. | 
|  | 1755 | // create a copy of mEffectChains as calling moveEffectChain_l() can reorder some effect chains | 
|  | 1756 | Vector< sp<EffectChain> > effectChains = mEffectChains; | 
|  | 1757 | for (size_t i = 0; i < effectChains.size(); i ++) { | 
| Eric Laurent | 39e94f8 | 2010-07-28 01:32:47 -0700 | [diff] [blame] | 1758 | mAudioFlinger->moveEffectChain_l(effectChains[i]->sessionId(), this, this, false); | 
| Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 1759 | } | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1760 | } | 
|  | 1761 |  | 
|  | 1762 | status_t AudioFlinger::PlaybackThread::getRenderPosition(uint32_t *halFrames, uint32_t *dspFrames) | 
|  | 1763 | { | 
|  | 1764 | if (halFrames == 0 || dspFrames == 0) { | 
|  | 1765 | return BAD_VALUE; | 
|  | 1766 | } | 
| Eric Laurent | b8ba0a9 | 2011-08-07 16:32:26 -0700 | [diff] [blame] | 1767 | Mutex::Autolock _l(mLock); | 
| Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 1768 | if (initCheck() != NO_ERROR) { | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1769 | return INVALID_OPERATION; | 
|  | 1770 | } | 
| Dima Zavin | 799a70e | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 1771 | *halFrames = mBytesWritten / audio_stream_frame_size(&mOutput->stream->common); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1772 |  | 
| Dima Zavin | 799a70e | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 1773 | return mOutput->stream->get_render_position(mOutput->stream, dspFrames); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1774 | } | 
|  | 1775 |  | 
| Eric Laurent | 39e94f8 | 2010-07-28 01:32:47 -0700 | [diff] [blame] | 1776 | uint32_t AudioFlinger::PlaybackThread::hasAudioSession(int sessionId) | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1777 | { | 
|  | 1778 | Mutex::Autolock _l(mLock); | 
| Eric Laurent | 39e94f8 | 2010-07-28 01:32:47 -0700 | [diff] [blame] | 1779 | uint32_t result = 0; | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1780 | if (getEffectChain_l(sessionId) != 0) { | 
| Eric Laurent | 39e94f8 | 2010-07-28 01:32:47 -0700 | [diff] [blame] | 1781 | result = EFFECT_SESSION; | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1782 | } | 
|  | 1783 |  | 
|  | 1784 | for (size_t i = 0; i < mTracks.size(); ++i) { | 
|  | 1785 | sp<Track> track = mTracks[i]; | 
| Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 1786 | if (sessionId == track->sessionId() && | 
|  | 1787 | !(track->mCblk->flags & CBLK_INVALID_MSK)) { | 
| Eric Laurent | 39e94f8 | 2010-07-28 01:32:47 -0700 | [diff] [blame] | 1788 | result |= TRACK_SESSION; | 
|  | 1789 | break; | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1790 | } | 
|  | 1791 | } | 
|  | 1792 |  | 
| Eric Laurent | 39e94f8 | 2010-07-28 01:32:47 -0700 | [diff] [blame] | 1793 | return result; | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1794 | } | 
|  | 1795 |  | 
| Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 1796 | uint32_t AudioFlinger::PlaybackThread::getStrategyForSession_l(int sessionId) | 
|  | 1797 | { | 
| Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 1798 | // session AUDIO_SESSION_OUTPUT_MIX is placed in same strategy as MUSIC stream so that | 
| Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 1799 | // it is moved to correct output by audio policy manager when A2DP is connected or disconnected | 
| Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 1800 | if (sessionId == AUDIO_SESSION_OUTPUT_MIX) { | 
|  | 1801 | return AudioSystem::getStrategyForStream(AUDIO_STREAM_MUSIC); | 
| Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 1802 | } | 
|  | 1803 | for (size_t i = 0; i < mTracks.size(); i++) { | 
|  | 1804 | sp<Track> track = mTracks[i]; | 
|  | 1805 | if (sessionId == track->sessionId() && | 
|  | 1806 | !(track->mCblk->flags & CBLK_INVALID_MSK)) { | 
| Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 1807 | return AudioSystem::getStrategyForStream((audio_stream_type_t) track->type()); | 
| Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 1808 | } | 
|  | 1809 | } | 
| Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 1810 | return AudioSystem::getStrategyForStream(AUDIO_STREAM_MUSIC); | 
| Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 1811 | } | 
|  | 1812 |  | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1813 |  | 
| Eric Laurent | b8ba0a9 | 2011-08-07 16:32:26 -0700 | [diff] [blame] | 1814 | AudioFlinger::AudioStreamOut* AudioFlinger::PlaybackThread::getOutput() | 
|  | 1815 | { | 
|  | 1816 | Mutex::Autolock _l(mLock); | 
|  | 1817 | return mOutput; | 
|  | 1818 | } | 
|  | 1819 |  | 
|  | 1820 | AudioFlinger::AudioStreamOut* AudioFlinger::PlaybackThread::clearOutput() | 
|  | 1821 | { | 
|  | 1822 | Mutex::Autolock _l(mLock); | 
|  | 1823 | AudioStreamOut *output = mOutput; | 
|  | 1824 | mOutput = NULL; | 
|  | 1825 | return output; | 
|  | 1826 | } | 
|  | 1827 |  | 
|  | 1828 | // this method must always be called either with ThreadBase mLock held or inside the thread loop | 
|  | 1829 | audio_stream_t* AudioFlinger::PlaybackThread::stream() | 
|  | 1830 | { | 
|  | 1831 | if (mOutput == NULL) { | 
|  | 1832 | return NULL; | 
|  | 1833 | } | 
|  | 1834 | return &mOutput->stream->common; | 
|  | 1835 | } | 
|  | 1836 |  | 
| Eric Laurent | 162b40b | 2011-12-05 09:47:19 -0800 | [diff] [blame] | 1837 | uint32_t AudioFlinger::PlaybackThread::activeSleepTimeUs() | 
|  | 1838 | { | 
|  | 1839 | // A2DP output latency is not due only to buffering capacity. It also reflects encoding, | 
|  | 1840 | // decoding and transfer time. So sleeping for half of the latency would likely cause | 
|  | 1841 | // underruns | 
|  | 1842 | if (audio_is_a2dp_device((audio_devices_t)mDevice)) { | 
|  | 1843 | return (uint32_t)((uint32_t)((mFrameCount * 1000) / mSampleRate) * 1000); | 
|  | 1844 | } else { | 
|  | 1845 | return (uint32_t)(mOutput->stream->get_latency(mOutput->stream) * 1000) / 2; | 
|  | 1846 | } | 
|  | 1847 | } | 
|  | 1848 |  | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1849 | // ---------------------------------------------------------------------------- | 
|  | 1850 |  | 
| Glenn Kasten | 23bb8be | 2012-01-26 10:38:26 -0800 | [diff] [blame^] | 1851 | AudioFlinger::MixerThread::MixerThread(const sp<AudioFlinger>& audioFlinger, AudioStreamOut* output, | 
|  | 1852 | int id, uint32_t device, type_t type) | 
|  | 1853 | :   PlaybackThread(audioFlinger, output, id, device, type), | 
| Eric Laurent | aeeb7e2 | 2012-01-19 10:00:02 -0800 | [diff] [blame] | 1854 | mAudioMixer(NULL), mPrevMixerStatus(MIXER_IDLE) | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1855 | { | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1856 | mAudioMixer = new AudioMixer(mFrameCount, mSampleRate); | 
|  | 1857 |  | 
|  | 1858 | // FIXME - Current mixer implementation only supports stereo output | 
|  | 1859 | if (mChannelCount == 1) { | 
| Steve Block | 29357bc | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 1860 | ALOGE("Invalid audio hardware channel count"); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1861 | } | 
|  | 1862 | } | 
|  | 1863 |  | 
|  | 1864 | AudioFlinger::MixerThread::~MixerThread() | 
|  | 1865 | { | 
|  | 1866 | delete mAudioMixer; | 
|  | 1867 | } | 
|  | 1868 |  | 
|  | 1869 | bool AudioFlinger::MixerThread::threadLoop() | 
|  | 1870 | { | 
|  | 1871 | Vector< sp<Track> > tracksToRemove; | 
|  | 1872 | uint32_t mixerStatus = MIXER_IDLE; | 
|  | 1873 | nsecs_t standbyTime = systemTime(); | 
|  | 1874 | size_t mixBufferSize = mFrameCount * mFrameSize; | 
|  | 1875 | // FIXME: Relaxed timing because of a certain device that can't meet latency | 
|  | 1876 | // Should be reduced to 2x after the vendor fixes the driver issue | 
| Eric Laurent | 5c4e818 | 2011-10-18 15:42:27 -0700 | [diff] [blame] | 1877 | // increase threshold again due to low power audio mode. The way this warning threshold is | 
|  | 1878 | // calculated and its usefulness should be reconsidered anyway. | 
|  | 1879 | nsecs_t maxPeriod = seconds(mFrameCount) / mSampleRate * 15; | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1880 | nsecs_t lastWarning = 0; | 
|  | 1881 | bool longStandbyExit = false; | 
|  | 1882 | uint32_t activeSleepTime = activeSleepTimeUs(); | 
|  | 1883 | uint32_t idleSleepTime = idleSleepTimeUs(); | 
|  | 1884 | uint32_t sleepTime = idleSleepTime; | 
| Eric Laurent | 7cafbb3 | 2011-11-22 18:50:29 -0800 | [diff] [blame] | 1885 | uint32_t sleepTimeShift = 0; | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1886 | Vector< sp<EffectChain> > effectChains; | 
| Glenn Kasten | 4d8d0c3 | 2011-07-08 15:26:12 -0700 | [diff] [blame] | 1887 | #ifdef DEBUG_CPU_USAGE | 
|  | 1888 | ThreadCpuUsage cpu; | 
|  | 1889 | const CentralTendencyStatistics& stats = cpu.statistics(); | 
|  | 1890 | #endif | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1891 |  | 
| Eric Laurent | feb0db6 | 2011-07-22 09:04:31 -0700 | [diff] [blame] | 1892 | acquireWakeLock(); | 
|  | 1893 |  | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1894 | while (!exitPending()) | 
|  | 1895 | { | 
| Glenn Kasten | 4d8d0c3 | 2011-07-08 15:26:12 -0700 | [diff] [blame] | 1896 | #ifdef DEBUG_CPU_USAGE | 
|  | 1897 | cpu.sampleAndEnable(); | 
|  | 1898 | unsigned n = stats.n(); | 
|  | 1899 | // cpu.elapsed() is expensive, so don't call it every loop | 
|  | 1900 | if ((n & 127) == 1) { | 
|  | 1901 | long long elapsed = cpu.elapsed(); | 
|  | 1902 | if (elapsed >= DEBUG_CPU_USAGE * 1000000000LL) { | 
|  | 1903 | double perLoop = elapsed / (double) n; | 
|  | 1904 | double perLoop100 = perLoop * 0.01; | 
|  | 1905 | double mean = stats.mean(); | 
|  | 1906 | double stddev = stats.stddev(); | 
|  | 1907 | double minimum = stats.minimum(); | 
|  | 1908 | double maximum = stats.maximum(); | 
|  | 1909 | cpu.resetStatistics(); | 
| Steve Block | df64d15 | 2012-01-04 20:05:49 +0000 | [diff] [blame] | 1910 | ALOGI("CPU usage over past %.1f secs (%u mixer loops at %.1f mean ms per loop):\n  us per mix loop: mean=%.0f stddev=%.0f min=%.0f max=%.0f\n  %% of wall: mean=%.1f stddev=%.1f min=%.1f max=%.1f", | 
| Glenn Kasten | 4d8d0c3 | 2011-07-08 15:26:12 -0700 | [diff] [blame] | 1911 | elapsed * .000000001, n, perLoop * .000001, | 
|  | 1912 | mean * .001, | 
|  | 1913 | stddev * .001, | 
|  | 1914 | minimum * .001, | 
|  | 1915 | maximum * .001, | 
|  | 1916 | mean / perLoop100, | 
|  | 1917 | stddev / perLoop100, | 
|  | 1918 | minimum / perLoop100, | 
|  | 1919 | maximum / perLoop100); | 
|  | 1920 | } | 
|  | 1921 | } | 
|  | 1922 | #endif | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1923 | processConfigEvents(); | 
|  | 1924 |  | 
|  | 1925 | mixerStatus = MIXER_IDLE; | 
|  | 1926 | { // scope for mLock | 
|  | 1927 |  | 
|  | 1928 | Mutex::Autolock _l(mLock); | 
|  | 1929 |  | 
|  | 1930 | if (checkForNewParameters_l()) { | 
|  | 1931 | mixBufferSize = mFrameCount * mFrameSize; | 
|  | 1932 | // FIXME: Relaxed timing because of a certain device that can't meet latency | 
|  | 1933 | // Should be reduced to 2x after the vendor fixes the driver issue | 
| Eric Laurent | 5c4e818 | 2011-10-18 15:42:27 -0700 | [diff] [blame] | 1934 | // increase threshold again due to low power audio mode. The way this warning | 
|  | 1935 | // threshold is calculated and its usefulness should be reconsidered anyway. | 
|  | 1936 | maxPeriod = seconds(mFrameCount) / mSampleRate * 15; | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1937 | activeSleepTime = activeSleepTimeUs(); | 
|  | 1938 | idleSleepTime = idleSleepTimeUs(); | 
|  | 1939 | } | 
|  | 1940 |  | 
|  | 1941 | const SortedVector< wp<Track> >& activeTracks = mActiveTracks; | 
|  | 1942 |  | 
|  | 1943 | // put audio hardware into standby after short delay | 
| Glenn Kasten | f6b1678 | 2011-12-15 09:51:17 -0800 | [diff] [blame] | 1944 | if (CC_UNLIKELY((!activeTracks.size() && systemTime() > standbyTime) || | 
|  | 1945 | mSuspended)) { | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1946 | if (!mStandby) { | 
| Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 1947 | ALOGV("Audio hardware entering standby, mixer %p, mSuspended %d\n", this, mSuspended); | 
| Dima Zavin | 799a70e | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 1948 | mOutput->stream->common.standby(&mOutput->stream->common); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1949 | mStandby = true; | 
|  | 1950 | mBytesWritten = 0; | 
|  | 1951 | } | 
|  | 1952 |  | 
|  | 1953 | if (!activeTracks.size() && mConfigEvents.isEmpty()) { | 
|  | 1954 | // we're about to wait, flush the binder command buffer | 
|  | 1955 | IPCThreadState::self()->flushCommands(); | 
|  | 1956 |  | 
|  | 1957 | if (exitPending()) break; | 
|  | 1958 |  | 
| Eric Laurent | feb0db6 | 2011-07-22 09:04:31 -0700 | [diff] [blame] | 1959 | releaseWakeLock_l(); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1960 | // wait until we have something to do... | 
| Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 1961 | ALOGV("MixerThread %p TID %d going to sleep\n", this, gettid()); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1962 | mWaitWorkCV.wait(mLock); | 
| Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 1963 | ALOGV("MixerThread %p TID %d waking up\n", this, gettid()); | 
| Eric Laurent | feb0db6 | 2011-07-22 09:04:31 -0700 | [diff] [blame] | 1964 | acquireWakeLock_l(); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1965 |  | 
| Eric Laurent | 2774144 | 2012-01-17 19:20:12 -0800 | [diff] [blame] | 1966 | mPrevMixerStatus = MIXER_IDLE; | 
| Glenn Kasten | f1d4592 | 2012-01-13 15:54:24 -0800 | [diff] [blame] | 1967 | if (!mMasterMute) { | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1968 | char value[PROPERTY_VALUE_MAX]; | 
|  | 1969 | property_get("ro.audio.silent", value, "0"); | 
|  | 1970 | if (atoi(value)) { | 
| Steve Block | b8a8052 | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 1971 | ALOGD("Silence is golden"); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1972 | setMasterMute(true); | 
|  | 1973 | } | 
|  | 1974 | } | 
|  | 1975 |  | 
|  | 1976 | standbyTime = systemTime() + kStandbyTimeInNsecs; | 
|  | 1977 | sleepTime = idleSleepTime; | 
| Eric Laurent | 7cafbb3 | 2011-11-22 18:50:29 -0800 | [diff] [blame] | 1978 | sleepTimeShift = 0; | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1979 | continue; | 
|  | 1980 | } | 
|  | 1981 | } | 
|  | 1982 |  | 
|  | 1983 | mixerStatus = prepareTracks_l(activeTracks, &tracksToRemove); | 
|  | 1984 |  | 
|  | 1985 | // prevent any changes in effect chain list and in each effect chain | 
|  | 1986 | // during mixing and effect process as the audio buffers could be deleted | 
|  | 1987 | // or modified if an effect is created or deleted | 
| Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 1988 | lockEffectChains_l(effectChains); | 
| Glenn Kasten | c5ac4cb | 2011-12-12 09:05:55 -0800 | [diff] [blame] | 1989 | } | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1990 |  | 
| Glenn Kasten | f6b1678 | 2011-12-15 09:51:17 -0800 | [diff] [blame] | 1991 | if (CC_LIKELY(mixerStatus == MIXER_TRACKS_READY)) { | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1992 | // mix buffers... | 
|  | 1993 | mAudioMixer->process(); | 
| Eric Laurent | 21e4b6e | 2012-01-23 18:56:59 -0800 | [diff] [blame] | 1994 | // increase sleep time progressively when application underrun condition clears. | 
|  | 1995 | // Only increase sleep time if the mixer is ready for two consecutive times to avoid | 
|  | 1996 | // that a steady state of alternating ready/not ready conditions keeps the sleep time | 
|  | 1997 | // such that we would underrun the audio HAL. | 
|  | 1998 | if ((sleepTime == 0) && (sleepTimeShift > 0)) { | 
| Eric Laurent | 7cafbb3 | 2011-11-22 18:50:29 -0800 | [diff] [blame] | 1999 | sleepTimeShift--; | 
|  | 2000 | } | 
| Eric Laurent | 21e4b6e | 2012-01-23 18:56:59 -0800 | [diff] [blame] | 2001 | sleepTime = 0; | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2002 | standbyTime = systemTime() + kStandbyTimeInNsecs; | 
|  | 2003 | //TODO: delay standby when effects have a tail | 
|  | 2004 | } else { | 
|  | 2005 | // If no tracks are ready, sleep once for the duration of an output | 
|  | 2006 | // buffer size, then write 0s to the output | 
|  | 2007 | if (sleepTime == 0) { | 
|  | 2008 | if (mixerStatus == MIXER_TRACKS_ENABLED) { | 
| Eric Laurent | 7cafbb3 | 2011-11-22 18:50:29 -0800 | [diff] [blame] | 2009 | sleepTime = activeSleepTime >> sleepTimeShift; | 
|  | 2010 | if (sleepTime < kMinThreadSleepTimeUs) { | 
|  | 2011 | sleepTime = kMinThreadSleepTimeUs; | 
|  | 2012 | } | 
|  | 2013 | // reduce sleep time in case of consecutive application underruns to avoid | 
|  | 2014 | // starving the audio HAL. As activeSleepTimeUs() is larger than a buffer | 
|  | 2015 | // duration we would end up writing less data than needed by the audio HAL if | 
|  | 2016 | // the condition persists. | 
|  | 2017 | if (sleepTimeShift < kMaxThreadSleepTimeShift) { | 
|  | 2018 | sleepTimeShift++; | 
|  | 2019 | } | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2020 | } else { | 
|  | 2021 | sleepTime = idleSleepTime; | 
|  | 2022 | } | 
|  | 2023 | } else if (mBytesWritten != 0 || | 
|  | 2024 | (mixerStatus == MIXER_TRACKS_ENABLED && longStandbyExit)) { | 
|  | 2025 | memset (mMixBuffer, 0, mixBufferSize); | 
|  | 2026 | sleepTime = 0; | 
| Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 2027 | ALOGV_IF((mBytesWritten == 0 && (mixerStatus == MIXER_TRACKS_ENABLED && longStandbyExit)), "anticipated start"); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2028 | } | 
|  | 2029 | // TODO add standby time extension fct of effect tail | 
|  | 2030 | } | 
|  | 2031 |  | 
|  | 2032 | if (mSuspended) { | 
| Eric Laurent | 25cbe0e | 2010-08-18 18:13:17 -0700 | [diff] [blame] | 2033 | sleepTime = suspendSleepTimeUs(); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2034 | } | 
|  | 2035 | // sleepTime == 0 means we must write to audio hardware | 
|  | 2036 | if (sleepTime == 0) { | 
| Glenn Kasten | c5ac4cb | 2011-12-12 09:05:55 -0800 | [diff] [blame] | 2037 | for (size_t i = 0; i < effectChains.size(); i ++) { | 
|  | 2038 | effectChains[i]->process_l(); | 
|  | 2039 | } | 
|  | 2040 | // enable changes in effect chain | 
|  | 2041 | unlockEffectChains(effectChains); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2042 | mLastWriteTime = systemTime(); | 
|  | 2043 | mInWrite = true; | 
|  | 2044 | mBytesWritten += mixBufferSize; | 
|  | 2045 |  | 
| Dima Zavin | 799a70e | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 2046 | int bytesWritten = (int)mOutput->stream->write(mOutput->stream, mMixBuffer, mixBufferSize); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2047 | if (bytesWritten < 0) mBytesWritten -= mixBufferSize; | 
|  | 2048 | mNumWrites++; | 
|  | 2049 | mInWrite = false; | 
|  | 2050 | nsecs_t now = systemTime(); | 
|  | 2051 | nsecs_t delta = now - mLastWriteTime; | 
| Eric Laurent | 5c4e818 | 2011-10-18 15:42:27 -0700 | [diff] [blame] | 2052 | if (!mStandby && delta > maxPeriod) { | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2053 | mNumDelayedWrites++; | 
| Glenn Kasten | 7dede87 | 2011-12-13 11:04:14 -0800 | [diff] [blame] | 2054 | if ((now - lastWarning) > kWarningThrottleNs) { | 
| Steve Block | 5ff1dd5 | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 2055 | ALOGW("write blocked for %llu msecs, %d delayed writes, thread %p", | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2056 | ns2ms(delta), mNumDelayedWrites, this); | 
|  | 2057 | lastWarning = now; | 
|  | 2058 | } | 
|  | 2059 | if (mStandby) { | 
|  | 2060 | longStandbyExit = true; | 
|  | 2061 | } | 
|  | 2062 | } | 
|  | 2063 | mStandby = false; | 
|  | 2064 | } else { | 
|  | 2065 | // enable changes in effect chain | 
| Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 2066 | unlockEffectChains(effectChains); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2067 | usleep(sleepTime); | 
|  | 2068 | } | 
|  | 2069 |  | 
|  | 2070 | // finally let go of all our tracks, without the lock held | 
|  | 2071 | // since we can't guarantee the destructors won't acquire that | 
|  | 2072 | // same lock. | 
|  | 2073 | tracksToRemove.clear(); | 
|  | 2074 |  | 
|  | 2075 | // Effect chains will be actually deleted here if they were removed from | 
|  | 2076 | // mEffectChains list during mixing or effects processing | 
|  | 2077 | effectChains.clear(); | 
|  | 2078 | } | 
|  | 2079 |  | 
|  | 2080 | if (!mStandby) { | 
| Dima Zavin | 799a70e | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 2081 | mOutput->stream->common.standby(&mOutput->stream->common); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2082 | } | 
|  | 2083 |  | 
| Eric Laurent | feb0db6 | 2011-07-22 09:04:31 -0700 | [diff] [blame] | 2084 | releaseWakeLock(); | 
|  | 2085 |  | 
| Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 2086 | ALOGV("MixerThread %p exiting", this); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2087 | return false; | 
|  | 2088 | } | 
|  | 2089 |  | 
|  | 2090 | // prepareTracks_l() must be called with ThreadBase::mLock held | 
|  | 2091 | uint32_t AudioFlinger::MixerThread::prepareTracks_l(const SortedVector< wp<Track> >& activeTracks, Vector< sp<Track> > *tracksToRemove) | 
|  | 2092 | { | 
|  | 2093 |  | 
|  | 2094 | uint32_t mixerStatus = MIXER_IDLE; | 
|  | 2095 | // find out which tracks need to be processed | 
|  | 2096 | size_t count = activeTracks.size(); | 
|  | 2097 | size_t mixedTracks = 0; | 
|  | 2098 | size_t tracksWithEffect = 0; | 
|  | 2099 |  | 
|  | 2100 | float masterVolume = mMasterVolume; | 
|  | 2101 | bool  masterMute = mMasterMute; | 
|  | 2102 |  | 
| Eric Laurent | 571d49c | 2010-08-11 05:20:11 -0700 | [diff] [blame] | 2103 | if (masterMute) { | 
|  | 2104 | masterVolume = 0; | 
|  | 2105 | } | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2106 | // Delegate master volume control to effect in output mix effect chain if needed | 
| Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 2107 | sp<EffectChain> chain = getEffectChain_l(AUDIO_SESSION_OUTPUT_MIX); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2108 | if (chain != 0) { | 
| Eric Laurent | 571d49c | 2010-08-11 05:20:11 -0700 | [diff] [blame] | 2109 | uint32_t v = (uint32_t)(masterVolume * (1 << 24)); | 
| Eric Laurent | cab1124 | 2010-07-15 12:50:15 -0700 | [diff] [blame] | 2110 | chain->setVolume_l(&v, &v); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2111 | masterVolume = (float)((v + (1 << 23)) >> 24); | 
|  | 2112 | chain.clear(); | 
|  | 2113 | } | 
|  | 2114 |  | 
|  | 2115 | for (size_t i=0 ; i<count ; i++) { | 
|  | 2116 | sp<Track> t = activeTracks[i].promote(); | 
|  | 2117 | if (t == 0) continue; | 
|  | 2118 |  | 
| Glenn Kasten | 9c56d4a | 2011-12-19 15:06:39 -0800 | [diff] [blame] | 2119 | // this const just means the local variable doesn't change | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2120 | Track* const track = t.get(); | 
|  | 2121 | audio_track_cblk_t* cblk = track->cblk(); | 
|  | 2122 |  | 
|  | 2123 | // The first time a track is added we wait | 
|  | 2124 | // for all its buffers to be filled before processing it | 
| Glenn Kasten | 9c56d4a | 2011-12-19 15:06:39 -0800 | [diff] [blame] | 2125 | int name = track->name(); | 
| Eric Laurent | a47b69c | 2011-11-08 18:10:16 -0800 | [diff] [blame] | 2126 | // make sure that we have enough frames to mix one full buffer. | 
|  | 2127 | // enforce this condition only once to enable draining the buffer in case the client | 
|  | 2128 | // app does not call stop() and relies on underrun to stop: | 
| Eric Laurent | 2774144 | 2012-01-17 19:20:12 -0800 | [diff] [blame] | 2129 | // hence the test on (mPrevMixerStatus == MIXER_TRACKS_READY) meaning the track was mixed | 
| Eric Laurent | a47b69c | 2011-11-08 18:10:16 -0800 | [diff] [blame] | 2130 | // during last round | 
| Eric Laurent | 3dbe320 | 2011-11-03 12:16:05 -0700 | [diff] [blame] | 2131 | uint32_t minFrames = 1; | 
| Eric Laurent | a47b69c | 2011-11-08 18:10:16 -0800 | [diff] [blame] | 2132 | if (!track->isStopped() && !track->isPausing() && | 
| Eric Laurent | 2774144 | 2012-01-17 19:20:12 -0800 | [diff] [blame] | 2133 | (mPrevMixerStatus == MIXER_TRACKS_READY)) { | 
| Eric Laurent | 3dbe320 | 2011-11-03 12:16:05 -0700 | [diff] [blame] | 2134 | if (t->sampleRate() == (int)mSampleRate) { | 
|  | 2135 | minFrames = mFrameCount; | 
|  | 2136 | } else { | 
| Eric Laurent | 071ccd5 | 2011-12-22 16:08:41 -0800 | [diff] [blame] | 2137 | // +1 for rounding and +1 for additional sample needed for interpolation | 
|  | 2138 | minFrames = (mFrameCount * t->sampleRate()) / mSampleRate + 1 + 1; | 
|  | 2139 | // add frames already consumed but not yet released by the resampler | 
|  | 2140 | // because cblk->framesReady() will  include these frames | 
|  | 2141 | minFrames += mAudioMixer->getUnreleasedFrames(track->name()); | 
|  | 2142 | // the minimum track buffer size is normally twice the number of frames necessary | 
|  | 2143 | // to fill one buffer and the resampler should not leave more than one buffer worth | 
|  | 2144 | // of unreleased frames after each pass, but just in case... | 
| Steve Block | c1dc1cb | 2012-01-09 18:35:44 +0000 | [diff] [blame] | 2145 | ALOG_ASSERT(minFrames <= cblk->frameCount); | 
| Eric Laurent | 3dbe320 | 2011-11-03 12:16:05 -0700 | [diff] [blame] | 2146 | } | 
|  | 2147 | } | 
|  | 2148 | if ((cblk->framesReady() >= minFrames) && track->isReady() && | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2149 | !track->isPaused() && !track->isTerminated()) | 
|  | 2150 | { | 
| Glenn Kasten | 9c56d4a | 2011-12-19 15:06:39 -0800 | [diff] [blame] | 2151 | //ALOGV("track %d u=%08x, s=%08x [OK] on thread %p", name, cblk->user, cblk->server, this); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2152 |  | 
|  | 2153 | mixedTracks++; | 
|  | 2154 |  | 
|  | 2155 | // track->mainBuffer() != mMixBuffer means there is an effect chain | 
|  | 2156 | // connected to the track | 
|  | 2157 | chain.clear(); | 
|  | 2158 | if (track->mainBuffer() != mMixBuffer) { | 
|  | 2159 | chain = getEffectChain_l(track->sessionId()); | 
|  | 2160 | // Delegate volume control to effect in track effect chain if needed | 
|  | 2161 | if (chain != 0) { | 
|  | 2162 | tracksWithEffect++; | 
|  | 2163 | } else { | 
| Steve Block | 5ff1dd5 | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 2164 | ALOGW("prepareTracks_l(): track %d attached to effect but no chain found on session %d", | 
| Glenn Kasten | 9c56d4a | 2011-12-19 15:06:39 -0800 | [diff] [blame] | 2165 | name, track->sessionId()); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2166 | } | 
|  | 2167 | } | 
|  | 2168 |  | 
|  | 2169 |  | 
|  | 2170 | int param = AudioMixer::VOLUME; | 
|  | 2171 | if (track->mFillingUpStatus == Track::FS_FILLED) { | 
|  | 2172 | // no ramp for the first volume setting | 
|  | 2173 | track->mFillingUpStatus = Track::FS_ACTIVE; | 
|  | 2174 | if (track->mState == TrackBase::RESUMING) { | 
|  | 2175 | track->mState = TrackBase::ACTIVE; | 
|  | 2176 | param = AudioMixer::RAMP_VOLUME; | 
|  | 2177 | } | 
| Glenn Kasten | 9c56d4a | 2011-12-19 15:06:39 -0800 | [diff] [blame] | 2178 | mAudioMixer->setParameter(name, AudioMixer::RESAMPLE, AudioMixer::RESET, NULL); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2179 | } else if (cblk->server != 0) { | 
|  | 2180 | // If the track is stopped before the first frame was mixed, | 
|  | 2181 | // do not apply ramp | 
|  | 2182 | param = AudioMixer::RAMP_VOLUME; | 
|  | 2183 | } | 
|  | 2184 |  | 
|  | 2185 | // compute volume for this track | 
| Eric Laurent | e0aed6d | 2010-09-10 17:44:44 -0700 | [diff] [blame] | 2186 | uint32_t vl, vr, va; | 
| Eric Laurent | 8569f0d | 2010-07-29 23:43:43 -0700 | [diff] [blame] | 2187 | if (track->isMuted() || track->isPausing() || | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2188 | mStreamTypes[track->type()].mute) { | 
| Eric Laurent | e0aed6d | 2010-09-10 17:44:44 -0700 | [diff] [blame] | 2189 | vl = vr = va = 0; | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2190 | if (track->isPausing()) { | 
|  | 2191 | track->setPaused(); | 
|  | 2192 | } | 
|  | 2193 | } else { | 
| Eric Laurent | e0aed6d | 2010-09-10 17:44:44 -0700 | [diff] [blame] | 2194 |  | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2195 | // read original volumes with volume control | 
|  | 2196 | float typeVolume = mStreamTypes[track->type()].volume; | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2197 | float v = masterVolume * typeVolume; | 
| Glenn Kasten | b1cf75c | 2012-01-17 12:20:54 -0800 | [diff] [blame] | 2198 | uint32_t vlr = cblk->volumeLR; | 
|  | 2199 | vl = vlr & 0xFFFF; | 
|  | 2200 | vr = vlr >> 16; | 
|  | 2201 | // track volumes come from shared memory, so can't be trusted and must be clamped | 
|  | 2202 | if (vl > MAX_GAIN_INT) { | 
|  | 2203 | ALOGV("Track left volume out of range: %04X", vl); | 
|  | 2204 | vl = MAX_GAIN_INT; | 
|  | 2205 | } | 
|  | 2206 | if (vr > MAX_GAIN_INT) { | 
|  | 2207 | ALOGV("Track right volume out of range: %04X", vr); | 
|  | 2208 | vr = MAX_GAIN_INT; | 
|  | 2209 | } | 
|  | 2210 | // now apply the master volume and stream type volume | 
|  | 2211 | vl = (uint32_t)(v * vl) << 12; | 
|  | 2212 | vr = (uint32_t)(v * vr) << 12; | 
|  | 2213 | // assuming master volume and stream type volume each go up to 1.0, | 
|  | 2214 | // vl and vr are now in 8.24 format | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2215 |  | 
| Glenn Kasten | 05632a5 | 2012-01-03 14:22:33 -0800 | [diff] [blame] | 2216 | uint16_t sendLevel = cblk->getSendLevel_U4_12(); | 
|  | 2217 | // send level comes from shared memory and so may be corrupt | 
| Glenn Kasten | b1cf75c | 2012-01-17 12:20:54 -0800 | [diff] [blame] | 2218 | if (sendLevel >= MAX_GAIN_INT) { | 
| Glenn Kasten | 05632a5 | 2012-01-03 14:22:33 -0800 | [diff] [blame] | 2219 | ALOGV("Track send level out of range: %04X", sendLevel); | 
| Glenn Kasten | b1cf75c | 2012-01-17 12:20:54 -0800 | [diff] [blame] | 2220 | sendLevel = MAX_GAIN_INT; | 
| Glenn Kasten | 05632a5 | 2012-01-03 14:22:33 -0800 | [diff] [blame] | 2221 | } | 
|  | 2222 | va = (uint32_t)(v * sendLevel); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2223 | } | 
| Eric Laurent | e0aed6d | 2010-09-10 17:44:44 -0700 | [diff] [blame] | 2224 | // Delegate volume control to effect in track effect chain if needed | 
|  | 2225 | if (chain != 0 && chain->setVolume_l(&vl, &vr)) { | 
|  | 2226 | // Do not ramp volume if volume is controlled by effect | 
|  | 2227 | param = AudioMixer::VOLUME; | 
|  | 2228 | track->mHasVolumeController = true; | 
|  | 2229 | } else { | 
|  | 2230 | // force no volume ramp when volume controller was just disabled or removed | 
|  | 2231 | // from effect chain to avoid volume spike | 
|  | 2232 | if (track->mHasVolumeController) { | 
|  | 2233 | param = AudioMixer::VOLUME; | 
|  | 2234 | } | 
|  | 2235 | track->mHasVolumeController = false; | 
|  | 2236 | } | 
|  | 2237 |  | 
|  | 2238 | // Convert volumes from 8.24 to 4.12 format | 
|  | 2239 | int16_t left, right, aux; | 
| Glenn Kasten | b1cf75c | 2012-01-17 12:20:54 -0800 | [diff] [blame] | 2240 | // This additional clamping is needed in case chain->setVolume_l() overshot | 
| Eric Laurent | e0aed6d | 2010-09-10 17:44:44 -0700 | [diff] [blame] | 2241 | uint32_t v_clamped = (vl + (1 << 11)) >> 12; | 
|  | 2242 | if (v_clamped > MAX_GAIN_INT) v_clamped = MAX_GAIN_INT; | 
|  | 2243 | left = int16_t(v_clamped); | 
|  | 2244 | v_clamped = (vr + (1 << 11)) >> 12; | 
|  | 2245 | if (v_clamped > MAX_GAIN_INT) v_clamped = MAX_GAIN_INT; | 
|  | 2246 | right = int16_t(v_clamped); | 
|  | 2247 |  | 
|  | 2248 | if (va > MAX_GAIN_INT) va = MAX_GAIN_INT; | 
|  | 2249 | aux = int16_t(va); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2250 |  | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2251 | // XXX: these things DON'T need to be done each time | 
| Glenn Kasten | 9c56d4a | 2011-12-19 15:06:39 -0800 | [diff] [blame] | 2252 | mAudioMixer->setBufferProvider(name, track); | 
|  | 2253 | mAudioMixer->enable(name); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2254 |  | 
| Glenn Kasten | 9c56d4a | 2011-12-19 15:06:39 -0800 | [diff] [blame] | 2255 | mAudioMixer->setParameter(name, param, AudioMixer::VOLUME0, (void *)left); | 
|  | 2256 | mAudioMixer->setParameter(name, param, AudioMixer::VOLUME1, (void *)right); | 
|  | 2257 | mAudioMixer->setParameter(name, param, AudioMixer::AUXLEVEL, (void *)aux); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2258 | mAudioMixer->setParameter( | 
| Glenn Kasten | 9c56d4a | 2011-12-19 15:06:39 -0800 | [diff] [blame] | 2259 | name, | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2260 | AudioMixer::TRACK, | 
|  | 2261 | AudioMixer::FORMAT, (void *)track->format()); | 
|  | 2262 | mAudioMixer->setParameter( | 
| Glenn Kasten | 9c56d4a | 2011-12-19 15:06:39 -0800 | [diff] [blame] | 2263 | name, | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2264 | AudioMixer::TRACK, | 
| Jean-Michel Trivi | 0d255b2 | 2011-05-24 15:53:33 -0700 | [diff] [blame] | 2265 | AudioMixer::CHANNEL_MASK, (void *)track->channelMask()); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2266 | mAudioMixer->setParameter( | 
| Glenn Kasten | 9c56d4a | 2011-12-19 15:06:39 -0800 | [diff] [blame] | 2267 | name, | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2268 | AudioMixer::RESAMPLE, | 
|  | 2269 | AudioMixer::SAMPLE_RATE, | 
|  | 2270 | (void *)(cblk->sampleRate)); | 
|  | 2271 | mAudioMixer->setParameter( | 
| Glenn Kasten | 9c56d4a | 2011-12-19 15:06:39 -0800 | [diff] [blame] | 2272 | name, | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2273 | AudioMixer::TRACK, | 
|  | 2274 | AudioMixer::MAIN_BUFFER, (void *)track->mainBuffer()); | 
|  | 2275 | mAudioMixer->setParameter( | 
| Glenn Kasten | 9c56d4a | 2011-12-19 15:06:39 -0800 | [diff] [blame] | 2276 | name, | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2277 | AudioMixer::TRACK, | 
|  | 2278 | AudioMixer::AUX_BUFFER, (void *)track->auxBuffer()); | 
|  | 2279 |  | 
|  | 2280 | // reset retry count | 
|  | 2281 | track->mRetryCount = kMaxTrackRetries; | 
| Eric Laurent | 2774144 | 2012-01-17 19:20:12 -0800 | [diff] [blame] | 2282 | // If one track is ready, set the mixer ready if: | 
|  | 2283 | //  - the mixer was not ready during previous round OR | 
|  | 2284 | //  - no other track is not ready | 
|  | 2285 | if (mPrevMixerStatus != MIXER_TRACKS_READY || | 
|  | 2286 | mixerStatus != MIXER_TRACKS_ENABLED) { | 
|  | 2287 | mixerStatus = MIXER_TRACKS_READY; | 
|  | 2288 | } | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2289 | } else { | 
| Glenn Kasten | 9c56d4a | 2011-12-19 15:06:39 -0800 | [diff] [blame] | 2290 | //ALOGV("track %d u=%08x, s=%08x [NOT READY] on thread %p", name, cblk->user, cblk->server, this); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2291 | if (track->isStopped()) { | 
|  | 2292 | track->reset(); | 
|  | 2293 | } | 
|  | 2294 | if (track->isTerminated() || track->isStopped() || track->isPaused()) { | 
|  | 2295 | // We have consumed all the buffers of this track. | 
|  | 2296 | // Remove it from the list of active tracks. | 
|  | 2297 | tracksToRemove->add(track); | 
|  | 2298 | } else { | 
|  | 2299 | // No buffers for this track. Give it a few chances to | 
|  | 2300 | // fill a buffer, then remove it from active list. | 
|  | 2301 | if (--(track->mRetryCount) <= 0) { | 
| Glenn Kasten | 9c56d4a | 2011-12-19 15:06:39 -0800 | [diff] [blame] | 2302 | ALOGV("BUFFER TIMEOUT: remove(%d) from active list on thread %p", name, this); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2303 | tracksToRemove->add(track); | 
| Eric Laurent | 44d9848 | 2010-09-30 16:12:31 -0700 | [diff] [blame] | 2304 | // indicate to client process that the track was disabled because of underrun | 
| Eric Laurent | 38ccae2 | 2011-03-28 18:37:07 -0700 | [diff] [blame] | 2305 | android_atomic_or(CBLK_DISABLED_ON, &cblk->flags); | 
| Eric Laurent | 2774144 | 2012-01-17 19:20:12 -0800 | [diff] [blame] | 2306 | // If one track is not ready, mark the mixer also not ready if: | 
|  | 2307 | //  - the mixer was ready during previous round OR | 
|  | 2308 | //  - no other track is ready | 
|  | 2309 | } else if (mPrevMixerStatus == MIXER_TRACKS_READY || | 
|  | 2310 | mixerStatus != MIXER_TRACKS_READY) { | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2311 | mixerStatus = MIXER_TRACKS_ENABLED; | 
|  | 2312 | } | 
|  | 2313 | } | 
| Glenn Kasten | 9c56d4a | 2011-12-19 15:06:39 -0800 | [diff] [blame] | 2314 | mAudioMixer->disable(name); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2315 | } | 
|  | 2316 | } | 
|  | 2317 |  | 
|  | 2318 | // remove all the tracks that need to be... | 
|  | 2319 | count = tracksToRemove->size(); | 
| Glenn Kasten | f6b1678 | 2011-12-15 09:51:17 -0800 | [diff] [blame] | 2320 | if (CC_UNLIKELY(count)) { | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2321 | for (size_t i=0 ; i<count ; i++) { | 
|  | 2322 | const sp<Track>& track = tracksToRemove->itemAt(i); | 
|  | 2323 | mActiveTracks.remove(track); | 
|  | 2324 | if (track->mainBuffer() != mMixBuffer) { | 
|  | 2325 | chain = getEffectChain_l(track->sessionId()); | 
|  | 2326 | if (chain != 0) { | 
| Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 2327 | ALOGV("stopping track on chain %p for session Id: %d", chain.get(), track->sessionId()); | 
| Eric Laurent | b469b94 | 2011-05-09 12:09:06 -0700 | [diff] [blame] | 2328 | chain->decActiveTrackCnt(); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2329 | } | 
|  | 2330 | } | 
|  | 2331 | if (track->isTerminated()) { | 
| Eric Laurent | b469b94 | 2011-05-09 12:09:06 -0700 | [diff] [blame] | 2332 | removeTrack_l(track); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2333 | } | 
|  | 2334 | } | 
|  | 2335 | } | 
|  | 2336 |  | 
|  | 2337 | // mix buffer must be cleared if all tracks are connected to an | 
|  | 2338 | // effect chain as in this case the mixer will not write to | 
|  | 2339 | // mix buffer and track effects will accumulate into it | 
|  | 2340 | if (mixedTracks != 0 && mixedTracks == tracksWithEffect) { | 
|  | 2341 | memset(mMixBuffer, 0, mFrameCount * mChannelCount * sizeof(int16_t)); | 
|  | 2342 | } | 
|  | 2343 |  | 
| Eric Laurent | 2774144 | 2012-01-17 19:20:12 -0800 | [diff] [blame] | 2344 | mPrevMixerStatus = mixerStatus; | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2345 | return mixerStatus; | 
|  | 2346 | } | 
|  | 2347 |  | 
| Glenn Kasten | fff6d71 | 2012-01-12 16:38:12 -0800 | [diff] [blame] | 2348 | void AudioFlinger::MixerThread::invalidateTracks(audio_stream_type_t streamType) | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2349 | { | 
| Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 2350 | ALOGV ("MixerThread::invalidateTracks() mixer %p, streamType %d, mTracks.size %d", | 
| Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 2351 | this,  streamType, mTracks.size()); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2352 | Mutex::Autolock _l(mLock); | 
| Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 2353 |  | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2354 | size_t size = mTracks.size(); | 
|  | 2355 | for (size_t i = 0; i < size; i++) { | 
|  | 2356 | sp<Track> t = mTracks[i]; | 
|  | 2357 | if (t->type() == streamType) { | 
| Eric Laurent | 38ccae2 | 2011-03-28 18:37:07 -0700 | [diff] [blame] | 2358 | android_atomic_or(CBLK_INVALID_ON, &t->mCblk->flags); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2359 | t->mCblk->cv.signal(); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2360 | } | 
|  | 2361 | } | 
|  | 2362 | } | 
|  | 2363 |  | 
| Glenn Kasten | fff6d71 | 2012-01-12 16:38:12 -0800 | [diff] [blame] | 2364 | void AudioFlinger::PlaybackThread::setStreamValid(audio_stream_type_t streamType, bool valid) | 
| Eric Laurent | 9f6530f | 2011-08-30 10:18:54 -0700 | [diff] [blame] | 2365 | { | 
| Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 2366 | ALOGV ("PlaybackThread::setStreamValid() thread %p, streamType %d, valid %d", | 
| Eric Laurent | 9f6530f | 2011-08-30 10:18:54 -0700 | [diff] [blame] | 2367 | this,  streamType, valid); | 
|  | 2368 | Mutex::Autolock _l(mLock); | 
|  | 2369 |  | 
|  | 2370 | mStreamTypes[streamType].valid = valid; | 
|  | 2371 | } | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2372 |  | 
|  | 2373 | // getTrackName_l() must be called with ThreadBase::mLock held | 
|  | 2374 | int AudioFlinger::MixerThread::getTrackName_l() | 
|  | 2375 | { | 
|  | 2376 | return mAudioMixer->getTrackName(); | 
|  | 2377 | } | 
|  | 2378 |  | 
|  | 2379 | // deleteTrackName_l() must be called with ThreadBase::mLock held | 
|  | 2380 | void AudioFlinger::MixerThread::deleteTrackName_l(int name) | 
|  | 2381 | { | 
| Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 2382 | ALOGV("remove track (%d) and delete from mixer", name); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2383 | mAudioMixer->deleteTrackName(name); | 
|  | 2384 | } | 
|  | 2385 |  | 
|  | 2386 | // checkForNewParameters_l() must be called with ThreadBase::mLock held | 
|  | 2387 | bool AudioFlinger::MixerThread::checkForNewParameters_l() | 
|  | 2388 | { | 
|  | 2389 | bool reconfig = false; | 
|  | 2390 |  | 
|  | 2391 | while (!mNewParameters.isEmpty()) { | 
|  | 2392 | status_t status = NO_ERROR; | 
|  | 2393 | String8 keyValuePair = mNewParameters[0]; | 
|  | 2394 | AudioParameter param = AudioParameter(keyValuePair); | 
|  | 2395 | int value; | 
|  | 2396 |  | 
|  | 2397 | if (param.getInt(String8(AudioParameter::keySamplingRate), value) == NO_ERROR) { | 
|  | 2398 | reconfig = true; | 
|  | 2399 | } | 
|  | 2400 | if (param.getInt(String8(AudioParameter::keyFormat), value) == NO_ERROR) { | 
| Glenn Kasten | 58f3021 | 2012-01-12 12:27:51 -0800 | [diff] [blame] | 2401 | if ((audio_format_t) value != AUDIO_FORMAT_PCM_16_BIT) { | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2402 | status = BAD_VALUE; | 
|  | 2403 | } else { | 
|  | 2404 | reconfig = true; | 
|  | 2405 | } | 
|  | 2406 | } | 
|  | 2407 | if (param.getInt(String8(AudioParameter::keyChannels), value) == NO_ERROR) { | 
| Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 2408 | if (value != AUDIO_CHANNEL_OUT_STEREO) { | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2409 | status = BAD_VALUE; | 
|  | 2410 | } else { | 
|  | 2411 | reconfig = true; | 
|  | 2412 | } | 
|  | 2413 | } | 
|  | 2414 | if (param.getInt(String8(AudioParameter::keyFrameCount), value) == NO_ERROR) { | 
|  | 2415 | // do not accept frame count changes if tracks are open as the track buffer | 
| Glenn Kasten | 362c4e6 | 2011-12-14 10:28:06 -0800 | [diff] [blame] | 2416 | // size depends on frame count and correct behavior would not be guaranteed | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2417 | // if frame count is changed after track creation | 
|  | 2418 | if (!mTracks.isEmpty()) { | 
|  | 2419 | status = INVALID_OPERATION; | 
|  | 2420 | } else { | 
|  | 2421 | reconfig = true; | 
|  | 2422 | } | 
|  | 2423 | } | 
|  | 2424 | if (param.getInt(String8(AudioParameter::keyRouting), value) == NO_ERROR) { | 
| Gloria Wang | 9ee159b | 2011-02-24 14:51:45 -0800 | [diff] [blame] | 2425 | // when changing the audio output device, call addBatteryData to notify | 
|  | 2426 | // the change | 
| Eric Laurent | b469b94 | 2011-05-09 12:09:06 -0700 | [diff] [blame] | 2427 | if ((int)mDevice != value) { | 
| Gloria Wang | 9ee159b | 2011-02-24 14:51:45 -0800 | [diff] [blame] | 2428 | uint32_t params = 0; | 
|  | 2429 | // check whether speaker is on | 
| Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 2430 | if (value & AUDIO_DEVICE_OUT_SPEAKER) { | 
| Gloria Wang | 9ee159b | 2011-02-24 14:51:45 -0800 | [diff] [blame] | 2431 | params |= IMediaPlayerService::kBatteryDataSpeakerOn; | 
|  | 2432 | } | 
|  | 2433 |  | 
|  | 2434 | int deviceWithoutSpeaker | 
| Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 2435 | = AUDIO_DEVICE_OUT_ALL & ~AUDIO_DEVICE_OUT_SPEAKER; | 
| Gloria Wang | 9ee159b | 2011-02-24 14:51:45 -0800 | [diff] [blame] | 2436 | // check if any other device (except speaker) is on | 
|  | 2437 | if (value & deviceWithoutSpeaker ) { | 
|  | 2438 | params |= IMediaPlayerService::kBatteryDataOtherAudioDeviceOn; | 
|  | 2439 | } | 
|  | 2440 |  | 
|  | 2441 | if (params != 0) { | 
|  | 2442 | addBatteryData(params); | 
|  | 2443 | } | 
|  | 2444 | } | 
|  | 2445 |  | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2446 | // forward device change to effects that have requested to be | 
|  | 2447 | // aware of attached audio device. | 
|  | 2448 | mDevice = (uint32_t)value; | 
|  | 2449 | for (size_t i = 0; i < mEffectChains.size(); i++) { | 
| Eric Laurent | cab1124 | 2010-07-15 12:50:15 -0700 | [diff] [blame] | 2450 | mEffectChains[i]->setDevice_l(mDevice); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2451 | } | 
|  | 2452 | } | 
|  | 2453 |  | 
|  | 2454 | if (status == NO_ERROR) { | 
| Dima Zavin | 799a70e | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 2455 | status = mOutput->stream->common.set_parameters(&mOutput->stream->common, | 
| Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 2456 | keyValuePair.string()); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2457 | if (!mStandby && status == INVALID_OPERATION) { | 
| Dima Zavin | 799a70e | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 2458 | mOutput->stream->common.standby(&mOutput->stream->common); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2459 | mStandby = true; | 
|  | 2460 | mBytesWritten = 0; | 
| Dima Zavin | 799a70e | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 2461 | status = mOutput->stream->common.set_parameters(&mOutput->stream->common, | 
| Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 2462 | keyValuePair.string()); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2463 | } | 
|  | 2464 | if (status == NO_ERROR && reconfig) { | 
|  | 2465 | delete mAudioMixer; | 
|  | 2466 | readOutputParameters(); | 
|  | 2467 | mAudioMixer = new AudioMixer(mFrameCount, mSampleRate); | 
|  | 2468 | for (size_t i = 0; i < mTracks.size() ; i++) { | 
|  | 2469 | int name = getTrackName_l(); | 
|  | 2470 | if (name < 0) break; | 
|  | 2471 | mTracks[i]->mName = name; | 
|  | 2472 | // limit track sample rate to 2 x new output sample rate | 
|  | 2473 | if (mTracks[i]->mCblk->sampleRate > 2 * sampleRate()) { | 
|  | 2474 | mTracks[i]->mCblk->sampleRate = 2 * sampleRate(); | 
|  | 2475 | } | 
|  | 2476 | } | 
|  | 2477 | sendConfigEvent_l(AudioSystem::OUTPUT_CONFIG_CHANGED); | 
|  | 2478 | } | 
|  | 2479 | } | 
|  | 2480 |  | 
|  | 2481 | mNewParameters.removeAt(0); | 
|  | 2482 |  | 
|  | 2483 | mParamStatus = status; | 
|  | 2484 | mParamCond.signal(); | 
| Eric Laurent | 60cd0a0 | 2011-09-13 11:40:21 -0700 | [diff] [blame] | 2485 | // wait for condition with time out in case the thread calling ThreadBase::setParameters() | 
|  | 2486 | // already timed out waiting for the status and will never signal the condition. | 
| Glenn Kasten | 7dede87 | 2011-12-13 11:04:14 -0800 | [diff] [blame] | 2487 | mWaitWorkCV.waitRelative(mLock, kSetParametersTimeoutNs); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2488 | } | 
|  | 2489 | return reconfig; | 
|  | 2490 | } | 
|  | 2491 |  | 
|  | 2492 | status_t AudioFlinger::MixerThread::dumpInternals(int fd, const Vector<String16>& args) | 
|  | 2493 | { | 
|  | 2494 | const size_t SIZE = 256; | 
|  | 2495 | char buffer[SIZE]; | 
|  | 2496 | String8 result; | 
|  | 2497 |  | 
|  | 2498 | PlaybackThread::dumpInternals(fd, args); | 
|  | 2499 |  | 
|  | 2500 | snprintf(buffer, SIZE, "AudioMixer tracks: %08x\n", mAudioMixer->trackNames()); | 
|  | 2501 | result.append(buffer); | 
|  | 2502 | write(fd, result.string(), result.size()); | 
|  | 2503 | return NO_ERROR; | 
|  | 2504 | } | 
|  | 2505 |  | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2506 | uint32_t AudioFlinger::MixerThread::idleSleepTimeUs() | 
|  | 2507 | { | 
| Eric Laurent | 60e1824 | 2010-07-29 06:50:24 -0700 | [diff] [blame] | 2508 | return (uint32_t)(((mFrameCount * 1000) / mSampleRate) * 1000) / 2; | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2509 | } | 
|  | 2510 |  | 
| Eric Laurent | 25cbe0e | 2010-08-18 18:13:17 -0700 | [diff] [blame] | 2511 | uint32_t AudioFlinger::MixerThread::suspendSleepTimeUs() | 
|  | 2512 | { | 
|  | 2513 | return (uint32_t)(((mFrameCount * 1000) / mSampleRate) * 1000); | 
|  | 2514 | } | 
|  | 2515 |  | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2516 | // ---------------------------------------------------------------------------- | 
| Dima Zavin | 799a70e | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 2517 | AudioFlinger::DirectOutputThread::DirectOutputThread(const sp<AudioFlinger>& audioFlinger, AudioStreamOut* output, int id, uint32_t device) | 
| Glenn Kasten | 23bb8be | 2012-01-26 10:38:26 -0800 | [diff] [blame^] | 2518 | :   PlaybackThread(audioFlinger, output, id, device, DIRECT) | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2519 | { | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2520 | } | 
|  | 2521 |  | 
|  | 2522 | AudioFlinger::DirectOutputThread::~DirectOutputThread() | 
|  | 2523 | { | 
|  | 2524 | } | 
|  | 2525 |  | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2526 | static inline | 
|  | 2527 | int32_t mul(int16_t in, int16_t v) | 
|  | 2528 | { | 
|  | 2529 | #if defined(__arm__) && !defined(__thumb__) | 
|  | 2530 | int32_t out; | 
|  | 2531 | asm( "smulbb %[out], %[in], %[v] \n" | 
|  | 2532 | : [out]"=r"(out) | 
|  | 2533 | : [in]"%r"(in), [v]"r"(v) | 
|  | 2534 | : ); | 
|  | 2535 | return out; | 
|  | 2536 | #else | 
|  | 2537 | return in * int32_t(v); | 
|  | 2538 | #endif | 
|  | 2539 | } | 
|  | 2540 |  | 
|  | 2541 | void AudioFlinger::DirectOutputThread::applyVolume(uint16_t leftVol, uint16_t rightVol, bool ramp) | 
|  | 2542 | { | 
|  | 2543 | // Do not apply volume on compressed audio | 
| Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 2544 | if (!audio_is_linear_pcm(mFormat)) { | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2545 | return; | 
|  | 2546 | } | 
|  | 2547 |  | 
|  | 2548 | // convert to signed 16 bit before volume calculation | 
| Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 2549 | if (mFormat == AUDIO_FORMAT_PCM_8_BIT) { | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2550 | size_t count = mFrameCount * mChannelCount; | 
|  | 2551 | uint8_t *src = (uint8_t *)mMixBuffer + count-1; | 
|  | 2552 | int16_t *dst = mMixBuffer + count-1; | 
|  | 2553 | while(count--) { | 
|  | 2554 | *dst-- = (int16_t)(*src--^0x80) << 8; | 
|  | 2555 | } | 
|  | 2556 | } | 
|  | 2557 |  | 
|  | 2558 | size_t frameCount = mFrameCount; | 
|  | 2559 | int16_t *out = mMixBuffer; | 
|  | 2560 | if (ramp) { | 
|  | 2561 | if (mChannelCount == 1) { | 
|  | 2562 | int32_t d = ((int32_t)leftVol - (int32_t)mLeftVolShort) << 16; | 
|  | 2563 | int32_t vlInc = d / (int32_t)frameCount; | 
|  | 2564 | int32_t vl = ((int32_t)mLeftVolShort << 16); | 
|  | 2565 | do { | 
|  | 2566 | out[0] = clamp16(mul(out[0], vl >> 16) >> 12); | 
|  | 2567 | out++; | 
|  | 2568 | vl += vlInc; | 
|  | 2569 | } while (--frameCount); | 
|  | 2570 |  | 
|  | 2571 | } else { | 
|  | 2572 | int32_t d = ((int32_t)leftVol - (int32_t)mLeftVolShort) << 16; | 
|  | 2573 | int32_t vlInc = d / (int32_t)frameCount; | 
|  | 2574 | d = ((int32_t)rightVol - (int32_t)mRightVolShort) << 16; | 
|  | 2575 | int32_t vrInc = d / (int32_t)frameCount; | 
|  | 2576 | int32_t vl = ((int32_t)mLeftVolShort << 16); | 
|  | 2577 | int32_t vr = ((int32_t)mRightVolShort << 16); | 
|  | 2578 | do { | 
|  | 2579 | out[0] = clamp16(mul(out[0], vl >> 16) >> 12); | 
|  | 2580 | out[1] = clamp16(mul(out[1], vr >> 16) >> 12); | 
|  | 2581 | out += 2; | 
|  | 2582 | vl += vlInc; | 
|  | 2583 | vr += vrInc; | 
|  | 2584 | } while (--frameCount); | 
|  | 2585 | } | 
|  | 2586 | } else { | 
|  | 2587 | if (mChannelCount == 1) { | 
|  | 2588 | do { | 
|  | 2589 | out[0] = clamp16(mul(out[0], leftVol) >> 12); | 
|  | 2590 | out++; | 
|  | 2591 | } while (--frameCount); | 
|  | 2592 | } else { | 
|  | 2593 | do { | 
|  | 2594 | out[0] = clamp16(mul(out[0], leftVol) >> 12); | 
|  | 2595 | out[1] = clamp16(mul(out[1], rightVol) >> 12); | 
|  | 2596 | out += 2; | 
|  | 2597 | } while (--frameCount); | 
|  | 2598 | } | 
|  | 2599 | } | 
|  | 2600 |  | 
|  | 2601 | // convert back to unsigned 8 bit after volume calculation | 
| Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 2602 | if (mFormat == AUDIO_FORMAT_PCM_8_BIT) { | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2603 | size_t count = mFrameCount * mChannelCount; | 
|  | 2604 | int16_t *src = mMixBuffer; | 
|  | 2605 | uint8_t *dst = (uint8_t *)mMixBuffer; | 
|  | 2606 | while(count--) { | 
|  | 2607 | *dst++ = (uint8_t)(((int32_t)*src++ + (1<<7)) >> 8)^0x80; | 
|  | 2608 | } | 
|  | 2609 | } | 
|  | 2610 |  | 
|  | 2611 | mLeftVolShort = leftVol; | 
|  | 2612 | mRightVolShort = rightVol; | 
|  | 2613 | } | 
|  | 2614 |  | 
|  | 2615 | bool AudioFlinger::DirectOutputThread::threadLoop() | 
|  | 2616 | { | 
|  | 2617 | uint32_t mixerStatus = MIXER_IDLE; | 
|  | 2618 | sp<Track> trackToRemove; | 
|  | 2619 | sp<Track> activeTrack; | 
|  | 2620 | nsecs_t standbyTime = systemTime(); | 
|  | 2621 | int8_t *curBuf; | 
|  | 2622 | size_t mixBufferSize = mFrameCount*mFrameSize; | 
|  | 2623 | uint32_t activeSleepTime = activeSleepTimeUs(); | 
|  | 2624 | uint32_t idleSleepTime = idleSleepTimeUs(); | 
|  | 2625 | uint32_t sleepTime = idleSleepTime; | 
|  | 2626 | // use shorter standby delay as on normal output to release | 
|  | 2627 | // hardware resources as soon as possible | 
|  | 2628 | nsecs_t standbyDelay = microseconds(activeSleepTime*2); | 
|  | 2629 |  | 
| Eric Laurent | feb0db6 | 2011-07-22 09:04:31 -0700 | [diff] [blame] | 2630 | acquireWakeLock(); | 
|  | 2631 |  | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2632 | while (!exitPending()) | 
|  | 2633 | { | 
|  | 2634 | bool rampVolume; | 
|  | 2635 | uint16_t leftVol; | 
|  | 2636 | uint16_t rightVol; | 
|  | 2637 | Vector< sp<EffectChain> > effectChains; | 
|  | 2638 |  | 
|  | 2639 | processConfigEvents(); | 
|  | 2640 |  | 
|  | 2641 | mixerStatus = MIXER_IDLE; | 
|  | 2642 |  | 
|  | 2643 | { // scope for the mLock | 
|  | 2644 |  | 
|  | 2645 | Mutex::Autolock _l(mLock); | 
|  | 2646 |  | 
|  | 2647 | if (checkForNewParameters_l()) { | 
|  | 2648 | mixBufferSize = mFrameCount*mFrameSize; | 
|  | 2649 | activeSleepTime = activeSleepTimeUs(); | 
|  | 2650 | idleSleepTime = idleSleepTimeUs(); | 
|  | 2651 | standbyDelay = microseconds(activeSleepTime*2); | 
|  | 2652 | } | 
|  | 2653 |  | 
|  | 2654 | // put audio hardware into standby after short delay | 
| Glenn Kasten | f6b1678 | 2011-12-15 09:51:17 -0800 | [diff] [blame] | 2655 | if (CC_UNLIKELY((!mActiveTracks.size() && systemTime() > standbyTime) || | 
|  | 2656 | mSuspended)) { | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2657 | // wait until we have something to do... | 
|  | 2658 | if (!mStandby) { | 
| Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 2659 | ALOGV("Audio hardware entering standby, mixer %p\n", this); | 
| Dima Zavin | 799a70e | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 2660 | mOutput->stream->common.standby(&mOutput->stream->common); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2661 | mStandby = true; | 
|  | 2662 | mBytesWritten = 0; | 
|  | 2663 | } | 
|  | 2664 |  | 
|  | 2665 | if (!mActiveTracks.size() && mConfigEvents.isEmpty()) { | 
|  | 2666 | // we're about to wait, flush the binder command buffer | 
|  | 2667 | IPCThreadState::self()->flushCommands(); | 
|  | 2668 |  | 
|  | 2669 | if (exitPending()) break; | 
|  | 2670 |  | 
| Eric Laurent | feb0db6 | 2011-07-22 09:04:31 -0700 | [diff] [blame] | 2671 | releaseWakeLock_l(); | 
| Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 2672 | ALOGV("DirectOutputThread %p TID %d going to sleep\n", this, gettid()); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2673 | mWaitWorkCV.wait(mLock); | 
| Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 2674 | ALOGV("DirectOutputThread %p TID %d waking up in active mode\n", this, gettid()); | 
| Eric Laurent | feb0db6 | 2011-07-22 09:04:31 -0700 | [diff] [blame] | 2675 | acquireWakeLock_l(); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2676 |  | 
| Glenn Kasten | f1d4592 | 2012-01-13 15:54:24 -0800 | [diff] [blame] | 2677 | if (!mMasterMute) { | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2678 | char value[PROPERTY_VALUE_MAX]; | 
|  | 2679 | property_get("ro.audio.silent", value, "0"); | 
|  | 2680 | if (atoi(value)) { | 
| Steve Block | b8a8052 | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 2681 | ALOGD("Silence is golden"); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2682 | setMasterMute(true); | 
|  | 2683 | } | 
|  | 2684 | } | 
|  | 2685 |  | 
|  | 2686 | standbyTime = systemTime() + standbyDelay; | 
|  | 2687 | sleepTime = idleSleepTime; | 
|  | 2688 | continue; | 
|  | 2689 | } | 
|  | 2690 | } | 
|  | 2691 |  | 
|  | 2692 | effectChains = mEffectChains; | 
|  | 2693 |  | 
|  | 2694 | // find out which tracks need to be processed | 
|  | 2695 | if (mActiveTracks.size() != 0) { | 
|  | 2696 | sp<Track> t = mActiveTracks[0].promote(); | 
|  | 2697 | if (t == 0) continue; | 
|  | 2698 |  | 
|  | 2699 | Track* const track = t.get(); | 
|  | 2700 | audio_track_cblk_t* cblk = track->cblk(); | 
|  | 2701 |  | 
|  | 2702 | // The first time a track is added we wait | 
|  | 2703 | // for all its buffers to be filled before processing it | 
| Eric Laurent | af59ce2 | 2010-10-05 14:41:42 -0700 | [diff] [blame] | 2704 | if (cblk->framesReady() && track->isReady() && | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2705 | !track->isPaused() && !track->isTerminated()) | 
|  | 2706 | { | 
| Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 2707 | //ALOGV("track %d u=%08x, s=%08x [OK]", track->name(), cblk->user, cblk->server); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2708 |  | 
|  | 2709 | if (track->mFillingUpStatus == Track::FS_FILLED) { | 
|  | 2710 | track->mFillingUpStatus = Track::FS_ACTIVE; | 
|  | 2711 | mLeftVolFloat = mRightVolFloat = 0; | 
|  | 2712 | mLeftVolShort = mRightVolShort = 0; | 
|  | 2713 | if (track->mState == TrackBase::RESUMING) { | 
|  | 2714 | track->mState = TrackBase::ACTIVE; | 
|  | 2715 | rampVolume = true; | 
|  | 2716 | } | 
|  | 2717 | } else if (cblk->server != 0) { | 
|  | 2718 | // If the track is stopped before the first frame was mixed, | 
|  | 2719 | // do not apply ramp | 
|  | 2720 | rampVolume = true; | 
|  | 2721 | } | 
|  | 2722 | // compute volume for this track | 
|  | 2723 | float left, right; | 
|  | 2724 | if (track->isMuted() || mMasterMute || track->isPausing() || | 
|  | 2725 | mStreamTypes[track->type()].mute) { | 
|  | 2726 | left = right = 0; | 
|  | 2727 | if (track->isPausing()) { | 
|  | 2728 | track->setPaused(); | 
|  | 2729 | } | 
|  | 2730 | } else { | 
|  | 2731 | float typeVolume = mStreamTypes[track->type()].volume; | 
|  | 2732 | float v = mMasterVolume * typeVolume; | 
| Glenn Kasten | b1cf75c | 2012-01-17 12:20:54 -0800 | [diff] [blame] | 2733 | uint32_t vlr = cblk->volumeLR; | 
|  | 2734 | float v_clamped = v * (vlr & 0xFFFF); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2735 | if (v_clamped > MAX_GAIN) v_clamped = MAX_GAIN; | 
|  | 2736 | left = v_clamped/MAX_GAIN; | 
| Glenn Kasten | b1cf75c | 2012-01-17 12:20:54 -0800 | [diff] [blame] | 2737 | v_clamped = v * (vlr >> 16); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2738 | if (v_clamped > MAX_GAIN) v_clamped = MAX_GAIN; | 
|  | 2739 | right = v_clamped/MAX_GAIN; | 
|  | 2740 | } | 
|  | 2741 |  | 
|  | 2742 | if (left != mLeftVolFloat || right != mRightVolFloat) { | 
|  | 2743 | mLeftVolFloat = left; | 
|  | 2744 | mRightVolFloat = right; | 
|  | 2745 |  | 
|  | 2746 | // If audio HAL implements volume control, | 
|  | 2747 | // force software volume to nominal value | 
| Dima Zavin | 799a70e | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 2748 | if (mOutput->stream->set_volume(mOutput->stream, left, right) == NO_ERROR) { | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2749 | left = 1.0f; | 
|  | 2750 | right = 1.0f; | 
|  | 2751 | } | 
|  | 2752 |  | 
|  | 2753 | // Convert volumes from float to 8.24 | 
|  | 2754 | uint32_t vl = (uint32_t)(left * (1 << 24)); | 
|  | 2755 | uint32_t vr = (uint32_t)(right * (1 << 24)); | 
|  | 2756 |  | 
|  | 2757 | // Delegate volume control to effect in track effect chain if needed | 
|  | 2758 | // only one effect chain can be present on DirectOutputThread, so if | 
|  | 2759 | // there is one, the track is connected to it | 
|  | 2760 | if (!effectChains.isEmpty()) { | 
| Eric Laurent | e0aed6d | 2010-09-10 17:44:44 -0700 | [diff] [blame] | 2761 | // Do not ramp volume if volume is controlled by effect | 
| Eric Laurent | cab1124 | 2010-07-15 12:50:15 -0700 | [diff] [blame] | 2762 | if(effectChains[0]->setVolume_l(&vl, &vr)) { | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2763 | rampVolume = false; | 
|  | 2764 | } | 
|  | 2765 | } | 
|  | 2766 |  | 
|  | 2767 | // Convert volumes from 8.24 to 4.12 format | 
|  | 2768 | uint32_t v_clamped = (vl + (1 << 11)) >> 12; | 
|  | 2769 | if (v_clamped > MAX_GAIN_INT) v_clamped = MAX_GAIN_INT; | 
|  | 2770 | leftVol = (uint16_t)v_clamped; | 
|  | 2771 | v_clamped = (vr + (1 << 11)) >> 12; | 
|  | 2772 | if (v_clamped > MAX_GAIN_INT) v_clamped = MAX_GAIN_INT; | 
|  | 2773 | rightVol = (uint16_t)v_clamped; | 
|  | 2774 | } else { | 
|  | 2775 | leftVol = mLeftVolShort; | 
|  | 2776 | rightVol = mRightVolShort; | 
|  | 2777 | rampVolume = false; | 
|  | 2778 | } | 
|  | 2779 |  | 
|  | 2780 | // reset retry count | 
|  | 2781 | track->mRetryCount = kMaxTrackRetriesDirect; | 
|  | 2782 | activeTrack = t; | 
|  | 2783 | mixerStatus = MIXER_TRACKS_READY; | 
|  | 2784 | } else { | 
| Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 2785 | //ALOGV("track %d u=%08x, s=%08x [NOT READY]", track->name(), cblk->user, cblk->server); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2786 | if (track->isStopped()) { | 
|  | 2787 | track->reset(); | 
|  | 2788 | } | 
|  | 2789 | if (track->isTerminated() || track->isStopped() || track->isPaused()) { | 
|  | 2790 | // We have consumed all the buffers of this track. | 
|  | 2791 | // Remove it from the list of active tracks. | 
|  | 2792 | trackToRemove = track; | 
|  | 2793 | } else { | 
|  | 2794 | // No buffers for this track. Give it a few chances to | 
|  | 2795 | // fill a buffer, then remove it from active list. | 
|  | 2796 | if (--(track->mRetryCount) <= 0) { | 
| Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 2797 | ALOGV("BUFFER TIMEOUT: remove(%d) from active list", track->name()); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2798 | trackToRemove = track; | 
|  | 2799 | } else { | 
|  | 2800 | mixerStatus = MIXER_TRACKS_ENABLED; | 
|  | 2801 | } | 
|  | 2802 | } | 
|  | 2803 | } | 
|  | 2804 | } | 
|  | 2805 |  | 
|  | 2806 | // remove all the tracks that need to be... | 
| Glenn Kasten | f6b1678 | 2011-12-15 09:51:17 -0800 | [diff] [blame] | 2807 | if (CC_UNLIKELY(trackToRemove != 0)) { | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2808 | mActiveTracks.remove(trackToRemove); | 
|  | 2809 | if (!effectChains.isEmpty()) { | 
| Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 2810 | ALOGV("stopping track on chain %p for session Id: %d", effectChains[0].get(), | 
| Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 2811 | trackToRemove->sessionId()); | 
| Eric Laurent | b469b94 | 2011-05-09 12:09:06 -0700 | [diff] [blame] | 2812 | effectChains[0]->decActiveTrackCnt(); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2813 | } | 
|  | 2814 | if (trackToRemove->isTerminated()) { | 
| Eric Laurent | b469b94 | 2011-05-09 12:09:06 -0700 | [diff] [blame] | 2815 | removeTrack_l(trackToRemove); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2816 | } | 
|  | 2817 | } | 
|  | 2818 |  | 
| Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 2819 | lockEffectChains_l(effectChains); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2820 | } | 
|  | 2821 |  | 
| Glenn Kasten | f6b1678 | 2011-12-15 09:51:17 -0800 | [diff] [blame] | 2822 | if (CC_LIKELY(mixerStatus == MIXER_TRACKS_READY)) { | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2823 | AudioBufferProvider::Buffer buffer; | 
|  | 2824 | size_t frameCount = mFrameCount; | 
|  | 2825 | curBuf = (int8_t *)mMixBuffer; | 
|  | 2826 | // output audio to hardware | 
|  | 2827 | while (frameCount) { | 
|  | 2828 | buffer.frameCount = frameCount; | 
|  | 2829 | activeTrack->getNextBuffer(&buffer); | 
| Glenn Kasten | f6b1678 | 2011-12-15 09:51:17 -0800 | [diff] [blame] | 2830 | if (CC_UNLIKELY(buffer.raw == NULL)) { | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2831 | memset(curBuf, 0, frameCount * mFrameSize); | 
|  | 2832 | break; | 
|  | 2833 | } | 
|  | 2834 | memcpy(curBuf, buffer.raw, buffer.frameCount * mFrameSize); | 
|  | 2835 | frameCount -= buffer.frameCount; | 
|  | 2836 | curBuf += buffer.frameCount * mFrameSize; | 
|  | 2837 | activeTrack->releaseBuffer(&buffer); | 
|  | 2838 | } | 
|  | 2839 | sleepTime = 0; | 
|  | 2840 | standbyTime = systemTime() + standbyDelay; | 
|  | 2841 | } else { | 
|  | 2842 | if (sleepTime == 0) { | 
|  | 2843 | if (mixerStatus == MIXER_TRACKS_ENABLED) { | 
|  | 2844 | sleepTime = activeSleepTime; | 
|  | 2845 | } else { | 
|  | 2846 | sleepTime = idleSleepTime; | 
|  | 2847 | } | 
| Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 2848 | } else if (mBytesWritten != 0 && audio_is_linear_pcm(mFormat)) { | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2849 | memset (mMixBuffer, 0, mFrameCount * mFrameSize); | 
|  | 2850 | sleepTime = 0; | 
|  | 2851 | } | 
|  | 2852 | } | 
|  | 2853 |  | 
|  | 2854 | if (mSuspended) { | 
| Eric Laurent | 25cbe0e | 2010-08-18 18:13:17 -0700 | [diff] [blame] | 2855 | sleepTime = suspendSleepTimeUs(); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2856 | } | 
|  | 2857 | // sleepTime == 0 means we must write to audio hardware | 
|  | 2858 | if (sleepTime == 0) { | 
|  | 2859 | if (mixerStatus == MIXER_TRACKS_READY) { | 
|  | 2860 | applyVolume(leftVol, rightVol, rampVolume); | 
|  | 2861 | } | 
|  | 2862 | for (size_t i = 0; i < effectChains.size(); i ++) { | 
|  | 2863 | effectChains[i]->process_l(); | 
|  | 2864 | } | 
| Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 2865 | unlockEffectChains(effectChains); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2866 |  | 
|  | 2867 | mLastWriteTime = systemTime(); | 
|  | 2868 | mInWrite = true; | 
|  | 2869 | mBytesWritten += mixBufferSize; | 
| Dima Zavin | 799a70e | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 2870 | int bytesWritten = (int)mOutput->stream->write(mOutput->stream, mMixBuffer, mixBufferSize); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2871 | if (bytesWritten < 0) mBytesWritten -= mixBufferSize; | 
|  | 2872 | mNumWrites++; | 
|  | 2873 | mInWrite = false; | 
|  | 2874 | mStandby = false; | 
|  | 2875 | } else { | 
| Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 2876 | unlockEffectChains(effectChains); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2877 | usleep(sleepTime); | 
|  | 2878 | } | 
|  | 2879 |  | 
|  | 2880 | // finally let go of removed track, without the lock held | 
|  | 2881 | // since we can't guarantee the destructors won't acquire that | 
|  | 2882 | // same lock. | 
|  | 2883 | trackToRemove.clear(); | 
|  | 2884 | activeTrack.clear(); | 
|  | 2885 |  | 
|  | 2886 | // Effect chains will be actually deleted here if they were removed from | 
|  | 2887 | // mEffectChains list during mixing or effects processing | 
|  | 2888 | effectChains.clear(); | 
|  | 2889 | } | 
|  | 2890 |  | 
|  | 2891 | if (!mStandby) { | 
| Dima Zavin | 799a70e | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 2892 | mOutput->stream->common.standby(&mOutput->stream->common); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2893 | } | 
|  | 2894 |  | 
| Eric Laurent | feb0db6 | 2011-07-22 09:04:31 -0700 | [diff] [blame] | 2895 | releaseWakeLock(); | 
|  | 2896 |  | 
| Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 2897 | ALOGV("DirectOutputThread %p exiting", this); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2898 | return false; | 
|  | 2899 | } | 
|  | 2900 |  | 
|  | 2901 | // getTrackName_l() must be called with ThreadBase::mLock held | 
|  | 2902 | int AudioFlinger::DirectOutputThread::getTrackName_l() | 
|  | 2903 | { | 
|  | 2904 | return 0; | 
|  | 2905 | } | 
|  | 2906 |  | 
|  | 2907 | // deleteTrackName_l() must be called with ThreadBase::mLock held | 
|  | 2908 | void AudioFlinger::DirectOutputThread::deleteTrackName_l(int name) | 
|  | 2909 | { | 
|  | 2910 | } | 
|  | 2911 |  | 
|  | 2912 | // checkForNewParameters_l() must be called with ThreadBase::mLock held | 
|  | 2913 | bool AudioFlinger::DirectOutputThread::checkForNewParameters_l() | 
|  | 2914 | { | 
|  | 2915 | bool reconfig = false; | 
|  | 2916 |  | 
|  | 2917 | while (!mNewParameters.isEmpty()) { | 
|  | 2918 | status_t status = NO_ERROR; | 
|  | 2919 | String8 keyValuePair = mNewParameters[0]; | 
|  | 2920 | AudioParameter param = AudioParameter(keyValuePair); | 
|  | 2921 | int value; | 
|  | 2922 |  | 
|  | 2923 | if (param.getInt(String8(AudioParameter::keyFrameCount), value) == NO_ERROR) { | 
|  | 2924 | // do not accept frame count changes if tracks are open as the track buffer | 
|  | 2925 | // size depends on frame count and correct behavior would not be garantied | 
|  | 2926 | // if frame count is changed after track creation | 
|  | 2927 | if (!mTracks.isEmpty()) { | 
|  | 2928 | status = INVALID_OPERATION; | 
|  | 2929 | } else { | 
|  | 2930 | reconfig = true; | 
|  | 2931 | } | 
|  | 2932 | } | 
|  | 2933 | if (status == NO_ERROR) { | 
| Dima Zavin | 799a70e | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 2934 | status = mOutput->stream->common.set_parameters(&mOutput->stream->common, | 
| Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 2935 | keyValuePair.string()); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2936 | if (!mStandby && status == INVALID_OPERATION) { | 
| Dima Zavin | 799a70e | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 2937 | mOutput->stream->common.standby(&mOutput->stream->common); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2938 | mStandby = true; | 
|  | 2939 | mBytesWritten = 0; | 
| Dima Zavin | 799a70e | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 2940 | status = mOutput->stream->common.set_parameters(&mOutput->stream->common, | 
| Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 2941 | keyValuePair.string()); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2942 | } | 
|  | 2943 | if (status == NO_ERROR && reconfig) { | 
|  | 2944 | readOutputParameters(); | 
|  | 2945 | sendConfigEvent_l(AudioSystem::OUTPUT_CONFIG_CHANGED); | 
|  | 2946 | } | 
|  | 2947 | } | 
|  | 2948 |  | 
|  | 2949 | mNewParameters.removeAt(0); | 
|  | 2950 |  | 
|  | 2951 | mParamStatus = status; | 
|  | 2952 | mParamCond.signal(); | 
| Eric Laurent | 60cd0a0 | 2011-09-13 11:40:21 -0700 | [diff] [blame] | 2953 | // wait for condition with time out in case the thread calling ThreadBase::setParameters() | 
|  | 2954 | // already timed out waiting for the status and will never signal the condition. | 
| Glenn Kasten | 7dede87 | 2011-12-13 11:04:14 -0800 | [diff] [blame] | 2955 | mWaitWorkCV.waitRelative(mLock, kSetParametersTimeoutNs); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2956 | } | 
|  | 2957 | return reconfig; | 
|  | 2958 | } | 
|  | 2959 |  | 
|  | 2960 | uint32_t AudioFlinger::DirectOutputThread::activeSleepTimeUs() | 
|  | 2961 | { | 
|  | 2962 | uint32_t time; | 
| Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 2963 | if (audio_is_linear_pcm(mFormat)) { | 
| Eric Laurent | 162b40b | 2011-12-05 09:47:19 -0800 | [diff] [blame] | 2964 | time = PlaybackThread::activeSleepTimeUs(); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2965 | } else { | 
|  | 2966 | time = 10000; | 
|  | 2967 | } | 
|  | 2968 | return time; | 
|  | 2969 | } | 
|  | 2970 |  | 
|  | 2971 | uint32_t AudioFlinger::DirectOutputThread::idleSleepTimeUs() | 
|  | 2972 | { | 
|  | 2973 | uint32_t time; | 
| Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 2974 | if (audio_is_linear_pcm(mFormat)) { | 
| Eric Laurent | 60e1824 | 2010-07-29 06:50:24 -0700 | [diff] [blame] | 2975 | time = (uint32_t)(((mFrameCount * 1000) / mSampleRate) * 1000) / 2; | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2976 | } else { | 
|  | 2977 | time = 10000; | 
|  | 2978 | } | 
|  | 2979 | return time; | 
|  | 2980 | } | 
|  | 2981 |  | 
| Eric Laurent | 25cbe0e | 2010-08-18 18:13:17 -0700 | [diff] [blame] | 2982 | uint32_t AudioFlinger::DirectOutputThread::suspendSleepTimeUs() | 
|  | 2983 | { | 
|  | 2984 | uint32_t time; | 
| Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 2985 | if (audio_is_linear_pcm(mFormat)) { | 
| Eric Laurent | 25cbe0e | 2010-08-18 18:13:17 -0700 | [diff] [blame] | 2986 | time = (uint32_t)(((mFrameCount * 1000) / mSampleRate) * 1000); | 
|  | 2987 | } else { | 
|  | 2988 | time = 10000; | 
|  | 2989 | } | 
|  | 2990 | return time; | 
|  | 2991 | } | 
|  | 2992 |  | 
|  | 2993 |  | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2994 | // ---------------------------------------------------------------------------- | 
|  | 2995 |  | 
| Glenn Kasten | 23bb8be | 2012-01-26 10:38:26 -0800 | [diff] [blame^] | 2996 | AudioFlinger::DuplicatingThread::DuplicatingThread(const sp<AudioFlinger>& audioFlinger, | 
|  | 2997 | AudioFlinger::MixerThread* mainThread, int id) | 
|  | 2998 | :   MixerThread(audioFlinger, mainThread->getOutput(), id, mainThread->device(), DUPLICATING), | 
|  | 2999 | mWaitTimeMs(UINT_MAX) | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3000 | { | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3001 | addOutputTrack(mainThread); | 
|  | 3002 | } | 
|  | 3003 |  | 
|  | 3004 | AudioFlinger::DuplicatingThread::~DuplicatingThread() | 
|  | 3005 | { | 
|  | 3006 | for (size_t i = 0; i < mOutputTracks.size(); i++) { | 
|  | 3007 | mOutputTracks[i]->destroy(); | 
|  | 3008 | } | 
|  | 3009 | mOutputTracks.clear(); | 
|  | 3010 | } | 
|  | 3011 |  | 
|  | 3012 | bool AudioFlinger::DuplicatingThread::threadLoop() | 
|  | 3013 | { | 
|  | 3014 | Vector< sp<Track> > tracksToRemove; | 
|  | 3015 | uint32_t mixerStatus = MIXER_IDLE; | 
|  | 3016 | nsecs_t standbyTime = systemTime(); | 
|  | 3017 | size_t mixBufferSize = mFrameCount*mFrameSize; | 
|  | 3018 | SortedVector< sp<OutputTrack> > outputTracks; | 
|  | 3019 | uint32_t writeFrames = 0; | 
|  | 3020 | uint32_t activeSleepTime = activeSleepTimeUs(); | 
|  | 3021 | uint32_t idleSleepTime = idleSleepTimeUs(); | 
|  | 3022 | uint32_t sleepTime = idleSleepTime; | 
|  | 3023 | Vector< sp<EffectChain> > effectChains; | 
|  | 3024 |  | 
| Eric Laurent | feb0db6 | 2011-07-22 09:04:31 -0700 | [diff] [blame] | 3025 | acquireWakeLock(); | 
|  | 3026 |  | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3027 | while (!exitPending()) | 
|  | 3028 | { | 
|  | 3029 | processConfigEvents(); | 
|  | 3030 |  | 
|  | 3031 | mixerStatus = MIXER_IDLE; | 
|  | 3032 | { // scope for the mLock | 
|  | 3033 |  | 
|  | 3034 | Mutex::Autolock _l(mLock); | 
|  | 3035 |  | 
|  | 3036 | if (checkForNewParameters_l()) { | 
|  | 3037 | mixBufferSize = mFrameCount*mFrameSize; | 
|  | 3038 | updateWaitTime(); | 
|  | 3039 | activeSleepTime = activeSleepTimeUs(); | 
|  | 3040 | idleSleepTime = idleSleepTimeUs(); | 
|  | 3041 | } | 
|  | 3042 |  | 
|  | 3043 | const SortedVector< wp<Track> >& activeTracks = mActiveTracks; | 
|  | 3044 |  | 
|  | 3045 | for (size_t i = 0; i < mOutputTracks.size(); i++) { | 
|  | 3046 | outputTracks.add(mOutputTracks[i]); | 
|  | 3047 | } | 
|  | 3048 |  | 
|  | 3049 | // put audio hardware into standby after short delay | 
| Glenn Kasten | f6b1678 | 2011-12-15 09:51:17 -0800 | [diff] [blame] | 3050 | if (CC_UNLIKELY((!activeTracks.size() && systemTime() > standbyTime) || | 
|  | 3051 | mSuspended)) { | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3052 | if (!mStandby) { | 
|  | 3053 | for (size_t i = 0; i < outputTracks.size(); i++) { | 
|  | 3054 | outputTracks[i]->stop(); | 
|  | 3055 | } | 
|  | 3056 | mStandby = true; | 
|  | 3057 | mBytesWritten = 0; | 
|  | 3058 | } | 
|  | 3059 |  | 
|  | 3060 | if (!activeTracks.size() && mConfigEvents.isEmpty()) { | 
|  | 3061 | // we're about to wait, flush the binder command buffer | 
|  | 3062 | IPCThreadState::self()->flushCommands(); | 
|  | 3063 | outputTracks.clear(); | 
|  | 3064 |  | 
|  | 3065 | if (exitPending()) break; | 
|  | 3066 |  | 
| Eric Laurent | feb0db6 | 2011-07-22 09:04:31 -0700 | [diff] [blame] | 3067 | releaseWakeLock_l(); | 
| Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 3068 | ALOGV("DuplicatingThread %p TID %d going to sleep\n", this, gettid()); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3069 | mWaitWorkCV.wait(mLock); | 
| Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 3070 | ALOGV("DuplicatingThread %p TID %d waking up\n", this, gettid()); | 
| Eric Laurent | feb0db6 | 2011-07-22 09:04:31 -0700 | [diff] [blame] | 3071 | acquireWakeLock_l(); | 
|  | 3072 |  | 
| Eric Laurent | 2774144 | 2012-01-17 19:20:12 -0800 | [diff] [blame] | 3073 | mPrevMixerStatus = MIXER_IDLE; | 
| Glenn Kasten | f1d4592 | 2012-01-13 15:54:24 -0800 | [diff] [blame] | 3074 | if (!mMasterMute) { | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3075 | char value[PROPERTY_VALUE_MAX]; | 
|  | 3076 | property_get("ro.audio.silent", value, "0"); | 
|  | 3077 | if (atoi(value)) { | 
| Steve Block | b8a8052 | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 3078 | ALOGD("Silence is golden"); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3079 | setMasterMute(true); | 
|  | 3080 | } | 
|  | 3081 | } | 
|  | 3082 |  | 
|  | 3083 | standbyTime = systemTime() + kStandbyTimeInNsecs; | 
|  | 3084 | sleepTime = idleSleepTime; | 
|  | 3085 | continue; | 
|  | 3086 | } | 
|  | 3087 | } | 
|  | 3088 |  | 
|  | 3089 | mixerStatus = prepareTracks_l(activeTracks, &tracksToRemove); | 
|  | 3090 |  | 
|  | 3091 | // prevent any changes in effect chain list and in each effect chain | 
|  | 3092 | // during mixing and effect process as the audio buffers could be deleted | 
|  | 3093 | // or modified if an effect is created or deleted | 
| Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 3094 | lockEffectChains_l(effectChains); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3095 | } | 
|  | 3096 |  | 
| Glenn Kasten | f6b1678 | 2011-12-15 09:51:17 -0800 | [diff] [blame] | 3097 | if (CC_LIKELY(mixerStatus == MIXER_TRACKS_READY)) { | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3098 | // mix buffers... | 
|  | 3099 | if (outputsReady(outputTracks)) { | 
|  | 3100 | mAudioMixer->process(); | 
|  | 3101 | } else { | 
|  | 3102 | memset(mMixBuffer, 0, mixBufferSize); | 
|  | 3103 | } | 
|  | 3104 | sleepTime = 0; | 
|  | 3105 | writeFrames = mFrameCount; | 
|  | 3106 | } else { | 
|  | 3107 | if (sleepTime == 0) { | 
|  | 3108 | if (mixerStatus == MIXER_TRACKS_ENABLED) { | 
|  | 3109 | sleepTime = activeSleepTime; | 
|  | 3110 | } else { | 
|  | 3111 | sleepTime = idleSleepTime; | 
|  | 3112 | } | 
|  | 3113 | } else if (mBytesWritten != 0) { | 
|  | 3114 | // flush remaining overflow buffers in output tracks | 
|  | 3115 | for (size_t i = 0; i < outputTracks.size(); i++) { | 
|  | 3116 | if (outputTracks[i]->isActive()) { | 
|  | 3117 | sleepTime = 0; | 
|  | 3118 | writeFrames = 0; | 
|  | 3119 | memset(mMixBuffer, 0, mixBufferSize); | 
|  | 3120 | break; | 
|  | 3121 | } | 
|  | 3122 | } | 
|  | 3123 | } | 
|  | 3124 | } | 
|  | 3125 |  | 
|  | 3126 | if (mSuspended) { | 
| Eric Laurent | 25cbe0e | 2010-08-18 18:13:17 -0700 | [diff] [blame] | 3127 | sleepTime = suspendSleepTimeUs(); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3128 | } | 
|  | 3129 | // sleepTime == 0 means we must write to audio hardware | 
|  | 3130 | if (sleepTime == 0) { | 
|  | 3131 | for (size_t i = 0; i < effectChains.size(); i ++) { | 
|  | 3132 | effectChains[i]->process_l(); | 
|  | 3133 | } | 
|  | 3134 | // enable changes in effect chain | 
| Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 3135 | unlockEffectChains(effectChains); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3136 |  | 
|  | 3137 | standbyTime = systemTime() + kStandbyTimeInNsecs; | 
|  | 3138 | for (size_t i = 0; i < outputTracks.size(); i++) { | 
|  | 3139 | outputTracks[i]->write(mMixBuffer, writeFrames); | 
|  | 3140 | } | 
|  | 3141 | mStandby = false; | 
|  | 3142 | mBytesWritten += mixBufferSize; | 
|  | 3143 | } else { | 
|  | 3144 | // enable changes in effect chain | 
| Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 3145 | unlockEffectChains(effectChains); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3146 | usleep(sleepTime); | 
|  | 3147 | } | 
|  | 3148 |  | 
|  | 3149 | // finally let go of all our tracks, without the lock held | 
|  | 3150 | // since we can't guarantee the destructors won't acquire that | 
|  | 3151 | // same lock. | 
|  | 3152 | tracksToRemove.clear(); | 
|  | 3153 | outputTracks.clear(); | 
|  | 3154 |  | 
|  | 3155 | // Effect chains will be actually deleted here if they were removed from | 
|  | 3156 | // mEffectChains list during mixing or effects processing | 
|  | 3157 | effectChains.clear(); | 
|  | 3158 | } | 
|  | 3159 |  | 
| Eric Laurent | feb0db6 | 2011-07-22 09:04:31 -0700 | [diff] [blame] | 3160 | releaseWakeLock(); | 
|  | 3161 |  | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3162 | return false; | 
|  | 3163 | } | 
|  | 3164 |  | 
|  | 3165 | void AudioFlinger::DuplicatingThread::addOutputTrack(MixerThread *thread) | 
|  | 3166 | { | 
|  | 3167 | int frameCount = (3 * mFrameCount * mSampleRate) / thread->sampleRate(); | 
|  | 3168 | OutputTrack *outputTrack = new OutputTrack((ThreadBase *)thread, | 
|  | 3169 | this, | 
|  | 3170 | mSampleRate, | 
|  | 3171 | mFormat, | 
| Jean-Michel Trivi | 0d255b2 | 2011-05-24 15:53:33 -0700 | [diff] [blame] | 3172 | mChannelMask, | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3173 | frameCount); | 
|  | 3174 | if (outputTrack->cblk() != NULL) { | 
| Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 3175 | thread->setStreamVolume(AUDIO_STREAM_CNT, 1.0f); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3176 | mOutputTracks.add(outputTrack); | 
| Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 3177 | ALOGV("addOutputTrack() track %p, on thread %p", outputTrack, thread); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3178 | updateWaitTime(); | 
|  | 3179 | } | 
|  | 3180 | } | 
|  | 3181 |  | 
|  | 3182 | void AudioFlinger::DuplicatingThread::removeOutputTrack(MixerThread *thread) | 
|  | 3183 | { | 
|  | 3184 | Mutex::Autolock _l(mLock); | 
|  | 3185 | for (size_t i = 0; i < mOutputTracks.size(); i++) { | 
|  | 3186 | if (mOutputTracks[i]->thread() == (ThreadBase *)thread) { | 
|  | 3187 | mOutputTracks[i]->destroy(); | 
|  | 3188 | mOutputTracks.removeAt(i); | 
|  | 3189 | updateWaitTime(); | 
|  | 3190 | return; | 
|  | 3191 | } | 
|  | 3192 | } | 
| Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 3193 | ALOGV("removeOutputTrack(): unkonwn thread: %p", thread); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3194 | } | 
|  | 3195 |  | 
|  | 3196 | void AudioFlinger::DuplicatingThread::updateWaitTime() | 
|  | 3197 | { | 
|  | 3198 | mWaitTimeMs = UINT_MAX; | 
|  | 3199 | for (size_t i = 0; i < mOutputTracks.size(); i++) { | 
|  | 3200 | sp<ThreadBase> strong = mOutputTracks[i]->thread().promote(); | 
|  | 3201 | if (strong != NULL) { | 
|  | 3202 | uint32_t waitTimeMs = (strong->frameCount() * 2 * 1000) / strong->sampleRate(); | 
|  | 3203 | if (waitTimeMs < mWaitTimeMs) { | 
|  | 3204 | mWaitTimeMs = waitTimeMs; | 
|  | 3205 | } | 
|  | 3206 | } | 
|  | 3207 | } | 
|  | 3208 | } | 
|  | 3209 |  | 
|  | 3210 |  | 
|  | 3211 | bool AudioFlinger::DuplicatingThread::outputsReady(SortedVector< sp<OutputTrack> > &outputTracks) | 
|  | 3212 | { | 
|  | 3213 | for (size_t i = 0; i < outputTracks.size(); i++) { | 
|  | 3214 | sp <ThreadBase> thread = outputTracks[i]->thread().promote(); | 
|  | 3215 | if (thread == 0) { | 
| Steve Block | 5ff1dd5 | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 3216 | ALOGW("DuplicatingThread::outputsReady() could not promote thread on output track %p", outputTracks[i].get()); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3217 | return false; | 
|  | 3218 | } | 
|  | 3219 | PlaybackThread *playbackThread = (PlaybackThread *)thread.get(); | 
|  | 3220 | if (playbackThread->standby() && !playbackThread->isSuspended()) { | 
| Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 3221 | ALOGV("DuplicatingThread output track %p on thread %p Not Ready", outputTracks[i].get(), thread.get()); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3222 | return false; | 
|  | 3223 | } | 
|  | 3224 | } | 
|  | 3225 | return true; | 
|  | 3226 | } | 
|  | 3227 |  | 
|  | 3228 | uint32_t AudioFlinger::DuplicatingThread::activeSleepTimeUs() | 
|  | 3229 | { | 
|  | 3230 | return (mWaitTimeMs * 1000) / 2; | 
|  | 3231 | } | 
|  | 3232 |  | 
|  | 3233 | // ---------------------------------------------------------------------------- | 
|  | 3234 |  | 
|  | 3235 | // TrackBase constructor must be called with AudioFlinger::mLock held | 
|  | 3236 | AudioFlinger::ThreadBase::TrackBase::TrackBase( | 
|  | 3237 | const wp<ThreadBase>& thread, | 
|  | 3238 | const sp<Client>& client, | 
|  | 3239 | uint32_t sampleRate, | 
| Glenn Kasten | 58f3021 | 2012-01-12 12:27:51 -0800 | [diff] [blame] | 3240 | audio_format_t format, | 
| Jean-Michel Trivi | 0d255b2 | 2011-05-24 15:53:33 -0700 | [diff] [blame] | 3241 | uint32_t channelMask, | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3242 | int frameCount, | 
|  | 3243 | uint32_t flags, | 
|  | 3244 | const sp<IMemory>& sharedBuffer, | 
|  | 3245 | int sessionId) | 
|  | 3246 | :   RefBase(), | 
|  | 3247 | mThread(thread), | 
|  | 3248 | mClient(client), | 
|  | 3249 | mCblk(0), | 
|  | 3250 | mFrameCount(0), | 
|  | 3251 | mState(IDLE), | 
|  | 3252 | mClientTid(-1), | 
|  | 3253 | mFormat(format), | 
|  | 3254 | mFlags(flags & ~SYSTEM_FLAGS_MASK), | 
|  | 3255 | mSessionId(sessionId) | 
|  | 3256 | { | 
| Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 3257 | ALOGV_IF(sharedBuffer != 0, "sharedBuffer: %p, size: %d", sharedBuffer->pointer(), sharedBuffer->size()); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3258 |  | 
| Steve Block | b8a8052 | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 3259 | // ALOGD("Creating track with %d buffers @ %d bytes", bufferCount, bufferSize); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3260 | size_t size = sizeof(audio_track_cblk_t); | 
| Jean-Michel Trivi | 0d255b2 | 2011-05-24 15:53:33 -0700 | [diff] [blame] | 3261 | uint8_t channelCount = popcount(channelMask); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3262 | size_t bufferSize = frameCount*channelCount*sizeof(int16_t); | 
|  | 3263 | if (sharedBuffer == 0) { | 
|  | 3264 | size += bufferSize; | 
|  | 3265 | } | 
|  | 3266 |  | 
|  | 3267 | if (client != NULL) { | 
|  | 3268 | mCblkMemory = client->heap()->allocate(size); | 
|  | 3269 | if (mCblkMemory != 0) { | 
|  | 3270 | mCblk = static_cast<audio_track_cblk_t *>(mCblkMemory->pointer()); | 
|  | 3271 | if (mCblk) { // construct the shared structure in-place. | 
|  | 3272 | new(mCblk) audio_track_cblk_t(); | 
|  | 3273 | // clear all buffers | 
|  | 3274 | mCblk->frameCount = frameCount; | 
|  | 3275 | mCblk->sampleRate = sampleRate; | 
| Jean-Michel Trivi | 0d255b2 | 2011-05-24 15:53:33 -0700 | [diff] [blame] | 3276 | mChannelCount = channelCount; | 
|  | 3277 | mChannelMask = channelMask; | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3278 | if (sharedBuffer == 0) { | 
|  | 3279 | mBuffer = (char*)mCblk + sizeof(audio_track_cblk_t); | 
|  | 3280 | memset(mBuffer, 0, frameCount*channelCount*sizeof(int16_t)); | 
|  | 3281 | // Force underrun condition to avoid false underrun callback until first data is | 
| Eric Laurent | 44d9848 | 2010-09-30 16:12:31 -0700 | [diff] [blame] | 3282 | // written to buffer (other flags are cleared) | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3283 | mCblk->flags = CBLK_UNDERRUN_ON; | 
|  | 3284 | } else { | 
|  | 3285 | mBuffer = sharedBuffer->pointer(); | 
|  | 3286 | } | 
|  | 3287 | mBufferEnd = (uint8_t *)mBuffer + bufferSize; | 
|  | 3288 | } | 
|  | 3289 | } else { | 
| Steve Block | 29357bc | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 3290 | ALOGE("not enough memory for AudioTrack size=%u", size); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3291 | client->heap()->dump("AudioTrack"); | 
|  | 3292 | return; | 
|  | 3293 | } | 
|  | 3294 | } else { | 
|  | 3295 | mCblk = (audio_track_cblk_t *)(new uint8_t[size]); | 
| Glenn Kasten | 4a6f028 | 2012-01-06 15:03:11 -0800 | [diff] [blame] | 3296 | // construct the shared structure in-place. | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3297 | new(mCblk) audio_track_cblk_t(); | 
|  | 3298 | // clear all buffers | 
|  | 3299 | mCblk->frameCount = frameCount; | 
|  | 3300 | mCblk->sampleRate = sampleRate; | 
| Jean-Michel Trivi | 0d255b2 | 2011-05-24 15:53:33 -0700 | [diff] [blame] | 3301 | mChannelCount = channelCount; | 
|  | 3302 | mChannelMask = channelMask; | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3303 | mBuffer = (char*)mCblk + sizeof(audio_track_cblk_t); | 
|  | 3304 | memset(mBuffer, 0, frameCount*channelCount*sizeof(int16_t)); | 
|  | 3305 | // Force underrun condition to avoid false underrun callback until first data is | 
| Eric Laurent | 44d9848 | 2010-09-30 16:12:31 -0700 | [diff] [blame] | 3306 | // written to buffer (other flags are cleared) | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3307 | mCblk->flags = CBLK_UNDERRUN_ON; | 
|  | 3308 | mBufferEnd = (uint8_t *)mBuffer + bufferSize; | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3309 | } | 
|  | 3310 | } | 
|  | 3311 |  | 
|  | 3312 | AudioFlinger::ThreadBase::TrackBase::~TrackBase() | 
|  | 3313 | { | 
|  | 3314 | if (mCblk) { | 
|  | 3315 | mCblk->~audio_track_cblk_t();   // destroy our shared-structure. | 
|  | 3316 | if (mClient == NULL) { | 
|  | 3317 | delete mCblk; | 
|  | 3318 | } | 
|  | 3319 | } | 
|  | 3320 | mCblkMemory.clear();            // and free the shared memory | 
|  | 3321 | if (mClient != NULL) { | 
|  | 3322 | Mutex::Autolock _l(mClient->audioFlinger()->mLock); | 
|  | 3323 | mClient.clear(); | 
|  | 3324 | } | 
|  | 3325 | } | 
|  | 3326 |  | 
|  | 3327 | void AudioFlinger::ThreadBase::TrackBase::releaseBuffer(AudioBufferProvider::Buffer* buffer) | 
|  | 3328 | { | 
| Glenn Kasten | e0feee3 | 2011-12-13 11:53:26 -0800 | [diff] [blame] | 3329 | buffer->raw = NULL; | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3330 | mFrameCount = buffer->frameCount; | 
|  | 3331 | step(); | 
|  | 3332 | buffer->frameCount = 0; | 
|  | 3333 | } | 
|  | 3334 |  | 
|  | 3335 | bool AudioFlinger::ThreadBase::TrackBase::step() { | 
|  | 3336 | bool result; | 
|  | 3337 | audio_track_cblk_t* cblk = this->cblk(); | 
|  | 3338 |  | 
|  | 3339 | result = cblk->stepServer(mFrameCount); | 
|  | 3340 | if (!result) { | 
| Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 3341 | ALOGV("stepServer failed acquiring cblk mutex"); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3342 | mFlags |= STEPSERVER_FAILED; | 
|  | 3343 | } | 
|  | 3344 | return result; | 
|  | 3345 | } | 
|  | 3346 |  | 
|  | 3347 | void AudioFlinger::ThreadBase::TrackBase::reset() { | 
|  | 3348 | audio_track_cblk_t* cblk = this->cblk(); | 
|  | 3349 |  | 
|  | 3350 | cblk->user = 0; | 
|  | 3351 | cblk->server = 0; | 
|  | 3352 | cblk->userBase = 0; | 
|  | 3353 | cblk->serverBase = 0; | 
|  | 3354 | mFlags &= (uint32_t)(~SYSTEM_FLAGS_MASK); | 
| Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 3355 | ALOGV("TrackBase::reset"); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3356 | } | 
|  | 3357 |  | 
|  | 3358 | sp<IMemory> AudioFlinger::ThreadBase::TrackBase::getCblk() const | 
|  | 3359 | { | 
|  | 3360 | return mCblkMemory; | 
|  | 3361 | } | 
|  | 3362 |  | 
|  | 3363 | int AudioFlinger::ThreadBase::TrackBase::sampleRate() const { | 
|  | 3364 | return (int)mCblk->sampleRate; | 
|  | 3365 | } | 
|  | 3366 |  | 
|  | 3367 | int AudioFlinger::ThreadBase::TrackBase::channelCount() const { | 
| Jean-Michel Trivi | 0d255b2 | 2011-05-24 15:53:33 -0700 | [diff] [blame] | 3368 | return (const int)mChannelCount; | 
|  | 3369 | } | 
|  | 3370 |  | 
|  | 3371 | uint32_t AudioFlinger::ThreadBase::TrackBase::channelMask() const { | 
|  | 3372 | return mChannelMask; | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3373 | } | 
|  | 3374 |  | 
|  | 3375 | void* AudioFlinger::ThreadBase::TrackBase::getBuffer(uint32_t offset, uint32_t frames) const { | 
|  | 3376 | audio_track_cblk_t* cblk = this->cblk(); | 
| Glenn Kasten | b998065 | 2012-01-11 09:48:27 -0800 | [diff] [blame] | 3377 | size_t frameSize = cblk->frameSize; | 
|  | 3378 | int8_t *bufferStart = (int8_t *)mBuffer + (offset-cblk->serverBase)*frameSize; | 
|  | 3379 | int8_t *bufferEnd = bufferStart + frames * frameSize; | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3380 |  | 
|  | 3381 | // Check validity of returned pointer in case the track control block would have been corrupted. | 
|  | 3382 | if (bufferStart < mBuffer || bufferStart > bufferEnd || bufferEnd > mBufferEnd || | 
| Glenn Kasten | b998065 | 2012-01-11 09:48:27 -0800 | [diff] [blame] | 3383 | ((unsigned long)bufferStart & (unsigned long)(frameSize - 1))) { | 
| Steve Block | 29357bc | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 3384 | ALOGE("TrackBase::getBuffer buffer out of range:\n    start: %p, end %p , mBuffer %p mBufferEnd %p\n    \ | 
| Jean-Michel Trivi | 0d255b2 | 2011-05-24 15:53:33 -0700 | [diff] [blame] | 3385 | server %d, serverBase %d, user %d, userBase %d", | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3386 | bufferStart, bufferEnd, mBuffer, mBufferEnd, | 
| Jean-Michel Trivi | 0d255b2 | 2011-05-24 15:53:33 -0700 | [diff] [blame] | 3387 | cblk->server, cblk->serverBase, cblk->user, cblk->userBase); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3388 | return 0; | 
|  | 3389 | } | 
|  | 3390 |  | 
|  | 3391 | return bufferStart; | 
|  | 3392 | } | 
|  | 3393 |  | 
|  | 3394 | // ---------------------------------------------------------------------------- | 
|  | 3395 |  | 
|  | 3396 | // Track constructor must be called with AudioFlinger::mLock and ThreadBase::mLock held | 
|  | 3397 | AudioFlinger::PlaybackThread::Track::Track( | 
|  | 3398 | const wp<ThreadBase>& thread, | 
|  | 3399 | const sp<Client>& client, | 
| Glenn Kasten | fff6d71 | 2012-01-12 16:38:12 -0800 | [diff] [blame] | 3400 | audio_stream_type_t streamType, | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3401 | uint32_t sampleRate, | 
| Glenn Kasten | 58f3021 | 2012-01-12 12:27:51 -0800 | [diff] [blame] | 3402 | audio_format_t format, | 
| Jean-Michel Trivi | 0d255b2 | 2011-05-24 15:53:33 -0700 | [diff] [blame] | 3403 | uint32_t channelMask, | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3404 | int frameCount, | 
|  | 3405 | const sp<IMemory>& sharedBuffer, | 
|  | 3406 | int sessionId) | 
| Jean-Michel Trivi | 0d255b2 | 2011-05-24 15:53:33 -0700 | [diff] [blame] | 3407 | :   TrackBase(thread, client, sampleRate, format, channelMask, frameCount, 0, sharedBuffer, sessionId), | 
| Eric Laurent | 8f45bd7 | 2010-08-31 13:50:07 -0700 | [diff] [blame] | 3408 | mMute(false), mSharedBuffer(sharedBuffer), mName(-1), mMainBuffer(NULL), mAuxBuffer(NULL), | 
|  | 3409 | mAuxEffectId(0), mHasVolumeController(false) | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3410 | { | 
|  | 3411 | if (mCblk != NULL) { | 
|  | 3412 | sp<ThreadBase> baseThread = thread.promote(); | 
|  | 3413 | if (baseThread != 0) { | 
|  | 3414 | PlaybackThread *playbackThread = (PlaybackThread *)baseThread.get(); | 
|  | 3415 | mName = playbackThread->getTrackName_l(); | 
|  | 3416 | mMainBuffer = playbackThread->mixBuffer(); | 
|  | 3417 | } | 
| Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 3418 | ALOGV("Track constructor name %d, calling thread %d", mName, IPCThreadState::self()->getCallingPid()); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3419 | if (mName < 0) { | 
| Steve Block | 29357bc | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 3420 | ALOGE("no more track names available"); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3421 | } | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3422 | mStreamType = streamType; | 
|  | 3423 | // NOTE: audio_track_cblk_t::frameSize for 8 bit PCM data is based on a sample size of | 
|  | 3424 | // 16 bit because data is converted to 16 bit before being stored in buffer by AudioTrack | 
| Eric Laurent | edc15ad | 2011-07-21 19:35:01 -0700 | [diff] [blame] | 3425 | mCblk->frameSize = audio_is_linear_pcm(format) ? mChannelCount * sizeof(int16_t) : sizeof(uint8_t); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3426 | } | 
|  | 3427 | } | 
|  | 3428 |  | 
|  | 3429 | AudioFlinger::PlaybackThread::Track::~Track() | 
|  | 3430 | { | 
| Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 3431 | ALOGV("PlaybackThread::Track destructor"); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3432 | sp<ThreadBase> thread = mThread.promote(); | 
|  | 3433 | if (thread != 0) { | 
|  | 3434 | Mutex::Autolock _l(thread->mLock); | 
|  | 3435 | mState = TERMINATED; | 
|  | 3436 | } | 
|  | 3437 | } | 
|  | 3438 |  | 
|  | 3439 | void AudioFlinger::PlaybackThread::Track::destroy() | 
|  | 3440 | { | 
|  | 3441 | // NOTE: destroyTrack_l() can remove a strong reference to this Track | 
|  | 3442 | // by removing it from mTracks vector, so there is a risk that this Tracks's | 
|  | 3443 | // desctructor is called. As the destructor needs to lock mLock, | 
|  | 3444 | // we must acquire a strong reference on this Track before locking mLock | 
|  | 3445 | // here so that the destructor is called only when exiting this function. | 
|  | 3446 | // On the other hand, as long as Track::destroy() is only called by | 
|  | 3447 | // TrackHandle destructor, the TrackHandle still holds a strong ref on | 
|  | 3448 | // this Track with its member mTrack. | 
|  | 3449 | sp<Track> keep(this); | 
|  | 3450 | { // scope for mLock | 
|  | 3451 | sp<ThreadBase> thread = mThread.promote(); | 
|  | 3452 | if (thread != 0) { | 
|  | 3453 | if (!isOutputTrack()) { | 
|  | 3454 | if (mState == ACTIVE || mState == RESUMING) { | 
| Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 3455 | AudioSystem::stopOutput(thread->id(), | 
| Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 3456 | (audio_stream_type_t)mStreamType, | 
| Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 3457 | mSessionId); | 
| Gloria Wang | 9ee159b | 2011-02-24 14:51:45 -0800 | [diff] [blame] | 3458 |  | 
|  | 3459 | // to track the speaker usage | 
|  | 3460 | addBatteryData(IMediaPlayerService::kBatteryDataAudioFlingerStop); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3461 | } | 
|  | 3462 | AudioSystem::releaseOutput(thread->id()); | 
|  | 3463 | } | 
|  | 3464 | Mutex::Autolock _l(thread->mLock); | 
|  | 3465 | PlaybackThread *playbackThread = (PlaybackThread *)thread.get(); | 
|  | 3466 | playbackThread->destroyTrack_l(this); | 
|  | 3467 | } | 
|  | 3468 | } | 
|  | 3469 | } | 
|  | 3470 |  | 
|  | 3471 | void AudioFlinger::PlaybackThread::Track::dump(char* buffer, size_t size) | 
|  | 3472 | { | 
| Glenn Kasten | b1cf75c | 2012-01-17 12:20:54 -0800 | [diff] [blame] | 3473 | uint32_t vlr = mCblk->volumeLR; | 
| Jean-Michel Trivi | 0d255b2 | 2011-05-24 15:53:33 -0700 | [diff] [blame] | 3474 | snprintf(buffer, size, "   %05d %05d %03u %03u 0x%08x %05u   %04u %1d %1d %1d %05u %05u %05u  0x%08x 0x%08x 0x%08x 0x%08x\n", | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3475 | mName - AudioMixer::TRACK0, | 
|  | 3476 | (mClient == NULL) ? getpid() : mClient->pid(), | 
|  | 3477 | mStreamType, | 
|  | 3478 | mFormat, | 
| Jean-Michel Trivi | 0d255b2 | 2011-05-24 15:53:33 -0700 | [diff] [blame] | 3479 | mChannelMask, | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3480 | mSessionId, | 
|  | 3481 | mFrameCount, | 
|  | 3482 | mState, | 
|  | 3483 | mMute, | 
|  | 3484 | mFillingUpStatus, | 
|  | 3485 | mCblk->sampleRate, | 
| Glenn Kasten | b1cf75c | 2012-01-17 12:20:54 -0800 | [diff] [blame] | 3486 | vlr & 0xFFFF, | 
|  | 3487 | vlr >> 16, | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3488 | mCblk->server, | 
|  | 3489 | mCblk->user, | 
|  | 3490 | (int)mMainBuffer, | 
|  | 3491 | (int)mAuxBuffer); | 
|  | 3492 | } | 
|  | 3493 |  | 
|  | 3494 | status_t AudioFlinger::PlaybackThread::Track::getNextBuffer(AudioBufferProvider::Buffer* buffer) | 
|  | 3495 | { | 
|  | 3496 | audio_track_cblk_t* cblk = this->cblk(); | 
|  | 3497 | uint32_t framesReady; | 
|  | 3498 | uint32_t framesReq = buffer->frameCount; | 
|  | 3499 |  | 
|  | 3500 | // Check if last stepServer failed, try to step now | 
|  | 3501 | if (mFlags & TrackBase::STEPSERVER_FAILED) { | 
|  | 3502 | if (!step())  goto getNextBuffer_exit; | 
| Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 3503 | ALOGV("stepServer recovered"); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3504 | mFlags &= ~TrackBase::STEPSERVER_FAILED; | 
|  | 3505 | } | 
|  | 3506 |  | 
|  | 3507 | framesReady = cblk->framesReady(); | 
|  | 3508 |  | 
| Glenn Kasten | f6b1678 | 2011-12-15 09:51:17 -0800 | [diff] [blame] | 3509 | if (CC_LIKELY(framesReady)) { | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3510 | uint32_t s = cblk->server; | 
|  | 3511 | uint32_t bufferEnd = cblk->serverBase + cblk->frameCount; | 
|  | 3512 |  | 
|  | 3513 | bufferEnd = (cblk->loopEnd < bufferEnd) ? cblk->loopEnd : bufferEnd; | 
|  | 3514 | if (framesReq > framesReady) { | 
|  | 3515 | framesReq = framesReady; | 
|  | 3516 | } | 
|  | 3517 | if (s + framesReq > bufferEnd) { | 
|  | 3518 | framesReq = bufferEnd - s; | 
|  | 3519 | } | 
|  | 3520 |  | 
|  | 3521 | buffer->raw = getBuffer(s, framesReq); | 
| Glenn Kasten | e0feee3 | 2011-12-13 11:53:26 -0800 | [diff] [blame] | 3522 | if (buffer->raw == NULL) goto getNextBuffer_exit; | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3523 |  | 
|  | 3524 | buffer->frameCount = framesReq; | 
|  | 3525 | return NO_ERROR; | 
|  | 3526 | } | 
|  | 3527 |  | 
|  | 3528 | getNextBuffer_exit: | 
| Glenn Kasten | e0feee3 | 2011-12-13 11:53:26 -0800 | [diff] [blame] | 3529 | buffer->raw = NULL; | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3530 | buffer->frameCount = 0; | 
| Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 3531 | ALOGV("getNextBuffer() no more data for track %d on thread %p", mName, mThread.unsafe_get()); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3532 | return NOT_ENOUGH_DATA; | 
|  | 3533 | } | 
|  | 3534 |  | 
|  | 3535 | bool AudioFlinger::PlaybackThread::Track::isReady() const { | 
| Eric Laurent | af59ce2 | 2010-10-05 14:41:42 -0700 | [diff] [blame] | 3536 | if (mFillingUpStatus != FS_FILLING || isStopped() || isPausing()) return true; | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3537 |  | 
|  | 3538 | if (mCblk->framesReady() >= mCblk->frameCount || | 
|  | 3539 | (mCblk->flags & CBLK_FORCEREADY_MSK)) { | 
|  | 3540 | mFillingUpStatus = FS_FILLED; | 
| Eric Laurent | 38ccae2 | 2011-03-28 18:37:07 -0700 | [diff] [blame] | 3541 | android_atomic_and(~CBLK_FORCEREADY_MSK, &mCblk->flags); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3542 | return true; | 
|  | 3543 | } | 
|  | 3544 | return false; | 
|  | 3545 | } | 
|  | 3546 |  | 
|  | 3547 | status_t AudioFlinger::PlaybackThread::Track::start() | 
|  | 3548 | { | 
|  | 3549 | status_t status = NO_ERROR; | 
| Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 3550 | ALOGV("start(%d), calling thread %d session %d", | 
| Eric Laurent | f997cab | 2010-07-19 06:24:46 -0700 | [diff] [blame] | 3551 | mName, IPCThreadState::self()->getCallingPid(), mSessionId); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3552 | sp<ThreadBase> thread = mThread.promote(); | 
|  | 3553 | if (thread != 0) { | 
|  | 3554 | Mutex::Autolock _l(thread->mLock); | 
|  | 3555 | int state = mState; | 
|  | 3556 | // here the track could be either new, or restarted | 
|  | 3557 | // in both cases "unstop" the track | 
|  | 3558 | if (mState == PAUSED) { | 
|  | 3559 | mState = TrackBase::RESUMING; | 
| Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 3560 | ALOGV("PAUSED => RESUMING (%d) on thread %p", mName, this); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3561 | } else { | 
|  | 3562 | mState = TrackBase::ACTIVE; | 
| Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 3563 | ALOGV("? => ACTIVE (%d) on thread %p", mName, this); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3564 | } | 
|  | 3565 |  | 
|  | 3566 | if (!isOutputTrack() && state != ACTIVE && state != RESUMING) { | 
|  | 3567 | thread->mLock.unlock(); | 
| Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 3568 | status = AudioSystem::startOutput(thread->id(), | 
| Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 3569 | (audio_stream_type_t)mStreamType, | 
| Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 3570 | mSessionId); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3571 | thread->mLock.lock(); | 
| Gloria Wang | 9ee159b | 2011-02-24 14:51:45 -0800 | [diff] [blame] | 3572 |  | 
|  | 3573 | // to track the speaker usage | 
|  | 3574 | if (status == NO_ERROR) { | 
|  | 3575 | addBatteryData(IMediaPlayerService::kBatteryDataAudioFlingerStart); | 
|  | 3576 | } | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3577 | } | 
|  | 3578 | if (status == NO_ERROR) { | 
|  | 3579 | PlaybackThread *playbackThread = (PlaybackThread *)thread.get(); | 
|  | 3580 | playbackThread->addTrack_l(this); | 
|  | 3581 | } else { | 
|  | 3582 | mState = state; | 
|  | 3583 | } | 
|  | 3584 | } else { | 
|  | 3585 | status = BAD_VALUE; | 
|  | 3586 | } | 
|  | 3587 | return status; | 
|  | 3588 | } | 
|  | 3589 |  | 
|  | 3590 | void AudioFlinger::PlaybackThread::Track::stop() | 
|  | 3591 | { | 
| Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 3592 | ALOGV("stop(%d), calling thread %d", mName, IPCThreadState::self()->getCallingPid()); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3593 | sp<ThreadBase> thread = mThread.promote(); | 
|  | 3594 | if (thread != 0) { | 
|  | 3595 | Mutex::Autolock _l(thread->mLock); | 
|  | 3596 | int state = mState; | 
|  | 3597 | if (mState > STOPPED) { | 
|  | 3598 | mState = STOPPED; | 
|  | 3599 | // If the track is not active (PAUSED and buffers full), flush buffers | 
|  | 3600 | PlaybackThread *playbackThread = (PlaybackThread *)thread.get(); | 
|  | 3601 | if (playbackThread->mActiveTracks.indexOf(this) < 0) { | 
|  | 3602 | reset(); | 
|  | 3603 | } | 
| Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 3604 | ALOGV("(> STOPPED) => STOPPED (%d) on thread %p", mName, playbackThread); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3605 | } | 
|  | 3606 | if (!isOutputTrack() && (state == ACTIVE || state == RESUMING)) { | 
|  | 3607 | thread->mLock.unlock(); | 
| Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 3608 | AudioSystem::stopOutput(thread->id(), | 
| Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 3609 | (audio_stream_type_t)mStreamType, | 
| Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 3610 | mSessionId); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3611 | thread->mLock.lock(); | 
| Gloria Wang | 9ee159b | 2011-02-24 14:51:45 -0800 | [diff] [blame] | 3612 |  | 
|  | 3613 | // to track the speaker usage | 
|  | 3614 | addBatteryData(IMediaPlayerService::kBatteryDataAudioFlingerStop); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3615 | } | 
|  | 3616 | } | 
|  | 3617 | } | 
|  | 3618 |  | 
|  | 3619 | void AudioFlinger::PlaybackThread::Track::pause() | 
|  | 3620 | { | 
| Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 3621 | ALOGV("pause(%d), calling thread %d", mName, IPCThreadState::self()->getCallingPid()); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3622 | sp<ThreadBase> thread = mThread.promote(); | 
|  | 3623 | if (thread != 0) { | 
|  | 3624 | Mutex::Autolock _l(thread->mLock); | 
|  | 3625 | if (mState == ACTIVE || mState == RESUMING) { | 
|  | 3626 | mState = PAUSING; | 
| Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 3627 | ALOGV("ACTIVE/RESUMING => PAUSING (%d) on thread %p", mName, thread.get()); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3628 | if (!isOutputTrack()) { | 
|  | 3629 | thread->mLock.unlock(); | 
| Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 3630 | AudioSystem::stopOutput(thread->id(), | 
| Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 3631 | (audio_stream_type_t)mStreamType, | 
| Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 3632 | mSessionId); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3633 | thread->mLock.lock(); | 
| Gloria Wang | 9ee159b | 2011-02-24 14:51:45 -0800 | [diff] [blame] | 3634 |  | 
|  | 3635 | // to track the speaker usage | 
|  | 3636 | addBatteryData(IMediaPlayerService::kBatteryDataAudioFlingerStop); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3637 | } | 
|  | 3638 | } | 
|  | 3639 | } | 
|  | 3640 | } | 
|  | 3641 |  | 
|  | 3642 | void AudioFlinger::PlaybackThread::Track::flush() | 
|  | 3643 | { | 
| Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 3644 | ALOGV("flush(%d)", mName); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3645 | sp<ThreadBase> thread = mThread.promote(); | 
|  | 3646 | if (thread != 0) { | 
|  | 3647 | Mutex::Autolock _l(thread->mLock); | 
|  | 3648 | if (mState != STOPPED && mState != PAUSED && mState != PAUSING) { | 
|  | 3649 | return; | 
|  | 3650 | } | 
|  | 3651 | // No point remaining in PAUSED state after a flush => go to | 
|  | 3652 | // STOPPED state | 
|  | 3653 | mState = STOPPED; | 
|  | 3654 |  | 
| Eric Laurent | 38ccae2 | 2011-03-28 18:37:07 -0700 | [diff] [blame] | 3655 | // do not reset the track if it is still in the process of being stopped or paused. | 
|  | 3656 | // this will be done by prepareTracks_l() when the track is stopped. | 
|  | 3657 | PlaybackThread *playbackThread = (PlaybackThread *)thread.get(); | 
|  | 3658 | if (playbackThread->mActiveTracks.indexOf(this) < 0) { | 
|  | 3659 | reset(); | 
|  | 3660 | } | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3661 | } | 
|  | 3662 | } | 
|  | 3663 |  | 
|  | 3664 | void AudioFlinger::PlaybackThread::Track::reset() | 
|  | 3665 | { | 
|  | 3666 | // Do not reset twice to avoid discarding data written just after a flush and before | 
|  | 3667 | // the audioflinger thread detects the track is stopped. | 
|  | 3668 | if (!mResetDone) { | 
|  | 3669 | TrackBase::reset(); | 
|  | 3670 | // Force underrun condition to avoid false underrun callback until first data is | 
|  | 3671 | // written to buffer | 
| Eric Laurent | 38ccae2 | 2011-03-28 18:37:07 -0700 | [diff] [blame] | 3672 | android_atomic_and(~CBLK_FORCEREADY_MSK, &mCblk->flags); | 
|  | 3673 | android_atomic_or(CBLK_UNDERRUN_ON, &mCblk->flags); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3674 | mFillingUpStatus = FS_FILLING; | 
|  | 3675 | mResetDone = true; | 
|  | 3676 | } | 
|  | 3677 | } | 
|  | 3678 |  | 
|  | 3679 | void AudioFlinger::PlaybackThread::Track::mute(bool muted) | 
|  | 3680 | { | 
|  | 3681 | mMute = muted; | 
|  | 3682 | } | 
|  | 3683 |  | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3684 | status_t AudioFlinger::PlaybackThread::Track::attachAuxEffect(int EffectId) | 
|  | 3685 | { | 
|  | 3686 | status_t status = DEAD_OBJECT; | 
|  | 3687 | sp<ThreadBase> thread = mThread.promote(); | 
|  | 3688 | if (thread != 0) { | 
|  | 3689 | PlaybackThread *playbackThread = (PlaybackThread *)thread.get(); | 
|  | 3690 | status = playbackThread->attachAuxEffect(this, EffectId); | 
|  | 3691 | } | 
|  | 3692 | return status; | 
|  | 3693 | } | 
|  | 3694 |  | 
|  | 3695 | void AudioFlinger::PlaybackThread::Track::setAuxBuffer(int EffectId, int32_t *buffer) | 
|  | 3696 | { | 
|  | 3697 | mAuxEffectId = EffectId; | 
|  | 3698 | mAuxBuffer = buffer; | 
|  | 3699 | } | 
|  | 3700 |  | 
|  | 3701 | // ---------------------------------------------------------------------------- | 
|  | 3702 |  | 
|  | 3703 | // RecordTrack constructor must be called with AudioFlinger::mLock held | 
|  | 3704 | AudioFlinger::RecordThread::RecordTrack::RecordTrack( | 
|  | 3705 | const wp<ThreadBase>& thread, | 
|  | 3706 | const sp<Client>& client, | 
|  | 3707 | uint32_t sampleRate, | 
| Glenn Kasten | 58f3021 | 2012-01-12 12:27:51 -0800 | [diff] [blame] | 3708 | audio_format_t format, | 
| Jean-Michel Trivi | 0d255b2 | 2011-05-24 15:53:33 -0700 | [diff] [blame] | 3709 | uint32_t channelMask, | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3710 | int frameCount, | 
|  | 3711 | uint32_t flags, | 
|  | 3712 | int sessionId) | 
|  | 3713 | :   TrackBase(thread, client, sampleRate, format, | 
| Jean-Michel Trivi | 0d255b2 | 2011-05-24 15:53:33 -0700 | [diff] [blame] | 3714 | channelMask, frameCount, flags, 0, sessionId), | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3715 | mOverflow(false) | 
|  | 3716 | { | 
|  | 3717 | if (mCblk != NULL) { | 
| Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 3718 | ALOGV("RecordTrack constructor, size %d", (int)mBufferEnd - (int)mBuffer); | 
| Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 3719 | if (format == AUDIO_FORMAT_PCM_16_BIT) { | 
| Jean-Michel Trivi | 0d255b2 | 2011-05-24 15:53:33 -0700 | [diff] [blame] | 3720 | mCblk->frameSize = mChannelCount * sizeof(int16_t); | 
| Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 3721 | } else if (format == AUDIO_FORMAT_PCM_8_BIT) { | 
| Jean-Michel Trivi | 0d255b2 | 2011-05-24 15:53:33 -0700 | [diff] [blame] | 3722 | mCblk->frameSize = mChannelCount * sizeof(int8_t); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3723 | } else { | 
|  | 3724 | mCblk->frameSize = sizeof(int8_t); | 
|  | 3725 | } | 
|  | 3726 | } | 
|  | 3727 | } | 
|  | 3728 |  | 
|  | 3729 | AudioFlinger::RecordThread::RecordTrack::~RecordTrack() | 
|  | 3730 | { | 
|  | 3731 | sp<ThreadBase> thread = mThread.promote(); | 
|  | 3732 | if (thread != 0) { | 
|  | 3733 | AudioSystem::releaseInput(thread->id()); | 
|  | 3734 | } | 
|  | 3735 | } | 
|  | 3736 |  | 
|  | 3737 | status_t AudioFlinger::RecordThread::RecordTrack::getNextBuffer(AudioBufferProvider::Buffer* buffer) | 
|  | 3738 | { | 
|  | 3739 | audio_track_cblk_t* cblk = this->cblk(); | 
|  | 3740 | uint32_t framesAvail; | 
|  | 3741 | uint32_t framesReq = buffer->frameCount; | 
|  | 3742 |  | 
|  | 3743 | // Check if last stepServer failed, try to step now | 
|  | 3744 | if (mFlags & TrackBase::STEPSERVER_FAILED) { | 
|  | 3745 | if (!step()) goto getNextBuffer_exit; | 
| Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 3746 | ALOGV("stepServer recovered"); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3747 | mFlags &= ~TrackBase::STEPSERVER_FAILED; | 
|  | 3748 | } | 
|  | 3749 |  | 
|  | 3750 | framesAvail = cblk->framesAvailable_l(); | 
|  | 3751 |  | 
| Glenn Kasten | f6b1678 | 2011-12-15 09:51:17 -0800 | [diff] [blame] | 3752 | if (CC_LIKELY(framesAvail)) { | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3753 | uint32_t s = cblk->server; | 
|  | 3754 | uint32_t bufferEnd = cblk->serverBase + cblk->frameCount; | 
|  | 3755 |  | 
|  | 3756 | if (framesReq > framesAvail) { | 
|  | 3757 | framesReq = framesAvail; | 
|  | 3758 | } | 
|  | 3759 | if (s + framesReq > bufferEnd) { | 
|  | 3760 | framesReq = bufferEnd - s; | 
|  | 3761 | } | 
|  | 3762 |  | 
|  | 3763 | buffer->raw = getBuffer(s, framesReq); | 
| Glenn Kasten | e0feee3 | 2011-12-13 11:53:26 -0800 | [diff] [blame] | 3764 | if (buffer->raw == NULL) goto getNextBuffer_exit; | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3765 |  | 
|  | 3766 | buffer->frameCount = framesReq; | 
|  | 3767 | return NO_ERROR; | 
|  | 3768 | } | 
|  | 3769 |  | 
|  | 3770 | getNextBuffer_exit: | 
| Glenn Kasten | e0feee3 | 2011-12-13 11:53:26 -0800 | [diff] [blame] | 3771 | buffer->raw = NULL; | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3772 | buffer->frameCount = 0; | 
|  | 3773 | return NOT_ENOUGH_DATA; | 
|  | 3774 | } | 
|  | 3775 |  | 
|  | 3776 | status_t AudioFlinger::RecordThread::RecordTrack::start() | 
|  | 3777 | { | 
|  | 3778 | sp<ThreadBase> thread = mThread.promote(); | 
|  | 3779 | if (thread != 0) { | 
|  | 3780 | RecordThread *recordThread = (RecordThread *)thread.get(); | 
|  | 3781 | return recordThread->start(this); | 
|  | 3782 | } else { | 
|  | 3783 | return BAD_VALUE; | 
|  | 3784 | } | 
|  | 3785 | } | 
|  | 3786 |  | 
|  | 3787 | void AudioFlinger::RecordThread::RecordTrack::stop() | 
|  | 3788 | { | 
|  | 3789 | sp<ThreadBase> thread = mThread.promote(); | 
|  | 3790 | if (thread != 0) { | 
|  | 3791 | RecordThread *recordThread = (RecordThread *)thread.get(); | 
|  | 3792 | recordThread->stop(this); | 
| Eric Laurent | 38ccae2 | 2011-03-28 18:37:07 -0700 | [diff] [blame] | 3793 | TrackBase::reset(); | 
|  | 3794 | // Force overerrun condition to avoid false overrun callback until first data is | 
|  | 3795 | // read from buffer | 
|  | 3796 | android_atomic_or(CBLK_UNDERRUN_ON, &mCblk->flags); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3797 | } | 
|  | 3798 | } | 
|  | 3799 |  | 
|  | 3800 | void AudioFlinger::RecordThread::RecordTrack::dump(char* buffer, size_t size) | 
|  | 3801 | { | 
| Jean-Michel Trivi | 0d255b2 | 2011-05-24 15:53:33 -0700 | [diff] [blame] | 3802 | snprintf(buffer, size, "   %05d %03u 0x%08x %05d   %04u %01d %05u  %08x %08x\n", | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3803 | (mClient == NULL) ? getpid() : mClient->pid(), | 
|  | 3804 | mFormat, | 
| Jean-Michel Trivi | 0d255b2 | 2011-05-24 15:53:33 -0700 | [diff] [blame] | 3805 | mChannelMask, | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3806 | mSessionId, | 
|  | 3807 | mFrameCount, | 
|  | 3808 | mState, | 
|  | 3809 | mCblk->sampleRate, | 
|  | 3810 | mCblk->server, | 
|  | 3811 | mCblk->user); | 
|  | 3812 | } | 
|  | 3813 |  | 
|  | 3814 |  | 
|  | 3815 | // ---------------------------------------------------------------------------- | 
|  | 3816 |  | 
|  | 3817 | AudioFlinger::PlaybackThread::OutputTrack::OutputTrack( | 
|  | 3818 | const wp<ThreadBase>& thread, | 
|  | 3819 | DuplicatingThread *sourceThread, | 
|  | 3820 | uint32_t sampleRate, | 
| Glenn Kasten | 58f3021 | 2012-01-12 12:27:51 -0800 | [diff] [blame] | 3821 | audio_format_t format, | 
| Jean-Michel Trivi | 0d255b2 | 2011-05-24 15:53:33 -0700 | [diff] [blame] | 3822 | uint32_t channelMask, | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3823 | int frameCount) | 
| Jean-Michel Trivi | 0d255b2 | 2011-05-24 15:53:33 -0700 | [diff] [blame] | 3824 | :   Track(thread, NULL, AUDIO_STREAM_CNT, sampleRate, format, channelMask, frameCount, NULL, 0), | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3825 | mActive(false), mSourceThread(sourceThread) | 
|  | 3826 | { | 
|  | 3827 |  | 
|  | 3828 | PlaybackThread *playbackThread = (PlaybackThread *)thread.unsafe_get(); | 
|  | 3829 | if (mCblk != NULL) { | 
|  | 3830 | mCblk->flags |= CBLK_DIRECTION_OUT; | 
|  | 3831 | mCblk->buffers = (char*)mCblk + sizeof(audio_track_cblk_t); | 
| Glenn Kasten | b1cf75c | 2012-01-17 12:20:54 -0800 | [diff] [blame] | 3832 | mCblk->volumeLR = (MAX_GAIN_INT << 16) | MAX_GAIN_INT; | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3833 | mOutBuffer.frameCount = 0; | 
|  | 3834 | playbackThread->mTracks.add(this); | 
| Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 3835 | ALOGV("OutputTrack constructor mCblk %p, mBuffer %p, mCblk->buffers %p, " \ | 
| Jean-Michel Trivi | 0d255b2 | 2011-05-24 15:53:33 -0700 | [diff] [blame] | 3836 | "mCblk->frameCount %d, mCblk->sampleRate %d, mChannelMask 0x%08x mBufferEnd %p", | 
|  | 3837 | mCblk, mBuffer, mCblk->buffers, | 
|  | 3838 | mCblk->frameCount, mCblk->sampleRate, mChannelMask, mBufferEnd); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3839 | } else { | 
| Steve Block | 5ff1dd5 | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 3840 | ALOGW("Error creating output track on thread %p", playbackThread); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3841 | } | 
|  | 3842 | } | 
|  | 3843 |  | 
|  | 3844 | AudioFlinger::PlaybackThread::OutputTrack::~OutputTrack() | 
|  | 3845 | { | 
|  | 3846 | clearBufferQueue(); | 
|  | 3847 | } | 
|  | 3848 |  | 
|  | 3849 | status_t AudioFlinger::PlaybackThread::OutputTrack::start() | 
|  | 3850 | { | 
|  | 3851 | status_t status = Track::start(); | 
|  | 3852 | if (status != NO_ERROR) { | 
|  | 3853 | return status; | 
|  | 3854 | } | 
|  | 3855 |  | 
|  | 3856 | mActive = true; | 
|  | 3857 | mRetryCount = 127; | 
|  | 3858 | return status; | 
|  | 3859 | } | 
|  | 3860 |  | 
|  | 3861 | void AudioFlinger::PlaybackThread::OutputTrack::stop() | 
|  | 3862 | { | 
|  | 3863 | Track::stop(); | 
|  | 3864 | clearBufferQueue(); | 
|  | 3865 | mOutBuffer.frameCount = 0; | 
|  | 3866 | mActive = false; | 
|  | 3867 | } | 
|  | 3868 |  | 
|  | 3869 | bool AudioFlinger::PlaybackThread::OutputTrack::write(int16_t* data, uint32_t frames) | 
|  | 3870 | { | 
|  | 3871 | Buffer *pInBuffer; | 
|  | 3872 | Buffer inBuffer; | 
| Jean-Michel Trivi | 0d255b2 | 2011-05-24 15:53:33 -0700 | [diff] [blame] | 3873 | uint32_t channelCount = mChannelCount; | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3874 | bool outputBufferFull = false; | 
|  | 3875 | inBuffer.frameCount = frames; | 
|  | 3876 | inBuffer.i16 = data; | 
|  | 3877 |  | 
|  | 3878 | uint32_t waitTimeLeftMs = mSourceThread->waitTimeMs(); | 
|  | 3879 |  | 
|  | 3880 | if (!mActive && frames != 0) { | 
|  | 3881 | start(); | 
|  | 3882 | sp<ThreadBase> thread = mThread.promote(); | 
|  | 3883 | if (thread != 0) { | 
|  | 3884 | MixerThread *mixerThread = (MixerThread *)thread.get(); | 
|  | 3885 | if (mCblk->frameCount > frames){ | 
|  | 3886 | if (mBufferQueue.size() < kMaxOverFlowBuffers) { | 
|  | 3887 | uint32_t startFrames = (mCblk->frameCount - frames); | 
|  | 3888 | pInBuffer = new Buffer; | 
|  | 3889 | pInBuffer->mBuffer = new int16_t[startFrames * channelCount]; | 
|  | 3890 | pInBuffer->frameCount = startFrames; | 
|  | 3891 | pInBuffer->i16 = pInBuffer->mBuffer; | 
|  | 3892 | memset(pInBuffer->raw, 0, startFrames * channelCount * sizeof(int16_t)); | 
|  | 3893 | mBufferQueue.add(pInBuffer); | 
|  | 3894 | } else { | 
| Steve Block | 5ff1dd5 | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 3895 | ALOGW ("OutputTrack::write() %p no more buffers in queue", this); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3896 | } | 
|  | 3897 | } | 
|  | 3898 | } | 
|  | 3899 | } | 
|  | 3900 |  | 
|  | 3901 | while (waitTimeLeftMs) { | 
|  | 3902 | // First write pending buffers, then new data | 
|  | 3903 | if (mBufferQueue.size()) { | 
|  | 3904 | pInBuffer = mBufferQueue.itemAt(0); | 
|  | 3905 | } else { | 
|  | 3906 | pInBuffer = &inBuffer; | 
|  | 3907 | } | 
|  | 3908 |  | 
|  | 3909 | if (pInBuffer->frameCount == 0) { | 
|  | 3910 | break; | 
|  | 3911 | } | 
|  | 3912 |  | 
|  | 3913 | if (mOutBuffer.frameCount == 0) { | 
|  | 3914 | mOutBuffer.frameCount = pInBuffer->frameCount; | 
|  | 3915 | nsecs_t startTime = systemTime(); | 
| Glenn Kasten | 335787f | 2012-01-20 17:00:00 -0800 | [diff] [blame] | 3916 | if (obtainBuffer(&mOutBuffer, waitTimeLeftMs) == (status_t)NO_MORE_BUFFERS) { | 
| Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 3917 | ALOGV ("OutputTrack::write() %p thread %p no more output buffers", this, mThread.unsafe_get()); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3918 | outputBufferFull = true; | 
|  | 3919 | break; | 
|  | 3920 | } | 
|  | 3921 | uint32_t waitTimeMs = (uint32_t)ns2ms(systemTime() - startTime); | 
|  | 3922 | if (waitTimeLeftMs >= waitTimeMs) { | 
|  | 3923 | waitTimeLeftMs -= waitTimeMs; | 
|  | 3924 | } else { | 
|  | 3925 | waitTimeLeftMs = 0; | 
|  | 3926 | } | 
|  | 3927 | } | 
|  | 3928 |  | 
|  | 3929 | uint32_t outFrames = pInBuffer->frameCount > mOutBuffer.frameCount ? mOutBuffer.frameCount : pInBuffer->frameCount; | 
|  | 3930 | memcpy(mOutBuffer.raw, pInBuffer->raw, outFrames * channelCount * sizeof(int16_t)); | 
|  | 3931 | mCblk->stepUser(outFrames); | 
|  | 3932 | pInBuffer->frameCount -= outFrames; | 
|  | 3933 | pInBuffer->i16 += outFrames * channelCount; | 
|  | 3934 | mOutBuffer.frameCount -= outFrames; | 
|  | 3935 | mOutBuffer.i16 += outFrames * channelCount; | 
|  | 3936 |  | 
|  | 3937 | if (pInBuffer->frameCount == 0) { | 
|  | 3938 | if (mBufferQueue.size()) { | 
|  | 3939 | mBufferQueue.removeAt(0); | 
|  | 3940 | delete [] pInBuffer->mBuffer; | 
|  | 3941 | delete pInBuffer; | 
| Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 3942 | ALOGV("OutputTrack::write() %p thread %p released overflow buffer %d", this, mThread.unsafe_get(), mBufferQueue.size()); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3943 | } else { | 
|  | 3944 | break; | 
|  | 3945 | } | 
|  | 3946 | } | 
|  | 3947 | } | 
|  | 3948 |  | 
|  | 3949 | // If we could not write all frames, allocate a buffer and queue it for next time. | 
|  | 3950 | if (inBuffer.frameCount) { | 
|  | 3951 | sp<ThreadBase> thread = mThread.promote(); | 
|  | 3952 | if (thread != 0 && !thread->standby()) { | 
|  | 3953 | if (mBufferQueue.size() < kMaxOverFlowBuffers) { | 
|  | 3954 | pInBuffer = new Buffer; | 
|  | 3955 | pInBuffer->mBuffer = new int16_t[inBuffer.frameCount * channelCount]; | 
|  | 3956 | pInBuffer->frameCount = inBuffer.frameCount; | 
|  | 3957 | pInBuffer->i16 = pInBuffer->mBuffer; | 
|  | 3958 | memcpy(pInBuffer->raw, inBuffer.raw, inBuffer.frameCount * channelCount * sizeof(int16_t)); | 
|  | 3959 | mBufferQueue.add(pInBuffer); | 
| Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 3960 | ALOGV("OutputTrack::write() %p thread %p adding overflow buffer %d", this, mThread.unsafe_get(), mBufferQueue.size()); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3961 | } else { | 
| Steve Block | 5ff1dd5 | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 3962 | ALOGW("OutputTrack::write() %p thread %p no more overflow buffers", mThread.unsafe_get(), this); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3963 | } | 
|  | 3964 | } | 
|  | 3965 | } | 
|  | 3966 |  | 
|  | 3967 | // Calling write() with a 0 length buffer, means that no more data will be written: | 
|  | 3968 | // If no more buffers are pending, fill output track buffer to make sure it is started | 
|  | 3969 | // by output mixer. | 
|  | 3970 | if (frames == 0 && mBufferQueue.size() == 0) { | 
|  | 3971 | if (mCblk->user < mCblk->frameCount) { | 
|  | 3972 | frames = mCblk->frameCount - mCblk->user; | 
|  | 3973 | pInBuffer = new Buffer; | 
|  | 3974 | pInBuffer->mBuffer = new int16_t[frames * channelCount]; | 
|  | 3975 | pInBuffer->frameCount = frames; | 
|  | 3976 | pInBuffer->i16 = pInBuffer->mBuffer; | 
|  | 3977 | memset(pInBuffer->raw, 0, frames * channelCount * sizeof(int16_t)); | 
|  | 3978 | mBufferQueue.add(pInBuffer); | 
|  | 3979 | } else if (mActive) { | 
|  | 3980 | stop(); | 
|  | 3981 | } | 
|  | 3982 | } | 
|  | 3983 |  | 
|  | 3984 | return outputBufferFull; | 
|  | 3985 | } | 
|  | 3986 |  | 
|  | 3987 | status_t AudioFlinger::PlaybackThread::OutputTrack::obtainBuffer(AudioBufferProvider::Buffer* buffer, uint32_t waitTimeMs) | 
|  | 3988 | { | 
|  | 3989 | int active; | 
|  | 3990 | status_t result; | 
|  | 3991 | audio_track_cblk_t* cblk = mCblk; | 
|  | 3992 | uint32_t framesReq = buffer->frameCount; | 
|  | 3993 |  | 
| Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 3994 | //    ALOGV("OutputTrack::obtainBuffer user %d, server %d", cblk->user, cblk->server); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3995 | buffer->frameCount  = 0; | 
|  | 3996 |  | 
|  | 3997 | uint32_t framesAvail = cblk->framesAvailable(); | 
|  | 3998 |  | 
|  | 3999 |  | 
|  | 4000 | if (framesAvail == 0) { | 
|  | 4001 | Mutex::Autolock _l(cblk->lock); | 
|  | 4002 | goto start_loop_here; | 
|  | 4003 | while (framesAvail == 0) { | 
|  | 4004 | active = mActive; | 
| Glenn Kasten | f6b1678 | 2011-12-15 09:51:17 -0800 | [diff] [blame] | 4005 | if (CC_UNLIKELY(!active)) { | 
| Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 4006 | ALOGV("Not active and NO_MORE_BUFFERS"); | 
| Glenn Kasten | 335787f | 2012-01-20 17:00:00 -0800 | [diff] [blame] | 4007 | return NO_MORE_BUFFERS; | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4008 | } | 
|  | 4009 | result = cblk->cv.waitRelative(cblk->lock, milliseconds(waitTimeMs)); | 
|  | 4010 | if (result != NO_ERROR) { | 
| Glenn Kasten | 335787f | 2012-01-20 17:00:00 -0800 | [diff] [blame] | 4011 | return NO_MORE_BUFFERS; | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4012 | } | 
|  | 4013 | // read the server count again | 
|  | 4014 | start_loop_here: | 
|  | 4015 | framesAvail = cblk->framesAvailable_l(); | 
|  | 4016 | } | 
|  | 4017 | } | 
|  | 4018 |  | 
|  | 4019 | //    if (framesAvail < framesReq) { | 
| Glenn Kasten | 335787f | 2012-01-20 17:00:00 -0800 | [diff] [blame] | 4020 | //        return NO_MORE_BUFFERS; | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4021 | //    } | 
|  | 4022 |  | 
|  | 4023 | if (framesReq > framesAvail) { | 
|  | 4024 | framesReq = framesAvail; | 
|  | 4025 | } | 
|  | 4026 |  | 
|  | 4027 | uint32_t u = cblk->user; | 
|  | 4028 | uint32_t bufferEnd = cblk->userBase + cblk->frameCount; | 
|  | 4029 |  | 
|  | 4030 | if (u + framesReq > bufferEnd) { | 
|  | 4031 | framesReq = bufferEnd - u; | 
|  | 4032 | } | 
|  | 4033 |  | 
|  | 4034 | buffer->frameCount  = framesReq; | 
|  | 4035 | buffer->raw         = (void *)cblk->buffer(u); | 
|  | 4036 | return NO_ERROR; | 
|  | 4037 | } | 
|  | 4038 |  | 
|  | 4039 |  | 
|  | 4040 | void AudioFlinger::PlaybackThread::OutputTrack::clearBufferQueue() | 
|  | 4041 | { | 
|  | 4042 | size_t size = mBufferQueue.size(); | 
|  | 4043 | Buffer *pBuffer; | 
|  | 4044 |  | 
|  | 4045 | for (size_t i = 0; i < size; i++) { | 
|  | 4046 | pBuffer = mBufferQueue.itemAt(i); | 
|  | 4047 | delete [] pBuffer->mBuffer; | 
|  | 4048 | delete pBuffer; | 
|  | 4049 | } | 
|  | 4050 | mBufferQueue.clear(); | 
|  | 4051 | } | 
|  | 4052 |  | 
|  | 4053 | // ---------------------------------------------------------------------------- | 
|  | 4054 |  | 
|  | 4055 | AudioFlinger::Client::Client(const sp<AudioFlinger>& audioFlinger, pid_t pid) | 
|  | 4056 | :   RefBase(), | 
|  | 4057 | mAudioFlinger(audioFlinger), | 
|  | 4058 | mMemoryDealer(new MemoryDealer(1024*1024, "AudioFlinger::Client")), | 
|  | 4059 | mPid(pid) | 
|  | 4060 | { | 
|  | 4061 | // 1 MB of address space is good for 32 tracks, 8 buffers each, 4 KB/buffer | 
|  | 4062 | } | 
|  | 4063 |  | 
|  | 4064 | // Client destructor must be called with AudioFlinger::mLock held | 
|  | 4065 | AudioFlinger::Client::~Client() | 
|  | 4066 | { | 
|  | 4067 | mAudioFlinger->removeClient_l(mPid); | 
|  | 4068 | } | 
|  | 4069 |  | 
|  | 4070 | const sp<MemoryDealer>& AudioFlinger::Client::heap() const | 
|  | 4071 | { | 
|  | 4072 | return mMemoryDealer; | 
|  | 4073 | } | 
|  | 4074 |  | 
|  | 4075 | // ---------------------------------------------------------------------------- | 
|  | 4076 |  | 
|  | 4077 | AudioFlinger::NotificationClient::NotificationClient(const sp<AudioFlinger>& audioFlinger, | 
|  | 4078 | const sp<IAudioFlingerClient>& client, | 
|  | 4079 | pid_t pid) | 
|  | 4080 | : mAudioFlinger(audioFlinger), mPid(pid), mClient(client) | 
|  | 4081 | { | 
|  | 4082 | } | 
|  | 4083 |  | 
|  | 4084 | AudioFlinger::NotificationClient::~NotificationClient() | 
|  | 4085 | { | 
|  | 4086 | mClient.clear(); | 
|  | 4087 | } | 
|  | 4088 |  | 
|  | 4089 | void AudioFlinger::NotificationClient::binderDied(const wp<IBinder>& who) | 
|  | 4090 | { | 
|  | 4091 | sp<NotificationClient> keep(this); | 
|  | 4092 | { | 
|  | 4093 | mAudioFlinger->removeNotificationClient(mPid); | 
|  | 4094 | } | 
|  | 4095 | } | 
|  | 4096 |  | 
|  | 4097 | // ---------------------------------------------------------------------------- | 
|  | 4098 |  | 
|  | 4099 | AudioFlinger::TrackHandle::TrackHandle(const sp<AudioFlinger::PlaybackThread::Track>& track) | 
|  | 4100 | : BnAudioTrack(), | 
|  | 4101 | mTrack(track) | 
|  | 4102 | { | 
|  | 4103 | } | 
|  | 4104 |  | 
|  | 4105 | AudioFlinger::TrackHandle::~TrackHandle() { | 
|  | 4106 | // just stop the track on deletion, associated resources | 
|  | 4107 | // will be freed from the main thread once all pending buffers have | 
|  | 4108 | // been played. Unless it's not in the active track list, in which | 
|  | 4109 | // case we free everything now... | 
|  | 4110 | mTrack->destroy(); | 
|  | 4111 | } | 
|  | 4112 |  | 
|  | 4113 | status_t AudioFlinger::TrackHandle::start() { | 
|  | 4114 | return mTrack->start(); | 
|  | 4115 | } | 
|  | 4116 |  | 
|  | 4117 | void AudioFlinger::TrackHandle::stop() { | 
|  | 4118 | mTrack->stop(); | 
|  | 4119 | } | 
|  | 4120 |  | 
|  | 4121 | void AudioFlinger::TrackHandle::flush() { | 
|  | 4122 | mTrack->flush(); | 
|  | 4123 | } | 
|  | 4124 |  | 
|  | 4125 | void AudioFlinger::TrackHandle::mute(bool e) { | 
|  | 4126 | mTrack->mute(e); | 
|  | 4127 | } | 
|  | 4128 |  | 
|  | 4129 | void AudioFlinger::TrackHandle::pause() { | 
|  | 4130 | mTrack->pause(); | 
|  | 4131 | } | 
|  | 4132 |  | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4133 | sp<IMemory> AudioFlinger::TrackHandle::getCblk() const { | 
|  | 4134 | return mTrack->getCblk(); | 
|  | 4135 | } | 
|  | 4136 |  | 
|  | 4137 | status_t AudioFlinger::TrackHandle::attachAuxEffect(int EffectId) | 
|  | 4138 | { | 
|  | 4139 | return mTrack->attachAuxEffect(EffectId); | 
|  | 4140 | } | 
|  | 4141 |  | 
|  | 4142 | status_t AudioFlinger::TrackHandle::onTransact( | 
|  | 4143 | uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags) | 
|  | 4144 | { | 
|  | 4145 | return BnAudioTrack::onTransact(code, data, reply, flags); | 
|  | 4146 | } | 
|  | 4147 |  | 
|  | 4148 | // ---------------------------------------------------------------------------- | 
|  | 4149 |  | 
|  | 4150 | sp<IAudioRecord> AudioFlinger::openRecord( | 
|  | 4151 | pid_t pid, | 
|  | 4152 | int input, | 
|  | 4153 | uint32_t sampleRate, | 
| Glenn Kasten | 58f3021 | 2012-01-12 12:27:51 -0800 | [diff] [blame] | 4154 | audio_format_t format, | 
| Jean-Michel Trivi | 0d255b2 | 2011-05-24 15:53:33 -0700 | [diff] [blame] | 4155 | uint32_t channelMask, | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4156 | int frameCount, | 
|  | 4157 | uint32_t flags, | 
|  | 4158 | int *sessionId, | 
|  | 4159 | status_t *status) | 
|  | 4160 | { | 
|  | 4161 | sp<RecordThread::RecordTrack> recordTrack; | 
|  | 4162 | sp<RecordHandle> recordHandle; | 
|  | 4163 | sp<Client> client; | 
|  | 4164 | wp<Client> wclient; | 
|  | 4165 | status_t lStatus; | 
|  | 4166 | RecordThread *thread; | 
|  | 4167 | size_t inFrameCount; | 
|  | 4168 | int lSessionId; | 
|  | 4169 |  | 
|  | 4170 | // check calling permissions | 
|  | 4171 | if (!recordingAllowed()) { | 
|  | 4172 | lStatus = PERMISSION_DENIED; | 
|  | 4173 | goto Exit; | 
|  | 4174 | } | 
|  | 4175 |  | 
|  | 4176 | // add client to list | 
|  | 4177 | { // scope for mLock | 
|  | 4178 | Mutex::Autolock _l(mLock); | 
|  | 4179 | thread = checkRecordThread_l(input); | 
|  | 4180 | if (thread == NULL) { | 
|  | 4181 | lStatus = BAD_VALUE; | 
|  | 4182 | goto Exit; | 
|  | 4183 | } | 
|  | 4184 |  | 
|  | 4185 | wclient = mClients.valueFor(pid); | 
|  | 4186 | if (wclient != NULL) { | 
|  | 4187 | client = wclient.promote(); | 
|  | 4188 | } else { | 
|  | 4189 | client = new Client(this, pid); | 
|  | 4190 | mClients.add(pid, client); | 
|  | 4191 | } | 
|  | 4192 |  | 
|  | 4193 | // If no audio session id is provided, create one here | 
| Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 4194 | if (sessionId != NULL && *sessionId != AUDIO_SESSION_OUTPUT_MIX) { | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4195 | lSessionId = *sessionId; | 
|  | 4196 | } else { | 
| Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 4197 | lSessionId = nextUniqueId(); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4198 | if (sessionId != NULL) { | 
|  | 4199 | *sessionId = lSessionId; | 
|  | 4200 | } | 
|  | 4201 | } | 
|  | 4202 | // create new record track. The record track uses one track in mHardwareMixerThread by convention. | 
| Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 4203 | recordTrack = thread->createRecordTrack_l(client, | 
|  | 4204 | sampleRate, | 
|  | 4205 | format, | 
|  | 4206 | channelMask, | 
|  | 4207 | frameCount, | 
|  | 4208 | flags, | 
|  | 4209 | lSessionId, | 
|  | 4210 | &lStatus); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4211 | } | 
| Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 4212 | if (lStatus != NO_ERROR) { | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4213 | // remove local strong reference to Client before deleting the RecordTrack so that the Client | 
|  | 4214 | // destructor is called by the TrackBase destructor with mLock held | 
|  | 4215 | client.clear(); | 
|  | 4216 | recordTrack.clear(); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4217 | goto Exit; | 
|  | 4218 | } | 
|  | 4219 |  | 
|  | 4220 | // return to handle to client | 
|  | 4221 | recordHandle = new RecordHandle(recordTrack); | 
|  | 4222 | lStatus = NO_ERROR; | 
|  | 4223 |  | 
|  | 4224 | Exit: | 
|  | 4225 | if (status) { | 
|  | 4226 | *status = lStatus; | 
|  | 4227 | } | 
|  | 4228 | return recordHandle; | 
|  | 4229 | } | 
|  | 4230 |  | 
|  | 4231 | // ---------------------------------------------------------------------------- | 
|  | 4232 |  | 
|  | 4233 | AudioFlinger::RecordHandle::RecordHandle(const sp<AudioFlinger::RecordThread::RecordTrack>& recordTrack) | 
|  | 4234 | : BnAudioRecord(), | 
|  | 4235 | mRecordTrack(recordTrack) | 
|  | 4236 | { | 
|  | 4237 | } | 
|  | 4238 |  | 
|  | 4239 | AudioFlinger::RecordHandle::~RecordHandle() { | 
|  | 4240 | stop(); | 
|  | 4241 | } | 
|  | 4242 |  | 
|  | 4243 | status_t AudioFlinger::RecordHandle::start() { | 
| Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 4244 | ALOGV("RecordHandle::start()"); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4245 | return mRecordTrack->start(); | 
|  | 4246 | } | 
|  | 4247 |  | 
|  | 4248 | void AudioFlinger::RecordHandle::stop() { | 
| Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 4249 | ALOGV("RecordHandle::stop()"); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4250 | mRecordTrack->stop(); | 
|  | 4251 | } | 
|  | 4252 |  | 
|  | 4253 | sp<IMemory> AudioFlinger::RecordHandle::getCblk() const { | 
|  | 4254 | return mRecordTrack->getCblk(); | 
|  | 4255 | } | 
|  | 4256 |  | 
|  | 4257 | status_t AudioFlinger::RecordHandle::onTransact( | 
|  | 4258 | uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags) | 
|  | 4259 | { | 
|  | 4260 | return BnAudioRecord::onTransact(code, data, reply, flags); | 
|  | 4261 | } | 
|  | 4262 |  | 
|  | 4263 | // ---------------------------------------------------------------------------- | 
|  | 4264 |  | 
| Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 4265 | AudioFlinger::RecordThread::RecordThread(const sp<AudioFlinger>& audioFlinger, | 
|  | 4266 | AudioStreamIn *input, | 
|  | 4267 | uint32_t sampleRate, | 
|  | 4268 | uint32_t channels, | 
|  | 4269 | int id, | 
|  | 4270 | uint32_t device) : | 
| Glenn Kasten | 23bb8be | 2012-01-26 10:38:26 -0800 | [diff] [blame^] | 4271 | ThreadBase(audioFlinger, id, device, RECORD), | 
| Glenn Kasten | e0feee3 | 2011-12-13 11:53:26 -0800 | [diff] [blame] | 4272 | mInput(input), mTrack(NULL), mResampler(NULL), mRsmpOutBuffer(NULL), mRsmpInBuffer(NULL) | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4273 | { | 
| Eric Laurent | feb0db6 | 2011-07-22 09:04:31 -0700 | [diff] [blame] | 4274 | snprintf(mName, kNameLength, "AudioIn_%d", id); | 
|  | 4275 |  | 
| Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 4276 | mReqChannelCount = popcount(channels); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4277 | mReqSampleRate = sampleRate; | 
|  | 4278 | readInputParameters(); | 
|  | 4279 | } | 
|  | 4280 |  | 
|  | 4281 |  | 
|  | 4282 | AudioFlinger::RecordThread::~RecordThread() | 
|  | 4283 | { | 
|  | 4284 | delete[] mRsmpInBuffer; | 
| Glenn Kasten | e0feee3 | 2011-12-13 11:53:26 -0800 | [diff] [blame] | 4285 | if (mResampler != NULL) { | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4286 | delete mResampler; | 
|  | 4287 | delete[] mRsmpOutBuffer; | 
|  | 4288 | } | 
|  | 4289 | } | 
|  | 4290 |  | 
|  | 4291 | void AudioFlinger::RecordThread::onFirstRef() | 
|  | 4292 | { | 
| Eric Laurent | feb0db6 | 2011-07-22 09:04:31 -0700 | [diff] [blame] | 4293 | run(mName, PRIORITY_URGENT_AUDIO); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4294 | } | 
|  | 4295 |  | 
| Eric Laurent | b8ba0a9 | 2011-08-07 16:32:26 -0700 | [diff] [blame] | 4296 | status_t AudioFlinger::RecordThread::readyToRun() | 
|  | 4297 | { | 
|  | 4298 | status_t status = initCheck(); | 
| Steve Block | 5ff1dd5 | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 4299 | ALOGW_IF(status != NO_ERROR,"RecordThread %p could not initialize", this); | 
| Eric Laurent | b8ba0a9 | 2011-08-07 16:32:26 -0700 | [diff] [blame] | 4300 | return status; | 
|  | 4301 | } | 
|  | 4302 |  | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4303 | bool AudioFlinger::RecordThread::threadLoop() | 
|  | 4304 | { | 
|  | 4305 | AudioBufferProvider::Buffer buffer; | 
|  | 4306 | sp<RecordTrack> activeTrack; | 
| Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 4307 | Vector< sp<EffectChain> > effectChains; | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4308 |  | 
| Eric Laurent | 44d9848 | 2010-09-30 16:12:31 -0700 | [diff] [blame] | 4309 | nsecs_t lastWarning = 0; | 
|  | 4310 |  | 
| Eric Laurent | feb0db6 | 2011-07-22 09:04:31 -0700 | [diff] [blame] | 4311 | acquireWakeLock(); | 
|  | 4312 |  | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4313 | // start recording | 
|  | 4314 | while (!exitPending()) { | 
|  | 4315 |  | 
|  | 4316 | processConfigEvents(); | 
|  | 4317 |  | 
|  | 4318 | { // scope for mLock | 
|  | 4319 | Mutex::Autolock _l(mLock); | 
|  | 4320 | checkForNewParameters_l(); | 
|  | 4321 | if (mActiveTrack == 0 && mConfigEvents.isEmpty()) { | 
|  | 4322 | if (!mStandby) { | 
| Dima Zavin | 799a70e | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 4323 | mInput->stream->common.standby(&mInput->stream->common); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4324 | mStandby = true; | 
|  | 4325 | } | 
|  | 4326 |  | 
|  | 4327 | if (exitPending()) break; | 
|  | 4328 |  | 
| Eric Laurent | feb0db6 | 2011-07-22 09:04:31 -0700 | [diff] [blame] | 4329 | releaseWakeLock_l(); | 
| Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 4330 | ALOGV("RecordThread: loop stopping"); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4331 | // go to sleep | 
|  | 4332 | mWaitWorkCV.wait(mLock); | 
| Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 4333 | ALOGV("RecordThread: loop starting"); | 
| Eric Laurent | feb0db6 | 2011-07-22 09:04:31 -0700 | [diff] [blame] | 4334 | acquireWakeLock_l(); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4335 | continue; | 
|  | 4336 | } | 
|  | 4337 | if (mActiveTrack != 0) { | 
|  | 4338 | if (mActiveTrack->mState == TrackBase::PAUSING) { | 
|  | 4339 | if (!mStandby) { | 
| Dima Zavin | 799a70e | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 4340 | mInput->stream->common.standby(&mInput->stream->common); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4341 | mStandby = true; | 
|  | 4342 | } | 
|  | 4343 | mActiveTrack.clear(); | 
|  | 4344 | mStartStopCond.broadcast(); | 
|  | 4345 | } else if (mActiveTrack->mState == TrackBase::RESUMING) { | 
|  | 4346 | if (mReqChannelCount != mActiveTrack->channelCount()) { | 
|  | 4347 | mActiveTrack.clear(); | 
|  | 4348 | mStartStopCond.broadcast(); | 
|  | 4349 | } else if (mBytesRead != 0) { | 
|  | 4350 | // record start succeeds only if first read from audio input | 
|  | 4351 | // succeeds | 
|  | 4352 | if (mBytesRead > 0) { | 
|  | 4353 | mActiveTrack->mState = TrackBase::ACTIVE; | 
|  | 4354 | } else { | 
|  | 4355 | mActiveTrack.clear(); | 
|  | 4356 | } | 
|  | 4357 | mStartStopCond.broadcast(); | 
|  | 4358 | } | 
|  | 4359 | mStandby = false; | 
|  | 4360 | } | 
|  | 4361 | } | 
| Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 4362 | lockEffectChains_l(effectChains); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4363 | } | 
|  | 4364 |  | 
|  | 4365 | if (mActiveTrack != 0) { | 
|  | 4366 | if (mActiveTrack->mState != TrackBase::ACTIVE && | 
|  | 4367 | mActiveTrack->mState != TrackBase::RESUMING) { | 
| Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 4368 | unlockEffectChains(effectChains); | 
|  | 4369 | usleep(kRecordThreadSleepUs); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4370 | continue; | 
|  | 4371 | } | 
| Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 4372 | for (size_t i = 0; i < effectChains.size(); i ++) { | 
|  | 4373 | effectChains[i]->process_l(); | 
|  | 4374 | } | 
| Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 4375 |  | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4376 | buffer.frameCount = mFrameCount; | 
| Glenn Kasten | f6b1678 | 2011-12-15 09:51:17 -0800 | [diff] [blame] | 4377 | if (CC_LIKELY(mActiveTrack->getNextBuffer(&buffer) == NO_ERROR)) { | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4378 | size_t framesOut = buffer.frameCount; | 
| Glenn Kasten | e0feee3 | 2011-12-13 11:53:26 -0800 | [diff] [blame] | 4379 | if (mResampler == NULL) { | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4380 | // no resampling | 
|  | 4381 | while (framesOut) { | 
|  | 4382 | size_t framesIn = mFrameCount - mRsmpInIndex; | 
|  | 4383 | if (framesIn) { | 
|  | 4384 | int8_t *src = (int8_t *)mRsmpInBuffer + mRsmpInIndex * mFrameSize; | 
|  | 4385 | int8_t *dst = buffer.i8 + (buffer.frameCount - framesOut) * mActiveTrack->mCblk->frameSize; | 
|  | 4386 | if (framesIn > framesOut) | 
|  | 4387 | framesIn = framesOut; | 
|  | 4388 | mRsmpInIndex += framesIn; | 
|  | 4389 | framesOut -= framesIn; | 
|  | 4390 | if ((int)mChannelCount == mReqChannelCount || | 
| Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 4391 | mFormat != AUDIO_FORMAT_PCM_16_BIT) { | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4392 | memcpy(dst, src, framesIn * mFrameSize); | 
|  | 4393 | } else { | 
|  | 4394 | int16_t *src16 = (int16_t *)src; | 
|  | 4395 | int16_t *dst16 = (int16_t *)dst; | 
|  | 4396 | if (mChannelCount == 1) { | 
|  | 4397 | while (framesIn--) { | 
|  | 4398 | *dst16++ = *src16; | 
|  | 4399 | *dst16++ = *src16++; | 
|  | 4400 | } | 
|  | 4401 | } else { | 
|  | 4402 | while (framesIn--) { | 
|  | 4403 | *dst16++ = (int16_t)(((int32_t)*src16 + (int32_t)*(src16 + 1)) >> 1); | 
|  | 4404 | src16 += 2; | 
|  | 4405 | } | 
|  | 4406 | } | 
|  | 4407 | } | 
|  | 4408 | } | 
|  | 4409 | if (framesOut && mFrameCount == mRsmpInIndex) { | 
|  | 4410 | if (framesOut == mFrameCount && | 
| Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 4411 | ((int)mChannelCount == mReqChannelCount || mFormat != AUDIO_FORMAT_PCM_16_BIT)) { | 
| Dima Zavin | 799a70e | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 4412 | mBytesRead = mInput->stream->read(mInput->stream, buffer.raw, mInputBytes); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4413 | framesOut = 0; | 
|  | 4414 | } else { | 
| Dima Zavin | 799a70e | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 4415 | mBytesRead = mInput->stream->read(mInput->stream, mRsmpInBuffer, mInputBytes); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4416 | mRsmpInIndex = 0; | 
|  | 4417 | } | 
|  | 4418 | if (mBytesRead < 0) { | 
| Steve Block | 29357bc | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 4419 | ALOGE("Error reading audio input"); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4420 | if (mActiveTrack->mState == TrackBase::ACTIVE) { | 
|  | 4421 | // Force input into standby so that it tries to | 
|  | 4422 | // recover at next read attempt | 
| Dima Zavin | 799a70e | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 4423 | mInput->stream->common.standby(&mInput->stream->common); | 
| Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 4424 | usleep(kRecordThreadSleepUs); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4425 | } | 
|  | 4426 | mRsmpInIndex = mFrameCount; | 
|  | 4427 | framesOut = 0; | 
|  | 4428 | buffer.frameCount = 0; | 
|  | 4429 | } | 
|  | 4430 | } | 
|  | 4431 | } | 
|  | 4432 | } else { | 
|  | 4433 | // resampling | 
|  | 4434 |  | 
|  | 4435 | memset(mRsmpOutBuffer, 0, framesOut * 2 * sizeof(int32_t)); | 
|  | 4436 | // alter output frame count as if we were expecting stereo samples | 
|  | 4437 | if (mChannelCount == 1 && mReqChannelCount == 1) { | 
|  | 4438 | framesOut >>= 1; | 
|  | 4439 | } | 
|  | 4440 | mResampler->resample(mRsmpOutBuffer, framesOut, this); | 
|  | 4441 | // ditherAndClamp() works as long as all buffers returned by mActiveTrack->getNextBuffer() | 
|  | 4442 | // are 32 bit aligned which should be always true. | 
|  | 4443 | if (mChannelCount == 2 && mReqChannelCount == 1) { | 
| Glenn Kasten | 3b21c50 | 2011-12-15 09:52:39 -0800 | [diff] [blame] | 4444 | ditherAndClamp(mRsmpOutBuffer, mRsmpOutBuffer, framesOut); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4445 | // the resampler always outputs stereo samples: do post stereo to mono conversion | 
|  | 4446 | int16_t *src = (int16_t *)mRsmpOutBuffer; | 
|  | 4447 | int16_t *dst = buffer.i16; | 
|  | 4448 | while (framesOut--) { | 
|  | 4449 | *dst++ = (int16_t)(((int32_t)*src + (int32_t)*(src + 1)) >> 1); | 
|  | 4450 | src += 2; | 
|  | 4451 | } | 
|  | 4452 | } else { | 
| Glenn Kasten | 3b21c50 | 2011-12-15 09:52:39 -0800 | [diff] [blame] | 4453 | ditherAndClamp((int32_t *)buffer.raw, mRsmpOutBuffer, framesOut); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4454 | } | 
|  | 4455 |  | 
|  | 4456 | } | 
|  | 4457 | mActiveTrack->releaseBuffer(&buffer); | 
|  | 4458 | mActiveTrack->overflow(); | 
|  | 4459 | } | 
|  | 4460 | // client isn't retrieving buffers fast enough | 
|  | 4461 | else { | 
| Eric Laurent | 44d9848 | 2010-09-30 16:12:31 -0700 | [diff] [blame] | 4462 | if (!mActiveTrack->setOverflow()) { | 
|  | 4463 | nsecs_t now = systemTime(); | 
| Glenn Kasten | 7dede87 | 2011-12-13 11:04:14 -0800 | [diff] [blame] | 4464 | if ((now - lastWarning) > kWarningThrottleNs) { | 
| Steve Block | 5ff1dd5 | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 4465 | ALOGW("RecordThread: buffer overflow"); | 
| Eric Laurent | 44d9848 | 2010-09-30 16:12:31 -0700 | [diff] [blame] | 4466 | lastWarning = now; | 
|  | 4467 | } | 
|  | 4468 | } | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4469 | // Release the processor for a while before asking for a new buffer. | 
|  | 4470 | // This will give the application more chance to read from the buffer and | 
|  | 4471 | // clear the overflow. | 
| Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 4472 | usleep(kRecordThreadSleepUs); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4473 | } | 
|  | 4474 | } | 
| Eric Laurent | ec437d8 | 2011-07-26 20:54:46 -0700 | [diff] [blame] | 4475 | // enable changes in effect chain | 
|  | 4476 | unlockEffectChains(effectChains); | 
| Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 4477 | effectChains.clear(); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4478 | } | 
|  | 4479 |  | 
|  | 4480 | if (!mStandby) { | 
| Dima Zavin | 799a70e | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 4481 | mInput->stream->common.standby(&mInput->stream->common); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4482 | } | 
|  | 4483 | mActiveTrack.clear(); | 
|  | 4484 |  | 
|  | 4485 | mStartStopCond.broadcast(); | 
|  | 4486 |  | 
| Eric Laurent | feb0db6 | 2011-07-22 09:04:31 -0700 | [diff] [blame] | 4487 | releaseWakeLock(); | 
|  | 4488 |  | 
| Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 4489 | ALOGV("RecordThread %p exiting", this); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4490 | return false; | 
|  | 4491 | } | 
|  | 4492 |  | 
| Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 4493 |  | 
|  | 4494 | sp<AudioFlinger::RecordThread::RecordTrack>  AudioFlinger::RecordThread::createRecordTrack_l( | 
|  | 4495 | const sp<AudioFlinger::Client>& client, | 
|  | 4496 | uint32_t sampleRate, | 
| Glenn Kasten | 58f3021 | 2012-01-12 12:27:51 -0800 | [diff] [blame] | 4497 | audio_format_t format, | 
| Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 4498 | int channelMask, | 
|  | 4499 | int frameCount, | 
|  | 4500 | uint32_t flags, | 
|  | 4501 | int sessionId, | 
|  | 4502 | status_t *status) | 
|  | 4503 | { | 
|  | 4504 | sp<RecordTrack> track; | 
|  | 4505 | status_t lStatus; | 
|  | 4506 |  | 
|  | 4507 | lStatus = initCheck(); | 
|  | 4508 | if (lStatus != NO_ERROR) { | 
| Steve Block | 29357bc | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 4509 | ALOGE("Audio driver not initialized."); | 
| Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 4510 | goto Exit; | 
|  | 4511 | } | 
|  | 4512 |  | 
|  | 4513 | { // scope for mLock | 
|  | 4514 | Mutex::Autolock _l(mLock); | 
|  | 4515 |  | 
|  | 4516 | track = new RecordTrack(this, client, sampleRate, | 
|  | 4517 | format, channelMask, frameCount, flags, sessionId); | 
|  | 4518 |  | 
|  | 4519 | if (track->getCblk() == NULL) { | 
|  | 4520 | lStatus = NO_MEMORY; | 
|  | 4521 | goto Exit; | 
|  | 4522 | } | 
|  | 4523 |  | 
|  | 4524 | mTrack = track.get(); | 
| Eric Laurent | 59bd0da | 2011-08-01 09:52:20 -0700 | [diff] [blame] | 4525 | // disable AEC and NS if the device is a BT SCO headset supporting those pre processings | 
|  | 4526 | bool suspend = audio_is_bluetooth_sco_device( | 
| Eric Laurent | bee5337 | 2011-08-29 12:42:48 -0700 | [diff] [blame] | 4527 | (audio_devices_t)(mDevice & AUDIO_DEVICE_IN_ALL)) && mAudioFlinger->btNrecIsOff(); | 
| Eric Laurent | 59bd0da | 2011-08-01 09:52:20 -0700 | [diff] [blame] | 4528 | setEffectSuspended_l(FX_IID_AEC, suspend, sessionId); | 
|  | 4529 | setEffectSuspended_l(FX_IID_NS, suspend, sessionId); | 
| Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 4530 | } | 
|  | 4531 | lStatus = NO_ERROR; | 
|  | 4532 |  | 
|  | 4533 | Exit: | 
|  | 4534 | if (status) { | 
|  | 4535 | *status = lStatus; | 
|  | 4536 | } | 
|  | 4537 | return track; | 
|  | 4538 | } | 
|  | 4539 |  | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4540 | status_t AudioFlinger::RecordThread::start(RecordThread::RecordTrack* recordTrack) | 
|  | 4541 | { | 
| Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 4542 | ALOGV("RecordThread::start"); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4543 | sp <ThreadBase> strongMe = this; | 
|  | 4544 | status_t status = NO_ERROR; | 
|  | 4545 | { | 
| Glenn Kasten | a7d8d6f | 2012-01-05 15:41:56 -0800 | [diff] [blame] | 4546 | AutoMutex lock(mLock); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4547 | if (mActiveTrack != 0) { | 
|  | 4548 | if (recordTrack != mActiveTrack.get()) { | 
|  | 4549 | status = -EBUSY; | 
|  | 4550 | } else if (mActiveTrack->mState == TrackBase::PAUSING) { | 
|  | 4551 | mActiveTrack->mState = TrackBase::ACTIVE; | 
|  | 4552 | } | 
|  | 4553 | return status; | 
|  | 4554 | } | 
|  | 4555 |  | 
|  | 4556 | recordTrack->mState = TrackBase::IDLE; | 
|  | 4557 | mActiveTrack = recordTrack; | 
|  | 4558 | mLock.unlock(); | 
|  | 4559 | status_t status = AudioSystem::startInput(mId); | 
|  | 4560 | mLock.lock(); | 
|  | 4561 | if (status != NO_ERROR) { | 
|  | 4562 | mActiveTrack.clear(); | 
|  | 4563 | return status; | 
|  | 4564 | } | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4565 | mRsmpInIndex = mFrameCount; | 
|  | 4566 | mBytesRead = 0; | 
| Eric Laurent | 243f5f9 | 2011-02-28 16:52:51 -0800 | [diff] [blame] | 4567 | if (mResampler != NULL) { | 
|  | 4568 | mResampler->reset(); | 
|  | 4569 | } | 
|  | 4570 | mActiveTrack->mState = TrackBase::RESUMING; | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4571 | // signal thread to start | 
| Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 4572 | ALOGV("Signal record thread"); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4573 | mWaitWorkCV.signal(); | 
|  | 4574 | // do not wait for mStartStopCond if exiting | 
|  | 4575 | if (mExiting) { | 
|  | 4576 | mActiveTrack.clear(); | 
|  | 4577 | status = INVALID_OPERATION; | 
|  | 4578 | goto startError; | 
|  | 4579 | } | 
|  | 4580 | mStartStopCond.wait(mLock); | 
|  | 4581 | if (mActiveTrack == 0) { | 
| Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 4582 | ALOGV("Record failed to start"); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4583 | status = BAD_VALUE; | 
|  | 4584 | goto startError; | 
|  | 4585 | } | 
| Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 4586 | ALOGV("Record started OK"); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4587 | return status; | 
|  | 4588 | } | 
|  | 4589 | startError: | 
|  | 4590 | AudioSystem::stopInput(mId); | 
|  | 4591 | return status; | 
|  | 4592 | } | 
|  | 4593 |  | 
|  | 4594 | void AudioFlinger::RecordThread::stop(RecordThread::RecordTrack* recordTrack) { | 
| Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 4595 | ALOGV("RecordThread::stop"); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4596 | sp <ThreadBase> strongMe = this; | 
|  | 4597 | { | 
| Glenn Kasten | a7d8d6f | 2012-01-05 15:41:56 -0800 | [diff] [blame] | 4598 | AutoMutex lock(mLock); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4599 | if (mActiveTrack != 0 && recordTrack == mActiveTrack.get()) { | 
|  | 4600 | mActiveTrack->mState = TrackBase::PAUSING; | 
|  | 4601 | // do not wait for mStartStopCond if exiting | 
|  | 4602 | if (mExiting) { | 
|  | 4603 | return; | 
|  | 4604 | } | 
|  | 4605 | mStartStopCond.wait(mLock); | 
|  | 4606 | // if we have been restarted, recordTrack == mActiveTrack.get() here | 
|  | 4607 | if (mActiveTrack == 0 || recordTrack != mActiveTrack.get()) { | 
|  | 4608 | mLock.unlock(); | 
|  | 4609 | AudioSystem::stopInput(mId); | 
|  | 4610 | mLock.lock(); | 
| Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 4611 | ALOGV("Record stopped OK"); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4612 | } | 
|  | 4613 | } | 
|  | 4614 | } | 
|  | 4615 | } | 
|  | 4616 |  | 
|  | 4617 | status_t AudioFlinger::RecordThread::dump(int fd, const Vector<String16>& args) | 
|  | 4618 | { | 
|  | 4619 | const size_t SIZE = 256; | 
|  | 4620 | char buffer[SIZE]; | 
|  | 4621 | String8 result; | 
|  | 4622 | pid_t pid = 0; | 
|  | 4623 |  | 
|  | 4624 | snprintf(buffer, SIZE, "\nInput thread %p internals\n", this); | 
|  | 4625 | result.append(buffer); | 
|  | 4626 |  | 
|  | 4627 | if (mActiveTrack != 0) { | 
|  | 4628 | result.append("Active Track:\n"); | 
| Jean-Michel Trivi | 0d255b2 | 2011-05-24 15:53:33 -0700 | [diff] [blame] | 4629 | result.append("   Clien Fmt Chn mask   Session Buf  S SRate  Serv     User\n"); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4630 | mActiveTrack->dump(buffer, SIZE); | 
|  | 4631 | result.append(buffer); | 
|  | 4632 |  | 
|  | 4633 | snprintf(buffer, SIZE, "In index: %d\n", mRsmpInIndex); | 
|  | 4634 | result.append(buffer); | 
|  | 4635 | snprintf(buffer, SIZE, "In size: %d\n", mInputBytes); | 
|  | 4636 | result.append(buffer); | 
| Glenn Kasten | e0feee3 | 2011-12-13 11:53:26 -0800 | [diff] [blame] | 4637 | snprintf(buffer, SIZE, "Resampling: %d\n", (mResampler != NULL)); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4638 | result.append(buffer); | 
|  | 4639 | snprintf(buffer, SIZE, "Out channel count: %d\n", mReqChannelCount); | 
|  | 4640 | result.append(buffer); | 
|  | 4641 | snprintf(buffer, SIZE, "Out sample rate: %d\n", mReqSampleRate); | 
|  | 4642 | result.append(buffer); | 
|  | 4643 |  | 
|  | 4644 |  | 
|  | 4645 | } else { | 
|  | 4646 | result.append("No record client\n"); | 
|  | 4647 | } | 
|  | 4648 | write(fd, result.string(), result.size()); | 
|  | 4649 |  | 
|  | 4650 | dumpBase(fd, args); | 
| Eric Laurent | 1d2bff0 | 2011-07-24 17:49:51 -0700 | [diff] [blame] | 4651 | dumpEffectChains(fd, args); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4652 |  | 
|  | 4653 | return NO_ERROR; | 
|  | 4654 | } | 
|  | 4655 |  | 
|  | 4656 | status_t AudioFlinger::RecordThread::getNextBuffer(AudioBufferProvider::Buffer* buffer) | 
|  | 4657 | { | 
|  | 4658 | size_t framesReq = buffer->frameCount; | 
|  | 4659 | size_t framesReady = mFrameCount - mRsmpInIndex; | 
|  | 4660 | int channelCount; | 
|  | 4661 |  | 
|  | 4662 | if (framesReady == 0) { | 
| Dima Zavin | 799a70e | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 4663 | mBytesRead = mInput->stream->read(mInput->stream, mRsmpInBuffer, mInputBytes); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4664 | if (mBytesRead < 0) { | 
| Steve Block | 29357bc | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 4665 | ALOGE("RecordThread::getNextBuffer() Error reading audio input"); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4666 | if (mActiveTrack->mState == TrackBase::ACTIVE) { | 
|  | 4667 | // Force input into standby so that it tries to | 
|  | 4668 | // recover at next read attempt | 
| Dima Zavin | 799a70e | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 4669 | mInput->stream->common.standby(&mInput->stream->common); | 
| Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 4670 | usleep(kRecordThreadSleepUs); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4671 | } | 
| Glenn Kasten | e0feee3 | 2011-12-13 11:53:26 -0800 | [diff] [blame] | 4672 | buffer->raw = NULL; | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4673 | buffer->frameCount = 0; | 
|  | 4674 | return NOT_ENOUGH_DATA; | 
|  | 4675 | } | 
|  | 4676 | mRsmpInIndex = 0; | 
|  | 4677 | framesReady = mFrameCount; | 
|  | 4678 | } | 
|  | 4679 |  | 
|  | 4680 | if (framesReq > framesReady) { | 
|  | 4681 | framesReq = framesReady; | 
|  | 4682 | } | 
|  | 4683 |  | 
|  | 4684 | if (mChannelCount == 1 && mReqChannelCount == 2) { | 
|  | 4685 | channelCount = 1; | 
|  | 4686 | } else { | 
|  | 4687 | channelCount = 2; | 
|  | 4688 | } | 
|  | 4689 | buffer->raw = mRsmpInBuffer + mRsmpInIndex * channelCount; | 
|  | 4690 | buffer->frameCount = framesReq; | 
|  | 4691 | return NO_ERROR; | 
|  | 4692 | } | 
|  | 4693 |  | 
|  | 4694 | void AudioFlinger::RecordThread::releaseBuffer(AudioBufferProvider::Buffer* buffer) | 
|  | 4695 | { | 
|  | 4696 | mRsmpInIndex += buffer->frameCount; | 
|  | 4697 | buffer->frameCount = 0; | 
|  | 4698 | } | 
|  | 4699 |  | 
|  | 4700 | bool AudioFlinger::RecordThread::checkForNewParameters_l() | 
|  | 4701 | { | 
|  | 4702 | bool reconfig = false; | 
|  | 4703 |  | 
|  | 4704 | while (!mNewParameters.isEmpty()) { | 
|  | 4705 | status_t status = NO_ERROR; | 
|  | 4706 | String8 keyValuePair = mNewParameters[0]; | 
|  | 4707 | AudioParameter param = AudioParameter(keyValuePair); | 
|  | 4708 | int value; | 
| Glenn Kasten | 58f3021 | 2012-01-12 12:27:51 -0800 | [diff] [blame] | 4709 | audio_format_t reqFormat = mFormat; | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4710 | int reqSamplingRate = mReqSampleRate; | 
|  | 4711 | int reqChannelCount = mReqChannelCount; | 
|  | 4712 |  | 
|  | 4713 | if (param.getInt(String8(AudioParameter::keySamplingRate), value) == NO_ERROR) { | 
|  | 4714 | reqSamplingRate = value; | 
|  | 4715 | reconfig = true; | 
|  | 4716 | } | 
|  | 4717 | if (param.getInt(String8(AudioParameter::keyFormat), value) == NO_ERROR) { | 
| Glenn Kasten | 58f3021 | 2012-01-12 12:27:51 -0800 | [diff] [blame] | 4718 | reqFormat = (audio_format_t) value; | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4719 | reconfig = true; | 
|  | 4720 | } | 
|  | 4721 | if (param.getInt(String8(AudioParameter::keyChannels), value) == NO_ERROR) { | 
| Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 4722 | reqChannelCount = popcount(value); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4723 | reconfig = true; | 
|  | 4724 | } | 
|  | 4725 | if (param.getInt(String8(AudioParameter::keyFrameCount), value) == NO_ERROR) { | 
|  | 4726 | // do not accept frame count changes if tracks are open as the track buffer | 
|  | 4727 | // size depends on frame count and correct behavior would not be garantied | 
|  | 4728 | // if frame count is changed after track creation | 
|  | 4729 | if (mActiveTrack != 0) { | 
|  | 4730 | status = INVALID_OPERATION; | 
|  | 4731 | } else { | 
|  | 4732 | reconfig = true; | 
|  | 4733 | } | 
|  | 4734 | } | 
| Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 4735 | if (param.getInt(String8(AudioParameter::keyRouting), value) == NO_ERROR) { | 
|  | 4736 | // forward device change to effects that have requested to be | 
|  | 4737 | // aware of attached audio device. | 
|  | 4738 | for (size_t i = 0; i < mEffectChains.size(); i++) { | 
|  | 4739 | mEffectChains[i]->setDevice_l(value); | 
|  | 4740 | } | 
|  | 4741 | // store input device and output device but do not forward output device to audio HAL. | 
|  | 4742 | // Note that status is ignored by the caller for output device | 
|  | 4743 | // (see AudioFlinger::setParameters() | 
|  | 4744 | if (value & AUDIO_DEVICE_OUT_ALL) { | 
|  | 4745 | mDevice &= (uint32_t)~(value & AUDIO_DEVICE_OUT_ALL); | 
|  | 4746 | status = BAD_VALUE; | 
|  | 4747 | } else { | 
|  | 4748 | mDevice &= (uint32_t)~(value & AUDIO_DEVICE_IN_ALL); | 
| Eric Laurent | 59bd0da | 2011-08-01 09:52:20 -0700 | [diff] [blame] | 4749 | // disable AEC and NS if the device is a BT SCO headset supporting those pre processings | 
|  | 4750 | if (mTrack != NULL) { | 
|  | 4751 | bool suspend = audio_is_bluetooth_sco_device( | 
| Eric Laurent | bee5337 | 2011-08-29 12:42:48 -0700 | [diff] [blame] | 4752 | (audio_devices_t)value) && mAudioFlinger->btNrecIsOff(); | 
| Eric Laurent | 59bd0da | 2011-08-01 09:52:20 -0700 | [diff] [blame] | 4753 | setEffectSuspended_l(FX_IID_AEC, suspend, mTrack->sessionId()); | 
|  | 4754 | setEffectSuspended_l(FX_IID_NS, suspend, mTrack->sessionId()); | 
|  | 4755 | } | 
| Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 4756 | } | 
|  | 4757 | mDevice |= (uint32_t)value; | 
|  | 4758 | } | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4759 | if (status == NO_ERROR) { | 
| Dima Zavin | 799a70e | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 4760 | status = mInput->stream->common.set_parameters(&mInput->stream->common, keyValuePair.string()); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4761 | if (status == INVALID_OPERATION) { | 
| Dima Zavin | 799a70e | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 4762 | mInput->stream->common.standby(&mInput->stream->common); | 
|  | 4763 | status = mInput->stream->common.set_parameters(&mInput->stream->common, keyValuePair.string()); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4764 | } | 
|  | 4765 | if (reconfig) { | 
|  | 4766 | if (status == BAD_VALUE && | 
| Dima Zavin | 799a70e | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 4767 | reqFormat == mInput->stream->common.get_format(&mInput->stream->common) && | 
| Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 4768 | reqFormat == AUDIO_FORMAT_PCM_16_BIT && | 
| Dima Zavin | 799a70e | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 4769 | ((int)mInput->stream->common.get_sample_rate(&mInput->stream->common) <= (2 * reqSamplingRate)) && | 
|  | 4770 | (popcount(mInput->stream->common.get_channels(&mInput->stream->common)) < 3) && | 
| Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 4771 | (reqChannelCount < 3)) { | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4772 | status = NO_ERROR; | 
|  | 4773 | } | 
|  | 4774 | if (status == NO_ERROR) { | 
|  | 4775 | readInputParameters(); | 
|  | 4776 | sendConfigEvent_l(AudioSystem::INPUT_CONFIG_CHANGED); | 
|  | 4777 | } | 
|  | 4778 | } | 
|  | 4779 | } | 
|  | 4780 |  | 
|  | 4781 | mNewParameters.removeAt(0); | 
|  | 4782 |  | 
|  | 4783 | mParamStatus = status; | 
|  | 4784 | mParamCond.signal(); | 
| Eric Laurent | 60cd0a0 | 2011-09-13 11:40:21 -0700 | [diff] [blame] | 4785 | // wait for condition with time out in case the thread calling ThreadBase::setParameters() | 
|  | 4786 | // already timed out waiting for the status and will never signal the condition. | 
| Glenn Kasten | 7dede87 | 2011-12-13 11:04:14 -0800 | [diff] [blame] | 4787 | mWaitWorkCV.waitRelative(mLock, kSetParametersTimeoutNs); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4788 | } | 
|  | 4789 | return reconfig; | 
|  | 4790 | } | 
|  | 4791 |  | 
|  | 4792 | String8 AudioFlinger::RecordThread::getParameters(const String8& keys) | 
|  | 4793 | { | 
| Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 4794 | char *s; | 
| Eric Laurent | b8ba0a9 | 2011-08-07 16:32:26 -0700 | [diff] [blame] | 4795 | String8 out_s8 = String8(); | 
|  | 4796 |  | 
|  | 4797 | Mutex::Autolock _l(mLock); | 
|  | 4798 | if (initCheck() != NO_ERROR) { | 
|  | 4799 | return out_s8; | 
|  | 4800 | } | 
| Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 4801 |  | 
| Dima Zavin | 799a70e | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 4802 | s = mInput->stream->common.get_parameters(&mInput->stream->common, keys.string()); | 
| Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 4803 | out_s8 = String8(s); | 
|  | 4804 | free(s); | 
|  | 4805 | return out_s8; | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4806 | } | 
|  | 4807 |  | 
|  | 4808 | void AudioFlinger::RecordThread::audioConfigChanged_l(int event, int param) { | 
|  | 4809 | AudioSystem::OutputDescriptor desc; | 
|  | 4810 | void *param2 = 0; | 
|  | 4811 |  | 
|  | 4812 | switch (event) { | 
|  | 4813 | case AudioSystem::INPUT_OPENED: | 
|  | 4814 | case AudioSystem::INPUT_CONFIG_CHANGED: | 
| Jean-Michel Trivi | 0d255b2 | 2011-05-24 15:53:33 -0700 | [diff] [blame] | 4815 | desc.channels = mChannelMask; | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4816 | desc.samplingRate = mSampleRate; | 
|  | 4817 | desc.format = mFormat; | 
|  | 4818 | desc.frameCount = mFrameCount; | 
|  | 4819 | desc.latency = 0; | 
|  | 4820 | param2 = &desc; | 
|  | 4821 | break; | 
|  | 4822 |  | 
|  | 4823 | case AudioSystem::INPUT_CLOSED: | 
|  | 4824 | default: | 
|  | 4825 | break; | 
|  | 4826 | } | 
|  | 4827 | mAudioFlinger->audioConfigChanged_l(event, mId, param2); | 
|  | 4828 | } | 
|  | 4829 |  | 
|  | 4830 | void AudioFlinger::RecordThread::readInputParameters() | 
|  | 4831 | { | 
|  | 4832 | if (mRsmpInBuffer) delete mRsmpInBuffer; | 
|  | 4833 | if (mRsmpOutBuffer) delete mRsmpOutBuffer; | 
|  | 4834 | if (mResampler) delete mResampler; | 
| Glenn Kasten | e0feee3 | 2011-12-13 11:53:26 -0800 | [diff] [blame] | 4835 | mResampler = NULL; | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4836 |  | 
| Dima Zavin | 799a70e | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 4837 | mSampleRate = mInput->stream->common.get_sample_rate(&mInput->stream->common); | 
| Jean-Michel Trivi | 0d255b2 | 2011-05-24 15:53:33 -0700 | [diff] [blame] | 4838 | mChannelMask = mInput->stream->common.get_channels(&mInput->stream->common); | 
|  | 4839 | mChannelCount = (uint16_t)popcount(mChannelMask); | 
| Dima Zavin | 799a70e | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 4840 | mFormat = mInput->stream->common.get_format(&mInput->stream->common); | 
| Glenn Kasten | b998065 | 2012-01-11 09:48:27 -0800 | [diff] [blame] | 4841 | mFrameSize = audio_stream_frame_size(&mInput->stream->common); | 
| Dima Zavin | 799a70e | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 4842 | mInputBytes = mInput->stream->common.get_buffer_size(&mInput->stream->common); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4843 | mFrameCount = mInputBytes / mFrameSize; | 
|  | 4844 | mRsmpInBuffer = new int16_t[mFrameCount * mChannelCount]; | 
|  | 4845 |  | 
|  | 4846 | if (mSampleRate != mReqSampleRate && mChannelCount < 3 && mReqChannelCount < 3) | 
|  | 4847 | { | 
|  | 4848 | int channelCount; | 
|  | 4849 | // optmization: if mono to mono, use the resampler in stereo to stereo mode to avoid | 
|  | 4850 | // stereo to mono post process as the resampler always outputs stereo. | 
|  | 4851 | if (mChannelCount == 1 && mReqChannelCount == 2) { | 
|  | 4852 | channelCount = 1; | 
|  | 4853 | } else { | 
|  | 4854 | channelCount = 2; | 
|  | 4855 | } | 
|  | 4856 | mResampler = AudioResampler::create(16, channelCount, mReqSampleRate); | 
|  | 4857 | mResampler->setSampleRate(mSampleRate); | 
|  | 4858 | mResampler->setVolume(AudioMixer::UNITY_GAIN, AudioMixer::UNITY_GAIN); | 
|  | 4859 | mRsmpOutBuffer = new int32_t[mFrameCount * 2]; | 
|  | 4860 |  | 
|  | 4861 | // optmization: if mono to mono, alter input frame count as if we were inputing stereo samples | 
|  | 4862 | if (mChannelCount == 1 && mReqChannelCount == 1) { | 
|  | 4863 | mFrameCount >>= 1; | 
|  | 4864 | } | 
|  | 4865 |  | 
|  | 4866 | } | 
|  | 4867 | mRsmpInIndex = mFrameCount; | 
|  | 4868 | } | 
|  | 4869 |  | 
|  | 4870 | unsigned int AudioFlinger::RecordThread::getInputFramesLost() | 
|  | 4871 | { | 
| Eric Laurent | b8ba0a9 | 2011-08-07 16:32:26 -0700 | [diff] [blame] | 4872 | Mutex::Autolock _l(mLock); | 
|  | 4873 | if (initCheck() != NO_ERROR) { | 
|  | 4874 | return 0; | 
|  | 4875 | } | 
|  | 4876 |  | 
| Dima Zavin | 799a70e | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 4877 | return mInput->stream->get_input_frames_lost(mInput->stream); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4878 | } | 
|  | 4879 |  | 
| Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 4880 | uint32_t AudioFlinger::RecordThread::hasAudioSession(int sessionId) | 
|  | 4881 | { | 
|  | 4882 | Mutex::Autolock _l(mLock); | 
|  | 4883 | uint32_t result = 0; | 
|  | 4884 | if (getEffectChain_l(sessionId) != 0) { | 
|  | 4885 | result = EFFECT_SESSION; | 
|  | 4886 | } | 
|  | 4887 |  | 
|  | 4888 | if (mTrack != NULL && sessionId == mTrack->sessionId()) { | 
|  | 4889 | result |= TRACK_SESSION; | 
|  | 4890 | } | 
|  | 4891 |  | 
|  | 4892 | return result; | 
|  | 4893 | } | 
|  | 4894 |  | 
| Eric Laurent | 59bd0da | 2011-08-01 09:52:20 -0700 | [diff] [blame] | 4895 | AudioFlinger::RecordThread::RecordTrack* AudioFlinger::RecordThread::track() | 
|  | 4896 | { | 
|  | 4897 | Mutex::Autolock _l(mLock); | 
|  | 4898 | return mTrack; | 
|  | 4899 | } | 
|  | 4900 |  | 
| Eric Laurent | b8ba0a9 | 2011-08-07 16:32:26 -0700 | [diff] [blame] | 4901 | AudioFlinger::AudioStreamIn* AudioFlinger::RecordThread::getInput() | 
|  | 4902 | { | 
|  | 4903 | Mutex::Autolock _l(mLock); | 
|  | 4904 | return mInput; | 
|  | 4905 | } | 
|  | 4906 |  | 
|  | 4907 | AudioFlinger::AudioStreamIn* AudioFlinger::RecordThread::clearInput() | 
|  | 4908 | { | 
|  | 4909 | Mutex::Autolock _l(mLock); | 
|  | 4910 | AudioStreamIn *input = mInput; | 
|  | 4911 | mInput = NULL; | 
|  | 4912 | return input; | 
|  | 4913 | } | 
|  | 4914 |  | 
|  | 4915 | // this method must always be called either with ThreadBase mLock held or inside the thread loop | 
|  | 4916 | audio_stream_t* AudioFlinger::RecordThread::stream() | 
|  | 4917 | { | 
|  | 4918 | if (mInput == NULL) { | 
|  | 4919 | return NULL; | 
|  | 4920 | } | 
|  | 4921 | return &mInput->stream->common; | 
|  | 4922 | } | 
|  | 4923 |  | 
|  | 4924 |  | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4925 | // ---------------------------------------------------------------------------- | 
|  | 4926 |  | 
|  | 4927 | int AudioFlinger::openOutput(uint32_t *pDevices, | 
|  | 4928 | uint32_t *pSamplingRate, | 
| Glenn Kasten | 58f3021 | 2012-01-12 12:27:51 -0800 | [diff] [blame] | 4929 | audio_format_t *pFormat, | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4930 | uint32_t *pChannels, | 
|  | 4931 | uint32_t *pLatencyMs, | 
|  | 4932 | uint32_t flags) | 
|  | 4933 | { | 
|  | 4934 | status_t status; | 
|  | 4935 | PlaybackThread *thread = NULL; | 
|  | 4936 | mHardwareStatus = AUDIO_HW_OUTPUT_OPEN; | 
|  | 4937 | uint32_t samplingRate = pSamplingRate ? *pSamplingRate : 0; | 
| Glenn Kasten | 58f3021 | 2012-01-12 12:27:51 -0800 | [diff] [blame] | 4938 | audio_format_t format = pFormat ? *pFormat : AUDIO_FORMAT_DEFAULT; | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4939 | uint32_t channels = pChannels ? *pChannels : 0; | 
|  | 4940 | uint32_t latency = pLatencyMs ? *pLatencyMs : 0; | 
| Dima Zavin | 799a70e | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 4941 | audio_stream_out_t *outStream; | 
|  | 4942 | audio_hw_device_t *outHwDev; | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4943 |  | 
| Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 4944 | ALOGV("openOutput(), Device %x, SamplingRate %d, Format %d, Channels %x, flags %x", | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4945 | pDevices ? *pDevices : 0, | 
|  | 4946 | samplingRate, | 
|  | 4947 | format, | 
|  | 4948 | channels, | 
|  | 4949 | flags); | 
|  | 4950 |  | 
|  | 4951 | if (pDevices == NULL || *pDevices == 0) { | 
|  | 4952 | return 0; | 
|  | 4953 | } | 
| Dima Zavin | 799a70e | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 4954 |  | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4955 | Mutex::Autolock _l(mLock); | 
|  | 4956 |  | 
| Dima Zavin | 799a70e | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 4957 | outHwDev = findSuitableHwDev_l(*pDevices); | 
|  | 4958 | if (outHwDev == NULL) | 
|  | 4959 | return 0; | 
|  | 4960 |  | 
| Glenn Kasten | 58f3021 | 2012-01-12 12:27:51 -0800 | [diff] [blame] | 4961 | status = outHwDev->open_output_stream(outHwDev, *pDevices, &format, | 
| Dima Zavin | 799a70e | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 4962 | &channels, &samplingRate, &outStream); | 
| Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 4963 | ALOGV("openOutput() openOutputStream returned output %p, SamplingRate %d, Format %d, Channels %x, status %d", | 
| Dima Zavin | 799a70e | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 4964 | outStream, | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4965 | samplingRate, | 
|  | 4966 | format, | 
|  | 4967 | channels, | 
|  | 4968 | status); | 
|  | 4969 |  | 
|  | 4970 | mHardwareStatus = AUDIO_HW_IDLE; | 
| Dima Zavin | 799a70e | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 4971 | if (outStream != NULL) { | 
|  | 4972 | AudioStreamOut *output = new AudioStreamOut(outHwDev, outStream); | 
| Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 4973 | int id = nextUniqueId(); | 
| Dima Zavin | 799a70e | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 4974 |  | 
| Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 4975 | if ((flags & AUDIO_POLICY_OUTPUT_FLAG_DIRECT) || | 
|  | 4976 | (format != AUDIO_FORMAT_PCM_16_BIT) || | 
|  | 4977 | (channels != AUDIO_CHANNEL_OUT_STEREO)) { | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4978 | thread = new DirectOutputThread(this, output, id, *pDevices); | 
| Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 4979 | ALOGV("openOutput() created direct output: ID %d thread %p", id, thread); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4980 | } else { | 
|  | 4981 | thread = new MixerThread(this, output, id, *pDevices); | 
| Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 4982 | ALOGV("openOutput() created mixer output: ID %d thread %p", id, thread); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4983 | } | 
|  | 4984 | mPlaybackThreads.add(id, thread); | 
|  | 4985 |  | 
|  | 4986 | if (pSamplingRate) *pSamplingRate = samplingRate; | 
|  | 4987 | if (pFormat) *pFormat = format; | 
|  | 4988 | if (pChannels) *pChannels = channels; | 
|  | 4989 | if (pLatencyMs) *pLatencyMs = thread->latency(); | 
|  | 4990 |  | 
|  | 4991 | // notify client processes of the new output creation | 
|  | 4992 | thread->audioConfigChanged_l(AudioSystem::OUTPUT_OPENED); | 
|  | 4993 | return id; | 
|  | 4994 | } | 
|  | 4995 |  | 
|  | 4996 | return 0; | 
|  | 4997 | } | 
|  | 4998 |  | 
|  | 4999 | int AudioFlinger::openDuplicateOutput(int output1, int output2) | 
|  | 5000 | { | 
|  | 5001 | Mutex::Autolock _l(mLock); | 
|  | 5002 | MixerThread *thread1 = checkMixerThread_l(output1); | 
|  | 5003 | MixerThread *thread2 = checkMixerThread_l(output2); | 
|  | 5004 |  | 
|  | 5005 | if (thread1 == NULL || thread2 == NULL) { | 
| Steve Block | 5ff1dd5 | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 5006 | ALOGW("openDuplicateOutput() wrong output mixer type for output %d or %d", output1, output2); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5007 | return 0; | 
|  | 5008 | } | 
|  | 5009 |  | 
| Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 5010 | int id = nextUniqueId(); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5011 | DuplicatingThread *thread = new DuplicatingThread(this, thread1, id); | 
|  | 5012 | thread->addOutputTrack(thread2); | 
|  | 5013 | mPlaybackThreads.add(id, thread); | 
|  | 5014 | // notify client processes of the new output creation | 
|  | 5015 | thread->audioConfigChanged_l(AudioSystem::OUTPUT_OPENED); | 
|  | 5016 | return id; | 
|  | 5017 | } | 
|  | 5018 |  | 
|  | 5019 | status_t AudioFlinger::closeOutput(int output) | 
|  | 5020 | { | 
|  | 5021 | // keep strong reference on the playback thread so that | 
|  | 5022 | // it is not destroyed while exit() is executed | 
|  | 5023 | sp <PlaybackThread> thread; | 
|  | 5024 | { | 
|  | 5025 | Mutex::Autolock _l(mLock); | 
|  | 5026 | thread = checkPlaybackThread_l(output); | 
|  | 5027 | if (thread == NULL) { | 
|  | 5028 | return BAD_VALUE; | 
|  | 5029 | } | 
|  | 5030 |  | 
| Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 5031 | ALOGV("closeOutput() %d", output); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5032 |  | 
| Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 5033 | if (thread->type() == ThreadBase::MIXER) { | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5034 | for (size_t i = 0; i < mPlaybackThreads.size(); i++) { | 
| Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 5035 | if (mPlaybackThreads.valueAt(i)->type() == ThreadBase::DUPLICATING) { | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5036 | DuplicatingThread *dupThread = (DuplicatingThread *)mPlaybackThreads.valueAt(i).get(); | 
|  | 5037 | dupThread->removeOutputTrack((MixerThread *)thread.get()); | 
|  | 5038 | } | 
|  | 5039 | } | 
|  | 5040 | } | 
|  | 5041 | void *param2 = 0; | 
|  | 5042 | audioConfigChanged_l(AudioSystem::OUTPUT_CLOSED, output, param2); | 
|  | 5043 | mPlaybackThreads.removeItem(output); | 
|  | 5044 | } | 
|  | 5045 | thread->exit(); | 
|  | 5046 |  | 
| Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 5047 | if (thread->type() != ThreadBase::DUPLICATING) { | 
| Eric Laurent | b8ba0a9 | 2011-08-07 16:32:26 -0700 | [diff] [blame] | 5048 | AudioStreamOut *out = thread->clearOutput(); | 
|  | 5049 | // from now on thread->mOutput is NULL | 
| Dima Zavin | 799a70e | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 5050 | out->hwDev->close_output_stream(out->hwDev, out->stream); | 
|  | 5051 | delete out; | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5052 | } | 
|  | 5053 | return NO_ERROR; | 
|  | 5054 | } | 
|  | 5055 |  | 
|  | 5056 | status_t AudioFlinger::suspendOutput(int output) | 
|  | 5057 | { | 
|  | 5058 | Mutex::Autolock _l(mLock); | 
|  | 5059 | PlaybackThread *thread = checkPlaybackThread_l(output); | 
|  | 5060 |  | 
|  | 5061 | if (thread == NULL) { | 
|  | 5062 | return BAD_VALUE; | 
|  | 5063 | } | 
|  | 5064 |  | 
| Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 5065 | ALOGV("suspendOutput() %d", output); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5066 | thread->suspend(); | 
|  | 5067 |  | 
|  | 5068 | return NO_ERROR; | 
|  | 5069 | } | 
|  | 5070 |  | 
|  | 5071 | status_t AudioFlinger::restoreOutput(int output) | 
|  | 5072 | { | 
|  | 5073 | Mutex::Autolock _l(mLock); | 
|  | 5074 | PlaybackThread *thread = checkPlaybackThread_l(output); | 
|  | 5075 |  | 
|  | 5076 | if (thread == NULL) { | 
|  | 5077 | return BAD_VALUE; | 
|  | 5078 | } | 
|  | 5079 |  | 
| Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 5080 | ALOGV("restoreOutput() %d", output); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5081 |  | 
|  | 5082 | thread->restore(); | 
|  | 5083 |  | 
|  | 5084 | return NO_ERROR; | 
|  | 5085 | } | 
|  | 5086 |  | 
|  | 5087 | int AudioFlinger::openInput(uint32_t *pDevices, | 
|  | 5088 | uint32_t *pSamplingRate, | 
| Glenn Kasten | 58f3021 | 2012-01-12 12:27:51 -0800 | [diff] [blame] | 5089 | audio_format_t *pFormat, | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5090 | uint32_t *pChannels, | 
|  | 5091 | uint32_t acoustics) | 
|  | 5092 | { | 
|  | 5093 | status_t status; | 
|  | 5094 | RecordThread *thread = NULL; | 
|  | 5095 | uint32_t samplingRate = pSamplingRate ? *pSamplingRate : 0; | 
| Glenn Kasten | 58f3021 | 2012-01-12 12:27:51 -0800 | [diff] [blame] | 5096 | audio_format_t format = pFormat ? *pFormat : AUDIO_FORMAT_DEFAULT; | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5097 | uint32_t channels = pChannels ? *pChannels : 0; | 
|  | 5098 | uint32_t reqSamplingRate = samplingRate; | 
| Glenn Kasten | 58f3021 | 2012-01-12 12:27:51 -0800 | [diff] [blame] | 5099 | audio_format_t reqFormat = format; | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5100 | uint32_t reqChannels = channels; | 
| Dima Zavin | 799a70e | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 5101 | audio_stream_in_t *inStream; | 
|  | 5102 | audio_hw_device_t *inHwDev; | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5103 |  | 
|  | 5104 | if (pDevices == NULL || *pDevices == 0) { | 
|  | 5105 | return 0; | 
|  | 5106 | } | 
| Dima Zavin | 799a70e | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 5107 |  | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5108 | Mutex::Autolock _l(mLock); | 
|  | 5109 |  | 
| Dima Zavin | 799a70e | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 5110 | inHwDev = findSuitableHwDev_l(*pDevices); | 
|  | 5111 | if (inHwDev == NULL) | 
|  | 5112 | return 0; | 
|  | 5113 |  | 
| Glenn Kasten | 58f3021 | 2012-01-12 12:27:51 -0800 | [diff] [blame] | 5114 | status = inHwDev->open_input_stream(inHwDev, *pDevices, &format, | 
| Dima Zavin | 799a70e | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 5115 | &channels, &samplingRate, | 
| Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 5116 | (audio_in_acoustics_t)acoustics, | 
| Dima Zavin | 799a70e | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 5117 | &inStream); | 
| Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 5118 | ALOGV("openInput() openInputStream returned input %p, SamplingRate %d, Format %d, Channels %x, acoustics %x, status %d", | 
| Dima Zavin | 799a70e | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 5119 | inStream, | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5120 | samplingRate, | 
|  | 5121 | format, | 
|  | 5122 | channels, | 
|  | 5123 | acoustics, | 
|  | 5124 | status); | 
|  | 5125 |  | 
|  | 5126 | // If the input could not be opened with the requested parameters and we can handle the conversion internally, | 
|  | 5127 | // try to open again with the proposed parameters. The AudioFlinger can resample the input and do mono to stereo | 
|  | 5128 | // or stereo to mono conversions on 16 bit PCM inputs. | 
| Dima Zavin | 799a70e | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 5129 | if (inStream == NULL && status == BAD_VALUE && | 
| Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 5130 | reqFormat == format && format == AUDIO_FORMAT_PCM_16_BIT && | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5131 | (samplingRate <= 2 * reqSamplingRate) && | 
| Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 5132 | (popcount(channels) < 3) && (popcount(reqChannels) < 3)) { | 
| Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 5133 | ALOGV("openInput() reopening with proposed sampling rate and channels"); | 
| Glenn Kasten | 58f3021 | 2012-01-12 12:27:51 -0800 | [diff] [blame] | 5134 | status = inHwDev->open_input_stream(inHwDev, *pDevices, &format, | 
| Dima Zavin | 799a70e | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 5135 | &channels, &samplingRate, | 
| Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 5136 | (audio_in_acoustics_t)acoustics, | 
| Dima Zavin | 799a70e | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 5137 | &inStream); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5138 | } | 
|  | 5139 |  | 
| Dima Zavin | 799a70e | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 5140 | if (inStream != NULL) { | 
|  | 5141 | AudioStreamIn *input = new AudioStreamIn(inHwDev, inStream); | 
|  | 5142 |  | 
| Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 5143 | int id = nextUniqueId(); | 
|  | 5144 | // Start record thread | 
|  | 5145 | // RecorThread require both input and output device indication to forward to audio | 
|  | 5146 | // pre processing modules | 
|  | 5147 | uint32_t device = (*pDevices) | primaryOutputDevice_l(); | 
|  | 5148 | thread = new RecordThread(this, | 
|  | 5149 | input, | 
|  | 5150 | reqSamplingRate, | 
|  | 5151 | reqChannels, | 
|  | 5152 | id, | 
|  | 5153 | device); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5154 | mRecordThreads.add(id, thread); | 
| Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 5155 | ALOGV("openInput() created record thread: ID %d thread %p", id, thread); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5156 | if (pSamplingRate) *pSamplingRate = reqSamplingRate; | 
|  | 5157 | if (pFormat) *pFormat = format; | 
|  | 5158 | if (pChannels) *pChannels = reqChannels; | 
|  | 5159 |  | 
| Dima Zavin | 799a70e | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 5160 | input->stream->common.standby(&input->stream->common); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5161 |  | 
|  | 5162 | // notify client processes of the new input creation | 
|  | 5163 | thread->audioConfigChanged_l(AudioSystem::INPUT_OPENED); | 
|  | 5164 | return id; | 
|  | 5165 | } | 
|  | 5166 |  | 
|  | 5167 | return 0; | 
|  | 5168 | } | 
|  | 5169 |  | 
|  | 5170 | status_t AudioFlinger::closeInput(int input) | 
|  | 5171 | { | 
|  | 5172 | // keep strong reference on the record thread so that | 
|  | 5173 | // it is not destroyed while exit() is executed | 
|  | 5174 | sp <RecordThread> thread; | 
|  | 5175 | { | 
|  | 5176 | Mutex::Autolock _l(mLock); | 
|  | 5177 | thread = checkRecordThread_l(input); | 
|  | 5178 | if (thread == NULL) { | 
|  | 5179 | return BAD_VALUE; | 
|  | 5180 | } | 
|  | 5181 |  | 
| Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 5182 | ALOGV("closeInput() %d", input); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5183 | void *param2 = 0; | 
|  | 5184 | audioConfigChanged_l(AudioSystem::INPUT_CLOSED, input, param2); | 
|  | 5185 | mRecordThreads.removeItem(input); | 
|  | 5186 | } | 
|  | 5187 | thread->exit(); | 
|  | 5188 |  | 
| Eric Laurent | b8ba0a9 | 2011-08-07 16:32:26 -0700 | [diff] [blame] | 5189 | AudioStreamIn *in = thread->clearInput(); | 
|  | 5190 | // from now on thread->mInput is NULL | 
| Dima Zavin | 799a70e | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 5191 | in->hwDev->close_input_stream(in->hwDev, in->stream); | 
|  | 5192 | delete in; | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5193 |  | 
|  | 5194 | return NO_ERROR; | 
|  | 5195 | } | 
|  | 5196 |  | 
| Glenn Kasten | fff6d71 | 2012-01-12 16:38:12 -0800 | [diff] [blame] | 5197 | status_t AudioFlinger::setStreamOutput(audio_stream_type_t stream, int output) | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5198 | { | 
|  | 5199 | Mutex::Autolock _l(mLock); | 
|  | 5200 | MixerThread *dstThread = checkMixerThread_l(output); | 
|  | 5201 | if (dstThread == NULL) { | 
| Steve Block | 5ff1dd5 | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 5202 | ALOGW("setStreamOutput() bad output id %d", output); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5203 | return BAD_VALUE; | 
|  | 5204 | } | 
|  | 5205 |  | 
| Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 5206 | ALOGV("setStreamOutput() stream %d to output %d", stream, output); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5207 | audioConfigChanged_l(AudioSystem::STREAM_CONFIG_CHANGED, output, &stream); | 
|  | 5208 |  | 
| Eric Laurent | 9f6530f | 2011-08-30 10:18:54 -0700 | [diff] [blame] | 5209 | dstThread->setStreamValid(stream, true); | 
|  | 5210 |  | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5211 | for (size_t i = 0; i < mPlaybackThreads.size(); i++) { | 
|  | 5212 | PlaybackThread *thread = mPlaybackThreads.valueAt(i).get(); | 
|  | 5213 | if (thread != dstThread && | 
| Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 5214 | thread->type() != ThreadBase::DIRECT) { | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5215 | MixerThread *srcThread = (MixerThread *)thread; | 
| Eric Laurent | 9f6530f | 2011-08-30 10:18:54 -0700 | [diff] [blame] | 5216 | srcThread->setStreamValid(stream, false); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5217 | srcThread->invalidateTracks(stream); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5218 | } | 
| Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 5219 | } | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5220 |  | 
|  | 5221 | return NO_ERROR; | 
|  | 5222 | } | 
|  | 5223 |  | 
|  | 5224 |  | 
|  | 5225 | int AudioFlinger::newAudioSessionId() | 
|  | 5226 | { | 
| Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 5227 | return nextUniqueId(); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5228 | } | 
|  | 5229 |  | 
| Marco Nelissen | 3a34bef | 2011-08-02 13:33:41 -0700 | [diff] [blame] | 5230 | void AudioFlinger::acquireAudioSessionId(int audioSession) | 
|  | 5231 | { | 
|  | 5232 | Mutex::Autolock _l(mLock); | 
|  | 5233 | int caller = IPCThreadState::self()->getCallingPid(); | 
| Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 5234 | ALOGV("acquiring %d from %d", audioSession, caller); | 
| Marco Nelissen | 3a34bef | 2011-08-02 13:33:41 -0700 | [diff] [blame] | 5235 | int num = mAudioSessionRefs.size(); | 
|  | 5236 | for (int i = 0; i< num; i++) { | 
|  | 5237 | AudioSessionRef *ref = mAudioSessionRefs.editItemAt(i); | 
|  | 5238 | if (ref->sessionid == audioSession && ref->pid == caller) { | 
|  | 5239 | ref->cnt++; | 
| Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 5240 | ALOGV(" incremented refcount to %d", ref->cnt); | 
| Marco Nelissen | 3a34bef | 2011-08-02 13:33:41 -0700 | [diff] [blame] | 5241 | return; | 
|  | 5242 | } | 
|  | 5243 | } | 
|  | 5244 | AudioSessionRef *ref = new AudioSessionRef(); | 
|  | 5245 | ref->sessionid = audioSession; | 
|  | 5246 | ref->pid = caller; | 
|  | 5247 | ref->cnt = 1; | 
|  | 5248 | mAudioSessionRefs.push(ref); | 
| Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 5249 | ALOGV(" added new entry for %d", ref->sessionid); | 
| Marco Nelissen | 3a34bef | 2011-08-02 13:33:41 -0700 | [diff] [blame] | 5250 | } | 
|  | 5251 |  | 
|  | 5252 | void AudioFlinger::releaseAudioSessionId(int audioSession) | 
|  | 5253 | { | 
|  | 5254 | Mutex::Autolock _l(mLock); | 
|  | 5255 | int caller = IPCThreadState::self()->getCallingPid(); | 
| Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 5256 | ALOGV("releasing %d from %d", audioSession, caller); | 
| Marco Nelissen | 3a34bef | 2011-08-02 13:33:41 -0700 | [diff] [blame] | 5257 | int num = mAudioSessionRefs.size(); | 
|  | 5258 | for (int i = 0; i< num; i++) { | 
|  | 5259 | AudioSessionRef *ref = mAudioSessionRefs.itemAt(i); | 
|  | 5260 | if (ref->sessionid == audioSession && ref->pid == caller) { | 
|  | 5261 | ref->cnt--; | 
| Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 5262 | ALOGV(" decremented refcount to %d", ref->cnt); | 
| Marco Nelissen | 3a34bef | 2011-08-02 13:33:41 -0700 | [diff] [blame] | 5263 | if (ref->cnt == 0) { | 
|  | 5264 | mAudioSessionRefs.removeAt(i); | 
|  | 5265 | delete ref; | 
|  | 5266 | purgeStaleEffects_l(); | 
|  | 5267 | } | 
|  | 5268 | return; | 
|  | 5269 | } | 
|  | 5270 | } | 
| Steve Block | 5ff1dd5 | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 5271 | ALOGW("session id %d not found for pid %d", audioSession, caller); | 
| Marco Nelissen | 3a34bef | 2011-08-02 13:33:41 -0700 | [diff] [blame] | 5272 | } | 
|  | 5273 |  | 
|  | 5274 | void AudioFlinger::purgeStaleEffects_l() { | 
|  | 5275 |  | 
| Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 5276 | ALOGV("purging stale effects"); | 
| Marco Nelissen | 3a34bef | 2011-08-02 13:33:41 -0700 | [diff] [blame] | 5277 |  | 
|  | 5278 | Vector< sp<EffectChain> > chains; | 
|  | 5279 |  | 
|  | 5280 | for (size_t i = 0; i < mPlaybackThreads.size(); i++) { | 
|  | 5281 | sp<PlaybackThread> t = mPlaybackThreads.valueAt(i); | 
|  | 5282 | for (size_t j = 0; j < t->mEffectChains.size(); j++) { | 
|  | 5283 | sp<EffectChain> ec = t->mEffectChains[j]; | 
| Marco Nelissen | 0270b18 | 2011-08-12 14:14:39 -0700 | [diff] [blame] | 5284 | if (ec->sessionId() > AUDIO_SESSION_OUTPUT_MIX) { | 
|  | 5285 | chains.push(ec); | 
|  | 5286 | } | 
| Marco Nelissen | 3a34bef | 2011-08-02 13:33:41 -0700 | [diff] [blame] | 5287 | } | 
|  | 5288 | } | 
|  | 5289 | for (size_t i = 0; i < mRecordThreads.size(); i++) { | 
|  | 5290 | sp<RecordThread> t = mRecordThreads.valueAt(i); | 
|  | 5291 | for (size_t j = 0; j < t->mEffectChains.size(); j++) { | 
|  | 5292 | sp<EffectChain> ec = t->mEffectChains[j]; | 
|  | 5293 | chains.push(ec); | 
|  | 5294 | } | 
|  | 5295 | } | 
|  | 5296 |  | 
|  | 5297 | for (size_t i = 0; i < chains.size(); i++) { | 
|  | 5298 | sp<EffectChain> ec = chains[i]; | 
|  | 5299 | int sessionid = ec->sessionId(); | 
|  | 5300 | sp<ThreadBase> t = ec->mThread.promote(); | 
|  | 5301 | if (t == 0) { | 
|  | 5302 | continue; | 
|  | 5303 | } | 
|  | 5304 | size_t numsessionrefs = mAudioSessionRefs.size(); | 
|  | 5305 | bool found = false; | 
|  | 5306 | for (size_t k = 0; k < numsessionrefs; k++) { | 
|  | 5307 | AudioSessionRef *ref = mAudioSessionRefs.itemAt(k); | 
|  | 5308 | if (ref->sessionid == sessionid) { | 
| Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 5309 | ALOGV(" session %d still exists for %d with %d refs", | 
| Marco Nelissen | 3a34bef | 2011-08-02 13:33:41 -0700 | [diff] [blame] | 5310 | sessionid, ref->pid, ref->cnt); | 
|  | 5311 | found = true; | 
|  | 5312 | break; | 
|  | 5313 | } | 
|  | 5314 | } | 
|  | 5315 | if (!found) { | 
|  | 5316 | // remove all effects from the chain | 
|  | 5317 | while (ec->mEffects.size()) { | 
|  | 5318 | sp<EffectModule> effect = ec->mEffects[0]; | 
|  | 5319 | effect->unPin(); | 
|  | 5320 | Mutex::Autolock _l (t->mLock); | 
|  | 5321 | t->removeEffect_l(effect); | 
|  | 5322 | for (size_t j = 0; j < effect->mHandles.size(); j++) { | 
|  | 5323 | sp<EffectHandle> handle = effect->mHandles[j].promote(); | 
|  | 5324 | if (handle != 0) { | 
|  | 5325 | handle->mEffect.clear(); | 
| Eric Laurent | a85a74a | 2011-10-19 11:44:54 -0700 | [diff] [blame] | 5326 | if (handle->mHasControl && handle->mEnabled) { | 
|  | 5327 | t->checkSuspendOnEffectEnabled_l(effect, false, effect->sessionId()); | 
|  | 5328 | } | 
| Marco Nelissen | 3a34bef | 2011-08-02 13:33:41 -0700 | [diff] [blame] | 5329 | } | 
|  | 5330 | } | 
|  | 5331 | AudioSystem::unregisterEffect(effect->id()); | 
|  | 5332 | } | 
|  | 5333 | } | 
|  | 5334 | } | 
|  | 5335 | return; | 
|  | 5336 | } | 
|  | 5337 |  | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5338 | // checkPlaybackThread_l() must be called with AudioFlinger::mLock held | 
|  | 5339 | AudioFlinger::PlaybackThread *AudioFlinger::checkPlaybackThread_l(int output) const | 
|  | 5340 | { | 
|  | 5341 | PlaybackThread *thread = NULL; | 
|  | 5342 | if (mPlaybackThreads.indexOfKey(output) >= 0) { | 
|  | 5343 | thread = (PlaybackThread *)mPlaybackThreads.valueFor(output).get(); | 
|  | 5344 | } | 
|  | 5345 | return thread; | 
|  | 5346 | } | 
|  | 5347 |  | 
|  | 5348 | // checkMixerThread_l() must be called with AudioFlinger::mLock held | 
|  | 5349 | AudioFlinger::MixerThread *AudioFlinger::checkMixerThread_l(int output) const | 
|  | 5350 | { | 
|  | 5351 | PlaybackThread *thread = checkPlaybackThread_l(output); | 
|  | 5352 | if (thread != NULL) { | 
| Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 5353 | if (thread->type() == ThreadBase::DIRECT) { | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5354 | thread = NULL; | 
|  | 5355 | } | 
|  | 5356 | } | 
|  | 5357 | return (MixerThread *)thread; | 
|  | 5358 | } | 
|  | 5359 |  | 
|  | 5360 | // checkRecordThread_l() must be called with AudioFlinger::mLock held | 
|  | 5361 | AudioFlinger::RecordThread *AudioFlinger::checkRecordThread_l(int input) const | 
|  | 5362 | { | 
|  | 5363 | RecordThread *thread = NULL; | 
|  | 5364 | if (mRecordThreads.indexOfKey(input) >= 0) { | 
|  | 5365 | thread = (RecordThread *)mRecordThreads.valueFor(input).get(); | 
|  | 5366 | } | 
|  | 5367 | return thread; | 
|  | 5368 | } | 
|  | 5369 |  | 
| Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 5370 | uint32_t AudioFlinger::nextUniqueId() | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5371 | { | 
| Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 5372 | return android_atomic_inc(&mNextUniqueId); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5373 | } | 
|  | 5374 |  | 
| Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 5375 | AudioFlinger::PlaybackThread *AudioFlinger::primaryPlaybackThread_l() | 
|  | 5376 | { | 
|  | 5377 | for (size_t i = 0; i < mPlaybackThreads.size(); i++) { | 
|  | 5378 | PlaybackThread *thread = mPlaybackThreads.valueAt(i).get(); | 
| Eric Laurent | b8ba0a9 | 2011-08-07 16:32:26 -0700 | [diff] [blame] | 5379 | AudioStreamOut *output = thread->getOutput(); | 
|  | 5380 | if (output != NULL && output->hwDev == mPrimaryHardwareDev) { | 
| Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 5381 | return thread; | 
|  | 5382 | } | 
|  | 5383 | } | 
|  | 5384 | return NULL; | 
|  | 5385 | } | 
|  | 5386 |  | 
|  | 5387 | uint32_t AudioFlinger::primaryOutputDevice_l() | 
|  | 5388 | { | 
|  | 5389 | PlaybackThread *thread = primaryPlaybackThread_l(); | 
|  | 5390 |  | 
|  | 5391 | if (thread == NULL) { | 
|  | 5392 | return 0; | 
|  | 5393 | } | 
|  | 5394 |  | 
|  | 5395 | return thread->device(); | 
|  | 5396 | } | 
|  | 5397 |  | 
|  | 5398 |  | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5399 | // ---------------------------------------------------------------------------- | 
|  | 5400 | //  Effect management | 
|  | 5401 | // ---------------------------------------------------------------------------- | 
|  | 5402 |  | 
|  | 5403 |  | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5404 | status_t AudioFlinger::queryNumberEffects(uint32_t *numEffects) | 
|  | 5405 | { | 
|  | 5406 | Mutex::Autolock _l(mLock); | 
|  | 5407 | return EffectQueryNumberEffects(numEffects); | 
|  | 5408 | } | 
|  | 5409 |  | 
|  | 5410 | status_t AudioFlinger::queryEffect(uint32_t index, effect_descriptor_t *descriptor) | 
|  | 5411 | { | 
|  | 5412 | Mutex::Autolock _l(mLock); | 
|  | 5413 | return EffectQueryEffect(index, descriptor); | 
|  | 5414 | } | 
|  | 5415 |  | 
|  | 5416 | status_t AudioFlinger::getEffectDescriptor(effect_uuid_t *pUuid, effect_descriptor_t *descriptor) | 
|  | 5417 | { | 
|  | 5418 | Mutex::Autolock _l(mLock); | 
|  | 5419 | return EffectGetDescriptor(pUuid, descriptor); | 
|  | 5420 | } | 
|  | 5421 |  | 
|  | 5422 |  | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5423 | sp<IEffect> AudioFlinger::createEffect(pid_t pid, | 
|  | 5424 | effect_descriptor_t *pDesc, | 
|  | 5425 | const sp<IEffectClient>& effectClient, | 
|  | 5426 | int32_t priority, | 
| Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 5427 | int io, | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5428 | int sessionId, | 
|  | 5429 | status_t *status, | 
|  | 5430 | int *id, | 
|  | 5431 | int *enabled) | 
|  | 5432 | { | 
|  | 5433 | status_t lStatus = NO_ERROR; | 
|  | 5434 | sp<EffectHandle> handle; | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5435 | effect_descriptor_t desc; | 
|  | 5436 | sp<Client> client; | 
|  | 5437 | wp<Client> wclient; | 
|  | 5438 |  | 
| Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 5439 | ALOGV("createEffect pid %d, client %p, priority %d, sessionId %d, io %d", | 
| Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 5440 | pid, effectClient.get(), priority, sessionId, io); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5441 |  | 
|  | 5442 | if (pDesc == NULL) { | 
|  | 5443 | lStatus = BAD_VALUE; | 
|  | 5444 | goto Exit; | 
|  | 5445 | } | 
|  | 5446 |  | 
| Eric Laurent | 84e9a10 | 2010-09-23 16:10:16 -0700 | [diff] [blame] | 5447 | // check audio settings permission for global effects | 
| Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 5448 | if (sessionId == AUDIO_SESSION_OUTPUT_MIX && !settingsAllowed()) { | 
| Eric Laurent | 84e9a10 | 2010-09-23 16:10:16 -0700 | [diff] [blame] | 5449 | lStatus = PERMISSION_DENIED; | 
|  | 5450 | goto Exit; | 
|  | 5451 | } | 
|  | 5452 |  | 
| Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 5453 | // Session AUDIO_SESSION_OUTPUT_STAGE is reserved for output stage effects | 
| Eric Laurent | 84e9a10 | 2010-09-23 16:10:16 -0700 | [diff] [blame] | 5454 | // that can only be created by audio policy manager (running in same process) | 
| Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 5455 | if (sessionId == AUDIO_SESSION_OUTPUT_STAGE && getpid() != pid) { | 
| Eric Laurent | 84e9a10 | 2010-09-23 16:10:16 -0700 | [diff] [blame] | 5456 | lStatus = PERMISSION_DENIED; | 
|  | 5457 | goto Exit; | 
|  | 5458 | } | 
|  | 5459 |  | 
| Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 5460 | if (io == 0) { | 
| Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 5461 | if (sessionId == AUDIO_SESSION_OUTPUT_STAGE) { | 
| Eric Laurent | 84e9a10 | 2010-09-23 16:10:16 -0700 | [diff] [blame] | 5462 | // output must be specified by AudioPolicyManager when using session | 
| Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 5463 | // AUDIO_SESSION_OUTPUT_STAGE | 
| Eric Laurent | 84e9a10 | 2010-09-23 16:10:16 -0700 | [diff] [blame] | 5464 | lStatus = BAD_VALUE; | 
|  | 5465 | goto Exit; | 
| Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 5466 | } else if (sessionId == AUDIO_SESSION_OUTPUT_MIX) { | 
| Eric Laurent | 84e9a10 | 2010-09-23 16:10:16 -0700 | [diff] [blame] | 5467 | // if the output returned by getOutputForEffect() is removed before we lock the | 
| Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 5468 | // mutex below, the call to checkPlaybackThread_l(io) below will detect it | 
| Eric Laurent | 84e9a10 | 2010-09-23 16:10:16 -0700 | [diff] [blame] | 5469 | // and we will exit safely | 
| Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 5470 | io = AudioSystem::getOutputForEffect(&desc); | 
| Eric Laurent | 84e9a10 | 2010-09-23 16:10:16 -0700 | [diff] [blame] | 5471 | } | 
|  | 5472 | } | 
|  | 5473 |  | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5474 | { | 
|  | 5475 | Mutex::Autolock _l(mLock); | 
|  | 5476 |  | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5477 |  | 
|  | 5478 | if (!EffectIsNullUuid(&pDesc->uuid)) { | 
|  | 5479 | // if uuid is specified, request effect descriptor | 
|  | 5480 | lStatus = EffectGetDescriptor(&pDesc->uuid, &desc); | 
|  | 5481 | if (lStatus < 0) { | 
| Steve Block | 5ff1dd5 | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 5482 | ALOGW("createEffect() error %d from EffectGetDescriptor", lStatus); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5483 | goto Exit; | 
|  | 5484 | } | 
|  | 5485 | } else { | 
|  | 5486 | // if uuid is not specified, look for an available implementation | 
|  | 5487 | // of the required type in effect factory | 
|  | 5488 | if (EffectIsNullUuid(&pDesc->type)) { | 
| Steve Block | 5ff1dd5 | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 5489 | ALOGW("createEffect() no effect type"); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5490 | lStatus = BAD_VALUE; | 
|  | 5491 | goto Exit; | 
|  | 5492 | } | 
|  | 5493 | uint32_t numEffects = 0; | 
|  | 5494 | effect_descriptor_t d; | 
| Marco Nelissen | 3a34bef | 2011-08-02 13:33:41 -0700 | [diff] [blame] | 5495 | d.flags = 0; // prevent compiler warning | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5496 | bool found = false; | 
|  | 5497 |  | 
|  | 5498 | lStatus = EffectQueryNumberEffects(&numEffects); | 
|  | 5499 | if (lStatus < 0) { | 
| Steve Block | 5ff1dd5 | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 5500 | ALOGW("createEffect() error %d from EffectQueryNumberEffects", lStatus); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5501 | goto Exit; | 
|  | 5502 | } | 
|  | 5503 | for (uint32_t i = 0; i < numEffects; i++) { | 
|  | 5504 | lStatus = EffectQueryEffect(i, &desc); | 
|  | 5505 | if (lStatus < 0) { | 
| Steve Block | 5ff1dd5 | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 5506 | ALOGW("createEffect() error %d from EffectQueryEffect", lStatus); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5507 | continue; | 
|  | 5508 | } | 
|  | 5509 | if (memcmp(&desc.type, &pDesc->type, sizeof(effect_uuid_t)) == 0) { | 
|  | 5510 | // If matching type found save effect descriptor. If the session is | 
|  | 5511 | // 0 and the effect is not auxiliary, continue enumeration in case | 
|  | 5512 | // an auxiliary version of this effect type is available | 
|  | 5513 | found = true; | 
|  | 5514 | memcpy(&d, &desc, sizeof(effect_descriptor_t)); | 
| Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 5515 | if (sessionId != AUDIO_SESSION_OUTPUT_MIX || | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5516 | (desc.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) { | 
|  | 5517 | break; | 
|  | 5518 | } | 
|  | 5519 | } | 
|  | 5520 | } | 
|  | 5521 | if (!found) { | 
|  | 5522 | lStatus = BAD_VALUE; | 
| Steve Block | 5ff1dd5 | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 5523 | ALOGW("createEffect() effect not found"); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5524 | goto Exit; | 
|  | 5525 | } | 
|  | 5526 | // For same effect type, chose auxiliary version over insert version if | 
|  | 5527 | // connect to output mix (Compliance to OpenSL ES) | 
| Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 5528 | if (sessionId == AUDIO_SESSION_OUTPUT_MIX && | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5529 | (d.flags & EFFECT_FLAG_TYPE_MASK) != EFFECT_FLAG_TYPE_AUXILIARY) { | 
|  | 5530 | memcpy(&desc, &d, sizeof(effect_descriptor_t)); | 
|  | 5531 | } | 
|  | 5532 | } | 
|  | 5533 |  | 
|  | 5534 | // Do not allow auxiliary effects on a session different from 0 (output mix) | 
| Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 5535 | if (sessionId != AUDIO_SESSION_OUTPUT_MIX && | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5536 | (desc.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) { | 
|  | 5537 | lStatus = INVALID_OPERATION; | 
|  | 5538 | goto Exit; | 
|  | 5539 | } | 
|  | 5540 |  | 
| Eric Laurent | 59255e4 | 2011-07-27 19:49:51 -0700 | [diff] [blame] | 5541 | // check recording permission for visualizer | 
|  | 5542 | if ((memcmp(&desc.type, SL_IID_VISUALIZATION, sizeof(effect_uuid_t)) == 0) && | 
|  | 5543 | !recordingAllowed()) { | 
|  | 5544 | lStatus = PERMISSION_DENIED; | 
|  | 5545 | goto Exit; | 
|  | 5546 | } | 
|  | 5547 |  | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5548 | // return effect descriptor | 
|  | 5549 | memcpy(pDesc, &desc, sizeof(effect_descriptor_t)); | 
|  | 5550 |  | 
|  | 5551 | // If output is not specified try to find a matching audio session ID in one of the | 
|  | 5552 | // output threads. | 
| Eric Laurent | 84e9a10 | 2010-09-23 16:10:16 -0700 | [diff] [blame] | 5553 | // If output is 0 here, sessionId is neither SESSION_OUTPUT_STAGE nor SESSION_OUTPUT_MIX | 
|  | 5554 | // because of code checking output when entering the function. | 
| Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 5555 | // Note: io is never 0 when creating an effect on an input | 
|  | 5556 | if (io == 0) { | 
| Eric Laurent | 84e9a10 | 2010-09-23 16:10:16 -0700 | [diff] [blame] | 5557 | // look for the thread where the specified audio session is present | 
|  | 5558 | for (size_t i = 0; i < mPlaybackThreads.size(); i++) { | 
|  | 5559 | if (mPlaybackThreads.valueAt(i)->hasAudioSession(sessionId) != 0) { | 
| Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 5560 | io = mPlaybackThreads.keyAt(i); | 
| Eric Laurent | 84e9a10 | 2010-09-23 16:10:16 -0700 | [diff] [blame] | 5561 | break; | 
| Eric Laurent | 39e94f8 | 2010-07-28 01:32:47 -0700 | [diff] [blame] | 5562 | } | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5563 | } | 
| Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 5564 | if (io == 0) { | 
|  | 5565 | for (size_t i = 0; i < mRecordThreads.size(); i++) { | 
|  | 5566 | if (mRecordThreads.valueAt(i)->hasAudioSession(sessionId) != 0) { | 
|  | 5567 | io = mRecordThreads.keyAt(i); | 
|  | 5568 | break; | 
|  | 5569 | } | 
|  | 5570 | } | 
|  | 5571 | } | 
| Eric Laurent | 84e9a10 | 2010-09-23 16:10:16 -0700 | [diff] [blame] | 5572 | // If no output thread contains the requested session ID, default to | 
|  | 5573 | // first output. The effect chain will be moved to the correct output | 
|  | 5574 | // thread when a track with the same session ID is created | 
| Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 5575 | if (io == 0 && mPlaybackThreads.size()) { | 
|  | 5576 | io = mPlaybackThreads.keyAt(0); | 
|  | 5577 | } | 
| Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 5578 | ALOGV("createEffect() got io %d for effect %s", io, desc.name); | 
| Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 5579 | } | 
|  | 5580 | ThreadBase *thread = checkRecordThread_l(io); | 
|  | 5581 | if (thread == NULL) { | 
|  | 5582 | thread = checkPlaybackThread_l(io); | 
|  | 5583 | if (thread == NULL) { | 
| Steve Block | 29357bc | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 5584 | ALOGE("createEffect() unknown output thread"); | 
| Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 5585 | lStatus = BAD_VALUE; | 
|  | 5586 | goto Exit; | 
| Eric Laurent | 84e9a10 | 2010-09-23 16:10:16 -0700 | [diff] [blame] | 5587 | } | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5588 | } | 
| Eric Laurent | 84e9a10 | 2010-09-23 16:10:16 -0700 | [diff] [blame] | 5589 |  | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5590 | wclient = mClients.valueFor(pid); | 
|  | 5591 |  | 
|  | 5592 | if (wclient != NULL) { | 
|  | 5593 | client = wclient.promote(); | 
|  | 5594 | } else { | 
|  | 5595 | client = new Client(this, pid); | 
|  | 5596 | mClients.add(pid, client); | 
|  | 5597 | } | 
|  | 5598 |  | 
| Marco Nelissen | 3a34bef | 2011-08-02 13:33:41 -0700 | [diff] [blame] | 5599 | // create effect on selected output thread | 
| Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 5600 | handle = thread->createEffect_l(client, effectClient, priority, sessionId, | 
|  | 5601 | &desc, enabled, &lStatus); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5602 | if (handle != 0 && id != NULL) { | 
|  | 5603 | *id = handle->id(); | 
|  | 5604 | } | 
|  | 5605 | } | 
|  | 5606 |  | 
|  | 5607 | Exit: | 
|  | 5608 | if(status) { | 
|  | 5609 | *status = lStatus; | 
|  | 5610 | } | 
|  | 5611 | return handle; | 
|  | 5612 | } | 
|  | 5613 |  | 
| Eric Laurent | 59255e4 | 2011-07-27 19:49:51 -0700 | [diff] [blame] | 5614 | status_t AudioFlinger::moveEffects(int sessionId, int srcOutput, int dstOutput) | 
| Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 5615 | { | 
| Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 5616 | ALOGV("moveEffects() session %d, srcOutput %d, dstOutput %d", | 
| Eric Laurent | 59255e4 | 2011-07-27 19:49:51 -0700 | [diff] [blame] | 5617 | sessionId, srcOutput, dstOutput); | 
| Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 5618 | Mutex::Autolock _l(mLock); | 
|  | 5619 | if (srcOutput == dstOutput) { | 
| Steve Block | 5ff1dd5 | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 5620 | ALOGW("moveEffects() same dst and src outputs %d", dstOutput); | 
| Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 5621 | return NO_ERROR; | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5622 | } | 
| Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 5623 | PlaybackThread *srcThread = checkPlaybackThread_l(srcOutput); | 
|  | 5624 | if (srcThread == NULL) { | 
| Steve Block | 5ff1dd5 | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 5625 | ALOGW("moveEffects() bad srcOutput %d", srcOutput); | 
| Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 5626 | return BAD_VALUE; | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5627 | } | 
| Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 5628 | PlaybackThread *dstThread = checkPlaybackThread_l(dstOutput); | 
|  | 5629 | if (dstThread == NULL) { | 
| Steve Block | 5ff1dd5 | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 5630 | ALOGW("moveEffects() bad dstOutput %d", dstOutput); | 
| Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 5631 | return BAD_VALUE; | 
|  | 5632 | } | 
|  | 5633 |  | 
|  | 5634 | Mutex::Autolock _dl(dstThread->mLock); | 
|  | 5635 | Mutex::Autolock _sl(srcThread->mLock); | 
| Eric Laurent | 59255e4 | 2011-07-27 19:49:51 -0700 | [diff] [blame] | 5636 | moveEffectChain_l(sessionId, srcThread, dstThread, false); | 
| Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 5637 |  | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5638 | return NO_ERROR; | 
|  | 5639 | } | 
|  | 5640 |  | 
| Marco Nelissen | 3a34bef | 2011-08-02 13:33:41 -0700 | [diff] [blame] | 5641 | // moveEffectChain_l must be called with both srcThread and dstThread mLocks held | 
| Eric Laurent | 59255e4 | 2011-07-27 19:49:51 -0700 | [diff] [blame] | 5642 | status_t AudioFlinger::moveEffectChain_l(int sessionId, | 
| Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 5643 | AudioFlinger::PlaybackThread *srcThread, | 
| Eric Laurent | 39e94f8 | 2010-07-28 01:32:47 -0700 | [diff] [blame] | 5644 | AudioFlinger::PlaybackThread *dstThread, | 
|  | 5645 | bool reRegister) | 
| Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 5646 | { | 
| Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 5647 | ALOGV("moveEffectChain_l() session %d from thread %p to thread %p", | 
| Eric Laurent | 59255e4 | 2011-07-27 19:49:51 -0700 | [diff] [blame] | 5648 | sessionId, srcThread, dstThread); | 
| Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 5649 |  | 
| Eric Laurent | 59255e4 | 2011-07-27 19:49:51 -0700 | [diff] [blame] | 5650 | sp<EffectChain> chain = srcThread->getEffectChain_l(sessionId); | 
| Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 5651 | if (chain == 0) { | 
| Steve Block | 5ff1dd5 | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 5652 | ALOGW("moveEffectChain_l() effect chain for session %d not on source thread %p", | 
| Eric Laurent | 59255e4 | 2011-07-27 19:49:51 -0700 | [diff] [blame] | 5653 | sessionId, srcThread); | 
| Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 5654 | return INVALID_OPERATION; | 
|  | 5655 | } | 
|  | 5656 |  | 
| Eric Laurent | 39e94f8 | 2010-07-28 01:32:47 -0700 | [diff] [blame] | 5657 | // remove chain first. This is useful only if reconfiguring effect chain on same output thread, | 
| Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 5658 | // so that a new chain is created with correct parameters when first effect is added. This is | 
| Eric Laurent | ec35a14 | 2011-10-05 17:42:25 -0700 | [diff] [blame] | 5659 | // otherwise unnecessary as removeEffect_l() will remove the chain when last effect is | 
| Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 5660 | // removed. | 
|  | 5661 | srcThread->removeEffectChain_l(chain); | 
|  | 5662 |  | 
|  | 5663 | // transfer all effects one by one so that new effect chain is created on new thread with | 
|  | 5664 | // correct buffer sizes and audio parameters and effect engines reconfigured accordingly | 
| Eric Laurent | 39e94f8 | 2010-07-28 01:32:47 -0700 | [diff] [blame] | 5665 | int dstOutput = dstThread->id(); | 
|  | 5666 | sp<EffectChain> dstChain; | 
| Marco Nelissen | 3a34bef | 2011-08-02 13:33:41 -0700 | [diff] [blame] | 5667 | uint32_t strategy = 0; // prevent compiler warning | 
| Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 5668 | sp<EffectModule> effect = chain->getEffectFromId_l(0); | 
|  | 5669 | while (effect != 0) { | 
|  | 5670 | srcThread->removeEffect_l(effect); | 
|  | 5671 | dstThread->addEffect_l(effect); | 
| Eric Laurent | ec35a14 | 2011-10-05 17:42:25 -0700 | [diff] [blame] | 5672 | // removeEffect_l() has stopped the effect if it was active so it must be restarted | 
|  | 5673 | if (effect->state() == EffectModule::ACTIVE || | 
|  | 5674 | effect->state() == EffectModule::STOPPING) { | 
|  | 5675 | effect->start(); | 
|  | 5676 | } | 
| Eric Laurent | 39e94f8 | 2010-07-28 01:32:47 -0700 | [diff] [blame] | 5677 | // if the move request is not received from audio policy manager, the effect must be | 
|  | 5678 | // re-registered with the new strategy and output | 
|  | 5679 | if (dstChain == 0) { | 
|  | 5680 | dstChain = effect->chain().promote(); | 
|  | 5681 | if (dstChain == 0) { | 
| Steve Block | 5ff1dd5 | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 5682 | ALOGW("moveEffectChain_l() cannot get chain from effect %p", effect.get()); | 
| Eric Laurent | 39e94f8 | 2010-07-28 01:32:47 -0700 | [diff] [blame] | 5683 | srcThread->addEffect_l(effect); | 
|  | 5684 | return NO_INIT; | 
|  | 5685 | } | 
|  | 5686 | strategy = dstChain->strategy(); | 
|  | 5687 | } | 
|  | 5688 | if (reRegister) { | 
|  | 5689 | AudioSystem::unregisterEffect(effect->id()); | 
|  | 5690 | AudioSystem::registerEffect(&effect->desc(), | 
|  | 5691 | dstOutput, | 
|  | 5692 | strategy, | 
| Eric Laurent | 59255e4 | 2011-07-27 19:49:51 -0700 | [diff] [blame] | 5693 | sessionId, | 
| Eric Laurent | 39e94f8 | 2010-07-28 01:32:47 -0700 | [diff] [blame] | 5694 | effect->id()); | 
|  | 5695 | } | 
| Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 5696 | effect = chain->getEffectFromId_l(0); | 
|  | 5697 | } | 
|  | 5698 |  | 
|  | 5699 | return NO_ERROR; | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5700 | } | 
|  | 5701 |  | 
| Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 5702 |  | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5703 | // PlaybackThread::createEffect_l() must be called with AudioFlinger::mLock held | 
| Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 5704 | sp<AudioFlinger::EffectHandle> AudioFlinger::ThreadBase::createEffect_l( | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5705 | const sp<AudioFlinger::Client>& client, | 
|  | 5706 | const sp<IEffectClient>& effectClient, | 
|  | 5707 | int32_t priority, | 
|  | 5708 | int sessionId, | 
|  | 5709 | effect_descriptor_t *desc, | 
|  | 5710 | int *enabled, | 
|  | 5711 | status_t *status | 
|  | 5712 | ) | 
|  | 5713 | { | 
|  | 5714 | sp<EffectModule> effect; | 
|  | 5715 | sp<EffectHandle> handle; | 
|  | 5716 | status_t lStatus; | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5717 | sp<EffectChain> chain; | 
| Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 5718 | bool chainCreated = false; | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5719 | bool effectCreated = false; | 
|  | 5720 | bool effectRegistered = false; | 
|  | 5721 |  | 
| Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 5722 | lStatus = initCheck(); | 
|  | 5723 | if (lStatus != NO_ERROR) { | 
| Steve Block | 5ff1dd5 | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 5724 | ALOGW("createEffect_l() Audio driver not initialized."); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5725 | goto Exit; | 
|  | 5726 | } | 
|  | 5727 |  | 
|  | 5728 | // Do not allow effects with session ID 0 on direct output or duplicating threads | 
|  | 5729 | // TODO: add rule for hw accelerated effects on direct outputs with non PCM format | 
| Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 5730 | if (sessionId == AUDIO_SESSION_OUTPUT_MIX && mType != MIXER) { | 
| Steve Block | 5ff1dd5 | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 5731 | ALOGW("createEffect_l() Cannot add auxiliary effect %s to session %d", | 
| Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 5732 | desc->name, sessionId); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5733 | lStatus = BAD_VALUE; | 
|  | 5734 | goto Exit; | 
|  | 5735 | } | 
| Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 5736 | // Only Pre processor effects are allowed on input threads and only on input threads | 
|  | 5737 | if ((mType == RECORD && | 
|  | 5738 | (desc->flags & EFFECT_FLAG_TYPE_MASK) != EFFECT_FLAG_TYPE_PRE_PROC) || | 
|  | 5739 | (mType != RECORD && | 
|  | 5740 | (desc->flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_PRE_PROC)) { | 
| Steve Block | 5ff1dd5 | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 5741 | ALOGW("createEffect_l() effect %s (flags %08x) created on wrong thread type %d", | 
| Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 5742 | desc->name, desc->flags, mType); | 
|  | 5743 | lStatus = BAD_VALUE; | 
|  | 5744 | goto Exit; | 
|  | 5745 | } | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5746 |  | 
| Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 5747 | ALOGV("createEffect_l() thread %p effect %s on session %d", this, desc->name, sessionId); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5748 |  | 
|  | 5749 | { // scope for mLock | 
|  | 5750 | Mutex::Autolock _l(mLock); | 
|  | 5751 |  | 
|  | 5752 | // check for existing effect chain with the requested audio session | 
|  | 5753 | chain = getEffectChain_l(sessionId); | 
|  | 5754 | if (chain == 0) { | 
|  | 5755 | // create a new chain for this session | 
| Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 5756 | ALOGV("createEffect_l() new effect chain for session %d", sessionId); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5757 | chain = new EffectChain(this, sessionId); | 
|  | 5758 | addEffectChain_l(chain); | 
| Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 5759 | chain->setStrategy(getStrategyForSession_l(sessionId)); | 
|  | 5760 | chainCreated = true; | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5761 | } else { | 
| Eric Laurent | cab1124 | 2010-07-15 12:50:15 -0700 | [diff] [blame] | 5762 | effect = chain->getEffectFromDesc_l(desc); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5763 | } | 
|  | 5764 |  | 
| Glenn Kasten | 7fc9a6f | 2012-01-10 10:46:34 -0800 | [diff] [blame] | 5765 | ALOGV("createEffect_l() got effect %p on chain %p", effect.get(), chain.get()); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5766 |  | 
|  | 5767 | if (effect == 0) { | 
| Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 5768 | int id = mAudioFlinger->nextUniqueId(); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5769 | // Check CPU and memory usage | 
| Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 5770 | lStatus = AudioSystem::registerEffect(desc, mId, chain->strategy(), sessionId, id); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5771 | if (lStatus != NO_ERROR) { | 
|  | 5772 | goto Exit; | 
|  | 5773 | } | 
|  | 5774 | effectRegistered = true; | 
|  | 5775 | // create a new effect module if none present in the chain | 
| Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 5776 | effect = new EffectModule(this, chain, desc, id, sessionId); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5777 | lStatus = effect->status(); | 
|  | 5778 | if (lStatus != NO_ERROR) { | 
|  | 5779 | goto Exit; | 
|  | 5780 | } | 
| Eric Laurent | cab1124 | 2010-07-15 12:50:15 -0700 | [diff] [blame] | 5781 | lStatus = chain->addEffect_l(effect); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5782 | if (lStatus != NO_ERROR) { | 
|  | 5783 | goto Exit; | 
|  | 5784 | } | 
|  | 5785 | effectCreated = true; | 
|  | 5786 |  | 
|  | 5787 | effect->setDevice(mDevice); | 
|  | 5788 | effect->setMode(mAudioFlinger->getMode()); | 
|  | 5789 | } | 
|  | 5790 | // create effect handle and connect it to effect module | 
|  | 5791 | handle = new EffectHandle(effect, client, effectClient, priority); | 
|  | 5792 | lStatus = effect->addHandle(handle); | 
|  | 5793 | if (enabled) { | 
|  | 5794 | *enabled = (int)effect->isEnabled(); | 
|  | 5795 | } | 
|  | 5796 | } | 
|  | 5797 |  | 
|  | 5798 | Exit: | 
|  | 5799 | if (lStatus != NO_ERROR && lStatus != ALREADY_EXISTS) { | 
| Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 5800 | Mutex::Autolock _l(mLock); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5801 | if (effectCreated) { | 
| Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 5802 | chain->removeEffect_l(effect); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5803 | } | 
|  | 5804 | if (effectRegistered) { | 
| Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 5805 | AudioSystem::unregisterEffect(effect->id()); | 
|  | 5806 | } | 
|  | 5807 | if (chainCreated) { | 
|  | 5808 | removeEffectChain_l(chain); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5809 | } | 
|  | 5810 | handle.clear(); | 
|  | 5811 | } | 
|  | 5812 |  | 
|  | 5813 | if(status) { | 
|  | 5814 | *status = lStatus; | 
|  | 5815 | } | 
|  | 5816 | return handle; | 
|  | 5817 | } | 
|  | 5818 |  | 
| Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 5819 | sp<AudioFlinger::EffectModule> AudioFlinger::ThreadBase::getEffect_l(int sessionId, int effectId) | 
|  | 5820 | { | 
|  | 5821 | sp<EffectModule> effect; | 
|  | 5822 |  | 
|  | 5823 | sp<EffectChain> chain = getEffectChain_l(sessionId); | 
|  | 5824 | if (chain != 0) { | 
|  | 5825 | effect = chain->getEffectFromId_l(effectId); | 
|  | 5826 | } | 
|  | 5827 | return effect; | 
|  | 5828 | } | 
|  | 5829 |  | 
| Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 5830 | // PlaybackThread::addEffect_l() must be called with AudioFlinger::mLock and | 
|  | 5831 | // PlaybackThread::mLock held | 
| Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 5832 | status_t AudioFlinger::ThreadBase::addEffect_l(const sp<EffectModule>& effect) | 
| Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 5833 | { | 
|  | 5834 | // check for existing effect chain with the requested audio session | 
|  | 5835 | int sessionId = effect->sessionId(); | 
|  | 5836 | sp<EffectChain> chain = getEffectChain_l(sessionId); | 
|  | 5837 | bool chainCreated = false; | 
|  | 5838 |  | 
|  | 5839 | if (chain == 0) { | 
|  | 5840 | // create a new chain for this session | 
| Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 5841 | ALOGV("addEffect_l() new effect chain for session %d", sessionId); | 
| Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 5842 | chain = new EffectChain(this, sessionId); | 
|  | 5843 | addEffectChain_l(chain); | 
|  | 5844 | chain->setStrategy(getStrategyForSession_l(sessionId)); | 
|  | 5845 | chainCreated = true; | 
|  | 5846 | } | 
| Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 5847 | ALOGV("addEffect_l() %p chain %p effect %p", this, chain.get(), effect.get()); | 
| Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 5848 |  | 
|  | 5849 | if (chain->getEffectFromId_l(effect->id()) != 0) { | 
| Steve Block | 5ff1dd5 | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 5850 | ALOGW("addEffect_l() %p effect %s already present in chain %p", | 
| Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 5851 | this, effect->desc().name, chain.get()); | 
|  | 5852 | return BAD_VALUE; | 
|  | 5853 | } | 
|  | 5854 |  | 
|  | 5855 | status_t status = chain->addEffect_l(effect); | 
|  | 5856 | if (status != NO_ERROR) { | 
|  | 5857 | if (chainCreated) { | 
|  | 5858 | removeEffectChain_l(chain); | 
|  | 5859 | } | 
|  | 5860 | return status; | 
|  | 5861 | } | 
|  | 5862 |  | 
|  | 5863 | effect->setDevice(mDevice); | 
|  | 5864 | effect->setMode(mAudioFlinger->getMode()); | 
|  | 5865 | return NO_ERROR; | 
|  | 5866 | } | 
|  | 5867 |  | 
| Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 5868 | void AudioFlinger::ThreadBase::removeEffect_l(const sp<EffectModule>& effect) { | 
| Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 5869 |  | 
| Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 5870 | ALOGV("removeEffect_l() %p effect %p", this, effect.get()); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5871 | effect_descriptor_t desc = effect->desc(); | 
| Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 5872 | if ((desc.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) { | 
|  | 5873 | detachAuxEffect_l(effect->id()); | 
|  | 5874 | } | 
|  | 5875 |  | 
|  | 5876 | sp<EffectChain> chain = effect->chain().promote(); | 
|  | 5877 | if (chain != 0) { | 
|  | 5878 | // remove effect chain if removing last effect | 
|  | 5879 | if (chain->removeEffect_l(effect) == 0) { | 
|  | 5880 | removeEffectChain_l(chain); | 
|  | 5881 | } | 
|  | 5882 | } else { | 
| Steve Block | 5ff1dd5 | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 5883 | ALOGW("removeEffect_l() %p cannot promote chain for effect %p", this, effect.get()); | 
| Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 5884 | } | 
|  | 5885 | } | 
|  | 5886 |  | 
| Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 5887 | void AudioFlinger::ThreadBase::lockEffectChains_l( | 
|  | 5888 | Vector<sp <AudioFlinger::EffectChain> >& effectChains) | 
|  | 5889 | { | 
|  | 5890 | effectChains = mEffectChains; | 
|  | 5891 | for (size_t i = 0; i < mEffectChains.size(); i++) { | 
|  | 5892 | mEffectChains[i]->lock(); | 
|  | 5893 | } | 
|  | 5894 | } | 
|  | 5895 |  | 
|  | 5896 | void AudioFlinger::ThreadBase::unlockEffectChains( | 
|  | 5897 | Vector<sp <AudioFlinger::EffectChain> >& effectChains) | 
|  | 5898 | { | 
|  | 5899 | for (size_t i = 0; i < effectChains.size(); i++) { | 
|  | 5900 | effectChains[i]->unlock(); | 
|  | 5901 | } | 
|  | 5902 | } | 
|  | 5903 |  | 
|  | 5904 | sp<AudioFlinger::EffectChain> AudioFlinger::ThreadBase::getEffectChain(int sessionId) | 
|  | 5905 | { | 
|  | 5906 | Mutex::Autolock _l(mLock); | 
|  | 5907 | return getEffectChain_l(sessionId); | 
|  | 5908 | } | 
|  | 5909 |  | 
|  | 5910 | sp<AudioFlinger::EffectChain> AudioFlinger::ThreadBase::getEffectChain_l(int sessionId) | 
|  | 5911 | { | 
|  | 5912 | sp<EffectChain> chain; | 
|  | 5913 |  | 
|  | 5914 | size_t size = mEffectChains.size(); | 
|  | 5915 | for (size_t i = 0; i < size; i++) { | 
|  | 5916 | if (mEffectChains[i]->sessionId() == sessionId) { | 
|  | 5917 | chain = mEffectChains[i]; | 
|  | 5918 | break; | 
|  | 5919 | } | 
|  | 5920 | } | 
|  | 5921 | return chain; | 
|  | 5922 | } | 
|  | 5923 |  | 
| Glenn Kasten | f78aee7 | 2012-01-04 11:00:47 -0800 | [diff] [blame] | 5924 | void AudioFlinger::ThreadBase::setMode(audio_mode_t mode) | 
| Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 5925 | { | 
|  | 5926 | Mutex::Autolock _l(mLock); | 
|  | 5927 | size_t size = mEffectChains.size(); | 
|  | 5928 | for (size_t i = 0; i < size; i++) { | 
|  | 5929 | mEffectChains[i]->setMode_l(mode); | 
|  | 5930 | } | 
|  | 5931 | } | 
|  | 5932 |  | 
|  | 5933 | void AudioFlinger::ThreadBase::disconnectEffect(const sp<EffectModule>& effect, | 
| Marco Nelissen | 3a34bef | 2011-08-02 13:33:41 -0700 | [diff] [blame] | 5934 | const wp<EffectHandle>& handle, | 
|  | 5935 | bool unpiniflast) { | 
| Eric Laurent | 59255e4 | 2011-07-27 19:49:51 -0700 | [diff] [blame] | 5936 |  | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5937 | Mutex::Autolock _l(mLock); | 
| Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 5938 | ALOGV("disconnectEffect() %p effect %p", this, effect.get()); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5939 | // delete the effect module if removing last handle on it | 
|  | 5940 | if (effect->removeHandle(handle) == 0) { | 
| Marco Nelissen | 3a34bef | 2011-08-02 13:33:41 -0700 | [diff] [blame] | 5941 | if (!effect->isPinned() || unpiniflast) { | 
|  | 5942 | removeEffect_l(effect); | 
|  | 5943 | AudioSystem::unregisterEffect(effect->id()); | 
|  | 5944 | } | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5945 | } | 
|  | 5946 | } | 
|  | 5947 |  | 
|  | 5948 | status_t AudioFlinger::PlaybackThread::addEffectChain_l(const sp<EffectChain>& chain) | 
|  | 5949 | { | 
|  | 5950 | int session = chain->sessionId(); | 
|  | 5951 | int16_t *buffer = mMixBuffer; | 
|  | 5952 | bool ownsBuffer = false; | 
|  | 5953 |  | 
| Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 5954 | ALOGV("addEffectChain_l() %p on thread %p for session %d", chain.get(), this, session); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5955 | if (session > 0) { | 
|  | 5956 | // Only one effect chain can be present in direct output thread and it uses | 
|  | 5957 | // the mix buffer as input | 
|  | 5958 | if (mType != DIRECT) { | 
|  | 5959 | size_t numSamples = mFrameCount * mChannelCount; | 
|  | 5960 | buffer = new int16_t[numSamples]; | 
|  | 5961 | memset(buffer, 0, numSamples * sizeof(int16_t)); | 
| Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 5962 | ALOGV("addEffectChain_l() creating new input buffer %p session %d", buffer, session); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5963 | ownsBuffer = true; | 
|  | 5964 | } | 
|  | 5965 |  | 
|  | 5966 | // Attach all tracks with same session ID to this chain. | 
|  | 5967 | for (size_t i = 0; i < mTracks.size(); ++i) { | 
|  | 5968 | sp<Track> track = mTracks[i]; | 
|  | 5969 | if (session == track->sessionId()) { | 
| Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 5970 | ALOGV("addEffectChain_l() track->setMainBuffer track %p buffer %p", track.get(), buffer); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5971 | track->setMainBuffer(buffer); | 
| Eric Laurent | b469b94 | 2011-05-09 12:09:06 -0700 | [diff] [blame] | 5972 | chain->incTrackCnt(); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5973 | } | 
|  | 5974 | } | 
|  | 5975 |  | 
|  | 5976 | // indicate all active tracks in the chain | 
|  | 5977 | for (size_t i = 0 ; i < mActiveTracks.size() ; ++i) { | 
|  | 5978 | sp<Track> track = mActiveTracks[i].promote(); | 
|  | 5979 | if (track == 0) continue; | 
|  | 5980 | if (session == track->sessionId()) { | 
| Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 5981 | ALOGV("addEffectChain_l() activating track %p on session %d", track.get(), session); | 
| Eric Laurent | b469b94 | 2011-05-09 12:09:06 -0700 | [diff] [blame] | 5982 | chain->incActiveTrackCnt(); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5983 | } | 
|  | 5984 | } | 
|  | 5985 | } | 
|  | 5986 |  | 
|  | 5987 | chain->setInBuffer(buffer, ownsBuffer); | 
|  | 5988 | chain->setOutBuffer(mMixBuffer); | 
| Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 5989 | // Effect chain for session AUDIO_SESSION_OUTPUT_STAGE is inserted at end of effect | 
| Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 5990 | // chains list in order to be processed last as it contains output stage effects | 
| Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 5991 | // Effect chain for session AUDIO_SESSION_OUTPUT_MIX is inserted before | 
|  | 5992 | // session AUDIO_SESSION_OUTPUT_STAGE to be processed | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5993 | // after track specific effects and before output stage | 
| Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 5994 | // It is therefore mandatory that AUDIO_SESSION_OUTPUT_MIX == 0 and | 
|  | 5995 | // that AUDIO_SESSION_OUTPUT_STAGE < AUDIO_SESSION_OUTPUT_MIX | 
| Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 5996 | // Effect chain for other sessions are inserted at beginning of effect | 
|  | 5997 | // chains list to be processed before output mix effects. Relative order between other | 
|  | 5998 | // sessions is not important | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5999 | size_t size = mEffectChains.size(); | 
|  | 6000 | size_t i = 0; | 
|  | 6001 | for (i = 0; i < size; i++) { | 
|  | 6002 | if (mEffectChains[i]->sessionId() < session) break; | 
|  | 6003 | } | 
|  | 6004 | mEffectChains.insertAt(chain, i); | 
| Eric Laurent | 59255e4 | 2011-07-27 19:49:51 -0700 | [diff] [blame] | 6005 | checkSuspendOnAddEffectChain_l(chain); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6006 |  | 
|  | 6007 | return NO_ERROR; | 
|  | 6008 | } | 
|  | 6009 |  | 
|  | 6010 | size_t AudioFlinger::PlaybackThread::removeEffectChain_l(const sp<EffectChain>& chain) | 
|  | 6011 | { | 
|  | 6012 | int session = chain->sessionId(); | 
|  | 6013 |  | 
| Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 6014 | ALOGV("removeEffectChain_l() %p from thread %p for session %d", chain.get(), this, session); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6015 |  | 
|  | 6016 | for (size_t i = 0; i < mEffectChains.size(); i++) { | 
|  | 6017 | if (chain == mEffectChains[i]) { | 
|  | 6018 | mEffectChains.removeAt(i); | 
| Eric Laurent | b469b94 | 2011-05-09 12:09:06 -0700 | [diff] [blame] | 6019 | // detach all active tracks from the chain | 
|  | 6020 | for (size_t i = 0 ; i < mActiveTracks.size() ; ++i) { | 
|  | 6021 | sp<Track> track = mActiveTracks[i].promote(); | 
|  | 6022 | if (track == 0) continue; | 
|  | 6023 | if (session == track->sessionId()) { | 
| Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 6024 | ALOGV("removeEffectChain_l(): stopping track on chain %p for session Id: %d", | 
| Eric Laurent | b469b94 | 2011-05-09 12:09:06 -0700 | [diff] [blame] | 6025 | chain.get(), session); | 
|  | 6026 | chain->decActiveTrackCnt(); | 
|  | 6027 | } | 
|  | 6028 | } | 
|  | 6029 |  | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6030 | // detach all tracks with same session ID from this chain | 
|  | 6031 | for (size_t i = 0; i < mTracks.size(); ++i) { | 
|  | 6032 | sp<Track> track = mTracks[i]; | 
|  | 6033 | if (session == track->sessionId()) { | 
|  | 6034 | track->setMainBuffer(mMixBuffer); | 
| Eric Laurent | b469b94 | 2011-05-09 12:09:06 -0700 | [diff] [blame] | 6035 | chain->decTrackCnt(); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6036 | } | 
|  | 6037 | } | 
| Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 6038 | break; | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6039 | } | 
|  | 6040 | } | 
|  | 6041 | return mEffectChains.size(); | 
|  | 6042 | } | 
|  | 6043 |  | 
| Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 6044 | status_t AudioFlinger::PlaybackThread::attachAuxEffect( | 
|  | 6045 | const sp<AudioFlinger::PlaybackThread::Track> track, int EffectId) | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6046 | { | 
|  | 6047 | Mutex::Autolock _l(mLock); | 
|  | 6048 | return attachAuxEffect_l(track, EffectId); | 
|  | 6049 | } | 
|  | 6050 |  | 
| Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 6051 | status_t AudioFlinger::PlaybackThread::attachAuxEffect_l( | 
|  | 6052 | const sp<AudioFlinger::PlaybackThread::Track> track, int EffectId) | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6053 | { | 
|  | 6054 | status_t status = NO_ERROR; | 
|  | 6055 |  | 
|  | 6056 | if (EffectId == 0) { | 
|  | 6057 | track->setAuxBuffer(0, NULL); | 
|  | 6058 | } else { | 
| Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 6059 | // Auxiliary effects are always in audio session AUDIO_SESSION_OUTPUT_MIX | 
|  | 6060 | sp<EffectModule> effect = getEffect_l(AUDIO_SESSION_OUTPUT_MIX, EffectId); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6061 | if (effect != 0) { | 
|  | 6062 | if ((effect->desc().flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) { | 
|  | 6063 | track->setAuxBuffer(EffectId, (int32_t *)effect->inBuffer()); | 
|  | 6064 | } else { | 
|  | 6065 | status = INVALID_OPERATION; | 
|  | 6066 | } | 
|  | 6067 | } else { | 
|  | 6068 | status = BAD_VALUE; | 
|  | 6069 | } | 
|  | 6070 | } | 
|  | 6071 | return status; | 
|  | 6072 | } | 
|  | 6073 |  | 
|  | 6074 | void AudioFlinger::PlaybackThread::detachAuxEffect_l(int effectId) | 
|  | 6075 | { | 
|  | 6076 | for (size_t i = 0; i < mTracks.size(); ++i) { | 
|  | 6077 | sp<Track> track = mTracks[i]; | 
|  | 6078 | if (track->auxEffectId() == effectId) { | 
|  | 6079 | attachAuxEffect_l(track, 0); | 
|  | 6080 | } | 
|  | 6081 | } | 
|  | 6082 | } | 
|  | 6083 |  | 
| Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 6084 | status_t AudioFlinger::RecordThread::addEffectChain_l(const sp<EffectChain>& chain) | 
|  | 6085 | { | 
|  | 6086 | // only one chain per input thread | 
|  | 6087 | if (mEffectChains.size() != 0) { | 
|  | 6088 | return INVALID_OPERATION; | 
|  | 6089 | } | 
| Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 6090 | ALOGV("addEffectChain_l() %p on thread %p", chain.get(), this); | 
| Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 6091 |  | 
|  | 6092 | chain->setInBuffer(NULL); | 
|  | 6093 | chain->setOutBuffer(NULL); | 
|  | 6094 |  | 
| Eric Laurent | 59255e4 | 2011-07-27 19:49:51 -0700 | [diff] [blame] | 6095 | checkSuspendOnAddEffectChain_l(chain); | 
|  | 6096 |  | 
| Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 6097 | mEffectChains.add(chain); | 
|  | 6098 |  | 
|  | 6099 | return NO_ERROR; | 
|  | 6100 | } | 
|  | 6101 |  | 
|  | 6102 | size_t AudioFlinger::RecordThread::removeEffectChain_l(const sp<EffectChain>& chain) | 
|  | 6103 | { | 
| Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 6104 | ALOGV("removeEffectChain_l() %p from thread %p", chain.get(), this); | 
| Steve Block | 5ff1dd5 | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 6105 | ALOGW_IF(mEffectChains.size() != 1, | 
| Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 6106 | "removeEffectChain_l() %p invalid chain size %d on thread %p", | 
|  | 6107 | chain.get(), mEffectChains.size(), this); | 
|  | 6108 | if (mEffectChains.size() == 1) { | 
|  | 6109 | mEffectChains.removeAt(0); | 
|  | 6110 | } | 
|  | 6111 | return 0; | 
|  | 6112 | } | 
|  | 6113 |  | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6114 | // ---------------------------------------------------------------------------- | 
|  | 6115 | //  EffectModule implementation | 
|  | 6116 | // ---------------------------------------------------------------------------- | 
|  | 6117 |  | 
|  | 6118 | #undef LOG_TAG | 
|  | 6119 | #define LOG_TAG "AudioFlinger::EffectModule" | 
|  | 6120 |  | 
|  | 6121 | AudioFlinger::EffectModule::EffectModule(const wp<ThreadBase>& wThread, | 
|  | 6122 | const wp<AudioFlinger::EffectChain>& chain, | 
|  | 6123 | effect_descriptor_t *desc, | 
|  | 6124 | int id, | 
|  | 6125 | int sessionId) | 
|  | 6126 | : mThread(wThread), mChain(chain), mId(id), mSessionId(sessionId), mEffectInterface(NULL), | 
| Eric Laurent | 59255e4 | 2011-07-27 19:49:51 -0700 | [diff] [blame] | 6127 | mStatus(NO_INIT), mState(IDLE), mSuspended(false) | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6128 | { | 
| Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 6129 | ALOGV("Constructor %p", this); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6130 | int lStatus; | 
|  | 6131 | sp<ThreadBase> thread = mThread.promote(); | 
|  | 6132 | if (thread == 0) { | 
|  | 6133 | return; | 
|  | 6134 | } | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6135 |  | 
|  | 6136 | memcpy(&mDescriptor, desc, sizeof(effect_descriptor_t)); | 
|  | 6137 |  | 
|  | 6138 | // create effect engine from effect factory | 
| Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 6139 | mStatus = EffectCreate(&desc->uuid, sessionId, thread->id(), &mEffectInterface); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6140 |  | 
|  | 6141 | if (mStatus != NO_ERROR) { | 
|  | 6142 | return; | 
|  | 6143 | } | 
|  | 6144 | lStatus = init(); | 
|  | 6145 | if (lStatus < 0) { | 
|  | 6146 | mStatus = lStatus; | 
|  | 6147 | goto Error; | 
|  | 6148 | } | 
|  | 6149 |  | 
| Marco Nelissen | 3a34bef | 2011-08-02 13:33:41 -0700 | [diff] [blame] | 6150 | if (mSessionId > AUDIO_SESSION_OUTPUT_MIX) { | 
|  | 6151 | mPinned = true; | 
|  | 6152 | } | 
| Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 6153 | ALOGV("Constructor success name %s, Interface %p", mDescriptor.name, mEffectInterface); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6154 | return; | 
|  | 6155 | Error: | 
|  | 6156 | EffectRelease(mEffectInterface); | 
|  | 6157 | mEffectInterface = NULL; | 
| Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 6158 | ALOGV("Constructor Error %d", mStatus); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6159 | } | 
|  | 6160 |  | 
|  | 6161 | AudioFlinger::EffectModule::~EffectModule() | 
|  | 6162 | { | 
| Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 6163 | ALOGV("Destructor %p", this); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6164 | if (mEffectInterface != NULL) { | 
| Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 6165 | if ((mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_PRE_PROC || | 
|  | 6166 | (mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_POST_PROC) { | 
|  | 6167 | sp<ThreadBase> thread = mThread.promote(); | 
|  | 6168 | if (thread != 0) { | 
| Eric Laurent | b8ba0a9 | 2011-08-07 16:32:26 -0700 | [diff] [blame] | 6169 | audio_stream_t *stream = thread->stream(); | 
|  | 6170 | if (stream != NULL) { | 
|  | 6171 | stream->remove_audio_effect(stream, mEffectInterface); | 
|  | 6172 | } | 
| Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 6173 | } | 
|  | 6174 | } | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6175 | // release effect engine | 
|  | 6176 | EffectRelease(mEffectInterface); | 
|  | 6177 | } | 
|  | 6178 | } | 
|  | 6179 |  | 
|  | 6180 | status_t AudioFlinger::EffectModule::addHandle(sp<EffectHandle>& handle) | 
|  | 6181 | { | 
|  | 6182 | status_t status; | 
|  | 6183 |  | 
|  | 6184 | Mutex::Autolock _l(mLock); | 
|  | 6185 | // First handle in mHandles has highest priority and controls the effect module | 
|  | 6186 | int priority = handle->priority(); | 
|  | 6187 | size_t size = mHandles.size(); | 
|  | 6188 | sp<EffectHandle> h; | 
|  | 6189 | size_t i; | 
|  | 6190 | for (i = 0; i < size; i++) { | 
|  | 6191 | h = mHandles[i].promote(); | 
|  | 6192 | if (h == 0) continue; | 
|  | 6193 | if (h->priority() <= priority) break; | 
|  | 6194 | } | 
|  | 6195 | // if inserted in first place, move effect control from previous owner to this handle | 
|  | 6196 | if (i == 0) { | 
| Eric Laurent | 59255e4 | 2011-07-27 19:49:51 -0700 | [diff] [blame] | 6197 | bool enabled = false; | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6198 | if (h != 0) { | 
| Eric Laurent | 59255e4 | 2011-07-27 19:49:51 -0700 | [diff] [blame] | 6199 | enabled = h->enabled(); | 
|  | 6200 | h->setControl(false/*hasControl*/, true /*signal*/, enabled /*enabled*/); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6201 | } | 
| Eric Laurent | 59255e4 | 2011-07-27 19:49:51 -0700 | [diff] [blame] | 6202 | handle->setControl(true /*hasControl*/, false /*signal*/, enabled /*enabled*/); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6203 | status = NO_ERROR; | 
|  | 6204 | } else { | 
|  | 6205 | status = ALREADY_EXISTS; | 
|  | 6206 | } | 
| Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 6207 | ALOGV("addHandle() %p added handle %p in position %d", this, handle.get(), i); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6208 | mHandles.insertAt(handle, i); | 
|  | 6209 | return status; | 
|  | 6210 | } | 
|  | 6211 |  | 
|  | 6212 | size_t AudioFlinger::EffectModule::removeHandle(const wp<EffectHandle>& handle) | 
|  | 6213 | { | 
|  | 6214 | Mutex::Autolock _l(mLock); | 
|  | 6215 | size_t size = mHandles.size(); | 
|  | 6216 | size_t i; | 
|  | 6217 | for (i = 0; i < size; i++) { | 
|  | 6218 | if (mHandles[i] == handle) break; | 
|  | 6219 | } | 
|  | 6220 | if (i == size) { | 
|  | 6221 | return size; | 
|  | 6222 | } | 
| Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 6223 | ALOGV("removeHandle() %p removed handle %p in position %d", this, handle.unsafe_get(), i); | 
| Eric Laurent | 59255e4 | 2011-07-27 19:49:51 -0700 | [diff] [blame] | 6224 |  | 
|  | 6225 | bool enabled = false; | 
|  | 6226 | EffectHandle *hdl = handle.unsafe_get(); | 
|  | 6227 | if (hdl) { | 
| Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 6228 | ALOGV("removeHandle() unsafe_get OK"); | 
| Eric Laurent | 59255e4 | 2011-07-27 19:49:51 -0700 | [diff] [blame] | 6229 | enabled = hdl->enabled(); | 
|  | 6230 | } | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6231 | mHandles.removeAt(i); | 
|  | 6232 | size = mHandles.size(); | 
|  | 6233 | // if removed from first place, move effect control from this handle to next in line | 
|  | 6234 | if (i == 0 && size != 0) { | 
|  | 6235 | sp<EffectHandle> h = mHandles[0].promote(); | 
|  | 6236 | if (h != 0) { | 
| Eric Laurent | 59255e4 | 2011-07-27 19:49:51 -0700 | [diff] [blame] | 6237 | h->setControl(true /*hasControl*/, true /*signal*/ , enabled /*enabled*/); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6238 | } | 
|  | 6239 | } | 
|  | 6240 |  | 
| Eric Laurent | ec437d8 | 2011-07-26 20:54:46 -0700 | [diff] [blame] | 6241 | // Prevent calls to process() and other functions on effect interface from now on. | 
|  | 6242 | // The effect engine will be released by the destructor when the last strong reference on | 
|  | 6243 | // this object is released which can happen after next process is called. | 
| Marco Nelissen | 3a34bef | 2011-08-02 13:33:41 -0700 | [diff] [blame] | 6244 | if (size == 0 && !mPinned) { | 
| Eric Laurent | ec437d8 | 2011-07-26 20:54:46 -0700 | [diff] [blame] | 6245 | mState = DESTROYED; | 
| Eric Laurent | dac6911 | 2010-09-28 14:09:57 -0700 | [diff] [blame] | 6246 | } | 
|  | 6247 |  | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6248 | return size; | 
|  | 6249 | } | 
|  | 6250 |  | 
| Eric Laurent | 59255e4 | 2011-07-27 19:49:51 -0700 | [diff] [blame] | 6251 | sp<AudioFlinger::EffectHandle> AudioFlinger::EffectModule::controlHandle() | 
|  | 6252 | { | 
|  | 6253 | Mutex::Autolock _l(mLock); | 
|  | 6254 | sp<EffectHandle> handle; | 
|  | 6255 | if (mHandles.size() != 0) { | 
|  | 6256 | handle = mHandles[0].promote(); | 
|  | 6257 | } | 
|  | 6258 | return handle; | 
|  | 6259 | } | 
|  | 6260 |  | 
| Marco Nelissen | 3a34bef | 2011-08-02 13:33:41 -0700 | [diff] [blame] | 6261 | void AudioFlinger::EffectModule::disconnect(const wp<EffectHandle>& handle, bool unpiniflast) | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6262 | { | 
| Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 6263 | ALOGV("disconnect() %p handle %p ", this, handle.unsafe_get()); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6264 | // keep a strong reference on this EffectModule to avoid calling the | 
|  | 6265 | // destructor before we exit | 
|  | 6266 | sp<EffectModule> keep(this); | 
|  | 6267 | { | 
|  | 6268 | sp<ThreadBase> thread = mThread.promote(); | 
|  | 6269 | if (thread != 0) { | 
| Marco Nelissen | 3a34bef | 2011-08-02 13:33:41 -0700 | [diff] [blame] | 6270 | thread->disconnectEffect(keep, handle, unpiniflast); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6271 | } | 
|  | 6272 | } | 
|  | 6273 | } | 
|  | 6274 |  | 
|  | 6275 | void AudioFlinger::EffectModule::updateState() { | 
|  | 6276 | Mutex::Autolock _l(mLock); | 
|  | 6277 |  | 
|  | 6278 | switch (mState) { | 
|  | 6279 | case RESTART: | 
|  | 6280 | reset_l(); | 
|  | 6281 | // FALL THROUGH | 
|  | 6282 |  | 
|  | 6283 | case STARTING: | 
|  | 6284 | // clear auxiliary effect input buffer for next accumulation | 
|  | 6285 | if ((mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) { | 
|  | 6286 | memset(mConfig.inputCfg.buffer.raw, | 
|  | 6287 | 0, | 
|  | 6288 | mConfig.inputCfg.buffer.frameCount*sizeof(int32_t)); | 
|  | 6289 | } | 
|  | 6290 | start_l(); | 
|  | 6291 | mState = ACTIVE; | 
|  | 6292 | break; | 
|  | 6293 | case STOPPING: | 
|  | 6294 | stop_l(); | 
|  | 6295 | mDisableWaitCnt = mMaxDisableWaitCnt; | 
|  | 6296 | mState = STOPPED; | 
|  | 6297 | break; | 
|  | 6298 | case STOPPED: | 
|  | 6299 | // mDisableWaitCnt is forced to 1 by process() when the engine indicates the end of the | 
|  | 6300 | // turn off sequence. | 
|  | 6301 | if (--mDisableWaitCnt == 0) { | 
|  | 6302 | reset_l(); | 
|  | 6303 | mState = IDLE; | 
|  | 6304 | } | 
|  | 6305 | break; | 
| Eric Laurent | ec437d8 | 2011-07-26 20:54:46 -0700 | [diff] [blame] | 6306 | default: //IDLE , ACTIVE, DESTROYED | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6307 | break; | 
|  | 6308 | } | 
|  | 6309 | } | 
|  | 6310 |  | 
|  | 6311 | void AudioFlinger::EffectModule::process() | 
|  | 6312 | { | 
|  | 6313 | Mutex::Autolock _l(mLock); | 
|  | 6314 |  | 
| Eric Laurent | ec437d8 | 2011-07-26 20:54:46 -0700 | [diff] [blame] | 6315 | if (mState == DESTROYED || mEffectInterface == NULL || | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6316 | mConfig.inputCfg.buffer.raw == NULL || | 
|  | 6317 | mConfig.outputCfg.buffer.raw == NULL) { | 
|  | 6318 | return; | 
|  | 6319 | } | 
|  | 6320 |  | 
| Eric Laurent | 8f45bd7 | 2010-08-31 13:50:07 -0700 | [diff] [blame] | 6321 | if (isProcessEnabled()) { | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6322 | // do 32 bit to 16 bit conversion for auxiliary effect input buffer | 
|  | 6323 | if ((mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) { | 
| Glenn Kasten | 3b21c50 | 2011-12-15 09:52:39 -0800 | [diff] [blame] | 6324 | ditherAndClamp(mConfig.inputCfg.buffer.s32, | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6325 | mConfig.inputCfg.buffer.s32, | 
| Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 6326 | mConfig.inputCfg.buffer.frameCount/2); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6327 | } | 
|  | 6328 |  | 
|  | 6329 | // do the actual processing in the effect engine | 
|  | 6330 | int ret = (*mEffectInterface)->process(mEffectInterface, | 
|  | 6331 | &mConfig.inputCfg.buffer, | 
|  | 6332 | &mConfig.outputCfg.buffer); | 
|  | 6333 |  | 
|  | 6334 | // force transition to IDLE state when engine is ready | 
|  | 6335 | if (mState == STOPPED && ret == -ENODATA) { | 
|  | 6336 | mDisableWaitCnt = 1; | 
|  | 6337 | } | 
|  | 6338 |  | 
|  | 6339 | // clear auxiliary effect input buffer for next accumulation | 
|  | 6340 | if ((mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) { | 
| Eric Laurent | 7333748 | 2011-01-19 18:36:13 -0800 | [diff] [blame] | 6341 | memset(mConfig.inputCfg.buffer.raw, 0, | 
|  | 6342 | mConfig.inputCfg.buffer.frameCount*sizeof(int32_t)); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6343 | } | 
|  | 6344 | } else if ((mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_INSERT && | 
| Eric Laurent | 7333748 | 2011-01-19 18:36:13 -0800 | [diff] [blame] | 6345 | mConfig.inputCfg.buffer.raw != mConfig.outputCfg.buffer.raw) { | 
|  | 6346 | // If an insert effect is idle and input buffer is different from output buffer, | 
|  | 6347 | // accumulate input onto output | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6348 | sp<EffectChain> chain = mChain.promote(); | 
| Eric Laurent | b469b94 | 2011-05-09 12:09:06 -0700 | [diff] [blame] | 6349 | if (chain != 0 && chain->activeTrackCnt() != 0) { | 
| Eric Laurent | 7333748 | 2011-01-19 18:36:13 -0800 | [diff] [blame] | 6350 | size_t frameCnt = mConfig.inputCfg.buffer.frameCount * 2;  //always stereo here | 
|  | 6351 | int16_t *in = mConfig.inputCfg.buffer.s16; | 
|  | 6352 | int16_t *out = mConfig.outputCfg.buffer.s16; | 
|  | 6353 | for (size_t i = 0; i < frameCnt; i++) { | 
|  | 6354 | out[i] = clamp16((int32_t)out[i] + (int32_t)in[i]); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6355 | } | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6356 | } | 
|  | 6357 | } | 
|  | 6358 | } | 
|  | 6359 |  | 
|  | 6360 | void AudioFlinger::EffectModule::reset_l() | 
|  | 6361 | { | 
|  | 6362 | if (mEffectInterface == NULL) { | 
|  | 6363 | return; | 
|  | 6364 | } | 
|  | 6365 | (*mEffectInterface)->command(mEffectInterface, EFFECT_CMD_RESET, 0, NULL, 0, NULL); | 
|  | 6366 | } | 
|  | 6367 |  | 
|  | 6368 | status_t AudioFlinger::EffectModule::configure() | 
|  | 6369 | { | 
|  | 6370 | uint32_t channels; | 
|  | 6371 | if (mEffectInterface == NULL) { | 
|  | 6372 | return NO_INIT; | 
|  | 6373 | } | 
|  | 6374 |  | 
|  | 6375 | sp<ThreadBase> thread = mThread.promote(); | 
|  | 6376 | if (thread == 0) { | 
|  | 6377 | return DEAD_OBJECT; | 
|  | 6378 | } | 
|  | 6379 |  | 
|  | 6380 | // TODO: handle configuration of effects replacing track process | 
|  | 6381 | if (thread->channelCount() == 1) { | 
| Eric Laurent | e1315cf | 2011-05-17 19:16:02 -0700 | [diff] [blame] | 6382 | channels = AUDIO_CHANNEL_OUT_MONO; | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6383 | } else { | 
| Eric Laurent | e1315cf | 2011-05-17 19:16:02 -0700 | [diff] [blame] | 6384 | channels = AUDIO_CHANNEL_OUT_STEREO; | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6385 | } | 
|  | 6386 |  | 
|  | 6387 | if ((mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) { | 
| Eric Laurent | e1315cf | 2011-05-17 19:16:02 -0700 | [diff] [blame] | 6388 | mConfig.inputCfg.channels = AUDIO_CHANNEL_OUT_MONO; | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6389 | } else { | 
|  | 6390 | mConfig.inputCfg.channels = channels; | 
|  | 6391 | } | 
|  | 6392 | mConfig.outputCfg.channels = channels; | 
| Eric Laurent | e1315cf | 2011-05-17 19:16:02 -0700 | [diff] [blame] | 6393 | mConfig.inputCfg.format = AUDIO_FORMAT_PCM_16_BIT; | 
|  | 6394 | mConfig.outputCfg.format = AUDIO_FORMAT_PCM_16_BIT; | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6395 | mConfig.inputCfg.samplingRate = thread->sampleRate(); | 
|  | 6396 | mConfig.outputCfg.samplingRate = mConfig.inputCfg.samplingRate; | 
|  | 6397 | mConfig.inputCfg.bufferProvider.cookie = NULL; | 
|  | 6398 | mConfig.inputCfg.bufferProvider.getBuffer = NULL; | 
|  | 6399 | mConfig.inputCfg.bufferProvider.releaseBuffer = NULL; | 
|  | 6400 | mConfig.outputCfg.bufferProvider.cookie = NULL; | 
|  | 6401 | mConfig.outputCfg.bufferProvider.getBuffer = NULL; | 
|  | 6402 | mConfig.outputCfg.bufferProvider.releaseBuffer = NULL; | 
|  | 6403 | mConfig.inputCfg.accessMode = EFFECT_BUFFER_ACCESS_READ; | 
|  | 6404 | // Insert effect: | 
| Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 6405 | // - in session AUDIO_SESSION_OUTPUT_MIX or AUDIO_SESSION_OUTPUT_STAGE, | 
| Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 6406 | // always overwrites output buffer: input buffer == output buffer | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6407 | // - in other sessions: | 
|  | 6408 | //      last effect in the chain accumulates in output buffer: input buffer != output buffer | 
|  | 6409 | //      other effect: overwrites output buffer: input buffer == output buffer | 
|  | 6410 | // Auxiliary effect: | 
|  | 6411 | //      accumulates in output buffer: input buffer != output buffer | 
|  | 6412 | // Therefore: accumulate <=> input buffer != output buffer | 
|  | 6413 | if (mConfig.inputCfg.buffer.raw != mConfig.outputCfg.buffer.raw) { | 
|  | 6414 | mConfig.outputCfg.accessMode = EFFECT_BUFFER_ACCESS_ACCUMULATE; | 
|  | 6415 | } else { | 
|  | 6416 | mConfig.outputCfg.accessMode = EFFECT_BUFFER_ACCESS_WRITE; | 
|  | 6417 | } | 
|  | 6418 | mConfig.inputCfg.mask = EFFECT_CONFIG_ALL; | 
|  | 6419 | mConfig.outputCfg.mask = EFFECT_CONFIG_ALL; | 
|  | 6420 | mConfig.inputCfg.buffer.frameCount = thread->frameCount(); | 
|  | 6421 | mConfig.outputCfg.buffer.frameCount = mConfig.inputCfg.buffer.frameCount; | 
|  | 6422 |  | 
| Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 6423 | ALOGV("configure() %p thread %p buffer %p framecount %d", | 
| Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 6424 | this, thread.get(), mConfig.inputCfg.buffer.raw, mConfig.inputCfg.buffer.frameCount); | 
|  | 6425 |  | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6426 | status_t cmdStatus; | 
| Eric Laurent | 25f4395 | 2010-07-28 05:40:18 -0700 | [diff] [blame] | 6427 | uint32_t size = sizeof(int); | 
|  | 6428 | status_t status = (*mEffectInterface)->command(mEffectInterface, | 
| Eric Laurent | 3d5188b | 2011-12-16 15:30:36 -0800 | [diff] [blame] | 6429 | EFFECT_CMD_SET_CONFIG, | 
| Eric Laurent | 25f4395 | 2010-07-28 05:40:18 -0700 | [diff] [blame] | 6430 | sizeof(effect_config_t), | 
|  | 6431 | &mConfig, | 
|  | 6432 | &size, | 
|  | 6433 | &cmdStatus); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6434 | if (status == 0) { | 
|  | 6435 | status = cmdStatus; | 
|  | 6436 | } | 
|  | 6437 |  | 
|  | 6438 | mMaxDisableWaitCnt = (MAX_DISABLE_TIME_MS * mConfig.outputCfg.samplingRate) / | 
|  | 6439 | (1000 * mConfig.outputCfg.buffer.frameCount); | 
|  | 6440 |  | 
|  | 6441 | return status; | 
|  | 6442 | } | 
|  | 6443 |  | 
|  | 6444 | status_t AudioFlinger::EffectModule::init() | 
|  | 6445 | { | 
|  | 6446 | Mutex::Autolock _l(mLock); | 
|  | 6447 | if (mEffectInterface == NULL) { | 
|  | 6448 | return NO_INIT; | 
|  | 6449 | } | 
|  | 6450 | status_t cmdStatus; | 
| Eric Laurent | 25f4395 | 2010-07-28 05:40:18 -0700 | [diff] [blame] | 6451 | uint32_t size = sizeof(status_t); | 
|  | 6452 | status_t status = (*mEffectInterface)->command(mEffectInterface, | 
|  | 6453 | EFFECT_CMD_INIT, | 
|  | 6454 | 0, | 
|  | 6455 | NULL, | 
|  | 6456 | &size, | 
|  | 6457 | &cmdStatus); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6458 | if (status == 0) { | 
|  | 6459 | status = cmdStatus; | 
|  | 6460 | } | 
|  | 6461 | return status; | 
|  | 6462 | } | 
|  | 6463 |  | 
| Eric Laurent | ec35a14 | 2011-10-05 17:42:25 -0700 | [diff] [blame] | 6464 | status_t AudioFlinger::EffectModule::start() | 
|  | 6465 | { | 
|  | 6466 | Mutex::Autolock _l(mLock); | 
|  | 6467 | return start_l(); | 
|  | 6468 | } | 
|  | 6469 |  | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6470 | status_t AudioFlinger::EffectModule::start_l() | 
|  | 6471 | { | 
|  | 6472 | if (mEffectInterface == NULL) { | 
|  | 6473 | return NO_INIT; | 
|  | 6474 | } | 
|  | 6475 | status_t cmdStatus; | 
| Eric Laurent | 25f4395 | 2010-07-28 05:40:18 -0700 | [diff] [blame] | 6476 | uint32_t size = sizeof(status_t); | 
|  | 6477 | status_t status = (*mEffectInterface)->command(mEffectInterface, | 
|  | 6478 | EFFECT_CMD_ENABLE, | 
|  | 6479 | 0, | 
|  | 6480 | NULL, | 
|  | 6481 | &size, | 
|  | 6482 | &cmdStatus); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6483 | if (status == 0) { | 
|  | 6484 | status = cmdStatus; | 
|  | 6485 | } | 
| Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 6486 | if (status == 0 && | 
|  | 6487 | ((mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_PRE_PROC || | 
|  | 6488 | (mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_POST_PROC)) { | 
|  | 6489 | sp<ThreadBase> thread = mThread.promote(); | 
|  | 6490 | if (thread != 0) { | 
| Eric Laurent | b8ba0a9 | 2011-08-07 16:32:26 -0700 | [diff] [blame] | 6491 | audio_stream_t *stream = thread->stream(); | 
|  | 6492 | if (stream != NULL) { | 
|  | 6493 | stream->add_audio_effect(stream, mEffectInterface); | 
|  | 6494 | } | 
| Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 6495 | } | 
|  | 6496 | } | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6497 | return status; | 
|  | 6498 | } | 
|  | 6499 |  | 
| Eric Laurent | ec437d8 | 2011-07-26 20:54:46 -0700 | [diff] [blame] | 6500 | status_t AudioFlinger::EffectModule::stop() | 
|  | 6501 | { | 
|  | 6502 | Mutex::Autolock _l(mLock); | 
|  | 6503 | return stop_l(); | 
|  | 6504 | } | 
|  | 6505 |  | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6506 | status_t AudioFlinger::EffectModule::stop_l() | 
|  | 6507 | { | 
|  | 6508 | if (mEffectInterface == NULL) { | 
|  | 6509 | return NO_INIT; | 
|  | 6510 | } | 
|  | 6511 | status_t cmdStatus; | 
| Eric Laurent | 25f4395 | 2010-07-28 05:40:18 -0700 | [diff] [blame] | 6512 | uint32_t size = sizeof(status_t); | 
|  | 6513 | status_t status = (*mEffectInterface)->command(mEffectInterface, | 
|  | 6514 | EFFECT_CMD_DISABLE, | 
|  | 6515 | 0, | 
|  | 6516 | NULL, | 
|  | 6517 | &size, | 
|  | 6518 | &cmdStatus); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6519 | if (status == 0) { | 
|  | 6520 | status = cmdStatus; | 
|  | 6521 | } | 
| Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 6522 | if (status == 0 && | 
|  | 6523 | ((mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_PRE_PROC || | 
|  | 6524 | (mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_POST_PROC)) { | 
|  | 6525 | sp<ThreadBase> thread = mThread.promote(); | 
|  | 6526 | if (thread != 0) { | 
| Eric Laurent | b8ba0a9 | 2011-08-07 16:32:26 -0700 | [diff] [blame] | 6527 | audio_stream_t *stream = thread->stream(); | 
|  | 6528 | if (stream != NULL) { | 
|  | 6529 | stream->remove_audio_effect(stream, mEffectInterface); | 
|  | 6530 | } | 
| Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 6531 | } | 
|  | 6532 | } | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6533 | return status; | 
|  | 6534 | } | 
|  | 6535 |  | 
| Eric Laurent | 25f4395 | 2010-07-28 05:40:18 -0700 | [diff] [blame] | 6536 | status_t AudioFlinger::EffectModule::command(uint32_t cmdCode, | 
|  | 6537 | uint32_t cmdSize, | 
|  | 6538 | void *pCmdData, | 
|  | 6539 | uint32_t *replySize, | 
|  | 6540 | void *pReplyData) | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6541 | { | 
|  | 6542 | Mutex::Autolock _l(mLock); | 
| Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 6543 | //    ALOGV("command(), cmdCode: %d, mEffectInterface: %p", cmdCode, mEffectInterface); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6544 |  | 
| Eric Laurent | ec437d8 | 2011-07-26 20:54:46 -0700 | [diff] [blame] | 6545 | if (mState == DESTROYED || mEffectInterface == NULL) { | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6546 | return NO_INIT; | 
|  | 6547 | } | 
| Eric Laurent | 25f4395 | 2010-07-28 05:40:18 -0700 | [diff] [blame] | 6548 | status_t status = (*mEffectInterface)->command(mEffectInterface, | 
|  | 6549 | cmdCode, | 
|  | 6550 | cmdSize, | 
|  | 6551 | pCmdData, | 
|  | 6552 | replySize, | 
|  | 6553 | pReplyData); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6554 | if (cmdCode != EFFECT_CMD_GET_PARAM && status == NO_ERROR) { | 
| Eric Laurent | 25f4395 | 2010-07-28 05:40:18 -0700 | [diff] [blame] | 6555 | uint32_t size = (replySize == NULL) ? 0 : *replySize; | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6556 | for (size_t i = 1; i < mHandles.size(); i++) { | 
|  | 6557 | sp<EffectHandle> h = mHandles[i].promote(); | 
|  | 6558 | if (h != 0) { | 
|  | 6559 | h->commandExecuted(cmdCode, cmdSize, pCmdData, size, pReplyData); | 
|  | 6560 | } | 
|  | 6561 | } | 
|  | 6562 | } | 
|  | 6563 | return status; | 
|  | 6564 | } | 
|  | 6565 |  | 
|  | 6566 | status_t AudioFlinger::EffectModule::setEnabled(bool enabled) | 
|  | 6567 | { | 
| Eric Laurent | db7c079 | 2011-08-10 10:37:50 -0700 | [diff] [blame] | 6568 |  | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6569 | Mutex::Autolock _l(mLock); | 
| Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 6570 | ALOGV("setEnabled %p enabled %d", this, enabled); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6571 |  | 
|  | 6572 | if (enabled != isEnabled()) { | 
| Eric Laurent | db7c079 | 2011-08-10 10:37:50 -0700 | [diff] [blame] | 6573 | status_t status = AudioSystem::setEffectEnabled(mId, enabled); | 
|  | 6574 | if (enabled && status != NO_ERROR) { | 
|  | 6575 | return status; | 
|  | 6576 | } | 
|  | 6577 |  | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6578 | switch (mState) { | 
|  | 6579 | // going from disabled to enabled | 
|  | 6580 | case IDLE: | 
|  | 6581 | mState = STARTING; | 
|  | 6582 | break; | 
|  | 6583 | case STOPPED: | 
|  | 6584 | mState = RESTART; | 
|  | 6585 | break; | 
|  | 6586 | case STOPPING: | 
|  | 6587 | mState = ACTIVE; | 
|  | 6588 | break; | 
|  | 6589 |  | 
|  | 6590 | // going from enabled to disabled | 
|  | 6591 | case RESTART: | 
| Eric Laurent | 8f45bd7 | 2010-08-31 13:50:07 -0700 | [diff] [blame] | 6592 | mState = STOPPED; | 
|  | 6593 | break; | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6594 | case STARTING: | 
|  | 6595 | mState = IDLE; | 
|  | 6596 | break; | 
|  | 6597 | case ACTIVE: | 
|  | 6598 | mState = STOPPING; | 
|  | 6599 | break; | 
| Eric Laurent | ec437d8 | 2011-07-26 20:54:46 -0700 | [diff] [blame] | 6600 | case DESTROYED: | 
|  | 6601 | return NO_ERROR; // simply ignore as we are being destroyed | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6602 | } | 
|  | 6603 | for (size_t i = 1; i < mHandles.size(); i++) { | 
|  | 6604 | sp<EffectHandle> h = mHandles[i].promote(); | 
|  | 6605 | if (h != 0) { | 
|  | 6606 | h->setEnabled(enabled); | 
|  | 6607 | } | 
|  | 6608 | } | 
|  | 6609 | } | 
|  | 6610 | return NO_ERROR; | 
|  | 6611 | } | 
|  | 6612 |  | 
|  | 6613 | bool AudioFlinger::EffectModule::isEnabled() | 
|  | 6614 | { | 
|  | 6615 | switch (mState) { | 
|  | 6616 | case RESTART: | 
|  | 6617 | case STARTING: | 
|  | 6618 | case ACTIVE: | 
|  | 6619 | return true; | 
|  | 6620 | case IDLE: | 
|  | 6621 | case STOPPING: | 
|  | 6622 | case STOPPED: | 
| Eric Laurent | ec437d8 | 2011-07-26 20:54:46 -0700 | [diff] [blame] | 6623 | case DESTROYED: | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6624 | default: | 
|  | 6625 | return false; | 
|  | 6626 | } | 
|  | 6627 | } | 
|  | 6628 |  | 
| Eric Laurent | 8f45bd7 | 2010-08-31 13:50:07 -0700 | [diff] [blame] | 6629 | bool AudioFlinger::EffectModule::isProcessEnabled() | 
|  | 6630 | { | 
|  | 6631 | switch (mState) { | 
|  | 6632 | case RESTART: | 
|  | 6633 | case ACTIVE: | 
|  | 6634 | case STOPPING: | 
|  | 6635 | case STOPPED: | 
|  | 6636 | return true; | 
|  | 6637 | case IDLE: | 
|  | 6638 | case STARTING: | 
| Eric Laurent | ec437d8 | 2011-07-26 20:54:46 -0700 | [diff] [blame] | 6639 | case DESTROYED: | 
| Eric Laurent | 8f45bd7 | 2010-08-31 13:50:07 -0700 | [diff] [blame] | 6640 | default: | 
|  | 6641 | return false; | 
|  | 6642 | } | 
|  | 6643 | } | 
|  | 6644 |  | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6645 | status_t AudioFlinger::EffectModule::setVolume(uint32_t *left, uint32_t *right, bool controller) | 
|  | 6646 | { | 
|  | 6647 | Mutex::Autolock _l(mLock); | 
|  | 6648 | status_t status = NO_ERROR; | 
|  | 6649 |  | 
|  | 6650 | // Send volume indication if EFFECT_FLAG_VOLUME_IND is set and read back altered volume | 
|  | 6651 | // if controller flag is set (Note that controller == TRUE => EFFECT_FLAG_VOLUME_CTRL set) | 
| Eric Laurent | 8f45bd7 | 2010-08-31 13:50:07 -0700 | [diff] [blame] | 6652 | if (isProcessEnabled() && | 
| Eric Laurent | f997cab | 2010-07-19 06:24:46 -0700 | [diff] [blame] | 6653 | ((mDescriptor.flags & EFFECT_FLAG_VOLUME_MASK) == EFFECT_FLAG_VOLUME_CTRL || | 
|  | 6654 | (mDescriptor.flags & EFFECT_FLAG_VOLUME_MASK) == EFFECT_FLAG_VOLUME_IND)) { | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6655 | status_t cmdStatus; | 
|  | 6656 | uint32_t volume[2]; | 
|  | 6657 | uint32_t *pVolume = NULL; | 
| Eric Laurent | 25f4395 | 2010-07-28 05:40:18 -0700 | [diff] [blame] | 6658 | uint32_t size = sizeof(volume); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6659 | volume[0] = *left; | 
|  | 6660 | volume[1] = *right; | 
|  | 6661 | if (controller) { | 
|  | 6662 | pVolume = volume; | 
|  | 6663 | } | 
| Eric Laurent | 25f4395 | 2010-07-28 05:40:18 -0700 | [diff] [blame] | 6664 | status = (*mEffectInterface)->command(mEffectInterface, | 
|  | 6665 | EFFECT_CMD_SET_VOLUME, | 
|  | 6666 | size, | 
|  | 6667 | volume, | 
|  | 6668 | &size, | 
|  | 6669 | pVolume); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6670 | if (controller && status == NO_ERROR && size == sizeof(volume)) { | 
|  | 6671 | *left = volume[0]; | 
|  | 6672 | *right = volume[1]; | 
|  | 6673 | } | 
|  | 6674 | } | 
|  | 6675 | return status; | 
|  | 6676 | } | 
|  | 6677 |  | 
|  | 6678 | status_t AudioFlinger::EffectModule::setDevice(uint32_t device) | 
|  | 6679 | { | 
|  | 6680 | Mutex::Autolock _l(mLock); | 
|  | 6681 | status_t status = NO_ERROR; | 
| Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 6682 | if (device && (mDescriptor.flags & EFFECT_FLAG_DEVICE_MASK) == EFFECT_FLAG_DEVICE_IND) { | 
|  | 6683 | // audio pre processing modules on RecordThread can receive both output and | 
|  | 6684 | // input device indication in the same call | 
|  | 6685 | uint32_t dev = device & AUDIO_DEVICE_OUT_ALL; | 
|  | 6686 | if (dev) { | 
|  | 6687 | status_t cmdStatus; | 
|  | 6688 | uint32_t size = sizeof(status_t); | 
|  | 6689 |  | 
|  | 6690 | status = (*mEffectInterface)->command(mEffectInterface, | 
|  | 6691 | EFFECT_CMD_SET_DEVICE, | 
|  | 6692 | sizeof(uint32_t), | 
|  | 6693 | &dev, | 
|  | 6694 | &size, | 
|  | 6695 | &cmdStatus); | 
|  | 6696 | if (status == NO_ERROR) { | 
|  | 6697 | status = cmdStatus; | 
|  | 6698 | } | 
|  | 6699 | } | 
|  | 6700 | dev = device & AUDIO_DEVICE_IN_ALL; | 
|  | 6701 | if (dev) { | 
|  | 6702 | status_t cmdStatus; | 
|  | 6703 | uint32_t size = sizeof(status_t); | 
|  | 6704 |  | 
|  | 6705 | status_t status2 = (*mEffectInterface)->command(mEffectInterface, | 
|  | 6706 | EFFECT_CMD_SET_INPUT_DEVICE, | 
|  | 6707 | sizeof(uint32_t), | 
|  | 6708 | &dev, | 
|  | 6709 | &size, | 
|  | 6710 | &cmdStatus); | 
|  | 6711 | if (status2 == NO_ERROR) { | 
|  | 6712 | status2 = cmdStatus; | 
|  | 6713 | } | 
|  | 6714 | if (status == NO_ERROR) { | 
|  | 6715 | status = status2; | 
|  | 6716 | } | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6717 | } | 
|  | 6718 | } | 
|  | 6719 | return status; | 
|  | 6720 | } | 
|  | 6721 |  | 
| Glenn Kasten | f78aee7 | 2012-01-04 11:00:47 -0800 | [diff] [blame] | 6722 | status_t AudioFlinger::EffectModule::setMode(audio_mode_t mode) | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6723 | { | 
|  | 6724 | Mutex::Autolock _l(mLock); | 
|  | 6725 | status_t status = NO_ERROR; | 
|  | 6726 | if ((mDescriptor.flags & EFFECT_FLAG_AUDIO_MODE_MASK) == EFFECT_FLAG_AUDIO_MODE_IND) { | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6727 | status_t cmdStatus; | 
| Eric Laurent | 25f4395 | 2010-07-28 05:40:18 -0700 | [diff] [blame] | 6728 | uint32_t size = sizeof(status_t); | 
|  | 6729 | status = (*mEffectInterface)->command(mEffectInterface, | 
|  | 6730 | EFFECT_CMD_SET_AUDIO_MODE, | 
| Glenn Kasten | f78aee7 | 2012-01-04 11:00:47 -0800 | [diff] [blame] | 6731 | sizeof(audio_mode_t), | 
| Eric Laurent | e1315cf | 2011-05-17 19:16:02 -0700 | [diff] [blame] | 6732 | &mode, | 
| Eric Laurent | 25f4395 | 2010-07-28 05:40:18 -0700 | [diff] [blame] | 6733 | &size, | 
|  | 6734 | &cmdStatus); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6735 | if (status == NO_ERROR) { | 
|  | 6736 | status = cmdStatus; | 
|  | 6737 | } | 
|  | 6738 | } | 
|  | 6739 | return status; | 
|  | 6740 | } | 
|  | 6741 |  | 
| Eric Laurent | 59255e4 | 2011-07-27 19:49:51 -0700 | [diff] [blame] | 6742 | void AudioFlinger::EffectModule::setSuspended(bool suspended) | 
|  | 6743 | { | 
|  | 6744 | Mutex::Autolock _l(mLock); | 
|  | 6745 | mSuspended = suspended; | 
|  | 6746 | } | 
| Glenn Kasten | a3a8548 | 2012-01-04 11:01:11 -0800 | [diff] [blame] | 6747 |  | 
|  | 6748 | bool AudioFlinger::EffectModule::suspended() const | 
| Eric Laurent | 59255e4 | 2011-07-27 19:49:51 -0700 | [diff] [blame] | 6749 | { | 
|  | 6750 | Mutex::Autolock _l(mLock); | 
|  | 6751 | return mSuspended; | 
|  | 6752 | } | 
|  | 6753 |  | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6754 | status_t AudioFlinger::EffectModule::dump(int fd, const Vector<String16>& args) | 
|  | 6755 | { | 
|  | 6756 | const size_t SIZE = 256; | 
|  | 6757 | char buffer[SIZE]; | 
|  | 6758 | String8 result; | 
|  | 6759 |  | 
|  | 6760 | snprintf(buffer, SIZE, "\tEffect ID %d:\n", mId); | 
|  | 6761 | result.append(buffer); | 
|  | 6762 |  | 
|  | 6763 | bool locked = tryLock(mLock); | 
|  | 6764 | // failed to lock - AudioFlinger is probably deadlocked | 
|  | 6765 | if (!locked) { | 
|  | 6766 | result.append("\t\tCould not lock Fx mutex:\n"); | 
|  | 6767 | } | 
|  | 6768 |  | 
|  | 6769 | result.append("\t\tSession Status State Engine:\n"); | 
|  | 6770 | snprintf(buffer, SIZE, "\t\t%05d   %03d    %03d   0x%08x\n", | 
|  | 6771 | mSessionId, mStatus, mState, (uint32_t)mEffectInterface); | 
|  | 6772 | result.append(buffer); | 
|  | 6773 |  | 
|  | 6774 | result.append("\t\tDescriptor:\n"); | 
|  | 6775 | snprintf(buffer, SIZE, "\t\t- UUID: %08X-%04X-%04X-%04X-%02X%02X%02X%02X%02X%02X\n", | 
|  | 6776 | mDescriptor.uuid.timeLow, mDescriptor.uuid.timeMid, mDescriptor.uuid.timeHiAndVersion, | 
|  | 6777 | mDescriptor.uuid.clockSeq, mDescriptor.uuid.node[0], mDescriptor.uuid.node[1],mDescriptor.uuid.node[2], | 
|  | 6778 | mDescriptor.uuid.node[3],mDescriptor.uuid.node[4],mDescriptor.uuid.node[5]); | 
|  | 6779 | result.append(buffer); | 
|  | 6780 | snprintf(buffer, SIZE, "\t\t- TYPE: %08X-%04X-%04X-%04X-%02X%02X%02X%02X%02X%02X\n", | 
|  | 6781 | mDescriptor.type.timeLow, mDescriptor.type.timeMid, mDescriptor.type.timeHiAndVersion, | 
|  | 6782 | mDescriptor.type.clockSeq, mDescriptor.type.node[0], mDescriptor.type.node[1],mDescriptor.type.node[2], | 
|  | 6783 | mDescriptor.type.node[3],mDescriptor.type.node[4],mDescriptor.type.node[5]); | 
|  | 6784 | result.append(buffer); | 
| Eric Laurent | e1315cf | 2011-05-17 19:16:02 -0700 | [diff] [blame] | 6785 | snprintf(buffer, SIZE, "\t\t- apiVersion: %08X\n\t\t- flags: %08X\n", | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6786 | mDescriptor.apiVersion, | 
|  | 6787 | mDescriptor.flags); | 
|  | 6788 | result.append(buffer); | 
|  | 6789 | snprintf(buffer, SIZE, "\t\t- name: %s\n", | 
|  | 6790 | mDescriptor.name); | 
|  | 6791 | result.append(buffer); | 
|  | 6792 | snprintf(buffer, SIZE, "\t\t- implementor: %s\n", | 
|  | 6793 | mDescriptor.implementor); | 
|  | 6794 | result.append(buffer); | 
|  | 6795 |  | 
|  | 6796 | result.append("\t\t- Input configuration:\n"); | 
|  | 6797 | result.append("\t\t\tBuffer     Frames  Smp rate Channels Format\n"); | 
|  | 6798 | snprintf(buffer, SIZE, "\t\t\t0x%08x %05d   %05d    %08x %d\n", | 
|  | 6799 | (uint32_t)mConfig.inputCfg.buffer.raw, | 
|  | 6800 | mConfig.inputCfg.buffer.frameCount, | 
|  | 6801 | mConfig.inputCfg.samplingRate, | 
|  | 6802 | mConfig.inputCfg.channels, | 
|  | 6803 | mConfig.inputCfg.format); | 
|  | 6804 | result.append(buffer); | 
|  | 6805 |  | 
|  | 6806 | result.append("\t\t- Output configuration:\n"); | 
|  | 6807 | result.append("\t\t\tBuffer     Frames  Smp rate Channels Format\n"); | 
|  | 6808 | snprintf(buffer, SIZE, "\t\t\t0x%08x %05d   %05d    %08x %d\n", | 
|  | 6809 | (uint32_t)mConfig.outputCfg.buffer.raw, | 
|  | 6810 | mConfig.outputCfg.buffer.frameCount, | 
|  | 6811 | mConfig.outputCfg.samplingRate, | 
|  | 6812 | mConfig.outputCfg.channels, | 
|  | 6813 | mConfig.outputCfg.format); | 
|  | 6814 | result.append(buffer); | 
|  | 6815 |  | 
|  | 6816 | snprintf(buffer, SIZE, "\t\t%d Clients:\n", mHandles.size()); | 
|  | 6817 | result.append(buffer); | 
|  | 6818 | result.append("\t\t\tPid   Priority Ctrl Locked client server\n"); | 
|  | 6819 | for (size_t i = 0; i < mHandles.size(); ++i) { | 
|  | 6820 | sp<EffectHandle> handle = mHandles[i].promote(); | 
|  | 6821 | if (handle != 0) { | 
|  | 6822 | handle->dump(buffer, SIZE); | 
|  | 6823 | result.append(buffer); | 
|  | 6824 | } | 
|  | 6825 | } | 
|  | 6826 |  | 
|  | 6827 | result.append("\n"); | 
|  | 6828 |  | 
|  | 6829 | write(fd, result.string(), result.length()); | 
|  | 6830 |  | 
|  | 6831 | if (locked) { | 
|  | 6832 | mLock.unlock(); | 
|  | 6833 | } | 
|  | 6834 |  | 
|  | 6835 | return NO_ERROR; | 
|  | 6836 | } | 
|  | 6837 |  | 
|  | 6838 | // ---------------------------------------------------------------------------- | 
|  | 6839 | //  EffectHandle implementation | 
|  | 6840 | // ---------------------------------------------------------------------------- | 
|  | 6841 |  | 
|  | 6842 | #undef LOG_TAG | 
|  | 6843 | #define LOG_TAG "AudioFlinger::EffectHandle" | 
|  | 6844 |  | 
|  | 6845 | AudioFlinger::EffectHandle::EffectHandle(const sp<EffectModule>& effect, | 
|  | 6846 | const sp<AudioFlinger::Client>& client, | 
|  | 6847 | const sp<IEffectClient>& effectClient, | 
|  | 6848 | int32_t priority) | 
|  | 6849 | : BnEffect(), | 
| Marco Nelissen | 3a34bef | 2011-08-02 13:33:41 -0700 | [diff] [blame] | 6850 | mEffect(effect), mEffectClient(effectClient), mClient(client), mCblk(NULL), | 
| Eric Laurent | 59255e4 | 2011-07-27 19:49:51 -0700 | [diff] [blame] | 6851 | mPriority(priority), mHasControl(false), mEnabled(false) | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6852 | { | 
| Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 6853 | ALOGV("constructor %p", this); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6854 |  | 
| Marco Nelissen | 3a34bef | 2011-08-02 13:33:41 -0700 | [diff] [blame] | 6855 | if (client == 0) { | 
|  | 6856 | return; | 
|  | 6857 | } | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6858 | int bufOffset = ((sizeof(effect_param_cblk_t) - 1) / sizeof(int) + 1) * sizeof(int); | 
|  | 6859 | mCblkMemory = client->heap()->allocate(EFFECT_PARAM_BUFFER_SIZE + bufOffset); | 
|  | 6860 | if (mCblkMemory != 0) { | 
|  | 6861 | mCblk = static_cast<effect_param_cblk_t *>(mCblkMemory->pointer()); | 
|  | 6862 |  | 
|  | 6863 | if (mCblk) { | 
|  | 6864 | new(mCblk) effect_param_cblk_t(); | 
|  | 6865 | mBuffer = (uint8_t *)mCblk + bufOffset; | 
|  | 6866 | } | 
|  | 6867 | } else { | 
| Steve Block | 29357bc | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 6868 | ALOGE("not enough memory for Effect size=%u", EFFECT_PARAM_BUFFER_SIZE + sizeof(effect_param_cblk_t)); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6869 | return; | 
|  | 6870 | } | 
|  | 6871 | } | 
|  | 6872 |  | 
|  | 6873 | AudioFlinger::EffectHandle::~EffectHandle() | 
|  | 6874 | { | 
| Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 6875 | ALOGV("Destructor %p", this); | 
| Marco Nelissen | 3a34bef | 2011-08-02 13:33:41 -0700 | [diff] [blame] | 6876 | disconnect(false); | 
| Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 6877 | ALOGV("Destructor DONE %p", this); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6878 | } | 
|  | 6879 |  | 
|  | 6880 | status_t AudioFlinger::EffectHandle::enable() | 
|  | 6881 | { | 
| Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 6882 | ALOGV("enable %p", this); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6883 | if (!mHasControl) return INVALID_OPERATION; | 
|  | 6884 | if (mEffect == 0) return DEAD_OBJECT; | 
|  | 6885 |  | 
| Eric Laurent | db7c079 | 2011-08-10 10:37:50 -0700 | [diff] [blame] | 6886 | if (mEnabled) { | 
|  | 6887 | return NO_ERROR; | 
|  | 6888 | } | 
|  | 6889 |  | 
| Eric Laurent | 59255e4 | 2011-07-27 19:49:51 -0700 | [diff] [blame] | 6890 | mEnabled = true; | 
|  | 6891 |  | 
|  | 6892 | sp<ThreadBase> thread = mEffect->thread().promote(); | 
|  | 6893 | if (thread != 0) { | 
|  | 6894 | thread->checkSuspendOnEffectEnabled(mEffect, true, mEffect->sessionId()); | 
|  | 6895 | } | 
|  | 6896 |  | 
|  | 6897 | // checkSuspendOnEffectEnabled() can suspend this same effect when enabled | 
|  | 6898 | if (mEffect->suspended()) { | 
|  | 6899 | return NO_ERROR; | 
|  | 6900 | } | 
|  | 6901 |  | 
| Eric Laurent | db7c079 | 2011-08-10 10:37:50 -0700 | [diff] [blame] | 6902 | status_t status = mEffect->setEnabled(true); | 
|  | 6903 | if (status != NO_ERROR) { | 
|  | 6904 | if (thread != 0) { | 
|  | 6905 | thread->checkSuspendOnEffectEnabled(mEffect, false, mEffect->sessionId()); | 
|  | 6906 | } | 
|  | 6907 | mEnabled = false; | 
|  | 6908 | } | 
|  | 6909 | return status; | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6910 | } | 
|  | 6911 |  | 
|  | 6912 | status_t AudioFlinger::EffectHandle::disable() | 
|  | 6913 | { | 
| Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 6914 | ALOGV("disable %p", this); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6915 | if (!mHasControl) return INVALID_OPERATION; | 
| Eric Laurent | 59255e4 | 2011-07-27 19:49:51 -0700 | [diff] [blame] | 6916 | if (mEffect == 0) return DEAD_OBJECT; | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6917 |  | 
| Eric Laurent | db7c079 | 2011-08-10 10:37:50 -0700 | [diff] [blame] | 6918 | if (!mEnabled) { | 
|  | 6919 | return NO_ERROR; | 
|  | 6920 | } | 
| Eric Laurent | 59255e4 | 2011-07-27 19:49:51 -0700 | [diff] [blame] | 6921 | mEnabled = false; | 
|  | 6922 |  | 
|  | 6923 | if (mEffect->suspended()) { | 
|  | 6924 | return NO_ERROR; | 
|  | 6925 | } | 
|  | 6926 |  | 
|  | 6927 | status_t status = mEffect->setEnabled(false); | 
|  | 6928 |  | 
|  | 6929 | sp<ThreadBase> thread = mEffect->thread().promote(); | 
|  | 6930 | if (thread != 0) { | 
|  | 6931 | thread->checkSuspendOnEffectEnabled(mEffect, false, mEffect->sessionId()); | 
|  | 6932 | } | 
|  | 6933 |  | 
|  | 6934 | return status; | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6935 | } | 
|  | 6936 |  | 
|  | 6937 | void AudioFlinger::EffectHandle::disconnect() | 
|  | 6938 | { | 
| Marco Nelissen | 3a34bef | 2011-08-02 13:33:41 -0700 | [diff] [blame] | 6939 | disconnect(true); | 
|  | 6940 | } | 
|  | 6941 |  | 
|  | 6942 | void AudioFlinger::EffectHandle::disconnect(bool unpiniflast) | 
|  | 6943 | { | 
| Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 6944 | ALOGV("disconnect(%s)", unpiniflast ? "true" : "false"); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6945 | if (mEffect == 0) { | 
|  | 6946 | return; | 
|  | 6947 | } | 
| Marco Nelissen | 3a34bef | 2011-08-02 13:33:41 -0700 | [diff] [blame] | 6948 | mEffect->disconnect(this, unpiniflast); | 
| Eric Laurent | 59255e4 | 2011-07-27 19:49:51 -0700 | [diff] [blame] | 6949 |  | 
| Eric Laurent | a85a74a | 2011-10-19 11:44:54 -0700 | [diff] [blame] | 6950 | if (mHasControl && mEnabled) { | 
| Eric Laurent | db7c079 | 2011-08-10 10:37:50 -0700 | [diff] [blame] | 6951 | sp<ThreadBase> thread = mEffect->thread().promote(); | 
|  | 6952 | if (thread != 0) { | 
|  | 6953 | thread->checkSuspendOnEffectEnabled(mEffect, false, mEffect->sessionId()); | 
|  | 6954 | } | 
| Eric Laurent | 59255e4 | 2011-07-27 19:49:51 -0700 | [diff] [blame] | 6955 | } | 
|  | 6956 |  | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6957 | // release sp on module => module destructor can be called now | 
|  | 6958 | mEffect.clear(); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6959 | if (mClient != 0) { | 
| Marco Nelissen | 3a34bef | 2011-08-02 13:33:41 -0700 | [diff] [blame] | 6960 | if (mCblk) { | 
|  | 6961 | mCblk->~effect_param_cblk_t();   // destroy our shared-structure. | 
|  | 6962 | } | 
|  | 6963 | mCblkMemory.clear();            // and free the shared memory | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6964 | Mutex::Autolock _l(mClient->audioFlinger()->mLock); | 
|  | 6965 | mClient.clear(); | 
|  | 6966 | } | 
|  | 6967 | } | 
|  | 6968 |  | 
| Eric Laurent | 25f4395 | 2010-07-28 05:40:18 -0700 | [diff] [blame] | 6969 | status_t AudioFlinger::EffectHandle::command(uint32_t cmdCode, | 
|  | 6970 | uint32_t cmdSize, | 
|  | 6971 | void *pCmdData, | 
|  | 6972 | uint32_t *replySize, | 
|  | 6973 | void *pReplyData) | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6974 | { | 
| Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 6975 | //    ALOGV("command(), cmdCode: %d, mHasControl: %d, mEffect: %p", | 
| Eric Laurent | 25f4395 | 2010-07-28 05:40:18 -0700 | [diff] [blame] | 6976 | //              cmdCode, mHasControl, (mEffect == 0) ? 0 : mEffect.get()); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6977 |  | 
|  | 6978 | // only get parameter command is permitted for applications not controlling the effect | 
|  | 6979 | if (!mHasControl && cmdCode != EFFECT_CMD_GET_PARAM) { | 
|  | 6980 | return INVALID_OPERATION; | 
|  | 6981 | } | 
|  | 6982 | if (mEffect == 0) return DEAD_OBJECT; | 
| Marco Nelissen | 3a34bef | 2011-08-02 13:33:41 -0700 | [diff] [blame] | 6983 | if (mClient == 0) return INVALID_OPERATION; | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6984 |  | 
|  | 6985 | // handle commands that are not forwarded transparently to effect engine | 
|  | 6986 | if (cmdCode == EFFECT_CMD_SET_PARAM_COMMIT) { | 
|  | 6987 | // No need to trylock() here as this function is executed in the binder thread serving a particular client process: | 
|  | 6988 | // no risk to block the whole media server process or mixer threads is we are stuck here | 
|  | 6989 | Mutex::Autolock _l(mCblk->lock); | 
|  | 6990 | if (mCblk->clientIndex > EFFECT_PARAM_BUFFER_SIZE || | 
|  | 6991 | mCblk->serverIndex > EFFECT_PARAM_BUFFER_SIZE) { | 
|  | 6992 | mCblk->serverIndex = 0; | 
|  | 6993 | mCblk->clientIndex = 0; | 
|  | 6994 | return BAD_VALUE; | 
|  | 6995 | } | 
|  | 6996 | status_t status = NO_ERROR; | 
|  | 6997 | while (mCblk->serverIndex < mCblk->clientIndex) { | 
|  | 6998 | int reply; | 
| Eric Laurent | 25f4395 | 2010-07-28 05:40:18 -0700 | [diff] [blame] | 6999 | uint32_t rsize = sizeof(int); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 7000 | int *p = (int *)(mBuffer + mCblk->serverIndex); | 
|  | 7001 | int size = *p++; | 
|  | 7002 | if (((uint8_t *)p + size) > mBuffer + mCblk->clientIndex) { | 
| Steve Block | 5ff1dd5 | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 7003 | ALOGW("command(): invalid parameter block size"); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 7004 | break; | 
|  | 7005 | } | 
|  | 7006 | effect_param_t *param = (effect_param_t *)p; | 
|  | 7007 | if (param->psize == 0 || param->vsize == 0) { | 
| Steve Block | 5ff1dd5 | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 7008 | ALOGW("command(): null parameter or value size"); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 7009 | mCblk->serverIndex += size; | 
|  | 7010 | continue; | 
|  | 7011 | } | 
| Eric Laurent | 25f4395 | 2010-07-28 05:40:18 -0700 | [diff] [blame] | 7012 | uint32_t psize = sizeof(effect_param_t) + | 
|  | 7013 | ((param->psize - 1) / sizeof(int) + 1) * sizeof(int) + | 
|  | 7014 | param->vsize; | 
|  | 7015 | status_t ret = mEffect->command(EFFECT_CMD_SET_PARAM, | 
|  | 7016 | psize, | 
|  | 7017 | p, | 
|  | 7018 | &rsize, | 
|  | 7019 | &reply); | 
| Eric Laurent | aeae3de | 2010-09-02 11:56:55 -0700 | [diff] [blame] | 7020 | // stop at first error encountered | 
|  | 7021 | if (ret != NO_ERROR) { | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 7022 | status = ret; | 
| Eric Laurent | aeae3de | 2010-09-02 11:56:55 -0700 | [diff] [blame] | 7023 | *(int *)pReplyData = reply; | 
|  | 7024 | break; | 
|  | 7025 | } else if (reply != NO_ERROR) { | 
|  | 7026 | *(int *)pReplyData = reply; | 
|  | 7027 | break; | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 7028 | } | 
|  | 7029 | mCblk->serverIndex += size; | 
|  | 7030 | } | 
|  | 7031 | mCblk->serverIndex = 0; | 
|  | 7032 | mCblk->clientIndex = 0; | 
|  | 7033 | return status; | 
|  | 7034 | } else if (cmdCode == EFFECT_CMD_ENABLE) { | 
| Eric Laurent | aeae3de | 2010-09-02 11:56:55 -0700 | [diff] [blame] | 7035 | *(int *)pReplyData = NO_ERROR; | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 7036 | return enable(); | 
|  | 7037 | } else if (cmdCode == EFFECT_CMD_DISABLE) { | 
| Eric Laurent | aeae3de | 2010-09-02 11:56:55 -0700 | [diff] [blame] | 7038 | *(int *)pReplyData = NO_ERROR; | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 7039 | return disable(); | 
|  | 7040 | } | 
|  | 7041 |  | 
|  | 7042 | return mEffect->command(cmdCode, cmdSize, pCmdData, replySize, pReplyData); | 
|  | 7043 | } | 
|  | 7044 |  | 
|  | 7045 | sp<IMemory> AudioFlinger::EffectHandle::getCblk() const { | 
|  | 7046 | return mCblkMemory; | 
|  | 7047 | } | 
|  | 7048 |  | 
| Eric Laurent | 59255e4 | 2011-07-27 19:49:51 -0700 | [diff] [blame] | 7049 | void AudioFlinger::EffectHandle::setControl(bool hasControl, bool signal, bool enabled) | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 7050 | { | 
| Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 7051 | ALOGV("setControl %p control %d", this, hasControl); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 7052 |  | 
|  | 7053 | mHasControl = hasControl; | 
| Eric Laurent | 59255e4 | 2011-07-27 19:49:51 -0700 | [diff] [blame] | 7054 | mEnabled = enabled; | 
|  | 7055 |  | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 7056 | if (signal && mEffectClient != 0) { | 
|  | 7057 | mEffectClient->controlStatusChanged(hasControl); | 
|  | 7058 | } | 
|  | 7059 | } | 
|  | 7060 |  | 
| Eric Laurent | 25f4395 | 2010-07-28 05:40:18 -0700 | [diff] [blame] | 7061 | void AudioFlinger::EffectHandle::commandExecuted(uint32_t cmdCode, | 
|  | 7062 | uint32_t cmdSize, | 
|  | 7063 | void *pCmdData, | 
|  | 7064 | uint32_t replySize, | 
|  | 7065 | void *pReplyData) | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 7066 | { | 
|  | 7067 | if (mEffectClient != 0) { | 
|  | 7068 | mEffectClient->commandExecuted(cmdCode, cmdSize, pCmdData, replySize, pReplyData); | 
|  | 7069 | } | 
|  | 7070 | } | 
|  | 7071 |  | 
|  | 7072 |  | 
|  | 7073 |  | 
|  | 7074 | void AudioFlinger::EffectHandle::setEnabled(bool enabled) | 
|  | 7075 | { | 
|  | 7076 | if (mEffectClient != 0) { | 
|  | 7077 | mEffectClient->enableStatusChanged(enabled); | 
|  | 7078 | } | 
|  | 7079 | } | 
|  | 7080 |  | 
|  | 7081 | status_t AudioFlinger::EffectHandle::onTransact( | 
|  | 7082 | uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags) | 
|  | 7083 | { | 
|  | 7084 | return BnEffect::onTransact(code, data, reply, flags); | 
|  | 7085 | } | 
|  | 7086 |  | 
|  | 7087 |  | 
|  | 7088 | void AudioFlinger::EffectHandle::dump(char* buffer, size_t size) | 
|  | 7089 | { | 
| Marco Nelissen | 3a34bef | 2011-08-02 13:33:41 -0700 | [diff] [blame] | 7090 | bool locked = mCblk ? tryLock(mCblk->lock) : false; | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 7091 |  | 
|  | 7092 | snprintf(buffer, size, "\t\t\t%05d %05d    %01u    %01u      %05u  %05u\n", | 
|  | 7093 | (mClient == NULL) ? getpid() : mClient->pid(), | 
|  | 7094 | mPriority, | 
|  | 7095 | mHasControl, | 
|  | 7096 | !locked, | 
| Marco Nelissen | 3a34bef | 2011-08-02 13:33:41 -0700 | [diff] [blame] | 7097 | mCblk ? mCblk->clientIndex : 0, | 
|  | 7098 | mCblk ? mCblk->serverIndex : 0 | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 7099 | ); | 
|  | 7100 |  | 
|  | 7101 | if (locked) { | 
|  | 7102 | mCblk->lock.unlock(); | 
|  | 7103 | } | 
|  | 7104 | } | 
|  | 7105 |  | 
|  | 7106 | #undef LOG_TAG | 
|  | 7107 | #define LOG_TAG "AudioFlinger::EffectChain" | 
|  | 7108 |  | 
|  | 7109 | AudioFlinger::EffectChain::EffectChain(const wp<ThreadBase>& wThread, | 
|  | 7110 | int sessionId) | 
| Eric Laurent | 544fe9b | 2011-11-11 15:42:52 -0800 | [diff] [blame] | 7111 | : mThread(wThread), mSessionId(sessionId), mActiveTrackCnt(0), mTrackCnt(0), mTailBufferCount(0), | 
| Eric Laurent | b469b94 | 2011-05-09 12:09:06 -0700 | [diff] [blame] | 7112 | mOwnInBuffer(false), mVolumeCtrlIdx(-1), mLeftVolume(UINT_MAX), mRightVolume(UINT_MAX), | 
|  | 7113 | mNewLeftVolume(UINT_MAX), mNewRightVolume(UINT_MAX) | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 7114 | { | 
| Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 7115 | mStrategy = AudioSystem::getStrategyForStream(AUDIO_STREAM_MUSIC); | 
| Eric Laurent | 544fe9b | 2011-11-11 15:42:52 -0800 | [diff] [blame] | 7116 | sp<ThreadBase> thread = mThread.promote(); | 
|  | 7117 | if (thread == 0) { | 
|  | 7118 | return; | 
|  | 7119 | } | 
|  | 7120 | mMaxTailBuffers = ((kProcessTailDurationMs * thread->sampleRate()) / 1000) / | 
|  | 7121 | thread->frameCount(); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 7122 | } | 
|  | 7123 |  | 
|  | 7124 | AudioFlinger::EffectChain::~EffectChain() | 
|  | 7125 | { | 
|  | 7126 | if (mOwnInBuffer) { | 
|  | 7127 | delete mInBuffer; | 
|  | 7128 | } | 
|  | 7129 |  | 
|  | 7130 | } | 
|  | 7131 |  | 
| Eric Laurent | 59255e4 | 2011-07-27 19:49:51 -0700 | [diff] [blame] | 7132 | // getEffectFromDesc_l() must be called with ThreadBase::mLock held | 
| Eric Laurent | cab1124 | 2010-07-15 12:50:15 -0700 | [diff] [blame] | 7133 | sp<AudioFlinger::EffectModule> AudioFlinger::EffectChain::getEffectFromDesc_l(effect_descriptor_t *descriptor) | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 7134 | { | 
|  | 7135 | sp<EffectModule> effect; | 
|  | 7136 | size_t size = mEffects.size(); | 
|  | 7137 |  | 
|  | 7138 | for (size_t i = 0; i < size; i++) { | 
|  | 7139 | if (memcmp(&mEffects[i]->desc().uuid, &descriptor->uuid, sizeof(effect_uuid_t)) == 0) { | 
|  | 7140 | effect = mEffects[i]; | 
|  | 7141 | break; | 
|  | 7142 | } | 
|  | 7143 | } | 
|  | 7144 | return effect; | 
|  | 7145 | } | 
|  | 7146 |  | 
| Eric Laurent | 59255e4 | 2011-07-27 19:49:51 -0700 | [diff] [blame] | 7147 | // getEffectFromId_l() must be called with ThreadBase::mLock held | 
| Eric Laurent | cab1124 | 2010-07-15 12:50:15 -0700 | [diff] [blame] | 7148 | sp<AudioFlinger::EffectModule> AudioFlinger::EffectChain::getEffectFromId_l(int id) | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 7149 | { | 
|  | 7150 | sp<EffectModule> effect; | 
|  | 7151 | size_t size = mEffects.size(); | 
|  | 7152 |  | 
|  | 7153 | for (size_t i = 0; i < size; i++) { | 
| Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 7154 | // by convention, return first effect if id provided is 0 (0 is never a valid id) | 
|  | 7155 | if (id == 0 || mEffects[i]->id() == id) { | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 7156 | effect = mEffects[i]; | 
|  | 7157 | break; | 
|  | 7158 | } | 
|  | 7159 | } | 
|  | 7160 | return effect; | 
|  | 7161 | } | 
|  | 7162 |  | 
| Eric Laurent | 59255e4 | 2011-07-27 19:49:51 -0700 | [diff] [blame] | 7163 | // getEffectFromType_l() must be called with ThreadBase::mLock held | 
|  | 7164 | sp<AudioFlinger::EffectModule> AudioFlinger::EffectChain::getEffectFromType_l( | 
|  | 7165 | const effect_uuid_t *type) | 
|  | 7166 | { | 
|  | 7167 | sp<EffectModule> effect; | 
|  | 7168 | size_t size = mEffects.size(); | 
|  | 7169 |  | 
|  | 7170 | for (size_t i = 0; i < size; i++) { | 
|  | 7171 | if (memcmp(&mEffects[i]->desc().type, type, sizeof(effect_uuid_t)) == 0) { | 
|  | 7172 | effect = mEffects[i]; | 
|  | 7173 | break; | 
|  | 7174 | } | 
|  | 7175 | } | 
|  | 7176 | return effect; | 
|  | 7177 | } | 
|  | 7178 |  | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 7179 | // Must be called with EffectChain::mLock locked | 
|  | 7180 | void AudioFlinger::EffectChain::process_l() | 
|  | 7181 | { | 
| Eric Laurent | dac6911 | 2010-09-28 14:09:57 -0700 | [diff] [blame] | 7182 | sp<ThreadBase> thread = mThread.promote(); | 
|  | 7183 | if (thread == 0) { | 
| Steve Block | 5ff1dd5 | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 7184 | ALOGW("process_l(): cannot promote mixer thread"); | 
| Eric Laurent | dac6911 | 2010-09-28 14:09:57 -0700 | [diff] [blame] | 7185 | return; | 
|  | 7186 | } | 
| Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 7187 | bool isGlobalSession = (mSessionId == AUDIO_SESSION_OUTPUT_MIX) || | 
|  | 7188 | (mSessionId == AUDIO_SESSION_OUTPUT_STAGE); | 
| Eric Laurent | 544fe9b | 2011-11-11 15:42:52 -0800 | [diff] [blame] | 7189 | // always process effects unless no more tracks are on the session and the effect tail | 
|  | 7190 | // has been rendered | 
|  | 7191 | bool doProcess = true; | 
| Eric Laurent | dac6911 | 2010-09-28 14:09:57 -0700 | [diff] [blame] | 7192 | if (!isGlobalSession) { | 
| Eric Laurent | 544fe9b | 2011-11-11 15:42:52 -0800 | [diff] [blame] | 7193 | bool tracksOnSession = (trackCnt() != 0); | 
| Eric Laurent | b469b94 | 2011-05-09 12:09:06 -0700 | [diff] [blame] | 7194 |  | 
| Eric Laurent | 544fe9b | 2011-11-11 15:42:52 -0800 | [diff] [blame] | 7195 | if (!tracksOnSession && mTailBufferCount == 0) { | 
|  | 7196 | doProcess = false; | 
|  | 7197 | } | 
|  | 7198 |  | 
|  | 7199 | if (activeTrackCnt() == 0) { | 
|  | 7200 | // if no track is active and the effect tail has not been rendered, | 
|  | 7201 | // the input buffer must be cleared here as the mixer process will not do it | 
|  | 7202 | if (tracksOnSession || mTailBufferCount > 0) { | 
|  | 7203 | size_t numSamples = thread->frameCount() * thread->channelCount(); | 
|  | 7204 | memset(mInBuffer, 0, numSamples * sizeof(int16_t)); | 
|  | 7205 | if (mTailBufferCount > 0) { | 
|  | 7206 | mTailBufferCount--; | 
|  | 7207 | } | 
|  | 7208 | } | 
|  | 7209 | } | 
| Eric Laurent | dac6911 | 2010-09-28 14:09:57 -0700 | [diff] [blame] | 7210 | } | 
|  | 7211 |  | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 7212 | size_t size = mEffects.size(); | 
| Eric Laurent | 544fe9b | 2011-11-11 15:42:52 -0800 | [diff] [blame] | 7213 | if (doProcess) { | 
| Eric Laurent | dac6911 | 2010-09-28 14:09:57 -0700 | [diff] [blame] | 7214 | for (size_t i = 0; i < size; i++) { | 
|  | 7215 | mEffects[i]->process(); | 
|  | 7216 | } | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 7217 | } | 
|  | 7218 | for (size_t i = 0; i < size; i++) { | 
|  | 7219 | mEffects[i]->updateState(); | 
|  | 7220 | } | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 7221 | } | 
|  | 7222 |  | 
| Eric Laurent | cab1124 | 2010-07-15 12:50:15 -0700 | [diff] [blame] | 7223 | // addEffect_l() must be called with PlaybackThread::mLock held | 
| Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 7224 | status_t AudioFlinger::EffectChain::addEffect_l(const sp<EffectModule>& effect) | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 7225 | { | 
|  | 7226 | effect_descriptor_t desc = effect->desc(); | 
|  | 7227 | uint32_t insertPref = desc.flags & EFFECT_FLAG_INSERT_MASK; | 
|  | 7228 |  | 
|  | 7229 | Mutex::Autolock _l(mLock); | 
| Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 7230 | effect->setChain(this); | 
|  | 7231 | sp<ThreadBase> thread = mThread.promote(); | 
|  | 7232 | if (thread == 0) { | 
|  | 7233 | return NO_INIT; | 
|  | 7234 | } | 
|  | 7235 | effect->setThread(thread); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 7236 |  | 
|  | 7237 | if ((desc.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) { | 
|  | 7238 | // Auxiliary effects are inserted at the beginning of mEffects vector as | 
|  | 7239 | // they are processed first and accumulated in chain input buffer | 
|  | 7240 | mEffects.insertAt(effect, 0); | 
| Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 7241 |  | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 7242 | // the input buffer for auxiliary effect contains mono samples in | 
|  | 7243 | // 32 bit format. This is to avoid saturation in AudoMixer | 
|  | 7244 | // accumulation stage. Saturation is done in EffectModule::process() before | 
|  | 7245 | // calling the process in effect engine | 
|  | 7246 | size_t numSamples = thread->frameCount(); | 
|  | 7247 | int32_t *buffer = new int32_t[numSamples]; | 
|  | 7248 | memset(buffer, 0, numSamples * sizeof(int32_t)); | 
|  | 7249 | effect->setInBuffer((int16_t *)buffer); | 
|  | 7250 | // auxiliary effects output samples to chain input buffer for further processing | 
|  | 7251 | // by insert effects | 
|  | 7252 | effect->setOutBuffer(mInBuffer); | 
|  | 7253 | } else { | 
|  | 7254 | // Insert effects are inserted at the end of mEffects vector as they are processed | 
|  | 7255 | //  after track and auxiliary effects. | 
|  | 7256 | // Insert effect order as a function of indicated preference: | 
|  | 7257 | //  if EFFECT_FLAG_INSERT_EXCLUSIVE, insert in first position or reject if | 
|  | 7258 | //  another effect is present | 
|  | 7259 | //  else if EFFECT_FLAG_INSERT_FIRST, insert in first position or after the | 
|  | 7260 | //  last effect claiming first position | 
|  | 7261 | //  else if EFFECT_FLAG_INSERT_LAST, insert in last position or before the | 
|  | 7262 | //  first effect claiming last position | 
|  | 7263 | //  else if EFFECT_FLAG_INSERT_ANY insert after first or before last | 
|  | 7264 | // Reject insertion if an effect with EFFECT_FLAG_INSERT_EXCLUSIVE is | 
|  | 7265 | // already present | 
|  | 7266 |  | 
|  | 7267 | int size = (int)mEffects.size(); | 
|  | 7268 | int idx_insert = size; | 
|  | 7269 | int idx_insert_first = -1; | 
|  | 7270 | int idx_insert_last = -1; | 
|  | 7271 |  | 
|  | 7272 | for (int i = 0; i < size; i++) { | 
|  | 7273 | effect_descriptor_t d = mEffects[i]->desc(); | 
|  | 7274 | uint32_t iMode = d.flags & EFFECT_FLAG_TYPE_MASK; | 
|  | 7275 | uint32_t iPref = d.flags & EFFECT_FLAG_INSERT_MASK; | 
|  | 7276 | if (iMode == EFFECT_FLAG_TYPE_INSERT) { | 
|  | 7277 | // check invalid effect chaining combinations | 
|  | 7278 | if (insertPref == EFFECT_FLAG_INSERT_EXCLUSIVE || | 
|  | 7279 | iPref == EFFECT_FLAG_INSERT_EXCLUSIVE) { | 
| Steve Block | 5ff1dd5 | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 7280 | ALOGW("addEffect_l() could not insert effect %s: exclusive conflict with %s", desc.name, d.name); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 7281 | return INVALID_OPERATION; | 
|  | 7282 | } | 
|  | 7283 | // remember position of first insert effect and by default | 
|  | 7284 | // select this as insert position for new effect | 
|  | 7285 | if (idx_insert == size) { | 
|  | 7286 | idx_insert = i; | 
|  | 7287 | } | 
|  | 7288 | // remember position of last insert effect claiming | 
|  | 7289 | // first position | 
|  | 7290 | if (iPref == EFFECT_FLAG_INSERT_FIRST) { | 
|  | 7291 | idx_insert_first = i; | 
|  | 7292 | } | 
|  | 7293 | // remember position of first insert effect claiming | 
|  | 7294 | // last position | 
|  | 7295 | if (iPref == EFFECT_FLAG_INSERT_LAST && | 
|  | 7296 | idx_insert_last == -1) { | 
|  | 7297 | idx_insert_last = i; | 
|  | 7298 | } | 
|  | 7299 | } | 
|  | 7300 | } | 
|  | 7301 |  | 
|  | 7302 | // modify idx_insert from first position if needed | 
|  | 7303 | if (insertPref == EFFECT_FLAG_INSERT_LAST) { | 
|  | 7304 | if (idx_insert_last != -1) { | 
|  | 7305 | idx_insert = idx_insert_last; | 
|  | 7306 | } else { | 
|  | 7307 | idx_insert = size; | 
|  | 7308 | } | 
|  | 7309 | } else { | 
|  | 7310 | if (idx_insert_first != -1) { | 
|  | 7311 | idx_insert = idx_insert_first + 1; | 
|  | 7312 | } | 
|  | 7313 | } | 
|  | 7314 |  | 
|  | 7315 | // always read samples from chain input buffer | 
|  | 7316 | effect->setInBuffer(mInBuffer); | 
|  | 7317 |  | 
|  | 7318 | // if last effect in the chain, output samples to chain | 
|  | 7319 | // output buffer, otherwise to chain input buffer | 
|  | 7320 | if (idx_insert == size) { | 
|  | 7321 | if (idx_insert != 0) { | 
|  | 7322 | mEffects[idx_insert-1]->setOutBuffer(mInBuffer); | 
|  | 7323 | mEffects[idx_insert-1]->configure(); | 
|  | 7324 | } | 
|  | 7325 | effect->setOutBuffer(mOutBuffer); | 
|  | 7326 | } else { | 
|  | 7327 | effect->setOutBuffer(mInBuffer); | 
|  | 7328 | } | 
|  | 7329 | mEffects.insertAt(effect, idx_insert); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 7330 |  | 
| Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 7331 | ALOGV("addEffect_l() effect %p, added in chain %p at rank %d", effect.get(), this, idx_insert); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 7332 | } | 
|  | 7333 | effect->configure(); | 
|  | 7334 | return NO_ERROR; | 
|  | 7335 | } | 
|  | 7336 |  | 
| Eric Laurent | cab1124 | 2010-07-15 12:50:15 -0700 | [diff] [blame] | 7337 | // removeEffect_l() must be called with PlaybackThread::mLock held | 
|  | 7338 | size_t AudioFlinger::EffectChain::removeEffect_l(const sp<EffectModule>& effect) | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 7339 | { | 
|  | 7340 | Mutex::Autolock _l(mLock); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 7341 | int size = (int)mEffects.size(); | 
|  | 7342 | int i; | 
|  | 7343 | uint32_t type = effect->desc().flags & EFFECT_FLAG_TYPE_MASK; | 
|  | 7344 |  | 
|  | 7345 | for (i = 0; i < size; i++) { | 
|  | 7346 | if (effect == mEffects[i]) { | 
| Eric Laurent | ec437d8 | 2011-07-26 20:54:46 -0700 | [diff] [blame] | 7347 | // calling stop here will remove pre-processing effect from the audio HAL. | 
|  | 7348 | // This is safe as we hold the EffectChain mutex which guarantees that we are not in | 
|  | 7349 | // the middle of a read from audio HAL | 
| Eric Laurent | ec35a14 | 2011-10-05 17:42:25 -0700 | [diff] [blame] | 7350 | if (mEffects[i]->state() == EffectModule::ACTIVE || | 
|  | 7351 | mEffects[i]->state() == EffectModule::STOPPING) { | 
|  | 7352 | mEffects[i]->stop(); | 
|  | 7353 | } | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 7354 | if (type == EFFECT_FLAG_TYPE_AUXILIARY) { | 
|  | 7355 | delete[] effect->inBuffer(); | 
|  | 7356 | } else { | 
|  | 7357 | if (i == size - 1 && i != 0) { | 
|  | 7358 | mEffects[i - 1]->setOutBuffer(mOutBuffer); | 
|  | 7359 | mEffects[i - 1]->configure(); | 
|  | 7360 | } | 
|  | 7361 | } | 
|  | 7362 | mEffects.removeAt(i); | 
| Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 7363 | ALOGV("removeEffect_l() effect %p, removed from chain %p at rank %d", effect.get(), this, i); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 7364 | break; | 
|  | 7365 | } | 
|  | 7366 | } | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 7367 |  | 
|  | 7368 | return mEffects.size(); | 
|  | 7369 | } | 
|  | 7370 |  | 
| Eric Laurent | cab1124 | 2010-07-15 12:50:15 -0700 | [diff] [blame] | 7371 | // setDevice_l() must be called with PlaybackThread::mLock held | 
|  | 7372 | void AudioFlinger::EffectChain::setDevice_l(uint32_t device) | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 7373 | { | 
|  | 7374 | size_t size = mEffects.size(); | 
|  | 7375 | for (size_t i = 0; i < size; i++) { | 
|  | 7376 | mEffects[i]->setDevice(device); | 
|  | 7377 | } | 
|  | 7378 | } | 
|  | 7379 |  | 
| Eric Laurent | cab1124 | 2010-07-15 12:50:15 -0700 | [diff] [blame] | 7380 | // setMode_l() must be called with PlaybackThread::mLock held | 
| Glenn Kasten | f78aee7 | 2012-01-04 11:00:47 -0800 | [diff] [blame] | 7381 | void AudioFlinger::EffectChain::setMode_l(audio_mode_t mode) | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 7382 | { | 
|  | 7383 | size_t size = mEffects.size(); | 
|  | 7384 | for (size_t i = 0; i < size; i++) { | 
|  | 7385 | mEffects[i]->setMode(mode); | 
|  | 7386 | } | 
|  | 7387 | } | 
|  | 7388 |  | 
| Eric Laurent | cab1124 | 2010-07-15 12:50:15 -0700 | [diff] [blame] | 7389 | // setVolume_l() must be called with PlaybackThread::mLock held | 
|  | 7390 | bool AudioFlinger::EffectChain::setVolume_l(uint32_t *left, uint32_t *right) | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 7391 | { | 
|  | 7392 | uint32_t newLeft = *left; | 
|  | 7393 | uint32_t newRight = *right; | 
|  | 7394 | bool hasControl = false; | 
| Eric Laurent | cab1124 | 2010-07-15 12:50:15 -0700 | [diff] [blame] | 7395 | int ctrlIdx = -1; | 
|  | 7396 | size_t size = mEffects.size(); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 7397 |  | 
| Eric Laurent | cab1124 | 2010-07-15 12:50:15 -0700 | [diff] [blame] | 7398 | // first update volume controller | 
|  | 7399 | for (size_t i = size; i > 0; i--) { | 
| Eric Laurent | 8f45bd7 | 2010-08-31 13:50:07 -0700 | [diff] [blame] | 7400 | if (mEffects[i - 1]->isProcessEnabled() && | 
| Eric Laurent | cab1124 | 2010-07-15 12:50:15 -0700 | [diff] [blame] | 7401 | (mEffects[i - 1]->desc().flags & EFFECT_FLAG_VOLUME_MASK) == EFFECT_FLAG_VOLUME_CTRL) { | 
|  | 7402 | ctrlIdx = i - 1; | 
| Eric Laurent | f997cab | 2010-07-19 06:24:46 -0700 | [diff] [blame] | 7403 | hasControl = true; | 
| Eric Laurent | cab1124 | 2010-07-15 12:50:15 -0700 | [diff] [blame] | 7404 | break; | 
|  | 7405 | } | 
|  | 7406 | } | 
|  | 7407 |  | 
|  | 7408 | if (ctrlIdx == mVolumeCtrlIdx && *left == mLeftVolume && *right == mRightVolume) { | 
| Eric Laurent | f997cab | 2010-07-19 06:24:46 -0700 | [diff] [blame] | 7409 | if (hasControl) { | 
|  | 7410 | *left = mNewLeftVolume; | 
|  | 7411 | *right = mNewRightVolume; | 
|  | 7412 | } | 
|  | 7413 | return hasControl; | 
| Eric Laurent | cab1124 | 2010-07-15 12:50:15 -0700 | [diff] [blame] | 7414 | } | 
|  | 7415 |  | 
|  | 7416 | mVolumeCtrlIdx = ctrlIdx; | 
| Eric Laurent | f997cab | 2010-07-19 06:24:46 -0700 | [diff] [blame] | 7417 | mLeftVolume = newLeft; | 
|  | 7418 | mRightVolume = newRight; | 
| Eric Laurent | cab1124 | 2010-07-15 12:50:15 -0700 | [diff] [blame] | 7419 |  | 
|  | 7420 | // second get volume update from volume controller | 
|  | 7421 | if (ctrlIdx >= 0) { | 
|  | 7422 | mEffects[ctrlIdx]->setVolume(&newLeft, &newRight, true); | 
| Eric Laurent | f997cab | 2010-07-19 06:24:46 -0700 | [diff] [blame] | 7423 | mNewLeftVolume = newLeft; | 
|  | 7424 | mNewRightVolume = newRight; | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 7425 | } | 
|  | 7426 | // then indicate volume to all other effects in chain. | 
|  | 7427 | // Pass altered volume to effects before volume controller | 
|  | 7428 | // and requested volume to effects after controller | 
|  | 7429 | uint32_t lVol = newLeft; | 
|  | 7430 | uint32_t rVol = newRight; | 
| Eric Laurent | cab1124 | 2010-07-15 12:50:15 -0700 | [diff] [blame] | 7431 |  | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 7432 | for (size_t i = 0; i < size; i++) { | 
| Eric Laurent | cab1124 | 2010-07-15 12:50:15 -0700 | [diff] [blame] | 7433 | if ((int)i == ctrlIdx) continue; | 
|  | 7434 | // this also works for ctrlIdx == -1 when there is no volume controller | 
|  | 7435 | if ((int)i > ctrlIdx) { | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 7436 | lVol = *left; | 
|  | 7437 | rVol = *right; | 
|  | 7438 | } | 
|  | 7439 | mEffects[i]->setVolume(&lVol, &rVol, false); | 
|  | 7440 | } | 
|  | 7441 | *left = newLeft; | 
|  | 7442 | *right = newRight; | 
|  | 7443 |  | 
|  | 7444 | return hasControl; | 
|  | 7445 | } | 
|  | 7446 |  | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 7447 | status_t AudioFlinger::EffectChain::dump(int fd, const Vector<String16>& args) | 
|  | 7448 | { | 
|  | 7449 | const size_t SIZE = 256; | 
|  | 7450 | char buffer[SIZE]; | 
|  | 7451 | String8 result; | 
|  | 7452 |  | 
|  | 7453 | snprintf(buffer, SIZE, "Effects for session %d:\n", mSessionId); | 
|  | 7454 | result.append(buffer); | 
|  | 7455 |  | 
|  | 7456 | bool locked = tryLock(mLock); | 
|  | 7457 | // failed to lock - AudioFlinger is probably deadlocked | 
|  | 7458 | if (!locked) { | 
|  | 7459 | result.append("\tCould not lock mutex:\n"); | 
|  | 7460 | } | 
|  | 7461 |  | 
| Eric Laurent | cab1124 | 2010-07-15 12:50:15 -0700 | [diff] [blame] | 7462 | result.append("\tNum fx In buffer   Out buffer   Active tracks:\n"); | 
|  | 7463 | snprintf(buffer, SIZE, "\t%02d     0x%08x  0x%08x   %d\n", | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 7464 | mEffects.size(), | 
|  | 7465 | (uint32_t)mInBuffer, | 
|  | 7466 | (uint32_t)mOutBuffer, | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 7467 | mActiveTrackCnt); | 
|  | 7468 | result.append(buffer); | 
|  | 7469 | write(fd, result.string(), result.size()); | 
|  | 7470 |  | 
|  | 7471 | for (size_t i = 0; i < mEffects.size(); ++i) { | 
|  | 7472 | sp<EffectModule> effect = mEffects[i]; | 
|  | 7473 | if (effect != 0) { | 
|  | 7474 | effect->dump(fd, args); | 
|  | 7475 | } | 
|  | 7476 | } | 
|  | 7477 |  | 
|  | 7478 | if (locked) { | 
|  | 7479 | mLock.unlock(); | 
|  | 7480 | } | 
|  | 7481 |  | 
|  | 7482 | return NO_ERROR; | 
|  | 7483 | } | 
|  | 7484 |  | 
| Eric Laurent | 59255e4 | 2011-07-27 19:49:51 -0700 | [diff] [blame] | 7485 | // must be called with ThreadBase::mLock held | 
|  | 7486 | void AudioFlinger::EffectChain::setEffectSuspended_l( | 
|  | 7487 | const effect_uuid_t *type, bool suspend) | 
|  | 7488 | { | 
|  | 7489 | sp<SuspendedEffectDesc> desc; | 
|  | 7490 | // use effect type UUID timelow as key as there is no real risk of identical | 
|  | 7491 | // timeLow fields among effect type UUIDs. | 
|  | 7492 | int index = mSuspendedEffects.indexOfKey(type->timeLow); | 
|  | 7493 | if (suspend) { | 
|  | 7494 | if (index >= 0) { | 
|  | 7495 | desc = mSuspendedEffects.valueAt(index); | 
|  | 7496 | } else { | 
|  | 7497 | desc = new SuspendedEffectDesc(); | 
|  | 7498 | memcpy(&desc->mType, type, sizeof(effect_uuid_t)); | 
|  | 7499 | mSuspendedEffects.add(type->timeLow, desc); | 
| Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 7500 | ALOGV("setEffectSuspended_l() add entry for %08x", type->timeLow); | 
| Eric Laurent | 59255e4 | 2011-07-27 19:49:51 -0700 | [diff] [blame] | 7501 | } | 
|  | 7502 | if (desc->mRefCount++ == 0) { | 
|  | 7503 | sp<EffectModule> effect = getEffectIfEnabled(type); | 
|  | 7504 | if (effect != 0) { | 
|  | 7505 | desc->mEffect = effect; | 
|  | 7506 | effect->setSuspended(true); | 
|  | 7507 | effect->setEnabled(false); | 
|  | 7508 | } | 
|  | 7509 | } | 
|  | 7510 | } else { | 
|  | 7511 | if (index < 0) { | 
|  | 7512 | return; | 
|  | 7513 | } | 
|  | 7514 | desc = mSuspendedEffects.valueAt(index); | 
|  | 7515 | if (desc->mRefCount <= 0) { | 
| Steve Block | 5ff1dd5 | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 7516 | ALOGW("setEffectSuspended_l() restore refcount should not be 0 %d", desc->mRefCount); | 
| Eric Laurent | 59255e4 | 2011-07-27 19:49:51 -0700 | [diff] [blame] | 7517 | desc->mRefCount = 1; | 
|  | 7518 | } | 
|  | 7519 | if (--desc->mRefCount == 0) { | 
| Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 7520 | ALOGV("setEffectSuspended_l() remove entry for %08x", mSuspendedEffects.keyAt(index)); | 
| Eric Laurent | 59255e4 | 2011-07-27 19:49:51 -0700 | [diff] [blame] | 7521 | if (desc->mEffect != 0) { | 
|  | 7522 | sp<EffectModule> effect = desc->mEffect.promote(); | 
|  | 7523 | if (effect != 0) { | 
|  | 7524 | effect->setSuspended(false); | 
|  | 7525 | sp<EffectHandle> handle = effect->controlHandle(); | 
|  | 7526 | if (handle != 0) { | 
|  | 7527 | effect->setEnabled(handle->enabled()); | 
|  | 7528 | } | 
|  | 7529 | } | 
|  | 7530 | desc->mEffect.clear(); | 
|  | 7531 | } | 
|  | 7532 | mSuspendedEffects.removeItemsAt(index); | 
|  | 7533 | } | 
|  | 7534 | } | 
|  | 7535 | } | 
|  | 7536 |  | 
|  | 7537 | // must be called with ThreadBase::mLock held | 
|  | 7538 | void AudioFlinger::EffectChain::setEffectSuspendedAll_l(bool suspend) | 
|  | 7539 | { | 
|  | 7540 | sp<SuspendedEffectDesc> desc; | 
|  | 7541 |  | 
|  | 7542 | int index = mSuspendedEffects.indexOfKey((int)kKeyForSuspendAll); | 
|  | 7543 | if (suspend) { | 
|  | 7544 | if (index >= 0) { | 
|  | 7545 | desc = mSuspendedEffects.valueAt(index); | 
|  | 7546 | } else { | 
|  | 7547 | desc = new SuspendedEffectDesc(); | 
|  | 7548 | mSuspendedEffects.add((int)kKeyForSuspendAll, desc); | 
| Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 7549 | ALOGV("setEffectSuspendedAll_l() add entry for 0"); | 
| Eric Laurent | 59255e4 | 2011-07-27 19:49:51 -0700 | [diff] [blame] | 7550 | } | 
|  | 7551 | if (desc->mRefCount++ == 0) { | 
|  | 7552 | Vector< sp<EffectModule> > effects = getSuspendEligibleEffects(); | 
|  | 7553 | for (size_t i = 0; i < effects.size(); i++) { | 
|  | 7554 | setEffectSuspended_l(&effects[i]->desc().type, true); | 
|  | 7555 | } | 
|  | 7556 | } | 
|  | 7557 | } else { | 
|  | 7558 | if (index < 0) { | 
|  | 7559 | return; | 
|  | 7560 | } | 
|  | 7561 | desc = mSuspendedEffects.valueAt(index); | 
|  | 7562 | if (desc->mRefCount <= 0) { | 
| Steve Block | 5ff1dd5 | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 7563 | ALOGW("setEffectSuspendedAll_l() restore refcount should not be 0 %d", desc->mRefCount); | 
| Eric Laurent | 59255e4 | 2011-07-27 19:49:51 -0700 | [diff] [blame] | 7564 | desc->mRefCount = 1; | 
|  | 7565 | } | 
|  | 7566 | if (--desc->mRefCount == 0) { | 
|  | 7567 | Vector<const effect_uuid_t *> types; | 
|  | 7568 | for (size_t i = 0; i < mSuspendedEffects.size(); i++) { | 
|  | 7569 | if (mSuspendedEffects.keyAt(i) == (int)kKeyForSuspendAll) { | 
|  | 7570 | continue; | 
|  | 7571 | } | 
|  | 7572 | types.add(&mSuspendedEffects.valueAt(i)->mType); | 
|  | 7573 | } | 
|  | 7574 | for (size_t i = 0; i < types.size(); i++) { | 
|  | 7575 | setEffectSuspended_l(types[i], false); | 
|  | 7576 | } | 
| Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 7577 | ALOGV("setEffectSuspendedAll_l() remove entry for %08x", mSuspendedEffects.keyAt(index)); | 
| Eric Laurent | 59255e4 | 2011-07-27 19:49:51 -0700 | [diff] [blame] | 7578 | mSuspendedEffects.removeItem((int)kKeyForSuspendAll); | 
|  | 7579 | } | 
|  | 7580 | } | 
|  | 7581 | } | 
|  | 7582 |  | 
| Eric Laurent | 6bffdb8 | 2011-09-23 08:40:41 -0700 | [diff] [blame] | 7583 |  | 
|  | 7584 | // The volume effect is used for automated tests only | 
|  | 7585 | #ifndef OPENSL_ES_H_ | 
|  | 7586 | static const effect_uuid_t SL_IID_VOLUME_ = { 0x09e8ede0, 0xddde, 0x11db, 0xb4f6, | 
|  | 7587 | { 0x00, 0x02, 0xa5, 0xd5, 0xc5, 0x1b } }; | 
|  | 7588 | const effect_uuid_t * const SL_IID_VOLUME = &SL_IID_VOLUME_; | 
|  | 7589 | #endif //OPENSL_ES_H_ | 
|  | 7590 |  | 
| Eric Laurent | db7c079 | 2011-08-10 10:37:50 -0700 | [diff] [blame] | 7591 | bool AudioFlinger::EffectChain::isEffectEligibleForSuspend(const effect_descriptor_t& desc) | 
|  | 7592 | { | 
|  | 7593 | // auxiliary effects and visualizer are never suspended on output mix | 
|  | 7594 | if ((mSessionId == AUDIO_SESSION_OUTPUT_MIX) && | 
|  | 7595 | (((desc.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) || | 
| Eric Laurent | 6bffdb8 | 2011-09-23 08:40:41 -0700 | [diff] [blame] | 7596 | (memcmp(&desc.type, SL_IID_VISUALIZATION, sizeof(effect_uuid_t)) == 0) || | 
|  | 7597 | (memcmp(&desc.type, SL_IID_VOLUME, sizeof(effect_uuid_t)) == 0))) { | 
| Eric Laurent | db7c079 | 2011-08-10 10:37:50 -0700 | [diff] [blame] | 7598 | return false; | 
|  | 7599 | } | 
|  | 7600 | return true; | 
|  | 7601 | } | 
|  | 7602 |  | 
| Eric Laurent | 59255e4 | 2011-07-27 19:49:51 -0700 | [diff] [blame] | 7603 | Vector< sp<AudioFlinger::EffectModule> > AudioFlinger::EffectChain::getSuspendEligibleEffects() | 
|  | 7604 | { | 
|  | 7605 | Vector< sp<EffectModule> > effects; | 
|  | 7606 | for (size_t i = 0; i < mEffects.size(); i++) { | 
| Eric Laurent | db7c079 | 2011-08-10 10:37:50 -0700 | [diff] [blame] | 7607 | if (!isEffectEligibleForSuspend(mEffects[i]->desc())) { | 
| Eric Laurent | 59255e4 | 2011-07-27 19:49:51 -0700 | [diff] [blame] | 7608 | continue; | 
|  | 7609 | } | 
|  | 7610 | effects.add(mEffects[i]); | 
|  | 7611 | } | 
|  | 7612 | return effects; | 
|  | 7613 | } | 
|  | 7614 |  | 
|  | 7615 | sp<AudioFlinger::EffectModule> AudioFlinger::EffectChain::getEffectIfEnabled( | 
|  | 7616 | const effect_uuid_t *type) | 
|  | 7617 | { | 
|  | 7618 | sp<EffectModule> effect; | 
|  | 7619 | effect = getEffectFromType_l(type); | 
|  | 7620 | if (effect != 0 && !effect->isEnabled()) { | 
|  | 7621 | effect.clear(); | 
|  | 7622 | } | 
|  | 7623 | return effect; | 
|  | 7624 | } | 
|  | 7625 |  | 
|  | 7626 | void AudioFlinger::EffectChain::checkSuspendOnEffectEnabled(const sp<EffectModule>& effect, | 
|  | 7627 | bool enabled) | 
|  | 7628 | { | 
|  | 7629 | int index = mSuspendedEffects.indexOfKey(effect->desc().type.timeLow); | 
|  | 7630 | if (enabled) { | 
|  | 7631 | if (index < 0) { | 
|  | 7632 | // if the effect is not suspend check if all effects are suspended | 
|  | 7633 | index = mSuspendedEffects.indexOfKey((int)kKeyForSuspendAll); | 
|  | 7634 | if (index < 0) { | 
|  | 7635 | return; | 
|  | 7636 | } | 
| Eric Laurent | db7c079 | 2011-08-10 10:37:50 -0700 | [diff] [blame] | 7637 | if (!isEffectEligibleForSuspend(effect->desc())) { | 
|  | 7638 | return; | 
|  | 7639 | } | 
| Eric Laurent | 59255e4 | 2011-07-27 19:49:51 -0700 | [diff] [blame] | 7640 | setEffectSuspended_l(&effect->desc().type, enabled); | 
|  | 7641 | index = mSuspendedEffects.indexOfKey(effect->desc().type.timeLow); | 
| Eric Laurent | db7c079 | 2011-08-10 10:37:50 -0700 | [diff] [blame] | 7642 | if (index < 0) { | 
| Steve Block | 5ff1dd5 | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 7643 | ALOGW("checkSuspendOnEffectEnabled() Fx should be suspended here!"); | 
| Eric Laurent | db7c079 | 2011-08-10 10:37:50 -0700 | [diff] [blame] | 7644 | return; | 
|  | 7645 | } | 
| Eric Laurent | 59255e4 | 2011-07-27 19:49:51 -0700 | [diff] [blame] | 7646 | } | 
| Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 7647 | ALOGV("checkSuspendOnEffectEnabled() enable suspending fx %08x", | 
| Eric Laurent | 59255e4 | 2011-07-27 19:49:51 -0700 | [diff] [blame] | 7648 | effect->desc().type.timeLow); | 
|  | 7649 | sp<SuspendedEffectDesc> desc = mSuspendedEffects.valueAt(index); | 
|  | 7650 | // if effect is requested to suspended but was not yet enabled, supend it now. | 
|  | 7651 | if (desc->mEffect == 0) { | 
|  | 7652 | desc->mEffect = effect; | 
|  | 7653 | effect->setEnabled(false); | 
|  | 7654 | effect->setSuspended(true); | 
|  | 7655 | } | 
|  | 7656 | } else { | 
|  | 7657 | if (index < 0) { | 
|  | 7658 | return; | 
|  | 7659 | } | 
| Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 7660 | ALOGV("checkSuspendOnEffectEnabled() disable restoring fx %08x", | 
| Eric Laurent | 59255e4 | 2011-07-27 19:49:51 -0700 | [diff] [blame] | 7661 | effect->desc().type.timeLow); | 
|  | 7662 | sp<SuspendedEffectDesc> desc = mSuspendedEffects.valueAt(index); | 
|  | 7663 | desc->mEffect.clear(); | 
|  | 7664 | effect->setSuspended(false); | 
|  | 7665 | } | 
|  | 7666 | } | 
|  | 7667 |  | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 7668 | #undef LOG_TAG | 
|  | 7669 | #define LOG_TAG "AudioFlinger" | 
|  | 7670 |  | 
|  | 7671 | // ---------------------------------------------------------------------------- | 
|  | 7672 |  | 
|  | 7673 | status_t AudioFlinger::onTransact( | 
|  | 7674 | uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags) | 
|  | 7675 | { | 
|  | 7676 | return BnAudioFlinger::onTransact(code, data, reply, flags); | 
|  | 7677 | } | 
|  | 7678 |  | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 7679 | }; // namespace android |