The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -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" |
Eric Laurent | 7d850f2 | 2010-07-09 13:34:17 -0700 | [diff] [blame] | 20 | //#define LOG_NDEBUG 0 |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 21 | |
| 22 | #include <math.h> |
| 23 | #include <signal.h> |
| 24 | #include <sys/time.h> |
| 25 | #include <sys/resource.h> |
| 26 | |
Gloria Wang | 9b3f152 | 2011-02-24 14:51:45 -0800 | [diff] [blame] | 27 | #include <binder/IPCThreadState.h> |
Mathias Agopian | 0795272 | 2009-05-19 19:08:10 -0700 | [diff] [blame] | 28 | #include <binder/IServiceManager.h> |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 29 | #include <utils/Log.h> |
Mathias Agopian | 0795272 | 2009-05-19 19:08:10 -0700 | [diff] [blame] | 30 | #include <binder/Parcel.h> |
| 31 | #include <binder/IPCThreadState.h> |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 32 | #include <utils/String16.h> |
| 33 | #include <utils/threads.h> |
Eric Laurent | ae29b76 | 2011-03-28 18:37:07 -0700 | [diff] [blame] | 34 | #include <utils/Atomic.h> |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 35 | |
Dima Zavin | 24fc2fb | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 36 | #include <cutils/bitops.h> |
The Android Open Source Project | f013e1a | 2008-12-17 18:05:43 -0800 | [diff] [blame] | 37 | #include <cutils/properties.h> |
Glenn Kasten | e80a4cc | 2011-12-15 09:51:17 -0800 | [diff] [blame] | 38 | #include <cutils/compiler.h> |
The Android Open Source Project | f013e1a | 2008-12-17 18:05:43 -0800 | [diff] [blame] | 39 | |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 40 | #include <media/AudioTrack.h> |
| 41 | #include <media/AudioRecord.h> |
Gloria Wang | 9b3f152 | 2011-02-24 14:51:45 -0800 | [diff] [blame] | 42 | #include <media/IMediaPlayerService.h> |
Glenn Kasten | 26f260a | 2012-01-03 15:28:29 -0800 | [diff] [blame] | 43 | #include <media/IMediaDeathNotifier.h> |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 44 | |
| 45 | #include <private/media/AudioTrackShared.h> |
Eric Laurent | 65b6545 | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 46 | #include <private/media/AudioEffectShared.h> |
Dima Zavin | 24fc2fb | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 47 | |
Dima Zavin | 34bb419 | 2011-05-11 14:15:23 -0700 | [diff] [blame] | 48 | #include <system/audio.h> |
Dima Zavin | 290029d | 2011-06-13 18:16:26 -0700 | [diff] [blame] | 49 | #include <hardware/audio.h> |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 50 | |
| 51 | #include "AudioMixer.h" |
| 52 | #include "AudioFlinger.h" |
| 53 | |
Eric Laurent | 53334cd | 2010-06-23 17:38:20 -0700 | [diff] [blame] | 54 | #include <media/EffectsFactoryApi.h> |
Eric Laurent | 5cc0526 | 2011-06-24 07:01:31 -0700 | [diff] [blame] | 55 | #include <audio_effects/effect_visualizer.h> |
Eric Laurent | 6639b55 | 2011-08-01 09:52:20 -0700 | [diff] [blame] | 56 | #include <audio_effects/effect_ns.h> |
| 57 | #include <audio_effects/effect_aec.h> |
Eric Laurent | 65b6545 | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 58 | |
Glenn Kasten | 490909d | 2011-12-15 09:52:39 -0800 | [diff] [blame] | 59 | #include <audio_utils/primitives.h> |
| 60 | |
Glenn Kasten | da494f9 | 2011-07-08 15:26:12 -0700 | [diff] [blame] | 61 | #include <cpustats/ThreadCpuUsage.h> |
Eric Laurent | 6dbdc40 | 2011-07-22 09:04:31 -0700 | [diff] [blame] | 62 | #include <powermanager/PowerManager.h> |
Glenn Kasten | da494f9 | 2011-07-08 15:26:12 -0700 | [diff] [blame] | 63 | // #define DEBUG_CPU_USAGE 10 // log statistics every n wall clock seconds |
| 64 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 65 | // ---------------------------------------------------------------------------- |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 66 | |
Eric Laurent | 8ed6ed0 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 67 | |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 68 | namespace android { |
| 69 | |
Glenn Kasten | 62de3db | 2011-12-12 09:04:45 -0800 | [diff] [blame] | 70 | static const char kDeadlockedString[] = "AudioFlinger may be deadlocked\n"; |
| 71 | static const char kHardwareLockedString[] = "Hardware lock is taken\n"; |
The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 72 | |
The Android Open Source Project | f013e1a | 2008-12-17 18:05:43 -0800 | [diff] [blame] | 73 | //static const nsecs_t kStandbyTimeInNsecs = seconds(3); |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 74 | static const float MAX_GAIN = 4096.0f; |
Glenn Kasten | 0632bad | 2012-01-17 12:20:54 -0800 | [diff] [blame] | 75 | static const uint32_t MAX_GAIN_INT = 0x1000; |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 76 | |
| 77 | // retry counts for buffer fill timeout |
| 78 | // 50 * ~20msecs = 1 second |
| 79 | static const int8_t kMaxTrackRetries = 50; |
| 80 | static const int8_t kMaxTrackStartupRetries = 50; |
Eric Laurent | ef9500f | 2010-03-11 14:47:00 -0800 | [diff] [blame] | 81 | // allow less retry attempts on direct output thread. |
| 82 | // direct outputs can be a scarce resource in audio hardware and should |
| 83 | // be released as quickly as possible. |
| 84 | static const int8_t kMaxTrackRetriesDirect = 2; |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 85 | |
The Android Open Source Project | b2a3dd8 | 2009-03-09 11:52:12 -0700 | [diff] [blame] | 86 | static const int kDumpLockRetries = 50; |
Glenn Kasten | cbfe2e1 | 2011-12-13 11:04:14 -0800 | [diff] [blame] | 87 | static const int kDumpLockSleepUs = 20000; |
The Android Open Source Project | b2a3dd8 | 2009-03-09 11:52:12 -0700 | [diff] [blame] | 88 | |
Glenn Kasten | cbfe2e1 | 2011-12-13 11:04:14 -0800 | [diff] [blame] | 89 | // don't warn about blocked writes or record buffer overflows more often than this |
| 90 | static const nsecs_t kWarningThrottleNs = seconds(5); |
Dave Sparks | d0ac8c0 | 2009-09-30 03:09:03 -0700 | [diff] [blame] | 91 | |
Eric Laurent | 464d5b3 | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 92 | // RecordThread loop sleep time upon application overrun or audio HAL read error |
| 93 | static const int kRecordThreadSleepUs = 5000; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 94 | |
Glenn Kasten | cbfe2e1 | 2011-12-13 11:04:14 -0800 | [diff] [blame] | 95 | // maximum time to wait for setParameters to complete |
| 96 | static const nsecs_t kSetParametersTimeoutNs = seconds(2); |
Eric Laurent | 5f37be3 | 2011-09-13 11:40:21 -0700 | [diff] [blame] | 97 | |
Eric Laurent | f1f5fc8 | 2011-11-22 18:50:29 -0800 | [diff] [blame] | 98 | // minimum sleep time for the mixer thread loop when tracks are active but in underrun |
| 99 | static const uint32_t kMinThreadSleepTimeUs = 5000; |
| 100 | // maximum divider applied to the active sleep time in the mixer thread loop |
| 101 | static const uint32_t kMaxThreadSleepTimeShift = 2; |
| 102 | |
| 103 | |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 104 | // ---------------------------------------------------------------------------- |
| 105 | |
| 106 | static bool recordingAllowed() { |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 107 | if (getpid() == IPCThreadState::self()->getCallingPid()) return true; |
| 108 | bool ok = checkCallingPermission(String16("android.permission.RECORD_AUDIO")); |
Steve Block | 3762c31 | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 109 | if (!ok) ALOGE("Request requires android.permission.RECORD_AUDIO"); |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 110 | return ok; |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 111 | } |
| 112 | |
| 113 | static bool settingsAllowed() { |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 114 | if (getpid() == IPCThreadState::self()->getCallingPid()) return true; |
| 115 | bool ok = checkCallingPermission(String16("android.permission.MODIFY_AUDIO_SETTINGS")); |
Steve Block | 3762c31 | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 116 | if (!ok) ALOGE("Request requires android.permission.MODIFY_AUDIO_SETTINGS"); |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 117 | return ok; |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 118 | } |
| 119 | |
Gloria Wang | 9b3f152 | 2011-02-24 14:51:45 -0800 | [diff] [blame] | 120 | // To collect the amplifier usage |
| 121 | static void addBatteryData(uint32_t params) { |
Glenn Kasten | 26f260a | 2012-01-03 15:28:29 -0800 | [diff] [blame] | 122 | sp<IMediaPlayerService> service = IMediaDeathNotifier::getMediaPlayerService(); |
| 123 | if (service == NULL) { |
| 124 | // it already logged |
Gloria Wang | 9b3f152 | 2011-02-24 14:51:45 -0800 | [diff] [blame] | 125 | return; |
| 126 | } |
| 127 | |
| 128 | service->addBatteryData(params); |
| 129 | } |
| 130 | |
Dima Zavin | 31f18889 | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 131 | static int load_audio_interface(const char *if_name, const hw_module_t **mod, |
| 132 | audio_hw_device_t **dev) |
| 133 | { |
| 134 | int rc; |
| 135 | |
| 136 | rc = hw_get_module_by_class(AUDIO_HARDWARE_MODULE_ID, if_name, mod); |
| 137 | if (rc) |
| 138 | goto out; |
| 139 | |
| 140 | rc = audio_hw_device_open(*mod, dev); |
Steve Block | 3762c31 | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 141 | ALOGE_IF(rc, "couldn't open audio hw device in %s.%s (%s)", |
Dima Zavin | 31f18889 | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 142 | AUDIO_HARDWARE_MODULE_ID, if_name, strerror(-rc)); |
| 143 | if (rc) |
| 144 | goto out; |
| 145 | |
| 146 | return 0; |
| 147 | |
| 148 | out: |
| 149 | *mod = NULL; |
| 150 | *dev = NULL; |
| 151 | return rc; |
| 152 | } |
| 153 | |
Glenn Kasten | 62de3db | 2011-12-12 09:04:45 -0800 | [diff] [blame] | 154 | static const char * const audio_interfaces[] = { |
Dima Zavin | 31f18889 | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 155 | "primary", |
| 156 | "a2dp", |
| 157 | "usb", |
| 158 | }; |
| 159 | #define ARRAY_SIZE(x) (sizeof((x))/sizeof(((x)[0]))) |
| 160 | |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 161 | // ---------------------------------------------------------------------------- |
| 162 | |
| 163 | AudioFlinger::AudioFlinger() |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 164 | : BnAudioFlinger(), |
Glenn Kasten | c434c90 | 2011-12-13 11:53:26 -0800 | [diff] [blame] | 165 | mPrimaryHardwareDev(NULL), mMasterVolume(1.0f), mMasterMute(false), mNextUniqueId(1), |
Eric Laurent | 2d95dfb | 2011-08-29 12:42:48 -0700 | [diff] [blame] | 166 | mBtNrecIsOff(false) |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 167 | { |
Dima Zavin | 2986f5b | 2011-04-19 19:04:32 -0700 | [diff] [blame] | 168 | } |
| 169 | |
| 170 | void AudioFlinger::onFirstRef() |
| 171 | { |
Dima Zavin | 31f18889 | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 172 | int rc = 0; |
Dima Zavin | 24fc2fb | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 173 | |
Eric Laurent | 0163594 | 2011-01-18 18:39:02 -0800 | [diff] [blame] | 174 | Mutex::Autolock _l(mLock); |
| 175 | |
Dima Zavin | 31f18889 | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 176 | /* TODO: move all this work into an Init() function */ |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 177 | mHardwareStatus = AUDIO_HW_IDLE; |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 178 | |
Dima Zavin | 31f18889 | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 179 | for (size_t i = 0; i < ARRAY_SIZE(audio_interfaces); i++) { |
| 180 | const hw_module_t *mod; |
| 181 | audio_hw_device_t *dev; |
Dima Zavin | 24fc2fb | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 182 | |
Dima Zavin | 31f18889 | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 183 | rc = load_audio_interface(audio_interfaces[i], &mod, &dev); |
| 184 | if (rc) |
| 185 | continue; |
| 186 | |
Steve Block | 6215d3f | 2012-01-04 20:05:49 +0000 | [diff] [blame] | 187 | ALOGI("Loaded %s audio interface from %s (%s)", audio_interfaces[i], |
Dima Zavin | 31f18889 | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 188 | mod->name, mod->id); |
| 189 | mAudioHwDevs.push(dev); |
| 190 | |
| 191 | if (!mPrimaryHardwareDev) { |
| 192 | mPrimaryHardwareDev = dev; |
Steve Block | 6215d3f | 2012-01-04 20:05:49 +0000 | [diff] [blame] | 193 | ALOGI("Using '%s' (%s.%s) as the primary audio interface", |
Dima Zavin | 2986f5b | 2011-04-19 19:04:32 -0700 | [diff] [blame] | 194 | mod->name, mod->id, audio_interfaces[i]); |
Dima Zavin | 31f18889 | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 195 | } |
| 196 | } |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 197 | |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 198 | mHardwareStatus = AUDIO_HW_INIT; |
Dima Zavin | 24fc2fb | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 199 | |
Dima Zavin | 31f18889 | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 200 | if (!mPrimaryHardwareDev || mAudioHwDevs.size() == 0) { |
Steve Block | 3762c31 | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 201 | ALOGE("Primary audio interface not found"); |
Dima Zavin | 31f18889 | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 202 | return; |
| 203 | } |
| 204 | |
| 205 | for (size_t i = 0; i < mAudioHwDevs.size(); i++) { |
| 206 | audio_hw_device_t *dev = mAudioHwDevs[i]; |
| 207 | |
| 208 | mHardwareStatus = AUDIO_HW_INIT; |
| 209 | rc = dev->init_check(dev); |
| 210 | if (rc == 0) { |
| 211 | AutoMutex lock(mHardwareLock); |
| 212 | |
| 213 | mMode = AUDIO_MODE_NORMAL; |
| 214 | mHardwareStatus = AUDIO_HW_SET_MODE; |
| 215 | dev->set_mode(dev, mMode); |
| 216 | mHardwareStatus = AUDIO_HW_SET_MASTER_VOLUME; |
| 217 | dev->set_master_volume(dev, 1.0f); |
| 218 | mHardwareStatus = AUDIO_HW_IDLE; |
| 219 | } |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 220 | } |
| 221 | } |
| 222 | |
Dima Zavin | 2986f5b | 2011-04-19 19:04:32 -0700 | [diff] [blame] | 223 | status_t AudioFlinger::initCheck() const |
| 224 | { |
| 225 | Mutex::Autolock _l(mLock); |
| 226 | if (mPrimaryHardwareDev == NULL || mAudioHwDevs.size() == 0) |
| 227 | return NO_INIT; |
| 228 | return NO_ERROR; |
| 229 | } |
| 230 | |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 231 | AudioFlinger::~AudioFlinger() |
| 232 | { |
Dima Zavin | 31f18889 | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 233 | int num_devs = mAudioHwDevs.size(); |
| 234 | |
Eric Laurent | 7954c46 | 2009-08-28 10:39:03 -0700 | [diff] [blame] | 235 | while (!mRecordThreads.isEmpty()) { |
| 236 | // closeInput() will remove first entry from mRecordThreads |
| 237 | closeInput(mRecordThreads.keyAt(0)); |
| 238 | } |
| 239 | while (!mPlaybackThreads.isEmpty()) { |
| 240 | // closeOutput() will remove first entry from mPlaybackThreads |
| 241 | closeOutput(mPlaybackThreads.keyAt(0)); |
| 242 | } |
Dima Zavin | 31f18889 | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 243 | |
| 244 | for (int i = 0; i < num_devs; i++) { |
| 245 | audio_hw_device_t *dev = mAudioHwDevs[i]; |
| 246 | audio_hw_device_close(dev); |
Eric Laurent | 7954c46 | 2009-08-28 10:39:03 -0700 | [diff] [blame] | 247 | } |
Dima Zavin | 31f18889 | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 248 | mAudioHwDevs.clear(); |
The Android Open Source Project | b798689 | 2009-01-09 17:51:23 -0800 | [diff] [blame] | 249 | } |
The Android Open Source Project | f013e1a | 2008-12-17 18:05:43 -0800 | [diff] [blame] | 250 | |
Dima Zavin | 31f18889 | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 251 | audio_hw_device_t* AudioFlinger::findSuitableHwDev_l(uint32_t devices) |
| 252 | { |
| 253 | /* first matching HW device is returned */ |
| 254 | for (size_t i = 0; i < mAudioHwDevs.size(); i++) { |
| 255 | audio_hw_device_t *dev = mAudioHwDevs[i]; |
| 256 | if ((dev->get_supported_devices(dev) & devices) == devices) |
| 257 | return dev; |
| 258 | } |
| 259 | return NULL; |
| 260 | } |
The Android Open Source Project | c39a6e0 | 2009-03-11 12:11:56 -0700 | [diff] [blame] | 261 | |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 262 | status_t AudioFlinger::dumpClients(int fd, const Vector<String16>& args) |
| 263 | { |
| 264 | const size_t SIZE = 256; |
| 265 | char buffer[SIZE]; |
| 266 | String8 result; |
| 267 | |
| 268 | result.append("Clients:\n"); |
| 269 | for (size_t i = 0; i < mClients.size(); ++i) { |
| 270 | wp<Client> wClient = mClients.valueAt(i); |
| 271 | if (wClient != 0) { |
| 272 | sp<Client> client = wClient.promote(); |
| 273 | if (client != 0) { |
| 274 | snprintf(buffer, SIZE, " pid: %d\n", client->pid()); |
| 275 | result.append(buffer); |
| 276 | } |
| 277 | } |
| 278 | } |
Marco Nelissen | c74b93f | 2011-08-02 13:33:41 -0700 | [diff] [blame] | 279 | |
| 280 | result.append("Global session refs:\n"); |
| 281 | result.append(" session pid cnt\n"); |
| 282 | for (size_t i = 0; i < mAudioSessionRefs.size(); i++) { |
| 283 | AudioSessionRef *r = mAudioSessionRefs[i]; |
| 284 | snprintf(buffer, SIZE, " %7d %3d %3d\n", r->sessionid, r->pid, r->cnt); |
| 285 | result.append(buffer); |
| 286 | } |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 287 | write(fd, result.string(), result.size()); |
| 288 | return NO_ERROR; |
| 289 | } |
| 290 | |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 291 | |
| 292 | status_t AudioFlinger::dumpInternals(int fd, const Vector<String16>& args) |
| 293 | { |
| 294 | const size_t SIZE = 256; |
| 295 | char buffer[SIZE]; |
| 296 | String8 result; |
Glenn Kasten | a934c2c | 2012-01-04 11:02:33 -0800 | [diff] [blame] | 297 | hardware_call_state hardwareStatus = mHardwareStatus; |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 298 | |
The Android Open Source Project | b2a3dd8 | 2009-03-09 11:52:12 -0700 | [diff] [blame] | 299 | snprintf(buffer, SIZE, "Hardware status: %d\n", hardwareStatus); |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 300 | result.append(buffer); |
| 301 | write(fd, result.string(), result.size()); |
| 302 | return NO_ERROR; |
| 303 | } |
| 304 | |
| 305 | status_t AudioFlinger::dumpPermissionDenial(int fd, const Vector<String16>& args) |
| 306 | { |
| 307 | const size_t SIZE = 256; |
| 308 | char buffer[SIZE]; |
| 309 | String8 result; |
| 310 | snprintf(buffer, SIZE, "Permission Denial: " |
| 311 | "can't dump AudioFlinger from pid=%d, uid=%d\n", |
| 312 | IPCThreadState::self()->getCallingPid(), |
| 313 | IPCThreadState::self()->getCallingUid()); |
| 314 | result.append(buffer); |
| 315 | write(fd, result.string(), result.size()); |
| 316 | return NO_ERROR; |
| 317 | } |
| 318 | |
The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 319 | static bool tryLock(Mutex& mutex) |
| 320 | { |
| 321 | bool locked = false; |
| 322 | for (int i = 0; i < kDumpLockRetries; ++i) { |
| 323 | if (mutex.tryLock() == NO_ERROR) { |
| 324 | locked = true; |
| 325 | break; |
| 326 | } |
Glenn Kasten | cbfe2e1 | 2011-12-13 11:04:14 -0800 | [diff] [blame] | 327 | usleep(kDumpLockSleepUs); |
The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 328 | } |
| 329 | return locked; |
| 330 | } |
| 331 | |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 332 | status_t AudioFlinger::dump(int fd, const Vector<String16>& args) |
| 333 | { |
Glenn Kasten | b737dbb | 2012-01-13 15:54:24 -0800 | [diff] [blame] | 334 | if (!checkCallingPermission(String16("android.permission.DUMP"))) { |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 335 | dumpPermissionDenial(fd, args); |
| 336 | } else { |
The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 337 | // get state of hardware lock |
| 338 | bool hardwareLocked = tryLock(mHardwareLock); |
| 339 | if (!hardwareLocked) { |
| 340 | String8 result(kHardwareLockedString); |
| 341 | write(fd, result.string(), result.size()); |
| 342 | } else { |
| 343 | mHardwareLock.unlock(); |
| 344 | } |
| 345 | |
| 346 | bool locked = tryLock(mLock); |
| 347 | |
| 348 | // failed to lock - AudioFlinger is probably deadlocked |
| 349 | if (!locked) { |
| 350 | String8 result(kDeadlockedString); |
| 351 | write(fd, result.string(), result.size()); |
The Android Open Source Project | b2a3dd8 | 2009-03-09 11:52:12 -0700 | [diff] [blame] | 352 | } |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 353 | |
| 354 | dumpClients(fd, args); |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 355 | dumpInternals(fd, args); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 356 | |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 357 | // dump playback threads |
| 358 | for (size_t i = 0; i < mPlaybackThreads.size(); i++) { |
Eric Laurent | ddb78e7 | 2009-07-28 08:44:33 -0700 | [diff] [blame] | 359 | mPlaybackThreads.valueAt(i)->dump(fd, args); |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 360 | } |
| 361 | |
| 362 | // dump record threads |
Eric Laurent | 102313a | 2009-07-23 13:35:01 -0700 | [diff] [blame] | 363 | for (size_t i = 0; i < mRecordThreads.size(); i++) { |
Eric Laurent | ddb78e7 | 2009-07-28 08:44:33 -0700 | [diff] [blame] | 364 | mRecordThreads.valueAt(i)->dump(fd, args); |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 365 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 366 | |
Dima Zavin | 31f18889 | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 367 | // dump all hardware devs |
| 368 | for (size_t i = 0; i < mAudioHwDevs.size(); i++) { |
| 369 | audio_hw_device_t *dev = mAudioHwDevs[i]; |
| 370 | dev->dump(dev, fd); |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 371 | } |
The Android Open Source Project | b2a3dd8 | 2009-03-09 11:52:12 -0700 | [diff] [blame] | 372 | if (locked) mLock.unlock(); |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 373 | } |
| 374 | return NO_ERROR; |
| 375 | } |
| 376 | |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 377 | |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 378 | // IAudioFlinger interface |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 379 | |
| 380 | |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 381 | sp<IAudioTrack> AudioFlinger::createTrack( |
| 382 | pid_t pid, |
Glenn Kasten | bc1d77b | 2012-01-12 16:38:12 -0800 | [diff] [blame] | 383 | audio_stream_type_t streamType, |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 384 | uint32_t sampleRate, |
Glenn Kasten | 0a204ed | 2012-01-12 12:27:51 -0800 | [diff] [blame^] | 385 | audio_format_t format, |
Jean-Michel Trivi | 5439223 | 2011-05-24 15:53:33 -0700 | [diff] [blame] | 386 | uint32_t channelMask, |
The Android Open Source Project | f013e1a | 2008-12-17 18:05:43 -0800 | [diff] [blame] | 387 | int frameCount, |
| 388 | uint32_t flags, |
| 389 | const sp<IMemory>& sharedBuffer, |
Eric Laurent | ddb78e7 | 2009-07-28 08:44:33 -0700 | [diff] [blame] | 390 | int output, |
Eric Laurent | 65b6545 | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 391 | int *sessionId, |
The Android Open Source Project | f013e1a | 2008-12-17 18:05:43 -0800 | [diff] [blame] | 392 | status_t *status) |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 393 | { |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 394 | sp<PlaybackThread::Track> track; |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 395 | sp<TrackHandle> trackHandle; |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 396 | sp<Client> client; |
The Android Open Source Project | f013e1a | 2008-12-17 18:05:43 -0800 | [diff] [blame] | 397 | wp<Client> wclient; |
| 398 | status_t lStatus; |
Eric Laurent | 65b6545 | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 399 | int lSessionId; |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 400 | |
Glenn Kasten | 6e987a4 | 2012-01-06 08:40:01 -0800 | [diff] [blame] | 401 | // client AudioTrack::set already implements AUDIO_STREAM_DEFAULT => AUDIO_STREAM_MUSIC, |
| 402 | // but if someone uses binder directly they could bypass that and cause us to crash |
| 403 | if (uint32_t(streamType) >= AUDIO_STREAM_CNT) { |
Steve Block | 3762c31 | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 404 | ALOGE("createTrack() invalid stream type %d", streamType); |
The Android Open Source Project | f013e1a | 2008-12-17 18:05:43 -0800 | [diff] [blame] | 405 | lStatus = BAD_VALUE; |
| 406 | goto Exit; |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 407 | } |
| 408 | |
The Android Open Source Project | f013e1a | 2008-12-17 18:05:43 -0800 | [diff] [blame] | 409 | { |
| 410 | Mutex::Autolock _l(mLock); |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 411 | PlaybackThread *thread = checkPlaybackThread_l(output); |
Eric Laurent | 493941b | 2010-07-28 01:32:47 -0700 | [diff] [blame] | 412 | PlaybackThread *effectThread = NULL; |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 413 | if (thread == NULL) { |
Steve Block | 3762c31 | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 414 | ALOGE("unknown output thread"); |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 415 | lStatus = BAD_VALUE; |
| 416 | goto Exit; |
| 417 | } |
The Android Open Source Project | f013e1a | 2008-12-17 18:05:43 -0800 | [diff] [blame] | 418 | |
The Android Open Source Project | f013e1a | 2008-12-17 18:05:43 -0800 | [diff] [blame] | 419 | wclient = mClients.valueFor(pid); |
| 420 | |
| 421 | if (wclient != NULL) { |
| 422 | client = wclient.promote(); |
| 423 | } else { |
| 424 | client = new Client(this, pid); |
| 425 | mClients.add(pid, client); |
| 426 | } |
Eric Laurent | 65b6545 | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 427 | |
Steve Block | 71f2cf1 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 428 | ALOGV("createTrack() sessionId: %d", (sessionId == NULL) ? -2 : *sessionId); |
Dima Zavin | 24fc2fb | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 429 | if (sessionId != NULL && *sessionId != AUDIO_SESSION_OUTPUT_MIX) { |
Eric Laurent | 8ed6ed0 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 430 | for (size_t i = 0; i < mPlaybackThreads.size(); i++) { |
Eric Laurent | 493941b | 2010-07-28 01:32:47 -0700 | [diff] [blame] | 431 | sp<PlaybackThread> t = mPlaybackThreads.valueAt(i); |
| 432 | if (mPlaybackThreads.keyAt(i) != output) { |
| 433 | // prevent same audio session on different output threads |
| 434 | uint32_t sessions = t->hasAudioSession(*sessionId); |
| 435 | if (sessions & PlaybackThread::TRACK_SESSION) { |
Steve Block | 3762c31 | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 436 | ALOGE("createTrack() session ID %d already in use", *sessionId); |
Eric Laurent | 493941b | 2010-07-28 01:32:47 -0700 | [diff] [blame] | 437 | lStatus = BAD_VALUE; |
| 438 | goto Exit; |
| 439 | } |
| 440 | // check if an effect with same session ID is waiting for a track to be created |
| 441 | if (sessions & PlaybackThread::EFFECT_SESSION) { |
| 442 | effectThread = t.get(); |
| 443 | } |
Eric Laurent | 8ed6ed0 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 444 | } |
| 445 | } |
Eric Laurent | 65b6545 | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 446 | lSessionId = *sessionId; |
| 447 | } else { |
Eric Laurent | 8ed6ed0 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 448 | // if no audio session id is provided, create one here |
Eric Laurent | 464d5b3 | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 449 | lSessionId = nextUniqueId(); |
Eric Laurent | 65b6545 | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 450 | if (sessionId != NULL) { |
| 451 | *sessionId = lSessionId; |
| 452 | } |
| 453 | } |
Steve Block | 71f2cf1 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 454 | ALOGV("createTrack() lSessionId: %d", lSessionId); |
Eric Laurent | 65b6545 | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 455 | |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 456 | track = thread->createTrack_l(client, streamType, sampleRate, format, |
Jean-Michel Trivi | 5439223 | 2011-05-24 15:53:33 -0700 | [diff] [blame] | 457 | channelMask, frameCount, sharedBuffer, lSessionId, &lStatus); |
Eric Laurent | 493941b | 2010-07-28 01:32:47 -0700 | [diff] [blame] | 458 | |
| 459 | // move effect chain to this output thread if an effect on same session was waiting |
| 460 | // for a track to be created |
| 461 | if (lStatus == NO_ERROR && effectThread != NULL) { |
| 462 | Mutex::Autolock _dl(thread->mLock); |
| 463 | Mutex::Autolock _sl(effectThread->mLock); |
| 464 | moveEffectChain_l(lSessionId, effectThread, thread, true); |
| 465 | } |
The Android Open Source Project | b2a3dd8 | 2009-03-09 11:52:12 -0700 | [diff] [blame] | 466 | } |
| 467 | if (lStatus == NO_ERROR) { |
The Android Open Source Project | f013e1a | 2008-12-17 18:05:43 -0800 | [diff] [blame] | 468 | trackHandle = new TrackHandle(track); |
The Android Open Source Project | b2a3dd8 | 2009-03-09 11:52:12 -0700 | [diff] [blame] | 469 | } else { |
Eric Laurent | b9481d8 | 2009-09-17 05:12:56 -0700 | [diff] [blame] | 470 | // remove local strong reference to Client before deleting the Track so that the Client |
| 471 | // destructor is called by the TrackBase destructor with mLock held |
| 472 | client.clear(); |
The Android Open Source Project | b2a3dd8 | 2009-03-09 11:52:12 -0700 | [diff] [blame] | 473 | track.clear(); |
The Android Open Source Project | f013e1a | 2008-12-17 18:05:43 -0800 | [diff] [blame] | 474 | } |
| 475 | |
| 476 | Exit: |
| 477 | if(status) { |
| 478 | *status = lStatus; |
| 479 | } |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 480 | return trackHandle; |
| 481 | } |
| 482 | |
Eric Laurent | ddb78e7 | 2009-07-28 08:44:33 -0700 | [diff] [blame] | 483 | uint32_t AudioFlinger::sampleRate(int output) const |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 484 | { |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 485 | Mutex::Autolock _l(mLock); |
| 486 | PlaybackThread *thread = checkPlaybackThread_l(output); |
| 487 | if (thread == NULL) { |
Steve Block | 8564c8d | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 488 | ALOGW("sampleRate() unknown thread %d", output); |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 489 | return 0; |
| 490 | } |
| 491 | return thread->sampleRate(); |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 492 | } |
| 493 | |
Eric Laurent | ddb78e7 | 2009-07-28 08:44:33 -0700 | [diff] [blame] | 494 | int AudioFlinger::channelCount(int output) const |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 495 | { |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 496 | Mutex::Autolock _l(mLock); |
| 497 | PlaybackThread *thread = checkPlaybackThread_l(output); |
| 498 | if (thread == NULL) { |
Steve Block | 8564c8d | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 499 | ALOGW("channelCount() unknown thread %d", output); |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 500 | return 0; |
| 501 | } |
| 502 | return thread->channelCount(); |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 503 | } |
| 504 | |
Glenn Kasten | 0a204ed | 2012-01-12 12:27:51 -0800 | [diff] [blame^] | 505 | audio_format_t AudioFlinger::format(int output) const |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 506 | { |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 507 | Mutex::Autolock _l(mLock); |
| 508 | PlaybackThread *thread = checkPlaybackThread_l(output); |
| 509 | if (thread == NULL) { |
Steve Block | 8564c8d | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 510 | ALOGW("format() unknown thread %d", output); |
Glenn Kasten | 0a204ed | 2012-01-12 12:27:51 -0800 | [diff] [blame^] | 511 | return AUDIO_FORMAT_INVALID; |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 512 | } |
| 513 | return thread->format(); |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 514 | } |
| 515 | |
Eric Laurent | ddb78e7 | 2009-07-28 08:44:33 -0700 | [diff] [blame] | 516 | size_t AudioFlinger::frameCount(int output) const |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 517 | { |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 518 | Mutex::Autolock _l(mLock); |
| 519 | PlaybackThread *thread = checkPlaybackThread_l(output); |
| 520 | if (thread == NULL) { |
Steve Block | 8564c8d | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 521 | ALOGW("frameCount() unknown thread %d", output); |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 522 | return 0; |
| 523 | } |
| 524 | return thread->frameCount(); |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 525 | } |
| 526 | |
Eric Laurent | ddb78e7 | 2009-07-28 08:44:33 -0700 | [diff] [blame] | 527 | uint32_t AudioFlinger::latency(int output) const |
The Android Open Source Project | f013e1a | 2008-12-17 18:05:43 -0800 | [diff] [blame] | 528 | { |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 529 | Mutex::Autolock _l(mLock); |
| 530 | PlaybackThread *thread = checkPlaybackThread_l(output); |
| 531 | if (thread == NULL) { |
Steve Block | 8564c8d | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 532 | ALOGW("latency() unknown thread %d", output); |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 533 | return 0; |
| 534 | } |
| 535 | return thread->latency(); |
The Android Open Source Project | f013e1a | 2008-12-17 18:05:43 -0800 | [diff] [blame] | 536 | } |
| 537 | |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 538 | status_t AudioFlinger::setMasterVolume(float value) |
| 539 | { |
Eric Laurent | 8b4dbf7 | 2011-08-23 08:25:03 -0700 | [diff] [blame] | 540 | status_t ret = initCheck(); |
| 541 | if (ret != NO_ERROR) { |
| 542 | return ret; |
| 543 | } |
| 544 | |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 545 | // check calling permissions |
| 546 | if (!settingsAllowed()) { |
| 547 | return PERMISSION_DENIED; |
| 548 | } |
| 549 | |
| 550 | // when hw supports master volume, don't scale in sw mixer |
Eric Laurent | 0163594 | 2011-01-18 18:39:02 -0800 | [diff] [blame] | 551 | { // scope for the lock |
| 552 | AutoMutex lock(mHardwareLock); |
| 553 | mHardwareStatus = AUDIO_HW_SET_MASTER_VOLUME; |
Dima Zavin | 31f18889 | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 554 | if (mPrimaryHardwareDev->set_master_volume(mPrimaryHardwareDev, value) == NO_ERROR) { |
Eric Laurent | 0163594 | 2011-01-18 18:39:02 -0800 | [diff] [blame] | 555 | value = 1.0f; |
| 556 | } |
| 557 | mHardwareStatus = AUDIO_HW_IDLE; |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 558 | } |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 559 | |
Eric Laurent | 0163594 | 2011-01-18 18:39:02 -0800 | [diff] [blame] | 560 | Mutex::Autolock _l(mLock); |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 561 | mMasterVolume = value; |
| 562 | for (uint32_t i = 0; i < mPlaybackThreads.size(); i++) |
Eric Laurent | ddb78e7 | 2009-07-28 08:44:33 -0700 | [diff] [blame] | 563 | mPlaybackThreads.valueAt(i)->setMasterVolume(value); |
The Android Open Source Project | b2a3dd8 | 2009-03-09 11:52:12 -0700 | [diff] [blame] | 564 | |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 565 | return NO_ERROR; |
| 566 | } |
| 567 | |
Glenn Kasten | accb114 | 2012-01-04 11:00:47 -0800 | [diff] [blame] | 568 | status_t AudioFlinger::setMode(audio_mode_t mode) |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 569 | { |
Eric Laurent | 8b4dbf7 | 2011-08-23 08:25:03 -0700 | [diff] [blame] | 570 | status_t ret = initCheck(); |
| 571 | if (ret != NO_ERROR) { |
| 572 | return ret; |
| 573 | } |
Eric Laurent | 53334cd | 2010-06-23 17:38:20 -0700 | [diff] [blame] | 574 | |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 575 | // check calling permissions |
| 576 | if (!settingsAllowed()) { |
| 577 | return PERMISSION_DENIED; |
| 578 | } |
Glenn Kasten | 01aaf2c | 2012-01-06 16:47:31 -0800 | [diff] [blame] | 579 | if (uint32_t(mode) >= AUDIO_MODE_CNT) { |
Steve Block | 8564c8d | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 580 | ALOGW("Illegal value: setMode(%d)", mode); |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 581 | return BAD_VALUE; |
| 582 | } |
| 583 | |
Eric Laurent | 53334cd | 2010-06-23 17:38:20 -0700 | [diff] [blame] | 584 | { // scope for the lock |
| 585 | AutoMutex lock(mHardwareLock); |
| 586 | mHardwareStatus = AUDIO_HW_SET_MODE; |
Dima Zavin | 31f18889 | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 587 | ret = mPrimaryHardwareDev->set_mode(mPrimaryHardwareDev, mode); |
Eric Laurent | 53334cd | 2010-06-23 17:38:20 -0700 | [diff] [blame] | 588 | mHardwareStatus = AUDIO_HW_IDLE; |
Glenn Kasten | 871c16c | 2010-03-05 12:18:01 -0800 | [diff] [blame] | 589 | } |
Eric Laurent | 53334cd | 2010-06-23 17:38:20 -0700 | [diff] [blame] | 590 | |
| 591 | if (NO_ERROR == ret) { |
| 592 | Mutex::Autolock _l(mLock); |
| 593 | mMode = mode; |
| 594 | for (uint32_t i = 0; i < mPlaybackThreads.size(); i++) |
| 595 | mPlaybackThreads.valueAt(i)->setMode(mode); |
Eric Laurent | 53334cd | 2010-06-23 17:38:20 -0700 | [diff] [blame] | 596 | } |
| 597 | |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 598 | return ret; |
| 599 | } |
| 600 | |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 601 | status_t AudioFlinger::setMicMute(bool state) |
| 602 | { |
Eric Laurent | 8b4dbf7 | 2011-08-23 08:25:03 -0700 | [diff] [blame] | 603 | status_t ret = initCheck(); |
| 604 | if (ret != NO_ERROR) { |
| 605 | return ret; |
| 606 | } |
| 607 | |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 608 | // check calling permissions |
| 609 | if (!settingsAllowed()) { |
| 610 | return PERMISSION_DENIED; |
| 611 | } |
| 612 | |
| 613 | AutoMutex lock(mHardwareLock); |
| 614 | mHardwareStatus = AUDIO_HW_SET_MIC_MUTE; |
Eric Laurent | 8b4dbf7 | 2011-08-23 08:25:03 -0700 | [diff] [blame] | 615 | ret = mPrimaryHardwareDev->set_mic_mute(mPrimaryHardwareDev, state); |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 616 | mHardwareStatus = AUDIO_HW_IDLE; |
| 617 | return ret; |
| 618 | } |
| 619 | |
| 620 | bool AudioFlinger::getMicMute() const |
| 621 | { |
Eric Laurent | 8b4dbf7 | 2011-08-23 08:25:03 -0700 | [diff] [blame] | 622 | status_t ret = initCheck(); |
| 623 | if (ret != NO_ERROR) { |
| 624 | return false; |
| 625 | } |
| 626 | |
Dima Zavin | 24fc2fb | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 627 | bool state = AUDIO_MODE_INVALID; |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 628 | mHardwareStatus = AUDIO_HW_GET_MIC_MUTE; |
Dima Zavin | 31f18889 | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 629 | mPrimaryHardwareDev->get_mic_mute(mPrimaryHardwareDev, &state); |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 630 | mHardwareStatus = AUDIO_HW_IDLE; |
| 631 | return state; |
| 632 | } |
| 633 | |
| 634 | status_t AudioFlinger::setMasterMute(bool muted) |
| 635 | { |
| 636 | // check calling permissions |
| 637 | if (!settingsAllowed()) { |
| 638 | return PERMISSION_DENIED; |
| 639 | } |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 640 | |
Eric Laurent | 0163594 | 2011-01-18 18:39:02 -0800 | [diff] [blame] | 641 | Mutex::Autolock _l(mLock); |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 642 | mMasterMute = muted; |
| 643 | for (uint32_t i = 0; i < mPlaybackThreads.size(); i++) |
Eric Laurent | ddb78e7 | 2009-07-28 08:44:33 -0700 | [diff] [blame] | 644 | mPlaybackThreads.valueAt(i)->setMasterMute(muted); |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 645 | |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 646 | return NO_ERROR; |
| 647 | } |
| 648 | |
| 649 | float AudioFlinger::masterVolume() const |
| 650 | { |
Glenn Kasten | e6f8a42 | 2011-12-13 11:47:54 -0800 | [diff] [blame] | 651 | Mutex::Autolock _l(mLock); |
| 652 | return masterVolume_l(); |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 653 | } |
| 654 | |
| 655 | bool AudioFlinger::masterMute() const |
| 656 | { |
Glenn Kasten | e6f8a42 | 2011-12-13 11:47:54 -0800 | [diff] [blame] | 657 | Mutex::Autolock _l(mLock); |
| 658 | return masterMute_l(); |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 659 | } |
| 660 | |
Glenn Kasten | bc1d77b | 2012-01-12 16:38:12 -0800 | [diff] [blame] | 661 | status_t AudioFlinger::setStreamVolume(audio_stream_type_t stream, float value, int output) |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 662 | { |
| 663 | // check calling permissions |
| 664 | if (!settingsAllowed()) { |
| 665 | return PERMISSION_DENIED; |
| 666 | } |
| 667 | |
Glenn Kasten | 6e987a4 | 2012-01-06 08:40:01 -0800 | [diff] [blame] | 668 | if (uint32_t(stream) >= AUDIO_STREAM_CNT) { |
Steve Block | 3762c31 | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 669 | ALOGE("setStreamVolume() invalid stream %d", stream); |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 670 | return BAD_VALUE; |
| 671 | } |
The Android Open Source Project | f013e1a | 2008-12-17 18:05:43 -0800 | [diff] [blame] | 672 | |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 673 | AutoMutex lock(mLock); |
| 674 | PlaybackThread *thread = NULL; |
| 675 | if (output) { |
| 676 | thread = checkPlaybackThread_l(output); |
| 677 | if (thread == NULL) { |
| 678 | return BAD_VALUE; |
| 679 | } |
| 680 | } |
| 681 | |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 682 | mStreamTypes[stream].volume = value; |
| 683 | |
| 684 | if (thread == NULL) { |
Eric Laurent | 415f3e2 | 2009-10-21 08:14:22 -0700 | [diff] [blame] | 685 | for (uint32_t i = 0; i < mPlaybackThreads.size(); i++) { |
Eric Laurent | ddb78e7 | 2009-07-28 08:44:33 -0700 | [diff] [blame] | 686 | mPlaybackThreads.valueAt(i)->setStreamVolume(stream, value); |
Eric Laurent | 415f3e2 | 2009-10-21 08:14:22 -0700 | [diff] [blame] | 687 | } |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 688 | } else { |
| 689 | thread->setStreamVolume(stream, value); |
| 690 | } |
Eric Laurent | ef02827 | 2009-04-21 07:56:33 -0700 | [diff] [blame] | 691 | |
Eric Laurent | 415f3e2 | 2009-10-21 08:14:22 -0700 | [diff] [blame] | 692 | return NO_ERROR; |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 693 | } |
| 694 | |
Glenn Kasten | bc1d77b | 2012-01-12 16:38:12 -0800 | [diff] [blame] | 695 | status_t AudioFlinger::setStreamMute(audio_stream_type_t stream, bool muted) |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 696 | { |
| 697 | // check calling permissions |
| 698 | if (!settingsAllowed()) { |
| 699 | return PERMISSION_DENIED; |
| 700 | } |
| 701 | |
Glenn Kasten | 6e987a4 | 2012-01-06 08:40:01 -0800 | [diff] [blame] | 702 | if (uint32_t(stream) >= AUDIO_STREAM_CNT || |
Dima Zavin | 24fc2fb | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 703 | uint32_t(stream) == AUDIO_STREAM_ENFORCED_AUDIBLE) { |
Steve Block | 3762c31 | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 704 | ALOGE("setStreamMute() invalid stream %d", stream); |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 705 | return BAD_VALUE; |
| 706 | } |
The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 707 | |
Eric Laurent | 0163594 | 2011-01-18 18:39:02 -0800 | [diff] [blame] | 708 | AutoMutex lock(mLock); |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 709 | mStreamTypes[stream].mute = muted; |
| 710 | for (uint32_t i = 0; i < mPlaybackThreads.size(); i++) |
Eric Laurent | ddb78e7 | 2009-07-28 08:44:33 -0700 | [diff] [blame] | 711 | mPlaybackThreads.valueAt(i)->setStreamMute(stream, muted); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 712 | |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 713 | return NO_ERROR; |
| 714 | } |
| 715 | |
Glenn Kasten | bc1d77b | 2012-01-12 16:38:12 -0800 | [diff] [blame] | 716 | float AudioFlinger::streamVolume(audio_stream_type_t stream, int output) const |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 717 | { |
Glenn Kasten | 6e987a4 | 2012-01-06 08:40:01 -0800 | [diff] [blame] | 718 | if (uint32_t(stream) >= AUDIO_STREAM_CNT) { |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 719 | return 0.0f; |
| 720 | } |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 721 | |
| 722 | AutoMutex lock(mLock); |
| 723 | float volume; |
| 724 | if (output) { |
| 725 | PlaybackThread *thread = checkPlaybackThread_l(output); |
| 726 | if (thread == NULL) { |
| 727 | return 0.0f; |
| 728 | } |
| 729 | volume = thread->streamVolume(stream); |
| 730 | } else { |
| 731 | volume = mStreamTypes[stream].volume; |
| 732 | } |
| 733 | |
Eric Laurent | ef02827 | 2009-04-21 07:56:33 -0700 | [diff] [blame] | 734 | return volume; |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 735 | } |
| 736 | |
Glenn Kasten | bc1d77b | 2012-01-12 16:38:12 -0800 | [diff] [blame] | 737 | bool AudioFlinger::streamMute(audio_stream_type_t stream) const |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 738 | { |
Glenn Kasten | 6e987a4 | 2012-01-06 08:40:01 -0800 | [diff] [blame] | 739 | if (uint32_t(stream) >= AUDIO_STREAM_CNT) { |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 740 | return true; |
| 741 | } |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 742 | |
| 743 | return mStreamTypes[stream].mute; |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 744 | } |
| 745 | |
Eric Laurent | ddb78e7 | 2009-07-28 08:44:33 -0700 | [diff] [blame] | 746 | status_t AudioFlinger::setParameters(int ioHandle, const String8& keyValuePairs) |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 747 | { |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 748 | status_t result; |
| 749 | |
Steve Block | 71f2cf1 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 750 | ALOGV("setParameters(): io %d, keyvalue %s, tid %d, calling tid %d", |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 751 | ioHandle, keyValuePairs.string(), gettid(), IPCThreadState::self()->getCallingPid()); |
| 752 | // check calling permissions |
| 753 | if (!settingsAllowed()) { |
| 754 | return PERMISSION_DENIED; |
The Android Open Source Project | 9266c55 | 2009-01-15 16:12:10 -0800 | [diff] [blame] | 755 | } |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 756 | |
| 757 | // ioHandle == 0 means the parameters are global to the audio hardware interface |
| 758 | if (ioHandle == 0) { |
| 759 | AutoMutex lock(mHardwareLock); |
| 760 | mHardwareStatus = AUDIO_SET_PARAMETER; |
Dima Zavin | 31f18889 | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 761 | status_t final_result = NO_ERROR; |
| 762 | for (size_t i = 0; i < mAudioHwDevs.size(); i++) { |
| 763 | audio_hw_device_t *dev = mAudioHwDevs[i]; |
| 764 | result = dev->set_parameters(dev, keyValuePairs.string()); |
| 765 | final_result = result ?: final_result; |
| 766 | } |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 767 | mHardwareStatus = AUDIO_HW_IDLE; |
Eric Laurent | 6639b55 | 2011-08-01 09:52:20 -0700 | [diff] [blame] | 768 | // disable AEC and NS if the device is a BT SCO headset supporting those pre processings |
| 769 | AudioParameter param = AudioParameter(keyValuePairs); |
| 770 | String8 value; |
| 771 | if (param.get(String8(AUDIO_PARAMETER_KEY_BT_NREC), value) == NO_ERROR) { |
| 772 | Mutex::Autolock _l(mLock); |
Eric Laurent | 2d95dfb | 2011-08-29 12:42:48 -0700 | [diff] [blame] | 773 | bool btNrecIsOff = (value == AUDIO_PARAMETER_VALUE_OFF); |
| 774 | if (mBtNrecIsOff != btNrecIsOff) { |
Eric Laurent | 6639b55 | 2011-08-01 09:52:20 -0700 | [diff] [blame] | 775 | for (size_t i = 0; i < mRecordThreads.size(); i++) { |
| 776 | sp<RecordThread> thread = mRecordThreads.valueAt(i); |
| 777 | RecordThread::RecordTrack *track = thread->track(); |
| 778 | if (track != NULL) { |
| 779 | audio_devices_t device = (audio_devices_t)( |
| 780 | thread->device() & AUDIO_DEVICE_IN_ALL); |
Eric Laurent | 2d95dfb | 2011-08-29 12:42:48 -0700 | [diff] [blame] | 781 | bool suspend = audio_is_bluetooth_sco_device(device) && btNrecIsOff; |
Eric Laurent | 6639b55 | 2011-08-01 09:52:20 -0700 | [diff] [blame] | 782 | thread->setEffectSuspended(FX_IID_AEC, |
| 783 | suspend, |
| 784 | track->sessionId()); |
| 785 | thread->setEffectSuspended(FX_IID_NS, |
| 786 | suspend, |
| 787 | track->sessionId()); |
| 788 | } |
| 789 | } |
Eric Laurent | 2d95dfb | 2011-08-29 12:42:48 -0700 | [diff] [blame] | 790 | mBtNrecIsOff = btNrecIsOff; |
Eric Laurent | 6639b55 | 2011-08-01 09:52:20 -0700 | [diff] [blame] | 791 | } |
| 792 | } |
Dima Zavin | 31f18889 | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 793 | return final_result; |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 794 | } |
| 795 | |
Eric Laurent | b7d9460 | 2009-09-29 11:12:57 -0700 | [diff] [blame] | 796 | // hold a strong ref on thread in case closeOutput() or closeInput() is called |
| 797 | // and the thread is exited once the lock is released |
| 798 | sp<ThreadBase> thread; |
| 799 | { |
| 800 | Mutex::Autolock _l(mLock); |
| 801 | thread = checkPlaybackThread_l(ioHandle); |
| 802 | if (thread == NULL) { |
| 803 | thread = checkRecordThread_l(ioHandle); |
Glenn Kasten | 70ed6b7 | 2012-01-10 10:46:34 -0800 | [diff] [blame] | 804 | } else if (thread == primaryPlaybackThread_l()) { |
Eric Laurent | 464d5b3 | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 805 | // indicate output device change to all input threads for pre processing |
| 806 | AudioParameter param = AudioParameter(keyValuePairs); |
| 807 | int value; |
| 808 | if (param.getInt(String8(AudioParameter::keyRouting), value) == NO_ERROR) { |
| 809 | for (size_t i = 0; i < mRecordThreads.size(); i++) { |
| 810 | mRecordThreads.valueAt(i)->setParameters(keyValuePairs); |
| 811 | } |
| 812 | } |
Eric Laurent | b7d9460 | 2009-09-29 11:12:57 -0700 | [diff] [blame] | 813 | } |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 814 | } |
Eric Laurent | b7d9460 | 2009-09-29 11:12:57 -0700 | [diff] [blame] | 815 | if (thread != NULL) { |
Glenn Kasten | 871c16c | 2010-03-05 12:18:01 -0800 | [diff] [blame] | 816 | result = thread->setParameters(keyValuePairs); |
Glenn Kasten | 871c16c | 2010-03-05 12:18:01 -0800 | [diff] [blame] | 817 | return result; |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 818 | } |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 819 | return BAD_VALUE; |
| 820 | } |
| 821 | |
Eric Laurent | ddb78e7 | 2009-07-28 08:44:33 -0700 | [diff] [blame] | 822 | String8 AudioFlinger::getParameters(int ioHandle, const String8& keys) |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 823 | { |
Steve Block | 71f2cf1 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 824 | // ALOGV("getParameters() io %d, keys %s, tid %d, calling tid %d", |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 825 | // ioHandle, keys.string(), gettid(), IPCThreadState::self()->getCallingPid()); |
| 826 | |
| 827 | if (ioHandle == 0) { |
Dima Zavin | 24fc2fb | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 828 | String8 out_s8; |
| 829 | |
Dima Zavin | 31f18889 | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 830 | for (size_t i = 0; i < mAudioHwDevs.size(); i++) { |
| 831 | audio_hw_device_t *dev = mAudioHwDevs[i]; |
| 832 | char *s = dev->get_parameters(dev, keys.string()); |
| 833 | out_s8 += String8(s); |
| 834 | free(s); |
| 835 | } |
Dima Zavin | 24fc2fb | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 836 | return out_s8; |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 837 | } |
Eric Laurent | b7d9460 | 2009-09-29 11:12:57 -0700 | [diff] [blame] | 838 | |
| 839 | Mutex::Autolock _l(mLock); |
| 840 | |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 841 | PlaybackThread *playbackThread = checkPlaybackThread_l(ioHandle); |
| 842 | if (playbackThread != NULL) { |
| 843 | return playbackThread->getParameters(keys); |
| 844 | } |
| 845 | RecordThread *recordThread = checkRecordThread_l(ioHandle); |
| 846 | if (recordThread != NULL) { |
| 847 | return recordThread->getParameters(keys); |
| 848 | } |
| 849 | return String8(""); |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 850 | } |
| 851 | |
Glenn Kasten | 0a204ed | 2012-01-12 12:27:51 -0800 | [diff] [blame^] | 852 | size_t AudioFlinger::getInputBufferSize(uint32_t sampleRate, audio_format_t format, int channelCount) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 853 | { |
Eric Laurent | 8b4dbf7 | 2011-08-23 08:25:03 -0700 | [diff] [blame] | 854 | status_t ret = initCheck(); |
| 855 | if (ret != NO_ERROR) { |
| 856 | return 0; |
| 857 | } |
| 858 | |
Dima Zavin | 31f18889 | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 859 | return mPrimaryHardwareDev->get_input_buffer_size(mPrimaryHardwareDev, sampleRate, format, channelCount); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 860 | } |
| 861 | |
Eric Laurent | 47d0a92 | 2010-02-26 02:47:27 -0800 | [diff] [blame] | 862 | unsigned int AudioFlinger::getInputFramesLost(int ioHandle) |
| 863 | { |
| 864 | if (ioHandle == 0) { |
| 865 | return 0; |
| 866 | } |
| 867 | |
| 868 | Mutex::Autolock _l(mLock); |
| 869 | |
| 870 | RecordThread *recordThread = checkRecordThread_l(ioHandle); |
| 871 | if (recordThread != NULL) { |
| 872 | return recordThread->getInputFramesLost(); |
| 873 | } |
| 874 | return 0; |
| 875 | } |
| 876 | |
Eric Laurent | 415f3e2 | 2009-10-21 08:14:22 -0700 | [diff] [blame] | 877 | status_t AudioFlinger::setVoiceVolume(float value) |
| 878 | { |
Eric Laurent | 8b4dbf7 | 2011-08-23 08:25:03 -0700 | [diff] [blame] | 879 | status_t ret = initCheck(); |
| 880 | if (ret != NO_ERROR) { |
| 881 | return ret; |
| 882 | } |
| 883 | |
Eric Laurent | 415f3e2 | 2009-10-21 08:14:22 -0700 | [diff] [blame] | 884 | // check calling permissions |
| 885 | if (!settingsAllowed()) { |
| 886 | return PERMISSION_DENIED; |
| 887 | } |
| 888 | |
| 889 | AutoMutex lock(mHardwareLock); |
| 890 | mHardwareStatus = AUDIO_SET_VOICE_VOLUME; |
Eric Laurent | 8b4dbf7 | 2011-08-23 08:25:03 -0700 | [diff] [blame] | 891 | ret = mPrimaryHardwareDev->set_voice_volume(mPrimaryHardwareDev, value); |
Eric Laurent | 415f3e2 | 2009-10-21 08:14:22 -0700 | [diff] [blame] | 892 | mHardwareStatus = AUDIO_HW_IDLE; |
| 893 | |
| 894 | return ret; |
| 895 | } |
| 896 | |
Eric Laurent | 0986e79 | 2010-01-19 17:37:09 -0800 | [diff] [blame] | 897 | status_t AudioFlinger::getRenderPosition(uint32_t *halFrames, uint32_t *dspFrames, int output) |
| 898 | { |
| 899 | status_t status; |
| 900 | |
| 901 | Mutex::Autolock _l(mLock); |
| 902 | |
| 903 | PlaybackThread *playbackThread = checkPlaybackThread_l(output); |
| 904 | if (playbackThread != NULL) { |
| 905 | return playbackThread->getRenderPosition(halFrames, dspFrames); |
| 906 | } |
| 907 | |
| 908 | return BAD_VALUE; |
| 909 | } |
| 910 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 911 | void AudioFlinger::registerClient(const sp<IAudioFlingerClient>& client) |
| 912 | { |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 913 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 914 | Mutex::Autolock _l(mLock); |
| 915 | |
Eric Laurent | 4f0f17d | 2010-05-12 02:05:53 -0700 | [diff] [blame] | 916 | int pid = IPCThreadState::self()->getCallingPid(); |
| 917 | if (mNotificationClients.indexOfKey(pid) < 0) { |
| 918 | sp<NotificationClient> notificationClient = new NotificationClient(this, |
| 919 | client, |
| 920 | pid); |
Steve Block | 71f2cf1 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 921 | ALOGV("registerClient() client %p, pid %d", notificationClient.get(), pid); |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 922 | |
Eric Laurent | 4f0f17d | 2010-05-12 02:05:53 -0700 | [diff] [blame] | 923 | mNotificationClients.add(pid, notificationClient); |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 924 | |
Eric Laurent | 4f0f17d | 2010-05-12 02:05:53 -0700 | [diff] [blame] | 925 | sp<IBinder> binder = client->asBinder(); |
| 926 | binder->linkToDeath(notificationClient); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 927 | |
Eric Laurent | 4f0f17d | 2010-05-12 02:05:53 -0700 | [diff] [blame] | 928 | // the config change is always sent from playback or record threads to avoid deadlock |
| 929 | // with AudioSystem::gLock |
| 930 | for (size_t i = 0; i < mPlaybackThreads.size(); i++) { |
| 931 | mPlaybackThreads.valueAt(i)->sendConfigEvent(AudioSystem::OUTPUT_OPENED); |
| 932 | } |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 933 | |
Eric Laurent | 4f0f17d | 2010-05-12 02:05:53 -0700 | [diff] [blame] | 934 | for (size_t i = 0; i < mRecordThreads.size(); i++) { |
| 935 | mRecordThreads.valueAt(i)->sendConfigEvent(AudioSystem::INPUT_OPENED); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 936 | } |
| 937 | } |
| 938 | } |
| 939 | |
Eric Laurent | 4f0f17d | 2010-05-12 02:05:53 -0700 | [diff] [blame] | 940 | void AudioFlinger::removeNotificationClient(pid_t pid) |
| 941 | { |
| 942 | Mutex::Autolock _l(mLock); |
| 943 | |
| 944 | int index = mNotificationClients.indexOfKey(pid); |
| 945 | if (index >= 0) { |
| 946 | sp <NotificationClient> client = mNotificationClients.valueFor(pid); |
Steve Block | 71f2cf1 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 947 | ALOGV("removeNotificationClient() %p, pid %d", client.get(), pid); |
Eric Laurent | 4f0f17d | 2010-05-12 02:05:53 -0700 | [diff] [blame] | 948 | mNotificationClients.removeItem(pid); |
| 949 | } |
Marco Nelissen | c74b93f | 2011-08-02 13:33:41 -0700 | [diff] [blame] | 950 | |
Steve Block | 71f2cf1 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 951 | ALOGV("%d died, releasing its sessions", pid); |
Marco Nelissen | c74b93f | 2011-08-02 13:33:41 -0700 | [diff] [blame] | 952 | int num = mAudioSessionRefs.size(); |
| 953 | bool removed = false; |
| 954 | for (int i = 0; i< num; i++) { |
| 955 | AudioSessionRef *ref = mAudioSessionRefs.itemAt(i); |
Steve Block | 71f2cf1 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 956 | ALOGV(" pid %d @ %d", ref->pid, i); |
Marco Nelissen | c74b93f | 2011-08-02 13:33:41 -0700 | [diff] [blame] | 957 | if (ref->pid == pid) { |
Steve Block | 71f2cf1 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 958 | ALOGV(" removing entry for pid %d session %d", pid, ref->sessionid); |
Marco Nelissen | c74b93f | 2011-08-02 13:33:41 -0700 | [diff] [blame] | 959 | mAudioSessionRefs.removeAt(i); |
| 960 | delete ref; |
| 961 | removed = true; |
| 962 | i--; |
| 963 | num--; |
| 964 | } |
| 965 | } |
| 966 | if (removed) { |
| 967 | purgeStaleEffects_l(); |
| 968 | } |
Eric Laurent | 4f0f17d | 2010-05-12 02:05:53 -0700 | [diff] [blame] | 969 | } |
| 970 | |
Eric Laurent | 296a0ec | 2009-09-15 07:10:12 -0700 | [diff] [blame] | 971 | // audioConfigChanged_l() must be called with AudioFlinger::mLock held |
Eric Laurent | 4f0f17d | 2010-05-12 02:05:53 -0700 | [diff] [blame] | 972 | void AudioFlinger::audioConfigChanged_l(int event, int ioHandle, void *param2) |
| 973 | { |
Eric Laurent | 49f02be | 2009-11-19 09:00:56 -0800 | [diff] [blame] | 974 | size_t size = mNotificationClients.size(); |
| 975 | for (size_t i = 0; i < size; i++) { |
Eric Laurent | 4f0f17d | 2010-05-12 02:05:53 -0700 | [diff] [blame] | 976 | mNotificationClients.valueAt(i)->client()->ioConfigChanged(event, ioHandle, param2); |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 977 | } |
| 978 | } |
| 979 | |
Eric Laurent | b9481d8 | 2009-09-17 05:12:56 -0700 | [diff] [blame] | 980 | // removeClient_l() must be called with AudioFlinger::mLock held |
| 981 | void AudioFlinger::removeClient_l(pid_t pid) |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 982 | { |
Steve Block | 71f2cf1 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 983 | ALOGV("removeClient_l() pid %d, tid %d, calling tid %d", pid, gettid(), IPCThreadState::self()->getCallingPid()); |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 984 | mClients.removeItem(pid); |
| 985 | } |
| 986 | |
Eric Laurent | 4f0f17d | 2010-05-12 02:05:53 -0700 | [diff] [blame] | 987 | |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 988 | // ---------------------------------------------------------------------------- |
| 989 | |
Eric Laurent | 464d5b3 | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 990 | AudioFlinger::ThreadBase::ThreadBase(const sp<AudioFlinger>& audioFlinger, int id, uint32_t device) |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 991 | : Thread(false), |
| 992 | mAudioFlinger(audioFlinger), mSampleRate(0), mFrameCount(0), mChannelCount(0), |
Glenn Kasten | 0a204ed | 2012-01-12 12:27:51 -0800 | [diff] [blame^] | 993 | mFrameSize(1), mFormat(AUDIO_FORMAT_INVALID), mStandby(false), mId(id), mExiting(false), |
Eric Laurent | 6dbdc40 | 2011-07-22 09:04:31 -0700 | [diff] [blame] | 994 | mDevice(device) |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 995 | { |
Eric Laurent | 6dbdc40 | 2011-07-22 09:04:31 -0700 | [diff] [blame] | 996 | mDeathRecipient = new PMDeathRecipient(this); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 997 | } |
| 998 | |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 999 | AudioFlinger::ThreadBase::~ThreadBase() |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1000 | { |
Eric Laurent | 8fce46a | 2009-08-04 09:45:33 -0700 | [diff] [blame] | 1001 | mParamCond.broadcast(); |
Eric Laurent | 6dbdc40 | 2011-07-22 09:04:31 -0700 | [diff] [blame] | 1002 | // do not lock the mutex in destructor |
| 1003 | releaseWakeLock_l(); |
Eric Laurent | 4a64a6e | 2011-09-27 12:07:15 -0700 | [diff] [blame] | 1004 | if (mPowerManager != 0) { |
| 1005 | sp<IBinder> binder = mPowerManager->asBinder(); |
| 1006 | binder->unlinkToDeath(mDeathRecipient); |
| 1007 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1008 | } |
| 1009 | |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 1010 | void AudioFlinger::ThreadBase::exit() |
The Android Open Source Project | c39a6e0 | 2009-03-11 12:11:56 -0700 | [diff] [blame] | 1011 | { |
Glenn Kasten | e5fb263 | 2011-12-14 10:28:06 -0800 | [diff] [blame] | 1012 | // keep a strong ref on ourself so that we won't get |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 1013 | // destroyed in the middle of requestExitAndWait() |
| 1014 | sp <ThreadBase> strongMe = this; |
| 1015 | |
Steve Block | 71f2cf1 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 1016 | ALOGV("ThreadBase::exit"); |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 1017 | { |
Glenn Kasten | 325ee1c | 2012-01-05 15:41:56 -0800 | [diff] [blame] | 1018 | AutoMutex lock(mLock); |
Eric Laurent | 49f02be | 2009-11-19 09:00:56 -0800 | [diff] [blame] | 1019 | mExiting = true; |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 1020 | requestExit(); |
| 1021 | mWaitWorkCV.signal(); |
The Android Open Source Project | c39a6e0 | 2009-03-11 12:11:56 -0700 | [diff] [blame] | 1022 | } |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 1023 | requestExitAndWait(); |
The Android Open Source Project | c39a6e0 | 2009-03-11 12:11:56 -0700 | [diff] [blame] | 1024 | } |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 1025 | |
| 1026 | uint32_t AudioFlinger::ThreadBase::sampleRate() const |
| 1027 | { |
| 1028 | return mSampleRate; |
| 1029 | } |
| 1030 | |
| 1031 | int AudioFlinger::ThreadBase::channelCount() const |
| 1032 | { |
Eric Laurent | b0a0147 | 2010-05-14 05:45:46 -0700 | [diff] [blame] | 1033 | return (int)mChannelCount; |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 1034 | } |
| 1035 | |
Glenn Kasten | 0a204ed | 2012-01-12 12:27:51 -0800 | [diff] [blame^] | 1036 | audio_format_t AudioFlinger::ThreadBase::format() const |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 1037 | { |
| 1038 | return mFormat; |
| 1039 | } |
| 1040 | |
| 1041 | size_t AudioFlinger::ThreadBase::frameCount() const |
| 1042 | { |
| 1043 | return mFrameCount; |
| 1044 | } |
| 1045 | |
| 1046 | status_t AudioFlinger::ThreadBase::setParameters(const String8& keyValuePairs) |
| 1047 | { |
Eric Laurent | 8fce46a | 2009-08-04 09:45:33 -0700 | [diff] [blame] | 1048 | status_t status; |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 1049 | |
Steve Block | 71f2cf1 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 1050 | ALOGV("ThreadBase::setParameters() %s", keyValuePairs.string()); |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 1051 | Mutex::Autolock _l(mLock); |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 1052 | |
Eric Laurent | 8fce46a | 2009-08-04 09:45:33 -0700 | [diff] [blame] | 1053 | mNewParameters.add(keyValuePairs); |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 1054 | mWaitWorkCV.signal(); |
Eric Laurent | b7d9460 | 2009-09-29 11:12:57 -0700 | [diff] [blame] | 1055 | // wait condition with timeout in case the thread loop has exited |
| 1056 | // before the request could be processed |
Glenn Kasten | cbfe2e1 | 2011-12-13 11:04:14 -0800 | [diff] [blame] | 1057 | if (mParamCond.waitRelative(mLock, kSetParametersTimeoutNs) == NO_ERROR) { |
Eric Laurent | b7d9460 | 2009-09-29 11:12:57 -0700 | [diff] [blame] | 1058 | status = mParamStatus; |
| 1059 | mWaitWorkCV.signal(); |
| 1060 | } else { |
| 1061 | status = TIMED_OUT; |
| 1062 | } |
Eric Laurent | 8fce46a | 2009-08-04 09:45:33 -0700 | [diff] [blame] | 1063 | return status; |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 1064 | } |
| 1065 | |
| 1066 | void AudioFlinger::ThreadBase::sendConfigEvent(int event, int param) |
| 1067 | { |
| 1068 | Mutex::Autolock _l(mLock); |
Eric Laurent | 8fce46a | 2009-08-04 09:45:33 -0700 | [diff] [blame] | 1069 | sendConfigEvent_l(event, param); |
| 1070 | } |
| 1071 | |
| 1072 | // sendConfigEvent_l() must be called with ThreadBase::mLock held |
| 1073 | void AudioFlinger::ThreadBase::sendConfigEvent_l(int event, int param) |
| 1074 | { |
Glenn Kasten | 4b220f0 | 2011-12-13 11:50:00 -0800 | [diff] [blame] | 1075 | ConfigEvent configEvent; |
| 1076 | configEvent.mEvent = event; |
| 1077 | configEvent.mParam = param; |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 1078 | mConfigEvents.add(configEvent); |
Steve Block | 71f2cf1 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 1079 | ALOGV("sendConfigEvent() num events %d event %d, param %d", mConfigEvents.size(), event, param); |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 1080 | mWaitWorkCV.signal(); |
| 1081 | } |
| 1082 | |
| 1083 | void AudioFlinger::ThreadBase::processConfigEvents() |
| 1084 | { |
| 1085 | mLock.lock(); |
| 1086 | while(!mConfigEvents.isEmpty()) { |
Steve Block | 71f2cf1 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 1087 | ALOGV("processConfigEvents() remaining events %d", mConfigEvents.size()); |
Glenn Kasten | 4b220f0 | 2011-12-13 11:50:00 -0800 | [diff] [blame] | 1088 | ConfigEvent configEvent = mConfigEvents[0]; |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 1089 | mConfigEvents.removeAt(0); |
Eric Laurent | eb8f850d | 2010-05-14 03:26:45 -0700 | [diff] [blame] | 1090 | // release mLock before locking AudioFlinger mLock: lock order is always |
| 1091 | // AudioFlinger then ThreadBase to avoid cross deadlock |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 1092 | mLock.unlock(); |
Eric Laurent | eb8f850d | 2010-05-14 03:26:45 -0700 | [diff] [blame] | 1093 | mAudioFlinger->mLock.lock(); |
Glenn Kasten | 4b220f0 | 2011-12-13 11:50:00 -0800 | [diff] [blame] | 1094 | audioConfigChanged_l(configEvent.mEvent, configEvent.mParam); |
Eric Laurent | eb8f850d | 2010-05-14 03:26:45 -0700 | [diff] [blame] | 1095 | mAudioFlinger->mLock.unlock(); |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 1096 | mLock.lock(); |
| 1097 | } |
| 1098 | mLock.unlock(); |
| 1099 | } |
| 1100 | |
Eric Laurent | 3fdb126 | 2009-11-07 00:01:32 -0800 | [diff] [blame] | 1101 | status_t AudioFlinger::ThreadBase::dumpBase(int fd, const Vector<String16>& args) |
| 1102 | { |
| 1103 | const size_t SIZE = 256; |
| 1104 | char buffer[SIZE]; |
| 1105 | String8 result; |
| 1106 | |
| 1107 | bool locked = tryLock(mLock); |
| 1108 | if (!locked) { |
| 1109 | snprintf(buffer, SIZE, "thread %p maybe dead locked\n", this); |
| 1110 | write(fd, buffer, strlen(buffer)); |
| 1111 | } |
| 1112 | |
| 1113 | snprintf(buffer, SIZE, "standby: %d\n", mStandby); |
| 1114 | result.append(buffer); |
| 1115 | snprintf(buffer, SIZE, "Sample rate: %d\n", mSampleRate); |
| 1116 | result.append(buffer); |
| 1117 | snprintf(buffer, SIZE, "Frame count: %d\n", mFrameCount); |
| 1118 | result.append(buffer); |
| 1119 | snprintf(buffer, SIZE, "Channel Count: %d\n", mChannelCount); |
| 1120 | result.append(buffer); |
Jean-Michel Trivi | 5439223 | 2011-05-24 15:53:33 -0700 | [diff] [blame] | 1121 | snprintf(buffer, SIZE, "Channel Mask: 0x%08x\n", mChannelMask); |
| 1122 | result.append(buffer); |
Eric Laurent | 3fdb126 | 2009-11-07 00:01:32 -0800 | [diff] [blame] | 1123 | snprintf(buffer, SIZE, "Format: %d\n", mFormat); |
| 1124 | result.append(buffer); |
Glenn Kasten | faf354d | 2012-01-11 09:48:27 -0800 | [diff] [blame] | 1125 | snprintf(buffer, SIZE, "Frame size: %u\n", mFrameSize); |
Eric Laurent | 3fdb126 | 2009-11-07 00:01:32 -0800 | [diff] [blame] | 1126 | result.append(buffer); |
| 1127 | |
| 1128 | snprintf(buffer, SIZE, "\nPending setParameters commands: \n"); |
| 1129 | result.append(buffer); |
| 1130 | result.append(" Index Command"); |
| 1131 | for (size_t i = 0; i < mNewParameters.size(); ++i) { |
| 1132 | snprintf(buffer, SIZE, "\n %02d ", i); |
| 1133 | result.append(buffer); |
| 1134 | result.append(mNewParameters[i]); |
| 1135 | } |
| 1136 | |
| 1137 | snprintf(buffer, SIZE, "\n\nPending config events: \n"); |
| 1138 | result.append(buffer); |
| 1139 | snprintf(buffer, SIZE, " Index event param\n"); |
| 1140 | result.append(buffer); |
| 1141 | for (size_t i = 0; i < mConfigEvents.size(); i++) { |
Glenn Kasten | 4b220f0 | 2011-12-13 11:50:00 -0800 | [diff] [blame] | 1142 | snprintf(buffer, SIZE, " %02d %02d %d\n", i, mConfigEvents[i].mEvent, mConfigEvents[i].mParam); |
Eric Laurent | 3fdb126 | 2009-11-07 00:01:32 -0800 | [diff] [blame] | 1143 | result.append(buffer); |
| 1144 | } |
| 1145 | result.append("\n"); |
| 1146 | |
| 1147 | write(fd, result.string(), result.size()); |
| 1148 | |
| 1149 | if (locked) { |
| 1150 | mLock.unlock(); |
| 1151 | } |
| 1152 | return NO_ERROR; |
| 1153 | } |
| 1154 | |
Eric Laurent | 1345d33 | 2011-07-24 17:49:51 -0700 | [diff] [blame] | 1155 | status_t AudioFlinger::ThreadBase::dumpEffectChains(int fd, const Vector<String16>& args) |
| 1156 | { |
| 1157 | const size_t SIZE = 256; |
| 1158 | char buffer[SIZE]; |
| 1159 | String8 result; |
| 1160 | |
| 1161 | snprintf(buffer, SIZE, "\n- %d Effect Chains:\n", mEffectChains.size()); |
| 1162 | write(fd, buffer, strlen(buffer)); |
| 1163 | |
| 1164 | for (size_t i = 0; i < mEffectChains.size(); ++i) { |
| 1165 | sp<EffectChain> chain = mEffectChains[i]; |
| 1166 | if (chain != 0) { |
| 1167 | chain->dump(fd, args); |
| 1168 | } |
| 1169 | } |
| 1170 | return NO_ERROR; |
| 1171 | } |
| 1172 | |
Eric Laurent | 6dbdc40 | 2011-07-22 09:04:31 -0700 | [diff] [blame] | 1173 | void AudioFlinger::ThreadBase::acquireWakeLock() |
| 1174 | { |
| 1175 | Mutex::Autolock _l(mLock); |
| 1176 | acquireWakeLock_l(); |
| 1177 | } |
| 1178 | |
| 1179 | void AudioFlinger::ThreadBase::acquireWakeLock_l() |
| 1180 | { |
| 1181 | if (mPowerManager == 0) { |
| 1182 | // use checkService() to avoid blocking if power service is not up yet |
| 1183 | sp<IBinder> binder = |
| 1184 | defaultServiceManager()->checkService(String16("power")); |
| 1185 | if (binder == 0) { |
Steve Block | 8564c8d | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 1186 | ALOGW("Thread %s cannot connect to the power manager service", mName); |
Eric Laurent | 6dbdc40 | 2011-07-22 09:04:31 -0700 | [diff] [blame] | 1187 | } else { |
| 1188 | mPowerManager = interface_cast<IPowerManager>(binder); |
| 1189 | binder->linkToDeath(mDeathRecipient); |
| 1190 | } |
| 1191 | } |
| 1192 | if (mPowerManager != 0) { |
| 1193 | sp<IBinder> binder = new BBinder(); |
| 1194 | status_t status = mPowerManager->acquireWakeLock(POWERMANAGER_PARTIAL_WAKE_LOCK, |
| 1195 | binder, |
| 1196 | String16(mName)); |
| 1197 | if (status == NO_ERROR) { |
| 1198 | mWakeLockToken = binder; |
| 1199 | } |
Steve Block | 71f2cf1 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 1200 | ALOGV("acquireWakeLock_l() %s status %d", mName, status); |
Eric Laurent | 6dbdc40 | 2011-07-22 09:04:31 -0700 | [diff] [blame] | 1201 | } |
| 1202 | } |
| 1203 | |
| 1204 | void AudioFlinger::ThreadBase::releaseWakeLock() |
| 1205 | { |
| 1206 | Mutex::Autolock _l(mLock); |
Eric Laurent | d49ead6 | 2011-07-28 13:59:02 -0700 | [diff] [blame] | 1207 | releaseWakeLock_l(); |
Eric Laurent | 6dbdc40 | 2011-07-22 09:04:31 -0700 | [diff] [blame] | 1208 | } |
| 1209 | |
| 1210 | void AudioFlinger::ThreadBase::releaseWakeLock_l() |
| 1211 | { |
| 1212 | if (mWakeLockToken != 0) { |
Steve Block | 71f2cf1 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 1213 | ALOGV("releaseWakeLock_l() %s", mName); |
Eric Laurent | 6dbdc40 | 2011-07-22 09:04:31 -0700 | [diff] [blame] | 1214 | if (mPowerManager != 0) { |
| 1215 | mPowerManager->releaseWakeLock(mWakeLockToken, 0); |
| 1216 | } |
| 1217 | mWakeLockToken.clear(); |
| 1218 | } |
| 1219 | } |
| 1220 | |
| 1221 | void AudioFlinger::ThreadBase::clearPowerManager() |
| 1222 | { |
| 1223 | Mutex::Autolock _l(mLock); |
| 1224 | releaseWakeLock_l(); |
| 1225 | mPowerManager.clear(); |
| 1226 | } |
| 1227 | |
| 1228 | void AudioFlinger::ThreadBase::PMDeathRecipient::binderDied(const wp<IBinder>& who) |
| 1229 | { |
| 1230 | sp<ThreadBase> thread = mThread.promote(); |
| 1231 | if (thread != 0) { |
| 1232 | thread->clearPowerManager(); |
| 1233 | } |
Steve Block | 8564c8d | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 1234 | ALOGW("power manager service died !!!"); |
Eric Laurent | 6dbdc40 | 2011-07-22 09:04:31 -0700 | [diff] [blame] | 1235 | } |
Eric Laurent | 1345d33 | 2011-07-24 17:49:51 -0700 | [diff] [blame] | 1236 | |
Eric Laurent | f82fccd | 2011-07-27 19:49:51 -0700 | [diff] [blame] | 1237 | void AudioFlinger::ThreadBase::setEffectSuspended( |
| 1238 | const effect_uuid_t *type, bool suspend, int sessionId) |
| 1239 | { |
| 1240 | Mutex::Autolock _l(mLock); |
| 1241 | setEffectSuspended_l(type, suspend, sessionId); |
| 1242 | } |
| 1243 | |
| 1244 | void AudioFlinger::ThreadBase::setEffectSuspended_l( |
| 1245 | const effect_uuid_t *type, bool suspend, int sessionId) |
| 1246 | { |
| 1247 | sp<EffectChain> chain; |
| 1248 | chain = getEffectChain_l(sessionId); |
| 1249 | if (chain != 0) { |
| 1250 | if (type != NULL) { |
| 1251 | chain->setEffectSuspended_l(type, suspend); |
| 1252 | } else { |
| 1253 | chain->setEffectSuspendedAll_l(suspend); |
| 1254 | } |
| 1255 | } |
| 1256 | |
| 1257 | updateSuspendedSessions_l(type, suspend, sessionId); |
| 1258 | } |
| 1259 | |
| 1260 | void AudioFlinger::ThreadBase::checkSuspendOnAddEffectChain_l(const sp<EffectChain>& chain) |
| 1261 | { |
| 1262 | int index = mSuspendedSessions.indexOfKey(chain->sessionId()); |
| 1263 | if (index < 0) { |
| 1264 | return; |
| 1265 | } |
| 1266 | |
| 1267 | KeyedVector <int, sp<SuspendedSessionDesc> > sessionEffects = |
| 1268 | mSuspendedSessions.editValueAt(index); |
| 1269 | |
| 1270 | for (size_t i = 0; i < sessionEffects.size(); i++) { |
| 1271 | sp <SuspendedSessionDesc> desc = sessionEffects.valueAt(i); |
| 1272 | for (int j = 0; j < desc->mRefCount; j++) { |
| 1273 | if (sessionEffects.keyAt(i) == EffectChain::kKeyForSuspendAll) { |
| 1274 | chain->setEffectSuspendedAll_l(true); |
| 1275 | } else { |
Steve Block | 71f2cf1 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 1276 | ALOGV("checkSuspendOnAddEffectChain_l() suspending effects %08x", |
Eric Laurent | f82fccd | 2011-07-27 19:49:51 -0700 | [diff] [blame] | 1277 | desc->mType.timeLow); |
| 1278 | chain->setEffectSuspended_l(&desc->mType, true); |
| 1279 | } |
| 1280 | } |
| 1281 | } |
| 1282 | } |
| 1283 | |
Eric Laurent | f82fccd | 2011-07-27 19:49:51 -0700 | [diff] [blame] | 1284 | void AudioFlinger::ThreadBase::updateSuspendedSessions_l(const effect_uuid_t *type, |
| 1285 | bool suspend, |
| 1286 | int sessionId) |
| 1287 | { |
| 1288 | int index = mSuspendedSessions.indexOfKey(sessionId); |
| 1289 | |
| 1290 | KeyedVector <int, sp<SuspendedSessionDesc> > sessionEffects; |
| 1291 | |
| 1292 | if (suspend) { |
| 1293 | if (index >= 0) { |
| 1294 | sessionEffects = mSuspendedSessions.editValueAt(index); |
| 1295 | } else { |
| 1296 | mSuspendedSessions.add(sessionId, sessionEffects); |
| 1297 | } |
| 1298 | } else { |
| 1299 | if (index < 0) { |
| 1300 | return; |
| 1301 | } |
| 1302 | sessionEffects = mSuspendedSessions.editValueAt(index); |
| 1303 | } |
| 1304 | |
| 1305 | |
| 1306 | int key = EffectChain::kKeyForSuspendAll; |
| 1307 | if (type != NULL) { |
| 1308 | key = type->timeLow; |
| 1309 | } |
| 1310 | index = sessionEffects.indexOfKey(key); |
| 1311 | |
| 1312 | sp <SuspendedSessionDesc> desc; |
| 1313 | if (suspend) { |
| 1314 | if (index >= 0) { |
| 1315 | desc = sessionEffects.valueAt(index); |
| 1316 | } else { |
| 1317 | desc = new SuspendedSessionDesc(); |
| 1318 | if (type != NULL) { |
| 1319 | memcpy(&desc->mType, type, sizeof(effect_uuid_t)); |
| 1320 | } |
| 1321 | sessionEffects.add(key, desc); |
Steve Block | 71f2cf1 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 1322 | ALOGV("updateSuspendedSessions_l() suspend adding effect %08x", key); |
Eric Laurent | f82fccd | 2011-07-27 19:49:51 -0700 | [diff] [blame] | 1323 | } |
| 1324 | desc->mRefCount++; |
| 1325 | } else { |
| 1326 | if (index < 0) { |
| 1327 | return; |
| 1328 | } |
| 1329 | desc = sessionEffects.valueAt(index); |
| 1330 | if (--desc->mRefCount == 0) { |
Steve Block | 71f2cf1 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 1331 | ALOGV("updateSuspendedSessions_l() restore removing effect %08x", key); |
Eric Laurent | f82fccd | 2011-07-27 19:49:51 -0700 | [diff] [blame] | 1332 | sessionEffects.removeItemsAt(index); |
| 1333 | if (sessionEffects.isEmpty()) { |
Steve Block | 71f2cf1 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 1334 | ALOGV("updateSuspendedSessions_l() restore removing session %d", |
Eric Laurent | f82fccd | 2011-07-27 19:49:51 -0700 | [diff] [blame] | 1335 | sessionId); |
| 1336 | mSuspendedSessions.removeItem(sessionId); |
| 1337 | } |
| 1338 | } |
| 1339 | } |
| 1340 | if (!sessionEffects.isEmpty()) { |
| 1341 | mSuspendedSessions.replaceValueFor(sessionId, sessionEffects); |
| 1342 | } |
| 1343 | } |
| 1344 | |
| 1345 | void AudioFlinger::ThreadBase::checkSuspendOnEffectEnabled(const sp<EffectModule>& effect, |
| 1346 | bool enabled, |
| 1347 | int sessionId) |
| 1348 | { |
| 1349 | Mutex::Autolock _l(mLock); |
Eric Laurent | 7fa1cee | 2011-10-19 11:44:54 -0700 | [diff] [blame] | 1350 | checkSuspendOnEffectEnabled_l(effect, enabled, sessionId); |
| 1351 | } |
Eric Laurent | f82fccd | 2011-07-27 19:49:51 -0700 | [diff] [blame] | 1352 | |
Eric Laurent | 7fa1cee | 2011-10-19 11:44:54 -0700 | [diff] [blame] | 1353 | void AudioFlinger::ThreadBase::checkSuspendOnEffectEnabled_l(const sp<EffectModule>& effect, |
| 1354 | bool enabled, |
| 1355 | int sessionId) |
| 1356 | { |
Eric Laurent | 6752ec8 | 2011-08-10 10:37:50 -0700 | [diff] [blame] | 1357 | if (mType != RECORD) { |
| 1358 | // suspend all effects in AUDIO_SESSION_OUTPUT_MIX when enabling any effect on |
| 1359 | // another session. This gives the priority to well behaved effect control panels |
| 1360 | // and applications not using global effects. |
| 1361 | if (sessionId != AUDIO_SESSION_OUTPUT_MIX) { |
| 1362 | setEffectSuspended_l(NULL, enabled, AUDIO_SESSION_OUTPUT_MIX); |
| 1363 | } |
| 1364 | } |
Eric Laurent | f82fccd | 2011-07-27 19:49:51 -0700 | [diff] [blame] | 1365 | |
| 1366 | sp<EffectChain> chain = getEffectChain_l(sessionId); |
| 1367 | if (chain != 0) { |
| 1368 | chain->checkSuspendOnEffectEnabled(effect, enabled); |
| 1369 | } |
| 1370 | } |
| 1371 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1372 | // ---------------------------------------------------------------------------- |
| 1373 | |
Eric Laurent | 464d5b3 | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 1374 | AudioFlinger::PlaybackThread::PlaybackThread(const sp<AudioFlinger>& audioFlinger, |
| 1375 | AudioStreamOut* output, |
| 1376 | int id, |
| 1377 | uint32_t device) |
| 1378 | : ThreadBase(audioFlinger, id, device), |
Glenn Kasten | c434c90 | 2011-12-13 11:53:26 -0800 | [diff] [blame] | 1379 | mMixBuffer(NULL), mSuspended(0), mBytesWritten(0), mOutput(output), |
Eric Laurent | 464d5b3 | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 1380 | mLastWriteTime(0), mNumWrites(0), mNumDelayedWrites(0), mInWrite(false) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1381 | { |
Eric Laurent | 6dbdc40 | 2011-07-22 09:04:31 -0700 | [diff] [blame] | 1382 | snprintf(mName, kNameLength, "AudioOut_%d", id); |
| 1383 | |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 1384 | readOutputParameters(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1385 | |
Glenn Kasten | e6f8a42 | 2011-12-13 11:47:54 -0800 | [diff] [blame] | 1386 | // Assumes constructor is called by AudioFlinger with it's mLock held, |
| 1387 | // but it would be safer to explicitly pass these as parameters |
| 1388 | mMasterVolume = mAudioFlinger->masterVolume_l(); |
| 1389 | mMasterMute = mAudioFlinger->masterMute_l(); |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 1390 | |
Glenn Kasten | 6e987a4 | 2012-01-06 08:40:01 -0800 | [diff] [blame] | 1391 | // mStreamTypes[AUDIO_STREAM_CNT] is initialized by stream_type_t default constructor |
Glenn Kasten | bc1d77b | 2012-01-12 16:38:12 -0800 | [diff] [blame] | 1392 | // There is no AUDIO_STREAM_MIN, and ++ operator does not compile |
| 1393 | for (audio_stream_type_t stream = (audio_stream_type_t) 0; stream < AUDIO_STREAM_CNT; |
| 1394 | stream = (audio_stream_type_t) (stream + 1)) { |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 1395 | mStreamTypes[stream].volume = mAudioFlinger->streamVolumeInternal(stream); |
| 1396 | mStreamTypes[stream].mute = mAudioFlinger->streamMute(stream); |
Glenn Kasten | 6e987a4 | 2012-01-06 08:40:01 -0800 | [diff] [blame] | 1397 | // initialized by stream_type_t default constructor |
| 1398 | // mStreamTypes[stream].valid = true; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1399 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1400 | } |
| 1401 | |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 1402 | AudioFlinger::PlaybackThread::~PlaybackThread() |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1403 | { |
| 1404 | delete [] mMixBuffer; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1405 | } |
| 1406 | |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 1407 | status_t AudioFlinger::PlaybackThread::dump(int fd, const Vector<String16>& args) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1408 | { |
| 1409 | dumpInternals(fd, args); |
| 1410 | dumpTracks(fd, args); |
Eric Laurent | 65b6545 | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 1411 | dumpEffectChains(fd, args); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1412 | return NO_ERROR; |
| 1413 | } |
| 1414 | |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 1415 | status_t AudioFlinger::PlaybackThread::dumpTracks(int fd, const Vector<String16>& args) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1416 | { |
| 1417 | const size_t SIZE = 256; |
| 1418 | char buffer[SIZE]; |
| 1419 | String8 result; |
| 1420 | |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 1421 | snprintf(buffer, SIZE, "Output thread %p tracks\n", this); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1422 | result.append(buffer); |
Jean-Michel Trivi | 5439223 | 2011-05-24 15:53:33 -0700 | [diff] [blame] | 1423 | result.append(" Name Clien Typ Fmt Chn mask Session Buf S M F SRate LeftV RighV Serv User Main buf Aux Buf\n"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1424 | for (size_t i = 0; i < mTracks.size(); ++i) { |
Eric Laurent | d3b4d0c | 2009-03-31 14:34:35 -0700 | [diff] [blame] | 1425 | sp<Track> track = mTracks[i]; |
| 1426 | if (track != 0) { |
| 1427 | track->dump(buffer, SIZE); |
| 1428 | result.append(buffer); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1429 | } |
| 1430 | } |
| 1431 | |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 1432 | snprintf(buffer, SIZE, "Output thread %p active tracks\n", this); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1433 | result.append(buffer); |
Jean-Michel Trivi | 5439223 | 2011-05-24 15:53:33 -0700 | [diff] [blame] | 1434 | result.append(" Name Clien Typ Fmt Chn mask Session Buf S M F SRate LeftV RighV Serv User Main buf Aux Buf\n"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1435 | for (size_t i = 0; i < mActiveTracks.size(); ++i) { |
Eric Laurent | d3b4d0c | 2009-03-31 14:34:35 -0700 | [diff] [blame] | 1436 | wp<Track> wTrack = mActiveTracks[i]; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1437 | if (wTrack != 0) { |
| 1438 | sp<Track> track = wTrack.promote(); |
| 1439 | if (track != 0) { |
| 1440 | track->dump(buffer, SIZE); |
| 1441 | result.append(buffer); |
| 1442 | } |
| 1443 | } |
| 1444 | } |
| 1445 | write(fd, result.string(), result.size()); |
| 1446 | return NO_ERROR; |
| 1447 | } |
| 1448 | |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 1449 | status_t AudioFlinger::PlaybackThread::dumpInternals(int fd, const Vector<String16>& args) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1450 | { |
| 1451 | const size_t SIZE = 256; |
| 1452 | char buffer[SIZE]; |
| 1453 | String8 result; |
| 1454 | |
Eric Laurent | 3fdb126 | 2009-11-07 00:01:32 -0800 | [diff] [blame] | 1455 | snprintf(buffer, SIZE, "\nOutput thread %p internals\n", this); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1456 | result.append(buffer); |
| 1457 | snprintf(buffer, SIZE, "last write occurred (msecs): %llu\n", ns2ms(systemTime() - mLastWriteTime)); |
| 1458 | result.append(buffer); |
| 1459 | snprintf(buffer, SIZE, "total writes: %d\n", mNumWrites); |
| 1460 | result.append(buffer); |
| 1461 | snprintf(buffer, SIZE, "delayed writes: %d\n", mNumDelayedWrites); |
| 1462 | result.append(buffer); |
| 1463 | snprintf(buffer, SIZE, "blocked in write: %d\n", mInWrite); |
| 1464 | result.append(buffer); |
Eric Laurent | 8ac9f8d | 2009-12-18 05:47:48 -0800 | [diff] [blame] | 1465 | snprintf(buffer, SIZE, "suspend count: %d\n", mSuspended); |
| 1466 | result.append(buffer); |
Eric Laurent | 65b6545 | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 1467 | snprintf(buffer, SIZE, "mix buffer : %p\n", mMixBuffer); |
| 1468 | result.append(buffer); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1469 | write(fd, result.string(), result.size()); |
Eric Laurent | 3fdb126 | 2009-11-07 00:01:32 -0800 | [diff] [blame] | 1470 | |
| 1471 | dumpBase(fd, args); |
| 1472 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1473 | return NO_ERROR; |
| 1474 | } |
| 1475 | |
| 1476 | // Thread virtuals |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 1477 | status_t AudioFlinger::PlaybackThread::readyToRun() |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1478 | { |
Eric Laurent | 828b977 | 2011-08-07 16:32:26 -0700 | [diff] [blame] | 1479 | status_t status = initCheck(); |
| 1480 | if (status == NO_ERROR) { |
Steve Block | 6215d3f | 2012-01-04 20:05:49 +0000 | [diff] [blame] | 1481 | ALOGI("AudioFlinger's thread %p ready to run", this); |
Eric Laurent | 828b977 | 2011-08-07 16:32:26 -0700 | [diff] [blame] | 1482 | } else { |
Steve Block | 3762c31 | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 1483 | ALOGE("No working audio driver found."); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1484 | } |
Eric Laurent | 828b977 | 2011-08-07 16:32:26 -0700 | [diff] [blame] | 1485 | return status; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1486 | } |
| 1487 | |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 1488 | void AudioFlinger::PlaybackThread::onFirstRef() |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1489 | { |
Eric Laurent | 6dbdc40 | 2011-07-22 09:04:31 -0700 | [diff] [blame] | 1490 | run(mName, ANDROID_PRIORITY_URGENT_AUDIO); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1491 | } |
| 1492 | |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 1493 | // PlaybackThread::createTrack_l() must be called with AudioFlinger::mLock held |
| 1494 | sp<AudioFlinger::PlaybackThread::Track> AudioFlinger::PlaybackThread::createTrack_l( |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1495 | const sp<AudioFlinger::Client>& client, |
Glenn Kasten | bc1d77b | 2012-01-12 16:38:12 -0800 | [diff] [blame] | 1496 | audio_stream_type_t streamType, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1497 | uint32_t sampleRate, |
Glenn Kasten | 0a204ed | 2012-01-12 12:27:51 -0800 | [diff] [blame^] | 1498 | audio_format_t format, |
Jean-Michel Trivi | 5439223 | 2011-05-24 15:53:33 -0700 | [diff] [blame] | 1499 | uint32_t channelMask, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1500 | int frameCount, |
| 1501 | const sp<IMemory>& sharedBuffer, |
Eric Laurent | 65b6545 | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 1502 | int sessionId, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1503 | status_t *status) |
| 1504 | { |
| 1505 | sp<Track> track; |
| 1506 | status_t lStatus; |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 1507 | |
| 1508 | if (mType == DIRECT) { |
Jean-Michel Trivi | 5439223 | 2011-05-24 15:53:33 -0700 | [diff] [blame] | 1509 | if ((format & AUDIO_FORMAT_MAIN_MASK) == AUDIO_FORMAT_PCM) { |
| 1510 | if (sampleRate != mSampleRate || format != mFormat || channelMask != mChannelMask) { |
Steve Block | 3762c31 | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 1511 | ALOGE("createTrack_l() Bad parameter: sampleRate %d format %d, channelMask 0x%08x \"" |
Jean-Michel Trivi | 5439223 | 2011-05-24 15:53:33 -0700 | [diff] [blame] | 1512 | "for output %p with format %d", |
| 1513 | sampleRate, format, channelMask, mOutput, mFormat); |
| 1514 | lStatus = BAD_VALUE; |
| 1515 | goto Exit; |
| 1516 | } |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 1517 | } |
| 1518 | } else { |
| 1519 | // Resampler implementation limits input sampling rate to 2 x output sampling rate. |
| 1520 | if (sampleRate > mSampleRate*2) { |
Steve Block | 3762c31 | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 1521 | ALOGE("Sample rate out of range: %d mSampleRate %d", sampleRate, mSampleRate); |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 1522 | lStatus = BAD_VALUE; |
| 1523 | goto Exit; |
| 1524 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1525 | } |
| 1526 | |
Eric Laurent | 464d5b3 | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 1527 | lStatus = initCheck(); |
| 1528 | if (lStatus != NO_ERROR) { |
Steve Block | 3762c31 | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 1529 | ALOGE("Audio driver not initialized."); |
The Android Open Source Project | b2a3dd8 | 2009-03-09 11:52:12 -0700 | [diff] [blame] | 1530 | goto Exit; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1531 | } |
| 1532 | |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 1533 | { // scope for mLock |
| 1534 | Mutex::Autolock _l(mLock); |
Eric Laurent | 8ed6ed0 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 1535 | |
| 1536 | // all tracks in same audio session must share the same routing strategy otherwise |
| 1537 | // conflicts will happen when tracks are moved from one output to another by audio policy |
| 1538 | // manager |
| 1539 | uint32_t strategy = |
Dima Zavin | 24fc2fb | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 1540 | AudioSystem::getStrategyForStream((audio_stream_type_t)streamType); |
Eric Laurent | 8ed6ed0 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 1541 | for (size_t i = 0; i < mTracks.size(); ++i) { |
| 1542 | sp<Track> t = mTracks[i]; |
| 1543 | if (t != 0) { |
Glenn Kasten | 9818a9d | 2011-10-28 10:31:42 -0700 | [diff] [blame] | 1544 | uint32_t actual = AudioSystem::getStrategyForStream((audio_stream_type_t)t->type()); |
| 1545 | if (sessionId == t->sessionId() && strategy != actual) { |
Steve Block | 3762c31 | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 1546 | ALOGE("createTrack_l() mismatched strategy; expected %u but found %u", |
Glenn Kasten | 9818a9d | 2011-10-28 10:31:42 -0700 | [diff] [blame] | 1547 | strategy, actual); |
Eric Laurent | 8ed6ed0 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 1548 | lStatus = BAD_VALUE; |
| 1549 | goto Exit; |
| 1550 | } |
| 1551 | } |
| 1552 | } |
| 1553 | |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 1554 | track = new Track(this, client, streamType, sampleRate, format, |
Jean-Michel Trivi | 5439223 | 2011-05-24 15:53:33 -0700 | [diff] [blame] | 1555 | channelMask, frameCount, sharedBuffer, sessionId); |
Eric Laurent | 73b6035 | 2009-11-09 04:45:39 -0800 | [diff] [blame] | 1556 | if (track->getCblk() == NULL || track->name() < 0) { |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 1557 | lStatus = NO_MEMORY; |
| 1558 | goto Exit; |
| 1559 | } |
| 1560 | mTracks.add(track); |
Eric Laurent | 65b6545 | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 1561 | |
| 1562 | sp<EffectChain> chain = getEffectChain_l(sessionId); |
| 1563 | if (chain != 0) { |
Steve Block | 71f2cf1 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 1564 | ALOGV("createTrack_l() setting main buffer %p", chain->inBuffer()); |
Eric Laurent | 65b6545 | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 1565 | track->setMainBuffer(chain->inBuffer()); |
Dima Zavin | 24fc2fb | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 1566 | chain->setStrategy(AudioSystem::getStrategyForStream((audio_stream_type_t)track->type())); |
Eric Laurent | 90681d6 | 2011-05-09 12:09:06 -0700 | [diff] [blame] | 1567 | chain->incTrackCnt(); |
Eric Laurent | 65b6545 | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 1568 | } |
Eric Laurent | 05ce094 | 2011-08-30 10:18:54 -0700 | [diff] [blame] | 1569 | |
| 1570 | // invalidate track immediately if the stream type was moved to another thread since |
| 1571 | // createTrack() was called by the client process. |
| 1572 | if (!mStreamTypes[streamType].valid) { |
Steve Block | 8564c8d | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 1573 | ALOGW("createTrack_l() on thread %p: invalidating track on stream %d", |
Eric Laurent | 05ce094 | 2011-08-30 10:18:54 -0700 | [diff] [blame] | 1574 | this, streamType); |
| 1575 | android_atomic_or(CBLK_INVALID_ON, &track->mCblk->flags); |
| 1576 | } |
The Android Open Source Project | b2a3dd8 | 2009-03-09 11:52:12 -0700 | [diff] [blame] | 1577 | } |
The Android Open Source Project | b2a3dd8 | 2009-03-09 11:52:12 -0700 | [diff] [blame] | 1578 | lStatus = NO_ERROR; |
| 1579 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1580 | Exit: |
| 1581 | if(status) { |
| 1582 | *status = lStatus; |
| 1583 | } |
| 1584 | return track; |
| 1585 | } |
| 1586 | |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 1587 | uint32_t AudioFlinger::PlaybackThread::latency() const |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1588 | { |
Eric Laurent | 828b977 | 2011-08-07 16:32:26 -0700 | [diff] [blame] | 1589 | Mutex::Autolock _l(mLock); |
| 1590 | if (initCheck() == NO_ERROR) { |
Dima Zavin | 31f18889 | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 1591 | return mOutput->stream->get_latency(mOutput->stream); |
Eric Laurent | 828b977 | 2011-08-07 16:32:26 -0700 | [diff] [blame] | 1592 | } else { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1593 | return 0; |
| 1594 | } |
| 1595 | } |
| 1596 | |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 1597 | status_t AudioFlinger::PlaybackThread::setMasterVolume(float value) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1598 | { |
| 1599 | mMasterVolume = value; |
| 1600 | return NO_ERROR; |
| 1601 | } |
| 1602 | |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 1603 | status_t AudioFlinger::PlaybackThread::setMasterMute(bool muted) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1604 | { |
| 1605 | mMasterMute = muted; |
| 1606 | return NO_ERROR; |
| 1607 | } |
| 1608 | |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 1609 | float AudioFlinger::PlaybackThread::masterVolume() const |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1610 | { |
| 1611 | return mMasterVolume; |
| 1612 | } |
| 1613 | |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 1614 | bool AudioFlinger::PlaybackThread::masterMute() const |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1615 | { |
| 1616 | return mMasterMute; |
| 1617 | } |
| 1618 | |
Glenn Kasten | bc1d77b | 2012-01-12 16:38:12 -0800 | [diff] [blame] | 1619 | status_t AudioFlinger::PlaybackThread::setStreamVolume(audio_stream_type_t stream, float value) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1620 | { |
| 1621 | mStreamTypes[stream].volume = value; |
| 1622 | return NO_ERROR; |
| 1623 | } |
| 1624 | |
Glenn Kasten | bc1d77b | 2012-01-12 16:38:12 -0800 | [diff] [blame] | 1625 | status_t AudioFlinger::PlaybackThread::setStreamMute(audio_stream_type_t stream, bool muted) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1626 | { |
| 1627 | mStreamTypes[stream].mute = muted; |
| 1628 | return NO_ERROR; |
| 1629 | } |
| 1630 | |
Glenn Kasten | bc1d77b | 2012-01-12 16:38:12 -0800 | [diff] [blame] | 1631 | float AudioFlinger::PlaybackThread::streamVolume(audio_stream_type_t stream) const |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1632 | { |
| 1633 | return mStreamTypes[stream].volume; |
| 1634 | } |
| 1635 | |
Glenn Kasten | bc1d77b | 2012-01-12 16:38:12 -0800 | [diff] [blame] | 1636 | bool AudioFlinger::PlaybackThread::streamMute(audio_stream_type_t stream) const |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1637 | { |
| 1638 | return mStreamTypes[stream].mute; |
| 1639 | } |
| 1640 | |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 1641 | // addTrack_l() must be called with ThreadBase::mLock held |
| 1642 | status_t AudioFlinger::PlaybackThread::addTrack_l(const sp<Track>& track) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1643 | { |
| 1644 | status_t status = ALREADY_EXISTS; |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1645 | |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1646 | // set retry count for buffer fill |
| 1647 | track->mRetryCount = kMaxTrackStartupRetries; |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1648 | if (mActiveTracks.indexOf(track) < 0) { |
| 1649 | // the track is newly added, make sure it fills up all its |
| 1650 | // buffers before playing. This is to ensure the client will |
| 1651 | // effectively get the latency it requested. |
| 1652 | track->mFillingUpStatus = Track::FS_FILLING; |
The Android Open Source Project | f013e1a | 2008-12-17 18:05:43 -0800 | [diff] [blame] | 1653 | track->mResetDone = false; |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 1654 | mActiveTracks.add(track); |
Eric Laurent | 65b6545 | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 1655 | if (track->mainBuffer() != mMixBuffer) { |
| 1656 | sp<EffectChain> chain = getEffectChain_l(track->sessionId()); |
| 1657 | if (chain != 0) { |
Steve Block | 71f2cf1 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 1658 | ALOGV("addTrack_l() starting track on chain %p for session %d", chain.get(), track->sessionId()); |
Eric Laurent | 90681d6 | 2011-05-09 12:09:06 -0700 | [diff] [blame] | 1659 | chain->incActiveTrackCnt(); |
Eric Laurent | 65b6545 | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 1660 | } |
| 1661 | } |
| 1662 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1663 | status = NO_ERROR; |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1664 | } |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 1665 | |
Steve Block | 71f2cf1 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 1666 | ALOGV("mWaitWorkCV.broadcast"); |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 1667 | mWaitWorkCV.broadcast(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1668 | |
| 1669 | return status; |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1670 | } |
| 1671 | |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 1672 | // destroyTrack_l() must be called with ThreadBase::mLock held |
| 1673 | void AudioFlinger::PlaybackThread::destroyTrack_l(const sp<Track>& track) |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1674 | { |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1675 | track->mState = TrackBase::TERMINATED; |
| 1676 | if (mActiveTracks.indexOf(track) < 0) { |
Eric Laurent | 90681d6 | 2011-05-09 12:09:06 -0700 | [diff] [blame] | 1677 | removeTrack_l(track); |
| 1678 | } |
| 1679 | } |
| 1680 | |
| 1681 | void AudioFlinger::PlaybackThread::removeTrack_l(const sp<Track>& track) |
| 1682 | { |
| 1683 | mTracks.remove(track); |
| 1684 | deleteTrackName_l(track->name()); |
| 1685 | sp<EffectChain> chain = getEffectChain_l(track->sessionId()); |
| 1686 | if (chain != 0) { |
| 1687 | chain->decTrackCnt(); |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1688 | } |
| 1689 | } |
| 1690 | |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 1691 | String8 AudioFlinger::PlaybackThread::getParameters(const String8& keys) |
The Android Open Source Project | f1e484a | 2009-01-22 00:13:42 -0800 | [diff] [blame] | 1692 | { |
Eric Laurent | 828b977 | 2011-08-07 16:32:26 -0700 | [diff] [blame] | 1693 | String8 out_s8 = String8(""); |
Dima Zavin | 24fc2fb | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 1694 | char *s; |
| 1695 | |
Eric Laurent | 828b977 | 2011-08-07 16:32:26 -0700 | [diff] [blame] | 1696 | Mutex::Autolock _l(mLock); |
| 1697 | if (initCheck() != NO_ERROR) { |
| 1698 | return out_s8; |
| 1699 | } |
| 1700 | |
Dima Zavin | 31f18889 | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 1701 | s = mOutput->stream->common.get_parameters(&mOutput->stream->common, keys.string()); |
Dima Zavin | 24fc2fb | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 1702 | out_s8 = String8(s); |
| 1703 | free(s); |
| 1704 | return out_s8; |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 1705 | } |
The Android Open Source Project | f1e484a | 2009-01-22 00:13:42 -0800 | [diff] [blame] | 1706 | |
Eric Laurent | 828b977 | 2011-08-07 16:32:26 -0700 | [diff] [blame] | 1707 | // audioConfigChanged_l() must be called with AudioFlinger::mLock held |
Eric Laurent | eb8f850d | 2010-05-14 03:26:45 -0700 | [diff] [blame] | 1708 | void AudioFlinger::PlaybackThread::audioConfigChanged_l(int event, int param) { |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 1709 | AudioSystem::OutputDescriptor desc; |
| 1710 | void *param2 = 0; |
| 1711 | |
Steve Block | 71f2cf1 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 1712 | ALOGV("PlaybackThread::audioConfigChanged_l, thread %p, event %d, param %d", this, event, param); |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 1713 | |
| 1714 | switch (event) { |
| 1715 | case AudioSystem::OUTPUT_OPENED: |
| 1716 | case AudioSystem::OUTPUT_CONFIG_CHANGED: |
Jean-Michel Trivi | 5439223 | 2011-05-24 15:53:33 -0700 | [diff] [blame] | 1717 | desc.channels = mChannelMask; |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 1718 | desc.samplingRate = mSampleRate; |
| 1719 | desc.format = mFormat; |
| 1720 | desc.frameCount = mFrameCount; |
| 1721 | desc.latency = latency(); |
| 1722 | param2 = &desc; |
| 1723 | break; |
| 1724 | |
| 1725 | case AudioSystem::STREAM_CONFIG_CHANGED: |
| 1726 | param2 = ¶m; |
| 1727 | case AudioSystem::OUTPUT_CLOSED: |
| 1728 | default: |
| 1729 | break; |
| 1730 | } |
Eric Laurent | 49f02be | 2009-11-19 09:00:56 -0800 | [diff] [blame] | 1731 | mAudioFlinger->audioConfigChanged_l(event, mId, param2); |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 1732 | } |
| 1733 | |
| 1734 | void AudioFlinger::PlaybackThread::readOutputParameters() |
| 1735 | { |
Dima Zavin | 31f18889 | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 1736 | mSampleRate = mOutput->stream->common.get_sample_rate(&mOutput->stream->common); |
Jean-Michel Trivi | 5439223 | 2011-05-24 15:53:33 -0700 | [diff] [blame] | 1737 | mChannelMask = mOutput->stream->common.get_channels(&mOutput->stream->common); |
| 1738 | mChannelCount = (uint16_t)popcount(mChannelMask); |
Dima Zavin | 31f18889 | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 1739 | mFormat = mOutput->stream->common.get_format(&mOutput->stream->common); |
Glenn Kasten | faf354d | 2012-01-11 09:48:27 -0800 | [diff] [blame] | 1740 | mFrameSize = audio_stream_frame_size(&mOutput->stream->common); |
Dima Zavin | 31f18889 | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 1741 | mFrameCount = mOutput->stream->common.get_buffer_size(&mOutput->stream->common) / mFrameSize; |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 1742 | |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 1743 | // FIXME - Current mixer implementation only supports stereo output: Always |
| 1744 | // Allocate a stereo buffer even if HW output is mono. |
Eric Laurent | 65b6545 | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 1745 | if (mMixBuffer != NULL) delete[] mMixBuffer; |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 1746 | mMixBuffer = new int16_t[mFrameCount * 2]; |
| 1747 | memset(mMixBuffer, 0, mFrameCount * 2 * sizeof(int16_t)); |
Eric Laurent | 65b6545 | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 1748 | |
Eric Laurent | 8ed6ed0 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 1749 | // force reconfiguration of effect chains and engines to take new buffer size and audio |
| 1750 | // parameters into account |
| 1751 | // Note that mLock is not held when readOutputParameters() is called from the constructor |
| 1752 | // but in this case nothing is done below as no audio sessions have effect yet so it doesn't |
| 1753 | // matter. |
| 1754 | // create a copy of mEffectChains as calling moveEffectChain_l() can reorder some effect chains |
| 1755 | Vector< sp<EffectChain> > effectChains = mEffectChains; |
| 1756 | for (size_t i = 0; i < effectChains.size(); i ++) { |
Eric Laurent | 493941b | 2010-07-28 01:32:47 -0700 | [diff] [blame] | 1757 | mAudioFlinger->moveEffectChain_l(effectChains[i]->sessionId(), this, this, false); |
Eric Laurent | 8ed6ed0 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 1758 | } |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 1759 | } |
| 1760 | |
Eric Laurent | 0986e79 | 2010-01-19 17:37:09 -0800 | [diff] [blame] | 1761 | status_t AudioFlinger::PlaybackThread::getRenderPosition(uint32_t *halFrames, uint32_t *dspFrames) |
| 1762 | { |
| 1763 | if (halFrames == 0 || dspFrames == 0) { |
| 1764 | return BAD_VALUE; |
| 1765 | } |
Eric Laurent | 828b977 | 2011-08-07 16:32:26 -0700 | [diff] [blame] | 1766 | Mutex::Autolock _l(mLock); |
Eric Laurent | 464d5b3 | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 1767 | if (initCheck() != NO_ERROR) { |
Eric Laurent | 0986e79 | 2010-01-19 17:37:09 -0800 | [diff] [blame] | 1768 | return INVALID_OPERATION; |
| 1769 | } |
Dima Zavin | 31f18889 | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 1770 | *halFrames = mBytesWritten / audio_stream_frame_size(&mOutput->stream->common); |
Eric Laurent | 0986e79 | 2010-01-19 17:37:09 -0800 | [diff] [blame] | 1771 | |
Dima Zavin | 31f18889 | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 1772 | return mOutput->stream->get_render_position(mOutput->stream, dspFrames); |
Eric Laurent | 0986e79 | 2010-01-19 17:37:09 -0800 | [diff] [blame] | 1773 | } |
| 1774 | |
Eric Laurent | 493941b | 2010-07-28 01:32:47 -0700 | [diff] [blame] | 1775 | uint32_t AudioFlinger::PlaybackThread::hasAudioSession(int sessionId) |
Eric Laurent | 65b6545 | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 1776 | { |
| 1777 | Mutex::Autolock _l(mLock); |
Eric Laurent | 493941b | 2010-07-28 01:32:47 -0700 | [diff] [blame] | 1778 | uint32_t result = 0; |
Eric Laurent | 65b6545 | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 1779 | if (getEffectChain_l(sessionId) != 0) { |
Eric Laurent | 493941b | 2010-07-28 01:32:47 -0700 | [diff] [blame] | 1780 | result = EFFECT_SESSION; |
Eric Laurent | 65b6545 | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 1781 | } |
| 1782 | |
| 1783 | for (size_t i = 0; i < mTracks.size(); ++i) { |
| 1784 | sp<Track> track = mTracks[i]; |
Eric Laurent | 8ed6ed0 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 1785 | if (sessionId == track->sessionId() && |
| 1786 | !(track->mCblk->flags & CBLK_INVALID_MSK)) { |
Eric Laurent | 493941b | 2010-07-28 01:32:47 -0700 | [diff] [blame] | 1787 | result |= TRACK_SESSION; |
| 1788 | break; |
Eric Laurent | 65b6545 | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 1789 | } |
| 1790 | } |
| 1791 | |
Eric Laurent | 493941b | 2010-07-28 01:32:47 -0700 | [diff] [blame] | 1792 | return result; |
Eric Laurent | 65b6545 | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 1793 | } |
| 1794 | |
Eric Laurent | 8ed6ed0 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 1795 | uint32_t AudioFlinger::PlaybackThread::getStrategyForSession_l(int sessionId) |
| 1796 | { |
Dima Zavin | 24fc2fb | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 1797 | // session AUDIO_SESSION_OUTPUT_MIX is placed in same strategy as MUSIC stream so that |
Eric Laurent | 8ed6ed0 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 1798 | // it is moved to correct output by audio policy manager when A2DP is connected or disconnected |
Dima Zavin | 24fc2fb | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 1799 | if (sessionId == AUDIO_SESSION_OUTPUT_MIX) { |
| 1800 | return AudioSystem::getStrategyForStream(AUDIO_STREAM_MUSIC); |
Eric Laurent | 8ed6ed0 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 1801 | } |
| 1802 | for (size_t i = 0; i < mTracks.size(); i++) { |
| 1803 | sp<Track> track = mTracks[i]; |
| 1804 | if (sessionId == track->sessionId() && |
| 1805 | !(track->mCblk->flags & CBLK_INVALID_MSK)) { |
Dima Zavin | 24fc2fb | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 1806 | return AudioSystem::getStrategyForStream((audio_stream_type_t) track->type()); |
Eric Laurent | 8ed6ed0 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 1807 | } |
| 1808 | } |
Dima Zavin | 24fc2fb | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 1809 | return AudioSystem::getStrategyForStream(AUDIO_STREAM_MUSIC); |
Eric Laurent | 8ed6ed0 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 1810 | } |
| 1811 | |
Eric Laurent | 53334cd | 2010-06-23 17:38:20 -0700 | [diff] [blame] | 1812 | |
Eric Laurent | 828b977 | 2011-08-07 16:32:26 -0700 | [diff] [blame] | 1813 | AudioFlinger::AudioStreamOut* AudioFlinger::PlaybackThread::getOutput() |
| 1814 | { |
| 1815 | Mutex::Autolock _l(mLock); |
| 1816 | return mOutput; |
| 1817 | } |
| 1818 | |
| 1819 | AudioFlinger::AudioStreamOut* AudioFlinger::PlaybackThread::clearOutput() |
| 1820 | { |
| 1821 | Mutex::Autolock _l(mLock); |
| 1822 | AudioStreamOut *output = mOutput; |
| 1823 | mOutput = NULL; |
| 1824 | return output; |
| 1825 | } |
| 1826 | |
| 1827 | // this method must always be called either with ThreadBase mLock held or inside the thread loop |
| 1828 | audio_stream_t* AudioFlinger::PlaybackThread::stream() |
| 1829 | { |
| 1830 | if (mOutput == NULL) { |
| 1831 | return NULL; |
| 1832 | } |
| 1833 | return &mOutput->stream->common; |
| 1834 | } |
| 1835 | |
Eric Laurent | 4433169 | 2011-12-05 09:47:19 -0800 | [diff] [blame] | 1836 | uint32_t AudioFlinger::PlaybackThread::activeSleepTimeUs() |
| 1837 | { |
| 1838 | // A2DP output latency is not due only to buffering capacity. It also reflects encoding, |
| 1839 | // decoding and transfer time. So sleeping for half of the latency would likely cause |
| 1840 | // underruns |
| 1841 | if (audio_is_a2dp_device((audio_devices_t)mDevice)) { |
| 1842 | return (uint32_t)((uint32_t)((mFrameCount * 1000) / mSampleRate) * 1000); |
| 1843 | } else { |
| 1844 | return (uint32_t)(mOutput->stream->get_latency(mOutput->stream) * 1000) / 2; |
| 1845 | } |
| 1846 | } |
| 1847 | |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 1848 | // ---------------------------------------------------------------------------- |
| 1849 | |
Dima Zavin | 31f18889 | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 1850 | AudioFlinger::MixerThread::MixerThread(const sp<AudioFlinger>& audioFlinger, AudioStreamOut* output, int id, uint32_t device) |
Eric Laurent | 65b6545 | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 1851 | : PlaybackThread(audioFlinger, output, id, device), |
Eric Laurent | 3562931 | 2012-01-19 10:00:02 -0800 | [diff] [blame] | 1852 | mAudioMixer(NULL), mPrevMixerStatus(MIXER_IDLE) |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 1853 | { |
Eric Laurent | 464d5b3 | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 1854 | mType = ThreadBase::MIXER; |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 1855 | mAudioMixer = new AudioMixer(mFrameCount, mSampleRate); |
| 1856 | |
| 1857 | // FIXME - Current mixer implementation only supports stereo output |
| 1858 | if (mChannelCount == 1) { |
Steve Block | 3762c31 | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 1859 | ALOGE("Invalid audio hardware channel count"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1860 | } |
The Android Open Source Project | f1e484a | 2009-01-22 00:13:42 -0800 | [diff] [blame] | 1861 | } |
| 1862 | |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 1863 | AudioFlinger::MixerThread::~MixerThread() |
The Android Open Source Project | f1e484a | 2009-01-22 00:13:42 -0800 | [diff] [blame] | 1864 | { |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 1865 | delete mAudioMixer; |
| 1866 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1867 | |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 1868 | bool AudioFlinger::MixerThread::threadLoop() |
| 1869 | { |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 1870 | Vector< sp<Track> > tracksToRemove; |
Eric Laurent | 059b4be | 2009-11-09 23:32:22 -0800 | [diff] [blame] | 1871 | uint32_t mixerStatus = MIXER_IDLE; |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 1872 | nsecs_t standbyTime = systemTime(); |
| 1873 | size_t mixBufferSize = mFrameCount * mFrameSize; |
Dave Sparks | d0ac8c0 | 2009-09-30 03:09:03 -0700 | [diff] [blame] | 1874 | // FIXME: Relaxed timing because of a certain device that can't meet latency |
| 1875 | // Should be reduced to 2x after the vendor fixes the driver issue |
Eric Laurent | 276fa43 | 2011-10-18 15:42:27 -0700 | [diff] [blame] | 1876 | // increase threshold again due to low power audio mode. The way this warning threshold is |
| 1877 | // calculated and its usefulness should be reconsidered anyway. |
| 1878 | nsecs_t maxPeriod = seconds(mFrameCount) / mSampleRate * 15; |
Dave Sparks | d0ac8c0 | 2009-09-30 03:09:03 -0700 | [diff] [blame] | 1879 | nsecs_t lastWarning = 0; |
Eric Laurent | 059b4be | 2009-11-09 23:32:22 -0800 | [diff] [blame] | 1880 | bool longStandbyExit = false; |
| 1881 | uint32_t activeSleepTime = activeSleepTimeUs(); |
| 1882 | uint32_t idleSleepTime = idleSleepTimeUs(); |
| 1883 | uint32_t sleepTime = idleSleepTime; |
Eric Laurent | f1f5fc8 | 2011-11-22 18:50:29 -0800 | [diff] [blame] | 1884 | uint32_t sleepTimeShift = 0; |
Eric Laurent | 65b6545 | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 1885 | Vector< sp<EffectChain> > effectChains; |
Glenn Kasten | da494f9 | 2011-07-08 15:26:12 -0700 | [diff] [blame] | 1886 | #ifdef DEBUG_CPU_USAGE |
| 1887 | ThreadCpuUsage cpu; |
| 1888 | const CentralTendencyStatistics& stats = cpu.statistics(); |
| 1889 | #endif |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1890 | |
Eric Laurent | 6dbdc40 | 2011-07-22 09:04:31 -0700 | [diff] [blame] | 1891 | acquireWakeLock(); |
| 1892 | |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 1893 | while (!exitPending()) |
| 1894 | { |
Glenn Kasten | da494f9 | 2011-07-08 15:26:12 -0700 | [diff] [blame] | 1895 | #ifdef DEBUG_CPU_USAGE |
| 1896 | cpu.sampleAndEnable(); |
| 1897 | unsigned n = stats.n(); |
| 1898 | // cpu.elapsed() is expensive, so don't call it every loop |
| 1899 | if ((n & 127) == 1) { |
| 1900 | long long elapsed = cpu.elapsed(); |
| 1901 | if (elapsed >= DEBUG_CPU_USAGE * 1000000000LL) { |
| 1902 | double perLoop = elapsed / (double) n; |
| 1903 | double perLoop100 = perLoop * 0.01; |
| 1904 | double mean = stats.mean(); |
| 1905 | double stddev = stats.stddev(); |
| 1906 | double minimum = stats.minimum(); |
| 1907 | double maximum = stats.maximum(); |
| 1908 | cpu.resetStatistics(); |
Steve Block | 6215d3f | 2012-01-04 20:05:49 +0000 | [diff] [blame] | 1909 | 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 | da494f9 | 2011-07-08 15:26:12 -0700 | [diff] [blame] | 1910 | elapsed * .000000001, n, perLoop * .000001, |
| 1911 | mean * .001, |
| 1912 | stddev * .001, |
| 1913 | minimum * .001, |
| 1914 | maximum * .001, |
| 1915 | mean / perLoop100, |
| 1916 | stddev / perLoop100, |
| 1917 | minimum / perLoop100, |
| 1918 | maximum / perLoop100); |
| 1919 | } |
| 1920 | } |
| 1921 | #endif |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 1922 | processConfigEvents(); |
| 1923 | |
Eric Laurent | 059b4be | 2009-11-09 23:32:22 -0800 | [diff] [blame] | 1924 | mixerStatus = MIXER_IDLE; |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 1925 | { // scope for mLock |
| 1926 | |
| 1927 | Mutex::Autolock _l(mLock); |
| 1928 | |
| 1929 | if (checkForNewParameters_l()) { |
| 1930 | mixBufferSize = mFrameCount * mFrameSize; |
Dave Sparks | d0ac8c0 | 2009-09-30 03:09:03 -0700 | [diff] [blame] | 1931 | // FIXME: Relaxed timing because of a certain device that can't meet latency |
| 1932 | // Should be reduced to 2x after the vendor fixes the driver issue |
Eric Laurent | 276fa43 | 2011-10-18 15:42:27 -0700 | [diff] [blame] | 1933 | // increase threshold again due to low power audio mode. The way this warning |
| 1934 | // threshold is calculated and its usefulness should be reconsidered anyway. |
| 1935 | maxPeriod = seconds(mFrameCount) / mSampleRate * 15; |
Eric Laurent | 059b4be | 2009-11-09 23:32:22 -0800 | [diff] [blame] | 1936 | activeSleepTime = activeSleepTimeUs(); |
| 1937 | idleSleepTime = idleSleepTimeUs(); |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 1938 | } |
| 1939 | |
| 1940 | const SortedVector< wp<Track> >& activeTracks = mActiveTracks; |
| 1941 | |
| 1942 | // put audio hardware into standby after short delay |
Glenn Kasten | e80a4cc | 2011-12-15 09:51:17 -0800 | [diff] [blame] | 1943 | if (CC_UNLIKELY((!activeTracks.size() && systemTime() > standbyTime) || |
| 1944 | mSuspended)) { |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 1945 | if (!mStandby) { |
Steve Block | 71f2cf1 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 1946 | ALOGV("Audio hardware entering standby, mixer %p, mSuspended %d\n", this, mSuspended); |
Dima Zavin | 31f18889 | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 1947 | mOutput->stream->common.standby(&mOutput->stream->common); |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 1948 | mStandby = true; |
| 1949 | mBytesWritten = 0; |
| 1950 | } |
| 1951 | |
| 1952 | if (!activeTracks.size() && mConfigEvents.isEmpty()) { |
| 1953 | // we're about to wait, flush the binder command buffer |
| 1954 | IPCThreadState::self()->flushCommands(); |
| 1955 | |
| 1956 | if (exitPending()) break; |
| 1957 | |
Eric Laurent | 6dbdc40 | 2011-07-22 09:04:31 -0700 | [diff] [blame] | 1958 | releaseWakeLock_l(); |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 1959 | // wait until we have something to do... |
Steve Block | 71f2cf1 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 1960 | ALOGV("MixerThread %p TID %d going to sleep\n", this, gettid()); |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 1961 | mWaitWorkCV.wait(mLock); |
Steve Block | 71f2cf1 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 1962 | ALOGV("MixerThread %p TID %d waking up\n", this, gettid()); |
Eric Laurent | 6dbdc40 | 2011-07-22 09:04:31 -0700 | [diff] [blame] | 1963 | acquireWakeLock_l(); |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 1964 | |
Eric Laurent | 71c4496 | 2012-01-17 19:20:12 -0800 | [diff] [blame] | 1965 | mPrevMixerStatus = MIXER_IDLE; |
Glenn Kasten | b737dbb | 2012-01-13 15:54:24 -0800 | [diff] [blame] | 1966 | if (!mMasterMute) { |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 1967 | char value[PROPERTY_VALUE_MAX]; |
| 1968 | property_get("ro.audio.silent", value, "0"); |
| 1969 | if (atoi(value)) { |
Steve Block | 5baa3a6 | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 1970 | ALOGD("Silence is golden"); |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 1971 | setMasterMute(true); |
| 1972 | } |
| 1973 | } |
| 1974 | |
| 1975 | standbyTime = systemTime() + kStandbyTimeInNsecs; |
Eric Laurent | 059b4be | 2009-11-09 23:32:22 -0800 | [diff] [blame] | 1976 | sleepTime = idleSleepTime; |
Eric Laurent | f1f5fc8 | 2011-11-22 18:50:29 -0800 | [diff] [blame] | 1977 | sleepTimeShift = 0; |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 1978 | continue; |
| 1979 | } |
| 1980 | } |
| 1981 | |
Eric Laurent | 059b4be | 2009-11-09 23:32:22 -0800 | [diff] [blame] | 1982 | mixerStatus = prepareTracks_l(activeTracks, &tracksToRemove); |
Eric Laurent | 65b6545 | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 1983 | |
| 1984 | // prevent any changes in effect chain list and in each effect chain |
| 1985 | // during mixing and effect process as the audio buffers could be deleted |
| 1986 | // or modified if an effect is created or deleted |
Eric Laurent | 8ed6ed0 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 1987 | lockEffectChains_l(effectChains); |
Glenn Kasten | fb2ab9e | 2011-12-12 09:05:55 -0800 | [diff] [blame] | 1988 | } |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 1989 | |
Glenn Kasten | e80a4cc | 2011-12-15 09:51:17 -0800 | [diff] [blame] | 1990 | if (CC_LIKELY(mixerStatus == MIXER_TRACKS_READY)) { |
Eric Laurent | f69a3f8 | 2009-09-22 00:35:48 -0700 | [diff] [blame] | 1991 | // mix buffers... |
Eric Laurent | 65b6545 | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 1992 | mAudioMixer->process(); |
Eric Laurent | f69a3f8 | 2009-09-22 00:35:48 -0700 | [diff] [blame] | 1993 | sleepTime = 0; |
Eric Laurent | f1f5fc8 | 2011-11-22 18:50:29 -0800 | [diff] [blame] | 1994 | // increase sleep time progressively when application underrun condition clears |
| 1995 | if (sleepTimeShift > 0) { |
| 1996 | sleepTimeShift--; |
| 1997 | } |
Eric Laurent | f69a3f8 | 2009-09-22 00:35:48 -0700 | [diff] [blame] | 1998 | standbyTime = systemTime() + kStandbyTimeInNsecs; |
Eric Laurent | 65b6545 | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 1999 | //TODO: delay standby when effects have a tail |
Eric Laurent | 96c08a6 | 2009-09-07 08:38:38 -0700 | [diff] [blame] | 2000 | } else { |
Eric Laurent | 62443f5 | 2009-10-05 20:29:18 -0700 | [diff] [blame] | 2001 | // If no tracks are ready, sleep once for the duration of an output |
| 2002 | // buffer size, then write 0s to the output |
| 2003 | if (sleepTime == 0) { |
Eric Laurent | 059b4be | 2009-11-09 23:32:22 -0800 | [diff] [blame] | 2004 | if (mixerStatus == MIXER_TRACKS_ENABLED) { |
Eric Laurent | f1f5fc8 | 2011-11-22 18:50:29 -0800 | [diff] [blame] | 2005 | sleepTime = activeSleepTime >> sleepTimeShift; |
| 2006 | if (sleepTime < kMinThreadSleepTimeUs) { |
| 2007 | sleepTime = kMinThreadSleepTimeUs; |
| 2008 | } |
| 2009 | // reduce sleep time in case of consecutive application underruns to avoid |
| 2010 | // starving the audio HAL. As activeSleepTimeUs() is larger than a buffer |
| 2011 | // duration we would end up writing less data than needed by the audio HAL if |
| 2012 | // the condition persists. |
| 2013 | if (sleepTimeShift < kMaxThreadSleepTimeShift) { |
| 2014 | sleepTimeShift++; |
| 2015 | } |
Eric Laurent | 059b4be | 2009-11-09 23:32:22 -0800 | [diff] [blame] | 2016 | } else { |
| 2017 | sleepTime = idleSleepTime; |
| 2018 | } |
| 2019 | } else if (mBytesWritten != 0 || |
| 2020 | (mixerStatus == MIXER_TRACKS_ENABLED && longStandbyExit)) { |
Eric Laurent | 65b6545 | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 2021 | memset (mMixBuffer, 0, mixBufferSize); |
Eric Laurent | 96c08a6 | 2009-09-07 08:38:38 -0700 | [diff] [blame] | 2022 | sleepTime = 0; |
Steve Block | 71f2cf1 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 2023 | ALOGV_IF((mBytesWritten == 0 && (mixerStatus == MIXER_TRACKS_ENABLED && longStandbyExit)), "anticipated start"); |
Eric Laurent | 96c08a6 | 2009-09-07 08:38:38 -0700 | [diff] [blame] | 2024 | } |
Eric Laurent | 65b6545 | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 2025 | // TODO add standby time extension fct of effect tail |
Eric Laurent | f69a3f8 | 2009-09-22 00:35:48 -0700 | [diff] [blame] | 2026 | } |
| 2027 | |
| 2028 | if (mSuspended) { |
Eric Laurent | 8448a79 | 2010-08-18 18:13:17 -0700 | [diff] [blame] | 2029 | sleepTime = suspendSleepTimeUs(); |
Eric Laurent | f69a3f8 | 2009-09-22 00:35:48 -0700 | [diff] [blame] | 2030 | } |
| 2031 | // sleepTime == 0 means we must write to audio hardware |
| 2032 | if (sleepTime == 0) { |
Glenn Kasten | fb2ab9e | 2011-12-12 09:05:55 -0800 | [diff] [blame] | 2033 | for (size_t i = 0; i < effectChains.size(); i ++) { |
| 2034 | effectChains[i]->process_l(); |
| 2035 | } |
| 2036 | // enable changes in effect chain |
| 2037 | unlockEffectChains(effectChains); |
Eric Laurent | 65b6545 | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 2038 | mLastWriteTime = systemTime(); |
| 2039 | mInWrite = true; |
| 2040 | mBytesWritten += mixBufferSize; |
| 2041 | |
Dima Zavin | 31f18889 | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 2042 | int bytesWritten = (int)mOutput->stream->write(mOutput->stream, mMixBuffer, mixBufferSize); |
Eric Laurent | 0986e79 | 2010-01-19 17:37:09 -0800 | [diff] [blame] | 2043 | if (bytesWritten < 0) mBytesWritten -= mixBufferSize; |
Eric Laurent | f69a3f8 | 2009-09-22 00:35:48 -0700 | [diff] [blame] | 2044 | mNumWrites++; |
| 2045 | mInWrite = false; |
Dave Sparks | d0ac8c0 | 2009-09-30 03:09:03 -0700 | [diff] [blame] | 2046 | nsecs_t now = systemTime(); |
| 2047 | nsecs_t delta = now - mLastWriteTime; |
Eric Laurent | 276fa43 | 2011-10-18 15:42:27 -0700 | [diff] [blame] | 2048 | if (!mStandby && delta > maxPeriod) { |
Eric Laurent | f69a3f8 | 2009-09-22 00:35:48 -0700 | [diff] [blame] | 2049 | mNumDelayedWrites++; |
Glenn Kasten | cbfe2e1 | 2011-12-13 11:04:14 -0800 | [diff] [blame] | 2050 | if ((now - lastWarning) > kWarningThrottleNs) { |
Steve Block | 8564c8d | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 2051 | ALOGW("write blocked for %llu msecs, %d delayed writes, thread %p", |
Dave Sparks | d0ac8c0 | 2009-09-30 03:09:03 -0700 | [diff] [blame] | 2052 | ns2ms(delta), mNumDelayedWrites, this); |
| 2053 | lastWarning = now; |
| 2054 | } |
Eric Laurent | 059b4be | 2009-11-09 23:32:22 -0800 | [diff] [blame] | 2055 | if (mStandby) { |
| 2056 | longStandbyExit = true; |
| 2057 | } |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 2058 | } |
Eric Laurent | 059b4be | 2009-11-09 23:32:22 -0800 | [diff] [blame] | 2059 | mStandby = false; |
Eric Laurent | f69a3f8 | 2009-09-22 00:35:48 -0700 | [diff] [blame] | 2060 | } else { |
Eric Laurent | 65b6545 | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 2061 | // enable changes in effect chain |
Eric Laurent | 8ed6ed0 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 2062 | unlockEffectChains(effectChains); |
Eric Laurent | f69a3f8 | 2009-09-22 00:35:48 -0700 | [diff] [blame] | 2063 | usleep(sleepTime); |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 2064 | } |
| 2065 | |
| 2066 | // finally let go of all our tracks, without the lock held |
| 2067 | // since we can't guarantee the destructors won't acquire that |
| 2068 | // same lock. |
| 2069 | tracksToRemove.clear(); |
Eric Laurent | 65b6545 | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 2070 | |
| 2071 | // Effect chains will be actually deleted here if they were removed from |
| 2072 | // mEffectChains list during mixing or effects processing |
| 2073 | effectChains.clear(); |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 2074 | } |
| 2075 | |
| 2076 | if (!mStandby) { |
Dima Zavin | 31f18889 | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 2077 | mOutput->stream->common.standby(&mOutput->stream->common); |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 2078 | } |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 2079 | |
Eric Laurent | 6dbdc40 | 2011-07-22 09:04:31 -0700 | [diff] [blame] | 2080 | releaseWakeLock(); |
| 2081 | |
Steve Block | 71f2cf1 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 2082 | ALOGV("MixerThread %p exiting", this); |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 2083 | return false; |
| 2084 | } |
| 2085 | |
| 2086 | // prepareTracks_l() must be called with ThreadBase::mLock held |
Eric Laurent | 059b4be | 2009-11-09 23:32:22 -0800 | [diff] [blame] | 2087 | uint32_t AudioFlinger::MixerThread::prepareTracks_l(const SortedVector< wp<Track> >& activeTracks, Vector< sp<Track> > *tracksToRemove) |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 2088 | { |
| 2089 | |
Eric Laurent | 059b4be | 2009-11-09 23:32:22 -0800 | [diff] [blame] | 2090 | uint32_t mixerStatus = MIXER_IDLE; |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 2091 | // find out which tracks need to be processed |
| 2092 | size_t count = activeTracks.size(); |
Eric Laurent | 65b6545 | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 2093 | size_t mixedTracks = 0; |
| 2094 | size_t tracksWithEffect = 0; |
Glenn Kasten | 871c16c | 2010-03-05 12:18:01 -0800 | [diff] [blame] | 2095 | |
| 2096 | float masterVolume = mMasterVolume; |
| 2097 | bool masterMute = mMasterMute; |
| 2098 | |
Eric Laurent | 8cc93b9 | 2010-08-11 05:20:11 -0700 | [diff] [blame] | 2099 | if (masterMute) { |
| 2100 | masterVolume = 0; |
| 2101 | } |
Eric Laurent | 65b6545 | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 2102 | // Delegate master volume control to effect in output mix effect chain if needed |
Dima Zavin | 24fc2fb | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 2103 | sp<EffectChain> chain = getEffectChain_l(AUDIO_SESSION_OUTPUT_MIX); |
Eric Laurent | 65b6545 | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 2104 | if (chain != 0) { |
Eric Laurent | 8cc93b9 | 2010-08-11 05:20:11 -0700 | [diff] [blame] | 2105 | uint32_t v = (uint32_t)(masterVolume * (1 << 24)); |
Eric Laurent | 76c40f7 | 2010-07-15 12:50:15 -0700 | [diff] [blame] | 2106 | chain->setVolume_l(&v, &v); |
Eric Laurent | 65b6545 | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 2107 | masterVolume = (float)((v + (1 << 23)) >> 24); |
| 2108 | chain.clear(); |
| 2109 | } |
Glenn Kasten | 871c16c | 2010-03-05 12:18:01 -0800 | [diff] [blame] | 2110 | |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 2111 | for (size_t i=0 ; i<count ; i++) { |
| 2112 | sp<Track> t = activeTracks[i].promote(); |
| 2113 | if (t == 0) continue; |
| 2114 | |
Glenn Kasten | 68deb15 | 2011-12-19 15:06:39 -0800 | [diff] [blame] | 2115 | // this const just means the local variable doesn't change |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 2116 | Track* const track = t.get(); |
| 2117 | audio_track_cblk_t* cblk = track->cblk(); |
| 2118 | |
| 2119 | // The first time a track is added we wait |
| 2120 | // for all its buffers to be filled before processing it |
Glenn Kasten | 68deb15 | 2011-12-19 15:06:39 -0800 | [diff] [blame] | 2121 | int name = track->name(); |
Eric Laurent | 8a04fe0 | 2011-11-08 18:10:16 -0800 | [diff] [blame] | 2122 | // make sure that we have enough frames to mix one full buffer. |
| 2123 | // enforce this condition only once to enable draining the buffer in case the client |
| 2124 | // app does not call stop() and relies on underrun to stop: |
Eric Laurent | 71c4496 | 2012-01-17 19:20:12 -0800 | [diff] [blame] | 2125 | // hence the test on (mPrevMixerStatus == MIXER_TRACKS_READY) meaning the track was mixed |
Eric Laurent | 8a04fe0 | 2011-11-08 18:10:16 -0800 | [diff] [blame] | 2126 | // during last round |
Eric Laurent | 19ddf0e | 2011-11-03 12:16:05 -0700 | [diff] [blame] | 2127 | uint32_t minFrames = 1; |
Eric Laurent | 8a04fe0 | 2011-11-08 18:10:16 -0800 | [diff] [blame] | 2128 | if (!track->isStopped() && !track->isPausing() && |
Eric Laurent | 71c4496 | 2012-01-17 19:20:12 -0800 | [diff] [blame] | 2129 | (mPrevMixerStatus == MIXER_TRACKS_READY)) { |
Eric Laurent | 19ddf0e | 2011-11-03 12:16:05 -0700 | [diff] [blame] | 2130 | if (t->sampleRate() == (int)mSampleRate) { |
| 2131 | minFrames = mFrameCount; |
| 2132 | } else { |
Eric Laurent | 72dafb2 | 2011-12-22 16:08:41 -0800 | [diff] [blame] | 2133 | // +1 for rounding and +1 for additional sample needed for interpolation |
| 2134 | minFrames = (mFrameCount * t->sampleRate()) / mSampleRate + 1 + 1; |
| 2135 | // add frames already consumed but not yet released by the resampler |
| 2136 | // because cblk->framesReady() will include these frames |
| 2137 | minFrames += mAudioMixer->getUnreleasedFrames(track->name()); |
| 2138 | // the minimum track buffer size is normally twice the number of frames necessary |
| 2139 | // to fill one buffer and the resampler should not leave more than one buffer worth |
| 2140 | // of unreleased frames after each pass, but just in case... |
Steve Block | ec193de | 2012-01-09 18:35:44 +0000 | [diff] [blame] | 2141 | ALOG_ASSERT(minFrames <= cblk->frameCount); |
Eric Laurent | 19ddf0e | 2011-11-03 12:16:05 -0700 | [diff] [blame] | 2142 | } |
| 2143 | } |
| 2144 | if ((cblk->framesReady() >= minFrames) && track->isReady() && |
Eric Laurent | 71f37cd | 2010-03-31 12:21:17 -0700 | [diff] [blame] | 2145 | !track->isPaused() && !track->isTerminated()) |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 2146 | { |
Glenn Kasten | 68deb15 | 2011-12-19 15:06:39 -0800 | [diff] [blame] | 2147 | //ALOGV("track %d u=%08x, s=%08x [OK] on thread %p", name, cblk->user, cblk->server, this); |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 2148 | |
Eric Laurent | 65b6545 | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 2149 | mixedTracks++; |
| 2150 | |
| 2151 | // track->mainBuffer() != mMixBuffer means there is an effect chain |
| 2152 | // connected to the track |
| 2153 | chain.clear(); |
| 2154 | if (track->mainBuffer() != mMixBuffer) { |
| 2155 | chain = getEffectChain_l(track->sessionId()); |
| 2156 | // Delegate volume control to effect in track effect chain if needed |
| 2157 | if (chain != 0) { |
| 2158 | tracksWithEffect++; |
| 2159 | } else { |
Steve Block | 8564c8d | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 2160 | ALOGW("prepareTracks_l(): track %d attached to effect but no chain found on session %d", |
Glenn Kasten | 68deb15 | 2011-12-19 15:06:39 -0800 | [diff] [blame] | 2161 | name, track->sessionId()); |
Eric Laurent | 65b6545 | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 2162 | } |
| 2163 | } |
| 2164 | |
| 2165 | |
| 2166 | int param = AudioMixer::VOLUME; |
| 2167 | if (track->mFillingUpStatus == Track::FS_FILLED) { |
| 2168 | // no ramp for the first volume setting |
| 2169 | track->mFillingUpStatus = Track::FS_ACTIVE; |
| 2170 | if (track->mState == TrackBase::RESUMING) { |
| 2171 | track->mState = TrackBase::ACTIVE; |
| 2172 | param = AudioMixer::RAMP_VOLUME; |
| 2173 | } |
Glenn Kasten | 68deb15 | 2011-12-19 15:06:39 -0800 | [diff] [blame] | 2174 | mAudioMixer->setParameter(name, AudioMixer::RESAMPLE, AudioMixer::RESET, NULL); |
Eric Laurent | 65b6545 | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 2175 | } else if (cblk->server != 0) { |
| 2176 | // If the track is stopped before the first frame was mixed, |
| 2177 | // do not apply ramp |
| 2178 | param = AudioMixer::RAMP_VOLUME; |
| 2179 | } |
| 2180 | |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 2181 | // compute volume for this track |
Eric Laurent | 27a2fdf | 2010-09-10 17:44:44 -0700 | [diff] [blame] | 2182 | uint32_t vl, vr, va; |
Eric Laurent | 2a6b80b | 2010-07-29 23:43:43 -0700 | [diff] [blame] | 2183 | if (track->isMuted() || track->isPausing() || |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 2184 | mStreamTypes[track->type()].mute) { |
Eric Laurent | 27a2fdf | 2010-09-10 17:44:44 -0700 | [diff] [blame] | 2185 | vl = vr = va = 0; |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 2186 | if (track->isPausing()) { |
| 2187 | track->setPaused(); |
| 2188 | } |
| 2189 | } else { |
Eric Laurent | 27a2fdf | 2010-09-10 17:44:44 -0700 | [diff] [blame] | 2190 | |
Glenn Kasten | 871c16c | 2010-03-05 12:18:01 -0800 | [diff] [blame] | 2191 | // read original volumes with volume control |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 2192 | float typeVolume = mStreamTypes[track->type()].volume; |
Glenn Kasten | 871c16c | 2010-03-05 12:18:01 -0800 | [diff] [blame] | 2193 | float v = masterVolume * typeVolume; |
Glenn Kasten | 0632bad | 2012-01-17 12:20:54 -0800 | [diff] [blame] | 2194 | uint32_t vlr = cblk->volumeLR; |
| 2195 | vl = vlr & 0xFFFF; |
| 2196 | vr = vlr >> 16; |
| 2197 | // track volumes come from shared memory, so can't be trusted and must be clamped |
| 2198 | if (vl > MAX_GAIN_INT) { |
| 2199 | ALOGV("Track left volume out of range: %04X", vl); |
| 2200 | vl = MAX_GAIN_INT; |
| 2201 | } |
| 2202 | if (vr > MAX_GAIN_INT) { |
| 2203 | ALOGV("Track right volume out of range: %04X", vr); |
| 2204 | vr = MAX_GAIN_INT; |
| 2205 | } |
| 2206 | // now apply the master volume and stream type volume |
| 2207 | vl = (uint32_t)(v * vl) << 12; |
| 2208 | vr = (uint32_t)(v * vr) << 12; |
| 2209 | // assuming master volume and stream type volume each go up to 1.0, |
| 2210 | // vl and vr are now in 8.24 format |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 2211 | |
Glenn Kasten | 4790bd8 | 2012-01-03 14:22:33 -0800 | [diff] [blame] | 2212 | uint16_t sendLevel = cblk->getSendLevel_U4_12(); |
| 2213 | // send level comes from shared memory and so may be corrupt |
Glenn Kasten | 0632bad | 2012-01-17 12:20:54 -0800 | [diff] [blame] | 2214 | if (sendLevel >= MAX_GAIN_INT) { |
Glenn Kasten | 4790bd8 | 2012-01-03 14:22:33 -0800 | [diff] [blame] | 2215 | ALOGV("Track send level out of range: %04X", sendLevel); |
Glenn Kasten | 0632bad | 2012-01-17 12:20:54 -0800 | [diff] [blame] | 2216 | sendLevel = MAX_GAIN_INT; |
Glenn Kasten | 4790bd8 | 2012-01-03 14:22:33 -0800 | [diff] [blame] | 2217 | } |
| 2218 | va = (uint32_t)(v * sendLevel); |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 2219 | } |
Eric Laurent | 27a2fdf | 2010-09-10 17:44:44 -0700 | [diff] [blame] | 2220 | // Delegate volume control to effect in track effect chain if needed |
| 2221 | if (chain != 0 && chain->setVolume_l(&vl, &vr)) { |
| 2222 | // Do not ramp volume if volume is controlled by effect |
| 2223 | param = AudioMixer::VOLUME; |
| 2224 | track->mHasVolumeController = true; |
| 2225 | } else { |
| 2226 | // force no volume ramp when volume controller was just disabled or removed |
| 2227 | // from effect chain to avoid volume spike |
| 2228 | if (track->mHasVolumeController) { |
| 2229 | param = AudioMixer::VOLUME; |
| 2230 | } |
| 2231 | track->mHasVolumeController = false; |
| 2232 | } |
| 2233 | |
| 2234 | // Convert volumes from 8.24 to 4.12 format |
| 2235 | int16_t left, right, aux; |
Glenn Kasten | 0632bad | 2012-01-17 12:20:54 -0800 | [diff] [blame] | 2236 | // This additional clamping is needed in case chain->setVolume_l() overshot |
Eric Laurent | 27a2fdf | 2010-09-10 17:44:44 -0700 | [diff] [blame] | 2237 | uint32_t v_clamped = (vl + (1 << 11)) >> 12; |
| 2238 | if (v_clamped > MAX_GAIN_INT) v_clamped = MAX_GAIN_INT; |
| 2239 | left = int16_t(v_clamped); |
| 2240 | v_clamped = (vr + (1 << 11)) >> 12; |
| 2241 | if (v_clamped > MAX_GAIN_INT) v_clamped = MAX_GAIN_INT; |
| 2242 | right = int16_t(v_clamped); |
| 2243 | |
| 2244 | if (va > MAX_GAIN_INT) va = MAX_GAIN_INT; |
| 2245 | aux = int16_t(va); |
Eric Laurent | 65b6545 | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 2246 | |
Eric Laurent | 65b6545 | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 2247 | // XXX: these things DON'T need to be done each time |
Glenn Kasten | 68deb15 | 2011-12-19 15:06:39 -0800 | [diff] [blame] | 2248 | mAudioMixer->setBufferProvider(name, track); |
| 2249 | mAudioMixer->enable(name); |
Eric Laurent | 65b6545 | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 2250 | |
Glenn Kasten | 68deb15 | 2011-12-19 15:06:39 -0800 | [diff] [blame] | 2251 | mAudioMixer->setParameter(name, param, AudioMixer::VOLUME0, (void *)left); |
| 2252 | mAudioMixer->setParameter(name, param, AudioMixer::VOLUME1, (void *)right); |
| 2253 | mAudioMixer->setParameter(name, param, AudioMixer::AUXLEVEL, (void *)aux); |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 2254 | mAudioMixer->setParameter( |
Glenn Kasten | 68deb15 | 2011-12-19 15:06:39 -0800 | [diff] [blame] | 2255 | name, |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 2256 | AudioMixer::TRACK, |
Eric Laurent | 65b6545 | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 2257 | AudioMixer::FORMAT, (void *)track->format()); |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 2258 | mAudioMixer->setParameter( |
Glenn Kasten | 68deb15 | 2011-12-19 15:06:39 -0800 | [diff] [blame] | 2259 | name, |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 2260 | AudioMixer::TRACK, |
Jean-Michel Trivi | 5439223 | 2011-05-24 15:53:33 -0700 | [diff] [blame] | 2261 | AudioMixer::CHANNEL_MASK, (void *)track->channelMask()); |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 2262 | mAudioMixer->setParameter( |
Glenn Kasten | 68deb15 | 2011-12-19 15:06:39 -0800 | [diff] [blame] | 2263 | name, |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 2264 | AudioMixer::RESAMPLE, |
| 2265 | AudioMixer::SAMPLE_RATE, |
Eric Laurent | 65b6545 | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 2266 | (void *)(cblk->sampleRate)); |
| 2267 | mAudioMixer->setParameter( |
Glenn Kasten | 68deb15 | 2011-12-19 15:06:39 -0800 | [diff] [blame] | 2268 | name, |
Eric Laurent | 65b6545 | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 2269 | AudioMixer::TRACK, |
| 2270 | AudioMixer::MAIN_BUFFER, (void *)track->mainBuffer()); |
| 2271 | mAudioMixer->setParameter( |
Glenn Kasten | 68deb15 | 2011-12-19 15:06:39 -0800 | [diff] [blame] | 2272 | name, |
Eric Laurent | 65b6545 | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 2273 | AudioMixer::TRACK, |
| 2274 | AudioMixer::AUX_BUFFER, (void *)track->auxBuffer()); |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 2275 | |
| 2276 | // reset retry count |
| 2277 | track->mRetryCount = kMaxTrackRetries; |
Eric Laurent | 71c4496 | 2012-01-17 19:20:12 -0800 | [diff] [blame] | 2278 | // If one track is ready, set the mixer ready if: |
| 2279 | // - the mixer was not ready during previous round OR |
| 2280 | // - no other track is not ready |
| 2281 | if (mPrevMixerStatus != MIXER_TRACKS_READY || |
| 2282 | mixerStatus != MIXER_TRACKS_ENABLED) { |
| 2283 | mixerStatus = MIXER_TRACKS_READY; |
| 2284 | } |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 2285 | } else { |
Glenn Kasten | 68deb15 | 2011-12-19 15:06:39 -0800 | [diff] [blame] | 2286 | //ALOGV("track %d u=%08x, s=%08x [NOT READY] on thread %p", name, cblk->user, cblk->server, this); |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 2287 | if (track->isStopped()) { |
| 2288 | track->reset(); |
| 2289 | } |
| 2290 | if (track->isTerminated() || track->isStopped() || track->isPaused()) { |
| 2291 | // We have consumed all the buffers of this track. |
| 2292 | // Remove it from the list of active tracks. |
| 2293 | tracksToRemove->add(track); |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 2294 | } else { |
| 2295 | // No buffers for this track. Give it a few chances to |
| 2296 | // fill a buffer, then remove it from active list. |
| 2297 | if (--(track->mRetryCount) <= 0) { |
Glenn Kasten | 68deb15 | 2011-12-19 15:06:39 -0800 | [diff] [blame] | 2298 | ALOGV("BUFFER TIMEOUT: remove(%d) from active list on thread %p", name, this); |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 2299 | tracksToRemove->add(track); |
Eric Laurent | 4712baa | 2010-09-30 16:12:31 -0700 | [diff] [blame] | 2300 | // indicate to client process that the track was disabled because of underrun |
Eric Laurent | ae29b76 | 2011-03-28 18:37:07 -0700 | [diff] [blame] | 2301 | android_atomic_or(CBLK_DISABLED_ON, &cblk->flags); |
Eric Laurent | 71c4496 | 2012-01-17 19:20:12 -0800 | [diff] [blame] | 2302 | // If one track is not ready, mark the mixer also not ready if: |
| 2303 | // - the mixer was ready during previous round OR |
| 2304 | // - no other track is ready |
| 2305 | } else if (mPrevMixerStatus == MIXER_TRACKS_READY || |
| 2306 | mixerStatus != MIXER_TRACKS_READY) { |
Eric Laurent | 059b4be | 2009-11-09 23:32:22 -0800 | [diff] [blame] | 2307 | mixerStatus = MIXER_TRACKS_ENABLED; |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 2308 | } |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 2309 | } |
Glenn Kasten | 68deb15 | 2011-12-19 15:06:39 -0800 | [diff] [blame] | 2310 | mAudioMixer->disable(name); |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 2311 | } |
| 2312 | } |
| 2313 | |
| 2314 | // remove all the tracks that need to be... |
| 2315 | count = tracksToRemove->size(); |
Glenn Kasten | e80a4cc | 2011-12-15 09:51:17 -0800 | [diff] [blame] | 2316 | if (CC_UNLIKELY(count)) { |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 2317 | for (size_t i=0 ; i<count ; i++) { |
| 2318 | const sp<Track>& track = tracksToRemove->itemAt(i); |
| 2319 | mActiveTracks.remove(track); |
Eric Laurent | 65b6545 | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 2320 | if (track->mainBuffer() != mMixBuffer) { |
| 2321 | chain = getEffectChain_l(track->sessionId()); |
| 2322 | if (chain != 0) { |
Steve Block | 71f2cf1 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 2323 | ALOGV("stopping track on chain %p for session Id: %d", chain.get(), track->sessionId()); |
Eric Laurent | 90681d6 | 2011-05-09 12:09:06 -0700 | [diff] [blame] | 2324 | chain->decActiveTrackCnt(); |
Eric Laurent | 65b6545 | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 2325 | } |
| 2326 | } |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 2327 | if (track->isTerminated()) { |
Eric Laurent | 90681d6 | 2011-05-09 12:09:06 -0700 | [diff] [blame] | 2328 | removeTrack_l(track); |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 2329 | } |
| 2330 | } |
| 2331 | } |
| 2332 | |
Eric Laurent | 65b6545 | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 2333 | // mix buffer must be cleared if all tracks are connected to an |
| 2334 | // effect chain as in this case the mixer will not write to |
| 2335 | // mix buffer and track effects will accumulate into it |
| 2336 | if (mixedTracks != 0 && mixedTracks == tracksWithEffect) { |
| 2337 | memset(mMixBuffer, 0, mFrameCount * mChannelCount * sizeof(int16_t)); |
| 2338 | } |
| 2339 | |
Eric Laurent | 71c4496 | 2012-01-17 19:20:12 -0800 | [diff] [blame] | 2340 | mPrevMixerStatus = mixerStatus; |
Eric Laurent | 059b4be | 2009-11-09 23:32:22 -0800 | [diff] [blame] | 2341 | return mixerStatus; |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 2342 | } |
| 2343 | |
Glenn Kasten | bc1d77b | 2012-01-12 16:38:12 -0800 | [diff] [blame] | 2344 | void AudioFlinger::MixerThread::invalidateTracks(audio_stream_type_t streamType) |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 2345 | { |
Steve Block | 71f2cf1 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 2346 | ALOGV ("MixerThread::invalidateTracks() mixer %p, streamType %d, mTracks.size %d", |
Eric Laurent | 8ed6ed0 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 2347 | this, streamType, mTracks.size()); |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 2348 | Mutex::Autolock _l(mLock); |
Eric Laurent | 8ed6ed0 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 2349 | |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 2350 | size_t size = mTracks.size(); |
| 2351 | for (size_t i = 0; i < size; i++) { |
| 2352 | sp<Track> t = mTracks[i]; |
| 2353 | if (t->type() == streamType) { |
Eric Laurent | ae29b76 | 2011-03-28 18:37:07 -0700 | [diff] [blame] | 2354 | android_atomic_or(CBLK_INVALID_ON, &t->mCblk->flags); |
Eric Laurent | eb8f850d | 2010-05-14 03:26:45 -0700 | [diff] [blame] | 2355 | t->mCblk->cv.signal(); |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 2356 | } |
Eric Laurent | 53334cd | 2010-06-23 17:38:20 -0700 | [diff] [blame] | 2357 | } |
| 2358 | } |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 2359 | |
Glenn Kasten | bc1d77b | 2012-01-12 16:38:12 -0800 | [diff] [blame] | 2360 | void AudioFlinger::PlaybackThread::setStreamValid(audio_stream_type_t streamType, bool valid) |
Eric Laurent | 05ce094 | 2011-08-30 10:18:54 -0700 | [diff] [blame] | 2361 | { |
Steve Block | 71f2cf1 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 2362 | ALOGV ("PlaybackThread::setStreamValid() thread %p, streamType %d, valid %d", |
Eric Laurent | 05ce094 | 2011-08-30 10:18:54 -0700 | [diff] [blame] | 2363 | this, streamType, valid); |
| 2364 | Mutex::Autolock _l(mLock); |
| 2365 | |
| 2366 | mStreamTypes[streamType].valid = valid; |
| 2367 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2368 | |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 2369 | // getTrackName_l() must be called with ThreadBase::mLock held |
The Android Open Source Project | b2a3dd8 | 2009-03-09 11:52:12 -0700 | [diff] [blame] | 2370 | int AudioFlinger::MixerThread::getTrackName_l() |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2371 | { |
| 2372 | return mAudioMixer->getTrackName(); |
| 2373 | } |
| 2374 | |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 2375 | // deleteTrackName_l() must be called with ThreadBase::mLock held |
The Android Open Source Project | b2a3dd8 | 2009-03-09 11:52:12 -0700 | [diff] [blame] | 2376 | void AudioFlinger::MixerThread::deleteTrackName_l(int name) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2377 | { |
Steve Block | 71f2cf1 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 2378 | ALOGV("remove track (%d) and delete from mixer", name); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2379 | mAudioMixer->deleteTrackName(name); |
| 2380 | } |
| 2381 | |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 2382 | // checkForNewParameters_l() must be called with ThreadBase::mLock held |
| 2383 | bool AudioFlinger::MixerThread::checkForNewParameters_l() |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2384 | { |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 2385 | bool reconfig = false; |
| 2386 | |
Eric Laurent | 8fce46a | 2009-08-04 09:45:33 -0700 | [diff] [blame] | 2387 | while (!mNewParameters.isEmpty()) { |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 2388 | status_t status = NO_ERROR; |
Eric Laurent | 8fce46a | 2009-08-04 09:45:33 -0700 | [diff] [blame] | 2389 | String8 keyValuePair = mNewParameters[0]; |
| 2390 | AudioParameter param = AudioParameter(keyValuePair); |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 2391 | int value; |
Eric Laurent | 8fce46a | 2009-08-04 09:45:33 -0700 | [diff] [blame] | 2392 | |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 2393 | if (param.getInt(String8(AudioParameter::keySamplingRate), value) == NO_ERROR) { |
| 2394 | reconfig = true; |
| 2395 | } |
| 2396 | if (param.getInt(String8(AudioParameter::keyFormat), value) == NO_ERROR) { |
Glenn Kasten | 0a204ed | 2012-01-12 12:27:51 -0800 | [diff] [blame^] | 2397 | if ((audio_format_t) value != AUDIO_FORMAT_PCM_16_BIT) { |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 2398 | status = BAD_VALUE; |
| 2399 | } else { |
| 2400 | reconfig = true; |
| 2401 | } |
| 2402 | } |
| 2403 | if (param.getInt(String8(AudioParameter::keyChannels), value) == NO_ERROR) { |
Dima Zavin | 24fc2fb | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 2404 | if (value != AUDIO_CHANNEL_OUT_STEREO) { |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 2405 | status = BAD_VALUE; |
| 2406 | } else { |
| 2407 | reconfig = true; |
| 2408 | } |
| 2409 | } |
| 2410 | if (param.getInt(String8(AudioParameter::keyFrameCount), value) == NO_ERROR) { |
| 2411 | // do not accept frame count changes if tracks are open as the track buffer |
Glenn Kasten | e5fb263 | 2011-12-14 10:28:06 -0800 | [diff] [blame] | 2412 | // size depends on frame count and correct behavior would not be guaranteed |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 2413 | // if frame count is changed after track creation |
| 2414 | if (!mTracks.isEmpty()) { |
| 2415 | status = INVALID_OPERATION; |
| 2416 | } else { |
| 2417 | reconfig = true; |
| 2418 | } |
| 2419 | } |
Eric Laurent | 65b6545 | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 2420 | if (param.getInt(String8(AudioParameter::keyRouting), value) == NO_ERROR) { |
Gloria Wang | 9b3f152 | 2011-02-24 14:51:45 -0800 | [diff] [blame] | 2421 | // when changing the audio output device, call addBatteryData to notify |
| 2422 | // the change |
Eric Laurent | 90681d6 | 2011-05-09 12:09:06 -0700 | [diff] [blame] | 2423 | if ((int)mDevice != value) { |
Gloria Wang | 9b3f152 | 2011-02-24 14:51:45 -0800 | [diff] [blame] | 2424 | uint32_t params = 0; |
| 2425 | // check whether speaker is on |
Dima Zavin | 24fc2fb | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 2426 | if (value & AUDIO_DEVICE_OUT_SPEAKER) { |
Gloria Wang | 9b3f152 | 2011-02-24 14:51:45 -0800 | [diff] [blame] | 2427 | params |= IMediaPlayerService::kBatteryDataSpeakerOn; |
| 2428 | } |
| 2429 | |
| 2430 | int deviceWithoutSpeaker |
Dima Zavin | 24fc2fb | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 2431 | = AUDIO_DEVICE_OUT_ALL & ~AUDIO_DEVICE_OUT_SPEAKER; |
Gloria Wang | 9b3f152 | 2011-02-24 14:51:45 -0800 | [diff] [blame] | 2432 | // check if any other device (except speaker) is on |
| 2433 | if (value & deviceWithoutSpeaker ) { |
| 2434 | params |= IMediaPlayerService::kBatteryDataOtherAudioDeviceOn; |
| 2435 | } |
| 2436 | |
| 2437 | if (params != 0) { |
| 2438 | addBatteryData(params); |
| 2439 | } |
| 2440 | } |
| 2441 | |
Eric Laurent | 65b6545 | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 2442 | // forward device change to effects that have requested to be |
| 2443 | // aware of attached audio device. |
| 2444 | mDevice = (uint32_t)value; |
| 2445 | for (size_t i = 0; i < mEffectChains.size(); i++) { |
Eric Laurent | 76c40f7 | 2010-07-15 12:50:15 -0700 | [diff] [blame] | 2446 | mEffectChains[i]->setDevice_l(mDevice); |
Eric Laurent | 65b6545 | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 2447 | } |
| 2448 | } |
| 2449 | |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 2450 | if (status == NO_ERROR) { |
Dima Zavin | 31f18889 | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 2451 | status = mOutput->stream->common.set_parameters(&mOutput->stream->common, |
Dima Zavin | 24fc2fb | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 2452 | keyValuePair.string()); |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 2453 | if (!mStandby && status == INVALID_OPERATION) { |
Dima Zavin | 31f18889 | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 2454 | mOutput->stream->common.standby(&mOutput->stream->common); |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 2455 | mStandby = true; |
| 2456 | mBytesWritten = 0; |
Dima Zavin | 31f18889 | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 2457 | status = mOutput->stream->common.set_parameters(&mOutput->stream->common, |
Dima Zavin | 24fc2fb | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 2458 | keyValuePair.string()); |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 2459 | } |
| 2460 | if (status == NO_ERROR && reconfig) { |
| 2461 | delete mAudioMixer; |
| 2462 | readOutputParameters(); |
| 2463 | mAudioMixer = new AudioMixer(mFrameCount, mSampleRate); |
| 2464 | for (size_t i = 0; i < mTracks.size() ; i++) { |
| 2465 | int name = getTrackName_l(); |
| 2466 | if (name < 0) break; |
| 2467 | mTracks[i]->mName = name; |
Eric Laurent | 6f7e097 | 2009-08-10 08:15:12 -0700 | [diff] [blame] | 2468 | // limit track sample rate to 2 x new output sample rate |
| 2469 | if (mTracks[i]->mCblk->sampleRate > 2 * sampleRate()) { |
| 2470 | mTracks[i]->mCblk->sampleRate = 2 * sampleRate(); |
| 2471 | } |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 2472 | } |
Eric Laurent | 8fce46a | 2009-08-04 09:45:33 -0700 | [diff] [blame] | 2473 | sendConfigEvent_l(AudioSystem::OUTPUT_CONFIG_CHANGED); |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 2474 | } |
| 2475 | } |
Eric Laurent | 3fdb126 | 2009-11-07 00:01:32 -0800 | [diff] [blame] | 2476 | |
| 2477 | mNewParameters.removeAt(0); |
| 2478 | |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 2479 | mParamStatus = status; |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 2480 | mParamCond.signal(); |
Eric Laurent | 5f37be3 | 2011-09-13 11:40:21 -0700 | [diff] [blame] | 2481 | // wait for condition with time out in case the thread calling ThreadBase::setParameters() |
| 2482 | // already timed out waiting for the status and will never signal the condition. |
Glenn Kasten | cbfe2e1 | 2011-12-13 11:04:14 -0800 | [diff] [blame] | 2483 | mWaitWorkCV.waitRelative(mLock, kSetParametersTimeoutNs); |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 2484 | } |
| 2485 | return reconfig; |
| 2486 | } |
| 2487 | |
| 2488 | status_t AudioFlinger::MixerThread::dumpInternals(int fd, const Vector<String16>& args) |
| 2489 | { |
| 2490 | const size_t SIZE = 256; |
| 2491 | char buffer[SIZE]; |
| 2492 | String8 result; |
| 2493 | |
| 2494 | PlaybackThread::dumpInternals(fd, args); |
| 2495 | |
| 2496 | snprintf(buffer, SIZE, "AudioMixer tracks: %08x\n", mAudioMixer->trackNames()); |
| 2497 | result.append(buffer); |
| 2498 | write(fd, result.string(), result.size()); |
| 2499 | return NO_ERROR; |
| 2500 | } |
| 2501 | |
Eric Laurent | 059b4be | 2009-11-09 23:32:22 -0800 | [diff] [blame] | 2502 | uint32_t AudioFlinger::MixerThread::idleSleepTimeUs() |
| 2503 | { |
Eric Laurent | a54d7d3 | 2010-07-29 06:50:24 -0700 | [diff] [blame] | 2504 | return (uint32_t)(((mFrameCount * 1000) / mSampleRate) * 1000) / 2; |
Eric Laurent | 62443f5 | 2009-10-05 20:29:18 -0700 | [diff] [blame] | 2505 | } |
| 2506 | |
Eric Laurent | 8448a79 | 2010-08-18 18:13:17 -0700 | [diff] [blame] | 2507 | uint32_t AudioFlinger::MixerThread::suspendSleepTimeUs() |
| 2508 | { |
| 2509 | return (uint32_t)(((mFrameCount * 1000) / mSampleRate) * 1000); |
| 2510 | } |
| 2511 | |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 2512 | // ---------------------------------------------------------------------------- |
Dima Zavin | 31f18889 | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 2513 | AudioFlinger::DirectOutputThread::DirectOutputThread(const sp<AudioFlinger>& audioFlinger, AudioStreamOut* output, int id, uint32_t device) |
Eric Laurent | 65b6545 | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 2514 | : PlaybackThread(audioFlinger, output, id, device) |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 2515 | { |
Eric Laurent | 464d5b3 | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 2516 | mType = ThreadBase::DIRECT; |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 2517 | } |
| 2518 | |
| 2519 | AudioFlinger::DirectOutputThread::~DirectOutputThread() |
| 2520 | { |
| 2521 | } |
| 2522 | |
Eric Laurent | 65b6545 | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 2523 | static inline |
| 2524 | int32_t mul(int16_t in, int16_t v) |
| 2525 | { |
| 2526 | #if defined(__arm__) && !defined(__thumb__) |
| 2527 | int32_t out; |
| 2528 | asm( "smulbb %[out], %[in], %[v] \n" |
| 2529 | : [out]"=r"(out) |
| 2530 | : [in]"%r"(in), [v]"r"(v) |
| 2531 | : ); |
| 2532 | return out; |
| 2533 | #else |
| 2534 | return in * int32_t(v); |
| 2535 | #endif |
| 2536 | } |
| 2537 | |
| 2538 | void AudioFlinger::DirectOutputThread::applyVolume(uint16_t leftVol, uint16_t rightVol, bool ramp) |
| 2539 | { |
| 2540 | // Do not apply volume on compressed audio |
Dima Zavin | 24fc2fb | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 2541 | if (!audio_is_linear_pcm(mFormat)) { |
Eric Laurent | 65b6545 | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 2542 | return; |
| 2543 | } |
| 2544 | |
| 2545 | // convert to signed 16 bit before volume calculation |
Dima Zavin | 24fc2fb | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 2546 | if (mFormat == AUDIO_FORMAT_PCM_8_BIT) { |
Eric Laurent | 65b6545 | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 2547 | size_t count = mFrameCount * mChannelCount; |
| 2548 | uint8_t *src = (uint8_t *)mMixBuffer + count-1; |
| 2549 | int16_t *dst = mMixBuffer + count-1; |
| 2550 | while(count--) { |
| 2551 | *dst-- = (int16_t)(*src--^0x80) << 8; |
| 2552 | } |
| 2553 | } |
| 2554 | |
| 2555 | size_t frameCount = mFrameCount; |
| 2556 | int16_t *out = mMixBuffer; |
| 2557 | if (ramp) { |
| 2558 | if (mChannelCount == 1) { |
| 2559 | int32_t d = ((int32_t)leftVol - (int32_t)mLeftVolShort) << 16; |
| 2560 | int32_t vlInc = d / (int32_t)frameCount; |
| 2561 | int32_t vl = ((int32_t)mLeftVolShort << 16); |
| 2562 | do { |
| 2563 | out[0] = clamp16(mul(out[0], vl >> 16) >> 12); |
| 2564 | out++; |
| 2565 | vl += vlInc; |
| 2566 | } while (--frameCount); |
| 2567 | |
| 2568 | } else { |
| 2569 | int32_t d = ((int32_t)leftVol - (int32_t)mLeftVolShort) << 16; |
| 2570 | int32_t vlInc = d / (int32_t)frameCount; |
| 2571 | d = ((int32_t)rightVol - (int32_t)mRightVolShort) << 16; |
| 2572 | int32_t vrInc = d / (int32_t)frameCount; |
| 2573 | int32_t vl = ((int32_t)mLeftVolShort << 16); |
| 2574 | int32_t vr = ((int32_t)mRightVolShort << 16); |
| 2575 | do { |
| 2576 | out[0] = clamp16(mul(out[0], vl >> 16) >> 12); |
| 2577 | out[1] = clamp16(mul(out[1], vr >> 16) >> 12); |
| 2578 | out += 2; |
| 2579 | vl += vlInc; |
| 2580 | vr += vrInc; |
| 2581 | } while (--frameCount); |
| 2582 | } |
| 2583 | } else { |
| 2584 | if (mChannelCount == 1) { |
| 2585 | do { |
| 2586 | out[0] = clamp16(mul(out[0], leftVol) >> 12); |
| 2587 | out++; |
| 2588 | } while (--frameCount); |
| 2589 | } else { |
| 2590 | do { |
| 2591 | out[0] = clamp16(mul(out[0], leftVol) >> 12); |
| 2592 | out[1] = clamp16(mul(out[1], rightVol) >> 12); |
| 2593 | out += 2; |
| 2594 | } while (--frameCount); |
| 2595 | } |
| 2596 | } |
| 2597 | |
| 2598 | // convert back to unsigned 8 bit after volume calculation |
Dima Zavin | 24fc2fb | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 2599 | if (mFormat == AUDIO_FORMAT_PCM_8_BIT) { |
Eric Laurent | 65b6545 | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 2600 | size_t count = mFrameCount * mChannelCount; |
| 2601 | int16_t *src = mMixBuffer; |
| 2602 | uint8_t *dst = (uint8_t *)mMixBuffer; |
| 2603 | while(count--) { |
| 2604 | *dst++ = (uint8_t)(((int32_t)*src++ + (1<<7)) >> 8)^0x80; |
| 2605 | } |
| 2606 | } |
| 2607 | |
| 2608 | mLeftVolShort = leftVol; |
| 2609 | mRightVolShort = rightVol; |
| 2610 | } |
| 2611 | |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 2612 | bool AudioFlinger::DirectOutputThread::threadLoop() |
| 2613 | { |
Eric Laurent | 059b4be | 2009-11-09 23:32:22 -0800 | [diff] [blame] | 2614 | uint32_t mixerStatus = MIXER_IDLE; |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 2615 | sp<Track> trackToRemove; |
| 2616 | sp<Track> activeTrack; |
| 2617 | nsecs_t standbyTime = systemTime(); |
| 2618 | int8_t *curBuf; |
| 2619 | size_t mixBufferSize = mFrameCount*mFrameSize; |
Eric Laurent | 059b4be | 2009-11-09 23:32:22 -0800 | [diff] [blame] | 2620 | uint32_t activeSleepTime = activeSleepTimeUs(); |
| 2621 | uint32_t idleSleepTime = idleSleepTimeUs(); |
| 2622 | uint32_t sleepTime = idleSleepTime; |
Eric Laurent | ef9500f | 2010-03-11 14:47:00 -0800 | [diff] [blame] | 2623 | // use shorter standby delay as on normal output to release |
| 2624 | // hardware resources as soon as possible |
| 2625 | nsecs_t standbyDelay = microseconds(activeSleepTime*2); |
Eric Laurent | 059b4be | 2009-11-09 23:32:22 -0800 | [diff] [blame] | 2626 | |
Eric Laurent | 6dbdc40 | 2011-07-22 09:04:31 -0700 | [diff] [blame] | 2627 | acquireWakeLock(); |
| 2628 | |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 2629 | while (!exitPending()) |
| 2630 | { |
Eric Laurent | 65b6545 | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 2631 | bool rampVolume; |
| 2632 | uint16_t leftVol; |
| 2633 | uint16_t rightVol; |
| 2634 | Vector< sp<EffectChain> > effectChains; |
| 2635 | |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 2636 | processConfigEvents(); |
| 2637 | |
Eric Laurent | 059b4be | 2009-11-09 23:32:22 -0800 | [diff] [blame] | 2638 | mixerStatus = MIXER_IDLE; |
| 2639 | |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 2640 | { // scope for the mLock |
| 2641 | |
| 2642 | Mutex::Autolock _l(mLock); |
| 2643 | |
| 2644 | if (checkForNewParameters_l()) { |
| 2645 | mixBufferSize = mFrameCount*mFrameSize; |
Eric Laurent | 059b4be | 2009-11-09 23:32:22 -0800 | [diff] [blame] | 2646 | activeSleepTime = activeSleepTimeUs(); |
| 2647 | idleSleepTime = idleSleepTimeUs(); |
Eric Laurent | ef9500f | 2010-03-11 14:47:00 -0800 | [diff] [blame] | 2648 | standbyDelay = microseconds(activeSleepTime*2); |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 2649 | } |
| 2650 | |
| 2651 | // put audio hardware into standby after short delay |
Glenn Kasten | e80a4cc | 2011-12-15 09:51:17 -0800 | [diff] [blame] | 2652 | if (CC_UNLIKELY((!mActiveTracks.size() && systemTime() > standbyTime) || |
| 2653 | mSuspended)) { |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 2654 | // wait until we have something to do... |
| 2655 | if (!mStandby) { |
Steve Block | 71f2cf1 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 2656 | ALOGV("Audio hardware entering standby, mixer %p\n", this); |
Dima Zavin | 31f18889 | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 2657 | mOutput->stream->common.standby(&mOutput->stream->common); |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 2658 | mStandby = true; |
| 2659 | mBytesWritten = 0; |
| 2660 | } |
| 2661 | |
| 2662 | if (!mActiveTracks.size() && mConfigEvents.isEmpty()) { |
| 2663 | // we're about to wait, flush the binder command buffer |
| 2664 | IPCThreadState::self()->flushCommands(); |
| 2665 | |
| 2666 | if (exitPending()) break; |
| 2667 | |
Eric Laurent | 6dbdc40 | 2011-07-22 09:04:31 -0700 | [diff] [blame] | 2668 | releaseWakeLock_l(); |
Steve Block | 71f2cf1 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 2669 | ALOGV("DirectOutputThread %p TID %d going to sleep\n", this, gettid()); |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 2670 | mWaitWorkCV.wait(mLock); |
Steve Block | 71f2cf1 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 2671 | ALOGV("DirectOutputThread %p TID %d waking up in active mode\n", this, gettid()); |
Eric Laurent | 6dbdc40 | 2011-07-22 09:04:31 -0700 | [diff] [blame] | 2672 | acquireWakeLock_l(); |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 2673 | |
Glenn Kasten | b737dbb | 2012-01-13 15:54:24 -0800 | [diff] [blame] | 2674 | if (!mMasterMute) { |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 2675 | char value[PROPERTY_VALUE_MAX]; |
| 2676 | property_get("ro.audio.silent", value, "0"); |
| 2677 | if (atoi(value)) { |
Steve Block | 5baa3a6 | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 2678 | ALOGD("Silence is golden"); |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 2679 | setMasterMute(true); |
| 2680 | } |
| 2681 | } |
| 2682 | |
Eric Laurent | ef9500f | 2010-03-11 14:47:00 -0800 | [diff] [blame] | 2683 | standbyTime = systemTime() + standbyDelay; |
Eric Laurent | 059b4be | 2009-11-09 23:32:22 -0800 | [diff] [blame] | 2684 | sleepTime = idleSleepTime; |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 2685 | continue; |
| 2686 | } |
| 2687 | } |
| 2688 | |
Eric Laurent | 65b6545 | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 2689 | effectChains = mEffectChains; |
| 2690 | |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 2691 | // find out which tracks need to be processed |
| 2692 | if (mActiveTracks.size() != 0) { |
| 2693 | sp<Track> t = mActiveTracks[0].promote(); |
| 2694 | if (t == 0) continue; |
| 2695 | |
| 2696 | Track* const track = t.get(); |
| 2697 | audio_track_cblk_t* cblk = track->cblk(); |
| 2698 | |
| 2699 | // The first time a track is added we wait |
| 2700 | // for all its buffers to be filled before processing it |
Eric Laurent | 9a30fc1 | 2010-10-05 14:41:42 -0700 | [diff] [blame] | 2701 | if (cblk->framesReady() && track->isReady() && |
Eric Laurent | 380558b | 2010-04-09 06:11:48 -0700 | [diff] [blame] | 2702 | !track->isPaused() && !track->isTerminated()) |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 2703 | { |
Steve Block | 71f2cf1 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 2704 | //ALOGV("track %d u=%08x, s=%08x [OK]", track->name(), cblk->user, cblk->server); |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 2705 | |
Eric Laurent | 65b6545 | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 2706 | if (track->mFillingUpStatus == Track::FS_FILLED) { |
| 2707 | track->mFillingUpStatus = Track::FS_ACTIVE; |
| 2708 | mLeftVolFloat = mRightVolFloat = 0; |
| 2709 | mLeftVolShort = mRightVolShort = 0; |
| 2710 | if (track->mState == TrackBase::RESUMING) { |
| 2711 | track->mState = TrackBase::ACTIVE; |
| 2712 | rampVolume = true; |
| 2713 | } |
| 2714 | } else if (cblk->server != 0) { |
| 2715 | // If the track is stopped before the first frame was mixed, |
| 2716 | // do not apply ramp |
| 2717 | rampVolume = true; |
| 2718 | } |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 2719 | // compute volume for this track |
| 2720 | float left, right; |
| 2721 | if (track->isMuted() || mMasterMute || track->isPausing() || |
| 2722 | mStreamTypes[track->type()].mute) { |
| 2723 | left = right = 0; |
| 2724 | if (track->isPausing()) { |
| 2725 | track->setPaused(); |
| 2726 | } |
| 2727 | } else { |
| 2728 | float typeVolume = mStreamTypes[track->type()].volume; |
| 2729 | float v = mMasterVolume * typeVolume; |
Glenn Kasten | 0632bad | 2012-01-17 12:20:54 -0800 | [diff] [blame] | 2730 | uint32_t vlr = cblk->volumeLR; |
| 2731 | float v_clamped = v * (vlr & 0xFFFF); |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 2732 | if (v_clamped > MAX_GAIN) v_clamped = MAX_GAIN; |
| 2733 | left = v_clamped/MAX_GAIN; |
Glenn Kasten | 0632bad | 2012-01-17 12:20:54 -0800 | [diff] [blame] | 2734 | v_clamped = v * (vlr >> 16); |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 2735 | if (v_clamped > MAX_GAIN) v_clamped = MAX_GAIN; |
| 2736 | right = v_clamped/MAX_GAIN; |
| 2737 | } |
| 2738 | |
Eric Laurent | 65b6545 | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 2739 | if (left != mLeftVolFloat || right != mRightVolFloat) { |
| 2740 | mLeftVolFloat = left; |
| 2741 | mRightVolFloat = right; |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 2742 | |
Eric Laurent | 65b6545 | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 2743 | // If audio HAL implements volume control, |
| 2744 | // force software volume to nominal value |
Dima Zavin | 31f18889 | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 2745 | if (mOutput->stream->set_volume(mOutput->stream, left, right) == NO_ERROR) { |
Eric Laurent | 65b6545 | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 2746 | left = 1.0f; |
| 2747 | right = 1.0f; |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 2748 | } |
Eric Laurent | 65b6545 | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 2749 | |
| 2750 | // Convert volumes from float to 8.24 |
| 2751 | uint32_t vl = (uint32_t)(left * (1 << 24)); |
| 2752 | uint32_t vr = (uint32_t)(right * (1 << 24)); |
| 2753 | |
| 2754 | // Delegate volume control to effect in track effect chain if needed |
| 2755 | // only one effect chain can be present on DirectOutputThread, so if |
| 2756 | // there is one, the track is connected to it |
| 2757 | if (!effectChains.isEmpty()) { |
Eric Laurent | 27a2fdf | 2010-09-10 17:44:44 -0700 | [diff] [blame] | 2758 | // Do not ramp volume if volume is controlled by effect |
Eric Laurent | 76c40f7 | 2010-07-15 12:50:15 -0700 | [diff] [blame] | 2759 | if(effectChains[0]->setVolume_l(&vl, &vr)) { |
Eric Laurent | 65b6545 | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 2760 | rampVolume = false; |
| 2761 | } |
| 2762 | } |
| 2763 | |
| 2764 | // Convert volumes from 8.24 to 4.12 format |
| 2765 | uint32_t v_clamped = (vl + (1 << 11)) >> 12; |
| 2766 | if (v_clamped > MAX_GAIN_INT) v_clamped = MAX_GAIN_INT; |
| 2767 | leftVol = (uint16_t)v_clamped; |
| 2768 | v_clamped = (vr + (1 << 11)) >> 12; |
| 2769 | if (v_clamped > MAX_GAIN_INT) v_clamped = MAX_GAIN_INT; |
| 2770 | rightVol = (uint16_t)v_clamped; |
| 2771 | } else { |
| 2772 | leftVol = mLeftVolShort; |
| 2773 | rightVol = mRightVolShort; |
| 2774 | rampVolume = false; |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 2775 | } |
| 2776 | |
| 2777 | // reset retry count |
Eric Laurent | ef9500f | 2010-03-11 14:47:00 -0800 | [diff] [blame] | 2778 | track->mRetryCount = kMaxTrackRetriesDirect; |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 2779 | activeTrack = t; |
Eric Laurent | 059b4be | 2009-11-09 23:32:22 -0800 | [diff] [blame] | 2780 | mixerStatus = MIXER_TRACKS_READY; |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 2781 | } else { |
Steve Block | 71f2cf1 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 2782 | //ALOGV("track %d u=%08x, s=%08x [NOT READY]", track->name(), cblk->user, cblk->server); |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 2783 | if (track->isStopped()) { |
| 2784 | track->reset(); |
| 2785 | } |
| 2786 | if (track->isTerminated() || track->isStopped() || track->isPaused()) { |
| 2787 | // We have consumed all the buffers of this track. |
| 2788 | // Remove it from the list of active tracks. |
| 2789 | trackToRemove = track; |
| 2790 | } else { |
| 2791 | // No buffers for this track. Give it a few chances to |
| 2792 | // fill a buffer, then remove it from active list. |
| 2793 | if (--(track->mRetryCount) <= 0) { |
Steve Block | 71f2cf1 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 2794 | ALOGV("BUFFER TIMEOUT: remove(%d) from active list", track->name()); |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 2795 | trackToRemove = track; |
Eric Laurent | 059b4be | 2009-11-09 23:32:22 -0800 | [diff] [blame] | 2796 | } else { |
| 2797 | mixerStatus = MIXER_TRACKS_ENABLED; |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 2798 | } |
Eric Laurent | 059b4be | 2009-11-09 23:32:22 -0800 | [diff] [blame] | 2799 | } |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 2800 | } |
| 2801 | } |
| 2802 | |
| 2803 | // remove all the tracks that need to be... |
Glenn Kasten | e80a4cc | 2011-12-15 09:51:17 -0800 | [diff] [blame] | 2804 | if (CC_UNLIKELY(trackToRemove != 0)) { |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 2805 | mActiveTracks.remove(trackToRemove); |
Eric Laurent | 65b6545 | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 2806 | if (!effectChains.isEmpty()) { |
Steve Block | 71f2cf1 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 2807 | ALOGV("stopping track on chain %p for session Id: %d", effectChains[0].get(), |
Eric Laurent | 8ed6ed0 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 2808 | trackToRemove->sessionId()); |
Eric Laurent | 90681d6 | 2011-05-09 12:09:06 -0700 | [diff] [blame] | 2809 | effectChains[0]->decActiveTrackCnt(); |
Eric Laurent | 65b6545 | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 2810 | } |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 2811 | if (trackToRemove->isTerminated()) { |
Eric Laurent | 90681d6 | 2011-05-09 12:09:06 -0700 | [diff] [blame] | 2812 | removeTrack_l(trackToRemove); |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 2813 | } |
| 2814 | } |
Eric Laurent | 65b6545 | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 2815 | |
Eric Laurent | 8ed6ed0 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 2816 | lockEffectChains_l(effectChains); |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 2817 | } |
| 2818 | |
Glenn Kasten | e80a4cc | 2011-12-15 09:51:17 -0800 | [diff] [blame] | 2819 | if (CC_LIKELY(mixerStatus == MIXER_TRACKS_READY)) { |
Eric Laurent | f69a3f8 | 2009-09-22 00:35:48 -0700 | [diff] [blame] | 2820 | AudioBufferProvider::Buffer buffer; |
| 2821 | size_t frameCount = mFrameCount; |
| 2822 | curBuf = (int8_t *)mMixBuffer; |
| 2823 | // output audio to hardware |
Eric Laurent | 65b6545 | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 2824 | while (frameCount) { |
Eric Laurent | f69a3f8 | 2009-09-22 00:35:48 -0700 | [diff] [blame] | 2825 | buffer.frameCount = frameCount; |
| 2826 | activeTrack->getNextBuffer(&buffer); |
Glenn Kasten | e80a4cc | 2011-12-15 09:51:17 -0800 | [diff] [blame] | 2827 | if (CC_UNLIKELY(buffer.raw == NULL)) { |
Eric Laurent | f69a3f8 | 2009-09-22 00:35:48 -0700 | [diff] [blame] | 2828 | memset(curBuf, 0, frameCount * mFrameSize); |
| 2829 | break; |
| 2830 | } |
| 2831 | memcpy(curBuf, buffer.raw, buffer.frameCount * mFrameSize); |
| 2832 | frameCount -= buffer.frameCount; |
| 2833 | curBuf += buffer.frameCount * mFrameSize; |
| 2834 | activeTrack->releaseBuffer(&buffer); |
| 2835 | } |
| 2836 | sleepTime = 0; |
Eric Laurent | ef9500f | 2010-03-11 14:47:00 -0800 | [diff] [blame] | 2837 | standbyTime = systemTime() + standbyDelay; |
Eric Laurent | 96c08a6 | 2009-09-07 08:38:38 -0700 | [diff] [blame] | 2838 | } else { |
Eric Laurent | 62443f5 | 2009-10-05 20:29:18 -0700 | [diff] [blame] | 2839 | if (sleepTime == 0) { |
Eric Laurent | 059b4be | 2009-11-09 23:32:22 -0800 | [diff] [blame] | 2840 | if (mixerStatus == MIXER_TRACKS_ENABLED) { |
| 2841 | sleepTime = activeSleepTime; |
| 2842 | } else { |
| 2843 | sleepTime = idleSleepTime; |
| 2844 | } |
Dima Zavin | 24fc2fb | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 2845 | } else if (mBytesWritten != 0 && audio_is_linear_pcm(mFormat)) { |
Eric Laurent | f69a3f8 | 2009-09-22 00:35:48 -0700 | [diff] [blame] | 2846 | memset (mMixBuffer, 0, mFrameCount * mFrameSize); |
Eric Laurent | 96c08a6 | 2009-09-07 08:38:38 -0700 | [diff] [blame] | 2847 | sleepTime = 0; |
Eric Laurent | 96c08a6 | 2009-09-07 08:38:38 -0700 | [diff] [blame] | 2848 | } |
Eric Laurent | f69a3f8 | 2009-09-22 00:35:48 -0700 | [diff] [blame] | 2849 | } |
Eric Laurent | 96c08a6 | 2009-09-07 08:38:38 -0700 | [diff] [blame] | 2850 | |
Eric Laurent | f69a3f8 | 2009-09-22 00:35:48 -0700 | [diff] [blame] | 2851 | if (mSuspended) { |
Eric Laurent | 8448a79 | 2010-08-18 18:13:17 -0700 | [diff] [blame] | 2852 | sleepTime = suspendSleepTimeUs(); |
Eric Laurent | f69a3f8 | 2009-09-22 00:35:48 -0700 | [diff] [blame] | 2853 | } |
| 2854 | // sleepTime == 0 means we must write to audio hardware |
| 2855 | if (sleepTime == 0) { |
Eric Laurent | 65b6545 | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 2856 | if (mixerStatus == MIXER_TRACKS_READY) { |
| 2857 | applyVolume(leftVol, rightVol, rampVolume); |
| 2858 | } |
| 2859 | for (size_t i = 0; i < effectChains.size(); i ++) { |
| 2860 | effectChains[i]->process_l(); |
| 2861 | } |
Eric Laurent | 8ed6ed0 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 2862 | unlockEffectChains(effectChains); |
Eric Laurent | 65b6545 | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 2863 | |
Eric Laurent | f69a3f8 | 2009-09-22 00:35:48 -0700 | [diff] [blame] | 2864 | mLastWriteTime = systemTime(); |
| 2865 | mInWrite = true; |
Eric Laurent | 0986e79 | 2010-01-19 17:37:09 -0800 | [diff] [blame] | 2866 | mBytesWritten += mixBufferSize; |
Dima Zavin | 31f18889 | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 2867 | int bytesWritten = (int)mOutput->stream->write(mOutput->stream, mMixBuffer, mixBufferSize); |
Eric Laurent | 0986e79 | 2010-01-19 17:37:09 -0800 | [diff] [blame] | 2868 | if (bytesWritten < 0) mBytesWritten -= mixBufferSize; |
Eric Laurent | f69a3f8 | 2009-09-22 00:35:48 -0700 | [diff] [blame] | 2869 | mNumWrites++; |
| 2870 | mInWrite = false; |
| 2871 | mStandby = false; |
| 2872 | } else { |
Eric Laurent | 8ed6ed0 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 2873 | unlockEffectChains(effectChains); |
Eric Laurent | f69a3f8 | 2009-09-22 00:35:48 -0700 | [diff] [blame] | 2874 | usleep(sleepTime); |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 2875 | } |
| 2876 | |
| 2877 | // finally let go of removed track, without the lock held |
| 2878 | // since we can't guarantee the destructors won't acquire that |
| 2879 | // same lock. |
| 2880 | trackToRemove.clear(); |
| 2881 | activeTrack.clear(); |
Eric Laurent | 65b6545 | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 2882 | |
| 2883 | // Effect chains will be actually deleted here if they were removed from |
| 2884 | // mEffectChains list during mixing or effects processing |
| 2885 | effectChains.clear(); |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 2886 | } |
| 2887 | |
| 2888 | if (!mStandby) { |
Dima Zavin | 31f18889 | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 2889 | mOutput->stream->common.standby(&mOutput->stream->common); |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 2890 | } |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 2891 | |
Eric Laurent | 6dbdc40 | 2011-07-22 09:04:31 -0700 | [diff] [blame] | 2892 | releaseWakeLock(); |
| 2893 | |
Steve Block | 71f2cf1 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 2894 | ALOGV("DirectOutputThread %p exiting", this); |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 2895 | return false; |
| 2896 | } |
| 2897 | |
| 2898 | // getTrackName_l() must be called with ThreadBase::mLock held |
| 2899 | int AudioFlinger::DirectOutputThread::getTrackName_l() |
| 2900 | { |
| 2901 | return 0; |
| 2902 | } |
| 2903 | |
| 2904 | // deleteTrackName_l() must be called with ThreadBase::mLock held |
| 2905 | void AudioFlinger::DirectOutputThread::deleteTrackName_l(int name) |
| 2906 | { |
| 2907 | } |
| 2908 | |
| 2909 | // checkForNewParameters_l() must be called with ThreadBase::mLock held |
| 2910 | bool AudioFlinger::DirectOutputThread::checkForNewParameters_l() |
| 2911 | { |
| 2912 | bool reconfig = false; |
| 2913 | |
Eric Laurent | 8fce46a | 2009-08-04 09:45:33 -0700 | [diff] [blame] | 2914 | while (!mNewParameters.isEmpty()) { |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 2915 | status_t status = NO_ERROR; |
Eric Laurent | 8fce46a | 2009-08-04 09:45:33 -0700 | [diff] [blame] | 2916 | String8 keyValuePair = mNewParameters[0]; |
| 2917 | AudioParameter param = AudioParameter(keyValuePair); |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 2918 | int value; |
Eric Laurent | 8fce46a | 2009-08-04 09:45:33 -0700 | [diff] [blame] | 2919 | |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 2920 | if (param.getInt(String8(AudioParameter::keyFrameCount), value) == NO_ERROR) { |
| 2921 | // do not accept frame count changes if tracks are open as the track buffer |
| 2922 | // size depends on frame count and correct behavior would not be garantied |
| 2923 | // if frame count is changed after track creation |
| 2924 | if (!mTracks.isEmpty()) { |
| 2925 | status = INVALID_OPERATION; |
| 2926 | } else { |
| 2927 | reconfig = true; |
| 2928 | } |
| 2929 | } |
| 2930 | if (status == NO_ERROR) { |
Dima Zavin | 31f18889 | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 2931 | status = mOutput->stream->common.set_parameters(&mOutput->stream->common, |
Dima Zavin | 24fc2fb | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 2932 | keyValuePair.string()); |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 2933 | if (!mStandby && status == INVALID_OPERATION) { |
Dima Zavin | 31f18889 | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 2934 | mOutput->stream->common.standby(&mOutput->stream->common); |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 2935 | mStandby = true; |
| 2936 | mBytesWritten = 0; |
Dima Zavin | 31f18889 | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 2937 | status = mOutput->stream->common.set_parameters(&mOutput->stream->common, |
Dima Zavin | 24fc2fb | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 2938 | keyValuePair.string()); |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 2939 | } |
| 2940 | if (status == NO_ERROR && reconfig) { |
| 2941 | readOutputParameters(); |
Eric Laurent | 8fce46a | 2009-08-04 09:45:33 -0700 | [diff] [blame] | 2942 | sendConfigEvent_l(AudioSystem::OUTPUT_CONFIG_CHANGED); |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 2943 | } |
| 2944 | } |
Eric Laurent | 3fdb126 | 2009-11-07 00:01:32 -0800 | [diff] [blame] | 2945 | |
| 2946 | mNewParameters.removeAt(0); |
| 2947 | |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 2948 | mParamStatus = status; |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 2949 | mParamCond.signal(); |
Eric Laurent | 5f37be3 | 2011-09-13 11:40:21 -0700 | [diff] [blame] | 2950 | // wait for condition with time out in case the thread calling ThreadBase::setParameters() |
| 2951 | // already timed out waiting for the status and will never signal the condition. |
Glenn Kasten | cbfe2e1 | 2011-12-13 11:04:14 -0800 | [diff] [blame] | 2952 | mWaitWorkCV.waitRelative(mLock, kSetParametersTimeoutNs); |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 2953 | } |
| 2954 | return reconfig; |
The Android Open Source Project | f1e484a | 2009-01-22 00:13:42 -0800 | [diff] [blame] | 2955 | } |
| 2956 | |
Eric Laurent | 059b4be | 2009-11-09 23:32:22 -0800 | [diff] [blame] | 2957 | uint32_t AudioFlinger::DirectOutputThread::activeSleepTimeUs() |
Eric Laurent | 62443f5 | 2009-10-05 20:29:18 -0700 | [diff] [blame] | 2958 | { |
| 2959 | uint32_t time; |
Dima Zavin | 24fc2fb | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 2960 | if (audio_is_linear_pcm(mFormat)) { |
Eric Laurent | 4433169 | 2011-12-05 09:47:19 -0800 | [diff] [blame] | 2961 | time = PlaybackThread::activeSleepTimeUs(); |
Eric Laurent | 059b4be | 2009-11-09 23:32:22 -0800 | [diff] [blame] | 2962 | } else { |
| 2963 | time = 10000; |
| 2964 | } |
| 2965 | return time; |
| 2966 | } |
| 2967 | |
| 2968 | uint32_t AudioFlinger::DirectOutputThread::idleSleepTimeUs() |
| 2969 | { |
| 2970 | uint32_t time; |
Dima Zavin | 24fc2fb | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 2971 | if (audio_is_linear_pcm(mFormat)) { |
Eric Laurent | a54d7d3 | 2010-07-29 06:50:24 -0700 | [diff] [blame] | 2972 | time = (uint32_t)(((mFrameCount * 1000) / mSampleRate) * 1000) / 2; |
Eric Laurent | 62443f5 | 2009-10-05 20:29:18 -0700 | [diff] [blame] | 2973 | } else { |
| 2974 | time = 10000; |
| 2975 | } |
| 2976 | return time; |
| 2977 | } |
| 2978 | |
Eric Laurent | 8448a79 | 2010-08-18 18:13:17 -0700 | [diff] [blame] | 2979 | uint32_t AudioFlinger::DirectOutputThread::suspendSleepTimeUs() |
| 2980 | { |
| 2981 | uint32_t time; |
Dima Zavin | 24fc2fb | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 2982 | if (audio_is_linear_pcm(mFormat)) { |
Eric Laurent | 8448a79 | 2010-08-18 18:13:17 -0700 | [diff] [blame] | 2983 | time = (uint32_t)(((mFrameCount * 1000) / mSampleRate) * 1000); |
| 2984 | } else { |
| 2985 | time = 10000; |
| 2986 | } |
| 2987 | return time; |
| 2988 | } |
| 2989 | |
| 2990 | |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 2991 | // ---------------------------------------------------------------------------- |
| 2992 | |
Eric Laurent | 49f02be | 2009-11-19 09:00:56 -0800 | [diff] [blame] | 2993 | AudioFlinger::DuplicatingThread::DuplicatingThread(const sp<AudioFlinger>& audioFlinger, AudioFlinger::MixerThread* mainThread, int id) |
Eric Laurent | 65b6545 | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 2994 | : MixerThread(audioFlinger, mainThread->getOutput(), id, mainThread->device()), mWaitTimeMs(UINT_MAX) |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 2995 | { |
Eric Laurent | 464d5b3 | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 2996 | mType = ThreadBase::DUPLICATING; |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 2997 | addOutputTrack(mainThread); |
| 2998 | } |
| 2999 | |
| 3000 | AudioFlinger::DuplicatingThread::~DuplicatingThread() |
| 3001 | { |
Eric Laurent | 0a08029 | 2009-12-07 10:53:10 -0800 | [diff] [blame] | 3002 | for (size_t i = 0; i < mOutputTracks.size(); i++) { |
| 3003 | mOutputTracks[i]->destroy(); |
| 3004 | } |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 3005 | mOutputTracks.clear(); |
| 3006 | } |
| 3007 | |
| 3008 | bool AudioFlinger::DuplicatingThread::threadLoop() |
| 3009 | { |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 3010 | Vector< sp<Track> > tracksToRemove; |
Eric Laurent | 059b4be | 2009-11-09 23:32:22 -0800 | [diff] [blame] | 3011 | uint32_t mixerStatus = MIXER_IDLE; |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 3012 | nsecs_t standbyTime = systemTime(); |
| 3013 | size_t mixBufferSize = mFrameCount*mFrameSize; |
| 3014 | SortedVector< sp<OutputTrack> > outputTracks; |
Eric Laurent | 62443f5 | 2009-10-05 20:29:18 -0700 | [diff] [blame] | 3015 | uint32_t writeFrames = 0; |
Eric Laurent | 059b4be | 2009-11-09 23:32:22 -0800 | [diff] [blame] | 3016 | uint32_t activeSleepTime = activeSleepTimeUs(); |
| 3017 | uint32_t idleSleepTime = idleSleepTimeUs(); |
| 3018 | uint32_t sleepTime = idleSleepTime; |
Eric Laurent | 65b6545 | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 3019 | Vector< sp<EffectChain> > effectChains; |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 3020 | |
Eric Laurent | 6dbdc40 | 2011-07-22 09:04:31 -0700 | [diff] [blame] | 3021 | acquireWakeLock(); |
| 3022 | |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 3023 | while (!exitPending()) |
| 3024 | { |
| 3025 | processConfigEvents(); |
| 3026 | |
Eric Laurent | 059b4be | 2009-11-09 23:32:22 -0800 | [diff] [blame] | 3027 | mixerStatus = MIXER_IDLE; |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 3028 | { // scope for the mLock |
| 3029 | |
| 3030 | Mutex::Autolock _l(mLock); |
| 3031 | |
| 3032 | if (checkForNewParameters_l()) { |
| 3033 | mixBufferSize = mFrameCount*mFrameSize; |
Eric Laurent | 8ac9f8d | 2009-12-18 05:47:48 -0800 | [diff] [blame] | 3034 | updateWaitTime(); |
Eric Laurent | 059b4be | 2009-11-09 23:32:22 -0800 | [diff] [blame] | 3035 | activeSleepTime = activeSleepTimeUs(); |
| 3036 | idleSleepTime = idleSleepTimeUs(); |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 3037 | } |
| 3038 | |
| 3039 | const SortedVector< wp<Track> >& activeTracks = mActiveTracks; |
| 3040 | |
| 3041 | for (size_t i = 0; i < mOutputTracks.size(); i++) { |
| 3042 | outputTracks.add(mOutputTracks[i]); |
| 3043 | } |
| 3044 | |
| 3045 | // put audio hardware into standby after short delay |
Glenn Kasten | e80a4cc | 2011-12-15 09:51:17 -0800 | [diff] [blame] | 3046 | if (CC_UNLIKELY((!activeTracks.size() && systemTime() > standbyTime) || |
| 3047 | mSuspended)) { |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 3048 | if (!mStandby) { |
| 3049 | for (size_t i = 0; i < outputTracks.size(); i++) { |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 3050 | outputTracks[i]->stop(); |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 3051 | } |
| 3052 | mStandby = true; |
| 3053 | mBytesWritten = 0; |
| 3054 | } |
| 3055 | |
| 3056 | if (!activeTracks.size() && mConfigEvents.isEmpty()) { |
| 3057 | // we're about to wait, flush the binder command buffer |
| 3058 | IPCThreadState::self()->flushCommands(); |
| 3059 | outputTracks.clear(); |
| 3060 | |
| 3061 | if (exitPending()) break; |
| 3062 | |
Eric Laurent | 6dbdc40 | 2011-07-22 09:04:31 -0700 | [diff] [blame] | 3063 | releaseWakeLock_l(); |
Steve Block | 71f2cf1 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 3064 | ALOGV("DuplicatingThread %p TID %d going to sleep\n", this, gettid()); |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 3065 | mWaitWorkCV.wait(mLock); |
Steve Block | 71f2cf1 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 3066 | ALOGV("DuplicatingThread %p TID %d waking up\n", this, gettid()); |
Eric Laurent | 6dbdc40 | 2011-07-22 09:04:31 -0700 | [diff] [blame] | 3067 | acquireWakeLock_l(); |
| 3068 | |
Eric Laurent | 71c4496 | 2012-01-17 19:20:12 -0800 | [diff] [blame] | 3069 | mPrevMixerStatus = MIXER_IDLE; |
Glenn Kasten | b737dbb | 2012-01-13 15:54:24 -0800 | [diff] [blame] | 3070 | if (!mMasterMute) { |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 3071 | char value[PROPERTY_VALUE_MAX]; |
| 3072 | property_get("ro.audio.silent", value, "0"); |
| 3073 | if (atoi(value)) { |
Steve Block | 5baa3a6 | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 3074 | ALOGD("Silence is golden"); |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 3075 | setMasterMute(true); |
| 3076 | } |
| 3077 | } |
| 3078 | |
| 3079 | standbyTime = systemTime() + kStandbyTimeInNsecs; |
Eric Laurent | 059b4be | 2009-11-09 23:32:22 -0800 | [diff] [blame] | 3080 | sleepTime = idleSleepTime; |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 3081 | continue; |
| 3082 | } |
| 3083 | } |
| 3084 | |
Eric Laurent | 059b4be | 2009-11-09 23:32:22 -0800 | [diff] [blame] | 3085 | mixerStatus = prepareTracks_l(activeTracks, &tracksToRemove); |
Eric Laurent | 65b6545 | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 3086 | |
| 3087 | // prevent any changes in effect chain list and in each effect chain |
| 3088 | // during mixing and effect process as the audio buffers could be deleted |
| 3089 | // or modified if an effect is created or deleted |
Eric Laurent | 8ed6ed0 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 3090 | lockEffectChains_l(effectChains); |
Eric Laurent | f69a3f8 | 2009-09-22 00:35:48 -0700 | [diff] [blame] | 3091 | } |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 3092 | |
Glenn Kasten | e80a4cc | 2011-12-15 09:51:17 -0800 | [diff] [blame] | 3093 | if (CC_LIKELY(mixerStatus == MIXER_TRACKS_READY)) { |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 3094 | // mix buffers... |
Eric Laurent | 8ac9f8d | 2009-12-18 05:47:48 -0800 | [diff] [blame] | 3095 | if (outputsReady(outputTracks)) { |
Eric Laurent | 65b6545 | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 3096 | mAudioMixer->process(); |
Eric Laurent | 8ac9f8d | 2009-12-18 05:47:48 -0800 | [diff] [blame] | 3097 | } else { |
Eric Laurent | 65b6545 | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 3098 | memset(mMixBuffer, 0, mixBufferSize); |
Eric Laurent | 8ac9f8d | 2009-12-18 05:47:48 -0800 | [diff] [blame] | 3099 | } |
Eric Laurent | f69a3f8 | 2009-09-22 00:35:48 -0700 | [diff] [blame] | 3100 | sleepTime = 0; |
Eric Laurent | 62443f5 | 2009-10-05 20:29:18 -0700 | [diff] [blame] | 3101 | writeFrames = mFrameCount; |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 3102 | } else { |
Eric Laurent | 62443f5 | 2009-10-05 20:29:18 -0700 | [diff] [blame] | 3103 | if (sleepTime == 0) { |
Eric Laurent | 059b4be | 2009-11-09 23:32:22 -0800 | [diff] [blame] | 3104 | if (mixerStatus == MIXER_TRACKS_ENABLED) { |
| 3105 | sleepTime = activeSleepTime; |
| 3106 | } else { |
| 3107 | sleepTime = idleSleepTime; |
| 3108 | } |
Eric Laurent | 62443f5 | 2009-10-05 20:29:18 -0700 | [diff] [blame] | 3109 | } else if (mBytesWritten != 0) { |
| 3110 | // flush remaining overflow buffers in output tracks |
| 3111 | for (size_t i = 0; i < outputTracks.size(); i++) { |
| 3112 | if (outputTracks[i]->isActive()) { |
| 3113 | sleepTime = 0; |
| 3114 | writeFrames = 0; |
Eric Laurent | 65b6545 | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 3115 | memset(mMixBuffer, 0, mixBufferSize); |
Eric Laurent | 62443f5 | 2009-10-05 20:29:18 -0700 | [diff] [blame] | 3116 | break; |
| 3117 | } |
| 3118 | } |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 3119 | } |
| 3120 | } |
Eric Laurent | f69a3f8 | 2009-09-22 00:35:48 -0700 | [diff] [blame] | 3121 | |
| 3122 | if (mSuspended) { |
Eric Laurent | 8448a79 | 2010-08-18 18:13:17 -0700 | [diff] [blame] | 3123 | sleepTime = suspendSleepTimeUs(); |
Eric Laurent | f69a3f8 | 2009-09-22 00:35:48 -0700 | [diff] [blame] | 3124 | } |
| 3125 | // sleepTime == 0 means we must write to audio hardware |
| 3126 | if (sleepTime == 0) { |
Eric Laurent | 65b6545 | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 3127 | for (size_t i = 0; i < effectChains.size(); i ++) { |
| 3128 | effectChains[i]->process_l(); |
| 3129 | } |
| 3130 | // enable changes in effect chain |
Eric Laurent | 8ed6ed0 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 3131 | unlockEffectChains(effectChains); |
Eric Laurent | 65b6545 | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 3132 | |
Eric Laurent | 62443f5 | 2009-10-05 20:29:18 -0700 | [diff] [blame] | 3133 | standbyTime = systemTime() + kStandbyTimeInNsecs; |
Eric Laurent | f69a3f8 | 2009-09-22 00:35:48 -0700 | [diff] [blame] | 3134 | for (size_t i = 0; i < outputTracks.size(); i++) { |
Eric Laurent | 65b6545 | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 3135 | outputTracks[i]->write(mMixBuffer, writeFrames); |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 3136 | } |
Eric Laurent | f69a3f8 | 2009-09-22 00:35:48 -0700 | [diff] [blame] | 3137 | mStandby = false; |
| 3138 | mBytesWritten += mixBufferSize; |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 3139 | } else { |
Eric Laurent | 65b6545 | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 3140 | // enable changes in effect chain |
Eric Laurent | 8ed6ed0 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 3141 | unlockEffectChains(effectChains); |
Eric Laurent | f69a3f8 | 2009-09-22 00:35:48 -0700 | [diff] [blame] | 3142 | usleep(sleepTime); |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 3143 | } |
| 3144 | |
| 3145 | // finally let go of all our tracks, without the lock held |
| 3146 | // since we can't guarantee the destructors won't acquire that |
| 3147 | // same lock. |
| 3148 | tracksToRemove.clear(); |
| 3149 | outputTracks.clear(); |
Eric Laurent | 65b6545 | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 3150 | |
| 3151 | // Effect chains will be actually deleted here if they were removed from |
| 3152 | // mEffectChains list during mixing or effects processing |
| 3153 | effectChains.clear(); |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 3154 | } |
| 3155 | |
Eric Laurent | 6dbdc40 | 2011-07-22 09:04:31 -0700 | [diff] [blame] | 3156 | releaseWakeLock(); |
| 3157 | |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 3158 | return false; |
| 3159 | } |
| 3160 | |
| 3161 | void AudioFlinger::DuplicatingThread::addOutputTrack(MixerThread *thread) |
| 3162 | { |
| 3163 | int frameCount = (3 * mFrameCount * mSampleRate) / thread->sampleRate(); |
| 3164 | OutputTrack *outputTrack = new OutputTrack((ThreadBase *)thread, |
Eric Laurent | 8ac9f8d | 2009-12-18 05:47:48 -0800 | [diff] [blame] | 3165 | this, |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 3166 | mSampleRate, |
| 3167 | mFormat, |
Jean-Michel Trivi | 5439223 | 2011-05-24 15:53:33 -0700 | [diff] [blame] | 3168 | mChannelMask, |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 3169 | frameCount); |
Eric Laurent | 6c30a71 | 2009-08-10 23:22:32 -0700 | [diff] [blame] | 3170 | if (outputTrack->cblk() != NULL) { |
Dima Zavin | 24fc2fb | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 3171 | thread->setStreamVolume(AUDIO_STREAM_CNT, 1.0f); |
Eric Laurent | 6c30a71 | 2009-08-10 23:22:32 -0700 | [diff] [blame] | 3172 | mOutputTracks.add(outputTrack); |
Steve Block | 71f2cf1 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 3173 | ALOGV("addOutputTrack() track %p, on thread %p", outputTrack, thread); |
Eric Laurent | 8ac9f8d | 2009-12-18 05:47:48 -0800 | [diff] [blame] | 3174 | updateWaitTime(); |
Eric Laurent | 6c30a71 | 2009-08-10 23:22:32 -0700 | [diff] [blame] | 3175 | } |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 3176 | } |
| 3177 | |
| 3178 | void AudioFlinger::DuplicatingThread::removeOutputTrack(MixerThread *thread) |
| 3179 | { |
| 3180 | Mutex::Autolock _l(mLock); |
| 3181 | for (size_t i = 0; i < mOutputTracks.size(); i++) { |
| 3182 | if (mOutputTracks[i]->thread() == (ThreadBase *)thread) { |
Eric Laurent | 6c30a71 | 2009-08-10 23:22:32 -0700 | [diff] [blame] | 3183 | mOutputTracks[i]->destroy(); |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 3184 | mOutputTracks.removeAt(i); |
Eric Laurent | 8ac9f8d | 2009-12-18 05:47:48 -0800 | [diff] [blame] | 3185 | updateWaitTime(); |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 3186 | return; |
| 3187 | } |
| 3188 | } |
Steve Block | 71f2cf1 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 3189 | ALOGV("removeOutputTrack(): unkonwn thread: %p", thread); |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 3190 | } |
| 3191 | |
Eric Laurent | 8ac9f8d | 2009-12-18 05:47:48 -0800 | [diff] [blame] | 3192 | void AudioFlinger::DuplicatingThread::updateWaitTime() |
| 3193 | { |
| 3194 | mWaitTimeMs = UINT_MAX; |
| 3195 | for (size_t i = 0; i < mOutputTracks.size(); i++) { |
| 3196 | sp<ThreadBase> strong = mOutputTracks[i]->thread().promote(); |
| 3197 | if (strong != NULL) { |
| 3198 | uint32_t waitTimeMs = (strong->frameCount() * 2 * 1000) / strong->sampleRate(); |
| 3199 | if (waitTimeMs < mWaitTimeMs) { |
| 3200 | mWaitTimeMs = waitTimeMs; |
| 3201 | } |
| 3202 | } |
| 3203 | } |
| 3204 | } |
| 3205 | |
| 3206 | |
| 3207 | bool AudioFlinger::DuplicatingThread::outputsReady(SortedVector< sp<OutputTrack> > &outputTracks) |
| 3208 | { |
| 3209 | for (size_t i = 0; i < outputTracks.size(); i++) { |
| 3210 | sp <ThreadBase> thread = outputTracks[i]->thread().promote(); |
| 3211 | if (thread == 0) { |
Steve Block | 8564c8d | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 3212 | ALOGW("DuplicatingThread::outputsReady() could not promote thread on output track %p", outputTracks[i].get()); |
Eric Laurent | 8ac9f8d | 2009-12-18 05:47:48 -0800 | [diff] [blame] | 3213 | return false; |
| 3214 | } |
| 3215 | PlaybackThread *playbackThread = (PlaybackThread *)thread.get(); |
| 3216 | if (playbackThread->standby() && !playbackThread->isSuspended()) { |
Steve Block | 71f2cf1 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 3217 | ALOGV("DuplicatingThread output track %p on thread %p Not Ready", outputTracks[i].get(), thread.get()); |
Eric Laurent | 8ac9f8d | 2009-12-18 05:47:48 -0800 | [diff] [blame] | 3218 | return false; |
| 3219 | } |
| 3220 | } |
| 3221 | return true; |
| 3222 | } |
| 3223 | |
| 3224 | uint32_t AudioFlinger::DuplicatingThread::activeSleepTimeUs() |
| 3225 | { |
| 3226 | return (mWaitTimeMs * 1000) / 2; |
| 3227 | } |
| 3228 | |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 3229 | // ---------------------------------------------------------------------------- |
| 3230 | |
The Android Open Source Project | b2a3dd8 | 2009-03-09 11:52:12 -0700 | [diff] [blame] | 3231 | // TrackBase constructor must be called with AudioFlinger::mLock held |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 3232 | AudioFlinger::ThreadBase::TrackBase::TrackBase( |
| 3233 | const wp<ThreadBase>& thread, |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 3234 | const sp<Client>& client, |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 3235 | uint32_t sampleRate, |
Glenn Kasten | 0a204ed | 2012-01-12 12:27:51 -0800 | [diff] [blame^] | 3236 | audio_format_t format, |
Jean-Michel Trivi | 5439223 | 2011-05-24 15:53:33 -0700 | [diff] [blame] | 3237 | uint32_t channelMask, |
The Android Open Source Project | f013e1a | 2008-12-17 18:05:43 -0800 | [diff] [blame] | 3238 | int frameCount, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3239 | uint32_t flags, |
Eric Laurent | 65b6545 | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 3240 | const sp<IMemory>& sharedBuffer, |
| 3241 | int sessionId) |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 3242 | : RefBase(), |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 3243 | mThread(thread), |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 3244 | mClient(client), |
Eric Laurent | 8a77a99 | 2009-09-09 05:16:08 -0700 | [diff] [blame] | 3245 | mCblk(0), |
The Android Open Source Project | f013e1a | 2008-12-17 18:05:43 -0800 | [diff] [blame] | 3246 | mFrameCount(0), |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 3247 | mState(IDLE), |
The Android Open Source Project | f013e1a | 2008-12-17 18:05:43 -0800 | [diff] [blame] | 3248 | mClientTid(-1), |
| 3249 | mFormat(format), |
Eric Laurent | 65b6545 | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 3250 | mFlags(flags & ~SYSTEM_FLAGS_MASK), |
| 3251 | mSessionId(sessionId) |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 3252 | { |
Steve Block | 71f2cf1 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 3253 | ALOGV_IF(sharedBuffer != 0, "sharedBuffer: %p, size: %d", sharedBuffer->pointer(), sharedBuffer->size()); |
The Android Open Source Project | f013e1a | 2008-12-17 18:05:43 -0800 | [diff] [blame] | 3254 | |
Steve Block | 5baa3a6 | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 3255 | // ALOGD("Creating track with %d buffers @ %d bytes", bufferCount, bufferSize); |
The Android Open Source Project | f013e1a | 2008-12-17 18:05:43 -0800 | [diff] [blame] | 3256 | size_t size = sizeof(audio_track_cblk_t); |
Jean-Michel Trivi | 5439223 | 2011-05-24 15:53:33 -0700 | [diff] [blame] | 3257 | uint8_t channelCount = popcount(channelMask); |
The Android Open Source Project | f013e1a | 2008-12-17 18:05:43 -0800 | [diff] [blame] | 3258 | size_t bufferSize = frameCount*channelCount*sizeof(int16_t); |
| 3259 | if (sharedBuffer == 0) { |
| 3260 | size += bufferSize; |
| 3261 | } |
| 3262 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3263 | if (client != NULL) { |
| 3264 | mCblkMemory = client->heap()->allocate(size); |
| 3265 | if (mCblkMemory != 0) { |
| 3266 | mCblk = static_cast<audio_track_cblk_t *>(mCblkMemory->pointer()); |
| 3267 | if (mCblk) { // construct the shared structure in-place. |
| 3268 | new(mCblk) audio_track_cblk_t(); |
| 3269 | // clear all buffers |
| 3270 | mCblk->frameCount = frameCount; |
Eric Laurent | 88e209d | 2009-07-07 07:10:45 -0700 | [diff] [blame] | 3271 | mCblk->sampleRate = sampleRate; |
Jean-Michel Trivi | 5439223 | 2011-05-24 15:53:33 -0700 | [diff] [blame] | 3272 | mChannelCount = channelCount; |
| 3273 | mChannelMask = channelMask; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3274 | if (sharedBuffer == 0) { |
| 3275 | mBuffer = (char*)mCblk + sizeof(audio_track_cblk_t); |
| 3276 | memset(mBuffer, 0, frameCount*channelCount*sizeof(int16_t)); |
| 3277 | // Force underrun condition to avoid false underrun callback until first data is |
Eric Laurent | 4712baa | 2010-09-30 16:12:31 -0700 | [diff] [blame] | 3278 | // written to buffer (other flags are cleared) |
Eric Laurent | eb8f850d | 2010-05-14 03:26:45 -0700 | [diff] [blame] | 3279 | mCblk->flags = CBLK_UNDERRUN_ON; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3280 | } else { |
| 3281 | mBuffer = sharedBuffer->pointer(); |
| 3282 | } |
| 3283 | mBufferEnd = (uint8_t *)mBuffer + bufferSize; |
The Android Open Source Project | f013e1a | 2008-12-17 18:05:43 -0800 | [diff] [blame] | 3284 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3285 | } else { |
Steve Block | 3762c31 | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 3286 | ALOGE("not enough memory for AudioTrack size=%u", size); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3287 | client->heap()->dump("AudioTrack"); |
| 3288 | return; |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 3289 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3290 | } else { |
| 3291 | mCblk = (audio_track_cblk_t *)(new uint8_t[size]); |
Glenn Kasten | c95ca2c | 2012-01-06 15:03:11 -0800 | [diff] [blame] | 3292 | // construct the shared structure in-place. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3293 | new(mCblk) audio_track_cblk_t(); |
| 3294 | // clear all buffers |
| 3295 | mCblk->frameCount = frameCount; |
Eric Laurent | 88e209d | 2009-07-07 07:10:45 -0700 | [diff] [blame] | 3296 | mCblk->sampleRate = sampleRate; |
Jean-Michel Trivi | 5439223 | 2011-05-24 15:53:33 -0700 | [diff] [blame] | 3297 | mChannelCount = channelCount; |
| 3298 | mChannelMask = channelMask; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3299 | mBuffer = (char*)mCblk + sizeof(audio_track_cblk_t); |
| 3300 | memset(mBuffer, 0, frameCount*channelCount*sizeof(int16_t)); |
| 3301 | // Force underrun condition to avoid false underrun callback until first data is |
Eric Laurent | 4712baa | 2010-09-30 16:12:31 -0700 | [diff] [blame] | 3302 | // written to buffer (other flags are cleared) |
Eric Laurent | eb8f850d | 2010-05-14 03:26:45 -0700 | [diff] [blame] | 3303 | mCblk->flags = CBLK_UNDERRUN_ON; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3304 | mBufferEnd = (uint8_t *)mBuffer + bufferSize; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3305 | } |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 3306 | } |
| 3307 | |
Eric Laurent | 2bb6b2a | 2009-09-16 06:02:45 -0700 | [diff] [blame] | 3308 | AudioFlinger::ThreadBase::TrackBase::~TrackBase() |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 3309 | { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3310 | if (mCblk) { |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 3311 | mCblk->~audio_track_cblk_t(); // destroy our shared-structure. |
| 3312 | if (mClient == NULL) { |
| 3313 | delete mCblk; |
| 3314 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3315 | } |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 3316 | mCblkMemory.clear(); // and free the shared memory |
Eric Laurent | b9481d8 | 2009-09-17 05:12:56 -0700 | [diff] [blame] | 3317 | if (mClient != NULL) { |
| 3318 | Mutex::Autolock _l(mClient->audioFlinger()->mLock); |
| 3319 | mClient.clear(); |
| 3320 | } |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 3321 | } |
| 3322 | |
Eric Laurent | 2bb6b2a | 2009-09-16 06:02:45 -0700 | [diff] [blame] | 3323 | void AudioFlinger::ThreadBase::TrackBase::releaseBuffer(AudioBufferProvider::Buffer* buffer) |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 3324 | { |
Glenn Kasten | c434c90 | 2011-12-13 11:53:26 -0800 | [diff] [blame] | 3325 | buffer->raw = NULL; |
The Android Open Source Project | f013e1a | 2008-12-17 18:05:43 -0800 | [diff] [blame] | 3326 | mFrameCount = buffer->frameCount; |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 3327 | step(); |
The Android Open Source Project | f013e1a | 2008-12-17 18:05:43 -0800 | [diff] [blame] | 3328 | buffer->frameCount = 0; |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 3329 | } |
| 3330 | |
Eric Laurent | 2bb6b2a | 2009-09-16 06:02:45 -0700 | [diff] [blame] | 3331 | bool AudioFlinger::ThreadBase::TrackBase::step() { |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 3332 | bool result; |
| 3333 | audio_track_cblk_t* cblk = this->cblk(); |
The Android Open Source Project | f013e1a | 2008-12-17 18:05:43 -0800 | [diff] [blame] | 3334 | |
| 3335 | result = cblk->stepServer(mFrameCount); |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 3336 | if (!result) { |
Steve Block | 71f2cf1 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 3337 | ALOGV("stepServer failed acquiring cblk mutex"); |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 3338 | mFlags |= STEPSERVER_FAILED; |
| 3339 | } |
| 3340 | return result; |
| 3341 | } |
| 3342 | |
Eric Laurent | 2bb6b2a | 2009-09-16 06:02:45 -0700 | [diff] [blame] | 3343 | void AudioFlinger::ThreadBase::TrackBase::reset() { |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 3344 | audio_track_cblk_t* cblk = this->cblk(); |
| 3345 | |
| 3346 | cblk->user = 0; |
| 3347 | cblk->server = 0; |
The Android Open Source Project | f013e1a | 2008-12-17 18:05:43 -0800 | [diff] [blame] | 3348 | cblk->userBase = 0; |
| 3349 | cblk->serverBase = 0; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3350 | mFlags &= (uint32_t)(~SYSTEM_FLAGS_MASK); |
Steve Block | 71f2cf1 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 3351 | ALOGV("TrackBase::reset"); |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 3352 | } |
| 3353 | |
Eric Laurent | 2bb6b2a | 2009-09-16 06:02:45 -0700 | [diff] [blame] | 3354 | sp<IMemory> AudioFlinger::ThreadBase::TrackBase::getCblk() const |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 3355 | { |
| 3356 | return mCblkMemory; |
| 3357 | } |
| 3358 | |
Eric Laurent | 2bb6b2a | 2009-09-16 06:02:45 -0700 | [diff] [blame] | 3359 | int AudioFlinger::ThreadBase::TrackBase::sampleRate() const { |
The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 3360 | return (int)mCblk->sampleRate; |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 3361 | } |
| 3362 | |
Eric Laurent | 2bb6b2a | 2009-09-16 06:02:45 -0700 | [diff] [blame] | 3363 | int AudioFlinger::ThreadBase::TrackBase::channelCount() const { |
Jean-Michel Trivi | 5439223 | 2011-05-24 15:53:33 -0700 | [diff] [blame] | 3364 | return (const int)mChannelCount; |
| 3365 | } |
| 3366 | |
| 3367 | uint32_t AudioFlinger::ThreadBase::TrackBase::channelMask() const { |
| 3368 | return mChannelMask; |
The Android Open Source Project | f013e1a | 2008-12-17 18:05:43 -0800 | [diff] [blame] | 3369 | } |
| 3370 | |
Eric Laurent | 2bb6b2a | 2009-09-16 06:02:45 -0700 | [diff] [blame] | 3371 | void* AudioFlinger::ThreadBase::TrackBase::getBuffer(uint32_t offset, uint32_t frames) const { |
The Android Open Source Project | f013e1a | 2008-12-17 18:05:43 -0800 | [diff] [blame] | 3372 | audio_track_cblk_t* cblk = this->cblk(); |
Glenn Kasten | faf354d | 2012-01-11 09:48:27 -0800 | [diff] [blame] | 3373 | size_t frameSize = cblk->frameSize; |
| 3374 | int8_t *bufferStart = (int8_t *)mBuffer + (offset-cblk->serverBase)*frameSize; |
| 3375 | int8_t *bufferEnd = bufferStart + frames * frameSize; |
The Android Open Source Project | f013e1a | 2008-12-17 18:05:43 -0800 | [diff] [blame] | 3376 | |
| 3377 | // Check validity of returned pointer in case the track control block would have been corrupted. |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 3378 | if (bufferStart < mBuffer || bufferStart > bufferEnd || bufferEnd > mBufferEnd || |
Glenn Kasten | faf354d | 2012-01-11 09:48:27 -0800 | [diff] [blame] | 3379 | ((unsigned long)bufferStart & (unsigned long)(frameSize - 1))) { |
Steve Block | 3762c31 | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 3380 | ALOGE("TrackBase::getBuffer buffer out of range:\n start: %p, end %p , mBuffer %p mBufferEnd %p\n \ |
Jean-Michel Trivi | 5439223 | 2011-05-24 15:53:33 -0700 | [diff] [blame] | 3381 | server %d, serverBase %d, user %d, userBase %d", |
The Android Open Source Project | f013e1a | 2008-12-17 18:05:43 -0800 | [diff] [blame] | 3382 | bufferStart, bufferEnd, mBuffer, mBufferEnd, |
Jean-Michel Trivi | 5439223 | 2011-05-24 15:53:33 -0700 | [diff] [blame] | 3383 | cblk->server, cblk->serverBase, cblk->user, cblk->userBase); |
The Android Open Source Project | f013e1a | 2008-12-17 18:05:43 -0800 | [diff] [blame] | 3384 | return 0; |
| 3385 | } |
| 3386 | |
| 3387 | return bufferStart; |
| 3388 | } |
| 3389 | |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 3390 | // ---------------------------------------------------------------------------- |
| 3391 | |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 3392 | // Track constructor must be called with AudioFlinger::mLock and ThreadBase::mLock held |
| 3393 | AudioFlinger::PlaybackThread::Track::Track( |
| 3394 | const wp<ThreadBase>& thread, |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 3395 | const sp<Client>& client, |
Glenn Kasten | bc1d77b | 2012-01-12 16:38:12 -0800 | [diff] [blame] | 3396 | audio_stream_type_t streamType, |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 3397 | uint32_t sampleRate, |
Glenn Kasten | 0a204ed | 2012-01-12 12:27:51 -0800 | [diff] [blame^] | 3398 | audio_format_t format, |
Jean-Michel Trivi | 5439223 | 2011-05-24 15:53:33 -0700 | [diff] [blame] | 3399 | uint32_t channelMask, |
The Android Open Source Project | f013e1a | 2008-12-17 18:05:43 -0800 | [diff] [blame] | 3400 | int frameCount, |
Eric Laurent | 65b6545 | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 3401 | const sp<IMemory>& sharedBuffer, |
| 3402 | int sessionId) |
Jean-Michel Trivi | 5439223 | 2011-05-24 15:53:33 -0700 | [diff] [blame] | 3403 | : TrackBase(thread, client, sampleRate, format, channelMask, frameCount, 0, sharedBuffer, sessionId), |
Eric Laurent | a92ebfa | 2010-08-31 13:50:07 -0700 | [diff] [blame] | 3404 | mMute(false), mSharedBuffer(sharedBuffer), mName(-1), mMainBuffer(NULL), mAuxBuffer(NULL), |
| 3405 | mAuxEffectId(0), mHasVolumeController(false) |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 3406 | { |
Eric Laurent | 8a77a99 | 2009-09-09 05:16:08 -0700 | [diff] [blame] | 3407 | if (mCblk != NULL) { |
| 3408 | sp<ThreadBase> baseThread = thread.promote(); |
| 3409 | if (baseThread != 0) { |
| 3410 | PlaybackThread *playbackThread = (PlaybackThread *)baseThread.get(); |
| 3411 | mName = playbackThread->getTrackName_l(); |
Eric Laurent | 65b6545 | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 3412 | mMainBuffer = playbackThread->mixBuffer(); |
Eric Laurent | 8a77a99 | 2009-09-09 05:16:08 -0700 | [diff] [blame] | 3413 | } |
Steve Block | 71f2cf1 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 3414 | ALOGV("Track constructor name %d, calling thread %d", mName, IPCThreadState::self()->getCallingPid()); |
Eric Laurent | 8a77a99 | 2009-09-09 05:16:08 -0700 | [diff] [blame] | 3415 | if (mName < 0) { |
Steve Block | 3762c31 | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 3416 | ALOGE("no more track names available"); |
Eric Laurent | 8a77a99 | 2009-09-09 05:16:08 -0700 | [diff] [blame] | 3417 | } |
Eric Laurent | 8a77a99 | 2009-09-09 05:16:08 -0700 | [diff] [blame] | 3418 | mStreamType = streamType; |
| 3419 | // NOTE: audio_track_cblk_t::frameSize for 8 bit PCM data is based on a sample size of |
| 3420 | // 16 bit because data is converted to 16 bit before being stored in buffer by AudioTrack |
Eric Laurent | 0950861 | 2011-07-21 19:35:01 -0700 | [diff] [blame] | 3421 | mCblk->frameSize = audio_is_linear_pcm(format) ? mChannelCount * sizeof(int16_t) : sizeof(uint8_t); |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 3422 | } |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 3423 | } |
| 3424 | |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 3425 | AudioFlinger::PlaybackThread::Track::~Track() |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 3426 | { |
Steve Block | 71f2cf1 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 3427 | ALOGV("PlaybackThread::Track destructor"); |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 3428 | sp<ThreadBase> thread = mThread.promote(); |
| 3429 | if (thread != 0) { |
Eric Laurent | ac196e1 | 2009-12-01 02:17:41 -0800 | [diff] [blame] | 3430 | Mutex::Autolock _l(thread->mLock); |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 3431 | mState = TERMINATED; |
The Android Open Source Project | b2a3dd8 | 2009-03-09 11:52:12 -0700 | [diff] [blame] | 3432 | } |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 3433 | } |
| 3434 | |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 3435 | void AudioFlinger::PlaybackThread::Track::destroy() |
| 3436 | { |
| 3437 | // NOTE: destroyTrack_l() can remove a strong reference to this Track |
| 3438 | // by removing it from mTracks vector, so there is a risk that this Tracks's |
| 3439 | // desctructor is called. As the destructor needs to lock mLock, |
| 3440 | // we must acquire a strong reference on this Track before locking mLock |
| 3441 | // here so that the destructor is called only when exiting this function. |
| 3442 | // On the other hand, as long as Track::destroy() is only called by |
| 3443 | // TrackHandle destructor, the TrackHandle still holds a strong ref on |
| 3444 | // this Track with its member mTrack. |
| 3445 | sp<Track> keep(this); |
| 3446 | { // scope for mLock |
| 3447 | sp<ThreadBase> thread = mThread.promote(); |
| 3448 | if (thread != 0) { |
Eric Laurent | ac196e1 | 2009-12-01 02:17:41 -0800 | [diff] [blame] | 3449 | if (!isOutputTrack()) { |
| 3450 | if (mState == ACTIVE || mState == RESUMING) { |
Eric Laurent | 8ed6ed0 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 3451 | AudioSystem::stopOutput(thread->id(), |
Dima Zavin | 24fc2fb | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 3452 | (audio_stream_type_t)mStreamType, |
Eric Laurent | 8ed6ed0 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 3453 | mSessionId); |
Gloria Wang | 9b3f152 | 2011-02-24 14:51:45 -0800 | [diff] [blame] | 3454 | |
| 3455 | // to track the speaker usage |
| 3456 | addBatteryData(IMediaPlayerService::kBatteryDataAudioFlingerStop); |
Eric Laurent | ac196e1 | 2009-12-01 02:17:41 -0800 | [diff] [blame] | 3457 | } |
| 3458 | AudioSystem::releaseOutput(thread->id()); |
Eric Laurent | 49f02be | 2009-11-19 09:00:56 -0800 | [diff] [blame] | 3459 | } |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 3460 | Mutex::Autolock _l(thread->mLock); |
| 3461 | PlaybackThread *playbackThread = (PlaybackThread *)thread.get(); |
| 3462 | playbackThread->destroyTrack_l(this); |
| 3463 | } |
| 3464 | } |
| 3465 | } |
| 3466 | |
| 3467 | void AudioFlinger::PlaybackThread::Track::dump(char* buffer, size_t size) |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 3468 | { |
Glenn Kasten | 0632bad | 2012-01-17 12:20:54 -0800 | [diff] [blame] | 3469 | uint32_t vlr = mCblk->volumeLR; |
Jean-Michel Trivi | 5439223 | 2011-05-24 15:53:33 -0700 | [diff] [blame] | 3470 | 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", |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 3471 | mName - AudioMixer::TRACK0, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3472 | (mClient == NULL) ? getpid() : mClient->pid(), |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 3473 | mStreamType, |
| 3474 | mFormat, |
Jean-Michel Trivi | 5439223 | 2011-05-24 15:53:33 -0700 | [diff] [blame] | 3475 | mChannelMask, |
Eric Laurent | 65b6545 | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 3476 | mSessionId, |
The Android Open Source Project | f013e1a | 2008-12-17 18:05:43 -0800 | [diff] [blame] | 3477 | mFrameCount, |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 3478 | mState, |
| 3479 | mMute, |
| 3480 | mFillingUpStatus, |
| 3481 | mCblk->sampleRate, |
Glenn Kasten | 0632bad | 2012-01-17 12:20:54 -0800 | [diff] [blame] | 3482 | vlr & 0xFFFF, |
| 3483 | vlr >> 16, |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 3484 | mCblk->server, |
Eric Laurent | 65b6545 | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 3485 | mCblk->user, |
| 3486 | (int)mMainBuffer, |
| 3487 | (int)mAuxBuffer); |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 3488 | } |
| 3489 | |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 3490 | status_t AudioFlinger::PlaybackThread::Track::getNextBuffer(AudioBufferProvider::Buffer* buffer) |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 3491 | { |
| 3492 | audio_track_cblk_t* cblk = this->cblk(); |
The Android Open Source Project | f013e1a | 2008-12-17 18:05:43 -0800 | [diff] [blame] | 3493 | uint32_t framesReady; |
| 3494 | uint32_t framesReq = buffer->frameCount; |
| 3495 | |
| 3496 | // Check if last stepServer failed, try to step now |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 3497 | if (mFlags & TrackBase::STEPSERVER_FAILED) { |
| 3498 | if (!step()) goto getNextBuffer_exit; |
Steve Block | 71f2cf1 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 3499 | ALOGV("stepServer recovered"); |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 3500 | mFlags &= ~TrackBase::STEPSERVER_FAILED; |
| 3501 | } |
| 3502 | |
The Android Open Source Project | f013e1a | 2008-12-17 18:05:43 -0800 | [diff] [blame] | 3503 | framesReady = cblk->framesReady(); |
| 3504 | |
Glenn Kasten | e80a4cc | 2011-12-15 09:51:17 -0800 | [diff] [blame] | 3505 | if (CC_LIKELY(framesReady)) { |
The Android Open Source Project | f013e1a | 2008-12-17 18:05:43 -0800 | [diff] [blame] | 3506 | uint32_t s = cblk->server; |
| 3507 | uint32_t bufferEnd = cblk->serverBase + cblk->frameCount; |
| 3508 | |
| 3509 | bufferEnd = (cblk->loopEnd < bufferEnd) ? cblk->loopEnd : bufferEnd; |
| 3510 | if (framesReq > framesReady) { |
| 3511 | framesReq = framesReady; |
| 3512 | } |
| 3513 | if (s + framesReq > bufferEnd) { |
| 3514 | framesReq = bufferEnd - s; |
| 3515 | } |
| 3516 | |
| 3517 | buffer->raw = getBuffer(s, framesReq); |
Glenn Kasten | c434c90 | 2011-12-13 11:53:26 -0800 | [diff] [blame] | 3518 | if (buffer->raw == NULL) goto getNextBuffer_exit; |
The Android Open Source Project | f013e1a | 2008-12-17 18:05:43 -0800 | [diff] [blame] | 3519 | |
| 3520 | buffer->frameCount = framesReq; |
| 3521 | return NO_ERROR; |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 3522 | } |
The Android Open Source Project | f013e1a | 2008-12-17 18:05:43 -0800 | [diff] [blame] | 3523 | |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 3524 | getNextBuffer_exit: |
Glenn Kasten | c434c90 | 2011-12-13 11:53:26 -0800 | [diff] [blame] | 3525 | buffer->raw = NULL; |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 3526 | buffer->frameCount = 0; |
Steve Block | 71f2cf1 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 3527 | ALOGV("getNextBuffer() no more data for track %d on thread %p", mName, mThread.unsafe_get()); |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 3528 | return NOT_ENOUGH_DATA; |
| 3529 | } |
| 3530 | |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 3531 | bool AudioFlinger::PlaybackThread::Track::isReady() const { |
Eric Laurent | 9a30fc1 | 2010-10-05 14:41:42 -0700 | [diff] [blame] | 3532 | if (mFillingUpStatus != FS_FILLING || isStopped() || isPausing()) return true; |
The Android Open Source Project | f013e1a | 2008-12-17 18:05:43 -0800 | [diff] [blame] | 3533 | |
| 3534 | if (mCblk->framesReady() >= mCblk->frameCount || |
Eric Laurent | eb8f850d | 2010-05-14 03:26:45 -0700 | [diff] [blame] | 3535 | (mCblk->flags & CBLK_FORCEREADY_MSK)) { |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 3536 | mFillingUpStatus = FS_FILLED; |
Eric Laurent | ae29b76 | 2011-03-28 18:37:07 -0700 | [diff] [blame] | 3537 | android_atomic_and(~CBLK_FORCEREADY_MSK, &mCblk->flags); |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 3538 | return true; |
| 3539 | } |
| 3540 | return false; |
| 3541 | } |
| 3542 | |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 3543 | status_t AudioFlinger::PlaybackThread::Track::start() |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 3544 | { |
Eric Laurent | 49f02be | 2009-11-19 09:00:56 -0800 | [diff] [blame] | 3545 | status_t status = NO_ERROR; |
Steve Block | 71f2cf1 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 3546 | ALOGV("start(%d), calling thread %d session %d", |
Eric Laurent | 0d7e048 | 2010-07-19 06:24:46 -0700 | [diff] [blame] | 3547 | mName, IPCThreadState::self()->getCallingPid(), mSessionId); |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 3548 | sp<ThreadBase> thread = mThread.promote(); |
| 3549 | if (thread != 0) { |
| 3550 | Mutex::Autolock _l(thread->mLock); |
Eric Laurent | 49f02be | 2009-11-19 09:00:56 -0800 | [diff] [blame] | 3551 | int state = mState; |
| 3552 | // here the track could be either new, or restarted |
| 3553 | // in both cases "unstop" the track |
| 3554 | if (mState == PAUSED) { |
| 3555 | mState = TrackBase::RESUMING; |
Steve Block | 71f2cf1 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 3556 | ALOGV("PAUSED => RESUMING (%d) on thread %p", mName, this); |
Eric Laurent | 49f02be | 2009-11-19 09:00:56 -0800 | [diff] [blame] | 3557 | } else { |
| 3558 | mState = TrackBase::ACTIVE; |
Steve Block | 71f2cf1 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 3559 | ALOGV("? => ACTIVE (%d) on thread %p", mName, this); |
Eric Laurent | 49f02be | 2009-11-19 09:00:56 -0800 | [diff] [blame] | 3560 | } |
| 3561 | |
| 3562 | if (!isOutputTrack() && state != ACTIVE && state != RESUMING) { |
| 3563 | thread->mLock.unlock(); |
Eric Laurent | 8ed6ed0 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 3564 | status = AudioSystem::startOutput(thread->id(), |
Dima Zavin | 24fc2fb | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 3565 | (audio_stream_type_t)mStreamType, |
Eric Laurent | 8ed6ed0 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 3566 | mSessionId); |
Eric Laurent | 49f02be | 2009-11-19 09:00:56 -0800 | [diff] [blame] | 3567 | thread->mLock.lock(); |
Gloria Wang | 9b3f152 | 2011-02-24 14:51:45 -0800 | [diff] [blame] | 3568 | |
| 3569 | // to track the speaker usage |
| 3570 | if (status == NO_ERROR) { |
| 3571 | addBatteryData(IMediaPlayerService::kBatteryDataAudioFlingerStart); |
| 3572 | } |
Eric Laurent | 49f02be | 2009-11-19 09:00:56 -0800 | [diff] [blame] | 3573 | } |
| 3574 | if (status == NO_ERROR) { |
| 3575 | PlaybackThread *playbackThread = (PlaybackThread *)thread.get(); |
| 3576 | playbackThread->addTrack_l(this); |
| 3577 | } else { |
| 3578 | mState = state; |
| 3579 | } |
| 3580 | } else { |
| 3581 | status = BAD_VALUE; |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 3582 | } |
Eric Laurent | 49f02be | 2009-11-19 09:00:56 -0800 | [diff] [blame] | 3583 | return status; |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 3584 | } |
| 3585 | |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 3586 | void AudioFlinger::PlaybackThread::Track::stop() |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 3587 | { |
Steve Block | 71f2cf1 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 3588 | ALOGV("stop(%d), calling thread %d", mName, IPCThreadState::self()->getCallingPid()); |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 3589 | sp<ThreadBase> thread = mThread.promote(); |
| 3590 | if (thread != 0) { |
| 3591 | Mutex::Autolock _l(thread->mLock); |
Eric Laurent | 49f02be | 2009-11-19 09:00:56 -0800 | [diff] [blame] | 3592 | int state = mState; |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 3593 | if (mState > STOPPED) { |
| 3594 | mState = STOPPED; |
| 3595 | // If the track is not active (PAUSED and buffers full), flush buffers |
| 3596 | PlaybackThread *playbackThread = (PlaybackThread *)thread.get(); |
| 3597 | if (playbackThread->mActiveTracks.indexOf(this) < 0) { |
| 3598 | reset(); |
| 3599 | } |
Steve Block | 71f2cf1 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 3600 | ALOGV("(> STOPPED) => STOPPED (%d) on thread %p", mName, playbackThread); |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 3601 | } |
Eric Laurent | 49f02be | 2009-11-19 09:00:56 -0800 | [diff] [blame] | 3602 | if (!isOutputTrack() && (state == ACTIVE || state == RESUMING)) { |
| 3603 | thread->mLock.unlock(); |
Eric Laurent | 8ed6ed0 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 3604 | AudioSystem::stopOutput(thread->id(), |
Dima Zavin | 24fc2fb | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 3605 | (audio_stream_type_t)mStreamType, |
Eric Laurent | 8ed6ed0 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 3606 | mSessionId); |
Eric Laurent | 49f02be | 2009-11-19 09:00:56 -0800 | [diff] [blame] | 3607 | thread->mLock.lock(); |
Gloria Wang | 9b3f152 | 2011-02-24 14:51:45 -0800 | [diff] [blame] | 3608 | |
| 3609 | // to track the speaker usage |
| 3610 | addBatteryData(IMediaPlayerService::kBatteryDataAudioFlingerStop); |
Eric Laurent | 49f02be | 2009-11-19 09:00:56 -0800 | [diff] [blame] | 3611 | } |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 3612 | } |
| 3613 | } |
| 3614 | |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 3615 | void AudioFlinger::PlaybackThread::Track::pause() |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 3616 | { |
Steve Block | 71f2cf1 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 3617 | ALOGV("pause(%d), calling thread %d", mName, IPCThreadState::self()->getCallingPid()); |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 3618 | sp<ThreadBase> thread = mThread.promote(); |
| 3619 | if (thread != 0) { |
| 3620 | Mutex::Autolock _l(thread->mLock); |
| 3621 | if (mState == ACTIVE || mState == RESUMING) { |
| 3622 | mState = PAUSING; |
Steve Block | 71f2cf1 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 3623 | ALOGV("ACTIVE/RESUMING => PAUSING (%d) on thread %p", mName, thread.get()); |
Eric Laurent | 49f02be | 2009-11-19 09:00:56 -0800 | [diff] [blame] | 3624 | if (!isOutputTrack()) { |
| 3625 | thread->mLock.unlock(); |
Eric Laurent | 8ed6ed0 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 3626 | AudioSystem::stopOutput(thread->id(), |
Dima Zavin | 24fc2fb | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 3627 | (audio_stream_type_t)mStreamType, |
Eric Laurent | 8ed6ed0 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 3628 | mSessionId); |
Eric Laurent | 49f02be | 2009-11-19 09:00:56 -0800 | [diff] [blame] | 3629 | thread->mLock.lock(); |
Gloria Wang | 9b3f152 | 2011-02-24 14:51:45 -0800 | [diff] [blame] | 3630 | |
| 3631 | // to track the speaker usage |
| 3632 | addBatteryData(IMediaPlayerService::kBatteryDataAudioFlingerStop); |
Eric Laurent | 49f02be | 2009-11-19 09:00:56 -0800 | [diff] [blame] | 3633 | } |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 3634 | } |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 3635 | } |
| 3636 | } |
| 3637 | |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 3638 | void AudioFlinger::PlaybackThread::Track::flush() |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 3639 | { |
Steve Block | 71f2cf1 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 3640 | ALOGV("flush(%d)", mName); |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 3641 | sp<ThreadBase> thread = mThread.promote(); |
| 3642 | if (thread != 0) { |
| 3643 | Mutex::Autolock _l(thread->mLock); |
| 3644 | if (mState != STOPPED && mState != PAUSED && mState != PAUSING) { |
| 3645 | return; |
| 3646 | } |
| 3647 | // No point remaining in PAUSED state after a flush => go to |
| 3648 | // STOPPED state |
| 3649 | mState = STOPPED; |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 3650 | |
Eric Laurent | ae29b76 | 2011-03-28 18:37:07 -0700 | [diff] [blame] | 3651 | // do not reset the track if it is still in the process of being stopped or paused. |
| 3652 | // this will be done by prepareTracks_l() when the track is stopped. |
| 3653 | PlaybackThread *playbackThread = (PlaybackThread *)thread.get(); |
| 3654 | if (playbackThread->mActiveTracks.indexOf(this) < 0) { |
| 3655 | reset(); |
| 3656 | } |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 3657 | } |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 3658 | } |
| 3659 | |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 3660 | void AudioFlinger::PlaybackThread::Track::reset() |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 3661 | { |
The Android Open Source Project | f013e1a | 2008-12-17 18:05:43 -0800 | [diff] [blame] | 3662 | // Do not reset twice to avoid discarding data written just after a flush and before |
| 3663 | // the audioflinger thread detects the track is stopped. |
| 3664 | if (!mResetDone) { |
| 3665 | TrackBase::reset(); |
| 3666 | // Force underrun condition to avoid false underrun callback until first data is |
| 3667 | // written to buffer |
Eric Laurent | ae29b76 | 2011-03-28 18:37:07 -0700 | [diff] [blame] | 3668 | android_atomic_and(~CBLK_FORCEREADY_MSK, &mCblk->flags); |
| 3669 | android_atomic_or(CBLK_UNDERRUN_ON, &mCblk->flags); |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 3670 | mFillingUpStatus = FS_FILLING; |
The Android Open Source Project | f013e1a | 2008-12-17 18:05:43 -0800 | [diff] [blame] | 3671 | mResetDone = true; |
| 3672 | } |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 3673 | } |
| 3674 | |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 3675 | void AudioFlinger::PlaybackThread::Track::mute(bool muted) |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 3676 | { |
| 3677 | mMute = muted; |
| 3678 | } |
| 3679 | |
Eric Laurent | 65b6545 | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 3680 | status_t AudioFlinger::PlaybackThread::Track::attachAuxEffect(int EffectId) |
| 3681 | { |
| 3682 | status_t status = DEAD_OBJECT; |
| 3683 | sp<ThreadBase> thread = mThread.promote(); |
| 3684 | if (thread != 0) { |
| 3685 | PlaybackThread *playbackThread = (PlaybackThread *)thread.get(); |
| 3686 | status = playbackThread->attachAuxEffect(this, EffectId); |
| 3687 | } |
| 3688 | return status; |
| 3689 | } |
| 3690 | |
| 3691 | void AudioFlinger::PlaybackThread::Track::setAuxBuffer(int EffectId, int32_t *buffer) |
| 3692 | { |
| 3693 | mAuxEffectId = EffectId; |
| 3694 | mAuxBuffer = buffer; |
| 3695 | } |
| 3696 | |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 3697 | // ---------------------------------------------------------------------------- |
| 3698 | |
The Android Open Source Project | b2a3dd8 | 2009-03-09 11:52:12 -0700 | [diff] [blame] | 3699 | // RecordTrack constructor must be called with AudioFlinger::mLock held |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 3700 | AudioFlinger::RecordThread::RecordTrack::RecordTrack( |
| 3701 | const wp<ThreadBase>& thread, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3702 | const sp<Client>& client, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3703 | uint32_t sampleRate, |
Glenn Kasten | 0a204ed | 2012-01-12 12:27:51 -0800 | [diff] [blame^] | 3704 | audio_format_t format, |
Jean-Michel Trivi | 5439223 | 2011-05-24 15:53:33 -0700 | [diff] [blame] | 3705 | uint32_t channelMask, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3706 | int frameCount, |
Eric Laurent | 65b6545 | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 3707 | uint32_t flags, |
| 3708 | int sessionId) |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 3709 | : TrackBase(thread, client, sampleRate, format, |
Jean-Michel Trivi | 5439223 | 2011-05-24 15:53:33 -0700 | [diff] [blame] | 3710 | channelMask, frameCount, flags, 0, sessionId), |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 3711 | mOverflow(false) |
| 3712 | { |
Eric Laurent | 8a77a99 | 2009-09-09 05:16:08 -0700 | [diff] [blame] | 3713 | if (mCblk != NULL) { |
Steve Block | 71f2cf1 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 3714 | ALOGV("RecordTrack constructor, size %d", (int)mBufferEnd - (int)mBuffer); |
Dima Zavin | 24fc2fb | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 3715 | if (format == AUDIO_FORMAT_PCM_16_BIT) { |
Jean-Michel Trivi | 5439223 | 2011-05-24 15:53:33 -0700 | [diff] [blame] | 3716 | mCblk->frameSize = mChannelCount * sizeof(int16_t); |
Dima Zavin | 24fc2fb | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 3717 | } else if (format == AUDIO_FORMAT_PCM_8_BIT) { |
Jean-Michel Trivi | 5439223 | 2011-05-24 15:53:33 -0700 | [diff] [blame] | 3718 | mCblk->frameSize = mChannelCount * sizeof(int8_t); |
Eric Laurent | 8a77a99 | 2009-09-09 05:16:08 -0700 | [diff] [blame] | 3719 | } else { |
| 3720 | mCblk->frameSize = sizeof(int8_t); |
| 3721 | } |
| 3722 | } |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 3723 | } |
| 3724 | |
| 3725 | AudioFlinger::RecordThread::RecordTrack::~RecordTrack() |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3726 | { |
Eric Laurent | 49f02be | 2009-11-19 09:00:56 -0800 | [diff] [blame] | 3727 | sp<ThreadBase> thread = mThread.promote(); |
| 3728 | if (thread != 0) { |
| 3729 | AudioSystem::releaseInput(thread->id()); |
| 3730 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3731 | } |
| 3732 | |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 3733 | status_t AudioFlinger::RecordThread::RecordTrack::getNextBuffer(AudioBufferProvider::Buffer* buffer) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3734 | { |
| 3735 | audio_track_cblk_t* cblk = this->cblk(); |
| 3736 | uint32_t framesAvail; |
| 3737 | uint32_t framesReq = buffer->frameCount; |
| 3738 | |
| 3739 | // Check if last stepServer failed, try to step now |
| 3740 | if (mFlags & TrackBase::STEPSERVER_FAILED) { |
| 3741 | if (!step()) goto getNextBuffer_exit; |
Steve Block | 71f2cf1 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 3742 | ALOGV("stepServer recovered"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3743 | mFlags &= ~TrackBase::STEPSERVER_FAILED; |
| 3744 | } |
| 3745 | |
| 3746 | framesAvail = cblk->framesAvailable_l(); |
| 3747 | |
Glenn Kasten | e80a4cc | 2011-12-15 09:51:17 -0800 | [diff] [blame] | 3748 | if (CC_LIKELY(framesAvail)) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3749 | uint32_t s = cblk->server; |
| 3750 | uint32_t bufferEnd = cblk->serverBase + cblk->frameCount; |
| 3751 | |
| 3752 | if (framesReq > framesAvail) { |
| 3753 | framesReq = framesAvail; |
| 3754 | } |
| 3755 | if (s + framesReq > bufferEnd) { |
| 3756 | framesReq = bufferEnd - s; |
| 3757 | } |
| 3758 | |
| 3759 | buffer->raw = getBuffer(s, framesReq); |
Glenn Kasten | c434c90 | 2011-12-13 11:53:26 -0800 | [diff] [blame] | 3760 | if (buffer->raw == NULL) goto getNextBuffer_exit; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3761 | |
| 3762 | buffer->frameCount = framesReq; |
| 3763 | return NO_ERROR; |
| 3764 | } |
| 3765 | |
| 3766 | getNextBuffer_exit: |
Glenn Kasten | c434c90 | 2011-12-13 11:53:26 -0800 | [diff] [blame] | 3767 | buffer->raw = NULL; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3768 | buffer->frameCount = 0; |
| 3769 | return NOT_ENOUGH_DATA; |
| 3770 | } |
| 3771 | |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 3772 | status_t AudioFlinger::RecordThread::RecordTrack::start() |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3773 | { |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 3774 | sp<ThreadBase> thread = mThread.promote(); |
| 3775 | if (thread != 0) { |
| 3776 | RecordThread *recordThread = (RecordThread *)thread.get(); |
| 3777 | return recordThread->start(this); |
Eric Laurent | 49f02be | 2009-11-19 09:00:56 -0800 | [diff] [blame] | 3778 | } else { |
| 3779 | return BAD_VALUE; |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 3780 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3781 | } |
| 3782 | |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 3783 | void AudioFlinger::RecordThread::RecordTrack::stop() |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3784 | { |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 3785 | sp<ThreadBase> thread = mThread.promote(); |
| 3786 | if (thread != 0) { |
| 3787 | RecordThread *recordThread = (RecordThread *)thread.get(); |
| 3788 | recordThread->stop(this); |
Eric Laurent | ae29b76 | 2011-03-28 18:37:07 -0700 | [diff] [blame] | 3789 | TrackBase::reset(); |
| 3790 | // Force overerrun condition to avoid false overrun callback until first data is |
| 3791 | // read from buffer |
| 3792 | android_atomic_or(CBLK_UNDERRUN_ON, &mCblk->flags); |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 3793 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3794 | } |
| 3795 | |
Eric Laurent | 3fdb126 | 2009-11-07 00:01:32 -0800 | [diff] [blame] | 3796 | void AudioFlinger::RecordThread::RecordTrack::dump(char* buffer, size_t size) |
| 3797 | { |
Jean-Michel Trivi | 5439223 | 2011-05-24 15:53:33 -0700 | [diff] [blame] | 3798 | snprintf(buffer, size, " %05d %03u 0x%08x %05d %04u %01d %05u %08x %08x\n", |
Eric Laurent | 3fdb126 | 2009-11-07 00:01:32 -0800 | [diff] [blame] | 3799 | (mClient == NULL) ? getpid() : mClient->pid(), |
| 3800 | mFormat, |
Jean-Michel Trivi | 5439223 | 2011-05-24 15:53:33 -0700 | [diff] [blame] | 3801 | mChannelMask, |
Eric Laurent | 65b6545 | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 3802 | mSessionId, |
Eric Laurent | 3fdb126 | 2009-11-07 00:01:32 -0800 | [diff] [blame] | 3803 | mFrameCount, |
| 3804 | mState, |
| 3805 | mCblk->sampleRate, |
| 3806 | mCblk->server, |
| 3807 | mCblk->user); |
| 3808 | } |
| 3809 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3810 | |
| 3811 | // ---------------------------------------------------------------------------- |
| 3812 | |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 3813 | AudioFlinger::PlaybackThread::OutputTrack::OutputTrack( |
| 3814 | const wp<ThreadBase>& thread, |
Eric Laurent | 8ac9f8d | 2009-12-18 05:47:48 -0800 | [diff] [blame] | 3815 | DuplicatingThread *sourceThread, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3816 | uint32_t sampleRate, |
Glenn Kasten | 0a204ed | 2012-01-12 12:27:51 -0800 | [diff] [blame^] | 3817 | audio_format_t format, |
Jean-Michel Trivi | 5439223 | 2011-05-24 15:53:33 -0700 | [diff] [blame] | 3818 | uint32_t channelMask, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3819 | int frameCount) |
Jean-Michel Trivi | 5439223 | 2011-05-24 15:53:33 -0700 | [diff] [blame] | 3820 | : Track(thread, NULL, AUDIO_STREAM_CNT, sampleRate, format, channelMask, frameCount, NULL, 0), |
Eric Laurent | 8ac9f8d | 2009-12-18 05:47:48 -0800 | [diff] [blame] | 3821 | mActive(false), mSourceThread(sourceThread) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3822 | { |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 3823 | |
| 3824 | PlaybackThread *playbackThread = (PlaybackThread *)thread.unsafe_get(); |
Eric Laurent | 6c30a71 | 2009-08-10 23:22:32 -0700 | [diff] [blame] | 3825 | if (mCblk != NULL) { |
Eric Laurent | eb8f850d | 2010-05-14 03:26:45 -0700 | [diff] [blame] | 3826 | mCblk->flags |= CBLK_DIRECTION_OUT; |
Eric Laurent | 6c30a71 | 2009-08-10 23:22:32 -0700 | [diff] [blame] | 3827 | mCblk->buffers = (char*)mCblk + sizeof(audio_track_cblk_t); |
Glenn Kasten | 0632bad | 2012-01-17 12:20:54 -0800 | [diff] [blame] | 3828 | mCblk->volumeLR = (MAX_GAIN_INT << 16) | MAX_GAIN_INT; |
Eric Laurent | 6c30a71 | 2009-08-10 23:22:32 -0700 | [diff] [blame] | 3829 | mOutBuffer.frameCount = 0; |
Eric Laurent | 6c30a71 | 2009-08-10 23:22:32 -0700 | [diff] [blame] | 3830 | playbackThread->mTracks.add(this); |
Steve Block | 71f2cf1 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 3831 | ALOGV("OutputTrack constructor mCblk %p, mBuffer %p, mCblk->buffers %p, " \ |
Jean-Michel Trivi | 5439223 | 2011-05-24 15:53:33 -0700 | [diff] [blame] | 3832 | "mCblk->frameCount %d, mCblk->sampleRate %d, mChannelMask 0x%08x mBufferEnd %p", |
| 3833 | mCblk, mBuffer, mCblk->buffers, |
| 3834 | mCblk->frameCount, mCblk->sampleRate, mChannelMask, mBufferEnd); |
Eric Laurent | 6c30a71 | 2009-08-10 23:22:32 -0700 | [diff] [blame] | 3835 | } else { |
Steve Block | 8564c8d | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 3836 | ALOGW("Error creating output track on thread %p", playbackThread); |
Eric Laurent | 6c30a71 | 2009-08-10 23:22:32 -0700 | [diff] [blame] | 3837 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3838 | } |
| 3839 | |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 3840 | AudioFlinger::PlaybackThread::OutputTrack::~OutputTrack() |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3841 | { |
Eric Laurent | 6c30a71 | 2009-08-10 23:22:32 -0700 | [diff] [blame] | 3842 | clearBufferQueue(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3843 | } |
| 3844 | |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 3845 | status_t AudioFlinger::PlaybackThread::OutputTrack::start() |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3846 | { |
| 3847 | status_t status = Track::start(); |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 3848 | if (status != NO_ERROR) { |
| 3849 | return status; |
| 3850 | } |
| 3851 | |
| 3852 | mActive = true; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3853 | mRetryCount = 127; |
| 3854 | return status; |
| 3855 | } |
| 3856 | |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 3857 | void AudioFlinger::PlaybackThread::OutputTrack::stop() |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3858 | { |
| 3859 | Track::stop(); |
| 3860 | clearBufferQueue(); |
| 3861 | mOutBuffer.frameCount = 0; |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 3862 | mActive = false; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3863 | } |
| 3864 | |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 3865 | bool AudioFlinger::PlaybackThread::OutputTrack::write(int16_t* data, uint32_t frames) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3866 | { |
| 3867 | Buffer *pInBuffer; |
| 3868 | Buffer inBuffer; |
Jean-Michel Trivi | 5439223 | 2011-05-24 15:53:33 -0700 | [diff] [blame] | 3869 | uint32_t channelCount = mChannelCount; |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 3870 | bool outputBufferFull = false; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3871 | inBuffer.frameCount = frames; |
| 3872 | inBuffer.i16 = data; |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 3873 | |
Eric Laurent | 8ac9f8d | 2009-12-18 05:47:48 -0800 | [diff] [blame] | 3874 | uint32_t waitTimeLeftMs = mSourceThread->waitTimeMs(); |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 3875 | |
Eric Laurent | 62443f5 | 2009-10-05 20:29:18 -0700 | [diff] [blame] | 3876 | if (!mActive && frames != 0) { |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 3877 | start(); |
| 3878 | sp<ThreadBase> thread = mThread.promote(); |
| 3879 | if (thread != 0) { |
| 3880 | MixerThread *mixerThread = (MixerThread *)thread.get(); |
| 3881 | if (mCblk->frameCount > frames){ |
| 3882 | if (mBufferQueue.size() < kMaxOverFlowBuffers) { |
| 3883 | uint32_t startFrames = (mCblk->frameCount - frames); |
| 3884 | pInBuffer = new Buffer; |
Eric Laurent | b0a0147 | 2010-05-14 05:45:46 -0700 | [diff] [blame] | 3885 | pInBuffer->mBuffer = new int16_t[startFrames * channelCount]; |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 3886 | pInBuffer->frameCount = startFrames; |
| 3887 | pInBuffer->i16 = pInBuffer->mBuffer; |
Eric Laurent | b0a0147 | 2010-05-14 05:45:46 -0700 | [diff] [blame] | 3888 | memset(pInBuffer->raw, 0, startFrames * channelCount * sizeof(int16_t)); |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 3889 | mBufferQueue.add(pInBuffer); |
| 3890 | } else { |
Steve Block | 8564c8d | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 3891 | ALOGW ("OutputTrack::write() %p no more buffers in queue", this); |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 3892 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3893 | } |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 3894 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3895 | } |
| 3896 | |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 3897 | while (waitTimeLeftMs) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3898 | // First write pending buffers, then new data |
| 3899 | if (mBufferQueue.size()) { |
| 3900 | pInBuffer = mBufferQueue.itemAt(0); |
| 3901 | } else { |
| 3902 | pInBuffer = &inBuffer; |
| 3903 | } |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 3904 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3905 | if (pInBuffer->frameCount == 0) { |
| 3906 | break; |
| 3907 | } |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 3908 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3909 | if (mOutBuffer.frameCount == 0) { |
| 3910 | mOutBuffer.frameCount = pInBuffer->frameCount; |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 3911 | nsecs_t startTime = systemTime(); |
| 3912 | if (obtainBuffer(&mOutBuffer, waitTimeLeftMs) == (status_t)AudioTrack::NO_MORE_BUFFERS) { |
Steve Block | 71f2cf1 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 3913 | ALOGV ("OutputTrack::write() %p thread %p no more output buffers", this, mThread.unsafe_get()); |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 3914 | outputBufferFull = true; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3915 | break; |
| 3916 | } |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 3917 | uint32_t waitTimeMs = (uint32_t)ns2ms(systemTime() - startTime); |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 3918 | if (waitTimeLeftMs >= waitTimeMs) { |
| 3919 | waitTimeLeftMs -= waitTimeMs; |
| 3920 | } else { |
| 3921 | waitTimeLeftMs = 0; |
| 3922 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3923 | } |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 3924 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3925 | uint32_t outFrames = pInBuffer->frameCount > mOutBuffer.frameCount ? mOutBuffer.frameCount : pInBuffer->frameCount; |
Eric Laurent | b0a0147 | 2010-05-14 05:45:46 -0700 | [diff] [blame] | 3926 | memcpy(mOutBuffer.raw, pInBuffer->raw, outFrames * channelCount * sizeof(int16_t)); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3927 | mCblk->stepUser(outFrames); |
| 3928 | pInBuffer->frameCount -= outFrames; |
Eric Laurent | b0a0147 | 2010-05-14 05:45:46 -0700 | [diff] [blame] | 3929 | pInBuffer->i16 += outFrames * channelCount; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3930 | mOutBuffer.frameCount -= outFrames; |
Eric Laurent | b0a0147 | 2010-05-14 05:45:46 -0700 | [diff] [blame] | 3931 | mOutBuffer.i16 += outFrames * channelCount; |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 3932 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3933 | if (pInBuffer->frameCount == 0) { |
| 3934 | if (mBufferQueue.size()) { |
| 3935 | mBufferQueue.removeAt(0); |
| 3936 | delete [] pInBuffer->mBuffer; |
| 3937 | delete pInBuffer; |
Steve Block | 71f2cf1 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 3938 | ALOGV("OutputTrack::write() %p thread %p released overflow buffer %d", this, mThread.unsafe_get(), mBufferQueue.size()); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3939 | } else { |
| 3940 | break; |
| 3941 | } |
| 3942 | } |
| 3943 | } |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 3944 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3945 | // If we could not write all frames, allocate a buffer and queue it for next time. |
| 3946 | if (inBuffer.frameCount) { |
Eric Laurent | 8ac9f8d | 2009-12-18 05:47:48 -0800 | [diff] [blame] | 3947 | sp<ThreadBase> thread = mThread.promote(); |
| 3948 | if (thread != 0 && !thread->standby()) { |
| 3949 | if (mBufferQueue.size() < kMaxOverFlowBuffers) { |
| 3950 | pInBuffer = new Buffer; |
Eric Laurent | b0a0147 | 2010-05-14 05:45:46 -0700 | [diff] [blame] | 3951 | pInBuffer->mBuffer = new int16_t[inBuffer.frameCount * channelCount]; |
Eric Laurent | 8ac9f8d | 2009-12-18 05:47:48 -0800 | [diff] [blame] | 3952 | pInBuffer->frameCount = inBuffer.frameCount; |
| 3953 | pInBuffer->i16 = pInBuffer->mBuffer; |
Eric Laurent | b0a0147 | 2010-05-14 05:45:46 -0700 | [diff] [blame] | 3954 | memcpy(pInBuffer->raw, inBuffer.raw, inBuffer.frameCount * channelCount * sizeof(int16_t)); |
Eric Laurent | 8ac9f8d | 2009-12-18 05:47:48 -0800 | [diff] [blame] | 3955 | mBufferQueue.add(pInBuffer); |
Steve Block | 71f2cf1 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 3956 | ALOGV("OutputTrack::write() %p thread %p adding overflow buffer %d", this, mThread.unsafe_get(), mBufferQueue.size()); |
Eric Laurent | 8ac9f8d | 2009-12-18 05:47:48 -0800 | [diff] [blame] | 3957 | } else { |
Steve Block | 8564c8d | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 3958 | ALOGW("OutputTrack::write() %p thread %p no more overflow buffers", mThread.unsafe_get(), this); |
Eric Laurent | 8ac9f8d | 2009-12-18 05:47:48 -0800 | [diff] [blame] | 3959 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3960 | } |
| 3961 | } |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 3962 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3963 | // Calling write() with a 0 length buffer, means that no more data will be written: |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 3964 | // If no more buffers are pending, fill output track buffer to make sure it is started |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3965 | // by output mixer. |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 3966 | if (frames == 0 && mBufferQueue.size() == 0) { |
| 3967 | if (mCblk->user < mCblk->frameCount) { |
| 3968 | frames = mCblk->frameCount - mCblk->user; |
| 3969 | pInBuffer = new Buffer; |
Eric Laurent | b0a0147 | 2010-05-14 05:45:46 -0700 | [diff] [blame] | 3970 | pInBuffer->mBuffer = new int16_t[frames * channelCount]; |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 3971 | pInBuffer->frameCount = frames; |
| 3972 | pInBuffer->i16 = pInBuffer->mBuffer; |
Eric Laurent | b0a0147 | 2010-05-14 05:45:46 -0700 | [diff] [blame] | 3973 | memset(pInBuffer->raw, 0, frames * channelCount * sizeof(int16_t)); |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 3974 | mBufferQueue.add(pInBuffer); |
Eric Laurent | 62443f5 | 2009-10-05 20:29:18 -0700 | [diff] [blame] | 3975 | } else if (mActive) { |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 3976 | stop(); |
| 3977 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3978 | } |
| 3979 | |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 3980 | return outputBufferFull; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3981 | } |
| 3982 | |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 3983 | status_t AudioFlinger::PlaybackThread::OutputTrack::obtainBuffer(AudioBufferProvider::Buffer* buffer, uint32_t waitTimeMs) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3984 | { |
| 3985 | int active; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3986 | status_t result; |
| 3987 | audio_track_cblk_t* cblk = mCblk; |
| 3988 | uint32_t framesReq = buffer->frameCount; |
| 3989 | |
Steve Block | 71f2cf1 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 3990 | // ALOGV("OutputTrack::obtainBuffer user %d, server %d", cblk->user, cblk->server); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3991 | buffer->frameCount = 0; |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 3992 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3993 | uint32_t framesAvail = cblk->framesAvailable(); |
| 3994 | |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 3995 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3996 | if (framesAvail == 0) { |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 3997 | Mutex::Autolock _l(cblk->lock); |
| 3998 | goto start_loop_here; |
| 3999 | while (framesAvail == 0) { |
| 4000 | active = mActive; |
Glenn Kasten | e80a4cc | 2011-12-15 09:51:17 -0800 | [diff] [blame] | 4001 | if (CC_UNLIKELY(!active)) { |
Steve Block | 71f2cf1 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 4002 | ALOGV("Not active and NO_MORE_BUFFERS"); |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 4003 | return AudioTrack::NO_MORE_BUFFERS; |
| 4004 | } |
| 4005 | result = cblk->cv.waitRelative(cblk->lock, milliseconds(waitTimeMs)); |
| 4006 | if (result != NO_ERROR) { |
| 4007 | return AudioTrack::NO_MORE_BUFFERS; |
| 4008 | } |
| 4009 | // read the server count again |
| 4010 | start_loop_here: |
| 4011 | framesAvail = cblk->framesAvailable_l(); |
| 4012 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4013 | } |
| 4014 | |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 4015 | // if (framesAvail < framesReq) { |
| 4016 | // return AudioTrack::NO_MORE_BUFFERS; |
| 4017 | // } |
| 4018 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4019 | if (framesReq > framesAvail) { |
| 4020 | framesReq = framesAvail; |
| 4021 | } |
| 4022 | |
| 4023 | uint32_t u = cblk->user; |
| 4024 | uint32_t bufferEnd = cblk->userBase + cblk->frameCount; |
| 4025 | |
| 4026 | if (u + framesReq > bufferEnd) { |
| 4027 | framesReq = bufferEnd - u; |
| 4028 | } |
| 4029 | |
| 4030 | buffer->frameCount = framesReq; |
| 4031 | buffer->raw = (void *)cblk->buffer(u); |
| 4032 | return NO_ERROR; |
| 4033 | } |
| 4034 | |
| 4035 | |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 4036 | void AudioFlinger::PlaybackThread::OutputTrack::clearBufferQueue() |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4037 | { |
| 4038 | size_t size = mBufferQueue.size(); |
| 4039 | Buffer *pBuffer; |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 4040 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4041 | for (size_t i = 0; i < size; i++) { |
| 4042 | pBuffer = mBufferQueue.itemAt(i); |
| 4043 | delete [] pBuffer->mBuffer; |
| 4044 | delete pBuffer; |
| 4045 | } |
| 4046 | mBufferQueue.clear(); |
| 4047 | } |
| 4048 | |
| 4049 | // ---------------------------------------------------------------------------- |
| 4050 | |
| 4051 | AudioFlinger::Client::Client(const sp<AudioFlinger>& audioFlinger, pid_t pid) |
| 4052 | : RefBase(), |
| 4053 | mAudioFlinger(audioFlinger), |
Mathias Agopian | 6faf789 | 2010-01-25 19:00:00 -0800 | [diff] [blame] | 4054 | mMemoryDealer(new MemoryDealer(1024*1024, "AudioFlinger::Client")), |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4055 | mPid(pid) |
| 4056 | { |
| 4057 | // 1 MB of address space is good for 32 tracks, 8 buffers each, 4 KB/buffer |
| 4058 | } |
| 4059 | |
Eric Laurent | b9481d8 | 2009-09-17 05:12:56 -0700 | [diff] [blame] | 4060 | // Client destructor must be called with AudioFlinger::mLock held |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4061 | AudioFlinger::Client::~Client() |
| 4062 | { |
Eric Laurent | b9481d8 | 2009-09-17 05:12:56 -0700 | [diff] [blame] | 4063 | mAudioFlinger->removeClient_l(mPid); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4064 | } |
| 4065 | |
| 4066 | const sp<MemoryDealer>& AudioFlinger::Client::heap() const |
| 4067 | { |
| 4068 | return mMemoryDealer; |
| 4069 | } |
| 4070 | |
| 4071 | // ---------------------------------------------------------------------------- |
| 4072 | |
Eric Laurent | 4f0f17d | 2010-05-12 02:05:53 -0700 | [diff] [blame] | 4073 | AudioFlinger::NotificationClient::NotificationClient(const sp<AudioFlinger>& audioFlinger, |
| 4074 | const sp<IAudioFlingerClient>& client, |
| 4075 | pid_t pid) |
| 4076 | : mAudioFlinger(audioFlinger), mPid(pid), mClient(client) |
| 4077 | { |
| 4078 | } |
| 4079 | |
| 4080 | AudioFlinger::NotificationClient::~NotificationClient() |
| 4081 | { |
| 4082 | mClient.clear(); |
| 4083 | } |
| 4084 | |
| 4085 | void AudioFlinger::NotificationClient::binderDied(const wp<IBinder>& who) |
| 4086 | { |
| 4087 | sp<NotificationClient> keep(this); |
| 4088 | { |
| 4089 | mAudioFlinger->removeNotificationClient(mPid); |
| 4090 | } |
| 4091 | } |
| 4092 | |
| 4093 | // ---------------------------------------------------------------------------- |
| 4094 | |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 4095 | AudioFlinger::TrackHandle::TrackHandle(const sp<AudioFlinger::PlaybackThread::Track>& track) |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 4096 | : BnAudioTrack(), |
| 4097 | mTrack(track) |
| 4098 | { |
| 4099 | } |
| 4100 | |
| 4101 | AudioFlinger::TrackHandle::~TrackHandle() { |
| 4102 | // just stop the track on deletion, associated resources |
| 4103 | // will be freed from the main thread once all pending buffers have |
| 4104 | // been played. Unless it's not in the active track list, in which |
| 4105 | // case we free everything now... |
| 4106 | mTrack->destroy(); |
| 4107 | } |
| 4108 | |
| 4109 | status_t AudioFlinger::TrackHandle::start() { |
| 4110 | return mTrack->start(); |
| 4111 | } |
| 4112 | |
| 4113 | void AudioFlinger::TrackHandle::stop() { |
| 4114 | mTrack->stop(); |
| 4115 | } |
| 4116 | |
| 4117 | void AudioFlinger::TrackHandle::flush() { |
| 4118 | mTrack->flush(); |
| 4119 | } |
| 4120 | |
| 4121 | void AudioFlinger::TrackHandle::mute(bool e) { |
| 4122 | mTrack->mute(e); |
| 4123 | } |
| 4124 | |
| 4125 | void AudioFlinger::TrackHandle::pause() { |
| 4126 | mTrack->pause(); |
| 4127 | } |
| 4128 | |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 4129 | sp<IMemory> AudioFlinger::TrackHandle::getCblk() const { |
| 4130 | return mTrack->getCblk(); |
| 4131 | } |
| 4132 | |
Eric Laurent | 65b6545 | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 4133 | status_t AudioFlinger::TrackHandle::attachAuxEffect(int EffectId) |
| 4134 | { |
| 4135 | return mTrack->attachAuxEffect(EffectId); |
| 4136 | } |
| 4137 | |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 4138 | status_t AudioFlinger::TrackHandle::onTransact( |
| 4139 | uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags) |
| 4140 | { |
| 4141 | return BnAudioTrack::onTransact(code, data, reply, flags); |
| 4142 | } |
| 4143 | |
| 4144 | // ---------------------------------------------------------------------------- |
| 4145 | |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 4146 | sp<IAudioRecord> AudioFlinger::openRecord( |
| 4147 | pid_t pid, |
Eric Laurent | ddb78e7 | 2009-07-28 08:44:33 -0700 | [diff] [blame] | 4148 | int input, |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 4149 | uint32_t sampleRate, |
Glenn Kasten | 0a204ed | 2012-01-12 12:27:51 -0800 | [diff] [blame^] | 4150 | audio_format_t format, |
Jean-Michel Trivi | 5439223 | 2011-05-24 15:53:33 -0700 | [diff] [blame] | 4151 | uint32_t channelMask, |
The Android Open Source Project | f013e1a | 2008-12-17 18:05:43 -0800 | [diff] [blame] | 4152 | int frameCount, |
| 4153 | uint32_t flags, |
Eric Laurent | 65b6545 | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 4154 | int *sessionId, |
The Android Open Source Project | f013e1a | 2008-12-17 18:05:43 -0800 | [diff] [blame] | 4155 | status_t *status) |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 4156 | { |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 4157 | sp<RecordThread::RecordTrack> recordTrack; |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 4158 | sp<RecordHandle> recordHandle; |
| 4159 | sp<Client> client; |
| 4160 | wp<Client> wclient; |
The Android Open Source Project | f013e1a | 2008-12-17 18:05:43 -0800 | [diff] [blame] | 4161 | status_t lStatus; |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 4162 | RecordThread *thread; |
| 4163 | size_t inFrameCount; |
Eric Laurent | 65b6545 | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 4164 | int lSessionId; |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 4165 | |
| 4166 | // check calling permissions |
| 4167 | if (!recordingAllowed()) { |
The Android Open Source Project | f013e1a | 2008-12-17 18:05:43 -0800 | [diff] [blame] | 4168 | lStatus = PERMISSION_DENIED; |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 4169 | goto Exit; |
| 4170 | } |
| 4171 | |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 4172 | // add client to list |
The Android Open Source Project | b2a3dd8 | 2009-03-09 11:52:12 -0700 | [diff] [blame] | 4173 | { // scope for mLock |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 4174 | Mutex::Autolock _l(mLock); |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 4175 | thread = checkRecordThread_l(input); |
| 4176 | if (thread == NULL) { |
| 4177 | lStatus = BAD_VALUE; |
| 4178 | goto Exit; |
| 4179 | } |
| 4180 | |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 4181 | wclient = mClients.valueFor(pid); |
| 4182 | if (wclient != NULL) { |
| 4183 | client = wclient.promote(); |
| 4184 | } else { |
| 4185 | client = new Client(this, pid); |
| 4186 | mClients.add(pid, client); |
| 4187 | } |
The Android Open Source Project | b2a3dd8 | 2009-03-09 11:52:12 -0700 | [diff] [blame] | 4188 | |
Eric Laurent | 65b6545 | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 4189 | // If no audio session id is provided, create one here |
Dima Zavin | 24fc2fb | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 4190 | if (sessionId != NULL && *sessionId != AUDIO_SESSION_OUTPUT_MIX) { |
Eric Laurent | 65b6545 | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 4191 | lSessionId = *sessionId; |
| 4192 | } else { |
Eric Laurent | 464d5b3 | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 4193 | lSessionId = nextUniqueId(); |
Eric Laurent | 65b6545 | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 4194 | if (sessionId != NULL) { |
| 4195 | *sessionId = lSessionId; |
| 4196 | } |
| 4197 | } |
The Android Open Source Project | b2a3dd8 | 2009-03-09 11:52:12 -0700 | [diff] [blame] | 4198 | // create new record track. The record track uses one track in mHardwareMixerThread by convention. |
Eric Laurent | 464d5b3 | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 4199 | recordTrack = thread->createRecordTrack_l(client, |
| 4200 | sampleRate, |
| 4201 | format, |
| 4202 | channelMask, |
| 4203 | frameCount, |
| 4204 | flags, |
| 4205 | lSessionId, |
| 4206 | &lStatus); |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 4207 | } |
Eric Laurent | 464d5b3 | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 4208 | if (lStatus != NO_ERROR) { |
Eric Laurent | b9481d8 | 2009-09-17 05:12:56 -0700 | [diff] [blame] | 4209 | // remove local strong reference to Client before deleting the RecordTrack so that the Client |
| 4210 | // destructor is called by the TrackBase destructor with mLock held |
| 4211 | client.clear(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4212 | recordTrack.clear(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4213 | goto Exit; |
| 4214 | } |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 4215 | |
| 4216 | // return to handle to client |
| 4217 | recordHandle = new RecordHandle(recordTrack); |
The Android Open Source Project | f013e1a | 2008-12-17 18:05:43 -0800 | [diff] [blame] | 4218 | lStatus = NO_ERROR; |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 4219 | |
| 4220 | Exit: |
The Android Open Source Project | f013e1a | 2008-12-17 18:05:43 -0800 | [diff] [blame] | 4221 | if (status) { |
| 4222 | *status = lStatus; |
| 4223 | } |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 4224 | return recordHandle; |
| 4225 | } |
| 4226 | |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 4227 | // ---------------------------------------------------------------------------- |
| 4228 | |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 4229 | AudioFlinger::RecordHandle::RecordHandle(const sp<AudioFlinger::RecordThread::RecordTrack>& recordTrack) |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 4230 | : BnAudioRecord(), |
| 4231 | mRecordTrack(recordTrack) |
| 4232 | { |
| 4233 | } |
| 4234 | |
The Android Open Source Project | f013e1a | 2008-12-17 18:05:43 -0800 | [diff] [blame] | 4235 | AudioFlinger::RecordHandle::~RecordHandle() { |
| 4236 | stop(); |
| 4237 | } |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 4238 | |
| 4239 | status_t AudioFlinger::RecordHandle::start() { |
Steve Block | 71f2cf1 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 4240 | ALOGV("RecordHandle::start()"); |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 4241 | return mRecordTrack->start(); |
| 4242 | } |
| 4243 | |
| 4244 | void AudioFlinger::RecordHandle::stop() { |
Steve Block | 71f2cf1 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 4245 | ALOGV("RecordHandle::stop()"); |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 4246 | mRecordTrack->stop(); |
| 4247 | } |
| 4248 | |
| 4249 | sp<IMemory> AudioFlinger::RecordHandle::getCblk() const { |
| 4250 | return mRecordTrack->getCblk(); |
| 4251 | } |
| 4252 | |
| 4253 | status_t AudioFlinger::RecordHandle::onTransact( |
| 4254 | uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags) |
| 4255 | { |
| 4256 | return BnAudioRecord::onTransact(code, data, reply, flags); |
| 4257 | } |
| 4258 | |
| 4259 | // ---------------------------------------------------------------------------- |
| 4260 | |
Eric Laurent | 464d5b3 | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 4261 | AudioFlinger::RecordThread::RecordThread(const sp<AudioFlinger>& audioFlinger, |
| 4262 | AudioStreamIn *input, |
| 4263 | uint32_t sampleRate, |
| 4264 | uint32_t channels, |
| 4265 | int id, |
| 4266 | uint32_t device) : |
| 4267 | ThreadBase(audioFlinger, id, device), |
Glenn Kasten | c434c90 | 2011-12-13 11:53:26 -0800 | [diff] [blame] | 4268 | mInput(input), mTrack(NULL), mResampler(NULL), mRsmpOutBuffer(NULL), mRsmpInBuffer(NULL) |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 4269 | { |
Eric Laurent | 464d5b3 | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 4270 | mType = ThreadBase::RECORD; |
Eric Laurent | 6dbdc40 | 2011-07-22 09:04:31 -0700 | [diff] [blame] | 4271 | |
| 4272 | snprintf(mName, kNameLength, "AudioIn_%d", id); |
| 4273 | |
Dima Zavin | 24fc2fb | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 4274 | mReqChannelCount = popcount(channels); |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 4275 | mReqSampleRate = sampleRate; |
| 4276 | readInputParameters(); |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 4277 | } |
| 4278 | |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 4279 | |
| 4280 | AudioFlinger::RecordThread::~RecordThread() |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 4281 | { |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 4282 | delete[] mRsmpInBuffer; |
Glenn Kasten | c434c90 | 2011-12-13 11:53:26 -0800 | [diff] [blame] | 4283 | if (mResampler != NULL) { |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 4284 | delete mResampler; |
| 4285 | delete[] mRsmpOutBuffer; |
| 4286 | } |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 4287 | } |
| 4288 | |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 4289 | void AudioFlinger::RecordThread::onFirstRef() |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 4290 | { |
Eric Laurent | 6dbdc40 | 2011-07-22 09:04:31 -0700 | [diff] [blame] | 4291 | run(mName, PRIORITY_URGENT_AUDIO); |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 4292 | } |
Eric Laurent | 49f02be | 2009-11-19 09:00:56 -0800 | [diff] [blame] | 4293 | |
Eric Laurent | 828b977 | 2011-08-07 16:32:26 -0700 | [diff] [blame] | 4294 | status_t AudioFlinger::RecordThread::readyToRun() |
| 4295 | { |
| 4296 | status_t status = initCheck(); |
Steve Block | 8564c8d | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 4297 | ALOGW_IF(status != NO_ERROR,"RecordThread %p could not initialize", this); |
Eric Laurent | 828b977 | 2011-08-07 16:32:26 -0700 | [diff] [blame] | 4298 | return status; |
| 4299 | } |
| 4300 | |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 4301 | bool AudioFlinger::RecordThread::threadLoop() |
| 4302 | { |
The Android Open Source Project | f013e1a | 2008-12-17 18:05:43 -0800 | [diff] [blame] | 4303 | AudioBufferProvider::Buffer buffer; |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 4304 | sp<RecordTrack> activeTrack; |
Eric Laurent | 464d5b3 | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 4305 | Vector< sp<EffectChain> > effectChains; |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 4306 | |
Eric Laurent | 4712baa | 2010-09-30 16:12:31 -0700 | [diff] [blame] | 4307 | nsecs_t lastWarning = 0; |
| 4308 | |
Eric Laurent | 6dbdc40 | 2011-07-22 09:04:31 -0700 | [diff] [blame] | 4309 | acquireWakeLock(); |
| 4310 | |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 4311 | // start recording |
| 4312 | while (!exitPending()) { |
The Android Open Source Project | f013e1a | 2008-12-17 18:05:43 -0800 | [diff] [blame] | 4313 | |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 4314 | processConfigEvents(); |
| 4315 | |
| 4316 | { // scope for mLock |
| 4317 | Mutex::Autolock _l(mLock); |
| 4318 | checkForNewParameters_l(); |
| 4319 | if (mActiveTrack == 0 && mConfigEvents.isEmpty()) { |
| 4320 | if (!mStandby) { |
Dima Zavin | 31f18889 | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 4321 | mInput->stream->common.standby(&mInput->stream->common); |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 4322 | mStandby = true; |
| 4323 | } |
| 4324 | |
| 4325 | if (exitPending()) break; |
| 4326 | |
Eric Laurent | 6dbdc40 | 2011-07-22 09:04:31 -0700 | [diff] [blame] | 4327 | releaseWakeLock_l(); |
Steve Block | 71f2cf1 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 4328 | ALOGV("RecordThread: loop stopping"); |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 4329 | // go to sleep |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 4330 | mWaitWorkCV.wait(mLock); |
Steve Block | 71f2cf1 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 4331 | ALOGV("RecordThread: loop starting"); |
Eric Laurent | 6dbdc40 | 2011-07-22 09:04:31 -0700 | [diff] [blame] | 4332 | acquireWakeLock_l(); |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 4333 | continue; |
| 4334 | } |
| 4335 | if (mActiveTrack != 0) { |
| 4336 | if (mActiveTrack->mState == TrackBase::PAUSING) { |
Eric Laurent | 9cc489a | 2009-12-05 05:20:01 -0800 | [diff] [blame] | 4337 | if (!mStandby) { |
Dima Zavin | 31f18889 | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 4338 | mInput->stream->common.standby(&mInput->stream->common); |
Eric Laurent | 9cc489a | 2009-12-05 05:20:01 -0800 | [diff] [blame] | 4339 | mStandby = true; |
| 4340 | } |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 4341 | mActiveTrack.clear(); |
| 4342 | mStartStopCond.broadcast(); |
| 4343 | } else if (mActiveTrack->mState == TrackBase::RESUMING) { |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 4344 | if (mReqChannelCount != mActiveTrack->channelCount()) { |
| 4345 | mActiveTrack.clear(); |
Eric Laurent | 9cc489a | 2009-12-05 05:20:01 -0800 | [diff] [blame] | 4346 | mStartStopCond.broadcast(); |
| 4347 | } else if (mBytesRead != 0) { |
| 4348 | // record start succeeds only if first read from audio input |
| 4349 | // succeeds |
| 4350 | if (mBytesRead > 0) { |
| 4351 | mActiveTrack->mState = TrackBase::ACTIVE; |
| 4352 | } else { |
| 4353 | mActiveTrack.clear(); |
| 4354 | } |
| 4355 | mStartStopCond.broadcast(); |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 4356 | } |
Eric Laurent | 9cc489a | 2009-12-05 05:20:01 -0800 | [diff] [blame] | 4357 | mStandby = false; |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 4358 | } |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 4359 | } |
Eric Laurent | 464d5b3 | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 4360 | lockEffectChains_l(effectChains); |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 4361 | } |
| 4362 | |
| 4363 | if (mActiveTrack != 0) { |
Eric Laurent | 9cc489a | 2009-12-05 05:20:01 -0800 | [diff] [blame] | 4364 | if (mActiveTrack->mState != TrackBase::ACTIVE && |
| 4365 | mActiveTrack->mState != TrackBase::RESUMING) { |
Eric Laurent | 464d5b3 | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 4366 | unlockEffectChains(effectChains); |
| 4367 | usleep(kRecordThreadSleepUs); |
Eric Laurent | 49f02be | 2009-11-19 09:00:56 -0800 | [diff] [blame] | 4368 | continue; |
| 4369 | } |
Eric Laurent | 464d5b3 | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 4370 | for (size_t i = 0; i < effectChains.size(); i ++) { |
| 4371 | effectChains[i]->process_l(); |
| 4372 | } |
Eric Laurent | 464d5b3 | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 4373 | |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 4374 | buffer.frameCount = mFrameCount; |
Glenn Kasten | e80a4cc | 2011-12-15 09:51:17 -0800 | [diff] [blame] | 4375 | if (CC_LIKELY(mActiveTrack->getNextBuffer(&buffer) == NO_ERROR)) { |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 4376 | size_t framesOut = buffer.frameCount; |
Glenn Kasten | c434c90 | 2011-12-13 11:53:26 -0800 | [diff] [blame] | 4377 | if (mResampler == NULL) { |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 4378 | // no resampling |
| 4379 | while (framesOut) { |
| 4380 | size_t framesIn = mFrameCount - mRsmpInIndex; |
| 4381 | if (framesIn) { |
| 4382 | int8_t *src = (int8_t *)mRsmpInBuffer + mRsmpInIndex * mFrameSize; |
| 4383 | int8_t *dst = buffer.i8 + (buffer.frameCount - framesOut) * mActiveTrack->mCblk->frameSize; |
| 4384 | if (framesIn > framesOut) |
| 4385 | framesIn = framesOut; |
| 4386 | mRsmpInIndex += framesIn; |
| 4387 | framesOut -= framesIn; |
Eric Laurent | b0a0147 | 2010-05-14 05:45:46 -0700 | [diff] [blame] | 4388 | if ((int)mChannelCount == mReqChannelCount || |
Dima Zavin | 24fc2fb | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 4389 | mFormat != AUDIO_FORMAT_PCM_16_BIT) { |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 4390 | memcpy(dst, src, framesIn * mFrameSize); |
| 4391 | } else { |
| 4392 | int16_t *src16 = (int16_t *)src; |
| 4393 | int16_t *dst16 = (int16_t *)dst; |
| 4394 | if (mChannelCount == 1) { |
| 4395 | while (framesIn--) { |
| 4396 | *dst16++ = *src16; |
| 4397 | *dst16++ = *src16++; |
| 4398 | } |
| 4399 | } else { |
| 4400 | while (framesIn--) { |
| 4401 | *dst16++ = (int16_t)(((int32_t)*src16 + (int32_t)*(src16 + 1)) >> 1); |
| 4402 | src16 += 2; |
| 4403 | } |
| 4404 | } |
| 4405 | } |
| 4406 | } |
| 4407 | if (framesOut && mFrameCount == mRsmpInIndex) { |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 4408 | if (framesOut == mFrameCount && |
Dima Zavin | 24fc2fb | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 4409 | ((int)mChannelCount == mReqChannelCount || mFormat != AUDIO_FORMAT_PCM_16_BIT)) { |
Dima Zavin | 31f18889 | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 4410 | mBytesRead = mInput->stream->read(mInput->stream, buffer.raw, mInputBytes); |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 4411 | framesOut = 0; |
| 4412 | } else { |
Dima Zavin | 31f18889 | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 4413 | mBytesRead = mInput->stream->read(mInput->stream, mRsmpInBuffer, mInputBytes); |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 4414 | mRsmpInIndex = 0; |
| 4415 | } |
Eric Laurent | 9cc489a | 2009-12-05 05:20:01 -0800 | [diff] [blame] | 4416 | if (mBytesRead < 0) { |
Steve Block | 3762c31 | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 4417 | ALOGE("Error reading audio input"); |
Eric Laurent | 9cc489a | 2009-12-05 05:20:01 -0800 | [diff] [blame] | 4418 | if (mActiveTrack->mState == TrackBase::ACTIVE) { |
Eric Laurent | ba8811f | 2010-03-02 18:38:06 -0800 | [diff] [blame] | 4419 | // Force input into standby so that it tries to |
| 4420 | // recover at next read attempt |
Dima Zavin | 31f18889 | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 4421 | mInput->stream->common.standby(&mInput->stream->common); |
Eric Laurent | 464d5b3 | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 4422 | usleep(kRecordThreadSleepUs); |
Eric Laurent | 9cc489a | 2009-12-05 05:20:01 -0800 | [diff] [blame] | 4423 | } |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 4424 | mRsmpInIndex = mFrameCount; |
| 4425 | framesOut = 0; |
| 4426 | buffer.frameCount = 0; |
| 4427 | } |
| 4428 | } |
The Android Open Source Project | f013e1a | 2008-12-17 18:05:43 -0800 | [diff] [blame] | 4429 | } |
| 4430 | } else { |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 4431 | // resampling |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 4432 | |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 4433 | memset(mRsmpOutBuffer, 0, framesOut * 2 * sizeof(int32_t)); |
| 4434 | // alter output frame count as if we were expecting stereo samples |
| 4435 | if (mChannelCount == 1 && mReqChannelCount == 1) { |
| 4436 | framesOut >>= 1; |
| 4437 | } |
| 4438 | mResampler->resample(mRsmpOutBuffer, framesOut, this); |
| 4439 | // ditherAndClamp() works as long as all buffers returned by mActiveTrack->getNextBuffer() |
| 4440 | // are 32 bit aligned which should be always true. |
| 4441 | if (mChannelCount == 2 && mReqChannelCount == 1) { |
Glenn Kasten | 490909d | 2011-12-15 09:52:39 -0800 | [diff] [blame] | 4442 | ditherAndClamp(mRsmpOutBuffer, mRsmpOutBuffer, framesOut); |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 4443 | // the resampler always outputs stereo samples: do post stereo to mono conversion |
| 4444 | int16_t *src = (int16_t *)mRsmpOutBuffer; |
| 4445 | int16_t *dst = buffer.i16; |
| 4446 | while (framesOut--) { |
| 4447 | *dst++ = (int16_t)(((int32_t)*src + (int32_t)*(src + 1)) >> 1); |
| 4448 | src += 2; |
| 4449 | } |
| 4450 | } else { |
Glenn Kasten | 490909d | 2011-12-15 09:52:39 -0800 | [diff] [blame] | 4451 | ditherAndClamp((int32_t *)buffer.raw, mRsmpOutBuffer, framesOut); |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 4452 | } |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 4453 | |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 4454 | } |
| 4455 | mActiveTrack->releaseBuffer(&buffer); |
| 4456 | mActiveTrack->overflow(); |
| 4457 | } |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 4458 | // client isn't retrieving buffers fast enough |
| 4459 | else { |
Eric Laurent | 4712baa | 2010-09-30 16:12:31 -0700 | [diff] [blame] | 4460 | if (!mActiveTrack->setOverflow()) { |
| 4461 | nsecs_t now = systemTime(); |
Glenn Kasten | cbfe2e1 | 2011-12-13 11:04:14 -0800 | [diff] [blame] | 4462 | if ((now - lastWarning) > kWarningThrottleNs) { |
Steve Block | 8564c8d | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 4463 | ALOGW("RecordThread: buffer overflow"); |
Eric Laurent | 4712baa | 2010-09-30 16:12:31 -0700 | [diff] [blame] | 4464 | lastWarning = now; |
| 4465 | } |
| 4466 | } |
The Android Open Source Project | f013e1a | 2008-12-17 18:05:43 -0800 | [diff] [blame] | 4467 | // Release the processor for a while before asking for a new buffer. |
| 4468 | // This will give the application more chance to read from the buffer and |
| 4469 | // clear the overflow. |
Eric Laurent | 464d5b3 | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 4470 | usleep(kRecordThreadSleepUs); |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 4471 | } |
| 4472 | } |
Eric Laurent | 21b5c47 | 2011-07-26 20:54:46 -0700 | [diff] [blame] | 4473 | // enable changes in effect chain |
| 4474 | unlockEffectChains(effectChains); |
Eric Laurent | 464d5b3 | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 4475 | effectChains.clear(); |
The Android Open Source Project | f013e1a | 2008-12-17 18:05:43 -0800 | [diff] [blame] | 4476 | } |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 4477 | |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 4478 | if (!mStandby) { |
Dima Zavin | 31f18889 | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 4479 | mInput->stream->common.standby(&mInput->stream->common); |
The Android Open Source Project | f013e1a | 2008-12-17 18:05:43 -0800 | [diff] [blame] | 4480 | } |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 4481 | mActiveTrack.clear(); |
| 4482 | |
Eric Laurent | 49f02be | 2009-11-19 09:00:56 -0800 | [diff] [blame] | 4483 | mStartStopCond.broadcast(); |
| 4484 | |
Eric Laurent | 6dbdc40 | 2011-07-22 09:04:31 -0700 | [diff] [blame] | 4485 | releaseWakeLock(); |
| 4486 | |
Steve Block | 71f2cf1 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 4487 | ALOGV("RecordThread %p exiting", this); |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 4488 | return false; |
| 4489 | } |
| 4490 | |
Eric Laurent | 464d5b3 | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 4491 | |
| 4492 | sp<AudioFlinger::RecordThread::RecordTrack> AudioFlinger::RecordThread::createRecordTrack_l( |
| 4493 | const sp<AudioFlinger::Client>& client, |
| 4494 | uint32_t sampleRate, |
Glenn Kasten | 0a204ed | 2012-01-12 12:27:51 -0800 | [diff] [blame^] | 4495 | audio_format_t format, |
Eric Laurent | 464d5b3 | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 4496 | int channelMask, |
| 4497 | int frameCount, |
| 4498 | uint32_t flags, |
| 4499 | int sessionId, |
| 4500 | status_t *status) |
| 4501 | { |
| 4502 | sp<RecordTrack> track; |
| 4503 | status_t lStatus; |
| 4504 | |
| 4505 | lStatus = initCheck(); |
| 4506 | if (lStatus != NO_ERROR) { |
Steve Block | 3762c31 | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 4507 | ALOGE("Audio driver not initialized."); |
Eric Laurent | 464d5b3 | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 4508 | goto Exit; |
| 4509 | } |
| 4510 | |
| 4511 | { // scope for mLock |
| 4512 | Mutex::Autolock _l(mLock); |
| 4513 | |
| 4514 | track = new RecordTrack(this, client, sampleRate, |
| 4515 | format, channelMask, frameCount, flags, sessionId); |
| 4516 | |
| 4517 | if (track->getCblk() == NULL) { |
| 4518 | lStatus = NO_MEMORY; |
| 4519 | goto Exit; |
| 4520 | } |
| 4521 | |
| 4522 | mTrack = track.get(); |
Eric Laurent | 6639b55 | 2011-08-01 09:52:20 -0700 | [diff] [blame] | 4523 | // disable AEC and NS if the device is a BT SCO headset supporting those pre processings |
| 4524 | bool suspend = audio_is_bluetooth_sco_device( |
Eric Laurent | 2d95dfb | 2011-08-29 12:42:48 -0700 | [diff] [blame] | 4525 | (audio_devices_t)(mDevice & AUDIO_DEVICE_IN_ALL)) && mAudioFlinger->btNrecIsOff(); |
Eric Laurent | 6639b55 | 2011-08-01 09:52:20 -0700 | [diff] [blame] | 4526 | setEffectSuspended_l(FX_IID_AEC, suspend, sessionId); |
| 4527 | setEffectSuspended_l(FX_IID_NS, suspend, sessionId); |
Eric Laurent | 464d5b3 | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 4528 | } |
| 4529 | lStatus = NO_ERROR; |
| 4530 | |
| 4531 | Exit: |
| 4532 | if (status) { |
| 4533 | *status = lStatus; |
| 4534 | } |
| 4535 | return track; |
| 4536 | } |
| 4537 | |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 4538 | status_t AudioFlinger::RecordThread::start(RecordThread::RecordTrack* recordTrack) |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 4539 | { |
Steve Block | 71f2cf1 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 4540 | ALOGV("RecordThread::start"); |
Eric Laurent | 49f02be | 2009-11-19 09:00:56 -0800 | [diff] [blame] | 4541 | sp <ThreadBase> strongMe = this; |
| 4542 | status_t status = NO_ERROR; |
| 4543 | { |
Glenn Kasten | 325ee1c | 2012-01-05 15:41:56 -0800 | [diff] [blame] | 4544 | AutoMutex lock(mLock); |
Eric Laurent | 49f02be | 2009-11-19 09:00:56 -0800 | [diff] [blame] | 4545 | if (mActiveTrack != 0) { |
| 4546 | if (recordTrack != mActiveTrack.get()) { |
| 4547 | status = -EBUSY; |
| 4548 | } else if (mActiveTrack->mState == TrackBase::PAUSING) { |
Eric Laurent | 9cc489a | 2009-12-05 05:20:01 -0800 | [diff] [blame] | 4549 | mActiveTrack->mState = TrackBase::ACTIVE; |
Eric Laurent | 49f02be | 2009-11-19 09:00:56 -0800 | [diff] [blame] | 4550 | } |
| 4551 | return status; |
| 4552 | } |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 4553 | |
Eric Laurent | 49f02be | 2009-11-19 09:00:56 -0800 | [diff] [blame] | 4554 | recordTrack->mState = TrackBase::IDLE; |
| 4555 | mActiveTrack = recordTrack; |
| 4556 | mLock.unlock(); |
| 4557 | status_t status = AudioSystem::startInput(mId); |
| 4558 | mLock.lock(); |
| 4559 | if (status != NO_ERROR) { |
| 4560 | mActiveTrack.clear(); |
| 4561 | return status; |
| 4562 | } |
Eric Laurent | 9cc489a | 2009-12-05 05:20:01 -0800 | [diff] [blame] | 4563 | mRsmpInIndex = mFrameCount; |
| 4564 | mBytesRead = 0; |
Eric Laurent | 4bb21c4 | 2011-02-28 16:52:51 -0800 | [diff] [blame] | 4565 | if (mResampler != NULL) { |
| 4566 | mResampler->reset(); |
| 4567 | } |
| 4568 | mActiveTrack->mState = TrackBase::RESUMING; |
Eric Laurent | 49f02be | 2009-11-19 09:00:56 -0800 | [diff] [blame] | 4569 | // signal thread to start |
Steve Block | 71f2cf1 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 4570 | ALOGV("Signal record thread"); |
Eric Laurent | 49f02be | 2009-11-19 09:00:56 -0800 | [diff] [blame] | 4571 | mWaitWorkCV.signal(); |
| 4572 | // do not wait for mStartStopCond if exiting |
| 4573 | if (mExiting) { |
| 4574 | mActiveTrack.clear(); |
| 4575 | status = INVALID_OPERATION; |
| 4576 | goto startError; |
| 4577 | } |
| 4578 | mStartStopCond.wait(mLock); |
| 4579 | if (mActiveTrack == 0) { |
Steve Block | 71f2cf1 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 4580 | ALOGV("Record failed to start"); |
Eric Laurent | 49f02be | 2009-11-19 09:00:56 -0800 | [diff] [blame] | 4581 | status = BAD_VALUE; |
| 4582 | goto startError; |
| 4583 | } |
Steve Block | 71f2cf1 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 4584 | ALOGV("Record started OK"); |
Eric Laurent | 49f02be | 2009-11-19 09:00:56 -0800 | [diff] [blame] | 4585 | return status; |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 4586 | } |
Eric Laurent | 49f02be | 2009-11-19 09:00:56 -0800 | [diff] [blame] | 4587 | startError: |
| 4588 | AudioSystem::stopInput(mId); |
| 4589 | return status; |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 4590 | } |
| 4591 | |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 4592 | void AudioFlinger::RecordThread::stop(RecordThread::RecordTrack* recordTrack) { |
Steve Block | 71f2cf1 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 4593 | ALOGV("RecordThread::stop"); |
Eric Laurent | 49f02be | 2009-11-19 09:00:56 -0800 | [diff] [blame] | 4594 | sp <ThreadBase> strongMe = this; |
| 4595 | { |
Glenn Kasten | 325ee1c | 2012-01-05 15:41:56 -0800 | [diff] [blame] | 4596 | AutoMutex lock(mLock); |
Eric Laurent | 49f02be | 2009-11-19 09:00:56 -0800 | [diff] [blame] | 4597 | if (mActiveTrack != 0 && recordTrack == mActiveTrack.get()) { |
| 4598 | mActiveTrack->mState = TrackBase::PAUSING; |
| 4599 | // do not wait for mStartStopCond if exiting |
| 4600 | if (mExiting) { |
| 4601 | return; |
| 4602 | } |
| 4603 | mStartStopCond.wait(mLock); |
| 4604 | // if we have been restarted, recordTrack == mActiveTrack.get() here |
| 4605 | if (mActiveTrack == 0 || recordTrack != mActiveTrack.get()) { |
| 4606 | mLock.unlock(); |
| 4607 | AudioSystem::stopInput(mId); |
| 4608 | mLock.lock(); |
Steve Block | 71f2cf1 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 4609 | ALOGV("Record stopped OK"); |
Eric Laurent | 49f02be | 2009-11-19 09:00:56 -0800 | [diff] [blame] | 4610 | } |
| 4611 | } |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 4612 | } |
| 4613 | } |
| 4614 | |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 4615 | status_t AudioFlinger::RecordThread::dump(int fd, const Vector<String16>& args) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4616 | { |
| 4617 | const size_t SIZE = 256; |
| 4618 | char buffer[SIZE]; |
| 4619 | String8 result; |
| 4620 | pid_t pid = 0; |
| 4621 | |
Eric Laurent | 3fdb126 | 2009-11-07 00:01:32 -0800 | [diff] [blame] | 4622 | snprintf(buffer, SIZE, "\nInput thread %p internals\n", this); |
| 4623 | result.append(buffer); |
| 4624 | |
| 4625 | if (mActiveTrack != 0) { |
| 4626 | result.append("Active Track:\n"); |
Jean-Michel Trivi | 5439223 | 2011-05-24 15:53:33 -0700 | [diff] [blame] | 4627 | result.append(" Clien Fmt Chn mask Session Buf S SRate Serv User\n"); |
Eric Laurent | 3fdb126 | 2009-11-07 00:01:32 -0800 | [diff] [blame] | 4628 | mActiveTrack->dump(buffer, SIZE); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4629 | result.append(buffer); |
Eric Laurent | 3fdb126 | 2009-11-07 00:01:32 -0800 | [diff] [blame] | 4630 | |
| 4631 | snprintf(buffer, SIZE, "In index: %d\n", mRsmpInIndex); |
| 4632 | result.append(buffer); |
| 4633 | snprintf(buffer, SIZE, "In size: %d\n", mInputBytes); |
| 4634 | result.append(buffer); |
Glenn Kasten | c434c90 | 2011-12-13 11:53:26 -0800 | [diff] [blame] | 4635 | snprintf(buffer, SIZE, "Resampling: %d\n", (mResampler != NULL)); |
Eric Laurent | 3fdb126 | 2009-11-07 00:01:32 -0800 | [diff] [blame] | 4636 | result.append(buffer); |
| 4637 | snprintf(buffer, SIZE, "Out channel count: %d\n", mReqChannelCount); |
| 4638 | result.append(buffer); |
| 4639 | snprintf(buffer, SIZE, "Out sample rate: %d\n", mReqSampleRate); |
| 4640 | result.append(buffer); |
| 4641 | |
| 4642 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4643 | } else { |
| 4644 | result.append("No record client\n"); |
| 4645 | } |
| 4646 | write(fd, result.string(), result.size()); |
Eric Laurent | 3fdb126 | 2009-11-07 00:01:32 -0800 | [diff] [blame] | 4647 | |
| 4648 | dumpBase(fd, args); |
Eric Laurent | 1345d33 | 2011-07-24 17:49:51 -0700 | [diff] [blame] | 4649 | dumpEffectChains(fd, args); |
Eric Laurent | 3fdb126 | 2009-11-07 00:01:32 -0800 | [diff] [blame] | 4650 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4651 | return NO_ERROR; |
| 4652 | } |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 4653 | |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 4654 | status_t AudioFlinger::RecordThread::getNextBuffer(AudioBufferProvider::Buffer* buffer) |
| 4655 | { |
| 4656 | size_t framesReq = buffer->frameCount; |
| 4657 | size_t framesReady = mFrameCount - mRsmpInIndex; |
| 4658 | int channelCount; |
| 4659 | |
| 4660 | if (framesReady == 0) { |
Dima Zavin | 31f18889 | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 4661 | mBytesRead = mInput->stream->read(mInput->stream, mRsmpInBuffer, mInputBytes); |
Eric Laurent | 9cc489a | 2009-12-05 05:20:01 -0800 | [diff] [blame] | 4662 | if (mBytesRead < 0) { |
Steve Block | 3762c31 | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 4663 | ALOGE("RecordThread::getNextBuffer() Error reading audio input"); |
Eric Laurent | 9cc489a | 2009-12-05 05:20:01 -0800 | [diff] [blame] | 4664 | if (mActiveTrack->mState == TrackBase::ACTIVE) { |
Eric Laurent | ba8811f | 2010-03-02 18:38:06 -0800 | [diff] [blame] | 4665 | // Force input into standby so that it tries to |
| 4666 | // recover at next read attempt |
Dima Zavin | 31f18889 | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 4667 | mInput->stream->common.standby(&mInput->stream->common); |
Eric Laurent | 464d5b3 | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 4668 | usleep(kRecordThreadSleepUs); |
Eric Laurent | 9cc489a | 2009-12-05 05:20:01 -0800 | [diff] [blame] | 4669 | } |
Glenn Kasten | c434c90 | 2011-12-13 11:53:26 -0800 | [diff] [blame] | 4670 | buffer->raw = NULL; |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 4671 | buffer->frameCount = 0; |
| 4672 | return NOT_ENOUGH_DATA; |
| 4673 | } |
| 4674 | mRsmpInIndex = 0; |
| 4675 | framesReady = mFrameCount; |
| 4676 | } |
| 4677 | |
| 4678 | if (framesReq > framesReady) { |
| 4679 | framesReq = framesReady; |
| 4680 | } |
| 4681 | |
| 4682 | if (mChannelCount == 1 && mReqChannelCount == 2) { |
| 4683 | channelCount = 1; |
| 4684 | } else { |
| 4685 | channelCount = 2; |
| 4686 | } |
| 4687 | buffer->raw = mRsmpInBuffer + mRsmpInIndex * channelCount; |
| 4688 | buffer->frameCount = framesReq; |
| 4689 | return NO_ERROR; |
| 4690 | } |
| 4691 | |
| 4692 | void AudioFlinger::RecordThread::releaseBuffer(AudioBufferProvider::Buffer* buffer) |
| 4693 | { |
| 4694 | mRsmpInIndex += buffer->frameCount; |
| 4695 | buffer->frameCount = 0; |
| 4696 | } |
| 4697 | |
| 4698 | bool AudioFlinger::RecordThread::checkForNewParameters_l() |
| 4699 | { |
| 4700 | bool reconfig = false; |
| 4701 | |
Eric Laurent | 8fce46a | 2009-08-04 09:45:33 -0700 | [diff] [blame] | 4702 | while (!mNewParameters.isEmpty()) { |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 4703 | status_t status = NO_ERROR; |
Eric Laurent | 8fce46a | 2009-08-04 09:45:33 -0700 | [diff] [blame] | 4704 | String8 keyValuePair = mNewParameters[0]; |
| 4705 | AudioParameter param = AudioParameter(keyValuePair); |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 4706 | int value; |
Glenn Kasten | 0a204ed | 2012-01-12 12:27:51 -0800 | [diff] [blame^] | 4707 | audio_format_t reqFormat = mFormat; |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 4708 | int reqSamplingRate = mReqSampleRate; |
| 4709 | int reqChannelCount = mReqChannelCount; |
Eric Laurent | 8fce46a | 2009-08-04 09:45:33 -0700 | [diff] [blame] | 4710 | |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 4711 | if (param.getInt(String8(AudioParameter::keySamplingRate), value) == NO_ERROR) { |
| 4712 | reqSamplingRate = value; |
| 4713 | reconfig = true; |
| 4714 | } |
| 4715 | if (param.getInt(String8(AudioParameter::keyFormat), value) == NO_ERROR) { |
Glenn Kasten | 0a204ed | 2012-01-12 12:27:51 -0800 | [diff] [blame^] | 4716 | reqFormat = (audio_format_t) value; |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 4717 | reconfig = true; |
| 4718 | } |
| 4719 | if (param.getInt(String8(AudioParameter::keyChannels), value) == NO_ERROR) { |
Dima Zavin | 24fc2fb | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 4720 | reqChannelCount = popcount(value); |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 4721 | reconfig = true; |
| 4722 | } |
| 4723 | if (param.getInt(String8(AudioParameter::keyFrameCount), value) == NO_ERROR) { |
| 4724 | // do not accept frame count changes if tracks are open as the track buffer |
| 4725 | // size depends on frame count and correct behavior would not be garantied |
| 4726 | // if frame count is changed after track creation |
| 4727 | if (mActiveTrack != 0) { |
| 4728 | status = INVALID_OPERATION; |
| 4729 | } else { |
| 4730 | reconfig = true; |
| 4731 | } |
| 4732 | } |
Eric Laurent | 464d5b3 | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 4733 | if (param.getInt(String8(AudioParameter::keyRouting), value) == NO_ERROR) { |
| 4734 | // forward device change to effects that have requested to be |
| 4735 | // aware of attached audio device. |
| 4736 | for (size_t i = 0; i < mEffectChains.size(); i++) { |
| 4737 | mEffectChains[i]->setDevice_l(value); |
| 4738 | } |
| 4739 | // store input device and output device but do not forward output device to audio HAL. |
| 4740 | // Note that status is ignored by the caller for output device |
| 4741 | // (see AudioFlinger::setParameters() |
| 4742 | if (value & AUDIO_DEVICE_OUT_ALL) { |
| 4743 | mDevice &= (uint32_t)~(value & AUDIO_DEVICE_OUT_ALL); |
| 4744 | status = BAD_VALUE; |
| 4745 | } else { |
| 4746 | mDevice &= (uint32_t)~(value & AUDIO_DEVICE_IN_ALL); |
Eric Laurent | 6639b55 | 2011-08-01 09:52:20 -0700 | [diff] [blame] | 4747 | // disable AEC and NS if the device is a BT SCO headset supporting those pre processings |
| 4748 | if (mTrack != NULL) { |
| 4749 | bool suspend = audio_is_bluetooth_sco_device( |
Eric Laurent | 2d95dfb | 2011-08-29 12:42:48 -0700 | [diff] [blame] | 4750 | (audio_devices_t)value) && mAudioFlinger->btNrecIsOff(); |
Eric Laurent | 6639b55 | 2011-08-01 09:52:20 -0700 | [diff] [blame] | 4751 | setEffectSuspended_l(FX_IID_AEC, suspend, mTrack->sessionId()); |
| 4752 | setEffectSuspended_l(FX_IID_NS, suspend, mTrack->sessionId()); |
| 4753 | } |
Eric Laurent | 464d5b3 | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 4754 | } |
| 4755 | mDevice |= (uint32_t)value; |
| 4756 | } |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 4757 | if (status == NO_ERROR) { |
Dima Zavin | 31f18889 | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 4758 | status = mInput->stream->common.set_parameters(&mInput->stream->common, keyValuePair.string()); |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 4759 | if (status == INVALID_OPERATION) { |
Dima Zavin | 31f18889 | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 4760 | mInput->stream->common.standby(&mInput->stream->common); |
| 4761 | status = mInput->stream->common.set_parameters(&mInput->stream->common, keyValuePair.string()); |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 4762 | } |
| 4763 | if (reconfig) { |
| 4764 | if (status == BAD_VALUE && |
Dima Zavin | 31f18889 | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 4765 | reqFormat == mInput->stream->common.get_format(&mInput->stream->common) && |
Dima Zavin | 24fc2fb | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 4766 | reqFormat == AUDIO_FORMAT_PCM_16_BIT && |
Dima Zavin | 31f18889 | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 4767 | ((int)mInput->stream->common.get_sample_rate(&mInput->stream->common) <= (2 * reqSamplingRate)) && |
| 4768 | (popcount(mInput->stream->common.get_channels(&mInput->stream->common)) < 3) && |
Dima Zavin | 24fc2fb | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 4769 | (reqChannelCount < 3)) { |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 4770 | status = NO_ERROR; |
| 4771 | } |
| 4772 | if (status == NO_ERROR) { |
| 4773 | readInputParameters(); |
Eric Laurent | 8fce46a | 2009-08-04 09:45:33 -0700 | [diff] [blame] | 4774 | sendConfigEvent_l(AudioSystem::INPUT_CONFIG_CHANGED); |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 4775 | } |
| 4776 | } |
| 4777 | } |
Eric Laurent | 3fdb126 | 2009-11-07 00:01:32 -0800 | [diff] [blame] | 4778 | |
| 4779 | mNewParameters.removeAt(0); |
| 4780 | |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 4781 | mParamStatus = status; |
| 4782 | mParamCond.signal(); |
Eric Laurent | 5f37be3 | 2011-09-13 11:40:21 -0700 | [diff] [blame] | 4783 | // wait for condition with time out in case the thread calling ThreadBase::setParameters() |
| 4784 | // already timed out waiting for the status and will never signal the condition. |
Glenn Kasten | cbfe2e1 | 2011-12-13 11:04:14 -0800 | [diff] [blame] | 4785 | mWaitWorkCV.waitRelative(mLock, kSetParametersTimeoutNs); |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 4786 | } |
| 4787 | return reconfig; |
| 4788 | } |
| 4789 | |
| 4790 | String8 AudioFlinger::RecordThread::getParameters(const String8& keys) |
| 4791 | { |
Dima Zavin | 24fc2fb | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 4792 | char *s; |
Eric Laurent | 828b977 | 2011-08-07 16:32:26 -0700 | [diff] [blame] | 4793 | String8 out_s8 = String8(); |
| 4794 | |
| 4795 | Mutex::Autolock _l(mLock); |
| 4796 | if (initCheck() != NO_ERROR) { |
| 4797 | return out_s8; |
| 4798 | } |
Dima Zavin | 24fc2fb | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 4799 | |
Dima Zavin | 31f18889 | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 4800 | s = mInput->stream->common.get_parameters(&mInput->stream->common, keys.string()); |
Dima Zavin | 24fc2fb | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 4801 | out_s8 = String8(s); |
| 4802 | free(s); |
| 4803 | return out_s8; |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 4804 | } |
| 4805 | |
Eric Laurent | eb8f850d | 2010-05-14 03:26:45 -0700 | [diff] [blame] | 4806 | void AudioFlinger::RecordThread::audioConfigChanged_l(int event, int param) { |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 4807 | AudioSystem::OutputDescriptor desc; |
| 4808 | void *param2 = 0; |
| 4809 | |
| 4810 | switch (event) { |
| 4811 | case AudioSystem::INPUT_OPENED: |
| 4812 | case AudioSystem::INPUT_CONFIG_CHANGED: |
Jean-Michel Trivi | 5439223 | 2011-05-24 15:53:33 -0700 | [diff] [blame] | 4813 | desc.channels = mChannelMask; |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 4814 | desc.samplingRate = mSampleRate; |
| 4815 | desc.format = mFormat; |
| 4816 | desc.frameCount = mFrameCount; |
| 4817 | desc.latency = 0; |
| 4818 | param2 = &desc; |
| 4819 | break; |
| 4820 | |
| 4821 | case AudioSystem::INPUT_CLOSED: |
| 4822 | default: |
| 4823 | break; |
| 4824 | } |
Eric Laurent | 49f02be | 2009-11-19 09:00:56 -0800 | [diff] [blame] | 4825 | mAudioFlinger->audioConfigChanged_l(event, mId, param2); |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 4826 | } |
| 4827 | |
| 4828 | void AudioFlinger::RecordThread::readInputParameters() |
| 4829 | { |
| 4830 | if (mRsmpInBuffer) delete mRsmpInBuffer; |
| 4831 | if (mRsmpOutBuffer) delete mRsmpOutBuffer; |
| 4832 | if (mResampler) delete mResampler; |
Glenn Kasten | c434c90 | 2011-12-13 11:53:26 -0800 | [diff] [blame] | 4833 | mResampler = NULL; |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 4834 | |
Dima Zavin | 31f18889 | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 4835 | mSampleRate = mInput->stream->common.get_sample_rate(&mInput->stream->common); |
Jean-Michel Trivi | 5439223 | 2011-05-24 15:53:33 -0700 | [diff] [blame] | 4836 | mChannelMask = mInput->stream->common.get_channels(&mInput->stream->common); |
| 4837 | mChannelCount = (uint16_t)popcount(mChannelMask); |
Dima Zavin | 31f18889 | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 4838 | mFormat = mInput->stream->common.get_format(&mInput->stream->common); |
Glenn Kasten | faf354d | 2012-01-11 09:48:27 -0800 | [diff] [blame] | 4839 | mFrameSize = audio_stream_frame_size(&mInput->stream->common); |
Dima Zavin | 31f18889 | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 4840 | mInputBytes = mInput->stream->common.get_buffer_size(&mInput->stream->common); |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 4841 | mFrameCount = mInputBytes / mFrameSize; |
| 4842 | mRsmpInBuffer = new int16_t[mFrameCount * mChannelCount]; |
| 4843 | |
| 4844 | if (mSampleRate != mReqSampleRate && mChannelCount < 3 && mReqChannelCount < 3) |
| 4845 | { |
| 4846 | int channelCount; |
| 4847 | // optmization: if mono to mono, use the resampler in stereo to stereo mode to avoid |
| 4848 | // stereo to mono post process as the resampler always outputs stereo. |
| 4849 | if (mChannelCount == 1 && mReqChannelCount == 2) { |
| 4850 | channelCount = 1; |
| 4851 | } else { |
| 4852 | channelCount = 2; |
| 4853 | } |
| 4854 | mResampler = AudioResampler::create(16, channelCount, mReqSampleRate); |
| 4855 | mResampler->setSampleRate(mSampleRate); |
| 4856 | mResampler->setVolume(AudioMixer::UNITY_GAIN, AudioMixer::UNITY_GAIN); |
| 4857 | mRsmpOutBuffer = new int32_t[mFrameCount * 2]; |
| 4858 | |
| 4859 | // optmization: if mono to mono, alter input frame count as if we were inputing stereo samples |
| 4860 | if (mChannelCount == 1 && mReqChannelCount == 1) { |
| 4861 | mFrameCount >>= 1; |
| 4862 | } |
| 4863 | |
| 4864 | } |
| 4865 | mRsmpInIndex = mFrameCount; |
| 4866 | } |
| 4867 | |
Eric Laurent | 47d0a92 | 2010-02-26 02:47:27 -0800 | [diff] [blame] | 4868 | unsigned int AudioFlinger::RecordThread::getInputFramesLost() |
| 4869 | { |
Eric Laurent | 828b977 | 2011-08-07 16:32:26 -0700 | [diff] [blame] | 4870 | Mutex::Autolock _l(mLock); |
| 4871 | if (initCheck() != NO_ERROR) { |
| 4872 | return 0; |
| 4873 | } |
| 4874 | |
Dima Zavin | 31f18889 | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 4875 | return mInput->stream->get_input_frames_lost(mInput->stream); |
Eric Laurent | 47d0a92 | 2010-02-26 02:47:27 -0800 | [diff] [blame] | 4876 | } |
| 4877 | |
Eric Laurent | 464d5b3 | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 4878 | uint32_t AudioFlinger::RecordThread::hasAudioSession(int sessionId) |
| 4879 | { |
| 4880 | Mutex::Autolock _l(mLock); |
| 4881 | uint32_t result = 0; |
| 4882 | if (getEffectChain_l(sessionId) != 0) { |
| 4883 | result = EFFECT_SESSION; |
| 4884 | } |
| 4885 | |
| 4886 | if (mTrack != NULL && sessionId == mTrack->sessionId()) { |
| 4887 | result |= TRACK_SESSION; |
| 4888 | } |
| 4889 | |
| 4890 | return result; |
| 4891 | } |
| 4892 | |
Eric Laurent | 6639b55 | 2011-08-01 09:52:20 -0700 | [diff] [blame] | 4893 | AudioFlinger::RecordThread::RecordTrack* AudioFlinger::RecordThread::track() |
| 4894 | { |
| 4895 | Mutex::Autolock _l(mLock); |
| 4896 | return mTrack; |
| 4897 | } |
| 4898 | |
Eric Laurent | 828b977 | 2011-08-07 16:32:26 -0700 | [diff] [blame] | 4899 | AudioFlinger::AudioStreamIn* AudioFlinger::RecordThread::getInput() |
| 4900 | { |
| 4901 | Mutex::Autolock _l(mLock); |
| 4902 | return mInput; |
| 4903 | } |
| 4904 | |
| 4905 | AudioFlinger::AudioStreamIn* AudioFlinger::RecordThread::clearInput() |
| 4906 | { |
| 4907 | Mutex::Autolock _l(mLock); |
| 4908 | AudioStreamIn *input = mInput; |
| 4909 | mInput = NULL; |
| 4910 | return input; |
| 4911 | } |
| 4912 | |
| 4913 | // this method must always be called either with ThreadBase mLock held or inside the thread loop |
| 4914 | audio_stream_t* AudioFlinger::RecordThread::stream() |
| 4915 | { |
| 4916 | if (mInput == NULL) { |
| 4917 | return NULL; |
| 4918 | } |
| 4919 | return &mInput->stream->common; |
| 4920 | } |
| 4921 | |
| 4922 | |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 4923 | // ---------------------------------------------------------------------------- |
| 4924 | |
Eric Laurent | ddb78e7 | 2009-07-28 08:44:33 -0700 | [diff] [blame] | 4925 | int AudioFlinger::openOutput(uint32_t *pDevices, |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 4926 | uint32_t *pSamplingRate, |
Glenn Kasten | 0a204ed | 2012-01-12 12:27:51 -0800 | [diff] [blame^] | 4927 | audio_format_t *pFormat, |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 4928 | uint32_t *pChannels, |
| 4929 | uint32_t *pLatencyMs, |
| 4930 | uint32_t flags) |
| 4931 | { |
| 4932 | status_t status; |
| 4933 | PlaybackThread *thread = NULL; |
| 4934 | mHardwareStatus = AUDIO_HW_OUTPUT_OPEN; |
| 4935 | uint32_t samplingRate = pSamplingRate ? *pSamplingRate : 0; |
Glenn Kasten | 0a204ed | 2012-01-12 12:27:51 -0800 | [diff] [blame^] | 4936 | audio_format_t format = pFormat ? *pFormat : AUDIO_FORMAT_DEFAULT; |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 4937 | uint32_t channels = pChannels ? *pChannels : 0; |
| 4938 | uint32_t latency = pLatencyMs ? *pLatencyMs : 0; |
Dima Zavin | 31f18889 | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 4939 | audio_stream_out_t *outStream; |
| 4940 | audio_hw_device_t *outHwDev; |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 4941 | |
Steve Block | 71f2cf1 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 4942 | ALOGV("openOutput(), Device %x, SamplingRate %d, Format %d, Channels %x, flags %x", |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 4943 | pDevices ? *pDevices : 0, |
| 4944 | samplingRate, |
| 4945 | format, |
| 4946 | channels, |
| 4947 | flags); |
| 4948 | |
| 4949 | if (pDevices == NULL || *pDevices == 0) { |
Eric Laurent | ddb78e7 | 2009-07-28 08:44:33 -0700 | [diff] [blame] | 4950 | return 0; |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 4951 | } |
Dima Zavin | 31f18889 | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 4952 | |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 4953 | Mutex::Autolock _l(mLock); |
| 4954 | |
Dima Zavin | 31f18889 | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 4955 | outHwDev = findSuitableHwDev_l(*pDevices); |
| 4956 | if (outHwDev == NULL) |
| 4957 | return 0; |
| 4958 | |
Glenn Kasten | 0a204ed | 2012-01-12 12:27:51 -0800 | [diff] [blame^] | 4959 | status = outHwDev->open_output_stream(outHwDev, *pDevices, &format, |
Dima Zavin | 31f18889 | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 4960 | &channels, &samplingRate, &outStream); |
Steve Block | 71f2cf1 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 4961 | ALOGV("openOutput() openOutputStream returned output %p, SamplingRate %d, Format %d, Channels %x, status %d", |
Dima Zavin | 31f18889 | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 4962 | outStream, |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 4963 | samplingRate, |
| 4964 | format, |
| 4965 | channels, |
| 4966 | status); |
| 4967 | |
| 4968 | mHardwareStatus = AUDIO_HW_IDLE; |
Dima Zavin | 31f18889 | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 4969 | if (outStream != NULL) { |
| 4970 | AudioStreamOut *output = new AudioStreamOut(outHwDev, outStream); |
Eric Laurent | 464d5b3 | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 4971 | int id = nextUniqueId(); |
Dima Zavin | 31f18889 | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 4972 | |
Dima Zavin | 24fc2fb | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 4973 | if ((flags & AUDIO_POLICY_OUTPUT_FLAG_DIRECT) || |
| 4974 | (format != AUDIO_FORMAT_PCM_16_BIT) || |
| 4975 | (channels != AUDIO_CHANNEL_OUT_STEREO)) { |
Eric Laurent | 65b6545 | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 4976 | thread = new DirectOutputThread(this, output, id, *pDevices); |
Steve Block | 71f2cf1 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 4977 | ALOGV("openOutput() created direct output: ID %d thread %p", id, thread); |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 4978 | } else { |
Eric Laurent | 65b6545 | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 4979 | thread = new MixerThread(this, output, id, *pDevices); |
Steve Block | 71f2cf1 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 4980 | ALOGV("openOutput() created mixer output: ID %d thread %p", id, thread); |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 4981 | } |
Eric Laurent | 65b6545 | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 4982 | mPlaybackThreads.add(id, thread); |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 4983 | |
| 4984 | if (pSamplingRate) *pSamplingRate = samplingRate; |
| 4985 | if (pFormat) *pFormat = format; |
| 4986 | if (pChannels) *pChannels = channels; |
| 4987 | if (pLatencyMs) *pLatencyMs = thread->latency(); |
Eric Laurent | 9cc489a | 2009-12-05 05:20:01 -0800 | [diff] [blame] | 4988 | |
Eric Laurent | eb8f850d | 2010-05-14 03:26:45 -0700 | [diff] [blame] | 4989 | // notify client processes of the new output creation |
| 4990 | thread->audioConfigChanged_l(AudioSystem::OUTPUT_OPENED); |
Eric Laurent | 65b6545 | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 4991 | return id; |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 4992 | } |
| 4993 | |
Eric Laurent | 9cc489a | 2009-12-05 05:20:01 -0800 | [diff] [blame] | 4994 | return 0; |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 4995 | } |
| 4996 | |
Eric Laurent | ddb78e7 | 2009-07-28 08:44:33 -0700 | [diff] [blame] | 4997 | int AudioFlinger::openDuplicateOutput(int output1, int output2) |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 4998 | { |
| 4999 | Mutex::Autolock _l(mLock); |
Eric Laurent | ddb78e7 | 2009-07-28 08:44:33 -0700 | [diff] [blame] | 5000 | MixerThread *thread1 = checkMixerThread_l(output1); |
| 5001 | MixerThread *thread2 = checkMixerThread_l(output2); |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 5002 | |
Eric Laurent | ddb78e7 | 2009-07-28 08:44:33 -0700 | [diff] [blame] | 5003 | if (thread1 == NULL || thread2 == NULL) { |
Steve Block | 8564c8d | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 5004 | ALOGW("openDuplicateOutput() wrong output mixer type for output %d or %d", output1, output2); |
Eric Laurent | ddb78e7 | 2009-07-28 08:44:33 -0700 | [diff] [blame] | 5005 | return 0; |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 5006 | } |
| 5007 | |
Eric Laurent | 464d5b3 | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 5008 | int id = nextUniqueId(); |
Eric Laurent | 65b6545 | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 5009 | DuplicatingThread *thread = new DuplicatingThread(this, thread1, id); |
Eric Laurent | ddb78e7 | 2009-07-28 08:44:33 -0700 | [diff] [blame] | 5010 | thread->addOutputTrack(thread2); |
Eric Laurent | 65b6545 | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 5011 | mPlaybackThreads.add(id, thread); |
Eric Laurent | eb8f850d | 2010-05-14 03:26:45 -0700 | [diff] [blame] | 5012 | // notify client processes of the new output creation |
| 5013 | thread->audioConfigChanged_l(AudioSystem::OUTPUT_OPENED); |
Eric Laurent | 65b6545 | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 5014 | return id; |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 5015 | } |
| 5016 | |
Eric Laurent | ddb78e7 | 2009-07-28 08:44:33 -0700 | [diff] [blame] | 5017 | status_t AudioFlinger::closeOutput(int output) |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 5018 | { |
Eric Laurent | 49018a5 | 2009-08-04 08:37:05 -0700 | [diff] [blame] | 5019 | // keep strong reference on the playback thread so that |
| 5020 | // it is not destroyed while exit() is executed |
| 5021 | sp <PlaybackThread> thread; |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 5022 | { |
| 5023 | Mutex::Autolock _l(mLock); |
| 5024 | thread = checkPlaybackThread_l(output); |
| 5025 | if (thread == NULL) { |
| 5026 | return BAD_VALUE; |
| 5027 | } |
| 5028 | |
Steve Block | 71f2cf1 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 5029 | ALOGV("closeOutput() %d", output); |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 5030 | |
Eric Laurent | 464d5b3 | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 5031 | if (thread->type() == ThreadBase::MIXER) { |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 5032 | for (size_t i = 0; i < mPlaybackThreads.size(); i++) { |
Eric Laurent | 464d5b3 | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 5033 | if (mPlaybackThreads.valueAt(i)->type() == ThreadBase::DUPLICATING) { |
Eric Laurent | ddb78e7 | 2009-07-28 08:44:33 -0700 | [diff] [blame] | 5034 | DuplicatingThread *dupThread = (DuplicatingThread *)mPlaybackThreads.valueAt(i).get(); |
Eric Laurent | 49018a5 | 2009-08-04 08:37:05 -0700 | [diff] [blame] | 5035 | dupThread->removeOutputTrack((MixerThread *)thread.get()); |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 5036 | } |
| 5037 | } |
| 5038 | } |
Eric Laurent | 296a0ec | 2009-09-15 07:10:12 -0700 | [diff] [blame] | 5039 | void *param2 = 0; |
Eric Laurent | 49f02be | 2009-11-19 09:00:56 -0800 | [diff] [blame] | 5040 | audioConfigChanged_l(AudioSystem::OUTPUT_CLOSED, output, param2); |
Eric Laurent | ddb78e7 | 2009-07-28 08:44:33 -0700 | [diff] [blame] | 5041 | mPlaybackThreads.removeItem(output); |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 5042 | } |
| 5043 | thread->exit(); |
| 5044 | |
Eric Laurent | 464d5b3 | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 5045 | if (thread->type() != ThreadBase::DUPLICATING) { |
Eric Laurent | 828b977 | 2011-08-07 16:32:26 -0700 | [diff] [blame] | 5046 | AudioStreamOut *out = thread->clearOutput(); |
| 5047 | // from now on thread->mOutput is NULL |
Dima Zavin | 31f18889 | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 5048 | out->hwDev->close_output_stream(out->hwDev, out->stream); |
| 5049 | delete out; |
Eric Laurent | 49018a5 | 2009-08-04 08:37:05 -0700 | [diff] [blame] | 5050 | } |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 5051 | return NO_ERROR; |
| 5052 | } |
| 5053 | |
Eric Laurent | ddb78e7 | 2009-07-28 08:44:33 -0700 | [diff] [blame] | 5054 | status_t AudioFlinger::suspendOutput(int output) |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 5055 | { |
| 5056 | Mutex::Autolock _l(mLock); |
| 5057 | PlaybackThread *thread = checkPlaybackThread_l(output); |
| 5058 | |
| 5059 | if (thread == NULL) { |
| 5060 | return BAD_VALUE; |
| 5061 | } |
| 5062 | |
Steve Block | 71f2cf1 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 5063 | ALOGV("suspendOutput() %d", output); |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 5064 | thread->suspend(); |
| 5065 | |
| 5066 | return NO_ERROR; |
| 5067 | } |
| 5068 | |
Eric Laurent | ddb78e7 | 2009-07-28 08:44:33 -0700 | [diff] [blame] | 5069 | status_t AudioFlinger::restoreOutput(int output) |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 5070 | { |
| 5071 | Mutex::Autolock _l(mLock); |
| 5072 | PlaybackThread *thread = checkPlaybackThread_l(output); |
| 5073 | |
| 5074 | if (thread == NULL) { |
| 5075 | return BAD_VALUE; |
| 5076 | } |
| 5077 | |
Steve Block | 71f2cf1 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 5078 | ALOGV("restoreOutput() %d", output); |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 5079 | |
| 5080 | thread->restore(); |
| 5081 | |
| 5082 | return NO_ERROR; |
| 5083 | } |
| 5084 | |
Eric Laurent | ddb78e7 | 2009-07-28 08:44:33 -0700 | [diff] [blame] | 5085 | int AudioFlinger::openInput(uint32_t *pDevices, |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 5086 | uint32_t *pSamplingRate, |
Glenn Kasten | 0a204ed | 2012-01-12 12:27:51 -0800 | [diff] [blame^] | 5087 | audio_format_t *pFormat, |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 5088 | uint32_t *pChannels, |
| 5089 | uint32_t acoustics) |
| 5090 | { |
| 5091 | status_t status; |
| 5092 | RecordThread *thread = NULL; |
| 5093 | uint32_t samplingRate = pSamplingRate ? *pSamplingRate : 0; |
Glenn Kasten | 0a204ed | 2012-01-12 12:27:51 -0800 | [diff] [blame^] | 5094 | audio_format_t format = pFormat ? *pFormat : AUDIO_FORMAT_DEFAULT; |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 5095 | uint32_t channels = pChannels ? *pChannels : 0; |
| 5096 | uint32_t reqSamplingRate = samplingRate; |
Glenn Kasten | 0a204ed | 2012-01-12 12:27:51 -0800 | [diff] [blame^] | 5097 | audio_format_t reqFormat = format; |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 5098 | uint32_t reqChannels = channels; |
Dima Zavin | 31f18889 | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 5099 | audio_stream_in_t *inStream; |
| 5100 | audio_hw_device_t *inHwDev; |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 5101 | |
| 5102 | if (pDevices == NULL || *pDevices == 0) { |
Eric Laurent | ddb78e7 | 2009-07-28 08:44:33 -0700 | [diff] [blame] | 5103 | return 0; |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 5104 | } |
Dima Zavin | 31f18889 | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 5105 | |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 5106 | Mutex::Autolock _l(mLock); |
| 5107 | |
Dima Zavin | 31f18889 | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 5108 | inHwDev = findSuitableHwDev_l(*pDevices); |
| 5109 | if (inHwDev == NULL) |
| 5110 | return 0; |
| 5111 | |
Glenn Kasten | 0a204ed | 2012-01-12 12:27:51 -0800 | [diff] [blame^] | 5112 | status = inHwDev->open_input_stream(inHwDev, *pDevices, &format, |
Dima Zavin | 31f18889 | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 5113 | &channels, &samplingRate, |
Dima Zavin | 24fc2fb | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 5114 | (audio_in_acoustics_t)acoustics, |
Dima Zavin | 31f18889 | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 5115 | &inStream); |
Steve Block | 71f2cf1 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 5116 | ALOGV("openInput() openInputStream returned input %p, SamplingRate %d, Format %d, Channels %x, acoustics %x, status %d", |
Dima Zavin | 31f18889 | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 5117 | inStream, |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 5118 | samplingRate, |
| 5119 | format, |
| 5120 | channels, |
| 5121 | acoustics, |
| 5122 | status); |
| 5123 | |
| 5124 | // If the input could not be opened with the requested parameters and we can handle the conversion internally, |
| 5125 | // try to open again with the proposed parameters. The AudioFlinger can resample the input and do mono to stereo |
| 5126 | // or stereo to mono conversions on 16 bit PCM inputs. |
Dima Zavin | 31f18889 | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 5127 | if (inStream == NULL && status == BAD_VALUE && |
Dima Zavin | 24fc2fb | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 5128 | reqFormat == format && format == AUDIO_FORMAT_PCM_16_BIT && |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 5129 | (samplingRate <= 2 * reqSamplingRate) && |
Dima Zavin | 24fc2fb | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 5130 | (popcount(channels) < 3) && (popcount(reqChannels) < 3)) { |
Steve Block | 71f2cf1 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 5131 | ALOGV("openInput() reopening with proposed sampling rate and channels"); |
Glenn Kasten | 0a204ed | 2012-01-12 12:27:51 -0800 | [diff] [blame^] | 5132 | status = inHwDev->open_input_stream(inHwDev, *pDevices, &format, |
Dima Zavin | 31f18889 | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 5133 | &channels, &samplingRate, |
Dima Zavin | 24fc2fb | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 5134 | (audio_in_acoustics_t)acoustics, |
Dima Zavin | 31f18889 | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 5135 | &inStream); |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 5136 | } |
| 5137 | |
Dima Zavin | 31f18889 | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 5138 | if (inStream != NULL) { |
| 5139 | AudioStreamIn *input = new AudioStreamIn(inHwDev, inStream); |
| 5140 | |
Eric Laurent | 464d5b3 | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 5141 | int id = nextUniqueId(); |
| 5142 | // Start record thread |
| 5143 | // RecorThread require both input and output device indication to forward to audio |
| 5144 | // pre processing modules |
| 5145 | uint32_t device = (*pDevices) | primaryOutputDevice_l(); |
| 5146 | thread = new RecordThread(this, |
| 5147 | input, |
| 5148 | reqSamplingRate, |
| 5149 | reqChannels, |
| 5150 | id, |
| 5151 | device); |
Eric Laurent | 65b6545 | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 5152 | mRecordThreads.add(id, thread); |
Steve Block | 71f2cf1 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 5153 | ALOGV("openInput() created record thread: ID %d thread %p", id, thread); |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 5154 | if (pSamplingRate) *pSamplingRate = reqSamplingRate; |
| 5155 | if (pFormat) *pFormat = format; |
| 5156 | if (pChannels) *pChannels = reqChannels; |
| 5157 | |
Dima Zavin | 31f18889 | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 5158 | input->stream->common.standby(&input->stream->common); |
Eric Laurent | 9cc489a | 2009-12-05 05:20:01 -0800 | [diff] [blame] | 5159 | |
Eric Laurent | eb8f850d | 2010-05-14 03:26:45 -0700 | [diff] [blame] | 5160 | // notify client processes of the new input creation |
| 5161 | thread->audioConfigChanged_l(AudioSystem::INPUT_OPENED); |
Eric Laurent | 65b6545 | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 5162 | return id; |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 5163 | } |
| 5164 | |
Eric Laurent | 9cc489a | 2009-12-05 05:20:01 -0800 | [diff] [blame] | 5165 | return 0; |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 5166 | } |
| 5167 | |
Eric Laurent | ddb78e7 | 2009-07-28 08:44:33 -0700 | [diff] [blame] | 5168 | status_t AudioFlinger::closeInput(int input) |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 5169 | { |
Eric Laurent | 49018a5 | 2009-08-04 08:37:05 -0700 | [diff] [blame] | 5170 | // keep strong reference on the record thread so that |
| 5171 | // it is not destroyed while exit() is executed |
| 5172 | sp <RecordThread> thread; |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 5173 | { |
| 5174 | Mutex::Autolock _l(mLock); |
| 5175 | thread = checkRecordThread_l(input); |
| 5176 | if (thread == NULL) { |
| 5177 | return BAD_VALUE; |
| 5178 | } |
| 5179 | |
Steve Block | 71f2cf1 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 5180 | ALOGV("closeInput() %d", input); |
Eric Laurent | 296a0ec | 2009-09-15 07:10:12 -0700 | [diff] [blame] | 5181 | void *param2 = 0; |
Eric Laurent | 49f02be | 2009-11-19 09:00:56 -0800 | [diff] [blame] | 5182 | audioConfigChanged_l(AudioSystem::INPUT_CLOSED, input, param2); |
Eric Laurent | ddb78e7 | 2009-07-28 08:44:33 -0700 | [diff] [blame] | 5183 | mRecordThreads.removeItem(input); |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 5184 | } |
| 5185 | thread->exit(); |
| 5186 | |
Eric Laurent | 828b977 | 2011-08-07 16:32:26 -0700 | [diff] [blame] | 5187 | AudioStreamIn *in = thread->clearInput(); |
| 5188 | // from now on thread->mInput is NULL |
Dima Zavin | 31f18889 | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 5189 | in->hwDev->close_input_stream(in->hwDev, in->stream); |
| 5190 | delete in; |
Eric Laurent | 49018a5 | 2009-08-04 08:37:05 -0700 | [diff] [blame] | 5191 | |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 5192 | return NO_ERROR; |
| 5193 | } |
| 5194 | |
Glenn Kasten | bc1d77b | 2012-01-12 16:38:12 -0800 | [diff] [blame] | 5195 | status_t AudioFlinger::setStreamOutput(audio_stream_type_t stream, int output) |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 5196 | { |
| 5197 | Mutex::Autolock _l(mLock); |
| 5198 | MixerThread *dstThread = checkMixerThread_l(output); |
| 5199 | if (dstThread == NULL) { |
Steve Block | 8564c8d | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 5200 | ALOGW("setStreamOutput() bad output id %d", output); |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 5201 | return BAD_VALUE; |
| 5202 | } |
| 5203 | |
Steve Block | 71f2cf1 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 5204 | ALOGV("setStreamOutput() stream %d to output %d", stream, output); |
Eric Laurent | eb8f850d | 2010-05-14 03:26:45 -0700 | [diff] [blame] | 5205 | audioConfigChanged_l(AudioSystem::STREAM_CONFIG_CHANGED, output, &stream); |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 5206 | |
Eric Laurent | 05ce094 | 2011-08-30 10:18:54 -0700 | [diff] [blame] | 5207 | dstThread->setStreamValid(stream, true); |
| 5208 | |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 5209 | for (size_t i = 0; i < mPlaybackThreads.size(); i++) { |
Eric Laurent | ddb78e7 | 2009-07-28 08:44:33 -0700 | [diff] [blame] | 5210 | PlaybackThread *thread = mPlaybackThreads.valueAt(i).get(); |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 5211 | if (thread != dstThread && |
Eric Laurent | 464d5b3 | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 5212 | thread->type() != ThreadBase::DIRECT) { |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 5213 | MixerThread *srcThread = (MixerThread *)thread; |
Eric Laurent | 05ce094 | 2011-08-30 10:18:54 -0700 | [diff] [blame] | 5214 | srcThread->setStreamValid(stream, false); |
Eric Laurent | eb8f850d | 2010-05-14 03:26:45 -0700 | [diff] [blame] | 5215 | srcThread->invalidateTracks(stream); |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 5216 | } |
Eric Laurent | 8ed6ed0 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 5217 | } |
Eric Laurent | d069f32 | 2009-09-01 05:56:26 -0700 | [diff] [blame] | 5218 | |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 5219 | return NO_ERROR; |
| 5220 | } |
| 5221 | |
Eric Laurent | 65b6545 | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 5222 | |
| 5223 | int AudioFlinger::newAudioSessionId() |
| 5224 | { |
Eric Laurent | 464d5b3 | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 5225 | return nextUniqueId(); |
Eric Laurent | 65b6545 | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 5226 | } |
| 5227 | |
Marco Nelissen | c74b93f | 2011-08-02 13:33:41 -0700 | [diff] [blame] | 5228 | void AudioFlinger::acquireAudioSessionId(int audioSession) |
| 5229 | { |
| 5230 | Mutex::Autolock _l(mLock); |
| 5231 | int caller = IPCThreadState::self()->getCallingPid(); |
Steve Block | 71f2cf1 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 5232 | ALOGV("acquiring %d from %d", audioSession, caller); |
Marco Nelissen | c74b93f | 2011-08-02 13:33:41 -0700 | [diff] [blame] | 5233 | int num = mAudioSessionRefs.size(); |
| 5234 | for (int i = 0; i< num; i++) { |
| 5235 | AudioSessionRef *ref = mAudioSessionRefs.editItemAt(i); |
| 5236 | if (ref->sessionid == audioSession && ref->pid == caller) { |
| 5237 | ref->cnt++; |
Steve Block | 71f2cf1 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 5238 | ALOGV(" incremented refcount to %d", ref->cnt); |
Marco Nelissen | c74b93f | 2011-08-02 13:33:41 -0700 | [diff] [blame] | 5239 | return; |
| 5240 | } |
| 5241 | } |
| 5242 | AudioSessionRef *ref = new AudioSessionRef(); |
| 5243 | ref->sessionid = audioSession; |
| 5244 | ref->pid = caller; |
| 5245 | ref->cnt = 1; |
| 5246 | mAudioSessionRefs.push(ref); |
Steve Block | 71f2cf1 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 5247 | ALOGV(" added new entry for %d", ref->sessionid); |
Marco Nelissen | c74b93f | 2011-08-02 13:33:41 -0700 | [diff] [blame] | 5248 | } |
| 5249 | |
| 5250 | void AudioFlinger::releaseAudioSessionId(int audioSession) |
| 5251 | { |
| 5252 | Mutex::Autolock _l(mLock); |
| 5253 | int caller = IPCThreadState::self()->getCallingPid(); |
Steve Block | 71f2cf1 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 5254 | ALOGV("releasing %d from %d", audioSession, caller); |
Marco Nelissen | c74b93f | 2011-08-02 13:33:41 -0700 | [diff] [blame] | 5255 | int num = mAudioSessionRefs.size(); |
| 5256 | for (int i = 0; i< num; i++) { |
| 5257 | AudioSessionRef *ref = mAudioSessionRefs.itemAt(i); |
| 5258 | if (ref->sessionid == audioSession && ref->pid == caller) { |
| 5259 | ref->cnt--; |
Steve Block | 71f2cf1 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 5260 | ALOGV(" decremented refcount to %d", ref->cnt); |
Marco Nelissen | c74b93f | 2011-08-02 13:33:41 -0700 | [diff] [blame] | 5261 | if (ref->cnt == 0) { |
| 5262 | mAudioSessionRefs.removeAt(i); |
| 5263 | delete ref; |
| 5264 | purgeStaleEffects_l(); |
| 5265 | } |
| 5266 | return; |
| 5267 | } |
| 5268 | } |
Steve Block | 8564c8d | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 5269 | ALOGW("session id %d not found for pid %d", audioSession, caller); |
Marco Nelissen | c74b93f | 2011-08-02 13:33:41 -0700 | [diff] [blame] | 5270 | } |
| 5271 | |
| 5272 | void AudioFlinger::purgeStaleEffects_l() { |
| 5273 | |
Steve Block | 71f2cf1 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 5274 | ALOGV("purging stale effects"); |
Marco Nelissen | c74b93f | 2011-08-02 13:33:41 -0700 | [diff] [blame] | 5275 | |
| 5276 | Vector< sp<EffectChain> > chains; |
| 5277 | |
| 5278 | for (size_t i = 0; i < mPlaybackThreads.size(); i++) { |
| 5279 | sp<PlaybackThread> t = mPlaybackThreads.valueAt(i); |
| 5280 | for (size_t j = 0; j < t->mEffectChains.size(); j++) { |
| 5281 | sp<EffectChain> ec = t->mEffectChains[j]; |
Marco Nelissen | 2255a1e | 2011-08-12 14:14:39 -0700 | [diff] [blame] | 5282 | if (ec->sessionId() > AUDIO_SESSION_OUTPUT_MIX) { |
| 5283 | chains.push(ec); |
| 5284 | } |
Marco Nelissen | c74b93f | 2011-08-02 13:33:41 -0700 | [diff] [blame] | 5285 | } |
| 5286 | } |
| 5287 | for (size_t i = 0; i < mRecordThreads.size(); i++) { |
| 5288 | sp<RecordThread> t = mRecordThreads.valueAt(i); |
| 5289 | for (size_t j = 0; j < t->mEffectChains.size(); j++) { |
| 5290 | sp<EffectChain> ec = t->mEffectChains[j]; |
| 5291 | chains.push(ec); |
| 5292 | } |
| 5293 | } |
| 5294 | |
| 5295 | for (size_t i = 0; i < chains.size(); i++) { |
| 5296 | sp<EffectChain> ec = chains[i]; |
| 5297 | int sessionid = ec->sessionId(); |
| 5298 | sp<ThreadBase> t = ec->mThread.promote(); |
| 5299 | if (t == 0) { |
| 5300 | continue; |
| 5301 | } |
| 5302 | size_t numsessionrefs = mAudioSessionRefs.size(); |
| 5303 | bool found = false; |
| 5304 | for (size_t k = 0; k < numsessionrefs; k++) { |
| 5305 | AudioSessionRef *ref = mAudioSessionRefs.itemAt(k); |
| 5306 | if (ref->sessionid == sessionid) { |
Steve Block | 71f2cf1 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 5307 | ALOGV(" session %d still exists for %d with %d refs", |
Marco Nelissen | c74b93f | 2011-08-02 13:33:41 -0700 | [diff] [blame] | 5308 | sessionid, ref->pid, ref->cnt); |
| 5309 | found = true; |
| 5310 | break; |
| 5311 | } |
| 5312 | } |
| 5313 | if (!found) { |
| 5314 | // remove all effects from the chain |
| 5315 | while (ec->mEffects.size()) { |
| 5316 | sp<EffectModule> effect = ec->mEffects[0]; |
| 5317 | effect->unPin(); |
| 5318 | Mutex::Autolock _l (t->mLock); |
| 5319 | t->removeEffect_l(effect); |
| 5320 | for (size_t j = 0; j < effect->mHandles.size(); j++) { |
| 5321 | sp<EffectHandle> handle = effect->mHandles[j].promote(); |
| 5322 | if (handle != 0) { |
| 5323 | handle->mEffect.clear(); |
Eric Laurent | 7fa1cee | 2011-10-19 11:44:54 -0700 | [diff] [blame] | 5324 | if (handle->mHasControl && handle->mEnabled) { |
| 5325 | t->checkSuspendOnEffectEnabled_l(effect, false, effect->sessionId()); |
| 5326 | } |
Marco Nelissen | c74b93f | 2011-08-02 13:33:41 -0700 | [diff] [blame] | 5327 | } |
| 5328 | } |
| 5329 | AudioSystem::unregisterEffect(effect->id()); |
| 5330 | } |
| 5331 | } |
| 5332 | } |
| 5333 | return; |
| 5334 | } |
| 5335 | |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 5336 | // checkPlaybackThread_l() must be called with AudioFlinger::mLock held |
Eric Laurent | ddb78e7 | 2009-07-28 08:44:33 -0700 | [diff] [blame] | 5337 | AudioFlinger::PlaybackThread *AudioFlinger::checkPlaybackThread_l(int output) const |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 5338 | { |
| 5339 | PlaybackThread *thread = NULL; |
Eric Laurent | ddb78e7 | 2009-07-28 08:44:33 -0700 | [diff] [blame] | 5340 | if (mPlaybackThreads.indexOfKey(output) >= 0) { |
| 5341 | thread = (PlaybackThread *)mPlaybackThreads.valueFor(output).get(); |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 5342 | } |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 5343 | return thread; |
| 5344 | } |
| 5345 | |
| 5346 | // checkMixerThread_l() must be called with AudioFlinger::mLock held |
Eric Laurent | ddb78e7 | 2009-07-28 08:44:33 -0700 | [diff] [blame] | 5347 | AudioFlinger::MixerThread *AudioFlinger::checkMixerThread_l(int output) const |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 5348 | { |
| 5349 | PlaybackThread *thread = checkPlaybackThread_l(output); |
| 5350 | if (thread != NULL) { |
Eric Laurent | 464d5b3 | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 5351 | if (thread->type() == ThreadBase::DIRECT) { |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 5352 | thread = NULL; |
| 5353 | } |
| 5354 | } |
| 5355 | return (MixerThread *)thread; |
| 5356 | } |
| 5357 | |
| 5358 | // checkRecordThread_l() must be called with AudioFlinger::mLock held |
Eric Laurent | ddb78e7 | 2009-07-28 08:44:33 -0700 | [diff] [blame] | 5359 | AudioFlinger::RecordThread *AudioFlinger::checkRecordThread_l(int input) const |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 5360 | { |
| 5361 | RecordThread *thread = NULL; |
Eric Laurent | ddb78e7 | 2009-07-28 08:44:33 -0700 | [diff] [blame] | 5362 | if (mRecordThreads.indexOfKey(input) >= 0) { |
| 5363 | thread = (RecordThread *)mRecordThreads.valueFor(input).get(); |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 5364 | } |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 5365 | return thread; |
| 5366 | } |
| 5367 | |
Eric Laurent | 464d5b3 | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 5368 | uint32_t AudioFlinger::nextUniqueId() |
Eric Laurent | 65b6545 | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 5369 | { |
Eric Laurent | 464d5b3 | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 5370 | return android_atomic_inc(&mNextUniqueId); |
Eric Laurent | 65b6545 | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 5371 | } |
| 5372 | |
Eric Laurent | 464d5b3 | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 5373 | AudioFlinger::PlaybackThread *AudioFlinger::primaryPlaybackThread_l() |
| 5374 | { |
| 5375 | for (size_t i = 0; i < mPlaybackThreads.size(); i++) { |
| 5376 | PlaybackThread *thread = mPlaybackThreads.valueAt(i).get(); |
Eric Laurent | 828b977 | 2011-08-07 16:32:26 -0700 | [diff] [blame] | 5377 | AudioStreamOut *output = thread->getOutput(); |
| 5378 | if (output != NULL && output->hwDev == mPrimaryHardwareDev) { |
Eric Laurent | 464d5b3 | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 5379 | return thread; |
| 5380 | } |
| 5381 | } |
| 5382 | return NULL; |
| 5383 | } |
| 5384 | |
| 5385 | uint32_t AudioFlinger::primaryOutputDevice_l() |
| 5386 | { |
| 5387 | PlaybackThread *thread = primaryPlaybackThread_l(); |
| 5388 | |
| 5389 | if (thread == NULL) { |
| 5390 | return 0; |
| 5391 | } |
| 5392 | |
| 5393 | return thread->device(); |
| 5394 | } |
| 5395 | |
| 5396 | |
Eric Laurent | 65b6545 | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 5397 | // ---------------------------------------------------------------------------- |
| 5398 | // Effect management |
| 5399 | // ---------------------------------------------------------------------------- |
| 5400 | |
| 5401 | |
Eric Laurent | 65b6545 | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 5402 | status_t AudioFlinger::queryNumberEffects(uint32_t *numEffects) |
| 5403 | { |
| 5404 | Mutex::Autolock _l(mLock); |
| 5405 | return EffectQueryNumberEffects(numEffects); |
| 5406 | } |
| 5407 | |
Eric Laurent | 53334cd | 2010-06-23 17:38:20 -0700 | [diff] [blame] | 5408 | status_t AudioFlinger::queryEffect(uint32_t index, effect_descriptor_t *descriptor) |
Eric Laurent | 65b6545 | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 5409 | { |
| 5410 | Mutex::Autolock _l(mLock); |
Eric Laurent | 53334cd | 2010-06-23 17:38:20 -0700 | [diff] [blame] | 5411 | return EffectQueryEffect(index, descriptor); |
Eric Laurent | 65b6545 | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 5412 | } |
| 5413 | |
| 5414 | status_t AudioFlinger::getEffectDescriptor(effect_uuid_t *pUuid, effect_descriptor_t *descriptor) |
| 5415 | { |
| 5416 | Mutex::Autolock _l(mLock); |
| 5417 | return EffectGetDescriptor(pUuid, descriptor); |
| 5418 | } |
| 5419 | |
Eric Laurent | df9b81c | 2010-07-02 08:12:41 -0700 | [diff] [blame] | 5420 | |
Eric Laurent | 65b6545 | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 5421 | sp<IEffect> AudioFlinger::createEffect(pid_t pid, |
| 5422 | effect_descriptor_t *pDesc, |
| 5423 | const sp<IEffectClient>& effectClient, |
| 5424 | int32_t priority, |
Eric Laurent | 464d5b3 | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 5425 | int io, |
Eric Laurent | 65b6545 | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 5426 | int sessionId, |
| 5427 | status_t *status, |
| 5428 | int *id, |
| 5429 | int *enabled) |
| 5430 | { |
| 5431 | status_t lStatus = NO_ERROR; |
| 5432 | sp<EffectHandle> handle; |
Eric Laurent | 65b6545 | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 5433 | effect_descriptor_t desc; |
| 5434 | sp<Client> client; |
| 5435 | wp<Client> wclient; |
| 5436 | |
Steve Block | 71f2cf1 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 5437 | ALOGV("createEffect pid %d, client %p, priority %d, sessionId %d, io %d", |
Eric Laurent | 464d5b3 | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 5438 | pid, effectClient.get(), priority, sessionId, io); |
Eric Laurent | 65b6545 | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 5439 | |
| 5440 | if (pDesc == NULL) { |
| 5441 | lStatus = BAD_VALUE; |
| 5442 | goto Exit; |
| 5443 | } |
| 5444 | |
Eric Laurent | 98c9259 | 2010-09-23 16:10:16 -0700 | [diff] [blame] | 5445 | // check audio settings permission for global effects |
Dima Zavin | 24fc2fb | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 5446 | if (sessionId == AUDIO_SESSION_OUTPUT_MIX && !settingsAllowed()) { |
Eric Laurent | 98c9259 | 2010-09-23 16:10:16 -0700 | [diff] [blame] | 5447 | lStatus = PERMISSION_DENIED; |
| 5448 | goto Exit; |
| 5449 | } |
| 5450 | |
Dima Zavin | 24fc2fb | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 5451 | // Session AUDIO_SESSION_OUTPUT_STAGE is reserved for output stage effects |
Eric Laurent | 98c9259 | 2010-09-23 16:10:16 -0700 | [diff] [blame] | 5452 | // that can only be created by audio policy manager (running in same process) |
Dima Zavin | 24fc2fb | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 5453 | if (sessionId == AUDIO_SESSION_OUTPUT_STAGE && getpid() != pid) { |
Eric Laurent | 98c9259 | 2010-09-23 16:10:16 -0700 | [diff] [blame] | 5454 | lStatus = PERMISSION_DENIED; |
| 5455 | goto Exit; |
| 5456 | } |
| 5457 | |
Eric Laurent | 464d5b3 | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 5458 | if (io == 0) { |
Dima Zavin | 24fc2fb | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 5459 | if (sessionId == AUDIO_SESSION_OUTPUT_STAGE) { |
Eric Laurent | 98c9259 | 2010-09-23 16:10:16 -0700 | [diff] [blame] | 5460 | // output must be specified by AudioPolicyManager when using session |
Dima Zavin | 24fc2fb | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 5461 | // AUDIO_SESSION_OUTPUT_STAGE |
Eric Laurent | 98c9259 | 2010-09-23 16:10:16 -0700 | [diff] [blame] | 5462 | lStatus = BAD_VALUE; |
| 5463 | goto Exit; |
Dima Zavin | 24fc2fb | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 5464 | } else if (sessionId == AUDIO_SESSION_OUTPUT_MIX) { |
Eric Laurent | 98c9259 | 2010-09-23 16:10:16 -0700 | [diff] [blame] | 5465 | // if the output returned by getOutputForEffect() is removed before we lock the |
Eric Laurent | 464d5b3 | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 5466 | // mutex below, the call to checkPlaybackThread_l(io) below will detect it |
Eric Laurent | 98c9259 | 2010-09-23 16:10:16 -0700 | [diff] [blame] | 5467 | // and we will exit safely |
Eric Laurent | 464d5b3 | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 5468 | io = AudioSystem::getOutputForEffect(&desc); |
Eric Laurent | 98c9259 | 2010-09-23 16:10:16 -0700 | [diff] [blame] | 5469 | } |
| 5470 | } |
| 5471 | |
Eric Laurent | 65b6545 | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 5472 | { |
| 5473 | Mutex::Autolock _l(mLock); |
| 5474 | |
Eric Laurent | df9b81c | 2010-07-02 08:12:41 -0700 | [diff] [blame] | 5475 | |
Eric Laurent | 65b6545 | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 5476 | if (!EffectIsNullUuid(&pDesc->uuid)) { |
| 5477 | // if uuid is specified, request effect descriptor |
| 5478 | lStatus = EffectGetDescriptor(&pDesc->uuid, &desc); |
| 5479 | if (lStatus < 0) { |
Steve Block | 8564c8d | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 5480 | ALOGW("createEffect() error %d from EffectGetDescriptor", lStatus); |
Eric Laurent | 65b6545 | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 5481 | goto Exit; |
| 5482 | } |
| 5483 | } else { |
| 5484 | // if uuid is not specified, look for an available implementation |
| 5485 | // of the required type in effect factory |
| 5486 | if (EffectIsNullUuid(&pDesc->type)) { |
Steve Block | 8564c8d | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 5487 | ALOGW("createEffect() no effect type"); |
Eric Laurent | 65b6545 | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 5488 | lStatus = BAD_VALUE; |
| 5489 | goto Exit; |
| 5490 | } |
| 5491 | uint32_t numEffects = 0; |
| 5492 | effect_descriptor_t d; |
Marco Nelissen | c74b93f | 2011-08-02 13:33:41 -0700 | [diff] [blame] | 5493 | d.flags = 0; // prevent compiler warning |
Eric Laurent | 65b6545 | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 5494 | bool found = false; |
| 5495 | |
| 5496 | lStatus = EffectQueryNumberEffects(&numEffects); |
| 5497 | if (lStatus < 0) { |
Steve Block | 8564c8d | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 5498 | ALOGW("createEffect() error %d from EffectQueryNumberEffects", lStatus); |
Eric Laurent | 65b6545 | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 5499 | goto Exit; |
| 5500 | } |
Eric Laurent | 53334cd | 2010-06-23 17:38:20 -0700 | [diff] [blame] | 5501 | for (uint32_t i = 0; i < numEffects; i++) { |
| 5502 | lStatus = EffectQueryEffect(i, &desc); |
Eric Laurent | 65b6545 | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 5503 | if (lStatus < 0) { |
Steve Block | 8564c8d | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 5504 | ALOGW("createEffect() error %d from EffectQueryEffect", lStatus); |
Eric Laurent | 65b6545 | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 5505 | continue; |
| 5506 | } |
| 5507 | if (memcmp(&desc.type, &pDesc->type, sizeof(effect_uuid_t)) == 0) { |
| 5508 | // If matching type found save effect descriptor. If the session is |
| 5509 | // 0 and the effect is not auxiliary, continue enumeration in case |
| 5510 | // an auxiliary version of this effect type is available |
| 5511 | found = true; |
| 5512 | memcpy(&d, &desc, sizeof(effect_descriptor_t)); |
Dima Zavin | 24fc2fb | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 5513 | if (sessionId != AUDIO_SESSION_OUTPUT_MIX || |
Eric Laurent | 65b6545 | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 5514 | (desc.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) { |
| 5515 | break; |
| 5516 | } |
| 5517 | } |
| 5518 | } |
| 5519 | if (!found) { |
| 5520 | lStatus = BAD_VALUE; |
Steve Block | 8564c8d | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 5521 | ALOGW("createEffect() effect not found"); |
Eric Laurent | 65b6545 | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 5522 | goto Exit; |
| 5523 | } |
| 5524 | // For same effect type, chose auxiliary version over insert version if |
| 5525 | // connect to output mix (Compliance to OpenSL ES) |
Dima Zavin | 24fc2fb | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 5526 | if (sessionId == AUDIO_SESSION_OUTPUT_MIX && |
Eric Laurent | 65b6545 | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 5527 | (d.flags & EFFECT_FLAG_TYPE_MASK) != EFFECT_FLAG_TYPE_AUXILIARY) { |
| 5528 | memcpy(&desc, &d, sizeof(effect_descriptor_t)); |
| 5529 | } |
| 5530 | } |
| 5531 | |
| 5532 | // Do not allow auxiliary effects on a session different from 0 (output mix) |
Dima Zavin | 24fc2fb | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 5533 | if (sessionId != AUDIO_SESSION_OUTPUT_MIX && |
Eric Laurent | 65b6545 | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 5534 | (desc.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) { |
| 5535 | lStatus = INVALID_OPERATION; |
| 5536 | goto Exit; |
| 5537 | } |
| 5538 | |
Eric Laurent | f82fccd | 2011-07-27 19:49:51 -0700 | [diff] [blame] | 5539 | // check recording permission for visualizer |
| 5540 | if ((memcmp(&desc.type, SL_IID_VISUALIZATION, sizeof(effect_uuid_t)) == 0) && |
| 5541 | !recordingAllowed()) { |
| 5542 | lStatus = PERMISSION_DENIED; |
| 5543 | goto Exit; |
| 5544 | } |
| 5545 | |
Eric Laurent | 65b6545 | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 5546 | // return effect descriptor |
| 5547 | memcpy(pDesc, &desc, sizeof(effect_descriptor_t)); |
| 5548 | |
| 5549 | // If output is not specified try to find a matching audio session ID in one of the |
| 5550 | // output threads. |
Eric Laurent | 98c9259 | 2010-09-23 16:10:16 -0700 | [diff] [blame] | 5551 | // If output is 0 here, sessionId is neither SESSION_OUTPUT_STAGE nor SESSION_OUTPUT_MIX |
| 5552 | // because of code checking output when entering the function. |
Eric Laurent | 464d5b3 | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 5553 | // Note: io is never 0 when creating an effect on an input |
| 5554 | if (io == 0) { |
Eric Laurent | 98c9259 | 2010-09-23 16:10:16 -0700 | [diff] [blame] | 5555 | // look for the thread where the specified audio session is present |
| 5556 | for (size_t i = 0; i < mPlaybackThreads.size(); i++) { |
| 5557 | if (mPlaybackThreads.valueAt(i)->hasAudioSession(sessionId) != 0) { |
Eric Laurent | 464d5b3 | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 5558 | io = mPlaybackThreads.keyAt(i); |
Eric Laurent | 98c9259 | 2010-09-23 16:10:16 -0700 | [diff] [blame] | 5559 | break; |
Eric Laurent | 493941b | 2010-07-28 01:32:47 -0700 | [diff] [blame] | 5560 | } |
Eric Laurent | 65b6545 | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 5561 | } |
Eric Laurent | 464d5b3 | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 5562 | if (io == 0) { |
| 5563 | for (size_t i = 0; i < mRecordThreads.size(); i++) { |
| 5564 | if (mRecordThreads.valueAt(i)->hasAudioSession(sessionId) != 0) { |
| 5565 | io = mRecordThreads.keyAt(i); |
| 5566 | break; |
| 5567 | } |
| 5568 | } |
| 5569 | } |
Eric Laurent | 98c9259 | 2010-09-23 16:10:16 -0700 | [diff] [blame] | 5570 | // If no output thread contains the requested session ID, default to |
| 5571 | // first output. The effect chain will be moved to the correct output |
| 5572 | // thread when a track with the same session ID is created |
Eric Laurent | 464d5b3 | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 5573 | if (io == 0 && mPlaybackThreads.size()) { |
| 5574 | io = mPlaybackThreads.keyAt(0); |
| 5575 | } |
Steve Block | 71f2cf1 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 5576 | ALOGV("createEffect() got io %d for effect %s", io, desc.name); |
Eric Laurent | 464d5b3 | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 5577 | } |
| 5578 | ThreadBase *thread = checkRecordThread_l(io); |
| 5579 | if (thread == NULL) { |
| 5580 | thread = checkPlaybackThread_l(io); |
| 5581 | if (thread == NULL) { |
Steve Block | 3762c31 | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 5582 | ALOGE("createEffect() unknown output thread"); |
Eric Laurent | 464d5b3 | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 5583 | lStatus = BAD_VALUE; |
| 5584 | goto Exit; |
Eric Laurent | 98c9259 | 2010-09-23 16:10:16 -0700 | [diff] [blame] | 5585 | } |
Eric Laurent | 65b6545 | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 5586 | } |
Eric Laurent | 98c9259 | 2010-09-23 16:10:16 -0700 | [diff] [blame] | 5587 | |
Eric Laurent | 65b6545 | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 5588 | wclient = mClients.valueFor(pid); |
| 5589 | |
| 5590 | if (wclient != NULL) { |
| 5591 | client = wclient.promote(); |
| 5592 | } else { |
| 5593 | client = new Client(this, pid); |
| 5594 | mClients.add(pid, client); |
| 5595 | } |
| 5596 | |
Marco Nelissen | c74b93f | 2011-08-02 13:33:41 -0700 | [diff] [blame] | 5597 | // create effect on selected output thread |
Eric Laurent | 8ed6ed0 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 5598 | handle = thread->createEffect_l(client, effectClient, priority, sessionId, |
| 5599 | &desc, enabled, &lStatus); |
Eric Laurent | 65b6545 | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 5600 | if (handle != 0 && id != NULL) { |
| 5601 | *id = handle->id(); |
| 5602 | } |
| 5603 | } |
| 5604 | |
| 5605 | Exit: |
| 5606 | if(status) { |
| 5607 | *status = lStatus; |
| 5608 | } |
| 5609 | return handle; |
| 5610 | } |
| 5611 | |
Eric Laurent | f82fccd | 2011-07-27 19:49:51 -0700 | [diff] [blame] | 5612 | status_t AudioFlinger::moveEffects(int sessionId, int srcOutput, int dstOutput) |
Eric Laurent | 8ed6ed0 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 5613 | { |
Steve Block | 71f2cf1 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 5614 | ALOGV("moveEffects() session %d, srcOutput %d, dstOutput %d", |
Eric Laurent | f82fccd | 2011-07-27 19:49:51 -0700 | [diff] [blame] | 5615 | sessionId, srcOutput, dstOutput); |
Eric Laurent | 8ed6ed0 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 5616 | Mutex::Autolock _l(mLock); |
| 5617 | if (srcOutput == dstOutput) { |
Steve Block | 8564c8d | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 5618 | ALOGW("moveEffects() same dst and src outputs %d", dstOutput); |
Eric Laurent | 8ed6ed0 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 5619 | return NO_ERROR; |
Eric Laurent | 53334cd | 2010-06-23 17:38:20 -0700 | [diff] [blame] | 5620 | } |
Eric Laurent | 8ed6ed0 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 5621 | PlaybackThread *srcThread = checkPlaybackThread_l(srcOutput); |
| 5622 | if (srcThread == NULL) { |
Steve Block | 8564c8d | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 5623 | ALOGW("moveEffects() bad srcOutput %d", srcOutput); |
Eric Laurent | 8ed6ed0 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 5624 | return BAD_VALUE; |
Eric Laurent | 53334cd | 2010-06-23 17:38:20 -0700 | [diff] [blame] | 5625 | } |
Eric Laurent | 8ed6ed0 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 5626 | PlaybackThread *dstThread = checkPlaybackThread_l(dstOutput); |
| 5627 | if (dstThread == NULL) { |
Steve Block | 8564c8d | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 5628 | ALOGW("moveEffects() bad dstOutput %d", dstOutput); |
Eric Laurent | 8ed6ed0 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 5629 | return BAD_VALUE; |
| 5630 | } |
| 5631 | |
| 5632 | Mutex::Autolock _dl(dstThread->mLock); |
| 5633 | Mutex::Autolock _sl(srcThread->mLock); |
Eric Laurent | f82fccd | 2011-07-27 19:49:51 -0700 | [diff] [blame] | 5634 | moveEffectChain_l(sessionId, srcThread, dstThread, false); |
Eric Laurent | 8ed6ed0 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 5635 | |
Eric Laurent | 53334cd | 2010-06-23 17:38:20 -0700 | [diff] [blame] | 5636 | return NO_ERROR; |
| 5637 | } |
| 5638 | |
Marco Nelissen | c74b93f | 2011-08-02 13:33:41 -0700 | [diff] [blame] | 5639 | // moveEffectChain_l must be called with both srcThread and dstThread mLocks held |
Eric Laurent | f82fccd | 2011-07-27 19:49:51 -0700 | [diff] [blame] | 5640 | status_t AudioFlinger::moveEffectChain_l(int sessionId, |
Eric Laurent | 8ed6ed0 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 5641 | AudioFlinger::PlaybackThread *srcThread, |
Eric Laurent | 493941b | 2010-07-28 01:32:47 -0700 | [diff] [blame] | 5642 | AudioFlinger::PlaybackThread *dstThread, |
| 5643 | bool reRegister) |
Eric Laurent | 8ed6ed0 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 5644 | { |
Steve Block | 71f2cf1 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 5645 | ALOGV("moveEffectChain_l() session %d from thread %p to thread %p", |
Eric Laurent | f82fccd | 2011-07-27 19:49:51 -0700 | [diff] [blame] | 5646 | sessionId, srcThread, dstThread); |
Eric Laurent | 8ed6ed0 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 5647 | |
Eric Laurent | f82fccd | 2011-07-27 19:49:51 -0700 | [diff] [blame] | 5648 | sp<EffectChain> chain = srcThread->getEffectChain_l(sessionId); |
Eric Laurent | 8ed6ed0 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 5649 | if (chain == 0) { |
Steve Block | 8564c8d | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 5650 | ALOGW("moveEffectChain_l() effect chain for session %d not on source thread %p", |
Eric Laurent | f82fccd | 2011-07-27 19:49:51 -0700 | [diff] [blame] | 5651 | sessionId, srcThread); |
Eric Laurent | 8ed6ed0 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 5652 | return INVALID_OPERATION; |
| 5653 | } |
| 5654 | |
Eric Laurent | 493941b | 2010-07-28 01:32:47 -0700 | [diff] [blame] | 5655 | // remove chain first. This is useful only if reconfiguring effect chain on same output thread, |
Eric Laurent | 8ed6ed0 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 5656 | // so that a new chain is created with correct parameters when first effect is added. This is |
Eric Laurent | 6fccbd0 | 2011-10-05 17:42:25 -0700 | [diff] [blame] | 5657 | // otherwise unnecessary as removeEffect_l() will remove the chain when last effect is |
Eric Laurent | 8ed6ed0 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 5658 | // removed. |
| 5659 | srcThread->removeEffectChain_l(chain); |
| 5660 | |
| 5661 | // transfer all effects one by one so that new effect chain is created on new thread with |
| 5662 | // correct buffer sizes and audio parameters and effect engines reconfigured accordingly |
Eric Laurent | 493941b | 2010-07-28 01:32:47 -0700 | [diff] [blame] | 5663 | int dstOutput = dstThread->id(); |
| 5664 | sp<EffectChain> dstChain; |
Marco Nelissen | c74b93f | 2011-08-02 13:33:41 -0700 | [diff] [blame] | 5665 | uint32_t strategy = 0; // prevent compiler warning |
Eric Laurent | 8ed6ed0 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 5666 | sp<EffectModule> effect = chain->getEffectFromId_l(0); |
| 5667 | while (effect != 0) { |
| 5668 | srcThread->removeEffect_l(effect); |
| 5669 | dstThread->addEffect_l(effect); |
Eric Laurent | 6fccbd0 | 2011-10-05 17:42:25 -0700 | [diff] [blame] | 5670 | // removeEffect_l() has stopped the effect if it was active so it must be restarted |
| 5671 | if (effect->state() == EffectModule::ACTIVE || |
| 5672 | effect->state() == EffectModule::STOPPING) { |
| 5673 | effect->start(); |
| 5674 | } |
Eric Laurent | 493941b | 2010-07-28 01:32:47 -0700 | [diff] [blame] | 5675 | // if the move request is not received from audio policy manager, the effect must be |
| 5676 | // re-registered with the new strategy and output |
| 5677 | if (dstChain == 0) { |
| 5678 | dstChain = effect->chain().promote(); |
| 5679 | if (dstChain == 0) { |
Steve Block | 8564c8d | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 5680 | ALOGW("moveEffectChain_l() cannot get chain from effect %p", effect.get()); |
Eric Laurent | 493941b | 2010-07-28 01:32:47 -0700 | [diff] [blame] | 5681 | srcThread->addEffect_l(effect); |
| 5682 | return NO_INIT; |
| 5683 | } |
| 5684 | strategy = dstChain->strategy(); |
| 5685 | } |
| 5686 | if (reRegister) { |
| 5687 | AudioSystem::unregisterEffect(effect->id()); |
| 5688 | AudioSystem::registerEffect(&effect->desc(), |
| 5689 | dstOutput, |
| 5690 | strategy, |
Eric Laurent | f82fccd | 2011-07-27 19:49:51 -0700 | [diff] [blame] | 5691 | sessionId, |
Eric Laurent | 493941b | 2010-07-28 01:32:47 -0700 | [diff] [blame] | 5692 | effect->id()); |
| 5693 | } |
Eric Laurent | 8ed6ed0 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 5694 | effect = chain->getEffectFromId_l(0); |
| 5695 | } |
| 5696 | |
| 5697 | return NO_ERROR; |
Eric Laurent | 53334cd | 2010-06-23 17:38:20 -0700 | [diff] [blame] | 5698 | } |
| 5699 | |
Eric Laurent | 464d5b3 | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 5700 | |
Eric Laurent | 65b6545 | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 5701 | // PlaybackThread::createEffect_l() must be called with AudioFlinger::mLock held |
Eric Laurent | 464d5b3 | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 5702 | sp<AudioFlinger::EffectHandle> AudioFlinger::ThreadBase::createEffect_l( |
Eric Laurent | 65b6545 | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 5703 | const sp<AudioFlinger::Client>& client, |
| 5704 | const sp<IEffectClient>& effectClient, |
| 5705 | int32_t priority, |
| 5706 | int sessionId, |
| 5707 | effect_descriptor_t *desc, |
| 5708 | int *enabled, |
| 5709 | status_t *status |
| 5710 | ) |
| 5711 | { |
| 5712 | sp<EffectModule> effect; |
| 5713 | sp<EffectHandle> handle; |
| 5714 | status_t lStatus; |
Eric Laurent | 65b6545 | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 5715 | sp<EffectChain> chain; |
Eric Laurent | 8ed6ed0 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 5716 | bool chainCreated = false; |
Eric Laurent | 65b6545 | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 5717 | bool effectCreated = false; |
Eric Laurent | 53334cd | 2010-06-23 17:38:20 -0700 | [diff] [blame] | 5718 | bool effectRegistered = false; |
Eric Laurent | 65b6545 | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 5719 | |
Eric Laurent | 464d5b3 | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 5720 | lStatus = initCheck(); |
| 5721 | if (lStatus != NO_ERROR) { |
Steve Block | 8564c8d | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 5722 | ALOGW("createEffect_l() Audio driver not initialized."); |
Eric Laurent | 65b6545 | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 5723 | goto Exit; |
| 5724 | } |
| 5725 | |
| 5726 | // Do not allow effects with session ID 0 on direct output or duplicating threads |
| 5727 | // TODO: add rule for hw accelerated effects on direct outputs with non PCM format |
Dima Zavin | 24fc2fb | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 5728 | if (sessionId == AUDIO_SESSION_OUTPUT_MIX && mType != MIXER) { |
Steve Block | 8564c8d | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 5729 | ALOGW("createEffect_l() Cannot add auxiliary effect %s to session %d", |
Eric Laurent | 8ed6ed0 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 5730 | desc->name, sessionId); |
Eric Laurent | 65b6545 | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 5731 | lStatus = BAD_VALUE; |
| 5732 | goto Exit; |
| 5733 | } |
Eric Laurent | 464d5b3 | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 5734 | // Only Pre processor effects are allowed on input threads and only on input threads |
| 5735 | if ((mType == RECORD && |
| 5736 | (desc->flags & EFFECT_FLAG_TYPE_MASK) != EFFECT_FLAG_TYPE_PRE_PROC) || |
| 5737 | (mType != RECORD && |
| 5738 | (desc->flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_PRE_PROC)) { |
Steve Block | 8564c8d | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 5739 | ALOGW("createEffect_l() effect %s (flags %08x) created on wrong thread type %d", |
Eric Laurent | 464d5b3 | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 5740 | desc->name, desc->flags, mType); |
| 5741 | lStatus = BAD_VALUE; |
| 5742 | goto Exit; |
| 5743 | } |
Eric Laurent | 65b6545 | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 5744 | |
Steve Block | 71f2cf1 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 5745 | ALOGV("createEffect_l() thread %p effect %s on session %d", this, desc->name, sessionId); |
Eric Laurent | 65b6545 | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 5746 | |
| 5747 | { // scope for mLock |
| 5748 | Mutex::Autolock _l(mLock); |
| 5749 | |
| 5750 | // check for existing effect chain with the requested audio session |
| 5751 | chain = getEffectChain_l(sessionId); |
| 5752 | if (chain == 0) { |
| 5753 | // create a new chain for this session |
Steve Block | 71f2cf1 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 5754 | ALOGV("createEffect_l() new effect chain for session %d", sessionId); |
Eric Laurent | 65b6545 | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 5755 | chain = new EffectChain(this, sessionId); |
| 5756 | addEffectChain_l(chain); |
Eric Laurent | 8ed6ed0 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 5757 | chain->setStrategy(getStrategyForSession_l(sessionId)); |
| 5758 | chainCreated = true; |
Eric Laurent | 65b6545 | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 5759 | } else { |
Eric Laurent | 76c40f7 | 2010-07-15 12:50:15 -0700 | [diff] [blame] | 5760 | effect = chain->getEffectFromDesc_l(desc); |
Eric Laurent | 65b6545 | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 5761 | } |
| 5762 | |
Glenn Kasten | 70ed6b7 | 2012-01-10 10:46:34 -0800 | [diff] [blame] | 5763 | ALOGV("createEffect_l() got effect %p on chain %p", effect.get(), chain.get()); |
Eric Laurent | 65b6545 | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 5764 | |
| 5765 | if (effect == 0) { |
Eric Laurent | 464d5b3 | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 5766 | int id = mAudioFlinger->nextUniqueId(); |
Eric Laurent | 53334cd | 2010-06-23 17:38:20 -0700 | [diff] [blame] | 5767 | // Check CPU and memory usage |
Eric Laurent | 8ed6ed0 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 5768 | lStatus = AudioSystem::registerEffect(desc, mId, chain->strategy(), sessionId, id); |
Eric Laurent | 53334cd | 2010-06-23 17:38:20 -0700 | [diff] [blame] | 5769 | if (lStatus != NO_ERROR) { |
| 5770 | goto Exit; |
| 5771 | } |
| 5772 | effectRegistered = true; |
Eric Laurent | 65b6545 | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 5773 | // create a new effect module if none present in the chain |
Eric Laurent | 8ed6ed0 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 5774 | effect = new EffectModule(this, chain, desc, id, sessionId); |
Eric Laurent | 65b6545 | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 5775 | lStatus = effect->status(); |
| 5776 | if (lStatus != NO_ERROR) { |
| 5777 | goto Exit; |
| 5778 | } |
Eric Laurent | 76c40f7 | 2010-07-15 12:50:15 -0700 | [diff] [blame] | 5779 | lStatus = chain->addEffect_l(effect); |
Eric Laurent | 65b6545 | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 5780 | if (lStatus != NO_ERROR) { |
| 5781 | goto Exit; |
| 5782 | } |
Eric Laurent | 53334cd | 2010-06-23 17:38:20 -0700 | [diff] [blame] | 5783 | effectCreated = true; |
Eric Laurent | 65b6545 | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 5784 | |
| 5785 | effect->setDevice(mDevice); |
Eric Laurent | 53334cd | 2010-06-23 17:38:20 -0700 | [diff] [blame] | 5786 | effect->setMode(mAudioFlinger->getMode()); |
Eric Laurent | 65b6545 | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 5787 | } |
| 5788 | // create effect handle and connect it to effect module |
| 5789 | handle = new EffectHandle(effect, client, effectClient, priority); |
| 5790 | lStatus = effect->addHandle(handle); |
| 5791 | if (enabled) { |
| 5792 | *enabled = (int)effect->isEnabled(); |
| 5793 | } |
| 5794 | } |
| 5795 | |
| 5796 | Exit: |
| 5797 | if (lStatus != NO_ERROR && lStatus != ALREADY_EXISTS) { |
Eric Laurent | 8ed6ed0 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 5798 | Mutex::Autolock _l(mLock); |
Eric Laurent | 53334cd | 2010-06-23 17:38:20 -0700 | [diff] [blame] | 5799 | if (effectCreated) { |
Eric Laurent | 8ed6ed0 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 5800 | chain->removeEffect_l(effect); |
Eric Laurent | 65b6545 | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 5801 | } |
Eric Laurent | 53334cd | 2010-06-23 17:38:20 -0700 | [diff] [blame] | 5802 | if (effectRegistered) { |
Eric Laurent | 8ed6ed0 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 5803 | AudioSystem::unregisterEffect(effect->id()); |
| 5804 | } |
| 5805 | if (chainCreated) { |
| 5806 | removeEffectChain_l(chain); |
Eric Laurent | 53334cd | 2010-06-23 17:38:20 -0700 | [diff] [blame] | 5807 | } |
Eric Laurent | 65b6545 | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 5808 | handle.clear(); |
| 5809 | } |
| 5810 | |
| 5811 | if(status) { |
| 5812 | *status = lStatus; |
| 5813 | } |
| 5814 | return handle; |
| 5815 | } |
| 5816 | |
Eric Laurent | 464d5b3 | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 5817 | sp<AudioFlinger::EffectModule> AudioFlinger::ThreadBase::getEffect_l(int sessionId, int effectId) |
| 5818 | { |
| 5819 | sp<EffectModule> effect; |
| 5820 | |
| 5821 | sp<EffectChain> chain = getEffectChain_l(sessionId); |
| 5822 | if (chain != 0) { |
| 5823 | effect = chain->getEffectFromId_l(effectId); |
| 5824 | } |
| 5825 | return effect; |
| 5826 | } |
| 5827 | |
Eric Laurent | 8ed6ed0 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 5828 | // PlaybackThread::addEffect_l() must be called with AudioFlinger::mLock and |
| 5829 | // PlaybackThread::mLock held |
Eric Laurent | 464d5b3 | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 5830 | status_t AudioFlinger::ThreadBase::addEffect_l(const sp<EffectModule>& effect) |
Eric Laurent | 8ed6ed0 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 5831 | { |
| 5832 | // check for existing effect chain with the requested audio session |
| 5833 | int sessionId = effect->sessionId(); |
| 5834 | sp<EffectChain> chain = getEffectChain_l(sessionId); |
| 5835 | bool chainCreated = false; |
| 5836 | |
| 5837 | if (chain == 0) { |
| 5838 | // create a new chain for this session |
Steve Block | 71f2cf1 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 5839 | ALOGV("addEffect_l() new effect chain for session %d", sessionId); |
Eric Laurent | 8ed6ed0 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 5840 | chain = new EffectChain(this, sessionId); |
| 5841 | addEffectChain_l(chain); |
| 5842 | chain->setStrategy(getStrategyForSession_l(sessionId)); |
| 5843 | chainCreated = true; |
| 5844 | } |
Steve Block | 71f2cf1 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 5845 | ALOGV("addEffect_l() %p chain %p effect %p", this, chain.get(), effect.get()); |
Eric Laurent | 8ed6ed0 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 5846 | |
| 5847 | if (chain->getEffectFromId_l(effect->id()) != 0) { |
Steve Block | 8564c8d | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 5848 | ALOGW("addEffect_l() %p effect %s already present in chain %p", |
Eric Laurent | 8ed6ed0 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 5849 | this, effect->desc().name, chain.get()); |
| 5850 | return BAD_VALUE; |
| 5851 | } |
| 5852 | |
| 5853 | status_t status = chain->addEffect_l(effect); |
| 5854 | if (status != NO_ERROR) { |
| 5855 | if (chainCreated) { |
| 5856 | removeEffectChain_l(chain); |
| 5857 | } |
| 5858 | return status; |
| 5859 | } |
| 5860 | |
| 5861 | effect->setDevice(mDevice); |
| 5862 | effect->setMode(mAudioFlinger->getMode()); |
| 5863 | return NO_ERROR; |
| 5864 | } |
| 5865 | |
Eric Laurent | 464d5b3 | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 5866 | void AudioFlinger::ThreadBase::removeEffect_l(const sp<EffectModule>& effect) { |
Eric Laurent | 8ed6ed0 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 5867 | |
Steve Block | 71f2cf1 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 5868 | ALOGV("removeEffect_l() %p effect %p", this, effect.get()); |
Eric Laurent | 53334cd | 2010-06-23 17:38:20 -0700 | [diff] [blame] | 5869 | effect_descriptor_t desc = effect->desc(); |
Eric Laurent | 8ed6ed0 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 5870 | if ((desc.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) { |
| 5871 | detachAuxEffect_l(effect->id()); |
| 5872 | } |
| 5873 | |
| 5874 | sp<EffectChain> chain = effect->chain().promote(); |
| 5875 | if (chain != 0) { |
| 5876 | // remove effect chain if removing last effect |
| 5877 | if (chain->removeEffect_l(effect) == 0) { |
| 5878 | removeEffectChain_l(chain); |
| 5879 | } |
| 5880 | } else { |
Steve Block | 8564c8d | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 5881 | ALOGW("removeEffect_l() %p cannot promote chain for effect %p", this, effect.get()); |
Eric Laurent | 8ed6ed0 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 5882 | } |
| 5883 | } |
| 5884 | |
Eric Laurent | 464d5b3 | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 5885 | void AudioFlinger::ThreadBase::lockEffectChains_l( |
| 5886 | Vector<sp <AudioFlinger::EffectChain> >& effectChains) |
| 5887 | { |
| 5888 | effectChains = mEffectChains; |
| 5889 | for (size_t i = 0; i < mEffectChains.size(); i++) { |
| 5890 | mEffectChains[i]->lock(); |
| 5891 | } |
| 5892 | } |
| 5893 | |
| 5894 | void AudioFlinger::ThreadBase::unlockEffectChains( |
| 5895 | Vector<sp <AudioFlinger::EffectChain> >& effectChains) |
| 5896 | { |
| 5897 | for (size_t i = 0; i < effectChains.size(); i++) { |
| 5898 | effectChains[i]->unlock(); |
| 5899 | } |
| 5900 | } |
| 5901 | |
| 5902 | sp<AudioFlinger::EffectChain> AudioFlinger::ThreadBase::getEffectChain(int sessionId) |
| 5903 | { |
| 5904 | Mutex::Autolock _l(mLock); |
| 5905 | return getEffectChain_l(sessionId); |
| 5906 | } |
| 5907 | |
| 5908 | sp<AudioFlinger::EffectChain> AudioFlinger::ThreadBase::getEffectChain_l(int sessionId) |
| 5909 | { |
| 5910 | sp<EffectChain> chain; |
| 5911 | |
| 5912 | size_t size = mEffectChains.size(); |
| 5913 | for (size_t i = 0; i < size; i++) { |
| 5914 | if (mEffectChains[i]->sessionId() == sessionId) { |
| 5915 | chain = mEffectChains[i]; |
| 5916 | break; |
| 5917 | } |
| 5918 | } |
| 5919 | return chain; |
| 5920 | } |
| 5921 | |
Glenn Kasten | accb114 | 2012-01-04 11:00:47 -0800 | [diff] [blame] | 5922 | void AudioFlinger::ThreadBase::setMode(audio_mode_t mode) |
Eric Laurent | 464d5b3 | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 5923 | { |
| 5924 | Mutex::Autolock _l(mLock); |
| 5925 | size_t size = mEffectChains.size(); |
| 5926 | for (size_t i = 0; i < size; i++) { |
| 5927 | mEffectChains[i]->setMode_l(mode); |
| 5928 | } |
| 5929 | } |
| 5930 | |
| 5931 | void AudioFlinger::ThreadBase::disconnectEffect(const sp<EffectModule>& effect, |
Marco Nelissen | c74b93f | 2011-08-02 13:33:41 -0700 | [diff] [blame] | 5932 | const wp<EffectHandle>& handle, |
| 5933 | bool unpiniflast) { |
Eric Laurent | f82fccd | 2011-07-27 19:49:51 -0700 | [diff] [blame] | 5934 | |
Eric Laurent | 53334cd | 2010-06-23 17:38:20 -0700 | [diff] [blame] | 5935 | Mutex::Autolock _l(mLock); |
Steve Block | 71f2cf1 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 5936 | ALOGV("disconnectEffect() %p effect %p", this, effect.get()); |
Eric Laurent | 53334cd | 2010-06-23 17:38:20 -0700 | [diff] [blame] | 5937 | // delete the effect module if removing last handle on it |
| 5938 | if (effect->removeHandle(handle) == 0) { |
Marco Nelissen | c74b93f | 2011-08-02 13:33:41 -0700 | [diff] [blame] | 5939 | if (!effect->isPinned() || unpiniflast) { |
| 5940 | removeEffect_l(effect); |
| 5941 | AudioSystem::unregisterEffect(effect->id()); |
| 5942 | } |
Eric Laurent | 53334cd | 2010-06-23 17:38:20 -0700 | [diff] [blame] | 5943 | } |
| 5944 | } |
| 5945 | |
Eric Laurent | 65b6545 | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 5946 | status_t AudioFlinger::PlaybackThread::addEffectChain_l(const sp<EffectChain>& chain) |
| 5947 | { |
| 5948 | int session = chain->sessionId(); |
| 5949 | int16_t *buffer = mMixBuffer; |
Eric Laurent | 53334cd | 2010-06-23 17:38:20 -0700 | [diff] [blame] | 5950 | bool ownsBuffer = false; |
Eric Laurent | 65b6545 | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 5951 | |
Steve Block | 71f2cf1 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 5952 | ALOGV("addEffectChain_l() %p on thread %p for session %d", chain.get(), this, session); |
Eric Laurent | 53334cd | 2010-06-23 17:38:20 -0700 | [diff] [blame] | 5953 | if (session > 0) { |
Eric Laurent | 65b6545 | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 5954 | // Only one effect chain can be present in direct output thread and it uses |
| 5955 | // the mix buffer as input |
| 5956 | if (mType != DIRECT) { |
| 5957 | size_t numSamples = mFrameCount * mChannelCount; |
| 5958 | buffer = new int16_t[numSamples]; |
| 5959 | memset(buffer, 0, numSamples * sizeof(int16_t)); |
Steve Block | 71f2cf1 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 5960 | ALOGV("addEffectChain_l() creating new input buffer %p session %d", buffer, session); |
Eric Laurent | 65b6545 | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 5961 | ownsBuffer = true; |
| 5962 | } |
Eric Laurent | 65b6545 | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 5963 | |
Eric Laurent | 53334cd | 2010-06-23 17:38:20 -0700 | [diff] [blame] | 5964 | // Attach all tracks with same session ID to this chain. |
| 5965 | for (size_t i = 0; i < mTracks.size(); ++i) { |
| 5966 | sp<Track> track = mTracks[i]; |
| 5967 | if (session == track->sessionId()) { |
Steve Block | 71f2cf1 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 5968 | ALOGV("addEffectChain_l() track->setMainBuffer track %p buffer %p", track.get(), buffer); |
Eric Laurent | 53334cd | 2010-06-23 17:38:20 -0700 | [diff] [blame] | 5969 | track->setMainBuffer(buffer); |
Eric Laurent | 90681d6 | 2011-05-09 12:09:06 -0700 | [diff] [blame] | 5970 | chain->incTrackCnt(); |
Eric Laurent | 53334cd | 2010-06-23 17:38:20 -0700 | [diff] [blame] | 5971 | } |
| 5972 | } |
| 5973 | |
| 5974 | // indicate all active tracks in the chain |
| 5975 | for (size_t i = 0 ; i < mActiveTracks.size() ; ++i) { |
| 5976 | sp<Track> track = mActiveTracks[i].promote(); |
| 5977 | if (track == 0) continue; |
| 5978 | if (session == track->sessionId()) { |
Steve Block | 71f2cf1 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 5979 | ALOGV("addEffectChain_l() activating track %p on session %d", track.get(), session); |
Eric Laurent | 90681d6 | 2011-05-09 12:09:06 -0700 | [diff] [blame] | 5980 | chain->incActiveTrackCnt(); |
Eric Laurent | 53334cd | 2010-06-23 17:38:20 -0700 | [diff] [blame] | 5981 | } |
Eric Laurent | 65b6545 | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 5982 | } |
| 5983 | } |
| 5984 | |
Eric Laurent | 53334cd | 2010-06-23 17:38:20 -0700 | [diff] [blame] | 5985 | chain->setInBuffer(buffer, ownsBuffer); |
| 5986 | chain->setOutBuffer(mMixBuffer); |
Dima Zavin | 24fc2fb | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 5987 | // Effect chain for session AUDIO_SESSION_OUTPUT_STAGE is inserted at end of effect |
Eric Laurent | 8ed6ed0 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 5988 | // chains list in order to be processed last as it contains output stage effects |
Dima Zavin | 24fc2fb | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 5989 | // Effect chain for session AUDIO_SESSION_OUTPUT_MIX is inserted before |
| 5990 | // session AUDIO_SESSION_OUTPUT_STAGE to be processed |
Eric Laurent | 53334cd | 2010-06-23 17:38:20 -0700 | [diff] [blame] | 5991 | // after track specific effects and before output stage |
Dima Zavin | 24fc2fb | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 5992 | // It is therefore mandatory that AUDIO_SESSION_OUTPUT_MIX == 0 and |
| 5993 | // that AUDIO_SESSION_OUTPUT_STAGE < AUDIO_SESSION_OUTPUT_MIX |
Eric Laurent | 8ed6ed0 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 5994 | // Effect chain for other sessions are inserted at beginning of effect |
| 5995 | // chains list to be processed before output mix effects. Relative order between other |
| 5996 | // sessions is not important |
Eric Laurent | 53334cd | 2010-06-23 17:38:20 -0700 | [diff] [blame] | 5997 | size_t size = mEffectChains.size(); |
| 5998 | size_t i = 0; |
| 5999 | for (i = 0; i < size; i++) { |
| 6000 | if (mEffectChains[i]->sessionId() < session) break; |
Eric Laurent | 65b6545 | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 6001 | } |
Eric Laurent | 53334cd | 2010-06-23 17:38:20 -0700 | [diff] [blame] | 6002 | mEffectChains.insertAt(chain, i); |
Eric Laurent | f82fccd | 2011-07-27 19:49:51 -0700 | [diff] [blame] | 6003 | checkSuspendOnAddEffectChain_l(chain); |
Eric Laurent | 65b6545 | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 6004 | |
| 6005 | return NO_ERROR; |
| 6006 | } |
| 6007 | |
| 6008 | size_t AudioFlinger::PlaybackThread::removeEffectChain_l(const sp<EffectChain>& chain) |
| 6009 | { |
| 6010 | int session = chain->sessionId(); |
| 6011 | |
Steve Block | 71f2cf1 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 6012 | ALOGV("removeEffectChain_l() %p from thread %p for session %d", chain.get(), this, session); |
Eric Laurent | 65b6545 | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 6013 | |
| 6014 | for (size_t i = 0; i < mEffectChains.size(); i++) { |
| 6015 | if (chain == mEffectChains[i]) { |
| 6016 | mEffectChains.removeAt(i); |
Eric Laurent | 90681d6 | 2011-05-09 12:09:06 -0700 | [diff] [blame] | 6017 | // detach all active tracks from the chain |
| 6018 | for (size_t i = 0 ; i < mActiveTracks.size() ; ++i) { |
| 6019 | sp<Track> track = mActiveTracks[i].promote(); |
| 6020 | if (track == 0) continue; |
| 6021 | if (session == track->sessionId()) { |
Steve Block | 71f2cf1 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 6022 | ALOGV("removeEffectChain_l(): stopping track on chain %p for session Id: %d", |
Eric Laurent | 90681d6 | 2011-05-09 12:09:06 -0700 | [diff] [blame] | 6023 | chain.get(), session); |
| 6024 | chain->decActiveTrackCnt(); |
| 6025 | } |
| 6026 | } |
| 6027 | |
Eric Laurent | 65b6545 | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 6028 | // detach all tracks with same session ID from this chain |
| 6029 | for (size_t i = 0; i < mTracks.size(); ++i) { |
| 6030 | sp<Track> track = mTracks[i]; |
| 6031 | if (session == track->sessionId()) { |
| 6032 | track->setMainBuffer(mMixBuffer); |
Eric Laurent | 90681d6 | 2011-05-09 12:09:06 -0700 | [diff] [blame] | 6033 | chain->decTrackCnt(); |
Eric Laurent | 65b6545 | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 6034 | } |
| 6035 | } |
Eric Laurent | 8ed6ed0 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 6036 | break; |
Eric Laurent | 65b6545 | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 6037 | } |
| 6038 | } |
| 6039 | return mEffectChains.size(); |
| 6040 | } |
| 6041 | |
Eric Laurent | 8ed6ed0 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 6042 | status_t AudioFlinger::PlaybackThread::attachAuxEffect( |
| 6043 | const sp<AudioFlinger::PlaybackThread::Track> track, int EffectId) |
Eric Laurent | 65b6545 | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 6044 | { |
| 6045 | Mutex::Autolock _l(mLock); |
| 6046 | return attachAuxEffect_l(track, EffectId); |
| 6047 | } |
| 6048 | |
Eric Laurent | 8ed6ed0 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 6049 | status_t AudioFlinger::PlaybackThread::attachAuxEffect_l( |
| 6050 | const sp<AudioFlinger::PlaybackThread::Track> track, int EffectId) |
Eric Laurent | 65b6545 | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 6051 | { |
| 6052 | status_t status = NO_ERROR; |
| 6053 | |
| 6054 | if (EffectId == 0) { |
| 6055 | track->setAuxBuffer(0, NULL); |
| 6056 | } else { |
Dima Zavin | 24fc2fb | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 6057 | // Auxiliary effects are always in audio session AUDIO_SESSION_OUTPUT_MIX |
| 6058 | sp<EffectModule> effect = getEffect_l(AUDIO_SESSION_OUTPUT_MIX, EffectId); |
Eric Laurent | 65b6545 | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 6059 | if (effect != 0) { |
| 6060 | if ((effect->desc().flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) { |
| 6061 | track->setAuxBuffer(EffectId, (int32_t *)effect->inBuffer()); |
| 6062 | } else { |
| 6063 | status = INVALID_OPERATION; |
| 6064 | } |
| 6065 | } else { |
| 6066 | status = BAD_VALUE; |
| 6067 | } |
| 6068 | } |
| 6069 | return status; |
| 6070 | } |
| 6071 | |
| 6072 | void AudioFlinger::PlaybackThread::detachAuxEffect_l(int effectId) |
| 6073 | { |
| 6074 | for (size_t i = 0; i < mTracks.size(); ++i) { |
| 6075 | sp<Track> track = mTracks[i]; |
| 6076 | if (track->auxEffectId() == effectId) { |
| 6077 | attachAuxEffect_l(track, 0); |
| 6078 | } |
| 6079 | } |
| 6080 | } |
| 6081 | |
Eric Laurent | 464d5b3 | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 6082 | status_t AudioFlinger::RecordThread::addEffectChain_l(const sp<EffectChain>& chain) |
| 6083 | { |
| 6084 | // only one chain per input thread |
| 6085 | if (mEffectChains.size() != 0) { |
| 6086 | return INVALID_OPERATION; |
| 6087 | } |
Steve Block | 71f2cf1 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 6088 | ALOGV("addEffectChain_l() %p on thread %p", chain.get(), this); |
Eric Laurent | 464d5b3 | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 6089 | |
| 6090 | chain->setInBuffer(NULL); |
| 6091 | chain->setOutBuffer(NULL); |
| 6092 | |
Eric Laurent | f82fccd | 2011-07-27 19:49:51 -0700 | [diff] [blame] | 6093 | checkSuspendOnAddEffectChain_l(chain); |
| 6094 | |
Eric Laurent | 464d5b3 | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 6095 | mEffectChains.add(chain); |
| 6096 | |
| 6097 | return NO_ERROR; |
| 6098 | } |
| 6099 | |
| 6100 | size_t AudioFlinger::RecordThread::removeEffectChain_l(const sp<EffectChain>& chain) |
| 6101 | { |
Steve Block | 71f2cf1 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 6102 | ALOGV("removeEffectChain_l() %p from thread %p", chain.get(), this); |
Steve Block | 8564c8d | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 6103 | ALOGW_IF(mEffectChains.size() != 1, |
Eric Laurent | 464d5b3 | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 6104 | "removeEffectChain_l() %p invalid chain size %d on thread %p", |
| 6105 | chain.get(), mEffectChains.size(), this); |
| 6106 | if (mEffectChains.size() == 1) { |
| 6107 | mEffectChains.removeAt(0); |
| 6108 | } |
| 6109 | return 0; |
| 6110 | } |
| 6111 | |
Eric Laurent | 65b6545 | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 6112 | // ---------------------------------------------------------------------------- |
| 6113 | // EffectModule implementation |
| 6114 | // ---------------------------------------------------------------------------- |
| 6115 | |
| 6116 | #undef LOG_TAG |
| 6117 | #define LOG_TAG "AudioFlinger::EffectModule" |
| 6118 | |
| 6119 | AudioFlinger::EffectModule::EffectModule(const wp<ThreadBase>& wThread, |
| 6120 | const wp<AudioFlinger::EffectChain>& chain, |
| 6121 | effect_descriptor_t *desc, |
| 6122 | int id, |
| 6123 | int sessionId) |
| 6124 | : mThread(wThread), mChain(chain), mId(id), mSessionId(sessionId), mEffectInterface(NULL), |
Eric Laurent | f82fccd | 2011-07-27 19:49:51 -0700 | [diff] [blame] | 6125 | mStatus(NO_INIT), mState(IDLE), mSuspended(false) |
Eric Laurent | 65b6545 | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 6126 | { |
Steve Block | 71f2cf1 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 6127 | ALOGV("Constructor %p", this); |
Eric Laurent | 65b6545 | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 6128 | int lStatus; |
| 6129 | sp<ThreadBase> thread = mThread.promote(); |
| 6130 | if (thread == 0) { |
| 6131 | return; |
| 6132 | } |
Eric Laurent | 65b6545 | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 6133 | |
| 6134 | memcpy(&mDescriptor, desc, sizeof(effect_descriptor_t)); |
| 6135 | |
| 6136 | // create effect engine from effect factory |
Eric Laurent | 464d5b3 | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 6137 | mStatus = EffectCreate(&desc->uuid, sessionId, thread->id(), &mEffectInterface); |
Eric Laurent | 53334cd | 2010-06-23 17:38:20 -0700 | [diff] [blame] | 6138 | |
Eric Laurent | 65b6545 | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 6139 | if (mStatus != NO_ERROR) { |
| 6140 | return; |
| 6141 | } |
| 6142 | lStatus = init(); |
| 6143 | if (lStatus < 0) { |
| 6144 | mStatus = lStatus; |
| 6145 | goto Error; |
| 6146 | } |
| 6147 | |
Marco Nelissen | c74b93f | 2011-08-02 13:33:41 -0700 | [diff] [blame] | 6148 | if (mSessionId > AUDIO_SESSION_OUTPUT_MIX) { |
| 6149 | mPinned = true; |
| 6150 | } |
Steve Block | 71f2cf1 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 6151 | ALOGV("Constructor success name %s, Interface %p", mDescriptor.name, mEffectInterface); |
Eric Laurent | 65b6545 | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 6152 | return; |
| 6153 | Error: |
| 6154 | EffectRelease(mEffectInterface); |
| 6155 | mEffectInterface = NULL; |
Steve Block | 71f2cf1 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 6156 | ALOGV("Constructor Error %d", mStatus); |
Eric Laurent | 65b6545 | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 6157 | } |
| 6158 | |
| 6159 | AudioFlinger::EffectModule::~EffectModule() |
| 6160 | { |
Steve Block | 71f2cf1 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 6161 | ALOGV("Destructor %p", this); |
Eric Laurent | 65b6545 | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 6162 | if (mEffectInterface != NULL) { |
Eric Laurent | 464d5b3 | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 6163 | if ((mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_PRE_PROC || |
| 6164 | (mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_POST_PROC) { |
| 6165 | sp<ThreadBase> thread = mThread.promote(); |
| 6166 | if (thread != 0) { |
Eric Laurent | 828b977 | 2011-08-07 16:32:26 -0700 | [diff] [blame] | 6167 | audio_stream_t *stream = thread->stream(); |
| 6168 | if (stream != NULL) { |
| 6169 | stream->remove_audio_effect(stream, mEffectInterface); |
| 6170 | } |
Eric Laurent | 464d5b3 | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 6171 | } |
| 6172 | } |
Eric Laurent | 65b6545 | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 6173 | // release effect engine |
| 6174 | EffectRelease(mEffectInterface); |
| 6175 | } |
| 6176 | } |
| 6177 | |
| 6178 | status_t AudioFlinger::EffectModule::addHandle(sp<EffectHandle>& handle) |
| 6179 | { |
| 6180 | status_t status; |
| 6181 | |
| 6182 | Mutex::Autolock _l(mLock); |
| 6183 | // First handle in mHandles has highest priority and controls the effect module |
| 6184 | int priority = handle->priority(); |
| 6185 | size_t size = mHandles.size(); |
| 6186 | sp<EffectHandle> h; |
| 6187 | size_t i; |
| 6188 | for (i = 0; i < size; i++) { |
| 6189 | h = mHandles[i].promote(); |
| 6190 | if (h == 0) continue; |
| 6191 | if (h->priority() <= priority) break; |
| 6192 | } |
| 6193 | // if inserted in first place, move effect control from previous owner to this handle |
| 6194 | if (i == 0) { |
Eric Laurent | f82fccd | 2011-07-27 19:49:51 -0700 | [diff] [blame] | 6195 | bool enabled = false; |
Eric Laurent | 65b6545 | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 6196 | if (h != 0) { |
Eric Laurent | f82fccd | 2011-07-27 19:49:51 -0700 | [diff] [blame] | 6197 | enabled = h->enabled(); |
| 6198 | h->setControl(false/*hasControl*/, true /*signal*/, enabled /*enabled*/); |
Eric Laurent | 65b6545 | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 6199 | } |
Eric Laurent | f82fccd | 2011-07-27 19:49:51 -0700 | [diff] [blame] | 6200 | handle->setControl(true /*hasControl*/, false /*signal*/, enabled /*enabled*/); |
Eric Laurent | 65b6545 | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 6201 | status = NO_ERROR; |
| 6202 | } else { |
| 6203 | status = ALREADY_EXISTS; |
| 6204 | } |
Steve Block | 71f2cf1 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 6205 | ALOGV("addHandle() %p added handle %p in position %d", this, handle.get(), i); |
Eric Laurent | 65b6545 | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 6206 | mHandles.insertAt(handle, i); |
| 6207 | return status; |
| 6208 | } |
| 6209 | |
| 6210 | size_t AudioFlinger::EffectModule::removeHandle(const wp<EffectHandle>& handle) |
| 6211 | { |
| 6212 | Mutex::Autolock _l(mLock); |
| 6213 | size_t size = mHandles.size(); |
| 6214 | size_t i; |
| 6215 | for (i = 0; i < size; i++) { |
| 6216 | if (mHandles[i] == handle) break; |
| 6217 | } |
| 6218 | if (i == size) { |
| 6219 | return size; |
| 6220 | } |
Steve Block | 71f2cf1 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 6221 | ALOGV("removeHandle() %p removed handle %p in position %d", this, handle.unsafe_get(), i); |
Eric Laurent | f82fccd | 2011-07-27 19:49:51 -0700 | [diff] [blame] | 6222 | |
| 6223 | bool enabled = false; |
| 6224 | EffectHandle *hdl = handle.unsafe_get(); |
| 6225 | if (hdl) { |
Steve Block | 71f2cf1 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 6226 | ALOGV("removeHandle() unsafe_get OK"); |
Eric Laurent | f82fccd | 2011-07-27 19:49:51 -0700 | [diff] [blame] | 6227 | enabled = hdl->enabled(); |
| 6228 | } |
Eric Laurent | 65b6545 | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 6229 | mHandles.removeAt(i); |
| 6230 | size = mHandles.size(); |
| 6231 | // if removed from first place, move effect control from this handle to next in line |
| 6232 | if (i == 0 && size != 0) { |
| 6233 | sp<EffectHandle> h = mHandles[0].promote(); |
| 6234 | if (h != 0) { |
Eric Laurent | f82fccd | 2011-07-27 19:49:51 -0700 | [diff] [blame] | 6235 | h->setControl(true /*hasControl*/, true /*signal*/ , enabled /*enabled*/); |
Eric Laurent | 65b6545 | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 6236 | } |
| 6237 | } |
| 6238 | |
Eric Laurent | 21b5c47 | 2011-07-26 20:54:46 -0700 | [diff] [blame] | 6239 | // Prevent calls to process() and other functions on effect interface from now on. |
| 6240 | // The effect engine will be released by the destructor when the last strong reference on |
| 6241 | // this object is released which can happen after next process is called. |
Marco Nelissen | c74b93f | 2011-08-02 13:33:41 -0700 | [diff] [blame] | 6242 | if (size == 0 && !mPinned) { |
Eric Laurent | 21b5c47 | 2011-07-26 20:54:46 -0700 | [diff] [blame] | 6243 | mState = DESTROYED; |
Eric Laurent | 4fd3ecc | 2010-09-28 14:09:57 -0700 | [diff] [blame] | 6244 | } |
| 6245 | |
Eric Laurent | 65b6545 | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 6246 | return size; |
| 6247 | } |
| 6248 | |
Eric Laurent | f82fccd | 2011-07-27 19:49:51 -0700 | [diff] [blame] | 6249 | sp<AudioFlinger::EffectHandle> AudioFlinger::EffectModule::controlHandle() |
| 6250 | { |
| 6251 | Mutex::Autolock _l(mLock); |
| 6252 | sp<EffectHandle> handle; |
| 6253 | if (mHandles.size() != 0) { |
| 6254 | handle = mHandles[0].promote(); |
| 6255 | } |
| 6256 | return handle; |
| 6257 | } |
| 6258 | |
Marco Nelissen | c74b93f | 2011-08-02 13:33:41 -0700 | [diff] [blame] | 6259 | void AudioFlinger::EffectModule::disconnect(const wp<EffectHandle>& handle, bool unpiniflast) |
Eric Laurent | 65b6545 | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 6260 | { |
Steve Block | 71f2cf1 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 6261 | ALOGV("disconnect() %p handle %p ", this, handle.unsafe_get()); |
Eric Laurent | 65b6545 | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 6262 | // keep a strong reference on this EffectModule to avoid calling the |
| 6263 | // destructor before we exit |
| 6264 | sp<EffectModule> keep(this); |
Eric Laurent | 53334cd | 2010-06-23 17:38:20 -0700 | [diff] [blame] | 6265 | { |
| 6266 | sp<ThreadBase> thread = mThread.promote(); |
| 6267 | if (thread != 0) { |
Marco Nelissen | c74b93f | 2011-08-02 13:33:41 -0700 | [diff] [blame] | 6268 | thread->disconnectEffect(keep, handle, unpiniflast); |
Eric Laurent | 65b6545 | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 6269 | } |
| 6270 | } |
| 6271 | } |
| 6272 | |
Eric Laurent | 7d850f2 | 2010-07-09 13:34:17 -0700 | [diff] [blame] | 6273 | void AudioFlinger::EffectModule::updateState() { |
| 6274 | Mutex::Autolock _l(mLock); |
| 6275 | |
| 6276 | switch (mState) { |
| 6277 | case RESTART: |
| 6278 | reset_l(); |
| 6279 | // FALL THROUGH |
| 6280 | |
| 6281 | case STARTING: |
| 6282 | // clear auxiliary effect input buffer for next accumulation |
| 6283 | if ((mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) { |
| 6284 | memset(mConfig.inputCfg.buffer.raw, |
| 6285 | 0, |
| 6286 | mConfig.inputCfg.buffer.frameCount*sizeof(int32_t)); |
| 6287 | } |
| 6288 | start_l(); |
| 6289 | mState = ACTIVE; |
| 6290 | break; |
| 6291 | case STOPPING: |
| 6292 | stop_l(); |
| 6293 | mDisableWaitCnt = mMaxDisableWaitCnt; |
| 6294 | mState = STOPPED; |
| 6295 | break; |
| 6296 | case STOPPED: |
| 6297 | // mDisableWaitCnt is forced to 1 by process() when the engine indicates the end of the |
| 6298 | // turn off sequence. |
| 6299 | if (--mDisableWaitCnt == 0) { |
| 6300 | reset_l(); |
| 6301 | mState = IDLE; |
| 6302 | } |
| 6303 | break; |
Eric Laurent | 21b5c47 | 2011-07-26 20:54:46 -0700 | [diff] [blame] | 6304 | default: //IDLE , ACTIVE, DESTROYED |
Eric Laurent | 7d850f2 | 2010-07-09 13:34:17 -0700 | [diff] [blame] | 6305 | break; |
| 6306 | } |
| 6307 | } |
| 6308 | |
Eric Laurent | 65b6545 | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 6309 | void AudioFlinger::EffectModule::process() |
| 6310 | { |
| 6311 | Mutex::Autolock _l(mLock); |
| 6312 | |
Eric Laurent | 21b5c47 | 2011-07-26 20:54:46 -0700 | [diff] [blame] | 6313 | if (mState == DESTROYED || mEffectInterface == NULL || |
Eric Laurent | 7d850f2 | 2010-07-09 13:34:17 -0700 | [diff] [blame] | 6314 | mConfig.inputCfg.buffer.raw == NULL || |
| 6315 | mConfig.outputCfg.buffer.raw == NULL) { |
Eric Laurent | 65b6545 | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 6316 | return; |
| 6317 | } |
| 6318 | |
Eric Laurent | a92ebfa | 2010-08-31 13:50:07 -0700 | [diff] [blame] | 6319 | if (isProcessEnabled()) { |
Eric Laurent | 65b6545 | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 6320 | // do 32 bit to 16 bit conversion for auxiliary effect input buffer |
| 6321 | if ((mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) { |
Glenn Kasten | 490909d | 2011-12-15 09:52:39 -0800 | [diff] [blame] | 6322 | ditherAndClamp(mConfig.inputCfg.buffer.s32, |
Eric Laurent | 65b6545 | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 6323 | mConfig.inputCfg.buffer.s32, |
Eric Laurent | 8ed6ed0 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 6324 | mConfig.inputCfg.buffer.frameCount/2); |
Eric Laurent | 65b6545 | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 6325 | } |
| 6326 | |
Eric Laurent | 65b6545 | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 6327 | // do the actual processing in the effect engine |
Eric Laurent | 7d850f2 | 2010-07-09 13:34:17 -0700 | [diff] [blame] | 6328 | int ret = (*mEffectInterface)->process(mEffectInterface, |
| 6329 | &mConfig.inputCfg.buffer, |
| 6330 | &mConfig.outputCfg.buffer); |
| 6331 | |
| 6332 | // force transition to IDLE state when engine is ready |
| 6333 | if (mState == STOPPED && ret == -ENODATA) { |
| 6334 | mDisableWaitCnt = 1; |
| 6335 | } |
Eric Laurent | 65b6545 | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 6336 | |
| 6337 | // clear auxiliary effect input buffer for next accumulation |
| 6338 | if ((mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) { |
Eric Laurent | 67b5ed3 | 2011-01-19 18:36:13 -0800 | [diff] [blame] | 6339 | memset(mConfig.inputCfg.buffer.raw, 0, |
| 6340 | mConfig.inputCfg.buffer.frameCount*sizeof(int32_t)); |
Eric Laurent | 65b6545 | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 6341 | } |
| 6342 | } else if ((mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_INSERT && |
Eric Laurent | 67b5ed3 | 2011-01-19 18:36:13 -0800 | [diff] [blame] | 6343 | mConfig.inputCfg.buffer.raw != mConfig.outputCfg.buffer.raw) { |
| 6344 | // If an insert effect is idle and input buffer is different from output buffer, |
| 6345 | // accumulate input onto output |
Eric Laurent | 65b6545 | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 6346 | sp<EffectChain> chain = mChain.promote(); |
Eric Laurent | 90681d6 | 2011-05-09 12:09:06 -0700 | [diff] [blame] | 6347 | if (chain != 0 && chain->activeTrackCnt() != 0) { |
Eric Laurent | 67b5ed3 | 2011-01-19 18:36:13 -0800 | [diff] [blame] | 6348 | size_t frameCnt = mConfig.inputCfg.buffer.frameCount * 2; //always stereo here |
| 6349 | int16_t *in = mConfig.inputCfg.buffer.s16; |
| 6350 | int16_t *out = mConfig.outputCfg.buffer.s16; |
| 6351 | for (size_t i = 0; i < frameCnt; i++) { |
| 6352 | out[i] = clamp16((int32_t)out[i] + (int32_t)in[i]); |
Eric Laurent | 65b6545 | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 6353 | } |
Eric Laurent | 65b6545 | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 6354 | } |
| 6355 | } |
| 6356 | } |
| 6357 | |
Eric Laurent | df9b81c | 2010-07-02 08:12:41 -0700 | [diff] [blame] | 6358 | void AudioFlinger::EffectModule::reset_l() |
Eric Laurent | 65b6545 | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 6359 | { |
| 6360 | if (mEffectInterface == NULL) { |
| 6361 | return; |
| 6362 | } |
| 6363 | (*mEffectInterface)->command(mEffectInterface, EFFECT_CMD_RESET, 0, NULL, 0, NULL); |
| 6364 | } |
| 6365 | |
| 6366 | status_t AudioFlinger::EffectModule::configure() |
| 6367 | { |
| 6368 | uint32_t channels; |
| 6369 | if (mEffectInterface == NULL) { |
| 6370 | return NO_INIT; |
| 6371 | } |
| 6372 | |
| 6373 | sp<ThreadBase> thread = mThread.promote(); |
| 6374 | if (thread == 0) { |
| 6375 | return DEAD_OBJECT; |
| 6376 | } |
| 6377 | |
| 6378 | // TODO: handle configuration of effects replacing track process |
| 6379 | if (thread->channelCount() == 1) { |
Eric Laurent | 0fb66c2 | 2011-05-17 19:16:02 -0700 | [diff] [blame] | 6380 | channels = AUDIO_CHANNEL_OUT_MONO; |
Eric Laurent | 65b6545 | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 6381 | } else { |
Eric Laurent | 0fb66c2 | 2011-05-17 19:16:02 -0700 | [diff] [blame] | 6382 | channels = AUDIO_CHANNEL_OUT_STEREO; |
Eric Laurent | 65b6545 | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 6383 | } |
| 6384 | |
| 6385 | if ((mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) { |
Eric Laurent | 0fb66c2 | 2011-05-17 19:16:02 -0700 | [diff] [blame] | 6386 | mConfig.inputCfg.channels = AUDIO_CHANNEL_OUT_MONO; |
Eric Laurent | 65b6545 | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 6387 | } else { |
| 6388 | mConfig.inputCfg.channels = channels; |
| 6389 | } |
| 6390 | mConfig.outputCfg.channels = channels; |
Eric Laurent | 0fb66c2 | 2011-05-17 19:16:02 -0700 | [diff] [blame] | 6391 | mConfig.inputCfg.format = AUDIO_FORMAT_PCM_16_BIT; |
| 6392 | mConfig.outputCfg.format = AUDIO_FORMAT_PCM_16_BIT; |
Eric Laurent | 65b6545 | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 6393 | mConfig.inputCfg.samplingRate = thread->sampleRate(); |
| 6394 | mConfig.outputCfg.samplingRate = mConfig.inputCfg.samplingRate; |
| 6395 | mConfig.inputCfg.bufferProvider.cookie = NULL; |
| 6396 | mConfig.inputCfg.bufferProvider.getBuffer = NULL; |
| 6397 | mConfig.inputCfg.bufferProvider.releaseBuffer = NULL; |
| 6398 | mConfig.outputCfg.bufferProvider.cookie = NULL; |
| 6399 | mConfig.outputCfg.bufferProvider.getBuffer = NULL; |
| 6400 | mConfig.outputCfg.bufferProvider.releaseBuffer = NULL; |
| 6401 | mConfig.inputCfg.accessMode = EFFECT_BUFFER_ACCESS_READ; |
| 6402 | // Insert effect: |
Dima Zavin | 24fc2fb | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 6403 | // - in session AUDIO_SESSION_OUTPUT_MIX or AUDIO_SESSION_OUTPUT_STAGE, |
Eric Laurent | 8ed6ed0 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 6404 | // always overwrites output buffer: input buffer == output buffer |
Eric Laurent | 65b6545 | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 6405 | // - in other sessions: |
| 6406 | // last effect in the chain accumulates in output buffer: input buffer != output buffer |
| 6407 | // other effect: overwrites output buffer: input buffer == output buffer |
| 6408 | // Auxiliary effect: |
| 6409 | // accumulates in output buffer: input buffer != output buffer |
| 6410 | // Therefore: accumulate <=> input buffer != output buffer |
| 6411 | if (mConfig.inputCfg.buffer.raw != mConfig.outputCfg.buffer.raw) { |
| 6412 | mConfig.outputCfg.accessMode = EFFECT_BUFFER_ACCESS_ACCUMULATE; |
| 6413 | } else { |
| 6414 | mConfig.outputCfg.accessMode = EFFECT_BUFFER_ACCESS_WRITE; |
| 6415 | } |
| 6416 | mConfig.inputCfg.mask = EFFECT_CONFIG_ALL; |
| 6417 | mConfig.outputCfg.mask = EFFECT_CONFIG_ALL; |
| 6418 | mConfig.inputCfg.buffer.frameCount = thread->frameCount(); |
| 6419 | mConfig.outputCfg.buffer.frameCount = mConfig.inputCfg.buffer.frameCount; |
| 6420 | |
Steve Block | 71f2cf1 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 6421 | ALOGV("configure() %p thread %p buffer %p framecount %d", |
Eric Laurent | 8ed6ed0 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 6422 | this, thread.get(), mConfig.inputCfg.buffer.raw, mConfig.inputCfg.buffer.frameCount); |
| 6423 | |
Eric Laurent | 65b6545 | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 6424 | status_t cmdStatus; |
Eric Laurent | a4c72ac | 2010-07-28 05:40:18 -0700 | [diff] [blame] | 6425 | uint32_t size = sizeof(int); |
| 6426 | status_t status = (*mEffectInterface)->command(mEffectInterface, |
Eric Laurent | 4abf882 | 2011-12-16 15:30:36 -0800 | [diff] [blame] | 6427 | EFFECT_CMD_SET_CONFIG, |
Eric Laurent | a4c72ac | 2010-07-28 05:40:18 -0700 | [diff] [blame] | 6428 | sizeof(effect_config_t), |
| 6429 | &mConfig, |
| 6430 | &size, |
| 6431 | &cmdStatus); |
Eric Laurent | 65b6545 | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 6432 | if (status == 0) { |
| 6433 | status = cmdStatus; |
| 6434 | } |
Eric Laurent | 7d850f2 | 2010-07-09 13:34:17 -0700 | [diff] [blame] | 6435 | |
| 6436 | mMaxDisableWaitCnt = (MAX_DISABLE_TIME_MS * mConfig.outputCfg.samplingRate) / |
| 6437 | (1000 * mConfig.outputCfg.buffer.frameCount); |
| 6438 | |
Eric Laurent | 65b6545 | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 6439 | return status; |
| 6440 | } |
| 6441 | |
| 6442 | status_t AudioFlinger::EffectModule::init() |
| 6443 | { |
Eric Laurent | df9b81c | 2010-07-02 08:12:41 -0700 | [diff] [blame] | 6444 | Mutex::Autolock _l(mLock); |
Eric Laurent | 65b6545 | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 6445 | if (mEffectInterface == NULL) { |
| 6446 | return NO_INIT; |
| 6447 | } |
| 6448 | status_t cmdStatus; |
Eric Laurent | a4c72ac | 2010-07-28 05:40:18 -0700 | [diff] [blame] | 6449 | uint32_t size = sizeof(status_t); |
| 6450 | status_t status = (*mEffectInterface)->command(mEffectInterface, |
| 6451 | EFFECT_CMD_INIT, |
| 6452 | 0, |
| 6453 | NULL, |
| 6454 | &size, |
| 6455 | &cmdStatus); |
Eric Laurent | 65b6545 | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 6456 | if (status == 0) { |
| 6457 | status = cmdStatus; |
| 6458 | } |
| 6459 | return status; |
| 6460 | } |
| 6461 | |
Eric Laurent | 6fccbd0 | 2011-10-05 17:42:25 -0700 | [diff] [blame] | 6462 | status_t AudioFlinger::EffectModule::start() |
| 6463 | { |
| 6464 | Mutex::Autolock _l(mLock); |
| 6465 | return start_l(); |
| 6466 | } |
| 6467 | |
Eric Laurent | df9b81c | 2010-07-02 08:12:41 -0700 | [diff] [blame] | 6468 | status_t AudioFlinger::EffectModule::start_l() |
Eric Laurent | 65b6545 | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 6469 | { |
| 6470 | if (mEffectInterface == NULL) { |
| 6471 | return NO_INIT; |
| 6472 | } |
| 6473 | status_t cmdStatus; |
Eric Laurent | a4c72ac | 2010-07-28 05:40:18 -0700 | [diff] [blame] | 6474 | uint32_t size = sizeof(status_t); |
| 6475 | status_t status = (*mEffectInterface)->command(mEffectInterface, |
| 6476 | EFFECT_CMD_ENABLE, |
| 6477 | 0, |
| 6478 | NULL, |
| 6479 | &size, |
| 6480 | &cmdStatus); |
Eric Laurent | 65b6545 | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 6481 | if (status == 0) { |
| 6482 | status = cmdStatus; |
| 6483 | } |
Eric Laurent | 464d5b3 | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 6484 | if (status == 0 && |
| 6485 | ((mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_PRE_PROC || |
| 6486 | (mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_POST_PROC)) { |
| 6487 | sp<ThreadBase> thread = mThread.promote(); |
| 6488 | if (thread != 0) { |
Eric Laurent | 828b977 | 2011-08-07 16:32:26 -0700 | [diff] [blame] | 6489 | audio_stream_t *stream = thread->stream(); |
| 6490 | if (stream != NULL) { |
| 6491 | stream->add_audio_effect(stream, mEffectInterface); |
| 6492 | } |
Eric Laurent | 464d5b3 | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 6493 | } |
| 6494 | } |
Eric Laurent | 65b6545 | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 6495 | return status; |
| 6496 | } |
| 6497 | |
Eric Laurent | 21b5c47 | 2011-07-26 20:54:46 -0700 | [diff] [blame] | 6498 | status_t AudioFlinger::EffectModule::stop() |
| 6499 | { |
| 6500 | Mutex::Autolock _l(mLock); |
| 6501 | return stop_l(); |
| 6502 | } |
| 6503 | |
Eric Laurent | df9b81c | 2010-07-02 08:12:41 -0700 | [diff] [blame] | 6504 | status_t AudioFlinger::EffectModule::stop_l() |
Eric Laurent | 65b6545 | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 6505 | { |
| 6506 | if (mEffectInterface == NULL) { |
| 6507 | return NO_INIT; |
| 6508 | } |
| 6509 | status_t cmdStatus; |
Eric Laurent | a4c72ac | 2010-07-28 05:40:18 -0700 | [diff] [blame] | 6510 | uint32_t size = sizeof(status_t); |
| 6511 | status_t status = (*mEffectInterface)->command(mEffectInterface, |
| 6512 | EFFECT_CMD_DISABLE, |
| 6513 | 0, |
| 6514 | NULL, |
| 6515 | &size, |
| 6516 | &cmdStatus); |
Eric Laurent | 65b6545 | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 6517 | if (status == 0) { |
| 6518 | status = cmdStatus; |
| 6519 | } |
Eric Laurent | 464d5b3 | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 6520 | if (status == 0 && |
| 6521 | ((mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_PRE_PROC || |
| 6522 | (mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_POST_PROC)) { |
| 6523 | sp<ThreadBase> thread = mThread.promote(); |
| 6524 | if (thread != 0) { |
Eric Laurent | 828b977 | 2011-08-07 16:32:26 -0700 | [diff] [blame] | 6525 | audio_stream_t *stream = thread->stream(); |
| 6526 | if (stream != NULL) { |
| 6527 | stream->remove_audio_effect(stream, mEffectInterface); |
| 6528 | } |
Eric Laurent | 464d5b3 | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 6529 | } |
| 6530 | } |
Eric Laurent | 65b6545 | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 6531 | return status; |
| 6532 | } |
| 6533 | |
Eric Laurent | a4c72ac | 2010-07-28 05:40:18 -0700 | [diff] [blame] | 6534 | status_t AudioFlinger::EffectModule::command(uint32_t cmdCode, |
| 6535 | uint32_t cmdSize, |
| 6536 | void *pCmdData, |
| 6537 | uint32_t *replySize, |
| 6538 | void *pReplyData) |
Eric Laurent | 65b6545 | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 6539 | { |
Eric Laurent | df9b81c | 2010-07-02 08:12:41 -0700 | [diff] [blame] | 6540 | Mutex::Autolock _l(mLock); |
Steve Block | 71f2cf1 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 6541 | // ALOGV("command(), cmdCode: %d, mEffectInterface: %p", cmdCode, mEffectInterface); |
Eric Laurent | 65b6545 | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 6542 | |
Eric Laurent | 21b5c47 | 2011-07-26 20:54:46 -0700 | [diff] [blame] | 6543 | if (mState == DESTROYED || mEffectInterface == NULL) { |
Eric Laurent | 65b6545 | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 6544 | return NO_INIT; |
| 6545 | } |
Eric Laurent | a4c72ac | 2010-07-28 05:40:18 -0700 | [diff] [blame] | 6546 | status_t status = (*mEffectInterface)->command(mEffectInterface, |
| 6547 | cmdCode, |
| 6548 | cmdSize, |
| 6549 | pCmdData, |
| 6550 | replySize, |
| 6551 | pReplyData); |
Eric Laurent | 65b6545 | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 6552 | if (cmdCode != EFFECT_CMD_GET_PARAM && status == NO_ERROR) { |
Eric Laurent | a4c72ac | 2010-07-28 05:40:18 -0700 | [diff] [blame] | 6553 | uint32_t size = (replySize == NULL) ? 0 : *replySize; |
Eric Laurent | 65b6545 | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 6554 | for (size_t i = 1; i < mHandles.size(); i++) { |
| 6555 | sp<EffectHandle> h = mHandles[i].promote(); |
| 6556 | if (h != 0) { |
| 6557 | h->commandExecuted(cmdCode, cmdSize, pCmdData, size, pReplyData); |
| 6558 | } |
| 6559 | } |
| 6560 | } |
| 6561 | return status; |
| 6562 | } |
| 6563 | |
| 6564 | status_t AudioFlinger::EffectModule::setEnabled(bool enabled) |
| 6565 | { |
Eric Laurent | 6752ec8 | 2011-08-10 10:37:50 -0700 | [diff] [blame] | 6566 | |
Eric Laurent | 65b6545 | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 6567 | Mutex::Autolock _l(mLock); |
Steve Block | 71f2cf1 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 6568 | ALOGV("setEnabled %p enabled %d", this, enabled); |
Eric Laurent | 65b6545 | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 6569 | |
| 6570 | if (enabled != isEnabled()) { |
Eric Laurent | 6752ec8 | 2011-08-10 10:37:50 -0700 | [diff] [blame] | 6571 | status_t status = AudioSystem::setEffectEnabled(mId, enabled); |
| 6572 | if (enabled && status != NO_ERROR) { |
| 6573 | return status; |
| 6574 | } |
| 6575 | |
Eric Laurent | 65b6545 | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 6576 | switch (mState) { |
| 6577 | // going from disabled to enabled |
| 6578 | case IDLE: |
Eric Laurent | 7d850f2 | 2010-07-09 13:34:17 -0700 | [diff] [blame] | 6579 | mState = STARTING; |
| 6580 | break; |
| 6581 | case STOPPED: |
| 6582 | mState = RESTART; |
Eric Laurent | 65b6545 | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 6583 | break; |
| 6584 | case STOPPING: |
| 6585 | mState = ACTIVE; |
| 6586 | break; |
Eric Laurent | 65b6545 | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 6587 | |
| 6588 | // going from enabled to disabled |
Eric Laurent | 7d850f2 | 2010-07-09 13:34:17 -0700 | [diff] [blame] | 6589 | case RESTART: |
Eric Laurent | a92ebfa | 2010-08-31 13:50:07 -0700 | [diff] [blame] | 6590 | mState = STOPPED; |
| 6591 | break; |
Eric Laurent | 65b6545 | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 6592 | case STARTING: |
Eric Laurent | 7d850f2 | 2010-07-09 13:34:17 -0700 | [diff] [blame] | 6593 | mState = IDLE; |
Eric Laurent | 65b6545 | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 6594 | break; |
| 6595 | case ACTIVE: |
| 6596 | mState = STOPPING; |
| 6597 | break; |
Eric Laurent | 21b5c47 | 2011-07-26 20:54:46 -0700 | [diff] [blame] | 6598 | case DESTROYED: |
| 6599 | return NO_ERROR; // simply ignore as we are being destroyed |
Eric Laurent | 65b6545 | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 6600 | } |
| 6601 | for (size_t i = 1; i < mHandles.size(); i++) { |
| 6602 | sp<EffectHandle> h = mHandles[i].promote(); |
| 6603 | if (h != 0) { |
| 6604 | h->setEnabled(enabled); |
| 6605 | } |
| 6606 | } |
| 6607 | } |
| 6608 | return NO_ERROR; |
| 6609 | } |
| 6610 | |
| 6611 | bool AudioFlinger::EffectModule::isEnabled() |
| 6612 | { |
| 6613 | switch (mState) { |
Eric Laurent | 7d850f2 | 2010-07-09 13:34:17 -0700 | [diff] [blame] | 6614 | case RESTART: |
Eric Laurent | 65b6545 | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 6615 | case STARTING: |
| 6616 | case ACTIVE: |
| 6617 | return true; |
| 6618 | case IDLE: |
| 6619 | case STOPPING: |
| 6620 | case STOPPED: |
Eric Laurent | 21b5c47 | 2011-07-26 20:54:46 -0700 | [diff] [blame] | 6621 | case DESTROYED: |
Eric Laurent | 65b6545 | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 6622 | default: |
| 6623 | return false; |
| 6624 | } |
| 6625 | } |
| 6626 | |
Eric Laurent | a92ebfa | 2010-08-31 13:50:07 -0700 | [diff] [blame] | 6627 | bool AudioFlinger::EffectModule::isProcessEnabled() |
| 6628 | { |
| 6629 | switch (mState) { |
| 6630 | case RESTART: |
| 6631 | case ACTIVE: |
| 6632 | case STOPPING: |
| 6633 | case STOPPED: |
| 6634 | return true; |
| 6635 | case IDLE: |
| 6636 | case STARTING: |
Eric Laurent | 21b5c47 | 2011-07-26 20:54:46 -0700 | [diff] [blame] | 6637 | case DESTROYED: |
Eric Laurent | a92ebfa | 2010-08-31 13:50:07 -0700 | [diff] [blame] | 6638 | default: |
| 6639 | return false; |
| 6640 | } |
| 6641 | } |
| 6642 | |
Eric Laurent | 65b6545 | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 6643 | status_t AudioFlinger::EffectModule::setVolume(uint32_t *left, uint32_t *right, bool controller) |
| 6644 | { |
Eric Laurent | df9b81c | 2010-07-02 08:12:41 -0700 | [diff] [blame] | 6645 | Mutex::Autolock _l(mLock); |
Eric Laurent | 65b6545 | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 6646 | status_t status = NO_ERROR; |
| 6647 | |
| 6648 | // Send volume indication if EFFECT_FLAG_VOLUME_IND is set and read back altered volume |
| 6649 | // if controller flag is set (Note that controller == TRUE => EFFECT_FLAG_VOLUME_CTRL set) |
Eric Laurent | a92ebfa | 2010-08-31 13:50:07 -0700 | [diff] [blame] | 6650 | if (isProcessEnabled() && |
Eric Laurent | 0d7e048 | 2010-07-19 06:24:46 -0700 | [diff] [blame] | 6651 | ((mDescriptor.flags & EFFECT_FLAG_VOLUME_MASK) == EFFECT_FLAG_VOLUME_CTRL || |
| 6652 | (mDescriptor.flags & EFFECT_FLAG_VOLUME_MASK) == EFFECT_FLAG_VOLUME_IND)) { |
Eric Laurent | 65b6545 | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 6653 | status_t cmdStatus; |
| 6654 | uint32_t volume[2]; |
| 6655 | uint32_t *pVolume = NULL; |
Eric Laurent | a4c72ac | 2010-07-28 05:40:18 -0700 | [diff] [blame] | 6656 | uint32_t size = sizeof(volume); |
Eric Laurent | 65b6545 | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 6657 | volume[0] = *left; |
| 6658 | volume[1] = *right; |
| 6659 | if (controller) { |
| 6660 | pVolume = volume; |
| 6661 | } |
Eric Laurent | a4c72ac | 2010-07-28 05:40:18 -0700 | [diff] [blame] | 6662 | status = (*mEffectInterface)->command(mEffectInterface, |
| 6663 | EFFECT_CMD_SET_VOLUME, |
| 6664 | size, |
| 6665 | volume, |
| 6666 | &size, |
| 6667 | pVolume); |
Eric Laurent | 65b6545 | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 6668 | if (controller && status == NO_ERROR && size == sizeof(volume)) { |
| 6669 | *left = volume[0]; |
| 6670 | *right = volume[1]; |
| 6671 | } |
| 6672 | } |
| 6673 | return status; |
| 6674 | } |
| 6675 | |
| 6676 | status_t AudioFlinger::EffectModule::setDevice(uint32_t device) |
| 6677 | { |
Eric Laurent | df9b81c | 2010-07-02 08:12:41 -0700 | [diff] [blame] | 6678 | Mutex::Autolock _l(mLock); |
Eric Laurent | 65b6545 | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 6679 | status_t status = NO_ERROR; |
Eric Laurent | 464d5b3 | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 6680 | if (device && (mDescriptor.flags & EFFECT_FLAG_DEVICE_MASK) == EFFECT_FLAG_DEVICE_IND) { |
| 6681 | // audio pre processing modules on RecordThread can receive both output and |
| 6682 | // input device indication in the same call |
| 6683 | uint32_t dev = device & AUDIO_DEVICE_OUT_ALL; |
| 6684 | if (dev) { |
| 6685 | status_t cmdStatus; |
| 6686 | uint32_t size = sizeof(status_t); |
| 6687 | |
| 6688 | status = (*mEffectInterface)->command(mEffectInterface, |
| 6689 | EFFECT_CMD_SET_DEVICE, |
| 6690 | sizeof(uint32_t), |
| 6691 | &dev, |
| 6692 | &size, |
| 6693 | &cmdStatus); |
| 6694 | if (status == NO_ERROR) { |
| 6695 | status = cmdStatus; |
| 6696 | } |
| 6697 | } |
| 6698 | dev = device & AUDIO_DEVICE_IN_ALL; |
| 6699 | if (dev) { |
| 6700 | status_t cmdStatus; |
| 6701 | uint32_t size = sizeof(status_t); |
| 6702 | |
| 6703 | status_t status2 = (*mEffectInterface)->command(mEffectInterface, |
| 6704 | EFFECT_CMD_SET_INPUT_DEVICE, |
| 6705 | sizeof(uint32_t), |
| 6706 | &dev, |
| 6707 | &size, |
| 6708 | &cmdStatus); |
| 6709 | if (status2 == NO_ERROR) { |
| 6710 | status2 = cmdStatus; |
| 6711 | } |
| 6712 | if (status == NO_ERROR) { |
| 6713 | status = status2; |
| 6714 | } |
Eric Laurent | 65b6545 | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 6715 | } |
| 6716 | } |
| 6717 | return status; |
| 6718 | } |
| 6719 | |
Glenn Kasten | accb114 | 2012-01-04 11:00:47 -0800 | [diff] [blame] | 6720 | status_t AudioFlinger::EffectModule::setMode(audio_mode_t mode) |
Eric Laurent | 53334cd | 2010-06-23 17:38:20 -0700 | [diff] [blame] | 6721 | { |
Eric Laurent | df9b81c | 2010-07-02 08:12:41 -0700 | [diff] [blame] | 6722 | Mutex::Autolock _l(mLock); |
Eric Laurent | 53334cd | 2010-06-23 17:38:20 -0700 | [diff] [blame] | 6723 | status_t status = NO_ERROR; |
| 6724 | if ((mDescriptor.flags & EFFECT_FLAG_AUDIO_MODE_MASK) == EFFECT_FLAG_AUDIO_MODE_IND) { |
Eric Laurent | 53334cd | 2010-06-23 17:38:20 -0700 | [diff] [blame] | 6725 | status_t cmdStatus; |
Eric Laurent | a4c72ac | 2010-07-28 05:40:18 -0700 | [diff] [blame] | 6726 | uint32_t size = sizeof(status_t); |
| 6727 | status = (*mEffectInterface)->command(mEffectInterface, |
| 6728 | EFFECT_CMD_SET_AUDIO_MODE, |
Glenn Kasten | accb114 | 2012-01-04 11:00:47 -0800 | [diff] [blame] | 6729 | sizeof(audio_mode_t), |
Eric Laurent | 0fb66c2 | 2011-05-17 19:16:02 -0700 | [diff] [blame] | 6730 | &mode, |
Eric Laurent | a4c72ac | 2010-07-28 05:40:18 -0700 | [diff] [blame] | 6731 | &size, |
| 6732 | &cmdStatus); |
Eric Laurent | 53334cd | 2010-06-23 17:38:20 -0700 | [diff] [blame] | 6733 | if (status == NO_ERROR) { |
| 6734 | status = cmdStatus; |
| 6735 | } |
| 6736 | } |
| 6737 | return status; |
| 6738 | } |
| 6739 | |
Eric Laurent | f82fccd | 2011-07-27 19:49:51 -0700 | [diff] [blame] | 6740 | void AudioFlinger::EffectModule::setSuspended(bool suspended) |
| 6741 | { |
| 6742 | Mutex::Autolock _l(mLock); |
| 6743 | mSuspended = suspended; |
| 6744 | } |
Glenn Kasten | 1dce841 | 2012-01-04 11:01:11 -0800 | [diff] [blame] | 6745 | |
| 6746 | bool AudioFlinger::EffectModule::suspended() const |
Eric Laurent | f82fccd | 2011-07-27 19:49:51 -0700 | [diff] [blame] | 6747 | { |
| 6748 | Mutex::Autolock _l(mLock); |
| 6749 | return mSuspended; |
| 6750 | } |
| 6751 | |
Eric Laurent | 65b6545 | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 6752 | status_t AudioFlinger::EffectModule::dump(int fd, const Vector<String16>& args) |
| 6753 | { |
| 6754 | const size_t SIZE = 256; |
| 6755 | char buffer[SIZE]; |
| 6756 | String8 result; |
| 6757 | |
| 6758 | snprintf(buffer, SIZE, "\tEffect ID %d:\n", mId); |
| 6759 | result.append(buffer); |
| 6760 | |
| 6761 | bool locked = tryLock(mLock); |
| 6762 | // failed to lock - AudioFlinger is probably deadlocked |
| 6763 | if (!locked) { |
| 6764 | result.append("\t\tCould not lock Fx mutex:\n"); |
| 6765 | } |
| 6766 | |
| 6767 | result.append("\t\tSession Status State Engine:\n"); |
| 6768 | snprintf(buffer, SIZE, "\t\t%05d %03d %03d 0x%08x\n", |
| 6769 | mSessionId, mStatus, mState, (uint32_t)mEffectInterface); |
| 6770 | result.append(buffer); |
| 6771 | |
| 6772 | result.append("\t\tDescriptor:\n"); |
| 6773 | snprintf(buffer, SIZE, "\t\t- UUID: %08X-%04X-%04X-%04X-%02X%02X%02X%02X%02X%02X\n", |
| 6774 | mDescriptor.uuid.timeLow, mDescriptor.uuid.timeMid, mDescriptor.uuid.timeHiAndVersion, |
| 6775 | mDescriptor.uuid.clockSeq, mDescriptor.uuid.node[0], mDescriptor.uuid.node[1],mDescriptor.uuid.node[2], |
| 6776 | mDescriptor.uuid.node[3],mDescriptor.uuid.node[4],mDescriptor.uuid.node[5]); |
| 6777 | result.append(buffer); |
| 6778 | snprintf(buffer, SIZE, "\t\t- TYPE: %08X-%04X-%04X-%04X-%02X%02X%02X%02X%02X%02X\n", |
| 6779 | mDescriptor.type.timeLow, mDescriptor.type.timeMid, mDescriptor.type.timeHiAndVersion, |
| 6780 | mDescriptor.type.clockSeq, mDescriptor.type.node[0], mDescriptor.type.node[1],mDescriptor.type.node[2], |
| 6781 | mDescriptor.type.node[3],mDescriptor.type.node[4],mDescriptor.type.node[5]); |
| 6782 | result.append(buffer); |
Eric Laurent | 0fb66c2 | 2011-05-17 19:16:02 -0700 | [diff] [blame] | 6783 | snprintf(buffer, SIZE, "\t\t- apiVersion: %08X\n\t\t- flags: %08X\n", |
Eric Laurent | 65b6545 | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 6784 | mDescriptor.apiVersion, |
| 6785 | mDescriptor.flags); |
| 6786 | result.append(buffer); |
| 6787 | snprintf(buffer, SIZE, "\t\t- name: %s\n", |
| 6788 | mDescriptor.name); |
| 6789 | result.append(buffer); |
| 6790 | snprintf(buffer, SIZE, "\t\t- implementor: %s\n", |
| 6791 | mDescriptor.implementor); |
| 6792 | result.append(buffer); |
| 6793 | |
| 6794 | result.append("\t\t- Input configuration:\n"); |
| 6795 | result.append("\t\t\tBuffer Frames Smp rate Channels Format\n"); |
| 6796 | snprintf(buffer, SIZE, "\t\t\t0x%08x %05d %05d %08x %d\n", |
| 6797 | (uint32_t)mConfig.inputCfg.buffer.raw, |
| 6798 | mConfig.inputCfg.buffer.frameCount, |
| 6799 | mConfig.inputCfg.samplingRate, |
| 6800 | mConfig.inputCfg.channels, |
| 6801 | mConfig.inputCfg.format); |
| 6802 | result.append(buffer); |
| 6803 | |
| 6804 | result.append("\t\t- Output configuration:\n"); |
| 6805 | result.append("\t\t\tBuffer Frames Smp rate Channels Format\n"); |
| 6806 | snprintf(buffer, SIZE, "\t\t\t0x%08x %05d %05d %08x %d\n", |
| 6807 | (uint32_t)mConfig.outputCfg.buffer.raw, |
| 6808 | mConfig.outputCfg.buffer.frameCount, |
| 6809 | mConfig.outputCfg.samplingRate, |
| 6810 | mConfig.outputCfg.channels, |
| 6811 | mConfig.outputCfg.format); |
| 6812 | result.append(buffer); |
| 6813 | |
| 6814 | snprintf(buffer, SIZE, "\t\t%d Clients:\n", mHandles.size()); |
| 6815 | result.append(buffer); |
| 6816 | result.append("\t\t\tPid Priority Ctrl Locked client server\n"); |
| 6817 | for (size_t i = 0; i < mHandles.size(); ++i) { |
| 6818 | sp<EffectHandle> handle = mHandles[i].promote(); |
| 6819 | if (handle != 0) { |
| 6820 | handle->dump(buffer, SIZE); |
| 6821 | result.append(buffer); |
| 6822 | } |
| 6823 | } |
| 6824 | |
| 6825 | result.append("\n"); |
| 6826 | |
| 6827 | write(fd, result.string(), result.length()); |
| 6828 | |
| 6829 | if (locked) { |
| 6830 | mLock.unlock(); |
| 6831 | } |
| 6832 | |
| 6833 | return NO_ERROR; |
| 6834 | } |
| 6835 | |
| 6836 | // ---------------------------------------------------------------------------- |
| 6837 | // EffectHandle implementation |
| 6838 | // ---------------------------------------------------------------------------- |
| 6839 | |
| 6840 | #undef LOG_TAG |
| 6841 | #define LOG_TAG "AudioFlinger::EffectHandle" |
| 6842 | |
| 6843 | AudioFlinger::EffectHandle::EffectHandle(const sp<EffectModule>& effect, |
| 6844 | const sp<AudioFlinger::Client>& client, |
| 6845 | const sp<IEffectClient>& effectClient, |
| 6846 | int32_t priority) |
| 6847 | : BnEffect(), |
Marco Nelissen | c74b93f | 2011-08-02 13:33:41 -0700 | [diff] [blame] | 6848 | mEffect(effect), mEffectClient(effectClient), mClient(client), mCblk(NULL), |
Eric Laurent | f82fccd | 2011-07-27 19:49:51 -0700 | [diff] [blame] | 6849 | mPriority(priority), mHasControl(false), mEnabled(false) |
Eric Laurent | 65b6545 | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 6850 | { |
Steve Block | 71f2cf1 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 6851 | ALOGV("constructor %p", this); |
Eric Laurent | 65b6545 | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 6852 | |
Marco Nelissen | c74b93f | 2011-08-02 13:33:41 -0700 | [diff] [blame] | 6853 | if (client == 0) { |
| 6854 | return; |
| 6855 | } |
Eric Laurent | 65b6545 | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 6856 | int bufOffset = ((sizeof(effect_param_cblk_t) - 1) / sizeof(int) + 1) * sizeof(int); |
| 6857 | mCblkMemory = client->heap()->allocate(EFFECT_PARAM_BUFFER_SIZE + bufOffset); |
| 6858 | if (mCblkMemory != 0) { |
| 6859 | mCblk = static_cast<effect_param_cblk_t *>(mCblkMemory->pointer()); |
| 6860 | |
| 6861 | if (mCblk) { |
| 6862 | new(mCblk) effect_param_cblk_t(); |
| 6863 | mBuffer = (uint8_t *)mCblk + bufOffset; |
| 6864 | } |
| 6865 | } else { |
Steve Block | 3762c31 | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 6866 | ALOGE("not enough memory for Effect size=%u", EFFECT_PARAM_BUFFER_SIZE + sizeof(effect_param_cblk_t)); |
Eric Laurent | 65b6545 | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 6867 | return; |
| 6868 | } |
| 6869 | } |
| 6870 | |
| 6871 | AudioFlinger::EffectHandle::~EffectHandle() |
| 6872 | { |
Steve Block | 71f2cf1 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 6873 | ALOGV("Destructor %p", this); |
Marco Nelissen | c74b93f | 2011-08-02 13:33:41 -0700 | [diff] [blame] | 6874 | disconnect(false); |
Steve Block | 71f2cf1 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 6875 | ALOGV("Destructor DONE %p", this); |
Eric Laurent | 65b6545 | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 6876 | } |
| 6877 | |
| 6878 | status_t AudioFlinger::EffectHandle::enable() |
| 6879 | { |
Steve Block | 71f2cf1 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 6880 | ALOGV("enable %p", this); |
Eric Laurent | 65b6545 | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 6881 | if (!mHasControl) return INVALID_OPERATION; |
| 6882 | if (mEffect == 0) return DEAD_OBJECT; |
| 6883 | |
Eric Laurent | 6752ec8 | 2011-08-10 10:37:50 -0700 | [diff] [blame] | 6884 | if (mEnabled) { |
| 6885 | return NO_ERROR; |
| 6886 | } |
| 6887 | |
Eric Laurent | f82fccd | 2011-07-27 19:49:51 -0700 | [diff] [blame] | 6888 | mEnabled = true; |
| 6889 | |
| 6890 | sp<ThreadBase> thread = mEffect->thread().promote(); |
| 6891 | if (thread != 0) { |
| 6892 | thread->checkSuspendOnEffectEnabled(mEffect, true, mEffect->sessionId()); |
| 6893 | } |
| 6894 | |
| 6895 | // checkSuspendOnEffectEnabled() can suspend this same effect when enabled |
| 6896 | if (mEffect->suspended()) { |
| 6897 | return NO_ERROR; |
| 6898 | } |
| 6899 | |
Eric Laurent | 6752ec8 | 2011-08-10 10:37:50 -0700 | [diff] [blame] | 6900 | status_t status = mEffect->setEnabled(true); |
| 6901 | if (status != NO_ERROR) { |
| 6902 | if (thread != 0) { |
| 6903 | thread->checkSuspendOnEffectEnabled(mEffect, false, mEffect->sessionId()); |
| 6904 | } |
| 6905 | mEnabled = false; |
| 6906 | } |
| 6907 | return status; |
Eric Laurent | 65b6545 | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 6908 | } |
| 6909 | |
| 6910 | status_t AudioFlinger::EffectHandle::disable() |
| 6911 | { |
Steve Block | 71f2cf1 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 6912 | ALOGV("disable %p", this); |
Eric Laurent | 65b6545 | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 6913 | if (!mHasControl) return INVALID_OPERATION; |
Eric Laurent | f82fccd | 2011-07-27 19:49:51 -0700 | [diff] [blame] | 6914 | if (mEffect == 0) return DEAD_OBJECT; |
Eric Laurent | 65b6545 | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 6915 | |
Eric Laurent | 6752ec8 | 2011-08-10 10:37:50 -0700 | [diff] [blame] | 6916 | if (!mEnabled) { |
| 6917 | return NO_ERROR; |
| 6918 | } |
Eric Laurent | f82fccd | 2011-07-27 19:49:51 -0700 | [diff] [blame] | 6919 | mEnabled = false; |
| 6920 | |
| 6921 | if (mEffect->suspended()) { |
| 6922 | return NO_ERROR; |
| 6923 | } |
| 6924 | |
| 6925 | status_t status = mEffect->setEnabled(false); |
| 6926 | |
| 6927 | sp<ThreadBase> thread = mEffect->thread().promote(); |
| 6928 | if (thread != 0) { |
| 6929 | thread->checkSuspendOnEffectEnabled(mEffect, false, mEffect->sessionId()); |
| 6930 | } |
| 6931 | |
| 6932 | return status; |
Eric Laurent | 65b6545 | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 6933 | } |
| 6934 | |
| 6935 | void AudioFlinger::EffectHandle::disconnect() |
| 6936 | { |
Marco Nelissen | c74b93f | 2011-08-02 13:33:41 -0700 | [diff] [blame] | 6937 | disconnect(true); |
| 6938 | } |
| 6939 | |
| 6940 | void AudioFlinger::EffectHandle::disconnect(bool unpiniflast) |
| 6941 | { |
Steve Block | 71f2cf1 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 6942 | ALOGV("disconnect(%s)", unpiniflast ? "true" : "false"); |
Eric Laurent | 65b6545 | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 6943 | if (mEffect == 0) { |
| 6944 | return; |
| 6945 | } |
Marco Nelissen | c74b93f | 2011-08-02 13:33:41 -0700 | [diff] [blame] | 6946 | mEffect->disconnect(this, unpiniflast); |
Eric Laurent | f82fccd | 2011-07-27 19:49:51 -0700 | [diff] [blame] | 6947 | |
Eric Laurent | 7fa1cee | 2011-10-19 11:44:54 -0700 | [diff] [blame] | 6948 | if (mHasControl && mEnabled) { |
Eric Laurent | 6752ec8 | 2011-08-10 10:37:50 -0700 | [diff] [blame] | 6949 | sp<ThreadBase> thread = mEffect->thread().promote(); |
| 6950 | if (thread != 0) { |
| 6951 | thread->checkSuspendOnEffectEnabled(mEffect, false, mEffect->sessionId()); |
| 6952 | } |
Eric Laurent | f82fccd | 2011-07-27 19:49:51 -0700 | [diff] [blame] | 6953 | } |
| 6954 | |
Eric Laurent | 65b6545 | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 6955 | // release sp on module => module destructor can be called now |
| 6956 | mEffect.clear(); |
Eric Laurent | 65b6545 | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 6957 | if (mClient != 0) { |
Marco Nelissen | c74b93f | 2011-08-02 13:33:41 -0700 | [diff] [blame] | 6958 | if (mCblk) { |
| 6959 | mCblk->~effect_param_cblk_t(); // destroy our shared-structure. |
| 6960 | } |
| 6961 | mCblkMemory.clear(); // and free the shared memory |
Eric Laurent | 65b6545 | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 6962 | Mutex::Autolock _l(mClient->audioFlinger()->mLock); |
| 6963 | mClient.clear(); |
| 6964 | } |
| 6965 | } |
| 6966 | |
Eric Laurent | a4c72ac | 2010-07-28 05:40:18 -0700 | [diff] [blame] | 6967 | status_t AudioFlinger::EffectHandle::command(uint32_t cmdCode, |
| 6968 | uint32_t cmdSize, |
| 6969 | void *pCmdData, |
| 6970 | uint32_t *replySize, |
| 6971 | void *pReplyData) |
Eric Laurent | 65b6545 | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 6972 | { |
Steve Block | 71f2cf1 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 6973 | // ALOGV("command(), cmdCode: %d, mHasControl: %d, mEffect: %p", |
Eric Laurent | a4c72ac | 2010-07-28 05:40:18 -0700 | [diff] [blame] | 6974 | // cmdCode, mHasControl, (mEffect == 0) ? 0 : mEffect.get()); |
Eric Laurent | 65b6545 | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 6975 | |
| 6976 | // only get parameter command is permitted for applications not controlling the effect |
| 6977 | if (!mHasControl && cmdCode != EFFECT_CMD_GET_PARAM) { |
| 6978 | return INVALID_OPERATION; |
| 6979 | } |
| 6980 | if (mEffect == 0) return DEAD_OBJECT; |
Marco Nelissen | c74b93f | 2011-08-02 13:33:41 -0700 | [diff] [blame] | 6981 | if (mClient == 0) return INVALID_OPERATION; |
Eric Laurent | 65b6545 | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 6982 | |
| 6983 | // handle commands that are not forwarded transparently to effect engine |
| 6984 | if (cmdCode == EFFECT_CMD_SET_PARAM_COMMIT) { |
| 6985 | // No need to trylock() here as this function is executed in the binder thread serving a particular client process: |
| 6986 | // no risk to block the whole media server process or mixer threads is we are stuck here |
| 6987 | Mutex::Autolock _l(mCblk->lock); |
| 6988 | if (mCblk->clientIndex > EFFECT_PARAM_BUFFER_SIZE || |
| 6989 | mCblk->serverIndex > EFFECT_PARAM_BUFFER_SIZE) { |
| 6990 | mCblk->serverIndex = 0; |
| 6991 | mCblk->clientIndex = 0; |
| 6992 | return BAD_VALUE; |
| 6993 | } |
| 6994 | status_t status = NO_ERROR; |
| 6995 | while (mCblk->serverIndex < mCblk->clientIndex) { |
| 6996 | int reply; |
Eric Laurent | a4c72ac | 2010-07-28 05:40:18 -0700 | [diff] [blame] | 6997 | uint32_t rsize = sizeof(int); |
Eric Laurent | 65b6545 | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 6998 | int *p = (int *)(mBuffer + mCblk->serverIndex); |
| 6999 | int size = *p++; |
Eric Laurent | 53334cd | 2010-06-23 17:38:20 -0700 | [diff] [blame] | 7000 | if (((uint8_t *)p + size) > mBuffer + mCblk->clientIndex) { |
Steve Block | 8564c8d | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 7001 | ALOGW("command(): invalid parameter block size"); |
Eric Laurent | 53334cd | 2010-06-23 17:38:20 -0700 | [diff] [blame] | 7002 | break; |
| 7003 | } |
Eric Laurent | 65b6545 | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 7004 | effect_param_t *param = (effect_param_t *)p; |
Eric Laurent | 53334cd | 2010-06-23 17:38:20 -0700 | [diff] [blame] | 7005 | if (param->psize == 0 || param->vsize == 0) { |
Steve Block | 8564c8d | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 7006 | ALOGW("command(): null parameter or value size"); |
Eric Laurent | 53334cd | 2010-06-23 17:38:20 -0700 | [diff] [blame] | 7007 | mCblk->serverIndex += size; |
| 7008 | continue; |
| 7009 | } |
Eric Laurent | a4c72ac | 2010-07-28 05:40:18 -0700 | [diff] [blame] | 7010 | uint32_t psize = sizeof(effect_param_t) + |
| 7011 | ((param->psize - 1) / sizeof(int) + 1) * sizeof(int) + |
| 7012 | param->vsize; |
| 7013 | status_t ret = mEffect->command(EFFECT_CMD_SET_PARAM, |
| 7014 | psize, |
| 7015 | p, |
| 7016 | &rsize, |
| 7017 | &reply); |
Eric Laurent | e65280c | 2010-09-02 11:56:55 -0700 | [diff] [blame] | 7018 | // stop at first error encountered |
| 7019 | if (ret != NO_ERROR) { |
Eric Laurent | 65b6545 | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 7020 | status = ret; |
Eric Laurent | e65280c | 2010-09-02 11:56:55 -0700 | [diff] [blame] | 7021 | *(int *)pReplyData = reply; |
| 7022 | break; |
| 7023 | } else if (reply != NO_ERROR) { |
| 7024 | *(int *)pReplyData = reply; |
| 7025 | break; |
Eric Laurent | 65b6545 | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 7026 | } |
| 7027 | mCblk->serverIndex += size; |
| 7028 | } |
| 7029 | mCblk->serverIndex = 0; |
| 7030 | mCblk->clientIndex = 0; |
| 7031 | return status; |
| 7032 | } else if (cmdCode == EFFECT_CMD_ENABLE) { |
Eric Laurent | e65280c | 2010-09-02 11:56:55 -0700 | [diff] [blame] | 7033 | *(int *)pReplyData = NO_ERROR; |
Eric Laurent | 65b6545 | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 7034 | return enable(); |
| 7035 | } else if (cmdCode == EFFECT_CMD_DISABLE) { |
Eric Laurent | e65280c | 2010-09-02 11:56:55 -0700 | [diff] [blame] | 7036 | *(int *)pReplyData = NO_ERROR; |
Eric Laurent | 65b6545 | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 7037 | return disable(); |
| 7038 | } |
| 7039 | |
| 7040 | return mEffect->command(cmdCode, cmdSize, pCmdData, replySize, pReplyData); |
| 7041 | } |
| 7042 | |
| 7043 | sp<IMemory> AudioFlinger::EffectHandle::getCblk() const { |
| 7044 | return mCblkMemory; |
| 7045 | } |
| 7046 | |
Eric Laurent | f82fccd | 2011-07-27 19:49:51 -0700 | [diff] [blame] | 7047 | void AudioFlinger::EffectHandle::setControl(bool hasControl, bool signal, bool enabled) |
Eric Laurent | 65b6545 | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 7048 | { |
Steve Block | 71f2cf1 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 7049 | ALOGV("setControl %p control %d", this, hasControl); |
Eric Laurent | 65b6545 | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 7050 | |
| 7051 | mHasControl = hasControl; |
Eric Laurent | f82fccd | 2011-07-27 19:49:51 -0700 | [diff] [blame] | 7052 | mEnabled = enabled; |
| 7053 | |
Eric Laurent | 65b6545 | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 7054 | if (signal && mEffectClient != 0) { |
| 7055 | mEffectClient->controlStatusChanged(hasControl); |
| 7056 | } |
| 7057 | } |
| 7058 | |
Eric Laurent | a4c72ac | 2010-07-28 05:40:18 -0700 | [diff] [blame] | 7059 | void AudioFlinger::EffectHandle::commandExecuted(uint32_t cmdCode, |
| 7060 | uint32_t cmdSize, |
| 7061 | void *pCmdData, |
| 7062 | uint32_t replySize, |
| 7063 | void *pReplyData) |
Eric Laurent | 65b6545 | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 7064 | { |
| 7065 | if (mEffectClient != 0) { |
| 7066 | mEffectClient->commandExecuted(cmdCode, cmdSize, pCmdData, replySize, pReplyData); |
| 7067 | } |
| 7068 | } |
| 7069 | |
| 7070 | |
| 7071 | |
| 7072 | void AudioFlinger::EffectHandle::setEnabled(bool enabled) |
| 7073 | { |
| 7074 | if (mEffectClient != 0) { |
| 7075 | mEffectClient->enableStatusChanged(enabled); |
| 7076 | } |
| 7077 | } |
| 7078 | |
| 7079 | status_t AudioFlinger::EffectHandle::onTransact( |
| 7080 | uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags) |
| 7081 | { |
| 7082 | return BnEffect::onTransact(code, data, reply, flags); |
| 7083 | } |
| 7084 | |
| 7085 | |
| 7086 | void AudioFlinger::EffectHandle::dump(char* buffer, size_t size) |
| 7087 | { |
Marco Nelissen | c74b93f | 2011-08-02 13:33:41 -0700 | [diff] [blame] | 7088 | bool locked = mCblk ? tryLock(mCblk->lock) : false; |
Eric Laurent | 65b6545 | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 7089 | |
| 7090 | snprintf(buffer, size, "\t\t\t%05d %05d %01u %01u %05u %05u\n", |
| 7091 | (mClient == NULL) ? getpid() : mClient->pid(), |
| 7092 | mPriority, |
| 7093 | mHasControl, |
| 7094 | !locked, |
Marco Nelissen | c74b93f | 2011-08-02 13:33:41 -0700 | [diff] [blame] | 7095 | mCblk ? mCblk->clientIndex : 0, |
| 7096 | mCblk ? mCblk->serverIndex : 0 |
Eric Laurent | 65b6545 | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 7097 | ); |
| 7098 | |
| 7099 | if (locked) { |
| 7100 | mCblk->lock.unlock(); |
| 7101 | } |
| 7102 | } |
| 7103 | |
| 7104 | #undef LOG_TAG |
| 7105 | #define LOG_TAG "AudioFlinger::EffectChain" |
| 7106 | |
| 7107 | AudioFlinger::EffectChain::EffectChain(const wp<ThreadBase>& wThread, |
| 7108 | int sessionId) |
Eric Laurent | f9c361d | 2011-11-11 15:42:52 -0800 | [diff] [blame] | 7109 | : mThread(wThread), mSessionId(sessionId), mActiveTrackCnt(0), mTrackCnt(0), mTailBufferCount(0), |
Eric Laurent | 90681d6 | 2011-05-09 12:09:06 -0700 | [diff] [blame] | 7110 | mOwnInBuffer(false), mVolumeCtrlIdx(-1), mLeftVolume(UINT_MAX), mRightVolume(UINT_MAX), |
| 7111 | mNewLeftVolume(UINT_MAX), mNewRightVolume(UINT_MAX) |
Eric Laurent | 65b6545 | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 7112 | { |
Dima Zavin | 24fc2fb | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 7113 | mStrategy = AudioSystem::getStrategyForStream(AUDIO_STREAM_MUSIC); |
Eric Laurent | f9c361d | 2011-11-11 15:42:52 -0800 | [diff] [blame] | 7114 | sp<ThreadBase> thread = mThread.promote(); |
| 7115 | if (thread == 0) { |
| 7116 | return; |
| 7117 | } |
| 7118 | mMaxTailBuffers = ((kProcessTailDurationMs * thread->sampleRate()) / 1000) / |
| 7119 | thread->frameCount(); |
Eric Laurent | 65b6545 | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 7120 | } |
| 7121 | |
| 7122 | AudioFlinger::EffectChain::~EffectChain() |
| 7123 | { |
| 7124 | if (mOwnInBuffer) { |
| 7125 | delete mInBuffer; |
| 7126 | } |
| 7127 | |
| 7128 | } |
| 7129 | |
Eric Laurent | f82fccd | 2011-07-27 19:49:51 -0700 | [diff] [blame] | 7130 | // getEffectFromDesc_l() must be called with ThreadBase::mLock held |
Eric Laurent | 76c40f7 | 2010-07-15 12:50:15 -0700 | [diff] [blame] | 7131 | sp<AudioFlinger::EffectModule> AudioFlinger::EffectChain::getEffectFromDesc_l(effect_descriptor_t *descriptor) |
Eric Laurent | 65b6545 | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 7132 | { |
| 7133 | sp<EffectModule> effect; |
| 7134 | size_t size = mEffects.size(); |
| 7135 | |
| 7136 | for (size_t i = 0; i < size; i++) { |
| 7137 | if (memcmp(&mEffects[i]->desc().uuid, &descriptor->uuid, sizeof(effect_uuid_t)) == 0) { |
| 7138 | effect = mEffects[i]; |
| 7139 | break; |
| 7140 | } |
| 7141 | } |
| 7142 | return effect; |
| 7143 | } |
| 7144 | |
Eric Laurent | f82fccd | 2011-07-27 19:49:51 -0700 | [diff] [blame] | 7145 | // getEffectFromId_l() must be called with ThreadBase::mLock held |
Eric Laurent | 76c40f7 | 2010-07-15 12:50:15 -0700 | [diff] [blame] | 7146 | sp<AudioFlinger::EffectModule> AudioFlinger::EffectChain::getEffectFromId_l(int id) |
Eric Laurent | 65b6545 | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 7147 | { |
| 7148 | sp<EffectModule> effect; |
| 7149 | size_t size = mEffects.size(); |
| 7150 | |
| 7151 | for (size_t i = 0; i < size; i++) { |
Eric Laurent | 8ed6ed0 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 7152 | // by convention, return first effect if id provided is 0 (0 is never a valid id) |
| 7153 | if (id == 0 || mEffects[i]->id() == id) { |
Eric Laurent | 65b6545 | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 7154 | effect = mEffects[i]; |
| 7155 | break; |
| 7156 | } |
| 7157 | } |
| 7158 | return effect; |
| 7159 | } |
| 7160 | |
Eric Laurent | f82fccd | 2011-07-27 19:49:51 -0700 | [diff] [blame] | 7161 | // getEffectFromType_l() must be called with ThreadBase::mLock held |
| 7162 | sp<AudioFlinger::EffectModule> AudioFlinger::EffectChain::getEffectFromType_l( |
| 7163 | const effect_uuid_t *type) |
| 7164 | { |
| 7165 | sp<EffectModule> effect; |
| 7166 | size_t size = mEffects.size(); |
| 7167 | |
| 7168 | for (size_t i = 0; i < size; i++) { |
| 7169 | if (memcmp(&mEffects[i]->desc().type, type, sizeof(effect_uuid_t)) == 0) { |
| 7170 | effect = mEffects[i]; |
| 7171 | break; |
| 7172 | } |
| 7173 | } |
| 7174 | return effect; |
| 7175 | } |
| 7176 | |
Eric Laurent | 65b6545 | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 7177 | // Must be called with EffectChain::mLock locked |
| 7178 | void AudioFlinger::EffectChain::process_l() |
| 7179 | { |
Eric Laurent | 4fd3ecc | 2010-09-28 14:09:57 -0700 | [diff] [blame] | 7180 | sp<ThreadBase> thread = mThread.promote(); |
| 7181 | if (thread == 0) { |
Steve Block | 8564c8d | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 7182 | ALOGW("process_l(): cannot promote mixer thread"); |
Eric Laurent | 4fd3ecc | 2010-09-28 14:09:57 -0700 | [diff] [blame] | 7183 | return; |
| 7184 | } |
Dima Zavin | 24fc2fb | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 7185 | bool isGlobalSession = (mSessionId == AUDIO_SESSION_OUTPUT_MIX) || |
| 7186 | (mSessionId == AUDIO_SESSION_OUTPUT_STAGE); |
Eric Laurent | f9c361d | 2011-11-11 15:42:52 -0800 | [diff] [blame] | 7187 | // always process effects unless no more tracks are on the session and the effect tail |
| 7188 | // has been rendered |
| 7189 | bool doProcess = true; |
Eric Laurent | 4fd3ecc | 2010-09-28 14:09:57 -0700 | [diff] [blame] | 7190 | if (!isGlobalSession) { |
Eric Laurent | f9c361d | 2011-11-11 15:42:52 -0800 | [diff] [blame] | 7191 | bool tracksOnSession = (trackCnt() != 0); |
Eric Laurent | 90681d6 | 2011-05-09 12:09:06 -0700 | [diff] [blame] | 7192 | |
Eric Laurent | f9c361d | 2011-11-11 15:42:52 -0800 | [diff] [blame] | 7193 | if (!tracksOnSession && mTailBufferCount == 0) { |
| 7194 | doProcess = false; |
| 7195 | } |
| 7196 | |
| 7197 | if (activeTrackCnt() == 0) { |
| 7198 | // if no track is active and the effect tail has not been rendered, |
| 7199 | // the input buffer must be cleared here as the mixer process will not do it |
| 7200 | if (tracksOnSession || mTailBufferCount > 0) { |
| 7201 | size_t numSamples = thread->frameCount() * thread->channelCount(); |
| 7202 | memset(mInBuffer, 0, numSamples * sizeof(int16_t)); |
| 7203 | if (mTailBufferCount > 0) { |
| 7204 | mTailBufferCount--; |
| 7205 | } |
| 7206 | } |
| 7207 | } |
Eric Laurent | 4fd3ecc | 2010-09-28 14:09:57 -0700 | [diff] [blame] | 7208 | } |
| 7209 | |
Eric Laurent | 65b6545 | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 7210 | size_t size = mEffects.size(); |
Eric Laurent | f9c361d | 2011-11-11 15:42:52 -0800 | [diff] [blame] | 7211 | if (doProcess) { |
Eric Laurent | 4fd3ecc | 2010-09-28 14:09:57 -0700 | [diff] [blame] | 7212 | for (size_t i = 0; i < size; i++) { |
| 7213 | mEffects[i]->process(); |
| 7214 | } |
Eric Laurent | 65b6545 | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 7215 | } |
Eric Laurent | 7d850f2 | 2010-07-09 13:34:17 -0700 | [diff] [blame] | 7216 | for (size_t i = 0; i < size; i++) { |
| 7217 | mEffects[i]->updateState(); |
| 7218 | } |
Eric Laurent | 65b6545 | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 7219 | } |
| 7220 | |
Eric Laurent | 76c40f7 | 2010-07-15 12:50:15 -0700 | [diff] [blame] | 7221 | // addEffect_l() must be called with PlaybackThread::mLock held |
Eric Laurent | 8ed6ed0 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 7222 | status_t AudioFlinger::EffectChain::addEffect_l(const sp<EffectModule>& effect) |
Eric Laurent | 65b6545 | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 7223 | { |
| 7224 | effect_descriptor_t desc = effect->desc(); |
| 7225 | uint32_t insertPref = desc.flags & EFFECT_FLAG_INSERT_MASK; |
| 7226 | |
| 7227 | Mutex::Autolock _l(mLock); |
Eric Laurent | 8ed6ed0 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 7228 | effect->setChain(this); |
| 7229 | sp<ThreadBase> thread = mThread.promote(); |
| 7230 | if (thread == 0) { |
| 7231 | return NO_INIT; |
| 7232 | } |
| 7233 | effect->setThread(thread); |
Eric Laurent | 65b6545 | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 7234 | |
| 7235 | if ((desc.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) { |
| 7236 | // Auxiliary effects are inserted at the beginning of mEffects vector as |
| 7237 | // they are processed first and accumulated in chain input buffer |
| 7238 | mEffects.insertAt(effect, 0); |
Eric Laurent | 8ed6ed0 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 7239 | |
Eric Laurent | 65b6545 | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 7240 | // the input buffer for auxiliary effect contains mono samples in |
| 7241 | // 32 bit format. This is to avoid saturation in AudoMixer |
| 7242 | // accumulation stage. Saturation is done in EffectModule::process() before |
| 7243 | // calling the process in effect engine |
| 7244 | size_t numSamples = thread->frameCount(); |
| 7245 | int32_t *buffer = new int32_t[numSamples]; |
| 7246 | memset(buffer, 0, numSamples * sizeof(int32_t)); |
| 7247 | effect->setInBuffer((int16_t *)buffer); |
| 7248 | // auxiliary effects output samples to chain input buffer for further processing |
| 7249 | // by insert effects |
| 7250 | effect->setOutBuffer(mInBuffer); |
| 7251 | } else { |
| 7252 | // Insert effects are inserted at the end of mEffects vector as they are processed |
| 7253 | // after track and auxiliary effects. |
Eric Laurent | 53334cd | 2010-06-23 17:38:20 -0700 | [diff] [blame] | 7254 | // Insert effect order as a function of indicated preference: |
| 7255 | // if EFFECT_FLAG_INSERT_EXCLUSIVE, insert in first position or reject if |
| 7256 | // another effect is present |
| 7257 | // else if EFFECT_FLAG_INSERT_FIRST, insert in first position or after the |
| 7258 | // last effect claiming first position |
| 7259 | // else if EFFECT_FLAG_INSERT_LAST, insert in last position or before the |
| 7260 | // first effect claiming last position |
Eric Laurent | 65b6545 | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 7261 | // else if EFFECT_FLAG_INSERT_ANY insert after first or before last |
Eric Laurent | 53334cd | 2010-06-23 17:38:20 -0700 | [diff] [blame] | 7262 | // Reject insertion if an effect with EFFECT_FLAG_INSERT_EXCLUSIVE is |
| 7263 | // already present |
Eric Laurent | 65b6545 | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 7264 | |
| 7265 | int size = (int)mEffects.size(); |
| 7266 | int idx_insert = size; |
| 7267 | int idx_insert_first = -1; |
| 7268 | int idx_insert_last = -1; |
| 7269 | |
| 7270 | for (int i = 0; i < size; i++) { |
| 7271 | effect_descriptor_t d = mEffects[i]->desc(); |
| 7272 | uint32_t iMode = d.flags & EFFECT_FLAG_TYPE_MASK; |
| 7273 | uint32_t iPref = d.flags & EFFECT_FLAG_INSERT_MASK; |
| 7274 | if (iMode == EFFECT_FLAG_TYPE_INSERT) { |
| 7275 | // check invalid effect chaining combinations |
| 7276 | if (insertPref == EFFECT_FLAG_INSERT_EXCLUSIVE || |
Eric Laurent | 53334cd | 2010-06-23 17:38:20 -0700 | [diff] [blame] | 7277 | iPref == EFFECT_FLAG_INSERT_EXCLUSIVE) { |
Steve Block | 8564c8d | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 7278 | ALOGW("addEffect_l() could not insert effect %s: exclusive conflict with %s", desc.name, d.name); |
Eric Laurent | 65b6545 | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 7279 | return INVALID_OPERATION; |
| 7280 | } |
Eric Laurent | 53334cd | 2010-06-23 17:38:20 -0700 | [diff] [blame] | 7281 | // remember position of first insert effect and by default |
| 7282 | // select this as insert position for new effect |
Eric Laurent | 65b6545 | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 7283 | if (idx_insert == size) { |
| 7284 | idx_insert = i; |
| 7285 | } |
Eric Laurent | 53334cd | 2010-06-23 17:38:20 -0700 | [diff] [blame] | 7286 | // remember position of last insert effect claiming |
| 7287 | // first position |
Eric Laurent | 65b6545 | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 7288 | if (iPref == EFFECT_FLAG_INSERT_FIRST) { |
| 7289 | idx_insert_first = i; |
| 7290 | } |
Eric Laurent | 53334cd | 2010-06-23 17:38:20 -0700 | [diff] [blame] | 7291 | // remember position of first insert effect claiming |
| 7292 | // last position |
| 7293 | if (iPref == EFFECT_FLAG_INSERT_LAST && |
| 7294 | idx_insert_last == -1) { |
Eric Laurent | 65b6545 | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 7295 | idx_insert_last = i; |
| 7296 | } |
| 7297 | } |
| 7298 | } |
| 7299 | |
Eric Laurent | 53334cd | 2010-06-23 17:38:20 -0700 | [diff] [blame] | 7300 | // modify idx_insert from first position if needed |
| 7301 | if (insertPref == EFFECT_FLAG_INSERT_LAST) { |
| 7302 | if (idx_insert_last != -1) { |
| 7303 | idx_insert = idx_insert_last; |
| 7304 | } else { |
| 7305 | idx_insert = size; |
| 7306 | } |
| 7307 | } else { |
| 7308 | if (idx_insert_first != -1) { |
| 7309 | idx_insert = idx_insert_first + 1; |
| 7310 | } |
Eric Laurent | 65b6545 | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 7311 | } |
| 7312 | |
| 7313 | // always read samples from chain input buffer |
| 7314 | effect->setInBuffer(mInBuffer); |
| 7315 | |
| 7316 | // if last effect in the chain, output samples to chain |
| 7317 | // output buffer, otherwise to chain input buffer |
| 7318 | if (idx_insert == size) { |
| 7319 | if (idx_insert != 0) { |
| 7320 | mEffects[idx_insert-1]->setOutBuffer(mInBuffer); |
| 7321 | mEffects[idx_insert-1]->configure(); |
| 7322 | } |
| 7323 | effect->setOutBuffer(mOutBuffer); |
| 7324 | } else { |
| 7325 | effect->setOutBuffer(mInBuffer); |
| 7326 | } |
Eric Laurent | 53334cd | 2010-06-23 17:38:20 -0700 | [diff] [blame] | 7327 | mEffects.insertAt(effect, idx_insert); |
Eric Laurent | 65b6545 | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 7328 | |
Steve Block | 71f2cf1 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 7329 | ALOGV("addEffect_l() effect %p, added in chain %p at rank %d", effect.get(), this, idx_insert); |
Eric Laurent | 65b6545 | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 7330 | } |
| 7331 | effect->configure(); |
| 7332 | return NO_ERROR; |
| 7333 | } |
| 7334 | |
Eric Laurent | 76c40f7 | 2010-07-15 12:50:15 -0700 | [diff] [blame] | 7335 | // removeEffect_l() must be called with PlaybackThread::mLock held |
| 7336 | size_t AudioFlinger::EffectChain::removeEffect_l(const sp<EffectModule>& effect) |
Eric Laurent | 65b6545 | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 7337 | { |
| 7338 | Mutex::Autolock _l(mLock); |
Eric Laurent | 65b6545 | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 7339 | int size = (int)mEffects.size(); |
| 7340 | int i; |
| 7341 | uint32_t type = effect->desc().flags & EFFECT_FLAG_TYPE_MASK; |
| 7342 | |
| 7343 | for (i = 0; i < size; i++) { |
| 7344 | if (effect == mEffects[i]) { |
Eric Laurent | 21b5c47 | 2011-07-26 20:54:46 -0700 | [diff] [blame] | 7345 | // calling stop here will remove pre-processing effect from the audio HAL. |
| 7346 | // This is safe as we hold the EffectChain mutex which guarantees that we are not in |
| 7347 | // the middle of a read from audio HAL |
Eric Laurent | 6fccbd0 | 2011-10-05 17:42:25 -0700 | [diff] [blame] | 7348 | if (mEffects[i]->state() == EffectModule::ACTIVE || |
| 7349 | mEffects[i]->state() == EffectModule::STOPPING) { |
| 7350 | mEffects[i]->stop(); |
| 7351 | } |
Eric Laurent | 65b6545 | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 7352 | if (type == EFFECT_FLAG_TYPE_AUXILIARY) { |
| 7353 | delete[] effect->inBuffer(); |
| 7354 | } else { |
| 7355 | if (i == size - 1 && i != 0) { |
| 7356 | mEffects[i - 1]->setOutBuffer(mOutBuffer); |
| 7357 | mEffects[i - 1]->configure(); |
| 7358 | } |
| 7359 | } |
| 7360 | mEffects.removeAt(i); |
Steve Block | 71f2cf1 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 7361 | ALOGV("removeEffect_l() effect %p, removed from chain %p at rank %d", effect.get(), this, i); |
Eric Laurent | 65b6545 | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 7362 | break; |
| 7363 | } |
| 7364 | } |
Eric Laurent | 65b6545 | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 7365 | |
| 7366 | return mEffects.size(); |
| 7367 | } |
| 7368 | |
Eric Laurent | 76c40f7 | 2010-07-15 12:50:15 -0700 | [diff] [blame] | 7369 | // setDevice_l() must be called with PlaybackThread::mLock held |
| 7370 | void AudioFlinger::EffectChain::setDevice_l(uint32_t device) |
Eric Laurent | 65b6545 | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 7371 | { |
| 7372 | size_t size = mEffects.size(); |
| 7373 | for (size_t i = 0; i < size; i++) { |
| 7374 | mEffects[i]->setDevice(device); |
| 7375 | } |
| 7376 | } |
| 7377 | |
Eric Laurent | 76c40f7 | 2010-07-15 12:50:15 -0700 | [diff] [blame] | 7378 | // setMode_l() must be called with PlaybackThread::mLock held |
Glenn Kasten | accb114 | 2012-01-04 11:00:47 -0800 | [diff] [blame] | 7379 | void AudioFlinger::EffectChain::setMode_l(audio_mode_t mode) |
Eric Laurent | 53334cd | 2010-06-23 17:38:20 -0700 | [diff] [blame] | 7380 | { |
| 7381 | size_t size = mEffects.size(); |
| 7382 | for (size_t i = 0; i < size; i++) { |
| 7383 | mEffects[i]->setMode(mode); |
| 7384 | } |
| 7385 | } |
| 7386 | |
Eric Laurent | 76c40f7 | 2010-07-15 12:50:15 -0700 | [diff] [blame] | 7387 | // setVolume_l() must be called with PlaybackThread::mLock held |
| 7388 | bool AudioFlinger::EffectChain::setVolume_l(uint32_t *left, uint32_t *right) |
Eric Laurent | 65b6545 | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 7389 | { |
| 7390 | uint32_t newLeft = *left; |
| 7391 | uint32_t newRight = *right; |
| 7392 | bool hasControl = false; |
Eric Laurent | 76c40f7 | 2010-07-15 12:50:15 -0700 | [diff] [blame] | 7393 | int ctrlIdx = -1; |
| 7394 | size_t size = mEffects.size(); |
Eric Laurent | 65b6545 | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 7395 | |
Eric Laurent | 76c40f7 | 2010-07-15 12:50:15 -0700 | [diff] [blame] | 7396 | // first update volume controller |
| 7397 | for (size_t i = size; i > 0; i--) { |
Eric Laurent | a92ebfa | 2010-08-31 13:50:07 -0700 | [diff] [blame] | 7398 | if (mEffects[i - 1]->isProcessEnabled() && |
Eric Laurent | 76c40f7 | 2010-07-15 12:50:15 -0700 | [diff] [blame] | 7399 | (mEffects[i - 1]->desc().flags & EFFECT_FLAG_VOLUME_MASK) == EFFECT_FLAG_VOLUME_CTRL) { |
| 7400 | ctrlIdx = i - 1; |
Eric Laurent | 0d7e048 | 2010-07-19 06:24:46 -0700 | [diff] [blame] | 7401 | hasControl = true; |
Eric Laurent | 76c40f7 | 2010-07-15 12:50:15 -0700 | [diff] [blame] | 7402 | break; |
| 7403 | } |
| 7404 | } |
| 7405 | |
| 7406 | if (ctrlIdx == mVolumeCtrlIdx && *left == mLeftVolume && *right == mRightVolume) { |
Eric Laurent | 0d7e048 | 2010-07-19 06:24:46 -0700 | [diff] [blame] | 7407 | if (hasControl) { |
| 7408 | *left = mNewLeftVolume; |
| 7409 | *right = mNewRightVolume; |
| 7410 | } |
| 7411 | return hasControl; |
Eric Laurent | 76c40f7 | 2010-07-15 12:50:15 -0700 | [diff] [blame] | 7412 | } |
| 7413 | |
| 7414 | mVolumeCtrlIdx = ctrlIdx; |
Eric Laurent | 0d7e048 | 2010-07-19 06:24:46 -0700 | [diff] [blame] | 7415 | mLeftVolume = newLeft; |
| 7416 | mRightVolume = newRight; |
Eric Laurent | 76c40f7 | 2010-07-15 12:50:15 -0700 | [diff] [blame] | 7417 | |
| 7418 | // second get volume update from volume controller |
| 7419 | if (ctrlIdx >= 0) { |
| 7420 | mEffects[ctrlIdx]->setVolume(&newLeft, &newRight, true); |
Eric Laurent | 0d7e048 | 2010-07-19 06:24:46 -0700 | [diff] [blame] | 7421 | mNewLeftVolume = newLeft; |
| 7422 | mNewRightVolume = newRight; |
Eric Laurent | 65b6545 | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 7423 | } |
| 7424 | // then indicate volume to all other effects in chain. |
| 7425 | // Pass altered volume to effects before volume controller |
| 7426 | // and requested volume to effects after controller |
| 7427 | uint32_t lVol = newLeft; |
| 7428 | uint32_t rVol = newRight; |
Eric Laurent | 76c40f7 | 2010-07-15 12:50:15 -0700 | [diff] [blame] | 7429 | |
Eric Laurent | 65b6545 | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 7430 | for (size_t i = 0; i < size; i++) { |
Eric Laurent | 76c40f7 | 2010-07-15 12:50:15 -0700 | [diff] [blame] | 7431 | if ((int)i == ctrlIdx) continue; |
| 7432 | // this also works for ctrlIdx == -1 when there is no volume controller |
| 7433 | if ((int)i > ctrlIdx) { |
Eric Laurent | 65b6545 | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 7434 | lVol = *left; |
| 7435 | rVol = *right; |
| 7436 | } |
| 7437 | mEffects[i]->setVolume(&lVol, &rVol, false); |
| 7438 | } |
| 7439 | *left = newLeft; |
| 7440 | *right = newRight; |
| 7441 | |
| 7442 | return hasControl; |
| 7443 | } |
| 7444 | |
Eric Laurent | 65b6545 | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 7445 | status_t AudioFlinger::EffectChain::dump(int fd, const Vector<String16>& args) |
| 7446 | { |
| 7447 | const size_t SIZE = 256; |
| 7448 | char buffer[SIZE]; |
| 7449 | String8 result; |
| 7450 | |
| 7451 | snprintf(buffer, SIZE, "Effects for session %d:\n", mSessionId); |
| 7452 | result.append(buffer); |
| 7453 | |
| 7454 | bool locked = tryLock(mLock); |
| 7455 | // failed to lock - AudioFlinger is probably deadlocked |
| 7456 | if (!locked) { |
| 7457 | result.append("\tCould not lock mutex:\n"); |
| 7458 | } |
| 7459 | |
Eric Laurent | 76c40f7 | 2010-07-15 12:50:15 -0700 | [diff] [blame] | 7460 | result.append("\tNum fx In buffer Out buffer Active tracks:\n"); |
| 7461 | snprintf(buffer, SIZE, "\t%02d 0x%08x 0x%08x %d\n", |
Eric Laurent | 65b6545 | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 7462 | mEffects.size(), |
| 7463 | (uint32_t)mInBuffer, |
| 7464 | (uint32_t)mOutBuffer, |
Eric Laurent | 65b6545 | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 7465 | mActiveTrackCnt); |
| 7466 | result.append(buffer); |
| 7467 | write(fd, result.string(), result.size()); |
| 7468 | |
| 7469 | for (size_t i = 0; i < mEffects.size(); ++i) { |
| 7470 | sp<EffectModule> effect = mEffects[i]; |
| 7471 | if (effect != 0) { |
| 7472 | effect->dump(fd, args); |
| 7473 | } |
| 7474 | } |
| 7475 | |
| 7476 | if (locked) { |
| 7477 | mLock.unlock(); |
| 7478 | } |
| 7479 | |
| 7480 | return NO_ERROR; |
| 7481 | } |
| 7482 | |
Eric Laurent | f82fccd | 2011-07-27 19:49:51 -0700 | [diff] [blame] | 7483 | // must be called with ThreadBase::mLock held |
| 7484 | void AudioFlinger::EffectChain::setEffectSuspended_l( |
| 7485 | const effect_uuid_t *type, bool suspend) |
| 7486 | { |
| 7487 | sp<SuspendedEffectDesc> desc; |
| 7488 | // use effect type UUID timelow as key as there is no real risk of identical |
| 7489 | // timeLow fields among effect type UUIDs. |
| 7490 | int index = mSuspendedEffects.indexOfKey(type->timeLow); |
| 7491 | if (suspend) { |
| 7492 | if (index >= 0) { |
| 7493 | desc = mSuspendedEffects.valueAt(index); |
| 7494 | } else { |
| 7495 | desc = new SuspendedEffectDesc(); |
| 7496 | memcpy(&desc->mType, type, sizeof(effect_uuid_t)); |
| 7497 | mSuspendedEffects.add(type->timeLow, desc); |
Steve Block | 71f2cf1 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 7498 | ALOGV("setEffectSuspended_l() add entry for %08x", type->timeLow); |
Eric Laurent | f82fccd | 2011-07-27 19:49:51 -0700 | [diff] [blame] | 7499 | } |
| 7500 | if (desc->mRefCount++ == 0) { |
| 7501 | sp<EffectModule> effect = getEffectIfEnabled(type); |
| 7502 | if (effect != 0) { |
| 7503 | desc->mEffect = effect; |
| 7504 | effect->setSuspended(true); |
| 7505 | effect->setEnabled(false); |
| 7506 | } |
| 7507 | } |
| 7508 | } else { |
| 7509 | if (index < 0) { |
| 7510 | return; |
| 7511 | } |
| 7512 | desc = mSuspendedEffects.valueAt(index); |
| 7513 | if (desc->mRefCount <= 0) { |
Steve Block | 8564c8d | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 7514 | ALOGW("setEffectSuspended_l() restore refcount should not be 0 %d", desc->mRefCount); |
Eric Laurent | f82fccd | 2011-07-27 19:49:51 -0700 | [diff] [blame] | 7515 | desc->mRefCount = 1; |
| 7516 | } |
| 7517 | if (--desc->mRefCount == 0) { |
Steve Block | 71f2cf1 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 7518 | ALOGV("setEffectSuspended_l() remove entry for %08x", mSuspendedEffects.keyAt(index)); |
Eric Laurent | f82fccd | 2011-07-27 19:49:51 -0700 | [diff] [blame] | 7519 | if (desc->mEffect != 0) { |
| 7520 | sp<EffectModule> effect = desc->mEffect.promote(); |
| 7521 | if (effect != 0) { |
| 7522 | effect->setSuspended(false); |
| 7523 | sp<EffectHandle> handle = effect->controlHandle(); |
| 7524 | if (handle != 0) { |
| 7525 | effect->setEnabled(handle->enabled()); |
| 7526 | } |
| 7527 | } |
| 7528 | desc->mEffect.clear(); |
| 7529 | } |
| 7530 | mSuspendedEffects.removeItemsAt(index); |
| 7531 | } |
| 7532 | } |
| 7533 | } |
| 7534 | |
| 7535 | // must be called with ThreadBase::mLock held |
| 7536 | void AudioFlinger::EffectChain::setEffectSuspendedAll_l(bool suspend) |
| 7537 | { |
| 7538 | sp<SuspendedEffectDesc> desc; |
| 7539 | |
| 7540 | int index = mSuspendedEffects.indexOfKey((int)kKeyForSuspendAll); |
| 7541 | if (suspend) { |
| 7542 | if (index >= 0) { |
| 7543 | desc = mSuspendedEffects.valueAt(index); |
| 7544 | } else { |
| 7545 | desc = new SuspendedEffectDesc(); |
| 7546 | mSuspendedEffects.add((int)kKeyForSuspendAll, desc); |
Steve Block | 71f2cf1 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 7547 | ALOGV("setEffectSuspendedAll_l() add entry for 0"); |
Eric Laurent | f82fccd | 2011-07-27 19:49:51 -0700 | [diff] [blame] | 7548 | } |
| 7549 | if (desc->mRefCount++ == 0) { |
| 7550 | Vector< sp<EffectModule> > effects = getSuspendEligibleEffects(); |
| 7551 | for (size_t i = 0; i < effects.size(); i++) { |
| 7552 | setEffectSuspended_l(&effects[i]->desc().type, true); |
| 7553 | } |
| 7554 | } |
| 7555 | } else { |
| 7556 | if (index < 0) { |
| 7557 | return; |
| 7558 | } |
| 7559 | desc = mSuspendedEffects.valueAt(index); |
| 7560 | if (desc->mRefCount <= 0) { |
Steve Block | 8564c8d | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 7561 | ALOGW("setEffectSuspendedAll_l() restore refcount should not be 0 %d", desc->mRefCount); |
Eric Laurent | f82fccd | 2011-07-27 19:49:51 -0700 | [diff] [blame] | 7562 | desc->mRefCount = 1; |
| 7563 | } |
| 7564 | if (--desc->mRefCount == 0) { |
| 7565 | Vector<const effect_uuid_t *> types; |
| 7566 | for (size_t i = 0; i < mSuspendedEffects.size(); i++) { |
| 7567 | if (mSuspendedEffects.keyAt(i) == (int)kKeyForSuspendAll) { |
| 7568 | continue; |
| 7569 | } |
| 7570 | types.add(&mSuspendedEffects.valueAt(i)->mType); |
| 7571 | } |
| 7572 | for (size_t i = 0; i < types.size(); i++) { |
| 7573 | setEffectSuspended_l(types[i], false); |
| 7574 | } |
Steve Block | 71f2cf1 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 7575 | ALOGV("setEffectSuspendedAll_l() remove entry for %08x", mSuspendedEffects.keyAt(index)); |
Eric Laurent | f82fccd | 2011-07-27 19:49:51 -0700 | [diff] [blame] | 7576 | mSuspendedEffects.removeItem((int)kKeyForSuspendAll); |
| 7577 | } |
| 7578 | } |
| 7579 | } |
| 7580 | |
Eric Laurent | 5e7acae | 2011-09-23 08:40:41 -0700 | [diff] [blame] | 7581 | |
| 7582 | // The volume effect is used for automated tests only |
| 7583 | #ifndef OPENSL_ES_H_ |
| 7584 | static const effect_uuid_t SL_IID_VOLUME_ = { 0x09e8ede0, 0xddde, 0x11db, 0xb4f6, |
| 7585 | { 0x00, 0x02, 0xa5, 0xd5, 0xc5, 0x1b } }; |
| 7586 | const effect_uuid_t * const SL_IID_VOLUME = &SL_IID_VOLUME_; |
| 7587 | #endif //OPENSL_ES_H_ |
| 7588 | |
Eric Laurent | 6752ec8 | 2011-08-10 10:37:50 -0700 | [diff] [blame] | 7589 | bool AudioFlinger::EffectChain::isEffectEligibleForSuspend(const effect_descriptor_t& desc) |
| 7590 | { |
| 7591 | // auxiliary effects and visualizer are never suspended on output mix |
| 7592 | if ((mSessionId == AUDIO_SESSION_OUTPUT_MIX) && |
| 7593 | (((desc.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) || |
Eric Laurent | 5e7acae | 2011-09-23 08:40:41 -0700 | [diff] [blame] | 7594 | (memcmp(&desc.type, SL_IID_VISUALIZATION, sizeof(effect_uuid_t)) == 0) || |
| 7595 | (memcmp(&desc.type, SL_IID_VOLUME, sizeof(effect_uuid_t)) == 0))) { |
Eric Laurent | 6752ec8 | 2011-08-10 10:37:50 -0700 | [diff] [blame] | 7596 | return false; |
| 7597 | } |
| 7598 | return true; |
| 7599 | } |
| 7600 | |
Eric Laurent | f82fccd | 2011-07-27 19:49:51 -0700 | [diff] [blame] | 7601 | Vector< sp<AudioFlinger::EffectModule> > AudioFlinger::EffectChain::getSuspendEligibleEffects() |
| 7602 | { |
| 7603 | Vector< sp<EffectModule> > effects; |
| 7604 | for (size_t i = 0; i < mEffects.size(); i++) { |
Eric Laurent | 6752ec8 | 2011-08-10 10:37:50 -0700 | [diff] [blame] | 7605 | if (!isEffectEligibleForSuspend(mEffects[i]->desc())) { |
Eric Laurent | f82fccd | 2011-07-27 19:49:51 -0700 | [diff] [blame] | 7606 | continue; |
| 7607 | } |
| 7608 | effects.add(mEffects[i]); |
| 7609 | } |
| 7610 | return effects; |
| 7611 | } |
| 7612 | |
| 7613 | sp<AudioFlinger::EffectModule> AudioFlinger::EffectChain::getEffectIfEnabled( |
| 7614 | const effect_uuid_t *type) |
| 7615 | { |
| 7616 | sp<EffectModule> effect; |
| 7617 | effect = getEffectFromType_l(type); |
| 7618 | if (effect != 0 && !effect->isEnabled()) { |
| 7619 | effect.clear(); |
| 7620 | } |
| 7621 | return effect; |
| 7622 | } |
| 7623 | |
| 7624 | void AudioFlinger::EffectChain::checkSuspendOnEffectEnabled(const sp<EffectModule>& effect, |
| 7625 | bool enabled) |
| 7626 | { |
| 7627 | int index = mSuspendedEffects.indexOfKey(effect->desc().type.timeLow); |
| 7628 | if (enabled) { |
| 7629 | if (index < 0) { |
| 7630 | // if the effect is not suspend check if all effects are suspended |
| 7631 | index = mSuspendedEffects.indexOfKey((int)kKeyForSuspendAll); |
| 7632 | if (index < 0) { |
| 7633 | return; |
| 7634 | } |
Eric Laurent | 6752ec8 | 2011-08-10 10:37:50 -0700 | [diff] [blame] | 7635 | if (!isEffectEligibleForSuspend(effect->desc())) { |
| 7636 | return; |
| 7637 | } |
Eric Laurent | f82fccd | 2011-07-27 19:49:51 -0700 | [diff] [blame] | 7638 | setEffectSuspended_l(&effect->desc().type, enabled); |
| 7639 | index = mSuspendedEffects.indexOfKey(effect->desc().type.timeLow); |
Eric Laurent | 6752ec8 | 2011-08-10 10:37:50 -0700 | [diff] [blame] | 7640 | if (index < 0) { |
Steve Block | 8564c8d | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 7641 | ALOGW("checkSuspendOnEffectEnabled() Fx should be suspended here!"); |
Eric Laurent | 6752ec8 | 2011-08-10 10:37:50 -0700 | [diff] [blame] | 7642 | return; |
| 7643 | } |
Eric Laurent | f82fccd | 2011-07-27 19:49:51 -0700 | [diff] [blame] | 7644 | } |
Steve Block | 71f2cf1 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 7645 | ALOGV("checkSuspendOnEffectEnabled() enable suspending fx %08x", |
Eric Laurent | f82fccd | 2011-07-27 19:49:51 -0700 | [diff] [blame] | 7646 | effect->desc().type.timeLow); |
| 7647 | sp<SuspendedEffectDesc> desc = mSuspendedEffects.valueAt(index); |
| 7648 | // if effect is requested to suspended but was not yet enabled, supend it now. |
| 7649 | if (desc->mEffect == 0) { |
| 7650 | desc->mEffect = effect; |
| 7651 | effect->setEnabled(false); |
| 7652 | effect->setSuspended(true); |
| 7653 | } |
| 7654 | } else { |
| 7655 | if (index < 0) { |
| 7656 | return; |
| 7657 | } |
Steve Block | 71f2cf1 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 7658 | ALOGV("checkSuspendOnEffectEnabled() disable restoring fx %08x", |
Eric Laurent | f82fccd | 2011-07-27 19:49:51 -0700 | [diff] [blame] | 7659 | effect->desc().type.timeLow); |
| 7660 | sp<SuspendedEffectDesc> desc = mSuspendedEffects.valueAt(index); |
| 7661 | desc->mEffect.clear(); |
| 7662 | effect->setSuspended(false); |
| 7663 | } |
| 7664 | } |
| 7665 | |
Eric Laurent | 65b6545 | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 7666 | #undef LOG_TAG |
| 7667 | #define LOG_TAG "AudioFlinger" |
| 7668 | |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 7669 | // ---------------------------------------------------------------------------- |
| 7670 | |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 7671 | status_t AudioFlinger::onTransact( |
| 7672 | uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags) |
| 7673 | { |
| 7674 | return BnAudioFlinger::onTransact(code, data, reply, flags); |
| 7675 | } |
| 7676 | |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 7677 | }; // namespace android |