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 | |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 988 | AudioFlinger::ThreadBase::ThreadBase(const sp<AudioFlinger>& audioFlinger, int id, uint32_t device) |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 989 | : Thread(false), |
| 990 | mAudioFlinger(audioFlinger), mSampleRate(0), mFrameCount(0), mChannelCount(0), |
Glenn Kasten | 58f3021 | 2012-01-12 12:27:51 -0800 | [diff] [blame] | 991 | mFrameSize(1), mFormat(AUDIO_FORMAT_INVALID), mStandby(false), mId(id), mExiting(false), |
Eric Laurent | feb0db6 | 2011-07-22 09:04:31 -0700 | [diff] [blame] | 992 | mDevice(device) |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 993 | { |
Eric Laurent | feb0db6 | 2011-07-22 09:04:31 -0700 | [diff] [blame] | 994 | mDeathRecipient = new PMDeathRecipient(this); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 995 | } |
| 996 | |
| 997 | AudioFlinger::ThreadBase::~ThreadBase() |
| 998 | { |
| 999 | mParamCond.broadcast(); |
Eric Laurent | feb0db6 | 2011-07-22 09:04:31 -0700 | [diff] [blame] | 1000 | // do not lock the mutex in destructor |
| 1001 | releaseWakeLock_l(); |
Eric Laurent | 9d18ec5 | 2011-09-27 12:07:15 -0700 | [diff] [blame] | 1002 | if (mPowerManager != 0) { |
| 1003 | sp<IBinder> binder = mPowerManager->asBinder(); |
| 1004 | binder->unlinkToDeath(mDeathRecipient); |
| 1005 | } |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1006 | } |
| 1007 | |
| 1008 | void AudioFlinger::ThreadBase::exit() |
| 1009 | { |
Glenn Kasten | 362c4e6 | 2011-12-14 10:28:06 -0800 | [diff] [blame] | 1010 | // keep a strong ref on ourself so that we won't get |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1011 | // destroyed in the middle of requestExitAndWait() |
| 1012 | sp <ThreadBase> strongMe = this; |
| 1013 | |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 1014 | ALOGV("ThreadBase::exit"); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1015 | { |
Glenn Kasten | a7d8d6f | 2012-01-05 15:41:56 -0800 | [diff] [blame] | 1016 | AutoMutex lock(mLock); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1017 | mExiting = true; |
| 1018 | requestExit(); |
| 1019 | mWaitWorkCV.signal(); |
| 1020 | } |
| 1021 | requestExitAndWait(); |
| 1022 | } |
| 1023 | |
| 1024 | uint32_t AudioFlinger::ThreadBase::sampleRate() const |
| 1025 | { |
| 1026 | return mSampleRate; |
| 1027 | } |
| 1028 | |
| 1029 | int AudioFlinger::ThreadBase::channelCount() const |
| 1030 | { |
| 1031 | return (int)mChannelCount; |
| 1032 | } |
| 1033 | |
Glenn Kasten | 58f3021 | 2012-01-12 12:27:51 -0800 | [diff] [blame] | 1034 | audio_format_t AudioFlinger::ThreadBase::format() const |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1035 | { |
| 1036 | return mFormat; |
| 1037 | } |
| 1038 | |
| 1039 | size_t AudioFlinger::ThreadBase::frameCount() const |
| 1040 | { |
| 1041 | return mFrameCount; |
| 1042 | } |
| 1043 | |
| 1044 | status_t AudioFlinger::ThreadBase::setParameters(const String8& keyValuePairs) |
| 1045 | { |
| 1046 | status_t status; |
| 1047 | |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 1048 | ALOGV("ThreadBase::setParameters() %s", keyValuePairs.string()); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1049 | Mutex::Autolock _l(mLock); |
| 1050 | |
| 1051 | mNewParameters.add(keyValuePairs); |
| 1052 | mWaitWorkCV.signal(); |
| 1053 | // wait condition with timeout in case the thread loop has exited |
| 1054 | // before the request could be processed |
Glenn Kasten | 7dede87 | 2011-12-13 11:04:14 -0800 | [diff] [blame] | 1055 | if (mParamCond.waitRelative(mLock, kSetParametersTimeoutNs) == NO_ERROR) { |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1056 | status = mParamStatus; |
| 1057 | mWaitWorkCV.signal(); |
| 1058 | } else { |
| 1059 | status = TIMED_OUT; |
| 1060 | } |
| 1061 | return status; |
| 1062 | } |
| 1063 | |
| 1064 | void AudioFlinger::ThreadBase::sendConfigEvent(int event, int param) |
| 1065 | { |
| 1066 | Mutex::Autolock _l(mLock); |
| 1067 | sendConfigEvent_l(event, param); |
| 1068 | } |
| 1069 | |
| 1070 | // sendConfigEvent_l() must be called with ThreadBase::mLock held |
| 1071 | void AudioFlinger::ThreadBase::sendConfigEvent_l(int event, int param) |
| 1072 | { |
Glenn Kasten | f3990f2 | 2011-12-13 11:50:00 -0800 | [diff] [blame] | 1073 | ConfigEvent configEvent; |
| 1074 | configEvent.mEvent = event; |
| 1075 | configEvent.mParam = param; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1076 | mConfigEvents.add(configEvent); |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 1077 | 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] | 1078 | mWaitWorkCV.signal(); |
| 1079 | } |
| 1080 | |
| 1081 | void AudioFlinger::ThreadBase::processConfigEvents() |
| 1082 | { |
| 1083 | mLock.lock(); |
| 1084 | while(!mConfigEvents.isEmpty()) { |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 1085 | ALOGV("processConfigEvents() remaining events %d", mConfigEvents.size()); |
Glenn Kasten | f3990f2 | 2011-12-13 11:50:00 -0800 | [diff] [blame] | 1086 | ConfigEvent configEvent = mConfigEvents[0]; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1087 | mConfigEvents.removeAt(0); |
| 1088 | // release mLock before locking AudioFlinger mLock: lock order is always |
| 1089 | // AudioFlinger then ThreadBase to avoid cross deadlock |
| 1090 | mLock.unlock(); |
| 1091 | mAudioFlinger->mLock.lock(); |
Glenn Kasten | f3990f2 | 2011-12-13 11:50:00 -0800 | [diff] [blame] | 1092 | audioConfigChanged_l(configEvent.mEvent, configEvent.mParam); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1093 | mAudioFlinger->mLock.unlock(); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1094 | mLock.lock(); |
| 1095 | } |
| 1096 | mLock.unlock(); |
| 1097 | } |
| 1098 | |
| 1099 | status_t AudioFlinger::ThreadBase::dumpBase(int fd, const Vector<String16>& args) |
| 1100 | { |
| 1101 | const size_t SIZE = 256; |
| 1102 | char buffer[SIZE]; |
| 1103 | String8 result; |
| 1104 | |
| 1105 | bool locked = tryLock(mLock); |
| 1106 | if (!locked) { |
| 1107 | snprintf(buffer, SIZE, "thread %p maybe dead locked\n", this); |
| 1108 | write(fd, buffer, strlen(buffer)); |
| 1109 | } |
| 1110 | |
| 1111 | snprintf(buffer, SIZE, "standby: %d\n", mStandby); |
| 1112 | result.append(buffer); |
| 1113 | snprintf(buffer, SIZE, "Sample rate: %d\n", mSampleRate); |
| 1114 | result.append(buffer); |
| 1115 | snprintf(buffer, SIZE, "Frame count: %d\n", mFrameCount); |
| 1116 | result.append(buffer); |
| 1117 | snprintf(buffer, SIZE, "Channel Count: %d\n", mChannelCount); |
| 1118 | result.append(buffer); |
Jean-Michel Trivi | 0d255b2 | 2011-05-24 15:53:33 -0700 | [diff] [blame] | 1119 | snprintf(buffer, SIZE, "Channel Mask: 0x%08x\n", mChannelMask); |
| 1120 | result.append(buffer); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1121 | snprintf(buffer, SIZE, "Format: %d\n", mFormat); |
| 1122 | result.append(buffer); |
Glenn Kasten | b998065 | 2012-01-11 09:48:27 -0800 | [diff] [blame] | 1123 | snprintf(buffer, SIZE, "Frame size: %u\n", mFrameSize); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1124 | result.append(buffer); |
| 1125 | |
| 1126 | snprintf(buffer, SIZE, "\nPending setParameters commands: \n"); |
| 1127 | result.append(buffer); |
| 1128 | result.append(" Index Command"); |
| 1129 | for (size_t i = 0; i < mNewParameters.size(); ++i) { |
| 1130 | snprintf(buffer, SIZE, "\n %02d ", i); |
| 1131 | result.append(buffer); |
| 1132 | result.append(mNewParameters[i]); |
| 1133 | } |
| 1134 | |
| 1135 | snprintf(buffer, SIZE, "\n\nPending config events: \n"); |
| 1136 | result.append(buffer); |
| 1137 | snprintf(buffer, SIZE, " Index event param\n"); |
| 1138 | result.append(buffer); |
| 1139 | for (size_t i = 0; i < mConfigEvents.size(); i++) { |
Glenn Kasten | f3990f2 | 2011-12-13 11:50:00 -0800 | [diff] [blame] | 1140 | 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] | 1141 | result.append(buffer); |
| 1142 | } |
| 1143 | result.append("\n"); |
| 1144 | |
| 1145 | write(fd, result.string(), result.size()); |
| 1146 | |
| 1147 | if (locked) { |
| 1148 | mLock.unlock(); |
| 1149 | } |
| 1150 | return NO_ERROR; |
| 1151 | } |
| 1152 | |
Eric Laurent | 1d2bff0 | 2011-07-24 17:49:51 -0700 | [diff] [blame] | 1153 | status_t AudioFlinger::ThreadBase::dumpEffectChains(int fd, const Vector<String16>& args) |
| 1154 | { |
| 1155 | const size_t SIZE = 256; |
| 1156 | char buffer[SIZE]; |
| 1157 | String8 result; |
| 1158 | |
| 1159 | snprintf(buffer, SIZE, "\n- %d Effect Chains:\n", mEffectChains.size()); |
| 1160 | write(fd, buffer, strlen(buffer)); |
| 1161 | |
| 1162 | for (size_t i = 0; i < mEffectChains.size(); ++i) { |
| 1163 | sp<EffectChain> chain = mEffectChains[i]; |
| 1164 | if (chain != 0) { |
| 1165 | chain->dump(fd, args); |
| 1166 | } |
| 1167 | } |
| 1168 | return NO_ERROR; |
| 1169 | } |
| 1170 | |
Eric Laurent | feb0db6 | 2011-07-22 09:04:31 -0700 | [diff] [blame] | 1171 | void AudioFlinger::ThreadBase::acquireWakeLock() |
| 1172 | { |
| 1173 | Mutex::Autolock _l(mLock); |
| 1174 | acquireWakeLock_l(); |
| 1175 | } |
| 1176 | |
| 1177 | void AudioFlinger::ThreadBase::acquireWakeLock_l() |
| 1178 | { |
| 1179 | if (mPowerManager == 0) { |
| 1180 | // use checkService() to avoid blocking if power service is not up yet |
| 1181 | sp<IBinder> binder = |
| 1182 | defaultServiceManager()->checkService(String16("power")); |
| 1183 | if (binder == 0) { |
Steve Block | 5ff1dd5 | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 1184 | ALOGW("Thread %s cannot connect to the power manager service", mName); |
Eric Laurent | feb0db6 | 2011-07-22 09:04:31 -0700 | [diff] [blame] | 1185 | } else { |
| 1186 | mPowerManager = interface_cast<IPowerManager>(binder); |
| 1187 | binder->linkToDeath(mDeathRecipient); |
| 1188 | } |
| 1189 | } |
| 1190 | if (mPowerManager != 0) { |
| 1191 | sp<IBinder> binder = new BBinder(); |
| 1192 | status_t status = mPowerManager->acquireWakeLock(POWERMANAGER_PARTIAL_WAKE_LOCK, |
| 1193 | binder, |
| 1194 | String16(mName)); |
| 1195 | if (status == NO_ERROR) { |
| 1196 | mWakeLockToken = binder; |
| 1197 | } |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 1198 | ALOGV("acquireWakeLock_l() %s status %d", mName, status); |
Eric Laurent | feb0db6 | 2011-07-22 09:04:31 -0700 | [diff] [blame] | 1199 | } |
| 1200 | } |
| 1201 | |
| 1202 | void AudioFlinger::ThreadBase::releaseWakeLock() |
| 1203 | { |
| 1204 | Mutex::Autolock _l(mLock); |
Eric Laurent | 6dbe883 | 2011-07-28 13:59:02 -0700 | [diff] [blame] | 1205 | releaseWakeLock_l(); |
Eric Laurent | feb0db6 | 2011-07-22 09:04:31 -0700 | [diff] [blame] | 1206 | } |
| 1207 | |
| 1208 | void AudioFlinger::ThreadBase::releaseWakeLock_l() |
| 1209 | { |
| 1210 | if (mWakeLockToken != 0) { |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 1211 | ALOGV("releaseWakeLock_l() %s", mName); |
Eric Laurent | feb0db6 | 2011-07-22 09:04:31 -0700 | [diff] [blame] | 1212 | if (mPowerManager != 0) { |
| 1213 | mPowerManager->releaseWakeLock(mWakeLockToken, 0); |
| 1214 | } |
| 1215 | mWakeLockToken.clear(); |
| 1216 | } |
| 1217 | } |
| 1218 | |
| 1219 | void AudioFlinger::ThreadBase::clearPowerManager() |
| 1220 | { |
| 1221 | Mutex::Autolock _l(mLock); |
| 1222 | releaseWakeLock_l(); |
| 1223 | mPowerManager.clear(); |
| 1224 | } |
| 1225 | |
| 1226 | void AudioFlinger::ThreadBase::PMDeathRecipient::binderDied(const wp<IBinder>& who) |
| 1227 | { |
| 1228 | sp<ThreadBase> thread = mThread.promote(); |
| 1229 | if (thread != 0) { |
| 1230 | thread->clearPowerManager(); |
| 1231 | } |
Steve Block | 5ff1dd5 | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 1232 | ALOGW("power manager service died !!!"); |
Eric Laurent | feb0db6 | 2011-07-22 09:04:31 -0700 | [diff] [blame] | 1233 | } |
Eric Laurent | 1d2bff0 | 2011-07-24 17:49:51 -0700 | [diff] [blame] | 1234 | |
Eric Laurent | 59255e4 | 2011-07-27 19:49:51 -0700 | [diff] [blame] | 1235 | void AudioFlinger::ThreadBase::setEffectSuspended( |
| 1236 | const effect_uuid_t *type, bool suspend, int sessionId) |
| 1237 | { |
| 1238 | Mutex::Autolock _l(mLock); |
| 1239 | setEffectSuspended_l(type, suspend, sessionId); |
| 1240 | } |
| 1241 | |
| 1242 | void AudioFlinger::ThreadBase::setEffectSuspended_l( |
| 1243 | const effect_uuid_t *type, bool suspend, int sessionId) |
| 1244 | { |
| 1245 | sp<EffectChain> chain; |
| 1246 | chain = getEffectChain_l(sessionId); |
| 1247 | if (chain != 0) { |
| 1248 | if (type != NULL) { |
| 1249 | chain->setEffectSuspended_l(type, suspend); |
| 1250 | } else { |
| 1251 | chain->setEffectSuspendedAll_l(suspend); |
| 1252 | } |
| 1253 | } |
| 1254 | |
| 1255 | updateSuspendedSessions_l(type, suspend, sessionId); |
| 1256 | } |
| 1257 | |
| 1258 | void AudioFlinger::ThreadBase::checkSuspendOnAddEffectChain_l(const sp<EffectChain>& chain) |
| 1259 | { |
| 1260 | int index = mSuspendedSessions.indexOfKey(chain->sessionId()); |
| 1261 | if (index < 0) { |
| 1262 | return; |
| 1263 | } |
| 1264 | |
| 1265 | KeyedVector <int, sp<SuspendedSessionDesc> > sessionEffects = |
| 1266 | mSuspendedSessions.editValueAt(index); |
| 1267 | |
| 1268 | for (size_t i = 0; i < sessionEffects.size(); i++) { |
| 1269 | sp <SuspendedSessionDesc> desc = sessionEffects.valueAt(i); |
| 1270 | for (int j = 0; j < desc->mRefCount; j++) { |
| 1271 | if (sessionEffects.keyAt(i) == EffectChain::kKeyForSuspendAll) { |
| 1272 | chain->setEffectSuspendedAll_l(true); |
| 1273 | } else { |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 1274 | ALOGV("checkSuspendOnAddEffectChain_l() suspending effects %08x", |
Eric Laurent | 59255e4 | 2011-07-27 19:49:51 -0700 | [diff] [blame] | 1275 | desc->mType.timeLow); |
| 1276 | chain->setEffectSuspended_l(&desc->mType, true); |
| 1277 | } |
| 1278 | } |
| 1279 | } |
| 1280 | } |
| 1281 | |
Eric Laurent | 59255e4 | 2011-07-27 19:49:51 -0700 | [diff] [blame] | 1282 | void AudioFlinger::ThreadBase::updateSuspendedSessions_l(const effect_uuid_t *type, |
| 1283 | bool suspend, |
| 1284 | int sessionId) |
| 1285 | { |
| 1286 | int index = mSuspendedSessions.indexOfKey(sessionId); |
| 1287 | |
| 1288 | KeyedVector <int, sp<SuspendedSessionDesc> > sessionEffects; |
| 1289 | |
| 1290 | if (suspend) { |
| 1291 | if (index >= 0) { |
| 1292 | sessionEffects = mSuspendedSessions.editValueAt(index); |
| 1293 | } else { |
| 1294 | mSuspendedSessions.add(sessionId, sessionEffects); |
| 1295 | } |
| 1296 | } else { |
| 1297 | if (index < 0) { |
| 1298 | return; |
| 1299 | } |
| 1300 | sessionEffects = mSuspendedSessions.editValueAt(index); |
| 1301 | } |
| 1302 | |
| 1303 | |
| 1304 | int key = EffectChain::kKeyForSuspendAll; |
| 1305 | if (type != NULL) { |
| 1306 | key = type->timeLow; |
| 1307 | } |
| 1308 | index = sessionEffects.indexOfKey(key); |
| 1309 | |
| 1310 | sp <SuspendedSessionDesc> desc; |
| 1311 | if (suspend) { |
| 1312 | if (index >= 0) { |
| 1313 | desc = sessionEffects.valueAt(index); |
| 1314 | } else { |
| 1315 | desc = new SuspendedSessionDesc(); |
| 1316 | if (type != NULL) { |
| 1317 | memcpy(&desc->mType, type, sizeof(effect_uuid_t)); |
| 1318 | } |
| 1319 | sessionEffects.add(key, desc); |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 1320 | ALOGV("updateSuspendedSessions_l() suspend adding effect %08x", key); |
Eric Laurent | 59255e4 | 2011-07-27 19:49:51 -0700 | [diff] [blame] | 1321 | } |
| 1322 | desc->mRefCount++; |
| 1323 | } else { |
| 1324 | if (index < 0) { |
| 1325 | return; |
| 1326 | } |
| 1327 | desc = sessionEffects.valueAt(index); |
| 1328 | if (--desc->mRefCount == 0) { |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 1329 | ALOGV("updateSuspendedSessions_l() restore removing effect %08x", key); |
Eric Laurent | 59255e4 | 2011-07-27 19:49:51 -0700 | [diff] [blame] | 1330 | sessionEffects.removeItemsAt(index); |
| 1331 | if (sessionEffects.isEmpty()) { |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 1332 | ALOGV("updateSuspendedSessions_l() restore removing session %d", |
Eric Laurent | 59255e4 | 2011-07-27 19:49:51 -0700 | [diff] [blame] | 1333 | sessionId); |
| 1334 | mSuspendedSessions.removeItem(sessionId); |
| 1335 | } |
| 1336 | } |
| 1337 | } |
| 1338 | if (!sessionEffects.isEmpty()) { |
| 1339 | mSuspendedSessions.replaceValueFor(sessionId, sessionEffects); |
| 1340 | } |
| 1341 | } |
| 1342 | |
| 1343 | void AudioFlinger::ThreadBase::checkSuspendOnEffectEnabled(const sp<EffectModule>& effect, |
| 1344 | bool enabled, |
| 1345 | int sessionId) |
| 1346 | { |
| 1347 | Mutex::Autolock _l(mLock); |
Eric Laurent | a85a74a | 2011-10-19 11:44:54 -0700 | [diff] [blame] | 1348 | checkSuspendOnEffectEnabled_l(effect, enabled, sessionId); |
| 1349 | } |
Eric Laurent | 59255e4 | 2011-07-27 19:49:51 -0700 | [diff] [blame] | 1350 | |
Eric Laurent | a85a74a | 2011-10-19 11:44:54 -0700 | [diff] [blame] | 1351 | void AudioFlinger::ThreadBase::checkSuspendOnEffectEnabled_l(const sp<EffectModule>& effect, |
| 1352 | bool enabled, |
| 1353 | int sessionId) |
| 1354 | { |
Eric Laurent | db7c079 | 2011-08-10 10:37:50 -0700 | [diff] [blame] | 1355 | if (mType != RECORD) { |
| 1356 | // suspend all effects in AUDIO_SESSION_OUTPUT_MIX when enabling any effect on |
| 1357 | // another session. This gives the priority to well behaved effect control panels |
| 1358 | // and applications not using global effects. |
| 1359 | if (sessionId != AUDIO_SESSION_OUTPUT_MIX) { |
| 1360 | setEffectSuspended_l(NULL, enabled, AUDIO_SESSION_OUTPUT_MIX); |
| 1361 | } |
| 1362 | } |
Eric Laurent | 59255e4 | 2011-07-27 19:49:51 -0700 | [diff] [blame] | 1363 | |
| 1364 | sp<EffectChain> chain = getEffectChain_l(sessionId); |
| 1365 | if (chain != 0) { |
| 1366 | chain->checkSuspendOnEffectEnabled(effect, enabled); |
| 1367 | } |
| 1368 | } |
| 1369 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1370 | // ---------------------------------------------------------------------------- |
| 1371 | |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 1372 | AudioFlinger::PlaybackThread::PlaybackThread(const sp<AudioFlinger>& audioFlinger, |
| 1373 | AudioStreamOut* output, |
| 1374 | int id, |
| 1375 | uint32_t device) |
| 1376 | : ThreadBase(audioFlinger, id, device), |
Glenn Kasten | e0feee3 | 2011-12-13 11:53:26 -0800 | [diff] [blame] | 1377 | mMixBuffer(NULL), mSuspended(0), mBytesWritten(0), mOutput(output), |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 1378 | mLastWriteTime(0), mNumWrites(0), mNumDelayedWrites(0), mInWrite(false) |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1379 | { |
Eric Laurent | feb0db6 | 2011-07-22 09:04:31 -0700 | [diff] [blame] | 1380 | snprintf(mName, kNameLength, "AudioOut_%d", id); |
| 1381 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1382 | readOutputParameters(); |
| 1383 | |
Glenn Kasten | 9806710 | 2011-12-13 11:47:54 -0800 | [diff] [blame] | 1384 | // Assumes constructor is called by AudioFlinger with it's mLock held, |
| 1385 | // but it would be safer to explicitly pass these as parameters |
| 1386 | mMasterVolume = mAudioFlinger->masterVolume_l(); |
| 1387 | mMasterMute = mAudioFlinger->masterMute_l(); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1388 | |
Glenn Kasten | 263709e | 2012-01-06 08:40:01 -0800 | [diff] [blame] | 1389 | // mStreamTypes[AUDIO_STREAM_CNT] is initialized by stream_type_t default constructor |
Glenn Kasten | fff6d71 | 2012-01-12 16:38:12 -0800 | [diff] [blame] | 1390 | // There is no AUDIO_STREAM_MIN, and ++ operator does not compile |
| 1391 | for (audio_stream_type_t stream = (audio_stream_type_t) 0; stream < AUDIO_STREAM_CNT; |
| 1392 | stream = (audio_stream_type_t) (stream + 1)) { |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1393 | mStreamTypes[stream].volume = mAudioFlinger->streamVolumeInternal(stream); |
| 1394 | mStreamTypes[stream].mute = mAudioFlinger->streamMute(stream); |
Glenn Kasten | 263709e | 2012-01-06 08:40:01 -0800 | [diff] [blame] | 1395 | // initialized by stream_type_t default constructor |
| 1396 | // mStreamTypes[stream].valid = true; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1397 | } |
| 1398 | } |
| 1399 | |
| 1400 | AudioFlinger::PlaybackThread::~PlaybackThread() |
| 1401 | { |
| 1402 | delete [] mMixBuffer; |
| 1403 | } |
| 1404 | |
| 1405 | status_t AudioFlinger::PlaybackThread::dump(int fd, const Vector<String16>& args) |
| 1406 | { |
| 1407 | dumpInternals(fd, args); |
| 1408 | dumpTracks(fd, args); |
| 1409 | dumpEffectChains(fd, args); |
| 1410 | return NO_ERROR; |
| 1411 | } |
| 1412 | |
| 1413 | status_t AudioFlinger::PlaybackThread::dumpTracks(int fd, const Vector<String16>& args) |
| 1414 | { |
| 1415 | const size_t SIZE = 256; |
| 1416 | char buffer[SIZE]; |
| 1417 | String8 result; |
| 1418 | |
| 1419 | snprintf(buffer, SIZE, "Output thread %p tracks\n", this); |
| 1420 | result.append(buffer); |
Jean-Michel Trivi | 0d255b2 | 2011-05-24 15:53:33 -0700 | [diff] [blame] | 1421 | 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] | 1422 | for (size_t i = 0; i < mTracks.size(); ++i) { |
| 1423 | sp<Track> track = mTracks[i]; |
| 1424 | if (track != 0) { |
| 1425 | track->dump(buffer, SIZE); |
| 1426 | result.append(buffer); |
| 1427 | } |
| 1428 | } |
| 1429 | |
| 1430 | snprintf(buffer, SIZE, "Output thread %p active tracks\n", this); |
| 1431 | result.append(buffer); |
Jean-Michel Trivi | 0d255b2 | 2011-05-24 15:53:33 -0700 | [diff] [blame] | 1432 | 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] | 1433 | for (size_t i = 0; i < mActiveTracks.size(); ++i) { |
| 1434 | wp<Track> wTrack = mActiveTracks[i]; |
| 1435 | if (wTrack != 0) { |
| 1436 | sp<Track> track = wTrack.promote(); |
| 1437 | if (track != 0) { |
| 1438 | track->dump(buffer, SIZE); |
| 1439 | result.append(buffer); |
| 1440 | } |
| 1441 | } |
| 1442 | } |
| 1443 | write(fd, result.string(), result.size()); |
| 1444 | return NO_ERROR; |
| 1445 | } |
| 1446 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1447 | status_t AudioFlinger::PlaybackThread::dumpInternals(int fd, const Vector<String16>& args) |
| 1448 | { |
| 1449 | const size_t SIZE = 256; |
| 1450 | char buffer[SIZE]; |
| 1451 | String8 result; |
| 1452 | |
| 1453 | snprintf(buffer, SIZE, "\nOutput thread %p internals\n", this); |
| 1454 | result.append(buffer); |
| 1455 | snprintf(buffer, SIZE, "last write occurred (msecs): %llu\n", ns2ms(systemTime() - mLastWriteTime)); |
| 1456 | result.append(buffer); |
| 1457 | snprintf(buffer, SIZE, "total writes: %d\n", mNumWrites); |
| 1458 | result.append(buffer); |
| 1459 | snprintf(buffer, SIZE, "delayed writes: %d\n", mNumDelayedWrites); |
| 1460 | result.append(buffer); |
| 1461 | snprintf(buffer, SIZE, "blocked in write: %d\n", mInWrite); |
| 1462 | result.append(buffer); |
| 1463 | snprintf(buffer, SIZE, "suspend count: %d\n", mSuspended); |
| 1464 | result.append(buffer); |
| 1465 | snprintf(buffer, SIZE, "mix buffer : %p\n", mMixBuffer); |
| 1466 | result.append(buffer); |
| 1467 | write(fd, result.string(), result.size()); |
| 1468 | |
| 1469 | dumpBase(fd, args); |
| 1470 | |
| 1471 | return NO_ERROR; |
| 1472 | } |
| 1473 | |
| 1474 | // Thread virtuals |
| 1475 | status_t AudioFlinger::PlaybackThread::readyToRun() |
| 1476 | { |
Eric Laurent | b8ba0a9 | 2011-08-07 16:32:26 -0700 | [diff] [blame] | 1477 | status_t status = initCheck(); |
| 1478 | if (status == NO_ERROR) { |
Steve Block | df64d15 | 2012-01-04 20:05:49 +0000 | [diff] [blame] | 1479 | ALOGI("AudioFlinger's thread %p ready to run", this); |
Eric Laurent | b8ba0a9 | 2011-08-07 16:32:26 -0700 | [diff] [blame] | 1480 | } else { |
Steve Block | 29357bc | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 1481 | ALOGE("No working audio driver found."); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1482 | } |
Eric Laurent | b8ba0a9 | 2011-08-07 16:32:26 -0700 | [diff] [blame] | 1483 | return status; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1484 | } |
| 1485 | |
| 1486 | void AudioFlinger::PlaybackThread::onFirstRef() |
| 1487 | { |
Eric Laurent | feb0db6 | 2011-07-22 09:04:31 -0700 | [diff] [blame] | 1488 | run(mName, ANDROID_PRIORITY_URGENT_AUDIO); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1489 | } |
| 1490 | |
| 1491 | // PlaybackThread::createTrack_l() must be called with AudioFlinger::mLock held |
| 1492 | sp<AudioFlinger::PlaybackThread::Track> AudioFlinger::PlaybackThread::createTrack_l( |
| 1493 | const sp<AudioFlinger::Client>& client, |
Glenn Kasten | fff6d71 | 2012-01-12 16:38:12 -0800 | [diff] [blame] | 1494 | audio_stream_type_t streamType, |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1495 | uint32_t sampleRate, |
Glenn Kasten | 58f3021 | 2012-01-12 12:27:51 -0800 | [diff] [blame] | 1496 | audio_format_t format, |
Jean-Michel Trivi | 0d255b2 | 2011-05-24 15:53:33 -0700 | [diff] [blame] | 1497 | uint32_t channelMask, |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1498 | int frameCount, |
| 1499 | const sp<IMemory>& sharedBuffer, |
| 1500 | int sessionId, |
| 1501 | status_t *status) |
| 1502 | { |
| 1503 | sp<Track> track; |
| 1504 | status_t lStatus; |
| 1505 | |
| 1506 | if (mType == DIRECT) { |
Jean-Michel Trivi | 0d255b2 | 2011-05-24 15:53:33 -0700 | [diff] [blame] | 1507 | if ((format & AUDIO_FORMAT_MAIN_MASK) == AUDIO_FORMAT_PCM) { |
| 1508 | if (sampleRate != mSampleRate || format != mFormat || channelMask != mChannelMask) { |
Steve Block | 29357bc | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 1509 | 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] | 1510 | "for output %p with format %d", |
| 1511 | sampleRate, format, channelMask, mOutput, mFormat); |
| 1512 | lStatus = BAD_VALUE; |
| 1513 | goto Exit; |
| 1514 | } |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1515 | } |
| 1516 | } else { |
| 1517 | // Resampler implementation limits input sampling rate to 2 x output sampling rate. |
| 1518 | if (sampleRate > mSampleRate*2) { |
Steve Block | 29357bc | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 1519 | ALOGE("Sample rate out of range: %d mSampleRate %d", sampleRate, mSampleRate); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1520 | lStatus = BAD_VALUE; |
| 1521 | goto Exit; |
| 1522 | } |
| 1523 | } |
| 1524 | |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 1525 | lStatus = initCheck(); |
| 1526 | if (lStatus != NO_ERROR) { |
Steve Block | 29357bc | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 1527 | ALOGE("Audio driver not initialized."); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1528 | goto Exit; |
| 1529 | } |
| 1530 | |
| 1531 | { // scope for mLock |
| 1532 | Mutex::Autolock _l(mLock); |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 1533 | |
| 1534 | // all tracks in same audio session must share the same routing strategy otherwise |
| 1535 | // conflicts will happen when tracks are moved from one output to another by audio policy |
| 1536 | // manager |
| 1537 | uint32_t strategy = |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 1538 | AudioSystem::getStrategyForStream((audio_stream_type_t)streamType); |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 1539 | for (size_t i = 0; i < mTracks.size(); ++i) { |
| 1540 | sp<Track> t = mTracks[i]; |
| 1541 | if (t != 0) { |
Glenn Kasten | d879601 | 2011-10-28 10:31:42 -0700 | [diff] [blame] | 1542 | uint32_t actual = AudioSystem::getStrategyForStream((audio_stream_type_t)t->type()); |
| 1543 | if (sessionId == t->sessionId() && strategy != actual) { |
Steve Block | 29357bc | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 1544 | ALOGE("createTrack_l() mismatched strategy; expected %u but found %u", |
Glenn Kasten | d879601 | 2011-10-28 10:31:42 -0700 | [diff] [blame] | 1545 | strategy, actual); |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 1546 | lStatus = BAD_VALUE; |
| 1547 | goto Exit; |
| 1548 | } |
| 1549 | } |
| 1550 | } |
| 1551 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1552 | track = new Track(this, client, streamType, sampleRate, format, |
Jean-Michel Trivi | 0d255b2 | 2011-05-24 15:53:33 -0700 | [diff] [blame] | 1553 | channelMask, frameCount, sharedBuffer, sessionId); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1554 | if (track->getCblk() == NULL || track->name() < 0) { |
| 1555 | lStatus = NO_MEMORY; |
| 1556 | goto Exit; |
| 1557 | } |
| 1558 | mTracks.add(track); |
| 1559 | |
| 1560 | sp<EffectChain> chain = getEffectChain_l(sessionId); |
| 1561 | if (chain != 0) { |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 1562 | ALOGV("createTrack_l() setting main buffer %p", chain->inBuffer()); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1563 | track->setMainBuffer(chain->inBuffer()); |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 1564 | chain->setStrategy(AudioSystem::getStrategyForStream((audio_stream_type_t)track->type())); |
Eric Laurent | b469b94 | 2011-05-09 12:09:06 -0700 | [diff] [blame] | 1565 | chain->incTrackCnt(); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1566 | } |
Eric Laurent | 9f6530f | 2011-08-30 10:18:54 -0700 | [diff] [blame] | 1567 | |
| 1568 | // invalidate track immediately if the stream type was moved to another thread since |
| 1569 | // createTrack() was called by the client process. |
| 1570 | if (!mStreamTypes[streamType].valid) { |
Steve Block | 5ff1dd5 | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 1571 | ALOGW("createTrack_l() on thread %p: invalidating track on stream %d", |
Eric Laurent | 9f6530f | 2011-08-30 10:18:54 -0700 | [diff] [blame] | 1572 | this, streamType); |
| 1573 | android_atomic_or(CBLK_INVALID_ON, &track->mCblk->flags); |
| 1574 | } |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1575 | } |
| 1576 | lStatus = NO_ERROR; |
| 1577 | |
| 1578 | Exit: |
| 1579 | if(status) { |
| 1580 | *status = lStatus; |
| 1581 | } |
| 1582 | return track; |
| 1583 | } |
| 1584 | |
| 1585 | uint32_t AudioFlinger::PlaybackThread::latency() const |
| 1586 | { |
Eric Laurent | b8ba0a9 | 2011-08-07 16:32:26 -0700 | [diff] [blame] | 1587 | Mutex::Autolock _l(mLock); |
| 1588 | if (initCheck() == NO_ERROR) { |
Dima Zavin | 799a70e | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 1589 | return mOutput->stream->get_latency(mOutput->stream); |
Eric Laurent | b8ba0a9 | 2011-08-07 16:32:26 -0700 | [diff] [blame] | 1590 | } else { |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1591 | return 0; |
| 1592 | } |
| 1593 | } |
| 1594 | |
| 1595 | status_t AudioFlinger::PlaybackThread::setMasterVolume(float value) |
| 1596 | { |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1597 | mMasterVolume = value; |
| 1598 | return NO_ERROR; |
| 1599 | } |
| 1600 | |
| 1601 | status_t AudioFlinger::PlaybackThread::setMasterMute(bool muted) |
| 1602 | { |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1603 | mMasterMute = muted; |
| 1604 | return NO_ERROR; |
| 1605 | } |
| 1606 | |
| 1607 | float AudioFlinger::PlaybackThread::masterVolume() const |
| 1608 | { |
| 1609 | return mMasterVolume; |
| 1610 | } |
| 1611 | |
| 1612 | bool AudioFlinger::PlaybackThread::masterMute() const |
| 1613 | { |
| 1614 | return mMasterMute; |
| 1615 | } |
| 1616 | |
Glenn Kasten | fff6d71 | 2012-01-12 16:38:12 -0800 | [diff] [blame] | 1617 | status_t AudioFlinger::PlaybackThread::setStreamVolume(audio_stream_type_t stream, float value) |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1618 | { |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1619 | mStreamTypes[stream].volume = value; |
| 1620 | return NO_ERROR; |
| 1621 | } |
| 1622 | |
Glenn Kasten | fff6d71 | 2012-01-12 16:38:12 -0800 | [diff] [blame] | 1623 | status_t AudioFlinger::PlaybackThread::setStreamMute(audio_stream_type_t stream, bool muted) |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1624 | { |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1625 | mStreamTypes[stream].mute = muted; |
| 1626 | return NO_ERROR; |
| 1627 | } |
| 1628 | |
Glenn Kasten | fff6d71 | 2012-01-12 16:38:12 -0800 | [diff] [blame] | 1629 | float AudioFlinger::PlaybackThread::streamVolume(audio_stream_type_t stream) const |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1630 | { |
| 1631 | return mStreamTypes[stream].volume; |
| 1632 | } |
| 1633 | |
Glenn Kasten | fff6d71 | 2012-01-12 16:38:12 -0800 | [diff] [blame] | 1634 | bool AudioFlinger::PlaybackThread::streamMute(audio_stream_type_t stream) const |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1635 | { |
| 1636 | return mStreamTypes[stream].mute; |
| 1637 | } |
| 1638 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1639 | // addTrack_l() must be called with ThreadBase::mLock held |
| 1640 | status_t AudioFlinger::PlaybackThread::addTrack_l(const sp<Track>& track) |
| 1641 | { |
| 1642 | status_t status = ALREADY_EXISTS; |
| 1643 | |
| 1644 | // set retry count for buffer fill |
| 1645 | track->mRetryCount = kMaxTrackStartupRetries; |
| 1646 | if (mActiveTracks.indexOf(track) < 0) { |
| 1647 | // the track is newly added, make sure it fills up all its |
| 1648 | // buffers before playing. This is to ensure the client will |
| 1649 | // effectively get the latency it requested. |
| 1650 | track->mFillingUpStatus = Track::FS_FILLING; |
| 1651 | track->mResetDone = false; |
| 1652 | mActiveTracks.add(track); |
| 1653 | if (track->mainBuffer() != mMixBuffer) { |
| 1654 | sp<EffectChain> chain = getEffectChain_l(track->sessionId()); |
| 1655 | if (chain != 0) { |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 1656 | 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] | 1657 | chain->incActiveTrackCnt(); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1658 | } |
| 1659 | } |
| 1660 | |
| 1661 | status = NO_ERROR; |
| 1662 | } |
| 1663 | |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 1664 | ALOGV("mWaitWorkCV.broadcast"); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1665 | mWaitWorkCV.broadcast(); |
| 1666 | |
| 1667 | return status; |
| 1668 | } |
| 1669 | |
| 1670 | // destroyTrack_l() must be called with ThreadBase::mLock held |
| 1671 | void AudioFlinger::PlaybackThread::destroyTrack_l(const sp<Track>& track) |
| 1672 | { |
| 1673 | track->mState = TrackBase::TERMINATED; |
| 1674 | if (mActiveTracks.indexOf(track) < 0) { |
Eric Laurent | b469b94 | 2011-05-09 12:09:06 -0700 | [diff] [blame] | 1675 | removeTrack_l(track); |
| 1676 | } |
| 1677 | } |
| 1678 | |
| 1679 | void AudioFlinger::PlaybackThread::removeTrack_l(const sp<Track>& track) |
| 1680 | { |
| 1681 | mTracks.remove(track); |
| 1682 | deleteTrackName_l(track->name()); |
| 1683 | sp<EffectChain> chain = getEffectChain_l(track->sessionId()); |
| 1684 | if (chain != 0) { |
| 1685 | chain->decTrackCnt(); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1686 | } |
| 1687 | } |
| 1688 | |
| 1689 | String8 AudioFlinger::PlaybackThread::getParameters(const String8& keys) |
| 1690 | { |
Eric Laurent | b8ba0a9 | 2011-08-07 16:32:26 -0700 | [diff] [blame] | 1691 | String8 out_s8 = String8(""); |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 1692 | char *s; |
| 1693 | |
Eric Laurent | b8ba0a9 | 2011-08-07 16:32:26 -0700 | [diff] [blame] | 1694 | Mutex::Autolock _l(mLock); |
| 1695 | if (initCheck() != NO_ERROR) { |
| 1696 | return out_s8; |
| 1697 | } |
| 1698 | |
Dima Zavin | 799a70e | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 1699 | s = mOutput->stream->common.get_parameters(&mOutput->stream->common, keys.string()); |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 1700 | out_s8 = String8(s); |
| 1701 | free(s); |
| 1702 | return out_s8; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1703 | } |
| 1704 | |
Eric Laurent | b8ba0a9 | 2011-08-07 16:32:26 -0700 | [diff] [blame] | 1705 | // audioConfigChanged_l() must be called with AudioFlinger::mLock held |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1706 | void AudioFlinger::PlaybackThread::audioConfigChanged_l(int event, int param) { |
| 1707 | AudioSystem::OutputDescriptor desc; |
| 1708 | void *param2 = 0; |
| 1709 | |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 1710 | 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] | 1711 | |
| 1712 | switch (event) { |
| 1713 | case AudioSystem::OUTPUT_OPENED: |
| 1714 | case AudioSystem::OUTPUT_CONFIG_CHANGED: |
Jean-Michel Trivi | 0d255b2 | 2011-05-24 15:53:33 -0700 | [diff] [blame] | 1715 | desc.channels = mChannelMask; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1716 | desc.samplingRate = mSampleRate; |
| 1717 | desc.format = mFormat; |
| 1718 | desc.frameCount = mFrameCount; |
| 1719 | desc.latency = latency(); |
| 1720 | param2 = &desc; |
| 1721 | break; |
| 1722 | |
| 1723 | case AudioSystem::STREAM_CONFIG_CHANGED: |
| 1724 | param2 = ¶m; |
| 1725 | case AudioSystem::OUTPUT_CLOSED: |
| 1726 | default: |
| 1727 | break; |
| 1728 | } |
| 1729 | mAudioFlinger->audioConfigChanged_l(event, mId, param2); |
| 1730 | } |
| 1731 | |
| 1732 | void AudioFlinger::PlaybackThread::readOutputParameters() |
| 1733 | { |
Dima Zavin | 799a70e | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 1734 | mSampleRate = mOutput->stream->common.get_sample_rate(&mOutput->stream->common); |
Jean-Michel Trivi | 0d255b2 | 2011-05-24 15:53:33 -0700 | [diff] [blame] | 1735 | mChannelMask = mOutput->stream->common.get_channels(&mOutput->stream->common); |
| 1736 | mChannelCount = (uint16_t)popcount(mChannelMask); |
Dima Zavin | 799a70e | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 1737 | mFormat = mOutput->stream->common.get_format(&mOutput->stream->common); |
Glenn Kasten | b998065 | 2012-01-11 09:48:27 -0800 | [diff] [blame] | 1738 | mFrameSize = audio_stream_frame_size(&mOutput->stream->common); |
Dima Zavin | 799a70e | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 1739 | mFrameCount = mOutput->stream->common.get_buffer_size(&mOutput->stream->common) / mFrameSize; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1740 | |
| 1741 | // FIXME - Current mixer implementation only supports stereo output: Always |
| 1742 | // Allocate a stereo buffer even if HW output is mono. |
| 1743 | if (mMixBuffer != NULL) delete[] mMixBuffer; |
| 1744 | mMixBuffer = new int16_t[mFrameCount * 2]; |
| 1745 | memset(mMixBuffer, 0, mFrameCount * 2 * sizeof(int16_t)); |
| 1746 | |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 1747 | // force reconfiguration of effect chains and engines to take new buffer size and audio |
| 1748 | // parameters into account |
| 1749 | // Note that mLock is not held when readOutputParameters() is called from the constructor |
| 1750 | // but in this case nothing is done below as no audio sessions have effect yet so it doesn't |
| 1751 | // matter. |
| 1752 | // create a copy of mEffectChains as calling moveEffectChain_l() can reorder some effect chains |
| 1753 | Vector< sp<EffectChain> > effectChains = mEffectChains; |
| 1754 | for (size_t i = 0; i < effectChains.size(); i ++) { |
Eric Laurent | 39e94f8 | 2010-07-28 01:32:47 -0700 | [diff] [blame] | 1755 | mAudioFlinger->moveEffectChain_l(effectChains[i]->sessionId(), this, this, false); |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 1756 | } |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1757 | } |
| 1758 | |
| 1759 | status_t AudioFlinger::PlaybackThread::getRenderPosition(uint32_t *halFrames, uint32_t *dspFrames) |
| 1760 | { |
| 1761 | if (halFrames == 0 || dspFrames == 0) { |
| 1762 | return BAD_VALUE; |
| 1763 | } |
Eric Laurent | b8ba0a9 | 2011-08-07 16:32:26 -0700 | [diff] [blame] | 1764 | Mutex::Autolock _l(mLock); |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 1765 | if (initCheck() != NO_ERROR) { |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1766 | return INVALID_OPERATION; |
| 1767 | } |
Dima Zavin | 799a70e | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 1768 | *halFrames = mBytesWritten / audio_stream_frame_size(&mOutput->stream->common); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1769 | |
Dima Zavin | 799a70e | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 1770 | return mOutput->stream->get_render_position(mOutput->stream, dspFrames); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1771 | } |
| 1772 | |
Eric Laurent | 39e94f8 | 2010-07-28 01:32:47 -0700 | [diff] [blame] | 1773 | uint32_t AudioFlinger::PlaybackThread::hasAudioSession(int sessionId) |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1774 | { |
| 1775 | Mutex::Autolock _l(mLock); |
Eric Laurent | 39e94f8 | 2010-07-28 01:32:47 -0700 | [diff] [blame] | 1776 | uint32_t result = 0; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1777 | if (getEffectChain_l(sessionId) != 0) { |
Eric Laurent | 39e94f8 | 2010-07-28 01:32:47 -0700 | [diff] [blame] | 1778 | result = EFFECT_SESSION; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1779 | } |
| 1780 | |
| 1781 | for (size_t i = 0; i < mTracks.size(); ++i) { |
| 1782 | sp<Track> track = mTracks[i]; |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 1783 | if (sessionId == track->sessionId() && |
| 1784 | !(track->mCblk->flags & CBLK_INVALID_MSK)) { |
Eric Laurent | 39e94f8 | 2010-07-28 01:32:47 -0700 | [diff] [blame] | 1785 | result |= TRACK_SESSION; |
| 1786 | break; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1787 | } |
| 1788 | } |
| 1789 | |
Eric Laurent | 39e94f8 | 2010-07-28 01:32:47 -0700 | [diff] [blame] | 1790 | return result; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1791 | } |
| 1792 | |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 1793 | uint32_t AudioFlinger::PlaybackThread::getStrategyForSession_l(int sessionId) |
| 1794 | { |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 1795 | // 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] | 1796 | // 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] | 1797 | if (sessionId == AUDIO_SESSION_OUTPUT_MIX) { |
| 1798 | return AudioSystem::getStrategyForStream(AUDIO_STREAM_MUSIC); |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 1799 | } |
| 1800 | for (size_t i = 0; i < mTracks.size(); i++) { |
| 1801 | sp<Track> track = mTracks[i]; |
| 1802 | if (sessionId == track->sessionId() && |
| 1803 | !(track->mCblk->flags & CBLK_INVALID_MSK)) { |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 1804 | return AudioSystem::getStrategyForStream((audio_stream_type_t) track->type()); |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 1805 | } |
| 1806 | } |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 1807 | return AudioSystem::getStrategyForStream(AUDIO_STREAM_MUSIC); |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 1808 | } |
| 1809 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1810 | |
Eric Laurent | b8ba0a9 | 2011-08-07 16:32:26 -0700 | [diff] [blame] | 1811 | AudioFlinger::AudioStreamOut* AudioFlinger::PlaybackThread::getOutput() |
| 1812 | { |
| 1813 | Mutex::Autolock _l(mLock); |
| 1814 | return mOutput; |
| 1815 | } |
| 1816 | |
| 1817 | AudioFlinger::AudioStreamOut* AudioFlinger::PlaybackThread::clearOutput() |
| 1818 | { |
| 1819 | Mutex::Autolock _l(mLock); |
| 1820 | AudioStreamOut *output = mOutput; |
| 1821 | mOutput = NULL; |
| 1822 | return output; |
| 1823 | } |
| 1824 | |
| 1825 | // this method must always be called either with ThreadBase mLock held or inside the thread loop |
| 1826 | audio_stream_t* AudioFlinger::PlaybackThread::stream() |
| 1827 | { |
| 1828 | if (mOutput == NULL) { |
| 1829 | return NULL; |
| 1830 | } |
| 1831 | return &mOutput->stream->common; |
| 1832 | } |
| 1833 | |
Eric Laurent | 162b40b | 2011-12-05 09:47:19 -0800 | [diff] [blame] | 1834 | uint32_t AudioFlinger::PlaybackThread::activeSleepTimeUs() |
| 1835 | { |
| 1836 | // A2DP output latency is not due only to buffering capacity. It also reflects encoding, |
| 1837 | // decoding and transfer time. So sleeping for half of the latency would likely cause |
| 1838 | // underruns |
| 1839 | if (audio_is_a2dp_device((audio_devices_t)mDevice)) { |
| 1840 | return (uint32_t)((uint32_t)((mFrameCount * 1000) / mSampleRate) * 1000); |
| 1841 | } else { |
| 1842 | return (uint32_t)(mOutput->stream->get_latency(mOutput->stream) * 1000) / 2; |
| 1843 | } |
| 1844 | } |
| 1845 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1846 | // ---------------------------------------------------------------------------- |
| 1847 | |
Dima Zavin | 799a70e | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 1848 | AudioFlinger::MixerThread::MixerThread(const sp<AudioFlinger>& audioFlinger, AudioStreamOut* output, int id, uint32_t device) |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1849 | : PlaybackThread(audioFlinger, output, id, device), |
Eric Laurent | aeeb7e2 | 2012-01-19 10:00:02 -0800 | [diff] [blame] | 1850 | mAudioMixer(NULL), mPrevMixerStatus(MIXER_IDLE) |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1851 | { |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 1852 | mType = ThreadBase::MIXER; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1853 | mAudioMixer = new AudioMixer(mFrameCount, mSampleRate); |
| 1854 | |
| 1855 | // FIXME - Current mixer implementation only supports stereo output |
| 1856 | if (mChannelCount == 1) { |
Steve Block | 29357bc | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 1857 | ALOGE("Invalid audio hardware channel count"); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1858 | } |
| 1859 | } |
| 1860 | |
| 1861 | AudioFlinger::MixerThread::~MixerThread() |
| 1862 | { |
| 1863 | delete mAudioMixer; |
| 1864 | } |
| 1865 | |
| 1866 | bool AudioFlinger::MixerThread::threadLoop() |
| 1867 | { |
| 1868 | Vector< sp<Track> > tracksToRemove; |
| 1869 | uint32_t mixerStatus = MIXER_IDLE; |
| 1870 | nsecs_t standbyTime = systemTime(); |
| 1871 | size_t mixBufferSize = mFrameCount * mFrameSize; |
| 1872 | // FIXME: Relaxed timing because of a certain device that can't meet latency |
| 1873 | // Should be reduced to 2x after the vendor fixes the driver issue |
Eric Laurent | 5c4e818 | 2011-10-18 15:42:27 -0700 | [diff] [blame] | 1874 | // increase threshold again due to low power audio mode. The way this warning threshold is |
| 1875 | // calculated and its usefulness should be reconsidered anyway. |
| 1876 | nsecs_t maxPeriod = seconds(mFrameCount) / mSampleRate * 15; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1877 | nsecs_t lastWarning = 0; |
| 1878 | bool longStandbyExit = false; |
| 1879 | uint32_t activeSleepTime = activeSleepTimeUs(); |
| 1880 | uint32_t idleSleepTime = idleSleepTimeUs(); |
| 1881 | uint32_t sleepTime = idleSleepTime; |
Eric Laurent | 7cafbb3 | 2011-11-22 18:50:29 -0800 | [diff] [blame] | 1882 | uint32_t sleepTimeShift = 0; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1883 | Vector< sp<EffectChain> > effectChains; |
Glenn Kasten | 4d8d0c3 | 2011-07-08 15:26:12 -0700 | [diff] [blame] | 1884 | #ifdef DEBUG_CPU_USAGE |
| 1885 | ThreadCpuUsage cpu; |
| 1886 | const CentralTendencyStatistics& stats = cpu.statistics(); |
| 1887 | #endif |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1888 | |
Eric Laurent | feb0db6 | 2011-07-22 09:04:31 -0700 | [diff] [blame] | 1889 | acquireWakeLock(); |
| 1890 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1891 | while (!exitPending()) |
| 1892 | { |
Glenn Kasten | 4d8d0c3 | 2011-07-08 15:26:12 -0700 | [diff] [blame] | 1893 | #ifdef DEBUG_CPU_USAGE |
| 1894 | cpu.sampleAndEnable(); |
| 1895 | unsigned n = stats.n(); |
| 1896 | // cpu.elapsed() is expensive, so don't call it every loop |
| 1897 | if ((n & 127) == 1) { |
| 1898 | long long elapsed = cpu.elapsed(); |
| 1899 | if (elapsed >= DEBUG_CPU_USAGE * 1000000000LL) { |
| 1900 | double perLoop = elapsed / (double) n; |
| 1901 | double perLoop100 = perLoop * 0.01; |
| 1902 | double mean = stats.mean(); |
| 1903 | double stddev = stats.stddev(); |
| 1904 | double minimum = stats.minimum(); |
| 1905 | double maximum = stats.maximum(); |
| 1906 | cpu.resetStatistics(); |
Steve Block | df64d15 | 2012-01-04 20:05:49 +0000 | [diff] [blame] | 1907 | 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] | 1908 | elapsed * .000000001, n, perLoop * .000001, |
| 1909 | mean * .001, |
| 1910 | stddev * .001, |
| 1911 | minimum * .001, |
| 1912 | maximum * .001, |
| 1913 | mean / perLoop100, |
| 1914 | stddev / perLoop100, |
| 1915 | minimum / perLoop100, |
| 1916 | maximum / perLoop100); |
| 1917 | } |
| 1918 | } |
| 1919 | #endif |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1920 | processConfigEvents(); |
| 1921 | |
| 1922 | mixerStatus = MIXER_IDLE; |
| 1923 | { // scope for mLock |
| 1924 | |
| 1925 | Mutex::Autolock _l(mLock); |
| 1926 | |
| 1927 | if (checkForNewParameters_l()) { |
| 1928 | mixBufferSize = mFrameCount * mFrameSize; |
| 1929 | // FIXME: Relaxed timing because of a certain device that can't meet latency |
| 1930 | // Should be reduced to 2x after the vendor fixes the driver issue |
Eric Laurent | 5c4e818 | 2011-10-18 15:42:27 -0700 | [diff] [blame] | 1931 | // increase threshold again due to low power audio mode. The way this warning |
| 1932 | // threshold is calculated and its usefulness should be reconsidered anyway. |
| 1933 | maxPeriod = seconds(mFrameCount) / mSampleRate * 15; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1934 | activeSleepTime = activeSleepTimeUs(); |
| 1935 | idleSleepTime = idleSleepTimeUs(); |
| 1936 | } |
| 1937 | |
| 1938 | const SortedVector< wp<Track> >& activeTracks = mActiveTracks; |
| 1939 | |
| 1940 | // put audio hardware into standby after short delay |
Glenn Kasten | f6b1678 | 2011-12-15 09:51:17 -0800 | [diff] [blame] | 1941 | if (CC_UNLIKELY((!activeTracks.size() && systemTime() > standbyTime) || |
| 1942 | mSuspended)) { |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1943 | if (!mStandby) { |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 1944 | ALOGV("Audio hardware entering standby, mixer %p, mSuspended %d\n", this, mSuspended); |
Dima Zavin | 799a70e | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 1945 | mOutput->stream->common.standby(&mOutput->stream->common); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1946 | mStandby = true; |
| 1947 | mBytesWritten = 0; |
| 1948 | } |
| 1949 | |
| 1950 | if (!activeTracks.size() && mConfigEvents.isEmpty()) { |
| 1951 | // we're about to wait, flush the binder command buffer |
| 1952 | IPCThreadState::self()->flushCommands(); |
| 1953 | |
| 1954 | if (exitPending()) break; |
| 1955 | |
Eric Laurent | feb0db6 | 2011-07-22 09:04:31 -0700 | [diff] [blame] | 1956 | releaseWakeLock_l(); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1957 | // wait until we have something to do... |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 1958 | ALOGV("MixerThread %p TID %d going to sleep\n", this, gettid()); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1959 | mWaitWorkCV.wait(mLock); |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 1960 | ALOGV("MixerThread %p TID %d waking up\n", this, gettid()); |
Eric Laurent | feb0db6 | 2011-07-22 09:04:31 -0700 | [diff] [blame] | 1961 | acquireWakeLock_l(); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1962 | |
Eric Laurent | 2774144 | 2012-01-17 19:20:12 -0800 | [diff] [blame] | 1963 | mPrevMixerStatus = MIXER_IDLE; |
Glenn Kasten | f1d4592 | 2012-01-13 15:54:24 -0800 | [diff] [blame] | 1964 | if (!mMasterMute) { |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1965 | char value[PROPERTY_VALUE_MAX]; |
| 1966 | property_get("ro.audio.silent", value, "0"); |
| 1967 | if (atoi(value)) { |
Steve Block | b8a8052 | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 1968 | ALOGD("Silence is golden"); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1969 | setMasterMute(true); |
| 1970 | } |
| 1971 | } |
| 1972 | |
| 1973 | standbyTime = systemTime() + kStandbyTimeInNsecs; |
| 1974 | sleepTime = idleSleepTime; |
Eric Laurent | 7cafbb3 | 2011-11-22 18:50:29 -0800 | [diff] [blame] | 1975 | sleepTimeShift = 0; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1976 | continue; |
| 1977 | } |
| 1978 | } |
| 1979 | |
| 1980 | mixerStatus = prepareTracks_l(activeTracks, &tracksToRemove); |
| 1981 | |
| 1982 | // prevent any changes in effect chain list and in each effect chain |
| 1983 | // during mixing and effect process as the audio buffers could be deleted |
| 1984 | // or modified if an effect is created or deleted |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 1985 | lockEffectChains_l(effectChains); |
Glenn Kasten | c5ac4cb | 2011-12-12 09:05:55 -0800 | [diff] [blame] | 1986 | } |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1987 | |
Glenn Kasten | f6b1678 | 2011-12-15 09:51:17 -0800 | [diff] [blame] | 1988 | if (CC_LIKELY(mixerStatus == MIXER_TRACKS_READY)) { |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1989 | // mix buffers... |
| 1990 | mAudioMixer->process(); |
| 1991 | sleepTime = 0; |
Eric Laurent | 7cafbb3 | 2011-11-22 18:50:29 -0800 | [diff] [blame] | 1992 | // increase sleep time progressively when application underrun condition clears |
| 1993 | if (sleepTimeShift > 0) { |
| 1994 | sleepTimeShift--; |
| 1995 | } |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1996 | standbyTime = systemTime() + kStandbyTimeInNsecs; |
| 1997 | //TODO: delay standby when effects have a tail |
| 1998 | } else { |
| 1999 | // If no tracks are ready, sleep once for the duration of an output |
| 2000 | // buffer size, then write 0s to the output |
| 2001 | if (sleepTime == 0) { |
| 2002 | if (mixerStatus == MIXER_TRACKS_ENABLED) { |
Eric Laurent | 7cafbb3 | 2011-11-22 18:50:29 -0800 | [diff] [blame] | 2003 | sleepTime = activeSleepTime >> sleepTimeShift; |
| 2004 | if (sleepTime < kMinThreadSleepTimeUs) { |
| 2005 | sleepTime = kMinThreadSleepTimeUs; |
| 2006 | } |
| 2007 | // reduce sleep time in case of consecutive application underruns to avoid |
| 2008 | // starving the audio HAL. As activeSleepTimeUs() is larger than a buffer |
| 2009 | // duration we would end up writing less data than needed by the audio HAL if |
| 2010 | // the condition persists. |
| 2011 | if (sleepTimeShift < kMaxThreadSleepTimeShift) { |
| 2012 | sleepTimeShift++; |
| 2013 | } |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2014 | } else { |
| 2015 | sleepTime = idleSleepTime; |
| 2016 | } |
| 2017 | } else if (mBytesWritten != 0 || |
| 2018 | (mixerStatus == MIXER_TRACKS_ENABLED && longStandbyExit)) { |
| 2019 | memset (mMixBuffer, 0, mixBufferSize); |
| 2020 | sleepTime = 0; |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 2021 | ALOGV_IF((mBytesWritten == 0 && (mixerStatus == MIXER_TRACKS_ENABLED && longStandbyExit)), "anticipated start"); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2022 | } |
| 2023 | // TODO add standby time extension fct of effect tail |
| 2024 | } |
| 2025 | |
| 2026 | if (mSuspended) { |
Eric Laurent | 25cbe0e | 2010-08-18 18:13:17 -0700 | [diff] [blame] | 2027 | sleepTime = suspendSleepTimeUs(); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2028 | } |
| 2029 | // sleepTime == 0 means we must write to audio hardware |
| 2030 | if (sleepTime == 0) { |
Glenn Kasten | c5ac4cb | 2011-12-12 09:05:55 -0800 | [diff] [blame] | 2031 | for (size_t i = 0; i < effectChains.size(); i ++) { |
| 2032 | effectChains[i]->process_l(); |
| 2033 | } |
| 2034 | // enable changes in effect chain |
| 2035 | unlockEffectChains(effectChains); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2036 | mLastWriteTime = systemTime(); |
| 2037 | mInWrite = true; |
| 2038 | mBytesWritten += mixBufferSize; |
| 2039 | |
Dima Zavin | 799a70e | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 2040 | int bytesWritten = (int)mOutput->stream->write(mOutput->stream, mMixBuffer, mixBufferSize); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2041 | if (bytesWritten < 0) mBytesWritten -= mixBufferSize; |
| 2042 | mNumWrites++; |
| 2043 | mInWrite = false; |
| 2044 | nsecs_t now = systemTime(); |
| 2045 | nsecs_t delta = now - mLastWriteTime; |
Eric Laurent | 5c4e818 | 2011-10-18 15:42:27 -0700 | [diff] [blame] | 2046 | if (!mStandby && delta > maxPeriod) { |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2047 | mNumDelayedWrites++; |
Glenn Kasten | 7dede87 | 2011-12-13 11:04:14 -0800 | [diff] [blame] | 2048 | if ((now - lastWarning) > kWarningThrottleNs) { |
Steve Block | 5ff1dd5 | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 2049 | ALOGW("write blocked for %llu msecs, %d delayed writes, thread %p", |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2050 | ns2ms(delta), mNumDelayedWrites, this); |
| 2051 | lastWarning = now; |
| 2052 | } |
| 2053 | if (mStandby) { |
| 2054 | longStandbyExit = true; |
| 2055 | } |
| 2056 | } |
| 2057 | mStandby = false; |
| 2058 | } else { |
| 2059 | // enable changes in effect chain |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 2060 | unlockEffectChains(effectChains); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2061 | usleep(sleepTime); |
| 2062 | } |
| 2063 | |
| 2064 | // finally let go of all our tracks, without the lock held |
| 2065 | // since we can't guarantee the destructors won't acquire that |
| 2066 | // same lock. |
| 2067 | tracksToRemove.clear(); |
| 2068 | |
| 2069 | // Effect chains will be actually deleted here if they were removed from |
| 2070 | // mEffectChains list during mixing or effects processing |
| 2071 | effectChains.clear(); |
| 2072 | } |
| 2073 | |
| 2074 | if (!mStandby) { |
Dima Zavin | 799a70e | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 2075 | mOutput->stream->common.standby(&mOutput->stream->common); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2076 | } |
| 2077 | |
Eric Laurent | feb0db6 | 2011-07-22 09:04:31 -0700 | [diff] [blame] | 2078 | releaseWakeLock(); |
| 2079 | |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 2080 | ALOGV("MixerThread %p exiting", this); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2081 | return false; |
| 2082 | } |
| 2083 | |
| 2084 | // prepareTracks_l() must be called with ThreadBase::mLock held |
| 2085 | uint32_t AudioFlinger::MixerThread::prepareTracks_l(const SortedVector< wp<Track> >& activeTracks, Vector< sp<Track> > *tracksToRemove) |
| 2086 | { |
| 2087 | |
| 2088 | uint32_t mixerStatus = MIXER_IDLE; |
| 2089 | // find out which tracks need to be processed |
| 2090 | size_t count = activeTracks.size(); |
| 2091 | size_t mixedTracks = 0; |
| 2092 | size_t tracksWithEffect = 0; |
| 2093 | |
| 2094 | float masterVolume = mMasterVolume; |
| 2095 | bool masterMute = mMasterMute; |
| 2096 | |
Eric Laurent | 571d49c | 2010-08-11 05:20:11 -0700 | [diff] [blame] | 2097 | if (masterMute) { |
| 2098 | masterVolume = 0; |
| 2099 | } |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2100 | // 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] | 2101 | sp<EffectChain> chain = getEffectChain_l(AUDIO_SESSION_OUTPUT_MIX); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2102 | if (chain != 0) { |
Eric Laurent | 571d49c | 2010-08-11 05:20:11 -0700 | [diff] [blame] | 2103 | uint32_t v = (uint32_t)(masterVolume * (1 << 24)); |
Eric Laurent | cab1124 | 2010-07-15 12:50:15 -0700 | [diff] [blame] | 2104 | chain->setVolume_l(&v, &v); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2105 | masterVolume = (float)((v + (1 << 23)) >> 24); |
| 2106 | chain.clear(); |
| 2107 | } |
| 2108 | |
| 2109 | for (size_t i=0 ; i<count ; i++) { |
| 2110 | sp<Track> t = activeTracks[i].promote(); |
| 2111 | if (t == 0) continue; |
| 2112 | |
Glenn Kasten | 9c56d4a | 2011-12-19 15:06:39 -0800 | [diff] [blame] | 2113 | // this const just means the local variable doesn't change |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2114 | Track* const track = t.get(); |
| 2115 | audio_track_cblk_t* cblk = track->cblk(); |
| 2116 | |
| 2117 | // The first time a track is added we wait |
| 2118 | // for all its buffers to be filled before processing it |
Glenn Kasten | 9c56d4a | 2011-12-19 15:06:39 -0800 | [diff] [blame] | 2119 | int name = track->name(); |
Eric Laurent | a47b69c | 2011-11-08 18:10:16 -0800 | [diff] [blame] | 2120 | // make sure that we have enough frames to mix one full buffer. |
| 2121 | // enforce this condition only once to enable draining the buffer in case the client |
| 2122 | // app does not call stop() and relies on underrun to stop: |
Eric Laurent | 2774144 | 2012-01-17 19:20:12 -0800 | [diff] [blame] | 2123 | // 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] | 2124 | // during last round |
Eric Laurent | 3dbe320 | 2011-11-03 12:16:05 -0700 | [diff] [blame] | 2125 | uint32_t minFrames = 1; |
Eric Laurent | a47b69c | 2011-11-08 18:10:16 -0800 | [diff] [blame] | 2126 | if (!track->isStopped() && !track->isPausing() && |
Eric Laurent | 2774144 | 2012-01-17 19:20:12 -0800 | [diff] [blame] | 2127 | (mPrevMixerStatus == MIXER_TRACKS_READY)) { |
Eric Laurent | 3dbe320 | 2011-11-03 12:16:05 -0700 | [diff] [blame] | 2128 | if (t->sampleRate() == (int)mSampleRate) { |
| 2129 | minFrames = mFrameCount; |
| 2130 | } else { |
Eric Laurent | 071ccd5 | 2011-12-22 16:08:41 -0800 | [diff] [blame] | 2131 | // +1 for rounding and +1 for additional sample needed for interpolation |
| 2132 | minFrames = (mFrameCount * t->sampleRate()) / mSampleRate + 1 + 1; |
| 2133 | // add frames already consumed but not yet released by the resampler |
| 2134 | // because cblk->framesReady() will include these frames |
| 2135 | minFrames += mAudioMixer->getUnreleasedFrames(track->name()); |
| 2136 | // the minimum track buffer size is normally twice the number of frames necessary |
| 2137 | // to fill one buffer and the resampler should not leave more than one buffer worth |
| 2138 | // of unreleased frames after each pass, but just in case... |
Steve Block | c1dc1cb | 2012-01-09 18:35:44 +0000 | [diff] [blame] | 2139 | ALOG_ASSERT(minFrames <= cblk->frameCount); |
Eric Laurent | 3dbe320 | 2011-11-03 12:16:05 -0700 | [diff] [blame] | 2140 | } |
| 2141 | } |
| 2142 | if ((cblk->framesReady() >= minFrames) && track->isReady() && |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2143 | !track->isPaused() && !track->isTerminated()) |
| 2144 | { |
Glenn Kasten | 9c56d4a | 2011-12-19 15:06:39 -0800 | [diff] [blame] | 2145 | //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] | 2146 | |
| 2147 | mixedTracks++; |
| 2148 | |
| 2149 | // track->mainBuffer() != mMixBuffer means there is an effect chain |
| 2150 | // connected to the track |
| 2151 | chain.clear(); |
| 2152 | if (track->mainBuffer() != mMixBuffer) { |
| 2153 | chain = getEffectChain_l(track->sessionId()); |
| 2154 | // Delegate volume control to effect in track effect chain if needed |
| 2155 | if (chain != 0) { |
| 2156 | tracksWithEffect++; |
| 2157 | } else { |
Steve Block | 5ff1dd5 | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 2158 | 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] | 2159 | name, track->sessionId()); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2160 | } |
| 2161 | } |
| 2162 | |
| 2163 | |
| 2164 | int param = AudioMixer::VOLUME; |
| 2165 | if (track->mFillingUpStatus == Track::FS_FILLED) { |
| 2166 | // no ramp for the first volume setting |
| 2167 | track->mFillingUpStatus = Track::FS_ACTIVE; |
| 2168 | if (track->mState == TrackBase::RESUMING) { |
| 2169 | track->mState = TrackBase::ACTIVE; |
| 2170 | param = AudioMixer::RAMP_VOLUME; |
| 2171 | } |
Glenn Kasten | 9c56d4a | 2011-12-19 15:06:39 -0800 | [diff] [blame] | 2172 | mAudioMixer->setParameter(name, AudioMixer::RESAMPLE, AudioMixer::RESET, NULL); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2173 | } else if (cblk->server != 0) { |
| 2174 | // If the track is stopped before the first frame was mixed, |
| 2175 | // do not apply ramp |
| 2176 | param = AudioMixer::RAMP_VOLUME; |
| 2177 | } |
| 2178 | |
| 2179 | // compute volume for this track |
Eric Laurent | e0aed6d | 2010-09-10 17:44:44 -0700 | [diff] [blame] | 2180 | uint32_t vl, vr, va; |
Eric Laurent | 8569f0d | 2010-07-29 23:43:43 -0700 | [diff] [blame] | 2181 | if (track->isMuted() || track->isPausing() || |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2182 | mStreamTypes[track->type()].mute) { |
Eric Laurent | e0aed6d | 2010-09-10 17:44:44 -0700 | [diff] [blame] | 2183 | vl = vr = va = 0; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2184 | if (track->isPausing()) { |
| 2185 | track->setPaused(); |
| 2186 | } |
| 2187 | } else { |
Eric Laurent | e0aed6d | 2010-09-10 17:44:44 -0700 | [diff] [blame] | 2188 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2189 | // read original volumes with volume control |
| 2190 | float typeVolume = mStreamTypes[track->type()].volume; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2191 | float v = masterVolume * typeVolume; |
Glenn Kasten | b1cf75c | 2012-01-17 12:20:54 -0800 | [diff] [blame] | 2192 | uint32_t vlr = cblk->volumeLR; |
| 2193 | vl = vlr & 0xFFFF; |
| 2194 | vr = vlr >> 16; |
| 2195 | // track volumes come from shared memory, so can't be trusted and must be clamped |
| 2196 | if (vl > MAX_GAIN_INT) { |
| 2197 | ALOGV("Track left volume out of range: %04X", vl); |
| 2198 | vl = MAX_GAIN_INT; |
| 2199 | } |
| 2200 | if (vr > MAX_GAIN_INT) { |
| 2201 | ALOGV("Track right volume out of range: %04X", vr); |
| 2202 | vr = MAX_GAIN_INT; |
| 2203 | } |
| 2204 | // now apply the master volume and stream type volume |
| 2205 | vl = (uint32_t)(v * vl) << 12; |
| 2206 | vr = (uint32_t)(v * vr) << 12; |
| 2207 | // assuming master volume and stream type volume each go up to 1.0, |
| 2208 | // vl and vr are now in 8.24 format |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2209 | |
Glenn Kasten | 05632a5 | 2012-01-03 14:22:33 -0800 | [diff] [blame] | 2210 | uint16_t sendLevel = cblk->getSendLevel_U4_12(); |
| 2211 | // send level comes from shared memory and so may be corrupt |
Glenn Kasten | b1cf75c | 2012-01-17 12:20:54 -0800 | [diff] [blame] | 2212 | if (sendLevel >= MAX_GAIN_INT) { |
Glenn Kasten | 05632a5 | 2012-01-03 14:22:33 -0800 | [diff] [blame] | 2213 | ALOGV("Track send level out of range: %04X", sendLevel); |
Glenn Kasten | b1cf75c | 2012-01-17 12:20:54 -0800 | [diff] [blame] | 2214 | sendLevel = MAX_GAIN_INT; |
Glenn Kasten | 05632a5 | 2012-01-03 14:22:33 -0800 | [diff] [blame] | 2215 | } |
| 2216 | va = (uint32_t)(v * sendLevel); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2217 | } |
Eric Laurent | e0aed6d | 2010-09-10 17:44:44 -0700 | [diff] [blame] | 2218 | // Delegate volume control to effect in track effect chain if needed |
| 2219 | if (chain != 0 && chain->setVolume_l(&vl, &vr)) { |
| 2220 | // Do not ramp volume if volume is controlled by effect |
| 2221 | param = AudioMixer::VOLUME; |
| 2222 | track->mHasVolumeController = true; |
| 2223 | } else { |
| 2224 | // force no volume ramp when volume controller was just disabled or removed |
| 2225 | // from effect chain to avoid volume spike |
| 2226 | if (track->mHasVolumeController) { |
| 2227 | param = AudioMixer::VOLUME; |
| 2228 | } |
| 2229 | track->mHasVolumeController = false; |
| 2230 | } |
| 2231 | |
| 2232 | // Convert volumes from 8.24 to 4.12 format |
| 2233 | int16_t left, right, aux; |
Glenn Kasten | b1cf75c | 2012-01-17 12:20:54 -0800 | [diff] [blame] | 2234 | // This additional clamping is needed in case chain->setVolume_l() overshot |
Eric Laurent | e0aed6d | 2010-09-10 17:44:44 -0700 | [diff] [blame] | 2235 | uint32_t v_clamped = (vl + (1 << 11)) >> 12; |
| 2236 | if (v_clamped > MAX_GAIN_INT) v_clamped = MAX_GAIN_INT; |
| 2237 | left = int16_t(v_clamped); |
| 2238 | v_clamped = (vr + (1 << 11)) >> 12; |
| 2239 | if (v_clamped > MAX_GAIN_INT) v_clamped = MAX_GAIN_INT; |
| 2240 | right = int16_t(v_clamped); |
| 2241 | |
| 2242 | if (va > MAX_GAIN_INT) va = MAX_GAIN_INT; |
| 2243 | aux = int16_t(va); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2244 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2245 | // XXX: these things DON'T need to be done each time |
Glenn Kasten | 9c56d4a | 2011-12-19 15:06:39 -0800 | [diff] [blame] | 2246 | mAudioMixer->setBufferProvider(name, track); |
| 2247 | mAudioMixer->enable(name); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2248 | |
Glenn Kasten | 9c56d4a | 2011-12-19 15:06:39 -0800 | [diff] [blame] | 2249 | mAudioMixer->setParameter(name, param, AudioMixer::VOLUME0, (void *)left); |
| 2250 | mAudioMixer->setParameter(name, param, AudioMixer::VOLUME1, (void *)right); |
| 2251 | mAudioMixer->setParameter(name, param, AudioMixer::AUXLEVEL, (void *)aux); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2252 | mAudioMixer->setParameter( |
Glenn Kasten | 9c56d4a | 2011-12-19 15:06:39 -0800 | [diff] [blame] | 2253 | name, |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2254 | AudioMixer::TRACK, |
| 2255 | AudioMixer::FORMAT, (void *)track->format()); |
| 2256 | mAudioMixer->setParameter( |
Glenn Kasten | 9c56d4a | 2011-12-19 15:06:39 -0800 | [diff] [blame] | 2257 | name, |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2258 | AudioMixer::TRACK, |
Jean-Michel Trivi | 0d255b2 | 2011-05-24 15:53:33 -0700 | [diff] [blame] | 2259 | AudioMixer::CHANNEL_MASK, (void *)track->channelMask()); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2260 | mAudioMixer->setParameter( |
Glenn Kasten | 9c56d4a | 2011-12-19 15:06:39 -0800 | [diff] [blame] | 2261 | name, |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2262 | AudioMixer::RESAMPLE, |
| 2263 | AudioMixer::SAMPLE_RATE, |
| 2264 | (void *)(cblk->sampleRate)); |
| 2265 | mAudioMixer->setParameter( |
Glenn Kasten | 9c56d4a | 2011-12-19 15:06:39 -0800 | [diff] [blame] | 2266 | name, |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2267 | AudioMixer::TRACK, |
| 2268 | AudioMixer::MAIN_BUFFER, (void *)track->mainBuffer()); |
| 2269 | mAudioMixer->setParameter( |
Glenn Kasten | 9c56d4a | 2011-12-19 15:06:39 -0800 | [diff] [blame] | 2270 | name, |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2271 | AudioMixer::TRACK, |
| 2272 | AudioMixer::AUX_BUFFER, (void *)track->auxBuffer()); |
| 2273 | |
| 2274 | // reset retry count |
| 2275 | track->mRetryCount = kMaxTrackRetries; |
Eric Laurent | 2774144 | 2012-01-17 19:20:12 -0800 | [diff] [blame] | 2276 | // If one track is ready, set the mixer ready if: |
| 2277 | // - the mixer was not ready during previous round OR |
| 2278 | // - no other track is not ready |
| 2279 | if (mPrevMixerStatus != MIXER_TRACKS_READY || |
| 2280 | mixerStatus != MIXER_TRACKS_ENABLED) { |
| 2281 | mixerStatus = MIXER_TRACKS_READY; |
| 2282 | } |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2283 | } else { |
Glenn Kasten | 9c56d4a | 2011-12-19 15:06:39 -0800 | [diff] [blame] | 2284 | //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] | 2285 | if (track->isStopped()) { |
| 2286 | track->reset(); |
| 2287 | } |
| 2288 | if (track->isTerminated() || track->isStopped() || track->isPaused()) { |
| 2289 | // We have consumed all the buffers of this track. |
| 2290 | // Remove it from the list of active tracks. |
| 2291 | tracksToRemove->add(track); |
| 2292 | } else { |
| 2293 | // No buffers for this track. Give it a few chances to |
| 2294 | // fill a buffer, then remove it from active list. |
| 2295 | if (--(track->mRetryCount) <= 0) { |
Glenn Kasten | 9c56d4a | 2011-12-19 15:06:39 -0800 | [diff] [blame] | 2296 | 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] | 2297 | tracksToRemove->add(track); |
Eric Laurent | 44d9848 | 2010-09-30 16:12:31 -0700 | [diff] [blame] | 2298 | // indicate to client process that the track was disabled because of underrun |
Eric Laurent | 38ccae2 | 2011-03-28 18:37:07 -0700 | [diff] [blame] | 2299 | android_atomic_or(CBLK_DISABLED_ON, &cblk->flags); |
Eric Laurent | 2774144 | 2012-01-17 19:20:12 -0800 | [diff] [blame] | 2300 | // If one track is not ready, mark the mixer also not ready if: |
| 2301 | // - the mixer was ready during previous round OR |
| 2302 | // - no other track is ready |
| 2303 | } else if (mPrevMixerStatus == MIXER_TRACKS_READY || |
| 2304 | mixerStatus != MIXER_TRACKS_READY) { |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2305 | mixerStatus = MIXER_TRACKS_ENABLED; |
| 2306 | } |
| 2307 | } |
Glenn Kasten | 9c56d4a | 2011-12-19 15:06:39 -0800 | [diff] [blame] | 2308 | mAudioMixer->disable(name); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2309 | } |
| 2310 | } |
| 2311 | |
| 2312 | // remove all the tracks that need to be... |
| 2313 | count = tracksToRemove->size(); |
Glenn Kasten | f6b1678 | 2011-12-15 09:51:17 -0800 | [diff] [blame] | 2314 | if (CC_UNLIKELY(count)) { |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2315 | for (size_t i=0 ; i<count ; i++) { |
| 2316 | const sp<Track>& track = tracksToRemove->itemAt(i); |
| 2317 | mActiveTracks.remove(track); |
| 2318 | if (track->mainBuffer() != mMixBuffer) { |
| 2319 | chain = getEffectChain_l(track->sessionId()); |
| 2320 | if (chain != 0) { |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 2321 | 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] | 2322 | chain->decActiveTrackCnt(); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2323 | } |
| 2324 | } |
| 2325 | if (track->isTerminated()) { |
Eric Laurent | b469b94 | 2011-05-09 12:09:06 -0700 | [diff] [blame] | 2326 | removeTrack_l(track); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2327 | } |
| 2328 | } |
| 2329 | } |
| 2330 | |
| 2331 | // mix buffer must be cleared if all tracks are connected to an |
| 2332 | // effect chain as in this case the mixer will not write to |
| 2333 | // mix buffer and track effects will accumulate into it |
| 2334 | if (mixedTracks != 0 && mixedTracks == tracksWithEffect) { |
| 2335 | memset(mMixBuffer, 0, mFrameCount * mChannelCount * sizeof(int16_t)); |
| 2336 | } |
| 2337 | |
Eric Laurent | 2774144 | 2012-01-17 19:20:12 -0800 | [diff] [blame] | 2338 | mPrevMixerStatus = mixerStatus; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2339 | return mixerStatus; |
| 2340 | } |
| 2341 | |
Glenn Kasten | fff6d71 | 2012-01-12 16:38:12 -0800 | [diff] [blame] | 2342 | void AudioFlinger::MixerThread::invalidateTracks(audio_stream_type_t streamType) |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2343 | { |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 2344 | ALOGV ("MixerThread::invalidateTracks() mixer %p, streamType %d, mTracks.size %d", |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 2345 | this, streamType, mTracks.size()); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2346 | Mutex::Autolock _l(mLock); |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 2347 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2348 | size_t size = mTracks.size(); |
| 2349 | for (size_t i = 0; i < size; i++) { |
| 2350 | sp<Track> t = mTracks[i]; |
| 2351 | if (t->type() == streamType) { |
Eric Laurent | 38ccae2 | 2011-03-28 18:37:07 -0700 | [diff] [blame] | 2352 | android_atomic_or(CBLK_INVALID_ON, &t->mCblk->flags); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2353 | t->mCblk->cv.signal(); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2354 | } |
| 2355 | } |
| 2356 | } |
| 2357 | |
Glenn Kasten | fff6d71 | 2012-01-12 16:38:12 -0800 | [diff] [blame] | 2358 | void AudioFlinger::PlaybackThread::setStreamValid(audio_stream_type_t streamType, bool valid) |
Eric Laurent | 9f6530f | 2011-08-30 10:18:54 -0700 | [diff] [blame] | 2359 | { |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 2360 | ALOGV ("PlaybackThread::setStreamValid() thread %p, streamType %d, valid %d", |
Eric Laurent | 9f6530f | 2011-08-30 10:18:54 -0700 | [diff] [blame] | 2361 | this, streamType, valid); |
| 2362 | Mutex::Autolock _l(mLock); |
| 2363 | |
| 2364 | mStreamTypes[streamType].valid = valid; |
| 2365 | } |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2366 | |
| 2367 | // getTrackName_l() must be called with ThreadBase::mLock held |
| 2368 | int AudioFlinger::MixerThread::getTrackName_l() |
| 2369 | { |
| 2370 | return mAudioMixer->getTrackName(); |
| 2371 | } |
| 2372 | |
| 2373 | // deleteTrackName_l() must be called with ThreadBase::mLock held |
| 2374 | void AudioFlinger::MixerThread::deleteTrackName_l(int name) |
| 2375 | { |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 2376 | ALOGV("remove track (%d) and delete from mixer", name); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2377 | mAudioMixer->deleteTrackName(name); |
| 2378 | } |
| 2379 | |
| 2380 | // checkForNewParameters_l() must be called with ThreadBase::mLock held |
| 2381 | bool AudioFlinger::MixerThread::checkForNewParameters_l() |
| 2382 | { |
| 2383 | bool reconfig = false; |
| 2384 | |
| 2385 | while (!mNewParameters.isEmpty()) { |
| 2386 | status_t status = NO_ERROR; |
| 2387 | String8 keyValuePair = mNewParameters[0]; |
| 2388 | AudioParameter param = AudioParameter(keyValuePair); |
| 2389 | int value; |
| 2390 | |
| 2391 | if (param.getInt(String8(AudioParameter::keySamplingRate), value) == NO_ERROR) { |
| 2392 | reconfig = true; |
| 2393 | } |
| 2394 | if (param.getInt(String8(AudioParameter::keyFormat), value) == NO_ERROR) { |
Glenn Kasten | 58f3021 | 2012-01-12 12:27:51 -0800 | [diff] [blame] | 2395 | if ((audio_format_t) value != AUDIO_FORMAT_PCM_16_BIT) { |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2396 | status = BAD_VALUE; |
| 2397 | } else { |
| 2398 | reconfig = true; |
| 2399 | } |
| 2400 | } |
| 2401 | if (param.getInt(String8(AudioParameter::keyChannels), value) == NO_ERROR) { |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 2402 | if (value != AUDIO_CHANNEL_OUT_STEREO) { |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2403 | status = BAD_VALUE; |
| 2404 | } else { |
| 2405 | reconfig = true; |
| 2406 | } |
| 2407 | } |
| 2408 | if (param.getInt(String8(AudioParameter::keyFrameCount), value) == NO_ERROR) { |
| 2409 | // 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] | 2410 | // size depends on frame count and correct behavior would not be guaranteed |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2411 | // if frame count is changed after track creation |
| 2412 | if (!mTracks.isEmpty()) { |
| 2413 | status = INVALID_OPERATION; |
| 2414 | } else { |
| 2415 | reconfig = true; |
| 2416 | } |
| 2417 | } |
| 2418 | if (param.getInt(String8(AudioParameter::keyRouting), value) == NO_ERROR) { |
Gloria Wang | 9ee159b | 2011-02-24 14:51:45 -0800 | [diff] [blame] | 2419 | // when changing the audio output device, call addBatteryData to notify |
| 2420 | // the change |
Eric Laurent | b469b94 | 2011-05-09 12:09:06 -0700 | [diff] [blame] | 2421 | if ((int)mDevice != value) { |
Gloria Wang | 9ee159b | 2011-02-24 14:51:45 -0800 | [diff] [blame] | 2422 | uint32_t params = 0; |
| 2423 | // check whether speaker is on |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 2424 | if (value & AUDIO_DEVICE_OUT_SPEAKER) { |
Gloria Wang | 9ee159b | 2011-02-24 14:51:45 -0800 | [diff] [blame] | 2425 | params |= IMediaPlayerService::kBatteryDataSpeakerOn; |
| 2426 | } |
| 2427 | |
| 2428 | int deviceWithoutSpeaker |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 2429 | = AUDIO_DEVICE_OUT_ALL & ~AUDIO_DEVICE_OUT_SPEAKER; |
Gloria Wang | 9ee159b | 2011-02-24 14:51:45 -0800 | [diff] [blame] | 2430 | // check if any other device (except speaker) is on |
| 2431 | if (value & deviceWithoutSpeaker ) { |
| 2432 | params |= IMediaPlayerService::kBatteryDataOtherAudioDeviceOn; |
| 2433 | } |
| 2434 | |
| 2435 | if (params != 0) { |
| 2436 | addBatteryData(params); |
| 2437 | } |
| 2438 | } |
| 2439 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2440 | // forward device change to effects that have requested to be |
| 2441 | // aware of attached audio device. |
| 2442 | mDevice = (uint32_t)value; |
| 2443 | for (size_t i = 0; i < mEffectChains.size(); i++) { |
Eric Laurent | cab1124 | 2010-07-15 12:50:15 -0700 | [diff] [blame] | 2444 | mEffectChains[i]->setDevice_l(mDevice); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2445 | } |
| 2446 | } |
| 2447 | |
| 2448 | if (status == NO_ERROR) { |
Dima Zavin | 799a70e | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 2449 | status = mOutput->stream->common.set_parameters(&mOutput->stream->common, |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 2450 | keyValuePair.string()); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2451 | if (!mStandby && status == INVALID_OPERATION) { |
Dima Zavin | 799a70e | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 2452 | mOutput->stream->common.standby(&mOutput->stream->common); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2453 | mStandby = true; |
| 2454 | mBytesWritten = 0; |
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 | } |
| 2458 | if (status == NO_ERROR && reconfig) { |
| 2459 | delete mAudioMixer; |
| 2460 | readOutputParameters(); |
| 2461 | mAudioMixer = new AudioMixer(mFrameCount, mSampleRate); |
| 2462 | for (size_t i = 0; i < mTracks.size() ; i++) { |
| 2463 | int name = getTrackName_l(); |
| 2464 | if (name < 0) break; |
| 2465 | mTracks[i]->mName = name; |
| 2466 | // limit track sample rate to 2 x new output sample rate |
| 2467 | if (mTracks[i]->mCblk->sampleRate > 2 * sampleRate()) { |
| 2468 | mTracks[i]->mCblk->sampleRate = 2 * sampleRate(); |
| 2469 | } |
| 2470 | } |
| 2471 | sendConfigEvent_l(AudioSystem::OUTPUT_CONFIG_CHANGED); |
| 2472 | } |
| 2473 | } |
| 2474 | |
| 2475 | mNewParameters.removeAt(0); |
| 2476 | |
| 2477 | mParamStatus = status; |
| 2478 | mParamCond.signal(); |
Eric Laurent | 60cd0a0 | 2011-09-13 11:40:21 -0700 | [diff] [blame] | 2479 | // wait for condition with time out in case the thread calling ThreadBase::setParameters() |
| 2480 | // 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] | 2481 | mWaitWorkCV.waitRelative(mLock, kSetParametersTimeoutNs); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2482 | } |
| 2483 | return reconfig; |
| 2484 | } |
| 2485 | |
| 2486 | status_t AudioFlinger::MixerThread::dumpInternals(int fd, const Vector<String16>& args) |
| 2487 | { |
| 2488 | const size_t SIZE = 256; |
| 2489 | char buffer[SIZE]; |
| 2490 | String8 result; |
| 2491 | |
| 2492 | PlaybackThread::dumpInternals(fd, args); |
| 2493 | |
| 2494 | snprintf(buffer, SIZE, "AudioMixer tracks: %08x\n", mAudioMixer->trackNames()); |
| 2495 | result.append(buffer); |
| 2496 | write(fd, result.string(), result.size()); |
| 2497 | return NO_ERROR; |
| 2498 | } |
| 2499 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2500 | uint32_t AudioFlinger::MixerThread::idleSleepTimeUs() |
| 2501 | { |
Eric Laurent | 60e1824 | 2010-07-29 06:50:24 -0700 | [diff] [blame] | 2502 | return (uint32_t)(((mFrameCount * 1000) / mSampleRate) * 1000) / 2; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2503 | } |
| 2504 | |
Eric Laurent | 25cbe0e | 2010-08-18 18:13:17 -0700 | [diff] [blame] | 2505 | uint32_t AudioFlinger::MixerThread::suspendSleepTimeUs() |
| 2506 | { |
| 2507 | return (uint32_t)(((mFrameCount * 1000) / mSampleRate) * 1000); |
| 2508 | } |
| 2509 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2510 | // ---------------------------------------------------------------------------- |
Dima Zavin | 799a70e | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 2511 | AudioFlinger::DirectOutputThread::DirectOutputThread(const sp<AudioFlinger>& audioFlinger, AudioStreamOut* output, int id, uint32_t device) |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2512 | : PlaybackThread(audioFlinger, output, id, device) |
| 2513 | { |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 2514 | mType = ThreadBase::DIRECT; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2515 | } |
| 2516 | |
| 2517 | AudioFlinger::DirectOutputThread::~DirectOutputThread() |
| 2518 | { |
| 2519 | } |
| 2520 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2521 | static inline |
| 2522 | int32_t mul(int16_t in, int16_t v) |
| 2523 | { |
| 2524 | #if defined(__arm__) && !defined(__thumb__) |
| 2525 | int32_t out; |
| 2526 | asm( "smulbb %[out], %[in], %[v] \n" |
| 2527 | : [out]"=r"(out) |
| 2528 | : [in]"%r"(in), [v]"r"(v) |
| 2529 | : ); |
| 2530 | return out; |
| 2531 | #else |
| 2532 | return in * int32_t(v); |
| 2533 | #endif |
| 2534 | } |
| 2535 | |
| 2536 | void AudioFlinger::DirectOutputThread::applyVolume(uint16_t leftVol, uint16_t rightVol, bool ramp) |
| 2537 | { |
| 2538 | // Do not apply volume on compressed audio |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 2539 | if (!audio_is_linear_pcm(mFormat)) { |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2540 | return; |
| 2541 | } |
| 2542 | |
| 2543 | // convert to signed 16 bit before volume calculation |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 2544 | if (mFormat == AUDIO_FORMAT_PCM_8_BIT) { |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2545 | size_t count = mFrameCount * mChannelCount; |
| 2546 | uint8_t *src = (uint8_t *)mMixBuffer + count-1; |
| 2547 | int16_t *dst = mMixBuffer + count-1; |
| 2548 | while(count--) { |
| 2549 | *dst-- = (int16_t)(*src--^0x80) << 8; |
| 2550 | } |
| 2551 | } |
| 2552 | |
| 2553 | size_t frameCount = mFrameCount; |
| 2554 | int16_t *out = mMixBuffer; |
| 2555 | if (ramp) { |
| 2556 | if (mChannelCount == 1) { |
| 2557 | int32_t d = ((int32_t)leftVol - (int32_t)mLeftVolShort) << 16; |
| 2558 | int32_t vlInc = d / (int32_t)frameCount; |
| 2559 | int32_t vl = ((int32_t)mLeftVolShort << 16); |
| 2560 | do { |
| 2561 | out[0] = clamp16(mul(out[0], vl >> 16) >> 12); |
| 2562 | out++; |
| 2563 | vl += vlInc; |
| 2564 | } while (--frameCount); |
| 2565 | |
| 2566 | } else { |
| 2567 | int32_t d = ((int32_t)leftVol - (int32_t)mLeftVolShort) << 16; |
| 2568 | int32_t vlInc = d / (int32_t)frameCount; |
| 2569 | d = ((int32_t)rightVol - (int32_t)mRightVolShort) << 16; |
| 2570 | int32_t vrInc = d / (int32_t)frameCount; |
| 2571 | int32_t vl = ((int32_t)mLeftVolShort << 16); |
| 2572 | int32_t vr = ((int32_t)mRightVolShort << 16); |
| 2573 | do { |
| 2574 | out[0] = clamp16(mul(out[0], vl >> 16) >> 12); |
| 2575 | out[1] = clamp16(mul(out[1], vr >> 16) >> 12); |
| 2576 | out += 2; |
| 2577 | vl += vlInc; |
| 2578 | vr += vrInc; |
| 2579 | } while (--frameCount); |
| 2580 | } |
| 2581 | } else { |
| 2582 | if (mChannelCount == 1) { |
| 2583 | do { |
| 2584 | out[0] = clamp16(mul(out[0], leftVol) >> 12); |
| 2585 | out++; |
| 2586 | } while (--frameCount); |
| 2587 | } else { |
| 2588 | do { |
| 2589 | out[0] = clamp16(mul(out[0], leftVol) >> 12); |
| 2590 | out[1] = clamp16(mul(out[1], rightVol) >> 12); |
| 2591 | out += 2; |
| 2592 | } while (--frameCount); |
| 2593 | } |
| 2594 | } |
| 2595 | |
| 2596 | // convert back to unsigned 8 bit after volume calculation |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 2597 | if (mFormat == AUDIO_FORMAT_PCM_8_BIT) { |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2598 | size_t count = mFrameCount * mChannelCount; |
| 2599 | int16_t *src = mMixBuffer; |
| 2600 | uint8_t *dst = (uint8_t *)mMixBuffer; |
| 2601 | while(count--) { |
| 2602 | *dst++ = (uint8_t)(((int32_t)*src++ + (1<<7)) >> 8)^0x80; |
| 2603 | } |
| 2604 | } |
| 2605 | |
| 2606 | mLeftVolShort = leftVol; |
| 2607 | mRightVolShort = rightVol; |
| 2608 | } |
| 2609 | |
| 2610 | bool AudioFlinger::DirectOutputThread::threadLoop() |
| 2611 | { |
| 2612 | uint32_t mixerStatus = MIXER_IDLE; |
| 2613 | sp<Track> trackToRemove; |
| 2614 | sp<Track> activeTrack; |
| 2615 | nsecs_t standbyTime = systemTime(); |
| 2616 | int8_t *curBuf; |
| 2617 | size_t mixBufferSize = mFrameCount*mFrameSize; |
| 2618 | uint32_t activeSleepTime = activeSleepTimeUs(); |
| 2619 | uint32_t idleSleepTime = idleSleepTimeUs(); |
| 2620 | uint32_t sleepTime = idleSleepTime; |
| 2621 | // use shorter standby delay as on normal output to release |
| 2622 | // hardware resources as soon as possible |
| 2623 | nsecs_t standbyDelay = microseconds(activeSleepTime*2); |
| 2624 | |
Eric Laurent | feb0db6 | 2011-07-22 09:04:31 -0700 | [diff] [blame] | 2625 | acquireWakeLock(); |
| 2626 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2627 | while (!exitPending()) |
| 2628 | { |
| 2629 | bool rampVolume; |
| 2630 | uint16_t leftVol; |
| 2631 | uint16_t rightVol; |
| 2632 | Vector< sp<EffectChain> > effectChains; |
| 2633 | |
| 2634 | processConfigEvents(); |
| 2635 | |
| 2636 | mixerStatus = MIXER_IDLE; |
| 2637 | |
| 2638 | { // scope for the mLock |
| 2639 | |
| 2640 | Mutex::Autolock _l(mLock); |
| 2641 | |
| 2642 | if (checkForNewParameters_l()) { |
| 2643 | mixBufferSize = mFrameCount*mFrameSize; |
| 2644 | activeSleepTime = activeSleepTimeUs(); |
| 2645 | idleSleepTime = idleSleepTimeUs(); |
| 2646 | standbyDelay = microseconds(activeSleepTime*2); |
| 2647 | } |
| 2648 | |
| 2649 | // put audio hardware into standby after short delay |
Glenn Kasten | f6b1678 | 2011-12-15 09:51:17 -0800 | [diff] [blame] | 2650 | if (CC_UNLIKELY((!mActiveTracks.size() && systemTime() > standbyTime) || |
| 2651 | mSuspended)) { |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2652 | // wait until we have something to do... |
| 2653 | if (!mStandby) { |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 2654 | ALOGV("Audio hardware entering standby, mixer %p\n", this); |
Dima Zavin | 799a70e | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 2655 | mOutput->stream->common.standby(&mOutput->stream->common); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2656 | mStandby = true; |
| 2657 | mBytesWritten = 0; |
| 2658 | } |
| 2659 | |
| 2660 | if (!mActiveTracks.size() && mConfigEvents.isEmpty()) { |
| 2661 | // we're about to wait, flush the binder command buffer |
| 2662 | IPCThreadState::self()->flushCommands(); |
| 2663 | |
| 2664 | if (exitPending()) break; |
| 2665 | |
Eric Laurent | feb0db6 | 2011-07-22 09:04:31 -0700 | [diff] [blame] | 2666 | releaseWakeLock_l(); |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 2667 | ALOGV("DirectOutputThread %p TID %d going to sleep\n", this, gettid()); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2668 | mWaitWorkCV.wait(mLock); |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 2669 | 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] | 2670 | acquireWakeLock_l(); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2671 | |
Glenn Kasten | f1d4592 | 2012-01-13 15:54:24 -0800 | [diff] [blame] | 2672 | if (!mMasterMute) { |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2673 | char value[PROPERTY_VALUE_MAX]; |
| 2674 | property_get("ro.audio.silent", value, "0"); |
| 2675 | if (atoi(value)) { |
Steve Block | b8a8052 | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 2676 | ALOGD("Silence is golden"); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2677 | setMasterMute(true); |
| 2678 | } |
| 2679 | } |
| 2680 | |
| 2681 | standbyTime = systemTime() + standbyDelay; |
| 2682 | sleepTime = idleSleepTime; |
| 2683 | continue; |
| 2684 | } |
| 2685 | } |
| 2686 | |
| 2687 | effectChains = mEffectChains; |
| 2688 | |
| 2689 | // find out which tracks need to be processed |
| 2690 | if (mActiveTracks.size() != 0) { |
| 2691 | sp<Track> t = mActiveTracks[0].promote(); |
| 2692 | if (t == 0) continue; |
| 2693 | |
| 2694 | Track* const track = t.get(); |
| 2695 | audio_track_cblk_t* cblk = track->cblk(); |
| 2696 | |
| 2697 | // The first time a track is added we wait |
| 2698 | // for all its buffers to be filled before processing it |
Eric Laurent | af59ce2 | 2010-10-05 14:41:42 -0700 | [diff] [blame] | 2699 | if (cblk->framesReady() && track->isReady() && |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2700 | !track->isPaused() && !track->isTerminated()) |
| 2701 | { |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 2702 | //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] | 2703 | |
| 2704 | if (track->mFillingUpStatus == Track::FS_FILLED) { |
| 2705 | track->mFillingUpStatus = Track::FS_ACTIVE; |
| 2706 | mLeftVolFloat = mRightVolFloat = 0; |
| 2707 | mLeftVolShort = mRightVolShort = 0; |
| 2708 | if (track->mState == TrackBase::RESUMING) { |
| 2709 | track->mState = TrackBase::ACTIVE; |
| 2710 | rampVolume = true; |
| 2711 | } |
| 2712 | } else if (cblk->server != 0) { |
| 2713 | // If the track is stopped before the first frame was mixed, |
| 2714 | // do not apply ramp |
| 2715 | rampVolume = true; |
| 2716 | } |
| 2717 | // compute volume for this track |
| 2718 | float left, right; |
| 2719 | if (track->isMuted() || mMasterMute || track->isPausing() || |
| 2720 | mStreamTypes[track->type()].mute) { |
| 2721 | left = right = 0; |
| 2722 | if (track->isPausing()) { |
| 2723 | track->setPaused(); |
| 2724 | } |
| 2725 | } else { |
| 2726 | float typeVolume = mStreamTypes[track->type()].volume; |
| 2727 | float v = mMasterVolume * typeVolume; |
Glenn Kasten | b1cf75c | 2012-01-17 12:20:54 -0800 | [diff] [blame] | 2728 | uint32_t vlr = cblk->volumeLR; |
| 2729 | float v_clamped = v * (vlr & 0xFFFF); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2730 | if (v_clamped > MAX_GAIN) v_clamped = MAX_GAIN; |
| 2731 | left = v_clamped/MAX_GAIN; |
Glenn Kasten | b1cf75c | 2012-01-17 12:20:54 -0800 | [diff] [blame] | 2732 | v_clamped = v * (vlr >> 16); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2733 | if (v_clamped > MAX_GAIN) v_clamped = MAX_GAIN; |
| 2734 | right = v_clamped/MAX_GAIN; |
| 2735 | } |
| 2736 | |
| 2737 | if (left != mLeftVolFloat || right != mRightVolFloat) { |
| 2738 | mLeftVolFloat = left; |
| 2739 | mRightVolFloat = right; |
| 2740 | |
| 2741 | // If audio HAL implements volume control, |
| 2742 | // force software volume to nominal value |
Dima Zavin | 799a70e | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 2743 | if (mOutput->stream->set_volume(mOutput->stream, left, right) == NO_ERROR) { |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2744 | left = 1.0f; |
| 2745 | right = 1.0f; |
| 2746 | } |
| 2747 | |
| 2748 | // Convert volumes from float to 8.24 |
| 2749 | uint32_t vl = (uint32_t)(left * (1 << 24)); |
| 2750 | uint32_t vr = (uint32_t)(right * (1 << 24)); |
| 2751 | |
| 2752 | // Delegate volume control to effect in track effect chain if needed |
| 2753 | // only one effect chain can be present on DirectOutputThread, so if |
| 2754 | // there is one, the track is connected to it |
| 2755 | if (!effectChains.isEmpty()) { |
Eric Laurent | e0aed6d | 2010-09-10 17:44:44 -0700 | [diff] [blame] | 2756 | // Do not ramp volume if volume is controlled by effect |
Eric Laurent | cab1124 | 2010-07-15 12:50:15 -0700 | [diff] [blame] | 2757 | if(effectChains[0]->setVolume_l(&vl, &vr)) { |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2758 | rampVolume = false; |
| 2759 | } |
| 2760 | } |
| 2761 | |
| 2762 | // Convert volumes from 8.24 to 4.12 format |
| 2763 | uint32_t v_clamped = (vl + (1 << 11)) >> 12; |
| 2764 | if (v_clamped > MAX_GAIN_INT) v_clamped = MAX_GAIN_INT; |
| 2765 | leftVol = (uint16_t)v_clamped; |
| 2766 | v_clamped = (vr + (1 << 11)) >> 12; |
| 2767 | if (v_clamped > MAX_GAIN_INT) v_clamped = MAX_GAIN_INT; |
| 2768 | rightVol = (uint16_t)v_clamped; |
| 2769 | } else { |
| 2770 | leftVol = mLeftVolShort; |
| 2771 | rightVol = mRightVolShort; |
| 2772 | rampVolume = false; |
| 2773 | } |
| 2774 | |
| 2775 | // reset retry count |
| 2776 | track->mRetryCount = kMaxTrackRetriesDirect; |
| 2777 | activeTrack = t; |
| 2778 | mixerStatus = MIXER_TRACKS_READY; |
| 2779 | } else { |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 2780 | //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] | 2781 | if (track->isStopped()) { |
| 2782 | track->reset(); |
| 2783 | } |
| 2784 | if (track->isTerminated() || track->isStopped() || track->isPaused()) { |
| 2785 | // We have consumed all the buffers of this track. |
| 2786 | // Remove it from the list of active tracks. |
| 2787 | trackToRemove = track; |
| 2788 | } else { |
| 2789 | // No buffers for this track. Give it a few chances to |
| 2790 | // fill a buffer, then remove it from active list. |
| 2791 | if (--(track->mRetryCount) <= 0) { |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 2792 | ALOGV("BUFFER TIMEOUT: remove(%d) from active list", track->name()); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2793 | trackToRemove = track; |
| 2794 | } else { |
| 2795 | mixerStatus = MIXER_TRACKS_ENABLED; |
| 2796 | } |
| 2797 | } |
| 2798 | } |
| 2799 | } |
| 2800 | |
| 2801 | // remove all the tracks that need to be... |
Glenn Kasten | f6b1678 | 2011-12-15 09:51:17 -0800 | [diff] [blame] | 2802 | if (CC_UNLIKELY(trackToRemove != 0)) { |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2803 | mActiveTracks.remove(trackToRemove); |
| 2804 | if (!effectChains.isEmpty()) { |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 2805 | 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] | 2806 | trackToRemove->sessionId()); |
Eric Laurent | b469b94 | 2011-05-09 12:09:06 -0700 | [diff] [blame] | 2807 | effectChains[0]->decActiveTrackCnt(); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2808 | } |
| 2809 | if (trackToRemove->isTerminated()) { |
Eric Laurent | b469b94 | 2011-05-09 12:09:06 -0700 | [diff] [blame] | 2810 | removeTrack_l(trackToRemove); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2811 | } |
| 2812 | } |
| 2813 | |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 2814 | lockEffectChains_l(effectChains); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2815 | } |
| 2816 | |
Glenn Kasten | f6b1678 | 2011-12-15 09:51:17 -0800 | [diff] [blame] | 2817 | if (CC_LIKELY(mixerStatus == MIXER_TRACKS_READY)) { |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2818 | AudioBufferProvider::Buffer buffer; |
| 2819 | size_t frameCount = mFrameCount; |
| 2820 | curBuf = (int8_t *)mMixBuffer; |
| 2821 | // output audio to hardware |
| 2822 | while (frameCount) { |
| 2823 | buffer.frameCount = frameCount; |
| 2824 | activeTrack->getNextBuffer(&buffer); |
Glenn Kasten | f6b1678 | 2011-12-15 09:51:17 -0800 | [diff] [blame] | 2825 | if (CC_UNLIKELY(buffer.raw == NULL)) { |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2826 | memset(curBuf, 0, frameCount * mFrameSize); |
| 2827 | break; |
| 2828 | } |
| 2829 | memcpy(curBuf, buffer.raw, buffer.frameCount * mFrameSize); |
| 2830 | frameCount -= buffer.frameCount; |
| 2831 | curBuf += buffer.frameCount * mFrameSize; |
| 2832 | activeTrack->releaseBuffer(&buffer); |
| 2833 | } |
| 2834 | sleepTime = 0; |
| 2835 | standbyTime = systemTime() + standbyDelay; |
| 2836 | } else { |
| 2837 | if (sleepTime == 0) { |
| 2838 | if (mixerStatus == MIXER_TRACKS_ENABLED) { |
| 2839 | sleepTime = activeSleepTime; |
| 2840 | } else { |
| 2841 | sleepTime = idleSleepTime; |
| 2842 | } |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 2843 | } else if (mBytesWritten != 0 && audio_is_linear_pcm(mFormat)) { |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2844 | memset (mMixBuffer, 0, mFrameCount * mFrameSize); |
| 2845 | sleepTime = 0; |
| 2846 | } |
| 2847 | } |
| 2848 | |
| 2849 | if (mSuspended) { |
Eric Laurent | 25cbe0e | 2010-08-18 18:13:17 -0700 | [diff] [blame] | 2850 | sleepTime = suspendSleepTimeUs(); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2851 | } |
| 2852 | // sleepTime == 0 means we must write to audio hardware |
| 2853 | if (sleepTime == 0) { |
| 2854 | if (mixerStatus == MIXER_TRACKS_READY) { |
| 2855 | applyVolume(leftVol, rightVol, rampVolume); |
| 2856 | } |
| 2857 | for (size_t i = 0; i < effectChains.size(); i ++) { |
| 2858 | effectChains[i]->process_l(); |
| 2859 | } |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 2860 | unlockEffectChains(effectChains); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2861 | |
| 2862 | mLastWriteTime = systemTime(); |
| 2863 | mInWrite = true; |
| 2864 | mBytesWritten += mixBufferSize; |
Dima Zavin | 799a70e | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 2865 | int bytesWritten = (int)mOutput->stream->write(mOutput->stream, mMixBuffer, mixBufferSize); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2866 | if (bytesWritten < 0) mBytesWritten -= mixBufferSize; |
| 2867 | mNumWrites++; |
| 2868 | mInWrite = false; |
| 2869 | mStandby = false; |
| 2870 | } else { |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 2871 | unlockEffectChains(effectChains); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2872 | usleep(sleepTime); |
| 2873 | } |
| 2874 | |
| 2875 | // finally let go of removed track, without the lock held |
| 2876 | // since we can't guarantee the destructors won't acquire that |
| 2877 | // same lock. |
| 2878 | trackToRemove.clear(); |
| 2879 | activeTrack.clear(); |
| 2880 | |
| 2881 | // Effect chains will be actually deleted here if they were removed from |
| 2882 | // mEffectChains list during mixing or effects processing |
| 2883 | effectChains.clear(); |
| 2884 | } |
| 2885 | |
| 2886 | if (!mStandby) { |
Dima Zavin | 799a70e | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 2887 | mOutput->stream->common.standby(&mOutput->stream->common); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2888 | } |
| 2889 | |
Eric Laurent | feb0db6 | 2011-07-22 09:04:31 -0700 | [diff] [blame] | 2890 | releaseWakeLock(); |
| 2891 | |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 2892 | ALOGV("DirectOutputThread %p exiting", this); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2893 | return false; |
| 2894 | } |
| 2895 | |
| 2896 | // getTrackName_l() must be called with ThreadBase::mLock held |
| 2897 | int AudioFlinger::DirectOutputThread::getTrackName_l() |
| 2898 | { |
| 2899 | return 0; |
| 2900 | } |
| 2901 | |
| 2902 | // deleteTrackName_l() must be called with ThreadBase::mLock held |
| 2903 | void AudioFlinger::DirectOutputThread::deleteTrackName_l(int name) |
| 2904 | { |
| 2905 | } |
| 2906 | |
| 2907 | // checkForNewParameters_l() must be called with ThreadBase::mLock held |
| 2908 | bool AudioFlinger::DirectOutputThread::checkForNewParameters_l() |
| 2909 | { |
| 2910 | bool reconfig = false; |
| 2911 | |
| 2912 | while (!mNewParameters.isEmpty()) { |
| 2913 | status_t status = NO_ERROR; |
| 2914 | String8 keyValuePair = mNewParameters[0]; |
| 2915 | AudioParameter param = AudioParameter(keyValuePair); |
| 2916 | int value; |
| 2917 | |
| 2918 | if (param.getInt(String8(AudioParameter::keyFrameCount), value) == NO_ERROR) { |
| 2919 | // do not accept frame count changes if tracks are open as the track buffer |
| 2920 | // size depends on frame count and correct behavior would not be garantied |
| 2921 | // if frame count is changed after track creation |
| 2922 | if (!mTracks.isEmpty()) { |
| 2923 | status = INVALID_OPERATION; |
| 2924 | } else { |
| 2925 | reconfig = true; |
| 2926 | } |
| 2927 | } |
| 2928 | if (status == NO_ERROR) { |
Dima Zavin | 799a70e | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 2929 | status = mOutput->stream->common.set_parameters(&mOutput->stream->common, |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 2930 | keyValuePair.string()); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2931 | if (!mStandby && status == INVALID_OPERATION) { |
Dima Zavin | 799a70e | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 2932 | mOutput->stream->common.standby(&mOutput->stream->common); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2933 | mStandby = true; |
| 2934 | mBytesWritten = 0; |
Dima Zavin | 799a70e | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 2935 | status = mOutput->stream->common.set_parameters(&mOutput->stream->common, |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 2936 | keyValuePair.string()); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2937 | } |
| 2938 | if (status == NO_ERROR && reconfig) { |
| 2939 | readOutputParameters(); |
| 2940 | sendConfigEvent_l(AudioSystem::OUTPUT_CONFIG_CHANGED); |
| 2941 | } |
| 2942 | } |
| 2943 | |
| 2944 | mNewParameters.removeAt(0); |
| 2945 | |
| 2946 | mParamStatus = status; |
| 2947 | mParamCond.signal(); |
Eric Laurent | 60cd0a0 | 2011-09-13 11:40:21 -0700 | [diff] [blame] | 2948 | // wait for condition with time out in case the thread calling ThreadBase::setParameters() |
| 2949 | // 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] | 2950 | mWaitWorkCV.waitRelative(mLock, kSetParametersTimeoutNs); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2951 | } |
| 2952 | return reconfig; |
| 2953 | } |
| 2954 | |
| 2955 | uint32_t AudioFlinger::DirectOutputThread::activeSleepTimeUs() |
| 2956 | { |
| 2957 | uint32_t time; |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 2958 | if (audio_is_linear_pcm(mFormat)) { |
Eric Laurent | 162b40b | 2011-12-05 09:47:19 -0800 | [diff] [blame] | 2959 | time = PlaybackThread::activeSleepTimeUs(); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2960 | } else { |
| 2961 | time = 10000; |
| 2962 | } |
| 2963 | return time; |
| 2964 | } |
| 2965 | |
| 2966 | uint32_t AudioFlinger::DirectOutputThread::idleSleepTimeUs() |
| 2967 | { |
| 2968 | uint32_t time; |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 2969 | if (audio_is_linear_pcm(mFormat)) { |
Eric Laurent | 60e1824 | 2010-07-29 06:50:24 -0700 | [diff] [blame] | 2970 | time = (uint32_t)(((mFrameCount * 1000) / mSampleRate) * 1000) / 2; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2971 | } else { |
| 2972 | time = 10000; |
| 2973 | } |
| 2974 | return time; |
| 2975 | } |
| 2976 | |
Eric Laurent | 25cbe0e | 2010-08-18 18:13:17 -0700 | [diff] [blame] | 2977 | uint32_t AudioFlinger::DirectOutputThread::suspendSleepTimeUs() |
| 2978 | { |
| 2979 | uint32_t time; |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 2980 | if (audio_is_linear_pcm(mFormat)) { |
Eric Laurent | 25cbe0e | 2010-08-18 18:13:17 -0700 | [diff] [blame] | 2981 | time = (uint32_t)(((mFrameCount * 1000) / mSampleRate) * 1000); |
| 2982 | } else { |
| 2983 | time = 10000; |
| 2984 | } |
| 2985 | return time; |
| 2986 | } |
| 2987 | |
| 2988 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2989 | // ---------------------------------------------------------------------------- |
| 2990 | |
| 2991 | AudioFlinger::DuplicatingThread::DuplicatingThread(const sp<AudioFlinger>& audioFlinger, AudioFlinger::MixerThread* mainThread, int id) |
| 2992 | : MixerThread(audioFlinger, mainThread->getOutput(), id, mainThread->device()), mWaitTimeMs(UINT_MAX) |
| 2993 | { |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 2994 | mType = ThreadBase::DUPLICATING; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2995 | addOutputTrack(mainThread); |
| 2996 | } |
| 2997 | |
| 2998 | AudioFlinger::DuplicatingThread::~DuplicatingThread() |
| 2999 | { |
| 3000 | for (size_t i = 0; i < mOutputTracks.size(); i++) { |
| 3001 | mOutputTracks[i]->destroy(); |
| 3002 | } |
| 3003 | mOutputTracks.clear(); |
| 3004 | } |
| 3005 | |
| 3006 | bool AudioFlinger::DuplicatingThread::threadLoop() |
| 3007 | { |
| 3008 | Vector< sp<Track> > tracksToRemove; |
| 3009 | uint32_t mixerStatus = MIXER_IDLE; |
| 3010 | nsecs_t standbyTime = systemTime(); |
| 3011 | size_t mixBufferSize = mFrameCount*mFrameSize; |
| 3012 | SortedVector< sp<OutputTrack> > outputTracks; |
| 3013 | uint32_t writeFrames = 0; |
| 3014 | uint32_t activeSleepTime = activeSleepTimeUs(); |
| 3015 | uint32_t idleSleepTime = idleSleepTimeUs(); |
| 3016 | uint32_t sleepTime = idleSleepTime; |
| 3017 | Vector< sp<EffectChain> > effectChains; |
| 3018 | |
Eric Laurent | feb0db6 | 2011-07-22 09:04:31 -0700 | [diff] [blame] | 3019 | acquireWakeLock(); |
| 3020 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3021 | while (!exitPending()) |
| 3022 | { |
| 3023 | processConfigEvents(); |
| 3024 | |
| 3025 | mixerStatus = MIXER_IDLE; |
| 3026 | { // scope for the mLock |
| 3027 | |
| 3028 | Mutex::Autolock _l(mLock); |
| 3029 | |
| 3030 | if (checkForNewParameters_l()) { |
| 3031 | mixBufferSize = mFrameCount*mFrameSize; |
| 3032 | updateWaitTime(); |
| 3033 | activeSleepTime = activeSleepTimeUs(); |
| 3034 | idleSleepTime = idleSleepTimeUs(); |
| 3035 | } |
| 3036 | |
| 3037 | const SortedVector< wp<Track> >& activeTracks = mActiveTracks; |
| 3038 | |
| 3039 | for (size_t i = 0; i < mOutputTracks.size(); i++) { |
| 3040 | outputTracks.add(mOutputTracks[i]); |
| 3041 | } |
| 3042 | |
| 3043 | // put audio hardware into standby after short delay |
Glenn Kasten | f6b1678 | 2011-12-15 09:51:17 -0800 | [diff] [blame] | 3044 | if (CC_UNLIKELY((!activeTracks.size() && systemTime() > standbyTime) || |
| 3045 | mSuspended)) { |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3046 | if (!mStandby) { |
| 3047 | for (size_t i = 0; i < outputTracks.size(); i++) { |
| 3048 | outputTracks[i]->stop(); |
| 3049 | } |
| 3050 | mStandby = true; |
| 3051 | mBytesWritten = 0; |
| 3052 | } |
| 3053 | |
| 3054 | if (!activeTracks.size() && mConfigEvents.isEmpty()) { |
| 3055 | // we're about to wait, flush the binder command buffer |
| 3056 | IPCThreadState::self()->flushCommands(); |
| 3057 | outputTracks.clear(); |
| 3058 | |
| 3059 | if (exitPending()) break; |
| 3060 | |
Eric Laurent | feb0db6 | 2011-07-22 09:04:31 -0700 | [diff] [blame] | 3061 | releaseWakeLock_l(); |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 3062 | ALOGV("DuplicatingThread %p TID %d going to sleep\n", this, gettid()); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3063 | mWaitWorkCV.wait(mLock); |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 3064 | ALOGV("DuplicatingThread %p TID %d waking up\n", this, gettid()); |
Eric Laurent | feb0db6 | 2011-07-22 09:04:31 -0700 | [diff] [blame] | 3065 | acquireWakeLock_l(); |
| 3066 | |
Eric Laurent | 2774144 | 2012-01-17 19:20:12 -0800 | [diff] [blame] | 3067 | mPrevMixerStatus = MIXER_IDLE; |
Glenn Kasten | f1d4592 | 2012-01-13 15:54:24 -0800 | [diff] [blame] | 3068 | if (!mMasterMute) { |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3069 | char value[PROPERTY_VALUE_MAX]; |
| 3070 | property_get("ro.audio.silent", value, "0"); |
| 3071 | if (atoi(value)) { |
Steve Block | b8a8052 | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 3072 | ALOGD("Silence is golden"); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3073 | setMasterMute(true); |
| 3074 | } |
| 3075 | } |
| 3076 | |
| 3077 | standbyTime = systemTime() + kStandbyTimeInNsecs; |
| 3078 | sleepTime = idleSleepTime; |
| 3079 | continue; |
| 3080 | } |
| 3081 | } |
| 3082 | |
| 3083 | mixerStatus = prepareTracks_l(activeTracks, &tracksToRemove); |
| 3084 | |
| 3085 | // prevent any changes in effect chain list and in each effect chain |
| 3086 | // during mixing and effect process as the audio buffers could be deleted |
| 3087 | // or modified if an effect is created or deleted |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 3088 | lockEffectChains_l(effectChains); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3089 | } |
| 3090 | |
Glenn Kasten | f6b1678 | 2011-12-15 09:51:17 -0800 | [diff] [blame] | 3091 | if (CC_LIKELY(mixerStatus == MIXER_TRACKS_READY)) { |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3092 | // mix buffers... |
| 3093 | if (outputsReady(outputTracks)) { |
| 3094 | mAudioMixer->process(); |
| 3095 | } else { |
| 3096 | memset(mMixBuffer, 0, mixBufferSize); |
| 3097 | } |
| 3098 | sleepTime = 0; |
| 3099 | writeFrames = mFrameCount; |
| 3100 | } else { |
| 3101 | if (sleepTime == 0) { |
| 3102 | if (mixerStatus == MIXER_TRACKS_ENABLED) { |
| 3103 | sleepTime = activeSleepTime; |
| 3104 | } else { |
| 3105 | sleepTime = idleSleepTime; |
| 3106 | } |
| 3107 | } else if (mBytesWritten != 0) { |
| 3108 | // flush remaining overflow buffers in output tracks |
| 3109 | for (size_t i = 0; i < outputTracks.size(); i++) { |
| 3110 | if (outputTracks[i]->isActive()) { |
| 3111 | sleepTime = 0; |
| 3112 | writeFrames = 0; |
| 3113 | memset(mMixBuffer, 0, mixBufferSize); |
| 3114 | break; |
| 3115 | } |
| 3116 | } |
| 3117 | } |
| 3118 | } |
| 3119 | |
| 3120 | if (mSuspended) { |
Eric Laurent | 25cbe0e | 2010-08-18 18:13:17 -0700 | [diff] [blame] | 3121 | sleepTime = suspendSleepTimeUs(); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3122 | } |
| 3123 | // sleepTime == 0 means we must write to audio hardware |
| 3124 | if (sleepTime == 0) { |
| 3125 | for (size_t i = 0; i < effectChains.size(); i ++) { |
| 3126 | effectChains[i]->process_l(); |
| 3127 | } |
| 3128 | // enable changes in effect chain |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 3129 | unlockEffectChains(effectChains); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3130 | |
| 3131 | standbyTime = systemTime() + kStandbyTimeInNsecs; |
| 3132 | for (size_t i = 0; i < outputTracks.size(); i++) { |
| 3133 | outputTracks[i]->write(mMixBuffer, writeFrames); |
| 3134 | } |
| 3135 | mStandby = false; |
| 3136 | mBytesWritten += mixBufferSize; |
| 3137 | } else { |
| 3138 | // enable changes in effect chain |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 3139 | unlockEffectChains(effectChains); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3140 | usleep(sleepTime); |
| 3141 | } |
| 3142 | |
| 3143 | // finally let go of all our tracks, without the lock held |
| 3144 | // since we can't guarantee the destructors won't acquire that |
| 3145 | // same lock. |
| 3146 | tracksToRemove.clear(); |
| 3147 | outputTracks.clear(); |
| 3148 | |
| 3149 | // Effect chains will be actually deleted here if they were removed from |
| 3150 | // mEffectChains list during mixing or effects processing |
| 3151 | effectChains.clear(); |
| 3152 | } |
| 3153 | |
Eric Laurent | feb0db6 | 2011-07-22 09:04:31 -0700 | [diff] [blame] | 3154 | releaseWakeLock(); |
| 3155 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3156 | return false; |
| 3157 | } |
| 3158 | |
| 3159 | void AudioFlinger::DuplicatingThread::addOutputTrack(MixerThread *thread) |
| 3160 | { |
| 3161 | int frameCount = (3 * mFrameCount * mSampleRate) / thread->sampleRate(); |
| 3162 | OutputTrack *outputTrack = new OutputTrack((ThreadBase *)thread, |
| 3163 | this, |
| 3164 | mSampleRate, |
| 3165 | mFormat, |
Jean-Michel Trivi | 0d255b2 | 2011-05-24 15:53:33 -0700 | [diff] [blame] | 3166 | mChannelMask, |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3167 | frameCount); |
| 3168 | if (outputTrack->cblk() != NULL) { |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 3169 | thread->setStreamVolume(AUDIO_STREAM_CNT, 1.0f); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3170 | mOutputTracks.add(outputTrack); |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 3171 | ALOGV("addOutputTrack() track %p, on thread %p", outputTrack, thread); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3172 | updateWaitTime(); |
| 3173 | } |
| 3174 | } |
| 3175 | |
| 3176 | void AudioFlinger::DuplicatingThread::removeOutputTrack(MixerThread *thread) |
| 3177 | { |
| 3178 | Mutex::Autolock _l(mLock); |
| 3179 | for (size_t i = 0; i < mOutputTracks.size(); i++) { |
| 3180 | if (mOutputTracks[i]->thread() == (ThreadBase *)thread) { |
| 3181 | mOutputTracks[i]->destroy(); |
| 3182 | mOutputTracks.removeAt(i); |
| 3183 | updateWaitTime(); |
| 3184 | return; |
| 3185 | } |
| 3186 | } |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 3187 | ALOGV("removeOutputTrack(): unkonwn thread: %p", thread); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3188 | } |
| 3189 | |
| 3190 | void AudioFlinger::DuplicatingThread::updateWaitTime() |
| 3191 | { |
| 3192 | mWaitTimeMs = UINT_MAX; |
| 3193 | for (size_t i = 0; i < mOutputTracks.size(); i++) { |
| 3194 | sp<ThreadBase> strong = mOutputTracks[i]->thread().promote(); |
| 3195 | if (strong != NULL) { |
| 3196 | uint32_t waitTimeMs = (strong->frameCount() * 2 * 1000) / strong->sampleRate(); |
| 3197 | if (waitTimeMs < mWaitTimeMs) { |
| 3198 | mWaitTimeMs = waitTimeMs; |
| 3199 | } |
| 3200 | } |
| 3201 | } |
| 3202 | } |
| 3203 | |
| 3204 | |
| 3205 | bool AudioFlinger::DuplicatingThread::outputsReady(SortedVector< sp<OutputTrack> > &outputTracks) |
| 3206 | { |
| 3207 | for (size_t i = 0; i < outputTracks.size(); i++) { |
| 3208 | sp <ThreadBase> thread = outputTracks[i]->thread().promote(); |
| 3209 | if (thread == 0) { |
Steve Block | 5ff1dd5 | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 3210 | 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] | 3211 | return false; |
| 3212 | } |
| 3213 | PlaybackThread *playbackThread = (PlaybackThread *)thread.get(); |
| 3214 | if (playbackThread->standby() && !playbackThread->isSuspended()) { |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 3215 | 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] | 3216 | return false; |
| 3217 | } |
| 3218 | } |
| 3219 | return true; |
| 3220 | } |
| 3221 | |
| 3222 | uint32_t AudioFlinger::DuplicatingThread::activeSleepTimeUs() |
| 3223 | { |
| 3224 | return (mWaitTimeMs * 1000) / 2; |
| 3225 | } |
| 3226 | |
| 3227 | // ---------------------------------------------------------------------------- |
| 3228 | |
| 3229 | // TrackBase constructor must be called with AudioFlinger::mLock held |
| 3230 | AudioFlinger::ThreadBase::TrackBase::TrackBase( |
| 3231 | const wp<ThreadBase>& thread, |
| 3232 | const sp<Client>& client, |
| 3233 | uint32_t sampleRate, |
Glenn Kasten | 58f3021 | 2012-01-12 12:27:51 -0800 | [diff] [blame] | 3234 | audio_format_t format, |
Jean-Michel Trivi | 0d255b2 | 2011-05-24 15:53:33 -0700 | [diff] [blame] | 3235 | uint32_t channelMask, |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3236 | int frameCount, |
| 3237 | uint32_t flags, |
| 3238 | const sp<IMemory>& sharedBuffer, |
| 3239 | int sessionId) |
| 3240 | : RefBase(), |
| 3241 | mThread(thread), |
| 3242 | mClient(client), |
| 3243 | mCblk(0), |
| 3244 | mFrameCount(0), |
| 3245 | mState(IDLE), |
| 3246 | mClientTid(-1), |
| 3247 | mFormat(format), |
| 3248 | mFlags(flags & ~SYSTEM_FLAGS_MASK), |
| 3249 | mSessionId(sessionId) |
| 3250 | { |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 3251 | ALOGV_IF(sharedBuffer != 0, "sharedBuffer: %p, size: %d", sharedBuffer->pointer(), sharedBuffer->size()); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3252 | |
Steve Block | b8a8052 | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 3253 | // ALOGD("Creating track with %d buffers @ %d bytes", bufferCount, bufferSize); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3254 | size_t size = sizeof(audio_track_cblk_t); |
Jean-Michel Trivi | 0d255b2 | 2011-05-24 15:53:33 -0700 | [diff] [blame] | 3255 | uint8_t channelCount = popcount(channelMask); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3256 | size_t bufferSize = frameCount*channelCount*sizeof(int16_t); |
| 3257 | if (sharedBuffer == 0) { |
| 3258 | size += bufferSize; |
| 3259 | } |
| 3260 | |
| 3261 | if (client != NULL) { |
| 3262 | mCblkMemory = client->heap()->allocate(size); |
| 3263 | if (mCblkMemory != 0) { |
| 3264 | mCblk = static_cast<audio_track_cblk_t *>(mCblkMemory->pointer()); |
| 3265 | if (mCblk) { // construct the shared structure in-place. |
| 3266 | new(mCblk) audio_track_cblk_t(); |
| 3267 | // clear all buffers |
| 3268 | mCblk->frameCount = frameCount; |
| 3269 | mCblk->sampleRate = sampleRate; |
Jean-Michel Trivi | 0d255b2 | 2011-05-24 15:53:33 -0700 | [diff] [blame] | 3270 | mChannelCount = channelCount; |
| 3271 | mChannelMask = channelMask; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3272 | if (sharedBuffer == 0) { |
| 3273 | mBuffer = (char*)mCblk + sizeof(audio_track_cblk_t); |
| 3274 | memset(mBuffer, 0, frameCount*channelCount*sizeof(int16_t)); |
| 3275 | // Force underrun condition to avoid false underrun callback until first data is |
Eric Laurent | 44d9848 | 2010-09-30 16:12:31 -0700 | [diff] [blame] | 3276 | // written to buffer (other flags are cleared) |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3277 | mCblk->flags = CBLK_UNDERRUN_ON; |
| 3278 | } else { |
| 3279 | mBuffer = sharedBuffer->pointer(); |
| 3280 | } |
| 3281 | mBufferEnd = (uint8_t *)mBuffer + bufferSize; |
| 3282 | } |
| 3283 | } else { |
Steve Block | 29357bc | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 3284 | ALOGE("not enough memory for AudioTrack size=%u", size); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3285 | client->heap()->dump("AudioTrack"); |
| 3286 | return; |
| 3287 | } |
| 3288 | } else { |
| 3289 | mCblk = (audio_track_cblk_t *)(new uint8_t[size]); |
Glenn Kasten | 4a6f028 | 2012-01-06 15:03:11 -0800 | [diff] [blame] | 3290 | // construct the shared structure in-place. |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3291 | new(mCblk) audio_track_cblk_t(); |
| 3292 | // clear all buffers |
| 3293 | mCblk->frameCount = frameCount; |
| 3294 | mCblk->sampleRate = sampleRate; |
Jean-Michel Trivi | 0d255b2 | 2011-05-24 15:53:33 -0700 | [diff] [blame] | 3295 | mChannelCount = channelCount; |
| 3296 | mChannelMask = channelMask; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3297 | mBuffer = (char*)mCblk + sizeof(audio_track_cblk_t); |
| 3298 | memset(mBuffer, 0, frameCount*channelCount*sizeof(int16_t)); |
| 3299 | // Force underrun condition to avoid false underrun callback until first data is |
Eric Laurent | 44d9848 | 2010-09-30 16:12:31 -0700 | [diff] [blame] | 3300 | // written to buffer (other flags are cleared) |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3301 | mCblk->flags = CBLK_UNDERRUN_ON; |
| 3302 | mBufferEnd = (uint8_t *)mBuffer + bufferSize; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3303 | } |
| 3304 | } |
| 3305 | |
| 3306 | AudioFlinger::ThreadBase::TrackBase::~TrackBase() |
| 3307 | { |
| 3308 | if (mCblk) { |
| 3309 | mCblk->~audio_track_cblk_t(); // destroy our shared-structure. |
| 3310 | if (mClient == NULL) { |
| 3311 | delete mCblk; |
| 3312 | } |
| 3313 | } |
| 3314 | mCblkMemory.clear(); // and free the shared memory |
| 3315 | if (mClient != NULL) { |
| 3316 | Mutex::Autolock _l(mClient->audioFlinger()->mLock); |
| 3317 | mClient.clear(); |
| 3318 | } |
| 3319 | } |
| 3320 | |
| 3321 | void AudioFlinger::ThreadBase::TrackBase::releaseBuffer(AudioBufferProvider::Buffer* buffer) |
| 3322 | { |
Glenn Kasten | e0feee3 | 2011-12-13 11:53:26 -0800 | [diff] [blame] | 3323 | buffer->raw = NULL; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3324 | mFrameCount = buffer->frameCount; |
| 3325 | step(); |
| 3326 | buffer->frameCount = 0; |
| 3327 | } |
| 3328 | |
| 3329 | bool AudioFlinger::ThreadBase::TrackBase::step() { |
| 3330 | bool result; |
| 3331 | audio_track_cblk_t* cblk = this->cblk(); |
| 3332 | |
| 3333 | result = cblk->stepServer(mFrameCount); |
| 3334 | if (!result) { |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 3335 | ALOGV("stepServer failed acquiring cblk mutex"); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3336 | mFlags |= STEPSERVER_FAILED; |
| 3337 | } |
| 3338 | return result; |
| 3339 | } |
| 3340 | |
| 3341 | void AudioFlinger::ThreadBase::TrackBase::reset() { |
| 3342 | audio_track_cblk_t* cblk = this->cblk(); |
| 3343 | |
| 3344 | cblk->user = 0; |
| 3345 | cblk->server = 0; |
| 3346 | cblk->userBase = 0; |
| 3347 | cblk->serverBase = 0; |
| 3348 | mFlags &= (uint32_t)(~SYSTEM_FLAGS_MASK); |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 3349 | ALOGV("TrackBase::reset"); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3350 | } |
| 3351 | |
| 3352 | sp<IMemory> AudioFlinger::ThreadBase::TrackBase::getCblk() const |
| 3353 | { |
| 3354 | return mCblkMemory; |
| 3355 | } |
| 3356 | |
| 3357 | int AudioFlinger::ThreadBase::TrackBase::sampleRate() const { |
| 3358 | return (int)mCblk->sampleRate; |
| 3359 | } |
| 3360 | |
| 3361 | int AudioFlinger::ThreadBase::TrackBase::channelCount() const { |
Jean-Michel Trivi | 0d255b2 | 2011-05-24 15:53:33 -0700 | [diff] [blame] | 3362 | return (const int)mChannelCount; |
| 3363 | } |
| 3364 | |
| 3365 | uint32_t AudioFlinger::ThreadBase::TrackBase::channelMask() const { |
| 3366 | return mChannelMask; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3367 | } |
| 3368 | |
| 3369 | void* AudioFlinger::ThreadBase::TrackBase::getBuffer(uint32_t offset, uint32_t frames) const { |
| 3370 | audio_track_cblk_t* cblk = this->cblk(); |
Glenn Kasten | b998065 | 2012-01-11 09:48:27 -0800 | [diff] [blame] | 3371 | size_t frameSize = cblk->frameSize; |
| 3372 | int8_t *bufferStart = (int8_t *)mBuffer + (offset-cblk->serverBase)*frameSize; |
| 3373 | int8_t *bufferEnd = bufferStart + frames * frameSize; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3374 | |
| 3375 | // Check validity of returned pointer in case the track control block would have been corrupted. |
| 3376 | if (bufferStart < mBuffer || bufferStart > bufferEnd || bufferEnd > mBufferEnd || |
Glenn Kasten | b998065 | 2012-01-11 09:48:27 -0800 | [diff] [blame] | 3377 | ((unsigned long)bufferStart & (unsigned long)(frameSize - 1))) { |
Steve Block | 29357bc | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 3378 | 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] | 3379 | server %d, serverBase %d, user %d, userBase %d", |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3380 | bufferStart, bufferEnd, mBuffer, mBufferEnd, |
Jean-Michel Trivi | 0d255b2 | 2011-05-24 15:53:33 -0700 | [diff] [blame] | 3381 | cblk->server, cblk->serverBase, cblk->user, cblk->userBase); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3382 | return 0; |
| 3383 | } |
| 3384 | |
| 3385 | return bufferStart; |
| 3386 | } |
| 3387 | |
| 3388 | // ---------------------------------------------------------------------------- |
| 3389 | |
| 3390 | // Track constructor must be called with AudioFlinger::mLock and ThreadBase::mLock held |
| 3391 | AudioFlinger::PlaybackThread::Track::Track( |
| 3392 | const wp<ThreadBase>& thread, |
| 3393 | const sp<Client>& client, |
Glenn Kasten | fff6d71 | 2012-01-12 16:38:12 -0800 | [diff] [blame] | 3394 | audio_stream_type_t streamType, |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3395 | uint32_t sampleRate, |
Glenn Kasten | 58f3021 | 2012-01-12 12:27:51 -0800 | [diff] [blame] | 3396 | audio_format_t format, |
Jean-Michel Trivi | 0d255b2 | 2011-05-24 15:53:33 -0700 | [diff] [blame] | 3397 | uint32_t channelMask, |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3398 | int frameCount, |
| 3399 | const sp<IMemory>& sharedBuffer, |
| 3400 | int sessionId) |
Jean-Michel Trivi | 0d255b2 | 2011-05-24 15:53:33 -0700 | [diff] [blame] | 3401 | : TrackBase(thread, client, sampleRate, format, channelMask, frameCount, 0, sharedBuffer, sessionId), |
Eric Laurent | 8f45bd7 | 2010-08-31 13:50:07 -0700 | [diff] [blame] | 3402 | mMute(false), mSharedBuffer(sharedBuffer), mName(-1), mMainBuffer(NULL), mAuxBuffer(NULL), |
| 3403 | mAuxEffectId(0), mHasVolumeController(false) |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3404 | { |
| 3405 | if (mCblk != NULL) { |
| 3406 | sp<ThreadBase> baseThread = thread.promote(); |
| 3407 | if (baseThread != 0) { |
| 3408 | PlaybackThread *playbackThread = (PlaybackThread *)baseThread.get(); |
| 3409 | mName = playbackThread->getTrackName_l(); |
| 3410 | mMainBuffer = playbackThread->mixBuffer(); |
| 3411 | } |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 3412 | ALOGV("Track constructor name %d, calling thread %d", mName, IPCThreadState::self()->getCallingPid()); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3413 | if (mName < 0) { |
Steve Block | 29357bc | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 3414 | ALOGE("no more track names available"); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3415 | } |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3416 | mStreamType = streamType; |
| 3417 | // NOTE: audio_track_cblk_t::frameSize for 8 bit PCM data is based on a sample size of |
| 3418 | // 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] | 3419 | 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] | 3420 | } |
| 3421 | } |
| 3422 | |
| 3423 | AudioFlinger::PlaybackThread::Track::~Track() |
| 3424 | { |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 3425 | ALOGV("PlaybackThread::Track destructor"); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3426 | sp<ThreadBase> thread = mThread.promote(); |
| 3427 | if (thread != 0) { |
| 3428 | Mutex::Autolock _l(thread->mLock); |
| 3429 | mState = TERMINATED; |
| 3430 | } |
| 3431 | } |
| 3432 | |
| 3433 | void AudioFlinger::PlaybackThread::Track::destroy() |
| 3434 | { |
| 3435 | // NOTE: destroyTrack_l() can remove a strong reference to this Track |
| 3436 | // by removing it from mTracks vector, so there is a risk that this Tracks's |
| 3437 | // desctructor is called. As the destructor needs to lock mLock, |
| 3438 | // we must acquire a strong reference on this Track before locking mLock |
| 3439 | // here so that the destructor is called only when exiting this function. |
| 3440 | // On the other hand, as long as Track::destroy() is only called by |
| 3441 | // TrackHandle destructor, the TrackHandle still holds a strong ref on |
| 3442 | // this Track with its member mTrack. |
| 3443 | sp<Track> keep(this); |
| 3444 | { // scope for mLock |
| 3445 | sp<ThreadBase> thread = mThread.promote(); |
| 3446 | if (thread != 0) { |
| 3447 | if (!isOutputTrack()) { |
| 3448 | if (mState == ACTIVE || mState == RESUMING) { |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 3449 | AudioSystem::stopOutput(thread->id(), |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 3450 | (audio_stream_type_t)mStreamType, |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 3451 | mSessionId); |
Gloria Wang | 9ee159b | 2011-02-24 14:51:45 -0800 | [diff] [blame] | 3452 | |
| 3453 | // to track the speaker usage |
| 3454 | addBatteryData(IMediaPlayerService::kBatteryDataAudioFlingerStop); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3455 | } |
| 3456 | AudioSystem::releaseOutput(thread->id()); |
| 3457 | } |
| 3458 | Mutex::Autolock _l(thread->mLock); |
| 3459 | PlaybackThread *playbackThread = (PlaybackThread *)thread.get(); |
| 3460 | playbackThread->destroyTrack_l(this); |
| 3461 | } |
| 3462 | } |
| 3463 | } |
| 3464 | |
| 3465 | void AudioFlinger::PlaybackThread::Track::dump(char* buffer, size_t size) |
| 3466 | { |
Glenn Kasten | b1cf75c | 2012-01-17 12:20:54 -0800 | [diff] [blame] | 3467 | uint32_t vlr = mCblk->volumeLR; |
Jean-Michel Trivi | 0d255b2 | 2011-05-24 15:53:33 -0700 | [diff] [blame] | 3468 | 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] | 3469 | mName - AudioMixer::TRACK0, |
| 3470 | (mClient == NULL) ? getpid() : mClient->pid(), |
| 3471 | mStreamType, |
| 3472 | mFormat, |
Jean-Michel Trivi | 0d255b2 | 2011-05-24 15:53:33 -0700 | [diff] [blame] | 3473 | mChannelMask, |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3474 | mSessionId, |
| 3475 | mFrameCount, |
| 3476 | mState, |
| 3477 | mMute, |
| 3478 | mFillingUpStatus, |
| 3479 | mCblk->sampleRate, |
Glenn Kasten | b1cf75c | 2012-01-17 12:20:54 -0800 | [diff] [blame] | 3480 | vlr & 0xFFFF, |
| 3481 | vlr >> 16, |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3482 | mCblk->server, |
| 3483 | mCblk->user, |
| 3484 | (int)mMainBuffer, |
| 3485 | (int)mAuxBuffer); |
| 3486 | } |
| 3487 | |
| 3488 | status_t AudioFlinger::PlaybackThread::Track::getNextBuffer(AudioBufferProvider::Buffer* buffer) |
| 3489 | { |
| 3490 | audio_track_cblk_t* cblk = this->cblk(); |
| 3491 | uint32_t framesReady; |
| 3492 | uint32_t framesReq = buffer->frameCount; |
| 3493 | |
| 3494 | // Check if last stepServer failed, try to step now |
| 3495 | if (mFlags & TrackBase::STEPSERVER_FAILED) { |
| 3496 | if (!step()) goto getNextBuffer_exit; |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 3497 | ALOGV("stepServer recovered"); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3498 | mFlags &= ~TrackBase::STEPSERVER_FAILED; |
| 3499 | } |
| 3500 | |
| 3501 | framesReady = cblk->framesReady(); |
| 3502 | |
Glenn Kasten | f6b1678 | 2011-12-15 09:51:17 -0800 | [diff] [blame] | 3503 | if (CC_LIKELY(framesReady)) { |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3504 | uint32_t s = cblk->server; |
| 3505 | uint32_t bufferEnd = cblk->serverBase + cblk->frameCount; |
| 3506 | |
| 3507 | bufferEnd = (cblk->loopEnd < bufferEnd) ? cblk->loopEnd : bufferEnd; |
| 3508 | if (framesReq > framesReady) { |
| 3509 | framesReq = framesReady; |
| 3510 | } |
| 3511 | if (s + framesReq > bufferEnd) { |
| 3512 | framesReq = bufferEnd - s; |
| 3513 | } |
| 3514 | |
| 3515 | buffer->raw = getBuffer(s, framesReq); |
Glenn Kasten | e0feee3 | 2011-12-13 11:53:26 -0800 | [diff] [blame] | 3516 | if (buffer->raw == NULL) goto getNextBuffer_exit; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3517 | |
| 3518 | buffer->frameCount = framesReq; |
| 3519 | return NO_ERROR; |
| 3520 | } |
| 3521 | |
| 3522 | getNextBuffer_exit: |
Glenn Kasten | e0feee3 | 2011-12-13 11:53:26 -0800 | [diff] [blame] | 3523 | buffer->raw = NULL; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3524 | buffer->frameCount = 0; |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 3525 | 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] | 3526 | return NOT_ENOUGH_DATA; |
| 3527 | } |
| 3528 | |
| 3529 | bool AudioFlinger::PlaybackThread::Track::isReady() const { |
Eric Laurent | af59ce2 | 2010-10-05 14:41:42 -0700 | [diff] [blame] | 3530 | if (mFillingUpStatus != FS_FILLING || isStopped() || isPausing()) return true; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3531 | |
| 3532 | if (mCblk->framesReady() >= mCblk->frameCount || |
| 3533 | (mCblk->flags & CBLK_FORCEREADY_MSK)) { |
| 3534 | mFillingUpStatus = FS_FILLED; |
Eric Laurent | 38ccae2 | 2011-03-28 18:37:07 -0700 | [diff] [blame] | 3535 | android_atomic_and(~CBLK_FORCEREADY_MSK, &mCblk->flags); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3536 | return true; |
| 3537 | } |
| 3538 | return false; |
| 3539 | } |
| 3540 | |
| 3541 | status_t AudioFlinger::PlaybackThread::Track::start() |
| 3542 | { |
| 3543 | status_t status = NO_ERROR; |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 3544 | ALOGV("start(%d), calling thread %d session %d", |
Eric Laurent | f997cab | 2010-07-19 06:24:46 -0700 | [diff] [blame] | 3545 | mName, IPCThreadState::self()->getCallingPid(), mSessionId); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3546 | sp<ThreadBase> thread = mThread.promote(); |
| 3547 | if (thread != 0) { |
| 3548 | Mutex::Autolock _l(thread->mLock); |
| 3549 | int state = mState; |
| 3550 | // here the track could be either new, or restarted |
| 3551 | // in both cases "unstop" the track |
| 3552 | if (mState == PAUSED) { |
| 3553 | mState = TrackBase::RESUMING; |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 3554 | ALOGV("PAUSED => RESUMING (%d) on thread %p", mName, this); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3555 | } else { |
| 3556 | mState = TrackBase::ACTIVE; |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 3557 | ALOGV("? => ACTIVE (%d) on thread %p", mName, this); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3558 | } |
| 3559 | |
| 3560 | if (!isOutputTrack() && state != ACTIVE && state != RESUMING) { |
| 3561 | thread->mLock.unlock(); |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 3562 | status = AudioSystem::startOutput(thread->id(), |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 3563 | (audio_stream_type_t)mStreamType, |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 3564 | mSessionId); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3565 | thread->mLock.lock(); |
Gloria Wang | 9ee159b | 2011-02-24 14:51:45 -0800 | [diff] [blame] | 3566 | |
| 3567 | // to track the speaker usage |
| 3568 | if (status == NO_ERROR) { |
| 3569 | addBatteryData(IMediaPlayerService::kBatteryDataAudioFlingerStart); |
| 3570 | } |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3571 | } |
| 3572 | if (status == NO_ERROR) { |
| 3573 | PlaybackThread *playbackThread = (PlaybackThread *)thread.get(); |
| 3574 | playbackThread->addTrack_l(this); |
| 3575 | } else { |
| 3576 | mState = state; |
| 3577 | } |
| 3578 | } else { |
| 3579 | status = BAD_VALUE; |
| 3580 | } |
| 3581 | return status; |
| 3582 | } |
| 3583 | |
| 3584 | void AudioFlinger::PlaybackThread::Track::stop() |
| 3585 | { |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 3586 | ALOGV("stop(%d), calling thread %d", mName, IPCThreadState::self()->getCallingPid()); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3587 | sp<ThreadBase> thread = mThread.promote(); |
| 3588 | if (thread != 0) { |
| 3589 | Mutex::Autolock _l(thread->mLock); |
| 3590 | int state = mState; |
| 3591 | if (mState > STOPPED) { |
| 3592 | mState = STOPPED; |
| 3593 | // If the track is not active (PAUSED and buffers full), flush buffers |
| 3594 | PlaybackThread *playbackThread = (PlaybackThread *)thread.get(); |
| 3595 | if (playbackThread->mActiveTracks.indexOf(this) < 0) { |
| 3596 | reset(); |
| 3597 | } |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 3598 | ALOGV("(> STOPPED) => STOPPED (%d) on thread %p", mName, playbackThread); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3599 | } |
| 3600 | if (!isOutputTrack() && (state == ACTIVE || state == RESUMING)) { |
| 3601 | thread->mLock.unlock(); |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 3602 | AudioSystem::stopOutput(thread->id(), |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 3603 | (audio_stream_type_t)mStreamType, |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 3604 | mSessionId); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3605 | thread->mLock.lock(); |
Gloria Wang | 9ee159b | 2011-02-24 14:51:45 -0800 | [diff] [blame] | 3606 | |
| 3607 | // to track the speaker usage |
| 3608 | addBatteryData(IMediaPlayerService::kBatteryDataAudioFlingerStop); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3609 | } |
| 3610 | } |
| 3611 | } |
| 3612 | |
| 3613 | void AudioFlinger::PlaybackThread::Track::pause() |
| 3614 | { |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 3615 | ALOGV("pause(%d), calling thread %d", mName, IPCThreadState::self()->getCallingPid()); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3616 | sp<ThreadBase> thread = mThread.promote(); |
| 3617 | if (thread != 0) { |
| 3618 | Mutex::Autolock _l(thread->mLock); |
| 3619 | if (mState == ACTIVE || mState == RESUMING) { |
| 3620 | mState = PAUSING; |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 3621 | ALOGV("ACTIVE/RESUMING => PAUSING (%d) on thread %p", mName, thread.get()); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3622 | if (!isOutputTrack()) { |
| 3623 | thread->mLock.unlock(); |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 3624 | AudioSystem::stopOutput(thread->id(), |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 3625 | (audio_stream_type_t)mStreamType, |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 3626 | mSessionId); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3627 | thread->mLock.lock(); |
Gloria Wang | 9ee159b | 2011-02-24 14:51:45 -0800 | [diff] [blame] | 3628 | |
| 3629 | // to track the speaker usage |
| 3630 | addBatteryData(IMediaPlayerService::kBatteryDataAudioFlingerStop); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3631 | } |
| 3632 | } |
| 3633 | } |
| 3634 | } |
| 3635 | |
| 3636 | void AudioFlinger::PlaybackThread::Track::flush() |
| 3637 | { |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 3638 | ALOGV("flush(%d)", mName); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3639 | sp<ThreadBase> thread = mThread.promote(); |
| 3640 | if (thread != 0) { |
| 3641 | Mutex::Autolock _l(thread->mLock); |
| 3642 | if (mState != STOPPED && mState != PAUSED && mState != PAUSING) { |
| 3643 | return; |
| 3644 | } |
| 3645 | // No point remaining in PAUSED state after a flush => go to |
| 3646 | // STOPPED state |
| 3647 | mState = STOPPED; |
| 3648 | |
Eric Laurent | 38ccae2 | 2011-03-28 18:37:07 -0700 | [diff] [blame] | 3649 | // do not reset the track if it is still in the process of being stopped or paused. |
| 3650 | // this will be done by prepareTracks_l() when the track is stopped. |
| 3651 | PlaybackThread *playbackThread = (PlaybackThread *)thread.get(); |
| 3652 | if (playbackThread->mActiveTracks.indexOf(this) < 0) { |
| 3653 | reset(); |
| 3654 | } |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3655 | } |
| 3656 | } |
| 3657 | |
| 3658 | void AudioFlinger::PlaybackThread::Track::reset() |
| 3659 | { |
| 3660 | // Do not reset twice to avoid discarding data written just after a flush and before |
| 3661 | // the audioflinger thread detects the track is stopped. |
| 3662 | if (!mResetDone) { |
| 3663 | TrackBase::reset(); |
| 3664 | // Force underrun condition to avoid false underrun callback until first data is |
| 3665 | // written to buffer |
Eric Laurent | 38ccae2 | 2011-03-28 18:37:07 -0700 | [diff] [blame] | 3666 | android_atomic_and(~CBLK_FORCEREADY_MSK, &mCblk->flags); |
| 3667 | android_atomic_or(CBLK_UNDERRUN_ON, &mCblk->flags); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3668 | mFillingUpStatus = FS_FILLING; |
| 3669 | mResetDone = true; |
| 3670 | } |
| 3671 | } |
| 3672 | |
| 3673 | void AudioFlinger::PlaybackThread::Track::mute(bool muted) |
| 3674 | { |
| 3675 | mMute = muted; |
| 3676 | } |
| 3677 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3678 | status_t AudioFlinger::PlaybackThread::Track::attachAuxEffect(int EffectId) |
| 3679 | { |
| 3680 | status_t status = DEAD_OBJECT; |
| 3681 | sp<ThreadBase> thread = mThread.promote(); |
| 3682 | if (thread != 0) { |
| 3683 | PlaybackThread *playbackThread = (PlaybackThread *)thread.get(); |
| 3684 | status = playbackThread->attachAuxEffect(this, EffectId); |
| 3685 | } |
| 3686 | return status; |
| 3687 | } |
| 3688 | |
| 3689 | void AudioFlinger::PlaybackThread::Track::setAuxBuffer(int EffectId, int32_t *buffer) |
| 3690 | { |
| 3691 | mAuxEffectId = EffectId; |
| 3692 | mAuxBuffer = buffer; |
| 3693 | } |
| 3694 | |
| 3695 | // ---------------------------------------------------------------------------- |
| 3696 | |
| 3697 | // RecordTrack constructor must be called with AudioFlinger::mLock held |
| 3698 | AudioFlinger::RecordThread::RecordTrack::RecordTrack( |
| 3699 | const wp<ThreadBase>& thread, |
| 3700 | const sp<Client>& client, |
| 3701 | uint32_t sampleRate, |
Glenn Kasten | 58f3021 | 2012-01-12 12:27:51 -0800 | [diff] [blame] | 3702 | audio_format_t format, |
Jean-Michel Trivi | 0d255b2 | 2011-05-24 15:53:33 -0700 | [diff] [blame] | 3703 | uint32_t channelMask, |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3704 | int frameCount, |
| 3705 | uint32_t flags, |
| 3706 | int sessionId) |
| 3707 | : TrackBase(thread, client, sampleRate, format, |
Jean-Michel Trivi | 0d255b2 | 2011-05-24 15:53:33 -0700 | [diff] [blame] | 3708 | channelMask, frameCount, flags, 0, sessionId), |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3709 | mOverflow(false) |
| 3710 | { |
| 3711 | if (mCblk != NULL) { |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 3712 | ALOGV("RecordTrack constructor, size %d", (int)mBufferEnd - (int)mBuffer); |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 3713 | if (format == AUDIO_FORMAT_PCM_16_BIT) { |
Jean-Michel Trivi | 0d255b2 | 2011-05-24 15:53:33 -0700 | [diff] [blame] | 3714 | mCblk->frameSize = mChannelCount * sizeof(int16_t); |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 3715 | } else if (format == AUDIO_FORMAT_PCM_8_BIT) { |
Jean-Michel Trivi | 0d255b2 | 2011-05-24 15:53:33 -0700 | [diff] [blame] | 3716 | mCblk->frameSize = mChannelCount * sizeof(int8_t); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3717 | } else { |
| 3718 | mCblk->frameSize = sizeof(int8_t); |
| 3719 | } |
| 3720 | } |
| 3721 | } |
| 3722 | |
| 3723 | AudioFlinger::RecordThread::RecordTrack::~RecordTrack() |
| 3724 | { |
| 3725 | sp<ThreadBase> thread = mThread.promote(); |
| 3726 | if (thread != 0) { |
| 3727 | AudioSystem::releaseInput(thread->id()); |
| 3728 | } |
| 3729 | } |
| 3730 | |
| 3731 | status_t AudioFlinger::RecordThread::RecordTrack::getNextBuffer(AudioBufferProvider::Buffer* buffer) |
| 3732 | { |
| 3733 | audio_track_cblk_t* cblk = this->cblk(); |
| 3734 | uint32_t framesAvail; |
| 3735 | uint32_t framesReq = buffer->frameCount; |
| 3736 | |
| 3737 | // Check if last stepServer failed, try to step now |
| 3738 | if (mFlags & TrackBase::STEPSERVER_FAILED) { |
| 3739 | if (!step()) goto getNextBuffer_exit; |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 3740 | ALOGV("stepServer recovered"); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3741 | mFlags &= ~TrackBase::STEPSERVER_FAILED; |
| 3742 | } |
| 3743 | |
| 3744 | framesAvail = cblk->framesAvailable_l(); |
| 3745 | |
Glenn Kasten | f6b1678 | 2011-12-15 09:51:17 -0800 | [diff] [blame] | 3746 | if (CC_LIKELY(framesAvail)) { |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3747 | uint32_t s = cblk->server; |
| 3748 | uint32_t bufferEnd = cblk->serverBase + cblk->frameCount; |
| 3749 | |
| 3750 | if (framesReq > framesAvail) { |
| 3751 | framesReq = framesAvail; |
| 3752 | } |
| 3753 | if (s + framesReq > bufferEnd) { |
| 3754 | framesReq = bufferEnd - s; |
| 3755 | } |
| 3756 | |
| 3757 | buffer->raw = getBuffer(s, framesReq); |
Glenn Kasten | e0feee3 | 2011-12-13 11:53:26 -0800 | [diff] [blame] | 3758 | if (buffer->raw == NULL) goto getNextBuffer_exit; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3759 | |
| 3760 | buffer->frameCount = framesReq; |
| 3761 | return NO_ERROR; |
| 3762 | } |
| 3763 | |
| 3764 | getNextBuffer_exit: |
Glenn Kasten | e0feee3 | 2011-12-13 11:53:26 -0800 | [diff] [blame] | 3765 | buffer->raw = NULL; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3766 | buffer->frameCount = 0; |
| 3767 | return NOT_ENOUGH_DATA; |
| 3768 | } |
| 3769 | |
| 3770 | status_t AudioFlinger::RecordThread::RecordTrack::start() |
| 3771 | { |
| 3772 | sp<ThreadBase> thread = mThread.promote(); |
| 3773 | if (thread != 0) { |
| 3774 | RecordThread *recordThread = (RecordThread *)thread.get(); |
| 3775 | return recordThread->start(this); |
| 3776 | } else { |
| 3777 | return BAD_VALUE; |
| 3778 | } |
| 3779 | } |
| 3780 | |
| 3781 | void AudioFlinger::RecordThread::RecordTrack::stop() |
| 3782 | { |
| 3783 | sp<ThreadBase> thread = mThread.promote(); |
| 3784 | if (thread != 0) { |
| 3785 | RecordThread *recordThread = (RecordThread *)thread.get(); |
| 3786 | recordThread->stop(this); |
Eric Laurent | 38ccae2 | 2011-03-28 18:37:07 -0700 | [diff] [blame] | 3787 | TrackBase::reset(); |
| 3788 | // Force overerrun condition to avoid false overrun callback until first data is |
| 3789 | // read from buffer |
| 3790 | android_atomic_or(CBLK_UNDERRUN_ON, &mCblk->flags); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3791 | } |
| 3792 | } |
| 3793 | |
| 3794 | void AudioFlinger::RecordThread::RecordTrack::dump(char* buffer, size_t size) |
| 3795 | { |
Jean-Michel Trivi | 0d255b2 | 2011-05-24 15:53:33 -0700 | [diff] [blame] | 3796 | 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] | 3797 | (mClient == NULL) ? getpid() : mClient->pid(), |
| 3798 | mFormat, |
Jean-Michel Trivi | 0d255b2 | 2011-05-24 15:53:33 -0700 | [diff] [blame] | 3799 | mChannelMask, |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3800 | mSessionId, |
| 3801 | mFrameCount, |
| 3802 | mState, |
| 3803 | mCblk->sampleRate, |
| 3804 | mCblk->server, |
| 3805 | mCblk->user); |
| 3806 | } |
| 3807 | |
| 3808 | |
| 3809 | // ---------------------------------------------------------------------------- |
| 3810 | |
| 3811 | AudioFlinger::PlaybackThread::OutputTrack::OutputTrack( |
| 3812 | const wp<ThreadBase>& thread, |
| 3813 | DuplicatingThread *sourceThread, |
| 3814 | uint32_t sampleRate, |
Glenn Kasten | 58f3021 | 2012-01-12 12:27:51 -0800 | [diff] [blame] | 3815 | audio_format_t format, |
Jean-Michel Trivi | 0d255b2 | 2011-05-24 15:53:33 -0700 | [diff] [blame] | 3816 | uint32_t channelMask, |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3817 | int frameCount) |
Jean-Michel Trivi | 0d255b2 | 2011-05-24 15:53:33 -0700 | [diff] [blame] | 3818 | : Track(thread, NULL, AUDIO_STREAM_CNT, sampleRate, format, channelMask, frameCount, NULL, 0), |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3819 | mActive(false), mSourceThread(sourceThread) |
| 3820 | { |
| 3821 | |
| 3822 | PlaybackThread *playbackThread = (PlaybackThread *)thread.unsafe_get(); |
| 3823 | if (mCblk != NULL) { |
| 3824 | mCblk->flags |= CBLK_DIRECTION_OUT; |
| 3825 | mCblk->buffers = (char*)mCblk + sizeof(audio_track_cblk_t); |
Glenn Kasten | b1cf75c | 2012-01-17 12:20:54 -0800 | [diff] [blame] | 3826 | mCblk->volumeLR = (MAX_GAIN_INT << 16) | MAX_GAIN_INT; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3827 | mOutBuffer.frameCount = 0; |
| 3828 | playbackThread->mTracks.add(this); |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 3829 | ALOGV("OutputTrack constructor mCblk %p, mBuffer %p, mCblk->buffers %p, " \ |
Jean-Michel Trivi | 0d255b2 | 2011-05-24 15:53:33 -0700 | [diff] [blame] | 3830 | "mCblk->frameCount %d, mCblk->sampleRate %d, mChannelMask 0x%08x mBufferEnd %p", |
| 3831 | mCblk, mBuffer, mCblk->buffers, |
| 3832 | mCblk->frameCount, mCblk->sampleRate, mChannelMask, mBufferEnd); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3833 | } else { |
Steve Block | 5ff1dd5 | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 3834 | ALOGW("Error creating output track on thread %p", playbackThread); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3835 | } |
| 3836 | } |
| 3837 | |
| 3838 | AudioFlinger::PlaybackThread::OutputTrack::~OutputTrack() |
| 3839 | { |
| 3840 | clearBufferQueue(); |
| 3841 | } |
| 3842 | |
| 3843 | status_t AudioFlinger::PlaybackThread::OutputTrack::start() |
| 3844 | { |
| 3845 | status_t status = Track::start(); |
| 3846 | if (status != NO_ERROR) { |
| 3847 | return status; |
| 3848 | } |
| 3849 | |
| 3850 | mActive = true; |
| 3851 | mRetryCount = 127; |
| 3852 | return status; |
| 3853 | } |
| 3854 | |
| 3855 | void AudioFlinger::PlaybackThread::OutputTrack::stop() |
| 3856 | { |
| 3857 | Track::stop(); |
| 3858 | clearBufferQueue(); |
| 3859 | mOutBuffer.frameCount = 0; |
| 3860 | mActive = false; |
| 3861 | } |
| 3862 | |
| 3863 | bool AudioFlinger::PlaybackThread::OutputTrack::write(int16_t* data, uint32_t frames) |
| 3864 | { |
| 3865 | Buffer *pInBuffer; |
| 3866 | Buffer inBuffer; |
Jean-Michel Trivi | 0d255b2 | 2011-05-24 15:53:33 -0700 | [diff] [blame] | 3867 | uint32_t channelCount = mChannelCount; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3868 | bool outputBufferFull = false; |
| 3869 | inBuffer.frameCount = frames; |
| 3870 | inBuffer.i16 = data; |
| 3871 | |
| 3872 | uint32_t waitTimeLeftMs = mSourceThread->waitTimeMs(); |
| 3873 | |
| 3874 | if (!mActive && frames != 0) { |
| 3875 | start(); |
| 3876 | sp<ThreadBase> thread = mThread.promote(); |
| 3877 | if (thread != 0) { |
| 3878 | MixerThread *mixerThread = (MixerThread *)thread.get(); |
| 3879 | if (mCblk->frameCount > frames){ |
| 3880 | if (mBufferQueue.size() < kMaxOverFlowBuffers) { |
| 3881 | uint32_t startFrames = (mCblk->frameCount - frames); |
| 3882 | pInBuffer = new Buffer; |
| 3883 | pInBuffer->mBuffer = new int16_t[startFrames * channelCount]; |
| 3884 | pInBuffer->frameCount = startFrames; |
| 3885 | pInBuffer->i16 = pInBuffer->mBuffer; |
| 3886 | memset(pInBuffer->raw, 0, startFrames * channelCount * sizeof(int16_t)); |
| 3887 | mBufferQueue.add(pInBuffer); |
| 3888 | } else { |
Steve Block | 5ff1dd5 | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 3889 | ALOGW ("OutputTrack::write() %p no more buffers in queue", this); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3890 | } |
| 3891 | } |
| 3892 | } |
| 3893 | } |
| 3894 | |
| 3895 | while (waitTimeLeftMs) { |
| 3896 | // First write pending buffers, then new data |
| 3897 | if (mBufferQueue.size()) { |
| 3898 | pInBuffer = mBufferQueue.itemAt(0); |
| 3899 | } else { |
| 3900 | pInBuffer = &inBuffer; |
| 3901 | } |
| 3902 | |
| 3903 | if (pInBuffer->frameCount == 0) { |
| 3904 | break; |
| 3905 | } |
| 3906 | |
| 3907 | if (mOutBuffer.frameCount == 0) { |
| 3908 | mOutBuffer.frameCount = pInBuffer->frameCount; |
| 3909 | nsecs_t startTime = systemTime(); |
Glenn Kasten | 335787f | 2012-01-20 17:00:00 -0800 | [diff] [blame] | 3910 | if (obtainBuffer(&mOutBuffer, waitTimeLeftMs) == (status_t)NO_MORE_BUFFERS) { |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 3911 | 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] | 3912 | outputBufferFull = true; |
| 3913 | break; |
| 3914 | } |
| 3915 | uint32_t waitTimeMs = (uint32_t)ns2ms(systemTime() - startTime); |
| 3916 | if (waitTimeLeftMs >= waitTimeMs) { |
| 3917 | waitTimeLeftMs -= waitTimeMs; |
| 3918 | } else { |
| 3919 | waitTimeLeftMs = 0; |
| 3920 | } |
| 3921 | } |
| 3922 | |
| 3923 | uint32_t outFrames = pInBuffer->frameCount > mOutBuffer.frameCount ? mOutBuffer.frameCount : pInBuffer->frameCount; |
| 3924 | memcpy(mOutBuffer.raw, pInBuffer->raw, outFrames * channelCount * sizeof(int16_t)); |
| 3925 | mCblk->stepUser(outFrames); |
| 3926 | pInBuffer->frameCount -= outFrames; |
| 3927 | pInBuffer->i16 += outFrames * channelCount; |
| 3928 | mOutBuffer.frameCount -= outFrames; |
| 3929 | mOutBuffer.i16 += outFrames * channelCount; |
| 3930 | |
| 3931 | if (pInBuffer->frameCount == 0) { |
| 3932 | if (mBufferQueue.size()) { |
| 3933 | mBufferQueue.removeAt(0); |
| 3934 | delete [] pInBuffer->mBuffer; |
| 3935 | delete pInBuffer; |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 3936 | 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] | 3937 | } else { |
| 3938 | break; |
| 3939 | } |
| 3940 | } |
| 3941 | } |
| 3942 | |
| 3943 | // If we could not write all frames, allocate a buffer and queue it for next time. |
| 3944 | if (inBuffer.frameCount) { |
| 3945 | sp<ThreadBase> thread = mThread.promote(); |
| 3946 | if (thread != 0 && !thread->standby()) { |
| 3947 | if (mBufferQueue.size() < kMaxOverFlowBuffers) { |
| 3948 | pInBuffer = new Buffer; |
| 3949 | pInBuffer->mBuffer = new int16_t[inBuffer.frameCount * channelCount]; |
| 3950 | pInBuffer->frameCount = inBuffer.frameCount; |
| 3951 | pInBuffer->i16 = pInBuffer->mBuffer; |
| 3952 | memcpy(pInBuffer->raw, inBuffer.raw, inBuffer.frameCount * channelCount * sizeof(int16_t)); |
| 3953 | mBufferQueue.add(pInBuffer); |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 3954 | 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] | 3955 | } else { |
Steve Block | 5ff1dd5 | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 3956 | 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] | 3957 | } |
| 3958 | } |
| 3959 | } |
| 3960 | |
| 3961 | // Calling write() with a 0 length buffer, means that no more data will be written: |
| 3962 | // If no more buffers are pending, fill output track buffer to make sure it is started |
| 3963 | // by output mixer. |
| 3964 | if (frames == 0 && mBufferQueue.size() == 0) { |
| 3965 | if (mCblk->user < mCblk->frameCount) { |
| 3966 | frames = mCblk->frameCount - mCblk->user; |
| 3967 | pInBuffer = new Buffer; |
| 3968 | pInBuffer->mBuffer = new int16_t[frames * channelCount]; |
| 3969 | pInBuffer->frameCount = frames; |
| 3970 | pInBuffer->i16 = pInBuffer->mBuffer; |
| 3971 | memset(pInBuffer->raw, 0, frames * channelCount * sizeof(int16_t)); |
| 3972 | mBufferQueue.add(pInBuffer); |
| 3973 | } else if (mActive) { |
| 3974 | stop(); |
| 3975 | } |
| 3976 | } |
| 3977 | |
| 3978 | return outputBufferFull; |
| 3979 | } |
| 3980 | |
| 3981 | status_t AudioFlinger::PlaybackThread::OutputTrack::obtainBuffer(AudioBufferProvider::Buffer* buffer, uint32_t waitTimeMs) |
| 3982 | { |
| 3983 | int active; |
| 3984 | status_t result; |
| 3985 | audio_track_cblk_t* cblk = mCblk; |
| 3986 | uint32_t framesReq = buffer->frameCount; |
| 3987 | |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 3988 | // ALOGV("OutputTrack::obtainBuffer user %d, server %d", cblk->user, cblk->server); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3989 | buffer->frameCount = 0; |
| 3990 | |
| 3991 | uint32_t framesAvail = cblk->framesAvailable(); |
| 3992 | |
| 3993 | |
| 3994 | if (framesAvail == 0) { |
| 3995 | Mutex::Autolock _l(cblk->lock); |
| 3996 | goto start_loop_here; |
| 3997 | while (framesAvail == 0) { |
| 3998 | active = mActive; |
Glenn Kasten | f6b1678 | 2011-12-15 09:51:17 -0800 | [diff] [blame] | 3999 | if (CC_UNLIKELY(!active)) { |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 4000 | ALOGV("Not active and NO_MORE_BUFFERS"); |
Glenn Kasten | 335787f | 2012-01-20 17:00:00 -0800 | [diff] [blame] | 4001 | return NO_MORE_BUFFERS; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4002 | } |
| 4003 | result = cblk->cv.waitRelative(cblk->lock, milliseconds(waitTimeMs)); |
| 4004 | if (result != NO_ERROR) { |
Glenn Kasten | 335787f | 2012-01-20 17:00:00 -0800 | [diff] [blame] | 4005 | return NO_MORE_BUFFERS; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4006 | } |
| 4007 | // read the server count again |
| 4008 | start_loop_here: |
| 4009 | framesAvail = cblk->framesAvailable_l(); |
| 4010 | } |
| 4011 | } |
| 4012 | |
| 4013 | // if (framesAvail < framesReq) { |
Glenn Kasten | 335787f | 2012-01-20 17:00:00 -0800 | [diff] [blame] | 4014 | // return NO_MORE_BUFFERS; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4015 | // } |
| 4016 | |
| 4017 | if (framesReq > framesAvail) { |
| 4018 | framesReq = framesAvail; |
| 4019 | } |
| 4020 | |
| 4021 | uint32_t u = cblk->user; |
| 4022 | uint32_t bufferEnd = cblk->userBase + cblk->frameCount; |
| 4023 | |
| 4024 | if (u + framesReq > bufferEnd) { |
| 4025 | framesReq = bufferEnd - u; |
| 4026 | } |
| 4027 | |
| 4028 | buffer->frameCount = framesReq; |
| 4029 | buffer->raw = (void *)cblk->buffer(u); |
| 4030 | return NO_ERROR; |
| 4031 | } |
| 4032 | |
| 4033 | |
| 4034 | void AudioFlinger::PlaybackThread::OutputTrack::clearBufferQueue() |
| 4035 | { |
| 4036 | size_t size = mBufferQueue.size(); |
| 4037 | Buffer *pBuffer; |
| 4038 | |
| 4039 | for (size_t i = 0; i < size; i++) { |
| 4040 | pBuffer = mBufferQueue.itemAt(i); |
| 4041 | delete [] pBuffer->mBuffer; |
| 4042 | delete pBuffer; |
| 4043 | } |
| 4044 | mBufferQueue.clear(); |
| 4045 | } |
| 4046 | |
| 4047 | // ---------------------------------------------------------------------------- |
| 4048 | |
| 4049 | AudioFlinger::Client::Client(const sp<AudioFlinger>& audioFlinger, pid_t pid) |
| 4050 | : RefBase(), |
| 4051 | mAudioFlinger(audioFlinger), |
| 4052 | mMemoryDealer(new MemoryDealer(1024*1024, "AudioFlinger::Client")), |
| 4053 | mPid(pid) |
| 4054 | { |
| 4055 | // 1 MB of address space is good for 32 tracks, 8 buffers each, 4 KB/buffer |
| 4056 | } |
| 4057 | |
| 4058 | // Client destructor must be called with AudioFlinger::mLock held |
| 4059 | AudioFlinger::Client::~Client() |
| 4060 | { |
| 4061 | mAudioFlinger->removeClient_l(mPid); |
| 4062 | } |
| 4063 | |
| 4064 | const sp<MemoryDealer>& AudioFlinger::Client::heap() const |
| 4065 | { |
| 4066 | return mMemoryDealer; |
| 4067 | } |
| 4068 | |
| 4069 | // ---------------------------------------------------------------------------- |
| 4070 | |
| 4071 | AudioFlinger::NotificationClient::NotificationClient(const sp<AudioFlinger>& audioFlinger, |
| 4072 | const sp<IAudioFlingerClient>& client, |
| 4073 | pid_t pid) |
| 4074 | : mAudioFlinger(audioFlinger), mPid(pid), mClient(client) |
| 4075 | { |
| 4076 | } |
| 4077 | |
| 4078 | AudioFlinger::NotificationClient::~NotificationClient() |
| 4079 | { |
| 4080 | mClient.clear(); |
| 4081 | } |
| 4082 | |
| 4083 | void AudioFlinger::NotificationClient::binderDied(const wp<IBinder>& who) |
| 4084 | { |
| 4085 | sp<NotificationClient> keep(this); |
| 4086 | { |
| 4087 | mAudioFlinger->removeNotificationClient(mPid); |
| 4088 | } |
| 4089 | } |
| 4090 | |
| 4091 | // ---------------------------------------------------------------------------- |
| 4092 | |
| 4093 | AudioFlinger::TrackHandle::TrackHandle(const sp<AudioFlinger::PlaybackThread::Track>& track) |
| 4094 | : BnAudioTrack(), |
| 4095 | mTrack(track) |
| 4096 | { |
| 4097 | } |
| 4098 | |
| 4099 | AudioFlinger::TrackHandle::~TrackHandle() { |
| 4100 | // just stop the track on deletion, associated resources |
| 4101 | // will be freed from the main thread once all pending buffers have |
| 4102 | // been played. Unless it's not in the active track list, in which |
| 4103 | // case we free everything now... |
| 4104 | mTrack->destroy(); |
| 4105 | } |
| 4106 | |
Glenn Kasten | 90716c5 | 2012-01-26 13:40:12 -0800 | [diff] [blame^] | 4107 | sp<IMemory> AudioFlinger::TrackHandle::getCblk() const { |
| 4108 | return mTrack->getCblk(); |
| 4109 | } |
| 4110 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4111 | status_t AudioFlinger::TrackHandle::start() { |
| 4112 | return mTrack->start(); |
| 4113 | } |
| 4114 | |
| 4115 | void AudioFlinger::TrackHandle::stop() { |
| 4116 | mTrack->stop(); |
| 4117 | } |
| 4118 | |
| 4119 | void AudioFlinger::TrackHandle::flush() { |
| 4120 | mTrack->flush(); |
| 4121 | } |
| 4122 | |
| 4123 | void AudioFlinger::TrackHandle::mute(bool e) { |
| 4124 | mTrack->mute(e); |
| 4125 | } |
| 4126 | |
| 4127 | void AudioFlinger::TrackHandle::pause() { |
| 4128 | mTrack->pause(); |
| 4129 | } |
| 4130 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4131 | status_t AudioFlinger::TrackHandle::attachAuxEffect(int EffectId) |
| 4132 | { |
| 4133 | return mTrack->attachAuxEffect(EffectId); |
| 4134 | } |
| 4135 | |
| 4136 | status_t AudioFlinger::TrackHandle::onTransact( |
| 4137 | uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags) |
| 4138 | { |
| 4139 | return BnAudioTrack::onTransact(code, data, reply, flags); |
| 4140 | } |
| 4141 | |
| 4142 | // ---------------------------------------------------------------------------- |
| 4143 | |
| 4144 | sp<IAudioRecord> AudioFlinger::openRecord( |
| 4145 | pid_t pid, |
| 4146 | int input, |
| 4147 | uint32_t sampleRate, |
Glenn Kasten | 58f3021 | 2012-01-12 12:27:51 -0800 | [diff] [blame] | 4148 | audio_format_t format, |
Jean-Michel Trivi | 0d255b2 | 2011-05-24 15:53:33 -0700 | [diff] [blame] | 4149 | uint32_t channelMask, |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4150 | int frameCount, |
| 4151 | uint32_t flags, |
| 4152 | int *sessionId, |
| 4153 | status_t *status) |
| 4154 | { |
| 4155 | sp<RecordThread::RecordTrack> recordTrack; |
| 4156 | sp<RecordHandle> recordHandle; |
| 4157 | sp<Client> client; |
| 4158 | wp<Client> wclient; |
| 4159 | status_t lStatus; |
| 4160 | RecordThread *thread; |
| 4161 | size_t inFrameCount; |
| 4162 | int lSessionId; |
| 4163 | |
| 4164 | // check calling permissions |
| 4165 | if (!recordingAllowed()) { |
| 4166 | lStatus = PERMISSION_DENIED; |
| 4167 | goto Exit; |
| 4168 | } |
| 4169 | |
| 4170 | // add client to list |
| 4171 | { // scope for mLock |
| 4172 | Mutex::Autolock _l(mLock); |
| 4173 | thread = checkRecordThread_l(input); |
| 4174 | if (thread == NULL) { |
| 4175 | lStatus = BAD_VALUE; |
| 4176 | goto Exit; |
| 4177 | } |
| 4178 | |
| 4179 | wclient = mClients.valueFor(pid); |
| 4180 | if (wclient != NULL) { |
| 4181 | client = wclient.promote(); |
| 4182 | } else { |
| 4183 | client = new Client(this, pid); |
| 4184 | mClients.add(pid, client); |
| 4185 | } |
| 4186 | |
| 4187 | // If no audio session id is provided, create one here |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 4188 | if (sessionId != NULL && *sessionId != AUDIO_SESSION_OUTPUT_MIX) { |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4189 | lSessionId = *sessionId; |
| 4190 | } else { |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 4191 | lSessionId = nextUniqueId(); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4192 | if (sessionId != NULL) { |
| 4193 | *sessionId = lSessionId; |
| 4194 | } |
| 4195 | } |
| 4196 | // 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] | 4197 | recordTrack = thread->createRecordTrack_l(client, |
| 4198 | sampleRate, |
| 4199 | format, |
| 4200 | channelMask, |
| 4201 | frameCount, |
| 4202 | flags, |
| 4203 | lSessionId, |
| 4204 | &lStatus); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4205 | } |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 4206 | if (lStatus != NO_ERROR) { |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4207 | // remove local strong reference to Client before deleting the RecordTrack so that the Client |
| 4208 | // destructor is called by the TrackBase destructor with mLock held |
| 4209 | client.clear(); |
| 4210 | recordTrack.clear(); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4211 | goto Exit; |
| 4212 | } |
| 4213 | |
| 4214 | // return to handle to client |
| 4215 | recordHandle = new RecordHandle(recordTrack); |
| 4216 | lStatus = NO_ERROR; |
| 4217 | |
| 4218 | Exit: |
| 4219 | if (status) { |
| 4220 | *status = lStatus; |
| 4221 | } |
| 4222 | return recordHandle; |
| 4223 | } |
| 4224 | |
| 4225 | // ---------------------------------------------------------------------------- |
| 4226 | |
| 4227 | AudioFlinger::RecordHandle::RecordHandle(const sp<AudioFlinger::RecordThread::RecordTrack>& recordTrack) |
| 4228 | : BnAudioRecord(), |
| 4229 | mRecordTrack(recordTrack) |
| 4230 | { |
| 4231 | } |
| 4232 | |
| 4233 | AudioFlinger::RecordHandle::~RecordHandle() { |
| 4234 | stop(); |
| 4235 | } |
| 4236 | |
Glenn Kasten | 90716c5 | 2012-01-26 13:40:12 -0800 | [diff] [blame^] | 4237 | sp<IMemory> AudioFlinger::RecordHandle::getCblk() const { |
| 4238 | return mRecordTrack->getCblk(); |
| 4239 | } |
| 4240 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4241 | status_t AudioFlinger::RecordHandle::start() { |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 4242 | ALOGV("RecordHandle::start()"); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4243 | return mRecordTrack->start(); |
| 4244 | } |
| 4245 | |
| 4246 | void AudioFlinger::RecordHandle::stop() { |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 4247 | ALOGV("RecordHandle::stop()"); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4248 | mRecordTrack->stop(); |
| 4249 | } |
| 4250 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4251 | status_t AudioFlinger::RecordHandle::onTransact( |
| 4252 | uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags) |
| 4253 | { |
| 4254 | return BnAudioRecord::onTransact(code, data, reply, flags); |
| 4255 | } |
| 4256 | |
| 4257 | // ---------------------------------------------------------------------------- |
| 4258 | |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 4259 | AudioFlinger::RecordThread::RecordThread(const sp<AudioFlinger>& audioFlinger, |
| 4260 | AudioStreamIn *input, |
| 4261 | uint32_t sampleRate, |
| 4262 | uint32_t channels, |
| 4263 | int id, |
| 4264 | uint32_t device) : |
| 4265 | ThreadBase(audioFlinger, id, device), |
Glenn Kasten | e0feee3 | 2011-12-13 11:53:26 -0800 | [diff] [blame] | 4266 | mInput(input), mTrack(NULL), mResampler(NULL), mRsmpOutBuffer(NULL), mRsmpInBuffer(NULL) |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4267 | { |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 4268 | mType = ThreadBase::RECORD; |
Eric Laurent | feb0db6 | 2011-07-22 09:04:31 -0700 | [diff] [blame] | 4269 | |
| 4270 | snprintf(mName, kNameLength, "AudioIn_%d", id); |
| 4271 | |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 4272 | mReqChannelCount = popcount(channels); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4273 | mReqSampleRate = sampleRate; |
| 4274 | readInputParameters(); |
| 4275 | } |
| 4276 | |
| 4277 | |
| 4278 | AudioFlinger::RecordThread::~RecordThread() |
| 4279 | { |
| 4280 | delete[] mRsmpInBuffer; |
Glenn Kasten | e0feee3 | 2011-12-13 11:53:26 -0800 | [diff] [blame] | 4281 | if (mResampler != NULL) { |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4282 | delete mResampler; |
| 4283 | delete[] mRsmpOutBuffer; |
| 4284 | } |
| 4285 | } |
| 4286 | |
| 4287 | void AudioFlinger::RecordThread::onFirstRef() |
| 4288 | { |
Eric Laurent | feb0db6 | 2011-07-22 09:04:31 -0700 | [diff] [blame] | 4289 | run(mName, PRIORITY_URGENT_AUDIO); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4290 | } |
| 4291 | |
Eric Laurent | b8ba0a9 | 2011-08-07 16:32:26 -0700 | [diff] [blame] | 4292 | status_t AudioFlinger::RecordThread::readyToRun() |
| 4293 | { |
| 4294 | status_t status = initCheck(); |
Steve Block | 5ff1dd5 | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 4295 | ALOGW_IF(status != NO_ERROR,"RecordThread %p could not initialize", this); |
Eric Laurent | b8ba0a9 | 2011-08-07 16:32:26 -0700 | [diff] [blame] | 4296 | return status; |
| 4297 | } |
| 4298 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4299 | bool AudioFlinger::RecordThread::threadLoop() |
| 4300 | { |
| 4301 | AudioBufferProvider::Buffer buffer; |
| 4302 | sp<RecordTrack> activeTrack; |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 4303 | Vector< sp<EffectChain> > effectChains; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4304 | |
Eric Laurent | 44d9848 | 2010-09-30 16:12:31 -0700 | [diff] [blame] | 4305 | nsecs_t lastWarning = 0; |
| 4306 | |
Eric Laurent | feb0db6 | 2011-07-22 09:04:31 -0700 | [diff] [blame] | 4307 | acquireWakeLock(); |
| 4308 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4309 | // start recording |
| 4310 | while (!exitPending()) { |
| 4311 | |
| 4312 | processConfigEvents(); |
| 4313 | |
| 4314 | { // scope for mLock |
| 4315 | Mutex::Autolock _l(mLock); |
| 4316 | checkForNewParameters_l(); |
| 4317 | if (mActiveTrack == 0 && mConfigEvents.isEmpty()) { |
| 4318 | if (!mStandby) { |
Dima Zavin | 799a70e | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 4319 | mInput->stream->common.standby(&mInput->stream->common); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4320 | mStandby = true; |
| 4321 | } |
| 4322 | |
| 4323 | if (exitPending()) break; |
| 4324 | |
Eric Laurent | feb0db6 | 2011-07-22 09:04:31 -0700 | [diff] [blame] | 4325 | releaseWakeLock_l(); |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 4326 | ALOGV("RecordThread: loop stopping"); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4327 | // go to sleep |
| 4328 | mWaitWorkCV.wait(mLock); |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 4329 | ALOGV("RecordThread: loop starting"); |
Eric Laurent | feb0db6 | 2011-07-22 09:04:31 -0700 | [diff] [blame] | 4330 | acquireWakeLock_l(); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4331 | continue; |
| 4332 | } |
| 4333 | if (mActiveTrack != 0) { |
| 4334 | if (mActiveTrack->mState == TrackBase::PAUSING) { |
| 4335 | if (!mStandby) { |
Dima Zavin | 799a70e | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 4336 | mInput->stream->common.standby(&mInput->stream->common); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4337 | mStandby = true; |
| 4338 | } |
| 4339 | mActiveTrack.clear(); |
| 4340 | mStartStopCond.broadcast(); |
| 4341 | } else if (mActiveTrack->mState == TrackBase::RESUMING) { |
| 4342 | if (mReqChannelCount != mActiveTrack->channelCount()) { |
| 4343 | mActiveTrack.clear(); |
| 4344 | mStartStopCond.broadcast(); |
| 4345 | } else if (mBytesRead != 0) { |
| 4346 | // record start succeeds only if first read from audio input |
| 4347 | // succeeds |
| 4348 | if (mBytesRead > 0) { |
| 4349 | mActiveTrack->mState = TrackBase::ACTIVE; |
| 4350 | } else { |
| 4351 | mActiveTrack.clear(); |
| 4352 | } |
| 4353 | mStartStopCond.broadcast(); |
| 4354 | } |
| 4355 | mStandby = false; |
| 4356 | } |
| 4357 | } |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 4358 | lockEffectChains_l(effectChains); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4359 | } |
| 4360 | |
| 4361 | if (mActiveTrack != 0) { |
| 4362 | if (mActiveTrack->mState != TrackBase::ACTIVE && |
| 4363 | mActiveTrack->mState != TrackBase::RESUMING) { |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 4364 | unlockEffectChains(effectChains); |
| 4365 | usleep(kRecordThreadSleepUs); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4366 | continue; |
| 4367 | } |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 4368 | for (size_t i = 0; i < effectChains.size(); i ++) { |
| 4369 | effectChains[i]->process_l(); |
| 4370 | } |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 4371 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4372 | buffer.frameCount = mFrameCount; |
Glenn Kasten | f6b1678 | 2011-12-15 09:51:17 -0800 | [diff] [blame] | 4373 | if (CC_LIKELY(mActiveTrack->getNextBuffer(&buffer) == NO_ERROR)) { |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4374 | size_t framesOut = buffer.frameCount; |
Glenn Kasten | e0feee3 | 2011-12-13 11:53:26 -0800 | [diff] [blame] | 4375 | if (mResampler == NULL) { |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4376 | // no resampling |
| 4377 | while (framesOut) { |
| 4378 | size_t framesIn = mFrameCount - mRsmpInIndex; |
| 4379 | if (framesIn) { |
| 4380 | int8_t *src = (int8_t *)mRsmpInBuffer + mRsmpInIndex * mFrameSize; |
| 4381 | int8_t *dst = buffer.i8 + (buffer.frameCount - framesOut) * mActiveTrack->mCblk->frameSize; |
| 4382 | if (framesIn > framesOut) |
| 4383 | framesIn = framesOut; |
| 4384 | mRsmpInIndex += framesIn; |
| 4385 | framesOut -= framesIn; |
| 4386 | if ((int)mChannelCount == mReqChannelCount || |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 4387 | mFormat != AUDIO_FORMAT_PCM_16_BIT) { |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4388 | memcpy(dst, src, framesIn * mFrameSize); |
| 4389 | } else { |
| 4390 | int16_t *src16 = (int16_t *)src; |
| 4391 | int16_t *dst16 = (int16_t *)dst; |
| 4392 | if (mChannelCount == 1) { |
| 4393 | while (framesIn--) { |
| 4394 | *dst16++ = *src16; |
| 4395 | *dst16++ = *src16++; |
| 4396 | } |
| 4397 | } else { |
| 4398 | while (framesIn--) { |
| 4399 | *dst16++ = (int16_t)(((int32_t)*src16 + (int32_t)*(src16 + 1)) >> 1); |
| 4400 | src16 += 2; |
| 4401 | } |
| 4402 | } |
| 4403 | } |
| 4404 | } |
| 4405 | if (framesOut && mFrameCount == mRsmpInIndex) { |
| 4406 | if (framesOut == mFrameCount && |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 4407 | ((int)mChannelCount == mReqChannelCount || mFormat != AUDIO_FORMAT_PCM_16_BIT)) { |
Dima Zavin | 799a70e | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 4408 | mBytesRead = mInput->stream->read(mInput->stream, buffer.raw, mInputBytes); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4409 | framesOut = 0; |
| 4410 | } else { |
Dima Zavin | 799a70e | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 4411 | mBytesRead = mInput->stream->read(mInput->stream, mRsmpInBuffer, mInputBytes); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4412 | mRsmpInIndex = 0; |
| 4413 | } |
| 4414 | if (mBytesRead < 0) { |
Steve Block | 29357bc | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 4415 | ALOGE("Error reading audio input"); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4416 | if (mActiveTrack->mState == TrackBase::ACTIVE) { |
| 4417 | // Force input into standby so that it tries to |
| 4418 | // recover at next read attempt |
Dima Zavin | 799a70e | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 4419 | mInput->stream->common.standby(&mInput->stream->common); |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 4420 | usleep(kRecordThreadSleepUs); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4421 | } |
| 4422 | mRsmpInIndex = mFrameCount; |
| 4423 | framesOut = 0; |
| 4424 | buffer.frameCount = 0; |
| 4425 | } |
| 4426 | } |
| 4427 | } |
| 4428 | } else { |
| 4429 | // resampling |
| 4430 | |
| 4431 | memset(mRsmpOutBuffer, 0, framesOut * 2 * sizeof(int32_t)); |
| 4432 | // alter output frame count as if we were expecting stereo samples |
| 4433 | if (mChannelCount == 1 && mReqChannelCount == 1) { |
| 4434 | framesOut >>= 1; |
| 4435 | } |
| 4436 | mResampler->resample(mRsmpOutBuffer, framesOut, this); |
| 4437 | // ditherAndClamp() works as long as all buffers returned by mActiveTrack->getNextBuffer() |
| 4438 | // are 32 bit aligned which should be always true. |
| 4439 | if (mChannelCount == 2 && mReqChannelCount == 1) { |
Glenn Kasten | 3b21c50 | 2011-12-15 09:52:39 -0800 | [diff] [blame] | 4440 | ditherAndClamp(mRsmpOutBuffer, mRsmpOutBuffer, framesOut); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4441 | // the resampler always outputs stereo samples: do post stereo to mono conversion |
| 4442 | int16_t *src = (int16_t *)mRsmpOutBuffer; |
| 4443 | int16_t *dst = buffer.i16; |
| 4444 | while (framesOut--) { |
| 4445 | *dst++ = (int16_t)(((int32_t)*src + (int32_t)*(src + 1)) >> 1); |
| 4446 | src += 2; |
| 4447 | } |
| 4448 | } else { |
Glenn Kasten | 3b21c50 | 2011-12-15 09:52:39 -0800 | [diff] [blame] | 4449 | ditherAndClamp((int32_t *)buffer.raw, mRsmpOutBuffer, framesOut); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4450 | } |
| 4451 | |
| 4452 | } |
| 4453 | mActiveTrack->releaseBuffer(&buffer); |
| 4454 | mActiveTrack->overflow(); |
| 4455 | } |
| 4456 | // client isn't retrieving buffers fast enough |
| 4457 | else { |
Eric Laurent | 44d9848 | 2010-09-30 16:12:31 -0700 | [diff] [blame] | 4458 | if (!mActiveTrack->setOverflow()) { |
| 4459 | nsecs_t now = systemTime(); |
Glenn Kasten | 7dede87 | 2011-12-13 11:04:14 -0800 | [diff] [blame] | 4460 | if ((now - lastWarning) > kWarningThrottleNs) { |
Steve Block | 5ff1dd5 | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 4461 | ALOGW("RecordThread: buffer overflow"); |
Eric Laurent | 44d9848 | 2010-09-30 16:12:31 -0700 | [diff] [blame] | 4462 | lastWarning = now; |
| 4463 | } |
| 4464 | } |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4465 | // Release the processor for a while before asking for a new buffer. |
| 4466 | // This will give the application more chance to read from the buffer and |
| 4467 | // clear the overflow. |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 4468 | usleep(kRecordThreadSleepUs); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4469 | } |
| 4470 | } |
Eric Laurent | ec437d8 | 2011-07-26 20:54:46 -0700 | [diff] [blame] | 4471 | // enable changes in effect chain |
| 4472 | unlockEffectChains(effectChains); |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 4473 | effectChains.clear(); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4474 | } |
| 4475 | |
| 4476 | if (!mStandby) { |
Dima Zavin | 799a70e | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 4477 | mInput->stream->common.standby(&mInput->stream->common); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4478 | } |
| 4479 | mActiveTrack.clear(); |
| 4480 | |
| 4481 | mStartStopCond.broadcast(); |
| 4482 | |
Eric Laurent | feb0db6 | 2011-07-22 09:04:31 -0700 | [diff] [blame] | 4483 | releaseWakeLock(); |
| 4484 | |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 4485 | ALOGV("RecordThread %p exiting", this); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4486 | return false; |
| 4487 | } |
| 4488 | |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 4489 | |
| 4490 | sp<AudioFlinger::RecordThread::RecordTrack> AudioFlinger::RecordThread::createRecordTrack_l( |
| 4491 | const sp<AudioFlinger::Client>& client, |
| 4492 | uint32_t sampleRate, |
Glenn Kasten | 58f3021 | 2012-01-12 12:27:51 -0800 | [diff] [blame] | 4493 | audio_format_t format, |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 4494 | int channelMask, |
| 4495 | int frameCount, |
| 4496 | uint32_t flags, |
| 4497 | int sessionId, |
| 4498 | status_t *status) |
| 4499 | { |
| 4500 | sp<RecordTrack> track; |
| 4501 | status_t lStatus; |
| 4502 | |
| 4503 | lStatus = initCheck(); |
| 4504 | if (lStatus != NO_ERROR) { |
Steve Block | 29357bc | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 4505 | ALOGE("Audio driver not initialized."); |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 4506 | goto Exit; |
| 4507 | } |
| 4508 | |
| 4509 | { // scope for mLock |
| 4510 | Mutex::Autolock _l(mLock); |
| 4511 | |
| 4512 | track = new RecordTrack(this, client, sampleRate, |
| 4513 | format, channelMask, frameCount, flags, sessionId); |
| 4514 | |
| 4515 | if (track->getCblk() == NULL) { |
| 4516 | lStatus = NO_MEMORY; |
| 4517 | goto Exit; |
| 4518 | } |
| 4519 | |
| 4520 | mTrack = track.get(); |
Eric Laurent | 59bd0da | 2011-08-01 09:52:20 -0700 | [diff] [blame] | 4521 | // disable AEC and NS if the device is a BT SCO headset supporting those pre processings |
| 4522 | bool suspend = audio_is_bluetooth_sco_device( |
Eric Laurent | bee5337 | 2011-08-29 12:42:48 -0700 | [diff] [blame] | 4523 | (audio_devices_t)(mDevice & AUDIO_DEVICE_IN_ALL)) && mAudioFlinger->btNrecIsOff(); |
Eric Laurent | 59bd0da | 2011-08-01 09:52:20 -0700 | [diff] [blame] | 4524 | setEffectSuspended_l(FX_IID_AEC, suspend, sessionId); |
| 4525 | setEffectSuspended_l(FX_IID_NS, suspend, sessionId); |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 4526 | } |
| 4527 | lStatus = NO_ERROR; |
| 4528 | |
| 4529 | Exit: |
| 4530 | if (status) { |
| 4531 | *status = lStatus; |
| 4532 | } |
| 4533 | return track; |
| 4534 | } |
| 4535 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4536 | status_t AudioFlinger::RecordThread::start(RecordThread::RecordTrack* recordTrack) |
| 4537 | { |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 4538 | ALOGV("RecordThread::start"); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4539 | sp <ThreadBase> strongMe = this; |
| 4540 | status_t status = NO_ERROR; |
| 4541 | { |
Glenn Kasten | a7d8d6f | 2012-01-05 15:41:56 -0800 | [diff] [blame] | 4542 | AutoMutex lock(mLock); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4543 | if (mActiveTrack != 0) { |
| 4544 | if (recordTrack != mActiveTrack.get()) { |
| 4545 | status = -EBUSY; |
| 4546 | } else if (mActiveTrack->mState == TrackBase::PAUSING) { |
| 4547 | mActiveTrack->mState = TrackBase::ACTIVE; |
| 4548 | } |
| 4549 | return status; |
| 4550 | } |
| 4551 | |
| 4552 | recordTrack->mState = TrackBase::IDLE; |
| 4553 | mActiveTrack = recordTrack; |
| 4554 | mLock.unlock(); |
| 4555 | status_t status = AudioSystem::startInput(mId); |
| 4556 | mLock.lock(); |
| 4557 | if (status != NO_ERROR) { |
| 4558 | mActiveTrack.clear(); |
| 4559 | return status; |
| 4560 | } |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4561 | mRsmpInIndex = mFrameCount; |
| 4562 | mBytesRead = 0; |
Eric Laurent | 243f5f9 | 2011-02-28 16:52:51 -0800 | [diff] [blame] | 4563 | if (mResampler != NULL) { |
| 4564 | mResampler->reset(); |
| 4565 | } |
| 4566 | mActiveTrack->mState = TrackBase::RESUMING; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4567 | // signal thread to start |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 4568 | ALOGV("Signal record thread"); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4569 | mWaitWorkCV.signal(); |
| 4570 | // do not wait for mStartStopCond if exiting |
| 4571 | if (mExiting) { |
| 4572 | mActiveTrack.clear(); |
| 4573 | status = INVALID_OPERATION; |
| 4574 | goto startError; |
| 4575 | } |
| 4576 | mStartStopCond.wait(mLock); |
| 4577 | if (mActiveTrack == 0) { |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 4578 | ALOGV("Record failed to start"); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4579 | status = BAD_VALUE; |
| 4580 | goto startError; |
| 4581 | } |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 4582 | ALOGV("Record started OK"); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4583 | return status; |
| 4584 | } |
| 4585 | startError: |
| 4586 | AudioSystem::stopInput(mId); |
| 4587 | return status; |
| 4588 | } |
| 4589 | |
| 4590 | void AudioFlinger::RecordThread::stop(RecordThread::RecordTrack* recordTrack) { |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 4591 | ALOGV("RecordThread::stop"); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4592 | sp <ThreadBase> strongMe = this; |
| 4593 | { |
Glenn Kasten | a7d8d6f | 2012-01-05 15:41:56 -0800 | [diff] [blame] | 4594 | AutoMutex lock(mLock); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4595 | if (mActiveTrack != 0 && recordTrack == mActiveTrack.get()) { |
| 4596 | mActiveTrack->mState = TrackBase::PAUSING; |
| 4597 | // do not wait for mStartStopCond if exiting |
| 4598 | if (mExiting) { |
| 4599 | return; |
| 4600 | } |
| 4601 | mStartStopCond.wait(mLock); |
| 4602 | // if we have been restarted, recordTrack == mActiveTrack.get() here |
| 4603 | if (mActiveTrack == 0 || recordTrack != mActiveTrack.get()) { |
| 4604 | mLock.unlock(); |
| 4605 | AudioSystem::stopInput(mId); |
| 4606 | mLock.lock(); |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 4607 | ALOGV("Record stopped OK"); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4608 | } |
| 4609 | } |
| 4610 | } |
| 4611 | } |
| 4612 | |
| 4613 | status_t AudioFlinger::RecordThread::dump(int fd, const Vector<String16>& args) |
| 4614 | { |
| 4615 | const size_t SIZE = 256; |
| 4616 | char buffer[SIZE]; |
| 4617 | String8 result; |
| 4618 | pid_t pid = 0; |
| 4619 | |
| 4620 | snprintf(buffer, SIZE, "\nInput thread %p internals\n", this); |
| 4621 | result.append(buffer); |
| 4622 | |
| 4623 | if (mActiveTrack != 0) { |
| 4624 | result.append("Active Track:\n"); |
Jean-Michel Trivi | 0d255b2 | 2011-05-24 15:53:33 -0700 | [diff] [blame] | 4625 | 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] | 4626 | mActiveTrack->dump(buffer, SIZE); |
| 4627 | result.append(buffer); |
| 4628 | |
| 4629 | snprintf(buffer, SIZE, "In index: %d\n", mRsmpInIndex); |
| 4630 | result.append(buffer); |
| 4631 | snprintf(buffer, SIZE, "In size: %d\n", mInputBytes); |
| 4632 | result.append(buffer); |
Glenn Kasten | e0feee3 | 2011-12-13 11:53:26 -0800 | [diff] [blame] | 4633 | snprintf(buffer, SIZE, "Resampling: %d\n", (mResampler != NULL)); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4634 | result.append(buffer); |
| 4635 | snprintf(buffer, SIZE, "Out channel count: %d\n", mReqChannelCount); |
| 4636 | result.append(buffer); |
| 4637 | snprintf(buffer, SIZE, "Out sample rate: %d\n", mReqSampleRate); |
| 4638 | result.append(buffer); |
| 4639 | |
| 4640 | |
| 4641 | } else { |
| 4642 | result.append("No record client\n"); |
| 4643 | } |
| 4644 | write(fd, result.string(), result.size()); |
| 4645 | |
| 4646 | dumpBase(fd, args); |
Eric Laurent | 1d2bff0 | 2011-07-24 17:49:51 -0700 | [diff] [blame] | 4647 | dumpEffectChains(fd, args); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4648 | |
| 4649 | return NO_ERROR; |
| 4650 | } |
| 4651 | |
| 4652 | status_t AudioFlinger::RecordThread::getNextBuffer(AudioBufferProvider::Buffer* buffer) |
| 4653 | { |
| 4654 | size_t framesReq = buffer->frameCount; |
| 4655 | size_t framesReady = mFrameCount - mRsmpInIndex; |
| 4656 | int channelCount; |
| 4657 | |
| 4658 | if (framesReady == 0) { |
Dima Zavin | 799a70e | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 4659 | mBytesRead = mInput->stream->read(mInput->stream, mRsmpInBuffer, mInputBytes); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4660 | if (mBytesRead < 0) { |
Steve Block | 29357bc | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 4661 | ALOGE("RecordThread::getNextBuffer() Error reading audio input"); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4662 | if (mActiveTrack->mState == TrackBase::ACTIVE) { |
| 4663 | // Force input into standby so that it tries to |
| 4664 | // recover at next read attempt |
Dima Zavin | 799a70e | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 4665 | mInput->stream->common.standby(&mInput->stream->common); |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 4666 | usleep(kRecordThreadSleepUs); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4667 | } |
Glenn Kasten | e0feee3 | 2011-12-13 11:53:26 -0800 | [diff] [blame] | 4668 | buffer->raw = NULL; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4669 | buffer->frameCount = 0; |
| 4670 | return NOT_ENOUGH_DATA; |
| 4671 | } |
| 4672 | mRsmpInIndex = 0; |
| 4673 | framesReady = mFrameCount; |
| 4674 | } |
| 4675 | |
| 4676 | if (framesReq > framesReady) { |
| 4677 | framesReq = framesReady; |
| 4678 | } |
| 4679 | |
| 4680 | if (mChannelCount == 1 && mReqChannelCount == 2) { |
| 4681 | channelCount = 1; |
| 4682 | } else { |
| 4683 | channelCount = 2; |
| 4684 | } |
| 4685 | buffer->raw = mRsmpInBuffer + mRsmpInIndex * channelCount; |
| 4686 | buffer->frameCount = framesReq; |
| 4687 | return NO_ERROR; |
| 4688 | } |
| 4689 | |
| 4690 | void AudioFlinger::RecordThread::releaseBuffer(AudioBufferProvider::Buffer* buffer) |
| 4691 | { |
| 4692 | mRsmpInIndex += buffer->frameCount; |
| 4693 | buffer->frameCount = 0; |
| 4694 | } |
| 4695 | |
| 4696 | bool AudioFlinger::RecordThread::checkForNewParameters_l() |
| 4697 | { |
| 4698 | bool reconfig = false; |
| 4699 | |
| 4700 | while (!mNewParameters.isEmpty()) { |
| 4701 | status_t status = NO_ERROR; |
| 4702 | String8 keyValuePair = mNewParameters[0]; |
| 4703 | AudioParameter param = AudioParameter(keyValuePair); |
| 4704 | int value; |
Glenn Kasten | 58f3021 | 2012-01-12 12:27:51 -0800 | [diff] [blame] | 4705 | audio_format_t reqFormat = mFormat; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4706 | int reqSamplingRate = mReqSampleRate; |
| 4707 | int reqChannelCount = mReqChannelCount; |
| 4708 | |
| 4709 | if (param.getInt(String8(AudioParameter::keySamplingRate), value) == NO_ERROR) { |
| 4710 | reqSamplingRate = value; |
| 4711 | reconfig = true; |
| 4712 | } |
| 4713 | if (param.getInt(String8(AudioParameter::keyFormat), value) == NO_ERROR) { |
Glenn Kasten | 58f3021 | 2012-01-12 12:27:51 -0800 | [diff] [blame] | 4714 | reqFormat = (audio_format_t) value; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4715 | reconfig = true; |
| 4716 | } |
| 4717 | if (param.getInt(String8(AudioParameter::keyChannels), value) == NO_ERROR) { |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 4718 | reqChannelCount = popcount(value); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4719 | reconfig = true; |
| 4720 | } |
| 4721 | if (param.getInt(String8(AudioParameter::keyFrameCount), value) == NO_ERROR) { |
| 4722 | // do not accept frame count changes if tracks are open as the track buffer |
| 4723 | // size depends on frame count and correct behavior would not be garantied |
| 4724 | // if frame count is changed after track creation |
| 4725 | if (mActiveTrack != 0) { |
| 4726 | status = INVALID_OPERATION; |
| 4727 | } else { |
| 4728 | reconfig = true; |
| 4729 | } |
| 4730 | } |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 4731 | if (param.getInt(String8(AudioParameter::keyRouting), value) == NO_ERROR) { |
| 4732 | // forward device change to effects that have requested to be |
| 4733 | // aware of attached audio device. |
| 4734 | for (size_t i = 0; i < mEffectChains.size(); i++) { |
| 4735 | mEffectChains[i]->setDevice_l(value); |
| 4736 | } |
| 4737 | // store input device and output device but do not forward output device to audio HAL. |
| 4738 | // Note that status is ignored by the caller for output device |
| 4739 | // (see AudioFlinger::setParameters() |
| 4740 | if (value & AUDIO_DEVICE_OUT_ALL) { |
| 4741 | mDevice &= (uint32_t)~(value & AUDIO_DEVICE_OUT_ALL); |
| 4742 | status = BAD_VALUE; |
| 4743 | } else { |
| 4744 | mDevice &= (uint32_t)~(value & AUDIO_DEVICE_IN_ALL); |
Eric Laurent | 59bd0da | 2011-08-01 09:52:20 -0700 | [diff] [blame] | 4745 | // disable AEC and NS if the device is a BT SCO headset supporting those pre processings |
| 4746 | if (mTrack != NULL) { |
| 4747 | bool suspend = audio_is_bluetooth_sco_device( |
Eric Laurent | bee5337 | 2011-08-29 12:42:48 -0700 | [diff] [blame] | 4748 | (audio_devices_t)value) && mAudioFlinger->btNrecIsOff(); |
Eric Laurent | 59bd0da | 2011-08-01 09:52:20 -0700 | [diff] [blame] | 4749 | setEffectSuspended_l(FX_IID_AEC, suspend, mTrack->sessionId()); |
| 4750 | setEffectSuspended_l(FX_IID_NS, suspend, mTrack->sessionId()); |
| 4751 | } |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 4752 | } |
| 4753 | mDevice |= (uint32_t)value; |
| 4754 | } |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4755 | if (status == NO_ERROR) { |
Dima Zavin | 799a70e | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 4756 | status = mInput->stream->common.set_parameters(&mInput->stream->common, keyValuePair.string()); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4757 | if (status == INVALID_OPERATION) { |
Dima Zavin | 799a70e | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 4758 | mInput->stream->common.standby(&mInput->stream->common); |
| 4759 | status = mInput->stream->common.set_parameters(&mInput->stream->common, keyValuePair.string()); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4760 | } |
| 4761 | if (reconfig) { |
| 4762 | if (status == BAD_VALUE && |
Dima Zavin | 799a70e | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 4763 | reqFormat == mInput->stream->common.get_format(&mInput->stream->common) && |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 4764 | reqFormat == AUDIO_FORMAT_PCM_16_BIT && |
Dima Zavin | 799a70e | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 4765 | ((int)mInput->stream->common.get_sample_rate(&mInput->stream->common) <= (2 * reqSamplingRate)) && |
| 4766 | (popcount(mInput->stream->common.get_channels(&mInput->stream->common)) < 3) && |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 4767 | (reqChannelCount < 3)) { |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4768 | status = NO_ERROR; |
| 4769 | } |
| 4770 | if (status == NO_ERROR) { |
| 4771 | readInputParameters(); |
| 4772 | sendConfigEvent_l(AudioSystem::INPUT_CONFIG_CHANGED); |
| 4773 | } |
| 4774 | } |
| 4775 | } |
| 4776 | |
| 4777 | mNewParameters.removeAt(0); |
| 4778 | |
| 4779 | mParamStatus = status; |
| 4780 | mParamCond.signal(); |
Eric Laurent | 60cd0a0 | 2011-09-13 11:40:21 -0700 | [diff] [blame] | 4781 | // wait for condition with time out in case the thread calling ThreadBase::setParameters() |
| 4782 | // 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] | 4783 | mWaitWorkCV.waitRelative(mLock, kSetParametersTimeoutNs); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4784 | } |
| 4785 | return reconfig; |
| 4786 | } |
| 4787 | |
| 4788 | String8 AudioFlinger::RecordThread::getParameters(const String8& keys) |
| 4789 | { |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 4790 | char *s; |
Eric Laurent | b8ba0a9 | 2011-08-07 16:32:26 -0700 | [diff] [blame] | 4791 | String8 out_s8 = String8(); |
| 4792 | |
| 4793 | Mutex::Autolock _l(mLock); |
| 4794 | if (initCheck() != NO_ERROR) { |
| 4795 | return out_s8; |
| 4796 | } |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 4797 | |
Dima Zavin | 799a70e | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 4798 | s = mInput->stream->common.get_parameters(&mInput->stream->common, keys.string()); |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 4799 | out_s8 = String8(s); |
| 4800 | free(s); |
| 4801 | return out_s8; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4802 | } |
| 4803 | |
| 4804 | void AudioFlinger::RecordThread::audioConfigChanged_l(int event, int param) { |
| 4805 | AudioSystem::OutputDescriptor desc; |
| 4806 | void *param2 = 0; |
| 4807 | |
| 4808 | switch (event) { |
| 4809 | case AudioSystem::INPUT_OPENED: |
| 4810 | case AudioSystem::INPUT_CONFIG_CHANGED: |
Jean-Michel Trivi | 0d255b2 | 2011-05-24 15:53:33 -0700 | [diff] [blame] | 4811 | desc.channels = mChannelMask; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4812 | desc.samplingRate = mSampleRate; |
| 4813 | desc.format = mFormat; |
| 4814 | desc.frameCount = mFrameCount; |
| 4815 | desc.latency = 0; |
| 4816 | param2 = &desc; |
| 4817 | break; |
| 4818 | |
| 4819 | case AudioSystem::INPUT_CLOSED: |
| 4820 | default: |
| 4821 | break; |
| 4822 | } |
| 4823 | mAudioFlinger->audioConfigChanged_l(event, mId, param2); |
| 4824 | } |
| 4825 | |
| 4826 | void AudioFlinger::RecordThread::readInputParameters() |
| 4827 | { |
| 4828 | if (mRsmpInBuffer) delete mRsmpInBuffer; |
| 4829 | if (mRsmpOutBuffer) delete mRsmpOutBuffer; |
| 4830 | if (mResampler) delete mResampler; |
Glenn Kasten | e0feee3 | 2011-12-13 11:53:26 -0800 | [diff] [blame] | 4831 | mResampler = NULL; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4832 | |
Dima Zavin | 799a70e | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 4833 | mSampleRate = mInput->stream->common.get_sample_rate(&mInput->stream->common); |
Jean-Michel Trivi | 0d255b2 | 2011-05-24 15:53:33 -0700 | [diff] [blame] | 4834 | mChannelMask = mInput->stream->common.get_channels(&mInput->stream->common); |
| 4835 | mChannelCount = (uint16_t)popcount(mChannelMask); |
Dima Zavin | 799a70e | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 4836 | mFormat = mInput->stream->common.get_format(&mInput->stream->common); |
Glenn Kasten | b998065 | 2012-01-11 09:48:27 -0800 | [diff] [blame] | 4837 | mFrameSize = audio_stream_frame_size(&mInput->stream->common); |
Dima Zavin | 799a70e | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 4838 | mInputBytes = mInput->stream->common.get_buffer_size(&mInput->stream->common); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4839 | mFrameCount = mInputBytes / mFrameSize; |
| 4840 | mRsmpInBuffer = new int16_t[mFrameCount * mChannelCount]; |
| 4841 | |
| 4842 | if (mSampleRate != mReqSampleRate && mChannelCount < 3 && mReqChannelCount < 3) |
| 4843 | { |
| 4844 | int channelCount; |
| 4845 | // optmization: if mono to mono, use the resampler in stereo to stereo mode to avoid |
| 4846 | // stereo to mono post process as the resampler always outputs stereo. |
| 4847 | if (mChannelCount == 1 && mReqChannelCount == 2) { |
| 4848 | channelCount = 1; |
| 4849 | } else { |
| 4850 | channelCount = 2; |
| 4851 | } |
| 4852 | mResampler = AudioResampler::create(16, channelCount, mReqSampleRate); |
| 4853 | mResampler->setSampleRate(mSampleRate); |
| 4854 | mResampler->setVolume(AudioMixer::UNITY_GAIN, AudioMixer::UNITY_GAIN); |
| 4855 | mRsmpOutBuffer = new int32_t[mFrameCount * 2]; |
| 4856 | |
| 4857 | // optmization: if mono to mono, alter input frame count as if we were inputing stereo samples |
| 4858 | if (mChannelCount == 1 && mReqChannelCount == 1) { |
| 4859 | mFrameCount >>= 1; |
| 4860 | } |
| 4861 | |
| 4862 | } |
| 4863 | mRsmpInIndex = mFrameCount; |
| 4864 | } |
| 4865 | |
| 4866 | unsigned int AudioFlinger::RecordThread::getInputFramesLost() |
| 4867 | { |
Eric Laurent | b8ba0a9 | 2011-08-07 16:32:26 -0700 | [diff] [blame] | 4868 | Mutex::Autolock _l(mLock); |
| 4869 | if (initCheck() != NO_ERROR) { |
| 4870 | return 0; |
| 4871 | } |
| 4872 | |
Dima Zavin | 799a70e | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 4873 | return mInput->stream->get_input_frames_lost(mInput->stream); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4874 | } |
| 4875 | |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 4876 | uint32_t AudioFlinger::RecordThread::hasAudioSession(int sessionId) |
| 4877 | { |
| 4878 | Mutex::Autolock _l(mLock); |
| 4879 | uint32_t result = 0; |
| 4880 | if (getEffectChain_l(sessionId) != 0) { |
| 4881 | result = EFFECT_SESSION; |
| 4882 | } |
| 4883 | |
| 4884 | if (mTrack != NULL && sessionId == mTrack->sessionId()) { |
| 4885 | result |= TRACK_SESSION; |
| 4886 | } |
| 4887 | |
| 4888 | return result; |
| 4889 | } |
| 4890 | |
Eric Laurent | 59bd0da | 2011-08-01 09:52:20 -0700 | [diff] [blame] | 4891 | AudioFlinger::RecordThread::RecordTrack* AudioFlinger::RecordThread::track() |
| 4892 | { |
| 4893 | Mutex::Autolock _l(mLock); |
| 4894 | return mTrack; |
| 4895 | } |
| 4896 | |
Eric Laurent | b8ba0a9 | 2011-08-07 16:32:26 -0700 | [diff] [blame] | 4897 | AudioFlinger::AudioStreamIn* AudioFlinger::RecordThread::getInput() |
| 4898 | { |
| 4899 | Mutex::Autolock _l(mLock); |
| 4900 | return mInput; |
| 4901 | } |
| 4902 | |
| 4903 | AudioFlinger::AudioStreamIn* AudioFlinger::RecordThread::clearInput() |
| 4904 | { |
| 4905 | Mutex::Autolock _l(mLock); |
| 4906 | AudioStreamIn *input = mInput; |
| 4907 | mInput = NULL; |
| 4908 | return input; |
| 4909 | } |
| 4910 | |
| 4911 | // this method must always be called either with ThreadBase mLock held or inside the thread loop |
| 4912 | audio_stream_t* AudioFlinger::RecordThread::stream() |
| 4913 | { |
| 4914 | if (mInput == NULL) { |
| 4915 | return NULL; |
| 4916 | } |
| 4917 | return &mInput->stream->common; |
| 4918 | } |
| 4919 | |
| 4920 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4921 | // ---------------------------------------------------------------------------- |
| 4922 | |
| 4923 | int AudioFlinger::openOutput(uint32_t *pDevices, |
| 4924 | uint32_t *pSamplingRate, |
Glenn Kasten | 58f3021 | 2012-01-12 12:27:51 -0800 | [diff] [blame] | 4925 | audio_format_t *pFormat, |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4926 | uint32_t *pChannels, |
| 4927 | uint32_t *pLatencyMs, |
| 4928 | uint32_t flags) |
| 4929 | { |
| 4930 | status_t status; |
| 4931 | PlaybackThread *thread = NULL; |
| 4932 | mHardwareStatus = AUDIO_HW_OUTPUT_OPEN; |
| 4933 | uint32_t samplingRate = pSamplingRate ? *pSamplingRate : 0; |
Glenn Kasten | 58f3021 | 2012-01-12 12:27:51 -0800 | [diff] [blame] | 4934 | audio_format_t format = pFormat ? *pFormat : AUDIO_FORMAT_DEFAULT; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4935 | uint32_t channels = pChannels ? *pChannels : 0; |
| 4936 | uint32_t latency = pLatencyMs ? *pLatencyMs : 0; |
Dima Zavin | 799a70e | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 4937 | audio_stream_out_t *outStream; |
| 4938 | audio_hw_device_t *outHwDev; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4939 | |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 4940 | ALOGV("openOutput(), Device %x, SamplingRate %d, Format %d, Channels %x, flags %x", |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4941 | pDevices ? *pDevices : 0, |
| 4942 | samplingRate, |
| 4943 | format, |
| 4944 | channels, |
| 4945 | flags); |
| 4946 | |
| 4947 | if (pDevices == NULL || *pDevices == 0) { |
| 4948 | return 0; |
| 4949 | } |
Dima Zavin | 799a70e | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 4950 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4951 | Mutex::Autolock _l(mLock); |
| 4952 | |
Dima Zavin | 799a70e | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 4953 | outHwDev = findSuitableHwDev_l(*pDevices); |
| 4954 | if (outHwDev == NULL) |
| 4955 | return 0; |
| 4956 | |
Glenn Kasten | 58f3021 | 2012-01-12 12:27:51 -0800 | [diff] [blame] | 4957 | status = outHwDev->open_output_stream(outHwDev, *pDevices, &format, |
Dima Zavin | 799a70e | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 4958 | &channels, &samplingRate, &outStream); |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 4959 | 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] | 4960 | outStream, |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4961 | samplingRate, |
| 4962 | format, |
| 4963 | channels, |
| 4964 | status); |
| 4965 | |
| 4966 | mHardwareStatus = AUDIO_HW_IDLE; |
Dima Zavin | 799a70e | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 4967 | if (outStream != NULL) { |
| 4968 | AudioStreamOut *output = new AudioStreamOut(outHwDev, outStream); |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 4969 | int id = nextUniqueId(); |
Dima Zavin | 799a70e | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 4970 | |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 4971 | if ((flags & AUDIO_POLICY_OUTPUT_FLAG_DIRECT) || |
| 4972 | (format != AUDIO_FORMAT_PCM_16_BIT) || |
| 4973 | (channels != AUDIO_CHANNEL_OUT_STEREO)) { |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4974 | thread = new DirectOutputThread(this, output, id, *pDevices); |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 4975 | ALOGV("openOutput() created direct output: ID %d thread %p", id, thread); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4976 | } else { |
| 4977 | thread = new MixerThread(this, output, id, *pDevices); |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 4978 | ALOGV("openOutput() created mixer output: ID %d thread %p", id, thread); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4979 | } |
| 4980 | mPlaybackThreads.add(id, thread); |
| 4981 | |
| 4982 | if (pSamplingRate) *pSamplingRate = samplingRate; |
| 4983 | if (pFormat) *pFormat = format; |
| 4984 | if (pChannels) *pChannels = channels; |
| 4985 | if (pLatencyMs) *pLatencyMs = thread->latency(); |
| 4986 | |
| 4987 | // notify client processes of the new output creation |
| 4988 | thread->audioConfigChanged_l(AudioSystem::OUTPUT_OPENED); |
| 4989 | return id; |
| 4990 | } |
| 4991 | |
| 4992 | return 0; |
| 4993 | } |
| 4994 | |
| 4995 | int AudioFlinger::openDuplicateOutput(int output1, int output2) |
| 4996 | { |
| 4997 | Mutex::Autolock _l(mLock); |
| 4998 | MixerThread *thread1 = checkMixerThread_l(output1); |
| 4999 | MixerThread *thread2 = checkMixerThread_l(output2); |
| 5000 | |
| 5001 | if (thread1 == NULL || thread2 == NULL) { |
Steve Block | 5ff1dd5 | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 5002 | 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] | 5003 | return 0; |
| 5004 | } |
| 5005 | |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 5006 | int id = nextUniqueId(); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5007 | DuplicatingThread *thread = new DuplicatingThread(this, thread1, id); |
| 5008 | thread->addOutputTrack(thread2); |
| 5009 | mPlaybackThreads.add(id, thread); |
| 5010 | // notify client processes of the new output creation |
| 5011 | thread->audioConfigChanged_l(AudioSystem::OUTPUT_OPENED); |
| 5012 | return id; |
| 5013 | } |
| 5014 | |
| 5015 | status_t AudioFlinger::closeOutput(int output) |
| 5016 | { |
| 5017 | // keep strong reference on the playback thread so that |
| 5018 | // it is not destroyed while exit() is executed |
| 5019 | sp <PlaybackThread> thread; |
| 5020 | { |
| 5021 | Mutex::Autolock _l(mLock); |
| 5022 | thread = checkPlaybackThread_l(output); |
| 5023 | if (thread == NULL) { |
| 5024 | return BAD_VALUE; |
| 5025 | } |
| 5026 | |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 5027 | ALOGV("closeOutput() %d", output); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5028 | |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 5029 | if (thread->type() == ThreadBase::MIXER) { |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5030 | for (size_t i = 0; i < mPlaybackThreads.size(); i++) { |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 5031 | if (mPlaybackThreads.valueAt(i)->type() == ThreadBase::DUPLICATING) { |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5032 | DuplicatingThread *dupThread = (DuplicatingThread *)mPlaybackThreads.valueAt(i).get(); |
| 5033 | dupThread->removeOutputTrack((MixerThread *)thread.get()); |
| 5034 | } |
| 5035 | } |
| 5036 | } |
| 5037 | void *param2 = 0; |
| 5038 | audioConfigChanged_l(AudioSystem::OUTPUT_CLOSED, output, param2); |
| 5039 | mPlaybackThreads.removeItem(output); |
| 5040 | } |
| 5041 | thread->exit(); |
| 5042 | |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 5043 | if (thread->type() != ThreadBase::DUPLICATING) { |
Eric Laurent | b8ba0a9 | 2011-08-07 16:32:26 -0700 | [diff] [blame] | 5044 | AudioStreamOut *out = thread->clearOutput(); |
| 5045 | // from now on thread->mOutput is NULL |
Dima Zavin | 799a70e | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 5046 | out->hwDev->close_output_stream(out->hwDev, out->stream); |
| 5047 | delete out; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5048 | } |
| 5049 | return NO_ERROR; |
| 5050 | } |
| 5051 | |
| 5052 | status_t AudioFlinger::suspendOutput(int output) |
| 5053 | { |
| 5054 | Mutex::Autolock _l(mLock); |
| 5055 | PlaybackThread *thread = checkPlaybackThread_l(output); |
| 5056 | |
| 5057 | if (thread == NULL) { |
| 5058 | return BAD_VALUE; |
| 5059 | } |
| 5060 | |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 5061 | ALOGV("suspendOutput() %d", output); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5062 | thread->suspend(); |
| 5063 | |
| 5064 | return NO_ERROR; |
| 5065 | } |
| 5066 | |
| 5067 | status_t AudioFlinger::restoreOutput(int output) |
| 5068 | { |
| 5069 | Mutex::Autolock _l(mLock); |
| 5070 | PlaybackThread *thread = checkPlaybackThread_l(output); |
| 5071 | |
| 5072 | if (thread == NULL) { |
| 5073 | return BAD_VALUE; |
| 5074 | } |
| 5075 | |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 5076 | ALOGV("restoreOutput() %d", output); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5077 | |
| 5078 | thread->restore(); |
| 5079 | |
| 5080 | return NO_ERROR; |
| 5081 | } |
| 5082 | |
| 5083 | int AudioFlinger::openInput(uint32_t *pDevices, |
| 5084 | uint32_t *pSamplingRate, |
Glenn Kasten | 58f3021 | 2012-01-12 12:27:51 -0800 | [diff] [blame] | 5085 | audio_format_t *pFormat, |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5086 | uint32_t *pChannels, |
| 5087 | uint32_t acoustics) |
| 5088 | { |
| 5089 | status_t status; |
| 5090 | RecordThread *thread = NULL; |
| 5091 | uint32_t samplingRate = pSamplingRate ? *pSamplingRate : 0; |
Glenn Kasten | 58f3021 | 2012-01-12 12:27:51 -0800 | [diff] [blame] | 5092 | audio_format_t format = pFormat ? *pFormat : AUDIO_FORMAT_DEFAULT; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5093 | uint32_t channels = pChannels ? *pChannels : 0; |
| 5094 | uint32_t reqSamplingRate = samplingRate; |
Glenn Kasten | 58f3021 | 2012-01-12 12:27:51 -0800 | [diff] [blame] | 5095 | audio_format_t reqFormat = format; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5096 | uint32_t reqChannels = channels; |
Dima Zavin | 799a70e | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 5097 | audio_stream_in_t *inStream; |
| 5098 | audio_hw_device_t *inHwDev; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5099 | |
| 5100 | if (pDevices == NULL || *pDevices == 0) { |
| 5101 | return 0; |
| 5102 | } |
Dima Zavin | 799a70e | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 5103 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5104 | Mutex::Autolock _l(mLock); |
| 5105 | |
Dima Zavin | 799a70e | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 5106 | inHwDev = findSuitableHwDev_l(*pDevices); |
| 5107 | if (inHwDev == NULL) |
| 5108 | return 0; |
| 5109 | |
Glenn Kasten | 58f3021 | 2012-01-12 12:27:51 -0800 | [diff] [blame] | 5110 | status = inHwDev->open_input_stream(inHwDev, *pDevices, &format, |
Dima Zavin | 799a70e | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 5111 | &channels, &samplingRate, |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 5112 | (audio_in_acoustics_t)acoustics, |
Dima Zavin | 799a70e | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 5113 | &inStream); |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 5114 | 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] | 5115 | inStream, |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5116 | samplingRate, |
| 5117 | format, |
| 5118 | channels, |
| 5119 | acoustics, |
| 5120 | status); |
| 5121 | |
| 5122 | // If the input could not be opened with the requested parameters and we can handle the conversion internally, |
| 5123 | // try to open again with the proposed parameters. The AudioFlinger can resample the input and do mono to stereo |
| 5124 | // or stereo to mono conversions on 16 bit PCM inputs. |
Dima Zavin | 799a70e | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 5125 | if (inStream == NULL && status == BAD_VALUE && |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 5126 | reqFormat == format && format == AUDIO_FORMAT_PCM_16_BIT && |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5127 | (samplingRate <= 2 * reqSamplingRate) && |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 5128 | (popcount(channels) < 3) && (popcount(reqChannels) < 3)) { |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 5129 | ALOGV("openInput() reopening with proposed sampling rate and channels"); |
Glenn Kasten | 58f3021 | 2012-01-12 12:27:51 -0800 | [diff] [blame] | 5130 | status = inHwDev->open_input_stream(inHwDev, *pDevices, &format, |
Dima Zavin | 799a70e | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 5131 | &channels, &samplingRate, |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 5132 | (audio_in_acoustics_t)acoustics, |
Dima Zavin | 799a70e | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 5133 | &inStream); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5134 | } |
| 5135 | |
Dima Zavin | 799a70e | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 5136 | if (inStream != NULL) { |
| 5137 | AudioStreamIn *input = new AudioStreamIn(inHwDev, inStream); |
| 5138 | |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 5139 | int id = nextUniqueId(); |
| 5140 | // Start record thread |
| 5141 | // RecorThread require both input and output device indication to forward to audio |
| 5142 | // pre processing modules |
| 5143 | uint32_t device = (*pDevices) | primaryOutputDevice_l(); |
| 5144 | thread = new RecordThread(this, |
| 5145 | input, |
| 5146 | reqSamplingRate, |
| 5147 | reqChannels, |
| 5148 | id, |
| 5149 | device); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5150 | mRecordThreads.add(id, thread); |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 5151 | ALOGV("openInput() created record thread: ID %d thread %p", id, thread); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5152 | if (pSamplingRate) *pSamplingRate = reqSamplingRate; |
| 5153 | if (pFormat) *pFormat = format; |
| 5154 | if (pChannels) *pChannels = reqChannels; |
| 5155 | |
Dima Zavin | 799a70e | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 5156 | input->stream->common.standby(&input->stream->common); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5157 | |
| 5158 | // notify client processes of the new input creation |
| 5159 | thread->audioConfigChanged_l(AudioSystem::INPUT_OPENED); |
| 5160 | return id; |
| 5161 | } |
| 5162 | |
| 5163 | return 0; |
| 5164 | } |
| 5165 | |
| 5166 | status_t AudioFlinger::closeInput(int input) |
| 5167 | { |
| 5168 | // keep strong reference on the record thread so that |
| 5169 | // it is not destroyed while exit() is executed |
| 5170 | sp <RecordThread> thread; |
| 5171 | { |
| 5172 | Mutex::Autolock _l(mLock); |
| 5173 | thread = checkRecordThread_l(input); |
| 5174 | if (thread == NULL) { |
| 5175 | return BAD_VALUE; |
| 5176 | } |
| 5177 | |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 5178 | ALOGV("closeInput() %d", input); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5179 | void *param2 = 0; |
| 5180 | audioConfigChanged_l(AudioSystem::INPUT_CLOSED, input, param2); |
| 5181 | mRecordThreads.removeItem(input); |
| 5182 | } |
| 5183 | thread->exit(); |
| 5184 | |
Eric Laurent | b8ba0a9 | 2011-08-07 16:32:26 -0700 | [diff] [blame] | 5185 | AudioStreamIn *in = thread->clearInput(); |
| 5186 | // from now on thread->mInput is NULL |
Dima Zavin | 799a70e | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 5187 | in->hwDev->close_input_stream(in->hwDev, in->stream); |
| 5188 | delete in; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5189 | |
| 5190 | return NO_ERROR; |
| 5191 | } |
| 5192 | |
Glenn Kasten | fff6d71 | 2012-01-12 16:38:12 -0800 | [diff] [blame] | 5193 | status_t AudioFlinger::setStreamOutput(audio_stream_type_t stream, int output) |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5194 | { |
| 5195 | Mutex::Autolock _l(mLock); |
| 5196 | MixerThread *dstThread = checkMixerThread_l(output); |
| 5197 | if (dstThread == NULL) { |
Steve Block | 5ff1dd5 | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 5198 | ALOGW("setStreamOutput() bad output id %d", output); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5199 | return BAD_VALUE; |
| 5200 | } |
| 5201 | |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 5202 | ALOGV("setStreamOutput() stream %d to output %d", stream, output); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5203 | audioConfigChanged_l(AudioSystem::STREAM_CONFIG_CHANGED, output, &stream); |
| 5204 | |
Eric Laurent | 9f6530f | 2011-08-30 10:18:54 -0700 | [diff] [blame] | 5205 | dstThread->setStreamValid(stream, true); |
| 5206 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5207 | for (size_t i = 0; i < mPlaybackThreads.size(); i++) { |
| 5208 | PlaybackThread *thread = mPlaybackThreads.valueAt(i).get(); |
| 5209 | if (thread != dstThread && |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 5210 | thread->type() != ThreadBase::DIRECT) { |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5211 | MixerThread *srcThread = (MixerThread *)thread; |
Eric Laurent | 9f6530f | 2011-08-30 10:18:54 -0700 | [diff] [blame] | 5212 | srcThread->setStreamValid(stream, false); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5213 | srcThread->invalidateTracks(stream); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5214 | } |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 5215 | } |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5216 | |
| 5217 | return NO_ERROR; |
| 5218 | } |
| 5219 | |
| 5220 | |
| 5221 | int AudioFlinger::newAudioSessionId() |
| 5222 | { |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 5223 | return nextUniqueId(); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5224 | } |
| 5225 | |
Marco Nelissen | 3a34bef | 2011-08-02 13:33:41 -0700 | [diff] [blame] | 5226 | void AudioFlinger::acquireAudioSessionId(int audioSession) |
| 5227 | { |
| 5228 | Mutex::Autolock _l(mLock); |
| 5229 | int caller = IPCThreadState::self()->getCallingPid(); |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 5230 | ALOGV("acquiring %d from %d", audioSession, caller); |
Marco Nelissen | 3a34bef | 2011-08-02 13:33:41 -0700 | [diff] [blame] | 5231 | int num = mAudioSessionRefs.size(); |
| 5232 | for (int i = 0; i< num; i++) { |
| 5233 | AudioSessionRef *ref = mAudioSessionRefs.editItemAt(i); |
| 5234 | if (ref->sessionid == audioSession && ref->pid == caller) { |
| 5235 | ref->cnt++; |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 5236 | ALOGV(" incremented refcount to %d", ref->cnt); |
Marco Nelissen | 3a34bef | 2011-08-02 13:33:41 -0700 | [diff] [blame] | 5237 | return; |
| 5238 | } |
| 5239 | } |
| 5240 | AudioSessionRef *ref = new AudioSessionRef(); |
| 5241 | ref->sessionid = audioSession; |
| 5242 | ref->pid = caller; |
| 5243 | ref->cnt = 1; |
| 5244 | mAudioSessionRefs.push(ref); |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 5245 | ALOGV(" added new entry for %d", ref->sessionid); |
Marco Nelissen | 3a34bef | 2011-08-02 13:33:41 -0700 | [diff] [blame] | 5246 | } |
| 5247 | |
| 5248 | void AudioFlinger::releaseAudioSessionId(int audioSession) |
| 5249 | { |
| 5250 | Mutex::Autolock _l(mLock); |
| 5251 | int caller = IPCThreadState::self()->getCallingPid(); |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 5252 | ALOGV("releasing %d from %d", audioSession, caller); |
Marco Nelissen | 3a34bef | 2011-08-02 13:33:41 -0700 | [diff] [blame] | 5253 | int num = mAudioSessionRefs.size(); |
| 5254 | for (int i = 0; i< num; i++) { |
| 5255 | AudioSessionRef *ref = mAudioSessionRefs.itemAt(i); |
| 5256 | if (ref->sessionid == audioSession && ref->pid == caller) { |
| 5257 | ref->cnt--; |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 5258 | ALOGV(" decremented refcount to %d", ref->cnt); |
Marco Nelissen | 3a34bef | 2011-08-02 13:33:41 -0700 | [diff] [blame] | 5259 | if (ref->cnt == 0) { |
| 5260 | mAudioSessionRefs.removeAt(i); |
| 5261 | delete ref; |
| 5262 | purgeStaleEffects_l(); |
| 5263 | } |
| 5264 | return; |
| 5265 | } |
| 5266 | } |
Steve Block | 5ff1dd5 | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 5267 | ALOGW("session id %d not found for pid %d", audioSession, caller); |
Marco Nelissen | 3a34bef | 2011-08-02 13:33:41 -0700 | [diff] [blame] | 5268 | } |
| 5269 | |
| 5270 | void AudioFlinger::purgeStaleEffects_l() { |
| 5271 | |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 5272 | ALOGV("purging stale effects"); |
Marco Nelissen | 3a34bef | 2011-08-02 13:33:41 -0700 | [diff] [blame] | 5273 | |
| 5274 | Vector< sp<EffectChain> > chains; |
| 5275 | |
| 5276 | for (size_t i = 0; i < mPlaybackThreads.size(); i++) { |
| 5277 | sp<PlaybackThread> t = mPlaybackThreads.valueAt(i); |
| 5278 | for (size_t j = 0; j < t->mEffectChains.size(); j++) { |
| 5279 | sp<EffectChain> ec = t->mEffectChains[j]; |
Marco Nelissen | 0270b18 | 2011-08-12 14:14:39 -0700 | [diff] [blame] | 5280 | if (ec->sessionId() > AUDIO_SESSION_OUTPUT_MIX) { |
| 5281 | chains.push(ec); |
| 5282 | } |
Marco Nelissen | 3a34bef | 2011-08-02 13:33:41 -0700 | [diff] [blame] | 5283 | } |
| 5284 | } |
| 5285 | for (size_t i = 0; i < mRecordThreads.size(); i++) { |
| 5286 | sp<RecordThread> t = mRecordThreads.valueAt(i); |
| 5287 | for (size_t j = 0; j < t->mEffectChains.size(); j++) { |
| 5288 | sp<EffectChain> ec = t->mEffectChains[j]; |
| 5289 | chains.push(ec); |
| 5290 | } |
| 5291 | } |
| 5292 | |
| 5293 | for (size_t i = 0; i < chains.size(); i++) { |
| 5294 | sp<EffectChain> ec = chains[i]; |
| 5295 | int sessionid = ec->sessionId(); |
| 5296 | sp<ThreadBase> t = ec->mThread.promote(); |
| 5297 | if (t == 0) { |
| 5298 | continue; |
| 5299 | } |
| 5300 | size_t numsessionrefs = mAudioSessionRefs.size(); |
| 5301 | bool found = false; |
| 5302 | for (size_t k = 0; k < numsessionrefs; k++) { |
| 5303 | AudioSessionRef *ref = mAudioSessionRefs.itemAt(k); |
| 5304 | if (ref->sessionid == sessionid) { |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 5305 | ALOGV(" session %d still exists for %d with %d refs", |
Marco Nelissen | 3a34bef | 2011-08-02 13:33:41 -0700 | [diff] [blame] | 5306 | sessionid, ref->pid, ref->cnt); |
| 5307 | found = true; |
| 5308 | break; |
| 5309 | } |
| 5310 | } |
| 5311 | if (!found) { |
| 5312 | // remove all effects from the chain |
| 5313 | while (ec->mEffects.size()) { |
| 5314 | sp<EffectModule> effect = ec->mEffects[0]; |
| 5315 | effect->unPin(); |
| 5316 | Mutex::Autolock _l (t->mLock); |
| 5317 | t->removeEffect_l(effect); |
| 5318 | for (size_t j = 0; j < effect->mHandles.size(); j++) { |
| 5319 | sp<EffectHandle> handle = effect->mHandles[j].promote(); |
| 5320 | if (handle != 0) { |
| 5321 | handle->mEffect.clear(); |
Eric Laurent | a85a74a | 2011-10-19 11:44:54 -0700 | [diff] [blame] | 5322 | if (handle->mHasControl && handle->mEnabled) { |
| 5323 | t->checkSuspendOnEffectEnabled_l(effect, false, effect->sessionId()); |
| 5324 | } |
Marco Nelissen | 3a34bef | 2011-08-02 13:33:41 -0700 | [diff] [blame] | 5325 | } |
| 5326 | } |
| 5327 | AudioSystem::unregisterEffect(effect->id()); |
| 5328 | } |
| 5329 | } |
| 5330 | } |
| 5331 | return; |
| 5332 | } |
| 5333 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5334 | // checkPlaybackThread_l() must be called with AudioFlinger::mLock held |
| 5335 | AudioFlinger::PlaybackThread *AudioFlinger::checkPlaybackThread_l(int output) const |
| 5336 | { |
| 5337 | PlaybackThread *thread = NULL; |
| 5338 | if (mPlaybackThreads.indexOfKey(output) >= 0) { |
| 5339 | thread = (PlaybackThread *)mPlaybackThreads.valueFor(output).get(); |
| 5340 | } |
| 5341 | return thread; |
| 5342 | } |
| 5343 | |
| 5344 | // checkMixerThread_l() must be called with AudioFlinger::mLock held |
| 5345 | AudioFlinger::MixerThread *AudioFlinger::checkMixerThread_l(int output) const |
| 5346 | { |
| 5347 | PlaybackThread *thread = checkPlaybackThread_l(output); |
| 5348 | if (thread != NULL) { |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 5349 | if (thread->type() == ThreadBase::DIRECT) { |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5350 | thread = NULL; |
| 5351 | } |
| 5352 | } |
| 5353 | return (MixerThread *)thread; |
| 5354 | } |
| 5355 | |
| 5356 | // checkRecordThread_l() must be called with AudioFlinger::mLock held |
| 5357 | AudioFlinger::RecordThread *AudioFlinger::checkRecordThread_l(int input) const |
| 5358 | { |
| 5359 | RecordThread *thread = NULL; |
| 5360 | if (mRecordThreads.indexOfKey(input) >= 0) { |
| 5361 | thread = (RecordThread *)mRecordThreads.valueFor(input).get(); |
| 5362 | } |
| 5363 | return thread; |
| 5364 | } |
| 5365 | |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 5366 | uint32_t AudioFlinger::nextUniqueId() |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5367 | { |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 5368 | return android_atomic_inc(&mNextUniqueId); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5369 | } |
| 5370 | |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 5371 | AudioFlinger::PlaybackThread *AudioFlinger::primaryPlaybackThread_l() |
| 5372 | { |
| 5373 | for (size_t i = 0; i < mPlaybackThreads.size(); i++) { |
| 5374 | PlaybackThread *thread = mPlaybackThreads.valueAt(i).get(); |
Eric Laurent | b8ba0a9 | 2011-08-07 16:32:26 -0700 | [diff] [blame] | 5375 | AudioStreamOut *output = thread->getOutput(); |
| 5376 | if (output != NULL && output->hwDev == mPrimaryHardwareDev) { |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 5377 | return thread; |
| 5378 | } |
| 5379 | } |
| 5380 | return NULL; |
| 5381 | } |
| 5382 | |
| 5383 | uint32_t AudioFlinger::primaryOutputDevice_l() |
| 5384 | { |
| 5385 | PlaybackThread *thread = primaryPlaybackThread_l(); |
| 5386 | |
| 5387 | if (thread == NULL) { |
| 5388 | return 0; |
| 5389 | } |
| 5390 | |
| 5391 | return thread->device(); |
| 5392 | } |
| 5393 | |
| 5394 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5395 | // ---------------------------------------------------------------------------- |
| 5396 | // Effect management |
| 5397 | // ---------------------------------------------------------------------------- |
| 5398 | |
| 5399 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5400 | status_t AudioFlinger::queryNumberEffects(uint32_t *numEffects) |
| 5401 | { |
| 5402 | Mutex::Autolock _l(mLock); |
| 5403 | return EffectQueryNumberEffects(numEffects); |
| 5404 | } |
| 5405 | |
| 5406 | status_t AudioFlinger::queryEffect(uint32_t index, effect_descriptor_t *descriptor) |
| 5407 | { |
| 5408 | Mutex::Autolock _l(mLock); |
| 5409 | return EffectQueryEffect(index, descriptor); |
| 5410 | } |
| 5411 | |
| 5412 | status_t AudioFlinger::getEffectDescriptor(effect_uuid_t *pUuid, effect_descriptor_t *descriptor) |
| 5413 | { |
| 5414 | Mutex::Autolock _l(mLock); |
| 5415 | return EffectGetDescriptor(pUuid, descriptor); |
| 5416 | } |
| 5417 | |
| 5418 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5419 | sp<IEffect> AudioFlinger::createEffect(pid_t pid, |
| 5420 | effect_descriptor_t *pDesc, |
| 5421 | const sp<IEffectClient>& effectClient, |
| 5422 | int32_t priority, |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 5423 | int io, |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5424 | int sessionId, |
| 5425 | status_t *status, |
| 5426 | int *id, |
| 5427 | int *enabled) |
| 5428 | { |
| 5429 | status_t lStatus = NO_ERROR; |
| 5430 | sp<EffectHandle> handle; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5431 | effect_descriptor_t desc; |
| 5432 | sp<Client> client; |
| 5433 | wp<Client> wclient; |
| 5434 | |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 5435 | ALOGV("createEffect pid %d, client %p, priority %d, sessionId %d, io %d", |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 5436 | pid, effectClient.get(), priority, sessionId, io); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5437 | |
| 5438 | if (pDesc == NULL) { |
| 5439 | lStatus = BAD_VALUE; |
| 5440 | goto Exit; |
| 5441 | } |
| 5442 | |
Eric Laurent | 84e9a10 | 2010-09-23 16:10:16 -0700 | [diff] [blame] | 5443 | // check audio settings permission for global effects |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 5444 | if (sessionId == AUDIO_SESSION_OUTPUT_MIX && !settingsAllowed()) { |
Eric Laurent | 84e9a10 | 2010-09-23 16:10:16 -0700 | [diff] [blame] | 5445 | lStatus = PERMISSION_DENIED; |
| 5446 | goto Exit; |
| 5447 | } |
| 5448 | |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 5449 | // Session AUDIO_SESSION_OUTPUT_STAGE is reserved for output stage effects |
Eric Laurent | 84e9a10 | 2010-09-23 16:10:16 -0700 | [diff] [blame] | 5450 | // 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] | 5451 | if (sessionId == AUDIO_SESSION_OUTPUT_STAGE && getpid() != pid) { |
Eric Laurent | 84e9a10 | 2010-09-23 16:10:16 -0700 | [diff] [blame] | 5452 | lStatus = PERMISSION_DENIED; |
| 5453 | goto Exit; |
| 5454 | } |
| 5455 | |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 5456 | if (io == 0) { |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 5457 | if (sessionId == AUDIO_SESSION_OUTPUT_STAGE) { |
Eric Laurent | 84e9a10 | 2010-09-23 16:10:16 -0700 | [diff] [blame] | 5458 | // output must be specified by AudioPolicyManager when using session |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 5459 | // AUDIO_SESSION_OUTPUT_STAGE |
Eric Laurent | 84e9a10 | 2010-09-23 16:10:16 -0700 | [diff] [blame] | 5460 | lStatus = BAD_VALUE; |
| 5461 | goto Exit; |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 5462 | } else if (sessionId == AUDIO_SESSION_OUTPUT_MIX) { |
Eric Laurent | 84e9a10 | 2010-09-23 16:10:16 -0700 | [diff] [blame] | 5463 | // if the output returned by getOutputForEffect() is removed before we lock the |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 5464 | // mutex below, the call to checkPlaybackThread_l(io) below will detect it |
Eric Laurent | 84e9a10 | 2010-09-23 16:10:16 -0700 | [diff] [blame] | 5465 | // and we will exit safely |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 5466 | io = AudioSystem::getOutputForEffect(&desc); |
Eric Laurent | 84e9a10 | 2010-09-23 16:10:16 -0700 | [diff] [blame] | 5467 | } |
| 5468 | } |
| 5469 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5470 | { |
| 5471 | Mutex::Autolock _l(mLock); |
| 5472 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5473 | |
| 5474 | if (!EffectIsNullUuid(&pDesc->uuid)) { |
| 5475 | // if uuid is specified, request effect descriptor |
| 5476 | lStatus = EffectGetDescriptor(&pDesc->uuid, &desc); |
| 5477 | if (lStatus < 0) { |
Steve Block | 5ff1dd5 | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 5478 | ALOGW("createEffect() error %d from EffectGetDescriptor", lStatus); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5479 | goto Exit; |
| 5480 | } |
| 5481 | } else { |
| 5482 | // if uuid is not specified, look for an available implementation |
| 5483 | // of the required type in effect factory |
| 5484 | if (EffectIsNullUuid(&pDesc->type)) { |
Steve Block | 5ff1dd5 | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 5485 | ALOGW("createEffect() no effect type"); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5486 | lStatus = BAD_VALUE; |
| 5487 | goto Exit; |
| 5488 | } |
| 5489 | uint32_t numEffects = 0; |
| 5490 | effect_descriptor_t d; |
Marco Nelissen | 3a34bef | 2011-08-02 13:33:41 -0700 | [diff] [blame] | 5491 | d.flags = 0; // prevent compiler warning |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5492 | bool found = false; |
| 5493 | |
| 5494 | lStatus = EffectQueryNumberEffects(&numEffects); |
| 5495 | if (lStatus < 0) { |
Steve Block | 5ff1dd5 | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 5496 | ALOGW("createEffect() error %d from EffectQueryNumberEffects", lStatus); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5497 | goto Exit; |
| 5498 | } |
| 5499 | for (uint32_t i = 0; i < numEffects; i++) { |
| 5500 | lStatus = EffectQueryEffect(i, &desc); |
| 5501 | if (lStatus < 0) { |
Steve Block | 5ff1dd5 | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 5502 | ALOGW("createEffect() error %d from EffectQueryEffect", lStatus); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5503 | continue; |
| 5504 | } |
| 5505 | if (memcmp(&desc.type, &pDesc->type, sizeof(effect_uuid_t)) == 0) { |
| 5506 | // If matching type found save effect descriptor. If the session is |
| 5507 | // 0 and the effect is not auxiliary, continue enumeration in case |
| 5508 | // an auxiliary version of this effect type is available |
| 5509 | found = true; |
| 5510 | memcpy(&d, &desc, sizeof(effect_descriptor_t)); |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 5511 | if (sessionId != AUDIO_SESSION_OUTPUT_MIX || |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5512 | (desc.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) { |
| 5513 | break; |
| 5514 | } |
| 5515 | } |
| 5516 | } |
| 5517 | if (!found) { |
| 5518 | lStatus = BAD_VALUE; |
Steve Block | 5ff1dd5 | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 5519 | ALOGW("createEffect() effect not found"); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5520 | goto Exit; |
| 5521 | } |
| 5522 | // For same effect type, chose auxiliary version over insert version if |
| 5523 | // connect to output mix (Compliance to OpenSL ES) |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 5524 | if (sessionId == AUDIO_SESSION_OUTPUT_MIX && |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5525 | (d.flags & EFFECT_FLAG_TYPE_MASK) != EFFECT_FLAG_TYPE_AUXILIARY) { |
| 5526 | memcpy(&desc, &d, sizeof(effect_descriptor_t)); |
| 5527 | } |
| 5528 | } |
| 5529 | |
| 5530 | // 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] | 5531 | if (sessionId != AUDIO_SESSION_OUTPUT_MIX && |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5532 | (desc.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) { |
| 5533 | lStatus = INVALID_OPERATION; |
| 5534 | goto Exit; |
| 5535 | } |
| 5536 | |
Eric Laurent | 59255e4 | 2011-07-27 19:49:51 -0700 | [diff] [blame] | 5537 | // check recording permission for visualizer |
| 5538 | if ((memcmp(&desc.type, SL_IID_VISUALIZATION, sizeof(effect_uuid_t)) == 0) && |
| 5539 | !recordingAllowed()) { |
| 5540 | lStatus = PERMISSION_DENIED; |
| 5541 | goto Exit; |
| 5542 | } |
| 5543 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5544 | // return effect descriptor |
| 5545 | memcpy(pDesc, &desc, sizeof(effect_descriptor_t)); |
| 5546 | |
| 5547 | // If output is not specified try to find a matching audio session ID in one of the |
| 5548 | // output threads. |
Eric Laurent | 84e9a10 | 2010-09-23 16:10:16 -0700 | [diff] [blame] | 5549 | // If output is 0 here, sessionId is neither SESSION_OUTPUT_STAGE nor SESSION_OUTPUT_MIX |
| 5550 | // because of code checking output when entering the function. |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 5551 | // Note: io is never 0 when creating an effect on an input |
| 5552 | if (io == 0) { |
Eric Laurent | 84e9a10 | 2010-09-23 16:10:16 -0700 | [diff] [blame] | 5553 | // look for the thread where the specified audio session is present |
| 5554 | for (size_t i = 0; i < mPlaybackThreads.size(); i++) { |
| 5555 | if (mPlaybackThreads.valueAt(i)->hasAudioSession(sessionId) != 0) { |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 5556 | io = mPlaybackThreads.keyAt(i); |
Eric Laurent | 84e9a10 | 2010-09-23 16:10:16 -0700 | [diff] [blame] | 5557 | break; |
Eric Laurent | 39e94f8 | 2010-07-28 01:32:47 -0700 | [diff] [blame] | 5558 | } |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5559 | } |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 5560 | if (io == 0) { |
| 5561 | for (size_t i = 0; i < mRecordThreads.size(); i++) { |
| 5562 | if (mRecordThreads.valueAt(i)->hasAudioSession(sessionId) != 0) { |
| 5563 | io = mRecordThreads.keyAt(i); |
| 5564 | break; |
| 5565 | } |
| 5566 | } |
| 5567 | } |
Eric Laurent | 84e9a10 | 2010-09-23 16:10:16 -0700 | [diff] [blame] | 5568 | // If no output thread contains the requested session ID, default to |
| 5569 | // first output. The effect chain will be moved to the correct output |
| 5570 | // thread when a track with the same session ID is created |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 5571 | if (io == 0 && mPlaybackThreads.size()) { |
| 5572 | io = mPlaybackThreads.keyAt(0); |
| 5573 | } |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 5574 | ALOGV("createEffect() got io %d for effect %s", io, desc.name); |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 5575 | } |
| 5576 | ThreadBase *thread = checkRecordThread_l(io); |
| 5577 | if (thread == NULL) { |
| 5578 | thread = checkPlaybackThread_l(io); |
| 5579 | if (thread == NULL) { |
Steve Block | 29357bc | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 5580 | ALOGE("createEffect() unknown output thread"); |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 5581 | lStatus = BAD_VALUE; |
| 5582 | goto Exit; |
Eric Laurent | 84e9a10 | 2010-09-23 16:10:16 -0700 | [diff] [blame] | 5583 | } |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5584 | } |
Eric Laurent | 84e9a10 | 2010-09-23 16:10:16 -0700 | [diff] [blame] | 5585 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5586 | wclient = mClients.valueFor(pid); |
| 5587 | |
| 5588 | if (wclient != NULL) { |
| 5589 | client = wclient.promote(); |
| 5590 | } else { |
| 5591 | client = new Client(this, pid); |
| 5592 | mClients.add(pid, client); |
| 5593 | } |
| 5594 | |
Marco Nelissen | 3a34bef | 2011-08-02 13:33:41 -0700 | [diff] [blame] | 5595 | // create effect on selected output thread |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 5596 | handle = thread->createEffect_l(client, effectClient, priority, sessionId, |
| 5597 | &desc, enabled, &lStatus); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5598 | if (handle != 0 && id != NULL) { |
| 5599 | *id = handle->id(); |
| 5600 | } |
| 5601 | } |
| 5602 | |
| 5603 | Exit: |
| 5604 | if(status) { |
| 5605 | *status = lStatus; |
| 5606 | } |
| 5607 | return handle; |
| 5608 | } |
| 5609 | |
Eric Laurent | 59255e4 | 2011-07-27 19:49:51 -0700 | [diff] [blame] | 5610 | status_t AudioFlinger::moveEffects(int sessionId, int srcOutput, int dstOutput) |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 5611 | { |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 5612 | ALOGV("moveEffects() session %d, srcOutput %d, dstOutput %d", |
Eric Laurent | 59255e4 | 2011-07-27 19:49:51 -0700 | [diff] [blame] | 5613 | sessionId, srcOutput, dstOutput); |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 5614 | Mutex::Autolock _l(mLock); |
| 5615 | if (srcOutput == dstOutput) { |
Steve Block | 5ff1dd5 | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 5616 | ALOGW("moveEffects() same dst and src outputs %d", dstOutput); |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 5617 | return NO_ERROR; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5618 | } |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 5619 | PlaybackThread *srcThread = checkPlaybackThread_l(srcOutput); |
| 5620 | if (srcThread == NULL) { |
Steve Block | 5ff1dd5 | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 5621 | ALOGW("moveEffects() bad srcOutput %d", srcOutput); |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 5622 | return BAD_VALUE; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5623 | } |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 5624 | PlaybackThread *dstThread = checkPlaybackThread_l(dstOutput); |
| 5625 | if (dstThread == NULL) { |
Steve Block | 5ff1dd5 | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 5626 | ALOGW("moveEffects() bad dstOutput %d", dstOutput); |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 5627 | return BAD_VALUE; |
| 5628 | } |
| 5629 | |
| 5630 | Mutex::Autolock _dl(dstThread->mLock); |
| 5631 | Mutex::Autolock _sl(srcThread->mLock); |
Eric Laurent | 59255e4 | 2011-07-27 19:49:51 -0700 | [diff] [blame] | 5632 | moveEffectChain_l(sessionId, srcThread, dstThread, false); |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 5633 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5634 | return NO_ERROR; |
| 5635 | } |
| 5636 | |
Marco Nelissen | 3a34bef | 2011-08-02 13:33:41 -0700 | [diff] [blame] | 5637 | // moveEffectChain_l must be called with both srcThread and dstThread mLocks held |
Eric Laurent | 59255e4 | 2011-07-27 19:49:51 -0700 | [diff] [blame] | 5638 | status_t AudioFlinger::moveEffectChain_l(int sessionId, |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 5639 | AudioFlinger::PlaybackThread *srcThread, |
Eric Laurent | 39e94f8 | 2010-07-28 01:32:47 -0700 | [diff] [blame] | 5640 | AudioFlinger::PlaybackThread *dstThread, |
| 5641 | bool reRegister) |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 5642 | { |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 5643 | ALOGV("moveEffectChain_l() session %d from thread %p to thread %p", |
Eric Laurent | 59255e4 | 2011-07-27 19:49:51 -0700 | [diff] [blame] | 5644 | sessionId, srcThread, dstThread); |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 5645 | |
Eric Laurent | 59255e4 | 2011-07-27 19:49:51 -0700 | [diff] [blame] | 5646 | sp<EffectChain> chain = srcThread->getEffectChain_l(sessionId); |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 5647 | if (chain == 0) { |
Steve Block | 5ff1dd5 | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 5648 | 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] | 5649 | sessionId, srcThread); |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 5650 | return INVALID_OPERATION; |
| 5651 | } |
| 5652 | |
Eric Laurent | 39e94f8 | 2010-07-28 01:32:47 -0700 | [diff] [blame] | 5653 | // 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] | 5654 | // 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] | 5655 | // 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] | 5656 | // removed. |
| 5657 | srcThread->removeEffectChain_l(chain); |
| 5658 | |
| 5659 | // transfer all effects one by one so that new effect chain is created on new thread with |
| 5660 | // correct buffer sizes and audio parameters and effect engines reconfigured accordingly |
Eric Laurent | 39e94f8 | 2010-07-28 01:32:47 -0700 | [diff] [blame] | 5661 | int dstOutput = dstThread->id(); |
| 5662 | sp<EffectChain> dstChain; |
Marco Nelissen | 3a34bef | 2011-08-02 13:33:41 -0700 | [diff] [blame] | 5663 | uint32_t strategy = 0; // prevent compiler warning |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 5664 | sp<EffectModule> effect = chain->getEffectFromId_l(0); |
| 5665 | while (effect != 0) { |
| 5666 | srcThread->removeEffect_l(effect); |
| 5667 | dstThread->addEffect_l(effect); |
Eric Laurent | ec35a14 | 2011-10-05 17:42:25 -0700 | [diff] [blame] | 5668 | // removeEffect_l() has stopped the effect if it was active so it must be restarted |
| 5669 | if (effect->state() == EffectModule::ACTIVE || |
| 5670 | effect->state() == EffectModule::STOPPING) { |
| 5671 | effect->start(); |
| 5672 | } |
Eric Laurent | 39e94f8 | 2010-07-28 01:32:47 -0700 | [diff] [blame] | 5673 | // if the move request is not received from audio policy manager, the effect must be |
| 5674 | // re-registered with the new strategy and output |
| 5675 | if (dstChain == 0) { |
| 5676 | dstChain = effect->chain().promote(); |
| 5677 | if (dstChain == 0) { |
Steve Block | 5ff1dd5 | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 5678 | ALOGW("moveEffectChain_l() cannot get chain from effect %p", effect.get()); |
Eric Laurent | 39e94f8 | 2010-07-28 01:32:47 -0700 | [diff] [blame] | 5679 | srcThread->addEffect_l(effect); |
| 5680 | return NO_INIT; |
| 5681 | } |
| 5682 | strategy = dstChain->strategy(); |
| 5683 | } |
| 5684 | if (reRegister) { |
| 5685 | AudioSystem::unregisterEffect(effect->id()); |
| 5686 | AudioSystem::registerEffect(&effect->desc(), |
| 5687 | dstOutput, |
| 5688 | strategy, |
Eric Laurent | 59255e4 | 2011-07-27 19:49:51 -0700 | [diff] [blame] | 5689 | sessionId, |
Eric Laurent | 39e94f8 | 2010-07-28 01:32:47 -0700 | [diff] [blame] | 5690 | effect->id()); |
| 5691 | } |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 5692 | effect = chain->getEffectFromId_l(0); |
| 5693 | } |
| 5694 | |
| 5695 | return NO_ERROR; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5696 | } |
| 5697 | |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 5698 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5699 | // PlaybackThread::createEffect_l() must be called with AudioFlinger::mLock held |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 5700 | sp<AudioFlinger::EffectHandle> AudioFlinger::ThreadBase::createEffect_l( |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5701 | const sp<AudioFlinger::Client>& client, |
| 5702 | const sp<IEffectClient>& effectClient, |
| 5703 | int32_t priority, |
| 5704 | int sessionId, |
| 5705 | effect_descriptor_t *desc, |
| 5706 | int *enabled, |
| 5707 | status_t *status |
| 5708 | ) |
| 5709 | { |
| 5710 | sp<EffectModule> effect; |
| 5711 | sp<EffectHandle> handle; |
| 5712 | status_t lStatus; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5713 | sp<EffectChain> chain; |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 5714 | bool chainCreated = false; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5715 | bool effectCreated = false; |
| 5716 | bool effectRegistered = false; |
| 5717 | |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 5718 | lStatus = initCheck(); |
| 5719 | if (lStatus != NO_ERROR) { |
Steve Block | 5ff1dd5 | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 5720 | ALOGW("createEffect_l() Audio driver not initialized."); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5721 | goto Exit; |
| 5722 | } |
| 5723 | |
| 5724 | // Do not allow effects with session ID 0 on direct output or duplicating threads |
| 5725 | // 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] | 5726 | if (sessionId == AUDIO_SESSION_OUTPUT_MIX && mType != MIXER) { |
Steve Block | 5ff1dd5 | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 5727 | ALOGW("createEffect_l() Cannot add auxiliary effect %s to session %d", |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 5728 | desc->name, sessionId); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5729 | lStatus = BAD_VALUE; |
| 5730 | goto Exit; |
| 5731 | } |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 5732 | // Only Pre processor effects are allowed on input threads and only on input threads |
| 5733 | if ((mType == RECORD && |
| 5734 | (desc->flags & EFFECT_FLAG_TYPE_MASK) != EFFECT_FLAG_TYPE_PRE_PROC) || |
| 5735 | (mType != RECORD && |
| 5736 | (desc->flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_PRE_PROC)) { |
Steve Block | 5ff1dd5 | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 5737 | 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] | 5738 | desc->name, desc->flags, mType); |
| 5739 | lStatus = BAD_VALUE; |
| 5740 | goto Exit; |
| 5741 | } |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5742 | |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 5743 | 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] | 5744 | |
| 5745 | { // scope for mLock |
| 5746 | Mutex::Autolock _l(mLock); |
| 5747 | |
| 5748 | // check for existing effect chain with the requested audio session |
| 5749 | chain = getEffectChain_l(sessionId); |
| 5750 | if (chain == 0) { |
| 5751 | // create a new chain for this session |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 5752 | ALOGV("createEffect_l() new effect chain for session %d", sessionId); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5753 | chain = new EffectChain(this, sessionId); |
| 5754 | addEffectChain_l(chain); |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 5755 | chain->setStrategy(getStrategyForSession_l(sessionId)); |
| 5756 | chainCreated = true; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5757 | } else { |
Eric Laurent | cab1124 | 2010-07-15 12:50:15 -0700 | [diff] [blame] | 5758 | effect = chain->getEffectFromDesc_l(desc); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5759 | } |
| 5760 | |
Glenn Kasten | 7fc9a6f | 2012-01-10 10:46:34 -0800 | [diff] [blame] | 5761 | 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] | 5762 | |
| 5763 | if (effect == 0) { |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 5764 | int id = mAudioFlinger->nextUniqueId(); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5765 | // Check CPU and memory usage |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 5766 | lStatus = AudioSystem::registerEffect(desc, mId, chain->strategy(), sessionId, id); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5767 | if (lStatus != NO_ERROR) { |
| 5768 | goto Exit; |
| 5769 | } |
| 5770 | effectRegistered = true; |
| 5771 | // create a new effect module if none present in the chain |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 5772 | effect = new EffectModule(this, chain, desc, id, sessionId); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5773 | lStatus = effect->status(); |
| 5774 | if (lStatus != NO_ERROR) { |
| 5775 | goto Exit; |
| 5776 | } |
Eric Laurent | cab1124 | 2010-07-15 12:50:15 -0700 | [diff] [blame] | 5777 | lStatus = chain->addEffect_l(effect); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5778 | if (lStatus != NO_ERROR) { |
| 5779 | goto Exit; |
| 5780 | } |
| 5781 | effectCreated = true; |
| 5782 | |
| 5783 | effect->setDevice(mDevice); |
| 5784 | effect->setMode(mAudioFlinger->getMode()); |
| 5785 | } |
| 5786 | // create effect handle and connect it to effect module |
| 5787 | handle = new EffectHandle(effect, client, effectClient, priority); |
| 5788 | lStatus = effect->addHandle(handle); |
| 5789 | if (enabled) { |
| 5790 | *enabled = (int)effect->isEnabled(); |
| 5791 | } |
| 5792 | } |
| 5793 | |
| 5794 | Exit: |
| 5795 | if (lStatus != NO_ERROR && lStatus != ALREADY_EXISTS) { |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 5796 | Mutex::Autolock _l(mLock); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5797 | if (effectCreated) { |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 5798 | chain->removeEffect_l(effect); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5799 | } |
| 5800 | if (effectRegistered) { |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 5801 | AudioSystem::unregisterEffect(effect->id()); |
| 5802 | } |
| 5803 | if (chainCreated) { |
| 5804 | removeEffectChain_l(chain); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5805 | } |
| 5806 | handle.clear(); |
| 5807 | } |
| 5808 | |
| 5809 | if(status) { |
| 5810 | *status = lStatus; |
| 5811 | } |
| 5812 | return handle; |
| 5813 | } |
| 5814 | |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 5815 | sp<AudioFlinger::EffectModule> AudioFlinger::ThreadBase::getEffect_l(int sessionId, int effectId) |
| 5816 | { |
| 5817 | sp<EffectModule> effect; |
| 5818 | |
| 5819 | sp<EffectChain> chain = getEffectChain_l(sessionId); |
| 5820 | if (chain != 0) { |
| 5821 | effect = chain->getEffectFromId_l(effectId); |
| 5822 | } |
| 5823 | return effect; |
| 5824 | } |
| 5825 | |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 5826 | // PlaybackThread::addEffect_l() must be called with AudioFlinger::mLock and |
| 5827 | // PlaybackThread::mLock held |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 5828 | status_t AudioFlinger::ThreadBase::addEffect_l(const sp<EffectModule>& effect) |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 5829 | { |
| 5830 | // check for existing effect chain with the requested audio session |
| 5831 | int sessionId = effect->sessionId(); |
| 5832 | sp<EffectChain> chain = getEffectChain_l(sessionId); |
| 5833 | bool chainCreated = false; |
| 5834 | |
| 5835 | if (chain == 0) { |
| 5836 | // create a new chain for this session |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 5837 | ALOGV("addEffect_l() new effect chain for session %d", sessionId); |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 5838 | chain = new EffectChain(this, sessionId); |
| 5839 | addEffectChain_l(chain); |
| 5840 | chain->setStrategy(getStrategyForSession_l(sessionId)); |
| 5841 | chainCreated = true; |
| 5842 | } |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 5843 | 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] | 5844 | |
| 5845 | if (chain->getEffectFromId_l(effect->id()) != 0) { |
Steve Block | 5ff1dd5 | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 5846 | ALOGW("addEffect_l() %p effect %s already present in chain %p", |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 5847 | this, effect->desc().name, chain.get()); |
| 5848 | return BAD_VALUE; |
| 5849 | } |
| 5850 | |
| 5851 | status_t status = chain->addEffect_l(effect); |
| 5852 | if (status != NO_ERROR) { |
| 5853 | if (chainCreated) { |
| 5854 | removeEffectChain_l(chain); |
| 5855 | } |
| 5856 | return status; |
| 5857 | } |
| 5858 | |
| 5859 | effect->setDevice(mDevice); |
| 5860 | effect->setMode(mAudioFlinger->getMode()); |
| 5861 | return NO_ERROR; |
| 5862 | } |
| 5863 | |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 5864 | void AudioFlinger::ThreadBase::removeEffect_l(const sp<EffectModule>& effect) { |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 5865 | |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 5866 | ALOGV("removeEffect_l() %p effect %p", this, effect.get()); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5867 | effect_descriptor_t desc = effect->desc(); |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 5868 | if ((desc.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) { |
| 5869 | detachAuxEffect_l(effect->id()); |
| 5870 | } |
| 5871 | |
| 5872 | sp<EffectChain> chain = effect->chain().promote(); |
| 5873 | if (chain != 0) { |
| 5874 | // remove effect chain if removing last effect |
| 5875 | if (chain->removeEffect_l(effect) == 0) { |
| 5876 | removeEffectChain_l(chain); |
| 5877 | } |
| 5878 | } else { |
Steve Block | 5ff1dd5 | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 5879 | 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] | 5880 | } |
| 5881 | } |
| 5882 | |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 5883 | void AudioFlinger::ThreadBase::lockEffectChains_l( |
| 5884 | Vector<sp <AudioFlinger::EffectChain> >& effectChains) |
| 5885 | { |
| 5886 | effectChains = mEffectChains; |
| 5887 | for (size_t i = 0; i < mEffectChains.size(); i++) { |
| 5888 | mEffectChains[i]->lock(); |
| 5889 | } |
| 5890 | } |
| 5891 | |
| 5892 | void AudioFlinger::ThreadBase::unlockEffectChains( |
| 5893 | Vector<sp <AudioFlinger::EffectChain> >& effectChains) |
| 5894 | { |
| 5895 | for (size_t i = 0; i < effectChains.size(); i++) { |
| 5896 | effectChains[i]->unlock(); |
| 5897 | } |
| 5898 | } |
| 5899 | |
| 5900 | sp<AudioFlinger::EffectChain> AudioFlinger::ThreadBase::getEffectChain(int sessionId) |
| 5901 | { |
| 5902 | Mutex::Autolock _l(mLock); |
| 5903 | return getEffectChain_l(sessionId); |
| 5904 | } |
| 5905 | |
| 5906 | sp<AudioFlinger::EffectChain> AudioFlinger::ThreadBase::getEffectChain_l(int sessionId) |
| 5907 | { |
| 5908 | sp<EffectChain> chain; |
| 5909 | |
| 5910 | size_t size = mEffectChains.size(); |
| 5911 | for (size_t i = 0; i < size; i++) { |
| 5912 | if (mEffectChains[i]->sessionId() == sessionId) { |
| 5913 | chain = mEffectChains[i]; |
| 5914 | break; |
| 5915 | } |
| 5916 | } |
| 5917 | return chain; |
| 5918 | } |
| 5919 | |
Glenn Kasten | f78aee7 | 2012-01-04 11:00:47 -0800 | [diff] [blame] | 5920 | void AudioFlinger::ThreadBase::setMode(audio_mode_t mode) |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 5921 | { |
| 5922 | Mutex::Autolock _l(mLock); |
| 5923 | size_t size = mEffectChains.size(); |
| 5924 | for (size_t i = 0; i < size; i++) { |
| 5925 | mEffectChains[i]->setMode_l(mode); |
| 5926 | } |
| 5927 | } |
| 5928 | |
| 5929 | void AudioFlinger::ThreadBase::disconnectEffect(const sp<EffectModule>& effect, |
Marco Nelissen | 3a34bef | 2011-08-02 13:33:41 -0700 | [diff] [blame] | 5930 | const wp<EffectHandle>& handle, |
| 5931 | bool unpiniflast) { |
Eric Laurent | 59255e4 | 2011-07-27 19:49:51 -0700 | [diff] [blame] | 5932 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5933 | Mutex::Autolock _l(mLock); |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 5934 | ALOGV("disconnectEffect() %p effect %p", this, effect.get()); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5935 | // delete the effect module if removing last handle on it |
| 5936 | if (effect->removeHandle(handle) == 0) { |
Marco Nelissen | 3a34bef | 2011-08-02 13:33:41 -0700 | [diff] [blame] | 5937 | if (!effect->isPinned() || unpiniflast) { |
| 5938 | removeEffect_l(effect); |
| 5939 | AudioSystem::unregisterEffect(effect->id()); |
| 5940 | } |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5941 | } |
| 5942 | } |
| 5943 | |
| 5944 | status_t AudioFlinger::PlaybackThread::addEffectChain_l(const sp<EffectChain>& chain) |
| 5945 | { |
| 5946 | int session = chain->sessionId(); |
| 5947 | int16_t *buffer = mMixBuffer; |
| 5948 | bool ownsBuffer = false; |
| 5949 | |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 5950 | 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] | 5951 | if (session > 0) { |
| 5952 | // Only one effect chain can be present in direct output thread and it uses |
| 5953 | // the mix buffer as input |
| 5954 | if (mType != DIRECT) { |
| 5955 | size_t numSamples = mFrameCount * mChannelCount; |
| 5956 | buffer = new int16_t[numSamples]; |
| 5957 | memset(buffer, 0, numSamples * sizeof(int16_t)); |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 5958 | ALOGV("addEffectChain_l() creating new input buffer %p session %d", buffer, session); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5959 | ownsBuffer = true; |
| 5960 | } |
| 5961 | |
| 5962 | // Attach all tracks with same session ID to this chain. |
| 5963 | for (size_t i = 0; i < mTracks.size(); ++i) { |
| 5964 | sp<Track> track = mTracks[i]; |
| 5965 | if (session == track->sessionId()) { |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 5966 | ALOGV("addEffectChain_l() track->setMainBuffer track %p buffer %p", track.get(), buffer); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5967 | track->setMainBuffer(buffer); |
Eric Laurent | b469b94 | 2011-05-09 12:09:06 -0700 | [diff] [blame] | 5968 | chain->incTrackCnt(); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5969 | } |
| 5970 | } |
| 5971 | |
| 5972 | // indicate all active tracks in the chain |
| 5973 | for (size_t i = 0 ; i < mActiveTracks.size() ; ++i) { |
| 5974 | sp<Track> track = mActiveTracks[i].promote(); |
| 5975 | if (track == 0) continue; |
| 5976 | if (session == track->sessionId()) { |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 5977 | ALOGV("addEffectChain_l() activating track %p on session %d", track.get(), session); |
Eric Laurent | b469b94 | 2011-05-09 12:09:06 -0700 | [diff] [blame] | 5978 | chain->incActiveTrackCnt(); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5979 | } |
| 5980 | } |
| 5981 | } |
| 5982 | |
| 5983 | chain->setInBuffer(buffer, ownsBuffer); |
| 5984 | chain->setOutBuffer(mMixBuffer); |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 5985 | // 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] | 5986 | // 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] | 5987 | // Effect chain for session AUDIO_SESSION_OUTPUT_MIX is inserted before |
| 5988 | // session AUDIO_SESSION_OUTPUT_STAGE to be processed |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5989 | // after track specific effects and before output stage |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 5990 | // It is therefore mandatory that AUDIO_SESSION_OUTPUT_MIX == 0 and |
| 5991 | // that AUDIO_SESSION_OUTPUT_STAGE < AUDIO_SESSION_OUTPUT_MIX |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 5992 | // Effect chain for other sessions are inserted at beginning of effect |
| 5993 | // chains list to be processed before output mix effects. Relative order between other |
| 5994 | // sessions is not important |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5995 | size_t size = mEffectChains.size(); |
| 5996 | size_t i = 0; |
| 5997 | for (i = 0; i < size; i++) { |
| 5998 | if (mEffectChains[i]->sessionId() < session) break; |
| 5999 | } |
| 6000 | mEffectChains.insertAt(chain, i); |
Eric Laurent | 59255e4 | 2011-07-27 19:49:51 -0700 | [diff] [blame] | 6001 | checkSuspendOnAddEffectChain_l(chain); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6002 | |
| 6003 | return NO_ERROR; |
| 6004 | } |
| 6005 | |
| 6006 | size_t AudioFlinger::PlaybackThread::removeEffectChain_l(const sp<EffectChain>& chain) |
| 6007 | { |
| 6008 | int session = chain->sessionId(); |
| 6009 | |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 6010 | 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] | 6011 | |
| 6012 | for (size_t i = 0; i < mEffectChains.size(); i++) { |
| 6013 | if (chain == mEffectChains[i]) { |
| 6014 | mEffectChains.removeAt(i); |
Eric Laurent | b469b94 | 2011-05-09 12:09:06 -0700 | [diff] [blame] | 6015 | // detach all active tracks from the chain |
| 6016 | for (size_t i = 0 ; i < mActiveTracks.size() ; ++i) { |
| 6017 | sp<Track> track = mActiveTracks[i].promote(); |
| 6018 | if (track == 0) continue; |
| 6019 | if (session == track->sessionId()) { |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 6020 | ALOGV("removeEffectChain_l(): stopping track on chain %p for session Id: %d", |
Eric Laurent | b469b94 | 2011-05-09 12:09:06 -0700 | [diff] [blame] | 6021 | chain.get(), session); |
| 6022 | chain->decActiveTrackCnt(); |
| 6023 | } |
| 6024 | } |
| 6025 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6026 | // detach all tracks with same session ID from this chain |
| 6027 | for (size_t i = 0; i < mTracks.size(); ++i) { |
| 6028 | sp<Track> track = mTracks[i]; |
| 6029 | if (session == track->sessionId()) { |
| 6030 | track->setMainBuffer(mMixBuffer); |
Eric Laurent | b469b94 | 2011-05-09 12:09:06 -0700 | [diff] [blame] | 6031 | chain->decTrackCnt(); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6032 | } |
| 6033 | } |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 6034 | break; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6035 | } |
| 6036 | } |
| 6037 | return mEffectChains.size(); |
| 6038 | } |
| 6039 | |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 6040 | status_t AudioFlinger::PlaybackThread::attachAuxEffect( |
| 6041 | const sp<AudioFlinger::PlaybackThread::Track> track, int EffectId) |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6042 | { |
| 6043 | Mutex::Autolock _l(mLock); |
| 6044 | return attachAuxEffect_l(track, EffectId); |
| 6045 | } |
| 6046 | |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 6047 | status_t AudioFlinger::PlaybackThread::attachAuxEffect_l( |
| 6048 | const sp<AudioFlinger::PlaybackThread::Track> track, int EffectId) |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6049 | { |
| 6050 | status_t status = NO_ERROR; |
| 6051 | |
| 6052 | if (EffectId == 0) { |
| 6053 | track->setAuxBuffer(0, NULL); |
| 6054 | } else { |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 6055 | // Auxiliary effects are always in audio session AUDIO_SESSION_OUTPUT_MIX |
| 6056 | sp<EffectModule> effect = getEffect_l(AUDIO_SESSION_OUTPUT_MIX, EffectId); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6057 | if (effect != 0) { |
| 6058 | if ((effect->desc().flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) { |
| 6059 | track->setAuxBuffer(EffectId, (int32_t *)effect->inBuffer()); |
| 6060 | } else { |
| 6061 | status = INVALID_OPERATION; |
| 6062 | } |
| 6063 | } else { |
| 6064 | status = BAD_VALUE; |
| 6065 | } |
| 6066 | } |
| 6067 | return status; |
| 6068 | } |
| 6069 | |
| 6070 | void AudioFlinger::PlaybackThread::detachAuxEffect_l(int effectId) |
| 6071 | { |
| 6072 | for (size_t i = 0; i < mTracks.size(); ++i) { |
| 6073 | sp<Track> track = mTracks[i]; |
| 6074 | if (track->auxEffectId() == effectId) { |
| 6075 | attachAuxEffect_l(track, 0); |
| 6076 | } |
| 6077 | } |
| 6078 | } |
| 6079 | |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 6080 | status_t AudioFlinger::RecordThread::addEffectChain_l(const sp<EffectChain>& chain) |
| 6081 | { |
| 6082 | // only one chain per input thread |
| 6083 | if (mEffectChains.size() != 0) { |
| 6084 | return INVALID_OPERATION; |
| 6085 | } |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 6086 | ALOGV("addEffectChain_l() %p on thread %p", chain.get(), this); |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 6087 | |
| 6088 | chain->setInBuffer(NULL); |
| 6089 | chain->setOutBuffer(NULL); |
| 6090 | |
Eric Laurent | 59255e4 | 2011-07-27 19:49:51 -0700 | [diff] [blame] | 6091 | checkSuspendOnAddEffectChain_l(chain); |
| 6092 | |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 6093 | mEffectChains.add(chain); |
| 6094 | |
| 6095 | return NO_ERROR; |
| 6096 | } |
| 6097 | |
| 6098 | size_t AudioFlinger::RecordThread::removeEffectChain_l(const sp<EffectChain>& chain) |
| 6099 | { |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 6100 | ALOGV("removeEffectChain_l() %p from thread %p", chain.get(), this); |
Steve Block | 5ff1dd5 | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 6101 | ALOGW_IF(mEffectChains.size() != 1, |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 6102 | "removeEffectChain_l() %p invalid chain size %d on thread %p", |
| 6103 | chain.get(), mEffectChains.size(), this); |
| 6104 | if (mEffectChains.size() == 1) { |
| 6105 | mEffectChains.removeAt(0); |
| 6106 | } |
| 6107 | return 0; |
| 6108 | } |
| 6109 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6110 | // ---------------------------------------------------------------------------- |
| 6111 | // EffectModule implementation |
| 6112 | // ---------------------------------------------------------------------------- |
| 6113 | |
| 6114 | #undef LOG_TAG |
| 6115 | #define LOG_TAG "AudioFlinger::EffectModule" |
| 6116 | |
| 6117 | AudioFlinger::EffectModule::EffectModule(const wp<ThreadBase>& wThread, |
| 6118 | const wp<AudioFlinger::EffectChain>& chain, |
| 6119 | effect_descriptor_t *desc, |
| 6120 | int id, |
| 6121 | int sessionId) |
| 6122 | : mThread(wThread), mChain(chain), mId(id), mSessionId(sessionId), mEffectInterface(NULL), |
Eric Laurent | 59255e4 | 2011-07-27 19:49:51 -0700 | [diff] [blame] | 6123 | mStatus(NO_INIT), mState(IDLE), mSuspended(false) |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6124 | { |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 6125 | ALOGV("Constructor %p", this); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6126 | int lStatus; |
| 6127 | sp<ThreadBase> thread = mThread.promote(); |
| 6128 | if (thread == 0) { |
| 6129 | return; |
| 6130 | } |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6131 | |
| 6132 | memcpy(&mDescriptor, desc, sizeof(effect_descriptor_t)); |
| 6133 | |
| 6134 | // create effect engine from effect factory |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 6135 | mStatus = EffectCreate(&desc->uuid, sessionId, thread->id(), &mEffectInterface); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6136 | |
| 6137 | if (mStatus != NO_ERROR) { |
| 6138 | return; |
| 6139 | } |
| 6140 | lStatus = init(); |
| 6141 | if (lStatus < 0) { |
| 6142 | mStatus = lStatus; |
| 6143 | goto Error; |
| 6144 | } |
| 6145 | |
Marco Nelissen | 3a34bef | 2011-08-02 13:33:41 -0700 | [diff] [blame] | 6146 | if (mSessionId > AUDIO_SESSION_OUTPUT_MIX) { |
| 6147 | mPinned = true; |
| 6148 | } |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 6149 | ALOGV("Constructor success name %s, Interface %p", mDescriptor.name, mEffectInterface); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6150 | return; |
| 6151 | Error: |
| 6152 | EffectRelease(mEffectInterface); |
| 6153 | mEffectInterface = NULL; |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 6154 | ALOGV("Constructor Error %d", mStatus); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6155 | } |
| 6156 | |
| 6157 | AudioFlinger::EffectModule::~EffectModule() |
| 6158 | { |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 6159 | ALOGV("Destructor %p", this); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6160 | if (mEffectInterface != NULL) { |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 6161 | if ((mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_PRE_PROC || |
| 6162 | (mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_POST_PROC) { |
| 6163 | sp<ThreadBase> thread = mThread.promote(); |
| 6164 | if (thread != 0) { |
Eric Laurent | b8ba0a9 | 2011-08-07 16:32:26 -0700 | [diff] [blame] | 6165 | audio_stream_t *stream = thread->stream(); |
| 6166 | if (stream != NULL) { |
| 6167 | stream->remove_audio_effect(stream, mEffectInterface); |
| 6168 | } |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 6169 | } |
| 6170 | } |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6171 | // release effect engine |
| 6172 | EffectRelease(mEffectInterface); |
| 6173 | } |
| 6174 | } |
| 6175 | |
| 6176 | status_t AudioFlinger::EffectModule::addHandle(sp<EffectHandle>& handle) |
| 6177 | { |
| 6178 | status_t status; |
| 6179 | |
| 6180 | Mutex::Autolock _l(mLock); |
| 6181 | // First handle in mHandles has highest priority and controls the effect module |
| 6182 | int priority = handle->priority(); |
| 6183 | size_t size = mHandles.size(); |
| 6184 | sp<EffectHandle> h; |
| 6185 | size_t i; |
| 6186 | for (i = 0; i < size; i++) { |
| 6187 | h = mHandles[i].promote(); |
| 6188 | if (h == 0) continue; |
| 6189 | if (h->priority() <= priority) break; |
| 6190 | } |
| 6191 | // if inserted in first place, move effect control from previous owner to this handle |
| 6192 | if (i == 0) { |
Eric Laurent | 59255e4 | 2011-07-27 19:49:51 -0700 | [diff] [blame] | 6193 | bool enabled = false; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6194 | if (h != 0) { |
Eric Laurent | 59255e4 | 2011-07-27 19:49:51 -0700 | [diff] [blame] | 6195 | enabled = h->enabled(); |
| 6196 | h->setControl(false/*hasControl*/, true /*signal*/, enabled /*enabled*/); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6197 | } |
Eric Laurent | 59255e4 | 2011-07-27 19:49:51 -0700 | [diff] [blame] | 6198 | handle->setControl(true /*hasControl*/, false /*signal*/, enabled /*enabled*/); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6199 | status = NO_ERROR; |
| 6200 | } else { |
| 6201 | status = ALREADY_EXISTS; |
| 6202 | } |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 6203 | 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] | 6204 | mHandles.insertAt(handle, i); |
| 6205 | return status; |
| 6206 | } |
| 6207 | |
| 6208 | size_t AudioFlinger::EffectModule::removeHandle(const wp<EffectHandle>& handle) |
| 6209 | { |
| 6210 | Mutex::Autolock _l(mLock); |
| 6211 | size_t size = mHandles.size(); |
| 6212 | size_t i; |
| 6213 | for (i = 0; i < size; i++) { |
| 6214 | if (mHandles[i] == handle) break; |
| 6215 | } |
| 6216 | if (i == size) { |
| 6217 | return size; |
| 6218 | } |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 6219 | 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] | 6220 | |
| 6221 | bool enabled = false; |
| 6222 | EffectHandle *hdl = handle.unsafe_get(); |
| 6223 | if (hdl) { |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 6224 | ALOGV("removeHandle() unsafe_get OK"); |
Eric Laurent | 59255e4 | 2011-07-27 19:49:51 -0700 | [diff] [blame] | 6225 | enabled = hdl->enabled(); |
| 6226 | } |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6227 | mHandles.removeAt(i); |
| 6228 | size = mHandles.size(); |
| 6229 | // if removed from first place, move effect control from this handle to next in line |
| 6230 | if (i == 0 && size != 0) { |
| 6231 | sp<EffectHandle> h = mHandles[0].promote(); |
| 6232 | if (h != 0) { |
Eric Laurent | 59255e4 | 2011-07-27 19:49:51 -0700 | [diff] [blame] | 6233 | h->setControl(true /*hasControl*/, true /*signal*/ , enabled /*enabled*/); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6234 | } |
| 6235 | } |
| 6236 | |
Eric Laurent | ec437d8 | 2011-07-26 20:54:46 -0700 | [diff] [blame] | 6237 | // Prevent calls to process() and other functions on effect interface from now on. |
| 6238 | // The effect engine will be released by the destructor when the last strong reference on |
| 6239 | // this object is released which can happen after next process is called. |
Marco Nelissen | 3a34bef | 2011-08-02 13:33:41 -0700 | [diff] [blame] | 6240 | if (size == 0 && !mPinned) { |
Eric Laurent | ec437d8 | 2011-07-26 20:54:46 -0700 | [diff] [blame] | 6241 | mState = DESTROYED; |
Eric Laurent | dac6911 | 2010-09-28 14:09:57 -0700 | [diff] [blame] | 6242 | } |
| 6243 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6244 | return size; |
| 6245 | } |
| 6246 | |
Eric Laurent | 59255e4 | 2011-07-27 19:49:51 -0700 | [diff] [blame] | 6247 | sp<AudioFlinger::EffectHandle> AudioFlinger::EffectModule::controlHandle() |
| 6248 | { |
| 6249 | Mutex::Autolock _l(mLock); |
| 6250 | sp<EffectHandle> handle; |
| 6251 | if (mHandles.size() != 0) { |
| 6252 | handle = mHandles[0].promote(); |
| 6253 | } |
| 6254 | return handle; |
| 6255 | } |
| 6256 | |
Marco Nelissen | 3a34bef | 2011-08-02 13:33:41 -0700 | [diff] [blame] | 6257 | void AudioFlinger::EffectModule::disconnect(const wp<EffectHandle>& handle, bool unpiniflast) |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6258 | { |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 6259 | ALOGV("disconnect() %p handle %p ", this, handle.unsafe_get()); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6260 | // keep a strong reference on this EffectModule to avoid calling the |
| 6261 | // destructor before we exit |
| 6262 | sp<EffectModule> keep(this); |
| 6263 | { |
| 6264 | sp<ThreadBase> thread = mThread.promote(); |
| 6265 | if (thread != 0) { |
Marco Nelissen | 3a34bef | 2011-08-02 13:33:41 -0700 | [diff] [blame] | 6266 | thread->disconnectEffect(keep, handle, unpiniflast); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6267 | } |
| 6268 | } |
| 6269 | } |
| 6270 | |
| 6271 | void AudioFlinger::EffectModule::updateState() { |
| 6272 | Mutex::Autolock _l(mLock); |
| 6273 | |
| 6274 | switch (mState) { |
| 6275 | case RESTART: |
| 6276 | reset_l(); |
| 6277 | // FALL THROUGH |
| 6278 | |
| 6279 | case STARTING: |
| 6280 | // clear auxiliary effect input buffer for next accumulation |
| 6281 | if ((mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) { |
| 6282 | memset(mConfig.inputCfg.buffer.raw, |
| 6283 | 0, |
| 6284 | mConfig.inputCfg.buffer.frameCount*sizeof(int32_t)); |
| 6285 | } |
| 6286 | start_l(); |
| 6287 | mState = ACTIVE; |
| 6288 | break; |
| 6289 | case STOPPING: |
| 6290 | stop_l(); |
| 6291 | mDisableWaitCnt = mMaxDisableWaitCnt; |
| 6292 | mState = STOPPED; |
| 6293 | break; |
| 6294 | case STOPPED: |
| 6295 | // mDisableWaitCnt is forced to 1 by process() when the engine indicates the end of the |
| 6296 | // turn off sequence. |
| 6297 | if (--mDisableWaitCnt == 0) { |
| 6298 | reset_l(); |
| 6299 | mState = IDLE; |
| 6300 | } |
| 6301 | break; |
Eric Laurent | ec437d8 | 2011-07-26 20:54:46 -0700 | [diff] [blame] | 6302 | default: //IDLE , ACTIVE, DESTROYED |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6303 | break; |
| 6304 | } |
| 6305 | } |
| 6306 | |
| 6307 | void AudioFlinger::EffectModule::process() |
| 6308 | { |
| 6309 | Mutex::Autolock _l(mLock); |
| 6310 | |
Eric Laurent | ec437d8 | 2011-07-26 20:54:46 -0700 | [diff] [blame] | 6311 | if (mState == DESTROYED || mEffectInterface == NULL || |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6312 | mConfig.inputCfg.buffer.raw == NULL || |
| 6313 | mConfig.outputCfg.buffer.raw == NULL) { |
| 6314 | return; |
| 6315 | } |
| 6316 | |
Eric Laurent | 8f45bd7 | 2010-08-31 13:50:07 -0700 | [diff] [blame] | 6317 | if (isProcessEnabled()) { |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6318 | // do 32 bit to 16 bit conversion for auxiliary effect input buffer |
| 6319 | if ((mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) { |
Glenn Kasten | 3b21c50 | 2011-12-15 09:52:39 -0800 | [diff] [blame] | 6320 | ditherAndClamp(mConfig.inputCfg.buffer.s32, |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6321 | mConfig.inputCfg.buffer.s32, |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 6322 | mConfig.inputCfg.buffer.frameCount/2); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6323 | } |
| 6324 | |
| 6325 | // do the actual processing in the effect engine |
| 6326 | int ret = (*mEffectInterface)->process(mEffectInterface, |
| 6327 | &mConfig.inputCfg.buffer, |
| 6328 | &mConfig.outputCfg.buffer); |
| 6329 | |
| 6330 | // force transition to IDLE state when engine is ready |
| 6331 | if (mState == STOPPED && ret == -ENODATA) { |
| 6332 | mDisableWaitCnt = 1; |
| 6333 | } |
| 6334 | |
| 6335 | // clear auxiliary effect input buffer for next accumulation |
| 6336 | if ((mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) { |
Eric Laurent | 7333748 | 2011-01-19 18:36:13 -0800 | [diff] [blame] | 6337 | memset(mConfig.inputCfg.buffer.raw, 0, |
| 6338 | mConfig.inputCfg.buffer.frameCount*sizeof(int32_t)); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6339 | } |
| 6340 | } else if ((mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_INSERT && |
Eric Laurent | 7333748 | 2011-01-19 18:36:13 -0800 | [diff] [blame] | 6341 | mConfig.inputCfg.buffer.raw != mConfig.outputCfg.buffer.raw) { |
| 6342 | // If an insert effect is idle and input buffer is different from output buffer, |
| 6343 | // accumulate input onto output |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6344 | sp<EffectChain> chain = mChain.promote(); |
Eric Laurent | b469b94 | 2011-05-09 12:09:06 -0700 | [diff] [blame] | 6345 | if (chain != 0 && chain->activeTrackCnt() != 0) { |
Eric Laurent | 7333748 | 2011-01-19 18:36:13 -0800 | [diff] [blame] | 6346 | size_t frameCnt = mConfig.inputCfg.buffer.frameCount * 2; //always stereo here |
| 6347 | int16_t *in = mConfig.inputCfg.buffer.s16; |
| 6348 | int16_t *out = mConfig.outputCfg.buffer.s16; |
| 6349 | for (size_t i = 0; i < frameCnt; i++) { |
| 6350 | out[i] = clamp16((int32_t)out[i] + (int32_t)in[i]); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6351 | } |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6352 | } |
| 6353 | } |
| 6354 | } |
| 6355 | |
| 6356 | void AudioFlinger::EffectModule::reset_l() |
| 6357 | { |
| 6358 | if (mEffectInterface == NULL) { |
| 6359 | return; |
| 6360 | } |
| 6361 | (*mEffectInterface)->command(mEffectInterface, EFFECT_CMD_RESET, 0, NULL, 0, NULL); |
| 6362 | } |
| 6363 | |
| 6364 | status_t AudioFlinger::EffectModule::configure() |
| 6365 | { |
| 6366 | uint32_t channels; |
| 6367 | if (mEffectInterface == NULL) { |
| 6368 | return NO_INIT; |
| 6369 | } |
| 6370 | |
| 6371 | sp<ThreadBase> thread = mThread.promote(); |
| 6372 | if (thread == 0) { |
| 6373 | return DEAD_OBJECT; |
| 6374 | } |
| 6375 | |
| 6376 | // TODO: handle configuration of effects replacing track process |
| 6377 | if (thread->channelCount() == 1) { |
Eric Laurent | e1315cf | 2011-05-17 19:16:02 -0700 | [diff] [blame] | 6378 | channels = AUDIO_CHANNEL_OUT_MONO; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6379 | } else { |
Eric Laurent | e1315cf | 2011-05-17 19:16:02 -0700 | [diff] [blame] | 6380 | channels = AUDIO_CHANNEL_OUT_STEREO; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6381 | } |
| 6382 | |
| 6383 | if ((mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) { |
Eric Laurent | e1315cf | 2011-05-17 19:16:02 -0700 | [diff] [blame] | 6384 | mConfig.inputCfg.channels = AUDIO_CHANNEL_OUT_MONO; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6385 | } else { |
| 6386 | mConfig.inputCfg.channels = channels; |
| 6387 | } |
| 6388 | mConfig.outputCfg.channels = channels; |
Eric Laurent | e1315cf | 2011-05-17 19:16:02 -0700 | [diff] [blame] | 6389 | mConfig.inputCfg.format = AUDIO_FORMAT_PCM_16_BIT; |
| 6390 | mConfig.outputCfg.format = AUDIO_FORMAT_PCM_16_BIT; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6391 | mConfig.inputCfg.samplingRate = thread->sampleRate(); |
| 6392 | mConfig.outputCfg.samplingRate = mConfig.inputCfg.samplingRate; |
| 6393 | mConfig.inputCfg.bufferProvider.cookie = NULL; |
| 6394 | mConfig.inputCfg.bufferProvider.getBuffer = NULL; |
| 6395 | mConfig.inputCfg.bufferProvider.releaseBuffer = NULL; |
| 6396 | mConfig.outputCfg.bufferProvider.cookie = NULL; |
| 6397 | mConfig.outputCfg.bufferProvider.getBuffer = NULL; |
| 6398 | mConfig.outputCfg.bufferProvider.releaseBuffer = NULL; |
| 6399 | mConfig.inputCfg.accessMode = EFFECT_BUFFER_ACCESS_READ; |
| 6400 | // Insert effect: |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 6401 | // - in session AUDIO_SESSION_OUTPUT_MIX or AUDIO_SESSION_OUTPUT_STAGE, |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 6402 | // always overwrites output buffer: input buffer == output buffer |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6403 | // - in other sessions: |
| 6404 | // last effect in the chain accumulates in output buffer: input buffer != output buffer |
| 6405 | // other effect: overwrites output buffer: input buffer == output buffer |
| 6406 | // Auxiliary effect: |
| 6407 | // accumulates in output buffer: input buffer != output buffer |
| 6408 | // Therefore: accumulate <=> input buffer != output buffer |
| 6409 | if (mConfig.inputCfg.buffer.raw != mConfig.outputCfg.buffer.raw) { |
| 6410 | mConfig.outputCfg.accessMode = EFFECT_BUFFER_ACCESS_ACCUMULATE; |
| 6411 | } else { |
| 6412 | mConfig.outputCfg.accessMode = EFFECT_BUFFER_ACCESS_WRITE; |
| 6413 | } |
| 6414 | mConfig.inputCfg.mask = EFFECT_CONFIG_ALL; |
| 6415 | mConfig.outputCfg.mask = EFFECT_CONFIG_ALL; |
| 6416 | mConfig.inputCfg.buffer.frameCount = thread->frameCount(); |
| 6417 | mConfig.outputCfg.buffer.frameCount = mConfig.inputCfg.buffer.frameCount; |
| 6418 | |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 6419 | ALOGV("configure() %p thread %p buffer %p framecount %d", |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 6420 | this, thread.get(), mConfig.inputCfg.buffer.raw, mConfig.inputCfg.buffer.frameCount); |
| 6421 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6422 | status_t cmdStatus; |
Eric Laurent | 25f4395 | 2010-07-28 05:40:18 -0700 | [diff] [blame] | 6423 | uint32_t size = sizeof(int); |
| 6424 | status_t status = (*mEffectInterface)->command(mEffectInterface, |
Eric Laurent | 3d5188b | 2011-12-16 15:30:36 -0800 | [diff] [blame] | 6425 | EFFECT_CMD_SET_CONFIG, |
Eric Laurent | 25f4395 | 2010-07-28 05:40:18 -0700 | [diff] [blame] | 6426 | sizeof(effect_config_t), |
| 6427 | &mConfig, |
| 6428 | &size, |
| 6429 | &cmdStatus); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6430 | if (status == 0) { |
| 6431 | status = cmdStatus; |
| 6432 | } |
| 6433 | |
| 6434 | mMaxDisableWaitCnt = (MAX_DISABLE_TIME_MS * mConfig.outputCfg.samplingRate) / |
| 6435 | (1000 * mConfig.outputCfg.buffer.frameCount); |
| 6436 | |
| 6437 | return status; |
| 6438 | } |
| 6439 | |
| 6440 | status_t AudioFlinger::EffectModule::init() |
| 6441 | { |
| 6442 | Mutex::Autolock _l(mLock); |
| 6443 | if (mEffectInterface == NULL) { |
| 6444 | return NO_INIT; |
| 6445 | } |
| 6446 | status_t cmdStatus; |
Eric Laurent | 25f4395 | 2010-07-28 05:40:18 -0700 | [diff] [blame] | 6447 | uint32_t size = sizeof(status_t); |
| 6448 | status_t status = (*mEffectInterface)->command(mEffectInterface, |
| 6449 | EFFECT_CMD_INIT, |
| 6450 | 0, |
| 6451 | NULL, |
| 6452 | &size, |
| 6453 | &cmdStatus); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6454 | if (status == 0) { |
| 6455 | status = cmdStatus; |
| 6456 | } |
| 6457 | return status; |
| 6458 | } |
| 6459 | |
Eric Laurent | ec35a14 | 2011-10-05 17:42:25 -0700 | [diff] [blame] | 6460 | status_t AudioFlinger::EffectModule::start() |
| 6461 | { |
| 6462 | Mutex::Autolock _l(mLock); |
| 6463 | return start_l(); |
| 6464 | } |
| 6465 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6466 | status_t AudioFlinger::EffectModule::start_l() |
| 6467 | { |
| 6468 | if (mEffectInterface == NULL) { |
| 6469 | return NO_INIT; |
| 6470 | } |
| 6471 | status_t cmdStatus; |
Eric Laurent | 25f4395 | 2010-07-28 05:40:18 -0700 | [diff] [blame] | 6472 | uint32_t size = sizeof(status_t); |
| 6473 | status_t status = (*mEffectInterface)->command(mEffectInterface, |
| 6474 | EFFECT_CMD_ENABLE, |
| 6475 | 0, |
| 6476 | NULL, |
| 6477 | &size, |
| 6478 | &cmdStatus); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6479 | if (status == 0) { |
| 6480 | status = cmdStatus; |
| 6481 | } |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 6482 | if (status == 0 && |
| 6483 | ((mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_PRE_PROC || |
| 6484 | (mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_POST_PROC)) { |
| 6485 | sp<ThreadBase> thread = mThread.promote(); |
| 6486 | if (thread != 0) { |
Eric Laurent | b8ba0a9 | 2011-08-07 16:32:26 -0700 | [diff] [blame] | 6487 | audio_stream_t *stream = thread->stream(); |
| 6488 | if (stream != NULL) { |
| 6489 | stream->add_audio_effect(stream, mEffectInterface); |
| 6490 | } |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 6491 | } |
| 6492 | } |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6493 | return status; |
| 6494 | } |
| 6495 | |
Eric Laurent | ec437d8 | 2011-07-26 20:54:46 -0700 | [diff] [blame] | 6496 | status_t AudioFlinger::EffectModule::stop() |
| 6497 | { |
| 6498 | Mutex::Autolock _l(mLock); |
| 6499 | return stop_l(); |
| 6500 | } |
| 6501 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6502 | status_t AudioFlinger::EffectModule::stop_l() |
| 6503 | { |
| 6504 | if (mEffectInterface == NULL) { |
| 6505 | return NO_INIT; |
| 6506 | } |
| 6507 | status_t cmdStatus; |
Eric Laurent | 25f4395 | 2010-07-28 05:40:18 -0700 | [diff] [blame] | 6508 | uint32_t size = sizeof(status_t); |
| 6509 | status_t status = (*mEffectInterface)->command(mEffectInterface, |
| 6510 | EFFECT_CMD_DISABLE, |
| 6511 | 0, |
| 6512 | NULL, |
| 6513 | &size, |
| 6514 | &cmdStatus); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6515 | if (status == 0) { |
| 6516 | status = cmdStatus; |
| 6517 | } |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 6518 | if (status == 0 && |
| 6519 | ((mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_PRE_PROC || |
| 6520 | (mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_POST_PROC)) { |
| 6521 | sp<ThreadBase> thread = mThread.promote(); |
| 6522 | if (thread != 0) { |
Eric Laurent | b8ba0a9 | 2011-08-07 16:32:26 -0700 | [diff] [blame] | 6523 | audio_stream_t *stream = thread->stream(); |
| 6524 | if (stream != NULL) { |
| 6525 | stream->remove_audio_effect(stream, mEffectInterface); |
| 6526 | } |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 6527 | } |
| 6528 | } |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6529 | return status; |
| 6530 | } |
| 6531 | |
Eric Laurent | 25f4395 | 2010-07-28 05:40:18 -0700 | [diff] [blame] | 6532 | status_t AudioFlinger::EffectModule::command(uint32_t cmdCode, |
| 6533 | uint32_t cmdSize, |
| 6534 | void *pCmdData, |
| 6535 | uint32_t *replySize, |
| 6536 | void *pReplyData) |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6537 | { |
| 6538 | Mutex::Autolock _l(mLock); |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 6539 | // ALOGV("command(), cmdCode: %d, mEffectInterface: %p", cmdCode, mEffectInterface); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6540 | |
Eric Laurent | ec437d8 | 2011-07-26 20:54:46 -0700 | [diff] [blame] | 6541 | if (mState == DESTROYED || mEffectInterface == NULL) { |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6542 | return NO_INIT; |
| 6543 | } |
Eric Laurent | 25f4395 | 2010-07-28 05:40:18 -0700 | [diff] [blame] | 6544 | status_t status = (*mEffectInterface)->command(mEffectInterface, |
| 6545 | cmdCode, |
| 6546 | cmdSize, |
| 6547 | pCmdData, |
| 6548 | replySize, |
| 6549 | pReplyData); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6550 | if (cmdCode != EFFECT_CMD_GET_PARAM && status == NO_ERROR) { |
Eric Laurent | 25f4395 | 2010-07-28 05:40:18 -0700 | [diff] [blame] | 6551 | uint32_t size = (replySize == NULL) ? 0 : *replySize; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6552 | for (size_t i = 1; i < mHandles.size(); i++) { |
| 6553 | sp<EffectHandle> h = mHandles[i].promote(); |
| 6554 | if (h != 0) { |
| 6555 | h->commandExecuted(cmdCode, cmdSize, pCmdData, size, pReplyData); |
| 6556 | } |
| 6557 | } |
| 6558 | } |
| 6559 | return status; |
| 6560 | } |
| 6561 | |
| 6562 | status_t AudioFlinger::EffectModule::setEnabled(bool enabled) |
| 6563 | { |
Eric Laurent | db7c079 | 2011-08-10 10:37:50 -0700 | [diff] [blame] | 6564 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6565 | Mutex::Autolock _l(mLock); |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 6566 | ALOGV("setEnabled %p enabled %d", this, enabled); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6567 | |
| 6568 | if (enabled != isEnabled()) { |
Eric Laurent | db7c079 | 2011-08-10 10:37:50 -0700 | [diff] [blame] | 6569 | status_t status = AudioSystem::setEffectEnabled(mId, enabled); |
| 6570 | if (enabled && status != NO_ERROR) { |
| 6571 | return status; |
| 6572 | } |
| 6573 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6574 | switch (mState) { |
| 6575 | // going from disabled to enabled |
| 6576 | case IDLE: |
| 6577 | mState = STARTING; |
| 6578 | break; |
| 6579 | case STOPPED: |
| 6580 | mState = RESTART; |
| 6581 | break; |
| 6582 | case STOPPING: |
| 6583 | mState = ACTIVE; |
| 6584 | break; |
| 6585 | |
| 6586 | // going from enabled to disabled |
| 6587 | case RESTART: |
Eric Laurent | 8f45bd7 | 2010-08-31 13:50:07 -0700 | [diff] [blame] | 6588 | mState = STOPPED; |
| 6589 | break; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6590 | case STARTING: |
| 6591 | mState = IDLE; |
| 6592 | break; |
| 6593 | case ACTIVE: |
| 6594 | mState = STOPPING; |
| 6595 | break; |
Eric Laurent | ec437d8 | 2011-07-26 20:54:46 -0700 | [diff] [blame] | 6596 | case DESTROYED: |
| 6597 | return NO_ERROR; // simply ignore as we are being destroyed |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6598 | } |
| 6599 | for (size_t i = 1; i < mHandles.size(); i++) { |
| 6600 | sp<EffectHandle> h = mHandles[i].promote(); |
| 6601 | if (h != 0) { |
| 6602 | h->setEnabled(enabled); |
| 6603 | } |
| 6604 | } |
| 6605 | } |
| 6606 | return NO_ERROR; |
| 6607 | } |
| 6608 | |
| 6609 | bool AudioFlinger::EffectModule::isEnabled() |
| 6610 | { |
| 6611 | switch (mState) { |
| 6612 | case RESTART: |
| 6613 | case STARTING: |
| 6614 | case ACTIVE: |
| 6615 | return true; |
| 6616 | case IDLE: |
| 6617 | case STOPPING: |
| 6618 | case STOPPED: |
Eric Laurent | ec437d8 | 2011-07-26 20:54:46 -0700 | [diff] [blame] | 6619 | case DESTROYED: |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6620 | default: |
| 6621 | return false; |
| 6622 | } |
| 6623 | } |
| 6624 | |
Eric Laurent | 8f45bd7 | 2010-08-31 13:50:07 -0700 | [diff] [blame] | 6625 | bool AudioFlinger::EffectModule::isProcessEnabled() |
| 6626 | { |
| 6627 | switch (mState) { |
| 6628 | case RESTART: |
| 6629 | case ACTIVE: |
| 6630 | case STOPPING: |
| 6631 | case STOPPED: |
| 6632 | return true; |
| 6633 | case IDLE: |
| 6634 | case STARTING: |
Eric Laurent | ec437d8 | 2011-07-26 20:54:46 -0700 | [diff] [blame] | 6635 | case DESTROYED: |
Eric Laurent | 8f45bd7 | 2010-08-31 13:50:07 -0700 | [diff] [blame] | 6636 | default: |
| 6637 | return false; |
| 6638 | } |
| 6639 | } |
| 6640 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6641 | status_t AudioFlinger::EffectModule::setVolume(uint32_t *left, uint32_t *right, bool controller) |
| 6642 | { |
| 6643 | Mutex::Autolock _l(mLock); |
| 6644 | status_t status = NO_ERROR; |
| 6645 | |
| 6646 | // Send volume indication if EFFECT_FLAG_VOLUME_IND is set and read back altered volume |
| 6647 | // 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] | 6648 | if (isProcessEnabled() && |
Eric Laurent | f997cab | 2010-07-19 06:24:46 -0700 | [diff] [blame] | 6649 | ((mDescriptor.flags & EFFECT_FLAG_VOLUME_MASK) == EFFECT_FLAG_VOLUME_CTRL || |
| 6650 | (mDescriptor.flags & EFFECT_FLAG_VOLUME_MASK) == EFFECT_FLAG_VOLUME_IND)) { |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6651 | status_t cmdStatus; |
| 6652 | uint32_t volume[2]; |
| 6653 | uint32_t *pVolume = NULL; |
Eric Laurent | 25f4395 | 2010-07-28 05:40:18 -0700 | [diff] [blame] | 6654 | uint32_t size = sizeof(volume); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6655 | volume[0] = *left; |
| 6656 | volume[1] = *right; |
| 6657 | if (controller) { |
| 6658 | pVolume = volume; |
| 6659 | } |
Eric Laurent | 25f4395 | 2010-07-28 05:40:18 -0700 | [diff] [blame] | 6660 | status = (*mEffectInterface)->command(mEffectInterface, |
| 6661 | EFFECT_CMD_SET_VOLUME, |
| 6662 | size, |
| 6663 | volume, |
| 6664 | &size, |
| 6665 | pVolume); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6666 | if (controller && status == NO_ERROR && size == sizeof(volume)) { |
| 6667 | *left = volume[0]; |
| 6668 | *right = volume[1]; |
| 6669 | } |
| 6670 | } |
| 6671 | return status; |
| 6672 | } |
| 6673 | |
| 6674 | status_t AudioFlinger::EffectModule::setDevice(uint32_t device) |
| 6675 | { |
| 6676 | Mutex::Autolock _l(mLock); |
| 6677 | status_t status = NO_ERROR; |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 6678 | if (device && (mDescriptor.flags & EFFECT_FLAG_DEVICE_MASK) == EFFECT_FLAG_DEVICE_IND) { |
| 6679 | // audio pre processing modules on RecordThread can receive both output and |
| 6680 | // input device indication in the same call |
| 6681 | uint32_t dev = device & AUDIO_DEVICE_OUT_ALL; |
| 6682 | if (dev) { |
| 6683 | status_t cmdStatus; |
| 6684 | uint32_t size = sizeof(status_t); |
| 6685 | |
| 6686 | status = (*mEffectInterface)->command(mEffectInterface, |
| 6687 | EFFECT_CMD_SET_DEVICE, |
| 6688 | sizeof(uint32_t), |
| 6689 | &dev, |
| 6690 | &size, |
| 6691 | &cmdStatus); |
| 6692 | if (status == NO_ERROR) { |
| 6693 | status = cmdStatus; |
| 6694 | } |
| 6695 | } |
| 6696 | dev = device & AUDIO_DEVICE_IN_ALL; |
| 6697 | if (dev) { |
| 6698 | status_t cmdStatus; |
| 6699 | uint32_t size = sizeof(status_t); |
| 6700 | |
| 6701 | status_t status2 = (*mEffectInterface)->command(mEffectInterface, |
| 6702 | EFFECT_CMD_SET_INPUT_DEVICE, |
| 6703 | sizeof(uint32_t), |
| 6704 | &dev, |
| 6705 | &size, |
| 6706 | &cmdStatus); |
| 6707 | if (status2 == NO_ERROR) { |
| 6708 | status2 = cmdStatus; |
| 6709 | } |
| 6710 | if (status == NO_ERROR) { |
| 6711 | status = status2; |
| 6712 | } |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6713 | } |
| 6714 | } |
| 6715 | return status; |
| 6716 | } |
| 6717 | |
Glenn Kasten | f78aee7 | 2012-01-04 11:00:47 -0800 | [diff] [blame] | 6718 | status_t AudioFlinger::EffectModule::setMode(audio_mode_t mode) |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6719 | { |
| 6720 | Mutex::Autolock _l(mLock); |
| 6721 | status_t status = NO_ERROR; |
| 6722 | 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] | 6723 | status_t cmdStatus; |
Eric Laurent | 25f4395 | 2010-07-28 05:40:18 -0700 | [diff] [blame] | 6724 | uint32_t size = sizeof(status_t); |
| 6725 | status = (*mEffectInterface)->command(mEffectInterface, |
| 6726 | EFFECT_CMD_SET_AUDIO_MODE, |
Glenn Kasten | f78aee7 | 2012-01-04 11:00:47 -0800 | [diff] [blame] | 6727 | sizeof(audio_mode_t), |
Eric Laurent | e1315cf | 2011-05-17 19:16:02 -0700 | [diff] [blame] | 6728 | &mode, |
Eric Laurent | 25f4395 | 2010-07-28 05:40:18 -0700 | [diff] [blame] | 6729 | &size, |
| 6730 | &cmdStatus); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6731 | if (status == NO_ERROR) { |
| 6732 | status = cmdStatus; |
| 6733 | } |
| 6734 | } |
| 6735 | return status; |
| 6736 | } |
| 6737 | |
Eric Laurent | 59255e4 | 2011-07-27 19:49:51 -0700 | [diff] [blame] | 6738 | void AudioFlinger::EffectModule::setSuspended(bool suspended) |
| 6739 | { |
| 6740 | Mutex::Autolock _l(mLock); |
| 6741 | mSuspended = suspended; |
| 6742 | } |
Glenn Kasten | a3a8548 | 2012-01-04 11:01:11 -0800 | [diff] [blame] | 6743 | |
| 6744 | bool AudioFlinger::EffectModule::suspended() const |
Eric Laurent | 59255e4 | 2011-07-27 19:49:51 -0700 | [diff] [blame] | 6745 | { |
| 6746 | Mutex::Autolock _l(mLock); |
| 6747 | return mSuspended; |
| 6748 | } |
| 6749 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6750 | status_t AudioFlinger::EffectModule::dump(int fd, const Vector<String16>& args) |
| 6751 | { |
| 6752 | const size_t SIZE = 256; |
| 6753 | char buffer[SIZE]; |
| 6754 | String8 result; |
| 6755 | |
| 6756 | snprintf(buffer, SIZE, "\tEffect ID %d:\n", mId); |
| 6757 | result.append(buffer); |
| 6758 | |
| 6759 | bool locked = tryLock(mLock); |
| 6760 | // failed to lock - AudioFlinger is probably deadlocked |
| 6761 | if (!locked) { |
| 6762 | result.append("\t\tCould not lock Fx mutex:\n"); |
| 6763 | } |
| 6764 | |
| 6765 | result.append("\t\tSession Status State Engine:\n"); |
| 6766 | snprintf(buffer, SIZE, "\t\t%05d %03d %03d 0x%08x\n", |
| 6767 | mSessionId, mStatus, mState, (uint32_t)mEffectInterface); |
| 6768 | result.append(buffer); |
| 6769 | |
| 6770 | result.append("\t\tDescriptor:\n"); |
| 6771 | snprintf(buffer, SIZE, "\t\t- UUID: %08X-%04X-%04X-%04X-%02X%02X%02X%02X%02X%02X\n", |
| 6772 | mDescriptor.uuid.timeLow, mDescriptor.uuid.timeMid, mDescriptor.uuid.timeHiAndVersion, |
| 6773 | mDescriptor.uuid.clockSeq, mDescriptor.uuid.node[0], mDescriptor.uuid.node[1],mDescriptor.uuid.node[2], |
| 6774 | mDescriptor.uuid.node[3],mDescriptor.uuid.node[4],mDescriptor.uuid.node[5]); |
| 6775 | result.append(buffer); |
| 6776 | snprintf(buffer, SIZE, "\t\t- TYPE: %08X-%04X-%04X-%04X-%02X%02X%02X%02X%02X%02X\n", |
| 6777 | mDescriptor.type.timeLow, mDescriptor.type.timeMid, mDescriptor.type.timeHiAndVersion, |
| 6778 | mDescriptor.type.clockSeq, mDescriptor.type.node[0], mDescriptor.type.node[1],mDescriptor.type.node[2], |
| 6779 | mDescriptor.type.node[3],mDescriptor.type.node[4],mDescriptor.type.node[5]); |
| 6780 | result.append(buffer); |
Eric Laurent | e1315cf | 2011-05-17 19:16:02 -0700 | [diff] [blame] | 6781 | 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] | 6782 | mDescriptor.apiVersion, |
| 6783 | mDescriptor.flags); |
| 6784 | result.append(buffer); |
| 6785 | snprintf(buffer, SIZE, "\t\t- name: %s\n", |
| 6786 | mDescriptor.name); |
| 6787 | result.append(buffer); |
| 6788 | snprintf(buffer, SIZE, "\t\t- implementor: %s\n", |
| 6789 | mDescriptor.implementor); |
| 6790 | result.append(buffer); |
| 6791 | |
| 6792 | result.append("\t\t- Input configuration:\n"); |
| 6793 | result.append("\t\t\tBuffer Frames Smp rate Channels Format\n"); |
| 6794 | snprintf(buffer, SIZE, "\t\t\t0x%08x %05d %05d %08x %d\n", |
| 6795 | (uint32_t)mConfig.inputCfg.buffer.raw, |
| 6796 | mConfig.inputCfg.buffer.frameCount, |
| 6797 | mConfig.inputCfg.samplingRate, |
| 6798 | mConfig.inputCfg.channels, |
| 6799 | mConfig.inputCfg.format); |
| 6800 | result.append(buffer); |
| 6801 | |
| 6802 | result.append("\t\t- Output configuration:\n"); |
| 6803 | result.append("\t\t\tBuffer Frames Smp rate Channels Format\n"); |
| 6804 | snprintf(buffer, SIZE, "\t\t\t0x%08x %05d %05d %08x %d\n", |
| 6805 | (uint32_t)mConfig.outputCfg.buffer.raw, |
| 6806 | mConfig.outputCfg.buffer.frameCount, |
| 6807 | mConfig.outputCfg.samplingRate, |
| 6808 | mConfig.outputCfg.channels, |
| 6809 | mConfig.outputCfg.format); |
| 6810 | result.append(buffer); |
| 6811 | |
| 6812 | snprintf(buffer, SIZE, "\t\t%d Clients:\n", mHandles.size()); |
| 6813 | result.append(buffer); |
| 6814 | result.append("\t\t\tPid Priority Ctrl Locked client server\n"); |
| 6815 | for (size_t i = 0; i < mHandles.size(); ++i) { |
| 6816 | sp<EffectHandle> handle = mHandles[i].promote(); |
| 6817 | if (handle != 0) { |
| 6818 | handle->dump(buffer, SIZE); |
| 6819 | result.append(buffer); |
| 6820 | } |
| 6821 | } |
| 6822 | |
| 6823 | result.append("\n"); |
| 6824 | |
| 6825 | write(fd, result.string(), result.length()); |
| 6826 | |
| 6827 | if (locked) { |
| 6828 | mLock.unlock(); |
| 6829 | } |
| 6830 | |
| 6831 | return NO_ERROR; |
| 6832 | } |
| 6833 | |
| 6834 | // ---------------------------------------------------------------------------- |
| 6835 | // EffectHandle implementation |
| 6836 | // ---------------------------------------------------------------------------- |
| 6837 | |
| 6838 | #undef LOG_TAG |
| 6839 | #define LOG_TAG "AudioFlinger::EffectHandle" |
| 6840 | |
| 6841 | AudioFlinger::EffectHandle::EffectHandle(const sp<EffectModule>& effect, |
| 6842 | const sp<AudioFlinger::Client>& client, |
| 6843 | const sp<IEffectClient>& effectClient, |
| 6844 | int32_t priority) |
| 6845 | : BnEffect(), |
Marco Nelissen | 3a34bef | 2011-08-02 13:33:41 -0700 | [diff] [blame] | 6846 | mEffect(effect), mEffectClient(effectClient), mClient(client), mCblk(NULL), |
Eric Laurent | 59255e4 | 2011-07-27 19:49:51 -0700 | [diff] [blame] | 6847 | mPriority(priority), mHasControl(false), mEnabled(false) |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6848 | { |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 6849 | ALOGV("constructor %p", this); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6850 | |
Marco Nelissen | 3a34bef | 2011-08-02 13:33:41 -0700 | [diff] [blame] | 6851 | if (client == 0) { |
| 6852 | return; |
| 6853 | } |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6854 | int bufOffset = ((sizeof(effect_param_cblk_t) - 1) / sizeof(int) + 1) * sizeof(int); |
| 6855 | mCblkMemory = client->heap()->allocate(EFFECT_PARAM_BUFFER_SIZE + bufOffset); |
| 6856 | if (mCblkMemory != 0) { |
| 6857 | mCblk = static_cast<effect_param_cblk_t *>(mCblkMemory->pointer()); |
| 6858 | |
| 6859 | if (mCblk) { |
| 6860 | new(mCblk) effect_param_cblk_t(); |
| 6861 | mBuffer = (uint8_t *)mCblk + bufOffset; |
| 6862 | } |
| 6863 | } else { |
Steve Block | 29357bc | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 6864 | 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] | 6865 | return; |
| 6866 | } |
| 6867 | } |
| 6868 | |
| 6869 | AudioFlinger::EffectHandle::~EffectHandle() |
| 6870 | { |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 6871 | ALOGV("Destructor %p", this); |
Marco Nelissen | 3a34bef | 2011-08-02 13:33:41 -0700 | [diff] [blame] | 6872 | disconnect(false); |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 6873 | ALOGV("Destructor DONE %p", this); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6874 | } |
| 6875 | |
| 6876 | status_t AudioFlinger::EffectHandle::enable() |
| 6877 | { |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 6878 | ALOGV("enable %p", this); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6879 | if (!mHasControl) return INVALID_OPERATION; |
| 6880 | if (mEffect == 0) return DEAD_OBJECT; |
| 6881 | |
Eric Laurent | db7c079 | 2011-08-10 10:37:50 -0700 | [diff] [blame] | 6882 | if (mEnabled) { |
| 6883 | return NO_ERROR; |
| 6884 | } |
| 6885 | |
Eric Laurent | 59255e4 | 2011-07-27 19:49:51 -0700 | [diff] [blame] | 6886 | mEnabled = true; |
| 6887 | |
| 6888 | sp<ThreadBase> thread = mEffect->thread().promote(); |
| 6889 | if (thread != 0) { |
| 6890 | thread->checkSuspendOnEffectEnabled(mEffect, true, mEffect->sessionId()); |
| 6891 | } |
| 6892 | |
| 6893 | // checkSuspendOnEffectEnabled() can suspend this same effect when enabled |
| 6894 | if (mEffect->suspended()) { |
| 6895 | return NO_ERROR; |
| 6896 | } |
| 6897 | |
Eric Laurent | db7c079 | 2011-08-10 10:37:50 -0700 | [diff] [blame] | 6898 | status_t status = mEffect->setEnabled(true); |
| 6899 | if (status != NO_ERROR) { |
| 6900 | if (thread != 0) { |
| 6901 | thread->checkSuspendOnEffectEnabled(mEffect, false, mEffect->sessionId()); |
| 6902 | } |
| 6903 | mEnabled = false; |
| 6904 | } |
| 6905 | return status; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6906 | } |
| 6907 | |
| 6908 | status_t AudioFlinger::EffectHandle::disable() |
| 6909 | { |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 6910 | ALOGV("disable %p", this); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6911 | if (!mHasControl) return INVALID_OPERATION; |
Eric Laurent | 59255e4 | 2011-07-27 19:49:51 -0700 | [diff] [blame] | 6912 | if (mEffect == 0) return DEAD_OBJECT; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6913 | |
Eric Laurent | db7c079 | 2011-08-10 10:37:50 -0700 | [diff] [blame] | 6914 | if (!mEnabled) { |
| 6915 | return NO_ERROR; |
| 6916 | } |
Eric Laurent | 59255e4 | 2011-07-27 19:49:51 -0700 | [diff] [blame] | 6917 | mEnabled = false; |
| 6918 | |
| 6919 | if (mEffect->suspended()) { |
| 6920 | return NO_ERROR; |
| 6921 | } |
| 6922 | |
| 6923 | status_t status = mEffect->setEnabled(false); |
| 6924 | |
| 6925 | sp<ThreadBase> thread = mEffect->thread().promote(); |
| 6926 | if (thread != 0) { |
| 6927 | thread->checkSuspendOnEffectEnabled(mEffect, false, mEffect->sessionId()); |
| 6928 | } |
| 6929 | |
| 6930 | return status; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6931 | } |
| 6932 | |
| 6933 | void AudioFlinger::EffectHandle::disconnect() |
| 6934 | { |
Marco Nelissen | 3a34bef | 2011-08-02 13:33:41 -0700 | [diff] [blame] | 6935 | disconnect(true); |
| 6936 | } |
| 6937 | |
| 6938 | void AudioFlinger::EffectHandle::disconnect(bool unpiniflast) |
| 6939 | { |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 6940 | ALOGV("disconnect(%s)", unpiniflast ? "true" : "false"); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6941 | if (mEffect == 0) { |
| 6942 | return; |
| 6943 | } |
Marco Nelissen | 3a34bef | 2011-08-02 13:33:41 -0700 | [diff] [blame] | 6944 | mEffect->disconnect(this, unpiniflast); |
Eric Laurent | 59255e4 | 2011-07-27 19:49:51 -0700 | [diff] [blame] | 6945 | |
Eric Laurent | a85a74a | 2011-10-19 11:44:54 -0700 | [diff] [blame] | 6946 | if (mHasControl && mEnabled) { |
Eric Laurent | db7c079 | 2011-08-10 10:37:50 -0700 | [diff] [blame] | 6947 | sp<ThreadBase> thread = mEffect->thread().promote(); |
| 6948 | if (thread != 0) { |
| 6949 | thread->checkSuspendOnEffectEnabled(mEffect, false, mEffect->sessionId()); |
| 6950 | } |
Eric Laurent | 59255e4 | 2011-07-27 19:49:51 -0700 | [diff] [blame] | 6951 | } |
| 6952 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6953 | // release sp on module => module destructor can be called now |
| 6954 | mEffect.clear(); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6955 | if (mClient != 0) { |
Marco Nelissen | 3a34bef | 2011-08-02 13:33:41 -0700 | [diff] [blame] | 6956 | if (mCblk) { |
| 6957 | mCblk->~effect_param_cblk_t(); // destroy our shared-structure. |
| 6958 | } |
| 6959 | mCblkMemory.clear(); // and free the shared memory |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6960 | Mutex::Autolock _l(mClient->audioFlinger()->mLock); |
| 6961 | mClient.clear(); |
| 6962 | } |
| 6963 | } |
| 6964 | |
Eric Laurent | 25f4395 | 2010-07-28 05:40:18 -0700 | [diff] [blame] | 6965 | status_t AudioFlinger::EffectHandle::command(uint32_t cmdCode, |
| 6966 | uint32_t cmdSize, |
| 6967 | void *pCmdData, |
| 6968 | uint32_t *replySize, |
| 6969 | void *pReplyData) |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6970 | { |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 6971 | // ALOGV("command(), cmdCode: %d, mHasControl: %d, mEffect: %p", |
Eric Laurent | 25f4395 | 2010-07-28 05:40:18 -0700 | [diff] [blame] | 6972 | // cmdCode, mHasControl, (mEffect == 0) ? 0 : mEffect.get()); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6973 | |
| 6974 | // only get parameter command is permitted for applications not controlling the effect |
| 6975 | if (!mHasControl && cmdCode != EFFECT_CMD_GET_PARAM) { |
| 6976 | return INVALID_OPERATION; |
| 6977 | } |
| 6978 | if (mEffect == 0) return DEAD_OBJECT; |
Marco Nelissen | 3a34bef | 2011-08-02 13:33:41 -0700 | [diff] [blame] | 6979 | if (mClient == 0) return INVALID_OPERATION; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6980 | |
| 6981 | // handle commands that are not forwarded transparently to effect engine |
| 6982 | if (cmdCode == EFFECT_CMD_SET_PARAM_COMMIT) { |
| 6983 | // No need to trylock() here as this function is executed in the binder thread serving a particular client process: |
| 6984 | // no risk to block the whole media server process or mixer threads is we are stuck here |
| 6985 | Mutex::Autolock _l(mCblk->lock); |
| 6986 | if (mCblk->clientIndex > EFFECT_PARAM_BUFFER_SIZE || |
| 6987 | mCblk->serverIndex > EFFECT_PARAM_BUFFER_SIZE) { |
| 6988 | mCblk->serverIndex = 0; |
| 6989 | mCblk->clientIndex = 0; |
| 6990 | return BAD_VALUE; |
| 6991 | } |
| 6992 | status_t status = NO_ERROR; |
| 6993 | while (mCblk->serverIndex < mCblk->clientIndex) { |
| 6994 | int reply; |
Eric Laurent | 25f4395 | 2010-07-28 05:40:18 -0700 | [diff] [blame] | 6995 | uint32_t rsize = sizeof(int); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6996 | int *p = (int *)(mBuffer + mCblk->serverIndex); |
| 6997 | int size = *p++; |
| 6998 | if (((uint8_t *)p + size) > mBuffer + mCblk->clientIndex) { |
Steve Block | 5ff1dd5 | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 6999 | ALOGW("command(): invalid parameter block size"); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 7000 | break; |
| 7001 | } |
| 7002 | effect_param_t *param = (effect_param_t *)p; |
| 7003 | if (param->psize == 0 || param->vsize == 0) { |
Steve Block | 5ff1dd5 | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 7004 | ALOGW("command(): null parameter or value size"); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 7005 | mCblk->serverIndex += size; |
| 7006 | continue; |
| 7007 | } |
Eric Laurent | 25f4395 | 2010-07-28 05:40:18 -0700 | [diff] [blame] | 7008 | uint32_t psize = sizeof(effect_param_t) + |
| 7009 | ((param->psize - 1) / sizeof(int) + 1) * sizeof(int) + |
| 7010 | param->vsize; |
| 7011 | status_t ret = mEffect->command(EFFECT_CMD_SET_PARAM, |
| 7012 | psize, |
| 7013 | p, |
| 7014 | &rsize, |
| 7015 | &reply); |
Eric Laurent | aeae3de | 2010-09-02 11:56:55 -0700 | [diff] [blame] | 7016 | // stop at first error encountered |
| 7017 | if (ret != NO_ERROR) { |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 7018 | status = ret; |
Eric Laurent | aeae3de | 2010-09-02 11:56:55 -0700 | [diff] [blame] | 7019 | *(int *)pReplyData = reply; |
| 7020 | break; |
| 7021 | } else if (reply != NO_ERROR) { |
| 7022 | *(int *)pReplyData = reply; |
| 7023 | break; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 7024 | } |
| 7025 | mCblk->serverIndex += size; |
| 7026 | } |
| 7027 | mCblk->serverIndex = 0; |
| 7028 | mCblk->clientIndex = 0; |
| 7029 | return status; |
| 7030 | } else if (cmdCode == EFFECT_CMD_ENABLE) { |
Eric Laurent | aeae3de | 2010-09-02 11:56:55 -0700 | [diff] [blame] | 7031 | *(int *)pReplyData = NO_ERROR; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 7032 | return enable(); |
| 7033 | } else if (cmdCode == EFFECT_CMD_DISABLE) { |
Eric Laurent | aeae3de | 2010-09-02 11:56:55 -0700 | [diff] [blame] | 7034 | *(int *)pReplyData = NO_ERROR; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 7035 | return disable(); |
| 7036 | } |
| 7037 | |
| 7038 | return mEffect->command(cmdCode, cmdSize, pCmdData, replySize, pReplyData); |
| 7039 | } |
| 7040 | |
| 7041 | sp<IMemory> AudioFlinger::EffectHandle::getCblk() const { |
| 7042 | return mCblkMemory; |
| 7043 | } |
| 7044 | |
Eric Laurent | 59255e4 | 2011-07-27 19:49:51 -0700 | [diff] [blame] | 7045 | void AudioFlinger::EffectHandle::setControl(bool hasControl, bool signal, bool enabled) |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 7046 | { |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 7047 | ALOGV("setControl %p control %d", this, hasControl); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 7048 | |
| 7049 | mHasControl = hasControl; |
Eric Laurent | 59255e4 | 2011-07-27 19:49:51 -0700 | [diff] [blame] | 7050 | mEnabled = enabled; |
| 7051 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 7052 | if (signal && mEffectClient != 0) { |
| 7053 | mEffectClient->controlStatusChanged(hasControl); |
| 7054 | } |
| 7055 | } |
| 7056 | |
Eric Laurent | 25f4395 | 2010-07-28 05:40:18 -0700 | [diff] [blame] | 7057 | void AudioFlinger::EffectHandle::commandExecuted(uint32_t cmdCode, |
| 7058 | uint32_t cmdSize, |
| 7059 | void *pCmdData, |
| 7060 | uint32_t replySize, |
| 7061 | void *pReplyData) |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 7062 | { |
| 7063 | if (mEffectClient != 0) { |
| 7064 | mEffectClient->commandExecuted(cmdCode, cmdSize, pCmdData, replySize, pReplyData); |
| 7065 | } |
| 7066 | } |
| 7067 | |
| 7068 | |
| 7069 | |
| 7070 | void AudioFlinger::EffectHandle::setEnabled(bool enabled) |
| 7071 | { |
| 7072 | if (mEffectClient != 0) { |
| 7073 | mEffectClient->enableStatusChanged(enabled); |
| 7074 | } |
| 7075 | } |
| 7076 | |
| 7077 | status_t AudioFlinger::EffectHandle::onTransact( |
| 7078 | uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags) |
| 7079 | { |
| 7080 | return BnEffect::onTransact(code, data, reply, flags); |
| 7081 | } |
| 7082 | |
| 7083 | |
| 7084 | void AudioFlinger::EffectHandle::dump(char* buffer, size_t size) |
| 7085 | { |
Marco Nelissen | 3a34bef | 2011-08-02 13:33:41 -0700 | [diff] [blame] | 7086 | bool locked = mCblk ? tryLock(mCblk->lock) : false; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 7087 | |
| 7088 | snprintf(buffer, size, "\t\t\t%05d %05d %01u %01u %05u %05u\n", |
| 7089 | (mClient == NULL) ? getpid() : mClient->pid(), |
| 7090 | mPriority, |
| 7091 | mHasControl, |
| 7092 | !locked, |
Marco Nelissen | 3a34bef | 2011-08-02 13:33:41 -0700 | [diff] [blame] | 7093 | mCblk ? mCblk->clientIndex : 0, |
| 7094 | mCblk ? mCblk->serverIndex : 0 |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 7095 | ); |
| 7096 | |
| 7097 | if (locked) { |
| 7098 | mCblk->lock.unlock(); |
| 7099 | } |
| 7100 | } |
| 7101 | |
| 7102 | #undef LOG_TAG |
| 7103 | #define LOG_TAG "AudioFlinger::EffectChain" |
| 7104 | |
| 7105 | AudioFlinger::EffectChain::EffectChain(const wp<ThreadBase>& wThread, |
| 7106 | int sessionId) |
Eric Laurent | 544fe9b | 2011-11-11 15:42:52 -0800 | [diff] [blame] | 7107 | : mThread(wThread), mSessionId(sessionId), mActiveTrackCnt(0), mTrackCnt(0), mTailBufferCount(0), |
Eric Laurent | b469b94 | 2011-05-09 12:09:06 -0700 | [diff] [blame] | 7108 | mOwnInBuffer(false), mVolumeCtrlIdx(-1), mLeftVolume(UINT_MAX), mRightVolume(UINT_MAX), |
| 7109 | mNewLeftVolume(UINT_MAX), mNewRightVolume(UINT_MAX) |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 7110 | { |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 7111 | mStrategy = AudioSystem::getStrategyForStream(AUDIO_STREAM_MUSIC); |
Eric Laurent | 544fe9b | 2011-11-11 15:42:52 -0800 | [diff] [blame] | 7112 | sp<ThreadBase> thread = mThread.promote(); |
| 7113 | if (thread == 0) { |
| 7114 | return; |
| 7115 | } |
| 7116 | mMaxTailBuffers = ((kProcessTailDurationMs * thread->sampleRate()) / 1000) / |
| 7117 | thread->frameCount(); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 7118 | } |
| 7119 | |
| 7120 | AudioFlinger::EffectChain::~EffectChain() |
| 7121 | { |
| 7122 | if (mOwnInBuffer) { |
| 7123 | delete mInBuffer; |
| 7124 | } |
| 7125 | |
| 7126 | } |
| 7127 | |
Eric Laurent | 59255e4 | 2011-07-27 19:49:51 -0700 | [diff] [blame] | 7128 | // getEffectFromDesc_l() must be called with ThreadBase::mLock held |
Eric Laurent | cab1124 | 2010-07-15 12:50:15 -0700 | [diff] [blame] | 7129 | sp<AudioFlinger::EffectModule> AudioFlinger::EffectChain::getEffectFromDesc_l(effect_descriptor_t *descriptor) |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 7130 | { |
| 7131 | sp<EffectModule> effect; |
| 7132 | size_t size = mEffects.size(); |
| 7133 | |
| 7134 | for (size_t i = 0; i < size; i++) { |
| 7135 | if (memcmp(&mEffects[i]->desc().uuid, &descriptor->uuid, sizeof(effect_uuid_t)) == 0) { |
| 7136 | effect = mEffects[i]; |
| 7137 | break; |
| 7138 | } |
| 7139 | } |
| 7140 | return effect; |
| 7141 | } |
| 7142 | |
Eric Laurent | 59255e4 | 2011-07-27 19:49:51 -0700 | [diff] [blame] | 7143 | // getEffectFromId_l() must be called with ThreadBase::mLock held |
Eric Laurent | cab1124 | 2010-07-15 12:50:15 -0700 | [diff] [blame] | 7144 | sp<AudioFlinger::EffectModule> AudioFlinger::EffectChain::getEffectFromId_l(int id) |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 7145 | { |
| 7146 | sp<EffectModule> effect; |
| 7147 | size_t size = mEffects.size(); |
| 7148 | |
| 7149 | for (size_t i = 0; i < size; i++) { |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 7150 | // by convention, return first effect if id provided is 0 (0 is never a valid id) |
| 7151 | if (id == 0 || mEffects[i]->id() == id) { |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 7152 | effect = mEffects[i]; |
| 7153 | break; |
| 7154 | } |
| 7155 | } |
| 7156 | return effect; |
| 7157 | } |
| 7158 | |
Eric Laurent | 59255e4 | 2011-07-27 19:49:51 -0700 | [diff] [blame] | 7159 | // getEffectFromType_l() must be called with ThreadBase::mLock held |
| 7160 | sp<AudioFlinger::EffectModule> AudioFlinger::EffectChain::getEffectFromType_l( |
| 7161 | const effect_uuid_t *type) |
| 7162 | { |
| 7163 | sp<EffectModule> effect; |
| 7164 | size_t size = mEffects.size(); |
| 7165 | |
| 7166 | for (size_t i = 0; i < size; i++) { |
| 7167 | if (memcmp(&mEffects[i]->desc().type, type, sizeof(effect_uuid_t)) == 0) { |
| 7168 | effect = mEffects[i]; |
| 7169 | break; |
| 7170 | } |
| 7171 | } |
| 7172 | return effect; |
| 7173 | } |
| 7174 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 7175 | // Must be called with EffectChain::mLock locked |
| 7176 | void AudioFlinger::EffectChain::process_l() |
| 7177 | { |
Eric Laurent | dac6911 | 2010-09-28 14:09:57 -0700 | [diff] [blame] | 7178 | sp<ThreadBase> thread = mThread.promote(); |
| 7179 | if (thread == 0) { |
Steve Block | 5ff1dd5 | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 7180 | ALOGW("process_l(): cannot promote mixer thread"); |
Eric Laurent | dac6911 | 2010-09-28 14:09:57 -0700 | [diff] [blame] | 7181 | return; |
| 7182 | } |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 7183 | bool isGlobalSession = (mSessionId == AUDIO_SESSION_OUTPUT_MIX) || |
| 7184 | (mSessionId == AUDIO_SESSION_OUTPUT_STAGE); |
Eric Laurent | 544fe9b | 2011-11-11 15:42:52 -0800 | [diff] [blame] | 7185 | // always process effects unless no more tracks are on the session and the effect tail |
| 7186 | // has been rendered |
| 7187 | bool doProcess = true; |
Eric Laurent | dac6911 | 2010-09-28 14:09:57 -0700 | [diff] [blame] | 7188 | if (!isGlobalSession) { |
Eric Laurent | 544fe9b | 2011-11-11 15:42:52 -0800 | [diff] [blame] | 7189 | bool tracksOnSession = (trackCnt() != 0); |
Eric Laurent | b469b94 | 2011-05-09 12:09:06 -0700 | [diff] [blame] | 7190 | |
Eric Laurent | 544fe9b | 2011-11-11 15:42:52 -0800 | [diff] [blame] | 7191 | if (!tracksOnSession && mTailBufferCount == 0) { |
| 7192 | doProcess = false; |
| 7193 | } |
| 7194 | |
| 7195 | if (activeTrackCnt() == 0) { |
| 7196 | // if no track is active and the effect tail has not been rendered, |
| 7197 | // the input buffer must be cleared here as the mixer process will not do it |
| 7198 | if (tracksOnSession || mTailBufferCount > 0) { |
| 7199 | size_t numSamples = thread->frameCount() * thread->channelCount(); |
| 7200 | memset(mInBuffer, 0, numSamples * sizeof(int16_t)); |
| 7201 | if (mTailBufferCount > 0) { |
| 7202 | mTailBufferCount--; |
| 7203 | } |
| 7204 | } |
| 7205 | } |
Eric Laurent | dac6911 | 2010-09-28 14:09:57 -0700 | [diff] [blame] | 7206 | } |
| 7207 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 7208 | size_t size = mEffects.size(); |
Eric Laurent | 544fe9b | 2011-11-11 15:42:52 -0800 | [diff] [blame] | 7209 | if (doProcess) { |
Eric Laurent | dac6911 | 2010-09-28 14:09:57 -0700 | [diff] [blame] | 7210 | for (size_t i = 0; i < size; i++) { |
| 7211 | mEffects[i]->process(); |
| 7212 | } |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 7213 | } |
| 7214 | for (size_t i = 0; i < size; i++) { |
| 7215 | mEffects[i]->updateState(); |
| 7216 | } |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 7217 | } |
| 7218 | |
Eric Laurent | cab1124 | 2010-07-15 12:50:15 -0700 | [diff] [blame] | 7219 | // addEffect_l() must be called with PlaybackThread::mLock held |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 7220 | status_t AudioFlinger::EffectChain::addEffect_l(const sp<EffectModule>& effect) |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 7221 | { |
| 7222 | effect_descriptor_t desc = effect->desc(); |
| 7223 | uint32_t insertPref = desc.flags & EFFECT_FLAG_INSERT_MASK; |
| 7224 | |
| 7225 | Mutex::Autolock _l(mLock); |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 7226 | effect->setChain(this); |
| 7227 | sp<ThreadBase> thread = mThread.promote(); |
| 7228 | if (thread == 0) { |
| 7229 | return NO_INIT; |
| 7230 | } |
| 7231 | effect->setThread(thread); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 7232 | |
| 7233 | if ((desc.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) { |
| 7234 | // Auxiliary effects are inserted at the beginning of mEffects vector as |
| 7235 | // they are processed first and accumulated in chain input buffer |
| 7236 | mEffects.insertAt(effect, 0); |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 7237 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 7238 | // the input buffer for auxiliary effect contains mono samples in |
| 7239 | // 32 bit format. This is to avoid saturation in AudoMixer |
| 7240 | // accumulation stage. Saturation is done in EffectModule::process() before |
| 7241 | // calling the process in effect engine |
| 7242 | size_t numSamples = thread->frameCount(); |
| 7243 | int32_t *buffer = new int32_t[numSamples]; |
| 7244 | memset(buffer, 0, numSamples * sizeof(int32_t)); |
| 7245 | effect->setInBuffer((int16_t *)buffer); |
| 7246 | // auxiliary effects output samples to chain input buffer for further processing |
| 7247 | // by insert effects |
| 7248 | effect->setOutBuffer(mInBuffer); |
| 7249 | } else { |
| 7250 | // Insert effects are inserted at the end of mEffects vector as they are processed |
| 7251 | // after track and auxiliary effects. |
| 7252 | // Insert effect order as a function of indicated preference: |
| 7253 | // if EFFECT_FLAG_INSERT_EXCLUSIVE, insert in first position or reject if |
| 7254 | // another effect is present |
| 7255 | // else if EFFECT_FLAG_INSERT_FIRST, insert in first position or after the |
| 7256 | // last effect claiming first position |
| 7257 | // else if EFFECT_FLAG_INSERT_LAST, insert in last position or before the |
| 7258 | // first effect claiming last position |
| 7259 | // else if EFFECT_FLAG_INSERT_ANY insert after first or before last |
| 7260 | // Reject insertion if an effect with EFFECT_FLAG_INSERT_EXCLUSIVE is |
| 7261 | // already present |
| 7262 | |
| 7263 | int size = (int)mEffects.size(); |
| 7264 | int idx_insert = size; |
| 7265 | int idx_insert_first = -1; |
| 7266 | int idx_insert_last = -1; |
| 7267 | |
| 7268 | for (int i = 0; i < size; i++) { |
| 7269 | effect_descriptor_t d = mEffects[i]->desc(); |
| 7270 | uint32_t iMode = d.flags & EFFECT_FLAG_TYPE_MASK; |
| 7271 | uint32_t iPref = d.flags & EFFECT_FLAG_INSERT_MASK; |
| 7272 | if (iMode == EFFECT_FLAG_TYPE_INSERT) { |
| 7273 | // check invalid effect chaining combinations |
| 7274 | if (insertPref == EFFECT_FLAG_INSERT_EXCLUSIVE || |
| 7275 | iPref == EFFECT_FLAG_INSERT_EXCLUSIVE) { |
Steve Block | 5ff1dd5 | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 7276 | 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] | 7277 | return INVALID_OPERATION; |
| 7278 | } |
| 7279 | // remember position of first insert effect and by default |
| 7280 | // select this as insert position for new effect |
| 7281 | if (idx_insert == size) { |
| 7282 | idx_insert = i; |
| 7283 | } |
| 7284 | // remember position of last insert effect claiming |
| 7285 | // first position |
| 7286 | if (iPref == EFFECT_FLAG_INSERT_FIRST) { |
| 7287 | idx_insert_first = i; |
| 7288 | } |
| 7289 | // remember position of first insert effect claiming |
| 7290 | // last position |
| 7291 | if (iPref == EFFECT_FLAG_INSERT_LAST && |
| 7292 | idx_insert_last == -1) { |
| 7293 | idx_insert_last = i; |
| 7294 | } |
| 7295 | } |
| 7296 | } |
| 7297 | |
| 7298 | // modify idx_insert from first position if needed |
| 7299 | if (insertPref == EFFECT_FLAG_INSERT_LAST) { |
| 7300 | if (idx_insert_last != -1) { |
| 7301 | idx_insert = idx_insert_last; |
| 7302 | } else { |
| 7303 | idx_insert = size; |
| 7304 | } |
| 7305 | } else { |
| 7306 | if (idx_insert_first != -1) { |
| 7307 | idx_insert = idx_insert_first + 1; |
| 7308 | } |
| 7309 | } |
| 7310 | |
| 7311 | // always read samples from chain input buffer |
| 7312 | effect->setInBuffer(mInBuffer); |
| 7313 | |
| 7314 | // if last effect in the chain, output samples to chain |
| 7315 | // output buffer, otherwise to chain input buffer |
| 7316 | if (idx_insert == size) { |
| 7317 | if (idx_insert != 0) { |
| 7318 | mEffects[idx_insert-1]->setOutBuffer(mInBuffer); |
| 7319 | mEffects[idx_insert-1]->configure(); |
| 7320 | } |
| 7321 | effect->setOutBuffer(mOutBuffer); |
| 7322 | } else { |
| 7323 | effect->setOutBuffer(mInBuffer); |
| 7324 | } |
| 7325 | mEffects.insertAt(effect, idx_insert); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 7326 | |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 7327 | 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] | 7328 | } |
| 7329 | effect->configure(); |
| 7330 | return NO_ERROR; |
| 7331 | } |
| 7332 | |
Eric Laurent | cab1124 | 2010-07-15 12:50:15 -0700 | [diff] [blame] | 7333 | // removeEffect_l() must be called with PlaybackThread::mLock held |
| 7334 | size_t AudioFlinger::EffectChain::removeEffect_l(const sp<EffectModule>& effect) |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 7335 | { |
| 7336 | Mutex::Autolock _l(mLock); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 7337 | int size = (int)mEffects.size(); |
| 7338 | int i; |
| 7339 | uint32_t type = effect->desc().flags & EFFECT_FLAG_TYPE_MASK; |
| 7340 | |
| 7341 | for (i = 0; i < size; i++) { |
| 7342 | if (effect == mEffects[i]) { |
Eric Laurent | ec437d8 | 2011-07-26 20:54:46 -0700 | [diff] [blame] | 7343 | // calling stop here will remove pre-processing effect from the audio HAL. |
| 7344 | // This is safe as we hold the EffectChain mutex which guarantees that we are not in |
| 7345 | // the middle of a read from audio HAL |
Eric Laurent | ec35a14 | 2011-10-05 17:42:25 -0700 | [diff] [blame] | 7346 | if (mEffects[i]->state() == EffectModule::ACTIVE || |
| 7347 | mEffects[i]->state() == EffectModule::STOPPING) { |
| 7348 | mEffects[i]->stop(); |
| 7349 | } |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 7350 | if (type == EFFECT_FLAG_TYPE_AUXILIARY) { |
| 7351 | delete[] effect->inBuffer(); |
| 7352 | } else { |
| 7353 | if (i == size - 1 && i != 0) { |
| 7354 | mEffects[i - 1]->setOutBuffer(mOutBuffer); |
| 7355 | mEffects[i - 1]->configure(); |
| 7356 | } |
| 7357 | } |
| 7358 | mEffects.removeAt(i); |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 7359 | 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] | 7360 | break; |
| 7361 | } |
| 7362 | } |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 7363 | |
| 7364 | return mEffects.size(); |
| 7365 | } |
| 7366 | |
Eric Laurent | cab1124 | 2010-07-15 12:50:15 -0700 | [diff] [blame] | 7367 | // setDevice_l() must be called with PlaybackThread::mLock held |
| 7368 | void AudioFlinger::EffectChain::setDevice_l(uint32_t device) |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 7369 | { |
| 7370 | size_t size = mEffects.size(); |
| 7371 | for (size_t i = 0; i < size; i++) { |
| 7372 | mEffects[i]->setDevice(device); |
| 7373 | } |
| 7374 | } |
| 7375 | |
Eric Laurent | cab1124 | 2010-07-15 12:50:15 -0700 | [diff] [blame] | 7376 | // setMode_l() must be called with PlaybackThread::mLock held |
Glenn Kasten | f78aee7 | 2012-01-04 11:00:47 -0800 | [diff] [blame] | 7377 | void AudioFlinger::EffectChain::setMode_l(audio_mode_t mode) |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 7378 | { |
| 7379 | size_t size = mEffects.size(); |
| 7380 | for (size_t i = 0; i < size; i++) { |
| 7381 | mEffects[i]->setMode(mode); |
| 7382 | } |
| 7383 | } |
| 7384 | |
Eric Laurent | cab1124 | 2010-07-15 12:50:15 -0700 | [diff] [blame] | 7385 | // setVolume_l() must be called with PlaybackThread::mLock held |
| 7386 | bool AudioFlinger::EffectChain::setVolume_l(uint32_t *left, uint32_t *right) |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 7387 | { |
| 7388 | uint32_t newLeft = *left; |
| 7389 | uint32_t newRight = *right; |
| 7390 | bool hasControl = false; |
Eric Laurent | cab1124 | 2010-07-15 12:50:15 -0700 | [diff] [blame] | 7391 | int ctrlIdx = -1; |
| 7392 | size_t size = mEffects.size(); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 7393 | |
Eric Laurent | cab1124 | 2010-07-15 12:50:15 -0700 | [diff] [blame] | 7394 | // first update volume controller |
| 7395 | for (size_t i = size; i > 0; i--) { |
Eric Laurent | 8f45bd7 | 2010-08-31 13:50:07 -0700 | [diff] [blame] | 7396 | if (mEffects[i - 1]->isProcessEnabled() && |
Eric Laurent | cab1124 | 2010-07-15 12:50:15 -0700 | [diff] [blame] | 7397 | (mEffects[i - 1]->desc().flags & EFFECT_FLAG_VOLUME_MASK) == EFFECT_FLAG_VOLUME_CTRL) { |
| 7398 | ctrlIdx = i - 1; |
Eric Laurent | f997cab | 2010-07-19 06:24:46 -0700 | [diff] [blame] | 7399 | hasControl = true; |
Eric Laurent | cab1124 | 2010-07-15 12:50:15 -0700 | [diff] [blame] | 7400 | break; |
| 7401 | } |
| 7402 | } |
| 7403 | |
| 7404 | if (ctrlIdx == mVolumeCtrlIdx && *left == mLeftVolume && *right == mRightVolume) { |
Eric Laurent | f997cab | 2010-07-19 06:24:46 -0700 | [diff] [blame] | 7405 | if (hasControl) { |
| 7406 | *left = mNewLeftVolume; |
| 7407 | *right = mNewRightVolume; |
| 7408 | } |
| 7409 | return hasControl; |
Eric Laurent | cab1124 | 2010-07-15 12:50:15 -0700 | [diff] [blame] | 7410 | } |
| 7411 | |
| 7412 | mVolumeCtrlIdx = ctrlIdx; |
Eric Laurent | f997cab | 2010-07-19 06:24:46 -0700 | [diff] [blame] | 7413 | mLeftVolume = newLeft; |
| 7414 | mRightVolume = newRight; |
Eric Laurent | cab1124 | 2010-07-15 12:50:15 -0700 | [diff] [blame] | 7415 | |
| 7416 | // second get volume update from volume controller |
| 7417 | if (ctrlIdx >= 0) { |
| 7418 | mEffects[ctrlIdx]->setVolume(&newLeft, &newRight, true); |
Eric Laurent | f997cab | 2010-07-19 06:24:46 -0700 | [diff] [blame] | 7419 | mNewLeftVolume = newLeft; |
| 7420 | mNewRightVolume = newRight; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 7421 | } |
| 7422 | // then indicate volume to all other effects in chain. |
| 7423 | // Pass altered volume to effects before volume controller |
| 7424 | // and requested volume to effects after controller |
| 7425 | uint32_t lVol = newLeft; |
| 7426 | uint32_t rVol = newRight; |
Eric Laurent | cab1124 | 2010-07-15 12:50:15 -0700 | [diff] [blame] | 7427 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 7428 | for (size_t i = 0; i < size; i++) { |
Eric Laurent | cab1124 | 2010-07-15 12:50:15 -0700 | [diff] [blame] | 7429 | if ((int)i == ctrlIdx) continue; |
| 7430 | // this also works for ctrlIdx == -1 when there is no volume controller |
| 7431 | if ((int)i > ctrlIdx) { |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 7432 | lVol = *left; |
| 7433 | rVol = *right; |
| 7434 | } |
| 7435 | mEffects[i]->setVolume(&lVol, &rVol, false); |
| 7436 | } |
| 7437 | *left = newLeft; |
| 7438 | *right = newRight; |
| 7439 | |
| 7440 | return hasControl; |
| 7441 | } |
| 7442 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 7443 | status_t AudioFlinger::EffectChain::dump(int fd, const Vector<String16>& args) |
| 7444 | { |
| 7445 | const size_t SIZE = 256; |
| 7446 | char buffer[SIZE]; |
| 7447 | String8 result; |
| 7448 | |
| 7449 | snprintf(buffer, SIZE, "Effects for session %d:\n", mSessionId); |
| 7450 | result.append(buffer); |
| 7451 | |
| 7452 | bool locked = tryLock(mLock); |
| 7453 | // failed to lock - AudioFlinger is probably deadlocked |
| 7454 | if (!locked) { |
| 7455 | result.append("\tCould not lock mutex:\n"); |
| 7456 | } |
| 7457 | |
Eric Laurent | cab1124 | 2010-07-15 12:50:15 -0700 | [diff] [blame] | 7458 | result.append("\tNum fx In buffer Out buffer Active tracks:\n"); |
| 7459 | snprintf(buffer, SIZE, "\t%02d 0x%08x 0x%08x %d\n", |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 7460 | mEffects.size(), |
| 7461 | (uint32_t)mInBuffer, |
| 7462 | (uint32_t)mOutBuffer, |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 7463 | mActiveTrackCnt); |
| 7464 | result.append(buffer); |
| 7465 | write(fd, result.string(), result.size()); |
| 7466 | |
| 7467 | for (size_t i = 0; i < mEffects.size(); ++i) { |
| 7468 | sp<EffectModule> effect = mEffects[i]; |
| 7469 | if (effect != 0) { |
| 7470 | effect->dump(fd, args); |
| 7471 | } |
| 7472 | } |
| 7473 | |
| 7474 | if (locked) { |
| 7475 | mLock.unlock(); |
| 7476 | } |
| 7477 | |
| 7478 | return NO_ERROR; |
| 7479 | } |
| 7480 | |
Eric Laurent | 59255e4 | 2011-07-27 19:49:51 -0700 | [diff] [blame] | 7481 | // must be called with ThreadBase::mLock held |
| 7482 | void AudioFlinger::EffectChain::setEffectSuspended_l( |
| 7483 | const effect_uuid_t *type, bool suspend) |
| 7484 | { |
| 7485 | sp<SuspendedEffectDesc> desc; |
| 7486 | // use effect type UUID timelow as key as there is no real risk of identical |
| 7487 | // timeLow fields among effect type UUIDs. |
| 7488 | int index = mSuspendedEffects.indexOfKey(type->timeLow); |
| 7489 | if (suspend) { |
| 7490 | if (index >= 0) { |
| 7491 | desc = mSuspendedEffects.valueAt(index); |
| 7492 | } else { |
| 7493 | desc = new SuspendedEffectDesc(); |
| 7494 | memcpy(&desc->mType, type, sizeof(effect_uuid_t)); |
| 7495 | mSuspendedEffects.add(type->timeLow, desc); |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 7496 | ALOGV("setEffectSuspended_l() add entry for %08x", type->timeLow); |
Eric Laurent | 59255e4 | 2011-07-27 19:49:51 -0700 | [diff] [blame] | 7497 | } |
| 7498 | if (desc->mRefCount++ == 0) { |
| 7499 | sp<EffectModule> effect = getEffectIfEnabled(type); |
| 7500 | if (effect != 0) { |
| 7501 | desc->mEffect = effect; |
| 7502 | effect->setSuspended(true); |
| 7503 | effect->setEnabled(false); |
| 7504 | } |
| 7505 | } |
| 7506 | } else { |
| 7507 | if (index < 0) { |
| 7508 | return; |
| 7509 | } |
| 7510 | desc = mSuspendedEffects.valueAt(index); |
| 7511 | if (desc->mRefCount <= 0) { |
Steve Block | 5ff1dd5 | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 7512 | ALOGW("setEffectSuspended_l() restore refcount should not be 0 %d", desc->mRefCount); |
Eric Laurent | 59255e4 | 2011-07-27 19:49:51 -0700 | [diff] [blame] | 7513 | desc->mRefCount = 1; |
| 7514 | } |
| 7515 | if (--desc->mRefCount == 0) { |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 7516 | ALOGV("setEffectSuspended_l() remove entry for %08x", mSuspendedEffects.keyAt(index)); |
Eric Laurent | 59255e4 | 2011-07-27 19:49:51 -0700 | [diff] [blame] | 7517 | if (desc->mEffect != 0) { |
| 7518 | sp<EffectModule> effect = desc->mEffect.promote(); |
| 7519 | if (effect != 0) { |
| 7520 | effect->setSuspended(false); |
| 7521 | sp<EffectHandle> handle = effect->controlHandle(); |
| 7522 | if (handle != 0) { |
| 7523 | effect->setEnabled(handle->enabled()); |
| 7524 | } |
| 7525 | } |
| 7526 | desc->mEffect.clear(); |
| 7527 | } |
| 7528 | mSuspendedEffects.removeItemsAt(index); |
| 7529 | } |
| 7530 | } |
| 7531 | } |
| 7532 | |
| 7533 | // must be called with ThreadBase::mLock held |
| 7534 | void AudioFlinger::EffectChain::setEffectSuspendedAll_l(bool suspend) |
| 7535 | { |
| 7536 | sp<SuspendedEffectDesc> desc; |
| 7537 | |
| 7538 | int index = mSuspendedEffects.indexOfKey((int)kKeyForSuspendAll); |
| 7539 | if (suspend) { |
| 7540 | if (index >= 0) { |
| 7541 | desc = mSuspendedEffects.valueAt(index); |
| 7542 | } else { |
| 7543 | desc = new SuspendedEffectDesc(); |
| 7544 | mSuspendedEffects.add((int)kKeyForSuspendAll, desc); |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 7545 | ALOGV("setEffectSuspendedAll_l() add entry for 0"); |
Eric Laurent | 59255e4 | 2011-07-27 19:49:51 -0700 | [diff] [blame] | 7546 | } |
| 7547 | if (desc->mRefCount++ == 0) { |
| 7548 | Vector< sp<EffectModule> > effects = getSuspendEligibleEffects(); |
| 7549 | for (size_t i = 0; i < effects.size(); i++) { |
| 7550 | setEffectSuspended_l(&effects[i]->desc().type, true); |
| 7551 | } |
| 7552 | } |
| 7553 | } else { |
| 7554 | if (index < 0) { |
| 7555 | return; |
| 7556 | } |
| 7557 | desc = mSuspendedEffects.valueAt(index); |
| 7558 | if (desc->mRefCount <= 0) { |
Steve Block | 5ff1dd5 | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 7559 | ALOGW("setEffectSuspendedAll_l() restore refcount should not be 0 %d", desc->mRefCount); |
Eric Laurent | 59255e4 | 2011-07-27 19:49:51 -0700 | [diff] [blame] | 7560 | desc->mRefCount = 1; |
| 7561 | } |
| 7562 | if (--desc->mRefCount == 0) { |
| 7563 | Vector<const effect_uuid_t *> types; |
| 7564 | for (size_t i = 0; i < mSuspendedEffects.size(); i++) { |
| 7565 | if (mSuspendedEffects.keyAt(i) == (int)kKeyForSuspendAll) { |
| 7566 | continue; |
| 7567 | } |
| 7568 | types.add(&mSuspendedEffects.valueAt(i)->mType); |
| 7569 | } |
| 7570 | for (size_t i = 0; i < types.size(); i++) { |
| 7571 | setEffectSuspended_l(types[i], false); |
| 7572 | } |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 7573 | ALOGV("setEffectSuspendedAll_l() remove entry for %08x", mSuspendedEffects.keyAt(index)); |
Eric Laurent | 59255e4 | 2011-07-27 19:49:51 -0700 | [diff] [blame] | 7574 | mSuspendedEffects.removeItem((int)kKeyForSuspendAll); |
| 7575 | } |
| 7576 | } |
| 7577 | } |
| 7578 | |
Eric Laurent | 6bffdb8 | 2011-09-23 08:40:41 -0700 | [diff] [blame] | 7579 | |
| 7580 | // The volume effect is used for automated tests only |
| 7581 | #ifndef OPENSL_ES_H_ |
| 7582 | static const effect_uuid_t SL_IID_VOLUME_ = { 0x09e8ede0, 0xddde, 0x11db, 0xb4f6, |
| 7583 | { 0x00, 0x02, 0xa5, 0xd5, 0xc5, 0x1b } }; |
| 7584 | const effect_uuid_t * const SL_IID_VOLUME = &SL_IID_VOLUME_; |
| 7585 | #endif //OPENSL_ES_H_ |
| 7586 | |
Eric Laurent | db7c079 | 2011-08-10 10:37:50 -0700 | [diff] [blame] | 7587 | bool AudioFlinger::EffectChain::isEffectEligibleForSuspend(const effect_descriptor_t& desc) |
| 7588 | { |
| 7589 | // auxiliary effects and visualizer are never suspended on output mix |
| 7590 | if ((mSessionId == AUDIO_SESSION_OUTPUT_MIX) && |
| 7591 | (((desc.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) || |
Eric Laurent | 6bffdb8 | 2011-09-23 08:40:41 -0700 | [diff] [blame] | 7592 | (memcmp(&desc.type, SL_IID_VISUALIZATION, sizeof(effect_uuid_t)) == 0) || |
| 7593 | (memcmp(&desc.type, SL_IID_VOLUME, sizeof(effect_uuid_t)) == 0))) { |
Eric Laurent | db7c079 | 2011-08-10 10:37:50 -0700 | [diff] [blame] | 7594 | return false; |
| 7595 | } |
| 7596 | return true; |
| 7597 | } |
| 7598 | |
Eric Laurent | 59255e4 | 2011-07-27 19:49:51 -0700 | [diff] [blame] | 7599 | Vector< sp<AudioFlinger::EffectModule> > AudioFlinger::EffectChain::getSuspendEligibleEffects() |
| 7600 | { |
| 7601 | Vector< sp<EffectModule> > effects; |
| 7602 | for (size_t i = 0; i < mEffects.size(); i++) { |
Eric Laurent | db7c079 | 2011-08-10 10:37:50 -0700 | [diff] [blame] | 7603 | if (!isEffectEligibleForSuspend(mEffects[i]->desc())) { |
Eric Laurent | 59255e4 | 2011-07-27 19:49:51 -0700 | [diff] [blame] | 7604 | continue; |
| 7605 | } |
| 7606 | effects.add(mEffects[i]); |
| 7607 | } |
| 7608 | return effects; |
| 7609 | } |
| 7610 | |
| 7611 | sp<AudioFlinger::EffectModule> AudioFlinger::EffectChain::getEffectIfEnabled( |
| 7612 | const effect_uuid_t *type) |
| 7613 | { |
| 7614 | sp<EffectModule> effect; |
| 7615 | effect = getEffectFromType_l(type); |
| 7616 | if (effect != 0 && !effect->isEnabled()) { |
| 7617 | effect.clear(); |
| 7618 | } |
| 7619 | return effect; |
| 7620 | } |
| 7621 | |
| 7622 | void AudioFlinger::EffectChain::checkSuspendOnEffectEnabled(const sp<EffectModule>& effect, |
| 7623 | bool enabled) |
| 7624 | { |
| 7625 | int index = mSuspendedEffects.indexOfKey(effect->desc().type.timeLow); |
| 7626 | if (enabled) { |
| 7627 | if (index < 0) { |
| 7628 | // if the effect is not suspend check if all effects are suspended |
| 7629 | index = mSuspendedEffects.indexOfKey((int)kKeyForSuspendAll); |
| 7630 | if (index < 0) { |
| 7631 | return; |
| 7632 | } |
Eric Laurent | db7c079 | 2011-08-10 10:37:50 -0700 | [diff] [blame] | 7633 | if (!isEffectEligibleForSuspend(effect->desc())) { |
| 7634 | return; |
| 7635 | } |
Eric Laurent | 59255e4 | 2011-07-27 19:49:51 -0700 | [diff] [blame] | 7636 | setEffectSuspended_l(&effect->desc().type, enabled); |
| 7637 | index = mSuspendedEffects.indexOfKey(effect->desc().type.timeLow); |
Eric Laurent | db7c079 | 2011-08-10 10:37:50 -0700 | [diff] [blame] | 7638 | if (index < 0) { |
Steve Block | 5ff1dd5 | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 7639 | ALOGW("checkSuspendOnEffectEnabled() Fx should be suspended here!"); |
Eric Laurent | db7c079 | 2011-08-10 10:37:50 -0700 | [diff] [blame] | 7640 | return; |
| 7641 | } |
Eric Laurent | 59255e4 | 2011-07-27 19:49:51 -0700 | [diff] [blame] | 7642 | } |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 7643 | ALOGV("checkSuspendOnEffectEnabled() enable suspending fx %08x", |
Eric Laurent | 59255e4 | 2011-07-27 19:49:51 -0700 | [diff] [blame] | 7644 | effect->desc().type.timeLow); |
| 7645 | sp<SuspendedEffectDesc> desc = mSuspendedEffects.valueAt(index); |
| 7646 | // if effect is requested to suspended but was not yet enabled, supend it now. |
| 7647 | if (desc->mEffect == 0) { |
| 7648 | desc->mEffect = effect; |
| 7649 | effect->setEnabled(false); |
| 7650 | effect->setSuspended(true); |
| 7651 | } |
| 7652 | } else { |
| 7653 | if (index < 0) { |
| 7654 | return; |
| 7655 | } |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 7656 | ALOGV("checkSuspendOnEffectEnabled() disable restoring fx %08x", |
Eric Laurent | 59255e4 | 2011-07-27 19:49:51 -0700 | [diff] [blame] | 7657 | effect->desc().type.timeLow); |
| 7658 | sp<SuspendedEffectDesc> desc = mSuspendedEffects.valueAt(index); |
| 7659 | desc->mEffect.clear(); |
| 7660 | effect->setSuspended(false); |
| 7661 | } |
| 7662 | } |
| 7663 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 7664 | #undef LOG_TAG |
| 7665 | #define LOG_TAG "AudioFlinger" |
| 7666 | |
| 7667 | // ---------------------------------------------------------------------------- |
| 7668 | |
| 7669 | status_t AudioFlinger::onTransact( |
| 7670 | uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags) |
| 7671 | { |
| 7672 | return BnAudioFlinger::onTransact(code, data, reply, flags); |
| 7673 | } |
| 7674 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 7675 | }; // namespace android |